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
);
403 tree elt
= common_type (TREE_TYPE (t1
), TREE_TYPE (t2
));
404 /* Save space: see if the result is identical to one of the args. */
405 if (elt
== TREE_TYPE (t1
) && TYPE_DOMAIN (t1
))
406 return build_type_attribute_variant (t1
, attributes
);
407 if (elt
== TREE_TYPE (t2
) && TYPE_DOMAIN (t2
))
408 return build_type_attribute_variant (t2
, attributes
);
409 /* Merge the element types, and have a size if either arg has one. */
410 t1
= build_cplus_array_type (elt
, TYPE_DOMAIN (TYPE_DOMAIN (t1
) ? t1
: t2
));
411 return build_type_attribute_variant (t1
, attributes
);
415 /* Function types: prefer the one that specified arg types.
416 If both do, merge the arg types. Also merge the return types. */
418 tree valtype
= common_type (TREE_TYPE (t1
), TREE_TYPE (t2
));
419 tree p1
= TYPE_ARG_TYPES (t1
);
420 tree p2
= TYPE_ARG_TYPES (t2
);
423 /* Save space: see if the result is identical to one of the args. */
424 if (valtype
== TREE_TYPE (t1
) && ! p2
)
425 return build_type_attribute_variant (t1
, attributes
);
426 if (valtype
== TREE_TYPE (t2
) && ! p1
)
427 return build_type_attribute_variant (t2
, attributes
);
429 /* Simple way if one arg fails to specify argument types. */
430 if (p1
== NULL_TREE
|| TREE_VALUE (p1
) == void_type_node
)
432 rval
= build_function_type (valtype
, p2
);
433 if ((raises
= TYPE_RAISES_EXCEPTIONS (t2
)))
434 rval
= build_exception_variant (rval
, raises
);
435 return build_type_attribute_variant (rval
, attributes
);
437 raises
= TYPE_RAISES_EXCEPTIONS (t1
);
438 if (p2
== NULL_TREE
|| TREE_VALUE (p2
) == void_type_node
)
440 rval
= build_function_type (valtype
, p1
);
442 rval
= build_exception_variant (rval
, raises
);
443 return build_type_attribute_variant (rval
, attributes
);
446 rval
= build_function_type (valtype
, commonparms (p1
, p2
));
447 rval
= build_exception_variant (rval
, raises
);
448 return build_type_attribute_variant (rval
, attributes
);
453 my_friendly_assert (TYPE_MAIN_VARIANT (t1
) == t1
454 && TYPE_MAIN_VARIANT (t2
) == t2
, 306);
456 if (DERIVED_FROM_P (t1
, t2
) && binfo_or_else (t1
, t2
))
457 return build_type_attribute_variant (t1
, attributes
);
458 else if (binfo_or_else (t2
, t1
))
459 return build_type_attribute_variant (t2
, attributes
);
461 compiler_error ("common_type called with uncommon aggregate types");
464 if (TREE_CODE (TREE_TYPE (t1
)) == TREE_CODE (TREE_TYPE (t2
)))
466 /* Get this value the long way, since TYPE_METHOD_BASETYPE
467 is just the main variant of this. */
471 tree b1
= TYPE_OFFSET_BASETYPE (t1
);
472 tree b2
= TYPE_OFFSET_BASETYPE (t2
);
474 if (DERIVED_FROM_P (b1
, b2
) && binfo_or_else (b1
, b2
))
475 basetype
= TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (t2
)));
478 if (binfo_or_else (b2
, b1
) == NULL_TREE
)
479 compiler_error ("common_type called with uncommon method types");
480 basetype
= TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (t1
)));
483 raises
= TYPE_RAISES_EXCEPTIONS (t1
);
485 /* If this was a member function type, get back to the
486 original type of type member function (i.e., without
487 the class instance variable up front. */
488 t1
= build_function_type (TREE_TYPE (t1
), TREE_CHAIN (TYPE_ARG_TYPES (t1
)));
489 t2
= build_function_type (TREE_TYPE (t2
), TREE_CHAIN (TYPE_ARG_TYPES (t2
)));
490 t3
= common_type (t1
, t2
);
491 t3
= build_cplus_method_type (basetype
, TREE_TYPE (t3
), TYPE_ARG_TYPES (t3
));
492 t1
= build_exception_variant (t3
, raises
);
495 compiler_error ("common_type called with uncommon method types");
497 return build_type_attribute_variant (t1
, attributes
);
500 if (TREE_TYPE (t1
) == TREE_TYPE (t2
))
502 tree b1
= TYPE_OFFSET_BASETYPE (t1
);
503 tree b2
= TYPE_OFFSET_BASETYPE (t2
);
505 if (DERIVED_FROM_P (b1
, b2
) && binfo_or_else (b1
, b2
))
506 return build_type_attribute_variant (t2
, attributes
);
507 else if (binfo_or_else (b2
, b1
))
508 return build_type_attribute_variant (t1
, attributes
);
510 compiler_error ("common_type called with uncommon member types");
513 return build_type_attribute_variant (t1
, attributes
);
517 /* Return 1 if TYPE1 and TYPE2 raise the same exceptions. */
519 compexcepttypes (t1
, t2
, strict
)
523 return TYPE_RAISES_EXCEPTIONS (t1
) == TYPE_RAISES_EXCEPTIONS (t2
);
527 comp_array_types (cmp
, t1
, t2
, strict
)
528 register int (*cmp
)();
532 tree d1
= TYPE_DOMAIN (t1
);
533 tree d2
= TYPE_DOMAIN (t2
);
535 /* Target types must match incl. qualifiers. */
536 if (!(TREE_TYPE (t1
) == TREE_TYPE (t2
)
537 || (*cmp
) (TREE_TYPE (t1
), TREE_TYPE (t2
), strict
)))
540 /* Sizes must match unless one is missing or variable. */
541 if (d1
== 0 || d2
== 0 || d1
== d2
542 || TREE_CODE (TYPE_MIN_VALUE (d1
)) != INTEGER_CST
543 || TREE_CODE (TYPE_MIN_VALUE (d2
)) != INTEGER_CST
544 || TREE_CODE (TYPE_MAX_VALUE (d1
)) != INTEGER_CST
545 || TREE_CODE (TYPE_MAX_VALUE (d2
)) != INTEGER_CST
)
548 return ((TREE_INT_CST_LOW (TYPE_MIN_VALUE (d1
))
549 == TREE_INT_CST_LOW (TYPE_MIN_VALUE (d2
)))
550 && (TREE_INT_CST_HIGH (TYPE_MIN_VALUE (d1
))
551 == TREE_INT_CST_HIGH (TYPE_MIN_VALUE (d2
)))
552 && (TREE_INT_CST_LOW (TYPE_MAX_VALUE (d1
))
553 == TREE_INT_CST_LOW (TYPE_MAX_VALUE (d2
)))
554 && (TREE_INT_CST_HIGH (TYPE_MAX_VALUE (d1
))
555 == TREE_INT_CST_HIGH (TYPE_MAX_VALUE (d2
))));
558 /* Return 1 if TYPE1 and TYPE2 are compatible types for assignment
559 or various other operations. This is what ANSI C++ speaks of as
562 For C++: argument STRICT says we should be strict about this
565 2 : strict, except that if one type is a reference and
566 the other is not, compare the target type of the
567 reference to the type that's not a reference (ARM, p308).
568 This is used for checking for invalid overloading.
569 1 : strict (compared according to ANSI C)
570 This is used for checking whether two function decls match.
571 0 : <= (compared according to C++)
572 -1: <= or >= (relaxed)
574 Otherwise, pointers involving base classes and derived classes
575 can be mixed as valid: i.e. a pointer to a base class may be assigned
576 to a pointer to one of its derived classes, as per C++. A pointer to
577 a derived class may be passed as a parameter to a function expecting a
578 pointer to a base classes. These allowances do not commute. In this
579 case, TYPE1 is assumed to be the base class, and TYPE2 is assumed to
580 be the derived class. */
582 comptypes (type1
, type2
, strict
)
586 register tree t1
= type1
;
587 register tree t2
= type2
;
590 /* Suppress errors caused by previously reported errors */
595 /* This should never happen. */
596 my_friendly_assert (t1
!= error_mark_node
, 307);
598 if (t2
== error_mark_node
)
603 /* Treat an enum type as the unsigned integer type of the same width. */
605 if (TREE_CODE (t1
) == ENUMERAL_TYPE
)
606 t1
= type_for_size (TYPE_PRECISION (t1
), 1);
607 if (TREE_CODE (t2
) == ENUMERAL_TYPE
)
608 t2
= type_for_size (TYPE_PRECISION (t2
), 1);
614 /* Different classes of types can't be compatible. */
616 if (TREE_CODE (t1
) != TREE_CODE (t2
))
619 && ((TREE_CODE (t1
) == REFERENCE_TYPE
)
620 ^ (TREE_CODE (t2
) == REFERENCE_TYPE
)))
622 if (TREE_CODE (t1
) == REFERENCE_TYPE
)
623 return comptypes (TREE_TYPE (t1
), t2
, 1);
624 return comptypes (t1
, TREE_TYPE (t2
), 1);
632 /* Qualifiers must match. */
634 if (TYPE_READONLY (t1
) != TYPE_READONLY (t2
))
636 if (TYPE_VOLATILE (t1
) != TYPE_VOLATILE (t2
))
639 /* Allow for two different type nodes which have essentially the same
640 definition. Note that we already checked for equality of the type
641 type qualifiers (just above). */
643 if (TYPE_MAIN_VARIANT (t1
) == TYPE_MAIN_VARIANT (t2
))
646 /* ??? COMP_TYPE_ATTRIBUTES is currently useless for variables as each
647 attribute is its own main variant (`val' will remain 0). */
648 #ifndef COMP_TYPE_ATTRIBUTES
649 #define COMP_TYPE_ATTRIBUTES(t1,t2) 1
652 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
653 if (! (attrval
= COMP_TYPE_ATTRIBUTES (t1
, t2
)))
656 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
659 switch (TREE_CODE (t1
))
668 val
= (comptypes (build_pointer_type (TYPE_OFFSET_BASETYPE (t1
)),
669 build_pointer_type (TYPE_OFFSET_BASETYPE (t2
)), strict
)
670 && comptypes (TREE_TYPE (t1
), TREE_TYPE (t2
), strict
));
674 if (! compexcepttypes (t1
, t2
, strict
))
677 /* This case is anti-symmetrical!
678 One can pass a base member (or member function)
679 to something expecting a derived member (or member function),
680 but not vice-versa! */
682 val
= (comptypes (TREE_TYPE (t1
), TREE_TYPE (t2
), strict
)
683 && compparms (TYPE_ARG_TYPES (t1
),
684 TYPE_ARG_TYPES (t2
), strict
));
698 if (TREE_CODE (t1
) == RECORD_TYPE
&& TREE_CODE (t2
) == RECORD_TYPE
)
702 rval
= t1
== t2
|| UNIQUELY_DERIVED_FROM_P (t1
, t2
);
711 val
= UNIQUELY_DERIVED_FROM_P (t2
, t1
);
718 val
= comptypes (t1
, t2
, strict
);
722 if (! compexcepttypes (t1
, t2
, strict
))
725 val
= ((TREE_TYPE (t1
) == TREE_TYPE (t2
)
726 || comptypes (TREE_TYPE (t1
), TREE_TYPE (t2
), strict
))
727 && compparms (TYPE_ARG_TYPES (t1
), TYPE_ARG_TYPES (t2
), strict
));
731 /* Target types must match incl. qualifiers. */
732 val
= comp_array_types (comptypes
, t1
, t2
, strict
);
735 case TEMPLATE_TYPE_PARM
:
736 return TEMPLATE_TYPE_IDX (t1
) == TEMPLATE_TYPE_IDX (t2
);
738 case UNINSTANTIATED_P_TYPE
:
739 if (UPT_TEMPLATE (t1
) != UPT_TEMPLATE (t2
))
742 int i
= TREE_VEC_LENGTH (UPT_PARMS (t1
));
743 tree
*p1
= &TREE_VEC_ELT (UPT_PARMS (t1
), 0);
744 tree
*p2
= &TREE_VEC_ELT (UPT_PARMS (t2
), 0);
748 if (TREE_CODE_CLASS (TREE_CODE (p1
[i
])) == 't')
750 if (! comptypes (p1
[i
], p2
[i
], 1))
755 if (simple_cst_equal (p1
[i
], p2
[i
]) <= 0)
762 return attrval
== 2 && val
== 1 ? 2 : val
;
765 /* Return 1 if TTL and TTR are pointers to types that are equivalent,
766 ignoring their qualifiers.
768 NPTRS is the number of pointers we can strip off and keep cool.
769 This is used to permit (for aggr A, aggr B) A, B* to convert to A*,
770 but to not permit B** to convert to A**. */
773 comp_target_types (ttl
, ttr
, nptrs
)
777 ttl
= TYPE_MAIN_VARIANT (ttl
);
778 ttr
= TYPE_MAIN_VARIANT (ttr
);
782 if (TREE_CODE (ttr
) != TREE_CODE (ttl
))
785 if (TREE_CODE (ttr
) == POINTER_TYPE
)
787 ttl
= TREE_TYPE (ttl
);
788 ttr
= TREE_TYPE (ttr
);
792 if (TREE_CODE (ttl
) == VOID_TYPE
793 && TREE_CODE (ttr
) != FUNCTION_TYPE
794 && TREE_CODE (ttr
) != METHOD_TYPE
795 && TREE_CODE (ttr
) != OFFSET_TYPE
)
797 else if (TREE_CODE (ttr
) == VOID_TYPE
798 && TREE_CODE (ttl
) != FUNCTION_TYPE
799 && TREE_CODE (ttl
) != METHOD_TYPE
800 && TREE_CODE (ttl
) != OFFSET_TYPE
)
802 else if (TREE_CODE (ttl
) == POINTER_TYPE
803 || TREE_CODE (ttl
) == ARRAY_TYPE
)
805 if (comp_ptr_ttypes (ttl
, ttr
))
807 else if (comp_ptr_ttypes (ttr
, ttl
))
813 /* Const and volatile mean something different for function types,
814 so the usual checks are not appropriate. */
815 if (TREE_CODE (ttl
) == FUNCTION_TYPE
|| TREE_CODE (ttl
) == METHOD_TYPE
)
816 return comp_target_types (ttl
, ttr
, nptrs
- 1);
818 /* Make sure that the cv-quals change only in the same direction as
822 int c
= TYPE_READONLY (ttl
) - TYPE_READONLY (ttr
);
823 int v
= TYPE_VOLATILE (ttl
) - TYPE_VOLATILE (ttr
);
825 if ((c
> 0 && v
< 0) || (c
< 0 && v
> 0))
828 if (TYPE_MAIN_VARIANT (ttl
) == TYPE_MAIN_VARIANT (ttr
))
829 return (c
+ v
< 0) ? -1 : 1;
831 t
= comp_target_types (ttl
, ttr
, nptrs
- 1);
832 if ((t
== 1 && c
+ v
>= 0) || (t
== -1 && c
+ v
<= 0))
839 if (TREE_CODE (ttr
) == REFERENCE_TYPE
)
840 return comp_target_types (TREE_TYPE (ttl
), TREE_TYPE (ttr
), nptrs
);
841 if (TREE_CODE (ttr
) == ARRAY_TYPE
)
842 return comp_array_types (comp_target_types
, ttl
, ttr
, 0);
843 else if (TREE_CODE (ttr
) == FUNCTION_TYPE
|| TREE_CODE (ttr
) == METHOD_TYPE
)
844 if (comp_target_types (TREE_TYPE (ttl
), TREE_TYPE (ttr
), nptrs
))
845 switch (comp_target_parms (TYPE_ARG_TYPES (ttl
), TYPE_ARG_TYPES (ttr
), 1))
854 my_friendly_abort (112);
860 else if (TREE_CODE (ttr
) == OFFSET_TYPE
)
862 /* Contravariance: we can assign a pointer to base member to a pointer
863 to derived member. Note difference from simple pointer case, where
864 we can pass a pointer to derived to a pointer to base. */
865 if (comptypes (TYPE_OFFSET_BASETYPE (ttr
), TYPE_OFFSET_BASETYPE (ttl
), 0))
866 return comp_target_types (TREE_TYPE (ttl
), TREE_TYPE (ttr
), nptrs
);
867 else if (comptypes (TYPE_OFFSET_BASETYPE (ttl
), TYPE_OFFSET_BASETYPE (ttr
), 0)
868 && comp_target_types (TREE_TYPE (ttl
), TREE_TYPE (ttr
), nptrs
))
871 else if (IS_AGGR_TYPE (ttl
))
875 if (comptypes (build_pointer_type (ttl
), build_pointer_type (ttr
), 0))
877 if (comptypes (build_pointer_type (ttr
), build_pointer_type (ttl
), 0))
885 /* If two types share a common base type, return that basetype.
886 If there is not a unique most-derived base type, this function
887 returns ERROR_MARK_NODE. */
889 common_base_type (tt1
, tt2
)
892 tree best
= NULL_TREE
, tmp
;
895 /* If one is a baseclass of another, that's good enough. */
896 if (UNIQUELY_DERIVED_FROM_P (tt1
, tt2
))
898 if (UNIQUELY_DERIVED_FROM_P (tt2
, tt1
))
901 /* Otherwise, try to find a unique baseclass of TT1
902 that is shared by TT2, and follow that down. */
903 for (i
= CLASSTYPE_N_BASECLASSES (tt1
)-1; i
>= 0; i
--)
905 tree basetype
= TYPE_BINFO_BASETYPE (tt1
, i
);
906 tree trial
= common_base_type (basetype
, tt2
);
909 if (trial
== error_mark_node
)
911 if (best
== NULL_TREE
)
913 else if (best
!= trial
)
914 return error_mark_node
;
919 for (i
= CLASSTYPE_N_BASECLASSES (tt2
)-1; i
>= 0; i
--)
921 tree basetype
= TYPE_BINFO_BASETYPE (tt2
, i
);
922 tree trial
= common_base_type (tt1
, basetype
);
925 if (trial
== error_mark_node
)
927 if (best
== NULL_TREE
)
929 else if (best
!= trial
)
930 return error_mark_node
;
936 /* Subroutines of `comptypes'. */
938 /* Return 1 if two parameter type lists PARMS1 and PARMS2
939 are equivalent in the sense that functions with those parameter types
940 can have equivalent types.
941 If either list is empty, we win.
942 Otherwise, the two lists must be equivalent, element by element.
944 C++: See comment above about TYPE1, TYPE2, STRICT.
945 If STRICT == 3, it means checking is strict, but do not compare
946 default parameter values. */
948 compparms (parms1
, parms2
, strict
)
952 register tree t1
= parms1
, t2
= parms2
;
954 /* An unspecified parmlist matches any specified parmlist
955 whose argument types don't need default promotions. */
957 if (strict
<= 0 && t1
== 0)
958 return self_promoting_args_p (t2
);
959 if (strict
< 0 && t2
== 0)
960 return self_promoting_args_p (t1
);
964 if (t1
== 0 && t2
== 0)
966 /* If one parmlist is shorter than the other,
967 they fail to match, unless STRICT is <= 0. */
968 if (t1
== 0 || t2
== 0)
975 return t1
&& TREE_PURPOSE (t1
);
977 if (! comptypes (TREE_VALUE (t2
), TREE_VALUE (t1
), strict
))
982 return t2
== void_list_node
&& TREE_PURPOSE (t1
);
983 return TREE_PURPOSE (t1
) || TREE_PURPOSE (t2
);
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 /* Target types are compatible--just make sure that if
1074 we use parameter lists, that they are ok as well. */
1075 if (TREE_CODE (p1
) == FUNCTION_TYPE
|| TREE_CODE (p1
) == METHOD_TYPE
)
1076 switch (comp_target_parms (TYPE_ARG_TYPES (p1
),
1077 TYPE_ARG_TYPES (p2
),
1085 warn_contravariance
= 1;
1088 if (TREE_PURPOSE (t1
) && TREE_PURPOSE (t2
))
1090 int cmp
= simple_cst_equal (TREE_PURPOSE (t1
), TREE_PURPOSE (t2
));
1092 my_friendly_abort (114);
1097 return 1 + warn_contravariance
;
1100 /* Return 1 if PARMS specifies a fixed number of parameters
1101 and none of their types is affected by default promotions. */
1104 self_promoting_args_p (parms
)
1108 for (t
= parms
; t
; t
= TREE_CHAIN (t
))
1110 register tree type
= TREE_VALUE (t
);
1112 if (TREE_CHAIN (t
) == 0 && type
!= void_type_node
)
1118 if (TYPE_MAIN_VARIANT (type
) == float_type_node
)
1121 if (C_PROMOTING_INTEGER_TYPE_P (type
))
1127 /* Return an unsigned type the same as TYPE in other respects.
1129 C++: must make these work for type variants as well. */
1132 unsigned_type (type
)
1135 tree type1
= TYPE_MAIN_VARIANT (type
);
1136 if (type1
== signed_char_type_node
|| type1
== char_type_node
)
1137 return unsigned_char_type_node
;
1138 if (type1
== integer_type_node
)
1139 return unsigned_type_node
;
1140 if (type1
== short_integer_type_node
)
1141 return short_unsigned_type_node
;
1142 if (type1
== long_integer_type_node
)
1143 return long_unsigned_type_node
;
1144 if (type1
== long_long_integer_type_node
)
1145 return long_long_unsigned_type_node
;
1146 if (type1
== intDI_type_node
)
1147 return unsigned_intDI_type_node
;
1148 if (type1
== intSI_type_node
)
1149 return unsigned_intSI_type_node
;
1150 if (type1
== intHI_type_node
)
1151 return unsigned_intHI_type_node
;
1152 if (type1
== intQI_type_node
)
1153 return unsigned_intQI_type_node
;
1157 /* Return a signed type the same as TYPE in other respects. */
1163 tree type1
= TYPE_MAIN_VARIANT (type
);
1164 if (type1
== unsigned_char_type_node
|| type1
== char_type_node
)
1165 return signed_char_type_node
;
1166 if (type1
== unsigned_type_node
)
1167 return integer_type_node
;
1168 if (type1
== short_unsigned_type_node
)
1169 return short_integer_type_node
;
1170 if (type1
== long_unsigned_type_node
)
1171 return long_integer_type_node
;
1172 if (type1
== long_long_unsigned_type_node
)
1173 return long_long_integer_type_node
;
1174 if (type1
== unsigned_intDI_type_node
)
1175 return intDI_type_node
;
1176 if (type1
== unsigned_intSI_type_node
)
1177 return intSI_type_node
;
1178 if (type1
== unsigned_intHI_type_node
)
1179 return intHI_type_node
;
1180 if (type1
== unsigned_intQI_type_node
)
1181 return intQI_type_node
;
1185 /* Return a type the same as TYPE except unsigned or
1186 signed according to UNSIGNEDP. */
1189 signed_or_unsigned_type (unsignedp
, type
)
1193 if (! INTEGRAL_TYPE_P (type
))
1195 if (TYPE_PRECISION (type
) == TYPE_PRECISION (signed_char_type_node
))
1196 return unsignedp
? unsigned_char_type_node
: signed_char_type_node
;
1197 if (TYPE_PRECISION (type
) == TYPE_PRECISION (integer_type_node
))
1198 return unsignedp
? unsigned_type_node
: integer_type_node
;
1199 if (TYPE_PRECISION (type
) == TYPE_PRECISION (short_integer_type_node
))
1200 return unsignedp
? short_unsigned_type_node
: short_integer_type_node
;
1201 if (TYPE_PRECISION (type
) == TYPE_PRECISION (long_integer_type_node
))
1202 return unsignedp
? long_unsigned_type_node
: long_integer_type_node
;
1203 if (TYPE_PRECISION (type
) == TYPE_PRECISION (long_long_integer_type_node
))
1204 return (unsignedp
? long_long_unsigned_type_node
1205 : long_long_integer_type_node
);
1209 /* Compute the value of the `sizeof' operator. */
1215 enum tree_code code
= TREE_CODE (type
);
1218 if (code
== FUNCTION_TYPE
)
1220 if (pedantic
|| warn_pointer_arith
)
1221 pedwarn ("ANSI C++ forbids taking the sizeof a function type");
1222 return size_int (1);
1224 if (code
== METHOD_TYPE
)
1226 if (pedantic
|| warn_pointer_arith
)
1227 pedwarn ("ANSI C++ forbids taking the sizeof a method type");
1228 return size_int (1);
1230 if (code
== VOID_TYPE
)
1232 if (pedantic
|| warn_pointer_arith
)
1233 pedwarn ("ANSI C++ forbids taking the sizeof a void type");
1234 return size_int (1);
1236 if (code
== ERROR_MARK
)
1237 return size_int (1);
1239 /* ARM $5.3.2: ``When applied to a reference, the result is the size of the
1240 referenced object.'' */
1241 if (code
== REFERENCE_TYPE
)
1242 type
= TREE_TYPE (type
);
1244 /* We couldn't find anything in the ARM or the draft standard that says,
1245 one way or the other, if doing sizeof on something that doesn't have
1246 an object associated with it is correct or incorrect. For example, if
1247 you declare `struct S { char str[16]; };', and in your program do
1248 a `sizeof (S::str)', should we flag that as an error or should we give
1249 the size of it? Since it seems like a reasonable thing to do, we'll go
1250 with giving the value. */
1251 if (code
== OFFSET_TYPE
)
1252 type
= TREE_TYPE (type
);
1254 /* @@ This also produces an error for a signature ref.
1255 In that case we should be able to do better. */
1256 if (IS_SIGNATURE (type
))
1258 error ("`sizeof' applied to a signature type");
1259 return size_int (0);
1262 if (TYPE_SIZE (type
) == 0)
1264 error ("`sizeof' applied to an incomplete type");
1265 return size_int (0);
1268 /* Convert in case a char is more than one unit. */
1269 t
= size_binop (CEIL_DIV_EXPR
, TYPE_SIZE (type
),
1270 size_int (TYPE_PRECISION (char_type_node
)));
1271 /* size_binop does not put the constant in range, so do it now. */
1272 if (TREE_CODE (t
) == INTEGER_CST
&& force_fit_type (t
, 0))
1273 TREE_CONSTANT_OVERFLOW (t
) = TREE_OVERFLOW (t
) = 1;
1278 c_sizeof_nowarn (type
)
1281 enum tree_code code
= TREE_CODE (type
);
1284 if (code
== FUNCTION_TYPE
1285 || code
== METHOD_TYPE
1286 || code
== VOID_TYPE
1287 || code
== ERROR_MARK
)
1288 return size_int (1);
1289 if (code
== REFERENCE_TYPE
)
1290 type
= TREE_TYPE (type
);
1292 if (TYPE_SIZE (type
) == 0)
1293 return size_int (0);
1295 /* Convert in case a char is more than one unit. */
1296 t
= size_binop (CEIL_DIV_EXPR
, TYPE_SIZE (type
),
1297 size_int (TYPE_PRECISION (char_type_node
)));
1298 force_fit_type (t
, 0);
1302 /* Implement the __alignof keyword: Return the minimum required
1303 alignment of TYPE, measured in bytes. */
1309 enum tree_code code
= TREE_CODE (type
);
1312 if (code
== FUNCTION_TYPE
|| code
== METHOD_TYPE
)
1313 return size_int (FUNCTION_BOUNDARY
/ BITS_PER_UNIT
);
1315 if (code
== VOID_TYPE
|| code
== ERROR_MARK
)
1316 return size_int (1);
1318 /* C++: this is really correct! */
1319 if (code
== REFERENCE_TYPE
)
1320 type
= TREE_TYPE (type
);
1322 /* @@ This also produces an error for a signature ref.
1323 In that case we should be able to do better. */
1324 if (IS_SIGNATURE (type
))
1326 error ("`__alignof' applied to a signature type");
1327 return size_int (1);
1330 t
= size_int (TYPE_ALIGN (type
) / BITS_PER_UNIT
);
1331 force_fit_type (t
, 0);
1335 /* Perform default promotions for C data used in expressions.
1336 Arrays and functions are converted to pointers;
1337 enumeral types or short or char, to int.
1338 In addition, manifest constants symbols are replaced by their values.
1340 C++: this will automatically bash references to their target type. */
1343 decay_conversion (exp
)
1346 register tree type
= TREE_TYPE (exp
);
1347 register enum tree_code code
= TREE_CODE (type
);
1349 if (code
== OFFSET_TYPE
/* || TREE_CODE (exp) == OFFSET_REF */ )
1351 if (TREE_CODE (exp
) == OFFSET_REF
)
1352 return decay_conversion (resolve_offset_ref (exp
));
1354 type
= TREE_TYPE (type
);
1355 code
= TREE_CODE (type
);
1358 if (code
== REFERENCE_TYPE
)
1360 exp
= convert_from_reference (exp
);
1361 type
= TREE_TYPE (exp
);
1362 code
= TREE_CODE (type
);
1365 /* Constants can be used directly unless they're not loadable. */
1366 if (TREE_CODE (exp
) == CONST_DECL
)
1367 exp
= DECL_INITIAL (exp
);
1368 /* Replace a nonvolatile const static variable with its value. */
1369 else if (TREE_READONLY_DECL_P (exp
))
1371 exp
= decl_constant_value (exp
);
1372 type
= TREE_TYPE (exp
);
1375 /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
1376 Leave such NOP_EXPRs, since RHS is being used in non-lvalue context. */
1378 if (code
== VOID_TYPE
)
1380 error ("void value not ignored as it ought to be");
1381 return error_mark_node
;
1383 if (code
== FUNCTION_TYPE
)
1385 return build_unary_op (ADDR_EXPR
, exp
, 0);
1387 if (code
== METHOD_TYPE
)
1389 if (TREE_CODE (exp
) == OFFSET_REF
)
1391 /* FIXME: We should emit an error here about using a ptrmemfunc
1392 for something other than a function call. */
1393 my_friendly_assert (TREE_CODE (TREE_OPERAND (exp
, 1)) == FUNCTION_DECL
,
1395 return build_unary_op (ADDR_EXPR
, TREE_OPERAND (exp
, 1), 0);
1397 return build_unary_op (ADDR_EXPR
, exp
, 0);
1399 if (code
== ARRAY_TYPE
)
1404 int constp
, volatilep
;
1406 if (TREE_CODE (exp
) == INDIRECT_REF
)
1408 /* Stripping away the INDIRECT_REF is not the right
1409 thing to do for references... */
1410 tree inner
= TREE_OPERAND (exp
, 0);
1411 if (TREE_CODE (TREE_TYPE (inner
)) == REFERENCE_TYPE
)
1413 inner
= build1 (CONVERT_EXPR
,
1414 build_pointer_type (TREE_TYPE (TREE_TYPE (inner
))),
1416 TREE_REFERENCE_EXPR (inner
) = 1;
1418 return convert (build_pointer_type (TREE_TYPE (type
)), inner
);
1421 if (TREE_CODE (exp
) == COMPOUND_EXPR
)
1423 tree op1
= decay_conversion (TREE_OPERAND (exp
, 1));
1424 return build (COMPOUND_EXPR
, TREE_TYPE (op1
),
1425 TREE_OPERAND (exp
, 0), op1
);
1429 && ! (TREE_CODE (exp
) == CONSTRUCTOR
&& TREE_STATIC (exp
)))
1431 error ("invalid use of non-lvalue array");
1432 return error_mark_node
;
1435 constp
= volatilep
= 0;
1436 if (TREE_CODE_CLASS (TREE_CODE (exp
)) == 'r'
1437 || TREE_CODE_CLASS (TREE_CODE (exp
)) == 'd')
1439 constp
= TREE_READONLY (exp
);
1440 volatilep
= TREE_THIS_VOLATILE (exp
);
1443 restype
= TREE_TYPE (type
);
1444 if (TYPE_READONLY (type
) || TYPE_VOLATILE (type
)
1445 || constp
|| volatilep
)
1446 restype
= cp_build_type_variant (restype
,
1447 TYPE_READONLY (type
) || constp
,
1448 TYPE_VOLATILE (type
) || volatilep
);
1449 ptrtype
= build_pointer_type (restype
);
1451 if (TREE_CODE (exp
) == VAR_DECL
)
1453 /* ??? This is not really quite correct
1454 in that the type of the operand of ADDR_EXPR
1455 is not the target type of the type of the ADDR_EXPR itself.
1456 Question is, can this lossage be avoided? */
1457 adr
= build1 (ADDR_EXPR
, ptrtype
, exp
);
1458 if (mark_addressable (exp
) == 0)
1459 return error_mark_node
;
1460 TREE_CONSTANT (adr
) = staticp (exp
);
1461 TREE_SIDE_EFFECTS (adr
) = 0; /* Default would be, same as EXP. */
1464 /* This way is better for a COMPONENT_REF since it can
1465 simplify the offset for a component. */
1466 adr
= build_unary_op (ADDR_EXPR
, exp
, 1);
1467 return convert (ptrtype
, adr
);
1474 default_conversion (exp
)
1478 enum tree_code code
;
1480 exp
= decay_conversion (exp
);
1482 type
= TREE_TYPE (exp
);
1483 code
= TREE_CODE (type
);
1485 if (INTEGRAL_CODE_P (code
))
1487 tree t
= type_promotes_to (type
);
1489 return convert (t
, exp
);
1491 if (flag_traditional
1492 && TYPE_MAIN_VARIANT (type
) == float_type_node
)
1493 return convert (double_type_node
, exp
);
1499 build_object_ref (datum
, basetype
, field
)
1500 tree datum
, basetype
, field
;
1503 if (datum
== error_mark_node
)
1504 return error_mark_node
;
1506 dtype
= TREE_TYPE (datum
);
1507 if (TREE_CODE (dtype
) == REFERENCE_TYPE
)
1508 dtype
= TREE_TYPE (dtype
);
1509 if (! IS_AGGR_TYPE_CODE (TREE_CODE (dtype
)))
1511 cp_error ("request for member `%T::%D' in expression of non-aggregate type `%T'",
1512 basetype
, field
, dtype
);
1513 return error_mark_node
;
1515 else if (IS_SIGNATURE (basetype
))
1517 warning ("signature name in scope resolution ignored");
1518 return build_component_ref (datum
, field
, NULL_TREE
, 1);
1520 else if (is_aggr_type (basetype
, 1))
1522 tree binfo
= binfo_or_else (basetype
, dtype
);
1524 return build_component_ref (build_scoped_ref (datum
, basetype
),
1527 return error_mark_node
;
1530 /* Like `build_component_ref, but uses an already found field.
1531 Must compute access for C_C_D. Otherwise, ok. */
1533 build_component_ref_1 (datum
, field
, protect
)
1537 register tree basetype
= TREE_TYPE (datum
);
1538 register enum tree_code code
= TREE_CODE (basetype
);
1541 if (code
== REFERENCE_TYPE
)
1543 datum
= convert_from_reference (datum
);
1544 basetype
= TREE_TYPE (datum
);
1545 code
= TREE_CODE (basetype
);
1548 if (! IS_AGGR_TYPE_CODE (code
))
1550 if (code
!= ERROR_MARK
)
1551 cp_error ("request for member `%D' in `%E', which is of non-aggregate type `%T'",
1552 field
, datum
, basetype
);
1553 return error_mark_node
;
1556 if (TYPE_SIZE (basetype
) == 0)
1558 incomplete_type_error (0, basetype
);
1559 return error_mark_node
;
1562 /* Look up component name in the structure type definition. */
1564 if (field
== error_mark_node
)
1565 my_friendly_abort (115);
1567 if (TREE_STATIC (field
))
1572 tree access
= compute_access (TYPE_BINFO (current_class_type
), field
);
1574 if (access
== access_private_node
)
1576 cp_error ("field `%D' is private", field
);
1577 return error_mark_node
;
1579 else if (access
== access_protected_node
)
1581 cp_error ("field `%D' is protected", field
);
1582 return error_mark_node
;
1586 ref
= build (COMPONENT_REF
, TREE_TYPE (field
), datum
, field
);
1588 if (TREE_READONLY (datum
) || TREE_READONLY (field
))
1589 TREE_READONLY (ref
) = 1;
1590 if (TREE_THIS_VOLATILE (datum
) || TREE_THIS_VOLATILE (field
))
1591 TREE_THIS_VOLATILE (ref
) = 1;
1592 if (DECL_MUTABLE_P (field
))
1593 TREE_READONLY (ref
) = 0;
1598 /* Given a COND_EXPR in T, return it in a form that we can, for
1599 example, use as an lvalue. This code used to be in unary_complex_lvalue,
1600 but we needed it to deal with `a = (d == c) ? b : c' expressions, where
1601 we're dealing with aggregates. So, we now call this in unary_complex_lvalue,
1602 and in build_modify_expr. The case (in particular) that led to this was
1603 with CODE == ADDR_EXPR, since it's not an lvalue when we'd get it there. */
1605 rationalize_conditional_expr (code
, t
)
1606 enum tree_code code
;
1610 build_conditional_expr (TREE_OPERAND (t
, 0),
1611 build_unary_op (code
, TREE_OPERAND (t
, 1), 0),
1612 build_unary_op (code
, TREE_OPERAND (t
, 2), 0));
1615 /* Given the TYPE of an anonymous union field inside T, return the
1616 FIELD_DECL for the field. If not found return NULL_TREE. Because
1617 anonymous unions can nest, we must also search all anonymous unions
1618 that are directly reachable. */
1620 lookup_anon_field (t
, type
)
1625 for (field
= TYPE_FIELDS (t
); field
; field
= TREE_CHAIN (field
))
1627 if (TREE_STATIC (field
))
1629 if (TREE_CODE (field
) != FIELD_DECL
)
1632 /* If we find it directly, return the field. */
1633 if (DECL_NAME (field
) == NULL_TREE
1634 && type
== TREE_TYPE (field
))
1639 /* Otherwise, it could be nested, search harder. */
1640 if (DECL_NAME (field
) == NULL_TREE
1641 && TREE_CODE (TREE_TYPE (field
)) == UNION_TYPE
)
1643 tree subfield
= lookup_anon_field (TREE_TYPE (field
), type
);
1651 /* Build a COMPONENT_REF for a given DATUM, and it's member COMPONENT.
1652 COMPONENT can be an IDENTIFIER_NODE that is the name of the member
1653 that we are interested in, or it can be a FIELD_DECL. */
1655 build_component_ref (datum
, component
, basetype_path
, protect
)
1656 tree datum
, component
, basetype_path
;
1659 register tree basetype
= TREE_TYPE (datum
);
1660 register enum tree_code code
= TREE_CODE (basetype
);
1661 register tree field
= NULL
;
1664 /* If DATUM is a COMPOUND_EXPR or COND_EXPR, move our reference inside it. */
1665 switch (TREE_CODE (datum
))
1669 tree value
= build_component_ref (TREE_OPERAND (datum
, 1), component
,
1670 basetype_path
, protect
);
1671 return build (COMPOUND_EXPR
, TREE_TYPE (value
),
1672 TREE_OPERAND (datum
, 0), value
);
1675 return build_conditional_expr
1676 (TREE_OPERAND (datum
, 0),
1677 build_component_ref (TREE_OPERAND (datum
, 1), component
,
1678 basetype_path
, protect
),
1679 build_component_ref (TREE_OPERAND (datum
, 2), component
,
1680 basetype_path
, protect
));
1683 if (code
== REFERENCE_TYPE
)
1685 datum
= convert_from_reference (datum
);
1686 basetype
= TREE_TYPE (datum
);
1687 code
= TREE_CODE (basetype
);
1690 /* First, see if there is a field or component with name COMPONENT. */
1691 if (TREE_CODE (component
) == TREE_LIST
)
1693 my_friendly_assert (!(TREE_CHAIN (component
) == NULL_TREE
1694 && DECL_CHAIN (TREE_VALUE (component
)) == NULL_TREE
), 309);
1695 return build (COMPONENT_REF
, TREE_TYPE (component
), datum
, component
);
1698 if (! IS_AGGR_TYPE_CODE (code
))
1700 if (code
!= ERROR_MARK
)
1701 cp_error ("request for member `%D' in `%E', which is of non-aggregate type `%T'",
1702 component
, datum
, basetype
);
1703 return error_mark_node
;
1706 if (TYPE_SIZE (basetype
) == 0)
1708 incomplete_type_error (0, basetype
);
1709 return error_mark_node
;
1712 if (TREE_CODE (component
) == BIT_NOT_EXPR
)
1714 if (TYPE_IDENTIFIER (basetype
) != TREE_OPERAND (component
, 0))
1716 cp_error ("destructor specifier `%T::~%T' must have matching names",
1717 basetype
, TREE_OPERAND (component
, 0));
1718 return error_mark_node
;
1720 if (! TYPE_HAS_DESTRUCTOR (basetype
))
1722 cp_error ("type `%T' has no destructor", basetype
);
1723 return error_mark_node
;
1725 return TREE_VEC_ELT (CLASSTYPE_METHOD_VEC (basetype
), 0);
1728 /* Look up component name in the structure type definition. */
1729 if (CLASSTYPE_VFIELD (basetype
)
1730 && DECL_NAME (CLASSTYPE_VFIELD (basetype
)) == component
)
1731 /* Special-case this because if we use normal lookups in an ambiguous
1732 hierarchy, the compiler will abort (because vptr lookups are
1733 not supposed to be ambiguous. */
1734 field
= CLASSTYPE_VFIELD (basetype
);
1735 else if (TREE_CODE (component
) == FIELD_DECL
)
1741 if (basetype_path
== NULL_TREE
)
1742 basetype_path
= TYPE_BINFO (basetype
);
1743 field
= lookup_field (basetype_path
, component
,
1744 protect
&& ! VFIELD_NAME_P (component
), 0);
1745 if (field
== error_mark_node
)
1746 return error_mark_node
;
1748 if (field
== NULL_TREE
)
1750 /* Not found as a data field, look for it as a method. If found,
1751 then if this is the only possible one, return it, else
1752 report ambiguity error. */
1753 tree fndecls
= lookup_fnfields (basetype_path
, component
, 1);
1754 if (fndecls
== error_mark_node
)
1755 return error_mark_node
;
1758 if (TREE_CHAIN (fndecls
) == NULL_TREE
1759 && DECL_CHAIN (TREE_VALUE (fndecls
)) == NULL_TREE
)
1761 tree access
, fndecl
;
1763 /* Unique, so use this one now. */
1764 basetype
= TREE_PURPOSE (fndecls
);
1765 fndecl
= TREE_VALUE (fndecls
);
1766 access
= compute_access (TREE_PURPOSE (fndecls
), fndecl
);
1767 if (access
== access_public_node
)
1769 if (DECL_VINDEX (fndecl
)
1770 && ! resolves_to_fixed_type_p (datum
, 0))
1772 tree addr
= build_unary_op (ADDR_EXPR
, datum
, 0);
1773 addr
= convert_pointer_to (DECL_CONTEXT (fndecl
), addr
);
1774 datum
= build_indirect_ref (addr
, NULL_PTR
);
1775 my_friendly_assert (datum
!= error_mark_node
, 310);
1776 fndecl
= build_vfn_ref (&addr
, datum
, DECL_VINDEX (fndecl
));
1781 if (access
== access_protected_node
)
1782 cp_error ("member function `%D' is protected", fndecl
);
1784 cp_error ("member function `%D' is private", fndecl
);
1785 return error_mark_node
;
1789 /* Just act like build_offset_ref, since the object does
1790 not matter unless we're actually calling the function. */
1793 t
= build_tree_list (error_mark_node
, fndecls
);
1794 TREE_TYPE (t
) = build_offset_type (basetype
,
1800 cp_error ("`%#T' has no member named `%D'", basetype
, component
);
1801 return error_mark_node
;
1803 else if (TREE_TYPE (field
) == error_mark_node
)
1804 return error_mark_node
;
1806 if (TREE_CODE (field
) != FIELD_DECL
)
1808 if (TREE_CODE (field
) == TYPE_DECL
)
1810 cp_error ("invalid use of type decl `%#D' as expression", field
);
1811 return error_mark_node
;
1813 else if (DECL_RTL (field
) != 0)
1816 TREE_USED (field
) = 1;
1821 /* See if we have to do any conversions so that we pick up the field from the
1823 if (DECL_FIELD_CONTEXT (field
) != basetype
)
1825 tree context
= DECL_FIELD_CONTEXT (field
);
1826 tree base
= context
;
1827 while (base
!= basetype
&& ANON_AGGRNAME_P (TYPE_IDENTIFIER (base
)))
1829 base
= TYPE_CONTEXT (base
);
1832 /* Handle base classes here... */
1833 if (base
!= basetype
&& TYPE_USES_COMPLEX_INHERITANCE (basetype
))
1835 tree addr
= build_unary_op (ADDR_EXPR
, datum
, 0);
1836 if (integer_zerop (addr
))
1838 error ("invalid reference to NULL ptr, use ptr-to-member instead");
1839 return error_mark_node
;
1841 if (VBASE_NAME_P (DECL_NAME (field
)))
1843 /* It doesn't matter which vbase pointer we grab, just
1844 find one of them. */
1845 tree binfo
= get_binfo (base
,
1846 TREE_TYPE (TREE_TYPE (addr
)), 0);
1847 addr
= convert_pointer_to_real (binfo
, addr
);
1850 addr
= convert_pointer_to (base
, addr
);
1851 datum
= build_indirect_ref (addr
, NULL_PTR
);
1852 my_friendly_assert (datum
!= error_mark_node
, 311);
1856 /* Handle things from anon unions here... */
1857 if (ANON_AGGRNAME_P (TYPE_IDENTIFIER (context
)))
1859 tree subfield
= lookup_anon_field (basetype
, context
);
1860 tree subdatum
= build_component_ref (datum
, subfield
,
1861 basetype_path
, protect
);
1862 return build_component_ref (subdatum
, field
, basetype_path
, protect
);
1866 ref
= fold (build (COMPONENT_REF
, TREE_TYPE (field
),
1867 break_out_cleanups (datum
), field
));
1869 if (TREE_READONLY (datum
) || TREE_READONLY (field
))
1870 TREE_READONLY (ref
) = 1;
1871 if (TREE_THIS_VOLATILE (datum
) || TREE_THIS_VOLATILE (field
))
1872 TREE_THIS_VOLATILE (ref
) = 1;
1873 if (DECL_MUTABLE_P (field
))
1874 TREE_READONLY (ref
) = 0;
1879 /* Given an expression PTR for a pointer, return an expression
1880 for the value pointed to.
1881 ERRORSTRING is the name of the operator to appear in error messages.
1883 This function may need to overload OPERATOR_FNNAME.
1884 Must also handle REFERENCE_TYPEs for C++. */
1887 build_x_indirect_ref (ptr
, errorstring
)
1891 tree rval
= build_opfncall (INDIRECT_REF
, LOOKUP_NORMAL
, ptr
, NULL_TREE
, NULL_TREE
);
1894 return build_indirect_ref (ptr
, errorstring
);
1898 build_indirect_ref (ptr
, errorstring
)
1902 register tree pointer
= (TREE_CODE (TREE_TYPE (ptr
)) == REFERENCE_TYPE
?
1903 ptr
: default_conversion (ptr
));
1904 register tree type
= TREE_TYPE (pointer
);
1906 if (ptr
== current_class_decl
)
1909 if (IS_AGGR_TYPE (type
))
1911 ptr
= build_expr_type_conversion (WANT_POINTER
, pointer
, 1);
1916 type
= TREE_TYPE (pointer
);
1920 if (TREE_CODE (type
) == POINTER_TYPE
|| TREE_CODE (type
) == REFERENCE_TYPE
)
1922 if (TREE_CODE (pointer
) == ADDR_EXPR
1923 && (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_OPERAND (pointer
, 0)))
1924 == TYPE_MAIN_VARIANT (TREE_TYPE (type
)))
1925 && (TREE_READONLY (TREE_OPERAND (pointer
, 0))
1926 == TYPE_READONLY (TREE_TYPE (type
)))
1927 && (TREE_THIS_VOLATILE (TREE_OPERAND (pointer
, 0))
1928 == TYPE_VOLATILE (TREE_TYPE (type
))))
1929 return TREE_OPERAND (pointer
, 0);
1932 tree t
= TREE_TYPE (type
);
1933 register tree ref
= build1 (INDIRECT_REF
,
1934 TYPE_MAIN_VARIANT (t
), pointer
);
1936 TREE_READONLY (ref
) = TYPE_READONLY (t
);
1937 TREE_THIS_VOLATILE (ref
) = TYPE_VOLATILE (t
);
1938 TREE_SIDE_EFFECTS (ref
)
1939 = TYPE_VOLATILE (t
) || TREE_SIDE_EFFECTS (pointer
);
1943 /* `pointer' won't be an error_mark_node if we were given a
1944 pointer to member, so it's cool to check for this here. */
1945 else if (TYPE_PTRMEMFUNC_P (type
))
1946 error ("invalid use of `%s' on pointer to member function", errorstring
);
1947 else if (TREE_CODE (type
) == RECORD_TYPE
1948 && (IS_SIGNATURE_POINTER (type
) || IS_SIGNATURE_REFERENCE (type
)))
1949 error ("cannot dereference signature pointer/reference");
1950 else if (pointer
!= error_mark_node
)
1953 error ("invalid type argument of `%s'", errorstring
);
1955 error ("invalid type argument");
1957 return error_mark_node
;
1960 /* This handles expressions of the form "a[i]", which denotes
1963 This is logically equivalent in C to *(a+i), but we may do it differently.
1964 If A is a variable or a member, we generate a primitive ARRAY_REF.
1965 This avoids forcing the array out of registers, and can work on
1966 arrays that are not lvalues (for example, members of structures returned
1969 If INDEX is of some user-defined type, it must be converted to
1970 integer type. Otherwise, to make a compatible PLUS_EXPR, it
1971 will inherit the type of the array, which will be some pointer type. */
1974 build_x_array_ref (array
, index
)
1977 tree rval
= build_opfncall (ARRAY_REF
, LOOKUP_NORMAL
, array
, index
, NULL_TREE
);
1980 return build_array_ref (array
, index
);
1984 build_array_ref (array
, idx
)
1991 error ("subscript missing in array reference");
1992 return error_mark_node
;
1995 if (TREE_TYPE (array
) == error_mark_node
1996 || TREE_TYPE (idx
) == error_mark_node
)
1997 return error_mark_node
;
1999 itype
= TREE_TYPE (idx
);
2001 if (TREE_CODE (TREE_TYPE (array
)) == ARRAY_TYPE
2002 && TREE_CODE (array
) != INDIRECT_REF
)
2006 /* Subscripting with type char is likely to lose
2007 on a machine where chars are signed.
2008 So warn on any machine, but optionally.
2009 Don't warn for unsigned char since that type is safe.
2010 Don't warn for signed char because anyone who uses that
2011 must have done so deliberately. */
2012 if (warn_char_subscripts
2013 && TYPE_MAIN_VARIANT (TREE_TYPE (idx
)) == char_type_node
)
2014 warning ("array subscript has type `char'");
2016 /* Apply default promotions *after* noticing character types. */
2017 idx
= default_conversion (idx
);
2019 if (TREE_CODE (TREE_TYPE (idx
)) != INTEGER_TYPE
)
2021 error ("array subscript is not an integer");
2022 return error_mark_node
;
2025 /* An array that is indexed by a non-constant
2026 cannot be stored in a register; we must be able to do
2027 address arithmetic on its address.
2028 Likewise an array of elements of variable size. */
2029 if (TREE_CODE (idx
) != INTEGER_CST
2030 || (TYPE_SIZE (TREE_TYPE (TREE_TYPE (array
))) != 0
2031 && TREE_CODE (TYPE_SIZE (TREE_TYPE (TREE_TYPE (array
)))) != INTEGER_CST
))
2033 if (mark_addressable (array
) == 0)
2034 return error_mark_node
;
2036 /* An array that is indexed by a constant value which is not within
2037 the array bounds cannot be stored in a register either; because we
2038 would get a crash in store_bit_field/extract_bit_field when trying
2039 to access a non-existent part of the register. */
2040 if (TREE_CODE (idx
) == INTEGER_CST
2041 && TYPE_VALUES (TREE_TYPE (array
))
2042 && ! int_fits_type_p (idx
, TYPE_VALUES (TREE_TYPE (array
))))
2044 if (mark_addressable (array
) == 0)
2045 return error_mark_node
;
2048 if (pedantic
&& !lvalue_p (array
))
2049 pedwarn ("ANSI C++ forbids subscripting non-lvalue array");
2051 /* Note in C++ it is valid to subscript a `register' array, since
2052 it is valid to take the address of something with that
2053 storage specification. */
2057 while (TREE_CODE (foo
) == COMPONENT_REF
)
2058 foo
= TREE_OPERAND (foo
, 0);
2059 if (TREE_CODE (foo
) == VAR_DECL
&& DECL_REGISTER (foo
))
2060 warning ("subscripting array declared `register'");
2063 type
= TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (array
)));
2064 rval
= build (ARRAY_REF
, type
, array
, idx
);
2065 /* Array ref is const/volatile if the array elements are
2066 or if the array is.. */
2067 TREE_READONLY (rval
)
2068 |= (TYPE_READONLY (TREE_TYPE (TREE_TYPE (array
)))
2069 | TREE_READONLY (array
));
2070 TREE_SIDE_EFFECTS (rval
)
2071 |= (TYPE_VOLATILE (TREE_TYPE (TREE_TYPE (array
)))
2072 | TREE_SIDE_EFFECTS (array
));
2073 TREE_THIS_VOLATILE (rval
)
2074 |= (TYPE_VOLATILE (TREE_TYPE (TREE_TYPE (array
)))
2075 /* This was added by rms on 16 Nov 91.
2076 It fixes vol struct foo *a; a->elts[1]
2077 in an inline function.
2078 Hope it doesn't break something else. */
2079 | TREE_THIS_VOLATILE (array
));
2080 return require_complete_type (fold (rval
));
2084 tree ar
= default_conversion (array
);
2085 tree ind
= default_conversion (idx
);
2087 /* Put the integer in IND to simplify error checking. */
2088 if (TREE_CODE (TREE_TYPE (ar
)) == INTEGER_TYPE
)
2095 if (ar
== error_mark_node
)
2098 if (TREE_CODE (TREE_TYPE (ar
)) != POINTER_TYPE
)
2100 error ("subscripted value is neither array nor pointer");
2101 return error_mark_node
;
2103 if (TREE_CODE (TREE_TYPE (ind
)) != INTEGER_TYPE
)
2105 error ("array subscript is not an integer");
2106 return error_mark_node
;
2109 return build_indirect_ref (build_binary_op_nodefault (PLUS_EXPR
, ar
, ind
, PLUS_EXPR
),
2114 /* Build a function call to function FUNCTION with parameters PARAMS.
2115 PARAMS is a list--a chain of TREE_LIST nodes--in which the
2116 TREE_VALUE of each node is a parameter-expression.
2117 FUNCTION's data type may be a function type or a pointer-to-function.
2119 For C++: If FUNCTION's data type is a TREE_LIST, then the tree list
2120 is the list of possible methods that FUNCTION could conceivably
2121 be. If the list of methods comes from a class, then it will be
2122 a list of lists (where each element is associated with the class
2123 that produced it), otherwise it will be a simple list (for
2124 functions overloaded in global scope).
2126 In the first case, TREE_VALUE (function) is the head of one of those
2127 lists, and TREE_PURPOSE is the name of the function.
2129 In the second case, TREE_PURPOSE (function) is the function's
2132 DECL is the class instance variable, usually CURRENT_CLASS_DECL. */
2135 * [eichin:19911015.1726EST] actually return a possibly incomplete
2139 build_x_function_call (function
, params
, decl
)
2140 tree function
, params
, decl
;
2145 if (function
== error_mark_node
)
2146 return error_mark_node
;
2148 type
= TREE_TYPE (function
);
2149 is_method
= ((TREE_CODE (function
) == TREE_LIST
2150 && current_class_type
!= NULL_TREE
2151 && IDENTIFIER_CLASS_VALUE (TREE_PURPOSE (function
)) == function
)
2152 || TREE_CODE (function
) == IDENTIFIER_NODE
2153 || TREE_CODE (type
) == METHOD_TYPE
2154 || TYPE_PTRMEMFUNC_P (type
));
2156 /* Handle methods, friends, and overloaded functions, respectively. */
2159 if (TREE_CODE (function
) == FUNCTION_DECL
)
2161 if (DECL_NAME (function
))
2162 function
= DECL_NAME (function
);
2164 function
= TYPE_IDENTIFIER (DECL_CLASS_CONTEXT (function
));
2166 else if (TREE_CODE (function
) == TREE_LIST
)
2168 my_friendly_assert (TREE_CODE (TREE_VALUE (function
)) == FUNCTION_DECL
, 312);
2169 function
= TREE_PURPOSE (function
);
2171 else if (TREE_CODE (function
) != IDENTIFIER_NODE
)
2173 if (TREE_CODE (function
) == OFFSET_REF
)
2175 if (TREE_OPERAND (function
, 0))
2176 decl
= TREE_OPERAND (function
, 0);
2178 /* Call via a pointer to member function. */
2179 if (decl
== NULL_TREE
)
2181 error ("pointer to member function called, but not in class scope");
2182 return error_mark_node
;
2184 /* What other type of POINTER_TYPE could this be? */
2185 if (TREE_CODE (TREE_TYPE (function
)) != POINTER_TYPE
2186 && ! TYPE_PTRMEMFUNC_P (TREE_TYPE (function
))
2187 && TREE_CODE (function
) != OFFSET_REF
)
2188 function
= build (OFFSET_REF
, TREE_TYPE (type
), NULL_TREE
, function
);
2192 /* this is an abbreviated method call.
2193 must go through here in case it is a virtual function.
2194 @@ Perhaps this could be optimized. */
2196 if (decl
== NULL_TREE
)
2198 if (current_class_type
== NULL_TREE
)
2200 error ("object missing in call to method `%s'",
2201 IDENTIFIER_POINTER (function
));
2202 return error_mark_node
;
2204 /* Yow: call from a static member function. */
2205 decl
= build1 (NOP_EXPR
, build_pointer_type (current_class_type
),
2207 decl
= build_indirect_ref (decl
, NULL_PTR
);
2210 return build_method_call (decl
, function
, params
,
2211 NULL_TREE
, LOOKUP_NORMAL
);
2213 else if (TREE_CODE (function
) == COMPONENT_REF
2214 && type
== unknown_type_node
)
2216 /* Should we undo what was done in build_component_ref? */
2217 if (TREE_CODE (TREE_PURPOSE (TREE_OPERAND (function
, 1))) == TREE_VEC
)
2218 /* Get the name that build_component_ref hid. */
2219 function
= DECL_NAME (TREE_VALUE (TREE_OPERAND (function
, 1)));
2221 function
= TREE_PURPOSE (TREE_OPERAND (function
, 1));
2222 return build_method_call (decl
, function
, params
,
2223 NULL_TREE
, LOOKUP_NORMAL
);
2225 else if (TREE_CODE (function
) == TREE_LIST
)
2227 if (TREE_VALUE (function
) == NULL_TREE
)
2229 cp_error ("function `%D' declared overloaded, but no definitions appear with which to resolve it?!?",
2230 TREE_PURPOSE (function
));
2231 return error_mark_node
;
2235 tree val
= TREE_VALUE (function
);
2237 if (TREE_CODE (val
) == TEMPLATE_DECL
)
2238 return build_overload_call_maybe
2239 (function
, params
, LOOKUP_COMPLAIN
, (struct candidate
*)0);
2240 else if (DECL_CHAIN (val
) != NULL_TREE
)
2241 return build_overload_call
2242 (function
, params
, LOOKUP_COMPLAIN
, (struct candidate
*)0);
2244 my_friendly_abort (360);
2249 if (TREE_CODE (function
) == OFFSET_REF
)
2251 /* If the component is a data element (or a virtual function), we play
2252 games here to make things work. */
2255 if (TREE_OPERAND (function
, 0))
2256 decl
= TREE_OPERAND (function
, 0);
2260 decl_addr
= build_unary_op (ADDR_EXPR
, decl
, 0);
2261 function
= get_member_function_from_ptrfunc (&decl_addr
,
2262 TREE_OPERAND (function
, 1));
2263 params
= tree_cons (NULL_TREE
, decl_addr
, params
);
2264 return build_function_call (function
, params
);
2267 type
= TREE_TYPE (function
);
2268 if (type
!= error_mark_node
)
2270 if (TREE_CODE (type
) == REFERENCE_TYPE
)
2271 type
= TREE_TYPE (type
);
2273 if (TYPE_LANG_SPECIFIC (type
) && TYPE_OVERLOADS_CALL_EXPR (type
))
2274 return build_opfncall (CALL_EXPR
, LOOKUP_NORMAL
, function
, params
, NULL_TREE
);
2279 tree fntype
= TREE_TYPE (function
);
2282 /* Explicitly named method? */
2283 if (TREE_CODE (function
) == FUNCTION_DECL
)
2284 ctypeptr
= build_pointer_type (DECL_CLASS_CONTEXT (function
));
2285 /* Expression with ptr-to-method type? It could either be a plain
2286 usage, or it might be a case where the ptr-to-method is being
2287 passed in as an argument. */
2288 else if (TYPE_PTRMEMFUNC_P (fntype
))
2290 tree rec
= TYPE_METHOD_BASETYPE (TREE_TYPE (TYPE_PTRMEMFUNC_FN_TYPE (fntype
)));
2291 ctypeptr
= build_pointer_type (rec
);
2293 /* Unexpected node type? */
2295 my_friendly_abort (116);
2296 if (decl
== NULL_TREE
)
2298 if (current_function_decl
2299 && DECL_STATIC_FUNCTION_P (current_function_decl
))
2300 error ("invalid call to member function needing `this' in static member function scope");
2302 error ("pointer to member function called, but not in class scope");
2303 return error_mark_node
;
2305 if (TREE_CODE (TREE_TYPE (decl
)) != POINTER_TYPE
2306 && ! TYPE_PTRMEMFUNC_P (TREE_TYPE (decl
)))
2308 decl
= build_unary_op (ADDR_EXPR
, decl
, 0);
2309 decl
= convert_pointer_to (TREE_TYPE (ctypeptr
), decl
);
2312 decl
= build_c_cast (ctypeptr
, decl
, 0);
2313 params
= tree_cons (NULL_TREE
, decl
, params
);
2316 return build_function_call (function
, params
);
2319 /* Resolve a pointer to member function. INSTANCE is the object
2320 instance to use, if the member points to a virtual member. */
2323 get_member_function_from_ptrfunc (instance_ptrptr
, function
)
2324 tree
*instance_ptrptr
;
2327 if (TREE_CODE (function
) == OFFSET_REF
)
2329 function
= TREE_OPERAND (function
, 1);
2332 if (TYPE_PTRMEMFUNC_P (TREE_TYPE (function
)))
2334 tree fntype
, index
, e1
, delta
, delta2
, e2
, e3
, aref
, vtbl
;
2337 tree instance_ptr
= *instance_ptrptr
;
2339 if (TREE_SIDE_EFFECTS (instance_ptr
))
2340 instance_ptr
= save_expr (instance_ptr
);
2342 if (TREE_SIDE_EFFECTS (function
))
2343 function
= save_expr (function
);
2345 fntype
= TYPE_PTRMEMFUNC_FN_TYPE (TREE_TYPE (function
));
2346 index
= save_expr (build_component_ref (function
,
2349 e1
= build (GT_EXPR
, boolean_type_node
, index
,
2350 convert (delta_type_node
, integer_zero_node
));
2351 delta
= convert (ptrdiff_type_node
,
2352 build_component_ref (function
, delta_identifier
, 0, 0));
2353 delta2
= DELTA2_FROM_PTRMEMFUNC (function
);
2355 /* convert down to the right base, before using the instance. */
2357 = convert_pointer_to_real (TYPE_METHOD_BASETYPE (TREE_TYPE (fntype
)),
2359 if (instance
== error_mark_node
)
2362 vtbl
= convert_pointer_to (ptr_type_node
, instance
);
2365 build_pointer_type (build_pointer_type (vtable_entry_type
)),
2366 vtbl
, convert (ptrdiff_type_node
, delta2
));
2367 vtbl
= build_indirect_ref (vtbl
, NULL_PTR
);
2368 aref
= build_array_ref (vtbl
, build_binary_op (MINUS_EXPR
,
2370 integer_one_node
, 1));
2371 if (! flag_vtable_thunks
)
2373 aref
= save_expr (aref
);
2375 /* Save the intermediate result in a SAVE_EXPR so we don't have to
2376 compute each component of the virtual function pointer twice. */
2377 if (TREE_CODE (aref
) == INDIRECT_REF
)
2378 TREE_OPERAND (aref
, 0) = save_expr (TREE_OPERAND (aref
, 0));
2380 delta
= build_binary_op (PLUS_EXPR
,
2381 build_conditional_expr (e1
, build_component_ref (aref
, delta_identifier
, 0, 0), integer_zero_node
),
2385 *instance_ptrptr
= build (PLUS_EXPR
, TREE_TYPE (instance_ptr
),
2386 instance_ptr
, delta
);
2387 if (flag_vtable_thunks
)
2390 e2
= build_component_ref (aref
, pfn_identifier
, 0, 0);
2392 e3
= PFN_FROM_PTRMEMFUNC (function
);
2393 TREE_TYPE (e2
) = TREE_TYPE (e3
);
2394 function
= build_conditional_expr (e1
, e2
, e3
);
2396 /* Make sure this doesn't get evaluated first inside one of the
2397 branches of the COND_EXPR. */
2398 if (TREE_CODE (instance_ptr
) == SAVE_EXPR
)
2399 function
= build (COMPOUND_EXPR
, TREE_TYPE (function
),
2400 instance_ptr
, function
);
2406 build_function_call_real (function
, params
, require_complete
, flags
)
2407 tree function
, params
;
2408 int require_complete
, flags
;
2410 register tree fntype
, fndecl
;
2411 register tree value_type
;
2412 register tree coerced_params
;
2413 tree name
= NULL_TREE
, assembler_name
= NULL_TREE
;
2416 /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
2417 Strip such NOP_EXPRs, since FUNCTION is used in non-lvalue context. */
2418 if (TREE_CODE (function
) == NOP_EXPR
2419 && TREE_TYPE (function
) == TREE_TYPE (TREE_OPERAND (function
, 0)))
2420 function
= TREE_OPERAND (function
, 0);
2422 if (TREE_CODE (function
) == FUNCTION_DECL
)
2424 name
= DECL_NAME (function
);
2425 assembler_name
= DECL_ASSEMBLER_NAME (function
);
2427 GNU_xref_call (current_function_decl
,
2428 IDENTIFIER_POINTER (name
? name
2429 : TYPE_IDENTIFIER (DECL_CLASS_CONTEXT (function
))));
2430 mark_used (function
);
2433 /* Convert anything with function type to a pointer-to-function. */
2436 && IDENTIFIER_LENGTH (name
) == 4
2437 && ! strcmp (IDENTIFIER_POINTER (name
), "main")
2438 && DECL_CONTEXT (function
) == NULL_TREE
)
2440 pedwarn ("ANSI C++ forbids calling `main' from within program");
2443 if (pedantic
&& DECL_THIS_INLINE (function
) && ! DECL_INITIAL (function
)
2444 && ! DECL_ARTIFICIAL (function
)
2445 && ! DECL_PENDING_INLINE_INFO (function
))
2446 cp_pedwarn ("inline function `%#D' called before definition",
2449 /* Differs from default_conversion by not setting TREE_ADDRESSABLE
2450 (because calling an inline function does not mean the function
2451 needs to be separately compiled). */
2453 if (DECL_INLINE (function
))
2455 /* Is it a synthesized method that needs to be synthesized? */
2456 if (DECL_ARTIFICIAL (function
) && ! DECL_INITIAL (function
)
2457 /* Kludge: don't synthesize for default args. */
2458 && current_function_decl
)
2459 synthesize_method (function
);
2461 fntype
= build_type_variant (TREE_TYPE (function
),
2462 TREE_READONLY (function
),
2463 TREE_THIS_VOLATILE (function
));
2464 function
= build1 (ADDR_EXPR
, build_pointer_type (fntype
), function
);
2467 function
= default_conversion (function
);
2473 /* Convert anything with function type to a pointer-to-function. */
2474 if (function
== error_mark_node
)
2475 return error_mark_node
;
2476 function
= default_conversion (function
);
2479 fntype
= TREE_TYPE (function
);
2481 if (TYPE_PTRMEMFUNC_P (fntype
))
2483 tree instance_ptr
= build_unary_op (ADDR_EXPR
, C_C_D
, 0);
2484 fntype
= TYPE_PTRMEMFUNC_FN_TYPE (fntype
);
2485 function
= get_member_function_from_ptrfunc (&instance_ptr
, function
);
2488 is_method
= (TREE_CODE (fntype
) == POINTER_TYPE
2489 && TREE_CODE (TREE_TYPE (fntype
)) == METHOD_TYPE
);
2491 if (!((TREE_CODE (fntype
) == POINTER_TYPE
2492 && TREE_CODE (TREE_TYPE (fntype
)) == FUNCTION_TYPE
)
2495 cp_error ("`%E' cannot be used as a function", function
);
2496 return error_mark_node
;
2499 /* fntype now gets the type of function pointed to. */
2500 fntype
= TREE_TYPE (fntype
);
2502 /* Convert the parameters to the types declared in the
2503 function prototype, or apply default promotions. */
2505 if (flags
& LOOKUP_COMPLAIN
)
2506 coerced_params
= convert_arguments (NULL_TREE
, TYPE_ARG_TYPES (fntype
),
2507 params
, fndecl
, LOOKUP_NORMAL
);
2509 coerced_params
= convert_arguments (NULL_TREE
, TYPE_ARG_TYPES (fntype
),
2512 if (coerced_params
== error_mark_node
)
2513 if (flags
& LOOKUP_SPECULATIVELY
)
2516 return error_mark_node
;
2518 /* Check for errors in format strings. */
2520 if (warn_format
&& (name
|| assembler_name
))
2521 check_function_format (name
, assembler_name
, coerced_params
);
2523 /* Recognize certain built-in functions so we can make tree-codes
2524 other than CALL_EXPR. We do this when it enables fold-const.c
2525 to do something useful. */
2527 if (TREE_CODE (function
) == ADDR_EXPR
2528 && TREE_CODE (TREE_OPERAND (function
, 0)) == FUNCTION_DECL
2529 && DECL_BUILT_IN (TREE_OPERAND (function
, 0)))
2530 switch (DECL_FUNCTION_CODE (TREE_OPERAND (function
, 0)))
2535 if (coerced_params
== 0)
2536 return integer_zero_node
;
2537 return build_unary_op (ABS_EXPR
, TREE_VALUE (coerced_params
), 0);
2541 value_type
= TREE_TYPE (fntype
) ? TREE_TYPE (fntype
) : void_type_node
;
2543 register tree result
=
2544 build (CALL_EXPR
, value_type
,
2545 function
, coerced_params
, NULL_TREE
);
2547 TREE_SIDE_EFFECTS (result
) = 1;
2549 if (! require_complete
)
2550 return convert_from_reference (result
);
2551 if (value_type
== void_type_node
)
2553 result
= require_complete_type (result
);
2554 return convert_from_reference (result
);
2559 build_function_call (function
, params
)
2560 tree function
, params
;
2562 return build_function_call_real (function
, params
, 1, LOOKUP_NORMAL
);
2565 /* Convert the actual parameter expressions in the list VALUES
2566 to the types in the list TYPELIST.
2567 If parmdecls is exhausted, or when an element has NULL as its type,
2568 perform the default conversions.
2570 RETURN_LOC is the location of the return value, if known, NULL_TREE
2571 otherwise. This is useful in the case where we can avoid creating
2572 a temporary variable in the case where we can initialize the return
2573 value directly. If we are not eliding constructors, then we set this
2574 to NULL_TREE to avoid this avoidance.
2576 NAME is an IDENTIFIER_NODE or 0. It is used only for error messages.
2578 This is also where warnings about wrong number of args are generated.
2580 Return a list of expressions for the parameters as converted.
2582 Both VALUES and the returned value are chains of TREE_LIST nodes
2583 with the elements of the list in the TREE_VALUE slots of those nodes.
2585 In C++, unspecified trailing parameters can be filled in with their
2586 default arguments, if such were specified. Do so here. */
2589 convert_arguments (return_loc
, typelist
, values
, fndecl
, flags
)
2590 tree return_loc
, typelist
, values
, fndecl
;
2593 register tree typetail
, valtail
;
2594 register tree result
= NULL_TREE
;
2598 if (! flag_elide_constructors
)
2603 if (TREE_CODE (TREE_TYPE (fndecl
)) == METHOD_TYPE
)
2605 if (DECL_NAME (fndecl
) == NULL_TREE
2606 || IDENTIFIER_HAS_TYPE_VALUE (DECL_NAME (fndecl
)))
2607 called_thing
= "constructor";
2609 called_thing
= "member function";
2612 called_thing
= "function";
2615 for (valtail
= values
, typetail
= typelist
;
2617 valtail
= TREE_CHAIN (valtail
), i
++)
2619 register tree type
= typetail
? TREE_VALUE (typetail
) : 0;
2620 register tree val
= TREE_VALUE (valtail
);
2622 if (val
== error_mark_node
)
2623 return error_mark_node
;
2625 if (type
== void_type_node
)
2629 char *buf
= (char *)alloca (40 + strlen (called_thing
));
2630 sprintf (buf
, "too many arguments to %s `%%s'", called_thing
);
2631 error_with_decl (fndecl
, buf
);
2632 error ("at this point in file");
2635 error ("too many arguments to function");
2636 /* In case anybody wants to know if this argument
2639 TREE_TYPE (tree_last (result
)) = error_mark_node
;
2643 /* The tree type of the parameter being passed may not yet be
2644 known. In this case, its type is TYPE_UNKNOWN, and will
2645 be instantiated by the type given by TYPE. If TYPE
2646 is also NULL, the tree type of VAL is ERROR_MARK_NODE. */
2647 if (type
&& type_unknown_p (val
))
2648 val
= require_instantiated_type (type
, val
, integer_zero_node
);
2649 else if (type_unknown_p (val
))
2651 /* Strip the `&' from an overloaded FUNCTION_DECL. */
2652 if (TREE_CODE (val
) == ADDR_EXPR
)
2653 val
= TREE_OPERAND (val
, 0);
2654 if (TREE_CODE (val
) == TREE_LIST
2655 && TREE_CHAIN (val
) == NULL_TREE
2656 && TREE_TYPE (TREE_VALUE (val
)) != NULL_TREE
2657 && (TREE_TYPE (val
) == unknown_type_node
2658 || DECL_CHAIN (TREE_VALUE (val
)) == NULL_TREE
))
2659 /* Instantiates automatically. */
2660 val
= TREE_VALUE (val
);
2663 error ("insufficient type information in parameter list");
2664 val
= integer_zero_node
;
2667 else if (TREE_CODE (val
) == OFFSET_REF
2668 && TREE_CODE (TREE_TYPE (val
)) == METHOD_TYPE
)
2670 /* This is unclean. Should be handled elsewhere. */
2671 val
= build_unary_op (ADDR_EXPR
, val
, 0);
2673 else if (TREE_CODE (val
) == OFFSET_REF
)
2674 val
= resolve_offset_ref (val
);
2676 /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
2677 Strip such NOP_EXPRs, since VAL is used in non-lvalue context. */
2678 if (TREE_CODE (val
) == NOP_EXPR
2679 && TREE_TYPE (val
) == TREE_TYPE (TREE_OPERAND (val
, 0))
2680 && (type
== 0 || TREE_CODE (type
) != REFERENCE_TYPE
))
2681 val
= TREE_OPERAND (val
, 0);
2683 if (type
== 0 || TREE_CODE (type
) != REFERENCE_TYPE
)
2685 if (TREE_CODE (TREE_TYPE (val
)) == ARRAY_TYPE
2686 || TREE_CODE (TREE_TYPE (val
)) == FUNCTION_TYPE
2687 || TREE_CODE (TREE_TYPE (val
)) == METHOD_TYPE
)
2688 val
= default_conversion (val
);
2690 val
= require_complete_type (val
);
2693 if (val
== error_mark_node
)
2694 return error_mark_node
;
2698 /* Formal parm type is specified by a function prototype. */
2701 if (TYPE_SIZE (type
) == 0)
2703 error ("parameter type of called function is incomplete");
2708 parmval
= convert_for_initialization (return_loc
, type
, val
,
2709 flags
|INDIRECT_BIND
,
2710 "argument passing", fndecl
, i
);
2711 #ifdef PROMOTE_PROTOTYPES
2712 if ((TREE_CODE (type
) == INTEGER_TYPE
2713 || TREE_CODE (type
) == ENUMERAL_TYPE
)
2714 && (TYPE_PRECISION (type
) < TYPE_PRECISION (integer_type_node
)))
2715 parmval
= default_conversion (parmval
);
2719 if (parmval
== error_mark_node
)
2720 return error_mark_node
;
2722 result
= tree_cons (NULL_TREE
, parmval
, result
);
2726 if (TREE_CODE (TREE_TYPE (val
)) == REFERENCE_TYPE
)
2727 val
= convert_from_reference (val
);
2729 if (TREE_CODE (TREE_TYPE (val
)) == REAL_TYPE
2730 && (TYPE_PRECISION (TREE_TYPE (val
))
2731 < TYPE_PRECISION (double_type_node
)))
2732 /* Convert `float' to `double'. */
2733 result
= tree_cons (NULL_TREE
, convert (double_type_node
, val
), result
);
2734 else if (TYPE_LANG_SPECIFIC (TREE_TYPE (val
))
2735 && ! TYPE_HAS_TRIVIAL_INIT_REF (TREE_TYPE (val
)))
2737 cp_warning ("cannot pass objects of type `%T' through `...'",
2739 result
= tree_cons (NULL_TREE
, val
, result
);
2742 /* Convert `short' and `char' to full-size `int'. */
2743 result
= tree_cons (NULL_TREE
, default_conversion (val
), result
);
2747 typetail
= TREE_CHAIN (typetail
);
2750 if (typetail
!= 0 && typetail
!= void_list_node
)
2752 /* See if there are default arguments that can be used */
2753 if (TREE_PURPOSE (typetail
))
2755 for (; typetail
!= void_list_node
; ++i
)
2757 tree type
= TREE_VALUE (typetail
);
2758 tree val
= break_out_target_exprs (TREE_PURPOSE (typetail
));
2761 if (val
== NULL_TREE
)
2762 parmval
= error_mark_node
;
2763 else if (TREE_CODE (val
) == CONSTRUCTOR
)
2765 parmval
= digest_init (type
, val
, (tree
*)0);
2766 parmval
= convert_for_initialization (return_loc
, type
, parmval
, flags
,
2767 "default constructor", fndecl
, i
);
2771 /* This could get clobbered by the following call. */
2772 if (TREE_HAS_CONSTRUCTOR (val
))
2773 val
= copy_node (val
);
2775 parmval
= convert_for_initialization (return_loc
, type
, val
, flags
,
2776 "default argument", fndecl
, i
);
2777 #ifdef PROMOTE_PROTOTYPES
2778 if ((TREE_CODE (type
) == INTEGER_TYPE
2779 || TREE_CODE (type
) == ENUMERAL_TYPE
)
2780 && (TYPE_PRECISION (type
) < TYPE_PRECISION (integer_type_node
)))
2781 parmval
= default_conversion (parmval
);
2785 if (parmval
== error_mark_node
)
2786 return error_mark_node
;
2788 result
= tree_cons (0, parmval
, result
);
2789 typetail
= TREE_CHAIN (typetail
);
2790 /* ends with `...'. */
2791 if (typetail
== NULL_TREE
)
2799 char *buf
= (char *)alloca (32 + strlen (called_thing
));
2800 sprintf (buf
, "too few arguments to %s `%%#D'", called_thing
);
2801 cp_error_at (buf
, fndecl
);
2802 error ("at this point in file");
2805 error ("too few arguments to function");
2806 return error_mark_list
;
2810 return nreverse (result
);
2813 /* Build a binary-operation expression, after performing default
2814 conversions on the operands. CODE is the kind of expression to build. */
2817 build_x_binary_op (code
, arg1
, arg2
)
2818 enum tree_code code
;
2821 tree rval
= build_opfncall (code
, LOOKUP_SPECULATIVELY
,
2822 arg1
, arg2
, NULL_TREE
);
2824 return build_opfncall (code
, LOOKUP_NORMAL
, arg1
, arg2
, NULL_TREE
);
2825 if (code
== MEMBER_REF
)
2826 return build_m_component_ref (build_indirect_ref (arg1
, NULL_PTR
),
2828 return build_binary_op (code
, arg1
, arg2
, 1);
2832 build_binary_op (code
, arg1
, arg2
, convert_p
)
2833 enum tree_code code
;
2846 args
[0] = decay_conversion (args
[0]);
2847 args
[1] = decay_conversion (args
[1]);
2849 if (args
[0] == error_mark_node
|| args
[1] == error_mark_node
)
2850 return error_mark_node
;
2852 type0
= TREE_TYPE (args
[0]);
2853 type1
= TREE_TYPE (args
[1]);
2855 if (type_unknown_p (args
[0]))
2857 args
[0] = instantiate_type (type1
, args
[0], 1);
2858 args
[0] = decay_conversion (args
[0]);
2860 else if (type_unknown_p (args
[1]))
2862 args
[1] = require_instantiated_type (type0
, args
[1],
2864 args
[1] = decay_conversion (args
[1]);
2867 if (IS_AGGR_TYPE (type0
) || IS_AGGR_TYPE (type1
))
2869 /* Try to convert this to something reasonable. */
2870 if (! build_default_binary_type_conversion(code
, &args
[0], &args
[1]))
2872 cp_error ("no match for `%O(%#T, %#T)'", code
,
2873 TREE_TYPE (arg1
), TREE_TYPE (arg2
));
2874 return error_mark_node
;
2878 return build_binary_op_nodefault (code
, args
[0], args
[1], code
);
2881 /* Build a binary-operation expression without default conversions.
2882 CODE is the kind of expression to build.
2883 This function differs from `build' in several ways:
2884 the data type of the result is computed and recorded in it,
2885 warnings are generated if arg data types are invalid,
2886 special handling for addition and subtraction of pointers is known,
2887 and some optimization is done (operations on narrow ints
2888 are done in the narrower type when that gives the same result).
2889 Constant folding is also done before the result is returned.
2891 ERROR_CODE is the code that determines what to say in error messages.
2892 It is usually, but not always, the same as CODE.
2894 Note that the operands will never have enumeral types
2895 because either they have just had the default conversions performed
2896 or they have both just been converted to some other type in which
2897 the arithmetic is to be done.
2899 C++: must do special pointer arithmetic when implementing
2900 multiple inheritance, and deal with pointer to member functions. */
2903 build_binary_op_nodefault (code
, orig_op0
, orig_op1
, error_code
)
2904 enum tree_code code
;
2905 tree orig_op0
, orig_op1
;
2906 enum tree_code error_code
;
2909 register enum tree_code code0
, code1
;
2912 /* Expression code to give to the expression when it is built.
2913 Normally this is CODE, which is what the caller asked for,
2914 but in some special cases we change it. */
2915 register enum tree_code resultcode
= code
;
2917 /* Data type in which the computation is to be performed.
2918 In the simplest cases this is the common type of the arguments. */
2919 register tree result_type
= NULL
;
2921 /* Nonzero means operands have already been type-converted
2922 in whatever way is necessary.
2923 Zero means they need to be converted to RESULT_TYPE. */
2926 /* Nonzero means create the expression with this type, rather than
2928 tree build_type
= 0;
2930 /* Nonzero means after finally constructing the expression
2931 convert it to this type. */
2932 tree final_type
= 0;
2934 /* Nonzero if this is an operation like MIN or MAX which can
2935 safely be computed in short if both args are promoted shorts.
2936 Also implies COMMON.
2937 -1 indicates a bitwise operation; this makes a difference
2938 in the exact conditions for when it is safe to do the operation
2939 in a narrower mode. */
2942 /* Nonzero if this is a comparison operation;
2943 if both args are promoted shorts, compare the original shorts.
2944 Also implies COMMON. */
2945 int short_compare
= 0;
2947 /* Nonzero if this is a right-shift operation, which can be computed on the
2948 original short and then promoted if the operand is a promoted short. */
2949 int short_shift
= 0;
2951 /* Nonzero means set RESULT_TYPE to the common type of the args. */
2954 /* Apply default conversions. */
2955 if (code
== TRUTH_AND_EXPR
|| code
== TRUTH_ANDIF_EXPR
2956 || code
== TRUTH_OR_EXPR
|| code
== TRUTH_ORIF_EXPR
2957 || code
== TRUTH_XOR_EXPR
)
2959 op0
= decay_conversion (orig_op0
);
2960 op1
= decay_conversion (orig_op1
);
2964 op0
= default_conversion (orig_op0
);
2965 op1
= default_conversion (orig_op1
);
2968 type0
= TREE_TYPE (op0
);
2969 type1
= TREE_TYPE (op1
);
2971 /* The expression codes of the data types of the arguments tell us
2972 whether the arguments are integers, floating, pointers, etc. */
2973 code0
= TREE_CODE (type0
);
2974 code1
= TREE_CODE (type1
);
2976 /* Strip NON_LVALUE_EXPRs, etc., since we aren't using as an lvalue. */
2977 STRIP_TYPE_NOPS (op0
);
2978 STRIP_TYPE_NOPS (op1
);
2980 /* If an error was already reported for one of the arguments,
2981 avoid reporting another error. */
2983 if (code0
== ERROR_MARK
|| code1
== ERROR_MARK
)
2984 return error_mark_node
;
2989 /* Handle the pointer + int case. */
2990 if (code0
== POINTER_TYPE
&& code1
== INTEGER_TYPE
)
2991 return pointer_int_sum (PLUS_EXPR
, op0
, op1
);
2992 else if (code1
== POINTER_TYPE
&& code0
== INTEGER_TYPE
)
2993 return pointer_int_sum (PLUS_EXPR
, op1
, op0
);
2999 /* Subtraction of two similar pointers.
3000 We must subtract them as integers, then divide by object size. */
3001 if (code0
== POINTER_TYPE
&& code1
== POINTER_TYPE
3002 && comp_target_types (type0
, type1
, 1))
3003 return pointer_diff (op0
, op1
);
3004 /* Handle pointer minus int. Just like pointer plus int. */
3005 else if (code0
== POINTER_TYPE
&& code1
== INTEGER_TYPE
)
3006 return pointer_int_sum (MINUS_EXPR
, op0
, op1
);
3015 case TRUNC_DIV_EXPR
:
3017 case FLOOR_DIV_EXPR
:
3018 case ROUND_DIV_EXPR
:
3019 case EXACT_DIV_EXPR
:
3020 if ((code0
== INTEGER_TYPE
|| code0
== REAL_TYPE
)
3021 && (code1
== INTEGER_TYPE
|| code1
== REAL_TYPE
))
3023 if (TREE_CODE (op1
) == INTEGER_CST
&& integer_zerop (op1
))
3024 cp_warning ("division by zero in `%E / 0'", op0
);
3025 else if (TREE_CODE (op1
) == REAL_CST
&& real_zerop (op1
))
3026 cp_warning ("division by zero in `%E / 0.'", op0
);
3028 if (!(code0
== INTEGER_TYPE
&& code1
== INTEGER_TYPE
))
3029 resultcode
= RDIV_EXPR
;
3031 /* When dividing two signed integers, we have to promote to int.
3032 unless we divide by a constant != -1. Note that default
3033 conversion will have been performed on the operands at this
3034 point, so we have to dig out the original type to find out if
3036 shorten
= ((TREE_CODE (op0
) == NOP_EXPR
3037 && TREE_UNSIGNED (TREE_TYPE (TREE_OPERAND (op0
, 0))))
3038 || (TREE_CODE (op1
) == INTEGER_CST
3039 && (TREE_INT_CST_LOW (op1
) != -1
3040 || TREE_INT_CST_HIGH (op1
) != -1)));
3046 case BIT_ANDTC_EXPR
:
3049 if (code0
== INTEGER_TYPE
&& code1
== INTEGER_TYPE
)
3051 /* If one operand is a constant, and the other is a short type
3052 that has been converted to an int,
3053 really do the work in the short type and then convert the
3054 result to int. If we are lucky, the constant will be 0 or 1
3055 in the short type, making the entire operation go away. */
3056 if (TREE_CODE (op0
) == INTEGER_CST
3057 && TREE_CODE (op1
) == NOP_EXPR
3058 && TYPE_PRECISION (type1
) > TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (op1
, 0)))
3059 && TREE_UNSIGNED (TREE_TYPE (TREE_OPERAND (op1
, 0))))
3061 final_type
= result_type
;
3062 op1
= TREE_OPERAND (op1
, 0);
3063 result_type
= TREE_TYPE (op1
);
3065 if (TREE_CODE (op1
) == INTEGER_CST
3066 && TREE_CODE (op0
) == NOP_EXPR
3067 && TYPE_PRECISION (type0
) > TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (op0
, 0)))
3068 && TREE_UNSIGNED (TREE_TYPE (TREE_OPERAND (op0
, 0))))
3070 final_type
= result_type
;
3071 op0
= TREE_OPERAND (op0
, 0);
3072 result_type
= TREE_TYPE (op0
);
3076 case TRUNC_MOD_EXPR
:
3077 case FLOOR_MOD_EXPR
:
3078 if (code1
== INTEGER_TYPE
&& integer_zerop (op1
))
3079 cp_warning ("division by zero in `%E % 0'", op0
);
3080 else if (code1
== REAL_TYPE
&& real_zerop (op1
))
3081 cp_warning ("division by zero in `%E % 0.'", op0
);
3083 if (code0
== INTEGER_TYPE
&& code1
== INTEGER_TYPE
)
3085 /* Although it would be tempting to shorten always here, that loses
3086 on some targets, since the modulo instruction is undefined if the
3087 quotient can't be represented in the computation mode. We shorten
3088 only if unsigned or if dividing by something we know != -1. */
3089 shorten
= ((TREE_CODE (op0
) == NOP_EXPR
3090 && TREE_UNSIGNED (TREE_TYPE (TREE_OPERAND (op0
, 0))))
3091 || (TREE_CODE (op1
) == INTEGER_CST
3092 && (TREE_INT_CST_LOW (op1
) != -1
3093 || TREE_INT_CST_HIGH (op1
) != -1)));
3098 case TRUTH_ANDIF_EXPR
:
3099 case TRUTH_ORIF_EXPR
:
3100 case TRUTH_AND_EXPR
:
3102 result_type
= boolean_type_node
;
3105 /* Shift operations: result has same type as first operand;
3106 always convert second operand to int.
3107 Also set SHORT_SHIFT if shifting rightward. */
3110 if (code0
== INTEGER_TYPE
&& code1
== INTEGER_TYPE
)
3112 result_type
= type0
;
3113 if (TREE_CODE (op1
) == INTEGER_CST
)
3115 if (tree_int_cst_lt (op1
, integer_zero_node
))
3116 warning ("right shift count is negative");
3119 if (TREE_INT_CST_LOW (op1
) | TREE_INT_CST_HIGH (op1
))
3121 if (TREE_INT_CST_HIGH (op1
) != 0
3122 || ((unsigned HOST_WIDE_INT
) TREE_INT_CST_LOW (op1
)
3123 >= TYPE_PRECISION (type0
)))
3124 warning ("right shift count >= width of type");
3127 /* Convert the shift-count to an integer, regardless of
3128 size of value being shifted. */
3129 if (TYPE_MAIN_VARIANT (TREE_TYPE (op1
)) != integer_type_node
)
3130 op1
= convert (integer_type_node
, op1
);
3131 /* Avoid converting op1 to result_type later. */
3137 if (code0
== INTEGER_TYPE
&& code1
== INTEGER_TYPE
)
3139 result_type
= type0
;
3140 if (TREE_CODE (op1
) == INTEGER_CST
)
3142 if (tree_int_cst_lt (op1
, integer_zero_node
))
3143 warning ("left shift count is negative");
3144 else if (TREE_INT_CST_HIGH (op1
) != 0
3145 || ((unsigned HOST_WIDE_INT
) TREE_INT_CST_LOW (op1
)
3146 >= TYPE_PRECISION (type0
)))
3147 warning ("left shift count >= width of type");
3149 /* Convert the shift-count to an integer, regardless of
3150 size of value being shifted. */
3151 if (TYPE_MAIN_VARIANT (TREE_TYPE (op1
)) != integer_type_node
)
3152 op1
= convert (integer_type_node
, op1
);
3153 /* Avoid converting op1 to result_type later. */
3160 if (code0
== INTEGER_TYPE
&& code1
== INTEGER_TYPE
)
3162 result_type
= type0
;
3163 if (TREE_CODE (op1
) == INTEGER_CST
)
3165 if (tree_int_cst_lt (op1
, integer_zero_node
))
3166 warning ("%s rotate count is negative",
3167 (code
== LROTATE_EXPR
) ? "left" : "right");
3168 else if (TREE_INT_CST_HIGH (op1
) != 0
3169 || ((unsigned HOST_WIDE_INT
) TREE_INT_CST_LOW (op1
)
3170 >= TYPE_PRECISION (type0
)))
3171 warning ("%s rotate count >= width of type",
3172 (code
== LROTATE_EXPR
) ? "left" : "right");
3174 /* Convert the shift-count to an integer, regardless of
3175 size of value being shifted. */
3176 if (TYPE_MAIN_VARIANT (TREE_TYPE (op1
)) != integer_type_node
)
3177 op1
= convert (integer_type_node
, op1
);
3183 build_type
= boolean_type_node
;
3184 if ((code0
== INTEGER_TYPE
|| code0
== REAL_TYPE
)
3185 && (code1
== INTEGER_TYPE
|| code1
== REAL_TYPE
))
3187 else if (code0
== POINTER_TYPE
&& code1
== POINTER_TYPE
)
3189 register tree tt0
= TYPE_MAIN_VARIANT (TREE_TYPE (type0
));
3190 register tree tt1
= TYPE_MAIN_VARIANT (TREE_TYPE (type1
));
3192 if (comp_target_types (type0
, type1
, 1))
3193 result_type
= common_type (type0
, type1
);
3194 else if (tt0
== void_type_node
)
3196 if (pedantic
&& TREE_CODE (tt1
) == FUNCTION_TYPE
3197 && tree_int_cst_lt (TYPE_SIZE (type0
), TYPE_SIZE (type1
)))
3198 pedwarn ("ANSI C++ forbids comparison of `void *' with function pointer");
3199 else if (TREE_CODE (tt1
) == OFFSET_TYPE
)
3200 pedwarn ("ANSI C++ forbids conversion of a pointer to member to `void *'");
3202 else if (tt1
== void_type_node
)
3204 if (pedantic
&& TREE_CODE (tt0
) == FUNCTION_TYPE
3205 && tree_int_cst_lt (TYPE_SIZE (type1
), TYPE_SIZE (type0
)))
3206 pedwarn ("ANSI C++ forbids comparison of `void *' with function pointer");
3209 cp_pedwarn ("comparison of distinct pointer types `%T' and `%T' lacks a cast",
3212 if (result_type
== NULL_TREE
)
3213 result_type
= ptr_type_node
;
3215 else if (code0
== POINTER_TYPE
&& TREE_CODE (op1
) == INTEGER_CST
3216 && integer_zerop (op1
))
3217 result_type
= type0
;
3218 else if (code1
== POINTER_TYPE
&& TREE_CODE (op0
) == INTEGER_CST
3219 && integer_zerop (op0
))
3220 result_type
= type1
;
3221 else if (code0
== POINTER_TYPE
&& code1
== INTEGER_TYPE
)
3223 result_type
= type0
;
3224 error ("ANSI C++ forbids comparison between pointer and integer");
3226 else if (code0
== INTEGER_TYPE
&& code1
== POINTER_TYPE
)
3228 result_type
= type1
;
3229 error ("ANSI C++ forbids comparison between pointer and integer");
3231 else if (TYPE_PTRMEMFUNC_P (type0
) && TREE_CODE (op1
) == INTEGER_CST
3232 && integer_zerop (op1
))
3234 op0
= build_component_ref (op0
, index_identifier
, 0, 0);
3235 op1
= integer_zero_node
;
3236 result_type
= TREE_TYPE (op0
);
3238 else if (TYPE_PTRMEMFUNC_P (type1
) && TREE_CODE (op0
) == INTEGER_CST
3239 && integer_zerop (op0
))
3241 op0
= build_component_ref (op1
, index_identifier
, 0, 0);
3242 op1
= integer_zero_node
;
3243 result_type
= TREE_TYPE (op0
);
3245 else if (TYPE_PTRMEMFUNC_P (type0
) && TYPE_PTRMEMFUNC_P (type1
)
3246 && (TYPE_PTRMEMFUNC_FN_TYPE (type0
)
3247 == TYPE_PTRMEMFUNC_FN_TYPE (type1
)))
3249 /* The code we generate for the test is:
3251 (op0.index == op1.index
3252 && ((op1.index != -1 && op0.delta2 == op1.delta2)
3253 || op0.pfn == op1.pfn)) */
3255 tree index0
= build_component_ref (op0
, index_identifier
, 0, 0);
3256 tree index1
= save_expr (build_component_ref (op1
, index_identifier
, 0, 0));
3257 tree pfn0
= PFN_FROM_PTRMEMFUNC (op0
);
3258 tree pfn1
= PFN_FROM_PTRMEMFUNC (op1
);
3259 tree delta20
= DELTA2_FROM_PTRMEMFUNC (op0
);
3260 tree delta21
= DELTA2_FROM_PTRMEMFUNC (op1
);
3262 tree integer_neg_one_node
3263 = build_binary_op (MINUS_EXPR
, integer_zero_node
, integer_one_node
, 1);
3264 e1
= build_binary_op (EQ_EXPR
, index0
, index1
, 1);
3265 e2
= build_binary_op (NE_EXPR
, index1
, integer_neg_one_node
, 1);
3266 e2
= build_binary_op (TRUTH_ANDIF_EXPR
, e2
, build_binary_op (EQ_EXPR
, delta20
, delta21
, 1), 1);
3267 e3
= build_binary_op (EQ_EXPR
, pfn0
, pfn1
, 1);
3268 e2
= build_binary_op (TRUTH_ORIF_EXPR
, e2
, e3
, 1);
3269 e2
= build_binary_op (TRUTH_ANDIF_EXPR
, e1
, e2
, 1);
3270 if (code
== EQ_EXPR
)
3272 return build_binary_op (EQ_EXPR
, e2
, integer_zero_node
, 1);
3274 else if (TYPE_PTRMEMFUNC_P (type0
)
3275 && TYPE_PTRMEMFUNC_FN_TYPE (type0
) == type1
)
3277 tree index0
= build_component_ref (op0
, index_identifier
, 0, 0);
3279 tree pfn0
= PFN_FROM_PTRMEMFUNC (op0
);
3280 tree delta20
= DELTA2_FROM_PTRMEMFUNC (op0
);
3281 tree delta21
= integer_zero_node
;
3283 tree integer_neg_one_node
3284 = build_binary_op (MINUS_EXPR
, integer_zero_node
, integer_one_node
, 1);
3285 if (TREE_CODE (TREE_OPERAND (op1
, 0)) == FUNCTION_DECL
3286 && DECL_VINDEX (TREE_OPERAND (op1
, 0)))
3288 /* Map everything down one to make room for the null pointer to member. */
3289 index1
= size_binop (PLUS_EXPR
,
3290 DECL_VINDEX (TREE_OPERAND (op1
, 0)),
3292 op1
= integer_zero_node
;
3293 delta21
= CLASSTYPE_VFIELD (TYPE_METHOD_BASETYPE (TREE_TYPE (type1
)));
3294 delta21
= DECL_FIELD_BITPOS (delta21
);
3295 delta21
= size_binop (FLOOR_DIV_EXPR
, delta21
, size_int (BITS_PER_UNIT
));
3298 index1
= integer_neg_one_node
;
3300 tree nop1
= build1 (NOP_EXPR
, TYPE_PTRMEMFUNC_FN_TYPE (type0
), op1
);
3301 TREE_CONSTANT (nop1
) = TREE_CONSTANT (op1
);
3304 e1
= build_binary_op (EQ_EXPR
, index0
, index1
, 1);
3305 e2
= build_binary_op (NE_EXPR
, index1
, integer_neg_one_node
, 1);
3306 e2
= build_binary_op (TRUTH_ANDIF_EXPR
, e2
, build_binary_op (EQ_EXPR
, delta20
, delta21
, 1), 1);
3307 e3
= build_binary_op (EQ_EXPR
, pfn0
, op1
, 1);
3308 e2
= build_binary_op (TRUTH_ORIF_EXPR
, e2
, e3
, 1);
3309 e2
= build_binary_op (TRUTH_ANDIF_EXPR
, e1
, e2
, 1);
3310 if (code
== EQ_EXPR
)
3312 return build_binary_op (EQ_EXPR
, e2
, integer_zero_node
, 1);
3314 else if (TYPE_PTRMEMFUNC_P (type1
)
3315 && TYPE_PTRMEMFUNC_FN_TYPE (type1
) == type0
)
3317 return build_binary_op (code
, op1
, op0
, 1);
3323 if ((code0
== INTEGER_TYPE
|| code0
== REAL_TYPE
)
3324 && (code1
== INTEGER_TYPE
|| code1
== REAL_TYPE
))
3326 else if (code0
== POINTER_TYPE
&& code1
== POINTER_TYPE
)
3328 if (comp_target_types (type0
, type1
, 1))
3329 result_type
= common_type (type0
, type1
);
3332 cp_pedwarn ("comparison of distinct pointer types `%T' and `%T' lacks a cast",
3334 result_type
= ptr_type_node
;
3343 build_type
= boolean_type_node
;
3344 if ((code0
== INTEGER_TYPE
|| code0
== REAL_TYPE
)
3345 && (code1
== INTEGER_TYPE
|| code1
== REAL_TYPE
))
3347 else if (code0
== POINTER_TYPE
&& code1
== POINTER_TYPE
)
3349 if (comp_target_types (type0
, type1
, 1))
3350 result_type
= common_type (type0
, type1
);
3353 cp_pedwarn ("comparison of distinct pointer types `%T' and `%T' lacks a cast",
3355 result_type
= ptr_type_node
;
3358 else if (code0
== POINTER_TYPE
&& TREE_CODE (op1
) == INTEGER_CST
3359 && integer_zerop (op1
))
3360 result_type
= type0
;
3361 else if (code1
== POINTER_TYPE
&& TREE_CODE (op0
) == INTEGER_CST
3362 && integer_zerop (op0
))
3363 result_type
= type1
;
3364 else if (code0
== POINTER_TYPE
&& code1
== INTEGER_TYPE
)
3366 result_type
= type0
;
3368 pedwarn ("ANSI C++ forbids comparison between pointer and integer");
3369 else if (! flag_traditional
)
3370 warning ("comparison between pointer and integer");
3372 else if (code0
== INTEGER_TYPE
&& code1
== POINTER_TYPE
)
3374 result_type
= type1
;
3376 pedwarn ("ANSI C++ forbids comparison between pointer and integer");
3377 else if (! flag_traditional
)
3378 warning ("comparison between pointer and integer");
3383 if ((code0
== INTEGER_TYPE
|| code0
== REAL_TYPE
)
3384 && (code1
== INTEGER_TYPE
|| code1
== REAL_TYPE
))
3386 if (shorten
|| common
|| short_compare
)
3387 result_type
= common_type (type0
, type1
);
3389 /* For certain operations (which identify themselves by shorten != 0)
3390 if both args were extended from the same smaller type,
3391 do the arithmetic in that type and then extend.
3393 shorten !=0 and !=1 indicates a bitwise operation.
3394 For them, this optimization is safe only if
3395 both args are zero-extended or both are sign-extended.
3396 Otherwise, we might change the result.
3397 Eg, (short)-1 | (unsigned short)-1 is (int)-1
3398 but calculated in (unsigned short) it would be (unsigned short)-1. */
3402 int unsigned0
, unsigned1
;
3403 tree arg0
= get_narrower (op0
, &unsigned0
);
3404 tree arg1
= get_narrower (op1
, &unsigned1
);
3405 /* UNS is 1 if the operation to be done is an unsigned one. */
3406 int uns
= TREE_UNSIGNED (result_type
);
3409 final_type
= result_type
;
3411 /* Handle the case that OP0 does not *contain* a conversion
3412 but it *requires* conversion to FINAL_TYPE. */
3414 if (op0
== arg0
&& TREE_TYPE (op0
) != final_type
)
3415 unsigned0
= TREE_UNSIGNED (TREE_TYPE (op0
));
3416 if (op1
== arg1
&& TREE_TYPE (op1
) != final_type
)
3417 unsigned1
= TREE_UNSIGNED (TREE_TYPE (op1
));
3419 /* Now UNSIGNED0 is 1 if ARG0 zero-extends to FINAL_TYPE. */
3421 /* For bitwise operations, signedness of nominal type
3422 does not matter. Consider only how operands were extended. */
3426 /* Note that in all three cases below we refrain from optimizing
3427 an unsigned operation on sign-extended args.
3428 That would not be valid. */
3430 /* Both args variable: if both extended in same way
3431 from same width, do it in that width.
3432 Do it unsigned if args were zero-extended. */
3433 if ((TYPE_PRECISION (TREE_TYPE (arg0
))
3434 < TYPE_PRECISION (result_type
))
3435 && (TYPE_PRECISION (TREE_TYPE (arg1
))
3436 == TYPE_PRECISION (TREE_TYPE (arg0
)))
3437 && unsigned0
== unsigned1
3438 && (unsigned0
|| !uns
))
3440 = signed_or_unsigned_type (unsigned0
,
3441 common_type (TREE_TYPE (arg0
), TREE_TYPE (arg1
)));
3442 else if (TREE_CODE (arg0
) == INTEGER_CST
3443 && (unsigned1
|| !uns
)
3444 && (TYPE_PRECISION (TREE_TYPE (arg1
))
3445 < TYPE_PRECISION (result_type
))
3446 && (type
= signed_or_unsigned_type (unsigned1
,
3448 int_fits_type_p (arg0
, type
)))
3450 else if (TREE_CODE (arg1
) == INTEGER_CST
3451 && (unsigned0
|| !uns
)
3452 && (TYPE_PRECISION (TREE_TYPE (arg0
))
3453 < TYPE_PRECISION (result_type
))
3454 && (type
= signed_or_unsigned_type (unsigned0
,
3456 int_fits_type_p (arg1
, type
)))
3460 /* Shifts can be shortened if shifting right. */
3465 tree arg0
= get_narrower (op0
, &unsigned_arg
);
3467 final_type
= result_type
;
3469 if (arg0
== op0
&& final_type
== TREE_TYPE (op0
))
3470 unsigned_arg
= TREE_UNSIGNED (TREE_TYPE (op0
));
3472 if (TYPE_PRECISION (TREE_TYPE (arg0
)) < TYPE_PRECISION (result_type
)
3473 /* We can shorten only if the shift count is less than the
3474 number of bits in the smaller type size. */
3475 && TREE_INT_CST_HIGH (op1
) == 0
3476 && TYPE_PRECISION (TREE_TYPE (arg0
)) > TREE_INT_CST_LOW (op1
)
3477 /* If arg is sign-extended and then unsigned-shifted,
3478 we can simulate this with a signed shift in arg's type
3479 only if the extended result is at least twice as wide
3480 as the arg. Otherwise, the shift could use up all the
3481 ones made by sign-extension and bring in zeros.
3482 We can't optimize that case at all, but in most machines
3483 it never happens because available widths are 2**N. */
3484 && (!TREE_UNSIGNED (final_type
)
3486 || ((unsigned) 2 * TYPE_PRECISION (TREE_TYPE (arg0
))
3487 <= TYPE_PRECISION (result_type
))))
3489 /* Do an unsigned shift if the operand was zero-extended. */
3491 = signed_or_unsigned_type (unsigned_arg
,
3493 /* Convert value-to-be-shifted to that type. */
3494 if (TREE_TYPE (op0
) != result_type
)
3495 op0
= convert (result_type
, op0
);
3500 /* Comparison operations are shortened too but differently.
3501 They identify themselves by setting short_compare = 1. */
3505 /* Don't write &op0, etc., because that would prevent op0
3506 from being kept in a register.
3507 Instead, make copies of the our local variables and
3508 pass the copies by reference, then copy them back afterward. */
3509 tree xop0
= op0
, xop1
= op1
, xresult_type
= result_type
;
3510 enum tree_code xresultcode
= resultcode
;
3512 = shorten_compare (&xop0
, &xop1
, &xresult_type
, &xresultcode
);
3514 return convert (boolean_type_node
, val
);
3515 op0
= xop0
, op1
= xop1
;
3517 resultcode
= xresultcode
;
3520 if (short_compare
&& warn_sign_compare
)
3522 int op0_signed
= ! TREE_UNSIGNED (TREE_TYPE (orig_op0
));
3523 int op1_signed
= ! TREE_UNSIGNED (TREE_TYPE (orig_op1
));
3525 int unsignedp0
, unsignedp1
;
3526 tree primop0
= get_narrower (op0
, &unsignedp0
);
3527 tree primop1
= get_narrower (op1
, &unsignedp1
);
3529 /* Check for comparison of different enum types. */
3530 if (flag_int_enum_equivalence
== 0
3531 && TREE_CODE (TREE_TYPE (orig_op0
)) == ENUMERAL_TYPE
3532 && TREE_CODE (TREE_TYPE (orig_op1
)) == ENUMERAL_TYPE
3533 && TYPE_MAIN_VARIANT (TREE_TYPE (orig_op0
))
3534 != TYPE_MAIN_VARIANT (TREE_TYPE (orig_op1
)))
3536 cp_warning ("comparison between `%#T' and `%#T'",
3537 TREE_TYPE (orig_op0
), TREE_TYPE (orig_op1
));
3540 /* Give warnings for comparisons between signed and unsigned
3541 quantities that may fail. */
3542 /* Do the checking based on the original operand trees, so that
3543 casts will be considered, but default promotions won't be. */
3545 /* Do not warn if the comparison is being done in a signed type,
3546 since the signed type will only be chosen if it can represent
3547 all the values of the unsigned type. */
3548 if (! TREE_UNSIGNED (result_type
))
3550 /* Do not warn if both operands are unsigned. */
3551 else if (op0_signed
== op1_signed
)
3553 /* Do not warn if the signed quantity is an unsuffixed
3554 integer literal (or some static constant expression
3555 involving such literals) and it is non-negative. */
3556 else if ((op0_signed
&& TREE_CODE (orig_op0
) == INTEGER_CST
3557 && tree_int_cst_sgn (orig_op0
) >= 0)
3558 || (op1_signed
&& TREE_CODE (orig_op1
) == INTEGER_CST
3559 && tree_int_cst_sgn (orig_op1
) >= 0))
3561 /* Do not warn if the comparison is an equality operation,
3562 the unsigned quantity is an integral constant and it does
3563 not use the most significant bit of result_type. */
3564 else if ((resultcode
== EQ_EXPR
|| resultcode
== NE_EXPR
)
3565 && ((op0_signed
&& TREE_CODE (orig_op1
) == INTEGER_CST
3566 && int_fits_type_p (orig_op1
, signed_type (result_type
))
3567 || (op1_signed
&& TREE_CODE (orig_op0
) == INTEGER_CST
3568 && int_fits_type_p (orig_op0
, signed_type (result_type
))))))
3571 warning ("comparison between signed and unsigned");
3573 /* Warn if two unsigned values are being compared in a size
3574 larger than their original size, and one (and only one) is the
3575 result of a `~' operator. This comparison will always fail.
3577 Also warn if one operand is a constant, and the constant does not
3578 have all bits set that are set in the ~ operand when it is
3581 if (TREE_CODE (primop0
) == BIT_NOT_EXPR
3582 ^ TREE_CODE (primop1
) == BIT_NOT_EXPR
)
3584 if (TREE_CODE (primop0
) == BIT_NOT_EXPR
)
3585 primop0
= get_narrower (TREE_OPERAND (op0
, 0), &unsignedp0
);
3586 if (TREE_CODE (primop1
) == BIT_NOT_EXPR
)
3587 primop1
= get_narrower (TREE_OPERAND (op1
, 0), &unsignedp1
);
3589 if (TREE_CODE (primop0
) == INTEGER_CST
3590 || TREE_CODE (primop1
) == INTEGER_CST
)
3593 HOST_WIDE_INT constant
, mask
;
3597 if (TREE_CODE (primop0
) == INTEGER_CST
)
3600 unsignedp
= unsignedp1
;
3601 constant
= TREE_INT_CST_LOW (primop0
);
3606 unsignedp
= unsignedp0
;
3607 constant
= TREE_INT_CST_LOW (primop1
);
3610 bits
= TYPE_PRECISION (TREE_TYPE (primop
));
3611 if (bits
< TYPE_PRECISION (result_type
)
3612 && bits
< HOST_BITS_PER_LONG
&& unsignedp
)
3614 mask
= (~ (HOST_WIDE_INT
) 0) << bits
;
3615 if ((mask
& constant
) != mask
)
3616 warning ("comparison of promoted ~unsigned with constant");
3619 else if (unsignedp0
&& unsignedp1
3620 && (TYPE_PRECISION (TREE_TYPE (primop0
))
3621 < TYPE_PRECISION (result_type
))
3622 && (TYPE_PRECISION (TREE_TYPE (primop1
))
3623 < TYPE_PRECISION (result_type
)))
3624 warning ("comparison of promoted ~unsigned with unsigned");
3629 /* At this point, RESULT_TYPE must be nonzero to avoid an error message.
3630 If CONVERTED is zero, both args will be converted to type RESULT_TYPE.
3631 Then the expression will be built.
3632 It will be given type FINAL_TYPE if that is nonzero;
3633 otherwise, it will be given type RESULT_TYPE. */
3637 cp_error ("invalid operands `%T' and `%T' to binary `%O'",
3638 TREE_TYPE (orig_op0
), TREE_TYPE (orig_op1
), error_code
);
3639 return error_mark_node
;
3644 if (TREE_TYPE (op0
) != result_type
)
3645 op0
= convert (result_type
, op0
);
3646 if (TREE_TYPE (op1
) != result_type
)
3647 op1
= convert (result_type
, op1
);
3650 if (build_type
== NULL_TREE
)
3651 build_type
= result_type
;
3654 register tree result
= build (resultcode
, build_type
, op0
, op1
);
3655 register tree folded
;
3657 folded
= fold (result
);
3658 if (folded
== result
)
3659 TREE_CONSTANT (folded
) = TREE_CONSTANT (op0
) & TREE_CONSTANT (op1
);
3660 if (final_type
!= 0)
3661 return convert (final_type
, folded
);
3666 /* Return a tree for the sum or difference (RESULTCODE says which)
3667 of pointer PTROP and integer INTOP. */
3670 pointer_int_sum (resultcode
, ptrop
, intop
)
3671 enum tree_code resultcode
;
3672 register tree ptrop
, intop
;
3676 register tree result
;
3677 register tree folded
= fold (intop
);
3679 /* The result is a pointer of the same type that is being added. */
3681 register tree result_type
= TREE_TYPE (ptrop
);
3683 if (TREE_CODE (TREE_TYPE (result_type
)) == VOID_TYPE
)
3685 if (pedantic
|| warn_pointer_arith
)
3686 pedwarn ("ANSI C++ forbids using pointer of type `void *' in arithmetic");
3687 size_exp
= integer_one_node
;
3689 else if (TREE_CODE (TREE_TYPE (result_type
)) == FUNCTION_TYPE
)
3691 if (pedantic
|| warn_pointer_arith
)
3692 pedwarn ("ANSI C++ forbids using pointer to a function in arithmetic");
3693 size_exp
= integer_one_node
;
3695 else if (TREE_CODE (TREE_TYPE (result_type
)) == METHOD_TYPE
)
3697 if (pedantic
|| warn_pointer_arith
)
3698 pedwarn ("ANSI C++ forbids using pointer to a method in arithmetic");
3699 size_exp
= integer_one_node
;
3701 else if (TREE_CODE (TREE_TYPE (result_type
)) == OFFSET_TYPE
)
3703 if (pedantic
|| warn_pointer_arith
)
3704 pedwarn ("ANSI C++ forbids using pointer to a member in arithmetic");
3705 size_exp
= integer_one_node
;
3708 size_exp
= size_in_bytes (TREE_TYPE (result_type
));
3710 /* Needed to make OOPS V2R3 work. */
3712 if (TREE_CODE (intop
) == INTEGER_CST
3713 && TREE_INT_CST_LOW (intop
) == 0
3714 && TREE_INT_CST_HIGH (intop
) == 0)
3717 /* If what we are about to multiply by the size of the elements
3718 contains a constant term, apply distributive law
3719 and multiply that constant term separately.
3720 This helps produce common subexpressions. */
3722 if ((TREE_CODE (intop
) == PLUS_EXPR
|| TREE_CODE (intop
) == MINUS_EXPR
)
3723 && ! TREE_CONSTANT (intop
)
3724 && TREE_CONSTANT (TREE_OPERAND (intop
, 1))
3725 && TREE_CONSTANT (size_exp
))
3727 enum tree_code subcode
= resultcode
;
3728 if (TREE_CODE (intop
) == MINUS_EXPR
)
3729 subcode
= (subcode
== PLUS_EXPR
? MINUS_EXPR
: PLUS_EXPR
);
3730 ptrop
= build_binary_op (subcode
, ptrop
, TREE_OPERAND (intop
, 1), 1);
3731 intop
= TREE_OPERAND (intop
, 0);
3734 /* Convert the integer argument to a type the same size as sizetype
3735 so the multiply won't overflow spuriously. */
3737 if (TYPE_PRECISION (TREE_TYPE (intop
)) != TYPE_PRECISION (sizetype
))
3738 intop
= convert (type_for_size (TYPE_PRECISION (sizetype
), 0), intop
);
3740 /* Replace the integer argument with a suitable product by the object size.
3741 Do this multiplication as signed, then convert to the appropriate
3742 pointer type (actually unsigned integral). */
3744 intop
= convert (result_type
,
3745 build_binary_op (MULT_EXPR
, intop
,
3746 convert (TREE_TYPE (intop
), size_exp
), 1));
3748 /* Create the sum or difference. */
3750 result
= build (resultcode
, result_type
, ptrop
, intop
);
3752 folded
= fold (result
);
3753 if (folded
== result
)
3754 TREE_CONSTANT (folded
) = TREE_CONSTANT (ptrop
) & TREE_CONSTANT (intop
);
3758 /* Return a tree for the difference of pointers OP0 and OP1.
3759 The resulting tree has type int. */
3762 pointer_diff (op0
, op1
)
3763 register tree op0
, op1
;
3765 register tree result
, folded
;
3766 tree restype
= ptrdiff_type_node
;
3767 tree target_type
= TREE_TYPE (TREE_TYPE (op0
));
3769 if (pedantic
|| warn_pointer_arith
)
3771 if (TREE_CODE (target_type
) == VOID_TYPE
)
3772 pedwarn ("ANSI C++ forbids using pointer of type `void *' in subtraction");
3773 if (TREE_CODE (target_type
) == FUNCTION_TYPE
)
3774 pedwarn ("ANSI C++ forbids using pointer to a function in subtraction");
3775 if (TREE_CODE (target_type
) == METHOD_TYPE
)
3776 pedwarn ("ANSI C++ forbids using pointer to a method in subtraction");
3777 if (TREE_CODE (target_type
) == OFFSET_TYPE
)
3778 pedwarn ("ANSI C++ forbids using pointer to a member in subtraction");
3781 /* First do the subtraction as integers;
3782 then drop through to build the divide operator. */
3784 op0
= build_binary_op (MINUS_EXPR
,
3785 convert (restype
, op0
), convert (restype
, op1
), 1);
3787 /* This generates an error if op1 is a pointer to an incomplete type. */
3788 if (TYPE_SIZE (TREE_TYPE (TREE_TYPE (op1
))) == 0)
3789 error ("arithmetic on pointer to an incomplete type");
3791 op1
= ((TREE_CODE (target_type
) == VOID_TYPE
3792 || TREE_CODE (target_type
) == FUNCTION_TYPE
3793 || TREE_CODE (target_type
) == METHOD_TYPE
3794 || TREE_CODE (target_type
) == OFFSET_TYPE
)
3796 : size_in_bytes (target_type
));
3798 /* Do the division. */
3800 result
= build (EXACT_DIV_EXPR
, restype
, op0
, convert (restype
, op1
));
3802 folded
= fold (result
);
3803 if (folded
== result
)
3804 TREE_CONSTANT (folded
) = TREE_CONSTANT (op0
) & TREE_CONSTANT (op1
);
3808 /* Handle the case of taking the address of a COMPONENT_REF.
3809 Called by `build_unary_op' and `build_up_reference'.
3811 ARG is the COMPONENT_REF whose address we want.
3812 ARGTYPE is the pointer type that this address should have.
3813 MSG is an error message to print if this COMPONENT_REF is not
3814 addressable (such as a bitfield). */
3817 build_component_addr (arg
, argtype
, msg
)
3821 tree field
= TREE_OPERAND (arg
, 1);
3822 tree basetype
= decl_type_context (field
);
3823 tree rval
= build_unary_op (ADDR_EXPR
, TREE_OPERAND (arg
, 0), 0);
3825 if (DECL_BIT_FIELD (field
))
3827 error (msg
, IDENTIFIER_POINTER (DECL_NAME (field
)));
3828 return error_mark_node
;
3831 if (TREE_CODE (field
) == FIELD_DECL
3832 && TYPE_USES_COMPLEX_INHERITANCE (basetype
))
3834 /* Can't convert directly to ARGTYPE, since that
3835 may have the same pointer type as one of our
3837 rval
= build1 (NOP_EXPR
, argtype
,
3838 convert_pointer_to (basetype
, rval
));
3839 TREE_CONSTANT (rval
) = TREE_CONSTANT (TREE_OPERAND (rval
, 0));
3842 /* This conversion is harmless. */
3843 rval
= convert_force (argtype
, rval
, 0);
3845 if (! integer_zerop (DECL_FIELD_BITPOS (field
)))
3847 tree offset
= size_binop (EASY_DIV_EXPR
, DECL_FIELD_BITPOS (field
),
3848 size_int (BITS_PER_UNIT
));
3849 int flag
= TREE_CONSTANT (rval
);
3850 rval
= fold (build (PLUS_EXPR
, argtype
,
3851 rval
, convert (argtype
, offset
)));
3852 TREE_CONSTANT (rval
) = flag
;
3857 /* Construct and perhaps optimize a tree representation
3858 for a unary operation. CODE, a tree_code, specifies the operation
3859 and XARG is the operand. */
3862 build_x_unary_op (code
, xarg
)
3863 enum tree_code code
;
3866 /* & rec, on incomplete RECORD_TYPEs is the simple opr &, not an
3868 if (code
== ADDR_EXPR
3869 && ((IS_AGGR_TYPE_CODE (TREE_CODE (TREE_TYPE (xarg
)))
3870 && TYPE_SIZE (TREE_TYPE (xarg
)) == NULL_TREE
)
3871 || (TREE_CODE (xarg
) == OFFSET_REF
)))
3872 /* don't look for a function */;
3875 tree rval
= build_opfncall (code
, LOOKUP_SPECULATIVELY
, xarg
,
3876 NULL_TREE
, NULL_TREE
);
3878 return build_opfncall (code
, LOOKUP_NORMAL
, xarg
,
3879 NULL_TREE
, NULL_TREE
);
3881 return build_unary_op (code
, xarg
, 0);
3884 /* Just like truthvalue_conversion, but we want a CLEANUP_POINT_EXPR. */
3887 condition_conversion (expr
)
3890 tree t
= convert (boolean_type_node
, expr
);
3891 t
= fold (build1 (CLEANUP_POINT_EXPR
, boolean_type_node
, t
));
3895 /* C++: Must handle pointers to members.
3897 Perhaps type instantiation should be extended to handle conversion
3898 from aggregates to types we don't yet know we want? (Or are those
3899 cases typically errors which should be reported?)
3901 NOCONVERT nonzero suppresses the default promotions
3902 (such as from short to int). */
3904 build_unary_op (code
, xarg
, noconvert
)
3905 enum tree_code code
;
3909 /* No default_conversion here. It causes trouble for ADDR_EXPR. */
3910 register tree arg
= xarg
;
3911 register tree argtype
= 0;
3912 char *errstring
= NULL
;
3915 if (arg
== error_mark_node
)
3916 return error_mark_node
;
3921 /* This is used for unary plus, because a CONVERT_EXPR
3922 is enough to prevent anybody from looking inside for
3923 associativity, but won't generate any code. */
3924 if (!(arg
= build_expr_type_conversion
3925 (WANT_ARITH
| WANT_ENUM
| WANT_POINTER
, arg
, 1)))
3926 errstring
= "wrong type argument to unary plus";
3930 arg
= default_conversion (arg
);
3931 arg
= build1 (NON_LVALUE_EXPR
, TREE_TYPE (arg
), arg
);
3936 if (!(arg
= build_expr_type_conversion (WANT_ARITH
| WANT_ENUM
, arg
, 1)))
3937 errstring
= "wrong type argument to unary minus";
3938 else if (!noconvert
)
3939 arg
= default_conversion (arg
);
3943 if (!(arg
= build_expr_type_conversion (WANT_INT
| WANT_ENUM
, arg
, 1)))
3944 errstring
= "wrong type argument to bit-complement";
3945 else if (!noconvert
)
3946 arg
= default_conversion (arg
);
3950 if (!(arg
= build_expr_type_conversion (WANT_ARITH
| WANT_ENUM
, arg
, 1)))
3951 errstring
= "wrong type argument to abs";
3952 else if (!noconvert
)
3953 arg
= default_conversion (arg
);
3956 case TRUTH_NOT_EXPR
:
3957 arg
= convert (boolean_type_node
, arg
);
3958 val
= invert_truthvalue (arg
);
3959 if (arg
!= error_mark_node
)
3961 errstring
= "in argument to unary !";
3967 case PREINCREMENT_EXPR
:
3968 case POSTINCREMENT_EXPR
:
3969 case PREDECREMENT_EXPR
:
3970 case POSTDECREMENT_EXPR
:
3971 /* Handle complex lvalues (when permitted)
3972 by reduction to simpler cases. */
3974 val
= unary_complex_lvalue (code
, arg
);
3978 /* Report invalid types. */
3980 if (!(arg
= build_expr_type_conversion (WANT_ARITH
| WANT_POINTER
,
3983 if (code
== PREINCREMENT_EXPR
)
3984 errstring
="no pre-increment operator for type";
3985 else if (code
== POSTINCREMENT_EXPR
)
3986 errstring
="no post-increment operator for type";
3987 else if (code
== PREDECREMENT_EXPR
)
3988 errstring
="no pre-decrement operator for type";
3990 errstring
="no post-decrement operator for type";
3994 /* Report something read-only. */
3996 if (TYPE_READONLY (TREE_TYPE (arg
))
3997 || TREE_READONLY (arg
))
3998 readonly_error (arg
, ((code
== PREINCREMENT_EXPR
3999 || code
== POSTINCREMENT_EXPR
)
4000 ? "increment" : "decrement"),
4005 tree result_type
= TREE_TYPE (arg
);
4007 arg
= get_unwidened (arg
, 0);
4008 argtype
= TREE_TYPE (arg
);
4010 /* ARM $5.2.5 last annotation says this should be forbidden. */
4011 if (TREE_CODE (argtype
) == ENUMERAL_TYPE
)
4012 pedwarn ("ANSI C++ forbids %sing an enum",
4013 (code
== PREINCREMENT_EXPR
|| code
== POSTINCREMENT_EXPR
)
4014 ? "increment" : "decrement");
4016 /* Compute the increment. */
4018 if (TREE_CODE (argtype
) == POINTER_TYPE
)
4020 enum tree_code tmp
= TREE_CODE (TREE_TYPE (argtype
));
4021 if (TYPE_SIZE (TREE_TYPE (argtype
)) == 0)
4022 cp_error ("cannot %s a pointer to incomplete type `%T'",
4023 ((code
== PREINCREMENT_EXPR
4024 || code
== POSTINCREMENT_EXPR
)
4025 ? "increment" : "decrement"), TREE_TYPE (argtype
));
4026 else if (tmp
== FUNCTION_TYPE
|| tmp
== METHOD_TYPE
4027 || tmp
== VOID_TYPE
|| tmp
== OFFSET_TYPE
)
4028 cp_pedwarn ("ANSI C++ forbids %sing a pointer of type `%T'",
4029 ((code
== PREINCREMENT_EXPR
4030 || code
== POSTINCREMENT_EXPR
)
4031 ? "increment" : "decrement"), argtype
);
4032 inc
= c_sizeof_nowarn (TREE_TYPE (argtype
));
4035 inc
= integer_one_node
;
4037 inc
= convert (argtype
, inc
);
4039 /* Handle incrementing a cast-expression. */
4041 switch (TREE_CODE (arg
))
4046 case FIX_TRUNC_EXPR
:
4047 case FIX_FLOOR_EXPR
:
4048 case FIX_ROUND_EXPR
:
4051 tree incremented
, modify
, value
, compound
;
4052 if (! lvalue_p (arg
) && pedantic
)
4053 pedwarn ("cast to non-reference type used as lvalue");
4054 arg
= stabilize_reference (arg
);
4055 if (code
== PREINCREMENT_EXPR
|| code
== PREDECREMENT_EXPR
)
4058 value
= save_expr (arg
);
4059 incremented
= build (((code
== PREINCREMENT_EXPR
4060 || code
== POSTINCREMENT_EXPR
)
4061 ? PLUS_EXPR
: MINUS_EXPR
),
4062 argtype
, value
, inc
);
4063 TREE_SIDE_EFFECTS (incremented
) = 1;
4065 modify
= build_modify_expr (arg
, NOP_EXPR
, incremented
);
4066 compound
= build (COMPOUND_EXPR
, TREE_TYPE (arg
), modify
, value
);
4068 /* Eliminate warning about unused result of + or -. */
4069 TREE_NO_UNUSED_WARNING (compound
) = 1;
4074 /* Complain about anything else that is not a true lvalue. */
4075 if (!lvalue_or_else (arg
, ((code
== PREINCREMENT_EXPR
4076 || code
== POSTINCREMENT_EXPR
)
4077 ? "increment" : "decrement")))
4078 return error_mark_node
;
4080 /* Forbid using -- on `bool'. */
4081 if (TREE_TYPE (arg
) == boolean_type_node
)
4083 if (code
== POSTDECREMENT_EXPR
|| code
== PREDECREMENT_EXPR
)
4085 cp_error ("invalid use of `--' on bool variable `%D'", arg
);
4086 return error_mark_node
;
4089 /* This will only work if someone can convince Kenner to accept
4090 my patch to expand_increment. (jason) */
4091 val
= build (code
, TREE_TYPE (arg
), arg
, inc
);
4093 if (code
== POSTINCREMENT_EXPR
)
4095 arg
= stabilize_reference (arg
);
4096 val
= build (MODIFY_EXPR
, TREE_TYPE (arg
), arg
,
4098 TREE_SIDE_EFFECTS (val
) = 1;
4099 arg
= save_expr (arg
);
4100 val
= build (COMPOUND_EXPR
, TREE_TYPE (arg
), val
, arg
);
4101 val
= build (COMPOUND_EXPR
, TREE_TYPE (arg
), arg
, val
);
4104 val
= build (MODIFY_EXPR
, TREE_TYPE (arg
), arg
,
4109 val
= build (code
, TREE_TYPE (arg
), arg
, inc
);
4111 TREE_SIDE_EFFECTS (val
) = 1;
4112 return convert (result_type
, val
);
4116 /* Note that this operation never does default_conversion
4117 regardless of NOCONVERT. */
4119 argtype
= TREE_TYPE (arg
);
4120 if (TREE_CODE (argtype
) == REFERENCE_TYPE
)
4122 arg
= build1 (CONVERT_EXPR
, build_pointer_type (TREE_TYPE (TREE_TYPE (arg
))), arg
);
4123 TREE_REFERENCE_EXPR (arg
) = 1;
4127 && TREE_CODE (arg
) == FUNCTION_DECL
4129 && DECL_CONTEXT (arg
) == NULL_TREE
4130 && IDENTIFIER_LENGTH (DECL_NAME (arg
)) == 4
4131 && IDENTIFIER_POINTER (DECL_NAME (arg
))[0] == 'm'
4132 && ! strcmp (IDENTIFIER_POINTER (DECL_NAME (arg
)), "main"))
4134 pedwarn ("taking address of function `main'");
4136 /* Let &* cancel out to simplify resulting code. */
4137 if (TREE_CODE (arg
) == INDIRECT_REF
)
4139 /* We don't need to have `current_class_decl' wrapped in a
4140 NON_LVALUE_EXPR node. */
4142 return current_class_decl
;
4144 /* Keep `default_conversion' from converting if
4145 ARG is of REFERENCE_TYPE. */
4146 arg
= TREE_OPERAND (arg
, 0);
4147 if (TREE_CODE (TREE_TYPE (arg
)) == REFERENCE_TYPE
)
4149 if (TREE_CODE (arg
) == VAR_DECL
&& DECL_INITIAL (arg
)
4150 && !TREE_SIDE_EFFECTS (DECL_INITIAL (arg
)))
4151 arg
= DECL_INITIAL (arg
);
4152 arg
= build1 (CONVERT_EXPR
, build_pointer_type (TREE_TYPE (TREE_TYPE (arg
))), arg
);
4153 TREE_REFERENCE_EXPR (arg
) = 1;
4154 TREE_CONSTANT (arg
) = TREE_CONSTANT (TREE_OPERAND (arg
, 0));
4156 else if (lvalue_p (arg
))
4157 /* Don't let this be an lvalue. */
4158 return non_lvalue (arg
);
4162 /* For &x[y], return x+y */
4163 if (TREE_CODE (arg
) == ARRAY_REF
)
4165 if (mark_addressable (TREE_OPERAND (arg
, 0)) == 0)
4166 return error_mark_node
;
4167 return build_binary_op (PLUS_EXPR
, TREE_OPERAND (arg
, 0),
4168 TREE_OPERAND (arg
, 1), 1);
4171 /* Uninstantiated types are all functions. Taking the
4172 address of a function is a no-op, so just return the
4175 if (TREE_CODE (arg
) == IDENTIFIER_NODE
4176 && IDENTIFIER_OPNAME_P (arg
))
4178 my_friendly_abort (117);
4179 /* We don't know the type yet, so just work around the problem.
4180 We know that this will resolve to an lvalue. */
4181 return build1 (ADDR_EXPR
, unknown_type_node
, arg
);
4184 if (TREE_CODE (arg
) == TREE_LIST
)
4186 if (TREE_CODE (TREE_VALUE (arg
)) == FUNCTION_DECL
4187 && DECL_CHAIN (TREE_VALUE (arg
)) == NULL_TREE
)
4188 /* Unique overloaded non-member function. */
4189 return build_unary_op (ADDR_EXPR
, TREE_VALUE (arg
), 0);
4190 if (TREE_CHAIN (arg
) == NULL_TREE
4191 && TREE_CODE (TREE_VALUE (arg
)) == TREE_LIST
4192 && DECL_CHAIN (TREE_VALUE (TREE_VALUE (arg
))) == NULL_TREE
)
4193 /* Unique overloaded member function. */
4194 return build_unary_op (ADDR_EXPR
, TREE_VALUE (TREE_VALUE (arg
)),
4196 return build1 (ADDR_EXPR
, unknown_type_node
, arg
);
4199 /* Handle complex lvalues (when permitted)
4200 by reduction to simpler cases. */
4201 val
= unary_complex_lvalue (code
, arg
);
4205 switch (TREE_CODE (arg
))
4210 case FIX_TRUNC_EXPR
:
4211 case FIX_FLOOR_EXPR
:
4212 case FIX_ROUND_EXPR
:
4214 if (! lvalue_p (arg
) && pedantic
)
4215 pedwarn ("taking the address of a cast to non-reference type");
4218 /* Allow the address of a constructor if all the elements
4220 if (TREE_CODE (arg
) == CONSTRUCTOR
&& TREE_CONSTANT (arg
))
4222 /* Anything not already handled and not a true memory reference
4224 else if (TREE_CODE (argtype
) != FUNCTION_TYPE
4225 && TREE_CODE (argtype
) != METHOD_TYPE
4226 && !lvalue_or_else (arg
, "unary `&'"))
4227 return error_mark_node
;
4229 /* Ordinary case; arg is a COMPONENT_REF or a decl. */
4230 /* If the lvalue is const or volatile,
4231 merge that into the type that the address will point to. */
4232 if (TREE_CODE_CLASS (TREE_CODE (arg
)) == 'd'
4233 || TREE_CODE_CLASS (TREE_CODE (arg
)) == 'r')
4235 if (TREE_READONLY (arg
) || TREE_THIS_VOLATILE (arg
))
4236 argtype
= cp_build_type_variant (argtype
,
4237 TREE_READONLY (arg
),
4238 TREE_THIS_VOLATILE (arg
));
4241 argtype
= build_pointer_type (argtype
);
4243 if (mark_addressable (arg
) == 0)
4244 return error_mark_node
;
4249 if (TREE_CODE (arg
) == COMPONENT_REF
)
4250 addr
= build_component_addr (arg
, argtype
,
4251 "attempt to take address of bit-field structure member `%s'");
4253 addr
= build1 (code
, argtype
, arg
);
4255 /* Address of a static or external variable or
4256 function counts as a constant */
4258 TREE_CONSTANT (addr
) = 1;
4266 argtype
= TREE_TYPE (arg
);
4267 return fold (build1 (code
, argtype
, arg
));
4271 return error_mark_node
;
4275 /* If CONVERSIONS is a conversion expression or a nested sequence of such,
4276 convert ARG with the same conversions in the same order
4277 and return the result. */
4280 convert_sequence (conversions
, arg
)
4284 switch (TREE_CODE (conversions
))
4289 case FIX_TRUNC_EXPR
:
4290 case FIX_FLOOR_EXPR
:
4291 case FIX_ROUND_EXPR
:
4293 return convert (TREE_TYPE (conversions
),
4294 convert_sequence (TREE_OPERAND (conversions
, 0),
4303 /* Apply unary lvalue-demanding operator CODE to the expression ARG
4304 for certain kinds of expressions which are not really lvalues
4305 but which we can accept as lvalues.
4307 If ARG is not a kind of expression we can handle, return zero. */
4310 unary_complex_lvalue (code
, arg
)
4311 enum tree_code code
;
4314 /* Handle (a, b) used as an "lvalue". */
4315 if (TREE_CODE (arg
) == COMPOUND_EXPR
)
4317 tree real_result
= build_unary_op (code
, TREE_OPERAND (arg
, 1), 0);
4318 return build (COMPOUND_EXPR
, TREE_TYPE (real_result
),
4319 TREE_OPERAND (arg
, 0), real_result
);
4322 /* Handle (a ? b : c) used as an "lvalue". */
4323 if (TREE_CODE (arg
) == COND_EXPR
)
4324 return rationalize_conditional_expr (code
, arg
);
4326 if (TREE_CODE (arg
) == MODIFY_EXPR
4327 || TREE_CODE (arg
) == PREINCREMENT_EXPR
4328 || TREE_CODE (arg
) == PREDECREMENT_EXPR
)
4329 return unary_complex_lvalue
4330 (code
, build (COMPOUND_EXPR
, TREE_TYPE (TREE_OPERAND (arg
, 0)),
4331 arg
, TREE_OPERAND (arg
, 0)));
4333 if (code
!= ADDR_EXPR
)
4336 /* Handle (a = b) used as an "lvalue" for `&'. */
4337 if (TREE_CODE (arg
) == MODIFY_EXPR
4338 || TREE_CODE (arg
) == INIT_EXPR
)
4340 tree real_result
= build_unary_op (code
, TREE_OPERAND (arg
, 0), 0);
4341 return build (COMPOUND_EXPR
, TREE_TYPE (real_result
), arg
, real_result
);
4344 if (TREE_CODE (TREE_TYPE (arg
)) == FUNCTION_TYPE
4345 || TREE_CODE (TREE_TYPE (arg
)) == METHOD_TYPE
4346 || TREE_CODE (TREE_TYPE (arg
)) == OFFSET_TYPE
)
4348 /* The representation of something of type OFFSET_TYPE
4349 is really the representation of a pointer to it.
4350 Here give the representation its true type. */
4354 my_friendly_assert (TREE_CODE (arg
) != SCOPE_REF
, 313);
4356 if (TREE_CODE (arg
) != OFFSET_REF
)
4359 t
= TREE_OPERAND (arg
, 1);
4361 if (TREE_CODE (t
) == FUNCTION_DECL
) /* Check all this code for right semantics. */
4362 return build_unary_op (ADDR_EXPR
, t
, 0);
4363 if (TREE_CODE (t
) == VAR_DECL
)
4364 return build_unary_op (ADDR_EXPR
, t
, 0);
4367 if (TREE_OPERAND (arg
, 0)
4368 && (TREE_CODE (TREE_OPERAND (arg
, 0)) != NOP_EXPR
4369 || TREE_OPERAND (TREE_OPERAND (arg
, 0), 0) != error_mark_node
))
4370 if (TREE_CODE (t
) != FIELD_DECL
)
4372 /* Don't know if this should return address to just
4373 _DECL, or actual address resolved in this expression. */
4374 sorry ("address of bound pointer-to-member expression");
4375 return error_mark_node
;
4378 offset
= get_delta_difference (DECL_FIELD_CONTEXT (t
),
4379 TREE_TYPE (TREE_OPERAND (arg
, 0)),
4381 offset
= size_binop (PLUS_EXPR
, offset
,
4382 size_binop (EASY_DIV_EXPR
,
4383 DECL_FIELD_BITPOS (t
),
4384 size_int (BITS_PER_UNIT
)));
4385 return convert (build_pointer_type (TREE_TYPE (arg
)), offset
);
4389 if (TREE_CODE (arg
) == OFFSET_REF
)
4391 tree left
= TREE_OPERAND (arg
, 0), left_addr
;
4392 tree right_addr
= build_unary_op (ADDR_EXPR
, TREE_OPERAND (arg
, 1), 0);
4395 if (current_class_decl
)
4396 left_addr
= current_class_decl
;
4399 error ("no `this' for pointer to member");
4400 return error_mark_node
;
4403 left_addr
= build_unary_op (ADDR_EXPR
, left
, 0);
4405 return build (PLUS_EXPR
, build_pointer_type (TREE_TYPE (arg
)),
4406 build1 (NOP_EXPR
, integer_type_node
, left_addr
),
4407 build1 (NOP_EXPR
, integer_type_node
, right_addr
));
4410 /* We permit compiler to make function calls returning
4411 objects of aggregate type look like lvalues. */
4415 if (TREE_CODE (targ
) == SAVE_EXPR
)
4416 targ
= TREE_OPERAND (targ
, 0);
4418 if (TREE_CODE (targ
) == CALL_EXPR
&& IS_AGGR_TYPE (TREE_TYPE (targ
)))
4420 if (TREE_CODE (arg
) == SAVE_EXPR
)
4423 targ
= build_cplus_new (TREE_TYPE (arg
), arg
, 1);
4424 return build1 (ADDR_EXPR
, build_pointer_type (TREE_TYPE (arg
)), targ
);
4427 if (TREE_CODE (arg
) == SAVE_EXPR
&& TREE_CODE (targ
) == INDIRECT_REF
)
4428 return build (SAVE_EXPR
, build_pointer_type (TREE_TYPE (arg
)),
4429 TREE_OPERAND (targ
, 0), current_function_decl
, NULL
);
4432 /* Don't let anything else be handled specially. */
4436 /* Mark EXP saying that we need to be able to take the
4437 address of it; it should not be allocated in a register.
4438 Value is 1 if successful.
4440 C++: we do not allow `current_class_decl' to be addressable. */
4443 mark_addressable (exp
)
4446 register tree x
= exp
;
4448 if (TREE_ADDRESSABLE (x
) == 1)
4452 switch (TREE_CODE (x
))
4457 x
= TREE_OPERAND (x
, 0);
4461 if (x
== current_class_decl
)
4463 error ("address of `this' not available");
4464 TREE_ADDRESSABLE (x
) = 1; /* so compiler doesn't die later */
4465 put_var_into_stack (x
);
4469 if (TREE_STATIC (x
) && 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 /* Don't promote the operands separately if they promote
4595 the same way. Return the unpromoted type and let the combined
4596 value get promoted if necessary. */
4598 if (TYPE_MAIN_VARIANT (type1
) == TYPE_MAIN_VARIANT (type2
)
4599 && code2
!= ARRAY_TYPE
4600 && code2
!= FUNCTION_TYPE
4601 && code2
!= METHOD_TYPE
)
4605 if (TREE_CONSTANT (ifexp
)
4606 && (TREE_CODE (ifexp
) == INTEGER_CST
4607 || TREE_CODE (ifexp
) == ADDR_EXPR
))
4608 return (integer_zerop (ifexp
) ? op2
: op1
);
4610 if (TREE_CODE (op1
) == CONST_DECL
)
4611 op1
= DECL_INITIAL (op1
);
4612 else if (TREE_READONLY_DECL_P (op1
))
4613 op1
= decl_constant_value (op1
);
4614 if (TREE_CODE (op2
) == CONST_DECL
)
4615 op2
= DECL_INITIAL (op2
);
4616 else if (TREE_READONLY_DECL_P (op2
))
4617 op2
= decl_constant_value (op2
);
4619 type1
= cp_build_type_variant
4621 TREE_READONLY (op1
) || TREE_READONLY (op2
),
4622 TREE_THIS_VOLATILE (op1
) || TREE_THIS_VOLATILE (op2
));
4623 /* ??? This is a kludge to deal with the fact that
4624 we don't sort out integers and enums properly, yet. */
4625 result
= fold (build (COND_EXPR
, type1
, ifexp
, op1
, op2
));
4626 if (TREE_TYPE (result
) != type1
)
4627 result
= build1 (NOP_EXPR
, type1
, result
);
4631 /* They don't match; promote them both and then try to reconcile them.
4632 But don't permit mismatching enum types. */
4633 if (code1
== ENUMERAL_TYPE
)
4635 if (code2
== ENUMERAL_TYPE
)
4637 cp_error ("enumeral mismatch in conditional expression: `%T' vs `%T'", type1
, type2
);
4638 return error_mark_node
;
4640 else if (extra_warnings
&& ! IS_AGGR_TYPE_CODE (code2
)
4641 && type2
!= type_promotes_to (type1
))
4642 warning ("enumeral and non-enumeral type in conditional expression");
4644 else if (extra_warnings
4645 && code2
== ENUMERAL_TYPE
&& ! IS_AGGR_TYPE_CODE (code1
)
4646 && type1
!= type_promotes_to (type2
))
4647 warning ("enumeral and non-enumeral type in conditional expression");
4649 if (code1
!= VOID_TYPE
)
4651 op1
= default_conversion (op1
);
4652 type1
= TREE_TYPE (op1
);
4653 if (TYPE_PTRMEMFUNC_P (type1
))
4654 type1
= TYPE_PTRMEMFUNC_FN_TYPE (type1
);
4655 code1
= TREE_CODE (type1
);
4657 if (code2
!= VOID_TYPE
)
4659 op2
= default_conversion (op2
);
4660 type2
= TREE_TYPE (op2
);
4661 if (TYPE_PTRMEMFUNC_P (type2
))
4662 type2
= TYPE_PTRMEMFUNC_FN_TYPE (type2
);
4663 code2
= TREE_CODE (type2
);
4666 if (code1
== RECORD_TYPE
&& code2
== RECORD_TYPE
4667 && real_lvalue_p (op1
) && real_lvalue_p (op2
)
4668 && comptypes (type1
, type2
, -1))
4670 type1
= build_reference_type (type1
);
4671 type2
= build_reference_type (type2
);
4672 result_type
= common_type (type1
, type2
);
4673 op1
= convert_to_reference (result_type
, op1
, CONV_IMPLICIT
,
4674 LOOKUP_NORMAL
, NULL_TREE
);
4675 op2
= convert_to_reference (result_type
, op2
, CONV_IMPLICIT
,
4676 LOOKUP_NORMAL
, NULL_TREE
);
4678 /* Quickly detect the usual case where op1 and op2 have the same type
4680 else if (TYPE_MAIN_VARIANT (type1
) == TYPE_MAIN_VARIANT (type2
))
4683 result_type
= type1
;
4685 result_type
= cp_build_type_variant
4687 TREE_READONLY (op1
) || TREE_READONLY (op2
),
4688 TREE_THIS_VOLATILE (op1
) || TREE_THIS_VOLATILE (op2
));
4690 else if ((code1
== INTEGER_TYPE
|| code1
== REAL_TYPE
)
4691 && (code2
== INTEGER_TYPE
|| code2
== REAL_TYPE
))
4693 result_type
= common_type (type1
, type2
);
4695 else if (code1
== VOID_TYPE
|| code2
== VOID_TYPE
)
4697 if (pedantic
&& (code1
!= VOID_TYPE
|| code2
!= VOID_TYPE
))
4698 pedwarn ("ANSI C++ forbids conditional expr with only one void side");
4699 result_type
= void_type_node
;
4701 else if (code1
== POINTER_TYPE
&& code2
== POINTER_TYPE
)
4703 if (comp_target_types (type1
, type2
, 1))
4704 result_type
= common_type (type1
, type2
);
4705 else if (integer_zerop (op1
) && TREE_TYPE (type1
) == void_type_node
4706 && TREE_CODE (orig_op1
) != NOP_EXPR
)
4707 result_type
= qualify_type (type2
, type1
);
4708 else if (integer_zerop (op2
) && TREE_TYPE (type2
) == void_type_node
4709 && TREE_CODE (orig_op2
) != NOP_EXPR
)
4710 result_type
= qualify_type (type1
, type2
);
4711 else if (TYPE_MAIN_VARIANT (TREE_TYPE (type1
)) == void_type_node
)
4713 if (pedantic
&& TREE_CODE (type2
) == FUNCTION_TYPE
)
4714 pedwarn ("ANSI C++ forbids conditional expr between `void *' and function pointer");
4715 result_type
= qualify_type (type1
, type2
);
4717 else if (TYPE_MAIN_VARIANT (TREE_TYPE (type2
)) == void_type_node
)
4719 if (pedantic
&& TREE_CODE (type1
) == FUNCTION_TYPE
)
4720 pedwarn ("ANSI C++ forbids conditional expr between `void *' and function pointer");
4721 result_type
= qualify_type (type2
, type1
);
4724 else if (comptypes (type2
, type1
, 0))
4725 result_type
= type2
;
4726 else if (IS_AGGR_TYPE (TREE_TYPE (type1
))
4727 && IS_AGGR_TYPE (TREE_TYPE (type2
))
4728 && (result_type
= common_base_type (TREE_TYPE (type1
), TREE_TYPE (type2
))))
4730 if (result_type
== error_mark_node
)
4732 cp_error ("common base type of types `%T' and `%T' is ambiguous",
4733 TREE_TYPE (type1
), TREE_TYPE (type2
));
4734 result_type
= ptr_type_node
;
4739 && result_type
!= TREE_TYPE (type1
)
4740 && result_type
!= TREE_TYPE (type2
))
4741 cp_pedwarn ("`%T' and `%T' converted to `%T *' in conditional expression",
4742 type1
, type2
, result_type
);
4744 result_type
= build_pointer_type (result_type
);
4749 pedwarn ("pointer type mismatch in conditional expression");
4750 result_type
= ptr_type_node
;
4753 else if (code1
== POINTER_TYPE
&& code2
== INTEGER_TYPE
)
4755 if (!integer_zerop (op2
))
4756 pedwarn ("pointer/integer type mismatch in conditional expression");
4758 op2
= null_pointer_node
;
4760 result_type
= type1
;
4762 else if (code2
== POINTER_TYPE
&& code1
== INTEGER_TYPE
)
4764 if (!integer_zerop (op1
))
4765 pedwarn ("pointer/integer type mismatch in conditional expression");
4767 op1
= null_pointer_node
;
4769 result_type
= type2
;
4774 /* The match does not look good. If either is
4775 an aggregate value, try converting to a scalar type. */
4776 if (code1
== RECORD_TYPE
&& code2
== RECORD_TYPE
)
4778 cp_error ("aggregate mismatch in conditional expression: `%T' vs `%T'", type1
, type2
);
4779 return error_mark_node
;
4781 if (code1
== RECORD_TYPE
&& TYPE_HAS_CONVERSION (type1
))
4783 tree tmp
= build_type_conversion (CONVERT_EXPR
, type2
, op1
, 0);
4784 if (tmp
== NULL_TREE
)
4786 cp_error ("aggregate type `%T' could not convert on lhs of `:'", type1
);
4787 return error_mark_node
;
4789 if (tmp
== error_mark_node
)
4790 error ("ambiguous pointer conversion");
4791 result_type
= type2
;
4794 else if (code2
== RECORD_TYPE
&& TYPE_HAS_CONVERSION (type2
))
4796 tree tmp
= build_type_conversion (CONVERT_EXPR
, type1
, op2
, 0);
4797 if (tmp
== NULL_TREE
)
4799 cp_error ("aggregate type `%T' could not convert on rhs of `:'", type2
);
4800 return error_mark_node
;
4802 if (tmp
== error_mark_node
)
4803 error ("ambiguous pointer conversion");
4804 result_type
= type1
;
4807 else if (flag_cond_mismatch
)
4808 result_type
= void_type_node
;
4811 error ("type mismatch in conditional expression");
4812 return error_mark_node
;
4816 if (TREE_CODE (result_type
) == POINTER_TYPE
4817 && TREE_CODE (TREE_TYPE (result_type
)) == METHOD_TYPE
)
4818 result_type
= build_ptrmemfunc_type (result_type
);
4820 if (result_type
!= TREE_TYPE (op1
))
4821 op1
= convert_and_check (result_type
, op1
);
4822 if (result_type
!= TREE_TYPE (op2
))
4823 op2
= convert_and_check (result_type
, op2
);
4825 if (TREE_CONSTANT (ifexp
))
4826 return integer_zerop (ifexp
) ? op2
: op1
;
4828 return convert_from_reference
4829 (fold (build (COND_EXPR
, result_type
, ifexp
, op1
, op2
)));
4832 /* Handle overloading of the ',' operator when needed. Otherwise,
4833 this function just builds an expression list. */
4835 build_x_compound_expr (list
)
4838 tree rest
= TREE_CHAIN (list
);
4841 if (rest
== NULL_TREE
)
4842 return build_compound_expr (list
);
4844 result
= build_opfncall (COMPOUND_EXPR
, LOOKUP_NORMAL
,
4845 TREE_VALUE (list
), TREE_VALUE (rest
), NULL_TREE
);
4847 return build_x_compound_expr (tree_cons (NULL_TREE
, result
, TREE_CHAIN (rest
)));
4849 if (! TREE_SIDE_EFFECTS (TREE_VALUE (list
)))
4851 /* the left-hand operand of a comma expression is like an expression
4852 statement: we should warn if it doesn't have any side-effects,
4853 unless it was explicitly cast to (void). */
4854 if ((extra_warnings
|| warn_unused
)
4855 && !(TREE_CODE (TREE_VALUE(list
)) == CONVERT_EXPR
4856 && TREE_TYPE (TREE_VALUE(list
)) == void_type_node
))
4857 warning("left-hand operand of comma expression has no effect");
4859 #if 0 /* this requires a gcc backend patch to export warn_if_unused_value */
4860 else if (warn_unused
)
4861 warn_if_unused_value (TREE_VALUE(list
));
4864 return build_compound_expr (tree_cons (NULL_TREE
, TREE_VALUE (list
),
4865 build_tree_list (NULL_TREE
, build_x_compound_expr (rest
))));
4868 /* Given a list of expressions, return a compound expression
4869 that performs them all and returns the value of the last of them. */
4872 build_compound_expr (list
)
4877 if (TREE_READONLY_DECL_P (TREE_VALUE (list
)))
4878 TREE_VALUE (list
) = decl_constant_value (TREE_VALUE (list
));
4880 if (TREE_CHAIN (list
) == 0)
4882 /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
4883 Strip such NOP_EXPRs, since LIST is used in non-lvalue context. */
4884 if (TREE_CODE (list
) == NOP_EXPR
4885 && TREE_TYPE (list
) == TREE_TYPE (TREE_OPERAND (list
, 0)))
4886 list
= TREE_OPERAND (list
, 0);
4888 /* Convert arrays to pointers. */
4889 if (TREE_CODE (TREE_TYPE (TREE_VALUE (list
))) == ARRAY_TYPE
)
4890 return default_conversion (TREE_VALUE (list
));
4892 return TREE_VALUE (list
);
4895 rest
= build_compound_expr (TREE_CHAIN (list
));
4897 /* When pedantic, a compound expression cannot be a constant expression. */
4898 if (! TREE_SIDE_EFFECTS (TREE_VALUE (list
)) && ! pedantic
)
4901 return build (COMPOUND_EXPR
, TREE_TYPE (rest
),
4902 break_out_cleanups (TREE_VALUE (list
)), rest
);
4905 tree
build_static_cast (type
, expr
)
4908 return build_c_cast (type
, expr
, 0);
4911 tree
build_reinterpret_cast (type
, expr
)
4914 tree intype
= TREE_TYPE (expr
);
4916 if (TYPE_PTRMEMFUNC_P (type
))
4917 type
= TYPE_PTRMEMFUNC_FN_TYPE (type
);
4918 if (TYPE_PTRMEMFUNC_P (intype
))
4919 intype
= TYPE_PTRMEMFUNC_FN_TYPE (intype
);
4921 if (! POINTER_TYPE_P (type
) && ! TREE_CODE (type
) == INTEGER_TYPE
)
4923 cp_error ("reinterpret_cast cannot convert to type `%T'", type
);
4924 return error_mark_node
;
4926 if (! POINTER_TYPE_P (intype
) && ! TREE_CODE (intype
) == INTEGER_TYPE
)
4928 cp_error ("reinterpret_cast cannot convert from type `%T'", type
);
4929 return error_mark_node
;
4931 if (TREE_CODE (type
) == INTEGER_TYPE
&& TREE_CODE (intype
) != POINTER_TYPE
)
4933 cp_error ("reinterpret_cast cannot convert non-pointer type `%T' to `%T'",
4935 return error_mark_node
;
4937 if (TREE_CODE (intype
) == INTEGER_TYPE
&& TREE_CODE (type
) != POINTER_TYPE
)
4939 cp_error ("reinterpret_cast cannot convert `%T' to non-pointer type `%T'",
4941 return error_mark_node
;
4944 if (TREE_CODE (type
) == POINTER_TYPE
&& TREE_CODE (intype
) == POINTER_TYPE
)
4945 expr
= convert (ptr_type_node
, expr
);
4947 return build_c_cast (type
, expr
, 0);
4950 tree
build_const_cast (type
, expr
)
4953 tree intype
= TREE_TYPE (expr
);
4956 if (type
== error_mark_node
|| expr
== error_mark_node
)
4957 return error_mark_node
;
4959 if (TYPE_PTRMEMFUNC_P (type
))
4960 type
= TYPE_PTRMEMFUNC_FN_TYPE (type
);
4961 if (TYPE_PTRMEMFUNC_P (intype
))
4962 intype
= TYPE_PTRMEMFUNC_FN_TYPE (intype
);
4964 if (! POINTER_TYPE_P (type
))
4966 cp_error ("const_cast cannot convert to non-pointer type `%T'", type
);
4967 return error_mark_node
;
4969 if (TREE_CODE (type
) == REFERENCE_TYPE
&& ! real_lvalue_p (expr
))
4971 cp_error ("const_cast cannot convert rvalue to type `%T'", type
);
4972 return error_mark_node
;
4974 if (TREE_CODE (type
) == POINTER_TYPE
&& TREE_CODE (intype
) != POINTER_TYPE
)
4976 cp_error ("const_cast cannot convert non-pointer type `%T' to type `%T'",
4978 return error_mark_node
;
4981 if (TREE_CODE (type
) == REFERENCE_TYPE
)
4983 t1
= TREE_TYPE (type
);
4988 t1
= TREE_TYPE (type
);
4989 t2
= TREE_TYPE (intype
);
4991 for (; TREE_CODE (t1
) == POINTER_TYPE
&& TREE_CODE (t2
) == POINTER_TYPE
;
4992 t1
= TREE_TYPE (t1
), t2
= TREE_TYPE (t2
))
4996 if (TREE_CODE (t1
) == OFFSET_TYPE
&& TREE_CODE (t2
) == OFFSET_TYPE
)
4998 if (TYPE_OFFSET_BASETYPE (t1
) != TYPE_OFFSET_BASETYPE (t2
))
5000 cp_error ("const_cast cannot convert between pointers to members of different types `%T' and `%T'",
5001 TYPE_OFFSET_BASETYPE (t2
), TYPE_OFFSET_BASETYPE (t1
));
5002 return error_mark_node
;
5004 t1
= TREE_TYPE (t1
);
5005 t2
= TREE_TYPE (t2
);
5008 if (TYPE_MAIN_VARIANT (t1
) != TYPE_MAIN_VARIANT (t2
))
5010 cp_error ("const_cast cannot convert unrelated type `%T' to `%T'",
5012 return error_mark_node
;
5015 return build_c_cast (type
, expr
, 0);
5018 /* Build an expression representing a cast to type TYPE of expression EXPR.
5020 ALLOW_NONCONVERTING is true if we should allow non-converting constructors
5021 when doing the cast. */
5024 build_c_cast (type
, expr
, allow_nonconverting
)
5027 int allow_nonconverting
;
5029 register tree value
= expr
;
5031 if (type
== error_mark_node
|| expr
== error_mark_node
)
5032 return error_mark_node
;
5034 /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
5035 Strip such NOP_EXPRs if VALUE is being used in non-lvalue context. */
5036 if (TREE_CODE (type
) != REFERENCE_TYPE
5037 && TREE_CODE (value
) == NOP_EXPR
5038 && TREE_TYPE (value
) == TREE_TYPE (TREE_OPERAND (value
, 0)))
5039 value
= TREE_OPERAND (value
, 0);
5041 if (TREE_TYPE (expr
)
5042 && TREE_CODE (TREE_TYPE (expr
)) == OFFSET_TYPE
5043 && TREE_CODE (type
) != OFFSET_TYPE
)
5044 value
= resolve_offset_ref (value
);
5046 if (TREE_CODE (type
) == ARRAY_TYPE
)
5048 /* Allow casting from T1* to T2[] because Cfront allows it.
5049 NIHCL uses it. It is not valid ANSI C however, and hence, not
5051 if (TREE_CODE (TREE_TYPE (expr
)) == POINTER_TYPE
)
5054 pedwarn ("ANSI C++ forbids casting to an array type");
5055 type
= build_pointer_type (TREE_TYPE (type
));
5059 error ("ANSI C++ forbids casting to an array type");
5060 return error_mark_node
;
5064 if (TREE_CODE (type
) == FUNCTION_TYPE
5065 || TREE_CODE (type
) == METHOD_TYPE
)
5067 cp_error ("casting to function type `%T'", type
);
5068 return error_mark_node
;
5071 if (IS_SIGNATURE (type
))
5073 error ("cast specifies signature type");
5074 return error_mark_node
;
5077 /* If there's only one function in the overloaded space,
5079 if (TREE_CODE (value
) == TREE_LIST
5080 && TREE_CHAIN (value
) == NULL_TREE
)
5081 value
= TREE_VALUE (value
);
5083 if (TREE_CODE (type
) == VOID_TYPE
)
5084 value
= build1 (CONVERT_EXPR
, type
, value
);
5085 else if (TREE_TYPE (value
) == NULL_TREE
5086 || type_unknown_p (value
))
5088 value
= instantiate_type (type
, value
, 1);
5090 if (value
== error_mark_node
)
5091 return error_mark_node
;
5098 /* Convert functions and arrays to pointers and
5099 convert references to their expanded types,
5100 but don't convert any other types. */
5101 if (TREE_CODE (TREE_TYPE (value
)) == FUNCTION_TYPE
5102 || TREE_CODE (TREE_TYPE (value
)) == METHOD_TYPE
5103 || TREE_CODE (TREE_TYPE (value
)) == ARRAY_TYPE
5104 || TREE_CODE (TREE_TYPE (value
)) == REFERENCE_TYPE
)
5105 value
= default_conversion (value
);
5106 otype
= TREE_TYPE (value
);
5108 /* Optionally warn about potentially worrisome casts. */
5111 && TREE_CODE (type
) == POINTER_TYPE
5112 && TREE_CODE (otype
) == POINTER_TYPE
)
5114 /* For C++ we make these regular warnings, rather than
5115 softening them into pedwarns. */
5116 if (TYPE_VOLATILE (TREE_TYPE (otype
))
5117 && ! TYPE_VOLATILE (TREE_TYPE (type
)))
5118 warning ("cast discards `volatile' from pointer target type");
5119 if (TYPE_READONLY (TREE_TYPE (otype
))
5120 && ! TYPE_READONLY (TREE_TYPE (type
)))
5121 warning ("cast discards `const' from pointer target type");
5124 /* Warn about possible alignment problems. */
5125 if (STRICT_ALIGNMENT
&& warn_cast_align
5126 && TREE_CODE (type
) == POINTER_TYPE
5127 && TREE_CODE (otype
) == POINTER_TYPE
5128 && TREE_CODE (TREE_TYPE (otype
)) != VOID_TYPE
5129 && TREE_CODE (TREE_TYPE (otype
)) != FUNCTION_TYPE
5130 && TYPE_ALIGN (TREE_TYPE (type
)) > TYPE_ALIGN (TREE_TYPE (otype
)))
5131 warning ("cast increases required alignment of target type");
5134 /* We should see about re-enabling these, they seem useful to
5136 if (TREE_CODE (type
) == INTEGER_TYPE
5137 && TREE_CODE (otype
) == POINTER_TYPE
5138 && TYPE_PRECISION (type
) != TYPE_PRECISION (otype
))
5139 warning ("cast from pointer to integer of different size");
5141 if (TREE_CODE (type
) == POINTER_TYPE
5142 && TREE_CODE (otype
) == INTEGER_TYPE
5143 && TYPE_PRECISION (type
) != TYPE_PRECISION (otype
)
5144 /* Don't warn about converting 0 to pointer,
5145 provided the 0 was explicit--not cast or made by folding. */
5146 && !(TREE_CODE (value
) == INTEGER_CST
&& integer_zerop (value
)))
5147 warning ("cast to pointer from integer of different size");
5150 flag
= allow_nonconverting
? CONV_NONCONVERTING
: 0;
5152 if (TREE_CODE (type
) == REFERENCE_TYPE
)
5153 value
= (convert_from_reference
5154 (convert_to_reference (type
, value
, CONV_OLD_CONVERT
|flag
,
5155 LOOKUP_COMPLAIN
, NULL_TREE
)));
5160 if (TREE_READONLY_DECL_P (value
))
5161 value
= decl_constant_value (value
);
5164 value
= convert_force (type
, value
, flag
);
5166 /* Ignore any integer overflow caused by the cast. */
5167 if (TREE_CODE (value
) == INTEGER_CST
)
5169 TREE_OVERFLOW (value
) = TREE_OVERFLOW (ovalue
);
5170 TREE_CONSTANT_OVERFLOW (value
) = TREE_CONSTANT_OVERFLOW (ovalue
);
5175 /* Always produce some operator for an explicit cast,
5176 so we can tell (for -pedantic) that the cast is no lvalue.
5177 Also, pedantically, don't let (void *) (FOO *) 0 be a null
5178 pointer constant. */
5179 if (TREE_CODE (type
) != REFERENCE_TYPE
5182 && TREE_CODE (value
) == INTEGER_CST
5183 && TREE_CODE (expr
) == INTEGER_CST
5184 && TREE_CODE (TREE_TYPE (expr
)) != INTEGER_TYPE
)))
5185 value
= non_lvalue (value
);
5191 expand_target_expr (t
)
5194 extern int temp_slot_level
;
5195 extern int target_temp_slot_level
;
5196 int old_temp_level
= target_temp_slot_level
;
5198 tree xval
= make_node (RTL_EXPR
);
5201 /* Any TARGET_EXPR temps live only as long as the outer temp level.
5202 Since they are preserved in this new inner level, we know they
5203 will make it into the outer level. */
5205 target_temp_slot_level
= temp_slot_level
;
5207 do_pending_stack_adjust ();
5208 start_sequence_for_rtl_expr (xval
);
5210 rtxval
= expand_expr (t
, NULL
, VOIDmode
, 0);
5211 do_pending_stack_adjust ();
5212 TREE_SIDE_EFFECTS (xval
) = 1;
5213 RTL_EXPR_SEQUENCE (xval
) = get_insns ();
5215 RTL_EXPR_RTL (xval
) = rtxval
;
5216 TREE_TYPE (xval
) = TREE_TYPE (t
);
5219 target_temp_slot_level
= old_temp_level
;
5224 /* Build an assignment expression of lvalue LHS from value RHS.
5225 MODIFYCODE is the code for a binary operator that we use
5226 to combine the old value of LHS with RHS to get the new value.
5227 Or else MODIFYCODE is NOP_EXPR meaning do a simple assignment.
5229 C++: If MODIFYCODE is INIT_EXPR, then leave references unbashed.
5232 build_modify_expr (lhs
, modifycode
, rhs
)
5234 enum tree_code modifycode
;
5237 register tree result
;
5239 tree lhstype
= TREE_TYPE (lhs
);
5240 tree olhstype
= lhstype
;
5243 /* Avoid duplicate error messages from operands that had errors. */
5244 if (TREE_CODE (lhs
) == ERROR_MARK
|| TREE_CODE (rhs
) == ERROR_MARK
)
5245 return error_mark_node
;
5247 /* Types that aren't fully specified cannot be used in assignments. */
5248 lhs
= require_complete_type (lhs
);
5252 /* Handle assignment to signature pointers/refs. */
5254 if (TYPE_LANG_SPECIFIC (lhstype
) &&
5255 (IS_SIGNATURE_POINTER (lhstype
) || IS_SIGNATURE_REFERENCE (lhstype
)))
5257 return build_signature_pointer_constructor (lhs
, rhs
);
5260 /* Handle control structure constructs used as "lvalues". */
5262 switch (TREE_CODE (lhs
))
5264 /* Handle --foo = 5; as these are valid constructs in C++ */
5265 case PREDECREMENT_EXPR
:
5266 case PREINCREMENT_EXPR
:
5267 if (TREE_SIDE_EFFECTS (TREE_OPERAND (lhs
, 0)))
5268 lhs
= build (TREE_CODE (lhs
), TREE_TYPE (lhs
),
5269 stabilize_reference (TREE_OPERAND (lhs
, 0)));
5270 return build (COMPOUND_EXPR
, lhstype
,
5272 build_modify_expr (TREE_OPERAND (lhs
, 0),
5275 /* Handle (a, b) used as an "lvalue". */
5277 newrhs
= build_modify_expr (TREE_OPERAND (lhs
, 1),
5279 if (TREE_CODE (newrhs
) == ERROR_MARK
)
5280 return error_mark_node
;
5281 return build (COMPOUND_EXPR
, lhstype
,
5282 TREE_OPERAND (lhs
, 0), newrhs
);
5285 newrhs
= build_modify_expr (TREE_OPERAND (lhs
, 0), modifycode
, rhs
);
5286 if (TREE_CODE (newrhs
) == ERROR_MARK
)
5287 return error_mark_node
;
5288 return build (COMPOUND_EXPR
, lhstype
, lhs
, newrhs
);
5290 /* Handle (a ? b : c) used as an "lvalue". */
5292 rhs
= save_expr (rhs
);
5294 /* Produce (a ? (b = rhs) : (c = rhs))
5295 except that the RHS goes through a save-expr
5296 so the code to compute it is only emitted once. */
5298 = build_conditional_expr (TREE_OPERAND (lhs
, 0),
5299 build_modify_expr (convert (TREE_TYPE (lhs
), TREE_OPERAND (lhs
, 1)),
5301 build_modify_expr (convert (TREE_TYPE (lhs
), TREE_OPERAND (lhs
, 2)),
5303 if (TREE_CODE (cond
) == ERROR_MARK
)
5305 /* Make sure the code to compute the rhs comes out
5306 before the split. */
5307 return build (COMPOUND_EXPR
, TREE_TYPE (lhs
),
5308 /* Case to void to suppress warning
5309 from warn_if_unused_value. */
5310 convert (void_type_node
, rhs
), cond
);
5314 if (TREE_CODE (lhs
) == OFFSET_REF
)
5316 if (TREE_OPERAND (lhs
, 0) == NULL_TREE
)
5318 /* Static class member? */
5319 tree member
= TREE_OPERAND (lhs
, 1);
5320 if (TREE_CODE (member
) == VAR_DECL
)
5324 compiler_error ("invalid static class member");
5325 return error_mark_node
;
5329 lhs
= resolve_offset_ref (lhs
);
5331 olhstype
= lhstype
= TREE_TYPE (lhs
);
5334 if (TREE_CODE (lhstype
) == REFERENCE_TYPE
5335 && modifycode
!= INIT_EXPR
)
5337 lhs
= convert_from_reference (lhs
);
5338 olhstype
= lhstype
= TREE_TYPE (lhs
);
5341 /* If a binary op has been requested, combine the old LHS value with the RHS
5342 producing the value we should actually store into the LHS. */
5344 if (modifycode
== INIT_EXPR
)
5346 if (! IS_AGGR_TYPE (lhstype
))
5347 /* Do the default thing */;
5348 else if (! TYPE_HAS_CONSTRUCTOR (lhstype
))
5350 cp_error ("`%T' has no constructors", lhstype
);
5351 return error_mark_node
;
5353 else if (TYPE_HAS_TRIVIAL_INIT_REF (lhstype
)
5354 && TYPE_MAIN_VARIANT (lhstype
) == TYPE_MAIN_VARIANT (TREE_TYPE (newrhs
)))
5355 /* Do the default thing */;
5358 result
= build_method_call (lhs
, constructor_name_full (lhstype
),
5359 build_tree_list (NULL_TREE
, rhs
),
5360 NULL_TREE
, LOOKUP_NORMAL
);
5361 if (result
== NULL_TREE
)
5362 return error_mark_node
;
5366 else if (modifycode
== NOP_EXPR
)
5368 /* `operator=' is not an inheritable operator. */
5369 if (! IS_AGGR_TYPE (lhstype
))
5370 /* Do the default thing */;
5371 else if (! TYPE_HAS_ASSIGNMENT (lhstype
))
5373 cp_error ("`%T' does not define operator=", lhstype
);
5374 return error_mark_node
;
5376 else if (TYPE_HAS_TRIVIAL_ASSIGN_REF (lhstype
)
5377 && TYPE_MAIN_VARIANT (lhstype
) == TYPE_MAIN_VARIANT (TREE_TYPE (newrhs
)))
5380 /* If we care about this, do overload resolution. */
5381 build_opfncall (MODIFY_EXPR
, LOOKUP_NORMAL
,
5382 lhs
, rhs
, make_node (NOP_EXPR
));
5384 /* Do the default thing */;
5388 result
= build_opfncall (MODIFY_EXPR
, LOOKUP_NORMAL
,
5389 lhs
, rhs
, make_node (NOP_EXPR
));
5390 if (result
== NULL_TREE
)
5391 return error_mark_node
;
5396 else if (PROMOTES_TO_AGGR_TYPE (lhstype
, REFERENCE_TYPE
))
5398 /* This case must convert to some sort of lvalue that
5399 can participate in an op= operation. */
5402 if (build_default_binary_type_conversion (modifycode
, &lhs_tmp
, &rhs_tmp
))
5404 lhs
= stabilize_reference (lhs_tmp
);
5405 /* Forget it was ever anything else. */
5406 olhstype
= lhstype
= TREE_TYPE (lhs
);
5407 newrhs
= build_binary_op (modifycode
, lhs
, rhs_tmp
, 1);
5411 cp_error ("no match for `%O(%#T, %#T)'", modifycode
,
5412 TREE_TYPE (lhs
), TREE_TYPE (rhs
));
5413 return error_mark_node
;
5418 lhs
= stabilize_reference (lhs
);
5419 newrhs
= build_binary_op (modifycode
, lhs
, rhs
, 1);
5422 /* Handle a cast used as an "lvalue".
5423 We have already performed any binary operator using the value as cast.
5424 Now convert the result to the cast type of the lhs,
5425 and then true type of the lhs and store it there;
5426 then convert result back to the cast type to be the value
5427 of the assignment. */
5429 switch (TREE_CODE (lhs
))
5434 case FIX_TRUNC_EXPR
:
5435 case FIX_FLOOR_EXPR
:
5436 case FIX_ROUND_EXPR
:
5438 if (TREE_CODE (TREE_TYPE (newrhs
)) == ARRAY_TYPE
5439 || TREE_CODE (TREE_TYPE (newrhs
)) == FUNCTION_TYPE
5440 || TREE_CODE (TREE_TYPE (newrhs
)) == METHOD_TYPE
5441 || TREE_CODE (TREE_TYPE (newrhs
)) == OFFSET_TYPE
)
5442 newrhs
= default_conversion (newrhs
);
5444 tree inner_lhs
= TREE_OPERAND (lhs
, 0);
5446 if (! lvalue_p (lhs
) && pedantic
)
5447 pedwarn ("cast to non-reference type used as lvalue");
5449 result
= build_modify_expr (inner_lhs
, NOP_EXPR
,
5450 convert (TREE_TYPE (inner_lhs
),
5451 convert (lhstype
, newrhs
)));
5452 if (TREE_CODE (result
) == ERROR_MARK
)
5454 return convert (TREE_TYPE (lhs
), result
);
5458 /* Now we have handled acceptable kinds of LHS that are not truly lvalues.
5459 Reject anything strange now. */
5461 if (!lvalue_or_else (lhs
, "assignment"))
5462 return error_mark_node
;
5464 GNU_xref_assign (lhs
);
5466 /* Warn about storing in something that is `const'. */
5467 /* For C++, don't warn if this is initialization. */
5468 if (modifycode
!= INIT_EXPR
5469 /* For assignment to `const' signature pointer/reference fields,
5470 don't warn either, we already printed a better message before. */
5471 && ! (TREE_CODE (lhs
) == COMPONENT_REF
5472 && (IS_SIGNATURE_POINTER (TREE_TYPE (TREE_OPERAND (lhs
, 0)))
5473 || IS_SIGNATURE_REFERENCE (TREE_TYPE (TREE_OPERAND (lhs
, 0)))))
5474 && (TREE_READONLY (lhs
) || TYPE_READONLY (lhstype
)
5475 || ((TREE_CODE (lhstype
) == RECORD_TYPE
5476 || TREE_CODE (lhstype
) == UNION_TYPE
)
5477 && C_TYPE_FIELDS_READONLY (lhstype
))
5478 || (TREE_CODE (lhstype
) == REFERENCE_TYPE
5479 && TYPE_READONLY (TREE_TYPE (lhstype
)))))
5480 readonly_error (lhs
, "assignment", 0);
5482 /* If storing into a structure or union member,
5483 it has probably been given type `int'.
5484 Compute the type that would go with
5485 the actual amount of storage the member occupies. */
5487 if (TREE_CODE (lhs
) == COMPONENT_REF
5488 && (TREE_CODE (lhstype
) == INTEGER_TYPE
5489 || TREE_CODE (lhstype
) == REAL_TYPE
5490 || TREE_CODE (lhstype
) == ENUMERAL_TYPE
))
5492 lhstype
= TREE_TYPE (get_unwidened (lhs
, 0));
5494 /* If storing in a field that is in actuality a short or narrower
5495 than one, we must store in the field in its actual type. */
5497 if (lhstype
!= TREE_TYPE (lhs
))
5499 lhs
= copy_node (lhs
);
5500 TREE_TYPE (lhs
) = lhstype
;
5504 /* check to see if there is an assignment to `this' */
5505 if (lhs
== current_class_decl
)
5507 if (flag_this_is_variable
> 0
5508 && DECL_NAME (current_function_decl
) != NULL_TREE
5509 && (DECL_NAME (current_function_decl
)
5510 != constructor_name (current_class_type
)))
5511 warning ("assignment to `this' not in constructor or destructor");
5512 current_function_just_assigned_this
= 1;
5515 /* The TREE_TYPE of RHS may be TYPE_UNKNOWN. This can happen
5516 when the type of RHS is not yet known, i.e. its type
5517 is inherited from LHS. */
5518 rhs
= require_instantiated_type (lhstype
, newrhs
, error_mark_node
);
5519 if (rhs
== error_mark_node
)
5520 return error_mark_node
;
5523 if (modifycode
!= INIT_EXPR
)
5525 /* Make modifycode now either a NOP_EXPR or an INIT_EXPR. */
5526 modifycode
= NOP_EXPR
;
5527 /* Reference-bashing */
5528 if (TREE_CODE (lhstype
) == REFERENCE_TYPE
)
5530 tree tmp
= convert_from_reference (lhs
);
5531 lhstype
= TREE_TYPE (tmp
);
5532 if (TYPE_SIZE (lhstype
) == 0)
5534 incomplete_type_error (lhs
, lhstype
);
5535 return error_mark_node
;
5540 if (TREE_CODE (TREE_TYPE (newrhs
)) == REFERENCE_TYPE
)
5542 tree tmp
= convert_from_reference (newrhs
);
5543 if (TYPE_SIZE (TREE_TYPE (tmp
)) == 0)
5545 incomplete_type_error (newrhs
, TREE_TYPE (tmp
));
5546 return error_mark_node
;
5552 if (TREE_SIDE_EFFECTS (lhs
))
5553 lhs
= stabilize_reference (lhs
);
5554 if (TREE_SIDE_EFFECTS (newrhs
))
5555 newrhs
= stabilize_reference (newrhs
);
5557 /* Convert new value to destination type. */
5559 if (TREE_CODE (lhstype
) == ARRAY_TYPE
)
5563 if (! comptypes (lhstype
, TREE_TYPE (rhs
), 0))
5565 cp_error ("incompatible types in assignment of `%T' to `%T'",
5566 TREE_TYPE (rhs
), lhstype
);
5567 return error_mark_node
;
5570 /* Allow array assignment in compiler-generated code. */
5571 if (pedantic
&& ! DECL_ARTIFICIAL (current_function_decl
))
5572 pedwarn ("ANSI C++ forbids assignment of arrays");
5574 /* Have to wrap this in RTL_EXPR for two cases:
5575 in base or member initialization and if we
5576 are a branch of a ?: operator. Since we
5577 can't easily know the latter, just do it always. */
5579 result
= make_node (RTL_EXPR
);
5581 TREE_TYPE (result
) = void_type_node
;
5582 do_pending_stack_adjust ();
5583 start_sequence_for_rtl_expr (result
);
5585 /* As a matter of principle, `start_sequence' should do this. */
5588 from_array
= TREE_CODE (TREE_TYPE (newrhs
)) == ARRAY_TYPE
5589 ? 1 + (modifycode
!= INIT_EXPR
): 0;
5590 expand_vec_init (lhs
, lhs
, array_type_nelts (lhstype
), newrhs
,
5593 do_pending_stack_adjust ();
5595 TREE_SIDE_EFFECTS (result
) = 1;
5596 RTL_EXPR_SEQUENCE (result
) = get_insns ();
5597 RTL_EXPR_RTL (result
) = const0_rtx
;
5602 if (modifycode
== INIT_EXPR
)
5604 newrhs
= convert_for_initialization (lhs
, lhstype
, newrhs
, LOOKUP_NORMAL
,
5605 "assignment", NULL_TREE
, 0);
5606 if (lhs
== DECL_RESULT (current_function_decl
))
5608 if (DECL_INITIAL (lhs
))
5609 warning ("return value from function receives multiple initializations");
5610 DECL_INITIAL (lhs
) = newrhs
;
5615 /* Avoid warnings on enum bit fields. */
5616 if (TREE_CODE (olhstype
) == ENUMERAL_TYPE
5617 && TREE_CODE (lhstype
) == INTEGER_TYPE
)
5619 newrhs
= convert_for_assignment (olhstype
, newrhs
, "assignment",
5621 newrhs
= convert_force (lhstype
, newrhs
, 0);
5624 newrhs
= convert_for_assignment (lhstype
, newrhs
, "assignment",
5626 if (TREE_CODE (newrhs
) == CALL_EXPR
5627 && TYPE_NEEDS_CONSTRUCTING (lhstype
))
5628 newrhs
= build_cplus_new (lhstype
, newrhs
, 0);
5630 /* Can't initialize directly from a TARGET_EXPR, since that would
5631 cause the lhs to be constructed twice, and possibly result in
5632 accidental self-initialization. So we force the TARGET_EXPR to be
5633 expanded without a target. */
5634 if (TREE_CODE (newrhs
) == TARGET_EXPR
)
5635 newrhs
= build (COMPOUND_EXPR
, TREE_TYPE (newrhs
), newrhs
,
5636 TREE_VALUE (newrhs
));
5639 if (TREE_CODE (newrhs
) == ERROR_MARK
)
5640 return error_mark_node
;
5642 if (TREE_CODE (newrhs
) == COND_EXPR
)
5645 tree cond
= TREE_OPERAND (newrhs
, 0);
5647 if (TREE_SIDE_EFFECTS (lhs
))
5648 cond
= build_compound_expr (tree_cons
5650 build_tree_list (NULL_TREE
, cond
)));
5652 /* Cannot have two identical lhs on this one tree (result) as preexpand
5653 calls will rip them out and fill in RTL for them, but when the
5654 rtl is generated, the calls will only be in the first side of the
5655 condition, not on both, or before the conditional jump! (mrs) */
5656 lhs1
= break_out_calls (lhs
);
5659 /* If there's no change, the COND_EXPR behaves like any other rhs. */
5660 result
= build (modifycode
== NOP_EXPR
? MODIFY_EXPR
: INIT_EXPR
,
5661 lhstype
, lhs
, newrhs
);
5664 tree result_type
= TREE_TYPE (newrhs
);
5665 /* We have to convert each arm to the proper type because the
5666 types may have been munged by constant folding. */
5668 = build (COND_EXPR
, result_type
, cond
,
5669 build_modify_expr (lhs
, modifycode
,
5670 convert (result_type
,
5671 TREE_OPERAND (newrhs
, 1))),
5672 build_modify_expr (lhs1
, modifycode
,
5673 convert (result_type
,
5674 TREE_OPERAND (newrhs
, 2))));
5678 result
= build (modifycode
== NOP_EXPR
? MODIFY_EXPR
: INIT_EXPR
,
5679 lhstype
, lhs
, newrhs
);
5681 TREE_SIDE_EFFECTS (result
) = 1;
5683 /* If we got the LHS in a different type for storing in,
5684 convert the result back to the nominal type of LHS
5685 so that the value we return always has the same type
5686 as the LHS argument. */
5688 if (olhstype
== TREE_TYPE (result
))
5690 /* Avoid warnings converting integral types back into enums
5691 for enum bit fields. */
5692 if (TREE_CODE (TREE_TYPE (result
)) == INTEGER_TYPE
5693 && TREE_CODE (olhstype
) == ENUMERAL_TYPE
)
5695 result
= build (COMPOUND_EXPR
, olhstype
, result
, olhs
);
5696 TREE_NO_UNUSED_WARNING (result
) = 1;
5699 return convert_for_assignment (olhstype
, result
, "assignment",
5704 /* Return 0 if EXP is not a valid lvalue in this language
5705 even though `lvalue_or_else' would accept it. */
5708 language_lvalue_valid (exp
)
5714 /* Get difference in deltas for different pointer to member function
5715 types. Return integer_zero_node, if FROM cannot be converted to a
5716 TO type. If FORCE is true, then allow reverse conversions as well. */
5718 get_delta_difference (from
, to
, force
)
5722 tree delta
= integer_zero_node
;
5728 /* Should get_base_distance here, so we can check if any thing along the
5729 path is virtual, and we need to make sure we stay
5730 inside the real binfos when going through virtual bases.
5731 Maybe we should replace virtual bases with
5732 binfo_member (...CLASSTYPE_VBASECLASSES...)... (mrs) */
5733 binfo
= get_binfo (from
, to
, 1);
5734 if (binfo
== error_mark_node
)
5736 error (" in pointer to member function conversion");
5743 error_not_base_type (from
, to
);
5744 error (" in pointer to member function conversion");
5747 binfo
= get_binfo (to
, from
, 1);
5748 if (binfo
== error_mark_node
)
5750 error (" in pointer to member function conversion");
5755 error ("cannot convert pointer to member of type %T to unrelated pointer to member of type %T", from
, to
);
5758 if (TREE_VIA_VIRTUAL (binfo
))
5760 warning ("pointer to member conversion to virtual base class will only work if you are very careful");
5762 return build_binary_op (MINUS_EXPR
,
5764 BINFO_OFFSET (binfo
), 1);
5766 if (TREE_VIA_VIRTUAL (binfo
))
5768 warning ("pointer to member conversion from virtual base class will only work if you are very careful");
5770 return BINFO_OFFSET (binfo
);
5773 /* Build a constructor for a pointer to member function. It can be
5774 used to initialize global variables, local variable, or used
5775 as a value in expressions. TYPE is the POINTER to METHOD_TYPE we
5778 If FORCE is non-zero, then force this conversion, even if
5779 we would rather not do it. Usually set when using an explicit
5782 Return error_mark_node, if something goes wrong. */
5785 build_ptrmemfunc (type
, pfn
, force
)
5789 tree index
= integer_zero_node
;
5790 tree delta
= integer_zero_node
;
5791 tree delta2
= integer_zero_node
;
5796 /* Handle multiple conversions of pointer to member functions. */
5797 if (TYPE_PTRMEMFUNC_P (TREE_TYPE (pfn
)))
5799 tree ndelta
, ndelta2
, nindex
;
5800 /* Is is already the right type? */
5801 if (type
== TYPE_PTRMEMFUNC_FN_TYPE (TREE_TYPE (pfn
)))
5804 if (TREE_CODE (pfn
) != CONSTRUCTOR
)
5807 ndelta
= convert (ptrdiff_type_node
, build_component_ref (pfn
, delta_identifier
, 0, 0));
5808 ndelta2
= convert (ptrdiff_type_node
, DELTA2_FROM_PTRMEMFUNC (pfn
));
5809 index
= build_component_ref (pfn
, index_identifier
, 0, 0);
5810 delta
= get_delta_difference (TYPE_METHOD_BASETYPE (TREE_TYPE (TYPE_PTRMEMFUNC_FN_TYPE (TREE_TYPE (pfn
)))),
5811 TYPE_METHOD_BASETYPE (TREE_TYPE (type
)),
5813 delta
= build_binary_op (PLUS_EXPR
, delta
, ndelta
, 1);
5814 delta2
= build_binary_op (PLUS_EXPR
, ndelta2
, delta2
, 1);
5815 e1
= fold (build (GT_EXPR
, boolean_type_node
, index
, integer_zero_node
));
5817 u
= build_nt (CONSTRUCTOR
, 0, tree_cons (delta2_identifier
, delta2
, NULL_TREE
));
5818 u
= build_nt (CONSTRUCTOR
, 0, tree_cons (NULL_TREE
, delta
,
5819 tree_cons (NULL_TREE
, index
,
5820 tree_cons (NULL_TREE
, u
, NULL_TREE
))));
5821 e2
= digest_init (TYPE_GET_PTRMEMFUNC_TYPE (type
), u
, (tree
*)0);
5823 pfn
= PFN_FROM_PTRMEMFUNC (pfn
);
5824 npfn
= build1 (NOP_EXPR
, type
, pfn
);
5825 TREE_CONSTANT (npfn
) = TREE_CONSTANT (pfn
);
5827 u
= build_nt (CONSTRUCTOR
, 0, tree_cons (pfn_identifier
, npfn
, NULL_TREE
));
5828 u
= build_nt (CONSTRUCTOR
, 0, tree_cons (NULL_TREE
, delta
,
5829 tree_cons (NULL_TREE
, index
,
5830 tree_cons (NULL_TREE
, u
, NULL_TREE
))));
5831 e3
= digest_init (TYPE_GET_PTRMEMFUNC_TYPE (type
), u
, (tree
*)0);
5832 return build_conditional_expr (e1
, e2
, e3
);
5835 ndelta
= TREE_VALUE (CONSTRUCTOR_ELTS (pfn
));
5836 nindex
= TREE_VALUE (TREE_CHAIN (CONSTRUCTOR_ELTS (pfn
)));
5837 npfn
= TREE_VALUE (TREE_CHAIN (TREE_CHAIN (CONSTRUCTOR_ELTS (pfn
))));
5838 npfn
= TREE_VALUE (CONSTRUCTOR_ELTS (npfn
));
5839 if (integer_zerop (nindex
))
5840 pfn
= integer_zero_node
;
5841 else if (integer_zerop (fold (size_binop (PLUS_EXPR
, nindex
, integer_one_node
))))
5844 delta
= get_delta_difference (TYPE_METHOD_BASETYPE (TREE_TYPE (TYPE_PTRMEMFUNC_FN_TYPE (TREE_TYPE (pfn
)))),
5845 TYPE_METHOD_BASETYPE (TREE_TYPE (type
)),
5847 delta
= build_binary_op (PLUS_EXPR
, delta
, ndelta
, 1);
5848 pfn
= build1 (NOP_EXPR
, type
, npfn
);
5849 TREE_CONSTANT (pfn
) = TREE_CONSTANT (npfn
);
5851 u
= build_nt (CONSTRUCTOR
, 0, tree_cons (pfn_identifier
, pfn
, NULL_TREE
));
5852 u
= build_nt (CONSTRUCTOR
, 0, tree_cons (NULL_TREE
, delta
,
5853 tree_cons (NULL_TREE
, nindex
,
5854 tree_cons (NULL_TREE
, u
, NULL_TREE
))));
5855 e3
= digest_init (TYPE_GET_PTRMEMFUNC_TYPE (type
), u
, (tree
*)0);
5860 sorry ("value casting of variable nonnull pointer to member functions not supported");
5861 return error_mark_node
;
5865 /* Handle null pointer to member function conversions. */
5866 if (integer_zerop (pfn
))
5868 pfn
= build_c_cast (type
, integer_zero_node
, 0);
5869 u
= build_nt (CONSTRUCTOR
, 0, tree_cons (pfn_identifier
, pfn
, NULL_TREE
));
5870 u
= build_nt (CONSTRUCTOR
, 0, tree_cons (NULL_TREE
, integer_zero_node
,
5871 tree_cons (NULL_TREE
, integer_zero_node
,
5872 tree_cons (NULL_TREE
, u
, NULL_TREE
))));
5873 return digest_init (TYPE_GET_PTRMEMFUNC_TYPE (type
), u
, (tree
*)0);
5876 if (TREE_CODE (pfn
) == TREE_LIST
5877 || (TREE_CODE (pfn
) == ADDR_EXPR
5878 && TREE_CODE (TREE_OPERAND (pfn
, 0)) == TREE_LIST
))
5880 pfn
= instantiate_type (type
, pfn
, 1);
5881 if (pfn
== error_mark_node
)
5882 return error_mark_node
;
5883 if (TREE_CODE (pfn
) != ADDR_EXPR
)
5884 pfn
= build_unary_op (ADDR_EXPR
, pfn
, 0);
5887 /* Allow pointer to member conversions here. */
5888 delta
= get_delta_difference (TYPE_METHOD_BASETYPE (TREE_TYPE (TREE_TYPE (pfn
))),
5889 TYPE_METHOD_BASETYPE (TREE_TYPE (type
)),
5891 delta2
= build_binary_op (PLUS_EXPR
, delta2
, delta
, 1);
5893 if (TREE_CODE (TREE_OPERAND (pfn
, 0)) != FUNCTION_DECL
)
5894 warning ("assuming pointer to member function is non-virtual");
5896 if (TREE_CODE (TREE_OPERAND (pfn
, 0)) == FUNCTION_DECL
5897 && DECL_VINDEX (TREE_OPERAND (pfn
, 0)))
5899 /* Find the offset to the vfield pointer in the object. */
5900 vfield_offset
= get_binfo (DECL_CONTEXT (TREE_OPERAND (pfn
, 0)),
5901 DECL_CLASS_CONTEXT (TREE_OPERAND (pfn
, 0)),
5903 vfield_offset
= get_vfield_offset (vfield_offset
);
5904 delta2
= size_binop (PLUS_EXPR
, vfield_offset
, delta2
);
5906 /* Map everything down one to make room for the null pointer to member. */
5907 index
= size_binop (PLUS_EXPR
,
5908 DECL_VINDEX (TREE_OPERAND (pfn
, 0)),
5910 u
= build_nt (CONSTRUCTOR
, 0, tree_cons (delta2_identifier
, delta2
, NULL_TREE
));
5914 index
= size_binop (MINUS_EXPR
, integer_zero_node
, integer_one_node
);
5916 npfn
= build1 (NOP_EXPR
, type
, pfn
);
5917 TREE_CONSTANT (npfn
) = TREE_CONSTANT (pfn
);
5919 u
= build_nt (CONSTRUCTOR
, 0, tree_cons (pfn_identifier
, npfn
, NULL_TREE
));
5922 u
= build_nt (CONSTRUCTOR
, 0, tree_cons (NULL_TREE
, delta
,
5923 tree_cons (NULL_TREE
, index
,
5924 tree_cons (NULL_TREE
, u
, NULL_TREE
))));
5925 return digest_init (TYPE_GET_PTRMEMFUNC_TYPE (type
), u
, (tree
*)0);
5928 /* Convert value RHS to type TYPE as preparation for an assignment
5929 to an lvalue of type TYPE.
5930 The real work of conversion is done by `convert'.
5931 The purpose of this function is to generate error messages
5932 for assignments that are not allowed in C.
5933 ERRTYPE is a string to use in error messages:
5934 "assignment", "return", etc.
5936 C++: attempts to allow `convert' to find conversions involving
5937 implicit type conversion between aggregate and scalar types
5938 as per 8.5.6 of C++ manual. Does not randomly dereference
5939 pointers to aggregates! */
5942 convert_for_assignment (type
, rhs
, errtype
, fndecl
, parmnum
)
5948 register enum tree_code codel
= TREE_CODE (type
);
5949 register tree rhstype
;
5950 register enum tree_code coder
= TREE_CODE (TREE_TYPE (rhs
));
5952 if (coder
== UNKNOWN_TYPE
)
5953 rhs
= instantiate_type (type
, rhs
, 1);
5955 if (coder
== ERROR_MARK
)
5956 return error_mark_node
;
5958 if (codel
== OFFSET_TYPE
)
5960 type
= TREE_TYPE (type
);
5961 codel
= TREE_CODE (type
);
5964 /* Strip NON_LVALUE_EXPRs since we aren't using as an lvalue. */
5965 if (TREE_CODE (rhs
) == NON_LVALUE_EXPR
)
5966 rhs
= TREE_OPERAND (rhs
, 0);
5968 if (rhs
== error_mark_node
)
5969 return error_mark_node
;
5971 if (TREE_VALUE (rhs
) == error_mark_node
)
5972 return error_mark_node
;
5974 if (TREE_CODE (TREE_TYPE (rhs
)) == OFFSET_TYPE
)
5976 rhs
= resolve_offset_ref (rhs
);
5977 if (rhs
== error_mark_node
)
5978 return error_mark_node
;
5979 rhstype
= TREE_TYPE (rhs
);
5980 coder
= TREE_CODE (rhstype
);
5983 if (TREE_CODE (TREE_TYPE (rhs
)) == ARRAY_TYPE
5984 || TREE_CODE (TREE_TYPE (rhs
)) == FUNCTION_TYPE
5985 || TREE_CODE (TREE_TYPE (rhs
)) == METHOD_TYPE
)
5986 rhs
= default_conversion (rhs
);
5987 else if (TREE_CODE (TREE_TYPE (rhs
)) == REFERENCE_TYPE
)
5988 rhs
= convert_from_reference (rhs
);
5990 rhstype
= TREE_TYPE (rhs
);
5991 coder
= TREE_CODE (rhstype
);
5993 /* This should no longer change types on us. */
5994 if (TREE_CODE (rhs
) == CONST_DECL
)
5995 rhs
= DECL_INITIAL (rhs
);
5996 else if (TREE_READONLY_DECL_P (rhs
))
5997 rhs
= decl_constant_value (rhs
);
5999 if (type
== rhstype
)
6001 overflow_warning (rhs
);
6005 if (coder
== VOID_TYPE
)
6007 error ("void value not ignored as it ought to be");
6008 return error_mark_node
;
6010 /* Arithmetic types all interconvert. */
6011 if ((codel
== INTEGER_TYPE
|| codel
== REAL_TYPE
|| codel
== BOOLEAN_TYPE
)
6012 && (coder
== INTEGER_TYPE
|| coder
== REAL_TYPE
|| coder
== BOOLEAN_TYPE
))
6014 /* But we should warn if assigning REAL_TYPE to INTEGER_TYPE. */
6015 if (coder
== REAL_TYPE
&& codel
== INTEGER_TYPE
)
6018 cp_warning ("`%T' used for argument %P of `%D'",
6019 rhstype
, parmnum
, fndecl
);
6021 cp_warning ("%s to `%T' from `%T'", errtype
, type
, rhstype
);
6023 /* And we should warn if assigning a negative value to
6024 an unsigned variable. */
6025 else if (TREE_UNSIGNED (type
) && codel
!= BOOLEAN_TYPE
)
6027 if (TREE_CODE (rhs
) == INTEGER_CST
6028 && TREE_NEGATED_INT (rhs
))
6031 cp_warning ("negative value `%E' passed as argument %P of `%D'",
6032 rhs
, parmnum
, fndecl
);
6034 cp_warning ("%s of negative value `%E' to `%T'",
6035 errtype
, rhs
, type
);
6037 overflow_warning (rhs
);
6038 if (TREE_CONSTANT (rhs
))
6042 return convert_and_check (type
, rhs
);
6044 /* Conversions involving enums. */
6045 else if ((codel
== ENUMERAL_TYPE
6046 && (INTEGRAL_CODE_P (coder
) || coder
== REAL_TYPE
))
6047 || (coder
== ENUMERAL_TYPE
6048 && (INTEGRAL_CODE_P (codel
) || codel
== REAL_TYPE
)))
6050 return cp_convert (type
, rhs
, CONV_IMPLICIT
, LOOKUP_NORMAL
);
6052 /* Conversions among pointers */
6053 else if (codel
== POINTER_TYPE
6054 && (coder
== POINTER_TYPE
6055 || (coder
== RECORD_TYPE
6056 && (IS_SIGNATURE_POINTER (rhstype
)
6057 || IS_SIGNATURE_REFERENCE (rhstype
)))))
6059 register tree ttl
= TREE_TYPE (type
);
6063 if (coder
== RECORD_TYPE
)
6065 rhs
= build_optr_ref (rhs
);
6066 rhstype
= TREE_TYPE (rhs
);
6068 ttr
= TREE_TYPE (rhstype
);
6070 /* If both pointers are of aggregate type, then we
6071 can give better error messages, and save some work
6073 if (TREE_CODE (ttl
) == RECORD_TYPE
&& TREE_CODE (ttr
) == RECORD_TYPE
)
6077 if (TYPE_MAIN_VARIANT (ttl
) == TYPE_MAIN_VARIANT (ttr
)
6078 || type
== class_star_type_node
6079 || rhstype
== class_star_type_node
)
6080 binfo
= TYPE_BINFO (ttl
);
6082 binfo
= get_binfo (ttl
, ttr
, 1);
6084 if (binfo
== error_mark_node
)
6085 return error_mark_node
;
6087 return error_not_base_type (ttl
, ttr
);
6089 if (! TYPE_READONLY (ttl
) && TYPE_READONLY (ttr
))
6092 cp_pedwarn ("passing `%T' as argument %P of `%D' discards const",
6093 rhstype
, parmnum
, fndecl
);
6095 cp_pedwarn ("%s to `%T' from `%T' discards const",
6096 errtype
, type
, rhstype
);
6098 if (! TYPE_VOLATILE (ttl
) && TYPE_VOLATILE (ttr
))
6101 cp_pedwarn ("passing `%T' as argument %P of `%D' discards volatile",
6102 rhstype
, parmnum
, fndecl
);
6104 cp_pedwarn ("%s to `%T' from `%T' discards volatile",
6105 errtype
, type
, rhstype
);
6109 /* Any non-function converts to a [const][volatile] void *
6110 and vice versa; otherwise, targets must be the same.
6111 Meanwhile, the lhs target must have all the qualifiers of the rhs. */
6112 else if (TYPE_MAIN_VARIANT (ttl
) == void_type_node
6113 || TYPE_MAIN_VARIANT (ttr
) == void_type_node
6114 || (ctt
= comp_target_types (type
, rhstype
, 1))
6115 || (unsigned_type (TYPE_MAIN_VARIANT (ttl
))
6116 == unsigned_type (TYPE_MAIN_VARIANT (ttr
))))
6118 /* ARM $4.8, commentary on p39. */
6119 if (TYPE_MAIN_VARIANT (ttl
) == void_type_node
6120 && TREE_CODE (ttr
) == OFFSET_TYPE
)
6122 cp_error ("no standard conversion from `%T' to `void *'", ttr
);
6123 return error_mark_node
;
6127 cp_pedwarn ("converting `%T' to `%T' is a contravariance violation",
6130 if (TYPE_MAIN_VARIANT (ttl
) != void_type_node
6131 && TYPE_MAIN_VARIANT (ttr
) == void_type_node
6132 && rhs
!= null_pointer_node
)
6134 if (coder
== RECORD_TYPE
)
6135 cp_pedwarn ("implicit conversion of signature pointer to type `%T'",
6138 pedwarn ("ANSI C++ forbids implicit conversion from `void *' in %s",
6141 /* Const and volatile mean something different for function types,
6142 so the usual warnings are not appropriate. */
6143 else if ((TREE_CODE (ttr
) != FUNCTION_TYPE
&& TREE_CODE (ttr
) != METHOD_TYPE
)
6144 || (TREE_CODE (ttl
) != FUNCTION_TYPE
&& TREE_CODE (ttl
) != METHOD_TYPE
))
6146 if (TREE_CODE (ttl
) == OFFSET_TYPE
6147 && binfo_member (TYPE_OFFSET_BASETYPE (ttr
),
6148 CLASSTYPE_VBASECLASSES (TYPE_OFFSET_BASETYPE (ttl
))))
6150 sorry ("%s between pointer to members converting across virtual baseclasses", errtype
);
6151 return error_mark_node
;
6153 else if (! TYPE_READONLY (ttl
) && TYPE_READONLY (ttr
))
6156 cp_pedwarn ("passing `%T' as argument %P of `%D' discards const",
6157 rhstype
, parmnum
, fndecl
);
6159 cp_pedwarn ("%s to `%T' from `%T' discards const",
6160 errtype
, type
, rhstype
);
6162 else if (! TYPE_VOLATILE (ttl
) && TYPE_VOLATILE (ttr
))
6165 cp_pedwarn ("passing `%T' as argument %P of `%D' discards volatile",
6166 rhstype
, parmnum
, fndecl
);
6168 cp_pedwarn ("%s to `%T' from `%T' discards volatile",
6169 errtype
, type
, rhstype
);
6171 else if (TREE_CODE (ttl
) == TREE_CODE (ttr
)
6172 && ! comp_target_types (type
, rhstype
, 1))
6175 cp_pedwarn ("passing `%T' as argument %P of `%D' changes signedness",
6176 rhstype
, parmnum
, fndecl
);
6178 cp_pedwarn ("%s to `%T' from `%T' changes signedness",
6179 errtype
, type
, rhstype
);
6183 else if (TREE_CODE (ttr
) == OFFSET_TYPE
6184 && TREE_CODE (ttl
) != OFFSET_TYPE
)
6186 /* Normally, pointers to different type codes (other
6187 than void) are not compatible, but we perform
6188 some type instantiation if that resolves the
6189 ambiguity of (X Y::*) and (X *). */
6191 if (current_class_decl
)
6193 if (TREE_CODE (rhs
) == INTEGER_CST
)
6195 rhs
= build (PLUS_EXPR
, build_pointer_type (TREE_TYPE (ttr
)),
6196 current_class_decl
, rhs
);
6197 return convert_for_assignment (type
, rhs
,
6198 errtype
, fndecl
, parmnum
);
6201 if (TREE_CODE (ttl
) == METHOD_TYPE
)
6202 error ("%s between pointer-to-method and pointer-to-member types",
6205 error ("%s between pointer and pointer-to-member types", errtype
);
6206 return error_mark_node
;
6210 int add_quals
= 0, const_parity
= 0, volatile_parity
= 0;
6212 int unsigned_parity
;
6215 /* This code is basically a duplicate of comp_ptr_ttypes_real. */
6216 for (; ; ttl
= TREE_TYPE (ttl
), ttr
= TREE_TYPE (ttr
))
6219 const_parity
|= TYPE_READONLY (ttl
) < TYPE_READONLY (ttr
);
6220 volatile_parity
|= TYPE_VOLATILE (ttl
) < TYPE_VOLATILE (ttr
);
6223 && (TYPE_READONLY (ttl
) > TYPE_READONLY (ttr
)
6224 || TYPE_VOLATILE (ttl
) > TYPE_VOLATILE (ttr
)))
6226 left_const
&= TYPE_READONLY (ttl
);
6228 if (TREE_CODE (ttl
) != POINTER_TYPE
6229 || TREE_CODE (ttr
) != POINTER_TYPE
)
6232 unsigned_parity
= TREE_UNSIGNED (ttl
) - TREE_UNSIGNED (ttr
);
6233 if (unsigned_parity
)
6235 if (TREE_UNSIGNED (ttl
))
6236 ttr
= unsigned_type (ttr
);
6238 ttl
= unsigned_type (ttl
);
6241 if (comp_target_types (ttl
, ttr
, nptrs
) > 0)
6246 cp_pedwarn ("passing `%T' as argument %P of `%D' adds cv-quals without intervening `const'",
6247 rhstype
, parmnum
, fndecl
);
6249 cp_pedwarn ("%s to `%T' from `%T' adds cv-quals without intervening `const'",
6250 errtype
, type
, rhstype
);
6255 cp_pedwarn ("passing `%T' as argument %P of `%D' discards const",
6256 rhstype
, parmnum
, fndecl
);
6258 cp_pedwarn ("%s to `%T' from `%T' discards const",
6259 errtype
, type
, rhstype
);
6261 if (volatile_parity
)
6264 cp_pedwarn ("passing `%T' as argument %P of `%D' discards volatile",
6265 rhstype
, parmnum
, fndecl
);
6267 cp_pedwarn ("%s to `%T' from `%T' discards volatile",
6268 errtype
, type
, rhstype
);
6270 if (unsigned_parity
> 0)
6273 cp_pedwarn ("passing `%T' as argument %P of `%D' changes signed to unsigned",
6274 rhstype
, parmnum
, fndecl
);
6276 cp_pedwarn ("%s to `%T' from `%T' changes signed to unsigned",
6277 errtype
, type
, rhstype
);
6279 else if (unsigned_parity
< 0)
6282 cp_pedwarn ("passing `%T' as argument %P of `%D' changes unsigned to signed",
6283 rhstype
, parmnum
, fndecl
);
6285 cp_pedwarn ("%s to `%T' from `%T' changes unsigned to signed",
6286 errtype
, type
, rhstype
);
6289 /* C++ is not so friendly about converting function and
6290 member function pointers as C. Emit warnings here. */
6291 if (TREE_CODE (ttl
) == FUNCTION_TYPE
6292 || TREE_CODE (ttl
) == METHOD_TYPE
)
6293 if (! comptypes (ttl
, ttr
, 0))
6295 warning ("conflicting function types in %s:", errtype
);
6296 cp_warning ("\t`%T' != `%T'", type
, rhstype
);
6299 else if (TREE_CODE (TREE_TYPE (rhs
)) == METHOD_TYPE
)
6301 /* When does this happen? */
6302 my_friendly_abort (119);
6303 /* Conversion of a pointer-to-member type to void *. */
6304 rhs
= build_unary_op (ADDR_EXPR
, rhs
, 0);
6305 TREE_TYPE (rhs
) = type
;
6308 else if (TREE_CODE (TREE_TYPE (rhs
)) == OFFSET_TYPE
)
6310 /* When does this happen? */
6311 my_friendly_abort (120);
6312 /* Conversion of a pointer-to-member type to void *. */
6313 rhs
= build_unary_op (ADDR_EXPR
, rhs
, 0);
6314 TREE_TYPE (rhs
) = type
;
6320 cp_error ("passing `%T' as argument %P of `%D'",
6321 rhstype
, parmnum
, fndecl
);
6323 cp_error ("%s to `%T' from `%T'", errtype
, type
, rhstype
);
6324 return error_mark_node
;
6327 return convert (type
, rhs
);
6329 else if (codel
== POINTER_TYPE
&& coder
== INTEGER_TYPE
)
6331 /* An explicit constant 0 can convert to a pointer,
6332 but not a 0 that results from casting or folding. */
6333 if (! (TREE_CODE (rhs
) == INTEGER_CST
&& integer_zerop (rhs
)))
6336 cp_pedwarn ("passing `%T' to argument %P of `%D' lacks a cast",
6337 rhstype
, parmnum
, fndecl
);
6339 cp_pedwarn ("%s to `%T' from `%T' lacks a cast",
6340 errtype
, type
, rhstype
);
6341 return convert (type
, rhs
);
6343 return null_pointer_node
;
6345 else if (codel
== INTEGER_TYPE
6346 && (coder
== POINTER_TYPE
6347 || (coder
== RECORD_TYPE
6348 && (IS_SIGNATURE_POINTER (rhstype
)
6349 || TYPE_PTRMEMFUNC_FLAG (rhstype
)
6350 || IS_SIGNATURE_REFERENCE (rhstype
)))))
6353 cp_pedwarn ("passing `%T' to argument %P of `%D' lacks a cast",
6354 rhstype
, parmnum
, fndecl
);
6356 cp_pedwarn ("%s to `%T' from `%T' lacks a cast",
6357 errtype
, type
, rhstype
);
6358 return convert (type
, rhs
);
6360 else if (codel
== BOOLEAN_TYPE
6361 && (coder
== POINTER_TYPE
6362 || (coder
== RECORD_TYPE
6363 && (IS_SIGNATURE_POINTER (rhstype
)
6364 || TYPE_PTRMEMFUNC_FLAG (rhstype
)
6365 || IS_SIGNATURE_REFERENCE (rhstype
)))))
6366 return convert (type
, rhs
);
6369 else if (((coder
== POINTER_TYPE
6370 && TREE_CODE (TREE_TYPE (rhstype
)) == METHOD_TYPE
)
6371 || integer_zerop (rhs
)
6372 || TYPE_PTRMEMFUNC_P (rhstype
))
6373 && TYPE_PTRMEMFUNC_P (type
))
6375 tree ttl
= TYPE_PTRMEMFUNC_FN_TYPE (type
);
6376 tree ttr
= (TREE_CODE (rhstype
) == POINTER_TYPE
? rhstype
6377 : TYPE_PTRMEMFUNC_FN_TYPE (type
));
6378 int ctt
= comp_target_types (ttl
, ttr
, 1);
6381 cp_pedwarn ("converting `%T' to `%T' is a contravariance violation",
6384 cp_error ("%s to `%T' from `%T'", errtype
, ttl
, ttr
);
6386 /* compatible pointer to member functions. */
6387 return build_ptrmemfunc (ttl
, rhs
, 0);
6389 else if (codel
== ERROR_MARK
|| coder
== ERROR_MARK
)
6390 return error_mark_node
;
6392 /* This should no longer happen. References are initialized via
6393 `convert_for_initialization'. They should otherwise be
6394 bashed before coming here. */
6395 else if (codel
== REFERENCE_TYPE
)
6396 my_friendly_abort (317);
6397 else if (TYPE_MAIN_VARIANT (type
) == TYPE_MAIN_VARIANT (TREE_TYPE (rhs
)))
6399 tree nrhs
= build1 (NOP_EXPR
, type
, rhs
);
6400 TREE_CONSTANT (nrhs
) = TREE_CONSTANT (rhs
);
6403 else if (TYPE_HAS_CONSTRUCTOR (type
) || IS_AGGR_TYPE (TREE_TYPE (rhs
)))
6404 return convert (type
, rhs
);
6406 cp_error ("%s to `%T' from `%T'", errtype
, type
, rhstype
);
6407 return error_mark_node
;
6410 /* Convert RHS to be of type TYPE. If EXP is non-zero,
6411 it is the target of the initialization.
6412 ERRTYPE is a string to use in error messages.
6414 Two major differences between the behavior of
6415 `convert_for_assignment' and `convert_for_initialization'
6416 are that references are bashed in the former, while
6417 copied in the latter, and aggregates are assigned in
6418 the former (operator=) while initialized in the
6421 If using constructor make sure no conversion operator exists, if one does
6422 exist, an ambiguity exists.
6424 If flags doesn't include LOOKUP_COMPLAIN, don't complain about anything. */
6426 convert_for_initialization (exp
, type
, rhs
, flags
, errtype
, fndecl
, parmnum
)
6427 tree exp
, type
, rhs
;
6433 register enum tree_code codel
= TREE_CODE (type
);
6434 register tree rhstype
;
6435 register enum tree_code coder
;
6437 /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
6438 Strip such NOP_EXPRs, since RHS is used in non-lvalue context. */
6439 if (TREE_CODE (rhs
) == NOP_EXPR
6440 && TREE_TYPE (rhs
) == TREE_TYPE (TREE_OPERAND (rhs
, 0))
6441 && codel
!= REFERENCE_TYPE
)
6442 rhs
= TREE_OPERAND (rhs
, 0);
6444 if (rhs
== error_mark_node
6445 || (TREE_CODE (rhs
) == TREE_LIST
&& TREE_VALUE (rhs
) == error_mark_node
))
6446 return error_mark_node
;
6448 if (TREE_CODE (TREE_TYPE (rhs
)) == OFFSET_TYPE
)
6450 rhs
= resolve_offset_ref (rhs
);
6451 if (rhs
== error_mark_node
)
6452 return error_mark_node
;
6455 if (TREE_CODE (TREE_TYPE (rhs
)) == REFERENCE_TYPE
)
6456 rhs
= convert_from_reference (rhs
);
6458 if ((TREE_CODE (TREE_TYPE (rhs
)) == ARRAY_TYPE
6459 && TREE_CODE (type
) != ARRAY_TYPE
6460 && (TREE_CODE (type
) != REFERENCE_TYPE
6461 || TREE_CODE (TREE_TYPE (type
)) != ARRAY_TYPE
))
6462 || TREE_CODE (TREE_TYPE (rhs
)) == FUNCTION_TYPE
6463 || TREE_CODE (TREE_TYPE (rhs
)) == METHOD_TYPE
)
6464 rhs
= default_conversion (rhs
);
6466 rhstype
= TREE_TYPE (rhs
);
6467 coder
= TREE_CODE (rhstype
);
6469 if (coder
== UNKNOWN_TYPE
)
6471 rhs
= instantiate_type (type
, rhs
, 1);
6472 rhstype
= TREE_TYPE (rhs
);
6473 coder
= TREE_CODE (rhstype
);
6476 if (coder
== ERROR_MARK
)
6477 return error_mark_node
;
6479 /* We accept references to incomplete types, so we can
6480 return here before checking if RHS is of complete type. */
6482 if (codel
== REFERENCE_TYPE
)
6484 /* This should eventually happen in convert_arguments. */
6485 extern int warningcount
, errorcount
;
6489 savew
= warningcount
, savee
= errorcount
;
6490 rhs
= convert_to_reference (type
, rhs
, CONV_IMPLICIT
, flags
,
6491 exp
? exp
: error_mark_node
);
6494 if (warningcount
> savew
)
6495 cp_warning_at ("in passing argument %P of `%+D'", parmnum
, fndecl
);
6496 else if (errorcount
> savee
)
6497 cp_error_at ("in passing argument %P of `%+D'", parmnum
, fndecl
);
6502 rhs
= require_complete_type (rhs
);
6503 if (rhs
== error_mark_node
)
6504 return error_mark_node
;
6506 if (exp
!= 0) exp
= require_complete_type (exp
);
6507 if (exp
== error_mark_node
)
6508 return error_mark_node
;
6510 if (TREE_CODE (rhstype
) == REFERENCE_TYPE
)
6511 rhstype
= TREE_TYPE (rhstype
);
6513 if (TYPE_LANG_SPECIFIC (type
)
6514 && (IS_SIGNATURE_POINTER (type
) || IS_SIGNATURE_REFERENCE (type
)))
6515 return build_signature_pointer_constructor (type
, rhs
);
6517 if (IS_AGGR_TYPE (type
)
6518 && (TYPE_NEEDS_CONSTRUCTING (type
) || TREE_HAS_CONSTRUCTOR (rhs
)))
6520 if (TYPE_MAIN_VARIANT (type
) == TYPE_MAIN_VARIANT (rhstype
))
6522 /* This is sufficient to perform initialization. No need,
6523 apparently, to go through X(X&) to do first-cut
6524 initialization. Return through a TARGET_EXPR so that we get
6525 cleanups if it is used. */
6526 if (TREE_CODE (rhs
) == CALL_EXPR
)
6528 rhs
= build_cplus_new (type
, rhs
, 0);
6531 /* Handle the case of default parameter initialization and
6532 initialization of static variables. */
6533 else if (TREE_CODE (rhs
) == TARGET_EXPR
)
6535 else if (TREE_CODE (rhs
) == INDIRECT_REF
&& TREE_HAS_CONSTRUCTOR (rhs
))
6537 my_friendly_assert (TREE_CODE (TREE_OPERAND (rhs
, 0)) == CALL_EXPR
, 318);
6540 my_friendly_assert (TREE_VALUE (TREE_OPERAND (TREE_OPERAND (rhs
, 0), 1)) == NULL_TREE
, 316);
6541 TREE_VALUE (TREE_OPERAND (TREE_OPERAND (rhs
, 0), 1))
6542 = build_unary_op (ADDR_EXPR
, exp
, 0);
6545 rhs
= build_cplus_new (type
, TREE_OPERAND (rhs
, 0), 0);
6548 else if (TYPE_HAS_TRIVIAL_INIT_REF (type
))
6551 if (TYPE_MAIN_VARIANT (type
) == TYPE_MAIN_VARIANT (rhstype
)
6552 || (IS_AGGR_TYPE (rhstype
) && UNIQUELY_DERIVED_FROM_P (type
, rhstype
)))
6554 if (TYPE_HAS_INIT_REF (type
))
6556 tree init
= build_method_call (exp
, constructor_name_full (type
),
6557 build_tree_list (NULL_TREE
, rhs
),
6558 TYPE_BINFO (type
), LOOKUP_NORMAL
);
6560 if (init
== error_mark_node
)
6561 return error_mark_node
;
6565 exp
= build_cplus_new (type
, init
, 0);
6569 return build (COMPOUND_EXPR
, type
, init
, exp
);
6572 /* ??? The following warnings are turned off because
6573 this is another place where the default X(X&) constructor
6575 if (TYPE_HAS_ASSIGNMENT (type
))
6576 cp_warning ("bitwise copy: `%T' defines operator=", type
);
6578 if (TREE_CODE (TREE_TYPE (rhs
)) == REFERENCE_TYPE
)
6579 rhs
= convert_from_reference (rhs
);
6580 if (type
!= rhstype
)
6582 tree nrhs
= build1 (NOP_EXPR
, type
, rhs
);
6583 TREE_CONSTANT (nrhs
) = TREE_CONSTANT (rhs
);
6589 return cp_convert (type
, rhs
, CONV_OLD_CONVERT
, flags
);
6592 if (type
== TREE_TYPE (rhs
))
6594 if (TREE_READONLY_DECL_P (rhs
))
6595 rhs
= decl_constant_value (rhs
);
6599 return convert_for_assignment (type
, rhs
, errtype
, fndecl
, parmnum
);
6602 /* Expand an ASM statement with operands, handling output operands
6603 that are not variables or INDIRECT_REFS by transforming such
6604 cases into cases that expand_asm_operands can handle.
6606 Arguments are same as for expand_asm_operands.
6608 We don't do default conversions on all inputs, because it can screw
6609 up operands that are expected to be in memory. */
6612 c_expand_asm_operands (string
, outputs
, inputs
, clobbers
, vol
, filename
, line
)
6613 tree string
, outputs
, inputs
, clobbers
;
6618 int noutputs
= list_length (outputs
);
6620 /* o[I] is the place that output number I should be written. */
6621 register tree
*o
= (tree
*) alloca (noutputs
* sizeof (tree
));
6624 /* Record the contents of OUTPUTS before it is modified. */
6625 for (i
= 0, tail
= outputs
; tail
; tail
= TREE_CHAIN (tail
), i
++)
6626 o
[i
] = TREE_VALUE (tail
);
6628 /* Generate the ASM_OPERANDS insn;
6629 store into the TREE_VALUEs of OUTPUTS some trees for
6630 where the values were actually stored. */
6631 expand_asm_operands (string
, outputs
, inputs
, clobbers
, vol
, filename
, line
);
6633 /* Copy all the intermediate outputs into the specified outputs. */
6634 for (i
= 0, tail
= outputs
; tail
; tail
= TREE_CHAIN (tail
), i
++)
6636 if (o
[i
] != TREE_VALUE (tail
))
6638 expand_expr (build_modify_expr (o
[i
], NOP_EXPR
, TREE_VALUE (tail
)),
6639 const0_rtx
, VOIDmode
, 0);
6642 /* Detect modification of read-only values.
6643 (Otherwise done by build_modify_expr.) */
6646 tree type
= TREE_TYPE (o
[i
]);
6647 if (TYPE_READONLY (type
)
6648 || ((TREE_CODE (type
) == RECORD_TYPE
6649 || TREE_CODE (type
) == UNION_TYPE
)
6650 && C_TYPE_FIELDS_READONLY (type
)))
6651 readonly_error (o
[i
], "modification by `asm'", 1);
6655 /* Those MODIFY_EXPRs could do autoincrements. */
6659 /* Expand a C `return' statement.
6660 RETVAL is the expression for what to return,
6661 or a null pointer for `return;' with no value.
6663 C++: upon seeing a `return', we must call destructors on all
6664 variables in scope which had constructors called on them.
6665 This means that if in a destructor, the base class destructors
6666 must be called before returning.
6668 The RETURN statement in C++ has initialization semantics. */
6671 c_expand_return (retval
)
6674 extern struct nesting
*cond_stack
, *loop_stack
, *case_stack
;
6675 extern tree dtor_label
, ctor_label
;
6676 tree result
= DECL_RESULT (current_function_decl
);
6677 tree valtype
= TREE_TYPE (result
);
6678 register int use_temp
= 0;
6679 int returns_value
= 1;
6681 if (TREE_THIS_VOLATILE (current_function_decl
))
6682 warning ("function declared `noreturn' has a `return' statement");
6684 if (retval
== error_mark_node
)
6686 current_function_returns_null
= 1;
6690 if (retval
== NULL_TREE
)
6692 /* A non-named return value does not count. */
6694 /* Can't just return from a destructor. */
6697 expand_goto (dtor_label
);
6701 if (DECL_CONSTRUCTOR_P (current_function_decl
))
6702 retval
= current_class_decl
;
6703 else if (DECL_NAME (result
) != NULL_TREE
6704 && TREE_CODE (valtype
) != VOID_TYPE
)
6708 current_function_returns_null
= 1;
6710 if (valtype
!= NULL_TREE
&& TREE_CODE (valtype
) != VOID_TYPE
)
6712 if (DECL_NAME (DECL_RESULT (current_function_decl
)) == NULL_TREE
)
6714 pedwarn ("`return' with no value, in function returning non-void");
6715 /* Clear this, so finish_function won't say that we
6716 reach the end of a non-void function (which we don't,
6717 we gave a return!). */
6718 current_function_returns_null
= 0;
6722 expand_null_return ();
6726 else if (DECL_CONSTRUCTOR_P (current_function_decl
)
6727 && retval
!= current_class_decl
)
6729 error ("return from a constructor: use `this = ...' instead");
6730 retval
= current_class_decl
;
6733 if (valtype
== NULL_TREE
|| TREE_CODE (valtype
) == VOID_TYPE
)
6735 current_function_returns_null
= 1;
6736 /* We do this here so we'll avoid a warning about how the function
6737 "may or may not return a value" in finish_function. */
6741 pedwarn ("`return' with a value, in function returning void");
6742 expand_return (retval
);
6744 /* Add some useful error checking for C++. */
6745 else if (TREE_CODE (valtype
) == REFERENCE_TYPE
)
6747 tree whats_returned
;
6748 tree tmp_result
= result
;
6750 /* Don't initialize directly into a non-BLKmode retval, since that
6751 could lose when being inlined by another caller. (GCC can't
6752 read the function return register in an inline function when
6753 the return value is being ignored). */
6754 if (result
&& TYPE_MODE (TREE_TYPE (tmp_result
)) != BLKmode
)
6757 /* convert to reference now, so we can give error if we
6758 return an reference to a non-lvalue. */
6759 retval
= convert_for_initialization (tmp_result
, valtype
, retval
,
6760 LOOKUP_NORMAL
, "return",
6763 /* Sort through common things to see what it is
6764 we are returning. */
6765 whats_returned
= retval
;
6766 if (TREE_CODE (whats_returned
) == COMPOUND_EXPR
)
6768 whats_returned
= TREE_OPERAND (whats_returned
, 1);
6769 if (TREE_CODE (whats_returned
) == ADDR_EXPR
)
6770 whats_returned
= TREE_OPERAND (whats_returned
, 0);
6772 if (TREE_CODE (whats_returned
) == ADDR_EXPR
)
6774 whats_returned
= TREE_OPERAND (whats_returned
, 0);
6775 while (TREE_CODE (whats_returned
) == NEW_EXPR
6776 || TREE_CODE (whats_returned
) == TARGET_EXPR
)
6778 /* Get the target. */
6779 whats_returned
= TREE_OPERAND (whats_returned
, 0);
6780 warning ("returning reference to temporary");
6784 if (TREE_CODE (whats_returned
) == VAR_DECL
&& DECL_NAME (whats_returned
))
6786 if (TEMP_NAME_P (DECL_NAME (whats_returned
)))
6787 warning ("reference to non-lvalue returned");
6788 else if (! TREE_STATIC (whats_returned
)
6789 && IDENTIFIER_LOCAL_VALUE (DECL_NAME (whats_returned
)))
6790 cp_warning_at ("reference to local variable `%D' returned", whats_returned
);
6793 else if (TREE_CODE (retval
) == ADDR_EXPR
)
6795 tree whats_returned
= TREE_OPERAND (retval
, 0);
6797 if (TREE_CODE (whats_returned
) == VAR_DECL
6798 && DECL_NAME (whats_returned
)
6799 && IDENTIFIER_LOCAL_VALUE (DECL_NAME (whats_returned
))
6800 && !TREE_STATIC (whats_returned
))
6801 cp_warning_at ("address of local variable `%D' returned", whats_returned
);
6804 /* Now deal with possible C++ hair:
6805 (1) Compute the return value.
6806 (2) If there are aggregate values with destructors which
6807 must be cleaned up, clean them (taking care
6808 not to clobber the return value).
6809 (3) If an X(X&) constructor is defined, the return
6810 value must be returned via that. */
6812 /* If we're returning in a register, we can't initialize the
6813 return value from a TARGET_EXPR. */
6814 if (TREE_CODE (retval
) == TARGET_EXPR
6815 && TYPE_MAIN_VARIANT (TREE_TYPE (retval
)) == TYPE_MAIN_VARIANT (valtype
)
6816 && ! current_function_returns_struct
)
6817 retval
= expand_target_expr (retval
);
6819 if (retval
== result
6820 /* Watch out for constructors, which "return" aggregates
6821 via initialization, but which otherwise "return" a pointer. */
6822 || DECL_CONSTRUCTOR_P (current_function_decl
))
6824 /* This is just an error--it's already been reported. */
6825 if (TYPE_SIZE (valtype
) == NULL_TREE
)
6828 if (TYPE_MODE (valtype
) != BLKmode
6829 && any_pending_cleanups (1))
6831 retval
= get_temp_regvar (valtype
, retval
);
6832 use_temp
= obey_regdecls
;
6835 else if (IS_AGGR_TYPE (valtype
) && current_function_returns_struct
)
6837 expand_aggr_init (result
, retval
, 0, LOOKUP_ONLYCONVERTING
);
6838 expand_cleanups_to (NULL_TREE
);
6839 DECL_INITIAL (result
) = NULL_TREE
;
6844 if (TYPE_MODE (valtype
) == VOIDmode
)
6846 if (TYPE_MODE (TREE_TYPE (result
)) != VOIDmode
6847 && warn_return_type
)
6848 warning ("return of void value in function returning non-void");
6849 expand_expr_stmt (retval
);
6853 else if (TYPE_MODE (valtype
) != BLKmode
6854 && any_pending_cleanups (1))
6856 retval
= get_temp_regvar (valtype
, retval
);
6857 expand_cleanups_to (NULL_TREE
);
6858 use_temp
= obey_regdecls
;
6863 retval
= convert_for_initialization (result
, valtype
, retval
,
6865 "return", NULL_TREE
, 0);
6866 DECL_INITIAL (result
) = NULL_TREE
;
6868 if (retval
== error_mark_node
)
6874 if (retval
!= NULL_TREE
6875 && TREE_CODE_CLASS (TREE_CODE (retval
)) == 'd'
6876 && cond_stack
== 0 && loop_stack
== 0 && case_stack
== 0)
6877 current_function_return_value
= retval
;
6881 /* Everything's great--RETVAL is in RESULT. */
6882 if (original_result_rtx
)
6884 store_expr (result
, original_result_rtx
, 0);
6885 expand_cleanups_to (NULL_TREE
);
6886 use_variable (DECL_RTL (result
));
6887 if (ctor_label
&& TREE_CODE (ctor_label
) != ERROR_MARK
)
6888 expand_goto (ctor_label
);
6890 expand_null_return ();
6892 else if (retval
&& retval
!= result
)
6894 /* Clear this out so the later call to decl_function_context
6895 won't end up bombing on us. */
6896 if (DECL_CONTEXT (result
) == error_mark_node
)
6897 DECL_CONTEXT (result
) = NULL_TREE
;
6898 /* Here is where we finally get RETVAL into RESULT.
6899 `expand_return' does the magic of protecting
6900 RESULT from cleanups. */
6901 retval
= fold (build1 (CLEANUP_POINT_EXPR
, TREE_TYPE (result
),
6903 /* This part _must_ come second, because expand_return looks for
6904 the INIT_EXPR as the toplevel node only. :-( */
6905 retval
= build (INIT_EXPR
, TREE_TYPE (result
), result
, retval
);
6906 TREE_SIDE_EFFECTS (retval
) = 1;
6907 expand_return (retval
);
6910 expand_return (result
);
6914 /* We may still need to put RETVAL into RESULT. */
6915 result
= DECL_RESULT (current_function_decl
);
6916 if (original_result_rtx
)
6918 /* Here we have a named return value that went
6919 into memory. We can compute RETVAL into that. */
6921 expand_assignment (result
, retval
, 0, 0);
6923 store_expr (result
, original_result_rtx
, 0);
6924 result
= make_tree (TREE_TYPE (result
), original_result_rtx
);
6926 else if (ctor_label
&& TREE_CODE (ctor_label
) != ERROR_MARK
)
6928 /* Here RETVAL is CURRENT_CLASS_DECL, so there's nothing to do. */
6929 expand_goto (ctor_label
);
6933 /* Here is where we finally get RETVAL into RESULT.
6934 `expand_return' does the magic of protecting
6935 RESULT from cleanups. */
6936 result
= build (INIT_EXPR
, TREE_TYPE (result
), result
, retval
);
6937 TREE_SIDE_EFFECTS (result
) = 1;
6938 expand_return (result
);
6940 else if (TYPE_MODE (TREE_TYPE (result
)) != VOIDmode
)
6941 expand_return (result
);
6944 current_function_returns_value
= returns_value
;
6946 /* One way to clear out cleanups that EXPR might
6947 generate. Note that this code will really be
6948 dead code, but that is ok--cleanups that were
6949 needed were handled by the magic of `return'. */
6950 expand_cleanups_to (NULL_TREE
);
6953 /* Start a C switch statement, testing expression EXP.
6954 Return EXP if it is valid, an error node otherwise. */
6957 c_expand_start_case (exp
)
6961 register enum tree_code code
;
6963 /* Convert from references, etc. */
6964 exp
= default_conversion (exp
);
6965 type
= TREE_TYPE (exp
);
6966 code
= TREE_CODE (type
);
6968 if (IS_AGGR_TYPE_CODE (code
))
6969 exp
= build_type_conversion (CONVERT_EXPR
, integer_type_node
, exp
, 1);
6971 if (exp
== NULL_TREE
)
6973 error ("switch quantity not an integer");
6974 exp
= error_mark_node
;
6976 type
= TREE_TYPE (exp
);
6977 code
= TREE_CODE (type
);
6979 if (code
!= INTEGER_TYPE
&& code
!= ENUMERAL_TYPE
&& code
!= ERROR_MARK
)
6981 error ("switch quantity not an integer");
6982 exp
= error_mark_node
;
6988 exp
= default_conversion (exp
);
6989 type
= TREE_TYPE (exp
);
6990 index
= get_unwidened (exp
, 0);
6991 /* We can't strip a conversion from a signed type to an unsigned,
6992 because if we did, int_fits_type_p would do the wrong thing
6993 when checking case values for being in range,
6994 and it's too hard to do the right thing. */
6995 if (TREE_UNSIGNED (TREE_TYPE (exp
))
6996 == TREE_UNSIGNED (TREE_TYPE (index
)))
7001 (1, fold (build1 (CLEANUP_POINT_EXPR
, TREE_TYPE (exp
), exp
)),
7002 type
, "switch statement");
7007 /* CONSTP remembers whether or not all the intervening pointers in the `to'
7008 type have been const. */
7010 comp_ptr_ttypes_real (to
, from
, constp
)
7014 for (; ; to
= TREE_TYPE (to
), from
= TREE_TYPE (from
))
7016 if (TREE_CODE (to
) != TREE_CODE (from
))
7019 /* Const and volatile mean something different for function types,
7020 so the usual checks are not appropriate. */
7021 if (TREE_CODE (to
) != FUNCTION_TYPE
&& TREE_CODE (to
) != METHOD_TYPE
)
7023 if (TYPE_READONLY (from
) > TYPE_READONLY (to
)
7024 || TYPE_VOLATILE (from
) > TYPE_VOLATILE (to
))
7028 && (TYPE_READONLY (to
) > TYPE_READONLY (from
)
7029 || TYPE_VOLATILE (to
) > TYPE_READONLY (from
)))
7031 constp
&= TYPE_READONLY (to
);
7034 if (TREE_CODE (to
) != POINTER_TYPE
)
7035 return comptypes (TYPE_MAIN_VARIANT (to
), TYPE_MAIN_VARIANT (from
), 1);
7039 /* When comparing, say, char ** to char const **, this function takes the
7040 'char *' and 'char const *'. Do not pass non-pointer types to this
7043 comp_ptr_ttypes (to
, from
)
7046 return comp_ptr_ttypes_real (to
, from
, 1);