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, 59 Temple Place - Suite 330,
20 Boston, MA 02111-1307, USA. */
23 /* This file is part of the C++ front end.
24 It contains routines to build C++ expressions given their operands,
25 including computing the types of the result, C and C++ specific error
26 checks, and some optimization.
28 There are also routines to build RETURN_STMT nodes and CASE_STMT nodes,
29 and to process initializations in declarations (since they work
30 like a strange sort of assignment). */
33 extern void warning ();
43 int mark_addressable ();
44 static tree
convert_for_assignment ();
45 /* static */ tree
convert_for_initialization ();
46 extern tree
shorten_compare ();
47 extern void binary_op_error ();
48 static tree
pointer_int_sum ();
49 static tree
pointer_diff ();
50 static tree
convert_sequence ();
51 /* static */ tree
unary_complex_lvalue ();
52 static tree get_delta_difference
PROTO((tree
, tree
, int));
54 extern rtx original_result_rtx
;
55 extern int warn_synth
;
57 /* Return the target type of TYPE, which meas return T for:
58 T*, T&, T[], T (...), and otherwise, just T. */
64 if (TREE_CODE (type
) == REFERENCE_TYPE
)
65 type
= TREE_TYPE (type
);
66 while (TREE_CODE (type
) == POINTER_TYPE
67 || TREE_CODE (type
) == ARRAY_TYPE
68 || TREE_CODE (type
) == FUNCTION_TYPE
69 || TREE_CODE (type
) == METHOD_TYPE
70 || TREE_CODE (type
) == OFFSET_TYPE
)
71 type
= TREE_TYPE (type
);
75 /* Do `exp = require_complete_type (exp);' to make sure exp
76 does not have an incomplete type. (That includes void types.) */
79 require_complete_type (value
)
82 tree type
= TREE_TYPE (value
);
84 /* First, detect a valid value with a complete type. */
85 if (TYPE_SIZE (type
) != 0
86 && type
!= void_type_node
87 && ! (TYPE_LANG_SPECIFIC (type
)
88 && (IS_SIGNATURE_POINTER (type
) || IS_SIGNATURE_REFERENCE (type
))
89 && TYPE_SIZE (SIGNATURE_TYPE (type
)) == 0))
92 /* If we see X::Y, we build an OFFSET_TYPE which has
93 not been laid out. Try to avoid an error by interpreting
94 it as this->X::Y, if reasonable. */
95 if (TREE_CODE (value
) == OFFSET_REF
97 && TREE_OPERAND (value
, 0) == C_C_D
)
99 tree base
, member
= TREE_OPERAND (value
, 1);
100 tree basetype
= TYPE_OFFSET_BASETYPE (type
);
101 my_friendly_assert (TREE_CODE (member
) == FIELD_DECL
, 305);
102 base
= convert_pointer_to (basetype
, current_class_decl
);
103 value
= build (COMPONENT_REF
, TREE_TYPE (member
),
104 build_indirect_ref (base
, NULL_PTR
), member
);
105 return require_complete_type (value
);
108 incomplete_type_error (value
, type
);
109 return error_mark_node
;
112 /* Return truthvalue of whether type of EXP is instantiated. */
117 return (TREE_CODE (exp
) == TREE_LIST
118 || TREE_TYPE (exp
) == unknown_type_node
119 || (TREE_CODE (TREE_TYPE (exp
)) == OFFSET_TYPE
120 && TREE_TYPE (TREE_TYPE (exp
)) == unknown_type_node
));
123 /* Return truthvalue of whether T is function (or pfn) type. */
128 return (TREE_CODE (t
) == FUNCTION_TYPE
|| TREE_CODE (t
) == METHOD_TYPE
129 || (TREE_CODE (t
) == POINTER_TYPE
130 && (TREE_CODE (TREE_TYPE (t
)) == FUNCTION_TYPE
131 || TREE_CODE (TREE_TYPE (t
)) == METHOD_TYPE
)));
134 /* Do `exp = require_instantiated_type (type, exp);' to make sure EXP
135 does not have an uninstantiated type.
136 TYPE is type to instantiate with, if uninstantiated. */
138 require_instantiated_type (type
, exp
, errval
)
139 tree type
, exp
, errval
;
141 if (TREE_TYPE (exp
) == NULL_TREE
)
143 error ("argument list may not have an initializer list");
147 if (TREE_TYPE (exp
) == unknown_type_node
148 || (TREE_CODE (TREE_TYPE (exp
)) == OFFSET_TYPE
149 && TREE_TYPE (TREE_TYPE (exp
)) == unknown_type_node
))
151 exp
= instantiate_type (type
, exp
, 1);
152 if (TREE_TYPE (exp
) == error_mark_node
)
158 /* Return a variant of TYPE which has all the type qualifiers of LIKE
159 as well as those of TYPE. */
162 qualify_type (type
, like
)
165 int constflag
= TYPE_READONLY (type
) || TYPE_READONLY (like
);
166 int volflag
= TYPE_VOLATILE (type
) || TYPE_VOLATILE (like
);
167 /* @@ Must do member pointers here. */
168 return cp_build_type_variant (type
, constflag
, volflag
);
171 /* Return the common type of two parameter lists.
172 We assume that comptypes has already been done and returned 1;
173 if that isn't so, this may crash.
175 As an optimization, free the space we allocate if the parameter
176 lists are already common. */
182 tree oldargs
= p1
, newargs
, n
;
185 char *first_obj
= (char *) oballoc (0);
187 len
= list_length (p1
);
188 newargs
= tree_last (p1
);
190 if (newargs
== void_list_node
)
199 newargs
= tree_cons (NULL_TREE
, NULL_TREE
, newargs
);
204 p1
= TREE_CHAIN (p1
), p2
= TREE_CHAIN (p2
), n
= TREE_CHAIN (n
), i
++)
206 if (TREE_PURPOSE (p1
) && !TREE_PURPOSE (p2
))
208 TREE_PURPOSE (n
) = TREE_PURPOSE (p1
);
211 else if (! TREE_PURPOSE (p1
))
213 if (TREE_PURPOSE (p2
))
215 TREE_PURPOSE (n
) = TREE_PURPOSE (p2
);
221 if (1 != simple_cst_equal (TREE_PURPOSE (p1
), TREE_PURPOSE (p2
)))
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 list. */
289 /* ??? For the moment we punt on the issue of attrs with args. */
291 if (list_length (a1
) < list_length (a2
))
292 attributes
= a2
, a2
= a1
;
294 for (; a2
; a2
= TREE_CHAIN (a2
))
295 if (lookup_attribute (IDENTIFIER_POINTER (TREE_PURPOSE (a2
)),
296 attributes
) == NULL_TREE
)
299 TREE_CHAIN (a1
) = attributes
;
305 /* Treat an enum type as the unsigned integer type of the same width. */
307 if (TREE_CODE (t1
) == ENUMERAL_TYPE
)
308 t1
= type_for_size (TYPE_PRECISION (t1
), 1);
309 if (TREE_CODE (t2
) == ENUMERAL_TYPE
)
310 t2
= type_for_size (TYPE_PRECISION (t2
), 1);
312 code1
= TREE_CODE (t1
);
313 code2
= TREE_CODE (t2
);
319 /* If only one is real, use it as the result. */
321 if (code1
== REAL_TYPE
&& code2
!= REAL_TYPE
)
322 return build_type_attribute_variant (t1
, attributes
);
324 if (code2
== REAL_TYPE
&& code1
!= REAL_TYPE
)
325 return build_type_attribute_variant (t2
, attributes
);
327 /* Both real or both integers; use the one with greater precision. */
329 if (TYPE_PRECISION (t1
) > TYPE_PRECISION (t2
))
330 return build_type_attribute_variant (t1
, attributes
);
331 else if (TYPE_PRECISION (t2
) > TYPE_PRECISION (t1
))
332 return build_type_attribute_variant (t2
, attributes
);
334 /* Same precision. Prefer longs to ints even when same size. */
336 if (TYPE_MAIN_VARIANT (t1
) == long_unsigned_type_node
337 || TYPE_MAIN_VARIANT (t2
) == long_unsigned_type_node
)
338 return build_type_attribute_variant (long_unsigned_type_node
,
341 if (TYPE_MAIN_VARIANT (t1
) == long_integer_type_node
342 || TYPE_MAIN_VARIANT (t2
) == long_integer_type_node
)
344 /* But preserve unsignedness from the other type,
345 since long cannot hold all the values of an unsigned int. */
346 if (TREE_UNSIGNED (t1
) || TREE_UNSIGNED (t2
))
347 t1
= long_unsigned_type_node
;
349 t1
= long_integer_type_node
;
350 return build_type_attribute_variant (t1
, attributes
);
353 if (TYPE_MAIN_VARIANT (t1
) == long_double_type_node
354 || TYPE_MAIN_VARIANT (t2
) == long_double_type_node
)
355 return build_type_attribute_variant (long_double_type_node
,
358 /* Otherwise prefer the unsigned one. */
360 if (TREE_UNSIGNED (t1
))
361 return build_type_attribute_variant (t1
, attributes
);
363 return build_type_attribute_variant (t2
, attributes
);
367 /* For two pointers, do this recursively on the target type,
368 and combine the qualifiers of the two types' targets. */
369 /* This code was turned off; I don't know why.
370 But ANSI C++ specifies doing this with the qualifiers.
371 So I turned it on again. */
373 tree tt1
= TYPE_MAIN_VARIANT (TREE_TYPE (t1
));
374 tree tt2
= TYPE_MAIN_VARIANT (TREE_TYPE (t2
));
376 = TYPE_READONLY (TREE_TYPE (t1
)) || TYPE_READONLY (TREE_TYPE (t2
));
378 = TYPE_VOLATILE (TREE_TYPE (t1
)) || TYPE_VOLATILE (TREE_TYPE (t2
));
383 else if (tt1
== void_type_node
|| tt2
== void_type_node
)
384 target
= void_type_node
;
386 target
= common_type (tt1
, tt2
);
388 target
= cp_build_type_variant (target
, constp
, volatilep
);
389 if (code1
== POINTER_TYPE
)
390 t1
= build_pointer_type (target
);
392 t1
= build_reference_type (target
);
393 t1
= build_type_attribute_variant (t1
, attributes
);
395 if (TREE_CODE (target
) == METHOD_TYPE
)
396 t1
= build_ptrmemfunc_type (t1
);
402 t1
= build_pointer_type (common_type (TREE_TYPE (t1
), TREE_TYPE (t2
)));
403 return build_type_attribute_variant (t1
, attributes
);
406 t1
= build_reference_type (common_type (TREE_TYPE (t1
), TREE_TYPE (t2
)));
407 return build_type_attribute_variant (t1
, attributes
);
412 tree elt
= common_type (TREE_TYPE (t1
), TREE_TYPE (t2
));
413 /* Save space: see if the result is identical to one of the args. */
414 if (elt
== TREE_TYPE (t1
) && TYPE_DOMAIN (t1
))
415 return build_type_attribute_variant (t1
, attributes
);
416 if (elt
== TREE_TYPE (t2
) && TYPE_DOMAIN (t2
))
417 return build_type_attribute_variant (t2
, attributes
);
418 /* Merge the element types, and have a size if either arg has one. */
419 t1
= build_cplus_array_type (elt
, TYPE_DOMAIN (TYPE_DOMAIN (t1
) ? t1
: t2
));
420 return build_type_attribute_variant (t1
, attributes
);
424 /* Function types: prefer the one that specified arg types.
425 If both do, merge the arg types. Also merge the return types. */
427 tree valtype
= common_type (TREE_TYPE (t1
), TREE_TYPE (t2
));
428 tree p1
= TYPE_ARG_TYPES (t1
);
429 tree p2
= TYPE_ARG_TYPES (t2
);
432 /* Save space: see if the result is identical to one of the args. */
433 if (valtype
== TREE_TYPE (t1
) && ! p2
)
434 return build_type_attribute_variant (t1
, attributes
);
435 if (valtype
== TREE_TYPE (t2
) && ! p1
)
436 return build_type_attribute_variant (t2
, attributes
);
438 /* Simple way if one arg fails to specify argument types. */
439 if (p1
== NULL_TREE
|| TREE_VALUE (p1
) == void_type_node
)
441 rval
= build_function_type (valtype
, p2
);
442 if ((raises
= TYPE_RAISES_EXCEPTIONS (t2
)))
443 rval
= build_exception_variant (NULL_TREE
, rval
, raises
);
444 return build_type_attribute_variant (rval
, attributes
);
446 raises
= TYPE_RAISES_EXCEPTIONS (t1
);
447 if (p2
== NULL_TREE
|| TREE_VALUE (p2
) == void_type_node
)
449 rval
= build_function_type (valtype
, p1
);
451 rval
= build_exception_variant (NULL_TREE
, rval
, raises
);
452 return build_type_attribute_variant (rval
, attributes
);
455 rval
= build_function_type (valtype
, commonparms (p1
, p2
));
456 rval
= build_exception_variant (NULL_TREE
, rval
, raises
);
457 return build_type_attribute_variant (rval
, attributes
);
462 my_friendly_assert (TYPE_MAIN_VARIANT (t1
) == t1
463 && TYPE_MAIN_VARIANT (t2
) == t2
, 306);
465 if (DERIVED_FROM_P (t1
, t2
) && binfo_or_else (t1
, t2
))
466 return build_type_attribute_variant (t1
, attributes
);
467 else if (binfo_or_else (t2
, t1
))
468 return build_type_attribute_variant (t2
, attributes
);
470 compiler_error ("common_type called with uncommon aggregate types");
473 if (TREE_CODE (TREE_TYPE (t1
)) == TREE_CODE (TREE_TYPE (t2
)))
475 /* Get this value the long way, since TYPE_METHOD_BASETYPE
476 is just the main variant of this. */
480 tree b1
= TYPE_OFFSET_BASETYPE (t1
);
481 tree b2
= TYPE_OFFSET_BASETYPE (t2
);
483 if (DERIVED_FROM_P (b1
, b2
) && binfo_or_else (b1
, b2
))
484 basetype
= TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (t2
)));
487 if (binfo_or_else (b2
, b1
) == NULL_TREE
)
488 compiler_error ("common_type called with uncommon method types");
489 basetype
= TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (t1
)));
492 raises
= TYPE_RAISES_EXCEPTIONS (t1
);
494 /* If this was a member function type, get back to the
495 original type of type member function (i.e., without
496 the class instance variable up front. */
497 t1
= build_function_type (TREE_TYPE (t1
), TREE_CHAIN (TYPE_ARG_TYPES (t1
)));
498 t2
= build_function_type (TREE_TYPE (t2
), TREE_CHAIN (TYPE_ARG_TYPES (t2
)));
499 t3
= common_type (t1
, t2
);
500 t3
= build_cplus_method_type (basetype
, TREE_TYPE (t3
), TYPE_ARG_TYPES (t3
));
501 t1
= build_exception_variant (basetype
, t3
, raises
);
504 compiler_error ("common_type called with uncommon method types");
506 return build_type_attribute_variant (t1
, attributes
);
509 if (TREE_TYPE (t1
) == TREE_TYPE (t2
))
511 tree b1
= TYPE_OFFSET_BASETYPE (t1
);
512 tree b2
= TYPE_OFFSET_BASETYPE (t2
);
514 if (DERIVED_FROM_P (b1
, b2
) && binfo_or_else (b1
, b2
))
515 return build_type_attribute_variant (t2
, attributes
);
516 else if (binfo_or_else (b2
, b1
))
517 return build_type_attribute_variant (t1
, attributes
);
519 compiler_error ("common_type called with uncommon member types");
522 return build_type_attribute_variant (t1
, attributes
);
526 /* Return 1 if TYPE1 and TYPE2 raise the same exceptions. */
528 compexcepttypes (t1
, t2
, strict
)
532 return TYPE_RAISES_EXCEPTIONS (t1
) == TYPE_RAISES_EXCEPTIONS (t2
);
536 comp_array_types (cmp
, t1
, t2
, strict
)
537 register int (*cmp
)();
541 tree d1
= TYPE_DOMAIN (t1
);
542 tree d2
= TYPE_DOMAIN (t2
);
544 /* Target types must match incl. qualifiers. */
545 if (!(TREE_TYPE (t1
) == TREE_TYPE (t2
)
546 || (*cmp
) (TREE_TYPE (t1
), TREE_TYPE (t2
), strict
)))
549 /* Sizes must match unless one is missing or variable. */
550 if (d1
== 0 || d2
== 0 || d1
== d2
551 || TREE_CODE (TYPE_MIN_VALUE (d1
)) != INTEGER_CST
552 || TREE_CODE (TYPE_MIN_VALUE (d2
)) != INTEGER_CST
553 || TREE_CODE (TYPE_MAX_VALUE (d1
)) != INTEGER_CST
554 || TREE_CODE (TYPE_MAX_VALUE (d2
)) != INTEGER_CST
)
557 return ((TREE_INT_CST_LOW (TYPE_MIN_VALUE (d1
))
558 == TREE_INT_CST_LOW (TYPE_MIN_VALUE (d2
)))
559 && (TREE_INT_CST_HIGH (TYPE_MIN_VALUE (d1
))
560 == TREE_INT_CST_HIGH (TYPE_MIN_VALUE (d2
)))
561 && (TREE_INT_CST_LOW (TYPE_MAX_VALUE (d1
))
562 == TREE_INT_CST_LOW (TYPE_MAX_VALUE (d2
)))
563 && (TREE_INT_CST_HIGH (TYPE_MAX_VALUE (d1
))
564 == TREE_INT_CST_HIGH (TYPE_MAX_VALUE (d2
))));
567 /* Return 1 if TYPE1 and TYPE2 are compatible types for assignment
568 or various other operations. This is what ANSI C++ speaks of as
571 For C++: argument STRICT says we should be strict about this
574 2 : strict, except that if one type is a reference and
575 the other is not, compare the target type of the
576 reference to the type that's not a reference (ARM, p308).
577 This is used for checking for invalid overloading.
578 1 : strict (compared according to ANSI C)
579 This is used for checking whether two function decls match.
580 0 : <= (compared according to C++)
581 -1: <= or >= (relaxed)
583 Otherwise, pointers involving base classes and derived classes
584 can be mixed as valid: i.e. a pointer to a base class may be assigned
585 to a pointer to one of its derived classes, as per C++. A pointer to
586 a derived class may be passed as a parameter to a function expecting a
587 pointer to a base classes. These allowances do not commute. In this
588 case, TYPE1 is assumed to be the base class, and TYPE2 is assumed to
589 be the derived class. */
591 comptypes (type1
, type2
, strict
)
595 register tree t1
= type1
;
596 register tree t2
= type2
;
599 /* Suppress errors caused by previously reported errors */
604 /* This should never happen. */
605 my_friendly_assert (t1
!= error_mark_node
, 307);
607 if (t2
== error_mark_node
)
612 /* Treat an enum type as the unsigned integer type of the same width. */
614 if (TREE_CODE (t1
) == ENUMERAL_TYPE
)
615 t1
= type_for_size (TYPE_PRECISION (t1
), 1);
616 if (TREE_CODE (t2
) == ENUMERAL_TYPE
)
617 t2
= type_for_size (TYPE_PRECISION (t2
), 1);
623 /* Different classes of types can't be compatible. */
625 if (TREE_CODE (t1
) != TREE_CODE (t2
))
628 && ((TREE_CODE (t1
) == REFERENCE_TYPE
)
629 ^ (TREE_CODE (t2
) == REFERENCE_TYPE
)))
631 if (TREE_CODE (t1
) == REFERENCE_TYPE
)
632 return comptypes (TREE_TYPE (t1
), t2
, 1);
633 return comptypes (t1
, TREE_TYPE (t2
), 1);
641 /* Qualifiers must match. */
643 if (TYPE_READONLY (t1
) != TYPE_READONLY (t2
))
645 if (TYPE_VOLATILE (t1
) != TYPE_VOLATILE (t2
))
648 /* Allow for two different type nodes which have essentially the same
649 definition. Note that we already checked for equality of the type
650 type qualifiers (just above). */
652 if (TYPE_MAIN_VARIANT (t1
) == TYPE_MAIN_VARIANT (t2
))
655 #ifdef COMP_TYPE_ATTRIBUTES
656 if (! (attrval
= COMP_TYPE_ATTRIBUTES (t1
, t2
)))
659 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
663 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
666 switch (TREE_CODE (t1
))
675 val
= (comptypes (TYPE_POINTER_TO (TYPE_OFFSET_BASETYPE (t1
)),
676 TYPE_POINTER_TO (TYPE_OFFSET_BASETYPE (t2
)), strict
)
677 && comptypes (TREE_TYPE (t1
), TREE_TYPE (t2
), strict
));
681 if (! compexcepttypes (t1
, t2
, strict
))
684 /* This case is anti-symmetrical!
685 One can pass a base member (or member function)
686 to something expecting a derived member (or member function),
687 but not vice-versa! */
689 val
= (comptypes (TYPE_POINTER_TO (TYPE_METHOD_BASETYPE (t2
)),
690 TYPE_POINTER_TO (TYPE_METHOD_BASETYPE (t1
)), strict
)
691 && comptypes (TREE_TYPE (t1
), TREE_TYPE (t2
), strict
)
692 && compparms (TREE_CHAIN (TYPE_ARG_TYPES (t1
)),
693 TREE_CHAIN (TYPE_ARG_TYPES (t2
)), strict
));
707 if (TREE_CODE (t1
) == RECORD_TYPE
&& TREE_CODE (t2
) == RECORD_TYPE
)
711 rval
= t1
== t2
|| UNIQUELY_DERIVED_FROM_P (t1
, t2
);
720 val
= UNIQUELY_DERIVED_FROM_P (t2
, t1
);
727 val
= comptypes (t1
, t2
, strict
);
731 if (! compexcepttypes (t1
, t2
, strict
))
734 val
= ((TREE_TYPE (t1
) == TREE_TYPE (t2
)
735 || comptypes (TREE_TYPE (t1
), TREE_TYPE (t2
), strict
))
736 && compparms (TYPE_ARG_TYPES (t1
), TYPE_ARG_TYPES (t2
), strict
));
740 /* Target types must match incl. qualifiers. */
741 val
= comp_array_types (comptypes
, t1
, t2
, strict
);
744 case TEMPLATE_TYPE_PARM
:
747 case UNINSTANTIATED_P_TYPE
:
748 if (UPT_TEMPLATE (t1
) != UPT_TEMPLATE (t2
))
751 int i
= TREE_VEC_LENGTH (UPT_PARMS (t1
));
752 tree
*p1
= &TREE_VEC_ELT (UPT_PARMS (t1
), 0);
753 tree
*p2
= &TREE_VEC_ELT (UPT_PARMS (t2
), 0);
757 if (TREE_CODE_CLASS (TREE_CODE (p1
[i
])) == 't')
759 if (! comptypes (p1
[i
], p2
[i
], 1))
764 if (simple_cst_equal (p1
[i
], p2
[i
]) <= 0)
771 return attrval
== 2 && val
== 1 ? 2 : val
;
774 /* Return 1 if TTL and TTR are pointers to types that are equivalent,
775 ignoring their qualifiers.
777 NPTRS is the number of pointers we can strip off and keep cool.
778 This is used to permit (for aggr A, aggr B) A, B* to convert to A*,
779 but to not permit B** to convert to A**. */
782 comp_target_types (ttl
, ttr
, nptrs
)
786 ttl
= TYPE_MAIN_VARIANT (ttl
);
787 ttr
= TYPE_MAIN_VARIANT (ttr
);
791 if (TREE_CODE (ttr
) != TREE_CODE (ttl
))
794 if (TREE_CODE (ttr
) == POINTER_TYPE
)
796 ttl
= TREE_TYPE (ttl
);
797 ttr
= TREE_TYPE (ttr
);
801 if (TREE_CODE (ttl
) == VOID_TYPE
802 && TREE_CODE (ttr
) != FUNCTION_TYPE
803 && TREE_CODE (ttr
) != METHOD_TYPE
804 && TREE_CODE (ttr
) != OFFSET_TYPE
)
806 else if (TREE_CODE (ttr
) == VOID_TYPE
807 && TREE_CODE (ttl
) != FUNCTION_TYPE
808 && TREE_CODE (ttl
) != METHOD_TYPE
809 && TREE_CODE (ttl
) != OFFSET_TYPE
)
811 else if (TREE_CODE (ttl
) == POINTER_TYPE
812 || TREE_CODE (ttl
) == ARRAY_TYPE
)
813 return comp_ptr_ttypes (ttl
, ttr
);
816 return comp_target_types (ttl
, ttr
, nptrs
- 1);
819 if (TREE_CODE (ttr
) == REFERENCE_TYPE
)
820 return comp_target_types (TREE_TYPE (ttl
), TREE_TYPE (ttr
), nptrs
);
821 if (TREE_CODE (ttr
) == ARRAY_TYPE
)
822 return comp_array_types (comp_target_types
, ttl
, ttr
, 0);
823 else if (TREE_CODE (ttr
) == FUNCTION_TYPE
|| TREE_CODE (ttr
) == METHOD_TYPE
)
824 if (comp_target_types (TREE_TYPE (ttl
), TREE_TYPE (ttr
), nptrs
))
825 switch (comp_target_parms (TYPE_ARG_TYPES (ttl
), TYPE_ARG_TYPES (ttr
), 1))
834 my_friendly_abort (112);
840 else if (TREE_CODE (ttr
) == OFFSET_TYPE
)
842 /* Contravariance: we can assign a pointer to base member to a pointer
843 to derived member. Note difference from simple pointer case, where
844 we can pass a pointer to derived to a pointer to base. */
845 if (comptypes (TYPE_OFFSET_BASETYPE (ttr
), TYPE_OFFSET_BASETYPE (ttl
), 0))
846 return comp_target_types (TREE_TYPE (ttl
), TREE_TYPE (ttr
), nptrs
);
847 else if (comptypes (TYPE_OFFSET_BASETYPE (ttl
), TYPE_OFFSET_BASETYPE (ttr
), 0)
848 && comp_target_types (TREE_TYPE (ttl
), TREE_TYPE (ttr
), nptrs
))
851 else if (IS_AGGR_TYPE (ttl
))
855 if (comptypes (TYPE_POINTER_TO (ttl
), TYPE_POINTER_TO (ttr
), 0))
857 if (comptypes (TYPE_POINTER_TO (ttr
), TYPE_POINTER_TO (ttl
), 0))
865 /* If two types share a common base type, return that basetype.
866 If there is not a unique most-derived base type, this function
867 returns ERROR_MARK_NODE. */
869 common_base_type (tt1
, tt2
)
872 tree best
= NULL_TREE
, tmp
;
875 /* If one is a baseclass of another, that's good enough. */
876 if (UNIQUELY_DERIVED_FROM_P (tt1
, tt2
))
878 if (UNIQUELY_DERIVED_FROM_P (tt2
, tt1
))
882 /* If they share a virtual baseclass, that's good enough. */
883 for (tmp
= CLASSTYPE_VBASECLASSES (tt1
); tmp
; tmp
= TREE_CHAIN (tmp
))
885 if (binfo_member (BINFO_TYPE (tmp
), CLASSTYPE_VBASECLASSES (tt2
)))
886 return BINFO_TYPE (tmp
);
890 /* Otherwise, try to find a unique baseclass of TT1
891 that is shared by TT2, and follow that down. */
892 for (i
= CLASSTYPE_N_BASECLASSES (tt1
)-1; i
>= 0; i
--)
894 tree basetype
= TYPE_BINFO_BASETYPE (tt1
, i
);
895 tree trial
= common_base_type (basetype
, tt2
);
898 if (trial
== error_mark_node
)
900 if (best
== NULL_TREE
)
902 else if (best
!= trial
)
903 return error_mark_node
;
908 for (i
= CLASSTYPE_N_BASECLASSES (tt2
)-1; i
>= 0; i
--)
910 tree basetype
= TYPE_BINFO_BASETYPE (tt2
, i
);
911 tree trial
= common_base_type (tt1
, basetype
);
914 if (trial
== error_mark_node
)
916 if (best
== NULL_TREE
)
918 else if (best
!= trial
)
919 return error_mark_node
;
925 /* Subroutines of `comptypes'. */
927 /* Return 1 if two parameter type lists PARMS1 and PARMS2
928 are equivalent in the sense that functions with those parameter types
929 can have equivalent types.
930 If either list is empty, we win.
931 Otherwise, the two lists must be equivalent, element by element.
933 C++: See comment above about TYPE1, TYPE2, STRICT.
934 If STRICT == 3, it means checking is strict, but do not compare
935 default parameter values. */
937 compparms (parms1
, parms2
, strict
)
941 register tree t1
= parms1
, t2
= parms2
;
943 /* An unspecified parmlist matches any specified parmlist
944 whose argument types don't need default promotions. */
946 if (strict
<= 0 && t1
== 0)
947 return self_promoting_args_p (t2
);
948 if (strict
< 0 && t2
== 0)
949 return self_promoting_args_p (t1
);
953 if (t1
== 0 && t2
== 0)
955 /* If one parmlist is shorter than the other,
956 they fail to match, unless STRICT is <= 0. */
957 if (t1
== 0 || t2
== 0)
964 return t1
&& TREE_PURPOSE (t1
);
966 if (! comptypes (TREE_VALUE (t2
), TREE_VALUE (t1
), strict
))
971 return t2
== void_list_node
&& TREE_PURPOSE (t1
);
972 return TREE_PURPOSE (t1
) || TREE_PURPOSE (t2
);
975 /* Default parms are not part of the type of a function. */
976 if (strict
!= 3 && TREE_PURPOSE (t1
) && TREE_PURPOSE (t2
))
978 int cmp
= simple_cst_equal (TREE_PURPOSE (t1
), TREE_PURPOSE (t2
));
980 my_friendly_abort (113);
986 t1
= TREE_CHAIN (t1
);
987 t2
= TREE_CHAIN (t2
);
991 /* This really wants return whether or not parameter type lists
992 would make their owning functions assignment compatible or not. */
994 comp_target_parms (parms1
, parms2
, strict
)
998 register tree t1
= parms1
, t2
= parms2
;
999 int warn_contravariance
= 0;
1001 /* An unspecified parmlist matches any specified parmlist
1002 whose argument types don't need default promotions.
1003 @@@ see 13.3.3 for a counterexample... */
1005 if (t1
== 0 && t2
!= 0)
1007 cp_pedwarn ("ANSI C++ prohibits conversion from `(%#T)' to `(...)'",
1009 return self_promoting_args_p (t2
);
1012 return self_promoting_args_p (t1
);
1014 for (; t1
|| t2
; t1
= TREE_CHAIN (t1
), t2
= TREE_CHAIN (t2
))
1018 /* If one parmlist is shorter than the other,
1019 they fail to match, unless STRICT is <= 0. */
1020 if (t1
== 0 || t2
== 0)
1025 return 1 + warn_contravariance
;
1026 return ((t1
&& TREE_PURPOSE (t1
)) + warn_contravariance
);
1028 p1
= TREE_VALUE (t1
);
1029 p2
= TREE_VALUE (t2
);
1033 if ((TREE_CODE (p1
) == POINTER_TYPE
&& TREE_CODE (p2
) == POINTER_TYPE
)
1034 || (TREE_CODE (p1
) == REFERENCE_TYPE
&& TREE_CODE (p2
) == REFERENCE_TYPE
))
1037 && (TYPE_MAIN_VARIANT (TREE_TYPE (p1
))
1038 == TYPE_MAIN_VARIANT (TREE_TYPE (p2
))))
1041 /* The following is wrong for contravariance,
1042 but many programs depend on it. */
1043 if (TREE_TYPE (p1
) == void_type_node
)
1045 if (TREE_TYPE (p2
) == void_type_node
)
1047 warn_contravariance
= 1;
1050 if (IS_AGGR_TYPE (TREE_TYPE (p1
)))
1052 if (comptypes (p2
, p1
, 0) == 0)
1054 if (comptypes (p1
, p2
, 0) != 0)
1055 warn_contravariance
= 1;
1062 /* Note backwards order due to contravariance. */
1063 if (comp_target_types (p2
, p1
, 1) == 0)
1065 if (comp_target_types (p1
, p2
, 1))
1067 warn_contravariance
= 1;
1073 /* What good do these cases do? */
1075 return p2
== void_type_node
&& TREE_PURPOSE (t1
);
1076 return TREE_PURPOSE (t1
) || TREE_PURPOSE (t2
);
1079 /* Target types are compatible--just make sure that if
1080 we use parameter lists, that they are ok as well. */
1081 if (TREE_CODE (p1
) == FUNCTION_TYPE
|| TREE_CODE (p1
) == METHOD_TYPE
)
1082 switch (comp_target_parms (TYPE_ARG_TYPES (p1
),
1083 TYPE_ARG_TYPES (p2
),
1091 warn_contravariance
= 1;
1094 if (TREE_PURPOSE (t1
) && TREE_PURPOSE (t2
))
1096 int cmp
= simple_cst_equal (TREE_PURPOSE (t1
), TREE_PURPOSE (t2
));
1098 my_friendly_abort (114);
1103 return 1 + warn_contravariance
;
1106 /* Return 1 if PARMS specifies a fixed number of parameters
1107 and none of their types is affected by default promotions. */
1110 self_promoting_args_p (parms
)
1114 for (t
= parms
; t
; t
= TREE_CHAIN (t
))
1116 register tree type
= TREE_VALUE (t
);
1118 if (TREE_CHAIN (t
) == 0 && type
!= void_type_node
)
1121 if (TYPE_MAIN_VARIANT (type
) == float_type_node
)
1127 if (C_PROMOTING_INTEGER_TYPE_P (type
))
1133 /* Return an unsigned type the same as TYPE in other respects.
1135 C++: must make these work for type variants as well. */
1138 unsigned_type (type
)
1141 tree type1
= TYPE_MAIN_VARIANT (type
);
1142 if (type1
== signed_char_type_node
|| type1
== char_type_node
)
1143 return unsigned_char_type_node
;
1144 if (type1
== integer_type_node
)
1145 return unsigned_type_node
;
1146 if (type1
== short_integer_type_node
)
1147 return short_unsigned_type_node
;
1148 if (type1
== long_integer_type_node
)
1149 return long_unsigned_type_node
;
1150 if (type1
== long_long_integer_type_node
)
1151 return long_long_unsigned_type_node
;
1155 /* Return a signed type the same as TYPE in other respects. */
1161 tree type1
= TYPE_MAIN_VARIANT (type
);
1162 if (type1
== unsigned_char_type_node
|| type1
== char_type_node
)
1163 return signed_char_type_node
;
1164 if (type1
== unsigned_type_node
)
1165 return integer_type_node
;
1166 if (type1
== short_unsigned_type_node
)
1167 return short_integer_type_node
;
1168 if (type1
== long_unsigned_type_node
)
1169 return long_integer_type_node
;
1170 if (type1
== long_long_unsigned_type_node
)
1171 return long_long_integer_type_node
;
1175 /* Return a type the same as TYPE except unsigned or
1176 signed according to UNSIGNEDP. */
1179 signed_or_unsigned_type (unsignedp
, type
)
1183 if (! INTEGRAL_TYPE_P (type
))
1185 if (TYPE_PRECISION (type
) == TYPE_PRECISION (signed_char_type_node
))
1186 return unsignedp
? unsigned_char_type_node
: signed_char_type_node
;
1187 if (TYPE_PRECISION (type
) == TYPE_PRECISION (integer_type_node
))
1188 return unsignedp
? unsigned_type_node
: integer_type_node
;
1189 if (TYPE_PRECISION (type
) == TYPE_PRECISION (short_integer_type_node
))
1190 return unsignedp
? short_unsigned_type_node
: short_integer_type_node
;
1191 if (TYPE_PRECISION (type
) == TYPE_PRECISION (long_integer_type_node
))
1192 return unsignedp
? long_unsigned_type_node
: long_integer_type_node
;
1193 if (TYPE_PRECISION (type
) == TYPE_PRECISION (long_long_integer_type_node
))
1194 return (unsignedp
? long_long_unsigned_type_node
1195 : long_long_integer_type_node
);
1203 enum tree_code code
= TREE_CODE (type
);
1206 if (code
== FUNCTION_TYPE
)
1208 if (pedantic
|| warn_pointer_arith
)
1209 pedwarn ("ANSI C++ forbids taking the sizeof a function type");
1210 return size_int (1);
1212 if (code
== METHOD_TYPE
)
1214 if (pedantic
|| warn_pointer_arith
)
1215 pedwarn ("ANSI C++ forbids taking the sizeof a method type");
1216 return size_int (1);
1218 if (code
== VOID_TYPE
)
1220 if (pedantic
|| warn_pointer_arith
)
1221 pedwarn ("ANSI C++ forbids taking the sizeof a void type");
1222 return size_int (1);
1224 if (code
== ERROR_MARK
)
1225 return size_int (1);
1227 /* ARM $5.3.2: ``When applied to a reference, the result is the size of the
1228 referenced object.'' */
1229 if (code
== REFERENCE_TYPE
)
1230 type
= TREE_TYPE (type
);
1232 /* We couldn't find anything in the ARM or the draft standard that says,
1233 one way or the other, if doing sizeof on something that doesn't have
1234 an object associated with it is correct or incorrect. For example, if
1235 you declare `struct S { char str[16]; };', and in your program do
1236 a `sizeof (S::str)', should we flag that as an error or should we give
1237 the size of it? Since it seems like a reasonable thing to do, we'll go
1238 with giving the value. */
1239 if (code
== OFFSET_TYPE
)
1240 type
= TREE_TYPE (type
);
1242 /* @@ This also produces an error for a signature ref.
1243 In that case we should be able to do better. */
1244 if (IS_SIGNATURE (type
))
1246 error ("`sizeof' applied to a signature type");
1247 return size_int (0);
1250 if (TYPE_SIZE (type
) == 0)
1252 error ("`sizeof' applied to an incomplete type");
1253 return size_int (0);
1256 /* Convert in case a char is more than one unit. */
1257 t
= size_binop (CEIL_DIV_EXPR
, TYPE_SIZE (type
),
1258 size_int (TYPE_PRECISION (char_type_node
)));
1259 /* size_binop does not put the constant in range, so do it now. */
1260 if (TREE_CODE (t
) == INTEGER_CST
&& force_fit_type (t
, 0))
1261 TREE_CONSTANT_OVERFLOW (t
) = TREE_OVERFLOW (t
) = 1;
1266 c_sizeof_nowarn (type
)
1269 enum tree_code code
= TREE_CODE (type
);
1272 if (code
== FUNCTION_TYPE
1273 || code
== METHOD_TYPE
1274 || code
== VOID_TYPE
1275 || code
== ERROR_MARK
)
1276 return size_int (1);
1277 if (code
== REFERENCE_TYPE
)
1278 type
= TREE_TYPE (type
);
1280 if (TYPE_SIZE (type
) == 0)
1283 /* ??? Tiemann, why have any diagnostic here?
1284 There is none in the corresponding function for C. */
1285 warning ("sizeof applied to an incomplete type");
1287 return size_int (0);
1290 /* Convert in case a char is more than one unit. */
1291 t
= size_binop (CEIL_DIV_EXPR
, TYPE_SIZE (type
),
1292 size_int (TYPE_PRECISION (char_type_node
)));
1293 force_fit_type (t
, 0);
1297 /* Implement the __alignof keyword: Return the minimum required
1298 alignment of TYPE, measured in bytes. */
1304 enum tree_code code
= TREE_CODE (type
);
1307 if (code
== FUNCTION_TYPE
|| code
== METHOD_TYPE
)
1308 return size_int (FUNCTION_BOUNDARY
/ BITS_PER_UNIT
);
1310 if (code
== VOID_TYPE
|| code
== ERROR_MARK
)
1311 return size_int (1);
1313 /* C++: this is really correct! */
1314 if (code
== REFERENCE_TYPE
)
1315 type
= TREE_TYPE (type
);
1317 /* @@ This also produces an error for a signature ref.
1318 In that case we should be able to do better. */
1319 if (IS_SIGNATURE (type
))
1321 error ("`__alignof' applied to a signature type");
1322 return size_int (1);
1325 t
= size_int (TYPE_ALIGN (type
) / BITS_PER_UNIT
);
1326 force_fit_type (t
, 0);
1330 /* Perform default promotions for C data used in expressions.
1331 Arrays and functions are converted to pointers;
1332 enumeral types or short or char, to int.
1333 In addition, manifest constants symbols are replaced by their values.
1335 C++: this will automatically bash references to their target type. */
1338 decay_conversion (exp
)
1341 register tree type
= TREE_TYPE (exp
);
1342 register enum tree_code code
= TREE_CODE (type
);
1344 if (code
== OFFSET_TYPE
/* || TREE_CODE (exp) == OFFSET_REF */ )
1346 if (TREE_CODE (exp
) == OFFSET_REF
)
1347 return decay_conversion (resolve_offset_ref (exp
));
1349 type
= TREE_TYPE (type
);
1350 code
= TREE_CODE (type
);
1353 if (code
== REFERENCE_TYPE
)
1355 exp
= convert_from_reference (exp
);
1356 type
= TREE_TYPE (exp
);
1357 code
= TREE_CODE (type
);
1360 /* Constants can be used directly unless they're not loadable. */
1361 if (TREE_CODE (exp
) == CONST_DECL
)
1362 exp
= DECL_INITIAL (exp
);
1363 /* Replace a nonvolatile const static variable with its value. */
1364 else if (TREE_READONLY_DECL_P (exp
))
1366 exp
= decl_constant_value (exp
);
1367 type
= TREE_TYPE (exp
);
1370 /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
1371 Leave such NOP_EXPRs, since RHS is being used in non-lvalue context. */
1373 if (code
== VOID_TYPE
)
1375 error ("void value not ignored as it ought to be");
1376 return error_mark_node
;
1378 if (code
== FUNCTION_TYPE
)
1380 return build_unary_op (ADDR_EXPR
, exp
, 0);
1382 if (code
== METHOD_TYPE
)
1384 if (TREE_CODE (exp
) == OFFSET_REF
)
1386 my_friendly_assert (TREE_CODE (TREE_OPERAND (exp
, 1)) == FUNCTION_DECL
,
1388 return build_unary_op (ADDR_EXPR
, TREE_OPERAND (exp
, 1), 0);
1390 return build_unary_op (ADDR_EXPR
, exp
, 0);
1392 if (code
== ARRAY_TYPE
)
1397 int constp
, volatilep
;
1399 if (TREE_CODE (exp
) == INDIRECT_REF
)
1401 /* Stripping away the INDIRECT_REF is not the right
1402 thing to do for references... */
1403 tree inner
= TREE_OPERAND (exp
, 0);
1404 if (TREE_CODE (TREE_TYPE (inner
)) == REFERENCE_TYPE
)
1406 inner
= build1 (CONVERT_EXPR
,
1407 build_pointer_type (TREE_TYPE (TREE_TYPE (inner
))),
1409 TREE_REFERENCE_EXPR (inner
) = 1;
1411 return convert (TYPE_POINTER_TO (TREE_TYPE (type
)), inner
);
1414 if (TREE_CODE (exp
) == COMPOUND_EXPR
)
1416 tree op1
= decay_conversion (TREE_OPERAND (exp
, 1));
1417 return build (COMPOUND_EXPR
, TREE_TYPE (op1
),
1418 TREE_OPERAND (exp
, 0), op1
);
1422 && ! (TREE_CODE (exp
) == CONSTRUCTOR
&& TREE_STATIC (exp
)))
1424 error ("invalid use of non-lvalue array");
1425 return error_mark_node
;
1428 constp
= volatilep
= 0;
1429 if (TREE_CODE_CLASS (TREE_CODE (exp
)) == 'r'
1430 || TREE_CODE_CLASS (TREE_CODE (exp
)) == 'd')
1432 constp
= TREE_READONLY (exp
);
1433 volatilep
= TREE_THIS_VOLATILE (exp
);
1436 restype
= TREE_TYPE (type
);
1437 if (TYPE_READONLY (type
) || TYPE_VOLATILE (type
)
1438 || constp
|| volatilep
)
1439 restype
= cp_build_type_variant (restype
,
1440 TYPE_READONLY (type
) || constp
,
1441 TYPE_VOLATILE (type
) || volatilep
);
1442 ptrtype
= build_pointer_type (restype
);
1444 if (TREE_CODE (exp
) == VAR_DECL
)
1446 /* ??? This is not really quite correct
1447 in that the type of the operand of ADDR_EXPR
1448 is not the target type of the type of the ADDR_EXPR itself.
1449 Question is, can this lossage be avoided? */
1450 adr
= build1 (ADDR_EXPR
, ptrtype
, exp
);
1451 if (mark_addressable (exp
) == 0)
1452 return error_mark_node
;
1453 TREE_CONSTANT (adr
) = staticp (exp
);
1454 TREE_SIDE_EFFECTS (adr
) = 0; /* Default would be, same as EXP. */
1457 /* This way is better for a COMPONENT_REF since it can
1458 simplify the offset for a component. */
1459 adr
= build_unary_op (ADDR_EXPR
, exp
, 1);
1460 return convert (ptrtype
, adr
);
1467 default_conversion (exp
)
1471 enum tree_code code
;
1473 exp
= decay_conversion (exp
);
1475 type
= TREE_TYPE (exp
);
1476 code
= TREE_CODE (type
);
1478 if (INTEGRAL_CODE_P (code
))
1480 tree t
= type_promotes_to (type
);
1482 return convert (t
, exp
);
1484 if (flag_traditional
1485 && TYPE_MAIN_VARIANT (type
) == float_type_node
)
1486 return convert (double_type_node
, exp
);
1492 build_object_ref (datum
, basetype
, field
)
1493 tree datum
, basetype
, field
;
1496 if (datum
== error_mark_node
)
1497 return error_mark_node
;
1499 dtype
= TREE_TYPE (datum
);
1500 if (TREE_CODE (dtype
) == REFERENCE_TYPE
)
1501 dtype
= TREE_TYPE (dtype
);
1502 if (! IS_AGGR_TYPE_CODE (TREE_CODE (dtype
)))
1504 cp_error ("request for member `%T::%D' in expression of non-aggregate type `%T'",
1505 basetype
, field
, dtype
);
1506 return error_mark_node
;
1508 else if (IS_SIGNATURE (IDENTIFIER_TYPE_VALUE (basetype
)))
1510 warning ("signature name in scope resolution ignored");
1511 return build_component_ref (datum
, field
, NULL_TREE
, 1);
1513 else if (is_aggr_typedef (basetype
, 1))
1515 tree real_basetype
= IDENTIFIER_TYPE_VALUE (basetype
);
1516 tree binfo
= binfo_or_else (real_basetype
, TREE_TYPE (datum
));
1518 return build_component_ref (build_scoped_ref (datum
, basetype
),
1521 return error_mark_node
;
1524 /* Like `build_component_ref, but uses an already found field.
1525 Must compute access for C_C_D. Otherwise, ok. */
1527 build_component_ref_1 (datum
, field
, protect
)
1531 register tree basetype
= TREE_TYPE (datum
);
1532 register enum tree_code code
= TREE_CODE (basetype
);
1535 if (code
== REFERENCE_TYPE
)
1537 datum
= convert_from_reference (datum
);
1538 basetype
= TREE_TYPE (datum
);
1539 code
= TREE_CODE (basetype
);
1542 if (! IS_AGGR_TYPE_CODE (code
))
1544 if (code
!= ERROR_MARK
)
1545 cp_error ("request for member `%D' in `%E', which is of non-aggregate type `%T'",
1546 field
, datum
, basetype
);
1547 return error_mark_node
;
1550 if (TYPE_SIZE (basetype
) == 0)
1552 incomplete_type_error (0, basetype
);
1553 return error_mark_node
;
1556 /* Look up component name in the structure type definition. */
1558 if (field
== error_mark_node
)
1559 my_friendly_abort (115);
1561 if (TREE_STATIC (field
))
1566 enum access_type access
1567 = compute_access (TYPE_BINFO (current_class_type
), field
);
1569 if (access
== access_private
)
1571 cp_error ("field `%D' is private", field
);
1572 return error_mark_node
;
1574 else if (access
== access_protected
)
1576 cp_error ("field `%D' is protected", field
);
1577 return error_mark_node
;
1581 ref
= build (COMPONENT_REF
, TREE_TYPE (field
), datum
, field
);
1583 if (TREE_READONLY (datum
) || TREE_READONLY (field
))
1584 TREE_READONLY (ref
) = 1;
1585 if (TREE_THIS_VOLATILE (datum
) || TREE_THIS_VOLATILE (field
))
1586 TREE_THIS_VOLATILE (ref
) = 1;
1587 if (DECL_MUTABLE_P (field
))
1588 TREE_READONLY (ref
) = 0;
1593 /* Given a COND_EXPR in T, return it in a form that we can, for
1594 example, use as an lvalue. This code used to be in unary_complex_lvalue,
1595 but we needed it to deal with `a = (d == c) ? b : c' expressions, where
1596 we're dealing with aggregates. So, we now call this in unary_complex_lvalue,
1597 and in build_modify_expr. The case (in particular) that led to this was
1598 with CODE == ADDR_EXPR, since it's not an lvalue when we'd get it there. */
1600 rationalize_conditional_expr (code
, t
)
1601 enum tree_code code
;
1605 build_conditional_expr (TREE_OPERAND (t
, 0),
1606 build_unary_op (code
, TREE_OPERAND (t
, 1), 0),
1607 build_unary_op (code
, TREE_OPERAND (t
, 2), 0));
1611 build_component_ref (datum
, component
, basetype_path
, protect
)
1612 tree datum
, component
, basetype_path
;
1615 register tree basetype
= TREE_TYPE (datum
);
1616 register enum tree_code code
= TREE_CODE (basetype
);
1617 register tree field
= NULL
;
1620 /* If DATUM is a COMPOUND_EXPR or COND_EXPR, move our reference inside it. */
1621 switch (TREE_CODE (datum
))
1625 tree value
= build_component_ref (TREE_OPERAND (datum
, 1), component
,
1626 basetype_path
, protect
);
1627 return build (COMPOUND_EXPR
, TREE_TYPE (value
),
1628 TREE_OPERAND (datum
, 0), value
);
1631 return build_conditional_expr
1632 (TREE_OPERAND (datum
, 0),
1633 build_component_ref (TREE_OPERAND (datum
, 1), component
,
1634 basetype_path
, protect
),
1635 build_component_ref (TREE_OPERAND (datum
, 2), component
,
1636 basetype_path
, protect
));
1639 if (code
== REFERENCE_TYPE
)
1642 /* TREE_REFERENCE_EXPRs are not converted by `convert_from_reference'.
1643 @@ Maybe that is not right. */
1644 if (TREE_REFERENCE_EXPR (datum
))
1645 datum
= build1 (INDIRECT_REF
, TREE_TYPE (basetype
), datum
);
1648 datum
= convert_from_reference (datum
);
1649 basetype
= TREE_TYPE (datum
);
1650 code
= TREE_CODE (basetype
);
1653 /* First, see if there is a field or component with name COMPONENT. */
1654 if (TREE_CODE (component
) == TREE_LIST
)
1656 my_friendly_assert (!(TREE_CHAIN (component
) == NULL_TREE
1657 && DECL_CHAIN (TREE_VALUE (component
)) == NULL_TREE
), 309);
1658 return build (COMPONENT_REF
, TREE_TYPE (component
), datum
, component
);
1661 if (TREE_CODE (component
) == TYPE_EXPR
)
1662 return build_component_type_expr (datum
, component
, NULL_TREE
, protect
);
1665 if (! IS_AGGR_TYPE_CODE (code
))
1667 if (code
!= ERROR_MARK
)
1668 cp_error ("request for member `%D' in `%E', which is of non-aggregate type `%T'",
1669 component
, datum
, basetype
);
1670 return error_mark_node
;
1673 if (TYPE_SIZE (basetype
) == 0)
1675 incomplete_type_error (0, basetype
);
1676 return error_mark_node
;
1679 if (TREE_CODE (component
) == BIT_NOT_EXPR
)
1681 if (TYPE_IDENTIFIER (basetype
) != TREE_OPERAND (component
, 0))
1683 cp_error ("destructor specifier `%T::~%T' must have matching names",
1684 basetype
, TREE_OPERAND (component
, 0));
1685 return error_mark_node
;
1687 if (! TYPE_HAS_DESTRUCTOR (basetype
))
1689 cp_error ("type `%T' has no destructor", basetype
);
1690 return error_mark_node
;
1692 return TREE_VEC_ELT (CLASSTYPE_METHOD_VEC (basetype
), 0);
1695 /* Look up component name in the structure type definition. */
1696 if (CLASSTYPE_VFIELD (basetype
)
1697 && DECL_NAME (CLASSTYPE_VFIELD (basetype
)) == component
)
1698 /* Special-case this because if we use normal lookups in an ambiguous
1699 hierarchy, the compiler will abort (because vptr lookups are
1700 not supposed to be ambiguous. */
1701 field
= CLASSTYPE_VFIELD (basetype
);
1704 if (basetype_path
== NULL_TREE
)
1705 basetype_path
= TYPE_BINFO (basetype
);
1706 field
= lookup_field (basetype_path
, component
,
1707 protect
&& ! VFIELD_NAME_P (component
), 0);
1708 if (field
== error_mark_node
)
1709 return error_mark_node
;
1711 if (field
== NULL_TREE
)
1713 /* Not found as a data field, look for it as a method. If found,
1714 then if this is the only possible one, return it, else
1715 report ambiguity error. */
1716 tree fndecls
= lookup_fnfields (basetype_path
, component
, 1);
1717 if (fndecls
== error_mark_node
)
1718 return error_mark_node
;
1721 if (TREE_CHAIN (fndecls
) == NULL_TREE
1722 && DECL_CHAIN (TREE_VALUE (fndecls
)) == NULL_TREE
)
1724 enum access_type access
;
1727 /* Unique, so use this one now. */
1728 basetype
= TREE_PURPOSE (fndecls
);
1729 fndecl
= TREE_VALUE (fndecls
);
1730 access
= compute_access (TREE_PURPOSE (fndecls
), fndecl
);
1731 if (access
== access_public
)
1733 if (DECL_VINDEX (fndecl
)
1734 && ! resolves_to_fixed_type_p (datum
, 0))
1736 tree addr
= build_unary_op (ADDR_EXPR
, datum
, 0);
1737 addr
= convert_pointer_to (DECL_CONTEXT (fndecl
), addr
);
1738 datum
= build_indirect_ref (addr
, NULL_PTR
);
1739 my_friendly_assert (datum
!= error_mark_node
, 310);
1740 fndecl
= build_vfn_ref (&addr
, datum
, DECL_VINDEX (fndecl
));
1742 assemble_external (fndecl
);
1745 if (access
== access_protected
)
1746 cp_error ("member function `%D' is protected", fndecl
);
1748 cp_error ("member function `%D' is private", fndecl
);
1749 return error_mark_node
;
1753 /* Just act like build_offset_ref, since the object does
1754 not matter unless we're actually calling the function. */
1757 for (t
= TREE_VALUE (fndecls
); t
; t
= DECL_CHAIN (t
))
1758 assemble_external (t
);
1760 t
= build_tree_list (error_mark_node
, fndecls
);
1761 TREE_TYPE (t
) = build_offset_type (basetype
,
1768 if (component
== ansi_opname
[(int) TYPE_EXPR
])
1769 cp_error ("`%#T' has no such type conversion operator", basetype
);
1772 cp_error ("`%#T' has no member named `%D'", basetype
, component
);
1773 return error_mark_node
;
1775 else if (TREE_TYPE (field
) == error_mark_node
)
1776 return error_mark_node
;
1778 if (TREE_CODE (field
) != FIELD_DECL
)
1780 if (TREE_CODE (field
) == TYPE_DECL
)
1782 cp_error ("invalid use of type decl `%#D' as expression", field
);
1783 return error_mark_node
;
1785 if (DECL_RTL (field
) != 0)
1786 assemble_external (field
);
1787 TREE_USED (field
) = 1;
1792 if (DECL_FIELD_CONTEXT (field
) != basetype
1793 && TYPE_USES_COMPLEX_INHERITANCE (basetype
))
1795 tree addr
= build_unary_op (ADDR_EXPR
, datum
, 0);
1796 if (integer_zerop (addr
))
1798 error ("invalid reference to NULL ptr, use ptr-to-member instead");
1799 return error_mark_node
;
1801 addr
= convert_pointer_to (DECL_FIELD_CONTEXT (field
), addr
);
1802 datum
= build_indirect_ref (addr
, NULL_PTR
);
1803 my_friendly_assert (datum
!= error_mark_node
, 311);
1805 ref
= fold (build (COMPONENT_REF
, TREE_TYPE (field
),
1806 break_out_cleanups (datum
), field
));
1808 if (TREE_READONLY (datum
) || TREE_READONLY (field
))
1809 TREE_READONLY (ref
) = 1;
1810 if (TREE_THIS_VOLATILE (datum
) || TREE_THIS_VOLATILE (field
))
1811 TREE_THIS_VOLATILE (ref
) = 1;
1812 if (DECL_MUTABLE_P (field
))
1813 TREE_READONLY (ref
) = 0;
1818 /* Given an expression PTR for a pointer, return an expression
1819 for the value pointed to.
1820 ERRORSTRING is the name of the operator to appear in error messages.
1822 This function may need to overload OPERATOR_FNNAME.
1823 Must also handle REFERENCE_TYPEs for C++. */
1826 build_x_indirect_ref (ptr
, errorstring
)
1830 tree rval
= build_opfncall (INDIRECT_REF
, LOOKUP_NORMAL
, ptr
, NULL_TREE
, NULL_TREE
);
1833 return build_indirect_ref (ptr
, errorstring
);
1837 build_indirect_ref (ptr
, errorstring
)
1841 register tree pointer
= (TREE_CODE (TREE_TYPE (ptr
)) == REFERENCE_TYPE
?
1842 ptr
: default_conversion (ptr
));
1843 register tree type
= TREE_TYPE (pointer
);
1845 if (ptr
== current_class_decl
)
1848 ptr
= build_expr_type_conversion (WANT_POINTER
, pointer
, 1);
1852 type
= TREE_TYPE (pointer
);
1855 if (TREE_CODE (type
) == POINTER_TYPE
|| TREE_CODE (type
) == REFERENCE_TYPE
)
1857 if (TREE_CODE (pointer
) == ADDR_EXPR
1858 && (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_OPERAND (pointer
, 0)))
1859 == TYPE_MAIN_VARIANT (TREE_TYPE (type
)))
1860 && (TREE_READONLY (TREE_OPERAND (pointer
, 0))
1861 == TYPE_READONLY (TREE_TYPE (type
)))
1862 && (TREE_THIS_VOLATILE (TREE_OPERAND (pointer
, 0))
1863 == TYPE_VOLATILE (TREE_TYPE (type
))))
1864 return TREE_OPERAND (pointer
, 0);
1867 tree t
= TREE_TYPE (type
);
1868 register tree ref
= build1 (INDIRECT_REF
,
1869 TYPE_MAIN_VARIANT (t
), pointer
);
1871 TREE_READONLY (ref
) = TYPE_READONLY (t
);
1872 TREE_THIS_VOLATILE (ref
) = TYPE_VOLATILE (t
);
1873 TREE_SIDE_EFFECTS (ref
)
1874 = TYPE_VOLATILE (t
) || TREE_SIDE_EFFECTS (pointer
);
1878 /* `pointer' won't be an error_mark_node if we were given a
1879 pointer to member, so it's cool to check for this here. */
1880 else if (TYPE_PTRMEMFUNC_P (type
))
1881 error ("invalid use of `%s' on pointer to member function", errorstring
);
1882 else if (TREE_CODE (type
) == RECORD_TYPE
1883 && (IS_SIGNATURE_POINTER (type
) || IS_SIGNATURE_REFERENCE (type
)))
1884 error ("cannot dereference signature pointer/reference");
1885 else if (pointer
!= error_mark_node
)
1888 error ("invalid type argument of `%s'", errorstring
);
1890 error ("invalid type argument");
1892 return error_mark_node
;
1895 /* This handles expressions of the form "a[i]", which denotes
1898 This is logically equivalent in C to *(a+i), but we may do it differently.
1899 If A is a variable or a member, we generate a primitive ARRAY_REF.
1900 This avoids forcing the array out of registers, and can work on
1901 arrays that are not lvalues (for example, members of structures returned
1904 If INDEX is of some user-defined type, it must be converted to
1905 integer type. Otherwise, to make a compatible PLUS_EXPR, it
1906 will inherit the type of the array, which will be some pointer type. */
1909 build_x_array_ref (array
, index
)
1912 tree rval
= build_opfncall (ARRAY_REF
, LOOKUP_NORMAL
, array
, index
, NULL_TREE
);
1915 return build_array_ref (array
, index
);
1919 build_array_ref (array
, idx
)
1926 error ("subscript missing in array reference");
1927 return error_mark_node
;
1930 if (TREE_TYPE (array
) == error_mark_node
1931 || TREE_TYPE (idx
) == error_mark_node
)
1932 return error_mark_node
;
1934 itype
= TREE_TYPE (idx
);
1936 if (TREE_CODE (TREE_TYPE (array
)) == ARRAY_TYPE
1937 && TREE_CODE (array
) != INDIRECT_REF
)
1941 /* Subscripting with type char is likely to lose
1942 on a machine where chars are signed.
1943 So warn on any machine, but optionally.
1944 Don't warn for unsigned char since that type is safe.
1945 Don't warn for signed char because anyone who uses that
1946 must have done so deliberately. */
1947 if (warn_char_subscripts
1948 && TYPE_MAIN_VARIANT (TREE_TYPE (idx
)) == char_type_node
)
1949 warning ("array subscript has type `char'");
1951 /* Apply default promotions *after* noticing character types. */
1952 idx
= default_conversion (idx
);
1954 if (TREE_CODE (TREE_TYPE (idx
)) != INTEGER_TYPE
)
1956 error ("array subscript is not an integer");
1957 return error_mark_node
;
1960 /* An array that is indexed by a non-constant
1961 cannot be stored in a register; we must be able to do
1962 address arithmetic on its address.
1963 Likewise an array of elements of variable size. */
1964 if (TREE_CODE (idx
) != INTEGER_CST
1965 || (TYPE_SIZE (TREE_TYPE (TREE_TYPE (array
))) != 0
1966 && TREE_CODE (TYPE_SIZE (TREE_TYPE (TREE_TYPE (array
)))) != INTEGER_CST
))
1968 if (mark_addressable (array
) == 0)
1969 return error_mark_node
;
1971 /* An array that is indexed by a constant value which is not within
1972 the array bounds cannot be stored in a register either; because we
1973 would get a crash in store_bit_field/extract_bit_field when trying
1974 to access a non-existent part of the register. */
1975 if (TREE_CODE (idx
) == INTEGER_CST
1976 && TYPE_VALUES (TREE_TYPE (array
))
1977 && ! int_fits_type_p (idx
, TYPE_VALUES (TREE_TYPE (array
))))
1979 if (mark_addressable (array
) == 0)
1980 return error_mark_node
;
1983 if (pedantic
&& !lvalue_p (array
))
1984 pedwarn ("ANSI C++ forbids subscripting non-lvalue array");
1986 /* Note in C++ it is valid to subscript a `register' array, since
1987 it is valid to take the address of something with that
1988 storage specification. */
1992 while (TREE_CODE (foo
) == COMPONENT_REF
)
1993 foo
= TREE_OPERAND (foo
, 0);
1994 if (TREE_CODE (foo
) == VAR_DECL
&& DECL_REGISTER (foo
))
1995 warning ("subscripting array declared `register'");
1998 type
= TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (array
)));
1999 rval
= build (ARRAY_REF
, type
, array
, idx
);
2000 /* Array ref is const/volatile if the array elements are
2001 or if the array is.. */
2002 TREE_READONLY (rval
)
2003 |= (TYPE_READONLY (TREE_TYPE (TREE_TYPE (array
)))
2004 | TREE_READONLY (array
));
2005 TREE_SIDE_EFFECTS (rval
)
2006 |= (TYPE_VOLATILE (TREE_TYPE (TREE_TYPE (array
)))
2007 | TREE_SIDE_EFFECTS (array
));
2008 TREE_THIS_VOLATILE (rval
)
2009 |= (TYPE_VOLATILE (TREE_TYPE (TREE_TYPE (array
)))
2010 /* This was added by rms on 16 Nov 91.
2011 It fixes vol struct foo *a; a->elts[1]
2012 in an inline function.
2013 Hope it doesn't break something else. */
2014 | TREE_THIS_VOLATILE (array
));
2015 return require_complete_type (fold (rval
));
2019 tree ar
= default_conversion (array
);
2020 tree ind
= default_conversion (idx
);
2022 /* Put the integer in IND to simplify error checking. */
2023 if (TREE_CODE (TREE_TYPE (ar
)) == INTEGER_TYPE
)
2030 if (ar
== error_mark_node
)
2033 if (TREE_CODE (TREE_TYPE (ar
)) != POINTER_TYPE
)
2035 error ("subscripted value is neither array nor pointer");
2036 return error_mark_node
;
2038 if (TREE_CODE (TREE_TYPE (ind
)) != INTEGER_TYPE
)
2040 error ("array subscript is not an integer");
2041 return error_mark_node
;
2044 return build_indirect_ref (build_binary_op_nodefault (PLUS_EXPR
, ar
, ind
, PLUS_EXPR
),
2049 /* Build a function call to function FUNCTION with parameters PARAMS.
2050 PARAMS is a list--a chain of TREE_LIST nodes--in which the
2051 TREE_VALUE of each node is a parameter-expression.
2052 FUNCTION's data type may be a function type or a pointer-to-function.
2054 For C++: If FUNCTION's data type is a TREE_LIST, then the tree list
2055 is the list of possible methods that FUNCTION could conceivably
2056 be. If the list of methods comes from a class, then it will be
2057 a list of lists (where each element is associated with the class
2058 that produced it), otherwise it will be a simple list (for
2059 functions overloaded in global scope).
2061 In the first case, TREE_VALUE (function) is the head of one of those
2062 lists, and TREE_PURPOSE is the name of the function.
2064 In the second case, TREE_PURPOSE (function) is the function's
2067 DECL is the class instance variable, usually CURRENT_CLASS_DECL. */
2070 * [eichin:19911015.1726EST] actually return a possibly incomplete
2074 build_x_function_call (function
, params
, decl
)
2075 tree function
, params
, decl
;
2080 if (function
== error_mark_node
)
2081 return error_mark_node
;
2083 type
= TREE_TYPE (function
);
2084 is_method
= ((TREE_CODE (function
) == TREE_LIST
2085 && current_class_type
!= NULL_TREE
2086 && IDENTIFIER_CLASS_VALUE (TREE_PURPOSE (function
)) == function
)
2087 || TREE_CODE (function
) == IDENTIFIER_NODE
2088 || TREE_CODE (type
) == METHOD_TYPE
2089 || TYPE_PTRMEMFUNC_P (type
));
2091 /* Handle methods, friends, and overloaded functions, respectively. */
2094 if (TREE_CODE (function
) == FUNCTION_DECL
)
2096 if (DECL_NAME (function
))
2097 function
= DECL_NAME (function
);
2099 function
= TYPE_IDENTIFIER (DECL_CLASS_CONTEXT (function
));
2101 else if (TREE_CODE (function
) == TREE_LIST
)
2104 if (TREE_CODE (TREE_VALUE (function
)) == TREE_LIST
)
2105 function
= TREE_PURPOSE (TREE_VALUE (function
));
2107 function
= TREE_PURPOSE (function
);
2109 my_friendly_assert (TREE_CODE (TREE_VALUE (function
)) == FUNCTION_DECL
, 312);
2110 function
= TREE_PURPOSE (function
);
2113 else if (TREE_CODE (function
) != IDENTIFIER_NODE
)
2115 if (TREE_CODE (function
) == OFFSET_REF
)
2117 if (TREE_OPERAND (function
, 0))
2118 decl
= TREE_OPERAND (function
, 0);
2120 /* Call via a pointer to member function. */
2121 if (decl
== NULL_TREE
)
2123 error ("pointer to member function called, but not in class scope");
2124 return error_mark_node
;
2126 /* What other type of POINTER_TYPE could this be? */
2127 if (TREE_CODE (TREE_TYPE (function
)) != POINTER_TYPE
2128 && ! TYPE_PTRMEMFUNC_P (TREE_TYPE (function
))
2129 && TREE_CODE (function
) != OFFSET_REF
)
2130 function
= build (OFFSET_REF
, TREE_TYPE (type
), NULL_TREE
, function
);
2134 /* this is an abbreviated method call.
2135 must go through here in case it is a virtual function.
2136 @@ Perhaps this could be optimized. */
2138 if (decl
== NULL_TREE
)
2140 if (current_class_type
== NULL_TREE
)
2142 error ("object missing in call to method `%s'",
2143 IDENTIFIER_POINTER (function
));
2144 return error_mark_node
;
2146 /* Yow: call from a static member function. */
2147 decl
= build1 (NOP_EXPR
, TYPE_POINTER_TO (current_class_type
),
2149 decl
= build_indirect_ref (decl
, NULL_PTR
);
2152 return build_method_call (decl
, function
, params
,
2153 NULL_TREE
, LOOKUP_NORMAL
);
2155 else if (TREE_CODE (function
) == COMPONENT_REF
2156 && type
== unknown_type_node
)
2158 /* Should we undo what was done in build_component_ref? */
2159 if (TREE_CODE (TREE_PURPOSE (TREE_OPERAND (function
, 1))) == TREE_VEC
)
2160 /* Get the name that build_component_ref hid. */
2161 function
= DECL_NAME (TREE_VALUE (TREE_OPERAND (function
, 1)));
2163 function
= TREE_PURPOSE (TREE_OPERAND (function
, 1));
2164 return build_method_call (decl
, function
, params
,
2165 NULL_TREE
, LOOKUP_NORMAL
);
2167 else if (TREE_CODE (function
) == TREE_LIST
)
2169 if (TREE_VALUE (function
) == NULL_TREE
)
2171 cp_error ("function `%D' declared overloaded, but no definitions appear with which to resolve it?!?",
2172 TREE_PURPOSE (function
));
2173 return error_mark_node
;
2177 tree val
= TREE_VALUE (function
);
2179 if (TREE_CODE (val
) == TEMPLATE_DECL
)
2180 return build_overload_call_maybe
2181 (function
, params
, LOOKUP_COMPLAIN
, (struct candidate
*)0);
2182 else if (DECL_CHAIN (val
) != NULL_TREE
)
2183 return build_overload_call
2184 (function
, params
, LOOKUP_COMPLAIN
, (struct candidate
*)0);
2186 my_friendly_abort (360);
2191 if (TREE_CODE (function
) == OFFSET_REF
)
2193 /* If the component is a data element (or a virtual function), we play
2194 games here to make things work. */
2197 if (TREE_OPERAND (function
, 0))
2198 decl
= TREE_OPERAND (function
, 0);
2202 decl_addr
= build_unary_op (ADDR_EXPR
, decl
, 0);
2203 function
= get_member_function_from_ptrfunc (&decl_addr
,
2204 TREE_OPERAND (function
, 1));
2205 params
= tree_cons (NULL_TREE
, decl_addr
, params
);
2206 return build_function_call (function
, params
);
2209 type
= TREE_TYPE (function
);
2210 if (type
!= error_mark_node
)
2212 if (TREE_CODE (type
) == REFERENCE_TYPE
)
2213 type
= TREE_TYPE (type
);
2215 if (TYPE_LANG_SPECIFIC (type
) && TYPE_OVERLOADS_CALL_EXPR (type
))
2216 return build_opfncall (CALL_EXPR
, LOOKUP_NORMAL
, function
, params
, NULL_TREE
);
2221 tree fntype
= TREE_TYPE (function
);
2224 /* Explicitly named method? */
2225 if (TREE_CODE (function
) == FUNCTION_DECL
)
2226 ctypeptr
= TYPE_POINTER_TO (DECL_CLASS_CONTEXT (function
));
2227 /* Expression with ptr-to-method type? It could either be a plain
2228 usage, or it might be a case where the ptr-to-method is being
2229 passed in as an argument. */
2230 else if (TYPE_PTRMEMFUNC_P (fntype
))
2232 tree rec
= TYPE_METHOD_BASETYPE (TREE_TYPE (TYPE_PTRMEMFUNC_FN_TYPE (fntype
)));
2233 ctypeptr
= TYPE_POINTER_TO (rec
);
2235 /* Unexpected node type? */
2237 my_friendly_abort (116);
2238 if (decl
== NULL_TREE
)
2240 if (current_function_decl
2241 && DECL_STATIC_FUNCTION_P (current_function_decl
))
2242 error ("invalid call to member function needing `this' in static member function scope");
2244 error ("pointer to member function called, but not in class scope");
2245 return error_mark_node
;
2247 if (TREE_CODE (TREE_TYPE (decl
)) != POINTER_TYPE
2248 && ! TYPE_PTRMEMFUNC_P (TREE_TYPE (decl
)))
2250 decl
= build_unary_op (ADDR_EXPR
, decl
, 0);
2251 decl
= convert_pointer_to (TREE_TYPE (ctypeptr
), decl
);
2254 decl
= build_c_cast (ctypeptr
, decl
, 0);
2255 params
= tree_cons (NULL_TREE
, decl
, params
);
2258 return build_function_call (function
, params
);
2261 /* Resolve a pointer to member function. INSTANCE is the object
2262 instance to use, if the member points to a virtual member. */
2265 get_member_function_from_ptrfunc (instance_ptrptr
, function
)
2266 tree
*instance_ptrptr
;
2269 if (TREE_CODE (function
) == OFFSET_REF
)
2271 function
= TREE_OPERAND (function
, 1);
2274 if (TYPE_PTRMEMFUNC_P (TREE_TYPE (function
)))
2276 tree fntype
= TYPE_PTRMEMFUNC_FN_TYPE (TREE_TYPE (function
));
2277 tree index
= save_expr (build_component_ref (function
,
2280 tree e1
= build (GT_EXPR
, boolean_type_node
, index
,
2281 convert (delta_type_node
, integer_zero_node
));
2282 tree delta
= convert (ptrdiff_type_node
,
2283 build_component_ref (function
, delta_identifier
, 0, 0));
2284 tree delta2
= DELTA2_FROM_PTRMEMFUNC (function
);
2290 tree instance_ptr
= *instance_ptrptr
;
2292 if (TREE_SIDE_EFFECTS (instance_ptr
))
2293 instance_ptr
= save_expr (instance_ptr
);
2295 /* convert down to the right base, before using the instance. */
2297 = convert_pointer_to_real (TYPE_METHOD_BASETYPE (TREE_TYPE (fntype
)),
2299 if (instance
== error_mark_node
)
2302 vtbl
= convert_pointer_to (ptr_type_node
, instance
);
2305 build_pointer_type (build_pointer_type (vtable_entry_type
)),
2306 vtbl
, convert (ptrdiff_type_node
, delta2
));
2307 vtbl
= build_indirect_ref (vtbl
, NULL_PTR
);
2308 aref
= build_array_ref (vtbl
, build_binary_op (MINUS_EXPR
,
2310 integer_one_node
, 1));
2311 if (! flag_vtable_thunks
)
2313 aref
= save_expr (aref
);
2315 /* Save the intermediate result in a SAVE_EXPR so we don't have to
2316 compute each component of the virtual function pointer twice. */
2317 if (/* !building_cleanup && */ TREE_CODE (aref
) == INDIRECT_REF
)
2318 TREE_OPERAND (aref
, 0) = save_expr (TREE_OPERAND (aref
, 0));
2320 delta
= build_binary_op (PLUS_EXPR
,
2321 build_conditional_expr (e1
, build_component_ref (aref
, delta_identifier
, 0, 0), integer_zero_node
),
2325 *instance_ptrptr
= build (PLUS_EXPR
, TREE_TYPE (instance_ptr
),
2326 instance_ptr
, delta
);
2327 if (flag_vtable_thunks
)
2330 e2
= build_component_ref (aref
, pfn_identifier
, 0, 0);
2332 e3
= PFN_FROM_PTRMEMFUNC (function
);
2333 TREE_TYPE (e2
) = TREE_TYPE (e3
);
2334 function
= build_conditional_expr (e1
, e2
, e3
);
2336 /* Make sure this doesn't get evaluated first inside one of the
2337 branches of the COND_EXPR. */
2338 if (TREE_CODE (instance_ptr
) == SAVE_EXPR
)
2339 function
= build (COMPOUND_EXPR
, TREE_TYPE (function
),
2340 instance_ptr
, function
);
2346 build_function_call_real (function
, params
, require_complete
, flags
)
2347 tree function
, params
;
2348 int require_complete
, flags
;
2350 register tree fntype
, fndecl
;
2351 register tree value_type
;
2352 register tree coerced_params
;
2353 tree name
= NULL_TREE
, assembler_name
= NULL_TREE
;
2356 /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
2357 Strip such NOP_EXPRs, since FUNCTION is used in non-lvalue context. */
2358 if (TREE_CODE (function
) == NOP_EXPR
2359 && TREE_TYPE (function
) == TREE_TYPE (TREE_OPERAND (function
, 0)))
2360 function
= TREE_OPERAND (function
, 0);
2362 if (TREE_CODE (function
) == FUNCTION_DECL
)
2364 name
= DECL_NAME (function
);
2365 assembler_name
= DECL_ASSEMBLER_NAME (function
);
2367 GNU_xref_call (current_function_decl
,
2368 IDENTIFIER_POINTER (name
? name
2369 : TYPE_IDENTIFIER (DECL_CLASS_CONTEXT (function
))));
2370 assemble_external (function
);
2373 /* Convert anything with function type to a pointer-to-function. */
2376 && IDENTIFIER_LENGTH (name
) == 4
2377 && ! strcmp (IDENTIFIER_POINTER (name
), "main")
2378 && DECL_CONTEXT (function
) == NULL_TREE
)
2380 pedwarn ("ANSI C++ forbids calling `main' from within program");
2383 if (pedantic
&& DECL_THIS_INLINE (function
) && ! DECL_INITIAL (function
)
2384 && ! DECL_ARTIFICIAL (function
)
2385 && ! DECL_PENDING_INLINE_INFO (function
))
2386 cp_pedwarn ("inline function `%#D' called before definition",
2389 /* Differs from default_conversion by not setting TREE_ADDRESSABLE
2390 (because calling an inline function does not mean the function
2391 needs to be separately compiled). */
2393 if (DECL_INLINE (function
))
2395 /* Is it a synthesized method that needs to be synthesized? */
2396 if (DECL_ARTIFICIAL (function
) && ! flag_no_inline
2397 && ! DECL_INITIAL (function
)
2398 /* Kludge: don't synthesize for default args. */
2399 && current_function_decl
)
2400 synthesize_method (function
);
2402 fntype
= build_type_variant (TREE_TYPE (function
),
2403 TREE_READONLY (function
),
2404 TREE_THIS_VOLATILE (function
));
2405 function
= build1 (ADDR_EXPR
, build_pointer_type (fntype
), function
);
2409 assemble_external (function
);
2410 TREE_USED (function
) = 1;
2411 function
= default_conversion (function
);
2418 /* Convert anything with function type to a pointer-to-function. */
2419 if (function
== error_mark_node
)
2420 return error_mark_node
;
2421 function
= default_conversion (function
);
2424 fntype
= TREE_TYPE (function
);
2426 if (TYPE_PTRMEMFUNC_P (fntype
))
2428 tree instance_ptr
= build_unary_op (ADDR_EXPR
, C_C_D
, 0);
2429 fntype
= TYPE_PTRMEMFUNC_FN_TYPE (fntype
);
2430 function
= get_member_function_from_ptrfunc (&instance_ptr
, function
);
2433 is_method
= (TREE_CODE (fntype
) == POINTER_TYPE
2434 && TREE_CODE (TREE_TYPE (fntype
)) == METHOD_TYPE
);
2436 if (!((TREE_CODE (fntype
) == POINTER_TYPE
2437 && TREE_CODE (TREE_TYPE (fntype
)) == FUNCTION_TYPE
)
2440 cp_error ("`%E' cannot be used as a function", function
);
2441 return error_mark_node
;
2444 /* fntype now gets the type of function pointed to. */
2445 fntype
= TREE_TYPE (fntype
);
2447 /* Convert the parameters to the types declared in the
2448 function prototype, or apply default promotions. */
2450 if (flags
& LOOKUP_COMPLAIN
)
2451 coerced_params
= convert_arguments (NULL_TREE
, TYPE_ARG_TYPES (fntype
),
2452 params
, fndecl
, LOOKUP_NORMAL
);
2454 coerced_params
= convert_arguments (NULL_TREE
, TYPE_ARG_TYPES (fntype
),
2457 if (coerced_params
== error_mark_node
)
2458 if (flags
& LOOKUP_SPECULATIVELY
)
2461 return error_mark_node
;
2463 /* Check for errors in format strings. */
2465 if (warn_format
&& (name
|| assembler_name
))
2466 check_function_format (name
, assembler_name
, coerced_params
);
2468 /* Recognize certain built-in functions so we can make tree-codes
2469 other than CALL_EXPR. We do this when it enables fold-const.c
2470 to do something useful. */
2472 if (TREE_CODE (function
) == ADDR_EXPR
2473 && TREE_CODE (TREE_OPERAND (function
, 0)) == FUNCTION_DECL
2474 && DECL_BUILT_IN (TREE_OPERAND (function
, 0)))
2475 switch (DECL_FUNCTION_CODE (TREE_OPERAND (function
, 0)))
2480 if (coerced_params
== 0)
2481 return integer_zero_node
;
2482 return build_unary_op (ABS_EXPR
, TREE_VALUE (coerced_params
), 0);
2486 value_type
= TREE_TYPE (fntype
) ? TREE_TYPE (fntype
) : void_type_node
;
2488 register tree result
=
2489 build (CALL_EXPR
, value_type
,
2490 function
, coerced_params
, NULL_TREE
);
2492 TREE_SIDE_EFFECTS (result
) = 1;
2494 if (! require_complete
)
2495 return convert_from_reference (result
);
2496 if (value_type
== void_type_node
)
2498 result
= require_complete_type (result
);
2499 return convert_from_reference (result
);
2504 build_function_call (function
, params
)
2505 tree function
, params
;
2507 return build_function_call_real (function
, params
, 1, LOOKUP_NORMAL
);
2511 build_function_call_maybe (function
, params
)
2512 tree function
, params
;
2514 return build_function_call_real (function
, params
, 0, 0);
2518 /* Convert the actual parameter expressions in the list VALUES
2519 to the types in the list TYPELIST.
2520 If parmdecls is exhausted, or when an element has NULL as its type,
2521 perform the default conversions.
2523 RETURN_LOC is the location of the return value, if known, NULL_TREE
2524 otherwise. This is useful in the case where we can avoid creating
2525 a temporary variable in the case where we can initialize the return
2526 value directly. If we are not eliding constructors, then we set this
2527 to NULL_TREE to avoid this avoidance.
2529 NAME is an IDENTIFIER_NODE or 0. It is used only for error messages.
2531 This is also where warnings about wrong number of args are generated.
2533 Return a list of expressions for the parameters as converted.
2535 Both VALUES and the returned value are chains of TREE_LIST nodes
2536 with the elements of the list in the TREE_VALUE slots of those nodes.
2538 In C++, unspecified trailing parameters can be filled in with their
2539 default arguments, if such were specified. Do so here. */
2542 convert_arguments (return_loc
, typelist
, values
, fndecl
, flags
)
2543 tree return_loc
, typelist
, values
, fndecl
;
2546 extern tree gc_protect_fndecl
;
2547 register tree typetail
, valtail
;
2548 register tree result
= NULL_TREE
;
2552 if (! flag_elide_constructors
)
2557 if (TREE_CODE (TREE_TYPE (fndecl
)) == METHOD_TYPE
)
2559 if (DECL_NAME (fndecl
) == NULL_TREE
2560 || IDENTIFIER_HAS_TYPE_VALUE (DECL_NAME (fndecl
)))
2561 called_thing
= "constructor";
2563 called_thing
= "member function";
2566 called_thing
= "function";
2569 for (valtail
= values
, typetail
= typelist
;
2571 valtail
= TREE_CHAIN (valtail
), i
++)
2573 register tree type
= typetail
? TREE_VALUE (typetail
) : 0;
2574 register tree val
= TREE_VALUE (valtail
);
2576 if (val
== error_mark_node
)
2577 return error_mark_node
;
2579 if (type
== void_type_node
)
2583 char *buf
= (char *)alloca (40 + strlen (called_thing
));
2584 sprintf (buf
, "too many arguments to %s `%%s'", called_thing
);
2585 error_with_decl (fndecl
, buf
);
2586 error ("at this point in file");
2589 error ("too many arguments to function");
2590 /* In case anybody wants to know if this argument
2593 TREE_TYPE (tree_last (result
)) = error_mark_node
;
2597 /* The tree type of the parameter being passed may not yet be
2598 known. In this case, its type is TYPE_UNKNOWN, and will
2599 be instantiated by the type given by TYPE. If TYPE
2600 is also NULL, the tree type of VAL is ERROR_MARK_NODE. */
2601 if (type
&& type_unknown_p (val
))
2602 val
= require_instantiated_type (type
, val
, integer_zero_node
);
2603 else if (type_unknown_p (val
))
2605 /* Strip the `&' from an overloaded FUNCTION_DECL. */
2606 if (TREE_CODE (val
) == ADDR_EXPR
)
2607 val
= TREE_OPERAND (val
, 0);
2608 if (TREE_CODE (val
) == TREE_LIST
2609 && TREE_CHAIN (val
) == NULL_TREE
2610 && TREE_TYPE (TREE_VALUE (val
)) != NULL_TREE
2611 && (TREE_TYPE (val
) == unknown_type_node
2612 || DECL_CHAIN (TREE_VALUE (val
)) == NULL_TREE
))
2613 /* Instantiates automatically. */
2614 val
= TREE_VALUE (val
);
2617 error ("insufficient type information in parameter list");
2618 val
= integer_zero_node
;
2621 else if (TREE_CODE (val
) == OFFSET_REF
2622 && TREE_CODE (TREE_TYPE (val
)) == METHOD_TYPE
)
2624 /* This is unclean. Should be handled elsewhere. */
2625 val
= build_unary_op (ADDR_EXPR
, val
, 0);
2627 else if (TREE_CODE (val
) == OFFSET_REF
)
2628 val
= resolve_offset_ref (val
);
2632 /* This code forces the assumption that if we have a ptr-to-func
2633 type in an arglist, that every routine that wants to check
2634 its validity has done so, and thus we need not do any
2635 more conversion. I don't remember why this is necessary. */
2636 else if (TREE_CODE (ttype
) == FUNCTION_TYPE
2638 || TREE_CODE (TREE_TYPE (type
)) == FUNCTION_TYPE
2639 || TREE_CODE (TREE_TYPE (type
)) == VOID_TYPE
))
2641 type
= build_pointer_type (ttype
);
2646 /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
2647 Strip such NOP_EXPRs, since VAL is used in non-lvalue context. */
2648 if (TREE_CODE (val
) == NOP_EXPR
2649 && TREE_TYPE (val
) == TREE_TYPE (TREE_OPERAND (val
, 0))
2650 && (type
== 0 || TREE_CODE (type
) != REFERENCE_TYPE
))
2651 val
= TREE_OPERAND (val
, 0);
2653 if (type
== 0 || TREE_CODE (type
) != REFERENCE_TYPE
)
2655 if (TREE_CODE (TREE_TYPE (val
)) == ARRAY_TYPE
2656 || TREE_CODE (TREE_TYPE (val
)) == FUNCTION_TYPE
2657 || TREE_CODE (TREE_TYPE (val
)) == METHOD_TYPE
)
2658 val
= default_conversion (val
);
2660 val
= require_complete_type (val
);
2663 if (val
== error_mark_node
)
2664 return error_mark_node
;
2668 /* Formal parm type is specified by a function prototype. */
2671 if (TYPE_SIZE (type
) == 0)
2673 error ("parameter type of called function is incomplete");
2678 #if 0 && defined (PROMOTE_PROTOTYPES)
2679 /* This breaks user-defined conversions. */
2680 /* Rather than truncating and then reextending,
2681 convert directly to int, if that's the type we will want. */
2682 if (! flag_traditional
2683 && (TREE_CODE (type
) == INTEGER_TYPE
2684 || TREE_CODE (type
) == ENUMERAL_TYPE
)
2685 && (TYPE_PRECISION (type
) < TYPE_PRECISION (integer_type_node
)))
2686 type
= integer_type_node
;
2688 parmval
= convert_for_initialization (return_loc
, type
, val
, flags
,
2689 "argument passing", fndecl
, i
);
2690 #ifdef PROMOTE_PROTOTYPES
2691 if ((TREE_CODE (type
) == INTEGER_TYPE
2692 || TREE_CODE (type
) == ENUMERAL_TYPE
)
2693 && (TYPE_PRECISION (type
) < TYPE_PRECISION (integer_type_node
)))
2694 parmval
= default_conversion (parmval
);
2698 if (parmval
== error_mark_node
)
2699 return error_mark_node
;
2701 result
= tree_cons (NULL_TREE
, parmval
, result
);
2705 if (TREE_CODE (TREE_TYPE (val
)) == REFERENCE_TYPE
)
2706 val
= convert_from_reference (val
);
2708 if (TREE_CODE (TREE_TYPE (val
)) == REAL_TYPE
2709 && (TYPE_PRECISION (TREE_TYPE (val
))
2710 < TYPE_PRECISION (double_type_node
)))
2711 /* Convert `float' to `double'. */
2712 result
= tree_cons (NULL_TREE
, convert (double_type_node
, val
), result
);
2713 else if (TYPE_LANG_SPECIFIC (TREE_TYPE (val
))
2714 && ! TYPE_HAS_TRIVIAL_INIT_REF (TREE_TYPE (val
)))
2716 cp_warning ("cannot pass objects of type `%T' through `...'",
2718 result
= tree_cons (NULL_TREE
, val
, result
);
2721 /* Convert `short' and `char' to full-size `int'. */
2722 result
= tree_cons (NULL_TREE
, default_conversion (val
), result
);
2726 /* There are certain functions for which we don't need
2727 to protect our arguments. GC_PROTECT_FNDECL is one. */
2728 && fndecl
!= gc_protect_fndecl
2729 && type_needs_gc_entry (TREE_TYPE (TREE_VALUE (result
)))
2730 && ! value_safe_from_gc (NULL_TREE
, TREE_VALUE (result
)))
2731 /* This will build a temporary variable whose cleanup is
2732 to clear the obstack entry. */
2733 TREE_VALUE (result
) = protect_value_from_gc (NULL_TREE
,
2734 TREE_VALUE (result
));
2737 typetail
= TREE_CHAIN (typetail
);
2740 if (typetail
!= 0 && typetail
!= void_list_node
)
2742 /* See if there are default arguments that can be used */
2743 if (TREE_PURPOSE (typetail
))
2745 for (; typetail
!= void_list_node
; ++i
)
2747 tree type
= TREE_VALUE (typetail
);
2748 tree val
= break_out_target_exprs (TREE_PURPOSE (typetail
));
2751 if (val
== NULL_TREE
)
2752 parmval
= error_mark_node
;
2753 else if (TREE_CODE (val
) == CONSTRUCTOR
)
2755 parmval
= digest_init (type
, val
, (tree
*)0);
2756 parmval
= convert_for_initialization (return_loc
, type
, parmval
, flags
,
2757 "default constructor", fndecl
, i
);
2761 /* This could get clobbered by the following call. */
2762 if (TREE_HAS_CONSTRUCTOR (val
))
2763 val
= copy_node (val
);
2765 parmval
= convert_for_initialization (return_loc
, type
, val
, flags
,
2766 "default argument", fndecl
, i
);
2767 #ifdef PROMOTE_PROTOTYPES
2768 if ((TREE_CODE (type
) == INTEGER_TYPE
2769 || TREE_CODE (type
) == ENUMERAL_TYPE
)
2770 && (TYPE_PRECISION (type
) < TYPE_PRECISION (integer_type_node
)))
2771 parmval
= default_conversion (parmval
);
2775 if (parmval
== error_mark_node
)
2776 return error_mark_node
;
2779 && type_needs_gc_entry (TREE_TYPE (parmval
))
2780 && ! value_safe_from_gc (NULL_TREE
, parmval
))
2781 parmval
= protect_value_from_gc (NULL_TREE
, parmval
);
2783 result
= tree_cons (0, parmval
, result
);
2784 typetail
= TREE_CHAIN (typetail
);
2785 /* ends with `...'. */
2786 if (typetail
== NULL_TREE
)
2794 char *buf
= (char *)alloca (32 + strlen (called_thing
));
2795 sprintf (buf
, "too few arguments to %s `%%#D'", called_thing
);
2796 cp_error_at (buf
, fndecl
);
2797 error ("at this point in file");
2800 error ("too few arguments to function");
2801 return error_mark_list
;
2805 return nreverse (result
);
2808 /* Build a binary-operation expression, after performing default
2809 conversions on the operands. CODE is the kind of expression to build. */
2812 build_x_binary_op (code
, arg1
, arg2
)
2813 enum tree_code code
;
2816 tree rval
= build_opfncall (code
, LOOKUP_SPECULATIVELY
,
2817 arg1
, arg2
, NULL_TREE
);
2819 return build_opfncall (code
, LOOKUP_NORMAL
, arg1
, arg2
, NULL_TREE
);
2820 if (code
== MEMBER_REF
)
2821 return build_m_component_ref (build_indirect_ref (arg1
, NULL_PTR
),
2823 return build_binary_op (code
, arg1
, arg2
, 1);
2827 build_binary_op (code
, arg1
, arg2
, convert_p
)
2828 enum tree_code code
;
2841 args
[0] = decay_conversion (args
[0]);
2842 args
[1] = decay_conversion (args
[1]);
2844 if (args
[0] == error_mark_node
|| args
[1] == error_mark_node
)
2845 return error_mark_node
;
2847 type0
= TREE_TYPE (args
[0]);
2848 type1
= TREE_TYPE (args
[1]);
2850 if (type_unknown_p (args
[0]))
2852 args
[0] = instantiate_type (type1
, args
[0], 1);
2853 args
[0] = decay_conversion (args
[0]);
2855 else if (type_unknown_p (args
[1]))
2857 args
[1] = require_instantiated_type (type0
, args
[1],
2859 args
[1] = decay_conversion (args
[1]);
2862 if (IS_AGGR_TYPE (type0
) || IS_AGGR_TYPE (type1
))
2864 /* Try to convert this to something reasonable. */
2865 if (! build_default_binary_type_conversion(code
, &args
[0], &args
[1]))
2867 cp_error ("no match for `%O(%#T, %#T)'", code
,
2868 TREE_TYPE (arg1
), TREE_TYPE (arg2
));
2869 return error_mark_node
;
2873 return build_binary_op_nodefault (code
, args
[0], args
[1], code
);
2876 /* Build a binary-operation expression without default conversions.
2877 CODE is the kind of expression to build.
2878 This function differs from `build' in several ways:
2879 the data type of the result is computed and recorded in it,
2880 warnings are generated if arg data types are invalid,
2881 special handling for addition and subtraction of pointers is known,
2882 and some optimization is done (operations on narrow ints
2883 are done in the narrower type when that gives the same result).
2884 Constant folding is also done before the result is returned.
2886 ERROR_CODE is the code that determines what to say in error messages.
2887 It is usually, but not always, the same as CODE.
2889 Note that the operands will never have enumeral types
2890 because either they have just had the default conversions performed
2891 or they have both just been converted to some other type in which
2892 the arithmetic is to be done.
2894 C++: must do special pointer arithmetic when implementing
2895 multiple inheritance, and deal with pointer to member functions. */
2898 build_binary_op_nodefault (code
, orig_op0
, orig_op1
, error_code
)
2899 enum tree_code code
;
2900 tree orig_op0
, orig_op1
;
2901 enum tree_code error_code
;
2904 register enum tree_code code0
, code1
;
2907 /* Expression code to give to the expression when it is built.
2908 Normally this is CODE, which is what the caller asked for,
2909 but in some special cases we change it. */
2910 register enum tree_code resultcode
= code
;
2912 /* Data type in which the computation is to be performed.
2913 In the simplest cases this is the common type of the arguments. */
2914 register tree result_type
= NULL
;
2916 /* Nonzero means operands have already been type-converted
2917 in whatever way is necessary.
2918 Zero means they need to be converted to RESULT_TYPE. */
2921 /* Nonzero means create the expression with this type, rather than
2923 tree build_type
= 0;
2925 /* Nonzero means after finally constructing the expression
2926 convert it to this type. */
2927 tree final_type
= 0;
2929 /* Nonzero if this is an operation like MIN or MAX which can
2930 safely be computed in short if both args are promoted shorts.
2931 Also implies COMMON.
2932 -1 indicates a bitwise operation; this makes a difference
2933 in the exact conditions for when it is safe to do the operation
2934 in a narrower mode. */
2937 /* Nonzero if this is a comparison operation;
2938 if both args are promoted shorts, compare the original shorts.
2939 Also implies COMMON. */
2940 int short_compare
= 0;
2942 /* Nonzero if this is a right-shift operation, which can be computed on the
2943 original short and then promoted if the operand is a promoted short. */
2944 int short_shift
= 0;
2946 /* Nonzero means set RESULT_TYPE to the common type of the args. */
2949 /* Apply default conversions. */
2950 if (code
== TRUTH_AND_EXPR
|| code
== TRUTH_ANDIF_EXPR
2951 || code
== TRUTH_OR_EXPR
|| code
== TRUTH_ORIF_EXPR
2952 || code
== TRUTH_XOR_EXPR
)
2954 op0
= decay_conversion (orig_op0
);
2955 op1
= decay_conversion (orig_op1
);
2959 op0
= default_conversion (orig_op0
);
2960 op1
= default_conversion (orig_op1
);
2963 type0
= TREE_TYPE (op0
);
2964 type1
= TREE_TYPE (op1
);
2966 /* The expression codes of the data types of the arguments tell us
2967 whether the arguments are integers, floating, pointers, etc. */
2968 code0
= TREE_CODE (type0
);
2969 code1
= TREE_CODE (type1
);
2971 /* Strip NON_LVALUE_EXPRs, etc., since we aren't using as an lvalue. */
2972 STRIP_TYPE_NOPS (op0
);
2973 STRIP_TYPE_NOPS (op1
);
2975 /* If an error was already reported for one of the arguments,
2976 avoid reporting another error. */
2978 if (code0
== ERROR_MARK
|| code1
== ERROR_MARK
)
2979 return error_mark_node
;
2984 /* Handle the pointer + int case. */
2985 if (code0
== POINTER_TYPE
&& code1
== INTEGER_TYPE
)
2986 return pointer_int_sum (PLUS_EXPR
, op0
, op1
);
2987 else if (code1
== POINTER_TYPE
&& code0
== INTEGER_TYPE
)
2988 return pointer_int_sum (PLUS_EXPR
, op1
, op0
);
2994 /* Subtraction of two similar pointers.
2995 We must subtract them as integers, then divide by object size. */
2996 if (code0
== POINTER_TYPE
&& code1
== POINTER_TYPE
2997 && comp_target_types (type0
, type1
, 1))
2998 return pointer_diff (op0
, op1
);
2999 /* Handle pointer minus int. Just like pointer plus int. */
3000 else if (code0
== POINTER_TYPE
&& code1
== INTEGER_TYPE
)
3001 return pointer_int_sum (MINUS_EXPR
, op0
, op1
);
3010 case TRUNC_DIV_EXPR
:
3012 case FLOOR_DIV_EXPR
:
3013 case ROUND_DIV_EXPR
:
3014 case EXACT_DIV_EXPR
:
3015 if ((code0
== INTEGER_TYPE
|| code0
== REAL_TYPE
)
3016 && (code1
== INTEGER_TYPE
|| code1
== REAL_TYPE
))
3018 if (TREE_CODE (op1
) == INTEGER_CST
&& integer_zerop (op1
))
3019 cp_warning ("division by zero in `%E / 0'", op0
);
3020 else if (TREE_CODE (op1
) == REAL_CST
&& real_zerop (op1
))
3021 cp_warning ("division by zero in `%E / 0.'", op0
);
3023 if (!(code0
== INTEGER_TYPE
&& code1
== INTEGER_TYPE
))
3024 resultcode
= RDIV_EXPR
;
3026 /* When dividing two signed integers, we have to promote to int.
3027 unless we divide by a constant != -1. Note that default
3028 conversion will have been performed on the operands at this
3029 point, so we have to dig out the original type to find out if
3031 shorten
= ((TREE_CODE (op0
) == NOP_EXPR
3032 && TREE_UNSIGNED (TREE_TYPE (TREE_OPERAND (op0
, 0))))
3033 || (TREE_CODE (op1
) == INTEGER_CST
3034 && (TREE_INT_CST_LOW (op1
) != -1
3035 || TREE_INT_CST_HIGH (op1
) != -1)));
3041 case BIT_ANDTC_EXPR
:
3044 if (code0
== INTEGER_TYPE
&& code1
== INTEGER_TYPE
)
3046 /* If one operand is a constant, and the other is a short type
3047 that has been converted to an int,
3048 really do the work in the short type and then convert the
3049 result to int. If we are lucky, the constant will be 0 or 1
3050 in the short type, making the entire operation go away. */
3051 if (TREE_CODE (op0
) == INTEGER_CST
3052 && TREE_CODE (op1
) == NOP_EXPR
3053 && TYPE_PRECISION (type1
) > TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (op1
, 0)))
3054 && TREE_UNSIGNED (TREE_TYPE (TREE_OPERAND (op1
, 0))))
3056 final_type
= result_type
;
3057 op1
= TREE_OPERAND (op1
, 0);
3058 result_type
= TREE_TYPE (op1
);
3060 if (TREE_CODE (op1
) == INTEGER_CST
3061 && TREE_CODE (op0
) == NOP_EXPR
3062 && TYPE_PRECISION (type0
) > TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (op0
, 0)))
3063 && TREE_UNSIGNED (TREE_TYPE (TREE_OPERAND (op0
, 0))))
3065 final_type
= result_type
;
3066 op0
= TREE_OPERAND (op0
, 0);
3067 result_type
= TREE_TYPE (op0
);
3071 case TRUNC_MOD_EXPR
:
3072 case FLOOR_MOD_EXPR
:
3073 if (code1
== INTEGER_TYPE
&& integer_zerop (op1
))
3074 cp_warning ("division by zero in `%E % 0'", op0
);
3075 else if (code1
== REAL_TYPE
&& real_zerop (op1
))
3076 cp_warning ("division by zero in `%E % 0.'", op0
);
3078 if (code0
== INTEGER_TYPE
&& code1
== INTEGER_TYPE
)
3080 /* Although it would be tempting to shorten always here, that loses
3081 on some targets, since the modulo instruction is undefined if the
3082 quotient can't be represented in the computation mode. We shorten
3083 only if unsigned or if dividing by something we know != -1. */
3084 shorten
= ((TREE_CODE (op0
) == NOP_EXPR
3085 && TREE_UNSIGNED (TREE_TYPE (TREE_OPERAND (op0
, 0))))
3086 || (TREE_CODE (op1
) == INTEGER_CST
3087 && (TREE_INT_CST_LOW (op1
) != -1
3088 || TREE_INT_CST_HIGH (op1
) != -1)));
3093 case TRUTH_ANDIF_EXPR
:
3094 case TRUTH_ORIF_EXPR
:
3095 case TRUTH_AND_EXPR
:
3097 result_type
= boolean_type_node
;
3100 /* Shift operations: result has same type as first operand;
3101 always convert second operand to int.
3102 Also set SHORT_SHIFT if shifting rightward. */
3105 if (code0
== INTEGER_TYPE
&& code1
== INTEGER_TYPE
)
3107 result_type
= type0
;
3108 if (TREE_CODE (op1
) == INTEGER_CST
)
3110 if (tree_int_cst_lt (op1
, integer_zero_node
))
3111 warning ("right shift count is negative");
3114 if (TREE_INT_CST_LOW (op1
) | TREE_INT_CST_HIGH (op1
))
3116 if (TREE_INT_CST_HIGH (op1
) != 0
3117 || ((unsigned HOST_WIDE_INT
) TREE_INT_CST_LOW (op1
)
3118 >= TYPE_PRECISION (type0
)))
3119 warning ("right shift count >= width of type");
3122 /* Convert the shift-count to an integer, regardless of
3123 size of value being shifted. */
3124 if (TYPE_MAIN_VARIANT (TREE_TYPE (op1
)) != integer_type_node
)
3125 op1
= convert (integer_type_node
, op1
);
3126 /* Avoid converting op1 to result_type later. */
3132 if (code0
== INTEGER_TYPE
&& code1
== INTEGER_TYPE
)
3134 result_type
= type0
;
3135 if (TREE_CODE (op1
) == INTEGER_CST
)
3137 if (tree_int_cst_lt (op1
, integer_zero_node
))
3138 warning ("left shift count is negative");
3139 else if (TREE_INT_CST_HIGH (op1
) != 0
3140 || ((unsigned HOST_WIDE_INT
) TREE_INT_CST_LOW (op1
)
3141 >= TYPE_PRECISION (type0
)))
3142 warning ("left shift count >= width of type");
3144 /* Convert the shift-count to an integer, regardless of
3145 size of value being shifted. */
3146 if (TYPE_MAIN_VARIANT (TREE_TYPE (op1
)) != integer_type_node
)
3147 op1
= convert (integer_type_node
, op1
);
3148 /* Avoid converting op1 to result_type later. */
3155 if (code0
== INTEGER_TYPE
&& code1
== INTEGER_TYPE
)
3157 result_type
= type0
;
3158 if (TREE_CODE (op1
) == INTEGER_CST
)
3160 if (tree_int_cst_lt (op1
, integer_zero_node
))
3161 warning ("%s rotate count is negative",
3162 (code
== LROTATE_EXPR
) ? "left" : "right");
3163 else if (TREE_INT_CST_HIGH (op1
) != 0
3164 || ((unsigned HOST_WIDE_INT
) TREE_INT_CST_LOW (op1
)
3165 >= TYPE_PRECISION (type0
)))
3166 warning ("%s rotate count >= width of type",
3167 (code
== LROTATE_EXPR
) ? "left" : "right");
3169 /* Convert the shift-count to an integer, regardless of
3170 size of value being shifted. */
3171 if (TYPE_MAIN_VARIANT (TREE_TYPE (op1
)) != integer_type_node
)
3172 op1
= convert (integer_type_node
, op1
);
3178 build_type
= boolean_type_node
;
3179 if ((code0
== INTEGER_TYPE
|| code0
== REAL_TYPE
)
3180 && (code1
== INTEGER_TYPE
|| code1
== REAL_TYPE
))
3182 else if (code0
== POINTER_TYPE
&& code1
== POINTER_TYPE
)
3184 register tree tt0
= TYPE_MAIN_VARIANT (TREE_TYPE (type0
));
3185 register tree tt1
= TYPE_MAIN_VARIANT (TREE_TYPE (type1
));
3187 if (comp_target_types (type0
, type1
, 1))
3188 result_type
= common_type (type0
, type1
);
3189 else if (tt0
== void_type_node
)
3191 if (pedantic
&& TREE_CODE (tt1
) == FUNCTION_TYPE
3192 && tree_int_cst_lt (TYPE_SIZE (type0
), TYPE_SIZE (type1
)))
3193 pedwarn ("ANSI C++ forbids comparison of `void *' with function pointer");
3194 else if (TREE_CODE (tt1
) == OFFSET_TYPE
)
3195 pedwarn ("ANSI C++ forbids conversion of a pointer to member to `void *'");
3197 else if (tt1
== void_type_node
)
3199 if (pedantic
&& TREE_CODE (tt0
) == FUNCTION_TYPE
3200 && tree_int_cst_lt (TYPE_SIZE (type1
), TYPE_SIZE (type0
)))
3201 pedwarn ("ANSI C++ forbids comparison of `void *' with function pointer");
3204 cp_pedwarn ("comparison of distinct pointer types `%T' and `%T' lacks a cast",
3207 if (result_type
== NULL_TREE
)
3208 result_type
= ptr_type_node
;
3210 else if (code0
== POINTER_TYPE
&& TREE_CODE (op1
) == INTEGER_CST
3211 && integer_zerop (op1
))
3212 result_type
= type0
;
3213 else if (code1
== POINTER_TYPE
&& TREE_CODE (op0
) == INTEGER_CST
3214 && integer_zerop (op0
))
3215 result_type
= type1
;
3216 else if (code0
== POINTER_TYPE
&& code1
== INTEGER_TYPE
)
3218 result_type
= type0
;
3219 error ("ANSI C++ forbids comparison between pointer and integer");
3221 else if (code0
== INTEGER_TYPE
&& code1
== POINTER_TYPE
)
3223 result_type
= type1
;
3224 error ("ANSI C++ forbids comparison between pointer and integer");
3226 else if (TYPE_PTRMEMFUNC_P (type0
) && TREE_CODE (op1
) == INTEGER_CST
3227 && integer_zerop (op1
))
3229 op0
= build_component_ref (op0
, index_identifier
, 0, 0);
3230 op1
= integer_zero_node
;
3231 result_type
= TREE_TYPE (op0
);
3233 else if (TYPE_PTRMEMFUNC_P (type1
) && TREE_CODE (op0
) == INTEGER_CST
3234 && integer_zerop (op0
))
3236 op0
= build_component_ref (op1
, index_identifier
, 0, 0);
3237 op1
= integer_zero_node
;
3238 result_type
= TREE_TYPE (op0
);
3240 else if (TYPE_PTRMEMFUNC_P (type0
) && TYPE_PTRMEMFUNC_P (type1
)
3241 && (TYPE_PTRMEMFUNC_FN_TYPE (type0
)
3242 == TYPE_PTRMEMFUNC_FN_TYPE (type1
)))
3244 /* The code we generate for the test is:
3246 (op0.index == op1.index
3247 && ((op1.index != -1 && op0.delta2 == op1.delta2)
3248 || op0.pfn == op1.pfn)) */
3250 tree index0
= build_component_ref (op0
, index_identifier
, 0, 0);
3251 tree index1
= save_expr (build_component_ref (op1
, index_identifier
, 0, 0));
3252 tree pfn0
= PFN_FROM_PTRMEMFUNC (op0
);
3253 tree pfn1
= PFN_FROM_PTRMEMFUNC (op1
);
3254 tree delta20
= DELTA2_FROM_PTRMEMFUNC (op0
);
3255 tree delta21
= DELTA2_FROM_PTRMEMFUNC (op1
);
3257 tree integer_neg_one_node
3258 = build_binary_op (MINUS_EXPR
, integer_zero_node
, integer_one_node
, 1);
3259 e1
= build_binary_op (EQ_EXPR
, index0
, index1
, 1);
3260 e2
= build_binary_op (NE_EXPR
, index1
, integer_neg_one_node
, 1);
3261 e2
= build_binary_op (TRUTH_ANDIF_EXPR
, e2
, build_binary_op (EQ_EXPR
, delta20
, delta21
, 1), 1);
3262 e3
= build_binary_op (EQ_EXPR
, pfn0
, pfn1
, 1);
3263 e2
= build_binary_op (TRUTH_ORIF_EXPR
, e2
, e3
, 1);
3264 e2
= build_binary_op (TRUTH_ANDIF_EXPR
, e1
, e2
, 1);
3265 if (code
== EQ_EXPR
)
3267 return build_binary_op (EQ_EXPR
, e2
, integer_zero_node
, 1);
3269 else if (TYPE_PTRMEMFUNC_P (type0
)
3270 && TYPE_PTRMEMFUNC_FN_TYPE (type0
) == type1
)
3272 tree index0
= build_component_ref (op0
, index_identifier
, 0, 0);
3274 tree pfn0
= PFN_FROM_PTRMEMFUNC (op0
);
3275 tree delta20
= DELTA2_FROM_PTRMEMFUNC (op0
);
3276 tree delta21
= integer_zero_node
;
3278 tree integer_neg_one_node
3279 = build_binary_op (MINUS_EXPR
, integer_zero_node
, integer_one_node
, 1);
3280 if (TREE_CODE (TREE_OPERAND (op1
, 0)) == FUNCTION_DECL
3281 && DECL_VINDEX (TREE_OPERAND (op1
, 0)))
3283 /* Map everything down one to make room for the null pointer to member. */
3284 index1
= size_binop (PLUS_EXPR
,
3285 DECL_VINDEX (TREE_OPERAND (op1
, 0)),
3287 op1
= integer_zero_node
;
3288 delta21
= CLASSTYPE_VFIELD (TYPE_METHOD_BASETYPE (TREE_TYPE (type1
)));
3289 delta21
= DECL_FIELD_BITPOS (delta21
);
3290 delta21
= size_binop (FLOOR_DIV_EXPR
, delta21
, size_int (BITS_PER_UNIT
));
3293 index1
= integer_neg_one_node
;
3295 tree nop1
= build1 (NOP_EXPR
, TYPE_PTRMEMFUNC_FN_TYPE (type0
), op1
);
3296 TREE_CONSTANT (nop1
) = TREE_CONSTANT (op1
);
3299 e1
= build_binary_op (EQ_EXPR
, index0
, index1
, 1);
3300 e2
= build_binary_op (NE_EXPR
, index1
, integer_neg_one_node
, 1);
3301 e2
= build_binary_op (TRUTH_ANDIF_EXPR
, e2
, build_binary_op (EQ_EXPR
, delta20
, delta21
, 1), 1);
3302 e3
= build_binary_op (EQ_EXPR
, pfn0
, op1
, 1);
3303 e2
= build_binary_op (TRUTH_ORIF_EXPR
, e2
, e3
, 1);
3304 e2
= build_binary_op (TRUTH_ANDIF_EXPR
, e1
, e2
, 1);
3305 if (code
== EQ_EXPR
)
3307 return build_binary_op (EQ_EXPR
, e2
, integer_zero_node
, 1);
3309 else if (TYPE_PTRMEMFUNC_P (type1
)
3310 && TYPE_PTRMEMFUNC_FN_TYPE (type1
) == type0
)
3312 return build_binary_op (code
, op1
, op0
, 1);
3318 if ((code0
== INTEGER_TYPE
|| code0
== REAL_TYPE
)
3319 && (code1
== INTEGER_TYPE
|| code1
== REAL_TYPE
))
3321 else if (code0
== POINTER_TYPE
&& code1
== POINTER_TYPE
)
3323 if (comp_target_types (type0
, type1
, 1))
3324 result_type
= common_type (type0
, type1
);
3327 cp_pedwarn ("comparison of distinct pointer types `%T' and `%T' lacks a cast",
3329 result_type
= ptr_type_node
;
3338 build_type
= boolean_type_node
;
3339 if ((code0
== INTEGER_TYPE
|| code0
== REAL_TYPE
)
3340 && (code1
== INTEGER_TYPE
|| code1
== REAL_TYPE
))
3342 else if (code0
== POINTER_TYPE
&& code1
== POINTER_TYPE
)
3344 if (comp_target_types (type0
, type1
, 1))
3345 result_type
= common_type (type0
, type1
);
3348 cp_pedwarn ("comparison of distinct pointer types `%T' and `%T' lacks a cast",
3350 result_type
= ptr_type_node
;
3353 else if (code0
== POINTER_TYPE
&& TREE_CODE (op1
) == INTEGER_CST
3354 && integer_zerop (op1
))
3355 result_type
= type0
;
3356 else if (code1
== POINTER_TYPE
&& TREE_CODE (op0
) == INTEGER_CST
3357 && integer_zerop (op0
))
3358 result_type
= type1
;
3359 else if (code0
== POINTER_TYPE
&& code1
== INTEGER_TYPE
)
3361 result_type
= type0
;
3363 pedwarn ("ANSI C++ forbids comparison between pointer and integer");
3364 else if (! flag_traditional
)
3365 warning ("comparison between pointer and integer");
3367 else if (code0
== INTEGER_TYPE
&& code1
== POINTER_TYPE
)
3369 result_type
= type1
;
3371 pedwarn ("ANSI C++ forbids comparison between pointer and integer");
3372 else if (! flag_traditional
)
3373 warning ("comparison between pointer and integer");
3378 if ((code0
== INTEGER_TYPE
|| code0
== REAL_TYPE
)
3379 && (code1
== INTEGER_TYPE
|| code1
== REAL_TYPE
))
3381 if (shorten
|| common
|| short_compare
)
3382 result_type
= common_type (type0
, type1
);
3384 /* For certain operations (which identify themselves by shorten != 0)
3385 if both args were extended from the same smaller type,
3386 do the arithmetic in that type and then extend.
3388 shorten !=0 and !=1 indicates a bitwise operation.
3389 For them, this optimization is safe only if
3390 both args are zero-extended or both are sign-extended.
3391 Otherwise, we might change the result.
3392 Eg, (short)-1 | (unsigned short)-1 is (int)-1
3393 but calculated in (unsigned short) it would be (unsigned short)-1. */
3397 int unsigned0
, unsigned1
;
3398 tree arg0
= get_narrower (op0
, &unsigned0
);
3399 tree arg1
= get_narrower (op1
, &unsigned1
);
3400 /* UNS is 1 if the operation to be done is an unsigned one. */
3401 int uns
= TREE_UNSIGNED (result_type
);
3404 final_type
= result_type
;
3406 /* Handle the case that OP0 does not *contain* a conversion
3407 but it *requires* conversion to FINAL_TYPE. */
3409 if (op0
== arg0
&& TREE_TYPE (op0
) != final_type
)
3410 unsigned0
= TREE_UNSIGNED (TREE_TYPE (op0
));
3411 if (op1
== arg1
&& TREE_TYPE (op1
) != final_type
)
3412 unsigned1
= TREE_UNSIGNED (TREE_TYPE (op1
));
3414 /* Now UNSIGNED0 is 1 if ARG0 zero-extends to FINAL_TYPE. */
3416 /* For bitwise operations, signedness of nominal type
3417 does not matter. Consider only how operands were extended. */
3421 /* Note that in all three cases below we refrain from optimizing
3422 an unsigned operation on sign-extended args.
3423 That would not be valid. */
3425 /* Both args variable: if both extended in same way
3426 from same width, do it in that width.
3427 Do it unsigned if args were zero-extended. */
3428 if ((TYPE_PRECISION (TREE_TYPE (arg0
))
3429 < TYPE_PRECISION (result_type
))
3430 && (TYPE_PRECISION (TREE_TYPE (arg1
))
3431 == TYPE_PRECISION (TREE_TYPE (arg0
)))
3432 && unsigned0
== unsigned1
3433 && (unsigned0
|| !uns
))
3435 = signed_or_unsigned_type (unsigned0
,
3436 common_type (TREE_TYPE (arg0
), TREE_TYPE (arg1
)));
3437 else if (TREE_CODE (arg0
) == INTEGER_CST
3438 && (unsigned1
|| !uns
)
3439 && (TYPE_PRECISION (TREE_TYPE (arg1
))
3440 < TYPE_PRECISION (result_type
))
3441 && (type
= signed_or_unsigned_type (unsigned1
,
3443 int_fits_type_p (arg0
, type
)))
3445 else if (TREE_CODE (arg1
) == INTEGER_CST
3446 && (unsigned0
|| !uns
)
3447 && (TYPE_PRECISION (TREE_TYPE (arg0
))
3448 < TYPE_PRECISION (result_type
))
3449 && (type
= signed_or_unsigned_type (unsigned0
,
3451 int_fits_type_p (arg1
, type
)))
3455 /* Shifts can be shortened if shifting right. */
3460 tree arg0
= get_narrower (op0
, &unsigned_arg
);
3462 final_type
= result_type
;
3464 if (arg0
== op0
&& final_type
== TREE_TYPE (op0
))
3465 unsigned_arg
= TREE_UNSIGNED (TREE_TYPE (op0
));
3467 if (TYPE_PRECISION (TREE_TYPE (arg0
)) < TYPE_PRECISION (result_type
)
3468 /* We can shorten only if the shift count is less than the
3469 number of bits in the smaller type size. */
3470 && TREE_INT_CST_HIGH (op1
) == 0
3471 && TYPE_PRECISION (TREE_TYPE (arg0
)) > TREE_INT_CST_LOW (op1
)
3472 /* If arg is sign-extended and then unsigned-shifted,
3473 we can simulate this with a signed shift in arg's type
3474 only if the extended result is at least twice as wide
3475 as the arg. Otherwise, the shift could use up all the
3476 ones made by sign-extension and bring in zeros.
3477 We can't optimize that case at all, but in most machines
3478 it never happens because available widths are 2**N. */
3479 && (!TREE_UNSIGNED (final_type
)
3481 || ((unsigned) 2 * TYPE_PRECISION (TREE_TYPE (arg0
))
3482 <= TYPE_PRECISION (result_type
))))
3484 /* Do an unsigned shift if the operand was zero-extended. */
3486 = signed_or_unsigned_type (unsigned_arg
,
3488 /* Convert value-to-be-shifted to that type. */
3489 if (TREE_TYPE (op0
) != result_type
)
3490 op0
= convert (result_type
, op0
);
3495 /* Comparison operations are shortened too but differently.
3496 They identify themselves by setting short_compare = 1. */
3500 /* Don't write &op0, etc., because that would prevent op0
3501 from being kept in a register.
3502 Instead, make copies of the our local variables and
3503 pass the copies by reference, then copy them back afterward. */
3504 tree xop0
= op0
, xop1
= op1
, xresult_type
= result_type
;
3505 enum tree_code xresultcode
= resultcode
;
3507 = shorten_compare (&xop0
, &xop1
, &xresult_type
, &xresultcode
);
3509 return convert (boolean_type_node
, val
);
3510 op0
= xop0
, op1
= xop1
;
3512 resultcode
= xresultcode
;
3515 if (short_compare
&& extra_warnings
)
3517 int op0_signed
= ! TREE_UNSIGNED (TREE_TYPE (orig_op0
));
3518 int op1_signed
= ! TREE_UNSIGNED (TREE_TYPE (orig_op1
));
3520 int unsignedp0
, unsignedp1
;
3521 tree primop0
= get_narrower (op0
, &unsignedp0
);
3522 tree primop1
= get_narrower (op1
, &unsignedp1
);
3524 /* Give warnings for comparisons between signed and unsigned
3525 quantities that may fail. */
3526 /* Do the checking based on the original operand trees, so that
3527 casts will be considered, but default promotions won't be. */
3529 /* Do not warn if the comparison is being done in a signed type,
3530 since the signed type will only be chosen if it can represent
3531 all the values of the unsigned type. */
3532 if (! TREE_UNSIGNED (result_type
))
3534 /* Do not warn if both operands are unsigned. */
3535 else if (op0_signed
== op1_signed
)
3537 /* Do not warn if the signed quantity is an unsuffixed
3538 integer literal (or some static constant expression
3539 involving such literals) and it is non-negative. */
3540 else if ((op0_signed
&& TREE_CODE (orig_op0
) == INTEGER_CST
3541 && tree_int_cst_sgn (orig_op0
) >= 0)
3542 || (op1_signed
&& TREE_CODE (orig_op1
) == INTEGER_CST
3543 && tree_int_cst_sgn (orig_op1
) >= 0))
3545 /* Do not warn if the comparison is an equality operation,
3546 the unsigned quantity is an integral constant and it does
3547 not use the most significant bit of result_type. */
3548 else if ((resultcode
== EQ_EXPR
|| resultcode
== NE_EXPR
)
3549 && ((op0_signed
&& TREE_CODE (orig_op1
) == INTEGER_CST
3550 && int_fits_type_p (orig_op1
, signed_type (result_type
))
3551 || (op1_signed
&& TREE_CODE (orig_op0
) == INTEGER_CST
3552 && int_fits_type_p (orig_op0
, signed_type (result_type
))))))
3555 warning ("comparison between signed and unsigned");
3557 /* Warn if two unsigned values are being compared in a size
3558 larger than their original size, and one (and only one) is the
3559 result of a `~' operator. This comparison will always fail.
3561 Also warn if one operand is a constant, and the constant does not
3562 have all bits set that are set in the ~ operand when it is
3565 if (TREE_CODE (primop0
) == BIT_NOT_EXPR
3566 ^ TREE_CODE (primop1
) == BIT_NOT_EXPR
)
3568 if (TREE_CODE (primop0
) == BIT_NOT_EXPR
)
3569 primop0
= get_narrower (TREE_OPERAND (op0
, 0), &unsignedp0
);
3570 if (TREE_CODE (primop1
) == BIT_NOT_EXPR
)
3571 primop1
= get_narrower (TREE_OPERAND (op1
, 0), &unsignedp1
);
3573 if (TREE_CODE (primop0
) == INTEGER_CST
3574 || TREE_CODE (primop1
) == INTEGER_CST
)
3577 HOST_WIDE_INT constant
, mask
;
3581 if (TREE_CODE (primop0
) == INTEGER_CST
)
3584 unsignedp
= unsignedp1
;
3585 constant
= TREE_INT_CST_LOW (primop0
);
3590 unsignedp
= unsignedp0
;
3591 constant
= TREE_INT_CST_LOW (primop1
);
3594 bits
= TYPE_PRECISION (TREE_TYPE (primop
));
3595 if (bits
< TYPE_PRECISION (result_type
)
3596 && bits
< HOST_BITS_PER_LONG
&& unsignedp
)
3598 mask
= (~ (HOST_WIDE_INT
) 0) << bits
;
3599 if ((mask
& constant
) != mask
)
3600 warning ("comparison of promoted ~unsigned with constant");
3603 else if (unsignedp0
&& unsignedp1
3604 && (TYPE_PRECISION (TREE_TYPE (primop0
))
3605 < TYPE_PRECISION (result_type
))
3606 && (TYPE_PRECISION (TREE_TYPE (primop1
))
3607 < TYPE_PRECISION (result_type
)))
3608 warning ("comparison of promoted ~unsigned with unsigned");
3613 /* At this point, RESULT_TYPE must be nonzero to avoid an error message.
3614 If CONVERTED is zero, both args will be converted to type RESULT_TYPE.
3615 Then the expression will be built.
3616 It will be given type FINAL_TYPE if that is nonzero;
3617 otherwise, it will be given type RESULT_TYPE. */
3621 cp_error ("invalid operands `%T' and `%T' to binary `%O'",
3622 TREE_TYPE (orig_op0
), TREE_TYPE (orig_op1
), error_code
);
3623 return error_mark_node
;
3628 if (TREE_TYPE (op0
) != result_type
)
3629 op0
= convert (result_type
, op0
);
3630 if (TREE_TYPE (op1
) != result_type
)
3631 op1
= convert (result_type
, op1
);
3634 if (build_type
== NULL_TREE
)
3635 build_type
= result_type
;
3638 register tree result
= build (resultcode
, build_type
, op0
, op1
);
3639 register tree folded
;
3641 folded
= fold (result
);
3642 if (folded
== result
)
3643 TREE_CONSTANT (folded
) = TREE_CONSTANT (op0
) & TREE_CONSTANT (op1
);
3644 if (final_type
!= 0)
3645 return convert (final_type
, folded
);
3650 /* Return a tree for the sum or difference (RESULTCODE says which)
3651 of pointer PTROP and integer INTOP. */
3654 pointer_int_sum (resultcode
, ptrop
, intop
)
3655 enum tree_code resultcode
;
3656 register tree ptrop
, intop
;
3660 register tree result
;
3661 register tree folded
= fold (intop
);
3663 /* The result is a pointer of the same type that is being added. */
3665 register tree result_type
= TREE_TYPE (ptrop
);
3667 if (TREE_CODE (TREE_TYPE (result_type
)) == VOID_TYPE
)
3669 if (pedantic
|| warn_pointer_arith
)
3670 pedwarn ("ANSI C++ forbids using pointer of type `void *' in arithmetic");
3671 size_exp
= integer_one_node
;
3673 else if (TREE_CODE (TREE_TYPE (result_type
)) == FUNCTION_TYPE
)
3675 if (pedantic
|| warn_pointer_arith
)
3676 pedwarn ("ANSI C++ forbids using pointer to a function in arithmetic");
3677 size_exp
= integer_one_node
;
3679 else if (TREE_CODE (TREE_TYPE (result_type
)) == METHOD_TYPE
)
3681 if (pedantic
|| warn_pointer_arith
)
3682 pedwarn ("ANSI C++ forbids using pointer to a method in arithmetic");
3683 size_exp
= integer_one_node
;
3685 else if (TREE_CODE (TREE_TYPE (result_type
)) == OFFSET_TYPE
)
3688 pedwarn ("ANSI C++ forbids using pointer to a member in arithmetic");
3689 size_exp
= integer_one_node
;
3692 size_exp
= size_in_bytes (TREE_TYPE (result_type
));
3694 /* Needed to make OOPS V2R3 work. */
3696 if (TREE_CODE (intop
) == INTEGER_CST
3697 && TREE_INT_CST_LOW (intop
) == 0
3698 && TREE_INT_CST_HIGH (intop
) == 0)
3701 /* If what we are about to multiply by the size of the elements
3702 contains a constant term, apply distributive law
3703 and multiply that constant term separately.
3704 This helps produce common subexpressions. */
3706 if ((TREE_CODE (intop
) == PLUS_EXPR
|| TREE_CODE (intop
) == MINUS_EXPR
)
3707 && ! TREE_CONSTANT (intop
)
3708 && TREE_CONSTANT (TREE_OPERAND (intop
, 1))
3709 && TREE_CONSTANT (size_exp
))
3711 enum tree_code subcode
= resultcode
;
3712 if (TREE_CODE (intop
) == MINUS_EXPR
)
3713 subcode
= (subcode
== PLUS_EXPR
? MINUS_EXPR
: PLUS_EXPR
);
3714 ptrop
= build_binary_op (subcode
, ptrop
, TREE_OPERAND (intop
, 1), 1);
3715 intop
= TREE_OPERAND (intop
, 0);
3718 /* Convert the integer argument to a type the same size as a pointer
3719 so the multiply won't overflow spuriously. */
3721 if (TYPE_PRECISION (TREE_TYPE (intop
)) != POINTER_SIZE
)
3722 intop
= convert (type_for_size (POINTER_SIZE
, 0), intop
);
3724 /* Replace the integer argument with a suitable product by the object size.
3725 Do this multiplication as signed, then convert to the appropriate
3726 pointer type (actually unsigned integral). */
3728 intop
= convert (result_type
,
3729 build_binary_op (MULT_EXPR
, intop
,
3730 convert (TREE_TYPE (intop
), size_exp
), 1));
3732 /* Create the sum or difference. */
3734 result
= build (resultcode
, result_type
, ptrop
, intop
);
3736 folded
= fold (result
);
3737 if (folded
== result
)
3738 TREE_CONSTANT (folded
) = TREE_CONSTANT (ptrop
) & TREE_CONSTANT (intop
);
3742 /* Return a tree for the difference of pointers OP0 and OP1.
3743 The resulting tree has type int. */
3746 pointer_diff (op0
, op1
)
3747 register tree op0
, op1
;
3749 register tree result
, folded
;
3750 tree restype
= ptrdiff_type_node
;
3751 tree target_type
= TREE_TYPE (TREE_TYPE (op0
));
3755 if (TREE_CODE (target_type
) == VOID_TYPE
)
3756 pedwarn ("ANSI C++ forbids using pointer of type `void *' in subtraction");
3757 if (TREE_CODE (target_type
) == FUNCTION_TYPE
)
3758 pedwarn ("ANSI C++ forbids using pointer to a function in subtraction");
3759 if (TREE_CODE (target_type
) == METHOD_TYPE
)
3760 pedwarn ("ANSI C++ forbids using pointer to a method in subtraction");
3761 if (TREE_CODE (target_type
) == OFFSET_TYPE
)
3762 pedwarn ("ANSI C++ forbids using pointer to a member in subtraction");
3765 /* First do the subtraction as integers;
3766 then drop through to build the divide operator. */
3768 op0
= build_binary_op (MINUS_EXPR
,
3769 convert (restype
, op0
), convert (restype
, op1
), 1);
3771 /* This generates an error if op1 is a pointer to an incomplete type. */
3772 if (TYPE_SIZE (TREE_TYPE (TREE_TYPE (op1
))) == 0)
3773 error ("arithmetic on pointer to an incomplete type");
3775 op1
= ((TREE_CODE (target_type
) == VOID_TYPE
3776 || TREE_CODE (target_type
) == FUNCTION_TYPE
3777 || TREE_CODE (target_type
) == METHOD_TYPE
3778 || TREE_CODE (target_type
) == OFFSET_TYPE
)
3780 : size_in_bytes (target_type
));
3782 /* Do the division. */
3784 result
= build (EXACT_DIV_EXPR
, restype
, op0
, convert (restype
, op1
));
3786 folded
= fold (result
);
3787 if (folded
== result
)
3788 TREE_CONSTANT (folded
) = TREE_CONSTANT (op0
) & TREE_CONSTANT (op1
);
3792 /* Handle the case of taking the address of a COMPONENT_REF.
3793 Called by `build_unary_op' and `build_up_reference'.
3795 ARG is the COMPONENT_REF whose address we want.
3796 ARGTYPE is the pointer type that this address should have.
3797 MSG is an error message to print if this COMPONENT_REF is not
3798 addressable (such as a bitfield). */
3801 build_component_addr (arg
, argtype
, msg
)
3805 tree field
= TREE_OPERAND (arg
, 1);
3806 tree basetype
= decl_type_context (field
);
3807 tree rval
= build_unary_op (ADDR_EXPR
, TREE_OPERAND (arg
, 0), 0);
3809 if (DECL_BIT_FIELD (field
))
3811 error (msg
, IDENTIFIER_POINTER (DECL_NAME (field
)));
3812 return error_mark_node
;
3816 cp_warning ("address of `%T::%D' taken", basetype
, field
);
3818 if (TREE_CODE (field
) == FIELD_DECL
3819 && TYPE_USES_COMPLEX_INHERITANCE (basetype
))
3821 /* Can't convert directly to ARGTYPE, since that
3822 may have the same pointer type as one of our
3824 rval
= build1 (NOP_EXPR
, argtype
,
3825 convert_pointer_to (basetype
, rval
));
3826 TREE_CONSTANT (rval
) = TREE_CONSTANT (TREE_OPERAND (rval
, 0));
3829 /* This conversion is harmless. */
3830 rval
= convert_force (argtype
, rval
, 0);
3832 if (! integer_zerop (DECL_FIELD_BITPOS (field
)))
3834 tree offset
= size_binop (EASY_DIV_EXPR
, DECL_FIELD_BITPOS (field
),
3835 size_int (BITS_PER_UNIT
));
3836 int flag
= TREE_CONSTANT (rval
);
3837 rval
= fold (build (PLUS_EXPR
, argtype
,
3838 rval
, convert (argtype
, offset
)));
3839 TREE_CONSTANT (rval
) = flag
;
3844 /* Construct and perhaps optimize a tree representation
3845 for a unary operation. CODE, a tree_code, specifies the operation
3846 and XARG is the operand. */
3849 build_x_unary_op (code
, xarg
)
3850 enum tree_code code
;
3853 /* & rec, on incomplete RECORD_TYPEs is the simple opr &, not an
3855 if (code
== ADDR_EXPR
3856 && ((IS_AGGR_TYPE_CODE (TREE_CODE (TREE_TYPE (xarg
)))
3857 && TYPE_SIZE (TREE_TYPE (xarg
)) == NULL_TREE
)
3858 || (TREE_CODE (xarg
) == OFFSET_REF
)))
3859 /* don't look for a function */;
3862 tree rval
= build_opfncall (code
, LOOKUP_SPECULATIVELY
, xarg
,
3863 NULL_TREE
, NULL_TREE
);
3865 return build_opfncall (code
, LOOKUP_NORMAL
, xarg
,
3866 NULL_TREE
, NULL_TREE
);
3868 return build_unary_op (code
, xarg
, 0);
3871 /* Just like truthvalue_conversion, but we want a CLEANUP_POINT_EXPR. */
3874 condition_conversion (expr
)
3877 tree t
= convert (boolean_type_node
, expr
);
3878 t
= fold (build1 (CLEANUP_POINT_EXPR
, boolean_type_node
, t
));
3882 /* C++: Must handle pointers to members.
3884 Perhaps type instantiation should be extended to handle conversion
3885 from aggregates to types we don't yet know we want? (Or are those
3886 cases typically errors which should be reported?)
3888 NOCONVERT nonzero suppresses the default promotions
3889 (such as from short to int). */
3891 build_unary_op (code
, xarg
, noconvert
)
3892 enum tree_code code
;
3896 /* No default_conversion here. It causes trouble for ADDR_EXPR. */
3897 register tree arg
= xarg
;
3898 register tree argtype
= 0;
3899 char *errstring
= NULL
;
3902 if (arg
== error_mark_node
)
3903 return error_mark_node
;
3908 /* This is used for unary plus, because a CONVERT_EXPR
3909 is enough to prevent anybody from looking inside for
3910 associativity, but won't generate any code. */
3911 if (!(arg
= build_expr_type_conversion
3912 (WANT_ARITH
| WANT_ENUM
| WANT_POINTER
, arg
, 1)))
3913 errstring
= "wrong type argument to unary plus";
3917 arg
= default_conversion (arg
);
3918 arg
= build1 (NON_LVALUE_EXPR
, TREE_TYPE (arg
), arg
);
3923 if (!(arg
= build_expr_type_conversion (WANT_ARITH
| WANT_ENUM
, arg
, 1)))
3924 errstring
= "wrong type argument to unary minus";
3925 else if (!noconvert
)
3926 arg
= default_conversion (arg
);
3930 if (!(arg
= build_expr_type_conversion (WANT_INT
| WANT_ENUM
, arg
, 1)))
3931 errstring
= "wrong type argument to bit-complement";
3932 else if (!noconvert
)
3933 arg
= default_conversion (arg
);
3937 if (!(arg
= build_expr_type_conversion (WANT_ARITH
| WANT_ENUM
, arg
, 1)))
3938 errstring
= "wrong type argument to abs";
3939 else if (!noconvert
)
3940 arg
= default_conversion (arg
);
3943 case TRUTH_NOT_EXPR
:
3944 arg
= convert (boolean_type_node
, arg
);
3945 val
= invert_truthvalue (arg
);
3946 if (arg
!= error_mark_node
)
3948 errstring
= "in argument to unary !";
3954 case PREINCREMENT_EXPR
:
3955 case POSTINCREMENT_EXPR
:
3956 case PREDECREMENT_EXPR
:
3957 case POSTDECREMENT_EXPR
:
3958 /* Handle complex lvalues (when permitted)
3959 by reduction to simpler cases. */
3961 val
= unary_complex_lvalue (code
, arg
);
3965 /* Report invalid types. */
3967 if (!(arg
= build_expr_type_conversion (WANT_ARITH
| WANT_POINTER
,
3970 if (code
== PREINCREMENT_EXPR
)
3971 errstring
="no pre-increment operator for type";
3972 else if (code
== POSTINCREMENT_EXPR
)
3973 errstring
="no post-increment operator for type";
3974 else if (code
== PREDECREMENT_EXPR
)
3975 errstring
="no pre-decrement operator for type";
3977 errstring
="no post-decrement operator for type";
3981 /* Report something read-only. */
3983 if (TYPE_READONLY (TREE_TYPE (arg
))
3984 || TREE_READONLY (arg
))
3985 readonly_error (arg
, ((code
== PREINCREMENT_EXPR
3986 || code
== POSTINCREMENT_EXPR
)
3987 ? "increment" : "decrement"),
3992 tree result_type
= TREE_TYPE (arg
);
3994 arg
= get_unwidened (arg
, 0);
3995 argtype
= TREE_TYPE (arg
);
3997 /* ARM $5.2.5 last annotation says this should be forbidden. */
3998 if (TREE_CODE (argtype
) == ENUMERAL_TYPE
)
3999 pedwarn ("ANSI C++ forbids %sing an enum",
4000 (code
== PREINCREMENT_EXPR
|| code
== POSTINCREMENT_EXPR
)
4001 ? "increment" : "decrement");
4003 /* Compute the increment. */
4005 if (TREE_CODE (argtype
) == POINTER_TYPE
)
4007 enum tree_code tmp
= TREE_CODE (TREE_TYPE (argtype
));
4008 if (TYPE_SIZE (TREE_TYPE (argtype
)) == 0)
4009 cp_error ("cannot %s a pointer to incomplete type `%T'",
4010 ((code
== PREINCREMENT_EXPR
4011 || code
== POSTINCREMENT_EXPR
)
4012 ? "increment" : "decrement"), TREE_TYPE (argtype
));
4013 else if (tmp
== FUNCTION_TYPE
|| tmp
== METHOD_TYPE
4014 || tmp
== VOID_TYPE
|| tmp
== OFFSET_TYPE
)
4015 cp_pedwarn ("ANSI C++ forbids %sing a pointer of type `%T'",
4016 ((code
== PREINCREMENT_EXPR
4017 || code
== POSTINCREMENT_EXPR
)
4018 ? "increment" : "decrement"), argtype
);
4019 inc
= c_sizeof_nowarn (TREE_TYPE (argtype
));
4022 inc
= integer_one_node
;
4024 inc
= convert (argtype
, inc
);
4026 /* Handle incrementing a cast-expression. */
4028 switch (TREE_CODE (arg
))
4033 case FIX_TRUNC_EXPR
:
4034 case FIX_FLOOR_EXPR
:
4035 case FIX_ROUND_EXPR
:
4038 tree incremented
, modify
, value
;
4039 if (! lvalue_p (arg
) && pedantic
)
4040 pedwarn ("cast to non-reference type used as lvalue");
4041 arg
= stabilize_reference (arg
);
4042 if (code
== PREINCREMENT_EXPR
|| code
== PREDECREMENT_EXPR
)
4045 value
= save_expr (arg
);
4046 incremented
= build (((code
== PREINCREMENT_EXPR
4047 || code
== POSTINCREMENT_EXPR
)
4048 ? PLUS_EXPR
: MINUS_EXPR
),
4049 argtype
, value
, inc
);
4050 TREE_SIDE_EFFECTS (incremented
) = 1;
4051 modify
= build_modify_expr (arg
, NOP_EXPR
, incremented
);
4052 return build (COMPOUND_EXPR
, TREE_TYPE (arg
), modify
, value
);
4056 /* Complain about anything else that is not a true lvalue. */
4057 if (!lvalue_or_else (arg
, ((code
== PREINCREMENT_EXPR
4058 || code
== POSTINCREMENT_EXPR
)
4059 ? "increment" : "decrement")))
4060 return error_mark_node
;
4062 /* Forbid using -- on `bool'. */
4063 if (TREE_TYPE (arg
) == boolean_type_node
)
4065 if (code
== POSTDECREMENT_EXPR
|| code
== PREDECREMENT_EXPR
)
4067 cp_error ("invalid use of `--' on bool variable `%D'", arg
);
4068 return error_mark_node
;
4071 /* This will only work if someone can convince Kenner to accept
4072 my patch to expand_increment. (jason) */
4073 val
= build (code
, TREE_TYPE (arg
), arg
, inc
);
4075 if (code
== POSTINCREMENT_EXPR
)
4077 arg
= stabilize_reference (arg
);
4078 val
= build (MODIFY_EXPR
, TREE_TYPE (arg
), arg
,
4080 TREE_SIDE_EFFECTS (val
) = 1;
4081 arg
= save_expr (arg
);
4082 val
= build (COMPOUND_EXPR
, TREE_TYPE (arg
), val
, arg
);
4083 val
= build (COMPOUND_EXPR
, TREE_TYPE (arg
), arg
, val
);
4086 val
= build (MODIFY_EXPR
, TREE_TYPE (arg
), arg
,
4091 val
= build (code
, TREE_TYPE (arg
), arg
, inc
);
4093 TREE_SIDE_EFFECTS (val
) = 1;
4094 return convert (result_type
, val
);
4098 /* Note that this operation never does default_conversion
4099 regardless of NOCONVERT. */
4101 argtype
= TREE_TYPE (arg
);
4102 if (TREE_CODE (argtype
) == REFERENCE_TYPE
)
4104 arg
= build1 (CONVERT_EXPR
, build_pointer_type (TREE_TYPE (TREE_TYPE (arg
))), arg
);
4105 TREE_REFERENCE_EXPR (arg
) = 1;
4109 && TREE_CODE (arg
) == FUNCTION_DECL
4111 && DECL_CONTEXT (arg
) == NULL_TREE
4112 && IDENTIFIER_LENGTH (DECL_NAME (arg
)) == 4
4113 && IDENTIFIER_POINTER (DECL_NAME (arg
))[0] == 'm'
4114 && ! strcmp (IDENTIFIER_POINTER (DECL_NAME (arg
)), "main"))
4116 pedwarn ("taking address of function `main'");
4118 /* Let &* cancel out to simplify resulting code. */
4119 if (TREE_CODE (arg
) == INDIRECT_REF
)
4121 /* We don't need to have `current_class_decl' wrapped in a
4122 NON_LVALUE_EXPR node. */
4124 return current_class_decl
;
4126 /* Keep `default_conversion' from converting if
4127 ARG is of REFERENCE_TYPE. */
4128 arg
= TREE_OPERAND (arg
, 0);
4129 if (TREE_CODE (TREE_TYPE (arg
)) == REFERENCE_TYPE
)
4131 if (TREE_CODE (arg
) == VAR_DECL
&& DECL_INITIAL (arg
)
4132 && !TREE_SIDE_EFFECTS (DECL_INITIAL (arg
)))
4133 arg
= DECL_INITIAL (arg
);
4134 arg
= build1 (CONVERT_EXPR
, build_pointer_type (TREE_TYPE (TREE_TYPE (arg
))), arg
);
4135 TREE_REFERENCE_EXPR (arg
) = 1;
4136 TREE_CONSTANT (arg
) = TREE_CONSTANT (TREE_OPERAND (arg
, 0));
4138 else if (lvalue_p (arg
))
4139 /* Don't let this be an lvalue. */
4140 return non_lvalue (arg
);
4144 /* For &x[y], return x+y */
4145 if (TREE_CODE (arg
) == ARRAY_REF
)
4147 if (mark_addressable (TREE_OPERAND (arg
, 0)) == 0)
4148 return error_mark_node
;
4149 return build_binary_op (PLUS_EXPR
, TREE_OPERAND (arg
, 0),
4150 TREE_OPERAND (arg
, 1), 1);
4153 /* Uninstantiated types are all functions. Taking the
4154 address of a function is a no-op, so just return the
4157 if (TREE_CODE (arg
) == IDENTIFIER_NODE
4158 && IDENTIFIER_OPNAME_P (arg
))
4160 my_friendly_abort (117);
4161 /* We don't know the type yet, so just work around the problem.
4162 We know that this will resolve to an lvalue. */
4163 return build1 (ADDR_EXPR
, unknown_type_node
, arg
);
4166 if (TREE_CODE (arg
) == TREE_LIST
)
4168 if (TREE_CODE (TREE_VALUE (arg
)) == FUNCTION_DECL
4169 && DECL_CHAIN (TREE_VALUE (arg
)) == NULL_TREE
)
4170 /* Unique overloaded non-member function. */
4171 return build_unary_op (ADDR_EXPR
, TREE_VALUE (arg
), 0);
4172 if (TREE_CHAIN (arg
) == NULL_TREE
4173 && TREE_CODE (TREE_VALUE (arg
)) == TREE_LIST
4174 && DECL_CHAIN (TREE_VALUE (TREE_VALUE (arg
))) == NULL_TREE
)
4175 /* Unique overloaded member function. */
4176 return build_unary_op (ADDR_EXPR
, TREE_VALUE (TREE_VALUE (arg
)),
4178 return build1 (ADDR_EXPR
, unknown_type_node
, arg
);
4181 /* Handle complex lvalues (when permitted)
4182 by reduction to simpler cases. */
4183 val
= unary_complex_lvalue (code
, arg
);
4187 switch (TREE_CODE (arg
))
4192 case FIX_TRUNC_EXPR
:
4193 case FIX_FLOOR_EXPR
:
4194 case FIX_ROUND_EXPR
:
4196 if (! lvalue_p (arg
) && pedantic
)
4197 pedwarn ("taking the address of a cast to non-reference type");
4200 /* Allow the address of a constructor if all the elements
4202 if (TREE_CODE (arg
) == CONSTRUCTOR
&& TREE_CONSTANT (arg
))
4204 /* Anything not already handled and not a true memory reference
4206 else if (TREE_CODE (argtype
) != FUNCTION_TYPE
4207 && TREE_CODE (argtype
) != METHOD_TYPE
4208 && !lvalue_or_else (arg
, "unary `&'"))
4209 return error_mark_node
;
4211 /* Ordinary case; arg is a COMPONENT_REF or a decl. */
4212 /* If the lvalue is const or volatile,
4213 merge that into the type that the address will point to. */
4214 if (TREE_CODE_CLASS (TREE_CODE (arg
)) == 'd'
4215 || TREE_CODE_CLASS (TREE_CODE (arg
)) == 'r')
4217 if (TREE_READONLY (arg
) || TREE_THIS_VOLATILE (arg
))
4218 argtype
= cp_build_type_variant (argtype
,
4219 TREE_READONLY (arg
),
4220 TREE_THIS_VOLATILE (arg
));
4223 argtype
= build_pointer_type (argtype
);
4225 if (mark_addressable (arg
) == 0)
4226 return error_mark_node
;
4231 if (TREE_CODE (arg
) == COMPONENT_REF
)
4232 addr
= build_component_addr (arg
, argtype
,
4233 "attempt to take address of bit-field structure member `%s'");
4235 addr
= build1 (code
, argtype
, arg
);
4237 /* Address of a static or external variable or
4238 function counts as a constant */
4240 TREE_CONSTANT (addr
) = 1;
4248 argtype
= TREE_TYPE (arg
);
4249 return fold (build1 (code
, argtype
, arg
));
4253 return error_mark_node
;
4256 /* If CONVERSIONS is a conversion expression or a nested sequence of such,
4257 convert ARG with the same conversions in the same order
4258 and return the result. */
4261 convert_sequence (conversions
, arg
)
4265 switch (TREE_CODE (conversions
))
4270 case FIX_TRUNC_EXPR
:
4271 case FIX_FLOOR_EXPR
:
4272 case FIX_ROUND_EXPR
:
4274 return convert (TREE_TYPE (conversions
),
4275 convert_sequence (TREE_OPERAND (conversions
, 0),
4283 /* Apply unary lvalue-demanding operator CODE to the expression ARG
4284 for certain kinds of expressions which are not really lvalues
4285 but which we can accept as lvalues.
4287 If ARG is not a kind of expression we can handle, return zero. */
4290 unary_complex_lvalue (code
, arg
)
4291 enum tree_code code
;
4294 /* Handle (a, b) used as an "lvalue". */
4295 if (TREE_CODE (arg
) == COMPOUND_EXPR
)
4297 tree real_result
= build_unary_op (code
, TREE_OPERAND (arg
, 1), 0);
4298 return build (COMPOUND_EXPR
, TREE_TYPE (real_result
),
4299 TREE_OPERAND (arg
, 0), real_result
);
4302 /* Handle (a ? b : c) used as an "lvalue". */
4303 if (TREE_CODE (arg
) == COND_EXPR
)
4304 return rationalize_conditional_expr (code
, arg
);
4306 if (TREE_CODE (arg
) == MODIFY_EXPR
4307 || TREE_CODE (arg
) == PREINCREMENT_EXPR
4308 || TREE_CODE (arg
) == PREDECREMENT_EXPR
)
4309 return unary_complex_lvalue
4310 (code
, build (COMPOUND_EXPR
, TREE_TYPE (TREE_OPERAND (arg
, 0)),
4311 arg
, TREE_OPERAND (arg
, 0)));
4313 if (code
!= ADDR_EXPR
)
4316 /* Handle (a = b) used as an "lvalue" for `&'. */
4317 if (TREE_CODE (arg
) == MODIFY_EXPR
4318 || TREE_CODE (arg
) == INIT_EXPR
)
4320 tree real_result
= build_unary_op (code
, TREE_OPERAND (arg
, 0), 0);
4321 return build (COMPOUND_EXPR
, TREE_TYPE (real_result
), arg
, real_result
);
4324 if (TREE_CODE (arg
) == WITH_CLEANUP_EXPR
)
4326 tree real_result
= build_unary_op (code
, TREE_OPERAND (arg
, 0), 0);
4327 real_result
= build (WITH_CLEANUP_EXPR
, TREE_TYPE (real_result
),
4328 real_result
, 0, TREE_OPERAND (arg
, 2));
4332 if (TREE_CODE (TREE_TYPE (arg
)) == FUNCTION_TYPE
4333 || TREE_CODE (TREE_TYPE (arg
)) == METHOD_TYPE
4334 || TREE_CODE (TREE_TYPE (arg
)) == OFFSET_TYPE
)
4336 /* The representation of something of type OFFSET_TYPE
4337 is really the representation of a pointer to it.
4338 Here give the representation its true type. */
4342 my_friendly_assert (TREE_CODE (arg
) != SCOPE_REF
, 313);
4344 if (TREE_CODE (arg
) != OFFSET_REF
)
4347 t
= TREE_OPERAND (arg
, 1);
4349 if (TREE_CODE (t
) == FUNCTION_DECL
) /* Check all this code for right semantics. */
4350 return build_unary_op (ADDR_EXPR
, t
, 0);
4351 if (TREE_CODE (t
) == VAR_DECL
)
4352 return build_unary_op (ADDR_EXPR
, t
, 0);
4355 if (TREE_OPERAND (arg
, 0)
4356 && (TREE_CODE (TREE_OPERAND (arg
, 0)) != NOP_EXPR
4357 || TREE_OPERAND (TREE_OPERAND (arg
, 0), 0) != error_mark_node
))
4358 if (TREE_CODE (t
) != FIELD_DECL
)
4360 /* Don't know if this should return address to just
4361 _DECL, or actual address resolved in this expression. */
4362 sorry ("address of bound pointer-to-member expression");
4363 return error_mark_node
;
4366 offset
= get_delta_difference (DECL_FIELD_CONTEXT (t
),
4367 TREE_TYPE (TREE_OPERAND (arg
, 0)),
4369 offset
= size_binop (PLUS_EXPR
, offset
,
4370 size_binop (EASY_DIV_EXPR
,
4371 DECL_FIELD_BITPOS (t
),
4372 size_int (BITS_PER_UNIT
)));
4373 return convert (build_pointer_type (TREE_TYPE (arg
)), offset
);
4377 if (TREE_CODE (arg
) == OFFSET_REF
)
4379 tree left
= TREE_OPERAND (arg
, 0), left_addr
;
4380 tree right_addr
= build_unary_op (ADDR_EXPR
, TREE_OPERAND (arg
, 1), 0);
4383 if (current_class_decl
)
4384 left_addr
= current_class_decl
;
4387 error ("no `this' for pointer to member");
4388 return error_mark_node
;
4391 left_addr
= build_unary_op (ADDR_EXPR
, left
, 0);
4393 return build (PLUS_EXPR
, build_pointer_type (TREE_TYPE (arg
)),
4394 build1 (NOP_EXPR
, integer_type_node
, left_addr
),
4395 build1 (NOP_EXPR
, integer_type_node
, right_addr
));
4398 /* We permit compiler to make function calls returning
4399 objects of aggregate type look like lvalues. */
4403 if (TREE_CODE (targ
) == SAVE_EXPR
)
4404 targ
= TREE_OPERAND (targ
, 0);
4406 if (TREE_CODE (targ
) == CALL_EXPR
&& IS_AGGR_TYPE (TREE_TYPE (targ
)))
4408 if (TREE_CODE (arg
) == SAVE_EXPR
)
4411 targ
= build_cplus_new (TREE_TYPE (arg
), arg
, 1);
4412 return build1 (ADDR_EXPR
, TYPE_POINTER_TO (TREE_TYPE (arg
)), targ
);
4415 if (TREE_CODE (arg
) == SAVE_EXPR
&& TREE_CODE (targ
) == INDIRECT_REF
)
4416 return build (SAVE_EXPR
, TYPE_POINTER_TO (TREE_TYPE (arg
)),
4417 TREE_OPERAND (targ
, 0), current_function_decl
, NULL
);
4419 /* We shouldn't wrap WITH_CLEANUP_EXPRs inside of SAVE_EXPRs, but in case
4420 we do, here's how to handle it. */
4421 if (TREE_CODE (arg
) == SAVE_EXPR
&& TREE_CODE (targ
) == WITH_CLEANUP_EXPR
)
4424 /* Not really a bug, but something to turn on when testing. */
4425 compiler_error ("WITH_CLEANUP_EXPR wrapped in SAVE_EXPR");
4427 return unary_complex_lvalue (ADDR_EXPR
, targ
);
4431 /* Don't let anything else be handled specially. */
4435 /* Mark EXP saying that we need to be able to take the
4436 address of it; it should not be allocated in a register.
4437 Value is 1 if successful.
4439 C++: we do not allow `current_class_decl' to be addressable. */
4442 mark_addressable (exp
)
4445 register tree x
= exp
;
4447 if (TREE_ADDRESSABLE (x
) == 1)
4451 switch (TREE_CODE (x
))
4456 x
= TREE_OPERAND (x
, 0);
4460 if (x
== current_class_decl
)
4462 error ("address of `this' not available");
4463 TREE_ADDRESSABLE (x
) = 1; /* so compiler doesn't die later */
4464 put_var_into_stack (x
);
4469 && TREE_READONLY (x
)
4470 && DECL_RTL (x
) != 0
4471 && ! decl_in_memory_p (x
))
4473 /* We thought this would make a good constant variable,
4474 but we were wrong. */
4475 push_obstacks_nochange ();
4476 end_temporary_allocation ();
4478 TREE_ASM_WRITTEN (x
) = 0;
4480 rest_of_decl_compilation (x
, 0, IDENTIFIER_LOCAL_VALUE (x
) == 0, 0);
4481 TREE_ADDRESSABLE (x
) = 1;
4487 /* Caller should not be trying to mark initialized
4488 constant fields addressable. */
4489 my_friendly_assert (DECL_LANG_SPECIFIC (x
) == 0
4490 || DECL_IN_AGGR_P (x
) == 0
4492 || DECL_EXTERNAL (x
), 314);
4496 /* For C++, we don't warn about taking the address of a register
4497 variable for CONST_DECLs; ARM p97 explicitly says it's okay. */
4498 put_var_into_stack (x
);
4499 TREE_ADDRESSABLE (x
) = 1;
4503 /* We have to test both conditions here. The first may
4504 be non-zero in the case of processing a default function.
4505 The second may be non-zero in the case of a template function. */
4506 x
= DECL_MAIN_VARIANT (x
);
4507 if ((DECL_THIS_INLINE (x
) || DECL_PENDING_INLINE_INFO (x
))
4508 && (DECL_CONTEXT (x
) == NULL_TREE
4509 || TREE_CODE_CLASS (TREE_CODE (DECL_CONTEXT (x
))) != 't'
4510 || ! CLASSTYPE_INTERFACE_ONLY (DECL_CONTEXT (x
))))
4512 mark_inline_for_output (x
);
4513 if (x
== current_function_decl
)
4514 DECL_EXTERNAL (x
) = 0;
4516 TREE_ADDRESSABLE (x
) = 1;
4518 TREE_ADDRESSABLE (DECL_ASSEMBLER_NAME (x
)) = 1;
4526 /* Build and return a conditional expression IFEXP ? OP1 : OP2. */
4529 build_x_conditional_expr (ifexp
, op1
, op2
)
4530 tree ifexp
, op1
, op2
;
4532 tree rval
= NULL_TREE
;
4534 /* See comments in `build_x_binary_op'. */
4536 rval
= build_opfncall (COND_EXPR
, LOOKUP_SPECULATIVELY
, ifexp
, op1
, op2
);
4538 return build_opfncall (COND_EXPR
, LOOKUP_NORMAL
, ifexp
, op1
, op2
);
4540 return build_conditional_expr (ifexp
, op1
, op2
);
4544 build_conditional_expr (ifexp
, op1
, op2
)
4545 tree ifexp
, op1
, op2
;
4547 register tree type1
;
4548 register tree type2
;
4549 register enum tree_code code1
;
4550 register enum tree_code code2
;
4551 register tree result_type
= NULL_TREE
;
4552 tree orig_op1
= op1
, orig_op2
= op2
;
4554 /* If second operand is omitted, it is the same as the first one;
4555 make sure it is calculated only once. */
4559 pedwarn ("ANSI C++ forbids omitting the middle term of a ?: expression");
4560 ifexp
= op1
= save_expr (ifexp
);
4563 ifexp
= convert (boolean_type_node
, ifexp
);
4565 if (TREE_CODE (ifexp
) == ERROR_MARK
)
4566 return error_mark_node
;
4568 op1
= require_instantiated_type (TREE_TYPE (op2
), op1
, error_mark_node
);
4569 if (op1
== error_mark_node
)
4570 return error_mark_node
;
4571 op2
= require_instantiated_type (TREE_TYPE (op1
), op2
, error_mark_node
);
4572 if (op2
== error_mark_node
)
4573 return error_mark_node
;
4575 /* C++: REFERENCE_TYPES must be dereferenced. */
4576 type1
= TREE_TYPE (op1
);
4577 code1
= TREE_CODE (type1
);
4578 type2
= TREE_TYPE (op2
);
4579 code2
= TREE_CODE (type2
);
4581 if (code1
== REFERENCE_TYPE
)
4583 op1
= convert_from_reference (op1
);
4584 type1
= TREE_TYPE (op1
);
4585 code1
= TREE_CODE (type1
);
4587 if (code2
== REFERENCE_TYPE
)
4589 op2
= convert_from_reference (op2
);
4590 type2
= TREE_TYPE (op2
);
4591 code2
= TREE_CODE (type2
);
4594 #if 1 /* Produces wrong result if within sizeof. Sorry. */
4595 /* Don't promote the operands separately if they promote
4596 the same way. Return the unpromoted type and let the combined
4597 value get promoted if necessary. */
4599 if (TYPE_MAIN_VARIANT (type1
) == TYPE_MAIN_VARIANT (type2
)
4600 && code2
!= ARRAY_TYPE
4602 /* For C++, let the enumeral type come through. */
4603 && code2
!= ENUMERAL_TYPE
4605 && code2
!= FUNCTION_TYPE
4606 && code2
!= METHOD_TYPE
)
4610 if (TREE_CONSTANT (ifexp
)
4611 && (TREE_CODE (ifexp
) == INTEGER_CST
4612 || TREE_CODE (ifexp
) == ADDR_EXPR
))
4613 return (integer_zerop (ifexp
) ? op2
: op1
);
4615 if (TREE_CODE (op1
) == CONST_DECL
)
4616 op1
= DECL_INITIAL (op1
);
4617 else if (TREE_READONLY_DECL_P (op1
))
4618 op1
= decl_constant_value (op1
);
4619 if (TREE_CODE (op2
) == CONST_DECL
)
4620 op2
= DECL_INITIAL (op2
);
4621 else if (TREE_READONLY_DECL_P (op2
))
4622 op2
= decl_constant_value (op2
);
4624 type1
= cp_build_type_variant
4626 TREE_READONLY (op1
) || TREE_READONLY (op2
),
4627 TREE_THIS_VOLATILE (op1
) || TREE_THIS_VOLATILE (op2
));
4628 /* ??? This is a kludge to deal with the fact that
4629 we don't sort out integers and enums properly, yet. */
4630 result
= fold (build (COND_EXPR
, type1
, ifexp
, op1
, op2
));
4631 if (TREE_TYPE (result
) != type1
)
4632 result
= build1 (NOP_EXPR
, type1
, result
);
4637 /* They don't match; promote them both and then try to reconcile them.
4638 But don't permit mismatching enum types. */
4639 if (code1
== ENUMERAL_TYPE
)
4641 if (code2
== ENUMERAL_TYPE
)
4643 cp_error ("enumeral mismatch in conditional expression: `%T' vs `%T'", type1
, type2
);
4644 return error_mark_node
;
4646 else if (extra_warnings
&& ! IS_AGGR_TYPE_CODE (code2
)
4647 && type2
!= type_promotes_to (type1
))
4648 warning ("enumeral and non-enumeral type in conditional expression");
4650 else if (extra_warnings
4651 && code2
== ENUMERAL_TYPE
&& ! IS_AGGR_TYPE_CODE (code1
)
4652 && type1
!= type_promotes_to (type2
))
4653 warning ("enumeral and non-enumeral type in conditional expression");
4655 if (code1
!= VOID_TYPE
)
4657 op1
= default_conversion (op1
);
4658 type1
= TREE_TYPE (op1
);
4659 if (TYPE_PTRMEMFUNC_P (type1
))
4660 type1
= TYPE_PTRMEMFUNC_FN_TYPE (type1
);
4661 code1
= TREE_CODE (type1
);
4663 if (code2
!= VOID_TYPE
)
4665 op2
= default_conversion (op2
);
4666 type2
= TREE_TYPE (op2
);
4667 if (TYPE_PTRMEMFUNC_P (type2
))
4668 type2
= TYPE_PTRMEMFUNC_FN_TYPE (type2
);
4669 code2
= TREE_CODE (type2
);
4672 if (code1
== RECORD_TYPE
&& code2
== RECORD_TYPE
4673 && real_lvalue_p (op1
) && real_lvalue_p (op2
)
4674 && comptypes (type1
, type2
, -1))
4676 type1
= build_reference_type (type1
);
4677 type2
= build_reference_type (type2
);
4678 result_type
= common_type (type1
, type2
);
4679 op1
= convert_to_reference (result_type
, op1
, CONV_IMPLICIT
,
4680 LOOKUP_NORMAL
, NULL_TREE
);
4681 op2
= convert_to_reference (result_type
, op2
, CONV_IMPLICIT
,
4682 LOOKUP_NORMAL
, NULL_TREE
);
4684 /* Quickly detect the usual case where op1 and op2 have the same type
4686 else if (TYPE_MAIN_VARIANT (type1
) == TYPE_MAIN_VARIANT (type2
))
4689 result_type
= type1
;
4691 result_type
= cp_build_type_variant
4693 TREE_READONLY (op1
) || TREE_READONLY (op2
),
4694 TREE_THIS_VOLATILE (op1
) || TREE_THIS_VOLATILE (op2
));
4696 else if ((code1
== INTEGER_TYPE
|| code1
== REAL_TYPE
)
4697 && (code2
== INTEGER_TYPE
|| code2
== REAL_TYPE
))
4699 result_type
= common_type (type1
, type2
);
4701 else if (code1
== VOID_TYPE
|| code2
== VOID_TYPE
)
4703 if (pedantic
&& (code1
!= VOID_TYPE
|| code2
!= VOID_TYPE
))
4704 pedwarn ("ANSI C++ forbids conditional expr with only one void side");
4705 result_type
= void_type_node
;
4707 else if (code1
== POINTER_TYPE
&& code2
== POINTER_TYPE
)
4709 if (comp_target_types (type1
, type2
, 1))
4710 result_type
= common_type (type1
, type2
);
4711 else if (integer_zerop (op1
) && TREE_TYPE (type1
) == void_type_node
4712 && TREE_CODE (orig_op1
) != NOP_EXPR
)
4713 result_type
= qualify_type (type2
, type1
);
4714 else if (integer_zerop (op2
) && TREE_TYPE (type2
) == void_type_node
4715 && TREE_CODE (orig_op2
) != NOP_EXPR
)
4716 result_type
= qualify_type (type1
, type2
);
4717 else if (TYPE_MAIN_VARIANT (TREE_TYPE (type1
)) == void_type_node
)
4719 if (pedantic
&& TREE_CODE (type2
) == FUNCTION_TYPE
)
4720 pedwarn ("ANSI C++ forbids conditional expr between `void *' and function pointer");
4721 result_type
= qualify_type (type1
, type2
);
4723 else if (TYPE_MAIN_VARIANT (TREE_TYPE (type2
)) == void_type_node
)
4725 if (pedantic
&& TREE_CODE (type1
) == FUNCTION_TYPE
)
4726 pedwarn ("ANSI C++ forbids conditional expr between `void *' and function pointer");
4727 result_type
= qualify_type (type2
, type1
);
4730 else if (comptypes (type2
, type1
, 0))
4731 result_type
= type2
;
4732 else if (IS_AGGR_TYPE (TREE_TYPE (type1
))
4733 && IS_AGGR_TYPE (TREE_TYPE (type2
))
4734 && (result_type
= common_base_type (TREE_TYPE (type1
), TREE_TYPE (type2
))))
4736 if (result_type
== error_mark_node
)
4738 cp_error ("common base type of types `%T' and `%T' is ambiguous",
4739 TREE_TYPE (type1
), TREE_TYPE (type2
));
4740 result_type
= ptr_type_node
;
4745 && result_type
!= TREE_TYPE (type1
)
4746 && result_type
!= TREE_TYPE (type2
))
4747 cp_pedwarn ("`%T' and `%T' converted to `%T *' in conditional expression",
4748 type1
, type2
, result_type
);
4750 result_type
= TYPE_POINTER_TO (result_type
);
4755 pedwarn ("pointer type mismatch in conditional expression");
4756 result_type
= ptr_type_node
;
4759 else if (code1
== POINTER_TYPE
&& code2
== INTEGER_TYPE
)
4761 if (!integer_zerop (op2
))
4762 pedwarn ("pointer/integer type mismatch in conditional expression");
4765 op2
= null_pointer_node
;
4766 #if 0 /* Sez who? */
4767 if (pedantic
&& TREE_CODE (type1
) == FUNCTION_TYPE
)
4768 pedwarn ("ANSI C++ forbids conditional expr between 0 and function pointer");
4771 result_type
= type1
;
4773 else if (code2
== POINTER_TYPE
&& code1
== INTEGER_TYPE
)
4775 if (!integer_zerop (op1
))
4776 pedwarn ("pointer/integer type mismatch in conditional expression");
4779 op1
= null_pointer_node
;
4780 #if 0 /* Sez who? */
4781 if (pedantic
&& TREE_CODE (type2
) == FUNCTION_TYPE
)
4782 pedwarn ("ANSI C++ forbids conditional expr between 0 and function pointer");
4785 result_type
= type2
;
4790 /* The match does not look good. If either is
4791 an aggregate value, try converting to a scalar type. */
4792 if (code1
== RECORD_TYPE
&& code2
== RECORD_TYPE
)
4794 cp_error ("aggregate mismatch in conditional expression: `%T' vs `%T'", type1
, type2
);
4795 return error_mark_node
;
4797 if (code1
== RECORD_TYPE
&& TYPE_HAS_CONVERSION (type1
))
4799 tree tmp
= build_type_conversion (CONVERT_EXPR
, type2
, op1
, 0);
4800 if (tmp
== NULL_TREE
)
4802 cp_error ("aggregate type `%T' could not convert on lhs of `:'", type1
);
4803 return error_mark_node
;
4805 if (tmp
== error_mark_node
)
4806 error ("ambiguous pointer conversion");
4807 result_type
= type2
;
4810 else if (code2
== RECORD_TYPE
&& TYPE_HAS_CONVERSION (type2
))
4812 tree tmp
= build_type_conversion (CONVERT_EXPR
, type1
, op2
, 0);
4813 if (tmp
== NULL_TREE
)
4815 cp_error ("aggregate type `%T' could not convert on rhs of `:'", type2
);
4816 return error_mark_node
;
4818 if (tmp
== error_mark_node
)
4819 error ("ambiguous pointer conversion");
4820 result_type
= type1
;
4823 else if (flag_cond_mismatch
)
4824 result_type
= void_type_node
;
4827 error ("type mismatch in conditional expression");
4828 return error_mark_node
;
4832 if (TREE_CODE (result_type
) == POINTER_TYPE
4833 && TREE_CODE (TREE_TYPE (result_type
)) == METHOD_TYPE
)
4834 result_type
= build_ptrmemfunc_type (result_type
);
4836 if (result_type
!= TREE_TYPE (op1
))
4837 op1
= convert_and_check (result_type
, op1
);
4838 if (result_type
!= TREE_TYPE (op2
))
4839 op2
= convert_and_check (result_type
, op2
);
4842 /* XXX delete me, I've been here for years. */
4843 if (IS_AGGR_TYPE_CODE (code1
))
4845 result_type
= TREE_TYPE (op1
);
4846 if (TREE_CONSTANT (ifexp
))
4847 return (integer_zerop (ifexp
) ? op2
: op1
);
4849 if (TYPE_MODE (result_type
) == BLKmode
)
4851 register tree tempvar
4852 = build_decl (VAR_DECL
, NULL_TREE
, result_type
);
4853 register tree xop1
= build_modify_expr (tempvar
, NOP_EXPR
, op1
);
4854 register tree xop2
= build_modify_expr (tempvar
, NOP_EXPR
, op2
);
4855 register tree result
= fold (build (COND_EXPR
, result_type
,
4856 ifexp
, xop1
, xop2
));
4858 layout_decl (tempvar
, 0);
4859 /* No way to handle variable-sized objects here.
4860 I fear that the entire handling of BLKmode conditional exprs
4861 needs to be redone. */
4862 my_friendly_assert (TREE_CONSTANT (DECL_SIZE (tempvar
)), 315);
4864 = assign_stack_local (DECL_MODE (tempvar
),
4865 (TREE_INT_CST_LOW (DECL_SIZE (tempvar
))
4866 + BITS_PER_UNIT
- 1)
4870 TREE_SIDE_EFFECTS (result
)
4871 = TREE_SIDE_EFFECTS (ifexp
) | TREE_SIDE_EFFECTS (op1
)
4872 | TREE_SIDE_EFFECTS (op2
);
4873 return build (COMPOUND_EXPR
, result_type
, result
, tempvar
);
4878 if (TREE_CONSTANT (ifexp
))
4879 return integer_zerop (ifexp
) ? op2
: op1
;
4881 return convert_from_reference
4882 (fold (build (COND_EXPR
, result_type
, ifexp
, op1
, op2
)));
4885 /* Handle overloading of the ',' operator when needed. Otherwise,
4886 this function just builds an expression list. */
4888 build_x_compound_expr (list
)
4891 tree rest
= TREE_CHAIN (list
);
4894 if (rest
== NULL_TREE
)
4895 return build_compound_expr (list
);
4897 result
= build_opfncall (COMPOUND_EXPR
, LOOKUP_NORMAL
,
4898 TREE_VALUE (list
), TREE_VALUE (rest
), NULL_TREE
);
4900 return build_x_compound_expr (tree_cons (NULL_TREE
, result
, TREE_CHAIN (rest
)));
4902 if (! TREE_SIDE_EFFECTS (TREE_VALUE (list
)))
4904 /* the left-hand operand of a comma expression is like an expression
4905 statement: we should warn if it doesn't have any side-effects,
4906 unless it was explicitly cast to (void). */
4907 if ((extra_warnings
|| warn_unused
)
4908 && !(TREE_CODE (TREE_VALUE(list
)) == CONVERT_EXPR
4909 && TREE_TYPE (TREE_VALUE(list
)) == void_type_node
))
4910 warning("left-hand operand of comma expression has no effect");
4912 #if 0 /* this requires a gcc backend patch to export warn_if_unused_value */
4913 else if (warn_unused
)
4914 warn_if_unused_value (TREE_VALUE(list
));
4917 return build_compound_expr (tree_cons (NULL_TREE
, TREE_VALUE (list
),
4918 build_tree_list (NULL_TREE
, build_x_compound_expr (rest
))));
4921 /* Given a list of expressions, return a compound expression
4922 that performs them all and returns the value of the last of them. */
4925 build_compound_expr (list
)
4930 if (TREE_READONLY_DECL_P (TREE_VALUE (list
)))
4931 TREE_VALUE (list
) = decl_constant_value (TREE_VALUE (list
));
4933 if (TREE_CHAIN (list
) == 0)
4935 /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
4936 Strip such NOP_EXPRs, since LIST is used in non-lvalue context. */
4937 if (TREE_CODE (list
) == NOP_EXPR
4938 && TREE_TYPE (list
) == TREE_TYPE (TREE_OPERAND (list
, 0)))
4939 list
= TREE_OPERAND (list
, 0);
4941 /* Convert arrays to pointers. */
4942 if (TREE_CODE (TREE_TYPE (TREE_VALUE (list
))) == ARRAY_TYPE
)
4943 return default_conversion (TREE_VALUE (list
));
4945 return TREE_VALUE (list
);
4948 rest
= build_compound_expr (TREE_CHAIN (list
));
4950 /* When pedantic, a compound expression cannot be a constant expression. */
4951 if (! TREE_SIDE_EFFECTS (TREE_VALUE (list
)) && ! pedantic
)
4954 return build (COMPOUND_EXPR
, TREE_TYPE (rest
),
4955 break_out_cleanups (TREE_VALUE (list
)), rest
);
4965 return (TREE_CODE (t
) == INTEGER_CST
&& integer_zerop (t
));
4968 tree
build_static_cast (type
, expr
)
4971 return build_c_cast (type
, expr
, 0);
4974 tree
build_reinterpret_cast (type
, expr
)
4977 tree intype
= TREE_TYPE (expr
);
4979 if (TYPE_PTRMEMFUNC_P (type
))
4980 type
= TYPE_PTRMEMFUNC_FN_TYPE (type
);
4981 if (TYPE_PTRMEMFUNC_P (intype
))
4982 intype
= TYPE_PTRMEMFUNC_FN_TYPE (intype
);
4984 if (! POINTER_TYPE_P (type
) && ! TREE_CODE (type
) == INTEGER_TYPE
)
4986 cp_error ("reinterpret_cast cannot convert to type `%T'", type
);
4987 return error_mark_node
;
4989 if (! POINTER_TYPE_P (intype
) && ! TREE_CODE (intype
) == INTEGER_TYPE
)
4991 cp_error ("reinterpret_cast cannot convert from type `%T'", type
);
4992 return error_mark_node
;
4994 if (TREE_CODE (type
) == INTEGER_TYPE
&& TREE_CODE (intype
) != POINTER_TYPE
)
4996 cp_error ("reinterpret_cast cannot convert non-pointer type `%T' to `%T'",
4998 return error_mark_node
;
5000 if (TREE_CODE (intype
) == INTEGER_TYPE
&& TREE_CODE (type
) != POINTER_TYPE
)
5002 cp_error ("reinterpret_cast cannot convert `%T' to non-pointer type `%T'",
5004 return error_mark_node
;
5007 if (TREE_CODE (type
) == POINTER_TYPE
&& TREE_CODE (intype
) == POINTER_TYPE
)
5008 expr
= convert (ptr_type_node
, expr
);
5010 return build_c_cast (type
, expr
, 0);
5013 tree
build_const_cast (type
, expr
)
5016 tree intype
= TREE_TYPE (expr
);
5019 if (TYPE_PTRMEMFUNC_P (type
))
5020 type
= TYPE_PTRMEMFUNC_FN_TYPE (type
);
5021 if (TYPE_PTRMEMFUNC_P (intype
))
5022 intype
= TYPE_PTRMEMFUNC_FN_TYPE (intype
);
5024 if (! POINTER_TYPE_P (type
))
5026 cp_error ("const_cast cannot convert to non-pointer type `%T'", type
);
5027 return error_mark_node
;
5029 if (TREE_CODE (type
) == REFERENCE_TYPE
&& ! real_lvalue_p (expr
))
5031 cp_error ("const_cast cannot convert rvalue to type `%T'", type
);
5032 return error_mark_node
;
5034 if (TREE_CODE (type
) == POINTER_TYPE
&& TREE_CODE (intype
) != POINTER_TYPE
)
5036 cp_error ("const_cast cannot convert non-pointer type `%T' to type `%T'",
5038 return error_mark_node
;
5041 if (TREE_CODE (type
) == REFERENCE_TYPE
)
5043 t1
= TREE_TYPE (type
);
5048 t1
= TREE_TYPE (type
);
5049 t2
= TREE_TYPE (intype
);
5051 for (; TREE_CODE (t1
) == POINTER_TYPE
&& TREE_CODE (t2
) == POINTER_TYPE
;
5052 t1
= TREE_TYPE (t1
), t2
= TREE_TYPE (t2
))
5056 if (TREE_CODE (t1
) == OFFSET_TYPE
&& TREE_CODE (t2
) == OFFSET_TYPE
)
5058 if (TYPE_OFFSET_BASETYPE (t1
) != TYPE_OFFSET_BASETYPE (t2
))
5060 cp_error ("const_cast cannot convert between pointers to members of different types `%T' and `%T'",
5061 TYPE_OFFSET_BASETYPE (t2
), TYPE_OFFSET_BASETYPE (t1
));
5062 return error_mark_node
;
5064 t1
= TREE_TYPE (t1
);
5065 t2
= TREE_TYPE (t2
);
5068 if (TYPE_MAIN_VARIANT (t1
) != TYPE_MAIN_VARIANT (t2
))
5070 cp_error ("const_cast cannot convert unrelated type `%T' to `%T'",
5072 return error_mark_node
;
5075 return build_c_cast (type
, expr
, 0);
5078 /* Build an expression representing a cast to type TYPE of expression EXPR.
5080 ALLOW_NONCONVERTING is true if we should allow non-converting constructors
5081 when doing the cast. */
5084 build_c_cast (type
, expr
, allow_nonconverting
)
5087 int allow_nonconverting
;
5089 register tree value
= expr
;
5091 if (type
== error_mark_node
|| expr
== error_mark_node
)
5092 return error_mark_node
;
5094 /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
5095 Strip such NOP_EXPRs if VALUE is being used in non-lvalue context. */
5096 if (TREE_CODE (type
) != REFERENCE_TYPE
5097 && TREE_CODE (value
) == NOP_EXPR
5098 && TREE_TYPE (value
) == TREE_TYPE (TREE_OPERAND (value
, 0)))
5099 value
= TREE_OPERAND (value
, 0);
5101 if (TREE_TYPE (expr
)
5102 && TREE_CODE (TREE_TYPE (expr
)) == OFFSET_TYPE
5103 && TREE_CODE (type
) != OFFSET_TYPE
)
5104 value
= resolve_offset_ref (value
);
5106 if (TREE_CODE (type
) == ARRAY_TYPE
)
5108 /* Allow casting from T1* to T2[] because Cfront allows it.
5109 NIHCL uses it. It is not valid ANSI C however, and hence, not
5111 if (TREE_CODE (TREE_TYPE (expr
)) == POINTER_TYPE
)
5114 pedwarn ("ANSI C++ forbids casting to an array type");
5115 type
= build_pointer_type (TREE_TYPE (type
));
5119 error ("ANSI C++ forbids casting to an array type");
5120 return error_mark_node
;
5124 if (TREE_CODE (type
) == FUNCTION_TYPE
5125 || TREE_CODE (type
) == METHOD_TYPE
)
5127 cp_error ("casting to function type `%T'", type
);
5128 return error_mark_node
;
5131 if (IS_SIGNATURE (type
))
5133 error ("cast specifies signature type");
5134 return error_mark_node
;
5137 /* If there's only one function in the overloaded space,
5139 if (TREE_CODE (value
) == TREE_LIST
5140 && TREE_CHAIN (value
) == NULL_TREE
)
5141 value
= TREE_VALUE (value
);
5143 if (TREE_CODE (type
) == VOID_TYPE
)
5144 value
= build1 (CONVERT_EXPR
, type
, value
);
5145 else if (TREE_TYPE (value
) == NULL_TREE
5146 || type_unknown_p (value
))
5148 value
= instantiate_type (type
, value
, 1);
5150 if (value
== error_mark_node
)
5151 return error_mark_node
;
5158 /* Convert functions and arrays to pointers and
5159 convert references to their expanded types,
5160 but don't convert any other types. */
5161 if (TREE_CODE (TREE_TYPE (value
)) == FUNCTION_TYPE
5162 || TREE_CODE (TREE_TYPE (value
)) == METHOD_TYPE
5163 || TREE_CODE (TREE_TYPE (value
)) == ARRAY_TYPE
5164 || TREE_CODE (TREE_TYPE (value
)) == REFERENCE_TYPE
)
5165 value
= default_conversion (value
);
5166 otype
= TREE_TYPE (value
);
5168 /* Optionally warn about potentially worrisome casts. */
5171 && TREE_CODE (type
) == POINTER_TYPE
5172 && TREE_CODE (otype
) == POINTER_TYPE
)
5174 /* For C++ we make these regular warnings, rather than
5175 softening them into pedwarns. */
5176 if (TYPE_VOLATILE (TREE_TYPE (otype
))
5177 && ! TYPE_VOLATILE (TREE_TYPE (type
)))
5178 warning ("cast discards `volatile' from pointer target type");
5179 if (TYPE_READONLY (TREE_TYPE (otype
))
5180 && ! TYPE_READONLY (TREE_TYPE (type
)))
5181 warning ("cast discards `const' from pointer target type");
5184 /* Warn about possible alignment problems. */
5185 if (STRICT_ALIGNMENT
&& warn_cast_align
5186 && TREE_CODE (type
) == POINTER_TYPE
5187 && TREE_CODE (otype
) == POINTER_TYPE
5188 && TREE_CODE (TREE_TYPE (otype
)) != VOID_TYPE
5189 && TREE_CODE (TREE_TYPE (otype
)) != FUNCTION_TYPE
5190 && TYPE_ALIGN (TREE_TYPE (type
)) > TYPE_ALIGN (TREE_TYPE (otype
)))
5191 warning ("cast increases required alignment of target type");
5194 if (TREE_CODE (type
) == INTEGER_TYPE
5195 && TREE_CODE (otype
) == POINTER_TYPE
5196 && TYPE_PRECISION (type
) != TYPE_PRECISION (otype
))
5197 warning ("cast from pointer to integer of different size");
5199 if (TREE_CODE (type
) == POINTER_TYPE
5200 && TREE_CODE (otype
) == INTEGER_TYPE
5201 && TYPE_PRECISION (type
) != TYPE_PRECISION (otype
)
5202 /* Don't warn about converting 0 to pointer,
5203 provided the 0 was explicit--not cast or made by folding. */
5204 && !(TREE_CODE (value
) == INTEGER_CST
&& integer_zerop (value
)))
5205 warning ("cast to pointer from integer of different size");
5208 flag
= allow_nonconverting
? CONV_NONCONVERTING
: 0;
5210 if (TREE_CODE (type
) == REFERENCE_TYPE
)
5211 value
= (convert_from_reference
5212 (convert_to_reference (type
, value
, CONV_OLD_CONVERT
|flag
,
5213 LOOKUP_COMPLAIN
, NULL_TREE
)));
5218 if (TREE_READONLY_DECL_P (value
))
5219 value
= decl_constant_value (value
);
5222 value
= convert_force (type
, value
, flag
);
5224 /* Ignore any integer overflow caused by the cast. */
5225 if (TREE_CODE (value
) == INTEGER_CST
)
5227 TREE_OVERFLOW (value
) = TREE_OVERFLOW (ovalue
);
5228 TREE_CONSTANT_OVERFLOW (value
) = TREE_CONSTANT_OVERFLOW (ovalue
);
5233 /* Always produce some operator for an explicit cast,
5234 so we can tell (for -pedantic) that the cast is no lvalue.
5235 Also, pedantically, don't let (void *) (FOO *) 0 be a null
5236 pointer constant. */
5237 if (TREE_CODE (type
) != REFERENCE_TYPE
5240 && TREE_CODE (value
) == INTEGER_CST
5241 && TREE_CODE (expr
) == INTEGER_CST
5242 && TREE_CODE (TREE_TYPE (expr
)) != INTEGER_TYPE
)))
5243 value
= non_lvalue (value
);
5249 /* Build an assignment expression of lvalue LHS from value RHS.
5251 In C++, if the left hand side of the assignment is a REFERENCE_TYPE,
5252 that reference becomes deferenced down to it base type. */
5254 /* Return a reference to the BASE_INDEX part of EXPR. TYPE is
5255 the type to which BASE_INDEX applies. */
5257 get_base_ref (type
, base_index
, expr
)
5262 tree binfos
= TYPE_BINFO_BASETYPES (type
);
5263 tree base_binfo
= TREE_VEC_ELT (binfos
, base_index
);
5266 if (TREE_CODE (expr
) == ARRAY_REF
5267 || ! BINFO_OFFSET_ZEROP (base_binfo
)
5268 || TREE_VIA_VIRTUAL (base_binfo
)
5269 || TYPE_MODE (type
) != TYPE_MODE (BINFO_TYPE (base_binfo
)))
5271 tree addr
= build_unary_op (ADDR_EXPR
, expr
, 0);
5272 ref
= build_indirect_ref (convert_pointer_to (base_binfo
, addr
),
5277 ref
= copy_node (expr
);
5278 TREE_TYPE (ref
) = BINFO_TYPE (base_binfo
);
5283 /* Build an assignment expression of lvalue LHS from value RHS.
5284 MODIFYCODE is the code for a binary operator that we use
5285 to combine the old value of LHS with RHS to get the new value.
5286 Or else MODIFYCODE is NOP_EXPR meaning do a simple assignment.
5288 C++: If MODIFYCODE is INIT_EXPR, then leave references unbashed.
5290 `build_modify_expr_1' implements recursive part of memberwise
5291 assignment operation. */
5293 build_modify_expr_1 (lhs
, modifycode
, rhs
, basetype_path
)
5295 enum tree_code modifycode
;
5298 register tree result
;
5300 tree lhstype
= TREE_TYPE (lhs
);
5301 tree olhstype
= lhstype
;
5303 /* Avoid duplicate error messages from operands that had errors. */
5304 if (TREE_CODE (lhs
) == ERROR_MARK
|| TREE_CODE (rhs
) == ERROR_MARK
)
5305 return error_mark_node
;
5307 /* If a binary op has been requested, combine the old LHS value with the RHS
5308 producing the value we should actually store into the LHS. */
5310 if (modifycode
== INIT_EXPR
)
5312 else if (modifycode
== NOP_EXPR
)
5314 /* must deal with overloading of `operator=' here. */
5315 if (TREE_CODE (lhstype
) == REFERENCE_TYPE
)
5316 lhstype
= TREE_TYPE (lhstype
);
5322 lhs
= stabilize_reference (lhs
);
5323 newrhs
= build_binary_op (modifycode
, lhs
, rhs
, 1);
5324 modifycode
= NOP_EXPR
;
5327 /* If storing into a structure or union member,
5328 it has probably been given type `int'.
5329 Compute the type that would go with
5330 the actual amount of storage the member occupies. */
5332 if (TREE_CODE (lhs
) == COMPONENT_REF
5333 && (TREE_CODE (lhstype
) == INTEGER_TYPE
5334 || TREE_CODE (lhstype
) == REAL_TYPE
5335 || TREE_CODE (lhstype
) == ENUMERAL_TYPE
))
5336 lhstype
= TREE_TYPE (get_unwidened (lhs
, 0));
5338 /* C++: The semantics of C++ differ from those of C when an
5339 assignment of an aggregate is desired. Assignment in C++ is
5340 now defined as memberwise assignment of non-static members
5341 and base class objects. This rule applies recursively
5342 until a member of a built-in type is found.
5344 Also, we cannot do a bit-wise copy of aggregates which
5345 contain virtual function table pointers. Those
5346 pointer values must be preserved through the copy.
5347 However, this is handled in expand_expr, and not here.
5348 This is because much better code can be generated at
5349 that stage than this one. */
5350 if (TREE_CODE (lhstype
) == RECORD_TYPE
5351 && TYPE_LANG_SPECIFIC (lhstype
)
5352 && TYPE_MAIN_VARIANT (lhstype
) == TYPE_MAIN_VARIANT (TREE_TYPE (newrhs
)))
5357 /* Perform operation on object. */
5358 if (modifycode
== INIT_EXPR
&& TYPE_HAS_INIT_REF (lhstype
))
5360 result
= build_method_call (lhs
, constructor_name_full (lhstype
),
5361 build_tree_list (NULL_TREE
, rhs
),
5362 basetype_path
, LOOKUP_NORMAL
);
5363 return build_indirect_ref (result
, NULL_PTR
);
5365 else if (modifycode
== NOP_EXPR
)
5367 /* `operator=' is not an inheritable operator; see 13.4.3. */
5368 if (TYPE_LANG_SPECIFIC (lhstype
) && TYPE_HAS_ASSIGNMENT (lhstype
))
5370 result
= build_opfncall (MODIFY_EXPR
, LOOKUP_NORMAL
,
5371 lhs
, rhs
, make_node (NOP_EXPR
));
5372 if (result
== NULL_TREE
)
5373 return error_mark_node
;
5378 if (TYPE_USES_VIRTUAL_BASECLASSES (lhstype
)
5379 || (modifycode
== NOP_EXPR
&& TYPE_GETS_ASSIGNMENT (lhstype
))
5380 || (modifycode
== INIT_EXPR
&& TYPE_GETS_INIT_REF (lhstype
)))
5382 tree binfos
= BINFO_BASETYPES (TYPE_BINFO (lhstype
));
5385 if (binfos
!= NULL_TREE
)
5386 /* Perform operation on each member, depth-first, left-right. */
5387 for (i
= 0; i
<= TREE_VEC_LENGTH (binfos
)-1; i
++)
5389 tree base_binfo
= TREE_VEC_ELT (binfos
, i
);
5390 tree base_lhs
, base_rhs
;
5393 /* Assignments from virtual baseclasses handled elsewhere. */
5394 if (TREE_VIA_VIRTUAL (base_binfo
))
5397 base_lhs
= get_base_ref (lhstype
, i
, lhs
);
5398 base_rhs
= get_base_ref (lhstype
, i
, newrhs
);
5400 BINFO_INHERITANCE_CHAIN (base_binfo
) = basetype_path
;
5402 = build_modify_expr_1 (base_lhs
, modifycode
, base_rhs
,
5405 /* We either get back a compound stmt, or a simple one. */
5406 if (new_result
&& TREE_CODE (new_result
) == TREE_LIST
)
5407 new_result
= build_compound_expr (new_result
);
5408 result
= tree_cons (NULL_TREE
, new_result
, result
);
5411 for (elt
= TYPE_FIELDS (lhstype
); elt
; elt
= TREE_CHAIN (elt
))
5413 tree vbases
= NULL_TREE
;
5414 tree elt_lhs
, elt_rhs
;
5416 if (TREE_CODE (elt
) != FIELD_DECL
)
5419 && (VFIELD_NAME_P (DECL_NAME (elt
))
5420 || VBASE_NAME_P (DECL_NAME (elt
))))
5423 if (TREE_READONLY (elt
)
5424 || TREE_CODE (TREE_TYPE (elt
)) == REFERENCE_TYPE
)
5426 cp_error ("cannot generate default `%T::operator ='",
5428 if (TREE_CODE (TREE_TYPE (elt
)) == REFERENCE_TYPE
)
5429 cp_error_at ("because member `%#D' is a reference", elt
);
5431 cp_error_at ("because member `%#D' is const", elt
);
5433 return error_mark_node
;
5436 if (IS_AGGR_TYPE (TREE_TYPE (elt
))
5437 && TYPE_LANG_SPECIFIC (TREE_TYPE (elt
)))
5438 vbases
= CLASSTYPE_VBASECLASSES (TREE_TYPE (elt
));
5440 elt_lhs
= build (COMPONENT_REF
, TREE_TYPE (elt
), lhs
, elt
);
5441 elt_rhs
= build (COMPONENT_REF
, TREE_TYPE (elt
), newrhs
, elt
);
5442 /* It is not always safe to go through `build_modify_expr_1'
5443 when performing element-wise copying. This is because
5444 an element may be of ARRAY_TYPE, which will not
5445 be properly copied as a naked element. */
5446 if (TREE_CODE (TREE_TYPE (elt
)) == RECORD_TYPE
5447 && TYPE_LANG_SPECIFIC (TREE_TYPE (elt
)))
5448 basetype_path
= TYPE_BINFO (TREE_TYPE (elt
));
5452 tree elt_lhs_addr
= build_unary_op (ADDR_EXPR
, elt_lhs
, 0);
5453 tree elt_rhs_addr
= build_unary_op (ADDR_EXPR
, elt_rhs
, 0);
5455 elt_lhs_addr
= convert_pointer_to (vbases
, elt_lhs_addr
);
5456 elt_rhs_addr
= convert_pointer_to (vbases
, elt_rhs_addr
);
5458 = tree_cons (NULL_TREE
,
5460 (build_indirect_ref (elt_lhs_addr
, NULL_PTR
),
5462 build_indirect_ref (elt_rhs_addr
, NULL_PTR
),
5465 if (TREE_VALUE (result
) == error_mark_node
)
5466 return error_mark_node
;
5467 vbases
= TREE_CHAIN (vbases
);
5469 elt_lhs
= build_modify_expr_1 (elt_lhs
, modifycode
, elt_rhs
,
5471 result
= tree_cons (NULL_TREE
, elt_lhs
, result
);
5475 return build_compound_expr (result
);
5476 /* No fields to move. */
5477 return integer_zero_node
;
5481 result
= build (modifycode
== NOP_EXPR
? MODIFY_EXPR
: INIT_EXPR
,
5482 void_type_node
, lhs
, rhs
);
5483 TREE_SIDE_EFFECTS (result
) = 1;
5488 result
= build_modify_expr (lhs
, modifycode
, newrhs
);
5489 /* ARRAY_TYPEs cannot be converted to anything meaningful,
5490 and leaving it there screws up `build_compound_expr' when
5491 it tries to defaultly convert everything. */
5492 if (TREE_CODE (TREE_TYPE (result
)) == ARRAY_TYPE
)
5493 TREE_TYPE (result
) = void_type_node
;
5498 /* Taken from expr.c:
5499 Subroutine of expand_expr:
5500 record the non-copied parts (LIST) of an expr (LHS), and return a list
5501 which specifies the initial values of these parts. */
5504 init_noncopied_parts (lhs
, list
)
5511 for (tail
= list
; tail
; tail
= TREE_CHAIN (tail
))
5512 if (TREE_CODE (TREE_VALUE (tail
)) == TREE_LIST
)
5513 parts
= chainon (parts
, init_noncopied_parts (lhs
, TREE_VALUE (tail
)));
5516 tree part
= TREE_VALUE (tail
);
5517 tree part_type
= TREE_TYPE (part
);
5518 tree to_be_initialized
= build (COMPONENT_REF
, part_type
, lhs
, part
);
5519 parts
= tree_cons (TREE_PURPOSE (tail
), to_be_initialized
, parts
);
5525 expand_target_expr (t
)
5528 tree xval
= make_node (RTL_EXPR
);
5531 do_pending_stack_adjust ();
5532 start_sequence_for_rtl_expr (xval
);
5534 rtxval
= expand_expr (t
, NULL
, VOIDmode
, 0);
5535 do_pending_stack_adjust ();
5536 TREE_SIDE_EFFECTS (xval
) = 1;
5537 RTL_EXPR_SEQUENCE (xval
) = get_insns ();
5539 RTL_EXPR_RTL (xval
) = rtxval
;
5540 TREE_TYPE (xval
) = TREE_TYPE (t
);
5544 /* Build an assignment expression of lvalue LHS from value RHS.
5545 MODIFYCODE is the code for a binary operator that we use
5546 to combine the old value of LHS with RHS to get the new value.
5547 Or else MODIFYCODE is NOP_EXPR meaning do a simple assignment.
5549 C++: If MODIFYCODE is INIT_EXPR, then leave references unbashed.
5552 build_modify_expr (lhs
, modifycode
, rhs
)
5554 enum tree_code modifycode
;
5557 register tree result
;
5559 tree lhstype
= TREE_TYPE (lhs
);
5560 tree olhstype
= lhstype
;
5563 /* Avoid duplicate error messages from operands that had errors. */
5564 if (TREE_CODE (lhs
) == ERROR_MARK
|| TREE_CODE (rhs
) == ERROR_MARK
)
5565 return error_mark_node
;
5567 /* Types that aren't fully specified cannot be used in assignments. */
5568 lhs
= require_complete_type (lhs
);
5570 /* Decide early if we are going to protect RHS from GC
5571 before assigning it to LHS. */
5572 if (type_needs_gc_entry (TREE_TYPE (rhs
))
5573 && ! value_safe_from_gc (lhs
, rhs
))
5574 rhs
= protect_value_from_gc (lhs
, rhs
);
5578 /* Handle assignment to signature pointers/refs. */
5580 if (TYPE_LANG_SPECIFIC (lhstype
) &&
5581 (IS_SIGNATURE_POINTER (lhstype
) || IS_SIGNATURE_REFERENCE (lhstype
)))
5583 return build_signature_pointer_constructor (lhs
, rhs
);
5586 /* Handle control structure constructs used as "lvalues". */
5588 switch (TREE_CODE (lhs
))
5590 /* Handle --foo = 5; as these are valid constructs in C++ */
5591 case PREDECREMENT_EXPR
:
5592 case PREINCREMENT_EXPR
:
5593 if (TREE_SIDE_EFFECTS (TREE_OPERAND (lhs
, 0)))
5594 lhs
= build (TREE_CODE (lhs
), TREE_TYPE (lhs
),
5595 stabilize_reference (TREE_OPERAND (lhs
, 0)));
5596 return build (COMPOUND_EXPR
, lhstype
,
5598 build_modify_expr (TREE_OPERAND (lhs
, 0),
5601 /* Handle (a, b) used as an "lvalue". */
5603 newrhs
= build_modify_expr (TREE_OPERAND (lhs
, 1),
5605 if (TREE_CODE (newrhs
) == ERROR_MARK
)
5606 return error_mark_node
;
5607 return build (COMPOUND_EXPR
, lhstype
,
5608 TREE_OPERAND (lhs
, 0), newrhs
);
5611 newrhs
= build_modify_expr (TREE_OPERAND (lhs
, 0), modifycode
, rhs
);
5612 if (TREE_CODE (newrhs
) == ERROR_MARK
)
5613 return error_mark_node
;
5614 return build (COMPOUND_EXPR
, lhstype
, lhs
, newrhs
);
5616 /* Handle (a ? b : c) used as an "lvalue". */
5618 rhs
= save_expr (rhs
);
5620 /* Produce (a ? (b = rhs) : (c = rhs))
5621 except that the RHS goes through a save-expr
5622 so the code to compute it is only emitted once. */
5624 = build_conditional_expr (TREE_OPERAND (lhs
, 0),
5625 build_modify_expr (convert (TREE_TYPE (lhs
), TREE_OPERAND (lhs
, 1)),
5627 build_modify_expr (convert (TREE_TYPE (lhs
), TREE_OPERAND (lhs
, 2)),
5629 if (TREE_CODE (cond
) == ERROR_MARK
)
5631 /* Make sure the code to compute the rhs comes out
5632 before the split. */
5633 return build (COMPOUND_EXPR
, TREE_TYPE (lhs
),
5634 /* Case to void to suppress warning
5635 from warn_if_unused_value. */
5636 convert (void_type_node
, rhs
), cond
);
5640 if (TREE_CODE (lhs
) == OFFSET_REF
)
5642 if (TREE_OPERAND (lhs
, 0) == NULL_TREE
)
5644 /* Static class member? */
5645 tree member
= TREE_OPERAND (lhs
, 1);
5646 if (TREE_CODE (member
) == VAR_DECL
)
5650 compiler_error ("invalid static class member");
5651 return error_mark_node
;
5655 lhs
= resolve_offset_ref (lhs
);
5657 olhstype
= lhstype
= TREE_TYPE (lhs
);
5660 if (TREE_CODE (lhstype
) == REFERENCE_TYPE
5661 && modifycode
!= INIT_EXPR
)
5663 lhs
= convert_from_reference (lhs
);
5664 olhstype
= lhstype
= TREE_TYPE (lhs
);
5667 /* If a binary op has been requested, combine the old LHS value with the RHS
5668 producing the value we should actually store into the LHS. */
5670 if (modifycode
== INIT_EXPR
)
5672 if (! IS_AGGR_TYPE (lhstype
))
5673 /* Do the default thing */;
5674 else if (! TYPE_HAS_CONSTRUCTOR (lhstype
))
5676 cp_error ("`%T' has no constructors", lhstype
);
5677 return error_mark_node
;
5679 else if (TYPE_HAS_TRIVIAL_INIT_REF (lhstype
)
5680 && TYPE_MAIN_VARIANT (lhstype
) == TYPE_MAIN_VARIANT (TREE_TYPE (newrhs
)))
5681 /* Do the default thing */;
5684 result
= build_method_call (lhs
, constructor_name_full (lhstype
),
5685 build_tree_list (NULL_TREE
, rhs
),
5686 NULL_TREE
, LOOKUP_NORMAL
);
5687 if (result
== NULL_TREE
)
5688 return error_mark_node
;
5692 else if (modifycode
== NOP_EXPR
)
5695 /* `operator=' is not an inheritable operator. */
5696 if (! IS_AGGR_TYPE (lhstype
))
5697 /* Do the default thing */;
5698 else if (! TYPE_HAS_ASSIGNMENT (lhstype
))
5700 cp_error ("`%T' does not define operator=", lhstype
);
5701 return error_mark_node
;
5703 else if (TYPE_HAS_TRIVIAL_ASSIGN_REF (lhstype
)
5704 && TYPE_MAIN_VARIANT (lhstype
) == TYPE_MAIN_VARIANT (TREE_TYPE (newrhs
)))
5707 /* If we care about this, do overload resolution. */
5708 build_opfncall (MODIFY_EXPR
, LOOKUP_NORMAL
,
5709 lhs
, rhs
, make_node (NOP_EXPR
));
5711 /* Do the default thing */;
5715 result
= build_opfncall (MODIFY_EXPR
, LOOKUP_NORMAL
,
5716 lhs
, rhs
, make_node (NOP_EXPR
));
5717 if (result
== NULL_TREE
)
5718 return error_mark_node
;
5722 /* Treat `operator=' as an inheritable operator. */
5723 if (TYPE_LANG_SPECIFIC (lhstype
) && TYPE_GETS_ASSIGNMENT (lhstype
))
5725 tree orig_lhstype
= lhstype
;
5726 while (! TYPE_HAS_ASSIGNMENT (lhstype
))
5728 int i
, n_baseclasses
= CLASSTYPE_N_BASECLASSES (lhstype
);
5729 tree basetype
= NULL_TREE
;
5730 for (i
= 0; i
< n_baseclasses
; i
++)
5731 if (TYPE_GETS_ASSIGNMENT (TYPE_BINFO_BASETYPE (lhstype
, i
)))
5733 if (basetype
!= NULL_TREE
)
5735 message_2_types (error
, "base classes `%s' and `%s' both have operator ='",
5737 TYPE_BINFO_BASETYPE (lhstype
, i
));
5738 return error_mark_node
;
5740 basetype
= TYPE_BINFO_BASETYPE (lhstype
, i
);
5744 if (orig_lhstype
!= lhstype
)
5746 lhs
= build_indirect_ref (convert_pointer_to (lhstype
,
5747 build_unary_op (ADDR_EXPR
, lhs
, 0)), NULL_PTR
);
5748 if (lhs
== error_mark_node
)
5750 cp_error ("conversion to private basetype `%T'", lhstype
);
5751 return error_mark_node
;
5754 result
= build_opfncall (MODIFY_EXPR
, LOOKUP_NORMAL
,
5755 lhs
, rhs
, make_node (NOP_EXPR
));
5756 if (result
== NULL_TREE
)
5757 return error_mark_node
;
5763 else if (PROMOTES_TO_AGGR_TYPE (lhstype
, REFERENCE_TYPE
))
5765 /* This case must convert to some sort of lvalue that
5766 can participate in an op= operation. */
5769 if (build_default_binary_type_conversion (modifycode
, &lhs_tmp
, &rhs_tmp
))
5771 lhs
= stabilize_reference (lhs_tmp
);
5772 /* Forget is was ever anything else. */
5773 olhstype
= lhstype
= TREE_TYPE (lhs
);
5774 newrhs
= build_binary_op (modifycode
, lhs
, rhs_tmp
, 1);
5778 cp_error ("no match for `%O(%#T, %#T)'", modifycode
,
5779 TREE_TYPE (lhs
), TREE_TYPE (rhs
));
5780 return error_mark_node
;
5785 lhs
= stabilize_reference (lhs
);
5786 newrhs
= build_binary_op (modifycode
, lhs
, rhs
, 1);
5789 /* Handle a cast used as an "lvalue".
5790 We have already performed any binary operator using the value as cast.
5791 Now convert the result to the cast type of the lhs,
5792 and then true type of the lhs and store it there;
5793 then convert result back to the cast type to be the value
5794 of the assignment. */
5796 switch (TREE_CODE (lhs
))
5801 case FIX_TRUNC_EXPR
:
5802 case FIX_FLOOR_EXPR
:
5803 case FIX_ROUND_EXPR
:
5805 if (TREE_CODE (TREE_TYPE (newrhs
)) == ARRAY_TYPE
5806 || TREE_CODE (TREE_TYPE (newrhs
)) == FUNCTION_TYPE
5807 || TREE_CODE (TREE_TYPE (newrhs
)) == METHOD_TYPE
5808 || TREE_CODE (TREE_TYPE (newrhs
)) == OFFSET_TYPE
)
5809 newrhs
= default_conversion (newrhs
);
5811 tree inner_lhs
= TREE_OPERAND (lhs
, 0);
5813 if (! lvalue_p (lhs
) && pedantic
)
5814 pedwarn ("cast to non-reference type used as lvalue");
5816 result
= build_modify_expr (inner_lhs
, NOP_EXPR
,
5817 convert (TREE_TYPE (inner_lhs
),
5818 convert (lhstype
, newrhs
)));
5819 if (TREE_CODE (result
) == ERROR_MARK
)
5821 return convert (TREE_TYPE (lhs
), result
);
5825 /* Now we have handled acceptable kinds of LHS that are not truly lvalues.
5826 Reject anything strange now. */
5828 if (!lvalue_or_else (lhs
, "assignment"))
5829 return error_mark_node
;
5831 GNU_xref_assign (lhs
);
5833 /* Warn about storing in something that is `const'. */
5834 /* For C++, don't warn if this is initialization. */
5835 if (modifycode
!= INIT_EXPR
5836 /* For assignment to `const' signature pointer/reference fields,
5837 don't warn either, we already printed a better message before. */
5838 && ! (TREE_CODE (lhs
) == COMPONENT_REF
5839 && (IS_SIGNATURE_POINTER (TREE_TYPE (TREE_OPERAND (lhs
, 0)))
5840 || IS_SIGNATURE_REFERENCE (TREE_TYPE (TREE_OPERAND (lhs
, 0)))))
5841 && (TREE_READONLY (lhs
) || TYPE_READONLY (lhstype
)
5842 || ((TREE_CODE (lhstype
) == RECORD_TYPE
5843 || TREE_CODE (lhstype
) == UNION_TYPE
)
5844 && C_TYPE_FIELDS_READONLY (lhstype
))
5845 || (TREE_CODE (lhstype
) == REFERENCE_TYPE
5846 && TYPE_READONLY (TREE_TYPE (lhstype
)))))
5847 readonly_error (lhs
, "assignment", 0);
5849 /* If storing into a structure or union member,
5850 it has probably been given type `int'.
5851 Compute the type that would go with
5852 the actual amount of storage the member occupies. */
5854 if (TREE_CODE (lhs
) == COMPONENT_REF
5855 && (TREE_CODE (lhstype
) == INTEGER_TYPE
5856 || TREE_CODE (lhstype
) == REAL_TYPE
5857 || TREE_CODE (lhstype
) == ENUMERAL_TYPE
))
5859 lhstype
= TREE_TYPE (get_unwidened (lhs
, 0));
5861 /* If storing in a field that is in actuality a short or narrower
5862 than one, we must store in the field in its actual type. */
5864 if (lhstype
!= TREE_TYPE (lhs
))
5866 lhs
= copy_node (lhs
);
5867 TREE_TYPE (lhs
) = lhstype
;
5871 /* check to see if there is an assignment to `this' */
5872 if (lhs
== current_class_decl
)
5874 if (flag_this_is_variable
> 0
5875 && DECL_NAME (current_function_decl
) != NULL_TREE
5876 && (DECL_NAME (current_function_decl
)
5877 != constructor_name (current_class_type
)))
5878 warning ("assignment to `this' not in constructor or destructor");
5879 current_function_just_assigned_this
= 1;
5882 /* The TREE_TYPE of RHS may be TYPE_UNKNOWN. This can happen
5883 when the type of RHS is not yet known, i.e. its type
5884 is inherited from LHS. */
5885 rhs
= require_instantiated_type (lhstype
, newrhs
, error_mark_node
);
5886 if (rhs
== error_mark_node
)
5887 return error_mark_node
;
5890 if (modifycode
!= INIT_EXPR
)
5892 /* Make modifycode now either a NOP_EXPR or an INIT_EXPR. */
5893 modifycode
= NOP_EXPR
;
5894 /* Reference-bashing */
5895 if (TREE_CODE (lhstype
) == REFERENCE_TYPE
)
5897 tree tmp
= convert_from_reference (lhs
);
5898 lhstype
= TREE_TYPE (tmp
);
5899 if (TYPE_SIZE (lhstype
) == 0)
5901 incomplete_type_error (lhs
, lhstype
);
5902 return error_mark_node
;
5907 if (TREE_CODE (TREE_TYPE (newrhs
)) == REFERENCE_TYPE
)
5909 tree tmp
= convert_from_reference (newrhs
);
5910 if (TYPE_SIZE (TREE_TYPE (tmp
)) == 0)
5912 incomplete_type_error (newrhs
, TREE_TYPE (tmp
));
5913 return error_mark_node
;
5919 if (TREE_SIDE_EFFECTS (lhs
))
5920 lhs
= stabilize_reference (lhs
);
5921 if (TREE_SIDE_EFFECTS (newrhs
))
5922 newrhs
= stabilize_reference (newrhs
);
5925 /* This is now done by generating X(X&) and operator=(X&). */
5926 /* C++: The semantics of C++ differ from those of C when an
5927 assignment of an aggregate is desired. Assignment in C++ is
5928 now defined as memberwise assignment of non-static members
5929 and base class objects. This rule applies recursively
5930 until a member of a built-in type is found.
5932 Also, we cannot do a bit-wise copy of aggregates which
5933 contain virtual function table pointers. Those
5934 pointer values must be preserved through the copy.
5935 However, this is handled in expand_expr, and not here.
5936 This is because much better code can be generated at
5937 that stage than this one. */
5938 if (TREE_CODE (lhstype
) == RECORD_TYPE
5939 && ! TYPE_PTRMEMFUNC_P (lhstype
)
5940 && (TYPE_MAIN_VARIANT (lhstype
) == TYPE_MAIN_VARIANT (TREE_TYPE (newrhs
))
5941 || (TREE_CODE (TREE_TYPE (newrhs
)) == RECORD_TYPE
5942 && UNIQUELY_DERIVED_FROM_P (lhstype
, TREE_TYPE (newrhs
)))))
5944 tree vbases
= CLASSTYPE_VBASECLASSES (lhstype
);
5945 tree lhs_addr
= build_unary_op (ADDR_EXPR
, lhs
, 0);
5948 /* Memberwise assignment would cause NEWRHS to be
5949 evaluated for every member that gets assigned.
5950 By wrapping side-effecting exprs in a SAVE_EXPR,
5951 NEWRHS will only be evaluated once. */
5952 if (IS_AGGR_TYPE (TREE_TYPE (newrhs
))
5953 && TREE_SIDE_EFFECTS (newrhs
)
5954 /* This are things we don't have to save. */
5955 && TREE_CODE (newrhs
) != COND_EXPR
5956 && TREE_CODE (newrhs
) != TARGET_EXPR
5957 && TREE_CODE (newrhs
) != WITH_CLEANUP_EXPR
)
5958 /* Call `break_out_cleanups' on NEWRHS in case there are cleanups.
5959 If NEWRHS is a CALL_EXPR that needs a cleanup, failure to do so
5960 will result in expand_expr expanding the call without knowing
5961 that it should run the cleanup. */
5962 newrhs
= save_expr (break_out_cleanups (newrhs
));
5964 if (TREE_CODE (newrhs
) == COND_EXPR
)
5965 rhs_addr
= rationalize_conditional_expr (ADDR_EXPR
, newrhs
);
5967 rhs_addr
= build_unary_op (ADDR_EXPR
, newrhs
, 0);
5969 result
= tree_cons (NULL_TREE
,
5970 convert (build_reference_type (lhstype
), lhs
),
5973 if (! comptypes (TREE_TYPE (lhs_addr
), TREE_TYPE (rhs_addr
), 1))
5974 rhs_addr
= convert_pointer_to (TREE_TYPE (TREE_TYPE (lhs_addr
)), rhs_addr
);
5976 tree noncopied_parts
= NULL_TREE
;
5978 if (TYPE_NONCOPIED_PARTS (lhstype
) != 0)
5979 noncopied_parts
= init_noncopied_parts (lhs
,
5980 TYPE_NONCOPIED_PARTS (lhstype
));
5981 while (noncopied_parts
!= 0)
5983 result
= tree_cons (NULL_TREE
,
5984 build_modify_expr (convert (ptr_type_node
, TREE_VALUE (noncopied_parts
)),
5986 TREE_PURPOSE (noncopied_parts
)),
5988 noncopied_parts
= TREE_CHAIN (noncopied_parts
);
5991 /* Once we have our hands on an address, we must change NEWRHS
5992 to work from there. Otherwise we can get multiple evaluations
5994 if (TREE_CODE (newrhs
) != SAVE_EXPR
)
5995 newrhs
= build_indirect_ref (rhs_addr
, NULL_PTR
);
5999 tree elt_lhs
= convert_pointer_to (vbases
, lhs_addr
);
6000 tree elt_rhs
= convert_pointer_to (vbases
, rhs_addr
);
6002 = tree_cons (NULL_TREE
,
6003 build_modify_expr_1 (build_indirect_ref (elt_lhs
, NULL_PTR
),
6005 build_indirect_ref (elt_rhs
, NULL_PTR
),
6006 TYPE_BINFO (lhstype
)),
6008 if (TREE_VALUE (result
) == error_mark_node
)
6009 return error_mark_node
;
6010 vbases
= TREE_CHAIN (vbases
);
6012 result
= tree_cons (NULL_TREE
,
6013 build_modify_expr_1 (lhs
,
6016 TYPE_BINFO (lhstype
)),
6018 return build_compound_expr (result
);
6022 /* Convert new value to destination type. */
6024 if (TREE_CODE (lhstype
) == ARRAY_TYPE
)
6028 if (! comptypes (lhstype
, TREE_TYPE (rhs
), 0))
6030 cp_error ("incompatible types in assignment of `%T' to `%T'",
6031 TREE_TYPE (rhs
), lhstype
);
6032 return error_mark_node
;
6035 /* Allow array assignment in compiler-generated code. */
6036 if (pedantic
&& ! DECL_ARTIFICIAL (current_function_decl
))
6037 pedwarn ("ANSI C++ forbids assignment of arrays");
6039 /* Have to wrap this in RTL_EXPR for two cases:
6040 in base or member initialization and if we
6041 are a branch of a ?: operator. Since we
6042 can't easily know the latter, just do it always. */
6044 result
= make_node (RTL_EXPR
);
6046 TREE_TYPE (result
) = void_type_node
;
6047 do_pending_stack_adjust ();
6048 start_sequence_for_rtl_expr (result
);
6050 /* As a matter of principle, `start_sequence' should do this. */
6053 from_array
= TREE_CODE (TREE_TYPE (newrhs
)) == ARRAY_TYPE
6054 ? 1 + (modifycode
!= INIT_EXPR
): 0;
6055 expand_vec_init (lhs
, lhs
, array_type_nelts (lhstype
), newrhs
,
6058 do_pending_stack_adjust ();
6060 TREE_SIDE_EFFECTS (result
) = 1;
6061 RTL_EXPR_SEQUENCE (result
) = get_insns ();
6062 RTL_EXPR_RTL (result
) = const0_rtx
;
6067 if (modifycode
== INIT_EXPR
)
6069 newrhs
= convert_for_initialization (lhs
, lhstype
, newrhs
, LOOKUP_NORMAL
,
6070 "assignment", NULL_TREE
, 0);
6071 if (lhs
== DECL_RESULT (current_function_decl
))
6073 if (DECL_INITIAL (lhs
))
6074 warning ("return value from function receives multiple initializations");
6075 DECL_INITIAL (lhs
) = newrhs
;
6081 if (IS_AGGR_TYPE (lhstype
))
6083 if (result
= build_opfncall (MODIFY_EXPR
,
6084 LOOKUP_NORMAL
, lhs
, newrhs
,
6085 make_node (NOP_EXPR
)))
6089 /* Avoid warnings on enum bit fields. */
6090 if (TREE_CODE (olhstype
) == ENUMERAL_TYPE
6091 && TREE_CODE (lhstype
) == INTEGER_TYPE
)
6093 newrhs
= convert_for_assignment (olhstype
, newrhs
, "assignment",
6095 newrhs
= convert_force (lhstype
, newrhs
, 0);
6098 newrhs
= convert_for_assignment (lhstype
, newrhs
, "assignment",
6100 if (TREE_CODE (newrhs
) == CALL_EXPR
6101 && TYPE_NEEDS_CONSTRUCTING (lhstype
))
6102 newrhs
= build_cplus_new (lhstype
, newrhs
, 0);
6104 /* Can't initialize directly from a TARGET_EXPR, since that would
6105 cause the lhs to be constructed twice. So we force the
6106 TARGET_EXPR to be expanded. expand_expr should really do this
6108 if (TREE_CODE (newrhs
) == TARGET_EXPR
)
6109 newrhs
= expand_target_expr (newrhs
);
6112 if (TREE_CODE (newrhs
) == ERROR_MARK
)
6113 return error_mark_node
;
6115 if (TREE_CODE (newrhs
) == COND_EXPR
)
6118 tree cond
= TREE_OPERAND (newrhs
, 0);
6120 if (TREE_SIDE_EFFECTS (lhs
))
6121 cond
= build_compound_expr (tree_cons
6123 build_tree_list (NULL_TREE
, cond
)));
6125 /* Cannot have two identical lhs on this one tree (result) as preexpand
6126 calls will rip them out and fill in RTL for them, but when the
6127 rtl is generated, the calls will only be in the first side of the
6128 condition, not on both, or before the conditional jump! (mrs) */
6129 lhs1
= break_out_calls (lhs
);
6132 /* If there's no change, the COND_EXPR behaves like any other rhs. */
6133 result
= build (modifycode
== NOP_EXPR
? MODIFY_EXPR
: INIT_EXPR
,
6134 lhstype
, lhs
, newrhs
);
6137 tree result_type
= TREE_TYPE (newrhs
);
6138 /* We have to convert each arm to the proper type because the
6139 types may have been munged by constant folding. */
6141 = build (COND_EXPR
, result_type
, cond
,
6142 build_modify_expr (lhs
, modifycode
,
6143 convert (result_type
,
6144 TREE_OPERAND (newrhs
, 1))),
6145 build_modify_expr (lhs1
, modifycode
,
6146 convert (result_type
,
6147 TREE_OPERAND (newrhs
, 2))));
6150 else if (modifycode
!= INIT_EXPR
&& TREE_CODE (newrhs
) == WITH_CLEANUP_EXPR
)
6152 tree cleanup
= TREE_OPERAND (newrhs
, 2);
6155 /* Finish up by running cleanups and having the "value" of the lhs. */
6156 tree exprlist
= tree_cons (NULL_TREE
, cleanup
,
6157 build_tree_list (NULL_TREE
, lhs
));
6158 newrhs
= TREE_OPERAND (newrhs
, 0);
6159 if (TREE_CODE (newrhs
) == TARGET_EXPR
)
6160 slot
= TREE_OPERAND (newrhs
, 0);
6161 else if (TREE_CODE (newrhs
) == ADDR_EXPR
)
6163 /* Bad but valid. */
6165 warning ("address taken of temporary object");
6168 my_friendly_abort (118);
6170 /* Copy the value computed in SLOT into LHS. */
6171 exprlist
= tree_cons (NULL_TREE
,
6172 build_modify_expr (lhs
, modifycode
, slot
),
6174 /* Evaluate the expression that needs CLEANUP. This will
6175 compute the value into SLOT. */
6176 exprlist
= tree_cons (NULL_TREE
, newrhs
, exprlist
);
6177 result
= convert (lhstype
, build_compound_expr (exprlist
));
6180 result
= build (modifycode
== NOP_EXPR
? MODIFY_EXPR
: INIT_EXPR
,
6181 lhstype
, lhs
, newrhs
);
6182 TREE_SIDE_EFFECTS (result
) = 1;
6184 /* If we got the LHS in a different type for storing in,
6185 convert the result back to the nominal type of LHS
6186 so that the value we return always has the same type
6187 as the LHS argument. */
6189 if (olhstype
== TREE_TYPE (result
))
6191 /* Avoid warnings converting integral types back into enums
6192 for enum bit fields. */
6193 if (TREE_CODE (TREE_TYPE (result
)) == INTEGER_TYPE
6194 && TREE_CODE (olhstype
) == ENUMERAL_TYPE
)
6196 result
= build (COMPOUND_EXPR
, olhstype
, result
, olhs
);
6197 TREE_NO_UNUSED_WARNING (result
) = 1;
6200 return convert_for_assignment (olhstype
, result
, "assignment",
6205 /* Return 0 if EXP is not a valid lvalue in this language
6206 even though `lvalue_or_else' would accept it. */
6209 language_lvalue_valid (exp
)
6215 /* Get difference in deltas for different pointer to member function
6216 types. Return integer_zero_node, if FROM cannot be converted to a
6217 TO type. If FORCE is true, then allow reverse conversions as well. */
6219 get_delta_difference (from
, to
, force
)
6223 tree delta
= integer_zero_node
;
6229 /* Should get_base_distance here, so we can check if any thing along the
6230 path is virtual, and we need to make sure we stay
6231 inside the real binfos when going through virtual bases.
6232 Maybe we should replace virtual bases with
6233 binfo_member (...CLASSTYPE_VBASECLASSES...)... (mrs) */
6234 binfo
= get_binfo (from
, to
, 1);
6235 if (binfo
== error_mark_node
)
6237 error (" in pointer to member function conversion");
6244 error_not_base_type (from
, to
);
6245 error (" in pointer to member function conversion");
6248 binfo
= get_binfo (to
, from
, 1);
6249 if (binfo
== error_mark_node
)
6251 error (" in pointer to member function conversion");
6256 error ("cannot convert pointer to member of type %T to unrelated pointer to member of type %T", from
, to
);
6259 if (TREE_VIA_VIRTUAL (binfo
))
6261 warning ("pointer to member conversion to virtual base class will only work if you are very careful");
6263 return build_binary_op (MINUS_EXPR
,
6265 BINFO_OFFSET (binfo
), 1);
6267 if (TREE_VIA_VIRTUAL (binfo
))
6269 warning ("pointer to member conversion from virtual base class will only work if you are very careful");
6271 return BINFO_OFFSET (binfo
);
6274 /* Build a constructor for a pointer to member function. It can be
6275 used to initialize global variables, local variable, or used
6276 as a value in expressions. TYPE is the POINTER to METHOD_TYPE we
6279 If FORCE is non-zero, then force this conversion, even if
6280 we would rather not do it. Usually set when using an explicit
6283 Return error_mark_node, if something goes wrong. */
6286 build_ptrmemfunc (type
, pfn
, force
)
6290 tree index
= integer_zero_node
;
6291 tree delta
= integer_zero_node
;
6292 tree delta2
= integer_zero_node
;
6297 /* Handle multiple conversions of pointer to member functions. */
6298 if (TYPE_PTRMEMFUNC_P (TREE_TYPE (pfn
)))
6300 tree ndelta
, ndelta2
, nindex
;
6301 /* Is is already the right type? */
6303 /* Sorry, can't do this, the backend is too stupid. */
6304 if (TYPE_METHOD_BASETYPE (TREE_TYPE (type
))
6305 == TYPE_METHOD_BASETYPE (TREE_TYPE (TYPE_PTRMEMFUNC_FN_TYPE (TREE_TYPE (pfn
)))))
6307 if (type
!= TYPE_PTRMEMFUNC_FN_TYPE (TREE_TYPE (pfn
)))
6309 npfn
= build1 (NOP_EXPR
, TYPE_GET_PTRMEMFUNC_TYPE (type
), pfn
);
6310 TREE_CONSTANT (npfn
) = TREE_CONSTANT (pfn
);
6315 if (type
== TYPE_PTRMEMFUNC_FN_TYPE (TREE_TYPE (pfn
)))
6319 if (TREE_CODE (pfn
) != CONSTRUCTOR
)
6322 ndelta
= convert (ptrdiff_type_node
, build_component_ref (pfn
, delta_identifier
, 0, 0));
6323 ndelta2
= convert (ptrdiff_type_node
, DELTA2_FROM_PTRMEMFUNC (pfn
));
6324 index
= build_component_ref (pfn
, index_identifier
, 0, 0);
6325 delta
= get_delta_difference (TYPE_METHOD_BASETYPE (TREE_TYPE (TYPE_PTRMEMFUNC_FN_TYPE (TREE_TYPE (pfn
)))),
6326 TYPE_METHOD_BASETYPE (TREE_TYPE (type
)),
6328 delta
= build_binary_op (PLUS_EXPR
, delta
, ndelta
, 1);
6329 delta2
= build_binary_op (PLUS_EXPR
, ndelta2
, delta2
, 1);
6330 e1
= fold (build (GT_EXPR
, boolean_type_node
, index
, integer_zero_node
));
6332 u
= build_nt (CONSTRUCTOR
, 0, tree_cons (delta2_identifier
, delta2
, NULL_TREE
));
6333 u
= build_nt (CONSTRUCTOR
, 0, tree_cons (NULL_TREE
, delta
,
6334 tree_cons (NULL_TREE
, index
,
6335 tree_cons (NULL_TREE
, u
, NULL_TREE
))));
6336 e2
= digest_init (TYPE_GET_PTRMEMFUNC_TYPE (type
), u
, (tree
*)0);
6338 pfn
= PFN_FROM_PTRMEMFUNC (pfn
);
6339 npfn
= build1 (NOP_EXPR
, type
, pfn
);
6340 TREE_CONSTANT (npfn
) = TREE_CONSTANT (pfn
);
6342 u
= build_nt (CONSTRUCTOR
, 0, tree_cons (pfn_identifier
, npfn
, NULL_TREE
));
6343 u
= build_nt (CONSTRUCTOR
, 0, tree_cons (NULL_TREE
, delta
,
6344 tree_cons (NULL_TREE
, index
,
6345 tree_cons (NULL_TREE
, u
, NULL_TREE
))));
6346 e3
= digest_init (TYPE_GET_PTRMEMFUNC_TYPE (type
), u
, (tree
*)0);
6347 return build_conditional_expr (e1
, e2
, e3
);
6350 ndelta
= TREE_VALUE (CONSTRUCTOR_ELTS (pfn
));
6351 nindex
= TREE_VALUE (TREE_CHAIN (CONSTRUCTOR_ELTS (pfn
)));
6352 npfn
= TREE_VALUE (TREE_CHAIN (TREE_CHAIN (CONSTRUCTOR_ELTS (pfn
))));
6353 npfn
= TREE_VALUE (CONSTRUCTOR_ELTS (npfn
));
6354 if (integer_zerop (nindex
))
6355 pfn
= integer_zero_node
;
6358 sorry ("value casting of variable nonnull pointer to member functions not supported");
6359 return error_mark_node
;
6363 /* Handle null pointer to member function conversions. */
6364 if (integer_zerop (pfn
))
6366 pfn
= build_c_cast (type
, integer_zero_node
, 0);
6367 u
= build_nt (CONSTRUCTOR
, 0, tree_cons (pfn_identifier
, pfn
, NULL_TREE
));
6368 u
= build_nt (CONSTRUCTOR
, 0, tree_cons (NULL_TREE
, integer_zero_node
,
6369 tree_cons (NULL_TREE
, integer_zero_node
,
6370 tree_cons (NULL_TREE
, u
, NULL_TREE
))));
6371 return digest_init (TYPE_GET_PTRMEMFUNC_TYPE (type
), u
, (tree
*)0);
6374 if (TREE_CODE (pfn
) == TREE_LIST
6375 || (TREE_CODE (pfn
) == ADDR_EXPR
6376 && TREE_CODE (TREE_OPERAND (pfn
, 0)) == TREE_LIST
))
6378 pfn
= instantiate_type (type
, pfn
, 1);
6379 if (pfn
== error_mark_node
)
6380 return error_mark_node
;
6381 if (TREE_CODE (pfn
) != ADDR_EXPR
)
6382 pfn
= build_unary_op (ADDR_EXPR
, pfn
, 0);
6385 /* Allow pointer to member conversions here. */
6386 delta
= get_delta_difference (TYPE_METHOD_BASETYPE (TREE_TYPE (TREE_TYPE (pfn
))),
6387 TYPE_METHOD_BASETYPE (TREE_TYPE (type
)),
6389 delta2
= build_binary_op (PLUS_EXPR
, delta2
, delta
, 1);
6391 if (TREE_CODE (TREE_OPERAND (pfn
, 0)) != FUNCTION_DECL
)
6392 warning ("assuming pointer to member function is non-virtual");
6394 if (TREE_CODE (TREE_OPERAND (pfn
, 0)) == FUNCTION_DECL
6395 && DECL_VINDEX (TREE_OPERAND (pfn
, 0)))
6397 /* Find the offset to the vfield pointer in the object. */
6398 vfield_offset
= get_binfo (DECL_CONTEXT (TREE_OPERAND (pfn
, 0)),
6399 DECL_CLASS_CONTEXT (TREE_OPERAND (pfn
, 0)),
6401 vfield_offset
= get_vfield_offset (vfield_offset
);
6402 delta2
= size_binop (PLUS_EXPR
, vfield_offset
, delta2
);
6404 /* Map everything down one to make room for the null pointer to member. */
6405 index
= size_binop (PLUS_EXPR
,
6406 DECL_VINDEX (TREE_OPERAND (pfn
, 0)),
6408 u
= build_nt (CONSTRUCTOR
, 0, tree_cons (delta2_identifier
, delta2
, NULL_TREE
));
6412 index
= size_binop (MINUS_EXPR
, integer_zero_node
, integer_one_node
);
6414 npfn
= build1 (NOP_EXPR
, type
, pfn
);
6415 TREE_CONSTANT (npfn
) = TREE_CONSTANT (pfn
);
6417 u
= build_nt (CONSTRUCTOR
, 0, tree_cons (pfn_identifier
, npfn
, NULL_TREE
));
6420 u
= build_nt (CONSTRUCTOR
, 0, tree_cons (NULL_TREE
, delta
,
6421 tree_cons (NULL_TREE
, index
,
6422 tree_cons (NULL_TREE
, u
, NULL_TREE
))));
6423 return digest_init (TYPE_GET_PTRMEMFUNC_TYPE (type
), u
, (tree
*)0);
6426 /* Convert value RHS to type TYPE as preparation for an assignment
6427 to an lvalue of type TYPE.
6428 The real work of conversion is done by `convert'.
6429 The purpose of this function is to generate error messages
6430 for assignments that are not allowed in C.
6431 ERRTYPE is a string to use in error messages:
6432 "assignment", "return", etc.
6434 C++: attempts to allow `convert' to find conversions involving
6435 implicit type conversion between aggregate and scalar types
6436 as per 8.5.6 of C++ manual. Does not randomly dereference
6437 pointers to aggregates! */
6440 convert_for_assignment (type
, rhs
, errtype
, fndecl
, parmnum
)
6446 register enum tree_code codel
= TREE_CODE (type
);
6447 register tree rhstype
;
6448 register enum tree_code coder
= TREE_CODE (TREE_TYPE (rhs
));
6450 if (coder
== UNKNOWN_TYPE
)
6451 rhs
= instantiate_type (type
, rhs
, 1);
6453 if (coder
== ERROR_MARK
)
6454 return error_mark_node
;
6456 if (codel
== OFFSET_TYPE
)
6458 type
= TREE_TYPE (type
);
6459 codel
= TREE_CODE (type
);
6462 /* Strip NON_LVALUE_EXPRs since we aren't using as an lvalue. */
6463 if (TREE_CODE (rhs
) == NON_LVALUE_EXPR
)
6464 rhs
= TREE_OPERAND (rhs
, 0);
6466 if (rhs
== error_mark_node
)
6467 return error_mark_node
;
6469 if (TREE_VALUE (rhs
) == error_mark_node
)
6470 return error_mark_node
;
6472 if (TREE_CODE (TREE_TYPE (rhs
)) == OFFSET_TYPE
)
6474 rhs
= resolve_offset_ref (rhs
);
6475 if (rhs
== error_mark_node
)
6476 return error_mark_node
;
6477 rhstype
= TREE_TYPE (rhs
);
6478 coder
= TREE_CODE (rhstype
);
6481 if (TREE_CODE (TREE_TYPE (rhs
)) == ARRAY_TYPE
6482 || TREE_CODE (TREE_TYPE (rhs
)) == FUNCTION_TYPE
6483 || TREE_CODE (TREE_TYPE (rhs
)) == METHOD_TYPE
)
6484 rhs
= default_conversion (rhs
);
6485 else if (TREE_CODE (TREE_TYPE (rhs
)) == REFERENCE_TYPE
)
6486 rhs
= convert_from_reference (rhs
);
6488 rhstype
= TREE_TYPE (rhs
);
6489 coder
= TREE_CODE (rhstype
);
6491 /* This should no longer change types on us. */
6492 if (TREE_CODE (rhs
) == CONST_DECL
)
6493 rhs
= DECL_INITIAL (rhs
);
6494 else if (TREE_READONLY_DECL_P (rhs
))
6495 rhs
= decl_constant_value (rhs
);
6497 if (type
== rhstype
)
6499 overflow_warning (rhs
);
6503 if (coder
== VOID_TYPE
)
6505 error ("void value not ignored as it ought to be");
6506 return error_mark_node
;
6508 /* Arithmetic types all interconvert. */
6509 if ((codel
== INTEGER_TYPE
|| codel
== REAL_TYPE
|| codel
== BOOLEAN_TYPE
)
6510 && (coder
== INTEGER_TYPE
|| coder
== REAL_TYPE
|| coder
== BOOLEAN_TYPE
))
6512 /* But we should warn if assigning REAL_TYPE to INTEGER_TYPE. */
6513 if (coder
== REAL_TYPE
&& codel
== INTEGER_TYPE
)
6516 cp_warning ("`%T' used for argument %P of `%D'",
6517 rhstype
, parmnum
, fndecl
);
6519 cp_warning ("%s to `%T' from `%T'", errtype
, type
, rhstype
);
6521 /* And we should warn if assigning a negative value to
6522 an unsigned variable. */
6523 else if (TREE_UNSIGNED (type
) && codel
!= BOOLEAN_TYPE
)
6525 if (TREE_CODE (rhs
) == INTEGER_CST
6526 && TREE_NEGATED_INT (rhs
))
6529 cp_warning ("negative value `%E' passed as argument %P of `%D'",
6530 rhs
, parmnum
, fndecl
);
6532 cp_warning ("%s of negative value `%E' to `%T'",
6533 errtype
, rhs
, type
);
6535 overflow_warning (rhs
);
6536 if (TREE_CONSTANT (rhs
))
6540 return convert_and_check (type
, rhs
);
6542 /* Conversions involving enums. */
6543 else if ((codel
== ENUMERAL_TYPE
6544 && (INTEGRAL_CODE_P (coder
) || coder
== REAL_TYPE
))
6545 || (coder
== ENUMERAL_TYPE
6546 && (INTEGRAL_CODE_P (codel
) || codel
== REAL_TYPE
)))
6548 return cp_convert (type
, rhs
, CONV_IMPLICIT
, LOOKUP_NORMAL
);
6550 /* Conversions among pointers */
6551 else if (codel
== POINTER_TYPE
6552 && (coder
== POINTER_TYPE
6553 || (coder
== RECORD_TYPE
6554 && (IS_SIGNATURE_POINTER (rhstype
)
6555 || IS_SIGNATURE_REFERENCE (rhstype
)))))
6557 register tree ttl
= TREE_TYPE (type
);
6561 if (coder
== RECORD_TYPE
)
6563 rhs
= build_optr_ref (rhs
);
6564 rhstype
= TREE_TYPE (rhs
);
6566 ttr
= TREE_TYPE (rhstype
);
6568 /* If both pointers are of aggregate type, then we
6569 can give better error messages, and save some work
6571 if (TREE_CODE (ttl
) == RECORD_TYPE
&& TREE_CODE (ttr
) == RECORD_TYPE
)
6575 if (TYPE_MAIN_VARIANT (ttl
) == TYPE_MAIN_VARIANT (ttr
)
6576 || type
== class_star_type_node
6577 || rhstype
== class_star_type_node
)
6578 binfo
= TYPE_BINFO (ttl
);
6580 binfo
= get_binfo (ttl
, ttr
, 1);
6582 if (binfo
== error_mark_node
)
6583 return error_mark_node
;
6585 return error_not_base_type (ttl
, ttr
);
6587 if (! TYPE_READONLY (ttl
) && TYPE_READONLY (ttr
))
6590 cp_pedwarn ("passing `%T' as argument %P of `%D' discards const",
6591 rhstype
, parmnum
, fndecl
);
6593 cp_pedwarn ("%s to `%T' from `%T' discards const",
6594 errtype
, type
, rhstype
);
6596 if (! TYPE_VOLATILE (ttl
) && TYPE_VOLATILE (ttr
))
6599 cp_pedwarn ("passing `%T' as argument %P of `%D' discards volatile",
6600 rhstype
, parmnum
, fndecl
);
6602 cp_pedwarn ("%s to `%T' from `%T' discards volatile",
6603 errtype
, type
, rhstype
);
6607 /* Any non-function converts to a [const][volatile] void *
6608 and vice versa; otherwise, targets must be the same.
6609 Meanwhile, the lhs target must have all the qualifiers of the rhs. */
6610 else if (TYPE_MAIN_VARIANT (ttl
) == void_type_node
6611 || TYPE_MAIN_VARIANT (ttr
) == void_type_node
6612 || (ctt
= comp_target_types (type
, rhstype
, 1))
6613 || (unsigned_type (TYPE_MAIN_VARIANT (ttl
))
6614 == unsigned_type (TYPE_MAIN_VARIANT (ttr
))))
6616 /* ARM $4.8, commentary on p39. */
6617 if (TYPE_MAIN_VARIANT (ttl
) == void_type_node
6618 && TREE_CODE (ttr
) == OFFSET_TYPE
)
6620 cp_error ("no standard conversion from `%T' to `void *'", ttr
);
6621 return error_mark_node
;
6625 cp_pedwarn ("converting `%T' to `%T' is a contravariance violation",
6628 if (TYPE_MAIN_VARIANT (ttl
) != void_type_node
6629 && TYPE_MAIN_VARIANT (ttr
) == void_type_node
6630 && rhs
!= null_pointer_node
)
6632 if (coder
== RECORD_TYPE
)
6633 cp_pedwarn ("implicit conversion of signature pointer to type `%T'",
6636 pedwarn ("ANSI C++ forbids implicit conversion from `void *' in %s",
6639 /* Const and volatile mean something different for function types,
6640 so the usual warnings are not appropriate. */
6641 else if ((TREE_CODE (ttr
) != FUNCTION_TYPE
&& TREE_CODE (ttr
) != METHOD_TYPE
)
6642 || (TREE_CODE (ttl
) != FUNCTION_TYPE
&& TREE_CODE (ttl
) != METHOD_TYPE
))
6644 if (TREE_CODE (ttl
) == OFFSET_TYPE
6645 && binfo_member (TYPE_OFFSET_BASETYPE (ttr
),
6646 CLASSTYPE_VBASECLASSES (TYPE_OFFSET_BASETYPE (ttl
))))
6648 sorry ("%s between pointer to members converting across virtual baseclasses", errtype
);
6649 return error_mark_node
;
6651 else if (! TYPE_READONLY (ttl
) && TYPE_READONLY (ttr
))
6654 cp_pedwarn ("passing `%T' as argument %P of `%D' discards const",
6655 rhstype
, parmnum
, fndecl
);
6657 cp_pedwarn ("%s to `%T' from `%T' discards const",
6658 errtype
, type
, rhstype
);
6660 else if (! TYPE_VOLATILE (ttl
) && TYPE_VOLATILE (ttr
))
6663 cp_pedwarn ("passing `%T' as argument %P of `%D' discards volatile",
6664 rhstype
, parmnum
, fndecl
);
6666 cp_pedwarn ("%s to `%T' from `%T' discards volatile",
6667 errtype
, type
, rhstype
);
6669 else if (TREE_CODE (ttl
) == TREE_CODE (ttr
)
6670 && ! comp_target_types (type
, rhstype
, 1))
6673 cp_pedwarn ("passing `%T' as argument %P of `%D' changes signedness",
6674 rhstype
, parmnum
, fndecl
);
6676 cp_pedwarn ("%s to `%T' from `%T' changes signedness",
6677 errtype
, type
, rhstype
);
6681 else if (TREE_CODE (ttr
) == OFFSET_TYPE
6682 && TREE_CODE (ttl
) != OFFSET_TYPE
)
6684 /* Normally, pointers to different type codes (other
6685 than void) are not compatible, but we perform
6686 some type instantiation if that resolves the
6687 ambiguity of (X Y::*) and (X *). */
6689 if (current_class_decl
)
6691 if (TREE_CODE (rhs
) == INTEGER_CST
)
6693 rhs
= build (PLUS_EXPR
, build_pointer_type (TREE_TYPE (ttr
)),
6694 current_class_decl
, rhs
);
6695 return convert_for_assignment (type
, rhs
,
6696 errtype
, fndecl
, parmnum
);
6699 if (TREE_CODE (ttl
) == METHOD_TYPE
)
6700 error ("%s between pointer-to-method and pointer-to-member types",
6703 error ("%s between pointer and pointer-to-member types", errtype
);
6704 return error_mark_node
;
6708 int add_quals
= 0, const_parity
= 0, volatile_parity
= 0;
6710 int unsigned_parity
;
6713 /* This code is basically a duplicate of comp_ptr_ttypes_real. */
6714 for (; ; ttl
= TREE_TYPE (ttl
), ttr
= TREE_TYPE (ttr
))
6717 const_parity
|= TYPE_READONLY (ttl
) < TYPE_READONLY (ttr
);
6718 volatile_parity
|= TYPE_VOLATILE (ttl
) < TYPE_VOLATILE (ttr
);
6721 && (TYPE_READONLY (ttl
) > TYPE_READONLY (ttr
)
6722 || TYPE_VOLATILE (ttl
) > TYPE_VOLATILE (ttr
)))
6724 left_const
&= TYPE_READONLY (ttl
);
6726 if (TREE_CODE (ttl
) != POINTER_TYPE
6727 || TREE_CODE (ttr
) != POINTER_TYPE
)
6730 unsigned_parity
= TREE_UNSIGNED (ttl
) - TREE_UNSIGNED (ttr
);
6731 if (unsigned_parity
)
6733 if (TREE_UNSIGNED (ttl
))
6734 ttr
= unsigned_type (ttr
);
6736 ttl
= unsigned_type (ttl
);
6739 if (comp_target_types (ttl
, ttr
, nptrs
) > 0)
6744 cp_pedwarn ("passing `%T' as argument %P of `%D' adds cv-quals without intervening `const'",
6745 rhstype
, parmnum
, fndecl
);
6747 cp_pedwarn ("%s to `%T' from `%T' adds cv-quals without intervening `const'",
6748 errtype
, type
, rhstype
);
6753 cp_pedwarn ("passing `%T' as argument %P of `%D' discards const",
6754 rhstype
, parmnum
, fndecl
);
6756 cp_pedwarn ("%s to `%T' from `%T' discards const",
6757 errtype
, type
, rhstype
);
6759 if (volatile_parity
)
6762 cp_pedwarn ("passing `%T' as argument %P of `%D' discards volatile",
6763 rhstype
, parmnum
, fndecl
);
6765 cp_pedwarn ("%s to `%T' from `%T' discards volatile",
6766 errtype
, type
, rhstype
);
6768 if (unsigned_parity
> 0)
6771 cp_pedwarn ("passing `%T' as argument %P of `%D' changes signed to unsigned",
6772 rhstype
, parmnum
, fndecl
);
6774 cp_pedwarn ("%s to `%T' from `%T' changes signed to unsigned",
6775 errtype
, type
, rhstype
);
6777 else if (unsigned_parity
< 0)
6780 cp_pedwarn ("passing `%T' as argument %P of `%D' changes unsigned to signed",
6781 rhstype
, parmnum
, fndecl
);
6783 cp_pedwarn ("%s to `%T' from `%T' changes unsigned to signed",
6784 errtype
, type
, rhstype
);
6787 /* C++ is not so friendly about converting function and
6788 member function pointers as C. Emit warnings here. */
6789 if (TREE_CODE (ttl
) == FUNCTION_TYPE
6790 || TREE_CODE (ttl
) == METHOD_TYPE
)
6791 if (! comptypes (ttl
, ttr
, 0))
6793 warning ("conflicting function types in %s:", errtype
);
6794 cp_warning ("\t`%T' != `%T'", type
, rhstype
);
6797 else if (TREE_CODE (TREE_TYPE (rhs
)) == METHOD_TYPE
)
6799 /* When does this happen? */
6800 my_friendly_abort (119);
6801 /* Conversion of a pointer-to-member type to void *. */
6802 rhs
= build_unary_op (ADDR_EXPR
, rhs
, 0);
6803 TREE_TYPE (rhs
) = type
;
6806 else if (TREE_CODE (TREE_TYPE (rhs
)) == OFFSET_TYPE
)
6808 /* When does this happen? */
6809 my_friendly_abort (120);
6810 /* Conversion of a pointer-to-member type to void *. */
6811 rhs
= build_unary_op (ADDR_EXPR
, rhs
, 0);
6812 TREE_TYPE (rhs
) = type
;
6818 cp_error ("passing `%T' as argument %P of `%D'",
6819 rhstype
, parmnum
, fndecl
);
6821 cp_error ("%s to `%T' from `%T'", errtype
, type
, rhstype
);
6822 return error_mark_node
;
6825 return convert (type
, rhs
);
6827 else if (codel
== POINTER_TYPE
&& coder
== INTEGER_TYPE
)
6829 /* An explicit constant 0 can convert to a pointer,
6830 but not a 0 that results from casting or folding. */
6831 if (! (TREE_CODE (rhs
) == INTEGER_CST
&& integer_zerop (rhs
)))
6834 cp_pedwarn ("passing `%T' to argument %P of `%D' lacks a cast",
6835 rhstype
, parmnum
, fndecl
);
6837 cp_pedwarn ("%s to `%T' from `%T' lacks a cast",
6838 errtype
, type
, rhstype
);
6839 return convert (type
, rhs
);
6841 return null_pointer_node
;
6843 else if (codel
== INTEGER_TYPE
6844 && (coder
== POINTER_TYPE
6845 || (coder
== RECORD_TYPE
6846 && (IS_SIGNATURE_POINTER (rhstype
)
6847 || TYPE_PTRMEMFUNC_FLAG (rhstype
)
6848 || IS_SIGNATURE_REFERENCE (rhstype
)))))
6851 cp_pedwarn ("passing `%T' to argument %P of `%D' lacks a cast",
6852 rhstype
, parmnum
, fndecl
);
6854 cp_pedwarn ("%s to `%T' from `%T' lacks a cast",
6855 errtype
, type
, rhstype
);
6856 return convert (type
, rhs
);
6858 else if (codel
== BOOLEAN_TYPE
6859 && (coder
== POINTER_TYPE
6860 || (coder
== RECORD_TYPE
6861 && (IS_SIGNATURE_POINTER (rhstype
)
6862 || TYPE_PTRMEMFUNC_FLAG (rhstype
)
6863 || IS_SIGNATURE_REFERENCE (rhstype
)))))
6864 return convert (type
, rhs
);
6867 else if (((coder
== POINTER_TYPE
6868 && TREE_CODE (TREE_TYPE (rhstype
)) == METHOD_TYPE
)
6869 || integer_zerop (rhs
)
6870 || TYPE_PTRMEMFUNC_P (rhstype
))
6871 && TYPE_PTRMEMFUNC_P (type
))
6873 tree ttl
= TYPE_PTRMEMFUNC_FN_TYPE (type
);
6874 tree ttr
= (TREE_CODE (rhstype
) == POINTER_TYPE
? rhstype
6875 : TYPE_PTRMEMFUNC_FN_TYPE (type
));
6876 int ctt
= comp_target_types (ttl
, ttr
, 1);
6879 cp_pedwarn ("converting `%T' to `%T' is a contravariance violation",
6882 cp_error ("%s to `%T' from `%T'", errtype
, ttl
, ttr
);
6884 /* compatible pointer to member functions. */
6885 return build_ptrmemfunc (ttl
, rhs
, 0);
6887 else if (codel
== ERROR_MARK
|| coder
== ERROR_MARK
)
6888 return error_mark_node
;
6890 /* This should no longer happen. References are initialized via
6891 `convert_for_initialization'. They should otherwise be
6892 bashed before coming here. */
6893 else if (codel
== REFERENCE_TYPE
)
6894 my_friendly_abort (317);
6895 else if (TYPE_MAIN_VARIANT (type
) == TYPE_MAIN_VARIANT (TREE_TYPE (rhs
)))
6897 tree nrhs
= build1 (NOP_EXPR
, type
, rhs
);
6898 TREE_CONSTANT (nrhs
) = TREE_CONSTANT (rhs
);
6901 else if (TYPE_HAS_CONSTRUCTOR (type
) || IS_AGGR_TYPE (TREE_TYPE (rhs
)))
6902 return convert (type
, rhs
);
6904 cp_error ("%s to `%T' from `%T'", errtype
, type
, rhstype
);
6905 return error_mark_node
;
6908 /* Convert RHS to be of type TYPE. If EXP is non-zero,
6909 it is the target of the initialization.
6910 ERRTYPE is a string to use in error messages.
6912 Two major differences between the behavior of
6913 `convert_for_assignment' and `convert_for_initialization'
6914 are that references are bashed in the former, while
6915 copied in the latter, and aggregates are assigned in
6916 the former (operator=) while initialized in the
6919 If using constructor make sure no conversion operator exists, if one does
6920 exist, an ambiguity exists.
6922 If flags doesn't include LOOKUP_COMPLAIN, don't complain about anything. */
6924 convert_for_initialization (exp
, type
, rhs
, flags
, errtype
, fndecl
, parmnum
)
6925 tree exp
, type
, rhs
;
6931 register enum tree_code codel
= TREE_CODE (type
);
6932 register tree rhstype
;
6933 register enum tree_code coder
;
6935 /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
6936 Strip such NOP_EXPRs, since RHS is used in non-lvalue context. */
6937 if (TREE_CODE (rhs
) == NOP_EXPR
6938 && TREE_TYPE (rhs
) == TREE_TYPE (TREE_OPERAND (rhs
, 0))
6939 && codel
!= REFERENCE_TYPE
)
6940 rhs
= TREE_OPERAND (rhs
, 0);
6942 if (rhs
== error_mark_node
6943 || (TREE_CODE (rhs
) == TREE_LIST
&& TREE_VALUE (rhs
) == error_mark_node
))
6944 return error_mark_node
;
6946 if (TREE_CODE (TREE_TYPE (rhs
)) == OFFSET_TYPE
)
6948 rhs
= resolve_offset_ref (rhs
);
6949 if (rhs
== error_mark_node
)
6950 return error_mark_node
;
6953 if (TREE_CODE (TREE_TYPE (rhs
)) == REFERENCE_TYPE
)
6954 rhs
= convert_from_reference (rhs
);
6956 if ((TREE_CODE (TREE_TYPE (rhs
)) == ARRAY_TYPE
6957 && TREE_CODE (type
) != ARRAY_TYPE
6958 && (TREE_CODE (type
) != REFERENCE_TYPE
6959 || TREE_CODE (TREE_TYPE (type
)) != ARRAY_TYPE
))
6960 || TREE_CODE (TREE_TYPE (rhs
)) == FUNCTION_TYPE
6961 || TREE_CODE (TREE_TYPE (rhs
)) == METHOD_TYPE
)
6962 rhs
= default_conversion (rhs
);
6964 rhstype
= TREE_TYPE (rhs
);
6965 coder
= TREE_CODE (rhstype
);
6967 if (coder
== UNKNOWN_TYPE
)
6969 rhs
= instantiate_type (type
, rhs
, 1);
6970 rhstype
= TREE_TYPE (rhs
);
6971 coder
= TREE_CODE (rhstype
);
6974 if (coder
== ERROR_MARK
)
6975 return error_mark_node
;
6978 /* This is *not* the quick way out! It is the way to disaster. */
6979 if (type
== rhstype
)
6983 /* We accept references to incomplete types, so we can
6984 return here before checking if RHS is of complete type. */
6986 if (codel
== REFERENCE_TYPE
)
6988 /* This should eventually happen in convert_arguments. */
6989 extern int warningcount
, errorcount
;
6993 savew
= warningcount
, savee
= errorcount
;
6994 rhs
= convert_to_reference (type
, rhs
, CONV_IMPLICIT
, flags
,
6995 exp
? exp
: error_mark_node
);
6998 if (warningcount
> savew
)
6999 cp_warning_at ("in passing argument %P of `%+D'", parmnum
, fndecl
);
7000 else if (errorcount
> savee
)
7001 cp_error_at ("in passing argument %P of `%+D'", parmnum
, fndecl
);
7006 rhs
= require_complete_type (rhs
);
7007 if (rhs
== error_mark_node
)
7008 return error_mark_node
;
7010 if (exp
!= 0) exp
= require_complete_type (exp
);
7011 if (exp
== error_mark_node
)
7012 return error_mark_node
;
7014 if (TREE_CODE (rhstype
) == REFERENCE_TYPE
)
7015 rhstype
= TREE_TYPE (rhstype
);
7017 if (TYPE_LANG_SPECIFIC (type
)
7018 && (IS_SIGNATURE_POINTER (type
) || IS_SIGNATURE_REFERENCE (type
)))
7019 return build_signature_pointer_constructor (type
, rhs
);
7021 if (IS_AGGR_TYPE (type
)
7022 && (TYPE_NEEDS_CONSTRUCTING (type
) || TREE_HAS_CONSTRUCTOR (rhs
)))
7024 if (TYPE_MAIN_VARIANT (type
) == TYPE_MAIN_VARIANT (rhstype
))
7026 /* This is sufficient to perform initialization. No need,
7027 apparently, to go through X(X&) to do first-cut
7028 initialization. Return through a TARGET_EXPR so that we get
7029 cleanups if it is used. */
7030 if (TREE_CODE (rhs
) == CALL_EXPR
)
7032 rhs
= build_cplus_new (type
, rhs
, 0);
7035 /* Handle the case of default parameter initialization and
7036 initialization of static variables. */
7037 else if (TREE_CODE (rhs
) == TARGET_EXPR
)
7039 else if (TREE_CODE (rhs
) == INDIRECT_REF
&& TREE_HAS_CONSTRUCTOR (rhs
))
7041 my_friendly_assert (TREE_CODE (TREE_OPERAND (rhs
, 0)) == CALL_EXPR
, 318);
7044 my_friendly_assert (TREE_VALUE (TREE_OPERAND (TREE_OPERAND (rhs
, 0), 1)) == NULL_TREE
, 316);
7045 TREE_VALUE (TREE_OPERAND (TREE_OPERAND (rhs
, 0), 1))
7046 = build_unary_op (ADDR_EXPR
, exp
, 0);
7049 rhs
= build_cplus_new (type
, TREE_OPERAND (rhs
, 0), 0);
7052 else if (TYPE_HAS_TRIVIAL_INIT_REF (type
))
7055 if (TYPE_MAIN_VARIANT (type
) == TYPE_MAIN_VARIANT (rhstype
)
7056 || (IS_AGGR_TYPE (rhstype
) && UNIQUELY_DERIVED_FROM_P (type
, rhstype
)))
7058 if (TYPE_HAS_INIT_REF (type
))
7060 tree init
= build_method_call (exp
, constructor_name_full (type
),
7061 build_tree_list (NULL_TREE
, rhs
),
7062 TYPE_BINFO (type
), LOOKUP_NORMAL
);
7064 if (init
== error_mark_node
)
7065 return error_mark_node
;
7069 exp
= build_cplus_new (type
, init
, 0);
7073 return build (COMPOUND_EXPR
, type
, init
, exp
);
7076 /* ??? The following warnings are turned off because
7077 this is another place where the default X(X&) constructor
7079 if (TYPE_HAS_ASSIGNMENT (type
))
7080 cp_warning ("bitwise copy: `%T' defines operator=", type
);
7082 if (TREE_CODE (TREE_TYPE (rhs
)) == REFERENCE_TYPE
)
7083 rhs
= convert_from_reference (rhs
);
7084 if (type
!= rhstype
)
7086 tree nrhs
= build1 (NOP_EXPR
, type
, rhs
);
7087 TREE_CONSTANT (nrhs
) = TREE_CONSTANT (rhs
);
7093 return cp_convert (type
, rhs
, CONV_OLD_CONVERT
, flags
);
7096 if (type
== TREE_TYPE (rhs
))
7098 if (TREE_READONLY_DECL_P (rhs
))
7099 rhs
= decl_constant_value (rhs
);
7103 return convert_for_assignment (type
, rhs
, errtype
, fndecl
, parmnum
);
7106 /* Expand an ASM statement with operands, handling output operands
7107 that are not variables or INDIRECT_REFS by transforming such
7108 cases into cases that expand_asm_operands can handle.
7110 Arguments are same as for expand_asm_operands.
7112 We don't do default conversions on all inputs, because it can screw
7113 up operands that are expected to be in memory. */
7116 c_expand_asm_operands (string
, outputs
, inputs
, clobbers
, vol
, filename
, line
)
7117 tree string
, outputs
, inputs
, clobbers
;
7122 int noutputs
= list_length (outputs
);
7124 /* o[I] is the place that output number I should be written. */
7125 register tree
*o
= (tree
*) alloca (noutputs
* sizeof (tree
));
7128 /* Record the contents of OUTPUTS before it is modified. */
7129 for (i
= 0, tail
= outputs
; tail
; tail
= TREE_CHAIN (tail
), i
++)
7130 o
[i
] = TREE_VALUE (tail
);
7132 /* Generate the ASM_OPERANDS insn;
7133 store into the TREE_VALUEs of OUTPUTS some trees for
7134 where the values were actually stored. */
7135 expand_asm_operands (string
, outputs
, inputs
, clobbers
, vol
, filename
, line
);
7137 /* Copy all the intermediate outputs into the specified outputs. */
7138 for (i
= 0, tail
= outputs
; tail
; tail
= TREE_CHAIN (tail
), i
++)
7140 if (o
[i
] != TREE_VALUE (tail
))
7142 expand_expr (build_modify_expr (o
[i
], NOP_EXPR
, TREE_VALUE (tail
)),
7143 const0_rtx
, VOIDmode
, 0);
7146 /* Detect modification of read-only values.
7147 (Otherwise done by build_modify_expr.) */
7150 tree type
= TREE_TYPE (o
[i
]);
7151 if (TYPE_READONLY (type
)
7152 || ((TREE_CODE (type
) == RECORD_TYPE
7153 || TREE_CODE (type
) == UNION_TYPE
)
7154 && C_TYPE_FIELDS_READONLY (type
)))
7155 readonly_error (o
[i
], "modification by `asm'", 1);
7159 /* Those MODIFY_EXPRs could do autoincrements. */
7163 /* Expand a C `return' statement.
7164 RETVAL is the expression for what to return,
7165 or a null pointer for `return;' with no value.
7167 C++: upon seeing a `return', we must call destructors on all
7168 variables in scope which had constructors called on them.
7169 This means that if in a destructor, the base class destructors
7170 must be called before returning.
7172 The RETURN statement in C++ has initialization semantics. */
7175 c_expand_return (retval
)
7178 extern struct nesting
*cond_stack
, *loop_stack
, *case_stack
;
7179 extern tree dtor_label
, ctor_label
;
7180 tree result
= DECL_RESULT (current_function_decl
);
7181 tree valtype
= TREE_TYPE (result
);
7182 register int use_temp
= 0;
7183 int returns_value
= 1;
7185 if (TREE_THIS_VOLATILE (current_function_decl
))
7186 warning ("function declared `noreturn' has a `return' statement");
7188 if (retval
== error_mark_node
)
7190 current_function_returns_null
= 1;
7194 if (retval
== NULL_TREE
)
7196 /* A non-named return value does not count. */
7198 /* Can't just return from a destructor. */
7201 expand_goto (dtor_label
);
7205 if (DECL_CONSTRUCTOR_P (current_function_decl
))
7206 retval
= current_class_decl
;
7207 else if (DECL_NAME (result
) != NULL_TREE
7208 && TREE_CODE (valtype
) != VOID_TYPE
)
7212 current_function_returns_null
= 1;
7214 if (valtype
!= NULL_TREE
&& TREE_CODE (valtype
) != VOID_TYPE
)
7216 if (DECL_NAME (DECL_RESULT (current_function_decl
)) == NULL_TREE
)
7218 pedwarn ("`return' with no value, in function returning non-void");
7219 /* Clear this, so finish_function won't say that we
7220 reach the end of a non-void function (which we don't,
7221 we gave a return!). */
7222 current_function_returns_null
= 0;
7226 expand_null_return ();
7230 else if (DECL_CONSTRUCTOR_P (current_function_decl
)
7231 && retval
!= current_class_decl
)
7233 error ("return from a constructor: use `this = ...' instead");
7234 retval
= current_class_decl
;
7237 if (valtype
== NULL_TREE
|| TREE_CODE (valtype
) == VOID_TYPE
)
7239 current_function_returns_null
= 1;
7240 /* We do this here so we'll avoid a warning about how the function
7241 "may or may not return a value" in finish_function. */
7245 pedwarn ("`return' with a value, in function returning void");
7246 expand_return (retval
);
7248 /* Add some useful error checking for C++. */
7249 else if (TREE_CODE (valtype
) == REFERENCE_TYPE
)
7251 tree whats_returned
;
7252 tree tmp_result
= result
;
7254 /* Don't initialize directly into a non-BLKmode retval, since that
7255 could lose when being inlined by another caller. (GCC can't
7256 read the function return register in an inline function when
7257 the return value is being ignored). */
7258 if (result
&& TYPE_MODE (TREE_TYPE (tmp_result
)) != BLKmode
)
7261 /* convert to reference now, so we can give error if we
7262 return an reference to a non-lvalue. */
7263 retval
= convert_for_initialization (tmp_result
, valtype
, retval
,
7264 LOOKUP_NORMAL
, "return",
7267 /* Sort through common things to see what it is
7268 we are returning. */
7269 whats_returned
= retval
;
7270 if (TREE_CODE (whats_returned
) == COMPOUND_EXPR
)
7272 whats_returned
= TREE_OPERAND (whats_returned
, 1);
7273 if (TREE_CODE (whats_returned
) == ADDR_EXPR
)
7274 whats_returned
= TREE_OPERAND (whats_returned
, 0);
7276 if (TREE_CODE (whats_returned
) == ADDR_EXPR
)
7278 whats_returned
= TREE_OPERAND (whats_returned
, 0);
7279 while (TREE_CODE (whats_returned
) == NEW_EXPR
7280 || TREE_CODE (whats_returned
) == TARGET_EXPR
7281 || TREE_CODE (whats_returned
) == WITH_CLEANUP_EXPR
)
7283 /* Get the target. */
7284 whats_returned
= TREE_OPERAND (whats_returned
, 0);
7285 warning ("returning reference to temporary");
7289 if (TREE_CODE (whats_returned
) == VAR_DECL
&& DECL_NAME (whats_returned
))
7291 if (TEMP_NAME_P (DECL_NAME (whats_returned
)))
7292 warning ("reference to non-lvalue returned");
7293 else if (! TREE_STATIC (whats_returned
)
7294 && IDENTIFIER_LOCAL_VALUE (DECL_NAME (whats_returned
)))
7295 cp_warning_at ("reference to local variable `%D' returned", whats_returned
);
7298 else if (TREE_CODE (retval
) == ADDR_EXPR
)
7300 tree whats_returned
= TREE_OPERAND (retval
, 0);
7302 if (TREE_CODE (whats_returned
) == VAR_DECL
7303 && DECL_NAME (whats_returned
)
7304 && IDENTIFIER_LOCAL_VALUE (DECL_NAME (whats_returned
))
7305 && !TREE_STATIC (whats_returned
))
7306 cp_warning_at ("address of local variable `%D' returned", whats_returned
);
7309 /* Now deal with possible C++ hair:
7310 (1) Compute the return value.
7311 (2) If there are aggregate values with destructors which
7312 must be cleaned up, clean them (taking care
7313 not to clobber the return value).
7314 (3) If an X(X&) constructor is defined, the return
7315 value must be returned via that. */
7317 /* If we're returning in a register, we can't initialize the
7318 return value from a TARGET_EXPR. */
7319 if (TREE_CODE (retval
) == TARGET_EXPR
7320 && TYPE_MAIN_VARIANT (TREE_TYPE (retval
)) == TYPE_MAIN_VARIANT (valtype
)
7321 && ! current_function_returns_struct
)
7322 retval
= expand_target_expr (retval
);
7324 if (retval
== result
7325 /* Watch out for constructors, which "return" aggregates
7326 via initialization, but which otherwise "return" a pointer. */
7327 || DECL_CONSTRUCTOR_P (current_function_decl
))
7329 /* This is just an error--it's already been reported. */
7330 if (TYPE_SIZE (valtype
) == NULL_TREE
)
7333 if (TYPE_MODE (valtype
) != BLKmode
7334 && any_pending_cleanups (1))
7336 retval
= get_temp_regvar (valtype
, retval
);
7337 use_temp
= obey_regdecls
;
7340 else if (IS_AGGR_TYPE (valtype
) && current_function_returns_struct
)
7342 expand_aggr_init (result
, retval
, 0, LOOKUP_ONLYCONVERTING
);
7343 expand_cleanups_to (NULL_TREE
);
7344 DECL_INITIAL (result
) = NULL_TREE
;
7349 if (TYPE_MODE (valtype
) == VOIDmode
)
7351 if (TYPE_MODE (TREE_TYPE (result
)) != VOIDmode
7352 && warn_return_type
)
7353 warning ("return of void value in function returning non-void");
7354 expand_expr_stmt (retval
);
7358 else if (TYPE_MODE (valtype
) != BLKmode
7359 && any_pending_cleanups (1))
7361 retval
= get_temp_regvar (valtype
, retval
);
7362 expand_cleanups_to (NULL_TREE
);
7363 use_temp
= obey_regdecls
;
7368 retval
= convert_for_initialization (result
, valtype
, retval
,
7370 "return", NULL_TREE
, 0);
7371 DECL_INITIAL (result
) = NULL_TREE
;
7373 if (retval
== error_mark_node
)
7379 if (retval
!= NULL_TREE
7380 && TREE_CODE_CLASS (TREE_CODE (retval
)) == 'd'
7381 && cond_stack
== 0 && loop_stack
== 0 && case_stack
== 0)
7382 current_function_return_value
= retval
;
7386 /* Everything's great--RETVAL is in RESULT. */
7387 if (original_result_rtx
)
7389 store_expr (result
, original_result_rtx
, 0);
7390 expand_cleanups_to (NULL_TREE
);
7392 else if (retval
&& retval
!= result
)
7394 /* Clear this out so the later call to decl_function_context
7395 won't end up bombing on us. */
7396 if (DECL_CONTEXT (result
) == error_mark_node
)
7397 DECL_CONTEXT (result
) = NULL_TREE
;
7398 /* Here is where we finally get RETVAL into RESULT.
7399 `expand_return' does the magic of protecting
7400 RESULT from cleanups. */
7401 retval
= fold (build1 (CLEANUP_POINT_EXPR
, TREE_TYPE (result
),
7403 /* This part _must_ come second, because expand_return looks for
7404 the INIT_EXPR as the toplevel node only. :-( */
7405 retval
= build (INIT_EXPR
, TREE_TYPE (result
), result
, retval
);
7406 TREE_SIDE_EFFECTS (retval
) = 1;
7407 expand_return (retval
);
7410 expand_return (result
);
7412 use_variable (DECL_RTL (result
));
7413 if (ctor_label
&& TREE_CODE (ctor_label
) != ERROR_MARK
)
7414 expand_goto (ctor_label
);
7416 expand_null_return ();
7420 /* We may still need to put RETVAL into RESULT. */
7421 result
= DECL_RESULT (current_function_decl
);
7422 if (original_result_rtx
)
7424 /* Here we have a named return value that went
7425 into memory. We can compute RETVAL into that. */
7427 expand_assignment (result
, retval
, 0, 0);
7429 store_expr (result
, original_result_rtx
, 0);
7430 result
= make_tree (TREE_TYPE (result
), original_result_rtx
);
7432 else if (ctor_label
&& TREE_CODE (ctor_label
) != ERROR_MARK
)
7434 /* Here RETVAL is CURRENT_CLASS_DECL, so there's nothing to do. */
7435 expand_goto (ctor_label
);
7439 /* Here is where we finally get RETVAL into RESULT.
7440 `expand_return' does the magic of protecting
7441 RESULT from cleanups. */
7442 result
= build (INIT_EXPR
, TREE_TYPE (result
), result
, retval
);
7443 TREE_SIDE_EFFECTS (result
) = 1;
7444 expand_return (result
);
7446 else if (TYPE_MODE (TREE_TYPE (result
)) != VOIDmode
)
7447 expand_return (result
);
7450 current_function_returns_value
= returns_value
;
7452 /* These wind up after the BARRIER, which causes problems for
7453 expand_end_binding. What purpose were they supposed to serve? */
7454 if (original_result_rtx
)
7455 use_variable (original_result_rtx
);
7457 use_variable (DECL_RTL (DECL_RESULT (current_function_decl
)));
7460 /* One way to clear out cleanups that EXPR might
7461 generate. Note that this code will really be
7462 dead code, but that is ok--cleanups that were
7463 needed were handled by the magic of `return'. */
7464 expand_cleanups_to (NULL_TREE
);
7467 /* Start a C switch statement, testing expression EXP.
7468 Return EXP if it is valid, an error node otherwise. */
7471 c_expand_start_case (exp
)
7475 register enum tree_code code
;
7477 /* Convert from references, etc. */
7478 exp
= default_conversion (exp
);
7479 type
= TREE_TYPE (exp
);
7480 code
= TREE_CODE (type
);
7482 if (IS_AGGR_TYPE_CODE (code
))
7483 exp
= build_type_conversion (CONVERT_EXPR
, integer_type_node
, exp
, 1);
7485 if (exp
== NULL_TREE
)
7487 error ("switch quantity not an integer");
7488 exp
= error_mark_node
;
7490 type
= TREE_TYPE (exp
);
7491 code
= TREE_CODE (type
);
7493 if (code
!= INTEGER_TYPE
&& code
!= ENUMERAL_TYPE
&& code
!= ERROR_MARK
)
7495 error ("switch quantity not an integer");
7496 exp
= error_mark_node
;
7502 exp
= default_conversion (exp
);
7503 type
= TREE_TYPE (exp
);
7504 index
= get_unwidened (exp
, 0);
7505 /* We can't strip a conversion from a signed type to an unsigned,
7506 because if we did, int_fits_type_p would do the wrong thing
7507 when checking case values for being in range,
7508 and it's too hard to do the right thing. */
7509 if (TREE_UNSIGNED (TREE_TYPE (exp
))
7510 == TREE_UNSIGNED (TREE_TYPE (index
)))
7515 (1, fold (build1 (CLEANUP_POINT_EXPR
, TREE_TYPE (exp
), exp
)),
7516 type
, "switch statement");
7521 /* CONSTP remembers whether or not all the intervening pointers in the `to'
7522 type have been const. */
7524 comp_ptr_ttypes_real (to
, from
, constp
)
7528 for (; ; to
= TREE_TYPE (to
), from
= TREE_TYPE (from
))
7530 if (TREE_CODE (to
) != TREE_CODE (from
))
7533 if (TYPE_READONLY (from
) > TYPE_READONLY (to
)
7534 || TYPE_VOLATILE (from
) > TYPE_VOLATILE (to
))
7538 && (TYPE_READONLY (to
) > TYPE_READONLY (from
)
7539 || TYPE_VOLATILE (to
) > TYPE_READONLY (from
)))
7541 constp
&= TYPE_READONLY (to
);
7543 if (TREE_CODE (to
) != POINTER_TYPE
)
7544 return comptypes (TYPE_MAIN_VARIANT (to
), TYPE_MAIN_VARIANT (from
), 1);
7548 /* When comparing, say, char ** to char const **, this function takes the
7549 'char *' and 'char const *'. Do not pass non-pointer types to this
7552 comp_ptr_ttypes (to
, from
)
7555 return comp_ptr_ttypes_real (to
, from
, 1);