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 build_component_addr
PARAMS ((tree
, tree
));
61 static tree qualify_type_recursive
PARAMS ((tree
, tree
));
62 static tree get_delta_difference
PARAMS ((tree
, tree
, int));
63 static int comp_cv_target_types
PARAMS ((tree
, tree
, int));
64 static void casts_away_constness_r
PARAMS ((tree
*, tree
*));
65 static int casts_away_constness
PARAMS ((tree
, tree
));
66 static void maybe_warn_about_returning_address_of_local
PARAMS ((tree
));
67 static tree strip_all_pointer_quals
PARAMS ((tree
));
69 /* Return the target type of TYPE, which means return T for:
70 T*, T&, T[], T (...), and otherwise, just T. */
76 if (TREE_CODE (type
) == REFERENCE_TYPE
)
77 type
= TREE_TYPE (type
);
78 while (TREE_CODE (type
) == POINTER_TYPE
79 || TREE_CODE (type
) == ARRAY_TYPE
80 || TREE_CODE (type
) == FUNCTION_TYPE
81 || TREE_CODE (type
) == METHOD_TYPE
82 || TREE_CODE (type
) == OFFSET_TYPE
)
83 type
= TREE_TYPE (type
);
87 /* Do `exp = require_complete_type (exp);' to make sure exp
88 does not have an incomplete type. (That includes void types.)
89 Returns the error_mark_node if the VALUE does not have
90 complete type when this function returns. */
93 require_complete_type (value
)
98 if (processing_template_decl
|| value
== error_mark_node
)
101 if (TREE_CODE (value
) == OVERLOAD
)
102 type
= unknown_type_node
;
104 type
= TREE_TYPE (value
);
106 /* First, detect a valid value with a complete type. */
107 if (COMPLETE_TYPE_P (type
))
110 /* If we see X::Y, we build an OFFSET_TYPE which has
111 not been laid out. Try to avoid an error by interpreting
112 it as this->X::Y, if reasonable. */
113 if (TREE_CODE (value
) == OFFSET_REF
114 && current_class_ref
!= 0
115 && TREE_OPERAND (value
, 0) == current_class_ref
)
117 value
= resolve_offset_ref (value
);
118 return require_complete_type (value
);
121 if (complete_type_or_else (type
, value
))
124 return error_mark_node
;
127 /* Try to complete TYPE, if it is incomplete. For example, if TYPE is
128 a template instantiation, do the instantiation. Returns TYPE,
129 whether or not it could be completed, unless something goes
130 horribly wrong, in which case the error_mark_node is returned. */
136 if (type
== NULL_TREE
)
137 /* Rather than crash, we return something sure to cause an error
139 return error_mark_node
;
141 if (type
== error_mark_node
|| COMPLETE_TYPE_P (type
))
143 else if (TREE_CODE (type
) == ARRAY_TYPE
&& TYPE_DOMAIN (type
))
145 tree t
= complete_type (TREE_TYPE (type
));
146 if (COMPLETE_TYPE_P (t
) && ! processing_template_decl
)
148 TYPE_NEEDS_CONSTRUCTING (type
)
149 = TYPE_NEEDS_CONSTRUCTING (TYPE_MAIN_VARIANT (t
));
150 TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type
)
151 = TYPE_HAS_NONTRIVIAL_DESTRUCTOR (TYPE_MAIN_VARIANT (t
));
153 else if (CLASS_TYPE_P (type
) && CLASSTYPE_TEMPLATE_INSTANTIATION (type
))
154 instantiate_class_template (TYPE_MAIN_VARIANT (type
));
159 /* Like complete_type, but issue an error if the TYPE cannot be
160 completed. VALUE is used for informative diagnostics.
161 Returns NULL_TREE if the type cannot be made complete. */
164 complete_type_or_else (type
, value
)
168 type
= complete_type (type
);
169 if (type
== error_mark_node
)
170 /* We already issued an error. */
172 else if (!COMPLETE_TYPE_P (type
))
174 incomplete_type_error (value
, type
);
181 /* Return truthvalue of whether type of EXP is instantiated. */
187 return (TREE_CODE (exp
) == OVERLOAD
188 || TREE_CODE (exp
) == TREE_LIST
189 || TREE_TYPE (exp
) == unknown_type_node
190 || (TREE_CODE (TREE_TYPE (exp
)) == OFFSET_TYPE
191 && TREE_TYPE (TREE_TYPE (exp
)) == unknown_type_node
));
194 /* Return a pointer or pointer to member type similar to T1, with a
195 cv-qualification signature that is the union of the cv-qualification
196 signatures of T1 and T2: [expr.rel], [expr.eq]. */
199 qualify_type_recursive (t1
, t2
)
202 if ((TYPE_PTR_P (t1
) && TYPE_PTR_P (t2
))
203 || (TYPE_PTRMEM_P (t1
) && TYPE_PTRMEM_P (t2
)))
205 tree tt1
= TREE_TYPE (t1
);
206 tree tt2
= TREE_TYPE (t2
);
210 tree attributes
= (*targetm
.merge_type_attributes
) (t1
, t2
);
212 if (TREE_CODE (tt1
) == OFFSET_TYPE
)
214 b1
= TYPE_OFFSET_BASETYPE (tt1
);
215 tt1
= TREE_TYPE (tt1
);
216 tt2
= TREE_TYPE (tt2
);
221 type_quals
= (cp_type_quals (tt1
) | cp_type_quals (tt2
));
222 tgt
= qualify_type_recursive (tt1
, tt2
);
223 tgt
= cp_build_qualified_type (tgt
, type_quals
);
225 tgt
= build_offset_type (b1
, tgt
);
226 t1
= build_pointer_type (tgt
);
227 t1
= build_type_attribute_variant (t1
, attributes
);
232 /* Return the common type of two parameter lists.
233 We assume that comptypes has already been done and returned 1;
234 if that isn't so, this may crash.
236 As an optimization, free the space we allocate if the parameter
237 lists are already common. */
243 tree oldargs
= p1
, newargs
, n
;
247 len
= list_length (p1
);
248 newargs
= tree_last (p1
);
250 if (newargs
== void_list_node
)
259 newargs
= tree_cons (NULL_TREE
, NULL_TREE
, newargs
);
264 p1
= TREE_CHAIN (p1
), p2
= TREE_CHAIN (p2
), n
= TREE_CHAIN (n
), i
++)
266 if (TREE_PURPOSE (p1
) && !TREE_PURPOSE (p2
))
268 TREE_PURPOSE (n
) = TREE_PURPOSE (p1
);
271 else if (! TREE_PURPOSE (p1
))
273 if (TREE_PURPOSE (p2
))
275 TREE_PURPOSE (n
) = TREE_PURPOSE (p2
);
281 if (1 != simple_cst_equal (TREE_PURPOSE (p1
), TREE_PURPOSE (p2
)))
283 TREE_PURPOSE (n
) = TREE_PURPOSE (p2
);
285 if (TREE_VALUE (p1
) != TREE_VALUE (p2
))
288 TREE_VALUE (n
) = merge_types (TREE_VALUE (p1
), TREE_VALUE (p2
));
291 TREE_VALUE (n
) = TREE_VALUE (p1
);
299 /* Given a type, perhaps copied for a typedef,
300 find the "original" version of it. */
305 while (TYPE_NAME (t
) != NULL_TREE
)
307 tree x
= TYPE_NAME (t
);
308 if (TREE_CODE (x
) != TYPE_DECL
)
310 x
= DECL_ORIGINAL_TYPE (x
);
318 /* T1 and T2 are arithmetic or enumeration types. Return the type
319 that will result from the "usual arithmetic conversions" on T1 and
320 T2 as described in [expr]. */
323 type_after_usual_arithmetic_conversions (t1
, t2
)
327 enum tree_code code1
= TREE_CODE (t1
);
328 enum tree_code code2
= TREE_CODE (t2
);
331 /* FIXME: Attributes. */
332 my_friendly_assert (ARITHMETIC_TYPE_P (t1
)
333 || TREE_CODE (t1
) == COMPLEX_TYPE
334 || TREE_CODE (t1
) == ENUMERAL_TYPE
,
336 my_friendly_assert (ARITHMETIC_TYPE_P (t2
)
337 || TREE_CODE (t2
) == COMPLEX_TYPE
338 || TREE_CODE (t2
) == ENUMERAL_TYPE
,
341 /* In what follows, we slightly generalize the rules given in [expr] so
342 as to deal with `long long' and `complex'. First, merge the
344 attributes
= (*targetm
.merge_type_attributes
) (t1
, t2
);
346 /* If one type is complex, form the common type of the non-complex
347 components, then make that complex. Use T1 or T2 if it is the
349 if (code1
== COMPLEX_TYPE
|| code2
== COMPLEX_TYPE
)
351 tree subtype1
= code1
== COMPLEX_TYPE
? TREE_TYPE (t1
) : t1
;
352 tree subtype2
= code2
== COMPLEX_TYPE
? TREE_TYPE (t2
) : t2
;
354 = type_after_usual_arithmetic_conversions (subtype1
, subtype2
);
356 if (code1
== COMPLEX_TYPE
&& TREE_TYPE (t1
) == subtype
)
357 return build_type_attribute_variant (t1
, attributes
);
358 else if (code2
== COMPLEX_TYPE
&& TREE_TYPE (t2
) == subtype
)
359 return build_type_attribute_variant (t2
, attributes
);
361 return build_type_attribute_variant (build_complex_type (subtype
),
365 /* If only one is real, use it as the result. */
366 if (code1
== REAL_TYPE
&& code2
!= REAL_TYPE
)
367 return build_type_attribute_variant (t1
, attributes
);
368 if (code2
== REAL_TYPE
&& code1
!= REAL_TYPE
)
369 return build_type_attribute_variant (t2
, attributes
);
371 /* Perform the integral promotions. */
372 if (code1
!= REAL_TYPE
)
374 t1
= type_promotes_to (t1
);
375 t2
= type_promotes_to (t2
);
378 /* Both real or both integers; use the one with greater precision. */
379 if (TYPE_PRECISION (t1
) > TYPE_PRECISION (t2
))
380 return build_type_attribute_variant (t1
, attributes
);
381 else if (TYPE_PRECISION (t2
) > TYPE_PRECISION (t1
))
382 return build_type_attribute_variant (t2
, attributes
);
384 /* The types are the same; no need to do anything fancy. */
385 if (TYPE_MAIN_VARIANT (t1
) == TYPE_MAIN_VARIANT (t2
))
386 return build_type_attribute_variant (t1
, attributes
);
388 if (code1
!= REAL_TYPE
)
390 /* If one is a sizetype, use it so size_binop doesn't blow up. */
391 if (TYPE_IS_SIZETYPE (t1
) > TYPE_IS_SIZETYPE (t2
))
392 return build_type_attribute_variant (t1
, attributes
);
393 if (TYPE_IS_SIZETYPE (t2
) > TYPE_IS_SIZETYPE (t1
))
394 return build_type_attribute_variant (t2
, attributes
);
396 /* If one is unsigned long long, then convert the other to unsigned
398 if (same_type_p (TYPE_MAIN_VARIANT (t1
), long_long_unsigned_type_node
)
399 || same_type_p (TYPE_MAIN_VARIANT (t2
), long_long_unsigned_type_node
))
400 return build_type_attribute_variant (long_long_unsigned_type_node
,
402 /* If one is a long long, and the other is an unsigned long, and
403 long long can represent all the values of an unsigned long, then
404 convert to a long long. Otherwise, convert to an unsigned long
405 long. Otherwise, if either operand is long long, convert the
408 Since we're here, we know the TYPE_PRECISION is the same;
409 therefore converting to long long cannot represent all the values
410 of an unsigned long, so we choose unsigned long long in that
412 if (same_type_p (TYPE_MAIN_VARIANT (t1
), long_long_integer_type_node
)
413 || same_type_p (TYPE_MAIN_VARIANT (t2
), long_long_integer_type_node
))
415 tree t
= ((TREE_UNSIGNED (t1
) || TREE_UNSIGNED (t2
))
416 ? long_long_unsigned_type_node
417 : long_long_integer_type_node
);
418 return build_type_attribute_variant (t
, attributes
);
421 /* Go through the same procedure, but for longs. */
422 if (same_type_p (TYPE_MAIN_VARIANT (t1
), long_unsigned_type_node
)
423 || same_type_p (TYPE_MAIN_VARIANT (t2
), long_unsigned_type_node
))
424 return build_type_attribute_variant (long_unsigned_type_node
,
426 if (same_type_p (TYPE_MAIN_VARIANT (t1
), long_integer_type_node
)
427 || same_type_p (TYPE_MAIN_VARIANT (t2
), long_integer_type_node
))
429 tree t
= ((TREE_UNSIGNED (t1
) || TREE_UNSIGNED (t2
))
430 ? long_unsigned_type_node
: long_integer_type_node
);
431 return build_type_attribute_variant (t
, attributes
);
433 /* Otherwise prefer the unsigned one. */
434 if (TREE_UNSIGNED (t1
))
435 return build_type_attribute_variant (t1
, attributes
);
437 return build_type_attribute_variant (t2
, attributes
);
441 if (same_type_p (TYPE_MAIN_VARIANT (t1
), long_double_type_node
)
442 || same_type_p (TYPE_MAIN_VARIANT (t2
), long_double_type_node
))
443 return build_type_attribute_variant (long_double_type_node
,
445 if (same_type_p (TYPE_MAIN_VARIANT (t1
), double_type_node
)
446 || same_type_p (TYPE_MAIN_VARIANT (t2
), double_type_node
))
447 return build_type_attribute_variant (double_type_node
,
449 if (same_type_p (TYPE_MAIN_VARIANT (t1
), float_type_node
)
450 || same_type_p (TYPE_MAIN_VARIANT (t2
), float_type_node
))
451 return build_type_attribute_variant (float_type_node
,
454 /* Two floating-point types whose TYPE_MAIN_VARIANTs are none of
455 the standard C++ floating-point types. Logic earlier in this
456 function has already eliminated the possibility that
457 TYPE_PRECISION (t2) != TYPE_PRECISION (t1), so there's no
458 compelling reason to choose one or the other. */
459 return build_type_attribute_variant (t1
, attributes
);
463 /* Return the composite pointer type (see [expr.rel]) for T1 and T2.
464 ARG1 and ARG2 are the values with those types. The LOCATION is a
465 string describing the current location, in case an error occurs. */
468 composite_pointer_type (t1
, t2
, arg1
, arg2
, location
)
473 const char* location
;
480 If one operand is a null pointer constant, the composite pointer
481 type is the type of the other operand. */
482 if (null_ptr_cst_p (arg1
))
484 if (null_ptr_cst_p (arg2
))
487 /* Deal with pointer-to-member functions in the same way as we deal
488 with pointers to functions. */
489 if (TYPE_PTRMEMFUNC_P (t1
))
490 t1
= TYPE_PTRMEMFUNC_FN_TYPE (t1
);
491 if (TYPE_PTRMEMFUNC_P (t2
))
492 t2
= TYPE_PTRMEMFUNC_FN_TYPE (t2
);
494 /* Merge the attributes. */
495 attributes
= (*targetm
.merge_type_attributes
) (t1
, t2
);
501 If one of the operands has type "pointer to cv1 void*", then
502 the other has type "pointer to cv2T", and the composite pointer
503 type is "pointer to cv12 void", where cv12 is the union of cv1
506 If either type is a pointer to void, make sure it is T1. */
507 if (VOID_TYPE_P (TREE_TYPE (t2
)))
514 /* Now, if T1 is a pointer to void, merge the qualifiers. */
515 if (VOID_TYPE_P (TREE_TYPE (t1
)))
517 if (pedantic
&& TYPE_PTRFN_P (t2
))
518 pedwarn ("ISO C++ forbids %s between pointer of type `void *' and pointer-to-function", location
);
521 result_type
= cp_build_qualified_type (void_type_node
,
523 | cp_type_quals (t2
)));
524 result_type
= build_pointer_type (result_type
);
528 tree full1
= qualify_type_recursive (t1
, t2
);
529 tree full2
= qualify_type_recursive (t2
, t1
);
531 int val
= comp_target_types (full1
, full2
, 1);
539 pedwarn ("%s between distinct pointer types `%T' and `%T' lacks a cast",
541 result_type
= ptr_type_node
;
545 return build_type_attribute_variant (result_type
, attributes
);
548 /* Return the merged type of two types.
549 We assume that comptypes has already been done and returned 1;
550 if that isn't so, this may crash.
552 This just combines attributes and default arguments; any other
553 differences would cause the two types to compare unalike. */
559 register enum tree_code code1
;
560 register enum tree_code code2
;
563 /* Save time if the two types are the same. */
566 if (original_type (t1
) == original_type (t2
))
569 /* If one type is nonsense, use the other. */
570 if (t1
== error_mark_node
)
572 if (t2
== error_mark_node
)
575 /* Merge the attributes. */
576 attributes
= (*targetm
.merge_type_attributes
) (t1
, t2
);
578 /* Treat an enum type as the unsigned integer type of the same width. */
580 if (TYPE_PTRMEMFUNC_P (t1
))
581 t1
= TYPE_PTRMEMFUNC_FN_TYPE (t1
);
582 if (TYPE_PTRMEMFUNC_P (t2
))
583 t2
= TYPE_PTRMEMFUNC_FN_TYPE (t2
);
585 code1
= TREE_CODE (t1
);
586 code2
= TREE_CODE (t2
);
592 /* For two pointers, do this recursively on the target type. */
594 tree target
= merge_types (TREE_TYPE (t1
), TREE_TYPE (t2
));
595 int quals
= cp_type_quals (t1
);
597 if (code1
== POINTER_TYPE
)
598 t1
= build_pointer_type (target
);
600 t1
= build_reference_type (target
);
601 t1
= build_type_attribute_variant (t1
, attributes
);
602 t1
= cp_build_qualified_type (t1
, quals
);
604 if (TREE_CODE (target
) == METHOD_TYPE
)
605 t1
= build_ptrmemfunc_type (t1
);
612 tree base
= TYPE_OFFSET_BASETYPE (t1
);
613 tree target
= merge_types (TREE_TYPE (t1
), TREE_TYPE (t2
));
614 t1
= build_offset_type (base
, target
);
620 tree elt
= merge_types (TREE_TYPE (t1
), TREE_TYPE (t2
));
621 /* Save space: see if the result is identical to one of the args. */
622 if (elt
== TREE_TYPE (t1
) && TYPE_DOMAIN (t1
))
623 return build_type_attribute_variant (t1
, attributes
);
624 if (elt
== TREE_TYPE (t2
) && TYPE_DOMAIN (t2
))
625 return build_type_attribute_variant (t2
, attributes
);
626 /* Merge the element types, and have a size if either arg has one. */
627 t1
= build_cplus_array_type
628 (elt
, TYPE_DOMAIN (TYPE_DOMAIN (t1
) ? t1
: t2
));
633 /* Function types: prefer the one that specified arg types.
634 If both do, merge the arg types. Also merge the return types. */
636 tree valtype
= merge_types (TREE_TYPE (t1
), TREE_TYPE (t2
));
637 tree p1
= TYPE_ARG_TYPES (t1
);
638 tree p2
= TYPE_ARG_TYPES (t2
);
641 /* Save space: see if the result is identical to one of the args. */
642 if (valtype
== TREE_TYPE (t1
) && ! p2
)
643 return build_type_attribute_variant (t1
, attributes
);
644 if (valtype
== TREE_TYPE (t2
) && ! p1
)
645 return build_type_attribute_variant (t2
, attributes
);
647 /* Simple way if one arg fails to specify argument types. */
648 if (p1
== NULL_TREE
|| TREE_VALUE (p1
) == void_type_node
)
650 rval
= build_function_type (valtype
, p2
);
651 if ((raises
= TYPE_RAISES_EXCEPTIONS (t2
)))
652 rval
= build_exception_variant (rval
, raises
);
653 return build_type_attribute_variant (rval
, attributes
);
655 raises
= TYPE_RAISES_EXCEPTIONS (t1
);
656 if (p2
== NULL_TREE
|| TREE_VALUE (p2
) == void_type_node
)
658 rval
= build_function_type (valtype
, p1
);
660 rval
= build_exception_variant (rval
, raises
);
661 return build_type_attribute_variant (rval
, attributes
);
664 rval
= build_function_type (valtype
, commonparms (p1
, p2
));
665 t1
= build_exception_variant (rval
, raises
);
671 /* Get this value the long way, since TYPE_METHOD_BASETYPE
672 is just the main variant of this. */
673 tree basetype
= TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (t2
)));
674 tree raises
= TYPE_RAISES_EXCEPTIONS (t1
);
677 /* If this was a member function type, get back to the
678 original type of type member function (i.e., without
679 the class instance variable up front. */
680 t1
= build_function_type (TREE_TYPE (t1
),
681 TREE_CHAIN (TYPE_ARG_TYPES (t1
)));
682 t2
= build_function_type (TREE_TYPE (t2
),
683 TREE_CHAIN (TYPE_ARG_TYPES (t2
)));
684 t3
= merge_types (t1
, t2
);
685 t3
= build_cplus_method_type (basetype
, TREE_TYPE (t3
),
686 TYPE_ARG_TYPES (t3
));
687 t1
= build_exception_variant (t3
, raises
);
693 return build_type_attribute_variant (t1
, attributes
);
696 /* Return the common type of two types.
697 We assume that comptypes has already been done and returned 1;
698 if that isn't so, this may crash.
700 This is the type for the result of most arithmetic operations
701 if the operands have the given two types. */
707 enum tree_code code1
;
708 enum tree_code code2
;
710 /* If one type is nonsense, bail. */
711 if (t1
== error_mark_node
|| t2
== error_mark_node
)
712 return error_mark_node
;
714 code1
= TREE_CODE (t1
);
715 code2
= TREE_CODE (t2
);
717 if ((ARITHMETIC_TYPE_P (t1
) || code1
== ENUMERAL_TYPE
718 || code1
== COMPLEX_TYPE
)
719 && (ARITHMETIC_TYPE_P (t2
) || code2
== ENUMERAL_TYPE
720 || code2
== COMPLEX_TYPE
))
721 return type_after_usual_arithmetic_conversions (t1
, t2
);
723 else if ((TYPE_PTR_P (t1
) && TYPE_PTR_P (t2
))
724 || (TYPE_PTRMEM_P (t1
) && TYPE_PTRMEM_P (t2
))
725 || (TYPE_PTRMEMFUNC_P (t1
) && TYPE_PTRMEMFUNC_P (t2
)))
726 return composite_pointer_type (t1
, t2
, error_mark_node
, error_mark_node
,
733 /* Compare two exception specifier types for exactness or subsetness, if
734 allowed. Returns 0 for mismatch, 1 for same, 2 if B is allowed by A.
736 [except.spec] "If a class X ... objects of class X or any class publicly
737 and unambigously derrived from X. Similarly, if a pointer type Y * ...
738 exceptions of type Y * or that are pointers to any type publicly and
739 unambigously derrived from Y. Otherwise a function only allows exceptions
740 that have the same type ..."
741 This does not mention cv qualifiers and is different to what throw
742 [except.throw] and catch [except.catch] will do. They will ignore the
743 top level cv qualifiers, and allow qualifiers in the pointer to class
746 We implement the letter of the standard. */
749 comp_except_types (a
, b
, exact
)
753 if (same_type_p (a
, b
))
757 if (cp_type_quals (a
) || cp_type_quals (b
))
760 if (TREE_CODE (a
) == POINTER_TYPE
761 && TREE_CODE (b
) == POINTER_TYPE
)
765 if (cp_type_quals (a
) || cp_type_quals (b
))
769 if (TREE_CODE (a
) != RECORD_TYPE
770 || TREE_CODE (b
) != RECORD_TYPE
)
773 if (ACCESSIBLY_UNIQUELY_DERIVED_P (a
, b
))
779 /* Return 1 if TYPE1 and TYPE2 are equivalent exception specifiers.
780 If EXACT is 0, T2 can be a subset of T1 (according to 15.4/7),
781 otherwise it must be exact. Exception lists are unordered, but
782 we've already filtered out duplicates. Most lists will be in order,
783 we should try to make use of that. */
786 comp_except_specs (t1
, t2
, exact
)
797 if (t1
== NULL_TREE
) /* T1 is ... */
798 return t2
== NULL_TREE
|| !exact
;
799 if (!TREE_VALUE (t1
)) /* t1 is EMPTY */
800 return t2
!= NULL_TREE
&& !TREE_VALUE (t2
);
801 if (t2
== NULL_TREE
) /* T2 is ... */
803 if (TREE_VALUE(t1
) && !TREE_VALUE (t2
)) /* T2 is EMPTY, T1 is not */
806 /* Neither set is ... or EMPTY, make sure each part of T2 is in T1.
807 Count how many we find, to determine exactness. For exact matching and
808 ordered T1, T2, this is an O(n) operation, otherwise its worst case is
810 for (base
= t1
; t2
!= NULL_TREE
; t2
= TREE_CHAIN (t2
))
812 for (probe
= base
; probe
!= NULL_TREE
; probe
= TREE_CHAIN (probe
))
814 tree a
= TREE_VALUE (probe
);
815 tree b
= TREE_VALUE (t2
);
817 if (comp_except_types (a
, b
, exact
))
819 if (probe
== base
&& exact
)
820 base
= TREE_CHAIN (probe
);
825 if (probe
== NULL_TREE
)
828 return !exact
|| base
== NULL_TREE
|| length
== list_length (t1
);
831 /* Compare the array types T1 and T2, using CMP as the type comparison
832 function for the element types. STRICT is as for comptypes. */
835 comp_array_types (cmp
, t1
, t2
, strict
)
836 register int (*cmp
) PARAMS ((tree
, tree
, int));
846 /* The type of the array elements must be the same. */
847 if (!(TREE_TYPE (t1
) == TREE_TYPE (t2
)
848 || (*cmp
) (TREE_TYPE (t1
), TREE_TYPE (t2
),
849 strict
& ~COMPARE_REDECLARATION
)))
852 d1
= TYPE_DOMAIN (t1
);
853 d2
= TYPE_DOMAIN (t2
);
858 /* If one of the arrays is dimensionless, and the other has a
859 dimension, they are of different types. However, it is legal to
867 declarations for an array object can specify
868 array types that differ by the presence or absence of a major
869 array bound (_dcl.array_). */
871 return strict
& COMPARE_REDECLARATION
;
873 /* Check that the dimensions are the same. */
874 return (cp_tree_equal (TYPE_MIN_VALUE (d1
),
876 && cp_tree_equal (TYPE_MAX_VALUE (d1
),
877 TYPE_MAX_VALUE (d2
)));
880 /* Return 1 if T1 and T2 are compatible types for assignment or
881 various other operations. STRICT is a bitwise-or of the COMPARE_*
885 comptypes (t1
, t2
, strict
)
891 int orig_strict
= strict
;
893 /* The special exemption for redeclaring array types without an
894 array bound only applies at the top level:
899 is not legal, for example. */
900 strict
&= ~COMPARE_REDECLARATION
;
902 /* Suppress errors caused by previously reported errors */
906 /* This should never happen. */
907 my_friendly_assert (t1
!= error_mark_node
, 307);
909 if (t2
== error_mark_node
)
912 /* If either type is the internal version of sizetype, return the
914 if (TREE_CODE (t1
) == INTEGER_TYPE
&& TYPE_IS_SIZETYPE (t1
)
915 && TYPE_DOMAIN (t1
) != 0)
916 t1
= TYPE_DOMAIN (t1
);
918 if (TREE_CODE (t2
) == INTEGER_TYPE
&& TYPE_IS_SIZETYPE (t2
)
919 && TYPE_DOMAIN (t2
) != 0)
920 t2
= TYPE_DOMAIN (t2
);
922 if (strict
& COMPARE_RELAXED
)
924 /* Treat an enum type as the unsigned integer type of the same width. */
926 if (TREE_CODE (t1
) == ENUMERAL_TYPE
)
927 t1
= type_for_size (TYPE_PRECISION (t1
), 1);
928 if (TREE_CODE (t2
) == ENUMERAL_TYPE
)
929 t2
= type_for_size (TYPE_PRECISION (t2
), 1);
935 if (TYPE_PTRMEMFUNC_P (t1
))
936 t1
= TYPE_PTRMEMFUNC_FN_TYPE (t1
);
937 if (TYPE_PTRMEMFUNC_P (t2
))
938 t2
= TYPE_PTRMEMFUNC_FN_TYPE (t2
);
940 /* Different classes of types can't be compatible. */
941 if (TREE_CODE (t1
) != TREE_CODE (t2
))
944 /* Qualifiers must match. */
945 if (cp_type_quals (t1
) != cp_type_quals (t2
))
947 if (strict
== COMPARE_STRICT
948 && TYPE_FOR_JAVA (t1
) != TYPE_FOR_JAVA (t2
))
951 /* Allow for two different type nodes which have essentially the same
952 definition. Note that we already checked for equality of the type
953 qualifiers (just above). */
955 if (TYPE_MAIN_VARIANT (t1
) == TYPE_MAIN_VARIANT (t2
))
958 if (strict
& COMPARE_NO_ATTRIBUTES
)
960 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
961 else if (! (attrval
= (*targetm
.comp_type_attributes
) (t1
, t2
)))
964 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
967 switch (TREE_CODE (t1
))
969 case TEMPLATE_TEMPLATE_PARM
:
970 case BOUND_TEMPLATE_TEMPLATE_PARM
:
971 if (TEMPLATE_TYPE_IDX (t1
) != TEMPLATE_TYPE_IDX (t2
)
972 || TEMPLATE_TYPE_LEVEL (t1
) != TEMPLATE_TYPE_LEVEL (t2
))
974 if (! comp_template_parms
975 (DECL_TEMPLATE_PARMS (TEMPLATE_TEMPLATE_PARM_TEMPLATE_DECL (t1
)),
976 DECL_TEMPLATE_PARMS (TEMPLATE_TEMPLATE_PARM_TEMPLATE_DECL (t2
))))
978 if (TREE_CODE (t1
) == TEMPLATE_TEMPLATE_PARM
)
980 /* Don't check inheritance. */
981 strict
= COMPARE_STRICT
;
986 if (TYPE_TEMPLATE_INFO (t1
) && TYPE_TEMPLATE_INFO (t2
)
987 && (TYPE_TI_TEMPLATE (t1
) == TYPE_TI_TEMPLATE (t2
)
988 || TREE_CODE (t1
) == BOUND_TEMPLATE_TEMPLATE_PARM
))
989 val
= comp_template_args (TYPE_TI_ARGS (t1
),
992 if ((strict
& COMPARE_BASE
) && DERIVED_FROM_P (t1
, t2
))
994 else if ((strict
& COMPARE_RELAXED
) && DERIVED_FROM_P (t2
, t1
))
999 val
= (comptypes (build_pointer_type (TYPE_OFFSET_BASETYPE (t1
)),
1000 build_pointer_type (TYPE_OFFSET_BASETYPE (t2
)), strict
)
1001 && comptypes (TREE_TYPE (t1
), TREE_TYPE (t2
), strict
));
1005 case REFERENCE_TYPE
:
1006 t1
= TREE_TYPE (t1
);
1007 t2
= TREE_TYPE (t2
);
1008 /* first, check whether the referred types match with the
1009 required level of strictness */
1010 val
= comptypes (t1
, t2
, strict
);
1013 if (TREE_CODE (t1
) == RECORD_TYPE
1014 && TREE_CODE (t2
) == RECORD_TYPE
)
1020 val
= ((TREE_TYPE (t1
) == TREE_TYPE (t2
)
1021 || comptypes (TREE_TYPE (t1
), TREE_TYPE (t2
), strict
))
1022 && compparms (TYPE_ARG_TYPES (t1
), TYPE_ARG_TYPES (t2
)));
1026 /* Target types must match incl. qualifiers. We use ORIG_STRICT
1027 here since this is the one place where
1028 COMPARE_REDECLARATION should be used. */
1029 val
= comp_array_types (comptypes
, t1
, t2
, orig_strict
);
1032 case TEMPLATE_TYPE_PARM
:
1033 return TEMPLATE_TYPE_IDX (t1
) == TEMPLATE_TYPE_IDX (t2
)
1034 && TEMPLATE_TYPE_LEVEL (t1
) == TEMPLATE_TYPE_LEVEL (t2
);
1037 if (cp_tree_equal (TYPENAME_TYPE_FULLNAME (t1
),
1038 TYPENAME_TYPE_FULLNAME (t2
)) < 1)
1040 return same_type_p (TYPE_CONTEXT (t1
), TYPE_CONTEXT (t2
));
1042 case UNBOUND_CLASS_TEMPLATE
:
1043 if (cp_tree_equal (TYPE_IDENTIFIER (t1
),
1044 TYPE_IDENTIFIER (t2
)) < 1)
1046 return same_type_p (TYPE_CONTEXT (t1
), TYPE_CONTEXT (t2
));
1049 return same_type_p (TREE_TYPE (t1
), TREE_TYPE (t2
));
1054 return attrval
== 2 && val
== 1 ? 2 : val
;
1057 /* Subroutine of comp_target-types. Make sure that the cv-quals change
1058 only in the same direction as the target type. */
1061 comp_cv_target_types (ttl
, ttr
, nptrs
)
1067 if (!at_least_as_qualified_p (ttl
, ttr
)
1068 && !at_least_as_qualified_p (ttr
, ttl
))
1069 /* The qualifications are incomparable. */
1072 if (TYPE_MAIN_VARIANT (ttl
) == TYPE_MAIN_VARIANT (ttr
))
1073 return more_qualified_p (ttr
, ttl
) ? -1 : 1;
1075 t
= comp_target_types (ttl
, ttr
, nptrs
);
1076 if ((t
== 1 && at_least_as_qualified_p (ttl
, ttr
))
1077 || (t
== -1 && at_least_as_qualified_p (ttr
, ttl
)))
1083 /* Return 1 or -1 if TTL and TTR are pointers to types that are equivalent,
1084 ignoring their qualifiers, 0 if not. Return 1 means that TTR can be
1085 converted to TTL. Return -1 means that TTL can be converted to TTR but
1088 NPTRS is the number of pointers we can strip off and keep cool.
1089 This is used to permit (for aggr A, aggr B) A, B* to convert to A*,
1090 but to not permit B** to convert to A**.
1092 This should go away. Callers should use can_convert or something
1093 similar instead. (jason 17 Apr 1997) */
1096 comp_target_types (ttl
, ttr
, nptrs
)
1100 ttl
= TYPE_MAIN_VARIANT (ttl
);
1101 ttr
= TYPE_MAIN_VARIANT (ttr
);
1102 if (same_type_p (ttl
, ttr
))
1105 if (TREE_CODE (ttr
) != TREE_CODE (ttl
))
1108 if ((TREE_CODE (ttr
) == POINTER_TYPE
1109 || TREE_CODE (ttr
) == REFERENCE_TYPE
)
1110 /* If we get a pointer with nptrs == 0, we don't allow any tweaking
1111 of the type pointed to. This is necessary for reference init
1112 semantics. We won't get here from a previous call with nptrs == 1;
1113 for multi-level pointers we end up in comp_ptr_ttypes. */
1116 int is_ptr
= TREE_CODE (ttr
) == POINTER_TYPE
;
1118 ttl
= TREE_TYPE (ttl
);
1119 ttr
= TREE_TYPE (ttr
);
1123 if (TREE_CODE (ttl
) == UNKNOWN_TYPE
1124 || TREE_CODE (ttr
) == UNKNOWN_TYPE
)
1126 else if (TREE_CODE (ttl
) == VOID_TYPE
1127 && TREE_CODE (ttr
) != FUNCTION_TYPE
1128 && TREE_CODE (ttr
) != METHOD_TYPE
1129 && TREE_CODE (ttr
) != OFFSET_TYPE
)
1131 else if (TREE_CODE (ttr
) == VOID_TYPE
1132 && TREE_CODE (ttl
) != FUNCTION_TYPE
1133 && TREE_CODE (ttl
) != METHOD_TYPE
1134 && TREE_CODE (ttl
) != OFFSET_TYPE
)
1136 else if (TREE_CODE (ttl
) == POINTER_TYPE
1137 || TREE_CODE (ttl
) == ARRAY_TYPE
)
1139 if (comp_ptr_ttypes (ttl
, ttr
))
1141 else if (comp_ptr_ttypes (ttr
, ttl
))
1147 /* Const and volatile mean something different for function types,
1148 so the usual checks are not appropriate. */
1149 if (TREE_CODE (ttl
) == FUNCTION_TYPE
|| TREE_CODE (ttl
) == METHOD_TYPE
)
1150 return comp_target_types (ttl
, ttr
, nptrs
- 1);
1152 return comp_cv_target_types (ttl
, ttr
, nptrs
- 1);
1155 if (TREE_CODE (ttr
) == ARRAY_TYPE
)
1156 return comp_array_types (comp_target_types
, ttl
, ttr
, COMPARE_STRICT
);
1157 else if (TREE_CODE (ttr
) == FUNCTION_TYPE
|| TREE_CODE (ttr
) == METHOD_TYPE
)
1164 if (!same_type_p (TREE_TYPE (ttl
), TREE_TYPE (ttr
)))
1169 switch (comp_target_types (TREE_TYPE (ttl
), TREE_TYPE (ttr
), -1))
1178 argsl
= TYPE_ARG_TYPES (ttl
);
1179 argsr
= TYPE_ARG_TYPES (ttr
);
1181 /* Compare 'this' here, not in comp_target_parms. */
1182 if (TREE_CODE (ttr
) == METHOD_TYPE
)
1184 tree tl
= TYPE_METHOD_BASETYPE (ttl
);
1185 tree tr
= TYPE_METHOD_BASETYPE (ttr
);
1187 if (!same_or_base_type_p (tr
, tl
))
1189 if (same_or_base_type_p (tl
, tr
))
1195 argsl
= TREE_CHAIN (argsl
);
1196 argsr
= TREE_CHAIN (argsr
);
1199 switch (comp_target_parms (argsl
, argsr
))
1207 return saw_contra
? -1 : 1;
1210 else if (TREE_CODE (ttr
) == OFFSET_TYPE
)
1214 /* Contravariance: we can assign a pointer to base member to a pointer
1215 to derived member. Note difference from simple pointer case, where
1216 we can pass a pointer to derived to a pointer to base. */
1217 if (same_or_base_type_p (TYPE_OFFSET_BASETYPE (ttr
),
1218 TYPE_OFFSET_BASETYPE (ttl
)))
1220 else if (same_or_base_type_p (TYPE_OFFSET_BASETYPE (ttl
),
1221 TYPE_OFFSET_BASETYPE (ttr
)))
1231 ttl
= TREE_TYPE (ttl
);
1232 ttr
= TREE_TYPE (ttr
);
1234 if (TREE_CODE (ttl
) == POINTER_TYPE
1235 || TREE_CODE (ttl
) == ARRAY_TYPE
)
1237 if (comp_ptr_ttypes (ttl
, ttr
))
1243 if (comp_cv_target_types (ttl
, ttr
, nptrs
) == 1)
1248 else if (IS_AGGR_TYPE (ttl
))
1252 if (same_or_base_type_p (build_pointer_type (ttl
),
1253 build_pointer_type (ttr
)))
1255 if (same_or_base_type_p (build_pointer_type (ttr
),
1256 build_pointer_type (ttl
)))
1264 /* Returns 1 if TYPE1 is at least as qualified as TYPE2. */
1267 at_least_as_qualified_p (type1
, type2
)
1271 /* All qualifiers for TYPE2 must also appear in TYPE1. */
1272 return ((cp_type_quals (type1
) & cp_type_quals (type2
))
1273 == cp_type_quals (type2
));
1276 /* Returns 1 if TYPE1 is more qualified than TYPE2. */
1279 more_qualified_p (type1
, type2
)
1283 return (cp_type_quals (type1
) != cp_type_quals (type2
)
1284 && at_least_as_qualified_p (type1
, type2
));
1287 /* Returns 1 if TYPE1 is more cv-qualified than TYPE2, -1 if TYPE2 is
1288 more cv-qualified that TYPE1, and 0 otherwise. */
1291 comp_cv_qualification (type1
, type2
)
1295 if (cp_type_quals (type1
) == cp_type_quals (type2
))
1298 if (at_least_as_qualified_p (type1
, type2
))
1301 else if (at_least_as_qualified_p (type2
, type1
))
1307 /* Returns 1 if the cv-qualification signature of TYPE1 is a proper
1308 subset of the cv-qualification signature of TYPE2, and the types
1309 are similar. Returns -1 if the other way 'round, and 0 otherwise. */
1312 comp_cv_qual_signature (type1
, type2
)
1316 if (comp_ptr_ttypes_real (type2
, type1
, -1))
1318 else if (comp_ptr_ttypes_real (type1
, type2
, -1))
1324 /* If two types share a common base type, return that basetype.
1325 If there is not a unique most-derived base type, this function
1326 returns ERROR_MARK_NODE. */
1329 common_base_type (tt1
, tt2
)
1332 tree best
= NULL_TREE
;
1335 /* If one is a baseclass of another, that's good enough. */
1336 if (UNIQUELY_DERIVED_FROM_P (tt1
, tt2
))
1338 if (UNIQUELY_DERIVED_FROM_P (tt2
, tt1
))
1341 /* Otherwise, try to find a unique baseclass of TT1
1342 that is shared by TT2, and follow that down. */
1343 for (i
= CLASSTYPE_N_BASECLASSES (tt1
)-1; i
>= 0; i
--)
1345 tree basetype
= TYPE_BINFO_BASETYPE (tt1
, i
);
1346 tree trial
= common_base_type (basetype
, tt2
);
1349 if (trial
== error_mark_node
)
1351 if (best
== NULL_TREE
)
1353 else if (best
!= trial
)
1354 return error_mark_node
;
1359 for (i
= CLASSTYPE_N_BASECLASSES (tt2
)-1; i
>= 0; i
--)
1361 tree basetype
= TYPE_BINFO_BASETYPE (tt2
, i
);
1362 tree trial
= common_base_type (tt1
, basetype
);
1365 if (trial
== error_mark_node
)
1367 if (best
== NULL_TREE
)
1369 else if (best
!= trial
)
1370 return error_mark_node
;
1376 /* Subroutines of `comptypes'. */
1378 /* Return 1 if two parameter type lists PARMS1 and PARMS2 are
1379 equivalent in the sense that functions with those parameter types
1380 can have equivalent types. The two lists must be equivalent,
1383 C++: See comment above about TYPE1, TYPE2. */
1386 compparms (parms1
, parms2
)
1387 tree parms1
, parms2
;
1389 register tree t1
= parms1
, t2
= parms2
;
1391 /* An unspecified parmlist matches any specified parmlist
1392 whose argument types don't need default promotions. */
1396 if (t1
== 0 && t2
== 0)
1398 /* If one parmlist is shorter than the other,
1399 they fail to match. */
1400 if (t1
== 0 || t2
== 0)
1402 if (!same_type_p (TREE_VALUE (t2
), TREE_VALUE (t1
)))
1405 t1
= TREE_CHAIN (t1
);
1406 t2
= TREE_CHAIN (t2
);
1410 /* This really wants return whether or not parameter type lists
1411 would make their owning functions assignment compatible or not.
1413 The return value is like for comp_target_types.
1415 This should go away, possibly with the exception of the empty parmlist
1416 conversion; there are no conversions between function types in C++.
1417 (jason 17 Apr 1997) */
1420 comp_target_parms (parms1
, parms2
)
1421 tree parms1
, parms2
;
1423 register tree t1
= parms1
, t2
= parms2
;
1424 int warn_contravariance
= 0;
1426 /* In C, an unspecified parmlist matches any specified parmlist
1427 whose argument types don't need default promotions. This is not
1428 true for C++, but let's do it anyway for unfixed headers. */
1430 if (t1
== 0 && t2
!= 0)
1432 pedwarn ("ISO C++ prohibits conversion from `%#T' to `(...)'",
1434 return self_promoting_args_p (t2
);
1437 return self_promoting_args_p (t1
);
1439 for (; t1
|| t2
; t1
= TREE_CHAIN (t1
), t2
= TREE_CHAIN (t2
))
1443 /* If one parmlist is shorter than the other,
1444 they fail to match, unless STRICT is <= 0. */
1445 if (t1
== 0 || t2
== 0)
1447 p1
= TREE_VALUE (t1
);
1448 p2
= TREE_VALUE (t2
);
1449 if (same_type_p (p1
, p2
))
1455 if ((TREE_CODE (p1
) == POINTER_TYPE
&& TREE_CODE (p2
) == POINTER_TYPE
)
1456 || (TREE_CODE (p1
) == REFERENCE_TYPE
1457 && TREE_CODE (p2
) == REFERENCE_TYPE
))
1459 /* The following is wrong for contravariance,
1460 but many programs depend on it. */
1461 if (TREE_TYPE (p1
) == void_type_node
)
1463 if (TREE_TYPE (p2
) == void_type_node
)
1465 warn_contravariance
= 1;
1468 if (IS_AGGR_TYPE (TREE_TYPE (p1
))
1469 && !same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (p1
),
1473 /* Note backwards order due to contravariance. */
1474 if (comp_target_types (p2
, p1
, 1) <= 0)
1476 if (comp_target_types (p1
, p2
, 1) > 0)
1478 warn_contravariance
= 1;
1484 return warn_contravariance
? -1 : 1;
1487 /* Compute the value of the `sizeof' operator. */
1493 enum tree_code code
= TREE_CODE (type
);
1496 if (processing_template_decl
)
1497 return build_min_nt (SIZEOF_EXPR
, type
);
1499 if (code
== FUNCTION_TYPE
)
1501 if (pedantic
|| warn_pointer_arith
)
1502 pedwarn ("ISO C++ forbids applying `sizeof' to a function type");
1503 size
= size_one_node
;
1505 else if (code
== METHOD_TYPE
)
1507 if (pedantic
|| warn_pointer_arith
)
1508 pedwarn ("ISO C++ forbids applying `sizeof' to a member function");
1509 size
= size_one_node
;
1511 else if (code
== VOID_TYPE
)
1513 if (pedantic
|| warn_pointer_arith
)
1514 pedwarn ("ISO C++ forbids applying `sizeof' to type `void' which is an incomplete type");
1515 size
= size_one_node
;
1517 else if (code
== ERROR_MARK
)
1518 size
= size_one_node
;
1521 /* ARM $5.3.2: ``When applied to a reference, the result is the
1522 size of the referenced object.'' */
1523 if (code
== REFERENCE_TYPE
)
1524 type
= TREE_TYPE (type
);
1526 if (code
== OFFSET_TYPE
)
1528 error ("`sizeof' applied to non-static member");
1529 size
= size_zero_node
;
1531 else if (!COMPLETE_TYPE_P (complete_type (type
)))
1533 error ("`sizeof' applied to incomplete type `%T'", type
);
1534 size
= size_zero_node
;
1537 /* Convert in case a char is more than one unit. */
1538 size
= size_binop (CEIL_DIV_EXPR
, TYPE_SIZE_UNIT (type
),
1539 size_int (TYPE_PRECISION (char_type_node
)
1543 /* SIZE will have an integer type with TYPE_IS_SIZETYPE set.
1544 TYPE_IS_SIZETYPE means that certain things (like overflow) will
1545 never happen. However, this node should really have type
1546 `size_t', which is just a typedef for an ordinary integer type. */
1547 size
= fold (build1 (NOP_EXPR
, c_size_type_node
, size
));
1548 my_friendly_assert (!TYPE_IS_SIZETYPE (TREE_TYPE (size
)),
1558 if (processing_template_decl
)
1559 return build_min_nt (SIZEOF_EXPR
, e
);
1561 if (TREE_CODE (e
) == COMPONENT_REF
1562 && DECL_C_BIT_FIELD (TREE_OPERAND (e
, 1)))
1563 error ("sizeof applied to a bit-field");
1564 if (is_overloaded_fn (e
))
1566 pedwarn ("ISO C++ forbids applying `sizeof' to an expression of function type");
1567 return c_sizeof (char_type_node
);
1569 else if (type_unknown_p (e
))
1571 incomplete_type_error (e
, TREE_TYPE (e
));
1572 return c_sizeof (char_type_node
);
1574 /* It's illegal to say `sizeof (X::i)' for `i' a non-static data
1575 member unless you're in a non-static member of X. So hand off to
1576 resolve_offset_ref. [expr.prim] */
1577 else if (TREE_CODE (e
) == OFFSET_REF
)
1578 e
= resolve_offset_ref (e
);
1580 if (e
== error_mark_node
)
1583 return c_sizeof (TREE_TYPE (e
));
1587 c_sizeof_nowarn (type
)
1590 enum tree_code code
= TREE_CODE (type
);
1593 if (code
== FUNCTION_TYPE
1594 || code
== METHOD_TYPE
1595 || code
== VOID_TYPE
1596 || code
== ERROR_MARK
)
1597 size
= size_one_node
;
1600 if (code
== REFERENCE_TYPE
)
1601 type
= TREE_TYPE (type
);
1603 if (!COMPLETE_TYPE_P (type
))
1604 size
= size_zero_node
;
1606 /* Convert in case a char is more than one unit. */
1607 size
= size_binop (CEIL_DIV_EXPR
, TYPE_SIZE_UNIT (type
),
1608 size_int (TYPE_PRECISION (char_type_node
)
1612 /* SIZE will have an integer type with TYPE_IS_SIZETYPE set.
1613 TYPE_IS_SIZETYPE means that certain things (like overflow) will
1614 never happen. However, this node should really have type
1615 `size_t', which is just a typedef for an ordinary integer type. */
1616 size
= fold (build1 (NOP_EXPR
, c_size_type_node
, size
));
1617 my_friendly_assert (!TYPE_IS_SIZETYPE (TREE_TYPE (size
)),
1622 /* Perform the array-to-pointer and function-to-pointer conversions
1625 In addition, references are converted to lvalues and manifest
1626 constants are replaced by their values. */
1629 decay_conversion (exp
)
1633 register enum tree_code code
;
1635 if (TREE_CODE (exp
) == OFFSET_REF
)
1636 exp
= resolve_offset_ref (exp
);
1638 type
= TREE_TYPE (exp
);
1639 code
= TREE_CODE (type
);
1641 if (code
== REFERENCE_TYPE
)
1643 exp
= convert_from_reference (exp
);
1644 type
= TREE_TYPE (exp
);
1645 code
= TREE_CODE (type
);
1648 if (type
== error_mark_node
)
1649 return error_mark_node
;
1651 if (type_unknown_p (exp
))
1653 incomplete_type_error (exp
, TREE_TYPE (exp
));
1654 return error_mark_node
;
1657 /* Constants can be used directly unless they're not loadable. */
1658 if (TREE_CODE (exp
) == CONST_DECL
)
1659 exp
= DECL_INITIAL (exp
);
1660 /* Replace a nonvolatile const static variable with its value. We
1661 don't do this for arrays, though; we want the address of the
1662 first element of the array, not the address of the first element
1663 of its initializing constant. */
1664 else if (code
!= ARRAY_TYPE
)
1666 exp
= decl_constant_value (exp
);
1667 type
= TREE_TYPE (exp
);
1670 /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
1671 Leave such NOP_EXPRs, since RHS is being used in non-lvalue context. */
1673 if (code
== VOID_TYPE
)
1675 error ("void value not ignored as it ought to be");
1676 return error_mark_node
;
1678 if (code
== METHOD_TYPE
)
1680 if (code
== FUNCTION_TYPE
|| is_overloaded_fn (exp
))
1681 return build_unary_op (ADDR_EXPR
, exp
, 0);
1682 if (code
== ARRAY_TYPE
)
1687 if (TREE_CODE (exp
) == INDIRECT_REF
)
1689 /* Stripping away the INDIRECT_REF is not the right
1690 thing to do for references... */
1691 tree inner
= TREE_OPERAND (exp
, 0);
1692 if (TREE_CODE (TREE_TYPE (inner
)) == REFERENCE_TYPE
)
1694 inner
= build1 (CONVERT_EXPR
,
1695 build_pointer_type (TREE_TYPE
1696 (TREE_TYPE (inner
))),
1698 TREE_CONSTANT (inner
) = TREE_CONSTANT (TREE_OPERAND (inner
, 0));
1700 return cp_convert (build_pointer_type (TREE_TYPE (type
)), inner
);
1703 if (TREE_CODE (exp
) == COMPOUND_EXPR
)
1705 tree op1
= decay_conversion (TREE_OPERAND (exp
, 1));
1706 return build (COMPOUND_EXPR
, TREE_TYPE (op1
),
1707 TREE_OPERAND (exp
, 0), op1
);
1711 && ! (TREE_CODE (exp
) == CONSTRUCTOR
&& TREE_STATIC (exp
)))
1713 error ("invalid use of non-lvalue array");
1714 return error_mark_node
;
1717 ptrtype
= build_pointer_type (TREE_TYPE (type
));
1719 if (TREE_CODE (exp
) == VAR_DECL
)
1721 /* ??? This is not really quite correct
1722 in that the type of the operand of ADDR_EXPR
1723 is not the target type of the type of the ADDR_EXPR itself.
1724 Question is, can this lossage be avoided? */
1725 adr
= build1 (ADDR_EXPR
, ptrtype
, exp
);
1726 if (mark_addressable (exp
) == 0)
1727 return error_mark_node
;
1728 TREE_CONSTANT (adr
) = staticp (exp
);
1729 TREE_SIDE_EFFECTS (adr
) = 0; /* Default would be, same as EXP. */
1732 /* This way is better for a COMPONENT_REF since it can
1733 simplify the offset for a component. */
1734 adr
= build_unary_op (ADDR_EXPR
, exp
, 1);
1735 return cp_convert (ptrtype
, adr
);
1738 /* [basic.lval]: Class rvalues can have cv-qualified types; non-class
1739 rvalues always have cv-unqualified types. */
1740 if (! CLASS_TYPE_P (type
))
1741 exp
= cp_convert (TYPE_MAIN_VARIANT (type
), exp
);
1747 default_conversion (exp
)
1751 enum tree_code code
;
1753 exp
= decay_conversion (exp
);
1755 type
= TREE_TYPE (exp
);
1756 code
= TREE_CODE (type
);
1758 if (INTEGRAL_CODE_P (code
))
1760 tree t
= type_promotes_to (type
);
1762 return cp_convert (t
, exp
);
1768 /* Take the address of an inline function without setting TREE_ADDRESSABLE
1772 inline_conversion (exp
)
1775 if (TREE_CODE (exp
) == FUNCTION_DECL
)
1776 exp
= build1 (ADDR_EXPR
, build_pointer_type (TREE_TYPE (exp
)), exp
);
1781 /* Returns nonzero iff exp is a STRING_CST or the result of applying
1782 decay_conversion to one. */
1785 string_conv_p (totype
, exp
, warn
)
1791 if (! flag_const_strings
|| TREE_CODE (totype
) != POINTER_TYPE
)
1794 t
= TREE_TYPE (totype
);
1795 if (!same_type_p (t
, char_type_node
)
1796 && !same_type_p (t
, wchar_type_node
))
1799 if (TREE_CODE (exp
) == STRING_CST
)
1801 /* Make sure that we don't try to convert between char and wchar_t. */
1802 if (!same_type_p (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (exp
))), t
))
1807 /* Is this a string constant which has decayed to 'const char *'? */
1808 t
= build_pointer_type (build_qualified_type (t
, TYPE_QUAL_CONST
));
1809 if (!same_type_p (TREE_TYPE (exp
), t
))
1812 if (TREE_CODE (exp
) != ADDR_EXPR
1813 || TREE_CODE (TREE_OPERAND (exp
, 0)) != STRING_CST
)
1817 /* This warning is not very useful, as it complains about printf. */
1818 if (warn
&& warn_write_strings
)
1819 warning ("deprecated conversion from string constant to `%T'", totype
);
1825 build_object_ref (datum
, basetype
, field
)
1826 tree datum
, basetype
, field
;
1829 if (datum
== error_mark_node
)
1830 return error_mark_node
;
1832 dtype
= TREE_TYPE (datum
);
1833 if (TREE_CODE (dtype
) == REFERENCE_TYPE
)
1834 dtype
= TREE_TYPE (dtype
);
1835 if (! IS_AGGR_TYPE_CODE (TREE_CODE (dtype
)))
1837 error ("request for member `%T::%D' in expression of non-aggregate type `%T'",
1838 basetype
, field
, dtype
);
1839 return error_mark_node
;
1841 else if (is_aggr_type (basetype
, 1))
1843 tree binfo
= binfo_or_else (basetype
, dtype
);
1845 return build_x_component_ref (build_scoped_ref (datum
, basetype
),
1848 return error_mark_node
;
1851 /* Like `build_component_ref, but uses an already found field, and converts
1852 from a reference. Must compute access for current_class_ref.
1856 build_component_ref_1 (datum
, field
, protect
)
1860 return convert_from_reference
1861 (build_component_ref (datum
, field
, NULL_TREE
, protect
));
1864 /* Given a COND_EXPR, MIN_EXPR, or MAX_EXPR in T, return it in a form that we
1865 can, for example, use as an lvalue. This code used to be in
1866 unary_complex_lvalue, but we needed it to deal with `a = (d == c) ? b : c'
1867 expressions, where we're dealing with aggregates. But now it's again only
1868 called from unary_complex_lvalue. The case (in particular) that led to
1869 this was with CODE == ADDR_EXPR, since it's not an lvalue when we'd
1873 rationalize_conditional_expr (code
, t
)
1874 enum tree_code code
;
1877 /* For MIN_EXPR or MAX_EXPR, fold-const.c has arranged things so that
1878 the first operand is always the one to be used if both operands
1879 are equal, so we know what conditional expression this used to be. */
1880 if (TREE_CODE (t
) == MIN_EXPR
|| TREE_CODE (t
) == MAX_EXPR
)
1883 build_conditional_expr (build_x_binary_op ((TREE_CODE (t
) == MIN_EXPR
1884 ? LE_EXPR
: GE_EXPR
),
1885 TREE_OPERAND (t
, 0),
1886 TREE_OPERAND (t
, 1)),
1887 build_unary_op (code
, TREE_OPERAND (t
, 0), 0),
1888 build_unary_op (code
, TREE_OPERAND (t
, 1), 0));
1892 build_conditional_expr (TREE_OPERAND (t
, 0),
1893 build_unary_op (code
, TREE_OPERAND (t
, 1), 0),
1894 build_unary_op (code
, TREE_OPERAND (t
, 2), 0));
1897 /* Given the TYPE of an anonymous union field inside T, return the
1898 FIELD_DECL for the field. If not found return NULL_TREE. Because
1899 anonymous unions can nest, we must also search all anonymous unions
1900 that are directly reachable. */
1903 lookup_anon_field (t
, type
)
1908 for (field
= TYPE_FIELDS (t
); field
; field
= TREE_CHAIN (field
))
1910 if (TREE_STATIC (field
))
1912 if (TREE_CODE (field
) != FIELD_DECL
)
1915 /* If we find it directly, return the field. */
1916 if (DECL_NAME (field
) == NULL_TREE
1917 && type
== TYPE_MAIN_VARIANT (TREE_TYPE (field
)))
1922 /* Otherwise, it could be nested, search harder. */
1923 if (DECL_NAME (field
) == NULL_TREE
1924 && ANON_AGGR_TYPE_P (TREE_TYPE (field
)))
1926 tree subfield
= lookup_anon_field (TREE_TYPE (field
), type
);
1934 /* Build a COMPONENT_REF for a given DATUM, and it's member COMPONENT.
1935 COMPONENT can be an IDENTIFIER_NODE that is the name of the member
1936 that we are interested in, or it can be a FIELD_DECL. */
1939 build_component_ref (datum
, component
, basetype_path
, protect
)
1940 tree datum
, component
, basetype_path
;
1943 register tree basetype
;
1944 register enum tree_code code
;
1945 register tree field
= NULL
;
1952 if (processing_template_decl
)
1953 return build_min_nt (COMPONENT_REF
, datum
, component
);
1955 if (datum
== error_mark_node
1956 || TREE_TYPE (datum
) == error_mark_node
)
1957 return error_mark_node
;
1959 /* BASETYPE holds the type of the class containing the COMPONENT. */
1960 basetype
= TYPE_MAIN_VARIANT (TREE_TYPE (datum
));
1962 /* If DATUM is a COMPOUND_EXPR or COND_EXPR, move our reference
1964 switch (TREE_CODE (datum
))
1968 tree value
= build_component_ref (TREE_OPERAND (datum
, 1), component
,
1969 basetype_path
, protect
);
1970 return build (COMPOUND_EXPR
, TREE_TYPE (value
),
1971 TREE_OPERAND (datum
, 0), value
);
1974 return build_conditional_expr
1975 (TREE_OPERAND (datum
, 0),
1976 build_component_ref (TREE_OPERAND (datum
, 1), component
,
1977 basetype_path
, protect
),
1978 build_component_ref (TREE_OPERAND (datum
, 2), component
,
1979 basetype_path
, protect
));
1982 error ("invalid use of `%D'", datum
);
1983 datum
= error_mark_node
;
1990 code
= TREE_CODE (basetype
);
1992 if (code
== REFERENCE_TYPE
)
1994 datum
= convert_from_reference (datum
);
1995 basetype
= TYPE_MAIN_VARIANT (TREE_TYPE (datum
));
1996 code
= TREE_CODE (basetype
);
1998 if (TREE_CODE (datum
) == OFFSET_REF
)
2000 datum
= resolve_offset_ref (datum
);
2001 basetype
= TYPE_MAIN_VARIANT (TREE_TYPE (datum
));
2002 code
= TREE_CODE (basetype
);
2005 /* First, see if there is a field or component with name COMPONENT. */
2006 if (TREE_CODE (component
) == TREE_LIST
)
2008 /* I could not trigger this code. MvL */
2011 my_friendly_assert (!(TREE_CHAIN (component
) == NULL_TREE
2012 && DECL_CHAIN (TREE_VALUE (component
)) == NULL_TREE
), 309);
2014 return build (COMPONENT_REF
, TREE_TYPE (component
), datum
, component
);
2017 if (! IS_AGGR_TYPE_CODE (code
))
2019 if (code
!= ERROR_MARK
)
2020 error ("request for member `%D' in `%E', which is of non-aggregate type `%T'",
2021 component
, datum
, basetype
);
2022 return error_mark_node
;
2025 if (!complete_type_or_else (basetype
, datum
))
2026 return error_mark_node
;
2028 if (TREE_CODE (component
) == BIT_NOT_EXPR
)
2030 if (TYPE_IDENTIFIER (basetype
) != TREE_OPERAND (component
, 0))
2032 error ("destructor specifier `%T::~%T' must have matching names",
2033 basetype
, TREE_OPERAND (component
, 0));
2034 return error_mark_node
;
2036 if (! TYPE_HAS_DESTRUCTOR (basetype
))
2038 error ("type `%T' has no destructor", basetype
);
2039 return error_mark_node
;
2041 return TREE_VEC_ELT (CLASSTYPE_METHOD_VEC (basetype
), 1);
2044 /* Look up component name in the structure type definition. */
2045 if (TYPE_VFIELD (basetype
)
2046 && DECL_NAME (TYPE_VFIELD (basetype
)) == component
)
2047 /* Special-case this because if we use normal lookups in an ambiguous
2048 hierarchy, the compiler will abort (because vptr lookups are
2049 not supposed to be ambiguous. */
2050 field
= TYPE_VFIELD (basetype
);
2051 else if (TREE_CODE (component
) == FIELD_DECL
)
2053 else if (TREE_CODE (component
) == TYPE_DECL
)
2055 error ("invalid use of type decl `%#D' as expression", component
);
2056 return error_mark_node
;
2058 else if (TREE_CODE (component
) == TEMPLATE_DECL
)
2060 error ("invalid use of template `%#D' as expression", component
);
2061 return error_mark_node
;
2065 tree name
= component
;
2067 if (TREE_CODE (component
) == TEMPLATE_ID_EXPR
)
2068 name
= TREE_OPERAND (component
, 0);
2069 else if (TREE_CODE (component
) == VAR_DECL
)
2070 name
= DECL_NAME (component
);
2071 if (TREE_CODE (component
) == NAMESPACE_DECL
)
2072 /* Source is in error, but produce a sensible diagnostic. */
2073 name
= DECL_NAME (component
);
2074 if (basetype_path
== NULL_TREE
)
2075 basetype_path
= TYPE_BINFO (basetype
);
2076 field
= lookup_field (basetype_path
, name
,
2077 protect
&& !VFIELD_NAME_P (name
), 0);
2078 if (field
== error_mark_node
)
2079 return error_mark_node
;
2081 if (field
== NULL_TREE
)
2083 /* Not found as a data field, look for it as a method. If found,
2084 then if this is the only possible one, return it, else
2085 report ambiguity error. */
2086 tree fndecls
= lookup_fnfields (basetype_path
, name
, 1);
2087 if (fndecls
== error_mark_node
)
2088 return error_mark_node
;
2091 /* If the function is unique and static, we can resolve it
2092 now. Otherwise, we have to wait and see what context it is
2093 used in; a component_ref involving a non-static member
2094 function can only be used in a call (expr.ref). */
2096 if (TREE_CHAIN (fndecls
) == NULL_TREE
2097 && TREE_CODE (TREE_VALUE (fndecls
)) == FUNCTION_DECL
)
2099 if (DECL_STATIC_FUNCTION_P (TREE_VALUE (fndecls
)))
2101 tree fndecl
= TREE_VALUE (fndecls
);
2102 enforce_access (basetype_path
, fndecl
);
2108 /* A unique non-static member function. Other parts
2109 of the compiler expect something with
2110 unknown_type_node to be really overloaded, so
2112 TREE_VALUE (fndecls
)
2113 = ovl_cons (TREE_VALUE (fndecls
), NULL_TREE
);
2117 fndecls
= TREE_VALUE (fndecls
);
2119 if (TREE_CODE (component
) == TEMPLATE_ID_EXPR
)
2120 fndecls
= build_nt (TEMPLATE_ID_EXPR
,
2121 fndecls
, TREE_OPERAND (component
, 1));
2123 ref
= build (COMPONENT_REF
, unknown_type_node
,
2128 error ("`%#T' has no member named `%D'", basetype
, name
);
2129 return error_mark_node
;
2131 else if (TREE_TYPE (field
) == error_mark_node
)
2132 return error_mark_node
;
2134 if (TREE_CODE (field
) != FIELD_DECL
)
2136 if (TREE_CODE (field
) == TYPE_DECL
)
2137 pedwarn ("invalid use of type decl `%#D' as expression", field
);
2138 else if (DECL_RTL (field
) != 0)
2141 TREE_USED (field
) = 1;
2143 /* Do evaluate the object when accessing a static member. */
2144 if (TREE_SIDE_EFFECTS (datum
))
2145 field
= build (COMPOUND_EXPR
, TREE_TYPE (field
), datum
, field
);
2151 if (TREE_DEPRECATED (field
))
2152 warn_deprecated_use (field
);
2155 old_basetype
= basetype
;
2157 /* See if we have to do any conversions so that we pick up the field from the
2159 if (DECL_FIELD_CONTEXT (field
) != basetype
)
2161 tree context
= DECL_FIELD_CONTEXT (field
);
2162 tree base
= context
;
2163 while (!same_type_p (base
, basetype
) && TYPE_NAME (base
)
2164 && ANON_AGGR_TYPE_P (base
))
2165 base
= TYPE_CONTEXT (base
);
2167 /* Handle base classes here... */
2168 if (base
!= basetype
&& TYPE_BASE_CONVS_MAY_REQUIRE_CODE_P (basetype
))
2171 tree binfo
= lookup_base (TREE_TYPE (datum
), base
, ba_check
, &kind
);
2173 /* Complain about use of offsetof which will break. */
2174 if (TREE_CODE (datum
) == INDIRECT_REF
2175 && integer_zerop (TREE_OPERAND (datum
, 0))
2176 && kind
== bk_via_virtual
)
2179 invalid offsetof from non-POD type `%#T'; use pointer to member instead",
2181 return error_mark_node
;
2183 datum
= build_base_path (PLUS_EXPR
, datum
, binfo
, 1);
2184 if (datum
== error_mark_node
)
2185 return error_mark_node
;
2189 /* Handle things from anon unions here... */
2190 if (TYPE_NAME (context
) && ANON_AGGR_TYPE_P (context
))
2192 tree subfield
= lookup_anon_field (basetype
, context
);
2193 tree subdatum
= build_component_ref (datum
, subfield
,
2194 basetype_path
, protect
);
2195 return build_component_ref (subdatum
, field
, basetype_path
, protect
);
2199 /* Complain about other invalid uses of offsetof, even though they will
2200 give the right answer. Note that we complain whether or not they
2201 actually used the offsetof macro, since there's no way to know at this
2202 point. So we just give a warning, instead of a pedwarn. */
2204 && CLASSTYPE_NON_POD_P (old_basetype
)
2205 && TREE_CODE (old_datum
) == INDIRECT_REF
2206 && integer_zerop (TREE_OPERAND (old_datum
, 0)))
2208 invalid offsetof from non-POD type `%#T'; use pointer to member instead",
2211 /* Compute the type of the field, as described in [expr.ref]. */
2212 type_quals
= TYPE_UNQUALIFIED
;
2213 field_type
= TREE_TYPE (field
);
2214 if (TREE_CODE (field_type
) == REFERENCE_TYPE
)
2215 /* The standard says that the type of the result should be the
2216 type referred to by the reference. But for now, at least, we
2217 do the conversion from reference type later. */
2221 type_quals
= (cp_type_quals (field_type
)
2222 | cp_type_quals (TREE_TYPE (datum
)));
2224 /* A field is const (volatile) if the enclosing object, or the
2225 field itself, is const (volatile). But, a mutable field is
2226 not const, even within a const object. */
2227 if (DECL_MUTABLE_P (field
))
2228 type_quals
&= ~TYPE_QUAL_CONST
;
2229 field_type
= cp_build_qualified_type (field_type
, type_quals
);
2232 ref
= fold (build (COMPONENT_REF
, field_type
, datum
, field
));
2234 /* Mark the expression const or volatile, as appropriate. Even
2235 though we've dealt with the type above, we still have to mark the
2236 expression itself. */
2237 if (type_quals
& TYPE_QUAL_CONST
)
2238 TREE_READONLY (ref
) = 1;
2239 else if (type_quals
& TYPE_QUAL_VOLATILE
)
2240 TREE_THIS_VOLATILE (ref
) = 1;
2245 /* Variant of build_component_ref for use in expressions, which should
2246 never have REFERENCE_TYPE. */
2249 build_x_component_ref (datum
, component
, basetype_path
, protect
)
2250 tree datum
, component
, basetype_path
;
2253 tree t
= build_component_ref (datum
, component
, basetype_path
, protect
);
2255 if (! processing_template_decl
)
2256 t
= convert_from_reference (t
);
2261 /* Given an expression PTR for a pointer, return an expression
2262 for the value pointed to.
2263 ERRORSTRING is the name of the operator to appear in error messages.
2265 This function may need to overload OPERATOR_FNNAME.
2266 Must also handle REFERENCE_TYPEs for C++. */
2269 build_x_indirect_ref (ptr
, errorstring
)
2271 const char *errorstring
;
2275 if (processing_template_decl
)
2276 return build_min_nt (INDIRECT_REF
, ptr
);
2278 rval
= build_opfncall (INDIRECT_REF
, LOOKUP_NORMAL
, ptr
, NULL_TREE
,
2282 return build_indirect_ref (ptr
, errorstring
);
2286 build_indirect_ref (ptr
, errorstring
)
2288 const char *errorstring
;
2290 register tree pointer
, type
;
2292 if (ptr
== error_mark_node
)
2293 return error_mark_node
;
2295 if (ptr
== current_class_ptr
)
2296 return current_class_ref
;
2298 pointer
= (TREE_CODE (TREE_TYPE (ptr
)) == REFERENCE_TYPE
2299 ? ptr
: default_conversion (ptr
));
2300 type
= TREE_TYPE (pointer
);
2302 if (TYPE_PTR_P (type
) || TREE_CODE (type
) == REFERENCE_TYPE
)
2306 If the type of the expression is "pointer to T," the type
2307 of the result is "T."
2309 We must use the canonical variant because certain parts of
2310 the back end, like fold, do pointer comparisons between
2312 tree t
= canonical_type_variant (TREE_TYPE (type
));
2314 if (VOID_TYPE_P (t
))
2316 /* A pointer to incomplete type (other than cv void) can be
2317 dereferenced [expr.unary.op]/1 */
2318 error ("`%T' is not a pointer-to-object type", type
);
2319 return error_mark_node
;
2321 else if (TREE_CODE (pointer
) == ADDR_EXPR
2323 && same_type_p (t
, TREE_TYPE (TREE_OPERAND (pointer
, 0))))
2324 /* The POINTER was something like `&x'. We simplify `*&x' to
2326 return TREE_OPERAND (pointer
, 0);
2329 tree ref
= build1 (INDIRECT_REF
, t
, pointer
);
2331 /* We *must* set TREE_READONLY when dereferencing a pointer to const,
2332 so that we get the proper error message if the result is used
2333 to assign to. Also, &* is supposed to be a no-op. */
2334 TREE_READONLY (ref
) = CP_TYPE_CONST_P (t
);
2335 TREE_THIS_VOLATILE (ref
) = CP_TYPE_VOLATILE_P (t
);
2336 TREE_SIDE_EFFECTS (ref
)
2337 = (TREE_THIS_VOLATILE (ref
) || TREE_SIDE_EFFECTS (pointer
)
2342 /* `pointer' won't be an error_mark_node if we were given a
2343 pointer to member, so it's cool to check for this here. */
2344 else if (TYPE_PTRMEM_P (type
) || TYPE_PTRMEMFUNC_P (type
))
2345 error ("invalid use of `%s' on pointer to member", errorstring
);
2346 else if (pointer
!= error_mark_node
)
2349 error ("invalid type argument of `%s'", errorstring
);
2351 error ("invalid type argument");
2353 return error_mark_node
;
2356 /* This handles expressions of the form "a[i]", which denotes
2359 This is logically equivalent in C to *(a+i), but we may do it differently.
2360 If A is a variable or a member, we generate a primitive ARRAY_REF.
2361 This avoids forcing the array out of registers, and can work on
2362 arrays that are not lvalues (for example, members of structures returned
2365 If INDEX is of some user-defined type, it must be converted to
2366 integer type. Otherwise, to make a compatible PLUS_EXPR, it
2367 will inherit the type of the array, which will be some pointer type. */
2370 build_array_ref (array
, idx
)
2375 error ("subscript missing in array reference");
2376 return error_mark_node
;
2379 if (TREE_TYPE (array
) == error_mark_node
2380 || TREE_TYPE (idx
) == error_mark_node
)
2381 return error_mark_node
;
2383 /* If ARRAY is a COMPOUND_EXPR or COND_EXPR, move our reference
2385 switch (TREE_CODE (array
))
2389 tree value
= build_array_ref (TREE_OPERAND (array
, 1), idx
);
2390 return build (COMPOUND_EXPR
, TREE_TYPE (value
),
2391 TREE_OPERAND (array
, 0), value
);
2395 return build_conditional_expr
2396 (TREE_OPERAND (array
, 0),
2397 build_array_ref (TREE_OPERAND (array
, 1), idx
),
2398 build_array_ref (TREE_OPERAND (array
, 2), idx
));
2404 if (TREE_CODE (TREE_TYPE (array
)) == ARRAY_TYPE
2405 && TREE_CODE (array
) != INDIRECT_REF
)
2409 /* Subscripting with type char is likely to lose
2410 on a machine where chars are signed.
2411 So warn on any machine, but optionally.
2412 Don't warn for unsigned char since that type is safe.
2413 Don't warn for signed char because anyone who uses that
2414 must have done so deliberately. */
2415 if (warn_char_subscripts
2416 && TYPE_MAIN_VARIANT (TREE_TYPE (idx
)) == char_type_node
)
2417 warning ("array subscript has type `char'");
2419 /* Apply default promotions *after* noticing character types. */
2420 idx
= default_conversion (idx
);
2422 if (TREE_CODE (TREE_TYPE (idx
)) != INTEGER_TYPE
)
2424 error ("array subscript is not an integer");
2425 return error_mark_node
;
2428 /* An array that is indexed by a non-constant
2429 cannot be stored in a register; we must be able to do
2430 address arithmetic on its address.
2431 Likewise an array of elements of variable size. */
2432 if (TREE_CODE (idx
) != INTEGER_CST
2433 || (COMPLETE_TYPE_P (TREE_TYPE (TREE_TYPE (array
)))
2434 && (TREE_CODE (TYPE_SIZE (TREE_TYPE (TREE_TYPE (array
))))
2437 if (mark_addressable (array
) == 0)
2438 return error_mark_node
;
2441 /* An array that is indexed by a constant value which is not within
2442 the array bounds cannot be stored in a register either; because we
2443 would get a crash in store_bit_field/extract_bit_field when trying
2444 to access a non-existent part of the register. */
2445 if (TREE_CODE (idx
) == INTEGER_CST
2446 && TYPE_VALUES (TREE_TYPE (array
))
2447 && ! int_fits_type_p (idx
, TYPE_VALUES (TREE_TYPE (array
))))
2449 if (mark_addressable (array
) == 0)
2450 return error_mark_node
;
2453 if (pedantic
&& !lvalue_p (array
))
2454 pedwarn ("ISO C++ forbids subscripting non-lvalue array");
2456 /* Note in C++ it is valid to subscript a `register' array, since
2457 it is valid to take the address of something with that
2458 storage specification. */
2462 while (TREE_CODE (foo
) == COMPONENT_REF
)
2463 foo
= TREE_OPERAND (foo
, 0);
2464 if (TREE_CODE (foo
) == VAR_DECL
&& DECL_REGISTER (foo
))
2465 warning ("subscripting array declared `register'");
2468 type
= TREE_TYPE (TREE_TYPE (array
));
2469 rval
= build (ARRAY_REF
, type
, array
, idx
);
2470 /* Array ref is const/volatile if the array elements are
2471 or if the array is.. */
2472 TREE_READONLY (rval
)
2473 |= (CP_TYPE_CONST_P (type
) | TREE_READONLY (array
));
2474 TREE_SIDE_EFFECTS (rval
)
2475 |= (CP_TYPE_VOLATILE_P (type
) | TREE_SIDE_EFFECTS (array
));
2476 TREE_THIS_VOLATILE (rval
)
2477 |= (CP_TYPE_VOLATILE_P (type
) | TREE_THIS_VOLATILE (array
));
2478 return require_complete_type (fold (rval
));
2482 tree ar
= default_conversion (array
);
2483 tree ind
= default_conversion (idx
);
2485 /* Put the integer in IND to simplify error checking. */
2486 if (TREE_CODE (TREE_TYPE (ar
)) == INTEGER_TYPE
)
2493 if (ar
== error_mark_node
)
2496 if (TREE_CODE (TREE_TYPE (ar
)) != POINTER_TYPE
)
2498 error ("subscripted value is neither array nor pointer");
2499 return error_mark_node
;
2501 if (TREE_CODE (TREE_TYPE (ind
)) != INTEGER_TYPE
)
2503 error ("array subscript is not an integer");
2504 return error_mark_node
;
2507 return build_indirect_ref (cp_build_binary_op (PLUS_EXPR
, ar
, ind
),
2512 /* Build a function call to function FUNCTION with parameters PARAMS.
2513 PARAMS is a list--a chain of TREE_LIST nodes--in which the
2514 TREE_VALUE of each node is a parameter-expression. The PARAMS do
2515 not include any object pointer that may be required. FUNCTION's
2516 data type may be a function type or a pointer-to-function.
2518 For C++: If FUNCTION's data type is a TREE_LIST, then the tree list
2519 is the list of possible methods that FUNCTION could conceivably
2520 be. If the list of methods comes from a class, then it will be
2521 a list of lists (where each element is associated with the class
2522 that produced it), otherwise it will be a simple list (for
2523 functions overloaded in global scope).
2525 In the first case, TREE_VALUE (function) is the head of one of those
2526 lists, and TREE_PURPOSE is the name of the function.
2528 In the second case, TREE_PURPOSE (function) is the function's
2531 DECL is the class instance variable, usually CURRENT_CLASS_REF.
2533 When calling a TEMPLATE_DECL, we don't require a complete return
2537 build_x_function_call (function
, params
, decl
)
2538 tree function
, params
, decl
;
2541 tree template_id
= NULL_TREE
;
2544 if (function
== error_mark_node
)
2545 return error_mark_node
;
2547 if (processing_template_decl
)
2548 return build_min_nt (CALL_EXPR
, function
, params
, NULL_TREE
);
2550 /* Save explicit template arguments if found */
2551 if (TREE_CODE (function
) == TEMPLATE_ID_EXPR
)
2553 template_id
= function
;
2554 function
= TREE_OPERAND (function
, 0);
2557 type
= TREE_TYPE (function
);
2559 if (TREE_CODE (type
) == OFFSET_TYPE
2560 && TREE_TYPE (type
) == unknown_type_node
2561 && TREE_CODE (function
) == TREE_LIST
2562 && TREE_CHAIN (function
) == NULL_TREE
)
2564 /* Undo (Foo:bar)()... */
2565 type
= TYPE_OFFSET_BASETYPE (type
);
2566 function
= TREE_VALUE (function
);
2567 my_friendly_assert (TREE_CODE (function
) == TREE_LIST
, 999);
2568 my_friendly_assert (TREE_CHAIN (function
) == NULL_TREE
, 999);
2569 function
= TREE_VALUE (function
);
2570 if (TREE_CODE (function
) == OVERLOAD
)
2571 function
= OVL_FUNCTION (function
);
2572 my_friendly_assert (TREE_CODE (function
) == FUNCTION_DECL
, 999);
2573 function
= DECL_NAME (function
);
2574 return build_method_call (decl
, function
, params
,
2575 TYPE_BINFO (type
), LOOKUP_NORMAL
);
2578 if (TREE_CODE (function
) == OFFSET_REF
2579 && TREE_CODE (type
) != METHOD_TYPE
)
2580 function
= resolve_offset_ref (function
);
2582 if ((TREE_CODE (function
) == FUNCTION_DECL
2583 && DECL_STATIC_FUNCTION_P (function
))
2584 || (DECL_FUNCTION_TEMPLATE_P (function
)
2585 && DECL_STATIC_FUNCTION_P (DECL_TEMPLATE_RESULT (function
))))
2586 return build_member_call (DECL_CONTEXT (function
),
2588 ? template_id
: DECL_NAME (function
),
2591 is_method
= ((TREE_CODE (function
) == TREE_LIST
2592 && current_class_type
!= NULL_TREE
2593 && (IDENTIFIER_CLASS_VALUE (TREE_PURPOSE (function
))
2595 || (TREE_CODE (function
) == OVERLOAD
2596 && DECL_FUNCTION_MEMBER_P (OVL_CURRENT (function
)))
2597 || TREE_CODE (function
) == IDENTIFIER_NODE
2598 || TREE_CODE (type
) == METHOD_TYPE
2599 || TYPE_PTRMEMFUNC_P (type
));
2601 /* A friend template. Make it look like a toplevel declaration. */
2602 if (! is_method
&& TREE_CODE (function
) == TEMPLATE_DECL
)
2603 function
= ovl_cons (function
, NULL_TREE
);
2605 /* Handle methods, friends, and overloaded functions, respectively. */
2608 tree basetype
= NULL_TREE
;
2610 if (TREE_CODE (function
) == OVERLOAD
)
2611 function
= OVL_CURRENT (function
);
2613 if (TREE_CODE (function
) == FUNCTION_DECL
2614 || DECL_FUNCTION_TEMPLATE_P (function
))
2616 basetype
= DECL_CONTEXT (function
);
2618 if (DECL_NAME (function
))
2619 function
= DECL_NAME (function
);
2621 function
= TYPE_IDENTIFIER (DECL_CONTEXT (function
));
2623 else if (TREE_CODE (function
) == TREE_LIST
)
2625 my_friendly_assert (TREE_CODE (TREE_VALUE (function
))
2626 == FUNCTION_DECL
, 312);
2627 basetype
= DECL_CONTEXT (TREE_VALUE (function
));
2628 function
= TREE_PURPOSE (function
);
2630 else if (TREE_CODE (function
) != IDENTIFIER_NODE
)
2632 if (TREE_CODE (function
) == OFFSET_REF
)
2634 if (TREE_OPERAND (function
, 0))
2635 decl
= TREE_OPERAND (function
, 0);
2637 /* Call via a pointer to member function. */
2638 if (decl
== NULL_TREE
)
2640 error ("pointer to member function called, but not in class scope");
2641 return error_mark_node
;
2643 /* What other type of POINTER_TYPE could this be? */
2644 if (TREE_CODE (TREE_TYPE (function
)) != POINTER_TYPE
2645 && ! TYPE_PTRMEMFUNC_P (TREE_TYPE (function
))
2646 && TREE_CODE (function
) != OFFSET_REF
)
2647 function
= build (OFFSET_REF
, TREE_TYPE (type
), NULL_TREE
,
2652 /* this is an abbreviated method call.
2653 must go through here in case it is a virtual function.
2654 @@ Perhaps this could be optimized. */
2656 if (basetype
&& (! current_class_type
2657 || ! DERIVED_FROM_P (basetype
, current_class_type
)))
2658 return build_member_call (basetype
, function
, params
);
2660 if (decl
== NULL_TREE
)
2662 if (current_class_type
== NULL_TREE
)
2664 error ("object missing in call to method `%D'", function
);
2665 return error_mark_node
;
2667 /* Yow: call from a static member function. */
2668 decl
= build_dummy_object (current_class_type
);
2671 /* Put back explicit template arguments, if any. */
2673 function
= template_id
;
2674 return build_method_call (decl
, function
, params
,
2675 NULL_TREE
, LOOKUP_NORMAL
);
2677 else if (TREE_CODE (function
) == COMPONENT_REF
2678 && type
== unknown_type_node
)
2680 /* Undo what we did in build_component_ref. */
2681 decl
= TREE_OPERAND (function
, 0);
2682 function
= TREE_OPERAND (function
, 1);
2684 if (TREE_CODE (function
) == TEMPLATE_ID_EXPR
)
2686 my_friendly_assert (!template_id
, 20011228);
2688 template_id
= function
;
2692 function
= DECL_NAME (OVL_CURRENT (function
));
2696 TREE_OPERAND (template_id
, 0) = function
;
2697 function
= template_id
;
2701 return build_method_call (decl
, function
, params
,
2702 NULL_TREE
, LOOKUP_NORMAL
);
2704 else if (really_overloaded_fn (function
))
2706 if (OVL_FUNCTION (function
) == NULL_TREE
)
2708 error ("function `%D' declared overloaded, but no definitions appear with which to resolve it?!?",
2709 TREE_PURPOSE (function
));
2710 return error_mark_node
;
2714 /* Put back explicit template arguments, if any. */
2716 function
= template_id
;
2717 return build_new_function_call (function
, params
);
2721 /* Remove a potential OVERLOAD around it */
2722 function
= OVL_CURRENT (function
);
2725 if (TREE_CODE (function
) == OFFSET_REF
)
2727 /* If the component is a data element (or a virtual function), we play
2728 games here to make things work. */
2731 if (TREE_OPERAND (function
, 0))
2732 decl
= TREE_OPERAND (function
, 0);
2734 decl
= current_class_ref
;
2736 decl_addr
= build_unary_op (ADDR_EXPR
, decl
, 0);
2738 /* Sigh. OFFSET_REFs are being used for too many things.
2739 They're being used both for -> and ->*, and we want to resolve
2740 the -> cases here, but leave the ->*. We could use
2741 resolve_offset_ref for those, too, but it would call
2742 get_member_function_from_ptrfunc and decl_addr wouldn't get
2743 updated properly. Nasty. */
2744 if (TREE_CODE (TREE_OPERAND (function
, 1)) == FIELD_DECL
)
2745 function
= resolve_offset_ref (function
);
2747 function
= TREE_OPERAND (function
, 1);
2749 function
= get_member_function_from_ptrfunc (&decl_addr
, function
);
2750 params
= tree_cons (NULL_TREE
, decl_addr
, params
);
2751 return build_function_call (function
, params
);
2754 type
= TREE_TYPE (function
);
2755 if (type
!= error_mark_node
)
2757 if (TREE_CODE (type
) == REFERENCE_TYPE
)
2758 type
= TREE_TYPE (type
);
2760 if (IS_AGGR_TYPE (type
))
2761 return build_opfncall (CALL_EXPR
, LOOKUP_NORMAL
, function
, params
, NULL_TREE
);
2766 tree fntype
= TREE_TYPE (function
);
2767 tree ctypeptr
= NULL_TREE
;
2769 /* Explicitly named method? */
2770 if (TREE_CODE (function
) == FUNCTION_DECL
)
2771 ctypeptr
= build_pointer_type (DECL_CLASS_CONTEXT (function
));
2772 /* Expression with ptr-to-method type? It could either be a plain
2773 usage, or it might be a case where the ptr-to-method is being
2774 passed in as an argument. */
2775 else if (TYPE_PTRMEMFUNC_P (fntype
))
2777 tree rec
= TYPE_METHOD_BASETYPE (TREE_TYPE
2778 (TYPE_PTRMEMFUNC_FN_TYPE (fntype
)));
2779 ctypeptr
= build_pointer_type (rec
);
2781 /* Unexpected node type? */
2784 if (decl
== NULL_TREE
)
2786 if (current_function_decl
2787 && DECL_STATIC_FUNCTION_P (current_function_decl
))
2788 error ("invalid call to member function needing `this' in static member function scope");
2790 error ("pointer to member function called, but not in class scope");
2791 return error_mark_node
;
2793 if (TREE_CODE (TREE_TYPE (decl
)) != POINTER_TYPE
2794 && ! TYPE_PTRMEMFUNC_P (TREE_TYPE (decl
)))
2796 tree binfo
= lookup_base (TREE_TYPE (decl
), TREE_TYPE (ctypeptr
),
2799 decl
= build_unary_op (ADDR_EXPR
, decl
, 0);
2800 decl
= build_base_path (PLUS_EXPR
, decl
, binfo
, 1);
2803 decl
= build_c_cast (ctypeptr
, decl
);
2804 params
= tree_cons (NULL_TREE
, decl
, params
);
2807 return build_function_call (function
, params
);
2810 /* Resolve a pointer to member function. INSTANCE is the object
2811 instance to use, if the member points to a virtual member. */
2814 get_member_function_from_ptrfunc (instance_ptrptr
, function
)
2815 tree
*instance_ptrptr
;
2818 if (TREE_CODE (function
) == OFFSET_REF
)
2819 function
= TREE_OPERAND (function
, 1);
2821 if (TYPE_PTRMEMFUNC_P (TREE_TYPE (function
)))
2823 tree fntype
, idx
, e1
, delta
, delta2
, e2
, e3
, vtbl
;
2824 tree instance
, basetype
;
2826 tree instance_ptr
= *instance_ptrptr
;
2828 if (instance_ptr
== error_mark_node
2829 && TREE_CODE (function
) == PTRMEM_CST
)
2831 /* Extracting the function address from a pmf is only
2832 allowed with -Wno-pmf-conversions. It only works for
2834 e1
= build_addr_func (PTRMEM_CST_MEMBER (function
));
2835 e1
= convert (TYPE_PTRMEMFUNC_FN_TYPE (TREE_TYPE (function
)), e1
);
2839 if (TREE_SIDE_EFFECTS (instance_ptr
))
2840 instance_ptr
= save_expr (instance_ptr
);
2842 if (TREE_SIDE_EFFECTS (function
))
2843 function
= save_expr (function
);
2845 fntype
= TYPE_PTRMEMFUNC_FN_TYPE (TREE_TYPE (function
));
2846 basetype
= TYPE_METHOD_BASETYPE (TREE_TYPE (fntype
));
2848 /* Convert down to the right base, before using the instance. */
2849 instance
= lookup_base (TREE_TYPE (TREE_TYPE (instance_ptr
)), basetype
,
2851 instance
= build_base_path (PLUS_EXPR
, instance_ptr
, instance
, 1);
2852 if (instance
== error_mark_node
&& instance_ptr
!= error_mark_node
)
2855 e3
= PFN_FROM_PTRMEMFUNC (function
);
2857 vtbl
= build1 (NOP_EXPR
, build_pointer_type (ptr_type_node
), instance
);
2858 TREE_CONSTANT (vtbl
) = TREE_CONSTANT (instance
);
2860 delta
= cp_convert (ptrdiff_type_node
,
2861 build_component_ref (function
, delta_identifier
,
2864 /* This used to avoid checking for virtual functions if basetype
2865 has no virtual functions, according to an earlier ANSI draft.
2866 With the final ISO C++ rules, such an optimization is
2867 incorrect: A pointer to a derived member can be static_cast
2868 to pointer-to-base-member, as long as the dynamic object
2869 later has the right member. */
2871 /* Promoting idx before saving it improves performance on RISC
2872 targets. Without promoting, the first compare used
2873 load-with-sign-extend, while the second used normal load then
2874 shift to sign-extend. An optimizer flaw, perhaps, but it's
2875 easier to make this change. */
2876 idx
= cp_build_binary_op (TRUNC_DIV_EXPR
,
2877 build1 (NOP_EXPR
, vtable_index_type
, e3
),
2878 TYPE_SIZE_UNIT (vtable_entry_type
));
2879 switch (TARGET_PTRMEMFUNC_VBIT_LOCATION
)
2881 case ptrmemfunc_vbit_in_pfn
:
2882 e1
= cp_build_binary_op (BIT_AND_EXPR
,
2883 build1 (NOP_EXPR
, vtable_index_type
, e3
),
2887 case ptrmemfunc_vbit_in_delta
:
2888 e1
= cp_build_binary_op (BIT_AND_EXPR
,
2889 delta
, integer_one_node
);
2890 delta
= cp_build_binary_op (RSHIFT_EXPR
,
2891 build1 (NOP_EXPR
, vtable_index_type
,
2900 /* DELTA2 is the amount by which to adjust the `this' pointer
2901 to find the vtbl. */
2905 build_pointer_type (build_pointer_type (vtable_entry_type
)),
2906 vtbl
, cp_convert (ptrdiff_type_node
, delta2
));
2907 vtbl
= build_indirect_ref (vtbl
, NULL
);
2908 e2
= build_array_ref (vtbl
, idx
);
2910 /* When using function descriptors, the address of the
2911 vtable entry is treated as a function pointer. */
2912 if (TARGET_VTABLE_USES_DESCRIPTORS
)
2913 e2
= build1 (NOP_EXPR
, TREE_TYPE (e2
),
2914 build_unary_op (ADDR_EXPR
, e2
, /*noconvert=*/1));
2916 TREE_TYPE (e2
) = TREE_TYPE (e3
);
2917 e1
= build_conditional_expr (e1
, e2
, e3
);
2919 /* Make sure this doesn't get evaluated first inside one of the
2920 branches of the COND_EXPR. */
2921 if (TREE_CODE (instance_ptr
) == SAVE_EXPR
)
2922 e1
= build (COMPOUND_EXPR
, TREE_TYPE (e1
),
2925 *instance_ptrptr
= build (PLUS_EXPR
, TREE_TYPE (instance_ptr
),
2926 instance_ptr
, delta
);
2928 if (instance_ptr
== error_mark_node
2929 && TREE_CODE (e1
) != ADDR_EXPR
2930 && TREE_CODE (TREE_OPERAND (e1
, 0)) != FUNCTION_DECL
)
2931 error ("object missing in `%E'", function
);
2939 build_function_call_real (function
, params
, require_complete
, flags
)
2940 tree function
, params
;
2941 int require_complete
, flags
;
2943 register tree fntype
, fndecl
;
2944 register tree value_type
;
2945 register tree coerced_params
;
2947 tree name
= NULL_TREE
, assembler_name
= NULL_TREE
;
2949 tree original
= function
;
2951 /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
2952 Strip such NOP_EXPRs, since FUNCTION is used in non-lvalue context. */
2953 if (TREE_CODE (function
) == NOP_EXPR
2954 && TREE_TYPE (function
) == TREE_TYPE (TREE_OPERAND (function
, 0)))
2955 function
= TREE_OPERAND (function
, 0);
2957 if (TREE_CODE (function
) == FUNCTION_DECL
)
2959 name
= DECL_NAME (function
);
2960 assembler_name
= DECL_ASSEMBLER_NAME (function
);
2962 mark_used (function
);
2965 /* Convert anything with function type to a pointer-to-function. */
2966 if (pedantic
&& DECL_MAIN_P (function
))
2967 pedwarn ("ISO C++ forbids calling `::main' from within program");
2969 /* Differs from default_conversion by not setting TREE_ADDRESSABLE
2970 (because calling an inline function does not mean the function
2971 needs to be separately compiled). */
2973 if (DECL_INLINE (function
))
2974 function
= inline_conversion (function
);
2976 function
= build_addr_func (function
);
2982 function
= build_addr_func (function
);
2985 if (function
== error_mark_node
)
2986 return error_mark_node
;
2988 fntype
= TREE_TYPE (function
);
2990 if (TYPE_PTRMEMFUNC_P (fntype
))
2992 error ("must use .* or ->* to call pointer-to-member function in `%E (...)'",
2994 return error_mark_node
;
2997 is_method
= (TREE_CODE (fntype
) == POINTER_TYPE
2998 && TREE_CODE (TREE_TYPE (fntype
)) == METHOD_TYPE
);
3000 if (!((TREE_CODE (fntype
) == POINTER_TYPE
3001 && TREE_CODE (TREE_TYPE (fntype
)) == FUNCTION_TYPE
)
3003 || TREE_CODE (function
) == TEMPLATE_ID_EXPR
))
3005 error ("`%E' cannot be used as a function", original
);
3006 return error_mark_node
;
3009 /* fntype now gets the type of function pointed to. */
3010 fntype
= TREE_TYPE (fntype
);
3012 /* Convert the parameters to the types declared in the
3013 function prototype, or apply default promotions. */
3015 if (flags
& LOOKUP_COMPLAIN
)
3016 coerced_params
= convert_arguments (TYPE_ARG_TYPES (fntype
),
3017 params
, fndecl
, LOOKUP_NORMAL
);
3019 coerced_params
= convert_arguments (TYPE_ARG_TYPES (fntype
),
3022 if (coerced_params
== error_mark_node
)
3024 if (flags
& LOOKUP_SPECULATIVELY
)
3027 return error_mark_node
;
3030 /* Check for errors in format strings. */
3033 check_function_format (NULL
, TYPE_ATTRIBUTES (fntype
), coerced_params
);
3035 /* Recognize certain built-in functions so we can make tree-codes
3036 other than CALL_EXPR. We do this when it enables fold-const.c
3037 to do something useful. */
3039 if (TREE_CODE (function
) == ADDR_EXPR
3040 && TREE_CODE (TREE_OPERAND (function
, 0)) == FUNCTION_DECL
3041 && DECL_BUILT_IN (TREE_OPERAND (function
, 0)))
3043 result
= expand_tree_builtin (TREE_OPERAND (function
, 0),
3044 params
, coerced_params
);
3049 /* Some built-in function calls will be evaluated at
3050 compile-time in fold (). */
3051 result
= fold (build_call (function
, coerced_params
));
3052 value_type
= TREE_TYPE (result
);
3054 if (require_complete
)
3056 if (TREE_CODE (value_type
) == VOID_TYPE
)
3058 result
= require_complete_type (result
);
3060 if (IS_AGGR_TYPE (value_type
))
3061 result
= build_cplus_new (value_type
, result
);
3062 return convert_from_reference (result
);
3066 build_function_call (function
, params
)
3067 tree function
, params
;
3069 return build_function_call_real (function
, params
, 1, LOOKUP_NORMAL
);
3072 /* Convert the actual parameter expressions in the list VALUES
3073 to the types in the list TYPELIST.
3074 If parmdecls is exhausted, or when an element has NULL as its type,
3075 perform the default conversions.
3077 NAME is an IDENTIFIER_NODE or 0. It is used only for error messages.
3079 This is also where warnings about wrong number of args are generated.
3081 Return a list of expressions for the parameters as converted.
3083 Both VALUES and the returned value are chains of TREE_LIST nodes
3084 with the elements of the list in the TREE_VALUE slots of those nodes.
3086 In C++, unspecified trailing parameters can be filled in with their
3087 default arguments, if such were specified. Do so here. */
3090 convert_arguments (typelist
, values
, fndecl
, flags
)
3091 tree typelist
, values
, fndecl
;
3094 register tree typetail
, valtail
;
3095 register tree result
= NULL_TREE
;
3096 const char *called_thing
= 0;
3099 /* Argument passing is always copy-initialization. */
3100 flags
|= LOOKUP_ONLYCONVERTING
;
3104 if (TREE_CODE (TREE_TYPE (fndecl
)) == METHOD_TYPE
)
3106 if (DECL_NAME (fndecl
) == NULL_TREE
3107 || IDENTIFIER_HAS_TYPE_VALUE (DECL_NAME (fndecl
)))
3108 called_thing
= "constructor";
3110 called_thing
= "member function";
3113 called_thing
= "function";
3116 for (valtail
= values
, typetail
= typelist
;
3118 valtail
= TREE_CHAIN (valtail
), i
++)
3120 register tree type
= typetail
? TREE_VALUE (typetail
) : 0;
3121 register tree val
= TREE_VALUE (valtail
);
3123 if (val
== error_mark_node
)
3124 return error_mark_node
;
3126 if (type
== void_type_node
)
3130 cp_error_at ("too many arguments to %s `%+#D'", called_thing
,
3132 error ("at this point in file");
3135 error ("too many arguments to function");
3136 /* In case anybody wants to know if this argument
3139 TREE_TYPE (tree_last (result
)) = error_mark_node
;
3143 if (TREE_CODE (val
) == OFFSET_REF
)
3144 val
= resolve_offset_ref (val
);
3146 /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
3147 Strip such NOP_EXPRs, since VAL is used in non-lvalue context. */
3148 if (TREE_CODE (val
) == NOP_EXPR
3149 && TREE_TYPE (val
) == TREE_TYPE (TREE_OPERAND (val
, 0))
3150 && (type
== 0 || TREE_CODE (type
) != REFERENCE_TYPE
))
3151 val
= TREE_OPERAND (val
, 0);
3153 if (type
== 0 || TREE_CODE (type
) != REFERENCE_TYPE
)
3155 if (TREE_CODE (TREE_TYPE (val
)) == ARRAY_TYPE
3156 || TREE_CODE (TREE_TYPE (val
)) == FUNCTION_TYPE
3157 || TREE_CODE (TREE_TYPE (val
)) == METHOD_TYPE
)
3158 val
= default_conversion (val
);
3161 if (val
== error_mark_node
)
3162 return error_mark_node
;
3166 /* Formal parm type is specified by a function prototype. */
3169 if (!COMPLETE_TYPE_P (complete_type (type
)))
3171 error ("parameter type of called function is incomplete");
3176 parmval
= convert_for_initialization
3177 (NULL_TREE
, type
, val
, flags
,
3178 "argument passing", fndecl
, i
);
3179 if (PROMOTE_PROTOTYPES
3180 && INTEGRAL_TYPE_P (type
)
3181 && (TYPE_PRECISION (type
)
3182 < TYPE_PRECISION (integer_type_node
)))
3183 parmval
= default_conversion (parmval
);
3186 if (parmval
== error_mark_node
)
3187 return error_mark_node
;
3189 result
= tree_cons (NULL_TREE
, parmval
, result
);
3193 if (TREE_CODE (TREE_TYPE (val
)) == REFERENCE_TYPE
)
3194 val
= convert_from_reference (val
);
3196 if (fndecl
&& DECL_BUILT_IN (fndecl
)
3197 && DECL_FUNCTION_CODE (fndecl
) == BUILT_IN_CONSTANT_P
)
3198 /* Don't do ellipsis conversion for __built_in_constant_p
3199 as this will result in spurious warnings for non-POD
3201 val
= require_complete_type (val
);
3203 val
= convert_arg_to_ellipsis (val
);
3205 result
= tree_cons (NULL_TREE
, val
, result
);
3209 typetail
= TREE_CHAIN (typetail
);
3212 if (typetail
!= 0 && typetail
!= void_list_node
)
3214 /* See if there are default arguments that can be used */
3215 if (TREE_PURPOSE (typetail
))
3217 for (; typetail
!= void_list_node
; ++i
)
3220 = convert_default_arg (TREE_VALUE (typetail
),
3221 TREE_PURPOSE (typetail
),
3224 if (parmval
== error_mark_node
)
3225 return error_mark_node
;
3227 result
= tree_cons (0, parmval
, result
);
3228 typetail
= TREE_CHAIN (typetail
);
3229 /* ends with `...'. */
3230 if (typetail
== NULL_TREE
)
3238 cp_error_at ("too few arguments to %s `%+#D'",
3239 called_thing
, fndecl
);
3240 error ("at this point in file");
3243 error ("too few arguments to function");
3244 return error_mark_list
;
3248 return nreverse (result
);
3251 /* Build a binary-operation expression, after performing default
3252 conversions on the operands. CODE is the kind of expression to build. */
3255 build_x_binary_op (code
, arg1
, arg2
)
3256 enum tree_code code
;
3259 if (processing_template_decl
)
3260 return build_min_nt (code
, arg1
, arg2
);
3262 return build_new_op (code
, LOOKUP_NORMAL
, arg1
, arg2
, NULL_TREE
);
3265 /* Build a binary-operation expression without default conversions.
3266 CODE is the kind of expression to build.
3267 This function differs from `build' in several ways:
3268 the data type of the result is computed and recorded in it,
3269 warnings are generated if arg data types are invalid,
3270 special handling for addition and subtraction of pointers is known,
3271 and some optimization is done (operations on narrow ints
3272 are done in the narrower type when that gives the same result).
3273 Constant folding is also done before the result is returned.
3275 Note that the operands will never have enumeral types
3276 because either they have just had the default conversions performed
3277 or they have both just been converted to some other type in which
3278 the arithmetic is to be done.
3280 C++: must do special pointer arithmetic when implementing
3281 multiple inheritance, and deal with pointer to member functions. */
3284 build_binary_op (code
, orig_op0
, orig_op1
, convert_p
)
3285 enum tree_code code
;
3286 tree orig_op0
, orig_op1
;
3287 int convert_p ATTRIBUTE_UNUSED
;
3290 register enum tree_code code0
, code1
;
3293 /* Expression code to give to the expression when it is built.
3294 Normally this is CODE, which is what the caller asked for,
3295 but in some special cases we change it. */
3296 register enum tree_code resultcode
= code
;
3298 /* Data type in which the computation is to be performed.
3299 In the simplest cases this is the common type of the arguments. */
3300 register tree result_type
= NULL
;
3302 /* Nonzero means operands have already been type-converted
3303 in whatever way is necessary.
3304 Zero means they need to be converted to RESULT_TYPE. */
3307 /* Nonzero means create the expression with this type, rather than
3309 tree build_type
= 0;
3311 /* Nonzero means after finally constructing the expression
3312 convert it to this type. */
3313 tree final_type
= 0;
3315 /* Nonzero if this is an operation like MIN or MAX which can
3316 safely be computed in short if both args are promoted shorts.
3317 Also implies COMMON.
3318 -1 indicates a bitwise operation; this makes a difference
3319 in the exact conditions for when it is safe to do the operation
3320 in a narrower mode. */
3323 /* Nonzero if this is a comparison operation;
3324 if both args are promoted shorts, compare the original shorts.
3325 Also implies COMMON. */
3326 int short_compare
= 0;
3328 /* Nonzero if this is a right-shift operation, which can be computed on the
3329 original short and then promoted if the operand is a promoted short. */
3330 int short_shift
= 0;
3332 /* Nonzero means set RESULT_TYPE to the common type of the args. */
3335 /* Apply default conversions. */
3339 if (code
== TRUTH_AND_EXPR
|| code
== TRUTH_ANDIF_EXPR
3340 || code
== TRUTH_OR_EXPR
|| code
== TRUTH_ORIF_EXPR
3341 || code
== TRUTH_XOR_EXPR
)
3343 if (!really_overloaded_fn (op0
))
3344 op0
= decay_conversion (op0
);
3345 if (!really_overloaded_fn (op1
))
3346 op1
= decay_conversion (op1
);
3350 if (!really_overloaded_fn (op0
))
3351 op0
= default_conversion (op0
);
3352 if (!really_overloaded_fn (op1
))
3353 op1
= default_conversion (op1
);
3356 /* Strip NON_LVALUE_EXPRs, etc., since we aren't using as an lvalue. */
3357 STRIP_TYPE_NOPS (op0
);
3358 STRIP_TYPE_NOPS (op1
);
3360 /* DTRT if one side is an overloaded function, but complain about it. */
3361 if (type_unknown_p (op0
))
3363 tree t
= instantiate_type (TREE_TYPE (op1
), op0
, tf_none
);
3364 if (t
!= error_mark_node
)
3366 pedwarn ("assuming cast to type `%T' from overloaded function",
3371 if (type_unknown_p (op1
))
3373 tree t
= instantiate_type (TREE_TYPE (op0
), op1
, tf_none
);
3374 if (t
!= error_mark_node
)
3376 pedwarn ("assuming cast to type `%T' from overloaded function",
3382 type0
= TREE_TYPE (op0
);
3383 type1
= TREE_TYPE (op1
);
3385 /* The expression codes of the data types of the arguments tell us
3386 whether the arguments are integers, floating, pointers, etc. */
3387 code0
= TREE_CODE (type0
);
3388 code1
= TREE_CODE (type1
);
3390 /* If an error was already reported for one of the arguments,
3391 avoid reporting another error. */
3393 if (code0
== ERROR_MARK
|| code1
== ERROR_MARK
)
3394 return error_mark_node
;
3399 /* Handle the pointer + int case. */
3400 if (code0
== POINTER_TYPE
&& code1
== INTEGER_TYPE
)
3401 return cp_pointer_int_sum (PLUS_EXPR
, op0
, op1
);
3402 else if (code1
== POINTER_TYPE
&& code0
== INTEGER_TYPE
)
3403 return cp_pointer_int_sum (PLUS_EXPR
, op1
, op0
);
3409 /* Subtraction of two similar pointers.
3410 We must subtract them as integers, then divide by object size. */
3411 if (code0
== POINTER_TYPE
&& code1
== POINTER_TYPE
3412 && comp_target_types (type0
, type1
, 1))
3413 return pointer_diff (op0
, op1
, common_type (type0
, type1
));
3414 /* Handle pointer minus int. Just like pointer plus int. */
3415 else if (code0
== POINTER_TYPE
&& code1
== INTEGER_TYPE
)
3416 return cp_pointer_int_sum (MINUS_EXPR
, op0
, op1
);
3425 case TRUNC_DIV_EXPR
:
3427 case FLOOR_DIV_EXPR
:
3428 case ROUND_DIV_EXPR
:
3429 case EXACT_DIV_EXPR
:
3430 if ((code0
== INTEGER_TYPE
|| code0
== REAL_TYPE
3431 || code0
== COMPLEX_TYPE
)
3432 && (code1
== INTEGER_TYPE
|| code1
== REAL_TYPE
3433 || code1
== COMPLEX_TYPE
))
3435 if (TREE_CODE (op1
) == INTEGER_CST
&& integer_zerop (op1
))
3436 warning ("division by zero in `%E / 0'", op0
);
3437 else if (TREE_CODE (op1
) == REAL_CST
&& real_zerop (op1
))
3438 warning ("division by zero in `%E / 0.'", op0
);
3440 if (!(code0
== INTEGER_TYPE
&& code1
== INTEGER_TYPE
))
3441 resultcode
= RDIV_EXPR
;
3443 /* When dividing two signed integers, we have to promote to int.
3444 unless we divide by a constant != -1. Note that default
3445 conversion will have been performed on the operands at this
3446 point, so we have to dig out the original type to find out if
3448 shorten
= ((TREE_CODE (op0
) == NOP_EXPR
3449 && TREE_UNSIGNED (TREE_TYPE (TREE_OPERAND (op0
, 0))))
3450 || (TREE_CODE (op1
) == INTEGER_CST
3451 && ! integer_all_onesp (op1
)));
3458 case BIT_ANDTC_EXPR
:
3461 if (code0
== INTEGER_TYPE
&& code1
== INTEGER_TYPE
)
3465 case TRUNC_MOD_EXPR
:
3466 case FLOOR_MOD_EXPR
:
3467 if (code1
== INTEGER_TYPE
&& integer_zerop (op1
))
3468 warning ("division by zero in `%E %% 0'", op0
);
3469 else if (code1
== REAL_TYPE
&& real_zerop (op1
))
3470 warning ("division by zero in `%E %% 0.'", op0
);
3472 if (code0
== INTEGER_TYPE
&& code1
== INTEGER_TYPE
)
3474 /* Although it would be tempting to shorten always here, that loses
3475 on some targets, since the modulo instruction is undefined if the
3476 quotient can't be represented in the computation mode. We shorten
3477 only if unsigned or if dividing by something we know != -1. */
3478 shorten
= ((TREE_CODE (op0
) == NOP_EXPR
3479 && TREE_UNSIGNED (TREE_TYPE (TREE_OPERAND (op0
, 0))))
3480 || (TREE_CODE (op1
) == INTEGER_CST
3481 && ! integer_all_onesp (op1
)));
3486 case TRUTH_ANDIF_EXPR
:
3487 case TRUTH_ORIF_EXPR
:
3488 case TRUTH_AND_EXPR
:
3490 result_type
= boolean_type_node
;
3493 /* Shift operations: result has same type as first operand;
3494 always convert second operand to int.
3495 Also set SHORT_SHIFT if shifting rightward. */
3498 if (code0
== INTEGER_TYPE
&& code1
== INTEGER_TYPE
)
3500 result_type
= type0
;
3501 if (TREE_CODE (op1
) == INTEGER_CST
)
3503 if (tree_int_cst_lt (op1
, integer_zero_node
))
3504 warning ("right shift count is negative");
3507 if (! integer_zerop (op1
))
3509 if (compare_tree_int (op1
, TYPE_PRECISION (type0
)) >= 0)
3510 warning ("right shift count >= width of type");
3513 /* Convert the shift-count to an integer, regardless of
3514 size of value being shifted. */
3515 if (TYPE_MAIN_VARIANT (TREE_TYPE (op1
)) != integer_type_node
)
3516 op1
= cp_convert (integer_type_node
, op1
);
3517 /* Avoid converting op1 to result_type later. */
3523 if (code0
== INTEGER_TYPE
&& code1
== INTEGER_TYPE
)
3525 result_type
= type0
;
3526 if (TREE_CODE (op1
) == INTEGER_CST
)
3528 if (tree_int_cst_lt (op1
, integer_zero_node
))
3529 warning ("left shift count is negative");
3530 else if (compare_tree_int (op1
, TYPE_PRECISION (type0
)) >= 0)
3531 warning ("left shift count >= width of type");
3533 /* Convert the shift-count to an integer, regardless of
3534 size of value being shifted. */
3535 if (TYPE_MAIN_VARIANT (TREE_TYPE (op1
)) != integer_type_node
)
3536 op1
= cp_convert (integer_type_node
, op1
);
3537 /* Avoid converting op1 to result_type later. */
3544 if (code0
== INTEGER_TYPE
&& code1
== INTEGER_TYPE
)
3546 result_type
= type0
;
3547 if (TREE_CODE (op1
) == INTEGER_CST
)
3549 if (tree_int_cst_lt (op1
, integer_zero_node
))
3550 warning ("%s rotate count is negative",
3551 (code
== LROTATE_EXPR
) ? "left" : "right");
3552 else if (compare_tree_int (op1
, TYPE_PRECISION (type0
)) >= 0)
3553 warning ("%s rotate count >= width of type",
3554 (code
== LROTATE_EXPR
) ? "left" : "right");
3556 /* Convert the shift-count to an integer, regardless of
3557 size of value being shifted. */
3558 if (TYPE_MAIN_VARIANT (TREE_TYPE (op1
)) != integer_type_node
)
3559 op1
= cp_convert (integer_type_node
, op1
);
3565 if (warn_float_equal
&& (code0
== REAL_TYPE
|| code1
== REAL_TYPE
))
3566 warning ("comparing floating point with == or != is unsafe");
3568 build_type
= boolean_type_node
;
3569 if ((code0
== INTEGER_TYPE
|| code0
== REAL_TYPE
3570 || code0
== COMPLEX_TYPE
)
3571 && (code1
== INTEGER_TYPE
|| code1
== REAL_TYPE
3572 || code1
== COMPLEX_TYPE
))
3574 else if (code0
== POINTER_TYPE
&& code1
== POINTER_TYPE
)
3575 result_type
= composite_pointer_type (type0
, type1
, op0
, op1
,
3577 else if (code0
== POINTER_TYPE
&& null_ptr_cst_p (op1
))
3578 result_type
= type0
;
3579 else if (code1
== POINTER_TYPE
&& null_ptr_cst_p (op0
))
3580 result_type
= type1
;
3581 else if (code0
== POINTER_TYPE
&& code1
== INTEGER_TYPE
)
3583 result_type
= type0
;
3584 error ("ISO C++ forbids comparison between pointer and integer");
3586 else if (code0
== INTEGER_TYPE
&& code1
== POINTER_TYPE
)
3588 result_type
= type1
;
3589 error ("ISO C++ forbids comparison between pointer and integer");
3591 else if (TYPE_PTRMEMFUNC_P (type0
) && null_ptr_cst_p (op1
))
3593 op0
= build_component_ref (op0
, pfn_identifier
, NULL_TREE
, 0);
3594 op1
= cp_convert (TREE_TYPE (op0
), integer_zero_node
);
3595 result_type
= TREE_TYPE (op0
);
3597 else if (TYPE_PTRMEMFUNC_P (type1
) && null_ptr_cst_p (op0
))
3598 return cp_build_binary_op (code
, op1
, op0
);
3599 else if (TYPE_PTRMEMFUNC_P (type0
) && TYPE_PTRMEMFUNC_P (type1
)
3600 && same_type_p (type0
, type1
))
3602 /* E will be the final comparison. */
3604 /* E1 and E2 are for scratch. */
3612 if (TREE_SIDE_EFFECTS (op0
))
3613 op0
= save_expr (op0
);
3614 if (TREE_SIDE_EFFECTS (op1
))
3615 op1
= save_expr (op1
);
3620 && (!op0.pfn || op0.delta == op1.delta))
3622 The reason for the `!op0.pfn' bit is that a NULL
3623 pointer-to-member is any member with a zero PFN; the
3624 DELTA field is unspecified. */
3625 pfn0
= pfn_from_ptrmemfunc (op0
);
3626 pfn1
= pfn_from_ptrmemfunc (op1
);
3627 delta0
= build_component_ref (op0
, delta_identifier
,
3629 delta1
= build_component_ref (op1
, delta_identifier
,
3631 e1
= cp_build_binary_op (EQ_EXPR
, delta0
, delta1
);
3632 e2
= cp_build_binary_op (EQ_EXPR
,
3634 cp_convert (TREE_TYPE (pfn0
),
3635 integer_zero_node
));
3636 e1
= cp_build_binary_op (TRUTH_ORIF_EXPR
, e1
, e2
);
3637 e2
= build (EQ_EXPR
, boolean_type_node
, pfn0
, pfn1
);
3638 e
= cp_build_binary_op (TRUTH_ANDIF_EXPR
, e2
, e1
);
3639 if (code
== EQ_EXPR
)
3641 return cp_build_binary_op (EQ_EXPR
, e
, integer_zero_node
);
3643 else if ((TYPE_PTRMEMFUNC_P (type0
)
3644 && same_type_p (TYPE_PTRMEMFUNC_FN_TYPE (type0
), type1
))
3645 || (TYPE_PTRMEMFUNC_P (type1
)
3646 && same_type_p (TYPE_PTRMEMFUNC_FN_TYPE (type1
), type0
)))
3652 if ((code0
== INTEGER_TYPE
|| code0
== REAL_TYPE
)
3653 && (code1
== INTEGER_TYPE
|| code1
== REAL_TYPE
))
3655 else if (code0
== POINTER_TYPE
&& code1
== POINTER_TYPE
)
3656 result_type
= composite_pointer_type (type0
, type1
, op0
, op1
,
3664 build_type
= boolean_type_node
;
3665 if ((code0
== INTEGER_TYPE
|| code0
== REAL_TYPE
)
3666 && (code1
== INTEGER_TYPE
|| code1
== REAL_TYPE
))
3668 else if (code0
== POINTER_TYPE
&& code1
== POINTER_TYPE
)
3669 result_type
= composite_pointer_type (type0
, type1
, op0
, op1
,
3671 else if (code0
== POINTER_TYPE
&& TREE_CODE (op1
) == INTEGER_CST
3672 && integer_zerop (op1
))
3673 result_type
= type0
;
3674 else if (code1
== POINTER_TYPE
&& TREE_CODE (op0
) == INTEGER_CST
3675 && integer_zerop (op0
))
3676 result_type
= type1
;
3677 else if (code0
== POINTER_TYPE
&& code1
== INTEGER_TYPE
)
3679 result_type
= type0
;
3680 pedwarn ("ISO C++ forbids comparison between pointer and integer");
3682 else if (code0
== INTEGER_TYPE
&& code1
== POINTER_TYPE
)
3684 result_type
= type1
;
3685 pedwarn ("ISO C++ forbids comparison between pointer and integer");
3689 case UNORDERED_EXPR
:
3696 build_type
= integer_type_node
;
3697 if (code0
!= REAL_TYPE
|| code1
!= REAL_TYPE
)
3699 error ("unordered comparison on non-floating point argument");
3700 return error_mark_node
;
3709 if ((code0
== INTEGER_TYPE
|| code0
== REAL_TYPE
|| code0
== COMPLEX_TYPE
)
3711 (code1
== INTEGER_TYPE
|| code1
== REAL_TYPE
|| code1
== COMPLEX_TYPE
))
3713 int none_complex
= (code0
!= COMPLEX_TYPE
&& code1
!= COMPLEX_TYPE
);
3715 if (shorten
|| common
|| short_compare
)
3716 result_type
= common_type (type0
, type1
);
3718 /* For certain operations (which identify themselves by shorten != 0)
3719 if both args were extended from the same smaller type,
3720 do the arithmetic in that type and then extend.
3722 shorten !=0 and !=1 indicates a bitwise operation.
3723 For them, this optimization is safe only if
3724 both args are zero-extended or both are sign-extended.
3725 Otherwise, we might change the result.
3726 Eg, (short)-1 | (unsigned short)-1 is (int)-1
3727 but calculated in (unsigned short) it would be (unsigned short)-1. */
3729 if (shorten
&& none_complex
)
3731 int unsigned0
, unsigned1
;
3732 tree arg0
= get_narrower (op0
, &unsigned0
);
3733 tree arg1
= get_narrower (op1
, &unsigned1
);
3734 /* UNS is 1 if the operation to be done is an unsigned one. */
3735 int uns
= TREE_UNSIGNED (result_type
);
3738 final_type
= result_type
;
3740 /* Handle the case that OP0 does not *contain* a conversion
3741 but it *requires* conversion to FINAL_TYPE. */
3743 if (op0
== arg0
&& TREE_TYPE (op0
) != final_type
)
3744 unsigned0
= TREE_UNSIGNED (TREE_TYPE (op0
));
3745 if (op1
== arg1
&& TREE_TYPE (op1
) != final_type
)
3746 unsigned1
= TREE_UNSIGNED (TREE_TYPE (op1
));
3748 /* Now UNSIGNED0 is 1 if ARG0 zero-extends to FINAL_TYPE. */
3750 /* For bitwise operations, signedness of nominal type
3751 does not matter. Consider only how operands were extended. */
3755 /* Note that in all three cases below we refrain from optimizing
3756 an unsigned operation on sign-extended args.
3757 That would not be valid. */
3759 /* Both args variable: if both extended in same way
3760 from same width, do it in that width.
3761 Do it unsigned if args were zero-extended. */
3762 if ((TYPE_PRECISION (TREE_TYPE (arg0
))
3763 < TYPE_PRECISION (result_type
))
3764 && (TYPE_PRECISION (TREE_TYPE (arg1
))
3765 == TYPE_PRECISION (TREE_TYPE (arg0
)))
3766 && unsigned0
== unsigned1
3767 && (unsigned0
|| !uns
))
3769 = signed_or_unsigned_type (unsigned0
,
3770 common_type (TREE_TYPE (arg0
),
3772 else if (TREE_CODE (arg0
) == INTEGER_CST
3773 && (unsigned1
|| !uns
)
3774 && (TYPE_PRECISION (TREE_TYPE (arg1
))
3775 < TYPE_PRECISION (result_type
))
3776 && (type
= signed_or_unsigned_type (unsigned1
,
3778 int_fits_type_p (arg0
, type
)))
3780 else if (TREE_CODE (arg1
) == INTEGER_CST
3781 && (unsigned0
|| !uns
)
3782 && (TYPE_PRECISION (TREE_TYPE (arg0
))
3783 < TYPE_PRECISION (result_type
))
3784 && (type
= signed_or_unsigned_type (unsigned0
,
3786 int_fits_type_p (arg1
, type
)))
3790 /* Shifts can be shortened if shifting right. */
3795 tree arg0
= get_narrower (op0
, &unsigned_arg
);
3797 final_type
= result_type
;
3799 if (arg0
== op0
&& final_type
== TREE_TYPE (op0
))
3800 unsigned_arg
= TREE_UNSIGNED (TREE_TYPE (op0
));
3802 if (TYPE_PRECISION (TREE_TYPE (arg0
)) < TYPE_PRECISION (result_type
)
3803 /* We can shorten only if the shift count is less than the
3804 number of bits in the smaller type size. */
3805 && compare_tree_int (op1
, TYPE_PRECISION (TREE_TYPE (arg0
))) < 0
3806 /* If arg is sign-extended and then unsigned-shifted,
3807 we can simulate this with a signed shift in arg's type
3808 only if the extended result is at least twice as wide
3809 as the arg. Otherwise, the shift could use up all the
3810 ones made by sign-extension and bring in zeros.
3811 We can't optimize that case at all, but in most machines
3812 it never happens because available widths are 2**N. */
3813 && (!TREE_UNSIGNED (final_type
)
3815 || (((unsigned) 2 * TYPE_PRECISION (TREE_TYPE (arg0
)))
3816 <= TYPE_PRECISION (result_type
))))
3818 /* Do an unsigned shift if the operand was zero-extended. */
3820 = signed_or_unsigned_type (unsigned_arg
,
3822 /* Convert value-to-be-shifted to that type. */
3823 if (TREE_TYPE (op0
) != result_type
)
3824 op0
= cp_convert (result_type
, op0
);
3829 /* Comparison operations are shortened too but differently.
3830 They identify themselves by setting short_compare = 1. */
3834 /* Don't write &op0, etc., because that would prevent op0
3835 from being kept in a register.
3836 Instead, make copies of the our local variables and
3837 pass the copies by reference, then copy them back afterward. */
3838 tree xop0
= op0
, xop1
= op1
, xresult_type
= result_type
;
3839 enum tree_code xresultcode
= resultcode
;
3841 = shorten_compare (&xop0
, &xop1
, &xresult_type
, &xresultcode
);
3843 return cp_convert (boolean_type_node
, val
);
3844 op0
= xop0
, op1
= xop1
;
3846 resultcode
= xresultcode
;
3849 if ((short_compare
|| code
== MIN_EXPR
|| code
== MAX_EXPR
)
3850 && warn_sign_compare
)
3852 int op0_signed
= ! TREE_UNSIGNED (TREE_TYPE (orig_op0
));
3853 int op1_signed
= ! TREE_UNSIGNED (TREE_TYPE (orig_op1
));
3855 int unsignedp0
, unsignedp1
;
3856 tree primop0
= get_narrower (op0
, &unsignedp0
);
3857 tree primop1
= get_narrower (op1
, &unsignedp1
);
3859 /* Check for comparison of different enum types. */
3860 if (TREE_CODE (TREE_TYPE (orig_op0
)) == ENUMERAL_TYPE
3861 && TREE_CODE (TREE_TYPE (orig_op1
)) == ENUMERAL_TYPE
3862 && TYPE_MAIN_VARIANT (TREE_TYPE (orig_op0
))
3863 != TYPE_MAIN_VARIANT (TREE_TYPE (orig_op1
)))
3865 warning ("comparison between types `%#T' and `%#T'",
3866 TREE_TYPE (orig_op0
), TREE_TYPE (orig_op1
));
3869 /* Give warnings for comparisons between signed and unsigned
3870 quantities that may fail. */
3871 /* Do the checking based on the original operand trees, so that
3872 casts will be considered, but default promotions won't be. */
3874 /* Do not warn if the comparison is being done in a signed type,
3875 since the signed type will only be chosen if it can represent
3876 all the values of the unsigned type. */
3877 if (! TREE_UNSIGNED (result_type
))
3879 /* Do not warn if both operands are unsigned. */
3880 else if (op0_signed
== op1_signed
)
3882 /* Do not warn if the signed quantity is an unsuffixed
3883 integer literal (or some static constant expression
3884 involving such literals or a conditional expression
3885 involving such literals) and it is non-negative. */
3886 else if ((op0_signed
&& tree_expr_nonnegative_p (orig_op0
))
3887 || (op1_signed
&& tree_expr_nonnegative_p (orig_op1
)))
3889 /* Do not warn if the comparison is an equality operation,
3890 the unsigned quantity is an integral constant and it does
3891 not use the most significant bit of result_type. */
3892 else if ((resultcode
== EQ_EXPR
|| resultcode
== NE_EXPR
)
3893 && ((op0_signed
&& TREE_CODE (orig_op1
) == INTEGER_CST
3894 && int_fits_type_p (orig_op1
,
3895 signed_type (result_type
)))
3896 || (op1_signed
&& TREE_CODE (orig_op0
) == INTEGER_CST
3897 && int_fits_type_p (orig_op0
,
3898 signed_type (result_type
)))))
3901 warning ("comparison between signed and unsigned integer expressions");
3903 /* Warn if two unsigned values are being compared in a size
3904 larger than their original size, and one (and only one) is the
3905 result of a `~' operator. This comparison will always fail.
3907 Also warn if one operand is a constant, and the constant does not
3908 have all bits set that are set in the ~ operand when it is
3911 if ((TREE_CODE (primop0
) == BIT_NOT_EXPR
)
3912 ^ (TREE_CODE (primop1
) == BIT_NOT_EXPR
))
3914 if (TREE_CODE (primop0
) == BIT_NOT_EXPR
)
3915 primop0
= get_narrower (TREE_OPERAND (op0
, 0), &unsignedp0
);
3916 if (TREE_CODE (primop1
) == BIT_NOT_EXPR
)
3917 primop1
= get_narrower (TREE_OPERAND (op1
, 0), &unsignedp1
);
3919 if (host_integerp (primop0
, 0) || host_integerp (primop1
, 0))
3922 HOST_WIDE_INT constant
, mask
;
3926 if (host_integerp (primop0
, 0))
3929 unsignedp
= unsignedp1
;
3930 constant
= tree_low_cst (primop0
, 0);
3935 unsignedp
= unsignedp0
;
3936 constant
= tree_low_cst (primop1
, 0);
3939 bits
= TYPE_PRECISION (TREE_TYPE (primop
));
3940 if (bits
< TYPE_PRECISION (result_type
)
3941 && bits
< HOST_BITS_PER_LONG
&& unsignedp
)
3943 mask
= (~ (HOST_WIDE_INT
) 0) << bits
;
3944 if ((mask
& constant
) != mask
)
3945 warning ("comparison of promoted ~unsigned with constant");
3948 else if (unsignedp0
&& unsignedp1
3949 && (TYPE_PRECISION (TREE_TYPE (primop0
))
3950 < TYPE_PRECISION (result_type
))
3951 && (TYPE_PRECISION (TREE_TYPE (primop1
))
3952 < TYPE_PRECISION (result_type
)))
3953 warning ("comparison of promoted ~unsigned with unsigned");
3958 /* At this point, RESULT_TYPE must be nonzero to avoid an error message.
3959 If CONVERTED is zero, both args will be converted to type RESULT_TYPE.
3960 Then the expression will be built.
3961 It will be given type FINAL_TYPE if that is nonzero;
3962 otherwise, it will be given type RESULT_TYPE. */
3966 error ("invalid operands of types `%T' and `%T' to binary `%O'",
3967 TREE_TYPE (orig_op0
), TREE_TYPE (orig_op1
), code
);
3968 return error_mark_node
;
3971 /* Issue warnings about peculiar, but legal, uses of NULL. */
3972 if (/* It's reasonable to use pointer values as operands of &&
3973 and ||, so NULL is no exception. */
3974 !(code
== TRUTH_ANDIF_EXPR
|| code
== TRUTH_ORIF_EXPR
)
3975 && (/* If OP0 is NULL and OP1 is not a pointer, or vice versa. */
3976 (orig_op0
== null_node
3977 && TREE_CODE (TREE_TYPE (op1
)) != POINTER_TYPE
)
3978 /* Or vice versa. */
3979 || (orig_op1
== null_node
3980 && TREE_CODE (TREE_TYPE (op0
)) != POINTER_TYPE
)
3981 /* Or, both are NULL and the operation was not a comparison. */
3982 || (orig_op0
== null_node
&& orig_op1
== null_node
3983 && code
!= EQ_EXPR
&& code
!= NE_EXPR
)))
3984 /* Some sort of arithmetic operation involving NULL was
3985 performed. Note that pointer-difference and pointer-addition
3986 have already been handled above, and so we don't end up here in
3988 warning ("NULL used in arithmetic");
3992 if (TREE_TYPE (op0
) != result_type
)
3993 op0
= cp_convert (result_type
, op0
);
3994 if (TREE_TYPE (op1
) != result_type
)
3995 op1
= cp_convert (result_type
, op1
);
3997 if (op0
== error_mark_node
|| op1
== error_mark_node
)
3998 return error_mark_node
;
4001 if (build_type
== NULL_TREE
)
4002 build_type
= result_type
;
4005 register tree result
= build (resultcode
, build_type
, op0
, op1
);
4006 register tree folded
;
4008 folded
= fold (result
);
4009 if (folded
== result
)
4010 TREE_CONSTANT (folded
) = TREE_CONSTANT (op0
) & TREE_CONSTANT (op1
);
4011 if (final_type
!= 0)
4012 return cp_convert (final_type
, folded
);
4017 /* Return a tree for the sum or difference (RESULTCODE says which)
4018 of pointer PTROP and integer INTOP. */
4021 cp_pointer_int_sum (resultcode
, ptrop
, intop
)
4022 enum tree_code resultcode
;
4023 register tree ptrop
, intop
;
4025 tree res_type
= TREE_TYPE (ptrop
);
4027 /* pointer_int_sum() uses size_in_bytes() on the TREE_TYPE(res_type)
4028 in certain circumstance (when it's valid to do so). So we need
4029 to make sure it's complete. We don't need to check here, if we
4030 can actually complete it at all, as those checks will be done in
4031 pointer_int_sum() anyway. */
4032 complete_type (TREE_TYPE (res_type
));
4034 return pointer_int_sum (resultcode
, ptrop
, fold (intop
));
4037 /* Return a tree for the difference of pointers OP0 and OP1.
4038 The resulting tree has type int. */
4041 pointer_diff (op0
, op1
, ptrtype
)
4042 register tree op0
, op1
;
4043 register tree ptrtype
;
4045 register tree result
, folded
;
4046 tree restype
= ptrdiff_type_node
;
4047 tree target_type
= TREE_TYPE (ptrtype
);
4049 if (!complete_type_or_else (target_type
, NULL_TREE
))
4050 return error_mark_node
;
4052 if (pedantic
|| warn_pointer_arith
)
4054 if (TREE_CODE (target_type
) == VOID_TYPE
)
4055 pedwarn ("ISO C++ forbids using pointer of type `void *' in subtraction");
4056 if (TREE_CODE (target_type
) == FUNCTION_TYPE
)
4057 pedwarn ("ISO C++ forbids using pointer to a function in subtraction");
4058 if (TREE_CODE (target_type
) == METHOD_TYPE
)
4059 pedwarn ("ISO C++ forbids using pointer to a method in subtraction");
4060 if (TREE_CODE (target_type
) == OFFSET_TYPE
)
4061 pedwarn ("ISO C++ forbids using pointer to a member in subtraction");
4064 /* First do the subtraction as integers;
4065 then drop through to build the divide operator. */
4067 op0
= cp_build_binary_op (MINUS_EXPR
,
4068 cp_convert (restype
, op0
),
4069 cp_convert (restype
, op1
));
4071 /* This generates an error if op1 is a pointer to an incomplete type. */
4072 if (!COMPLETE_TYPE_P (TREE_TYPE (TREE_TYPE (op1
))))
4073 error ("invalid use of a pointer to an incomplete type in pointer arithmetic");
4075 op1
= ((TREE_CODE (target_type
) == VOID_TYPE
4076 || TREE_CODE (target_type
) == FUNCTION_TYPE
4077 || TREE_CODE (target_type
) == METHOD_TYPE
4078 || TREE_CODE (target_type
) == OFFSET_TYPE
)
4080 : size_in_bytes (target_type
));
4082 /* Do the division. */
4084 result
= build (EXACT_DIV_EXPR
, restype
, op0
, cp_convert (restype
, op1
));
4086 folded
= fold (result
);
4087 if (folded
== result
)
4088 TREE_CONSTANT (folded
) = TREE_CONSTANT (op0
) & TREE_CONSTANT (op1
);
4092 /* Handle the case of taking the address of a COMPONENT_REF.
4093 Called by `build_unary_op'.
4095 ARG is the COMPONENT_REF whose address we want.
4096 ARGTYPE is the pointer type that this address should have. */
4099 build_component_addr (arg
, argtype
)
4102 tree field
= TREE_OPERAND (arg
, 1);
4103 tree basetype
= decl_type_context (field
);
4104 tree rval
= build_unary_op (ADDR_EXPR
, TREE_OPERAND (arg
, 0), 0);
4106 my_friendly_assert (TREE_CODE (field
) == FIELD_DECL
, 981018);
4108 if (DECL_C_BIT_FIELD (field
))
4110 error ("attempt to take address of bit-field structure member `%D'",
4112 return error_mark_node
;
4115 if (TREE_CODE (field
) == FIELD_DECL
4116 && TYPE_BASE_CONVS_MAY_REQUIRE_CODE_P (basetype
))
4118 /* Can't convert directly to ARGTYPE, since that
4119 may have the same pointer type as one of our
4121 tree binfo
= lookup_base (TREE_TYPE (TREE_TYPE (rval
)), basetype
,
4124 rval
= build_base_path (PLUS_EXPR
, rval
, binfo
, 1);
4125 rval
= build1 (NOP_EXPR
, argtype
, rval
);
4126 TREE_CONSTANT (rval
) = TREE_CONSTANT (TREE_OPERAND (rval
, 0));
4129 /* This conversion is harmless. */
4130 rval
= convert_force (argtype
, rval
, 0);
4132 return fold (build (PLUS_EXPR
, argtype
, rval
,
4133 cp_convert (argtype
, byte_position (field
))));
4136 /* Construct and perhaps optimize a tree representation
4137 for a unary operation. CODE, a tree_code, specifies the operation
4138 and XARG is the operand. */
4141 build_x_unary_op (code
, xarg
)
4142 enum tree_code code
;
4148 if (processing_template_decl
)
4149 return build_min_nt (code
, xarg
, NULL_TREE
);
4151 /* & rec, on incomplete RECORD_TYPEs is the simple opr &, not an
4153 if (code
== ADDR_EXPR
4154 && TREE_CODE (xarg
) != TEMPLATE_ID_EXPR
4155 && ((IS_AGGR_TYPE_CODE (TREE_CODE (TREE_TYPE (xarg
)))
4156 && !COMPLETE_TYPE_P (TREE_TYPE (xarg
)))
4157 || (TREE_CODE (xarg
) == OFFSET_REF
)))
4158 /* don't look for a function */;
4163 rval
= build_new_op (code
, LOOKUP_NORMAL
, xarg
,
4164 NULL_TREE
, NULL_TREE
);
4165 if (rval
|| code
!= ADDR_EXPR
)
4168 if (code
== ADDR_EXPR
)
4170 if (TREE_CODE (xarg
) == OFFSET_REF
)
4172 ptrmem
= PTRMEM_OK_P (xarg
);
4174 if (!ptrmem
&& !flag_ms_extensions
4175 && TREE_CODE (TREE_TYPE (TREE_OPERAND (xarg
, 1))) == METHOD_TYPE
)
4177 /* A single non-static member, make sure we don't allow a
4178 pointer-to-member. */
4179 xarg
= build (OFFSET_REF
, TREE_TYPE (xarg
),
4180 TREE_OPERAND (xarg
, 0),
4181 ovl_cons (TREE_OPERAND (xarg
, 1), NULL_TREE
));
4182 PTRMEM_OK_P (xarg
) = ptrmem
;
4186 else if (TREE_CODE (xarg
) == TARGET_EXPR
)
4187 warning ("taking address of temporary");
4189 exp
= build_unary_op (code
, xarg
, 0);
4190 if (TREE_CODE (exp
) == ADDR_EXPR
)
4191 PTRMEM_OK_P (exp
) = ptrmem
;
4196 /* Like truthvalue_conversion, but handle pointer-to-member constants, where
4197 a null value is represented by an INTEGER_CST of -1. */
4200 cp_truthvalue_conversion (expr
)
4203 tree type
= TREE_TYPE (expr
);
4204 if (TYPE_PTRMEM_P (type
))
4205 return build_binary_op (NE_EXPR
, expr
, integer_zero_node
, 1);
4207 return truthvalue_conversion (expr
);
4210 /* Just like cp_truthvalue_conversion, but we want a CLEANUP_POINT_EXPR. */
4213 condition_conversion (expr
)
4217 if (processing_template_decl
)
4219 if (TREE_CODE (expr
) == OFFSET_REF
)
4220 expr
= resolve_offset_ref (expr
);
4221 t
= perform_implicit_conversion (boolean_type_node
, expr
);
4222 t
= fold (build1 (CLEANUP_POINT_EXPR
, boolean_type_node
, t
));
4226 /* C++: Must handle pointers to members.
4228 Perhaps type instantiation should be extended to handle conversion
4229 from aggregates to types we don't yet know we want? (Or are those
4230 cases typically errors which should be reported?)
4232 NOCONVERT nonzero suppresses the default promotions
4233 (such as from short to int). */
4236 build_unary_op (code
, xarg
, noconvert
)
4237 enum tree_code code
;
4241 /* No default_conversion here. It causes trouble for ADDR_EXPR. */
4242 register tree arg
= xarg
;
4243 register tree argtype
= 0;
4244 const char *errstring
= NULL
;
4247 if (arg
== error_mark_node
)
4248 return error_mark_node
;
4253 /* This is used for unary plus, because a CONVERT_EXPR
4254 is enough to prevent anybody from looking inside for
4255 associativity, but won't generate any code. */
4256 if (!(arg
= build_expr_type_conversion
4257 (WANT_ARITH
| WANT_ENUM
| WANT_POINTER
, arg
, 1)))
4258 errstring
= "wrong type argument to unary plus";
4262 arg
= default_conversion (arg
);
4263 arg
= build1 (NON_LVALUE_EXPR
, TREE_TYPE (arg
), arg
);
4264 TREE_CONSTANT (arg
) = TREE_CONSTANT (TREE_OPERAND (arg
, 0));
4269 if (!(arg
= build_expr_type_conversion (WANT_ARITH
| WANT_ENUM
, arg
, 1)))
4270 errstring
= "wrong type argument to unary minus";
4271 else if (!noconvert
)
4272 arg
= default_conversion (arg
);
4276 if (TREE_CODE (TREE_TYPE (arg
)) == COMPLEX_TYPE
)
4280 arg
= default_conversion (arg
);
4282 else if (!(arg
= build_expr_type_conversion (WANT_INT
| WANT_ENUM
,
4284 errstring
= "wrong type argument to bit-complement";
4285 else if (!noconvert
)
4286 arg
= default_conversion (arg
);
4290 if (!(arg
= build_expr_type_conversion (WANT_ARITH
| WANT_ENUM
, arg
, 1)))
4291 errstring
= "wrong type argument to abs";
4292 else if (!noconvert
)
4293 arg
= default_conversion (arg
);
4297 /* Conjugating a real value is a no-op, but allow it anyway. */
4298 if (!(arg
= build_expr_type_conversion (WANT_ARITH
| WANT_ENUM
, arg
, 1)))
4299 errstring
= "wrong type argument to conjugation";
4300 else if (!noconvert
)
4301 arg
= default_conversion (arg
);
4304 case TRUTH_NOT_EXPR
:
4305 arg
= cp_convert (boolean_type_node
, arg
);
4306 val
= invert_truthvalue (arg
);
4307 if (arg
!= error_mark_node
)
4309 errstring
= "in argument to unary !";
4316 if (TREE_CODE (arg
) == COMPLEX_CST
)
4317 return TREE_REALPART (arg
);
4318 else if (TREE_CODE (TREE_TYPE (arg
)) == COMPLEX_TYPE
)
4319 return fold (build1 (REALPART_EXPR
, TREE_TYPE (TREE_TYPE (arg
)), arg
));
4324 if (TREE_CODE (arg
) == COMPLEX_CST
)
4325 return TREE_IMAGPART (arg
);
4326 else if (TREE_CODE (TREE_TYPE (arg
)) == COMPLEX_TYPE
)
4327 return fold (build1 (IMAGPART_EXPR
, TREE_TYPE (TREE_TYPE (arg
)), arg
));
4329 return cp_convert (TREE_TYPE (arg
), integer_zero_node
);
4331 case PREINCREMENT_EXPR
:
4332 case POSTINCREMENT_EXPR
:
4333 case PREDECREMENT_EXPR
:
4334 case POSTDECREMENT_EXPR
:
4335 /* Handle complex lvalues (when permitted)
4336 by reduction to simpler cases. */
4338 val
= unary_complex_lvalue (code
, arg
);
4342 /* Increment or decrement the real part of the value,
4343 and don't change the imaginary part. */
4344 if (TREE_CODE (TREE_TYPE (arg
)) == COMPLEX_TYPE
)
4348 arg
= stabilize_reference (arg
);
4349 real
= build_unary_op (REALPART_EXPR
, arg
, 1);
4350 imag
= build_unary_op (IMAGPART_EXPR
, arg
, 1);
4351 return build (COMPLEX_EXPR
, TREE_TYPE (arg
),
4352 build_unary_op (code
, real
, 1), imag
);
4355 /* Report invalid types. */
4357 if (!(arg
= build_expr_type_conversion (WANT_ARITH
| WANT_POINTER
,
4360 if (code
== PREINCREMENT_EXPR
)
4361 errstring
="no pre-increment operator for type";
4362 else if (code
== POSTINCREMENT_EXPR
)
4363 errstring
="no post-increment operator for type";
4364 else if (code
== PREDECREMENT_EXPR
)
4365 errstring
="no pre-decrement operator for type";
4367 errstring
="no post-decrement operator for type";
4371 /* Report something read-only. */
4373 if (CP_TYPE_CONST_P (TREE_TYPE (arg
))
4374 || TREE_READONLY (arg
))
4375 readonly_error (arg
, ((code
== PREINCREMENT_EXPR
4376 || code
== POSTINCREMENT_EXPR
)
4377 ? "increment" : "decrement"),
4382 tree result_type
= TREE_TYPE (arg
);
4384 arg
= get_unwidened (arg
, 0);
4385 argtype
= TREE_TYPE (arg
);
4387 /* ARM $5.2.5 last annotation says this should be forbidden. */
4388 if (TREE_CODE (argtype
) == ENUMERAL_TYPE
)
4389 pedwarn ("ISO C++ forbids %sing an enum",
4390 (code
== PREINCREMENT_EXPR
|| code
== POSTINCREMENT_EXPR
)
4391 ? "increment" : "decrement");
4393 /* Compute the increment. */
4395 if (TREE_CODE (argtype
) == POINTER_TYPE
)
4397 enum tree_code tmp
= TREE_CODE (TREE_TYPE (argtype
));
4398 tree type
= complete_type (TREE_TYPE (argtype
));
4400 if (!COMPLETE_OR_VOID_TYPE_P (type
))
4401 error ("cannot %s a pointer to incomplete type `%T'",
4402 ((code
== PREINCREMENT_EXPR
4403 || code
== POSTINCREMENT_EXPR
)
4404 ? "increment" : "decrement"), TREE_TYPE (argtype
));
4405 else if ((pedantic
|| warn_pointer_arith
)
4406 && (tmp
== FUNCTION_TYPE
|| tmp
== METHOD_TYPE
4407 || tmp
== VOID_TYPE
|| tmp
== OFFSET_TYPE
))
4408 pedwarn ("ISO C++ forbids %sing a pointer of type `%T'",
4409 ((code
== PREINCREMENT_EXPR
4410 || code
== POSTINCREMENT_EXPR
)
4411 ? "increment" : "decrement"), argtype
);
4412 inc
= c_sizeof_nowarn (TREE_TYPE (argtype
));
4415 inc
= integer_one_node
;
4417 inc
= cp_convert (argtype
, inc
);
4419 /* Handle incrementing a cast-expression. */
4421 switch (TREE_CODE (arg
))
4426 case FIX_TRUNC_EXPR
:
4427 case FIX_FLOOR_EXPR
:
4428 case FIX_ROUND_EXPR
:
4431 tree incremented
, modify
, value
, compound
;
4432 if (! lvalue_p (arg
) && pedantic
)
4433 pedwarn ("cast to non-reference type used as lvalue");
4434 arg
= stabilize_reference (arg
);
4435 if (code
== PREINCREMENT_EXPR
|| code
== PREDECREMENT_EXPR
)
4438 value
= save_expr (arg
);
4439 incremented
= build (((code
== PREINCREMENT_EXPR
4440 || code
== POSTINCREMENT_EXPR
)
4441 ? PLUS_EXPR
: MINUS_EXPR
),
4442 argtype
, value
, inc
);
4444 modify
= build_modify_expr (arg
, NOP_EXPR
, incremented
);
4445 compound
= build (COMPOUND_EXPR
, TREE_TYPE (arg
), modify
, value
);
4447 /* Eliminate warning about unused result of + or -. */
4448 TREE_NO_UNUSED_WARNING (compound
) = 1;
4456 /* Complain about anything else that is not a true lvalue. */
4457 if (!lvalue_or_else (arg
, ((code
== PREINCREMENT_EXPR
4458 || code
== POSTINCREMENT_EXPR
)
4459 ? "increment" : "decrement")))
4460 return error_mark_node
;
4462 /* Forbid using -- on `bool'. */
4463 if (TREE_TYPE (arg
) == boolean_type_node
)
4465 if (code
== POSTDECREMENT_EXPR
|| code
== PREDECREMENT_EXPR
)
4467 error ("invalid use of `--' on bool variable `%D'", arg
);
4468 return error_mark_node
;
4471 /* This will only work if someone can convince Kenner to accept
4472 my patch to expand_increment. (jason) */
4473 val
= build (code
, TREE_TYPE (arg
), arg
, inc
);
4475 val
= boolean_increment (code
, arg
);
4479 val
= build (code
, TREE_TYPE (arg
), arg
, inc
);
4481 TREE_SIDE_EFFECTS (val
) = 1;
4482 return cp_convert (result_type
, val
);
4486 /* Note that this operation never does default_conversion
4487 regardless of NOCONVERT. */
4489 argtype
= lvalue_type (arg
);
4490 if (TREE_CODE (argtype
) == REFERENCE_TYPE
)
4494 build_pointer_type (TREE_TYPE (argtype
)), arg
);
4495 TREE_CONSTANT (arg
) = TREE_CONSTANT (TREE_OPERAND (arg
, 0));
4498 else if (pedantic
&& DECL_MAIN_P (arg
))
4500 pedwarn ("ISO C++ forbids taking address of function `::main'");
4502 /* Let &* cancel out to simplify resulting code. */
4503 if (TREE_CODE (arg
) == INDIRECT_REF
)
4505 /* We don't need to have `current_class_ptr' wrapped in a
4506 NON_LVALUE_EXPR node. */
4507 if (arg
== current_class_ref
)
4508 return current_class_ptr
;
4510 arg
= TREE_OPERAND (arg
, 0);
4511 if (TREE_CODE (TREE_TYPE (arg
)) == REFERENCE_TYPE
)
4515 build_pointer_type (TREE_TYPE (TREE_TYPE (arg
))), arg
);
4516 TREE_CONSTANT (arg
) = TREE_CONSTANT (TREE_OPERAND (arg
, 0));
4518 else if (lvalue_p (arg
))
4519 /* Don't let this be an lvalue. */
4520 return non_lvalue (arg
);
4524 /* For &x[y], return x+y */
4525 if (TREE_CODE (arg
) == ARRAY_REF
)
4527 if (mark_addressable (TREE_OPERAND (arg
, 0)) == 0)
4528 return error_mark_node
;
4529 return cp_build_binary_op (PLUS_EXPR
, TREE_OPERAND (arg
, 0),
4530 TREE_OPERAND (arg
, 1));
4533 /* Uninstantiated types are all functions. Taking the
4534 address of a function is a no-op, so just return the
4537 if (TREE_CODE (arg
) == IDENTIFIER_NODE
4538 && IDENTIFIER_OPNAME_P (arg
))
4541 /* We don't know the type yet, so just work around the problem.
4542 We know that this will resolve to an lvalue. */
4543 return build1 (ADDR_EXPR
, unknown_type_node
, arg
);
4546 if (TREE_CODE (arg
) == COMPONENT_REF
&& type_unknown_p (arg
)
4547 && OVL_NEXT (TREE_OPERAND (arg
, 1)) == NULL_TREE
)
4549 /* They're trying to take the address of a unique non-static
4550 member function. This is ill-formed (except in MS-land),
4551 but let's try to DTRT.
4552 Note: We only handle unique functions here because we don't
4553 want to complain if there's a static overload; non-unique
4554 cases will be handled by instantiate_type. But we need to
4555 handle this case here to allow casts on the resulting PMF.
4556 We could defer this in non-MS mode, but it's easier to give
4557 a useful error here. */
4559 tree base
= TREE_TYPE (TREE_OPERAND (arg
, 0));
4560 tree name
= DECL_NAME (OVL_CURRENT (TREE_OPERAND (arg
, 1)));
4562 if (! flag_ms_extensions
)
4564 if (current_class_type
4565 && TREE_OPERAND (arg
, 0) == current_class_ref
)
4566 /* An expression like &memfn. */
4567 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
);
4569 pedwarn ("ISO C++ forbids taking the address of a bound member function to form a pointer to member function. Say `&%T::%D'", base
, name
);
4571 arg
= build_offset_ref (base
, name
);
4574 if (type_unknown_p (arg
))
4575 return build1 (ADDR_EXPR
, unknown_type_node
, arg
);
4577 /* Handle complex lvalues (when permitted)
4578 by reduction to simpler cases. */
4579 val
= unary_complex_lvalue (code
, arg
);
4583 switch (TREE_CODE (arg
))
4588 case FIX_TRUNC_EXPR
:
4589 case FIX_FLOOR_EXPR
:
4590 case FIX_ROUND_EXPR
:
4592 if (! lvalue_p (arg
) && pedantic
)
4593 pedwarn ("ISO C++ forbids taking the address of a cast to a non-lvalue expression");
4600 /* Allow the address of a constructor if all the elements
4602 if (TREE_CODE (arg
) == CONSTRUCTOR
&& TREE_HAS_CONSTRUCTOR (arg
)
4603 && TREE_CONSTANT (arg
))
4605 /* Anything not already handled and not a true memory reference
4607 else if (TREE_CODE (argtype
) != FUNCTION_TYPE
4608 && TREE_CODE (argtype
) != METHOD_TYPE
4609 && !lvalue_or_else (arg
, "unary `&'"))
4610 return error_mark_node
;
4612 if (argtype
!= error_mark_node
)
4613 argtype
= build_pointer_type (argtype
);
4615 if (mark_addressable (arg
) == 0)
4616 return error_mark_node
;
4621 if (TREE_CODE (arg
) == COMPONENT_REF
)
4622 addr
= build_component_addr (arg
, argtype
);
4624 addr
= build1 (ADDR_EXPR
, argtype
, arg
);
4626 /* Address of a static or external variable or
4627 function counts as a constant */
4629 TREE_CONSTANT (addr
) = 1;
4631 if (TREE_CODE (argtype
) == POINTER_TYPE
4632 && TREE_CODE (TREE_TYPE (argtype
)) == METHOD_TYPE
)
4634 build_ptrmemfunc_type (argtype
);
4635 addr
= build_ptrmemfunc (argtype
, addr
, 0);
4648 argtype
= TREE_TYPE (arg
);
4649 return fold (build1 (code
, argtype
, arg
));
4652 error ("%s", errstring
);
4653 return error_mark_node
;
4656 /* Apply unary lvalue-demanding operator CODE to the expression ARG
4657 for certain kinds of expressions which are not really lvalues
4658 but which we can accept as lvalues.
4660 If ARG is not a kind of expression we can handle, return zero. */
4663 unary_complex_lvalue (code
, arg
)
4664 enum tree_code code
;
4667 /* Handle (a, b) used as an "lvalue". */
4668 if (TREE_CODE (arg
) == COMPOUND_EXPR
)
4670 tree real_result
= build_unary_op (code
, TREE_OPERAND (arg
, 1), 0);
4671 return build (COMPOUND_EXPR
, TREE_TYPE (real_result
),
4672 TREE_OPERAND (arg
, 0), real_result
);
4675 /* Handle (a ? b : c) used as an "lvalue". */
4676 if (TREE_CODE (arg
) == COND_EXPR
4677 || TREE_CODE (arg
) == MIN_EXPR
|| TREE_CODE (arg
) == MAX_EXPR
)
4678 return rationalize_conditional_expr (code
, arg
);
4680 /* Handle (a = b), (++a), and (--a) used as an "lvalue". */
4681 if (TREE_CODE (arg
) == MODIFY_EXPR
4682 || TREE_CODE (arg
) == PREINCREMENT_EXPR
4683 || TREE_CODE (arg
) == PREDECREMENT_EXPR
)
4685 tree lvalue
= TREE_OPERAND (arg
, 0);
4686 if (TREE_SIDE_EFFECTS (lvalue
))
4688 lvalue
= stabilize_reference (lvalue
);
4689 arg
= build (TREE_CODE (arg
), TREE_TYPE (arg
),
4690 lvalue
, TREE_OPERAND (arg
, 1));
4692 return unary_complex_lvalue
4693 (code
, build (COMPOUND_EXPR
, TREE_TYPE (lvalue
), arg
, lvalue
));
4696 if (code
!= ADDR_EXPR
)
4699 /* Handle (a = b) used as an "lvalue" for `&'. */
4700 if (TREE_CODE (arg
) == MODIFY_EXPR
4701 || TREE_CODE (arg
) == INIT_EXPR
)
4703 tree real_result
= build_unary_op (code
, TREE_OPERAND (arg
, 0), 0);
4704 arg
= build (COMPOUND_EXPR
, TREE_TYPE (real_result
), arg
, real_result
);
4705 TREE_NO_UNUSED_WARNING (arg
) = 1;
4709 if (TREE_CODE (TREE_TYPE (arg
)) == FUNCTION_TYPE
4710 || TREE_CODE (TREE_TYPE (arg
)) == METHOD_TYPE
4711 || TREE_CODE (TREE_TYPE (arg
)) == OFFSET_TYPE
)
4713 /* The representation of something of type OFFSET_TYPE
4714 is really the representation of a pointer to it.
4715 Here give the representation its true type. */
4718 my_friendly_assert (TREE_CODE (arg
) != SCOPE_REF
, 313);
4720 if (TREE_CODE (arg
) != OFFSET_REF
)
4723 t
= TREE_OPERAND (arg
, 1);
4725 /* Check all this code for right semantics. */
4726 if (TREE_CODE (t
) == FUNCTION_DECL
)
4728 if (DECL_DESTRUCTOR_P (t
))
4729 error ("taking address of destructor");
4730 return build_unary_op (ADDR_EXPR
, t
, 0);
4732 if (TREE_CODE (t
) == VAR_DECL
)
4733 return build_unary_op (ADDR_EXPR
, t
, 0);
4738 if (TREE_OPERAND (arg
, 0)
4739 && ! is_dummy_object (TREE_OPERAND (arg
, 0))
4740 && TREE_CODE (t
) != FIELD_DECL
)
4742 error ("taking address of bound pointer-to-member expression");
4743 return error_mark_node
;
4745 if (!PTRMEM_OK_P (arg
))
4747 /* This cannot form a pointer to method, so we must
4748 resolve the offset ref, and take the address of the
4749 result. For instance,
4751 arg
= resolve_offset_ref (arg
);
4753 return build_unary_op (code
, arg
, 0);
4756 if (TREE_CODE (TREE_TYPE (t
)) == REFERENCE_TYPE
)
4758 error ("cannot create pointer to reference member `%D'", t
);
4759 return error_mark_node
;
4762 type
= build_offset_type (DECL_FIELD_CONTEXT (t
), TREE_TYPE (t
));
4763 type
= build_pointer_type (type
);
4765 t
= make_ptrmem_cst (type
, TREE_OPERAND (arg
, 1));
4771 /* We permit compiler to make function calls returning
4772 objects of aggregate type look like lvalues. */
4776 if (TREE_CODE (targ
) == SAVE_EXPR
)
4777 targ
= TREE_OPERAND (targ
, 0);
4779 if (TREE_CODE (targ
) == CALL_EXPR
&& IS_AGGR_TYPE (TREE_TYPE (targ
)))
4781 if (TREE_CODE (arg
) == SAVE_EXPR
)
4784 targ
= build_cplus_new (TREE_TYPE (arg
), arg
);
4785 return build1 (ADDR_EXPR
, build_pointer_type (TREE_TYPE (arg
)), targ
);
4788 if (TREE_CODE (arg
) == SAVE_EXPR
&& TREE_CODE (targ
) == INDIRECT_REF
)
4789 return build (SAVE_EXPR
, build_pointer_type (TREE_TYPE (arg
)),
4790 TREE_OPERAND (targ
, 0), current_function_decl
, NULL
);
4793 /* Don't let anything else be handled specially. */
4797 /* Mark EXP saying that we need to be able to take the
4798 address of it; it should not be allocated in a register.
4799 Value is 1 if successful.
4801 C++: we do not allow `current_class_ptr' to be addressable. */
4804 mark_addressable (exp
)
4807 register tree x
= exp
;
4810 switch (TREE_CODE (x
))
4817 x
= TREE_OPERAND (x
, 0);
4821 if (x
== current_class_ptr
)
4823 error ("cannot take the address of `this', which is an rvalue expression");
4824 TREE_ADDRESSABLE (x
) = 1; /* so compiler doesn't die later */
4830 /* Caller should not be trying to mark initialized
4831 constant fields addressable. */
4832 my_friendly_assert (DECL_LANG_SPECIFIC (x
) == 0
4833 || DECL_IN_AGGR_P (x
) == 0
4835 || DECL_EXTERNAL (x
), 314);
4840 if (DECL_REGISTER (x
) && !TREE_ADDRESSABLE (x
)
4841 && !DECL_ARTIFICIAL (x
) && extra_warnings
)
4842 warning ("address requested for `%D', which is declared `register'",
4844 TREE_ADDRESSABLE (x
) = 1;
4845 put_var_into_stack (x
);
4849 TREE_ADDRESSABLE (x
) = 1;
4850 TREE_ADDRESSABLE (DECL_ASSEMBLER_NAME (x
)) = 1;
4854 TREE_ADDRESSABLE (x
) = 1;
4858 TREE_ADDRESSABLE (x
) = 1;
4859 mark_addressable (TREE_OPERAND (x
, 0));
4867 /* Build and return a conditional expression IFEXP ? OP1 : OP2. */
4870 build_x_conditional_expr (ifexp
, op1
, op2
)
4871 tree ifexp
, op1
, op2
;
4873 if (processing_template_decl
)
4874 return build_min_nt (COND_EXPR
, ifexp
, op1
, op2
);
4876 return build_conditional_expr (ifexp
, op1
, op2
);
4879 /* Handle overloading of the ',' operator when needed. Otherwise,
4880 this function just builds an expression list. */
4883 build_x_compound_expr (list
)
4886 tree rest
= TREE_CHAIN (list
);
4889 if (processing_template_decl
)
4890 return build_min_nt (COMPOUND_EXPR
, list
, NULL_TREE
);
4892 if (rest
== NULL_TREE
)
4893 return build_compound_expr (list
);
4895 result
= build_opfncall (COMPOUND_EXPR
, LOOKUP_NORMAL
,
4896 TREE_VALUE (list
), TREE_VALUE (rest
), NULL_TREE
);
4898 return build_x_compound_expr (tree_cons (NULL_TREE
, result
,
4899 TREE_CHAIN (rest
)));
4901 if (! TREE_SIDE_EFFECTS (TREE_VALUE (list
)))
4903 /* FIXME: This test should be in the implicit cast to void of the LHS. */
4904 /* the left-hand operand of a comma expression is like an expression
4905 statement: we should warn if it doesn't have any side-effects,
4906 unless it was explicitly cast to (void). */
4907 if ((extra_warnings
|| warn_unused_value
)
4908 && !(TREE_CODE (TREE_VALUE(list
)) == CONVERT_EXPR
4909 && VOID_TYPE_P (TREE_TYPE (TREE_VALUE(list
)))))
4910 warning("left-hand operand of comma expression has no effect");
4912 #if 0 /* this requires a gcc backend patch to export warn_if_unused_value */
4913 else if (warn_unused_value
)
4914 warn_if_unused_value (TREE_VALUE(list
));
4917 return build_compound_expr
4918 (tree_cons (NULL_TREE
, TREE_VALUE (list
),
4919 build_tree_list (NULL_TREE
,
4920 build_x_compound_expr (rest
))));
4923 /* Given a list of expressions, return a compound expression
4924 that performs them all and returns the value of the last of them. */
4927 build_compound_expr (list
)
4933 TREE_VALUE (list
) = decl_constant_value (TREE_VALUE (list
));
4935 if (TREE_CHAIN (list
) == 0)
4937 /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
4938 Strip such NOP_EXPRs, since LIST is used in non-lvalue context. */
4939 if (TREE_CODE (list
) == NOP_EXPR
4940 && TREE_TYPE (list
) == TREE_TYPE (TREE_OPERAND (list
, 0)))
4941 list
= TREE_OPERAND (list
, 0);
4943 return TREE_VALUE (list
);
4946 first
= TREE_VALUE (list
);
4947 first
= convert_to_void (first
, "left-hand operand of comma");
4948 if (first
== error_mark_node
)
4949 return error_mark_node
;
4951 rest
= build_compound_expr (TREE_CHAIN (list
));
4952 if (rest
== error_mark_node
)
4953 return error_mark_node
;
4955 /* When pedantic, a compound expression cannot be a constant expression. */
4956 if (! TREE_SIDE_EFFECTS (first
) && ! pedantic
)
4959 return build (COMPOUND_EXPR
, TREE_TYPE (rest
), first
, rest
);
4963 build_static_cast (type
, expr
)
4969 if (type
== error_mark_node
|| expr
== error_mark_node
)
4970 return error_mark_node
;
4972 if (TREE_CODE (expr
) == OFFSET_REF
)
4973 expr
= resolve_offset_ref (expr
);
4975 if (processing_template_decl
)
4977 tree t
= build_min (STATIC_CAST_EXPR
, type
, expr
);
4981 /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
4982 Strip such NOP_EXPRs if VALUE is being used in non-lvalue context. */
4983 if (TREE_CODE (type
) != REFERENCE_TYPE
4984 && TREE_CODE (expr
) == NOP_EXPR
4985 && TREE_TYPE (expr
) == TREE_TYPE (TREE_OPERAND (expr
, 0)))
4986 expr
= TREE_OPERAND (expr
, 0);
4988 if (TREE_CODE (type
) == VOID_TYPE
)
4990 expr
= convert_to_void (expr
, /*implicit=*/NULL
);
4994 if (TREE_CODE (type
) == REFERENCE_TYPE
)
4995 return (convert_from_reference
4996 (convert_to_reference (type
, expr
, CONV_STATIC
|CONV_IMPLICIT
,
4997 LOOKUP_COMPLAIN
, NULL_TREE
)));
4999 if (IS_AGGR_TYPE (type
))
5000 return build_cplus_new (type
, (build_method_call
5001 (NULL_TREE
, complete_ctor_identifier
,
5002 build_tree_list (NULL_TREE
, expr
),
5003 TYPE_BINFO (type
), LOOKUP_NORMAL
)));
5005 intype
= TREE_TYPE (expr
);
5007 /* FIXME handle casting to array type. */
5010 if (IS_AGGR_TYPE (intype
)
5011 ? can_convert_arg (type
, intype
, expr
)
5012 : can_convert_arg (strip_all_pointer_quals (type
),
5013 strip_all_pointer_quals (intype
), expr
))
5014 /* This is a standard conversion. */
5016 else if (TYPE_PTROB_P (type
) && TYPE_PTROB_P (intype
))
5018 /* They're pointers to objects. They must be aggregates that
5019 are related non-virtually. */
5022 if (IS_AGGR_TYPE (TREE_TYPE (type
)) && IS_AGGR_TYPE (TREE_TYPE (intype
))
5023 && lookup_base (TREE_TYPE (type
), TREE_TYPE (intype
),
5024 ba_ignore
| ba_quiet
, &kind
)
5025 && kind
!= bk_via_virtual
)
5028 else if (TYPE_PTRMEM_P (type
) && TYPE_PTRMEM_P (intype
))
5030 /* They're pointers to members. The pointed to objects must be
5031 the same (ignoring CV qualifiers), and the containing classes
5032 must be related non-virtually. */
5036 (strip_all_pointer_quals (TREE_TYPE (TREE_TYPE (type
))),
5037 strip_all_pointer_quals (TREE_TYPE (TREE_TYPE (intype
))))
5038 && (lookup_base (TYPE_OFFSET_BASETYPE (TREE_TYPE (intype
)),
5039 TYPE_OFFSET_BASETYPE (TREE_TYPE (type
)),
5040 ba_ignore
| ba_quiet
, &kind
))
5041 && kind
!= bk_via_virtual
)
5044 else if (TREE_CODE (intype
) != BOOLEAN_TYPE
5045 && TREE_CODE (type
) != ARRAY_TYPE
5046 && TREE_CODE (type
) != FUNCTION_TYPE
5047 && can_convert (intype
, strip_all_pointer_quals (type
)))
5049 else if (TREE_CODE (intype
) == ENUMERAL_TYPE
5050 && TREE_CODE (type
) == ENUMERAL_TYPE
)
5051 /* DR 128: "A value of integral _or enumeration_ type can be explicitly
5052 converted to an enumeration type."
5053 The integral to enumeration will be accepted by the previous clause.
5054 We need to explicitly check for enumeration to enumeration. */
5057 /* [expr.static.cast]
5059 The static_cast operator shall not be used to cast away
5061 if (ok
&& casts_away_constness (intype
, type
))
5063 error ("static_cast from type `%T' to type `%T' casts away constness",
5065 return error_mark_node
;
5069 return build_c_cast (type
, expr
);
5071 error ("invalid static_cast from type `%T' to type `%T'", intype
, type
);
5072 return error_mark_node
;
5076 build_reinterpret_cast (type
, expr
)
5081 if (type
== error_mark_node
|| expr
== error_mark_node
)
5082 return error_mark_node
;
5084 if (TREE_CODE (expr
) == OFFSET_REF
)
5085 expr
= resolve_offset_ref (expr
);
5087 if (processing_template_decl
)
5089 tree t
= build_min (REINTERPRET_CAST_EXPR
, type
, expr
);
5093 if (TREE_CODE (type
) != REFERENCE_TYPE
)
5095 expr
= decay_conversion (expr
);
5097 /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
5098 Strip such NOP_EXPRs if VALUE is being used in non-lvalue context. */
5099 if (TREE_CODE (expr
) == NOP_EXPR
5100 && TREE_TYPE (expr
) == TREE_TYPE (TREE_OPERAND (expr
, 0)))
5101 expr
= TREE_OPERAND (expr
, 0);
5104 intype
= TREE_TYPE (expr
);
5106 if (TREE_CODE (type
) == REFERENCE_TYPE
)
5108 if (! real_lvalue_p (expr
))
5110 error ("invalid reinterpret_cast of an rvalue expression of type `%T' to type `%T'", intype
, type
);
5111 return error_mark_node
;
5113 expr
= build_unary_op (ADDR_EXPR
, expr
, 0);
5114 if (expr
!= error_mark_node
)
5115 expr
= build_reinterpret_cast
5116 (build_pointer_type (TREE_TYPE (type
)), expr
);
5117 if (expr
!= error_mark_node
)
5118 expr
= build_indirect_ref (expr
, 0);
5121 else if (same_type_ignoring_top_level_qualifiers_p (intype
, type
))
5122 return build_static_cast (type
, expr
);
5124 if (TYPE_PTR_P (type
) && (TREE_CODE (intype
) == INTEGER_TYPE
5125 || TREE_CODE (intype
) == ENUMERAL_TYPE
))
5127 else if (TREE_CODE (type
) == INTEGER_TYPE
&& TYPE_PTR_P (intype
))
5129 if (TYPE_PRECISION (type
) < TYPE_PRECISION (intype
))
5130 pedwarn ("reinterpret_cast from `%T' to `%T' loses precision",
5133 else if ((TYPE_PTRFN_P (type
) && TYPE_PTRFN_P (intype
))
5134 || (TYPE_PTRMEMFUNC_P (type
) && TYPE_PTRMEMFUNC_P (intype
)))
5136 expr
= decl_constant_value (expr
);
5137 return fold (build1 (NOP_EXPR
, type
, expr
));
5139 else if ((TYPE_PTRMEM_P (type
) && TYPE_PTRMEM_P (intype
))
5140 || (TYPE_PTROBV_P (type
) && TYPE_PTROBV_P (intype
)))
5142 if (! comp_ptr_ttypes_reinterpret (TREE_TYPE (type
), TREE_TYPE (intype
)))
5143 pedwarn ("reinterpret_cast from `%T' to `%T' casts away const (or volatile)",
5146 expr
= decl_constant_value (expr
);
5147 return fold (build1 (NOP_EXPR
, type
, expr
));
5149 else if ((TYPE_PTRFN_P (type
) && TYPE_PTROBV_P (intype
))
5150 || (TYPE_PTRFN_P (intype
) && TYPE_PTROBV_P (type
)))
5152 pedwarn ("ISO C++ forbids casting between pointer-to-function and pointer-to-object");
5153 expr
= decl_constant_value (expr
);
5154 return fold (build1 (NOP_EXPR
, type
, expr
));
5158 error ("invalid reinterpret_cast from type `%T' to type `%T'",
5160 return error_mark_node
;
5163 return cp_convert (type
, expr
);
5167 build_const_cast (type
, expr
)
5172 if (type
== error_mark_node
|| expr
== error_mark_node
)
5173 return error_mark_node
;
5175 if (TREE_CODE (expr
) == OFFSET_REF
)
5176 expr
= resolve_offset_ref (expr
);
5178 if (processing_template_decl
)
5180 tree t
= build_min (CONST_CAST_EXPR
, type
, expr
);
5184 if (!POINTER_TYPE_P (type
))
5185 error ("invalid use of const_cast with type `%T', which is not a pointer, reference, nor a pointer-to-data-member type", type
);
5186 else if (TREE_CODE (TREE_TYPE (type
)) == FUNCTION_TYPE
)
5188 error ("invalid use of const_cast with type `%T', which is a pointer or reference to a function type", type
);
5189 return error_mark_node
;
5192 if (TREE_CODE (type
) != REFERENCE_TYPE
)
5194 expr
= decay_conversion (expr
);
5196 /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
5197 Strip such NOP_EXPRs if VALUE is being used in non-lvalue context. */
5198 if (TREE_CODE (expr
) == NOP_EXPR
5199 && TREE_TYPE (expr
) == TREE_TYPE (TREE_OPERAND (expr
, 0)))
5200 expr
= TREE_OPERAND (expr
, 0);
5203 intype
= TREE_TYPE (expr
);
5205 if (same_type_ignoring_top_level_qualifiers_p (intype
, type
))
5206 return build_static_cast (type
, expr
);
5207 else if (TREE_CODE (type
) == REFERENCE_TYPE
)
5209 if (! real_lvalue_p (expr
))
5211 error ("invalid const_cast of an rvalue of type `%T' to type `%T'", intype
, type
);
5212 return error_mark_node
;
5215 if (comp_ptr_ttypes_const (TREE_TYPE (type
), intype
))
5217 expr
= build_unary_op (ADDR_EXPR
, expr
, 0);
5218 expr
= build1 (NOP_EXPR
, type
, expr
);
5219 return convert_from_reference (expr
);
5222 else if (TREE_CODE (type
) == POINTER_TYPE
5223 && TREE_CODE (intype
) == POINTER_TYPE
5224 && comp_ptr_ttypes_const (TREE_TYPE (type
), TREE_TYPE (intype
)))
5225 return cp_convert (type
, expr
);
5227 error ("invalid const_cast from type `%T' to type `%T'", intype
, type
);
5228 return error_mark_node
;
5231 /* Build an expression representing a cast to type TYPE of expression EXPR.
5233 ALLOW_NONCONVERTING is true if we should allow non-converting constructors
5234 when doing the cast. */
5237 build_c_cast (type
, expr
)
5240 register tree value
= expr
;
5243 if (type
== error_mark_node
|| expr
== error_mark_node
)
5244 return error_mark_node
;
5246 if (processing_template_decl
)
5248 tree t
= build_min (CAST_EXPR
, type
,
5249 tree_cons (NULL_TREE
, value
, NULL_TREE
));
5253 /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
5254 Strip such NOP_EXPRs if VALUE is being used in non-lvalue context. */
5255 if (TREE_CODE (type
) != REFERENCE_TYPE
5256 && TREE_CODE (value
) == NOP_EXPR
5257 && TREE_TYPE (value
) == TREE_TYPE (TREE_OPERAND (value
, 0)))
5258 value
= TREE_OPERAND (value
, 0);
5260 if (TREE_CODE (value
) == OFFSET_REF
)
5261 value
= resolve_offset_ref (value
);
5263 if (TREE_CODE (type
) == ARRAY_TYPE
)
5265 /* Allow casting from T1* to T2[] because Cfront allows it.
5266 NIHCL uses it. It is not valid ISO C++ however. */
5267 if (TREE_CODE (TREE_TYPE (expr
)) == POINTER_TYPE
)
5269 pedwarn ("ISO C++ forbids casting to an array type `%T'", type
);
5270 type
= build_pointer_type (TREE_TYPE (type
));
5274 error ("ISO C++ forbids casting to an array type `%T'", type
);
5275 return error_mark_node
;
5279 if (TREE_CODE (type
) == FUNCTION_TYPE
5280 || TREE_CODE (type
) == METHOD_TYPE
)
5282 error ("invalid cast to function type `%T'", type
);
5283 return error_mark_node
;
5286 if (TREE_CODE (type
) == VOID_TYPE
)
5288 /* Conversion to void does not cause any of the normal function to
5289 * pointer, array to pointer and lvalue to rvalue decays. */
5291 value
= convert_to_void (value
, /*implicit=*/NULL
);
5294 /* Convert functions and arrays to pointers and
5295 convert references to their expanded types,
5296 but don't convert any other types. If, however, we are
5297 casting to a class type, there's no reason to do this: the
5298 cast will only succeed if there is a converting constructor,
5299 and the default conversions will be done at that point. In
5300 fact, doing the default conversion here is actually harmful
5304 struct S { S(const A&); };
5306 since we don't want the array-to-pointer conversion done. */
5307 if (!IS_AGGR_TYPE (type
))
5309 if (TREE_CODE (TREE_TYPE (value
)) == FUNCTION_TYPE
5310 || (TREE_CODE (TREE_TYPE (value
)) == METHOD_TYPE
5311 /* Don't do the default conversion on a ->* expression. */
5312 && ! (TREE_CODE (type
) == POINTER_TYPE
5313 && bound_pmf_p (value
)))
5314 || TREE_CODE (TREE_TYPE (value
)) == ARRAY_TYPE
5315 || TREE_CODE (TREE_TYPE (value
)) == REFERENCE_TYPE
)
5316 value
= default_conversion (value
);
5318 else if (TREE_CODE (TREE_TYPE (value
)) == REFERENCE_TYPE
)
5319 /* However, even for class types, we still need to strip away
5320 the reference type, since the call to convert_force below
5321 does not expect the input expression to be of reference
5323 value
= convert_from_reference (value
);
5325 otype
= TREE_TYPE (value
);
5327 /* Optionally warn about potentially worrisome casts. */
5330 && TREE_CODE (type
) == POINTER_TYPE
5331 && TREE_CODE (otype
) == POINTER_TYPE
5332 && !at_least_as_qualified_p (TREE_TYPE (type
),
5334 warning ("cast from `%T' to `%T' discards qualifiers from pointer target type",
5337 if (TREE_CODE (type
) == INTEGER_TYPE
5338 && TREE_CODE (otype
) == POINTER_TYPE
5339 && TYPE_PRECISION (type
) != TYPE_PRECISION (otype
))
5340 warning ("cast from pointer to integer of different size");
5342 if (TREE_CODE (type
) == POINTER_TYPE
5343 && TREE_CODE (otype
) == INTEGER_TYPE
5344 && TYPE_PRECISION (type
) != TYPE_PRECISION (otype
)
5345 /* Don't warn about converting any constant. */
5346 && !TREE_CONSTANT (value
))
5347 warning ("cast to pointer from integer of different size");
5349 if (TREE_CODE (type
) == REFERENCE_TYPE
)
5350 value
= (convert_from_reference
5351 (convert_to_reference (type
, value
, CONV_C_CAST
,
5352 LOOKUP_COMPLAIN
, NULL_TREE
)));
5357 value
= decl_constant_value (value
);
5360 value
= convert_force (type
, value
, CONV_C_CAST
);
5362 /* Ignore any integer overflow caused by the cast. */
5363 if (TREE_CODE (value
) == INTEGER_CST
)
5365 TREE_OVERFLOW (value
) = TREE_OVERFLOW (ovalue
);
5366 TREE_CONSTANT_OVERFLOW (value
) = TREE_CONSTANT_OVERFLOW (ovalue
);
5370 /* Warn about possible alignment problems. Do this here when we will have
5371 instantiated any necessary template types. */
5372 if (STRICT_ALIGNMENT
&& warn_cast_align
5373 && TREE_CODE (type
) == POINTER_TYPE
5374 && TREE_CODE (otype
) == POINTER_TYPE
5375 && TREE_CODE (TREE_TYPE (otype
)) != VOID_TYPE
5376 && TREE_CODE (TREE_TYPE (otype
)) != FUNCTION_TYPE
5377 && COMPLETE_TYPE_P (TREE_TYPE (otype
))
5378 && COMPLETE_TYPE_P (TREE_TYPE (type
))
5379 && TYPE_ALIGN (TREE_TYPE (type
)) > TYPE_ALIGN (TREE_TYPE (otype
)))
5380 warning ("cast from `%T' to `%T' increases required alignment of target type",
5383 /* Always produce some operator for an explicit cast,
5384 so we can tell (for -pedantic) that the cast is no lvalue. */
5385 if (TREE_CODE (type
) != REFERENCE_TYPE
&& value
== expr
5386 && real_lvalue_p (value
))
5387 value
= non_lvalue (value
);
5392 /* Build an assignment expression of lvalue LHS from value RHS.
5393 MODIFYCODE is the code for a binary operator that we use
5394 to combine the old value of LHS with RHS to get the new value.
5395 Or else MODIFYCODE is NOP_EXPR meaning do a simple assignment.
5397 C++: If MODIFYCODE is INIT_EXPR, then leave references unbashed. */
5400 build_modify_expr (lhs
, modifycode
, rhs
)
5402 enum tree_code modifycode
;
5405 register tree result
;
5407 tree lhstype
= TREE_TYPE (lhs
);
5408 tree olhstype
= lhstype
;
5411 /* Avoid duplicate error messages from operands that had errors. */
5412 if (lhs
== error_mark_node
|| rhs
== error_mark_node
)
5413 return error_mark_node
;
5415 /* Handle control structure constructs used as "lvalues". */
5416 switch (TREE_CODE (lhs
))
5418 /* Handle --foo = 5; as these are valid constructs in C++ */
5419 case PREDECREMENT_EXPR
:
5420 case PREINCREMENT_EXPR
:
5421 if (TREE_SIDE_EFFECTS (TREE_OPERAND (lhs
, 0)))
5422 lhs
= build (TREE_CODE (lhs
), TREE_TYPE (lhs
),
5423 stabilize_reference (TREE_OPERAND (lhs
, 0)),
5424 TREE_OPERAND (lhs
, 1));
5425 return build (COMPOUND_EXPR
, lhstype
,
5427 build_modify_expr (TREE_OPERAND (lhs
, 0),
5430 /* Handle (a, b) used as an "lvalue". */
5432 newrhs
= build_modify_expr (TREE_OPERAND (lhs
, 1),
5434 if (newrhs
== error_mark_node
)
5435 return error_mark_node
;
5436 return build (COMPOUND_EXPR
, lhstype
,
5437 TREE_OPERAND (lhs
, 0), newrhs
);
5440 newrhs
= build_modify_expr (TREE_OPERAND (lhs
, 0), modifycode
, rhs
);
5441 if (newrhs
== error_mark_node
)
5442 return error_mark_node
;
5443 return build (COMPOUND_EXPR
, lhstype
, lhs
, newrhs
);
5445 /* Handle (a ? b : c) used as an "lvalue". */
5448 /* Produce (a ? (b = rhs) : (c = rhs))
5449 except that the RHS goes through a save-expr
5450 so the code to compute it is only emitted once. */
5453 rhs
= save_expr (rhs
);
5455 /* Check this here to avoid odd errors when trying to convert
5456 a throw to the type of the COND_EXPR. */
5457 if (!lvalue_or_else (lhs
, "assignment"))
5458 return error_mark_node
;
5460 cond
= build_conditional_expr
5461 (TREE_OPERAND (lhs
, 0),
5462 build_modify_expr (cp_convert (TREE_TYPE (lhs
),
5463 TREE_OPERAND (lhs
, 1)),
5465 build_modify_expr (cp_convert (TREE_TYPE (lhs
),
5466 TREE_OPERAND (lhs
, 2)),
5469 if (cond
== error_mark_node
)
5471 /* Make sure the code to compute the rhs comes out
5472 before the split. */
5473 return build (COMPOUND_EXPR
, TREE_TYPE (lhs
),
5474 /* Cast to void to suppress warning
5475 from warn_if_unused_value. */
5476 cp_convert (void_type_node
, rhs
), cond
);
5480 lhs
= resolve_offset_ref (lhs
);
5481 if (lhs
== error_mark_node
)
5482 return error_mark_node
;
5483 olhstype
= lhstype
= TREE_TYPE (lhs
);
5489 if (modifycode
== INIT_EXPR
)
5491 if (TREE_CODE (rhs
) == CONSTRUCTOR
)
5493 my_friendly_assert (same_type_p (TREE_TYPE (rhs
), lhstype
),
5495 result
= build (INIT_EXPR
, lhstype
, lhs
, rhs
);
5496 TREE_SIDE_EFFECTS (result
) = 1;
5499 else if (! IS_AGGR_TYPE (lhstype
))
5500 /* Do the default thing */;
5503 result
= build_method_call (lhs
, complete_ctor_identifier
,
5504 build_tree_list (NULL_TREE
, rhs
),
5505 TYPE_BINFO (lhstype
), LOOKUP_NORMAL
);
5506 if (result
== NULL_TREE
)
5507 return error_mark_node
;
5513 if (TREE_CODE (lhstype
) == REFERENCE_TYPE
)
5515 lhs
= convert_from_reference (lhs
);
5516 olhstype
= lhstype
= TREE_TYPE (lhs
);
5518 lhs
= require_complete_type (lhs
);
5519 if (lhs
== error_mark_node
)
5520 return error_mark_node
;
5522 if (modifycode
== NOP_EXPR
)
5524 /* `operator=' is not an inheritable operator. */
5525 if (! IS_AGGR_TYPE (lhstype
))
5526 /* Do the default thing */;
5529 result
= build_opfncall (MODIFY_EXPR
, LOOKUP_NORMAL
,
5530 lhs
, rhs
, make_node (NOP_EXPR
));
5531 if (result
== NULL_TREE
)
5532 return error_mark_node
;
5539 /* A binary op has been requested. Combine the old LHS
5540 value with the RHS producing the value we should actually
5541 store into the LHS. */
5543 my_friendly_assert (!PROMOTES_TO_AGGR_TYPE (lhstype
, REFERENCE_TYPE
),
5545 lhs
= stabilize_reference (lhs
);
5546 newrhs
= cp_build_binary_op (modifycode
, lhs
, rhs
);
5547 if (newrhs
== error_mark_node
)
5549 error (" in evaluation of `%Q(%#T, %#T)'", modifycode
,
5550 TREE_TYPE (lhs
), TREE_TYPE (rhs
));
5551 return error_mark_node
;
5554 /* Now it looks like a plain assignment. */
5555 modifycode
= NOP_EXPR
;
5557 my_friendly_assert (TREE_CODE (lhstype
) != REFERENCE_TYPE
, 20011220);
5558 my_friendly_assert (TREE_CODE (TREE_TYPE (newrhs
)) != REFERENCE_TYPE
,
5562 /* Handle a cast used as an "lvalue".
5563 We have already performed any binary operator using the value as cast.
5564 Now convert the result to the cast type of the lhs,
5565 and then true type of the lhs and store it there;
5566 then convert result back to the cast type to be the value
5567 of the assignment. */
5569 switch (TREE_CODE (lhs
))
5574 case FIX_TRUNC_EXPR
:
5575 case FIX_FLOOR_EXPR
:
5576 case FIX_ROUND_EXPR
:
5579 tree inner_lhs
= TREE_OPERAND (lhs
, 0);
5582 if (TREE_CODE (TREE_TYPE (newrhs
)) == ARRAY_TYPE
5583 || TREE_CODE (TREE_TYPE (newrhs
)) == FUNCTION_TYPE
5584 || TREE_CODE (TREE_TYPE (newrhs
)) == METHOD_TYPE
5585 || TREE_CODE (TREE_TYPE (newrhs
)) == OFFSET_TYPE
)
5586 newrhs
= default_conversion (newrhs
);
5588 /* ISO C++ 5.4/1: The result is an lvalue if T is a reference
5589 type, otherwise the result is an rvalue. */
5590 if (! lvalue_p (lhs
))
5591 pedwarn ("ISO C++ forbids cast to non-reference type used as lvalue");
5593 result
= build_modify_expr (inner_lhs
, NOP_EXPR
,
5594 cp_convert (TREE_TYPE (inner_lhs
),
5595 cp_convert (lhstype
, newrhs
)));
5596 if (result
== error_mark_node
)
5598 return cp_convert (TREE_TYPE (lhs
), result
);
5605 /* Now we have handled acceptable kinds of LHS that are not truly lvalues.
5606 Reject anything strange now. */
5608 if (!lvalue_or_else (lhs
, "assignment"))
5609 return error_mark_node
;
5611 /* Warn about modifying something that is `const'. Don't warn if
5612 this is initialization. */
5613 if (modifycode
!= INIT_EXPR
5614 && (TREE_READONLY (lhs
) || CP_TYPE_CONST_P (lhstype
)
5615 /* Functions are not modifiable, even though they are
5617 || TREE_CODE (TREE_TYPE (lhs
)) == FUNCTION_TYPE
5618 || TREE_CODE (TREE_TYPE (lhs
)) == METHOD_TYPE
5619 /* If it's an aggregate and any field is const, then it is
5620 effectively const. */
5621 || (IS_AGGR_TYPE_CODE (TREE_CODE (lhstype
))
5622 && C_TYPE_FIELDS_READONLY (lhstype
))))
5623 readonly_error (lhs
, "assignment", 0);
5625 /* If storing into a structure or union member, it has probably been
5626 given type `int'. Compute the type that would go with the actual
5627 amount of storage the member occupies. */
5629 if (TREE_CODE (lhs
) == COMPONENT_REF
5630 && (TREE_CODE (lhstype
) == INTEGER_TYPE
5631 || TREE_CODE (lhstype
) == REAL_TYPE
5632 || TREE_CODE (lhstype
) == ENUMERAL_TYPE
))
5634 lhstype
= TREE_TYPE (get_unwidened (lhs
, 0));
5636 /* If storing in a field that is in actuality a short or narrower
5637 than one, we must store in the field in its actual type. */
5639 if (lhstype
!= TREE_TYPE (lhs
))
5641 lhs
= copy_node (lhs
);
5642 TREE_TYPE (lhs
) = lhstype
;
5646 if (TREE_CODE (lhstype
) != REFERENCE_TYPE
)
5648 if (TREE_SIDE_EFFECTS (lhs
))
5649 lhs
= stabilize_reference (lhs
);
5650 if (TREE_SIDE_EFFECTS (newrhs
))
5651 newrhs
= stabilize_reference (newrhs
);
5654 /* Convert new value to destination type. */
5656 if (TREE_CODE (lhstype
) == ARRAY_TYPE
)
5660 if (!same_or_base_type_p (TYPE_MAIN_VARIANT (lhstype
),
5661 TYPE_MAIN_VARIANT (TREE_TYPE (rhs
))))
5663 error ("incompatible types in assignment of `%T' to `%T'",
5664 TREE_TYPE (rhs
), lhstype
);
5665 return error_mark_node
;
5668 /* Allow array assignment in compiler-generated code. */
5669 if (! DECL_ARTIFICIAL (current_function_decl
))
5670 pedwarn ("ISO C++ forbids assignment of arrays");
5672 from_array
= TREE_CODE (TREE_TYPE (newrhs
)) == ARRAY_TYPE
5673 ? 1 + (modifycode
!= INIT_EXPR
): 0;
5674 return build_vec_init (lhs
, newrhs
, from_array
);
5677 if (modifycode
== INIT_EXPR
)
5678 newrhs
= convert_for_initialization (lhs
, lhstype
, newrhs
, LOOKUP_NORMAL
,
5679 "initialization", NULL_TREE
, 0);
5682 /* Avoid warnings on enum bit fields. */
5683 if (TREE_CODE (olhstype
) == ENUMERAL_TYPE
5684 && TREE_CODE (lhstype
) == INTEGER_TYPE
)
5686 newrhs
= convert_for_assignment (olhstype
, newrhs
, "assignment",
5688 newrhs
= convert_force (lhstype
, newrhs
, 0);
5691 newrhs
= convert_for_assignment (lhstype
, newrhs
, "assignment",
5693 if (TREE_CODE (newrhs
) == CALL_EXPR
5694 && TYPE_NEEDS_CONSTRUCTING (lhstype
))
5695 newrhs
= build_cplus_new (lhstype
, newrhs
);
5697 /* Can't initialize directly from a TARGET_EXPR, since that would
5698 cause the lhs to be constructed twice, and possibly result in
5699 accidental self-initialization. So we force the TARGET_EXPR to be
5700 expanded without a target. */
5701 if (TREE_CODE (newrhs
) == TARGET_EXPR
)
5702 newrhs
= build (COMPOUND_EXPR
, TREE_TYPE (newrhs
), newrhs
,
5703 TREE_OPERAND (newrhs
, 0));
5706 if (newrhs
== error_mark_node
)
5707 return error_mark_node
;
5709 if (TREE_CODE (newrhs
) == COND_EXPR
)
5712 tree cond
= TREE_OPERAND (newrhs
, 0);
5714 if (TREE_SIDE_EFFECTS (lhs
))
5715 cond
= build_compound_expr (tree_cons
5717 build_tree_list (NULL_TREE
, cond
)));
5719 /* Cannot have two identical lhs on this one tree (result) as preexpand
5720 calls will rip them out and fill in RTL for them, but when the
5721 rtl is generated, the calls will only be in the first side of the
5722 condition, not on both, or before the conditional jump! (mrs) */
5723 lhs1
= break_out_calls (lhs
);
5726 /* If there's no change, the COND_EXPR behaves like any other rhs. */
5727 result
= build (modifycode
== NOP_EXPR
? MODIFY_EXPR
: INIT_EXPR
,
5728 lhstype
, lhs
, newrhs
);
5731 tree result_type
= TREE_TYPE (newrhs
);
5732 /* We have to convert each arm to the proper type because the
5733 types may have been munged by constant folding. */
5735 = build (COND_EXPR
, result_type
, cond
,
5736 build_modify_expr (lhs
, modifycode
,
5737 cp_convert (result_type
,
5738 TREE_OPERAND (newrhs
, 1))),
5739 build_modify_expr (lhs1
, modifycode
,
5740 cp_convert (result_type
,
5741 TREE_OPERAND (newrhs
, 2))));
5745 result
= build (modifycode
== NOP_EXPR
? MODIFY_EXPR
: INIT_EXPR
,
5746 lhstype
, lhs
, newrhs
);
5748 TREE_SIDE_EFFECTS (result
) = 1;
5750 /* If we got the LHS in a different type for storing in,
5751 convert the result back to the nominal type of LHS
5752 so that the value we return always has the same type
5753 as the LHS argument. */
5755 if (olhstype
== TREE_TYPE (result
))
5757 /* Avoid warnings converting integral types back into enums
5758 for enum bit fields. */
5759 if (TREE_CODE (TREE_TYPE (result
)) == INTEGER_TYPE
5760 && TREE_CODE (olhstype
) == ENUMERAL_TYPE
)
5762 result
= build (COMPOUND_EXPR
, olhstype
, result
, olhs
);
5763 TREE_NO_UNUSED_WARNING (result
) = 1;
5766 return convert_for_assignment (olhstype
, result
, "assignment",
5771 build_x_modify_expr (lhs
, modifycode
, rhs
)
5773 enum tree_code modifycode
;
5776 if (processing_template_decl
)
5777 return build_min_nt (MODOP_EXPR
, lhs
,
5778 build_min_nt (modifycode
, NULL_TREE
, NULL_TREE
), rhs
);
5780 if (modifycode
!= NOP_EXPR
)
5782 tree rval
= build_opfncall (MODIFY_EXPR
, LOOKUP_NORMAL
, lhs
, rhs
,
5783 make_node (modifycode
));
5787 return build_modify_expr (lhs
, modifycode
, rhs
);
5791 /* Get difference in deltas for different pointer to member function
5792 types. Return integer_zero_node, if FROM cannot be converted to a
5793 TO type. If FORCE is true, then allow reverse conversions as well.
5795 Note that the naming of FROM and TO is kind of backwards; the return
5796 value is what we add to a TO in order to get a FROM. They are named
5797 this way because we call this function to find out how to convert from
5798 a pointer to member of FROM to a pointer to member of TO. */
5801 get_delta_difference (from
, to
, force
)
5805 tree delta
= integer_zero_node
;
5810 binfo
= lookup_base (to
, from
, ba_check
, &kind
);
5811 if (kind
== bk_inaccessible
|| kind
== bk_ambig
)
5813 error (" in pointer to member function conversion");
5820 error_not_base_type (from
, to
);
5821 error (" in pointer to member conversion");
5824 binfo
= lookup_base (from
, to
, ba_check
, &kind
);
5827 virt_binfo
= binfo_from_vbase (binfo
);
5831 /* This is a reinterpret cast, we choose to do nothing. */
5832 warning ("pointer to member cast via virtual base `%T' of `%T'",
5833 BINFO_TYPE (virt_binfo
),
5834 BINFO_TYPE (BINFO_INHERITANCE_CHAIN (virt_binfo
)));
5837 delta
= BINFO_OFFSET (binfo
);
5838 delta
= cp_convert (ptrdiff_type_node
, delta
);
5839 delta
= cp_build_binary_op (MINUS_EXPR
,
5846 virt_binfo
= binfo_from_vbase (binfo
);
5849 /* This is a reinterpret cast, we choose to do nothing. */
5851 warning ("pointer to member cast via virtual base `%T' of `%T'",
5852 BINFO_TYPE (virt_binfo
),
5853 BINFO_TYPE (BINFO_INHERITANCE_CHAIN (virt_binfo
)));
5855 error ("pointer to member conversion via virtual base `%T' of `%T'",
5856 BINFO_TYPE (virt_binfo
),
5857 BINFO_TYPE (BINFO_INHERITANCE_CHAIN (virt_binfo
)));
5860 delta
= BINFO_OFFSET (binfo
);
5862 return cp_convert (ptrdiff_type_node
, delta
);
5865 /* Return a constructor for the pointer-to-member-function TYPE using
5866 the other components as specified. */
5869 build_ptrmemfunc1 (type
, delta
, pfn
)
5870 tree type
, delta
, pfn
;
5876 /* Pull the FIELD_DECLs out of the type. */
5877 pfn_field
= TYPE_FIELDS (type
);
5878 delta_field
= TREE_CHAIN (pfn_field
);
5880 /* Make sure DELTA has the type we want. */
5881 delta
= convert_and_check (delta_type_node
, delta
);
5883 /* Finish creating the initializer. */
5884 u
= tree_cons (pfn_field
, pfn
,
5885 build_tree_list (delta_field
, delta
));
5886 u
= build (CONSTRUCTOR
, type
, NULL_TREE
, u
);
5887 TREE_CONSTANT (u
) = TREE_CONSTANT (pfn
) && TREE_CONSTANT (delta
);
5888 TREE_STATIC (u
) = (TREE_CONSTANT (u
)
5889 && (initializer_constant_valid_p (pfn
, TREE_TYPE (pfn
))
5891 && (initializer_constant_valid_p (delta
, TREE_TYPE (delta
))
5896 /* Build a constructor for a pointer to member function. It can be
5897 used to initialize global variables, local variable, or used
5898 as a value in expressions. TYPE is the POINTER to METHOD_TYPE we
5901 If FORCE is non-zero, then force this conversion, even if
5902 we would rather not do it. Usually set when using an explicit
5905 Return error_mark_node, if something goes wrong. */
5908 build_ptrmemfunc (type
, pfn
, force
)
5913 tree pfn_type
= TREE_TYPE (pfn
);
5914 tree to_type
= build_ptrmemfunc_type (type
);
5916 /* Handle multiple conversions of pointer to member functions. */
5917 if (TYPE_PTRMEMFUNC_P (TREE_TYPE (pfn
)))
5919 tree delta
= NULL_TREE
;
5920 tree npfn
= NULL_TREE
;
5924 && !can_convert_arg (to_type
, TREE_TYPE (pfn
), pfn
))
5925 error ("invalid conversion to type `%T' from type `%T'",
5928 n
= get_delta_difference (TYPE_PTRMEMFUNC_OBJECT_TYPE (pfn_type
),
5929 TYPE_PTRMEMFUNC_OBJECT_TYPE (to_type
),
5932 /* We don't have to do any conversion to convert a
5933 pointer-to-member to its own type. But, we don't want to
5934 just return a PTRMEM_CST if there's an explicit cast; that
5935 cast should make the expression an invalid template argument. */
5936 if (TREE_CODE (pfn
) != PTRMEM_CST
)
5938 if (same_type_p (to_type
, pfn_type
))
5940 else if (integer_zerop (n
))
5941 return build_reinterpret_cast (to_type
, pfn
);
5944 if (TREE_SIDE_EFFECTS (pfn
))
5945 pfn
= save_expr (pfn
);
5947 /* Obtain the function pointer and the current DELTA. */
5948 if (TREE_CODE (pfn
) == PTRMEM_CST
)
5949 expand_ptrmemfunc_cst (pfn
, &delta
, &npfn
);
5952 npfn
= build_component_ref (pfn
, pfn_identifier
, NULL_TREE
, 0);
5953 delta
= build_component_ref (pfn
, delta_identifier
, NULL_TREE
, 0);
5956 /* Just adjust the DELTA field. */
5957 delta
= cp_convert (ptrdiff_type_node
, delta
);
5958 if (TARGET_PTRMEMFUNC_VBIT_LOCATION
== ptrmemfunc_vbit_in_delta
)
5959 n
= cp_build_binary_op (LSHIFT_EXPR
, n
, integer_one_node
);
5960 delta
= cp_build_binary_op (PLUS_EXPR
, delta
, n
);
5961 return build_ptrmemfunc1 (to_type
, delta
, npfn
);
5964 /* Handle null pointer to member function conversions. */
5965 if (integer_zerop (pfn
))
5967 pfn
= build_c_cast (type
, integer_zero_node
);
5968 return build_ptrmemfunc1 (to_type
,
5973 if (type_unknown_p (pfn
))
5974 return instantiate_type (type
, pfn
, tf_error
| tf_warning
);
5976 fn
= TREE_OPERAND (pfn
, 0);
5977 my_friendly_assert (TREE_CODE (fn
) == FUNCTION_DECL
, 0);
5978 return make_ptrmem_cst (to_type
, fn
);
5981 /* Return the DELTA, IDX, PFN, and DELTA2 values for the PTRMEM_CST
5984 ??? There is no consistency as to the types returned for the above
5985 values. Some code acts as if its a sizetype and some as if its
5986 integer_type_node. */
5989 expand_ptrmemfunc_cst (cst
, delta
, pfn
)
5994 tree type
= TREE_TYPE (cst
);
5995 tree fn
= PTRMEM_CST_MEMBER (cst
);
5996 tree ptr_class
, fn_class
;
5998 my_friendly_assert (TREE_CODE (fn
) == FUNCTION_DECL
, 0);
6000 /* The class that the function belongs to. */
6001 fn_class
= DECL_CONTEXT (fn
);
6003 /* The class that we're creating a pointer to member of. */
6004 ptr_class
= TYPE_PTRMEMFUNC_OBJECT_TYPE (type
);
6006 /* First, calculate the adjustment to the function's class. */
6007 *delta
= get_delta_difference (fn_class
, ptr_class
, /*force=*/0);
6009 if (!DECL_VIRTUAL_P (fn
))
6010 *pfn
= convert (TYPE_PTRMEMFUNC_FN_TYPE (type
), build_addr_func (fn
));
6013 /* If we're dealing with a virtual function, we have to adjust 'this'
6014 again, to point to the base which provides the vtable entry for
6015 fn; the call will do the opposite adjustment. */
6016 tree orig_class
= DECL_VIRTUAL_CONTEXT (fn
);
6017 tree binfo
= binfo_or_else (orig_class
, fn_class
);
6018 *delta
= fold (build (PLUS_EXPR
, TREE_TYPE (*delta
),
6019 *delta
, BINFO_OFFSET (binfo
)));
6021 /* We set PFN to the vtable offset at which the function can be
6022 found, plus one (unless ptrmemfunc_vbit_in_delta, in which
6023 case delta is shifted left, and then incremented). */
6024 *pfn
= DECL_VINDEX (fn
);
6025 *pfn
= fold (build (MULT_EXPR
, integer_type_node
, *pfn
,
6026 TYPE_SIZE_UNIT (vtable_entry_type
)));
6028 switch (TARGET_PTRMEMFUNC_VBIT_LOCATION
)
6030 case ptrmemfunc_vbit_in_pfn
:
6031 *pfn
= fold (build (PLUS_EXPR
, integer_type_node
, *pfn
,
6035 case ptrmemfunc_vbit_in_delta
:
6036 *delta
= fold (build (LSHIFT_EXPR
, TREE_TYPE (*delta
),
6037 *delta
, integer_one_node
));
6038 *delta
= fold (build (PLUS_EXPR
, TREE_TYPE (*delta
),
6039 *delta
, integer_one_node
));
6046 *pfn
= fold (build1 (NOP_EXPR
, TYPE_PTRMEMFUNC_FN_TYPE (type
),
6051 /* Return an expression for PFN from the pointer-to-member function
6055 pfn_from_ptrmemfunc (t
)
6058 if (TREE_CODE (t
) == PTRMEM_CST
)
6063 expand_ptrmemfunc_cst (t
, &delta
, &pfn
);
6068 return build_component_ref (t
, pfn_identifier
, NULL_TREE
, 0);
6071 /* Expression EXPR is about to be implicitly converted to TYPE. Warn
6072 if this is a potentially dangerous thing to do. Returns a possibly
6076 dubious_conversion_warnings (type
, expr
, errtype
, fndecl
, parmnum
)
6079 const char *errtype
;
6083 if (TREE_CODE (type
) == REFERENCE_TYPE
)
6084 type
= TREE_TYPE (type
);
6086 /* Issue warnings about peculiar, but legal, uses of NULL. */
6087 if (ARITHMETIC_TYPE_P (type
) && expr
== null_node
)
6090 warning ("passing NULL used for non-pointer %s %P of `%D'",
6091 errtype
, parmnum
, fndecl
);
6093 warning ("%s to non-pointer type `%T' from NULL", errtype
, type
);
6096 /* Warn about assigning a floating-point type to an integer type. */
6097 if (TREE_CODE (TREE_TYPE (expr
)) == REAL_TYPE
6098 && TREE_CODE (type
) == INTEGER_TYPE
)
6101 warning ("passing `%T' for %s %P of `%D'",
6102 TREE_TYPE (expr
), errtype
, parmnum
, fndecl
);
6104 warning ("%s to `%T' from `%T'", errtype
, type
, TREE_TYPE (expr
));
6106 /* And warn about assigning a negative value to an unsigned
6108 else if (TREE_UNSIGNED (type
) && TREE_CODE (type
) != BOOLEAN_TYPE
)
6110 if (TREE_CODE (expr
) == INTEGER_CST
6111 && TREE_NEGATED_INT (expr
))
6114 warning ("passing negative value `%E' for %s %P of `%D'",
6115 expr
, errtype
, parmnum
, fndecl
);
6117 warning ("%s of negative value `%E' to `%T'",
6118 errtype
, expr
, type
);
6121 overflow_warning (expr
);
6123 if (TREE_CONSTANT (expr
))
6129 /* Convert value RHS to type TYPE as preparation for an assignment to
6130 an lvalue of type TYPE. ERRTYPE is a string to use in error
6131 messages: "assignment", "return", etc. If FNDECL is non-NULL, we
6132 are doing the conversion in order to pass the PARMNUMth argument of
6136 convert_for_assignment (type
, rhs
, errtype
, fndecl
, parmnum
)
6138 const char *errtype
;
6142 register enum tree_code codel
= TREE_CODE (type
);
6143 register tree rhstype
;
6144 register enum tree_code coder
;
6146 if (codel
== OFFSET_TYPE
)
6149 if (TREE_CODE (rhs
) == OFFSET_REF
)
6150 rhs
= resolve_offset_ref (rhs
);
6152 /* Strip NON_LVALUE_EXPRs since we aren't using as an lvalue. */
6153 if (TREE_CODE (rhs
) == NON_LVALUE_EXPR
)
6154 rhs
= TREE_OPERAND (rhs
, 0);
6156 rhstype
= TREE_TYPE (rhs
);
6157 coder
= TREE_CODE (rhstype
);
6159 if (rhs
== error_mark_node
|| rhstype
== error_mark_node
)
6160 return error_mark_node
;
6161 if (TREE_CODE (rhs
) == TREE_LIST
&& TREE_VALUE (rhs
) == error_mark_node
)
6162 return error_mark_node
;
6164 rhs
= dubious_conversion_warnings (type
, rhs
, errtype
, fndecl
, parmnum
);
6166 /* The RHS of an assignment cannot have void type. */
6167 if (coder
== VOID_TYPE
)
6169 error ("void value not ignored as it ought to be");
6170 return error_mark_node
;
6173 /* Simplify the RHS if possible. */
6174 if (TREE_CODE (rhs
) == CONST_DECL
)
6175 rhs
= DECL_INITIAL (rhs
);
6176 else if (coder
!= ARRAY_TYPE
)
6177 rhs
= decl_constant_value (rhs
);
6181 The expression is implicitly converted (clause _conv_) to the
6182 cv-unqualified type of the left operand.
6184 We allow bad conversions here because by the time we get to this point
6185 we are committed to doing the conversion. If we end up doing a bad
6186 conversion, convert_like will complain. */
6187 if (!can_convert_arg_bad (type
, rhstype
, rhs
))
6189 /* When -Wno-pmf-conversions is use, we just silently allow
6190 conversions from pointers-to-members to plain pointers. If
6191 the conversion doesn't work, cp_convert will complain. */
6193 && TYPE_PTR_P (type
)
6194 && TYPE_PTRMEMFUNC_P (rhstype
))
6195 rhs
= cp_convert (strip_top_quals (type
), rhs
);
6198 /* If the right-hand side has unknown type, then it is an
6199 overloaded function. Call instantiate_type to get error
6201 if (rhstype
== unknown_type_node
)
6202 instantiate_type (type
, rhs
, tf_error
| tf_warning
);
6204 error ("cannot convert `%T' to `%T' for argument `%P' to `%D'",
6205 rhstype
, type
, parmnum
, fndecl
);
6207 error ("cannot convert `%T' to `%T' in %s", rhstype
, type
,
6209 return error_mark_node
;
6212 return perform_implicit_conversion (strip_top_quals (type
), rhs
);
6215 /* Convert RHS to be of type TYPE.
6216 If EXP is non-zero, it is the target of the initialization.
6217 ERRTYPE is a string to use in error messages.
6219 Two major differences between the behavior of
6220 `convert_for_assignment' and `convert_for_initialization'
6221 are that references are bashed in the former, while
6222 copied in the latter, and aggregates are assigned in
6223 the former (operator=) while initialized in the
6226 If using constructor make sure no conversion operator exists, if one does
6227 exist, an ambiguity exists.
6229 If flags doesn't include LOOKUP_COMPLAIN, don't complain about anything. */
6232 convert_for_initialization (exp
, type
, rhs
, flags
, errtype
, fndecl
, parmnum
)
6233 tree exp
, type
, rhs
;
6235 const char *errtype
;
6239 register enum tree_code codel
= TREE_CODE (type
);
6240 register tree rhstype
;
6241 register enum tree_code coder
;
6243 /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
6244 Strip such NOP_EXPRs, since RHS is used in non-lvalue context. */
6245 if (TREE_CODE (rhs
) == NOP_EXPR
6246 && TREE_TYPE (rhs
) == TREE_TYPE (TREE_OPERAND (rhs
, 0))
6247 && codel
!= REFERENCE_TYPE
)
6248 rhs
= TREE_OPERAND (rhs
, 0);
6250 if (rhs
== error_mark_node
6251 || (TREE_CODE (rhs
) == TREE_LIST
&& TREE_VALUE (rhs
) == error_mark_node
))
6252 return error_mark_node
;
6254 if (TREE_CODE (rhs
) == OFFSET_REF
)
6256 rhs
= resolve_offset_ref (rhs
);
6257 if (rhs
== error_mark_node
)
6258 return error_mark_node
;
6261 if (TREE_CODE (TREE_TYPE (rhs
)) == REFERENCE_TYPE
)
6262 rhs
= convert_from_reference (rhs
);
6264 if ((TREE_CODE (TREE_TYPE (rhs
)) == ARRAY_TYPE
6265 && TREE_CODE (type
) != ARRAY_TYPE
6266 && (TREE_CODE (type
) != REFERENCE_TYPE
6267 || TREE_CODE (TREE_TYPE (type
)) != ARRAY_TYPE
))
6268 || (TREE_CODE (TREE_TYPE (rhs
)) == FUNCTION_TYPE
6269 && (TREE_CODE (type
) != REFERENCE_TYPE
6270 || TREE_CODE (TREE_TYPE (type
)) != FUNCTION_TYPE
))
6271 || TREE_CODE (TREE_TYPE (rhs
)) == METHOD_TYPE
)
6272 rhs
= default_conversion (rhs
);
6274 rhstype
= TREE_TYPE (rhs
);
6275 coder
= TREE_CODE (rhstype
);
6277 if (coder
== ERROR_MARK
)
6278 return error_mark_node
;
6280 /* We accept references to incomplete types, so we can
6281 return here before checking if RHS is of complete type. */
6283 if (codel
== REFERENCE_TYPE
)
6285 /* This should eventually happen in convert_arguments. */
6286 int savew
= 0, savee
= 0;
6289 savew
= warningcount
, savee
= errorcount
;
6290 rhs
= initialize_reference (type
, rhs
);
6293 if (warningcount
> savew
)
6294 cp_warning_at ("in passing argument %P of `%+D'", parmnum
, fndecl
);
6295 else if (errorcount
> savee
)
6296 cp_error_at ("in passing argument %P of `%+D'", parmnum
, fndecl
);
6302 exp
= require_complete_type (exp
);
6303 if (exp
== error_mark_node
)
6304 return error_mark_node
;
6306 if (TREE_CODE (rhstype
) == REFERENCE_TYPE
)
6307 rhstype
= TREE_TYPE (rhstype
);
6309 type
= complete_type (type
);
6311 if (IS_AGGR_TYPE (type
))
6312 return ocp_convert (type
, rhs
, CONV_IMPLICIT
|CONV_FORCE_TEMP
, flags
);
6314 return convert_for_assignment (type
, rhs
, errtype
, fndecl
, parmnum
);
6317 /* Expand an ASM statement with operands, handling output operands
6318 that are not variables or INDIRECT_REFS by transforming such
6319 cases into cases that expand_asm_operands can handle.
6321 Arguments are same as for expand_asm_operands.
6323 We don't do default conversions on all inputs, because it can screw
6324 up operands that are expected to be in memory. */
6327 c_expand_asm_operands (string
, outputs
, inputs
, clobbers
, vol
, filename
, line
)
6328 tree string
, outputs
, inputs
, clobbers
;
6330 const char *filename
;
6333 int noutputs
= list_length (outputs
);
6335 /* o[I] is the place that output number I should be written. */
6336 register tree
*o
= (tree
*) alloca (noutputs
* sizeof (tree
));
6339 /* Record the contents of OUTPUTS before it is modified. */
6340 for (i
= 0, tail
= outputs
; tail
; tail
= TREE_CHAIN (tail
), i
++)
6341 o
[i
] = TREE_VALUE (tail
);
6343 /* Generate the ASM_OPERANDS insn;
6344 store into the TREE_VALUEs of OUTPUTS some trees for
6345 where the values were actually stored. */
6346 expand_asm_operands (string
, outputs
, inputs
, clobbers
, vol
, filename
, line
);
6348 /* Copy all the intermediate outputs into the specified outputs. */
6349 for (i
= 0, tail
= outputs
; tail
; tail
= TREE_CHAIN (tail
), i
++)
6351 if (o
[i
] != TREE_VALUE (tail
))
6353 expand_expr (build_modify_expr (o
[i
], NOP_EXPR
, TREE_VALUE (tail
)),
6354 const0_rtx
, VOIDmode
, EXPAND_NORMAL
);
6357 /* Restore the original value so that it's correct the next
6358 time we expand this function. */
6359 TREE_VALUE (tail
) = o
[i
];
6361 /* Detect modification of read-only values.
6362 (Otherwise done by build_modify_expr.) */
6365 tree type
= TREE_TYPE (o
[i
]);
6366 if (CP_TYPE_CONST_P (type
)
6367 || (IS_AGGR_TYPE_CODE (TREE_CODE (type
))
6368 && C_TYPE_FIELDS_READONLY (type
)))
6369 readonly_error (o
[i
], "modification by `asm'", 1);
6373 /* Those MODIFY_EXPRs could do autoincrements. */
6377 /* If RETVAL is the address of, or a reference to, a local variable or
6378 temporary give an appropraite warning. */
6381 maybe_warn_about_returning_address_of_local (retval
)
6384 tree valtype
= TREE_TYPE (DECL_RESULT (current_function_decl
));
6385 tree whats_returned
= retval
;
6389 if (TREE_CODE (whats_returned
) == COMPOUND_EXPR
)
6390 whats_returned
= TREE_OPERAND (whats_returned
, 1);
6391 else if (TREE_CODE (whats_returned
) == CONVERT_EXPR
6392 || TREE_CODE (whats_returned
) == NON_LVALUE_EXPR
6393 || TREE_CODE (whats_returned
) == NOP_EXPR
)
6394 whats_returned
= TREE_OPERAND (whats_returned
, 0);
6399 if (TREE_CODE (whats_returned
) != ADDR_EXPR
)
6401 whats_returned
= TREE_OPERAND (whats_returned
, 0);
6403 if (TREE_CODE (valtype
) == REFERENCE_TYPE
)
6405 if (TREE_CODE (whats_returned
) == AGGR_INIT_EXPR
6406 || TREE_CODE (whats_returned
) == TARGET_EXPR
)
6408 /* Get the target. */
6409 whats_returned
= TREE_OPERAND (whats_returned
, 0);
6410 warning ("returning reference to temporary");
6413 if (TREE_CODE (whats_returned
) == VAR_DECL
6414 && DECL_NAME (whats_returned
)
6415 && TEMP_NAME_P (DECL_NAME (whats_returned
)))
6417 warning ("reference to non-lvalue returned");
6422 if (TREE_CODE (whats_returned
) == VAR_DECL
6423 && DECL_NAME (whats_returned
)
6424 && DECL_FUNCTION_SCOPE_P (whats_returned
)
6425 && !(TREE_STATIC (whats_returned
)
6426 || TREE_PUBLIC (whats_returned
)))
6428 if (TREE_CODE (valtype
) == REFERENCE_TYPE
)
6429 cp_warning_at ("reference to local variable `%D' returned",
6432 cp_warning_at ("address of local variable `%D' returned",
6438 /* Check that returning RETVAL from the current function is legal.
6439 Return an expression explicitly showing all conversions required to
6440 change RETVAL into the function return type, and to assign it to
6441 the DECL_RESULT for the function. */
6444 check_return_expr (retval
)
6448 /* The type actually returned by the function, after any
6451 int fn_returns_value_p
;
6453 /* A `volatile' function is one that isn't supposed to return, ever.
6454 (This is a G++ extension, used to get better code for functions
6455 that call the `volatile' function.) */
6456 if (TREE_THIS_VOLATILE (current_function_decl
))
6457 warning ("function declared `noreturn' has a `return' statement");
6459 /* Check for various simple errors. */
6460 if (DECL_DESTRUCTOR_P (current_function_decl
))
6463 error ("returning a value from a destructor");
6466 else if (DECL_CONSTRUCTOR_P (current_function_decl
))
6468 if (in_function_try_handler
)
6469 /* If a return statement appears in a handler of the
6470 function-try-block of a constructor, the program is ill-formed. */
6471 error ("cannot return from a handler of a function-try-block of a constructor");
6473 /* You can't return a value from a constructor. */
6474 error ("returning a value from a constructor");
6478 /* When no explicit return-value is given in a function with a named
6479 return value, the named return value is used. */
6480 result
= DECL_RESULT (current_function_decl
);
6481 valtype
= TREE_TYPE (result
);
6482 my_friendly_assert (valtype
!= NULL_TREE
, 19990924);
6483 fn_returns_value_p
= !VOID_TYPE_P (valtype
);
6484 if (!retval
&& DECL_NAME (result
) && fn_returns_value_p
)
6487 /* Check for a return statement with no return value in a function
6488 that's supposed to return a value. */
6489 if (!retval
&& fn_returns_value_p
)
6491 pedwarn ("return-statement with no value, in function declared with a non-void return type");
6492 /* Clear this, so finish_function won't say that we reach the
6493 end of a non-void function (which we don't, we gave a
6495 current_function_returns_null
= 0;
6497 /* Check for a return statement with a value in a function that
6498 isn't supposed to return a value. */
6499 else if (retval
&& !fn_returns_value_p
)
6501 if (VOID_TYPE_P (TREE_TYPE (retval
)))
6502 /* You can return a `void' value from a function of `void'
6503 type. In that case, we have to evaluate the expression for
6504 its side-effects. */
6505 finish_expr_stmt (retval
);
6507 pedwarn ("return-statement with a value, in function declared with a void return type");
6509 current_function_returns_null
= 1;
6511 /* There's really no value to return, after all. */
6515 /* Remember that this function can sometimes return without a
6517 current_function_returns_null
= 1;
6519 /* Remember that this function did return a value. */
6520 current_function_returns_value
= 1;
6522 /* Only operator new(...) throw(), can return NULL [expr.new/13]. */
6523 if ((DECL_OVERLOADED_OPERATOR_P (current_function_decl
) == NEW_EXPR
6524 || DECL_OVERLOADED_OPERATOR_P (current_function_decl
) == VEC_NEW_EXPR
)
6525 && !TYPE_NOTHROW_P (TREE_TYPE (current_function_decl
))
6527 && null_ptr_cst_p (retval
))
6528 warning ("`operator new' must not return NULL unless it is declared `throw()' (or -fcheck-new is in effect)");
6530 /* Effective C++ rule 15. See also start_function. */
6532 && DECL_NAME (current_function_decl
) == ansi_assopname(NOP_EXPR
)
6533 && retval
!= current_class_ref
)
6534 warning ("`operator=' should return a reference to `*this'");
6536 /* The fabled Named Return Value optimization, as per [class.copy]/15:
6538 [...] For a function with a class return type, if the expression
6539 in the return statement is the name of a local object, and the cv-
6540 unqualified type of the local object is the same as the function
6541 return type, an implementation is permitted to omit creating the tem-
6542 porary object to hold the function return value [...]
6544 So, if this is a value-returning function that always returns the same
6545 local variable, remember it.
6547 It might be nice to be more flexible, and choose the first suitable
6548 variable even if the function sometimes returns something else, but
6549 then we run the risk of clobbering the variable we chose if the other
6550 returned expression uses the chosen variable somehow. And people expect
6551 this restriction, anyway. (jason 2000-11-19)
6553 See finish_function, genrtl_start_function, and declare_return_variable
6554 for other pieces of this optimization. */
6556 if (fn_returns_value_p
&& flag_elide_constructors
)
6558 if (retval
!= NULL_TREE
6559 && (current_function_return_value
== NULL_TREE
6560 || current_function_return_value
== retval
)
6561 && TREE_CODE (retval
) == VAR_DECL
6562 && DECL_CONTEXT (retval
) == current_function_decl
6563 && ! TREE_STATIC (retval
)
6564 && (DECL_ALIGN (retval
)
6565 >= DECL_ALIGN (DECL_RESULT (current_function_decl
)))
6566 && same_type_p ((TYPE_MAIN_VARIANT
6567 (TREE_TYPE (retval
))),
6569 (TREE_TYPE (TREE_TYPE (current_function_decl
))))))
6570 current_function_return_value
= retval
;
6572 current_function_return_value
= error_mark_node
;
6575 /* We don't need to do any conversions when there's nothing being
6577 if (!retval
|| retval
== error_mark_node
)
6580 /* Do any required conversions. */
6581 if (retval
== result
|| DECL_CONSTRUCTOR_P (current_function_decl
))
6582 /* No conversions are required. */
6586 /* The type the function is declared to return. */
6587 tree functype
= TREE_TYPE (TREE_TYPE (current_function_decl
));
6589 /* First convert the value to the function's return type, then
6590 to the type of return value's location to handle the
6591 case that functype is smaller than the valtype. */
6592 retval
= convert_for_initialization
6593 (NULL_TREE
, functype
, retval
, LOOKUP_NORMAL
|LOOKUP_ONLYCONVERTING
,
6594 "return", NULL_TREE
, 0);
6595 retval
= convert (valtype
, retval
);
6597 /* If the conversion failed, treat this just like `return;'. */
6598 if (retval
== error_mark_node
)
6600 /* We can't initialize a register from a AGGR_INIT_EXPR. */
6601 else if (! current_function_returns_struct
6602 && TREE_CODE (retval
) == TARGET_EXPR
6603 && TREE_CODE (TREE_OPERAND (retval
, 1)) == AGGR_INIT_EXPR
)
6604 retval
= build (COMPOUND_EXPR
, TREE_TYPE (retval
), retval
,
6605 TREE_OPERAND (retval
, 0));
6607 maybe_warn_about_returning_address_of_local (retval
);
6610 /* Actually copy the value returned into the appropriate location. */
6611 if (retval
&& retval
!= result
)
6612 retval
= build (INIT_EXPR
, TREE_TYPE (result
), result
, retval
);
6618 /* Returns non-zero if the pointer-type FROM can be converted to the
6619 pointer-type TO via a qualification conversion. If CONSTP is -1,
6620 then we return non-zero if the pointers are similar, and the
6621 cv-qualification signature of FROM is a proper subset of that of TO.
6623 If CONSTP is positive, then all outer pointers have been
6627 comp_ptr_ttypes_real (to
, from
, constp
)
6631 int to_more_cv_qualified
= 0;
6633 for (; ; to
= TREE_TYPE (to
), from
= TREE_TYPE (from
))
6635 if (TREE_CODE (to
) != TREE_CODE (from
))
6638 if (TREE_CODE (from
) == OFFSET_TYPE
6639 && same_type_p (TYPE_OFFSET_BASETYPE (from
),
6640 TYPE_OFFSET_BASETYPE (to
)))
6643 /* Const and volatile mean something different for function types,
6644 so the usual checks are not appropriate. */
6645 if (TREE_CODE (to
) != FUNCTION_TYPE
&& TREE_CODE (to
) != METHOD_TYPE
)
6647 if (!at_least_as_qualified_p (to
, from
))
6650 if (!at_least_as_qualified_p (from
, to
))
6655 ++to_more_cv_qualified
;
6659 constp
&= TYPE_READONLY (to
);
6662 if (TREE_CODE (to
) != POINTER_TYPE
)
6664 same_type_ignoring_top_level_qualifiers_p (to
, from
)
6665 && (constp
>= 0 || to_more_cv_qualified
);
6669 /* When comparing, say, char ** to char const **, this function takes the
6670 'char *' and 'char const *'. Do not pass non-pointer types to this
6674 comp_ptr_ttypes (to
, from
)
6677 return comp_ptr_ttypes_real (to
, from
, 1);
6680 /* Returns 1 if to and from are (possibly multi-level) pointers to the same
6681 type or inheritance-related types, regardless of cv-quals. */
6684 ptr_reasonably_similar (to
, from
)
6687 for (; ; to
= TREE_TYPE (to
), from
= TREE_TYPE (from
))
6689 /* Any target type is similar enough to void. */
6690 if (TREE_CODE (to
) == VOID_TYPE
6691 || TREE_CODE (from
) == VOID_TYPE
)
6694 if (TREE_CODE (to
) != TREE_CODE (from
))
6697 if (TREE_CODE (from
) == OFFSET_TYPE
6698 && comptypes (TYPE_OFFSET_BASETYPE (to
),
6699 TYPE_OFFSET_BASETYPE (from
),
6700 COMPARE_BASE
| COMPARE_RELAXED
))
6703 if (TREE_CODE (to
) == INTEGER_TYPE
6704 && TYPE_PRECISION (to
) == TYPE_PRECISION (from
))
6707 if (TREE_CODE (to
) == FUNCTION_TYPE
)
6710 if (TREE_CODE (to
) != POINTER_TYPE
)
6712 (TYPE_MAIN_VARIANT (to
), TYPE_MAIN_VARIANT (from
),
6713 COMPARE_BASE
| COMPARE_RELAXED
);
6717 /* Like comp_ptr_ttypes, for const_cast. */
6720 comp_ptr_ttypes_const (to
, from
)
6723 for (; ; to
= TREE_TYPE (to
), from
= TREE_TYPE (from
))
6725 if (TREE_CODE (to
) != TREE_CODE (from
))
6728 if (TREE_CODE (from
) == OFFSET_TYPE
6729 && same_type_p (TYPE_OFFSET_BASETYPE (from
),
6730 TYPE_OFFSET_BASETYPE (to
)))
6733 if (TREE_CODE (to
) != POINTER_TYPE
)
6734 return same_type_ignoring_top_level_qualifiers_p (to
, from
);
6738 /* Like comp_ptr_ttypes, for reinterpret_cast. */
6741 comp_ptr_ttypes_reinterpret (to
, from
)
6746 for (; ; to
= TREE_TYPE (to
), from
= TREE_TYPE (from
))
6748 if (TREE_CODE (from
) == OFFSET_TYPE
)
6749 from
= TREE_TYPE (from
);
6750 if (TREE_CODE (to
) == OFFSET_TYPE
)
6751 to
= TREE_TYPE (to
);
6753 /* Const and volatile mean something different for function types,
6754 so the usual checks are not appropriate. */
6755 if (TREE_CODE (from
) != FUNCTION_TYPE
&& TREE_CODE (from
) != METHOD_TYPE
6756 && TREE_CODE (to
) != FUNCTION_TYPE
&& TREE_CODE (to
) != METHOD_TYPE
)
6758 if (!at_least_as_qualified_p (to
, from
))
6762 && !at_least_as_qualified_p (from
, to
))
6764 constp
&= TYPE_READONLY (to
);
6767 if (TREE_CODE (from
) != POINTER_TYPE
6768 || TREE_CODE (to
) != POINTER_TYPE
)
6773 /* Returns the type qualifiers for this type, including the qualifiers on the
6774 elements for an array type. */
6777 cp_type_quals (type
)
6780 type
= strip_array_types (type
);
6781 return TYPE_QUALS (type
);
6784 /* Returns non-zero if the TYPE contains a mutable member */
6787 cp_has_mutable_p (type
)
6790 type
= strip_array_types (type
);
6792 return CLASS_TYPE_P (type
) && CLASSTYPE_HAS_MUTABLE (type
);
6795 /* Subroutine of casts_away_constness. Make T1 and T2 point at
6796 exemplar types such that casting T1 to T2 is casting away castness
6797 if and only if there is no implicit conversion from T1 to T2. */
6800 casts_away_constness_r (t1
, t2
)
6807 /* [expr.const.cast]
6809 For multi-level pointer to members and multi-level mixed pointers
6810 and pointers to members (conv.qual), the "member" aspect of a
6811 pointer to member level is ignored when determining if a const
6812 cv-qualifier has been cast away. */
6813 if (TYPE_PTRMEM_P (*t1
))
6814 *t1
= build_pointer_type (TREE_TYPE (TREE_TYPE (*t1
)));
6815 if (TYPE_PTRMEM_P (*t2
))
6816 *t2
= build_pointer_type (TREE_TYPE (TREE_TYPE (*t2
)));
6818 /* [expr.const.cast]
6820 For two pointer types:
6822 X1 is T1cv1,1 * ... cv1,N * where T1 is not a pointer type
6823 X2 is T2cv2,1 * ... cv2,M * where T2 is not a pointer type
6826 casting from X1 to X2 casts away constness if, for a non-pointer
6827 type T there does not exist an implicit conversion (clause
6830 Tcv1,(N-K+1) * cv1,(N-K+2) * ... cv1,N *
6834 Tcv2,(M-K+1) * cv2,(M-K+2) * ... cv2,M *. */
6836 if (TREE_CODE (*t1
) != POINTER_TYPE
6837 || TREE_CODE (*t2
) != POINTER_TYPE
)
6839 *t1
= cp_build_qualified_type (void_type_node
,
6840 cp_type_quals (*t1
));
6841 *t2
= cp_build_qualified_type (void_type_node
,
6842 cp_type_quals (*t2
));
6846 quals1
= cp_type_quals (*t1
);
6847 quals2
= cp_type_quals (*t2
);
6848 *t1
= TREE_TYPE (*t1
);
6849 *t2
= TREE_TYPE (*t2
);
6850 casts_away_constness_r (t1
, t2
);
6851 *t1
= build_pointer_type (*t1
);
6852 *t2
= build_pointer_type (*t2
);
6853 *t1
= cp_build_qualified_type (*t1
, quals1
);
6854 *t2
= cp_build_qualified_type (*t2
, quals2
);
6857 /* Returns non-zero if casting from TYPE1 to TYPE2 casts away
6861 casts_away_constness (t1
, t2
)
6865 if (TREE_CODE (t2
) == REFERENCE_TYPE
)
6867 /* [expr.const.cast]
6869 Casting from an lvalue of type T1 to an lvalue of type T2
6870 using a reference cast casts away constness if a cast from an
6871 rvalue of type "pointer to T1" to the type "pointer to T2"
6872 casts away constness. */
6873 t1
= (TREE_CODE (t1
) == REFERENCE_TYPE
6874 ? TREE_TYPE (t1
) : t1
);
6875 return casts_away_constness (build_pointer_type (t1
),
6876 build_pointer_type (TREE_TYPE (t2
)));
6879 if (TYPE_PTRMEM_P (t1
) && TYPE_PTRMEM_P (t2
))
6880 /* [expr.const.cast]
6882 Casting from an rvalue of type "pointer to data member of X
6883 of type T1" to the type "pointer to data member of Y of type
6884 T2" casts away constness if a cast from an rvalue of type
6885 "pointer to T1" to the type "pointer to T2" casts away
6887 return casts_away_constness
6888 (build_pointer_type (TREE_TYPE (TREE_TYPE (t1
))),
6889 build_pointer_type (TREE_TYPE (TREE_TYPE (t2
))));
6891 /* Casting away constness is only something that makes sense for
6892 pointer or reference types. */
6893 if (TREE_CODE (t1
) != POINTER_TYPE
6894 || TREE_CODE (t2
) != POINTER_TYPE
)
6897 /* Top-level qualifiers don't matter. */
6898 t1
= TYPE_MAIN_VARIANT (t1
);
6899 t2
= TYPE_MAIN_VARIANT (t2
);
6900 casts_away_constness_r (&t1
, &t2
);
6901 if (!can_convert (t2
, t1
))
6907 /* Returns TYPE with its cv qualifiers removed
6908 TYPE is T cv* .. *cv where T is not a pointer type,
6909 returns T * .. *. (If T is an array type, then the cv qualifiers
6910 above are those of the array members.) */
6913 strip_all_pointer_quals (type
)
6916 if (TREE_CODE (type
) == POINTER_TYPE
)
6917 return build_pointer_type (strip_all_pointer_quals (TREE_TYPE (type
)));
6918 else if (TREE_CODE (type
) == OFFSET_TYPE
)
6919 return build_offset_type (TYPE_OFFSET_BASETYPE (type
),
6920 strip_all_pointer_quals (TREE_TYPE (type
)));
6922 return TYPE_MAIN_VARIANT (type
);