1 /* Build expressions with type checking for C++ compiler.
2 Copyright (C) 1987, 88, 89, 92, 93, 94, 95, 1996 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
PROTO((tree
));
44 static tree convert_for_assignment
PROTO((tree
, tree
, char*, tree
, int));
45 /* static */ tree convert_for_initialization
PROTO((tree
, tree
, tree
, int, char*, tree
, int));
46 extern tree
shorten_compare ();
47 extern void binary_op_error ();
48 static tree pointer_int_sum
PROTO((enum tree_code
, register tree
, register tree
));
49 static tree pointer_diff
PROTO((register tree
, register tree
));
51 static tree
convert_sequence ();
53 /* static */ tree unary_complex_lvalue
PROTO((enum tree_code
, tree
));
54 static tree get_delta_difference
PROTO((tree
, tree
, int));
56 extern rtx original_result_rtx
;
57 extern int warn_synth
;
59 /* Return the target type of TYPE, which meas return T for:
60 T*, T&, T[], T (...), and otherwise, just T. */
66 if (TREE_CODE (type
) == REFERENCE_TYPE
)
67 type
= TREE_TYPE (type
);
68 while (TREE_CODE (type
) == POINTER_TYPE
69 || TREE_CODE (type
) == ARRAY_TYPE
70 || TREE_CODE (type
) == FUNCTION_TYPE
71 || TREE_CODE (type
) == METHOD_TYPE
72 || TREE_CODE (type
) == OFFSET_TYPE
)
73 type
= TREE_TYPE (type
);
77 /* Do `exp = require_complete_type (exp);' to make sure exp
78 does not have an incomplete type. (That includes void types.) */
81 require_complete_type (value
)
86 if (current_template_parms
)
89 type
= TREE_TYPE (value
);
91 /* First, detect a valid value with a complete type. */
92 if (TYPE_SIZE (type
) != 0
93 && type
!= void_type_node
94 && ! (TYPE_LANG_SPECIFIC (type
)
95 && (IS_SIGNATURE_POINTER (type
) || IS_SIGNATURE_REFERENCE (type
))
96 && TYPE_SIZE (SIGNATURE_TYPE (type
)) == 0))
99 /* If we see X::Y, we build an OFFSET_TYPE which has
100 not been laid out. Try to avoid an error by interpreting
101 it as this->X::Y, if reasonable. */
102 if (TREE_CODE (value
) == OFFSET_REF
104 && TREE_OPERAND (value
, 0) == C_C_D
)
106 tree base
, member
= TREE_OPERAND (value
, 1);
107 tree basetype
= TYPE_OFFSET_BASETYPE (type
);
108 my_friendly_assert (TREE_CODE (member
) == FIELD_DECL
, 305);
109 base
= convert_pointer_to (basetype
, current_class_decl
);
110 value
= build (COMPONENT_REF
, TREE_TYPE (member
),
111 build_indirect_ref (base
, NULL_PTR
), member
);
112 return require_complete_type (value
);
115 if (IS_AGGR_TYPE (type
) && CLASSTYPE_TEMPLATE_INSTANTIATION (type
))
117 instantiate_class_template (TYPE_MAIN_VARIANT (type
));
118 if (TYPE_SIZE (type
) != 0)
122 incomplete_type_error (value
, type
);
123 return error_mark_node
;
130 if (TYPE_SIZE (type
) != NULL_TREE
)
132 else if (TREE_CODE (type
) == ARRAY_TYPE
)
134 tree t
= complete_type (TREE_TYPE (type
));
135 if (TYPE_SIZE (t
) != NULL_TREE
)
136 type
= build_cplus_array_type (t
, TYPE_DOMAIN (type
));
138 else if (IS_AGGR_TYPE (type
) && CLASSTYPE_TEMPLATE_INSTANTIATION (type
))
139 instantiate_class_template (TYPE_MAIN_VARIANT (type
));
144 /* Return truthvalue of whether type of EXP is instantiated. */
149 return (TREE_CODE (exp
) == TREE_LIST
150 || TREE_TYPE (exp
) == unknown_type_node
151 || (TREE_CODE (TREE_TYPE (exp
)) == OFFSET_TYPE
152 && TREE_TYPE (TREE_TYPE (exp
)) == unknown_type_node
));
155 /* Return truthvalue of whether T is function (or pfn) type. */
160 return (TREE_CODE (t
) == FUNCTION_TYPE
|| TREE_CODE (t
) == METHOD_TYPE
161 || (TREE_CODE (t
) == POINTER_TYPE
162 && (TREE_CODE (TREE_TYPE (t
)) == FUNCTION_TYPE
163 || TREE_CODE (TREE_TYPE (t
)) == METHOD_TYPE
)));
166 /* Do `exp = require_instantiated_type (type, exp);' to make sure EXP
167 does not have an uninstantiated type.
168 TYPE is type to instantiate with, if uninstantiated. */
170 require_instantiated_type (type
, exp
, errval
)
171 tree type
, exp
, errval
;
173 if (TREE_TYPE (exp
) == NULL_TREE
)
175 error ("argument list may not have an initializer list");
179 if (TREE_TYPE (exp
) == unknown_type_node
180 || (TREE_CODE (TREE_TYPE (exp
)) == OFFSET_TYPE
181 && TREE_TYPE (TREE_TYPE (exp
)) == unknown_type_node
))
183 exp
= instantiate_type (type
, exp
, 1);
184 if (TREE_TYPE (exp
) == error_mark_node
)
190 /* Return a variant of TYPE which has all the type qualifiers of LIKE
191 as well as those of TYPE. */
194 qualify_type (type
, like
)
197 int constflag
= TYPE_READONLY (type
) || TYPE_READONLY (like
);
198 int volflag
= TYPE_VOLATILE (type
) || TYPE_VOLATILE (like
);
199 /* @@ Must do member pointers here. */
200 return cp_build_type_variant (type
, constflag
, volflag
);
203 /* Return the common type of two parameter lists.
204 We assume that comptypes has already been done and returned 1;
205 if that isn't so, this may crash.
207 As an optimization, free the space we allocate if the parameter
208 lists are already common. */
214 tree oldargs
= p1
, newargs
, n
;
217 char *first_obj
= (char *) oballoc (0);
219 len
= list_length (p1
);
220 newargs
= tree_last (p1
);
222 if (newargs
== void_list_node
)
231 newargs
= tree_cons (NULL_TREE
, NULL_TREE
, newargs
);
236 p1
= TREE_CHAIN (p1
), p2
= TREE_CHAIN (p2
), n
= TREE_CHAIN (n
), i
++)
238 if (TREE_PURPOSE (p1
) && !TREE_PURPOSE (p2
))
240 TREE_PURPOSE (n
) = TREE_PURPOSE (p1
);
243 else if (! TREE_PURPOSE (p1
))
245 if (TREE_PURPOSE (p2
))
247 TREE_PURPOSE (n
) = TREE_PURPOSE (p2
);
253 if (1 != simple_cst_equal (TREE_PURPOSE (p1
), TREE_PURPOSE (p2
)))
255 TREE_PURPOSE (n
) = TREE_PURPOSE (p2
);
257 if (TREE_VALUE (p1
) != TREE_VALUE (p2
))
260 TREE_VALUE (n
) = common_type (TREE_VALUE (p1
), TREE_VALUE (p2
));
263 TREE_VALUE (n
) = TREE_VALUE (p1
);
274 /* Return the common type of two types.
275 We assume that comptypes has already been done and returned 1;
276 if that isn't so, this may crash.
278 This is the type for the result of most arithmetic operations
279 if the operands have the given two types.
281 We do not deal with enumeral types here because they have already been
282 converted to integer types. */
288 register enum tree_code code1
;
289 register enum tree_code code2
;
292 /* Save time if the two types are the same. */
294 if (t1
== t2
) return t1
;
296 /* If one type is nonsense, use the other. */
297 if (t1
== error_mark_node
)
299 if (t2
== error_mark_node
)
302 /* Merge the attributes */
304 { register tree a1
, a2
;
305 a1
= TYPE_ATTRIBUTES (t1
);
306 a2
= TYPE_ATTRIBUTES (t2
);
308 /* Either one unset? Take the set one. */
310 if (!(attributes
= a1
))
313 /* One that completely contains the other? Take it. */
315 else if (a2
&& !attribute_list_contained (a1
, a2
))
316 if (attribute_list_contained (a2
, a1
))
320 /* Pick the longest list, and hang on the other list. */
321 /* ??? For the moment we punt on the issue of attrs with args. */
323 if (list_length (a1
) < list_length (a2
))
324 attributes
= a2
, a2
= a1
;
326 for (; a2
; a2
= TREE_CHAIN (a2
))
327 if (lookup_attribute (IDENTIFIER_POINTER (TREE_PURPOSE (a2
)),
328 attributes
) == NULL_TREE
)
331 TREE_CHAIN (a1
) = attributes
;
337 /* Treat an enum type as the unsigned integer type of the same width. */
339 if (TREE_CODE (t1
) == ENUMERAL_TYPE
)
340 t1
= type_for_size (TYPE_PRECISION (t1
), 1);
341 if (TREE_CODE (t2
) == ENUMERAL_TYPE
)
342 t2
= type_for_size (TYPE_PRECISION (t2
), 1);
344 if (TYPE_PTRMEMFUNC_P (t1
))
345 t1
= TYPE_PTRMEMFUNC_FN_TYPE (t1
);
346 if (TYPE_PTRMEMFUNC_P (t2
))
347 t2
= TYPE_PTRMEMFUNC_FN_TYPE (t2
);
349 code1
= TREE_CODE (t1
);
350 code2
= TREE_CODE (t2
);
356 /* If only one is real, use it as the result. */
358 if (code1
== REAL_TYPE
&& code2
!= REAL_TYPE
)
359 return build_type_attribute_variant (t1
, attributes
);
361 if (code2
== REAL_TYPE
&& code1
!= REAL_TYPE
)
362 return build_type_attribute_variant (t2
, attributes
);
364 /* Both real or both integers; use the one with greater precision. */
366 if (TYPE_PRECISION (t1
) > TYPE_PRECISION (t2
))
367 return build_type_attribute_variant (t1
, attributes
);
368 else if (TYPE_PRECISION (t2
) > TYPE_PRECISION (t1
))
369 return build_type_attribute_variant (t2
, attributes
);
371 /* Same precision. Prefer longs to ints even when same size. */
373 if (TYPE_MAIN_VARIANT (t1
) == long_unsigned_type_node
374 || TYPE_MAIN_VARIANT (t2
) == long_unsigned_type_node
)
375 return build_type_attribute_variant (long_unsigned_type_node
,
378 if (TYPE_MAIN_VARIANT (t1
) == long_integer_type_node
379 || TYPE_MAIN_VARIANT (t2
) == long_integer_type_node
)
381 /* But preserve unsignedness from the other type,
382 since long cannot hold all the values of an unsigned int. */
383 if (TREE_UNSIGNED (t1
) || TREE_UNSIGNED (t2
))
384 t1
= long_unsigned_type_node
;
386 t1
= long_integer_type_node
;
387 return build_type_attribute_variant (t1
, attributes
);
390 if (TYPE_MAIN_VARIANT (t1
) == long_double_type_node
391 || TYPE_MAIN_VARIANT (t2
) == long_double_type_node
)
392 return build_type_attribute_variant (long_double_type_node
,
395 /* Otherwise prefer the unsigned one. */
397 if (TREE_UNSIGNED (t1
))
398 return build_type_attribute_variant (t1
, attributes
);
400 return build_type_attribute_variant (t2
, attributes
);
404 /* For two pointers, do this recursively on the target type,
405 and combine the qualifiers of the two types' targets. */
406 /* This code was turned off; I don't know why.
407 But ANSI C++ specifies doing this with the qualifiers.
408 So I turned it on again. */
410 tree tt1
= TYPE_MAIN_VARIANT (TREE_TYPE (t1
));
411 tree tt2
= TYPE_MAIN_VARIANT (TREE_TYPE (t2
));
413 = TYPE_READONLY (TREE_TYPE (t1
)) || TYPE_READONLY (TREE_TYPE (t2
));
415 = TYPE_VOLATILE (TREE_TYPE (t1
)) || TYPE_VOLATILE (TREE_TYPE (t2
));
420 else if (tt1
== void_type_node
|| tt2
== void_type_node
)
421 target
= void_type_node
;
423 target
= common_type (tt1
, tt2
);
425 target
= cp_build_type_variant (target
, constp
, volatilep
);
426 if (code1
== POINTER_TYPE
)
427 t1
= build_pointer_type (target
);
429 t1
= build_reference_type (target
);
430 t1
= build_type_attribute_variant (t1
, attributes
);
432 if (TREE_CODE (target
) == METHOD_TYPE
)
433 t1
= build_ptrmemfunc_type (t1
);
440 tree elt
= common_type (TREE_TYPE (t1
), TREE_TYPE (t2
));
441 /* Save space: see if the result is identical to one of the args. */
442 if (elt
== TREE_TYPE (t1
) && TYPE_DOMAIN (t1
))
443 return build_type_attribute_variant (t1
, attributes
);
444 if (elt
== TREE_TYPE (t2
) && TYPE_DOMAIN (t2
))
445 return build_type_attribute_variant (t2
, attributes
);
446 /* Merge the element types, and have a size if either arg has one. */
447 t1
= build_cplus_array_type (elt
, TYPE_DOMAIN (TYPE_DOMAIN (t1
) ? t1
: t2
));
448 return build_type_attribute_variant (t1
, attributes
);
452 /* Function types: prefer the one that specified arg types.
453 If both do, merge the arg types. Also merge the return types. */
455 tree valtype
= common_type (TREE_TYPE (t1
), TREE_TYPE (t2
));
456 tree p1
= TYPE_ARG_TYPES (t1
);
457 tree p2
= TYPE_ARG_TYPES (t2
);
460 /* Save space: see if the result is identical to one of the args. */
461 if (valtype
== TREE_TYPE (t1
) && ! p2
)
462 return build_type_attribute_variant (t1
, attributes
);
463 if (valtype
== TREE_TYPE (t2
) && ! p1
)
464 return build_type_attribute_variant (t2
, attributes
);
466 /* Simple way if one arg fails to specify argument types. */
467 if (p1
== NULL_TREE
|| TREE_VALUE (p1
) == void_type_node
)
469 rval
= build_function_type (valtype
, p2
);
470 if ((raises
= TYPE_RAISES_EXCEPTIONS (t2
)))
471 rval
= build_exception_variant (rval
, raises
);
472 return build_type_attribute_variant (rval
, attributes
);
474 raises
= TYPE_RAISES_EXCEPTIONS (t1
);
475 if (p2
== NULL_TREE
|| TREE_VALUE (p2
) == void_type_node
)
477 rval
= build_function_type (valtype
, p1
);
479 rval
= build_exception_variant (rval
, raises
);
480 return build_type_attribute_variant (rval
, attributes
);
483 rval
= build_function_type (valtype
, commonparms (p1
, p2
));
484 rval
= build_exception_variant (rval
, raises
);
485 return build_type_attribute_variant (rval
, attributes
);
490 my_friendly_assert (TYPE_MAIN_VARIANT (t1
) == t1
491 && TYPE_MAIN_VARIANT (t2
) == t2
, 306);
493 if (DERIVED_FROM_P (t1
, t2
) && binfo_or_else (t1
, t2
))
494 return build_type_attribute_variant (t1
, attributes
);
495 else if (binfo_or_else (t2
, t1
))
496 return build_type_attribute_variant (t2
, attributes
);
498 compiler_error ("common_type called with uncommon aggregate types");
501 if (TREE_CODE (TREE_TYPE (t1
)) == TREE_CODE (TREE_TYPE (t2
)))
503 /* Get this value the long way, since TYPE_METHOD_BASETYPE
504 is just the main variant of this. */
508 tree b1
= TYPE_OFFSET_BASETYPE (t1
);
509 tree b2
= TYPE_OFFSET_BASETYPE (t2
);
511 if (comptypes (b1
, b2
, 1)
512 || (DERIVED_FROM_P (b1
, b2
) && binfo_or_else (b1
, b2
)))
513 basetype
= TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (t2
)));
516 if (binfo_or_else (b2
, b1
) == NULL_TREE
)
517 compiler_error ("common_type called with uncommon method types");
518 basetype
= TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (t1
)));
521 raises
= TYPE_RAISES_EXCEPTIONS (t1
);
523 /* If this was a member function type, get back to the
524 original type of type member function (i.e., without
525 the class instance variable up front. */
526 t1
= build_function_type (TREE_TYPE (t1
), TREE_CHAIN (TYPE_ARG_TYPES (t1
)));
527 t2
= build_function_type (TREE_TYPE (t2
), TREE_CHAIN (TYPE_ARG_TYPES (t2
)));
528 t3
= common_type (t1
, t2
);
529 t3
= build_cplus_method_type (basetype
, TREE_TYPE (t3
), TYPE_ARG_TYPES (t3
));
530 t1
= build_exception_variant (t3
, raises
);
533 compiler_error ("common_type called with uncommon method types");
535 return build_type_attribute_variant (t1
, attributes
);
538 if (TREE_TYPE (t1
) == TREE_TYPE (t2
))
540 tree b1
= TYPE_OFFSET_BASETYPE (t1
);
541 tree b2
= TYPE_OFFSET_BASETYPE (t2
);
543 if (DERIVED_FROM_P (b1
, b2
) && binfo_or_else (b1
, b2
))
544 return build_type_attribute_variant (t2
, attributes
);
545 else if (binfo_or_else (b2
, b1
))
546 return build_type_attribute_variant (t1
, attributes
);
548 compiler_error ("common_type called with uncommon member types");
551 return build_type_attribute_variant (t1
, attributes
);
555 /* Return 1 if TYPE1 and TYPE2 raise the same exceptions. */
557 compexcepttypes (t1
, t2
)
560 return TYPE_RAISES_EXCEPTIONS (t1
) == TYPE_RAISES_EXCEPTIONS (t2
);
564 comp_array_types (cmp
, t1
, t2
, strict
)
565 register int (*cmp
)();
569 tree d1
= TYPE_DOMAIN (t1
);
570 tree d2
= TYPE_DOMAIN (t2
);
572 /* Target types must match incl. qualifiers. */
573 if (!(TREE_TYPE (t1
) == TREE_TYPE (t2
)
574 || (*cmp
) (TREE_TYPE (t1
), TREE_TYPE (t2
), strict
)))
577 /* Sizes must match unless one is missing or variable. */
578 if (d1
== 0 || d2
== 0 || d1
== d2
579 || TREE_CODE (TYPE_MIN_VALUE (d1
)) != INTEGER_CST
580 || TREE_CODE (TYPE_MIN_VALUE (d2
)) != INTEGER_CST
581 || TREE_CODE (TYPE_MAX_VALUE (d1
)) != INTEGER_CST
582 || TREE_CODE (TYPE_MAX_VALUE (d2
)) != INTEGER_CST
)
585 return ((TREE_INT_CST_LOW (TYPE_MIN_VALUE (d1
))
586 == TREE_INT_CST_LOW (TYPE_MIN_VALUE (d2
)))
587 && (TREE_INT_CST_HIGH (TYPE_MIN_VALUE (d1
))
588 == TREE_INT_CST_HIGH (TYPE_MIN_VALUE (d2
)))
589 && (TREE_INT_CST_LOW (TYPE_MAX_VALUE (d1
))
590 == TREE_INT_CST_LOW (TYPE_MAX_VALUE (d2
)))
591 && (TREE_INT_CST_HIGH (TYPE_MAX_VALUE (d1
))
592 == TREE_INT_CST_HIGH (TYPE_MAX_VALUE (d2
))));
595 /* Return 1 if TYPE1 and TYPE2 are compatible types for assignment
596 or various other operations. This is what ANSI C++ speaks of as
599 For C++: argument STRICT says we should be strict about this
602 2 : strict, except that if one type is a reference and
603 the other is not, compare the target type of the
604 reference to the type that's not a reference (ARM, p308).
605 This is used for checking for invalid overloading.
606 1 : strict (compared according to ANSI C)
607 This is used for checking whether two function decls match.
608 0 : <= (compared according to C++)
609 -1: <= or >= (relaxed)
611 Otherwise, pointers involving base classes and derived classes
612 can be mixed as valid: i.e. a pointer to a base class may be assigned
613 to a pointer to one of its derived classes, as per C++. A pointer to
614 a derived class may be passed as a parameter to a function expecting a
615 pointer to a base classes. These allowances do not commute. In this
616 case, TYPE1 is assumed to be the base class, and TYPE2 is assumed to
617 be the derived class. */
619 comptypes (type1
, type2
, strict
)
623 register tree t1
= type1
;
624 register tree t2
= type2
;
627 /* Suppress errors caused by previously reported errors */
632 /* This should never happen. */
633 my_friendly_assert (t1
!= error_mark_node
, 307);
635 if (t2
== error_mark_node
)
640 /* Treat an enum type as the unsigned integer type of the same width. */
642 if (TREE_CODE (t1
) == ENUMERAL_TYPE
)
643 t1
= type_for_size (TYPE_PRECISION (t1
), 1);
644 if (TREE_CODE (t2
) == ENUMERAL_TYPE
)
645 t2
= type_for_size (TYPE_PRECISION (t2
), 1);
651 if (TYPE_PTRMEMFUNC_P (t1
))
652 t1
= TYPE_PTRMEMFUNC_FN_TYPE (t1
);
653 if (TYPE_PTRMEMFUNC_P (t2
))
654 t2
= TYPE_PTRMEMFUNC_FN_TYPE (t2
);
656 /* Different classes of types can't be compatible. */
658 if (TREE_CODE (t1
) != TREE_CODE (t2
))
661 && ((TREE_CODE (t1
) == REFERENCE_TYPE
)
662 ^ (TREE_CODE (t2
) == REFERENCE_TYPE
)))
664 if (TREE_CODE (t1
) == REFERENCE_TYPE
)
665 return comptypes (TREE_TYPE (t1
), t2
, 1);
666 return comptypes (t1
, TREE_TYPE (t2
), 1);
674 /* Qualifiers must match. */
676 if (TYPE_READONLY (t1
) != TYPE_READONLY (t2
))
678 if (TYPE_VOLATILE (t1
) != TYPE_VOLATILE (t2
))
681 /* Allow for two different type nodes which have essentially the same
682 definition. Note that we already checked for equality of the type
683 type qualifiers (just above). */
685 if (TYPE_MAIN_VARIANT (t1
) == TYPE_MAIN_VARIANT (t2
))
688 /* ??? COMP_TYPE_ATTRIBUTES is currently useless for variables as each
689 attribute is its own main variant (`val' will remain 0). */
690 #ifndef COMP_TYPE_ATTRIBUTES
691 #define COMP_TYPE_ATTRIBUTES(t1,t2) 1
694 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
695 if (! (attrval
= COMP_TYPE_ATTRIBUTES (t1
, t2
)))
698 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
701 switch (TREE_CODE (t1
))
705 if (CLASSTYPE_TEMPLATE_INFO (t1
) && CLASSTYPE_TEMPLATE_INFO (t2
)
706 && CLASSTYPE_TI_TEMPLATE (t1
) == CLASSTYPE_TI_TEMPLATE (t2
))
708 int i
= TREE_VEC_LENGTH (CLASSTYPE_TI_ARGS (t1
));
709 tree
*p1
= &TREE_VEC_ELT (CLASSTYPE_TI_ARGS (t1
), 0);
710 tree
*p2
= &TREE_VEC_ELT (CLASSTYPE_TI_ARGS (t2
), 0);
714 if (TREE_CODE_CLASS (TREE_CODE (p1
[i
])) == 't')
716 if (! comptypes (p1
[i
], p2
[i
], 1))
721 if (simple_cst_equal (p1
[i
], p2
[i
]) <= 0)
732 val
= (comptypes (build_pointer_type (TYPE_OFFSET_BASETYPE (t1
)),
733 build_pointer_type (TYPE_OFFSET_BASETYPE (t2
)), strict
)
734 && comptypes (TREE_TYPE (t1
), TREE_TYPE (t2
), strict
));
738 if (! compexcepttypes (t1
, t2
))
741 /* This case is anti-symmetrical!
742 One can pass a base member (or member function)
743 to something expecting a derived member (or member function),
744 but not vice-versa! */
746 val
= (comptypes (TREE_TYPE (t1
), TREE_TYPE (t2
), strict
)
747 && compparms (TYPE_ARG_TYPES (t1
),
748 TYPE_ARG_TYPES (t2
), strict
));
762 if (TREE_CODE (t1
) == RECORD_TYPE
&& TREE_CODE (t2
) == RECORD_TYPE
)
766 rval
= t1
== t2
|| UNIQUELY_DERIVED_FROM_P (t1
, t2
);
775 val
= UNIQUELY_DERIVED_FROM_P (t2
, t1
);
782 val
= comptypes (t1
, t2
, strict
);
786 if (! compexcepttypes (t1
, t2
))
789 val
= ((TREE_TYPE (t1
) == TREE_TYPE (t2
)
790 || comptypes (TREE_TYPE (t1
), TREE_TYPE (t2
), strict
))
791 && compparms (TYPE_ARG_TYPES (t1
), TYPE_ARG_TYPES (t2
), strict
));
795 /* Target types must match incl. qualifiers. */
796 val
= comp_array_types (comptypes
, t1
, t2
, strict
);
799 case TEMPLATE_TYPE_PARM
:
800 return TEMPLATE_TYPE_IDX (t1
) == TEMPLATE_TYPE_IDX (t2
);
803 if (TYPE_IDENTIFIER (t1
) != TYPE_IDENTIFIER (t2
))
805 return comptypes (TYPE_CONTEXT (t1
), TYPE_CONTEXT (t2
), 1);
807 return attrval
== 2 && val
== 1 ? 2 : val
;
810 /* Return 1 if TTL and TTR are pointers to types that are equivalent,
811 ignoring their qualifiers.
813 NPTRS is the number of pointers we can strip off and keep cool.
814 This is used to permit (for aggr A, aggr B) A, B* to convert to A*,
815 but to not permit B** to convert to A**. */
818 comp_target_types (ttl
, ttr
, nptrs
)
822 ttl
= TYPE_MAIN_VARIANT (ttl
);
823 ttr
= TYPE_MAIN_VARIANT (ttr
);
827 if (TREE_CODE (ttr
) != TREE_CODE (ttl
))
830 if (TREE_CODE (ttr
) == POINTER_TYPE
)
832 ttl
= TREE_TYPE (ttl
);
833 ttr
= TREE_TYPE (ttr
);
837 if (TREE_CODE (ttl
) == VOID_TYPE
838 && TREE_CODE (ttr
) != FUNCTION_TYPE
839 && TREE_CODE (ttr
) != METHOD_TYPE
840 && TREE_CODE (ttr
) != OFFSET_TYPE
)
842 else if (TREE_CODE (ttr
) == VOID_TYPE
843 && TREE_CODE (ttl
) != FUNCTION_TYPE
844 && TREE_CODE (ttl
) != METHOD_TYPE
845 && TREE_CODE (ttl
) != OFFSET_TYPE
)
847 else if (TREE_CODE (ttl
) == POINTER_TYPE
848 || TREE_CODE (ttl
) == ARRAY_TYPE
)
850 if (comp_ptr_ttypes (ttl
, ttr
))
852 else if (comp_ptr_ttypes (ttr
, ttl
))
858 /* Const and volatile mean something different for function types,
859 so the usual checks are not appropriate. */
860 if (TREE_CODE (ttl
) == FUNCTION_TYPE
|| TREE_CODE (ttl
) == METHOD_TYPE
)
861 return comp_target_types (ttl
, ttr
, nptrs
- 1);
863 /* Make sure that the cv-quals change only in the same direction as
867 int c
= TYPE_READONLY (ttl
) - TYPE_READONLY (ttr
);
868 int v
= TYPE_VOLATILE (ttl
) - TYPE_VOLATILE (ttr
);
870 if ((c
> 0 && v
< 0) || (c
< 0 && v
> 0))
873 if (TYPE_MAIN_VARIANT (ttl
) == TYPE_MAIN_VARIANT (ttr
))
874 return (c
+ v
< 0) ? -1 : 1;
876 t
= comp_target_types (ttl
, ttr
, nptrs
- 1);
877 if ((t
== 1 && c
+ v
>= 0) || (t
== -1 && c
+ v
<= 0))
884 if (TREE_CODE (ttr
) == REFERENCE_TYPE
)
885 return comp_target_types (TREE_TYPE (ttl
), TREE_TYPE (ttr
), nptrs
);
886 if (TREE_CODE (ttr
) == ARRAY_TYPE
)
887 return comp_array_types (comp_target_types
, ttl
, ttr
, 0);
888 else if (TREE_CODE (ttr
) == FUNCTION_TYPE
|| TREE_CODE (ttr
) == METHOD_TYPE
)
889 if (comp_target_types (TREE_TYPE (ttl
), TREE_TYPE (ttr
), nptrs
))
890 switch (comp_target_parms (TYPE_ARG_TYPES (ttl
), TYPE_ARG_TYPES (ttr
), 1))
899 my_friendly_abort (112);
905 else if (TREE_CODE (ttr
) == OFFSET_TYPE
)
907 /* Contravariance: we can assign a pointer to base member to a pointer
908 to derived member. Note difference from simple pointer case, where
909 we can pass a pointer to derived to a pointer to base. */
910 if (comptypes (TYPE_OFFSET_BASETYPE (ttr
), TYPE_OFFSET_BASETYPE (ttl
), 0))
911 return comp_target_types (TREE_TYPE (ttl
), TREE_TYPE (ttr
), nptrs
);
912 else if (comptypes (TYPE_OFFSET_BASETYPE (ttl
), TYPE_OFFSET_BASETYPE (ttr
), 0)
913 && comp_target_types (TREE_TYPE (ttl
), TREE_TYPE (ttr
), nptrs
))
916 else if (IS_AGGR_TYPE (ttl
))
920 if (comptypes (build_pointer_type (ttl
), build_pointer_type (ttr
), 0))
922 if (comptypes (build_pointer_type (ttr
), build_pointer_type (ttl
), 0))
930 /* If two types share a common base type, return that basetype.
931 If there is not a unique most-derived base type, this function
932 returns ERROR_MARK_NODE. */
934 common_base_type (tt1
, tt2
)
937 tree best
= NULL_TREE
;
940 /* If one is a baseclass of another, that's good enough. */
941 if (UNIQUELY_DERIVED_FROM_P (tt1
, tt2
))
943 if (UNIQUELY_DERIVED_FROM_P (tt2
, tt1
))
946 /* Otherwise, try to find a unique baseclass of TT1
947 that is shared by TT2, and follow that down. */
948 for (i
= CLASSTYPE_N_BASECLASSES (tt1
)-1; i
>= 0; i
--)
950 tree basetype
= TYPE_BINFO_BASETYPE (tt1
, i
);
951 tree trial
= common_base_type (basetype
, tt2
);
954 if (trial
== error_mark_node
)
956 if (best
== NULL_TREE
)
958 else if (best
!= trial
)
959 return error_mark_node
;
964 for (i
= CLASSTYPE_N_BASECLASSES (tt2
)-1; i
>= 0; i
--)
966 tree basetype
= TYPE_BINFO_BASETYPE (tt2
, i
);
967 tree trial
= common_base_type (tt1
, basetype
);
970 if (trial
== error_mark_node
)
972 if (best
== NULL_TREE
)
974 else if (best
!= trial
)
975 return error_mark_node
;
981 /* Subroutines of `comptypes'. */
983 /* Return 1 if two parameter type lists PARMS1 and PARMS2
984 are equivalent in the sense that functions with those parameter types
985 can have equivalent types.
986 If either list is empty, we win.
987 Otherwise, the two lists must be equivalent, element by element.
989 C++: See comment above about TYPE1, TYPE2, STRICT.
990 If STRICT == 3, it means checking is strict, but do not compare
991 default parameter values. */
993 compparms (parms1
, parms2
, strict
)
997 register tree t1
= parms1
, t2
= parms2
;
999 /* An unspecified parmlist matches any specified parmlist
1000 whose argument types don't need default promotions. */
1002 if (strict
<= 0 && t1
== 0)
1003 return self_promoting_args_p (t2
);
1004 if (strict
< 0 && t2
== 0)
1005 return self_promoting_args_p (t1
);
1009 if (t1
== 0 && t2
== 0)
1011 /* If one parmlist is shorter than the other,
1012 they fail to match, unless STRICT is <= 0. */
1013 if (t1
== 0 || t2
== 0)
1020 return t1
&& TREE_PURPOSE (t1
);
1022 if (! comptypes (TREE_VALUE (t2
), TREE_VALUE (t1
), strict
))
1027 return t2
== void_list_node
&& TREE_PURPOSE (t1
);
1028 return TREE_PURPOSE (t1
) || TREE_PURPOSE (t2
);
1031 t1
= TREE_CHAIN (t1
);
1032 t2
= TREE_CHAIN (t2
);
1036 /* This really wants return whether or not parameter type lists
1037 would make their owning functions assignment compatible or not. */
1039 comp_target_parms (parms1
, parms2
, strict
)
1040 tree parms1
, parms2
;
1043 register tree t1
= parms1
, t2
= parms2
;
1044 int warn_contravariance
= 0;
1046 /* An unspecified parmlist matches any specified parmlist
1047 whose argument types don't need default promotions.
1048 @@@ see 13.3.3 for a counterexample... */
1050 if (t1
== 0 && t2
!= 0)
1052 cp_pedwarn ("ANSI C++ prohibits conversion from `(%#T)' to `(...)'",
1054 return self_promoting_args_p (t2
);
1057 return self_promoting_args_p (t1
);
1059 for (; t1
|| t2
; t1
= TREE_CHAIN (t1
), t2
= TREE_CHAIN (t2
))
1063 /* If one parmlist is shorter than the other,
1064 they fail to match, unless STRICT is <= 0. */
1065 if (t1
== 0 || t2
== 0)
1070 return 1 + warn_contravariance
;
1071 return ((t1
&& TREE_PURPOSE (t1
)) + warn_contravariance
);
1073 p1
= TREE_VALUE (t1
);
1074 p2
= TREE_VALUE (t2
);
1078 if ((TREE_CODE (p1
) == POINTER_TYPE
&& TREE_CODE (p2
) == POINTER_TYPE
)
1079 || (TREE_CODE (p1
) == REFERENCE_TYPE
&& TREE_CODE (p2
) == REFERENCE_TYPE
))
1082 && (TYPE_MAIN_VARIANT (TREE_TYPE (p1
))
1083 == TYPE_MAIN_VARIANT (TREE_TYPE (p2
))))
1086 /* The following is wrong for contravariance,
1087 but many programs depend on it. */
1088 if (TREE_TYPE (p1
) == void_type_node
)
1090 if (TREE_TYPE (p2
) == void_type_node
)
1092 warn_contravariance
= 1;
1095 if (IS_AGGR_TYPE (TREE_TYPE (p1
)))
1097 if (comptypes (p2
, p1
, 0) == 0)
1099 if (comptypes (p1
, p2
, 0) != 0)
1100 warn_contravariance
= 1;
1107 /* Note backwards order due to contravariance. */
1108 if (comp_target_types (p2
, p1
, 1) == 0)
1110 if (comp_target_types (p1
, p2
, 1))
1112 warn_contravariance
= 1;
1118 /* Target types are compatible--just make sure that if
1119 we use parameter lists, that they are ok as well. */
1120 if (TREE_CODE (p1
) == FUNCTION_TYPE
|| TREE_CODE (p1
) == METHOD_TYPE
)
1121 switch (comp_target_parms (TYPE_ARG_TYPES (p1
),
1122 TYPE_ARG_TYPES (p2
),
1130 warn_contravariance
= 1;
1133 if (TREE_PURPOSE (t1
) && TREE_PURPOSE (t2
))
1135 int cmp
= simple_cst_equal (TREE_PURPOSE (t1
), TREE_PURPOSE (t2
));
1137 my_friendly_abort (114);
1142 return 1 + warn_contravariance
;
1145 /* Return 1 if PARMS specifies a fixed number of parameters
1146 and none of their types is affected by default promotions. */
1149 self_promoting_args_p (parms
)
1153 for (t
= parms
; t
; t
= TREE_CHAIN (t
))
1155 register tree type
= TREE_VALUE (t
);
1157 if (TREE_CHAIN (t
) == 0 && type
!= void_type_node
)
1163 if (TYPE_MAIN_VARIANT (type
) == float_type_node
)
1166 if (C_PROMOTING_INTEGER_TYPE_P (type
))
1172 /* Return an unsigned type the same as TYPE in other respects.
1174 C++: must make these work for type variants as well. */
1177 unsigned_type (type
)
1180 tree type1
= TYPE_MAIN_VARIANT (type
);
1181 if (type1
== signed_char_type_node
|| type1
== char_type_node
)
1182 return unsigned_char_type_node
;
1183 if (type1
== integer_type_node
)
1184 return unsigned_type_node
;
1185 if (type1
== short_integer_type_node
)
1186 return short_unsigned_type_node
;
1187 if (type1
== long_integer_type_node
)
1188 return long_unsigned_type_node
;
1189 if (type1
== long_long_integer_type_node
)
1190 return long_long_unsigned_type_node
;
1191 if (type1
== intDI_type_node
)
1192 return unsigned_intDI_type_node
;
1193 if (type1
== intSI_type_node
)
1194 return unsigned_intSI_type_node
;
1195 if (type1
== intHI_type_node
)
1196 return unsigned_intHI_type_node
;
1197 if (type1
== intQI_type_node
)
1198 return unsigned_intQI_type_node
;
1202 /* Return a signed type the same as TYPE in other respects. */
1208 tree type1
= TYPE_MAIN_VARIANT (type
);
1209 if (type1
== unsigned_char_type_node
|| type1
== char_type_node
)
1210 return signed_char_type_node
;
1211 if (type1
== unsigned_type_node
)
1212 return integer_type_node
;
1213 if (type1
== short_unsigned_type_node
)
1214 return short_integer_type_node
;
1215 if (type1
== long_unsigned_type_node
)
1216 return long_integer_type_node
;
1217 if (type1
== long_long_unsigned_type_node
)
1218 return long_long_integer_type_node
;
1219 if (type1
== unsigned_intDI_type_node
)
1220 return intDI_type_node
;
1221 if (type1
== unsigned_intSI_type_node
)
1222 return intSI_type_node
;
1223 if (type1
== unsigned_intHI_type_node
)
1224 return intHI_type_node
;
1225 if (type1
== unsigned_intQI_type_node
)
1226 return intQI_type_node
;
1230 /* Return a type the same as TYPE except unsigned or
1231 signed according to UNSIGNEDP. */
1234 signed_or_unsigned_type (unsignedp
, type
)
1238 if (! INTEGRAL_TYPE_P (type
))
1240 if (TYPE_PRECISION (type
) == TYPE_PRECISION (signed_char_type_node
))
1241 return unsignedp
? unsigned_char_type_node
: signed_char_type_node
;
1242 if (TYPE_PRECISION (type
) == TYPE_PRECISION (integer_type_node
))
1243 return unsignedp
? unsigned_type_node
: integer_type_node
;
1244 if (TYPE_PRECISION (type
) == TYPE_PRECISION (short_integer_type_node
))
1245 return unsignedp
? short_unsigned_type_node
: short_integer_type_node
;
1246 if (TYPE_PRECISION (type
) == TYPE_PRECISION (long_integer_type_node
))
1247 return unsignedp
? long_unsigned_type_node
: long_integer_type_node
;
1248 if (TYPE_PRECISION (type
) == TYPE_PRECISION (long_long_integer_type_node
))
1249 return (unsignedp
? long_long_unsigned_type_node
1250 : long_long_integer_type_node
);
1254 /* Compute the value of the `sizeof' operator. */
1260 enum tree_code code
= TREE_CODE (type
);
1263 if (current_template_parms
)
1264 return build_min (SIZEOF_EXPR
, sizetype
, type
);
1266 if (code
== FUNCTION_TYPE
)
1268 if (pedantic
|| warn_pointer_arith
)
1269 pedwarn ("ANSI C++ forbids taking the sizeof a function type");
1270 return size_int (1);
1272 if (code
== METHOD_TYPE
)
1274 if (pedantic
|| warn_pointer_arith
)
1275 pedwarn ("ANSI C++ forbids taking the sizeof a method type");
1276 return size_int (1);
1278 if (code
== VOID_TYPE
)
1280 if (pedantic
|| warn_pointer_arith
)
1281 pedwarn ("ANSI C++ forbids taking the sizeof a void type");
1282 return size_int (1);
1284 if (code
== ERROR_MARK
)
1285 return size_int (1);
1287 /* ARM $5.3.2: ``When applied to a reference, the result is the size of the
1288 referenced object.'' */
1289 if (code
== REFERENCE_TYPE
)
1290 type
= TREE_TYPE (type
);
1292 /* We couldn't find anything in the ARM or the draft standard that says,
1293 one way or the other, if doing sizeof on something that doesn't have
1294 an object associated with it is correct or incorrect. For example, if
1295 you declare `struct S { char str[16]; };', and in your program do
1296 a `sizeof (S::str)', should we flag that as an error or should we give
1297 the size of it? Since it seems like a reasonable thing to do, we'll go
1298 with giving the value. */
1299 if (code
== OFFSET_TYPE
)
1300 type
= TREE_TYPE (type
);
1302 /* @@ This also produces an error for a signature ref.
1303 In that case we should be able to do better. */
1304 if (IS_SIGNATURE (type
))
1306 error ("`sizeof' applied to a signature type");
1307 return size_int (0);
1310 if (TYPE_SIZE (complete_type (type
)) == 0)
1312 cp_error ("`sizeof' applied to incomplete type `%T'", type
);
1313 return size_int (0);
1316 /* Convert in case a char is more than one unit. */
1317 t
= size_binop (CEIL_DIV_EXPR
, TYPE_SIZE (type
),
1318 size_int (TYPE_PRECISION (char_type_node
)));
1319 /* size_binop does not put the constant in range, so do it now. */
1320 if (TREE_CODE (t
) == INTEGER_CST
&& force_fit_type (t
, 0))
1321 TREE_CONSTANT_OVERFLOW (t
) = TREE_OVERFLOW (t
) = 1;
1329 if (current_template_parms
)
1330 return build_min (SIZEOF_EXPR
, sizetype
, e
);
1332 if (TREE_CODE (e
) == COMPONENT_REF
1333 && DECL_BIT_FIELD (TREE_OPERAND (e
, 1)))
1334 error ("sizeof applied to a bit-field");
1335 /* ANSI says arrays and functions are converted inside comma.
1336 But we can't really convert them in build_compound_expr
1337 because that would break commas in lvalues.
1338 So do the conversion here if operand was a comma. */
1339 if (TREE_CODE (e
) == COMPOUND_EXPR
1340 && (TREE_CODE (TREE_TYPE (e
)) == ARRAY_TYPE
1341 || TREE_CODE (TREE_TYPE (e
)) == FUNCTION_TYPE
))
1342 e
= default_conversion (e
);
1343 else if (TREE_CODE (e
) == TREE_LIST
)
1345 tree t
= TREE_VALUE (e
);
1348 && TREE_CODE (TREE_TYPE (t
)) == FUNCTION_TYPE
)
1349 || is_overloaded_fn (t
)))
1350 pedwarn ("ANSI C++ forbids taking the sizeof a function type");
1352 return c_sizeof (TREE_TYPE (e
));
1356 c_sizeof_nowarn (type
)
1359 enum tree_code code
= TREE_CODE (type
);
1362 if (code
== FUNCTION_TYPE
1363 || code
== METHOD_TYPE
1364 || code
== VOID_TYPE
1365 || code
== ERROR_MARK
)
1366 return size_int (1);
1367 if (code
== REFERENCE_TYPE
)
1368 type
= TREE_TYPE (type
);
1370 if (TYPE_SIZE (type
) == 0)
1371 return size_int (0);
1373 /* Convert in case a char is more than one unit. */
1374 t
= size_binop (CEIL_DIV_EXPR
, TYPE_SIZE (type
),
1375 size_int (TYPE_PRECISION (char_type_node
)));
1376 force_fit_type (t
, 0);
1380 /* Implement the __alignof keyword: Return the minimum required
1381 alignment of TYPE, measured in bytes. */
1387 enum tree_code code
= TREE_CODE (type
);
1390 if (code
== FUNCTION_TYPE
|| code
== METHOD_TYPE
)
1391 return size_int (FUNCTION_BOUNDARY
/ BITS_PER_UNIT
);
1393 if (code
== VOID_TYPE
|| code
== ERROR_MARK
)
1394 return size_int (1);
1396 /* C++: this is really correct! */
1397 if (code
== REFERENCE_TYPE
)
1398 type
= TREE_TYPE (type
);
1400 /* @@ This also produces an error for a signature ref.
1401 In that case we should be able to do better. */
1402 if (IS_SIGNATURE (type
))
1404 error ("`__alignof' applied to a signature type");
1405 return size_int (1);
1408 t
= size_int (TYPE_ALIGN (type
) / BITS_PER_UNIT
);
1409 force_fit_type (t
, 0);
1413 /* Perform default promotions for C data used in expressions.
1414 Arrays and functions are converted to pointers;
1415 enumeral types or short or char, to int.
1416 In addition, manifest constants symbols are replaced by their values.
1418 C++: this will automatically bash references to their target type. */
1421 decay_conversion (exp
)
1424 register tree type
= TREE_TYPE (exp
);
1425 register enum tree_code code
= TREE_CODE (type
);
1427 if (code
== OFFSET_TYPE
/* || TREE_CODE (exp) == OFFSET_REF */ )
1429 if (TREE_CODE (exp
) == OFFSET_REF
)
1430 return decay_conversion (resolve_offset_ref (exp
));
1432 type
= TREE_TYPE (type
);
1433 code
= TREE_CODE (type
);
1436 if (code
== REFERENCE_TYPE
)
1438 exp
= convert_from_reference (exp
);
1439 type
= TREE_TYPE (exp
);
1440 code
= TREE_CODE (type
);
1443 /* Constants can be used directly unless they're not loadable. */
1444 if (TREE_CODE (exp
) == CONST_DECL
)
1445 exp
= DECL_INITIAL (exp
);
1446 /* Replace a nonvolatile const static variable with its value. */
1447 else if (TREE_READONLY_DECL_P (exp
))
1449 exp
= decl_constant_value (exp
);
1450 type
= TREE_TYPE (exp
);
1453 /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
1454 Leave such NOP_EXPRs, since RHS is being used in non-lvalue context. */
1456 if (code
== VOID_TYPE
)
1458 error ("void value not ignored as it ought to be");
1459 return error_mark_node
;
1461 if (code
== FUNCTION_TYPE
)
1463 return build_unary_op (ADDR_EXPR
, exp
, 0);
1465 if (code
== METHOD_TYPE
)
1467 if (TREE_CODE (exp
) == OFFSET_REF
)
1469 /* FIXME: We should emit an error here about using a ptrmemfunc
1470 for something other than a function call. */
1471 my_friendly_assert (TREE_CODE (TREE_OPERAND (exp
, 1)) == FUNCTION_DECL
,
1473 return build_unary_op (ADDR_EXPR
, TREE_OPERAND (exp
, 1), 0);
1475 return build_unary_op (ADDR_EXPR
, exp
, 0);
1477 if (code
== ARRAY_TYPE
)
1482 int constp
, volatilep
;
1484 if (TREE_CODE (exp
) == INDIRECT_REF
)
1486 /* Stripping away the INDIRECT_REF is not the right
1487 thing to do for references... */
1488 tree inner
= TREE_OPERAND (exp
, 0);
1489 if (TREE_CODE (TREE_TYPE (inner
)) == REFERENCE_TYPE
)
1491 inner
= build1 (CONVERT_EXPR
,
1492 build_pointer_type (TREE_TYPE (TREE_TYPE (inner
))),
1494 TREE_REFERENCE_EXPR (inner
) = 1;
1496 return convert (build_pointer_type (TREE_TYPE (type
)), inner
);
1499 if (TREE_CODE (exp
) == COMPOUND_EXPR
)
1501 tree op1
= decay_conversion (TREE_OPERAND (exp
, 1));
1502 return build (COMPOUND_EXPR
, TREE_TYPE (op1
),
1503 TREE_OPERAND (exp
, 0), op1
);
1507 && ! (TREE_CODE (exp
) == CONSTRUCTOR
&& TREE_STATIC (exp
)))
1509 error ("invalid use of non-lvalue array");
1510 return error_mark_node
;
1513 constp
= volatilep
= 0;
1514 if (TREE_CODE_CLASS (TREE_CODE (exp
)) == 'r'
1515 || TREE_CODE_CLASS (TREE_CODE (exp
)) == 'd')
1517 constp
= TREE_READONLY (exp
);
1518 volatilep
= TREE_THIS_VOLATILE (exp
);
1521 restype
= TREE_TYPE (type
);
1522 if (TYPE_READONLY (type
) || TYPE_VOLATILE (type
)
1523 || constp
|| volatilep
)
1524 restype
= cp_build_type_variant (restype
,
1525 TYPE_READONLY (type
) || constp
,
1526 TYPE_VOLATILE (type
) || volatilep
);
1527 ptrtype
= build_pointer_type (restype
);
1529 if (TREE_CODE (exp
) == VAR_DECL
)
1531 /* ??? This is not really quite correct
1532 in that the type of the operand of ADDR_EXPR
1533 is not the target type of the type of the ADDR_EXPR itself.
1534 Question is, can this lossage be avoided? */
1535 adr
= build1 (ADDR_EXPR
, ptrtype
, exp
);
1536 if (mark_addressable (exp
) == 0)
1537 return error_mark_node
;
1538 TREE_CONSTANT (adr
) = staticp (exp
);
1539 TREE_SIDE_EFFECTS (adr
) = 0; /* Default would be, same as EXP. */
1542 /* This way is better for a COMPONENT_REF since it can
1543 simplify the offset for a component. */
1544 adr
= build_unary_op (ADDR_EXPR
, exp
, 1);
1545 return convert (ptrtype
, adr
);
1552 default_conversion (exp
)
1556 enum tree_code code
;
1558 exp
= decay_conversion (exp
);
1560 type
= TREE_TYPE (exp
);
1561 code
= TREE_CODE (type
);
1563 if (INTEGRAL_CODE_P (code
))
1565 tree t
= type_promotes_to (type
);
1567 return convert (t
, exp
);
1569 if (flag_traditional
1570 && TYPE_MAIN_VARIANT (type
) == float_type_node
)
1571 return convert (double_type_node
, exp
);
1576 /* Take the address of an inline function without setting TREE_ADDRESSABLE
1580 inline_conversion (exp
)
1583 if (TREE_CODE (exp
) == FUNCTION_DECL
)
1585 tree type
= build_type_variant
1586 (TREE_TYPE (exp
), TREE_READONLY (exp
), TREE_THIS_VOLATILE (exp
));
1587 exp
= build1 (ADDR_EXPR
, build_pointer_type (type
), exp
);
1593 build_object_ref (datum
, basetype
, field
)
1594 tree datum
, basetype
, field
;
1597 if (datum
== error_mark_node
)
1598 return error_mark_node
;
1600 dtype
= TREE_TYPE (datum
);
1601 if (TREE_CODE (dtype
) == REFERENCE_TYPE
)
1602 dtype
= TREE_TYPE (dtype
);
1603 if (! IS_AGGR_TYPE_CODE (TREE_CODE (dtype
)))
1605 cp_error ("request for member `%T::%D' in expression of non-aggregate type `%T'",
1606 basetype
, field
, dtype
);
1607 return error_mark_node
;
1609 else if (IS_SIGNATURE (basetype
))
1611 warning ("signature name in scope resolution ignored");
1612 return build_component_ref (datum
, field
, NULL_TREE
, 1);
1614 else if (is_aggr_type (basetype
, 1))
1616 tree binfo
= binfo_or_else (basetype
, dtype
);
1618 return build_component_ref (build_scoped_ref (datum
, basetype
),
1621 return error_mark_node
;
1624 /* Like `build_component_ref, but uses an already found field.
1625 Must compute access for C_C_D. Otherwise, ok. */
1627 build_component_ref_1 (datum
, field
, protect
)
1631 register tree basetype
= TREE_TYPE (datum
);
1632 register enum tree_code code
= TREE_CODE (basetype
);
1635 if (code
== REFERENCE_TYPE
)
1637 datum
= convert_from_reference (datum
);
1638 basetype
= TREE_TYPE (datum
);
1639 code
= TREE_CODE (basetype
);
1642 if (! IS_AGGR_TYPE_CODE (code
))
1644 if (code
!= ERROR_MARK
)
1645 cp_error ("request for member `%D' in `%E', which is of non-aggregate type `%T'",
1646 field
, datum
, basetype
);
1647 return error_mark_node
;
1650 if (TYPE_SIZE (basetype
) == 0)
1652 incomplete_type_error (0, basetype
);
1653 return error_mark_node
;
1656 /* Look up component name in the structure type definition. */
1658 if (field
== error_mark_node
)
1659 my_friendly_abort (115);
1661 if (TREE_STATIC (field
))
1666 tree access
= compute_access (TYPE_BINFO (current_class_type
), field
);
1668 if (access
== access_private_node
)
1670 cp_error ("field `%D' is private", field
);
1671 return error_mark_node
;
1673 else if (access
== access_protected_node
)
1675 cp_error ("field `%D' is protected", field
);
1676 return error_mark_node
;
1680 ref
= build (COMPONENT_REF
, TREE_TYPE (field
), datum
, field
);
1682 if (TREE_READONLY (datum
) || TREE_READONLY (field
))
1683 TREE_READONLY (ref
) = 1;
1684 if (TREE_THIS_VOLATILE (datum
) || TREE_THIS_VOLATILE (field
))
1685 TREE_THIS_VOLATILE (ref
) = 1;
1686 if (DECL_MUTABLE_P (field
))
1687 TREE_READONLY (ref
) = 0;
1692 /* Given a COND_EXPR in T, return it in a form that we can, for
1693 example, use as an lvalue. This code used to be in unary_complex_lvalue,
1694 but we needed it to deal with `a = (d == c) ? b : c' expressions, where
1695 we're dealing with aggregates. So, we now call this in unary_complex_lvalue,
1696 and in build_modify_expr. The case (in particular) that led to this was
1697 with CODE == ADDR_EXPR, since it's not an lvalue when we'd get it there. */
1699 rationalize_conditional_expr (code
, t
)
1700 enum tree_code code
;
1704 build_conditional_expr (TREE_OPERAND (t
, 0),
1705 build_unary_op (code
, TREE_OPERAND (t
, 1), 0),
1706 build_unary_op (code
, TREE_OPERAND (t
, 2), 0));
1709 /* Given the TYPE of an anonymous union field inside T, return the
1710 FIELD_DECL for the field. If not found return NULL_TREE. Because
1711 anonymous unions can nest, we must also search all anonymous unions
1712 that are directly reachable. */
1714 lookup_anon_field (t
, type
)
1719 for (field
= TYPE_FIELDS (t
); field
; field
= TREE_CHAIN (field
))
1721 if (TREE_STATIC (field
))
1723 if (TREE_CODE (field
) != FIELD_DECL
)
1726 /* If we find it directly, return the field. */
1727 if (DECL_NAME (field
) == NULL_TREE
1728 && type
== TREE_TYPE (field
))
1733 /* Otherwise, it could be nested, search harder. */
1734 if (DECL_NAME (field
) == NULL_TREE
1735 && TREE_CODE (TREE_TYPE (field
)) == UNION_TYPE
)
1737 tree subfield
= lookup_anon_field (TREE_TYPE (field
), type
);
1745 /* Build a COMPONENT_REF for a given DATUM, and it's member COMPONENT.
1746 COMPONENT can be an IDENTIFIER_NODE that is the name of the member
1747 that we are interested in, or it can be a FIELD_DECL. */
1749 build_component_ref (datum
, component
, basetype_path
, protect
)
1750 tree datum
, component
, basetype_path
;
1753 register tree basetype
= TREE_TYPE (datum
);
1754 register enum tree_code code
;
1755 register tree field
= NULL
;
1758 if (current_template_parms
)
1759 return build_min_nt (COMPONENT_REF
, datum
, component
);
1761 /* If DATUM is a COMPOUND_EXPR or COND_EXPR, move our reference inside it. */
1762 switch (TREE_CODE (datum
))
1766 tree value
= build_component_ref (TREE_OPERAND (datum
, 1), component
,
1767 basetype_path
, protect
);
1768 return build (COMPOUND_EXPR
, TREE_TYPE (value
),
1769 TREE_OPERAND (datum
, 0), value
);
1772 return build_conditional_expr
1773 (TREE_OPERAND (datum
, 0),
1774 build_component_ref (TREE_OPERAND (datum
, 1), component
,
1775 basetype_path
, protect
),
1776 build_component_ref (TREE_OPERAND (datum
, 2), component
,
1777 basetype_path
, protect
));
1780 code
= TREE_CODE (basetype
);
1782 if (code
== REFERENCE_TYPE
)
1784 datum
= convert_from_reference (datum
);
1785 basetype
= TREE_TYPE (datum
);
1786 code
= TREE_CODE (basetype
);
1788 if (TREE_CODE (datum
) == OFFSET_REF
)
1790 datum
= resolve_offset_ref (datum
);
1791 basetype
= TREE_TYPE (datum
);
1792 code
= TREE_CODE (basetype
);
1795 /* First, see if there is a field or component with name COMPONENT. */
1796 if (TREE_CODE (component
) == TREE_LIST
)
1798 my_friendly_assert (!(TREE_CHAIN (component
) == NULL_TREE
1799 && DECL_CHAIN (TREE_VALUE (component
)) == NULL_TREE
), 309);
1800 return build (COMPONENT_REF
, TREE_TYPE (component
), datum
, component
);
1803 if (! IS_AGGR_TYPE_CODE (code
))
1805 if (code
!= ERROR_MARK
)
1806 cp_error ("request for member `%D' in `%E', which is of non-aggregate type `%T'",
1807 component
, datum
, basetype
);
1808 return error_mark_node
;
1811 if (TYPE_SIZE (complete_type (basetype
)) == 0)
1813 incomplete_type_error (0, basetype
);
1814 return error_mark_node
;
1817 if (TREE_CODE (component
) == BIT_NOT_EXPR
)
1819 if (TYPE_IDENTIFIER (basetype
) != TREE_OPERAND (component
, 0))
1821 cp_error ("destructor specifier `%T::~%T' must have matching names",
1822 basetype
, TREE_OPERAND (component
, 0));
1823 return error_mark_node
;
1825 if (! TYPE_HAS_DESTRUCTOR (basetype
))
1827 cp_error ("type `%T' has no destructor", basetype
);
1828 return error_mark_node
;
1830 return TREE_VEC_ELT (CLASSTYPE_METHOD_VEC (basetype
), 1);
1833 /* Look up component name in the structure type definition. */
1834 if (CLASSTYPE_VFIELD (basetype
)
1835 && DECL_NAME (CLASSTYPE_VFIELD (basetype
)) == component
)
1836 /* Special-case this because if we use normal lookups in an ambiguous
1837 hierarchy, the compiler will abort (because vptr lookups are
1838 not supposed to be ambiguous. */
1839 field
= CLASSTYPE_VFIELD (basetype
);
1840 else if (TREE_CODE (component
) == FIELD_DECL
)
1846 if (basetype_path
== NULL_TREE
)
1847 basetype_path
= TYPE_BINFO (basetype
);
1848 field
= lookup_field (basetype_path
, component
,
1849 protect
&& ! VFIELD_NAME_P (component
), 0);
1850 if (field
== error_mark_node
)
1851 return error_mark_node
;
1853 if (field
== NULL_TREE
)
1855 /* Not found as a data field, look for it as a method. If found,
1856 then if this is the only possible one, return it, else
1857 report ambiguity error. */
1858 tree fndecls
= lookup_fnfields (basetype_path
, component
, 1);
1859 if (fndecls
== error_mark_node
)
1860 return error_mark_node
;
1863 if (TREE_CHAIN (fndecls
) == NULL_TREE
1864 && DECL_CHAIN (TREE_VALUE (fndecls
)) == NULL_TREE
)
1866 tree access
, fndecl
;
1868 /* Unique, so use this one now. */
1869 basetype
= TREE_PURPOSE (fndecls
);
1870 fndecl
= TREE_VALUE (fndecls
);
1871 access
= compute_access (TREE_PURPOSE (fndecls
), fndecl
);
1872 if (access
== access_public_node
)
1874 if (DECL_VINDEX (fndecl
)
1875 && ! resolves_to_fixed_type_p (datum
, 0))
1877 tree addr
= build_unary_op (ADDR_EXPR
, datum
, 0);
1878 addr
= convert_pointer_to (DECL_CONTEXT (fndecl
), addr
);
1879 datum
= build_indirect_ref (addr
, NULL_PTR
);
1880 my_friendly_assert (datum
!= error_mark_node
, 310);
1881 fndecl
= build_vfn_ref (&addr
, datum
, DECL_VINDEX (fndecl
));
1886 if (access
== access_protected_node
)
1887 cp_error ("member function `%D' is protected", fndecl
);
1889 cp_error ("member function `%D' is private", fndecl
);
1890 return error_mark_node
;
1894 /* Just act like build_offset_ref, since the object does
1895 not matter unless we're actually calling the function. */
1898 t
= build_tree_list (error_mark_node
, fndecls
);
1899 TREE_TYPE (t
) = build_offset_type (basetype
,
1905 cp_error ("`%#T' has no member named `%D'", basetype
, component
);
1906 return error_mark_node
;
1908 else if (TREE_TYPE (field
) == error_mark_node
)
1909 return error_mark_node
;
1911 if (TREE_CODE (field
) != FIELD_DECL
)
1913 if (TREE_CODE (field
) == TYPE_DECL
)
1915 cp_error ("invalid use of type decl `%#D' as expression", field
);
1916 return error_mark_node
;
1918 else if (DECL_RTL (field
) != 0)
1921 TREE_USED (field
) = 1;
1926 /* See if we have to do any conversions so that we pick up the field from the
1928 if (DECL_FIELD_CONTEXT (field
) != basetype
)
1930 tree context
= DECL_FIELD_CONTEXT (field
);
1931 tree base
= context
;
1932 while (base
!= basetype
&& TYPE_NAME (base
)
1933 && ANON_AGGRNAME_P (TYPE_IDENTIFIER (base
)))
1935 base
= TYPE_CONTEXT (base
);
1938 /* Handle base classes here... */
1939 if (base
!= basetype
&& TYPE_USES_COMPLEX_INHERITANCE (basetype
))
1941 tree addr
= build_unary_op (ADDR_EXPR
, datum
, 0);
1942 if (integer_zerop (addr
))
1944 error ("invalid reference to NULL ptr, use ptr-to-member instead");
1945 return error_mark_node
;
1947 if (VBASE_NAME_P (DECL_NAME (field
)))
1949 /* It doesn't matter which vbase pointer we grab, just
1950 find one of them. */
1951 tree binfo
= get_binfo (base
,
1952 TREE_TYPE (TREE_TYPE (addr
)), 0);
1953 addr
= convert_pointer_to_real (binfo
, addr
);
1956 addr
= convert_pointer_to (base
, addr
);
1957 datum
= build_indirect_ref (addr
, NULL_PTR
);
1958 my_friendly_assert (datum
!= error_mark_node
, 311);
1962 /* Handle things from anon unions here... */
1963 if (TYPE_NAME (context
) && ANON_AGGRNAME_P (TYPE_IDENTIFIER (context
)))
1965 tree subfield
= lookup_anon_field (basetype
, context
);
1966 tree subdatum
= build_component_ref (datum
, subfield
,
1967 basetype_path
, protect
);
1968 return build_component_ref (subdatum
, field
, basetype_path
, protect
);
1972 ref
= fold (build (COMPONENT_REF
, TREE_TYPE (field
),
1973 break_out_cleanups (datum
), field
));
1975 if (TREE_READONLY (datum
) || TREE_READONLY (field
))
1976 TREE_READONLY (ref
) = 1;
1977 if (TREE_THIS_VOLATILE (datum
) || TREE_THIS_VOLATILE (field
))
1978 TREE_THIS_VOLATILE (ref
) = 1;
1979 if (DECL_MUTABLE_P (field
))
1980 TREE_READONLY (ref
) = 0;
1985 /* Given an expression PTR for a pointer, return an expression
1986 for the value pointed to.
1987 ERRORSTRING is the name of the operator to appear in error messages.
1989 This function may need to overload OPERATOR_FNNAME.
1990 Must also handle REFERENCE_TYPEs for C++. */
1993 build_x_indirect_ref (ptr
, errorstring
)
1999 if (current_template_parms
)
2000 return build_min_nt (INDIRECT_REF
, ptr
);
2002 rval
= build_opfncall (INDIRECT_REF
, LOOKUP_NORMAL
, ptr
, NULL_TREE
, NULL_TREE
);
2005 return build_indirect_ref (ptr
, errorstring
);
2009 build_indirect_ref (ptr
, errorstring
)
2013 register tree pointer
= (TREE_CODE (TREE_TYPE (ptr
)) == REFERENCE_TYPE
?
2014 ptr
: default_conversion (ptr
));
2015 register tree type
= TREE_TYPE (pointer
);
2017 if (ptr
== current_class_decl
)
2020 if (IS_AGGR_TYPE (type
))
2022 ptr
= build_expr_type_conversion (WANT_POINTER
, pointer
, 1);
2027 type
= TREE_TYPE (pointer
);
2031 if (TREE_CODE (type
) == POINTER_TYPE
|| TREE_CODE (type
) == REFERENCE_TYPE
)
2033 if (TREE_CODE (pointer
) == ADDR_EXPR
2034 && (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_OPERAND (pointer
, 0)))
2035 == TYPE_MAIN_VARIANT (TREE_TYPE (type
)))
2036 && (TREE_READONLY (TREE_OPERAND (pointer
, 0))
2037 == TYPE_READONLY (TREE_TYPE (type
)))
2038 && (TREE_THIS_VOLATILE (TREE_OPERAND (pointer
, 0))
2039 == TYPE_VOLATILE (TREE_TYPE (type
))))
2040 return TREE_OPERAND (pointer
, 0);
2043 tree t
= TREE_TYPE (type
);
2044 register tree ref
= build1 (INDIRECT_REF
,
2045 TYPE_MAIN_VARIANT (t
), pointer
);
2047 TREE_READONLY (ref
) = TYPE_READONLY (t
);
2048 TREE_THIS_VOLATILE (ref
) = TYPE_VOLATILE (t
);
2049 TREE_SIDE_EFFECTS (ref
)
2050 = TYPE_VOLATILE (t
) || TREE_SIDE_EFFECTS (pointer
);
2054 /* `pointer' won't be an error_mark_node if we were given a
2055 pointer to member, so it's cool to check for this here. */
2056 else if (TYPE_PTRMEMFUNC_P (type
))
2057 error ("invalid use of `%s' on pointer to member function", errorstring
);
2058 else if (TREE_CODE (type
) == RECORD_TYPE
2059 && (IS_SIGNATURE_POINTER (type
) || IS_SIGNATURE_REFERENCE (type
)))
2060 error ("cannot dereference signature pointer/reference");
2061 else if (pointer
!= error_mark_node
)
2064 error ("invalid type argument of `%s'", errorstring
);
2066 error ("invalid type argument");
2068 return error_mark_node
;
2071 /* This handles expressions of the form "a[i]", which denotes
2074 This is logically equivalent in C to *(a+i), but we may do it differently.
2075 If A is a variable or a member, we generate a primitive ARRAY_REF.
2076 This avoids forcing the array out of registers, and can work on
2077 arrays that are not lvalues (for example, members of structures returned
2080 If INDEX is of some user-defined type, it must be converted to
2081 integer type. Otherwise, to make a compatible PLUS_EXPR, it
2082 will inherit the type of the array, which will be some pointer type. */
2085 build_x_array_ref (array
, index
)
2088 tree rval
= build_opfncall (ARRAY_REF
, LOOKUP_NORMAL
, array
, index
, NULL_TREE
);
2091 return build_array_ref (array
, index
);
2095 build_array_ref (array
, idx
)
2100 error ("subscript missing in array reference");
2101 return error_mark_node
;
2104 if (TREE_TYPE (array
) == error_mark_node
2105 || TREE_TYPE (idx
) == error_mark_node
)
2106 return error_mark_node
;
2108 if (TREE_CODE (TREE_TYPE (array
)) == ARRAY_TYPE
2109 && TREE_CODE (array
) != INDIRECT_REF
)
2113 /* Subscripting with type char is likely to lose
2114 on a machine where chars are signed.
2115 So warn on any machine, but optionally.
2116 Don't warn for unsigned char since that type is safe.
2117 Don't warn for signed char because anyone who uses that
2118 must have done so deliberately. */
2119 if (warn_char_subscripts
2120 && TYPE_MAIN_VARIANT (TREE_TYPE (idx
)) == char_type_node
)
2121 warning ("array subscript has type `char'");
2123 /* Apply default promotions *after* noticing character types. */
2124 idx
= default_conversion (idx
);
2126 if (TREE_CODE (TREE_TYPE (idx
)) != INTEGER_TYPE
)
2128 error ("array subscript is not an integer");
2129 return error_mark_node
;
2132 /* An array that is indexed by a non-constant
2133 cannot be stored in a register; we must be able to do
2134 address arithmetic on its address.
2135 Likewise an array of elements of variable size. */
2136 if (TREE_CODE (idx
) != INTEGER_CST
2137 || (TYPE_SIZE (TREE_TYPE (TREE_TYPE (array
))) != 0
2138 && TREE_CODE (TYPE_SIZE (TREE_TYPE (TREE_TYPE (array
)))) != INTEGER_CST
))
2140 if (mark_addressable (array
) == 0)
2141 return error_mark_node
;
2143 /* An array that is indexed by a constant value which is not within
2144 the array bounds cannot be stored in a register either; because we
2145 would get a crash in store_bit_field/extract_bit_field when trying
2146 to access a non-existent part of the register. */
2147 if (TREE_CODE (idx
) == INTEGER_CST
2148 && TYPE_VALUES (TREE_TYPE (array
))
2149 && ! int_fits_type_p (idx
, TYPE_VALUES (TREE_TYPE (array
))))
2151 if (mark_addressable (array
) == 0)
2152 return error_mark_node
;
2155 if (pedantic
&& !lvalue_p (array
))
2156 pedwarn ("ANSI C++ forbids subscripting non-lvalue array");
2158 /* Note in C++ it is valid to subscript a `register' array, since
2159 it is valid to take the address of something with that
2160 storage specification. */
2164 while (TREE_CODE (foo
) == COMPONENT_REF
)
2165 foo
= TREE_OPERAND (foo
, 0);
2166 if (TREE_CODE (foo
) == VAR_DECL
&& DECL_REGISTER (foo
))
2167 warning ("subscripting array declared `register'");
2170 type
= TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (array
)));
2171 rval
= build (ARRAY_REF
, type
, array
, idx
);
2172 /* Array ref is const/volatile if the array elements are
2173 or if the array is.. */
2174 TREE_READONLY (rval
)
2175 |= (TYPE_READONLY (TREE_TYPE (TREE_TYPE (array
)))
2176 | TREE_READONLY (array
));
2177 TREE_SIDE_EFFECTS (rval
)
2178 |= (TYPE_VOLATILE (TREE_TYPE (TREE_TYPE (array
)))
2179 | TREE_SIDE_EFFECTS (array
));
2180 TREE_THIS_VOLATILE (rval
)
2181 |= (TYPE_VOLATILE (TREE_TYPE (TREE_TYPE (array
)))
2182 /* This was added by rms on 16 Nov 91.
2183 It fixes vol struct foo *a; a->elts[1]
2184 in an inline function.
2185 Hope it doesn't break something else. */
2186 | TREE_THIS_VOLATILE (array
));
2187 return require_complete_type (fold (rval
));
2191 tree ar
= default_conversion (array
);
2192 tree ind
= default_conversion (idx
);
2194 /* Put the integer in IND to simplify error checking. */
2195 if (TREE_CODE (TREE_TYPE (ar
)) == INTEGER_TYPE
)
2202 if (ar
== error_mark_node
)
2205 if (TREE_CODE (TREE_TYPE (ar
)) != POINTER_TYPE
)
2207 error ("subscripted value is neither array nor pointer");
2208 return error_mark_node
;
2210 if (TREE_CODE (TREE_TYPE (ind
)) != INTEGER_TYPE
)
2212 error ("array subscript is not an integer");
2213 return error_mark_node
;
2216 return build_indirect_ref (build_binary_op_nodefault (PLUS_EXPR
, ar
, ind
, PLUS_EXPR
),
2221 /* Build a function call to function FUNCTION with parameters PARAMS.
2222 PARAMS is a list--a chain of TREE_LIST nodes--in which the
2223 TREE_VALUE of each node is a parameter-expression.
2224 FUNCTION's data type may be a function type or a pointer-to-function.
2226 For C++: If FUNCTION's data type is a TREE_LIST, then the tree list
2227 is the list of possible methods that FUNCTION could conceivably
2228 be. If the list of methods comes from a class, then it will be
2229 a list of lists (where each element is associated with the class
2230 that produced it), otherwise it will be a simple list (for
2231 functions overloaded in global scope).
2233 In the first case, TREE_VALUE (function) is the head of one of those
2234 lists, and TREE_PURPOSE is the name of the function.
2236 In the second case, TREE_PURPOSE (function) is the function's
2239 DECL is the class instance variable, usually CURRENT_CLASS_DECL. */
2242 * [eichin:19911015.1726EST] actually return a possibly incomplete
2246 build_x_function_call (function
, params
, decl
)
2247 tree function
, params
, decl
;
2252 if (function
== error_mark_node
)
2253 return error_mark_node
;
2255 if (current_template_parms
)
2256 return build_min_nt (CALL_EXPR
, function
, params
, 0);
2258 type
= TREE_TYPE (function
);
2260 if (TREE_CODE (type
) == OFFSET_TYPE
2261 && TREE_TYPE (type
) == unknown_type_node
2262 && TREE_CODE (function
) == TREE_LIST
2263 && TREE_CHAIN (function
) == NULL_TREE
)
2265 /* Undo (Foo:bar)()... */
2266 type
= TYPE_OFFSET_BASETYPE (type
);
2267 function
= TREE_VALUE (function
);
2268 my_friendly_assert (TREE_CODE (function
) == TREE_LIST
, 999);
2269 my_friendly_assert (TREE_CHAIN (function
) == NULL_TREE
, 999);
2270 function
= TREE_VALUE (function
);
2271 my_friendly_assert (TREE_CODE (function
) == FUNCTION_DECL
, 999);
2272 function
= DECL_NAME (function
);
2273 return build_method_call (decl
, function
, params
, TYPE_BINFO (type
), LOOKUP_NORMAL
);
2276 is_method
= ((TREE_CODE (function
) == TREE_LIST
2277 && current_class_type
!= NULL_TREE
2278 && IDENTIFIER_CLASS_VALUE (TREE_PURPOSE (function
)) == function
)
2279 || TREE_CODE (function
) == IDENTIFIER_NODE
2280 || TREE_CODE (type
) == METHOD_TYPE
2281 || TYPE_PTRMEMFUNC_P (type
));
2283 if (TREE_CODE (function
) == FUNCTION_DECL
2284 && DECL_STATIC_FUNCTION_P (function
))
2285 return build_member_call
2286 (DECL_CONTEXT (function
), DECL_NAME (function
), params
);
2288 /* Handle methods, friends, and overloaded functions, respectively. */
2291 if (TREE_CODE (function
) == FUNCTION_DECL
)
2293 if (DECL_NAME (function
))
2294 function
= DECL_NAME (function
);
2296 function
= TYPE_IDENTIFIER (DECL_CLASS_CONTEXT (function
));
2298 else if (TREE_CODE (function
) == TREE_LIST
)
2300 my_friendly_assert (TREE_CODE (TREE_VALUE (function
)) == FUNCTION_DECL
, 312);
2301 function
= TREE_PURPOSE (function
);
2303 else if (TREE_CODE (function
) != IDENTIFIER_NODE
)
2305 if (TREE_CODE (function
) == OFFSET_REF
)
2307 if (TREE_OPERAND (function
, 0))
2308 decl
= TREE_OPERAND (function
, 0);
2310 /* Call via a pointer to member function. */
2311 if (decl
== NULL_TREE
)
2313 error ("pointer to member function called, but not in class scope");
2314 return error_mark_node
;
2316 /* What other type of POINTER_TYPE could this be? */
2317 if (TREE_CODE (TREE_TYPE (function
)) != POINTER_TYPE
2318 && ! TYPE_PTRMEMFUNC_P (TREE_TYPE (function
))
2319 && TREE_CODE (function
) != OFFSET_REF
)
2320 function
= build (OFFSET_REF
, TREE_TYPE (type
), NULL_TREE
, function
);
2324 /* this is an abbreviated method call.
2325 must go through here in case it is a virtual function.
2326 @@ Perhaps this could be optimized. */
2328 if (decl
== NULL_TREE
)
2330 if (current_class_type
== NULL_TREE
)
2332 error ("object missing in call to method `%s'",
2333 IDENTIFIER_POINTER (function
));
2334 return error_mark_node
;
2336 /* Yow: call from a static member function. */
2337 decl
= build1 (NOP_EXPR
, build_pointer_type (current_class_type
),
2339 decl
= build_indirect_ref (decl
, NULL_PTR
);
2342 return build_method_call (decl
, function
, params
,
2343 NULL_TREE
, LOOKUP_NORMAL
);
2345 else if (TREE_CODE (function
) == COMPONENT_REF
2346 && type
== unknown_type_node
)
2348 /* Should we undo what was done in build_component_ref? */
2349 if (TREE_CODE (TREE_PURPOSE (TREE_OPERAND (function
, 1))) == TREE_VEC
)
2350 /* Get the name that build_component_ref hid. */
2351 function
= DECL_NAME (TREE_VALUE (TREE_OPERAND (function
, 1)));
2353 function
= TREE_PURPOSE (TREE_OPERAND (function
, 1));
2354 return build_method_call (decl
, function
, params
,
2355 NULL_TREE
, LOOKUP_NORMAL
);
2357 else if (TREE_CODE (function
) == TREE_LIST
)
2359 if (TREE_VALUE (function
) == NULL_TREE
)
2361 cp_error ("function `%D' declared overloaded, but no definitions appear with which to resolve it?!?",
2362 TREE_PURPOSE (function
));
2363 return error_mark_node
;
2367 tree val
= TREE_VALUE (function
);
2369 if (TREE_CODE (val
) == TEMPLATE_DECL
)
2370 return build_overload_call_maybe
2371 (function
, params
, LOOKUP_COMPLAIN
, (struct candidate
*)0);
2372 else if (DECL_CHAIN (val
) != NULL_TREE
)
2373 return build_overload_call
2374 (function
, params
, LOOKUP_COMPLAIN
, (struct candidate
*)0);
2376 my_friendly_abort (360);
2381 if (TREE_CODE (function
) == OFFSET_REF
)
2383 /* If the component is a data element (or a virtual function), we play
2384 games here to make things work. */
2387 if (TREE_OPERAND (function
, 0))
2388 decl
= TREE_OPERAND (function
, 0);
2392 decl_addr
= build_unary_op (ADDR_EXPR
, decl
, 0);
2393 function
= get_member_function_from_ptrfunc (&decl_addr
,
2394 TREE_OPERAND (function
, 1));
2395 params
= tree_cons (NULL_TREE
, decl_addr
, params
);
2396 return build_function_call (function
, params
);
2399 type
= TREE_TYPE (function
);
2400 if (type
!= error_mark_node
)
2402 if (TREE_CODE (type
) == REFERENCE_TYPE
)
2403 type
= TREE_TYPE (type
);
2405 if (TYPE_LANG_SPECIFIC (type
)
2406 && TYPE_OVERLOADS_CALL_EXPR (complete_type (type
)))
2407 return build_opfncall (CALL_EXPR
, LOOKUP_NORMAL
, function
, params
, NULL_TREE
);
2412 tree fntype
= TREE_TYPE (function
);
2415 /* Explicitly named method? */
2416 if (TREE_CODE (function
) == FUNCTION_DECL
)
2417 ctypeptr
= build_pointer_type (DECL_CLASS_CONTEXT (function
));
2418 /* Expression with ptr-to-method type? It could either be a plain
2419 usage, or it might be a case where the ptr-to-method is being
2420 passed in as an argument. */
2421 else if (TYPE_PTRMEMFUNC_P (fntype
))
2423 tree rec
= TYPE_METHOD_BASETYPE (TREE_TYPE (TYPE_PTRMEMFUNC_FN_TYPE (fntype
)));
2424 ctypeptr
= build_pointer_type (rec
);
2426 /* Unexpected node type? */
2428 my_friendly_abort (116);
2429 if (decl
== NULL_TREE
)
2431 if (current_function_decl
2432 && DECL_STATIC_FUNCTION_P (current_function_decl
))
2433 error ("invalid call to member function needing `this' in static member function scope");
2435 error ("pointer to member function called, but not in class scope");
2436 return error_mark_node
;
2438 if (TREE_CODE (TREE_TYPE (decl
)) != POINTER_TYPE
2439 && ! TYPE_PTRMEMFUNC_P (TREE_TYPE (decl
)))
2441 decl
= build_unary_op (ADDR_EXPR
, decl
, 0);
2442 decl
= convert_pointer_to (TREE_TYPE (ctypeptr
), decl
);
2445 decl
= build_c_cast (ctypeptr
, decl
, 0);
2446 params
= tree_cons (NULL_TREE
, decl
, params
);
2449 return build_function_call (function
, params
);
2452 /* Resolve a pointer to member function. INSTANCE is the object
2453 instance to use, if the member points to a virtual member. */
2456 get_member_function_from_ptrfunc (instance_ptrptr
, function
)
2457 tree
*instance_ptrptr
;
2460 if (TREE_CODE (function
) == OFFSET_REF
)
2462 function
= TREE_OPERAND (function
, 1);
2465 if (TYPE_PTRMEMFUNC_P (TREE_TYPE (function
)))
2467 tree fntype
, index
, e1
, delta
, delta2
, e2
, e3
, aref
, vtbl
;
2470 tree instance_ptr
= *instance_ptrptr
;
2472 if (TREE_SIDE_EFFECTS (instance_ptr
))
2473 instance_ptr
= save_expr (instance_ptr
);
2475 if (TREE_SIDE_EFFECTS (function
))
2476 function
= save_expr (function
);
2478 fntype
= TYPE_PTRMEMFUNC_FN_TYPE (TREE_TYPE (function
));
2479 index
= save_expr (build_component_ref (function
,
2482 e1
= build (GT_EXPR
, boolean_type_node
, index
,
2483 convert (delta_type_node
, integer_zero_node
));
2484 delta
= convert (ptrdiff_type_node
,
2485 build_component_ref (function
, delta_identifier
, 0, 0));
2486 delta2
= DELTA2_FROM_PTRMEMFUNC (function
);
2488 /* convert down to the right base, before using the instance. */
2490 = convert_pointer_to_real (TYPE_METHOD_BASETYPE (TREE_TYPE (fntype
)),
2492 if (instance
== error_mark_node
)
2495 vtbl
= convert_pointer_to (ptr_type_node
, instance
);
2498 build_pointer_type (build_pointer_type (vtable_entry_type
)),
2499 vtbl
, convert (ptrdiff_type_node
, delta2
));
2500 vtbl
= build_indirect_ref (vtbl
, NULL_PTR
);
2501 aref
= build_array_ref (vtbl
, build_binary_op (MINUS_EXPR
,
2503 integer_one_node
, 1));
2504 if (! flag_vtable_thunks
)
2506 aref
= save_expr (aref
);
2508 delta
= build_binary_op (PLUS_EXPR
,
2509 build_conditional_expr (e1
, build_component_ref (aref
, delta_identifier
, 0, 0), integer_zero_node
),
2513 *instance_ptrptr
= build (PLUS_EXPR
, TREE_TYPE (instance_ptr
),
2514 instance_ptr
, delta
);
2515 if (flag_vtable_thunks
)
2518 e2
= build_component_ref (aref
, pfn_identifier
, 0, 0);
2520 e3
= PFN_FROM_PTRMEMFUNC (function
);
2521 TREE_TYPE (e2
) = TREE_TYPE (e3
);
2522 function
= build_conditional_expr (e1
, e2
, e3
);
2524 /* Make sure this doesn't get evaluated first inside one of the
2525 branches of the COND_EXPR. */
2526 if (TREE_CODE (instance_ptr
) == SAVE_EXPR
)
2527 function
= build (COMPOUND_EXPR
, TREE_TYPE (function
),
2528 instance_ptr
, function
);
2534 build_function_call_real (function
, params
, require_complete
, flags
)
2535 tree function
, params
;
2536 int require_complete
, flags
;
2538 register tree fntype
, fndecl
;
2539 register tree value_type
;
2540 register tree coerced_params
;
2541 tree name
= NULL_TREE
, assembler_name
= NULL_TREE
;
2544 /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
2545 Strip such NOP_EXPRs, since FUNCTION is used in non-lvalue context. */
2546 if (TREE_CODE (function
) == NOP_EXPR
2547 && TREE_TYPE (function
) == TREE_TYPE (TREE_OPERAND (function
, 0)))
2548 function
= TREE_OPERAND (function
, 0);
2550 if (TREE_CODE (function
) == FUNCTION_DECL
)
2552 name
= DECL_NAME (function
);
2553 assembler_name
= DECL_ASSEMBLER_NAME (function
);
2555 GNU_xref_call (current_function_decl
,
2556 IDENTIFIER_POINTER (name
? name
2557 : TYPE_IDENTIFIER (DECL_CLASS_CONTEXT (function
))));
2558 mark_used (function
);
2561 /* Convert anything with function type to a pointer-to-function. */
2564 && IDENTIFIER_LENGTH (name
) == 4
2565 && ! strcmp (IDENTIFIER_POINTER (name
), "main")
2566 && DECL_CONTEXT (function
) == NULL_TREE
)
2568 pedwarn ("ANSI C++ forbids calling `main' from within program");
2571 if (pedantic
&& DECL_THIS_INLINE (function
) && ! DECL_INITIAL (function
)
2572 && ! DECL_ARTIFICIAL (function
)
2573 && ! DECL_PENDING_INLINE_INFO (function
))
2574 cp_pedwarn ("inline function `%#D' called before definition",
2577 /* Differs from default_conversion by not setting TREE_ADDRESSABLE
2578 (because calling an inline function does not mean the function
2579 needs to be separately compiled). */
2581 if (DECL_INLINE (function
))
2583 /* Is it a synthesized method that needs to be synthesized? */
2584 if (DECL_ARTIFICIAL (function
) && ! DECL_INITIAL (function
)
2585 /* Kludge: don't synthesize for default args. */
2586 && current_function_decl
)
2587 synthesize_method (function
);
2589 function
= inline_conversion (function
);
2592 function
= default_conversion (function
);
2598 /* Convert anything with function type to a pointer-to-function. */
2599 if (function
== error_mark_node
)
2600 return error_mark_node
;
2601 function
= default_conversion (function
);
2604 fntype
= TREE_TYPE (function
);
2606 if (TYPE_PTRMEMFUNC_P (fntype
))
2608 tree instance_ptr
= build_unary_op (ADDR_EXPR
, C_C_D
, 0);
2609 fntype
= TYPE_PTRMEMFUNC_FN_TYPE (fntype
);
2610 function
= get_member_function_from_ptrfunc (&instance_ptr
, function
);
2613 is_method
= (TREE_CODE (fntype
) == POINTER_TYPE
2614 && TREE_CODE (TREE_TYPE (fntype
)) == METHOD_TYPE
);
2616 if (!((TREE_CODE (fntype
) == POINTER_TYPE
2617 && TREE_CODE (TREE_TYPE (fntype
)) == FUNCTION_TYPE
)
2620 cp_error ("`%E' cannot be used as a function", function
);
2621 return error_mark_node
;
2624 /* fntype now gets the type of function pointed to. */
2625 fntype
= TREE_TYPE (fntype
);
2627 /* Convert the parameters to the types declared in the
2628 function prototype, or apply default promotions. */
2630 if (flags
& LOOKUP_COMPLAIN
)
2631 coerced_params
= convert_arguments (NULL_TREE
, TYPE_ARG_TYPES (fntype
),
2632 params
, fndecl
, LOOKUP_NORMAL
);
2634 coerced_params
= convert_arguments (NULL_TREE
, TYPE_ARG_TYPES (fntype
),
2637 if (coerced_params
== error_mark_node
)
2638 if (flags
& LOOKUP_SPECULATIVELY
)
2641 return error_mark_node
;
2643 /* Check for errors in format strings. */
2645 if (warn_format
&& (name
|| assembler_name
))
2646 check_function_format (name
, assembler_name
, coerced_params
);
2648 /* Recognize certain built-in functions so we can make tree-codes
2649 other than CALL_EXPR. We do this when it enables fold-const.c
2650 to do something useful. */
2652 if (TREE_CODE (function
) == ADDR_EXPR
2653 && TREE_CODE (TREE_OPERAND (function
, 0)) == FUNCTION_DECL
2654 && DECL_BUILT_IN (TREE_OPERAND (function
, 0)))
2655 switch (DECL_FUNCTION_CODE (TREE_OPERAND (function
, 0)))
2660 if (coerced_params
== 0)
2661 return integer_zero_node
;
2662 return build_unary_op (ABS_EXPR
, TREE_VALUE (coerced_params
), 0);
2666 value_type
= TREE_TYPE (fntype
) ? TREE_TYPE (fntype
) : void_type_node
;
2668 register tree result
=
2669 build (CALL_EXPR
, value_type
,
2670 function
, coerced_params
, NULL_TREE
);
2672 TREE_SIDE_EFFECTS (result
) = 1;
2674 if (! require_complete
)
2675 return convert_from_reference (result
);
2676 if (value_type
== void_type_node
)
2678 result
= require_complete_type (result
);
2679 return convert_from_reference (result
);
2684 build_function_call (function
, params
)
2685 tree function
, params
;
2687 return build_function_call_real (function
, params
, 1, LOOKUP_NORMAL
);
2690 /* Convert the actual parameter expressions in the list VALUES
2691 to the types in the list TYPELIST.
2692 If parmdecls is exhausted, or when an element has NULL as its type,
2693 perform the default conversions.
2695 RETURN_LOC is the location of the return value, if known, NULL_TREE
2696 otherwise. This is useful in the case where we can avoid creating
2697 a temporary variable in the case where we can initialize the return
2698 value directly. If we are not eliding constructors, then we set this
2699 to NULL_TREE to avoid this avoidance.
2701 NAME is an IDENTIFIER_NODE or 0. It is used only for error messages.
2703 This is also where warnings about wrong number of args are generated.
2705 Return a list of expressions for the parameters as converted.
2707 Both VALUES and the returned value are chains of TREE_LIST nodes
2708 with the elements of the list in the TREE_VALUE slots of those nodes.
2710 In C++, unspecified trailing parameters can be filled in with their
2711 default arguments, if such were specified. Do so here. */
2714 convert_arguments (return_loc
, typelist
, values
, fndecl
, flags
)
2715 tree return_loc
, typelist
, values
, fndecl
;
2718 register tree typetail
, valtail
;
2719 register tree result
= NULL_TREE
;
2723 if (! flag_elide_constructors
)
2728 if (TREE_CODE (TREE_TYPE (fndecl
)) == METHOD_TYPE
)
2730 if (DECL_NAME (fndecl
) == NULL_TREE
2731 || IDENTIFIER_HAS_TYPE_VALUE (DECL_NAME (fndecl
)))
2732 called_thing
= "constructor";
2734 called_thing
= "member function";
2737 called_thing
= "function";
2740 for (valtail
= values
, typetail
= typelist
;
2742 valtail
= TREE_CHAIN (valtail
), i
++)
2744 register tree type
= typetail
? TREE_VALUE (typetail
) : 0;
2745 register tree val
= TREE_VALUE (valtail
);
2747 if (val
== error_mark_node
)
2748 return error_mark_node
;
2750 if (type
== void_type_node
)
2754 cp_error_at ("too many arguments to %s `%+D'", called_thing
,
2756 error ("at this point in file");
2759 error ("too many arguments to function");
2760 /* In case anybody wants to know if this argument
2763 TREE_TYPE (tree_last (result
)) = error_mark_node
;
2767 /* The tree type of the parameter being passed may not yet be
2768 known. In this case, its type is TYPE_UNKNOWN, and will
2769 be instantiated by the type given by TYPE. If TYPE
2770 is also NULL, the tree type of VAL is ERROR_MARK_NODE. */
2771 if (type
&& type_unknown_p (val
))
2772 val
= require_instantiated_type (type
, val
, integer_zero_node
);
2773 else if (type_unknown_p (val
))
2775 /* Strip the `&' from an overloaded FUNCTION_DECL. */
2776 if (TREE_CODE (val
) == ADDR_EXPR
)
2777 val
= TREE_OPERAND (val
, 0);
2778 if (TREE_CODE (val
) == TREE_LIST
2779 && TREE_CHAIN (val
) == NULL_TREE
2780 && TREE_TYPE (TREE_VALUE (val
)) != NULL_TREE
2781 && (TREE_TYPE (val
) == unknown_type_node
2782 || DECL_CHAIN (TREE_VALUE (val
)) == NULL_TREE
))
2783 /* Instantiates automatically. */
2784 val
= TREE_VALUE (val
);
2787 error ("insufficient type information in parameter list");
2788 val
= integer_zero_node
;
2791 else if (TREE_CODE (val
) == OFFSET_REF
2792 && TREE_CODE (TREE_TYPE (val
)) == METHOD_TYPE
)
2794 /* This is unclean. Should be handled elsewhere. */
2795 val
= build_unary_op (ADDR_EXPR
, val
, 0);
2797 else if (TREE_CODE (val
) == OFFSET_REF
)
2798 val
= resolve_offset_ref (val
);
2800 /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
2801 Strip such NOP_EXPRs, since VAL is used in non-lvalue context. */
2802 if (TREE_CODE (val
) == NOP_EXPR
2803 && TREE_TYPE (val
) == TREE_TYPE (TREE_OPERAND (val
, 0))
2804 && (type
== 0 || TREE_CODE (type
) != REFERENCE_TYPE
))
2805 val
= TREE_OPERAND (val
, 0);
2807 if (type
== 0 || TREE_CODE (type
) != REFERENCE_TYPE
)
2809 if (TREE_CODE (TREE_TYPE (val
)) == ARRAY_TYPE
2810 || TREE_CODE (TREE_TYPE (val
)) == FUNCTION_TYPE
2811 || TREE_CODE (TREE_TYPE (val
)) == METHOD_TYPE
)
2812 val
= default_conversion (val
);
2814 val
= require_complete_type (val
);
2817 if (val
== error_mark_node
)
2818 return error_mark_node
;
2822 /* Formal parm type is specified by a function prototype. */
2825 if (TYPE_SIZE (complete_type (type
)) == 0)
2827 error ("parameter type of called function is incomplete");
2832 parmval
= convert_for_initialization (return_loc
, type
, val
,
2833 flags
|INDIRECT_BIND
,
2834 "argument passing", fndecl
, i
);
2835 #ifdef PROMOTE_PROTOTYPES
2836 if ((TREE_CODE (type
) == INTEGER_TYPE
2837 || TREE_CODE (type
) == ENUMERAL_TYPE
)
2838 && (TYPE_PRECISION (type
) < TYPE_PRECISION (integer_type_node
)))
2839 parmval
= default_conversion (parmval
);
2843 if (parmval
== error_mark_node
)
2844 return error_mark_node
;
2846 result
= tree_cons (NULL_TREE
, parmval
, result
);
2850 if (TREE_CODE (TREE_TYPE (val
)) == REFERENCE_TYPE
)
2851 val
= convert_from_reference (val
);
2853 if (TREE_CODE (TREE_TYPE (val
)) == REAL_TYPE
2854 && (TYPE_PRECISION (TREE_TYPE (val
))
2855 < TYPE_PRECISION (double_type_node
)))
2856 /* Convert `float' to `double'. */
2857 result
= tree_cons (NULL_TREE
, convert (double_type_node
, val
), result
);
2858 else if (TYPE_LANG_SPECIFIC (TREE_TYPE (val
))
2859 && ! TYPE_HAS_TRIVIAL_INIT_REF (TREE_TYPE (val
)))
2861 cp_warning ("cannot pass objects of type `%T' through `...'",
2863 result
= tree_cons (NULL_TREE
, val
, result
);
2866 /* Convert `short' and `char' to full-size `int'. */
2867 result
= tree_cons (NULL_TREE
, default_conversion (val
), result
);
2871 typetail
= TREE_CHAIN (typetail
);
2874 if (typetail
!= 0 && typetail
!= void_list_node
)
2876 /* See if there are default arguments that can be used */
2877 if (TREE_PURPOSE (typetail
))
2879 for (; typetail
!= void_list_node
; ++i
)
2881 tree type
= TREE_VALUE (typetail
);
2882 tree val
= break_out_target_exprs (TREE_PURPOSE (typetail
));
2885 if (val
== NULL_TREE
)
2886 parmval
= error_mark_node
;
2887 else if (TREE_CODE (val
) == CONSTRUCTOR
)
2889 parmval
= digest_init (type
, val
, (tree
*)0);
2890 parmval
= convert_for_initialization (return_loc
, type
, parmval
, flags
,
2891 "default constructor", fndecl
, i
);
2895 /* This could get clobbered by the following call. */
2896 if (TREE_HAS_CONSTRUCTOR (val
))
2897 val
= copy_node (val
);
2899 parmval
= convert_for_initialization (return_loc
, type
, val
, flags
,
2900 "default argument", fndecl
, i
);
2901 #ifdef PROMOTE_PROTOTYPES
2902 if ((TREE_CODE (type
) == INTEGER_TYPE
2903 || TREE_CODE (type
) == ENUMERAL_TYPE
)
2904 && (TYPE_PRECISION (type
) < TYPE_PRECISION (integer_type_node
)))
2905 parmval
= default_conversion (parmval
);
2909 if (parmval
== error_mark_node
)
2910 return error_mark_node
;
2912 result
= tree_cons (0, parmval
, result
);
2913 typetail
= TREE_CHAIN (typetail
);
2914 /* ends with `...'. */
2915 if (typetail
== NULL_TREE
)
2923 char *buf
= (char *)alloca (32 + strlen (called_thing
));
2924 sprintf (buf
, "too few arguments to %s `%%#D'", called_thing
);
2925 cp_error_at (buf
, fndecl
);
2926 error ("at this point in file");
2929 error ("too few arguments to function");
2930 return error_mark_list
;
2934 return nreverse (result
);
2937 /* Build a binary-operation expression, after performing default
2938 conversions on the operands. CODE is the kind of expression to build. */
2941 build_x_binary_op (code
, arg1
, arg2
)
2942 enum tree_code code
;
2947 if (current_template_parms
)
2948 return build_min_nt (code
, arg1
, arg2
);
2950 rval
= build_opfncall (code
, LOOKUP_SPECULATIVELY
,
2951 arg1
, arg2
, NULL_TREE
);
2953 return build_opfncall (code
, LOOKUP_NORMAL
, arg1
, arg2
, NULL_TREE
);
2954 if (code
== MEMBER_REF
)
2955 return build_m_component_ref (build_indirect_ref (arg1
, NULL_PTR
),
2957 return build_binary_op (code
, arg1
, arg2
, 1);
2961 build_binary_op (code
, arg1
, arg2
, convert_p
)
2962 enum tree_code code
;
2974 args
[0] = decay_conversion (args
[0]);
2975 args
[1] = decay_conversion (args
[1]);
2977 if (args
[0] == error_mark_node
|| args
[1] == error_mark_node
)
2978 return error_mark_node
;
2980 type0
= TREE_TYPE (args
[0]);
2981 type1
= TREE_TYPE (args
[1]);
2983 if (type_unknown_p (args
[0]))
2985 args
[0] = instantiate_type (type1
, args
[0], 1);
2986 args
[0] = decay_conversion (args
[0]);
2988 else if (type_unknown_p (args
[1]))
2990 args
[1] = require_instantiated_type (type0
, args
[1],
2992 args
[1] = decay_conversion (args
[1]);
2995 if (IS_AGGR_TYPE (type0
) || IS_AGGR_TYPE (type1
))
2997 /* Try to convert this to something reasonable. */
2998 if (! build_default_binary_type_conversion(code
, &args
[0], &args
[1]))
3000 cp_error ("no match for `%O(%#T, %#T)'", code
,
3001 TREE_TYPE (arg1
), TREE_TYPE (arg2
));
3002 return error_mark_node
;
3006 return build_binary_op_nodefault (code
, args
[0], args
[1], code
);
3009 /* Build a binary-operation expression without default conversions.
3010 CODE is the kind of expression to build.
3011 This function differs from `build' in several ways:
3012 the data type of the result is computed and recorded in it,
3013 warnings are generated if arg data types are invalid,
3014 special handling for addition and subtraction of pointers is known,
3015 and some optimization is done (operations on narrow ints
3016 are done in the narrower type when that gives the same result).
3017 Constant folding is also done before the result is returned.
3019 ERROR_CODE is the code that determines what to say in error messages.
3020 It is usually, but not always, the same as CODE.
3022 Note that the operands will never have enumeral types
3023 because either they have just had the default conversions performed
3024 or they have both just been converted to some other type in which
3025 the arithmetic is to be done.
3027 C++: must do special pointer arithmetic when implementing
3028 multiple inheritance, and deal with pointer to member functions. */
3031 build_binary_op_nodefault (code
, orig_op0
, orig_op1
, error_code
)
3032 enum tree_code code
;
3033 tree orig_op0
, orig_op1
;
3034 enum tree_code error_code
;
3037 register enum tree_code code0
, code1
;
3040 /* Expression code to give to the expression when it is built.
3041 Normally this is CODE, which is what the caller asked for,
3042 but in some special cases we change it. */
3043 register enum tree_code resultcode
= code
;
3045 /* Data type in which the computation is to be performed.
3046 In the simplest cases this is the common type of the arguments. */
3047 register tree result_type
= NULL
;
3049 /* Nonzero means operands have already been type-converted
3050 in whatever way is necessary.
3051 Zero means they need to be converted to RESULT_TYPE. */
3054 /* Nonzero means create the expression with this type, rather than
3056 tree build_type
= 0;
3058 /* Nonzero means after finally constructing the expression
3059 convert it to this type. */
3060 tree final_type
= 0;
3062 /* Nonzero if this is an operation like MIN or MAX which can
3063 safely be computed in short if both args are promoted shorts.
3064 Also implies COMMON.
3065 -1 indicates a bitwise operation; this makes a difference
3066 in the exact conditions for when it is safe to do the operation
3067 in a narrower mode. */
3070 /* Nonzero if this is a comparison operation;
3071 if both args are promoted shorts, compare the original shorts.
3072 Also implies COMMON. */
3073 int short_compare
= 0;
3075 /* Nonzero if this is a right-shift operation, which can be computed on the
3076 original short and then promoted if the operand is a promoted short. */
3077 int short_shift
= 0;
3079 /* Nonzero means set RESULT_TYPE to the common type of the args. */
3082 /* Apply default conversions. */
3083 if (code
== TRUTH_AND_EXPR
|| code
== TRUTH_ANDIF_EXPR
3084 || code
== TRUTH_OR_EXPR
|| code
== TRUTH_ORIF_EXPR
3085 || code
== TRUTH_XOR_EXPR
)
3087 op0
= decay_conversion (orig_op0
);
3088 op1
= decay_conversion (orig_op1
);
3092 op0
= default_conversion (orig_op0
);
3093 op1
= default_conversion (orig_op1
);
3096 type0
= TREE_TYPE (op0
);
3097 type1
= TREE_TYPE (op1
);
3099 /* The expression codes of the data types of the arguments tell us
3100 whether the arguments are integers, floating, pointers, etc. */
3101 code0
= TREE_CODE (type0
);
3102 code1
= TREE_CODE (type1
);
3104 /* Strip NON_LVALUE_EXPRs, etc., since we aren't using as an lvalue. */
3105 STRIP_TYPE_NOPS (op0
);
3106 STRIP_TYPE_NOPS (op1
);
3108 /* If an error was already reported for one of the arguments,
3109 avoid reporting another error. */
3111 if (code0
== ERROR_MARK
|| code1
== ERROR_MARK
)
3112 return error_mark_node
;
3117 /* Handle the pointer + int case. */
3118 if (code0
== POINTER_TYPE
&& code1
== INTEGER_TYPE
)
3119 return pointer_int_sum (PLUS_EXPR
, op0
, op1
);
3120 else if (code1
== POINTER_TYPE
&& code0
== INTEGER_TYPE
)
3121 return pointer_int_sum (PLUS_EXPR
, op1
, op0
);
3127 /* Subtraction of two similar pointers.
3128 We must subtract them as integers, then divide by object size. */
3129 if (code0
== POINTER_TYPE
&& code1
== POINTER_TYPE
3130 && comp_target_types (type0
, type1
, 1))
3131 return pointer_diff (op0
, op1
);
3132 /* Handle pointer minus int. Just like pointer plus int. */
3133 else if (code0
== POINTER_TYPE
&& code1
== INTEGER_TYPE
)
3134 return pointer_int_sum (MINUS_EXPR
, op0
, op1
);
3143 case TRUNC_DIV_EXPR
:
3145 case FLOOR_DIV_EXPR
:
3146 case ROUND_DIV_EXPR
:
3147 case EXACT_DIV_EXPR
:
3148 if ((code0
== INTEGER_TYPE
|| code0
== REAL_TYPE
)
3149 && (code1
== INTEGER_TYPE
|| code1
== REAL_TYPE
))
3151 if (TREE_CODE (op1
) == INTEGER_CST
&& integer_zerop (op1
))
3152 cp_warning ("division by zero in `%E / 0'", op0
);
3153 else if (TREE_CODE (op1
) == REAL_CST
&& real_zerop (op1
))
3154 cp_warning ("division by zero in `%E / 0.'", op0
);
3156 if (!(code0
== INTEGER_TYPE
&& code1
== INTEGER_TYPE
))
3157 resultcode
= RDIV_EXPR
;
3159 /* When dividing two signed integers, we have to promote to int.
3160 unless we divide by a constant != -1. Note that default
3161 conversion will have been performed on the operands at this
3162 point, so we have to dig out the original type to find out if
3164 shorten
= ((TREE_CODE (op0
) == NOP_EXPR
3165 && TREE_UNSIGNED (TREE_TYPE (TREE_OPERAND (op0
, 0))))
3166 || (TREE_CODE (op1
) == INTEGER_CST
3167 && (TREE_INT_CST_LOW (op1
) != -1
3168 || TREE_INT_CST_HIGH (op1
) != -1)));
3174 case BIT_ANDTC_EXPR
:
3177 if (code0
== INTEGER_TYPE
&& code1
== INTEGER_TYPE
)
3179 /* If one operand is a constant, and the other is a short type
3180 that has been converted to an int,
3181 really do the work in the short type and then convert the
3182 result to int. If we are lucky, the constant will be 0 or 1
3183 in the short type, making the entire operation go away. */
3184 if (TREE_CODE (op0
) == INTEGER_CST
3185 && TREE_CODE (op1
) == NOP_EXPR
3186 && TYPE_PRECISION (type1
) > TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (op1
, 0)))
3187 && TREE_UNSIGNED (TREE_TYPE (TREE_OPERAND (op1
, 0))))
3189 final_type
= result_type
;
3190 op1
= TREE_OPERAND (op1
, 0);
3191 result_type
= TREE_TYPE (op1
);
3193 if (TREE_CODE (op1
) == INTEGER_CST
3194 && TREE_CODE (op0
) == NOP_EXPR
3195 && TYPE_PRECISION (type0
) > TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (op0
, 0)))
3196 && TREE_UNSIGNED (TREE_TYPE (TREE_OPERAND (op0
, 0))))
3198 final_type
= result_type
;
3199 op0
= TREE_OPERAND (op0
, 0);
3200 result_type
= TREE_TYPE (op0
);
3204 case TRUNC_MOD_EXPR
:
3205 case FLOOR_MOD_EXPR
:
3206 if (code1
== INTEGER_TYPE
&& integer_zerop (op1
))
3207 cp_warning ("division by zero in `%E % 0'", op0
);
3208 else if (code1
== REAL_TYPE
&& real_zerop (op1
))
3209 cp_warning ("division by zero in `%E % 0.'", op0
);
3211 if (code0
== INTEGER_TYPE
&& code1
== INTEGER_TYPE
)
3213 /* Although it would be tempting to shorten always here, that loses
3214 on some targets, since the modulo instruction is undefined if the
3215 quotient can't be represented in the computation mode. We shorten
3216 only if unsigned or if dividing by something we know != -1. */
3217 shorten
= ((TREE_CODE (op0
) == NOP_EXPR
3218 && TREE_UNSIGNED (TREE_TYPE (TREE_OPERAND (op0
, 0))))
3219 || (TREE_CODE (op1
) == INTEGER_CST
3220 && (TREE_INT_CST_LOW (op1
) != -1
3221 || TREE_INT_CST_HIGH (op1
) != -1)));
3226 case TRUTH_ANDIF_EXPR
:
3227 case TRUTH_ORIF_EXPR
:
3228 case TRUTH_AND_EXPR
:
3230 result_type
= boolean_type_node
;
3233 /* Shift operations: result has same type as first operand;
3234 always convert second operand to int.
3235 Also set SHORT_SHIFT if shifting rightward. */
3238 if (code0
== INTEGER_TYPE
&& code1
== INTEGER_TYPE
)
3240 result_type
= type0
;
3241 if (TREE_CODE (op1
) == INTEGER_CST
)
3243 if (tree_int_cst_lt (op1
, integer_zero_node
))
3244 warning ("right shift count is negative");
3247 if (TREE_INT_CST_LOW (op1
) | TREE_INT_CST_HIGH (op1
))
3249 if (TREE_INT_CST_HIGH (op1
) != 0
3250 || ((unsigned HOST_WIDE_INT
) TREE_INT_CST_LOW (op1
)
3251 >= TYPE_PRECISION (type0
)))
3252 warning ("right shift count >= width of type");
3255 /* Convert the shift-count to an integer, regardless of
3256 size of value being shifted. */
3257 if (TYPE_MAIN_VARIANT (TREE_TYPE (op1
)) != integer_type_node
)
3258 op1
= convert (integer_type_node
, op1
);
3259 /* Avoid converting op1 to result_type later. */
3265 if (code0
== INTEGER_TYPE
&& code1
== INTEGER_TYPE
)
3267 result_type
= type0
;
3268 if (TREE_CODE (op1
) == INTEGER_CST
)
3270 if (tree_int_cst_lt (op1
, integer_zero_node
))
3271 warning ("left shift count is negative");
3272 else if (TREE_INT_CST_HIGH (op1
) != 0
3273 || ((unsigned HOST_WIDE_INT
) TREE_INT_CST_LOW (op1
)
3274 >= TYPE_PRECISION (type0
)))
3275 warning ("left shift count >= width of type");
3277 /* Convert the shift-count to an integer, regardless of
3278 size of value being shifted. */
3279 if (TYPE_MAIN_VARIANT (TREE_TYPE (op1
)) != integer_type_node
)
3280 op1
= convert (integer_type_node
, op1
);
3281 /* Avoid converting op1 to result_type later. */
3288 if (code0
== INTEGER_TYPE
&& code1
== INTEGER_TYPE
)
3290 result_type
= type0
;
3291 if (TREE_CODE (op1
) == INTEGER_CST
)
3293 if (tree_int_cst_lt (op1
, integer_zero_node
))
3294 warning ("%s rotate count is negative",
3295 (code
== LROTATE_EXPR
) ? "left" : "right");
3296 else if (TREE_INT_CST_HIGH (op1
) != 0
3297 || ((unsigned HOST_WIDE_INT
) TREE_INT_CST_LOW (op1
)
3298 >= TYPE_PRECISION (type0
)))
3299 warning ("%s rotate count >= width of type",
3300 (code
== LROTATE_EXPR
) ? "left" : "right");
3302 /* Convert the shift-count to an integer, regardless of
3303 size of value being shifted. */
3304 if (TYPE_MAIN_VARIANT (TREE_TYPE (op1
)) != integer_type_node
)
3305 op1
= convert (integer_type_node
, op1
);
3311 build_type
= boolean_type_node
;
3312 if ((code0
== INTEGER_TYPE
|| code0
== REAL_TYPE
)
3313 && (code1
== INTEGER_TYPE
|| code1
== REAL_TYPE
))
3315 else if (code0
== POINTER_TYPE
&& code1
== POINTER_TYPE
)
3317 register tree tt0
= TYPE_MAIN_VARIANT (TREE_TYPE (type0
));
3318 register tree tt1
= TYPE_MAIN_VARIANT (TREE_TYPE (type1
));
3320 if (comp_target_types (type0
, type1
, 1))
3321 result_type
= common_type (type0
, type1
);
3322 else if (tt0
== void_type_node
)
3324 if (pedantic
&& TREE_CODE (tt1
) == FUNCTION_TYPE
3325 && tree_int_cst_lt (TYPE_SIZE (type0
), TYPE_SIZE (type1
)))
3326 pedwarn ("ANSI C++ forbids comparison of `void *' with function pointer");
3327 else if (TREE_CODE (tt1
) == OFFSET_TYPE
)
3328 pedwarn ("ANSI C++ forbids conversion of a pointer to member to `void *'");
3330 else if (tt1
== void_type_node
)
3332 if (pedantic
&& TREE_CODE (tt0
) == FUNCTION_TYPE
3333 && tree_int_cst_lt (TYPE_SIZE (type1
), TYPE_SIZE (type0
)))
3334 pedwarn ("ANSI C++ forbids comparison of `void *' with function pointer");
3337 cp_pedwarn ("comparison of distinct pointer types `%T' and `%T' lacks a cast",
3340 if (result_type
== NULL_TREE
)
3341 result_type
= ptr_type_node
;
3343 else if (code0
== POINTER_TYPE
&& TREE_CODE (op1
) == INTEGER_CST
3344 && integer_zerop (op1
))
3345 result_type
= type0
;
3346 else if (code1
== POINTER_TYPE
&& TREE_CODE (op0
) == INTEGER_CST
3347 && integer_zerop (op0
))
3348 result_type
= type1
;
3349 else if (code0
== POINTER_TYPE
&& code1
== INTEGER_TYPE
)
3351 result_type
= type0
;
3352 error ("ANSI C++ forbids comparison between pointer and integer");
3354 else if (code0
== INTEGER_TYPE
&& code1
== POINTER_TYPE
)
3356 result_type
= type1
;
3357 error ("ANSI C++ forbids comparison between pointer and integer");
3359 else if (TYPE_PTRMEMFUNC_P (type0
) && TREE_CODE (op1
) == INTEGER_CST
3360 && integer_zerop (op1
))
3362 op0
= build_component_ref (op0
, index_identifier
, 0, 0);
3363 op1
= integer_zero_node
;
3364 result_type
= TREE_TYPE (op0
);
3366 else if (TYPE_PTRMEMFUNC_P (type1
) && TREE_CODE (op0
) == INTEGER_CST
3367 && integer_zerop (op0
))
3369 op0
= build_component_ref (op1
, index_identifier
, 0, 0);
3370 op1
= integer_zero_node
;
3371 result_type
= TREE_TYPE (op0
);
3373 else if (TYPE_PTRMEMFUNC_P (type0
) && TYPE_PTRMEMFUNC_P (type1
)
3374 && (TYPE_PTRMEMFUNC_FN_TYPE (type0
)
3375 == TYPE_PTRMEMFUNC_FN_TYPE (type1
)))
3377 /* The code we generate for the test is:
3379 (op0.index == op1.index
3380 && ((op1.index != -1 && op0.delta2 == op1.delta2)
3381 || op0.pfn == op1.pfn)) */
3383 tree index0
= build_component_ref (op0
, index_identifier
, 0, 0);
3384 tree index1
= save_expr (build_component_ref (op1
, index_identifier
, 0, 0));
3385 tree pfn0
= PFN_FROM_PTRMEMFUNC (op0
);
3386 tree pfn1
= PFN_FROM_PTRMEMFUNC (op1
);
3387 tree delta20
= DELTA2_FROM_PTRMEMFUNC (op0
);
3388 tree delta21
= DELTA2_FROM_PTRMEMFUNC (op1
);
3390 tree integer_neg_one_node
3391 = build_binary_op (MINUS_EXPR
, integer_zero_node
, integer_one_node
, 1);
3392 e1
= build_binary_op (EQ_EXPR
, index0
, index1
, 1);
3393 e2
= build_binary_op (NE_EXPR
, index1
, integer_neg_one_node
, 1);
3394 e2
= build_binary_op (TRUTH_ANDIF_EXPR
, e2
, build_binary_op (EQ_EXPR
, delta20
, delta21
, 1), 1);
3395 e3
= build_binary_op (EQ_EXPR
, pfn0
, pfn1
, 1);
3396 e2
= build_binary_op (TRUTH_ORIF_EXPR
, e2
, e3
, 1);
3397 e2
= build_binary_op (TRUTH_ANDIF_EXPR
, e1
, e2
, 1);
3398 if (code
== EQ_EXPR
)
3400 return build_binary_op (EQ_EXPR
, e2
, integer_zero_node
, 1);
3402 else if (TYPE_PTRMEMFUNC_P (type0
)
3403 && TYPE_PTRMEMFUNC_FN_TYPE (type0
) == type1
)
3405 tree index0
= build_component_ref (op0
, index_identifier
, 0, 0);
3407 tree pfn0
= PFN_FROM_PTRMEMFUNC (op0
);
3408 tree delta20
= DELTA2_FROM_PTRMEMFUNC (op0
);
3409 tree delta21
= integer_zero_node
;
3411 tree integer_neg_one_node
3412 = build_binary_op (MINUS_EXPR
, integer_zero_node
, integer_one_node
, 1);
3413 if (TREE_CODE (TREE_OPERAND (op1
, 0)) == FUNCTION_DECL
3414 && DECL_VINDEX (TREE_OPERAND (op1
, 0)))
3416 /* Map everything down one to make room for the null pointer to member. */
3417 index1
= size_binop (PLUS_EXPR
,
3418 DECL_VINDEX (TREE_OPERAND (op1
, 0)),
3420 op1
= integer_zero_node
;
3421 delta21
= CLASSTYPE_VFIELD (TYPE_METHOD_BASETYPE (TREE_TYPE (type1
)));
3422 delta21
= DECL_FIELD_BITPOS (delta21
);
3423 delta21
= size_binop (FLOOR_DIV_EXPR
, delta21
, size_int (BITS_PER_UNIT
));
3426 index1
= integer_neg_one_node
;
3428 tree nop1
= build1 (NOP_EXPR
, TYPE_PTRMEMFUNC_FN_TYPE (type0
), op1
);
3429 TREE_CONSTANT (nop1
) = TREE_CONSTANT (op1
);
3432 e1
= build_binary_op (EQ_EXPR
, index0
, index1
, 1);
3433 e2
= build_binary_op (NE_EXPR
, index1
, integer_neg_one_node
, 1);
3434 e2
= build_binary_op (TRUTH_ANDIF_EXPR
, e2
, build_binary_op (EQ_EXPR
, delta20
, delta21
, 1), 1);
3435 e3
= build_binary_op (EQ_EXPR
, pfn0
, op1
, 1);
3436 e2
= build_binary_op (TRUTH_ORIF_EXPR
, e2
, e3
, 1);
3437 e2
= build_binary_op (TRUTH_ANDIF_EXPR
, e1
, e2
, 1);
3438 if (code
== EQ_EXPR
)
3440 return build_binary_op (EQ_EXPR
, e2
, integer_zero_node
, 1);
3442 else if (TYPE_PTRMEMFUNC_P (type1
)
3443 && TYPE_PTRMEMFUNC_FN_TYPE (type1
) == type0
)
3445 return build_binary_op (code
, op1
, op0
, 1);
3451 if ((code0
== INTEGER_TYPE
|| code0
== REAL_TYPE
)
3452 && (code1
== INTEGER_TYPE
|| code1
== REAL_TYPE
))
3454 else if (code0
== POINTER_TYPE
&& code1
== POINTER_TYPE
)
3456 if (comp_target_types (type0
, type1
, 1))
3457 result_type
= common_type (type0
, type1
);
3460 cp_pedwarn ("comparison of distinct pointer types `%T' and `%T' lacks a cast",
3462 result_type
= ptr_type_node
;
3471 build_type
= boolean_type_node
;
3472 if ((code0
== INTEGER_TYPE
|| code0
== REAL_TYPE
)
3473 && (code1
== INTEGER_TYPE
|| code1
== REAL_TYPE
))
3475 else if (code0
== POINTER_TYPE
&& code1
== POINTER_TYPE
)
3477 if (comp_target_types (type0
, type1
, 1))
3478 result_type
= common_type (type0
, type1
);
3481 cp_pedwarn ("comparison of distinct pointer types `%T' and `%T' lacks a cast",
3483 result_type
= ptr_type_node
;
3486 else if (code0
== POINTER_TYPE
&& TREE_CODE (op1
) == INTEGER_CST
3487 && integer_zerop (op1
))
3488 result_type
= type0
;
3489 else if (code1
== POINTER_TYPE
&& TREE_CODE (op0
) == INTEGER_CST
3490 && integer_zerop (op0
))
3491 result_type
= type1
;
3492 else if (code0
== POINTER_TYPE
&& code1
== INTEGER_TYPE
)
3494 result_type
= type0
;
3496 pedwarn ("ANSI C++ forbids comparison between pointer and integer");
3497 else if (! flag_traditional
)
3498 warning ("comparison between pointer and integer");
3500 else if (code0
== INTEGER_TYPE
&& code1
== POINTER_TYPE
)
3502 result_type
= type1
;
3504 pedwarn ("ANSI C++ forbids comparison between pointer and integer");
3505 else if (! flag_traditional
)
3506 warning ("comparison between pointer and integer");
3511 if ((code0
== INTEGER_TYPE
|| code0
== REAL_TYPE
)
3512 && (code1
== INTEGER_TYPE
|| code1
== REAL_TYPE
))
3514 if (shorten
|| common
|| short_compare
)
3515 result_type
= common_type (type0
, type1
);
3517 /* For certain operations (which identify themselves by shorten != 0)
3518 if both args were extended from the same smaller type,
3519 do the arithmetic in that type and then extend.
3521 shorten !=0 and !=1 indicates a bitwise operation.
3522 For them, this optimization is safe only if
3523 both args are zero-extended or both are sign-extended.
3524 Otherwise, we might change the result.
3525 Eg, (short)-1 | (unsigned short)-1 is (int)-1
3526 but calculated in (unsigned short) it would be (unsigned short)-1. */
3530 int unsigned0
, unsigned1
;
3531 tree arg0
= get_narrower (op0
, &unsigned0
);
3532 tree arg1
= get_narrower (op1
, &unsigned1
);
3533 /* UNS is 1 if the operation to be done is an unsigned one. */
3534 int uns
= TREE_UNSIGNED (result_type
);
3537 final_type
= result_type
;
3539 /* Handle the case that OP0 does not *contain* a conversion
3540 but it *requires* conversion to FINAL_TYPE. */
3542 if (op0
== arg0
&& TREE_TYPE (op0
) != final_type
)
3543 unsigned0
= TREE_UNSIGNED (TREE_TYPE (op0
));
3544 if (op1
== arg1
&& TREE_TYPE (op1
) != final_type
)
3545 unsigned1
= TREE_UNSIGNED (TREE_TYPE (op1
));
3547 /* Now UNSIGNED0 is 1 if ARG0 zero-extends to FINAL_TYPE. */
3549 /* For bitwise operations, signedness of nominal type
3550 does not matter. Consider only how operands were extended. */
3554 /* Note that in all three cases below we refrain from optimizing
3555 an unsigned operation on sign-extended args.
3556 That would not be valid. */
3558 /* Both args variable: if both extended in same way
3559 from same width, do it in that width.
3560 Do it unsigned if args were zero-extended. */
3561 if ((TYPE_PRECISION (TREE_TYPE (arg0
))
3562 < TYPE_PRECISION (result_type
))
3563 && (TYPE_PRECISION (TREE_TYPE (arg1
))
3564 == TYPE_PRECISION (TREE_TYPE (arg0
)))
3565 && unsigned0
== unsigned1
3566 && (unsigned0
|| !uns
))
3568 = signed_or_unsigned_type (unsigned0
,
3569 common_type (TREE_TYPE (arg0
), TREE_TYPE (arg1
)));
3570 else if (TREE_CODE (arg0
) == INTEGER_CST
3571 && (unsigned1
|| !uns
)
3572 && (TYPE_PRECISION (TREE_TYPE (arg1
))
3573 < TYPE_PRECISION (result_type
))
3574 && (type
= signed_or_unsigned_type (unsigned1
,
3576 int_fits_type_p (arg0
, type
)))
3578 else if (TREE_CODE (arg1
) == INTEGER_CST
3579 && (unsigned0
|| !uns
)
3580 && (TYPE_PRECISION (TREE_TYPE (arg0
))
3581 < TYPE_PRECISION (result_type
))
3582 && (type
= signed_or_unsigned_type (unsigned0
,
3584 int_fits_type_p (arg1
, type
)))
3588 /* Shifts can be shortened if shifting right. */
3593 tree arg0
= get_narrower (op0
, &unsigned_arg
);
3595 final_type
= result_type
;
3597 if (arg0
== op0
&& final_type
== TREE_TYPE (op0
))
3598 unsigned_arg
= TREE_UNSIGNED (TREE_TYPE (op0
));
3600 if (TYPE_PRECISION (TREE_TYPE (arg0
)) < TYPE_PRECISION (result_type
)
3601 /* We can shorten only if the shift count is less than the
3602 number of bits in the smaller type size. */
3603 && TREE_INT_CST_HIGH (op1
) == 0
3604 && TYPE_PRECISION (TREE_TYPE (arg0
)) > TREE_INT_CST_LOW (op1
)
3605 /* If arg is sign-extended and then unsigned-shifted,
3606 we can simulate this with a signed shift in arg's type
3607 only if the extended result is at least twice as wide
3608 as the arg. Otherwise, the shift could use up all the
3609 ones made by sign-extension and bring in zeros.
3610 We can't optimize that case at all, but in most machines
3611 it never happens because available widths are 2**N. */
3612 && (!TREE_UNSIGNED (final_type
)
3614 || (((unsigned) 2 * TYPE_PRECISION (TREE_TYPE (arg0
)))
3615 <= TYPE_PRECISION (result_type
))))
3617 /* Do an unsigned shift if the operand was zero-extended. */
3619 = signed_or_unsigned_type (unsigned_arg
,
3621 /* Convert value-to-be-shifted to that type. */
3622 if (TREE_TYPE (op0
) != result_type
)
3623 op0
= convert (result_type
, op0
);
3628 /* Comparison operations are shortened too but differently.
3629 They identify themselves by setting short_compare = 1. */
3633 /* Don't write &op0, etc., because that would prevent op0
3634 from being kept in a register.
3635 Instead, make copies of the our local variables and
3636 pass the copies by reference, then copy them back afterward. */
3637 tree xop0
= op0
, xop1
= op1
, xresult_type
= result_type
;
3638 enum tree_code xresultcode
= resultcode
;
3640 = shorten_compare (&xop0
, &xop1
, &xresult_type
, &xresultcode
);
3642 return convert (boolean_type_node
, val
);
3643 op0
= xop0
, op1
= xop1
;
3645 resultcode
= xresultcode
;
3648 if (short_compare
&& warn_sign_compare
)
3650 int op0_signed
= ! TREE_UNSIGNED (TREE_TYPE (orig_op0
));
3651 int op1_signed
= ! TREE_UNSIGNED (TREE_TYPE (orig_op1
));
3653 int unsignedp0
, unsignedp1
;
3654 tree primop0
= get_narrower (op0
, &unsignedp0
);
3655 tree primop1
= get_narrower (op1
, &unsignedp1
);
3657 /* Check for comparison of different enum types. */
3658 if (flag_int_enum_equivalence
== 0
3659 && TREE_CODE (TREE_TYPE (orig_op0
)) == ENUMERAL_TYPE
3660 && TREE_CODE (TREE_TYPE (orig_op1
)) == ENUMERAL_TYPE
3661 && TYPE_MAIN_VARIANT (TREE_TYPE (orig_op0
))
3662 != TYPE_MAIN_VARIANT (TREE_TYPE (orig_op1
)))
3664 cp_warning ("comparison between `%#T' and `%#T'",
3665 TREE_TYPE (orig_op0
), TREE_TYPE (orig_op1
));
3668 /* Give warnings for comparisons between signed and unsigned
3669 quantities that may fail. */
3670 /* Do the checking based on the original operand trees, so that
3671 casts will be considered, but default promotions won't be. */
3673 /* Do not warn if the comparison is being done in a signed type,
3674 since the signed type will only be chosen if it can represent
3675 all the values of the unsigned type. */
3676 if (! TREE_UNSIGNED (result_type
))
3678 /* Do not warn if both operands are unsigned. */
3679 else if (op0_signed
== op1_signed
)
3681 /* Do not warn if the signed quantity is an unsuffixed
3682 integer literal (or some static constant expression
3683 involving such literals) and it is non-negative. */
3684 else if ((op0_signed
&& TREE_CODE (orig_op0
) == INTEGER_CST
3685 && tree_int_cst_sgn (orig_op0
) >= 0)
3686 || (op1_signed
&& TREE_CODE (orig_op1
) == INTEGER_CST
3687 && tree_int_cst_sgn (orig_op1
) >= 0))
3689 /* Do not warn if the comparison is an equality operation,
3690 the unsigned quantity is an integral constant and it does
3691 not use the most significant bit of result_type. */
3692 else if ((resultcode
== EQ_EXPR
|| resultcode
== NE_EXPR
)
3693 && ((op0_signed
&& TREE_CODE (orig_op1
) == INTEGER_CST
3694 && int_fits_type_p (orig_op1
, signed_type (result_type
))
3695 || (op1_signed
&& TREE_CODE (orig_op0
) == INTEGER_CST
3696 && int_fits_type_p (orig_op0
, signed_type (result_type
))))))
3699 warning ("comparison between signed and unsigned");
3701 /* Warn if two unsigned values are being compared in a size
3702 larger than their original size, and one (and only one) is the
3703 result of a `~' operator. This comparison will always fail.
3705 Also warn if one operand is a constant, and the constant does not
3706 have all bits set that are set in the ~ operand when it is
3709 if ((TREE_CODE (primop0
) == BIT_NOT_EXPR
)
3710 ^ (TREE_CODE (primop1
) == BIT_NOT_EXPR
))
3712 if (TREE_CODE (primop0
) == BIT_NOT_EXPR
)
3713 primop0
= get_narrower (TREE_OPERAND (op0
, 0), &unsignedp0
);
3714 if (TREE_CODE (primop1
) == BIT_NOT_EXPR
)
3715 primop1
= get_narrower (TREE_OPERAND (op1
, 0), &unsignedp1
);
3717 if (TREE_CODE (primop0
) == INTEGER_CST
3718 || TREE_CODE (primop1
) == INTEGER_CST
)
3721 HOST_WIDE_INT constant
, mask
;
3725 if (TREE_CODE (primop0
) == INTEGER_CST
)
3728 unsignedp
= unsignedp1
;
3729 constant
= TREE_INT_CST_LOW (primop0
);
3734 unsignedp
= unsignedp0
;
3735 constant
= TREE_INT_CST_LOW (primop1
);
3738 bits
= TYPE_PRECISION (TREE_TYPE (primop
));
3739 if (bits
< TYPE_PRECISION (result_type
)
3740 && bits
< HOST_BITS_PER_LONG
&& unsignedp
)
3742 mask
= (~ (HOST_WIDE_INT
) 0) << bits
;
3743 if ((mask
& constant
) != mask
)
3744 warning ("comparison of promoted ~unsigned with constant");
3747 else if (unsignedp0
&& unsignedp1
3748 && (TYPE_PRECISION (TREE_TYPE (primop0
))
3749 < TYPE_PRECISION (result_type
))
3750 && (TYPE_PRECISION (TREE_TYPE (primop1
))
3751 < TYPE_PRECISION (result_type
)))
3752 warning ("comparison of promoted ~unsigned with unsigned");
3757 /* At this point, RESULT_TYPE must be nonzero to avoid an error message.
3758 If CONVERTED is zero, both args will be converted to type RESULT_TYPE.
3759 Then the expression will be built.
3760 It will be given type FINAL_TYPE if that is nonzero;
3761 otherwise, it will be given type RESULT_TYPE. */
3765 cp_error ("invalid operands `%T' and `%T' to binary `%O'",
3766 TREE_TYPE (orig_op0
), TREE_TYPE (orig_op1
), error_code
);
3767 return error_mark_node
;
3772 if (TREE_TYPE (op0
) != result_type
)
3773 op0
= convert (result_type
, op0
);
3774 if (TREE_TYPE (op1
) != result_type
)
3775 op1
= convert (result_type
, op1
);
3778 if (build_type
== NULL_TREE
)
3779 build_type
= result_type
;
3782 register tree result
= build (resultcode
, build_type
, op0
, op1
);
3783 register tree folded
;
3785 folded
= fold (result
);
3786 if (folded
== result
)
3787 TREE_CONSTANT (folded
) = TREE_CONSTANT (op0
) & TREE_CONSTANT (op1
);
3788 if (final_type
!= 0)
3789 return convert (final_type
, folded
);
3794 /* Return a tree for the sum or difference (RESULTCODE says which)
3795 of pointer PTROP and integer INTOP. */
3798 pointer_int_sum (resultcode
, ptrop
, intop
)
3799 enum tree_code resultcode
;
3800 register tree ptrop
, intop
;
3804 register tree result
;
3805 register tree folded
= fold (intop
);
3807 /* The result is a pointer of the same type that is being added. */
3809 register tree result_type
= TREE_TYPE (ptrop
);
3811 if (TREE_CODE (TREE_TYPE (result_type
)) == VOID_TYPE
)
3813 if (pedantic
|| warn_pointer_arith
)
3814 pedwarn ("ANSI C++ forbids using pointer of type `void *' in arithmetic");
3815 size_exp
= integer_one_node
;
3817 else if (TREE_CODE (TREE_TYPE (result_type
)) == FUNCTION_TYPE
)
3819 if (pedantic
|| warn_pointer_arith
)
3820 pedwarn ("ANSI C++ forbids using pointer to a function in arithmetic");
3821 size_exp
= integer_one_node
;
3823 else if (TREE_CODE (TREE_TYPE (result_type
)) == METHOD_TYPE
)
3825 if (pedantic
|| warn_pointer_arith
)
3826 pedwarn ("ANSI C++ forbids using pointer to a method in arithmetic");
3827 size_exp
= integer_one_node
;
3829 else if (TREE_CODE (TREE_TYPE (result_type
)) == OFFSET_TYPE
)
3831 if (pedantic
|| warn_pointer_arith
)
3832 pedwarn ("ANSI C++ forbids using pointer to a member in arithmetic");
3833 size_exp
= integer_one_node
;
3836 size_exp
= size_in_bytes (complete_type (TREE_TYPE (result_type
)));
3838 /* Needed to make OOPS V2R3 work. */
3840 if (TREE_CODE (intop
) == INTEGER_CST
3841 && TREE_INT_CST_LOW (intop
) == 0
3842 && TREE_INT_CST_HIGH (intop
) == 0)
3845 /* If what we are about to multiply by the size of the elements
3846 contains a constant term, apply distributive law
3847 and multiply that constant term separately.
3848 This helps produce common subexpressions. */
3850 if ((TREE_CODE (intop
) == PLUS_EXPR
|| TREE_CODE (intop
) == MINUS_EXPR
)
3851 && ! TREE_CONSTANT (intop
)
3852 && TREE_CONSTANT (TREE_OPERAND (intop
, 1))
3853 && TREE_CONSTANT (size_exp
))
3855 enum tree_code subcode
= resultcode
;
3856 if (TREE_CODE (intop
) == MINUS_EXPR
)
3857 subcode
= (subcode
== PLUS_EXPR
? MINUS_EXPR
: PLUS_EXPR
);
3858 ptrop
= build_binary_op (subcode
, ptrop
, TREE_OPERAND (intop
, 1), 1);
3859 intop
= TREE_OPERAND (intop
, 0);
3862 /* Convert the integer argument to a type the same size as sizetype
3863 so the multiply won't overflow spuriously. */
3865 if (TYPE_PRECISION (TREE_TYPE (intop
)) != TYPE_PRECISION (sizetype
))
3866 intop
= convert (type_for_size (TYPE_PRECISION (sizetype
), 0), intop
);
3868 /* Replace the integer argument with a suitable product by the object size.
3869 Do this multiplication as signed, then convert to the appropriate
3870 pointer type (actually unsigned integral). */
3872 intop
= convert (result_type
,
3873 build_binary_op (MULT_EXPR
, intop
,
3874 convert (TREE_TYPE (intop
), size_exp
), 1));
3876 /* Create the sum or difference. */
3878 result
= build (resultcode
, result_type
, ptrop
, intop
);
3880 folded
= fold (result
);
3881 if (folded
== result
)
3882 TREE_CONSTANT (folded
) = TREE_CONSTANT (ptrop
) & TREE_CONSTANT (intop
);
3886 /* Return a tree for the difference of pointers OP0 and OP1.
3887 The resulting tree has type int. */
3890 pointer_diff (op0
, op1
)
3891 register tree op0
, op1
;
3893 register tree result
, folded
;
3894 tree restype
= ptrdiff_type_node
;
3895 tree target_type
= TREE_TYPE (TREE_TYPE (op0
));
3897 if (pedantic
|| warn_pointer_arith
)
3899 if (TREE_CODE (target_type
) == VOID_TYPE
)
3900 pedwarn ("ANSI C++ forbids using pointer of type `void *' in subtraction");
3901 if (TREE_CODE (target_type
) == FUNCTION_TYPE
)
3902 pedwarn ("ANSI C++ forbids using pointer to a function in subtraction");
3903 if (TREE_CODE (target_type
) == METHOD_TYPE
)
3904 pedwarn ("ANSI C++ forbids using pointer to a method in subtraction");
3905 if (TREE_CODE (target_type
) == OFFSET_TYPE
)
3906 pedwarn ("ANSI C++ forbids using pointer to a member in subtraction");
3909 /* First do the subtraction as integers;
3910 then drop through to build the divide operator. */
3912 op0
= build_binary_op (MINUS_EXPR
,
3913 convert (restype
, op0
), convert (restype
, op1
), 1);
3915 /* This generates an error if op1 is a pointer to an incomplete type. */
3916 if (TYPE_SIZE (TREE_TYPE (TREE_TYPE (op1
))) == 0)
3917 error ("arithmetic on pointer to an incomplete type");
3919 op1
= ((TREE_CODE (target_type
) == VOID_TYPE
3920 || TREE_CODE (target_type
) == FUNCTION_TYPE
3921 || TREE_CODE (target_type
) == METHOD_TYPE
3922 || TREE_CODE (target_type
) == OFFSET_TYPE
)
3924 : size_in_bytes (target_type
));
3926 /* Do the division. */
3928 result
= build (EXACT_DIV_EXPR
, restype
, op0
, convert (restype
, op1
));
3930 folded
= fold (result
);
3931 if (folded
== result
)
3932 TREE_CONSTANT (folded
) = TREE_CONSTANT (op0
) & TREE_CONSTANT (op1
);
3936 /* Handle the case of taking the address of a COMPONENT_REF.
3937 Called by `build_unary_op' and `build_up_reference'.
3939 ARG is the COMPONENT_REF whose address we want.
3940 ARGTYPE is the pointer type that this address should have.
3941 MSG is an error message to print if this COMPONENT_REF is not
3942 addressable (such as a bitfield). */
3945 build_component_addr (arg
, argtype
, msg
)
3949 tree field
= TREE_OPERAND (arg
, 1);
3950 tree basetype
= decl_type_context (field
);
3951 tree rval
= build_unary_op (ADDR_EXPR
, TREE_OPERAND (arg
, 0), 0);
3953 if (DECL_BIT_FIELD (field
))
3955 error (msg
, IDENTIFIER_POINTER (DECL_NAME (field
)));
3956 return error_mark_node
;
3959 if (TREE_CODE (field
) == FIELD_DECL
3960 && TYPE_USES_COMPLEX_INHERITANCE (basetype
))
3962 /* Can't convert directly to ARGTYPE, since that
3963 may have the same pointer type as one of our
3965 rval
= build1 (NOP_EXPR
, argtype
,
3966 convert_pointer_to (basetype
, rval
));
3967 TREE_CONSTANT (rval
) = TREE_CONSTANT (TREE_OPERAND (rval
, 0));
3970 /* This conversion is harmless. */
3971 rval
= convert_force (argtype
, rval
, 0);
3973 if (! integer_zerop (DECL_FIELD_BITPOS (field
)))
3975 tree offset
= size_binop (EASY_DIV_EXPR
, DECL_FIELD_BITPOS (field
),
3976 size_int (BITS_PER_UNIT
));
3977 int flag
= TREE_CONSTANT (rval
);
3978 rval
= fold (build (PLUS_EXPR
, argtype
,
3979 rval
, convert (argtype
, offset
)));
3980 TREE_CONSTANT (rval
) = flag
;
3985 /* Construct and perhaps optimize a tree representation
3986 for a unary operation. CODE, a tree_code, specifies the operation
3987 and XARG is the operand. */
3990 build_x_unary_op (code
, xarg
)
3991 enum tree_code code
;
3994 if (current_template_parms
)
3995 return build_min_nt (code
, xarg
, NULL_TREE
);
3997 /* & rec, on incomplete RECORD_TYPEs is the simple opr &, not an
3999 if (code
== ADDR_EXPR
4000 && ((IS_AGGR_TYPE_CODE (TREE_CODE (TREE_TYPE (xarg
)))
4001 && TYPE_SIZE (TREE_TYPE (xarg
)) == NULL_TREE
)
4002 || (TREE_CODE (xarg
) == OFFSET_REF
)))
4003 /* don't look for a function */;
4006 tree rval
= build_opfncall (code
, LOOKUP_SPECULATIVELY
, xarg
,
4007 NULL_TREE
, NULL_TREE
);
4009 return build_opfncall (code
, LOOKUP_NORMAL
, xarg
,
4010 NULL_TREE
, NULL_TREE
);
4013 if (code
== ADDR_EXPR
)
4015 if (TREE_CODE (xarg
) == TARGET_EXPR
)
4016 warning ("taking address of temporary");
4019 return build_unary_op (code
, xarg
, 0);
4022 /* Just like truthvalue_conversion, but we want a CLEANUP_POINT_EXPR. */
4025 condition_conversion (expr
)
4029 if (current_template_parms
)
4031 t
= convert (boolean_type_node
, expr
);
4032 t
= fold (build1 (CLEANUP_POINT_EXPR
, boolean_type_node
, t
));
4036 /* C++: Must handle pointers to members.
4038 Perhaps type instantiation should be extended to handle conversion
4039 from aggregates to types we don't yet know we want? (Or are those
4040 cases typically errors which should be reported?)
4042 NOCONVERT nonzero suppresses the default promotions
4043 (such as from short to int). */
4045 build_unary_op (code
, xarg
, noconvert
)
4046 enum tree_code code
;
4050 /* No default_conversion here. It causes trouble for ADDR_EXPR. */
4051 register tree arg
= xarg
;
4052 register tree argtype
= 0;
4053 char *errstring
= NULL
;
4056 if (arg
== error_mark_node
)
4057 return error_mark_node
;
4062 /* This is used for unary plus, because a CONVERT_EXPR
4063 is enough to prevent anybody from looking inside for
4064 associativity, but won't generate any code. */
4065 if (!(arg
= build_expr_type_conversion
4066 (WANT_ARITH
| WANT_ENUM
| WANT_POINTER
, arg
, 1)))
4067 errstring
= "wrong type argument to unary plus";
4071 arg
= default_conversion (arg
);
4072 arg
= build1 (NON_LVALUE_EXPR
, TREE_TYPE (arg
), arg
);
4077 if (!(arg
= build_expr_type_conversion (WANT_ARITH
| WANT_ENUM
, arg
, 1)))
4078 errstring
= "wrong type argument to unary minus";
4079 else if (!noconvert
)
4080 arg
= default_conversion (arg
);
4084 if (!(arg
= build_expr_type_conversion (WANT_INT
| WANT_ENUM
, arg
, 1)))
4085 errstring
= "wrong type argument to bit-complement";
4086 else if (!noconvert
)
4087 arg
= default_conversion (arg
);
4091 if (!(arg
= build_expr_type_conversion (WANT_ARITH
| WANT_ENUM
, arg
, 1)))
4092 errstring
= "wrong type argument to abs";
4093 else if (!noconvert
)
4094 arg
= default_conversion (arg
);
4097 case TRUTH_NOT_EXPR
:
4098 arg
= convert (boolean_type_node
, arg
);
4099 val
= invert_truthvalue (arg
);
4100 if (arg
!= error_mark_node
)
4102 errstring
= "in argument to unary !";
4108 case PREINCREMENT_EXPR
:
4109 case POSTINCREMENT_EXPR
:
4110 case PREDECREMENT_EXPR
:
4111 case POSTDECREMENT_EXPR
:
4112 /* Handle complex lvalues (when permitted)
4113 by reduction to simpler cases. */
4115 val
= unary_complex_lvalue (code
, arg
);
4119 /* Report invalid types. */
4121 if (!(arg
= build_expr_type_conversion (WANT_ARITH
| WANT_POINTER
,
4124 if (code
== PREINCREMENT_EXPR
)
4125 errstring
="no pre-increment operator for type";
4126 else if (code
== POSTINCREMENT_EXPR
)
4127 errstring
="no post-increment operator for type";
4128 else if (code
== PREDECREMENT_EXPR
)
4129 errstring
="no pre-decrement operator for type";
4131 errstring
="no post-decrement operator for type";
4135 /* Report something read-only. */
4137 if (TYPE_READONLY (TREE_TYPE (arg
))
4138 || TREE_READONLY (arg
))
4139 readonly_error (arg
, ((code
== PREINCREMENT_EXPR
4140 || code
== POSTINCREMENT_EXPR
)
4141 ? "increment" : "decrement"),
4146 tree result_type
= TREE_TYPE (arg
);
4148 arg
= get_unwidened (arg
, 0);
4149 argtype
= TREE_TYPE (arg
);
4151 /* ARM $5.2.5 last annotation says this should be forbidden. */
4152 if (TREE_CODE (argtype
) == ENUMERAL_TYPE
)
4153 pedwarn ("ANSI C++ forbids %sing an enum",
4154 (code
== PREINCREMENT_EXPR
|| code
== POSTINCREMENT_EXPR
)
4155 ? "increment" : "decrement");
4157 /* Compute the increment. */
4159 if (TREE_CODE (argtype
) == POINTER_TYPE
)
4161 enum tree_code tmp
= TREE_CODE (TREE_TYPE (argtype
));
4162 if (TYPE_SIZE (complete_type (TREE_TYPE (argtype
))) == 0)
4163 cp_error ("cannot %s a pointer to incomplete type `%T'",
4164 ((code
== PREINCREMENT_EXPR
4165 || code
== POSTINCREMENT_EXPR
)
4166 ? "increment" : "decrement"), TREE_TYPE (argtype
));
4167 else if (tmp
== FUNCTION_TYPE
|| tmp
== METHOD_TYPE
4168 || tmp
== VOID_TYPE
|| tmp
== OFFSET_TYPE
)
4169 cp_pedwarn ("ANSI C++ forbids %sing a pointer of type `%T'",
4170 ((code
== PREINCREMENT_EXPR
4171 || code
== POSTINCREMENT_EXPR
)
4172 ? "increment" : "decrement"), argtype
);
4173 inc
= c_sizeof_nowarn (TREE_TYPE (argtype
));
4176 inc
= integer_one_node
;
4178 inc
= convert (argtype
, inc
);
4180 /* Handle incrementing a cast-expression. */
4182 switch (TREE_CODE (arg
))
4187 case FIX_TRUNC_EXPR
:
4188 case FIX_FLOOR_EXPR
:
4189 case FIX_ROUND_EXPR
:
4192 tree incremented
, modify
, value
, compound
;
4193 if (! lvalue_p (arg
) && pedantic
)
4194 pedwarn ("cast to non-reference type used as lvalue");
4195 arg
= stabilize_reference (arg
);
4196 if (code
== PREINCREMENT_EXPR
|| code
== PREDECREMENT_EXPR
)
4199 value
= save_expr (arg
);
4200 incremented
= build (((code
== PREINCREMENT_EXPR
4201 || code
== POSTINCREMENT_EXPR
)
4202 ? PLUS_EXPR
: MINUS_EXPR
),
4203 argtype
, value
, inc
);
4204 TREE_SIDE_EFFECTS (incremented
) = 1;
4206 modify
= build_modify_expr (arg
, NOP_EXPR
, incremented
);
4207 compound
= build (COMPOUND_EXPR
, TREE_TYPE (arg
), modify
, value
);
4209 /* Eliminate warning about unused result of + or -. */
4210 TREE_NO_UNUSED_WARNING (compound
) = 1;
4215 /* Complain about anything else that is not a true lvalue. */
4216 if (!lvalue_or_else (arg
, ((code
== PREINCREMENT_EXPR
4217 || code
== POSTINCREMENT_EXPR
)
4218 ? "increment" : "decrement")))
4219 return error_mark_node
;
4221 /* Forbid using -- on `bool'. */
4222 if (TREE_TYPE (arg
) == boolean_type_node
)
4224 if (code
== POSTDECREMENT_EXPR
|| code
== PREDECREMENT_EXPR
)
4226 cp_error ("invalid use of `--' on bool variable `%D'", arg
);
4227 return error_mark_node
;
4230 /* This will only work if someone can convince Kenner to accept
4231 my patch to expand_increment. (jason) */
4232 val
= build (code
, TREE_TYPE (arg
), arg
, inc
);
4234 if (code
== POSTINCREMENT_EXPR
)
4236 arg
= stabilize_reference (arg
);
4237 val
= build (MODIFY_EXPR
, TREE_TYPE (arg
), arg
,
4239 TREE_SIDE_EFFECTS (val
) = 1;
4240 arg
= save_expr (arg
);
4241 val
= build (COMPOUND_EXPR
, TREE_TYPE (arg
), val
, arg
);
4242 val
= build (COMPOUND_EXPR
, TREE_TYPE (arg
), arg
, val
);
4245 val
= build (MODIFY_EXPR
, TREE_TYPE (arg
), arg
,
4250 val
= build (code
, TREE_TYPE (arg
), arg
, inc
);
4252 TREE_SIDE_EFFECTS (val
) = 1;
4253 return convert (result_type
, val
);
4257 /* Note that this operation never does default_conversion
4258 regardless of NOCONVERT. */
4260 argtype
= TREE_TYPE (arg
);
4261 if (TREE_CODE (argtype
) == REFERENCE_TYPE
)
4263 arg
= build1 (CONVERT_EXPR
, build_pointer_type (TREE_TYPE (TREE_TYPE (arg
))), arg
);
4264 TREE_REFERENCE_EXPR (arg
) = 1;
4268 && TREE_CODE (arg
) == FUNCTION_DECL
4270 && DECL_CONTEXT (arg
) == NULL_TREE
4271 && IDENTIFIER_LENGTH (DECL_NAME (arg
)) == 4
4272 && IDENTIFIER_POINTER (DECL_NAME (arg
))[0] == 'm'
4273 && ! strcmp (IDENTIFIER_POINTER (DECL_NAME (arg
)), "main"))
4275 pedwarn ("taking address of function `main'");
4277 /* Let &* cancel out to simplify resulting code. */
4278 if (TREE_CODE (arg
) == INDIRECT_REF
)
4280 /* We don't need to have `current_class_decl' wrapped in a
4281 NON_LVALUE_EXPR node. */
4283 return current_class_decl
;
4285 /* Keep `default_conversion' from converting if
4286 ARG is of REFERENCE_TYPE. */
4287 arg
= TREE_OPERAND (arg
, 0);
4288 if (TREE_CODE (TREE_TYPE (arg
)) == REFERENCE_TYPE
)
4290 if (TREE_CODE (arg
) == VAR_DECL
&& DECL_INITIAL (arg
)
4291 && !TREE_SIDE_EFFECTS (DECL_INITIAL (arg
)))
4292 arg
= DECL_INITIAL (arg
);
4293 arg
= build1 (CONVERT_EXPR
, build_pointer_type (TREE_TYPE (TREE_TYPE (arg
))), arg
);
4294 TREE_REFERENCE_EXPR (arg
) = 1;
4295 TREE_CONSTANT (arg
) = TREE_CONSTANT (TREE_OPERAND (arg
, 0));
4297 else if (lvalue_p (arg
))
4298 /* Don't let this be an lvalue. */
4299 return non_lvalue (arg
);
4303 /* For &x[y], return x+y */
4304 if (TREE_CODE (arg
) == ARRAY_REF
)
4306 if (mark_addressable (TREE_OPERAND (arg
, 0)) == 0)
4307 return error_mark_node
;
4308 return build_binary_op (PLUS_EXPR
, TREE_OPERAND (arg
, 0),
4309 TREE_OPERAND (arg
, 1), 1);
4312 /* Uninstantiated types are all functions. Taking the
4313 address of a function is a no-op, so just return the
4316 if (TREE_CODE (arg
) == IDENTIFIER_NODE
4317 && IDENTIFIER_OPNAME_P (arg
))
4319 my_friendly_abort (117);
4320 /* We don't know the type yet, so just work around the problem.
4321 We know that this will resolve to an lvalue. */
4322 return build1 (ADDR_EXPR
, unknown_type_node
, arg
);
4325 if (TREE_CODE (arg
) == TREE_LIST
)
4327 if (TREE_CODE (TREE_VALUE (arg
)) == FUNCTION_DECL
4328 && DECL_CHAIN (TREE_VALUE (arg
)) == NULL_TREE
)
4329 /* Unique overloaded non-member function. */
4330 return build_unary_op (ADDR_EXPR
, TREE_VALUE (arg
), 0);
4331 if (TREE_CHAIN (arg
) == NULL_TREE
4332 && TREE_CODE (TREE_VALUE (arg
)) == TREE_LIST
4333 && DECL_CHAIN (TREE_VALUE (TREE_VALUE (arg
))) == NULL_TREE
)
4334 /* Unique overloaded member function. */
4335 return build_unary_op (ADDR_EXPR
, TREE_VALUE (TREE_VALUE (arg
)),
4337 return build1 (ADDR_EXPR
, unknown_type_node
, arg
);
4340 /* Handle complex lvalues (when permitted)
4341 by reduction to simpler cases. */
4342 val
= unary_complex_lvalue (code
, arg
);
4346 switch (TREE_CODE (arg
))
4351 case FIX_TRUNC_EXPR
:
4352 case FIX_FLOOR_EXPR
:
4353 case FIX_ROUND_EXPR
:
4355 if (! lvalue_p (arg
) && pedantic
)
4356 pedwarn ("taking the address of a cast to non-reference type");
4359 /* Allow the address of a constructor if all the elements
4361 if (TREE_CODE (arg
) == CONSTRUCTOR
&& TREE_CONSTANT (arg
))
4363 /* Anything not already handled and not a true memory reference
4365 else if (TREE_CODE (argtype
) != FUNCTION_TYPE
4366 && TREE_CODE (argtype
) != METHOD_TYPE
4367 && !lvalue_or_else (arg
, "unary `&'"))
4368 return error_mark_node
;
4370 /* Ordinary case; arg is a COMPONENT_REF or a decl. */
4371 /* If the lvalue is const or volatile,
4372 merge that into the type that the address will point to. */
4373 if (TREE_CODE_CLASS (TREE_CODE (arg
)) == 'd'
4374 || TREE_CODE_CLASS (TREE_CODE (arg
)) == 'r')
4376 if (TREE_READONLY (arg
) || TREE_THIS_VOLATILE (arg
))
4377 argtype
= cp_build_type_variant (argtype
,
4378 TREE_READONLY (arg
),
4379 TREE_THIS_VOLATILE (arg
));
4382 argtype
= build_pointer_type (argtype
);
4384 if (mark_addressable (arg
) == 0)
4385 return error_mark_node
;
4390 if (TREE_CODE (arg
) == COMPONENT_REF
)
4391 addr
= build_component_addr (arg
, argtype
,
4392 "attempt to take address of bit-field structure member `%s'");
4394 addr
= build1 (code
, argtype
, arg
);
4396 /* Address of a static or external variable or
4397 function counts as a constant */
4399 TREE_CONSTANT (addr
) = 1;
4407 argtype
= TREE_TYPE (arg
);
4408 return fold (build1 (code
, argtype
, arg
));
4412 return error_mark_node
;
4416 /* If CONVERSIONS is a conversion expression or a nested sequence of such,
4417 convert ARG with the same conversions in the same order
4418 and return the result. */
4421 convert_sequence (conversions
, arg
)
4425 switch (TREE_CODE (conversions
))
4430 case FIX_TRUNC_EXPR
:
4431 case FIX_FLOOR_EXPR
:
4432 case FIX_ROUND_EXPR
:
4434 return convert (TREE_TYPE (conversions
),
4435 convert_sequence (TREE_OPERAND (conversions
, 0),
4444 /* Apply unary lvalue-demanding operator CODE to the expression ARG
4445 for certain kinds of expressions which are not really lvalues
4446 but which we can accept as lvalues.
4448 If ARG is not a kind of expression we can handle, return zero. */
4451 unary_complex_lvalue (code
, arg
)
4452 enum tree_code code
;
4455 /* Handle (a, b) used as an "lvalue". */
4456 if (TREE_CODE (arg
) == COMPOUND_EXPR
)
4458 tree real_result
= build_unary_op (code
, TREE_OPERAND (arg
, 1), 0);
4459 return build (COMPOUND_EXPR
, TREE_TYPE (real_result
),
4460 TREE_OPERAND (arg
, 0), real_result
);
4463 /* Handle (a ? b : c) used as an "lvalue". */
4464 if (TREE_CODE (arg
) == COND_EXPR
)
4465 return rationalize_conditional_expr (code
, arg
);
4467 if (TREE_CODE (arg
) == MODIFY_EXPR
4468 || TREE_CODE (arg
) == PREINCREMENT_EXPR
4469 || TREE_CODE (arg
) == PREDECREMENT_EXPR
)
4470 return unary_complex_lvalue
4471 (code
, build (COMPOUND_EXPR
, TREE_TYPE (TREE_OPERAND (arg
, 0)),
4472 arg
, TREE_OPERAND (arg
, 0)));
4474 if (code
!= ADDR_EXPR
)
4477 /* Handle (a = b) used as an "lvalue" for `&'. */
4478 if (TREE_CODE (arg
) == MODIFY_EXPR
4479 || TREE_CODE (arg
) == INIT_EXPR
)
4481 tree real_result
= build_unary_op (code
, TREE_OPERAND (arg
, 0), 0);
4482 return build (COMPOUND_EXPR
, TREE_TYPE (real_result
), arg
, real_result
);
4485 if (TREE_CODE (TREE_TYPE (arg
)) == FUNCTION_TYPE
4486 || TREE_CODE (TREE_TYPE (arg
)) == METHOD_TYPE
4487 || TREE_CODE (TREE_TYPE (arg
)) == OFFSET_TYPE
)
4489 /* The representation of something of type OFFSET_TYPE
4490 is really the representation of a pointer to it.
4491 Here give the representation its true type. */
4495 my_friendly_assert (TREE_CODE (arg
) != SCOPE_REF
, 313);
4497 if (TREE_CODE (arg
) != OFFSET_REF
)
4500 t
= TREE_OPERAND (arg
, 1);
4502 if (TREE_CODE (t
) == FUNCTION_DECL
) /* Check all this code for right semantics. */
4503 return build_unary_op (ADDR_EXPR
, t
, 0);
4504 if (TREE_CODE (t
) == VAR_DECL
)
4505 return build_unary_op (ADDR_EXPR
, t
, 0);
4508 if (TREE_OPERAND (arg
, 0)
4509 && (TREE_CODE (TREE_OPERAND (arg
, 0)) != NOP_EXPR
4510 || TREE_OPERAND (TREE_OPERAND (arg
, 0), 0) != error_mark_node
))
4511 if (TREE_CODE (t
) != FIELD_DECL
)
4513 /* Don't know if this should return address to just
4514 _DECL, or actual address resolved in this expression. */
4515 sorry ("address of bound pointer-to-member expression");
4516 return error_mark_node
;
4519 /* Add in the offset to the right subobject. */
4520 offset
= get_delta_difference (DECL_FIELD_CONTEXT (t
),
4521 TREE_TYPE (TREE_OPERAND (arg
, 0)),
4524 /* Add in the offset to the field. */
4525 offset
= size_binop (PLUS_EXPR
, offset
,
4526 size_binop (EASY_DIV_EXPR
,
4527 DECL_FIELD_BITPOS (t
),
4528 size_int (BITS_PER_UNIT
)));
4530 /* We offset all pointer to data memebers by 1 so that we can
4531 distinguish between a null pointer to data member and the first
4532 data member of a structure. */
4533 offset
= size_binop (PLUS_EXPR
, offset
, size_int (1));
4535 return convert (build_pointer_type (TREE_TYPE (arg
)), offset
);
4541 /* This seems to be obsolete now (and posssibly wrong, compare with
4542 resolve_offset_ref). */
4543 if (TREE_CODE (arg
) == OFFSET_REF
)
4545 tree left
= TREE_OPERAND (arg
, 0), left_addr
;
4546 tree right_addr
= build_unary_op (ADDR_EXPR
, TREE_OPERAND (arg
, 1), 0);
4549 if (current_class_decl
)
4550 left_addr
= current_class_decl
;
4553 error ("no `this' for pointer to member");
4554 return error_mark_node
;
4557 left_addr
= build_unary_op (ADDR_EXPR
, left
, 0);
4559 return build (PLUS_EXPR
, build_pointer_type (TREE_TYPE (arg
)),
4560 build1 (NOP_EXPR
, integer_type_node
, left_addr
),
4561 build1 (NOP_EXPR
, integer_type_node
, right_addr
));
4565 /* We permit compiler to make function calls returning
4566 objects of aggregate type look like lvalues. */
4570 if (TREE_CODE (targ
) == SAVE_EXPR
)
4571 targ
= TREE_OPERAND (targ
, 0);
4573 if (TREE_CODE (targ
) == CALL_EXPR
&& IS_AGGR_TYPE (TREE_TYPE (targ
)))
4575 if (TREE_CODE (arg
) == SAVE_EXPR
)
4578 targ
= build_cplus_new (TREE_TYPE (arg
), arg
);
4579 return build1 (ADDR_EXPR
, build_pointer_type (TREE_TYPE (arg
)), targ
);
4582 if (TREE_CODE (arg
) == SAVE_EXPR
&& TREE_CODE (targ
) == INDIRECT_REF
)
4583 return build (SAVE_EXPR
, build_pointer_type (TREE_TYPE (arg
)),
4584 TREE_OPERAND (targ
, 0), current_function_decl
, NULL
);
4587 /* Don't let anything else be handled specially. */
4591 /* Mark EXP saying that we need to be able to take the
4592 address of it; it should not be allocated in a register.
4593 Value is 1 if successful.
4595 C++: we do not allow `current_class_decl' to be addressable. */
4598 mark_addressable (exp
)
4601 register tree x
= exp
;
4603 if (TREE_ADDRESSABLE (x
) == 1)
4607 switch (TREE_CODE (x
))
4612 x
= TREE_OPERAND (x
, 0);
4616 if (x
== current_class_decl
)
4618 error ("address of `this' not available");
4619 TREE_ADDRESSABLE (x
) = 1; /* so compiler doesn't die later */
4620 put_var_into_stack (x
);
4624 if (TREE_STATIC (x
) && TREE_READONLY (x
)
4625 && DECL_RTL (x
) != 0
4626 && ! DECL_IN_MEMORY_P (x
))
4628 /* We thought this would make a good constant variable,
4629 but we were wrong. */
4630 push_obstacks_nochange ();
4631 end_temporary_allocation ();
4633 TREE_ASM_WRITTEN (x
) = 0;
4635 rest_of_decl_compilation (x
, 0, IDENTIFIER_LOCAL_VALUE (x
) == 0, 0);
4636 TREE_ADDRESSABLE (x
) = 1;
4642 /* Caller should not be trying to mark initialized
4643 constant fields addressable. */
4644 my_friendly_assert (DECL_LANG_SPECIFIC (x
) == 0
4645 || DECL_IN_AGGR_P (x
) == 0
4647 || DECL_EXTERNAL (x
), 314);
4651 /* For C++, we don't warn about taking the address of a register
4652 variable for CONST_DECLs; ARM p97 explicitly says it's okay. */
4653 put_var_into_stack (x
);
4654 TREE_ADDRESSABLE (x
) = 1;
4658 /* We have to test both conditions here. The first may
4659 be non-zero in the case of processing a default function.
4660 The second may be non-zero in the case of a template function. */
4661 x
= DECL_MAIN_VARIANT (x
);
4662 if ((DECL_THIS_INLINE (x
) || DECL_PENDING_INLINE_INFO (x
))
4663 && (DECL_CONTEXT (x
) == NULL_TREE
4664 || TREE_CODE_CLASS (TREE_CODE (DECL_CONTEXT (x
))) != 't'
4665 || ! CLASSTYPE_INTERFACE_ONLY (DECL_CONTEXT (x
))))
4667 mark_inline_for_output (x
);
4668 if (x
== current_function_decl
)
4669 DECL_EXTERNAL (x
) = 0;
4671 TREE_ADDRESSABLE (x
) = 1;
4673 TREE_ADDRESSABLE (DECL_ASSEMBLER_NAME (x
)) = 1;
4681 /* Build and return a conditional expression IFEXP ? OP1 : OP2. */
4684 build_x_conditional_expr (ifexp
, op1
, op2
)
4685 tree ifexp
, op1
, op2
;
4687 tree rval
= NULL_TREE
;
4689 if (current_template_parms
)
4690 return build_min_nt (COND_EXPR
, ifexp
, op1
, op2
);
4692 /* See comments in `build_x_binary_op'. */
4694 rval
= build_opfncall (COND_EXPR
, LOOKUP_SPECULATIVELY
, ifexp
, op1
, op2
);
4696 return build_opfncall (COND_EXPR
, LOOKUP_NORMAL
, ifexp
, op1
, op2
);
4698 return build_conditional_expr (ifexp
, op1
, op2
);
4702 build_conditional_expr (ifexp
, op1
, op2
)
4703 tree ifexp
, op1
, op2
;
4705 register tree type1
;
4706 register tree type2
;
4707 register enum tree_code code1
;
4708 register enum tree_code code2
;
4709 register tree result_type
= NULL_TREE
;
4710 tree orig_op1
= op1
, orig_op2
= op2
;
4712 /* If second operand is omitted, it is the same as the first one;
4713 make sure it is calculated only once. */
4717 pedwarn ("ANSI C++ forbids omitting the middle term of a ?: expression");
4718 ifexp
= op1
= save_expr (ifexp
);
4721 ifexp
= convert (boolean_type_node
, ifexp
);
4723 if (TREE_CODE (ifexp
) == ERROR_MARK
)
4724 return error_mark_node
;
4726 op1
= require_instantiated_type (TREE_TYPE (op2
), op1
, error_mark_node
);
4727 if (op1
== error_mark_node
)
4728 return error_mark_node
;
4729 op2
= require_instantiated_type (TREE_TYPE (op1
), op2
, error_mark_node
);
4730 if (op2
== error_mark_node
)
4731 return error_mark_node
;
4733 /* C++: REFERENCE_TYPES must be dereferenced. */
4734 type1
= TREE_TYPE (op1
);
4735 code1
= TREE_CODE (type1
);
4736 type2
= TREE_TYPE (op2
);
4737 code2
= TREE_CODE (type2
);
4739 if (code1
== REFERENCE_TYPE
)
4741 op1
= convert_from_reference (op1
);
4742 type1
= TREE_TYPE (op1
);
4743 code1
= TREE_CODE (type1
);
4745 if (code2
== REFERENCE_TYPE
)
4747 op2
= convert_from_reference (op2
);
4748 type2
= TREE_TYPE (op2
);
4749 code2
= TREE_CODE (type2
);
4752 /* Don't promote the operands separately if they promote
4753 the same way. Return the unpromoted type and let the combined
4754 value get promoted if necessary. */
4756 if (TYPE_MAIN_VARIANT (type1
) == TYPE_MAIN_VARIANT (type2
)
4757 && code2
!= ARRAY_TYPE
4758 && code2
!= FUNCTION_TYPE
4759 && code2
!= METHOD_TYPE
)
4763 if (TREE_CONSTANT (ifexp
)
4764 && (TREE_CODE (ifexp
) == INTEGER_CST
4765 || TREE_CODE (ifexp
) == ADDR_EXPR
))
4766 return (integer_zerop (ifexp
) ? op2
: op1
);
4768 if (TREE_CODE (op1
) == CONST_DECL
)
4769 op1
= DECL_INITIAL (op1
);
4770 else if (TREE_READONLY_DECL_P (op1
))
4771 op1
= decl_constant_value (op1
);
4772 if (TREE_CODE (op2
) == CONST_DECL
)
4773 op2
= DECL_INITIAL (op2
);
4774 else if (TREE_READONLY_DECL_P (op2
))
4775 op2
= decl_constant_value (op2
);
4777 type1
= cp_build_type_variant
4779 TREE_READONLY (op1
) || TREE_READONLY (op2
),
4780 TREE_THIS_VOLATILE (op1
) || TREE_THIS_VOLATILE (op2
));
4781 /* ??? This is a kludge to deal with the fact that
4782 we don't sort out integers and enums properly, yet. */
4783 result
= fold (build (COND_EXPR
, type1
, ifexp
, op1
, op2
));
4784 if (TREE_TYPE (result
) != type1
)
4785 result
= build1 (NOP_EXPR
, type1
, result
);
4789 /* They don't match; promote them both and then try to reconcile them.
4790 But don't permit mismatching enum types. */
4791 if (code1
== ENUMERAL_TYPE
)
4793 if (code2
== ENUMERAL_TYPE
)
4795 cp_error ("enumeral mismatch in conditional expression: `%T' vs `%T'", type1
, type2
);
4796 return error_mark_node
;
4798 else if (extra_warnings
&& ! IS_AGGR_TYPE_CODE (code2
)
4799 && type2
!= type_promotes_to (type1
))
4800 warning ("enumeral and non-enumeral type in conditional expression");
4802 else if (extra_warnings
4803 && code2
== ENUMERAL_TYPE
&& ! IS_AGGR_TYPE_CODE (code1
)
4804 && type1
!= type_promotes_to (type2
))
4805 warning ("enumeral and non-enumeral type in conditional expression");
4807 if (code1
!= VOID_TYPE
)
4809 op1
= default_conversion (op1
);
4810 type1
= TREE_TYPE (op1
);
4811 if (TYPE_PTRMEMFUNC_P (type1
))
4812 type1
= TYPE_PTRMEMFUNC_FN_TYPE (type1
);
4813 code1
= TREE_CODE (type1
);
4815 if (code2
!= VOID_TYPE
)
4817 op2
= default_conversion (op2
);
4818 type2
= TREE_TYPE (op2
);
4819 if (TYPE_PTRMEMFUNC_P (type2
))
4820 type2
= TYPE_PTRMEMFUNC_FN_TYPE (type2
);
4821 code2
= TREE_CODE (type2
);
4824 if (code1
== RECORD_TYPE
&& code2
== RECORD_TYPE
4825 && real_lvalue_p (op1
) && real_lvalue_p (op2
)
4826 && comptypes (type1
, type2
, -1))
4828 type1
= build_reference_type (type1
);
4829 type2
= build_reference_type (type2
);
4830 result_type
= common_type (type1
, type2
);
4831 op1
= convert_to_reference (result_type
, op1
, CONV_IMPLICIT
,
4832 LOOKUP_NORMAL
, NULL_TREE
);
4833 op2
= convert_to_reference (result_type
, op2
, CONV_IMPLICIT
,
4834 LOOKUP_NORMAL
, NULL_TREE
);
4836 /* Quickly detect the usual case where op1 and op2 have the same type
4838 else if (TYPE_MAIN_VARIANT (type1
) == TYPE_MAIN_VARIANT (type2
))
4841 result_type
= type1
;
4843 result_type
= cp_build_type_variant
4845 TREE_READONLY (op1
) || TREE_READONLY (op2
),
4846 TREE_THIS_VOLATILE (op1
) || TREE_THIS_VOLATILE (op2
));
4848 else if ((code1
== INTEGER_TYPE
|| code1
== REAL_TYPE
)
4849 && (code2
== INTEGER_TYPE
|| code2
== REAL_TYPE
))
4851 result_type
= common_type (type1
, type2
);
4853 else if (code1
== VOID_TYPE
|| code2
== VOID_TYPE
)
4855 if (pedantic
&& (code1
!= VOID_TYPE
|| code2
!= VOID_TYPE
))
4856 pedwarn ("ANSI C++ forbids conditional expr with only one void side");
4857 result_type
= void_type_node
;
4859 else if (code1
== POINTER_TYPE
&& code2
== POINTER_TYPE
)
4861 if (comp_target_types (type1
, type2
, 1))
4862 result_type
= common_type (type1
, type2
);
4863 else if (integer_zerop (op1
) && TREE_TYPE (type1
) == void_type_node
4864 && TREE_CODE (orig_op1
) != NOP_EXPR
)
4865 result_type
= qualify_type (type2
, type1
);
4866 else if (integer_zerop (op2
) && TREE_TYPE (type2
) == void_type_node
4867 && TREE_CODE (orig_op2
) != NOP_EXPR
)
4868 result_type
= qualify_type (type1
, type2
);
4869 else if (TYPE_MAIN_VARIANT (TREE_TYPE (type1
)) == void_type_node
)
4871 if (pedantic
&& TREE_CODE (type2
) == FUNCTION_TYPE
)
4872 pedwarn ("ANSI C++ forbids conditional expr between `void *' and function pointer");
4873 result_type
= qualify_type (type1
, type2
);
4875 else if (TYPE_MAIN_VARIANT (TREE_TYPE (type2
)) == void_type_node
)
4877 if (pedantic
&& TREE_CODE (type1
) == FUNCTION_TYPE
)
4878 pedwarn ("ANSI C++ forbids conditional expr between `void *' and function pointer");
4879 result_type
= qualify_type (type2
, type1
);
4882 else if (comptypes (type2
, type1
, 0))
4883 result_type
= type2
;
4884 else if (IS_AGGR_TYPE (TREE_TYPE (type1
))
4885 && IS_AGGR_TYPE (TREE_TYPE (type2
))
4886 && (result_type
= common_base_type (TREE_TYPE (type1
), TREE_TYPE (type2
))))
4888 if (result_type
== error_mark_node
)
4890 cp_error ("common base type of types `%T' and `%T' is ambiguous",
4891 TREE_TYPE (type1
), TREE_TYPE (type2
));
4892 result_type
= ptr_type_node
;
4897 && result_type
!= TREE_TYPE (type1
)
4898 && result_type
!= TREE_TYPE (type2
))
4899 cp_pedwarn ("`%T' and `%T' converted to `%T *' in conditional expression",
4900 type1
, type2
, result_type
);
4902 result_type
= build_pointer_type (result_type
);
4907 pedwarn ("pointer type mismatch in conditional expression");
4908 result_type
= ptr_type_node
;
4911 else if (code1
== POINTER_TYPE
&& code2
== INTEGER_TYPE
)
4913 if (!integer_zerop (op2
))
4914 pedwarn ("pointer/integer type mismatch in conditional expression");
4916 op2
= null_pointer_node
;
4918 result_type
= type1
;
4920 else if (code2
== POINTER_TYPE
&& code1
== INTEGER_TYPE
)
4922 if (!integer_zerop (op1
))
4923 pedwarn ("pointer/integer type mismatch in conditional expression");
4925 op1
= null_pointer_node
;
4927 result_type
= type2
;
4932 /* The match does not look good. If either is
4933 an aggregate value, try converting to a scalar type. */
4934 if (code1
== RECORD_TYPE
&& code2
== RECORD_TYPE
)
4936 cp_error ("aggregate mismatch in conditional expression: `%T' vs `%T'", type1
, type2
);
4937 return error_mark_node
;
4939 /* Warning: this code assumes that conversion between cv-variants of
4940 a type is done using NOP_EXPRs. */
4941 if (code1
== RECORD_TYPE
&& TYPE_HAS_CONVERSION (type1
))
4943 tree tmp
= build_pointer_type
4944 (build_type_variant (TREE_TYPE (type2
), 1, 1));
4945 tmp
= build_type_conversion (CONVERT_EXPR
, tmp
, op1
, 0);
4946 if (tmp
== NULL_TREE
)
4948 cp_error ("incompatible types `%T' and `%T' in `?:'",
4950 return error_mark_node
;
4952 if (tmp
== error_mark_node
)
4953 error ("ambiguous pointer conversion");
4956 result_type
= common_type (type1
, TREE_TYPE (tmp
));
4959 else if (code2
== RECORD_TYPE
&& TYPE_HAS_CONVERSION (type2
))
4961 tree tmp
= build_pointer_type
4962 (build_type_variant (TREE_TYPE (type1
), 1, 1));
4963 tmp
= build_type_conversion (CONVERT_EXPR
, tmp
, op2
, 0);
4964 if (tmp
== NULL_TREE
)
4966 cp_error ("incompatible types `%T' and `%T' in `?:'",
4968 return error_mark_node
;
4970 if (tmp
== error_mark_node
)
4971 error ("ambiguous pointer conversion");
4974 result_type
= common_type (type1
, TREE_TYPE (tmp
));
4977 else if (flag_cond_mismatch
)
4978 result_type
= void_type_node
;
4981 error ("type mismatch in conditional expression");
4982 return error_mark_node
;
4986 if (TREE_CODE (result_type
) == POINTER_TYPE
4987 && TREE_CODE (TREE_TYPE (result_type
)) == METHOD_TYPE
)
4988 result_type
= build_ptrmemfunc_type (result_type
);
4990 if (result_type
!= TREE_TYPE (op1
))
4991 op1
= convert_and_check (result_type
, op1
);
4992 if (result_type
!= TREE_TYPE (op2
))
4993 op2
= convert_and_check (result_type
, op2
);
4995 if (TREE_CONSTANT (ifexp
))
4996 return integer_zerop (ifexp
) ? op2
: op1
;
4998 return convert_from_reference
4999 (fold (build (COND_EXPR
, result_type
, ifexp
, op1
, op2
)));
5002 /* Handle overloading of the ',' operator when needed. Otherwise,
5003 this function just builds an expression list. */
5005 build_x_compound_expr (list
)
5008 tree rest
= TREE_CHAIN (list
);
5011 if (current_template_parms
)
5012 return build_min_nt (COMPOUND_EXPR
, list
, NULL_TREE
);
5014 if (rest
== NULL_TREE
)
5015 return build_compound_expr (list
);
5017 result
= build_opfncall (COMPOUND_EXPR
, LOOKUP_NORMAL
,
5018 TREE_VALUE (list
), TREE_VALUE (rest
), NULL_TREE
);
5020 return build_x_compound_expr (tree_cons (NULL_TREE
, result
, TREE_CHAIN (rest
)));
5022 if (! TREE_SIDE_EFFECTS (TREE_VALUE (list
)))
5024 /* the left-hand operand of a comma expression is like an expression
5025 statement: we should warn if it doesn't have any side-effects,
5026 unless it was explicitly cast to (void). */
5027 if ((extra_warnings
|| warn_unused
)
5028 && !(TREE_CODE (TREE_VALUE(list
)) == CONVERT_EXPR
5029 && TREE_TYPE (TREE_VALUE(list
)) == void_type_node
))
5030 warning("left-hand operand of comma expression has no effect");
5032 #if 0 /* this requires a gcc backend patch to export warn_if_unused_value */
5033 else if (warn_unused
)
5034 warn_if_unused_value (TREE_VALUE(list
));
5037 return build_compound_expr (tree_cons (NULL_TREE
, TREE_VALUE (list
),
5038 build_tree_list (NULL_TREE
, build_x_compound_expr (rest
))));
5041 /* Given a list of expressions, return a compound expression
5042 that performs them all and returns the value of the last of them. */
5045 build_compound_expr (list
)
5050 if (TREE_READONLY_DECL_P (TREE_VALUE (list
)))
5051 TREE_VALUE (list
) = decl_constant_value (TREE_VALUE (list
));
5053 if (TREE_CHAIN (list
) == 0)
5055 /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
5056 Strip such NOP_EXPRs, since LIST is used in non-lvalue context. */
5057 if (TREE_CODE (list
) == NOP_EXPR
5058 && TREE_TYPE (list
) == TREE_TYPE (TREE_OPERAND (list
, 0)))
5059 list
= TREE_OPERAND (list
, 0);
5061 /* Convert arrays to pointers. */
5062 if (TREE_CODE (TREE_TYPE (TREE_VALUE (list
))) == ARRAY_TYPE
)
5063 return default_conversion (TREE_VALUE (list
));
5065 return TREE_VALUE (list
);
5068 rest
= build_compound_expr (TREE_CHAIN (list
));
5070 /* When pedantic, a compound expression cannot be a constant expression. */
5071 if (! TREE_SIDE_EFFECTS (TREE_VALUE (list
)) && ! pedantic
)
5074 return build (COMPOUND_EXPR
, TREE_TYPE (rest
),
5075 break_out_cleanups (TREE_VALUE (list
)), rest
);
5079 build_static_cast (type
, expr
)
5082 return build_c_cast (type
, expr
, 0);
5086 build_reinterpret_cast (type
, expr
)
5089 tree intype
= TREE_TYPE (expr
);
5091 if (current_template_parms
)
5093 tree t
= build_min (REINTERPRET_CAST_EXPR
, type
, expr
);
5097 if (TYPE_PTRMEMFUNC_P (type
))
5098 if (TYPE_PTRMEMFUNC_P (intype
))
5099 return build1 (NOP_EXPR
, type
, expr
);
5101 if (TYPE_PTRMEMFUNC_P (type
))
5102 type
= TYPE_PTRMEMFUNC_FN_TYPE (type
);
5104 if (TYPE_PTRMEMFUNC_P (intype
))
5105 intype
= TYPE_PTRMEMFUNC_FN_TYPE (intype
);
5107 if (! POINTER_TYPE_P (type
) && ! TREE_CODE (type
) == INTEGER_TYPE
)
5109 cp_error ("reinterpret_cast cannot convert to type `%T'", type
);
5110 return error_mark_node
;
5112 if (! POINTER_TYPE_P (intype
) && ! TREE_CODE (intype
) == INTEGER_TYPE
)
5114 cp_error ("reinterpret_cast cannot convert from type `%T'", type
);
5115 return error_mark_node
;
5117 if (TREE_CODE (type
) == INTEGER_TYPE
&& TREE_CODE (intype
) != POINTER_TYPE
)
5119 cp_error ("reinterpret_cast cannot convert non-pointer type `%T' to `%T'",
5121 return error_mark_node
;
5123 if (TREE_CODE (intype
) == INTEGER_TYPE
&& TREE_CODE (type
) != POINTER_TYPE
)
5125 cp_error ("reinterpret_cast cannot convert `%T' to non-pointer type `%T'",
5127 return error_mark_node
;
5130 if (TREE_CODE (type
) == POINTER_TYPE
&& TREE_CODE (intype
) == POINTER_TYPE
)
5131 expr
= convert (ptr_type_node
, expr
);
5133 return build_c_cast (type
, expr
, 0);
5137 build_const_cast (type
, expr
)
5140 tree intype
= TREE_TYPE (expr
);
5143 if (type
== error_mark_node
|| expr
== error_mark_node
)
5144 return error_mark_node
;
5146 if (TYPE_PTRMEMFUNC_P (type
))
5147 type
= TYPE_PTRMEMFUNC_FN_TYPE (type
);
5148 if (TYPE_PTRMEMFUNC_P (intype
))
5149 intype
= TYPE_PTRMEMFUNC_FN_TYPE (intype
);
5151 if (! POINTER_TYPE_P (type
))
5153 cp_error ("const_cast cannot convert to non-pointer type `%T'", type
);
5154 return error_mark_node
;
5156 if (TREE_CODE (type
) == REFERENCE_TYPE
&& ! real_lvalue_p (expr
))
5158 cp_error ("const_cast cannot convert rvalue to type `%T'", type
);
5159 return error_mark_node
;
5161 if (TREE_CODE (type
) == POINTER_TYPE
&& TREE_CODE (intype
) != POINTER_TYPE
)
5163 cp_error ("const_cast cannot convert non-pointer type `%T' to type `%T'",
5165 return error_mark_node
;
5168 if (TREE_CODE (type
) == REFERENCE_TYPE
)
5170 t1
= TREE_TYPE (type
);
5175 t1
= TREE_TYPE (type
);
5176 t2
= TREE_TYPE (intype
);
5178 for (; TREE_CODE (t1
) == POINTER_TYPE
&& TREE_CODE (t2
) == POINTER_TYPE
;
5179 t1
= TREE_TYPE (t1
), t2
= TREE_TYPE (t2
))
5183 if (TREE_CODE (t1
) == OFFSET_TYPE
&& TREE_CODE (t2
) == OFFSET_TYPE
)
5185 if (TYPE_OFFSET_BASETYPE (t1
) != TYPE_OFFSET_BASETYPE (t2
))
5187 cp_error ("const_cast cannot convert between pointers to members of different types `%T' and `%T'",
5188 TYPE_OFFSET_BASETYPE (t2
), TYPE_OFFSET_BASETYPE (t1
));
5189 return error_mark_node
;
5191 t1
= TREE_TYPE (t1
);
5192 t2
= TREE_TYPE (t2
);
5195 if (TYPE_MAIN_VARIANT (t1
) != TYPE_MAIN_VARIANT (t2
))
5197 cp_error ("const_cast cannot convert unrelated type `%T' to `%T'",
5199 return error_mark_node
;
5202 return build_c_cast (type
, expr
, 0);
5205 /* Build an expression representing a cast to type TYPE of expression EXPR.
5207 ALLOW_NONCONVERTING is true if we should allow non-converting constructors
5208 when doing the cast. */
5211 build_c_cast (type
, expr
, allow_nonconverting
)
5214 int allow_nonconverting
;
5216 register tree value
= expr
;
5218 if (type
== error_mark_node
|| expr
== error_mark_node
)
5219 return error_mark_node
;
5221 /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
5222 Strip such NOP_EXPRs if VALUE is being used in non-lvalue context. */
5223 if (TREE_CODE (type
) != REFERENCE_TYPE
5224 && TREE_CODE (value
) == NOP_EXPR
5225 && TREE_TYPE (value
) == TREE_TYPE (TREE_OPERAND (value
, 0)))
5226 value
= TREE_OPERAND (value
, 0);
5228 if (TREE_TYPE (expr
)
5229 && TREE_CODE (TREE_TYPE (expr
)) == OFFSET_TYPE
5230 && TREE_CODE (type
) != OFFSET_TYPE
)
5231 value
= resolve_offset_ref (value
);
5233 if (TREE_CODE (type
) == ARRAY_TYPE
)
5235 /* Allow casting from T1* to T2[] because Cfront allows it.
5236 NIHCL uses it. It is not valid ANSI C however, and hence, not
5238 if (TREE_CODE (TREE_TYPE (expr
)) == POINTER_TYPE
)
5241 pedwarn ("ANSI C++ forbids casting to an array type");
5242 type
= build_pointer_type (TREE_TYPE (type
));
5246 error ("ANSI C++ forbids casting to an array type");
5247 return error_mark_node
;
5251 if (TREE_CODE (type
) == FUNCTION_TYPE
5252 || TREE_CODE (type
) == METHOD_TYPE
)
5254 cp_error ("casting to function type `%T'", type
);
5255 return error_mark_node
;
5258 if (IS_SIGNATURE (type
))
5260 error ("cast specifies signature type");
5261 return error_mark_node
;
5264 if (current_template_parms
)
5266 tree t
= build_min (CAST_EXPR
, type
,
5267 min_tree_cons (NULL_TREE
, value
, NULL_TREE
));
5271 if (TREE_CODE (type
) == VOID_TYPE
)
5272 value
= build1 (CONVERT_EXPR
, type
, value
);
5273 else if (TREE_TYPE (value
) == NULL_TREE
5274 || type_unknown_p (value
))
5276 value
= instantiate_type (type
, value
, 1);
5278 if (value
== error_mark_node
)
5279 return error_mark_node
;
5286 /* Convert functions and arrays to pointers and
5287 convert references to their expanded types,
5288 but don't convert any other types. */
5289 if (TREE_CODE (TREE_TYPE (value
)) == FUNCTION_TYPE
5290 || TREE_CODE (TREE_TYPE (value
)) == METHOD_TYPE
5291 || TREE_CODE (TREE_TYPE (value
)) == ARRAY_TYPE
5292 || TREE_CODE (TREE_TYPE (value
)) == REFERENCE_TYPE
)
5293 value
= default_conversion (value
);
5294 otype
= TREE_TYPE (value
);
5296 /* Optionally warn about potentially worrisome casts. */
5299 && TREE_CODE (type
) == POINTER_TYPE
5300 && TREE_CODE (otype
) == POINTER_TYPE
)
5302 /* For C++ we make these regular warnings, rather than
5303 softening them into pedwarns. */
5304 if (TYPE_VOLATILE (TREE_TYPE (otype
))
5305 && ! TYPE_VOLATILE (TREE_TYPE (type
)))
5306 warning ("cast discards `volatile' from pointer target type");
5307 if (TYPE_READONLY (TREE_TYPE (otype
))
5308 && ! TYPE_READONLY (TREE_TYPE (type
)))
5309 warning ("cast discards `const' from pointer target type");
5312 /* Warn about possible alignment problems. */
5313 if (STRICT_ALIGNMENT
&& warn_cast_align
5314 && TREE_CODE (type
) == POINTER_TYPE
5315 && TREE_CODE (otype
) == POINTER_TYPE
5316 && TREE_CODE (TREE_TYPE (otype
)) != VOID_TYPE
5317 && TREE_CODE (TREE_TYPE (otype
)) != FUNCTION_TYPE
5318 && TYPE_ALIGN (TREE_TYPE (type
)) > TYPE_ALIGN (TREE_TYPE (otype
)))
5319 warning ("cast increases required alignment of target type");
5322 /* We should see about re-enabling these, they seem useful to
5324 if (TREE_CODE (type
) == INTEGER_TYPE
5325 && TREE_CODE (otype
) == POINTER_TYPE
5326 && TYPE_PRECISION (type
) != TYPE_PRECISION (otype
))
5327 warning ("cast from pointer to integer of different size");
5329 if (TREE_CODE (type
) == POINTER_TYPE
5330 && TREE_CODE (otype
) == INTEGER_TYPE
5331 && TYPE_PRECISION (type
) != TYPE_PRECISION (otype
)
5332 /* Don't warn about converting 0 to pointer,
5333 provided the 0 was explicit--not cast or made by folding. */
5334 && !(TREE_CODE (value
) == INTEGER_CST
&& integer_zerop (value
)))
5335 warning ("cast to pointer from integer of different size");
5338 flag
= allow_nonconverting
? CONV_NONCONVERTING
: 0;
5340 if (TREE_CODE (type
) == REFERENCE_TYPE
)
5341 value
= (convert_from_reference
5342 (convert_to_reference (type
, value
, CONV_OLD_CONVERT
|flag
,
5343 LOOKUP_COMPLAIN
, NULL_TREE
)));
5348 if (TREE_READONLY_DECL_P (value
))
5349 value
= decl_constant_value (value
);
5352 value
= convert_force (type
, value
, flag
);
5354 /* Ignore any integer overflow caused by the cast. */
5355 if (TREE_CODE (value
) == INTEGER_CST
)
5357 TREE_OVERFLOW (value
) = TREE_OVERFLOW (ovalue
);
5358 TREE_CONSTANT_OVERFLOW (value
) = TREE_CONSTANT_OVERFLOW (ovalue
);
5363 /* Always produce some operator for an explicit cast,
5364 so we can tell (for -pedantic) that the cast is no lvalue.
5365 Also, pedantically, don't let (void *) (FOO *) 0 be a null
5366 pointer constant. */
5367 if (TREE_CODE (type
) != REFERENCE_TYPE
5370 && TREE_CODE (value
) == INTEGER_CST
5371 && TREE_CODE (expr
) == INTEGER_CST
5372 && TREE_CODE (TREE_TYPE (expr
)) != INTEGER_TYPE
)))
5373 value
= non_lvalue (value
);
5379 expand_target_expr (t
)
5382 extern int temp_slot_level
;
5383 extern int target_temp_slot_level
;
5384 int old_temp_level
= target_temp_slot_level
;
5386 tree xval
= make_node (RTL_EXPR
);
5389 /* Any TARGET_EXPR temps live only as long as the outer temp level.
5390 Since they are preserved in this new inner level, we know they
5391 will make it into the outer level. */
5393 target_temp_slot_level
= temp_slot_level
;
5395 do_pending_stack_adjust ();
5396 start_sequence_for_rtl_expr (xval
);
5398 rtxval
= expand_expr (t
, NULL
, VOIDmode
, 0);
5399 do_pending_stack_adjust ();
5400 TREE_SIDE_EFFECTS (xval
) = 1;
5401 RTL_EXPR_SEQUENCE (xval
) = get_insns ();
5403 RTL_EXPR_RTL (xval
) = rtxval
;
5404 TREE_TYPE (xval
) = TREE_TYPE (t
);
5407 target_temp_slot_level
= old_temp_level
;
5412 /* Build an assignment expression of lvalue LHS from value RHS.
5413 MODIFYCODE is the code for a binary operator that we use
5414 to combine the old value of LHS with RHS to get the new value.
5415 Or else MODIFYCODE is NOP_EXPR meaning do a simple assignment.
5417 C++: If MODIFYCODE is INIT_EXPR, then leave references unbashed.
5420 build_modify_expr (lhs
, modifycode
, rhs
)
5422 enum tree_code modifycode
;
5425 register tree result
;
5427 tree lhstype
= TREE_TYPE (lhs
);
5428 tree olhstype
= lhstype
;
5431 /* Avoid duplicate error messages from operands that had errors. */
5432 if (TREE_CODE (lhs
) == ERROR_MARK
|| TREE_CODE (rhs
) == ERROR_MARK
)
5433 return error_mark_node
;
5435 /* Types that aren't fully specified cannot be used in assignments. */
5436 lhs
= require_complete_type (lhs
);
5440 /* Handle assignment to signature pointers/refs. */
5442 if (TYPE_LANG_SPECIFIC (lhstype
) &&
5443 (IS_SIGNATURE_POINTER (lhstype
) || IS_SIGNATURE_REFERENCE (lhstype
)))
5445 return build_signature_pointer_constructor (lhs
, rhs
);
5448 /* Handle control structure constructs used as "lvalues". */
5450 switch (TREE_CODE (lhs
))
5452 /* Handle --foo = 5; as these are valid constructs in C++ */
5453 case PREDECREMENT_EXPR
:
5454 case PREINCREMENT_EXPR
:
5455 if (TREE_SIDE_EFFECTS (TREE_OPERAND (lhs
, 0)))
5456 lhs
= build (TREE_CODE (lhs
), TREE_TYPE (lhs
),
5457 stabilize_reference (TREE_OPERAND (lhs
, 0)));
5458 return build (COMPOUND_EXPR
, lhstype
,
5460 build_modify_expr (TREE_OPERAND (lhs
, 0),
5463 /* Handle (a, b) used as an "lvalue". */
5465 newrhs
= build_modify_expr (TREE_OPERAND (lhs
, 1),
5467 if (TREE_CODE (newrhs
) == ERROR_MARK
)
5468 return error_mark_node
;
5469 return build (COMPOUND_EXPR
, lhstype
,
5470 TREE_OPERAND (lhs
, 0), newrhs
);
5473 newrhs
= build_modify_expr (TREE_OPERAND (lhs
, 0), modifycode
, rhs
);
5474 if (TREE_CODE (newrhs
) == ERROR_MARK
)
5475 return error_mark_node
;
5476 return build (COMPOUND_EXPR
, lhstype
, lhs
, newrhs
);
5478 /* Handle (a ? b : c) used as an "lvalue". */
5480 rhs
= save_expr (rhs
);
5482 /* Produce (a ? (b = rhs) : (c = rhs))
5483 except that the RHS goes through a save-expr
5484 so the code to compute it is only emitted once. */
5486 = build_conditional_expr (TREE_OPERAND (lhs
, 0),
5487 build_modify_expr (convert (TREE_TYPE (lhs
), TREE_OPERAND (lhs
, 1)),
5489 build_modify_expr (convert (TREE_TYPE (lhs
), TREE_OPERAND (lhs
, 2)),
5491 if (TREE_CODE (cond
) == ERROR_MARK
)
5493 /* Make sure the code to compute the rhs comes out
5494 before the split. */
5495 return build (COMPOUND_EXPR
, TREE_TYPE (lhs
),
5496 /* Case to void to suppress warning
5497 from warn_if_unused_value. */
5498 convert (void_type_node
, rhs
), cond
);
5502 if (TREE_CODE (lhs
) == OFFSET_REF
)
5504 if (TREE_OPERAND (lhs
, 0) == NULL_TREE
)
5506 /* Static class member? */
5507 tree member
= TREE_OPERAND (lhs
, 1);
5508 if (TREE_CODE (member
) == VAR_DECL
)
5512 compiler_error ("invalid static class member");
5513 return error_mark_node
;
5517 lhs
= resolve_offset_ref (lhs
);
5519 olhstype
= lhstype
= TREE_TYPE (lhs
);
5522 if (TREE_CODE (lhstype
) == REFERENCE_TYPE
5523 && modifycode
!= INIT_EXPR
)
5525 lhs
= convert_from_reference (lhs
);
5526 olhstype
= lhstype
= TREE_TYPE (lhs
);
5529 /* If a binary op has been requested, combine the old LHS value with the RHS
5530 producing the value we should actually store into the LHS. */
5532 if (modifycode
== INIT_EXPR
)
5534 if (! IS_AGGR_TYPE (lhstype
))
5535 /* Do the default thing */;
5536 else if (! TYPE_HAS_CONSTRUCTOR (lhstype
))
5538 cp_error ("`%T' has no constructors", lhstype
);
5539 return error_mark_node
;
5541 else if (TYPE_HAS_TRIVIAL_INIT_REF (lhstype
)
5542 && TYPE_MAIN_VARIANT (lhstype
) == TYPE_MAIN_VARIANT (TREE_TYPE (newrhs
)))
5543 /* Do the default thing */;
5546 result
= build_method_call (lhs
, ctor_identifier
,
5547 build_tree_list (NULL_TREE
, rhs
),
5548 TYPE_BINFO (lhstype
), LOOKUP_NORMAL
);
5549 if (result
== NULL_TREE
)
5550 return error_mark_node
;
5554 else if (modifycode
== NOP_EXPR
)
5556 /* `operator=' is not an inheritable operator. */
5557 if (! IS_AGGR_TYPE (lhstype
))
5558 /* Do the default thing */;
5559 else if (! TYPE_HAS_ASSIGNMENT (lhstype
))
5561 cp_error ("`%T' does not define operator=", lhstype
);
5562 return error_mark_node
;
5564 else if (TYPE_HAS_TRIVIAL_ASSIGN_REF (lhstype
)
5565 && TYPE_MAIN_VARIANT (lhstype
) == TYPE_MAIN_VARIANT (TREE_TYPE (newrhs
)))
5568 /* If we care about this, do overload resolution. */
5569 build_opfncall (MODIFY_EXPR
, LOOKUP_NORMAL
,
5570 lhs
, rhs
, make_node (NOP_EXPR
));
5572 /* Do the default thing */;
5576 result
= build_opfncall (MODIFY_EXPR
, LOOKUP_NORMAL
,
5577 lhs
, rhs
, make_node (NOP_EXPR
));
5578 if (result
== NULL_TREE
)
5579 return error_mark_node
;
5584 else if (PROMOTES_TO_AGGR_TYPE (lhstype
, REFERENCE_TYPE
))
5586 /* This case must convert to some sort of lvalue that
5587 can participate in an op= operation. */
5590 if (build_default_binary_type_conversion (modifycode
, &lhs_tmp
, &rhs_tmp
))
5592 lhs
= stabilize_reference (lhs_tmp
);
5593 /* Forget it was ever anything else. */
5594 olhstype
= lhstype
= TREE_TYPE (lhs
);
5595 newrhs
= build_binary_op (modifycode
, lhs
, rhs_tmp
, 1);
5599 cp_error ("no match for `%Q(%#T, %#T)'", modifycode
,
5600 TREE_TYPE (lhs
), TREE_TYPE (rhs
));
5601 return error_mark_node
;
5606 lhs
= stabilize_reference (lhs
);
5607 newrhs
= build_binary_op (modifycode
, lhs
, rhs
, 1);
5608 if (newrhs
== error_mark_node
)
5610 cp_error (" in evaluation of `%Q(%#T, %#T)'", modifycode
,
5611 TREE_TYPE (lhs
), TREE_TYPE (rhs
));
5612 return error_mark_node
;
5616 /* Handle a cast used as an "lvalue".
5617 We have already performed any binary operator using the value as cast.
5618 Now convert the result to the cast type of the lhs,
5619 and then true type of the lhs and store it there;
5620 then convert result back to the cast type to be the value
5621 of the assignment. */
5623 switch (TREE_CODE (lhs
))
5628 case FIX_TRUNC_EXPR
:
5629 case FIX_FLOOR_EXPR
:
5630 case FIX_ROUND_EXPR
:
5632 if (TREE_CODE (TREE_TYPE (newrhs
)) == ARRAY_TYPE
5633 || TREE_CODE (TREE_TYPE (newrhs
)) == FUNCTION_TYPE
5634 || TREE_CODE (TREE_TYPE (newrhs
)) == METHOD_TYPE
5635 || TREE_CODE (TREE_TYPE (newrhs
)) == OFFSET_TYPE
)
5636 newrhs
= default_conversion (newrhs
);
5638 tree inner_lhs
= TREE_OPERAND (lhs
, 0);
5640 if (! lvalue_p (lhs
) && pedantic
)
5641 pedwarn ("cast to non-reference type used as lvalue");
5643 result
= build_modify_expr (inner_lhs
, NOP_EXPR
,
5644 convert (TREE_TYPE (inner_lhs
),
5645 convert (lhstype
, newrhs
)));
5646 if (TREE_CODE (result
) == ERROR_MARK
)
5648 return convert (TREE_TYPE (lhs
), result
);
5652 /* Now we have handled acceptable kinds of LHS that are not truly lvalues.
5653 Reject anything strange now. */
5655 if (!lvalue_or_else (lhs
, "assignment"))
5656 return error_mark_node
;
5658 GNU_xref_assign (lhs
);
5660 /* Warn about storing in something that is `const'. */
5661 /* For C++, don't warn if this is initialization. */
5662 if (modifycode
!= INIT_EXPR
5663 /* For assignment to `const' signature pointer/reference fields,
5664 don't warn either, we already printed a better message before. */
5665 && ! (TREE_CODE (lhs
) == COMPONENT_REF
5666 && (IS_SIGNATURE_POINTER (TREE_TYPE (TREE_OPERAND (lhs
, 0)))
5667 || IS_SIGNATURE_REFERENCE (TREE_TYPE (TREE_OPERAND (lhs
, 0)))))
5668 && (TREE_READONLY (lhs
) || TYPE_READONLY (lhstype
)
5669 || ((TREE_CODE (lhstype
) == RECORD_TYPE
5670 || TREE_CODE (lhstype
) == UNION_TYPE
)
5671 && C_TYPE_FIELDS_READONLY (lhstype
))
5672 || (TREE_CODE (lhstype
) == REFERENCE_TYPE
5673 && TYPE_READONLY (TREE_TYPE (lhstype
)))))
5674 readonly_error (lhs
, "assignment", 0);
5676 /* If storing into a structure or union member,
5677 it has probably been given type `int'.
5678 Compute the type that would go with
5679 the actual amount of storage the member occupies. */
5681 if (TREE_CODE (lhs
) == COMPONENT_REF
5682 && (TREE_CODE (lhstype
) == INTEGER_TYPE
5683 || TREE_CODE (lhstype
) == REAL_TYPE
5684 || TREE_CODE (lhstype
) == ENUMERAL_TYPE
))
5686 lhstype
= TREE_TYPE (get_unwidened (lhs
, 0));
5688 /* If storing in a field that is in actuality a short or narrower
5689 than one, we must store in the field in its actual type. */
5691 if (lhstype
!= TREE_TYPE (lhs
))
5693 lhs
= copy_node (lhs
);
5694 TREE_TYPE (lhs
) = lhstype
;
5698 /* check to see if there is an assignment to `this' */
5699 if (lhs
== current_class_decl
)
5701 if (flag_this_is_variable
> 0
5702 && DECL_NAME (current_function_decl
) != NULL_TREE
5703 && (DECL_NAME (current_function_decl
)
5704 != constructor_name (current_class_type
)))
5705 warning ("assignment to `this' not in constructor or destructor");
5706 current_function_just_assigned_this
= 1;
5709 /* The TREE_TYPE of RHS may be TYPE_UNKNOWN. This can happen
5710 when the type of RHS is not yet known, i.e. its type
5711 is inherited from LHS. */
5712 rhs
= require_instantiated_type (lhstype
, newrhs
, error_mark_node
);
5713 if (rhs
== error_mark_node
)
5714 return error_mark_node
;
5717 if (modifycode
!= INIT_EXPR
)
5719 /* Make modifycode now either a NOP_EXPR or an INIT_EXPR. */
5720 modifycode
= NOP_EXPR
;
5721 /* Reference-bashing */
5722 if (TREE_CODE (lhstype
) == REFERENCE_TYPE
)
5724 tree tmp
= convert_from_reference (lhs
);
5725 lhstype
= TREE_TYPE (tmp
);
5726 if (TYPE_SIZE (lhstype
) == 0)
5728 incomplete_type_error (lhs
, lhstype
);
5729 return error_mark_node
;
5734 if (TREE_CODE (TREE_TYPE (newrhs
)) == REFERENCE_TYPE
)
5736 tree tmp
= convert_from_reference (newrhs
);
5737 if (TYPE_SIZE (TREE_TYPE (tmp
)) == 0)
5739 incomplete_type_error (newrhs
, TREE_TYPE (tmp
));
5740 return error_mark_node
;
5746 if (TREE_SIDE_EFFECTS (lhs
))
5747 lhs
= stabilize_reference (lhs
);
5748 if (TREE_SIDE_EFFECTS (newrhs
))
5749 newrhs
= stabilize_reference (newrhs
);
5751 /* Convert new value to destination type. */
5753 if (TREE_CODE (lhstype
) == ARRAY_TYPE
)
5757 if (! comptypes (lhstype
, TREE_TYPE (rhs
), 0))
5759 cp_error ("incompatible types in assignment of `%T' to `%T'",
5760 TREE_TYPE (rhs
), lhstype
);
5761 return error_mark_node
;
5764 /* Allow array assignment in compiler-generated code. */
5765 if (pedantic
&& ! DECL_ARTIFICIAL (current_function_decl
))
5766 pedwarn ("ANSI C++ forbids assignment of arrays");
5768 /* Have to wrap this in RTL_EXPR for two cases:
5769 in base or member initialization and if we
5770 are a branch of a ?: operator. Since we
5771 can't easily know the latter, just do it always. */
5773 result
= make_node (RTL_EXPR
);
5775 TREE_TYPE (result
) = void_type_node
;
5776 do_pending_stack_adjust ();
5777 start_sequence_for_rtl_expr (result
);
5779 /* As a matter of principle, `start_sequence' should do this. */
5782 from_array
= TREE_CODE (TREE_TYPE (newrhs
)) == ARRAY_TYPE
5783 ? 1 + (modifycode
!= INIT_EXPR
): 0;
5784 expand_vec_init (lhs
, lhs
, array_type_nelts (lhstype
), newrhs
,
5787 do_pending_stack_adjust ();
5789 TREE_SIDE_EFFECTS (result
) = 1;
5790 RTL_EXPR_SEQUENCE (result
) = get_insns ();
5791 RTL_EXPR_RTL (result
) = const0_rtx
;
5796 if (modifycode
== INIT_EXPR
)
5798 newrhs
= convert_for_initialization (lhs
, lhstype
, newrhs
, LOOKUP_NORMAL
,
5799 "assignment", NULL_TREE
, 0);
5800 if (lhs
== DECL_RESULT (current_function_decl
))
5802 if (DECL_INITIAL (lhs
))
5803 warning ("return value from function receives multiple initializations");
5804 DECL_INITIAL (lhs
) = newrhs
;
5809 /* Avoid warnings on enum bit fields. */
5810 if (TREE_CODE (olhstype
) == ENUMERAL_TYPE
5811 && TREE_CODE (lhstype
) == INTEGER_TYPE
)
5813 newrhs
= convert_for_assignment (olhstype
, newrhs
, "assignment",
5815 newrhs
= convert_force (lhstype
, newrhs
, 0);
5818 newrhs
= convert_for_assignment (lhstype
, newrhs
, "assignment",
5820 if (TREE_CODE (newrhs
) == CALL_EXPR
5821 && TYPE_NEEDS_CONSTRUCTING (lhstype
))
5822 newrhs
= build_cplus_new (lhstype
, newrhs
);
5824 /* Can't initialize directly from a TARGET_EXPR, since that would
5825 cause the lhs to be constructed twice, and possibly result in
5826 accidental self-initialization. So we force the TARGET_EXPR to be
5827 expanded without a target. */
5828 if (TREE_CODE (newrhs
) == TARGET_EXPR
)
5829 newrhs
= build (COMPOUND_EXPR
, TREE_TYPE (newrhs
), newrhs
,
5830 TREE_VALUE (newrhs
));
5833 if (TREE_CODE (newrhs
) == ERROR_MARK
)
5834 return error_mark_node
;
5836 if (TREE_CODE (newrhs
) == COND_EXPR
)
5839 tree cond
= TREE_OPERAND (newrhs
, 0);
5841 if (TREE_SIDE_EFFECTS (lhs
))
5842 cond
= build_compound_expr (tree_cons
5844 build_tree_list (NULL_TREE
, cond
)));
5846 /* Cannot have two identical lhs on this one tree (result) as preexpand
5847 calls will rip them out and fill in RTL for them, but when the
5848 rtl is generated, the calls will only be in the first side of the
5849 condition, not on both, or before the conditional jump! (mrs) */
5850 lhs1
= break_out_calls (lhs
);
5853 /* If there's no change, the COND_EXPR behaves like any other rhs. */
5854 result
= build (modifycode
== NOP_EXPR
? MODIFY_EXPR
: INIT_EXPR
,
5855 lhstype
, lhs
, newrhs
);
5858 tree result_type
= TREE_TYPE (newrhs
);
5859 /* We have to convert each arm to the proper type because the
5860 types may have been munged by constant folding. */
5862 = build (COND_EXPR
, result_type
, cond
,
5863 build_modify_expr (lhs
, modifycode
,
5864 convert (result_type
,
5865 TREE_OPERAND (newrhs
, 1))),
5866 build_modify_expr (lhs1
, modifycode
,
5867 convert (result_type
,
5868 TREE_OPERAND (newrhs
, 2))));
5872 result
= build (modifycode
== NOP_EXPR
? MODIFY_EXPR
: INIT_EXPR
,
5873 lhstype
, lhs
, newrhs
);
5875 TREE_SIDE_EFFECTS (result
) = 1;
5877 /* If we got the LHS in a different type for storing in,
5878 convert the result back to the nominal type of LHS
5879 so that the value we return always has the same type
5880 as the LHS argument. */
5882 if (olhstype
== TREE_TYPE (result
))
5884 /* Avoid warnings converting integral types back into enums
5885 for enum bit fields. */
5886 if (TREE_CODE (TREE_TYPE (result
)) == INTEGER_TYPE
5887 && TREE_CODE (olhstype
) == ENUMERAL_TYPE
)
5889 result
= build (COMPOUND_EXPR
, olhstype
, result
, olhs
);
5890 TREE_NO_UNUSED_WARNING (result
) = 1;
5893 return convert_for_assignment (olhstype
, result
, "assignment",
5898 build_x_modify_expr (lhs
, modifycode
, rhs
)
5900 enum tree_code modifycode
;
5903 if (current_template_parms
)
5904 return build_min_nt (MODOP_EXPR
, lhs
,
5905 build_min_nt (modifycode
, 0, 0), rhs
);
5907 if (modifycode
!= NOP_EXPR
)
5909 tree rval
= build_opfncall (MODIFY_EXPR
, LOOKUP_NORMAL
, lhs
, rhs
,
5910 make_node (modifycode
));
5914 return build_modify_expr (lhs
, modifycode
, rhs
);
5917 /* Return 0 if EXP is not a valid lvalue in this language
5918 even though `lvalue_or_else' would accept it. */
5921 language_lvalue_valid (exp
)
5927 /* Get difference in deltas for different pointer to member function
5928 types. Return integer_zero_node, if FROM cannot be converted to a
5929 TO type. If FORCE is true, then allow reverse conversions as well. */
5931 get_delta_difference (from
, to
, force
)
5935 tree delta
= integer_zero_node
;
5941 /* Should get_base_distance here, so we can check if any thing along the
5942 path is virtual, and we need to make sure we stay
5943 inside the real binfos when going through virtual bases.
5944 Maybe we should replace virtual bases with
5945 binfo_member (...CLASSTYPE_VBASECLASSES...)... (mrs) */
5946 binfo
= get_binfo (from
, to
, 1);
5947 if (binfo
== error_mark_node
)
5949 error (" in pointer to member function conversion");
5956 error_not_base_type (from
, to
);
5957 error (" in pointer to member function conversion");
5960 binfo
= get_binfo (to
, from
, 1);
5961 if (binfo
== error_mark_node
)
5963 error (" in pointer to member function conversion");
5968 cp_error ("cannot convert pointer to member of type %T to unrelated pointer to member of type %T", from
, to
);
5971 if (TREE_VIA_VIRTUAL (binfo
))
5973 warning ("pointer to member conversion to virtual base class will only work if you are very careful");
5975 return build_binary_op (MINUS_EXPR
,
5977 BINFO_OFFSET (binfo
), 1);
5979 if (TREE_VIA_VIRTUAL (binfo
))
5981 warning ("pointer to member conversion from virtual base class will only work if you are very careful");
5983 return BINFO_OFFSET (binfo
);
5986 /* Build a constructor for a pointer to member function. It can be
5987 used to initialize global variables, local variable, or used
5988 as a value in expressions. TYPE is the POINTER to METHOD_TYPE we
5991 If FORCE is non-zero, then force this conversion, even if
5992 we would rather not do it. Usually set when using an explicit
5995 Return error_mark_node, if something goes wrong. */
5998 build_ptrmemfunc (type
, pfn
, force
)
6002 tree index
= integer_zero_node
;
6003 tree delta
= integer_zero_node
;
6004 tree delta2
= integer_zero_node
;
6009 /* Handle multiple conversions of pointer to member functions. */
6010 if (TYPE_PTRMEMFUNC_P (TREE_TYPE (pfn
)))
6012 tree ndelta
, ndelta2
, nindex
;
6013 /* Is is already the right type? */
6014 if (type
== TYPE_PTRMEMFUNC_FN_TYPE (TREE_TYPE (pfn
)))
6017 if (TREE_CODE (pfn
) != CONSTRUCTOR
)
6020 ndelta
= convert (ptrdiff_type_node
, build_component_ref (pfn
, delta_identifier
, 0, 0));
6021 ndelta2
= convert (ptrdiff_type_node
, DELTA2_FROM_PTRMEMFUNC (pfn
));
6022 index
= build_component_ref (pfn
, index_identifier
, 0, 0);
6023 delta
= get_delta_difference (TYPE_METHOD_BASETYPE (TREE_TYPE (TYPE_PTRMEMFUNC_FN_TYPE (TREE_TYPE (pfn
)))),
6024 TYPE_METHOD_BASETYPE (TREE_TYPE (type
)),
6026 delta
= build_binary_op (PLUS_EXPR
, delta
, ndelta
, 1);
6027 delta2
= build_binary_op (PLUS_EXPR
, ndelta2
, delta2
, 1);
6028 e1
= fold (build (GT_EXPR
, boolean_type_node
, index
, integer_zero_node
));
6030 u
= build_nt (CONSTRUCTOR
, 0, tree_cons (delta2_identifier
, delta2
, NULL_TREE
));
6031 u
= build_nt (CONSTRUCTOR
, 0, tree_cons (NULL_TREE
, delta
,
6032 tree_cons (NULL_TREE
, index
,
6033 tree_cons (NULL_TREE
, u
, NULL_TREE
))));
6034 e2
= digest_init (TYPE_GET_PTRMEMFUNC_TYPE (type
), u
, (tree
*)0);
6036 pfn
= PFN_FROM_PTRMEMFUNC (pfn
);
6037 npfn
= build1 (NOP_EXPR
, type
, pfn
);
6038 TREE_CONSTANT (npfn
) = TREE_CONSTANT (pfn
);
6040 u
= build_nt (CONSTRUCTOR
, 0, tree_cons (pfn_identifier
, npfn
, NULL_TREE
));
6041 u
= build_nt (CONSTRUCTOR
, 0, tree_cons (NULL_TREE
, delta
,
6042 tree_cons (NULL_TREE
, index
,
6043 tree_cons (NULL_TREE
, u
, NULL_TREE
))));
6044 e3
= digest_init (TYPE_GET_PTRMEMFUNC_TYPE (type
), u
, (tree
*)0);
6045 return build_conditional_expr (e1
, e2
, e3
);
6048 ndelta
= TREE_VALUE (CONSTRUCTOR_ELTS (pfn
));
6049 nindex
= TREE_VALUE (TREE_CHAIN (CONSTRUCTOR_ELTS (pfn
)));
6050 npfn
= TREE_VALUE (TREE_CHAIN (TREE_CHAIN (CONSTRUCTOR_ELTS (pfn
))));
6051 npfn
= TREE_VALUE (CONSTRUCTOR_ELTS (npfn
));
6052 if (integer_zerop (nindex
))
6053 pfn
= integer_zero_node
;
6054 else if (integer_zerop (fold (size_binop (PLUS_EXPR
, nindex
, integer_one_node
))))
6057 delta
= get_delta_difference (TYPE_METHOD_BASETYPE (TREE_TYPE (TYPE_PTRMEMFUNC_FN_TYPE (TREE_TYPE (pfn
)))),
6058 TYPE_METHOD_BASETYPE (TREE_TYPE (type
)),
6060 delta
= build_binary_op (PLUS_EXPR
, delta
, ndelta
, 1);
6061 pfn
= build1 (NOP_EXPR
, type
, npfn
);
6062 TREE_CONSTANT (pfn
) = TREE_CONSTANT (npfn
);
6064 u
= build_nt (CONSTRUCTOR
, 0, tree_cons (pfn_identifier
, pfn
, NULL_TREE
));
6065 u
= build_nt (CONSTRUCTOR
, 0, tree_cons (NULL_TREE
, delta
,
6066 tree_cons (NULL_TREE
, nindex
,
6067 tree_cons (NULL_TREE
, u
, NULL_TREE
))));
6068 e3
= digest_init (TYPE_GET_PTRMEMFUNC_TYPE (type
), u
, (tree
*)0);
6073 sorry ("value casting of variable nonnull pointer to member functions not supported");
6074 return error_mark_node
;
6078 /* Handle null pointer to member function conversions. */
6079 if (integer_zerop (pfn
))
6081 pfn
= build_c_cast (type
, integer_zero_node
, 0);
6082 u
= build_nt (CONSTRUCTOR
, 0, tree_cons (pfn_identifier
, pfn
, NULL_TREE
));
6083 u
= build_nt (CONSTRUCTOR
, 0, tree_cons (NULL_TREE
, integer_zero_node
,
6084 tree_cons (NULL_TREE
, integer_zero_node
,
6085 tree_cons (NULL_TREE
, u
, NULL_TREE
))));
6086 return digest_init (TYPE_GET_PTRMEMFUNC_TYPE (type
), u
, (tree
*)0);
6089 if (TREE_CODE (pfn
) == TREE_LIST
6090 || (TREE_CODE (pfn
) == ADDR_EXPR
6091 && TREE_CODE (TREE_OPERAND (pfn
, 0)) == TREE_LIST
))
6093 pfn
= instantiate_type (type
, pfn
, 1);
6094 if (pfn
== error_mark_node
)
6095 return error_mark_node
;
6096 if (TREE_CODE (pfn
) != ADDR_EXPR
)
6097 pfn
= build_unary_op (ADDR_EXPR
, pfn
, 0);
6100 /* Allow pointer to member conversions here. */
6101 delta
= get_delta_difference (TYPE_METHOD_BASETYPE (TREE_TYPE (TREE_TYPE (pfn
))),
6102 TYPE_METHOD_BASETYPE (TREE_TYPE (type
)),
6104 delta2
= build_binary_op (PLUS_EXPR
, delta2
, delta
, 1);
6106 if (TREE_CODE (TREE_OPERAND (pfn
, 0)) != FUNCTION_DECL
)
6107 warning ("assuming pointer to member function is non-virtual");
6109 if (TREE_CODE (TREE_OPERAND (pfn
, 0)) == FUNCTION_DECL
6110 && DECL_VINDEX (TREE_OPERAND (pfn
, 0)))
6112 /* Find the offset to the vfield pointer in the object. */
6113 vfield_offset
= get_binfo (DECL_CONTEXT (TREE_OPERAND (pfn
, 0)),
6114 DECL_CLASS_CONTEXT (TREE_OPERAND (pfn
, 0)),
6116 vfield_offset
= get_vfield_offset (vfield_offset
);
6117 delta2
= size_binop (PLUS_EXPR
, vfield_offset
, delta2
);
6119 /* Map everything down one to make room for the null pointer to member. */
6120 index
= size_binop (PLUS_EXPR
,
6121 DECL_VINDEX (TREE_OPERAND (pfn
, 0)),
6123 u
= build_nt (CONSTRUCTOR
, 0, tree_cons (delta2_identifier
, delta2
, NULL_TREE
));
6127 index
= size_binop (MINUS_EXPR
, integer_zero_node
, integer_one_node
);
6129 npfn
= build1 (NOP_EXPR
, type
, pfn
);
6130 TREE_CONSTANT (npfn
) = TREE_CONSTANT (pfn
);
6132 u
= build_nt (CONSTRUCTOR
, 0, tree_cons (pfn_identifier
, npfn
, NULL_TREE
));
6135 u
= build_nt (CONSTRUCTOR
, 0, tree_cons (NULL_TREE
, delta
,
6136 tree_cons (NULL_TREE
, index
,
6137 tree_cons (NULL_TREE
, u
, NULL_TREE
))));
6138 return digest_init (TYPE_GET_PTRMEMFUNC_TYPE (type
), u
, (tree
*)0);
6141 /* Convert value RHS to type TYPE as preparation for an assignment
6142 to an lvalue of type TYPE.
6143 The real work of conversion is done by `convert'.
6144 The purpose of this function is to generate error messages
6145 for assignments that are not allowed in C.
6146 ERRTYPE is a string to use in error messages:
6147 "assignment", "return", etc.
6149 C++: attempts to allow `convert' to find conversions involving
6150 implicit type conversion between aggregate and scalar types
6151 as per 8.5.6 of C++ manual. Does not randomly dereference
6152 pointers to aggregates! */
6155 convert_for_assignment (type
, rhs
, errtype
, fndecl
, parmnum
)
6161 register enum tree_code codel
= TREE_CODE (type
);
6162 register tree rhstype
;
6163 register enum tree_code coder
= TREE_CODE (TREE_TYPE (rhs
));
6165 if (coder
== UNKNOWN_TYPE
)
6166 rhs
= instantiate_type (type
, rhs
, 1);
6168 if (coder
== ERROR_MARK
)
6169 return error_mark_node
;
6171 if (codel
== OFFSET_TYPE
)
6173 type
= TREE_TYPE (type
);
6174 codel
= TREE_CODE (type
);
6177 /* Strip NON_LVALUE_EXPRs since we aren't using as an lvalue. */
6178 if (TREE_CODE (rhs
) == NON_LVALUE_EXPR
)
6179 rhs
= TREE_OPERAND (rhs
, 0);
6181 if (rhs
== error_mark_node
)
6182 return error_mark_node
;
6184 if (TREE_VALUE (rhs
) == error_mark_node
)
6185 return error_mark_node
;
6187 if (TREE_CODE (TREE_TYPE (rhs
)) == OFFSET_TYPE
)
6189 rhs
= resolve_offset_ref (rhs
);
6190 if (rhs
== error_mark_node
)
6191 return error_mark_node
;
6192 rhstype
= TREE_TYPE (rhs
);
6193 coder
= TREE_CODE (rhstype
);
6196 if (TREE_CODE (TREE_TYPE (rhs
)) == ARRAY_TYPE
6197 || TREE_CODE (TREE_TYPE (rhs
)) == FUNCTION_TYPE
6198 || TREE_CODE (TREE_TYPE (rhs
)) == METHOD_TYPE
)
6199 rhs
= default_conversion (rhs
);
6200 else if (TREE_CODE (TREE_TYPE (rhs
)) == REFERENCE_TYPE
)
6201 rhs
= convert_from_reference (rhs
);
6203 rhstype
= TREE_TYPE (rhs
);
6204 coder
= TREE_CODE (rhstype
);
6206 /* This should no longer change types on us. */
6207 if (TREE_CODE (rhs
) == CONST_DECL
)
6208 rhs
= DECL_INITIAL (rhs
);
6209 else if (TREE_READONLY_DECL_P (rhs
))
6210 rhs
= decl_constant_value (rhs
);
6212 if (type
== rhstype
)
6214 overflow_warning (rhs
);
6218 if (coder
== VOID_TYPE
)
6220 error ("void value not ignored as it ought to be");
6221 return error_mark_node
;
6223 /* Arithmetic types all interconvert. */
6224 if ((codel
== INTEGER_TYPE
|| codel
== REAL_TYPE
|| codel
== BOOLEAN_TYPE
)
6225 && (coder
== INTEGER_TYPE
|| coder
== REAL_TYPE
|| coder
== BOOLEAN_TYPE
))
6227 /* But we should warn if assigning REAL_TYPE to INTEGER_TYPE. */
6228 if (coder
== REAL_TYPE
&& codel
== INTEGER_TYPE
)
6231 cp_warning ("`%T' used for argument %P of `%D'",
6232 rhstype
, parmnum
, fndecl
);
6234 cp_warning ("%s to `%T' from `%T'", errtype
, type
, rhstype
);
6236 /* And we should warn if assigning a negative value to
6237 an unsigned variable. */
6238 else if (TREE_UNSIGNED (type
) && codel
!= BOOLEAN_TYPE
)
6240 if (TREE_CODE (rhs
) == INTEGER_CST
6241 && TREE_NEGATED_INT (rhs
))
6244 cp_warning ("negative value `%E' passed as argument %P of `%D'",
6245 rhs
, parmnum
, fndecl
);
6247 cp_warning ("%s of negative value `%E' to `%T'",
6248 errtype
, rhs
, type
);
6250 overflow_warning (rhs
);
6251 if (TREE_CONSTANT (rhs
))
6255 return convert_and_check (type
, rhs
);
6257 /* Conversions involving enums. */
6258 else if ((codel
== ENUMERAL_TYPE
6259 && (INTEGRAL_CODE_P (coder
) || coder
== REAL_TYPE
))
6260 || (coder
== ENUMERAL_TYPE
6261 && (INTEGRAL_CODE_P (codel
) || codel
== REAL_TYPE
)))
6263 return cp_convert (type
, rhs
, CONV_IMPLICIT
, LOOKUP_NORMAL
);
6265 /* Conversions among pointers */
6266 else if (codel
== POINTER_TYPE
6267 && (coder
== POINTER_TYPE
6268 || (coder
== RECORD_TYPE
6269 && (IS_SIGNATURE_POINTER (rhstype
)
6270 || IS_SIGNATURE_REFERENCE (rhstype
)))))
6272 register tree ttl
= TREE_TYPE (type
);
6276 if (coder
== RECORD_TYPE
)
6278 rhs
= build_optr_ref (rhs
);
6279 rhstype
= TREE_TYPE (rhs
);
6281 ttr
= TREE_TYPE (rhstype
);
6283 /* If both pointers are of aggregate type, then we
6284 can give better error messages, and save some work
6286 if (TREE_CODE (ttl
) == RECORD_TYPE
&& TREE_CODE (ttr
) == RECORD_TYPE
)
6290 if (TYPE_MAIN_VARIANT (ttl
) == TYPE_MAIN_VARIANT (ttr
)
6291 || type
== class_star_type_node
6292 || rhstype
== class_star_type_node
)
6293 binfo
= TYPE_BINFO (ttl
);
6295 binfo
= get_binfo (ttl
, ttr
, 1);
6297 if (binfo
== error_mark_node
)
6298 return error_mark_node
;
6300 return error_not_base_type (ttl
, ttr
);
6302 if (! TYPE_READONLY (ttl
) && TYPE_READONLY (ttr
))
6305 cp_pedwarn ("passing `%T' as argument %P of `%D' discards const",
6306 rhstype
, parmnum
, fndecl
);
6308 cp_pedwarn ("%s to `%T' from `%T' discards const",
6309 errtype
, type
, rhstype
);
6311 if (! TYPE_VOLATILE (ttl
) && TYPE_VOLATILE (ttr
))
6314 cp_pedwarn ("passing `%T' as argument %P of `%D' discards volatile",
6315 rhstype
, parmnum
, fndecl
);
6317 cp_pedwarn ("%s to `%T' from `%T' discards volatile",
6318 errtype
, type
, rhstype
);
6322 /* Any non-function converts to a [const][volatile] void *
6323 and vice versa; otherwise, targets must be the same.
6324 Meanwhile, the lhs target must have all the qualifiers of the rhs. */
6325 else if (TYPE_MAIN_VARIANT (ttl
) == void_type_node
6326 || TYPE_MAIN_VARIANT (ttr
) == void_type_node
6327 || (ctt
= comp_target_types (type
, rhstype
, 1))
6328 || (unsigned_type (TYPE_MAIN_VARIANT (ttl
))
6329 == unsigned_type (TYPE_MAIN_VARIANT (ttr
))))
6331 /* ARM $4.8, commentary on p39. */
6332 if (TYPE_MAIN_VARIANT (ttl
) == void_type_node
6333 && TREE_CODE (ttr
) == OFFSET_TYPE
)
6335 cp_error ("no standard conversion from `%T' to `void *'", ttr
);
6336 return error_mark_node
;
6340 cp_pedwarn ("converting `%T' to `%T' is a contravariance violation",
6343 if (TYPE_MAIN_VARIANT (ttl
) != void_type_node
6344 && TYPE_MAIN_VARIANT (ttr
) == void_type_node
6345 && rhs
!= null_pointer_node
)
6347 if (coder
== RECORD_TYPE
)
6348 cp_pedwarn ("implicit conversion of signature pointer to type `%T'",
6351 pedwarn ("ANSI C++ forbids implicit conversion from `void *' in %s",
6354 /* Const and volatile mean something different for function types,
6355 so the usual warnings are not appropriate. */
6356 else if ((TREE_CODE (ttr
) != FUNCTION_TYPE
&& TREE_CODE (ttr
) != METHOD_TYPE
)
6357 || (TREE_CODE (ttl
) != FUNCTION_TYPE
&& TREE_CODE (ttl
) != METHOD_TYPE
))
6359 if (TREE_CODE (ttl
) == OFFSET_TYPE
6360 && binfo_member (TYPE_OFFSET_BASETYPE (ttr
),
6361 CLASSTYPE_VBASECLASSES (TYPE_OFFSET_BASETYPE (ttl
))))
6363 sorry ("%s between pointer to members converting across virtual baseclasses", errtype
);
6364 return error_mark_node
;
6366 else if (! TYPE_READONLY (ttl
) && TYPE_READONLY (ttr
))
6369 cp_pedwarn ("passing `%T' as argument %P of `%D' discards const",
6370 rhstype
, parmnum
, fndecl
);
6372 cp_pedwarn ("%s to `%T' from `%T' discards const",
6373 errtype
, type
, rhstype
);
6375 else if (! TYPE_VOLATILE (ttl
) && TYPE_VOLATILE (ttr
))
6378 cp_pedwarn ("passing `%T' as argument %P of `%D' discards volatile",
6379 rhstype
, parmnum
, fndecl
);
6381 cp_pedwarn ("%s to `%T' from `%T' discards volatile",
6382 errtype
, type
, rhstype
);
6384 else if (TREE_CODE (ttl
) == TREE_CODE (ttr
)
6385 && ! comp_target_types (type
, rhstype
, 1))
6388 cp_pedwarn ("passing `%T' as argument %P of `%D' changes signedness",
6389 rhstype
, parmnum
, fndecl
);
6391 cp_pedwarn ("%s to `%T' from `%T' changes signedness",
6392 errtype
, type
, rhstype
);
6396 else if (TREE_CODE (ttr
) == OFFSET_TYPE
6397 && TREE_CODE (ttl
) != OFFSET_TYPE
)
6399 /* Normally, pointers to different type codes (other
6400 than void) are not compatible, but we perform
6401 some type instantiation if that resolves the
6402 ambiguity of (X Y::*) and (X *). */
6404 if (current_class_decl
)
6406 if (TREE_CODE (rhs
) == INTEGER_CST
)
6408 rhs
= build (PLUS_EXPR
, build_pointer_type (TREE_TYPE (ttr
)),
6409 current_class_decl
, rhs
);
6410 return convert_for_assignment (type
, rhs
,
6411 errtype
, fndecl
, parmnum
);
6414 if (TREE_CODE (ttl
) == METHOD_TYPE
)
6415 error ("%s between pointer-to-method and pointer-to-member types",
6418 error ("%s between pointer and pointer-to-member types", errtype
);
6419 return error_mark_node
;
6423 int add_quals
= 0, const_parity
= 0, volatile_parity
= 0;
6425 int unsigned_parity
;
6428 /* This code is basically a duplicate of comp_ptr_ttypes_real. */
6429 for (; ; ttl
= TREE_TYPE (ttl
), ttr
= TREE_TYPE (ttr
))
6432 const_parity
|= (TYPE_READONLY (ttl
) < TYPE_READONLY (ttr
));
6433 volatile_parity
|= (TYPE_VOLATILE (ttl
) < TYPE_VOLATILE (ttr
));
6436 && (TYPE_READONLY (ttl
) > TYPE_READONLY (ttr
)
6437 || TYPE_VOLATILE (ttl
) > TYPE_VOLATILE (ttr
)))
6439 left_const
&= TYPE_READONLY (ttl
);
6441 if (TREE_CODE (ttl
) != POINTER_TYPE
6442 || TREE_CODE (ttr
) != POINTER_TYPE
)
6445 unsigned_parity
= TREE_UNSIGNED (ttl
) - TREE_UNSIGNED (ttr
);
6446 if (unsigned_parity
)
6448 if (TREE_UNSIGNED (ttl
))
6449 ttr
= unsigned_type (ttr
);
6451 ttl
= unsigned_type (ttl
);
6454 if (comp_target_types (ttl
, ttr
, nptrs
) > 0)
6459 cp_pedwarn ("passing `%T' as argument %P of `%D' adds cv-quals without intervening `const'",
6460 rhstype
, parmnum
, fndecl
);
6462 cp_pedwarn ("%s to `%T' from `%T' adds cv-quals without intervening `const'",
6463 errtype
, type
, rhstype
);
6468 cp_pedwarn ("passing `%T' as argument %P of `%D' discards const",
6469 rhstype
, parmnum
, fndecl
);
6471 cp_pedwarn ("%s to `%T' from `%T' discards const",
6472 errtype
, type
, rhstype
);
6474 if (volatile_parity
)
6477 cp_pedwarn ("passing `%T' as argument %P of `%D' discards volatile",
6478 rhstype
, parmnum
, fndecl
);
6480 cp_pedwarn ("%s to `%T' from `%T' discards volatile",
6481 errtype
, type
, rhstype
);
6483 if (unsigned_parity
> 0)
6486 cp_pedwarn ("passing `%T' as argument %P of `%D' changes signed to unsigned",
6487 rhstype
, parmnum
, fndecl
);
6489 cp_pedwarn ("%s to `%T' from `%T' changes signed to unsigned",
6490 errtype
, type
, rhstype
);
6492 else if (unsigned_parity
< 0)
6495 cp_pedwarn ("passing `%T' as argument %P of `%D' changes unsigned to signed",
6496 rhstype
, parmnum
, fndecl
);
6498 cp_pedwarn ("%s to `%T' from `%T' changes unsigned to signed",
6499 errtype
, type
, rhstype
);
6502 /* C++ is not so friendly about converting function and
6503 member function pointers as C. Emit warnings here. */
6504 if (TREE_CODE (ttl
) == FUNCTION_TYPE
6505 || TREE_CODE (ttl
) == METHOD_TYPE
)
6506 if (! comptypes (ttl
, ttr
, 0))
6508 warning ("conflicting function types in %s:", errtype
);
6509 cp_warning ("\t`%T' != `%T'", type
, rhstype
);
6512 else if (TREE_CODE (TREE_TYPE (rhs
)) == METHOD_TYPE
)
6514 /* When does this happen? */
6515 my_friendly_abort (119);
6516 /* Conversion of a pointer-to-member type to void *. */
6517 rhs
= build_unary_op (ADDR_EXPR
, rhs
, 0);
6518 TREE_TYPE (rhs
) = type
;
6521 else if (TREE_CODE (TREE_TYPE (rhs
)) == OFFSET_TYPE
)
6523 /* When does this happen? */
6524 my_friendly_abort (120);
6525 /* Conversion of a pointer-to-member type to void *. */
6526 rhs
= build_unary_op (ADDR_EXPR
, rhs
, 0);
6527 TREE_TYPE (rhs
) = type
;
6533 cp_error ("passing `%T' as argument %P of `%D'",
6534 rhstype
, parmnum
, fndecl
);
6536 cp_error ("%s to `%T' from `%T'", errtype
, type
, rhstype
);
6537 return error_mark_node
;
6540 return convert (type
, rhs
);
6542 else if (codel
== POINTER_TYPE
&& coder
== INTEGER_TYPE
)
6544 /* An explicit constant 0 can convert to a pointer,
6545 but not a 0 that results from casting or folding. */
6546 if (! (TREE_CODE (rhs
) == INTEGER_CST
&& integer_zerop (rhs
)))
6549 cp_pedwarn ("passing `%T' to argument %P of `%D' lacks a cast",
6550 rhstype
, parmnum
, fndecl
);
6552 cp_pedwarn ("%s to `%T' from `%T' lacks a cast",
6553 errtype
, type
, rhstype
);
6554 return convert (type
, rhs
);
6556 return null_pointer_node
;
6558 else if (codel
== INTEGER_TYPE
6559 && (coder
== POINTER_TYPE
6560 || (coder
== RECORD_TYPE
6561 && (IS_SIGNATURE_POINTER (rhstype
)
6562 || TYPE_PTRMEMFUNC_FLAG (rhstype
)
6563 || IS_SIGNATURE_REFERENCE (rhstype
)))))
6566 cp_pedwarn ("passing `%T' to argument %P of `%D' lacks a cast",
6567 rhstype
, parmnum
, fndecl
);
6569 cp_pedwarn ("%s to `%T' from `%T' lacks a cast",
6570 errtype
, type
, rhstype
);
6571 return convert (type
, rhs
);
6573 else if (codel
== BOOLEAN_TYPE
6574 && (coder
== POINTER_TYPE
6575 || (coder
== RECORD_TYPE
6576 && (IS_SIGNATURE_POINTER (rhstype
)
6577 || TYPE_PTRMEMFUNC_FLAG (rhstype
)
6578 || IS_SIGNATURE_REFERENCE (rhstype
)))))
6579 return convert (type
, rhs
);
6582 else if (((coder
== POINTER_TYPE
6583 && TREE_CODE (TREE_TYPE (rhstype
)) == METHOD_TYPE
)
6584 || integer_zerop (rhs
)
6585 || TYPE_PTRMEMFUNC_P (rhstype
))
6586 && TYPE_PTRMEMFUNC_P (type
))
6588 tree ttl
= TYPE_PTRMEMFUNC_FN_TYPE (type
);
6589 tree ttr
= (TREE_CODE (rhstype
) == POINTER_TYPE
? rhstype
6590 : TYPE_PTRMEMFUNC_FN_TYPE (type
));
6591 int ctt
= comp_target_types (ttl
, ttr
, 1);
6594 cp_pedwarn ("converting `%T' to `%T' is a contravariance violation",
6597 cp_error ("%s to `%T' from `%T'", errtype
, ttl
, ttr
);
6599 /* compatible pointer to member functions. */
6600 return build_ptrmemfunc (ttl
, rhs
, 0);
6602 else if (codel
== ERROR_MARK
|| coder
== ERROR_MARK
)
6603 return error_mark_node
;
6605 /* This should no longer happen. References are initialized via
6606 `convert_for_initialization'. They should otherwise be
6607 bashed before coming here. */
6608 else if (codel
== REFERENCE_TYPE
)
6609 my_friendly_abort (317);
6610 else if (TYPE_MAIN_VARIANT (type
) == TYPE_MAIN_VARIANT (TREE_TYPE (rhs
)))
6612 tree nrhs
= build1 (NOP_EXPR
, type
, rhs
);
6613 TREE_CONSTANT (nrhs
) = TREE_CONSTANT (rhs
);
6616 else if (TYPE_HAS_CONSTRUCTOR (type
) || IS_AGGR_TYPE (TREE_TYPE (rhs
)))
6617 return convert (type
, rhs
);
6619 cp_error ("%s to `%T' from `%T'", errtype
, type
, rhstype
);
6620 return error_mark_node
;
6623 /* Convert RHS to be of type TYPE. If EXP is non-zero,
6624 it is the target of the initialization.
6625 ERRTYPE is a string to use in error messages.
6627 Two major differences between the behavior of
6628 `convert_for_assignment' and `convert_for_initialization'
6629 are that references are bashed in the former, while
6630 copied in the latter, and aggregates are assigned in
6631 the former (operator=) while initialized in the
6634 If using constructor make sure no conversion operator exists, if one does
6635 exist, an ambiguity exists.
6637 If flags doesn't include LOOKUP_COMPLAIN, don't complain about anything. */
6639 convert_for_initialization (exp
, type
, rhs
, flags
, errtype
, fndecl
, parmnum
)
6640 tree exp
, type
, rhs
;
6646 register enum tree_code codel
= TREE_CODE (type
);
6647 register tree rhstype
;
6648 register enum tree_code coder
;
6650 /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
6651 Strip such NOP_EXPRs, since RHS is used in non-lvalue context. */
6652 if (TREE_CODE (rhs
) == NOP_EXPR
6653 && TREE_TYPE (rhs
) == TREE_TYPE (TREE_OPERAND (rhs
, 0))
6654 && codel
!= REFERENCE_TYPE
)
6655 rhs
= TREE_OPERAND (rhs
, 0);
6657 if (rhs
== error_mark_node
6658 || (TREE_CODE (rhs
) == TREE_LIST
&& TREE_VALUE (rhs
) == error_mark_node
))
6659 return error_mark_node
;
6661 if (TREE_CODE (TREE_TYPE (rhs
)) == OFFSET_TYPE
)
6663 rhs
= resolve_offset_ref (rhs
);
6664 if (rhs
== error_mark_node
)
6665 return error_mark_node
;
6668 if (TREE_CODE (TREE_TYPE (rhs
)) == REFERENCE_TYPE
)
6669 rhs
= convert_from_reference (rhs
);
6671 if ((TREE_CODE (TREE_TYPE (rhs
)) == ARRAY_TYPE
6672 && TREE_CODE (type
) != ARRAY_TYPE
6673 && (TREE_CODE (type
) != REFERENCE_TYPE
6674 || TREE_CODE (TREE_TYPE (type
)) != ARRAY_TYPE
))
6675 || TREE_CODE (TREE_TYPE (rhs
)) == FUNCTION_TYPE
6676 || TREE_CODE (TREE_TYPE (rhs
)) == METHOD_TYPE
)
6677 rhs
= default_conversion (rhs
);
6679 rhstype
= TREE_TYPE (rhs
);
6680 coder
= TREE_CODE (rhstype
);
6682 if (coder
== UNKNOWN_TYPE
)
6684 rhs
= instantiate_type (type
, rhs
, 1);
6685 rhstype
= TREE_TYPE (rhs
);
6686 coder
= TREE_CODE (rhstype
);
6689 if (coder
== ERROR_MARK
)
6690 return error_mark_node
;
6692 /* We accept references to incomplete types, so we can
6693 return here before checking if RHS is of complete type. */
6695 if (codel
== REFERENCE_TYPE
)
6697 /* This should eventually happen in convert_arguments. */
6698 extern int warningcount
, errorcount
;
6702 savew
= warningcount
, savee
= errorcount
;
6703 rhs
= convert_to_reference (type
, rhs
, CONV_IMPLICIT
, flags
,
6704 exp
? exp
: error_mark_node
);
6707 if (warningcount
> savew
)
6708 cp_warning_at ("in passing argument %P of `%+D'", parmnum
, fndecl
);
6709 else if (errorcount
> savee
)
6710 cp_error_at ("in passing argument %P of `%+D'", parmnum
, fndecl
);
6715 rhs
= require_complete_type (rhs
);
6716 if (rhs
== error_mark_node
)
6717 return error_mark_node
;
6719 if (exp
!= 0) exp
= require_complete_type (exp
);
6720 if (exp
== error_mark_node
)
6721 return error_mark_node
;
6723 if (TREE_CODE (rhstype
) == REFERENCE_TYPE
)
6724 rhstype
= TREE_TYPE (rhstype
);
6726 if (TYPE_LANG_SPECIFIC (type
)
6727 && (IS_SIGNATURE_POINTER (type
) || IS_SIGNATURE_REFERENCE (type
)))
6728 return build_signature_pointer_constructor (type
, rhs
);
6730 if (IS_AGGR_TYPE (type
)
6731 && (TYPE_NEEDS_CONSTRUCTING (type
) || TREE_HAS_CONSTRUCTOR (rhs
)))
6733 if (TYPE_MAIN_VARIANT (type
) == TYPE_MAIN_VARIANT (rhstype
))
6735 /* This is sufficient to perform initialization. No need,
6736 apparently, to go through X(X&) to do first-cut
6737 initialization. Return through a TARGET_EXPR so that we get
6738 cleanups if it is used. */
6739 if (TREE_CODE (rhs
) == CALL_EXPR
)
6741 rhs
= build_cplus_new (type
, rhs
);
6744 /* Handle the case of default parameter initialization and
6745 initialization of static variables. */
6746 else if (TREE_CODE (rhs
) == TARGET_EXPR
)
6748 else if (TREE_CODE (rhs
) == INDIRECT_REF
&& TREE_HAS_CONSTRUCTOR (rhs
))
6750 my_friendly_assert (TREE_CODE (TREE_OPERAND (rhs
, 0)) == CALL_EXPR
, 318);
6753 my_friendly_assert (TREE_VALUE (TREE_OPERAND (TREE_OPERAND (rhs
, 0), 1)) == NULL_TREE
, 316);
6754 TREE_VALUE (TREE_OPERAND (TREE_OPERAND (rhs
, 0), 1))
6755 = build_unary_op (ADDR_EXPR
, exp
, 0);
6758 rhs
= build_cplus_new (type
, TREE_OPERAND (rhs
, 0));
6761 else if (TYPE_HAS_TRIVIAL_INIT_REF (type
))
6764 if (TYPE_MAIN_VARIANT (type
) == TYPE_MAIN_VARIANT (rhstype
)
6765 || (IS_AGGR_TYPE (rhstype
) && UNIQUELY_DERIVED_FROM_P (type
, rhstype
)))
6767 if (TYPE_HAS_INIT_REF (type
))
6769 tree init
= build_method_call (exp
, ctor_identifier
,
6770 build_tree_list (NULL_TREE
, rhs
),
6771 TYPE_BINFO (type
), LOOKUP_NORMAL
);
6773 if (init
== error_mark_node
)
6774 return error_mark_node
;
6778 exp
= build_cplus_new (type
, init
);
6782 return build (COMPOUND_EXPR
, type
, init
, exp
);
6785 /* ??? The following warnings are turned off because
6786 this is another place where the default X(X&) constructor
6788 if (TYPE_HAS_ASSIGNMENT (type
))
6789 cp_warning ("bitwise copy: `%T' defines operator=", type
);
6791 if (TREE_CODE (TREE_TYPE (rhs
)) == REFERENCE_TYPE
)
6792 rhs
= convert_from_reference (rhs
);
6793 if (type
!= rhstype
)
6795 tree nrhs
= build1 (NOP_EXPR
, type
, rhs
);
6796 TREE_CONSTANT (nrhs
) = TREE_CONSTANT (rhs
);
6802 return cp_convert (type
, rhs
, CONV_OLD_CONVERT
,
6803 flags
| LOOKUP_NO_CONVERSION
);
6806 if (type
== TREE_TYPE (rhs
))
6808 if (TREE_READONLY_DECL_P (rhs
))
6809 rhs
= decl_constant_value (rhs
);
6813 return convert_for_assignment (type
, rhs
, errtype
, fndecl
, parmnum
);
6816 /* Expand an ASM statement with operands, handling output operands
6817 that are not variables or INDIRECT_REFS by transforming such
6818 cases into cases that expand_asm_operands can handle.
6820 Arguments are same as for expand_asm_operands.
6822 We don't do default conversions on all inputs, because it can screw
6823 up operands that are expected to be in memory. */
6826 c_expand_asm_operands (string
, outputs
, inputs
, clobbers
, vol
, filename
, line
)
6827 tree string
, outputs
, inputs
, clobbers
;
6832 int noutputs
= list_length (outputs
);
6834 /* o[I] is the place that output number I should be written. */
6835 register tree
*o
= (tree
*) alloca (noutputs
* sizeof (tree
));
6838 /* Record the contents of OUTPUTS before it is modified. */
6839 for (i
= 0, tail
= outputs
; tail
; tail
= TREE_CHAIN (tail
), i
++)
6840 o
[i
] = TREE_VALUE (tail
);
6842 /* Generate the ASM_OPERANDS insn;
6843 store into the TREE_VALUEs of OUTPUTS some trees for
6844 where the values were actually stored. */
6845 expand_asm_operands (string
, outputs
, inputs
, clobbers
, vol
, filename
, line
);
6847 /* Copy all the intermediate outputs into the specified outputs. */
6848 for (i
= 0, tail
= outputs
; tail
; tail
= TREE_CHAIN (tail
), i
++)
6850 if (o
[i
] != TREE_VALUE (tail
))
6852 expand_expr (build_modify_expr (o
[i
], NOP_EXPR
, TREE_VALUE (tail
)),
6853 const0_rtx
, VOIDmode
, 0);
6856 /* Detect modification of read-only values.
6857 (Otherwise done by build_modify_expr.) */
6860 tree type
= TREE_TYPE (o
[i
]);
6861 if (TYPE_READONLY (type
)
6862 || ((TREE_CODE (type
) == RECORD_TYPE
6863 || TREE_CODE (type
) == UNION_TYPE
)
6864 && C_TYPE_FIELDS_READONLY (type
)))
6865 readonly_error (o
[i
], "modification by `asm'", 1);
6869 /* Those MODIFY_EXPRs could do autoincrements. */
6873 /* Expand a C `return' statement.
6874 RETVAL is the expression for what to return,
6875 or a null pointer for `return;' with no value.
6877 C++: upon seeing a `return', we must call destructors on all
6878 variables in scope which had constructors called on them.
6879 This means that if in a destructor, the base class destructors
6880 must be called before returning.
6882 The RETURN statement in C++ has initialization semantics. */
6885 c_expand_return (retval
)
6888 extern struct nesting
*cond_stack
, *loop_stack
, *case_stack
;
6889 extern tree dtor_label
, ctor_label
;
6890 tree result
= DECL_RESULT (current_function_decl
);
6891 tree valtype
= TREE_TYPE (result
);
6892 int returns_value
= 1;
6894 if (TREE_THIS_VOLATILE (current_function_decl
))
6895 warning ("function declared `noreturn' has a `return' statement");
6897 if (retval
== error_mark_node
)
6899 current_function_returns_null
= 1;
6903 if (current_template_parms
)
6905 add_tree (build_min_nt (RETURN_STMT
, retval
));
6909 if (retval
== NULL_TREE
)
6911 /* A non-named return value does not count. */
6913 /* Can't just return from a destructor. */
6916 expand_goto (dtor_label
);
6920 if (DECL_CONSTRUCTOR_P (current_function_decl
))
6921 retval
= current_class_decl
;
6922 else if (DECL_NAME (result
) != NULL_TREE
6923 && TREE_CODE (valtype
) != VOID_TYPE
)
6927 current_function_returns_null
= 1;
6929 if (valtype
!= NULL_TREE
&& TREE_CODE (valtype
) != VOID_TYPE
)
6931 if (DECL_NAME (DECL_RESULT (current_function_decl
)) == NULL_TREE
)
6933 pedwarn ("`return' with no value, in function returning non-void");
6934 /* Clear this, so finish_function won't say that we
6935 reach the end of a non-void function (which we don't,
6936 we gave a return!). */
6937 current_function_returns_null
= 0;
6941 expand_null_return ();
6945 else if (DECL_CONSTRUCTOR_P (current_function_decl
)
6946 && retval
!= current_class_decl
)
6948 error ("return from a constructor: use `this = ...' instead");
6949 retval
= current_class_decl
;
6952 if (valtype
== NULL_TREE
|| TREE_CODE (valtype
) == VOID_TYPE
)
6954 current_function_returns_null
= 1;
6955 /* We do this here so we'll avoid a warning about how the function
6956 "may or may not return a value" in finish_function. */
6960 pedwarn ("`return' with a value, in function returning void");
6961 expand_return (retval
);
6963 /* Add some useful error checking for C++. */
6964 else if (TREE_CODE (valtype
) == REFERENCE_TYPE
)
6966 tree whats_returned
;
6967 tree tmp_result
= result
;
6969 /* Don't initialize directly into a non-BLKmode retval, since that
6970 could lose when being inlined by another caller. (GCC can't
6971 read the function return register in an inline function when
6972 the return value is being ignored). */
6973 if (result
&& TYPE_MODE (TREE_TYPE (tmp_result
)) != BLKmode
)
6976 /* convert to reference now, so we can give error if we
6977 return an reference to a non-lvalue. */
6978 retval
= convert_for_initialization (tmp_result
, valtype
, retval
,
6979 LOOKUP_NORMAL
, "return",
6982 /* Sort through common things to see what it is
6983 we are returning. */
6984 whats_returned
= retval
;
6985 if (TREE_CODE (whats_returned
) == COMPOUND_EXPR
)
6987 whats_returned
= TREE_OPERAND (whats_returned
, 1);
6988 if (TREE_CODE (whats_returned
) == ADDR_EXPR
)
6989 whats_returned
= TREE_OPERAND (whats_returned
, 0);
6991 if (TREE_CODE (whats_returned
) == ADDR_EXPR
)
6993 whats_returned
= TREE_OPERAND (whats_returned
, 0);
6994 while (TREE_CODE (whats_returned
) == NEW_EXPR
6995 || TREE_CODE (whats_returned
) == TARGET_EXPR
)
6997 /* Get the target. */
6998 whats_returned
= TREE_OPERAND (whats_returned
, 0);
6999 warning ("returning reference to temporary");
7003 if (TREE_CODE (whats_returned
) == VAR_DECL
&& DECL_NAME (whats_returned
))
7005 if (TEMP_NAME_P (DECL_NAME (whats_returned
)))
7006 warning ("reference to non-lvalue returned");
7007 else if (! TREE_STATIC (whats_returned
)
7008 && IDENTIFIER_LOCAL_VALUE (DECL_NAME (whats_returned
))
7009 && !TREE_PUBLIC (whats_returned
))
7010 cp_warning_at ("reference to local variable `%D' returned", whats_returned
);
7013 else if (TREE_CODE (retval
) == ADDR_EXPR
)
7015 tree whats_returned
= TREE_OPERAND (retval
, 0);
7017 if (TREE_CODE (whats_returned
) == VAR_DECL
7018 && DECL_NAME (whats_returned
)
7019 && IDENTIFIER_LOCAL_VALUE (DECL_NAME (whats_returned
))
7020 && !TREE_STATIC (whats_returned
)
7021 && !TREE_PUBLIC (whats_returned
))
7022 cp_warning_at ("address of local variable `%D' returned", whats_returned
);
7024 else if (TREE_CODE (retval
) == VAR_DECL
)
7026 if (TREE_CODE (TREE_TYPE (retval
)) == ARRAY_TYPE
7027 && DECL_NAME (retval
)
7028 && IDENTIFIER_LOCAL_VALUE (DECL_NAME (retval
))
7029 && !TREE_STATIC (retval
)
7030 && !TREE_PUBLIC (retval
))
7031 cp_warning_at ("address of local array `%D' returned", retval
);
7034 /* Now deal with possible C++ hair:
7035 (1) Compute the return value.
7036 (2) If there are aggregate values with destructors which
7037 must be cleaned up, clean them (taking care
7038 not to clobber the return value).
7039 (3) If an X(X&) constructor is defined, the return
7040 value must be returned via that. */
7042 /* If we're returning in a register, we can't initialize the
7043 return value from a TARGET_EXPR. */
7044 if (TREE_CODE (retval
) == TARGET_EXPR
7045 && TYPE_MAIN_VARIANT (TREE_TYPE (retval
)) == TYPE_MAIN_VARIANT (valtype
)
7046 && ! current_function_returns_struct
)
7047 retval
= expand_target_expr (retval
);
7049 if (retval
== result
7050 /* Watch out for constructors, which "return" aggregates
7051 via initialization, but which otherwise "return" a pointer. */
7052 || DECL_CONSTRUCTOR_P (current_function_decl
))
7054 /* This is just an error--it's already been reported. */
7055 if (TYPE_SIZE (valtype
) == NULL_TREE
)
7058 if (TYPE_MODE (valtype
) != BLKmode
7059 && any_pending_cleanups (1))
7060 retval
= get_temp_regvar (valtype
, retval
);
7062 else if (IS_AGGR_TYPE (valtype
) && current_function_returns_struct
)
7064 expand_aggr_init (result
, retval
, 0, LOOKUP_ONLYCONVERTING
);
7065 expand_cleanups_to (NULL_TREE
);
7066 DECL_INITIAL (result
) = NULL_TREE
;
7071 if (TYPE_MODE (valtype
) == VOIDmode
)
7073 if (TYPE_MODE (TREE_TYPE (result
)) != VOIDmode
7074 && warn_return_type
)
7075 warning ("return of void value in function returning non-void");
7076 expand_expr_stmt (retval
);
7080 else if (TYPE_MODE (valtype
) != BLKmode
7081 && any_pending_cleanups (1))
7083 retval
= get_temp_regvar (valtype
, retval
);
7084 expand_cleanups_to (NULL_TREE
);
7089 retval
= convert_for_initialization (result
, valtype
, retval
,
7091 "return", NULL_TREE
, 0);
7092 DECL_INITIAL (result
) = NULL_TREE
;
7094 if (retval
== error_mark_node
)
7100 if (retval
!= NULL_TREE
7101 && TREE_CODE_CLASS (TREE_CODE (retval
)) == 'd'
7102 && cond_stack
== 0 && loop_stack
== 0 && case_stack
== 0)
7103 current_function_return_value
= retval
;
7107 /* Everything's great--RETVAL is in RESULT. */
7108 if (original_result_rtx
)
7110 store_expr (result
, original_result_rtx
, 0);
7111 expand_cleanups_to (NULL_TREE
);
7112 use_variable (DECL_RTL (result
));
7113 if (ctor_label
&& TREE_CODE (ctor_label
) != ERROR_MARK
)
7114 expand_goto (ctor_label
);
7116 expand_null_return ();
7118 else if (retval
&& retval
!= result
)
7120 /* Clear this out so the later call to decl_function_context
7121 won't end up bombing on us. */
7122 if (DECL_CONTEXT (result
) == error_mark_node
)
7123 DECL_CONTEXT (result
) = NULL_TREE
;
7124 /* Here is where we finally get RETVAL into RESULT.
7125 `expand_return' does the magic of protecting
7126 RESULT from cleanups. */
7127 retval
= fold (build1 (CLEANUP_POINT_EXPR
, TREE_TYPE (result
),
7129 /* This part _must_ come second, because expand_return looks for
7130 the INIT_EXPR as the toplevel node only. :-( */
7131 retval
= build (INIT_EXPR
, TREE_TYPE (result
), result
, retval
);
7132 TREE_SIDE_EFFECTS (retval
) = 1;
7133 expand_return (retval
);
7136 expand_return (result
);
7140 /* We may still need to put RETVAL into RESULT. */
7141 result
= DECL_RESULT (current_function_decl
);
7142 if (original_result_rtx
)
7144 /* Here we have a named return value that went
7145 into memory. We can compute RETVAL into that. */
7147 expand_assignment (result
, retval
, 0, 0);
7149 store_expr (result
, original_result_rtx
, 0);
7150 result
= make_tree (TREE_TYPE (result
), original_result_rtx
);
7152 else if (ctor_label
&& TREE_CODE (ctor_label
) != ERROR_MARK
)
7154 /* Here RETVAL is CURRENT_CLASS_DECL, so there's nothing to do. */
7155 expand_goto (ctor_label
);
7159 /* Here is where we finally get RETVAL into RESULT.
7160 `expand_return' does the magic of protecting
7161 RESULT from cleanups. */
7162 result
= build (INIT_EXPR
, TREE_TYPE (result
), result
, retval
);
7163 TREE_SIDE_EFFECTS (result
) = 1;
7164 expand_return (result
);
7166 else if (TYPE_MODE (TREE_TYPE (result
)) != VOIDmode
)
7167 expand_return (result
);
7170 current_function_returns_value
= returns_value
;
7172 /* One way to clear out cleanups that EXPR might
7173 generate. Note that this code will really be
7174 dead code, but that is ok--cleanups that were
7175 needed were handled by the magic of `return'. */
7176 expand_cleanups_to (NULL_TREE
);
7179 /* Start a C switch statement, testing expression EXP.
7180 Return EXP if it is valid, an error node otherwise. */
7183 c_expand_start_case (exp
)
7187 register enum tree_code code
;
7189 /* Convert from references, etc. */
7190 exp
= default_conversion (exp
);
7191 type
= TREE_TYPE (exp
);
7192 code
= TREE_CODE (type
);
7194 if (IS_AGGR_TYPE_CODE (code
))
7195 exp
= build_type_conversion (CONVERT_EXPR
, integer_type_node
, exp
, 1);
7197 if (exp
== NULL_TREE
)
7199 error ("switch quantity not an integer");
7200 exp
= error_mark_node
;
7202 type
= TREE_TYPE (exp
);
7203 code
= TREE_CODE (type
);
7205 if (code
!= INTEGER_TYPE
&& code
!= ENUMERAL_TYPE
&& code
!= ERROR_MARK
)
7207 error ("switch quantity not an integer");
7208 exp
= error_mark_node
;
7214 exp
= default_conversion (exp
);
7215 type
= TREE_TYPE (exp
);
7216 index
= get_unwidened (exp
, 0);
7217 /* We can't strip a conversion from a signed type to an unsigned,
7218 because if we did, int_fits_type_p would do the wrong thing
7219 when checking case values for being in range,
7220 and it's too hard to do the right thing. */
7221 if (TREE_UNSIGNED (TREE_TYPE (exp
))
7222 == TREE_UNSIGNED (TREE_TYPE (index
)))
7227 (1, fold (build1 (CLEANUP_POINT_EXPR
, TREE_TYPE (exp
), exp
)),
7228 type
, "switch statement");
7233 /* CONSTP remembers whether or not all the intervening pointers in the `to'
7234 type have been const. */
7236 comp_ptr_ttypes_real (to
, from
, constp
)
7240 for (; ; to
= TREE_TYPE (to
), from
= TREE_TYPE (from
))
7242 if (TREE_CODE (to
) != TREE_CODE (from
))
7245 /* Const and volatile mean something different for function types,
7246 so the usual checks are not appropriate. */
7247 if (TREE_CODE (to
) != FUNCTION_TYPE
&& TREE_CODE (to
) != METHOD_TYPE
)
7249 if (TYPE_READONLY (from
) > TYPE_READONLY (to
)
7250 || TYPE_VOLATILE (from
) > TYPE_VOLATILE (to
))
7254 && (TYPE_READONLY (to
) > TYPE_READONLY (from
)
7255 || TYPE_VOLATILE (to
) > TYPE_READONLY (from
)))
7257 constp
&= TYPE_READONLY (to
);
7260 if (TREE_CODE (to
) != POINTER_TYPE
)
7261 return comptypes (TYPE_MAIN_VARIANT (to
), TYPE_MAIN_VARIANT (from
), 1);
7265 /* When comparing, say, char ** to char const **, this function takes the
7266 'char *' and 'char const *'. Do not pass non-pointer types to this
7269 comp_ptr_ttypes (to
, from
)
7272 return comp_ptr_ttypes_real (to
, from
, 1);