1 /* Build expressions with type checking for C++ compiler.
2 Copyright (C) 1987, 1988, 1989, 1992, 1993, 1994, 1995, 1996, 1997, 1998,
3 1999, 2000, 2001 Free Software Foundation, Inc.
4 Hacked by Michael Tiemann (tiemann@cygnus.com)
6 This file is part of GNU CC.
8 GNU CC is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2, or (at your option)
13 GNU CC is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with GNU CC; see the file COPYING. If not, write to
20 the Free Software Foundation, 59 Temple Place - Suite 330,
21 Boston, MA 02111-1307, USA. */
24 /* This file is part of the C++ front end.
25 It contains routines to build C++ expressions given their operands,
26 including computing the types of the result, C and C++ specific error
27 checks, and some optimization.
29 There are also routines to build RETURN_STMT nodes and CASE_STMT nodes,
30 and to process initializations in declarations (since they work
31 like a strange sort of assignment). */
43 #include "diagnostic.h"
46 static tree convert_for_assignment
PARAMS ((tree
, tree
, const char *, tree
,
48 static tree pointer_int_sum
PARAMS ((enum tree_code
, tree
, tree
));
49 static tree rationalize_conditional_expr
PARAMS ((enum tree_code
, tree
));
50 static int comp_target_parms
PARAMS ((tree
, tree
));
51 static int comp_ptr_ttypes_real
PARAMS ((tree
, tree
, int));
52 static int comp_ptr_ttypes_const
PARAMS ((tree
, tree
));
53 static int comp_ptr_ttypes_reinterpret
PARAMS ((tree
, tree
));
54 static int comp_except_types
PARAMS ((tree
, tree
, int));
55 static int comp_array_types
PARAMS ((int (*) (tree
, tree
, int), tree
,
57 static tree common_base_type
PARAMS ((tree
, tree
));
58 static tree lookup_anon_field
PARAMS ((tree
, tree
));
59 static tree pointer_diff
PARAMS ((tree
, tree
, tree
));
60 static tree build_component_addr
PARAMS ((tree
, tree
));
61 static tree qualify_type_recursive
PARAMS ((tree
, tree
));
62 static tree get_delta_difference
PARAMS ((tree
, tree
, int));
63 static int comp_cv_target_types
PARAMS ((tree
, tree
, int));
64 static void casts_away_constness_r
PARAMS ((tree
*, tree
*));
65 static int casts_away_constness
PARAMS ((tree
, tree
));
66 static void maybe_warn_about_returning_address_of_local
PARAMS ((tree
));
67 static tree strip_all_pointer_quals
PARAMS ((tree
));
69 /* Return the target type of TYPE, which means return T for:
70 T*, T&, T[], T (...), and otherwise, just T. */
76 if (TREE_CODE (type
) == REFERENCE_TYPE
)
77 type
= TREE_TYPE (type
);
78 while (TREE_CODE (type
) == POINTER_TYPE
79 || TREE_CODE (type
) == ARRAY_TYPE
80 || TREE_CODE (type
) == FUNCTION_TYPE
81 || TREE_CODE (type
) == METHOD_TYPE
82 || TREE_CODE (type
) == OFFSET_TYPE
)
83 type
= TREE_TYPE (type
);
87 /* Do `exp = require_complete_type (exp);' to make sure exp
88 does not have an incomplete type. (That includes void types.)
89 Returns the error_mark_node if the VALUE does not have
90 complete type when this function returns. */
93 require_complete_type (value
)
98 if (processing_template_decl
|| value
== error_mark_node
)
101 if (TREE_CODE (value
) == OVERLOAD
)
102 type
= unknown_type_node
;
104 type
= TREE_TYPE (value
);
106 /* First, detect a valid value with a complete type. */
107 if (COMPLETE_TYPE_P (type
))
110 /* If we see X::Y, we build an OFFSET_TYPE which has
111 not been laid out. Try to avoid an error by interpreting
112 it as this->X::Y, if reasonable. */
113 if (TREE_CODE (value
) == OFFSET_REF
114 && current_class_ref
!= 0
115 && TREE_OPERAND (value
, 0) == current_class_ref
)
117 tree base
, member
= TREE_OPERAND (value
, 1);
118 tree basetype
= TYPE_OFFSET_BASETYPE (type
);
119 my_friendly_assert (TREE_CODE (member
) == FIELD_DECL
, 305);
120 base
= convert_pointer_to (basetype
, current_class_ptr
);
121 value
= build (COMPONENT_REF
, TREE_TYPE (member
),
122 build_indirect_ref (base
, NULL
), member
);
123 return require_complete_type (value
);
126 if (complete_type_or_else (type
, value
))
129 return error_mark_node
;
132 /* Try to complete TYPE, if it is incomplete. For example, if TYPE is
133 a template instantiation, do the instantiation. Returns TYPE,
134 whether or not it could be completed, unless something goes
135 horribly wrong, in which case the error_mark_node is returned. */
141 if (type
== NULL_TREE
)
142 /* Rather than crash, we return something sure to cause an error
144 return error_mark_node
;
146 if (type
== error_mark_node
|| COMPLETE_TYPE_P (type
))
148 else if (TREE_CODE (type
) == ARRAY_TYPE
&& TYPE_DOMAIN (type
))
150 tree t
= complete_type (TREE_TYPE (type
));
151 if (COMPLETE_TYPE_P (t
) && ! processing_template_decl
)
153 TYPE_NEEDS_CONSTRUCTING (type
)
154 = TYPE_NEEDS_CONSTRUCTING (TYPE_MAIN_VARIANT (t
));
155 TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type
)
156 = TYPE_HAS_NONTRIVIAL_DESTRUCTOR (TYPE_MAIN_VARIANT (t
));
158 else if (CLASS_TYPE_P (type
) && CLASSTYPE_TEMPLATE_INSTANTIATION (type
))
159 instantiate_class_template (TYPE_MAIN_VARIANT (type
));
164 /* Like complete_type, but issue an error if the TYPE cannot be
165 completed. VALUE is used for informative diagnostics.
166 Returns NULL_TREE if the type cannot be made complete. */
169 complete_type_or_else (type
, value
)
173 type
= complete_type (type
);
174 if (type
== error_mark_node
)
175 /* We already issued an error. */
177 else if (!COMPLETE_TYPE_P (type
))
179 incomplete_type_error (value
, type
);
186 /* Return truthvalue of whether type of EXP is instantiated. */
192 return (TREE_CODE (exp
) == OVERLOAD
193 || TREE_CODE (exp
) == TREE_LIST
194 || TREE_TYPE (exp
) == unknown_type_node
195 || (TREE_CODE (TREE_TYPE (exp
)) == OFFSET_TYPE
196 && TREE_TYPE (TREE_TYPE (exp
)) == unknown_type_node
));
199 /* Return a pointer or pointer to member type similar to T1, with a
200 cv-qualification signature that is the union of the cv-qualification
201 signatures of T1 and T2: [expr.rel], [expr.eq]. */
204 qualify_type_recursive (t1
, t2
)
207 if ((TYPE_PTR_P (t1
) && TYPE_PTR_P (t2
))
208 || (TYPE_PTRMEM_P (t1
) && TYPE_PTRMEM_P (t2
)))
210 tree tt1
= TREE_TYPE (t1
);
211 tree tt2
= TREE_TYPE (t2
);
215 tree attributes
= (*target
.merge_type_attributes
) (t1
, t2
);
217 if (TREE_CODE (tt1
) == OFFSET_TYPE
)
219 b1
= TYPE_OFFSET_BASETYPE (tt1
);
220 tt1
= TREE_TYPE (tt1
);
221 tt2
= TREE_TYPE (tt2
);
226 type_quals
= (CP_TYPE_QUALS (tt1
) | CP_TYPE_QUALS (tt2
));
227 tgt
= qualify_type_recursive (tt1
, tt2
);
228 tgt
= cp_build_qualified_type (tgt
, type_quals
);
230 tgt
= build_offset_type (b1
, tgt
);
231 t1
= build_pointer_type (tgt
);
232 t1
= build_type_attribute_variant (t1
, attributes
);
237 /* Return the common type of two parameter lists.
238 We assume that comptypes has already been done and returned 1;
239 if that isn't so, this may crash.
241 As an optimization, free the space we allocate if the parameter
242 lists are already common. */
248 tree oldargs
= p1
, newargs
, n
;
252 len
= list_length (p1
);
253 newargs
= tree_last (p1
);
255 if (newargs
== void_list_node
)
264 newargs
= tree_cons (NULL_TREE
, NULL_TREE
, newargs
);
269 p1
= TREE_CHAIN (p1
), p2
= TREE_CHAIN (p2
), n
= TREE_CHAIN (n
), i
++)
271 if (TREE_PURPOSE (p1
) && !TREE_PURPOSE (p2
))
273 TREE_PURPOSE (n
) = TREE_PURPOSE (p1
);
276 else if (! TREE_PURPOSE (p1
))
278 if (TREE_PURPOSE (p2
))
280 TREE_PURPOSE (n
) = TREE_PURPOSE (p2
);
286 if (1 != simple_cst_equal (TREE_PURPOSE (p1
), TREE_PURPOSE (p2
)))
288 TREE_PURPOSE (n
) = TREE_PURPOSE (p2
);
290 if (TREE_VALUE (p1
) != TREE_VALUE (p2
))
293 TREE_VALUE (n
) = common_type (TREE_VALUE (p1
), TREE_VALUE (p2
));
296 TREE_VALUE (n
) = TREE_VALUE (p1
);
304 /* Given a type, perhaps copied for a typedef,
305 find the "original" version of it. */
310 while (TYPE_NAME (t
) != NULL_TREE
)
312 tree x
= TYPE_NAME (t
);
313 if (TREE_CODE (x
) != TYPE_DECL
)
315 x
= DECL_ORIGINAL_TYPE (x
);
323 /* T1 and T2 are arithmetic or enumeration types. Return the type
324 that will result from the "usual arithmetic converions" on T1 and
325 T2 as described in [expr]. */
328 type_after_usual_arithmetic_conversions (t1
, t2
)
332 enum tree_code code1
= TREE_CODE (t1
);
333 enum tree_code code2
= TREE_CODE (t2
);
336 /* FIXME: Attributes. */
337 my_friendly_assert (ARITHMETIC_TYPE_P (t1
)
338 || TREE_CODE (t1
) == ENUMERAL_TYPE
,
340 my_friendly_assert (ARITHMETIC_TYPE_P (t2
)
341 || TREE_CODE (t2
) == ENUMERAL_TYPE
,
344 /* In what follows, we slightly generalize the rules given in [expr]
345 so as to deal with `long long'. First, merge the attributes. */
346 attributes
= (*target
.merge_type_attributes
) (t1
, t2
);
348 /* If only one is real, use it as the result. */
349 if (code1
== REAL_TYPE
&& code2
!= REAL_TYPE
)
350 return build_type_attribute_variant (t1
, attributes
);
351 if (code2
== REAL_TYPE
&& code1
!= REAL_TYPE
)
352 return build_type_attribute_variant (t2
, attributes
);
354 /* Perform the integral promotions. */
355 if (code1
!= REAL_TYPE
)
357 t1
= type_promotes_to (t1
);
358 t2
= type_promotes_to (t2
);
361 /* Both real or both integers; use the one with greater precision. */
362 if (TYPE_PRECISION (t1
) > TYPE_PRECISION (t2
))
363 return build_type_attribute_variant (t1
, attributes
);
364 else if (TYPE_PRECISION (t2
) > TYPE_PRECISION (t1
))
365 return build_type_attribute_variant (t2
, attributes
);
367 if (code1
!= REAL_TYPE
)
369 /* If one is a sizetype, use it so size_binop doesn't blow up. */
370 if (TYPE_IS_SIZETYPE (t1
) > TYPE_IS_SIZETYPE (t2
))
371 return build_type_attribute_variant (t1
, attributes
);
372 if (TYPE_IS_SIZETYPE (t2
) > TYPE_IS_SIZETYPE (t1
))
373 return build_type_attribute_variant (t2
, attributes
);
375 /* If one is unsigned long long, then convert the other to unsigned
377 if (same_type_p (TYPE_MAIN_VARIANT (t1
), long_long_unsigned_type_node
)
378 || same_type_p (TYPE_MAIN_VARIANT (t2
), long_long_unsigned_type_node
))
379 return build_type_attribute_variant (long_long_unsigned_type_node
,
381 /* If one is a long long, and the other is an unsigned long, and
382 long long can represent all the values of an unsigned long, then
383 convert to a long long. Otherwise, convert to an unsigned long
384 long. Otherwise, if either operand is long long, convert the
387 Since we're here, we know the TYPE_PRECISION is the same;
388 therefore converting to long long cannot represent all the values
389 of an unsigned long, so we choose unsigned long long in that
391 if (same_type_p (TYPE_MAIN_VARIANT (t1
), long_long_integer_type_node
)
392 || same_type_p (TYPE_MAIN_VARIANT (t2
), long_long_integer_type_node
))
394 tree t
= ((TREE_UNSIGNED (t1
) || TREE_UNSIGNED (t2
))
395 ? long_long_unsigned_type_node
396 : long_long_integer_type_node
);
397 return build_type_attribute_variant (t
, attributes
);
400 /* Go through the same procedure, but for longs. */
401 if (same_type_p (TYPE_MAIN_VARIANT (t1
), long_unsigned_type_node
)
402 || same_type_p (TYPE_MAIN_VARIANT (t2
), long_unsigned_type_node
))
403 return build_type_attribute_variant (long_unsigned_type_node
,
405 if (same_type_p (TYPE_MAIN_VARIANT (t1
), long_integer_type_node
)
406 || same_type_p (TYPE_MAIN_VARIANT (t2
), long_integer_type_node
))
408 tree t
= ((TREE_UNSIGNED (t1
) || TREE_UNSIGNED (t2
))
409 ? long_unsigned_type_node
: long_integer_type_node
);
410 return build_type_attribute_variant (t
, attributes
);
412 /* Otherwise prefer the unsigned one. */
413 if (TREE_UNSIGNED (t1
))
414 return build_type_attribute_variant (t1
, attributes
);
416 return build_type_attribute_variant (t2
, attributes
);
420 if (same_type_p (TYPE_MAIN_VARIANT (t1
), long_double_type_node
)
421 || same_type_p (TYPE_MAIN_VARIANT (t2
), long_double_type_node
))
422 return build_type_attribute_variant (long_double_type_node
,
424 if (same_type_p (TYPE_MAIN_VARIANT (t1
), double_type_node
)
425 || same_type_p (TYPE_MAIN_VARIANT (t2
), double_type_node
))
426 return build_type_attribute_variant (double_type_node
,
429 return build_type_attribute_variant (float_type_node
,
434 /* Return the composite pointer type (see [expr.rel]) for T1 and T2.
435 ARG1 and ARG2 are the values with those types. The LOCATION is a
436 string describing the current location, in case an error occurs. */
439 composite_pointer_type (t1
, t2
, arg1
, arg2
, location
)
444 const char* location
;
450 If one operand is a null pointer constant, the composite pointer
451 type is the type of the other operand. */
452 if (null_ptr_cst_p (arg1
))
454 if (null_ptr_cst_p (arg2
))
457 /* Deal with pointer-to-member functions in the same way as we deal
458 with pointers to functions. */
459 if (TYPE_PTRMEMFUNC_P (t1
))
460 t1
= TYPE_PTRMEMFUNC_FN_TYPE (t1
);
461 if (TYPE_PTRMEMFUNC_P (t2
))
462 t2
= TYPE_PTRMEMFUNC_FN_TYPE (t2
);
468 If one of the operands has type "pointer to cv1 void*", then
469 the other has type "pointer to cv2T", and the composite pointer
470 type is "pointer to cv12 void", where cv12 is the union of cv1
473 If either type is a pointer to void, make sure it is T1. */
474 if (VOID_TYPE_P (TREE_TYPE (t2
)))
481 /* Now, if T1 is a pointer to void, merge the qualifiers. */
482 if (VOID_TYPE_P (TREE_TYPE (t1
)))
484 if (pedantic
&& TYPE_PTRFN_P (t2
))
485 pedwarn ("ISO C++ forbids %s between pointer of type `void *' and pointer-to-function", location
);
488 result_type
= cp_build_qualified_type (void_type_node
,
490 | CP_TYPE_QUALS (t2
)));
491 result_type
= build_pointer_type (result_type
);
495 tree full1
= qualify_type_recursive (t1
, t2
);
496 tree full2
= qualify_type_recursive (t2
, t1
);
498 int val
= comp_target_types (full1
, full2
, 1);
506 cp_pedwarn ("%s between distinct pointer types `%T' and `%T' lacks a cast",
508 result_type
= ptr_type_node
;
515 /* Return the common type of two types.
516 We assume that comptypes has already been done and returned 1;
517 if that isn't so, this may crash.
519 This is the type for the result of most arithmetic operations
520 if the operands have the given two types.
522 We do not deal with enumeral types here because they have already been
523 converted to integer types. */
529 register enum tree_code code1
;
530 register enum tree_code code2
;
533 /* Save time if the two types are the same. */
536 t1
= original_type (t1
);
537 t2
= original_type (t2
);
541 /* If one type is nonsense, use the other. */
542 if (t1
== error_mark_node
)
544 if (t2
== error_mark_node
)
547 if ((ARITHMETIC_TYPE_P (t1
) || TREE_CODE (t1
) == ENUMERAL_TYPE
)
548 && (ARITHMETIC_TYPE_P (t2
) || TREE_CODE (t2
) == ENUMERAL_TYPE
))
549 return type_after_usual_arithmetic_conversions (t1
, t2
);
551 /* Merge the attributes. */
552 attributes
= (*target
.merge_type_attributes
) (t1
, t2
);
554 /* Treat an enum type as the unsigned integer type of the same width. */
556 if (TREE_CODE (t1
) == ENUMERAL_TYPE
)
557 t1
= type_for_size (TYPE_PRECISION (t1
), 1);
558 if (TREE_CODE (t2
) == ENUMERAL_TYPE
)
559 t2
= type_for_size (TYPE_PRECISION (t2
), 1);
561 if (TYPE_PTRMEMFUNC_P (t1
))
562 t1
= TYPE_PTRMEMFUNC_FN_TYPE (t1
);
563 if (TYPE_PTRMEMFUNC_P (t2
))
564 t2
= TYPE_PTRMEMFUNC_FN_TYPE (t2
);
566 code1
= TREE_CODE (t1
);
567 code2
= TREE_CODE (t2
);
569 /* If one type is complex, form the common type of the non-complex
570 components, then make that complex. Use T1 or T2 if it is the
572 if (code1
== COMPLEX_TYPE
|| code2
== COMPLEX_TYPE
)
574 tree subtype1
= code1
== COMPLEX_TYPE
? TREE_TYPE (t1
) : t1
;
575 tree subtype2
= code2
== COMPLEX_TYPE
? TREE_TYPE (t2
) : t2
;
576 tree subtype
= common_type (subtype1
, subtype2
);
578 if (code1
== COMPLEX_TYPE
&& TREE_TYPE (t1
) == subtype
)
579 return build_type_attribute_variant (t1
, attributes
);
580 else if (code2
== COMPLEX_TYPE
&& TREE_TYPE (t2
) == subtype
)
581 return build_type_attribute_variant (t2
, attributes
);
583 return build_type_attribute_variant (build_complex_type (subtype
),
591 /* We should have called type_after_usual_arithmetic_conversions
593 my_friendly_abort (19990725);
598 /* For two pointers, do this recursively on the target type,
599 and combine the qualifiers of the two types' targets. */
600 /* This code was turned off; I don't know why.
601 But ISO C++ specifies doing this with the qualifiers.
602 So I turned it on again. */
604 tree tt1
= TREE_TYPE (t1
);
605 tree tt2
= TREE_TYPE (t2
);
610 if (TREE_CODE (tt1
) == OFFSET_TYPE
)
612 b1
= TYPE_OFFSET_BASETYPE (tt1
);
613 b2
= TYPE_OFFSET_BASETYPE (tt2
);
614 tt1
= TREE_TYPE (tt1
);
615 tt2
= TREE_TYPE (tt2
);
620 type_quals
= (CP_TYPE_QUALS (tt1
) | CP_TYPE_QUALS (tt2
));
621 tt1
= TYPE_MAIN_VARIANT (tt1
);
622 tt2
= TYPE_MAIN_VARIANT (tt2
);
626 else if (VOID_TYPE_P (tt1
) || VOID_TYPE_P (tt2
))
627 target
= void_type_node
;
628 else if (tt1
== unknown_type_node
)
630 else if (tt2
== unknown_type_node
)
633 target
= common_type (tt1
, tt2
);
635 target
= cp_build_qualified_type (target
, type_quals
);
639 if (same_type_p (b1
, b2
)
640 || (DERIVED_FROM_P (b1
, b2
) && binfo_or_else (b1
, b2
)))
641 target
= build_offset_type (b2
, target
);
642 else if (binfo_or_else (b2
, b1
))
643 target
= build_offset_type (b1
, target
);
646 if (code1
== POINTER_TYPE
)
647 t1
= build_pointer_type (target
);
649 t1
= build_reference_type (target
);
650 t1
= build_type_attribute_variant (t1
, attributes
);
652 if (TREE_CODE (target
) == METHOD_TYPE
)
653 t1
= build_ptrmemfunc_type (t1
);
660 tree elt
= common_type (TREE_TYPE (t1
), TREE_TYPE (t2
));
661 /* Save space: see if the result is identical to one of the args. */
662 if (elt
== TREE_TYPE (t1
) && TYPE_DOMAIN (t1
))
663 return build_type_attribute_variant (t1
, attributes
);
664 if (elt
== TREE_TYPE (t2
) && TYPE_DOMAIN (t2
))
665 return build_type_attribute_variant (t2
, attributes
);
666 /* Merge the element types, and have a size if either arg has one. */
667 t1
= build_cplus_array_type
668 (elt
, TYPE_DOMAIN (TYPE_DOMAIN (t1
) ? t1
: t2
));
669 return build_type_attribute_variant (t1
, attributes
);
673 /* Function types: prefer the one that specified arg types.
674 If both do, merge the arg types. Also merge the return types. */
676 tree valtype
= common_type (TREE_TYPE (t1
), TREE_TYPE (t2
));
677 tree p1
= TYPE_ARG_TYPES (t1
);
678 tree p2
= TYPE_ARG_TYPES (t2
);
681 /* Save space: see if the result is identical to one of the args. */
682 if (valtype
== TREE_TYPE (t1
) && ! p2
)
683 return build_type_attribute_variant (t1
, attributes
);
684 if (valtype
== TREE_TYPE (t2
) && ! p1
)
685 return build_type_attribute_variant (t2
, attributes
);
687 /* Simple way if one arg fails to specify argument types. */
688 if (p1
== NULL_TREE
|| TREE_VALUE (p1
) == void_type_node
)
690 rval
= build_function_type (valtype
, p2
);
691 if ((raises
= TYPE_RAISES_EXCEPTIONS (t2
)))
692 rval
= build_exception_variant (rval
, raises
);
693 return build_type_attribute_variant (rval
, attributes
);
695 raises
= TYPE_RAISES_EXCEPTIONS (t1
);
696 if (p2
== NULL_TREE
|| TREE_VALUE (p2
) == void_type_node
)
698 rval
= build_function_type (valtype
, p1
);
700 rval
= build_exception_variant (rval
, raises
);
701 return build_type_attribute_variant (rval
, attributes
);
704 rval
= build_function_type (valtype
, commonparms (p1
, p2
));
705 rval
= build_exception_variant (rval
, raises
);
706 return build_type_attribute_variant (rval
, attributes
);
711 t1
= TYPE_MAIN_VARIANT (t1
);
712 t2
= TYPE_MAIN_VARIANT (t2
);
714 if (DERIVED_FROM_P (t1
, t2
) && binfo_or_else (t1
, t2
))
715 return build_type_attribute_variant (t1
, attributes
);
716 else if (binfo_or_else (t2
, t1
))
717 return build_type_attribute_variant (t2
, attributes
);
720 compiler_error ("common_type called with uncommon aggregate types");
721 return error_mark_node
;
725 if (TREE_CODE (TREE_TYPE (t1
)) == TREE_CODE (TREE_TYPE (t2
)))
727 /* Get this value the long way, since TYPE_METHOD_BASETYPE
728 is just the main variant of this. */
732 tree b1
= TYPE_OFFSET_BASETYPE (t1
);
733 tree b2
= TYPE_OFFSET_BASETYPE (t2
);
735 if (same_type_p (b1
, b2
)
736 || (DERIVED_FROM_P (b1
, b2
) && binfo_or_else (b1
, b2
)))
737 basetype
= TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (t2
)));
740 if (binfo_or_else (b2
, b1
) == NULL_TREE
)
741 compiler_error ("common_type called with uncommon method types");
742 basetype
= TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (t1
)));
745 raises
= TYPE_RAISES_EXCEPTIONS (t1
);
747 /* If this was a member function type, get back to the
748 original type of type member function (i.e., without
749 the class instance variable up front. */
750 t1
= build_function_type (TREE_TYPE (t1
),
751 TREE_CHAIN (TYPE_ARG_TYPES (t1
)));
752 t2
= build_function_type (TREE_TYPE (t2
),
753 TREE_CHAIN (TYPE_ARG_TYPES (t2
)));
754 t3
= common_type (t1
, t2
);
755 t3
= build_cplus_method_type (basetype
, TREE_TYPE (t3
),
756 TYPE_ARG_TYPES (t3
));
757 t1
= build_exception_variant (t3
, raises
);
760 compiler_error ("common_type called with uncommon method types");
762 return build_type_attribute_variant (t1
, attributes
);
765 /* Pointers to members should now be handled by the POINTER_TYPE
767 my_friendly_abort (990325);
770 return build_type_attribute_variant (t1
, attributes
);
774 /* Compare two exception specifier types for exactness or subsetness, if
775 allowed. Returns 0 for mismatch, 1 for same, 2 if B is allowed by A.
777 [except.spec] "If a class X ... objects of class X or any class publicly
778 and unambigously derrived from X. Similarly, if a pointer type Y * ...
779 exceptions of type Y * or that are pointers to any type publicly and
780 unambigously derrived from Y. Otherwise a function only allows exceptions
781 that have the same type ..."
782 This does not mention cv qualifiers and is different to what throw
783 [except.throw] and catch [except.catch] will do. They will ignore the
784 top level cv qualifiers, and allow qualifiers in the pointer to class
787 We implement the letter of the standard. */
790 comp_except_types (a
, b
, exact
)
794 if (same_type_p (a
, b
))
798 if (CP_TYPE_QUALS (a
) || CP_TYPE_QUALS (b
))
801 if (TREE_CODE (a
) == POINTER_TYPE
802 && TREE_CODE (b
) == POINTER_TYPE
)
806 if (CP_TYPE_QUALS (a
) || CP_TYPE_QUALS (b
))
810 if (TREE_CODE (a
) != RECORD_TYPE
811 || TREE_CODE (b
) != RECORD_TYPE
)
814 if (ACCESSIBLY_UNIQUELY_DERIVED_P (a
, b
))
820 /* Return 1 if TYPE1 and TYPE2 are equivalent exception specifiers.
821 If EXACT is 0, T2 can be a subset of T1 (according to 15.4/7),
822 otherwise it must be exact. Exception lists are unordered, but
823 we've already filtered out duplicates. Most lists will be in order,
824 we should try to make use of that. */
827 comp_except_specs (t1
, t2
, exact
)
838 if (t1
== NULL_TREE
) /* T1 is ... */
839 return t2
== NULL_TREE
|| !exact
;
840 if (!TREE_VALUE (t1
)) /* t1 is EMPTY */
841 return t2
!= NULL_TREE
&& !TREE_VALUE (t2
);
842 if (t2
== NULL_TREE
) /* T2 is ... */
844 if (TREE_VALUE(t1
) && !TREE_VALUE (t2
)) /* T2 is EMPTY, T1 is not */
847 /* Neither set is ... or EMPTY, make sure each part of T2 is in T1.
848 Count how many we find, to determine exactness. For exact matching and
849 ordered T1, T2, this is an O(n) operation, otherwise its worst case is
851 for (base
= t1
; t2
!= NULL_TREE
; t2
= TREE_CHAIN (t2
))
853 for (probe
= base
; probe
!= NULL_TREE
; probe
= TREE_CHAIN (probe
))
855 tree a
= TREE_VALUE (probe
);
856 tree b
= TREE_VALUE (t2
);
858 if (comp_except_types (a
, b
, exact
))
860 if (probe
== base
&& exact
)
861 base
= TREE_CHAIN (probe
);
866 if (probe
== NULL_TREE
)
869 return !exact
|| base
== NULL_TREE
|| length
== list_length (t1
);
872 /* Compare the array types T1 and T2, using CMP as the type comparison
873 function for the element types. STRICT is as for comptypes. */
876 comp_array_types (cmp
, t1
, t2
, strict
)
877 register int (*cmp
) PARAMS ((tree
, tree
, int));
887 /* The type of the array elements must be the same. */
888 if (!(TREE_TYPE (t1
) == TREE_TYPE (t2
)
889 || (*cmp
) (TREE_TYPE (t1
), TREE_TYPE (t2
),
890 strict
& ~COMPARE_REDECLARATION
)))
893 d1
= TYPE_DOMAIN (t1
);
894 d2
= TYPE_DOMAIN (t2
);
899 /* If one of the arrays is dimensionless, and the other has a
900 dimension, they are of different types. However, it is legal to
908 declarations for an array object can specify
909 array types that differ by the presence or absence of a major
910 array bound (_dcl.array_). */
912 return strict
& COMPARE_REDECLARATION
;
914 /* Check that the dimensions are the same. */
915 return (cp_tree_equal (TYPE_MIN_VALUE (d1
),
917 && cp_tree_equal (TYPE_MAX_VALUE (d1
),
918 TYPE_MAX_VALUE (d2
)));
921 /* Return 1 if T1 and T2 are compatible types for assignment or
922 various other operations. STRICT is a bitwise-or of the COMPARE_*
926 comptypes (t1
, t2
, strict
)
932 int orig_strict
= strict
;
934 /* The special exemption for redeclaring array types without an
935 array bound only applies at the top level:
940 is not legal, for example. */
941 strict
&= ~COMPARE_REDECLARATION
;
943 /* Suppress errors caused by previously reported errors */
947 /* This should never happen. */
948 my_friendly_assert (t1
!= error_mark_node
, 307);
950 if (t2
== error_mark_node
)
953 /* If either type is the internal version of sizetype, return the
955 if (TREE_CODE (t1
) == INTEGER_TYPE
&& TYPE_IS_SIZETYPE (t1
)
956 && TYPE_DOMAIN (t1
) != 0)
957 t1
= TYPE_DOMAIN (t1
);
959 if (TREE_CODE (t2
) == INTEGER_TYPE
&& TYPE_IS_SIZETYPE (t2
)
960 && TYPE_DOMAIN (t2
) != 0)
961 t2
= TYPE_DOMAIN (t2
);
963 if (strict
& COMPARE_RELAXED
)
965 /* Treat an enum type as the unsigned integer type of the same width. */
967 if (TREE_CODE (t1
) == ENUMERAL_TYPE
)
968 t1
= type_for_size (TYPE_PRECISION (t1
), 1);
969 if (TREE_CODE (t2
) == ENUMERAL_TYPE
)
970 t2
= type_for_size (TYPE_PRECISION (t2
), 1);
976 if (TYPE_PTRMEMFUNC_P (t1
))
977 t1
= TYPE_PTRMEMFUNC_FN_TYPE (t1
);
978 if (TYPE_PTRMEMFUNC_P (t2
))
979 t2
= TYPE_PTRMEMFUNC_FN_TYPE (t2
);
981 /* Different classes of types can't be compatible. */
982 if (TREE_CODE (t1
) != TREE_CODE (t2
))
985 /* Qualifiers must match. */
986 if (CP_TYPE_QUALS (t1
) != CP_TYPE_QUALS (t2
))
988 if (strict
== COMPARE_STRICT
989 && TYPE_FOR_JAVA (t1
) != TYPE_FOR_JAVA (t2
))
992 /* Allow for two different type nodes which have essentially the same
993 definition. Note that we already checked for equality of the type
994 qualifiers (just above). */
996 if (TYPE_MAIN_VARIANT (t1
) == TYPE_MAIN_VARIANT (t2
))
999 /* ??? COMP_TYPE_ATTRIBUTES is currently useless for variables as each
1000 attribute is its own main variant (`val' will remain 0). */
1001 #ifndef COMP_TYPE_ATTRIBUTES
1002 #define COMP_TYPE_ATTRIBUTES(t1,t2) 1
1005 if (strict
& COMPARE_NO_ATTRIBUTES
)
1007 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
1008 else if (! (attrval
= COMP_TYPE_ATTRIBUTES (t1
, t2
)))
1011 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
1014 switch (TREE_CODE (t1
))
1016 case TEMPLATE_TEMPLATE_PARM
:
1017 case BOUND_TEMPLATE_TEMPLATE_PARM
:
1018 if (TEMPLATE_TYPE_IDX (t1
) != TEMPLATE_TYPE_IDX (t2
)
1019 || TEMPLATE_TYPE_LEVEL (t1
) != TEMPLATE_TYPE_LEVEL (t2
))
1021 if (! comp_template_parms
1022 (DECL_TEMPLATE_PARMS (TEMPLATE_TEMPLATE_PARM_TEMPLATE_DECL (t1
)),
1023 DECL_TEMPLATE_PARMS (TEMPLATE_TEMPLATE_PARM_TEMPLATE_DECL (t2
))))
1025 if (TREE_CODE (t1
) == TEMPLATE_TEMPLATE_PARM
)
1027 /* Don't check inheritance. */
1028 strict
= COMPARE_STRICT
;
1033 if (TYPE_TEMPLATE_INFO (t1
) && TYPE_TEMPLATE_INFO (t2
)
1034 && (TYPE_TI_TEMPLATE (t1
) == TYPE_TI_TEMPLATE (t2
)
1035 || TREE_CODE (t1
) == BOUND_TEMPLATE_TEMPLATE_PARM
))
1036 val
= comp_template_args (TYPE_TI_ARGS (t1
),
1039 if ((strict
& COMPARE_BASE
) && DERIVED_FROM_P (t1
, t2
))
1041 else if ((strict
& COMPARE_RELAXED
) && DERIVED_FROM_P (t2
, t1
))
1046 val
= (comptypes (build_pointer_type (TYPE_OFFSET_BASETYPE (t1
)),
1047 build_pointer_type (TYPE_OFFSET_BASETYPE (t2
)), strict
)
1048 && comptypes (TREE_TYPE (t1
), TREE_TYPE (t2
), strict
));
1052 if (! comp_except_specs (TYPE_RAISES_EXCEPTIONS (t1
),
1053 TYPE_RAISES_EXCEPTIONS (t2
), 1))
1056 /* This case is anti-symmetrical!
1057 One can pass a base member (or member function)
1058 to something expecting a derived member (or member function),
1059 but not vice-versa! */
1061 val
= (comptypes (TREE_TYPE (t1
), TREE_TYPE (t2
), strict
)
1062 && compparms (TYPE_ARG_TYPES (t1
), TYPE_ARG_TYPES (t2
)));
1066 case REFERENCE_TYPE
:
1067 t1
= TREE_TYPE (t1
);
1068 t2
= TREE_TYPE (t2
);
1069 /* first, check whether the referred types match with the
1070 required level of strictness */
1071 val
= comptypes (t1
, t2
, strict
);
1074 if (TREE_CODE (t1
) == RECORD_TYPE
1075 && TREE_CODE (t2
) == RECORD_TYPE
)
1080 if (! comp_except_specs (TYPE_RAISES_EXCEPTIONS (t1
),
1081 TYPE_RAISES_EXCEPTIONS (t2
), 1))
1084 val
= ((TREE_TYPE (t1
) == TREE_TYPE (t2
)
1085 || comptypes (TREE_TYPE (t1
), TREE_TYPE (t2
), strict
))
1086 && compparms (TYPE_ARG_TYPES (t1
), TYPE_ARG_TYPES (t2
)));
1090 /* Target types must match incl. qualifiers. We use ORIG_STRICT
1091 here since this is the one place where
1092 COMPARE_REDECLARATION should be used. */
1093 val
= comp_array_types (comptypes
, t1
, t2
, orig_strict
);
1096 case TEMPLATE_TYPE_PARM
:
1097 return TEMPLATE_TYPE_IDX (t1
) == TEMPLATE_TYPE_IDX (t2
)
1098 && TEMPLATE_TYPE_LEVEL (t1
) == TEMPLATE_TYPE_LEVEL (t2
);
1101 if (cp_tree_equal (TYPENAME_TYPE_FULLNAME (t1
),
1102 TYPENAME_TYPE_FULLNAME (t2
)) < 1)
1104 return same_type_p (TYPE_CONTEXT (t1
), TYPE_CONTEXT (t2
));
1107 return same_type_p (TREE_TYPE (t1
), TREE_TYPE (t2
));
1112 return attrval
== 2 && val
== 1 ? 2 : val
;
1115 /* Subroutine of comp_target-types. Make sure that the cv-quals change
1116 only in the same direction as the target type. */
1119 comp_cv_target_types (ttl
, ttr
, nptrs
)
1125 if (!at_least_as_qualified_p (ttl
, ttr
)
1126 && !at_least_as_qualified_p (ttr
, ttl
))
1127 /* The qualifications are incomparable. */
1130 if (TYPE_MAIN_VARIANT (ttl
) == TYPE_MAIN_VARIANT (ttr
))
1131 return more_qualified_p (ttr
, ttl
) ? -1 : 1;
1133 t
= comp_target_types (ttl
, ttr
, nptrs
);
1134 if ((t
== 1 && at_least_as_qualified_p (ttl
, ttr
))
1135 || (t
== -1 && at_least_as_qualified_p (ttr
, ttl
)))
1141 /* Return 1 or -1 if TTL and TTR are pointers to types that are equivalent,
1142 ignoring their qualifiers, 0 if not. Return 1 means that TTR can be
1143 converted to TTL. Return -1 means that TTL can be converted to TTR but
1146 NPTRS is the number of pointers we can strip off and keep cool.
1147 This is used to permit (for aggr A, aggr B) A, B* to convert to A*,
1148 but to not permit B** to convert to A**.
1150 This should go away. Callers should use can_convert or something
1151 similar instead. (jason 17 Apr 1997) */
1154 comp_target_types (ttl
, ttr
, nptrs
)
1158 ttl
= TYPE_MAIN_VARIANT (ttl
);
1159 ttr
= TYPE_MAIN_VARIANT (ttr
);
1160 if (same_type_p (ttl
, ttr
))
1163 if (TREE_CODE (ttr
) != TREE_CODE (ttl
))
1166 if ((TREE_CODE (ttr
) == POINTER_TYPE
1167 || TREE_CODE (ttr
) == REFERENCE_TYPE
)
1168 /* If we get a pointer with nptrs == 0, we don't allow any tweaking
1169 of the type pointed to. This is necessary for reference init
1170 semantics. We won't get here from a previous call with nptrs == 1;
1171 for multi-level pointers we end up in comp_ptr_ttypes. */
1174 int is_ptr
= TREE_CODE (ttr
) == POINTER_TYPE
;
1176 ttl
= TREE_TYPE (ttl
);
1177 ttr
= TREE_TYPE (ttr
);
1181 if (TREE_CODE (ttl
) == UNKNOWN_TYPE
1182 || TREE_CODE (ttr
) == UNKNOWN_TYPE
)
1184 else if (TREE_CODE (ttl
) == VOID_TYPE
1185 && TREE_CODE (ttr
) != FUNCTION_TYPE
1186 && TREE_CODE (ttr
) != METHOD_TYPE
1187 && TREE_CODE (ttr
) != OFFSET_TYPE
)
1189 else if (TREE_CODE (ttr
) == VOID_TYPE
1190 && TREE_CODE (ttl
) != FUNCTION_TYPE
1191 && TREE_CODE (ttl
) != METHOD_TYPE
1192 && TREE_CODE (ttl
) != OFFSET_TYPE
)
1194 else if (TREE_CODE (ttl
) == POINTER_TYPE
1195 || TREE_CODE (ttl
) == ARRAY_TYPE
)
1197 if (comp_ptr_ttypes (ttl
, ttr
))
1199 else if (comp_ptr_ttypes (ttr
, ttl
))
1205 /* Const and volatile mean something different for function types,
1206 so the usual checks are not appropriate. */
1207 if (TREE_CODE (ttl
) == FUNCTION_TYPE
|| TREE_CODE (ttl
) == METHOD_TYPE
)
1208 return comp_target_types (ttl
, ttr
, nptrs
- 1);
1210 return comp_cv_target_types (ttl
, ttr
, nptrs
- 1);
1213 if (TREE_CODE (ttr
) == ARRAY_TYPE
)
1214 return comp_array_types (comp_target_types
, ttl
, ttr
, COMPARE_STRICT
);
1215 else if (TREE_CODE (ttr
) == FUNCTION_TYPE
|| TREE_CODE (ttr
) == METHOD_TYPE
)
1222 if (!same_type_p (TREE_TYPE (ttl
), TREE_TYPE (ttr
)))
1227 switch (comp_target_types (TREE_TYPE (ttl
), TREE_TYPE (ttr
), -1))
1236 argsl
= TYPE_ARG_TYPES (ttl
);
1237 argsr
= TYPE_ARG_TYPES (ttr
);
1239 /* Compare 'this' here, not in comp_target_parms. */
1240 if (TREE_CODE (ttr
) == METHOD_TYPE
)
1242 tree tl
= TYPE_METHOD_BASETYPE (ttl
);
1243 tree tr
= TYPE_METHOD_BASETYPE (ttr
);
1245 if (!same_or_base_type_p (tr
, tl
))
1247 if (same_or_base_type_p (tl
, tr
))
1253 argsl
= TREE_CHAIN (argsl
);
1254 argsr
= TREE_CHAIN (argsr
);
1257 switch (comp_target_parms (argsl
, argsr
))
1265 return saw_contra
? -1 : 1;
1268 else if (TREE_CODE (ttr
) == OFFSET_TYPE
)
1272 /* Contravariance: we can assign a pointer to base member to a pointer
1273 to derived member. Note difference from simple pointer case, where
1274 we can pass a pointer to derived to a pointer to base. */
1275 if (same_or_base_type_p (TYPE_OFFSET_BASETYPE (ttr
),
1276 TYPE_OFFSET_BASETYPE (ttl
)))
1278 else if (same_or_base_type_p (TYPE_OFFSET_BASETYPE (ttl
),
1279 TYPE_OFFSET_BASETYPE (ttr
)))
1289 ttl
= TREE_TYPE (ttl
);
1290 ttr
= TREE_TYPE (ttr
);
1292 if (TREE_CODE (ttl
) == POINTER_TYPE
1293 || TREE_CODE (ttl
) == ARRAY_TYPE
)
1295 if (comp_ptr_ttypes (ttl
, ttr
))
1301 if (comp_cv_target_types (ttl
, ttr
, nptrs
) == 1)
1306 else if (IS_AGGR_TYPE (ttl
))
1310 if (same_or_base_type_p (build_pointer_type (ttl
),
1311 build_pointer_type (ttr
)))
1313 if (same_or_base_type_p (build_pointer_type (ttr
),
1314 build_pointer_type (ttl
)))
1322 /* Returns 1 if TYPE1 is at least as qualified as TYPE2. */
1325 at_least_as_qualified_p (type1
, type2
)
1329 /* All qualifiers for TYPE2 must also appear in TYPE1. */
1330 return ((CP_TYPE_QUALS (type1
) & CP_TYPE_QUALS (type2
))
1331 == CP_TYPE_QUALS (type2
));
1334 /* Returns 1 if TYPE1 is more qualified than TYPE2. */
1337 more_qualified_p (type1
, type2
)
1341 return (CP_TYPE_QUALS (type1
) != CP_TYPE_QUALS (type2
)
1342 && at_least_as_qualified_p (type1
, type2
));
1345 /* Returns 1 if TYPE1 is more cv-qualified than TYPE2, -1 if TYPE2 is
1346 more cv-qualified that TYPE1, and 0 otherwise. */
1349 comp_cv_qualification (type1
, type2
)
1353 if (CP_TYPE_QUALS (type1
) == CP_TYPE_QUALS (type2
))
1356 if (at_least_as_qualified_p (type1
, type2
))
1359 else if (at_least_as_qualified_p (type2
, type1
))
1365 /* Returns 1 if the cv-qualification signature of TYPE1 is a proper
1366 subset of the cv-qualification signature of TYPE2, and the types
1367 are similar. Returns -1 if the other way 'round, and 0 otherwise. */
1370 comp_cv_qual_signature (type1
, type2
)
1374 if (comp_ptr_ttypes_real (type2
, type1
, -1))
1376 else if (comp_ptr_ttypes_real (type1
, type2
, -1))
1382 /* If two types share a common base type, return that basetype.
1383 If there is not a unique most-derived base type, this function
1384 returns ERROR_MARK_NODE. */
1387 common_base_type (tt1
, tt2
)
1390 tree best
= NULL_TREE
;
1393 /* If one is a baseclass of another, that's good enough. */
1394 if (UNIQUELY_DERIVED_FROM_P (tt1
, tt2
))
1396 if (UNIQUELY_DERIVED_FROM_P (tt2
, tt1
))
1399 /* Otherwise, try to find a unique baseclass of TT1
1400 that is shared by TT2, and follow that down. */
1401 for (i
= CLASSTYPE_N_BASECLASSES (tt1
)-1; i
>= 0; i
--)
1403 tree basetype
= TYPE_BINFO_BASETYPE (tt1
, i
);
1404 tree trial
= common_base_type (basetype
, tt2
);
1407 if (trial
== error_mark_node
)
1409 if (best
== NULL_TREE
)
1411 else if (best
!= trial
)
1412 return error_mark_node
;
1417 for (i
= CLASSTYPE_N_BASECLASSES (tt2
)-1; i
>= 0; i
--)
1419 tree basetype
= TYPE_BINFO_BASETYPE (tt2
, i
);
1420 tree trial
= common_base_type (tt1
, basetype
);
1423 if (trial
== error_mark_node
)
1425 if (best
== NULL_TREE
)
1427 else if (best
!= trial
)
1428 return error_mark_node
;
1434 /* Subroutines of `comptypes'. */
1436 /* Return 1 if two parameter type lists PARMS1 and PARMS2 are
1437 equivalent in the sense that functions with those parameter types
1438 can have equivalent types. The two lists must be equivalent,
1441 C++: See comment above about TYPE1, TYPE2. */
1444 compparms (parms1
, parms2
)
1445 tree parms1
, parms2
;
1447 register tree t1
= parms1
, t2
= parms2
;
1449 /* An unspecified parmlist matches any specified parmlist
1450 whose argument types don't need default promotions. */
1454 if (t1
== 0 && t2
== 0)
1456 /* If one parmlist is shorter than the other,
1457 they fail to match. */
1458 if (t1
== 0 || t2
== 0)
1460 if (!same_type_p (TREE_VALUE (t2
), TREE_VALUE (t1
)))
1463 t1
= TREE_CHAIN (t1
);
1464 t2
= TREE_CHAIN (t2
);
1468 /* This really wants return whether or not parameter type lists
1469 would make their owning functions assignment compatible or not.
1471 The return value is like for comp_target_types.
1473 This should go away, possibly with the exception of the empty parmlist
1474 conversion; there are no conversions between function types in C++.
1475 (jason 17 Apr 1997) */
1478 comp_target_parms (parms1
, parms2
)
1479 tree parms1
, parms2
;
1481 register tree t1
= parms1
, t2
= parms2
;
1482 int warn_contravariance
= 0;
1484 /* In C, an unspecified parmlist matches any specified parmlist
1485 whose argument types don't need default promotions. This is not
1486 true for C++, but let's do it anyway for unfixed headers. */
1488 if (t1
== 0 && t2
!= 0)
1490 cp_pedwarn ("ISO C++ prohibits conversion from `%#T' to `(...)'",
1492 return self_promoting_args_p (t2
);
1495 return self_promoting_args_p (t1
);
1497 for (; t1
|| t2
; t1
= TREE_CHAIN (t1
), t2
= TREE_CHAIN (t2
))
1501 /* If one parmlist is shorter than the other,
1502 they fail to match, unless STRICT is <= 0. */
1503 if (t1
== 0 || t2
== 0)
1505 p1
= TREE_VALUE (t1
);
1506 p2
= TREE_VALUE (t2
);
1507 if (same_type_p (p1
, p2
))
1513 if ((TREE_CODE (p1
) == POINTER_TYPE
&& TREE_CODE (p2
) == POINTER_TYPE
)
1514 || (TREE_CODE (p1
) == REFERENCE_TYPE
1515 && TREE_CODE (p2
) == REFERENCE_TYPE
))
1517 /* The following is wrong for contravariance,
1518 but many programs depend on it. */
1519 if (TREE_TYPE (p1
) == void_type_node
)
1521 if (TREE_TYPE (p2
) == void_type_node
)
1523 warn_contravariance
= 1;
1526 if (IS_AGGR_TYPE (TREE_TYPE (p1
))
1527 && !same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (p1
),
1531 /* Note backwards order due to contravariance. */
1532 if (comp_target_types (p2
, p1
, 1) <= 0)
1534 if (comp_target_types (p1
, p2
, 1) > 0)
1536 warn_contravariance
= 1;
1542 return warn_contravariance
? -1 : 1;
1545 /* Compute the value of the `sizeof' operator. */
1551 enum tree_code code
= TREE_CODE (type
);
1554 if (processing_template_decl
)
1555 return build_min (SIZEOF_EXPR
, sizetype
, type
);
1557 if (code
== FUNCTION_TYPE
)
1559 if (pedantic
|| warn_pointer_arith
)
1560 pedwarn ("ISO C++ forbids applying `sizeof' to a function type");
1561 size
= size_one_node
;
1563 else if (code
== METHOD_TYPE
)
1565 if (pedantic
|| warn_pointer_arith
)
1566 pedwarn ("ISO C++ forbids applying `sizeof' to a member function");
1567 size
= size_one_node
;
1569 else if (code
== VOID_TYPE
)
1571 if (pedantic
|| warn_pointer_arith
)
1572 pedwarn ("ISO C++ forbids applying `sizeof' to type `void' which is an incomplete type");
1573 size
= size_one_node
;
1575 else if (code
== ERROR_MARK
)
1576 size
= size_one_node
;
1579 /* ARM $5.3.2: ``When applied to a reference, the result is the
1580 size of the referenced object.'' */
1581 if (code
== REFERENCE_TYPE
)
1582 type
= TREE_TYPE (type
);
1584 if (code
== OFFSET_TYPE
)
1586 cp_error ("`sizeof' applied to non-static member");
1587 size
= size_zero_node
;
1589 else if (!COMPLETE_TYPE_P (complete_type (type
)))
1591 cp_error ("`sizeof' applied to incomplete type `%T'", type
);
1592 size
= size_zero_node
;
1595 /* Convert in case a char is more than one unit. */
1596 size
= size_binop (CEIL_DIV_EXPR
, TYPE_SIZE_UNIT (type
),
1597 size_int (TYPE_PRECISION (char_type_node
)
1601 /* SIZE will have an integer type with TYPE_IS_SIZETYPE set.
1602 TYPE_IS_SIZETYPE means that certain things (like overflow) will
1603 never happen. However, this node should really have type
1604 `size_t', which is just a typedef for an ordinary integer type. */
1605 size
= fold (build1 (NOP_EXPR
, c_size_type_node
, size
));
1606 my_friendly_assert (!TYPE_IS_SIZETYPE (TREE_TYPE (size
)),
1616 if (processing_template_decl
)
1617 return build_min (SIZEOF_EXPR
, sizetype
, e
);
1619 if (TREE_CODE (e
) == COMPONENT_REF
1620 && DECL_C_BIT_FIELD (TREE_OPERAND (e
, 1)))
1621 error ("sizeof applied to a bit-field");
1622 if (is_overloaded_fn (e
))
1624 pedwarn ("ISO C++ forbids applying `sizeof' to an expression of function type");
1625 return c_sizeof (char_type_node
);
1627 else if (type_unknown_p (e
))
1629 incomplete_type_error (e
, TREE_TYPE (e
));
1630 return c_sizeof (char_type_node
);
1632 /* It's illegal to say `sizeof (X::i)' for `i' a non-static data
1633 member unless you're in a non-static member of X. So hand off to
1634 resolve_offset_ref. [expr.prim] */
1635 else if (TREE_CODE (e
) == OFFSET_REF
)
1636 e
= resolve_offset_ref (e
);
1638 if (e
== error_mark_node
)
1641 return c_sizeof (TREE_TYPE (e
));
1645 c_sizeof_nowarn (type
)
1648 enum tree_code code
= TREE_CODE (type
);
1651 if (code
== FUNCTION_TYPE
1652 || code
== METHOD_TYPE
1653 || code
== VOID_TYPE
1654 || code
== ERROR_MARK
)
1655 size
= size_one_node
;
1658 if (code
== REFERENCE_TYPE
)
1659 type
= TREE_TYPE (type
);
1661 if (!COMPLETE_TYPE_P (type
))
1662 size
= size_zero_node
;
1664 /* Convert in case a char is more than one unit. */
1665 size
= size_binop (CEIL_DIV_EXPR
, TYPE_SIZE_UNIT (type
),
1666 size_int (TYPE_PRECISION (char_type_node
)
1670 /* SIZE will have an integer type with TYPE_IS_SIZETYPE set.
1671 TYPE_IS_SIZETYPE means that certain things (like overflow) will
1672 never happen. However, this node should really have type
1673 `size_t', which is just a typedef for an ordinary integer type. */
1674 size
= fold (build1 (NOP_EXPR
, c_size_type_node
, size
));
1675 my_friendly_assert (!TYPE_IS_SIZETYPE (TREE_TYPE (size
)),
1680 /* Implement the __alignof keyword: Return the minimum required
1681 alignment of TYPE, measured in bytes. */
1687 enum tree_code code
= TREE_CODE (type
);
1690 if (processing_template_decl
)
1691 return build_min (ALIGNOF_EXPR
, sizetype
, type
);
1693 if (code
== FUNCTION_TYPE
|| code
== METHOD_TYPE
)
1694 t
= size_int (FUNCTION_BOUNDARY
/ BITS_PER_UNIT
);
1695 else if (code
== VOID_TYPE
|| code
== ERROR_MARK
)
1699 /* Similar to sizeof, __alignof applies to the referant. */
1700 if (code
== REFERENCE_TYPE
)
1701 type
= TREE_TYPE (type
);
1703 t
= size_int (TYPE_ALIGN (type
) / BITS_PER_UNIT
);
1706 return fold (build1 (NOP_EXPR
, c_size_type_node
, t
));
1709 /* Perform the array-to-pointer and function-to-pointer conversions
1712 In addition, references are converted to lvalues and manifest
1713 constants are replaced by their values. */
1716 decay_conversion (exp
)
1720 register enum tree_code code
;
1722 if (TREE_CODE (exp
) == OFFSET_REF
)
1723 exp
= resolve_offset_ref (exp
);
1725 type
= TREE_TYPE (exp
);
1726 code
= TREE_CODE (type
);
1728 if (code
== REFERENCE_TYPE
)
1730 exp
= convert_from_reference (exp
);
1731 type
= TREE_TYPE (exp
);
1732 code
= TREE_CODE (type
);
1735 if (type
== error_mark_node
)
1736 return error_mark_node
;
1738 /* Constants can be used directly unless they're not loadable. */
1739 if (TREE_CODE (exp
) == CONST_DECL
)
1740 exp
= DECL_INITIAL (exp
);
1741 /* Replace a nonvolatile const static variable with its value. We
1742 don't do this for arrays, though; we want the address of the
1743 first element of the array, not the address of the first element
1744 of its initializing constant. */
1745 else if (code
!= ARRAY_TYPE
)
1747 exp
= decl_constant_value (exp
);
1748 type
= TREE_TYPE (exp
);
1751 /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
1752 Leave such NOP_EXPRs, since RHS is being used in non-lvalue context. */
1754 if (code
== VOID_TYPE
)
1756 error ("void value not ignored as it ought to be");
1757 return error_mark_node
;
1759 if (code
== METHOD_TYPE
)
1760 my_friendly_abort (990506);
1761 if (code
== FUNCTION_TYPE
|| is_overloaded_fn (exp
))
1762 return build_unary_op (ADDR_EXPR
, exp
, 0);
1763 if (code
== ARRAY_TYPE
)
1768 if (TREE_CODE (exp
) == INDIRECT_REF
)
1770 /* Stripping away the INDIRECT_REF is not the right
1771 thing to do for references... */
1772 tree inner
= TREE_OPERAND (exp
, 0);
1773 if (TREE_CODE (TREE_TYPE (inner
)) == REFERENCE_TYPE
)
1775 inner
= build1 (CONVERT_EXPR
,
1776 build_pointer_type (TREE_TYPE
1777 (TREE_TYPE (inner
))),
1779 TREE_CONSTANT (inner
) = TREE_CONSTANT (TREE_OPERAND (inner
, 0));
1781 return cp_convert (build_pointer_type (TREE_TYPE (type
)), inner
);
1784 if (TREE_CODE (exp
) == COMPOUND_EXPR
)
1786 tree op1
= decay_conversion (TREE_OPERAND (exp
, 1));
1787 return build (COMPOUND_EXPR
, TREE_TYPE (op1
),
1788 TREE_OPERAND (exp
, 0), op1
);
1792 && ! (TREE_CODE (exp
) == CONSTRUCTOR
&& TREE_STATIC (exp
)))
1794 error ("invalid use of non-lvalue array");
1795 return error_mark_node
;
1798 ptrtype
= build_pointer_type (TREE_TYPE (type
));
1800 if (TREE_CODE (exp
) == VAR_DECL
)
1802 /* ??? This is not really quite correct
1803 in that the type of the operand of ADDR_EXPR
1804 is not the target type of the type of the ADDR_EXPR itself.
1805 Question is, can this lossage be avoided? */
1806 adr
= build1 (ADDR_EXPR
, ptrtype
, exp
);
1807 if (mark_addressable (exp
) == 0)
1808 return error_mark_node
;
1809 TREE_CONSTANT (adr
) = staticp (exp
);
1810 TREE_SIDE_EFFECTS (adr
) = 0; /* Default would be, same as EXP. */
1813 /* This way is better for a COMPONENT_REF since it can
1814 simplify the offset for a component. */
1815 adr
= build_unary_op (ADDR_EXPR
, exp
, 1);
1816 return cp_convert (ptrtype
, adr
);
1819 /* [basic.lval]: Class rvalues can have cv-qualified types; non-class
1820 rvalues always have cv-unqualified types. */
1821 if (! CLASS_TYPE_P (type
))
1822 exp
= cp_convert (TYPE_MAIN_VARIANT (type
), exp
);
1828 default_conversion (exp
)
1832 enum tree_code code
;
1834 exp
= decay_conversion (exp
);
1836 type
= TREE_TYPE (exp
);
1837 code
= TREE_CODE (type
);
1839 if (INTEGRAL_CODE_P (code
))
1841 tree t
= type_promotes_to (type
);
1843 return cp_convert (t
, exp
);
1849 /* Take the address of an inline function without setting TREE_ADDRESSABLE
1853 inline_conversion (exp
)
1856 if (TREE_CODE (exp
) == FUNCTION_DECL
)
1857 exp
= build1 (ADDR_EXPR
, build_pointer_type (TREE_TYPE (exp
)), exp
);
1862 /* Returns nonzero iff exp is a STRING_CST or the result of applying
1863 decay_conversion to one. */
1866 string_conv_p (totype
, exp
, warn
)
1872 if (! flag_const_strings
|| TREE_CODE (totype
) != POINTER_TYPE
)
1875 t
= TREE_TYPE (totype
);
1876 if (!same_type_p (t
, char_type_node
)
1877 && !same_type_p (t
, wchar_type_node
))
1880 if (TREE_CODE (exp
) == STRING_CST
)
1882 /* Make sure that we don't try to convert between char and wchar_t. */
1883 if (!same_type_p (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (exp
))), t
))
1888 /* Is this a string constant which has decayed to 'const char *'? */
1889 t
= build_pointer_type (build_qualified_type (t
, TYPE_QUAL_CONST
));
1890 if (!same_type_p (TREE_TYPE (exp
), t
))
1893 if (TREE_CODE (exp
) != ADDR_EXPR
1894 || TREE_CODE (TREE_OPERAND (exp
, 0)) != STRING_CST
)
1898 /* This warning is not very useful, as it complains about printf. */
1899 if (warn
&& warn_write_strings
)
1900 cp_warning ("deprecated conversion from string constant to `%T'", totype
);
1906 build_object_ref (datum
, basetype
, field
)
1907 tree datum
, basetype
, field
;
1910 if (datum
== error_mark_node
)
1911 return error_mark_node
;
1913 dtype
= TREE_TYPE (datum
);
1914 if (TREE_CODE (dtype
) == REFERENCE_TYPE
)
1915 dtype
= TREE_TYPE (dtype
);
1916 if (! IS_AGGR_TYPE_CODE (TREE_CODE (dtype
)))
1918 cp_error ("request for member `%T::%D' in expression of non-aggregate type `%T'",
1919 basetype
, field
, dtype
);
1920 return error_mark_node
;
1922 else if (is_aggr_type (basetype
, 1))
1924 tree binfo
= binfo_or_else (basetype
, dtype
);
1926 return build_x_component_ref (build_scoped_ref (datum
, basetype
),
1929 return error_mark_node
;
1932 /* Like `build_component_ref, but uses an already found field, and converts
1933 from a reference. Must compute access for current_class_ref.
1937 build_component_ref_1 (datum
, field
, protect
)
1941 return convert_from_reference
1942 (build_component_ref (datum
, field
, NULL_TREE
, protect
));
1945 /* Given a COND_EXPR, MIN_EXPR, or MAX_EXPR in T, return it in a form that we
1946 can, for example, use as an lvalue. This code used to be in
1947 unary_complex_lvalue, but we needed it to deal with `a = (d == c) ? b : c'
1948 expressions, where we're dealing with aggregates. But now it's again only
1949 called from unary_complex_lvalue. The case (in particular) that led to
1950 this was with CODE == ADDR_EXPR, since it's not an lvalue when we'd
1954 rationalize_conditional_expr (code
, t
)
1955 enum tree_code code
;
1958 /* For MIN_EXPR or MAX_EXPR, fold-const.c has arranged things so that
1959 the first operand is always the one to be used if both operands
1960 are equal, so we know what conditional expression this used to be. */
1961 if (TREE_CODE (t
) == MIN_EXPR
|| TREE_CODE (t
) == MAX_EXPR
)
1964 build_conditional_expr (build_x_binary_op ((TREE_CODE (t
) == MIN_EXPR
1965 ? LE_EXPR
: GE_EXPR
),
1966 TREE_OPERAND (t
, 0),
1967 TREE_OPERAND (t
, 1)),
1968 build_unary_op (code
, TREE_OPERAND (t
, 0), 0),
1969 build_unary_op (code
, TREE_OPERAND (t
, 1), 0));
1973 build_conditional_expr (TREE_OPERAND (t
, 0),
1974 build_unary_op (code
, TREE_OPERAND (t
, 1), 0),
1975 build_unary_op (code
, TREE_OPERAND (t
, 2), 0));
1978 /* Given the TYPE of an anonymous union field inside T, return the
1979 FIELD_DECL for the field. If not found return NULL_TREE. Because
1980 anonymous unions can nest, we must also search all anonymous unions
1981 that are directly reachable. */
1984 lookup_anon_field (t
, type
)
1989 for (field
= TYPE_FIELDS (t
); field
; field
= TREE_CHAIN (field
))
1991 if (TREE_STATIC (field
))
1993 if (TREE_CODE (field
) != FIELD_DECL
)
1996 /* If we find it directly, return the field. */
1997 if (DECL_NAME (field
) == NULL_TREE
1998 && type
== TYPE_MAIN_VARIANT (TREE_TYPE (field
)))
2003 /* Otherwise, it could be nested, search harder. */
2004 if (DECL_NAME (field
) == NULL_TREE
2005 && ANON_AGGR_TYPE_P (TREE_TYPE (field
)))
2007 tree subfield
= lookup_anon_field (TREE_TYPE (field
), type
);
2015 /* Build a COMPONENT_REF for a given DATUM, and it's member COMPONENT.
2016 COMPONENT can be an IDENTIFIER_NODE that is the name of the member
2017 that we are interested in, or it can be a FIELD_DECL. */
2020 build_component_ref (datum
, component
, basetype_path
, protect
)
2021 tree datum
, component
, basetype_path
;
2024 register tree basetype
;
2025 register enum tree_code code
;
2026 register tree field
= NULL
;
2031 if (processing_template_decl
)
2032 return build_min_nt (COMPONENT_REF
, datum
, component
);
2034 if (datum
== error_mark_node
2035 || TREE_TYPE (datum
) == error_mark_node
)
2036 return error_mark_node
;
2038 /* BASETYPE holds the type of the class containing the COMPONENT. */
2039 basetype
= TYPE_MAIN_VARIANT (TREE_TYPE (datum
));
2041 /* If DATUM is a COMPOUND_EXPR or COND_EXPR, move our reference
2043 switch (TREE_CODE (datum
))
2047 tree value
= build_component_ref (TREE_OPERAND (datum
, 1), component
,
2048 basetype_path
, protect
);
2049 return build (COMPOUND_EXPR
, TREE_TYPE (value
),
2050 TREE_OPERAND (datum
, 0), value
);
2053 return build_conditional_expr
2054 (TREE_OPERAND (datum
, 0),
2055 build_component_ref (TREE_OPERAND (datum
, 1), component
,
2056 basetype_path
, protect
),
2057 build_component_ref (TREE_OPERAND (datum
, 2), component
,
2058 basetype_path
, protect
));
2061 cp_error ("invalid use of %D", datum
);
2062 datum
= error_mark_node
;
2069 code
= TREE_CODE (basetype
);
2071 if (code
== REFERENCE_TYPE
)
2073 datum
= convert_from_reference (datum
);
2074 basetype
= TYPE_MAIN_VARIANT (TREE_TYPE (datum
));
2075 code
= TREE_CODE (basetype
);
2077 if (TREE_CODE (datum
) == OFFSET_REF
)
2079 datum
= resolve_offset_ref (datum
);
2080 basetype
= TYPE_MAIN_VARIANT (TREE_TYPE (datum
));
2081 code
= TREE_CODE (basetype
);
2084 /* First, see if there is a field or component with name COMPONENT. */
2085 if (TREE_CODE (component
) == TREE_LIST
)
2087 /* I could not trigger this code. MvL */
2088 my_friendly_abort (980326);
2090 my_friendly_assert (!(TREE_CHAIN (component
) == NULL_TREE
2091 && DECL_CHAIN (TREE_VALUE (component
)) == NULL_TREE
), 309);
2093 return build (COMPONENT_REF
, TREE_TYPE (component
), datum
, component
);
2096 if (! IS_AGGR_TYPE_CODE (code
))
2098 if (code
!= ERROR_MARK
)
2099 cp_error ("request for member `%D' in `%E', which is of non-aggregate type `%T'",
2100 component
, datum
, basetype
);
2101 return error_mark_node
;
2104 if (!complete_type_or_else (basetype
, datum
))
2105 return error_mark_node
;
2107 if (TREE_CODE (component
) == BIT_NOT_EXPR
)
2109 if (TYPE_IDENTIFIER (basetype
) != TREE_OPERAND (component
, 0))
2111 cp_error ("destructor specifier `%T::~%T' must have matching names",
2112 basetype
, TREE_OPERAND (component
, 0));
2113 return error_mark_node
;
2115 if (! TYPE_HAS_DESTRUCTOR (basetype
))
2117 cp_error ("type `%T' has no destructor", basetype
);
2118 return error_mark_node
;
2120 return TREE_VEC_ELT (CLASSTYPE_METHOD_VEC (basetype
), 1);
2123 /* Look up component name in the structure type definition. */
2124 if (TYPE_VFIELD (basetype
)
2125 && DECL_NAME (TYPE_VFIELD (basetype
)) == component
)
2126 /* Special-case this because if we use normal lookups in an ambiguous
2127 hierarchy, the compiler will abort (because vptr lookups are
2128 not supposed to be ambiguous. */
2129 field
= TYPE_VFIELD (basetype
);
2130 else if (TREE_CODE (component
) == FIELD_DECL
)
2132 else if (TREE_CODE (component
) == TYPE_DECL
)
2134 cp_error ("invalid use of type decl `%#D' as expression", component
);
2135 return error_mark_node
;
2137 else if (TREE_CODE (component
) == TEMPLATE_DECL
)
2139 cp_error ("invalid use of template `%#D' as expression", component
);
2140 return error_mark_node
;
2144 tree name
= component
;
2145 if (TREE_CODE (component
) == VAR_DECL
)
2146 name
= DECL_NAME (component
);
2147 if (TREE_CODE (component
) == NAMESPACE_DECL
)
2148 /* Source is in error, but produce a sensible diagnostic. */
2149 name
= DECL_NAME (component
);
2150 if (basetype_path
== NULL_TREE
)
2151 basetype_path
= TYPE_BINFO (basetype
);
2152 field
= lookup_field (basetype_path
, name
,
2153 protect
&& !VFIELD_NAME_P (name
), 0);
2154 if (field
== error_mark_node
)
2155 return error_mark_node
;
2157 if (field
== NULL_TREE
)
2159 /* Not found as a data field, look for it as a method. If found,
2160 then if this is the only possible one, return it, else
2161 report ambiguity error. */
2162 tree fndecls
= lookup_fnfields (basetype_path
, name
, 1);
2163 if (fndecls
== error_mark_node
)
2164 return error_mark_node
;
2167 /* If the function is unique and static, we can resolve it
2168 now. Otherwise, we have to wait and see what context it is
2169 used in; a component_ref involving a non-static member
2170 function can only be used in a call (expr.ref). */
2172 if (TREE_CHAIN (fndecls
) == NULL_TREE
2173 && TREE_CODE (TREE_VALUE (fndecls
)) == FUNCTION_DECL
)
2175 if (DECL_STATIC_FUNCTION_P (TREE_VALUE (fndecls
)))
2177 tree fndecl
= TREE_VALUE (fndecls
);
2178 enforce_access (basetype_path
, fndecl
);
2184 /* A unique non-static member function. Other parts
2185 of the compiler expect something with
2186 unknown_type_node to be really overloaded, so
2188 TREE_VALUE (fndecls
)
2189 = ovl_cons (TREE_VALUE (fndecls
), NULL_TREE
);
2193 ref
= build (COMPONENT_REF
, unknown_type_node
,
2194 datum
, TREE_VALUE (fndecls
));
2198 cp_error ("`%#T' has no member named `%D'", basetype
, name
);
2199 return error_mark_node
;
2201 else if (TREE_TYPE (field
) == error_mark_node
)
2202 return error_mark_node
;
2204 if (TREE_CODE (field
) != FIELD_DECL
)
2206 if (TREE_CODE (field
) == TYPE_DECL
)
2207 cp_pedwarn ("invalid use of type decl `%#D' as expression", field
);
2208 else if (DECL_RTL (field
) != 0)
2211 TREE_USED (field
) = 1;
2213 /* Do evaluate the object when accessing a static member. */
2214 if (TREE_SIDE_EFFECTS (datum
))
2215 field
= build (COMPOUND_EXPR
, TREE_TYPE (field
), datum
, field
);
2221 /* See if we have to do any conversions so that we pick up the field from the
2223 if (DECL_FIELD_CONTEXT (field
) != basetype
)
2225 tree context
= DECL_FIELD_CONTEXT (field
);
2226 tree base
= context
;
2227 while (!same_type_p (base
, basetype
) && TYPE_NAME (base
)
2228 && ANON_AGGR_TYPE_P (base
))
2229 base
= TYPE_CONTEXT (base
);
2231 /* Handle base classes here... */
2232 if (base
!= basetype
&& TYPE_BASE_CONVS_MAY_REQUIRE_CODE_P (basetype
))
2234 tree addr
= build_unary_op (ADDR_EXPR
, datum
, 0);
2235 if (integer_zerop (addr
))
2237 error ("invalid reference to NULL ptr, use ptr-to-member instead");
2238 return error_mark_node
;
2240 if (VBASE_NAME_P (DECL_NAME (field
)))
2242 /* It doesn't matter which vbase pointer we grab, just
2243 find one of them. */
2244 tree binfo
= get_binfo (base
,
2245 TREE_TYPE (TREE_TYPE (addr
)), 0);
2246 addr
= convert_pointer_to_real (binfo
, addr
);
2249 addr
= convert_pointer_to (base
, addr
);
2250 datum
= build_indirect_ref (addr
, NULL
);
2251 if (datum
== error_mark_node
)
2252 return error_mark_node
;
2256 /* Handle things from anon unions here... */
2257 if (TYPE_NAME (context
) && ANON_AGGR_TYPE_P (context
))
2259 tree subfield
= lookup_anon_field (basetype
, context
);
2260 tree subdatum
= build_component_ref (datum
, subfield
,
2261 basetype_path
, protect
);
2262 return build_component_ref (subdatum
, field
, basetype_path
, protect
);
2266 /* Compute the type of the field, as described in [expr.ref]. */
2267 type_quals
= TYPE_UNQUALIFIED
;
2268 field_type
= TREE_TYPE (field
);
2269 if (TREE_CODE (field_type
) == REFERENCE_TYPE
)
2270 /* The standard says that the type of the result should be the
2271 type referred to by the reference. But for now, at least, we
2272 do the conversion from reference type later. */
2276 type_quals
= (CP_TYPE_QUALS (field_type
)
2277 | CP_TYPE_QUALS (TREE_TYPE (datum
)));
2279 /* A field is const (volatile) if the enclosing object, or the
2280 field itself, is const (volatile). But, a mutable field is
2281 not const, even within a const object. */
2282 if (DECL_MUTABLE_P (field
))
2283 type_quals
&= ~TYPE_QUAL_CONST
;
2284 field_type
= cp_build_qualified_type (field_type
, type_quals
);
2287 ref
= fold (build (COMPONENT_REF
, field_type
, datum
, field
));
2289 /* Mark the expression const or volatile, as appropriate. Even
2290 though we've dealt with the type above, we still have to mark the
2291 expression itself. */
2292 if (type_quals
& TYPE_QUAL_CONST
)
2293 TREE_READONLY (ref
) = 1;
2294 else if (type_quals
& TYPE_QUAL_VOLATILE
)
2295 TREE_THIS_VOLATILE (ref
) = 1;
2300 /* Variant of build_component_ref for use in expressions, which should
2301 never have REFERENCE_TYPE. */
2304 build_x_component_ref (datum
, component
, basetype_path
, protect
)
2305 tree datum
, component
, basetype_path
;
2308 tree t
= build_component_ref (datum
, component
, basetype_path
, protect
);
2310 if (! processing_template_decl
)
2311 t
= convert_from_reference (t
);
2316 /* Given an expression PTR for a pointer, return an expression
2317 for the value pointed to.
2318 ERRORSTRING is the name of the operator to appear in error messages.
2320 This function may need to overload OPERATOR_FNNAME.
2321 Must also handle REFERENCE_TYPEs for C++. */
2324 build_x_indirect_ref (ptr
, errorstring
)
2326 const char *errorstring
;
2330 if (processing_template_decl
)
2331 return build_min_nt (INDIRECT_REF
, ptr
);
2333 rval
= build_opfncall (INDIRECT_REF
, LOOKUP_NORMAL
, ptr
, NULL_TREE
,
2337 return build_indirect_ref (ptr
, errorstring
);
2341 build_indirect_ref (ptr
, errorstring
)
2343 const char *errorstring
;
2345 register tree pointer
, type
;
2347 if (ptr
== error_mark_node
)
2348 return error_mark_node
;
2350 if (ptr
== current_class_ptr
)
2351 return current_class_ref
;
2353 pointer
= (TREE_CODE (TREE_TYPE (ptr
)) == REFERENCE_TYPE
2354 ? ptr
: default_conversion (ptr
));
2355 type
= TREE_TYPE (pointer
);
2357 if (TYPE_PTR_P (type
) || TREE_CODE (type
) == REFERENCE_TYPE
)
2361 If the type of the expression is "pointer to T," the type
2362 of the result is "T."
2364 We must use the canonical variant because certain parts of
2365 the back end, like fold, do pointer comparisons between
2367 tree t
= canonical_type_variant (TREE_TYPE (type
));
2369 if (VOID_TYPE_P (t
))
2371 /* A pointer to incomplete type (other than cv void) can be
2372 dereferenced [expr.unary.op]/1 */
2373 cp_error ("`%T' is not a pointer-to-object type", type
);
2374 return error_mark_node
;
2376 else if (TREE_CODE (pointer
) == ADDR_EXPR
2378 && same_type_p (t
, TREE_TYPE (TREE_OPERAND (pointer
, 0))))
2379 /* The POINTER was something like `&x'. We simplify `*&x' to
2381 return TREE_OPERAND (pointer
, 0);
2384 tree ref
= build1 (INDIRECT_REF
, t
, pointer
);
2386 /* We *must* set TREE_READONLY when dereferencing a pointer to const,
2387 so that we get the proper error message if the result is used
2388 to assign to. Also, &* is supposed to be a no-op. */
2389 TREE_READONLY (ref
) = CP_TYPE_CONST_P (t
);
2390 TREE_THIS_VOLATILE (ref
) = CP_TYPE_VOLATILE_P (t
);
2391 TREE_SIDE_EFFECTS (ref
)
2392 = (TREE_THIS_VOLATILE (ref
) || TREE_SIDE_EFFECTS (pointer
)
2397 /* `pointer' won't be an error_mark_node if we were given a
2398 pointer to member, so it's cool to check for this here. */
2399 else if (TYPE_PTRMEM_P (type
) || TYPE_PTRMEMFUNC_P (type
))
2400 error ("invalid use of `%s' on pointer to member", errorstring
);
2401 else if (pointer
!= error_mark_node
)
2404 error ("invalid type argument of `%s'", errorstring
);
2406 error ("invalid type argument");
2408 return error_mark_node
;
2411 /* This handles expressions of the form "a[i]", which denotes
2414 This is logically equivalent in C to *(a+i), but we may do it differently.
2415 If A is a variable or a member, we generate a primitive ARRAY_REF.
2416 This avoids forcing the array out of registers, and can work on
2417 arrays that are not lvalues (for example, members of structures returned
2420 If INDEX is of some user-defined type, it must be converted to
2421 integer type. Otherwise, to make a compatible PLUS_EXPR, it
2422 will inherit the type of the array, which will be some pointer type. */
2425 build_array_ref (array
, idx
)
2430 error ("subscript missing in array reference");
2431 return error_mark_node
;
2434 if (TREE_TYPE (array
) == error_mark_node
2435 || TREE_TYPE (idx
) == error_mark_node
)
2436 return error_mark_node
;
2438 /* If ARRAY is a COMPOUND_EXPR or COND_EXPR, move our reference
2440 switch (TREE_CODE (array
))
2444 tree value
= build_array_ref (TREE_OPERAND (array
, 1), idx
);
2445 return build (COMPOUND_EXPR
, TREE_TYPE (value
),
2446 TREE_OPERAND (array
, 0), value
);
2450 return build_conditional_expr
2451 (TREE_OPERAND (array
, 0),
2452 build_array_ref (TREE_OPERAND (array
, 1), idx
),
2453 build_array_ref (TREE_OPERAND (array
, 2), idx
));
2459 if (TREE_CODE (TREE_TYPE (array
)) == ARRAY_TYPE
2460 && TREE_CODE (array
) != INDIRECT_REF
)
2464 /* Subscripting with type char is likely to lose
2465 on a machine where chars are signed.
2466 So warn on any machine, but optionally.
2467 Don't warn for unsigned char since that type is safe.
2468 Don't warn for signed char because anyone who uses that
2469 must have done so deliberately. */
2470 if (warn_char_subscripts
2471 && TYPE_MAIN_VARIANT (TREE_TYPE (idx
)) == char_type_node
)
2472 warning ("array subscript has type `char'");
2474 /* Apply default promotions *after* noticing character types. */
2475 idx
= default_conversion (idx
);
2477 if (TREE_CODE (TREE_TYPE (idx
)) != INTEGER_TYPE
)
2479 error ("array subscript is not an integer");
2480 return error_mark_node
;
2483 /* An array that is indexed by a non-constant
2484 cannot be stored in a register; we must be able to do
2485 address arithmetic on its address.
2486 Likewise an array of elements of variable size. */
2487 if (TREE_CODE (idx
) != INTEGER_CST
2488 || (COMPLETE_TYPE_P (TREE_TYPE (TREE_TYPE (array
)))
2489 && (TREE_CODE (TYPE_SIZE (TREE_TYPE (TREE_TYPE (array
))))
2492 if (mark_addressable (array
) == 0)
2493 return error_mark_node
;
2496 /* An array that is indexed by a constant value which is not within
2497 the array bounds cannot be stored in a register either; because we
2498 would get a crash in store_bit_field/extract_bit_field when trying
2499 to access a non-existent part of the register. */
2500 if (TREE_CODE (idx
) == INTEGER_CST
2501 && TYPE_VALUES (TREE_TYPE (array
))
2502 && ! int_fits_type_p (idx
, TYPE_VALUES (TREE_TYPE (array
))))
2504 if (mark_addressable (array
) == 0)
2505 return error_mark_node
;
2508 if (pedantic
&& !lvalue_p (array
))
2509 pedwarn ("ISO C++ forbids subscripting non-lvalue array");
2511 /* Note in C++ it is valid to subscript a `register' array, since
2512 it is valid to take the address of something with that
2513 storage specification. */
2517 while (TREE_CODE (foo
) == COMPONENT_REF
)
2518 foo
= TREE_OPERAND (foo
, 0);
2519 if (TREE_CODE (foo
) == VAR_DECL
&& DECL_REGISTER (foo
))
2520 warning ("subscripting array declared `register'");
2523 type
= TREE_TYPE (TREE_TYPE (array
));
2524 rval
= build (ARRAY_REF
, type
, array
, idx
);
2525 /* Array ref is const/volatile if the array elements are
2526 or if the array is.. */
2527 TREE_READONLY (rval
)
2528 |= (CP_TYPE_CONST_P (type
) | TREE_READONLY (array
));
2529 TREE_SIDE_EFFECTS (rval
)
2530 |= (CP_TYPE_VOLATILE_P (type
) | TREE_SIDE_EFFECTS (array
));
2531 TREE_THIS_VOLATILE (rval
)
2532 |= (CP_TYPE_VOLATILE_P (type
) | TREE_THIS_VOLATILE (array
));
2533 return require_complete_type (fold (rval
));
2537 tree ar
= default_conversion (array
);
2538 tree ind
= default_conversion (idx
);
2540 /* Put the integer in IND to simplify error checking. */
2541 if (TREE_CODE (TREE_TYPE (ar
)) == INTEGER_TYPE
)
2548 if (ar
== error_mark_node
)
2551 if (TREE_CODE (TREE_TYPE (ar
)) != POINTER_TYPE
)
2553 error ("subscripted value is neither array nor pointer");
2554 return error_mark_node
;
2556 if (TREE_CODE (TREE_TYPE (ind
)) != INTEGER_TYPE
)
2558 error ("array subscript is not an integer");
2559 return error_mark_node
;
2562 return build_indirect_ref (cp_build_binary_op (PLUS_EXPR
, ar
, ind
),
2567 /* Build a function call to function FUNCTION with parameters PARAMS.
2568 PARAMS is a list--a chain of TREE_LIST nodes--in which the
2569 TREE_VALUE of each node is a parameter-expression. The PARAMS do
2570 not include any object pointer that may be required. FUNCTION's
2571 data type may be a function type or a pointer-to-function.
2573 For C++: If FUNCTION's data type is a TREE_LIST, then the tree list
2574 is the list of possible methods that FUNCTION could conceivably
2575 be. If the list of methods comes from a class, then it will be
2576 a list of lists (where each element is associated with the class
2577 that produced it), otherwise it will be a simple list (for
2578 functions overloaded in global scope).
2580 In the first case, TREE_VALUE (function) is the head of one of those
2581 lists, and TREE_PURPOSE is the name of the function.
2583 In the second case, TREE_PURPOSE (function) is the function's
2586 DECL is the class instance variable, usually CURRENT_CLASS_REF.
2588 When calling a TEMPLATE_DECL, we don't require a complete return
2592 build_x_function_call (function
, params
, decl
)
2593 tree function
, params
, decl
;
2596 tree template_id
= NULL_TREE
;
2599 if (function
== error_mark_node
)
2600 return error_mark_node
;
2602 if (processing_template_decl
)
2603 return build_min_nt (CALL_EXPR
, function
, params
, NULL_TREE
);
2605 /* Save explicit template arguments if found */
2606 if (TREE_CODE (function
) == TEMPLATE_ID_EXPR
)
2608 template_id
= function
;
2609 function
= TREE_OPERAND (function
, 0);
2612 type
= TREE_TYPE (function
);
2614 if (TREE_CODE (type
) == OFFSET_TYPE
2615 && TREE_TYPE (type
) == unknown_type_node
2616 && TREE_CODE (function
) == TREE_LIST
2617 && TREE_CHAIN (function
) == NULL_TREE
)
2619 /* Undo (Foo:bar)()... */
2620 type
= TYPE_OFFSET_BASETYPE (type
);
2621 function
= TREE_VALUE (function
);
2622 my_friendly_assert (TREE_CODE (function
) == TREE_LIST
, 999);
2623 my_friendly_assert (TREE_CHAIN (function
) == NULL_TREE
, 999);
2624 function
= TREE_VALUE (function
);
2625 if (TREE_CODE (function
) == OVERLOAD
)
2626 function
= OVL_FUNCTION (function
);
2627 my_friendly_assert (TREE_CODE (function
) == FUNCTION_DECL
, 999);
2628 function
= DECL_NAME (function
);
2629 return build_method_call (decl
, function
, params
,
2630 TYPE_BINFO (type
), LOOKUP_NORMAL
);
2633 if (TREE_CODE (function
) == OFFSET_REF
2634 && TREE_CODE (type
) != METHOD_TYPE
)
2635 function
= resolve_offset_ref (function
);
2637 if ((TREE_CODE (function
) == FUNCTION_DECL
2638 && DECL_STATIC_FUNCTION_P (function
))
2639 || (DECL_FUNCTION_TEMPLATE_P (function
)
2640 && DECL_STATIC_FUNCTION_P (DECL_TEMPLATE_RESULT (function
))))
2641 return build_member_call (DECL_CONTEXT (function
),
2643 ? template_id
: DECL_NAME (function
),
2646 is_method
= ((TREE_CODE (function
) == TREE_LIST
2647 && current_class_type
!= NULL_TREE
2648 && (IDENTIFIER_CLASS_VALUE (TREE_PURPOSE (function
))
2650 || (TREE_CODE (function
) == OVERLOAD
2651 && DECL_FUNCTION_MEMBER_P (OVL_CURRENT (function
)))
2652 || TREE_CODE (function
) == IDENTIFIER_NODE
2653 || TREE_CODE (type
) == METHOD_TYPE
2654 || TYPE_PTRMEMFUNC_P (type
));
2656 /* A friend template. Make it look like a toplevel declaration. */
2657 if (! is_method
&& TREE_CODE (function
) == TEMPLATE_DECL
)
2658 function
= ovl_cons (function
, NULL_TREE
);
2660 /* Handle methods, friends, and overloaded functions, respectively. */
2663 tree basetype
= NULL_TREE
;
2665 if (TREE_CODE (function
) == OVERLOAD
)
2666 function
= OVL_CURRENT (function
);
2668 if (TREE_CODE (function
) == FUNCTION_DECL
2669 || DECL_FUNCTION_TEMPLATE_P (function
))
2671 basetype
= DECL_CONTEXT (function
);
2673 if (DECL_NAME (function
))
2674 function
= DECL_NAME (function
);
2676 function
= TYPE_IDENTIFIER (DECL_CONTEXT (function
));
2678 else if (TREE_CODE (function
) == TREE_LIST
)
2680 my_friendly_assert (TREE_CODE (TREE_VALUE (function
))
2681 == FUNCTION_DECL
, 312);
2682 basetype
= DECL_CONTEXT (TREE_VALUE (function
));
2683 function
= TREE_PURPOSE (function
);
2685 else if (TREE_CODE (function
) != IDENTIFIER_NODE
)
2687 if (TREE_CODE (function
) == OFFSET_REF
)
2689 if (TREE_OPERAND (function
, 0))
2690 decl
= TREE_OPERAND (function
, 0);
2692 /* Call via a pointer to member function. */
2693 if (decl
== NULL_TREE
)
2695 error ("pointer to member function called, but not in class scope");
2696 return error_mark_node
;
2698 /* What other type of POINTER_TYPE could this be? */
2699 if (TREE_CODE (TREE_TYPE (function
)) != POINTER_TYPE
2700 && ! TYPE_PTRMEMFUNC_P (TREE_TYPE (function
))
2701 && TREE_CODE (function
) != OFFSET_REF
)
2702 function
= build (OFFSET_REF
, TREE_TYPE (type
), NULL_TREE
,
2707 /* this is an abbreviated method call.
2708 must go through here in case it is a virtual function.
2709 @@ Perhaps this could be optimized. */
2711 if (basetype
&& (! current_class_type
2712 || ! DERIVED_FROM_P (basetype
, current_class_type
)))
2713 return build_member_call (basetype
, function
, params
);
2715 if (decl
== NULL_TREE
)
2717 if (current_class_type
== NULL_TREE
)
2719 cp_error ("object missing in call to method `%D'", function
);
2720 return error_mark_node
;
2722 /* Yow: call from a static member function. */
2723 decl
= build_dummy_object (current_class_type
);
2726 /* Put back explicit template arguments, if any. */
2728 function
= template_id
;
2729 return build_method_call (decl
, function
, params
,
2730 NULL_TREE
, LOOKUP_NORMAL
);
2732 else if (TREE_CODE (function
) == COMPONENT_REF
2733 && type
== unknown_type_node
)
2735 /* Undo what we did in build_component_ref. */
2736 decl
= TREE_OPERAND (function
, 0);
2737 function
= TREE_OPERAND (function
, 1);
2738 function
= DECL_NAME (OVL_CURRENT (function
));
2742 TREE_OPERAND (template_id
, 0) = function
;
2743 function
= template_id
;
2746 return build_method_call (decl
, function
, params
,
2747 NULL_TREE
, LOOKUP_NORMAL
);
2749 else if (really_overloaded_fn (function
))
2751 if (OVL_FUNCTION (function
) == NULL_TREE
)
2753 cp_error ("function `%D' declared overloaded, but no definitions appear with which to resolve it?!?",
2754 TREE_PURPOSE (function
));
2755 return error_mark_node
;
2759 /* Put back explicit template arguments, if any. */
2761 function
= template_id
;
2762 return build_new_function_call (function
, params
);
2766 /* Remove a potential OVERLOAD around it */
2767 function
= OVL_CURRENT (function
);
2770 if (TREE_CODE (function
) == OFFSET_REF
)
2772 /* If the component is a data element (or a virtual function), we play
2773 games here to make things work. */
2776 if (TREE_OPERAND (function
, 0))
2777 decl
= TREE_OPERAND (function
, 0);
2779 decl
= current_class_ref
;
2781 decl_addr
= build_unary_op (ADDR_EXPR
, decl
, 0);
2783 /* Sigh. OFFSET_REFs are being used for too many things.
2784 They're being used both for -> and ->*, and we want to resolve
2785 the -> cases here, but leave the ->*. We could use
2786 resolve_offset_ref for those, too, but it would call
2787 get_member_function_from_ptrfunc and decl_addr wouldn't get
2788 updated properly. Nasty. */
2789 if (TREE_CODE (TREE_OPERAND (function
, 1)) == FIELD_DECL
)
2790 function
= resolve_offset_ref (function
);
2792 function
= TREE_OPERAND (function
, 1);
2794 function
= get_member_function_from_ptrfunc (&decl_addr
, function
);
2795 params
= tree_cons (NULL_TREE
, decl_addr
, params
);
2796 return build_function_call (function
, params
);
2799 type
= TREE_TYPE (function
);
2800 if (type
!= error_mark_node
)
2802 if (TREE_CODE (type
) == REFERENCE_TYPE
)
2803 type
= TREE_TYPE (type
);
2805 if (IS_AGGR_TYPE (type
))
2806 return build_opfncall (CALL_EXPR
, LOOKUP_NORMAL
, function
, params
, NULL_TREE
);
2811 tree fntype
= TREE_TYPE (function
);
2812 tree ctypeptr
= NULL_TREE
;
2814 /* Explicitly named method? */
2815 if (TREE_CODE (function
) == FUNCTION_DECL
)
2816 ctypeptr
= build_pointer_type (DECL_CLASS_CONTEXT (function
));
2817 /* Expression with ptr-to-method type? It could either be a plain
2818 usage, or it might be a case where the ptr-to-method is being
2819 passed in as an argument. */
2820 else if (TYPE_PTRMEMFUNC_P (fntype
))
2822 tree rec
= TYPE_METHOD_BASETYPE (TREE_TYPE
2823 (TYPE_PTRMEMFUNC_FN_TYPE (fntype
)));
2824 ctypeptr
= build_pointer_type (rec
);
2826 /* Unexpected node type? */
2828 my_friendly_abort (116);
2829 if (decl
== NULL_TREE
)
2831 if (current_function_decl
2832 && DECL_STATIC_FUNCTION_P (current_function_decl
))
2833 error ("invalid call to member function needing `this' in static member function scope");
2835 error ("pointer to member function called, but not in class scope");
2836 return error_mark_node
;
2838 if (TREE_CODE (TREE_TYPE (decl
)) != POINTER_TYPE
2839 && ! TYPE_PTRMEMFUNC_P (TREE_TYPE (decl
)))
2841 decl
= build_unary_op (ADDR_EXPR
, decl
, 0);
2842 decl
= convert_pointer_to (TREE_TYPE (ctypeptr
), decl
);
2845 decl
= build_c_cast (ctypeptr
, decl
);
2846 params
= tree_cons (NULL_TREE
, decl
, params
);
2849 return build_function_call (function
, params
);
2852 /* Resolve a pointer to member function. INSTANCE is the object
2853 instance to use, if the member points to a virtual member. */
2856 get_member_function_from_ptrfunc (instance_ptrptr
, function
)
2857 tree
*instance_ptrptr
;
2860 if (TREE_CODE (function
) == OFFSET_REF
)
2862 function
= TREE_OPERAND (function
, 1);
2865 if (TYPE_PTRMEMFUNC_P (TREE_TYPE (function
)))
2867 tree fntype
, idx
, e1
, delta
, delta2
, e2
, e3
, aref
, vtbl
;
2868 tree instance
, basetype
;
2870 tree instance_ptr
= *instance_ptrptr
;
2872 if (instance_ptr
== error_mark_node
2873 && TREE_CODE (function
) == PTRMEM_CST
)
2875 /* Extracting the function address from a pmf is only
2876 allowed with -Wno-pmf-conversions. It only works for
2878 e1
= build_addr_func (PTRMEM_CST_MEMBER (function
));
2879 e1
= convert (TYPE_PTRMEMFUNC_FN_TYPE (TREE_TYPE (function
)), e1
);
2883 if (TREE_SIDE_EFFECTS (instance_ptr
))
2884 instance_ptr
= save_expr (instance_ptr
);
2886 if (TREE_SIDE_EFFECTS (function
))
2887 function
= save_expr (function
);
2889 fntype
= TYPE_PTRMEMFUNC_FN_TYPE (TREE_TYPE (function
));
2890 basetype
= TYPE_METHOD_BASETYPE (TREE_TYPE (fntype
));
2892 /* Convert down to the right base, before using the instance. */
2893 instance
= convert_pointer_to_real (basetype
, instance_ptr
);
2894 if (instance
== error_mark_node
&& instance_ptr
!= error_mark_node
)
2897 e3
= PFN_FROM_PTRMEMFUNC (function
);
2899 vtbl
= convert_pointer_to (ptr_type_node
, instance
);
2900 delta
= cp_convert (ptrdiff_type_node
,
2901 build_component_ref (function
, delta_identifier
,
2904 /* This used to avoid checking for virtual functions if basetype
2905 has no virtual functions, according to an earlier ANSI draft.
2906 With the final ISO C++ rules, such an optimization is
2907 incorrect: A pointer to a derived member can be static_cast
2908 to pointer-to-base-member, as long as the dynamic object
2909 later has the right member. */
2911 /* Promoting idx before saving it improves performance on RISC
2912 targets. Without promoting, the first compare used
2913 load-with-sign-extend, while the second used normal load then
2914 shift to sign-extend. An optimizer flaw, perhaps, but it's
2915 easier to make this change. */
2916 switch (TARGET_PTRMEMFUNC_VBIT_LOCATION
)
2918 case ptrmemfunc_vbit_in_pfn
:
2919 idx
= cp_build_binary_op (TRUNC_DIV_EXPR
,
2920 build1 (NOP_EXPR
, vtable_index_type
, e3
),
2921 TYPE_SIZE_UNIT (vtable_entry_type
));
2922 e1
= cp_build_binary_op (BIT_AND_EXPR
,
2923 build1 (NOP_EXPR
, vtable_index_type
, e3
),
2927 case ptrmemfunc_vbit_in_delta
:
2928 idx
= build1 (NOP_EXPR
, vtable_index_type
, e3
);
2929 e1
= cp_build_binary_op (BIT_AND_EXPR
,
2930 delta
, integer_one_node
);
2931 delta
= cp_build_binary_op (RSHIFT_EXPR
,
2932 build1 (NOP_EXPR
, vtable_index_type
,
2941 /* DELTA2 is the amount by which to adjust the `this' pointer
2942 to find the vtbl. */
2946 build_pointer_type (build_pointer_type (vtable_entry_type
)),
2947 vtbl
, cp_convert (ptrdiff_type_node
, delta2
));
2948 vtbl
= build_indirect_ref (vtbl
, NULL
);
2949 aref
= build_array_ref (vtbl
, idx
);
2951 if (! flag_vtable_thunks
)
2953 aref
= save_expr (aref
);
2955 delta
= cp_build_binary_op
2957 build_conditional_expr (e1
,
2958 build_component_ref (aref
,
2965 if (flag_vtable_thunks
)
2968 e2
= build_component_ref (aref
, pfn_identifier
, NULL_TREE
, 0);
2969 TREE_TYPE (e2
) = TREE_TYPE (e3
);
2970 e1
= build_conditional_expr (e1
, e2
, e3
);
2972 /* Make sure this doesn't get evaluated first inside one of the
2973 branches of the COND_EXPR. */
2974 if (TREE_CODE (instance_ptr
) == SAVE_EXPR
)
2975 e1
= build (COMPOUND_EXPR
, TREE_TYPE (e1
),
2978 *instance_ptrptr
= build (PLUS_EXPR
, TREE_TYPE (instance_ptr
),
2979 instance_ptr
, delta
);
2981 if (instance_ptr
== error_mark_node
2982 && TREE_CODE (e1
) != ADDR_EXPR
2983 && TREE_CODE (TREE_OPERAND (e1
, 0)) != FUNCTION_DECL
)
2984 cp_error ("object missing in `%E'", function
);
2992 build_function_call_real (function
, params
, require_complete
, flags
)
2993 tree function
, params
;
2994 int require_complete
, flags
;
2996 register tree fntype
, fndecl
;
2997 register tree value_type
;
2998 register tree coerced_params
;
3000 tree name
= NULL_TREE
, assembler_name
= NULL_TREE
;
3003 /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
3004 Strip such NOP_EXPRs, since FUNCTION is used in non-lvalue context. */
3005 if (TREE_CODE (function
) == NOP_EXPR
3006 && TREE_TYPE (function
) == TREE_TYPE (TREE_OPERAND (function
, 0)))
3007 function
= TREE_OPERAND (function
, 0);
3009 if (TREE_CODE (function
) == FUNCTION_DECL
)
3011 name
= DECL_NAME (function
);
3012 assembler_name
= DECL_ASSEMBLER_NAME (function
);
3014 GNU_xref_call (current_function_decl
,
3015 IDENTIFIER_POINTER (name
? name
3016 : TYPE_IDENTIFIER (DECL_CLASS_CONTEXT
3018 mark_used (function
);
3021 /* Convert anything with function type to a pointer-to-function. */
3022 if (pedantic
&& DECL_MAIN_P (function
))
3023 pedwarn ("ISO C++ forbids calling `::main' from within program");
3025 /* Differs from default_conversion by not setting TREE_ADDRESSABLE
3026 (because calling an inline function does not mean the function
3027 needs to be separately compiled). */
3029 if (DECL_INLINE (function
))
3030 function
= inline_conversion (function
);
3032 function
= build_addr_func (function
);
3038 function
= build_addr_func (function
);
3041 if (function
== error_mark_node
)
3042 return error_mark_node
;
3044 fntype
= TREE_TYPE (function
);
3046 if (TYPE_PTRMEMFUNC_P (fntype
))
3048 cp_error ("must use .* or ->* to call pointer-to-member function in `%E (...)'",
3050 return error_mark_node
;
3053 is_method
= (TREE_CODE (fntype
) == POINTER_TYPE
3054 && TREE_CODE (TREE_TYPE (fntype
)) == METHOD_TYPE
);
3056 if (!((TREE_CODE (fntype
) == POINTER_TYPE
3057 && TREE_CODE (TREE_TYPE (fntype
)) == FUNCTION_TYPE
)
3059 || TREE_CODE (function
) == TEMPLATE_ID_EXPR
))
3061 cp_error ("`%E' cannot be used as a function", function
);
3062 return error_mark_node
;
3065 /* fntype now gets the type of function pointed to. */
3066 fntype
= TREE_TYPE (fntype
);
3068 /* Convert the parameters to the types declared in the
3069 function prototype, or apply default promotions. */
3071 if (flags
& LOOKUP_COMPLAIN
)
3072 coerced_params
= convert_arguments (TYPE_ARG_TYPES (fntype
),
3073 params
, fndecl
, LOOKUP_NORMAL
);
3075 coerced_params
= convert_arguments (TYPE_ARG_TYPES (fntype
),
3078 if (coerced_params
== error_mark_node
)
3080 if (flags
& LOOKUP_SPECULATIVELY
)
3083 return error_mark_node
;
3086 /* Check for errors in format strings. */
3088 if (warn_format
&& (name
|| assembler_name
))
3089 check_function_format (NULL
, name
, assembler_name
, coerced_params
);
3091 /* Recognize certain built-in functions so we can make tree-codes
3092 other than CALL_EXPR. We do this when it enables fold-const.c
3093 to do something useful. */
3095 if (TREE_CODE (function
) == ADDR_EXPR
3096 && TREE_CODE (TREE_OPERAND (function
, 0)) == FUNCTION_DECL
3097 && DECL_BUILT_IN (TREE_OPERAND (function
, 0)))
3099 result
= expand_tree_builtin (TREE_OPERAND (function
, 0),
3100 params
, coerced_params
);
3105 /* Some built-in function calls will be evaluated at
3106 compile-time in fold (). */
3107 result
= fold (build_call (function
, coerced_params
));
3108 value_type
= TREE_TYPE (result
);
3110 if (require_complete
)
3112 if (TREE_CODE (value_type
) == VOID_TYPE
)
3114 result
= require_complete_type (result
);
3116 if (IS_AGGR_TYPE (value_type
))
3117 result
= build_cplus_new (value_type
, result
);
3118 return convert_from_reference (result
);
3122 build_function_call (function
, params
)
3123 tree function
, params
;
3125 return build_function_call_real (function
, params
, 1, LOOKUP_NORMAL
);
3128 /* Convert the actual parameter expressions in the list VALUES
3129 to the types in the list TYPELIST.
3130 If parmdecls is exhausted, or when an element has NULL as its type,
3131 perform the default conversions.
3133 NAME is an IDENTIFIER_NODE or 0. It is used only for error messages.
3135 This is also where warnings about wrong number of args are generated.
3137 Return a list of expressions for the parameters as converted.
3139 Both VALUES and the returned value are chains of TREE_LIST nodes
3140 with the elements of the list in the TREE_VALUE slots of those nodes.
3142 In C++, unspecified trailing parameters can be filled in with their
3143 default arguments, if such were specified. Do so here. */
3146 convert_arguments (typelist
, values
, fndecl
, flags
)
3147 tree typelist
, values
, fndecl
;
3150 register tree typetail
, valtail
;
3151 register tree result
= NULL_TREE
;
3152 const char *called_thing
= 0;
3155 /* Argument passing is always copy-initialization. */
3156 flags
|= LOOKUP_ONLYCONVERTING
;
3160 if (TREE_CODE (TREE_TYPE (fndecl
)) == METHOD_TYPE
)
3162 if (DECL_NAME (fndecl
) == NULL_TREE
3163 || IDENTIFIER_HAS_TYPE_VALUE (DECL_NAME (fndecl
)))
3164 called_thing
= "constructor";
3166 called_thing
= "member function";
3169 called_thing
= "function";
3172 for (valtail
= values
, typetail
= typelist
;
3174 valtail
= TREE_CHAIN (valtail
), i
++)
3176 register tree type
= typetail
? TREE_VALUE (typetail
) : 0;
3177 register tree val
= TREE_VALUE (valtail
);
3179 if (val
== error_mark_node
)
3180 return error_mark_node
;
3182 if (type
== void_type_node
)
3186 cp_error_at ("too many arguments to %s `%+#D'", called_thing
,
3188 error ("at this point in file");
3191 error ("too many arguments to function");
3192 /* In case anybody wants to know if this argument
3195 TREE_TYPE (tree_last (result
)) = error_mark_node
;
3199 if (TREE_CODE (val
) == OFFSET_REF
)
3200 val
= resolve_offset_ref (val
);
3202 /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
3203 Strip such NOP_EXPRs, since VAL is used in non-lvalue context. */
3204 if (TREE_CODE (val
) == NOP_EXPR
3205 && TREE_TYPE (val
) == TREE_TYPE (TREE_OPERAND (val
, 0))
3206 && (type
== 0 || TREE_CODE (type
) != REFERENCE_TYPE
))
3207 val
= TREE_OPERAND (val
, 0);
3209 if (type
== 0 || TREE_CODE (type
) != REFERENCE_TYPE
)
3211 if (TREE_CODE (TREE_TYPE (val
)) == ARRAY_TYPE
3212 || TREE_CODE (TREE_TYPE (val
)) == FUNCTION_TYPE
3213 || TREE_CODE (TREE_TYPE (val
)) == METHOD_TYPE
)
3214 val
= default_conversion (val
);
3217 if (val
== error_mark_node
)
3218 return error_mark_node
;
3222 /* Formal parm type is specified by a function prototype. */
3225 if (!COMPLETE_TYPE_P (complete_type (type
)))
3227 error ("parameter type of called function is incomplete");
3232 parmval
= convert_for_initialization
3233 (NULL_TREE
, type
, val
, flags
,
3234 "argument passing", fndecl
, i
);
3235 if (PROMOTE_PROTOTYPES
3236 && INTEGRAL_TYPE_P (type
)
3237 && (TYPE_PRECISION (type
)
3238 < TYPE_PRECISION (integer_type_node
)))
3239 parmval
= default_conversion (parmval
);
3242 if (parmval
== error_mark_node
)
3243 return error_mark_node
;
3245 result
= tree_cons (NULL_TREE
, parmval
, result
);
3249 if (TREE_CODE (TREE_TYPE (val
)) == REFERENCE_TYPE
)
3250 val
= convert_from_reference (val
);
3252 if (fndecl
&& DECL_BUILT_IN (fndecl
)
3253 && DECL_FUNCTION_CODE (fndecl
) == BUILT_IN_CONSTANT_P
)
3254 /* Don't do ellipsis conversion for __built_in_constant_p
3255 as this will result in spurious warnings for non-POD
3257 val
= require_complete_type (val
);
3259 val
= convert_arg_to_ellipsis (val
);
3261 result
= tree_cons (NULL_TREE
, val
, result
);
3265 typetail
= TREE_CHAIN (typetail
);
3268 if (typetail
!= 0 && typetail
!= void_list_node
)
3270 /* See if there are default arguments that can be used */
3271 if (TREE_PURPOSE (typetail
))
3273 for (; typetail
!= void_list_node
; ++i
)
3276 = convert_default_arg (TREE_VALUE (typetail
),
3277 TREE_PURPOSE (typetail
),
3280 if (parmval
== error_mark_node
)
3281 return error_mark_node
;
3283 result
= tree_cons (0, parmval
, result
);
3284 typetail
= TREE_CHAIN (typetail
);
3285 /* ends with `...'. */
3286 if (typetail
== NULL_TREE
)
3294 cp_error_at ("too few arguments to %s `%+#D'",
3295 called_thing
, fndecl
);
3296 error ("at this point in file");
3299 error ("too few arguments to function");
3300 return error_mark_list
;
3304 return nreverse (result
);
3307 /* Build a binary-operation expression, after performing default
3308 conversions on the operands. CODE is the kind of expression to build. */
3311 build_x_binary_op (code
, arg1
, arg2
)
3312 enum tree_code code
;
3315 if (processing_template_decl
)
3316 return build_min_nt (code
, arg1
, arg2
);
3318 return build_new_op (code
, LOOKUP_NORMAL
, arg1
, arg2
, NULL_TREE
);
3321 /* Build a binary-operation expression without default conversions.
3322 CODE is the kind of expression to build.
3323 This function differs from `build' in several ways:
3324 the data type of the result is computed and recorded in it,
3325 warnings are generated if arg data types are invalid,
3326 special handling for addition and subtraction of pointers is known,
3327 and some optimization is done (operations on narrow ints
3328 are done in the narrower type when that gives the same result).
3329 Constant folding is also done before the result is returned.
3331 Note that the operands will never have enumeral types
3332 because either they have just had the default conversions performed
3333 or they have both just been converted to some other type in which
3334 the arithmetic is to be done.
3336 C++: must do special pointer arithmetic when implementing
3337 multiple inheritance, and deal with pointer to member functions. */
3340 build_binary_op (code
, orig_op0
, orig_op1
, convert_p
)
3341 enum tree_code code
;
3342 tree orig_op0
, orig_op1
;
3343 int convert_p ATTRIBUTE_UNUSED
;
3346 register enum tree_code code0
, code1
;
3349 /* Expression code to give to the expression when it is built.
3350 Normally this is CODE, which is what the caller asked for,
3351 but in some special cases we change it. */
3352 register enum tree_code resultcode
= code
;
3354 /* Data type in which the computation is to be performed.
3355 In the simplest cases this is the common type of the arguments. */
3356 register tree result_type
= NULL
;
3358 /* Nonzero means operands have already been type-converted
3359 in whatever way is necessary.
3360 Zero means they need to be converted to RESULT_TYPE. */
3363 /* Nonzero means create the expression with this type, rather than
3365 tree build_type
= 0;
3367 /* Nonzero means after finally constructing the expression
3368 convert it to this type. */
3369 tree final_type
= 0;
3371 /* Nonzero if this is an operation like MIN or MAX which can
3372 safely be computed in short if both args are promoted shorts.
3373 Also implies COMMON.
3374 -1 indicates a bitwise operation; this makes a difference
3375 in the exact conditions for when it is safe to do the operation
3376 in a narrower mode. */
3379 /* Nonzero if this is a comparison operation;
3380 if both args are promoted shorts, compare the original shorts.
3381 Also implies COMMON. */
3382 int short_compare
= 0;
3384 /* Nonzero if this is a right-shift operation, which can be computed on the
3385 original short and then promoted if the operand is a promoted short. */
3386 int short_shift
= 0;
3388 /* Nonzero means set RESULT_TYPE to the common type of the args. */
3391 /* Apply default conversions. */
3392 if (code
== TRUTH_AND_EXPR
|| code
== TRUTH_ANDIF_EXPR
3393 || code
== TRUTH_OR_EXPR
|| code
== TRUTH_ORIF_EXPR
3394 || code
== TRUTH_XOR_EXPR
)
3396 op0
= decay_conversion (orig_op0
);
3397 op1
= decay_conversion (orig_op1
);
3401 op0
= default_conversion (orig_op0
);
3402 op1
= default_conversion (orig_op1
);
3405 /* Strip NON_LVALUE_EXPRs, etc., since we aren't using as an lvalue. */
3406 STRIP_TYPE_NOPS (op0
);
3407 STRIP_TYPE_NOPS (op1
);
3409 /* DTRT if one side is an overloaded function, but complain about it. */
3410 if (type_unknown_p (op0
))
3412 tree t
= instantiate_type (TREE_TYPE (op1
), op0
, itf_none
);
3413 if (t
!= error_mark_node
)
3415 cp_pedwarn ("assuming cast to type `%T' from overloaded function",
3420 if (type_unknown_p (op1
))
3422 tree t
= instantiate_type (TREE_TYPE (op0
), op1
, itf_none
);
3423 if (t
!= error_mark_node
)
3425 cp_pedwarn ("assuming cast to type `%T' from overloaded function",
3431 type0
= TREE_TYPE (op0
);
3432 type1
= TREE_TYPE (op1
);
3434 /* The expression codes of the data types of the arguments tell us
3435 whether the arguments are integers, floating, pointers, etc. */
3436 code0
= TREE_CODE (type0
);
3437 code1
= TREE_CODE (type1
);
3439 /* If an error was already reported for one of the arguments,
3440 avoid reporting another error. */
3442 if (code0
== ERROR_MARK
|| code1
== ERROR_MARK
)
3443 return error_mark_node
;
3448 /* Handle the pointer + int case. */
3449 if (code0
== POINTER_TYPE
&& code1
== INTEGER_TYPE
)
3450 return pointer_int_sum (PLUS_EXPR
, op0
, op1
);
3451 else if (code1
== POINTER_TYPE
&& code0
== INTEGER_TYPE
)
3452 return pointer_int_sum (PLUS_EXPR
, op1
, op0
);
3458 /* Subtraction of two similar pointers.
3459 We must subtract them as integers, then divide by object size. */
3460 if (code0
== POINTER_TYPE
&& code1
== POINTER_TYPE
3461 && comp_target_types (type0
, type1
, 1))
3462 return pointer_diff (op0
, op1
, common_type (type0
, type1
));
3463 /* Handle pointer minus int. Just like pointer plus int. */
3464 else if (code0
== POINTER_TYPE
&& code1
== INTEGER_TYPE
)
3465 return pointer_int_sum (MINUS_EXPR
, op0
, op1
);
3474 case TRUNC_DIV_EXPR
:
3476 case FLOOR_DIV_EXPR
:
3477 case ROUND_DIV_EXPR
:
3478 case EXACT_DIV_EXPR
:
3479 if ((code0
== INTEGER_TYPE
|| code0
== REAL_TYPE
3480 || code0
== COMPLEX_TYPE
)
3481 && (code1
== INTEGER_TYPE
|| code1
== REAL_TYPE
3482 || code1
== COMPLEX_TYPE
))
3484 if (TREE_CODE (op1
) == INTEGER_CST
&& integer_zerop (op1
))
3485 cp_warning ("division by zero in `%E / 0'", op0
);
3486 else if (TREE_CODE (op1
) == REAL_CST
&& real_zerop (op1
))
3487 cp_warning ("division by zero in `%E / 0.'", op0
);
3489 if (!(code0
== INTEGER_TYPE
&& code1
== INTEGER_TYPE
))
3490 resultcode
= RDIV_EXPR
;
3492 /* When dividing two signed integers, we have to promote to int.
3493 unless we divide by a constant != -1. Note that default
3494 conversion will have been performed on the operands at this
3495 point, so we have to dig out the original type to find out if
3497 shorten
= ((TREE_CODE (op0
) == NOP_EXPR
3498 && TREE_UNSIGNED (TREE_TYPE (TREE_OPERAND (op0
, 0))))
3499 || (TREE_CODE (op1
) == INTEGER_CST
3500 && ! integer_all_onesp (op1
)));
3507 case BIT_ANDTC_EXPR
:
3510 if (code0
== INTEGER_TYPE
&& code1
== INTEGER_TYPE
)
3512 /* If one operand is a constant, and the other is a short type
3513 that has been converted to an int,
3514 really do the work in the short type and then convert the
3515 result to int. If we are lucky, the constant will be 0 or 1
3516 in the short type, making the entire operation go away. */
3517 if (TREE_CODE (op0
) == INTEGER_CST
3518 && TREE_CODE (op1
) == NOP_EXPR
3519 && (TYPE_PRECISION (type1
)
3520 > TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (op1
, 0))))
3521 && TREE_UNSIGNED (TREE_TYPE (TREE_OPERAND (op1
, 0))))
3523 final_type
= result_type
;
3524 op1
= TREE_OPERAND (op1
, 0);
3525 result_type
= TREE_TYPE (op1
);
3527 if (TREE_CODE (op1
) == INTEGER_CST
3528 && TREE_CODE (op0
) == NOP_EXPR
3529 && (TYPE_PRECISION (type0
)
3530 > TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (op0
, 0))))
3531 && TREE_UNSIGNED (TREE_TYPE (TREE_OPERAND (op0
, 0))))
3533 final_type
= result_type
;
3534 op0
= TREE_OPERAND (op0
, 0);
3535 result_type
= TREE_TYPE (op0
);
3539 case TRUNC_MOD_EXPR
:
3540 case FLOOR_MOD_EXPR
:
3541 if (code1
== INTEGER_TYPE
&& integer_zerop (op1
))
3542 cp_warning ("division by zero in `%E %% 0'", op0
);
3543 else if (code1
== REAL_TYPE
&& real_zerop (op1
))
3544 cp_warning ("division by zero in `%E %% 0.'", op0
);
3546 if (code0
== INTEGER_TYPE
&& code1
== INTEGER_TYPE
)
3548 /* Although it would be tempting to shorten always here, that loses
3549 on some targets, since the modulo instruction is undefined if the
3550 quotient can't be represented in the computation mode. We shorten
3551 only if unsigned or if dividing by something we know != -1. */
3552 shorten
= ((TREE_CODE (op0
) == NOP_EXPR
3553 && TREE_UNSIGNED (TREE_TYPE (TREE_OPERAND (op0
, 0))))
3554 || (TREE_CODE (op1
) == INTEGER_CST
3555 && ! integer_all_onesp (op1
)));
3560 case TRUTH_ANDIF_EXPR
:
3561 case TRUTH_ORIF_EXPR
:
3562 case TRUTH_AND_EXPR
:
3564 result_type
= boolean_type_node
;
3567 /* Shift operations: result has same type as first operand;
3568 always convert second operand to int.
3569 Also set SHORT_SHIFT if shifting rightward. */
3572 if (code0
== INTEGER_TYPE
&& code1
== INTEGER_TYPE
)
3574 result_type
= type0
;
3575 if (TREE_CODE (op1
) == INTEGER_CST
)
3577 if (tree_int_cst_lt (op1
, integer_zero_node
))
3578 warning ("right shift count is negative");
3581 if (! integer_zerop (op1
))
3583 if (compare_tree_int (op1
, TYPE_PRECISION (type0
)) >= 0)
3584 warning ("right shift count >= width of type");
3587 /* Convert the shift-count to an integer, regardless of
3588 size of value being shifted. */
3589 if (TYPE_MAIN_VARIANT (TREE_TYPE (op1
)) != integer_type_node
)
3590 op1
= cp_convert (integer_type_node
, op1
);
3591 /* Avoid converting op1 to result_type later. */
3597 if (code0
== INTEGER_TYPE
&& code1
== INTEGER_TYPE
)
3599 result_type
= type0
;
3600 if (TREE_CODE (op1
) == INTEGER_CST
)
3602 if (tree_int_cst_lt (op1
, integer_zero_node
))
3603 warning ("left shift count is negative");
3604 else if (compare_tree_int (op1
, TYPE_PRECISION (type0
)) >= 0)
3605 warning ("left shift count >= width of type");
3607 /* Convert the shift-count to an integer, regardless of
3608 size of value being shifted. */
3609 if (TYPE_MAIN_VARIANT (TREE_TYPE (op1
)) != integer_type_node
)
3610 op1
= cp_convert (integer_type_node
, op1
);
3611 /* Avoid converting op1 to result_type later. */
3618 if (code0
== INTEGER_TYPE
&& code1
== INTEGER_TYPE
)
3620 result_type
= type0
;
3621 if (TREE_CODE (op1
) == INTEGER_CST
)
3623 if (tree_int_cst_lt (op1
, integer_zero_node
))
3624 warning ("%s rotate count is negative",
3625 (code
== LROTATE_EXPR
) ? "left" : "right");
3626 else if (compare_tree_int (op1
, TYPE_PRECISION (type0
)) >= 0)
3627 warning ("%s rotate count >= width of type",
3628 (code
== LROTATE_EXPR
) ? "left" : "right");
3630 /* Convert the shift-count to an integer, regardless of
3631 size of value being shifted. */
3632 if (TYPE_MAIN_VARIANT (TREE_TYPE (op1
)) != integer_type_node
)
3633 op1
= cp_convert (integer_type_node
, op1
);
3639 if (warn_float_equal
&& (code0
== REAL_TYPE
|| code1
== REAL_TYPE
))
3640 warning ("comparing floating point with == or != is unsafe");
3642 build_type
= boolean_type_node
;
3643 if ((code0
== INTEGER_TYPE
|| code0
== REAL_TYPE
3644 || code0
== COMPLEX_TYPE
)
3645 && (code1
== INTEGER_TYPE
|| code1
== REAL_TYPE
3646 || code1
== COMPLEX_TYPE
))
3648 else if (code0
== POINTER_TYPE
&& code1
== POINTER_TYPE
)
3649 result_type
= composite_pointer_type (type0
, type1
, op0
, op1
,
3651 else if (code0
== POINTER_TYPE
&& null_ptr_cst_p (op1
))
3652 result_type
= type0
;
3653 else if (code1
== POINTER_TYPE
&& null_ptr_cst_p (op0
))
3654 result_type
= type1
;
3655 else if (code0
== POINTER_TYPE
&& code1
== INTEGER_TYPE
)
3657 result_type
= type0
;
3658 error ("ISO C++ forbids comparison between pointer and integer");
3660 else if (code0
== INTEGER_TYPE
&& code1
== POINTER_TYPE
)
3662 result_type
= type1
;
3663 error ("ISO C++ forbids comparison between pointer and integer");
3665 else if (TYPE_PTRMEMFUNC_P (type0
) && null_ptr_cst_p (op1
))
3667 op0
= build_component_ref (op0
, pfn_identifier
, NULL_TREE
, 0);
3668 op1
= cp_convert (TREE_TYPE (op0
), integer_zero_node
);
3669 result_type
= TREE_TYPE (op0
);
3671 else if (TYPE_PTRMEMFUNC_P (type1
) && null_ptr_cst_p (op0
))
3672 return cp_build_binary_op (code
, op1
, op0
);
3673 else if (TYPE_PTRMEMFUNC_P (type0
) && TYPE_PTRMEMFUNC_P (type1
)
3674 && same_type_p (type0
, type1
))
3676 /* E will be the final comparison. */
3678 /* E1 and E2 are for scratch. */
3686 if (TREE_SIDE_EFFECTS (op0
))
3687 op0
= save_expr (op0
);
3688 if (TREE_SIDE_EFFECTS (op1
))
3689 op1
= save_expr (op1
);
3694 && (!op0.pfn || op0.delta == op1.delta))
3696 The reason for the `!op0.pfn' bit is that a NULL
3697 pointer-to-member is any member with a zero PFN; the
3698 DELTA field is unspecified. */
3699 pfn0
= pfn_from_ptrmemfunc (op0
);
3700 pfn1
= pfn_from_ptrmemfunc (op1
);
3701 delta0
= build_component_ref (op0
, delta_identifier
,
3703 delta1
= build_component_ref (op1
, delta_identifier
,
3705 e1
= cp_build_binary_op (EQ_EXPR
, delta0
, delta1
);
3706 e2
= cp_build_binary_op (EQ_EXPR
,
3708 cp_convert (TREE_TYPE (pfn0
),
3709 integer_zero_node
));
3710 e1
= cp_build_binary_op (TRUTH_ORIF_EXPR
, e1
, e2
);
3711 e2
= build (EQ_EXPR
, boolean_type_node
, pfn0
, pfn1
);
3712 e
= cp_build_binary_op (TRUTH_ANDIF_EXPR
, e2
, e1
);
3713 if (code
== EQ_EXPR
)
3715 return cp_build_binary_op (EQ_EXPR
, e
, integer_zero_node
);
3717 else if ((TYPE_PTRMEMFUNC_P (type0
)
3718 && same_type_p (TYPE_PTRMEMFUNC_FN_TYPE (type0
), type1
))
3719 || (TYPE_PTRMEMFUNC_P (type1
)
3720 && same_type_p (TYPE_PTRMEMFUNC_FN_TYPE (type1
), type0
)))
3721 my_friendly_abort (20000221);
3726 if ((code0
== INTEGER_TYPE
|| code0
== REAL_TYPE
)
3727 && (code1
== INTEGER_TYPE
|| code1
== REAL_TYPE
))
3729 else if (code0
== POINTER_TYPE
&& code1
== POINTER_TYPE
)
3730 result_type
= composite_pointer_type (type0
, type1
, op0
, op1
,
3738 build_type
= boolean_type_node
;
3739 if ((code0
== INTEGER_TYPE
|| code0
== REAL_TYPE
)
3740 && (code1
== INTEGER_TYPE
|| code1
== REAL_TYPE
))
3742 else if (code0
== POINTER_TYPE
&& code1
== POINTER_TYPE
)
3743 result_type
= composite_pointer_type (type0
, type1
, op0
, op1
,
3745 else if (code0
== POINTER_TYPE
&& TREE_CODE (op1
) == INTEGER_CST
3746 && integer_zerop (op1
))
3747 result_type
= type0
;
3748 else if (code1
== POINTER_TYPE
&& TREE_CODE (op0
) == INTEGER_CST
3749 && integer_zerop (op0
))
3750 result_type
= type1
;
3751 else if (code0
== POINTER_TYPE
&& code1
== INTEGER_TYPE
)
3753 result_type
= type0
;
3754 pedwarn ("ISO C++ forbids comparison between pointer and integer");
3756 else if (code0
== INTEGER_TYPE
&& code1
== POINTER_TYPE
)
3758 result_type
= type1
;
3759 pedwarn ("ISO C++ forbids comparison between pointer and integer");
3763 case UNORDERED_EXPR
:
3770 build_type
= integer_type_node
;
3771 if (code0
!= REAL_TYPE
|| code1
!= REAL_TYPE
)
3773 error ("unordered comparison on non-floating point argument");
3774 return error_mark_node
;
3783 if ((code0
== INTEGER_TYPE
|| code0
== REAL_TYPE
|| code0
== COMPLEX_TYPE
)
3785 (code1
== INTEGER_TYPE
|| code1
== REAL_TYPE
|| code1
== COMPLEX_TYPE
))
3787 int none_complex
= (code0
!= COMPLEX_TYPE
&& code1
!= COMPLEX_TYPE
);
3789 if (shorten
|| common
|| short_compare
)
3790 result_type
= common_type (type0
, type1
);
3792 /* For certain operations (which identify themselves by shorten != 0)
3793 if both args were extended from the same smaller type,
3794 do the arithmetic in that type and then extend.
3796 shorten !=0 and !=1 indicates a bitwise operation.
3797 For them, this optimization is safe only if
3798 both args are zero-extended or both are sign-extended.
3799 Otherwise, we might change the result.
3800 Eg, (short)-1 | (unsigned short)-1 is (int)-1
3801 but calculated in (unsigned short) it would be (unsigned short)-1. */
3803 if (shorten
&& none_complex
)
3805 int unsigned0
, unsigned1
;
3806 tree arg0
= get_narrower (op0
, &unsigned0
);
3807 tree arg1
= get_narrower (op1
, &unsigned1
);
3808 /* UNS is 1 if the operation to be done is an unsigned one. */
3809 int uns
= TREE_UNSIGNED (result_type
);
3812 final_type
= result_type
;
3814 /* Handle the case that OP0 does not *contain* a conversion
3815 but it *requires* conversion to FINAL_TYPE. */
3817 if (op0
== arg0
&& TREE_TYPE (op0
) != final_type
)
3818 unsigned0
= TREE_UNSIGNED (TREE_TYPE (op0
));
3819 if (op1
== arg1
&& TREE_TYPE (op1
) != final_type
)
3820 unsigned1
= TREE_UNSIGNED (TREE_TYPE (op1
));
3822 /* Now UNSIGNED0 is 1 if ARG0 zero-extends to FINAL_TYPE. */
3824 /* For bitwise operations, signedness of nominal type
3825 does not matter. Consider only how operands were extended. */
3829 /* Note that in all three cases below we refrain from optimizing
3830 an unsigned operation on sign-extended args.
3831 That would not be valid. */
3833 /* Both args variable: if both extended in same way
3834 from same width, do it in that width.
3835 Do it unsigned if args were zero-extended. */
3836 if ((TYPE_PRECISION (TREE_TYPE (arg0
))
3837 < TYPE_PRECISION (result_type
))
3838 && (TYPE_PRECISION (TREE_TYPE (arg1
))
3839 == TYPE_PRECISION (TREE_TYPE (arg0
)))
3840 && unsigned0
== unsigned1
3841 && (unsigned0
|| !uns
))
3843 = signed_or_unsigned_type (unsigned0
,
3844 common_type (TREE_TYPE (arg0
),
3846 else if (TREE_CODE (arg0
) == INTEGER_CST
3847 && (unsigned1
|| !uns
)
3848 && (TYPE_PRECISION (TREE_TYPE (arg1
))
3849 < TYPE_PRECISION (result_type
))
3850 && (type
= signed_or_unsigned_type (unsigned1
,
3852 int_fits_type_p (arg0
, type
)))
3854 else if (TREE_CODE (arg1
) == INTEGER_CST
3855 && (unsigned0
|| !uns
)
3856 && (TYPE_PRECISION (TREE_TYPE (arg0
))
3857 < TYPE_PRECISION (result_type
))
3858 && (type
= signed_or_unsigned_type (unsigned0
,
3860 int_fits_type_p (arg1
, type
)))
3864 /* Shifts can be shortened if shifting right. */
3869 tree arg0
= get_narrower (op0
, &unsigned_arg
);
3871 final_type
= result_type
;
3873 if (arg0
== op0
&& final_type
== TREE_TYPE (op0
))
3874 unsigned_arg
= TREE_UNSIGNED (TREE_TYPE (op0
));
3876 if (TYPE_PRECISION (TREE_TYPE (arg0
)) < TYPE_PRECISION (result_type
)
3877 /* We can shorten only if the shift count is less than the
3878 number of bits in the smaller type size. */
3879 && compare_tree_int (op1
, TYPE_PRECISION (TREE_TYPE (arg0
))) < 0
3880 /* If arg is sign-extended and then unsigned-shifted,
3881 we can simulate this with a signed shift in arg's type
3882 only if the extended result is at least twice as wide
3883 as the arg. Otherwise, the shift could use up all the
3884 ones made by sign-extension and bring in zeros.
3885 We can't optimize that case at all, but in most machines
3886 it never happens because available widths are 2**N. */
3887 && (!TREE_UNSIGNED (final_type
)
3889 || (((unsigned) 2 * TYPE_PRECISION (TREE_TYPE (arg0
)))
3890 <= TYPE_PRECISION (result_type
))))
3892 /* Do an unsigned shift if the operand was zero-extended. */
3894 = signed_or_unsigned_type (unsigned_arg
,
3896 /* Convert value-to-be-shifted to that type. */
3897 if (TREE_TYPE (op0
) != result_type
)
3898 op0
= cp_convert (result_type
, op0
);
3903 /* Comparison operations are shortened too but differently.
3904 They identify themselves by setting short_compare = 1. */
3908 /* Don't write &op0, etc., because that would prevent op0
3909 from being kept in a register.
3910 Instead, make copies of the our local variables and
3911 pass the copies by reference, then copy them back afterward. */
3912 tree xop0
= op0
, xop1
= op1
, xresult_type
= result_type
;
3913 enum tree_code xresultcode
= resultcode
;
3915 = shorten_compare (&xop0
, &xop1
, &xresult_type
, &xresultcode
);
3917 return cp_convert (boolean_type_node
, val
);
3918 op0
= xop0
, op1
= xop1
;
3920 resultcode
= xresultcode
;
3923 if ((short_compare
|| code
== MIN_EXPR
|| code
== MAX_EXPR
)
3924 && warn_sign_compare
)
3926 int op0_signed
= ! TREE_UNSIGNED (TREE_TYPE (orig_op0
));
3927 int op1_signed
= ! TREE_UNSIGNED (TREE_TYPE (orig_op1
));
3929 int unsignedp0
, unsignedp1
;
3930 tree primop0
= get_narrower (op0
, &unsignedp0
);
3931 tree primop1
= get_narrower (op1
, &unsignedp1
);
3933 /* Check for comparison of different enum types. */
3934 if (TREE_CODE (TREE_TYPE (orig_op0
)) == ENUMERAL_TYPE
3935 && TREE_CODE (TREE_TYPE (orig_op1
)) == ENUMERAL_TYPE
3936 && TYPE_MAIN_VARIANT (TREE_TYPE (orig_op0
))
3937 != TYPE_MAIN_VARIANT (TREE_TYPE (orig_op1
)))
3939 cp_warning ("comparison between types `%#T' and `%#T'",
3940 TREE_TYPE (orig_op0
), TREE_TYPE (orig_op1
));
3943 /* Give warnings for comparisons between signed and unsigned
3944 quantities that may fail. */
3945 /* Do the checking based on the original operand trees, so that
3946 casts will be considered, but default promotions won't be. */
3948 /* Do not warn if the comparison is being done in a signed type,
3949 since the signed type will only be chosen if it can represent
3950 all the values of the unsigned type. */
3951 if (! TREE_UNSIGNED (result_type
))
3953 /* Do not warn if both operands are unsigned. */
3954 else if (op0_signed
== op1_signed
)
3956 /* Do not warn if the signed quantity is an unsuffixed
3957 integer literal (or some static constant expression
3958 involving such literals or a conditional expression
3959 involving such literals) and it is non-negative. */
3960 else if ((op0_signed
&& tree_expr_nonnegative_p (orig_op0
))
3961 || (op1_signed
&& tree_expr_nonnegative_p (orig_op1
)))
3963 /* Do not warn if the comparison is an equality operation,
3964 the unsigned quantity is an integral constant and it does
3965 not use the most significant bit of result_type. */
3966 else if ((resultcode
== EQ_EXPR
|| resultcode
== NE_EXPR
)
3967 && ((op0_signed
&& TREE_CODE (orig_op1
) == INTEGER_CST
3968 && int_fits_type_p (orig_op1
,
3969 signed_type (result_type
)))
3970 || (op1_signed
&& TREE_CODE (orig_op0
) == INTEGER_CST
3971 && int_fits_type_p (orig_op0
,
3972 signed_type (result_type
)))))
3975 warning ("comparison between signed and unsigned integer expressions");
3977 /* Warn if two unsigned values are being compared in a size
3978 larger than their original size, and one (and only one) is the
3979 result of a `~' operator. This comparison will always fail.
3981 Also warn if one operand is a constant, and the constant does not
3982 have all bits set that are set in the ~ operand when it is
3985 if ((TREE_CODE (primop0
) == BIT_NOT_EXPR
)
3986 ^ (TREE_CODE (primop1
) == BIT_NOT_EXPR
))
3988 if (TREE_CODE (primop0
) == BIT_NOT_EXPR
)
3989 primop0
= get_narrower (TREE_OPERAND (op0
, 0), &unsignedp0
);
3990 if (TREE_CODE (primop1
) == BIT_NOT_EXPR
)
3991 primop1
= get_narrower (TREE_OPERAND (op1
, 0), &unsignedp1
);
3993 if (host_integerp (primop0
, 0) || host_integerp (primop1
, 0))
3996 HOST_WIDE_INT constant
, mask
;
4000 if (host_integerp (primop0
, 0))
4003 unsignedp
= unsignedp1
;
4004 constant
= tree_low_cst (primop0
, 0);
4009 unsignedp
= unsignedp0
;
4010 constant
= tree_low_cst (primop1
, 0);
4013 bits
= TYPE_PRECISION (TREE_TYPE (primop
));
4014 if (bits
< TYPE_PRECISION (result_type
)
4015 && bits
< HOST_BITS_PER_LONG
&& unsignedp
)
4017 mask
= (~ (HOST_WIDE_INT
) 0) << bits
;
4018 if ((mask
& constant
) != mask
)
4019 warning ("comparison of promoted ~unsigned with constant");
4022 else if (unsignedp0
&& unsignedp1
4023 && (TYPE_PRECISION (TREE_TYPE (primop0
))
4024 < TYPE_PRECISION (result_type
))
4025 && (TYPE_PRECISION (TREE_TYPE (primop1
))
4026 < TYPE_PRECISION (result_type
)))
4027 warning ("comparison of promoted ~unsigned with unsigned");
4032 /* At this point, RESULT_TYPE must be nonzero to avoid an error message.
4033 If CONVERTED is zero, both args will be converted to type RESULT_TYPE.
4034 Then the expression will be built.
4035 It will be given type FINAL_TYPE if that is nonzero;
4036 otherwise, it will be given type RESULT_TYPE. */
4040 cp_error ("invalid operands of types `%T' and `%T' to binary `%O'",
4041 TREE_TYPE (orig_op0
), TREE_TYPE (orig_op1
), code
);
4042 return error_mark_node
;
4045 /* Issue warnings about peculiar, but legal, uses of NULL. */
4046 if (/* It's reasonable to use pointer values as operands of &&
4047 and ||, so NULL is no exception. */
4048 !(code
== TRUTH_ANDIF_EXPR
|| code
== TRUTH_ORIF_EXPR
)
4049 && (/* If OP0 is NULL and OP1 is not a pointer, or vice versa. */
4050 (orig_op0
== null_node
4051 && TREE_CODE (TREE_TYPE (op1
)) != POINTER_TYPE
)
4052 /* Or vice versa. */
4053 || (orig_op1
== null_node
4054 && TREE_CODE (TREE_TYPE (op0
)) != POINTER_TYPE
)
4055 /* Or, both are NULL and the operation was not a comparison. */
4056 || (orig_op0
== null_node
&& orig_op1
== null_node
4057 && code
!= EQ_EXPR
&& code
!= NE_EXPR
)))
4058 /* Some sort of arithmetic operation involving NULL was
4059 performed. Note that pointer-difference and pointer-addition
4060 have already been handled above, and so we don't end up here in
4062 cp_warning ("NULL used in arithmetic");
4066 if (TREE_TYPE (op0
) != result_type
)
4067 op0
= cp_convert (result_type
, op0
);
4068 if (TREE_TYPE (op1
) != result_type
)
4069 op1
= cp_convert (result_type
, op1
);
4071 if (op0
== error_mark_node
|| op1
== error_mark_node
)
4072 return error_mark_node
;
4075 if (build_type
== NULL_TREE
)
4076 build_type
= result_type
;
4079 register tree result
= build (resultcode
, build_type
, op0
, op1
);
4080 register tree folded
;
4082 folded
= fold (result
);
4083 if (folded
== result
)
4084 TREE_CONSTANT (folded
) = TREE_CONSTANT (op0
) & TREE_CONSTANT (op1
);
4085 if (final_type
!= 0)
4086 return cp_convert (final_type
, folded
);
4091 /* Return a tree for the sum or difference (RESULTCODE says which)
4092 of pointer PTROP and integer INTOP. */
4095 pointer_int_sum (resultcode
, ptrop
, intop
)
4096 enum tree_code resultcode
;
4097 register tree ptrop
, intop
;
4101 register tree result
;
4102 register tree folded
= fold (intop
);
4104 /* The result is a pointer of the same type that is being added. */
4106 register tree result_type
= TREE_TYPE (ptrop
);
4108 if (!complete_type_or_else (result_type
, ptrop
))
4109 return error_mark_node
;
4111 if (TREE_CODE (TREE_TYPE (result_type
)) == VOID_TYPE
)
4113 if (pedantic
|| warn_pointer_arith
)
4114 pedwarn ("ISO C++ forbids using pointer of type `void *' in pointer arithmetic");
4115 size_exp
= integer_one_node
;
4117 else if (TREE_CODE (TREE_TYPE (result_type
)) == FUNCTION_TYPE
)
4119 if (pedantic
|| warn_pointer_arith
)
4120 pedwarn ("ISO C++ forbids using a pointer-to-function in pointer arithmetic");
4121 size_exp
= integer_one_node
;
4123 else if (TREE_CODE (TREE_TYPE (result_type
)) == METHOD_TYPE
)
4125 if (pedantic
|| warn_pointer_arith
)
4126 pedwarn ("ISO C++ forbids using a pointer to member function in pointer arithmetic");
4127 size_exp
= integer_one_node
;
4129 else if (TREE_CODE (TREE_TYPE (result_type
)) == OFFSET_TYPE
)
4131 if (pedantic
|| warn_pointer_arith
)
4132 pedwarn ("ISO C++ forbids using pointer to a member in pointer arithmetic");
4133 size_exp
= integer_one_node
;
4136 size_exp
= size_in_bytes (complete_type (TREE_TYPE (result_type
)));
4138 /* Needed to make OOPS V2R3 work. */
4140 if (integer_zerop (intop
))
4143 /* If what we are about to multiply by the size of the elements
4144 contains a constant term, apply distributive law
4145 and multiply that constant term separately.
4146 This helps produce common subexpressions. */
4148 if ((TREE_CODE (intop
) == PLUS_EXPR
|| TREE_CODE (intop
) == MINUS_EXPR
)
4149 && ! TREE_CONSTANT (intop
)
4150 && TREE_CONSTANT (TREE_OPERAND (intop
, 1))
4151 && TREE_CONSTANT (size_exp
))
4153 enum tree_code subcode
= resultcode
;
4154 if (TREE_CODE (intop
) == MINUS_EXPR
)
4155 subcode
= (subcode
== PLUS_EXPR
? MINUS_EXPR
: PLUS_EXPR
);
4156 ptrop
= cp_build_binary_op (subcode
, ptrop
, TREE_OPERAND (intop
, 1));
4157 intop
= TREE_OPERAND (intop
, 0);
4160 /* Convert the integer argument to a type the same size as sizetype
4161 so the multiply won't overflow spuriously. */
4163 if (TYPE_PRECISION (TREE_TYPE (intop
)) != TYPE_PRECISION (sizetype
))
4164 intop
= cp_convert (type_for_size (TYPE_PRECISION (sizetype
), 0), intop
);
4166 /* Replace the integer argument with a suitable product by the object size.
4167 Do this multiplication as signed, then convert to the appropriate
4168 pointer type (actually unsigned integral). */
4170 intop
= cp_convert (result_type
,
4171 cp_build_binary_op (MULT_EXPR
, intop
,
4172 cp_convert (TREE_TYPE (intop
),
4175 /* Create the sum or difference. */
4177 result
= build (resultcode
, result_type
, ptrop
, intop
);
4179 folded
= fold (result
);
4180 if (folded
== result
)
4181 TREE_CONSTANT (folded
) = TREE_CONSTANT (ptrop
) & TREE_CONSTANT (intop
);
4185 /* Return a tree for the difference of pointers OP0 and OP1.
4186 The resulting tree has type int. */
4189 pointer_diff (op0
, op1
, ptrtype
)
4190 register tree op0
, op1
;
4191 register tree ptrtype
;
4193 register tree result
, folded
;
4194 tree restype
= ptrdiff_type_node
;
4195 tree target_type
= TREE_TYPE (ptrtype
);
4197 if (!complete_type_or_else (target_type
, NULL_TREE
))
4198 return error_mark_node
;
4200 if (pedantic
|| warn_pointer_arith
)
4202 if (TREE_CODE (target_type
) == VOID_TYPE
)
4203 pedwarn ("ISO C++ forbids using pointer of type `void *' in subtraction");
4204 if (TREE_CODE (target_type
) == FUNCTION_TYPE
)
4205 pedwarn ("ISO C++ forbids using pointer to a function in subtraction");
4206 if (TREE_CODE (target_type
) == METHOD_TYPE
)
4207 pedwarn ("ISO C++ forbids using pointer to a method in subtraction");
4208 if (TREE_CODE (target_type
) == OFFSET_TYPE
)
4209 pedwarn ("ISO C++ forbids using pointer to a member in subtraction");
4212 /* First do the subtraction as integers;
4213 then drop through to build the divide operator. */
4215 op0
= cp_build_binary_op (MINUS_EXPR
,
4216 cp_convert (restype
, op0
),
4217 cp_convert (restype
, op1
));
4219 /* This generates an error if op1 is a pointer to an incomplete type. */
4220 if (!COMPLETE_TYPE_P (TREE_TYPE (TREE_TYPE (op1
))))
4221 error ("invalid use of a pointer to an incomplete type in pointer arithmetic");
4223 op1
= ((TREE_CODE (target_type
) == VOID_TYPE
4224 || TREE_CODE (target_type
) == FUNCTION_TYPE
4225 || TREE_CODE (target_type
) == METHOD_TYPE
4226 || TREE_CODE (target_type
) == OFFSET_TYPE
)
4228 : size_in_bytes (target_type
));
4230 /* Do the division. */
4232 result
= build (EXACT_DIV_EXPR
, restype
, op0
, cp_convert (restype
, op1
));
4234 folded
= fold (result
);
4235 if (folded
== result
)
4236 TREE_CONSTANT (folded
) = TREE_CONSTANT (op0
) & TREE_CONSTANT (op1
);
4240 /* Handle the case of taking the address of a COMPONENT_REF.
4241 Called by `build_unary_op'.
4243 ARG is the COMPONENT_REF whose address we want.
4244 ARGTYPE is the pointer type that this address should have. */
4247 build_component_addr (arg
, argtype
)
4250 tree field
= TREE_OPERAND (arg
, 1);
4251 tree basetype
= decl_type_context (field
);
4252 tree rval
= build_unary_op (ADDR_EXPR
, TREE_OPERAND (arg
, 0), 0);
4254 my_friendly_assert (TREE_CODE (field
) == FIELD_DECL
, 981018);
4256 if (DECL_C_BIT_FIELD (field
))
4258 cp_error ("attempt to take address of bit-field structure member `%D'",
4260 return error_mark_node
;
4263 if (TREE_CODE (field
) == FIELD_DECL
4264 && TYPE_BASE_CONVS_MAY_REQUIRE_CODE_P (basetype
))
4266 /* Can't convert directly to ARGTYPE, since that
4267 may have the same pointer type as one of our
4269 rval
= build1 (NOP_EXPR
, argtype
,
4270 convert_pointer_to (basetype
, rval
));
4271 TREE_CONSTANT (rval
) = TREE_CONSTANT (TREE_OPERAND (rval
, 0));
4274 /* This conversion is harmless. */
4275 rval
= convert_force (argtype
, rval
, 0);
4277 return fold (build (PLUS_EXPR
, argtype
, rval
,
4278 cp_convert (argtype
, byte_position (field
))));
4281 /* Construct and perhaps optimize a tree representation
4282 for a unary operation. CODE, a tree_code, specifies the operation
4283 and XARG is the operand. */
4286 build_x_unary_op (code
, xarg
)
4287 enum tree_code code
;
4293 if (processing_template_decl
)
4294 return build_min_nt (code
, xarg
, NULL_TREE
);
4296 /* & rec, on incomplete RECORD_TYPEs is the simple opr &, not an
4298 if (code
== ADDR_EXPR
4299 && TREE_CODE (xarg
) != TEMPLATE_ID_EXPR
4300 && ((IS_AGGR_TYPE_CODE (TREE_CODE (TREE_TYPE (xarg
)))
4301 && !COMPLETE_TYPE_P (TREE_TYPE (xarg
)))
4302 || (TREE_CODE (xarg
) == OFFSET_REF
)))
4303 /* don't look for a function */;
4308 rval
= build_new_op (code
, LOOKUP_NORMAL
, xarg
,
4309 NULL_TREE
, NULL_TREE
);
4310 if (rval
|| code
!= ADDR_EXPR
)
4313 if (code
== ADDR_EXPR
)
4315 if (TREE_CODE (xarg
) == OFFSET_REF
)
4317 ptrmem
= PTRMEM_OK_P (xarg
);
4319 if (!ptrmem
&& !flag_ms_extensions
4320 && TREE_CODE (TREE_TYPE (TREE_OPERAND (xarg
, 1))) == METHOD_TYPE
)
4321 /* A single non-static member, make sure we don't allow a
4322 pointer-to-member. */
4323 xarg
= ovl_cons (TREE_OPERAND (xarg
, 1), NULL_TREE
);
4325 else if (TREE_CODE (xarg
) == TARGET_EXPR
)
4326 warning ("taking address of temporary");
4328 exp
= build_unary_op (code
, xarg
, 0);
4329 if (TREE_CODE (exp
) == ADDR_EXPR
)
4330 PTRMEM_OK_P (exp
) = ptrmem
;
4335 /* Like truthvalue_conversion, but handle pointer-to-member constants, where
4336 a null value is represented by an INTEGER_CST of -1. */
4339 cp_truthvalue_conversion (expr
)
4342 tree type
= TREE_TYPE (expr
);
4343 if (TYPE_PTRMEM_P (type
))
4344 return build_binary_op (NE_EXPR
, expr
, integer_zero_node
, 1);
4346 return truthvalue_conversion (expr
);
4349 /* Just like cp_truthvalue_conversion, but we want a CLEANUP_POINT_EXPR. */
4352 condition_conversion (expr
)
4356 if (processing_template_decl
)
4358 t
= perform_implicit_conversion (boolean_type_node
, expr
);
4359 t
= fold (build1 (CLEANUP_POINT_EXPR
, boolean_type_node
, t
));
4363 /* C++: Must handle pointers to members.
4365 Perhaps type instantiation should be extended to handle conversion
4366 from aggregates to types we don't yet know we want? (Or are those
4367 cases typically errors which should be reported?)
4369 NOCONVERT nonzero suppresses the default promotions
4370 (such as from short to int). */
4373 build_unary_op (code
, xarg
, noconvert
)
4374 enum tree_code code
;
4378 /* No default_conversion here. It causes trouble for ADDR_EXPR. */
4379 register tree arg
= xarg
;
4380 register tree argtype
= 0;
4381 const char *errstring
= NULL
;
4384 if (arg
== error_mark_node
)
4385 return error_mark_node
;
4390 /* This is used for unary plus, because a CONVERT_EXPR
4391 is enough to prevent anybody from looking inside for
4392 associativity, but won't generate any code. */
4393 if (!(arg
= build_expr_type_conversion
4394 (WANT_ARITH
| WANT_ENUM
| WANT_POINTER
, arg
, 1)))
4395 errstring
= "wrong type argument to unary plus";
4399 arg
= default_conversion (arg
);
4400 arg
= build1 (NON_LVALUE_EXPR
, TREE_TYPE (arg
), arg
);
4401 TREE_CONSTANT (arg
) = TREE_CONSTANT (TREE_OPERAND (arg
, 0));
4406 if (!(arg
= build_expr_type_conversion (WANT_ARITH
| WANT_ENUM
, arg
, 1)))
4407 errstring
= "wrong type argument to unary minus";
4408 else if (!noconvert
)
4409 arg
= default_conversion (arg
);
4413 if (TREE_CODE (TREE_TYPE (arg
)) == COMPLEX_TYPE
)
4417 arg
= default_conversion (arg
);
4419 else if (!(arg
= build_expr_type_conversion (WANT_INT
| WANT_ENUM
,
4421 errstring
= "wrong type argument to bit-complement";
4422 else if (!noconvert
)
4423 arg
= default_conversion (arg
);
4427 if (!(arg
= build_expr_type_conversion (WANT_ARITH
| WANT_ENUM
, arg
, 1)))
4428 errstring
= "wrong type argument to abs";
4429 else if (!noconvert
)
4430 arg
= default_conversion (arg
);
4434 /* Conjugating a real value is a no-op, but allow it anyway. */
4435 if (!(arg
= build_expr_type_conversion (WANT_ARITH
| WANT_ENUM
, arg
, 1)))
4436 errstring
= "wrong type argument to conjugation";
4437 else if (!noconvert
)
4438 arg
= default_conversion (arg
);
4441 case TRUTH_NOT_EXPR
:
4442 arg
= cp_convert (boolean_type_node
, arg
);
4443 val
= invert_truthvalue (arg
);
4444 if (arg
!= error_mark_node
)
4446 errstring
= "in argument to unary !";
4453 if (TREE_CODE (arg
) == COMPLEX_CST
)
4454 return TREE_REALPART (arg
);
4455 else if (TREE_CODE (TREE_TYPE (arg
)) == COMPLEX_TYPE
)
4456 return fold (build1 (REALPART_EXPR
, TREE_TYPE (TREE_TYPE (arg
)), arg
));
4461 if (TREE_CODE (arg
) == COMPLEX_CST
)
4462 return TREE_IMAGPART (arg
);
4463 else if (TREE_CODE (TREE_TYPE (arg
)) == COMPLEX_TYPE
)
4464 return fold (build1 (IMAGPART_EXPR
, TREE_TYPE (TREE_TYPE (arg
)), arg
));
4466 return cp_convert (TREE_TYPE (arg
), integer_zero_node
);
4468 case PREINCREMENT_EXPR
:
4469 case POSTINCREMENT_EXPR
:
4470 case PREDECREMENT_EXPR
:
4471 case POSTDECREMENT_EXPR
:
4472 /* Handle complex lvalues (when permitted)
4473 by reduction to simpler cases. */
4475 val
= unary_complex_lvalue (code
, arg
);
4479 /* Increment or decrement the real part of the value,
4480 and don't change the imaginary part. */
4481 if (TREE_CODE (TREE_TYPE (arg
)) == COMPLEX_TYPE
)
4485 arg
= stabilize_reference (arg
);
4486 real
= build_unary_op (REALPART_EXPR
, arg
, 1);
4487 imag
= build_unary_op (IMAGPART_EXPR
, arg
, 1);
4488 return build (COMPLEX_EXPR
, TREE_TYPE (arg
),
4489 build_unary_op (code
, real
, 1), imag
);
4492 /* Report invalid types. */
4494 if (!(arg
= build_expr_type_conversion (WANT_ARITH
| WANT_POINTER
,
4497 if (code
== PREINCREMENT_EXPR
)
4498 errstring
="no pre-increment operator for type";
4499 else if (code
== POSTINCREMENT_EXPR
)
4500 errstring
="no post-increment operator for type";
4501 else if (code
== PREDECREMENT_EXPR
)
4502 errstring
="no pre-decrement operator for type";
4504 errstring
="no post-decrement operator for type";
4508 /* Report something read-only. */
4510 if (CP_TYPE_CONST_P (TREE_TYPE (arg
))
4511 || TREE_READONLY (arg
))
4512 readonly_error (arg
, ((code
== PREINCREMENT_EXPR
4513 || code
== POSTINCREMENT_EXPR
)
4514 ? "increment" : "decrement"),
4519 tree result_type
= TREE_TYPE (arg
);
4521 arg
= get_unwidened (arg
, 0);
4522 argtype
= TREE_TYPE (arg
);
4524 /* ARM $5.2.5 last annotation says this should be forbidden. */
4525 if (TREE_CODE (argtype
) == ENUMERAL_TYPE
)
4526 pedwarn ("ISO C++ forbids %sing an enum",
4527 (code
== PREINCREMENT_EXPR
|| code
== POSTINCREMENT_EXPR
)
4528 ? "increment" : "decrement");
4530 /* Compute the increment. */
4532 if (TREE_CODE (argtype
) == POINTER_TYPE
)
4534 enum tree_code tmp
= TREE_CODE (TREE_TYPE (argtype
));
4535 tree type
= complete_type (TREE_TYPE (argtype
));
4537 if (!COMPLETE_OR_VOID_TYPE_P (type
))
4538 cp_error ("cannot %s a pointer to incomplete type `%T'",
4539 ((code
== PREINCREMENT_EXPR
4540 || code
== POSTINCREMENT_EXPR
)
4541 ? "increment" : "decrement"), TREE_TYPE (argtype
));
4542 else if ((pedantic
|| warn_pointer_arith
)
4543 && (tmp
== FUNCTION_TYPE
|| tmp
== METHOD_TYPE
4544 || tmp
== VOID_TYPE
|| tmp
== OFFSET_TYPE
))
4545 cp_pedwarn ("ISO C++ forbids %sing a pointer of type `%T'",
4546 ((code
== PREINCREMENT_EXPR
4547 || code
== POSTINCREMENT_EXPR
)
4548 ? "increment" : "decrement"), argtype
);
4549 inc
= c_sizeof_nowarn (TREE_TYPE (argtype
));
4552 inc
= integer_one_node
;
4554 inc
= cp_convert (argtype
, inc
);
4556 /* Handle incrementing a cast-expression. */
4558 switch (TREE_CODE (arg
))
4563 case FIX_TRUNC_EXPR
:
4564 case FIX_FLOOR_EXPR
:
4565 case FIX_ROUND_EXPR
:
4568 tree incremented
, modify
, value
, compound
;
4569 if (! lvalue_p (arg
) && pedantic
)
4570 pedwarn ("cast to non-reference type used as lvalue");
4571 arg
= stabilize_reference (arg
);
4572 if (code
== PREINCREMENT_EXPR
|| code
== PREDECREMENT_EXPR
)
4575 value
= save_expr (arg
);
4576 incremented
= build (((code
== PREINCREMENT_EXPR
4577 || code
== POSTINCREMENT_EXPR
)
4578 ? PLUS_EXPR
: MINUS_EXPR
),
4579 argtype
, value
, inc
);
4581 modify
= build_modify_expr (arg
, NOP_EXPR
, incremented
);
4582 compound
= build (COMPOUND_EXPR
, TREE_TYPE (arg
), modify
, value
);
4584 /* Eliminate warning about unused result of + or -. */
4585 TREE_NO_UNUSED_WARNING (compound
) = 1;
4593 /* Complain about anything else that is not a true lvalue. */
4594 if (!lvalue_or_else (arg
, ((code
== PREINCREMENT_EXPR
4595 || code
== POSTINCREMENT_EXPR
)
4596 ? "increment" : "decrement")))
4597 return error_mark_node
;
4599 /* Forbid using -- on `bool'. */
4600 if (TREE_TYPE (arg
) == boolean_type_node
)
4602 if (code
== POSTDECREMENT_EXPR
|| code
== PREDECREMENT_EXPR
)
4604 cp_error ("invalid use of `--' on bool variable `%D'", arg
);
4605 return error_mark_node
;
4608 /* This will only work if someone can convince Kenner to accept
4609 my patch to expand_increment. (jason) */
4610 val
= build (code
, TREE_TYPE (arg
), arg
, inc
);
4612 val
= boolean_increment (code
, arg
);
4616 val
= build (code
, TREE_TYPE (arg
), arg
, inc
);
4618 TREE_SIDE_EFFECTS (val
) = 1;
4619 return cp_convert (result_type
, val
);
4623 /* Note that this operation never does default_conversion
4624 regardless of NOCONVERT. */
4626 argtype
= lvalue_type (arg
);
4627 if (TREE_CODE (argtype
) == REFERENCE_TYPE
)
4631 build_pointer_type (TREE_TYPE (argtype
)), arg
);
4632 TREE_CONSTANT (arg
) = TREE_CONSTANT (TREE_OPERAND (arg
, 0));
4635 else if (pedantic
&& DECL_MAIN_P (arg
))
4637 pedwarn ("ISO C++ forbids taking address of function `::main'");
4639 /* Let &* cancel out to simplify resulting code. */
4640 if (TREE_CODE (arg
) == INDIRECT_REF
)
4642 /* We don't need to have `current_class_ptr' wrapped in a
4643 NON_LVALUE_EXPR node. */
4644 if (arg
== current_class_ref
)
4645 return current_class_ptr
;
4647 arg
= TREE_OPERAND (arg
, 0);
4648 if (TREE_CODE (TREE_TYPE (arg
)) == REFERENCE_TYPE
)
4652 build_pointer_type (TREE_TYPE (TREE_TYPE (arg
))), arg
);
4653 TREE_CONSTANT (arg
) = TREE_CONSTANT (TREE_OPERAND (arg
, 0));
4655 else if (lvalue_p (arg
))
4656 /* Don't let this be an lvalue. */
4657 return non_lvalue (arg
);
4661 /* For &x[y], return x+y */
4662 if (TREE_CODE (arg
) == ARRAY_REF
)
4664 if (mark_addressable (TREE_OPERAND (arg
, 0)) == 0)
4665 return error_mark_node
;
4666 return cp_build_binary_op (PLUS_EXPR
, TREE_OPERAND (arg
, 0),
4667 TREE_OPERAND (arg
, 1));
4670 /* Uninstantiated types are all functions. Taking the
4671 address of a function is a no-op, so just return the
4674 if (TREE_CODE (arg
) == IDENTIFIER_NODE
4675 && IDENTIFIER_OPNAME_P (arg
))
4677 my_friendly_abort (117);
4678 /* We don't know the type yet, so just work around the problem.
4679 We know that this will resolve to an lvalue. */
4680 return build1 (ADDR_EXPR
, unknown_type_node
, arg
);
4683 if (TREE_CODE (arg
) == COMPONENT_REF
&& type_unknown_p (arg
)
4684 && OVL_NEXT (TREE_OPERAND (arg
, 1)) == NULL_TREE
)
4686 /* They're trying to take the address of a unique non-static
4687 member function. This is ill-formed (except in MS-land),
4688 but let's try to DTRT.
4689 Note: We only handle unique functions here because we don't
4690 want to complain if there's a static overload; non-unique
4691 cases will be handled by instantiate_type. But we need to
4692 handle this case here to allow casts on the resulting PMF.
4693 We could defer this in non-MS mode, but it's easier to give
4694 a useful error here. */
4696 tree base
= TREE_TYPE (TREE_OPERAND (arg
, 0));
4697 tree name
= DECL_NAME (OVL_CURRENT (TREE_OPERAND (arg
, 1)));
4699 if (! flag_ms_extensions
)
4701 if (current_class_type
4702 && TREE_OPERAND (arg
, 0) == current_class_ref
)
4703 /* An expression like &memfn. */
4704 cp_pedwarn ("ISO C++ forbids taking the address of an unqualified non-static member function to form a pointer to member function. Say `&%T::%D'", base
, name
);
4706 cp_pedwarn ("ISO C++ forbids taking the address of a bound member function to form a pointer to member function. Say `&%T::%D'", base
, name
);
4708 arg
= build_offset_ref (base
, name
);
4711 if (type_unknown_p (arg
))
4712 return build1 (ADDR_EXPR
, unknown_type_node
, arg
);
4714 /* Handle complex lvalues (when permitted)
4715 by reduction to simpler cases. */
4716 val
= unary_complex_lvalue (code
, arg
);
4720 switch (TREE_CODE (arg
))
4725 case FIX_TRUNC_EXPR
:
4726 case FIX_FLOOR_EXPR
:
4727 case FIX_ROUND_EXPR
:
4729 if (! lvalue_p (arg
) && pedantic
)
4730 pedwarn ("ISO C++ forbids taking the address of a cast to a non-lvalue expression");
4737 /* Allow the address of a constructor if all the elements
4739 if (TREE_CODE (arg
) == CONSTRUCTOR
&& TREE_HAS_CONSTRUCTOR (arg
)
4740 && TREE_CONSTANT (arg
))
4742 /* Anything not already handled and not a true memory reference
4744 else if (TREE_CODE (argtype
) != FUNCTION_TYPE
4745 && TREE_CODE (argtype
) != METHOD_TYPE
4746 && !lvalue_or_else (arg
, "unary `&'"))
4747 return error_mark_node
;
4749 if (argtype
!= error_mark_node
)
4750 argtype
= build_pointer_type (argtype
);
4752 if (mark_addressable (arg
) == 0)
4753 return error_mark_node
;
4758 if (TREE_CODE (arg
) == COMPONENT_REF
)
4759 addr
= build_component_addr (arg
, argtype
);
4761 addr
= build1 (ADDR_EXPR
, argtype
, arg
);
4763 /* Address of a static or external variable or
4764 function counts as a constant */
4766 TREE_CONSTANT (addr
) = 1;
4768 if (TREE_CODE (argtype
) == POINTER_TYPE
4769 && TREE_CODE (TREE_TYPE (argtype
)) == METHOD_TYPE
)
4771 build_ptrmemfunc_type (argtype
);
4772 addr
= build_ptrmemfunc (argtype
, addr
, 0);
4785 argtype
= TREE_TYPE (arg
);
4786 return fold (build1 (code
, argtype
, arg
));
4789 error ("%s", errstring
);
4790 return error_mark_node
;
4793 /* Apply unary lvalue-demanding operator CODE to the expression ARG
4794 for certain kinds of expressions which are not really lvalues
4795 but which we can accept as lvalues.
4797 If ARG is not a kind of expression we can handle, return zero. */
4800 unary_complex_lvalue (code
, arg
)
4801 enum tree_code code
;
4804 /* Handle (a, b) used as an "lvalue". */
4805 if (TREE_CODE (arg
) == COMPOUND_EXPR
)
4807 tree real_result
= build_unary_op (code
, TREE_OPERAND (arg
, 1), 0);
4808 return build (COMPOUND_EXPR
, TREE_TYPE (real_result
),
4809 TREE_OPERAND (arg
, 0), real_result
);
4812 /* Handle (a ? b : c) used as an "lvalue". */
4813 if (TREE_CODE (arg
) == COND_EXPR
4814 || TREE_CODE (arg
) == MIN_EXPR
|| TREE_CODE (arg
) == MAX_EXPR
)
4815 return rationalize_conditional_expr (code
, arg
);
4817 if (TREE_CODE (arg
) == MODIFY_EXPR
4818 || TREE_CODE (arg
) == PREINCREMENT_EXPR
4819 || TREE_CODE (arg
) == PREDECREMENT_EXPR
)
4820 return unary_complex_lvalue
4821 (code
, build (COMPOUND_EXPR
, TREE_TYPE (TREE_OPERAND (arg
, 0)),
4822 arg
, TREE_OPERAND (arg
, 0)));
4824 if (code
!= ADDR_EXPR
)
4827 /* Handle (a = b) used as an "lvalue" for `&'. */
4828 if (TREE_CODE (arg
) == MODIFY_EXPR
4829 || TREE_CODE (arg
) == INIT_EXPR
)
4831 tree real_result
= build_unary_op (code
, TREE_OPERAND (arg
, 0), 0);
4832 arg
= build (COMPOUND_EXPR
, TREE_TYPE (real_result
), arg
, real_result
);
4833 TREE_NO_UNUSED_WARNING (arg
) = 1;
4837 if (TREE_CODE (TREE_TYPE (arg
)) == FUNCTION_TYPE
4838 || TREE_CODE (TREE_TYPE (arg
)) == METHOD_TYPE
4839 || TREE_CODE (TREE_TYPE (arg
)) == OFFSET_TYPE
)
4841 /* The representation of something of type OFFSET_TYPE
4842 is really the representation of a pointer to it.
4843 Here give the representation its true type. */
4846 my_friendly_assert (TREE_CODE (arg
) != SCOPE_REF
, 313);
4848 if (TREE_CODE (arg
) != OFFSET_REF
)
4851 t
= TREE_OPERAND (arg
, 1);
4853 /* Check all this code for right semantics. */
4854 if (TREE_CODE (t
) == FUNCTION_DECL
)
4856 if (DECL_DESTRUCTOR_P (t
))
4857 cp_error ("taking address of destructor");
4858 return build_unary_op (ADDR_EXPR
, t
, 0);
4860 if (TREE_CODE (t
) == VAR_DECL
)
4861 return build_unary_op (ADDR_EXPR
, t
, 0);
4866 if (TREE_OPERAND (arg
, 0)
4867 && ! is_dummy_object (TREE_OPERAND (arg
, 0))
4868 && TREE_CODE (t
) != FIELD_DECL
)
4870 cp_error ("taking address of bound pointer-to-member expression");
4871 return error_mark_node
;
4874 type
= build_offset_type (DECL_FIELD_CONTEXT (t
), TREE_TYPE (t
));
4875 type
= build_pointer_type (type
);
4877 t
= make_ptrmem_cst (type
, TREE_OPERAND (arg
, 1));
4883 /* We permit compiler to make function calls returning
4884 objects of aggregate type look like lvalues. */
4888 if (TREE_CODE (targ
) == SAVE_EXPR
)
4889 targ
= TREE_OPERAND (targ
, 0);
4891 if (TREE_CODE (targ
) == CALL_EXPR
&& IS_AGGR_TYPE (TREE_TYPE (targ
)))
4893 if (TREE_CODE (arg
) == SAVE_EXPR
)
4896 targ
= build_cplus_new (TREE_TYPE (arg
), arg
);
4897 return build1 (ADDR_EXPR
, build_pointer_type (TREE_TYPE (arg
)), targ
);
4900 if (TREE_CODE (arg
) == SAVE_EXPR
&& TREE_CODE (targ
) == INDIRECT_REF
)
4901 return build (SAVE_EXPR
, build_pointer_type (TREE_TYPE (arg
)),
4902 TREE_OPERAND (targ
, 0), current_function_decl
, NULL
);
4905 /* Don't let anything else be handled specially. */
4909 /* Mark EXP saying that we need to be able to take the
4910 address of it; it should not be allocated in a register.
4911 Value is 1 if successful.
4913 C++: we do not allow `current_class_ptr' to be addressable. */
4916 mark_addressable (exp
)
4919 register tree x
= exp
;
4921 if (TREE_ADDRESSABLE (x
) == 1)
4925 switch (TREE_CODE (x
))
4932 x
= TREE_OPERAND (x
, 0);
4936 if (x
== current_class_ptr
)
4938 error ("cannot take the address of `this', which is an rvalue expression");
4939 TREE_ADDRESSABLE (x
) = 1; /* so compiler doesn't die later */
4943 /* Caller should not be trying to mark initialized
4944 constant fields addressable. */
4945 my_friendly_assert (DECL_LANG_SPECIFIC (x
) == 0
4946 || DECL_IN_AGGR_P (x
) == 0
4948 || DECL_EXTERNAL (x
), 314);
4952 if (DECL_REGISTER (x
) && !TREE_ADDRESSABLE (x
)
4953 && !DECL_ARTIFICIAL (x
) && extra_warnings
)
4954 cp_warning ("address requested for `%D', which is declared `register'",
4956 TREE_ADDRESSABLE (x
) = 1;
4960 TREE_ADDRESSABLE (x
) = 1;
4961 TREE_ADDRESSABLE (DECL_ASSEMBLER_NAME (x
)) = 1;
4965 TREE_ADDRESSABLE (x
) = 1;
4969 TREE_ADDRESSABLE (x
) = 1;
4970 mark_addressable (TREE_OPERAND (x
, 0));
4978 /* Build and return a conditional expression IFEXP ? OP1 : OP2. */
4981 build_x_conditional_expr (ifexp
, op1
, op2
)
4982 tree ifexp
, op1
, op2
;
4984 if (processing_template_decl
)
4985 return build_min_nt (COND_EXPR
, ifexp
, op1
, op2
);
4987 return build_conditional_expr (ifexp
, op1
, op2
);
4990 /* Handle overloading of the ',' operator when needed. Otherwise,
4991 this function just builds an expression list. */
4994 build_x_compound_expr (list
)
4997 tree rest
= TREE_CHAIN (list
);
5000 if (processing_template_decl
)
5001 return build_min_nt (COMPOUND_EXPR
, list
, NULL_TREE
);
5003 if (rest
== NULL_TREE
)
5004 return build_compound_expr (list
);
5006 result
= build_opfncall (COMPOUND_EXPR
, LOOKUP_NORMAL
,
5007 TREE_VALUE (list
), TREE_VALUE (rest
), NULL_TREE
);
5009 return build_x_compound_expr (tree_cons (NULL_TREE
, result
,
5010 TREE_CHAIN (rest
)));
5012 if (! TREE_SIDE_EFFECTS (TREE_VALUE (list
)))
5014 /* FIXME: This test should be in the implicit cast to void of the LHS. */
5015 /* the left-hand operand of a comma expression is like an expression
5016 statement: we should warn if it doesn't have any side-effects,
5017 unless it was explicitly cast to (void). */
5018 if ((extra_warnings
|| warn_unused_value
)
5019 && !(TREE_CODE (TREE_VALUE(list
)) == CONVERT_EXPR
5020 && VOID_TYPE_P (TREE_TYPE (TREE_VALUE(list
)))))
5021 warning("left-hand operand of comma expression has no effect");
5023 #if 0 /* this requires a gcc backend patch to export warn_if_unused_value */
5024 else if (warn_unused_value
)
5025 warn_if_unused_value (TREE_VALUE(list
));
5028 return build_compound_expr
5029 (tree_cons (NULL_TREE
, TREE_VALUE (list
),
5030 build_tree_list (NULL_TREE
,
5031 build_x_compound_expr (rest
))));
5034 /* Given a list of expressions, return a compound expression
5035 that performs them all and returns the value of the last of them. */
5038 build_compound_expr (list
)
5044 TREE_VALUE (list
) = decl_constant_value (TREE_VALUE (list
));
5046 if (TREE_CHAIN (list
) == 0)
5048 /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
5049 Strip such NOP_EXPRs, since LIST is used in non-lvalue context. */
5050 if (TREE_CODE (list
) == NOP_EXPR
5051 && TREE_TYPE (list
) == TREE_TYPE (TREE_OPERAND (list
, 0)))
5052 list
= TREE_OPERAND (list
, 0);
5054 return TREE_VALUE (list
);
5057 first
= TREE_VALUE (list
);
5058 first
= convert_to_void (first
, "left-hand operand of comma");
5059 if (first
== error_mark_node
)
5060 return error_mark_node
;
5062 rest
= build_compound_expr (TREE_CHAIN (list
));
5063 if (rest
== error_mark_node
)
5064 return error_mark_node
;
5066 /* When pedantic, a compound expression cannot be a constant expression. */
5067 if (! TREE_SIDE_EFFECTS (first
) && ! pedantic
)
5070 return build (COMPOUND_EXPR
, TREE_TYPE (rest
), first
, rest
);
5074 build_static_cast (type
, expr
)
5080 if (type
== error_mark_node
|| expr
== error_mark_node
)
5081 return error_mark_node
;
5083 if (TREE_CODE (expr
) == OFFSET_REF
)
5084 expr
= resolve_offset_ref (expr
);
5086 if (processing_template_decl
)
5088 tree t
= build_min (STATIC_CAST_EXPR
, type
, expr
);
5092 /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
5093 Strip such NOP_EXPRs if VALUE is being used in non-lvalue context. */
5094 if (TREE_CODE (type
) != REFERENCE_TYPE
5095 && TREE_CODE (expr
) == NOP_EXPR
5096 && TREE_TYPE (expr
) == TREE_TYPE (TREE_OPERAND (expr
, 0)))
5097 expr
= TREE_OPERAND (expr
, 0);
5099 if (TREE_CODE (type
) == VOID_TYPE
)
5101 expr
= convert_to_void (expr
, /*implicit=*/NULL
);
5105 if (TREE_CODE (type
) == REFERENCE_TYPE
)
5106 return (convert_from_reference
5107 (convert_to_reference (type
, expr
, CONV_STATIC
|CONV_IMPLICIT
,
5108 LOOKUP_COMPLAIN
, NULL_TREE
)));
5110 if (IS_AGGR_TYPE (type
))
5111 return build_cplus_new (type
, (build_method_call
5112 (NULL_TREE
, complete_ctor_identifier
,
5113 build_tree_list (NULL_TREE
, expr
),
5114 TYPE_BINFO (type
), LOOKUP_NORMAL
)));
5116 expr
= decay_conversion (expr
);
5117 intype
= TREE_TYPE (expr
);
5119 /* FIXME handle casting to array type. */
5122 if (IS_AGGR_TYPE (intype
)
5123 ? can_convert_arg (type
, intype
, expr
)
5124 : can_convert_arg (strip_all_pointer_quals (type
),
5125 strip_all_pointer_quals (intype
), expr
))
5126 /* This is a standard conversion. */
5128 else if (TYPE_PTROB_P (type
) && TYPE_PTROB_P (intype
))
5130 /* They're pointers to objects. They must be aggregates that
5131 are related non-virtually. */
5135 if (IS_AGGR_TYPE (TREE_TYPE (type
)) && IS_AGGR_TYPE (TREE_TYPE (intype
))
5136 && (binfo
= get_binfo (TREE_TYPE (intype
), TREE_TYPE (type
), 0))
5137 && !binfo_from_vbase (binfo
))
5140 else if (TREE_CODE (intype
) != BOOLEAN_TYPE
5141 && TREE_CODE (type
) != ARRAY_TYPE
5142 && TREE_CODE (type
) != FUNCTION_TYPE
5143 && can_convert (intype
, strip_all_pointer_quals (type
)))
5145 else if (TREE_CODE (intype
) == ENUMERAL_TYPE
5146 && TREE_CODE (type
) == ENUMERAL_TYPE
)
5147 /* DR 128: "A value of integral _or enumeration_ type can be explicitly
5148 converted to an enumeration type."
5149 The integral to enumeration will be accepted by the previous clause.
5150 We need to explicitly check for enumeration to enumeration. */
5153 /* [expr.static.cast]
5155 The static_cast operator shall not be used to cast away
5157 if (ok
&& casts_away_constness (intype
, type
))
5159 cp_error ("static_cast from type `%T' to type `%T' casts away constness",
5161 return error_mark_node
;
5165 return build_c_cast (type
, expr
);
5167 cp_error ("invalid static_cast from type `%T' to type `%T'", intype
, type
);
5168 return error_mark_node
;
5172 build_reinterpret_cast (type
, expr
)
5177 if (type
== error_mark_node
|| expr
== error_mark_node
)
5178 return error_mark_node
;
5180 if (TREE_CODE (expr
) == OFFSET_REF
)
5181 expr
= resolve_offset_ref (expr
);
5183 if (processing_template_decl
)
5185 tree t
= build_min (REINTERPRET_CAST_EXPR
, type
, expr
);
5189 if (TREE_CODE (type
) != REFERENCE_TYPE
)
5191 expr
= decay_conversion (expr
);
5193 /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
5194 Strip such NOP_EXPRs if VALUE is being used in non-lvalue context. */
5195 if (TREE_CODE (expr
) == NOP_EXPR
5196 && TREE_TYPE (expr
) == TREE_TYPE (TREE_OPERAND (expr
, 0)))
5197 expr
= TREE_OPERAND (expr
, 0);
5200 intype
= TREE_TYPE (expr
);
5202 if (TREE_CODE (type
) == REFERENCE_TYPE
)
5204 if (! real_lvalue_p (expr
))
5206 cp_error ("invalid reinterpret_cast of an rvalue expression of type `%T' to type `%T'", intype
, type
);
5207 return error_mark_node
;
5209 expr
= build_unary_op (ADDR_EXPR
, expr
, 0);
5210 if (expr
!= error_mark_node
)
5211 expr
= build_reinterpret_cast
5212 (build_pointer_type (TREE_TYPE (type
)), expr
);
5213 if (expr
!= error_mark_node
)
5214 expr
= build_indirect_ref (expr
, 0);
5217 else if (same_type_ignoring_top_level_qualifiers_p (intype
, type
))
5218 return build_static_cast (type
, expr
);
5220 if (TYPE_PTR_P (type
) && (TREE_CODE (intype
) == INTEGER_TYPE
5221 || TREE_CODE (intype
) == ENUMERAL_TYPE
))
5223 else if (TREE_CODE (type
) == INTEGER_TYPE
&& TYPE_PTR_P (intype
))
5225 if (TYPE_PRECISION (type
) < TYPE_PRECISION (intype
))
5226 cp_pedwarn ("reinterpret_cast from `%T' to `%T' loses precision",
5229 else if ((TYPE_PTRFN_P (type
) && TYPE_PTRFN_P (intype
))
5230 || (TYPE_PTRMEMFUNC_P (type
) && TYPE_PTRMEMFUNC_P (intype
)))
5232 expr
= decl_constant_value (expr
);
5233 return fold (build1 (NOP_EXPR
, type
, expr
));
5235 else if ((TYPE_PTRMEM_P (type
) && TYPE_PTRMEM_P (intype
))
5236 || (TYPE_PTROBV_P (type
) && TYPE_PTROBV_P (intype
)))
5238 if (! comp_ptr_ttypes_reinterpret (TREE_TYPE (type
), TREE_TYPE (intype
)))
5239 cp_pedwarn ("reinterpret_cast from `%T' to `%T' casts away const (or volatile)",
5242 expr
= decl_constant_value (expr
);
5243 return fold (build1 (NOP_EXPR
, type
, expr
));
5245 else if ((TYPE_PTRFN_P (type
) && TYPE_PTROBV_P (intype
))
5246 || (TYPE_PTRFN_P (intype
) && TYPE_PTROBV_P (type
)))
5248 pedwarn ("ISO C++ forbids casting between pointer-to-function and pointer-to-object");
5249 expr
= decl_constant_value (expr
);
5250 return fold (build1 (NOP_EXPR
, type
, expr
));
5254 cp_error ("invalid reinterpret_cast from type `%T' to type `%T'",
5256 return error_mark_node
;
5259 return cp_convert (type
, expr
);
5263 build_const_cast (type
, expr
)
5268 if (type
== error_mark_node
|| expr
== error_mark_node
)
5269 return error_mark_node
;
5271 if (TREE_CODE (expr
) == OFFSET_REF
)
5272 expr
= resolve_offset_ref (expr
);
5274 if (processing_template_decl
)
5276 tree t
= build_min (CONST_CAST_EXPR
, type
, expr
);
5280 if (!POINTER_TYPE_P (type
))
5281 cp_error ("invalid use of const_cast with type `%T', which is not a pointer, reference, nor a pointer-to-data-member type", type
);
5282 else if (TREE_CODE (TREE_TYPE (type
)) == FUNCTION_TYPE
)
5284 cp_error ("invalid use of const_cast with type `%T', which is a pointer or reference to a function type", type
);
5285 return error_mark_node
;
5288 if (TREE_CODE (type
) != REFERENCE_TYPE
)
5290 expr
= decay_conversion (expr
);
5292 /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
5293 Strip such NOP_EXPRs if VALUE is being used in non-lvalue context. */
5294 if (TREE_CODE (expr
) == NOP_EXPR
5295 && TREE_TYPE (expr
) == TREE_TYPE (TREE_OPERAND (expr
, 0)))
5296 expr
= TREE_OPERAND (expr
, 0);
5299 intype
= TREE_TYPE (expr
);
5301 if (same_type_ignoring_top_level_qualifiers_p (intype
, type
))
5302 return build_static_cast (type
, expr
);
5303 else if (TREE_CODE (type
) == REFERENCE_TYPE
)
5305 if (! real_lvalue_p (expr
))
5307 cp_error ("invalid const_cast of an rvalue of type `%T' to type `%T'", intype
, type
);
5308 return error_mark_node
;
5311 if (comp_ptr_ttypes_const (TREE_TYPE (type
), intype
))
5313 expr
= build_unary_op (ADDR_EXPR
, expr
, 0);
5314 expr
= build1 (NOP_EXPR
, type
, expr
);
5315 return convert_from_reference (expr
);
5318 else if (TREE_CODE (type
) == POINTER_TYPE
5319 && TREE_CODE (intype
) == POINTER_TYPE
5320 && comp_ptr_ttypes_const (TREE_TYPE (type
), TREE_TYPE (intype
)))
5321 return cp_convert (type
, expr
);
5323 cp_error ("invalid const_cast from type `%T' to type `%T'", intype
, type
);
5324 return error_mark_node
;
5327 /* Build an expression representing a cast to type TYPE of expression EXPR.
5329 ALLOW_NONCONVERTING is true if we should allow non-converting constructors
5330 when doing the cast. */
5333 build_c_cast (type
, expr
)
5336 register tree value
= expr
;
5339 if (type
== error_mark_node
|| expr
== error_mark_node
)
5340 return error_mark_node
;
5342 if (processing_template_decl
)
5344 tree t
= build_min (CAST_EXPR
, type
,
5345 tree_cons (NULL_TREE
, value
, NULL_TREE
));
5349 /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
5350 Strip such NOP_EXPRs if VALUE is being used in non-lvalue context. */
5351 if (TREE_CODE (type
) != REFERENCE_TYPE
5352 && TREE_CODE (value
) == NOP_EXPR
5353 && TREE_TYPE (value
) == TREE_TYPE (TREE_OPERAND (value
, 0)))
5354 value
= TREE_OPERAND (value
, 0);
5356 if (TREE_CODE (value
) == OFFSET_REF
)
5357 value
= resolve_offset_ref (value
);
5359 if (TREE_CODE (type
) == ARRAY_TYPE
)
5361 /* Allow casting from T1* to T2[] because Cfront allows it.
5362 NIHCL uses it. It is not valid ISO C++ however. */
5363 if (TREE_CODE (TREE_TYPE (expr
)) == POINTER_TYPE
)
5365 cp_pedwarn ("ISO C++ forbids casting to an array type `%T'", type
);
5366 type
= build_pointer_type (TREE_TYPE (type
));
5370 cp_error ("ISO C++ forbids casting to an array type `%T'", type
);
5371 return error_mark_node
;
5375 if (TREE_CODE (type
) == FUNCTION_TYPE
5376 || TREE_CODE (type
) == METHOD_TYPE
)
5378 cp_error ("invalid cast to function type `%T'", type
);
5379 return error_mark_node
;
5382 if (TREE_CODE (type
) == VOID_TYPE
)
5384 /* Conversion to void does not cause any of the normal function to
5385 * pointer, array to pointer and lvalue to rvalue decays. */
5387 value
= convert_to_void (value
, /*implicit=*/NULL
);
5390 /* Convert functions and arrays to pointers and
5391 convert references to their expanded types,
5392 but don't convert any other types. If, however, we are
5393 casting to a class type, there's no reason to do this: the
5394 cast will only succeed if there is a converting constructor,
5395 and the default conversions will be done at that point. In
5396 fact, doing the default conversion here is actually harmful
5400 struct S { S(const A&); };
5402 since we don't want the array-to-pointer conversion done. */
5403 if (!IS_AGGR_TYPE (type
))
5405 if (TREE_CODE (TREE_TYPE (value
)) == FUNCTION_TYPE
5406 || (TREE_CODE (TREE_TYPE (value
)) == METHOD_TYPE
5407 /* Don't do the default conversion on a ->* expression. */
5408 && ! (TREE_CODE (type
) == POINTER_TYPE
5409 && bound_pmf_p (value
)))
5410 || TREE_CODE (TREE_TYPE (value
)) == ARRAY_TYPE
5411 || TREE_CODE (TREE_TYPE (value
)) == REFERENCE_TYPE
)
5412 value
= default_conversion (value
);
5414 else if (TREE_CODE (TREE_TYPE (value
)) == REFERENCE_TYPE
)
5415 /* However, even for class types, we still need to strip away
5416 the reference type, since the call to convert_force below
5417 does not expect the input expression to be of reference
5419 value
= convert_from_reference (value
);
5421 otype
= TREE_TYPE (value
);
5423 /* Optionally warn about potentially worrisome casts. */
5426 && TREE_CODE (type
) == POINTER_TYPE
5427 && TREE_CODE (otype
) == POINTER_TYPE
5428 && !at_least_as_qualified_p (TREE_TYPE (type
),
5430 cp_warning ("cast from `%T' to `%T' discards qualifiers from pointer target type",
5433 if (TREE_CODE (type
) == INTEGER_TYPE
5434 && TREE_CODE (otype
) == POINTER_TYPE
5435 && TYPE_PRECISION (type
) != TYPE_PRECISION (otype
))
5436 warning ("cast from pointer to integer of different size");
5438 if (TREE_CODE (type
) == POINTER_TYPE
5439 && TREE_CODE (otype
) == INTEGER_TYPE
5440 && TYPE_PRECISION (type
) != TYPE_PRECISION (otype
)
5441 /* Don't warn about converting any constant. */
5442 && !TREE_CONSTANT (value
))
5443 warning ("cast to pointer from integer of different size");
5445 if (TREE_CODE (type
) == REFERENCE_TYPE
)
5446 value
= (convert_from_reference
5447 (convert_to_reference (type
, value
, CONV_C_CAST
,
5448 LOOKUP_COMPLAIN
, NULL_TREE
)));
5453 value
= decl_constant_value (value
);
5456 value
= convert_force (type
, value
, CONV_C_CAST
);
5458 /* Ignore any integer overflow caused by the cast. */
5459 if (TREE_CODE (value
) == INTEGER_CST
)
5461 TREE_OVERFLOW (value
) = TREE_OVERFLOW (ovalue
);
5462 TREE_CONSTANT_OVERFLOW (value
) = TREE_CONSTANT_OVERFLOW (ovalue
);
5466 /* Warn about possible alignment problems. Do this here when we will have
5467 instantiated any necessary template types. */
5468 if (STRICT_ALIGNMENT
&& warn_cast_align
5469 && TREE_CODE (type
) == POINTER_TYPE
5470 && TREE_CODE (otype
) == POINTER_TYPE
5471 && TREE_CODE (TREE_TYPE (otype
)) != VOID_TYPE
5472 && TREE_CODE (TREE_TYPE (otype
)) != FUNCTION_TYPE
5473 && COMPLETE_TYPE_P (TREE_TYPE (otype
))
5474 && COMPLETE_TYPE_P (TREE_TYPE (type
))
5475 && TYPE_ALIGN (TREE_TYPE (type
)) > TYPE_ALIGN (TREE_TYPE (otype
)))
5476 cp_warning ("cast from `%T' to `%T' increases required alignment of target type",
5479 /* Always produce some operator for an explicit cast,
5480 so we can tell (for -pedantic) that the cast is no lvalue. */
5481 if (TREE_CODE (type
) != REFERENCE_TYPE
&& value
== expr
5482 && real_lvalue_p (value
))
5483 value
= non_lvalue (value
);
5488 /* Build an assignment expression of lvalue LHS from value RHS.
5489 MODIFYCODE is the code for a binary operator that we use
5490 to combine the old value of LHS with RHS to get the new value.
5491 Or else MODIFYCODE is NOP_EXPR meaning do a simple assignment.
5493 C++: If MODIFYCODE is INIT_EXPR, then leave references unbashed. */
5496 build_modify_expr (lhs
, modifycode
, rhs
)
5498 enum tree_code modifycode
;
5501 register tree result
;
5503 tree lhstype
= TREE_TYPE (lhs
);
5504 tree olhstype
= lhstype
;
5507 /* Avoid duplicate error messages from operands that had errors. */
5508 if (lhs
== error_mark_node
|| rhs
== error_mark_node
)
5509 return error_mark_node
;
5511 /* Types that aren't fully specified cannot be used in assignments. */
5512 lhs
= require_complete_type (lhs
);
5516 /* Handle control structure constructs used as "lvalues". */
5518 switch (TREE_CODE (lhs
))
5520 /* Handle --foo = 5; as these are valid constructs in C++ */
5521 case PREDECREMENT_EXPR
:
5522 case PREINCREMENT_EXPR
:
5523 if (TREE_SIDE_EFFECTS (TREE_OPERAND (lhs
, 0)))
5524 lhs
= build (TREE_CODE (lhs
), TREE_TYPE (lhs
),
5525 stabilize_reference (TREE_OPERAND (lhs
, 0)),
5526 TREE_OPERAND (lhs
, 1));
5527 return build (COMPOUND_EXPR
, lhstype
,
5529 build_modify_expr (TREE_OPERAND (lhs
, 0),
5532 /* Handle (a, b) used as an "lvalue". */
5534 newrhs
= build_modify_expr (TREE_OPERAND (lhs
, 1),
5536 if (newrhs
== error_mark_node
)
5537 return error_mark_node
;
5538 return build (COMPOUND_EXPR
, lhstype
,
5539 TREE_OPERAND (lhs
, 0), newrhs
);
5542 newrhs
= build_modify_expr (TREE_OPERAND (lhs
, 0), modifycode
, rhs
);
5543 if (newrhs
== error_mark_node
)
5544 return error_mark_node
;
5545 return build (COMPOUND_EXPR
, lhstype
, lhs
, newrhs
);
5547 /* Handle (a ? b : c) used as an "lvalue". */
5549 rhs
= save_expr (rhs
);
5551 /* Produce (a ? (b = rhs) : (c = rhs))
5552 except that the RHS goes through a save-expr
5553 so the code to compute it is only emitted once. */
5556 /* Check this here to avoid odd errors when trying to convert
5557 a throw to the type of the COND_EXPR. */
5558 if (!lvalue_or_else (lhs
, "assignment"))
5559 return error_mark_node
;
5561 cond
= build_conditional_expr
5562 (TREE_OPERAND (lhs
, 0),
5563 build_modify_expr (cp_convert (TREE_TYPE (lhs
),
5564 TREE_OPERAND (lhs
, 1)),
5566 build_modify_expr (cp_convert (TREE_TYPE (lhs
),
5567 TREE_OPERAND (lhs
, 2)),
5570 if (cond
== error_mark_node
)
5572 /* Make sure the code to compute the rhs comes out
5573 before the split. */
5574 return build (COMPOUND_EXPR
, TREE_TYPE (lhs
),
5575 /* Case to void to suppress warning
5576 from warn_if_unused_value. */
5577 cp_convert (void_type_node
, rhs
), cond
);
5584 if (TREE_CODE (lhs
) == OFFSET_REF
)
5586 if (TREE_OPERAND (lhs
, 0) == NULL_TREE
)
5588 /* Static class member? */
5589 tree member
= TREE_OPERAND (lhs
, 1);
5590 if (TREE_CODE (member
) == VAR_DECL
)
5594 compiler_error ("invalid static class member");
5595 return error_mark_node
;
5599 lhs
= resolve_offset_ref (lhs
);
5601 olhstype
= lhstype
= TREE_TYPE (lhs
);
5604 if (lhs
== error_mark_node
)
5607 if (TREE_CODE (lhstype
) == REFERENCE_TYPE
5608 && modifycode
!= INIT_EXPR
)
5610 lhs
= convert_from_reference (lhs
);
5611 olhstype
= lhstype
= TREE_TYPE (lhs
);
5614 /* If a binary op has been requested, combine the old LHS value with the RHS
5615 producing the value we should actually store into the LHS. */
5617 if (modifycode
== INIT_EXPR
)
5619 if (TREE_CODE (rhs
) == CONSTRUCTOR
)
5621 if (! same_type_p (TREE_TYPE (rhs
), lhstype
))
5623 result
= build (INIT_EXPR
, lhstype
, lhs
, rhs
);
5624 TREE_SIDE_EFFECTS (result
) = 1;
5627 else if (! IS_AGGR_TYPE (lhstype
))
5628 /* Do the default thing */;
5631 result
= build_method_call (lhs
, complete_ctor_identifier
,
5632 build_tree_list (NULL_TREE
, rhs
),
5633 TYPE_BINFO (lhstype
), LOOKUP_NORMAL
);
5634 if (result
== NULL_TREE
)
5635 return error_mark_node
;
5639 else if (modifycode
== NOP_EXPR
)
5641 /* `operator=' is not an inheritable operator. */
5642 if (! IS_AGGR_TYPE (lhstype
))
5643 /* Do the default thing */;
5646 result
= build_opfncall (MODIFY_EXPR
, LOOKUP_NORMAL
,
5647 lhs
, rhs
, make_node (NOP_EXPR
));
5648 if (result
== NULL_TREE
)
5649 return error_mark_node
;
5654 else if (PROMOTES_TO_AGGR_TYPE (lhstype
, REFERENCE_TYPE
))
5656 my_friendly_abort (978652);
5660 lhs
= stabilize_reference (lhs
);
5661 newrhs
= cp_build_binary_op (modifycode
, lhs
, rhs
);
5662 if (newrhs
== error_mark_node
)
5664 cp_error (" in evaluation of `%Q(%#T, %#T)'", modifycode
,
5665 TREE_TYPE (lhs
), TREE_TYPE (rhs
));
5666 return error_mark_node
;
5670 /* Handle a cast used as an "lvalue".
5671 We have already performed any binary operator using the value as cast.
5672 Now convert the result to the cast type of the lhs,
5673 and then true type of the lhs and store it there;
5674 then convert result back to the cast type to be the value
5675 of the assignment. */
5677 switch (TREE_CODE (lhs
))
5682 case FIX_TRUNC_EXPR
:
5683 case FIX_FLOOR_EXPR
:
5684 case FIX_ROUND_EXPR
:
5686 if (TREE_CODE (TREE_TYPE (newrhs
)) == ARRAY_TYPE
5687 || TREE_CODE (TREE_TYPE (newrhs
)) == FUNCTION_TYPE
5688 || TREE_CODE (TREE_TYPE (newrhs
)) == METHOD_TYPE
5689 || TREE_CODE (TREE_TYPE (newrhs
)) == OFFSET_TYPE
)
5690 newrhs
= default_conversion (newrhs
);
5692 tree inner_lhs
= TREE_OPERAND (lhs
, 0);
5695 /* ISO C++ 5.4/1: The result is an lvalue if T is a reference
5696 type, otherwise the result is an rvalue. */
5697 if (! lvalue_p (lhs
))
5698 pedwarn ("ISO C++ forbids cast to non-reference type used as lvalue");
5700 result
= build_modify_expr (inner_lhs
, NOP_EXPR
,
5701 cp_convert (TREE_TYPE (inner_lhs
),
5702 cp_convert (lhstype
, newrhs
)));
5703 if (result
== error_mark_node
)
5705 return cp_convert (TREE_TYPE (lhs
), result
);
5712 /* Now we have handled acceptable kinds of LHS that are not truly lvalues.
5713 Reject anything strange now. */
5715 if (!lvalue_or_else (lhs
, "assignment"))
5716 return error_mark_node
;
5718 GNU_xref_assign (lhs
);
5720 /* Warn about storing in something that is `const'. */
5721 /* For C++, don't warn if this is initialization. */
5722 if (modifycode
!= INIT_EXPR
5723 && (TREE_READONLY (lhs
) || CP_TYPE_CONST_P (lhstype
)
5724 /* Functions are not modifiable, even though they are
5726 || TREE_CODE (TREE_TYPE (lhs
)) == FUNCTION_TYPE
5727 || (IS_AGGR_TYPE_CODE (TREE_CODE (lhstype
))
5728 && C_TYPE_FIELDS_READONLY (lhstype
))
5729 || (TREE_CODE (lhstype
) == REFERENCE_TYPE
5730 && CP_TYPE_CONST_P (TREE_TYPE (lhstype
)))))
5731 readonly_error (lhs
, "assignment", 0);
5733 /* If storing into a structure or union member,
5734 it has probably been given type `int'.
5735 Compute the type that would go with
5736 the actual amount of storage the member occupies. */
5738 if (TREE_CODE (lhs
) == COMPONENT_REF
5739 && (TREE_CODE (lhstype
) == INTEGER_TYPE
5740 || TREE_CODE (lhstype
) == REAL_TYPE
5741 || TREE_CODE (lhstype
) == ENUMERAL_TYPE
))
5743 lhstype
= TREE_TYPE (get_unwidened (lhs
, 0));
5745 /* If storing in a field that is in actuality a short or narrower
5746 than one, we must store in the field in its actual type. */
5748 if (lhstype
!= TREE_TYPE (lhs
))
5750 lhs
= copy_node (lhs
);
5751 TREE_TYPE (lhs
) = lhstype
;
5755 if (modifycode
!= INIT_EXPR
)
5757 /* Make modifycode now either a NOP_EXPR or an INIT_EXPR. */
5758 modifycode
= NOP_EXPR
;
5759 /* Reference-bashing */
5760 if (TREE_CODE (lhstype
) == REFERENCE_TYPE
)
5762 tree tmp
= convert_from_reference (lhs
);
5763 lhstype
= TREE_TYPE (tmp
);
5764 if (!COMPLETE_TYPE_P (lhstype
))
5766 incomplete_type_error (lhs
, lhstype
);
5767 return error_mark_node
;
5772 if (TREE_CODE (TREE_TYPE (newrhs
)) == REFERENCE_TYPE
)
5774 tree tmp
= convert_from_reference (newrhs
);
5775 if (!COMPLETE_TYPE_P (TREE_TYPE (tmp
)))
5777 incomplete_type_error (newrhs
, TREE_TYPE (tmp
));
5778 return error_mark_node
;
5784 if (TREE_SIDE_EFFECTS (lhs
))
5785 lhs
= stabilize_reference (lhs
);
5786 if (TREE_SIDE_EFFECTS (newrhs
))
5787 newrhs
= stabilize_reference (newrhs
);
5789 /* Convert new value to destination type. */
5791 if (TREE_CODE (lhstype
) == ARRAY_TYPE
)
5795 if (!same_or_base_type_p (lhstype
, TREE_TYPE (rhs
)))
5797 cp_error ("incompatible types in assignment of `%T' to `%T'",
5798 TREE_TYPE (rhs
), lhstype
);
5799 return error_mark_node
;
5802 /* Allow array assignment in compiler-generated code. */
5803 if (pedantic
&& ! DECL_ARTIFICIAL (current_function_decl
))
5804 pedwarn ("ISO C++ forbids assignment of arrays");
5806 from_array
= TREE_CODE (TREE_TYPE (newrhs
)) == ARRAY_TYPE
5807 ? 1 + (modifycode
!= INIT_EXPR
): 0;
5808 return build_vec_init (lhs
, newrhs
, from_array
);
5811 if (modifycode
== INIT_EXPR
)
5813 newrhs
= convert_for_initialization (lhs
, lhstype
, newrhs
, LOOKUP_NORMAL
,
5814 "initialization", NULL_TREE
, 0);
5815 if (current_function_decl
&&
5816 lhs
== DECL_RESULT (current_function_decl
))
5818 if (DECL_INITIAL (lhs
))
5819 warning ("return value from function receives multiple initializations");
5820 DECL_INITIAL (lhs
) = newrhs
;
5825 /* Avoid warnings on enum bit fields. */
5826 if (TREE_CODE (olhstype
) == ENUMERAL_TYPE
5827 && TREE_CODE (lhstype
) == INTEGER_TYPE
)
5829 newrhs
= convert_for_assignment (olhstype
, newrhs
, "assignment",
5831 newrhs
= convert_force (lhstype
, newrhs
, 0);
5834 newrhs
= convert_for_assignment (lhstype
, newrhs
, "assignment",
5836 if (TREE_CODE (newrhs
) == CALL_EXPR
5837 && TYPE_NEEDS_CONSTRUCTING (lhstype
))
5838 newrhs
= build_cplus_new (lhstype
, newrhs
);
5840 /* Can't initialize directly from a TARGET_EXPR, since that would
5841 cause the lhs to be constructed twice, and possibly result in
5842 accidental self-initialization. So we force the TARGET_EXPR to be
5843 expanded without a target. */
5844 if (TREE_CODE (newrhs
) == TARGET_EXPR
)
5845 newrhs
= build (COMPOUND_EXPR
, TREE_TYPE (newrhs
), newrhs
,
5846 TREE_OPERAND (newrhs
, 0));
5849 if (newrhs
== error_mark_node
)
5850 return error_mark_node
;
5852 if (TREE_CODE (newrhs
) == COND_EXPR
)
5855 tree cond
= TREE_OPERAND (newrhs
, 0);
5857 if (TREE_SIDE_EFFECTS (lhs
))
5858 cond
= build_compound_expr (tree_cons
5860 build_tree_list (NULL_TREE
, cond
)));
5862 /* Cannot have two identical lhs on this one tree (result) as preexpand
5863 calls will rip them out and fill in RTL for them, but when the
5864 rtl is generated, the calls will only be in the first side of the
5865 condition, not on both, or before the conditional jump! (mrs) */
5866 lhs1
= break_out_calls (lhs
);
5869 /* If there's no change, the COND_EXPR behaves like any other rhs. */
5870 result
= build (modifycode
== NOP_EXPR
? MODIFY_EXPR
: INIT_EXPR
,
5871 lhstype
, lhs
, newrhs
);
5874 tree result_type
= TREE_TYPE (newrhs
);
5875 /* We have to convert each arm to the proper type because the
5876 types may have been munged by constant folding. */
5878 = build (COND_EXPR
, result_type
, cond
,
5879 build_modify_expr (lhs
, modifycode
,
5880 cp_convert (result_type
,
5881 TREE_OPERAND (newrhs
, 1))),
5882 build_modify_expr (lhs1
, modifycode
,
5883 cp_convert (result_type
,
5884 TREE_OPERAND (newrhs
, 2))));
5888 result
= build (modifycode
== NOP_EXPR
? MODIFY_EXPR
: INIT_EXPR
,
5889 lhstype
, lhs
, newrhs
);
5891 TREE_SIDE_EFFECTS (result
) = 1;
5893 /* If we got the LHS in a different type for storing in,
5894 convert the result back to the nominal type of LHS
5895 so that the value we return always has the same type
5896 as the LHS argument. */
5898 if (olhstype
== TREE_TYPE (result
))
5900 /* Avoid warnings converting integral types back into enums
5901 for enum bit fields. */
5902 if (TREE_CODE (TREE_TYPE (result
)) == INTEGER_TYPE
5903 && TREE_CODE (olhstype
) == ENUMERAL_TYPE
)
5905 result
= build (COMPOUND_EXPR
, olhstype
, result
, olhs
);
5906 TREE_NO_UNUSED_WARNING (result
) = 1;
5909 return convert_for_assignment (olhstype
, result
, "assignment",
5914 build_x_modify_expr (lhs
, modifycode
, rhs
)
5916 enum tree_code modifycode
;
5919 if (processing_template_decl
)
5920 return build_min_nt (MODOP_EXPR
, lhs
,
5921 build_min_nt (modifycode
, NULL_TREE
, NULL_TREE
), rhs
);
5923 if (modifycode
!= NOP_EXPR
)
5925 tree rval
= build_opfncall (MODIFY_EXPR
, LOOKUP_NORMAL
, lhs
, rhs
,
5926 make_node (modifycode
));
5930 return build_modify_expr (lhs
, modifycode
, rhs
);
5934 /* Get difference in deltas for different pointer to member function
5935 types. Return integer_zero_node, if FROM cannot be converted to a
5936 TO type. If FORCE is true, then allow reverse conversions as well.
5938 Note that the naming of FROM and TO is kind of backwards; the return
5939 value is what we add to a TO in order to get a FROM. They are named
5940 this way because we call this function to find out how to convert from
5941 a pointer to member of FROM to a pointer to member of TO. */
5944 get_delta_difference (from
, to
, force
)
5948 tree delta
= integer_zero_node
;
5955 /* Should get_base_distance here, so we can check if any thing along
5956 the path is virtual, and we need to make sure we stay inside the
5957 real binfos when going through virtual bases. Maybe we should
5958 replace virtual bases with BINFO_FOR_VBASE ... (mrs) */
5959 binfo
= get_binfo (from
, to
, 1);
5960 if (binfo
== error_mark_node
)
5962 error (" in pointer to member function conversion");
5969 error_not_base_type (from
, to
);
5970 error (" in pointer to member conversion");
5973 binfo
= get_binfo (to
, from
, 1);
5974 if (binfo
== 0 || binfo
== error_mark_node
)
5976 virt_binfo
= binfo_from_vbase (binfo
);
5980 /* This is a reinterpret cast, we choose to do nothing. */
5981 cp_warning ("pointer to member cast via virtual base `%T' of `%T'",
5982 BINFO_TYPE (virt_binfo
),
5983 BINFO_TYPE (BINFO_INHERITANCE_CHAIN (virt_binfo
)));
5986 delta
= BINFO_OFFSET (binfo
);
5987 delta
= cp_convert (ptrdiff_type_node
, delta
);
5988 delta
= cp_build_binary_op (MINUS_EXPR
,
5995 virt_binfo
= binfo_from_vbase (binfo
);
5998 /* This is a reinterpret cast, we choose to do nothing. */
6000 cp_warning ("pointer to member cast via virtual base `%T' of `%T'",
6001 BINFO_TYPE (virt_binfo
),
6002 BINFO_TYPE (BINFO_INHERITANCE_CHAIN (virt_binfo
)));
6004 cp_error ("pointer to member conversion via virtual base `%T' of `%T'",
6005 BINFO_TYPE (virt_binfo
),
6006 BINFO_TYPE (BINFO_INHERITANCE_CHAIN (virt_binfo
)));
6009 delta
= BINFO_OFFSET (binfo
);
6011 return cp_convert (ptrdiff_type_node
, delta
);
6014 /* Return a constructor for the pointer-to-member-function TYPE using
6015 the other components as specified. */
6018 build_ptrmemfunc1 (type
, delta
, pfn
)
6019 tree type
, delta
, pfn
;
6025 /* Pull the FIELD_DECLs out of the type. */
6026 pfn_field
= TYPE_FIELDS (type
);
6027 delta_field
= TREE_CHAIN (pfn_field
);
6029 /* Make sure DELTA has the type we want. */
6030 delta
= convert_and_check (delta_type_node
, delta
);
6032 /* Finish creating the initializer. */
6033 u
= tree_cons (pfn_field
, pfn
,
6034 build_tree_list (delta_field
, delta
));
6035 u
= build (CONSTRUCTOR
, type
, NULL_TREE
, u
);
6036 TREE_CONSTANT (u
) = TREE_CONSTANT (pfn
) && TREE_CONSTANT (delta
);
6037 TREE_STATIC (u
) = (TREE_CONSTANT (u
)
6038 && (initializer_constant_valid_p (pfn
, TREE_TYPE (pfn
))
6040 && (initializer_constant_valid_p (delta
, TREE_TYPE (delta
))
6045 /* Build a constructor for a pointer to member function. It can be
6046 used to initialize global variables, local variable, or used
6047 as a value in expressions. TYPE is the POINTER to METHOD_TYPE we
6050 If FORCE is non-zero, then force this conversion, even if
6051 we would rather not do it. Usually set when using an explicit
6054 Return error_mark_node, if something goes wrong. */
6057 build_ptrmemfunc (type
, pfn
, force
)
6062 tree pfn_type
= TREE_TYPE (pfn
);
6063 tree to_type
= build_ptrmemfunc_type (type
);
6065 /* Handle multiple conversions of pointer to member functions. */
6066 if (TYPE_PTRMEMFUNC_P (TREE_TYPE (pfn
)))
6068 tree delta
= NULL_TREE
;
6069 tree npfn
= NULL_TREE
;
6073 && !can_convert_arg (to_type
, TREE_TYPE (pfn
), pfn
))
6074 cp_error ("invalid conversion to type `%T' from type `%T'",
6077 n
= get_delta_difference (TYPE_PTRMEMFUNC_OBJECT_TYPE (pfn_type
),
6078 TYPE_PTRMEMFUNC_OBJECT_TYPE (to_type
),
6081 /* We don't have to do any conversion to convert a
6082 pointer-to-member to its own type. But, we don't want to
6083 just return a PTRMEM_CST if there's an explicit cast; that
6084 cast should make the expression an invalid template argument. */
6085 if (TREE_CODE (pfn
) != PTRMEM_CST
)
6087 if (same_type_p (to_type
, pfn_type
))
6089 else if (integer_zerop (n
))
6090 return build_reinterpret_cast (to_type
, pfn
);
6093 if (TREE_SIDE_EFFECTS (pfn
))
6094 pfn
= save_expr (pfn
);
6096 /* Obtain the function pointer and the current DELTA. */
6097 if (TREE_CODE (pfn
) == PTRMEM_CST
)
6098 expand_ptrmemfunc_cst (pfn
, &delta
, &npfn
);
6101 npfn
= build_component_ref (pfn
, pfn_identifier
, NULL_TREE
, 0);
6102 delta
= build_component_ref (pfn
, delta_identifier
, NULL_TREE
, 0);
6105 /* Under the new ABI, the conversion is easy. Just adjust
6107 delta
= cp_convert (ptrdiff_type_node
, delta
);
6108 if (TARGET_PTRMEMFUNC_VBIT_LOCATION
== ptrmemfunc_vbit_in_delta
)
6109 n
= cp_build_binary_op (LSHIFT_EXPR
, n
, integer_one_node
);
6110 delta
= cp_build_binary_op (PLUS_EXPR
, delta
, n
);
6111 return build_ptrmemfunc1 (to_type
, delta
, npfn
);
6114 /* Handle null pointer to member function conversions. */
6115 if (integer_zerop (pfn
))
6117 pfn
= build_c_cast (type
, integer_zero_node
);
6118 return build_ptrmemfunc1 (to_type
,
6123 if (type_unknown_p (pfn
))
6124 return instantiate_type (type
, pfn
, itf_complain
);
6126 fn
= TREE_OPERAND (pfn
, 0);
6127 my_friendly_assert (TREE_CODE (fn
) == FUNCTION_DECL
, 0);
6128 return make_ptrmem_cst (to_type
, fn
);
6131 /* Return the DELTA, IDX, PFN, and DELTA2 values for the PTRMEM_CST
6134 ??? There is no consistency as to the types returned for the above
6135 values. Some code acts as if its a sizetype and some as if its
6136 integer_type_node. */
6139 expand_ptrmemfunc_cst (cst
, delta
, pfn
)
6144 tree type
= TREE_TYPE (cst
);
6145 tree fn
= PTRMEM_CST_MEMBER (cst
);
6146 tree ptr_class
, fn_class
;
6148 my_friendly_assert (TREE_CODE (fn
) == FUNCTION_DECL
, 0);
6150 /* The class that the function belongs to. */
6151 fn_class
= DECL_CONTEXT (fn
);
6153 /* The class that we're creating a pointer to member of. */
6154 ptr_class
= TYPE_PTRMEMFUNC_OBJECT_TYPE (type
);
6156 /* First, calculate the adjustment to the function's class. */
6157 *delta
= get_delta_difference (fn_class
, ptr_class
, /*force=*/0);
6159 if (!DECL_VIRTUAL_P (fn
))
6160 *pfn
= convert (TYPE_PTRMEMFUNC_FN_TYPE (type
), build_addr_func (fn
));
6163 /* If we're dealing with a virtual function, we have to adjust 'this'
6164 again, to point to the base which provides the vtable entry for
6165 fn; the call will do the opposite adjustment. */
6166 tree orig_class
= DECL_VIRTUAL_CONTEXT (fn
);
6167 tree binfo
= binfo_or_else (orig_class
, fn_class
);
6168 *delta
= fold (build (PLUS_EXPR
, TREE_TYPE (*delta
),
6169 *delta
, BINFO_OFFSET (binfo
)));
6171 /* Under the new ABI, we set PFN to the vtable offset at
6172 which the function can be found, plus one (unless
6173 ptrmemfunc_vbit_in_delta, in which case delta is shifted
6174 left, and then incremented). */
6175 *pfn
= DECL_VINDEX (fn
);
6176 *pfn
= fold (build (MULT_EXPR
, integer_type_node
, *pfn
,
6177 TYPE_SIZE_UNIT (vtable_entry_type
)));
6179 switch (TARGET_PTRMEMFUNC_VBIT_LOCATION
)
6181 case ptrmemfunc_vbit_in_pfn
:
6182 *pfn
= fold (build (PLUS_EXPR
, integer_type_node
, *pfn
,
6186 case ptrmemfunc_vbit_in_delta
:
6187 *delta
= fold (build (LSHIFT_EXPR
, TREE_TYPE (*delta
),
6188 *delta
, integer_one_node
));
6189 *delta
= fold (build (PLUS_EXPR
, TREE_TYPE (*delta
),
6190 *delta
, integer_one_node
));
6197 *pfn
= fold (build1 (NOP_EXPR
, TYPE_PTRMEMFUNC_FN_TYPE (type
),
6202 /* Return an expression for PFN from the pointer-to-member function
6206 pfn_from_ptrmemfunc (t
)
6209 if (TREE_CODE (t
) == PTRMEM_CST
)
6214 expand_ptrmemfunc_cst (t
, &delta
, &pfn
);
6219 return build_component_ref (t
, pfn_identifier
, NULL_TREE
, 0);
6222 /* Expression EXPR is about to be implicitly converted to TYPE. Warn
6223 if this is a potentially dangerous thing to do. Returns a possibly
6227 dubious_conversion_warnings (type
, expr
, errtype
, fndecl
, parmnum
)
6230 const char *errtype
;
6234 if (TREE_CODE (type
) == REFERENCE_TYPE
)
6235 type
= TREE_TYPE (type
);
6237 /* Issue warnings about peculiar, but legal, uses of NULL. */
6238 if (ARITHMETIC_TYPE_P (type
) && expr
== null_node
)
6241 cp_warning ("passing NULL used for non-pointer %s %P of `%D'",
6242 errtype
, parmnum
, fndecl
);
6244 cp_warning ("%s to non-pointer type `%T' from NULL", errtype
, type
);
6247 /* Warn about assigning a floating-point type to an integer type. */
6248 if (TREE_CODE (TREE_TYPE (expr
)) == REAL_TYPE
6249 && TREE_CODE (type
) == INTEGER_TYPE
)
6252 cp_warning ("passing `%T' for %s %P of `%D'",
6253 TREE_TYPE (expr
), errtype
, parmnum
, fndecl
);
6255 cp_warning ("%s to `%T' from `%T'", errtype
, type
, TREE_TYPE (expr
));
6257 /* And warn about assigning a negative value to an unsigned
6259 else if (TREE_UNSIGNED (type
) && TREE_CODE (type
) != BOOLEAN_TYPE
)
6261 if (TREE_CODE (expr
) == INTEGER_CST
6262 && TREE_NEGATED_INT (expr
))
6265 cp_warning ("passing negative value `%E' for %s %P of `%D'",
6266 expr
, errtype
, parmnum
, fndecl
);
6268 cp_warning ("%s of negative value `%E' to `%T'",
6269 errtype
, expr
, type
);
6272 overflow_warning (expr
);
6274 if (TREE_CONSTANT (expr
))
6280 /* Convert value RHS to type TYPE as preparation for an assignment to
6281 an lvalue of type TYPE. ERRTYPE is a string to use in error
6282 messages: "assignment", "return", etc. If FNDECL is non-NULL, we
6283 are doing the conversion in order to pass the PARMNUMth argument of
6287 convert_for_assignment (type
, rhs
, errtype
, fndecl
, parmnum
)
6289 const char *errtype
;
6293 register enum tree_code codel
= TREE_CODE (type
);
6294 register tree rhstype
;
6295 register enum tree_code coder
;
6297 if (codel
== OFFSET_TYPE
)
6298 my_friendly_abort (990505);
6300 if (TREE_CODE (rhs
) == OFFSET_REF
)
6301 rhs
= resolve_offset_ref (rhs
);
6303 /* Strip NON_LVALUE_EXPRs since we aren't using as an lvalue. */
6304 if (TREE_CODE (rhs
) == NON_LVALUE_EXPR
)
6305 rhs
= TREE_OPERAND (rhs
, 0);
6307 rhstype
= TREE_TYPE (rhs
);
6308 coder
= TREE_CODE (rhstype
);
6310 if (rhs
== error_mark_node
|| rhstype
== error_mark_node
)
6311 return error_mark_node
;
6312 if (TREE_CODE (rhs
) == TREE_LIST
&& TREE_VALUE (rhs
) == error_mark_node
)
6313 return error_mark_node
;
6315 rhs
= dubious_conversion_warnings (type
, rhs
, errtype
, fndecl
, parmnum
);
6317 /* The RHS of an assignment cannot have void type. */
6318 if (coder
== VOID_TYPE
)
6320 error ("void value not ignored as it ought to be");
6321 return error_mark_node
;
6324 /* Simplify the RHS if possible. */
6325 if (TREE_CODE (rhs
) == CONST_DECL
)
6326 rhs
= DECL_INITIAL (rhs
);
6327 else if (coder
!= ARRAY_TYPE
)
6328 rhs
= decl_constant_value (rhs
);
6332 The expression is implicitly converted (clause _conv_) to the
6333 cv-unqualified type of the left operand. */
6334 if (!can_convert_arg (type
, rhstype
, rhs
))
6336 /* When -Wno-pmf-conversions is use, we just silently allow
6337 conversions from pointers-to-members to plain pointers. If
6338 the conversion doesn't work, cp_convert will complain. */
6340 && TYPE_PTR_P (type
)
6341 && TYPE_PTRMEMFUNC_P (rhstype
))
6342 rhs
= cp_convert (strip_top_quals (type
), rhs
);
6345 /* If the right-hand side has unknown type, then it is an
6346 overloaded function. Call instantiate_type to get error
6348 if (rhstype
== unknown_type_node
)
6349 instantiate_type (type
, rhs
, itf_complain
);
6351 cp_error ("cannot convert `%T' to `%T' for argument `%P' to `%D'",
6352 rhstype
, type
, parmnum
, fndecl
);
6354 cp_error ("cannot convert `%T' to `%T' in %s", rhstype
, type
,
6356 return error_mark_node
;
6359 return perform_implicit_conversion (strip_top_quals (type
), rhs
);
6362 /* Convert RHS to be of type TYPE.
6363 If EXP is non-zero, it is the target of the initialization.
6364 ERRTYPE is a string to use in error messages.
6366 Two major differences between the behavior of
6367 `convert_for_assignment' and `convert_for_initialization'
6368 are that references are bashed in the former, while
6369 copied in the latter, and aggregates are assigned in
6370 the former (operator=) while initialized in the
6373 If using constructor make sure no conversion operator exists, if one does
6374 exist, an ambiguity exists.
6376 If flags doesn't include LOOKUP_COMPLAIN, don't complain about anything. */
6379 convert_for_initialization (exp
, type
, rhs
, flags
, errtype
, fndecl
, parmnum
)
6380 tree exp
, type
, rhs
;
6382 const char *errtype
;
6386 register enum tree_code codel
= TREE_CODE (type
);
6387 register tree rhstype
;
6388 register enum tree_code coder
;
6390 /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
6391 Strip such NOP_EXPRs, since RHS is used in non-lvalue context. */
6392 if (TREE_CODE (rhs
) == NOP_EXPR
6393 && TREE_TYPE (rhs
) == TREE_TYPE (TREE_OPERAND (rhs
, 0))
6394 && codel
!= REFERENCE_TYPE
)
6395 rhs
= TREE_OPERAND (rhs
, 0);
6397 if (rhs
== error_mark_node
6398 || (TREE_CODE (rhs
) == TREE_LIST
&& TREE_VALUE (rhs
) == error_mark_node
))
6399 return error_mark_node
;
6401 if (TREE_CODE (rhs
) == OFFSET_REF
)
6403 rhs
= resolve_offset_ref (rhs
);
6404 if (rhs
== error_mark_node
)
6405 return error_mark_node
;
6408 if (TREE_CODE (TREE_TYPE (rhs
)) == REFERENCE_TYPE
)
6409 rhs
= convert_from_reference (rhs
);
6411 if ((TREE_CODE (TREE_TYPE (rhs
)) == ARRAY_TYPE
6412 && TREE_CODE (type
) != ARRAY_TYPE
6413 && (TREE_CODE (type
) != REFERENCE_TYPE
6414 || TREE_CODE (TREE_TYPE (type
)) != ARRAY_TYPE
))
6415 || (TREE_CODE (TREE_TYPE (rhs
)) == FUNCTION_TYPE
6416 && (TREE_CODE (type
) != REFERENCE_TYPE
6417 || TREE_CODE (TREE_TYPE (type
)) != FUNCTION_TYPE
))
6418 || TREE_CODE (TREE_TYPE (rhs
)) == METHOD_TYPE
)
6419 rhs
= default_conversion (rhs
);
6421 rhstype
= TREE_TYPE (rhs
);
6422 coder
= TREE_CODE (rhstype
);
6424 if (coder
== ERROR_MARK
)
6425 return error_mark_node
;
6427 /* We accept references to incomplete types, so we can
6428 return here before checking if RHS is of complete type. */
6430 if (codel
== REFERENCE_TYPE
)
6432 /* This should eventually happen in convert_arguments. */
6433 int savew
= 0, savee
= 0;
6436 savew
= warningcount
, savee
= errorcount
;
6437 rhs
= initialize_reference (type
, rhs
);
6440 if (warningcount
> savew
)
6441 cp_warning_at ("in passing argument %P of `%+D'", parmnum
, fndecl
);
6442 else if (errorcount
> savee
)
6443 cp_error_at ("in passing argument %P of `%+D'", parmnum
, fndecl
);
6449 exp
= require_complete_type (exp
);
6450 if (exp
== error_mark_node
)
6451 return error_mark_node
;
6453 if (TREE_CODE (rhstype
) == REFERENCE_TYPE
)
6454 rhstype
= TREE_TYPE (rhstype
);
6456 type
= complete_type (type
);
6458 if (IS_AGGR_TYPE (type
))
6459 return ocp_convert (type
, rhs
, CONV_IMPLICIT
|CONV_FORCE_TEMP
, flags
);
6461 return convert_for_assignment (type
, rhs
, errtype
, fndecl
, parmnum
);
6464 /* Expand an ASM statement with operands, handling output operands
6465 that are not variables or INDIRECT_REFS by transforming such
6466 cases into cases that expand_asm_operands can handle.
6468 Arguments are same as for expand_asm_operands.
6470 We don't do default conversions on all inputs, because it can screw
6471 up operands that are expected to be in memory. */
6474 c_expand_asm_operands (string
, outputs
, inputs
, clobbers
, vol
, filename
, line
)
6475 tree string
, outputs
, inputs
, clobbers
;
6477 const char *filename
;
6480 int noutputs
= list_length (outputs
);
6482 /* o[I] is the place that output number I should be written. */
6483 register tree
*o
= (tree
*) alloca (noutputs
* sizeof (tree
));
6486 /* Record the contents of OUTPUTS before it is modified. */
6487 for (i
= 0, tail
= outputs
; tail
; tail
= TREE_CHAIN (tail
), i
++)
6488 o
[i
] = TREE_VALUE (tail
);
6490 /* Generate the ASM_OPERANDS insn;
6491 store into the TREE_VALUEs of OUTPUTS some trees for
6492 where the values were actually stored. */
6493 expand_asm_operands (string
, outputs
, inputs
, clobbers
, vol
, filename
, line
);
6495 /* Copy all the intermediate outputs into the specified outputs. */
6496 for (i
= 0, tail
= outputs
; tail
; tail
= TREE_CHAIN (tail
), i
++)
6498 if (o
[i
] != TREE_VALUE (tail
))
6500 expand_expr (build_modify_expr (o
[i
], NOP_EXPR
, TREE_VALUE (tail
)),
6501 const0_rtx
, VOIDmode
, EXPAND_NORMAL
);
6504 /* Restore the original value so that it's correct the next
6505 time we expand this function. */
6506 TREE_VALUE (tail
) = o
[i
];
6508 /* Detect modification of read-only values.
6509 (Otherwise done by build_modify_expr.) */
6512 tree type
= TREE_TYPE (o
[i
]);
6513 if (CP_TYPE_CONST_P (type
)
6514 || (IS_AGGR_TYPE_CODE (TREE_CODE (type
))
6515 && C_TYPE_FIELDS_READONLY (type
)))
6516 readonly_error (o
[i
], "modification by `asm'", 1);
6520 /* Those MODIFY_EXPRs could do autoincrements. */
6524 /* If RETVAL is the address of, or a reference to, a local variable or
6525 temporary give an appropraite warning. */
6528 maybe_warn_about_returning_address_of_local (retval
)
6531 tree valtype
= TREE_TYPE (DECL_RESULT (current_function_decl
));
6532 tree whats_returned
= retval
;
6536 if (TREE_CODE (whats_returned
) == COMPOUND_EXPR
)
6537 whats_returned
= TREE_OPERAND (whats_returned
, 1);
6538 else if (TREE_CODE (whats_returned
) == CONVERT_EXPR
6539 || TREE_CODE (whats_returned
) == NON_LVALUE_EXPR
6540 || TREE_CODE (whats_returned
) == NOP_EXPR
)
6541 whats_returned
= TREE_OPERAND (whats_returned
, 0);
6546 if (TREE_CODE (whats_returned
) != ADDR_EXPR
)
6548 whats_returned
= TREE_OPERAND (whats_returned
, 0);
6550 if (TREE_CODE (valtype
) == REFERENCE_TYPE
)
6552 if (TREE_CODE (whats_returned
) == AGGR_INIT_EXPR
6553 || TREE_CODE (whats_returned
) == TARGET_EXPR
)
6555 /* Get the target. */
6556 whats_returned
= TREE_OPERAND (whats_returned
, 0);
6557 warning ("returning reference to temporary");
6560 if (TREE_CODE (whats_returned
) == VAR_DECL
6561 && DECL_NAME (whats_returned
)
6562 && TEMP_NAME_P (DECL_NAME (whats_returned
)))
6564 warning ("reference to non-lvalue returned");
6569 if (TREE_CODE (whats_returned
) == VAR_DECL
6570 && DECL_NAME (whats_returned
)
6571 && DECL_FUNCTION_SCOPE_P (whats_returned
)
6572 && !(TREE_STATIC (whats_returned
)
6573 || TREE_PUBLIC (whats_returned
)))
6575 if (TREE_CODE (valtype
) == REFERENCE_TYPE
)
6576 cp_warning_at ("reference to local variable `%D' returned",
6579 cp_warning_at ("address of local variable `%D' returned",
6585 /* Check that returning RETVAL from the current function is legal.
6586 Return an expression explicitly showing all conversions required to
6587 change RETVAL into the function return type, and to assign it to
6588 the DECL_RESULT for the function. */
6591 check_return_expr (retval
)
6595 /* The type actually returned by the function, after any
6598 int fn_returns_value_p
;
6600 /* A `volatile' function is one that isn't supposed to return, ever.
6601 (This is a G++ extension, used to get better code for functions
6602 that call the `volatile' function.) */
6603 if (TREE_THIS_VOLATILE (current_function_decl
))
6604 warning ("function declared `noreturn' has a `return' statement");
6606 /* Check for various simple errors. */
6610 error ("returning a value from a destructor");
6613 else if (DECL_CONSTRUCTOR_P (current_function_decl
))
6615 if (in_function_try_handler
)
6616 /* If a return statement appears in a handler of the
6617 function-try-block of a constructor, the program is ill-formed. */
6618 error ("cannot return from a handler of a function-try-block of a constructor");
6620 /* You can't return a value from a constructor. */
6621 error ("returning a value from a constructor");
6625 /* When no explicit return-value is given in a function with a named
6626 return value, the named return value is used. */
6627 result
= DECL_RESULT (current_function_decl
);
6628 valtype
= TREE_TYPE (result
);
6629 my_friendly_assert (valtype
!= NULL_TREE
, 19990924);
6630 fn_returns_value_p
= !VOID_TYPE_P (valtype
);
6631 if (!retval
&& DECL_NAME (result
) && fn_returns_value_p
)
6634 /* Check for a return statement with no return value in a function
6635 that's supposed to return a value. */
6636 if (!retval
&& fn_returns_value_p
)
6638 pedwarn ("return-statement with no value, in function declared with a non-void return type");
6639 /* Clear this, so finish_function won't say that we reach the
6640 end of a non-void function (which we don't, we gave a
6642 current_function_returns_null
= 0;
6644 /* Check for a return statement with a value in a function that
6645 isn't supposed to return a value. */
6646 else if (retval
&& !fn_returns_value_p
)
6648 if (VOID_TYPE_P (TREE_TYPE (retval
)))
6649 /* You can return a `void' value from a function of `void'
6650 type. In that case, we have to evaluate the expression for
6651 its side-effects. */
6652 finish_expr_stmt (retval
);
6654 pedwarn ("return-statement with a value, in function declared with a void return type");
6656 current_function_returns_null
= 1;
6658 /* There's really no value to return, after all. */
6662 /* Remember that this function can sometimes return without a
6664 current_function_returns_null
= 1;
6666 /* Remember that this function did return a value. */
6667 current_function_returns_value
= 1;
6669 /* Only operator new(...) throw(), can return NULL [expr.new/13]. */
6670 if ((DECL_OVERLOADED_OPERATOR_P (current_function_decl
) == NEW_EXPR
6671 || DECL_OVERLOADED_OPERATOR_P (current_function_decl
) == VEC_NEW_EXPR
)
6672 && !TYPE_NOTHROW_P (TREE_TYPE (current_function_decl
))
6674 && null_ptr_cst_p (retval
))
6675 cp_warning ("`operator new' must not return NULL unless it is declared `throw()' (or -fcheck-new is in effect)");
6677 /* Effective C++ rule 15. See also start_function. */
6679 && DECL_NAME (current_function_decl
) == ansi_assopname(NOP_EXPR
)
6680 && retval
!= current_class_ref
)
6681 cp_warning ("`operator=' should return a reference to `*this'");
6683 /* The fabled Named Return Value optimization: If this is a
6684 value-returning function that always returns the same local
6685 variable, remember it.
6687 It might be nice to be more flexible, and choose the first suitable
6688 variable even if the function sometimes returns something else, but
6689 then we run the risk of clobbering the variable we chose if the other
6690 returned expression uses the chosen variable somehow. And people expect
6691 this restriction, anyway. (jason 2000-11-19) */
6693 if (fn_returns_value_p
&& optimize
)
6695 if (retval
!= NULL_TREE
6696 && (current_function_return_value
== NULL_TREE
6697 || current_function_return_value
== retval
)
6698 && TREE_CODE (retval
) == VAR_DECL
6699 && DECL_CONTEXT (retval
) == current_function_decl
6700 && ! TREE_STATIC (retval
)
6701 && ! DECL_USER_ALIGN (retval
)
6702 && same_type_p (TREE_TYPE (retval
),
6703 TREE_TYPE (TREE_TYPE (current_function_decl
))))
6704 current_function_return_value
= retval
;
6706 current_function_return_value
= error_mark_node
;
6709 /* We don't need to do any conversions when there's nothing being
6711 if (!retval
|| retval
== error_mark_node
)
6714 /* Do any required conversions. */
6715 if (retval
== result
|| DECL_CONSTRUCTOR_P (current_function_decl
))
6716 /* No conversions are required. */
6720 /* The type the function is declared to return. */
6721 tree functype
= TREE_TYPE (TREE_TYPE (current_function_decl
));
6723 /* First convert the value to the function's return type, then
6724 to the type of return value's location to handle the
6725 case that functype is smaller than the valtype. */
6726 retval
= convert_for_initialization
6727 (NULL_TREE
, functype
, retval
, LOOKUP_NORMAL
|LOOKUP_ONLYCONVERTING
,
6728 "return", NULL_TREE
, 0);
6729 retval
= convert (valtype
, retval
);
6731 /* If the conversion failed, treat this just like `return;'. */
6732 if (retval
== error_mark_node
)
6734 /* We can't initialize a register from a AGGR_INIT_EXPR. */
6735 else if (! current_function_returns_struct
6736 && TREE_CODE (retval
) == TARGET_EXPR
6737 && TREE_CODE (TREE_OPERAND (retval
, 1)) == AGGR_INIT_EXPR
)
6738 retval
= build (COMPOUND_EXPR
, TREE_TYPE (retval
), retval
,
6739 TREE_OPERAND (retval
, 0));
6741 maybe_warn_about_returning_address_of_local (retval
);
6744 /* Actually copy the value returned into the appropriate location. */
6745 if (retval
&& retval
!= result
)
6746 retval
= build (INIT_EXPR
, TREE_TYPE (result
), result
, retval
);
6752 /* Returns non-zero if the pointer-type FROM can be converted to the
6753 pointer-type TO via a qualification conversion. If CONSTP is -1,
6754 then we return non-zero if the pointers are similar, and the
6755 cv-qualification signature of FROM is a proper subset of that of TO.
6757 If CONSTP is positive, then all outer pointers have been
6761 comp_ptr_ttypes_real (to
, from
, constp
)
6765 int to_more_cv_qualified
= 0;
6767 for (; ; to
= TREE_TYPE (to
), from
= TREE_TYPE (from
))
6769 if (TREE_CODE (to
) != TREE_CODE (from
))
6772 if (TREE_CODE (from
) == OFFSET_TYPE
6773 && same_type_p (TYPE_OFFSET_BASETYPE (from
),
6774 TYPE_OFFSET_BASETYPE (to
)))
6777 /* Const and volatile mean something different for function types,
6778 so the usual checks are not appropriate. */
6779 if (TREE_CODE (to
) != FUNCTION_TYPE
&& TREE_CODE (to
) != METHOD_TYPE
)
6781 if (!at_least_as_qualified_p (to
, from
))
6784 if (!at_least_as_qualified_p (from
, to
))
6789 ++to_more_cv_qualified
;
6793 constp
&= TYPE_READONLY (to
);
6796 if (TREE_CODE (to
) != POINTER_TYPE
)
6798 same_type_ignoring_top_level_qualifiers_p (to
, from
)
6799 && (constp
>= 0 || to_more_cv_qualified
);
6803 /* When comparing, say, char ** to char const **, this function takes the
6804 'char *' and 'char const *'. Do not pass non-pointer types to this
6808 comp_ptr_ttypes (to
, from
)
6811 return comp_ptr_ttypes_real (to
, from
, 1);
6814 /* Returns 1 if to and from are (possibly multi-level) pointers to the same
6815 type or inheritance-related types, regardless of cv-quals. */
6818 ptr_reasonably_similar (to
, from
)
6821 for (; ; to
= TREE_TYPE (to
), from
= TREE_TYPE (from
))
6823 if (TREE_CODE (to
) != TREE_CODE (from
))
6826 if (TREE_CODE (from
) == OFFSET_TYPE
6827 && comptypes (TYPE_OFFSET_BASETYPE (to
),
6828 TYPE_OFFSET_BASETYPE (from
),
6829 COMPARE_BASE
| COMPARE_RELAXED
))
6832 if (TREE_CODE (to
) != POINTER_TYPE
)
6834 (TYPE_MAIN_VARIANT (to
), TYPE_MAIN_VARIANT (from
),
6835 COMPARE_BASE
| COMPARE_RELAXED
);
6839 /* Like comp_ptr_ttypes, for const_cast. */
6842 comp_ptr_ttypes_const (to
, from
)
6845 for (; ; to
= TREE_TYPE (to
), from
= TREE_TYPE (from
))
6847 if (TREE_CODE (to
) != TREE_CODE (from
))
6850 if (TREE_CODE (from
) == OFFSET_TYPE
6851 && same_type_p (TYPE_OFFSET_BASETYPE (from
),
6852 TYPE_OFFSET_BASETYPE (to
)))
6855 if (TREE_CODE (to
) != POINTER_TYPE
)
6856 return same_type_ignoring_top_level_qualifiers_p (to
, from
);
6860 /* Like comp_ptr_ttypes, for reinterpret_cast. */
6863 comp_ptr_ttypes_reinterpret (to
, from
)
6868 for (; ; to
= TREE_TYPE (to
), from
= TREE_TYPE (from
))
6870 if (TREE_CODE (from
) == OFFSET_TYPE
)
6871 from
= TREE_TYPE (from
);
6872 if (TREE_CODE (to
) == OFFSET_TYPE
)
6873 to
= TREE_TYPE (to
);
6875 /* Const and volatile mean something different for function types,
6876 so the usual checks are not appropriate. */
6877 if (TREE_CODE (from
) != FUNCTION_TYPE
&& TREE_CODE (from
) != METHOD_TYPE
6878 && TREE_CODE (to
) != FUNCTION_TYPE
&& TREE_CODE (to
) != METHOD_TYPE
)
6880 if (!at_least_as_qualified_p (to
, from
))
6884 && !at_least_as_qualified_p (from
, to
))
6886 constp
&= TYPE_READONLY (to
);
6889 if (TREE_CODE (from
) != POINTER_TYPE
6890 || TREE_CODE (to
) != POINTER_TYPE
)
6895 /* Returns the type-qualifier set corresponding to TYPE. */
6898 cp_type_quals (type
)
6901 type
= strip_array_types (type
);
6902 return TYPE_QUALS (type
);
6905 /* Returns non-zero if the TYPE contains a mutable member */
6908 cp_has_mutable_p (type
)
6911 type
= strip_array_types (type
);
6913 return CLASS_TYPE_P (type
) && CLASSTYPE_HAS_MUTABLE (type
);
6916 /* Subroutine of casts_away_constness. Make T1 and T2 point at
6917 exemplar types such that casting T1 to T2 is casting away castness
6918 if and only if there is no implicit conversion from T1 to T2. */
6921 casts_away_constness_r (t1
, t2
)
6928 /* [expr.const.cast]
6930 For multi-level pointer to members and multi-level mixed pointers
6931 and pointers to members (conv.qual), the "member" aspect of a
6932 pointer to member level is ignored when determining if a const
6933 cv-qualifier has been cast away. */
6934 if (TYPE_PTRMEM_P (*t1
))
6935 *t1
= build_pointer_type (TREE_TYPE (TREE_TYPE (*t1
)));
6936 if (TYPE_PTRMEM_P (*t2
))
6937 *t2
= build_pointer_type (TREE_TYPE (TREE_TYPE (*t2
)));
6939 /* [expr.const.cast]
6941 For two pointer types:
6943 X1 is T1cv1,1 * ... cv1,N * where T1 is not a pointer type
6944 X2 is T2cv2,1 * ... cv2,M * where T2 is not a pointer type
6947 casting from X1 to X2 casts away constness if, for a non-pointer
6948 type T there does not exist an implicit conversion (clause
6951 Tcv1,(N-K+1) * cv1,(N-K+2) * ... cv1,N *
6955 Tcv2,(M-K+1) * cv2,(M-K+2) * ... cv2,M *. */
6957 if (TREE_CODE (*t1
) != POINTER_TYPE
6958 || TREE_CODE (*t2
) != POINTER_TYPE
)
6960 *t1
= cp_build_qualified_type (void_type_node
,
6961 CP_TYPE_QUALS (*t1
));
6962 *t2
= cp_build_qualified_type (void_type_node
,
6963 CP_TYPE_QUALS (*t2
));
6967 quals1
= CP_TYPE_QUALS (*t1
);
6968 quals2
= CP_TYPE_QUALS (*t2
);
6969 *t1
= TREE_TYPE (*t1
);
6970 *t2
= TREE_TYPE (*t2
);
6971 casts_away_constness_r (t1
, t2
);
6972 *t1
= build_pointer_type (*t1
);
6973 *t2
= build_pointer_type (*t2
);
6974 *t1
= cp_build_qualified_type (*t1
, quals1
);
6975 *t2
= cp_build_qualified_type (*t2
, quals2
);
6978 /* Returns non-zero if casting from TYPE1 to TYPE2 casts away
6982 casts_away_constness (t1
, t2
)
6986 if (TREE_CODE (t2
) == REFERENCE_TYPE
)
6988 /* [expr.const.cast]
6990 Casting from an lvalue of type T1 to an lvalue of type T2
6991 using a reference cast casts away constness if a cast from an
6992 rvalue of type "pointer to T1" to the type "pointer to T2"
6993 casts away constness. */
6994 t1
= (TREE_CODE (t1
) == REFERENCE_TYPE
6995 ? TREE_TYPE (t1
) : t1
);
6996 return casts_away_constness (build_pointer_type (t1
),
6997 build_pointer_type (TREE_TYPE (t2
)));
7000 if (TYPE_PTRMEM_P (t1
) && TYPE_PTRMEM_P (t2
))
7001 /* [expr.const.cast]
7003 Casting from an rvalue of type "pointer to data member of X
7004 of type T1" to the type "pointer to data member of Y of type
7005 T2" casts away constness if a cast from an rvalue of type
7006 "poitner to T1" to the type "pointer to T2" casts away
7008 return casts_away_constness
7009 (build_pointer_type (TREE_TYPE (TREE_TYPE (t1
))),
7010 build_pointer_type (TREE_TYPE (TREE_TYPE (t2
))));
7012 /* Casting away constness is only something that makes sense for
7013 pointer or reference types. */
7014 if (TREE_CODE (t1
) != POINTER_TYPE
7015 || TREE_CODE (t2
) != POINTER_TYPE
)
7018 /* Top-level qualifiers don't matter. */
7019 t1
= TYPE_MAIN_VARIANT (t1
);
7020 t2
= TYPE_MAIN_VARIANT (t2
);
7021 casts_away_constness_r (&t1
, &t2
);
7022 if (!can_convert (t2
, t1
))
7028 /* Returns TYPE with its cv qualifiers removed
7029 TYPE is T cv* .. *cv where T is not a pointer type,
7030 returns T * .. *. (If T is an array type, then the cv qualifiers
7031 above are those of the array members.) */
7034 strip_all_pointer_quals (type
)
7037 if (TREE_CODE (type
) == POINTER_TYPE
)
7038 return build_pointer_type (strip_all_pointer_quals (TREE_TYPE (type
)));
7039 else if (TREE_CODE (type
) == OFFSET_TYPE
)
7040 return build_offset_type (TYPE_OFFSET_BASETYPE (type
),
7041 strip_all_pointer_quals (TREE_TYPE (type
)));
7043 return TYPE_MAIN_VARIANT (type
);