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, 2002 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 cp_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 qualify_type_recursive
PARAMS ((tree
, tree
));
61 static tree get_delta_difference
PARAMS ((tree
, tree
, int));
62 static int comp_cv_target_types
PARAMS ((tree
, tree
, int));
63 static void casts_away_constness_r
PARAMS ((tree
*, tree
*));
64 static int casts_away_constness
PARAMS ((tree
, tree
));
65 static void maybe_warn_about_returning_address_of_local
PARAMS ((tree
));
66 static tree strip_all_pointer_quals
PARAMS ((tree
));
68 /* Return the target type of TYPE, which means return T for:
69 T*, T&, T[], T (...), and otherwise, just T. */
75 if (TREE_CODE (type
) == REFERENCE_TYPE
)
76 type
= TREE_TYPE (type
);
77 while (TREE_CODE (type
) == POINTER_TYPE
78 || TREE_CODE (type
) == ARRAY_TYPE
79 || TREE_CODE (type
) == FUNCTION_TYPE
80 || TREE_CODE (type
) == METHOD_TYPE
81 || TREE_CODE (type
) == OFFSET_TYPE
)
82 type
= TREE_TYPE (type
);
86 /* Do `exp = require_complete_type (exp);' to make sure exp
87 does not have an incomplete type. (That includes void types.)
88 Returns the error_mark_node if the VALUE does not have
89 complete type when this function returns. */
92 require_complete_type (value
)
97 if (processing_template_decl
|| value
== error_mark_node
)
100 if (TREE_CODE (value
) == OVERLOAD
)
101 type
= unknown_type_node
;
103 type
= TREE_TYPE (value
);
105 /* First, detect a valid value with a complete type. */
106 if (COMPLETE_TYPE_P (type
))
109 /* If we see X::Y, we build an OFFSET_TYPE which has
110 not been laid out. Try to avoid an error by interpreting
111 it as this->X::Y, if reasonable. */
112 if (TREE_CODE (value
) == OFFSET_REF
113 && current_class_ref
!= 0
114 && TREE_OPERAND (value
, 0) == current_class_ref
)
116 value
= resolve_offset_ref (value
);
117 return require_complete_type (value
);
120 if (complete_type_or_else (type
, value
))
123 return error_mark_node
;
126 /* Try to complete TYPE, if it is incomplete. For example, if TYPE is
127 a template instantiation, do the instantiation. Returns TYPE,
128 whether or not it could be completed, unless something goes
129 horribly wrong, in which case the error_mark_node is returned. */
135 if (type
== NULL_TREE
)
136 /* Rather than crash, we return something sure to cause an error
138 return error_mark_node
;
140 if (type
== error_mark_node
|| COMPLETE_TYPE_P (type
))
142 else if (TREE_CODE (type
) == ARRAY_TYPE
&& TYPE_DOMAIN (type
))
144 tree t
= complete_type (TREE_TYPE (type
));
145 if (COMPLETE_TYPE_P (t
) && ! processing_template_decl
)
147 TYPE_NEEDS_CONSTRUCTING (type
)
148 = TYPE_NEEDS_CONSTRUCTING (TYPE_MAIN_VARIANT (t
));
149 TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type
)
150 = TYPE_HAS_NONTRIVIAL_DESTRUCTOR (TYPE_MAIN_VARIANT (t
));
152 else if (CLASS_TYPE_P (type
) && CLASSTYPE_TEMPLATE_INSTANTIATION (type
))
153 instantiate_class_template (TYPE_MAIN_VARIANT (type
));
158 /* Like complete_type, but issue an error if the TYPE cannot be
159 completed. VALUE is used for informative diagnostics. WARN_ONLY
160 will cause a warning message to be printed, instead of an error.
161 Returns NULL_TREE if the type cannot be made complete. */
164 complete_type_or_diagnostic (type
, value
, warn_only
)
169 type
= complete_type (type
);
170 if (type
== error_mark_node
)
171 /* We already issued an error. */
173 else if (!COMPLETE_TYPE_P (type
))
175 cxx_incomplete_type_diagnostic (value
, type
, warn_only
);
182 /* Return truthvalue of whether type of EXP is instantiated. */
188 return (TREE_CODE (exp
) == OVERLOAD
189 || TREE_CODE (exp
) == TREE_LIST
190 || TREE_TYPE (exp
) == unknown_type_node
191 || (TREE_CODE (TREE_TYPE (exp
)) == OFFSET_TYPE
192 && TREE_TYPE (TREE_TYPE (exp
)) == unknown_type_node
));
195 /* Return a pointer or pointer to member type similar to T1, with a
196 cv-qualification signature that is the union of the cv-qualification
197 signatures of T1 and T2: [expr.rel], [expr.eq]. */
200 qualify_type_recursive (t1
, t2
)
203 if ((TYPE_PTR_P (t1
) && TYPE_PTR_P (t2
))
204 || (TYPE_PTRMEM_P (t1
) && TYPE_PTRMEM_P (t2
)))
211 tree attributes
= (*targetm
.merge_type_attributes
) (t1
, t2
);
213 if (TYPE_PTRMEM_P (t1
))
215 b1
= TYPE_PTRMEM_CLASS_TYPE (t1
);
216 tt1
= TYPE_PTRMEM_POINTED_TO_TYPE (t1
);
217 tt2
= TYPE_PTRMEM_POINTED_TO_TYPE (t2
);
222 tt1
= TREE_TYPE (t1
);
223 tt2
= TREE_TYPE (t2
);
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 t1
= build_ptrmem_type (b1
, tgt
);
232 t1
= build_pointer_type (tgt
);
233 t1
= build_type_attribute_variant (t1
, attributes
);
238 /* Return the common type of two parameter lists.
239 We assume that comptypes has already been done and returned 1;
240 if that isn't so, this may crash.
242 As an optimization, free the space we allocate if the parameter
243 lists are already common. */
249 tree oldargs
= p1
, newargs
, n
;
253 len
= list_length (p1
);
254 newargs
= tree_last (p1
);
256 if (newargs
== void_list_node
)
265 newargs
= tree_cons (NULL_TREE
, NULL_TREE
, newargs
);
270 p1
= TREE_CHAIN (p1
), p2
= TREE_CHAIN (p2
), n
= TREE_CHAIN (n
), i
++)
272 if (TREE_PURPOSE (p1
) && !TREE_PURPOSE (p2
))
274 TREE_PURPOSE (n
) = TREE_PURPOSE (p1
);
277 else if (! TREE_PURPOSE (p1
))
279 if (TREE_PURPOSE (p2
))
281 TREE_PURPOSE (n
) = TREE_PURPOSE (p2
);
287 if (1 != simple_cst_equal (TREE_PURPOSE (p1
), TREE_PURPOSE (p2
)))
289 TREE_PURPOSE (n
) = TREE_PURPOSE (p2
);
291 if (TREE_VALUE (p1
) != TREE_VALUE (p2
))
294 TREE_VALUE (n
) = merge_types (TREE_VALUE (p1
), TREE_VALUE (p2
));
297 TREE_VALUE (n
) = TREE_VALUE (p1
);
305 /* Given a type, perhaps copied for a typedef,
306 find the "original" version of it. */
311 while (TYPE_NAME (t
) != NULL_TREE
)
313 tree x
= TYPE_NAME (t
);
314 if (TREE_CODE (x
) != TYPE_DECL
)
316 x
= DECL_ORIGINAL_TYPE (x
);
324 /* T1 and T2 are arithmetic or enumeration types. Return the type
325 that will result from the "usual arithmetic conversions" on T1 and
326 T2 as described in [expr]. */
329 type_after_usual_arithmetic_conversions (t1
, t2
)
333 enum tree_code code1
= TREE_CODE (t1
);
334 enum tree_code code2
= TREE_CODE (t2
);
337 /* FIXME: Attributes. */
338 my_friendly_assert (ARITHMETIC_TYPE_P (t1
)
339 || TREE_CODE (t1
) == COMPLEX_TYPE
340 || TREE_CODE (t1
) == ENUMERAL_TYPE
,
342 my_friendly_assert (ARITHMETIC_TYPE_P (t2
)
343 || TREE_CODE (t2
) == COMPLEX_TYPE
344 || TREE_CODE (t2
) == ENUMERAL_TYPE
,
347 /* In what follows, we slightly generalize the rules given in [expr] so
348 as to deal with `long long' and `complex'. First, merge the
350 attributes
= (*targetm
.merge_type_attributes
) (t1
, t2
);
352 /* If one type is complex, form the common type of the non-complex
353 components, then make that complex. Use T1 or T2 if it is the
355 if (code1
== COMPLEX_TYPE
|| code2
== COMPLEX_TYPE
)
357 tree subtype1
= code1
== COMPLEX_TYPE
? TREE_TYPE (t1
) : t1
;
358 tree subtype2
= code2
== COMPLEX_TYPE
? TREE_TYPE (t2
) : t2
;
360 = type_after_usual_arithmetic_conversions (subtype1
, subtype2
);
362 if (code1
== COMPLEX_TYPE
&& TREE_TYPE (t1
) == subtype
)
363 return build_type_attribute_variant (t1
, attributes
);
364 else if (code2
== COMPLEX_TYPE
&& TREE_TYPE (t2
) == subtype
)
365 return build_type_attribute_variant (t2
, attributes
);
367 return build_type_attribute_variant (build_complex_type (subtype
),
371 /* If only one is real, use it as the result. */
372 if (code1
== REAL_TYPE
&& code2
!= REAL_TYPE
)
373 return build_type_attribute_variant (t1
, attributes
);
374 if (code2
== REAL_TYPE
&& code1
!= REAL_TYPE
)
375 return build_type_attribute_variant (t2
, attributes
);
377 /* Perform the integral promotions. */
378 if (code1
!= REAL_TYPE
)
380 t1
= type_promotes_to (t1
);
381 t2
= type_promotes_to (t2
);
384 /* Both real or both integers; use the one with greater precision. */
385 if (TYPE_PRECISION (t1
) > TYPE_PRECISION (t2
))
386 return build_type_attribute_variant (t1
, attributes
);
387 else if (TYPE_PRECISION (t2
) > TYPE_PRECISION (t1
))
388 return build_type_attribute_variant (t2
, attributes
);
390 /* The types are the same; no need to do anything fancy. */
391 if (TYPE_MAIN_VARIANT (t1
) == TYPE_MAIN_VARIANT (t2
))
392 return build_type_attribute_variant (t1
, attributes
);
394 if (code1
!= REAL_TYPE
)
396 /* If one is a sizetype, use it so size_binop doesn't blow up. */
397 if (TYPE_IS_SIZETYPE (t1
) > TYPE_IS_SIZETYPE (t2
))
398 return build_type_attribute_variant (t1
, attributes
);
399 if (TYPE_IS_SIZETYPE (t2
) > TYPE_IS_SIZETYPE (t1
))
400 return build_type_attribute_variant (t2
, attributes
);
402 /* If one is unsigned long long, then convert the other to unsigned
404 if (same_type_p (TYPE_MAIN_VARIANT (t1
), long_long_unsigned_type_node
)
405 || same_type_p (TYPE_MAIN_VARIANT (t2
), long_long_unsigned_type_node
))
406 return build_type_attribute_variant (long_long_unsigned_type_node
,
408 /* If one is a long long, and the other is an unsigned long, and
409 long long can represent all the values of an unsigned long, then
410 convert to a long long. Otherwise, convert to an unsigned long
411 long. Otherwise, if either operand is long long, convert the
414 Since we're here, we know the TYPE_PRECISION is the same;
415 therefore converting to long long cannot represent all the values
416 of an unsigned long, so we choose unsigned long long in that
418 if (same_type_p (TYPE_MAIN_VARIANT (t1
), long_long_integer_type_node
)
419 || same_type_p (TYPE_MAIN_VARIANT (t2
), long_long_integer_type_node
))
421 tree t
= ((TREE_UNSIGNED (t1
) || TREE_UNSIGNED (t2
))
422 ? long_long_unsigned_type_node
423 : long_long_integer_type_node
);
424 return build_type_attribute_variant (t
, attributes
);
427 /* Go through the same procedure, but for longs. */
428 if (same_type_p (TYPE_MAIN_VARIANT (t1
), long_unsigned_type_node
)
429 || same_type_p (TYPE_MAIN_VARIANT (t2
), long_unsigned_type_node
))
430 return build_type_attribute_variant (long_unsigned_type_node
,
432 if (same_type_p (TYPE_MAIN_VARIANT (t1
), long_integer_type_node
)
433 || same_type_p (TYPE_MAIN_VARIANT (t2
), long_integer_type_node
))
435 tree t
= ((TREE_UNSIGNED (t1
) || TREE_UNSIGNED (t2
))
436 ? long_unsigned_type_node
: long_integer_type_node
);
437 return build_type_attribute_variant (t
, attributes
);
439 /* Otherwise prefer the unsigned one. */
440 if (TREE_UNSIGNED (t1
))
441 return build_type_attribute_variant (t1
, attributes
);
443 return build_type_attribute_variant (t2
, attributes
);
447 if (same_type_p (TYPE_MAIN_VARIANT (t1
), long_double_type_node
)
448 || same_type_p (TYPE_MAIN_VARIANT (t2
), long_double_type_node
))
449 return build_type_attribute_variant (long_double_type_node
,
451 if (same_type_p (TYPE_MAIN_VARIANT (t1
), double_type_node
)
452 || same_type_p (TYPE_MAIN_VARIANT (t2
), double_type_node
))
453 return build_type_attribute_variant (double_type_node
,
455 if (same_type_p (TYPE_MAIN_VARIANT (t1
), float_type_node
)
456 || same_type_p (TYPE_MAIN_VARIANT (t2
), float_type_node
))
457 return build_type_attribute_variant (float_type_node
,
460 /* Two floating-point types whose TYPE_MAIN_VARIANTs are none of
461 the standard C++ floating-point types. Logic earlier in this
462 function has already eliminated the possibility that
463 TYPE_PRECISION (t2) != TYPE_PRECISION (t1), so there's no
464 compelling reason to choose one or the other. */
465 return build_type_attribute_variant (t1
, attributes
);
469 /* Return the composite pointer type (see [expr.rel]) for T1 and T2.
470 ARG1 and ARG2 are the values with those types. The LOCATION is a
471 string describing the current location, in case an error occurs. */
474 composite_pointer_type (t1
, t2
, arg1
, arg2
, location
)
479 const char* location
;
486 If one operand is a null pointer constant, the composite pointer
487 type is the type of the other operand. */
488 if (null_ptr_cst_p (arg1
))
490 if (null_ptr_cst_p (arg2
))
493 /* Deal with pointer-to-member functions in the same way as we deal
494 with pointers to functions. */
495 if (TYPE_PTRMEMFUNC_P (t1
))
496 t1
= TYPE_PTRMEMFUNC_FN_TYPE (t1
);
497 if (TYPE_PTRMEMFUNC_P (t2
))
498 t2
= TYPE_PTRMEMFUNC_FN_TYPE (t2
);
500 /* Merge the attributes. */
501 attributes
= (*targetm
.merge_type_attributes
) (t1
, t2
);
507 If one of the operands has type "pointer to cv1 void*", then
508 the other has type "pointer to cv2T", and the composite pointer
509 type is "pointer to cv12 void", where cv12 is the union of cv1
512 If either type is a pointer to void, make sure it is T1. */
513 if (VOID_TYPE_P (TREE_TYPE (t2
)))
520 /* Now, if T1 is a pointer to void, merge the qualifiers. */
521 if (VOID_TYPE_P (TREE_TYPE (t1
)))
523 if (pedantic
&& TYPE_PTRFN_P (t2
))
524 pedwarn ("ISO C++ forbids %s between pointer of type `void *' and pointer-to-function", location
);
527 result_type
= cp_build_qualified_type (void_type_node
,
529 | cp_type_quals (t2
)));
530 result_type
= build_pointer_type (result_type
);
534 tree full1
= qualify_type_recursive (t1
, t2
);
535 tree full2
= qualify_type_recursive (t2
, t1
);
537 int val
= comp_target_types (full1
, full2
, 1);
545 pedwarn ("%s between distinct pointer types `%T' and `%T' lacks a cast",
547 result_type
= ptr_type_node
;
551 return build_type_attribute_variant (result_type
, attributes
);
554 /* Return the merged type of two types.
555 We assume that comptypes has already been done and returned 1;
556 if that isn't so, this may crash.
558 This just combines attributes and default arguments; any other
559 differences would cause the two types to compare unalike. */
565 register enum tree_code code1
;
566 register enum tree_code code2
;
569 /* Save time if the two types are the same. */
572 if (original_type (t1
) == original_type (t2
))
575 /* If one type is nonsense, use the other. */
576 if (t1
== error_mark_node
)
578 if (t2
== error_mark_node
)
581 /* Merge the attributes. */
582 attributes
= (*targetm
.merge_type_attributes
) (t1
, t2
);
584 /* Treat an enum type as the unsigned integer type of the same width. */
586 if (TYPE_PTRMEMFUNC_P (t1
))
587 t1
= TYPE_PTRMEMFUNC_FN_TYPE (t1
);
588 if (TYPE_PTRMEMFUNC_P (t2
))
589 t2
= TYPE_PTRMEMFUNC_FN_TYPE (t2
);
591 code1
= TREE_CODE (t1
);
592 code2
= TREE_CODE (t2
);
598 /* For two pointers, do this recursively on the target type. */
600 tree target
= merge_types (TREE_TYPE (t1
), TREE_TYPE (t2
));
601 int quals
= cp_type_quals (t1
);
603 if (code1
== POINTER_TYPE
)
604 t1
= build_pointer_type (target
);
606 t1
= build_reference_type (target
);
607 t1
= build_type_attribute_variant (t1
, attributes
);
608 t1
= cp_build_qualified_type (t1
, quals
);
610 if (TREE_CODE (target
) == METHOD_TYPE
)
611 t1
= build_ptrmemfunc_type (t1
);
618 tree base
= TYPE_OFFSET_BASETYPE (t1
);
619 tree target
= merge_types (TREE_TYPE (t1
), TREE_TYPE (t2
));
620 t1
= build_offset_type (base
, target
);
626 tree elt
= merge_types (TREE_TYPE (t1
), TREE_TYPE (t2
));
627 /* Save space: see if the result is identical to one of the args. */
628 if (elt
== TREE_TYPE (t1
) && TYPE_DOMAIN (t1
))
629 return build_type_attribute_variant (t1
, attributes
);
630 if (elt
== TREE_TYPE (t2
) && TYPE_DOMAIN (t2
))
631 return build_type_attribute_variant (t2
, attributes
);
632 /* Merge the element types, and have a size if either arg has one. */
633 t1
= build_cplus_array_type
634 (elt
, TYPE_DOMAIN (TYPE_DOMAIN (t1
) ? t1
: t2
));
639 /* Function types: prefer the one that specified arg types.
640 If both do, merge the arg types. Also merge the return types. */
642 tree valtype
= merge_types (TREE_TYPE (t1
), TREE_TYPE (t2
));
643 tree p1
= TYPE_ARG_TYPES (t1
);
644 tree p2
= TYPE_ARG_TYPES (t2
);
647 /* Save space: see if the result is identical to one of the args. */
648 if (valtype
== TREE_TYPE (t1
) && ! p2
)
649 return build_type_attribute_variant (t1
, attributes
);
650 if (valtype
== TREE_TYPE (t2
) && ! p1
)
651 return build_type_attribute_variant (t2
, attributes
);
653 /* Simple way if one arg fails to specify argument types. */
654 if (p1
== NULL_TREE
|| TREE_VALUE (p1
) == void_type_node
)
656 rval
= build_function_type (valtype
, p2
);
657 if ((raises
= TYPE_RAISES_EXCEPTIONS (t2
)))
658 rval
= build_exception_variant (rval
, raises
);
659 return build_type_attribute_variant (rval
, attributes
);
661 raises
= TYPE_RAISES_EXCEPTIONS (t1
);
662 if (p2
== NULL_TREE
|| TREE_VALUE (p2
) == void_type_node
)
664 rval
= build_function_type (valtype
, p1
);
666 rval
= build_exception_variant (rval
, raises
);
667 return build_type_attribute_variant (rval
, attributes
);
670 rval
= build_function_type (valtype
, commonparms (p1
, p2
));
671 t1
= build_exception_variant (rval
, raises
);
677 /* Get this value the long way, since TYPE_METHOD_BASETYPE
678 is just the main variant of this. */
679 tree basetype
= TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (t2
)));
680 tree raises
= TYPE_RAISES_EXCEPTIONS (t1
);
683 /* If this was a member function type, get back to the
684 original type of type member function (i.e., without
685 the class instance variable up front. */
686 t1
= build_function_type (TREE_TYPE (t1
),
687 TREE_CHAIN (TYPE_ARG_TYPES (t1
)));
688 t2
= build_function_type (TREE_TYPE (t2
),
689 TREE_CHAIN (TYPE_ARG_TYPES (t2
)));
690 t3
= merge_types (t1
, t2
);
691 t3
= build_cplus_method_type (basetype
, TREE_TYPE (t3
),
692 TYPE_ARG_TYPES (t3
));
693 t1
= build_exception_variant (t3
, raises
);
699 return build_type_attribute_variant (t1
, attributes
);
702 /* Return the common type of two types.
703 We assume that comptypes has already been done and returned 1;
704 if that isn't so, this may crash.
706 This is the type for the result of most arithmetic operations
707 if the operands have the given two types. */
713 enum tree_code code1
;
714 enum tree_code code2
;
716 /* If one type is nonsense, bail. */
717 if (t1
== error_mark_node
|| t2
== error_mark_node
)
718 return error_mark_node
;
720 code1
= TREE_CODE (t1
);
721 code2
= TREE_CODE (t2
);
723 if ((ARITHMETIC_TYPE_P (t1
) || code1
== ENUMERAL_TYPE
724 || code1
== COMPLEX_TYPE
)
725 && (ARITHMETIC_TYPE_P (t2
) || code2
== ENUMERAL_TYPE
726 || code2
== COMPLEX_TYPE
))
727 return type_after_usual_arithmetic_conversions (t1
, t2
);
729 else if ((TYPE_PTR_P (t1
) && TYPE_PTR_P (t2
))
730 || (TYPE_PTRMEM_P (t1
) && TYPE_PTRMEM_P (t2
))
731 || (TYPE_PTRMEMFUNC_P (t1
) && TYPE_PTRMEMFUNC_P (t2
)))
732 return composite_pointer_type (t1
, t2
, error_mark_node
, error_mark_node
,
739 /* Compare two exception specifier types for exactness or subsetness, if
740 allowed. Returns 0 for mismatch, 1 for same, 2 if B is allowed by A.
742 [except.spec] "If a class X ... objects of class X or any class publicly
743 and unambigously derrived from X. Similarly, if a pointer type Y * ...
744 exceptions of type Y * or that are pointers to any type publicly and
745 unambigously derrived from Y. Otherwise a function only allows exceptions
746 that have the same type ..."
747 This does not mention cv qualifiers and is different to what throw
748 [except.throw] and catch [except.catch] will do. They will ignore the
749 top level cv qualifiers, and allow qualifiers in the pointer to class
752 We implement the letter of the standard. */
755 comp_except_types (a
, b
, exact
)
759 if (same_type_p (a
, b
))
763 if (cp_type_quals (a
) || cp_type_quals (b
))
766 if (TREE_CODE (a
) == POINTER_TYPE
767 && TREE_CODE (b
) == POINTER_TYPE
)
771 if (cp_type_quals (a
) || cp_type_quals (b
))
775 if (TREE_CODE (a
) != RECORD_TYPE
776 || TREE_CODE (b
) != RECORD_TYPE
)
779 if (ACCESSIBLY_UNIQUELY_DERIVED_P (a
, b
))
785 /* Return 1 if TYPE1 and TYPE2 are equivalent exception specifiers.
786 If EXACT is 0, T2 can be stricter than T1 (according to 15.4/7),
787 otherwise it must be exact. Exception lists are unordered, but
788 we've already filtered out duplicates. Most lists will be in order,
789 we should try to make use of that. */
792 comp_except_specs (t1
, t2
, exact
)
803 if (t1
== NULL_TREE
) /* T1 is ... */
804 return t2
== NULL_TREE
|| !exact
;
805 if (!TREE_VALUE (t1
)) /* t1 is EMPTY */
806 return t2
!= NULL_TREE
&& !TREE_VALUE (t2
);
807 if (t2
== NULL_TREE
) /* T2 is ... */
809 if (TREE_VALUE (t1
) && !TREE_VALUE (t2
)) /* T2 is EMPTY, T1 is not */
812 /* Neither set is ... or EMPTY, make sure each part of T2 is in T1.
813 Count how many we find, to determine exactness. For exact matching and
814 ordered T1, T2, this is an O(n) operation, otherwise its worst case is
816 for (base
= t1
; t2
!= NULL_TREE
; t2
= TREE_CHAIN (t2
))
818 for (probe
= base
; probe
!= NULL_TREE
; probe
= TREE_CHAIN (probe
))
820 tree a
= TREE_VALUE (probe
);
821 tree b
= TREE_VALUE (t2
);
823 if (comp_except_types (a
, b
, exact
))
825 if (probe
== base
&& exact
)
826 base
= TREE_CHAIN (probe
);
831 if (probe
== NULL_TREE
)
834 return !exact
|| base
== NULL_TREE
|| length
== list_length (t1
);
837 /* Compare the array types T1 and T2, using CMP as the type comparison
838 function for the element types. STRICT is as for comptypes. */
841 comp_array_types (cmp
, t1
, t2
, strict
)
842 register int (*cmp
) PARAMS ((tree
, tree
, int));
852 /* The type of the array elements must be the same. */
853 if (!(TREE_TYPE (t1
) == TREE_TYPE (t2
)
854 || (*cmp
) (TREE_TYPE (t1
), TREE_TYPE (t2
),
855 strict
& ~COMPARE_REDECLARATION
)))
858 d1
= TYPE_DOMAIN (t1
);
859 d2
= TYPE_DOMAIN (t2
);
864 /* If one of the arrays is dimensionless, and the other has a
865 dimension, they are of different types. However, it is valid to
873 declarations for an array object can specify
874 array types that differ by the presence or absence of a major
875 array bound (_dcl.array_). */
877 return strict
& COMPARE_REDECLARATION
;
879 /* Check that the dimensions are the same. */
880 return (cp_tree_equal (TYPE_MIN_VALUE (d1
),
882 && cp_tree_equal (TYPE_MAX_VALUE (d1
),
883 TYPE_MAX_VALUE (d2
)));
886 /* Return 1 if T1 and T2 are compatible types for assignment or
887 various other operations. STRICT is a bitwise-or of the COMPARE_*
891 comptypes (t1
, t2
, strict
)
897 int orig_strict
= strict
;
899 /* The special exemption for redeclaring array types without an
900 array bound only applies at the top level:
905 is invalid, for example. */
906 strict
&= ~COMPARE_REDECLARATION
;
908 /* Suppress errors caused by previously reported errors */
912 /* This should never happen. */
913 my_friendly_assert (t1
!= error_mark_node
, 307);
915 if (t2
== error_mark_node
)
918 /* If either type is the internal version of sizetype, return the
920 if (TREE_CODE (t1
) == INTEGER_TYPE
&& TYPE_IS_SIZETYPE (t1
)
921 && TYPE_DOMAIN (t1
) != 0)
922 t1
= TYPE_DOMAIN (t1
);
924 if (TREE_CODE (t2
) == INTEGER_TYPE
&& TYPE_IS_SIZETYPE (t2
)
925 && TYPE_DOMAIN (t2
) != 0)
926 t2
= TYPE_DOMAIN (t2
);
928 if (strict
& COMPARE_RELAXED
)
930 /* Treat an enum type as the unsigned integer type of the same width. */
932 if (TREE_CODE (t1
) == ENUMERAL_TYPE
)
933 t1
= c_common_type_for_size (TYPE_PRECISION (t1
), 1);
934 if (TREE_CODE (t2
) == ENUMERAL_TYPE
)
935 t2
= c_common_type_for_size (TYPE_PRECISION (t2
), 1);
941 if (TYPE_PTRMEMFUNC_P (t1
))
942 t1
= TYPE_PTRMEMFUNC_FN_TYPE (t1
);
943 if (TYPE_PTRMEMFUNC_P (t2
))
944 t2
= TYPE_PTRMEMFUNC_FN_TYPE (t2
);
946 /* Different classes of types can't be compatible. */
947 if (TREE_CODE (t1
) != TREE_CODE (t2
))
950 /* Qualifiers must match. */
951 if (cp_type_quals (t1
) != cp_type_quals (t2
))
953 if (strict
== COMPARE_STRICT
954 && TYPE_FOR_JAVA (t1
) != TYPE_FOR_JAVA (t2
))
957 /* Allow for two different type nodes which have essentially the same
958 definition. Note that we already checked for equality of the type
959 qualifiers (just above). */
961 if (TYPE_MAIN_VARIANT (t1
) == TYPE_MAIN_VARIANT (t2
))
964 if (strict
& COMPARE_NO_ATTRIBUTES
)
966 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
967 else if (! (attrval
= (*targetm
.comp_type_attributes
) (t1
, t2
)))
970 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
973 switch (TREE_CODE (t1
))
975 case TEMPLATE_TEMPLATE_PARM
:
976 case BOUND_TEMPLATE_TEMPLATE_PARM
:
977 if (TEMPLATE_TYPE_IDX (t1
) != TEMPLATE_TYPE_IDX (t2
)
978 || TEMPLATE_TYPE_LEVEL (t1
) != TEMPLATE_TYPE_LEVEL (t2
))
980 if (! comp_template_parms
981 (DECL_TEMPLATE_PARMS (TEMPLATE_TEMPLATE_PARM_TEMPLATE_DECL (t1
)),
982 DECL_TEMPLATE_PARMS (TEMPLATE_TEMPLATE_PARM_TEMPLATE_DECL (t2
))))
984 if (TREE_CODE (t1
) == TEMPLATE_TEMPLATE_PARM
)
986 /* Don't check inheritance. */
987 strict
= COMPARE_STRICT
;
992 if (TYPE_TEMPLATE_INFO (t1
) && TYPE_TEMPLATE_INFO (t2
)
993 && (TYPE_TI_TEMPLATE (t1
) == TYPE_TI_TEMPLATE (t2
)
994 || TREE_CODE (t1
) == BOUND_TEMPLATE_TEMPLATE_PARM
))
995 val
= comp_template_args (TYPE_TI_ARGS (t1
),
998 if ((strict
& COMPARE_BASE
) && DERIVED_FROM_P (t1
, t2
))
1000 else if ((strict
& COMPARE_RELAXED
) && DERIVED_FROM_P (t2
, t1
))
1005 val
= (comptypes (build_pointer_type (TYPE_OFFSET_BASETYPE (t1
)),
1006 build_pointer_type (TYPE_OFFSET_BASETYPE (t2
)), strict
)
1007 && comptypes (TREE_TYPE (t1
), TREE_TYPE (t2
), strict
));
1011 case REFERENCE_TYPE
:
1012 t1
= TREE_TYPE (t1
);
1013 t2
= TREE_TYPE (t2
);
1014 /* first, check whether the referred types match with the
1015 required level of strictness */
1016 val
= comptypes (t1
, t2
, strict
);
1019 if (TREE_CODE (t1
) == RECORD_TYPE
1020 && TREE_CODE (t2
) == RECORD_TYPE
)
1026 val
= ((TREE_TYPE (t1
) == TREE_TYPE (t2
)
1027 || comptypes (TREE_TYPE (t1
), TREE_TYPE (t2
), strict
))
1028 && compparms (TYPE_ARG_TYPES (t1
), TYPE_ARG_TYPES (t2
)));
1032 /* Target types must match incl. qualifiers. We use ORIG_STRICT
1033 here since this is the one place where
1034 COMPARE_REDECLARATION should be used. */
1035 val
= comp_array_types (comptypes
, t1
, t2
, orig_strict
);
1038 case TEMPLATE_TYPE_PARM
:
1039 return TEMPLATE_TYPE_IDX (t1
) == TEMPLATE_TYPE_IDX (t2
)
1040 && TEMPLATE_TYPE_LEVEL (t1
) == TEMPLATE_TYPE_LEVEL (t2
);
1043 if (cp_tree_equal (TYPENAME_TYPE_FULLNAME (t1
),
1044 TYPENAME_TYPE_FULLNAME (t2
)) < 1)
1046 return same_type_p (TYPE_CONTEXT (t1
), TYPE_CONTEXT (t2
));
1048 case UNBOUND_CLASS_TEMPLATE
:
1049 if (cp_tree_equal (TYPE_IDENTIFIER (t1
),
1050 TYPE_IDENTIFIER (t2
)) < 1)
1052 return same_type_p (TYPE_CONTEXT (t1
), TYPE_CONTEXT (t2
));
1055 return same_type_p (TREE_TYPE (t1
), TREE_TYPE (t2
));
1060 return attrval
== 2 && val
== 1 ? 2 : val
;
1063 /* Subroutine of comp_target-types. Make sure that the cv-quals change
1064 only in the same direction as the target type. */
1067 comp_cv_target_types (ttl
, ttr
, nptrs
)
1073 if (!at_least_as_qualified_p (ttl
, ttr
)
1074 && !at_least_as_qualified_p (ttr
, ttl
))
1075 /* The qualifications are incomparable. */
1078 if (TYPE_MAIN_VARIANT (ttl
) == TYPE_MAIN_VARIANT (ttr
))
1079 return more_qualified_p (ttr
, ttl
) ? -1 : 1;
1081 t
= comp_target_types (ttl
, ttr
, nptrs
);
1082 if ((t
== 1 && at_least_as_qualified_p (ttl
, ttr
))
1083 || (t
== -1 && at_least_as_qualified_p (ttr
, ttl
)))
1089 /* Return 1 or -1 if TTL and TTR are pointers to types that are equivalent,
1090 ignoring their qualifiers, 0 if not. Return 1 means that TTR can be
1091 converted to TTL. Return -1 means that TTL can be converted to TTR but
1094 NPTRS is the number of pointers we can strip off and keep cool.
1095 This is used to permit (for aggr A, aggr B) A, B* to convert to A*,
1096 but to not permit B** to convert to A**.
1098 This should go away. Callers should use can_convert or something
1099 similar instead. (jason 17 Apr 1997) */
1102 comp_target_types (ttl
, ttr
, nptrs
)
1106 ttl
= TYPE_MAIN_VARIANT (ttl
);
1107 ttr
= TYPE_MAIN_VARIANT (ttr
);
1108 if (same_type_p (ttl
, ttr
))
1111 if (TREE_CODE (ttr
) != TREE_CODE (ttl
))
1114 if ((TREE_CODE (ttr
) == POINTER_TYPE
1115 || TREE_CODE (ttr
) == REFERENCE_TYPE
)
1116 /* If we get a pointer with nptrs == 0, we don't allow any tweaking
1117 of the type pointed to. This is necessary for reference init
1118 semantics. We won't get here from a previous call with nptrs == 1;
1119 for multi-level pointers we end up in comp_ptr_ttypes. */
1122 int is_ptr
= TREE_CODE (ttr
) == POINTER_TYPE
;
1124 ttl
= TREE_TYPE (ttl
);
1125 ttr
= TREE_TYPE (ttr
);
1129 if (TREE_CODE (ttl
) == UNKNOWN_TYPE
1130 || TREE_CODE (ttr
) == UNKNOWN_TYPE
)
1132 else if (TREE_CODE (ttl
) == VOID_TYPE
1133 && TREE_CODE (ttr
) != FUNCTION_TYPE
1134 && TREE_CODE (ttr
) != METHOD_TYPE
1135 && TREE_CODE (ttr
) != OFFSET_TYPE
)
1137 else if (TREE_CODE (ttr
) == VOID_TYPE
1138 && TREE_CODE (ttl
) != FUNCTION_TYPE
1139 && TREE_CODE (ttl
) != METHOD_TYPE
1140 && TREE_CODE (ttl
) != OFFSET_TYPE
)
1142 else if (TREE_CODE (ttl
) == POINTER_TYPE
1143 || TREE_CODE (ttl
) == ARRAY_TYPE
)
1145 if (comp_ptr_ttypes (ttl
, ttr
))
1147 else if (comp_ptr_ttypes (ttr
, ttl
))
1153 /* Const and volatile mean something different for function types,
1154 so the usual checks are not appropriate. */
1155 if (TREE_CODE (ttl
) == FUNCTION_TYPE
|| TREE_CODE (ttl
) == METHOD_TYPE
)
1156 return comp_target_types (ttl
, ttr
, nptrs
- 1);
1158 return comp_cv_target_types (ttl
, ttr
, nptrs
- 1);
1161 if (TREE_CODE (ttr
) == ARRAY_TYPE
)
1162 return comp_array_types (comp_target_types
, ttl
, ttr
, COMPARE_STRICT
);
1163 else if (TREE_CODE (ttr
) == FUNCTION_TYPE
|| TREE_CODE (ttr
) == METHOD_TYPE
)
1170 if (!same_type_p (TREE_TYPE (ttl
), TREE_TYPE (ttr
)))
1175 switch (comp_target_types (TREE_TYPE (ttl
), TREE_TYPE (ttr
), -1))
1184 argsl
= TYPE_ARG_TYPES (ttl
);
1185 argsr
= TYPE_ARG_TYPES (ttr
);
1187 /* Compare 'this' here, not in comp_target_parms. */
1188 if (TREE_CODE (ttr
) == METHOD_TYPE
)
1190 tree tl
= TYPE_METHOD_BASETYPE (ttl
);
1191 tree tr
= TYPE_METHOD_BASETYPE (ttr
);
1193 if (!same_or_base_type_p (tr
, tl
))
1195 if (same_or_base_type_p (tl
, tr
))
1201 argsl
= TREE_CHAIN (argsl
);
1202 argsr
= TREE_CHAIN (argsr
);
1205 switch (comp_target_parms (argsl
, argsr
))
1213 return saw_contra
? -1 : 1;
1216 else if (TREE_CODE (ttr
) == OFFSET_TYPE
)
1220 /* Contravariance: we can assign a pointer to base member to a pointer
1221 to derived member. Note difference from simple pointer case, where
1222 we can pass a pointer to derived to a pointer to base. */
1223 if (same_or_base_type_p (TYPE_OFFSET_BASETYPE (ttr
),
1224 TYPE_OFFSET_BASETYPE (ttl
)))
1226 else if (same_or_base_type_p (TYPE_OFFSET_BASETYPE (ttl
),
1227 TYPE_OFFSET_BASETYPE (ttr
)))
1237 ttl
= TREE_TYPE (ttl
);
1238 ttr
= TREE_TYPE (ttr
);
1240 if (TREE_CODE (ttl
) == POINTER_TYPE
1241 || TREE_CODE (ttl
) == ARRAY_TYPE
)
1243 if (comp_ptr_ttypes (ttl
, ttr
))
1249 if (comp_cv_target_types (ttl
, ttr
, nptrs
) == 1)
1254 else if (IS_AGGR_TYPE (ttl
))
1258 if (same_or_base_type_p (build_pointer_type (ttl
),
1259 build_pointer_type (ttr
)))
1261 if (same_or_base_type_p (build_pointer_type (ttr
),
1262 build_pointer_type (ttl
)))
1270 /* Returns 1 if TYPE1 is at least as qualified as TYPE2. */
1273 at_least_as_qualified_p (type1
, type2
)
1277 /* All qualifiers for TYPE2 must also appear in TYPE1. */
1278 return ((cp_type_quals (type1
) & cp_type_quals (type2
))
1279 == cp_type_quals (type2
));
1282 /* Returns 1 if TYPE1 is more qualified than TYPE2. */
1285 more_qualified_p (type1
, type2
)
1289 return (cp_type_quals (type1
) != cp_type_quals (type2
)
1290 && at_least_as_qualified_p (type1
, type2
));
1293 /* Returns 1 if TYPE1 is more cv-qualified than TYPE2, -1 if TYPE2 is
1294 more cv-qualified that TYPE1, and 0 otherwise. */
1297 comp_cv_qualification (type1
, type2
)
1301 if (cp_type_quals (type1
) == cp_type_quals (type2
))
1304 if (at_least_as_qualified_p (type1
, type2
))
1307 else if (at_least_as_qualified_p (type2
, type1
))
1313 /* Returns 1 if the cv-qualification signature of TYPE1 is a proper
1314 subset of the cv-qualification signature of TYPE2, and the types
1315 are similar. Returns -1 if the other way 'round, and 0 otherwise. */
1318 comp_cv_qual_signature (type1
, type2
)
1322 if (comp_ptr_ttypes_real (type2
, type1
, -1))
1324 else if (comp_ptr_ttypes_real (type1
, type2
, -1))
1330 /* If two types share a common base type, return that basetype.
1331 If there is not a unique most-derived base type, this function
1332 returns ERROR_MARK_NODE. */
1335 common_base_type (tt1
, tt2
)
1338 tree best
= NULL_TREE
;
1341 /* If one is a baseclass of another, that's good enough. */
1342 if (UNIQUELY_DERIVED_FROM_P (tt1
, tt2
))
1344 if (UNIQUELY_DERIVED_FROM_P (tt2
, tt1
))
1347 /* Otherwise, try to find a unique baseclass of TT1
1348 that is shared by TT2, and follow that down. */
1349 for (i
= CLASSTYPE_N_BASECLASSES (tt1
)-1; i
>= 0; i
--)
1351 tree basetype
= TYPE_BINFO_BASETYPE (tt1
, i
);
1352 tree trial
= common_base_type (basetype
, tt2
);
1355 if (trial
== error_mark_node
)
1357 if (best
== NULL_TREE
)
1359 else if (best
!= trial
)
1360 return error_mark_node
;
1365 for (i
= CLASSTYPE_N_BASECLASSES (tt2
)-1; i
>= 0; i
--)
1367 tree basetype
= TYPE_BINFO_BASETYPE (tt2
, i
);
1368 tree trial
= common_base_type (tt1
, basetype
);
1371 if (trial
== error_mark_node
)
1373 if (best
== NULL_TREE
)
1375 else if (best
!= trial
)
1376 return error_mark_node
;
1382 /* Subroutines of `comptypes'. */
1384 /* Return 1 if two parameter type lists PARMS1 and PARMS2 are
1385 equivalent in the sense that functions with those parameter types
1386 can have equivalent types. The two lists must be equivalent,
1389 C++: See comment above about TYPE1, TYPE2. */
1392 compparms (parms1
, parms2
)
1393 tree parms1
, parms2
;
1395 register tree t1
= parms1
, t2
= parms2
;
1397 /* An unspecified parmlist matches any specified parmlist
1398 whose argument types don't need default promotions. */
1402 if (t1
== 0 && t2
== 0)
1404 /* If one parmlist is shorter than the other,
1405 they fail to match. */
1406 if (t1
== 0 || t2
== 0)
1408 if (!same_type_p (TREE_VALUE (t2
), TREE_VALUE (t1
)))
1411 t1
= TREE_CHAIN (t1
);
1412 t2
= TREE_CHAIN (t2
);
1416 /* This really wants return whether or not parameter type lists
1417 would make their owning functions assignment compatible or not.
1419 The return value is like for comp_target_types.
1421 This should go away, possibly with the exception of the empty parmlist
1422 conversion; there are no conversions between function types in C++.
1423 (jason 17 Apr 1997) */
1426 comp_target_parms (parms1
, parms2
)
1427 tree parms1
, parms2
;
1429 register tree t1
= parms1
, t2
= parms2
;
1430 int warn_contravariance
= 0;
1432 /* In C, an unspecified parmlist matches any specified parmlist
1433 whose argument types don't need default promotions. This is not
1434 true for C++, but let's do it anyway for unfixed headers. */
1436 if (t1
== 0 && t2
!= 0)
1438 pedwarn ("ISO C++ prohibits conversion from `%#T' to `(...)'",
1440 return self_promoting_args_p (t2
);
1443 return self_promoting_args_p (t1
);
1445 for (; t1
|| t2
; t1
= TREE_CHAIN (t1
), t2
= TREE_CHAIN (t2
))
1449 /* If one parmlist is shorter than the other,
1450 they fail to match, unless STRICT is <= 0. */
1451 if (t1
== 0 || t2
== 0)
1453 p1
= TREE_VALUE (t1
);
1454 p2
= TREE_VALUE (t2
);
1455 if (same_type_p (p1
, p2
))
1461 if ((TREE_CODE (p1
) == POINTER_TYPE
&& TREE_CODE (p2
) == POINTER_TYPE
)
1462 || (TREE_CODE (p1
) == REFERENCE_TYPE
1463 && TREE_CODE (p2
) == REFERENCE_TYPE
))
1465 /* The following is wrong for contravariance,
1466 but many programs depend on it. */
1467 if (TREE_TYPE (p1
) == void_type_node
)
1469 if (TREE_TYPE (p2
) == void_type_node
)
1471 warn_contravariance
= 1;
1474 if (IS_AGGR_TYPE (TREE_TYPE (p1
))
1475 && !same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (p1
),
1479 /* Note backwards order due to contravariance. */
1480 if (comp_target_types (p2
, p1
, 1) <= 0)
1482 if (comp_target_types (p1
, p2
, 1) > 0)
1484 warn_contravariance
= 1;
1490 return warn_contravariance
? -1 : 1;
1494 cxx_sizeof_or_alignof_type (type
, op
, complain
)
1499 enum tree_code type_code
;
1501 const char *op_name
;
1503 my_friendly_assert (op
== SIZEOF_EXPR
|| op
== ALIGNOF_EXPR
, 20020720);
1504 if (processing_template_decl
)
1505 return build_min_nt (op
, type
);
1507 op_name
= operator_name_info
[(int) op
].name
;
1509 if (TREE_CODE (type
) == REFERENCE_TYPE
)
1510 type
= TREE_TYPE (type
);
1511 type_code
= TREE_CODE (type
);
1513 if (type_code
== METHOD_TYPE
)
1515 if (complain
&& (pedantic
|| warn_pointer_arith
))
1516 pedwarn ("invalid application of `%s' to a member function", op_name
);
1517 value
= size_one_node
;
1519 else if (type_code
== OFFSET_TYPE
)
1522 error ("invalid application of `%s' to non-static member", op_name
);
1523 value
= size_zero_node
;
1526 value
= c_sizeof_or_alignof_type (complete_type (type
), op
, complain
);
1535 if (processing_template_decl
)
1536 return build_min_nt (SIZEOF_EXPR
, e
);
1538 if (TREE_CODE (e
) == COMPONENT_REF
1539 && DECL_C_BIT_FIELD (TREE_OPERAND (e
, 1)))
1540 error ("sizeof applied to a bit-field");
1541 if (is_overloaded_fn (e
))
1543 pedwarn ("ISO C++ forbids applying `sizeof' to an expression of function type");
1544 return c_sizeof (char_type_node
);
1546 else if (type_unknown_p (e
))
1548 cxx_incomplete_type_error (e
, TREE_TYPE (e
));
1549 return c_sizeof (char_type_node
);
1551 /* It's invalid to say `sizeof (X::i)' for `i' a non-static data
1552 member unless you're in a non-static member of X. So hand off to
1553 resolve_offset_ref. [expr.prim] */
1554 else if (TREE_CODE (e
) == OFFSET_REF
)
1555 e
= resolve_offset_ref (e
);
1557 if (e
== error_mark_node
)
1560 return cxx_sizeof (TREE_TYPE (e
));
1564 /* Perform the array-to-pointer and function-to-pointer conversions
1567 In addition, references are converted to lvalues and manifest
1568 constants are replaced by their values. */
1571 decay_conversion (exp
)
1575 register enum tree_code code
;
1577 if (TREE_CODE (exp
) == OFFSET_REF
)
1578 exp
= resolve_offset_ref (exp
);
1580 type
= TREE_TYPE (exp
);
1581 code
= TREE_CODE (type
);
1583 if (code
== REFERENCE_TYPE
)
1585 exp
= convert_from_reference (exp
);
1586 type
= TREE_TYPE (exp
);
1587 code
= TREE_CODE (type
);
1590 if (type
== error_mark_node
)
1591 return error_mark_node
;
1593 if (type_unknown_p (exp
))
1595 cxx_incomplete_type_error (exp
, TREE_TYPE (exp
));
1596 return error_mark_node
;
1599 /* Constants can be used directly unless they're not loadable. */
1600 if (TREE_CODE (exp
) == CONST_DECL
)
1601 exp
= DECL_INITIAL (exp
);
1602 /* Replace a nonvolatile const static variable with its value. We
1603 don't do this for arrays, though; we want the address of the
1604 first element of the array, not the address of the first element
1605 of its initializing constant. */
1606 else if (code
!= ARRAY_TYPE
)
1608 exp
= decl_constant_value (exp
);
1609 type
= TREE_TYPE (exp
);
1612 /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
1613 Leave such NOP_EXPRs, since RHS is being used in non-lvalue context. */
1615 if (code
== VOID_TYPE
)
1617 error ("void value not ignored as it ought to be");
1618 return error_mark_node
;
1620 if (code
== METHOD_TYPE
)
1622 if (code
== FUNCTION_TYPE
|| is_overloaded_fn (exp
))
1623 return build_unary_op (ADDR_EXPR
, exp
, 0);
1624 if (code
== ARRAY_TYPE
)
1629 if (TREE_CODE (exp
) == INDIRECT_REF
)
1631 /* Stripping away the INDIRECT_REF is not the right
1632 thing to do for references... */
1633 tree inner
= TREE_OPERAND (exp
, 0);
1634 if (TREE_CODE (TREE_TYPE (inner
)) == REFERENCE_TYPE
)
1636 inner
= build1 (CONVERT_EXPR
,
1637 build_pointer_type (TREE_TYPE
1638 (TREE_TYPE (inner
))),
1640 TREE_CONSTANT (inner
) = TREE_CONSTANT (TREE_OPERAND (inner
, 0));
1642 return cp_convert (build_pointer_type (TREE_TYPE (type
)), inner
);
1645 if (TREE_CODE (exp
) == COMPOUND_EXPR
)
1647 tree op1
= decay_conversion (TREE_OPERAND (exp
, 1));
1648 return build (COMPOUND_EXPR
, TREE_TYPE (op1
),
1649 TREE_OPERAND (exp
, 0), op1
);
1653 && ! (TREE_CODE (exp
) == CONSTRUCTOR
&& TREE_STATIC (exp
)))
1655 error ("invalid use of non-lvalue array");
1656 return error_mark_node
;
1659 ptrtype
= build_pointer_type (TREE_TYPE (type
));
1661 if (TREE_CODE (exp
) == VAR_DECL
)
1663 /* ??? This is not really quite correct
1664 in that the type of the operand of ADDR_EXPR
1665 is not the target type of the type of the ADDR_EXPR itself.
1666 Question is, can this lossage be avoided? */
1667 adr
= build1 (ADDR_EXPR
, ptrtype
, exp
);
1668 if (!cxx_mark_addressable (exp
))
1669 return error_mark_node
;
1670 TREE_CONSTANT (adr
) = staticp (exp
);
1671 TREE_SIDE_EFFECTS (adr
) = 0; /* Default would be, same as EXP. */
1674 /* This way is better for a COMPONENT_REF since it can
1675 simplify the offset for a component. */
1676 adr
= build_unary_op (ADDR_EXPR
, exp
, 1);
1677 return cp_convert (ptrtype
, adr
);
1680 /* [basic.lval]: Class rvalues can have cv-qualified types; non-class
1681 rvalues always have cv-unqualified types. */
1682 if (! CLASS_TYPE_P (type
))
1683 exp
= cp_convert (TYPE_MAIN_VARIANT (type
), exp
);
1689 default_conversion (exp
)
1693 enum tree_code code
;
1695 exp
= decay_conversion (exp
);
1697 type
= TREE_TYPE (exp
);
1698 code
= TREE_CODE (type
);
1700 if (INTEGRAL_CODE_P (code
))
1702 tree t
= type_promotes_to (type
);
1704 return cp_convert (t
, exp
);
1710 /* Take the address of an inline function without setting TREE_ADDRESSABLE
1714 inline_conversion (exp
)
1717 if (TREE_CODE (exp
) == FUNCTION_DECL
)
1718 exp
= build1 (ADDR_EXPR
, build_pointer_type (TREE_TYPE (exp
)), exp
);
1723 /* Returns nonzero iff exp is a STRING_CST or the result of applying
1724 decay_conversion to one. */
1727 string_conv_p (totype
, exp
, warn
)
1733 if (! flag_const_strings
|| TREE_CODE (totype
) != POINTER_TYPE
)
1736 t
= TREE_TYPE (totype
);
1737 if (!same_type_p (t
, char_type_node
)
1738 && !same_type_p (t
, wchar_type_node
))
1741 if (TREE_CODE (exp
) == STRING_CST
)
1743 /* Make sure that we don't try to convert between char and wchar_t. */
1744 if (!same_type_p (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (exp
))), t
))
1749 /* Is this a string constant which has decayed to 'const char *'? */
1750 t
= build_pointer_type (build_qualified_type (t
, TYPE_QUAL_CONST
));
1751 if (!same_type_p (TREE_TYPE (exp
), t
))
1754 if (TREE_CODE (exp
) != ADDR_EXPR
1755 || TREE_CODE (TREE_OPERAND (exp
, 0)) != STRING_CST
)
1759 /* This warning is not very useful, as it complains about printf. */
1760 if (warn
&& warn_write_strings
)
1761 warning ("deprecated conversion from string constant to `%T'", totype
);
1766 /* Given a COND_EXPR, MIN_EXPR, or MAX_EXPR in T, return it in a form that we
1767 can, for example, use as an lvalue. This code used to be in
1768 unary_complex_lvalue, but we needed it to deal with `a = (d == c) ? b : c'
1769 expressions, where we're dealing with aggregates. But now it's again only
1770 called from unary_complex_lvalue. The case (in particular) that led to
1771 this was with CODE == ADDR_EXPR, since it's not an lvalue when we'd
1775 rationalize_conditional_expr (code
, t
)
1776 enum tree_code code
;
1779 /* For MIN_EXPR or MAX_EXPR, fold-const.c has arranged things so that
1780 the first operand is always the one to be used if both operands
1781 are equal, so we know what conditional expression this used to be. */
1782 if (TREE_CODE (t
) == MIN_EXPR
|| TREE_CODE (t
) == MAX_EXPR
)
1785 build_conditional_expr (build_x_binary_op ((TREE_CODE (t
) == MIN_EXPR
1786 ? LE_EXPR
: GE_EXPR
),
1787 TREE_OPERAND (t
, 0),
1788 TREE_OPERAND (t
, 1)),
1789 build_unary_op (code
, TREE_OPERAND (t
, 0), 0),
1790 build_unary_op (code
, TREE_OPERAND (t
, 1), 0));
1794 build_conditional_expr (TREE_OPERAND (t
, 0),
1795 build_unary_op (code
, TREE_OPERAND (t
, 1), 0),
1796 build_unary_op (code
, TREE_OPERAND (t
, 2), 0));
1799 /* Given the TYPE of an anonymous union field inside T, return the
1800 FIELD_DECL for the field. If not found return NULL_TREE. Because
1801 anonymous unions can nest, we must also search all anonymous unions
1802 that are directly reachable. */
1805 lookup_anon_field (t
, type
)
1810 for (field
= TYPE_FIELDS (t
); field
; field
= TREE_CHAIN (field
))
1812 if (TREE_STATIC (field
))
1814 if (TREE_CODE (field
) != FIELD_DECL
)
1817 /* If we find it directly, return the field. */
1818 if (DECL_NAME (field
) == NULL_TREE
1819 && type
== TYPE_MAIN_VARIANT (TREE_TYPE (field
)))
1824 /* Otherwise, it could be nested, search harder. */
1825 if (DECL_NAME (field
) == NULL_TREE
1826 && ANON_AGGR_TYPE_P (TREE_TYPE (field
)))
1828 tree subfield
= lookup_anon_field (TREE_TYPE (field
), type
);
1836 /* Build an expression representing OBJECT.MEMBER. OBJECT is an
1837 expression; MEMBER is a DECL or baselink. If ACCESS_PATH is
1838 non-NULL, it indicates the path to the base used to name MEMBER.
1839 If PRESERVE_REFERENCE is true, the expression returned will have
1840 REFERENCE_TYPE if the MEMBER does. Otherwise, the expression
1841 returned will have the type referred to by the reference.
1843 This function does not perform access control; that is either done
1844 earlier by the parser when the name of MEMBER is resolved to MEMBER
1845 itself, or later when overload resolution selects one of the
1846 functions indicated by MEMBER. */
1849 build_class_member_access_expr (tree object
, tree member
,
1850 tree access_path
, bool preserve_reference
)
1854 tree result
= NULL_TREE
;
1856 if (object
== error_mark_node
|| member
== error_mark_node
)
1857 return error_mark_node
;
1859 my_friendly_assert (DECL_P (member
) || BASELINK_P (member
),
1864 The type of the first expression shall be "class object" (of a
1866 object_type
= TREE_TYPE (object
);
1867 if (!complete_type_or_else (object_type
, object
))
1868 return error_mark_node
;
1869 if (!CLASS_TYPE_P (object_type
))
1871 error ("request for member `%D' in `%E', which is of non-class type `%T'",
1872 member
, object
, object_type
);
1873 return error_mark_node
;
1876 /* The standard does not seem to actually say that MEMBER must be a
1877 member of OBJECT_TYPE. However, that is clearly what is
1879 if (DECL_P (member
))
1881 member_scope
= DECL_CLASS_CONTEXT (member
);
1883 if (TREE_DEPRECATED (member
))
1884 warn_deprecated_use (member
);
1887 member_scope
= BINFO_TYPE (BASELINK_BINFO (member
));
1888 /* If MEMBER is from an anonymous aggregate, MEMBER_SCOPE will
1889 presently be the anonymous union. Go outwards until we find a
1890 type related to OBJECT_TYPE. */
1891 while (ANON_AGGR_TYPE_P (member_scope
)
1892 && !same_type_ignoring_top_level_qualifiers_p (member_scope
,
1894 member_scope
= TYPE_CONTEXT (member_scope
);
1895 if (!member_scope
|| !DERIVED_FROM_P (member_scope
, object_type
))
1897 error ("`%D' is not a member of `%T'", member
, object_type
);
1898 return error_mark_node
;
1901 /* In [expr.ref], there is an explicit list of the valid choices for
1902 MEMBER. We check for each of those cases here. */
1903 if (TREE_CODE (member
) == VAR_DECL
)
1905 /* A static data member. */
1907 /* If OBJECT has side-effects, they are supposed to occur. */
1908 if (TREE_SIDE_EFFECTS (object
))
1909 result
= build (COMPOUND_EXPR
, TREE_TYPE (result
), object
, result
);
1911 else if (TREE_CODE (member
) == FIELD_DECL
)
1913 /* A non-static data member. */
1918 null_object_p
= (TREE_CODE (object
) == INDIRECT_REF
1919 && integer_zerop (TREE_OPERAND (object
, 0)));
1921 /* Convert OBJECT to the type of MEMBER. */
1922 if (!same_type_p (object_type
, member_scope
))
1927 binfo
= lookup_base (access_path
? access_path
: object_type
,
1928 member_scope
, ba_ignore
, &kind
);
1929 if (binfo
== error_mark_node
)
1930 return error_mark_node
;
1932 /* It is invalid to use to try to get to a virtual base of a
1933 NULL object. The most common cause is invalid use of
1935 if (null_object_p
&& kind
== bk_via_virtual
)
1937 error ("invalid access to non-static data member `%D' of NULL object",
1939 error ("(perhaps the `offsetof' macro was used incorrectly)");
1940 return error_mark_node
;
1943 /* Convert to the base. */
1944 object
= build_base_path (PLUS_EXPR
, object
, binfo
,
1946 /* If we found the base successfully then we should be able
1947 to convert to it successfully. */
1948 my_friendly_assert (object
!= error_mark_node
,
1952 /* Issue a warning about access a member of a NULL object. */
1953 if (null_object_p
&& CLASSTYPE_NON_POD_P (object_type
))
1955 warning ("invalid access to non-static data member `%D' of NULL object",
1957 warning ("(perhaps the `offsetof' macro was used incorrectly)");
1960 /* If MEMBER is from an anonymous aggregate, we have converted
1961 OBJECT so that it refers to the class containing the
1962 anonymous union. Generate a reference to the anonymous union
1963 itself, and recur to find MEMBER. */
1964 if (ANON_AGGR_TYPE_P (DECL_CONTEXT (member
)))
1966 tree anonymous_union
;
1968 anonymous_union
= lookup_anon_field (TREE_TYPE (object
),
1969 DECL_CONTEXT (member
));
1970 object
= build_class_member_access_expr (object
,
1972 /*access_path=*/NULL_TREE
,
1973 preserve_reference
);
1976 /* Compute the type of the field, as described in [expr.ref]. */
1977 type_quals
= TYPE_UNQUALIFIED
;
1978 member_type
= TREE_TYPE (member
);
1979 if (TREE_CODE (member_type
) != REFERENCE_TYPE
)
1981 type_quals
= (cp_type_quals (member_type
)
1982 | cp_type_quals (object_type
));
1984 /* A field is const (volatile) if the enclosing object, or the
1985 field itself, is const (volatile). But, a mutable field is
1986 not const, even within a const object. */
1987 if (DECL_MUTABLE_P (member
))
1988 type_quals
&= ~TYPE_QUAL_CONST
;
1989 member_type
= cp_build_qualified_type (member_type
, type_quals
);
1992 result
= fold (build (COMPONENT_REF
, member_type
, object
, member
));
1994 /* Mark the expression const or volatile, as appropriate. Even
1995 though we've dealt with the type above, we still have to mark the
1996 expression itself. */
1997 if (type_quals
& TYPE_QUAL_CONST
)
1998 TREE_READONLY (result
) = 1;
1999 else if (type_quals
& TYPE_QUAL_VOLATILE
)
2000 TREE_THIS_VOLATILE (result
) = 1;
2002 else if (BASELINK_P (member
))
2004 /* The member is a (possibly overloaded) member function. */
2007 /* If the MEMBER is exactly one static member function, then we
2008 know the type of the expression. Otherwise, we must wait
2009 until overload resolution has been performed. */
2010 functions
= BASELINK_FUNCTIONS (member
);
2011 if (TREE_CODE (functions
) == FUNCTION_DECL
2012 && DECL_STATIC_FUNCTION_P (functions
))
2014 /* A static member function. */
2017 /* If OBJECT has side-effects, they are supposed to occur. */
2018 if (TREE_SIDE_EFFECTS (object
))
2019 result
= build (COMPOUND_EXPR
, TREE_TYPE (result
),
2023 /* Note that we do not convert OBJECT to the BASELINK_BINFO
2024 base. That will happen when the function is called. */
2025 result
= build (COMPONENT_REF
, unknown_type_node
, object
, member
);
2027 else if (TREE_CODE (member
) == CONST_DECL
)
2029 /* The member is an enumerator. */
2031 /* If OBJECT has side-effects, they are supposed to occur. */
2032 if (TREE_SIDE_EFFECTS (object
))
2033 result
= build (COMPOUND_EXPR
, TREE_TYPE (result
),
2038 error ("invalid use of `%D'", member
);
2039 return error_mark_node
;
2042 if (!preserve_reference
)
2045 If E2 is declared to have type "reference to T", then ... the
2046 type of E1.E2 is T. */
2047 result
= convert_from_reference (result
);
2052 /* This function is called by the parser to process a class member
2053 access expression of the form OBJECT.NAME. NAME is a node used by
2054 the parser to represent a name; it is not yet a DECL. It may,
2055 however, be a BASELINK where the BASELINK_FUNCTIONS is a
2056 TEMPLATE_ID_EXPR. Templates must be looked up by the parser, and
2057 there is no reason to do the lookup twice, so the parser keeps the
2061 finish_class_member_access_expr (tree object
, tree name
)
2065 tree access_path
= NULL_TREE
;
2067 if (object
== error_mark_node
|| name
== error_mark_node
)
2068 return error_mark_node
;
2070 if (processing_template_decl
)
2071 return build_min_nt (COMPONENT_REF
, object
, name
);
2073 if (TREE_CODE (object
) == OFFSET_REF
)
2074 object
= resolve_offset_ref (object
);
2076 object_type
= TREE_TYPE (object
);
2077 if (TREE_CODE (object_type
) == REFERENCE_TYPE
)
2079 object
= convert_from_reference (object
);
2080 object_type
= TREE_TYPE (object
);
2085 The type of the first expression shall be "class object" (of a
2087 if (!complete_type_or_else (object_type
, object
))
2088 return error_mark_node
;
2089 if (!CLASS_TYPE_P (object_type
))
2091 error ("request for member `%D' in `%E', which is of non-class type `%T'",
2092 name
, object
, object_type
);
2093 return error_mark_node
;
2096 if (BASELINK_P (name
))
2098 /* A member function that has already been looked up. */
2099 my_friendly_assert ((TREE_CODE (BASELINK_FUNCTIONS (name
))
2100 == TEMPLATE_ID_EXPR
),
2106 bool is_template_id
= false;
2107 tree template_args
= NULL_TREE
;
2109 if (TREE_CODE (name
) == TEMPLATE_ID_EXPR
)
2111 is_template_id
= true;
2112 template_args
= TREE_OPERAND (name
, 1);
2113 name
= TREE_OPERAND (name
, 0);
2116 if (TREE_CODE (name
) == SCOPE_REF
)
2120 /* A qualified name. The qualifying class or namespace `S' has
2121 already been looked up; it is either a TYPE or a
2122 NAMESPACE_DECL. The member name is either an IDENTIFIER_NODE
2123 or a BIT_NOT_EXPR. */
2124 scope
= TREE_OPERAND (name
, 0);
2125 name
= TREE_OPERAND (name
, 1);
2126 my_friendly_assert ((CLASS_TYPE_P (scope
)
2127 || TREE_CODE (scope
) == NAMESPACE_DECL
),
2129 my_friendly_assert ((TREE_CODE (name
) == IDENTIFIER_NODE
2130 || TREE_CODE (name
) == BIT_NOT_EXPR
),
2133 /* If SCOPE is a namespace, then the qualified name does not
2134 name a member of OBJECT_TYPE. */
2135 if (TREE_CODE (scope
) == NAMESPACE_DECL
)
2137 error ("`%D::%D' is not a member of `%T'",
2138 scope
, member
, object_type
);
2139 return error_mark_node
;
2142 /* Find the base of OBJECT_TYPE corresponding to SCOPE. */
2143 access_path
= lookup_base (object_type
, scope
, ba_check
, NULL
);
2144 if (!access_path
|| access_path
== error_mark_node
)
2145 return error_mark_node
;
2147 /* Look up the member. */
2148 member
= lookup_member (access_path
, name
, /*protect=*/1,
2150 if (member
== NULL_TREE
)
2152 error ("'%D' has no member named '%E'", object_type
, name
);
2153 return error_mark_node
;
2155 else if (member
== error_mark_node
)
2156 return error_mark_node
;
2158 else if (TREE_CODE (name
) == BIT_NOT_EXPR
)
2161 if (TYPE_IDENTIFIER (object_type
) != TREE_OPERAND (name
, 0))
2163 error ("destructor specifier `%T::~%T' must have matching names",
2164 object_type
, TREE_OPERAND (name
, 0));
2165 return error_mark_node
;
2167 if (! TYPE_HAS_DESTRUCTOR (object_type
))
2169 error ("type `%T' has no destructor", object_type
);
2170 return error_mark_node
;
2172 member
= CLASSTYPE_DESTRUCTORS (object_type
);
2174 else if (TREE_CODE (name
) == IDENTIFIER_NODE
)
2176 /* An unqualified name. */
2177 member
= lookup_member (object_type
, name
, /*protect=*/1,
2179 if (member
== NULL_TREE
)
2181 error ("'%D' has no member named '%E'", object_type
, name
);
2182 return error_mark_node
;
2184 else if (member
== error_mark_node
)
2185 return error_mark_node
;
2189 /* The YACC parser sometimes gives us things that are not names.
2190 These always indicate errors. The recursive-descent parser
2191 does not do this, so this code can go away once that parser
2192 replaces the YACC parser. */
2193 error ("invalid use of `%D'", name
);
2194 return error_mark_node
;
2199 tree
template = member
;
2201 if (BASELINK_P (template))
2202 BASELINK_FUNCTIONS (template)
2203 = build_nt (TEMPLATE_ID_EXPR
,
2204 BASELINK_FUNCTIONS (template),
2208 error ("`%D' is not a member template function", name
);
2209 return error_mark_node
;
2214 return build_class_member_access_expr (object
, member
, access_path
,
2215 /*preserve_reference=*/false);
2218 /* Return an expression for the MEMBER_NAME field in the internal
2219 representation of PTRMEM, a pointer-to-member function. (Each
2220 pointer-to-member function type gets its own RECORD_TYPE so it is
2221 more convenient to access the fields by name than by FIELD_DECL.)
2222 This routine converts the NAME to a FIELD_DECL and then creates the
2223 node for the complete expression. */
2226 build_ptrmemfunc_access_expr (tree ptrmem
, tree member_name
)
2232 /* This code is a stripped down version of
2233 build_class_member_access_expr. It does not work to use that
2234 routine directly because it expects the object to be of class
2236 ptrmem_type
= TREE_TYPE (ptrmem
);
2237 my_friendly_assert (TYPE_PTRMEMFUNC_P (ptrmem_type
), 20020804);
2238 member
= lookup_member (ptrmem_type
, member_name
, /*protect=*/0,
2240 member_type
= cp_build_qualified_type (TREE_TYPE (member
),
2241 cp_type_quals (ptrmem_type
));
2242 return fold (build (COMPONENT_REF
, member_type
, ptrmem
, member
));
2245 /* Given an expression PTR for a pointer, return an expression
2246 for the value pointed to.
2247 ERRORSTRING is the name of the operator to appear in error messages.
2249 This function may need to overload OPERATOR_FNNAME.
2250 Must also handle REFERENCE_TYPEs for C++. */
2253 build_x_indirect_ref (ptr
, errorstring
)
2255 const char *errorstring
;
2259 if (processing_template_decl
)
2260 return build_min_nt (INDIRECT_REF
, ptr
);
2262 rval
= build_opfncall (INDIRECT_REF
, LOOKUP_NORMAL
, ptr
, NULL_TREE
,
2266 return build_indirect_ref (ptr
, errorstring
);
2270 build_indirect_ref (ptr
, errorstring
)
2272 const char *errorstring
;
2274 register tree pointer
, type
;
2276 if (ptr
== error_mark_node
)
2277 return error_mark_node
;
2279 if (ptr
== current_class_ptr
)
2280 return current_class_ref
;
2282 pointer
= (TREE_CODE (TREE_TYPE (ptr
)) == REFERENCE_TYPE
2283 ? ptr
: default_conversion (ptr
));
2284 type
= TREE_TYPE (pointer
);
2286 if (TYPE_PTR_P (type
) || TREE_CODE (type
) == REFERENCE_TYPE
)
2290 If the type of the expression is "pointer to T," the type
2291 of the result is "T."
2293 We must use the canonical variant because certain parts of
2294 the back end, like fold, do pointer comparisons between
2296 tree t
= canonical_type_variant (TREE_TYPE (type
));
2298 if (VOID_TYPE_P (t
))
2300 /* A pointer to incomplete type (other than cv void) can be
2301 dereferenced [expr.unary.op]/1 */
2302 error ("`%T' is not a pointer-to-object type", type
);
2303 return error_mark_node
;
2305 else if (TREE_CODE (pointer
) == ADDR_EXPR
2307 && same_type_p (t
, TREE_TYPE (TREE_OPERAND (pointer
, 0))))
2308 /* The POINTER was something like `&x'. We simplify `*&x' to
2310 return TREE_OPERAND (pointer
, 0);
2313 tree ref
= build1 (INDIRECT_REF
, t
, pointer
);
2315 /* We *must* set TREE_READONLY when dereferencing a pointer to const,
2316 so that we get the proper error message if the result is used
2317 to assign to. Also, &* is supposed to be a no-op. */
2318 TREE_READONLY (ref
) = CP_TYPE_CONST_P (t
);
2319 TREE_THIS_VOLATILE (ref
) = CP_TYPE_VOLATILE_P (t
);
2320 TREE_SIDE_EFFECTS (ref
)
2321 = (TREE_THIS_VOLATILE (ref
) || TREE_SIDE_EFFECTS (pointer
)
2326 /* `pointer' won't be an error_mark_node if we were given a
2327 pointer to member, so it's cool to check for this here. */
2328 else if (TYPE_PTRMEM_P (type
) || TYPE_PTRMEMFUNC_P (type
))
2329 error ("invalid use of `%s' on pointer to member", errorstring
);
2330 else if (pointer
!= error_mark_node
)
2333 error ("invalid type argument of `%s'", errorstring
);
2335 error ("invalid type argument");
2337 return error_mark_node
;
2340 /* This handles expressions of the form "a[i]", which denotes
2343 This is logically equivalent in C to *(a+i), but we may do it differently.
2344 If A is a variable or a member, we generate a primitive ARRAY_REF.
2345 This avoids forcing the array out of registers, and can work on
2346 arrays that are not lvalues (for example, members of structures returned
2349 If INDEX is of some user-defined type, it must be converted to
2350 integer type. Otherwise, to make a compatible PLUS_EXPR, it
2351 will inherit the type of the array, which will be some pointer type. */
2354 build_array_ref (array
, idx
)
2359 error ("subscript missing in array reference");
2360 return error_mark_node
;
2363 if (TREE_TYPE (array
) == error_mark_node
2364 || TREE_TYPE (idx
) == error_mark_node
)
2365 return error_mark_node
;
2367 /* If ARRAY is a COMPOUND_EXPR or COND_EXPR, move our reference
2369 switch (TREE_CODE (array
))
2373 tree value
= build_array_ref (TREE_OPERAND (array
, 1), idx
);
2374 return build (COMPOUND_EXPR
, TREE_TYPE (value
),
2375 TREE_OPERAND (array
, 0), value
);
2379 return build_conditional_expr
2380 (TREE_OPERAND (array
, 0),
2381 build_array_ref (TREE_OPERAND (array
, 1), idx
),
2382 build_array_ref (TREE_OPERAND (array
, 2), idx
));
2388 if (TREE_CODE (TREE_TYPE (array
)) == ARRAY_TYPE
2389 && TREE_CODE (array
) != INDIRECT_REF
)
2393 /* Subscripting with type char is likely to lose
2394 on a machine where chars are signed.
2395 So warn on any machine, but optionally.
2396 Don't warn for unsigned char since that type is safe.
2397 Don't warn for signed char because anyone who uses that
2398 must have done so deliberately. */
2399 if (warn_char_subscripts
2400 && TYPE_MAIN_VARIANT (TREE_TYPE (idx
)) == char_type_node
)
2401 warning ("array subscript has type `char'");
2403 /* Apply default promotions *after* noticing character types. */
2404 idx
= default_conversion (idx
);
2406 if (TREE_CODE (TREE_TYPE (idx
)) != INTEGER_TYPE
)
2408 error ("array subscript is not an integer");
2409 return error_mark_node
;
2412 /* An array that is indexed by a non-constant
2413 cannot be stored in a register; we must be able to do
2414 address arithmetic on its address.
2415 Likewise an array of elements of variable size. */
2416 if (TREE_CODE (idx
) != INTEGER_CST
2417 || (COMPLETE_TYPE_P (TREE_TYPE (TREE_TYPE (array
)))
2418 && (TREE_CODE (TYPE_SIZE (TREE_TYPE (TREE_TYPE (array
))))
2421 if (!cxx_mark_addressable (array
))
2422 return error_mark_node
;
2425 /* An array that is indexed by a constant value which is not within
2426 the array bounds cannot be stored in a register either; because we
2427 would get a crash in store_bit_field/extract_bit_field when trying
2428 to access a non-existent part of the register. */
2429 if (TREE_CODE (idx
) == INTEGER_CST
2430 && TYPE_VALUES (TREE_TYPE (array
))
2431 && ! int_fits_type_p (idx
, TYPE_VALUES (TREE_TYPE (array
))))
2433 if (!cxx_mark_addressable (array
))
2434 return error_mark_node
;
2437 if (pedantic
&& !lvalue_p (array
))
2438 pedwarn ("ISO C++ forbids subscripting non-lvalue array");
2440 /* Note in C++ it is valid to subscript a `register' array, since
2441 it is valid to take the address of something with that
2442 storage specification. */
2446 while (TREE_CODE (foo
) == COMPONENT_REF
)
2447 foo
= TREE_OPERAND (foo
, 0);
2448 if (TREE_CODE (foo
) == VAR_DECL
&& DECL_REGISTER (foo
))
2449 warning ("subscripting array declared `register'");
2452 type
= TREE_TYPE (TREE_TYPE (array
));
2453 rval
= build (ARRAY_REF
, type
, array
, idx
);
2454 /* Array ref is const/volatile if the array elements are
2455 or if the array is.. */
2456 TREE_READONLY (rval
)
2457 |= (CP_TYPE_CONST_P (type
) | TREE_READONLY (array
));
2458 TREE_SIDE_EFFECTS (rval
)
2459 |= (CP_TYPE_VOLATILE_P (type
) | TREE_SIDE_EFFECTS (array
));
2460 TREE_THIS_VOLATILE (rval
)
2461 |= (CP_TYPE_VOLATILE_P (type
) | TREE_THIS_VOLATILE (array
));
2462 return require_complete_type (fold (rval
));
2466 tree ar
= default_conversion (array
);
2467 tree ind
= default_conversion (idx
);
2469 /* Put the integer in IND to simplify error checking. */
2470 if (TREE_CODE (TREE_TYPE (ar
)) == INTEGER_TYPE
)
2477 if (ar
== error_mark_node
)
2480 if (TREE_CODE (TREE_TYPE (ar
)) != POINTER_TYPE
)
2482 error ("subscripted value is neither array nor pointer");
2483 return error_mark_node
;
2485 if (TREE_CODE (TREE_TYPE (ind
)) != INTEGER_TYPE
)
2487 error ("array subscript is not an integer");
2488 return error_mark_node
;
2491 return build_indirect_ref (cp_build_binary_op (PLUS_EXPR
, ar
, ind
),
2496 /* Resolve a pointer to member function. INSTANCE is the object
2497 instance to use, if the member points to a virtual member.
2499 This used to avoid checking for virtual functions if basetype
2500 has no virtual functions, according to an earlier ANSI draft.
2501 With the final ISO C++ rules, such an optimization is
2502 incorrect: A pointer to a derived member can be static_cast
2503 to pointer-to-base-member, as long as the dynamic object
2504 later has the right member. */
2507 get_member_function_from_ptrfunc (instance_ptrptr
, function
)
2508 tree
*instance_ptrptr
;
2511 if (TREE_CODE (function
) == OFFSET_REF
)
2512 function
= TREE_OPERAND (function
, 1);
2514 if (TYPE_PTRMEMFUNC_P (TREE_TYPE (function
)))
2516 tree idx
, delta
, e1
, e2
, e3
, vtbl
, basetype
;
2517 tree fntype
= TYPE_PTRMEMFUNC_FN_TYPE (TREE_TYPE (function
));
2519 tree instance_ptr
= *instance_ptrptr
;
2520 if (instance_ptr
== error_mark_node
)
2522 if (TREE_CODE (function
) == PTRMEM_CST
)
2524 /* Extracting the function address from a pmf is only
2525 allowed with -Wno-pmf-conversions. It only works for
2527 e1
= build_addr_func (PTRMEM_CST_MEMBER (function
));
2528 e1
= convert (fntype
, e1
);
2533 error ("object missing in use of `%E'", function
);
2534 return error_mark_node
;
2538 if (TREE_SIDE_EFFECTS (instance_ptr
))
2539 instance_ptr
= save_expr (instance_ptr
);
2541 if (TREE_SIDE_EFFECTS (function
))
2542 function
= save_expr (function
);
2544 /* Start by extracting all the information from the PMF itself. */
2545 e3
= PFN_FROM_PTRMEMFUNC (function
);
2546 delta
= build_ptrmemfunc_access_expr (function
, delta_identifier
);
2547 idx
= build1 (NOP_EXPR
, vtable_index_type
, e3
);
2548 switch (TARGET_PTRMEMFUNC_VBIT_LOCATION
)
2550 case ptrmemfunc_vbit_in_pfn
:
2551 e1
= cp_build_binary_op (BIT_AND_EXPR
, idx
, integer_one_node
);
2552 idx
= cp_build_binary_op (MINUS_EXPR
, idx
, integer_one_node
);
2555 case ptrmemfunc_vbit_in_delta
:
2556 e1
= cp_build_binary_op (BIT_AND_EXPR
, delta
, integer_one_node
);
2557 delta
= cp_build_binary_op (RSHIFT_EXPR
, delta
, integer_one_node
);
2564 /* Convert down to the right base before using the instance. First
2566 basetype
= TYPE_METHOD_BASETYPE (TREE_TYPE (fntype
));
2567 basetype
= lookup_base (TREE_TYPE (TREE_TYPE (instance_ptr
)),
2568 basetype
, ba_check
, NULL
);
2569 instance_ptr
= build_base_path (PLUS_EXPR
, instance_ptr
, basetype
, 1);
2570 if (instance_ptr
== error_mark_node
)
2571 return error_mark_node
;
2572 /* ...and then the delta in the PMF. */
2573 instance_ptr
= build (PLUS_EXPR
, TREE_TYPE (instance_ptr
),
2574 instance_ptr
, delta
);
2576 /* Hand back the adjusted 'this' argument to our caller. */
2577 *instance_ptrptr
= instance_ptr
;
2579 /* Next extract the vtable pointer from the object. */
2580 vtbl
= build1 (NOP_EXPR
, build_pointer_type (vtbl_ptr_type_node
),
2582 vtbl
= build_indirect_ref (vtbl
, NULL
);
2584 /* Finally, extract the function pointer from the vtable. */
2585 e2
= fold (build (PLUS_EXPR
, TREE_TYPE (vtbl
), vtbl
, idx
));
2586 e2
= build_indirect_ref (e2
, NULL
);
2587 TREE_CONSTANT (e2
) = 1;
2589 /* When using function descriptors, the address of the
2590 vtable entry is treated as a function pointer. */
2591 if (TARGET_VTABLE_USES_DESCRIPTORS
)
2592 e2
= build1 (NOP_EXPR
, TREE_TYPE (e2
),
2593 build_unary_op (ADDR_EXPR
, e2
, /*noconvert=*/1));
2595 TREE_TYPE (e2
) = TREE_TYPE (e3
);
2596 e1
= build_conditional_expr (e1
, e2
, e3
);
2598 /* Make sure this doesn't get evaluated first inside one of the
2599 branches of the COND_EXPR. */
2600 if (TREE_CODE (instance_ptr
) == SAVE_EXPR
)
2601 e1
= build (COMPOUND_EXPR
, TREE_TYPE (e1
),
2610 build_function_call_real (function
, params
, require_complete
, flags
)
2611 tree function
, params
;
2612 int require_complete
, flags
;
2614 register tree fntype
, fndecl
;
2615 register tree value_type
;
2616 register tree coerced_params
;
2618 tree name
= NULL_TREE
, assembler_name
= NULL_TREE
;
2620 tree original
= function
;
2622 /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
2623 Strip such NOP_EXPRs, since FUNCTION is used in non-lvalue context. */
2624 if (TREE_CODE (function
) == NOP_EXPR
2625 && TREE_TYPE (function
) == TREE_TYPE (TREE_OPERAND (function
, 0)))
2626 function
= TREE_OPERAND (function
, 0);
2628 if (TREE_CODE (function
) == FUNCTION_DECL
)
2630 name
= DECL_NAME (function
);
2631 assembler_name
= DECL_ASSEMBLER_NAME (function
);
2633 mark_used (function
);
2636 /* Convert anything with function type to a pointer-to-function. */
2637 if (pedantic
&& DECL_MAIN_P (function
))
2638 pedwarn ("ISO C++ forbids calling `::main' from within program");
2640 /* Differs from default_conversion by not setting TREE_ADDRESSABLE
2641 (because calling an inline function does not mean the function
2642 needs to be separately compiled). */
2644 if (DECL_INLINE (function
))
2645 function
= inline_conversion (function
);
2647 function
= build_addr_func (function
);
2653 function
= build_addr_func (function
);
2656 if (function
== error_mark_node
)
2657 return error_mark_node
;
2659 fntype
= TREE_TYPE (function
);
2661 if (TYPE_PTRMEMFUNC_P (fntype
))
2663 error ("must use .* or ->* to call pointer-to-member function in `%E (...)'",
2665 return error_mark_node
;
2668 is_method
= (TREE_CODE (fntype
) == POINTER_TYPE
2669 && TREE_CODE (TREE_TYPE (fntype
)) == METHOD_TYPE
);
2671 if (!((TREE_CODE (fntype
) == POINTER_TYPE
2672 && TREE_CODE (TREE_TYPE (fntype
)) == FUNCTION_TYPE
)
2674 || TREE_CODE (function
) == TEMPLATE_ID_EXPR
))
2676 error ("`%E' cannot be used as a function", original
);
2677 return error_mark_node
;
2680 /* fntype now gets the type of function pointed to. */
2681 fntype
= TREE_TYPE (fntype
);
2683 /* Convert the parameters to the types declared in the
2684 function prototype, or apply default promotions. */
2686 if (flags
& LOOKUP_COMPLAIN
)
2687 coerced_params
= convert_arguments (TYPE_ARG_TYPES (fntype
),
2688 params
, fndecl
, LOOKUP_NORMAL
);
2690 coerced_params
= convert_arguments (TYPE_ARG_TYPES (fntype
),
2693 if (coerced_params
== error_mark_node
)
2695 if (flags
& LOOKUP_SPECULATIVELY
)
2698 return error_mark_node
;
2701 /* Check for errors in format strings. */
2704 check_function_format (NULL
, TYPE_ATTRIBUTES (fntype
), coerced_params
);
2706 /* Recognize certain built-in functions so we can make tree-codes
2707 other than CALL_EXPR. We do this when it enables fold-const.c
2708 to do something useful. */
2710 if (TREE_CODE (function
) == ADDR_EXPR
2711 && TREE_CODE (TREE_OPERAND (function
, 0)) == FUNCTION_DECL
2712 && DECL_BUILT_IN (TREE_OPERAND (function
, 0)))
2714 result
= expand_tree_builtin (TREE_OPERAND (function
, 0),
2715 params
, coerced_params
);
2720 /* Some built-in function calls will be evaluated at
2721 compile-time in fold (). */
2722 result
= fold (build_call (function
, coerced_params
));
2723 value_type
= TREE_TYPE (result
);
2725 if (require_complete
)
2727 if (TREE_CODE (value_type
) == VOID_TYPE
)
2729 result
= require_complete_type (result
);
2731 if (IS_AGGR_TYPE (value_type
))
2732 result
= build_cplus_new (value_type
, result
);
2733 return convert_from_reference (result
);
2737 build_function_call (function
, params
)
2738 tree function
, params
;
2740 return build_function_call_real (function
, params
, 1, LOOKUP_NORMAL
);
2743 /* Convert the actual parameter expressions in the list VALUES
2744 to the types in the list TYPELIST.
2745 If parmdecls is exhausted, or when an element has NULL as its type,
2746 perform the default conversions.
2748 NAME is an IDENTIFIER_NODE or 0. It is used only for error messages.
2750 This is also where warnings about wrong number of args are generated.
2752 Return a list of expressions for the parameters as converted.
2754 Both VALUES and the returned value are chains of TREE_LIST nodes
2755 with the elements of the list in the TREE_VALUE slots of those nodes.
2757 In C++, unspecified trailing parameters can be filled in with their
2758 default arguments, if such were specified. Do so here. */
2761 convert_arguments (typelist
, values
, fndecl
, flags
)
2762 tree typelist
, values
, fndecl
;
2765 register tree typetail
, valtail
;
2766 register tree result
= NULL_TREE
;
2767 const char *called_thing
= 0;
2770 /* Argument passing is always copy-initialization. */
2771 flags
|= LOOKUP_ONLYCONVERTING
;
2775 if (TREE_CODE (TREE_TYPE (fndecl
)) == METHOD_TYPE
)
2777 if (DECL_NAME (fndecl
) == NULL_TREE
2778 || IDENTIFIER_HAS_TYPE_VALUE (DECL_NAME (fndecl
)))
2779 called_thing
= "constructor";
2781 called_thing
= "member function";
2784 called_thing
= "function";
2787 for (valtail
= values
, typetail
= typelist
;
2789 valtail
= TREE_CHAIN (valtail
), i
++)
2791 register tree type
= typetail
? TREE_VALUE (typetail
) : 0;
2792 register tree val
= TREE_VALUE (valtail
);
2794 if (val
== error_mark_node
)
2795 return error_mark_node
;
2797 if (type
== void_type_node
)
2801 cp_error_at ("too many arguments to %s `%+#D'", called_thing
,
2803 error ("at this point in file");
2806 error ("too many arguments to function");
2807 /* In case anybody wants to know if this argument
2810 TREE_TYPE (tree_last (result
)) = error_mark_node
;
2814 if (TREE_CODE (val
) == OFFSET_REF
)
2815 val
= resolve_offset_ref (val
);
2817 /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
2818 Strip such NOP_EXPRs, since VAL is used in non-lvalue context. */
2819 if (TREE_CODE (val
) == NOP_EXPR
2820 && TREE_TYPE (val
) == TREE_TYPE (TREE_OPERAND (val
, 0))
2821 && (type
== 0 || TREE_CODE (type
) != REFERENCE_TYPE
))
2822 val
= TREE_OPERAND (val
, 0);
2824 if (type
== 0 || TREE_CODE (type
) != REFERENCE_TYPE
)
2826 if (TREE_CODE (TREE_TYPE (val
)) == ARRAY_TYPE
2827 || TREE_CODE (TREE_TYPE (val
)) == FUNCTION_TYPE
2828 || TREE_CODE (TREE_TYPE (val
)) == METHOD_TYPE
)
2829 val
= default_conversion (val
);
2832 if (val
== error_mark_node
)
2833 return error_mark_node
;
2837 /* Formal parm type is specified by a function prototype. */
2840 if (!COMPLETE_TYPE_P (complete_type (type
)))
2842 error ("parameter type of called function is incomplete");
2847 parmval
= convert_for_initialization
2848 (NULL_TREE
, type
, val
, flags
,
2849 "argument passing", fndecl
, i
);
2850 parmval
= convert_for_arg_passing (type
, parmval
);
2853 if (parmval
== error_mark_node
)
2854 return error_mark_node
;
2856 result
= tree_cons (NULL_TREE
, parmval
, result
);
2860 if (TREE_CODE (TREE_TYPE (val
)) == REFERENCE_TYPE
)
2861 val
= convert_from_reference (val
);
2863 if (fndecl
&& DECL_BUILT_IN (fndecl
)
2864 && DECL_FUNCTION_CODE (fndecl
) == BUILT_IN_CONSTANT_P
)
2865 /* Don't do ellipsis conversion for __built_in_constant_p
2866 as this will result in spurious warnings for non-POD
2868 val
= require_complete_type (val
);
2870 val
= convert_arg_to_ellipsis (val
);
2872 result
= tree_cons (NULL_TREE
, val
, result
);
2876 typetail
= TREE_CHAIN (typetail
);
2879 if (typetail
!= 0 && typetail
!= void_list_node
)
2881 /* See if there are default arguments that can be used */
2882 if (TREE_PURPOSE (typetail
))
2884 for (; typetail
!= void_list_node
; ++i
)
2887 = convert_default_arg (TREE_VALUE (typetail
),
2888 TREE_PURPOSE (typetail
),
2891 if (parmval
== error_mark_node
)
2892 return error_mark_node
;
2894 result
= tree_cons (0, parmval
, result
);
2895 typetail
= TREE_CHAIN (typetail
);
2896 /* ends with `...'. */
2897 if (typetail
== NULL_TREE
)
2905 cp_error_at ("too few arguments to %s `%+#D'",
2906 called_thing
, fndecl
);
2907 error ("at this point in file");
2910 error ("too few arguments to function");
2911 return error_mark_list
;
2915 return nreverse (result
);
2918 /* Build a binary-operation expression, after performing default
2919 conversions on the operands. CODE is the kind of expression to build. */
2922 build_x_binary_op (code
, arg1
, arg2
)
2923 enum tree_code code
;
2926 if (processing_template_decl
)
2927 return build_min_nt (code
, arg1
, arg2
);
2929 return build_new_op (code
, LOOKUP_NORMAL
, arg1
, arg2
, NULL_TREE
);
2932 /* Build a binary-operation expression without default conversions.
2933 CODE is the kind of expression to build.
2934 This function differs from `build' in several ways:
2935 the data type of the result is computed and recorded in it,
2936 warnings are generated if arg data types are invalid,
2937 special handling for addition and subtraction of pointers is known,
2938 and some optimization is done (operations on narrow ints
2939 are done in the narrower type when that gives the same result).
2940 Constant folding is also done before the result is returned.
2942 Note that the operands will never have enumeral types
2943 because either they have just had the default conversions performed
2944 or they have both just been converted to some other type in which
2945 the arithmetic is to be done.
2947 C++: must do special pointer arithmetic when implementing
2948 multiple inheritance, and deal with pointer to member functions. */
2951 build_binary_op (code
, orig_op0
, orig_op1
, convert_p
)
2952 enum tree_code code
;
2953 tree orig_op0
, orig_op1
;
2954 int convert_p ATTRIBUTE_UNUSED
;
2957 register enum tree_code code0
, code1
;
2960 /* Expression code to give to the expression when it is built.
2961 Normally this is CODE, which is what the caller asked for,
2962 but in some special cases we change it. */
2963 register enum tree_code resultcode
= code
;
2965 /* Data type in which the computation is to be performed.
2966 In the simplest cases this is the common type of the arguments. */
2967 register tree result_type
= NULL
;
2969 /* Nonzero means operands have already been type-converted
2970 in whatever way is necessary.
2971 Zero means they need to be converted to RESULT_TYPE. */
2974 /* Nonzero means create the expression with this type, rather than
2976 tree build_type
= 0;
2978 /* Nonzero means after finally constructing the expression
2979 convert it to this type. */
2980 tree final_type
= 0;
2982 /* Nonzero if this is an operation like MIN or MAX which can
2983 safely be computed in short if both args are promoted shorts.
2984 Also implies COMMON.
2985 -1 indicates a bitwise operation; this makes a difference
2986 in the exact conditions for when it is safe to do the operation
2987 in a narrower mode. */
2990 /* Nonzero if this is a comparison operation;
2991 if both args are promoted shorts, compare the original shorts.
2992 Also implies COMMON. */
2993 int short_compare
= 0;
2995 /* Nonzero if this is a right-shift operation, which can be computed on the
2996 original short and then promoted if the operand is a promoted short. */
2997 int short_shift
= 0;
2999 /* Nonzero means set RESULT_TYPE to the common type of the args. */
3002 /* Apply default conversions. */
3006 if (code
== TRUTH_AND_EXPR
|| code
== TRUTH_ANDIF_EXPR
3007 || code
== TRUTH_OR_EXPR
|| code
== TRUTH_ORIF_EXPR
3008 || code
== TRUTH_XOR_EXPR
)
3010 if (!really_overloaded_fn (op0
))
3011 op0
= decay_conversion (op0
);
3012 if (!really_overloaded_fn (op1
))
3013 op1
= decay_conversion (op1
);
3017 if (!really_overloaded_fn (op0
))
3018 op0
= default_conversion (op0
);
3019 if (!really_overloaded_fn (op1
))
3020 op1
= default_conversion (op1
);
3023 /* Strip NON_LVALUE_EXPRs, etc., since we aren't using as an lvalue. */
3024 STRIP_TYPE_NOPS (op0
);
3025 STRIP_TYPE_NOPS (op1
);
3027 /* DTRT if one side is an overloaded function, but complain about it. */
3028 if (type_unknown_p (op0
))
3030 tree t
= instantiate_type (TREE_TYPE (op1
), op0
, tf_none
);
3031 if (t
!= error_mark_node
)
3033 pedwarn ("assuming cast to type `%T' from overloaded function",
3038 if (type_unknown_p (op1
))
3040 tree t
= instantiate_type (TREE_TYPE (op0
), op1
, tf_none
);
3041 if (t
!= error_mark_node
)
3043 pedwarn ("assuming cast to type `%T' from overloaded function",
3049 type0
= TREE_TYPE (op0
);
3050 type1
= TREE_TYPE (op1
);
3052 /* The expression codes of the data types of the arguments tell us
3053 whether the arguments are integers, floating, pointers, etc. */
3054 code0
= TREE_CODE (type0
);
3055 code1
= TREE_CODE (type1
);
3057 /* If an error was already reported for one of the arguments,
3058 avoid reporting another error. */
3060 if (code0
== ERROR_MARK
|| code1
== ERROR_MARK
)
3061 return error_mark_node
;
3066 /* Handle the pointer + int case. */
3067 if (code0
== POINTER_TYPE
&& code1
== INTEGER_TYPE
)
3068 return cp_pointer_int_sum (PLUS_EXPR
, op0
, op1
);
3069 else if (code1
== POINTER_TYPE
&& code0
== INTEGER_TYPE
)
3070 return cp_pointer_int_sum (PLUS_EXPR
, op1
, op0
);
3076 /* Subtraction of two similar pointers.
3077 We must subtract them as integers, then divide by object size. */
3078 if (code0
== POINTER_TYPE
&& code1
== POINTER_TYPE
3079 && comp_target_types (type0
, type1
, 1))
3080 return pointer_diff (op0
, op1
, common_type (type0
, type1
));
3081 /* Handle pointer minus int. Just like pointer plus int. */
3082 else if (code0
== POINTER_TYPE
&& code1
== INTEGER_TYPE
)
3083 return cp_pointer_int_sum (MINUS_EXPR
, op0
, op1
);
3092 case TRUNC_DIV_EXPR
:
3094 case FLOOR_DIV_EXPR
:
3095 case ROUND_DIV_EXPR
:
3096 case EXACT_DIV_EXPR
:
3097 if ((code0
== INTEGER_TYPE
|| code0
== REAL_TYPE
3098 || code0
== COMPLEX_TYPE
)
3099 && (code1
== INTEGER_TYPE
|| code1
== REAL_TYPE
3100 || code1
== COMPLEX_TYPE
))
3102 if (TREE_CODE (op1
) == INTEGER_CST
&& integer_zerop (op1
))
3103 warning ("division by zero in `%E / 0'", op0
);
3104 else if (TREE_CODE (op1
) == REAL_CST
&& real_zerop (op1
))
3105 warning ("division by zero in `%E / 0.'", op0
);
3107 if (!(code0
== INTEGER_TYPE
&& code1
== INTEGER_TYPE
))
3108 resultcode
= RDIV_EXPR
;
3110 /* When dividing two signed integers, we have to promote to int.
3111 unless we divide by a constant != -1. Note that default
3112 conversion will have been performed on the operands at this
3113 point, so we have to dig out the original type to find out if
3115 shorten
= ((TREE_CODE (op0
) == NOP_EXPR
3116 && TREE_UNSIGNED (TREE_TYPE (TREE_OPERAND (op0
, 0))))
3117 || (TREE_CODE (op1
) == INTEGER_CST
3118 && ! integer_all_onesp (op1
)));
3125 case BIT_ANDTC_EXPR
:
3128 if (code0
== INTEGER_TYPE
&& code1
== INTEGER_TYPE
)
3132 case TRUNC_MOD_EXPR
:
3133 case FLOOR_MOD_EXPR
:
3134 if (code1
== INTEGER_TYPE
&& integer_zerop (op1
))
3135 warning ("division by zero in `%E %% 0'", op0
);
3136 else if (code1
== REAL_TYPE
&& real_zerop (op1
))
3137 warning ("division by zero in `%E %% 0.'", op0
);
3139 if (code0
== INTEGER_TYPE
&& code1
== INTEGER_TYPE
)
3141 /* Although it would be tempting to shorten always here, that loses
3142 on some targets, since the modulo instruction is undefined if the
3143 quotient can't be represented in the computation mode. We shorten
3144 only if unsigned or if dividing by something we know != -1. */
3145 shorten
= ((TREE_CODE (op0
) == NOP_EXPR
3146 && TREE_UNSIGNED (TREE_TYPE (TREE_OPERAND (op0
, 0))))
3147 || (TREE_CODE (op1
) == INTEGER_CST
3148 && ! integer_all_onesp (op1
)));
3153 case TRUTH_ANDIF_EXPR
:
3154 case TRUTH_ORIF_EXPR
:
3155 case TRUTH_AND_EXPR
:
3157 result_type
= boolean_type_node
;
3160 /* Shift operations: result has same type as first operand;
3161 always convert second operand to int.
3162 Also set SHORT_SHIFT if shifting rightward. */
3165 if (code0
== INTEGER_TYPE
&& code1
== INTEGER_TYPE
)
3167 result_type
= type0
;
3168 if (TREE_CODE (op1
) == INTEGER_CST
)
3170 if (tree_int_cst_lt (op1
, integer_zero_node
))
3171 warning ("right shift count is negative");
3174 if (! integer_zerop (op1
))
3176 if (compare_tree_int (op1
, TYPE_PRECISION (type0
)) >= 0)
3177 warning ("right shift count >= width of type");
3180 /* Convert the shift-count to an integer, regardless of
3181 size of value being shifted. */
3182 if (TYPE_MAIN_VARIANT (TREE_TYPE (op1
)) != integer_type_node
)
3183 op1
= cp_convert (integer_type_node
, op1
);
3184 /* Avoid converting op1 to result_type later. */
3190 if (code0
== INTEGER_TYPE
&& code1
== INTEGER_TYPE
)
3192 result_type
= type0
;
3193 if (TREE_CODE (op1
) == INTEGER_CST
)
3195 if (tree_int_cst_lt (op1
, integer_zero_node
))
3196 warning ("left shift count is negative");
3197 else if (compare_tree_int (op1
, TYPE_PRECISION (type0
)) >= 0)
3198 warning ("left shift count >= width of type");
3200 /* Convert the shift-count to an integer, regardless of
3201 size of value being shifted. */
3202 if (TYPE_MAIN_VARIANT (TREE_TYPE (op1
)) != integer_type_node
)
3203 op1
= cp_convert (integer_type_node
, op1
);
3204 /* Avoid converting op1 to result_type later. */
3211 if (code0
== INTEGER_TYPE
&& code1
== INTEGER_TYPE
)
3213 result_type
= type0
;
3214 if (TREE_CODE (op1
) == INTEGER_CST
)
3216 if (tree_int_cst_lt (op1
, integer_zero_node
))
3217 warning ("%s rotate count is negative",
3218 (code
== LROTATE_EXPR
) ? "left" : "right");
3219 else if (compare_tree_int (op1
, TYPE_PRECISION (type0
)) >= 0)
3220 warning ("%s rotate count >= width of type",
3221 (code
== LROTATE_EXPR
) ? "left" : "right");
3223 /* Convert the shift-count to an integer, regardless of
3224 size of value being shifted. */
3225 if (TYPE_MAIN_VARIANT (TREE_TYPE (op1
)) != integer_type_node
)
3226 op1
= cp_convert (integer_type_node
, op1
);
3232 if (warn_float_equal
&& (code0
== REAL_TYPE
|| code1
== REAL_TYPE
))
3233 warning ("comparing floating point with == or != is unsafe");
3235 build_type
= boolean_type_node
;
3236 if ((code0
== INTEGER_TYPE
|| code0
== REAL_TYPE
3237 || code0
== COMPLEX_TYPE
)
3238 && (code1
== INTEGER_TYPE
|| code1
== REAL_TYPE
3239 || code1
== COMPLEX_TYPE
))
3241 else if (code0
== POINTER_TYPE
&& code1
== POINTER_TYPE
)
3242 result_type
= composite_pointer_type (type0
, type1
, op0
, op1
,
3244 else if (code0
== POINTER_TYPE
&& null_ptr_cst_p (op1
))
3245 result_type
= type0
;
3246 else if (code1
== POINTER_TYPE
&& null_ptr_cst_p (op0
))
3247 result_type
= type1
;
3248 else if (code0
== POINTER_TYPE
&& code1
== INTEGER_TYPE
)
3250 result_type
= type0
;
3251 error ("ISO C++ forbids comparison between pointer and integer");
3253 else if (code0
== INTEGER_TYPE
&& code1
== POINTER_TYPE
)
3255 result_type
= type1
;
3256 error ("ISO C++ forbids comparison between pointer and integer");
3258 else if (TYPE_PTRMEMFUNC_P (type0
) && null_ptr_cst_p (op1
))
3260 op0
= build_ptrmemfunc_access_expr (op0
, pfn_identifier
);
3261 op1
= cp_convert (TREE_TYPE (op0
), integer_zero_node
);
3262 result_type
= TREE_TYPE (op0
);
3264 else if (TYPE_PTRMEMFUNC_P (type1
) && null_ptr_cst_p (op0
))
3265 return cp_build_binary_op (code
, op1
, op0
);
3266 else if (TYPE_PTRMEMFUNC_P (type0
) && TYPE_PTRMEMFUNC_P (type1
)
3267 && same_type_p (type0
, type1
))
3269 /* E will be the final comparison. */
3271 /* E1 and E2 are for scratch. */
3279 if (TREE_SIDE_EFFECTS (op0
))
3280 op0
= save_expr (op0
);
3281 if (TREE_SIDE_EFFECTS (op1
))
3282 op1
= save_expr (op1
);
3287 && (!op0.pfn || op0.delta == op1.delta))
3289 The reason for the `!op0.pfn' bit is that a NULL
3290 pointer-to-member is any member with a zero PFN; the
3291 DELTA field is unspecified. */
3292 pfn0
= pfn_from_ptrmemfunc (op0
);
3293 pfn1
= pfn_from_ptrmemfunc (op1
);
3294 delta0
= build_ptrmemfunc_access_expr (op0
,
3296 delta1
= build_ptrmemfunc_access_expr (op1
,
3298 e1
= cp_build_binary_op (EQ_EXPR
, delta0
, delta1
);
3299 e2
= cp_build_binary_op (EQ_EXPR
,
3301 cp_convert (TREE_TYPE (pfn0
),
3302 integer_zero_node
));
3303 e1
= cp_build_binary_op (TRUTH_ORIF_EXPR
, e1
, e2
);
3304 e2
= build (EQ_EXPR
, boolean_type_node
, pfn0
, pfn1
);
3305 e
= cp_build_binary_op (TRUTH_ANDIF_EXPR
, e2
, e1
);
3306 if (code
== EQ_EXPR
)
3308 return cp_build_binary_op (EQ_EXPR
, e
, integer_zero_node
);
3310 else if ((TYPE_PTRMEMFUNC_P (type0
)
3311 && same_type_p (TYPE_PTRMEMFUNC_FN_TYPE (type0
), type1
))
3312 || (TYPE_PTRMEMFUNC_P (type1
)
3313 && same_type_p (TYPE_PTRMEMFUNC_FN_TYPE (type1
), type0
)))
3319 if ((code0
== INTEGER_TYPE
|| code0
== REAL_TYPE
)
3320 && (code1
== INTEGER_TYPE
|| code1
== REAL_TYPE
))
3322 else if (code0
== POINTER_TYPE
&& code1
== POINTER_TYPE
)
3323 result_type
= composite_pointer_type (type0
, type1
, op0
, op1
,
3331 build_type
= boolean_type_node
;
3332 if ((code0
== INTEGER_TYPE
|| code0
== REAL_TYPE
)
3333 && (code1
== INTEGER_TYPE
|| code1
== REAL_TYPE
))
3335 else if (code0
== POINTER_TYPE
&& code1
== POINTER_TYPE
)
3336 result_type
= composite_pointer_type (type0
, type1
, op0
, op1
,
3338 else if (code0
== POINTER_TYPE
&& TREE_CODE (op1
) == INTEGER_CST
3339 && integer_zerop (op1
))
3340 result_type
= type0
;
3341 else if (code1
== POINTER_TYPE
&& TREE_CODE (op0
) == INTEGER_CST
3342 && integer_zerop (op0
))
3343 result_type
= type1
;
3344 else if (code0
== POINTER_TYPE
&& code1
== INTEGER_TYPE
)
3346 result_type
= type0
;
3347 pedwarn ("ISO C++ forbids comparison between pointer and integer");
3349 else if (code0
== INTEGER_TYPE
&& code1
== POINTER_TYPE
)
3351 result_type
= type1
;
3352 pedwarn ("ISO C++ forbids comparison between pointer and integer");
3356 case UNORDERED_EXPR
:
3363 build_type
= integer_type_node
;
3364 if (code0
!= REAL_TYPE
|| code1
!= REAL_TYPE
)
3366 error ("unordered comparison on non-floating point argument");
3367 return error_mark_node
;
3376 if ((code0
== INTEGER_TYPE
|| code0
== REAL_TYPE
|| code0
== COMPLEX_TYPE
)
3378 (code1
== INTEGER_TYPE
|| code1
== REAL_TYPE
|| code1
== COMPLEX_TYPE
))
3380 int none_complex
= (code0
!= COMPLEX_TYPE
&& code1
!= COMPLEX_TYPE
);
3382 if (shorten
|| common
|| short_compare
)
3383 result_type
= common_type (type0
, type1
);
3385 /* For certain operations (which identify themselves by shorten != 0)
3386 if both args were extended from the same smaller type,
3387 do the arithmetic in that type and then extend.
3389 shorten !=0 and !=1 indicates a bitwise operation.
3390 For them, this optimization is safe only if
3391 both args are zero-extended or both are sign-extended.
3392 Otherwise, we might change the result.
3393 Eg, (short)-1 | (unsigned short)-1 is (int)-1
3394 but calculated in (unsigned short) it would be (unsigned short)-1. */
3396 if (shorten
&& none_complex
)
3398 int unsigned0
, unsigned1
;
3399 tree arg0
= get_narrower (op0
, &unsigned0
);
3400 tree arg1
= get_narrower (op1
, &unsigned1
);
3401 /* UNS is 1 if the operation to be done is an unsigned one. */
3402 int uns
= TREE_UNSIGNED (result_type
);
3405 final_type
= result_type
;
3407 /* Handle the case that OP0 does not *contain* a conversion
3408 but it *requires* conversion to FINAL_TYPE. */
3410 if (op0
== arg0
&& TREE_TYPE (op0
) != final_type
)
3411 unsigned0
= TREE_UNSIGNED (TREE_TYPE (op0
));
3412 if (op1
== arg1
&& TREE_TYPE (op1
) != final_type
)
3413 unsigned1
= TREE_UNSIGNED (TREE_TYPE (op1
));
3415 /* Now UNSIGNED0 is 1 if ARG0 zero-extends to FINAL_TYPE. */
3417 /* For bitwise operations, signedness of nominal type
3418 does not matter. Consider only how operands were extended. */
3422 /* Note that in all three cases below we refrain from optimizing
3423 an unsigned operation on sign-extended args.
3424 That would not be valid. */
3426 /* Both args variable: if both extended in same way
3427 from same width, do it in that width.
3428 Do it unsigned if args were zero-extended. */
3429 if ((TYPE_PRECISION (TREE_TYPE (arg0
))
3430 < TYPE_PRECISION (result_type
))
3431 && (TYPE_PRECISION (TREE_TYPE (arg1
))
3432 == TYPE_PRECISION (TREE_TYPE (arg0
)))
3433 && unsigned0
== unsigned1
3434 && (unsigned0
|| !uns
))
3435 result_type
= c_common_signed_or_unsigned_type
3436 (unsigned0
, common_type (TREE_TYPE (arg0
), TREE_TYPE (arg1
)));
3437 else if (TREE_CODE (arg0
) == INTEGER_CST
3438 && (unsigned1
|| !uns
)
3439 && (TYPE_PRECISION (TREE_TYPE (arg1
))
3440 < TYPE_PRECISION (result_type
))
3441 && (type
= c_common_signed_or_unsigned_type
3442 (unsigned1
, TREE_TYPE (arg1
)),
3443 int_fits_type_p (arg0
, type
)))
3445 else if (TREE_CODE (arg1
) == INTEGER_CST
3446 && (unsigned0
|| !uns
)
3447 && (TYPE_PRECISION (TREE_TYPE (arg0
))
3448 < TYPE_PRECISION (result_type
))
3449 && (type
= c_common_signed_or_unsigned_type
3450 (unsigned0
, TREE_TYPE (arg0
)),
3451 int_fits_type_p (arg1
, type
)))
3455 /* Shifts can be shortened if shifting right. */
3460 tree arg0
= get_narrower (op0
, &unsigned_arg
);
3462 final_type
= result_type
;
3464 if (arg0
== op0
&& final_type
== TREE_TYPE (op0
))
3465 unsigned_arg
= TREE_UNSIGNED (TREE_TYPE (op0
));
3467 if (TYPE_PRECISION (TREE_TYPE (arg0
)) < TYPE_PRECISION (result_type
)
3468 /* We can shorten only if the shift count is less than the
3469 number of bits in the smaller type size. */
3470 && compare_tree_int (op1
, TYPE_PRECISION (TREE_TYPE (arg0
))) < 0
3471 /* If arg is sign-extended and then unsigned-shifted,
3472 we can simulate this with a signed shift in arg's type
3473 only if the extended result is at least twice as wide
3474 as the arg. Otherwise, the shift could use up all the
3475 ones made by sign-extension and bring in zeros.
3476 We can't optimize that case at all, but in most machines
3477 it never happens because available widths are 2**N. */
3478 && (!TREE_UNSIGNED (final_type
)
3480 || (((unsigned) 2 * TYPE_PRECISION (TREE_TYPE (arg0
)))
3481 <= TYPE_PRECISION (result_type
))))
3483 /* Do an unsigned shift if the operand was zero-extended. */
3485 = c_common_signed_or_unsigned_type (unsigned_arg
,
3487 /* Convert value-to-be-shifted to that type. */
3488 if (TREE_TYPE (op0
) != result_type
)
3489 op0
= cp_convert (result_type
, op0
);
3494 /* Comparison operations are shortened too but differently.
3495 They identify themselves by setting short_compare = 1. */
3499 /* Don't write &op0, etc., because that would prevent op0
3500 from being kept in a register.
3501 Instead, make copies of the our local variables and
3502 pass the copies by reference, then copy them back afterward. */
3503 tree xop0
= op0
, xop1
= op1
, xresult_type
= result_type
;
3504 enum tree_code xresultcode
= resultcode
;
3506 = shorten_compare (&xop0
, &xop1
, &xresult_type
, &xresultcode
);
3508 return cp_convert (boolean_type_node
, val
);
3509 op0
= xop0
, op1
= xop1
;
3511 resultcode
= xresultcode
;
3514 if ((short_compare
|| code
== MIN_EXPR
|| code
== MAX_EXPR
)
3515 && warn_sign_compare
)
3517 int op0_signed
= ! TREE_UNSIGNED (TREE_TYPE (orig_op0
));
3518 int op1_signed
= ! TREE_UNSIGNED (TREE_TYPE (orig_op1
));
3520 int unsignedp0
, unsignedp1
;
3521 tree primop0
= get_narrower (op0
, &unsignedp0
);
3522 tree primop1
= get_narrower (op1
, &unsignedp1
);
3524 /* Check for comparison of different enum types. */
3525 if (TREE_CODE (TREE_TYPE (orig_op0
)) == ENUMERAL_TYPE
3526 && TREE_CODE (TREE_TYPE (orig_op1
)) == ENUMERAL_TYPE
3527 && TYPE_MAIN_VARIANT (TREE_TYPE (orig_op0
))
3528 != TYPE_MAIN_VARIANT (TREE_TYPE (orig_op1
)))
3530 warning ("comparison between types `%#T' and `%#T'",
3531 TREE_TYPE (orig_op0
), TREE_TYPE (orig_op1
));
3534 /* Give warnings for comparisons between signed and unsigned
3535 quantities that may fail. */
3536 /* Do the checking based on the original operand trees, so that
3537 casts will be considered, but default promotions won't be. */
3539 /* Do not warn if the comparison is being done in a signed type,
3540 since the signed type will only be chosen if it can represent
3541 all the values of the unsigned type. */
3542 if (! TREE_UNSIGNED (result_type
))
3544 /* Do not warn if both operands are unsigned. */
3545 else if (op0_signed
== op1_signed
)
3547 /* Do not warn if the signed quantity is an unsuffixed
3548 integer literal (or some static constant expression
3549 involving such literals or a conditional expression
3550 involving such literals) and it is non-negative. */
3551 else if ((op0_signed
&& tree_expr_nonnegative_p (orig_op0
))
3552 || (op1_signed
&& tree_expr_nonnegative_p (orig_op1
)))
3554 /* Do not warn if the comparison is an equality operation,
3555 the unsigned quantity is an integral constant and it does
3556 not use the most significant bit of result_type. */
3557 else if ((resultcode
== EQ_EXPR
|| resultcode
== NE_EXPR
)
3558 && ((op0_signed
&& TREE_CODE (orig_op1
) == INTEGER_CST
3559 && int_fits_type_p (orig_op1
, c_common_signed_type
3561 || (op1_signed
&& TREE_CODE (orig_op0
) == INTEGER_CST
3562 && int_fits_type_p (orig_op0
, c_common_signed_type
3566 warning ("comparison between signed and unsigned integer expressions");
3568 /* Warn if two unsigned values are being compared in a size
3569 larger than their original size, and one (and only one) is the
3570 result of a `~' operator. This comparison will always fail.
3572 Also warn if one operand is a constant, and the constant does not
3573 have all bits set that are set in the ~ operand when it is
3576 if ((TREE_CODE (primop0
) == BIT_NOT_EXPR
)
3577 ^ (TREE_CODE (primop1
) == BIT_NOT_EXPR
))
3579 if (TREE_CODE (primop0
) == BIT_NOT_EXPR
)
3580 primop0
= get_narrower (TREE_OPERAND (op0
, 0), &unsignedp0
);
3581 if (TREE_CODE (primop1
) == BIT_NOT_EXPR
)
3582 primop1
= get_narrower (TREE_OPERAND (op1
, 0), &unsignedp1
);
3584 if (host_integerp (primop0
, 0) || host_integerp (primop1
, 0))
3587 HOST_WIDE_INT constant
, mask
;
3591 if (host_integerp (primop0
, 0))
3594 unsignedp
= unsignedp1
;
3595 constant
= tree_low_cst (primop0
, 0);
3600 unsignedp
= unsignedp0
;
3601 constant
= tree_low_cst (primop1
, 0);
3604 bits
= TYPE_PRECISION (TREE_TYPE (primop
));
3605 if (bits
< TYPE_PRECISION (result_type
)
3606 && bits
< HOST_BITS_PER_LONG
&& unsignedp
)
3608 mask
= (~ (HOST_WIDE_INT
) 0) << bits
;
3609 if ((mask
& constant
) != mask
)
3610 warning ("comparison of promoted ~unsigned with constant");
3613 else if (unsignedp0
&& unsignedp1
3614 && (TYPE_PRECISION (TREE_TYPE (primop0
))
3615 < TYPE_PRECISION (result_type
))
3616 && (TYPE_PRECISION (TREE_TYPE (primop1
))
3617 < TYPE_PRECISION (result_type
)))
3618 warning ("comparison of promoted ~unsigned with unsigned");
3623 /* At this point, RESULT_TYPE must be nonzero to avoid an error message.
3624 If CONVERTED is zero, both args will be converted to type RESULT_TYPE.
3625 Then the expression will be built.
3626 It will be given type FINAL_TYPE if that is nonzero;
3627 otherwise, it will be given type RESULT_TYPE. */
3631 error ("invalid operands of types `%T' and `%T' to binary `%O'",
3632 TREE_TYPE (orig_op0
), TREE_TYPE (orig_op1
), code
);
3633 return error_mark_node
;
3636 /* Issue warnings about peculiar, but valid, uses of NULL. */
3637 if (/* It's reasonable to use pointer values as operands of &&
3638 and ||, so NULL is no exception. */
3639 !(code
== TRUTH_ANDIF_EXPR
|| code
== TRUTH_ORIF_EXPR
)
3640 && (/* If OP0 is NULL and OP1 is not a pointer, or vice versa. */
3641 (orig_op0
== null_node
3642 && TREE_CODE (TREE_TYPE (op1
)) != POINTER_TYPE
)
3643 /* Or vice versa. */
3644 || (orig_op1
== null_node
3645 && TREE_CODE (TREE_TYPE (op0
)) != POINTER_TYPE
)
3646 /* Or, both are NULL and the operation was not a comparison. */
3647 || (orig_op0
== null_node
&& orig_op1
== null_node
3648 && code
!= EQ_EXPR
&& code
!= NE_EXPR
)))
3649 /* Some sort of arithmetic operation involving NULL was
3650 performed. Note that pointer-difference and pointer-addition
3651 have already been handled above, and so we don't end up here in
3653 warning ("NULL used in arithmetic");
3657 if (TREE_TYPE (op0
) != result_type
)
3658 op0
= cp_convert (result_type
, op0
);
3659 if (TREE_TYPE (op1
) != result_type
)
3660 op1
= cp_convert (result_type
, op1
);
3662 if (op0
== error_mark_node
|| op1
== error_mark_node
)
3663 return error_mark_node
;
3666 if (build_type
== NULL_TREE
)
3667 build_type
= result_type
;
3670 register tree result
= build (resultcode
, build_type
, op0
, op1
);
3671 register tree folded
;
3673 folded
= fold (result
);
3674 if (folded
== result
)
3675 TREE_CONSTANT (folded
) = TREE_CONSTANT (op0
) & TREE_CONSTANT (op1
);
3676 if (final_type
!= 0)
3677 return cp_convert (final_type
, folded
);
3682 /* Return a tree for the sum or difference (RESULTCODE says which)
3683 of pointer PTROP and integer INTOP. */
3686 cp_pointer_int_sum (resultcode
, ptrop
, intop
)
3687 enum tree_code resultcode
;
3688 register tree ptrop
, intop
;
3690 tree res_type
= TREE_TYPE (ptrop
);
3692 /* pointer_int_sum() uses size_in_bytes() on the TREE_TYPE(res_type)
3693 in certain circumstance (when it's valid to do so). So we need
3694 to make sure it's complete. We don't need to check here, if we
3695 can actually complete it at all, as those checks will be done in
3696 pointer_int_sum() anyway. */
3697 complete_type (TREE_TYPE (res_type
));
3699 return pointer_int_sum (resultcode
, ptrop
, fold (intop
));
3702 /* Return a tree for the difference of pointers OP0 and OP1.
3703 The resulting tree has type int. */
3706 pointer_diff (op0
, op1
, ptrtype
)
3707 register tree op0
, op1
;
3708 register tree ptrtype
;
3710 register tree result
, folded
;
3711 tree restype
= ptrdiff_type_node
;
3712 tree target_type
= TREE_TYPE (ptrtype
);
3714 if (!complete_type_or_else (target_type
, NULL_TREE
))
3715 return error_mark_node
;
3717 if (pedantic
|| warn_pointer_arith
)
3719 if (TREE_CODE (target_type
) == VOID_TYPE
)
3720 pedwarn ("ISO C++ forbids using pointer of type `void *' in subtraction");
3721 if (TREE_CODE (target_type
) == FUNCTION_TYPE
)
3722 pedwarn ("ISO C++ forbids using pointer to a function in subtraction");
3723 if (TREE_CODE (target_type
) == METHOD_TYPE
)
3724 pedwarn ("ISO C++ forbids using pointer to a method in subtraction");
3725 if (TREE_CODE (target_type
) == OFFSET_TYPE
)
3726 pedwarn ("ISO C++ forbids using pointer to a member in subtraction");
3729 /* First do the subtraction as integers;
3730 then drop through to build the divide operator. */
3732 op0
= cp_build_binary_op (MINUS_EXPR
,
3733 cp_convert (restype
, op0
),
3734 cp_convert (restype
, op1
));
3736 /* This generates an error if op1 is a pointer to an incomplete type. */
3737 if (!COMPLETE_TYPE_P (TREE_TYPE (TREE_TYPE (op1
))))
3738 error ("invalid use of a pointer to an incomplete type in pointer arithmetic");
3740 op1
= ((TREE_CODE (target_type
) == VOID_TYPE
3741 || TREE_CODE (target_type
) == FUNCTION_TYPE
3742 || TREE_CODE (target_type
) == METHOD_TYPE
3743 || TREE_CODE (target_type
) == OFFSET_TYPE
)
3745 : size_in_bytes (target_type
));
3747 /* Do the division. */
3749 result
= build (EXACT_DIV_EXPR
, restype
, op0
, cp_convert (restype
, op1
));
3751 folded
= fold (result
);
3752 if (folded
== result
)
3753 TREE_CONSTANT (folded
) = TREE_CONSTANT (op0
) & TREE_CONSTANT (op1
);
3757 /* Construct and perhaps optimize a tree representation
3758 for a unary operation. CODE, a tree_code, specifies the operation
3759 and XARG is the operand. */
3762 build_x_unary_op (code
, xarg
)
3763 enum tree_code code
;
3769 if (processing_template_decl
)
3770 return build_min_nt (code
, xarg
, NULL_TREE
);
3772 /* & rec, on incomplete RECORD_TYPEs is the simple opr &, not an
3774 if (code
== ADDR_EXPR
3775 && TREE_CODE (xarg
) != TEMPLATE_ID_EXPR
3776 && ((IS_AGGR_TYPE_CODE (TREE_CODE (TREE_TYPE (xarg
)))
3777 && !COMPLETE_TYPE_P (TREE_TYPE (xarg
)))
3778 || (TREE_CODE (xarg
) == OFFSET_REF
)))
3779 /* don't look for a function */;
3784 rval
= build_new_op (code
, LOOKUP_NORMAL
, xarg
,
3785 NULL_TREE
, NULL_TREE
);
3786 if (rval
|| code
!= ADDR_EXPR
)
3789 if (code
== ADDR_EXPR
)
3791 /* A pointer to member-function can be formed only by saying
3793 if (!flag_ms_extensions
&& TREE_CODE (TREE_TYPE (xarg
)) == METHOD_TYPE
3794 && (TREE_CODE (xarg
) != OFFSET_REF
|| !PTRMEM_OK_P (xarg
)))
3796 if (TREE_CODE (xarg
) != OFFSET_REF
)
3798 error ("invalid use of '%E' to form a pointer-to-member-function. Use a qualified-id.",
3800 return error_mark_node
;
3804 error ("parenthesis around '%E' cannot be used to form a pointer-to-member-function",
3806 PTRMEM_OK_P (xarg
) = 1;
3810 if (TREE_CODE (xarg
) == OFFSET_REF
)
3812 ptrmem
= PTRMEM_OK_P (xarg
);
3814 if (!ptrmem
&& !flag_ms_extensions
3815 && TREE_CODE (TREE_TYPE (TREE_OPERAND (xarg
, 1))) == METHOD_TYPE
)
3817 /* A single non-static member, make sure we don't allow a
3818 pointer-to-member. */
3819 xarg
= build (OFFSET_REF
, TREE_TYPE (xarg
),
3820 TREE_OPERAND (xarg
, 0),
3821 ovl_cons (TREE_OPERAND (xarg
, 1), NULL_TREE
));
3822 PTRMEM_OK_P (xarg
) = ptrmem
;
3826 else if (TREE_CODE (xarg
) == TARGET_EXPR
)
3827 warning ("taking address of temporary");
3829 exp
= build_unary_op (code
, xarg
, 0);
3830 if (TREE_CODE (exp
) == ADDR_EXPR
)
3831 PTRMEM_OK_P (exp
) = ptrmem
;
3836 /* Like c_common_truthvalue_conversion, but handle pointer-to-member
3837 constants, where a null value is represented by an INTEGER_CST of
3841 cp_truthvalue_conversion (expr
)
3844 tree type
= TREE_TYPE (expr
);
3845 if (TYPE_PTRMEM_P (type
))
3846 return build_binary_op (NE_EXPR
, expr
, integer_zero_node
, 1);
3848 return c_common_truthvalue_conversion (expr
);
3851 /* Just like cp_truthvalue_conversion, but we want a CLEANUP_POINT_EXPR. */
3854 condition_conversion (expr
)
3858 if (processing_template_decl
)
3860 if (TREE_CODE (expr
) == OFFSET_REF
)
3861 expr
= resolve_offset_ref (expr
);
3862 t
= perform_implicit_conversion (boolean_type_node
, expr
);
3863 t
= fold (build1 (CLEANUP_POINT_EXPR
, boolean_type_node
, t
));
3867 /* C++: Must handle pointers to members.
3869 Perhaps type instantiation should be extended to handle conversion
3870 from aggregates to types we don't yet know we want? (Or are those
3871 cases typically errors which should be reported?)
3873 NOCONVERT nonzero suppresses the default promotions
3874 (such as from short to int). */
3877 build_unary_op (code
, xarg
, noconvert
)
3878 enum tree_code code
;
3882 /* No default_conversion here. It causes trouble for ADDR_EXPR. */
3883 register tree arg
= xarg
;
3884 register tree argtype
= 0;
3885 const char *errstring
= NULL
;
3888 if (arg
== error_mark_node
)
3889 return error_mark_node
;
3894 /* This is used for unary plus, because a CONVERT_EXPR
3895 is enough to prevent anybody from looking inside for
3896 associativity, but won't generate any code. */
3897 if (!(arg
= build_expr_type_conversion
3898 (WANT_ARITH
| WANT_ENUM
| WANT_POINTER
, arg
, 1)))
3899 errstring
= "wrong type argument to unary plus";
3903 arg
= default_conversion (arg
);
3904 arg
= build1 (NON_LVALUE_EXPR
, TREE_TYPE (arg
), arg
);
3905 TREE_CONSTANT (arg
) = TREE_CONSTANT (TREE_OPERAND (arg
, 0));
3910 if (!(arg
= build_expr_type_conversion (WANT_ARITH
| WANT_ENUM
, arg
, 1)))
3911 errstring
= "wrong type argument to unary minus";
3912 else if (!noconvert
)
3913 arg
= default_conversion (arg
);
3917 if (TREE_CODE (TREE_TYPE (arg
)) == COMPLEX_TYPE
)
3921 arg
= default_conversion (arg
);
3923 else if (!(arg
= build_expr_type_conversion (WANT_INT
| WANT_ENUM
,
3925 errstring
= "wrong type argument to bit-complement";
3926 else if (!noconvert
)
3927 arg
= default_conversion (arg
);
3931 if (!(arg
= build_expr_type_conversion (WANT_ARITH
| WANT_ENUM
, arg
, 1)))
3932 errstring
= "wrong type argument to abs";
3933 else if (!noconvert
)
3934 arg
= default_conversion (arg
);
3938 /* Conjugating a real value is a no-op, but allow it anyway. */
3939 if (!(arg
= build_expr_type_conversion (WANT_ARITH
| WANT_ENUM
, arg
, 1)))
3940 errstring
= "wrong type argument to conjugation";
3941 else if (!noconvert
)
3942 arg
= default_conversion (arg
);
3945 case TRUTH_NOT_EXPR
:
3946 arg
= cp_convert (boolean_type_node
, arg
);
3947 val
= invert_truthvalue (arg
);
3948 if (arg
!= error_mark_node
)
3950 errstring
= "in argument to unary !";
3957 if (TREE_CODE (arg
) == COMPLEX_CST
)
3958 return TREE_REALPART (arg
);
3959 else if (TREE_CODE (TREE_TYPE (arg
)) == COMPLEX_TYPE
)
3960 return fold (build1 (REALPART_EXPR
, TREE_TYPE (TREE_TYPE (arg
)), arg
));
3965 if (TREE_CODE (arg
) == COMPLEX_CST
)
3966 return TREE_IMAGPART (arg
);
3967 else if (TREE_CODE (TREE_TYPE (arg
)) == COMPLEX_TYPE
)
3968 return fold (build1 (IMAGPART_EXPR
, TREE_TYPE (TREE_TYPE (arg
)), arg
));
3970 return cp_convert (TREE_TYPE (arg
), integer_zero_node
);
3972 case PREINCREMENT_EXPR
:
3973 case POSTINCREMENT_EXPR
:
3974 case PREDECREMENT_EXPR
:
3975 case POSTDECREMENT_EXPR
:
3976 /* Handle complex lvalues (when permitted)
3977 by reduction to simpler cases. */
3979 val
= unary_complex_lvalue (code
, arg
);
3983 /* Increment or decrement the real part of the value,
3984 and don't change the imaginary part. */
3985 if (TREE_CODE (TREE_TYPE (arg
)) == COMPLEX_TYPE
)
3989 arg
= stabilize_reference (arg
);
3990 real
= build_unary_op (REALPART_EXPR
, arg
, 1);
3991 imag
= build_unary_op (IMAGPART_EXPR
, arg
, 1);
3992 return build (COMPLEX_EXPR
, TREE_TYPE (arg
),
3993 build_unary_op (code
, real
, 1), imag
);
3996 /* Report invalid types. */
3998 if (!(arg
= build_expr_type_conversion (WANT_ARITH
| WANT_POINTER
,
4001 if (code
== PREINCREMENT_EXPR
)
4002 errstring
="no pre-increment operator for type";
4003 else if (code
== POSTINCREMENT_EXPR
)
4004 errstring
="no post-increment operator for type";
4005 else if (code
== PREDECREMENT_EXPR
)
4006 errstring
="no pre-decrement operator for type";
4008 errstring
="no post-decrement operator for type";
4012 /* Report something read-only. */
4014 if (CP_TYPE_CONST_P (TREE_TYPE (arg
))
4015 || TREE_READONLY (arg
))
4016 readonly_error (arg
, ((code
== PREINCREMENT_EXPR
4017 || code
== POSTINCREMENT_EXPR
)
4018 ? "increment" : "decrement"),
4023 tree result_type
= TREE_TYPE (arg
);
4025 arg
= get_unwidened (arg
, 0);
4026 argtype
= TREE_TYPE (arg
);
4028 /* ARM $5.2.5 last annotation says this should be forbidden. */
4029 if (TREE_CODE (argtype
) == ENUMERAL_TYPE
)
4030 pedwarn ("ISO C++ forbids %sing an enum",
4031 (code
== PREINCREMENT_EXPR
|| code
== POSTINCREMENT_EXPR
)
4032 ? "increment" : "decrement");
4034 /* Compute the increment. */
4036 if (TREE_CODE (argtype
) == POINTER_TYPE
)
4038 enum tree_code tmp
= TREE_CODE (TREE_TYPE (argtype
));
4039 tree type
= complete_type (TREE_TYPE (argtype
));
4041 if (!COMPLETE_OR_VOID_TYPE_P (type
))
4042 error ("cannot %s a pointer to incomplete type `%T'",
4043 ((code
== PREINCREMENT_EXPR
4044 || code
== POSTINCREMENT_EXPR
)
4045 ? "increment" : "decrement"), TREE_TYPE (argtype
));
4046 else if ((pedantic
|| warn_pointer_arith
)
4047 && (tmp
== FUNCTION_TYPE
|| tmp
== METHOD_TYPE
4048 || tmp
== VOID_TYPE
|| tmp
== OFFSET_TYPE
))
4049 pedwarn ("ISO C++ forbids %sing a pointer of type `%T'",
4050 ((code
== PREINCREMENT_EXPR
4051 || code
== POSTINCREMENT_EXPR
)
4052 ? "increment" : "decrement"), argtype
);
4053 inc
= cxx_sizeof_nowarn (TREE_TYPE (argtype
));
4056 inc
= integer_one_node
;
4058 inc
= cp_convert (argtype
, inc
);
4060 /* Handle incrementing a cast-expression. */
4062 switch (TREE_CODE (arg
))
4067 case FIX_TRUNC_EXPR
:
4068 case FIX_FLOOR_EXPR
:
4069 case FIX_ROUND_EXPR
:
4072 tree incremented
, modify
, value
, compound
;
4073 if (! lvalue_p (arg
) && pedantic
)
4074 pedwarn ("cast to non-reference type used as lvalue");
4075 arg
= stabilize_reference (arg
);
4076 if (code
== PREINCREMENT_EXPR
|| code
== PREDECREMENT_EXPR
)
4079 value
= save_expr (arg
);
4080 incremented
= build (((code
== PREINCREMENT_EXPR
4081 || code
== POSTINCREMENT_EXPR
)
4082 ? PLUS_EXPR
: MINUS_EXPR
),
4083 argtype
, value
, inc
);
4085 modify
= build_modify_expr (arg
, NOP_EXPR
, incremented
);
4086 compound
= build (COMPOUND_EXPR
, TREE_TYPE (arg
), modify
, value
);
4088 /* Eliminate warning about unused result of + or -. */
4089 TREE_NO_UNUSED_WARNING (compound
) = 1;
4097 /* Complain about anything else that is not a true lvalue. */
4098 if (!lvalue_or_else (arg
, ((code
== PREINCREMENT_EXPR
4099 || code
== POSTINCREMENT_EXPR
)
4100 ? "increment" : "decrement")))
4101 return error_mark_node
;
4103 /* Forbid using -- on `bool'. */
4104 if (TREE_TYPE (arg
) == boolean_type_node
)
4106 if (code
== POSTDECREMENT_EXPR
|| code
== PREDECREMENT_EXPR
)
4108 error ("invalid use of `--' on bool variable `%D'", arg
);
4109 return error_mark_node
;
4112 /* This will only work if someone can convince Kenner to accept
4113 my patch to expand_increment. (jason) */
4114 val
= build (code
, TREE_TYPE (arg
), arg
, inc
);
4116 val
= boolean_increment (code
, arg
);
4120 val
= build (code
, TREE_TYPE (arg
), arg
, inc
);
4122 TREE_SIDE_EFFECTS (val
) = 1;
4123 return cp_convert (result_type
, val
);
4127 /* Note that this operation never does default_conversion
4128 regardless of NOCONVERT. */
4130 argtype
= lvalue_type (arg
);
4131 if (TREE_CODE (argtype
) == REFERENCE_TYPE
)
4135 build_pointer_type (TREE_TYPE (argtype
)), arg
);
4136 TREE_CONSTANT (arg
) = TREE_CONSTANT (TREE_OPERAND (arg
, 0));
4139 else if (pedantic
&& DECL_MAIN_P (arg
))
4141 pedwarn ("ISO C++ forbids taking address of function `::main'");
4143 /* Let &* cancel out to simplify resulting code. */
4144 if (TREE_CODE (arg
) == INDIRECT_REF
)
4146 /* We don't need to have `current_class_ptr' wrapped in a
4147 NON_LVALUE_EXPR node. */
4148 if (arg
== current_class_ref
)
4149 return current_class_ptr
;
4151 arg
= TREE_OPERAND (arg
, 0);
4152 if (TREE_CODE (TREE_TYPE (arg
)) == REFERENCE_TYPE
)
4156 build_pointer_type (TREE_TYPE (TREE_TYPE (arg
))), arg
);
4157 TREE_CONSTANT (arg
) = TREE_CONSTANT (TREE_OPERAND (arg
, 0));
4159 else if (lvalue_p (arg
))
4160 /* Don't let this be an lvalue. */
4161 return non_lvalue (arg
);
4165 /* For &x[y], return x+y */
4166 if (TREE_CODE (arg
) == ARRAY_REF
)
4168 if (!cxx_mark_addressable (TREE_OPERAND (arg
, 0)))
4169 return error_mark_node
;
4170 return cp_build_binary_op (PLUS_EXPR
, TREE_OPERAND (arg
, 0),
4171 TREE_OPERAND (arg
, 1));
4174 /* Uninstantiated types are all functions. Taking the
4175 address of a function is a no-op, so just return the
4178 if (TREE_CODE (arg
) == IDENTIFIER_NODE
4179 && IDENTIFIER_OPNAME_P (arg
))
4182 /* We don't know the type yet, so just work around the problem.
4183 We know that this will resolve to an lvalue. */
4184 return build1 (ADDR_EXPR
, unknown_type_node
, arg
);
4187 if (TREE_CODE (arg
) == COMPONENT_REF
&& type_unknown_p (arg
)
4188 && !really_overloaded_fn (TREE_OPERAND (arg
, 1)))
4190 /* They're trying to take the address of a unique non-static
4191 member function. This is ill-formed (except in MS-land),
4192 but let's try to DTRT.
4193 Note: We only handle unique functions here because we don't
4194 want to complain if there's a static overload; non-unique
4195 cases will be handled by instantiate_type. But we need to
4196 handle this case here to allow casts on the resulting PMF.
4197 We could defer this in non-MS mode, but it's easier to give
4198 a useful error here. */
4200 tree base
= TREE_TYPE (TREE_OPERAND (arg
, 0));
4201 tree name
= DECL_NAME (get_first_fn (TREE_OPERAND (arg
, 1)));
4203 if (! flag_ms_extensions
)
4205 if (current_class_type
4206 && TREE_OPERAND (arg
, 0) == current_class_ref
)
4207 /* An expression like &memfn. */
4208 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
);
4210 pedwarn ("ISO C++ forbids taking the address of a bound member function to form a pointer to member function. Say `&%T::%D'", base
, name
);
4212 arg
= build_offset_ref (base
, name
);
4215 if (type_unknown_p (arg
))
4216 return build1 (ADDR_EXPR
, unknown_type_node
, arg
);
4218 /* Handle complex lvalues (when permitted)
4219 by reduction to simpler cases. */
4220 val
= unary_complex_lvalue (code
, arg
);
4224 switch (TREE_CODE (arg
))
4229 case FIX_TRUNC_EXPR
:
4230 case FIX_FLOOR_EXPR
:
4231 case FIX_ROUND_EXPR
:
4233 if (! lvalue_p (arg
) && pedantic
)
4234 pedwarn ("ISO C++ forbids taking the address of a cast to a non-lvalue expression");
4241 /* Allow the address of a constructor if all the elements
4243 if (TREE_CODE (arg
) == CONSTRUCTOR
&& TREE_HAS_CONSTRUCTOR (arg
)
4244 && TREE_CONSTANT (arg
))
4246 /* Anything not already handled and not a true memory reference
4248 else if (TREE_CODE (argtype
) != FUNCTION_TYPE
4249 && TREE_CODE (argtype
) != METHOD_TYPE
4250 && !lvalue_or_else (arg
, "unary `&'"))
4251 return error_mark_node
;
4253 if (argtype
!= error_mark_node
)
4254 argtype
= build_pointer_type (argtype
);
4256 if (!cxx_mark_addressable (arg
))
4257 return error_mark_node
;
4262 if (TREE_CODE (arg
) == COMPONENT_REF
4263 && DECL_C_BIT_FIELD (TREE_OPERAND (arg
, 1)))
4265 error ("attempt to take address of bit-field structure member `%D'",
4266 TREE_OPERAND (arg
, 1));
4267 return error_mark_node
;
4269 else if (TREE_CODE (arg
) == COMPONENT_REF
4270 && TREE_CODE (TREE_OPERAND (arg
, 0)) == INDIRECT_REF
4271 && (TREE_CODE (TREE_OPERAND (TREE_OPERAND (arg
, 0), 0))
4274 /* offsetof idiom, fold it. */
4275 tree field
= TREE_OPERAND (arg
, 1);
4276 tree rval
= build_unary_op (ADDR_EXPR
, TREE_OPERAND (arg
, 0), 0);
4277 tree binfo
= lookup_base (TREE_TYPE (TREE_TYPE (rval
)),
4278 decl_type_context (field
),
4281 rval
= build_base_path (PLUS_EXPR
, rval
, binfo
, 1);
4282 rval
= build1 (NOP_EXPR
, argtype
, rval
);
4283 TREE_CONSTANT (rval
) = TREE_CONSTANT (TREE_OPERAND (rval
, 0));
4284 addr
= fold (build (PLUS_EXPR
, argtype
, rval
,
4285 cp_convert (argtype
, byte_position (field
))));
4288 addr
= build1 (ADDR_EXPR
, argtype
, arg
);
4290 /* Address of a static or external variable or
4291 function counts as a constant */
4293 TREE_CONSTANT (addr
) = 1;
4295 if (TREE_CODE (argtype
) == POINTER_TYPE
4296 && TREE_CODE (TREE_TYPE (argtype
)) == METHOD_TYPE
)
4298 build_ptrmemfunc_type (argtype
);
4299 addr
= build_ptrmemfunc (argtype
, addr
, 0);
4312 argtype
= TREE_TYPE (arg
);
4313 return fold (build1 (code
, argtype
, arg
));
4316 error ("%s", errstring
);
4317 return error_mark_node
;
4320 /* Apply unary lvalue-demanding operator CODE to the expression ARG
4321 for certain kinds of expressions which are not really lvalues
4322 but which we can accept as lvalues.
4324 If ARG is not a kind of expression we can handle, return zero. */
4327 unary_complex_lvalue (code
, arg
)
4328 enum tree_code code
;
4331 /* Handle (a, b) used as an "lvalue". */
4332 if (TREE_CODE (arg
) == COMPOUND_EXPR
)
4334 tree real_result
= build_unary_op (code
, TREE_OPERAND (arg
, 1), 0);
4335 return build (COMPOUND_EXPR
, TREE_TYPE (real_result
),
4336 TREE_OPERAND (arg
, 0), real_result
);
4339 /* Handle (a ? b : c) used as an "lvalue". */
4340 if (TREE_CODE (arg
) == COND_EXPR
4341 || TREE_CODE (arg
) == MIN_EXPR
|| TREE_CODE (arg
) == MAX_EXPR
)
4342 return rationalize_conditional_expr (code
, arg
);
4344 /* Handle (a = b), (++a), and (--a) used as an "lvalue". */
4345 if (TREE_CODE (arg
) == MODIFY_EXPR
4346 || TREE_CODE (arg
) == PREINCREMENT_EXPR
4347 || TREE_CODE (arg
) == PREDECREMENT_EXPR
)
4349 tree lvalue
= TREE_OPERAND (arg
, 0);
4350 if (TREE_SIDE_EFFECTS (lvalue
))
4352 lvalue
= stabilize_reference (lvalue
);
4353 arg
= build (TREE_CODE (arg
), TREE_TYPE (arg
),
4354 lvalue
, TREE_OPERAND (arg
, 1));
4356 return unary_complex_lvalue
4357 (code
, build (COMPOUND_EXPR
, TREE_TYPE (lvalue
), arg
, lvalue
));
4360 if (code
!= ADDR_EXPR
)
4363 /* Handle (a = b) used as an "lvalue" for `&'. */
4364 if (TREE_CODE (arg
) == MODIFY_EXPR
4365 || TREE_CODE (arg
) == INIT_EXPR
)
4367 tree real_result
= build_unary_op (code
, TREE_OPERAND (arg
, 0), 0);
4368 arg
= build (COMPOUND_EXPR
, TREE_TYPE (real_result
), arg
, real_result
);
4369 TREE_NO_UNUSED_WARNING (arg
) = 1;
4373 if (TREE_CODE (TREE_TYPE (arg
)) == FUNCTION_TYPE
4374 || TREE_CODE (TREE_TYPE (arg
)) == METHOD_TYPE
4375 || TREE_CODE (TREE_TYPE (arg
)) == OFFSET_TYPE
)
4377 /* The representation of something of type OFFSET_TYPE
4378 is really the representation of a pointer to it.
4379 Here give the representation its true type. */
4382 my_friendly_assert (TREE_CODE (arg
) != SCOPE_REF
, 313);
4384 if (TREE_CODE (arg
) != OFFSET_REF
)
4387 t
= TREE_OPERAND (arg
, 1);
4389 /* Check all this code for right semantics. */
4390 if (TREE_CODE (t
) == FUNCTION_DECL
)
4392 if (DECL_DESTRUCTOR_P (t
))
4393 error ("taking address of destructor");
4394 return build_unary_op (ADDR_EXPR
, t
, 0);
4396 if (TREE_CODE (t
) == VAR_DECL
)
4397 return build_unary_op (ADDR_EXPR
, t
, 0);
4402 if (TREE_OPERAND (arg
, 0)
4403 && ! is_dummy_object (TREE_OPERAND (arg
, 0))
4404 && TREE_CODE (t
) != FIELD_DECL
)
4406 error ("taking address of bound pointer-to-member expression");
4407 return error_mark_node
;
4409 if (!PTRMEM_OK_P (arg
))
4411 /* This cannot form a pointer to method, so we must
4412 resolve the offset ref, and take the address of the
4413 result. For instance,
4415 arg
= resolve_offset_ref (arg
);
4417 return build_unary_op (code
, arg
, 0);
4420 if (TREE_CODE (TREE_TYPE (t
)) == REFERENCE_TYPE
)
4422 error ("cannot create pointer to reference member `%D'", t
);
4423 return error_mark_node
;
4426 type
= build_ptrmem_type (DECL_FIELD_CONTEXT (t
), TREE_TYPE (t
));
4427 t
= make_ptrmem_cst (type
, TREE_OPERAND (arg
, 1));
4433 /* We permit compiler to make function calls returning
4434 objects of aggregate type look like lvalues. */
4438 if (TREE_CODE (targ
) == SAVE_EXPR
)
4439 targ
= TREE_OPERAND (targ
, 0);
4441 if (TREE_CODE (targ
) == CALL_EXPR
&& IS_AGGR_TYPE (TREE_TYPE (targ
)))
4443 if (TREE_CODE (arg
) == SAVE_EXPR
)
4446 targ
= build_cplus_new (TREE_TYPE (arg
), arg
);
4447 return build1 (ADDR_EXPR
, build_pointer_type (TREE_TYPE (arg
)), targ
);
4450 if (TREE_CODE (arg
) == SAVE_EXPR
&& TREE_CODE (targ
) == INDIRECT_REF
)
4451 return build (SAVE_EXPR
, build_pointer_type (TREE_TYPE (arg
)),
4452 TREE_OPERAND (targ
, 0), current_function_decl
, NULL
);
4455 /* Don't let anything else be handled specially. */
4459 /* Mark EXP saying that we need to be able to take the
4460 address of it; it should not be allocated in a register.
4461 Value is true if successful.
4463 C++: we do not allow `current_class_ptr' to be addressable. */
4466 cxx_mark_addressable (exp
)
4469 register tree x
= exp
;
4472 switch (TREE_CODE (x
))
4479 x
= TREE_OPERAND (x
, 0);
4483 if (x
== current_class_ptr
)
4485 error ("cannot take the address of `this', which is an rvalue expression");
4486 TREE_ADDRESSABLE (x
) = 1; /* so compiler doesn't die later */
4492 /* Caller should not be trying to mark initialized
4493 constant fields addressable. */
4494 my_friendly_assert (DECL_LANG_SPECIFIC (x
) == 0
4495 || DECL_IN_AGGR_P (x
) == 0
4497 || DECL_EXTERNAL (x
), 314);
4502 if (DECL_REGISTER (x
) && !TREE_ADDRESSABLE (x
)
4503 && !DECL_ARTIFICIAL (x
) && extra_warnings
)
4504 warning ("address requested for `%D', which is declared `register'",
4506 TREE_ADDRESSABLE (x
) = 1;
4507 put_var_into_stack (x
);
4511 TREE_ADDRESSABLE (x
) = 1;
4512 TREE_ADDRESSABLE (DECL_ASSEMBLER_NAME (x
)) = 1;
4516 TREE_ADDRESSABLE (x
) = 1;
4520 TREE_ADDRESSABLE (x
) = 1;
4521 cxx_mark_addressable (TREE_OPERAND (x
, 0));
4529 /* Build and return a conditional expression IFEXP ? OP1 : OP2. */
4532 build_x_conditional_expr (ifexp
, op1
, op2
)
4533 tree ifexp
, op1
, op2
;
4535 if (processing_template_decl
)
4536 return build_min_nt (COND_EXPR
, ifexp
, op1
, op2
);
4538 return build_conditional_expr (ifexp
, op1
, op2
);
4541 /* Handle overloading of the ',' operator when needed. Otherwise,
4542 this function just builds an expression list. */
4545 build_x_compound_expr (list
)
4548 tree rest
= TREE_CHAIN (list
);
4551 if (processing_template_decl
)
4552 return build_min_nt (COMPOUND_EXPR
, list
, NULL_TREE
);
4554 if (rest
== NULL_TREE
)
4555 return build_compound_expr (list
);
4557 result
= build_opfncall (COMPOUND_EXPR
, LOOKUP_NORMAL
,
4558 TREE_VALUE (list
), TREE_VALUE (rest
), NULL_TREE
);
4560 return build_x_compound_expr (tree_cons (NULL_TREE
, result
,
4561 TREE_CHAIN (rest
)));
4563 if (! TREE_SIDE_EFFECTS (TREE_VALUE (list
)))
4565 /* FIXME: This test should be in the implicit cast to void of the LHS. */
4566 /* the left-hand operand of a comma expression is like an expression
4567 statement: we should warn if it doesn't have any side-effects,
4568 unless it was explicitly cast to (void). */
4569 if ((extra_warnings
|| warn_unused_value
)
4570 && !(TREE_CODE (TREE_VALUE(list
)) == CONVERT_EXPR
4571 && VOID_TYPE_P (TREE_TYPE (TREE_VALUE(list
)))))
4572 warning("left-hand operand of comma expression has no effect");
4574 #if 0 /* this requires a gcc backend patch to export warn_if_unused_value */
4575 else if (warn_unused_value
)
4576 warn_if_unused_value (TREE_VALUE(list
));
4579 return build_compound_expr
4580 (tree_cons (NULL_TREE
, TREE_VALUE (list
),
4581 build_tree_list (NULL_TREE
,
4582 build_x_compound_expr (rest
))));
4585 /* Given a list of expressions, return a compound expression
4586 that performs them all and returns the value of the last of them. */
4589 build_compound_expr (list
)
4595 TREE_VALUE (list
) = decl_constant_value (TREE_VALUE (list
));
4597 if (TREE_CHAIN (list
) == 0)
4599 /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
4600 Strip such NOP_EXPRs, since LIST is used in non-lvalue context. */
4601 if (TREE_CODE (list
) == NOP_EXPR
4602 && TREE_TYPE (list
) == TREE_TYPE (TREE_OPERAND (list
, 0)))
4603 list
= TREE_OPERAND (list
, 0);
4605 return TREE_VALUE (list
);
4608 first
= TREE_VALUE (list
);
4609 first
= convert_to_void (first
, "left-hand operand of comma");
4610 if (first
== error_mark_node
)
4611 return error_mark_node
;
4613 rest
= build_compound_expr (TREE_CHAIN (list
));
4614 if (rest
== error_mark_node
)
4615 return error_mark_node
;
4617 /* When pedantic, a compound expression cannot be a constant expression. */
4618 if (! TREE_SIDE_EFFECTS (first
) && ! pedantic
)
4621 return build (COMPOUND_EXPR
, TREE_TYPE (rest
), first
, rest
);
4625 build_static_cast (type
, expr
)
4631 if (type
== error_mark_node
|| expr
== error_mark_node
)
4632 return error_mark_node
;
4634 if (TREE_CODE (expr
) == OFFSET_REF
)
4635 expr
= resolve_offset_ref (expr
);
4637 if (processing_template_decl
)
4639 tree t
= build_min (STATIC_CAST_EXPR
, type
, expr
);
4643 /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
4644 Strip such NOP_EXPRs if VALUE is being used in non-lvalue context. */
4645 if (TREE_CODE (type
) != REFERENCE_TYPE
4646 && TREE_CODE (expr
) == NOP_EXPR
4647 && TREE_TYPE (expr
) == TREE_TYPE (TREE_OPERAND (expr
, 0)))
4648 expr
= TREE_OPERAND (expr
, 0);
4650 if (TREE_CODE (type
) == VOID_TYPE
)
4652 expr
= convert_to_void (expr
, /*implicit=*/NULL
);
4656 if (TREE_CODE (type
) == REFERENCE_TYPE
)
4657 return (convert_from_reference
4658 (convert_to_reference (type
, expr
, CONV_STATIC
|CONV_IMPLICIT
,
4659 LOOKUP_COMPLAIN
, NULL_TREE
)));
4661 if (IS_AGGR_TYPE (type
))
4662 return build_cplus_new (type
, (build_special_member_call
4663 (NULL_TREE
, complete_ctor_identifier
,
4664 build_tree_list (NULL_TREE
, expr
),
4665 TYPE_BINFO (type
), LOOKUP_NORMAL
)));
4667 intype
= TREE_TYPE (expr
);
4669 /* FIXME handle casting to array type. */
4672 if (IS_AGGR_TYPE (intype
)
4673 ? can_convert_arg (type
, intype
, expr
)
4674 : can_convert_arg (strip_all_pointer_quals (type
),
4675 strip_all_pointer_quals (intype
), expr
))
4676 /* This is a standard conversion. */
4678 else if (TYPE_PTROB_P (type
) && TYPE_PTROB_P (intype
))
4680 /* They're pointers to objects. They must be aggregates that
4681 are related non-virtually. */
4684 if (IS_AGGR_TYPE (TREE_TYPE (type
)) && IS_AGGR_TYPE (TREE_TYPE (intype
))
4685 && lookup_base (TREE_TYPE (type
), TREE_TYPE (intype
),
4686 ba_ignore
| ba_quiet
, &kind
)
4687 && kind
!= bk_via_virtual
)
4690 else if (TYPE_PTRMEM_P (type
) && TYPE_PTRMEM_P (intype
))
4692 /* They're pointers to members. The pointed to objects must be
4693 the same (ignoring CV qualifiers), and the containing classes
4694 must be related non-virtually. */
4698 (strip_all_pointer_quals (TREE_TYPE (TREE_TYPE (type
))),
4699 strip_all_pointer_quals (TREE_TYPE (TREE_TYPE (intype
))))
4700 && (lookup_base (TYPE_OFFSET_BASETYPE (TREE_TYPE (intype
)),
4701 TYPE_OFFSET_BASETYPE (TREE_TYPE (type
)),
4702 ba_ignore
| ba_quiet
, &kind
))
4703 && kind
!= bk_via_virtual
)
4706 else if (TREE_CODE (intype
) != BOOLEAN_TYPE
4707 && TREE_CODE (type
) != ARRAY_TYPE
4708 && TREE_CODE (type
) != FUNCTION_TYPE
4709 && can_convert (intype
, strip_all_pointer_quals (type
)))
4711 else if (TREE_CODE (intype
) == ENUMERAL_TYPE
4712 && TREE_CODE (type
) == ENUMERAL_TYPE
)
4713 /* DR 128: "A value of integral _or enumeration_ type can be explicitly
4714 converted to an enumeration type."
4715 The integral to enumeration will be accepted by the previous clause.
4716 We need to explicitly check for enumeration to enumeration. */
4719 /* [expr.static.cast]
4721 The static_cast operator shall not be used to cast away
4723 if (ok
&& casts_away_constness (intype
, type
))
4725 error ("static_cast from type `%T' to type `%T' casts away constness",
4727 return error_mark_node
;
4731 return build_c_cast (type
, expr
);
4733 error ("invalid static_cast from type `%T' to type `%T'", intype
, type
);
4734 return error_mark_node
;
4738 build_reinterpret_cast (type
, expr
)
4743 if (type
== error_mark_node
|| expr
== error_mark_node
)
4744 return error_mark_node
;
4746 if (TREE_CODE (expr
) == OFFSET_REF
)
4747 expr
= resolve_offset_ref (expr
);
4749 if (processing_template_decl
)
4751 tree t
= build_min (REINTERPRET_CAST_EXPR
, type
, expr
);
4755 if (TREE_CODE (type
) != REFERENCE_TYPE
)
4757 expr
= decay_conversion (expr
);
4759 /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
4760 Strip such NOP_EXPRs if VALUE is being used in non-lvalue context. */
4761 if (TREE_CODE (expr
) == NOP_EXPR
4762 && TREE_TYPE (expr
) == TREE_TYPE (TREE_OPERAND (expr
, 0)))
4763 expr
= TREE_OPERAND (expr
, 0);
4766 intype
= TREE_TYPE (expr
);
4768 if (TREE_CODE (type
) == REFERENCE_TYPE
)
4770 if (! real_lvalue_p (expr
))
4772 error ("invalid reinterpret_cast of an rvalue expression of type `%T' to type `%T'", intype
, type
);
4773 return error_mark_node
;
4775 expr
= build_unary_op (ADDR_EXPR
, expr
, 0);
4776 if (expr
!= error_mark_node
)
4777 expr
= build_reinterpret_cast
4778 (build_pointer_type (TREE_TYPE (type
)), expr
);
4779 if (expr
!= error_mark_node
)
4780 expr
= build_indirect_ref (expr
, 0);
4783 else if (same_type_ignoring_top_level_qualifiers_p (intype
, type
))
4784 return build_static_cast (type
, expr
);
4786 if (TYPE_PTR_P (type
) && (TREE_CODE (intype
) == INTEGER_TYPE
4787 || TREE_CODE (intype
) == ENUMERAL_TYPE
))
4789 else if (TREE_CODE (type
) == INTEGER_TYPE
&& TYPE_PTR_P (intype
))
4791 if (TYPE_PRECISION (type
) < TYPE_PRECISION (intype
))
4792 pedwarn ("reinterpret_cast from `%T' to `%T' loses precision",
4795 else if ((TYPE_PTRFN_P (type
) && TYPE_PTRFN_P (intype
))
4796 || (TYPE_PTRMEMFUNC_P (type
) && TYPE_PTRMEMFUNC_P (intype
)))
4798 expr
= decl_constant_value (expr
);
4799 return fold (build1 (NOP_EXPR
, type
, expr
));
4801 else if ((TYPE_PTRMEM_P (type
) && TYPE_PTRMEM_P (intype
))
4802 || (TYPE_PTROBV_P (type
) && TYPE_PTROBV_P (intype
)))
4804 if (! comp_ptr_ttypes_reinterpret (TREE_TYPE (type
), TREE_TYPE (intype
)))
4805 pedwarn ("reinterpret_cast from `%T' to `%T' casts away const (or volatile)",
4808 expr
= decl_constant_value (expr
);
4809 return fold (build1 (NOP_EXPR
, type
, expr
));
4811 else if ((TYPE_PTRFN_P (type
) && TYPE_PTROBV_P (intype
))
4812 || (TYPE_PTRFN_P (intype
) && TYPE_PTROBV_P (type
)))
4814 pedwarn ("ISO C++ forbids casting between pointer-to-function and pointer-to-object");
4815 expr
= decl_constant_value (expr
);
4816 return fold (build1 (NOP_EXPR
, type
, expr
));
4820 error ("invalid reinterpret_cast from type `%T' to type `%T'",
4822 return error_mark_node
;
4825 return cp_convert (type
, expr
);
4829 build_const_cast (type
, expr
)
4834 if (type
== error_mark_node
|| expr
== error_mark_node
)
4835 return error_mark_node
;
4837 if (TREE_CODE (expr
) == OFFSET_REF
)
4838 expr
= resolve_offset_ref (expr
);
4840 if (processing_template_decl
)
4842 tree t
= build_min (CONST_CAST_EXPR
, type
, expr
);
4846 if (!POINTER_TYPE_P (type
))
4847 error ("invalid use of const_cast with type `%T', which is not a pointer, reference, nor a pointer-to-data-member type", type
);
4848 else if (TREE_CODE (TREE_TYPE (type
)) == FUNCTION_TYPE
)
4850 error ("invalid use of const_cast with type `%T', which is a pointer or reference to a function type", type
);
4851 return error_mark_node
;
4854 if (TREE_CODE (type
) != REFERENCE_TYPE
)
4856 expr
= decay_conversion (expr
);
4858 /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
4859 Strip such NOP_EXPRs if VALUE is being used in non-lvalue context. */
4860 if (TREE_CODE (expr
) == NOP_EXPR
4861 && TREE_TYPE (expr
) == TREE_TYPE (TREE_OPERAND (expr
, 0)))
4862 expr
= TREE_OPERAND (expr
, 0);
4865 intype
= TREE_TYPE (expr
);
4867 if (same_type_ignoring_top_level_qualifiers_p (intype
, type
))
4868 return build_static_cast (type
, expr
);
4869 else if (TREE_CODE (type
) == REFERENCE_TYPE
)
4871 if (! real_lvalue_p (expr
))
4873 error ("invalid const_cast of an rvalue of type `%T' to type `%T'", intype
, type
);
4874 return error_mark_node
;
4877 if (comp_ptr_ttypes_const (TREE_TYPE (type
), intype
))
4879 expr
= build_unary_op (ADDR_EXPR
, expr
, 0);
4880 expr
= build1 (NOP_EXPR
, type
, expr
);
4881 return convert_from_reference (expr
);
4884 else if (TREE_CODE (type
) == POINTER_TYPE
4885 && TREE_CODE (intype
) == POINTER_TYPE
4886 && comp_ptr_ttypes_const (TREE_TYPE (type
), TREE_TYPE (intype
)))
4887 return cp_convert (type
, expr
);
4889 error ("invalid const_cast from type `%T' to type `%T'", intype
, type
);
4890 return error_mark_node
;
4893 /* Build an expression representing a cast to type TYPE of expression EXPR.
4895 ALLOW_NONCONVERTING is true if we should allow non-converting constructors
4896 when doing the cast. */
4899 build_c_cast (type
, expr
)
4902 register tree value
= expr
;
4905 if (type
== error_mark_node
|| expr
== error_mark_node
)
4906 return error_mark_node
;
4908 if (processing_template_decl
)
4910 tree t
= build_min (CAST_EXPR
, type
,
4911 tree_cons (NULL_TREE
, value
, NULL_TREE
));
4915 /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
4916 Strip such NOP_EXPRs if VALUE is being used in non-lvalue context. */
4917 if (TREE_CODE (type
) != REFERENCE_TYPE
4918 && TREE_CODE (value
) == NOP_EXPR
4919 && TREE_TYPE (value
) == TREE_TYPE (TREE_OPERAND (value
, 0)))
4920 value
= TREE_OPERAND (value
, 0);
4922 if (TREE_CODE (value
) == OFFSET_REF
)
4923 value
= resolve_offset_ref (value
);
4925 if (TREE_CODE (type
) == ARRAY_TYPE
)
4927 /* Allow casting from T1* to T2[] because Cfront allows it.
4928 NIHCL uses it. It is not valid ISO C++ however. */
4929 if (TREE_CODE (TREE_TYPE (expr
)) == POINTER_TYPE
)
4931 pedwarn ("ISO C++ forbids casting to an array type `%T'", type
);
4932 type
= build_pointer_type (TREE_TYPE (type
));
4936 error ("ISO C++ forbids casting to an array type `%T'", type
);
4937 return error_mark_node
;
4941 if (TREE_CODE (type
) == FUNCTION_TYPE
4942 || TREE_CODE (type
) == METHOD_TYPE
)
4944 error ("invalid cast to function type `%T'", type
);
4945 return error_mark_node
;
4948 if (TREE_CODE (type
) == VOID_TYPE
)
4950 /* Conversion to void does not cause any of the normal function to
4951 * pointer, array to pointer and lvalue to rvalue decays. */
4953 value
= convert_to_void (value
, /*implicit=*/NULL
);
4956 /* Convert functions and arrays to pointers and
4957 convert references to their expanded types,
4958 but don't convert any other types. If, however, we are
4959 casting to a class type, there's no reason to do this: the
4960 cast will only succeed if there is a converting constructor,
4961 and the default conversions will be done at that point. In
4962 fact, doing the default conversion here is actually harmful
4966 struct S { S(const A&); };
4968 since we don't want the array-to-pointer conversion done. */
4969 if (!IS_AGGR_TYPE (type
))
4971 if (TREE_CODE (TREE_TYPE (value
)) == FUNCTION_TYPE
4972 || (TREE_CODE (TREE_TYPE (value
)) == METHOD_TYPE
4973 /* Don't do the default conversion on a ->* expression. */
4974 && ! (TREE_CODE (type
) == POINTER_TYPE
4975 && bound_pmf_p (value
)))
4976 || TREE_CODE (TREE_TYPE (value
)) == ARRAY_TYPE
4977 || TREE_CODE (TREE_TYPE (value
)) == REFERENCE_TYPE
)
4978 value
= default_conversion (value
);
4980 else if (TREE_CODE (TREE_TYPE (value
)) == REFERENCE_TYPE
)
4981 /* However, even for class types, we still need to strip away
4982 the reference type, since the call to convert_force below
4983 does not expect the input expression to be of reference
4985 value
= convert_from_reference (value
);
4987 otype
= TREE_TYPE (value
);
4989 /* Optionally warn about potentially worrisome casts. */
4992 && TREE_CODE (type
) == POINTER_TYPE
4993 && TREE_CODE (otype
) == POINTER_TYPE
4994 && !at_least_as_qualified_p (TREE_TYPE (type
),
4996 warning ("cast from `%T' to `%T' discards qualifiers from pointer target type",
4999 if (TREE_CODE (type
) == INTEGER_TYPE
5000 && TREE_CODE (otype
) == POINTER_TYPE
5001 && TYPE_PRECISION (type
) != TYPE_PRECISION (otype
))
5002 warning ("cast from pointer to integer of different size");
5004 if (TREE_CODE (type
) == POINTER_TYPE
5005 && TREE_CODE (otype
) == INTEGER_TYPE
5006 && TYPE_PRECISION (type
) != TYPE_PRECISION (otype
)
5007 /* Don't warn about converting any constant. */
5008 && !TREE_CONSTANT (value
))
5009 warning ("cast to pointer from integer of different size");
5011 if (TREE_CODE (type
) == REFERENCE_TYPE
)
5012 value
= (convert_from_reference
5013 (convert_to_reference (type
, value
, CONV_C_CAST
,
5014 LOOKUP_COMPLAIN
, NULL_TREE
)));
5019 value
= decl_constant_value (value
);
5022 value
= convert_force (type
, value
, CONV_C_CAST
);
5024 /* Ignore any integer overflow caused by the cast. */
5025 if (TREE_CODE (value
) == INTEGER_CST
)
5027 TREE_OVERFLOW (value
) = TREE_OVERFLOW (ovalue
);
5028 TREE_CONSTANT_OVERFLOW (value
) = TREE_CONSTANT_OVERFLOW (ovalue
);
5032 /* Warn about possible alignment problems. Do this here when we will have
5033 instantiated any necessary template types. */
5034 if (STRICT_ALIGNMENT
&& warn_cast_align
5035 && TREE_CODE (type
) == POINTER_TYPE
5036 && TREE_CODE (otype
) == POINTER_TYPE
5037 && TREE_CODE (TREE_TYPE (otype
)) != VOID_TYPE
5038 && TREE_CODE (TREE_TYPE (otype
)) != FUNCTION_TYPE
5039 && COMPLETE_TYPE_P (TREE_TYPE (otype
))
5040 && COMPLETE_TYPE_P (TREE_TYPE (type
))
5041 && TYPE_ALIGN (TREE_TYPE (type
)) > TYPE_ALIGN (TREE_TYPE (otype
)))
5042 warning ("cast from `%T' to `%T' increases required alignment of target type",
5045 /* Always produce some operator for an explicit cast,
5046 so we can tell (for -pedantic) that the cast is no lvalue. */
5047 if (TREE_CODE (type
) != REFERENCE_TYPE
&& value
== expr
5048 && real_lvalue_p (value
))
5049 value
= non_lvalue (value
);
5054 /* Build an assignment expression of lvalue LHS from value RHS.
5055 MODIFYCODE is the code for a binary operator that we use
5056 to combine the old value of LHS with RHS to get the new value.
5057 Or else MODIFYCODE is NOP_EXPR meaning do a simple assignment.
5059 C++: If MODIFYCODE is INIT_EXPR, then leave references unbashed. */
5062 build_modify_expr (lhs
, modifycode
, rhs
)
5064 enum tree_code modifycode
;
5067 register tree result
;
5069 tree lhstype
= TREE_TYPE (lhs
);
5070 tree olhstype
= lhstype
;
5073 /* Avoid duplicate error messages from operands that had errors. */
5074 if (lhs
== error_mark_node
|| rhs
== error_mark_node
)
5075 return error_mark_node
;
5077 /* Handle control structure constructs used as "lvalues". */
5078 switch (TREE_CODE (lhs
))
5080 /* Handle --foo = 5; as these are valid constructs in C++ */
5081 case PREDECREMENT_EXPR
:
5082 case PREINCREMENT_EXPR
:
5083 if (TREE_SIDE_EFFECTS (TREE_OPERAND (lhs
, 0)))
5084 lhs
= build (TREE_CODE (lhs
), TREE_TYPE (lhs
),
5085 stabilize_reference (TREE_OPERAND (lhs
, 0)),
5086 TREE_OPERAND (lhs
, 1));
5087 return build (COMPOUND_EXPR
, lhstype
,
5089 build_modify_expr (TREE_OPERAND (lhs
, 0),
5092 /* Handle (a, b) used as an "lvalue". */
5094 newrhs
= build_modify_expr (TREE_OPERAND (lhs
, 1),
5096 if (newrhs
== error_mark_node
)
5097 return error_mark_node
;
5098 return build (COMPOUND_EXPR
, lhstype
,
5099 TREE_OPERAND (lhs
, 0), newrhs
);
5102 newrhs
= build_modify_expr (TREE_OPERAND (lhs
, 0), modifycode
, rhs
);
5103 if (newrhs
== error_mark_node
)
5104 return error_mark_node
;
5105 return build (COMPOUND_EXPR
, lhstype
, lhs
, newrhs
);
5107 /* Handle (a ? b : c) used as an "lvalue". */
5110 /* Produce (a ? (b = rhs) : (c = rhs))
5111 except that the RHS goes through a save-expr
5112 so the code to compute it is only emitted once. */
5114 tree preeval
= NULL_TREE
;
5116 rhs
= stabilize_expr (rhs
, &preeval
);
5118 /* Check this here to avoid odd errors when trying to convert
5119 a throw to the type of the COND_EXPR. */
5120 if (!lvalue_or_else (lhs
, "assignment"))
5121 return error_mark_node
;
5123 cond
= build_conditional_expr
5124 (TREE_OPERAND (lhs
, 0),
5125 build_modify_expr (cp_convert (TREE_TYPE (lhs
),
5126 TREE_OPERAND (lhs
, 1)),
5128 build_modify_expr (cp_convert (TREE_TYPE (lhs
),
5129 TREE_OPERAND (lhs
, 2)),
5132 if (cond
== error_mark_node
)
5134 /* Make sure the code to compute the rhs comes out
5135 before the split. */
5136 return build (COMPOUND_EXPR
, TREE_TYPE (lhs
), preeval
, cond
);
5140 lhs
= resolve_offset_ref (lhs
);
5141 if (lhs
== error_mark_node
)
5142 return error_mark_node
;
5143 olhstype
= lhstype
= TREE_TYPE (lhs
);
5149 if (modifycode
== INIT_EXPR
)
5151 if (TREE_CODE (rhs
) == CONSTRUCTOR
)
5153 my_friendly_assert (same_type_p (TREE_TYPE (rhs
), lhstype
),
5155 result
= build (INIT_EXPR
, lhstype
, lhs
, rhs
);
5156 TREE_SIDE_EFFECTS (result
) = 1;
5159 else if (! IS_AGGR_TYPE (lhstype
))
5160 /* Do the default thing */;
5163 result
= build_special_member_call (lhs
, complete_ctor_identifier
,
5164 build_tree_list (NULL_TREE
, rhs
),
5165 TYPE_BINFO (lhstype
),
5167 if (result
== NULL_TREE
)
5168 return error_mark_node
;
5174 if (TREE_CODE (lhstype
) == REFERENCE_TYPE
)
5176 lhs
= convert_from_reference (lhs
);
5177 olhstype
= lhstype
= TREE_TYPE (lhs
);
5179 lhs
= require_complete_type (lhs
);
5180 if (lhs
== error_mark_node
)
5181 return error_mark_node
;
5183 if (modifycode
== NOP_EXPR
)
5185 /* `operator=' is not an inheritable operator. */
5186 if (! IS_AGGR_TYPE (lhstype
))
5187 /* Do the default thing */;
5190 result
= build_opfncall (MODIFY_EXPR
, LOOKUP_NORMAL
,
5191 lhs
, rhs
, make_node (NOP_EXPR
));
5192 if (result
== NULL_TREE
)
5193 return error_mark_node
;
5200 /* A binary op has been requested. Combine the old LHS
5201 value with the RHS producing the value we should actually
5202 store into the LHS. */
5204 my_friendly_assert (!PROMOTES_TO_AGGR_TYPE (lhstype
, REFERENCE_TYPE
),
5206 lhs
= stabilize_reference (lhs
);
5207 newrhs
= cp_build_binary_op (modifycode
, lhs
, rhs
);
5208 if (newrhs
== error_mark_node
)
5210 error (" in evaluation of `%Q(%#T, %#T)'", modifycode
,
5211 TREE_TYPE (lhs
), TREE_TYPE (rhs
));
5212 return error_mark_node
;
5215 /* Now it looks like a plain assignment. */
5216 modifycode
= NOP_EXPR
;
5218 my_friendly_assert (TREE_CODE (lhstype
) != REFERENCE_TYPE
, 20011220);
5219 my_friendly_assert (TREE_CODE (TREE_TYPE (newrhs
)) != REFERENCE_TYPE
,
5223 /* Handle a cast used as an "lvalue".
5224 We have already performed any binary operator using the value as cast.
5225 Now convert the result to the cast type of the lhs,
5226 and then true type of the lhs and store it there;
5227 then convert result back to the cast type to be the value
5228 of the assignment. */
5230 switch (TREE_CODE (lhs
))
5235 case FIX_TRUNC_EXPR
:
5236 case FIX_FLOOR_EXPR
:
5237 case FIX_ROUND_EXPR
:
5240 tree inner_lhs
= TREE_OPERAND (lhs
, 0);
5243 if (TREE_CODE (TREE_TYPE (newrhs
)) == ARRAY_TYPE
5244 || TREE_CODE (TREE_TYPE (newrhs
)) == FUNCTION_TYPE
5245 || TREE_CODE (TREE_TYPE (newrhs
)) == METHOD_TYPE
5246 || TREE_CODE (TREE_TYPE (newrhs
)) == OFFSET_TYPE
)
5247 newrhs
= default_conversion (newrhs
);
5249 /* ISO C++ 5.4/1: The result is an lvalue if T is a reference
5250 type, otherwise the result is an rvalue. */
5251 if (! lvalue_p (lhs
))
5252 pedwarn ("ISO C++ forbids cast to non-reference type used as lvalue");
5254 result
= build_modify_expr (inner_lhs
, NOP_EXPR
,
5255 cp_convert (TREE_TYPE (inner_lhs
),
5256 cp_convert (lhstype
, newrhs
)));
5257 if (result
== error_mark_node
)
5259 return cp_convert (TREE_TYPE (lhs
), result
);
5266 /* Now we have handled acceptable kinds of LHS that are not truly lvalues.
5267 Reject anything strange now. */
5269 if (!lvalue_or_else (lhs
, "assignment"))
5270 return error_mark_node
;
5272 /* Warn about modifying something that is `const'. Don't warn if
5273 this is initialization. */
5274 if (modifycode
!= INIT_EXPR
5275 && (TREE_READONLY (lhs
) || CP_TYPE_CONST_P (lhstype
)
5276 /* Functions are not modifiable, even though they are
5278 || TREE_CODE (TREE_TYPE (lhs
)) == FUNCTION_TYPE
5279 || TREE_CODE (TREE_TYPE (lhs
)) == METHOD_TYPE
5280 /* If it's an aggregate and any field is const, then it is
5281 effectively const. */
5282 || (IS_AGGR_TYPE_CODE (TREE_CODE (lhstype
))
5283 && C_TYPE_FIELDS_READONLY (lhstype
))))
5284 readonly_error (lhs
, "assignment", 0);
5286 /* If storing into a structure or union member, it has probably been
5287 given type `int'. Compute the type that would go with the actual
5288 amount of storage the member occupies. */
5290 if (TREE_CODE (lhs
) == COMPONENT_REF
5291 && (TREE_CODE (lhstype
) == INTEGER_TYPE
5292 || TREE_CODE (lhstype
) == REAL_TYPE
5293 || TREE_CODE (lhstype
) == ENUMERAL_TYPE
))
5295 lhstype
= TREE_TYPE (get_unwidened (lhs
, 0));
5297 /* If storing in a field that is in actuality a short or narrower
5298 than one, we must store in the field in its actual type. */
5300 if (lhstype
!= TREE_TYPE (lhs
))
5302 lhs
= copy_node (lhs
);
5303 TREE_TYPE (lhs
) = lhstype
;
5307 if (TREE_CODE (lhstype
) != REFERENCE_TYPE
)
5309 if (TREE_SIDE_EFFECTS (lhs
))
5310 lhs
= stabilize_reference (lhs
);
5311 if (TREE_SIDE_EFFECTS (newrhs
))
5312 newrhs
= stabilize_reference (newrhs
);
5315 /* Convert new value to destination type. */
5317 if (TREE_CODE (lhstype
) == ARRAY_TYPE
)
5321 if (!same_or_base_type_p (TYPE_MAIN_VARIANT (lhstype
),
5322 TYPE_MAIN_VARIANT (TREE_TYPE (rhs
))))
5324 error ("incompatible types in assignment of `%T' to `%T'",
5325 TREE_TYPE (rhs
), lhstype
);
5326 return error_mark_node
;
5329 /* Allow array assignment in compiler-generated code. */
5330 if (! DECL_ARTIFICIAL (current_function_decl
))
5331 pedwarn ("ISO C++ forbids assignment of arrays");
5333 from_array
= TREE_CODE (TREE_TYPE (newrhs
)) == ARRAY_TYPE
5334 ? 1 + (modifycode
!= INIT_EXPR
): 0;
5335 return build_vec_init (lhs
, newrhs
, from_array
);
5338 if (modifycode
== INIT_EXPR
)
5339 newrhs
= convert_for_initialization (lhs
, lhstype
, newrhs
, LOOKUP_NORMAL
,
5340 "initialization", NULL_TREE
, 0);
5343 /* Avoid warnings on enum bit fields. */
5344 if (TREE_CODE (olhstype
) == ENUMERAL_TYPE
5345 && TREE_CODE (lhstype
) == INTEGER_TYPE
)
5347 newrhs
= convert_for_assignment (olhstype
, newrhs
, "assignment",
5349 newrhs
= convert_force (lhstype
, newrhs
, 0);
5352 newrhs
= convert_for_assignment (lhstype
, newrhs
, "assignment",
5354 if (TREE_CODE (newrhs
) == CALL_EXPR
5355 && TYPE_NEEDS_CONSTRUCTING (lhstype
))
5356 newrhs
= build_cplus_new (lhstype
, newrhs
);
5358 /* Can't initialize directly from a TARGET_EXPR, since that would
5359 cause the lhs to be constructed twice, and possibly result in
5360 accidental self-initialization. So we force the TARGET_EXPR to be
5361 expanded without a target. */
5362 if (TREE_CODE (newrhs
) == TARGET_EXPR
)
5363 newrhs
= build (COMPOUND_EXPR
, TREE_TYPE (newrhs
), newrhs
,
5364 TREE_OPERAND (newrhs
, 0));
5367 if (newrhs
== error_mark_node
)
5368 return error_mark_node
;
5370 if (TREE_CODE (newrhs
) == COND_EXPR
)
5373 tree cond
= TREE_OPERAND (newrhs
, 0);
5375 if (TREE_SIDE_EFFECTS (lhs
))
5376 cond
= build_compound_expr (tree_cons
5378 build_tree_list (NULL_TREE
, cond
)));
5380 /* Cannot have two identical lhs on this one tree (result) as preexpand
5381 calls will rip them out and fill in RTL for them, but when the
5382 rtl is generated, the calls will only be in the first side of the
5383 condition, not on both, or before the conditional jump! (mrs) */
5384 lhs1
= break_out_calls (lhs
);
5387 /* If there's no change, the COND_EXPR behaves like any other rhs. */
5388 result
= build (modifycode
== NOP_EXPR
? MODIFY_EXPR
: INIT_EXPR
,
5389 lhstype
, lhs
, newrhs
);
5392 tree result_type
= TREE_TYPE (newrhs
);
5393 /* We have to convert each arm to the proper type because the
5394 types may have been munged by constant folding. */
5396 = build (COND_EXPR
, result_type
, cond
,
5397 build_modify_expr (lhs
, modifycode
,
5398 cp_convert (result_type
,
5399 TREE_OPERAND (newrhs
, 1))),
5400 build_modify_expr (lhs1
, modifycode
,
5401 cp_convert (result_type
,
5402 TREE_OPERAND (newrhs
, 2))));
5406 result
= build (modifycode
== NOP_EXPR
? MODIFY_EXPR
: INIT_EXPR
,
5407 lhstype
, lhs
, newrhs
);
5409 TREE_SIDE_EFFECTS (result
) = 1;
5411 /* If we got the LHS in a different type for storing in,
5412 convert the result back to the nominal type of LHS
5413 so that the value we return always has the same type
5414 as the LHS argument. */
5416 if (olhstype
== TREE_TYPE (result
))
5418 /* Avoid warnings converting integral types back into enums
5419 for enum bit fields. */
5420 if (TREE_CODE (TREE_TYPE (result
)) == INTEGER_TYPE
5421 && TREE_CODE (olhstype
) == ENUMERAL_TYPE
)
5423 result
= build (COMPOUND_EXPR
, olhstype
, result
, olhs
);
5424 TREE_NO_UNUSED_WARNING (result
) = 1;
5427 return convert_for_assignment (olhstype
, result
, "assignment",
5432 build_x_modify_expr (lhs
, modifycode
, rhs
)
5434 enum tree_code modifycode
;
5437 if (processing_template_decl
)
5438 return build_min_nt (MODOP_EXPR
, lhs
,
5439 build_min_nt (modifycode
, NULL_TREE
, NULL_TREE
), rhs
);
5441 if (modifycode
!= NOP_EXPR
)
5443 tree rval
= build_opfncall (MODIFY_EXPR
, LOOKUP_NORMAL
, lhs
, rhs
,
5444 make_node (modifycode
));
5448 return build_modify_expr (lhs
, modifycode
, rhs
);
5452 /* Get difference in deltas for different pointer to member function
5453 types. Return integer_zero_node, if FROM cannot be converted to a
5454 TO type. If FORCE is true, then allow reverse conversions as well.
5456 Note that the naming of FROM and TO is kind of backwards; the return
5457 value is what we add to a TO in order to get a FROM. They are named
5458 this way because we call this function to find out how to convert from
5459 a pointer to member of FROM to a pointer to member of TO. */
5462 get_delta_difference (from
, to
, force
)
5466 tree delta
= integer_zero_node
;
5471 binfo
= lookup_base (to
, from
, ba_check
, &kind
);
5472 if (kind
== bk_inaccessible
|| kind
== bk_ambig
)
5474 error (" in pointer to member function conversion");
5481 error_not_base_type (from
, to
);
5482 error (" in pointer to member conversion");
5485 binfo
= lookup_base (from
, to
, ba_check
, &kind
);
5488 virt_binfo
= binfo_from_vbase (binfo
);
5492 /* This is a reinterpret cast, we choose to do nothing. */
5493 warning ("pointer to member cast via virtual base `%T' of `%T'",
5494 BINFO_TYPE (virt_binfo
),
5495 BINFO_TYPE (BINFO_INHERITANCE_CHAIN (virt_binfo
)));
5498 delta
= BINFO_OFFSET (binfo
);
5499 delta
= cp_convert (ptrdiff_type_node
, delta
);
5500 delta
= cp_build_binary_op (MINUS_EXPR
,
5507 virt_binfo
= binfo_from_vbase (binfo
);
5510 /* This is a reinterpret cast, we choose to do nothing. */
5512 warning ("pointer to member cast via virtual base `%T' of `%T'",
5513 BINFO_TYPE (virt_binfo
),
5514 BINFO_TYPE (BINFO_INHERITANCE_CHAIN (virt_binfo
)));
5516 error ("pointer to member conversion via virtual base `%T' of `%T'",
5517 BINFO_TYPE (virt_binfo
),
5518 BINFO_TYPE (BINFO_INHERITANCE_CHAIN (virt_binfo
)));
5521 delta
= BINFO_OFFSET (binfo
);
5523 return cp_convert (ptrdiff_type_node
, delta
);
5526 /* Return a constructor for the pointer-to-member-function TYPE using
5527 the other components as specified. */
5530 build_ptrmemfunc1 (type
, delta
, pfn
)
5531 tree type
, delta
, pfn
;
5537 /* Pull the FIELD_DECLs out of the type. */
5538 pfn_field
= TYPE_FIELDS (type
);
5539 delta_field
= TREE_CHAIN (pfn_field
);
5541 /* Make sure DELTA has the type we want. */
5542 delta
= convert_and_check (delta_type_node
, delta
);
5544 /* Finish creating the initializer. */
5545 u
= tree_cons (pfn_field
, pfn
,
5546 build_tree_list (delta_field
, delta
));
5547 u
= build (CONSTRUCTOR
, type
, NULL_TREE
, u
);
5548 TREE_CONSTANT (u
) = TREE_CONSTANT (pfn
) && TREE_CONSTANT (delta
);
5549 TREE_STATIC (u
) = (TREE_CONSTANT (u
)
5550 && (initializer_constant_valid_p (pfn
, TREE_TYPE (pfn
))
5552 && (initializer_constant_valid_p (delta
, TREE_TYPE (delta
))
5557 /* Build a constructor for a pointer to member function. It can be
5558 used to initialize global variables, local variable, or used
5559 as a value in expressions. TYPE is the POINTER to METHOD_TYPE we
5562 If FORCE is non-zero, then force this conversion, even if
5563 we would rather not do it. Usually set when using an explicit
5566 Return error_mark_node, if something goes wrong. */
5569 build_ptrmemfunc (type
, pfn
, force
)
5574 tree pfn_type
= TREE_TYPE (pfn
);
5575 tree to_type
= build_ptrmemfunc_type (type
);
5577 /* Handle multiple conversions of pointer to member functions. */
5578 if (TYPE_PTRMEMFUNC_P (TREE_TYPE (pfn
)))
5580 tree delta
= NULL_TREE
;
5581 tree npfn
= NULL_TREE
;
5585 && !can_convert_arg (to_type
, TREE_TYPE (pfn
), pfn
))
5586 error ("invalid conversion to type `%T' from type `%T'",
5589 n
= get_delta_difference (TYPE_PTRMEMFUNC_OBJECT_TYPE (pfn_type
),
5590 TYPE_PTRMEMFUNC_OBJECT_TYPE (to_type
),
5593 /* We don't have to do any conversion to convert a
5594 pointer-to-member to its own type. But, we don't want to
5595 just return a PTRMEM_CST if there's an explicit cast; that
5596 cast should make the expression an invalid template argument. */
5597 if (TREE_CODE (pfn
) != PTRMEM_CST
)
5599 if (same_type_p (to_type
, pfn_type
))
5601 else if (integer_zerop (n
))
5602 return build_reinterpret_cast (to_type
, pfn
);
5605 if (TREE_SIDE_EFFECTS (pfn
))
5606 pfn
= save_expr (pfn
);
5608 /* Obtain the function pointer and the current DELTA. */
5609 if (TREE_CODE (pfn
) == PTRMEM_CST
)
5610 expand_ptrmemfunc_cst (pfn
, &delta
, &npfn
);
5613 npfn
= build_ptrmemfunc_access_expr (pfn
, pfn_identifier
);
5614 delta
= build_ptrmemfunc_access_expr (pfn
, delta_identifier
);
5617 /* Just adjust the DELTA field. */
5618 delta
= cp_convert (ptrdiff_type_node
, delta
);
5619 if (TARGET_PTRMEMFUNC_VBIT_LOCATION
== ptrmemfunc_vbit_in_delta
)
5620 n
= cp_build_binary_op (LSHIFT_EXPR
, n
, integer_one_node
);
5621 delta
= cp_build_binary_op (PLUS_EXPR
, delta
, n
);
5622 return build_ptrmemfunc1 (to_type
, delta
, npfn
);
5625 /* Handle null pointer to member function conversions. */
5626 if (integer_zerop (pfn
))
5628 pfn
= build_c_cast (type
, integer_zero_node
);
5629 return build_ptrmemfunc1 (to_type
,
5634 if (type_unknown_p (pfn
))
5635 return instantiate_type (type
, pfn
, tf_error
| tf_warning
);
5637 fn
= TREE_OPERAND (pfn
, 0);
5638 my_friendly_assert (TREE_CODE (fn
) == FUNCTION_DECL
, 0);
5639 return make_ptrmem_cst (to_type
, fn
);
5642 /* Return the DELTA, IDX, PFN, and DELTA2 values for the PTRMEM_CST
5645 ??? There is no consistency as to the types returned for the above
5646 values. Some code acts as if its a sizetype and some as if its
5647 integer_type_node. */
5650 expand_ptrmemfunc_cst (cst
, delta
, pfn
)
5655 tree type
= TREE_TYPE (cst
);
5656 tree fn
= PTRMEM_CST_MEMBER (cst
);
5657 tree ptr_class
, fn_class
;
5659 my_friendly_assert (TREE_CODE (fn
) == FUNCTION_DECL
, 0);
5661 /* The class that the function belongs to. */
5662 fn_class
= DECL_CONTEXT (fn
);
5664 /* The class that we're creating a pointer to member of. */
5665 ptr_class
= TYPE_PTRMEMFUNC_OBJECT_TYPE (type
);
5667 /* First, calculate the adjustment to the function's class. */
5668 *delta
= get_delta_difference (fn_class
, ptr_class
, /*force=*/0);
5670 if (!DECL_VIRTUAL_P (fn
))
5671 *pfn
= convert (TYPE_PTRMEMFUNC_FN_TYPE (type
), build_addr_func (fn
));
5674 /* If we're dealing with a virtual function, we have to adjust 'this'
5675 again, to point to the base which provides the vtable entry for
5676 fn; the call will do the opposite adjustment. */
5677 tree orig_class
= DECL_VIRTUAL_CONTEXT (fn
);
5678 tree binfo
= binfo_or_else (orig_class
, fn_class
);
5679 *delta
= fold (build (PLUS_EXPR
, TREE_TYPE (*delta
),
5680 *delta
, BINFO_OFFSET (binfo
)));
5682 /* We set PFN to the vtable offset at which the function can be
5683 found, plus one (unless ptrmemfunc_vbit_in_delta, in which
5684 case delta is shifted left, and then incremented). */
5685 *pfn
= DECL_VINDEX (fn
);
5686 *pfn
= fold (build (MULT_EXPR
, integer_type_node
, *pfn
,
5687 TYPE_SIZE_UNIT (vtable_entry_type
)));
5689 switch (TARGET_PTRMEMFUNC_VBIT_LOCATION
)
5691 case ptrmemfunc_vbit_in_pfn
:
5692 *pfn
= fold (build (PLUS_EXPR
, integer_type_node
, *pfn
,
5696 case ptrmemfunc_vbit_in_delta
:
5697 *delta
= fold (build (LSHIFT_EXPR
, TREE_TYPE (*delta
),
5698 *delta
, integer_one_node
));
5699 *delta
= fold (build (PLUS_EXPR
, TREE_TYPE (*delta
),
5700 *delta
, integer_one_node
));
5707 *pfn
= fold (build1 (NOP_EXPR
, TYPE_PTRMEMFUNC_FN_TYPE (type
),
5712 /* Return an expression for PFN from the pointer-to-member function
5716 pfn_from_ptrmemfunc (t
)
5719 if (TREE_CODE (t
) == PTRMEM_CST
)
5724 expand_ptrmemfunc_cst (t
, &delta
, &pfn
);
5729 return build_ptrmemfunc_access_expr (t
, pfn_identifier
);
5732 /* Expression EXPR is about to be implicitly converted to TYPE. Warn
5733 if this is a potentially dangerous thing to do. Returns a possibly
5737 dubious_conversion_warnings (type
, expr
, errtype
, fndecl
, parmnum
)
5740 const char *errtype
;
5744 if (TREE_CODE (type
) == REFERENCE_TYPE
)
5745 type
= TREE_TYPE (type
);
5747 /* Issue warnings about peculiar, but valid, uses of NULL. */
5748 if (ARITHMETIC_TYPE_P (type
) && expr
== null_node
)
5751 warning ("passing NULL used for non-pointer %s %P of `%D'",
5752 errtype
, parmnum
, fndecl
);
5754 warning ("%s to non-pointer type `%T' from NULL", errtype
, type
);
5757 /* Warn about assigning a floating-point type to an integer type. */
5758 if (TREE_CODE (TREE_TYPE (expr
)) == REAL_TYPE
5759 && TREE_CODE (type
) == INTEGER_TYPE
)
5762 warning ("passing `%T' for %s %P of `%D'",
5763 TREE_TYPE (expr
), errtype
, parmnum
, fndecl
);
5765 warning ("%s to `%T' from `%T'", errtype
, type
, TREE_TYPE (expr
));
5767 /* And warn about assigning a negative value to an unsigned
5769 else if (TREE_UNSIGNED (type
) && TREE_CODE (type
) != BOOLEAN_TYPE
)
5771 if (TREE_CODE (expr
) == INTEGER_CST
5772 && TREE_NEGATED_INT (expr
))
5775 warning ("passing negative value `%E' for %s %P of `%D'",
5776 expr
, errtype
, parmnum
, fndecl
);
5778 warning ("%s of negative value `%E' to `%T'",
5779 errtype
, expr
, type
);
5782 overflow_warning (expr
);
5784 if (TREE_CONSTANT (expr
))
5790 /* Convert value RHS to type TYPE as preparation for an assignment to
5791 an lvalue of type TYPE. ERRTYPE is a string to use in error
5792 messages: "assignment", "return", etc. If FNDECL is non-NULL, we
5793 are doing the conversion in order to pass the PARMNUMth argument of
5797 convert_for_assignment (type
, rhs
, errtype
, fndecl
, parmnum
)
5799 const char *errtype
;
5803 register enum tree_code codel
= TREE_CODE (type
);
5804 register tree rhstype
;
5805 register enum tree_code coder
;
5807 if (codel
== OFFSET_TYPE
)
5810 if (TREE_CODE (rhs
) == OFFSET_REF
)
5811 rhs
= resolve_offset_ref (rhs
);
5813 /* Strip NON_LVALUE_EXPRs since we aren't using as an lvalue. */
5814 if (TREE_CODE (rhs
) == NON_LVALUE_EXPR
)
5815 rhs
= TREE_OPERAND (rhs
, 0);
5817 rhstype
= TREE_TYPE (rhs
);
5818 coder
= TREE_CODE (rhstype
);
5820 if (rhs
== error_mark_node
|| rhstype
== error_mark_node
)
5821 return error_mark_node
;
5822 if (TREE_CODE (rhs
) == TREE_LIST
&& TREE_VALUE (rhs
) == error_mark_node
)
5823 return error_mark_node
;
5825 rhs
= dubious_conversion_warnings (type
, rhs
, errtype
, fndecl
, parmnum
);
5827 /* The RHS of an assignment cannot have void type. */
5828 if (coder
== VOID_TYPE
)
5830 error ("void value not ignored as it ought to be");
5831 return error_mark_node
;
5834 /* Simplify the RHS if possible. */
5835 if (TREE_CODE (rhs
) == CONST_DECL
)
5836 rhs
= DECL_INITIAL (rhs
);
5837 else if (coder
!= ARRAY_TYPE
)
5838 rhs
= decl_constant_value (rhs
);
5842 The expression is implicitly converted (clause _conv_) to the
5843 cv-unqualified type of the left operand.
5845 We allow bad conversions here because by the time we get to this point
5846 we are committed to doing the conversion. If we end up doing a bad
5847 conversion, convert_like will complain. */
5848 if (!can_convert_arg_bad (type
, rhstype
, rhs
))
5850 /* When -Wno-pmf-conversions is use, we just silently allow
5851 conversions from pointers-to-members to plain pointers. If
5852 the conversion doesn't work, cp_convert will complain. */
5854 && TYPE_PTR_P (type
)
5855 && TYPE_PTRMEMFUNC_P (rhstype
))
5856 rhs
= cp_convert (strip_top_quals (type
), rhs
);
5859 /* If the right-hand side has unknown type, then it is an
5860 overloaded function. Call instantiate_type to get error
5862 if (rhstype
== unknown_type_node
)
5863 instantiate_type (type
, rhs
, tf_error
| tf_warning
);
5865 error ("cannot convert `%T' to `%T' for argument `%P' to `%D'",
5866 rhstype
, type
, parmnum
, fndecl
);
5868 error ("cannot convert `%T' to `%T' in %s", rhstype
, type
,
5870 return error_mark_node
;
5873 return perform_implicit_conversion (strip_top_quals (type
), rhs
);
5876 /* Convert RHS to be of type TYPE.
5877 If EXP is non-zero, it is the target of the initialization.
5878 ERRTYPE is a string to use in error messages.
5880 Two major differences between the behavior of
5881 `convert_for_assignment' and `convert_for_initialization'
5882 are that references are bashed in the former, while
5883 copied in the latter, and aggregates are assigned in
5884 the former (operator=) while initialized in the
5887 If using constructor make sure no conversion operator exists, if one does
5888 exist, an ambiguity exists.
5890 If flags doesn't include LOOKUP_COMPLAIN, don't complain about anything. */
5893 convert_for_initialization (exp
, type
, rhs
, flags
, errtype
, fndecl
, parmnum
)
5894 tree exp
, type
, rhs
;
5896 const char *errtype
;
5900 register enum tree_code codel
= TREE_CODE (type
);
5901 register tree rhstype
;
5902 register enum tree_code coder
;
5904 /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
5905 Strip such NOP_EXPRs, since RHS is used in non-lvalue context. */
5906 if (TREE_CODE (rhs
) == NOP_EXPR
5907 && TREE_TYPE (rhs
) == TREE_TYPE (TREE_OPERAND (rhs
, 0))
5908 && codel
!= REFERENCE_TYPE
)
5909 rhs
= TREE_OPERAND (rhs
, 0);
5911 if (rhs
== error_mark_node
5912 || (TREE_CODE (rhs
) == TREE_LIST
&& TREE_VALUE (rhs
) == error_mark_node
))
5913 return error_mark_node
;
5915 if (TREE_CODE (rhs
) == OFFSET_REF
)
5917 rhs
= resolve_offset_ref (rhs
);
5918 if (rhs
== error_mark_node
)
5919 return error_mark_node
;
5922 if (TREE_CODE (TREE_TYPE (rhs
)) == REFERENCE_TYPE
)
5923 rhs
= convert_from_reference (rhs
);
5925 if ((TREE_CODE (TREE_TYPE (rhs
)) == ARRAY_TYPE
5926 && TREE_CODE (type
) != ARRAY_TYPE
5927 && (TREE_CODE (type
) != REFERENCE_TYPE
5928 || TREE_CODE (TREE_TYPE (type
)) != ARRAY_TYPE
))
5929 || (TREE_CODE (TREE_TYPE (rhs
)) == FUNCTION_TYPE
5930 && (TREE_CODE (type
) != REFERENCE_TYPE
5931 || TREE_CODE (TREE_TYPE (type
)) != FUNCTION_TYPE
))
5932 || TREE_CODE (TREE_TYPE (rhs
)) == METHOD_TYPE
)
5933 rhs
= default_conversion (rhs
);
5935 rhstype
= TREE_TYPE (rhs
);
5936 coder
= TREE_CODE (rhstype
);
5938 if (coder
== ERROR_MARK
)
5939 return error_mark_node
;
5941 /* We accept references to incomplete types, so we can
5942 return here before checking if RHS is of complete type. */
5944 if (codel
== REFERENCE_TYPE
)
5946 /* This should eventually happen in convert_arguments. */
5947 int savew
= 0, savee
= 0;
5950 savew
= warningcount
, savee
= errorcount
;
5951 rhs
= initialize_reference (type
, rhs
);
5954 if (warningcount
> savew
)
5955 cp_warning_at ("in passing argument %P of `%+D'", parmnum
, fndecl
);
5956 else if (errorcount
> savee
)
5957 cp_error_at ("in passing argument %P of `%+D'", parmnum
, fndecl
);
5963 exp
= require_complete_type (exp
);
5964 if (exp
== error_mark_node
)
5965 return error_mark_node
;
5967 if (TREE_CODE (rhstype
) == REFERENCE_TYPE
)
5968 rhstype
= TREE_TYPE (rhstype
);
5970 type
= complete_type (type
);
5972 if (IS_AGGR_TYPE (type
))
5973 return ocp_convert (type
, rhs
, CONV_IMPLICIT
|CONV_FORCE_TEMP
, flags
);
5975 return convert_for_assignment (type
, rhs
, errtype
, fndecl
, parmnum
);
5978 /* Expand an ASM statement with operands, handling output operands
5979 that are not variables or INDIRECT_REFS by transforming such
5980 cases into cases that expand_asm_operands can handle.
5982 Arguments are same as for expand_asm_operands.
5984 We don't do default conversions on all inputs, because it can screw
5985 up operands that are expected to be in memory. */
5988 c_expand_asm_operands (string
, outputs
, inputs
, clobbers
, vol
, filename
, line
)
5989 tree string
, outputs
, inputs
, clobbers
;
5991 const char *filename
;
5994 int noutputs
= list_length (outputs
);
5996 /* o[I] is the place that output number I should be written. */
5997 register tree
*o
= (tree
*) alloca (noutputs
* sizeof (tree
));
6000 /* Record the contents of OUTPUTS before it is modified. */
6001 for (i
= 0, tail
= outputs
; tail
; tail
= TREE_CHAIN (tail
), i
++)
6002 o
[i
] = TREE_VALUE (tail
);
6004 /* Generate the ASM_OPERANDS insn;
6005 store into the TREE_VALUEs of OUTPUTS some trees for
6006 where the values were actually stored. */
6007 expand_asm_operands (string
, outputs
, inputs
, clobbers
, vol
, filename
, line
);
6009 /* Copy all the intermediate outputs into the specified outputs. */
6010 for (i
= 0, tail
= outputs
; tail
; tail
= TREE_CHAIN (tail
), i
++)
6012 if (o
[i
] != TREE_VALUE (tail
))
6014 expand_expr (build_modify_expr (o
[i
], NOP_EXPR
, TREE_VALUE (tail
)),
6015 const0_rtx
, VOIDmode
, EXPAND_NORMAL
);
6018 /* Restore the original value so that it's correct the next
6019 time we expand this function. */
6020 TREE_VALUE (tail
) = o
[i
];
6022 /* Detect modification of read-only values.
6023 (Otherwise done by build_modify_expr.) */
6026 tree type
= TREE_TYPE (o
[i
]);
6027 if (CP_TYPE_CONST_P (type
)
6028 || (IS_AGGR_TYPE_CODE (TREE_CODE (type
))
6029 && C_TYPE_FIELDS_READONLY (type
)))
6030 readonly_error (o
[i
], "modification by `asm'", 1);
6034 /* Those MODIFY_EXPRs could do autoincrements. */
6038 /* If RETVAL is the address of, or a reference to, a local variable or
6039 temporary give an appropraite warning. */
6042 maybe_warn_about_returning_address_of_local (retval
)
6045 tree valtype
= TREE_TYPE (DECL_RESULT (current_function_decl
));
6046 tree whats_returned
= retval
;
6050 if (TREE_CODE (whats_returned
) == COMPOUND_EXPR
)
6051 whats_returned
= TREE_OPERAND (whats_returned
, 1);
6052 else if (TREE_CODE (whats_returned
) == CONVERT_EXPR
6053 || TREE_CODE (whats_returned
) == NON_LVALUE_EXPR
6054 || TREE_CODE (whats_returned
) == NOP_EXPR
)
6055 whats_returned
= TREE_OPERAND (whats_returned
, 0);
6060 if (TREE_CODE (whats_returned
) != ADDR_EXPR
)
6062 whats_returned
= TREE_OPERAND (whats_returned
, 0);
6064 if (TREE_CODE (valtype
) == REFERENCE_TYPE
)
6066 if (TREE_CODE (whats_returned
) == AGGR_INIT_EXPR
6067 || TREE_CODE (whats_returned
) == TARGET_EXPR
)
6069 /* Get the target. */
6070 whats_returned
= TREE_OPERAND (whats_returned
, 0);
6071 warning ("returning reference to temporary");
6074 if (TREE_CODE (whats_returned
) == VAR_DECL
6075 && DECL_NAME (whats_returned
)
6076 && TEMP_NAME_P (DECL_NAME (whats_returned
)))
6078 warning ("reference to non-lvalue returned");
6083 if (TREE_CODE (whats_returned
) == VAR_DECL
6084 && DECL_NAME (whats_returned
)
6085 && DECL_FUNCTION_SCOPE_P (whats_returned
)
6086 && !(TREE_STATIC (whats_returned
)
6087 || TREE_PUBLIC (whats_returned
)))
6089 if (TREE_CODE (valtype
) == REFERENCE_TYPE
)
6090 cp_warning_at ("reference to local variable `%D' returned",
6093 cp_warning_at ("address of local variable `%D' returned",
6099 /* Check that returning RETVAL from the current function is valid.
6100 Return an expression explicitly showing all conversions required to
6101 change RETVAL into the function return type, and to assign it to
6102 the DECL_RESULT for the function. */
6105 check_return_expr (retval
)
6109 /* The type actually returned by the function, after any
6112 int fn_returns_value_p
;
6114 /* A `volatile' function is one that isn't supposed to return, ever.
6115 (This is a G++ extension, used to get better code for functions
6116 that call the `volatile' function.) */
6117 if (TREE_THIS_VOLATILE (current_function_decl
))
6118 warning ("function declared `noreturn' has a `return' statement");
6120 /* Check for various simple errors. */
6121 if (DECL_DESTRUCTOR_P (current_function_decl
))
6124 error ("returning a value from a destructor");
6127 else if (DECL_CONSTRUCTOR_P (current_function_decl
))
6129 if (in_function_try_handler
)
6130 /* If a return statement appears in a handler of the
6131 function-try-block of a constructor, the program is ill-formed. */
6132 error ("cannot return from a handler of a function-try-block of a constructor");
6134 /* You can't return a value from a constructor. */
6135 error ("returning a value from a constructor");
6139 /* When no explicit return-value is given in a function with a named
6140 return value, the named return value is used. */
6141 result
= DECL_RESULT (current_function_decl
);
6142 valtype
= TREE_TYPE (result
);
6143 my_friendly_assert (valtype
!= NULL_TREE
, 19990924);
6144 fn_returns_value_p
= !VOID_TYPE_P (valtype
);
6145 if (!retval
&& DECL_NAME (result
) && fn_returns_value_p
)
6148 /* Check for a return statement with no return value in a function
6149 that's supposed to return a value. */
6150 if (!retval
&& fn_returns_value_p
)
6152 pedwarn ("return-statement with no value, in function declared with a non-void return type");
6153 /* Clear this, so finish_function won't say that we reach the
6154 end of a non-void function (which we don't, we gave a
6156 current_function_returns_null
= 0;
6158 /* Check for a return statement with a value in a function that
6159 isn't supposed to return a value. */
6160 else if (retval
&& !fn_returns_value_p
)
6162 if (VOID_TYPE_P (TREE_TYPE (retval
)))
6163 /* You can return a `void' value from a function of `void'
6164 type. In that case, we have to evaluate the expression for
6165 its side-effects. */
6166 finish_expr_stmt (retval
);
6168 pedwarn ("return-statement with a value, in function declared with a void return type");
6170 current_function_returns_null
= 1;
6172 /* There's really no value to return, after all. */
6176 /* Remember that this function can sometimes return without a
6178 current_function_returns_null
= 1;
6180 /* Remember that this function did return a value. */
6181 current_function_returns_value
= 1;
6183 /* Only operator new(...) throw(), can return NULL [expr.new/13]. */
6184 if ((DECL_OVERLOADED_OPERATOR_P (current_function_decl
) == NEW_EXPR
6185 || DECL_OVERLOADED_OPERATOR_P (current_function_decl
) == VEC_NEW_EXPR
)
6186 && !TYPE_NOTHROW_P (TREE_TYPE (current_function_decl
))
6188 && null_ptr_cst_p (retval
))
6189 warning ("`operator new' must not return NULL unless it is declared `throw()' (or -fcheck-new is in effect)");
6191 /* Effective C++ rule 15. See also start_function. */
6193 && DECL_NAME (current_function_decl
) == ansi_assopname(NOP_EXPR
)
6194 && retval
!= current_class_ref
)
6195 warning ("`operator=' should return a reference to `*this'");
6197 /* The fabled Named Return Value optimization, as per [class.copy]/15:
6199 [...] For a function with a class return type, if the expression
6200 in the return statement is the name of a local object, and the cv-
6201 unqualified type of the local object is the same as the function
6202 return type, an implementation is permitted to omit creating the tem-
6203 porary object to hold the function return value [...]
6205 So, if this is a value-returning function that always returns the same
6206 local variable, remember it.
6208 It might be nice to be more flexible, and choose the first suitable
6209 variable even if the function sometimes returns something else, but
6210 then we run the risk of clobbering the variable we chose if the other
6211 returned expression uses the chosen variable somehow. And people expect
6212 this restriction, anyway. (jason 2000-11-19)
6214 See finish_function, genrtl_start_function, and declare_return_variable
6215 for other pieces of this optimization. */
6217 if (fn_returns_value_p
&& flag_elide_constructors
)
6219 if (retval
!= NULL_TREE
6220 && (current_function_return_value
== NULL_TREE
6221 || current_function_return_value
== retval
)
6222 && TREE_CODE (retval
) == VAR_DECL
6223 && DECL_CONTEXT (retval
) == current_function_decl
6224 && ! TREE_STATIC (retval
)
6225 && (DECL_ALIGN (retval
)
6226 >= DECL_ALIGN (DECL_RESULT (current_function_decl
)))
6227 && same_type_p ((TYPE_MAIN_VARIANT
6228 (TREE_TYPE (retval
))),
6230 (TREE_TYPE (TREE_TYPE (current_function_decl
))))))
6231 current_function_return_value
= retval
;
6233 current_function_return_value
= error_mark_node
;
6236 /* We don't need to do any conversions when there's nothing being
6238 if (!retval
|| retval
== error_mark_node
)
6241 /* Do any required conversions. */
6242 if (retval
== result
|| DECL_CONSTRUCTOR_P (current_function_decl
))
6243 /* No conversions are required. */
6247 /* The type the function is declared to return. */
6248 tree functype
= TREE_TYPE (TREE_TYPE (current_function_decl
));
6250 /* First convert the value to the function's return type, then
6251 to the type of return value's location to handle the
6252 case that functype is smaller than the valtype. */
6253 retval
= convert_for_initialization
6254 (NULL_TREE
, functype
, retval
, LOOKUP_NORMAL
|LOOKUP_ONLYCONVERTING
,
6255 "return", NULL_TREE
, 0);
6256 retval
= convert (valtype
, retval
);
6258 /* If the conversion failed, treat this just like `return;'. */
6259 if (retval
== error_mark_node
)
6261 /* We can't initialize a register from a AGGR_INIT_EXPR. */
6262 else if (! current_function_returns_struct
6263 && TREE_CODE (retval
) == TARGET_EXPR
6264 && TREE_CODE (TREE_OPERAND (retval
, 1)) == AGGR_INIT_EXPR
)
6265 retval
= build (COMPOUND_EXPR
, TREE_TYPE (retval
), retval
,
6266 TREE_OPERAND (retval
, 0));
6268 maybe_warn_about_returning_address_of_local (retval
);
6271 /* Actually copy the value returned into the appropriate location. */
6272 if (retval
&& retval
!= result
)
6273 retval
= build (INIT_EXPR
, TREE_TYPE (result
), result
, retval
);
6279 /* Returns non-zero if the pointer-type FROM can be converted to the
6280 pointer-type TO via a qualification conversion. If CONSTP is -1,
6281 then we return non-zero if the pointers are similar, and the
6282 cv-qualification signature of FROM is a proper subset of that of TO.
6284 If CONSTP is positive, then all outer pointers have been
6288 comp_ptr_ttypes_real (to
, from
, constp
)
6292 int to_more_cv_qualified
= 0;
6294 for (; ; to
= TREE_TYPE (to
), from
= TREE_TYPE (from
))
6296 if (TREE_CODE (to
) != TREE_CODE (from
))
6299 if (TREE_CODE (from
) == OFFSET_TYPE
6300 && same_type_p (TYPE_OFFSET_BASETYPE (from
),
6301 TYPE_OFFSET_BASETYPE (to
)))
6304 /* Const and volatile mean something different for function types,
6305 so the usual checks are not appropriate. */
6306 if (TREE_CODE (to
) != FUNCTION_TYPE
&& TREE_CODE (to
) != METHOD_TYPE
)
6308 if (!at_least_as_qualified_p (to
, from
))
6311 if (!at_least_as_qualified_p (from
, to
))
6316 ++to_more_cv_qualified
;
6320 constp
&= TYPE_READONLY (to
);
6323 if (TREE_CODE (to
) != POINTER_TYPE
)
6325 same_type_ignoring_top_level_qualifiers_p (to
, from
)
6326 && (constp
>= 0 || to_more_cv_qualified
);
6330 /* When comparing, say, char ** to char const **, this function takes the
6331 'char *' and 'char const *'. Do not pass non-pointer types to this
6335 comp_ptr_ttypes (to
, from
)
6338 return comp_ptr_ttypes_real (to
, from
, 1);
6341 /* Returns 1 if to and from are (possibly multi-level) pointers to the same
6342 type or inheritance-related types, regardless of cv-quals. */
6345 ptr_reasonably_similar (to
, from
)
6348 for (; ; to
= TREE_TYPE (to
), from
= TREE_TYPE (from
))
6350 /* Any target type is similar enough to void. */
6351 if (TREE_CODE (to
) == VOID_TYPE
6352 || TREE_CODE (from
) == VOID_TYPE
)
6355 if (TREE_CODE (to
) != TREE_CODE (from
))
6358 if (TREE_CODE (from
) == OFFSET_TYPE
6359 && comptypes (TYPE_OFFSET_BASETYPE (to
),
6360 TYPE_OFFSET_BASETYPE (from
),
6361 COMPARE_BASE
| COMPARE_RELAXED
))
6364 if (TREE_CODE (to
) == INTEGER_TYPE
6365 && TYPE_PRECISION (to
) == TYPE_PRECISION (from
))
6368 if (TREE_CODE (to
) == FUNCTION_TYPE
)
6371 if (TREE_CODE (to
) != POINTER_TYPE
)
6373 (TYPE_MAIN_VARIANT (to
), TYPE_MAIN_VARIANT (from
),
6374 COMPARE_BASE
| COMPARE_RELAXED
);
6378 /* Like comp_ptr_ttypes, for const_cast. */
6381 comp_ptr_ttypes_const (to
, from
)
6384 for (; ; to
= TREE_TYPE (to
), from
= TREE_TYPE (from
))
6386 if (TREE_CODE (to
) != TREE_CODE (from
))
6389 if (TREE_CODE (from
) == OFFSET_TYPE
6390 && same_type_p (TYPE_OFFSET_BASETYPE (from
),
6391 TYPE_OFFSET_BASETYPE (to
)))
6394 if (TREE_CODE (to
) != POINTER_TYPE
)
6395 return same_type_ignoring_top_level_qualifiers_p (to
, from
);
6399 /* Like comp_ptr_ttypes, for reinterpret_cast. */
6402 comp_ptr_ttypes_reinterpret (to
, from
)
6407 for (; ; to
= TREE_TYPE (to
), from
= TREE_TYPE (from
))
6409 if (TREE_CODE (from
) == OFFSET_TYPE
)
6410 from
= TREE_TYPE (from
);
6411 if (TREE_CODE (to
) == OFFSET_TYPE
)
6412 to
= TREE_TYPE (to
);
6414 /* Const and volatile mean something different for function types,
6415 so the usual checks are not appropriate. */
6416 if (TREE_CODE (from
) != FUNCTION_TYPE
&& TREE_CODE (from
) != METHOD_TYPE
6417 && TREE_CODE (to
) != FUNCTION_TYPE
&& TREE_CODE (to
) != METHOD_TYPE
)
6419 if (!at_least_as_qualified_p (to
, from
))
6423 && !at_least_as_qualified_p (from
, to
))
6425 constp
&= TYPE_READONLY (to
);
6428 if (TREE_CODE (from
) != POINTER_TYPE
6429 || TREE_CODE (to
) != POINTER_TYPE
)
6434 /* Returns the type qualifiers for this type, including the qualifiers on the
6435 elements for an array type. */
6438 cp_type_quals (type
)
6441 type
= strip_array_types (type
);
6442 return TYPE_QUALS (type
);
6445 /* Returns non-zero if the TYPE contains a mutable member */
6448 cp_has_mutable_p (type
)
6451 type
= strip_array_types (type
);
6453 return CLASS_TYPE_P (type
) && CLASSTYPE_HAS_MUTABLE (type
);
6456 /* Subroutine of casts_away_constness. Make T1 and T2 point at
6457 exemplar types such that casting T1 to T2 is casting away castness
6458 if and only if there is no implicit conversion from T1 to T2. */
6461 casts_away_constness_r (t1
, t2
)
6468 /* [expr.const.cast]
6470 For multi-level pointer to members and multi-level mixed pointers
6471 and pointers to members (conv.qual), the "member" aspect of a
6472 pointer to member level is ignored when determining if a const
6473 cv-qualifier has been cast away. */
6474 if (TYPE_PTRMEM_P (*t1
))
6475 *t1
= build_pointer_type (TREE_TYPE (TREE_TYPE (*t1
)));
6476 if (TYPE_PTRMEM_P (*t2
))
6477 *t2
= build_pointer_type (TREE_TYPE (TREE_TYPE (*t2
)));
6479 /* [expr.const.cast]
6481 For two pointer types:
6483 X1 is T1cv1,1 * ... cv1,N * where T1 is not a pointer type
6484 X2 is T2cv2,1 * ... cv2,M * where T2 is not a pointer type
6487 casting from X1 to X2 casts away constness if, for a non-pointer
6488 type T there does not exist an implicit conversion (clause
6491 Tcv1,(N-K+1) * cv1,(N-K+2) * ... cv1,N *
6495 Tcv2,(M-K+1) * cv2,(M-K+2) * ... cv2,M *. */
6497 if (TREE_CODE (*t1
) != POINTER_TYPE
6498 || TREE_CODE (*t2
) != POINTER_TYPE
)
6500 *t1
= cp_build_qualified_type (void_type_node
,
6501 cp_type_quals (*t1
));
6502 *t2
= cp_build_qualified_type (void_type_node
,
6503 cp_type_quals (*t2
));
6507 quals1
= cp_type_quals (*t1
);
6508 quals2
= cp_type_quals (*t2
);
6509 *t1
= TREE_TYPE (*t1
);
6510 *t2
= TREE_TYPE (*t2
);
6511 casts_away_constness_r (t1
, t2
);
6512 *t1
= build_pointer_type (*t1
);
6513 *t2
= build_pointer_type (*t2
);
6514 *t1
= cp_build_qualified_type (*t1
, quals1
);
6515 *t2
= cp_build_qualified_type (*t2
, quals2
);
6518 /* Returns non-zero if casting from TYPE1 to TYPE2 casts away
6522 casts_away_constness (t1
, t2
)
6526 if (TREE_CODE (t2
) == REFERENCE_TYPE
)
6528 /* [expr.const.cast]
6530 Casting from an lvalue of type T1 to an lvalue of type T2
6531 using a reference cast casts away constness if a cast from an
6532 rvalue of type "pointer to T1" to the type "pointer to T2"
6533 casts away constness. */
6534 t1
= (TREE_CODE (t1
) == REFERENCE_TYPE
6535 ? TREE_TYPE (t1
) : t1
);
6536 return casts_away_constness (build_pointer_type (t1
),
6537 build_pointer_type (TREE_TYPE (t2
)));
6540 if (TYPE_PTRMEM_P (t1
) && TYPE_PTRMEM_P (t2
))
6541 /* [expr.const.cast]
6543 Casting from an rvalue of type "pointer to data member of X
6544 of type T1" to the type "pointer to data member of Y of type
6545 T2" casts away constness if a cast from an rvalue of type
6546 "pointer to T1" to the type "pointer to T2" casts away
6548 return casts_away_constness
6549 (build_pointer_type (TREE_TYPE (TREE_TYPE (t1
))),
6550 build_pointer_type (TREE_TYPE (TREE_TYPE (t2
))));
6552 /* Casting away constness is only something that makes sense for
6553 pointer or reference types. */
6554 if (TREE_CODE (t1
) != POINTER_TYPE
6555 || TREE_CODE (t2
) != POINTER_TYPE
)
6558 /* Top-level qualifiers don't matter. */
6559 t1
= TYPE_MAIN_VARIANT (t1
);
6560 t2
= TYPE_MAIN_VARIANT (t2
);
6561 casts_away_constness_r (&t1
, &t2
);
6562 if (!can_convert (t2
, t1
))
6568 /* Returns TYPE with its cv qualifiers removed
6569 TYPE is T cv* .. *cv where T is not a pointer type,
6570 returns T * .. *. (If T is an array type, then the cv qualifiers
6571 above are those of the array members.) */
6574 strip_all_pointer_quals (type
)
6577 if (TREE_CODE (type
) == POINTER_TYPE
)
6578 return build_pointer_type (strip_all_pointer_quals (TREE_TYPE (type
)));
6579 else if (TREE_CODE (type
) == OFFSET_TYPE
)
6580 return build_offset_type (TYPE_OFFSET_BASETYPE (type
),
6581 strip_all_pointer_quals (TREE_TYPE (type
)));
6583 return TYPE_MAIN_VARIANT (type
);