1 /* Build expressions with type checking for C++ compiler.
2 Copyright (C) 1987, 1988, 1989, 1992, 1993, 1994, 1995, 1996, 1997, 1998,
3 1999, 2000, 2001 Free Software Foundation, Inc.
4 Hacked by Michael Tiemann (tiemann@cygnus.com)
6 This file is part of GNU CC.
8 GNU CC is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2, or (at your option)
13 GNU CC is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with GNU CC; see the file COPYING. If not, write to
20 the Free Software Foundation, 59 Temple Place - Suite 330,
21 Boston, MA 02111-1307, USA. */
24 /* This file is part of the C++ front end.
25 It contains routines to build C++ expressions given their operands,
26 including computing the types of the result, C and C++ specific error
27 checks, and some optimization.
29 There are also routines to build RETURN_STMT nodes and CASE_STMT nodes,
30 and to process initializations in declarations (since they work
31 like a strange sort of assignment). */
43 #include "diagnostic.h"
46 static tree convert_for_assignment
PARAMS ((tree
, tree
, const char *, tree
,
48 static tree pointer_int_sum
PARAMS ((enum tree_code
, tree
, tree
));
49 static tree rationalize_conditional_expr
PARAMS ((enum tree_code
, tree
));
50 static int comp_target_parms
PARAMS ((tree
, tree
));
51 static int comp_ptr_ttypes_real
PARAMS ((tree
, tree
, int));
52 static int comp_ptr_ttypes_const
PARAMS ((tree
, tree
));
53 static int comp_ptr_ttypes_reinterpret
PARAMS ((tree
, tree
));
54 static int comp_except_types
PARAMS ((tree
, tree
, int));
55 static int comp_array_types
PARAMS ((int (*) (tree
, tree
, int), tree
,
57 static tree common_base_type
PARAMS ((tree
, tree
));
58 static tree lookup_anon_field
PARAMS ((tree
, tree
));
59 static tree pointer_diff
PARAMS ((tree
, tree
, tree
));
60 static tree build_component_addr
PARAMS ((tree
, tree
));
61 static tree qualify_type_recursive
PARAMS ((tree
, tree
));
62 static tree get_delta_difference
PARAMS ((tree
, tree
, int));
63 static int comp_cv_target_types
PARAMS ((tree
, tree
, int));
64 static void casts_away_constness_r
PARAMS ((tree
*, tree
*));
65 static int casts_away_constness
PARAMS ((tree
, tree
));
66 static void maybe_warn_about_returning_address_of_local
PARAMS ((tree
));
67 static tree strip_all_pointer_quals
PARAMS ((tree
));
69 /* Return the target type of TYPE, which means return T for:
70 T*, T&, T[], T (...), and otherwise, just T. */
76 if (TREE_CODE (type
) == REFERENCE_TYPE
)
77 type
= TREE_TYPE (type
);
78 while (TREE_CODE (type
) == POINTER_TYPE
79 || TREE_CODE (type
) == ARRAY_TYPE
80 || TREE_CODE (type
) == FUNCTION_TYPE
81 || TREE_CODE (type
) == METHOD_TYPE
82 || TREE_CODE (type
) == OFFSET_TYPE
)
83 type
= TREE_TYPE (type
);
87 /* Do `exp = require_complete_type (exp);' to make sure exp
88 does not have an incomplete type. (That includes void types.)
89 Returns the error_mark_node if the VALUE does not have
90 complete type when this function returns. */
93 require_complete_type (value
)
98 if (processing_template_decl
|| value
== error_mark_node
)
101 if (TREE_CODE (value
) == OVERLOAD
)
102 type
= unknown_type_node
;
104 type
= TREE_TYPE (value
);
106 /* First, detect a valid value with a complete type. */
107 if (COMPLETE_TYPE_P (type
))
110 /* If we see X::Y, we build an OFFSET_TYPE which has
111 not been laid out. Try to avoid an error by interpreting
112 it as this->X::Y, if reasonable. */
113 if (TREE_CODE (value
) == OFFSET_REF
114 && current_class_ref
!= 0
115 && TREE_OPERAND (value
, 0) == current_class_ref
)
117 tree base
, member
= TREE_OPERAND (value
, 1);
118 tree basetype
= TYPE_OFFSET_BASETYPE (type
);
120 my_friendly_assert (TREE_CODE (member
) == FIELD_DECL
, 305);
121 basetype
= lookup_base (current_class_type
, basetype
, ba_check
, NULL
);
122 base
= build_base_path (PLUS_EXPR
, current_class_ptr
, basetype
, 1);
124 value
= build (COMPONENT_REF
, TREE_TYPE (member
),
125 build_indirect_ref (base
, NULL
), member
);
126 return require_complete_type (value
);
129 if (complete_type_or_else (type
, value
))
132 return error_mark_node
;
135 /* Try to complete TYPE, if it is incomplete. For example, if TYPE is
136 a template instantiation, do the instantiation. Returns TYPE,
137 whether or not it could be completed, unless something goes
138 horribly wrong, in which case the error_mark_node is returned. */
144 if (type
== NULL_TREE
)
145 /* Rather than crash, we return something sure to cause an error
147 return error_mark_node
;
149 if (type
== error_mark_node
|| COMPLETE_TYPE_P (type
))
151 else if (TREE_CODE (type
) == ARRAY_TYPE
&& TYPE_DOMAIN (type
))
153 tree t
= complete_type (TREE_TYPE (type
));
154 if (COMPLETE_TYPE_P (t
) && ! processing_template_decl
)
156 TYPE_NEEDS_CONSTRUCTING (type
)
157 = TYPE_NEEDS_CONSTRUCTING (TYPE_MAIN_VARIANT (t
));
158 TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type
)
159 = TYPE_HAS_NONTRIVIAL_DESTRUCTOR (TYPE_MAIN_VARIANT (t
));
161 else if (CLASS_TYPE_P (type
) && CLASSTYPE_TEMPLATE_INSTANTIATION (type
))
162 instantiate_class_template (TYPE_MAIN_VARIANT (type
));
167 /* Like complete_type, but issue an error if the TYPE cannot be
168 completed. VALUE is used for informative diagnostics.
169 Returns NULL_TREE if the type cannot be made complete. */
172 complete_type_or_else (type
, value
)
176 type
= complete_type (type
);
177 if (type
== error_mark_node
)
178 /* We already issued an error. */
180 else if (!COMPLETE_TYPE_P (type
))
182 incomplete_type_error (value
, type
);
189 /* Return truthvalue of whether type of EXP is instantiated. */
195 return (TREE_CODE (exp
) == OVERLOAD
196 || TREE_CODE (exp
) == TREE_LIST
197 || TREE_TYPE (exp
) == unknown_type_node
198 || (TREE_CODE (TREE_TYPE (exp
)) == OFFSET_TYPE
199 && TREE_TYPE (TREE_TYPE (exp
)) == unknown_type_node
));
202 /* Return a pointer or pointer to member type similar to T1, with a
203 cv-qualification signature that is the union of the cv-qualification
204 signatures of T1 and T2: [expr.rel], [expr.eq]. */
207 qualify_type_recursive (t1
, t2
)
210 if ((TYPE_PTR_P (t1
) && TYPE_PTR_P (t2
))
211 || (TYPE_PTRMEM_P (t1
) && TYPE_PTRMEM_P (t2
)))
213 tree tt1
= TREE_TYPE (t1
);
214 tree tt2
= TREE_TYPE (t2
);
218 tree attributes
= (*targetm
.merge_type_attributes
) (t1
, t2
);
220 if (TREE_CODE (tt1
) == OFFSET_TYPE
)
222 b1
= TYPE_OFFSET_BASETYPE (tt1
);
223 tt1
= TREE_TYPE (tt1
);
224 tt2
= TREE_TYPE (tt2
);
229 type_quals
= (cp_type_quals (tt1
) | cp_type_quals (tt2
));
230 tgt
= qualify_type_recursive (tt1
, tt2
);
231 tgt
= cp_build_qualified_type (tgt
, type_quals
);
233 tgt
= build_offset_type (b1
, tgt
);
234 t1
= build_pointer_type (tgt
);
235 t1
= build_type_attribute_variant (t1
, attributes
);
240 /* Return the common type of two parameter lists.
241 We assume that comptypes has already been done and returned 1;
242 if that isn't so, this may crash.
244 As an optimization, free the space we allocate if the parameter
245 lists are already common. */
251 tree oldargs
= p1
, newargs
, n
;
255 len
= list_length (p1
);
256 newargs
= tree_last (p1
);
258 if (newargs
== void_list_node
)
267 newargs
= tree_cons (NULL_TREE
, NULL_TREE
, newargs
);
272 p1
= TREE_CHAIN (p1
), p2
= TREE_CHAIN (p2
), n
= TREE_CHAIN (n
), i
++)
274 if (TREE_PURPOSE (p1
) && !TREE_PURPOSE (p2
))
276 TREE_PURPOSE (n
) = TREE_PURPOSE (p1
);
279 else if (! TREE_PURPOSE (p1
))
281 if (TREE_PURPOSE (p2
))
283 TREE_PURPOSE (n
) = TREE_PURPOSE (p2
);
289 if (1 != simple_cst_equal (TREE_PURPOSE (p1
), TREE_PURPOSE (p2
)))
291 TREE_PURPOSE (n
) = TREE_PURPOSE (p2
);
293 if (TREE_VALUE (p1
) != TREE_VALUE (p2
))
296 TREE_VALUE (n
) = common_type (TREE_VALUE (p1
), TREE_VALUE (p2
));
299 TREE_VALUE (n
) = TREE_VALUE (p1
);
307 /* Given a type, perhaps copied for a typedef,
308 find the "original" version of it. */
313 while (TYPE_NAME (t
) != NULL_TREE
)
315 tree x
= TYPE_NAME (t
);
316 if (TREE_CODE (x
) != TYPE_DECL
)
318 x
= DECL_ORIGINAL_TYPE (x
);
326 /* T1 and T2 are arithmetic or enumeration types. Return the type
327 that will result from the "usual arithmetic conversions" on T1 and
328 T2 as described in [expr]. */
331 type_after_usual_arithmetic_conversions (t1
, t2
)
335 enum tree_code code1
= TREE_CODE (t1
);
336 enum tree_code code2
= TREE_CODE (t2
);
339 /* FIXME: Attributes. */
340 my_friendly_assert (ARITHMETIC_TYPE_P (t1
)
341 || TREE_CODE (t1
) == ENUMERAL_TYPE
,
343 my_friendly_assert (ARITHMETIC_TYPE_P (t2
)
344 || TREE_CODE (t2
) == ENUMERAL_TYPE
,
347 /* In what follows, we slightly generalize the rules given in [expr]
348 so as to deal with `long long'. First, merge the attributes. */
349 attributes
= (*targetm
.merge_type_attributes
) (t1
, t2
);
351 /* If only one is real, use it as the result. */
352 if (code1
== REAL_TYPE
&& code2
!= REAL_TYPE
)
353 return build_type_attribute_variant (t1
, attributes
);
354 if (code2
== REAL_TYPE
&& code1
!= REAL_TYPE
)
355 return build_type_attribute_variant (t2
, attributes
);
357 /* Perform the integral promotions. */
358 if (code1
!= REAL_TYPE
)
360 t1
= type_promotes_to (t1
);
361 t2
= type_promotes_to (t2
);
364 /* Both real or both integers; use the one with greater precision. */
365 if (TYPE_PRECISION (t1
) > TYPE_PRECISION (t2
))
366 return build_type_attribute_variant (t1
, attributes
);
367 else if (TYPE_PRECISION (t2
) > TYPE_PRECISION (t1
))
368 return build_type_attribute_variant (t2
, attributes
);
370 if (code1
!= REAL_TYPE
)
372 /* If one is a sizetype, use it so size_binop doesn't blow up. */
373 if (TYPE_IS_SIZETYPE (t1
) > TYPE_IS_SIZETYPE (t2
))
374 return build_type_attribute_variant (t1
, attributes
);
375 if (TYPE_IS_SIZETYPE (t2
) > TYPE_IS_SIZETYPE (t1
))
376 return build_type_attribute_variant (t2
, attributes
);
378 /* If one is unsigned long long, then convert the other to unsigned
380 if (same_type_p (TYPE_MAIN_VARIANT (t1
), long_long_unsigned_type_node
)
381 || same_type_p (TYPE_MAIN_VARIANT (t2
), long_long_unsigned_type_node
))
382 return build_type_attribute_variant (long_long_unsigned_type_node
,
384 /* If one is a long long, and the other is an unsigned long, and
385 long long can represent all the values of an unsigned long, then
386 convert to a long long. Otherwise, convert to an unsigned long
387 long. Otherwise, if either operand is long long, convert the
390 Since we're here, we know the TYPE_PRECISION is the same;
391 therefore converting to long long cannot represent all the values
392 of an unsigned long, so we choose unsigned long long in that
394 if (same_type_p (TYPE_MAIN_VARIANT (t1
), long_long_integer_type_node
)
395 || same_type_p (TYPE_MAIN_VARIANT (t2
), long_long_integer_type_node
))
397 tree t
= ((TREE_UNSIGNED (t1
) || TREE_UNSIGNED (t2
))
398 ? long_long_unsigned_type_node
399 : long_long_integer_type_node
);
400 return build_type_attribute_variant (t
, attributes
);
403 /* Go through the same procedure, but for longs. */
404 if (same_type_p (TYPE_MAIN_VARIANT (t1
), long_unsigned_type_node
)
405 || same_type_p (TYPE_MAIN_VARIANT (t2
), long_unsigned_type_node
))
406 return build_type_attribute_variant (long_unsigned_type_node
,
408 if (same_type_p (TYPE_MAIN_VARIANT (t1
), long_integer_type_node
)
409 || same_type_p (TYPE_MAIN_VARIANT (t2
), long_integer_type_node
))
411 tree t
= ((TREE_UNSIGNED (t1
) || TREE_UNSIGNED (t2
))
412 ? long_unsigned_type_node
: long_integer_type_node
);
413 return build_type_attribute_variant (t
, attributes
);
415 /* Otherwise prefer the unsigned one. */
416 if (TREE_UNSIGNED (t1
))
417 return build_type_attribute_variant (t1
, attributes
);
419 return build_type_attribute_variant (t2
, attributes
);
423 if (same_type_p (TYPE_MAIN_VARIANT (t1
), long_double_type_node
)
424 || same_type_p (TYPE_MAIN_VARIANT (t2
), long_double_type_node
))
425 return build_type_attribute_variant (long_double_type_node
,
427 if (same_type_p (TYPE_MAIN_VARIANT (t1
), double_type_node
)
428 || same_type_p (TYPE_MAIN_VARIANT (t2
), double_type_node
))
429 return build_type_attribute_variant (double_type_node
,
432 return build_type_attribute_variant (float_type_node
,
437 /* Return the composite pointer type (see [expr.rel]) for T1 and T2.
438 ARG1 and ARG2 are the values with those types. The LOCATION is a
439 string describing the current location, in case an error occurs. */
442 composite_pointer_type (t1
, t2
, arg1
, arg2
, location
)
447 const char* location
;
453 If one operand is a null pointer constant, the composite pointer
454 type is the type of the other operand. */
455 if (null_ptr_cst_p (arg1
))
457 if (null_ptr_cst_p (arg2
))
460 /* Deal with pointer-to-member functions in the same way as we deal
461 with pointers to functions. */
462 if (TYPE_PTRMEMFUNC_P (t1
))
463 t1
= TYPE_PTRMEMFUNC_FN_TYPE (t1
);
464 if (TYPE_PTRMEMFUNC_P (t2
))
465 t2
= TYPE_PTRMEMFUNC_FN_TYPE (t2
);
471 If one of the operands has type "pointer to cv1 void*", then
472 the other has type "pointer to cv2T", and the composite pointer
473 type is "pointer to cv12 void", where cv12 is the union of cv1
476 If either type is a pointer to void, make sure it is T1. */
477 if (VOID_TYPE_P (TREE_TYPE (t2
)))
484 /* Now, if T1 is a pointer to void, merge the qualifiers. */
485 if (VOID_TYPE_P (TREE_TYPE (t1
)))
487 if (pedantic
&& TYPE_PTRFN_P (t2
))
488 pedwarn ("ISO C++ forbids %s between pointer of type `void *' and pointer-to-function", location
);
491 result_type
= cp_build_qualified_type (void_type_node
,
493 | cp_type_quals (t2
)));
494 result_type
= build_pointer_type (result_type
);
498 tree full1
= qualify_type_recursive (t1
, t2
);
499 tree full2
= qualify_type_recursive (t2
, t1
);
501 int val
= comp_target_types (full1
, full2
, 1);
509 pedwarn ("%s between distinct pointer types `%T' and `%T' lacks a cast",
511 result_type
= ptr_type_node
;
518 /* Return the common type of two types.
519 We assume that comptypes has already been done and returned 1;
520 if that isn't so, this may crash.
522 This is the type for the result of most arithmetic operations
523 if the operands have the given two types.
525 We do not deal with enumeral types here because they have already been
526 converted to integer types. */
532 register enum tree_code code1
;
533 register enum tree_code code2
;
536 /* Save time if the two types are the same. */
539 t1
= original_type (t1
);
540 t2
= original_type (t2
);
544 /* If one type is nonsense, use the other. */
545 if (t1
== error_mark_node
)
547 if (t2
== error_mark_node
)
550 if ((ARITHMETIC_TYPE_P (t1
) || TREE_CODE (t1
) == ENUMERAL_TYPE
)
551 && (ARITHMETIC_TYPE_P (t2
) || TREE_CODE (t2
) == ENUMERAL_TYPE
))
552 return type_after_usual_arithmetic_conversions (t1
, t2
);
554 /* Merge the attributes. */
555 attributes
= (*targetm
.merge_type_attributes
) (t1
, t2
);
557 /* Treat an enum type as the unsigned integer type of the same width. */
559 if (TREE_CODE (t1
) == ENUMERAL_TYPE
)
560 t1
= type_for_size (TYPE_PRECISION (t1
), 1);
561 if (TREE_CODE (t2
) == ENUMERAL_TYPE
)
562 t2
= type_for_size (TYPE_PRECISION (t2
), 1);
564 if (TYPE_PTRMEMFUNC_P (t1
))
565 t1
= TYPE_PTRMEMFUNC_FN_TYPE (t1
);
566 if (TYPE_PTRMEMFUNC_P (t2
))
567 t2
= TYPE_PTRMEMFUNC_FN_TYPE (t2
);
569 code1
= TREE_CODE (t1
);
570 code2
= TREE_CODE (t2
);
572 /* If one type is complex, form the common type of the non-complex
573 components, then make that complex. Use T1 or T2 if it is the
575 if (code1
== COMPLEX_TYPE
|| code2
== COMPLEX_TYPE
)
577 tree subtype1
= code1
== COMPLEX_TYPE
? TREE_TYPE (t1
) : t1
;
578 tree subtype2
= code2
== COMPLEX_TYPE
? TREE_TYPE (t2
) : t2
;
579 tree subtype
= common_type (subtype1
, subtype2
);
581 if (code1
== COMPLEX_TYPE
&& TREE_TYPE (t1
) == subtype
)
582 return build_type_attribute_variant (t1
, attributes
);
583 else if (code2
== COMPLEX_TYPE
&& TREE_TYPE (t2
) == subtype
)
584 return build_type_attribute_variant (t2
, attributes
);
586 return build_type_attribute_variant (build_complex_type (subtype
),
594 /* We should have called type_after_usual_arithmetic_conversions
596 my_friendly_abort (19990725);
601 /* For two pointers, do this recursively on the target type,
602 and combine the qualifiers of the two types' targets. */
603 /* This code was turned off; I don't know why.
604 But ISO C++ specifies doing this with the qualifiers.
605 So I turned it on again. */
607 tree tt1
= TREE_TYPE (t1
);
608 tree tt2
= TREE_TYPE (t2
);
613 if (TREE_CODE (tt1
) == OFFSET_TYPE
)
615 b1
= TYPE_OFFSET_BASETYPE (tt1
);
616 b2
= TYPE_OFFSET_BASETYPE (tt2
);
617 tt1
= TREE_TYPE (tt1
);
618 tt2
= TREE_TYPE (tt2
);
623 type_quals
= (cp_type_quals (tt1
) | cp_type_quals (tt2
));
624 tt1
= TYPE_MAIN_VARIANT (tt1
);
625 tt2
= TYPE_MAIN_VARIANT (tt2
);
629 else if (VOID_TYPE_P (tt1
) || VOID_TYPE_P (tt2
))
630 target
= void_type_node
;
631 else if (tt1
== unknown_type_node
)
633 else if (tt2
== unknown_type_node
)
636 target
= common_type (tt1
, tt2
);
638 target
= cp_build_qualified_type (target
, type_quals
);
642 if (same_type_p (b1
, b2
)
643 || (DERIVED_FROM_P (b1
, b2
) && binfo_or_else (b1
, b2
)))
644 target
= build_offset_type (b2
, target
);
645 else if (binfo_or_else (b2
, b1
))
646 target
= build_offset_type (b1
, target
);
649 if (code1
== POINTER_TYPE
)
650 t1
= build_pointer_type (target
);
652 t1
= build_reference_type (target
);
653 t1
= build_type_attribute_variant (t1
, attributes
);
655 if (TREE_CODE (target
) == METHOD_TYPE
)
656 t1
= build_ptrmemfunc_type (t1
);
663 tree elt
= common_type (TREE_TYPE (t1
), TREE_TYPE (t2
));
664 /* Save space: see if the result is identical to one of the args. */
665 if (elt
== TREE_TYPE (t1
) && TYPE_DOMAIN (t1
))
666 return build_type_attribute_variant (t1
, attributes
);
667 if (elt
== TREE_TYPE (t2
) && TYPE_DOMAIN (t2
))
668 return build_type_attribute_variant (t2
, attributes
);
669 /* Merge the element types, and have a size if either arg has one. */
670 t1
= build_cplus_array_type
671 (elt
, TYPE_DOMAIN (TYPE_DOMAIN (t1
) ? t1
: t2
));
672 return build_type_attribute_variant (t1
, attributes
);
676 /* Function types: prefer the one that specified arg types.
677 If both do, merge the arg types. Also merge the return types. */
679 tree valtype
= common_type (TREE_TYPE (t1
), TREE_TYPE (t2
));
680 tree p1
= TYPE_ARG_TYPES (t1
);
681 tree p2
= TYPE_ARG_TYPES (t2
);
684 /* Save space: see if the result is identical to one of the args. */
685 if (valtype
== TREE_TYPE (t1
) && ! p2
)
686 return build_type_attribute_variant (t1
, attributes
);
687 if (valtype
== TREE_TYPE (t2
) && ! p1
)
688 return build_type_attribute_variant (t2
, attributes
);
690 /* Simple way if one arg fails to specify argument types. */
691 if (p1
== NULL_TREE
|| TREE_VALUE (p1
) == void_type_node
)
693 rval
= build_function_type (valtype
, p2
);
694 if ((raises
= TYPE_RAISES_EXCEPTIONS (t2
)))
695 rval
= build_exception_variant (rval
, raises
);
696 return build_type_attribute_variant (rval
, attributes
);
698 raises
= TYPE_RAISES_EXCEPTIONS (t1
);
699 if (p2
== NULL_TREE
|| TREE_VALUE (p2
) == void_type_node
)
701 rval
= build_function_type (valtype
, p1
);
703 rval
= build_exception_variant (rval
, raises
);
704 return build_type_attribute_variant (rval
, attributes
);
707 rval
= build_function_type (valtype
, commonparms (p1
, p2
));
708 rval
= build_exception_variant (rval
, raises
);
709 return build_type_attribute_variant (rval
, attributes
);
714 t1
= TYPE_MAIN_VARIANT (t1
);
715 t2
= TYPE_MAIN_VARIANT (t2
);
717 if (DERIVED_FROM_P (t1
, t2
) && binfo_or_else (t1
, t2
))
718 return build_type_attribute_variant (t1
, attributes
);
719 else if (binfo_or_else (t2
, t1
))
720 return build_type_attribute_variant (t2
, attributes
);
723 compiler_error ("common_type called with uncommon aggregate types");
724 return error_mark_node
;
728 if (TREE_CODE (TREE_TYPE (t1
)) == TREE_CODE (TREE_TYPE (t2
)))
730 /* Get this value the long way, since TYPE_METHOD_BASETYPE
731 is just the main variant of this. */
735 tree b1
= TYPE_OFFSET_BASETYPE (t1
);
736 tree b2
= TYPE_OFFSET_BASETYPE (t2
);
738 if (same_type_p (b1
, b2
)
739 || (DERIVED_FROM_P (b1
, b2
) && binfo_or_else (b1
, b2
)))
740 basetype
= TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (t2
)));
743 if (binfo_or_else (b2
, b1
) == NULL_TREE
)
744 compiler_error ("common_type called with uncommon method types");
745 basetype
= TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (t1
)));
748 raises
= TYPE_RAISES_EXCEPTIONS (t1
);
750 /* If this was a member function type, get back to the
751 original type of type member function (i.e., without
752 the class instance variable up front. */
753 t1
= build_function_type (TREE_TYPE (t1
),
754 TREE_CHAIN (TYPE_ARG_TYPES (t1
)));
755 t2
= build_function_type (TREE_TYPE (t2
),
756 TREE_CHAIN (TYPE_ARG_TYPES (t2
)));
757 t3
= common_type (t1
, t2
);
758 t3
= build_cplus_method_type (basetype
, TREE_TYPE (t3
),
759 TYPE_ARG_TYPES (t3
));
760 t1
= build_exception_variant (t3
, raises
);
763 compiler_error ("common_type called with uncommon method types");
765 return build_type_attribute_variant (t1
, attributes
);
768 /* Pointers to members should now be handled by the POINTER_TYPE
770 my_friendly_abort (990325);
773 return build_type_attribute_variant (t1
, attributes
);
777 /* Compare two exception specifier types for exactness or subsetness, if
778 allowed. Returns 0 for mismatch, 1 for same, 2 if B is allowed by A.
780 [except.spec] "If a class X ... objects of class X or any class publicly
781 and unambigously derrived from X. Similarly, if a pointer type Y * ...
782 exceptions of type Y * or that are pointers to any type publicly and
783 unambigously derrived from Y. Otherwise a function only allows exceptions
784 that have the same type ..."
785 This does not mention cv qualifiers and is different to what throw
786 [except.throw] and catch [except.catch] will do. They will ignore the
787 top level cv qualifiers, and allow qualifiers in the pointer to class
790 We implement the letter of the standard. */
793 comp_except_types (a
, b
, exact
)
797 if (same_type_p (a
, b
))
801 if (cp_type_quals (a
) || cp_type_quals (b
))
804 if (TREE_CODE (a
) == POINTER_TYPE
805 && TREE_CODE (b
) == POINTER_TYPE
)
809 if (cp_type_quals (a
) || cp_type_quals (b
))
813 if (TREE_CODE (a
) != RECORD_TYPE
814 || TREE_CODE (b
) != RECORD_TYPE
)
817 if (ACCESSIBLY_UNIQUELY_DERIVED_P (a
, b
))
823 /* Return 1 if TYPE1 and TYPE2 are equivalent exception specifiers.
824 If EXACT is 0, T2 can be a subset of T1 (according to 15.4/7),
825 otherwise it must be exact. Exception lists are unordered, but
826 we've already filtered out duplicates. Most lists will be in order,
827 we should try to make use of that. */
830 comp_except_specs (t1
, t2
, exact
)
841 if (t1
== NULL_TREE
) /* T1 is ... */
842 return t2
== NULL_TREE
|| !exact
;
843 if (!TREE_VALUE (t1
)) /* t1 is EMPTY */
844 return t2
!= NULL_TREE
&& !TREE_VALUE (t2
);
845 if (t2
== NULL_TREE
) /* T2 is ... */
847 if (TREE_VALUE(t1
) && !TREE_VALUE (t2
)) /* T2 is EMPTY, T1 is not */
850 /* Neither set is ... or EMPTY, make sure each part of T2 is in T1.
851 Count how many we find, to determine exactness. For exact matching and
852 ordered T1, T2, this is an O(n) operation, otherwise its worst case is
854 for (base
= t1
; t2
!= NULL_TREE
; t2
= TREE_CHAIN (t2
))
856 for (probe
= base
; probe
!= NULL_TREE
; probe
= TREE_CHAIN (probe
))
858 tree a
= TREE_VALUE (probe
);
859 tree b
= TREE_VALUE (t2
);
861 if (comp_except_types (a
, b
, exact
))
863 if (probe
== base
&& exact
)
864 base
= TREE_CHAIN (probe
);
869 if (probe
== NULL_TREE
)
872 return !exact
|| base
== NULL_TREE
|| length
== list_length (t1
);
875 /* Compare the array types T1 and T2, using CMP as the type comparison
876 function for the element types. STRICT is as for comptypes. */
879 comp_array_types (cmp
, t1
, t2
, strict
)
880 register int (*cmp
) PARAMS ((tree
, tree
, int));
890 /* The type of the array elements must be the same. */
891 if (!(TREE_TYPE (t1
) == TREE_TYPE (t2
)
892 || (*cmp
) (TREE_TYPE (t1
), TREE_TYPE (t2
),
893 strict
& ~COMPARE_REDECLARATION
)))
896 d1
= TYPE_DOMAIN (t1
);
897 d2
= TYPE_DOMAIN (t2
);
902 /* If one of the arrays is dimensionless, and the other has a
903 dimension, they are of different types. However, it is legal to
911 declarations for an array object can specify
912 array types that differ by the presence or absence of a major
913 array bound (_dcl.array_). */
915 return strict
& COMPARE_REDECLARATION
;
917 /* Check that the dimensions are the same. */
918 return (cp_tree_equal (TYPE_MIN_VALUE (d1
),
920 && cp_tree_equal (TYPE_MAX_VALUE (d1
),
921 TYPE_MAX_VALUE (d2
)));
924 /* Return 1 if T1 and T2 are compatible types for assignment or
925 various other operations. STRICT is a bitwise-or of the COMPARE_*
929 comptypes (t1
, t2
, strict
)
935 int orig_strict
= strict
;
937 /* The special exemption for redeclaring array types without an
938 array bound only applies at the top level:
943 is not legal, for example. */
944 strict
&= ~COMPARE_REDECLARATION
;
946 /* Suppress errors caused by previously reported errors */
950 /* This should never happen. */
951 my_friendly_assert (t1
!= error_mark_node
, 307);
953 if (t2
== error_mark_node
)
956 /* If either type is the internal version of sizetype, return the
958 if (TREE_CODE (t1
) == INTEGER_TYPE
&& TYPE_IS_SIZETYPE (t1
)
959 && TYPE_DOMAIN (t1
) != 0)
960 t1
= TYPE_DOMAIN (t1
);
962 if (TREE_CODE (t2
) == INTEGER_TYPE
&& TYPE_IS_SIZETYPE (t2
)
963 && TYPE_DOMAIN (t2
) != 0)
964 t2
= TYPE_DOMAIN (t2
);
966 if (strict
& COMPARE_RELAXED
)
968 /* Treat an enum type as the unsigned integer type of the same width. */
970 if (TREE_CODE (t1
) == ENUMERAL_TYPE
)
971 t1
= type_for_size (TYPE_PRECISION (t1
), 1);
972 if (TREE_CODE (t2
) == ENUMERAL_TYPE
)
973 t2
= type_for_size (TYPE_PRECISION (t2
), 1);
979 if (TYPE_PTRMEMFUNC_P (t1
))
980 t1
= TYPE_PTRMEMFUNC_FN_TYPE (t1
);
981 if (TYPE_PTRMEMFUNC_P (t2
))
982 t2
= TYPE_PTRMEMFUNC_FN_TYPE (t2
);
984 /* Different classes of types can't be compatible. */
985 if (TREE_CODE (t1
) != TREE_CODE (t2
))
988 /* Qualifiers must match. */
989 if (cp_type_quals (t1
) != cp_type_quals (t2
))
991 if (strict
== COMPARE_STRICT
992 && TYPE_FOR_JAVA (t1
) != TYPE_FOR_JAVA (t2
))
995 /* Allow for two different type nodes which have essentially the same
996 definition. Note that we already checked for equality of the type
997 qualifiers (just above). */
999 if (TYPE_MAIN_VARIANT (t1
) == TYPE_MAIN_VARIANT (t2
))
1002 if (strict
& COMPARE_NO_ATTRIBUTES
)
1004 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
1005 else if (! (attrval
= (*targetm
.comp_type_attributes
) (t1
, t2
)))
1008 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
1011 switch (TREE_CODE (t1
))
1013 case TEMPLATE_TEMPLATE_PARM
:
1014 case BOUND_TEMPLATE_TEMPLATE_PARM
:
1015 if (TEMPLATE_TYPE_IDX (t1
) != TEMPLATE_TYPE_IDX (t2
)
1016 || TEMPLATE_TYPE_LEVEL (t1
) != TEMPLATE_TYPE_LEVEL (t2
))
1018 if (! comp_template_parms
1019 (DECL_TEMPLATE_PARMS (TEMPLATE_TEMPLATE_PARM_TEMPLATE_DECL (t1
)),
1020 DECL_TEMPLATE_PARMS (TEMPLATE_TEMPLATE_PARM_TEMPLATE_DECL (t2
))))
1022 if (TREE_CODE (t1
) == TEMPLATE_TEMPLATE_PARM
)
1024 /* Don't check inheritance. */
1025 strict
= COMPARE_STRICT
;
1030 if (TYPE_TEMPLATE_INFO (t1
) && TYPE_TEMPLATE_INFO (t2
)
1031 && (TYPE_TI_TEMPLATE (t1
) == TYPE_TI_TEMPLATE (t2
)
1032 || TREE_CODE (t1
) == BOUND_TEMPLATE_TEMPLATE_PARM
))
1033 val
= comp_template_args (TYPE_TI_ARGS (t1
),
1036 if ((strict
& COMPARE_BASE
) && DERIVED_FROM_P (t1
, t2
))
1038 else if ((strict
& COMPARE_RELAXED
) && DERIVED_FROM_P (t2
, t1
))
1043 val
= (comptypes (build_pointer_type (TYPE_OFFSET_BASETYPE (t1
)),
1044 build_pointer_type (TYPE_OFFSET_BASETYPE (t2
)), strict
)
1045 && comptypes (TREE_TYPE (t1
), TREE_TYPE (t2
), strict
));
1049 if (! comp_except_specs (TYPE_RAISES_EXCEPTIONS (t1
),
1050 TYPE_RAISES_EXCEPTIONS (t2
), 1))
1053 /* This case is anti-symmetrical!
1054 One can pass a base member (or member function)
1055 to something expecting a derived member (or member function),
1056 but not vice-versa! */
1058 val
= (comptypes (TREE_TYPE (t1
), TREE_TYPE (t2
), strict
)
1059 && compparms (TYPE_ARG_TYPES (t1
), TYPE_ARG_TYPES (t2
)));
1063 case REFERENCE_TYPE
:
1064 t1
= TREE_TYPE (t1
);
1065 t2
= TREE_TYPE (t2
);
1066 /* first, check whether the referred types match with the
1067 required level of strictness */
1068 val
= comptypes (t1
, t2
, strict
);
1071 if (TREE_CODE (t1
) == RECORD_TYPE
1072 && TREE_CODE (t2
) == RECORD_TYPE
)
1077 if (! comp_except_specs (TYPE_RAISES_EXCEPTIONS (t1
),
1078 TYPE_RAISES_EXCEPTIONS (t2
), 1))
1081 val
= ((TREE_TYPE (t1
) == TREE_TYPE (t2
)
1082 || comptypes (TREE_TYPE (t1
), TREE_TYPE (t2
), strict
))
1083 && compparms (TYPE_ARG_TYPES (t1
), TYPE_ARG_TYPES (t2
)));
1087 /* Target types must match incl. qualifiers. We use ORIG_STRICT
1088 here since this is the one place where
1089 COMPARE_REDECLARATION should be used. */
1090 val
= comp_array_types (comptypes
, t1
, t2
, orig_strict
);
1093 case TEMPLATE_TYPE_PARM
:
1094 return TEMPLATE_TYPE_IDX (t1
) == TEMPLATE_TYPE_IDX (t2
)
1095 && TEMPLATE_TYPE_LEVEL (t1
) == TEMPLATE_TYPE_LEVEL (t2
);
1098 if (cp_tree_equal (TYPENAME_TYPE_FULLNAME (t1
),
1099 TYPENAME_TYPE_FULLNAME (t2
)) < 1)
1101 return same_type_p (TYPE_CONTEXT (t1
), TYPE_CONTEXT (t2
));
1103 case UNBOUND_CLASS_TEMPLATE
:
1104 if (cp_tree_equal (TYPE_IDENTIFIER (t1
),
1105 TYPE_IDENTIFIER (t2
)) < 1)
1107 return same_type_p (TYPE_CONTEXT (t1
), TYPE_CONTEXT (t2
));
1110 return same_type_p (TREE_TYPE (t1
), TREE_TYPE (t2
));
1115 return attrval
== 2 && val
== 1 ? 2 : val
;
1118 /* Subroutine of comp_target-types. Make sure that the cv-quals change
1119 only in the same direction as the target type. */
1122 comp_cv_target_types (ttl
, ttr
, nptrs
)
1128 if (!at_least_as_qualified_p (ttl
, ttr
)
1129 && !at_least_as_qualified_p (ttr
, ttl
))
1130 /* The qualifications are incomparable. */
1133 if (TYPE_MAIN_VARIANT (ttl
) == TYPE_MAIN_VARIANT (ttr
))
1134 return more_qualified_p (ttr
, ttl
) ? -1 : 1;
1136 t
= comp_target_types (ttl
, ttr
, nptrs
);
1137 if ((t
== 1 && at_least_as_qualified_p (ttl
, ttr
))
1138 || (t
== -1 && at_least_as_qualified_p (ttr
, ttl
)))
1144 /* Return 1 or -1 if TTL and TTR are pointers to types that are equivalent,
1145 ignoring their qualifiers, 0 if not. Return 1 means that TTR can be
1146 converted to TTL. Return -1 means that TTL can be converted to TTR but
1149 NPTRS is the number of pointers we can strip off and keep cool.
1150 This is used to permit (for aggr A, aggr B) A, B* to convert to A*,
1151 but to not permit B** to convert to A**.
1153 This should go away. Callers should use can_convert or something
1154 similar instead. (jason 17 Apr 1997) */
1157 comp_target_types (ttl
, ttr
, nptrs
)
1161 ttl
= TYPE_MAIN_VARIANT (ttl
);
1162 ttr
= TYPE_MAIN_VARIANT (ttr
);
1163 if (same_type_p (ttl
, ttr
))
1166 if (TREE_CODE (ttr
) != TREE_CODE (ttl
))
1169 if ((TREE_CODE (ttr
) == POINTER_TYPE
1170 || TREE_CODE (ttr
) == REFERENCE_TYPE
)
1171 /* If we get a pointer with nptrs == 0, we don't allow any tweaking
1172 of the type pointed to. This is necessary for reference init
1173 semantics. We won't get here from a previous call with nptrs == 1;
1174 for multi-level pointers we end up in comp_ptr_ttypes. */
1177 int is_ptr
= TREE_CODE (ttr
) == POINTER_TYPE
;
1179 ttl
= TREE_TYPE (ttl
);
1180 ttr
= TREE_TYPE (ttr
);
1184 if (TREE_CODE (ttl
) == UNKNOWN_TYPE
1185 || TREE_CODE (ttr
) == UNKNOWN_TYPE
)
1187 else if (TREE_CODE (ttl
) == VOID_TYPE
1188 && TREE_CODE (ttr
) != FUNCTION_TYPE
1189 && TREE_CODE (ttr
) != METHOD_TYPE
1190 && TREE_CODE (ttr
) != OFFSET_TYPE
)
1192 else if (TREE_CODE (ttr
) == VOID_TYPE
1193 && TREE_CODE (ttl
) != FUNCTION_TYPE
1194 && TREE_CODE (ttl
) != METHOD_TYPE
1195 && TREE_CODE (ttl
) != OFFSET_TYPE
)
1197 else if (TREE_CODE (ttl
) == POINTER_TYPE
1198 || TREE_CODE (ttl
) == ARRAY_TYPE
)
1200 if (comp_ptr_ttypes (ttl
, ttr
))
1202 else if (comp_ptr_ttypes (ttr
, ttl
))
1208 /* Const and volatile mean something different for function types,
1209 so the usual checks are not appropriate. */
1210 if (TREE_CODE (ttl
) == FUNCTION_TYPE
|| TREE_CODE (ttl
) == METHOD_TYPE
)
1211 return comp_target_types (ttl
, ttr
, nptrs
- 1);
1213 return comp_cv_target_types (ttl
, ttr
, nptrs
- 1);
1216 if (TREE_CODE (ttr
) == ARRAY_TYPE
)
1217 return comp_array_types (comp_target_types
, ttl
, ttr
, COMPARE_STRICT
);
1218 else if (TREE_CODE (ttr
) == FUNCTION_TYPE
|| TREE_CODE (ttr
) == METHOD_TYPE
)
1225 if (!same_type_p (TREE_TYPE (ttl
), TREE_TYPE (ttr
)))
1230 switch (comp_target_types (TREE_TYPE (ttl
), TREE_TYPE (ttr
), -1))
1239 argsl
= TYPE_ARG_TYPES (ttl
);
1240 argsr
= TYPE_ARG_TYPES (ttr
);
1242 /* Compare 'this' here, not in comp_target_parms. */
1243 if (TREE_CODE (ttr
) == METHOD_TYPE
)
1245 tree tl
= TYPE_METHOD_BASETYPE (ttl
);
1246 tree tr
= TYPE_METHOD_BASETYPE (ttr
);
1248 if (!same_or_base_type_p (tr
, tl
))
1250 if (same_or_base_type_p (tl
, tr
))
1256 argsl
= TREE_CHAIN (argsl
);
1257 argsr
= TREE_CHAIN (argsr
);
1260 switch (comp_target_parms (argsl
, argsr
))
1268 return saw_contra
? -1 : 1;
1271 else if (TREE_CODE (ttr
) == OFFSET_TYPE
)
1275 /* Contravariance: we can assign a pointer to base member to a pointer
1276 to derived member. Note difference from simple pointer case, where
1277 we can pass a pointer to derived to a pointer to base. */
1278 if (same_or_base_type_p (TYPE_OFFSET_BASETYPE (ttr
),
1279 TYPE_OFFSET_BASETYPE (ttl
)))
1281 else if (same_or_base_type_p (TYPE_OFFSET_BASETYPE (ttl
),
1282 TYPE_OFFSET_BASETYPE (ttr
)))
1292 ttl
= TREE_TYPE (ttl
);
1293 ttr
= TREE_TYPE (ttr
);
1295 if (TREE_CODE (ttl
) == POINTER_TYPE
1296 || TREE_CODE (ttl
) == ARRAY_TYPE
)
1298 if (comp_ptr_ttypes (ttl
, ttr
))
1304 if (comp_cv_target_types (ttl
, ttr
, nptrs
) == 1)
1309 else if (IS_AGGR_TYPE (ttl
))
1313 if (same_or_base_type_p (build_pointer_type (ttl
),
1314 build_pointer_type (ttr
)))
1316 if (same_or_base_type_p (build_pointer_type (ttr
),
1317 build_pointer_type (ttl
)))
1325 /* Returns 1 if TYPE1 is at least as qualified as TYPE2. */
1328 at_least_as_qualified_p (type1
, type2
)
1332 /* All qualifiers for TYPE2 must also appear in TYPE1. */
1333 return ((cp_type_quals (type1
) & cp_type_quals (type2
))
1334 == cp_type_quals (type2
));
1337 /* Returns 1 if TYPE1 is more qualified than TYPE2. */
1340 more_qualified_p (type1
, type2
)
1344 return (cp_type_quals (type1
) != cp_type_quals (type2
)
1345 && at_least_as_qualified_p (type1
, type2
));
1348 /* Returns 1 if TYPE1 is more cv-qualified than TYPE2, -1 if TYPE2 is
1349 more cv-qualified that TYPE1, and 0 otherwise. */
1352 comp_cv_qualification (type1
, type2
)
1356 if (cp_type_quals (type1
) == cp_type_quals (type2
))
1359 if (at_least_as_qualified_p (type1
, type2
))
1362 else if (at_least_as_qualified_p (type2
, type1
))
1368 /* Returns 1 if the cv-qualification signature of TYPE1 is a proper
1369 subset of the cv-qualification signature of TYPE2, and the types
1370 are similar. Returns -1 if the other way 'round, and 0 otherwise. */
1373 comp_cv_qual_signature (type1
, type2
)
1377 if (comp_ptr_ttypes_real (type2
, type1
, -1))
1379 else if (comp_ptr_ttypes_real (type1
, type2
, -1))
1385 /* If two types share a common base type, return that basetype.
1386 If there is not a unique most-derived base type, this function
1387 returns ERROR_MARK_NODE. */
1390 common_base_type (tt1
, tt2
)
1393 tree best
= NULL_TREE
;
1396 /* If one is a baseclass of another, that's good enough. */
1397 if (UNIQUELY_DERIVED_FROM_P (tt1
, tt2
))
1399 if (UNIQUELY_DERIVED_FROM_P (tt2
, tt1
))
1402 /* Otherwise, try to find a unique baseclass of TT1
1403 that is shared by TT2, and follow that down. */
1404 for (i
= CLASSTYPE_N_BASECLASSES (tt1
)-1; i
>= 0; i
--)
1406 tree basetype
= TYPE_BINFO_BASETYPE (tt1
, i
);
1407 tree trial
= common_base_type (basetype
, tt2
);
1410 if (trial
== error_mark_node
)
1412 if (best
== NULL_TREE
)
1414 else if (best
!= trial
)
1415 return error_mark_node
;
1420 for (i
= CLASSTYPE_N_BASECLASSES (tt2
)-1; i
>= 0; i
--)
1422 tree basetype
= TYPE_BINFO_BASETYPE (tt2
, i
);
1423 tree trial
= common_base_type (tt1
, basetype
);
1426 if (trial
== error_mark_node
)
1428 if (best
== NULL_TREE
)
1430 else if (best
!= trial
)
1431 return error_mark_node
;
1437 /* Subroutines of `comptypes'. */
1439 /* Return 1 if two parameter type lists PARMS1 and PARMS2 are
1440 equivalent in the sense that functions with those parameter types
1441 can have equivalent types. The two lists must be equivalent,
1444 C++: See comment above about TYPE1, TYPE2. */
1447 compparms (parms1
, parms2
)
1448 tree parms1
, parms2
;
1450 register tree t1
= parms1
, t2
= parms2
;
1452 /* An unspecified parmlist matches any specified parmlist
1453 whose argument types don't need default promotions. */
1457 if (t1
== 0 && t2
== 0)
1459 /* If one parmlist is shorter than the other,
1460 they fail to match. */
1461 if (t1
== 0 || t2
== 0)
1463 if (!same_type_p (TREE_VALUE (t2
), TREE_VALUE (t1
)))
1466 t1
= TREE_CHAIN (t1
);
1467 t2
= TREE_CHAIN (t2
);
1471 /* This really wants return whether or not parameter type lists
1472 would make their owning functions assignment compatible or not.
1474 The return value is like for comp_target_types.
1476 This should go away, possibly with the exception of the empty parmlist
1477 conversion; there are no conversions between function types in C++.
1478 (jason 17 Apr 1997) */
1481 comp_target_parms (parms1
, parms2
)
1482 tree parms1
, parms2
;
1484 register tree t1
= parms1
, t2
= parms2
;
1485 int warn_contravariance
= 0;
1487 /* In C, an unspecified parmlist matches any specified parmlist
1488 whose argument types don't need default promotions. This is not
1489 true for C++, but let's do it anyway for unfixed headers. */
1491 if (t1
== 0 && t2
!= 0)
1493 pedwarn ("ISO C++ prohibits conversion from `%#T' to `(...)'",
1495 return self_promoting_args_p (t2
);
1498 return self_promoting_args_p (t1
);
1500 for (; t1
|| t2
; t1
= TREE_CHAIN (t1
), t2
= TREE_CHAIN (t2
))
1504 /* If one parmlist is shorter than the other,
1505 they fail to match, unless STRICT is <= 0. */
1506 if (t1
== 0 || t2
== 0)
1508 p1
= TREE_VALUE (t1
);
1509 p2
= TREE_VALUE (t2
);
1510 if (same_type_p (p1
, p2
))
1516 if ((TREE_CODE (p1
) == POINTER_TYPE
&& TREE_CODE (p2
) == POINTER_TYPE
)
1517 || (TREE_CODE (p1
) == REFERENCE_TYPE
1518 && TREE_CODE (p2
) == REFERENCE_TYPE
))
1520 /* The following is wrong for contravariance,
1521 but many programs depend on it. */
1522 if (TREE_TYPE (p1
) == void_type_node
)
1524 if (TREE_TYPE (p2
) == void_type_node
)
1526 warn_contravariance
= 1;
1529 if (IS_AGGR_TYPE (TREE_TYPE (p1
))
1530 && !same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (p1
),
1534 /* Note backwards order due to contravariance. */
1535 if (comp_target_types (p2
, p1
, 1) <= 0)
1537 if (comp_target_types (p1
, p2
, 1) > 0)
1539 warn_contravariance
= 1;
1545 return warn_contravariance
? -1 : 1;
1548 /* Compute the value of the `sizeof' operator. */
1554 enum tree_code code
= TREE_CODE (type
);
1557 if (processing_template_decl
)
1558 return build_min_nt (SIZEOF_EXPR
, type
);
1560 if (code
== FUNCTION_TYPE
)
1562 if (pedantic
|| warn_pointer_arith
)
1563 pedwarn ("ISO C++ forbids applying `sizeof' to a function type");
1564 size
= size_one_node
;
1566 else if (code
== METHOD_TYPE
)
1568 if (pedantic
|| warn_pointer_arith
)
1569 pedwarn ("ISO C++ forbids applying `sizeof' to a member function");
1570 size
= size_one_node
;
1572 else if (code
== VOID_TYPE
)
1574 if (pedantic
|| warn_pointer_arith
)
1575 pedwarn ("ISO C++ forbids applying `sizeof' to type `void' which is an incomplete type");
1576 size
= size_one_node
;
1578 else if (code
== ERROR_MARK
)
1579 size
= size_one_node
;
1582 /* ARM $5.3.2: ``When applied to a reference, the result is the
1583 size of the referenced object.'' */
1584 if (code
== REFERENCE_TYPE
)
1585 type
= TREE_TYPE (type
);
1587 if (code
== OFFSET_TYPE
)
1589 error ("`sizeof' applied to non-static member");
1590 size
= size_zero_node
;
1592 else if (!COMPLETE_TYPE_P (complete_type (type
)))
1594 error ("`sizeof' applied to incomplete type `%T'", type
);
1595 size
= size_zero_node
;
1598 /* Convert in case a char is more than one unit. */
1599 size
= size_binop (CEIL_DIV_EXPR
, TYPE_SIZE_UNIT (type
),
1600 size_int (TYPE_PRECISION (char_type_node
)
1604 /* SIZE will have an integer type with TYPE_IS_SIZETYPE set.
1605 TYPE_IS_SIZETYPE means that certain things (like overflow) will
1606 never happen. However, this node should really have type
1607 `size_t', which is just a typedef for an ordinary integer type. */
1608 size
= fold (build1 (NOP_EXPR
, c_size_type_node
, size
));
1609 my_friendly_assert (!TYPE_IS_SIZETYPE (TREE_TYPE (size
)),
1619 if (processing_template_decl
)
1620 return build_min_nt (SIZEOF_EXPR
, e
);
1622 if (TREE_CODE (e
) == COMPONENT_REF
1623 && DECL_C_BIT_FIELD (TREE_OPERAND (e
, 1)))
1624 error ("sizeof applied to a bit-field");
1625 if (is_overloaded_fn (e
))
1627 pedwarn ("ISO C++ forbids applying `sizeof' to an expression of function type");
1628 return c_sizeof (char_type_node
);
1630 else if (type_unknown_p (e
))
1632 incomplete_type_error (e
, TREE_TYPE (e
));
1633 return c_sizeof (char_type_node
);
1635 /* It's illegal to say `sizeof (X::i)' for `i' a non-static data
1636 member unless you're in a non-static member of X. So hand off to
1637 resolve_offset_ref. [expr.prim] */
1638 else if (TREE_CODE (e
) == OFFSET_REF
)
1639 e
= resolve_offset_ref (e
);
1641 if (e
== error_mark_node
)
1644 return c_sizeof (TREE_TYPE (e
));
1648 c_sizeof_nowarn (type
)
1651 enum tree_code code
= TREE_CODE (type
);
1654 if (code
== FUNCTION_TYPE
1655 || code
== METHOD_TYPE
1656 || code
== VOID_TYPE
1657 || code
== ERROR_MARK
)
1658 size
= size_one_node
;
1661 if (code
== REFERENCE_TYPE
)
1662 type
= TREE_TYPE (type
);
1664 if (!COMPLETE_TYPE_P (type
))
1665 size
= size_zero_node
;
1667 /* Convert in case a char is more than one unit. */
1668 size
= size_binop (CEIL_DIV_EXPR
, TYPE_SIZE_UNIT (type
),
1669 size_int (TYPE_PRECISION (char_type_node
)
1673 /* SIZE will have an integer type with TYPE_IS_SIZETYPE set.
1674 TYPE_IS_SIZETYPE means that certain things (like overflow) will
1675 never happen. However, this node should really have type
1676 `size_t', which is just a typedef for an ordinary integer type. */
1677 size
= fold (build1 (NOP_EXPR
, c_size_type_node
, size
));
1678 my_friendly_assert (!TYPE_IS_SIZETYPE (TREE_TYPE (size
)),
1683 /* Perform the array-to-pointer and function-to-pointer conversions
1686 In addition, references are converted to lvalues and manifest
1687 constants are replaced by their values. */
1690 decay_conversion (exp
)
1694 register enum tree_code code
;
1696 if (TREE_CODE (exp
) == OFFSET_REF
)
1697 exp
= resolve_offset_ref (exp
);
1699 type
= TREE_TYPE (exp
);
1700 code
= TREE_CODE (type
);
1702 if (code
== REFERENCE_TYPE
)
1704 exp
= convert_from_reference (exp
);
1705 type
= TREE_TYPE (exp
);
1706 code
= TREE_CODE (type
);
1709 if (type
== error_mark_node
)
1710 return error_mark_node
;
1712 if (type_unknown_p (exp
))
1714 incomplete_type_error (exp
, TREE_TYPE (exp
));
1715 return error_mark_node
;
1718 /* Constants can be used directly unless they're not loadable. */
1719 if (TREE_CODE (exp
) == CONST_DECL
)
1720 exp
= DECL_INITIAL (exp
);
1721 /* Replace a nonvolatile const static variable with its value. We
1722 don't do this for arrays, though; we want the address of the
1723 first element of the array, not the address of the first element
1724 of its initializing constant. */
1725 else if (code
!= ARRAY_TYPE
)
1727 exp
= decl_constant_value (exp
);
1728 type
= TREE_TYPE (exp
);
1731 /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
1732 Leave such NOP_EXPRs, since RHS is being used in non-lvalue context. */
1734 if (code
== VOID_TYPE
)
1736 error ("void value not ignored as it ought to be");
1737 return error_mark_node
;
1739 if (code
== METHOD_TYPE
)
1740 my_friendly_abort (990506);
1741 if (code
== FUNCTION_TYPE
|| is_overloaded_fn (exp
))
1742 return build_unary_op (ADDR_EXPR
, exp
, 0);
1743 if (code
== ARRAY_TYPE
)
1748 if (TREE_CODE (exp
) == INDIRECT_REF
)
1750 /* Stripping away the INDIRECT_REF is not the right
1751 thing to do for references... */
1752 tree inner
= TREE_OPERAND (exp
, 0);
1753 if (TREE_CODE (TREE_TYPE (inner
)) == REFERENCE_TYPE
)
1755 inner
= build1 (CONVERT_EXPR
,
1756 build_pointer_type (TREE_TYPE
1757 (TREE_TYPE (inner
))),
1759 TREE_CONSTANT (inner
) = TREE_CONSTANT (TREE_OPERAND (inner
, 0));
1761 return cp_convert (build_pointer_type (TREE_TYPE (type
)), inner
);
1764 if (TREE_CODE (exp
) == COMPOUND_EXPR
)
1766 tree op1
= decay_conversion (TREE_OPERAND (exp
, 1));
1767 return build (COMPOUND_EXPR
, TREE_TYPE (op1
),
1768 TREE_OPERAND (exp
, 0), op1
);
1772 && ! (TREE_CODE (exp
) == CONSTRUCTOR
&& TREE_STATIC (exp
)))
1774 error ("invalid use of non-lvalue array");
1775 return error_mark_node
;
1778 ptrtype
= build_pointer_type (TREE_TYPE (type
));
1780 if (TREE_CODE (exp
) == VAR_DECL
)
1782 /* ??? This is not really quite correct
1783 in that the type of the operand of ADDR_EXPR
1784 is not the target type of the type of the ADDR_EXPR itself.
1785 Question is, can this lossage be avoided? */
1786 adr
= build1 (ADDR_EXPR
, ptrtype
, exp
);
1787 if (mark_addressable (exp
) == 0)
1788 return error_mark_node
;
1789 TREE_CONSTANT (adr
) = staticp (exp
);
1790 TREE_SIDE_EFFECTS (adr
) = 0; /* Default would be, same as EXP. */
1793 /* This way is better for a COMPONENT_REF since it can
1794 simplify the offset for a component. */
1795 adr
= build_unary_op (ADDR_EXPR
, exp
, 1);
1796 return cp_convert (ptrtype
, adr
);
1799 /* [basic.lval]: Class rvalues can have cv-qualified types; non-class
1800 rvalues always have cv-unqualified types. */
1801 if (! CLASS_TYPE_P (type
))
1802 exp
= cp_convert (TYPE_MAIN_VARIANT (type
), exp
);
1808 default_conversion (exp
)
1812 enum tree_code code
;
1814 exp
= decay_conversion (exp
);
1816 type
= TREE_TYPE (exp
);
1817 code
= TREE_CODE (type
);
1819 if (INTEGRAL_CODE_P (code
))
1821 tree t
= type_promotes_to (type
);
1823 return cp_convert (t
, exp
);
1829 /* Take the address of an inline function without setting TREE_ADDRESSABLE
1833 inline_conversion (exp
)
1836 if (TREE_CODE (exp
) == FUNCTION_DECL
)
1837 exp
= build1 (ADDR_EXPR
, build_pointer_type (TREE_TYPE (exp
)), exp
);
1842 /* Returns nonzero iff exp is a STRING_CST or the result of applying
1843 decay_conversion to one. */
1846 string_conv_p (totype
, exp
, warn
)
1852 if (! flag_const_strings
|| TREE_CODE (totype
) != POINTER_TYPE
)
1855 t
= TREE_TYPE (totype
);
1856 if (!same_type_p (t
, char_type_node
)
1857 && !same_type_p (t
, wchar_type_node
))
1860 if (TREE_CODE (exp
) == STRING_CST
)
1862 /* Make sure that we don't try to convert between char and wchar_t. */
1863 if (!same_type_p (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (exp
))), t
))
1868 /* Is this a string constant which has decayed to 'const char *'? */
1869 t
= build_pointer_type (build_qualified_type (t
, TYPE_QUAL_CONST
));
1870 if (!same_type_p (TREE_TYPE (exp
), t
))
1873 if (TREE_CODE (exp
) != ADDR_EXPR
1874 || TREE_CODE (TREE_OPERAND (exp
, 0)) != STRING_CST
)
1878 /* This warning is not very useful, as it complains about printf. */
1879 if (warn
&& warn_write_strings
)
1880 warning ("deprecated conversion from string constant to `%T'", totype
);
1886 build_object_ref (datum
, basetype
, field
)
1887 tree datum
, basetype
, field
;
1890 if (datum
== error_mark_node
)
1891 return error_mark_node
;
1893 dtype
= TREE_TYPE (datum
);
1894 if (TREE_CODE (dtype
) == REFERENCE_TYPE
)
1895 dtype
= TREE_TYPE (dtype
);
1896 if (! IS_AGGR_TYPE_CODE (TREE_CODE (dtype
)))
1898 error ("request for member `%T::%D' in expression of non-aggregate type `%T'",
1899 basetype
, field
, dtype
);
1900 return error_mark_node
;
1902 else if (is_aggr_type (basetype
, 1))
1904 tree binfo
= binfo_or_else (basetype
, dtype
);
1906 return build_x_component_ref (build_scoped_ref (datum
, basetype
),
1909 return error_mark_node
;
1912 /* Like `build_component_ref, but uses an already found field, and converts
1913 from a reference. Must compute access for current_class_ref.
1917 build_component_ref_1 (datum
, field
, protect
)
1921 return convert_from_reference
1922 (build_component_ref (datum
, field
, NULL_TREE
, protect
));
1925 /* Given a COND_EXPR, MIN_EXPR, or MAX_EXPR in T, return it in a form that we
1926 can, for example, use as an lvalue. This code used to be in
1927 unary_complex_lvalue, but we needed it to deal with `a = (d == c) ? b : c'
1928 expressions, where we're dealing with aggregates. But now it's again only
1929 called from unary_complex_lvalue. The case (in particular) that led to
1930 this was with CODE == ADDR_EXPR, since it's not an lvalue when we'd
1934 rationalize_conditional_expr (code
, t
)
1935 enum tree_code code
;
1938 /* For MIN_EXPR or MAX_EXPR, fold-const.c has arranged things so that
1939 the first operand is always the one to be used if both operands
1940 are equal, so we know what conditional expression this used to be. */
1941 if (TREE_CODE (t
) == MIN_EXPR
|| TREE_CODE (t
) == MAX_EXPR
)
1944 build_conditional_expr (build_x_binary_op ((TREE_CODE (t
) == MIN_EXPR
1945 ? LE_EXPR
: GE_EXPR
),
1946 TREE_OPERAND (t
, 0),
1947 TREE_OPERAND (t
, 1)),
1948 build_unary_op (code
, TREE_OPERAND (t
, 0), 0),
1949 build_unary_op (code
, TREE_OPERAND (t
, 1), 0));
1953 build_conditional_expr (TREE_OPERAND (t
, 0),
1954 build_unary_op (code
, TREE_OPERAND (t
, 1), 0),
1955 build_unary_op (code
, TREE_OPERAND (t
, 2), 0));
1958 /* Given the TYPE of an anonymous union field inside T, return the
1959 FIELD_DECL for the field. If not found return NULL_TREE. Because
1960 anonymous unions can nest, we must also search all anonymous unions
1961 that are directly reachable. */
1964 lookup_anon_field (t
, type
)
1969 for (field
= TYPE_FIELDS (t
); field
; field
= TREE_CHAIN (field
))
1971 if (TREE_STATIC (field
))
1973 if (TREE_CODE (field
) != FIELD_DECL
)
1976 /* If we find it directly, return the field. */
1977 if (DECL_NAME (field
) == NULL_TREE
1978 && type
== TYPE_MAIN_VARIANT (TREE_TYPE (field
)))
1983 /* Otherwise, it could be nested, search harder. */
1984 if (DECL_NAME (field
) == NULL_TREE
1985 && ANON_AGGR_TYPE_P (TREE_TYPE (field
)))
1987 tree subfield
= lookup_anon_field (TREE_TYPE (field
), type
);
1995 /* Build a COMPONENT_REF for a given DATUM, and it's member COMPONENT.
1996 COMPONENT can be an IDENTIFIER_NODE that is the name of the member
1997 that we are interested in, or it can be a FIELD_DECL. */
2000 build_component_ref (datum
, component
, basetype_path
, protect
)
2001 tree datum
, component
, basetype_path
;
2004 register tree basetype
;
2005 register enum tree_code code
;
2006 register tree field
= NULL
;
2011 if (processing_template_decl
)
2012 return build_min_nt (COMPONENT_REF
, datum
, component
);
2014 if (datum
== error_mark_node
2015 || TREE_TYPE (datum
) == error_mark_node
)
2016 return error_mark_node
;
2018 /* BASETYPE holds the type of the class containing the COMPONENT. */
2019 basetype
= TYPE_MAIN_VARIANT (TREE_TYPE (datum
));
2021 /* If DATUM is a COMPOUND_EXPR or COND_EXPR, move our reference
2023 switch (TREE_CODE (datum
))
2027 tree value
= build_component_ref (TREE_OPERAND (datum
, 1), component
,
2028 basetype_path
, protect
);
2029 return build (COMPOUND_EXPR
, TREE_TYPE (value
),
2030 TREE_OPERAND (datum
, 0), value
);
2033 return build_conditional_expr
2034 (TREE_OPERAND (datum
, 0),
2035 build_component_ref (TREE_OPERAND (datum
, 1), component
,
2036 basetype_path
, protect
),
2037 build_component_ref (TREE_OPERAND (datum
, 2), component
,
2038 basetype_path
, protect
));
2041 error ("invalid use of %D", datum
);
2042 datum
= error_mark_node
;
2049 code
= TREE_CODE (basetype
);
2051 if (code
== REFERENCE_TYPE
)
2053 datum
= convert_from_reference (datum
);
2054 basetype
= TYPE_MAIN_VARIANT (TREE_TYPE (datum
));
2055 code
= TREE_CODE (basetype
);
2057 if (TREE_CODE (datum
) == OFFSET_REF
)
2059 datum
= resolve_offset_ref (datum
);
2060 basetype
= TYPE_MAIN_VARIANT (TREE_TYPE (datum
));
2061 code
= TREE_CODE (basetype
);
2064 /* First, see if there is a field or component with name COMPONENT. */
2065 if (TREE_CODE (component
) == TREE_LIST
)
2067 /* I could not trigger this code. MvL */
2068 my_friendly_abort (980326);
2070 my_friendly_assert (!(TREE_CHAIN (component
) == NULL_TREE
2071 && DECL_CHAIN (TREE_VALUE (component
)) == NULL_TREE
), 309);
2073 return build (COMPONENT_REF
, TREE_TYPE (component
), datum
, component
);
2076 if (! IS_AGGR_TYPE_CODE (code
))
2078 if (code
!= ERROR_MARK
)
2079 error ("request for member `%D' in `%E', which is of non-aggregate type `%T'",
2080 component
, datum
, basetype
);
2081 return error_mark_node
;
2084 if (!complete_type_or_else (basetype
, datum
))
2085 return error_mark_node
;
2087 if (TREE_CODE (component
) == BIT_NOT_EXPR
)
2089 if (TYPE_IDENTIFIER (basetype
) != TREE_OPERAND (component
, 0))
2091 error ("destructor specifier `%T::~%T' must have matching names",
2092 basetype
, TREE_OPERAND (component
, 0));
2093 return error_mark_node
;
2095 if (! TYPE_HAS_DESTRUCTOR (basetype
))
2097 error ("type `%T' has no destructor", basetype
);
2098 return error_mark_node
;
2100 return TREE_VEC_ELT (CLASSTYPE_METHOD_VEC (basetype
), 1);
2103 /* Look up component name in the structure type definition. */
2104 if (TYPE_VFIELD (basetype
)
2105 && DECL_NAME (TYPE_VFIELD (basetype
)) == component
)
2106 /* Special-case this because if we use normal lookups in an ambiguous
2107 hierarchy, the compiler will abort (because vptr lookups are
2108 not supposed to be ambiguous. */
2109 field
= TYPE_VFIELD (basetype
);
2110 else if (TREE_CODE (component
) == FIELD_DECL
)
2112 else if (TREE_CODE (component
) == TYPE_DECL
)
2114 error ("invalid use of type decl `%#D' as expression", component
);
2115 return error_mark_node
;
2117 else if (TREE_CODE (component
) == TEMPLATE_DECL
)
2119 error ("invalid use of template `%#D' as expression", component
);
2120 return error_mark_node
;
2124 tree name
= component
;
2125 if (TREE_CODE (component
) == VAR_DECL
)
2126 name
= DECL_NAME (component
);
2127 if (TREE_CODE (component
) == NAMESPACE_DECL
)
2128 /* Source is in error, but produce a sensible diagnostic. */
2129 name
= DECL_NAME (component
);
2130 if (basetype_path
== NULL_TREE
)
2131 basetype_path
= TYPE_BINFO (basetype
);
2132 field
= lookup_field (basetype_path
, name
,
2133 protect
&& !VFIELD_NAME_P (name
), 0);
2134 if (field
== error_mark_node
)
2135 return error_mark_node
;
2137 if (field
== NULL_TREE
)
2139 /* Not found as a data field, look for it as a method. If found,
2140 then if this is the only possible one, return it, else
2141 report ambiguity error. */
2142 tree fndecls
= lookup_fnfields (basetype_path
, name
, 1);
2143 if (fndecls
== error_mark_node
)
2144 return error_mark_node
;
2147 /* If the function is unique and static, we can resolve it
2148 now. Otherwise, we have to wait and see what context it is
2149 used in; a component_ref involving a non-static member
2150 function can only be used in a call (expr.ref). */
2152 if (TREE_CHAIN (fndecls
) == NULL_TREE
2153 && TREE_CODE (TREE_VALUE (fndecls
)) == FUNCTION_DECL
)
2155 if (DECL_STATIC_FUNCTION_P (TREE_VALUE (fndecls
)))
2157 tree fndecl
= TREE_VALUE (fndecls
);
2158 enforce_access (basetype_path
, fndecl
);
2164 /* A unique non-static member function. Other parts
2165 of the compiler expect something with
2166 unknown_type_node to be really overloaded, so
2168 TREE_VALUE (fndecls
)
2169 = ovl_cons (TREE_VALUE (fndecls
), NULL_TREE
);
2173 ref
= build (COMPONENT_REF
, unknown_type_node
,
2174 datum
, TREE_VALUE (fndecls
));
2178 error ("`%#T' has no member named `%D'", basetype
, name
);
2179 return error_mark_node
;
2181 else if (TREE_TYPE (field
) == error_mark_node
)
2182 return error_mark_node
;
2184 if (TREE_CODE (field
) != FIELD_DECL
)
2186 if (TREE_CODE (field
) == TYPE_DECL
)
2187 pedwarn ("invalid use of type decl `%#D' as expression", field
);
2188 else if (DECL_RTL (field
) != 0)
2191 TREE_USED (field
) = 1;
2193 /* Do evaluate the object when accessing a static member. */
2194 if (TREE_SIDE_EFFECTS (datum
))
2195 field
= build (COMPOUND_EXPR
, TREE_TYPE (field
), datum
, field
);
2201 /* See if we have to do any conversions so that we pick up the field from the
2203 if (DECL_FIELD_CONTEXT (field
) != basetype
)
2205 tree context
= DECL_FIELD_CONTEXT (field
);
2206 tree base
= context
;
2207 while (!same_type_p (base
, basetype
) && TYPE_NAME (base
)
2208 && ANON_AGGR_TYPE_P (base
))
2209 base
= TYPE_CONTEXT (base
);
2211 /* Handle base classes here... */
2212 if (base
!= basetype
&& TYPE_BASE_CONVS_MAY_REQUIRE_CODE_P (basetype
))
2214 tree binfo
= lookup_base (TREE_TYPE (datum
), base
, ba_check
, NULL
);
2216 if (TREE_CODE (datum
) == INDIRECT_REF
2217 && integer_zerop (TREE_OPERAND (datum
, 0)))
2219 error ("invalid reference to NULL ptr, use ptr-to-member instead");
2220 return error_mark_node
;
2222 datum
= build_base_path (PLUS_EXPR
, datum
, binfo
, 1);
2223 if (datum
== error_mark_node
)
2224 return error_mark_node
;
2228 /* Handle things from anon unions here... */
2229 if (TYPE_NAME (context
) && ANON_AGGR_TYPE_P (context
))
2231 tree subfield
= lookup_anon_field (basetype
, context
);
2232 tree subdatum
= build_component_ref (datum
, subfield
,
2233 basetype_path
, protect
);
2234 return build_component_ref (subdatum
, field
, basetype_path
, protect
);
2238 /* Compute the type of the field, as described in [expr.ref]. */
2239 type_quals
= TYPE_UNQUALIFIED
;
2240 field_type
= TREE_TYPE (field
);
2241 if (TREE_CODE (field_type
) == REFERENCE_TYPE
)
2242 /* The standard says that the type of the result should be the
2243 type referred to by the reference. But for now, at least, we
2244 do the conversion from reference type later. */
2248 type_quals
= (cp_type_quals (field_type
)
2249 | cp_type_quals (TREE_TYPE (datum
)));
2251 /* A field is const (volatile) if the enclosing object, or the
2252 field itself, is const (volatile). But, a mutable field is
2253 not const, even within a const object. */
2254 if (DECL_MUTABLE_P (field
))
2255 type_quals
&= ~TYPE_QUAL_CONST
;
2256 field_type
= cp_build_qualified_type (field_type
, type_quals
);
2259 ref
= fold (build (COMPONENT_REF
, field_type
, datum
, field
));
2261 /* Mark the expression const or volatile, as appropriate. Even
2262 though we've dealt with the type above, we still have to mark the
2263 expression itself. */
2264 if (type_quals
& TYPE_QUAL_CONST
)
2265 TREE_READONLY (ref
) = 1;
2266 else if (type_quals
& TYPE_QUAL_VOLATILE
)
2267 TREE_THIS_VOLATILE (ref
) = 1;
2272 /* Variant of build_component_ref for use in expressions, which should
2273 never have REFERENCE_TYPE. */
2276 build_x_component_ref (datum
, component
, basetype_path
, protect
)
2277 tree datum
, component
, basetype_path
;
2280 tree t
= build_component_ref (datum
, component
, basetype_path
, protect
);
2282 if (! processing_template_decl
)
2283 t
= convert_from_reference (t
);
2288 /* Given an expression PTR for a pointer, return an expression
2289 for the value pointed to.
2290 ERRORSTRING is the name of the operator to appear in error messages.
2292 This function may need to overload OPERATOR_FNNAME.
2293 Must also handle REFERENCE_TYPEs for C++. */
2296 build_x_indirect_ref (ptr
, errorstring
)
2298 const char *errorstring
;
2302 if (processing_template_decl
)
2303 return build_min_nt (INDIRECT_REF
, ptr
);
2305 rval
= build_opfncall (INDIRECT_REF
, LOOKUP_NORMAL
, ptr
, NULL_TREE
,
2309 return build_indirect_ref (ptr
, errorstring
);
2313 build_indirect_ref (ptr
, errorstring
)
2315 const char *errorstring
;
2317 register tree pointer
, type
;
2319 if (ptr
== error_mark_node
)
2320 return error_mark_node
;
2322 if (ptr
== current_class_ptr
)
2323 return current_class_ref
;
2325 pointer
= (TREE_CODE (TREE_TYPE (ptr
)) == REFERENCE_TYPE
2326 ? ptr
: default_conversion (ptr
));
2327 type
= TREE_TYPE (pointer
);
2329 if (TYPE_PTR_P (type
) || TREE_CODE (type
) == REFERENCE_TYPE
)
2333 If the type of the expression is "pointer to T," the type
2334 of the result is "T."
2336 We must use the canonical variant because certain parts of
2337 the back end, like fold, do pointer comparisons between
2339 tree t
= canonical_type_variant (TREE_TYPE (type
));
2341 if (VOID_TYPE_P (t
))
2343 /* A pointer to incomplete type (other than cv void) can be
2344 dereferenced [expr.unary.op]/1 */
2345 error ("`%T' is not a pointer-to-object type", type
);
2346 return error_mark_node
;
2348 else if (TREE_CODE (pointer
) == ADDR_EXPR
2350 && same_type_p (t
, TREE_TYPE (TREE_OPERAND (pointer
, 0))))
2351 /* The POINTER was something like `&x'. We simplify `*&x' to
2353 return TREE_OPERAND (pointer
, 0);
2356 tree ref
= build1 (INDIRECT_REF
, t
, pointer
);
2358 /* We *must* set TREE_READONLY when dereferencing a pointer to const,
2359 so that we get the proper error message if the result is used
2360 to assign to. Also, &* is supposed to be a no-op. */
2361 TREE_READONLY (ref
) = CP_TYPE_CONST_P (t
);
2362 TREE_THIS_VOLATILE (ref
) = CP_TYPE_VOLATILE_P (t
);
2363 TREE_SIDE_EFFECTS (ref
)
2364 = (TREE_THIS_VOLATILE (ref
) || TREE_SIDE_EFFECTS (pointer
)
2369 /* `pointer' won't be an error_mark_node if we were given a
2370 pointer to member, so it's cool to check for this here. */
2371 else if (TYPE_PTRMEM_P (type
) || TYPE_PTRMEMFUNC_P (type
))
2372 error ("invalid use of `%s' on pointer to member", errorstring
);
2373 else if (pointer
!= error_mark_node
)
2376 error ("invalid type argument of `%s'", errorstring
);
2378 error ("invalid type argument");
2380 return error_mark_node
;
2383 /* This handles expressions of the form "a[i]", which denotes
2386 This is logically equivalent in C to *(a+i), but we may do it differently.
2387 If A is a variable or a member, we generate a primitive ARRAY_REF.
2388 This avoids forcing the array out of registers, and can work on
2389 arrays that are not lvalues (for example, members of structures returned
2392 If INDEX is of some user-defined type, it must be converted to
2393 integer type. Otherwise, to make a compatible PLUS_EXPR, it
2394 will inherit the type of the array, which will be some pointer type. */
2397 build_array_ref (array
, idx
)
2402 error ("subscript missing in array reference");
2403 return error_mark_node
;
2406 if (TREE_TYPE (array
) == error_mark_node
2407 || TREE_TYPE (idx
) == error_mark_node
)
2408 return error_mark_node
;
2410 /* If ARRAY is a COMPOUND_EXPR or COND_EXPR, move our reference
2412 switch (TREE_CODE (array
))
2416 tree value
= build_array_ref (TREE_OPERAND (array
, 1), idx
);
2417 return build (COMPOUND_EXPR
, TREE_TYPE (value
),
2418 TREE_OPERAND (array
, 0), value
);
2422 return build_conditional_expr
2423 (TREE_OPERAND (array
, 0),
2424 build_array_ref (TREE_OPERAND (array
, 1), idx
),
2425 build_array_ref (TREE_OPERAND (array
, 2), idx
));
2431 if (TREE_CODE (TREE_TYPE (array
)) == ARRAY_TYPE
2432 && TREE_CODE (array
) != INDIRECT_REF
)
2436 /* Subscripting with type char is likely to lose
2437 on a machine where chars are signed.
2438 So warn on any machine, but optionally.
2439 Don't warn for unsigned char since that type is safe.
2440 Don't warn for signed char because anyone who uses that
2441 must have done so deliberately. */
2442 if (warn_char_subscripts
2443 && TYPE_MAIN_VARIANT (TREE_TYPE (idx
)) == char_type_node
)
2444 warning ("array subscript has type `char'");
2446 /* Apply default promotions *after* noticing character types. */
2447 idx
= default_conversion (idx
);
2449 if (TREE_CODE (TREE_TYPE (idx
)) != INTEGER_TYPE
)
2451 error ("array subscript is not an integer");
2452 return error_mark_node
;
2455 /* An array that is indexed by a non-constant
2456 cannot be stored in a register; we must be able to do
2457 address arithmetic on its address.
2458 Likewise an array of elements of variable size. */
2459 if (TREE_CODE (idx
) != INTEGER_CST
2460 || (COMPLETE_TYPE_P (TREE_TYPE (TREE_TYPE (array
)))
2461 && (TREE_CODE (TYPE_SIZE (TREE_TYPE (TREE_TYPE (array
))))
2464 if (mark_addressable (array
) == 0)
2465 return error_mark_node
;
2468 /* An array that is indexed by a constant value which is not within
2469 the array bounds cannot be stored in a register either; because we
2470 would get a crash in store_bit_field/extract_bit_field when trying
2471 to access a non-existent part of the register. */
2472 if (TREE_CODE (idx
) == INTEGER_CST
2473 && TYPE_VALUES (TREE_TYPE (array
))
2474 && ! int_fits_type_p (idx
, TYPE_VALUES (TREE_TYPE (array
))))
2476 if (mark_addressable (array
) == 0)
2477 return error_mark_node
;
2480 if (pedantic
&& !lvalue_p (array
))
2481 pedwarn ("ISO C++ forbids subscripting non-lvalue array");
2483 /* Note in C++ it is valid to subscript a `register' array, since
2484 it is valid to take the address of something with that
2485 storage specification. */
2489 while (TREE_CODE (foo
) == COMPONENT_REF
)
2490 foo
= TREE_OPERAND (foo
, 0);
2491 if (TREE_CODE (foo
) == VAR_DECL
&& DECL_REGISTER (foo
))
2492 warning ("subscripting array declared `register'");
2495 type
= TREE_TYPE (TREE_TYPE (array
));
2496 rval
= build (ARRAY_REF
, type
, array
, idx
);
2497 /* Array ref is const/volatile if the array elements are
2498 or if the array is.. */
2499 TREE_READONLY (rval
)
2500 |= (CP_TYPE_CONST_P (type
) | TREE_READONLY (array
));
2501 TREE_SIDE_EFFECTS (rval
)
2502 |= (CP_TYPE_VOLATILE_P (type
) | TREE_SIDE_EFFECTS (array
));
2503 TREE_THIS_VOLATILE (rval
)
2504 |= (CP_TYPE_VOLATILE_P (type
) | TREE_THIS_VOLATILE (array
));
2505 return require_complete_type (fold (rval
));
2509 tree ar
= default_conversion (array
);
2510 tree ind
= default_conversion (idx
);
2512 /* Put the integer in IND to simplify error checking. */
2513 if (TREE_CODE (TREE_TYPE (ar
)) == INTEGER_TYPE
)
2520 if (ar
== error_mark_node
)
2523 if (TREE_CODE (TREE_TYPE (ar
)) != POINTER_TYPE
)
2525 error ("subscripted value is neither array nor pointer");
2526 return error_mark_node
;
2528 if (TREE_CODE (TREE_TYPE (ind
)) != INTEGER_TYPE
)
2530 error ("array subscript is not an integer");
2531 return error_mark_node
;
2534 return build_indirect_ref (cp_build_binary_op (PLUS_EXPR
, ar
, ind
),
2539 /* Build a function call to function FUNCTION with parameters PARAMS.
2540 PARAMS is a list--a chain of TREE_LIST nodes--in which the
2541 TREE_VALUE of each node is a parameter-expression. The PARAMS do
2542 not include any object pointer that may be required. FUNCTION's
2543 data type may be a function type or a pointer-to-function.
2545 For C++: If FUNCTION's data type is a TREE_LIST, then the tree list
2546 is the list of possible methods that FUNCTION could conceivably
2547 be. If the list of methods comes from a class, then it will be
2548 a list of lists (where each element is associated with the class
2549 that produced it), otherwise it will be a simple list (for
2550 functions overloaded in global scope).
2552 In the first case, TREE_VALUE (function) is the head of one of those
2553 lists, and TREE_PURPOSE is the name of the function.
2555 In the second case, TREE_PURPOSE (function) is the function's
2558 DECL is the class instance variable, usually CURRENT_CLASS_REF.
2560 When calling a TEMPLATE_DECL, we don't require a complete return
2564 build_x_function_call (function
, params
, decl
)
2565 tree function
, params
, decl
;
2568 tree template_id
= NULL_TREE
;
2571 if (function
== error_mark_node
)
2572 return error_mark_node
;
2574 if (processing_template_decl
)
2575 return build_min_nt (CALL_EXPR
, function
, params
, NULL_TREE
);
2577 /* Save explicit template arguments if found */
2578 if (TREE_CODE (function
) == TEMPLATE_ID_EXPR
)
2580 template_id
= function
;
2581 function
= TREE_OPERAND (function
, 0);
2584 type
= TREE_TYPE (function
);
2586 if (TREE_CODE (type
) == OFFSET_TYPE
2587 && TREE_TYPE (type
) == unknown_type_node
2588 && TREE_CODE (function
) == TREE_LIST
2589 && TREE_CHAIN (function
) == NULL_TREE
)
2591 /* Undo (Foo:bar)()... */
2592 type
= TYPE_OFFSET_BASETYPE (type
);
2593 function
= TREE_VALUE (function
);
2594 my_friendly_assert (TREE_CODE (function
) == TREE_LIST
, 999);
2595 my_friendly_assert (TREE_CHAIN (function
) == NULL_TREE
, 999);
2596 function
= TREE_VALUE (function
);
2597 if (TREE_CODE (function
) == OVERLOAD
)
2598 function
= OVL_FUNCTION (function
);
2599 my_friendly_assert (TREE_CODE (function
) == FUNCTION_DECL
, 999);
2600 function
= DECL_NAME (function
);
2601 return build_method_call (decl
, function
, params
,
2602 TYPE_BINFO (type
), LOOKUP_NORMAL
);
2605 if (TREE_CODE (function
) == OFFSET_REF
2606 && TREE_CODE (type
) != METHOD_TYPE
)
2607 function
= resolve_offset_ref (function
);
2609 if ((TREE_CODE (function
) == FUNCTION_DECL
2610 && DECL_STATIC_FUNCTION_P (function
))
2611 || (DECL_FUNCTION_TEMPLATE_P (function
)
2612 && DECL_STATIC_FUNCTION_P (DECL_TEMPLATE_RESULT (function
))))
2613 return build_member_call (DECL_CONTEXT (function
),
2615 ? template_id
: DECL_NAME (function
),
2618 is_method
= ((TREE_CODE (function
) == TREE_LIST
2619 && current_class_type
!= NULL_TREE
2620 && (IDENTIFIER_CLASS_VALUE (TREE_PURPOSE (function
))
2622 || (TREE_CODE (function
) == OVERLOAD
2623 && DECL_FUNCTION_MEMBER_P (OVL_CURRENT (function
)))
2624 || TREE_CODE (function
) == IDENTIFIER_NODE
2625 || TREE_CODE (type
) == METHOD_TYPE
2626 || TYPE_PTRMEMFUNC_P (type
));
2628 /* A friend template. Make it look like a toplevel declaration. */
2629 if (! is_method
&& TREE_CODE (function
) == TEMPLATE_DECL
)
2630 function
= ovl_cons (function
, NULL_TREE
);
2632 /* Handle methods, friends, and overloaded functions, respectively. */
2635 tree basetype
= NULL_TREE
;
2637 if (TREE_CODE (function
) == OVERLOAD
)
2638 function
= OVL_CURRENT (function
);
2640 if (TREE_CODE (function
) == FUNCTION_DECL
2641 || DECL_FUNCTION_TEMPLATE_P (function
))
2643 basetype
= DECL_CONTEXT (function
);
2645 if (DECL_NAME (function
))
2646 function
= DECL_NAME (function
);
2648 function
= TYPE_IDENTIFIER (DECL_CONTEXT (function
));
2650 else if (TREE_CODE (function
) == TREE_LIST
)
2652 my_friendly_assert (TREE_CODE (TREE_VALUE (function
))
2653 == FUNCTION_DECL
, 312);
2654 basetype
= DECL_CONTEXT (TREE_VALUE (function
));
2655 function
= TREE_PURPOSE (function
);
2657 else if (TREE_CODE (function
) != IDENTIFIER_NODE
)
2659 if (TREE_CODE (function
) == OFFSET_REF
)
2661 if (TREE_OPERAND (function
, 0))
2662 decl
= TREE_OPERAND (function
, 0);
2664 /* Call via a pointer to member function. */
2665 if (decl
== NULL_TREE
)
2667 error ("pointer to member function called, but not in class scope");
2668 return error_mark_node
;
2670 /* What other type of POINTER_TYPE could this be? */
2671 if (TREE_CODE (TREE_TYPE (function
)) != POINTER_TYPE
2672 && ! TYPE_PTRMEMFUNC_P (TREE_TYPE (function
))
2673 && TREE_CODE (function
) != OFFSET_REF
)
2674 function
= build (OFFSET_REF
, TREE_TYPE (type
), NULL_TREE
,
2679 /* this is an abbreviated method call.
2680 must go through here in case it is a virtual function.
2681 @@ Perhaps this could be optimized. */
2683 if (basetype
&& (! current_class_type
2684 || ! DERIVED_FROM_P (basetype
, current_class_type
)))
2685 return build_member_call (basetype
, function
, params
);
2687 if (decl
== NULL_TREE
)
2689 if (current_class_type
== NULL_TREE
)
2691 error ("object missing in call to method `%D'", function
);
2692 return error_mark_node
;
2694 /* Yow: call from a static member function. */
2695 decl
= build_dummy_object (current_class_type
);
2698 /* Put back explicit template arguments, if any. */
2700 function
= template_id
;
2701 return build_method_call (decl
, function
, params
,
2702 NULL_TREE
, LOOKUP_NORMAL
);
2704 else if (TREE_CODE (function
) == COMPONENT_REF
2705 && type
== unknown_type_node
)
2707 /* Undo what we did in build_component_ref. */
2708 decl
= TREE_OPERAND (function
, 0);
2709 function
= TREE_OPERAND (function
, 1);
2710 function
= DECL_NAME (OVL_CURRENT (function
));
2714 TREE_OPERAND (template_id
, 0) = function
;
2715 function
= template_id
;
2718 return build_method_call (decl
, function
, params
,
2719 NULL_TREE
, LOOKUP_NORMAL
);
2721 else if (really_overloaded_fn (function
))
2723 if (OVL_FUNCTION (function
) == NULL_TREE
)
2725 error ("function `%D' declared overloaded, but no definitions appear with which to resolve it?!?",
2726 TREE_PURPOSE (function
));
2727 return error_mark_node
;
2731 /* Put back explicit template arguments, if any. */
2733 function
= template_id
;
2734 return build_new_function_call (function
, params
);
2738 /* Remove a potential OVERLOAD around it */
2739 function
= OVL_CURRENT (function
);
2742 if (TREE_CODE (function
) == OFFSET_REF
)
2744 /* If the component is a data element (or a virtual function), we play
2745 games here to make things work. */
2748 if (TREE_OPERAND (function
, 0))
2749 decl
= TREE_OPERAND (function
, 0);
2751 decl
= current_class_ref
;
2753 decl_addr
= build_unary_op (ADDR_EXPR
, decl
, 0);
2755 /* Sigh. OFFSET_REFs are being used for too many things.
2756 They're being used both for -> and ->*, and we want to resolve
2757 the -> cases here, but leave the ->*. We could use
2758 resolve_offset_ref for those, too, but it would call
2759 get_member_function_from_ptrfunc and decl_addr wouldn't get
2760 updated properly. Nasty. */
2761 if (TREE_CODE (TREE_OPERAND (function
, 1)) == FIELD_DECL
)
2762 function
= resolve_offset_ref (function
);
2764 function
= TREE_OPERAND (function
, 1);
2766 function
= get_member_function_from_ptrfunc (&decl_addr
, function
);
2767 params
= tree_cons (NULL_TREE
, decl_addr
, params
);
2768 return build_function_call (function
, params
);
2771 type
= TREE_TYPE (function
);
2772 if (type
!= error_mark_node
)
2774 if (TREE_CODE (type
) == REFERENCE_TYPE
)
2775 type
= TREE_TYPE (type
);
2777 if (IS_AGGR_TYPE (type
))
2778 return build_opfncall (CALL_EXPR
, LOOKUP_NORMAL
, function
, params
, NULL_TREE
);
2783 tree fntype
= TREE_TYPE (function
);
2784 tree ctypeptr
= NULL_TREE
;
2786 /* Explicitly named method? */
2787 if (TREE_CODE (function
) == FUNCTION_DECL
)
2788 ctypeptr
= build_pointer_type (DECL_CLASS_CONTEXT (function
));
2789 /* Expression with ptr-to-method type? It could either be a plain
2790 usage, or it might be a case where the ptr-to-method is being
2791 passed in as an argument. */
2792 else if (TYPE_PTRMEMFUNC_P (fntype
))
2794 tree rec
= TYPE_METHOD_BASETYPE (TREE_TYPE
2795 (TYPE_PTRMEMFUNC_FN_TYPE (fntype
)));
2796 ctypeptr
= build_pointer_type (rec
);
2798 /* Unexpected node type? */
2800 my_friendly_abort (116);
2801 if (decl
== NULL_TREE
)
2803 if (current_function_decl
2804 && DECL_STATIC_FUNCTION_P (current_function_decl
))
2805 error ("invalid call to member function needing `this' in static member function scope");
2807 error ("pointer to member function called, but not in class scope");
2808 return error_mark_node
;
2810 if (TREE_CODE (TREE_TYPE (decl
)) != POINTER_TYPE
2811 && ! TYPE_PTRMEMFUNC_P (TREE_TYPE (decl
)))
2813 tree binfo
= lookup_base (TREE_TYPE (decl
), TREE_TYPE (ctypeptr
),
2816 decl
= build_unary_op (ADDR_EXPR
, decl
, 0);
2817 decl
= build_base_path (PLUS_EXPR
, decl
, binfo
, 1);
2820 decl
= build_c_cast (ctypeptr
, decl
);
2821 params
= tree_cons (NULL_TREE
, decl
, params
);
2824 return build_function_call (function
, params
);
2827 /* Resolve a pointer to member function. INSTANCE is the object
2828 instance to use, if the member points to a virtual member. */
2831 get_member_function_from_ptrfunc (instance_ptrptr
, function
)
2832 tree
*instance_ptrptr
;
2835 if (TREE_CODE (function
) == OFFSET_REF
)
2836 function
= TREE_OPERAND (function
, 1);
2838 if (TYPE_PTRMEMFUNC_P (TREE_TYPE (function
)))
2840 tree fntype
, idx
, e1
, delta
, delta2
, e2
, e3
, vtbl
;
2841 tree instance
, basetype
;
2843 tree instance_ptr
= *instance_ptrptr
;
2845 if (instance_ptr
== error_mark_node
2846 && TREE_CODE (function
) == PTRMEM_CST
)
2848 /* Extracting the function address from a pmf is only
2849 allowed with -Wno-pmf-conversions. It only works for
2851 e1
= build_addr_func (PTRMEM_CST_MEMBER (function
));
2852 e1
= convert (TYPE_PTRMEMFUNC_FN_TYPE (TREE_TYPE (function
)), e1
);
2856 if (TREE_SIDE_EFFECTS (instance_ptr
))
2857 instance_ptr
= save_expr (instance_ptr
);
2859 if (TREE_SIDE_EFFECTS (function
))
2860 function
= save_expr (function
);
2862 fntype
= TYPE_PTRMEMFUNC_FN_TYPE (TREE_TYPE (function
));
2863 basetype
= TYPE_METHOD_BASETYPE (TREE_TYPE (fntype
));
2865 /* Convert down to the right base, before using the instance. */
2866 instance
= lookup_base (TREE_TYPE (TREE_TYPE (instance_ptr
)), basetype
,
2868 instance
= build_base_path (PLUS_EXPR
, instance_ptr
, instance
, 1);
2869 if (instance
== error_mark_node
&& instance_ptr
!= error_mark_node
)
2872 e3
= PFN_FROM_PTRMEMFUNC (function
);
2874 vtbl
= build1 (NOP_EXPR
, build_pointer_type (ptr_type_node
), instance
);
2875 TREE_CONSTANT (vtbl
) = TREE_CONSTANT (instance
);
2877 delta
= cp_convert (ptrdiff_type_node
,
2878 build_component_ref (function
, delta_identifier
,
2881 /* This used to avoid checking for virtual functions if basetype
2882 has no virtual functions, according to an earlier ANSI draft.
2883 With the final ISO C++ rules, such an optimization is
2884 incorrect: A pointer to a derived member can be static_cast
2885 to pointer-to-base-member, as long as the dynamic object
2886 later has the right member. */
2888 /* Promoting idx before saving it improves performance on RISC
2889 targets. Without promoting, the first compare used
2890 load-with-sign-extend, while the second used normal load then
2891 shift to sign-extend. An optimizer flaw, perhaps, but it's
2892 easier to make this change. */
2893 switch (TARGET_PTRMEMFUNC_VBIT_LOCATION
)
2895 case ptrmemfunc_vbit_in_pfn
:
2896 idx
= cp_build_binary_op (TRUNC_DIV_EXPR
,
2897 build1 (NOP_EXPR
, vtable_index_type
, e3
),
2898 TYPE_SIZE_UNIT (vtable_entry_type
));
2899 e1
= cp_build_binary_op (BIT_AND_EXPR
,
2900 build1 (NOP_EXPR
, vtable_index_type
, e3
),
2904 case ptrmemfunc_vbit_in_delta
:
2905 idx
= build1 (NOP_EXPR
, vtable_index_type
, e3
);
2906 e1
= cp_build_binary_op (BIT_AND_EXPR
,
2907 delta
, integer_one_node
);
2908 delta
= cp_build_binary_op (RSHIFT_EXPR
,
2909 build1 (NOP_EXPR
, vtable_index_type
,
2918 /* DELTA2 is the amount by which to adjust the `this' pointer
2919 to find the vtbl. */
2923 build_pointer_type (build_pointer_type (vtable_entry_type
)),
2924 vtbl
, cp_convert (ptrdiff_type_node
, delta2
));
2925 vtbl
= build_indirect_ref (vtbl
, NULL
);
2926 e2
= build_array_ref (vtbl
, idx
);
2928 /* When using function descriptors, the address of the
2929 vtable entry is treated as a function pointer. */
2930 if (TARGET_VTABLE_USES_DESCRIPTORS
)
2931 e2
= build1 (NOP_EXPR
, TREE_TYPE (e2
),
2932 build_unary_op (ADDR_EXPR
, e2
, /*noconvert=*/1));
2934 TREE_TYPE (e2
) = TREE_TYPE (e3
);
2935 e1
= build_conditional_expr (e1
, e2
, e3
);
2937 /* Make sure this doesn't get evaluated first inside one of the
2938 branches of the COND_EXPR. */
2939 if (TREE_CODE (instance_ptr
) == SAVE_EXPR
)
2940 e1
= build (COMPOUND_EXPR
, TREE_TYPE (e1
),
2943 *instance_ptrptr
= build (PLUS_EXPR
, TREE_TYPE (instance_ptr
),
2944 instance_ptr
, delta
);
2946 if (instance_ptr
== error_mark_node
2947 && TREE_CODE (e1
) != ADDR_EXPR
2948 && TREE_CODE (TREE_OPERAND (e1
, 0)) != FUNCTION_DECL
)
2949 error ("object missing in `%E'", function
);
2957 build_function_call_real (function
, params
, require_complete
, flags
)
2958 tree function
, params
;
2959 int require_complete
, flags
;
2961 register tree fntype
, fndecl
;
2962 register tree value_type
;
2963 register tree coerced_params
;
2965 tree name
= NULL_TREE
, assembler_name
= NULL_TREE
;
2968 /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
2969 Strip such NOP_EXPRs, since FUNCTION is used in non-lvalue context. */
2970 if (TREE_CODE (function
) == NOP_EXPR
2971 && TREE_TYPE (function
) == TREE_TYPE (TREE_OPERAND (function
, 0)))
2972 function
= TREE_OPERAND (function
, 0);
2974 if (TREE_CODE (function
) == FUNCTION_DECL
)
2976 name
= DECL_NAME (function
);
2977 assembler_name
= DECL_ASSEMBLER_NAME (function
);
2979 GNU_xref_call (current_function_decl
,
2980 IDENTIFIER_POINTER (name
? name
2981 : TYPE_IDENTIFIER (DECL_CLASS_CONTEXT
2983 mark_used (function
);
2986 /* Convert anything with function type to a pointer-to-function. */
2987 if (pedantic
&& DECL_MAIN_P (function
))
2988 pedwarn ("ISO C++ forbids calling `::main' from within program");
2990 /* Differs from default_conversion by not setting TREE_ADDRESSABLE
2991 (because calling an inline function does not mean the function
2992 needs to be separately compiled). */
2994 if (DECL_INLINE (function
))
2995 function
= inline_conversion (function
);
2997 function
= build_addr_func (function
);
3003 function
= build_addr_func (function
);
3006 if (function
== error_mark_node
)
3007 return error_mark_node
;
3009 fntype
= TREE_TYPE (function
);
3011 if (TYPE_PTRMEMFUNC_P (fntype
))
3013 error ("must use .* or ->* to call pointer-to-member function in `%E (...)'",
3015 return error_mark_node
;
3018 is_method
= (TREE_CODE (fntype
) == POINTER_TYPE
3019 && TREE_CODE (TREE_TYPE (fntype
)) == METHOD_TYPE
);
3021 if (!((TREE_CODE (fntype
) == POINTER_TYPE
3022 && TREE_CODE (TREE_TYPE (fntype
)) == FUNCTION_TYPE
)
3024 || TREE_CODE (function
) == TEMPLATE_ID_EXPR
))
3026 error ("`%E' cannot be used as a function", function
);
3027 return error_mark_node
;
3030 /* fntype now gets the type of function pointed to. */
3031 fntype
= TREE_TYPE (fntype
);
3033 /* Convert the parameters to the types declared in the
3034 function prototype, or apply default promotions. */
3036 if (flags
& LOOKUP_COMPLAIN
)
3037 coerced_params
= convert_arguments (TYPE_ARG_TYPES (fntype
),
3038 params
, fndecl
, LOOKUP_NORMAL
);
3040 coerced_params
= convert_arguments (TYPE_ARG_TYPES (fntype
),
3043 if (coerced_params
== error_mark_node
)
3045 if (flags
& LOOKUP_SPECULATIVELY
)
3048 return error_mark_node
;
3051 /* Check for errors in format strings. */
3054 check_function_format (NULL
, TYPE_ATTRIBUTES (fntype
), coerced_params
);
3056 /* Recognize certain built-in functions so we can make tree-codes
3057 other than CALL_EXPR. We do this when it enables fold-const.c
3058 to do something useful. */
3060 if (TREE_CODE (function
) == ADDR_EXPR
3061 && TREE_CODE (TREE_OPERAND (function
, 0)) == FUNCTION_DECL
3062 && DECL_BUILT_IN (TREE_OPERAND (function
, 0)))
3064 result
= expand_tree_builtin (TREE_OPERAND (function
, 0),
3065 params
, coerced_params
);
3070 /* Some built-in function calls will be evaluated at
3071 compile-time in fold (). */
3072 result
= fold (build_call (function
, coerced_params
));
3073 value_type
= TREE_TYPE (result
);
3075 if (require_complete
)
3077 if (TREE_CODE (value_type
) == VOID_TYPE
)
3079 result
= require_complete_type (result
);
3081 if (IS_AGGR_TYPE (value_type
))
3082 result
= build_cplus_new (value_type
, result
);
3083 return convert_from_reference (result
);
3087 build_function_call (function
, params
)
3088 tree function
, params
;
3090 return build_function_call_real (function
, params
, 1, LOOKUP_NORMAL
);
3093 /* Convert the actual parameter expressions in the list VALUES
3094 to the types in the list TYPELIST.
3095 If parmdecls is exhausted, or when an element has NULL as its type,
3096 perform the default conversions.
3098 NAME is an IDENTIFIER_NODE or 0. It is used only for error messages.
3100 This is also where warnings about wrong number of args are generated.
3102 Return a list of expressions for the parameters as converted.
3104 Both VALUES and the returned value are chains of TREE_LIST nodes
3105 with the elements of the list in the TREE_VALUE slots of those nodes.
3107 In C++, unspecified trailing parameters can be filled in with their
3108 default arguments, if such were specified. Do so here. */
3111 convert_arguments (typelist
, values
, fndecl
, flags
)
3112 tree typelist
, values
, fndecl
;
3115 register tree typetail
, valtail
;
3116 register tree result
= NULL_TREE
;
3117 const char *called_thing
= 0;
3120 /* Argument passing is always copy-initialization. */
3121 flags
|= LOOKUP_ONLYCONVERTING
;
3125 if (TREE_CODE (TREE_TYPE (fndecl
)) == METHOD_TYPE
)
3127 if (DECL_NAME (fndecl
) == NULL_TREE
3128 || IDENTIFIER_HAS_TYPE_VALUE (DECL_NAME (fndecl
)))
3129 called_thing
= "constructor";
3131 called_thing
= "member function";
3134 called_thing
= "function";
3137 for (valtail
= values
, typetail
= typelist
;
3139 valtail
= TREE_CHAIN (valtail
), i
++)
3141 register tree type
= typetail
? TREE_VALUE (typetail
) : 0;
3142 register tree val
= TREE_VALUE (valtail
);
3144 if (val
== error_mark_node
)
3145 return error_mark_node
;
3147 if (type
== void_type_node
)
3151 cp_error_at ("too many arguments to %s `%+#D'", called_thing
,
3153 error ("at this point in file");
3156 error ("too many arguments to function");
3157 /* In case anybody wants to know if this argument
3160 TREE_TYPE (tree_last (result
)) = error_mark_node
;
3164 if (TREE_CODE (val
) == OFFSET_REF
)
3165 val
= resolve_offset_ref (val
);
3167 /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
3168 Strip such NOP_EXPRs, since VAL is used in non-lvalue context. */
3169 if (TREE_CODE (val
) == NOP_EXPR
3170 && TREE_TYPE (val
) == TREE_TYPE (TREE_OPERAND (val
, 0))
3171 && (type
== 0 || TREE_CODE (type
) != REFERENCE_TYPE
))
3172 val
= TREE_OPERAND (val
, 0);
3174 if (type
== 0 || TREE_CODE (type
) != REFERENCE_TYPE
)
3176 if (TREE_CODE (TREE_TYPE (val
)) == ARRAY_TYPE
3177 || TREE_CODE (TREE_TYPE (val
)) == FUNCTION_TYPE
3178 || TREE_CODE (TREE_TYPE (val
)) == METHOD_TYPE
)
3179 val
= default_conversion (val
);
3182 if (val
== error_mark_node
)
3183 return error_mark_node
;
3187 /* Formal parm type is specified by a function prototype. */
3190 if (!COMPLETE_TYPE_P (complete_type (type
)))
3192 error ("parameter type of called function is incomplete");
3197 parmval
= convert_for_initialization
3198 (NULL_TREE
, type
, val
, flags
,
3199 "argument passing", fndecl
, i
);
3200 if (PROMOTE_PROTOTYPES
3201 && INTEGRAL_TYPE_P (type
)
3202 && (TYPE_PRECISION (type
)
3203 < TYPE_PRECISION (integer_type_node
)))
3204 parmval
= default_conversion (parmval
);
3207 if (parmval
== error_mark_node
)
3208 return error_mark_node
;
3210 result
= tree_cons (NULL_TREE
, parmval
, result
);
3214 if (TREE_CODE (TREE_TYPE (val
)) == REFERENCE_TYPE
)
3215 val
= convert_from_reference (val
);
3217 if (fndecl
&& DECL_BUILT_IN (fndecl
)
3218 && DECL_FUNCTION_CODE (fndecl
) == BUILT_IN_CONSTANT_P
)
3219 /* Don't do ellipsis conversion for __built_in_constant_p
3220 as this will result in spurious warnings for non-POD
3222 val
= require_complete_type (val
);
3224 val
= convert_arg_to_ellipsis (val
);
3226 result
= tree_cons (NULL_TREE
, val
, result
);
3230 typetail
= TREE_CHAIN (typetail
);
3233 if (typetail
!= 0 && typetail
!= void_list_node
)
3235 /* See if there are default arguments that can be used */
3236 if (TREE_PURPOSE (typetail
))
3238 for (; typetail
!= void_list_node
; ++i
)
3241 = convert_default_arg (TREE_VALUE (typetail
),
3242 TREE_PURPOSE (typetail
),
3245 if (parmval
== error_mark_node
)
3246 return error_mark_node
;
3248 result
= tree_cons (0, parmval
, result
);
3249 typetail
= TREE_CHAIN (typetail
);
3250 /* ends with `...'. */
3251 if (typetail
== NULL_TREE
)
3259 cp_error_at ("too few arguments to %s `%+#D'",
3260 called_thing
, fndecl
);
3261 error ("at this point in file");
3264 error ("too few arguments to function");
3265 return error_mark_list
;
3269 return nreverse (result
);
3272 /* Build a binary-operation expression, after performing default
3273 conversions on the operands. CODE is the kind of expression to build. */
3276 build_x_binary_op (code
, arg1
, arg2
)
3277 enum tree_code code
;
3280 if (processing_template_decl
)
3281 return build_min_nt (code
, arg1
, arg2
);
3283 return build_new_op (code
, LOOKUP_NORMAL
, arg1
, arg2
, NULL_TREE
);
3286 /* Build a binary-operation expression without default conversions.
3287 CODE is the kind of expression to build.
3288 This function differs from `build' in several ways:
3289 the data type of the result is computed and recorded in it,
3290 warnings are generated if arg data types are invalid,
3291 special handling for addition and subtraction of pointers is known,
3292 and some optimization is done (operations on narrow ints
3293 are done in the narrower type when that gives the same result).
3294 Constant folding is also done before the result is returned.
3296 Note that the operands will never have enumeral types
3297 because either they have just had the default conversions performed
3298 or they have both just been converted to some other type in which
3299 the arithmetic is to be done.
3301 C++: must do special pointer arithmetic when implementing
3302 multiple inheritance, and deal with pointer to member functions. */
3305 build_binary_op (code
, orig_op0
, orig_op1
, convert_p
)
3306 enum tree_code code
;
3307 tree orig_op0
, orig_op1
;
3308 int convert_p ATTRIBUTE_UNUSED
;
3311 register enum tree_code code0
, code1
;
3314 /* Expression code to give to the expression when it is built.
3315 Normally this is CODE, which is what the caller asked for,
3316 but in some special cases we change it. */
3317 register enum tree_code resultcode
= code
;
3319 /* Data type in which the computation is to be performed.
3320 In the simplest cases this is the common type of the arguments. */
3321 register tree result_type
= NULL
;
3323 /* Nonzero means operands have already been type-converted
3324 in whatever way is necessary.
3325 Zero means they need to be converted to RESULT_TYPE. */
3328 /* Nonzero means create the expression with this type, rather than
3330 tree build_type
= 0;
3332 /* Nonzero means after finally constructing the expression
3333 convert it to this type. */
3334 tree final_type
= 0;
3336 /* Nonzero if this is an operation like MIN or MAX which can
3337 safely be computed in short if both args are promoted shorts.
3338 Also implies COMMON.
3339 -1 indicates a bitwise operation; this makes a difference
3340 in the exact conditions for when it is safe to do the operation
3341 in a narrower mode. */
3344 /* Nonzero if this is a comparison operation;
3345 if both args are promoted shorts, compare the original shorts.
3346 Also implies COMMON. */
3347 int short_compare
= 0;
3349 /* Nonzero if this is a right-shift operation, which can be computed on the
3350 original short and then promoted if the operand is a promoted short. */
3351 int short_shift
= 0;
3353 /* Nonzero means set RESULT_TYPE to the common type of the args. */
3356 /* Apply default conversions. */
3360 if (code
== TRUTH_AND_EXPR
|| code
== TRUTH_ANDIF_EXPR
3361 || code
== TRUTH_OR_EXPR
|| code
== TRUTH_ORIF_EXPR
3362 || code
== TRUTH_XOR_EXPR
)
3364 if (!really_overloaded_fn (op0
))
3365 op0
= decay_conversion (op0
);
3366 if (!really_overloaded_fn (op1
))
3367 op1
= decay_conversion (op1
);
3371 if (!really_overloaded_fn (op0
))
3372 op0
= default_conversion (op0
);
3373 if (!really_overloaded_fn (op1
))
3374 op1
= default_conversion (op1
);
3377 /* Strip NON_LVALUE_EXPRs, etc., since we aren't using as an lvalue. */
3378 STRIP_TYPE_NOPS (op0
);
3379 STRIP_TYPE_NOPS (op1
);
3381 /* DTRT if one side is an overloaded function, but complain about it. */
3382 if (type_unknown_p (op0
))
3384 tree t
= instantiate_type (TREE_TYPE (op1
), op0
, itf_none
);
3385 if (t
!= error_mark_node
)
3387 pedwarn ("assuming cast to type `%T' from overloaded function",
3392 if (type_unknown_p (op1
))
3394 tree t
= instantiate_type (TREE_TYPE (op0
), op1
, itf_none
);
3395 if (t
!= error_mark_node
)
3397 pedwarn ("assuming cast to type `%T' from overloaded function",
3403 type0
= TREE_TYPE (op0
);
3404 type1
= TREE_TYPE (op1
);
3406 /* The expression codes of the data types of the arguments tell us
3407 whether the arguments are integers, floating, pointers, etc. */
3408 code0
= TREE_CODE (type0
);
3409 code1
= TREE_CODE (type1
);
3411 /* If an error was already reported for one of the arguments,
3412 avoid reporting another error. */
3414 if (code0
== ERROR_MARK
|| code1
== ERROR_MARK
)
3415 return error_mark_node
;
3420 /* Handle the pointer + int case. */
3421 if (code0
== POINTER_TYPE
&& code1
== INTEGER_TYPE
)
3422 return pointer_int_sum (PLUS_EXPR
, op0
, op1
);
3423 else if (code1
== POINTER_TYPE
&& code0
== INTEGER_TYPE
)
3424 return pointer_int_sum (PLUS_EXPR
, op1
, op0
);
3430 /* Subtraction of two similar pointers.
3431 We must subtract them as integers, then divide by object size. */
3432 if (code0
== POINTER_TYPE
&& code1
== POINTER_TYPE
3433 && comp_target_types (type0
, type1
, 1))
3434 return pointer_diff (op0
, op1
, common_type (type0
, type1
));
3435 /* Handle pointer minus int. Just like pointer plus int. */
3436 else if (code0
== POINTER_TYPE
&& code1
== INTEGER_TYPE
)
3437 return pointer_int_sum (MINUS_EXPR
, op0
, op1
);
3446 case TRUNC_DIV_EXPR
:
3448 case FLOOR_DIV_EXPR
:
3449 case ROUND_DIV_EXPR
:
3450 case EXACT_DIV_EXPR
:
3451 if ((code0
== INTEGER_TYPE
|| code0
== REAL_TYPE
3452 || code0
== COMPLEX_TYPE
)
3453 && (code1
== INTEGER_TYPE
|| code1
== REAL_TYPE
3454 || code1
== COMPLEX_TYPE
))
3456 if (TREE_CODE (op1
) == INTEGER_CST
&& integer_zerop (op1
))
3457 warning ("division by zero in `%E / 0'", op0
);
3458 else if (TREE_CODE (op1
) == REAL_CST
&& real_zerop (op1
))
3459 warning ("division by zero in `%E / 0.'", op0
);
3461 if (!(code0
== INTEGER_TYPE
&& code1
== INTEGER_TYPE
))
3462 resultcode
= RDIV_EXPR
;
3464 /* When dividing two signed integers, we have to promote to int.
3465 unless we divide by a constant != -1. Note that default
3466 conversion will have been performed on the operands at this
3467 point, so we have to dig out the original type to find out if
3469 shorten
= ((TREE_CODE (op0
) == NOP_EXPR
3470 && TREE_UNSIGNED (TREE_TYPE (TREE_OPERAND (op0
, 0))))
3471 || (TREE_CODE (op1
) == INTEGER_CST
3472 && ! integer_all_onesp (op1
)));
3479 case BIT_ANDTC_EXPR
:
3482 if (code0
== INTEGER_TYPE
&& code1
== INTEGER_TYPE
)
3484 /* If one operand is a constant, and the other is a short type
3485 that has been converted to an int,
3486 really do the work in the short type and then convert the
3487 result to int. If we are lucky, the constant will be 0 or 1
3488 in the short type, making the entire operation go away. */
3489 if (TREE_CODE (op0
) == INTEGER_CST
3490 && TREE_CODE (op1
) == NOP_EXPR
3491 && (TYPE_PRECISION (type1
)
3492 > TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (op1
, 0))))
3493 && TREE_UNSIGNED (TREE_TYPE (TREE_OPERAND (op1
, 0))))
3495 final_type
= result_type
;
3496 op1
= TREE_OPERAND (op1
, 0);
3497 result_type
= TREE_TYPE (op1
);
3499 if (TREE_CODE (op1
) == INTEGER_CST
3500 && TREE_CODE (op0
) == NOP_EXPR
3501 && (TYPE_PRECISION (type0
)
3502 > TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (op0
, 0))))
3503 && TREE_UNSIGNED (TREE_TYPE (TREE_OPERAND (op0
, 0))))
3505 final_type
= result_type
;
3506 op0
= TREE_OPERAND (op0
, 0);
3507 result_type
= TREE_TYPE (op0
);
3511 case TRUNC_MOD_EXPR
:
3512 case FLOOR_MOD_EXPR
:
3513 if (code1
== INTEGER_TYPE
&& integer_zerop (op1
))
3514 warning ("division by zero in `%E %% 0'", op0
);
3515 else if (code1
== REAL_TYPE
&& real_zerop (op1
))
3516 warning ("division by zero in `%E %% 0.'", op0
);
3518 if (code0
== INTEGER_TYPE
&& code1
== INTEGER_TYPE
)
3520 /* Although it would be tempting to shorten always here, that loses
3521 on some targets, since the modulo instruction is undefined if the
3522 quotient can't be represented in the computation mode. We shorten
3523 only if unsigned or if dividing by something we know != -1. */
3524 shorten
= ((TREE_CODE (op0
) == NOP_EXPR
3525 && TREE_UNSIGNED (TREE_TYPE (TREE_OPERAND (op0
, 0))))
3526 || (TREE_CODE (op1
) == INTEGER_CST
3527 && ! integer_all_onesp (op1
)));
3532 case TRUTH_ANDIF_EXPR
:
3533 case TRUTH_ORIF_EXPR
:
3534 case TRUTH_AND_EXPR
:
3536 result_type
= boolean_type_node
;
3539 /* Shift operations: result has same type as first operand;
3540 always convert second operand to int.
3541 Also set SHORT_SHIFT if shifting rightward. */
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 ("right shift count is negative");
3553 if (! integer_zerop (op1
))
3555 if (compare_tree_int (op1
, TYPE_PRECISION (type0
)) >= 0)
3556 warning ("right shift count >= width of type");
3559 /* Convert the shift-count to an integer, regardless of
3560 size of value being shifted. */
3561 if (TYPE_MAIN_VARIANT (TREE_TYPE (op1
)) != integer_type_node
)
3562 op1
= cp_convert (integer_type_node
, op1
);
3563 /* Avoid converting op1 to result_type later. */
3569 if (code0
== INTEGER_TYPE
&& code1
== INTEGER_TYPE
)
3571 result_type
= type0
;
3572 if (TREE_CODE (op1
) == INTEGER_CST
)
3574 if (tree_int_cst_lt (op1
, integer_zero_node
))
3575 warning ("left shift count is negative");
3576 else if (compare_tree_int (op1
, TYPE_PRECISION (type0
)) >= 0)
3577 warning ("left shift count >= width of type");
3579 /* Convert the shift-count to an integer, regardless of
3580 size of value being shifted. */
3581 if (TYPE_MAIN_VARIANT (TREE_TYPE (op1
)) != integer_type_node
)
3582 op1
= cp_convert (integer_type_node
, op1
);
3583 /* Avoid converting op1 to result_type later. */
3590 if (code0
== INTEGER_TYPE
&& code1
== INTEGER_TYPE
)
3592 result_type
= type0
;
3593 if (TREE_CODE (op1
) == INTEGER_CST
)
3595 if (tree_int_cst_lt (op1
, integer_zero_node
))
3596 warning ("%s rotate count is negative",
3597 (code
== LROTATE_EXPR
) ? "left" : "right");
3598 else if (compare_tree_int (op1
, TYPE_PRECISION (type0
)) >= 0)
3599 warning ("%s rotate count >= width of type",
3600 (code
== LROTATE_EXPR
) ? "left" : "right");
3602 /* Convert the shift-count to an integer, regardless of
3603 size of value being shifted. */
3604 if (TYPE_MAIN_VARIANT (TREE_TYPE (op1
)) != integer_type_node
)
3605 op1
= cp_convert (integer_type_node
, op1
);
3611 if (warn_float_equal
&& (code0
== REAL_TYPE
|| code1
== REAL_TYPE
))
3612 warning ("comparing floating point with == or != is unsafe");
3614 build_type
= boolean_type_node
;
3615 if ((code0
== INTEGER_TYPE
|| code0
== REAL_TYPE
3616 || code0
== COMPLEX_TYPE
)
3617 && (code1
== INTEGER_TYPE
|| code1
== REAL_TYPE
3618 || code1
== COMPLEX_TYPE
))
3620 else if (code0
== POINTER_TYPE
&& code1
== POINTER_TYPE
)
3621 result_type
= composite_pointer_type (type0
, type1
, op0
, op1
,
3623 else if (code0
== POINTER_TYPE
&& null_ptr_cst_p (op1
))
3624 result_type
= type0
;
3625 else if (code1
== POINTER_TYPE
&& null_ptr_cst_p (op0
))
3626 result_type
= type1
;
3627 else if (code0
== POINTER_TYPE
&& code1
== INTEGER_TYPE
)
3629 result_type
= type0
;
3630 error ("ISO C++ forbids comparison between pointer and integer");
3632 else if (code0
== INTEGER_TYPE
&& code1
== POINTER_TYPE
)
3634 result_type
= type1
;
3635 error ("ISO C++ forbids comparison between pointer and integer");
3637 else if (TYPE_PTRMEMFUNC_P (type0
) && null_ptr_cst_p (op1
))
3639 op0
= build_component_ref (op0
, pfn_identifier
, NULL_TREE
, 0);
3640 op1
= cp_convert (TREE_TYPE (op0
), integer_zero_node
);
3641 result_type
= TREE_TYPE (op0
);
3643 else if (TYPE_PTRMEMFUNC_P (type1
) && null_ptr_cst_p (op0
))
3644 return cp_build_binary_op (code
, op1
, op0
);
3645 else if (TYPE_PTRMEMFUNC_P (type0
) && TYPE_PTRMEMFUNC_P (type1
)
3646 && same_type_p (type0
, type1
))
3648 /* E will be the final comparison. */
3650 /* E1 and E2 are for scratch. */
3658 if (TREE_SIDE_EFFECTS (op0
))
3659 op0
= save_expr (op0
);
3660 if (TREE_SIDE_EFFECTS (op1
))
3661 op1
= save_expr (op1
);
3666 && (!op0.pfn || op0.delta == op1.delta))
3668 The reason for the `!op0.pfn' bit is that a NULL
3669 pointer-to-member is any member with a zero PFN; the
3670 DELTA field is unspecified. */
3671 pfn0
= pfn_from_ptrmemfunc (op0
);
3672 pfn1
= pfn_from_ptrmemfunc (op1
);
3673 delta0
= build_component_ref (op0
, delta_identifier
,
3675 delta1
= build_component_ref (op1
, delta_identifier
,
3677 e1
= cp_build_binary_op (EQ_EXPR
, delta0
, delta1
);
3678 e2
= cp_build_binary_op (EQ_EXPR
,
3680 cp_convert (TREE_TYPE (pfn0
),
3681 integer_zero_node
));
3682 e1
= cp_build_binary_op (TRUTH_ORIF_EXPR
, e1
, e2
);
3683 e2
= build (EQ_EXPR
, boolean_type_node
, pfn0
, pfn1
);
3684 e
= cp_build_binary_op (TRUTH_ANDIF_EXPR
, e2
, e1
);
3685 if (code
== EQ_EXPR
)
3687 return cp_build_binary_op (EQ_EXPR
, e
, integer_zero_node
);
3689 else if ((TYPE_PTRMEMFUNC_P (type0
)
3690 && same_type_p (TYPE_PTRMEMFUNC_FN_TYPE (type0
), type1
))
3691 || (TYPE_PTRMEMFUNC_P (type1
)
3692 && same_type_p (TYPE_PTRMEMFUNC_FN_TYPE (type1
), type0
)))
3693 my_friendly_abort (20000221);
3698 if ((code0
== INTEGER_TYPE
|| code0
== REAL_TYPE
)
3699 && (code1
== INTEGER_TYPE
|| code1
== REAL_TYPE
))
3701 else if (code0
== POINTER_TYPE
&& code1
== POINTER_TYPE
)
3702 result_type
= composite_pointer_type (type0
, type1
, op0
, op1
,
3710 build_type
= boolean_type_node
;
3711 if ((code0
== INTEGER_TYPE
|| code0
== REAL_TYPE
)
3712 && (code1
== INTEGER_TYPE
|| code1
== REAL_TYPE
))
3714 else if (code0
== POINTER_TYPE
&& code1
== POINTER_TYPE
)
3715 result_type
= composite_pointer_type (type0
, type1
, op0
, op1
,
3717 else if (code0
== POINTER_TYPE
&& TREE_CODE (op1
) == INTEGER_CST
3718 && integer_zerop (op1
))
3719 result_type
= type0
;
3720 else if (code1
== POINTER_TYPE
&& TREE_CODE (op0
) == INTEGER_CST
3721 && integer_zerop (op0
))
3722 result_type
= type1
;
3723 else if (code0
== POINTER_TYPE
&& code1
== INTEGER_TYPE
)
3725 result_type
= type0
;
3726 pedwarn ("ISO C++ forbids comparison between pointer and integer");
3728 else if (code0
== INTEGER_TYPE
&& code1
== POINTER_TYPE
)
3730 result_type
= type1
;
3731 pedwarn ("ISO C++ forbids comparison between pointer and integer");
3735 case UNORDERED_EXPR
:
3742 build_type
= integer_type_node
;
3743 if (code0
!= REAL_TYPE
|| code1
!= REAL_TYPE
)
3745 error ("unordered comparison on non-floating point argument");
3746 return error_mark_node
;
3755 if ((code0
== INTEGER_TYPE
|| code0
== REAL_TYPE
|| code0
== COMPLEX_TYPE
)
3757 (code1
== INTEGER_TYPE
|| code1
== REAL_TYPE
|| code1
== COMPLEX_TYPE
))
3759 int none_complex
= (code0
!= COMPLEX_TYPE
&& code1
!= COMPLEX_TYPE
);
3761 if (shorten
|| common
|| short_compare
)
3762 result_type
= common_type (type0
, type1
);
3764 /* For certain operations (which identify themselves by shorten != 0)
3765 if both args were extended from the same smaller type,
3766 do the arithmetic in that type and then extend.
3768 shorten !=0 and !=1 indicates a bitwise operation.
3769 For them, this optimization is safe only if
3770 both args are zero-extended or both are sign-extended.
3771 Otherwise, we might change the result.
3772 Eg, (short)-1 | (unsigned short)-1 is (int)-1
3773 but calculated in (unsigned short) it would be (unsigned short)-1. */
3775 if (shorten
&& none_complex
)
3777 int unsigned0
, unsigned1
;
3778 tree arg0
= get_narrower (op0
, &unsigned0
);
3779 tree arg1
= get_narrower (op1
, &unsigned1
);
3780 /* UNS is 1 if the operation to be done is an unsigned one. */
3781 int uns
= TREE_UNSIGNED (result_type
);
3784 final_type
= result_type
;
3786 /* Handle the case that OP0 does not *contain* a conversion
3787 but it *requires* conversion to FINAL_TYPE. */
3789 if (op0
== arg0
&& TREE_TYPE (op0
) != final_type
)
3790 unsigned0
= TREE_UNSIGNED (TREE_TYPE (op0
));
3791 if (op1
== arg1
&& TREE_TYPE (op1
) != final_type
)
3792 unsigned1
= TREE_UNSIGNED (TREE_TYPE (op1
));
3794 /* Now UNSIGNED0 is 1 if ARG0 zero-extends to FINAL_TYPE. */
3796 /* For bitwise operations, signedness of nominal type
3797 does not matter. Consider only how operands were extended. */
3801 /* Note that in all three cases below we refrain from optimizing
3802 an unsigned operation on sign-extended args.
3803 That would not be valid. */
3805 /* Both args variable: if both extended in same way
3806 from same width, do it in that width.
3807 Do it unsigned if args were zero-extended. */
3808 if ((TYPE_PRECISION (TREE_TYPE (arg0
))
3809 < TYPE_PRECISION (result_type
))
3810 && (TYPE_PRECISION (TREE_TYPE (arg1
))
3811 == TYPE_PRECISION (TREE_TYPE (arg0
)))
3812 && unsigned0
== unsigned1
3813 && (unsigned0
|| !uns
))
3815 = signed_or_unsigned_type (unsigned0
,
3816 common_type (TREE_TYPE (arg0
),
3818 else if (TREE_CODE (arg0
) == INTEGER_CST
3819 && (unsigned1
|| !uns
)
3820 && (TYPE_PRECISION (TREE_TYPE (arg1
))
3821 < TYPE_PRECISION (result_type
))
3822 && (type
= signed_or_unsigned_type (unsigned1
,
3824 int_fits_type_p (arg0
, type
)))
3826 else if (TREE_CODE (arg1
) == INTEGER_CST
3827 && (unsigned0
|| !uns
)
3828 && (TYPE_PRECISION (TREE_TYPE (arg0
))
3829 < TYPE_PRECISION (result_type
))
3830 && (type
= signed_or_unsigned_type (unsigned0
,
3832 int_fits_type_p (arg1
, type
)))
3836 /* Shifts can be shortened if shifting right. */
3841 tree arg0
= get_narrower (op0
, &unsigned_arg
);
3843 final_type
= result_type
;
3845 if (arg0
== op0
&& final_type
== TREE_TYPE (op0
))
3846 unsigned_arg
= TREE_UNSIGNED (TREE_TYPE (op0
));
3848 if (TYPE_PRECISION (TREE_TYPE (arg0
)) < TYPE_PRECISION (result_type
)
3849 /* We can shorten only if the shift count is less than the
3850 number of bits in the smaller type size. */
3851 && compare_tree_int (op1
, TYPE_PRECISION (TREE_TYPE (arg0
))) < 0
3852 /* If arg is sign-extended and then unsigned-shifted,
3853 we can simulate this with a signed shift in arg's type
3854 only if the extended result is at least twice as wide
3855 as the arg. Otherwise, the shift could use up all the
3856 ones made by sign-extension and bring in zeros.
3857 We can't optimize that case at all, but in most machines
3858 it never happens because available widths are 2**N. */
3859 && (!TREE_UNSIGNED (final_type
)
3861 || (((unsigned) 2 * TYPE_PRECISION (TREE_TYPE (arg0
)))
3862 <= TYPE_PRECISION (result_type
))))
3864 /* Do an unsigned shift if the operand was zero-extended. */
3866 = signed_or_unsigned_type (unsigned_arg
,
3868 /* Convert value-to-be-shifted to that type. */
3869 if (TREE_TYPE (op0
) != result_type
)
3870 op0
= cp_convert (result_type
, op0
);
3875 /* Comparison operations are shortened too but differently.
3876 They identify themselves by setting short_compare = 1. */
3880 /* Don't write &op0, etc., because that would prevent op0
3881 from being kept in a register.
3882 Instead, make copies of the our local variables and
3883 pass the copies by reference, then copy them back afterward. */
3884 tree xop0
= op0
, xop1
= op1
, xresult_type
= result_type
;
3885 enum tree_code xresultcode
= resultcode
;
3887 = shorten_compare (&xop0
, &xop1
, &xresult_type
, &xresultcode
);
3889 return cp_convert (boolean_type_node
, val
);
3890 op0
= xop0
, op1
= xop1
;
3892 resultcode
= xresultcode
;
3895 if ((short_compare
|| code
== MIN_EXPR
|| code
== MAX_EXPR
)
3896 && warn_sign_compare
)
3898 int op0_signed
= ! TREE_UNSIGNED (TREE_TYPE (orig_op0
));
3899 int op1_signed
= ! TREE_UNSIGNED (TREE_TYPE (orig_op1
));
3901 int unsignedp0
, unsignedp1
;
3902 tree primop0
= get_narrower (op0
, &unsignedp0
);
3903 tree primop1
= get_narrower (op1
, &unsignedp1
);
3905 /* Check for comparison of different enum types. */
3906 if (TREE_CODE (TREE_TYPE (orig_op0
)) == ENUMERAL_TYPE
3907 && TREE_CODE (TREE_TYPE (orig_op1
)) == ENUMERAL_TYPE
3908 && TYPE_MAIN_VARIANT (TREE_TYPE (orig_op0
))
3909 != TYPE_MAIN_VARIANT (TREE_TYPE (orig_op1
)))
3911 warning ("comparison between types `%#T' and `%#T'",
3912 TREE_TYPE (orig_op0
), TREE_TYPE (orig_op1
));
3915 /* Give warnings for comparisons between signed and unsigned
3916 quantities that may fail. */
3917 /* Do the checking based on the original operand trees, so that
3918 casts will be considered, but default promotions won't be. */
3920 /* Do not warn if the comparison is being done in a signed type,
3921 since the signed type will only be chosen if it can represent
3922 all the values of the unsigned type. */
3923 if (! TREE_UNSIGNED (result_type
))
3925 /* Do not warn if both operands are unsigned. */
3926 else if (op0_signed
== op1_signed
)
3928 /* Do not warn if the signed quantity is an unsuffixed
3929 integer literal (or some static constant expression
3930 involving such literals or a conditional expression
3931 involving such literals) and it is non-negative. */
3932 else if ((op0_signed
&& tree_expr_nonnegative_p (orig_op0
))
3933 || (op1_signed
&& tree_expr_nonnegative_p (orig_op1
)))
3935 /* Do not warn if the comparison is an equality operation,
3936 the unsigned quantity is an integral constant and it does
3937 not use the most significant bit of result_type. */
3938 else if ((resultcode
== EQ_EXPR
|| resultcode
== NE_EXPR
)
3939 && ((op0_signed
&& TREE_CODE (orig_op1
) == INTEGER_CST
3940 && int_fits_type_p (orig_op1
,
3941 signed_type (result_type
)))
3942 || (op1_signed
&& TREE_CODE (orig_op0
) == INTEGER_CST
3943 && int_fits_type_p (orig_op0
,
3944 signed_type (result_type
)))))
3947 warning ("comparison between signed and unsigned integer expressions");
3949 /* Warn if two unsigned values are being compared in a size
3950 larger than their original size, and one (and only one) is the
3951 result of a `~' operator. This comparison will always fail.
3953 Also warn if one operand is a constant, and the constant does not
3954 have all bits set that are set in the ~ operand when it is
3957 if ((TREE_CODE (primop0
) == BIT_NOT_EXPR
)
3958 ^ (TREE_CODE (primop1
) == BIT_NOT_EXPR
))
3960 if (TREE_CODE (primop0
) == BIT_NOT_EXPR
)
3961 primop0
= get_narrower (TREE_OPERAND (op0
, 0), &unsignedp0
);
3962 if (TREE_CODE (primop1
) == BIT_NOT_EXPR
)
3963 primop1
= get_narrower (TREE_OPERAND (op1
, 0), &unsignedp1
);
3965 if (host_integerp (primop0
, 0) || host_integerp (primop1
, 0))
3968 HOST_WIDE_INT constant
, mask
;
3972 if (host_integerp (primop0
, 0))
3975 unsignedp
= unsignedp1
;
3976 constant
= tree_low_cst (primop0
, 0);
3981 unsignedp
= unsignedp0
;
3982 constant
= tree_low_cst (primop1
, 0);
3985 bits
= TYPE_PRECISION (TREE_TYPE (primop
));
3986 if (bits
< TYPE_PRECISION (result_type
)
3987 && bits
< HOST_BITS_PER_LONG
&& unsignedp
)
3989 mask
= (~ (HOST_WIDE_INT
) 0) << bits
;
3990 if ((mask
& constant
) != mask
)
3991 warning ("comparison of promoted ~unsigned with constant");
3994 else if (unsignedp0
&& unsignedp1
3995 && (TYPE_PRECISION (TREE_TYPE (primop0
))
3996 < TYPE_PRECISION (result_type
))
3997 && (TYPE_PRECISION (TREE_TYPE (primop1
))
3998 < TYPE_PRECISION (result_type
)))
3999 warning ("comparison of promoted ~unsigned with unsigned");
4004 /* At this point, RESULT_TYPE must be nonzero to avoid an error message.
4005 If CONVERTED is zero, both args will be converted to type RESULT_TYPE.
4006 Then the expression will be built.
4007 It will be given type FINAL_TYPE if that is nonzero;
4008 otherwise, it will be given type RESULT_TYPE. */
4012 error ("invalid operands of types `%T' and `%T' to binary `%O'",
4013 TREE_TYPE (orig_op0
), TREE_TYPE (orig_op1
), code
);
4014 return error_mark_node
;
4017 /* Issue warnings about peculiar, but legal, uses of NULL. */
4018 if (/* It's reasonable to use pointer values as operands of &&
4019 and ||, so NULL is no exception. */
4020 !(code
== TRUTH_ANDIF_EXPR
|| code
== TRUTH_ORIF_EXPR
)
4021 && (/* If OP0 is NULL and OP1 is not a pointer, or vice versa. */
4022 (orig_op0
== null_node
4023 && TREE_CODE (TREE_TYPE (op1
)) != POINTER_TYPE
)
4024 /* Or vice versa. */
4025 || (orig_op1
== null_node
4026 && TREE_CODE (TREE_TYPE (op0
)) != POINTER_TYPE
)
4027 /* Or, both are NULL and the operation was not a comparison. */
4028 || (orig_op0
== null_node
&& orig_op1
== null_node
4029 && code
!= EQ_EXPR
&& code
!= NE_EXPR
)))
4030 /* Some sort of arithmetic operation involving NULL was
4031 performed. Note that pointer-difference and pointer-addition
4032 have already been handled above, and so we don't end up here in
4034 warning ("NULL used in arithmetic");
4038 if (TREE_TYPE (op0
) != result_type
)
4039 op0
= cp_convert (result_type
, op0
);
4040 if (TREE_TYPE (op1
) != result_type
)
4041 op1
= cp_convert (result_type
, op1
);
4043 if (op0
== error_mark_node
|| op1
== error_mark_node
)
4044 return error_mark_node
;
4047 if (build_type
== NULL_TREE
)
4048 build_type
= result_type
;
4051 register tree result
= build (resultcode
, build_type
, op0
, op1
);
4052 register tree folded
;
4054 folded
= fold (result
);
4055 if (folded
== result
)
4056 TREE_CONSTANT (folded
) = TREE_CONSTANT (op0
) & TREE_CONSTANT (op1
);
4057 if (final_type
!= 0)
4058 return cp_convert (final_type
, folded
);
4063 /* Return a tree for the sum or difference (RESULTCODE says which)
4064 of pointer PTROP and integer INTOP. */
4067 pointer_int_sum (resultcode
, ptrop
, intop
)
4068 enum tree_code resultcode
;
4069 register tree ptrop
, intop
;
4073 register tree result
;
4074 register tree folded
= fold (intop
);
4076 /* The result is a pointer of the same type that is being added. */
4078 register tree result_type
= TREE_TYPE (ptrop
);
4080 if (!complete_type_or_else (result_type
, ptrop
))
4081 return error_mark_node
;
4083 if (TREE_CODE (TREE_TYPE (result_type
)) == VOID_TYPE
)
4085 if (pedantic
|| warn_pointer_arith
)
4086 pedwarn ("ISO C++ forbids using pointer of type `void *' in pointer arithmetic");
4087 size_exp
= integer_one_node
;
4089 else if (TREE_CODE (TREE_TYPE (result_type
)) == FUNCTION_TYPE
)
4091 if (pedantic
|| warn_pointer_arith
)
4092 pedwarn ("ISO C++ forbids using a pointer-to-function in pointer arithmetic");
4093 size_exp
= integer_one_node
;
4095 else if (TREE_CODE (TREE_TYPE (result_type
)) == METHOD_TYPE
)
4097 if (pedantic
|| warn_pointer_arith
)
4098 pedwarn ("ISO C++ forbids using a pointer to member function in pointer arithmetic");
4099 size_exp
= integer_one_node
;
4101 else if (TREE_CODE (TREE_TYPE (result_type
)) == OFFSET_TYPE
)
4103 if (pedantic
|| warn_pointer_arith
)
4104 pedwarn ("ISO C++ forbids using pointer to a member in pointer arithmetic");
4105 size_exp
= integer_one_node
;
4108 size_exp
= size_in_bytes (complete_type (TREE_TYPE (result_type
)));
4110 /* Needed to make OOPS V2R3 work. */
4112 if (integer_zerop (intop
))
4115 /* If what we are about to multiply by the size of the elements
4116 contains a constant term, apply distributive law
4117 and multiply that constant term separately.
4118 This helps produce common subexpressions. */
4120 if ((TREE_CODE (intop
) == PLUS_EXPR
|| TREE_CODE (intop
) == MINUS_EXPR
)
4121 && ! TREE_CONSTANT (intop
)
4122 && TREE_CONSTANT (TREE_OPERAND (intop
, 1))
4123 && TREE_CONSTANT (size_exp
))
4125 enum tree_code subcode
= resultcode
;
4126 if (TREE_CODE (intop
) == MINUS_EXPR
)
4127 subcode
= (subcode
== PLUS_EXPR
? MINUS_EXPR
: PLUS_EXPR
);
4128 ptrop
= cp_build_binary_op (subcode
, ptrop
, TREE_OPERAND (intop
, 1));
4129 intop
= TREE_OPERAND (intop
, 0);
4132 /* Convert the integer argument to a type the same size as sizetype
4133 so the multiply won't overflow spuriously. */
4135 if (TYPE_PRECISION (TREE_TYPE (intop
)) != TYPE_PRECISION (sizetype
))
4136 intop
= cp_convert (type_for_size (TYPE_PRECISION (sizetype
), 0), intop
);
4138 /* Replace the integer argument with a suitable product by the object size.
4139 Do this multiplication as signed, then convert to the appropriate
4140 pointer type (actually unsigned integral). */
4142 intop
= cp_convert (result_type
,
4143 cp_build_binary_op (MULT_EXPR
, intop
,
4144 cp_convert (TREE_TYPE (intop
),
4147 /* Create the sum or difference. */
4149 result
= build (resultcode
, result_type
, ptrop
, intop
);
4151 folded
= fold (result
);
4152 if (folded
== result
)
4153 TREE_CONSTANT (folded
) = TREE_CONSTANT (ptrop
) & TREE_CONSTANT (intop
);
4157 /* Return a tree for the difference of pointers OP0 and OP1.
4158 The resulting tree has type int. */
4161 pointer_diff (op0
, op1
, ptrtype
)
4162 register tree op0
, op1
;
4163 register tree ptrtype
;
4165 register tree result
, folded
;
4166 tree restype
= ptrdiff_type_node
;
4167 tree target_type
= TREE_TYPE (ptrtype
);
4169 if (!complete_type_or_else (target_type
, NULL_TREE
))
4170 return error_mark_node
;
4172 if (pedantic
|| warn_pointer_arith
)
4174 if (TREE_CODE (target_type
) == VOID_TYPE
)
4175 pedwarn ("ISO C++ forbids using pointer of type `void *' in subtraction");
4176 if (TREE_CODE (target_type
) == FUNCTION_TYPE
)
4177 pedwarn ("ISO C++ forbids using pointer to a function in subtraction");
4178 if (TREE_CODE (target_type
) == METHOD_TYPE
)
4179 pedwarn ("ISO C++ forbids using pointer to a method in subtraction");
4180 if (TREE_CODE (target_type
) == OFFSET_TYPE
)
4181 pedwarn ("ISO C++ forbids using pointer to a member in subtraction");
4184 /* First do the subtraction as integers;
4185 then drop through to build the divide operator. */
4187 op0
= cp_build_binary_op (MINUS_EXPR
,
4188 cp_convert (restype
, op0
),
4189 cp_convert (restype
, op1
));
4191 /* This generates an error if op1 is a pointer to an incomplete type. */
4192 if (!COMPLETE_TYPE_P (TREE_TYPE (TREE_TYPE (op1
))))
4193 error ("invalid use of a pointer to an incomplete type in pointer arithmetic");
4195 op1
= ((TREE_CODE (target_type
) == VOID_TYPE
4196 || TREE_CODE (target_type
) == FUNCTION_TYPE
4197 || TREE_CODE (target_type
) == METHOD_TYPE
4198 || TREE_CODE (target_type
) == OFFSET_TYPE
)
4200 : size_in_bytes (target_type
));
4202 /* Do the division. */
4204 result
= build (EXACT_DIV_EXPR
, restype
, op0
, cp_convert (restype
, op1
));
4206 folded
= fold (result
);
4207 if (folded
== result
)
4208 TREE_CONSTANT (folded
) = TREE_CONSTANT (op0
) & TREE_CONSTANT (op1
);
4212 /* Handle the case of taking the address of a COMPONENT_REF.
4213 Called by `build_unary_op'.
4215 ARG is the COMPONENT_REF whose address we want.
4216 ARGTYPE is the pointer type that this address should have. */
4219 build_component_addr (arg
, argtype
)
4222 tree field
= TREE_OPERAND (arg
, 1);
4223 tree basetype
= decl_type_context (field
);
4224 tree rval
= build_unary_op (ADDR_EXPR
, TREE_OPERAND (arg
, 0), 0);
4226 my_friendly_assert (TREE_CODE (field
) == FIELD_DECL
, 981018);
4228 if (DECL_C_BIT_FIELD (field
))
4230 error ("attempt to take address of bit-field structure member `%D'",
4232 return error_mark_node
;
4235 if (TREE_CODE (field
) == FIELD_DECL
4236 && TYPE_BASE_CONVS_MAY_REQUIRE_CODE_P (basetype
))
4238 /* Can't convert directly to ARGTYPE, since that
4239 may have the same pointer type as one of our
4241 tree binfo
= lookup_base (TREE_TYPE (TREE_TYPE (rval
)), basetype
,
4244 rval
= build_base_path (PLUS_EXPR
, rval
, binfo
, 1);
4245 rval
= build1 (NOP_EXPR
, argtype
, rval
);
4246 TREE_CONSTANT (rval
) = TREE_CONSTANT (TREE_OPERAND (rval
, 0));
4249 /* This conversion is harmless. */
4250 rval
= convert_force (argtype
, rval
, 0);
4252 return fold (build (PLUS_EXPR
, argtype
, rval
,
4253 cp_convert (argtype
, byte_position (field
))));
4256 /* Construct and perhaps optimize a tree representation
4257 for a unary operation. CODE, a tree_code, specifies the operation
4258 and XARG is the operand. */
4261 build_x_unary_op (code
, xarg
)
4262 enum tree_code code
;
4268 if (processing_template_decl
)
4269 return build_min_nt (code
, xarg
, NULL_TREE
);
4271 /* & rec, on incomplete RECORD_TYPEs is the simple opr &, not an
4273 if (code
== ADDR_EXPR
4274 && TREE_CODE (xarg
) != TEMPLATE_ID_EXPR
4275 && ((IS_AGGR_TYPE_CODE (TREE_CODE (TREE_TYPE (xarg
)))
4276 && !COMPLETE_TYPE_P (TREE_TYPE (xarg
)))
4277 || (TREE_CODE (xarg
) == OFFSET_REF
)))
4278 /* don't look for a function */;
4283 rval
= build_new_op (code
, LOOKUP_NORMAL
, xarg
,
4284 NULL_TREE
, NULL_TREE
);
4285 if (rval
|| code
!= ADDR_EXPR
)
4288 if (code
== ADDR_EXPR
)
4290 if (TREE_CODE (xarg
) == OFFSET_REF
)
4292 ptrmem
= PTRMEM_OK_P (xarg
);
4294 if (!ptrmem
&& !flag_ms_extensions
4295 && TREE_CODE (TREE_TYPE (TREE_OPERAND (xarg
, 1))) == METHOD_TYPE
)
4296 /* A single non-static member, make sure we don't allow a
4297 pointer-to-member. */
4298 xarg
= ovl_cons (TREE_OPERAND (xarg
, 1), NULL_TREE
);
4300 else if (TREE_CODE (xarg
) == TARGET_EXPR
)
4301 warning ("taking address of temporary");
4303 exp
= build_unary_op (code
, xarg
, 0);
4304 if (TREE_CODE (exp
) == ADDR_EXPR
)
4305 PTRMEM_OK_P (exp
) = ptrmem
;
4310 /* Like truthvalue_conversion, but handle pointer-to-member constants, where
4311 a null value is represented by an INTEGER_CST of -1. */
4314 cp_truthvalue_conversion (expr
)
4317 tree type
= TREE_TYPE (expr
);
4318 if (TYPE_PTRMEM_P (type
))
4319 return build_binary_op (NE_EXPR
, expr
, integer_zero_node
, 1);
4321 return truthvalue_conversion (expr
);
4324 /* Just like cp_truthvalue_conversion, but we want a CLEANUP_POINT_EXPR. */
4327 condition_conversion (expr
)
4331 if (processing_template_decl
)
4333 if (TREE_CODE (expr
) == OFFSET_REF
)
4334 expr
= resolve_offset_ref (expr
);
4335 t
= perform_implicit_conversion (boolean_type_node
, expr
);
4336 t
= fold (build1 (CLEANUP_POINT_EXPR
, boolean_type_node
, t
));
4340 /* C++: Must handle pointers to members.
4342 Perhaps type instantiation should be extended to handle conversion
4343 from aggregates to types we don't yet know we want? (Or are those
4344 cases typically errors which should be reported?)
4346 NOCONVERT nonzero suppresses the default promotions
4347 (such as from short to int). */
4350 build_unary_op (code
, xarg
, noconvert
)
4351 enum tree_code code
;
4355 /* No default_conversion here. It causes trouble for ADDR_EXPR. */
4356 register tree arg
= xarg
;
4357 register tree argtype
= 0;
4358 const char *errstring
= NULL
;
4361 if (arg
== error_mark_node
)
4362 return error_mark_node
;
4367 /* This is used for unary plus, because a CONVERT_EXPR
4368 is enough to prevent anybody from looking inside for
4369 associativity, but won't generate any code. */
4370 if (!(arg
= build_expr_type_conversion
4371 (WANT_ARITH
| WANT_ENUM
| WANT_POINTER
, arg
, 1)))
4372 errstring
= "wrong type argument to unary plus";
4376 arg
= default_conversion (arg
);
4377 arg
= build1 (NON_LVALUE_EXPR
, TREE_TYPE (arg
), arg
);
4378 TREE_CONSTANT (arg
) = TREE_CONSTANT (TREE_OPERAND (arg
, 0));
4383 if (!(arg
= build_expr_type_conversion (WANT_ARITH
| WANT_ENUM
, arg
, 1)))
4384 errstring
= "wrong type argument to unary minus";
4385 else if (!noconvert
)
4386 arg
= default_conversion (arg
);
4390 if (TREE_CODE (TREE_TYPE (arg
)) == COMPLEX_TYPE
)
4394 arg
= default_conversion (arg
);
4396 else if (!(arg
= build_expr_type_conversion (WANT_INT
| WANT_ENUM
,
4398 errstring
= "wrong type argument to bit-complement";
4399 else if (!noconvert
)
4400 arg
= default_conversion (arg
);
4404 if (!(arg
= build_expr_type_conversion (WANT_ARITH
| WANT_ENUM
, arg
, 1)))
4405 errstring
= "wrong type argument to abs";
4406 else if (!noconvert
)
4407 arg
= default_conversion (arg
);
4411 /* Conjugating a real value is a no-op, but allow it anyway. */
4412 if (!(arg
= build_expr_type_conversion (WANT_ARITH
| WANT_ENUM
, arg
, 1)))
4413 errstring
= "wrong type argument to conjugation";
4414 else if (!noconvert
)
4415 arg
= default_conversion (arg
);
4418 case TRUTH_NOT_EXPR
:
4419 arg
= cp_convert (boolean_type_node
, arg
);
4420 val
= invert_truthvalue (arg
);
4421 if (arg
!= error_mark_node
)
4423 errstring
= "in argument to unary !";
4430 if (TREE_CODE (arg
) == COMPLEX_CST
)
4431 return TREE_REALPART (arg
);
4432 else if (TREE_CODE (TREE_TYPE (arg
)) == COMPLEX_TYPE
)
4433 return fold (build1 (REALPART_EXPR
, TREE_TYPE (TREE_TYPE (arg
)), arg
));
4438 if (TREE_CODE (arg
) == COMPLEX_CST
)
4439 return TREE_IMAGPART (arg
);
4440 else if (TREE_CODE (TREE_TYPE (arg
)) == COMPLEX_TYPE
)
4441 return fold (build1 (IMAGPART_EXPR
, TREE_TYPE (TREE_TYPE (arg
)), arg
));
4443 return cp_convert (TREE_TYPE (arg
), integer_zero_node
);
4445 case PREINCREMENT_EXPR
:
4446 case POSTINCREMENT_EXPR
:
4447 case PREDECREMENT_EXPR
:
4448 case POSTDECREMENT_EXPR
:
4449 /* Handle complex lvalues (when permitted)
4450 by reduction to simpler cases. */
4452 val
= unary_complex_lvalue (code
, arg
);
4456 /* Increment or decrement the real part of the value,
4457 and don't change the imaginary part. */
4458 if (TREE_CODE (TREE_TYPE (arg
)) == COMPLEX_TYPE
)
4462 arg
= stabilize_reference (arg
);
4463 real
= build_unary_op (REALPART_EXPR
, arg
, 1);
4464 imag
= build_unary_op (IMAGPART_EXPR
, arg
, 1);
4465 return build (COMPLEX_EXPR
, TREE_TYPE (arg
),
4466 build_unary_op (code
, real
, 1), imag
);
4469 /* Report invalid types. */
4471 if (!(arg
= build_expr_type_conversion (WANT_ARITH
| WANT_POINTER
,
4474 if (code
== PREINCREMENT_EXPR
)
4475 errstring
="no pre-increment operator for type";
4476 else if (code
== POSTINCREMENT_EXPR
)
4477 errstring
="no post-increment operator for type";
4478 else if (code
== PREDECREMENT_EXPR
)
4479 errstring
="no pre-decrement operator for type";
4481 errstring
="no post-decrement operator for type";
4485 /* Report something read-only. */
4487 if (CP_TYPE_CONST_P (TREE_TYPE (arg
))
4488 || TREE_READONLY (arg
))
4489 readonly_error (arg
, ((code
== PREINCREMENT_EXPR
4490 || code
== POSTINCREMENT_EXPR
)
4491 ? "increment" : "decrement"),
4496 tree result_type
= TREE_TYPE (arg
);
4498 arg
= get_unwidened (arg
, 0);
4499 argtype
= TREE_TYPE (arg
);
4501 /* ARM $5.2.5 last annotation says this should be forbidden. */
4502 if (TREE_CODE (argtype
) == ENUMERAL_TYPE
)
4503 pedwarn ("ISO C++ forbids %sing an enum",
4504 (code
== PREINCREMENT_EXPR
|| code
== POSTINCREMENT_EXPR
)
4505 ? "increment" : "decrement");
4507 /* Compute the increment. */
4509 if (TREE_CODE (argtype
) == POINTER_TYPE
)
4511 enum tree_code tmp
= TREE_CODE (TREE_TYPE (argtype
));
4512 tree type
= complete_type (TREE_TYPE (argtype
));
4514 if (!COMPLETE_OR_VOID_TYPE_P (type
))
4515 error ("cannot %s a pointer to incomplete type `%T'",
4516 ((code
== PREINCREMENT_EXPR
4517 || code
== POSTINCREMENT_EXPR
)
4518 ? "increment" : "decrement"), TREE_TYPE (argtype
));
4519 else if ((pedantic
|| warn_pointer_arith
)
4520 && (tmp
== FUNCTION_TYPE
|| tmp
== METHOD_TYPE
4521 || tmp
== VOID_TYPE
|| tmp
== OFFSET_TYPE
))
4522 pedwarn ("ISO C++ forbids %sing a pointer of type `%T'",
4523 ((code
== PREINCREMENT_EXPR
4524 || code
== POSTINCREMENT_EXPR
)
4525 ? "increment" : "decrement"), argtype
);
4526 inc
= c_sizeof_nowarn (TREE_TYPE (argtype
));
4529 inc
= integer_one_node
;
4531 inc
= cp_convert (argtype
, inc
);
4533 /* Handle incrementing a cast-expression. */
4535 switch (TREE_CODE (arg
))
4540 case FIX_TRUNC_EXPR
:
4541 case FIX_FLOOR_EXPR
:
4542 case FIX_ROUND_EXPR
:
4545 tree incremented
, modify
, value
, compound
;
4546 if (! lvalue_p (arg
) && pedantic
)
4547 pedwarn ("cast to non-reference type used as lvalue");
4548 arg
= stabilize_reference (arg
);
4549 if (code
== PREINCREMENT_EXPR
|| code
== PREDECREMENT_EXPR
)
4552 value
= save_expr (arg
);
4553 incremented
= build (((code
== PREINCREMENT_EXPR
4554 || code
== POSTINCREMENT_EXPR
)
4555 ? PLUS_EXPR
: MINUS_EXPR
),
4556 argtype
, value
, inc
);
4558 modify
= build_modify_expr (arg
, NOP_EXPR
, incremented
);
4559 compound
= build (COMPOUND_EXPR
, TREE_TYPE (arg
), modify
, value
);
4561 /* Eliminate warning about unused result of + or -. */
4562 TREE_NO_UNUSED_WARNING (compound
) = 1;
4570 /* Complain about anything else that is not a true lvalue. */
4571 if (!lvalue_or_else (arg
, ((code
== PREINCREMENT_EXPR
4572 || code
== POSTINCREMENT_EXPR
)
4573 ? "increment" : "decrement")))
4574 return error_mark_node
;
4576 /* Forbid using -- on `bool'. */
4577 if (TREE_TYPE (arg
) == boolean_type_node
)
4579 if (code
== POSTDECREMENT_EXPR
|| code
== PREDECREMENT_EXPR
)
4581 error ("invalid use of `--' on bool variable `%D'", arg
);
4582 return error_mark_node
;
4585 /* This will only work if someone can convince Kenner to accept
4586 my patch to expand_increment. (jason) */
4587 val
= build (code
, TREE_TYPE (arg
), arg
, inc
);
4589 val
= boolean_increment (code
, arg
);
4593 val
= build (code
, TREE_TYPE (arg
), arg
, inc
);
4595 TREE_SIDE_EFFECTS (val
) = 1;
4596 return cp_convert (result_type
, val
);
4600 /* Note that this operation never does default_conversion
4601 regardless of NOCONVERT. */
4603 argtype
= lvalue_type (arg
);
4604 if (TREE_CODE (argtype
) == REFERENCE_TYPE
)
4608 build_pointer_type (TREE_TYPE (argtype
)), arg
);
4609 TREE_CONSTANT (arg
) = TREE_CONSTANT (TREE_OPERAND (arg
, 0));
4612 else if (pedantic
&& DECL_MAIN_P (arg
))
4614 pedwarn ("ISO C++ forbids taking address of function `::main'");
4616 /* Let &* cancel out to simplify resulting code. */
4617 if (TREE_CODE (arg
) == INDIRECT_REF
)
4619 /* We don't need to have `current_class_ptr' wrapped in a
4620 NON_LVALUE_EXPR node. */
4621 if (arg
== current_class_ref
)
4622 return current_class_ptr
;
4624 arg
= TREE_OPERAND (arg
, 0);
4625 if (TREE_CODE (TREE_TYPE (arg
)) == REFERENCE_TYPE
)
4629 build_pointer_type (TREE_TYPE (TREE_TYPE (arg
))), arg
);
4630 TREE_CONSTANT (arg
) = TREE_CONSTANT (TREE_OPERAND (arg
, 0));
4632 else if (lvalue_p (arg
))
4633 /* Don't let this be an lvalue. */
4634 return non_lvalue (arg
);
4638 /* For &x[y], return x+y */
4639 if (TREE_CODE (arg
) == ARRAY_REF
)
4641 if (mark_addressable (TREE_OPERAND (arg
, 0)) == 0)
4642 return error_mark_node
;
4643 return cp_build_binary_op (PLUS_EXPR
, TREE_OPERAND (arg
, 0),
4644 TREE_OPERAND (arg
, 1));
4647 /* Uninstantiated types are all functions. Taking the
4648 address of a function is a no-op, so just return the
4651 if (TREE_CODE (arg
) == IDENTIFIER_NODE
4652 && IDENTIFIER_OPNAME_P (arg
))
4654 my_friendly_abort (117);
4655 /* We don't know the type yet, so just work around the problem.
4656 We know that this will resolve to an lvalue. */
4657 return build1 (ADDR_EXPR
, unknown_type_node
, arg
);
4660 if (TREE_CODE (arg
) == COMPONENT_REF
&& type_unknown_p (arg
)
4661 && OVL_NEXT (TREE_OPERAND (arg
, 1)) == NULL_TREE
)
4663 /* They're trying to take the address of a unique non-static
4664 member function. This is ill-formed (except in MS-land),
4665 but let's try to DTRT.
4666 Note: We only handle unique functions here because we don't
4667 want to complain if there's a static overload; non-unique
4668 cases will be handled by instantiate_type. But we need to
4669 handle this case here to allow casts on the resulting PMF.
4670 We could defer this in non-MS mode, but it's easier to give
4671 a useful error here. */
4673 tree base
= TREE_TYPE (TREE_OPERAND (arg
, 0));
4674 tree name
= DECL_NAME (OVL_CURRENT (TREE_OPERAND (arg
, 1)));
4676 if (! flag_ms_extensions
)
4678 if (current_class_type
4679 && TREE_OPERAND (arg
, 0) == current_class_ref
)
4680 /* An expression like &memfn. */
4681 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
);
4683 pedwarn ("ISO C++ forbids taking the address of a bound member function to form a pointer to member function. Say `&%T::%D'", base
, name
);
4685 arg
= build_offset_ref (base
, name
);
4688 if (type_unknown_p (arg
))
4689 return build1 (ADDR_EXPR
, unknown_type_node
, arg
);
4691 /* Handle complex lvalues (when permitted)
4692 by reduction to simpler cases. */
4693 val
= unary_complex_lvalue (code
, arg
);
4697 switch (TREE_CODE (arg
))
4702 case FIX_TRUNC_EXPR
:
4703 case FIX_FLOOR_EXPR
:
4704 case FIX_ROUND_EXPR
:
4706 if (! lvalue_p (arg
) && pedantic
)
4707 pedwarn ("ISO C++ forbids taking the address of a cast to a non-lvalue expression");
4714 /* Allow the address of a constructor if all the elements
4716 if (TREE_CODE (arg
) == CONSTRUCTOR
&& TREE_HAS_CONSTRUCTOR (arg
)
4717 && TREE_CONSTANT (arg
))
4719 /* Anything not already handled and not a true memory reference
4721 else if (TREE_CODE (argtype
) != FUNCTION_TYPE
4722 && TREE_CODE (argtype
) != METHOD_TYPE
4723 && !lvalue_or_else (arg
, "unary `&'"))
4724 return error_mark_node
;
4726 if (argtype
!= error_mark_node
)
4727 argtype
= build_pointer_type (argtype
);
4729 if (mark_addressable (arg
) == 0)
4730 return error_mark_node
;
4735 if (TREE_CODE (arg
) == COMPONENT_REF
)
4736 addr
= build_component_addr (arg
, argtype
);
4738 addr
= build1 (ADDR_EXPR
, argtype
, arg
);
4740 /* Address of a static or external variable or
4741 function counts as a constant */
4743 TREE_CONSTANT (addr
) = 1;
4745 if (TREE_CODE (argtype
) == POINTER_TYPE
4746 && TREE_CODE (TREE_TYPE (argtype
)) == METHOD_TYPE
)
4748 build_ptrmemfunc_type (argtype
);
4749 addr
= build_ptrmemfunc (argtype
, addr
, 0);
4762 argtype
= TREE_TYPE (arg
);
4763 return fold (build1 (code
, argtype
, arg
));
4766 error ("%s", errstring
);
4767 return error_mark_node
;
4770 /* Apply unary lvalue-demanding operator CODE to the expression ARG
4771 for certain kinds of expressions which are not really lvalues
4772 but which we can accept as lvalues.
4774 If ARG is not a kind of expression we can handle, return zero. */
4777 unary_complex_lvalue (code
, arg
)
4778 enum tree_code code
;
4781 /* Handle (a, b) used as an "lvalue". */
4782 if (TREE_CODE (arg
) == COMPOUND_EXPR
)
4784 tree real_result
= build_unary_op (code
, TREE_OPERAND (arg
, 1), 0);
4785 return build (COMPOUND_EXPR
, TREE_TYPE (real_result
),
4786 TREE_OPERAND (arg
, 0), real_result
);
4789 /* Handle (a ? b : c) used as an "lvalue". */
4790 if (TREE_CODE (arg
) == COND_EXPR
4791 || TREE_CODE (arg
) == MIN_EXPR
|| TREE_CODE (arg
) == MAX_EXPR
)
4792 return rationalize_conditional_expr (code
, arg
);
4794 /* Handle (a = b), (++a), and (--a) used as an "lvalue". */
4795 if (TREE_CODE (arg
) == MODIFY_EXPR
4796 || TREE_CODE (arg
) == PREINCREMENT_EXPR
4797 || TREE_CODE (arg
) == PREDECREMENT_EXPR
)
4799 tree lvalue
= TREE_OPERAND (arg
, 0);
4800 if (TREE_SIDE_EFFECTS (lvalue
))
4802 lvalue
= stabilize_reference (lvalue
);
4803 arg
= build (TREE_CODE (arg
), TREE_TYPE (arg
),
4804 lvalue
, TREE_OPERAND (arg
, 1));
4806 return unary_complex_lvalue
4807 (code
, build (COMPOUND_EXPR
, TREE_TYPE (lvalue
), arg
, lvalue
));
4810 if (code
!= ADDR_EXPR
)
4813 /* Handle (a = b) used as an "lvalue" for `&'. */
4814 if (TREE_CODE (arg
) == MODIFY_EXPR
4815 || TREE_CODE (arg
) == INIT_EXPR
)
4817 tree real_result
= build_unary_op (code
, TREE_OPERAND (arg
, 0), 0);
4818 arg
= build (COMPOUND_EXPR
, TREE_TYPE (real_result
), arg
, real_result
);
4819 TREE_NO_UNUSED_WARNING (arg
) = 1;
4823 if (TREE_CODE (TREE_TYPE (arg
)) == FUNCTION_TYPE
4824 || TREE_CODE (TREE_TYPE (arg
)) == METHOD_TYPE
4825 || TREE_CODE (TREE_TYPE (arg
)) == OFFSET_TYPE
)
4827 /* The representation of something of type OFFSET_TYPE
4828 is really the representation of a pointer to it.
4829 Here give the representation its true type. */
4832 my_friendly_assert (TREE_CODE (arg
) != SCOPE_REF
, 313);
4834 if (TREE_CODE (arg
) != OFFSET_REF
)
4837 t
= TREE_OPERAND (arg
, 1);
4839 /* Check all this code for right semantics. */
4840 if (TREE_CODE (t
) == FUNCTION_DECL
)
4842 if (DECL_DESTRUCTOR_P (t
))
4843 error ("taking address of destructor");
4844 return build_unary_op (ADDR_EXPR
, t
, 0);
4846 if (TREE_CODE (t
) == VAR_DECL
)
4847 return build_unary_op (ADDR_EXPR
, t
, 0);
4852 if (TREE_OPERAND (arg
, 0)
4853 && ! is_dummy_object (TREE_OPERAND (arg
, 0))
4854 && TREE_CODE (t
) != FIELD_DECL
)
4856 error ("taking address of bound pointer-to-member expression");
4857 return error_mark_node
;
4860 type
= build_offset_type (DECL_FIELD_CONTEXT (t
), TREE_TYPE (t
));
4861 type
= build_pointer_type (type
);
4863 t
= make_ptrmem_cst (type
, TREE_OPERAND (arg
, 1));
4869 /* We permit compiler to make function calls returning
4870 objects of aggregate type look like lvalues. */
4874 if (TREE_CODE (targ
) == SAVE_EXPR
)
4875 targ
= TREE_OPERAND (targ
, 0);
4877 if (TREE_CODE (targ
) == CALL_EXPR
&& IS_AGGR_TYPE (TREE_TYPE (targ
)))
4879 if (TREE_CODE (arg
) == SAVE_EXPR
)
4882 targ
= build_cplus_new (TREE_TYPE (arg
), arg
);
4883 return build1 (ADDR_EXPR
, build_pointer_type (TREE_TYPE (arg
)), targ
);
4886 if (TREE_CODE (arg
) == SAVE_EXPR
&& TREE_CODE (targ
) == INDIRECT_REF
)
4887 return build (SAVE_EXPR
, build_pointer_type (TREE_TYPE (arg
)),
4888 TREE_OPERAND (targ
, 0), current_function_decl
, NULL
);
4891 /* Don't let anything else be handled specially. */
4895 /* Mark EXP saying that we need to be able to take the
4896 address of it; it should not be allocated in a register.
4897 Value is 1 if successful.
4899 C++: we do not allow `current_class_ptr' to be addressable. */
4902 mark_addressable (exp
)
4905 register tree x
= exp
;
4907 if (TREE_ADDRESSABLE (x
) == 1)
4911 switch (TREE_CODE (x
))
4918 x
= TREE_OPERAND (x
, 0);
4922 if (x
== current_class_ptr
)
4924 error ("cannot take the address of `this', which is an rvalue expression");
4925 TREE_ADDRESSABLE (x
) = 1; /* so compiler doesn't die later */
4929 /* Caller should not be trying to mark initialized
4930 constant fields addressable. */
4931 my_friendly_assert (DECL_LANG_SPECIFIC (x
) == 0
4932 || DECL_IN_AGGR_P (x
) == 0
4934 || DECL_EXTERNAL (x
), 314);
4938 if (DECL_REGISTER (x
) && !TREE_ADDRESSABLE (x
)
4939 && !DECL_ARTIFICIAL (x
) && extra_warnings
)
4940 warning ("address requested for `%D', which is declared `register'",
4942 TREE_ADDRESSABLE (x
) = 1;
4946 TREE_ADDRESSABLE (x
) = 1;
4947 TREE_ADDRESSABLE (DECL_ASSEMBLER_NAME (x
)) = 1;
4951 TREE_ADDRESSABLE (x
) = 1;
4955 TREE_ADDRESSABLE (x
) = 1;
4956 mark_addressable (TREE_OPERAND (x
, 0));
4964 /* Build and return a conditional expression IFEXP ? OP1 : OP2. */
4967 build_x_conditional_expr (ifexp
, op1
, op2
)
4968 tree ifexp
, op1
, op2
;
4970 if (processing_template_decl
)
4971 return build_min_nt (COND_EXPR
, ifexp
, op1
, op2
);
4973 return build_conditional_expr (ifexp
, op1
, op2
);
4976 /* Handle overloading of the ',' operator when needed. Otherwise,
4977 this function just builds an expression list. */
4980 build_x_compound_expr (list
)
4983 tree rest
= TREE_CHAIN (list
);
4986 if (processing_template_decl
)
4987 return build_min_nt (COMPOUND_EXPR
, list
, NULL_TREE
);
4989 if (rest
== NULL_TREE
)
4990 return build_compound_expr (list
);
4992 result
= build_opfncall (COMPOUND_EXPR
, LOOKUP_NORMAL
,
4993 TREE_VALUE (list
), TREE_VALUE (rest
), NULL_TREE
);
4995 return build_x_compound_expr (tree_cons (NULL_TREE
, result
,
4996 TREE_CHAIN (rest
)));
4998 if (! TREE_SIDE_EFFECTS (TREE_VALUE (list
)))
5000 /* FIXME: This test should be in the implicit cast to void of the LHS. */
5001 /* the left-hand operand of a comma expression is like an expression
5002 statement: we should warn if it doesn't have any side-effects,
5003 unless it was explicitly cast to (void). */
5004 if ((extra_warnings
|| warn_unused_value
)
5005 && !(TREE_CODE (TREE_VALUE(list
)) == CONVERT_EXPR
5006 && VOID_TYPE_P (TREE_TYPE (TREE_VALUE(list
)))))
5007 warning("left-hand operand of comma expression has no effect");
5009 #if 0 /* this requires a gcc backend patch to export warn_if_unused_value */
5010 else if (warn_unused_value
)
5011 warn_if_unused_value (TREE_VALUE(list
));
5014 return build_compound_expr
5015 (tree_cons (NULL_TREE
, TREE_VALUE (list
),
5016 build_tree_list (NULL_TREE
,
5017 build_x_compound_expr (rest
))));
5020 /* Given a list of expressions, return a compound expression
5021 that performs them all and returns the value of the last of them. */
5024 build_compound_expr (list
)
5030 TREE_VALUE (list
) = decl_constant_value (TREE_VALUE (list
));
5032 if (TREE_CHAIN (list
) == 0)
5034 /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
5035 Strip such NOP_EXPRs, since LIST is used in non-lvalue context. */
5036 if (TREE_CODE (list
) == NOP_EXPR
5037 && TREE_TYPE (list
) == TREE_TYPE (TREE_OPERAND (list
, 0)))
5038 list
= TREE_OPERAND (list
, 0);
5040 return TREE_VALUE (list
);
5043 first
= TREE_VALUE (list
);
5044 first
= convert_to_void (first
, "left-hand operand of comma");
5045 if (first
== error_mark_node
)
5046 return error_mark_node
;
5048 rest
= build_compound_expr (TREE_CHAIN (list
));
5049 if (rest
== error_mark_node
)
5050 return error_mark_node
;
5052 /* When pedantic, a compound expression cannot be a constant expression. */
5053 if (! TREE_SIDE_EFFECTS (first
) && ! pedantic
)
5056 return build (COMPOUND_EXPR
, TREE_TYPE (rest
), first
, rest
);
5060 build_static_cast (type
, expr
)
5066 if (type
== error_mark_node
|| expr
== error_mark_node
)
5067 return error_mark_node
;
5069 if (TREE_CODE (expr
) == OFFSET_REF
)
5070 expr
= resolve_offset_ref (expr
);
5072 if (processing_template_decl
)
5074 tree t
= build_min (STATIC_CAST_EXPR
, type
, expr
);
5078 /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
5079 Strip such NOP_EXPRs if VALUE is being used in non-lvalue context. */
5080 if (TREE_CODE (type
) != REFERENCE_TYPE
5081 && TREE_CODE (expr
) == NOP_EXPR
5082 && TREE_TYPE (expr
) == TREE_TYPE (TREE_OPERAND (expr
, 0)))
5083 expr
= TREE_OPERAND (expr
, 0);
5085 if (TREE_CODE (type
) == VOID_TYPE
)
5087 expr
= convert_to_void (expr
, /*implicit=*/NULL
);
5091 if (TREE_CODE (type
) == REFERENCE_TYPE
)
5092 return (convert_from_reference
5093 (convert_to_reference (type
, expr
, CONV_STATIC
|CONV_IMPLICIT
,
5094 LOOKUP_COMPLAIN
, NULL_TREE
)));
5096 if (IS_AGGR_TYPE (type
))
5097 return build_cplus_new (type
, (build_method_call
5098 (NULL_TREE
, complete_ctor_identifier
,
5099 build_tree_list (NULL_TREE
, expr
),
5100 TYPE_BINFO (type
), LOOKUP_NORMAL
)));
5102 intype
= TREE_TYPE (expr
);
5104 /* FIXME handle casting to array type. */
5107 if (IS_AGGR_TYPE (intype
)
5108 ? can_convert_arg (type
, intype
, expr
)
5109 : can_convert_arg (strip_all_pointer_quals (type
),
5110 strip_all_pointer_quals (intype
), expr
))
5111 /* This is a standard conversion. */
5113 else if (TYPE_PTROB_P (type
) && TYPE_PTROB_P (intype
))
5115 /* They're pointers to objects. They must be aggregates that
5116 are related non-virtually. */
5119 if (IS_AGGR_TYPE (TREE_TYPE (type
)) && IS_AGGR_TYPE (TREE_TYPE (intype
))
5120 && lookup_base (TREE_TYPE (type
), TREE_TYPE (intype
),
5121 ba_ignore
| ba_quiet
, &kind
)
5122 && kind
!= bk_via_virtual
)
5125 else if (TREE_CODE (intype
) != BOOLEAN_TYPE
5126 && TREE_CODE (type
) != ARRAY_TYPE
5127 && TREE_CODE (type
) != FUNCTION_TYPE
5128 && can_convert (intype
, strip_all_pointer_quals (type
)))
5130 else if (TREE_CODE (intype
) == ENUMERAL_TYPE
5131 && TREE_CODE (type
) == ENUMERAL_TYPE
)
5132 /* DR 128: "A value of integral _or enumeration_ type can be explicitly
5133 converted to an enumeration type."
5134 The integral to enumeration will be accepted by the previous clause.
5135 We need to explicitly check for enumeration to enumeration. */
5138 /* [expr.static.cast]
5140 The static_cast operator shall not be used to cast away
5142 if (ok
&& casts_away_constness (intype
, type
))
5144 error ("static_cast from type `%T' to type `%T' casts away constness",
5146 return error_mark_node
;
5150 return build_c_cast (type
, expr
);
5152 error ("invalid static_cast from type `%T' to type `%T'", intype
, type
);
5153 return error_mark_node
;
5157 build_reinterpret_cast (type
, expr
)
5162 if (type
== error_mark_node
|| expr
== error_mark_node
)
5163 return error_mark_node
;
5165 if (TREE_CODE (expr
) == OFFSET_REF
)
5166 expr
= resolve_offset_ref (expr
);
5168 if (processing_template_decl
)
5170 tree t
= build_min (REINTERPRET_CAST_EXPR
, type
, expr
);
5174 if (TREE_CODE (type
) != REFERENCE_TYPE
)
5176 expr
= decay_conversion (expr
);
5178 /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
5179 Strip such NOP_EXPRs if VALUE is being used in non-lvalue context. */
5180 if (TREE_CODE (expr
) == NOP_EXPR
5181 && TREE_TYPE (expr
) == TREE_TYPE (TREE_OPERAND (expr
, 0)))
5182 expr
= TREE_OPERAND (expr
, 0);
5185 intype
= TREE_TYPE (expr
);
5187 if (TREE_CODE (type
) == REFERENCE_TYPE
)
5189 if (! real_lvalue_p (expr
))
5191 error ("invalid reinterpret_cast of an rvalue expression of type `%T' to type `%T'", intype
, type
);
5192 return error_mark_node
;
5194 expr
= build_unary_op (ADDR_EXPR
, expr
, 0);
5195 if (expr
!= error_mark_node
)
5196 expr
= build_reinterpret_cast
5197 (build_pointer_type (TREE_TYPE (type
)), expr
);
5198 if (expr
!= error_mark_node
)
5199 expr
= build_indirect_ref (expr
, 0);
5202 else if (same_type_ignoring_top_level_qualifiers_p (intype
, type
))
5203 return build_static_cast (type
, expr
);
5205 if (TYPE_PTR_P (type
) && (TREE_CODE (intype
) == INTEGER_TYPE
5206 || TREE_CODE (intype
) == ENUMERAL_TYPE
))
5208 else if (TREE_CODE (type
) == INTEGER_TYPE
&& TYPE_PTR_P (intype
))
5210 if (TYPE_PRECISION (type
) < TYPE_PRECISION (intype
))
5211 pedwarn ("reinterpret_cast from `%T' to `%T' loses precision",
5214 else if ((TYPE_PTRFN_P (type
) && TYPE_PTRFN_P (intype
))
5215 || (TYPE_PTRMEMFUNC_P (type
) && TYPE_PTRMEMFUNC_P (intype
)))
5217 expr
= decl_constant_value (expr
);
5218 return fold (build1 (NOP_EXPR
, type
, expr
));
5220 else if ((TYPE_PTRMEM_P (type
) && TYPE_PTRMEM_P (intype
))
5221 || (TYPE_PTROBV_P (type
) && TYPE_PTROBV_P (intype
)))
5223 if (! comp_ptr_ttypes_reinterpret (TREE_TYPE (type
), TREE_TYPE (intype
)))
5224 pedwarn ("reinterpret_cast from `%T' to `%T' casts away const (or volatile)",
5227 expr
= decl_constant_value (expr
);
5228 return fold (build1 (NOP_EXPR
, type
, expr
));
5230 else if ((TYPE_PTRFN_P (type
) && TYPE_PTROBV_P (intype
))
5231 || (TYPE_PTRFN_P (intype
) && TYPE_PTROBV_P (type
)))
5233 pedwarn ("ISO C++ forbids casting between pointer-to-function and pointer-to-object");
5234 expr
= decl_constant_value (expr
);
5235 return fold (build1 (NOP_EXPR
, type
, expr
));
5239 error ("invalid reinterpret_cast from type `%T' to type `%T'",
5241 return error_mark_node
;
5244 return cp_convert (type
, expr
);
5248 build_const_cast (type
, expr
)
5253 if (type
== error_mark_node
|| expr
== error_mark_node
)
5254 return error_mark_node
;
5256 if (TREE_CODE (expr
) == OFFSET_REF
)
5257 expr
= resolve_offset_ref (expr
);
5259 if (processing_template_decl
)
5261 tree t
= build_min (CONST_CAST_EXPR
, type
, expr
);
5265 if (!POINTER_TYPE_P (type
))
5266 error ("invalid use of const_cast with type `%T', which is not a pointer, reference, nor a pointer-to-data-member type", type
);
5267 else if (TREE_CODE (TREE_TYPE (type
)) == FUNCTION_TYPE
)
5269 error ("invalid use of const_cast with type `%T', which is a pointer or reference to a function type", type
);
5270 return error_mark_node
;
5273 if (TREE_CODE (type
) != REFERENCE_TYPE
)
5275 expr
= decay_conversion (expr
);
5277 /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
5278 Strip such NOP_EXPRs if VALUE is being used in non-lvalue context. */
5279 if (TREE_CODE (expr
) == NOP_EXPR
5280 && TREE_TYPE (expr
) == TREE_TYPE (TREE_OPERAND (expr
, 0)))
5281 expr
= TREE_OPERAND (expr
, 0);
5284 intype
= TREE_TYPE (expr
);
5286 if (same_type_ignoring_top_level_qualifiers_p (intype
, type
))
5287 return build_static_cast (type
, expr
);
5288 else if (TREE_CODE (type
) == REFERENCE_TYPE
)
5290 if (! real_lvalue_p (expr
))
5292 error ("invalid const_cast of an rvalue of type `%T' to type `%T'", intype
, type
);
5293 return error_mark_node
;
5296 if (comp_ptr_ttypes_const (TREE_TYPE (type
), intype
))
5298 expr
= build_unary_op (ADDR_EXPR
, expr
, 0);
5299 expr
= build1 (NOP_EXPR
, type
, expr
);
5300 return convert_from_reference (expr
);
5303 else if (TREE_CODE (type
) == POINTER_TYPE
5304 && TREE_CODE (intype
) == POINTER_TYPE
5305 && comp_ptr_ttypes_const (TREE_TYPE (type
), TREE_TYPE (intype
)))
5306 return cp_convert (type
, expr
);
5308 error ("invalid const_cast from type `%T' to type `%T'", intype
, type
);
5309 return error_mark_node
;
5312 /* Build an expression representing a cast to type TYPE of expression EXPR.
5314 ALLOW_NONCONVERTING is true if we should allow non-converting constructors
5315 when doing the cast. */
5318 build_c_cast (type
, expr
)
5321 register tree value
= expr
;
5324 if (type
== error_mark_node
|| expr
== error_mark_node
)
5325 return error_mark_node
;
5327 if (processing_template_decl
)
5329 tree t
= build_min (CAST_EXPR
, type
,
5330 tree_cons (NULL_TREE
, value
, NULL_TREE
));
5334 /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
5335 Strip such NOP_EXPRs if VALUE is being used in non-lvalue context. */
5336 if (TREE_CODE (type
) != REFERENCE_TYPE
5337 && TREE_CODE (value
) == NOP_EXPR
5338 && TREE_TYPE (value
) == TREE_TYPE (TREE_OPERAND (value
, 0)))
5339 value
= TREE_OPERAND (value
, 0);
5341 if (TREE_CODE (value
) == OFFSET_REF
)
5342 value
= resolve_offset_ref (value
);
5344 if (TREE_CODE (type
) == ARRAY_TYPE
)
5346 /* Allow casting from T1* to T2[] because Cfront allows it.
5347 NIHCL uses it. It is not valid ISO C++ however. */
5348 if (TREE_CODE (TREE_TYPE (expr
)) == POINTER_TYPE
)
5350 pedwarn ("ISO C++ forbids casting to an array type `%T'", type
);
5351 type
= build_pointer_type (TREE_TYPE (type
));
5355 error ("ISO C++ forbids casting to an array type `%T'", type
);
5356 return error_mark_node
;
5360 if (TREE_CODE (type
) == FUNCTION_TYPE
5361 || TREE_CODE (type
) == METHOD_TYPE
)
5363 error ("invalid cast to function type `%T'", type
);
5364 return error_mark_node
;
5367 if (TREE_CODE (type
) == VOID_TYPE
)
5369 /* Conversion to void does not cause any of the normal function to
5370 * pointer, array to pointer and lvalue to rvalue decays. */
5372 value
= convert_to_void (value
, /*implicit=*/NULL
);
5375 /* Convert functions and arrays to pointers and
5376 convert references to their expanded types,
5377 but don't convert any other types. If, however, we are
5378 casting to a class type, there's no reason to do this: the
5379 cast will only succeed if there is a converting constructor,
5380 and the default conversions will be done at that point. In
5381 fact, doing the default conversion here is actually harmful
5385 struct S { S(const A&); };
5387 since we don't want the array-to-pointer conversion done. */
5388 if (!IS_AGGR_TYPE (type
))
5390 if (TREE_CODE (TREE_TYPE (value
)) == FUNCTION_TYPE
5391 || (TREE_CODE (TREE_TYPE (value
)) == METHOD_TYPE
5392 /* Don't do the default conversion on a ->* expression. */
5393 && ! (TREE_CODE (type
) == POINTER_TYPE
5394 && bound_pmf_p (value
)))
5395 || TREE_CODE (TREE_TYPE (value
)) == ARRAY_TYPE
5396 || TREE_CODE (TREE_TYPE (value
)) == REFERENCE_TYPE
)
5397 value
= default_conversion (value
);
5399 else if (TREE_CODE (TREE_TYPE (value
)) == REFERENCE_TYPE
)
5400 /* However, even for class types, we still need to strip away
5401 the reference type, since the call to convert_force below
5402 does not expect the input expression to be of reference
5404 value
= convert_from_reference (value
);
5406 otype
= TREE_TYPE (value
);
5408 /* Optionally warn about potentially worrisome casts. */
5411 && TREE_CODE (type
) == POINTER_TYPE
5412 && TREE_CODE (otype
) == POINTER_TYPE
5413 && !at_least_as_qualified_p (TREE_TYPE (type
),
5415 warning ("cast from `%T' to `%T' discards qualifiers from pointer target type",
5418 if (TREE_CODE (type
) == INTEGER_TYPE
5419 && TREE_CODE (otype
) == POINTER_TYPE
5420 && TYPE_PRECISION (type
) != TYPE_PRECISION (otype
))
5421 warning ("cast from pointer to integer of different size");
5423 if (TREE_CODE (type
) == POINTER_TYPE
5424 && TREE_CODE (otype
) == INTEGER_TYPE
5425 && TYPE_PRECISION (type
) != TYPE_PRECISION (otype
)
5426 /* Don't warn about converting any constant. */
5427 && !TREE_CONSTANT (value
))
5428 warning ("cast to pointer from integer of different size");
5430 if (TREE_CODE (type
) == REFERENCE_TYPE
)
5431 value
= (convert_from_reference
5432 (convert_to_reference (type
, value
, CONV_C_CAST
,
5433 LOOKUP_COMPLAIN
, NULL_TREE
)));
5438 value
= decl_constant_value (value
);
5441 value
= convert_force (type
, value
, CONV_C_CAST
);
5443 /* Ignore any integer overflow caused by the cast. */
5444 if (TREE_CODE (value
) == INTEGER_CST
)
5446 TREE_OVERFLOW (value
) = TREE_OVERFLOW (ovalue
);
5447 TREE_CONSTANT_OVERFLOW (value
) = TREE_CONSTANT_OVERFLOW (ovalue
);
5451 /* Warn about possible alignment problems. Do this here when we will have
5452 instantiated any necessary template types. */
5453 if (STRICT_ALIGNMENT
&& warn_cast_align
5454 && TREE_CODE (type
) == POINTER_TYPE
5455 && TREE_CODE (otype
) == POINTER_TYPE
5456 && TREE_CODE (TREE_TYPE (otype
)) != VOID_TYPE
5457 && TREE_CODE (TREE_TYPE (otype
)) != FUNCTION_TYPE
5458 && COMPLETE_TYPE_P (TREE_TYPE (otype
))
5459 && COMPLETE_TYPE_P (TREE_TYPE (type
))
5460 && TYPE_ALIGN (TREE_TYPE (type
)) > TYPE_ALIGN (TREE_TYPE (otype
)))
5461 warning ("cast from `%T' to `%T' increases required alignment of target type",
5464 /* Always produce some operator for an explicit cast,
5465 so we can tell (for -pedantic) that the cast is no lvalue. */
5466 if (TREE_CODE (type
) != REFERENCE_TYPE
&& value
== expr
5467 && real_lvalue_p (value
))
5468 value
= non_lvalue (value
);
5473 /* Build an assignment expression of lvalue LHS from value RHS.
5474 MODIFYCODE is the code for a binary operator that we use
5475 to combine the old value of LHS with RHS to get the new value.
5476 Or else MODIFYCODE is NOP_EXPR meaning do a simple assignment.
5478 C++: If MODIFYCODE is INIT_EXPR, then leave references unbashed. */
5481 build_modify_expr (lhs
, modifycode
, rhs
)
5483 enum tree_code modifycode
;
5486 register tree result
;
5488 tree lhstype
= TREE_TYPE (lhs
);
5489 tree olhstype
= lhstype
;
5492 /* Avoid duplicate error messages from operands that had errors. */
5493 if (lhs
== error_mark_node
|| rhs
== error_mark_node
)
5494 return error_mark_node
;
5496 /* Types that aren't fully specified cannot be used in assignments. */
5497 lhs
= require_complete_type (lhs
);
5501 /* Handle control structure constructs used as "lvalues". */
5503 switch (TREE_CODE (lhs
))
5505 /* Handle --foo = 5; as these are valid constructs in C++ */
5506 case PREDECREMENT_EXPR
:
5507 case PREINCREMENT_EXPR
:
5508 if (TREE_SIDE_EFFECTS (TREE_OPERAND (lhs
, 0)))
5509 lhs
= build (TREE_CODE (lhs
), TREE_TYPE (lhs
),
5510 stabilize_reference (TREE_OPERAND (lhs
, 0)),
5511 TREE_OPERAND (lhs
, 1));
5512 return build (COMPOUND_EXPR
, lhstype
,
5514 build_modify_expr (TREE_OPERAND (lhs
, 0),
5517 /* Handle (a, b) used as an "lvalue". */
5519 newrhs
= build_modify_expr (TREE_OPERAND (lhs
, 1),
5521 if (newrhs
== error_mark_node
)
5522 return error_mark_node
;
5523 return build (COMPOUND_EXPR
, lhstype
,
5524 TREE_OPERAND (lhs
, 0), newrhs
);
5527 newrhs
= build_modify_expr (TREE_OPERAND (lhs
, 0), modifycode
, rhs
);
5528 if (newrhs
== error_mark_node
)
5529 return error_mark_node
;
5530 return build (COMPOUND_EXPR
, lhstype
, lhs
, newrhs
);
5532 /* Handle (a ? b : c) used as an "lvalue". */
5534 rhs
= save_expr (rhs
);
5536 /* Produce (a ? (b = rhs) : (c = rhs))
5537 except that the RHS goes through a save-expr
5538 so the code to compute it is only emitted once. */
5541 /* Check this here to avoid odd errors when trying to convert
5542 a throw to the type of the COND_EXPR. */
5543 if (!lvalue_or_else (lhs
, "assignment"))
5544 return error_mark_node
;
5546 cond
= build_conditional_expr
5547 (TREE_OPERAND (lhs
, 0),
5548 build_modify_expr (cp_convert (TREE_TYPE (lhs
),
5549 TREE_OPERAND (lhs
, 1)),
5551 build_modify_expr (cp_convert (TREE_TYPE (lhs
),
5552 TREE_OPERAND (lhs
, 2)),
5555 if (cond
== error_mark_node
)
5557 /* Make sure the code to compute the rhs comes out
5558 before the split. */
5559 return build (COMPOUND_EXPR
, TREE_TYPE (lhs
),
5560 /* Case to void to suppress warning
5561 from warn_if_unused_value. */
5562 cp_convert (void_type_node
, rhs
), cond
);
5569 if (TREE_CODE (lhs
) == OFFSET_REF
)
5571 if (TREE_OPERAND (lhs
, 0) == NULL_TREE
)
5573 /* Static class member? */
5574 tree member
= TREE_OPERAND (lhs
, 1);
5575 if (TREE_CODE (member
) == VAR_DECL
)
5579 compiler_error ("invalid static class member");
5580 return error_mark_node
;
5584 lhs
= resolve_offset_ref (lhs
);
5586 olhstype
= lhstype
= TREE_TYPE (lhs
);
5589 if (lhs
== error_mark_node
)
5592 if (TREE_CODE (lhstype
) == REFERENCE_TYPE
5593 && modifycode
!= INIT_EXPR
)
5595 lhs
= convert_from_reference (lhs
);
5596 olhstype
= lhstype
= TREE_TYPE (lhs
);
5599 /* If a binary op has been requested, combine the old LHS value with the RHS
5600 producing the value we should actually store into the LHS. */
5602 if (modifycode
== INIT_EXPR
)
5604 if (TREE_CODE (rhs
) == CONSTRUCTOR
)
5606 if (! same_type_p (TREE_TYPE (rhs
), lhstype
))
5608 result
= build (INIT_EXPR
, lhstype
, lhs
, rhs
);
5609 TREE_SIDE_EFFECTS (result
) = 1;
5612 else if (! IS_AGGR_TYPE (lhstype
))
5613 /* Do the default thing */;
5616 result
= build_method_call (lhs
, complete_ctor_identifier
,
5617 build_tree_list (NULL_TREE
, rhs
),
5618 TYPE_BINFO (lhstype
), LOOKUP_NORMAL
);
5619 if (result
== NULL_TREE
)
5620 return error_mark_node
;
5624 else if (modifycode
== NOP_EXPR
)
5626 /* `operator=' is not an inheritable operator. */
5627 if (! IS_AGGR_TYPE (lhstype
))
5628 /* Do the default thing */;
5631 result
= build_opfncall (MODIFY_EXPR
, LOOKUP_NORMAL
,
5632 lhs
, rhs
, make_node (NOP_EXPR
));
5633 if (result
== NULL_TREE
)
5634 return error_mark_node
;
5639 else if (PROMOTES_TO_AGGR_TYPE (lhstype
, REFERENCE_TYPE
))
5641 my_friendly_abort (978652);
5645 lhs
= stabilize_reference (lhs
);
5646 newrhs
= cp_build_binary_op (modifycode
, lhs
, rhs
);
5647 if (newrhs
== error_mark_node
)
5649 error (" in evaluation of `%Q(%#T, %#T)'", modifycode
,
5650 TREE_TYPE (lhs
), TREE_TYPE (rhs
));
5651 return error_mark_node
;
5655 /* Handle a cast used as an "lvalue".
5656 We have already performed any binary operator using the value as cast.
5657 Now convert the result to the cast type of the lhs,
5658 and then true type of the lhs and store it there;
5659 then convert result back to the cast type to be the value
5660 of the assignment. */
5662 switch (TREE_CODE (lhs
))
5667 case FIX_TRUNC_EXPR
:
5668 case FIX_FLOOR_EXPR
:
5669 case FIX_ROUND_EXPR
:
5671 if (TREE_CODE (TREE_TYPE (newrhs
)) == ARRAY_TYPE
5672 || TREE_CODE (TREE_TYPE (newrhs
)) == FUNCTION_TYPE
5673 || TREE_CODE (TREE_TYPE (newrhs
)) == METHOD_TYPE
5674 || TREE_CODE (TREE_TYPE (newrhs
)) == OFFSET_TYPE
)
5675 newrhs
= default_conversion (newrhs
);
5677 tree inner_lhs
= TREE_OPERAND (lhs
, 0);
5680 /* ISO C++ 5.4/1: The result is an lvalue if T is a reference
5681 type, otherwise the result is an rvalue. */
5682 if (! lvalue_p (lhs
))
5683 pedwarn ("ISO C++ forbids cast to non-reference type used as lvalue");
5685 result
= build_modify_expr (inner_lhs
, NOP_EXPR
,
5686 cp_convert (TREE_TYPE (inner_lhs
),
5687 cp_convert (lhstype
, newrhs
)));
5688 if (result
== error_mark_node
)
5690 return cp_convert (TREE_TYPE (lhs
), result
);
5697 /* Now we have handled acceptable kinds of LHS that are not truly lvalues.
5698 Reject anything strange now. */
5700 if (!lvalue_or_else (lhs
, "assignment"))
5701 return error_mark_node
;
5703 GNU_xref_assign (lhs
);
5705 /* Warn about storing in something that is `const'. */
5706 /* For C++, don't warn if this is initialization. */
5707 if (modifycode
!= INIT_EXPR
5708 && (TREE_READONLY (lhs
) || CP_TYPE_CONST_P (lhstype
)
5709 /* Functions are not modifiable, even though they are
5711 || TREE_CODE (TREE_TYPE (lhs
)) == FUNCTION_TYPE
5712 || (IS_AGGR_TYPE_CODE (TREE_CODE (lhstype
))
5713 && C_TYPE_FIELDS_READONLY (lhstype
))
5714 || (TREE_CODE (lhstype
) == REFERENCE_TYPE
5715 && CP_TYPE_CONST_P (TREE_TYPE (lhstype
)))))
5716 readonly_error (lhs
, "assignment", 0);
5718 /* If storing into a structure or union member,
5719 it has probably been given type `int'.
5720 Compute the type that would go with
5721 the actual amount of storage the member occupies. */
5723 if (TREE_CODE (lhs
) == COMPONENT_REF
5724 && (TREE_CODE (lhstype
) == INTEGER_TYPE
5725 || TREE_CODE (lhstype
) == REAL_TYPE
5726 || TREE_CODE (lhstype
) == ENUMERAL_TYPE
))
5728 lhstype
= TREE_TYPE (get_unwidened (lhs
, 0));
5730 /* If storing in a field that is in actuality a short or narrower
5731 than one, we must store in the field in its actual type. */
5733 if (lhstype
!= TREE_TYPE (lhs
))
5735 lhs
= copy_node (lhs
);
5736 TREE_TYPE (lhs
) = lhstype
;
5740 if (modifycode
!= INIT_EXPR
)
5742 /* Make modifycode now either a NOP_EXPR or an INIT_EXPR. */
5743 modifycode
= NOP_EXPR
;
5744 /* Reference-bashing */
5745 if (TREE_CODE (lhstype
) == REFERENCE_TYPE
)
5747 tree tmp
= convert_from_reference (lhs
);
5748 lhstype
= TREE_TYPE (tmp
);
5749 if (!COMPLETE_TYPE_P (lhstype
))
5751 incomplete_type_error (lhs
, lhstype
);
5752 return error_mark_node
;
5757 if (TREE_CODE (TREE_TYPE (newrhs
)) == REFERENCE_TYPE
)
5759 tree tmp
= convert_from_reference (newrhs
);
5760 if (!COMPLETE_TYPE_P (TREE_TYPE (tmp
)))
5762 incomplete_type_error (newrhs
, TREE_TYPE (tmp
));
5763 return error_mark_node
;
5769 if (TREE_SIDE_EFFECTS (lhs
))
5770 lhs
= stabilize_reference (lhs
);
5771 if (TREE_SIDE_EFFECTS (newrhs
))
5772 newrhs
= stabilize_reference (newrhs
);
5774 /* Convert new value to destination type. */
5776 if (TREE_CODE (lhstype
) == ARRAY_TYPE
)
5780 if (!same_or_base_type_p (lhstype
, TREE_TYPE (rhs
)))
5782 error ("incompatible types in assignment of `%T' to `%T'",
5783 TREE_TYPE (rhs
), lhstype
);
5784 return error_mark_node
;
5787 /* Allow array assignment in compiler-generated code. */
5788 if (pedantic
&& ! DECL_ARTIFICIAL (current_function_decl
))
5789 pedwarn ("ISO C++ forbids assignment of arrays");
5791 from_array
= TREE_CODE (TREE_TYPE (newrhs
)) == ARRAY_TYPE
5792 ? 1 + (modifycode
!= INIT_EXPR
): 0;
5793 return build_vec_init (lhs
, newrhs
, from_array
);
5796 if (modifycode
== INIT_EXPR
)
5798 newrhs
= convert_for_initialization (lhs
, lhstype
, newrhs
, LOOKUP_NORMAL
,
5799 "initialization", NULL_TREE
, 0);
5800 if (current_function_decl
&&
5801 lhs
== DECL_RESULT (current_function_decl
))
5803 if (DECL_INITIAL (lhs
))
5804 warning ("return value from function receives multiple initializations");
5805 DECL_INITIAL (lhs
) = newrhs
;
5810 /* Avoid warnings on enum bit fields. */
5811 if (TREE_CODE (olhstype
) == ENUMERAL_TYPE
5812 && TREE_CODE (lhstype
) == INTEGER_TYPE
)
5814 newrhs
= convert_for_assignment (olhstype
, newrhs
, "assignment",
5816 newrhs
= convert_force (lhstype
, newrhs
, 0);
5819 newrhs
= convert_for_assignment (lhstype
, newrhs
, "assignment",
5821 if (TREE_CODE (newrhs
) == CALL_EXPR
5822 && TYPE_NEEDS_CONSTRUCTING (lhstype
))
5823 newrhs
= build_cplus_new (lhstype
, newrhs
);
5825 /* Can't initialize directly from a TARGET_EXPR, since that would
5826 cause the lhs to be constructed twice, and possibly result in
5827 accidental self-initialization. So we force the TARGET_EXPR to be
5828 expanded without a target. */
5829 if (TREE_CODE (newrhs
) == TARGET_EXPR
)
5830 newrhs
= build (COMPOUND_EXPR
, TREE_TYPE (newrhs
), newrhs
,
5831 TREE_OPERAND (newrhs
, 0));
5834 if (newrhs
== error_mark_node
)
5835 return error_mark_node
;
5837 if (TREE_CODE (newrhs
) == COND_EXPR
)
5840 tree cond
= TREE_OPERAND (newrhs
, 0);
5842 if (TREE_SIDE_EFFECTS (lhs
))
5843 cond
= build_compound_expr (tree_cons
5845 build_tree_list (NULL_TREE
, cond
)));
5847 /* Cannot have two identical lhs on this one tree (result) as preexpand
5848 calls will rip them out and fill in RTL for them, but when the
5849 rtl is generated, the calls will only be in the first side of the
5850 condition, not on both, or before the conditional jump! (mrs) */
5851 lhs1
= break_out_calls (lhs
);
5854 /* If there's no change, the COND_EXPR behaves like any other rhs. */
5855 result
= build (modifycode
== NOP_EXPR
? MODIFY_EXPR
: INIT_EXPR
,
5856 lhstype
, lhs
, newrhs
);
5859 tree result_type
= TREE_TYPE (newrhs
);
5860 /* We have to convert each arm to the proper type because the
5861 types may have been munged by constant folding. */
5863 = build (COND_EXPR
, result_type
, cond
,
5864 build_modify_expr (lhs
, modifycode
,
5865 cp_convert (result_type
,
5866 TREE_OPERAND (newrhs
, 1))),
5867 build_modify_expr (lhs1
, modifycode
,
5868 cp_convert (result_type
,
5869 TREE_OPERAND (newrhs
, 2))));
5873 result
= build (modifycode
== NOP_EXPR
? MODIFY_EXPR
: INIT_EXPR
,
5874 lhstype
, lhs
, newrhs
);
5876 TREE_SIDE_EFFECTS (result
) = 1;
5878 /* If we got the LHS in a different type for storing in,
5879 convert the result back to the nominal type of LHS
5880 so that the value we return always has the same type
5881 as the LHS argument. */
5883 if (olhstype
== TREE_TYPE (result
))
5885 /* Avoid warnings converting integral types back into enums
5886 for enum bit fields. */
5887 if (TREE_CODE (TREE_TYPE (result
)) == INTEGER_TYPE
5888 && TREE_CODE (olhstype
) == ENUMERAL_TYPE
)
5890 result
= build (COMPOUND_EXPR
, olhstype
, result
, olhs
);
5891 TREE_NO_UNUSED_WARNING (result
) = 1;
5894 return convert_for_assignment (olhstype
, result
, "assignment",
5899 build_x_modify_expr (lhs
, modifycode
, rhs
)
5901 enum tree_code modifycode
;
5904 if (processing_template_decl
)
5905 return build_min_nt (MODOP_EXPR
, lhs
,
5906 build_min_nt (modifycode
, NULL_TREE
, NULL_TREE
), rhs
);
5908 if (modifycode
!= NOP_EXPR
)
5910 tree rval
= build_opfncall (MODIFY_EXPR
, LOOKUP_NORMAL
, lhs
, rhs
,
5911 make_node (modifycode
));
5915 return build_modify_expr (lhs
, modifycode
, rhs
);
5919 /* Get difference in deltas for different pointer to member function
5920 types. Return integer_zero_node, if FROM cannot be converted to a
5921 TO type. If FORCE is true, then allow reverse conversions as well.
5923 Note that the naming of FROM and TO is kind of backwards; the return
5924 value is what we add to a TO in order to get a FROM. They are named
5925 this way because we call this function to find out how to convert from
5926 a pointer to member of FROM to a pointer to member of TO. */
5929 get_delta_difference (from
, to
, force
)
5933 tree delta
= integer_zero_node
;
5938 binfo
= lookup_base (to
, from
, ba_check
, &kind
);
5939 if (kind
== bk_inaccessible
|| kind
== bk_ambig
)
5941 error (" in pointer to member function conversion");
5948 error_not_base_type (from
, to
);
5949 error (" in pointer to member conversion");
5952 binfo
= lookup_base (from
, to
, ba_check
, &kind
);
5955 virt_binfo
= binfo_from_vbase (binfo
);
5959 /* This is a reinterpret cast, we choose to do nothing. */
5960 warning ("pointer to member cast via virtual base `%T' of `%T'",
5961 BINFO_TYPE (virt_binfo
),
5962 BINFO_TYPE (BINFO_INHERITANCE_CHAIN (virt_binfo
)));
5965 delta
= BINFO_OFFSET (binfo
);
5966 delta
= cp_convert (ptrdiff_type_node
, delta
);
5967 delta
= cp_build_binary_op (MINUS_EXPR
,
5974 virt_binfo
= binfo_from_vbase (binfo
);
5977 /* This is a reinterpret cast, we choose to do nothing. */
5979 warning ("pointer to member cast via virtual base `%T' of `%T'",
5980 BINFO_TYPE (virt_binfo
),
5981 BINFO_TYPE (BINFO_INHERITANCE_CHAIN (virt_binfo
)));
5983 error ("pointer to member conversion via virtual base `%T' of `%T'",
5984 BINFO_TYPE (virt_binfo
),
5985 BINFO_TYPE (BINFO_INHERITANCE_CHAIN (virt_binfo
)));
5988 delta
= BINFO_OFFSET (binfo
);
5990 return cp_convert (ptrdiff_type_node
, delta
);
5993 /* Return a constructor for the pointer-to-member-function TYPE using
5994 the other components as specified. */
5997 build_ptrmemfunc1 (type
, delta
, pfn
)
5998 tree type
, delta
, pfn
;
6004 /* Pull the FIELD_DECLs out of the type. */
6005 pfn_field
= TYPE_FIELDS (type
);
6006 delta_field
= TREE_CHAIN (pfn_field
);
6008 /* Make sure DELTA has the type we want. */
6009 delta
= convert_and_check (delta_type_node
, delta
);
6011 /* Finish creating the initializer. */
6012 u
= tree_cons (pfn_field
, pfn
,
6013 build_tree_list (delta_field
, delta
));
6014 u
= build (CONSTRUCTOR
, type
, NULL_TREE
, u
);
6015 TREE_CONSTANT (u
) = TREE_CONSTANT (pfn
) && TREE_CONSTANT (delta
);
6016 TREE_STATIC (u
) = (TREE_CONSTANT (u
)
6017 && (initializer_constant_valid_p (pfn
, TREE_TYPE (pfn
))
6019 && (initializer_constant_valid_p (delta
, TREE_TYPE (delta
))
6024 /* Build a constructor for a pointer to member function. It can be
6025 used to initialize global variables, local variable, or used
6026 as a value in expressions. TYPE is the POINTER to METHOD_TYPE we
6029 If FORCE is non-zero, then force this conversion, even if
6030 we would rather not do it. Usually set when using an explicit
6033 Return error_mark_node, if something goes wrong. */
6036 build_ptrmemfunc (type
, pfn
, force
)
6041 tree pfn_type
= TREE_TYPE (pfn
);
6042 tree to_type
= build_ptrmemfunc_type (type
);
6044 /* Handle multiple conversions of pointer to member functions. */
6045 if (TYPE_PTRMEMFUNC_P (TREE_TYPE (pfn
)))
6047 tree delta
= NULL_TREE
;
6048 tree npfn
= NULL_TREE
;
6052 && !can_convert_arg (to_type
, TREE_TYPE (pfn
), pfn
))
6053 error ("invalid conversion to type `%T' from type `%T'",
6056 n
= get_delta_difference (TYPE_PTRMEMFUNC_OBJECT_TYPE (pfn_type
),
6057 TYPE_PTRMEMFUNC_OBJECT_TYPE (to_type
),
6060 /* We don't have to do any conversion to convert a
6061 pointer-to-member to its own type. But, we don't want to
6062 just return a PTRMEM_CST if there's an explicit cast; that
6063 cast should make the expression an invalid template argument. */
6064 if (TREE_CODE (pfn
) != PTRMEM_CST
)
6066 if (same_type_p (to_type
, pfn_type
))
6068 else if (integer_zerop (n
))
6069 return build_reinterpret_cast (to_type
, pfn
);
6072 if (TREE_SIDE_EFFECTS (pfn
))
6073 pfn
= save_expr (pfn
);
6075 /* Obtain the function pointer and the current DELTA. */
6076 if (TREE_CODE (pfn
) == PTRMEM_CST
)
6077 expand_ptrmemfunc_cst (pfn
, &delta
, &npfn
);
6080 npfn
= build_component_ref (pfn
, pfn_identifier
, NULL_TREE
, 0);
6081 delta
= build_component_ref (pfn
, delta_identifier
, NULL_TREE
, 0);
6084 /* Just adjust the DELTA field. */
6085 delta
= cp_convert (ptrdiff_type_node
, delta
);
6086 if (TARGET_PTRMEMFUNC_VBIT_LOCATION
== ptrmemfunc_vbit_in_delta
)
6087 n
= cp_build_binary_op (LSHIFT_EXPR
, n
, integer_one_node
);
6088 delta
= cp_build_binary_op (PLUS_EXPR
, delta
, n
);
6089 return build_ptrmemfunc1 (to_type
, delta
, npfn
);
6092 /* Handle null pointer to member function conversions. */
6093 if (integer_zerop (pfn
))
6095 pfn
= build_c_cast (type
, integer_zero_node
);
6096 return build_ptrmemfunc1 (to_type
,
6101 if (type_unknown_p (pfn
))
6102 return instantiate_type (type
, pfn
, itf_complain
);
6104 fn
= TREE_OPERAND (pfn
, 0);
6105 my_friendly_assert (TREE_CODE (fn
) == FUNCTION_DECL
, 0);
6106 return make_ptrmem_cst (to_type
, fn
);
6109 /* Return the DELTA, IDX, PFN, and DELTA2 values for the PTRMEM_CST
6112 ??? There is no consistency as to the types returned for the above
6113 values. Some code acts as if its a sizetype and some as if its
6114 integer_type_node. */
6117 expand_ptrmemfunc_cst (cst
, delta
, pfn
)
6122 tree type
= TREE_TYPE (cst
);
6123 tree fn
= PTRMEM_CST_MEMBER (cst
);
6124 tree ptr_class
, fn_class
;
6126 my_friendly_assert (TREE_CODE (fn
) == FUNCTION_DECL
, 0);
6128 /* The class that the function belongs to. */
6129 fn_class
= DECL_CONTEXT (fn
);
6131 /* The class that we're creating a pointer to member of. */
6132 ptr_class
= TYPE_PTRMEMFUNC_OBJECT_TYPE (type
);
6134 /* First, calculate the adjustment to the function's class. */
6135 *delta
= get_delta_difference (fn_class
, ptr_class
, /*force=*/0);
6137 if (!DECL_VIRTUAL_P (fn
))
6138 *pfn
= convert (TYPE_PTRMEMFUNC_FN_TYPE (type
), build_addr_func (fn
));
6141 /* If we're dealing with a virtual function, we have to adjust 'this'
6142 again, to point to the base which provides the vtable entry for
6143 fn; the call will do the opposite adjustment. */
6144 tree orig_class
= DECL_VIRTUAL_CONTEXT (fn
);
6145 tree binfo
= binfo_or_else (orig_class
, fn_class
);
6146 *delta
= fold (build (PLUS_EXPR
, TREE_TYPE (*delta
),
6147 *delta
, BINFO_OFFSET (binfo
)));
6149 /* We set PFN to the vtable offset at which the function can be
6150 found, plus one (unless ptrmemfunc_vbit_in_delta, in which
6151 case delta is shifted left, and then incremented). */
6152 *pfn
= DECL_VINDEX (fn
);
6153 *pfn
= fold (build (MULT_EXPR
, integer_type_node
, *pfn
,
6154 TYPE_SIZE_UNIT (vtable_entry_type
)));
6156 switch (TARGET_PTRMEMFUNC_VBIT_LOCATION
)
6158 case ptrmemfunc_vbit_in_pfn
:
6159 *pfn
= fold (build (PLUS_EXPR
, integer_type_node
, *pfn
,
6163 case ptrmemfunc_vbit_in_delta
:
6164 *delta
= fold (build (LSHIFT_EXPR
, TREE_TYPE (*delta
),
6165 *delta
, integer_one_node
));
6166 *delta
= fold (build (PLUS_EXPR
, TREE_TYPE (*delta
),
6167 *delta
, integer_one_node
));
6174 *pfn
= fold (build1 (NOP_EXPR
, TYPE_PTRMEMFUNC_FN_TYPE (type
),
6179 /* Return an expression for PFN from the pointer-to-member function
6183 pfn_from_ptrmemfunc (t
)
6186 if (TREE_CODE (t
) == PTRMEM_CST
)
6191 expand_ptrmemfunc_cst (t
, &delta
, &pfn
);
6196 return build_component_ref (t
, pfn_identifier
, NULL_TREE
, 0);
6199 /* Expression EXPR is about to be implicitly converted to TYPE. Warn
6200 if this is a potentially dangerous thing to do. Returns a possibly
6204 dubious_conversion_warnings (type
, expr
, errtype
, fndecl
, parmnum
)
6207 const char *errtype
;
6211 if (TREE_CODE (type
) == REFERENCE_TYPE
)
6212 type
= TREE_TYPE (type
);
6214 /* Issue warnings about peculiar, but legal, uses of NULL. */
6215 if (ARITHMETIC_TYPE_P (type
) && expr
== null_node
)
6218 warning ("passing NULL used for non-pointer %s %P of `%D'",
6219 errtype
, parmnum
, fndecl
);
6221 warning ("%s to non-pointer type `%T' from NULL", errtype
, type
);
6224 /* Warn about assigning a floating-point type to an integer type. */
6225 if (TREE_CODE (TREE_TYPE (expr
)) == REAL_TYPE
6226 && TREE_CODE (type
) == INTEGER_TYPE
)
6229 warning ("passing `%T' for %s %P of `%D'",
6230 TREE_TYPE (expr
), errtype
, parmnum
, fndecl
);
6232 warning ("%s to `%T' from `%T'", errtype
, type
, TREE_TYPE (expr
));
6234 /* And warn about assigning a negative value to an unsigned
6236 else if (TREE_UNSIGNED (type
) && TREE_CODE (type
) != BOOLEAN_TYPE
)
6238 if (TREE_CODE (expr
) == INTEGER_CST
6239 && TREE_NEGATED_INT (expr
))
6242 warning ("passing negative value `%E' for %s %P of `%D'",
6243 expr
, errtype
, parmnum
, fndecl
);
6245 warning ("%s of negative value `%E' to `%T'",
6246 errtype
, expr
, type
);
6249 overflow_warning (expr
);
6251 if (TREE_CONSTANT (expr
))
6257 /* Convert value RHS to type TYPE as preparation for an assignment to
6258 an lvalue of type TYPE. ERRTYPE is a string to use in error
6259 messages: "assignment", "return", etc. If FNDECL is non-NULL, we
6260 are doing the conversion in order to pass the PARMNUMth argument of
6264 convert_for_assignment (type
, rhs
, errtype
, fndecl
, parmnum
)
6266 const char *errtype
;
6270 register enum tree_code codel
= TREE_CODE (type
);
6271 register tree rhstype
;
6272 register enum tree_code coder
;
6274 if (codel
== OFFSET_TYPE
)
6275 my_friendly_abort (990505);
6277 if (TREE_CODE (rhs
) == OFFSET_REF
)
6278 rhs
= resolve_offset_ref (rhs
);
6280 /* Strip NON_LVALUE_EXPRs since we aren't using as an lvalue. */
6281 if (TREE_CODE (rhs
) == NON_LVALUE_EXPR
)
6282 rhs
= TREE_OPERAND (rhs
, 0);
6284 rhstype
= TREE_TYPE (rhs
);
6285 coder
= TREE_CODE (rhstype
);
6287 if (rhs
== error_mark_node
|| rhstype
== error_mark_node
)
6288 return error_mark_node
;
6289 if (TREE_CODE (rhs
) == TREE_LIST
&& TREE_VALUE (rhs
) == error_mark_node
)
6290 return error_mark_node
;
6292 rhs
= dubious_conversion_warnings (type
, rhs
, errtype
, fndecl
, parmnum
);
6294 /* The RHS of an assignment cannot have void type. */
6295 if (coder
== VOID_TYPE
)
6297 error ("void value not ignored as it ought to be");
6298 return error_mark_node
;
6301 /* Simplify the RHS if possible. */
6302 if (TREE_CODE (rhs
) == CONST_DECL
)
6303 rhs
= DECL_INITIAL (rhs
);
6304 else if (coder
!= ARRAY_TYPE
)
6305 rhs
= decl_constant_value (rhs
);
6309 The expression is implicitly converted (clause _conv_) to the
6310 cv-unqualified type of the left operand.
6312 We allow bad conversions here because by the time we get to this point
6313 we are committed to doing the conversion. If we end up doing a bad
6314 conversion, convert_like will complain. */
6315 if (!can_convert_arg_bad (type
, rhstype
, rhs
))
6317 /* When -Wno-pmf-conversions is use, we just silently allow
6318 conversions from pointers-to-members to plain pointers. If
6319 the conversion doesn't work, cp_convert will complain. */
6321 && TYPE_PTR_P (type
)
6322 && TYPE_PTRMEMFUNC_P (rhstype
))
6323 rhs
= cp_convert (strip_top_quals (type
), rhs
);
6326 /* If the right-hand side has unknown type, then it is an
6327 overloaded function. Call instantiate_type to get error
6329 if (rhstype
== unknown_type_node
)
6330 instantiate_type (type
, rhs
, itf_complain
);
6332 error ("cannot convert `%T' to `%T' for argument `%P' to `%D'",
6333 rhstype
, type
, parmnum
, fndecl
);
6335 error ("cannot convert `%T' to `%T' in %s", rhstype
, type
,
6337 return error_mark_node
;
6340 return perform_implicit_conversion (strip_top_quals (type
), rhs
);
6343 /* Convert RHS to be of type TYPE.
6344 If EXP is non-zero, it is the target of the initialization.
6345 ERRTYPE is a string to use in error messages.
6347 Two major differences between the behavior of
6348 `convert_for_assignment' and `convert_for_initialization'
6349 are that references are bashed in the former, while
6350 copied in the latter, and aggregates are assigned in
6351 the former (operator=) while initialized in the
6354 If using constructor make sure no conversion operator exists, if one does
6355 exist, an ambiguity exists.
6357 If flags doesn't include LOOKUP_COMPLAIN, don't complain about anything. */
6360 convert_for_initialization (exp
, type
, rhs
, flags
, errtype
, fndecl
, parmnum
)
6361 tree exp
, type
, rhs
;
6363 const char *errtype
;
6367 register enum tree_code codel
= TREE_CODE (type
);
6368 register tree rhstype
;
6369 register enum tree_code coder
;
6371 /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
6372 Strip such NOP_EXPRs, since RHS is used in non-lvalue context. */
6373 if (TREE_CODE (rhs
) == NOP_EXPR
6374 && TREE_TYPE (rhs
) == TREE_TYPE (TREE_OPERAND (rhs
, 0))
6375 && codel
!= REFERENCE_TYPE
)
6376 rhs
= TREE_OPERAND (rhs
, 0);
6378 if (rhs
== error_mark_node
6379 || (TREE_CODE (rhs
) == TREE_LIST
&& TREE_VALUE (rhs
) == error_mark_node
))
6380 return error_mark_node
;
6382 if (TREE_CODE (rhs
) == OFFSET_REF
)
6384 rhs
= resolve_offset_ref (rhs
);
6385 if (rhs
== error_mark_node
)
6386 return error_mark_node
;
6389 if (TREE_CODE (TREE_TYPE (rhs
)) == REFERENCE_TYPE
)
6390 rhs
= convert_from_reference (rhs
);
6392 if ((TREE_CODE (TREE_TYPE (rhs
)) == ARRAY_TYPE
6393 && TREE_CODE (type
) != ARRAY_TYPE
6394 && (TREE_CODE (type
) != REFERENCE_TYPE
6395 || TREE_CODE (TREE_TYPE (type
)) != ARRAY_TYPE
))
6396 || (TREE_CODE (TREE_TYPE (rhs
)) == FUNCTION_TYPE
6397 && (TREE_CODE (type
) != REFERENCE_TYPE
6398 || TREE_CODE (TREE_TYPE (type
)) != FUNCTION_TYPE
))
6399 || TREE_CODE (TREE_TYPE (rhs
)) == METHOD_TYPE
)
6400 rhs
= default_conversion (rhs
);
6402 rhstype
= TREE_TYPE (rhs
);
6403 coder
= TREE_CODE (rhstype
);
6405 if (coder
== ERROR_MARK
)
6406 return error_mark_node
;
6408 /* We accept references to incomplete types, so we can
6409 return here before checking if RHS is of complete type. */
6411 if (codel
== REFERENCE_TYPE
)
6413 /* This should eventually happen in convert_arguments. */
6414 int savew
= 0, savee
= 0;
6417 savew
= warningcount
, savee
= errorcount
;
6418 rhs
= initialize_reference (type
, rhs
);
6421 if (warningcount
> savew
)
6422 cp_warning_at ("in passing argument %P of `%+D'", parmnum
, fndecl
);
6423 else if (errorcount
> savee
)
6424 cp_error_at ("in passing argument %P of `%+D'", parmnum
, fndecl
);
6430 exp
= require_complete_type (exp
);
6431 if (exp
== error_mark_node
)
6432 return error_mark_node
;
6434 if (TREE_CODE (rhstype
) == REFERENCE_TYPE
)
6435 rhstype
= TREE_TYPE (rhstype
);
6437 type
= complete_type (type
);
6439 if (IS_AGGR_TYPE (type
))
6440 return ocp_convert (type
, rhs
, CONV_IMPLICIT
|CONV_FORCE_TEMP
, flags
);
6442 return convert_for_assignment (type
, rhs
, errtype
, fndecl
, parmnum
);
6445 /* Expand an ASM statement with operands, handling output operands
6446 that are not variables or INDIRECT_REFS by transforming such
6447 cases into cases that expand_asm_operands can handle.
6449 Arguments are same as for expand_asm_operands.
6451 We don't do default conversions on all inputs, because it can screw
6452 up operands that are expected to be in memory. */
6455 c_expand_asm_operands (string
, outputs
, inputs
, clobbers
, vol
, filename
, line
)
6456 tree string
, outputs
, inputs
, clobbers
;
6458 const char *filename
;
6461 int noutputs
= list_length (outputs
);
6463 /* o[I] is the place that output number I should be written. */
6464 register tree
*o
= (tree
*) alloca (noutputs
* sizeof (tree
));
6467 /* Record the contents of OUTPUTS before it is modified. */
6468 for (i
= 0, tail
= outputs
; tail
; tail
= TREE_CHAIN (tail
), i
++)
6469 o
[i
] = TREE_VALUE (tail
);
6471 /* Generate the ASM_OPERANDS insn;
6472 store into the TREE_VALUEs of OUTPUTS some trees for
6473 where the values were actually stored. */
6474 expand_asm_operands (string
, outputs
, inputs
, clobbers
, vol
, filename
, line
);
6476 /* Copy all the intermediate outputs into the specified outputs. */
6477 for (i
= 0, tail
= outputs
; tail
; tail
= TREE_CHAIN (tail
), i
++)
6479 if (o
[i
] != TREE_VALUE (tail
))
6481 expand_expr (build_modify_expr (o
[i
], NOP_EXPR
, TREE_VALUE (tail
)),
6482 const0_rtx
, VOIDmode
, EXPAND_NORMAL
);
6485 /* Restore the original value so that it's correct the next
6486 time we expand this function. */
6487 TREE_VALUE (tail
) = o
[i
];
6489 /* Detect modification of read-only values.
6490 (Otherwise done by build_modify_expr.) */
6493 tree type
= TREE_TYPE (o
[i
]);
6494 if (CP_TYPE_CONST_P (type
)
6495 || (IS_AGGR_TYPE_CODE (TREE_CODE (type
))
6496 && C_TYPE_FIELDS_READONLY (type
)))
6497 readonly_error (o
[i
], "modification by `asm'", 1);
6501 /* Those MODIFY_EXPRs could do autoincrements. */
6505 /* If RETVAL is the address of, or a reference to, a local variable or
6506 temporary give an appropraite warning. */
6509 maybe_warn_about_returning_address_of_local (retval
)
6512 tree valtype
= TREE_TYPE (DECL_RESULT (current_function_decl
));
6513 tree whats_returned
= retval
;
6517 if (TREE_CODE (whats_returned
) == COMPOUND_EXPR
)
6518 whats_returned
= TREE_OPERAND (whats_returned
, 1);
6519 else if (TREE_CODE (whats_returned
) == CONVERT_EXPR
6520 || TREE_CODE (whats_returned
) == NON_LVALUE_EXPR
6521 || TREE_CODE (whats_returned
) == NOP_EXPR
)
6522 whats_returned
= TREE_OPERAND (whats_returned
, 0);
6527 if (TREE_CODE (whats_returned
) != ADDR_EXPR
)
6529 whats_returned
= TREE_OPERAND (whats_returned
, 0);
6531 if (TREE_CODE (valtype
) == REFERENCE_TYPE
)
6533 if (TREE_CODE (whats_returned
) == AGGR_INIT_EXPR
6534 || TREE_CODE (whats_returned
) == TARGET_EXPR
)
6536 /* Get the target. */
6537 whats_returned
= TREE_OPERAND (whats_returned
, 0);
6538 warning ("returning reference to temporary");
6541 if (TREE_CODE (whats_returned
) == VAR_DECL
6542 && DECL_NAME (whats_returned
)
6543 && TEMP_NAME_P (DECL_NAME (whats_returned
)))
6545 warning ("reference to non-lvalue returned");
6550 if (TREE_CODE (whats_returned
) == VAR_DECL
6551 && DECL_NAME (whats_returned
)
6552 && DECL_FUNCTION_SCOPE_P (whats_returned
)
6553 && !(TREE_STATIC (whats_returned
)
6554 || TREE_PUBLIC (whats_returned
)))
6556 if (TREE_CODE (valtype
) == REFERENCE_TYPE
)
6557 cp_warning_at ("reference to local variable `%D' returned",
6560 cp_warning_at ("address of local variable `%D' returned",
6566 /* Check that returning RETVAL from the current function is legal.
6567 Return an expression explicitly showing all conversions required to
6568 change RETVAL into the function return type, and to assign it to
6569 the DECL_RESULT for the function. */
6572 check_return_expr (retval
)
6576 /* The type actually returned by the function, after any
6579 int fn_returns_value_p
;
6581 /* A `volatile' function is one that isn't supposed to return, ever.
6582 (This is a G++ extension, used to get better code for functions
6583 that call the `volatile' function.) */
6584 if (TREE_THIS_VOLATILE (current_function_decl
))
6585 warning ("function declared `noreturn' has a `return' statement");
6587 /* Check for various simple errors. */
6588 if (DECL_DESTRUCTOR_P (current_function_decl
))
6591 error ("returning a value from a destructor");
6594 else if (DECL_CONSTRUCTOR_P (current_function_decl
))
6596 if (in_function_try_handler
)
6597 /* If a return statement appears in a handler of the
6598 function-try-block of a constructor, the program is ill-formed. */
6599 error ("cannot return from a handler of a function-try-block of a constructor");
6601 /* You can't return a value from a constructor. */
6602 error ("returning a value from a constructor");
6606 /* When no explicit return-value is given in a function with a named
6607 return value, the named return value is used. */
6608 result
= DECL_RESULT (current_function_decl
);
6609 valtype
= TREE_TYPE (result
);
6610 my_friendly_assert (valtype
!= NULL_TREE
, 19990924);
6611 fn_returns_value_p
= !VOID_TYPE_P (valtype
);
6612 if (!retval
&& DECL_NAME (result
) && fn_returns_value_p
)
6615 /* Check for a return statement with no return value in a function
6616 that's supposed to return a value. */
6617 if (!retval
&& fn_returns_value_p
)
6619 pedwarn ("return-statement with no value, in function declared with a non-void return type");
6620 /* Clear this, so finish_function won't say that we reach the
6621 end of a non-void function (which we don't, we gave a
6623 current_function_returns_null
= 0;
6625 /* Check for a return statement with a value in a function that
6626 isn't supposed to return a value. */
6627 else if (retval
&& !fn_returns_value_p
)
6629 if (VOID_TYPE_P (TREE_TYPE (retval
)))
6630 /* You can return a `void' value from a function of `void'
6631 type. In that case, we have to evaluate the expression for
6632 its side-effects. */
6633 finish_expr_stmt (retval
);
6635 pedwarn ("return-statement with a value, in function declared with a void return type");
6637 current_function_returns_null
= 1;
6639 /* There's really no value to return, after all. */
6643 /* Remember that this function can sometimes return without a
6645 current_function_returns_null
= 1;
6647 /* Remember that this function did return a value. */
6648 current_function_returns_value
= 1;
6650 /* Only operator new(...) throw(), can return NULL [expr.new/13]. */
6651 if ((DECL_OVERLOADED_OPERATOR_P (current_function_decl
) == NEW_EXPR
6652 || DECL_OVERLOADED_OPERATOR_P (current_function_decl
) == VEC_NEW_EXPR
)
6653 && !TYPE_NOTHROW_P (TREE_TYPE (current_function_decl
))
6655 && null_ptr_cst_p (retval
))
6656 warning ("`operator new' must not return NULL unless it is declared `throw()' (or -fcheck-new is in effect)");
6658 /* Effective C++ rule 15. See also start_function. */
6660 && DECL_NAME (current_function_decl
) == ansi_assopname(NOP_EXPR
)
6661 && retval
!= current_class_ref
)
6662 warning ("`operator=' should return a reference to `*this'");
6664 /* The fabled Named Return Value optimization, as per [class.copy]/15:
6666 [...] For a function with a class return type, if the expression
6667 in the return statement is the name of a local object, and the cv-
6668 unqualified type of the local object is the same as the function
6669 return type, an implementation is permitted to omit creating the tem-
6670 porary object to hold the function return value [...]
6672 So, if this is a value-returning function that always returns the same
6673 local variable, remember it.
6675 It might be nice to be more flexible, and choose the first suitable
6676 variable even if the function sometimes returns something else, but
6677 then we run the risk of clobbering the variable we chose if the other
6678 returned expression uses the chosen variable somehow. And people expect
6679 this restriction, anyway. (jason 2000-11-19)
6681 See finish_function, genrtl_start_function, and declare_return_variable
6682 for other pieces of this optimization. */
6684 if (fn_returns_value_p
&& flag_elide_constructors
)
6686 if (retval
!= NULL_TREE
6687 && (current_function_return_value
== NULL_TREE
6688 || current_function_return_value
== retval
)
6689 && TREE_CODE (retval
) == VAR_DECL
6690 && DECL_CONTEXT (retval
) == current_function_decl
6691 && ! TREE_STATIC (retval
)
6692 && (DECL_ALIGN (retval
)
6693 >= DECL_ALIGN (DECL_RESULT (current_function_decl
)))
6694 && same_type_p ((TYPE_MAIN_VARIANT
6695 (TREE_TYPE (retval
))),
6697 (TREE_TYPE (TREE_TYPE (current_function_decl
))))))
6698 current_function_return_value
= retval
;
6700 current_function_return_value
= error_mark_node
;
6703 /* We don't need to do any conversions when there's nothing being
6705 if (!retval
|| retval
== error_mark_node
)
6708 /* Do any required conversions. */
6709 if (retval
== result
|| DECL_CONSTRUCTOR_P (current_function_decl
))
6710 /* No conversions are required. */
6714 /* The type the function is declared to return. */
6715 tree functype
= TREE_TYPE (TREE_TYPE (current_function_decl
));
6717 /* First convert the value to the function's return type, then
6718 to the type of return value's location to handle the
6719 case that functype is smaller than the valtype. */
6720 retval
= convert_for_initialization
6721 (NULL_TREE
, functype
, retval
, LOOKUP_NORMAL
|LOOKUP_ONLYCONVERTING
,
6722 "return", NULL_TREE
, 0);
6723 retval
= convert (valtype
, retval
);
6725 /* If the conversion failed, treat this just like `return;'. */
6726 if (retval
== error_mark_node
)
6728 /* We can't initialize a register from a AGGR_INIT_EXPR. */
6729 else if (! current_function_returns_struct
6730 && TREE_CODE (retval
) == TARGET_EXPR
6731 && TREE_CODE (TREE_OPERAND (retval
, 1)) == AGGR_INIT_EXPR
)
6732 retval
= build (COMPOUND_EXPR
, TREE_TYPE (retval
), retval
,
6733 TREE_OPERAND (retval
, 0));
6735 maybe_warn_about_returning_address_of_local (retval
);
6738 /* Actually copy the value returned into the appropriate location. */
6739 if (retval
&& retval
!= result
)
6740 retval
= build (INIT_EXPR
, TREE_TYPE (result
), result
, retval
);
6746 /* Returns non-zero if the pointer-type FROM can be converted to the
6747 pointer-type TO via a qualification conversion. If CONSTP is -1,
6748 then we return non-zero if the pointers are similar, and the
6749 cv-qualification signature of FROM is a proper subset of that of TO.
6751 If CONSTP is positive, then all outer pointers have been
6755 comp_ptr_ttypes_real (to
, from
, constp
)
6759 int to_more_cv_qualified
= 0;
6761 for (; ; to
= TREE_TYPE (to
), from
= TREE_TYPE (from
))
6763 if (TREE_CODE (to
) != TREE_CODE (from
))
6766 if (TREE_CODE (from
) == OFFSET_TYPE
6767 && same_type_p (TYPE_OFFSET_BASETYPE (from
),
6768 TYPE_OFFSET_BASETYPE (to
)))
6771 /* Const and volatile mean something different for function types,
6772 so the usual checks are not appropriate. */
6773 if (TREE_CODE (to
) != FUNCTION_TYPE
&& TREE_CODE (to
) != METHOD_TYPE
)
6775 if (!at_least_as_qualified_p (to
, from
))
6778 if (!at_least_as_qualified_p (from
, to
))
6783 ++to_more_cv_qualified
;
6787 constp
&= TYPE_READONLY (to
);
6790 if (TREE_CODE (to
) != POINTER_TYPE
)
6792 same_type_ignoring_top_level_qualifiers_p (to
, from
)
6793 && (constp
>= 0 || to_more_cv_qualified
);
6797 /* When comparing, say, char ** to char const **, this function takes the
6798 'char *' and 'char const *'. Do not pass non-pointer types to this
6802 comp_ptr_ttypes (to
, from
)
6805 return comp_ptr_ttypes_real (to
, from
, 1);
6808 /* Returns 1 if to and from are (possibly multi-level) pointers to the same
6809 type or inheritance-related types, regardless of cv-quals. */
6812 ptr_reasonably_similar (to
, from
)
6815 for (; ; to
= TREE_TYPE (to
), from
= TREE_TYPE (from
))
6817 /* Any target type is similar enough to void. */
6818 if (TREE_CODE (to
) == VOID_TYPE
6819 || TREE_CODE (from
) == VOID_TYPE
)
6822 if (TREE_CODE (to
) != TREE_CODE (from
))
6825 if (TREE_CODE (from
) == OFFSET_TYPE
6826 && comptypes (TYPE_OFFSET_BASETYPE (to
),
6827 TYPE_OFFSET_BASETYPE (from
),
6828 COMPARE_BASE
| COMPARE_RELAXED
))
6831 if (TREE_CODE (to
) == INTEGER_TYPE
6832 && TYPE_PRECISION (to
) == TYPE_PRECISION (from
))
6835 if (TREE_CODE (to
) == FUNCTION_TYPE
)
6838 if (TREE_CODE (to
) != POINTER_TYPE
)
6840 (TYPE_MAIN_VARIANT (to
), TYPE_MAIN_VARIANT (from
),
6841 COMPARE_BASE
| COMPARE_RELAXED
);
6845 /* Like comp_ptr_ttypes, for const_cast. */
6848 comp_ptr_ttypes_const (to
, from
)
6851 for (; ; to
= TREE_TYPE (to
), from
= TREE_TYPE (from
))
6853 if (TREE_CODE (to
) != TREE_CODE (from
))
6856 if (TREE_CODE (from
) == OFFSET_TYPE
6857 && same_type_p (TYPE_OFFSET_BASETYPE (from
),
6858 TYPE_OFFSET_BASETYPE (to
)))
6861 if (TREE_CODE (to
) != POINTER_TYPE
)
6862 return same_type_ignoring_top_level_qualifiers_p (to
, from
);
6866 /* Like comp_ptr_ttypes, for reinterpret_cast. */
6869 comp_ptr_ttypes_reinterpret (to
, from
)
6874 for (; ; to
= TREE_TYPE (to
), from
= TREE_TYPE (from
))
6876 if (TREE_CODE (from
) == OFFSET_TYPE
)
6877 from
= TREE_TYPE (from
);
6878 if (TREE_CODE (to
) == OFFSET_TYPE
)
6879 to
= TREE_TYPE (to
);
6881 /* Const and volatile mean something different for function types,
6882 so the usual checks are not appropriate. */
6883 if (TREE_CODE (from
) != FUNCTION_TYPE
&& TREE_CODE (from
) != METHOD_TYPE
6884 && TREE_CODE (to
) != FUNCTION_TYPE
&& TREE_CODE (to
) != METHOD_TYPE
)
6886 if (!at_least_as_qualified_p (to
, from
))
6890 && !at_least_as_qualified_p (from
, to
))
6892 constp
&= TYPE_READONLY (to
);
6895 if (TREE_CODE (from
) != POINTER_TYPE
6896 || TREE_CODE (to
) != POINTER_TYPE
)
6901 /* Returns the type qualifiers for this type, including the qualifiers on the
6902 elements for an array type. */
6905 cp_type_quals (type
)
6908 type
= strip_array_types (type
);
6909 return TYPE_QUALS (type
);
6912 /* Returns non-zero if the TYPE contains a mutable member */
6915 cp_has_mutable_p (type
)
6918 type
= strip_array_types (type
);
6920 return CLASS_TYPE_P (type
) && CLASSTYPE_HAS_MUTABLE (type
);
6923 /* Subroutine of casts_away_constness. Make T1 and T2 point at
6924 exemplar types such that casting T1 to T2 is casting away castness
6925 if and only if there is no implicit conversion from T1 to T2. */
6928 casts_away_constness_r (t1
, t2
)
6935 /* [expr.const.cast]
6937 For multi-level pointer to members and multi-level mixed pointers
6938 and pointers to members (conv.qual), the "member" aspect of a
6939 pointer to member level is ignored when determining if a const
6940 cv-qualifier has been cast away. */
6941 if (TYPE_PTRMEM_P (*t1
))
6942 *t1
= build_pointer_type (TREE_TYPE (TREE_TYPE (*t1
)));
6943 if (TYPE_PTRMEM_P (*t2
))
6944 *t2
= build_pointer_type (TREE_TYPE (TREE_TYPE (*t2
)));
6946 /* [expr.const.cast]
6948 For two pointer types:
6950 X1 is T1cv1,1 * ... cv1,N * where T1 is not a pointer type
6951 X2 is T2cv2,1 * ... cv2,M * where T2 is not a pointer type
6954 casting from X1 to X2 casts away constness if, for a non-pointer
6955 type T there does not exist an implicit conversion (clause
6958 Tcv1,(N-K+1) * cv1,(N-K+2) * ... cv1,N *
6962 Tcv2,(M-K+1) * cv2,(M-K+2) * ... cv2,M *. */
6964 if (TREE_CODE (*t1
) != POINTER_TYPE
6965 || TREE_CODE (*t2
) != POINTER_TYPE
)
6967 *t1
= cp_build_qualified_type (void_type_node
,
6968 cp_type_quals (*t1
));
6969 *t2
= cp_build_qualified_type (void_type_node
,
6970 cp_type_quals (*t2
));
6974 quals1
= cp_type_quals (*t1
);
6975 quals2
= cp_type_quals (*t2
);
6976 *t1
= TREE_TYPE (*t1
);
6977 *t2
= TREE_TYPE (*t2
);
6978 casts_away_constness_r (t1
, t2
);
6979 *t1
= build_pointer_type (*t1
);
6980 *t2
= build_pointer_type (*t2
);
6981 *t1
= cp_build_qualified_type (*t1
, quals1
);
6982 *t2
= cp_build_qualified_type (*t2
, quals2
);
6985 /* Returns non-zero if casting from TYPE1 to TYPE2 casts away
6989 casts_away_constness (t1
, t2
)
6993 if (TREE_CODE (t2
) == REFERENCE_TYPE
)
6995 /* [expr.const.cast]
6997 Casting from an lvalue of type T1 to an lvalue of type T2
6998 using a reference cast casts away constness if a cast from an
6999 rvalue of type "pointer to T1" to the type "pointer to T2"
7000 casts away constness. */
7001 t1
= (TREE_CODE (t1
) == REFERENCE_TYPE
7002 ? TREE_TYPE (t1
) : t1
);
7003 return casts_away_constness (build_pointer_type (t1
),
7004 build_pointer_type (TREE_TYPE (t2
)));
7007 if (TYPE_PTRMEM_P (t1
) && TYPE_PTRMEM_P (t2
))
7008 /* [expr.const.cast]
7010 Casting from an rvalue of type "pointer to data member of X
7011 of type T1" to the type "pointer to data member of Y of type
7012 T2" casts away constness if a cast from an rvalue of type
7013 "pointer to T1" to the type "pointer to T2" casts away
7015 return casts_away_constness
7016 (build_pointer_type (TREE_TYPE (TREE_TYPE (t1
))),
7017 build_pointer_type (TREE_TYPE (TREE_TYPE (t2
))));
7019 /* Casting away constness is only something that makes sense for
7020 pointer or reference types. */
7021 if (TREE_CODE (t1
) != POINTER_TYPE
7022 || TREE_CODE (t2
) != POINTER_TYPE
)
7025 /* Top-level qualifiers don't matter. */
7026 t1
= TYPE_MAIN_VARIANT (t1
);
7027 t2
= TYPE_MAIN_VARIANT (t2
);
7028 casts_away_constness_r (&t1
, &t2
);
7029 if (!can_convert (t2
, t1
))
7035 /* Returns TYPE with its cv qualifiers removed
7036 TYPE is T cv* .. *cv where T is not a pointer type,
7037 returns T * .. *. (If T is an array type, then the cv qualifiers
7038 above are those of the array members.) */
7041 strip_all_pointer_quals (type
)
7044 if (TREE_CODE (type
) == POINTER_TYPE
)
7045 return build_pointer_type (strip_all_pointer_quals (TREE_TYPE (type
)));
7046 else if (TREE_CODE (type
) == OFFSET_TYPE
)
7047 return build_offset_type (TYPE_OFFSET_BASETYPE (type
),
7048 strip_all_pointer_quals (TREE_TYPE (type
)));
7050 return TYPE_MAIN_VARIANT (type
);