1 /* Build expressions with type checking for C++ compiler.
2 Copyright (C) 1987, 88, 89, 92-98, 1999 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). */
42 extern void compiler_error ();
44 static tree convert_for_assignment
PROTO((tree
, tree
, char*, tree
,
46 static tree pointer_int_sum
PROTO((enum tree_code
, tree
, tree
));
47 static tree rationalize_conditional_expr
PROTO((enum tree_code
, tree
));
48 static int comp_target_parms
PROTO((tree
, tree
, int));
49 static int comp_ptr_ttypes_real
PROTO((tree
, tree
, int));
50 static int comp_ptr_ttypes_const
PROTO((tree
, tree
));
51 static int comp_ptr_ttypes_reinterpret
PROTO((tree
, tree
));
52 static int comp_array_types
PROTO((int (*) (tree
, tree
, int), tree
,
54 static tree build_ptrmemfunc1
PROTO((tree
, tree
, tree
, tree
, tree
));
55 static tree common_base_type
PROTO((tree
, tree
));
57 static tree convert_sequence
PROTO((tree
, tree
));
59 static tree lookup_anon_field
PROTO((tree
, tree
));
60 static tree pointer_diff
PROTO((tree
, tree
, tree
));
61 static tree qualify_type
PROTO((tree
, tree
));
62 static tree get_delta_difference
PROTO((tree
, tree
, int));
64 /* Return the target type of TYPE, which meas return T for:
65 T*, T&, T[], T (...), and otherwise, just T. */
71 if (TREE_CODE (type
) == REFERENCE_TYPE
)
72 type
= TREE_TYPE (type
);
73 while (TREE_CODE (type
) == POINTER_TYPE
74 || TREE_CODE (type
) == ARRAY_TYPE
75 || TREE_CODE (type
) == FUNCTION_TYPE
76 || TREE_CODE (type
) == METHOD_TYPE
77 || TREE_CODE (type
) == OFFSET_TYPE
)
78 type
= TREE_TYPE (type
);
82 /* Do `exp = require_complete_type (exp);' to make sure exp
83 does not have an incomplete type. (That includes void types.)
84 Returns the error_mark_node if the VALUE does not have
85 complete type when this function returns. */
88 require_complete_type (value
)
93 if (processing_template_decl
|| value
== error_mark_node
)
96 if (TREE_CODE (value
) == OVERLOAD
)
97 type
= unknown_type_node
;
99 type
= TREE_TYPE (value
);
101 /* First, detect a valid value with a complete type. */
102 if (TYPE_SIZE (type
) != 0
103 && TYPE_SIZE (type
) != size_zero_node
104 && ! (TYPE_LANG_SPECIFIC (type
)
105 && (IS_SIGNATURE_POINTER (type
) || IS_SIGNATURE_REFERENCE (type
))
106 && TYPE_SIZE (SIGNATURE_TYPE (type
)) == 0))
109 /* If we see X::Y, we build an OFFSET_TYPE which has
110 not been laid out. Try to avoid an error by interpreting
111 it as this->X::Y, if reasonable. */
112 if (TREE_CODE (value
) == OFFSET_REF
113 && current_class_ref
!= 0
114 && TREE_OPERAND (value
, 0) == current_class_ref
)
116 tree base
, member
= TREE_OPERAND (value
, 1);
117 tree basetype
= TYPE_OFFSET_BASETYPE (type
);
118 my_friendly_assert (TREE_CODE (member
) == FIELD_DECL
, 305);
119 base
= convert_pointer_to (basetype
, current_class_ptr
);
120 value
= build (COMPONENT_REF
, TREE_TYPE (member
),
121 build_indirect_ref (base
, NULL_PTR
), member
);
122 return require_complete_type (value
);
125 if (complete_type_or_else (type
, value
))
128 return error_mark_node
;
131 /* Makes sure EXPR is a complete type when used in a void context, like a
132 whole expression, or lhs of a comma operator. Issue a diagnostic and
133 return error_mark_node on failure. This is a little tricky, because some
134 valid void types look stunningly similar to invalid void types. We err on
135 the side of caution */
138 require_complete_type_in_void (expr
)
141 switch (TREE_CODE (expr
))
147 op
= TREE_OPERAND (expr
,2);
148 op
= require_complete_type_in_void (op
);
149 TREE_OPERAND (expr
,2) = op
;
150 if (op
== error_mark_node
)
163 op
= TREE_OPERAND (expr
,1);
164 op
= require_complete_type_in_void (op
);
165 TREE_OPERAND (expr
,1) = op
;
166 if (op
== error_mark_node
)
175 case NON_LVALUE_EXPR
:
180 op
= TREE_OPERAND (expr
,0);
181 op
= require_complete_type_in_void (op
);
182 TREE_OPERAND (expr
,0) = op
;
183 if (op
== error_mark_node
)
191 case CALL_EXPR
: /* function call return can be ignored */
192 case RTL_EXPR
: /* RTL nodes have no value */
193 case DELETE_EXPR
: /* delete expressions have no type */
194 case VEC_DELETE_EXPR
:
195 case INTEGER_CST
: /* used for null pointer */
196 case EXIT_EXPR
: /* have no return */
197 case LOOP_EXPR
: /* have no return */
198 case BIND_EXPR
: /* have no return */
199 case THROW_EXPR
: /* have no return */
200 case MODIFY_EXPR
: /* sometimes this has a void type, but that's ok */
201 case CONVERT_EXPR
: /* sometimes has a void type */
206 tree op
= TREE_OPERAND (expr
,0);
208 /* Calling a function returning a reference has an implicit
209 dereference applied. We don't want to make that an error. */
210 if (TREE_CODE (op
) == CALL_EXPR
211 && TREE_CODE (TREE_TYPE (op
)) == REFERENCE_TYPE
)
213 /* else fallthrough */
217 expr
= require_complete_type (expr
);
224 /* Try to complete TYPE, if it is incomplete. For example, if TYPE is
225 a template instantiation, do the instantiation. Returns TYPE,
226 whether or not it could be completed, unless something goes
227 horribly wrong, in which case the error_mark_node is returned. */
233 if (type
== NULL_TREE
)
234 /* Rather than crash, we return something sure to cause an error
236 return error_mark_node
;
238 if (type
== error_mark_node
|| TYPE_SIZE (type
) != NULL_TREE
)
240 else if (TREE_CODE (type
) == ARRAY_TYPE
&& TYPE_DOMAIN (type
))
242 tree t
= complete_type (TREE_TYPE (type
));
243 if (TYPE_SIZE (t
) != NULL_TREE
&& ! processing_template_decl
)
245 TYPE_NEEDS_CONSTRUCTING (type
)
246 = TYPE_NEEDS_CONSTRUCTING (TYPE_MAIN_VARIANT (t
));
247 TYPE_NEEDS_DESTRUCTOR (type
)
248 = TYPE_NEEDS_DESTRUCTOR (TYPE_MAIN_VARIANT (t
));
250 else if (CLASS_TYPE_P (type
) && CLASSTYPE_TEMPLATE_INSTANTIATION (type
))
251 instantiate_class_template (TYPE_MAIN_VARIANT (type
));
256 /* Like complete_type, but issue an error if the TYPE cannot be
257 completed. VALUE is used for informative diagnostics.
258 Returns NULL_TREE if the type cannot be made complete. */
261 complete_type_or_else (type
, value
)
265 type
= complete_type (type
);
266 if (type
== error_mark_node
)
267 /* We already issued an error. */
269 else if (!TYPE_SIZE (type
) || TYPE_SIZE (type
) == size_zero_node
)
271 incomplete_type_error (value
, type
);
278 /* Return truthvalue of whether type of EXP is instantiated. */
284 return (TREE_CODE (exp
) == OVERLOAD
285 || TREE_CODE (exp
) == TREE_LIST
286 || TREE_TYPE (exp
) == unknown_type_node
287 || (TREE_CODE (TREE_TYPE (exp
)) == OFFSET_TYPE
288 && TREE_TYPE (TREE_TYPE (exp
)) == unknown_type_node
));
291 /* Return truthvalue of whether T is function (or pfn) type. */
297 return (TREE_CODE (t
) == FUNCTION_TYPE
|| TREE_CODE (t
) == METHOD_TYPE
298 || (TREE_CODE (t
) == POINTER_TYPE
299 && (TREE_CODE (TREE_TYPE (t
)) == FUNCTION_TYPE
300 || TREE_CODE (TREE_TYPE (t
)) == METHOD_TYPE
)));
303 /* Return a variant of TYPE which has all the type qualifiers of LIKE
304 as well as those of TYPE. */
307 qualify_type (type
, like
)
310 /* @@ Must do member pointers here. */
311 return cp_build_qualified_type (type
, (CP_TYPE_QUALS (type
)
312 | CP_TYPE_QUALS (like
)));
315 /* Return the common type of two parameter lists.
316 We assume that comptypes has already been done and returned 1;
317 if that isn't so, this may crash.
319 As an optimization, free the space we allocate if the parameter
320 lists are already common. */
326 tree oldargs
= p1
, newargs
, n
;
329 char *first_obj
= (char *) oballoc (0);
331 len
= list_length (p1
);
332 newargs
= tree_last (p1
);
334 if (newargs
== void_list_node
)
343 newargs
= tree_cons (NULL_TREE
, NULL_TREE
, newargs
);
348 p1
= TREE_CHAIN (p1
), p2
= TREE_CHAIN (p2
), n
= TREE_CHAIN (n
), i
++)
350 if (TREE_PURPOSE (p1
) && !TREE_PURPOSE (p2
))
352 TREE_PURPOSE (n
) = TREE_PURPOSE (p1
);
355 else if (! TREE_PURPOSE (p1
))
357 if (TREE_PURPOSE (p2
))
359 TREE_PURPOSE (n
) = TREE_PURPOSE (p2
);
365 if (1 != simple_cst_equal (TREE_PURPOSE (p1
), TREE_PURPOSE (p2
)))
367 TREE_PURPOSE (n
) = TREE_PURPOSE (p2
);
369 if (TREE_VALUE (p1
) != TREE_VALUE (p2
))
372 TREE_VALUE (n
) = common_type (TREE_VALUE (p1
), TREE_VALUE (p2
));
375 TREE_VALUE (n
) = TREE_VALUE (p1
);
386 /* Given a type, perhaps copied for a typedef,
387 find the "original" version of it. */
392 while (TYPE_NAME (t
) != NULL_TREE
)
394 tree x
= TYPE_NAME (t
);
395 if (TREE_CODE (x
) != TYPE_DECL
)
397 x
= DECL_ORIGINAL_TYPE (x
);
405 /* Return the common type of two types.
406 We assume that comptypes has already been done and returned 1;
407 if that isn't so, this may crash.
409 This is the type for the result of most arithmetic operations
410 if the operands have the given two types.
412 We do not deal with enumeral types here because they have already been
413 converted to integer types. */
419 register enum tree_code code1
;
420 register enum tree_code code2
;
423 /* Save time if the two types are the same. */
426 t1
= original_type (t1
);
427 t2
= original_type (t2
);
431 /* If one type is nonsense, use the other. */
432 if (t1
== error_mark_node
)
434 if (t2
== error_mark_node
)
437 /* Merge the attributes. */
438 attributes
= merge_machine_type_attributes (t1
, t2
);
440 { register tree a1
, a2
;
441 a1
= TYPE_ATTRIBUTES (t1
);
442 a2
= TYPE_ATTRIBUTES (t2
);
444 /* Either one unset? Take the set one. */
446 if (!(attributes
= a1
))
449 /* One that completely contains the other? Take it. */
451 else if (a2
&& !attribute_list_contained (a1
, a2
))
453 if (attribute_list_contained (a2
, a1
))
457 /* Pick the longest list, and hang on the other list. */
458 /* ??? For the moment we punt on the issue of attrs with args. */
460 if (list_length (a1
) < list_length (a2
))
461 attributes
= a2
, a2
= a1
;
463 for (; a2
; a2
= TREE_CHAIN (a2
))
464 if (lookup_attribute (IDENTIFIER_POINTER (TREE_PURPOSE (a2
)),
465 attributes
) == NULL_TREE
)
468 TREE_CHAIN (a1
) = attributes
;
475 /* Treat an enum type as the unsigned integer type of the same width. */
477 if (TREE_CODE (t1
) == ENUMERAL_TYPE
)
478 t1
= type_for_size (TYPE_PRECISION (t1
), 1);
479 if (TREE_CODE (t2
) == ENUMERAL_TYPE
)
480 t2
= type_for_size (TYPE_PRECISION (t2
), 1);
482 if (TYPE_PTRMEMFUNC_P (t1
))
483 t1
= TYPE_PTRMEMFUNC_FN_TYPE (t1
);
484 if (TYPE_PTRMEMFUNC_P (t2
))
485 t2
= TYPE_PTRMEMFUNC_FN_TYPE (t2
);
487 code1
= TREE_CODE (t1
);
488 code2
= TREE_CODE (t2
);
490 /* If one type is complex, form the common type of the non-complex
491 components, then make that complex. Use T1 or T2 if it is the
493 if (code1
== COMPLEX_TYPE
|| code2
== COMPLEX_TYPE
)
495 tree subtype1
= code1
== COMPLEX_TYPE
? TREE_TYPE (t1
) : t1
;
496 tree subtype2
= code2
== COMPLEX_TYPE
? TREE_TYPE (t2
) : t2
;
497 tree subtype
= common_type (subtype1
, subtype2
);
499 if (code1
== COMPLEX_TYPE
&& TREE_TYPE (t1
) == subtype
)
500 return build_type_attribute_variant (t1
, attributes
);
501 else if (code2
== COMPLEX_TYPE
&& TREE_TYPE (t2
) == subtype
)
502 return build_type_attribute_variant (t2
, attributes
);
504 return build_type_attribute_variant (build_complex_type (subtype
),
512 /* If only one is real, use it as the result. */
514 if (code1
== REAL_TYPE
&& code2
!= REAL_TYPE
)
515 return build_type_attribute_variant (t1
, attributes
);
517 if (code2
== REAL_TYPE
&& code1
!= REAL_TYPE
)
518 return build_type_attribute_variant (t2
, attributes
);
520 /* Both real or both integers; use the one with greater precision. */
522 if (TYPE_PRECISION (t1
) > TYPE_PRECISION (t2
))
523 return build_type_attribute_variant (t1
, attributes
);
524 else if (TYPE_PRECISION (t2
) > TYPE_PRECISION (t1
))
525 return build_type_attribute_variant (t2
, attributes
);
527 /* Same precision. Prefer longs to ints even when same size. */
529 if (TYPE_MAIN_VARIANT (t1
) == long_unsigned_type_node
530 || TYPE_MAIN_VARIANT (t2
) == long_unsigned_type_node
)
531 return build_type_attribute_variant (long_unsigned_type_node
,
534 if (TYPE_MAIN_VARIANT (t1
) == long_integer_type_node
535 || TYPE_MAIN_VARIANT (t2
) == long_integer_type_node
)
537 /* But preserve unsignedness from the other type,
538 since long cannot hold all the values of an unsigned int. */
539 if (TREE_UNSIGNED (t1
) || TREE_UNSIGNED (t2
))
540 t1
= long_unsigned_type_node
;
542 t1
= long_integer_type_node
;
543 return build_type_attribute_variant (t1
, attributes
);
546 if (TYPE_MAIN_VARIANT (t1
) == long_double_type_node
547 || TYPE_MAIN_VARIANT (t2
) == long_double_type_node
)
548 return build_type_attribute_variant (long_double_type_node
,
551 /* Otherwise prefer the unsigned one. */
553 if (TREE_UNSIGNED (t1
))
554 return build_type_attribute_variant (t1
, attributes
);
556 return build_type_attribute_variant (t2
, attributes
);
560 /* For two pointers, do this recursively on the target type,
561 and combine the qualifiers of the two types' targets. */
562 /* This code was turned off; I don't know why.
563 But ANSI C++ specifies doing this with the qualifiers.
564 So I turned it on again. */
566 tree tt1
= TYPE_MAIN_VARIANT (TREE_TYPE (t1
));
567 tree tt2
= TYPE_MAIN_VARIANT (TREE_TYPE (t2
));
568 int type_quals
= (CP_TYPE_QUALS (TREE_TYPE (t1
))
569 | CP_TYPE_QUALS (TREE_TYPE (t2
)));
574 else if (tt1
== void_type_node
|| tt2
== void_type_node
)
575 target
= void_type_node
;
576 else if (tt1
== unknown_type_node
)
578 else if (tt2
== unknown_type_node
)
581 target
= common_type (tt1
, tt2
);
583 target
= cp_build_qualified_type (target
, type_quals
);
584 if (code1
== POINTER_TYPE
)
585 t1
= build_pointer_type (target
);
587 t1
= build_reference_type (target
);
588 t1
= build_type_attribute_variant (t1
, attributes
);
590 if (TREE_CODE (target
) == METHOD_TYPE
)
591 t1
= build_ptrmemfunc_type (t1
);
598 tree elt
= common_type (TREE_TYPE (t1
), TREE_TYPE (t2
));
599 /* Save space: see if the result is identical to one of the args. */
600 if (elt
== TREE_TYPE (t1
) && TYPE_DOMAIN (t1
))
601 return build_type_attribute_variant (t1
, attributes
);
602 if (elt
== TREE_TYPE (t2
) && TYPE_DOMAIN (t2
))
603 return build_type_attribute_variant (t2
, attributes
);
604 /* Merge the element types, and have a size if either arg has one. */
605 t1
= build_cplus_array_type
606 (elt
, TYPE_DOMAIN (TYPE_DOMAIN (t1
) ? t1
: t2
));
607 return build_type_attribute_variant (t1
, attributes
);
611 /* Function types: prefer the one that specified arg types.
612 If both do, merge the arg types. Also merge the return types. */
614 tree valtype
= common_type (TREE_TYPE (t1
), TREE_TYPE (t2
));
615 tree p1
= TYPE_ARG_TYPES (t1
);
616 tree p2
= TYPE_ARG_TYPES (t2
);
619 /* Save space: see if the result is identical to one of the args. */
620 if (valtype
== TREE_TYPE (t1
) && ! p2
)
621 return build_type_attribute_variant (t1
, attributes
);
622 if (valtype
== TREE_TYPE (t2
) && ! p1
)
623 return build_type_attribute_variant (t2
, attributes
);
625 /* Simple way if one arg fails to specify argument types. */
626 if (p1
== NULL_TREE
|| TREE_VALUE (p1
) == void_type_node
)
628 rval
= build_function_type (valtype
, p2
);
629 if ((raises
= TYPE_RAISES_EXCEPTIONS (t2
)))
630 rval
= build_exception_variant (rval
, raises
);
631 return build_type_attribute_variant (rval
, attributes
);
633 raises
= TYPE_RAISES_EXCEPTIONS (t1
);
634 if (p2
== NULL_TREE
|| TREE_VALUE (p2
) == void_type_node
)
636 rval
= build_function_type (valtype
, p1
);
638 rval
= build_exception_variant (rval
, raises
);
639 return build_type_attribute_variant (rval
, attributes
);
642 rval
= build_function_type (valtype
, commonparms (p1
, p2
));
643 rval
= build_exception_variant (rval
, raises
);
644 return build_type_attribute_variant (rval
, attributes
);
649 t1
= TYPE_MAIN_VARIANT (t1
);
650 t2
= TYPE_MAIN_VARIANT (t2
);
652 if (DERIVED_FROM_P (t1
, t2
) && binfo_or_else (t1
, t2
))
653 return build_type_attribute_variant (t1
, attributes
);
654 else if (binfo_or_else (t2
, t1
))
655 return build_type_attribute_variant (t2
, attributes
);
658 compiler_error ("common_type called with uncommon aggregate types");
659 return error_mark_node
;
663 if (TREE_CODE (TREE_TYPE (t1
)) == TREE_CODE (TREE_TYPE (t2
)))
665 /* Get this value the long way, since TYPE_METHOD_BASETYPE
666 is just the main variant of this. */
670 tree b1
= TYPE_OFFSET_BASETYPE (t1
);
671 tree b2
= TYPE_OFFSET_BASETYPE (t2
);
673 if (same_type_p (b1
, b2
)
674 || (DERIVED_FROM_P (b1
, b2
) && binfo_or_else (b1
, b2
)))
675 basetype
= TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (t2
)));
678 if (binfo_or_else (b2
, b1
) == NULL_TREE
)
679 compiler_error ("common_type called with uncommon method types");
680 basetype
= TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (t1
)));
683 raises
= TYPE_RAISES_EXCEPTIONS (t1
);
685 /* If this was a member function type, get back to the
686 original type of type member function (i.e., without
687 the class instance variable up front. */
688 t1
= build_function_type (TREE_TYPE (t1
),
689 TREE_CHAIN (TYPE_ARG_TYPES (t1
)));
690 t2
= build_function_type (TREE_TYPE (t2
),
691 TREE_CHAIN (TYPE_ARG_TYPES (t2
)));
692 t3
= common_type (t1
, t2
);
693 t3
= build_cplus_method_type (basetype
, TREE_TYPE (t3
),
694 TYPE_ARG_TYPES (t3
));
695 t1
= build_exception_variant (t3
, raises
);
698 compiler_error ("common_type called with uncommon method types");
700 return build_type_attribute_variant (t1
, attributes
);
703 if (TREE_TYPE (t1
) == TREE_TYPE (t2
))
705 tree b1
= TYPE_OFFSET_BASETYPE (t1
);
706 tree b2
= TYPE_OFFSET_BASETYPE (t2
);
708 if (same_type_p (b1
, b2
)
709 || (DERIVED_FROM_P (b1
, b2
) && binfo_or_else (b1
, b2
)))
710 return build_type_attribute_variant (t2
, attributes
);
711 else if (binfo_or_else (b2
, b1
))
712 return build_type_attribute_variant (t1
, attributes
);
714 compiler_error ("common_type called with uncommon member types");
717 return build_type_attribute_variant (t1
, attributes
);
721 /* Return 1 if TYPE1 and TYPE2 raise the same exceptions. */
724 compexcepttypes (t1
, t2
)
727 return TYPE_RAISES_EXCEPTIONS (t1
) == TYPE_RAISES_EXCEPTIONS (t2
);
730 /* Compare the array types T1 and T2, using CMP as the type comparison
731 function for the element types. STRICT is as for comptypes. */
734 comp_array_types (cmp
, t1
, t2
, strict
)
735 register int (*cmp
) PROTO((tree
, tree
, int));
745 /* The type of the array elements must be the same. */
746 if (!(TREE_TYPE (t1
) == TREE_TYPE (t2
)
747 || (*cmp
) (TREE_TYPE (t1
), TREE_TYPE (t2
),
748 strict
& ~COMPARE_REDECLARATION
)))
751 d1
= TYPE_DOMAIN (t1
);
752 d2
= TYPE_DOMAIN (t2
);
757 /* If one of the arrays is dimensionless, and the other has a
758 dimension, they are of different types. However, it is legal to
766 declarations for an array object can specify
767 array types that differ by the presence or absence of a major
768 array bound (_dcl.array_). */
770 return strict
& COMPARE_REDECLARATION
;
772 /* Check that the dimensions are the same. */
773 return (cp_tree_equal (TYPE_MIN_VALUE (d1
),
775 && cp_tree_equal (TYPE_MAX_VALUE (d1
),
776 TYPE_MAX_VALUE (d2
)));
779 /* Return 1 if TYPE1 and TYPE2 are compatible types for assignment
780 or various other operations. STRICT is a bitwise-or of the
784 comptypes (type1
, type2
, strict
)
788 register tree t1
= type1
;
789 register tree t2
= type2
;
791 int orig_strict
= strict
;
793 /* The special exemption for redeclaring array types without an
794 array bound only applies at the top level:
799 is not legal, for example. */
800 strict
&= ~COMPARE_REDECLARATION
;
802 /* Suppress errors caused by previously reported errors */
806 /* This should never happen. */
807 my_friendly_assert (t1
!= error_mark_node
, 307);
809 if (t2
== error_mark_node
)
812 if (strict
& COMPARE_RELAXED
)
814 /* Treat an enum type as the unsigned integer type of the same width. */
816 if (TREE_CODE (t1
) == ENUMERAL_TYPE
)
817 t1
= type_for_size (TYPE_PRECISION (t1
), 1);
818 if (TREE_CODE (t2
) == ENUMERAL_TYPE
)
819 t2
= type_for_size (TYPE_PRECISION (t2
), 1);
825 if (TYPE_PTRMEMFUNC_P (t1
))
826 t1
= TYPE_PTRMEMFUNC_FN_TYPE (t1
);
827 if (TYPE_PTRMEMFUNC_P (t2
))
828 t2
= TYPE_PTRMEMFUNC_FN_TYPE (t2
);
830 /* Different classes of types can't be compatible. */
831 if (TREE_CODE (t1
) != TREE_CODE (t2
))
834 /* Qualifiers must match. */
835 if (CP_TYPE_QUALS (t1
) != CP_TYPE_QUALS (t2
))
837 if (strict
== COMPARE_STRICT
838 && TYPE_FOR_JAVA (t1
) != TYPE_FOR_JAVA (t2
))
841 /* Allow for two different type nodes which have essentially the same
842 definition. Note that we already checked for equality of the type
843 qualifiers (just above). */
845 if (TYPE_MAIN_VARIANT (t1
) == TYPE_MAIN_VARIANT (t2
))
848 /* ??? COMP_TYPE_ATTRIBUTES is currently useless for variables as each
849 attribute is its own main variant (`val' will remain 0). */
850 #ifndef COMP_TYPE_ATTRIBUTES
851 #define COMP_TYPE_ATTRIBUTES(t1,t2) 1
854 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
855 if (! (attrval
= COMP_TYPE_ATTRIBUTES (t1
, t2
)))
858 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
861 switch (TREE_CODE (t1
))
863 case TEMPLATE_TEMPLATE_PARM
:
864 if (TEMPLATE_TYPE_IDX (t1
) != TEMPLATE_TYPE_IDX (t2
)
865 || TEMPLATE_TYPE_LEVEL (t1
) != TEMPLATE_TYPE_LEVEL (t2
))
867 if (! comp_template_parms (DECL_TEMPLATE_PARMS (TYPE_NAME (t1
)),
868 DECL_TEMPLATE_PARMS (TYPE_NAME (t2
))))
870 if (!TEMPLATE_TEMPLATE_PARM_TEMPLATE_INFO (t1
)
871 && ! TEMPLATE_TEMPLATE_PARM_TEMPLATE_INFO (t2
))
873 /* Don't check inheritance. */
874 strict
= COMPARE_STRICT
;
879 if (TYPE_TEMPLATE_INFO (t1
) && TYPE_TEMPLATE_INFO (t2
)
880 && (TYPE_TI_TEMPLATE (t1
) == TYPE_TI_TEMPLATE (t2
)
881 || TREE_CODE (t1
) == TEMPLATE_TEMPLATE_PARM
))
882 val
= comp_template_args (TYPE_TI_ARGS (t1
),
885 if ((strict
& COMPARE_BASE
) && DERIVED_FROM_P (t1
, t2
))
890 if ((strict
& COMPARE_RELAXED
) && DERIVED_FROM_P (t2
, t1
))
898 val
= (comptypes (build_pointer_type (TYPE_OFFSET_BASETYPE (t1
)),
899 build_pointer_type (TYPE_OFFSET_BASETYPE (t2
)), strict
)
900 && comptypes (TREE_TYPE (t1
), TREE_TYPE (t2
), strict
));
904 if (! compexcepttypes (t1
, t2
))
907 /* This case is anti-symmetrical!
908 One can pass a base member (or member function)
909 to something expecting a derived member (or member function),
910 but not vice-versa! */
912 val
= (comptypes (TREE_TYPE (t1
), TREE_TYPE (t2
), strict
)
913 && compparms (TYPE_ARG_TYPES (t1
), TYPE_ARG_TYPES (t2
)));
920 /* first, check whether the referred types match with the
921 required level of strictness */
922 val
= comptypes (t1
, t2
, strict
);
925 if (TREE_CODE (t1
) == RECORD_TYPE
926 && TREE_CODE (t2
) == RECORD_TYPE
)
931 if (! compexcepttypes (t1
, t2
))
934 val
= ((TREE_TYPE (t1
) == TREE_TYPE (t2
)
935 || comptypes (TREE_TYPE (t1
), TREE_TYPE (t2
), strict
))
936 && compparms (TYPE_ARG_TYPES (t1
), TYPE_ARG_TYPES (t2
)));
940 /* Target types must match incl. qualifiers. We use ORIG_STRICT
941 here since this is the one place where
942 COMPARE_REDECLARATION should be used. */
943 val
= comp_array_types (comptypes
, t1
, t2
, orig_strict
);
946 case TEMPLATE_TYPE_PARM
:
947 return TEMPLATE_TYPE_IDX (t1
) == TEMPLATE_TYPE_IDX (t2
)
948 && TEMPLATE_TYPE_LEVEL (t1
) == TEMPLATE_TYPE_LEVEL (t2
);
951 if (TYPE_IDENTIFIER (t1
) != TYPE_IDENTIFIER (t2
))
953 return same_type_p (TYPE_CONTEXT (t1
), TYPE_CONTEXT (t2
));
958 return attrval
== 2 && val
== 1 ? 2 : val
;
961 /* Subroutine of comp_target-types. Make sure that the cv-quals change
962 only in the same direction as the target type. */
965 comp_cv_target_types (ttl
, ttr
, nptrs
)
971 if (!at_least_as_qualified_p (ttl
, ttr
)
972 && !at_least_as_qualified_p (ttr
, ttl
))
973 /* The qualifications are incomparable. */
976 if (TYPE_MAIN_VARIANT (ttl
) == TYPE_MAIN_VARIANT (ttr
))
977 return more_qualified_p (ttr
, ttl
) ? -1 : 1;
979 t
= comp_target_types (ttl
, ttr
, nptrs
);
980 if ((t
== 1 && at_least_as_qualified_p (ttl
, ttr
))
981 || (t
== -1 && at_least_as_qualified_p (ttr
, ttl
)))
987 /* Return 1 or -1 if TTL and TTR are pointers to types that are equivalent,
988 ignoring their qualifiers, 0 if not. Return 1 means that TTR can be
989 converted to TTL. Return -1 means that TTL can be converted to TTR but
992 NPTRS is the number of pointers we can strip off and keep cool.
993 This is used to permit (for aggr A, aggr B) A, B* to convert to A*,
994 but to not permit B** to convert to A**.
996 This should go away. Callers should use can_convert or something
997 similar instead. (jason 17 Apr 1997) */
1000 comp_target_types (ttl
, ttr
, nptrs
)
1004 ttl
= TYPE_MAIN_VARIANT (ttl
);
1005 ttr
= TYPE_MAIN_VARIANT (ttr
);
1009 if (TREE_CODE (ttr
) != TREE_CODE (ttl
))
1012 if ((TREE_CODE (ttr
) == POINTER_TYPE
1013 || TREE_CODE (ttr
) == REFERENCE_TYPE
)
1014 /* If we get a pointer with nptrs == 0, we don't allow any tweaking
1015 of the type pointed to. This is necessary for reference init
1016 semantics. We won't get here from a previous call with nptrs == 1;
1017 for multi-level pointers we end up in comp_ptr_ttypes. */
1020 int is_ptr
= TREE_CODE (ttr
) == POINTER_TYPE
;
1022 ttl
= TREE_TYPE (ttl
);
1023 ttr
= TREE_TYPE (ttr
);
1027 if (TREE_CODE (ttl
) == UNKNOWN_TYPE
1028 || TREE_CODE (ttr
) == UNKNOWN_TYPE
)
1030 else if (TREE_CODE (ttl
) == VOID_TYPE
1031 && TREE_CODE (ttr
) != FUNCTION_TYPE
1032 && TREE_CODE (ttr
) != METHOD_TYPE
1033 && TREE_CODE (ttr
) != OFFSET_TYPE
)
1035 else if (TREE_CODE (ttr
) == VOID_TYPE
1036 && TREE_CODE (ttl
) != FUNCTION_TYPE
1037 && TREE_CODE (ttl
) != METHOD_TYPE
1038 && TREE_CODE (ttl
) != OFFSET_TYPE
)
1040 else if (TREE_CODE (ttl
) == POINTER_TYPE
1041 || TREE_CODE (ttl
) == ARRAY_TYPE
)
1043 if (comp_ptr_ttypes (ttl
, ttr
))
1045 else if (comp_ptr_ttypes (ttr
, ttl
))
1051 /* Const and volatile mean something different for function types,
1052 so the usual checks are not appropriate. */
1053 if (TREE_CODE (ttl
) == FUNCTION_TYPE
|| TREE_CODE (ttl
) == METHOD_TYPE
)
1054 return comp_target_types (ttl
, ttr
, nptrs
- 1);
1056 return comp_cv_target_types (ttl
, ttr
, nptrs
- 1);
1059 if (TREE_CODE (ttr
) == ARRAY_TYPE
)
1060 return comp_array_types (comp_target_types
, ttl
, ttr
, COMPARE_STRICT
);
1061 else if (TREE_CODE (ttr
) == FUNCTION_TYPE
|| TREE_CODE (ttr
) == METHOD_TYPE
)
1068 if (!same_type_p (TREE_TYPE (ttl
), TREE_TYPE (ttr
)))
1073 switch (comp_target_types (TREE_TYPE (ttl
), TREE_TYPE (ttr
), -1))
1082 argsl
= TYPE_ARG_TYPES (ttl
);
1083 argsr
= TYPE_ARG_TYPES (ttr
);
1085 /* Compare 'this' here, not in comp_target_parms. */
1086 if (TREE_CODE (ttr
) == METHOD_TYPE
)
1088 tree tl
= TYPE_METHOD_BASETYPE (ttl
);
1089 tree tr
= TYPE_METHOD_BASETYPE (ttr
);
1091 if (!same_or_base_type_p (tr
, tl
))
1093 if (same_or_base_type_p (tl
, tr
))
1099 argsl
= TREE_CHAIN (argsl
);
1100 argsr
= TREE_CHAIN (argsr
);
1103 switch (comp_target_parms (argsl
, argsr
, 1))
1111 return saw_contra
? -1 : 1;
1114 else if (TREE_CODE (ttr
) == OFFSET_TYPE
)
1118 /* Contravariance: we can assign a pointer to base member to a pointer
1119 to derived member. Note difference from simple pointer case, where
1120 we can pass a pointer to derived to a pointer to base. */
1121 if (same_or_base_type_p (TYPE_OFFSET_BASETYPE (ttr
),
1122 TYPE_OFFSET_BASETYPE (ttl
)))
1124 else if (same_or_base_type_p (TYPE_OFFSET_BASETYPE (ttl
),
1125 TYPE_OFFSET_BASETYPE (ttr
)))
1135 ttl
= TREE_TYPE (ttl
);
1136 ttr
= TREE_TYPE (ttr
);
1138 if (TREE_CODE (ttl
) == POINTER_TYPE
1139 || TREE_CODE (ttl
) == ARRAY_TYPE
)
1141 if (comp_ptr_ttypes (ttl
, ttr
))
1147 if (comp_cv_target_types (ttl
, ttr
, nptrs
) == 1)
1152 else if (IS_AGGR_TYPE (ttl
))
1156 if (same_or_base_type_p (build_pointer_type (ttl
),
1157 build_pointer_type (ttr
)))
1159 if (same_or_base_type_p (build_pointer_type (ttr
),
1160 build_pointer_type (ttl
)))
1168 /* Returns 1 if TYPE1 is at least as qualified as TYPE2. */
1171 at_least_as_qualified_p (type1
, type2
)
1175 /* All qualifiers for TYPE2 must also appear in TYPE1. */
1176 return ((CP_TYPE_QUALS (type1
) & CP_TYPE_QUALS (type2
))
1177 == CP_TYPE_QUALS (type2
));
1180 /* Returns 1 if TYPE1 is more qualified than TYPE2. */
1183 more_qualified_p (type1
, type2
)
1187 return (CP_TYPE_QUALS (type1
) != CP_TYPE_QUALS (type2
)
1188 && at_least_as_qualified_p (type1
, type2
));
1191 /* Returns 1 if TYPE1 is more cv-qualified than TYPE2, -1 if TYPE2 is
1192 more cv-qualified that TYPE1, and 0 otherwise. */
1195 comp_cv_qualification (type1
, type2
)
1199 if (CP_TYPE_QUALS (type1
) == CP_TYPE_QUALS (type2
))
1202 if (at_least_as_qualified_p (type1
, type2
))
1205 else if (at_least_as_qualified_p (type2
, type1
))
1211 /* Returns 1 if the cv-qualification signature of TYPE1 is a proper
1212 subset of the cv-qualification signature of TYPE2, and the types
1213 are similar. Returns -1 if the other way 'round, and 0 otherwise. */
1216 comp_cv_qual_signature (type1
, type2
)
1220 if (comp_ptr_ttypes_real (type2
, type1
, -1))
1222 else if (comp_ptr_ttypes_real (type1
, type2
, -1))
1228 /* If two types share a common base type, return that basetype.
1229 If there is not a unique most-derived base type, this function
1230 returns ERROR_MARK_NODE. */
1233 common_base_type (tt1
, tt2
)
1236 tree best
= NULL_TREE
;
1239 /* If one is a baseclass of another, that's good enough. */
1240 if (UNIQUELY_DERIVED_FROM_P (tt1
, tt2
))
1242 if (UNIQUELY_DERIVED_FROM_P (tt2
, tt1
))
1245 /* Otherwise, try to find a unique baseclass of TT1
1246 that is shared by TT2, and follow that down. */
1247 for (i
= CLASSTYPE_N_BASECLASSES (tt1
)-1; i
>= 0; i
--)
1249 tree basetype
= TYPE_BINFO_BASETYPE (tt1
, i
);
1250 tree trial
= common_base_type (basetype
, tt2
);
1253 if (trial
== error_mark_node
)
1255 if (best
== NULL_TREE
)
1257 else if (best
!= trial
)
1258 return error_mark_node
;
1263 for (i
= CLASSTYPE_N_BASECLASSES (tt2
)-1; i
>= 0; i
--)
1265 tree basetype
= TYPE_BINFO_BASETYPE (tt2
, i
);
1266 tree trial
= common_base_type (tt1
, basetype
);
1269 if (trial
== error_mark_node
)
1271 if (best
== NULL_TREE
)
1273 else if (best
!= trial
)
1274 return error_mark_node
;
1280 /* Subroutines of `comptypes'. */
1282 /* Return 1 if two parameter type lists PARMS1 and PARMS2 are
1283 equivalent in the sense that functions with those parameter types
1284 can have equivalent types. The two lists must be equivalent,
1287 C++: See comment above about TYPE1, TYPE2. */
1290 compparms (parms1
, parms2
)
1291 tree parms1
, parms2
;
1293 register tree t1
= parms1
, t2
= parms2
;
1295 /* An unspecified parmlist matches any specified parmlist
1296 whose argument types don't need default promotions. */
1300 if (t1
== 0 && t2
== 0)
1302 /* If one parmlist is shorter than the other,
1303 they fail to match. */
1304 if (t1
== 0 || t2
== 0)
1306 if (!same_type_p (TREE_VALUE (t2
), TREE_VALUE (t1
)))
1309 t1
= TREE_CHAIN (t1
);
1310 t2
= TREE_CHAIN (t2
);
1314 /* This really wants return whether or not parameter type lists
1315 would make their owning functions assignment compatible or not.
1317 The return value is like for comp_target_types.
1319 This should go away, possibly with the exception of the empty parmlist
1320 conversion; there are no conversions between function types in C++.
1321 (jason 17 Apr 1997) */
1324 comp_target_parms (parms1
, parms2
, strict
)
1325 tree parms1
, parms2
;
1328 register tree t1
= parms1
, t2
= parms2
;
1329 int warn_contravariance
= 0;
1331 /* In C, an unspecified parmlist matches any specified parmlist
1332 whose argument types don't need default promotions. This is not
1333 true for C++, but let's do it anyway for unfixed headers. */
1335 if (t1
== 0 && t2
!= 0)
1337 cp_pedwarn ("ANSI C++ prohibits conversion from `(%#T)' to `(...)'",
1339 return self_promoting_args_p (t2
);
1342 return self_promoting_args_p (t1
);
1344 for (; t1
|| t2
; t1
= TREE_CHAIN (t1
), t2
= TREE_CHAIN (t2
))
1348 /* If one parmlist is shorter than the other,
1349 they fail to match, unless STRICT is <= 0. */
1350 if (t1
== 0 || t2
== 0)
1355 return 1 + warn_contravariance
;
1356 return ((t1
&& TREE_PURPOSE (t1
)) + warn_contravariance
);
1358 p1
= TREE_VALUE (t1
);
1359 p2
= TREE_VALUE (t2
);
1360 if (same_type_p (p1
, p2
))
1366 if ((TREE_CODE (p1
) == POINTER_TYPE
&& TREE_CODE (p2
) == POINTER_TYPE
)
1367 || (TREE_CODE (p1
) == REFERENCE_TYPE
1368 && TREE_CODE (p2
) == REFERENCE_TYPE
))
1371 && (TYPE_MAIN_VARIANT (TREE_TYPE (p1
))
1372 == TYPE_MAIN_VARIANT (TREE_TYPE (p2
))))
1375 /* The following is wrong for contravariance,
1376 but many programs depend on it. */
1377 if (TREE_TYPE (p1
) == void_type_node
)
1379 if (TREE_TYPE (p2
) == void_type_node
)
1381 warn_contravariance
= 1;
1384 if (IS_AGGR_TYPE (TREE_TYPE (p1
))
1385 && !same_type_p (TYPE_MAIN_VARIANT (TREE_TYPE (p1
)),
1386 TYPE_MAIN_VARIANT (TREE_TYPE (p2
))))
1389 /* Note backwards order due to contravariance. */
1390 if (comp_target_types (p2
, p1
, 1) <= 0)
1392 if (comp_target_types (p1
, p2
, 1) > 0)
1394 warn_contravariance
= 1;
1401 return warn_contravariance
? -1 : 1;
1404 /* Return 1 if PARMS specifies a fixed number of parameters
1405 and none of their types is affected by default promotions. */
1408 self_promoting_args_p (parms
)
1412 for (t
= parms
; t
; t
= TREE_CHAIN (t
))
1414 register tree type
= TREE_VALUE (t
);
1416 if (TREE_CHAIN (t
) == 0 && type
!= void_type_node
)
1422 if (TYPE_MAIN_VARIANT (type
) == float_type_node
)
1425 if (C_PROMOTING_INTEGER_TYPE_P (type
))
1431 /* Return an unsigned type the same as TYPE in other respects.
1433 C++: must make these work for type variants as well. */
1436 unsigned_type (type
)
1439 tree type1
= TYPE_MAIN_VARIANT (type
);
1440 if (type1
== signed_char_type_node
|| type1
== char_type_node
)
1441 return unsigned_char_type_node
;
1442 if (type1
== integer_type_node
)
1443 return unsigned_type_node
;
1444 if (type1
== short_integer_type_node
)
1445 return short_unsigned_type_node
;
1446 if (type1
== long_integer_type_node
)
1447 return long_unsigned_type_node
;
1448 if (type1
== long_long_integer_type_node
)
1449 return long_long_unsigned_type_node
;
1450 #if HOST_BITS_PER_WIDE_INT >= 64
1451 if (type1
== intTI_type_node
)
1452 return unsigned_intTI_type_node
;
1454 if (type1
== intDI_type_node
)
1455 return unsigned_intDI_type_node
;
1456 if (type1
== intSI_type_node
)
1457 return unsigned_intSI_type_node
;
1458 if (type1
== intHI_type_node
)
1459 return unsigned_intHI_type_node
;
1460 if (type1
== intQI_type_node
)
1461 return unsigned_intQI_type_node
;
1463 return signed_or_unsigned_type (1, type
);
1466 /* Return a signed type the same as TYPE in other respects. */
1472 tree type1
= TYPE_MAIN_VARIANT (type
);
1473 if (type1
== unsigned_char_type_node
|| type1
== char_type_node
)
1474 return signed_char_type_node
;
1475 if (type1
== unsigned_type_node
)
1476 return integer_type_node
;
1477 if (type1
== short_unsigned_type_node
)
1478 return short_integer_type_node
;
1479 if (type1
== long_unsigned_type_node
)
1480 return long_integer_type_node
;
1481 if (type1
== long_long_unsigned_type_node
)
1482 return long_long_integer_type_node
;
1483 #if HOST_BITS_PER_WIDE_INT >= 64
1484 if (type1
== unsigned_intTI_type_node
)
1485 return intTI_type_node
;
1487 if (type1
== unsigned_intDI_type_node
)
1488 return intDI_type_node
;
1489 if (type1
== unsigned_intSI_type_node
)
1490 return intSI_type_node
;
1491 if (type1
== unsigned_intHI_type_node
)
1492 return intHI_type_node
;
1493 if (type1
== unsigned_intQI_type_node
)
1494 return intQI_type_node
;
1496 return signed_or_unsigned_type (0, type
);
1499 /* Return a type the same as TYPE except unsigned or
1500 signed according to UNSIGNEDP. */
1503 signed_or_unsigned_type (unsignedp
, type
)
1507 if (! INTEGRAL_TYPE_P (type
)
1508 || TREE_UNSIGNED (type
) == unsignedp
)
1511 if (TYPE_PRECISION (type
) == TYPE_PRECISION (signed_char_type_node
))
1512 return unsignedp
? unsigned_char_type_node
: signed_char_type_node
;
1513 if (TYPE_PRECISION (type
) == TYPE_PRECISION (integer_type_node
))
1514 return unsignedp
? unsigned_type_node
: integer_type_node
;
1515 if (TYPE_PRECISION (type
) == TYPE_PRECISION (short_integer_type_node
))
1516 return unsignedp
? short_unsigned_type_node
: short_integer_type_node
;
1517 if (TYPE_PRECISION (type
) == TYPE_PRECISION (long_integer_type_node
))
1518 return unsignedp
? long_unsigned_type_node
: long_integer_type_node
;
1519 if (TYPE_PRECISION (type
) == TYPE_PRECISION (long_long_integer_type_node
))
1520 return (unsignedp
? long_long_unsigned_type_node
1521 : long_long_integer_type_node
);
1525 /* Compute the value of the `sizeof' operator. */
1531 enum tree_code code
= TREE_CODE (type
);
1534 if (processing_template_decl
)
1535 return build_min (SIZEOF_EXPR
, sizetype
, type
);
1537 if (code
== FUNCTION_TYPE
)
1539 if (pedantic
|| warn_pointer_arith
)
1540 pedwarn ("ANSI C++ forbids taking the sizeof a function type");
1541 return size_int (1);
1543 if (code
== METHOD_TYPE
)
1545 if (pedantic
|| warn_pointer_arith
)
1546 pedwarn ("ANSI C++ forbids taking the sizeof a method type");
1547 return size_int (1);
1549 if (code
== VOID_TYPE
)
1551 if (pedantic
|| warn_pointer_arith
)
1552 pedwarn ("ANSI C++ forbids taking the sizeof a void type");
1553 return size_int (1);
1555 if (code
== ERROR_MARK
)
1556 return size_int (1);
1558 /* ARM $5.3.2: ``When applied to a reference, the result is the size of the
1559 referenced object.'' */
1560 if (code
== REFERENCE_TYPE
)
1561 type
= TREE_TYPE (type
);
1563 /* We couldn't find anything in the ARM or the draft standard that says,
1564 one way or the other, if doing sizeof on something that doesn't have
1565 an object associated with it is correct or incorrect. For example, if
1566 you declare `struct S { char str[16]; };', and in your program do
1567 a `sizeof (S::str)', should we flag that as an error or should we give
1568 the size of it? Since it seems like a reasonable thing to do, we'll go
1569 with giving the value. */
1570 if (code
== OFFSET_TYPE
)
1571 type
= TREE_TYPE (type
);
1573 /* @@ This also produces an error for a signature ref.
1574 In that case we should be able to do better. */
1575 if (IS_SIGNATURE (type
))
1577 error ("`sizeof' applied to a signature type");
1578 return size_int (0);
1581 if (TYPE_SIZE (complete_type (type
)) == 0)
1583 cp_error ("`sizeof' applied to incomplete type `%T'", type
);
1584 return size_int (0);
1587 /* Convert in case a char is more than one unit. */
1588 t
= size_binop (CEIL_DIV_EXPR
, TYPE_SIZE (type
),
1589 size_int (TYPE_PRECISION (char_type_node
)));
1590 t
= convert (sizetype
, t
);
1591 /* size_binop does not put the constant in range, so do it now. */
1592 if (TREE_CODE (t
) == INTEGER_CST
&& force_fit_type (t
, 0))
1593 TREE_CONSTANT_OVERFLOW (t
) = TREE_OVERFLOW (t
) = 1;
1601 if (processing_template_decl
)
1602 return build_min (SIZEOF_EXPR
, sizetype
, e
);
1604 if (TREE_CODE (e
) == COMPONENT_REF
1605 && DECL_C_BIT_FIELD (TREE_OPERAND (e
, 1)))
1606 error ("sizeof applied to a bit-field");
1607 /* ANSI says arrays and functions are converted inside comma.
1608 But we can't really convert them in build_compound_expr
1609 because that would break commas in lvalues.
1610 So do the conversion here if operand was a comma. */
1611 if (TREE_CODE (e
) == COMPOUND_EXPR
1612 && (TREE_CODE (TREE_TYPE (e
)) == ARRAY_TYPE
1613 || TREE_CODE (TREE_TYPE (e
)) == FUNCTION_TYPE
))
1614 e
= default_conversion (e
);
1615 else if (is_overloaded_fn (e
))
1617 pedwarn ("ANSI C++ forbids taking the sizeof a function type");
1618 return size_int (1);
1621 return c_sizeof (TREE_TYPE (e
));
1625 c_sizeof_nowarn (type
)
1628 enum tree_code code
= TREE_CODE (type
);
1631 if (code
== FUNCTION_TYPE
1632 || code
== METHOD_TYPE
1633 || code
== VOID_TYPE
1634 || code
== ERROR_MARK
)
1635 return size_int (1);
1636 if (code
== REFERENCE_TYPE
)
1637 type
= TREE_TYPE (type
);
1639 if (TYPE_SIZE (type
) == 0)
1640 return size_int (0);
1642 /* Convert in case a char is more than one unit. */
1643 t
= size_binop (CEIL_DIV_EXPR
, TYPE_SIZE (type
),
1644 size_int (TYPE_PRECISION (char_type_node
)));
1645 t
= convert (sizetype
, t
);
1646 force_fit_type (t
, 0);
1650 /* Implement the __alignof keyword: Return the minimum required
1651 alignment of TYPE, measured in bytes. */
1657 enum tree_code code
= TREE_CODE (type
);
1660 if (processing_template_decl
)
1661 return build_min (ALIGNOF_EXPR
, sizetype
, type
);
1663 if (code
== FUNCTION_TYPE
|| code
== METHOD_TYPE
)
1664 return size_int (FUNCTION_BOUNDARY
/ BITS_PER_UNIT
);
1666 if (code
== VOID_TYPE
|| code
== ERROR_MARK
)
1667 return size_int (1);
1669 /* C++: this is really correct! */
1670 if (code
== REFERENCE_TYPE
)
1671 type
= TREE_TYPE (type
);
1673 /* @@ This also produces an error for a signature ref.
1674 In that case we should be able to do better. */
1675 if (IS_SIGNATURE (type
))
1677 error ("`__alignof' applied to a signature type");
1678 return size_int (1);
1681 t
= size_int (TYPE_ALIGN (type
) / BITS_PER_UNIT
);
1682 force_fit_type (t
, 0);
1686 /* Perform the array-to-pointer and function-to-pointer conversions
1689 In addition, references are converted to rvalues and manifest
1690 constants are replaced by their values. */
1693 decay_conversion (exp
)
1696 register tree type
= TREE_TYPE (exp
);
1697 register enum tree_code code
= TREE_CODE (type
);
1699 if (code
== OFFSET_TYPE
)
1701 if (TREE_CODE (exp
) == OFFSET_REF
)
1702 return decay_conversion (resolve_offset_ref (exp
));
1704 type
= TREE_TYPE (type
);
1705 code
= TREE_CODE (type
);
1707 if (type
== unknown_type_node
)
1709 cp_pedwarn ("assuming & on overloaded member function");
1710 return build_unary_op (ADDR_EXPR
, exp
, 0);
1714 if (code
== REFERENCE_TYPE
)
1716 exp
= convert_from_reference (exp
);
1717 type
= TREE_TYPE (exp
);
1718 code
= TREE_CODE (type
);
1721 /* Constants can be used directly unless they're not loadable. */
1722 if (TREE_CODE (exp
) == CONST_DECL
)
1723 exp
= DECL_INITIAL (exp
);
1724 /* Replace a nonvolatile const static variable with its value. We
1725 don't do this for arrays, though; we want the address of the
1726 first element of the array, not the address of the first element
1727 of its initializing constant. We *do* replace variables that the
1728 user isn't really supposed to know about; this is a hack to deal
1729 with __PRETTY_FUNCTION__ and the like. */
1730 else if (TREE_READONLY_DECL_P (exp
)
1731 && (code
!= ARRAY_TYPE
1732 || (TREE_CODE (exp
) == VAR_DECL
&& DECL_IGNORED_P (exp
))))
1734 exp
= decl_constant_value (exp
);
1735 type
= TREE_TYPE (exp
);
1738 /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
1739 Leave such NOP_EXPRs, since RHS is being used in non-lvalue context. */
1741 if (code
== VOID_TYPE
)
1743 error ("void value not ignored as it ought to be");
1744 return error_mark_node
;
1746 if (code
== METHOD_TYPE
)
1748 cp_pedwarn ("assuming & on `%E'", exp
);
1749 return build_unary_op (ADDR_EXPR
, exp
, 0);
1751 if (code
== FUNCTION_TYPE
|| is_overloaded_fn (exp
))
1752 return build_unary_op (ADDR_EXPR
, exp
, 0);
1753 if (code
== ARRAY_TYPE
)
1758 if (TREE_CODE (exp
) == INDIRECT_REF
)
1760 /* Stripping away the INDIRECT_REF is not the right
1761 thing to do for references... */
1762 tree inner
= TREE_OPERAND (exp
, 0);
1763 if (TREE_CODE (TREE_TYPE (inner
)) == REFERENCE_TYPE
)
1765 inner
= build1 (CONVERT_EXPR
,
1766 build_pointer_type (TREE_TYPE
1767 (TREE_TYPE (inner
))),
1769 TREE_CONSTANT (inner
) = TREE_CONSTANT (TREE_OPERAND (inner
, 0));
1771 return cp_convert (build_pointer_type (TREE_TYPE (type
)), inner
);
1774 if (TREE_CODE (exp
) == COMPOUND_EXPR
)
1776 tree op1
= decay_conversion (TREE_OPERAND (exp
, 1));
1777 return build (COMPOUND_EXPR
, TREE_TYPE (op1
),
1778 TREE_OPERAND (exp
, 0), op1
);
1782 && ! (TREE_CODE (exp
) == CONSTRUCTOR
&& TREE_STATIC (exp
)))
1784 error ("invalid use of non-lvalue array");
1785 return error_mark_node
;
1788 ptrtype
= build_pointer_type (TREE_TYPE (type
));
1790 if (TREE_CODE (exp
) == VAR_DECL
)
1792 /* ??? This is not really quite correct
1793 in that the type of the operand of ADDR_EXPR
1794 is not the target type of the type of the ADDR_EXPR itself.
1795 Question is, can this lossage be avoided? */
1796 adr
= build1 (ADDR_EXPR
, ptrtype
, exp
);
1797 if (mark_addressable (exp
) == 0)
1798 return error_mark_node
;
1799 TREE_CONSTANT (adr
) = staticp (exp
);
1800 TREE_SIDE_EFFECTS (adr
) = 0; /* Default would be, same as EXP. */
1803 /* This way is better for a COMPONENT_REF since it can
1804 simplify the offset for a component. */
1805 adr
= build_unary_op (ADDR_EXPR
, exp
, 1);
1806 return cp_convert (ptrtype
, adr
);
1813 default_conversion (exp
)
1817 enum tree_code code
;
1819 exp
= decay_conversion (exp
);
1821 type
= TREE_TYPE (exp
);
1822 code
= TREE_CODE (type
);
1824 if (INTEGRAL_CODE_P (code
))
1826 tree t
= type_promotes_to (type
);
1828 return cp_convert (t
, exp
);
1834 /* Take the address of an inline function without setting TREE_ADDRESSABLE
1838 inline_conversion (exp
)
1841 if (TREE_CODE (exp
) == FUNCTION_DECL
)
1842 exp
= build1 (ADDR_EXPR
, build_pointer_type (TREE_TYPE (exp
)), exp
);
1847 /* Returns nonzero iff exp is a STRING_CST or the result of applying
1848 decay_conversion to one. */
1851 string_conv_p (totype
, exp
, warn
)
1857 if (! flag_const_strings
|| TREE_CODE (totype
) != POINTER_TYPE
)
1860 t
= TREE_TYPE (totype
);
1861 if (!same_type_p (t
, char_type_node
)
1862 && !same_type_p (t
, wchar_type_node
))
1865 if (TREE_CODE (exp
) == STRING_CST
)
1867 /* Make sure that we don't try to convert between char and wchar_t. */
1868 if (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (exp
))) != t
)
1873 /* Is this a string constant which has decayed to 'const char *'? */
1874 t
= build_pointer_type (build_qualified_type (t
, TYPE_QUAL_CONST
));
1875 if (!same_type_p (TREE_TYPE (exp
), t
))
1878 if (TREE_CODE (exp
) != ADDR_EXPR
1879 || TREE_CODE (TREE_OPERAND (exp
, 0)) != STRING_CST
)
1883 /* This warning is not very useful, as it complains about printf. */
1884 if (warn
&& warn_write_strings
)
1885 cp_warning ("deprecated conversion from string constant to `%T'", totype
);
1891 build_object_ref (datum
, basetype
, field
)
1892 tree datum
, basetype
, field
;
1895 if (datum
== error_mark_node
)
1896 return error_mark_node
;
1898 dtype
= TREE_TYPE (datum
);
1899 if (TREE_CODE (dtype
) == REFERENCE_TYPE
)
1900 dtype
= TREE_TYPE (dtype
);
1901 if (! IS_AGGR_TYPE_CODE (TREE_CODE (dtype
)))
1903 cp_error ("request for member `%T::%D' in expression of non-aggregate type `%T'",
1904 basetype
, field
, dtype
);
1905 return error_mark_node
;
1907 else if (IS_SIGNATURE (basetype
))
1909 warning ("signature name in scope resolution ignored");
1910 return build_component_ref (datum
, field
, NULL_TREE
, 1);
1912 else if (is_aggr_type (basetype
, 1))
1914 tree binfo
= binfo_or_else (basetype
, dtype
);
1916 return build_x_component_ref (build_scoped_ref (datum
, basetype
),
1919 return error_mark_node
;
1922 /* Like `build_component_ref, but uses an already found field, and converts
1923 from a reference. Must compute access for current_class_ref.
1927 build_component_ref_1 (datum
, field
, protect
)
1931 return convert_from_reference
1932 (build_component_ref (datum
, field
, NULL_TREE
, protect
));
1935 /* Given a COND_EXPR, MIN_EXPR, or MAX_EXPR in T, return it in a form that we
1936 can, for example, use as an lvalue. This code used to be in
1937 unary_complex_lvalue, but we needed it to deal with `a = (d == c) ? b : c'
1938 expressions, where we're dealing with aggregates. But now it's again only
1939 called from unary_complex_lvalue. The case (in particular) that led to
1940 this was with CODE == ADDR_EXPR, since it's not an lvalue when we'd
1944 rationalize_conditional_expr (code
, t
)
1945 enum tree_code code
;
1948 /* For MIN_EXPR or MAX_EXPR, fold-const.c has arranged things so that
1949 the first operand is always the one to be used if both operands
1950 are equal, so we know what conditional expression this used to be. */
1951 if (TREE_CODE (t
) == MIN_EXPR
|| TREE_CODE (t
) == MAX_EXPR
)
1954 build_conditional_expr (build_x_binary_op ((TREE_CODE (t
) == MIN_EXPR
1955 ? LE_EXPR
: GE_EXPR
),
1956 TREE_OPERAND (t
, 0),
1957 TREE_OPERAND (t
, 1)),
1958 build_unary_op (code
, TREE_OPERAND (t
, 0), 0),
1959 build_unary_op (code
, TREE_OPERAND (t
, 1), 0));
1963 build_conditional_expr (TREE_OPERAND (t
, 0),
1964 build_unary_op (code
, TREE_OPERAND (t
, 1), 0),
1965 build_unary_op (code
, TREE_OPERAND (t
, 2), 0));
1968 /* Given the TYPE of an anonymous union field inside T, return the
1969 FIELD_DECL for the field. If not found return NULL_TREE. Because
1970 anonymous unions can nest, we must also search all anonymous unions
1971 that are directly reachable. */
1974 lookup_anon_field (t
, type
)
1979 for (field
= TYPE_FIELDS (t
); field
; field
= TREE_CHAIN (field
))
1981 if (TREE_STATIC (field
))
1983 if (TREE_CODE (field
) != FIELD_DECL
)
1986 /* If we find it directly, return the field. */
1987 if (DECL_NAME (field
) == NULL_TREE
1988 && type
== TREE_TYPE (field
))
1993 /* Otherwise, it could be nested, search harder. */
1994 if (DECL_NAME (field
) == NULL_TREE
1995 && TREE_CODE (TREE_TYPE (field
)) == UNION_TYPE
)
1997 tree subfield
= lookup_anon_field (TREE_TYPE (field
), type
);
2005 /* Build a COMPONENT_REF for a given DATUM, and it's member COMPONENT.
2006 COMPONENT can be an IDENTIFIER_NODE that is the name of the member
2007 that we are interested in, or it can be a FIELD_DECL. */
2010 build_component_ref (datum
, component
, basetype_path
, protect
)
2011 tree datum
, component
, basetype_path
;
2014 register tree basetype
;
2015 register enum tree_code code
;
2016 register tree field
= NULL
;
2021 if (processing_template_decl
)
2022 return build_min_nt (COMPONENT_REF
, datum
, component
);
2024 if (datum
== error_mark_node
2025 || TREE_TYPE (datum
) == error_mark_node
)
2026 return error_mark_node
;
2028 /* BASETYPE holds the type of the class containing the COMPONENT. */
2029 basetype
= TYPE_MAIN_VARIANT (TREE_TYPE (datum
));
2031 /* If DATUM is a COMPOUND_EXPR or COND_EXPR, move our reference
2033 switch (TREE_CODE (datum
))
2037 tree value
= build_component_ref (TREE_OPERAND (datum
, 1), component
,
2038 basetype_path
, protect
);
2039 return build (COMPOUND_EXPR
, TREE_TYPE (value
),
2040 TREE_OPERAND (datum
, 0), value
);
2043 return build_conditional_expr
2044 (TREE_OPERAND (datum
, 0),
2045 build_component_ref (TREE_OPERAND (datum
, 1), component
,
2046 basetype_path
, protect
),
2047 build_component_ref (TREE_OPERAND (datum
, 2), component
,
2048 basetype_path
, protect
));
2051 cp_error ("invalid use of %D", datum
);
2052 datum
= error_mark_node
;
2059 code
= TREE_CODE (basetype
);
2061 if (code
== REFERENCE_TYPE
)
2063 datum
= convert_from_reference (datum
);
2064 basetype
= TYPE_MAIN_VARIANT (TREE_TYPE (datum
));
2065 code
= TREE_CODE (basetype
);
2067 if (TREE_CODE (datum
) == OFFSET_REF
)
2069 datum
= resolve_offset_ref (datum
);
2070 basetype
= TYPE_MAIN_VARIANT (TREE_TYPE (datum
));
2071 code
= TREE_CODE (basetype
);
2074 /* First, see if there is a field or component with name COMPONENT. */
2075 if (TREE_CODE (component
) == TREE_LIST
)
2077 /* I could not trigger this code. MvL */
2078 my_friendly_abort (980326);
2080 my_friendly_assert (!(TREE_CHAIN (component
) == NULL_TREE
2081 && DECL_CHAIN (TREE_VALUE (component
)) == NULL_TREE
), 309);
2083 return build (COMPONENT_REF
, TREE_TYPE (component
), datum
, component
);
2086 if (! IS_AGGR_TYPE_CODE (code
))
2088 if (code
!= ERROR_MARK
)
2089 cp_error ("request for member `%D' in `%E', which is of non-aggregate type `%T'",
2090 component
, datum
, basetype
);
2091 return error_mark_node
;
2094 if (!complete_type_or_else (basetype
, datum
))
2095 return error_mark_node
;
2097 if (TREE_CODE (component
) == BIT_NOT_EXPR
)
2099 if (TYPE_IDENTIFIER (basetype
) != TREE_OPERAND (component
, 0))
2101 cp_error ("destructor specifier `%T::~%T' must have matching names",
2102 basetype
, TREE_OPERAND (component
, 0));
2103 return error_mark_node
;
2105 if (! TYPE_HAS_DESTRUCTOR (basetype
))
2107 cp_error ("type `%T' has no destructor", basetype
);
2108 return error_mark_node
;
2110 return TREE_VEC_ELT (CLASSTYPE_METHOD_VEC (basetype
), 1);
2113 /* Look up component name in the structure type definition. */
2114 if (CLASSTYPE_VFIELD (basetype
)
2115 && DECL_NAME (CLASSTYPE_VFIELD (basetype
)) == component
)
2116 /* Special-case this because if we use normal lookups in an ambiguous
2117 hierarchy, the compiler will abort (because vptr lookups are
2118 not supposed to be ambiguous. */
2119 field
= CLASSTYPE_VFIELD (basetype
);
2120 else if (TREE_CODE (component
) == FIELD_DECL
)
2122 else if (TREE_CODE (component
) == TYPE_DECL
)
2124 cp_error ("invalid use of type decl `%#D' as expression", component
);
2125 return error_mark_node
;
2129 tree name
= component
;
2130 if (TREE_CODE (component
) == VAR_DECL
)
2131 name
= DECL_NAME (component
);
2132 if (basetype_path
== NULL_TREE
)
2133 basetype_path
= TYPE_BINFO (basetype
);
2134 field
= lookup_field (basetype_path
, name
,
2135 protect
&& !VFIELD_NAME_P (name
), 0);
2136 if (field
== error_mark_node
)
2137 return error_mark_node
;
2139 if (field
== NULL_TREE
)
2141 /* Not found as a data field, look for it as a method. If found,
2142 then if this is the only possible one, return it, else
2143 report ambiguity error. */
2144 tree fndecls
= lookup_fnfields (basetype_path
, name
, 1);
2145 if (fndecls
== error_mark_node
)
2146 return error_mark_node
;
2149 /* If the function is unique and static, we can resolve it
2150 now. Otherwise, we have to wait and see what context it is
2151 used in; a component_ref involving a non-static member
2152 function can only be used in a call (expr.ref). */
2154 if (TREE_CHAIN (fndecls
) == NULL_TREE
2155 && TREE_CODE (TREE_VALUE (fndecls
)) == FUNCTION_DECL
)
2157 if (DECL_STATIC_FUNCTION_P (TREE_VALUE (fndecls
)))
2159 tree fndecl
= TREE_VALUE (fndecls
);
2160 enforce_access (TREE_PURPOSE (fndecls
), fndecl
);
2166 /* A unique non-static member function. Other parts
2167 of the compiler expect something with
2168 unknown_type_node to be really overloaded, so
2170 TREE_VALUE (fndecls
)
2171 = scratch_ovl_cons (TREE_VALUE (fndecls
), NULL_TREE
);
2175 ref
= build (COMPONENT_REF
, unknown_type_node
,
2180 cp_error ("`%#T' has no member named `%D'", basetype
, name
);
2181 return error_mark_node
;
2183 else if (TREE_TYPE (field
) == error_mark_node
)
2184 return error_mark_node
;
2186 if (TREE_CODE (field
) != FIELD_DECL
)
2188 if (TREE_CODE (field
) == TYPE_DECL
)
2189 cp_pedwarn ("invalid use of type decl `%#D' as expression", field
);
2190 else if (DECL_RTL (field
) != 0)
2193 TREE_USED (field
) = 1;
2198 /* See if we have to do any conversions so that we pick up the field from the
2200 if (DECL_FIELD_CONTEXT (field
) != basetype
)
2202 tree context
= DECL_FIELD_CONTEXT (field
);
2203 tree base
= context
;
2204 while (!same_type_p (base
, basetype
) && TYPE_NAME (base
)
2205 && ANON_UNION_TYPE_P (base
))
2207 base
= TYPE_CONTEXT (base
);
2210 /* Handle base classes here... */
2211 if (base
!= basetype
&& TYPE_USES_COMPLEX_INHERITANCE (basetype
))
2213 tree addr
= build_unary_op (ADDR_EXPR
, datum
, 0);
2214 if (integer_zerop (addr
))
2216 error ("invalid reference to NULL ptr, use ptr-to-member instead");
2217 return error_mark_node
;
2219 if (VBASE_NAME_P (DECL_NAME (field
)))
2221 /* It doesn't matter which vbase pointer we grab, just
2222 find one of them. */
2223 tree binfo
= get_binfo (base
,
2224 TREE_TYPE (TREE_TYPE (addr
)), 0);
2225 addr
= convert_pointer_to_real (binfo
, addr
);
2228 addr
= convert_pointer_to (base
, addr
);
2229 datum
= build_indirect_ref (addr
, NULL_PTR
);
2230 my_friendly_assert (datum
!= error_mark_node
, 311);
2234 /* Handle things from anon unions here... */
2235 if (TYPE_NAME (context
) && ANON_UNION_TYPE_P (context
))
2237 tree subfield
= lookup_anon_field (basetype
, context
);
2238 tree subdatum
= build_component_ref (datum
, subfield
,
2239 basetype_path
, protect
);
2240 return build_component_ref (subdatum
, field
, basetype_path
, protect
);
2244 /* Compute the type of the field, as described in [expr.ref]. */
2245 type_quals
= TYPE_UNQUALIFIED
;
2246 field_type
= TREE_TYPE (field
);
2247 if (TREE_CODE (field_type
) == REFERENCE_TYPE
)
2248 /* The standard says that the type of the result should be the
2249 type referred to by the reference. But for now, at least, we
2250 do the conversion from reference type later. */
2254 type_quals
= (CP_TYPE_QUALS (field_type
)
2255 | CP_TYPE_QUALS (TREE_TYPE (datum
)));
2257 /* A field is const (volatile) if the enclosing object, or the
2258 field itself, is const (volatile). But, a mutable field is
2259 not const, even within a const object. */
2260 if (DECL_LANG_SPECIFIC (field
) && DECL_MUTABLE_P (field
))
2261 type_quals
&= ~TYPE_QUAL_CONST
;
2262 if (!IS_SIGNATURE (field_type
))
2263 field_type
= cp_build_qualified_type (field_type
, type_quals
);
2266 ref
= fold (build (COMPONENT_REF
, field_type
,
2267 break_out_cleanups (datum
), field
));
2269 /* Mark the expression const or volatile, as appropriate. Even
2270 though we've dealt with the type above, we still have to mark the
2271 expression itself. */
2272 if (type_quals
& TYPE_QUAL_CONST
)
2273 TREE_READONLY (ref
) = 1;
2274 else if (type_quals
& TYPE_QUAL_VOLATILE
)
2275 TREE_THIS_VOLATILE (ref
) = 1;
2280 /* Variant of build_component_ref for use in expressions, which should
2281 never have REFERENCE_TYPE. */
2284 build_x_component_ref (datum
, component
, basetype_path
, protect
)
2285 tree datum
, component
, basetype_path
;
2288 tree t
= build_component_ref (datum
, component
, basetype_path
, protect
);
2290 if (! processing_template_decl
)
2291 t
= convert_from_reference (t
);
2296 /* Given an expression PTR for a pointer, return an expression
2297 for the value pointed to.
2298 ERRORSTRING is the name of the operator to appear in error messages.
2300 This function may need to overload OPERATOR_FNNAME.
2301 Must also handle REFERENCE_TYPEs for C++. */
2304 build_x_indirect_ref (ptr
, errorstring
)
2310 if (processing_template_decl
)
2311 return build_min_nt (INDIRECT_REF
, ptr
);
2313 rval
= build_opfncall (INDIRECT_REF
, LOOKUP_NORMAL
, ptr
, NULL_TREE
,
2317 return build_indirect_ref (ptr
, errorstring
);
2321 build_indirect_ref (ptr
, errorstring
)
2325 register tree pointer
, type
;
2327 if (ptr
== error_mark_node
)
2328 return error_mark_node
;
2330 pointer
= (TREE_CODE (TREE_TYPE (ptr
)) == REFERENCE_TYPE
2331 ? ptr
: default_conversion (ptr
));
2332 type
= TREE_TYPE (pointer
);
2334 if (ptr
== current_class_ptr
)
2335 return current_class_ref
;
2337 if (TYPE_PTR_P (type
) || TREE_CODE (type
) == REFERENCE_TYPE
)
2341 If the type of the expression is "pointer to T," the type
2342 of the result is "T."
2344 We must use the canonical variant because certain parts of
2345 the back end, like fold, do pointer comparisons between
2347 tree t
= canonical_type_variant (TREE_TYPE (type
));
2349 if (TREE_CODE (pointer
) == ADDR_EXPR
2351 && same_type_p (t
, TREE_TYPE (TREE_OPERAND (pointer
, 0))))
2352 /* The POINTER was something like `&x'. We simplify `*&x' to
2354 return TREE_OPERAND (pointer
, 0);
2357 tree ref
= build1 (INDIRECT_REF
, t
, pointer
);
2359 /* We *must* set TREE_READONLY when dereferencing a pointer to const,
2360 so that we get the proper error message if the result is used
2361 to assign to. Also, &* is supposed to be a no-op. */
2362 TREE_READONLY (ref
) = CP_TYPE_CONST_P (t
);
2363 TREE_THIS_VOLATILE (ref
) = CP_TYPE_VOLATILE_P (t
);
2364 TREE_SIDE_EFFECTS (ref
)
2365 = (TREE_THIS_VOLATILE (ref
) || TREE_SIDE_EFFECTS (pointer
)
2370 /* `pointer' won't be an error_mark_node if we were given a
2371 pointer to member, so it's cool to check for this here. */
2372 else if (TYPE_PTRMEM_P (type
) || TYPE_PTRMEMFUNC_P (type
))
2373 error ("invalid use of `%s' on pointer to member", errorstring
);
2374 else if (TREE_CODE (type
) == RECORD_TYPE
2375 && (IS_SIGNATURE_POINTER (type
) || IS_SIGNATURE_REFERENCE (type
)))
2376 error ("cannot dereference signature pointer/reference");
2377 else if (pointer
!= error_mark_node
)
2380 error ("invalid type argument of `%s'", errorstring
);
2382 error ("invalid type argument");
2384 return error_mark_node
;
2387 /* This handles expressions of the form "a[i]", which denotes
2390 This is logically equivalent in C to *(a+i), but we may do it differently.
2391 If A is a variable or a member, we generate a primitive ARRAY_REF.
2392 This avoids forcing the array out of registers, and can work on
2393 arrays that are not lvalues (for example, members of structures returned
2396 If INDEX is of some user-defined type, it must be converted to
2397 integer type. Otherwise, to make a compatible PLUS_EXPR, it
2398 will inherit the type of the array, which will be some pointer type. */
2401 build_array_ref (array
, idx
)
2406 error ("subscript missing in array reference");
2407 return error_mark_node
;
2410 if (TREE_TYPE (array
) == error_mark_node
2411 || TREE_TYPE (idx
) == error_mark_node
)
2412 return error_mark_node
;
2414 if (TREE_CODE (TREE_TYPE (array
)) == ARRAY_TYPE
2415 && TREE_CODE (array
) != INDIRECT_REF
)
2419 /* Subscripting with type char is likely to lose
2420 on a machine where chars are signed.
2421 So warn on any machine, but optionally.
2422 Don't warn for unsigned char since that type is safe.
2423 Don't warn for signed char because anyone who uses that
2424 must have done so deliberately. */
2425 if (warn_char_subscripts
2426 && TYPE_MAIN_VARIANT (TREE_TYPE (idx
)) == char_type_node
)
2427 warning ("array subscript has type `char'");
2429 /* Apply default promotions *after* noticing character types. */
2430 idx
= default_conversion (idx
);
2432 if (TREE_CODE (TREE_TYPE (idx
)) != INTEGER_TYPE
)
2434 error ("array subscript is not an integer");
2435 return error_mark_node
;
2438 /* An array that is indexed by a non-constant
2439 cannot be stored in a register; we must be able to do
2440 address arithmetic on its address.
2441 Likewise an array of elements of variable size. */
2442 if (TREE_CODE (idx
) != INTEGER_CST
2443 || (TYPE_SIZE (TREE_TYPE (TREE_TYPE (array
))) != 0
2444 && (TREE_CODE (TYPE_SIZE (TREE_TYPE (TREE_TYPE (array
))))
2447 if (mark_addressable (array
) == 0)
2448 return error_mark_node
;
2450 /* An array that is indexed by a constant value which is not within
2451 the array bounds cannot be stored in a register either; because we
2452 would get a crash in store_bit_field/extract_bit_field when trying
2453 to access a non-existent part of the register. */
2454 if (TREE_CODE (idx
) == INTEGER_CST
2455 && TYPE_VALUES (TREE_TYPE (array
))
2456 && ! int_fits_type_p (idx
, TYPE_VALUES (TREE_TYPE (array
))))
2458 if (mark_addressable (array
) == 0)
2459 return error_mark_node
;
2462 if (pedantic
&& !lvalue_p (array
))
2463 pedwarn ("ANSI C++ forbids subscripting non-lvalue array");
2465 /* Note in C++ it is valid to subscript a `register' array, since
2466 it is valid to take the address of something with that
2467 storage specification. */
2471 while (TREE_CODE (foo
) == COMPONENT_REF
)
2472 foo
= TREE_OPERAND (foo
, 0);
2473 if (TREE_CODE (foo
) == VAR_DECL
&& DECL_REGISTER (foo
))
2474 warning ("subscripting array declared `register'");
2477 type
= TREE_TYPE (TREE_TYPE (array
));
2478 rval
= build (ARRAY_REF
, type
, array
, idx
);
2479 /* Array ref is const/volatile if the array elements are
2480 or if the array is.. */
2481 TREE_READONLY (rval
)
2482 |= (CP_TYPE_CONST_P (type
) | TREE_READONLY (array
));
2483 TREE_SIDE_EFFECTS (rval
)
2484 |= (CP_TYPE_VOLATILE_P (type
) | TREE_SIDE_EFFECTS (array
));
2485 TREE_THIS_VOLATILE (rval
)
2486 |= (CP_TYPE_VOLATILE_P (type
) | TREE_THIS_VOLATILE (array
));
2487 return require_complete_type (fold (rval
));
2491 tree ar
= default_conversion (array
);
2492 tree ind
= default_conversion (idx
);
2494 /* Put the integer in IND to simplify error checking. */
2495 if (TREE_CODE (TREE_TYPE (ar
)) == INTEGER_TYPE
)
2502 if (ar
== error_mark_node
)
2505 if (TREE_CODE (TREE_TYPE (ar
)) != POINTER_TYPE
)
2507 error ("subscripted value is neither array nor pointer");
2508 return error_mark_node
;
2510 if (TREE_CODE (TREE_TYPE (ind
)) != INTEGER_TYPE
)
2512 error ("array subscript is not an integer");
2513 return error_mark_node
;
2516 return build_indirect_ref (build_binary_op_nodefault (PLUS_EXPR
, ar
,
2522 /* Build a function call to function FUNCTION with parameters PARAMS.
2523 PARAMS is a list--a chain of TREE_LIST nodes--in which the
2524 TREE_VALUE of each node is a parameter-expression. The PARAMS do
2525 not include any object pointer that may be required. FUNCTION's
2526 data type may be a function type or a pointer-to-function.
2528 For C++: If FUNCTION's data type is a TREE_LIST, then the tree list
2529 is the list of possible methods that FUNCTION could conceivably
2530 be. If the list of methods comes from a class, then it will be
2531 a list of lists (where each element is associated with the class
2532 that produced it), otherwise it will be a simple list (for
2533 functions overloaded in global scope).
2535 In the first case, TREE_VALUE (function) is the head of one of those
2536 lists, and TREE_PURPOSE is the name of the function.
2538 In the second case, TREE_PURPOSE (function) is the function's
2541 DECL is the class instance variable, usually CURRENT_CLASS_REF.
2543 When calling a TEMPLATE_DECL, we don't require a complete return
2547 build_x_function_call (function
, params
, decl
)
2548 tree function
, params
, decl
;
2551 tree template_id
= NULL_TREE
;
2554 if (function
== error_mark_node
)
2555 return error_mark_node
;
2557 if (processing_template_decl
)
2558 return build_min_nt (CALL_EXPR
, function
, params
, NULL_TREE
);
2560 /* Save explicit template arguments if found */
2561 if (TREE_CODE (function
) == TEMPLATE_ID_EXPR
)
2563 template_id
= function
;
2564 function
= TREE_OPERAND (function
, 0);
2567 type
= TREE_TYPE (function
);
2569 if (TREE_CODE (type
) == OFFSET_TYPE
2570 && TREE_TYPE (type
) == unknown_type_node
2571 && TREE_CODE (function
) == TREE_LIST
2572 && TREE_CHAIN (function
) == NULL_TREE
)
2574 /* Undo (Foo:bar)()... */
2575 type
= TYPE_OFFSET_BASETYPE (type
);
2576 function
= TREE_VALUE (function
);
2577 my_friendly_assert (TREE_CODE (function
) == TREE_LIST
, 999);
2578 my_friendly_assert (TREE_CHAIN (function
) == NULL_TREE
, 999);
2579 function
= TREE_VALUE (function
);
2580 if (TREE_CODE (function
) == OVERLOAD
)
2581 function
= OVL_FUNCTION (function
);
2582 my_friendly_assert (TREE_CODE (function
) == FUNCTION_DECL
, 999);
2583 function
= DECL_NAME (function
);
2584 return build_method_call (decl
, function
, params
,
2585 TYPE_BINFO (type
), LOOKUP_NORMAL
);
2588 is_method
= ((TREE_CODE (function
) == TREE_LIST
2589 && current_class_type
!= NULL_TREE
2590 && (IDENTIFIER_CLASS_VALUE (TREE_PURPOSE (function
))
2592 || TREE_CODE (function
) == IDENTIFIER_NODE
2593 || TREE_CODE (type
) == METHOD_TYPE
2594 || TYPE_PTRMEMFUNC_P (type
));
2596 if ((TREE_CODE (function
) == FUNCTION_DECL
2597 && DECL_STATIC_FUNCTION_P (function
))
2598 || (TREE_CODE (function
) == TEMPLATE_DECL
2599 && DECL_STATIC_FUNCTION_P (DECL_RESULT (function
))))
2600 return build_member_call
2601 (DECL_CONTEXT (function
), DECL_NAME (function
), params
);
2603 /* A friend template. Make it look like a toplevel declaration. */
2604 if (! is_method
&& TREE_CODE (function
) == TEMPLATE_DECL
)
2605 function
= scratch_ovl_cons (function
, NULL_TREE
);
2607 /* Handle methods, friends, and overloaded functions, respectively. */
2610 tree basetype
= NULL_TREE
;
2612 if (TREE_CODE (function
) == FUNCTION_DECL
2613 || DECL_FUNCTION_TEMPLATE_P (function
))
2615 basetype
= DECL_CLASS_CONTEXT (function
);
2617 if (DECL_NAME (function
))
2618 function
= DECL_NAME (function
);
2620 function
= TYPE_IDENTIFIER (DECL_CLASS_CONTEXT (function
));
2622 else if (TREE_CODE (function
) == TREE_LIST
)
2624 my_friendly_assert (TREE_CODE (TREE_VALUE (function
))
2625 == FUNCTION_DECL
, 312);
2626 basetype
= DECL_CLASS_CONTEXT (TREE_VALUE (function
));
2627 function
= TREE_PURPOSE (function
);
2629 else if (TREE_CODE (function
) != IDENTIFIER_NODE
)
2631 if (TREE_CODE (function
) == OFFSET_REF
)
2633 if (TREE_OPERAND (function
, 0))
2634 decl
= TREE_OPERAND (function
, 0);
2636 /* Call via a pointer to member function. */
2637 if (decl
== NULL_TREE
)
2639 error ("pointer to member function called, but not in class scope");
2640 return error_mark_node
;
2642 /* What other type of POINTER_TYPE could this be? */
2643 if (TREE_CODE (TREE_TYPE (function
)) != POINTER_TYPE
2644 && ! TYPE_PTRMEMFUNC_P (TREE_TYPE (function
))
2645 && TREE_CODE (function
) != OFFSET_REF
)
2646 function
= build (OFFSET_REF
, TREE_TYPE (type
), NULL_TREE
,
2651 /* this is an abbreviated method call.
2652 must go through here in case it is a virtual function.
2653 @@ Perhaps this could be optimized. */
2655 if (basetype
&& (! current_class_type
2656 || ! DERIVED_FROM_P (basetype
, current_class_type
)))
2657 return build_member_call (basetype
, function
, params
);
2659 if (decl
== NULL_TREE
)
2661 if (current_class_type
== NULL_TREE
)
2663 error ("object missing in call to method `%s'",
2664 IDENTIFIER_POINTER (function
));
2665 return error_mark_node
;
2667 /* Yow: call from a static member function. */
2668 decl
= build_dummy_object (current_class_type
);
2671 /* Put back explicit template arguments, if any. */
2673 function
= template_id
;
2674 return build_method_call (decl
, function
, params
,
2675 NULL_TREE
, LOOKUP_NORMAL
);
2677 else if (TREE_CODE (function
) == COMPONENT_REF
2678 && type
== unknown_type_node
)
2680 /* Undo what we did in build_component_ref. */
2681 decl
= TREE_OPERAND (function
, 0);
2682 function
= TREE_OPERAND (function
, 1);
2683 function
= DECL_NAME (OVL_CURRENT (TREE_VALUE (function
)));
2684 return build_method_call (decl
, function
, params
,
2685 NULL_TREE
, LOOKUP_NORMAL
);
2687 else if (really_overloaded_fn (function
))
2689 if (OVL_FUNCTION (function
) == NULL_TREE
)
2691 cp_error ("function `%D' declared overloaded, but no definitions appear with which to resolve it?!?",
2692 TREE_PURPOSE (function
));
2693 return error_mark_node
;
2697 /* Put back explicit template arguments, if any. */
2699 function
= template_id
;
2700 return build_new_function_call (function
, params
);
2704 /* Remove a potential OVERLOAD around it */
2705 function
= OVL_CURRENT (function
);
2708 if (TREE_CODE (function
) == OFFSET_REF
)
2710 /* If the component is a data element (or a virtual function), we play
2711 games here to make things work. */
2714 if (TREE_OPERAND (function
, 0))
2715 decl
= TREE_OPERAND (function
, 0);
2717 decl
= current_class_ref
;
2719 decl_addr
= build_unary_op (ADDR_EXPR
, decl
, 0);
2721 /* Sigh. OFFSET_REFs are being used for too many things.
2722 They're being used both for -> and ->*, and we want to resolve
2723 the -> cases here, but leave the ->*. We could use
2724 resolve_offset_ref for those, too, but it would call
2725 get_member_function_from_ptrfunc and decl_addr wouldn't get
2726 updated properly. Nasty. */
2727 if (TREE_CODE (TREE_OPERAND (function
, 1)) == FIELD_DECL
)
2728 function
= resolve_offset_ref (function
);
2730 function
= TREE_OPERAND (function
, 1);
2732 function
= get_member_function_from_ptrfunc (&decl_addr
, function
);
2733 params
= expr_tree_cons (NULL_TREE
, decl_addr
, params
);
2734 return build_function_call (function
, params
);
2737 type
= TREE_TYPE (function
);
2738 if (type
!= error_mark_node
)
2740 if (TREE_CODE (type
) == REFERENCE_TYPE
)
2741 type
= TREE_TYPE (type
);
2743 if (IS_AGGR_TYPE (type
))
2744 return build_opfncall (CALL_EXPR
, LOOKUP_NORMAL
, function
, params
, NULL_TREE
);
2749 tree fntype
= TREE_TYPE (function
);
2750 tree ctypeptr
= NULL_TREE
;
2752 /* Explicitly named method? */
2753 if (TREE_CODE (function
) == FUNCTION_DECL
)
2754 ctypeptr
= build_pointer_type (DECL_CLASS_CONTEXT (function
));
2755 /* Expression with ptr-to-method type? It could either be a plain
2756 usage, or it might be a case where the ptr-to-method is being
2757 passed in as an argument. */
2758 else if (TYPE_PTRMEMFUNC_P (fntype
))
2760 tree rec
= TYPE_METHOD_BASETYPE (TREE_TYPE
2761 (TYPE_PTRMEMFUNC_FN_TYPE (fntype
)));
2762 ctypeptr
= build_pointer_type (rec
);
2764 /* Unexpected node type? */
2766 my_friendly_abort (116);
2767 if (decl
== NULL_TREE
)
2769 if (current_function_decl
2770 && DECL_STATIC_FUNCTION_P (current_function_decl
))
2771 error ("invalid call to member function needing `this' in static member function scope");
2773 error ("pointer to member function called, but not in class scope");
2774 return error_mark_node
;
2776 if (TREE_CODE (TREE_TYPE (decl
)) != POINTER_TYPE
2777 && ! TYPE_PTRMEMFUNC_P (TREE_TYPE (decl
)))
2779 decl
= build_unary_op (ADDR_EXPR
, decl
, 0);
2780 decl
= convert_pointer_to (TREE_TYPE (ctypeptr
), decl
);
2783 decl
= build_c_cast (ctypeptr
, decl
);
2784 params
= expr_tree_cons (NULL_TREE
, decl
, params
);
2787 return build_function_call (function
, params
);
2790 /* Resolve a pointer to member function. INSTANCE is the object
2791 instance to use, if the member points to a virtual member. */
2794 get_member_function_from_ptrfunc (instance_ptrptr
, function
)
2795 tree
*instance_ptrptr
;
2798 if (TREE_CODE (function
) == OFFSET_REF
)
2800 function
= TREE_OPERAND (function
, 1);
2803 if (TYPE_PTRMEMFUNC_P (TREE_TYPE (function
)))
2805 tree fntype
, idx
, e1
, delta
, delta2
, e2
, e3
, aref
, vtbl
;
2806 tree instance
, basetype
;
2808 tree instance_ptr
= *instance_ptrptr
;
2810 if (TREE_SIDE_EFFECTS (instance_ptr
))
2811 instance_ptr
= save_expr (instance_ptr
);
2813 if (TREE_SIDE_EFFECTS (function
))
2814 function
= save_expr (function
);
2816 fntype
= TYPE_PTRMEMFUNC_FN_TYPE (TREE_TYPE (function
));
2817 basetype
= TYPE_METHOD_BASETYPE (TREE_TYPE (fntype
));
2819 delta
= cp_convert (ptrdiff_type_node
,
2820 build_component_ref (function
, delta_identifier
,
2822 e3
= PFN_FROM_PTRMEMFUNC (function
);
2824 if (TYPE_SIZE (basetype
) != NULL_TREE
2825 && ! TYPE_VIRTUAL_P (basetype
))
2826 /* If basetype doesn't have virtual functions, don't emit code to
2827 handle that case. */
2831 /* Promoting idx before saving it improves performance on RISC
2832 targets. Without promoting, the first compare used
2833 load-with-sign-extend, while the second used normal load then
2834 shift to sign-extend. An optimizer flaw, perhaps, but it's
2835 easier to make this change. */
2836 idx
= save_expr (default_conversion
2837 (build_component_ref (function
,
2840 e1
= build_binary_op (GT_EXPR
, idx
, integer_zero_node
, 1);
2842 /* Convert down to the right base, before using the instance. */
2843 instance
= convert_pointer_to_real (basetype
, instance_ptr
);
2844 if (instance
== error_mark_node
&& instance_ptr
!= error_mark_node
)
2847 vtbl
= convert_pointer_to (ptr_type_node
, instance
);
2848 delta2
= DELTA2_FROM_PTRMEMFUNC (function
);
2851 build_pointer_type (build_pointer_type (vtable_entry_type
)),
2852 vtbl
, cp_convert (ptrdiff_type_node
, delta2
));
2853 vtbl
= build_indirect_ref (vtbl
, NULL_PTR
);
2854 aref
= build_array_ref (vtbl
, build_binary_op (MINUS_EXPR
,
2856 integer_one_node
, 1));
2857 if (! flag_vtable_thunks
)
2859 aref
= save_expr (aref
);
2861 delta
= build_binary_op
2863 build_conditional_expr (e1
,
2864 build_component_ref (aref
,
2871 if (flag_vtable_thunks
)
2874 e2
= build_component_ref (aref
, pfn_identifier
, NULL_TREE
, 0);
2875 TREE_TYPE (e2
) = TREE_TYPE (e3
);
2876 e1
= build_conditional_expr (e1
, e2
, e3
);
2878 /* Make sure this doesn't get evaluated first inside one of the
2879 branches of the COND_EXPR. */
2880 if (TREE_CODE (instance_ptr
) == SAVE_EXPR
)
2881 e1
= build (COMPOUND_EXPR
, TREE_TYPE (e1
),
2885 *instance_ptrptr
= build (PLUS_EXPR
, TREE_TYPE (instance_ptr
),
2886 instance_ptr
, delta
);
2888 if (instance_ptr
== error_mark_node
2889 && TREE_CODE (e1
) != ADDR_EXPR
2890 && TREE_CODE (TREE_OPERAND (e1
, 0)) != FUNCTION_DECL
)
2891 cp_error ("object missing in `%E'", function
);
2899 build_function_call_real (function
, params
, require_complete
, flags
)
2900 tree function
, params
;
2901 int require_complete
, flags
;
2903 register tree fntype
, fndecl
;
2904 register tree value_type
;
2905 register tree coerced_params
;
2906 tree name
= NULL_TREE
, assembler_name
= NULL_TREE
;
2909 /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
2910 Strip such NOP_EXPRs, since FUNCTION is used in non-lvalue context. */
2911 if (TREE_CODE (function
) == NOP_EXPR
2912 && TREE_TYPE (function
) == TREE_TYPE (TREE_OPERAND (function
, 0)))
2913 function
= TREE_OPERAND (function
, 0);
2915 if (TREE_CODE (function
) == FUNCTION_DECL
)
2917 name
= DECL_NAME (function
);
2918 assembler_name
= DECL_ASSEMBLER_NAME (function
);
2920 GNU_xref_call (current_function_decl
,
2921 IDENTIFIER_POINTER (name
? name
2922 : TYPE_IDENTIFIER (DECL_CLASS_CONTEXT
2924 mark_used (function
);
2927 /* Convert anything with function type to a pointer-to-function. */
2928 if (pedantic
&& DECL_MAIN_P (function
))
2929 pedwarn ("ANSI C++ forbids calling `main' from within program");
2931 /* Differs from default_conversion by not setting TREE_ADDRESSABLE
2932 (because calling an inline function does not mean the function
2933 needs to be separately compiled). */
2935 if (DECL_INLINE (function
))
2936 function
= inline_conversion (function
);
2938 function
= build_addr_func (function
);
2944 function
= build_addr_func (function
);
2947 if (function
== error_mark_node
)
2948 return error_mark_node
;
2950 fntype
= TREE_TYPE (function
);
2952 if (TYPE_PTRMEMFUNC_P (fntype
))
2954 cp_error ("must use .* or ->* to call pointer-to-member function in `%E (...)'",
2956 return error_mark_node
;
2959 is_method
= (TREE_CODE (fntype
) == POINTER_TYPE
2960 && TREE_CODE (TREE_TYPE (fntype
)) == METHOD_TYPE
);
2962 if (!((TREE_CODE (fntype
) == POINTER_TYPE
2963 && TREE_CODE (TREE_TYPE (fntype
)) == FUNCTION_TYPE
)
2965 || TREE_CODE (function
) == TEMPLATE_ID_EXPR
))
2967 cp_error ("`%E' cannot be used as a function", function
);
2968 return error_mark_node
;
2971 /* fntype now gets the type of function pointed to. */
2972 fntype
= TREE_TYPE (fntype
);
2974 /* Convert the parameters to the types declared in the
2975 function prototype, or apply default promotions. */
2977 if (flags
& LOOKUP_COMPLAIN
)
2978 coerced_params
= convert_arguments (TYPE_ARG_TYPES (fntype
),
2979 params
, fndecl
, LOOKUP_NORMAL
);
2981 coerced_params
= convert_arguments (TYPE_ARG_TYPES (fntype
),
2984 if (coerced_params
== error_mark_node
)
2986 if (flags
& LOOKUP_SPECULATIVELY
)
2989 return error_mark_node
;
2992 /* Check for errors in format strings. */
2994 if (warn_format
&& (name
|| assembler_name
))
2995 check_function_format (name
, assembler_name
, coerced_params
);
2997 /* Recognize certain built-in functions so we can make tree-codes
2998 other than CALL_EXPR. We do this when it enables fold-const.c
2999 to do something useful. */
3001 if (TREE_CODE (function
) == ADDR_EXPR
3002 && TREE_CODE (TREE_OPERAND (function
, 0)) == FUNCTION_DECL
3003 && DECL_BUILT_IN (TREE_OPERAND (function
, 0)))
3004 switch (DECL_FUNCTION_CODE (TREE_OPERAND (function
, 0)))
3009 if (coerced_params
== 0)
3010 return integer_zero_node
;
3011 return build_unary_op (ABS_EXPR
, TREE_VALUE (coerced_params
), 0);
3018 value_type
= TREE_TYPE (fntype
) ? TREE_TYPE (fntype
) : void_type_node
;
3020 register tree result
3021 = build_call (function
, value_type
, coerced_params
);
3023 if (require_complete
)
3025 if (TREE_CODE (value_type
) == VOID_TYPE
)
3027 result
= require_complete_type (result
);
3029 if (IS_AGGR_TYPE (value_type
))
3030 result
= build_cplus_new (value_type
, result
);
3031 return convert_from_reference (result
);
3036 build_function_call (function
, params
)
3037 tree function
, params
;
3039 return build_function_call_real (function
, params
, 1, LOOKUP_NORMAL
);
3042 /* Convert the actual parameter expressions in the list VALUES
3043 to the types in the list TYPELIST.
3044 If parmdecls is exhausted, or when an element has NULL as its type,
3045 perform the default conversions.
3047 NAME is an IDENTIFIER_NODE or 0. It is used only for error messages.
3049 This is also where warnings about wrong number of args are generated.
3051 Return a list of expressions for the parameters as converted.
3053 Both VALUES and the returned value are chains of TREE_LIST nodes
3054 with the elements of the list in the TREE_VALUE slots of those nodes.
3056 In C++, unspecified trailing parameters can be filled in with their
3057 default arguments, if such were specified. Do so here. */
3060 convert_arguments (typelist
, values
, fndecl
, flags
)
3061 tree typelist
, values
, fndecl
;
3064 register tree typetail
, valtail
;
3065 register tree result
= NULL_TREE
;
3066 char *called_thing
= 0;
3069 /* Argument passing is always copy-initialization. */
3070 flags
|= LOOKUP_ONLYCONVERTING
;
3074 if (TREE_CODE (TREE_TYPE (fndecl
)) == METHOD_TYPE
)
3076 if (DECL_NAME (fndecl
) == NULL_TREE
3077 || IDENTIFIER_HAS_TYPE_VALUE (DECL_NAME (fndecl
)))
3078 called_thing
= "constructor";
3080 called_thing
= "member function";
3083 called_thing
= "function";
3086 for (valtail
= values
, typetail
= typelist
;
3088 valtail
= TREE_CHAIN (valtail
), i
++)
3090 register tree type
= typetail
? TREE_VALUE (typetail
) : 0;
3091 register tree val
= TREE_VALUE (valtail
);
3093 if (val
== error_mark_node
)
3094 return error_mark_node
;
3096 if (type
== void_type_node
)
3100 cp_error_at ("too many arguments to %s `%+#D'", called_thing
,
3102 error ("at this point in file");
3105 error ("too many arguments to function");
3106 /* In case anybody wants to know if this argument
3109 TREE_TYPE (tree_last (result
)) = error_mark_node
;
3113 if (TREE_CODE (val
) == OFFSET_REF
)
3114 val
= resolve_offset_ref (val
);
3116 /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
3117 Strip such NOP_EXPRs, since VAL is used in non-lvalue context. */
3118 if (TREE_CODE (val
) == NOP_EXPR
3119 && TREE_TYPE (val
) == TREE_TYPE (TREE_OPERAND (val
, 0))
3120 && (type
== 0 || TREE_CODE (type
) != REFERENCE_TYPE
))
3121 val
= TREE_OPERAND (val
, 0);
3123 if (type
== 0 || TREE_CODE (type
) != REFERENCE_TYPE
)
3125 if (TREE_CODE (TREE_TYPE (val
)) == ARRAY_TYPE
3126 || TREE_CODE (TREE_TYPE (val
)) == FUNCTION_TYPE
3127 || TREE_CODE (TREE_TYPE (val
)) == METHOD_TYPE
)
3128 val
= default_conversion (val
);
3131 if (val
== error_mark_node
)
3132 return error_mark_node
;
3136 /* Formal parm type is specified by a function prototype. */
3139 if (TYPE_SIZE (complete_type (type
)) == 0)
3141 error ("parameter type of called function is incomplete");
3146 parmval
= convert_for_initialization
3147 (NULL_TREE
, type
, val
, flags
,
3148 "argument passing", fndecl
, i
);
3149 #ifdef PROMOTE_PROTOTYPES
3150 if ((TREE_CODE (type
) == INTEGER_TYPE
3151 || TREE_CODE (type
) == ENUMERAL_TYPE
)
3152 && (TYPE_PRECISION (type
)
3153 < TYPE_PRECISION (integer_type_node
)))
3154 parmval
= default_conversion (parmval
);
3158 if (parmval
== error_mark_node
)
3159 return error_mark_node
;
3161 result
= expr_tree_cons (NULL_TREE
, parmval
, result
);
3165 if (TREE_CODE (TREE_TYPE (val
)) == REFERENCE_TYPE
)
3166 val
= convert_from_reference (val
);
3168 result
= expr_tree_cons (NULL_TREE
,
3169 convert_arg_to_ellipsis (val
),
3174 typetail
= TREE_CHAIN (typetail
);
3177 if (typetail
!= 0 && typetail
!= void_list_node
)
3179 /* See if there are default arguments that can be used */
3180 if (TREE_PURPOSE (typetail
))
3182 for (; typetail
!= void_list_node
; ++i
)
3185 = convert_default_arg (TREE_VALUE (typetail
),
3186 TREE_PURPOSE (typetail
),
3189 if (parmval
== error_mark_node
)
3190 return error_mark_node
;
3192 result
= expr_tree_cons (0, parmval
, result
);
3193 typetail
= TREE_CHAIN (typetail
);
3194 /* ends with `...'. */
3195 if (typetail
== NULL_TREE
)
3203 cp_error_at ("too few arguments to %s `%+#D'",
3204 called_thing
, fndecl
);
3205 error ("at this point in file");
3208 error ("too few arguments to function");
3209 return error_mark_list
;
3213 return nreverse (result
);
3216 /* Build a binary-operation expression, after performing default
3217 conversions on the operands. CODE is the kind of expression to build. */
3220 build_x_binary_op (code
, arg1
, arg2
)
3221 enum tree_code code
;
3224 if (processing_template_decl
)
3225 return build_min_nt (code
, arg1
, arg2
);
3227 return build_new_op (code
, LOOKUP_NORMAL
, arg1
, arg2
, NULL_TREE
);
3231 build_binary_op (code
, arg1
, arg2
, convert_p
)
3232 enum tree_code code
;
3234 int convert_p ATTRIBUTE_UNUSED
;
3236 return build_binary_op_nodefault (code
, arg1
, arg2
, code
);
3239 /* Build a binary-operation expression without default conversions.
3240 CODE is the kind of expression to build.
3241 This function differs from `build' in several ways:
3242 the data type of the result is computed and recorded in it,
3243 warnings are generated if arg data types are invalid,
3244 special handling for addition and subtraction of pointers is known,
3245 and some optimization is done (operations on narrow ints
3246 are done in the narrower type when that gives the same result).
3247 Constant folding is also done before the result is returned.
3249 ERROR_CODE is the code that determines what to say in error messages.
3250 It is usually, but not always, the same as CODE.
3252 Note that the operands will never have enumeral types
3253 because either they have just had the default conversions performed
3254 or they have both just been converted to some other type in which
3255 the arithmetic is to be done.
3257 C++: must do special pointer arithmetic when implementing
3258 multiple inheritance, and deal with pointer to member functions. */
3261 build_binary_op_nodefault (code
, orig_op0
, orig_op1
, error_code
)
3262 enum tree_code code
;
3263 tree orig_op0
, orig_op1
;
3264 enum tree_code error_code
;
3267 register enum tree_code code0
, code1
;
3270 /* Expression code to give to the expression when it is built.
3271 Normally this is CODE, which is what the caller asked for,
3272 but in some special cases we change it. */
3273 register enum tree_code resultcode
= code
;
3275 /* Data type in which the computation is to be performed.
3276 In the simplest cases this is the common type of the arguments. */
3277 register tree result_type
= NULL
;
3279 /* Nonzero means operands have already been type-converted
3280 in whatever way is necessary.
3281 Zero means they need to be converted to RESULT_TYPE. */
3284 /* Nonzero means create the expression with this type, rather than
3286 tree build_type
= 0;
3288 /* Nonzero means after finally constructing the expression
3289 convert it to this type. */
3290 tree final_type
= 0;
3292 /* Nonzero if this is an operation like MIN or MAX which can
3293 safely be computed in short if both args are promoted shorts.
3294 Also implies COMMON.
3295 -1 indicates a bitwise operation; this makes a difference
3296 in the exact conditions for when it is safe to do the operation
3297 in a narrower mode. */
3300 /* Nonzero if this is a comparison operation;
3301 if both args are promoted shorts, compare the original shorts.
3302 Also implies COMMON. */
3303 int short_compare
= 0;
3305 /* Nonzero if this is a right-shift operation, which can be computed on the
3306 original short and then promoted if the operand is a promoted short. */
3307 int short_shift
= 0;
3309 /* Nonzero means set RESULT_TYPE to the common type of the args. */
3312 /* Apply default conversions. */
3313 if (code
== TRUTH_AND_EXPR
|| code
== TRUTH_ANDIF_EXPR
3314 || code
== TRUTH_OR_EXPR
|| code
== TRUTH_ORIF_EXPR
3315 || code
== TRUTH_XOR_EXPR
)
3317 op0
= decay_conversion (orig_op0
);
3318 op1
= decay_conversion (orig_op1
);
3322 op0
= default_conversion (orig_op0
);
3323 op1
= default_conversion (orig_op1
);
3326 type0
= TREE_TYPE (op0
);
3327 type1
= TREE_TYPE (op1
);
3329 /* The expression codes of the data types of the arguments tell us
3330 whether the arguments are integers, floating, pointers, etc. */
3331 code0
= TREE_CODE (type0
);
3332 code1
= TREE_CODE (type1
);
3334 /* Strip NON_LVALUE_EXPRs, etc., since we aren't using as an lvalue. */
3335 STRIP_TYPE_NOPS (op0
);
3336 STRIP_TYPE_NOPS (op1
);
3338 /* If an error was already reported for one of the arguments,
3339 avoid reporting another error. */
3341 if (code0
== ERROR_MARK
|| code1
== ERROR_MARK
)
3342 return error_mark_node
;
3347 /* Handle the pointer + int case. */
3348 if (code0
== POINTER_TYPE
&& code1
== INTEGER_TYPE
)
3349 return pointer_int_sum (PLUS_EXPR
, op0
, op1
);
3350 else if (code1
== POINTER_TYPE
&& code0
== INTEGER_TYPE
)
3351 return pointer_int_sum (PLUS_EXPR
, op1
, op0
);
3357 /* Subtraction of two similar pointers.
3358 We must subtract them as integers, then divide by object size. */
3359 if (code0
== POINTER_TYPE
&& code1
== POINTER_TYPE
3360 && comp_target_types (type0
, type1
, 1))
3361 return pointer_diff (op0
, op1
, common_type (type0
, type1
));
3362 /* Handle pointer minus int. Just like pointer plus int. */
3363 else if (code0
== POINTER_TYPE
&& code1
== INTEGER_TYPE
)
3364 return pointer_int_sum (MINUS_EXPR
, op0
, op1
);
3373 case TRUNC_DIV_EXPR
:
3375 case FLOOR_DIV_EXPR
:
3376 case ROUND_DIV_EXPR
:
3377 case EXACT_DIV_EXPR
:
3378 if ((code0
== INTEGER_TYPE
|| code0
== REAL_TYPE
3379 || code0
== COMPLEX_TYPE
)
3380 && (code1
== INTEGER_TYPE
|| code1
== REAL_TYPE
3381 || code1
== COMPLEX_TYPE
))
3383 if (TREE_CODE (op1
) == INTEGER_CST
&& integer_zerop (op1
))
3384 cp_warning ("division by zero in `%E / 0'", op0
);
3385 else if (TREE_CODE (op1
) == REAL_CST
&& real_zerop (op1
))
3386 cp_warning ("division by zero in `%E / 0.'", op0
);
3388 if (!(code0
== INTEGER_TYPE
&& code1
== INTEGER_TYPE
))
3389 resultcode
= RDIV_EXPR
;
3391 /* When dividing two signed integers, we have to promote to int.
3392 unless we divide by a constant != -1. Note that default
3393 conversion will have been performed on the operands at this
3394 point, so we have to dig out the original type to find out if
3396 shorten
= ((TREE_CODE (op0
) == NOP_EXPR
3397 && TREE_UNSIGNED (TREE_TYPE (TREE_OPERAND (op0
, 0))))
3398 || (TREE_CODE (op1
) == INTEGER_CST
3399 && (TREE_INT_CST_LOW (op1
) != -1
3400 || TREE_INT_CST_HIGH (op1
) != -1)));
3406 case BIT_ANDTC_EXPR
:
3409 if (code0
== INTEGER_TYPE
&& code1
== INTEGER_TYPE
)
3411 /* If one operand is a constant, and the other is a short type
3412 that has been converted to an int,
3413 really do the work in the short type and then convert the
3414 result to int. If we are lucky, the constant will be 0 or 1
3415 in the short type, making the entire operation go away. */
3416 if (TREE_CODE (op0
) == INTEGER_CST
3417 && TREE_CODE (op1
) == NOP_EXPR
3418 && (TYPE_PRECISION (type1
)
3419 > TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (op1
, 0))))
3420 && TREE_UNSIGNED (TREE_TYPE (TREE_OPERAND (op1
, 0))))
3422 final_type
= result_type
;
3423 op1
= TREE_OPERAND (op1
, 0);
3424 result_type
= TREE_TYPE (op1
);
3426 if (TREE_CODE (op1
) == INTEGER_CST
3427 && TREE_CODE (op0
) == NOP_EXPR
3428 && (TYPE_PRECISION (type0
)
3429 > TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (op0
, 0))))
3430 && TREE_UNSIGNED (TREE_TYPE (TREE_OPERAND (op0
, 0))))
3432 final_type
= result_type
;
3433 op0
= TREE_OPERAND (op0
, 0);
3434 result_type
= TREE_TYPE (op0
);
3438 case TRUNC_MOD_EXPR
:
3439 case FLOOR_MOD_EXPR
:
3440 if (code1
== INTEGER_TYPE
&& integer_zerop (op1
))
3441 cp_warning ("division by zero in `%E %% 0'", op0
);
3442 else if (code1
== REAL_TYPE
&& real_zerop (op1
))
3443 cp_warning ("division by zero in `%E %% 0.'", op0
);
3445 if (code0
== INTEGER_TYPE
&& code1
== INTEGER_TYPE
)
3447 /* Although it would be tempting to shorten always here, that loses
3448 on some targets, since the modulo instruction is undefined if the
3449 quotient can't be represented in the computation mode. We shorten
3450 only if unsigned or if dividing by something we know != -1. */
3451 shorten
= ((TREE_CODE (op0
) == NOP_EXPR
3452 && TREE_UNSIGNED (TREE_TYPE (TREE_OPERAND (op0
, 0))))
3453 || (TREE_CODE (op1
) == INTEGER_CST
3454 && (TREE_INT_CST_LOW (op1
) != -1
3455 || TREE_INT_CST_HIGH (op1
) != -1)));
3460 case TRUTH_ANDIF_EXPR
:
3461 case TRUTH_ORIF_EXPR
:
3462 case TRUTH_AND_EXPR
:
3464 result_type
= boolean_type_node
;
3467 /* Shift operations: result has same type as first operand;
3468 always convert second operand to int.
3469 Also set SHORT_SHIFT if shifting rightward. */
3472 if (code0
== INTEGER_TYPE
&& code1
== INTEGER_TYPE
)
3474 result_type
= type0
;
3475 if (TREE_CODE (op1
) == INTEGER_CST
)
3477 if (tree_int_cst_lt (op1
, integer_zero_node
))
3478 warning ("right shift count is negative");
3481 if (TREE_INT_CST_LOW (op1
) | TREE_INT_CST_HIGH (op1
))
3483 if (TREE_INT_CST_HIGH (op1
) != 0
3484 || ((unsigned HOST_WIDE_INT
) TREE_INT_CST_LOW (op1
)
3485 >= TYPE_PRECISION (type0
)))
3486 warning ("right shift count >= width of type");
3489 /* Convert the shift-count to an integer, regardless of
3490 size of value being shifted. */
3491 if (TYPE_MAIN_VARIANT (TREE_TYPE (op1
)) != integer_type_node
)
3492 op1
= cp_convert (integer_type_node
, op1
);
3493 /* Avoid converting op1 to result_type later. */
3499 if (code0
== INTEGER_TYPE
&& code1
== INTEGER_TYPE
)
3501 result_type
= type0
;
3502 if (TREE_CODE (op1
) == INTEGER_CST
)
3504 if (tree_int_cst_lt (op1
, integer_zero_node
))
3505 warning ("left shift count is negative");
3506 else if (TREE_INT_CST_HIGH (op1
) != 0
3507 || ((unsigned HOST_WIDE_INT
) TREE_INT_CST_LOW (op1
)
3508 >= TYPE_PRECISION (type0
)))
3509 warning ("left shift count >= width of type");
3511 /* Convert the shift-count to an integer, regardless of
3512 size of value being shifted. */
3513 if (TYPE_MAIN_VARIANT (TREE_TYPE (op1
)) != integer_type_node
)
3514 op1
= cp_convert (integer_type_node
, op1
);
3515 /* Avoid converting op1 to result_type later. */
3522 if (code0
== INTEGER_TYPE
&& code1
== INTEGER_TYPE
)
3524 result_type
= type0
;
3525 if (TREE_CODE (op1
) == INTEGER_CST
)
3527 if (tree_int_cst_lt (op1
, integer_zero_node
))
3528 warning ("%s rotate count is negative",
3529 (code
== LROTATE_EXPR
) ? "left" : "right");
3530 else if (TREE_INT_CST_HIGH (op1
) != 0
3531 || ((unsigned HOST_WIDE_INT
) TREE_INT_CST_LOW (op1
)
3532 >= TYPE_PRECISION (type0
)))
3533 warning ("%s rotate count >= width of type",
3534 (code
== LROTATE_EXPR
) ? "left" : "right");
3536 /* Convert the shift-count to an integer, regardless of
3537 size of value being shifted. */
3538 if (TYPE_MAIN_VARIANT (TREE_TYPE (op1
)) != integer_type_node
)
3539 op1
= cp_convert (integer_type_node
, op1
);
3545 build_type
= boolean_type_node
;
3546 if ((code0
== INTEGER_TYPE
|| code0
== REAL_TYPE
3547 || code0
== COMPLEX_TYPE
)
3548 && (code1
== INTEGER_TYPE
|| code1
== REAL_TYPE
3549 || code1
== COMPLEX_TYPE
))
3551 else if (code0
== POINTER_TYPE
&& code1
== POINTER_TYPE
)
3553 register tree tt0
= TYPE_MAIN_VARIANT (TREE_TYPE (type0
));
3554 register tree tt1
= TYPE_MAIN_VARIANT (TREE_TYPE (type1
));
3556 if (comp_target_types (type0
, type1
, 1))
3557 result_type
= common_type (type0
, type1
);
3558 else if (tt0
== void_type_node
)
3560 if (pedantic
&& TREE_CODE (tt1
) == FUNCTION_TYPE
3561 && tree_int_cst_lt (TYPE_SIZE (type0
), TYPE_SIZE (type1
)))
3562 pedwarn ("ANSI C++ forbids comparison of `void *' with function pointer");
3563 else if (TREE_CODE (tt1
) == OFFSET_TYPE
)
3564 pedwarn ("ANSI C++ forbids conversion of a pointer to member to `void *'");
3566 else if (tt1
== void_type_node
)
3568 if (pedantic
&& TREE_CODE (tt0
) == FUNCTION_TYPE
3569 && tree_int_cst_lt (TYPE_SIZE (type1
), TYPE_SIZE (type0
)))
3570 pedwarn ("ANSI C++ forbids comparison of `void *' with function pointer");
3573 cp_pedwarn ("comparison of distinct pointer types `%T' and `%T' lacks a cast",
3576 if (result_type
== NULL_TREE
)
3577 result_type
= ptr_type_node
;
3579 else if (code0
== POINTER_TYPE
&& TREE_CODE (op1
) == INTEGER_CST
3580 && integer_zerop (op1
))
3581 result_type
= type0
;
3582 else if (code1
== POINTER_TYPE
&& TREE_CODE (op0
) == INTEGER_CST
3583 && integer_zerop (op0
))
3584 result_type
= type1
;
3585 else if (code0
== POINTER_TYPE
&& code1
== INTEGER_TYPE
)
3587 result_type
= type0
;
3588 error ("ANSI C++ forbids comparison between pointer and integer");
3590 else if (code0
== INTEGER_TYPE
&& code1
== POINTER_TYPE
)
3592 result_type
= type1
;
3593 error ("ANSI C++ forbids comparison between pointer and integer");
3595 else if (TYPE_PTRMEMFUNC_P (type0
) && TREE_CODE (op1
) == INTEGER_CST
3596 && integer_zerop (op1
))
3598 op0
= build_component_ref (op0
, index_identifier
, NULL_TREE
, 0);
3599 op1
= integer_zero_node
;
3600 result_type
= TREE_TYPE (op0
);
3602 else if (TYPE_PTRMEMFUNC_P (type1
) && TREE_CODE (op0
) == INTEGER_CST
3603 && integer_zerop (op0
))
3605 op0
= build_component_ref (op1
, index_identifier
, NULL_TREE
, 0);
3606 op1
= integer_zero_node
;
3607 result_type
= TREE_TYPE (op0
);
3609 else if (TYPE_PTRMEMFUNC_P (type0
) && TYPE_PTRMEMFUNC_P (type1
)
3610 && (TYPE_PTRMEMFUNC_FN_TYPE (type0
)
3611 == TYPE_PTRMEMFUNC_FN_TYPE (type1
)))
3613 /* The code we generate for the test is:
3615 (op0.index == op1.index
3616 && ((op1.index != -1 && op0.delta2 == op1.delta2)
3617 || op0.pfn == op1.pfn)) */
3619 tree index0
= build_component_ref (op0
, index_identifier
,
3621 tree index1
= save_expr (build_component_ref (op1
, index_identifier
,
3623 tree pfn0
= PFN_FROM_PTRMEMFUNC (op0
);
3624 tree pfn1
= PFN_FROM_PTRMEMFUNC (op1
);
3625 tree delta20
= DELTA2_FROM_PTRMEMFUNC (op0
);
3626 tree delta21
= DELTA2_FROM_PTRMEMFUNC (op1
);
3628 tree integer_neg_one_node
3629 = build_binary_op (MINUS_EXPR
, integer_zero_node
,
3630 integer_one_node
, 1);
3631 e1
= build_binary_op (EQ_EXPR
, index0
, index1
, 1);
3632 e2
= build_binary_op (NE_EXPR
, index1
, integer_neg_one_node
, 1);
3633 e2
= build_binary_op (TRUTH_ANDIF_EXPR
, e2
,
3634 build_binary_op (EQ_EXPR
, delta20
, delta21
, 1),
3636 e3
= build_binary_op (EQ_EXPR
, pfn0
, pfn1
, 1);
3637 e2
= build_binary_op (TRUTH_ORIF_EXPR
, e2
, e3
, 1);
3638 e2
= build_binary_op (TRUTH_ANDIF_EXPR
, e1
, e2
, 1);
3639 if (code
== EQ_EXPR
)
3641 return build_binary_op (EQ_EXPR
, e2
, integer_zero_node
, 1);
3643 else if (TYPE_PTRMEMFUNC_P (type0
)
3644 && TYPE_PTRMEMFUNC_FN_TYPE (type0
) == type1
)
3646 tree index0
= build_component_ref (op0
, index_identifier
,
3649 tree pfn0
= PFN_FROM_PTRMEMFUNC (op0
);
3650 tree delta20
= DELTA2_FROM_PTRMEMFUNC (op0
);
3651 tree delta21
= integer_zero_node
;
3653 tree integer_neg_one_node
3654 = build_binary_op (MINUS_EXPR
, integer_zero_node
, integer_one_node
, 1);
3655 if (TREE_CODE (TREE_OPERAND (op1
, 0)) == FUNCTION_DECL
3656 && DECL_VINDEX (TREE_OPERAND (op1
, 0)))
3658 /* Map everything down one to make room for
3659 the null pointer to member. */
3660 index1
= size_binop (PLUS_EXPR
,
3661 DECL_VINDEX (TREE_OPERAND (op1
, 0)),
3663 op1
= integer_zero_node
;
3664 delta21
= CLASSTYPE_VFIELD (TYPE_METHOD_BASETYPE
3665 (TREE_TYPE (type1
)));
3666 delta21
= DECL_FIELD_BITPOS (delta21
);
3667 delta21
= size_binop (FLOOR_DIV_EXPR
, delta21
,
3668 size_int (BITS_PER_UNIT
));
3669 delta21
= convert (sizetype
, delta21
);
3672 index1
= integer_neg_one_node
;
3674 tree nop1
= build1 (NOP_EXPR
, TYPE_PTRMEMFUNC_FN_TYPE (type0
),
3676 TREE_CONSTANT (nop1
) = TREE_CONSTANT (op1
);
3679 e1
= build_binary_op (EQ_EXPR
, index0
, index1
, 1);
3680 e2
= build_binary_op (NE_EXPR
, index1
, integer_neg_one_node
, 1);
3681 e2
= build_binary_op (TRUTH_ANDIF_EXPR
, e2
,
3682 build_binary_op (EQ_EXPR
, delta20
, delta21
, 1),
3684 e3
= build_binary_op (EQ_EXPR
, pfn0
, op1
, 1);
3685 e2
= build_binary_op (TRUTH_ORIF_EXPR
, e2
, e3
, 1);
3686 e2
= build_binary_op (TRUTH_ANDIF_EXPR
, e1
, e2
, 1);
3687 if (code
== EQ_EXPR
)
3689 return build_binary_op (EQ_EXPR
, e2
, integer_zero_node
, 1);
3691 else if (TYPE_PTRMEMFUNC_P (type1
)
3692 && TYPE_PTRMEMFUNC_FN_TYPE (type1
) == type0
)
3694 return build_binary_op (code
, op1
, op0
, 1);
3700 if ((code0
== INTEGER_TYPE
|| code0
== REAL_TYPE
)
3701 && (code1
== INTEGER_TYPE
|| code1
== REAL_TYPE
))
3703 else if (code0
== POINTER_TYPE
&& code1
== POINTER_TYPE
)
3705 if (comp_target_types (type0
, type1
, 1))
3706 result_type
= common_type (type0
, type1
);
3709 cp_pedwarn ("comparison of distinct pointer types `%T' and `%T' lacks a cast",
3711 result_type
= ptr_type_node
;
3720 build_type
= boolean_type_node
;
3721 if ((code0
== INTEGER_TYPE
|| code0
== REAL_TYPE
)
3722 && (code1
== INTEGER_TYPE
|| code1
== REAL_TYPE
))
3724 else if (code0
== POINTER_TYPE
&& code1
== POINTER_TYPE
)
3726 if (comp_target_types (type0
, type1
, 1))
3727 result_type
= common_type (type0
, type1
);
3730 cp_pedwarn ("comparison of distinct pointer types `%T' and `%T' lacks a cast",
3732 result_type
= ptr_type_node
;
3735 else if (code0
== POINTER_TYPE
&& TREE_CODE (op1
) == INTEGER_CST
3736 && integer_zerop (op1
))
3737 result_type
= type0
;
3738 else if (code1
== POINTER_TYPE
&& TREE_CODE (op0
) == INTEGER_CST
3739 && integer_zerop (op0
))
3740 result_type
= type1
;
3741 else if (code0
== POINTER_TYPE
&& code1
== INTEGER_TYPE
)
3743 result_type
= type0
;
3744 pedwarn ("ANSI C++ forbids comparison between pointer and integer");
3746 else if (code0
== INTEGER_TYPE
&& code1
== POINTER_TYPE
)
3748 result_type
= type1
;
3749 pedwarn ("ANSI C++ forbids comparison between pointer and integer");
3757 if ((code0
== INTEGER_TYPE
|| code0
== REAL_TYPE
|| code0
== COMPLEX_TYPE
)
3759 (code1
== INTEGER_TYPE
|| code1
== REAL_TYPE
|| code1
== COMPLEX_TYPE
))
3761 int none_complex
= (code0
!= COMPLEX_TYPE
&& code1
!= COMPLEX_TYPE
);
3763 if (shorten
|| common
|| short_compare
)
3764 result_type
= common_type (type0
, type1
);
3766 /* For certain operations (which identify themselves by shorten != 0)
3767 if both args were extended from the same smaller type,
3768 do the arithmetic in that type and then extend.
3770 shorten !=0 and !=1 indicates a bitwise operation.
3771 For them, this optimization is safe only if
3772 both args are zero-extended or both are sign-extended.
3773 Otherwise, we might change the result.
3774 Eg, (short)-1 | (unsigned short)-1 is (int)-1
3775 but calculated in (unsigned short) it would be (unsigned short)-1. */
3777 if (shorten
&& none_complex
)
3779 int unsigned0
, unsigned1
;
3780 tree arg0
= get_narrower (op0
, &unsigned0
);
3781 tree arg1
= get_narrower (op1
, &unsigned1
);
3782 /* UNS is 1 if the operation to be done is an unsigned one. */
3783 int uns
= TREE_UNSIGNED (result_type
);
3786 final_type
= result_type
;
3788 /* Handle the case that OP0 does not *contain* a conversion
3789 but it *requires* conversion to FINAL_TYPE. */
3791 if (op0
== arg0
&& TREE_TYPE (op0
) != final_type
)
3792 unsigned0
= TREE_UNSIGNED (TREE_TYPE (op0
));
3793 if (op1
== arg1
&& TREE_TYPE (op1
) != final_type
)
3794 unsigned1
= TREE_UNSIGNED (TREE_TYPE (op1
));
3796 /* Now UNSIGNED0 is 1 if ARG0 zero-extends to FINAL_TYPE. */
3798 /* For bitwise operations, signedness of nominal type
3799 does not matter. Consider only how operands were extended. */
3803 /* Note that in all three cases below we refrain from optimizing
3804 an unsigned operation on sign-extended args.
3805 That would not be valid. */
3807 /* Both args variable: if both extended in same way
3808 from same width, do it in that width.
3809 Do it unsigned if args were zero-extended. */
3810 if ((TYPE_PRECISION (TREE_TYPE (arg0
))
3811 < TYPE_PRECISION (result_type
))
3812 && (TYPE_PRECISION (TREE_TYPE (arg1
))
3813 == TYPE_PRECISION (TREE_TYPE (arg0
)))
3814 && unsigned0
== unsigned1
3815 && (unsigned0
|| !uns
))
3817 = signed_or_unsigned_type (unsigned0
,
3818 common_type (TREE_TYPE (arg0
),
3820 else if (TREE_CODE (arg0
) == INTEGER_CST
3821 && (unsigned1
|| !uns
)
3822 && (TYPE_PRECISION (TREE_TYPE (arg1
))
3823 < TYPE_PRECISION (result_type
))
3824 && (type
= signed_or_unsigned_type (unsigned1
,
3826 int_fits_type_p (arg0
, type
)))
3828 else if (TREE_CODE (arg1
) == INTEGER_CST
3829 && (unsigned0
|| !uns
)
3830 && (TYPE_PRECISION (TREE_TYPE (arg0
))
3831 < TYPE_PRECISION (result_type
))
3832 && (type
= signed_or_unsigned_type (unsigned0
,
3834 int_fits_type_p (arg1
, type
)))
3838 /* Shifts can be shortened if shifting right. */
3843 tree arg0
= get_narrower (op0
, &unsigned_arg
);
3845 final_type
= result_type
;
3847 if (arg0
== op0
&& final_type
== TREE_TYPE (op0
))
3848 unsigned_arg
= TREE_UNSIGNED (TREE_TYPE (op0
));
3850 if (TYPE_PRECISION (TREE_TYPE (arg0
)) < TYPE_PRECISION (result_type
)
3851 /* We can shorten only if the shift count is less than the
3852 number of bits in the smaller type size. */
3853 && TREE_INT_CST_HIGH (op1
) == 0
3854 && TYPE_PRECISION (TREE_TYPE (arg0
)) > TREE_INT_CST_LOW (op1
)
3855 /* If arg is sign-extended and then unsigned-shifted,
3856 we can simulate this with a signed shift in arg's type
3857 only if the extended result is at least twice as wide
3858 as the arg. Otherwise, the shift could use up all the
3859 ones made by sign-extension and bring in zeros.
3860 We can't optimize that case at all, but in most machines
3861 it never happens because available widths are 2**N. */
3862 && (!TREE_UNSIGNED (final_type
)
3864 || (((unsigned) 2 * TYPE_PRECISION (TREE_TYPE (arg0
)))
3865 <= TYPE_PRECISION (result_type
))))
3867 /* Do an unsigned shift if the operand was zero-extended. */
3869 = signed_or_unsigned_type (unsigned_arg
,
3871 /* Convert value-to-be-shifted to that type. */
3872 if (TREE_TYPE (op0
) != result_type
)
3873 op0
= cp_convert (result_type
, op0
);
3878 /* Comparison operations are shortened too but differently.
3879 They identify themselves by setting short_compare = 1. */
3883 /* Don't write &op0, etc., because that would prevent op0
3884 from being kept in a register.
3885 Instead, make copies of the our local variables and
3886 pass the copies by reference, then copy them back afterward. */
3887 tree xop0
= op0
, xop1
= op1
, xresult_type
= result_type
;
3888 enum tree_code xresultcode
= resultcode
;
3890 = shorten_compare (&xop0
, &xop1
, &xresult_type
, &xresultcode
);
3892 return cp_convert (boolean_type_node
, val
);
3893 op0
= xop0
, op1
= xop1
;
3895 resultcode
= xresultcode
;
3898 if (short_compare
&& warn_sign_compare
)
3900 int op0_signed
= ! TREE_UNSIGNED (TREE_TYPE (orig_op0
));
3901 int op1_signed
= ! TREE_UNSIGNED (TREE_TYPE (orig_op1
));
3903 int unsignedp0
, unsignedp1
;
3904 tree primop0
= get_narrower (op0
, &unsignedp0
);
3905 tree primop1
= get_narrower (op1
, &unsignedp1
);
3907 /* Check for comparison of different enum types. */
3908 if (TREE_CODE (TREE_TYPE (orig_op0
)) == ENUMERAL_TYPE
3909 && TREE_CODE (TREE_TYPE (orig_op1
)) == ENUMERAL_TYPE
3910 && TYPE_MAIN_VARIANT (TREE_TYPE (orig_op0
))
3911 != TYPE_MAIN_VARIANT (TREE_TYPE (orig_op1
)))
3913 cp_warning ("comparison between `%#T' and `%#T'",
3914 TREE_TYPE (orig_op0
), TREE_TYPE (orig_op1
));
3917 /* Give warnings for comparisons between signed and unsigned
3918 quantities that may fail. */
3919 /* Do the checking based on the original operand trees, so that
3920 casts will be considered, but default promotions won't be. */
3922 /* Do not warn if the comparison is being done in a signed type,
3923 since the signed type will only be chosen if it can represent
3924 all the values of the unsigned type. */
3925 if (! TREE_UNSIGNED (result_type
))
3927 /* Do not warn if both operands are unsigned. */
3928 else if (op0_signed
== op1_signed
)
3930 /* Do not warn if the signed quantity is an unsuffixed
3931 integer literal (or some static constant expression
3932 involving such literals) and it is non-negative. */
3933 else if ((op0_signed
&& TREE_CODE (orig_op0
) == INTEGER_CST
3934 && tree_int_cst_sgn (orig_op0
) >= 0)
3935 || (op1_signed
&& TREE_CODE (orig_op1
) == INTEGER_CST
3936 && tree_int_cst_sgn (orig_op1
) >= 0))
3938 /* Do not warn if the comparison is an equality operation,
3939 the unsigned quantity is an integral constant and it does
3940 not use the most significant bit of result_type. */
3941 else if ((resultcode
== EQ_EXPR
|| resultcode
== NE_EXPR
)
3942 && ((op0_signed
&& TREE_CODE (orig_op1
) == INTEGER_CST
3943 && int_fits_type_p (orig_op1
,
3944 signed_type (result_type
)))
3945 || (op1_signed
&& TREE_CODE (orig_op0
) == INTEGER_CST
3946 && int_fits_type_p (orig_op0
,
3947 signed_type (result_type
)))))
3950 warning ("comparison between signed and unsigned");
3952 /* Warn if two unsigned values are being compared in a size
3953 larger than their original size, and one (and only one) is the
3954 result of a `~' operator. This comparison will always fail.
3956 Also warn if one operand is a constant, and the constant does not
3957 have all bits set that are set in the ~ operand when it is
3960 if ((TREE_CODE (primop0
) == BIT_NOT_EXPR
)
3961 ^ (TREE_CODE (primop1
) == BIT_NOT_EXPR
))
3963 if (TREE_CODE (primop0
) == BIT_NOT_EXPR
)
3964 primop0
= get_narrower (TREE_OPERAND (op0
, 0), &unsignedp0
);
3965 if (TREE_CODE (primop1
) == BIT_NOT_EXPR
)
3966 primop1
= get_narrower (TREE_OPERAND (op1
, 0), &unsignedp1
);
3968 if (TREE_CODE (primop0
) == INTEGER_CST
3969 || TREE_CODE (primop1
) == INTEGER_CST
)
3972 HOST_WIDE_INT constant
, mask
;
3976 if (TREE_CODE (primop0
) == INTEGER_CST
)
3979 unsignedp
= unsignedp1
;
3980 constant
= TREE_INT_CST_LOW (primop0
);
3985 unsignedp
= unsignedp0
;
3986 constant
= TREE_INT_CST_LOW (primop1
);
3989 bits
= TYPE_PRECISION (TREE_TYPE (primop
));
3990 if (bits
< TYPE_PRECISION (result_type
)
3991 && bits
< HOST_BITS_PER_LONG
&& unsignedp
)
3993 mask
= (~ (HOST_WIDE_INT
) 0) << bits
;
3994 if ((mask
& constant
) != mask
)
3995 warning ("comparison of promoted ~unsigned with constant");
3998 else if (unsignedp0
&& unsignedp1
3999 && (TYPE_PRECISION (TREE_TYPE (primop0
))
4000 < TYPE_PRECISION (result_type
))
4001 && (TYPE_PRECISION (TREE_TYPE (primop1
))
4002 < TYPE_PRECISION (result_type
)))
4003 warning ("comparison of promoted ~unsigned with unsigned");
4008 /* At this point, RESULT_TYPE must be nonzero to avoid an error message.
4009 If CONVERTED is zero, both args will be converted to type RESULT_TYPE.
4010 Then the expression will be built.
4011 It will be given type FINAL_TYPE if that is nonzero;
4012 otherwise, it will be given type RESULT_TYPE. */
4016 cp_error ("invalid operands `%T' and `%T' to binary `%O'",
4017 TREE_TYPE (orig_op0
), TREE_TYPE (orig_op1
), error_code
);
4018 return error_mark_node
;
4021 /* Issue warnings about peculiar, but legal, uses of NULL. */
4022 if (/* It's reasonable to use pointer values as operands of &&
4023 and ||, so NULL is no exception. */
4024 !(code
== TRUTH_ANDIF_EXPR
|| code
== TRUTH_ORIF_EXPR
)
4025 && (/* If OP0 is NULL and OP1 is not a pointer, or vice versa. */
4026 (orig_op0
== null_node
4027 && TREE_CODE (TREE_TYPE (op1
)) != POINTER_TYPE
)
4028 /* Or vice versa. */
4029 || (orig_op1
== null_node
4030 && TREE_CODE (TREE_TYPE (op0
)) != POINTER_TYPE
)
4031 /* Or, both are NULL and the operation was not a comparison. */
4032 || (orig_op0
== null_node
&& orig_op1
== null_node
4033 && code
!= EQ_EXPR
&& code
!= NE_EXPR
)))
4034 /* Some sort of arithmetic operation involving NULL was
4035 performed. Note that pointer-difference and pointer-addition
4036 have already been handled above, and so we don't end up here in
4038 cp_warning ("NULL used in arithmetic");
4042 if (TREE_TYPE (op0
) != result_type
)
4043 op0
= cp_convert (result_type
, op0
);
4044 if (TREE_TYPE (op1
) != result_type
)
4045 op1
= cp_convert (result_type
, op1
);
4047 if (op0
== error_mark_node
|| op1
== error_mark_node
)
4048 return error_mark_node
;
4051 if (build_type
== NULL_TREE
)
4052 build_type
= result_type
;
4055 register tree result
= build (resultcode
, build_type
, op0
, op1
);
4056 register tree folded
;
4058 folded
= fold (result
);
4059 if (folded
== result
)
4060 TREE_CONSTANT (folded
) = TREE_CONSTANT (op0
) & TREE_CONSTANT (op1
);
4061 if (final_type
!= 0)
4062 return cp_convert (final_type
, folded
);
4067 /* Return a tree for the sum or difference (RESULTCODE says which)
4068 of pointer PTROP and integer INTOP. */
4071 pointer_int_sum (resultcode
, ptrop
, intop
)
4072 enum tree_code resultcode
;
4073 register tree ptrop
, intop
;
4077 register tree result
;
4078 register tree folded
= fold (intop
);
4080 /* The result is a pointer of the same type that is being added. */
4082 register tree result_type
= TREE_TYPE (ptrop
);
4084 if (!complete_type_or_else (result_type
, ptrop
))
4085 return error_mark_node
;
4087 if (TREE_CODE (TREE_TYPE (result_type
)) == VOID_TYPE
)
4089 if (pedantic
|| warn_pointer_arith
)
4090 pedwarn ("ANSI C++ forbids using pointer of type `void *' in arithmetic");
4091 size_exp
= integer_one_node
;
4093 else if (TREE_CODE (TREE_TYPE (result_type
)) == FUNCTION_TYPE
)
4095 if (pedantic
|| warn_pointer_arith
)
4096 pedwarn ("ANSI C++ forbids using pointer to a function in arithmetic");
4097 size_exp
= integer_one_node
;
4099 else if (TREE_CODE (TREE_TYPE (result_type
)) == METHOD_TYPE
)
4101 if (pedantic
|| warn_pointer_arith
)
4102 pedwarn ("ANSI C++ forbids using pointer to a method in arithmetic");
4103 size_exp
= integer_one_node
;
4105 else if (TREE_CODE (TREE_TYPE (result_type
)) == OFFSET_TYPE
)
4107 if (pedantic
|| warn_pointer_arith
)
4108 pedwarn ("ANSI C++ forbids using pointer to a member in arithmetic");
4109 size_exp
= integer_one_node
;
4112 size_exp
= size_in_bytes (complete_type (TREE_TYPE (result_type
)));
4114 /* Needed to make OOPS V2R3 work. */
4116 if (TREE_CODE (intop
) == INTEGER_CST
4117 && TREE_INT_CST_LOW (intop
) == 0
4118 && TREE_INT_CST_HIGH (intop
) == 0)
4121 /* If what we are about to multiply by the size of the elements
4122 contains a constant term, apply distributive law
4123 and multiply that constant term separately.
4124 This helps produce common subexpressions. */
4126 if ((TREE_CODE (intop
) == PLUS_EXPR
|| TREE_CODE (intop
) == MINUS_EXPR
)
4127 && ! TREE_CONSTANT (intop
)
4128 && TREE_CONSTANT (TREE_OPERAND (intop
, 1))
4129 && TREE_CONSTANT (size_exp
))
4131 enum tree_code subcode
= resultcode
;
4132 if (TREE_CODE (intop
) == MINUS_EXPR
)
4133 subcode
= (subcode
== PLUS_EXPR
? MINUS_EXPR
: PLUS_EXPR
);
4134 ptrop
= build_binary_op (subcode
, ptrop
, TREE_OPERAND (intop
, 1), 1);
4135 intop
= TREE_OPERAND (intop
, 0);
4138 /* Convert the integer argument to a type the same size as sizetype
4139 so the multiply won't overflow spuriously. */
4141 if (TYPE_PRECISION (TREE_TYPE (intop
)) != TYPE_PRECISION (sizetype
))
4142 intop
= cp_convert (type_for_size (TYPE_PRECISION (sizetype
), 0), intop
);
4144 /* Replace the integer argument with a suitable product by the object size.
4145 Do this multiplication as signed, then convert to the appropriate
4146 pointer type (actually unsigned integral). */
4148 intop
= cp_convert (result_type
,
4149 build_binary_op (MULT_EXPR
, intop
,
4150 cp_convert (TREE_TYPE (intop
),
4154 /* Create the sum or difference. */
4156 result
= build (resultcode
, result_type
, ptrop
, intop
);
4158 folded
= fold (result
);
4159 if (folded
== result
)
4160 TREE_CONSTANT (folded
) = TREE_CONSTANT (ptrop
) & TREE_CONSTANT (intop
);
4164 /* Return a tree for the difference of pointers OP0 and OP1.
4165 The resulting tree has type int. */
4168 pointer_diff (op0
, op1
, ptrtype
)
4169 register tree op0
, op1
;
4170 register tree ptrtype
;
4172 register tree result
, folded
;
4173 tree restype
= ptrdiff_type_node
;
4174 tree target_type
= TREE_TYPE (ptrtype
);
4176 if (!complete_type_or_else (target_type
, NULL_TREE
))
4177 return error_mark_node
;
4179 if (pedantic
|| warn_pointer_arith
)
4181 if (TREE_CODE (target_type
) == VOID_TYPE
)
4182 pedwarn ("ANSI C++ forbids using pointer of type `void *' in subtraction");
4183 if (TREE_CODE (target_type
) == FUNCTION_TYPE
)
4184 pedwarn ("ANSI C++ forbids using pointer to a function in subtraction");
4185 if (TREE_CODE (target_type
) == METHOD_TYPE
)
4186 pedwarn ("ANSI C++ forbids using pointer to a method in subtraction");
4187 if (TREE_CODE (target_type
) == OFFSET_TYPE
)
4188 pedwarn ("ANSI C++ forbids using pointer to a member in subtraction");
4191 /* First do the subtraction as integers;
4192 then drop through to build the divide operator. */
4194 op0
= build_binary_op (MINUS_EXPR
, cp_convert (restype
, op0
),
4195 cp_convert (restype
, op1
), 1);
4197 /* This generates an error if op1 is a pointer to an incomplete type. */
4198 if (TYPE_SIZE (TREE_TYPE (TREE_TYPE (op1
))) == 0)
4199 error ("arithmetic on pointer to an incomplete type");
4201 op1
= ((TREE_CODE (target_type
) == VOID_TYPE
4202 || TREE_CODE (target_type
) == FUNCTION_TYPE
4203 || TREE_CODE (target_type
) == METHOD_TYPE
4204 || TREE_CODE (target_type
) == OFFSET_TYPE
)
4206 : size_in_bytes (target_type
));
4208 /* Do the division. */
4210 result
= build (EXACT_DIV_EXPR
, restype
, op0
, cp_convert (restype
, op1
));
4212 folded
= fold (result
);
4213 if (folded
== result
)
4214 TREE_CONSTANT (folded
) = TREE_CONSTANT (op0
) & TREE_CONSTANT (op1
);
4218 /* Handle the case of taking the address of a COMPONENT_REF.
4219 Called by `build_unary_op' and `build_up_reference'.
4221 ARG is the COMPONENT_REF whose address we want.
4222 ARGTYPE is the pointer type that this address should have.
4223 MSG is an error message to print if this COMPONENT_REF is not
4224 addressable (such as a bitfield). */
4227 build_component_addr (arg
, argtype
, msg
)
4231 tree field
= TREE_OPERAND (arg
, 1);
4232 tree basetype
= decl_type_context (field
);
4233 tree rval
= build_unary_op (ADDR_EXPR
, TREE_OPERAND (arg
, 0), 0);
4235 my_friendly_assert (TREE_CODE (field
) == FIELD_DECL
, 981018);
4237 if (DECL_C_BIT_FIELD (field
))
4239 error (msg
, IDENTIFIER_POINTER (DECL_NAME (field
)));
4240 return error_mark_node
;
4243 if (TREE_CODE (field
) == FIELD_DECL
4244 && TYPE_USES_COMPLEX_INHERITANCE (basetype
))
4246 /* Can't convert directly to ARGTYPE, since that
4247 may have the same pointer type as one of our
4249 rval
= build1 (NOP_EXPR
, argtype
,
4250 convert_pointer_to (basetype
, rval
));
4251 TREE_CONSTANT (rval
) = TREE_CONSTANT (TREE_OPERAND (rval
, 0));
4254 /* This conversion is harmless. */
4255 rval
= convert_force (argtype
, rval
, 0);
4257 if (! integer_zerop (DECL_FIELD_BITPOS (field
)))
4259 tree offset
= size_binop (EASY_DIV_EXPR
, DECL_FIELD_BITPOS (field
),
4260 size_int (BITS_PER_UNIT
));
4261 int flag
= TREE_CONSTANT (rval
);
4262 offset
= convert (sizetype
, offset
);
4263 rval
= fold (build (PLUS_EXPR
, argtype
,
4264 rval
, cp_convert (argtype
, offset
)));
4265 TREE_CONSTANT (rval
) = flag
;
4270 /* Construct and perhaps optimize a tree representation
4271 for a unary operation. CODE, a tree_code, specifies the operation
4272 and XARG is the operand. */
4275 build_x_unary_op (code
, xarg
)
4276 enum tree_code code
;
4279 if (processing_template_decl
)
4280 return build_min_nt (code
, xarg
, NULL_TREE
);
4282 /* & rec, on incomplete RECORD_TYPEs is the simple opr &, not an
4284 if (code
== ADDR_EXPR
4285 && TREE_CODE (xarg
) != TEMPLATE_ID_EXPR
4286 && ((IS_AGGR_TYPE_CODE (TREE_CODE (TREE_TYPE (xarg
)))
4287 && TYPE_SIZE (TREE_TYPE (xarg
)) == NULL_TREE
)
4288 || (TREE_CODE (xarg
) == OFFSET_REF
)))
4289 /* don't look for a function */;
4294 rval
= build_new_op (code
, LOOKUP_NORMAL
, xarg
,
4295 NULL_TREE
, NULL_TREE
);
4296 if (rval
|| code
!= ADDR_EXPR
)
4300 if (code
== ADDR_EXPR
)
4302 if (TREE_CODE (xarg
) == TARGET_EXPR
)
4303 warning ("taking address of temporary");
4306 return build_unary_op (code
, xarg
, 0);
4309 /* Just like truthvalue_conversion, but we want a CLEANUP_POINT_EXPR. */
4312 condition_conversion (expr
)
4316 if (processing_template_decl
)
4318 t
= cp_convert (boolean_type_node
, expr
);
4319 t
= fold (build1 (CLEANUP_POINT_EXPR
, boolean_type_node
, t
));
4323 /* C++: Must handle pointers to members.
4325 Perhaps type instantiation should be extended to handle conversion
4326 from aggregates to types we don't yet know we want? (Or are those
4327 cases typically errors which should be reported?)
4329 NOCONVERT nonzero suppresses the default promotions
4330 (such as from short to int). */
4333 build_unary_op (code
, xarg
, noconvert
)
4334 enum tree_code code
;
4338 /* No default_conversion here. It causes trouble for ADDR_EXPR. */
4339 register tree arg
= xarg
;
4340 register tree argtype
= 0;
4341 char *errstring
= NULL
;
4344 if (arg
== error_mark_node
)
4345 return error_mark_node
;
4350 /* This is used for unary plus, because a CONVERT_EXPR
4351 is enough to prevent anybody from looking inside for
4352 associativity, but won't generate any code. */
4353 if (!(arg
= build_expr_type_conversion
4354 (WANT_ARITH
| WANT_ENUM
| WANT_POINTER
, arg
, 1)))
4355 errstring
= "wrong type argument to unary plus";
4359 arg
= default_conversion (arg
);
4360 arg
= build1 (NON_LVALUE_EXPR
, TREE_TYPE (arg
), arg
);
4361 TREE_CONSTANT (arg
) = TREE_CONSTANT (TREE_OPERAND (arg
, 0));
4366 if (!(arg
= build_expr_type_conversion (WANT_ARITH
| WANT_ENUM
, arg
, 1)))
4367 errstring
= "wrong type argument to unary minus";
4368 else if (!noconvert
)
4369 arg
= default_conversion (arg
);
4373 if (TREE_CODE (TREE_TYPE (arg
)) == COMPLEX_TYPE
)
4377 arg
= default_conversion (arg
);
4379 else if (!(arg
= build_expr_type_conversion (WANT_INT
| WANT_ENUM
,
4381 errstring
= "wrong type argument to bit-complement";
4382 else if (!noconvert
)
4383 arg
= default_conversion (arg
);
4387 if (!(arg
= build_expr_type_conversion (WANT_ARITH
| WANT_ENUM
, arg
, 1)))
4388 errstring
= "wrong type argument to abs";
4389 else if (!noconvert
)
4390 arg
= default_conversion (arg
);
4394 /* Conjugating a real value is a no-op, but allow it anyway. */
4395 if (!(arg
= build_expr_type_conversion (WANT_ARITH
| WANT_ENUM
, arg
, 1)))
4396 errstring
= "wrong type argument to conjugation";
4397 else if (!noconvert
)
4398 arg
= default_conversion (arg
);
4401 case TRUTH_NOT_EXPR
:
4402 arg
= cp_convert (boolean_type_node
, arg
);
4403 val
= invert_truthvalue (arg
);
4404 if (arg
!= error_mark_node
)
4406 errstring
= "in argument to unary !";
4413 if (TREE_CODE (arg
) == COMPLEX_CST
)
4414 return TREE_REALPART (arg
);
4415 else if (TREE_CODE (TREE_TYPE (arg
)) == COMPLEX_TYPE
)
4416 return fold (build1 (REALPART_EXPR
, TREE_TYPE (TREE_TYPE (arg
)), arg
));
4421 if (TREE_CODE (arg
) == COMPLEX_CST
)
4422 return TREE_IMAGPART (arg
);
4423 else if (TREE_CODE (TREE_TYPE (arg
)) == COMPLEX_TYPE
)
4424 return fold (build1 (IMAGPART_EXPR
, TREE_TYPE (TREE_TYPE (arg
)), arg
));
4426 return cp_convert (TREE_TYPE (arg
), integer_zero_node
);
4428 case PREINCREMENT_EXPR
:
4429 case POSTINCREMENT_EXPR
:
4430 case PREDECREMENT_EXPR
:
4431 case POSTDECREMENT_EXPR
:
4432 /* Handle complex lvalues (when permitted)
4433 by reduction to simpler cases. */
4435 val
= unary_complex_lvalue (code
, arg
);
4439 /* Increment or decrement the real part of the value,
4440 and don't change the imaginary part. */
4441 if (TREE_CODE (TREE_TYPE (arg
)) == COMPLEX_TYPE
)
4445 arg
= stabilize_reference (arg
);
4446 real
= build_unary_op (REALPART_EXPR
, arg
, 1);
4447 imag
= build_unary_op (IMAGPART_EXPR
, arg
, 1);
4448 return build (COMPLEX_EXPR
, TREE_TYPE (arg
),
4449 build_unary_op (code
, real
, 1), imag
);
4452 /* Report invalid types. */
4454 if (!(arg
= build_expr_type_conversion (WANT_ARITH
| WANT_POINTER
,
4457 if (code
== PREINCREMENT_EXPR
)
4458 errstring
="no pre-increment operator for type";
4459 else if (code
== POSTINCREMENT_EXPR
)
4460 errstring
="no post-increment operator for type";
4461 else if (code
== PREDECREMENT_EXPR
)
4462 errstring
="no pre-decrement operator for type";
4464 errstring
="no post-decrement operator for type";
4468 /* Report something read-only. */
4470 if (CP_TYPE_CONST_P (TREE_TYPE (arg
))
4471 || TREE_READONLY (arg
))
4472 readonly_error (arg
, ((code
== PREINCREMENT_EXPR
4473 || code
== POSTINCREMENT_EXPR
)
4474 ? "increment" : "decrement"),
4479 tree result_type
= TREE_TYPE (arg
);
4481 arg
= get_unwidened (arg
, 0);
4482 argtype
= TREE_TYPE (arg
);
4484 /* ARM $5.2.5 last annotation says this should be forbidden. */
4485 if (TREE_CODE (argtype
) == ENUMERAL_TYPE
)
4486 pedwarn ("ANSI C++ forbids %sing an enum",
4487 (code
== PREINCREMENT_EXPR
|| code
== POSTINCREMENT_EXPR
)
4488 ? "increment" : "decrement");
4490 /* Compute the increment. */
4492 if (TREE_CODE (argtype
) == POINTER_TYPE
)
4494 enum tree_code tmp
= TREE_CODE (TREE_TYPE (argtype
));
4495 if (TYPE_SIZE (complete_type (TREE_TYPE (argtype
))) == 0)
4496 cp_error ("cannot %s a pointer to incomplete type `%T'",
4497 ((code
== PREINCREMENT_EXPR
4498 || code
== POSTINCREMENT_EXPR
)
4499 ? "increment" : "decrement"), TREE_TYPE (argtype
));
4500 else if ((pedantic
|| warn_pointer_arith
)
4501 && (tmp
== FUNCTION_TYPE
|| tmp
== METHOD_TYPE
4502 || tmp
== VOID_TYPE
|| tmp
== OFFSET_TYPE
))
4503 cp_pedwarn ("ANSI C++ forbids %sing a pointer of type `%T'",
4504 ((code
== PREINCREMENT_EXPR
4505 || code
== POSTINCREMENT_EXPR
)
4506 ? "increment" : "decrement"), argtype
);
4507 inc
= c_sizeof_nowarn (TREE_TYPE (argtype
));
4510 inc
= integer_one_node
;
4512 inc
= cp_convert (argtype
, inc
);
4514 /* Handle incrementing a cast-expression. */
4516 switch (TREE_CODE (arg
))
4521 case FIX_TRUNC_EXPR
:
4522 case FIX_FLOOR_EXPR
:
4523 case FIX_ROUND_EXPR
:
4526 tree incremented
, modify
, value
, compound
;
4527 if (! lvalue_p (arg
) && pedantic
)
4528 pedwarn ("cast to non-reference type used as lvalue");
4529 arg
= stabilize_reference (arg
);
4530 if (code
== PREINCREMENT_EXPR
|| code
== PREDECREMENT_EXPR
)
4533 value
= save_expr (arg
);
4534 incremented
= build (((code
== PREINCREMENT_EXPR
4535 || code
== POSTINCREMENT_EXPR
)
4536 ? PLUS_EXPR
: MINUS_EXPR
),
4537 argtype
, value
, inc
);
4538 TREE_SIDE_EFFECTS (incremented
) = 1;
4540 modify
= build_modify_expr (arg
, NOP_EXPR
, incremented
);
4541 compound
= build (COMPOUND_EXPR
, TREE_TYPE (arg
), modify
, value
);
4543 /* Eliminate warning about unused result of + or -. */
4544 TREE_NO_UNUSED_WARNING (compound
) = 1;
4552 /* Complain about anything else that is not a true lvalue. */
4553 if (!lvalue_or_else (arg
, ((code
== PREINCREMENT_EXPR
4554 || code
== POSTINCREMENT_EXPR
)
4555 ? "increment" : "decrement")))
4556 return error_mark_node
;
4558 /* Forbid using -- on `bool'. */
4559 if (TREE_TYPE (arg
) == boolean_type_node
)
4561 if (code
== POSTDECREMENT_EXPR
|| code
== PREDECREMENT_EXPR
)
4563 cp_error ("invalid use of `--' on bool variable `%D'", arg
);
4564 return error_mark_node
;
4567 /* This will only work if someone can convince Kenner to accept
4568 my patch to expand_increment. (jason) */
4569 val
= build (code
, TREE_TYPE (arg
), arg
, inc
);
4571 if (code
== POSTINCREMENT_EXPR
)
4573 arg
= stabilize_reference (arg
);
4574 val
= build (MODIFY_EXPR
, TREE_TYPE (arg
), arg
,
4576 TREE_SIDE_EFFECTS (val
) = 1;
4577 arg
= save_expr (arg
);
4578 val
= build (COMPOUND_EXPR
, TREE_TYPE (arg
), val
, arg
);
4579 val
= build (COMPOUND_EXPR
, TREE_TYPE (arg
), arg
, val
);
4582 val
= build (MODIFY_EXPR
, TREE_TYPE (arg
), arg
,
4587 val
= build (code
, TREE_TYPE (arg
), arg
, inc
);
4589 TREE_SIDE_EFFECTS (val
) = 1;
4590 return cp_convert (result_type
, val
);
4594 /* Note that this operation never does default_conversion
4595 regardless of NOCONVERT. */
4597 argtype
= lvalue_type (arg
);
4598 if (TREE_CODE (argtype
) == REFERENCE_TYPE
)
4602 build_pointer_type (TREE_TYPE (argtype
)), arg
);
4603 TREE_CONSTANT (arg
) = TREE_CONSTANT (TREE_OPERAND (arg
, 0));
4606 else if (pedantic
&& DECL_MAIN_P (arg
))
4608 pedwarn ("taking address of function `main'");
4610 /* Let &* cancel out to simplify resulting code. */
4611 if (TREE_CODE (arg
) == INDIRECT_REF
)
4613 /* We don't need to have `current_class_ptr' wrapped in a
4614 NON_LVALUE_EXPR node. */
4615 if (arg
== current_class_ref
)
4616 return current_class_ptr
;
4618 arg
= TREE_OPERAND (arg
, 0);
4619 if (TREE_CODE (TREE_TYPE (arg
)) == REFERENCE_TYPE
)
4623 build_pointer_type (TREE_TYPE (TREE_TYPE (arg
))), arg
);
4624 TREE_CONSTANT (arg
) = TREE_CONSTANT (TREE_OPERAND (arg
, 0));
4626 else if (lvalue_p (arg
))
4627 /* Don't let this be an lvalue. */
4628 return non_lvalue (arg
);
4632 /* For &x[y], return x+y */
4633 if (TREE_CODE (arg
) == ARRAY_REF
)
4635 if (mark_addressable (TREE_OPERAND (arg
, 0)) == 0)
4636 return error_mark_node
;
4637 return build_binary_op (PLUS_EXPR
, TREE_OPERAND (arg
, 0),
4638 TREE_OPERAND (arg
, 1), 1);
4641 /* Uninstantiated types are all functions. Taking the
4642 address of a function is a no-op, so just return the
4645 if (TREE_CODE (arg
) == IDENTIFIER_NODE
4646 && IDENTIFIER_OPNAME_P (arg
))
4648 my_friendly_abort (117);
4649 /* We don't know the type yet, so just work around the problem.
4650 We know that this will resolve to an lvalue. */
4651 return build1 (ADDR_EXPR
, unknown_type_node
, arg
);
4654 if (type_unknown_p (arg
))
4655 return build1 (ADDR_EXPR
, unknown_type_node
, arg
);
4657 /* Handle complex lvalues (when permitted)
4658 by reduction to simpler cases. */
4659 val
= unary_complex_lvalue (code
, arg
);
4663 switch (TREE_CODE (arg
))
4668 case FIX_TRUNC_EXPR
:
4669 case FIX_FLOOR_EXPR
:
4670 case FIX_ROUND_EXPR
:
4672 if (! lvalue_p (arg
) && pedantic
)
4673 pedwarn ("taking the address of a cast to non-reference type");
4680 /* Allow the address of a constructor if all the elements
4682 if (TREE_CODE (arg
) == CONSTRUCTOR
&& TREE_HAS_CONSTRUCTOR (arg
)
4683 && TREE_CONSTANT (arg
))
4685 /* Anything not already handled and not a true memory reference
4687 else if (TREE_CODE (argtype
) != FUNCTION_TYPE
4688 && TREE_CODE (argtype
) != METHOD_TYPE
4689 && !lvalue_or_else (arg
, "unary `&'"))
4690 return error_mark_node
;
4692 if (argtype
!= error_mark_node
)
4693 argtype
= build_pointer_type (argtype
);
4695 if (mark_addressable (arg
) == 0)
4696 return error_mark_node
;
4701 if (TREE_CODE (arg
) == COMPONENT_REF
)
4702 addr
= build_component_addr
4704 "attempt to take address of bit-field structure member `%s'");
4706 addr
= build1 (code
, argtype
, arg
);
4708 /* Address of a static or external variable or
4709 function counts as a constant */
4711 TREE_CONSTANT (addr
) = 1;
4713 if (TREE_CODE (argtype
) == POINTER_TYPE
4714 && TREE_CODE (TREE_TYPE (argtype
)) == METHOD_TYPE
)
4716 build_ptrmemfunc_type (argtype
);
4717 addr
= build_ptrmemfunc (argtype
, addr
, 0);
4730 argtype
= TREE_TYPE (arg
);
4731 return fold (build1 (code
, argtype
, arg
));
4735 return error_mark_node
;
4739 /* If CONVERSIONS is a conversion expression or a nested sequence of such,
4740 convert ARG with the same conversions in the same order
4741 and return the result. */
4744 convert_sequence (conversions
, arg
)
4748 switch (TREE_CODE (conversions
))
4753 case FIX_TRUNC_EXPR
:
4754 case FIX_FLOOR_EXPR
:
4755 case FIX_ROUND_EXPR
:
4757 return cp_convert (TREE_TYPE (conversions
),
4758 convert_sequence (TREE_OPERAND (conversions
, 0),
4767 /* Apply unary lvalue-demanding operator CODE to the expression ARG
4768 for certain kinds of expressions which are not really lvalues
4769 but which we can accept as lvalues.
4771 If ARG is not a kind of expression we can handle, return zero. */
4774 unary_complex_lvalue (code
, arg
)
4775 enum tree_code code
;
4778 /* Handle (a, b) used as an "lvalue". */
4779 if (TREE_CODE (arg
) == COMPOUND_EXPR
)
4781 tree real_result
= build_unary_op (code
, TREE_OPERAND (arg
, 1), 0);
4782 return build (COMPOUND_EXPR
, TREE_TYPE (real_result
),
4783 TREE_OPERAND (arg
, 0), real_result
);
4786 /* Handle (a ? b : c) used as an "lvalue". */
4787 if (TREE_CODE (arg
) == COND_EXPR
4788 || TREE_CODE (arg
) == MIN_EXPR
|| TREE_CODE (arg
) == MAX_EXPR
)
4789 return rationalize_conditional_expr (code
, arg
);
4791 if (TREE_CODE (arg
) == MODIFY_EXPR
4792 || TREE_CODE (arg
) == PREINCREMENT_EXPR
4793 || TREE_CODE (arg
) == PREDECREMENT_EXPR
)
4794 return unary_complex_lvalue
4795 (code
, build (COMPOUND_EXPR
, TREE_TYPE (TREE_OPERAND (arg
, 0)),
4796 arg
, TREE_OPERAND (arg
, 0)));
4798 if (code
!= ADDR_EXPR
)
4801 /* Handle (a = b) used as an "lvalue" for `&'. */
4802 if (TREE_CODE (arg
) == MODIFY_EXPR
4803 || TREE_CODE (arg
) == INIT_EXPR
)
4805 tree real_result
= build_unary_op (code
, TREE_OPERAND (arg
, 0), 0);
4806 arg
= build (COMPOUND_EXPR
, TREE_TYPE (real_result
), arg
, real_result
);
4807 TREE_NO_UNUSED_WARNING (arg
) = 1;
4811 if (TREE_CODE (TREE_TYPE (arg
)) == FUNCTION_TYPE
4812 || TREE_CODE (TREE_TYPE (arg
)) == METHOD_TYPE
4813 || TREE_CODE (TREE_TYPE (arg
)) == OFFSET_TYPE
)
4815 /* The representation of something of type OFFSET_TYPE
4816 is really the representation of a pointer to it.
4817 Here give the representation its true type. */
4820 my_friendly_assert (TREE_CODE (arg
) != SCOPE_REF
, 313);
4822 if (TREE_CODE (arg
) != OFFSET_REF
)
4825 t
= TREE_OPERAND (arg
, 1);
4827 /* Check all this code for right semantics. */
4828 if (TREE_CODE (t
) == FUNCTION_DECL
)
4830 if (DECL_DESTRUCTOR_P (t
))
4831 cp_error ("taking address of destructor");
4832 return build_unary_op (ADDR_EXPR
, t
, 0);
4834 if (TREE_CODE (t
) == VAR_DECL
)
4835 return build_unary_op (ADDR_EXPR
, t
, 0);
4840 if (TREE_OPERAND (arg
, 0)
4841 && ! is_dummy_object (TREE_OPERAND (arg
, 0))
4842 && TREE_CODE (t
) != FIELD_DECL
)
4844 cp_error ("taking address of bound pointer-to-member expression");
4845 return error_mark_node
;
4848 type
= build_offset_type (DECL_FIELD_CONTEXT (t
), TREE_TYPE (t
));
4849 type
= build_pointer_type (type
);
4851 t
= make_node (PTRMEM_CST
);
4852 TREE_TYPE (t
) = type
;
4853 PTRMEM_CST_MEMBER (t
) = TREE_OPERAND (arg
, 1);
4859 /* We permit compiler to make function calls returning
4860 objects of aggregate type look like lvalues. */
4864 if (TREE_CODE (targ
) == SAVE_EXPR
)
4865 targ
= TREE_OPERAND (targ
, 0);
4867 if (TREE_CODE (targ
) == CALL_EXPR
&& IS_AGGR_TYPE (TREE_TYPE (targ
)))
4869 if (TREE_CODE (arg
) == SAVE_EXPR
)
4872 targ
= build_cplus_new (TREE_TYPE (arg
), arg
);
4873 return build1 (ADDR_EXPR
, build_pointer_type (TREE_TYPE (arg
)), targ
);
4876 if (TREE_CODE (arg
) == SAVE_EXPR
&& TREE_CODE (targ
) == INDIRECT_REF
)
4877 return build (SAVE_EXPR
, build_pointer_type (TREE_TYPE (arg
)),
4878 TREE_OPERAND (targ
, 0), current_function_decl
, NULL
);
4881 /* Don't let anything else be handled specially. */
4885 /* Mark EXP saying that we need to be able to take the
4886 address of it; it should not be allocated in a register.
4887 Value is 1 if successful.
4889 C++: we do not allow `current_class_ptr' to be addressable. */
4892 mark_addressable (exp
)
4895 register tree x
= exp
;
4897 if (TREE_ADDRESSABLE (x
) == 1)
4901 switch (TREE_CODE (x
))
4908 x
= TREE_OPERAND (x
, 0);
4912 if (x
== current_class_ptr
)
4914 if (! flag_this_is_variable
)
4915 error ("address of `this' not available");
4916 TREE_ADDRESSABLE (x
) = 1; /* so compiler doesn't die later */
4917 put_var_into_stack (x
);
4921 if (TREE_STATIC (x
) && TREE_READONLY (x
)
4922 && DECL_RTL (x
) != 0
4923 && ! DECL_IN_MEMORY_P (x
))
4925 /* We thought this would make a good constant variable,
4926 but we were wrong. */
4927 push_obstacks_nochange ();
4928 end_temporary_allocation ();
4930 TREE_ASM_WRITTEN (x
) = 0;
4932 rest_of_decl_compilation (x
, 0,
4933 !DECL_FUNCTION_SCOPE_P (x
),
4935 TREE_ADDRESSABLE (x
) = 1;
4941 /* Caller should not be trying to mark initialized
4942 constant fields addressable. */
4943 my_friendly_assert (DECL_LANG_SPECIFIC (x
) == 0
4944 || DECL_IN_AGGR_P (x
) == 0
4946 || DECL_EXTERNAL (x
), 314);
4950 if (DECL_REGISTER (x
) && !TREE_ADDRESSABLE (x
)
4951 && !DECL_ARTIFICIAL (x
) && extra_warnings
)
4952 cp_warning ("address requested for `%D', which is declared `register'",
4954 put_var_into_stack (x
);
4955 TREE_ADDRESSABLE (x
) = 1;
4959 if (DECL_LANG_SPECIFIC (x
) != 0)
4961 x
= DECL_MAIN_VARIANT (x
);
4962 /* We have to test both conditions here. The first may be
4963 non-zero in the case of processing a default function. The
4964 second may be non-zero in the case of a template function. */
4965 if (DECL_TEMPLATE_INFO (x
) && !DECL_TEMPLATE_SPECIALIZATION (x
))
4968 TREE_ADDRESSABLE (x
) = 1;
4970 TREE_ADDRESSABLE (DECL_ASSEMBLER_NAME (x
)) = 1;
4974 TREE_ADDRESSABLE (x
) = 1;
4978 TREE_ADDRESSABLE (x
) = 1;
4979 mark_addressable (TREE_OPERAND (x
, 0));
4987 /* Build and return a conditional expression IFEXP ? OP1 : OP2. */
4990 build_x_conditional_expr (ifexp
, op1
, op2
)
4991 tree ifexp
, op1
, op2
;
4993 if (processing_template_decl
)
4994 return build_min_nt (COND_EXPR
, ifexp
, op1
, op2
);
4996 return build_new_op (COND_EXPR
, LOOKUP_NORMAL
, ifexp
, op1
, op2
);
5000 build_conditional_expr (ifexp
, op1
, op2
)
5001 tree ifexp
, op1
, op2
;
5003 register tree type1
;
5004 register tree type2
;
5005 register enum tree_code code1
;
5006 register enum tree_code code2
;
5007 register tree result_type
= NULL_TREE
;
5009 /* If second operand is omitted, it is the same as the first one;
5010 make sure it is calculated only once. */
5014 pedwarn ("ANSI C++ forbids omitting the middle term of a ?: expression");
5015 ifexp
= op1
= save_expr (ifexp
);
5018 ifexp
= cp_convert (boolean_type_node
, ifexp
);
5020 if (TREE_CODE (ifexp
) == ERROR_MARK
)
5021 return error_mark_node
;
5023 /* C++: REFERENCE_TYPES must be dereferenced. */
5024 type1
= TREE_TYPE (op1
);
5025 code1
= TREE_CODE (type1
);
5026 type2
= TREE_TYPE (op2
);
5027 code2
= TREE_CODE (type2
);
5029 if (code1
== REFERENCE_TYPE
)
5031 op1
= convert_from_reference (op1
);
5032 type1
= TREE_TYPE (op1
);
5033 code1
= TREE_CODE (type1
);
5035 if (code2
== REFERENCE_TYPE
)
5037 op2
= convert_from_reference (op2
);
5038 type2
= TREE_TYPE (op2
);
5039 code2
= TREE_CODE (type2
);
5042 /* Don't promote the operands separately if they promote
5043 the same way. Return the unpromoted type and let the combined
5044 value get promoted if necessary. */
5046 if (TYPE_MAIN_VARIANT (type1
) == TYPE_MAIN_VARIANT (type2
)
5047 && code2
!= ARRAY_TYPE
5048 && code2
!= FUNCTION_TYPE
5049 && code2
!= METHOD_TYPE
)
5053 if (TREE_CONSTANT (ifexp
)
5054 && (TREE_CODE (ifexp
) == INTEGER_CST
5055 || TREE_CODE (ifexp
) == ADDR_EXPR
))
5056 return (integer_zerop (ifexp
) ? op2
: op1
);
5058 if (TREE_CODE (op1
) == CONST_DECL
)
5059 op1
= DECL_INITIAL (op1
);
5060 else if (TREE_READONLY_DECL_P (op1
))
5061 op1
= decl_constant_value (op1
);
5062 if (TREE_CODE (op2
) == CONST_DECL
)
5063 op2
= DECL_INITIAL (op2
);
5064 else if (TREE_READONLY_DECL_P (op2
))
5065 op2
= decl_constant_value (op2
);
5067 type1
= cp_build_qualified_type
5068 (type1
, (CP_TYPE_QUALS (TREE_TYPE (op1
))
5069 | CP_TYPE_QUALS (TREE_TYPE (op2
))));
5070 /* ??? This is a kludge to deal with the fact that
5071 we don't sort out integers and enums properly, yet. */
5072 result
= fold (build (COND_EXPR
, type1
, ifexp
, op1
, op2
));
5073 if (TREE_TYPE (result
) != type1
)
5074 result
= build1 (NOP_EXPR
, type1
, result
);
5075 /* Expand both sides into the same slot,
5076 hopefully the target of the ?: expression. */
5077 if (TREE_CODE (op1
) == TARGET_EXPR
&& TREE_CODE (op2
) == TARGET_EXPR
)
5079 tree slot
= build (VAR_DECL
, TREE_TYPE (result
));
5080 layout_decl (slot
, 0);
5081 result
= build (TARGET_EXPR
, TREE_TYPE (result
),
5082 slot
, result
, NULL_TREE
, NULL_TREE
);
5087 /* They don't match; promote them both and then try to reconcile them.
5088 But don't permit mismatching enum types. */
5089 if (code1
== ENUMERAL_TYPE
)
5091 if (code2
== ENUMERAL_TYPE
)
5093 cp_error ("enumeral mismatch in conditional expression: `%T' vs `%T'",
5095 return error_mark_node
;
5097 else if (extra_warnings
&& ! IS_AGGR_TYPE_CODE (code2
)
5098 && type2
!= type_promotes_to (type1
))
5099 warning ("enumeral and non-enumeral type in conditional expression");
5101 else if (extra_warnings
5102 && code2
== ENUMERAL_TYPE
&& ! IS_AGGR_TYPE_CODE (code1
)
5103 && type1
!= type_promotes_to (type2
))
5104 warning ("enumeral and non-enumeral type in conditional expression");
5106 if (code1
!= VOID_TYPE
)
5108 op1
= default_conversion (op1
);
5109 type1
= TREE_TYPE (op1
);
5110 if (TYPE_PTRMEMFUNC_P (type1
))
5111 type1
= TYPE_PTRMEMFUNC_FN_TYPE (type1
);
5112 code1
= TREE_CODE (type1
);
5114 if (code2
!= VOID_TYPE
)
5116 op2
= default_conversion (op2
);
5117 type2
= TREE_TYPE (op2
);
5118 if (TYPE_PTRMEMFUNC_P (type2
))
5119 type2
= TYPE_PTRMEMFUNC_FN_TYPE (type2
);
5120 code2
= TREE_CODE (type2
);
5123 if (code1
== RECORD_TYPE
&& code2
== RECORD_TYPE
5124 && real_lvalue_p (op1
) && real_lvalue_p (op2
)
5125 && comptypes (type1
, type2
, COMPARE_BASE
| COMPARE_RELAXED
))
5127 type1
= build_reference_type (type1
);
5128 type2
= build_reference_type (type2
);
5129 result_type
= common_type (type1
, type2
);
5130 op1
= convert_to_reference (result_type
, op1
, CONV_IMPLICIT
,
5131 LOOKUP_NORMAL
, NULL_TREE
);
5132 op2
= convert_to_reference (result_type
, op2
, CONV_IMPLICIT
,
5133 LOOKUP_NORMAL
, NULL_TREE
);
5135 /* Quickly detect the usual case where op1 and op2 have the same type
5137 else if (TYPE_MAIN_VARIANT (type1
) == TYPE_MAIN_VARIANT (type2
))
5140 result_type
= type1
;
5143 cp_build_qualified_type (type1
,
5144 CP_TYPE_QUALS (TREE_TYPE (op1
))
5145 | CP_TYPE_QUALS (TREE_TYPE (op2
)));
5147 else if ((code1
== INTEGER_TYPE
|| code1
== REAL_TYPE
)
5148 && (code2
== INTEGER_TYPE
|| code2
== REAL_TYPE
))
5150 result_type
= common_type (type1
, type2
);
5152 else if (code1
== VOID_TYPE
|| code2
== VOID_TYPE
)
5154 if (pedantic
&& (code1
!= VOID_TYPE
|| code2
!= VOID_TYPE
))
5155 pedwarn ("ANSI C++ forbids conditional expr with only one void side");
5156 result_type
= void_type_node
;
5158 else if (code1
== POINTER_TYPE
&& null_ptr_cst_p (op2
))
5159 result_type
= qualify_type (type1
, type2
);
5160 else if (code2
== POINTER_TYPE
&& null_ptr_cst_p (op1
))
5161 result_type
= qualify_type (type2
, type1
);
5162 else if (code1
== POINTER_TYPE
&& code2
== POINTER_TYPE
)
5164 if (comp_target_types (type1
, type2
, 1))
5165 result_type
= common_type (type1
, type2
);
5166 else if (TYPE_MAIN_VARIANT (TREE_TYPE (type1
)) == void_type_node
)
5168 if (pedantic
&& TREE_CODE (type2
) == FUNCTION_TYPE
)
5169 pedwarn ("ANSI C++ forbids conditional expr between `void *' and function pointer");
5170 result_type
= qualify_type (type1
, type2
);
5172 else if (TYPE_MAIN_VARIANT (TREE_TYPE (type2
)) == void_type_node
)
5174 if (pedantic
&& TREE_CODE (type1
) == FUNCTION_TYPE
)
5175 pedwarn ("ANSI C++ forbids conditional expr between `void *' and function pointer");
5176 result_type
= qualify_type (type2
, type1
);
5179 else if (same_or_base_type_p (type2
, type1
))
5180 result_type
= type2
;
5181 else if (IS_AGGR_TYPE (TREE_TYPE (type1
))
5182 && IS_AGGR_TYPE (TREE_TYPE (type2
))
5183 && (result_type
= common_base_type (TREE_TYPE (type1
),
5184 TREE_TYPE (type2
))))
5186 if (result_type
== error_mark_node
)
5188 cp_error ("common base type of types `%T' and `%T' is ambiguous",
5189 TREE_TYPE (type1
), TREE_TYPE (type2
));
5190 result_type
= ptr_type_node
;
5195 && result_type
!= TREE_TYPE (type1
)
5196 && result_type
!= TREE_TYPE (type2
))
5197 cp_pedwarn ("`%T' and `%T' converted to `%T *' in conditional expression",
5198 type1
, type2
, result_type
);
5200 result_type
= build_pointer_type (result_type
);
5205 pedwarn ("pointer type mismatch in conditional expression");
5206 result_type
= ptr_type_node
;
5209 else if (code1
== POINTER_TYPE
&& code2
== INTEGER_TYPE
)
5211 pedwarn ("pointer/integer type mismatch in conditional expression");
5212 result_type
= type1
;
5214 else if (code2
== POINTER_TYPE
&& code1
== INTEGER_TYPE
)
5216 pedwarn ("pointer/integer type mismatch in conditional expression");
5217 result_type
= type2
;
5219 if (type2
== unknown_type_node
)
5220 result_type
= type1
;
5221 else if (type1
== unknown_type_node
)
5222 result_type
= type2
;
5226 /* The match does not look good. If either is
5227 an aggregate value, try converting to a scalar type. */
5228 if (code1
== RECORD_TYPE
&& code2
== RECORD_TYPE
)
5230 cp_error ("aggregate mismatch in conditional expression: `%T' vs `%T'",
5232 return error_mark_node
;
5234 /* Warning: this code assumes that conversion between cv-variants of
5235 a type is done using NOP_EXPRs. */
5236 if (code1
== RECORD_TYPE
&& TYPE_HAS_CONVERSION (type1
))
5238 /* There are other types besides pointers and records. */
5240 if (code2
== POINTER_TYPE
)
5241 tmp
= build_pointer_type
5242 (cp_build_qualified_type (TREE_TYPE (type2
),
5244 | TYPE_QUAL_VOLATILE
5245 | TYPE_QUAL_RESTRICT
));
5248 tmp
= build_type_conversion (CONVERT_EXPR
, tmp
, op1
, 0);
5249 if (tmp
== NULL_TREE
)
5251 cp_error ("incompatible types `%T' and `%T' in `?:'",
5253 return error_mark_node
;
5255 if (tmp
== error_mark_node
)
5256 error ("ambiguous pointer conversion");
5259 result_type
= common_type (type2
, TREE_TYPE (tmp
));
5262 else if (code2
== RECORD_TYPE
&& TYPE_HAS_CONVERSION (type2
))
5265 if (code1
== POINTER_TYPE
)
5266 tmp
= build_pointer_type
5267 (cp_build_qualified_type (TREE_TYPE (type1
),
5269 | TYPE_QUAL_VOLATILE
5270 | TYPE_QUAL_RESTRICT
));
5274 tmp
= build_type_conversion (CONVERT_EXPR
, tmp
, op2
, 0);
5275 if (tmp
== NULL_TREE
)
5277 cp_error ("incompatible types `%T' and `%T' in `?:'",
5279 return error_mark_node
;
5281 if (tmp
== error_mark_node
)
5282 error ("ambiguous pointer conversion");
5285 result_type
= common_type (type1
, TREE_TYPE (tmp
));
5288 else if (flag_cond_mismatch
)
5289 result_type
= void_type_node
;
5292 error ("type mismatch in conditional expression");
5293 return error_mark_node
;
5297 if (TREE_CODE (result_type
) == POINTER_TYPE
5298 && TREE_CODE (TREE_TYPE (result_type
)) == METHOD_TYPE
)
5299 result_type
= build_ptrmemfunc_type (result_type
);
5301 if (result_type
!= TREE_TYPE (op1
))
5302 op1
= convert_for_initialization
5303 (NULL_TREE
, result_type
, op1
, LOOKUP_NORMAL
, "converting", NULL_TREE
, 0);
5304 if (result_type
!= TREE_TYPE (op2
))
5305 op2
= convert_for_initialization
5306 (NULL_TREE
, result_type
, op2
, LOOKUP_NORMAL
, "converting", NULL_TREE
, 0);
5308 if (TREE_CODE (ifexp
) == INTEGER_CST
)
5309 return integer_zerop (ifexp
) ? op2
: op1
;
5311 return convert_from_reference
5312 (fold (build (COND_EXPR
, result_type
, ifexp
, op1
, op2
)));
5315 /* Handle overloading of the ',' operator when needed. Otherwise,
5316 this function just builds an expression list. */
5319 build_x_compound_expr (list
)
5322 tree rest
= TREE_CHAIN (list
);
5325 if (processing_template_decl
)
5326 return build_min_nt (COMPOUND_EXPR
, list
, NULL_TREE
);
5328 if (rest
== NULL_TREE
)
5329 return build_compound_expr (list
);
5331 result
= build_opfncall (COMPOUND_EXPR
, LOOKUP_NORMAL
,
5332 TREE_VALUE (list
), TREE_VALUE (rest
), NULL_TREE
);
5334 return build_x_compound_expr (expr_tree_cons (NULL_TREE
, result
,
5335 TREE_CHAIN (rest
)));
5337 if (! TREE_SIDE_EFFECTS (TREE_VALUE (list
)))
5339 /* the left-hand operand of a comma expression is like an expression
5340 statement: we should warn if it doesn't have any side-effects,
5341 unless it was explicitly cast to (void). */
5342 if ((extra_warnings
|| warn_unused
)
5343 && !(TREE_CODE (TREE_VALUE(list
)) == CONVERT_EXPR
5344 && TREE_TYPE (TREE_VALUE(list
)) == void_type_node
))
5345 warning("left-hand operand of comma expression has no effect");
5347 #if 0 /* this requires a gcc backend patch to export warn_if_unused_value */
5348 else if (warn_unused
)
5349 warn_if_unused_value (TREE_VALUE(list
));
5352 return build_compound_expr
5353 (expr_tree_cons (NULL_TREE
, TREE_VALUE (list
),
5354 build_expr_list (NULL_TREE
,
5355 build_x_compound_expr (rest
))));
5358 /* Given a list of expressions, return a compound expression
5359 that performs them all and returns the value of the last of them. */
5362 build_compound_expr (list
)
5368 if (TREE_READONLY_DECL_P (TREE_VALUE (list
)))
5369 TREE_VALUE (list
) = decl_constant_value (TREE_VALUE (list
));
5371 if (TREE_CHAIN (list
) == 0)
5373 /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
5374 Strip such NOP_EXPRs, since LIST is used in non-lvalue context. */
5375 if (TREE_CODE (list
) == NOP_EXPR
5376 && TREE_TYPE (list
) == TREE_TYPE (TREE_OPERAND (list
, 0)))
5377 list
= TREE_OPERAND (list
, 0);
5379 /* Convert arrays to pointers. */
5380 if (TREE_CODE (TREE_TYPE (TREE_VALUE (list
))) == ARRAY_TYPE
)
5381 return default_conversion (TREE_VALUE (list
));
5383 return TREE_VALUE (list
);
5386 first
= TREE_VALUE (list
);
5387 first
= require_complete_type_in_void (first
);
5388 if (first
== error_mark_node
)
5389 return error_mark_node
;
5391 rest
= build_compound_expr (TREE_CHAIN (list
));
5392 if (rest
== error_mark_node
)
5393 return error_mark_node
;
5395 /* When pedantic, a compound expression cannot be a constant expression. */
5396 if (! TREE_SIDE_EFFECTS (first
) && ! pedantic
)
5399 return build (COMPOUND_EXPR
, TREE_TYPE (rest
),
5400 break_out_cleanups (first
), rest
);
5404 build_static_cast (type
, expr
)
5410 if (type
== error_mark_node
|| expr
== error_mark_node
)
5411 return error_mark_node
;
5413 if (TREE_CODE (expr
) == OFFSET_REF
)
5414 expr
= resolve_offset_ref (expr
);
5416 if (processing_template_decl
)
5418 tree t
= build_min (STATIC_CAST_EXPR
, copy_to_permanent (type
),
5423 /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
5424 Strip such NOP_EXPRs if VALUE is being used in non-lvalue context. */
5425 if (TREE_CODE (type
) != REFERENCE_TYPE
5426 && TREE_CODE (expr
) == NOP_EXPR
5427 && TREE_TYPE (expr
) == TREE_TYPE (TREE_OPERAND (expr
, 0)))
5428 expr
= TREE_OPERAND (expr
, 0);
5430 if (TREE_CODE (type
) == VOID_TYPE
)
5431 return build1 (CONVERT_EXPR
, type
, expr
);
5433 if (TREE_CODE (type
) == REFERENCE_TYPE
)
5434 return (convert_from_reference
5435 (convert_to_reference (type
, expr
, CONV_STATIC
|CONV_IMPLICIT
,
5436 LOOKUP_COMPLAIN
, NULL_TREE
)));
5438 if (IS_AGGR_TYPE (type
))
5439 return build_cplus_new
5440 (type
, (build_method_call
5441 (NULL_TREE
, ctor_identifier
, build_expr_list (NULL_TREE
, expr
),
5442 TYPE_BINFO (type
), LOOKUP_NORMAL
)));
5444 expr
= decay_conversion (expr
);
5445 intype
= TREE_TYPE (expr
);
5447 /* FIXME handle casting to array type. */
5450 if (can_convert_arg (type
, intype
, expr
))
5452 else if (TYPE_PTROB_P (type
) && TYPE_PTROB_P (intype
))
5455 if (IS_AGGR_TYPE (TREE_TYPE (type
)) && IS_AGGR_TYPE (TREE_TYPE (intype
))
5456 && at_least_as_qualified_p (TREE_TYPE (type
),
5458 && (binfo
= get_binfo (TREE_TYPE (intype
), TREE_TYPE (type
), 0))
5459 && ! TREE_VIA_VIRTUAL (binfo
))
5462 else if (TYPE_PTRMEM_P (type
) && TYPE_PTRMEM_P (intype
))
5464 if (same_type_p (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (type
))),
5465 TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (intype
))))
5466 && at_least_as_qualified_p (TREE_TYPE (TREE_TYPE (type
)),
5467 TREE_TYPE (TREE_TYPE (intype
)))
5468 && (binfo
= get_binfo (TYPE_OFFSET_BASETYPE (TREE_TYPE (type
)),
5469 TYPE_OFFSET_BASETYPE (TREE_TYPE (intype
)), 0))
5470 && ! TREE_VIA_VIRTUAL (binfo
))
5473 else if (TREE_CODE (intype
) != BOOLEAN_TYPE
5474 && TREE_CODE (type
) != ARRAY_TYPE
5475 && TREE_CODE (type
) != FUNCTION_TYPE
5476 && can_convert (intype
, type
))
5480 return build_c_cast (type
, expr
);
5482 cp_error ("static_cast from `%T' to `%T'", intype
, type
);
5483 return error_mark_node
;
5487 build_reinterpret_cast (type
, expr
)
5492 if (type
== error_mark_node
|| expr
== error_mark_node
)
5493 return error_mark_node
;
5495 if (TREE_CODE (expr
) == OFFSET_REF
)
5496 expr
= resolve_offset_ref (expr
);
5498 if (processing_template_decl
)
5500 tree t
= build_min (REINTERPRET_CAST_EXPR
,
5501 copy_to_permanent (type
), expr
);
5505 if (TREE_CODE (type
) != REFERENCE_TYPE
)
5507 expr
= decay_conversion (expr
);
5509 /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
5510 Strip such NOP_EXPRs if VALUE is being used in non-lvalue context. */
5511 if (TREE_CODE (expr
) == NOP_EXPR
5512 && TREE_TYPE (expr
) == TREE_TYPE (TREE_OPERAND (expr
, 0)))
5513 expr
= TREE_OPERAND (expr
, 0);
5516 intype
= TREE_TYPE (expr
);
5518 if (TREE_CODE (type
) == REFERENCE_TYPE
)
5520 if (! real_lvalue_p (expr
))
5522 cp_error ("reinterpret_cast from `%T' rvalue to `%T'", intype
, type
);
5523 return error_mark_node
;
5525 expr
= build_unary_op (ADDR_EXPR
, expr
, 0);
5526 if (expr
!= error_mark_node
)
5527 expr
= build_reinterpret_cast
5528 (build_pointer_type (TREE_TYPE (type
)), expr
);
5529 if (expr
!= error_mark_node
)
5530 expr
= build_indirect_ref (expr
, 0);
5533 else if (same_type_p (TYPE_MAIN_VARIANT (intype
),
5534 TYPE_MAIN_VARIANT (type
)))
5535 return build_static_cast (type
, expr
);
5537 if (TYPE_PTR_P (type
) && (TREE_CODE (intype
) == INTEGER_TYPE
5538 || TREE_CODE (intype
) == ENUMERAL_TYPE
))
5540 else if (TREE_CODE (type
) == INTEGER_TYPE
&& TYPE_PTR_P (intype
))
5542 if (TYPE_PRECISION (type
) < TYPE_PRECISION (intype
))
5543 cp_pedwarn ("reinterpret_cast from `%T' to `%T' loses precision",
5546 else if ((TYPE_PTRFN_P (type
) && TYPE_PTRFN_P (intype
))
5547 || (TYPE_PTRMEMFUNC_P (type
) && TYPE_PTRMEMFUNC_P (intype
)))
5549 if (TREE_READONLY_DECL_P (expr
))
5550 expr
= decl_constant_value (expr
);
5551 return fold (build1 (NOP_EXPR
, type
, expr
));
5553 else if ((TYPE_PTRMEM_P (type
) && TYPE_PTRMEM_P (intype
))
5554 || (TYPE_PTROBV_P (type
) && TYPE_PTROBV_P (intype
)))
5556 if (! comp_ptr_ttypes_reinterpret (TREE_TYPE (type
), TREE_TYPE (intype
)))
5557 cp_pedwarn ("reinterpret_cast from `%T' to `%T' casts away const (or volatile)",
5560 if (TREE_READONLY_DECL_P (expr
))
5561 expr
= decl_constant_value (expr
);
5562 return fold (build1 (NOP_EXPR
, type
, expr
));
5564 else if ((TYPE_PTRFN_P (type
) && TYPE_PTROBV_P (intype
))
5565 || (TYPE_PTRFN_P (intype
) && TYPE_PTROBV_P (type
)))
5567 pedwarn ("ANSI C++ forbids casting between pointers to functions and objects");
5568 if (TREE_READONLY_DECL_P (expr
))
5569 expr
= decl_constant_value (expr
);
5570 return fold (build1 (NOP_EXPR
, type
, expr
));
5574 cp_error ("reinterpret_cast from `%T' to `%T'", intype
, type
);
5575 return error_mark_node
;
5578 return cp_convert (type
, expr
);
5582 build_const_cast (type
, expr
)
5587 if (type
== error_mark_node
|| expr
== error_mark_node
)
5588 return error_mark_node
;
5590 if (TREE_CODE (expr
) == OFFSET_REF
)
5591 expr
= resolve_offset_ref (expr
);
5593 if (processing_template_decl
)
5595 tree t
= build_min (CONST_CAST_EXPR
, copy_to_permanent (type
),
5600 if (TREE_CODE (type
) != REFERENCE_TYPE
)
5602 expr
= decay_conversion (expr
);
5604 /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
5605 Strip such NOP_EXPRs if VALUE is being used in non-lvalue context. */
5606 if (TREE_CODE (expr
) == NOP_EXPR
5607 && TREE_TYPE (expr
) == TREE_TYPE (TREE_OPERAND (expr
, 0)))
5608 expr
= TREE_OPERAND (expr
, 0);
5611 intype
= TREE_TYPE (expr
);
5613 if (same_type_p (TYPE_MAIN_VARIANT (intype
), TYPE_MAIN_VARIANT (type
)))
5614 return build_static_cast (type
, expr
);
5615 else if (TREE_CODE (type
) == REFERENCE_TYPE
)
5617 if (! real_lvalue_p (expr
))
5619 cp_error ("const_cast from `%T' rvalue to `%T'", intype
, type
);
5620 return error_mark_node
;
5623 if (comp_ptr_ttypes_const (TREE_TYPE (type
), intype
))
5625 expr
= build_unary_op (ADDR_EXPR
, expr
, 0);
5626 expr
= build1 (NOP_EXPR
, type
, expr
);
5627 return convert_from_reference (expr
);
5630 else if (TREE_CODE (type
) == POINTER_TYPE
5631 && TREE_CODE (intype
) == POINTER_TYPE
5632 && comp_ptr_ttypes_const (TREE_TYPE (type
), TREE_TYPE (intype
)))
5633 return cp_convert (type
, expr
);
5635 cp_error ("const_cast from `%T' to `%T'", intype
, type
);
5636 return error_mark_node
;
5639 /* Build an expression representing a cast to type TYPE of expression EXPR.
5641 ALLOW_NONCONVERTING is true if we should allow non-converting constructors
5642 when doing the cast. */
5645 build_c_cast (type
, expr
)
5648 register tree value
= expr
;
5651 if (type
== error_mark_node
|| expr
== error_mark_node
)
5652 return error_mark_node
;
5654 /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
5655 Strip such NOP_EXPRs if VALUE is being used in non-lvalue context. */
5656 if (TREE_CODE (type
) != REFERENCE_TYPE
5657 && TREE_CODE (value
) == NOP_EXPR
5658 && TREE_TYPE (value
) == TREE_TYPE (TREE_OPERAND (value
, 0)))
5659 value
= TREE_OPERAND (value
, 0);
5661 if (TREE_TYPE (expr
)
5662 && TREE_CODE (TREE_TYPE (expr
)) == OFFSET_TYPE
5663 && TREE_CODE (type
) != OFFSET_TYPE
)
5664 value
= resolve_offset_ref (value
);
5666 if (TREE_CODE (type
) == ARRAY_TYPE
)
5668 /* Allow casting from T1* to T2[] because Cfront allows it.
5669 NIHCL uses it. It is not valid ANSI C however, and hence, not
5671 if (TREE_CODE (TREE_TYPE (expr
)) == POINTER_TYPE
)
5674 pedwarn ("ANSI C++ forbids casting to an array type");
5675 type
= build_pointer_type (TREE_TYPE (type
));
5679 error ("ANSI C++ forbids casting to an array type");
5680 return error_mark_node
;
5684 if (TREE_CODE (type
) == FUNCTION_TYPE
5685 || TREE_CODE (type
) == METHOD_TYPE
)
5687 cp_error ("casting to function type `%T'", type
);
5688 return error_mark_node
;
5691 if (IS_SIGNATURE (type
))
5693 error ("cast specifies signature type");
5694 return error_mark_node
;
5697 if (processing_template_decl
)
5699 tree t
= build_min (CAST_EXPR
, type
,
5700 min_tree_cons (NULL_TREE
, value
, NULL_TREE
));
5704 /* Convert functions and arrays to pointers and
5705 convert references to their expanded types,
5706 but don't convert any other types. If, however, we are
5707 casting to a class type, there's no reason to do this: the
5708 cast will only succeed if there is a converting constructor,
5709 and the default conversions will be done at that point. In
5710 fact, doing the default conversion here is actually harmful
5714 struct S { S(const A&); };
5716 since we don't want the array-to-pointer conversion done. */
5717 if (!IS_AGGR_TYPE (type
))
5719 if (TREE_CODE (TREE_TYPE (value
)) == FUNCTION_TYPE
5720 || (TREE_CODE (TREE_TYPE (value
)) == METHOD_TYPE
5721 /* Don't do the default conversion on a ->* expression. */
5722 && ! (TREE_CODE (type
) == POINTER_TYPE
5723 && bound_pmf_p (value
)))
5724 || TREE_CODE (TREE_TYPE (value
)) == ARRAY_TYPE
5725 || TREE_CODE (TREE_TYPE (value
)) == REFERENCE_TYPE
)
5726 value
= default_conversion (value
);
5728 else if (TREE_CODE (TREE_TYPE (value
)) == REFERENCE_TYPE
)
5729 /* However, even for class types, we still need to strip away
5730 the reference type, since the call to convert_force below
5731 does not expect the input expression to be of reference
5733 value
= convert_from_reference (value
);
5735 otype
= TREE_TYPE (value
);
5737 /* Optionally warn about potentially worrisome casts. */
5740 && TREE_CODE (type
) == POINTER_TYPE
5741 && TREE_CODE (otype
) == POINTER_TYPE
5742 && !at_least_as_qualified_p (TREE_TYPE (type
),
5744 cp_warning ("cast discards qualifiers from pointer target type");
5746 /* Warn about possible alignment problems. */
5747 if (STRICT_ALIGNMENT
&& warn_cast_align
5748 && TREE_CODE (type
) == POINTER_TYPE
5749 && TREE_CODE (otype
) == POINTER_TYPE
5750 && TREE_CODE (TREE_TYPE (otype
)) != VOID_TYPE
5751 && TREE_CODE (TREE_TYPE (otype
)) != FUNCTION_TYPE
5752 && TYPE_ALIGN (TREE_TYPE (type
)) > TYPE_ALIGN (TREE_TYPE (otype
)))
5753 warning ("cast increases required alignment of target type");
5756 /* We should see about re-enabling these, they seem useful to
5758 if (TREE_CODE (type
) == INTEGER_TYPE
5759 && TREE_CODE (otype
) == POINTER_TYPE
5760 && TYPE_PRECISION (type
) != TYPE_PRECISION (otype
))
5761 warning ("cast from pointer to integer of different size");
5763 if (TREE_CODE (type
) == POINTER_TYPE
5764 && TREE_CODE (otype
) == INTEGER_TYPE
5765 && TYPE_PRECISION (type
) != TYPE_PRECISION (otype
)
5766 /* Don't warn about converting 0 to pointer,
5767 provided the 0 was explicit--not cast or made by folding. */
5768 && !(TREE_CODE (value
) == INTEGER_CST
&& integer_zerop (value
)))
5769 warning ("cast to pointer from integer of different size");
5772 if (TREE_CODE (type
) == VOID_TYPE
)
5774 value
= require_complete_type_in_void (value
);
5775 if (value
!= error_mark_node
)
5776 value
= build1 (CONVERT_EXPR
, void_type_node
, value
);
5778 else if (TREE_CODE (type
) == REFERENCE_TYPE
)
5779 value
= (convert_from_reference
5780 (convert_to_reference (type
, value
, CONV_C_CAST
,
5781 LOOKUP_COMPLAIN
, NULL_TREE
)));
5786 if (TREE_READONLY_DECL_P (value
))
5787 value
= decl_constant_value (value
);
5790 value
= convert_force (type
, value
, CONV_C_CAST
);
5792 /* Ignore any integer overflow caused by the cast. */
5793 if (TREE_CODE (value
) == INTEGER_CST
)
5795 TREE_OVERFLOW (value
) = TREE_OVERFLOW (ovalue
);
5796 TREE_CONSTANT_OVERFLOW (value
) = TREE_CONSTANT_OVERFLOW (ovalue
);
5800 /* Always produce some operator for an explicit cast,
5801 so we can tell (for -pedantic) that the cast is no lvalue. */
5802 if (TREE_CODE (type
) != REFERENCE_TYPE
&& value
== expr
5803 && real_lvalue_p (value
))
5804 value
= non_lvalue (value
);
5809 /* Build an assignment expression of lvalue LHS from value RHS.
5810 MODIFYCODE is the code for a binary operator that we use
5811 to combine the old value of LHS with RHS to get the new value.
5812 Or else MODIFYCODE is NOP_EXPR meaning do a simple assignment.
5814 C++: If MODIFYCODE is INIT_EXPR, then leave references unbashed. */
5817 build_modify_expr (lhs
, modifycode
, rhs
)
5819 enum tree_code modifycode
;
5822 register tree result
;
5824 tree lhstype
= TREE_TYPE (lhs
);
5825 tree olhstype
= lhstype
;
5828 /* Avoid duplicate error messages from operands that had errors. */
5829 if (lhs
== error_mark_node
|| rhs
== error_mark_node
)
5830 return error_mark_node
;
5832 /* Types that aren't fully specified cannot be used in assignments. */
5833 lhs
= require_complete_type (lhs
);
5837 /* Handle assignment to signature pointers/refs. */
5839 if (TYPE_LANG_SPECIFIC (lhstype
)
5840 && (IS_SIGNATURE_POINTER (lhstype
) || IS_SIGNATURE_REFERENCE (lhstype
)))
5842 return build_signature_pointer_constructor (lhs
, rhs
);
5845 /* Handle control structure constructs used as "lvalues". */
5847 switch (TREE_CODE (lhs
))
5849 /* Handle --foo = 5; as these are valid constructs in C++ */
5850 case PREDECREMENT_EXPR
:
5851 case PREINCREMENT_EXPR
:
5852 if (TREE_SIDE_EFFECTS (TREE_OPERAND (lhs
, 0)))
5853 lhs
= build (TREE_CODE (lhs
), TREE_TYPE (lhs
),
5854 stabilize_reference (TREE_OPERAND (lhs
, 0)),
5855 TREE_OPERAND (lhs
, 1));
5856 return build (COMPOUND_EXPR
, lhstype
,
5858 build_modify_expr (TREE_OPERAND (lhs
, 0),
5861 /* Handle (a, b) used as an "lvalue". */
5863 newrhs
= build_modify_expr (TREE_OPERAND (lhs
, 1),
5865 if (newrhs
== error_mark_node
)
5866 return error_mark_node
;
5867 return build (COMPOUND_EXPR
, lhstype
,
5868 TREE_OPERAND (lhs
, 0), newrhs
);
5871 newrhs
= build_modify_expr (TREE_OPERAND (lhs
, 0), modifycode
, rhs
);
5872 if (newrhs
== error_mark_node
)
5873 return error_mark_node
;
5874 return build (COMPOUND_EXPR
, lhstype
, lhs
, newrhs
);
5876 /* Handle (a ? b : c) used as an "lvalue". */
5878 rhs
= save_expr (rhs
);
5880 /* Produce (a ? (b = rhs) : (c = rhs))
5881 except that the RHS goes through a save-expr
5882 so the code to compute it is only emitted once. */
5884 = build_conditional_expr (TREE_OPERAND (lhs
, 0),
5885 build_modify_expr (cp_convert (TREE_TYPE (lhs
), TREE_OPERAND (lhs
, 1)),
5887 build_modify_expr (cp_convert (TREE_TYPE (lhs
), TREE_OPERAND (lhs
, 2)),
5889 if (cond
== error_mark_node
)
5891 /* Make sure the code to compute the rhs comes out
5892 before the split. */
5893 return build (COMPOUND_EXPR
, TREE_TYPE (lhs
),
5894 /* Case to void to suppress warning
5895 from warn_if_unused_value. */
5896 cp_convert (void_type_node
, rhs
), cond
);
5903 if (TREE_CODE (lhs
) == OFFSET_REF
)
5905 if (TREE_OPERAND (lhs
, 0) == NULL_TREE
)
5907 /* Static class member? */
5908 tree member
= TREE_OPERAND (lhs
, 1);
5909 if (TREE_CODE (member
) == VAR_DECL
)
5913 compiler_error ("invalid static class member");
5914 return error_mark_node
;
5918 lhs
= resolve_offset_ref (lhs
);
5920 olhstype
= lhstype
= TREE_TYPE (lhs
);
5923 if (TREE_CODE (lhstype
) == REFERENCE_TYPE
5924 && modifycode
!= INIT_EXPR
)
5926 lhs
= convert_from_reference (lhs
);
5927 olhstype
= lhstype
= TREE_TYPE (lhs
);
5930 /* If a binary op has been requested, combine the old LHS value with the RHS
5931 producing the value we should actually store into the LHS. */
5933 if (modifycode
== INIT_EXPR
)
5935 if (! IS_AGGR_TYPE (lhstype
))
5936 /* Do the default thing */;
5939 result
= build_method_call (lhs
, ctor_identifier
,
5940 build_expr_list (NULL_TREE
, rhs
),
5941 TYPE_BINFO (lhstype
), LOOKUP_NORMAL
);
5942 if (result
== NULL_TREE
)
5943 return error_mark_node
;
5947 else if (modifycode
== NOP_EXPR
)
5949 /* `operator=' is not an inheritable operator. */
5950 if (! IS_AGGR_TYPE (lhstype
))
5951 /* Do the default thing */;
5954 result
= build_opfncall (MODIFY_EXPR
, LOOKUP_NORMAL
,
5955 lhs
, rhs
, make_node (NOP_EXPR
));
5956 if (result
== NULL_TREE
)
5957 return error_mark_node
;
5962 else if (PROMOTES_TO_AGGR_TYPE (lhstype
, REFERENCE_TYPE
))
5964 my_friendly_abort (978652);
5968 lhs
= stabilize_reference (lhs
);
5969 newrhs
= build_binary_op (modifycode
, lhs
, rhs
, 1);
5970 if (newrhs
== error_mark_node
)
5972 cp_error (" in evaluation of `%Q(%#T, %#T)'", modifycode
,
5973 TREE_TYPE (lhs
), TREE_TYPE (rhs
));
5974 return error_mark_node
;
5978 /* Handle a cast used as an "lvalue".
5979 We have already performed any binary operator using the value as cast.
5980 Now convert the result to the cast type of the lhs,
5981 and then true type of the lhs and store it there;
5982 then convert result back to the cast type to be the value
5983 of the assignment. */
5985 switch (TREE_CODE (lhs
))
5990 case FIX_TRUNC_EXPR
:
5991 case FIX_FLOOR_EXPR
:
5992 case FIX_ROUND_EXPR
:
5994 if (TREE_CODE (TREE_TYPE (newrhs
)) == ARRAY_TYPE
5995 || TREE_CODE (TREE_TYPE (newrhs
)) == FUNCTION_TYPE
5996 || TREE_CODE (TREE_TYPE (newrhs
)) == METHOD_TYPE
5997 || TREE_CODE (TREE_TYPE (newrhs
)) == OFFSET_TYPE
)
5998 newrhs
= default_conversion (newrhs
);
6000 tree inner_lhs
= TREE_OPERAND (lhs
, 0);
6003 /* WP 5.4.1: The result is an lvalue if T is a reference type,
6004 otherwise the result is an rvalue. */
6005 if (! lvalue_p (lhs
))
6006 pedwarn ("ANSI C++ forbids cast to non-reference type used as lvalue");
6008 result
= build_modify_expr (inner_lhs
, NOP_EXPR
,
6009 cp_convert (TREE_TYPE (inner_lhs
),
6010 cp_convert (lhstype
, newrhs
)));
6011 if (result
== error_mark_node
)
6013 return cp_convert (TREE_TYPE (lhs
), result
);
6020 /* Now we have handled acceptable kinds of LHS that are not truly lvalues.
6021 Reject anything strange now. */
6023 if (!lvalue_or_else (lhs
, "assignment"))
6024 return error_mark_node
;
6026 GNU_xref_assign (lhs
);
6028 /* Warn about storing in something that is `const'. */
6029 /* For C++, don't warn if this is initialization. */
6030 if (modifycode
!= INIT_EXPR
6031 /* For assignment to `const' signature pointer/reference fields,
6032 don't warn either, we already printed a better message before. */
6033 && ! (TREE_CODE (lhs
) == COMPONENT_REF
6034 && (IS_SIGNATURE_POINTER (TREE_TYPE (TREE_OPERAND (lhs
, 0)))
6035 || IS_SIGNATURE_REFERENCE (TREE_TYPE (TREE_OPERAND (lhs
, 0)))))
6036 && (TREE_READONLY (lhs
) || CP_TYPE_CONST_P (lhstype
)
6037 /* Functions are not modifiable, even though they are
6039 || TREE_CODE (TREE_TYPE (lhs
)) == FUNCTION_TYPE
6040 || ((TREE_CODE (lhstype
) == RECORD_TYPE
6041 || TREE_CODE (lhstype
) == UNION_TYPE
)
6042 && C_TYPE_FIELDS_READONLY (lhstype
))
6043 || (TREE_CODE (lhstype
) == REFERENCE_TYPE
6044 && CP_TYPE_CONST_P (TREE_TYPE (lhstype
)))))
6045 readonly_error (lhs
, "assignment", 0);
6047 /* If storing into a structure or union member,
6048 it has probably been given type `int'.
6049 Compute the type that would go with
6050 the actual amount of storage the member occupies. */
6052 if (TREE_CODE (lhs
) == COMPONENT_REF
6053 && (TREE_CODE (lhstype
) == INTEGER_TYPE
6054 || TREE_CODE (lhstype
) == REAL_TYPE
6055 || TREE_CODE (lhstype
) == ENUMERAL_TYPE
))
6057 lhstype
= TREE_TYPE (get_unwidened (lhs
, 0));
6059 /* If storing in a field that is in actuality a short or narrower
6060 than one, we must store in the field in its actual type. */
6062 if (lhstype
!= TREE_TYPE (lhs
))
6064 lhs
= copy_node (lhs
);
6065 TREE_TYPE (lhs
) = lhstype
;
6069 /* check to see if there is an assignment to `this' */
6070 if (lhs
== current_class_ptr
)
6072 if (flag_this_is_variable
> 0
6073 && DECL_NAME (current_function_decl
) != NULL_TREE
6074 && (DECL_NAME (current_function_decl
)
6075 != constructor_name (current_class_type
)))
6076 warning ("assignment to `this' not in constructor or destructor");
6077 current_function_just_assigned_this
= 1;
6080 if (modifycode
!= INIT_EXPR
)
6082 /* Make modifycode now either a NOP_EXPR or an INIT_EXPR. */
6083 modifycode
= NOP_EXPR
;
6084 /* Reference-bashing */
6085 if (TREE_CODE (lhstype
) == REFERENCE_TYPE
)
6087 tree tmp
= convert_from_reference (lhs
);
6088 lhstype
= TREE_TYPE (tmp
);
6089 if (TYPE_SIZE (lhstype
) == 0)
6091 incomplete_type_error (lhs
, lhstype
);
6092 return error_mark_node
;
6097 if (TREE_CODE (TREE_TYPE (newrhs
)) == REFERENCE_TYPE
)
6099 tree tmp
= convert_from_reference (newrhs
);
6100 if (TYPE_SIZE (TREE_TYPE (tmp
)) == 0)
6102 incomplete_type_error (newrhs
, TREE_TYPE (tmp
));
6103 return error_mark_node
;
6109 if (TREE_SIDE_EFFECTS (lhs
))
6110 lhs
= stabilize_reference (lhs
);
6111 if (TREE_SIDE_EFFECTS (newrhs
))
6112 newrhs
= stabilize_reference (newrhs
);
6114 /* Convert new value to destination type. */
6116 if (TREE_CODE (lhstype
) == ARRAY_TYPE
)
6120 if (!same_or_base_type_p (lhstype
, TREE_TYPE (rhs
)))
6122 cp_error ("incompatible types in assignment of `%T' to `%T'",
6123 TREE_TYPE (rhs
), lhstype
);
6124 return error_mark_node
;
6127 /* Allow array assignment in compiler-generated code. */
6128 if (pedantic
&& ! DECL_ARTIFICIAL (current_function_decl
))
6129 pedwarn ("ANSI C++ forbids assignment of arrays");
6131 /* Have to wrap this in RTL_EXPR for two cases:
6132 in base or member initialization and if we
6133 are a branch of a ?: operator. Since we
6134 can't easily know the latter, just do it always. */
6136 result
= make_node (RTL_EXPR
);
6138 TREE_TYPE (result
) = void_type_node
;
6139 do_pending_stack_adjust ();
6140 start_sequence_for_rtl_expr (result
);
6142 /* As a matter of principle, `start_sequence' should do this. */
6145 from_array
= TREE_CODE (TREE_TYPE (newrhs
)) == ARRAY_TYPE
6146 ? 1 + (modifycode
!= INIT_EXPR
): 0;
6147 expand_vec_init (lhs
, lhs
, array_type_nelts (lhstype
), newrhs
,
6150 do_pending_stack_adjust ();
6152 TREE_SIDE_EFFECTS (result
) = 1;
6153 RTL_EXPR_SEQUENCE (result
) = get_insns ();
6154 RTL_EXPR_RTL (result
) = const0_rtx
;
6159 if (modifycode
== INIT_EXPR
)
6161 newrhs
= convert_for_initialization (lhs
, lhstype
, newrhs
, LOOKUP_NORMAL
,
6162 "assignment", NULL_TREE
, 0);
6163 if (lhs
== DECL_RESULT (current_function_decl
))
6165 if (DECL_INITIAL (lhs
))
6166 warning ("return value from function receives multiple initializations");
6167 DECL_INITIAL (lhs
) = newrhs
;
6172 /* Avoid warnings on enum bit fields. */
6173 if (TREE_CODE (olhstype
) == ENUMERAL_TYPE
6174 && TREE_CODE (lhstype
) == INTEGER_TYPE
)
6176 newrhs
= convert_for_assignment (olhstype
, newrhs
, "assignment",
6178 newrhs
= convert_force (lhstype
, newrhs
, 0);
6181 newrhs
= convert_for_assignment (lhstype
, newrhs
, "assignment",
6183 if (TREE_CODE (newrhs
) == CALL_EXPR
6184 && TYPE_NEEDS_CONSTRUCTING (lhstype
))
6185 newrhs
= build_cplus_new (lhstype
, newrhs
);
6187 /* Can't initialize directly from a TARGET_EXPR, since that would
6188 cause the lhs to be constructed twice, and possibly result in
6189 accidental self-initialization. So we force the TARGET_EXPR to be
6190 expanded without a target. */
6191 if (TREE_CODE (newrhs
) == TARGET_EXPR
)
6192 newrhs
= build (COMPOUND_EXPR
, TREE_TYPE (newrhs
), newrhs
,
6193 TREE_OPERAND (newrhs
, 0));
6196 if (newrhs
== error_mark_node
)
6197 return error_mark_node
;
6199 if (TREE_CODE (newrhs
) == COND_EXPR
)
6202 tree cond
= TREE_OPERAND (newrhs
, 0);
6204 if (TREE_SIDE_EFFECTS (lhs
))
6205 cond
= build_compound_expr (tree_cons
6207 build_expr_list (NULL_TREE
, cond
)));
6209 /* Cannot have two identical lhs on this one tree (result) as preexpand
6210 calls will rip them out and fill in RTL for them, but when the
6211 rtl is generated, the calls will only be in the first side of the
6212 condition, not on both, or before the conditional jump! (mrs) */
6213 lhs1
= break_out_calls (lhs
);
6216 /* If there's no change, the COND_EXPR behaves like any other rhs. */
6217 result
= build (modifycode
== NOP_EXPR
? MODIFY_EXPR
: INIT_EXPR
,
6218 lhstype
, lhs
, newrhs
);
6221 tree result_type
= TREE_TYPE (newrhs
);
6222 /* We have to convert each arm to the proper type because the
6223 types may have been munged by constant folding. */
6225 = build (COND_EXPR
, result_type
, cond
,
6226 build_modify_expr (lhs
, modifycode
,
6227 cp_convert (result_type
,
6228 TREE_OPERAND (newrhs
, 1))),
6229 build_modify_expr (lhs1
, modifycode
,
6230 cp_convert (result_type
,
6231 TREE_OPERAND (newrhs
, 2))));
6235 result
= build (modifycode
== NOP_EXPR
? MODIFY_EXPR
: INIT_EXPR
,
6236 lhstype
, lhs
, newrhs
);
6238 TREE_SIDE_EFFECTS (result
) = 1;
6240 /* If we got the LHS in a different type for storing in,
6241 convert the result back to the nominal type of LHS
6242 so that the value we return always has the same type
6243 as the LHS argument. */
6245 if (olhstype
== TREE_TYPE (result
))
6247 /* Avoid warnings converting integral types back into enums
6248 for enum bit fields. */
6249 if (TREE_CODE (TREE_TYPE (result
)) == INTEGER_TYPE
6250 && TREE_CODE (olhstype
) == ENUMERAL_TYPE
)
6252 result
= build (COMPOUND_EXPR
, olhstype
, result
, olhs
);
6253 TREE_NO_UNUSED_WARNING (result
) = 1;
6256 return convert_for_assignment (olhstype
, result
, "assignment",
6261 build_x_modify_expr (lhs
, modifycode
, rhs
)
6263 enum tree_code modifycode
;
6266 if (processing_template_decl
)
6267 return build_min_nt (MODOP_EXPR
, lhs
,
6268 build_min_nt (modifycode
, NULL_TREE
, NULL_TREE
), rhs
);
6270 if (modifycode
!= NOP_EXPR
)
6272 tree rval
= build_opfncall (MODIFY_EXPR
, LOOKUP_NORMAL
, lhs
, rhs
,
6273 make_node (modifycode
));
6277 return build_modify_expr (lhs
, modifycode
, rhs
);
6281 /* Get difference in deltas for different pointer to member function
6282 types. Return integer_zero_node, if FROM cannot be converted to a
6283 TO type. If FORCE is true, then allow reverse conversions as well. */
6286 get_delta_difference (from
, to
, force
)
6290 tree delta
= integer_zero_node
;
6296 /* Should get_base_distance here, so we can check if any thing along the
6297 path is virtual, and we need to make sure we stay
6298 inside the real binfos when going through virtual bases.
6299 Maybe we should replace virtual bases with
6300 binfo_member (...CLASSTYPE_VBASECLASSES...)... (mrs) */
6301 binfo
= get_binfo (from
, to
, 1);
6302 if (binfo
== error_mark_node
)
6304 error (" in pointer to member function conversion");
6311 error_not_base_type (from
, to
);
6312 error (" in pointer to member conversion");
6315 binfo
= get_binfo (to
, from
, 1);
6316 if (binfo
== 0 || binfo
== error_mark_node
)
6318 if (TREE_VIA_VIRTUAL (binfo
))
6320 binfo
= binfo_member (BINFO_TYPE (binfo
),
6321 CLASSTYPE_VBASECLASSES (from
));
6322 cp_warning ("pointer to member cast to virtual base `%T'",
6323 BINFO_TYPE (binfo
));
6324 warning (" will only work if you are very careful");
6326 delta
= BINFO_OFFSET (binfo
);
6327 delta
= cp_convert (ptrdiff_type_node
, delta
);
6329 return build_binary_op (MINUS_EXPR
,
6334 if (TREE_VIA_VIRTUAL (binfo
))
6338 cp_warning ("pointer to member cast from virtual base `%T'",
6339 BINFO_TYPE (binfo
));
6340 warning (" will only work if you are very careful");
6343 cp_error ("pointer to member conversion from virtual base `%T'",
6344 BINFO_TYPE (binfo
));
6347 return BINFO_OFFSET (binfo
);
6351 build_ptrmemfunc1 (type
, delta
, idx
, pfn
, delta2
)
6352 tree type
, delta
, idx
, pfn
, delta2
;
6357 /* This is the old way we did it. We want to avoid calling
6358 digest_init, so that it can give an error if we use { } when
6359 initializing a pointer to member function. */
6363 u
= build_nt (CONSTRUCTOR
, NULL_TREE
,
6364 expr_tree_cons (pfn_identifier
, pfn
, NULL_TREE
));
6368 u
= build_nt (CONSTRUCTOR
, NULL_TREE
,
6369 expr_tree_cons (delta2_identifier
, delta2
, NULL_TREE
));
6372 u
= build_nt (CONSTRUCTOR
, NULL_TREE
,
6373 expr_tree_cons (NULL_TREE
, delta
,
6374 expr_tree_cons (NULL_TREE
, idx
,
6375 expr_tree_cons (NULL_TREE
, u
, NULL_TREE
))));
6377 return digest_init (type
, u
, (tree
*)0);
6379 tree delta_field
, idx_field
, pfn_or_delta2_field
, pfn_field
, delta2_field
;
6381 int allconstant
, allsimple
;
6383 delta_field
= TYPE_FIELDS (type
);
6384 idx_field
= TREE_CHAIN (delta_field
);
6385 pfn_or_delta2_field
= TREE_CHAIN (idx_field
);
6386 subtype
= TREE_TYPE (pfn_or_delta2_field
);
6387 pfn_field
= TYPE_FIELDS (subtype
);
6388 delta2_field
= TREE_CHAIN (pfn_field
);
6392 allconstant
= TREE_CONSTANT (pfn
);
6393 allsimple
= !! initializer_constant_valid_p (pfn
, TREE_TYPE (pfn
));
6394 u
= expr_tree_cons (pfn_field
, pfn
, NULL_TREE
);
6398 delta2
= convert_and_check (delta_type_node
, delta2
);
6399 allconstant
= TREE_CONSTANT (delta2
);
6400 allsimple
= !! initializer_constant_valid_p (delta2
, TREE_TYPE (delta2
));
6401 u
= expr_tree_cons (delta2_field
, delta2
, NULL_TREE
);
6404 delta
= convert_and_check (delta_type_node
, delta
);
6405 idx
= convert_and_check (delta_type_node
, idx
);
6407 allconstant
= allconstant
&& TREE_CONSTANT (delta
) && TREE_CONSTANT (idx
);
6408 allsimple
= allsimple
6409 && initializer_constant_valid_p (delta
, TREE_TYPE (delta
))
6410 && initializer_constant_valid_p (idx
, TREE_TYPE (idx
));
6412 u
= build (CONSTRUCTOR
, subtype
, NULL_TREE
, u
);
6413 u
= expr_tree_cons (delta_field
, delta
,
6414 expr_tree_cons (idx_field
, idx
,
6415 expr_tree_cons (pfn_or_delta2_field
, u
, NULL_TREE
)));
6416 u
= build (CONSTRUCTOR
, type
, NULL_TREE
, u
);
6417 TREE_CONSTANT (u
) = allconstant
;
6418 TREE_STATIC (u
) = allconstant
&& allsimple
;
6423 /* Build a constructor for a pointer to member function. It can be
6424 used to initialize global variables, local variable, or used
6425 as a value in expressions. TYPE is the POINTER to METHOD_TYPE we
6428 If FORCE is non-zero, then force this conversion, even if
6429 we would rather not do it. Usually set when using an explicit
6432 Return error_mark_node, if something goes wrong. */
6435 build_ptrmemfunc (type
, pfn
, force
)
6439 tree idx
= integer_zero_node
;
6440 tree delta
= integer_zero_node
;
6441 tree delta2
= integer_zero_node
;
6443 tree npfn
= NULL_TREE
;
6445 /* Handle multiple conversions of pointer to member functions. */
6446 if (TYPE_PTRMEMFUNC_P (TREE_TYPE (pfn
)))
6448 tree ndelta
, ndelta2
;
6452 /* Is is already the right type? */
6453 if (type
== TYPE_PTRMEMFUNC_FN_TYPE (TREE_TYPE (pfn
)))
6456 pfn_type
= TYPE_PTRMEMFUNC_FN_TYPE (TREE_TYPE (pfn
));
6458 && comp_target_types (type
, pfn_type
, 1) != 1)
6459 cp_error ("conversion to `%T' from `%T'", type
, pfn_type
);
6461 ndelta
= cp_convert (ptrdiff_type_node
, build_component_ref (pfn
, delta_identifier
, NULL_TREE
, 0));
6462 ndelta2
= cp_convert (ptrdiff_type_node
, DELTA2_FROM_PTRMEMFUNC (pfn
));
6463 idx
= build_component_ref (pfn
, index_identifier
, NULL_TREE
, 0);
6465 n
= get_delta_difference (TYPE_METHOD_BASETYPE (TREE_TYPE (pfn_type
)),
6466 TYPE_METHOD_BASETYPE (TREE_TYPE (type
)),
6469 delta
= build_binary_op (PLUS_EXPR
, ndelta
, n
, 1);
6470 delta2
= build_binary_op (PLUS_EXPR
, ndelta2
, n
, 1);
6471 e1
= fold (build (GT_EXPR
, boolean_type_node
, idx
, integer_zero_node
));
6473 e2
= build_ptrmemfunc1 (TYPE_GET_PTRMEMFUNC_TYPE (type
), delta
, idx
,
6476 pfn
= PFN_FROM_PTRMEMFUNC (pfn
);
6477 npfn
= build1 (NOP_EXPR
, type
, pfn
);
6478 TREE_CONSTANT (npfn
) = TREE_CONSTANT (pfn
);
6480 e3
= build_ptrmemfunc1 (TYPE_GET_PTRMEMFUNC_TYPE (type
), delta
, idx
, npfn
,
6482 return build_conditional_expr (e1
, e2
, e3
);
6485 /* Handle null pointer to member function conversions. */
6486 if (integer_zerop (pfn
))
6488 pfn
= build_c_cast (type
, integer_zero_node
);
6489 return build_ptrmemfunc1 (TYPE_GET_PTRMEMFUNC_TYPE (type
),
6490 integer_zero_node
, integer_zero_node
,
6494 if (type_unknown_p (pfn
))
6495 return instantiate_type (type
, pfn
, 1);
6498 && comp_target_types (type
, TREE_TYPE (pfn
), 0) != 1)
6499 cp_error ("conversion to `%T' from `%T'", type
, TREE_TYPE (pfn
));
6501 /* Allow pointer to member conversions here. */
6502 delta
= get_delta_difference (TYPE_METHOD_BASETYPE (TREE_TYPE (TREE_TYPE (pfn
))),
6503 TYPE_METHOD_BASETYPE (TREE_TYPE (type
)),
6505 delta2
= build_binary_op (PLUS_EXPR
, delta2
, delta
, 1);
6507 if (TREE_CODE (TREE_OPERAND (pfn
, 0)) != FUNCTION_DECL
)
6508 warning ("assuming pointer to member function is non-virtual");
6510 if (TREE_CODE (TREE_OPERAND (pfn
, 0)) == FUNCTION_DECL
6511 && DECL_VINDEX (TREE_OPERAND (pfn
, 0)))
6513 /* Find the offset to the vfield pointer in the object. */
6514 vfield_offset
= get_binfo (DECL_CONTEXT (TREE_OPERAND (pfn
, 0)),
6515 DECL_CLASS_CONTEXT (TREE_OPERAND (pfn
, 0)),
6517 vfield_offset
= get_vfield_offset (vfield_offset
);
6518 delta2
= size_binop (PLUS_EXPR
, vfield_offset
, delta2
);
6520 /* Map everything down one to make room for the null pointer to member. */
6521 idx
= size_binop (PLUS_EXPR
,
6522 DECL_VINDEX (TREE_OPERAND (pfn
, 0)),
6527 idx
= size_binop (MINUS_EXPR
, integer_zero_node
, integer_one_node
);
6529 if (type
== TREE_TYPE (pfn
))
6535 npfn
= build1 (NOP_EXPR
, type
, pfn
);
6536 TREE_CONSTANT (npfn
) = TREE_CONSTANT (pfn
);
6540 return build_ptrmemfunc1 (TYPE_GET_PTRMEMFUNC_TYPE (type
), delta
, idx
, npfn
, delta2
);
6543 /* Convert value RHS to type TYPE as preparation for an assignment
6544 to an lvalue of type TYPE.
6545 The real work of conversion is done by `convert'.
6546 The purpose of this function is to generate error messages
6547 for assignments that are not allowed in C.
6548 ERRTYPE is a string to use in error messages:
6549 "assignment", "return", etc.
6551 C++: attempts to allow `convert' to find conversions involving
6552 implicit type conversion between aggregate and scalar types
6553 as per 8.5.6 of C++ manual. Does not randomly dereference
6554 pointers to aggregates! */
6557 convert_for_assignment (type
, rhs
, errtype
, fndecl
, parmnum
)
6563 register enum tree_code codel
= TREE_CODE (type
);
6564 register tree rhstype
;
6565 register enum tree_code coder
= TREE_CODE (TREE_TYPE (rhs
));
6567 /* Issue warnings about peculiar, but legal, uses of NULL. */
6568 if (ARITHMETIC_TYPE_P (type
) && rhs
== null_node
)
6569 cp_warning ("converting NULL to non-pointer type");
6571 if (coder
== ERROR_MARK
)
6572 return error_mark_node
;
6574 if (codel
== OFFSET_TYPE
)
6576 type
= TREE_TYPE (type
);
6577 codel
= TREE_CODE (type
);
6580 /* Strip NON_LVALUE_EXPRs since we aren't using as an lvalue. */
6581 if (TREE_CODE (rhs
) == NON_LVALUE_EXPR
)
6582 rhs
= TREE_OPERAND (rhs
, 0);
6584 if (rhs
== error_mark_node
)
6585 return error_mark_node
;
6587 if (TREE_CODE (rhs
) == TREE_LIST
&& TREE_VALUE (rhs
) == error_mark_node
)
6588 return error_mark_node
;
6590 if (TREE_CODE (TREE_TYPE (rhs
)) == OFFSET_TYPE
)
6592 rhs
= resolve_offset_ref (rhs
);
6593 if (rhs
== error_mark_node
)
6594 return error_mark_node
;
6595 rhstype
= TREE_TYPE (rhs
);
6596 coder
= TREE_CODE (rhstype
);
6599 if (TREE_CODE (TREE_TYPE (rhs
)) == ARRAY_TYPE
6600 || is_overloaded_fn (rhs
))
6601 rhs
= default_conversion (rhs
);
6602 else if (TREE_CODE (TREE_TYPE (rhs
)) == REFERENCE_TYPE
)
6603 rhs
= convert_from_reference (rhs
);
6605 /* If rhs is some sort of overloaded function, ocp_convert will either
6606 do the right thing or complain; we don't need to check anything else.
6607 So just hand off. */
6608 if (type_unknown_p (rhs
))
6609 return ocp_convert (type
, rhs
, CONV_IMPLICIT
, LOOKUP_NORMAL
);
6611 rhstype
= TREE_TYPE (rhs
);
6612 coder
= TREE_CODE (rhstype
);
6614 /* This should no longer change types on us. */
6615 if (TREE_CODE (rhs
) == CONST_DECL
)
6616 rhs
= DECL_INITIAL (rhs
);
6617 else if (TREE_READONLY_DECL_P (rhs
))
6618 rhs
= decl_constant_value (rhs
);
6620 if (same_type_p (type
, rhstype
))
6622 overflow_warning (rhs
);
6626 if (coder
== VOID_TYPE
)
6628 error ("void value not ignored as it ought to be");
6629 return error_mark_node
;
6631 /* Arithmetic types all interconvert. */
6632 if ((codel
== INTEGER_TYPE
|| codel
== REAL_TYPE
|| codel
== BOOLEAN_TYPE
6633 || codel
== COMPLEX_TYPE
)
6634 && (coder
== INTEGER_TYPE
|| coder
== REAL_TYPE
|| coder
== BOOLEAN_TYPE
6635 || coder
== COMPLEX_TYPE
))
6637 /* But we should warn if assigning REAL_TYPE to INTEGER_TYPE. */
6638 if (coder
== REAL_TYPE
&& codel
== INTEGER_TYPE
)
6641 cp_warning ("`%T' used for argument %P of `%D'",
6642 rhstype
, parmnum
, fndecl
);
6644 cp_warning ("%s to `%T' from `%T'", errtype
, type
, rhstype
);
6646 /* And we should warn if assigning a negative value to
6647 an unsigned variable. */
6648 else if (TREE_UNSIGNED (type
) && codel
!= BOOLEAN_TYPE
)
6650 if (TREE_CODE (rhs
) == INTEGER_CST
6651 && TREE_NEGATED_INT (rhs
))
6654 cp_warning ("negative value `%E' passed as argument %P of `%D'",
6655 rhs
, parmnum
, fndecl
);
6657 cp_warning ("%s of negative value `%E' to `%T'",
6658 errtype
, rhs
, type
);
6660 overflow_warning (rhs
);
6661 if (TREE_CONSTANT (rhs
))
6665 return convert_and_check (type
, rhs
);
6667 /* Conversions involving enums. */
6668 else if ((codel
== ENUMERAL_TYPE
6669 && (INTEGRAL_CODE_P (coder
) || coder
== REAL_TYPE
))
6670 || (coder
== ENUMERAL_TYPE
6671 && (INTEGRAL_CODE_P (codel
) || codel
== REAL_TYPE
)))
6673 return ocp_convert (type
, rhs
, CONV_IMPLICIT
, LOOKUP_NORMAL
);
6675 /* Conversions among pointers */
6676 else if (codel
== POINTER_TYPE
6677 && (coder
== POINTER_TYPE
6678 || (coder
== RECORD_TYPE
6679 && (IS_SIGNATURE_POINTER (rhstype
)
6680 || IS_SIGNATURE_REFERENCE (rhstype
)))))
6682 register tree ttl
= TREE_TYPE (type
);
6686 if (coder
== RECORD_TYPE
)
6688 rhs
= build_optr_ref (rhs
);
6689 rhstype
= TREE_TYPE (rhs
);
6691 ttr
= TREE_TYPE (rhstype
);
6693 /* If both pointers are of aggregate type, then we
6694 can give better error messages, and save some work
6696 if (TREE_CODE (ttl
) == RECORD_TYPE
&& TREE_CODE (ttr
) == RECORD_TYPE
)
6700 if (TYPE_MAIN_VARIANT (ttl
) == TYPE_MAIN_VARIANT (ttr
)
6701 || type
== class_star_type_node
6702 || rhstype
== class_star_type_node
)
6703 binfo
= TYPE_BINFO (ttl
);
6705 binfo
= get_binfo (ttl
, ttr
, 1);
6707 if (binfo
== error_mark_node
)
6708 return error_mark_node
;
6710 return error_not_base_type (ttl
, ttr
);
6712 if (!at_least_as_qualified_p (ttl
, ttr
))
6715 cp_pedwarn ("passing `%T' as argument %P of `%D' discards qualifiers",
6716 rhstype
, parmnum
, fndecl
);
6718 cp_pedwarn ("%s to `%T' from `%T' discards qualifiers",
6719 errtype
, type
, rhstype
);
6723 /* Any non-function converts to a [const][volatile] void *
6724 and vice versa; otherwise, targets must be the same.
6725 Meanwhile, the lhs target must have all the qualifiers of the rhs. */
6726 else if (TYPE_MAIN_VARIANT (ttl
) == void_type_node
6727 || TYPE_MAIN_VARIANT (ttr
) == void_type_node
6728 || (ctt
= comp_target_types (type
, rhstype
, 1))
6729 || (unsigned_type (TYPE_MAIN_VARIANT (ttl
))
6730 == unsigned_type (TYPE_MAIN_VARIANT (ttr
))))
6732 /* ARM $4.8, commentary on p39. */
6733 if (TYPE_MAIN_VARIANT (ttl
) == void_type_node
6734 && TREE_CODE (ttr
) == OFFSET_TYPE
)
6736 cp_error ("no standard conversion from `%T' to `void *'", ttr
);
6737 return error_mark_node
;
6740 if (ctt
< 0 && TYPE_MAIN_VARIANT (ttl
) != TYPE_MAIN_VARIANT (ttr
))
6741 cp_pedwarn ("converting `%T' to `%T' is a contravariance violation",
6744 if (TYPE_MAIN_VARIANT (ttl
) != void_type_node
6745 && TYPE_MAIN_VARIANT (ttr
) == void_type_node
6746 && ! null_ptr_cst_p (rhs
))
6748 if (coder
== RECORD_TYPE
)
6749 cp_pedwarn ("implicit conversion of signature pointer to type `%T'",
6752 pedwarn ("ANSI C++ forbids implicit conversion from `void *' in %s",
6755 /* Const and volatile mean something different for function types,
6756 so the usual warnings are not appropriate. */
6757 else if ((TREE_CODE (ttr
) != FUNCTION_TYPE
&& TREE_CODE (ttr
) != METHOD_TYPE
)
6758 || (TREE_CODE (ttl
) != FUNCTION_TYPE
&& TREE_CODE (ttl
) != METHOD_TYPE
))
6760 if (TREE_CODE (ttl
) == OFFSET_TYPE
6761 && binfo_member (TYPE_OFFSET_BASETYPE (ttr
),
6762 CLASSTYPE_VBASECLASSES (TYPE_OFFSET_BASETYPE (ttl
))))
6764 error ("%s between pointer to members converting across virtual baseclasses", errtype
);
6765 return error_mark_node
;
6767 else if (!at_least_as_qualified_p (ttl
, ttr
))
6769 if (string_conv_p (type
, rhs
, 1))
6770 /* converting from string constant to char *, OK. */;
6772 cp_pedwarn ("passing `%T' as argument %P of `%D' discards qualifiers",
6773 rhstype
, parmnum
, fndecl
);
6775 cp_pedwarn ("%s to `%T' from `%T' discards qualifiers",
6776 errtype
, type
, rhstype
);
6778 else if (TREE_CODE (ttl
) == TREE_CODE (ttr
)
6779 && ! comp_target_types (type
, rhstype
, 1))
6782 cp_pedwarn ("passing `%T' as argument %P of `%D' changes signedness",
6783 rhstype
, parmnum
, fndecl
);
6785 cp_pedwarn ("%s to `%T' from `%T' changes signedness",
6786 errtype
, type
, rhstype
);
6793 int drops_quals
= 0;
6795 int unsigned_parity
;
6798 /* This code is basically a duplicate of comp_ptr_ttypes_real. */
6799 for (; ; ttl
= TREE_TYPE (ttl
), ttr
= TREE_TYPE (ttr
))
6802 drops_quals
|= !at_least_as_qualified_p (ttl
, ttr
);
6805 && !at_least_as_qualified_p (ttr
, ttl
))
6807 left_const
&= TYPE_READONLY (ttl
);
6809 if (TREE_CODE (ttl
) != POINTER_TYPE
6810 || TREE_CODE (ttr
) != POINTER_TYPE
)
6813 unsigned_parity
= TREE_UNSIGNED (ttl
) - TREE_UNSIGNED (ttr
);
6814 if (unsigned_parity
)
6816 if (TREE_UNSIGNED (ttl
))
6817 ttr
= unsigned_type (ttr
);
6819 ttl
= unsigned_type (ttl
);
6822 if (comp_target_types (ttl
, ttr
, nptrs
) > 0)
6827 cp_pedwarn ("passing `%T' as argument %P of `%D' adds cv-quals without intervening `const'",
6828 rhstype
, parmnum
, fndecl
);
6830 cp_pedwarn ("%s to `%T' from `%T' adds cv-quals without intervening `const'",
6831 errtype
, type
, rhstype
);
6836 cp_pedwarn ("passing `%T' as argument %P of `%D' discards qualifiers",
6837 rhstype
, parmnum
, fndecl
);
6839 cp_pedwarn ("%s to `%T' from `%T' discards qualifiers",
6840 errtype
, type
, rhstype
);
6842 if (unsigned_parity
> 0)
6845 cp_pedwarn ("passing `%T' as argument %P of `%D' changes signed to unsigned",
6846 rhstype
, parmnum
, fndecl
);
6848 cp_pedwarn ("%s to `%T' from `%T' changes signed to unsigned",
6849 errtype
, type
, rhstype
);
6851 else if (unsigned_parity
< 0)
6854 cp_pedwarn ("passing `%T' as argument %P of `%D' changes unsigned to signed",
6855 rhstype
, parmnum
, fndecl
);
6857 cp_pedwarn ("%s to `%T' from `%T' changes unsigned to signed",
6858 errtype
, type
, rhstype
);
6861 /* C++ is not so friendly about converting function and
6862 member function pointers as C. Emit warnings here. */
6863 if (TREE_CODE (ttl
) == FUNCTION_TYPE
6864 || TREE_CODE (ttl
) == METHOD_TYPE
)
6865 if (!same_or_base_type_p (ttl
, ttr
))
6867 warning ("conflicting function types in %s:", errtype
);
6868 cp_warning ("\t`%T' != `%T'", type
, rhstype
);
6874 cp_error ("passing `%T' as argument %P of `%D'",
6875 rhstype
, parmnum
, fndecl
);
6877 cp_error ("%s to `%T' from `%T'", errtype
, type
, rhstype
);
6878 return error_mark_node
;
6881 return cp_convert (type
, rhs
);
6883 else if (codel
== POINTER_TYPE
6884 && (coder
== INTEGER_TYPE
6885 || coder
== BOOLEAN_TYPE
))
6887 /* An explicit constant 0 can convert to a pointer,
6888 but not a 0 that results from casting or folding. */
6889 if (! (TREE_CODE (rhs
) == INTEGER_CST
&& integer_zerop (rhs
)))
6892 cp_pedwarn ("passing `%T' to argument %P of `%D' lacks a cast",
6893 rhstype
, parmnum
, fndecl
);
6895 cp_pedwarn ("%s to `%T' from `%T' lacks a cast",
6896 errtype
, type
, rhstype
);
6898 return cp_convert (type
, rhs
);
6900 else if (codel
== INTEGER_TYPE
6901 && (coder
== POINTER_TYPE
6902 || (coder
== RECORD_TYPE
6903 && (IS_SIGNATURE_POINTER (rhstype
)
6904 || TYPE_PTRMEMFUNC_FLAG (rhstype
)
6905 || IS_SIGNATURE_REFERENCE (rhstype
)))))
6908 cp_pedwarn ("passing `%T' to argument %P of `%D' lacks a cast",
6909 rhstype
, parmnum
, fndecl
);
6911 cp_pedwarn ("%s to `%T' from `%T' lacks a cast",
6912 errtype
, type
, rhstype
);
6913 return cp_convert (type
, rhs
);
6915 else if (codel
== BOOLEAN_TYPE
6916 && (coder
== POINTER_TYPE
6917 || (coder
== RECORD_TYPE
6918 && (IS_SIGNATURE_POINTER (rhstype
)
6919 || TYPE_PTRMEMFUNC_FLAG (rhstype
)
6920 || IS_SIGNATURE_REFERENCE (rhstype
)))))
6921 return cp_convert (type
, rhs
);
6924 else if (((coder
== POINTER_TYPE
6925 && TREE_CODE (TREE_TYPE (rhstype
)) == METHOD_TYPE
)
6926 || integer_zerop (rhs
)
6927 || TYPE_PTRMEMFUNC_P (rhstype
))
6928 && TYPE_PTRMEMFUNC_P (type
))
6930 tree ttl
= TYPE_PTRMEMFUNC_FN_TYPE (type
);
6931 tree ttr
= (TYPE_PTRMEMFUNC_P (rhstype
)
6932 ? TYPE_PTRMEMFUNC_FN_TYPE (rhstype
)
6934 int ctt
= (TREE_CODE (rhstype
) == INTEGER_TYPE
? 1
6935 : comp_target_types (ttl
, ttr
, 1));
6938 cp_pedwarn ("converting `%T' to `%T' is a contravariance violation",
6941 cp_error ("%s to `%T' from `%T'", errtype
, ttl
, ttr
);
6943 /* compatible pointer to member functions. */
6944 return build_ptrmemfunc (ttl
, rhs
, 0);
6946 else if (codel
== ERROR_MARK
|| coder
== ERROR_MARK
)
6947 return error_mark_node
;
6949 /* This should no longer happen. References are initialized via
6950 `convert_for_initialization'. They should otherwise be
6951 bashed before coming here. */
6952 else if (codel
== REFERENCE_TYPE
)
6953 my_friendly_abort (317);
6954 else if (TYPE_MAIN_VARIANT (type
) == TYPE_MAIN_VARIANT (TREE_TYPE (rhs
)))
6956 tree nrhs
= build1 (NOP_EXPR
, type
, rhs
);
6957 TREE_CONSTANT (nrhs
) = TREE_CONSTANT (rhs
);
6960 else if (TYPE_HAS_CONSTRUCTOR (type
) || IS_AGGR_TYPE (TREE_TYPE (rhs
)))
6961 return cp_convert (type
, rhs
);
6962 /* Handle anachronistic conversions from (::*)() to cv void* or (*)(). */
6963 else if (TREE_CODE (type
) == POINTER_TYPE
6964 && (TREE_CODE (TREE_TYPE (type
)) == FUNCTION_TYPE
6965 || TYPE_MAIN_VARIANT (TREE_TYPE (type
)) == void_type_node
)
6967 && TYPE_PTRMEMFUNC_P (TREE_TYPE (rhs
)))
6968 return cp_convert (type
, rhs
);
6970 cp_error ("%s to `%T' from `%T'", errtype
, type
, rhstype
);
6971 return error_mark_node
;
6974 /* Convert RHS to be of type TYPE.
6975 If EXP is non-zero, it is the target of the initialization.
6976 ERRTYPE is a string to use in error messages.
6978 Two major differences between the behavior of
6979 `convert_for_assignment' and `convert_for_initialization'
6980 are that references are bashed in the former, while
6981 copied in the latter, and aggregates are assigned in
6982 the former (operator=) while initialized in the
6985 If using constructor make sure no conversion operator exists, if one does
6986 exist, an ambiguity exists.
6988 If flags doesn't include LOOKUP_COMPLAIN, don't complain about anything. */
6991 convert_for_initialization (exp
, type
, rhs
, flags
, errtype
, fndecl
, parmnum
)
6992 tree exp
, type
, rhs
;
6998 register enum tree_code codel
= TREE_CODE (type
);
6999 register tree rhstype
;
7000 register enum tree_code coder
;
7002 /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
7003 Strip such NOP_EXPRs, since RHS is used in non-lvalue context. */
7004 if (TREE_CODE (rhs
) == NOP_EXPR
7005 && TREE_TYPE (rhs
) == TREE_TYPE (TREE_OPERAND (rhs
, 0))
7006 && codel
!= REFERENCE_TYPE
)
7007 rhs
= TREE_OPERAND (rhs
, 0);
7009 if (rhs
== error_mark_node
7010 || (TREE_CODE (rhs
) == TREE_LIST
&& TREE_VALUE (rhs
) == error_mark_node
))
7011 return error_mark_node
;
7013 if (TREE_CODE (TREE_TYPE (rhs
)) == OFFSET_TYPE
)
7015 rhs
= resolve_offset_ref (rhs
);
7016 if (rhs
== error_mark_node
)
7017 return error_mark_node
;
7020 if (TREE_CODE (TREE_TYPE (rhs
)) == REFERENCE_TYPE
)
7021 rhs
= convert_from_reference (rhs
);
7023 if ((TREE_CODE (TREE_TYPE (rhs
)) == ARRAY_TYPE
7024 && TREE_CODE (type
) != ARRAY_TYPE
7025 && (TREE_CODE (type
) != REFERENCE_TYPE
7026 || TREE_CODE (TREE_TYPE (type
)) != ARRAY_TYPE
))
7027 || (TREE_CODE (TREE_TYPE (rhs
)) == FUNCTION_TYPE
7028 && (TREE_CODE (type
) != REFERENCE_TYPE
7029 || TREE_CODE (TREE_TYPE (type
)) != FUNCTION_TYPE
))
7030 || TREE_CODE (TREE_TYPE (rhs
)) == METHOD_TYPE
)
7031 rhs
= default_conversion (rhs
);
7033 rhstype
= TREE_TYPE (rhs
);
7034 coder
= TREE_CODE (rhstype
);
7036 if (coder
== ERROR_MARK
)
7037 return error_mark_node
;
7039 /* We accept references to incomplete types, so we can
7040 return here before checking if RHS is of complete type. */
7042 if (codel
== REFERENCE_TYPE
)
7044 /* This should eventually happen in convert_arguments. */
7045 extern int warningcount
, errorcount
;
7046 int savew
= 0, savee
= 0;
7049 savew
= warningcount
, savee
= errorcount
;
7050 rhs
= convert_to_reference (type
, rhs
, CONV_IMPLICIT
, flags
,
7051 exp
? exp
: error_mark_node
);
7054 if (warningcount
> savew
)
7055 cp_warning_at ("in passing argument %P of `%+D'", parmnum
, fndecl
);
7056 else if (errorcount
> savee
)
7057 cp_error_at ("in passing argument %P of `%+D'", parmnum
, fndecl
);
7063 exp
= require_complete_type (exp
);
7064 if (exp
== error_mark_node
)
7065 return error_mark_node
;
7067 if (TREE_CODE (rhstype
) == REFERENCE_TYPE
)
7068 rhstype
= TREE_TYPE (rhstype
);
7070 type
= complete_type (type
);
7072 if (TYPE_LANG_SPECIFIC (type
)
7073 && (IS_SIGNATURE_POINTER (type
) || IS_SIGNATURE_REFERENCE (type
)))
7074 return build_signature_pointer_constructor (type
, rhs
);
7076 if (IS_AGGR_TYPE (type
))
7077 return ocp_convert (type
, rhs
, CONV_IMPLICIT
|CONV_FORCE_TEMP
, flags
);
7079 if (type
== TREE_TYPE (rhs
))
7081 /* Issue warnings about peculiar, but legal, uses of NULL. We
7082 do this *before* the call to decl_constant_value so as to
7083 avoid duplicate warnings on code like `const int I = NULL;
7085 if (ARITHMETIC_TYPE_P (type
) && rhs
== null_node
)
7086 cp_warning ("converting NULL to non-pointer type");
7088 if (TREE_READONLY_DECL_P (rhs
))
7089 rhs
= decl_constant_value (rhs
);
7094 return convert_for_assignment (type
, rhs
, errtype
, fndecl
, parmnum
);
7097 /* Expand an ASM statement with operands, handling output operands
7098 that are not variables or INDIRECT_REFS by transforming such
7099 cases into cases that expand_asm_operands can handle.
7101 Arguments are same as for expand_asm_operands.
7103 We don't do default conversions on all inputs, because it can screw
7104 up operands that are expected to be in memory. */
7107 c_expand_asm_operands (string
, outputs
, inputs
, clobbers
, vol
, filename
, line
)
7108 tree string
, outputs
, inputs
, clobbers
;
7113 int noutputs
= list_length (outputs
);
7115 /* o[I] is the place that output number I should be written. */
7116 register tree
*o
= (tree
*) alloca (noutputs
* sizeof (tree
));
7119 /* Record the contents of OUTPUTS before it is modified. */
7120 for (i
= 0, tail
= outputs
; tail
; tail
= TREE_CHAIN (tail
), i
++)
7121 o
[i
] = TREE_VALUE (tail
);
7123 /* Generate the ASM_OPERANDS insn;
7124 store into the TREE_VALUEs of OUTPUTS some trees for
7125 where the values were actually stored. */
7126 expand_asm_operands (string
, outputs
, inputs
, clobbers
, vol
, filename
, line
);
7128 /* Copy all the intermediate outputs into the specified outputs. */
7129 for (i
= 0, tail
= outputs
; tail
; tail
= TREE_CHAIN (tail
), i
++)
7131 if (o
[i
] != TREE_VALUE (tail
))
7133 expand_expr (build_modify_expr (o
[i
], NOP_EXPR
, TREE_VALUE (tail
)),
7134 const0_rtx
, VOIDmode
, EXPAND_NORMAL
);
7137 /* Detect modification of read-only values.
7138 (Otherwise done by build_modify_expr.) */
7141 tree type
= TREE_TYPE (o
[i
]);
7142 if (CP_TYPE_CONST_P (type
)
7143 || ((TREE_CODE (type
) == RECORD_TYPE
7144 || TREE_CODE (type
) == UNION_TYPE
)
7145 && C_TYPE_FIELDS_READONLY (type
)))
7146 readonly_error (o
[i
], "modification by `asm'", 1);
7150 /* Those MODIFY_EXPRs could do autoincrements. */
7154 /* Expand a C `return' statement.
7155 RETVAL is the expression for what to return,
7156 or a null pointer for `return;' with no value.
7158 C++: upon seeing a `return', we must call destructors on all
7159 variables in scope which had constructors called on them.
7160 This means that if in a destructor, the base class destructors
7161 must be called before returning.
7163 The RETURN statement in C++ has initialization semantics. */
7166 c_expand_return (retval
)
7169 extern struct nesting
*cond_stack
, *loop_stack
, *case_stack
;
7170 extern tree dtor_label
, ctor_label
;
7171 tree result
= DECL_RESULT (current_function_decl
);
7172 tree valtype
= TREE_TYPE (result
);
7174 if (TREE_THIS_VOLATILE (current_function_decl
))
7175 warning ("function declared `noreturn' has a `return' statement");
7177 if (retval
== error_mark_node
)
7179 current_function_returns_null
= 1;
7183 if (processing_template_decl
)
7185 add_tree (build_min_nt (RETURN_STMT
, retval
));
7192 error ("returning a value from a destructor");
7194 /* Can't just return from a destructor. */
7195 expand_goto (dtor_label
);
7199 if (retval
== NULL_TREE
)
7201 /* A non-named return value does not count. */
7203 if (DECL_CONSTRUCTOR_P (current_function_decl
))
7204 retval
= current_class_ptr
;
7205 else if (DECL_NAME (result
) != NULL_TREE
7206 && TREE_CODE (valtype
) != VOID_TYPE
)
7210 current_function_returns_null
= 1;
7212 if (valtype
!= NULL_TREE
&& TREE_CODE (valtype
) != VOID_TYPE
)
7214 if (DECL_NAME (DECL_RESULT (current_function_decl
)) == NULL_TREE
)
7216 pedwarn ("`return' with no value, in function returning non-void");
7217 /* Clear this, so finish_function won't say that we
7218 reach the end of a non-void function (which we don't,
7219 we gave a return!). */
7220 current_function_returns_null
= 0;
7224 expand_null_return ();
7228 else if (DECL_CONSTRUCTOR_P (current_function_decl
))
7230 if (flag_this_is_variable
)
7231 error ("return from a constructor: use `this = ...' instead");
7233 error ("returning a value from a constructor");
7234 retval
= current_class_ptr
;
7237 /* Effective C++ rule 15. See also start_function. */
7239 && DECL_NAME (current_function_decl
) == ansi_opname
[(int) MODIFY_EXPR
]
7240 && retval
!= current_class_ref
)
7241 cp_warning ("`operator=' should return a reference to `*this'");
7243 if (valtype
== NULL_TREE
|| TREE_CODE (valtype
) == VOID_TYPE
)
7245 current_function_returns_null
= 1;
7246 if (TREE_CODE (TREE_TYPE (retval
)) != VOID_TYPE
)
7247 pedwarn ("`return' with a value, in function returning void");
7248 expand_return (retval
);
7252 /* Now deal with possible C++ hair:
7253 (1) Compute the return value.
7254 (2) If there are aggregate values with destructors which
7255 must be cleaned up, clean them (taking care
7256 not to clobber the return value).
7257 (3) If an X(X&) constructor is defined, the return
7258 value must be returned via that. */
7260 if (retval
== result
7261 || DECL_CONSTRUCTOR_P (current_function_decl
))
7262 /* It's already done for us. */;
7263 else if (TREE_CODE (TREE_TYPE (retval
)) == VOID_TYPE
)
7265 pedwarn ("return of void value in function returning non-void");
7266 expand_expr_stmt (retval
);
7271 tree functype
= TREE_TYPE (TREE_TYPE (current_function_decl
));
7273 /* First convert the value to the function's return type, then
7274 to the type of return value's location to handle the
7275 case that functype is thiner than the valtype. */
7277 retval
= convert_for_initialization
7278 (NULL_TREE
, functype
, retval
, LOOKUP_NORMAL
|LOOKUP_ONLYCONVERTING
,
7279 "return", NULL_TREE
, 0);
7281 retval
= convert (valtype
, retval
);
7283 if (retval
== error_mark_node
)
7285 /* Avoid warning about control reaching end of function. */
7286 expand_null_return ();
7290 /* We can't initialize a register from a AGGR_INIT_EXPR. */
7291 else if (! current_function_returns_struct
7292 && TREE_CODE (retval
) == TARGET_EXPR
7293 && TREE_CODE (TREE_OPERAND (retval
, 1)) == AGGR_INIT_EXPR
)
7294 retval
= build (COMPOUND_EXPR
, TREE_TYPE (retval
), retval
,
7295 TREE_OPERAND (retval
, 0));
7297 /* Add some useful error checking for C++. */
7298 else if (TREE_CODE (valtype
) == REFERENCE_TYPE
)
7300 tree whats_returned
;
7302 /* Sort through common things to see what it is
7303 we are returning. */
7304 whats_returned
= retval
;
7305 if (TREE_CODE (whats_returned
) == COMPOUND_EXPR
)
7307 whats_returned
= TREE_OPERAND (whats_returned
, 1);
7308 if (TREE_CODE (whats_returned
) == ADDR_EXPR
)
7309 whats_returned
= TREE_OPERAND (whats_returned
, 0);
7311 while (TREE_CODE (whats_returned
) == CONVERT_EXPR
7312 || TREE_CODE (whats_returned
) == NOP_EXPR
)
7313 whats_returned
= TREE_OPERAND (whats_returned
, 0);
7314 if (TREE_CODE (whats_returned
) == ADDR_EXPR
)
7316 whats_returned
= TREE_OPERAND (whats_returned
, 0);
7317 while (TREE_CODE (whats_returned
) == AGGR_INIT_EXPR
7318 || TREE_CODE (whats_returned
) == TARGET_EXPR
)
7320 /* Get the target. */
7321 whats_returned
= TREE_OPERAND (whats_returned
, 0);
7322 warning ("returning reference to temporary");
7326 if (TREE_CODE (whats_returned
) == VAR_DECL
&& DECL_NAME (whats_returned
))
7328 if (TEMP_NAME_P (DECL_NAME (whats_returned
)))
7329 warning ("reference to non-lvalue returned");
7330 else if (TREE_CODE (TREE_TYPE (whats_returned
)) != REFERENCE_TYPE
7331 && DECL_FUNCTION_SCOPE_P (whats_returned
)
7332 && !(TREE_STATIC (whats_returned
)
7333 || TREE_PUBLIC (whats_returned
)))
7334 cp_warning_at ("reference to local variable `%D' returned", whats_returned
);
7337 else if (TREE_CODE (retval
) == ADDR_EXPR
)
7339 tree whats_returned
= TREE_OPERAND (retval
, 0);
7341 if (TREE_CODE (whats_returned
) == VAR_DECL
7342 && DECL_NAME (whats_returned
)
7343 && DECL_FUNCTION_SCOPE_P (whats_returned
)
7344 && !(TREE_STATIC (whats_returned
)
7345 || TREE_PUBLIC (whats_returned
)))
7346 cp_warning_at ("address of local variable `%D' returned", whats_returned
);
7350 if (retval
!= NULL_TREE
7351 && TREE_CODE_CLASS (TREE_CODE (retval
)) == 'd'
7352 && cond_stack
== 0 && loop_stack
== 0 && case_stack
== 0)
7353 current_function_return_value
= retval
;
7355 if (ctor_label
&& TREE_CODE (ctor_label
) != ERROR_MARK
)
7357 /* Here RETVAL is CURRENT_CLASS_PTR, so there's nothing to do. */
7358 expand_goto (ctor_label
);
7361 if (retval
&& retval
!= result
)
7363 result
= build (INIT_EXPR
, TREE_TYPE (result
), result
, retval
);
7364 TREE_SIDE_EFFECTS (result
) = 1;
7367 expand_start_target_temps ();
7369 expand_return (result
);
7371 expand_end_target_temps ();
7373 current_function_returns_value
= 1;
7376 /* Start a C switch statement, testing expression EXP.
7377 Return EXP if it is valid, an error node otherwise. */
7380 c_expand_start_case (exp
)
7385 exp
= build_expr_type_conversion (WANT_INT
| WANT_ENUM
, exp
, 1);
7386 if (exp
== NULL_TREE
)
7388 error ("switch quantity not an integer");
7389 exp
= error_mark_node
;
7391 if (exp
== error_mark_node
)
7392 return error_mark_node
;
7394 exp
= default_conversion (exp
);
7395 type
= TREE_TYPE (exp
);
7396 idx
= get_unwidened (exp
, 0);
7397 /* We can't strip a conversion from a signed type to an unsigned,
7398 because if we did, int_fits_type_p would do the wrong thing
7399 when checking case values for being in range,
7400 and it's too hard to do the right thing. */
7401 if (TREE_UNSIGNED (TREE_TYPE (exp
)) == TREE_UNSIGNED (TREE_TYPE (idx
)))
7405 (1, fold (build1 (CLEANUP_POINT_EXPR
, TREE_TYPE (exp
), exp
)),
7406 type
, "switch statement");
7411 /* Returns non-zero if the pointer-type FROM can be converted to the
7412 pointer-type TO via a qualification conversion. If CONSTP is -1,
7413 then we return non-zero if the pointers are similar, and the
7414 cv-qualification signature of FROM is a proper subset of that of TO.
7416 If CONSTP is positive, then all outer pointers have been
7420 comp_ptr_ttypes_real (to
, from
, constp
)
7424 int to_more_cv_qualified
= 0;
7426 for (; ; to
= TREE_TYPE (to
), from
= TREE_TYPE (from
))
7428 if (TREE_CODE (to
) != TREE_CODE (from
))
7431 if (TREE_CODE (from
) == OFFSET_TYPE
7432 && same_type_p (TYPE_OFFSET_BASETYPE (from
),
7433 TYPE_OFFSET_BASETYPE (to
)))
7436 /* Const and volatile mean something different for function types,
7437 so the usual checks are not appropriate. */
7438 if (TREE_CODE (to
) != FUNCTION_TYPE
&& TREE_CODE (to
) != METHOD_TYPE
)
7440 if (!at_least_as_qualified_p (to
, from
))
7443 if (!at_least_as_qualified_p (from
, to
))
7448 ++to_more_cv_qualified
;
7452 constp
&= TYPE_READONLY (to
);
7455 if (TREE_CODE (to
) != POINTER_TYPE
)
7457 same_type_p (TYPE_MAIN_VARIANT (to
), TYPE_MAIN_VARIANT (from
))
7458 && (constp
>= 0 || to_more_cv_qualified
);
7462 /* When comparing, say, char ** to char const **, this function takes the
7463 'char *' and 'char const *'. Do not pass non-pointer types to this
7467 comp_ptr_ttypes (to
, from
)
7470 return comp_ptr_ttypes_real (to
, from
, 1);
7473 /* Returns 1 if to and from are (possibly multi-level) pointers to the same
7474 type or inheritance-related types, regardless of cv-quals. */
7477 ptr_reasonably_similar (to
, from
)
7480 for (; ; to
= TREE_TYPE (to
), from
= TREE_TYPE (from
))
7482 if (TREE_CODE (to
) != TREE_CODE (from
))
7485 if (TREE_CODE (from
) == OFFSET_TYPE
7486 && comptypes (TYPE_OFFSET_BASETYPE (to
),
7487 TYPE_OFFSET_BASETYPE (from
),
7488 COMPARE_BASE
| COMPARE_RELAXED
))
7491 if (TREE_CODE (to
) != POINTER_TYPE
)
7493 (TYPE_MAIN_VARIANT (to
), TYPE_MAIN_VARIANT (from
),
7494 COMPARE_BASE
| COMPARE_RELAXED
);
7498 /* Like comp_ptr_ttypes, for const_cast. */
7501 comp_ptr_ttypes_const (to
, from
)
7504 for (; ; to
= TREE_TYPE (to
), from
= TREE_TYPE (from
))
7506 if (TREE_CODE (to
) != TREE_CODE (from
))
7509 if (TREE_CODE (from
) == OFFSET_TYPE
7510 && same_type_p (TYPE_OFFSET_BASETYPE (from
),
7511 TYPE_OFFSET_BASETYPE (to
)))
7514 if (TREE_CODE (to
) != POINTER_TYPE
)
7515 return same_type_p (TYPE_MAIN_VARIANT (to
),
7516 TYPE_MAIN_VARIANT (from
));
7520 /* Like comp_ptr_ttypes, for reinterpret_cast. */
7523 comp_ptr_ttypes_reinterpret (to
, from
)
7528 for (; ; to
= TREE_TYPE (to
), from
= TREE_TYPE (from
))
7530 if (TREE_CODE (from
) == OFFSET_TYPE
)
7531 from
= TREE_TYPE (from
);
7532 if (TREE_CODE (to
) == OFFSET_TYPE
)
7533 to
= TREE_TYPE (to
);
7535 /* Const and volatile mean something different for function types,
7536 so the usual checks are not appropriate. */
7537 if (TREE_CODE (from
) != FUNCTION_TYPE
&& TREE_CODE (from
) != METHOD_TYPE
7538 && TREE_CODE (to
) != FUNCTION_TYPE
&& TREE_CODE (to
) != METHOD_TYPE
)
7540 if (!at_least_as_qualified_p (to
, from
))
7544 && !at_least_as_qualified_p (from
, to
))
7546 constp
&= TYPE_READONLY (to
);
7549 if (TREE_CODE (from
) != POINTER_TYPE
7550 || TREE_CODE (to
) != POINTER_TYPE
)
7555 /* Returns the type-qualifier set corresponding to TYPE. */
7558 cp_type_quals (type
)
7561 while (TREE_CODE (type
) == ARRAY_TYPE
)
7562 type
= TREE_TYPE (type
);
7564 return TYPE_QUALS (type
);
7567 /* Returns non-zero if the TYPE contains a mutable member */
7570 cp_has_mutable_p (type
)
7573 while (TREE_CODE (type
) == ARRAY_TYPE
)
7574 type
= TREE_TYPE (type
);
7576 return CLASS_TYPE_P (type
) && CLASSTYPE_HAS_MUTABLE (type
);