1 /* Build expressions with type checking for C++ compiler.
2 Copyright (C) 1987, 1988, 1989, 1992, 1993, 1994, 1995, 1996, 1997, 1998,
3 1999, 2000, 2001, 2002 Free Software Foundation, Inc.
4 Hacked by Michael Tiemann (tiemann@cygnus.com)
6 This file is part of GNU CC.
8 GNU CC is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2, or (at your option)
13 GNU CC is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with GNU CC; see the file COPYING. If not, write to
20 the Free Software Foundation, 59 Temple Place - Suite 330,
21 Boston, MA 02111-1307, USA. */
24 /* This file is part of the C++ front end.
25 It contains routines to build C++ expressions given their operands,
26 including computing the types of the result, C and C++ specific error
27 checks, and some optimization.
29 There are also routines to build RETURN_STMT nodes and CASE_STMT nodes,
30 and to process initializations in declarations (since they work
31 like a strange sort of assignment). */
35 #include "coretypes.h"
45 #include "diagnostic.h"
48 static tree convert_for_assignment PARAMS ((tree, tree, const char *, tree,
50 static tree cp_pointer_int_sum PARAMS ((enum tree_code, tree, tree));
51 static tree rationalize_conditional_expr PARAMS ((enum tree_code, tree));
52 static int comp_target_parms PARAMS ((tree, tree));
53 static int comp_ptr_ttypes_real PARAMS ((tree, tree, int));
54 static int comp_ptr_ttypes_const PARAMS ((tree, tree));
55 static int comp_ptr_ttypes_reinterpret PARAMS ((tree, tree));
56 static int comp_except_types PARAMS ((tree, tree, int));
57 static int comp_array_types PARAMS ((int (*) (tree, tree, int), tree,
59 static tree common_base_type PARAMS ((tree, tree));
60 static tree lookup_anon_field PARAMS ((tree, tree));
61 static tree pointer_diff PARAMS ((tree, tree, tree));
62 static tree qualify_type_recursive PARAMS ((tree, tree));
63 static tree get_delta_difference PARAMS ((tree, tree, int));
64 static int comp_cv_target_types PARAMS ((tree, tree, int));
65 static void casts_away_constness_r PARAMS ((tree *, tree *));
66 static int casts_away_constness PARAMS ((tree, tree));
67 static void maybe_warn_about_returning_address_of_local PARAMS ((tree));
68 static tree strip_all_pointer_quals PARAMS ((tree));
70 /* Return the target type of TYPE, which means return T for:
71 T*, T&, T[], T (...), and otherwise, just T. */
77 if (TREE_CODE (type) == REFERENCE_TYPE)
78 type = TREE_TYPE (type);
79 while (TREE_CODE (type) == POINTER_TYPE
80 || TREE_CODE (type) == ARRAY_TYPE
81 || TREE_CODE (type) == FUNCTION_TYPE
82 || TREE_CODE (type) == METHOD_TYPE
83 || TREE_CODE (type) == OFFSET_TYPE)
84 type = TREE_TYPE (type);
88 /* Do `exp = require_complete_type (exp);' to make sure exp
89 does not have an incomplete type. (That includes void types.)
90 Returns the error_mark_node if the VALUE does not have
91 complete type when this function returns. */
94 require_complete_type (value)
99 if (processing_template_decl || value == error_mark_node)
102 if (TREE_CODE (value) == OVERLOAD)
103 type = unknown_type_node;
105 type = TREE_TYPE (value);
107 /* First, detect a valid value with a complete type. */
108 if (COMPLETE_TYPE_P (type))
111 /* If we see X::Y, we build an OFFSET_TYPE which has
112 not been laid out. Try to avoid an error by interpreting
113 it as this->X::Y, if reasonable. */
114 if (TREE_CODE (value) == OFFSET_REF
115 && current_class_ref != 0
116 && TREE_OPERAND (value, 0) == current_class_ref)
118 value = resolve_offset_ref (value);
119 return require_complete_type (value);
122 if (complete_type_or_else (type, value))
125 return error_mark_node;
128 /* Try to complete TYPE, if it is incomplete. For example, if TYPE is
129 a template instantiation, do the instantiation. Returns TYPE,
130 whether or not it could be completed, unless something goes
131 horribly wrong, in which case the error_mark_node is returned. */
137 if (type == NULL_TREE)
138 /* Rather than crash, we return something sure to cause an error
140 return error_mark_node;
142 if (type == error_mark_node || COMPLETE_TYPE_P (type))
144 else if (TREE_CODE (type) == ARRAY_TYPE && TYPE_DOMAIN (type))
146 tree t = complete_type (TREE_TYPE (type));
147 if (COMPLETE_TYPE_P (t) && ! processing_template_decl)
149 TYPE_NEEDS_CONSTRUCTING (type)
150 = TYPE_NEEDS_CONSTRUCTING (TYPE_MAIN_VARIANT (t));
151 TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type)
152 = TYPE_HAS_NONTRIVIAL_DESTRUCTOR (TYPE_MAIN_VARIANT (t));
154 else if (CLASS_TYPE_P (type) && CLASSTYPE_TEMPLATE_INSTANTIATION (type))
155 instantiate_class_template (TYPE_MAIN_VARIANT (type));
160 /* Like complete_type, but issue an error if the TYPE cannot be completed.
161 VALUE is used for informative diagnostics. DIAG_TYPE indicates the type
162 of diagnostic: 0 for an error, 1 for a warning, 2 for a pedwarn.
163 Returns NULL_TREE if the type cannot be made complete. */
166 complete_type_or_diagnostic (type, value, diag_type)
171 type = complete_type (type);
172 if (type == error_mark_node)
173 /* We already issued an error. */
175 else if (!COMPLETE_TYPE_P (type))
177 cxx_incomplete_type_diagnostic (value, type, diag_type);
184 /* Return truthvalue of whether type of EXP is instantiated. */
190 return (TREE_CODE (exp) == OVERLOAD
191 || TREE_CODE (exp) == TREE_LIST
192 || TREE_TYPE (exp) == unknown_type_node
193 || (TREE_CODE (TREE_TYPE (exp)) == OFFSET_TYPE
194 && TREE_TYPE (TREE_TYPE (exp)) == unknown_type_node));
197 /* Return a pointer or pointer to member type similar to T1, with a
198 cv-qualification signature that is the union of the cv-qualification
199 signatures of T1 and T2: [expr.rel], [expr.eq]. */
202 qualify_type_recursive (t1, t2)
205 if ((TYPE_PTR_P (t1) && TYPE_PTR_P (t2))
206 || (TYPE_PTRMEM_P (t1) && TYPE_PTRMEM_P (t2)))
213 tree attributes = (*targetm.merge_type_attributes) (t1, t2);
215 if (TYPE_PTRMEM_P (t1))
217 b1 = TYPE_PTRMEM_CLASS_TYPE (t1);
218 tt1 = TYPE_PTRMEM_POINTED_TO_TYPE (t1);
219 tt2 = TYPE_PTRMEM_POINTED_TO_TYPE (t2);
224 tt1 = TREE_TYPE (t1);
225 tt2 = TREE_TYPE (t2);
228 type_quals = (cp_type_quals (tt1) | cp_type_quals (tt2));
229 tgt = qualify_type_recursive (tt1, tt2);
230 tgt = cp_build_qualified_type (tgt, type_quals);
232 t1 = build_ptrmem_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) = merge_types (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) == COMPLEX_TYPE
342 || TREE_CODE (t1) == ENUMERAL_TYPE,
344 my_friendly_assert (ARITHMETIC_TYPE_P (t2)
345 || TREE_CODE (t2) == COMPLEX_TYPE
346 || TREE_CODE (t2) == ENUMERAL_TYPE,
349 /* In what follows, we slightly generalize the rules given in [expr] so
350 as to deal with `long long' and `complex'. First, merge the
352 attributes = (*targetm.merge_type_attributes) (t1, t2);
354 /* If one type is complex, form the common type of the non-complex
355 components, then make that complex. Use T1 or T2 if it is the
357 if (code1 == COMPLEX_TYPE || code2 == COMPLEX_TYPE)
359 tree subtype1 = code1 == COMPLEX_TYPE ? TREE_TYPE (t1) : t1;
360 tree subtype2 = code2 == COMPLEX_TYPE ? TREE_TYPE (t2) : t2;
362 = type_after_usual_arithmetic_conversions (subtype1, subtype2);
364 if (code1 == COMPLEX_TYPE && TREE_TYPE (t1) == subtype)
365 return build_type_attribute_variant (t1, attributes);
366 else if (code2 == COMPLEX_TYPE && TREE_TYPE (t2) == subtype)
367 return build_type_attribute_variant (t2, attributes);
369 return build_type_attribute_variant (build_complex_type (subtype),
373 /* If only one is real, use it as the result. */
374 if (code1 == REAL_TYPE && code2 != REAL_TYPE)
375 return build_type_attribute_variant (t1, attributes);
376 if (code2 == REAL_TYPE && code1 != REAL_TYPE)
377 return build_type_attribute_variant (t2, attributes);
379 /* Perform the integral promotions. */
380 if (code1 != REAL_TYPE)
382 t1 = type_promotes_to (t1);
383 t2 = type_promotes_to (t2);
386 /* Both real or both integers; use the one with greater precision. */
387 if (TYPE_PRECISION (t1) > TYPE_PRECISION (t2))
388 return build_type_attribute_variant (t1, attributes);
389 else if (TYPE_PRECISION (t2) > TYPE_PRECISION (t1))
390 return build_type_attribute_variant (t2, attributes);
392 /* The types are the same; no need to do anything fancy. */
393 if (TYPE_MAIN_VARIANT (t1) == TYPE_MAIN_VARIANT (t2))
394 return build_type_attribute_variant (t1, attributes);
396 if (code1 != REAL_TYPE)
398 /* If one is a sizetype, use it so size_binop doesn't blow up. */
399 if (TYPE_IS_SIZETYPE (t1) > TYPE_IS_SIZETYPE (t2))
400 return build_type_attribute_variant (t1, attributes);
401 if (TYPE_IS_SIZETYPE (t2) > TYPE_IS_SIZETYPE (t1))
402 return build_type_attribute_variant (t2, attributes);
404 /* If one is unsigned long long, then convert the other to unsigned
406 if (same_type_p (TYPE_MAIN_VARIANT (t1), long_long_unsigned_type_node)
407 || same_type_p (TYPE_MAIN_VARIANT (t2), long_long_unsigned_type_node))
408 return build_type_attribute_variant (long_long_unsigned_type_node,
410 /* If one is a long long, and the other is an unsigned long, and
411 long long can represent all the values of an unsigned long, then
412 convert to a long long. Otherwise, convert to an unsigned long
413 long. Otherwise, if either operand is long long, convert the
416 Since we're here, we know the TYPE_PRECISION is the same;
417 therefore converting to long long cannot represent all the values
418 of an unsigned long, so we choose unsigned long long in that
420 if (same_type_p (TYPE_MAIN_VARIANT (t1), long_long_integer_type_node)
421 || same_type_p (TYPE_MAIN_VARIANT (t2), long_long_integer_type_node))
423 tree t = ((TREE_UNSIGNED (t1) || TREE_UNSIGNED (t2))
424 ? long_long_unsigned_type_node
425 : long_long_integer_type_node);
426 return build_type_attribute_variant (t, attributes);
429 /* Go through the same procedure, but for longs. */
430 if (same_type_p (TYPE_MAIN_VARIANT (t1), long_unsigned_type_node)
431 || same_type_p (TYPE_MAIN_VARIANT (t2), long_unsigned_type_node))
432 return build_type_attribute_variant (long_unsigned_type_node,
434 if (same_type_p (TYPE_MAIN_VARIANT (t1), long_integer_type_node)
435 || same_type_p (TYPE_MAIN_VARIANT (t2), long_integer_type_node))
437 tree t = ((TREE_UNSIGNED (t1) || TREE_UNSIGNED (t2))
438 ? long_unsigned_type_node : long_integer_type_node);
439 return build_type_attribute_variant (t, attributes);
441 /* Otherwise prefer the unsigned one. */
442 if (TREE_UNSIGNED (t1))
443 return build_type_attribute_variant (t1, attributes);
445 return build_type_attribute_variant (t2, attributes);
449 if (same_type_p (TYPE_MAIN_VARIANT (t1), long_double_type_node)
450 || same_type_p (TYPE_MAIN_VARIANT (t2), long_double_type_node))
451 return build_type_attribute_variant (long_double_type_node,
453 if (same_type_p (TYPE_MAIN_VARIANT (t1), double_type_node)
454 || same_type_p (TYPE_MAIN_VARIANT (t2), double_type_node))
455 return build_type_attribute_variant (double_type_node,
457 if (same_type_p (TYPE_MAIN_VARIANT (t1), float_type_node)
458 || same_type_p (TYPE_MAIN_VARIANT (t2), float_type_node))
459 return build_type_attribute_variant (float_type_node,
462 /* Two floating-point types whose TYPE_MAIN_VARIANTs are none of
463 the standard C++ floating-point types. Logic earlier in this
464 function has already eliminated the possibility that
465 TYPE_PRECISION (t2) != TYPE_PRECISION (t1), so there's no
466 compelling reason to choose one or the other. */
467 return build_type_attribute_variant (t1, attributes);
471 /* Return the composite pointer type (see [expr.rel]) for T1 and T2.
472 ARG1 and ARG2 are the values with those types. The LOCATION is a
473 string describing the current location, in case an error occurs. */
476 composite_pointer_type (t1, t2, arg1, arg2, location)
481 const char* location;
488 If one operand is a null pointer constant, the composite pointer
489 type is the type of the other operand. */
490 if (null_ptr_cst_p (arg1))
492 if (null_ptr_cst_p (arg2))
495 /* Deal with pointer-to-member functions in the same way as we deal
496 with pointers to functions. */
497 if (TYPE_PTRMEMFUNC_P (t1))
498 t1 = TYPE_PTRMEMFUNC_FN_TYPE (t1);
499 if (TYPE_PTRMEMFUNC_P (t2))
500 t2 = TYPE_PTRMEMFUNC_FN_TYPE (t2);
502 /* Merge the attributes. */
503 attributes = (*targetm.merge_type_attributes) (t1, t2);
509 If one of the operands has type "pointer to cv1 void*", then
510 the other has type "pointer to cv2T", and the composite pointer
511 type is "pointer to cv12 void", where cv12 is the union of cv1
514 If either type is a pointer to void, make sure it is T1. */
515 if (VOID_TYPE_P (TREE_TYPE (t2)))
522 /* Now, if T1 is a pointer to void, merge the qualifiers. */
523 if (VOID_TYPE_P (TREE_TYPE (t1)))
525 if (pedantic && TYPE_PTRFN_P (t2))
526 pedwarn ("ISO C++ forbids %s between pointer of type `void *' and pointer-to-function", location);
529 result_type = cp_build_qualified_type (void_type_node,
531 | cp_type_quals (t2)));
532 result_type = build_pointer_type (result_type);
536 tree full1 = qualify_type_recursive (t1, t2);
537 tree full2 = qualify_type_recursive (t2, t1);
539 int val = comp_target_types (full1, full2, 1);
547 pedwarn ("%s between distinct pointer types `%T' and `%T' lacks a cast",
549 result_type = ptr_type_node;
553 return build_type_attribute_variant (result_type, attributes);
556 /* Return the merged type of two types.
557 We assume that comptypes has already been done and returned 1;
558 if that isn't so, this may crash.
560 This just combines attributes and default arguments; any other
561 differences would cause the two types to compare unalike. */
567 register enum tree_code code1;
568 register enum tree_code code2;
571 /* Save time if the two types are the same. */
574 if (original_type (t1) == original_type (t2))
577 /* If one type is nonsense, use the other. */
578 if (t1 == error_mark_node)
580 if (t2 == error_mark_node)
583 /* Merge the attributes. */
584 attributes = (*targetm.merge_type_attributes) (t1, t2);
586 /* Treat an enum type as the unsigned integer type of the same width. */
588 if (TYPE_PTRMEMFUNC_P (t1))
589 t1 = TYPE_PTRMEMFUNC_FN_TYPE (t1);
590 if (TYPE_PTRMEMFUNC_P (t2))
591 t2 = TYPE_PTRMEMFUNC_FN_TYPE (t2);
593 code1 = TREE_CODE (t1);
594 code2 = TREE_CODE (t2);
600 /* For two pointers, do this recursively on the target type. */
602 tree target = merge_types (TREE_TYPE (t1), TREE_TYPE (t2));
603 int quals = cp_type_quals (t1);
605 if (code1 == POINTER_TYPE)
606 t1 = build_pointer_type (target);
608 t1 = build_reference_type (target);
609 t1 = build_type_attribute_variant (t1, attributes);
610 t1 = cp_build_qualified_type (t1, quals);
612 if (TREE_CODE (target) == METHOD_TYPE)
613 t1 = build_ptrmemfunc_type (t1);
620 tree base = TYPE_OFFSET_BASETYPE (t1);
621 tree target = merge_types (TREE_TYPE (t1), TREE_TYPE (t2));
622 t1 = build_offset_type (base, target);
628 tree elt = merge_types (TREE_TYPE (t1), TREE_TYPE (t2));
629 /* Save space: see if the result is identical to one of the args. */
630 if (elt == TREE_TYPE (t1) && TYPE_DOMAIN (t1))
631 return build_type_attribute_variant (t1, attributes);
632 if (elt == TREE_TYPE (t2) && TYPE_DOMAIN (t2))
633 return build_type_attribute_variant (t2, attributes);
634 /* Merge the element types, and have a size if either arg has one. */
635 t1 = build_cplus_array_type
636 (elt, TYPE_DOMAIN (TYPE_DOMAIN (t1) ? t1 : t2));
641 /* Function types: prefer the one that specified arg types.
642 If both do, merge the arg types. Also merge the return types. */
644 tree valtype = merge_types (TREE_TYPE (t1), TREE_TYPE (t2));
645 tree p1 = TYPE_ARG_TYPES (t1);
646 tree p2 = TYPE_ARG_TYPES (t2);
649 /* Save space: see if the result is identical to one of the args. */
650 if (valtype == TREE_TYPE (t1) && ! p2)
651 return build_type_attribute_variant (t1, attributes);
652 if (valtype == TREE_TYPE (t2) && ! p1)
653 return build_type_attribute_variant (t2, attributes);
655 /* Simple way if one arg fails to specify argument types. */
656 if (p1 == NULL_TREE || TREE_VALUE (p1) == void_type_node)
658 rval = build_function_type (valtype, p2);
659 if ((raises = TYPE_RAISES_EXCEPTIONS (t2)))
660 rval = build_exception_variant (rval, raises);
661 return build_type_attribute_variant (rval, attributes);
663 raises = TYPE_RAISES_EXCEPTIONS (t1);
664 if (p2 == NULL_TREE || TREE_VALUE (p2) == void_type_node)
666 rval = build_function_type (valtype, p1);
668 rval = build_exception_variant (rval, raises);
669 return build_type_attribute_variant (rval, attributes);
672 rval = build_function_type (valtype, commonparms (p1, p2));
673 t1 = build_exception_variant (rval, raises);
679 /* Get this value the long way, since TYPE_METHOD_BASETYPE
680 is just the main variant of this. */
681 tree basetype = TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (t2)));
682 tree raises = TYPE_RAISES_EXCEPTIONS (t1);
685 /* If this was a member function type, get back to the
686 original type of type member function (i.e., without
687 the class instance variable up front. */
688 t1 = build_function_type (TREE_TYPE (t1),
689 TREE_CHAIN (TYPE_ARG_TYPES (t1)));
690 t2 = build_function_type (TREE_TYPE (t2),
691 TREE_CHAIN (TYPE_ARG_TYPES (t2)));
692 t3 = merge_types (t1, t2);
693 t3 = build_cplus_method_type (basetype, TREE_TYPE (t3),
694 TYPE_ARG_TYPES (t3));
695 t1 = build_exception_variant (t3, raises);
701 return build_type_attribute_variant (t1, attributes);
704 /* Return the common type of two types.
705 We assume that comptypes has already been done and returned 1;
706 if that isn't so, this may crash.
708 This is the type for the result of most arithmetic operations
709 if the operands have the given two types. */
715 enum tree_code code1;
716 enum tree_code code2;
718 /* If one type is nonsense, bail. */
719 if (t1 == error_mark_node || t2 == error_mark_node)
720 return error_mark_node;
722 code1 = TREE_CODE (t1);
723 code2 = TREE_CODE (t2);
725 if ((ARITHMETIC_TYPE_P (t1) || code1 == ENUMERAL_TYPE
726 || code1 == COMPLEX_TYPE)
727 && (ARITHMETIC_TYPE_P (t2) || code2 == ENUMERAL_TYPE
728 || code2 == COMPLEX_TYPE))
729 return type_after_usual_arithmetic_conversions (t1, t2);
731 else if ((TYPE_PTR_P (t1) && TYPE_PTR_P (t2))
732 || (TYPE_PTRMEM_P (t1) && TYPE_PTRMEM_P (t2))
733 || (TYPE_PTRMEMFUNC_P (t1) && TYPE_PTRMEMFUNC_P (t2)))
734 return composite_pointer_type (t1, t2, error_mark_node, error_mark_node,
741 /* Compare two exception specifier types for exactness or subsetness, if
742 allowed. Returns 0 for mismatch, 1 for same, 2 if B is allowed by A.
744 [except.spec] "If a class X ... objects of class X or any class publicly
745 and unambigously derrived from X. Similarly, if a pointer type Y * ...
746 exceptions of type Y * or that are pointers to any type publicly and
747 unambigously derrived from Y. Otherwise a function only allows exceptions
748 that have the same type ..."
749 This does not mention cv qualifiers and is different to what throw
750 [except.throw] and catch [except.catch] will do. They will ignore the
751 top level cv qualifiers, and allow qualifiers in the pointer to class
754 We implement the letter of the standard. */
757 comp_except_types (a, b, exact)
761 if (same_type_p (a, b))
765 if (cp_type_quals (a) || cp_type_quals (b))
768 if (TREE_CODE (a) == POINTER_TYPE
769 && TREE_CODE (b) == POINTER_TYPE)
773 if (cp_type_quals (a) || cp_type_quals (b))
777 if (TREE_CODE (a) != RECORD_TYPE
778 || TREE_CODE (b) != RECORD_TYPE)
781 if (ACCESSIBLY_UNIQUELY_DERIVED_P (a, b))
787 /* Return 1 if TYPE1 and TYPE2 are equivalent exception specifiers.
788 If EXACT is 0, T2 can be stricter than T1 (according to 15.4/7),
789 otherwise it must be exact. Exception lists are unordered, but
790 we've already filtered out duplicates. Most lists will be in order,
791 we should try to make use of that. */
794 comp_except_specs (t1, t2, exact)
805 if (t1 == NULL_TREE) /* T1 is ... */
806 return t2 == NULL_TREE || !exact;
807 if (!TREE_VALUE (t1)) /* t1 is EMPTY */
808 return t2 != NULL_TREE && !TREE_VALUE (t2);
809 if (t2 == NULL_TREE) /* T2 is ... */
811 if (TREE_VALUE (t1) && !TREE_VALUE (t2)) /* T2 is EMPTY, T1 is not */
814 /* Neither set is ... or EMPTY, make sure each part of T2 is in T1.
815 Count how many we find, to determine exactness. For exact matching and
816 ordered T1, T2, this is an O(n) operation, otherwise its worst case is
818 for (base = t1; t2 != NULL_TREE; t2 = TREE_CHAIN (t2))
820 for (probe = base; probe != NULL_TREE; probe = TREE_CHAIN (probe))
822 tree a = TREE_VALUE (probe);
823 tree b = TREE_VALUE (t2);
825 if (comp_except_types (a, b, exact))
827 if (probe == base && exact)
828 base = TREE_CHAIN (probe);
833 if (probe == NULL_TREE)
836 return !exact || base == NULL_TREE || length == list_length (t1);
839 /* Compare the array types T1 and T2, using CMP as the type comparison
840 function for the element types. STRICT is as for comptypes. */
843 comp_array_types (cmp, t1, t2, strict)
844 register int (*cmp) PARAMS ((tree, tree, int));
854 /* The type of the array elements must be the same. */
855 if (!(TREE_TYPE (t1) == TREE_TYPE (t2)
856 || (*cmp) (TREE_TYPE (t1), TREE_TYPE (t2),
857 strict & ~COMPARE_REDECLARATION)))
860 d1 = TYPE_DOMAIN (t1);
861 d2 = TYPE_DOMAIN (t2);
866 /* If one of the arrays is dimensionless, and the other has a
867 dimension, they are of different types. However, it is valid to
875 declarations for an array object can specify
876 array types that differ by the presence or absence of a major
877 array bound (_dcl.array_). */
879 return strict & COMPARE_REDECLARATION;
881 /* Check that the dimensions are the same. */
882 return (cp_tree_equal (TYPE_MIN_VALUE (d1),
884 && cp_tree_equal (TYPE_MAX_VALUE (d1),
885 TYPE_MAX_VALUE (d2)));
888 /* Return 1 if T1 and T2 are compatible types for assignment or
889 various other operations. STRICT is a bitwise-or of the COMPARE_*
893 comptypes (t1, t2, strict)
899 int orig_strict = strict;
901 /* The special exemption for redeclaring array types without an
902 array bound only applies at the top level:
907 is invalid, for example. */
908 strict &= ~COMPARE_REDECLARATION;
910 /* Suppress errors caused by previously reported errors */
914 /* This should never happen. */
915 my_friendly_assert (t1 != error_mark_node, 307);
917 if (t2 == error_mark_node)
920 /* If either type is the internal version of sizetype, return the
922 if (TREE_CODE (t1) == INTEGER_TYPE && TYPE_IS_SIZETYPE (t1)
923 && TYPE_DOMAIN (t1) != 0)
924 t1 = TYPE_DOMAIN (t1);
926 if (TREE_CODE (t2) == INTEGER_TYPE && TYPE_IS_SIZETYPE (t2)
927 && TYPE_DOMAIN (t2) != 0)
928 t2 = TYPE_DOMAIN (t2);
930 if (strict & COMPARE_RELAXED)
932 /* Treat an enum type as the unsigned integer type of the same width. */
934 if (TREE_CODE (t1) == ENUMERAL_TYPE)
935 t1 = c_common_type_for_size (TYPE_PRECISION (t1), 1);
936 if (TREE_CODE (t2) == ENUMERAL_TYPE)
937 t2 = c_common_type_for_size (TYPE_PRECISION (t2), 1);
943 if (TYPE_PTRMEMFUNC_P (t1))
944 t1 = TYPE_PTRMEMFUNC_FN_TYPE (t1);
945 if (TYPE_PTRMEMFUNC_P (t2))
946 t2 = TYPE_PTRMEMFUNC_FN_TYPE (t2);
948 /* Different classes of types can't be compatible. */
949 if (TREE_CODE (t1) != TREE_CODE (t2))
952 /* Qualifiers must match. */
953 if (cp_type_quals (t1) != cp_type_quals (t2))
955 if (strict == COMPARE_STRICT
956 && TYPE_FOR_JAVA (t1) != TYPE_FOR_JAVA (t2))
959 /* Allow for two different type nodes which have essentially the same
960 definition. Note that we already checked for equality of the type
961 qualifiers (just above). */
963 if (TYPE_MAIN_VARIANT (t1) == TYPE_MAIN_VARIANT (t2))
966 if (strict & COMPARE_NO_ATTRIBUTES)
968 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
969 else if (! (attrval = (*targetm.comp_type_attributes) (t1, t2)))
972 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
975 switch (TREE_CODE (t1))
977 case TEMPLATE_TEMPLATE_PARM:
978 case BOUND_TEMPLATE_TEMPLATE_PARM:
979 if (TEMPLATE_TYPE_IDX (t1) != TEMPLATE_TYPE_IDX (t2)
980 || TEMPLATE_TYPE_LEVEL (t1) != TEMPLATE_TYPE_LEVEL (t2))
982 if (! comp_template_parms
983 (DECL_TEMPLATE_PARMS (TEMPLATE_TEMPLATE_PARM_TEMPLATE_DECL (t1)),
984 DECL_TEMPLATE_PARMS (TEMPLATE_TEMPLATE_PARM_TEMPLATE_DECL (t2))))
986 if (TREE_CODE (t1) == TEMPLATE_TEMPLATE_PARM)
988 /* Don't check inheritance. */
989 strict = COMPARE_STRICT;
994 if (TYPE_TEMPLATE_INFO (t1) && TYPE_TEMPLATE_INFO (t2)
995 && (TYPE_TI_TEMPLATE (t1) == TYPE_TI_TEMPLATE (t2)
996 || TREE_CODE (t1) == BOUND_TEMPLATE_TEMPLATE_PARM))
997 val = comp_template_args (TYPE_TI_ARGS (t1),
1000 if ((strict & COMPARE_BASE) && DERIVED_FROM_P (t1, t2))
1002 else if ((strict & COMPARE_RELAXED) && DERIVED_FROM_P (t2, t1))
1007 val = (comptypes (build_pointer_type (TYPE_OFFSET_BASETYPE (t1)),
1008 build_pointer_type (TYPE_OFFSET_BASETYPE (t2)), strict)
1009 && comptypes (TREE_TYPE (t1), TREE_TYPE (t2), strict));
1013 case REFERENCE_TYPE:
1014 t1 = TREE_TYPE (t1);
1015 t2 = TREE_TYPE (t2);
1016 /* first, check whether the referred types match with the
1017 required level of strictness */
1018 val = comptypes (t1, t2, strict);
1021 if (TREE_CODE (t1) == RECORD_TYPE
1022 && TREE_CODE (t2) == RECORD_TYPE)
1028 val = ((TREE_TYPE (t1) == TREE_TYPE (t2)
1029 || comptypes (TREE_TYPE (t1), TREE_TYPE (t2), strict))
1030 && compparms (TYPE_ARG_TYPES (t1), TYPE_ARG_TYPES (t2)));
1034 /* Target types must match incl. qualifiers. We use ORIG_STRICT
1035 here since this is the one place where
1036 COMPARE_REDECLARATION should be used. */
1037 val = comp_array_types (comptypes, t1, t2, orig_strict);
1040 case TEMPLATE_TYPE_PARM:
1041 return TEMPLATE_TYPE_IDX (t1) == TEMPLATE_TYPE_IDX (t2)
1042 && TEMPLATE_TYPE_LEVEL (t1) == TEMPLATE_TYPE_LEVEL (t2);
1045 if (cp_tree_equal (TYPENAME_TYPE_FULLNAME (t1),
1046 TYPENAME_TYPE_FULLNAME (t2)) < 1)
1048 return same_type_p (TYPE_CONTEXT (t1), TYPE_CONTEXT (t2));
1050 case UNBOUND_CLASS_TEMPLATE:
1051 if (cp_tree_equal (TYPE_IDENTIFIER (t1),
1052 TYPE_IDENTIFIER (t2)) < 1)
1054 return same_type_p (TYPE_CONTEXT (t1), TYPE_CONTEXT (t2));
1057 return same_type_p (TREE_TYPE (t1), TREE_TYPE (t2));
1062 return attrval == 2 && val == 1 ? 2 : val;
1065 /* Subroutine of comp_target-types. Make sure that the cv-quals change
1066 only in the same direction as the target type. */
1069 comp_cv_target_types (ttl, ttr, nptrs)
1075 if (!at_least_as_qualified_p (ttl, ttr)
1076 && !at_least_as_qualified_p (ttr, ttl))
1077 /* The qualifications are incomparable. */
1080 if (TYPE_MAIN_VARIANT (ttl) == TYPE_MAIN_VARIANT (ttr))
1081 return more_qualified_p (ttr, ttl) ? -1 : 1;
1083 t = comp_target_types (ttl, ttr, nptrs);
1084 if ((t == 1 && at_least_as_qualified_p (ttl, ttr))
1085 || (t == -1 && at_least_as_qualified_p (ttr, ttl)))
1091 /* Return 1 or -1 if TTL and TTR are pointers to types that are equivalent,
1092 ignoring their qualifiers, 0 if not. Return 1 means that TTR can be
1093 converted to TTL. Return -1 means that TTL can be converted to TTR but
1096 NPTRS is the number of pointers we can strip off and keep cool.
1097 This is used to permit (for aggr A, aggr B) A, B* to convert to A*,
1098 but to not permit B** to convert to A**.
1100 This should go away. Callers should use can_convert or something
1101 similar instead. (jason 17 Apr 1997) */
1104 comp_target_types (ttl, ttr, nptrs)
1108 ttl = TYPE_MAIN_VARIANT (ttl);
1109 ttr = TYPE_MAIN_VARIANT (ttr);
1110 if (same_type_p (ttl, ttr))
1113 if (TREE_CODE (ttr) != TREE_CODE (ttl))
1116 if ((TREE_CODE (ttr) == POINTER_TYPE
1117 || TREE_CODE (ttr) == REFERENCE_TYPE)
1118 /* If we get a pointer with nptrs == 0, we don't allow any tweaking
1119 of the type pointed to. This is necessary for reference init
1120 semantics. We won't get here from a previous call with nptrs == 1;
1121 for multi-level pointers we end up in comp_ptr_ttypes. */
1124 int is_ptr = TREE_CODE (ttr) == POINTER_TYPE;
1126 ttl = TREE_TYPE (ttl);
1127 ttr = TREE_TYPE (ttr);
1131 if (TREE_CODE (ttl) == UNKNOWN_TYPE
1132 || TREE_CODE (ttr) == UNKNOWN_TYPE)
1134 else if (TREE_CODE (ttl) == VOID_TYPE
1135 && TREE_CODE (ttr) != FUNCTION_TYPE
1136 && TREE_CODE (ttr) != METHOD_TYPE
1137 && TREE_CODE (ttr) != OFFSET_TYPE)
1139 else if (TREE_CODE (ttr) == VOID_TYPE
1140 && TREE_CODE (ttl) != FUNCTION_TYPE
1141 && TREE_CODE (ttl) != METHOD_TYPE
1142 && TREE_CODE (ttl) != OFFSET_TYPE)
1144 else if (TREE_CODE (ttl) == POINTER_TYPE
1145 || TREE_CODE (ttl) == ARRAY_TYPE)
1147 if (comp_ptr_ttypes (ttl, ttr))
1149 else if (comp_ptr_ttypes (ttr, ttl))
1155 /* Const and volatile mean something different for function types,
1156 so the usual checks are not appropriate. */
1157 if (TREE_CODE (ttl) == FUNCTION_TYPE || TREE_CODE (ttl) == METHOD_TYPE)
1158 return comp_target_types (ttl, ttr, nptrs - 1);
1160 return comp_cv_target_types (ttl, ttr, nptrs - 1);
1163 if (TREE_CODE (ttr) == ARRAY_TYPE)
1164 return comp_array_types (comp_target_types, ttl, ttr, COMPARE_STRICT);
1165 else if (TREE_CODE (ttr) == FUNCTION_TYPE || TREE_CODE (ttr) == METHOD_TYPE)
1172 if (!same_type_p (TREE_TYPE (ttl), TREE_TYPE (ttr)))
1177 switch (comp_target_types (TREE_TYPE (ttl), TREE_TYPE (ttr), -1))
1186 argsl = TYPE_ARG_TYPES (ttl);
1187 argsr = TYPE_ARG_TYPES (ttr);
1189 /* Compare 'this' here, not in comp_target_parms. */
1190 if (TREE_CODE (ttr) == METHOD_TYPE)
1192 tree tl = TYPE_METHOD_BASETYPE (ttl);
1193 tree tr = TYPE_METHOD_BASETYPE (ttr);
1195 if (!same_or_base_type_p (tr, tl))
1197 if (same_or_base_type_p (tl, tr))
1203 argsl = TREE_CHAIN (argsl);
1204 argsr = TREE_CHAIN (argsr);
1207 switch (comp_target_parms (argsl, argsr))
1215 return saw_contra ? -1 : 1;
1218 else if (TREE_CODE (ttr) == OFFSET_TYPE)
1222 /* Contravariance: we can assign a pointer to base member to a pointer
1223 to derived member. Note difference from simple pointer case, where
1224 we can pass a pointer to derived to a pointer to base. */
1225 if (same_or_base_type_p (TYPE_OFFSET_BASETYPE (ttr),
1226 TYPE_OFFSET_BASETYPE (ttl)))
1228 else if (same_or_base_type_p (TYPE_OFFSET_BASETYPE (ttl),
1229 TYPE_OFFSET_BASETYPE (ttr)))
1239 ttl = TREE_TYPE (ttl);
1240 ttr = TREE_TYPE (ttr);
1242 if (TREE_CODE (ttl) == POINTER_TYPE
1243 || TREE_CODE (ttl) == ARRAY_TYPE)
1245 if (comp_ptr_ttypes (ttl, ttr))
1251 if (comp_cv_target_types (ttl, ttr, nptrs) == 1)
1256 else if (IS_AGGR_TYPE (ttl))
1260 if (same_or_base_type_p (build_pointer_type (ttl),
1261 build_pointer_type (ttr)))
1263 if (same_or_base_type_p (build_pointer_type (ttr),
1264 build_pointer_type (ttl)))
1272 /* Returns 1 if TYPE1 is at least as qualified as TYPE2. */
1275 at_least_as_qualified_p (type1, type2)
1279 /* All qualifiers for TYPE2 must also appear in TYPE1. */
1280 return ((cp_type_quals (type1) & cp_type_quals (type2))
1281 == cp_type_quals (type2));
1284 /* Returns 1 if TYPE1 is more qualified than TYPE2. */
1287 more_qualified_p (type1, type2)
1291 return (cp_type_quals (type1) != cp_type_quals (type2)
1292 && at_least_as_qualified_p (type1, type2));
1295 /* Returns 1 if TYPE1 is more cv-qualified than TYPE2, -1 if TYPE2 is
1296 more cv-qualified that TYPE1, and 0 otherwise. */
1299 comp_cv_qualification (type1, type2)
1303 if (cp_type_quals (type1) == cp_type_quals (type2))
1306 if (at_least_as_qualified_p (type1, type2))
1309 else if (at_least_as_qualified_p (type2, type1))
1315 /* Returns 1 if the cv-qualification signature of TYPE1 is a proper
1316 subset of the cv-qualification signature of TYPE2, and the types
1317 are similar. Returns -1 if the other way 'round, and 0 otherwise. */
1320 comp_cv_qual_signature (type1, type2)
1324 if (comp_ptr_ttypes_real (type2, type1, -1))
1326 else if (comp_ptr_ttypes_real (type1, type2, -1))
1332 /* If two types share a common base type, return that basetype.
1333 If there is not a unique most-derived base type, this function
1334 returns ERROR_MARK_NODE. */
1337 common_base_type (tt1, tt2)
1340 tree best = NULL_TREE;
1343 /* If one is a baseclass of another, that's good enough. */
1344 if (UNIQUELY_DERIVED_FROM_P (tt1, tt2))
1346 if (UNIQUELY_DERIVED_FROM_P (tt2, tt1))
1349 /* Otherwise, try to find a unique baseclass of TT1
1350 that is shared by TT2, and follow that down. */
1351 for (i = CLASSTYPE_N_BASECLASSES (tt1)-1; i >= 0; i--)
1353 tree basetype = TYPE_BINFO_BASETYPE (tt1, i);
1354 tree trial = common_base_type (basetype, tt2);
1357 if (trial == error_mark_node)
1359 if (best == NULL_TREE)
1361 else if (best != trial)
1362 return error_mark_node;
1367 for (i = CLASSTYPE_N_BASECLASSES (tt2)-1; i >= 0; i--)
1369 tree basetype = TYPE_BINFO_BASETYPE (tt2, i);
1370 tree trial = common_base_type (tt1, basetype);
1373 if (trial == error_mark_node)
1375 if (best == NULL_TREE)
1377 else if (best != trial)
1378 return error_mark_node;
1384 /* Subroutines of `comptypes'. */
1386 /* Return 1 if two parameter type lists PARMS1 and PARMS2 are
1387 equivalent in the sense that functions with those parameter types
1388 can have equivalent types. The two lists must be equivalent,
1391 C++: See comment above about TYPE1, TYPE2. */
1394 compparms (parms1, parms2)
1395 tree parms1, parms2;
1397 register tree t1 = parms1, t2 = parms2;
1399 /* An unspecified parmlist matches any specified parmlist
1400 whose argument types don't need default promotions. */
1404 if (t1 == 0 && t2 == 0)
1406 /* If one parmlist is shorter than the other,
1407 they fail to match. */
1408 if (t1 == 0 || t2 == 0)
1410 if (!same_type_p (TREE_VALUE (t2), TREE_VALUE (t1)))
1413 t1 = TREE_CHAIN (t1);
1414 t2 = TREE_CHAIN (t2);
1418 /* This really wants return whether or not parameter type lists
1419 would make their owning functions assignment compatible or not.
1421 The return value is like for comp_target_types.
1423 This should go away, possibly with the exception of the empty parmlist
1424 conversion; there are no conversions between function types in C++.
1425 (jason 17 Apr 1997) */
1428 comp_target_parms (parms1, parms2)
1429 tree parms1, parms2;
1431 register tree t1 = parms1, t2 = parms2;
1432 int warn_contravariance = 0;
1434 /* In C, an unspecified parmlist matches any specified parmlist
1435 whose argument types don't need default promotions. This is not
1436 true for C++, but let's do it anyway for unfixed headers. */
1438 if (t1 == 0 && t2 != 0)
1440 pedwarn ("ISO C++ prohibits conversion from `%#T' to `(...)'",
1442 return self_promoting_args_p (t2);
1445 return self_promoting_args_p (t1);
1447 for (; t1 || t2; t1 = TREE_CHAIN (t1), t2 = TREE_CHAIN (t2))
1451 /* If one parmlist is shorter than the other,
1452 they fail to match, unless STRICT is <= 0. */
1453 if (t1 == 0 || t2 == 0)
1455 p1 = TREE_VALUE (t1);
1456 p2 = TREE_VALUE (t2);
1457 if (same_type_p (p1, p2))
1463 if ((TREE_CODE (p1) == POINTER_TYPE && TREE_CODE (p2) == POINTER_TYPE)
1464 || (TREE_CODE (p1) == REFERENCE_TYPE
1465 && TREE_CODE (p2) == REFERENCE_TYPE))
1467 /* The following is wrong for contravariance,
1468 but many programs depend on it. */
1469 if (TREE_TYPE (p1) == void_type_node)
1471 if (TREE_TYPE (p2) == void_type_node)
1473 warn_contravariance = 1;
1476 if (IS_AGGR_TYPE (TREE_TYPE (p1))
1477 && !same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (p1),
1481 /* Note backwards order due to contravariance. */
1482 if (comp_target_types (p2, p1, 1) <= 0)
1484 if (comp_target_types (p1, p2, 1) > 0)
1486 warn_contravariance = 1;
1492 return warn_contravariance ? -1 : 1;
1496 cxx_sizeof_or_alignof_type (type, op, complain)
1501 enum tree_code type_code;
1503 const char *op_name;
1505 my_friendly_assert (op == SIZEOF_EXPR || op == ALIGNOF_EXPR, 20020720);
1506 if (processing_template_decl)
1507 return build_min_nt (op, type);
1509 op_name = operator_name_info[(int) op].name;
1511 if (TREE_CODE (type) == REFERENCE_TYPE)
1512 type = TREE_TYPE (type);
1513 type_code = TREE_CODE (type);
1515 if (type_code == METHOD_TYPE)
1517 if (complain && (pedantic || warn_pointer_arith))
1518 pedwarn ("invalid application of `%s' to a member function", op_name);
1519 value = size_one_node;
1521 else if (type_code == OFFSET_TYPE)
1524 error ("invalid application of `%s' to non-static member", op_name);
1525 value = size_zero_node;
1528 value = c_sizeof_or_alignof_type (complete_type (type), op, complain);
1537 if (processing_template_decl)
1538 return build_min_nt (SIZEOF_EXPR, e);
1540 if (TREE_CODE (e) == COMPONENT_REF
1541 && DECL_C_BIT_FIELD (TREE_OPERAND (e, 1)))
1542 error ("sizeof applied to a bit-field");
1543 if (is_overloaded_fn (e))
1545 pedwarn ("ISO C++ forbids applying `sizeof' to an expression of function type");
1546 return c_sizeof (char_type_node);
1548 else if (type_unknown_p (e))
1550 cxx_incomplete_type_error (e, TREE_TYPE (e));
1551 return c_sizeof (char_type_node);
1553 /* It's invalid to say `sizeof (X::i)' for `i' a non-static data
1554 member unless you're in a non-static member of X. So hand off to
1555 resolve_offset_ref. [expr.prim] */
1556 else if (TREE_CODE (e) == OFFSET_REF)
1557 e = resolve_offset_ref (e);
1559 if (e == error_mark_node)
1562 return cxx_sizeof (TREE_TYPE (e));
1566 /* Perform the array-to-pointer and function-to-pointer conversions
1569 In addition, references are converted to lvalues and manifest
1570 constants are replaced by their values. */
1573 decay_conversion (exp)
1577 register enum tree_code code;
1579 if (TREE_CODE (exp) == OFFSET_REF)
1580 exp = resolve_offset_ref (exp);
1582 type = TREE_TYPE (exp);
1583 code = TREE_CODE (type);
1585 if (code == REFERENCE_TYPE)
1587 exp = convert_from_reference (exp);
1588 type = TREE_TYPE (exp);
1589 code = TREE_CODE (type);
1592 if (type == error_mark_node)
1593 return error_mark_node;
1595 if (type_unknown_p (exp))
1597 cxx_incomplete_type_error (exp, TREE_TYPE (exp));
1598 return error_mark_node;
1601 /* Constants can be used directly unless they're not loadable. */
1602 if (TREE_CODE (exp) == CONST_DECL)
1603 exp = DECL_INITIAL (exp);
1604 /* Replace a nonvolatile const static variable with its value. We
1605 don't do this for arrays, though; we want the address of the
1606 first element of the array, not the address of the first element
1607 of its initializing constant. */
1608 else if (code != ARRAY_TYPE)
1610 exp = decl_constant_value (exp);
1611 type = TREE_TYPE (exp);
1614 /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
1615 Leave such NOP_EXPRs, since RHS is being used in non-lvalue context. */
1617 if (code == VOID_TYPE)
1619 error ("void value not ignored as it ought to be");
1620 return error_mark_node;
1622 if (code == METHOD_TYPE)
1624 if (code == FUNCTION_TYPE || is_overloaded_fn (exp))
1625 return build_unary_op (ADDR_EXPR, exp, 0);
1626 if (code == ARRAY_TYPE)
1631 if (TREE_CODE (exp) == INDIRECT_REF)
1633 /* Stripping away the INDIRECT_REF is not the right
1634 thing to do for references... */
1635 tree inner = TREE_OPERAND (exp, 0);
1636 if (TREE_CODE (TREE_TYPE (inner)) == REFERENCE_TYPE)
1638 inner = build1 (CONVERT_EXPR,
1639 build_pointer_type (TREE_TYPE
1640 (TREE_TYPE (inner))),
1642 TREE_CONSTANT (inner) = TREE_CONSTANT (TREE_OPERAND (inner, 0));
1644 return cp_convert (build_pointer_type (TREE_TYPE (type)), inner);
1647 if (TREE_CODE (exp) == COMPOUND_EXPR)
1649 tree op1 = decay_conversion (TREE_OPERAND (exp, 1));
1650 return build (COMPOUND_EXPR, TREE_TYPE (op1),
1651 TREE_OPERAND (exp, 0), op1);
1655 && ! (TREE_CODE (exp) == CONSTRUCTOR && TREE_STATIC (exp)))
1657 error ("invalid use of non-lvalue array");
1658 return error_mark_node;
1661 ptrtype = build_pointer_type (TREE_TYPE (type));
1663 if (TREE_CODE (exp) == VAR_DECL)
1665 /* ??? This is not really quite correct
1666 in that the type of the operand of ADDR_EXPR
1667 is not the target type of the type of the ADDR_EXPR itself.
1668 Question is, can this lossage be avoided? */
1669 adr = build1 (ADDR_EXPR, ptrtype, exp);
1670 if (!cxx_mark_addressable (exp))
1671 return error_mark_node;
1672 TREE_CONSTANT (adr) = staticp (exp);
1673 TREE_SIDE_EFFECTS (adr) = 0; /* Default would be, same as EXP. */
1676 /* This way is better for a COMPONENT_REF since it can
1677 simplify the offset for a component. */
1678 adr = build_unary_op (ADDR_EXPR, exp, 1);
1679 return cp_convert (ptrtype, adr);
1682 /* [basic.lval]: Class rvalues can have cv-qualified types; non-class
1683 rvalues always have cv-unqualified types. */
1684 if (! CLASS_TYPE_P (type))
1685 exp = cp_convert (TYPE_MAIN_VARIANT (type), exp);
1691 default_conversion (exp)
1695 enum tree_code code;
1697 exp = decay_conversion (exp);
1699 type = TREE_TYPE (exp);
1700 code = TREE_CODE (type);
1702 if (INTEGRAL_CODE_P (code))
1704 tree t = type_promotes_to (type);
1706 return cp_convert (t, exp);
1712 /* Take the address of an inline function without setting TREE_ADDRESSABLE
1716 inline_conversion (exp)
1719 if (TREE_CODE (exp) == FUNCTION_DECL)
1720 exp = build1 (ADDR_EXPR, build_pointer_type (TREE_TYPE (exp)), exp);
1725 /* Returns nonzero iff exp is a STRING_CST or the result of applying
1726 decay_conversion to one. */
1729 string_conv_p (totype, exp, warn)
1735 if (! flag_const_strings || TREE_CODE (totype) != POINTER_TYPE)
1738 t = TREE_TYPE (totype);
1739 if (!same_type_p (t, char_type_node)
1740 && !same_type_p (t, wchar_type_node))
1743 if (TREE_CODE (exp) == STRING_CST)
1745 /* Make sure that we don't try to convert between char and wchar_t. */
1746 if (!same_type_p (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (exp))), t))
1751 /* Is this a string constant which has decayed to 'const char *'? */
1752 t = build_pointer_type (build_qualified_type (t, TYPE_QUAL_CONST));
1753 if (!same_type_p (TREE_TYPE (exp), t))
1756 if (TREE_CODE (exp) != ADDR_EXPR
1757 || TREE_CODE (TREE_OPERAND (exp, 0)) != STRING_CST)
1761 /* This warning is not very useful, as it complains about printf. */
1762 if (warn && warn_write_strings)
1763 warning ("deprecated conversion from string constant to `%T'", totype);
1768 /* Given a COND_EXPR, MIN_EXPR, or MAX_EXPR in T, return it in a form that we
1769 can, for example, use as an lvalue. This code used to be in
1770 unary_complex_lvalue, but we needed it to deal with `a = (d == c) ? b : c'
1771 expressions, where we're dealing with aggregates. But now it's again only
1772 called from unary_complex_lvalue. The case (in particular) that led to
1773 this was with CODE == ADDR_EXPR, since it's not an lvalue when we'd
1777 rationalize_conditional_expr (code, t)
1778 enum tree_code code;
1781 /* For MIN_EXPR or MAX_EXPR, fold-const.c has arranged things so that
1782 the first operand is always the one to be used if both operands
1783 are equal, so we know what conditional expression this used to be. */
1784 if (TREE_CODE (t) == MIN_EXPR || TREE_CODE (t) == MAX_EXPR)
1787 build_conditional_expr (build_x_binary_op ((TREE_CODE (t) == MIN_EXPR
1788 ? LE_EXPR : GE_EXPR),
1789 TREE_OPERAND (t, 0),
1790 TREE_OPERAND (t, 1)),
1791 build_unary_op (code, TREE_OPERAND (t, 0), 0),
1792 build_unary_op (code, TREE_OPERAND (t, 1), 0));
1796 build_conditional_expr (TREE_OPERAND (t, 0),
1797 build_unary_op (code, TREE_OPERAND (t, 1), 0),
1798 build_unary_op (code, TREE_OPERAND (t, 2), 0));
1801 /* Given the TYPE of an anonymous union field inside T, return the
1802 FIELD_DECL for the field. If not found return NULL_TREE. Because
1803 anonymous unions can nest, we must also search all anonymous unions
1804 that are directly reachable. */
1807 lookup_anon_field (t, type)
1812 for (field = TYPE_FIELDS (t); field; field = TREE_CHAIN (field))
1814 if (TREE_STATIC (field))
1816 if (TREE_CODE (field) != FIELD_DECL || DECL_ARTIFICIAL (field))
1819 /* If we find it directly, return the field. */
1820 if (DECL_NAME (field) == NULL_TREE
1821 && type == TYPE_MAIN_VARIANT (TREE_TYPE (field)))
1826 /* Otherwise, it could be nested, search harder. */
1827 if (DECL_NAME (field) == NULL_TREE
1828 && ANON_AGGR_TYPE_P (TREE_TYPE (field)))
1830 tree subfield = lookup_anon_field (TREE_TYPE (field), type);
1838 /* Build an expression representing OBJECT.MEMBER. OBJECT is an
1839 expression; MEMBER is a DECL or baselink. If ACCESS_PATH is
1840 non-NULL, it indicates the path to the base used to name MEMBER.
1841 If PRESERVE_REFERENCE is true, the expression returned will have
1842 REFERENCE_TYPE if the MEMBER does. Otherwise, the expression
1843 returned will have the type referred to by the reference.
1845 This function does not perform access control; that is either done
1846 earlier by the parser when the name of MEMBER is resolved to MEMBER
1847 itself, or later when overload resolution selects one of the
1848 functions indicated by MEMBER. */
1851 build_class_member_access_expr (tree object, tree member,
1852 tree access_path, bool preserve_reference)
1856 tree result = NULL_TREE;
1858 if (object == error_mark_node || member == error_mark_node)
1859 return error_mark_node;
1861 my_friendly_assert (DECL_P (member) || BASELINK_P (member),
1864 /* Transform `(a, b).x' into `a, b.x' and `(a ? b : c).x' into
1865 `a ? b.x : c.x'. These transformations should not really be
1866 necessary, but they are. */
1867 if (TREE_CODE (object) == COMPOUND_EXPR)
1869 result = build_class_member_access_expr (TREE_OPERAND (object, 1),
1870 member, access_path,
1871 preserve_reference);
1872 return build (COMPOUND_EXPR, TREE_TYPE (result),
1873 TREE_OPERAND (object, 0), result);
1875 else if (TREE_CODE (object) == COND_EXPR)
1876 return (build_conditional_expr
1877 (TREE_OPERAND (object, 0),
1878 build_class_member_access_expr (TREE_OPERAND (object, 1),
1879 member, access_path,
1880 preserve_reference),
1881 build_class_member_access_expr (TREE_OPERAND (object, 2),
1882 member, access_path,
1883 preserve_reference)));
1887 The type of the first expression shall be "class object" (of a
1889 object_type = TREE_TYPE (object);
1890 if (!complete_type_or_else (object_type, object))
1891 return error_mark_node;
1892 if (!CLASS_TYPE_P (object_type))
1894 error ("request for member `%D' in `%E', which is of non-class type `%T'",
1895 member, object, object_type);
1896 return error_mark_node;
1899 /* The standard does not seem to actually say that MEMBER must be a
1900 member of OBJECT_TYPE. However, that is clearly what is
1902 if (DECL_P (member))
1904 member_scope = DECL_CLASS_CONTEXT (member);
1906 if (TREE_DEPRECATED (member))
1907 warn_deprecated_use (member);
1910 member_scope = BINFO_TYPE (BASELINK_BINFO (member));
1911 /* If MEMBER is from an anonymous aggregate, MEMBER_SCOPE will
1912 presently be the anonymous union. Go outwards until we find a
1913 type related to OBJECT_TYPE. */
1914 while (ANON_AGGR_TYPE_P (member_scope)
1915 && !same_type_ignoring_top_level_qualifiers_p (member_scope,
1917 member_scope = TYPE_CONTEXT (member_scope);
1918 if (!member_scope || !DERIVED_FROM_P (member_scope, object_type))
1920 error ("`%D' is not a member of `%T'", member, object_type);
1921 return error_mark_node;
1924 /* In [expr.ref], there is an explicit list of the valid choices for
1925 MEMBER. We check for each of those cases here. */
1926 if (TREE_CODE (member) == VAR_DECL)
1928 /* A static data member. */
1930 /* If OBJECT has side-effects, they are supposed to occur. */
1931 if (TREE_SIDE_EFFECTS (object))
1932 result = build (COMPOUND_EXPR, TREE_TYPE (result), object, result);
1934 else if (TREE_CODE (member) == FIELD_DECL)
1936 /* A non-static data member. */
1941 null_object_p = (TREE_CODE (object) == INDIRECT_REF
1942 && integer_zerop (TREE_OPERAND (object, 0)));
1944 /* Convert OBJECT to the type of MEMBER. */
1945 if (!same_type_p (object_type, member_scope))
1950 binfo = lookup_base (access_path ? access_path : object_type,
1951 member_scope, ba_ignore, &kind);
1952 if (binfo == error_mark_node)
1953 return error_mark_node;
1955 /* It is invalid to use to try to get to a virtual base of a
1956 NULL object. The most common cause is invalid use of
1958 if (null_object_p && kind == bk_via_virtual)
1960 error ("invalid access to non-static data member `%D' of NULL object",
1962 error ("(perhaps the `offsetof' macro was used incorrectly)");
1963 return error_mark_node;
1966 /* Convert to the base. */
1967 object = build_base_path (PLUS_EXPR, object, binfo,
1969 /* If we found the base successfully then we should be able
1970 to convert to it successfully. */
1971 my_friendly_assert (object != error_mark_node,
1975 /* Complain about other invalid uses of offsetof, even though they will
1976 give the right answer. Note that we complain whether or not they
1977 actually used the offsetof macro, since there's no way to know at this
1978 point. So we just give a warning, instead of a pedwarn. */
1979 if (null_object_p && CLASSTYPE_NON_POD_P (object_type))
1981 warning ("invalid access to non-static data member `%D' of NULL object",
1983 warning ("(perhaps the `offsetof' macro was used incorrectly)");
1986 /* If MEMBER is from an anonymous aggregate, we have converted
1987 OBJECT so that it refers to the class containing the
1988 anonymous union. Generate a reference to the anonymous union
1989 itself, and recur to find MEMBER. */
1990 if (ANON_AGGR_TYPE_P (DECL_CONTEXT (member)))
1992 tree anonymous_union;
1994 anonymous_union = lookup_anon_field (TREE_TYPE (object),
1995 DECL_CONTEXT (member));
1996 object = build_class_member_access_expr (object,
1998 /*access_path=*/NULL_TREE,
1999 preserve_reference);
2002 /* Compute the type of the field, as described in [expr.ref]. */
2003 type_quals = TYPE_UNQUALIFIED;
2004 member_type = TREE_TYPE (member);
2005 if (TREE_CODE (member_type) != REFERENCE_TYPE)
2007 type_quals = (cp_type_quals (member_type)
2008 | cp_type_quals (object_type));
2010 /* A field is const (volatile) if the enclosing object, or the
2011 field itself, is const (volatile). But, a mutable field is
2012 not const, even within a const object. */
2013 if (DECL_MUTABLE_P (member))
2014 type_quals &= ~TYPE_QUAL_CONST;
2015 member_type = cp_build_qualified_type (member_type, type_quals);
2018 result = fold (build (COMPONENT_REF, member_type, object, member));
2020 /* Mark the expression const or volatile, as appropriate. Even
2021 though we've dealt with the type above, we still have to mark the
2022 expression itself. */
2023 if (type_quals & TYPE_QUAL_CONST)
2024 TREE_READONLY (result) = 1;
2025 else if (type_quals & TYPE_QUAL_VOLATILE)
2026 TREE_THIS_VOLATILE (result) = 1;
2028 else if (BASELINK_P (member))
2030 /* The member is a (possibly overloaded) member function. */
2033 /* If the MEMBER is exactly one static member function, then we
2034 know the type of the expression. Otherwise, we must wait
2035 until overload resolution has been performed. */
2036 functions = BASELINK_FUNCTIONS (member);
2037 if (TREE_CODE (functions) == FUNCTION_DECL
2038 && DECL_STATIC_FUNCTION_P (functions))
2040 /* A static member function. */
2043 /* If OBJECT has side-effects, they are supposed to occur. */
2044 if (TREE_SIDE_EFFECTS (object))
2045 result = build (COMPOUND_EXPR, TREE_TYPE (result),
2049 /* Note that we do not convert OBJECT to the BASELINK_BINFO
2050 base. That will happen when the function is called. */
2051 result = build (COMPONENT_REF, unknown_type_node, object, member);
2053 else if (TREE_CODE (member) == CONST_DECL)
2055 /* The member is an enumerator. */
2057 /* If OBJECT has side-effects, they are supposed to occur. */
2058 if (TREE_SIDE_EFFECTS (object))
2059 result = build (COMPOUND_EXPR, TREE_TYPE (result),
2064 error ("invalid use of `%D'", member);
2065 return error_mark_node;
2068 if (!preserve_reference)
2071 If E2 is declared to have type "reference to T", then ... the
2072 type of E1.E2 is T. */
2073 result = convert_from_reference (result);
2078 /* This function is called by the parser to process a class member
2079 access expression of the form OBJECT.NAME. NAME is a node used by
2080 the parser to represent a name; it is not yet a DECL. It may,
2081 however, be a BASELINK where the BASELINK_FUNCTIONS is a
2082 TEMPLATE_ID_EXPR. Templates must be looked up by the parser, and
2083 there is no reason to do the lookup twice, so the parser keeps the
2087 finish_class_member_access_expr (tree object, tree name)
2091 tree access_path = NULL_TREE;
2093 if (object == error_mark_node || name == error_mark_node)
2094 return error_mark_node;
2096 if (processing_template_decl)
2097 return build_min_nt (COMPONENT_REF, object, name);
2099 if (TREE_CODE (object) == OFFSET_REF)
2100 object = resolve_offset_ref (object);
2102 object_type = TREE_TYPE (object);
2103 if (TREE_CODE (object_type) == REFERENCE_TYPE)
2105 object = convert_from_reference (object);
2106 object_type = TREE_TYPE (object);
2111 The type of the first expression shall be "class object" (of a
2113 if (!complete_type_or_else (object_type, object))
2114 return error_mark_node;
2115 if (!CLASS_TYPE_P (object_type))
2117 error ("request for member `%D' in `%E', which is of non-class type `%T'",
2118 name, object, object_type);
2119 return error_mark_node;
2122 if (BASELINK_P (name))
2124 /* A member function that has already been looked up. */
2125 my_friendly_assert ((TREE_CODE (BASELINK_FUNCTIONS (name))
2126 == TEMPLATE_ID_EXPR),
2132 bool is_template_id = false;
2133 tree template_args = NULL_TREE;
2135 if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
2137 is_template_id = true;
2138 template_args = TREE_OPERAND (name, 1);
2139 name = TREE_OPERAND (name, 0);
2142 if (TREE_CODE (name) == SCOPE_REF)
2146 /* A qualified name. The qualifying class or namespace `S' has
2147 already been looked up; it is either a TYPE or a
2148 NAMESPACE_DECL. The member name is either an IDENTIFIER_NODE
2149 or a BIT_NOT_EXPR. */
2150 scope = TREE_OPERAND (name, 0);
2151 name = TREE_OPERAND (name, 1);
2152 my_friendly_assert ((CLASS_TYPE_P (scope)
2153 || TREE_CODE (scope) == NAMESPACE_DECL),
2155 my_friendly_assert ((TREE_CODE (name) == IDENTIFIER_NODE
2156 || TREE_CODE (name) == BIT_NOT_EXPR),
2159 /* If SCOPE is a namespace, then the qualified name does not
2160 name a member of OBJECT_TYPE. */
2161 if (TREE_CODE (scope) == NAMESPACE_DECL)
2163 error ("`%D::%D' is not a member of `%T'",
2164 scope, name, object_type);
2165 return error_mark_node;
2168 /* Find the base of OBJECT_TYPE corresponding to SCOPE. */
2169 access_path = lookup_base (object_type, scope, ba_check, NULL);
2170 if (!access_path || access_path == error_mark_node)
2171 return error_mark_node;
2173 /* Look up the member. */
2174 member = lookup_member (access_path, name, /*protect=*/1,
2176 if (member == NULL_TREE)
2178 error ("'%D' has no member named '%E'", object_type, name);
2179 return error_mark_node;
2181 else if (member == error_mark_node)
2182 return error_mark_node;
2184 else if (TREE_CODE (name) == BIT_NOT_EXPR)
2187 if (TYPE_IDENTIFIER (object_type) != TREE_OPERAND (name, 0))
2189 error ("destructor specifier `%T::~%T' must have matching names",
2190 object_type, TREE_OPERAND (name, 0));
2191 return error_mark_node;
2193 if (! TYPE_HAS_DESTRUCTOR (object_type))
2195 error ("type `%T' has no destructor", object_type);
2196 return error_mark_node;
2198 member = CLASSTYPE_DESTRUCTORS (object_type);
2200 else if (TREE_CODE (name) == IDENTIFIER_NODE)
2202 /* An unqualified name. */
2203 member = lookup_member (object_type, name, /*protect=*/1,
2205 if (member == NULL_TREE)
2207 error ("'%D' has no member named '%E'", object_type, name);
2208 return error_mark_node;
2210 else if (member == error_mark_node)
2211 return error_mark_node;
2215 /* The YACC parser sometimes gives us things that are not names.
2216 These always indicate errors. The recursive-descent parser
2217 does not do this, so this code can go away once that parser
2218 replaces the YACC parser. */
2219 error ("invalid use of `%D'", name);
2220 return error_mark_node;
2225 tree template = member;
2227 if (BASELINK_P (template))
2228 BASELINK_FUNCTIONS (template)
2229 = build_nt (TEMPLATE_ID_EXPR,
2230 BASELINK_FUNCTIONS (template),
2234 error ("`%D' is not a member template function", name);
2235 return error_mark_node;
2240 return build_class_member_access_expr (object, member, access_path,
2241 /*preserve_reference=*/false);
2244 /* Return an expression for the MEMBER_NAME field in the internal
2245 representation of PTRMEM, a pointer-to-member function. (Each
2246 pointer-to-member function type gets its own RECORD_TYPE so it is
2247 more convenient to access the fields by name than by FIELD_DECL.)
2248 This routine converts the NAME to a FIELD_DECL and then creates the
2249 node for the complete expression. */
2252 build_ptrmemfunc_access_expr (tree ptrmem, tree member_name)
2258 /* This code is a stripped down version of
2259 build_class_member_access_expr. It does not work to use that
2260 routine directly because it expects the object to be of class
2262 ptrmem_type = TREE_TYPE (ptrmem);
2263 my_friendly_assert (TYPE_PTRMEMFUNC_P (ptrmem_type), 20020804);
2264 member = lookup_member (ptrmem_type, member_name, /*protect=*/0,
2266 member_type = cp_build_qualified_type (TREE_TYPE (member),
2267 cp_type_quals (ptrmem_type));
2268 return fold (build (COMPONENT_REF, member_type, ptrmem, member));
2271 /* Given an expression PTR for a pointer, return an expression
2272 for the value pointed to.
2273 ERRORSTRING is the name of the operator to appear in error messages.
2275 This function may need to overload OPERATOR_FNNAME.
2276 Must also handle REFERENCE_TYPEs for C++. */
2279 build_x_indirect_ref (ptr, errorstring)
2281 const char *errorstring;
2285 if (processing_template_decl)
2286 return build_min_nt (INDIRECT_REF, ptr);
2288 rval = build_opfncall (INDIRECT_REF, LOOKUP_NORMAL, ptr, NULL_TREE,
2292 return build_indirect_ref (ptr, errorstring);
2296 build_indirect_ref (ptr, errorstring)
2298 const char *errorstring;
2300 register tree pointer, type;
2302 if (ptr == error_mark_node)
2303 return error_mark_node;
2305 if (ptr == current_class_ptr)
2306 return current_class_ref;
2308 pointer = (TREE_CODE (TREE_TYPE (ptr)) == REFERENCE_TYPE
2309 ? ptr : default_conversion (ptr));
2310 type = TREE_TYPE (pointer);
2312 if (TYPE_PTR_P (type) || TREE_CODE (type) == REFERENCE_TYPE)
2316 If the type of the expression is "pointer to T," the type
2317 of the result is "T."
2319 We must use the canonical variant because certain parts of
2320 the back end, like fold, do pointer comparisons between
2322 tree t = canonical_type_variant (TREE_TYPE (type));
2324 if (VOID_TYPE_P (t))
2326 /* A pointer to incomplete type (other than cv void) can be
2327 dereferenced [expr.unary.op]/1 */
2328 error ("`%T' is not a pointer-to-object type", type);
2329 return error_mark_node;
2331 else if (TREE_CODE (pointer) == ADDR_EXPR
2333 && same_type_p (t, TREE_TYPE (TREE_OPERAND (pointer, 0))))
2334 /* The POINTER was something like `&x'. We simplify `*&x' to
2336 return TREE_OPERAND (pointer, 0);
2339 tree ref = build1 (INDIRECT_REF, t, pointer);
2341 /* We *must* set TREE_READONLY when dereferencing a pointer to const,
2342 so that we get the proper error message if the result is used
2343 to assign to. Also, &* is supposed to be a no-op. */
2344 TREE_READONLY (ref) = CP_TYPE_CONST_P (t);
2345 TREE_THIS_VOLATILE (ref) = CP_TYPE_VOLATILE_P (t);
2346 TREE_SIDE_EFFECTS (ref)
2347 = (TREE_THIS_VOLATILE (ref) || TREE_SIDE_EFFECTS (pointer)
2352 /* `pointer' won't be an error_mark_node if we were given a
2353 pointer to member, so it's cool to check for this here. */
2354 else if (TYPE_PTRMEM_P (type) || TYPE_PTRMEMFUNC_P (type))
2355 error ("invalid use of `%s' on pointer to member", errorstring);
2356 else if (pointer != error_mark_node)
2359 error ("invalid type argument of `%s'", errorstring);
2361 error ("invalid type argument");
2363 return error_mark_node;
2366 /* This handles expressions of the form "a[i]", which denotes
2369 This is logically equivalent in C to *(a+i), but we may do it differently.
2370 If A is a variable or a member, we generate a primitive ARRAY_REF.
2371 This avoids forcing the array out of registers, and can work on
2372 arrays that are not lvalues (for example, members of structures returned
2375 If INDEX is of some user-defined type, it must be converted to
2376 integer type. Otherwise, to make a compatible PLUS_EXPR, it
2377 will inherit the type of the array, which will be some pointer type. */
2380 build_array_ref (array, idx)
2385 error ("subscript missing in array reference");
2386 return error_mark_node;
2389 if (TREE_TYPE (array) == error_mark_node
2390 || TREE_TYPE (idx) == error_mark_node)
2391 return error_mark_node;
2393 /* If ARRAY is a COMPOUND_EXPR or COND_EXPR, move our reference
2395 switch (TREE_CODE (array))
2399 tree value = build_array_ref (TREE_OPERAND (array, 1), idx);
2400 return build (COMPOUND_EXPR, TREE_TYPE (value),
2401 TREE_OPERAND (array, 0), value);
2405 return build_conditional_expr
2406 (TREE_OPERAND (array, 0),
2407 build_array_ref (TREE_OPERAND (array, 1), idx),
2408 build_array_ref (TREE_OPERAND (array, 2), idx));
2414 if (TREE_CODE (TREE_TYPE (array)) == ARRAY_TYPE
2415 && TREE_CODE (array) != INDIRECT_REF)
2419 /* Subscripting with type char is likely to lose
2420 on a machine where chars are signed.
2421 So warn on any machine, but optionally.
2422 Don't warn for unsigned char since that type is safe.
2423 Don't warn for signed char because anyone who uses that
2424 must have done so deliberately. */
2425 if (warn_char_subscripts
2426 && TYPE_MAIN_VARIANT (TREE_TYPE (idx)) == char_type_node)
2427 warning ("array subscript has type `char'");
2429 /* Apply default promotions *after* noticing character types. */
2430 idx = default_conversion (idx);
2432 if (TREE_CODE (TREE_TYPE (idx)) != INTEGER_TYPE)
2434 error ("array subscript is not an integer");
2435 return error_mark_node;
2438 /* An array that is indexed by a non-constant
2439 cannot be stored in a register; we must be able to do
2440 address arithmetic on its address.
2441 Likewise an array of elements of variable size. */
2442 if (TREE_CODE (idx) != INTEGER_CST
2443 || (COMPLETE_TYPE_P (TREE_TYPE (TREE_TYPE (array)))
2444 && (TREE_CODE (TYPE_SIZE (TREE_TYPE (TREE_TYPE (array))))
2447 if (!cxx_mark_addressable (array))
2448 return error_mark_node;
2451 /* An array that is indexed by a constant value which is not within
2452 the array bounds cannot be stored in a register either; because we
2453 would get a crash in store_bit_field/extract_bit_field when trying
2454 to access a non-existent part of the register. */
2455 if (TREE_CODE (idx) == INTEGER_CST
2456 && TYPE_VALUES (TREE_TYPE (array))
2457 && ! int_fits_type_p (idx, TYPE_VALUES (TREE_TYPE (array))))
2459 if (!cxx_mark_addressable (array))
2460 return error_mark_node;
2463 if (pedantic && !lvalue_p (array))
2464 pedwarn ("ISO C++ forbids subscripting non-lvalue array");
2466 /* Note in C++ it is valid to subscript a `register' array, since
2467 it is valid to take the address of something with that
2468 storage specification. */
2472 while (TREE_CODE (foo) == COMPONENT_REF)
2473 foo = TREE_OPERAND (foo, 0);
2474 if (TREE_CODE (foo) == VAR_DECL && DECL_REGISTER (foo))
2475 warning ("subscripting array declared `register'");
2478 type = TREE_TYPE (TREE_TYPE (array));
2479 rval = build (ARRAY_REF, type, array, idx);
2480 /* Array ref is const/volatile if the array elements are
2481 or if the array is.. */
2482 TREE_READONLY (rval)
2483 |= (CP_TYPE_CONST_P (type) | TREE_READONLY (array));
2484 TREE_SIDE_EFFECTS (rval)
2485 |= (CP_TYPE_VOLATILE_P (type) | TREE_SIDE_EFFECTS (array));
2486 TREE_THIS_VOLATILE (rval)
2487 |= (CP_TYPE_VOLATILE_P (type) | TREE_THIS_VOLATILE (array));
2488 return require_complete_type (fold (rval));
2492 tree ar = default_conversion (array);
2493 tree ind = default_conversion (idx);
2495 /* Put the integer in IND to simplify error checking. */
2496 if (TREE_CODE (TREE_TYPE (ar)) == INTEGER_TYPE)
2503 if (ar == error_mark_node)
2506 if (TREE_CODE (TREE_TYPE (ar)) != POINTER_TYPE)
2508 error ("subscripted value is neither array nor pointer");
2509 return error_mark_node;
2511 if (TREE_CODE (TREE_TYPE (ind)) != INTEGER_TYPE)
2513 error ("array subscript is not an integer");
2514 return error_mark_node;
2517 return build_indirect_ref (cp_build_binary_op (PLUS_EXPR, ar, ind),
2522 /* Resolve a pointer to member function. INSTANCE is the object
2523 instance to use, if the member points to a virtual member.
2525 This used to avoid checking for virtual functions if basetype
2526 has no virtual functions, according to an earlier ANSI draft.
2527 With the final ISO C++ rules, such an optimization is
2528 incorrect: A pointer to a derived member can be static_cast
2529 to pointer-to-base-member, as long as the dynamic object
2530 later has the right member. */
2533 get_member_function_from_ptrfunc (instance_ptrptr, function)
2534 tree *instance_ptrptr;
2537 if (TREE_CODE (function) == OFFSET_REF)
2538 function = TREE_OPERAND (function, 1);
2540 if (TYPE_PTRMEMFUNC_P (TREE_TYPE (function)))
2542 tree idx, delta, e1, e2, e3, vtbl, basetype;
2543 tree fntype = TYPE_PTRMEMFUNC_FN_TYPE (TREE_TYPE (function));
2545 tree instance_ptr = *instance_ptrptr;
2546 tree instance_save_expr = 0;
2547 if (instance_ptr == error_mark_node)
2549 if (TREE_CODE (function) == PTRMEM_CST)
2551 /* Extracting the function address from a pmf is only
2552 allowed with -Wno-pmf-conversions. It only works for
2554 e1 = build_addr_func (PTRMEM_CST_MEMBER (function));
2555 e1 = convert (fntype, e1);
2560 error ("object missing in use of `%E'", function);
2561 return error_mark_node;
2565 if (TREE_SIDE_EFFECTS (instance_ptr))
2566 instance_ptr = instance_save_expr = save_expr (instance_ptr);
2568 if (TREE_SIDE_EFFECTS (function))
2569 function = save_expr (function);
2571 /* Start by extracting all the information from the PMF itself. */
2572 e3 = PFN_FROM_PTRMEMFUNC (function);
2573 delta = build_ptrmemfunc_access_expr (function, delta_identifier);
2574 idx = build1 (NOP_EXPR, vtable_index_type, e3);
2575 switch (TARGET_PTRMEMFUNC_VBIT_LOCATION)
2577 case ptrmemfunc_vbit_in_pfn:
2578 e1 = cp_build_binary_op (BIT_AND_EXPR, idx, integer_one_node);
2579 idx = cp_build_binary_op (MINUS_EXPR, idx, integer_one_node);
2582 case ptrmemfunc_vbit_in_delta:
2583 e1 = cp_build_binary_op (BIT_AND_EXPR, delta, integer_one_node);
2584 delta = cp_build_binary_op (RSHIFT_EXPR, delta, integer_one_node);
2591 /* Convert down to the right base before using the instance. First
2593 basetype = TYPE_METHOD_BASETYPE (TREE_TYPE (fntype));
2594 basetype = lookup_base (TREE_TYPE (TREE_TYPE (instance_ptr)),
2595 basetype, ba_check, NULL);
2596 instance_ptr = build_base_path (PLUS_EXPR, instance_ptr, basetype, 1);
2597 if (instance_ptr == error_mark_node)
2598 return error_mark_node;
2599 /* ...and then the delta in the PMF. */
2600 instance_ptr = build (PLUS_EXPR, TREE_TYPE (instance_ptr),
2601 instance_ptr, delta);
2603 /* Hand back the adjusted 'this' argument to our caller. */
2604 *instance_ptrptr = instance_ptr;
2606 /* Next extract the vtable pointer from the object. */
2607 vtbl = build1 (NOP_EXPR, build_pointer_type (vtbl_ptr_type_node),
2609 vtbl = build_indirect_ref (vtbl, NULL);
2611 /* Finally, extract the function pointer from the vtable. */
2612 e2 = fold (build (PLUS_EXPR, TREE_TYPE (vtbl), vtbl, idx));
2613 e2 = build_indirect_ref (e2, NULL);
2614 TREE_CONSTANT (e2) = 1;
2616 /* When using function descriptors, the address of the
2617 vtable entry is treated as a function pointer. */
2618 if (TARGET_VTABLE_USES_DESCRIPTORS)
2619 e2 = build1 (NOP_EXPR, TREE_TYPE (e2),
2620 build_unary_op (ADDR_EXPR, e2, /*noconvert=*/1));
2622 TREE_TYPE (e2) = TREE_TYPE (e3);
2623 e1 = build_conditional_expr (e1, e2, e3);
2625 /* Make sure this doesn't get evaluated first inside one of the
2626 branches of the COND_EXPR. */
2627 if (instance_save_expr)
2628 e1 = build (COMPOUND_EXPR, TREE_TYPE (e1),
2629 instance_save_expr, e1);
2637 build_function_call_real (function, params, require_complete, flags)
2638 tree function, params;
2639 int require_complete, flags;
2641 register tree fntype, fndecl;
2642 register tree value_type;
2643 register tree coerced_params;
2645 tree name = NULL_TREE, assembler_name = NULL_TREE;
2647 tree original = function;
2649 /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
2650 Strip such NOP_EXPRs, since FUNCTION is used in non-lvalue context. */
2651 if (TREE_CODE (function) == NOP_EXPR
2652 && TREE_TYPE (function) == TREE_TYPE (TREE_OPERAND (function, 0)))
2653 function = TREE_OPERAND (function, 0);
2655 if (TREE_CODE (function) == FUNCTION_DECL)
2657 name = DECL_NAME (function);
2658 assembler_name = DECL_ASSEMBLER_NAME (function);
2660 mark_used (function);
2663 /* Convert anything with function type to a pointer-to-function. */
2664 if (pedantic && DECL_MAIN_P (function))
2665 pedwarn ("ISO C++ forbids calling `::main' from within program");
2667 /* Differs from default_conversion by not setting TREE_ADDRESSABLE
2668 (because calling an inline function does not mean the function
2669 needs to be separately compiled). */
2671 if (DECL_INLINE (function))
2672 function = inline_conversion (function);
2674 function = build_addr_func (function);
2680 function = build_addr_func (function);
2683 if (function == error_mark_node)
2684 return error_mark_node;
2686 fntype = TREE_TYPE (function);
2688 if (TYPE_PTRMEMFUNC_P (fntype))
2690 error ("must use .* or ->* to call pointer-to-member function in `%E (...)'",
2692 return error_mark_node;
2695 is_method = (TREE_CODE (fntype) == POINTER_TYPE
2696 && TREE_CODE (TREE_TYPE (fntype)) == METHOD_TYPE);
2698 if (!((TREE_CODE (fntype) == POINTER_TYPE
2699 && TREE_CODE (TREE_TYPE (fntype)) == FUNCTION_TYPE)
2701 || TREE_CODE (function) == TEMPLATE_ID_EXPR))
2703 error ("`%E' cannot be used as a function", original);
2704 return error_mark_node;
2707 /* fntype now gets the type of function pointed to. */
2708 fntype = TREE_TYPE (fntype);
2710 /* Convert the parameters to the types declared in the
2711 function prototype, or apply default promotions. */
2713 if (flags & LOOKUP_COMPLAIN)
2714 coerced_params = convert_arguments (TYPE_ARG_TYPES (fntype),
2715 params, fndecl, LOOKUP_NORMAL);
2717 coerced_params = convert_arguments (TYPE_ARG_TYPES (fntype),
2720 if (coerced_params == error_mark_node)
2722 if (flags & LOOKUP_SPECULATIVELY)
2725 return error_mark_node;
2728 /* Check for errors in format strings. */
2731 check_function_format (NULL, TYPE_ATTRIBUTES (fntype), coerced_params);
2733 /* Recognize certain built-in functions so we can make tree-codes
2734 other than CALL_EXPR. We do this when it enables fold-const.c
2735 to do something useful. */
2737 if (TREE_CODE (function) == ADDR_EXPR
2738 && TREE_CODE (TREE_OPERAND (function, 0)) == FUNCTION_DECL
2739 && DECL_BUILT_IN (TREE_OPERAND (function, 0)))
2741 result = expand_tree_builtin (TREE_OPERAND (function, 0),
2742 params, coerced_params);
2747 /* Some built-in function calls will be evaluated at
2748 compile-time in fold (). */
2749 result = fold (build_call (function, coerced_params));
2750 value_type = TREE_TYPE (result);
2752 if (require_complete)
2754 if (TREE_CODE (value_type) == VOID_TYPE)
2756 result = require_complete_type (result);
2758 if (IS_AGGR_TYPE (value_type))
2759 result = build_cplus_new (value_type, result);
2760 return convert_from_reference (result);
2764 build_function_call (function, params)
2765 tree function, params;
2767 return build_function_call_real (function, params, 1, LOOKUP_NORMAL);
2770 /* Convert the actual parameter expressions in the list VALUES
2771 to the types in the list TYPELIST.
2772 If parmdecls is exhausted, or when an element has NULL as its type,
2773 perform the default conversions.
2775 NAME is an IDENTIFIER_NODE or 0. It is used only for error messages.
2777 This is also where warnings about wrong number of args are generated.
2779 Return a list of expressions for the parameters as converted.
2781 Both VALUES and the returned value are chains of TREE_LIST nodes
2782 with the elements of the list in the TREE_VALUE slots of those nodes.
2784 In C++, unspecified trailing parameters can be filled in with their
2785 default arguments, if such were specified. Do so here. */
2788 convert_arguments (typelist, values, fndecl, flags)
2789 tree typelist, values, fndecl;
2792 register tree typetail, valtail;
2793 register tree result = NULL_TREE;
2794 const char *called_thing = 0;
2797 /* Argument passing is always copy-initialization. */
2798 flags |= LOOKUP_ONLYCONVERTING;
2802 if (TREE_CODE (TREE_TYPE (fndecl)) == METHOD_TYPE)
2804 if (DECL_NAME (fndecl) == NULL_TREE
2805 || IDENTIFIER_HAS_TYPE_VALUE (DECL_NAME (fndecl)))
2806 called_thing = "constructor";
2808 called_thing = "member function";
2811 called_thing = "function";
2814 for (valtail = values, typetail = typelist;
2816 valtail = TREE_CHAIN (valtail), i++)
2818 register tree type = typetail ? TREE_VALUE (typetail) : 0;
2819 register tree val = TREE_VALUE (valtail);
2821 if (val == error_mark_node)
2822 return error_mark_node;
2824 if (type == void_type_node)
2828 cp_error_at ("too many arguments to %s `%+#D'", called_thing,
2830 error ("at this point in file");
2833 error ("too many arguments to function");
2834 /* In case anybody wants to know if this argument
2837 TREE_TYPE (tree_last (result)) = error_mark_node;
2841 if (TREE_CODE (val) == OFFSET_REF)
2842 val = resolve_offset_ref (val);
2844 /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
2845 Strip such NOP_EXPRs, since VAL is used in non-lvalue context. */
2846 if (TREE_CODE (val) == NOP_EXPR
2847 && TREE_TYPE (val) == TREE_TYPE (TREE_OPERAND (val, 0))
2848 && (type == 0 || TREE_CODE (type) != REFERENCE_TYPE))
2849 val = TREE_OPERAND (val, 0);
2851 if (type == 0 || TREE_CODE (type) != REFERENCE_TYPE)
2853 if (TREE_CODE (TREE_TYPE (val)) == ARRAY_TYPE
2854 || TREE_CODE (TREE_TYPE (val)) == FUNCTION_TYPE
2855 || TREE_CODE (TREE_TYPE (val)) == METHOD_TYPE)
2856 val = default_conversion (val);
2859 if (val == error_mark_node)
2860 return error_mark_node;
2864 /* Formal parm type is specified by a function prototype. */
2867 if (!COMPLETE_TYPE_P (complete_type (type)))
2869 error ("parameter type of called function is incomplete");
2874 parmval = convert_for_initialization
2875 (NULL_TREE, type, val, flags,
2876 "argument passing", fndecl, i);
2877 parmval = convert_for_arg_passing (type, parmval);
2880 if (parmval == error_mark_node)
2881 return error_mark_node;
2883 result = tree_cons (NULL_TREE, parmval, result);
2887 if (TREE_CODE (TREE_TYPE (val)) == REFERENCE_TYPE)
2888 val = convert_from_reference (val);
2890 if (fndecl && DECL_BUILT_IN (fndecl)
2891 && DECL_FUNCTION_CODE (fndecl) == BUILT_IN_CONSTANT_P)
2892 /* Don't do ellipsis conversion for __built_in_constant_p
2893 as this will result in spurious warnings for non-POD
2895 val = require_complete_type (val);
2897 val = convert_arg_to_ellipsis (val);
2899 result = tree_cons (NULL_TREE, val, result);
2903 typetail = TREE_CHAIN (typetail);
2906 if (typetail != 0 && typetail != void_list_node)
2908 /* See if there are default arguments that can be used */
2909 if (TREE_PURPOSE (typetail))
2911 for (; typetail != void_list_node; ++i)
2914 = convert_default_arg (TREE_VALUE (typetail),
2915 TREE_PURPOSE (typetail),
2918 if (parmval == error_mark_node)
2919 return error_mark_node;
2921 result = tree_cons (0, parmval, result);
2922 typetail = TREE_CHAIN (typetail);
2923 /* ends with `...'. */
2924 if (typetail == NULL_TREE)
2932 cp_error_at ("too few arguments to %s `%+#D'",
2933 called_thing, fndecl);
2934 error ("at this point in file");
2937 error ("too few arguments to function");
2938 return error_mark_list;
2942 return nreverse (result);
2945 /* Build a binary-operation expression, after performing default
2946 conversions on the operands. CODE is the kind of expression to build. */
2949 build_x_binary_op (code, arg1, arg2)
2950 enum tree_code code;
2953 if (processing_template_decl)
2954 return build_min_nt (code, arg1, arg2);
2956 return build_new_op (code, LOOKUP_NORMAL, arg1, arg2, NULL_TREE);
2959 /* Build a binary-operation expression without default conversions.
2960 CODE is the kind of expression to build.
2961 This function differs from `build' in several ways:
2962 the data type of the result is computed and recorded in it,
2963 warnings are generated if arg data types are invalid,
2964 special handling for addition and subtraction of pointers is known,
2965 and some optimization is done (operations on narrow ints
2966 are done in the narrower type when that gives the same result).
2967 Constant folding is also done before the result is returned.
2969 Note that the operands will never have enumeral types
2970 because either they have just had the default conversions performed
2971 or they have both just been converted to some other type in which
2972 the arithmetic is to be done.
2974 C++: must do special pointer arithmetic when implementing
2975 multiple inheritance, and deal with pointer to member functions. */
2978 build_binary_op (code, orig_op0, orig_op1, convert_p)
2979 enum tree_code code;
2980 tree orig_op0, orig_op1;
2981 int convert_p ATTRIBUTE_UNUSED;
2984 register enum tree_code code0, code1;
2987 /* Expression code to give to the expression when it is built.
2988 Normally this is CODE, which is what the caller asked for,
2989 but in some special cases we change it. */
2990 register enum tree_code resultcode = code;
2992 /* Data type in which the computation is to be performed.
2993 In the simplest cases this is the common type of the arguments. */
2994 register tree result_type = NULL;
2996 /* Nonzero means operands have already been type-converted
2997 in whatever way is necessary.
2998 Zero means they need to be converted to RESULT_TYPE. */
3001 /* Nonzero means create the expression with this type, rather than
3003 tree build_type = 0;
3005 /* Nonzero means after finally constructing the expression
3006 convert it to this type. */
3007 tree final_type = 0;
3009 /* Nonzero if this is an operation like MIN or MAX which can
3010 safely be computed in short if both args are promoted shorts.
3011 Also implies COMMON.
3012 -1 indicates a bitwise operation; this makes a difference
3013 in the exact conditions for when it is safe to do the operation
3014 in a narrower mode. */
3017 /* Nonzero if this is a comparison operation;
3018 if both args are promoted shorts, compare the original shorts.
3019 Also implies COMMON. */
3020 int short_compare = 0;
3022 /* Nonzero if this is a right-shift operation, which can be computed on the
3023 original short and then promoted if the operand is a promoted short. */
3024 int short_shift = 0;
3026 /* Nonzero means set RESULT_TYPE to the common type of the args. */
3029 /* Apply default conversions. */
3033 if (code == TRUTH_AND_EXPR || code == TRUTH_ANDIF_EXPR
3034 || code == TRUTH_OR_EXPR || code == TRUTH_ORIF_EXPR
3035 || code == TRUTH_XOR_EXPR)
3037 if (!really_overloaded_fn (op0))
3038 op0 = decay_conversion (op0);
3039 if (!really_overloaded_fn (op1))
3040 op1 = decay_conversion (op1);
3044 if (!really_overloaded_fn (op0))
3045 op0 = default_conversion (op0);
3046 if (!really_overloaded_fn (op1))
3047 op1 = default_conversion (op1);
3050 /* Strip NON_LVALUE_EXPRs, etc., since we aren't using as an lvalue. */
3051 STRIP_TYPE_NOPS (op0);
3052 STRIP_TYPE_NOPS (op1);
3054 /* DTRT if one side is an overloaded function, but complain about it. */
3055 if (type_unknown_p (op0))
3057 tree t = instantiate_type (TREE_TYPE (op1), op0, tf_none);
3058 if (t != error_mark_node)
3060 pedwarn ("assuming cast to type `%T' from overloaded function",
3065 if (type_unknown_p (op1))
3067 tree t = instantiate_type (TREE_TYPE (op0), op1, tf_none);
3068 if (t != error_mark_node)
3070 pedwarn ("assuming cast to type `%T' from overloaded function",
3076 type0 = TREE_TYPE (op0);
3077 type1 = TREE_TYPE (op1);
3079 /* The expression codes of the data types of the arguments tell us
3080 whether the arguments are integers, floating, pointers, etc. */
3081 code0 = TREE_CODE (type0);
3082 code1 = TREE_CODE (type1);
3084 /* If an error was already reported for one of the arguments,
3085 avoid reporting another error. */
3087 if (code0 == ERROR_MARK || code1 == ERROR_MARK)
3088 return error_mark_node;
3093 /* Handle the pointer + int case. */
3094 if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
3095 return cp_pointer_int_sum (PLUS_EXPR, op0, op1);
3096 else if (code1 == POINTER_TYPE && code0 == INTEGER_TYPE)
3097 return cp_pointer_int_sum (PLUS_EXPR, op1, op0);
3103 /* Subtraction of two similar pointers.
3104 We must subtract them as integers, then divide by object size. */
3105 if (code0 == POINTER_TYPE && code1 == POINTER_TYPE
3106 && comp_target_types (type0, type1, 1))
3107 return pointer_diff (op0, op1, common_type (type0, type1));
3108 /* Handle pointer minus int. Just like pointer plus int. */
3109 else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
3110 return cp_pointer_int_sum (MINUS_EXPR, op0, op1);
3119 case TRUNC_DIV_EXPR:
3121 case FLOOR_DIV_EXPR:
3122 case ROUND_DIV_EXPR:
3123 case EXACT_DIV_EXPR:
3124 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE
3125 || code0 == COMPLEX_TYPE)
3126 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
3127 || code1 == COMPLEX_TYPE))
3129 if (TREE_CODE (op1) == INTEGER_CST && integer_zerop (op1))
3130 warning ("division by zero in `%E / 0'", op0);
3131 else if (TREE_CODE (op1) == REAL_CST && real_zerop (op1))
3132 warning ("division by zero in `%E / 0.'", op0);
3134 if (!(code0 == INTEGER_TYPE && code1 == INTEGER_TYPE))
3135 resultcode = RDIV_EXPR;
3137 /* When dividing two signed integers, we have to promote to int.
3138 unless we divide by a constant != -1. Note that default
3139 conversion will have been performed on the operands at this
3140 point, so we have to dig out the original type to find out if
3142 shorten = ((TREE_CODE (op0) == NOP_EXPR
3143 && TREE_UNSIGNED (TREE_TYPE (TREE_OPERAND (op0, 0))))
3144 || (TREE_CODE (op1) == INTEGER_CST
3145 && ! integer_all_onesp (op1)));
3152 case BIT_ANDTC_EXPR:
3155 if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
3159 case TRUNC_MOD_EXPR:
3160 case FLOOR_MOD_EXPR:
3161 if (code1 == INTEGER_TYPE && integer_zerop (op1))
3162 warning ("division by zero in `%E %% 0'", op0);
3163 else if (code1 == REAL_TYPE && real_zerop (op1))
3164 warning ("division by zero in `%E %% 0.'", op0);
3166 if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
3168 /* Although it would be tempting to shorten always here, that loses
3169 on some targets, since the modulo instruction is undefined if the
3170 quotient can't be represented in the computation mode. We shorten
3171 only if unsigned or if dividing by something we know != -1. */
3172 shorten = ((TREE_CODE (op0) == NOP_EXPR
3173 && TREE_UNSIGNED (TREE_TYPE (TREE_OPERAND (op0, 0))))
3174 || (TREE_CODE (op1) == INTEGER_CST
3175 && ! integer_all_onesp (op1)));
3180 case TRUTH_ANDIF_EXPR:
3181 case TRUTH_ORIF_EXPR:
3182 case TRUTH_AND_EXPR:
3184 result_type = boolean_type_node;
3187 /* Shift operations: result has same type as first operand;
3188 always convert second operand to int.
3189 Also set SHORT_SHIFT if shifting rightward. */
3192 if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
3194 result_type = type0;
3195 if (TREE_CODE (op1) == INTEGER_CST)
3197 if (tree_int_cst_lt (op1, integer_zero_node))
3198 warning ("right shift count is negative");
3201 if (! integer_zerop (op1))
3203 if (compare_tree_int (op1, TYPE_PRECISION (type0)) >= 0)
3204 warning ("right shift count >= width of type");
3207 /* Convert the shift-count to an integer, regardless of
3208 size of value being shifted. */
3209 if (TYPE_MAIN_VARIANT (TREE_TYPE (op1)) != integer_type_node)
3210 op1 = cp_convert (integer_type_node, op1);
3211 /* Avoid converting op1 to result_type later. */
3217 if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
3219 result_type = type0;
3220 if (TREE_CODE (op1) == INTEGER_CST)
3222 if (tree_int_cst_lt (op1, integer_zero_node))
3223 warning ("left shift count is negative");
3224 else if (compare_tree_int (op1, TYPE_PRECISION (type0)) >= 0)
3225 warning ("left shift count >= width of type");
3227 /* Convert the shift-count to an integer, regardless of
3228 size of value being shifted. */
3229 if (TYPE_MAIN_VARIANT (TREE_TYPE (op1)) != integer_type_node)
3230 op1 = cp_convert (integer_type_node, op1);
3231 /* Avoid converting op1 to result_type later. */
3238 if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
3240 result_type = type0;
3241 if (TREE_CODE (op1) == INTEGER_CST)
3243 if (tree_int_cst_lt (op1, integer_zero_node))
3244 warning ("%s rotate count is negative",
3245 (code == LROTATE_EXPR) ? "left" : "right");
3246 else if (compare_tree_int (op1, TYPE_PRECISION (type0)) >= 0)
3247 warning ("%s rotate count >= width of type",
3248 (code == LROTATE_EXPR) ? "left" : "right");
3250 /* Convert the shift-count to an integer, regardless of
3251 size of value being shifted. */
3252 if (TYPE_MAIN_VARIANT (TREE_TYPE (op1)) != integer_type_node)
3253 op1 = cp_convert (integer_type_node, op1);
3259 if (warn_float_equal && (code0 == REAL_TYPE || code1 == REAL_TYPE))
3260 warning ("comparing floating point with == or != is unsafe");
3262 build_type = boolean_type_node;
3263 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE
3264 || code0 == COMPLEX_TYPE)
3265 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
3266 || code1 == COMPLEX_TYPE))
3268 else if (code0 == POINTER_TYPE && code1 == POINTER_TYPE)
3269 result_type = composite_pointer_type (type0, type1, op0, op1,
3271 else if (code0 == POINTER_TYPE && null_ptr_cst_p (op1))
3272 result_type = type0;
3273 else if (code1 == POINTER_TYPE && null_ptr_cst_p (op0))
3274 result_type = type1;
3275 else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
3277 result_type = type0;
3278 error ("ISO C++ forbids comparison between pointer and integer");
3280 else if (code0 == INTEGER_TYPE && code1 == POINTER_TYPE)
3282 result_type = type1;
3283 error ("ISO C++ forbids comparison between pointer and integer");
3285 else if (TYPE_PTRMEMFUNC_P (type0) && null_ptr_cst_p (op1))
3287 op0 = build_ptrmemfunc_access_expr (op0, pfn_identifier);
3288 op1 = cp_convert (TREE_TYPE (op0), integer_zero_node);
3289 result_type = TREE_TYPE (op0);
3291 else if (TYPE_PTRMEMFUNC_P (type1) && null_ptr_cst_p (op0))
3292 return cp_build_binary_op (code, op1, op0);
3293 else if (TYPE_PTRMEMFUNC_P (type0) && TYPE_PTRMEMFUNC_P (type1)
3294 && same_type_p (type0, type1))
3296 /* E will be the final comparison. */
3298 /* E1 and E2 are for scratch. */
3306 if (TREE_SIDE_EFFECTS (op0))
3307 op0 = save_expr (op0);
3308 if (TREE_SIDE_EFFECTS (op1))
3309 op1 = save_expr (op1);
3314 && (!op0.pfn || op0.delta == op1.delta))
3316 The reason for the `!op0.pfn' bit is that a NULL
3317 pointer-to-member is any member with a zero PFN; the
3318 DELTA field is unspecified. */
3319 pfn0 = pfn_from_ptrmemfunc (op0);
3320 pfn1 = pfn_from_ptrmemfunc (op1);
3321 delta0 = build_ptrmemfunc_access_expr (op0,
3323 delta1 = build_ptrmemfunc_access_expr (op1,
3325 e1 = cp_build_binary_op (EQ_EXPR, delta0, delta1);
3326 e2 = cp_build_binary_op (EQ_EXPR,
3328 cp_convert (TREE_TYPE (pfn0),
3329 integer_zero_node));
3330 e1 = cp_build_binary_op (TRUTH_ORIF_EXPR, e1, e2);
3331 e2 = build (EQ_EXPR, boolean_type_node, pfn0, pfn1);
3332 e = cp_build_binary_op (TRUTH_ANDIF_EXPR, e2, e1);
3333 if (code == EQ_EXPR)
3335 return cp_build_binary_op (EQ_EXPR, e, integer_zero_node);
3337 else if ((TYPE_PTRMEMFUNC_P (type0)
3338 && same_type_p (TYPE_PTRMEMFUNC_FN_TYPE (type0), type1))
3339 || (TYPE_PTRMEMFUNC_P (type1)
3340 && same_type_p (TYPE_PTRMEMFUNC_FN_TYPE (type1), type0)))
3346 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE)
3347 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE))
3349 else if (code0 == POINTER_TYPE && code1 == POINTER_TYPE)
3350 result_type = composite_pointer_type (type0, type1, op0, op1,
3358 build_type = boolean_type_node;
3359 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE)
3360 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE))
3362 else if (code0 == POINTER_TYPE && code1 == POINTER_TYPE)
3363 result_type = composite_pointer_type (type0, type1, op0, op1,
3365 else if (code0 == POINTER_TYPE && TREE_CODE (op1) == INTEGER_CST
3366 && integer_zerop (op1))
3367 result_type = type0;
3368 else if (code1 == POINTER_TYPE && TREE_CODE (op0) == INTEGER_CST
3369 && integer_zerop (op0))
3370 result_type = type1;
3371 else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
3373 result_type = type0;
3374 pedwarn ("ISO C++ forbids comparison between pointer and integer");
3376 else if (code0 == INTEGER_TYPE && code1 == POINTER_TYPE)
3378 result_type = type1;
3379 pedwarn ("ISO C++ forbids comparison between pointer and integer");
3383 case UNORDERED_EXPR:
3390 build_type = integer_type_node;
3391 if (code0 != REAL_TYPE || code1 != REAL_TYPE)
3393 error ("unordered comparison on non-floating point argument");
3394 return error_mark_node;
3403 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE || code0 == COMPLEX_TYPE)
3405 (code1 == INTEGER_TYPE || code1 == REAL_TYPE || code1 == COMPLEX_TYPE))
3407 int none_complex = (code0 != COMPLEX_TYPE && code1 != COMPLEX_TYPE);
3409 if (shorten || common || short_compare)
3410 result_type = common_type (type0, type1);
3412 /* For certain operations (which identify themselves by shorten != 0)
3413 if both args were extended from the same smaller type,
3414 do the arithmetic in that type and then extend.
3416 shorten !=0 and !=1 indicates a bitwise operation.
3417 For them, this optimization is safe only if
3418 both args are zero-extended or both are sign-extended.
3419 Otherwise, we might change the result.
3420 Eg, (short)-1 | (unsigned short)-1 is (int)-1
3421 but calculated in (unsigned short) it would be (unsigned short)-1. */
3423 if (shorten && none_complex)
3425 int unsigned0, unsigned1;
3426 tree arg0 = get_narrower (op0, &unsigned0);
3427 tree arg1 = get_narrower (op1, &unsigned1);
3428 /* UNS is 1 if the operation to be done is an unsigned one. */
3429 int uns = TREE_UNSIGNED (result_type);
3432 final_type = result_type;
3434 /* Handle the case that OP0 does not *contain* a conversion
3435 but it *requires* conversion to FINAL_TYPE. */
3437 if (op0 == arg0 && TREE_TYPE (op0) != final_type)
3438 unsigned0 = TREE_UNSIGNED (TREE_TYPE (op0));
3439 if (op1 == arg1 && TREE_TYPE (op1) != final_type)
3440 unsigned1 = TREE_UNSIGNED (TREE_TYPE (op1));
3442 /* Now UNSIGNED0 is 1 if ARG0 zero-extends to FINAL_TYPE. */
3444 /* For bitwise operations, signedness of nominal type
3445 does not matter. Consider only how operands were extended. */
3449 /* Note that in all three cases below we refrain from optimizing
3450 an unsigned operation on sign-extended args.
3451 That would not be valid. */
3453 /* Both args variable: if both extended in same way
3454 from same width, do it in that width.
3455 Do it unsigned if args were zero-extended. */
3456 if ((TYPE_PRECISION (TREE_TYPE (arg0))
3457 < TYPE_PRECISION (result_type))
3458 && (TYPE_PRECISION (TREE_TYPE (arg1))
3459 == TYPE_PRECISION (TREE_TYPE (arg0)))
3460 && unsigned0 == unsigned1
3461 && (unsigned0 || !uns))
3462 result_type = c_common_signed_or_unsigned_type
3463 (unsigned0, common_type (TREE_TYPE (arg0), TREE_TYPE (arg1)));
3464 else if (TREE_CODE (arg0) == INTEGER_CST
3465 && (unsigned1 || !uns)
3466 && (TYPE_PRECISION (TREE_TYPE (arg1))
3467 < TYPE_PRECISION (result_type))
3468 && (type = c_common_signed_or_unsigned_type
3469 (unsigned1, TREE_TYPE (arg1)),
3470 int_fits_type_p (arg0, type)))
3472 else if (TREE_CODE (arg1) == INTEGER_CST
3473 && (unsigned0 || !uns)
3474 && (TYPE_PRECISION (TREE_TYPE (arg0))
3475 < TYPE_PRECISION (result_type))
3476 && (type = c_common_signed_or_unsigned_type
3477 (unsigned0, TREE_TYPE (arg0)),
3478 int_fits_type_p (arg1, type)))
3482 /* Shifts can be shortened if shifting right. */
3487 tree arg0 = get_narrower (op0, &unsigned_arg);
3489 final_type = result_type;
3491 if (arg0 == op0 && final_type == TREE_TYPE (op0))
3492 unsigned_arg = TREE_UNSIGNED (TREE_TYPE (op0));
3494 if (TYPE_PRECISION (TREE_TYPE (arg0)) < TYPE_PRECISION (result_type)
3495 /* We can shorten only if the shift count is less than the
3496 number of bits in the smaller type size. */
3497 && compare_tree_int (op1, TYPE_PRECISION (TREE_TYPE (arg0))) < 0
3498 /* If arg is sign-extended and then unsigned-shifted,
3499 we can simulate this with a signed shift in arg's type
3500 only if the extended result is at least twice as wide
3501 as the arg. Otherwise, the shift could use up all the
3502 ones made by sign-extension and bring in zeros.
3503 We can't optimize that case at all, but in most machines
3504 it never happens because available widths are 2**N. */
3505 && (!TREE_UNSIGNED (final_type)
3507 || (((unsigned) 2 * TYPE_PRECISION (TREE_TYPE (arg0)))
3508 <= TYPE_PRECISION (result_type))))
3510 /* Do an unsigned shift if the operand was zero-extended. */
3512 = c_common_signed_or_unsigned_type (unsigned_arg,
3514 /* Convert value-to-be-shifted to that type. */
3515 if (TREE_TYPE (op0) != result_type)
3516 op0 = cp_convert (result_type, op0);
3521 /* Comparison operations are shortened too but differently.
3522 They identify themselves by setting short_compare = 1. */
3526 /* Don't write &op0, etc., because that would prevent op0
3527 from being kept in a register.
3528 Instead, make copies of the our local variables and
3529 pass the copies by reference, then copy them back afterward. */
3530 tree xop0 = op0, xop1 = op1, xresult_type = result_type;
3531 enum tree_code xresultcode = resultcode;
3533 = shorten_compare (&xop0, &xop1, &xresult_type, &xresultcode);
3535 return cp_convert (boolean_type_node, val);
3536 op0 = xop0, op1 = xop1;
3538 resultcode = xresultcode;
3541 if ((short_compare || code == MIN_EXPR || code == MAX_EXPR)
3542 && warn_sign_compare)
3544 int op0_signed = ! TREE_UNSIGNED (TREE_TYPE (orig_op0));
3545 int op1_signed = ! TREE_UNSIGNED (TREE_TYPE (orig_op1));
3547 int unsignedp0, unsignedp1;
3548 tree primop0 = get_narrower (op0, &unsignedp0);
3549 tree primop1 = get_narrower (op1, &unsignedp1);
3551 /* Check for comparison of different enum types. */
3552 if (TREE_CODE (TREE_TYPE (orig_op0)) == ENUMERAL_TYPE
3553 && TREE_CODE (TREE_TYPE (orig_op1)) == ENUMERAL_TYPE
3554 && TYPE_MAIN_VARIANT (TREE_TYPE (orig_op0))
3555 != TYPE_MAIN_VARIANT (TREE_TYPE (orig_op1)))
3557 warning ("comparison between types `%#T' and `%#T'",
3558 TREE_TYPE (orig_op0), TREE_TYPE (orig_op1));
3561 /* Give warnings for comparisons between signed and unsigned
3562 quantities that may fail. */
3563 /* Do the checking based on the original operand trees, so that
3564 casts will be considered, but default promotions won't be. */
3566 /* Do not warn if the comparison is being done in a signed type,
3567 since the signed type will only be chosen if it can represent
3568 all the values of the unsigned type. */
3569 if (! TREE_UNSIGNED (result_type))
3571 /* Do not warn if both operands are unsigned. */
3572 else if (op0_signed == op1_signed)
3574 /* Do not warn if the signed quantity is an unsuffixed
3575 integer literal (or some static constant expression
3576 involving such literals or a conditional expression
3577 involving such literals) and it is non-negative. */
3578 else if ((op0_signed && tree_expr_nonnegative_p (orig_op0))
3579 || (op1_signed && tree_expr_nonnegative_p (orig_op1)))
3581 /* Do not warn if the comparison is an equality operation,
3582 the unsigned quantity is an integral constant and it does
3583 not use the most significant bit of result_type. */
3584 else if ((resultcode == EQ_EXPR || resultcode == NE_EXPR)
3585 && ((op0_signed && TREE_CODE (orig_op1) == INTEGER_CST
3586 && int_fits_type_p (orig_op1, c_common_signed_type
3588 || (op1_signed && TREE_CODE (orig_op0) == INTEGER_CST
3589 && int_fits_type_p (orig_op0, c_common_signed_type
3593 warning ("comparison between signed and unsigned integer expressions");
3595 /* Warn if two unsigned values are being compared in a size
3596 larger than their original size, and one (and only one) is the
3597 result of a `~' operator. This comparison will always fail.
3599 Also warn if one operand is a constant, and the constant does not
3600 have all bits set that are set in the ~ operand when it is
3603 if ((TREE_CODE (primop0) == BIT_NOT_EXPR)
3604 ^ (TREE_CODE (primop1) == BIT_NOT_EXPR))
3606 if (TREE_CODE (primop0) == BIT_NOT_EXPR)
3607 primop0 = get_narrower (TREE_OPERAND (op0, 0), &unsignedp0);
3608 if (TREE_CODE (primop1) == BIT_NOT_EXPR)
3609 primop1 = get_narrower (TREE_OPERAND (op1, 0), &unsignedp1);
3611 if (host_integerp (primop0, 0) || host_integerp (primop1, 0))
3614 HOST_WIDE_INT constant, mask;
3618 if (host_integerp (primop0, 0))
3621 unsignedp = unsignedp1;
3622 constant = tree_low_cst (primop0, 0);
3627 unsignedp = unsignedp0;
3628 constant = tree_low_cst (primop1, 0);
3631 bits = TYPE_PRECISION (TREE_TYPE (primop));
3632 if (bits < TYPE_PRECISION (result_type)
3633 && bits < HOST_BITS_PER_LONG && unsignedp)
3635 mask = (~ (HOST_WIDE_INT) 0) << bits;
3636 if ((mask & constant) != mask)
3637 warning ("comparison of promoted ~unsigned with constant");
3640 else if (unsignedp0 && unsignedp1
3641 && (TYPE_PRECISION (TREE_TYPE (primop0))
3642 < TYPE_PRECISION (result_type))
3643 && (TYPE_PRECISION (TREE_TYPE (primop1))
3644 < TYPE_PRECISION (result_type)))
3645 warning ("comparison of promoted ~unsigned with unsigned");
3650 /* At this point, RESULT_TYPE must be nonzero to avoid an error message.
3651 If CONVERTED is zero, both args will be converted to type RESULT_TYPE.
3652 Then the expression will be built.
3653 It will be given type FINAL_TYPE if that is nonzero;
3654 otherwise, it will be given type RESULT_TYPE. */
3658 error ("invalid operands of types `%T' and `%T' to binary `%O'",
3659 TREE_TYPE (orig_op0), TREE_TYPE (orig_op1), code);
3660 return error_mark_node;
3663 /* Issue warnings about peculiar, but valid, uses of NULL. */
3664 if (/* It's reasonable to use pointer values as operands of &&
3665 and ||, so NULL is no exception. */
3666 !(code == TRUTH_ANDIF_EXPR || code == TRUTH_ORIF_EXPR)
3667 && (/* If OP0 is NULL and OP1 is not a pointer, or vice versa. */
3668 (orig_op0 == null_node
3669 && TREE_CODE (TREE_TYPE (op1)) != POINTER_TYPE)
3670 /* Or vice versa. */
3671 || (orig_op1 == null_node
3672 && TREE_CODE (TREE_TYPE (op0)) != POINTER_TYPE)
3673 /* Or, both are NULL and the operation was not a comparison. */
3674 || (orig_op0 == null_node && orig_op1 == null_node
3675 && code != EQ_EXPR && code != NE_EXPR)))
3676 /* Some sort of arithmetic operation involving NULL was
3677 performed. Note that pointer-difference and pointer-addition
3678 have already been handled above, and so we don't end up here in
3680 warning ("NULL used in arithmetic");
3684 if (TREE_TYPE (op0) != result_type)
3685 op0 = cp_convert (result_type, op0);
3686 if (TREE_TYPE (op1) != result_type)
3687 op1 = cp_convert (result_type, op1);
3689 if (op0 == error_mark_node || op1 == error_mark_node)
3690 return error_mark_node;
3693 if (build_type == NULL_TREE)
3694 build_type = result_type;
3697 register tree result = build (resultcode, build_type, op0, op1);
3698 register tree folded;
3700 folded = fold (result);
3701 if (folded == result)
3702 TREE_CONSTANT (folded) = TREE_CONSTANT (op0) & TREE_CONSTANT (op1);
3703 if (final_type != 0)
3704 return cp_convert (final_type, folded);
3709 /* Return a tree for the sum or difference (RESULTCODE says which)
3710 of pointer PTROP and integer INTOP. */
3713 cp_pointer_int_sum (resultcode, ptrop, intop)
3714 enum tree_code resultcode;
3715 register tree ptrop, intop;
3717 tree res_type = TREE_TYPE (ptrop);
3719 /* pointer_int_sum() uses size_in_bytes() on the TREE_TYPE(res_type)
3720 in certain circumstance (when it's valid to do so). So we need
3721 to make sure it's complete. We don't need to check here, if we
3722 can actually complete it at all, as those checks will be done in
3723 pointer_int_sum() anyway. */
3724 complete_type (TREE_TYPE (res_type));
3726 return pointer_int_sum (resultcode, ptrop, fold (intop));
3729 /* Return a tree for the difference of pointers OP0 and OP1.
3730 The resulting tree has type int. */
3733 pointer_diff (op0, op1, ptrtype)
3734 register tree op0, op1;
3735 register tree ptrtype;
3737 register tree result, folded;
3738 tree restype = ptrdiff_type_node;
3739 tree target_type = TREE_TYPE (ptrtype);
3741 if (!complete_type_or_else (target_type, NULL_TREE))
3742 return error_mark_node;
3744 if (pedantic || warn_pointer_arith)
3746 if (TREE_CODE (target_type) == VOID_TYPE)
3747 pedwarn ("ISO C++ forbids using pointer of type `void *' in subtraction");
3748 if (TREE_CODE (target_type) == FUNCTION_TYPE)
3749 pedwarn ("ISO C++ forbids using pointer to a function in subtraction");
3750 if (TREE_CODE (target_type) == METHOD_TYPE)
3751 pedwarn ("ISO C++ forbids using pointer to a method in subtraction");
3752 if (TREE_CODE (target_type) == OFFSET_TYPE)
3753 pedwarn ("ISO C++ forbids using pointer to a member in subtraction");
3756 /* First do the subtraction as integers;
3757 then drop through to build the divide operator. */
3759 op0 = cp_build_binary_op (MINUS_EXPR,
3760 cp_convert (restype, op0),
3761 cp_convert (restype, op1));
3763 /* This generates an error if op1 is a pointer to an incomplete type. */
3764 if (!COMPLETE_TYPE_P (TREE_TYPE (TREE_TYPE (op1))))
3765 error ("invalid use of a pointer to an incomplete type in pointer arithmetic");
3767 op1 = ((TREE_CODE (target_type) == VOID_TYPE
3768 || TREE_CODE (target_type) == FUNCTION_TYPE
3769 || TREE_CODE (target_type) == METHOD_TYPE
3770 || TREE_CODE (target_type) == OFFSET_TYPE)
3772 : size_in_bytes (target_type));
3774 /* Do the division. */
3776 result = build (EXACT_DIV_EXPR, restype, op0, cp_convert (restype, op1));
3778 folded = fold (result);
3779 if (folded == result)
3780 TREE_CONSTANT (folded) = TREE_CONSTANT (op0) & TREE_CONSTANT (op1);
3784 /* Construct and perhaps optimize a tree representation
3785 for a unary operation. CODE, a tree_code, specifies the operation
3786 and XARG is the operand. */
3789 build_x_unary_op (code, xarg)
3790 enum tree_code code;
3796 if (processing_template_decl)
3797 return build_min_nt (code, xarg, NULL_TREE);
3799 /* & rec, on incomplete RECORD_TYPEs is the simple opr &, not an
3801 if (code == ADDR_EXPR
3802 && TREE_CODE (xarg) != TEMPLATE_ID_EXPR
3803 && ((IS_AGGR_TYPE_CODE (TREE_CODE (TREE_TYPE (xarg)))
3804 && !COMPLETE_TYPE_P (TREE_TYPE (xarg)))
3805 || (TREE_CODE (xarg) == OFFSET_REF)))
3806 /* don't look for a function */;
3811 rval = build_new_op (code, LOOKUP_NORMAL, xarg,
3812 NULL_TREE, NULL_TREE);
3813 if (rval || code != ADDR_EXPR)
3816 if (code == ADDR_EXPR)
3818 /* A pointer to member-function can be formed only by saying
3820 if (!flag_ms_extensions && TREE_CODE (TREE_TYPE (xarg)) == METHOD_TYPE
3821 && (TREE_CODE (xarg) != OFFSET_REF || !PTRMEM_OK_P (xarg)))
3823 if (TREE_CODE (xarg) != OFFSET_REF)
3825 error ("invalid use of '%E' to form a pointer-to-member-function. Use a qualified-id.",
3827 return error_mark_node;
3831 error ("parenthesis around '%E' cannot be used to form a pointer-to-member-function",
3833 PTRMEM_OK_P (xarg) = 1;
3837 if (TREE_CODE (xarg) == OFFSET_REF)
3839 ptrmem = PTRMEM_OK_P (xarg);
3841 if (!ptrmem && !flag_ms_extensions
3842 && TREE_CODE (TREE_TYPE (TREE_OPERAND (xarg, 1))) == METHOD_TYPE)
3844 /* A single non-static member, make sure we don't allow a
3845 pointer-to-member. */
3846 xarg = build (OFFSET_REF, TREE_TYPE (xarg),
3847 TREE_OPERAND (xarg, 0),
3848 ovl_cons (TREE_OPERAND (xarg, 1), NULL_TREE));
3849 PTRMEM_OK_P (xarg) = ptrmem;
3853 else if (TREE_CODE (xarg) == TARGET_EXPR)
3854 warning ("taking address of temporary");
3856 exp = build_unary_op (code, xarg, 0);
3857 if (TREE_CODE (exp) == ADDR_EXPR)
3858 PTRMEM_OK_P (exp) = ptrmem;
3863 /* Like c_common_truthvalue_conversion, but handle pointer-to-member
3864 constants, where a null value is represented by an INTEGER_CST of
3868 cp_truthvalue_conversion (expr)
3871 tree type = TREE_TYPE (expr);
3872 if (TYPE_PTRMEM_P (type))
3873 return build_binary_op (NE_EXPR, expr, integer_zero_node, 1);
3875 return c_common_truthvalue_conversion (expr);
3878 /* Just like cp_truthvalue_conversion, but we want a CLEANUP_POINT_EXPR. */
3881 condition_conversion (expr)
3885 if (processing_template_decl)
3887 if (TREE_CODE (expr) == OFFSET_REF)
3888 expr = resolve_offset_ref (expr);
3889 t = perform_implicit_conversion (boolean_type_node, expr);
3890 t = fold (build1 (CLEANUP_POINT_EXPR, boolean_type_node, t));
3894 /* C++: Must handle pointers to members.
3896 Perhaps type instantiation should be extended to handle conversion
3897 from aggregates to types we don't yet know we want? (Or are those
3898 cases typically errors which should be reported?)
3900 NOCONVERT nonzero suppresses the default promotions
3901 (such as from short to int). */
3904 build_unary_op (code, xarg, noconvert)
3905 enum tree_code code;
3909 /* No default_conversion here. It causes trouble for ADDR_EXPR. */
3910 register tree arg = xarg;
3911 register tree argtype = 0;
3912 const char *errstring = NULL;
3915 if (arg == error_mark_node)
3916 return error_mark_node;
3921 /* This is used for unary plus, because a CONVERT_EXPR
3922 is enough to prevent anybody from looking inside for
3923 associativity, but won't generate any code. */
3924 if (!(arg = build_expr_type_conversion
3925 (WANT_ARITH | WANT_ENUM | WANT_POINTER, arg, 1)))
3926 errstring = "wrong type argument to unary plus";
3930 arg = default_conversion (arg);
3931 arg = build1 (NON_LVALUE_EXPR, TREE_TYPE (arg), arg);
3932 TREE_CONSTANT (arg) = TREE_CONSTANT (TREE_OPERAND (arg, 0));
3937 if (!(arg = build_expr_type_conversion (WANT_ARITH | WANT_ENUM, arg, 1)))
3938 errstring = "wrong type argument to unary minus";
3939 else if (!noconvert)
3940 arg = default_conversion (arg);
3944 if (TREE_CODE (TREE_TYPE (arg)) == COMPLEX_TYPE)
3948 arg = default_conversion (arg);
3950 else if (!(arg = build_expr_type_conversion (WANT_INT | WANT_ENUM,
3952 errstring = "wrong type argument to bit-complement";
3953 else if (!noconvert)
3954 arg = default_conversion (arg);
3958 if (!(arg = build_expr_type_conversion (WANT_ARITH | WANT_ENUM, arg, 1)))
3959 errstring = "wrong type argument to abs";
3960 else if (!noconvert)
3961 arg = default_conversion (arg);
3965 /* Conjugating a real value is a no-op, but allow it anyway. */
3966 if (!(arg = build_expr_type_conversion (WANT_ARITH | WANT_ENUM, arg, 1)))
3967 errstring = "wrong type argument to conjugation";
3968 else if (!noconvert)
3969 arg = default_conversion (arg);
3972 case TRUTH_NOT_EXPR:
3973 arg = cp_convert (boolean_type_node, arg);
3974 val = invert_truthvalue (arg);
3975 if (arg != error_mark_node)
3977 errstring = "in argument to unary !";
3984 if (TREE_CODE (arg) == COMPLEX_CST)
3985 return TREE_REALPART (arg);
3986 else if (TREE_CODE (TREE_TYPE (arg)) == COMPLEX_TYPE)
3987 return fold (build1 (REALPART_EXPR, TREE_TYPE (TREE_TYPE (arg)), arg));
3992 if (TREE_CODE (arg) == COMPLEX_CST)
3993 return TREE_IMAGPART (arg);
3994 else if (TREE_CODE (TREE_TYPE (arg)) == COMPLEX_TYPE)
3995 return fold (build1 (IMAGPART_EXPR, TREE_TYPE (TREE_TYPE (arg)), arg));
3997 return cp_convert (TREE_TYPE (arg), integer_zero_node);
3999 case PREINCREMENT_EXPR:
4000 case POSTINCREMENT_EXPR:
4001 case PREDECREMENT_EXPR:
4002 case POSTDECREMENT_EXPR:
4003 /* Handle complex lvalues (when permitted)
4004 by reduction to simpler cases. */
4006 val = unary_complex_lvalue (code, arg);
4010 /* Increment or decrement the real part of the value,
4011 and don't change the imaginary part. */
4012 if (TREE_CODE (TREE_TYPE (arg)) == COMPLEX_TYPE)
4016 arg = stabilize_reference (arg);
4017 real = build_unary_op (REALPART_EXPR, arg, 1);
4018 imag = build_unary_op (IMAGPART_EXPR, arg, 1);
4019 return build (COMPLEX_EXPR, TREE_TYPE (arg),
4020 build_unary_op (code, real, 1), imag);
4023 /* Report invalid types. */
4025 if (!(arg = build_expr_type_conversion (WANT_ARITH | WANT_POINTER,
4028 if (code == PREINCREMENT_EXPR)
4029 errstring ="no pre-increment operator for type";
4030 else if (code == POSTINCREMENT_EXPR)
4031 errstring ="no post-increment operator for type";
4032 else if (code == PREDECREMENT_EXPR)
4033 errstring ="no pre-decrement operator for type";
4035 errstring ="no post-decrement operator for type";
4039 /* Report something read-only. */
4041 if (CP_TYPE_CONST_P (TREE_TYPE (arg))
4042 || TREE_READONLY (arg))
4043 readonly_error (arg, ((code == PREINCREMENT_EXPR
4044 || code == POSTINCREMENT_EXPR)
4045 ? "increment" : "decrement"),
4050 tree result_type = TREE_TYPE (arg);
4052 arg = get_unwidened (arg, 0);
4053 argtype = TREE_TYPE (arg);
4055 /* ARM $5.2.5 last annotation says this should be forbidden. */
4056 if (TREE_CODE (argtype) == ENUMERAL_TYPE)
4057 pedwarn ("ISO C++ forbids %sing an enum",
4058 (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
4059 ? "increment" : "decrement");
4061 /* Compute the increment. */
4063 if (TREE_CODE (argtype) == POINTER_TYPE)
4065 enum tree_code tmp = TREE_CODE (TREE_TYPE (argtype));
4066 tree type = complete_type (TREE_TYPE (argtype));
4068 if (!COMPLETE_OR_VOID_TYPE_P (type))
4069 error ("cannot %s a pointer to incomplete type `%T'",
4070 ((code == PREINCREMENT_EXPR
4071 || code == POSTINCREMENT_EXPR)
4072 ? "increment" : "decrement"), TREE_TYPE (argtype));
4073 else if ((pedantic || warn_pointer_arith)
4074 && (tmp == FUNCTION_TYPE || tmp == METHOD_TYPE
4075 || tmp == VOID_TYPE || tmp == OFFSET_TYPE))
4076 pedwarn ("ISO C++ forbids %sing a pointer of type `%T'",
4077 ((code == PREINCREMENT_EXPR
4078 || code == POSTINCREMENT_EXPR)
4079 ? "increment" : "decrement"), argtype);
4080 inc = cxx_sizeof_nowarn (TREE_TYPE (argtype));
4083 inc = integer_one_node;
4085 inc = cp_convert (argtype, inc);
4087 /* Handle incrementing a cast-expression. */
4089 switch (TREE_CODE (arg))
4094 case FIX_TRUNC_EXPR:
4095 case FIX_FLOOR_EXPR:
4096 case FIX_ROUND_EXPR:
4099 tree incremented, modify, value, compound;
4100 if (! lvalue_p (arg) && pedantic)
4101 pedwarn ("cast to non-reference type used as lvalue");
4102 arg = stabilize_reference (arg);
4103 if (code == PREINCREMENT_EXPR || code == PREDECREMENT_EXPR)
4106 value = save_expr (arg);
4107 incremented = build (((code == PREINCREMENT_EXPR
4108 || code == POSTINCREMENT_EXPR)
4109 ? PLUS_EXPR : MINUS_EXPR),
4110 argtype, value, inc);
4112 modify = build_modify_expr (arg, NOP_EXPR, incremented);
4113 compound = build (COMPOUND_EXPR, TREE_TYPE (arg), modify, value);
4115 /* Eliminate warning about unused result of + or -. */
4116 TREE_NO_UNUSED_WARNING (compound) = 1;
4124 /* Complain about anything else that is not a true lvalue. */
4125 if (!lvalue_or_else (arg, ((code == PREINCREMENT_EXPR
4126 || code == POSTINCREMENT_EXPR)
4127 ? "increment" : "decrement")))
4128 return error_mark_node;
4130 /* Forbid using -- on `bool'. */
4131 if (TREE_TYPE (arg) == boolean_type_node)
4133 if (code == POSTDECREMENT_EXPR || code == PREDECREMENT_EXPR)
4135 error ("invalid use of `--' on bool variable `%D'", arg);
4136 return error_mark_node;
4139 /* This will only work if someone can convince Kenner to accept
4140 my patch to expand_increment. (jason) */
4141 val = build (code, TREE_TYPE (arg), arg, inc);
4143 val = boolean_increment (code, arg);
4147 val = build (code, TREE_TYPE (arg), arg, inc);
4149 TREE_SIDE_EFFECTS (val) = 1;
4150 return cp_convert (result_type, val);
4154 /* Note that this operation never does default_conversion
4155 regardless of NOCONVERT. */
4157 argtype = lvalue_type (arg);
4158 if (TREE_CODE (argtype) == REFERENCE_TYPE)
4162 build_pointer_type (TREE_TYPE (argtype)), arg);
4163 TREE_CONSTANT (arg) = TREE_CONSTANT (TREE_OPERAND (arg, 0));
4166 else if (pedantic && DECL_MAIN_P (arg))
4168 pedwarn ("ISO C++ forbids taking address of function `::main'");
4170 /* Let &* cancel out to simplify resulting code. */
4171 if (TREE_CODE (arg) == INDIRECT_REF)
4173 /* We don't need to have `current_class_ptr' wrapped in a
4174 NON_LVALUE_EXPR node. */
4175 if (arg == current_class_ref)
4176 return current_class_ptr;
4178 arg = TREE_OPERAND (arg, 0);
4179 if (TREE_CODE (TREE_TYPE (arg)) == REFERENCE_TYPE)
4183 build_pointer_type (TREE_TYPE (TREE_TYPE (arg))), arg);
4184 TREE_CONSTANT (arg) = TREE_CONSTANT (TREE_OPERAND (arg, 0));
4186 else if (lvalue_p (arg))
4187 /* Don't let this be an lvalue. */
4188 return non_lvalue (arg);
4192 /* For &x[y], return x+y */
4193 if (TREE_CODE (arg) == ARRAY_REF)
4195 if (!cxx_mark_addressable (TREE_OPERAND (arg, 0)))
4196 return error_mark_node;
4197 return cp_build_binary_op (PLUS_EXPR, TREE_OPERAND (arg, 0),
4198 TREE_OPERAND (arg, 1));
4201 /* Uninstantiated types are all functions. Taking the
4202 address of a function is a no-op, so just return the
4205 if (TREE_CODE (arg) == IDENTIFIER_NODE
4206 && IDENTIFIER_OPNAME_P (arg))
4209 /* We don't know the type yet, so just work around the problem.
4210 We know that this will resolve to an lvalue. */
4211 return build1 (ADDR_EXPR, unknown_type_node, arg);
4214 if (TREE_CODE (arg) == COMPONENT_REF && type_unknown_p (arg)
4215 && !really_overloaded_fn (TREE_OPERAND (arg, 1)))
4217 /* They're trying to take the address of a unique non-static
4218 member function. This is ill-formed (except in MS-land),
4219 but let's try to DTRT.
4220 Note: We only handle unique functions here because we don't
4221 want to complain if there's a static overload; non-unique
4222 cases will be handled by instantiate_type. But we need to
4223 handle this case here to allow casts on the resulting PMF.
4224 We could defer this in non-MS mode, but it's easier to give
4225 a useful error here. */
4227 tree base = TREE_TYPE (TREE_OPERAND (arg, 0));
4228 tree name = DECL_NAME (get_first_fn (TREE_OPERAND (arg, 1)));
4230 if (! flag_ms_extensions)
4232 if (current_class_type
4233 && TREE_OPERAND (arg, 0) == current_class_ref)
4234 /* An expression like &memfn. */
4235 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);
4237 pedwarn ("ISO C++ forbids taking the address of a bound member function to form a pointer to member function. Say `&%T::%D'", base, name);
4239 arg = build_offset_ref (base, name);
4242 if (type_unknown_p (arg))
4243 return build1 (ADDR_EXPR, unknown_type_node, arg);
4245 /* Handle complex lvalues (when permitted)
4246 by reduction to simpler cases. */
4247 val = unary_complex_lvalue (code, arg);
4251 switch (TREE_CODE (arg))
4256 case FIX_TRUNC_EXPR:
4257 case FIX_FLOOR_EXPR:
4258 case FIX_ROUND_EXPR:
4260 if (! lvalue_p (arg) && pedantic)
4261 pedwarn ("ISO C++ forbids taking the address of a cast to a non-lvalue expression");
4268 /* Allow the address of a constructor if all the elements
4270 if (TREE_CODE (arg) == CONSTRUCTOR && TREE_HAS_CONSTRUCTOR (arg)
4271 && TREE_CONSTANT (arg))
4273 /* Anything not already handled and not a true memory reference
4275 else if (TREE_CODE (argtype) != FUNCTION_TYPE
4276 && TREE_CODE (argtype) != METHOD_TYPE
4277 && !non_cast_lvalue_or_else (arg, "unary `&'"))
4278 return error_mark_node;
4280 if (argtype != error_mark_node)
4281 argtype = build_pointer_type (argtype);
4283 if (!cxx_mark_addressable (arg))
4284 return error_mark_node;
4289 if (TREE_CODE (arg) == COMPONENT_REF
4290 && DECL_C_BIT_FIELD (TREE_OPERAND (arg, 1)))
4292 error ("attempt to take address of bit-field structure member `%D'",
4293 TREE_OPERAND (arg, 1));
4294 return error_mark_node;
4296 else if (TREE_CODE (arg) == COMPONENT_REF
4297 && TREE_CODE (TREE_OPERAND (arg, 0)) == INDIRECT_REF
4298 && (TREE_CODE (TREE_OPERAND (TREE_OPERAND (arg, 0), 0))
4301 /* offsetof idiom, fold it. */
4302 tree field = TREE_OPERAND (arg, 1);
4303 tree rval = build_unary_op (ADDR_EXPR, TREE_OPERAND (arg, 0), 0);
4304 tree binfo = lookup_base (TREE_TYPE (TREE_TYPE (rval)),
4305 decl_type_context (field),
4308 rval = build_base_path (PLUS_EXPR, rval, binfo, 1);
4309 rval = build1 (NOP_EXPR, argtype, rval);
4310 TREE_CONSTANT (rval) = TREE_CONSTANT (TREE_OPERAND (rval, 0));
4311 addr = fold (build (PLUS_EXPR, argtype, rval,
4312 cp_convert (argtype, byte_position (field))));
4315 addr = build1 (ADDR_EXPR, argtype, arg);
4317 /* Address of a static or external variable or
4318 function counts as a constant */
4320 TREE_CONSTANT (addr) = 1;
4322 if (TREE_CODE (argtype) == POINTER_TYPE
4323 && TREE_CODE (TREE_TYPE (argtype)) == METHOD_TYPE)
4325 build_ptrmemfunc_type (argtype);
4326 addr = build_ptrmemfunc (argtype, addr, 0);
4339 argtype = TREE_TYPE (arg);
4340 return fold (build1 (code, argtype, arg));
4343 error ("%s", errstring);
4344 return error_mark_node;
4347 /* Apply unary lvalue-demanding operator CODE to the expression ARG
4348 for certain kinds of expressions which are not really lvalues
4349 but which we can accept as lvalues.
4351 If ARG is not a kind of expression we can handle, return zero. */
4354 unary_complex_lvalue (code, arg)
4355 enum tree_code code;
4358 /* Handle (a, b) used as an "lvalue". */
4359 if (TREE_CODE (arg) == COMPOUND_EXPR)
4361 tree real_result = build_unary_op (code, TREE_OPERAND (arg, 1), 0);
4362 return build (COMPOUND_EXPR, TREE_TYPE (real_result),
4363 TREE_OPERAND (arg, 0), real_result);
4366 /* Handle (a ? b : c) used as an "lvalue". */
4367 if (TREE_CODE (arg) == COND_EXPR
4368 || TREE_CODE (arg) == MIN_EXPR || TREE_CODE (arg) == MAX_EXPR)
4369 return rationalize_conditional_expr (code, arg);
4371 /* Handle (a = b), (++a), and (--a) used as an "lvalue". */
4372 if (TREE_CODE (arg) == MODIFY_EXPR
4373 || TREE_CODE (arg) == PREINCREMENT_EXPR
4374 || TREE_CODE (arg) == PREDECREMENT_EXPR)
4376 tree lvalue = TREE_OPERAND (arg, 0);
4377 if (TREE_SIDE_EFFECTS (lvalue))
4379 lvalue = stabilize_reference (lvalue);
4380 arg = build (TREE_CODE (arg), TREE_TYPE (arg),
4381 lvalue, TREE_OPERAND (arg, 1));
4383 return unary_complex_lvalue
4384 (code, build (COMPOUND_EXPR, TREE_TYPE (lvalue), arg, lvalue));
4387 if (code != ADDR_EXPR)
4390 /* Handle (a = b) used as an "lvalue" for `&'. */
4391 if (TREE_CODE (arg) == MODIFY_EXPR
4392 || TREE_CODE (arg) == INIT_EXPR)
4394 tree real_result = build_unary_op (code, TREE_OPERAND (arg, 0), 0);
4395 arg = build (COMPOUND_EXPR, TREE_TYPE (real_result), arg, real_result);
4396 TREE_NO_UNUSED_WARNING (arg) = 1;
4400 if (TREE_CODE (TREE_TYPE (arg)) == FUNCTION_TYPE
4401 || TREE_CODE (TREE_TYPE (arg)) == METHOD_TYPE
4402 || TREE_CODE (TREE_TYPE (arg)) == OFFSET_TYPE)
4404 /* The representation of something of type OFFSET_TYPE
4405 is really the representation of a pointer to it.
4406 Here give the representation its true type. */
4409 my_friendly_assert (TREE_CODE (arg) != SCOPE_REF, 313);
4411 if (TREE_CODE (arg) != OFFSET_REF)
4414 t = TREE_OPERAND (arg, 1);
4416 /* Check all this code for right semantics. */
4417 if (TREE_CODE (t) == FUNCTION_DECL)
4419 if (DECL_DESTRUCTOR_P (t))
4420 error ("taking address of destructor");
4421 return build_unary_op (ADDR_EXPR, t, 0);
4423 if (TREE_CODE (t) == VAR_DECL)
4424 return build_unary_op (ADDR_EXPR, t, 0);
4429 if (TREE_OPERAND (arg, 0)
4430 && ! is_dummy_object (TREE_OPERAND (arg, 0))
4431 && TREE_CODE (t) != FIELD_DECL)
4433 error ("taking address of bound pointer-to-member expression");
4434 return error_mark_node;
4436 if (!PTRMEM_OK_P (arg))
4438 /* This cannot form a pointer to method, so we must
4439 resolve the offset ref, and take the address of the
4440 result. For instance,
4442 arg = resolve_offset_ref (arg);
4444 return build_unary_op (code, arg, 0);
4447 if (TREE_CODE (TREE_TYPE (t)) == REFERENCE_TYPE)
4449 error ("cannot create pointer to reference member `%D'", t);
4450 return error_mark_node;
4453 type = build_ptrmem_type (DECL_FIELD_CONTEXT (t), TREE_TYPE (t));
4454 t = make_ptrmem_cst (type, TREE_OPERAND (arg, 1));
4460 /* We permit compiler to make function calls returning
4461 objects of aggregate type look like lvalues. */
4465 if (TREE_CODE (targ) == SAVE_EXPR)
4466 targ = TREE_OPERAND (targ, 0);
4468 if (TREE_CODE (targ) == CALL_EXPR && IS_AGGR_TYPE (TREE_TYPE (targ)))
4470 if (TREE_CODE (arg) == SAVE_EXPR)
4473 targ = build_cplus_new (TREE_TYPE (arg), arg);
4474 return build1 (ADDR_EXPR, build_pointer_type (TREE_TYPE (arg)), targ);
4477 if (TREE_CODE (arg) == SAVE_EXPR && TREE_CODE (targ) == INDIRECT_REF)
4478 return build (SAVE_EXPR, build_pointer_type (TREE_TYPE (arg)),
4479 TREE_OPERAND (targ, 0), current_function_decl, NULL);
4482 /* Don't let anything else be handled specially. */
4486 /* Mark EXP saying that we need to be able to take the
4487 address of it; it should not be allocated in a register.
4488 Value is true if successful.
4490 C++: we do not allow `current_class_ptr' to be addressable. */
4493 cxx_mark_addressable (exp)
4496 register tree x = exp;
4499 switch (TREE_CODE (x))
4506 x = TREE_OPERAND (x, 0);
4510 if (x == current_class_ptr)
4512 error ("cannot take the address of `this', which is an rvalue expression");
4513 TREE_ADDRESSABLE (x) = 1; /* so compiler doesn't die later */
4519 /* Caller should not be trying to mark initialized
4520 constant fields addressable. */
4521 my_friendly_assert (DECL_LANG_SPECIFIC (x) == 0
4522 || DECL_IN_AGGR_P (x) == 0
4524 || DECL_EXTERNAL (x), 314);
4529 if (DECL_REGISTER (x) && !TREE_ADDRESSABLE (x)
4530 && !DECL_ARTIFICIAL (x) && extra_warnings)
4531 warning ("address requested for `%D', which is declared `register'",
4533 TREE_ADDRESSABLE (x) = 1;
4534 put_var_into_stack (x);
4538 TREE_ADDRESSABLE (x) = 1;
4539 TREE_ADDRESSABLE (DECL_ASSEMBLER_NAME (x)) = 1;
4543 TREE_ADDRESSABLE (x) = 1;
4547 TREE_ADDRESSABLE (x) = 1;
4548 cxx_mark_addressable (TREE_OPERAND (x, 0));
4556 /* Build and return a conditional expression IFEXP ? OP1 : OP2. */
4559 build_x_conditional_expr (ifexp, op1, op2)
4560 tree ifexp, op1, op2;
4562 if (processing_template_decl)
4563 return build_min_nt (COND_EXPR, ifexp, op1, op2);
4565 return build_conditional_expr (ifexp, op1, op2);
4568 /* Handle overloading of the ',' operator when needed. Otherwise,
4569 this function just builds an expression list. */
4572 build_x_compound_expr (list)
4575 tree rest = TREE_CHAIN (list);
4578 if (processing_template_decl)
4579 return build_min_nt (COMPOUND_EXPR, list, NULL_TREE);
4581 if (rest == NULL_TREE)
4582 return build_compound_expr (list);
4584 result = build_opfncall (COMPOUND_EXPR, LOOKUP_NORMAL,
4585 TREE_VALUE (list), TREE_VALUE (rest), NULL_TREE);
4587 return build_x_compound_expr (tree_cons (NULL_TREE, result,
4588 TREE_CHAIN (rest)));
4590 if (! TREE_SIDE_EFFECTS (TREE_VALUE (list)))
4592 /* FIXME: This test should be in the implicit cast to void of the LHS. */
4593 /* the left-hand operand of a comma expression is like an expression
4594 statement: we should warn if it doesn't have any side-effects,
4595 unless it was explicitly cast to (void). */
4596 if ((extra_warnings || warn_unused_value)
4597 && !(TREE_CODE (TREE_VALUE(list)) == CONVERT_EXPR
4598 && VOID_TYPE_P (TREE_TYPE (TREE_VALUE(list)))))
4599 warning("left-hand operand of comma expression has no effect");
4601 #if 0 /* this requires a gcc backend patch to export warn_if_unused_value */
4602 else if (warn_unused_value)
4603 warn_if_unused_value (TREE_VALUE(list));
4606 return build_compound_expr
4607 (tree_cons (NULL_TREE, TREE_VALUE (list),
4608 build_tree_list (NULL_TREE,
4609 build_x_compound_expr (rest))));
4612 /* Given a list of expressions, return a compound expression
4613 that performs them all and returns the value of the last of them. */
4616 build_compound_expr (list)
4622 TREE_VALUE (list) = decl_constant_value (TREE_VALUE (list));
4624 if (TREE_CHAIN (list) == 0)
4626 /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
4627 Strip such NOP_EXPRs, since LIST is used in non-lvalue context. */
4628 if (TREE_CODE (list) == NOP_EXPR
4629 && TREE_TYPE (list) == TREE_TYPE (TREE_OPERAND (list, 0)))
4630 list = TREE_OPERAND (list, 0);
4632 return TREE_VALUE (list);
4635 first = TREE_VALUE (list);
4636 first = convert_to_void (first, "left-hand operand of comma");
4637 if (first == error_mark_node)
4638 return error_mark_node;
4640 rest = build_compound_expr (TREE_CHAIN (list));
4641 if (rest == error_mark_node)
4642 return error_mark_node;
4644 /* When pedantic, a compound expression cannot be a constant expression. */
4645 if (! TREE_SIDE_EFFECTS (first) && ! pedantic)
4648 return build (COMPOUND_EXPR, TREE_TYPE (rest), first, rest);
4652 build_static_cast (type, expr)
4658 if (type == error_mark_node || expr == error_mark_node)
4659 return error_mark_node;
4661 if (TREE_CODE (expr) == OFFSET_REF)
4662 expr = resolve_offset_ref (expr);
4664 if (processing_template_decl)
4666 tree t = build_min (STATIC_CAST_EXPR, type, expr);
4670 /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
4671 Strip such NOP_EXPRs if VALUE is being used in non-lvalue context. */
4672 if (TREE_CODE (type) != REFERENCE_TYPE
4673 && TREE_CODE (expr) == NOP_EXPR
4674 && TREE_TYPE (expr) == TREE_TYPE (TREE_OPERAND (expr, 0)))
4675 expr = TREE_OPERAND (expr, 0);
4677 if (TREE_CODE (type) == VOID_TYPE)
4679 expr = convert_to_void (expr, /*implicit=*/NULL);
4683 if (TREE_CODE (type) == REFERENCE_TYPE)
4684 return (convert_from_reference
4685 (convert_to_reference (type, expr, CONV_STATIC|CONV_IMPLICIT,
4686 LOOKUP_COMPLAIN, NULL_TREE)));
4688 if (IS_AGGR_TYPE (type))
4689 return build_cplus_new (type, (build_special_member_call
4690 (NULL_TREE, complete_ctor_identifier,
4691 build_tree_list (NULL_TREE, expr),
4692 TYPE_BINFO (type), LOOKUP_NORMAL)));
4694 intype = TREE_TYPE (expr);
4696 /* FIXME handle casting to array type. */
4699 if (IS_AGGR_TYPE (intype)
4700 ? can_convert_arg (type, intype, expr)
4701 : can_convert_arg (strip_all_pointer_quals (type),
4702 strip_all_pointer_quals (intype), expr))
4703 /* This is a standard conversion. */
4705 else if (TYPE_PTROB_P (type) && TYPE_PTROB_P (intype))
4707 /* They're pointers to objects. They must be aggregates that
4708 are related non-virtually. */
4711 if (IS_AGGR_TYPE (TREE_TYPE (type)) && IS_AGGR_TYPE (TREE_TYPE (intype))
4712 && lookup_base (TREE_TYPE (type), TREE_TYPE (intype),
4713 ba_ignore | ba_quiet, &kind)
4714 && kind != bk_via_virtual)
4717 else if (TYPE_PTRMEM_P (type) && TYPE_PTRMEM_P (intype))
4719 /* They're pointers to members. The pointed to objects must be
4720 the same (ignoring CV qualifiers), and the containing classes
4721 must be related non-virtually. */
4725 (strip_all_pointer_quals (TREE_TYPE (TREE_TYPE (type))),
4726 strip_all_pointer_quals (TREE_TYPE (TREE_TYPE (intype))))
4727 && (lookup_base (TYPE_OFFSET_BASETYPE (TREE_TYPE (intype)),
4728 TYPE_OFFSET_BASETYPE (TREE_TYPE (type)),
4729 ba_ignore | ba_quiet, &kind))
4730 && kind != bk_via_virtual)
4733 else if (TREE_CODE (intype) != BOOLEAN_TYPE
4734 && TREE_CODE (type) != ARRAY_TYPE
4735 && TREE_CODE (type) != FUNCTION_TYPE
4736 && can_convert (intype, strip_all_pointer_quals (type)))
4738 else if (TREE_CODE (intype) == ENUMERAL_TYPE
4739 && TREE_CODE (type) == ENUMERAL_TYPE)
4740 /* DR 128: "A value of integral _or enumeration_ type can be explicitly
4741 converted to an enumeration type."
4742 The integral to enumeration will be accepted by the previous clause.
4743 We need to explicitly check for enumeration to enumeration. */
4746 /* [expr.static.cast]
4748 The static_cast operator shall not be used to cast away
4750 if (ok && casts_away_constness (intype, type))
4752 error ("static_cast from type `%T' to type `%T' casts away constness",
4754 return error_mark_node;
4758 return build_c_cast (type, expr);
4760 error ("invalid static_cast from type `%T' to type `%T'", intype, type);
4761 return error_mark_node;
4765 build_reinterpret_cast (type, expr)
4770 if (type == error_mark_node || expr == error_mark_node)
4771 return error_mark_node;
4773 if (TREE_CODE (expr) == OFFSET_REF)
4774 expr = resolve_offset_ref (expr);
4776 if (processing_template_decl)
4778 tree t = build_min (REINTERPRET_CAST_EXPR, type, expr);
4782 if (TREE_CODE (type) != REFERENCE_TYPE)
4784 expr = decay_conversion (expr);
4786 /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
4787 Strip such NOP_EXPRs if VALUE is being used in non-lvalue context. */
4788 if (TREE_CODE (expr) == NOP_EXPR
4789 && TREE_TYPE (expr) == TREE_TYPE (TREE_OPERAND (expr, 0)))
4790 expr = TREE_OPERAND (expr, 0);
4793 intype = TREE_TYPE (expr);
4795 if (TREE_CODE (type) == REFERENCE_TYPE)
4797 if (! real_lvalue_p (expr))
4799 error ("invalid reinterpret_cast of an rvalue expression of type `%T' to type `%T'", intype, type);
4800 return error_mark_node;
4802 expr = build_unary_op (ADDR_EXPR, expr, 0);
4803 if (expr != error_mark_node)
4804 expr = build_reinterpret_cast
4805 (build_pointer_type (TREE_TYPE (type)), expr);
4806 if (expr != error_mark_node)
4807 expr = build_indirect_ref (expr, 0);
4810 else if (same_type_ignoring_top_level_qualifiers_p (intype, type))
4811 return build_static_cast (type, expr);
4813 if (TYPE_PTR_P (type) && (TREE_CODE (intype) == INTEGER_TYPE
4814 || TREE_CODE (intype) == ENUMERAL_TYPE))
4816 else if (TREE_CODE (type) == INTEGER_TYPE && TYPE_PTR_P (intype))
4818 if (TYPE_PRECISION (type) < TYPE_PRECISION (intype))
4819 pedwarn ("reinterpret_cast from `%T' to `%T' loses precision",
4822 else if ((TYPE_PTRFN_P (type) && TYPE_PTRFN_P (intype))
4823 || (TYPE_PTRMEMFUNC_P (type) && TYPE_PTRMEMFUNC_P (intype)))
4825 expr = decl_constant_value (expr);
4826 return fold (build1 (NOP_EXPR, type, expr));
4828 else if ((TYPE_PTRMEM_P (type) && TYPE_PTRMEM_P (intype))
4829 || (TYPE_PTROBV_P (type) && TYPE_PTROBV_P (intype)))
4831 if (! comp_ptr_ttypes_reinterpret (TREE_TYPE (type), TREE_TYPE (intype)))
4832 pedwarn ("reinterpret_cast from `%T' to `%T' casts away const (or volatile)",
4835 expr = decl_constant_value (expr);
4836 return fold (build1 (NOP_EXPR, type, expr));
4838 else if ((TYPE_PTRFN_P (type) && TYPE_PTROBV_P (intype))
4839 || (TYPE_PTRFN_P (intype) && TYPE_PTROBV_P (type)))
4841 pedwarn ("ISO C++ forbids casting between pointer-to-function and pointer-to-object");
4842 expr = decl_constant_value (expr);
4843 return fold (build1 (NOP_EXPR, type, expr));
4847 error ("invalid reinterpret_cast from type `%T' to type `%T'",
4849 return error_mark_node;
4852 return cp_convert (type, expr);
4856 build_const_cast (type, expr)
4861 if (type == error_mark_node || expr == error_mark_node)
4862 return error_mark_node;
4864 if (TREE_CODE (expr) == OFFSET_REF)
4865 expr = resolve_offset_ref (expr);
4867 if (processing_template_decl)
4869 tree t = build_min (CONST_CAST_EXPR, type, expr);
4873 if (!POINTER_TYPE_P (type))
4874 error ("invalid use of const_cast with type `%T', which is not a pointer, reference, nor a pointer-to-data-member type", type);
4875 else if (TREE_CODE (TREE_TYPE (type)) == FUNCTION_TYPE)
4877 error ("invalid use of const_cast with type `%T', which is a pointer or reference to a function type", type);
4878 return error_mark_node;
4881 if (TREE_CODE (type) != REFERENCE_TYPE)
4883 expr = decay_conversion (expr);
4885 /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
4886 Strip such NOP_EXPRs if VALUE is being used in non-lvalue context. */
4887 if (TREE_CODE (expr) == NOP_EXPR
4888 && TREE_TYPE (expr) == TREE_TYPE (TREE_OPERAND (expr, 0)))
4889 expr = TREE_OPERAND (expr, 0);
4892 intype = TREE_TYPE (expr);
4894 if (same_type_ignoring_top_level_qualifiers_p (intype, type))
4895 return build_static_cast (type, expr);
4896 else if (TREE_CODE (type) == REFERENCE_TYPE)
4898 if (! real_lvalue_p (expr))
4900 error ("invalid const_cast of an rvalue of type `%T' to type `%T'", intype, type);
4901 return error_mark_node;
4904 if (comp_ptr_ttypes_const (TREE_TYPE (type), intype))
4906 expr = build_unary_op (ADDR_EXPR, expr, 0);
4907 expr = build1 (NOP_EXPR, type, expr);
4908 return convert_from_reference (expr);
4911 else if (TREE_CODE (type) == POINTER_TYPE
4912 && TREE_CODE (intype) == POINTER_TYPE
4913 && comp_ptr_ttypes_const (TREE_TYPE (type), TREE_TYPE (intype)))
4914 return cp_convert (type, expr);
4916 error ("invalid const_cast from type `%T' to type `%T'", intype, type);
4917 return error_mark_node;
4920 /* Build an expression representing a cast to type TYPE of expression EXPR.
4922 ALLOW_NONCONVERTING is true if we should allow non-converting constructors
4923 when doing the cast. */
4926 build_c_cast (type, expr)
4929 register tree value = expr;
4932 if (type == error_mark_node || expr == error_mark_node)
4933 return error_mark_node;
4935 if (processing_template_decl)
4937 tree t = build_min (CAST_EXPR, type,
4938 tree_cons (NULL_TREE, value, NULL_TREE));
4942 /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
4943 Strip such NOP_EXPRs if VALUE is being used in non-lvalue context. */
4944 if (TREE_CODE (type) != REFERENCE_TYPE
4945 && TREE_CODE (value) == NOP_EXPR
4946 && TREE_TYPE (value) == TREE_TYPE (TREE_OPERAND (value, 0)))
4947 value = TREE_OPERAND (value, 0);
4949 if (TREE_CODE (value) == OFFSET_REF)
4950 value = resolve_offset_ref (value);
4952 if (TREE_CODE (type) == ARRAY_TYPE)
4954 /* Allow casting from T1* to T2[] because Cfront allows it.
4955 NIHCL uses it. It is not valid ISO C++ however. */
4956 if (TREE_CODE (TREE_TYPE (expr)) == POINTER_TYPE)
4958 pedwarn ("ISO C++ forbids casting to an array type `%T'", type);
4959 type = build_pointer_type (TREE_TYPE (type));
4963 error ("ISO C++ forbids casting to an array type `%T'", type);
4964 return error_mark_node;
4968 if (TREE_CODE (type) == FUNCTION_TYPE
4969 || TREE_CODE (type) == METHOD_TYPE)
4971 error ("invalid cast to function type `%T'", type);
4972 return error_mark_node;
4975 if (TREE_CODE (type) == VOID_TYPE)
4977 /* Conversion to void does not cause any of the normal function to
4978 * pointer, array to pointer and lvalue to rvalue decays. */
4980 value = convert_to_void (value, /*implicit=*/NULL);
4983 /* Convert functions and arrays to pointers and
4984 convert references to their expanded types,
4985 but don't convert any other types. If, however, we are
4986 casting to a class type, there's no reason to do this: the
4987 cast will only succeed if there is a converting constructor,
4988 and the default conversions will be done at that point. In
4989 fact, doing the default conversion here is actually harmful
4993 struct S { S(const A&); };
4995 since we don't want the array-to-pointer conversion done. */
4996 if (!IS_AGGR_TYPE (type))
4998 if (TREE_CODE (TREE_TYPE (value)) == FUNCTION_TYPE
4999 || (TREE_CODE (TREE_TYPE (value)) == METHOD_TYPE
5000 /* Don't do the default conversion on a ->* expression. */
5001 && ! (TREE_CODE (type) == POINTER_TYPE
5002 && bound_pmf_p (value)))
5003 || TREE_CODE (TREE_TYPE (value)) == ARRAY_TYPE
5004 || TREE_CODE (TREE_TYPE (value)) == REFERENCE_TYPE)
5005 value = default_conversion (value);
5007 else if (TREE_CODE (TREE_TYPE (value)) == REFERENCE_TYPE)
5008 /* However, even for class types, we still need to strip away
5009 the reference type, since the call to convert_force below
5010 does not expect the input expression to be of reference
5012 value = convert_from_reference (value);
5014 otype = TREE_TYPE (value);
5016 /* Optionally warn about potentially worrisome casts. */
5019 && TREE_CODE (type) == POINTER_TYPE
5020 && TREE_CODE (otype) == POINTER_TYPE
5021 && !at_least_as_qualified_p (TREE_TYPE (type),
5023 warning ("cast from `%T' to `%T' discards qualifiers from pointer target type",
5026 if (TREE_CODE (type) == INTEGER_TYPE
5027 && TREE_CODE (otype) == POINTER_TYPE
5028 && TYPE_PRECISION (type) != TYPE_PRECISION (otype))
5029 warning ("cast from pointer to integer of different size");
5031 if (TREE_CODE (type) == POINTER_TYPE
5032 && TREE_CODE (otype) == INTEGER_TYPE
5033 && TYPE_PRECISION (type) != TYPE_PRECISION (otype)
5034 /* Don't warn about converting any constant. */
5035 && !TREE_CONSTANT (value))
5036 warning ("cast to pointer from integer of different size");
5038 if (TREE_CODE (type) == REFERENCE_TYPE)
5039 value = (convert_from_reference
5040 (convert_to_reference (type, value, CONV_C_CAST,
5041 LOOKUP_COMPLAIN, NULL_TREE)));
5046 value = decl_constant_value (value);
5049 value = convert_force (type, value, CONV_C_CAST);
5051 /* Ignore any integer overflow caused by the cast. */
5052 if (TREE_CODE (value) == INTEGER_CST)
5054 TREE_OVERFLOW (value) = TREE_OVERFLOW (ovalue);
5055 TREE_CONSTANT_OVERFLOW (value) = TREE_CONSTANT_OVERFLOW (ovalue);
5059 /* Warn about possible alignment problems. Do this here when we will have
5060 instantiated any necessary template types. */
5061 if (STRICT_ALIGNMENT && warn_cast_align
5062 && TREE_CODE (type) == POINTER_TYPE
5063 && TREE_CODE (otype) == POINTER_TYPE
5064 && TREE_CODE (TREE_TYPE (otype)) != VOID_TYPE
5065 && TREE_CODE (TREE_TYPE (otype)) != FUNCTION_TYPE
5066 && COMPLETE_TYPE_P (TREE_TYPE (otype))
5067 && COMPLETE_TYPE_P (TREE_TYPE (type))
5068 && TYPE_ALIGN (TREE_TYPE (type)) > TYPE_ALIGN (TREE_TYPE (otype)))
5069 warning ("cast from `%T' to `%T' increases required alignment of target type",
5072 /* Always produce some operator for an explicit cast,
5073 so we can tell (for -pedantic) that the cast is no lvalue. */
5074 if (TREE_CODE (type) != REFERENCE_TYPE && value == expr
5075 && real_lvalue_p (value))
5076 value = non_lvalue (value);
5081 /* Build an assignment expression of lvalue LHS from value RHS.
5082 MODIFYCODE is the code for a binary operator that we use
5083 to combine the old value of LHS with RHS to get the new value.
5084 Or else MODIFYCODE is NOP_EXPR meaning do a simple assignment.
5086 C++: If MODIFYCODE is INIT_EXPR, then leave references unbashed. */
5089 build_modify_expr (lhs, modifycode, rhs)
5091 enum tree_code modifycode;
5094 register tree result;
5096 tree lhstype = TREE_TYPE (lhs);
5097 tree olhstype = lhstype;
5100 /* Avoid duplicate error messages from operands that had errors. */
5101 if (lhs == error_mark_node || rhs == error_mark_node)
5102 return error_mark_node;
5104 /* Handle control structure constructs used as "lvalues". */
5105 switch (TREE_CODE (lhs))
5107 /* Handle --foo = 5; as these are valid constructs in C++ */
5108 case PREDECREMENT_EXPR:
5109 case PREINCREMENT_EXPR:
5110 if (TREE_SIDE_EFFECTS (TREE_OPERAND (lhs, 0)))
5111 lhs = build (TREE_CODE (lhs), TREE_TYPE (lhs),
5112 stabilize_reference (TREE_OPERAND (lhs, 0)),
5113 TREE_OPERAND (lhs, 1));
5114 return build (COMPOUND_EXPR, lhstype,
5116 build_modify_expr (TREE_OPERAND (lhs, 0),
5119 /* Handle (a, b) used as an "lvalue". */
5121 newrhs = build_modify_expr (TREE_OPERAND (lhs, 1),
5123 if (newrhs == error_mark_node)
5124 return error_mark_node;
5125 return build (COMPOUND_EXPR, lhstype,
5126 TREE_OPERAND (lhs, 0), newrhs);
5129 newrhs = build_modify_expr (TREE_OPERAND (lhs, 0), modifycode, rhs);
5130 if (newrhs == error_mark_node)
5131 return error_mark_node;
5132 return build (COMPOUND_EXPR, lhstype, lhs, newrhs);
5134 /* Handle (a ? b : c) used as an "lvalue". */
5137 /* Produce (a ? (b = rhs) : (c = rhs))
5138 except that the RHS goes through a save-expr
5139 so the code to compute it is only emitted once. */
5141 tree preeval = NULL_TREE;
5143 rhs = stabilize_expr (rhs, &preeval);
5145 /* Check this here to avoid odd errors when trying to convert
5146 a throw to the type of the COND_EXPR. */
5147 if (!lvalue_or_else (lhs, "assignment"))
5148 return error_mark_node;
5150 cond = build_conditional_expr
5151 (TREE_OPERAND (lhs, 0),
5152 build_modify_expr (cp_convert (TREE_TYPE (lhs),
5153 TREE_OPERAND (lhs, 1)),
5155 build_modify_expr (cp_convert (TREE_TYPE (lhs),
5156 TREE_OPERAND (lhs, 2)),
5159 if (cond == error_mark_node)
5161 /* Make sure the code to compute the rhs comes out
5162 before the split. */
5163 return build (COMPOUND_EXPR, TREE_TYPE (lhs), preeval, cond);
5167 lhs = resolve_offset_ref (lhs);
5168 if (lhs == error_mark_node)
5169 return error_mark_node;
5170 olhstype = lhstype = TREE_TYPE (lhs);
5176 if (modifycode == INIT_EXPR)
5178 if (TREE_CODE (rhs) == CONSTRUCTOR)
5180 my_friendly_assert (same_type_p (TREE_TYPE (rhs), lhstype),
5182 result = build (INIT_EXPR, lhstype, lhs, rhs);
5183 TREE_SIDE_EFFECTS (result) = 1;
5186 else if (! IS_AGGR_TYPE (lhstype))
5187 /* Do the default thing */;
5190 result = build_special_member_call (lhs, complete_ctor_identifier,
5191 build_tree_list (NULL_TREE, rhs),
5192 TYPE_BINFO (lhstype),
5194 if (result == NULL_TREE)
5195 return error_mark_node;
5201 if (TREE_CODE (lhstype) == REFERENCE_TYPE)
5203 lhs = convert_from_reference (lhs);
5204 olhstype = lhstype = TREE_TYPE (lhs);
5206 lhs = require_complete_type (lhs);
5207 if (lhs == error_mark_node)
5208 return error_mark_node;
5210 if (modifycode == NOP_EXPR)
5212 /* `operator=' is not an inheritable operator. */
5213 if (! IS_AGGR_TYPE (lhstype))
5214 /* Do the default thing */;
5217 result = build_opfncall (MODIFY_EXPR, LOOKUP_NORMAL,
5218 lhs, rhs, make_node (NOP_EXPR));
5219 if (result == NULL_TREE)
5220 return error_mark_node;
5227 /* A binary op has been requested. Combine the old LHS
5228 value with the RHS producing the value we should actually
5229 store into the LHS. */
5231 my_friendly_assert (!PROMOTES_TO_AGGR_TYPE (lhstype, REFERENCE_TYPE),
5233 lhs = stabilize_reference (lhs);
5234 newrhs = cp_build_binary_op (modifycode, lhs, rhs);
5235 if (newrhs == error_mark_node)
5237 error (" in evaluation of `%Q(%#T, %#T)'", modifycode,
5238 TREE_TYPE (lhs), TREE_TYPE (rhs));
5239 return error_mark_node;
5242 /* Now it looks like a plain assignment. */
5243 modifycode = NOP_EXPR;
5245 my_friendly_assert (TREE_CODE (lhstype) != REFERENCE_TYPE, 20011220);
5246 my_friendly_assert (TREE_CODE (TREE_TYPE (newrhs)) != REFERENCE_TYPE,
5250 /* Handle a cast used as an "lvalue".
5251 We have already performed any binary operator using the value as cast.
5252 Now convert the result to the cast type of the lhs,
5253 and then true type of the lhs and store it there;
5254 then convert result back to the cast type to be the value
5255 of the assignment. */
5257 switch (TREE_CODE (lhs))
5262 case FIX_TRUNC_EXPR:
5263 case FIX_FLOOR_EXPR:
5264 case FIX_ROUND_EXPR:
5267 tree inner_lhs = TREE_OPERAND (lhs, 0);
5270 if (TREE_CODE (TREE_TYPE (newrhs)) == ARRAY_TYPE
5271 || TREE_CODE (TREE_TYPE (newrhs)) == FUNCTION_TYPE
5272 || TREE_CODE (TREE_TYPE (newrhs)) == METHOD_TYPE
5273 || TREE_CODE (TREE_TYPE (newrhs)) == OFFSET_TYPE)
5274 newrhs = default_conversion (newrhs);
5276 /* ISO C++ 5.4/1: The result is an lvalue if T is a reference
5277 type, otherwise the result is an rvalue. */
5278 if (! lvalue_p (lhs))
5279 pedwarn ("ISO C++ forbids cast to non-reference type used as lvalue");
5281 result = build_modify_expr (inner_lhs, NOP_EXPR,
5282 cp_convert (TREE_TYPE (inner_lhs),
5283 cp_convert (lhstype, newrhs)));
5284 if (result == error_mark_node)
5286 return cp_convert (TREE_TYPE (lhs), result);
5293 /* Now we have handled acceptable kinds of LHS that are not truly lvalues.
5294 Reject anything strange now. */
5296 if (!lvalue_or_else (lhs, "assignment"))
5297 return error_mark_node;
5299 /* Warn about modifying something that is `const'. Don't warn if
5300 this is initialization. */
5301 if (modifycode != INIT_EXPR
5302 && (TREE_READONLY (lhs) || CP_TYPE_CONST_P (lhstype)
5303 /* Functions are not modifiable, even though they are
5305 || TREE_CODE (TREE_TYPE (lhs)) == FUNCTION_TYPE
5306 || TREE_CODE (TREE_TYPE (lhs)) == METHOD_TYPE
5307 /* If it's an aggregate and any field is const, then it is
5308 effectively const. */
5309 || (IS_AGGR_TYPE_CODE (TREE_CODE (lhstype))
5310 && C_TYPE_FIELDS_READONLY (lhstype))))
5311 readonly_error (lhs, "assignment", 0);
5313 /* If storing into a structure or union member, it has probably been
5314 given type `int'. Compute the type that would go with the actual
5315 amount of storage the member occupies. */
5317 if (TREE_CODE (lhs) == COMPONENT_REF
5318 && (TREE_CODE (lhstype) == INTEGER_TYPE
5319 || TREE_CODE (lhstype) == REAL_TYPE
5320 || TREE_CODE (lhstype) == ENUMERAL_TYPE))
5322 lhstype = TREE_TYPE (get_unwidened (lhs, 0));
5324 /* If storing in a field that is in actuality a short or narrower
5325 than one, we must store in the field in its actual type. */
5327 if (lhstype != TREE_TYPE (lhs))
5329 lhs = copy_node (lhs);
5330 TREE_TYPE (lhs) = lhstype;
5334 if (TREE_CODE (lhstype) != REFERENCE_TYPE)
5336 if (TREE_SIDE_EFFECTS (lhs))
5337 lhs = stabilize_reference (lhs);
5338 if (TREE_SIDE_EFFECTS (newrhs))
5339 newrhs = stabilize_reference (newrhs);
5342 /* Convert new value to destination type. */
5344 if (TREE_CODE (lhstype) == ARRAY_TYPE)
5348 if (!same_or_base_type_p (TYPE_MAIN_VARIANT (lhstype),
5349 TYPE_MAIN_VARIANT (TREE_TYPE (rhs))))
5351 error ("incompatible types in assignment of `%T' to `%T'",
5352 TREE_TYPE (rhs), lhstype);
5353 return error_mark_node;
5356 /* Allow array assignment in compiler-generated code. */
5357 if (! DECL_ARTIFICIAL (current_function_decl))
5358 pedwarn ("ISO C++ forbids assignment of arrays");
5360 from_array = TREE_CODE (TREE_TYPE (newrhs)) == ARRAY_TYPE
5361 ? 1 + (modifycode != INIT_EXPR): 0;
5362 return build_vec_init (lhs, newrhs, from_array);
5365 if (modifycode == INIT_EXPR)
5366 newrhs = convert_for_initialization (lhs, lhstype, newrhs, LOOKUP_NORMAL,
5367 "initialization", NULL_TREE, 0);
5370 /* Avoid warnings on enum bit fields. */
5371 if (TREE_CODE (olhstype) == ENUMERAL_TYPE
5372 && TREE_CODE (lhstype) == INTEGER_TYPE)
5374 newrhs = convert_for_assignment (olhstype, newrhs, "assignment",
5376 newrhs = convert_force (lhstype, newrhs, 0);
5379 newrhs = convert_for_assignment (lhstype, newrhs, "assignment",
5381 if (TREE_CODE (newrhs) == CALL_EXPR
5382 && TYPE_NEEDS_CONSTRUCTING (lhstype))
5383 newrhs = build_cplus_new (lhstype, newrhs);
5385 /* Can't initialize directly from a TARGET_EXPR, since that would
5386 cause the lhs to be constructed twice, and possibly result in
5387 accidental self-initialization. So we force the TARGET_EXPR to be
5388 expanded without a target. */
5389 if (TREE_CODE (newrhs) == TARGET_EXPR)
5390 newrhs = build (COMPOUND_EXPR, TREE_TYPE (newrhs), newrhs,
5391 TREE_OPERAND (newrhs, 0));
5394 if (newrhs == error_mark_node)
5395 return error_mark_node;
5397 if (TREE_CODE (newrhs) == COND_EXPR)
5400 tree cond = TREE_OPERAND (newrhs, 0);
5402 if (TREE_SIDE_EFFECTS (lhs))
5403 cond = build_compound_expr (tree_cons
5405 build_tree_list (NULL_TREE, cond)));
5407 /* Cannot have two identical lhs on this one tree (result) as preexpand
5408 calls will rip them out and fill in RTL for them, but when the
5409 rtl is generated, the calls will only be in the first side of the
5410 condition, not on both, or before the conditional jump! (mrs) */
5411 lhs1 = break_out_calls (lhs);
5414 /* If there's no change, the COND_EXPR behaves like any other rhs. */
5415 result = build (modifycode == NOP_EXPR ? MODIFY_EXPR : INIT_EXPR,
5416 lhstype, lhs, newrhs);
5419 tree result_type = TREE_TYPE (newrhs);
5420 /* We have to convert each arm to the proper type because the
5421 types may have been munged by constant folding. */
5423 = build (COND_EXPR, result_type, cond,
5424 build_modify_expr (lhs, modifycode,
5425 cp_convert (result_type,
5426 TREE_OPERAND (newrhs, 1))),
5427 build_modify_expr (lhs1, modifycode,
5428 cp_convert (result_type,
5429 TREE_OPERAND (newrhs, 2))));
5433 result = build (modifycode == NOP_EXPR ? MODIFY_EXPR : INIT_EXPR,
5434 lhstype, lhs, newrhs);
5436 TREE_SIDE_EFFECTS (result) = 1;
5438 /* If we got the LHS in a different type for storing in,
5439 convert the result back to the nominal type of LHS
5440 so that the value we return always has the same type
5441 as the LHS argument. */
5443 if (olhstype == TREE_TYPE (result))
5445 /* Avoid warnings converting integral types back into enums
5446 for enum bit fields. */
5447 if (TREE_CODE (TREE_TYPE (result)) == INTEGER_TYPE
5448 && TREE_CODE (olhstype) == ENUMERAL_TYPE)
5450 result = build (COMPOUND_EXPR, olhstype, result, olhs);
5451 TREE_NO_UNUSED_WARNING (result) = 1;
5454 return convert_for_assignment (olhstype, result, "assignment",
5459 build_x_modify_expr (lhs, modifycode, rhs)
5461 enum tree_code modifycode;
5464 if (processing_template_decl)
5465 return build_min_nt (MODOP_EXPR, lhs,
5466 build_min_nt (modifycode, NULL_TREE, NULL_TREE), rhs);
5468 if (modifycode != NOP_EXPR)
5470 tree rval = build_opfncall (MODIFY_EXPR, LOOKUP_NORMAL, lhs, rhs,
5471 make_node (modifycode));
5475 return build_modify_expr (lhs, modifycode, rhs);
5479 /* Get difference in deltas for different pointer to member function
5480 types. Return integer_zero_node, if FROM cannot be converted to a
5481 TO type. If FORCE is true, then allow reverse conversions as well.
5483 Note that the naming of FROM and TO is kind of backwards; the return
5484 value is what we add to a TO in order to get a FROM. They are named
5485 this way because we call this function to find out how to convert from
5486 a pointer to member of FROM to a pointer to member of TO. */
5489 get_delta_difference (from, to, force)
5493 tree delta = integer_zero_node;
5498 binfo = lookup_base (to, from, ba_check, &kind);
5499 if (kind == bk_inaccessible || kind == bk_ambig)
5501 error (" in pointer to member function conversion");
5508 error_not_base_type (from, to);
5509 error (" in pointer to member conversion");
5512 binfo = lookup_base (from, to, ba_check, &kind);
5515 virt_binfo = binfo_from_vbase (binfo);
5519 /* This is a reinterpret cast, we choose to do nothing. */
5520 warning ("pointer to member cast via virtual base `%T' of `%T'",
5521 BINFO_TYPE (virt_binfo),
5522 BINFO_TYPE (BINFO_INHERITANCE_CHAIN (virt_binfo)));
5525 delta = BINFO_OFFSET (binfo);
5526 delta = cp_convert (ptrdiff_type_node, delta);
5527 delta = cp_build_binary_op (MINUS_EXPR,
5534 virt_binfo = binfo_from_vbase (binfo);
5537 /* This is a reinterpret cast, we choose to do nothing. */
5539 warning ("pointer to member cast via virtual base `%T' of `%T'",
5540 BINFO_TYPE (virt_binfo),
5541 BINFO_TYPE (BINFO_INHERITANCE_CHAIN (virt_binfo)));
5543 error ("pointer to member conversion via virtual base `%T' of `%T'",
5544 BINFO_TYPE (virt_binfo),
5545 BINFO_TYPE (BINFO_INHERITANCE_CHAIN (virt_binfo)));
5548 delta = BINFO_OFFSET (binfo);
5550 return cp_convert (ptrdiff_type_node, delta);
5553 /* Return a constructor for the pointer-to-member-function TYPE using
5554 the other components as specified. */
5557 build_ptrmemfunc1 (type, delta, pfn)
5558 tree type, delta, pfn;
5564 /* Pull the FIELD_DECLs out of the type. */
5565 pfn_field = TYPE_FIELDS (type);
5566 delta_field = TREE_CHAIN (pfn_field);
5568 /* Make sure DELTA has the type we want. */
5569 delta = convert_and_check (delta_type_node, delta);
5571 /* Finish creating the initializer. */
5572 u = tree_cons (pfn_field, pfn,
5573 build_tree_list (delta_field, delta));
5574 u = build (CONSTRUCTOR, type, NULL_TREE, u);
5575 TREE_CONSTANT (u) = TREE_CONSTANT (pfn) && TREE_CONSTANT (delta);
5576 TREE_STATIC (u) = (TREE_CONSTANT (u)
5577 && (initializer_constant_valid_p (pfn, TREE_TYPE (pfn))
5579 && (initializer_constant_valid_p (delta, TREE_TYPE (delta))
5584 /* Build a constructor for a pointer to member function. It can be
5585 used to initialize global variables, local variable, or used
5586 as a value in expressions. TYPE is the POINTER to METHOD_TYPE we
5589 If FORCE is nonzero, then force this conversion, even if
5590 we would rather not do it. Usually set when using an explicit
5593 Return error_mark_node, if something goes wrong. */
5596 build_ptrmemfunc (type, pfn, force)
5601 tree pfn_type = TREE_TYPE (pfn);
5602 tree to_type = build_ptrmemfunc_type (type);
5604 /* Handle multiple conversions of pointer to member functions. */
5605 if (TYPE_PTRMEMFUNC_P (TREE_TYPE (pfn)))
5607 tree delta = NULL_TREE;
5608 tree npfn = NULL_TREE;
5612 && !can_convert_arg (to_type, TREE_TYPE (pfn), pfn))
5613 error ("invalid conversion to type `%T' from type `%T'",
5616 n = get_delta_difference (TYPE_PTRMEMFUNC_OBJECT_TYPE (pfn_type),
5617 TYPE_PTRMEMFUNC_OBJECT_TYPE (to_type),
5620 /* We don't have to do any conversion to convert a
5621 pointer-to-member to its own type. But, we don't want to
5622 just return a PTRMEM_CST if there's an explicit cast; that
5623 cast should make the expression an invalid template argument. */
5624 if (TREE_CODE (pfn) != PTRMEM_CST)
5626 if (same_type_p (to_type, pfn_type))
5628 else if (integer_zerop (n))
5629 return build_reinterpret_cast (to_type, pfn);
5632 if (TREE_SIDE_EFFECTS (pfn))
5633 pfn = save_expr (pfn);
5635 /* Obtain the function pointer and the current DELTA. */
5636 if (TREE_CODE (pfn) == PTRMEM_CST)
5637 expand_ptrmemfunc_cst (pfn, &delta, &npfn);
5640 npfn = build_ptrmemfunc_access_expr (pfn, pfn_identifier);
5641 delta = build_ptrmemfunc_access_expr (pfn, delta_identifier);
5644 /* Just adjust the DELTA field. */
5645 delta = cp_convert (ptrdiff_type_node, delta);
5646 if (TARGET_PTRMEMFUNC_VBIT_LOCATION == ptrmemfunc_vbit_in_delta)
5647 n = cp_build_binary_op (LSHIFT_EXPR, n, integer_one_node);
5648 delta = cp_build_binary_op (PLUS_EXPR, delta, n);
5649 return build_ptrmemfunc1 (to_type, delta, npfn);
5652 /* Handle null pointer to member function conversions. */
5653 if (integer_zerop (pfn))
5655 pfn = build_c_cast (type, integer_zero_node);
5656 return build_ptrmemfunc1 (to_type,
5661 if (type_unknown_p (pfn))
5662 return instantiate_type (type, pfn, tf_error | tf_warning);
5664 fn = TREE_OPERAND (pfn, 0);
5665 my_friendly_assert (TREE_CODE (fn) == FUNCTION_DECL, 0);
5666 return make_ptrmem_cst (to_type, fn);
5669 /* Return the DELTA, IDX, PFN, and DELTA2 values for the PTRMEM_CST
5672 ??? There is no consistency as to the types returned for the above
5673 values. Some code acts as if its a sizetype and some as if its
5674 integer_type_node. */
5677 expand_ptrmemfunc_cst (cst, delta, pfn)
5682 tree type = TREE_TYPE (cst);
5683 tree fn = PTRMEM_CST_MEMBER (cst);
5684 tree ptr_class, fn_class;
5686 my_friendly_assert (TREE_CODE (fn) == FUNCTION_DECL, 0);
5688 /* The class that the function belongs to. */
5689 fn_class = DECL_CONTEXT (fn);
5691 /* The class that we're creating a pointer to member of. */
5692 ptr_class = TYPE_PTRMEMFUNC_OBJECT_TYPE (type);
5694 /* First, calculate the adjustment to the function's class. */
5695 *delta = get_delta_difference (fn_class, ptr_class, /*force=*/0);
5697 if (!DECL_VIRTUAL_P (fn))
5698 *pfn = convert (TYPE_PTRMEMFUNC_FN_TYPE (type), build_addr_func (fn));
5701 /* If we're dealing with a virtual function, we have to adjust 'this'
5702 again, to point to the base which provides the vtable entry for
5703 fn; the call will do the opposite adjustment. */
5704 tree orig_class = DECL_CONTEXT (fn);
5705 tree binfo = binfo_or_else (orig_class, fn_class);
5706 *delta = fold (build (PLUS_EXPR, TREE_TYPE (*delta),
5707 *delta, BINFO_OFFSET (binfo)));
5709 /* We set PFN to the vtable offset at which the function can be
5710 found, plus one (unless ptrmemfunc_vbit_in_delta, in which
5711 case delta is shifted left, and then incremented). */
5712 *pfn = DECL_VINDEX (fn);
5713 *pfn = fold (build (MULT_EXPR, integer_type_node, *pfn,
5714 TYPE_SIZE_UNIT (vtable_entry_type)));
5716 switch (TARGET_PTRMEMFUNC_VBIT_LOCATION)
5718 case ptrmemfunc_vbit_in_pfn:
5719 *pfn = fold (build (PLUS_EXPR, integer_type_node, *pfn,
5723 case ptrmemfunc_vbit_in_delta:
5724 *delta = fold (build (LSHIFT_EXPR, TREE_TYPE (*delta),
5725 *delta, integer_one_node));
5726 *delta = fold (build (PLUS_EXPR, TREE_TYPE (*delta),
5727 *delta, integer_one_node));
5734 *pfn = fold (build1 (NOP_EXPR, TYPE_PTRMEMFUNC_FN_TYPE (type),
5739 /* Return an expression for PFN from the pointer-to-member function
5743 pfn_from_ptrmemfunc (t)
5746 if (TREE_CODE (t) == PTRMEM_CST)
5751 expand_ptrmemfunc_cst (t, &delta, &pfn);
5756 return build_ptrmemfunc_access_expr (t, pfn_identifier);
5759 /* Expression EXPR is about to be implicitly converted to TYPE. Warn
5760 if this is a potentially dangerous thing to do. Returns a possibly
5764 dubious_conversion_warnings (type, expr, errtype, fndecl, parmnum)
5767 const char *errtype;
5771 if (TREE_CODE (type) == REFERENCE_TYPE)
5772 type = TREE_TYPE (type);
5774 /* Issue warnings about peculiar, but valid, uses of NULL. */
5775 if (ARITHMETIC_TYPE_P (type) && expr == null_node)
5778 warning ("passing NULL used for non-pointer %s %P of `%D'",
5779 errtype, parmnum, fndecl);
5781 warning ("%s to non-pointer type `%T' from NULL", errtype, type);
5784 /* Warn about assigning a floating-point type to an integer type. */
5785 if (TREE_CODE (TREE_TYPE (expr)) == REAL_TYPE
5786 && TREE_CODE (type) == INTEGER_TYPE)
5789 warning ("passing `%T' for %s %P of `%D'",
5790 TREE_TYPE (expr), errtype, parmnum, fndecl);
5792 warning ("%s to `%T' from `%T'", errtype, type, TREE_TYPE (expr));
5794 /* And warn about assigning a negative value to an unsigned
5796 else if (TREE_UNSIGNED (type) && TREE_CODE (type) != BOOLEAN_TYPE)
5798 if (TREE_CODE (expr) == INTEGER_CST
5799 && TREE_NEGATED_INT (expr))
5802 warning ("passing negative value `%E' for %s %P of `%D'",
5803 expr, errtype, parmnum, fndecl);
5805 warning ("%s of negative value `%E' to `%T'",
5806 errtype, expr, type);
5809 overflow_warning (expr);
5811 if (TREE_CONSTANT (expr))
5817 /* Convert value RHS to type TYPE as preparation for an assignment to
5818 an lvalue of type TYPE. ERRTYPE is a string to use in error
5819 messages: "assignment", "return", etc. If FNDECL is non-NULL, we
5820 are doing the conversion in order to pass the PARMNUMth argument of
5824 convert_for_assignment (type, rhs, errtype, fndecl, parmnum)
5826 const char *errtype;
5830 register enum tree_code codel = TREE_CODE (type);
5831 register tree rhstype;
5832 register enum tree_code coder;
5834 if (codel == OFFSET_TYPE)
5837 if (TREE_CODE (rhs) == OFFSET_REF)
5838 rhs = resolve_offset_ref (rhs);
5840 /* Strip NON_LVALUE_EXPRs since we aren't using as an lvalue. */
5841 if (TREE_CODE (rhs) == NON_LVALUE_EXPR)
5842 rhs = TREE_OPERAND (rhs, 0);
5844 rhstype = TREE_TYPE (rhs);
5845 coder = TREE_CODE (rhstype);
5847 if (rhs == error_mark_node || rhstype == error_mark_node)
5848 return error_mark_node;
5849 if (TREE_CODE (rhs) == TREE_LIST && TREE_VALUE (rhs) == error_mark_node)
5850 return error_mark_node;
5852 rhs = dubious_conversion_warnings (type, rhs, errtype, fndecl, parmnum);
5854 /* The RHS of an assignment cannot have void type. */
5855 if (coder == VOID_TYPE)
5857 error ("void value not ignored as it ought to be");
5858 return error_mark_node;
5861 /* Simplify the RHS if possible. */
5862 if (TREE_CODE (rhs) == CONST_DECL)
5863 rhs = DECL_INITIAL (rhs);
5865 /* We do not use decl_constant_value here because of this case:
5867 const char* const s = "s";
5869 The conversion rules for a string literal are more lax than for a
5870 variable; in particular, a string literal can be converted to a
5871 "char *" but the variable "s" cannot be converted in the same
5872 way. If the conversion is allowed, the optimization should be
5873 performed while creating the converted expression. */
5877 The expression is implicitly converted (clause _conv_) to the
5878 cv-unqualified type of the left operand.
5880 We allow bad conversions here because by the time we get to this point
5881 we are committed to doing the conversion. If we end up doing a bad
5882 conversion, convert_like will complain. */
5883 if (!can_convert_arg_bad (type, rhstype, rhs))
5885 /* When -Wno-pmf-conversions is use, we just silently allow
5886 conversions from pointers-to-members to plain pointers. If
5887 the conversion doesn't work, cp_convert will complain. */
5889 && TYPE_PTR_P (type)
5890 && TYPE_PTRMEMFUNC_P (rhstype))
5891 rhs = cp_convert (strip_top_quals (type), rhs);
5894 /* If the right-hand side has unknown type, then it is an
5895 overloaded function. Call instantiate_type to get error
5897 if (rhstype == unknown_type_node)
5898 instantiate_type (type, rhs, tf_error | tf_warning);
5900 error ("cannot convert `%T' to `%T' for argument `%P' to `%D'",
5901 rhstype, type, parmnum, fndecl);
5903 error ("cannot convert `%T' to `%T' in %s", rhstype, type,
5905 return error_mark_node;
5908 return perform_implicit_conversion (strip_top_quals (type), rhs);
5911 /* Convert RHS to be of type TYPE.
5912 If EXP is nonzero, it is the target of the initialization.
5913 ERRTYPE is a string to use in error messages.
5915 Two major differences between the behavior of
5916 `convert_for_assignment' and `convert_for_initialization'
5917 are that references are bashed in the former, while
5918 copied in the latter, and aggregates are assigned in
5919 the former (operator=) while initialized in the
5922 If using constructor make sure no conversion operator exists, if one does
5923 exist, an ambiguity exists.
5925 If flags doesn't include LOOKUP_COMPLAIN, don't complain about anything. */
5928 convert_for_initialization (exp, type, rhs, flags, errtype, fndecl, parmnum)
5929 tree exp, type, rhs;
5931 const char *errtype;
5935 register enum tree_code codel = TREE_CODE (type);
5936 register tree rhstype;
5937 register enum tree_code coder;
5939 /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
5940 Strip such NOP_EXPRs, since RHS is used in non-lvalue context. */
5941 if (TREE_CODE (rhs) == NOP_EXPR
5942 && TREE_TYPE (rhs) == TREE_TYPE (TREE_OPERAND (rhs, 0))
5943 && codel != REFERENCE_TYPE)
5944 rhs = TREE_OPERAND (rhs, 0);
5946 if (rhs == error_mark_node
5947 || (TREE_CODE (rhs) == TREE_LIST && TREE_VALUE (rhs) == error_mark_node))
5948 return error_mark_node;
5950 if (TREE_CODE (rhs) == OFFSET_REF)
5952 rhs = resolve_offset_ref (rhs);
5953 if (rhs == error_mark_node)
5954 return error_mark_node;
5957 if (TREE_CODE (TREE_TYPE (rhs)) == REFERENCE_TYPE)
5958 rhs = convert_from_reference (rhs);
5960 if ((TREE_CODE (TREE_TYPE (rhs)) == ARRAY_TYPE
5961 && TREE_CODE (type) != ARRAY_TYPE
5962 && (TREE_CODE (type) != REFERENCE_TYPE
5963 || TREE_CODE (TREE_TYPE (type)) != ARRAY_TYPE))
5964 || (TREE_CODE (TREE_TYPE (rhs)) == FUNCTION_TYPE
5965 && (TREE_CODE (type) != REFERENCE_TYPE
5966 || TREE_CODE (TREE_TYPE (type)) != FUNCTION_TYPE))
5967 || TREE_CODE (TREE_TYPE (rhs)) == METHOD_TYPE)
5968 rhs = default_conversion (rhs);
5970 rhstype = TREE_TYPE (rhs);
5971 coder = TREE_CODE (rhstype);
5973 if (coder == ERROR_MARK)
5974 return error_mark_node;
5976 /* We accept references to incomplete types, so we can
5977 return here before checking if RHS is of complete type. */
5979 if (codel == REFERENCE_TYPE)
5981 /* This should eventually happen in convert_arguments. */
5982 int savew = 0, savee = 0;
5985 savew = warningcount, savee = errorcount;
5986 rhs = initialize_reference (type, rhs);
5989 if (warningcount > savew)
5990 cp_warning_at ("in passing argument %P of `%+D'", parmnum, fndecl);
5991 else if (errorcount > savee)
5992 cp_error_at ("in passing argument %P of `%+D'", parmnum, fndecl);
5998 exp = require_complete_type (exp);
5999 if (exp == error_mark_node)
6000 return error_mark_node;
6002 if (TREE_CODE (rhstype) == REFERENCE_TYPE)
6003 rhstype = TREE_TYPE (rhstype);
6005 type = complete_type (type);
6007 if (IS_AGGR_TYPE (type))
6008 return ocp_convert (type, rhs, CONV_IMPLICIT|CONV_FORCE_TEMP, flags);
6010 return convert_for_assignment (type, rhs, errtype, fndecl, parmnum);
6013 /* Expand an ASM statement with operands, handling output operands
6014 that are not variables or INDIRECT_REFS by transforming such
6015 cases into cases that expand_asm_operands can handle.
6017 Arguments are same as for expand_asm_operands.
6019 We don't do default conversions on all inputs, because it can screw
6020 up operands that are expected to be in memory. */
6023 c_expand_asm_operands (string, outputs, inputs, clobbers, vol, filename, line)
6024 tree string, outputs, inputs, clobbers;
6026 const char *filename;
6029 int noutputs = list_length (outputs);
6031 /* o[I] is the place that output number I should be written. */
6032 register tree *o = (tree *) alloca (noutputs * sizeof (tree));
6035 /* Record the contents of OUTPUTS before it is modified. */
6036 for (i = 0, tail = outputs; tail; tail = TREE_CHAIN (tail), i++)
6037 o[i] = TREE_VALUE (tail);
6039 /* Generate the ASM_OPERANDS insn;
6040 store into the TREE_VALUEs of OUTPUTS some trees for
6041 where the values were actually stored. */
6042 expand_asm_operands (string, outputs, inputs, clobbers, vol, filename, line);
6044 /* Copy all the intermediate outputs into the specified outputs. */
6045 for (i = 0, tail = outputs; tail; tail = TREE_CHAIN (tail), i++)
6047 if (o[i] != TREE_VALUE (tail))
6049 expand_expr (build_modify_expr (o[i], NOP_EXPR, TREE_VALUE (tail)),
6050 const0_rtx, VOIDmode, EXPAND_NORMAL);
6053 /* Restore the original value so that it's correct the next
6054 time we expand this function. */
6055 TREE_VALUE (tail) = o[i];
6057 /* Detect modification of read-only values.
6058 (Otherwise done by build_modify_expr.) */
6061 tree type = TREE_TYPE (o[i]);
6062 if (type != error_mark_node
6063 && (CP_TYPE_CONST_P (type)
6064 || (IS_AGGR_TYPE_CODE (TREE_CODE (type))
6065 && C_TYPE_FIELDS_READONLY (type))))
6066 readonly_error (o[i], "modification by `asm'", 1);
6070 /* Those MODIFY_EXPRs could do autoincrements. */
6074 /* If RETVAL is the address of, or a reference to, a local variable or
6075 temporary give an appropraite warning. */
6078 maybe_warn_about_returning_address_of_local (retval)
6081 tree valtype = TREE_TYPE (DECL_RESULT (current_function_decl));
6082 tree whats_returned = retval;
6086 if (TREE_CODE (whats_returned) == COMPOUND_EXPR)
6087 whats_returned = TREE_OPERAND (whats_returned, 1);
6088 else if (TREE_CODE (whats_returned) == CONVERT_EXPR
6089 || TREE_CODE (whats_returned) == NON_LVALUE_EXPR
6090 || TREE_CODE (whats_returned) == NOP_EXPR)
6091 whats_returned = TREE_OPERAND (whats_returned, 0);
6096 if (TREE_CODE (whats_returned) != ADDR_EXPR)
6098 whats_returned = TREE_OPERAND (whats_returned, 0);
6100 if (TREE_CODE (valtype) == REFERENCE_TYPE)
6102 if (TREE_CODE (whats_returned) == AGGR_INIT_EXPR
6103 || TREE_CODE (whats_returned) == TARGET_EXPR)
6105 /* Get the target. */
6106 whats_returned = TREE_OPERAND (whats_returned, 0);
6107 warning ("returning reference to temporary");
6110 if (TREE_CODE (whats_returned) == VAR_DECL
6111 && DECL_NAME (whats_returned)
6112 && TEMP_NAME_P (DECL_NAME (whats_returned)))
6114 warning ("reference to non-lvalue returned");
6119 if (TREE_CODE (whats_returned) == VAR_DECL
6120 && DECL_NAME (whats_returned)
6121 && DECL_FUNCTION_SCOPE_P (whats_returned)
6122 && !(TREE_STATIC (whats_returned)
6123 || TREE_PUBLIC (whats_returned)))
6125 if (TREE_CODE (valtype) == REFERENCE_TYPE)
6126 cp_warning_at ("reference to local variable `%D' returned",
6129 cp_warning_at ("address of local variable `%D' returned",
6135 /* Check that returning RETVAL from the current function is valid.
6136 Return an expression explicitly showing all conversions required to
6137 change RETVAL into the function return type, and to assign it to
6138 the DECL_RESULT for the function. */
6141 check_return_expr (retval)
6145 /* The type actually returned by the function, after any
6148 int fn_returns_value_p;
6150 /* A `volatile' function is one that isn't supposed to return, ever.
6151 (This is a G++ extension, used to get better code for functions
6152 that call the `volatile' function.) */
6153 if (TREE_THIS_VOLATILE (current_function_decl))
6154 warning ("function declared `noreturn' has a `return' statement");
6156 /* Check for various simple errors. */
6157 if (DECL_DESTRUCTOR_P (current_function_decl))
6160 error ("returning a value from a destructor");
6163 else if (DECL_CONSTRUCTOR_P (current_function_decl))
6165 if (in_function_try_handler)
6166 /* If a return statement appears in a handler of the
6167 function-try-block of a constructor, the program is ill-formed. */
6168 error ("cannot return from a handler of a function-try-block of a constructor");
6170 /* You can't return a value from a constructor. */
6171 error ("returning a value from a constructor");
6175 if (processing_template_decl)
6177 current_function_returns_value = 1;
6181 /* When no explicit return-value is given in a function with a named
6182 return value, the named return value is used. */
6183 result = DECL_RESULT (current_function_decl);
6184 valtype = TREE_TYPE (result);
6185 my_friendly_assert (valtype != NULL_TREE, 19990924);
6186 fn_returns_value_p = !VOID_TYPE_P (valtype);
6187 if (!retval && DECL_NAME (result) && fn_returns_value_p)
6190 /* Check for a return statement with no return value in a function
6191 that's supposed to return a value. */
6192 if (!retval && fn_returns_value_p)
6194 pedwarn ("return-statement with no value, in function declared with a non-void return type");
6195 /* Clear this, so finish_function won't say that we reach the
6196 end of a non-void function (which we don't, we gave a
6198 current_function_returns_null = 0;
6200 /* Check for a return statement with a value in a function that
6201 isn't supposed to return a value. */
6202 else if (retval && !fn_returns_value_p)
6204 if (VOID_TYPE_P (TREE_TYPE (retval)))
6205 /* You can return a `void' value from a function of `void'
6206 type. In that case, we have to evaluate the expression for
6207 its side-effects. */
6208 finish_expr_stmt (retval);
6210 pedwarn ("return-statement with a value, in function declared with a void return type");
6212 current_function_returns_null = 1;
6214 /* There's really no value to return, after all. */
6218 /* Remember that this function can sometimes return without a
6220 current_function_returns_null = 1;
6222 /* Remember that this function did return a value. */
6223 current_function_returns_value = 1;
6225 /* Only operator new(...) throw(), can return NULL [expr.new/13]. */
6226 if ((DECL_OVERLOADED_OPERATOR_P (current_function_decl) == NEW_EXPR
6227 || DECL_OVERLOADED_OPERATOR_P (current_function_decl) == VEC_NEW_EXPR)
6228 && !TYPE_NOTHROW_P (TREE_TYPE (current_function_decl))
6230 && null_ptr_cst_p (retval))
6231 warning ("`operator new' must not return NULL unless it is declared `throw()' (or -fcheck-new is in effect)");
6233 /* Effective C++ rule 15. See also start_function. */
6235 && DECL_NAME (current_function_decl) == ansi_assopname(NOP_EXPR)
6236 && retval != current_class_ref)
6237 warning ("`operator=' should return a reference to `*this'");
6239 /* The fabled Named Return Value optimization, as per [class.copy]/15:
6241 [...] For a function with a class return type, if the expression
6242 in the return statement is the name of a local object, and the cv-
6243 unqualified type of the local object is the same as the function
6244 return type, an implementation is permitted to omit creating the tem-
6245 porary object to hold the function return value [...]
6247 So, if this is a value-returning function that always returns the same
6248 local variable, remember it.
6250 It might be nice to be more flexible, and choose the first suitable
6251 variable even if the function sometimes returns something else, but
6252 then we run the risk of clobbering the variable we chose if the other
6253 returned expression uses the chosen variable somehow. And people expect
6254 this restriction, anyway. (jason 2000-11-19)
6256 See finish_function, genrtl_start_function, and declare_return_variable
6257 for other pieces of this optimization. */
6259 if (fn_returns_value_p && flag_elide_constructors)
6261 if (retval != NULL_TREE
6262 && (current_function_return_value == NULL_TREE
6263 || current_function_return_value == retval)
6264 && TREE_CODE (retval) == VAR_DECL
6265 && DECL_CONTEXT (retval) == current_function_decl
6266 && ! TREE_STATIC (retval)
6267 && (DECL_ALIGN (retval)
6268 >= DECL_ALIGN (DECL_RESULT (current_function_decl)))
6269 && same_type_p ((TYPE_MAIN_VARIANT
6270 (TREE_TYPE (retval))),
6272 (TREE_TYPE (TREE_TYPE (current_function_decl))))))
6273 current_function_return_value = retval;
6275 current_function_return_value = error_mark_node;
6278 /* We don't need to do any conversions when there's nothing being
6280 if (!retval || retval == error_mark_node)
6283 /* Do any required conversions. */
6284 if (retval == result || DECL_CONSTRUCTOR_P (current_function_decl))
6285 /* No conversions are required. */
6289 /* The type the function is declared to return. */
6290 tree functype = TREE_TYPE (TREE_TYPE (current_function_decl));
6292 /* First convert the value to the function's return type, then
6293 to the type of return value's location to handle the
6294 case that functype is smaller than the valtype. */
6295 retval = convert_for_initialization
6296 (NULL_TREE, functype, retval, LOOKUP_NORMAL|LOOKUP_ONLYCONVERTING,
6297 "return", NULL_TREE, 0);
6298 retval = convert (valtype, retval);
6300 /* If the conversion failed, treat this just like `return;'. */
6301 if (retval == error_mark_node)
6303 /* We can't initialize a register from a AGGR_INIT_EXPR. */
6304 else if (! current_function_returns_struct
6305 && TREE_CODE (retval) == TARGET_EXPR
6306 && TREE_CODE (TREE_OPERAND (retval, 1)) == AGGR_INIT_EXPR)
6307 retval = build (COMPOUND_EXPR, TREE_TYPE (retval), retval,
6308 TREE_OPERAND (retval, 0));
6310 maybe_warn_about_returning_address_of_local (retval);
6313 /* Actually copy the value returned into the appropriate location. */
6314 if (retval && retval != result)
6315 retval = build (INIT_EXPR, TREE_TYPE (result), result, retval);
6321 /* Returns nonzero if the pointer-type FROM can be converted to the
6322 pointer-type TO via a qualification conversion. If CONSTP is -1,
6323 then we return nonzero if the pointers are similar, and the
6324 cv-qualification signature of FROM is a proper subset of that of TO.
6326 If CONSTP is positive, then all outer pointers have been
6330 comp_ptr_ttypes_real (to, from, constp)
6334 int to_more_cv_qualified = 0;
6336 for (; ; to = TREE_TYPE (to), from = TREE_TYPE (from))
6338 if (TREE_CODE (to) != TREE_CODE (from))
6341 if (TREE_CODE (from) == OFFSET_TYPE
6342 && same_type_p (TYPE_OFFSET_BASETYPE (from),
6343 TYPE_OFFSET_BASETYPE (to)))
6346 /* Const and volatile mean something different for function types,
6347 so the usual checks are not appropriate. */
6348 if (TREE_CODE (to) != FUNCTION_TYPE && TREE_CODE (to) != METHOD_TYPE)
6350 if (!at_least_as_qualified_p (to, from))
6353 if (!at_least_as_qualified_p (from, to))
6358 ++to_more_cv_qualified;
6362 constp &= TYPE_READONLY (to);
6365 if (TREE_CODE (to) != POINTER_TYPE)
6367 same_type_ignoring_top_level_qualifiers_p (to, from)
6368 && (constp >= 0 || to_more_cv_qualified);
6372 /* When comparing, say, char ** to char const **, this function takes the
6373 'char *' and 'char const *'. Do not pass non-pointer types to this
6377 comp_ptr_ttypes (to, from)
6380 return comp_ptr_ttypes_real (to, from, 1);
6383 /* Returns 1 if to and from are (possibly multi-level) pointers to the same
6384 type or inheritance-related types, regardless of cv-quals. */
6387 ptr_reasonably_similar (to, from)
6390 for (; ; to = TREE_TYPE (to), from = TREE_TYPE (from))
6392 /* Any target type is similar enough to void. */
6393 if (TREE_CODE (to) == VOID_TYPE
6394 || TREE_CODE (from) == VOID_TYPE)
6397 if (TREE_CODE (to) != TREE_CODE (from))
6400 if (TREE_CODE (from) == OFFSET_TYPE
6401 && comptypes (TYPE_OFFSET_BASETYPE (to),
6402 TYPE_OFFSET_BASETYPE (from),
6403 COMPARE_BASE | COMPARE_RELAXED))
6406 if (TREE_CODE (to) == INTEGER_TYPE
6407 && TYPE_PRECISION (to) == TYPE_PRECISION (from))
6410 if (TREE_CODE (to) == FUNCTION_TYPE)
6413 if (TREE_CODE (to) != POINTER_TYPE)
6415 (TYPE_MAIN_VARIANT (to), TYPE_MAIN_VARIANT (from),
6416 COMPARE_BASE | COMPARE_RELAXED);
6420 /* Like comp_ptr_ttypes, for const_cast. */
6423 comp_ptr_ttypes_const (to, from)
6426 for (; ; to = TREE_TYPE (to), from = TREE_TYPE (from))
6428 if (TREE_CODE (to) != TREE_CODE (from))
6431 if (TREE_CODE (from) == OFFSET_TYPE
6432 && same_type_p (TYPE_OFFSET_BASETYPE (from),
6433 TYPE_OFFSET_BASETYPE (to)))
6436 if (TREE_CODE (to) != POINTER_TYPE)
6437 return same_type_ignoring_top_level_qualifiers_p (to, from);
6441 /* Like comp_ptr_ttypes, for reinterpret_cast. */
6444 comp_ptr_ttypes_reinterpret (to, from)
6449 for (; ; to = TREE_TYPE (to), from = TREE_TYPE (from))
6451 if (TREE_CODE (from) == OFFSET_TYPE)
6452 from = TREE_TYPE (from);
6453 if (TREE_CODE (to) == OFFSET_TYPE)
6454 to = TREE_TYPE (to);
6456 /* Const and volatile mean something different for function types,
6457 so the usual checks are not appropriate. */
6458 if (TREE_CODE (from) != FUNCTION_TYPE && TREE_CODE (from) != METHOD_TYPE
6459 && TREE_CODE (to) != FUNCTION_TYPE && TREE_CODE (to) != METHOD_TYPE)
6461 if (!at_least_as_qualified_p (to, from))
6465 && !at_least_as_qualified_p (from, to))
6467 constp &= TYPE_READONLY (to);
6470 if (TREE_CODE (from) != POINTER_TYPE
6471 || TREE_CODE (to) != POINTER_TYPE)
6476 /* Returns the type qualifiers for this type, including the qualifiers on the
6477 elements for an array type. */
6480 cp_type_quals (type)
6483 type = strip_array_types (type);
6484 if (type == error_mark_node)
6485 return TYPE_UNQUALIFIED;
6486 return TYPE_QUALS (type);
6489 /* Returns nonzero if the TYPE contains a mutable member */
6492 cp_has_mutable_p (type)
6495 type = strip_array_types (type);
6497 return CLASS_TYPE_P (type) && CLASSTYPE_HAS_MUTABLE (type);
6500 /* Subroutine of casts_away_constness. Make T1 and T2 point at
6501 exemplar types such that casting T1 to T2 is casting away castness
6502 if and only if there is no implicit conversion from T1 to T2. */
6505 casts_away_constness_r (t1, t2)
6512 /* [expr.const.cast]
6514 For multi-level pointer to members and multi-level mixed pointers
6515 and pointers to members (conv.qual), the "member" aspect of a
6516 pointer to member level is ignored when determining if a const
6517 cv-qualifier has been cast away. */
6518 if (TYPE_PTRMEM_P (*t1))
6519 *t1 = build_pointer_type (TREE_TYPE (TREE_TYPE (*t1)));
6520 if (TYPE_PTRMEM_P (*t2))
6521 *t2 = build_pointer_type (TREE_TYPE (TREE_TYPE (*t2)));
6523 /* [expr.const.cast]
6525 For two pointer types:
6527 X1 is T1cv1,1 * ... cv1,N * where T1 is not a pointer type
6528 X2 is T2cv2,1 * ... cv2,M * where T2 is not a pointer type
6531 casting from X1 to X2 casts away constness if, for a non-pointer
6532 type T there does not exist an implicit conversion (clause
6535 Tcv1,(N-K+1) * cv1,(N-K+2) * ... cv1,N *
6539 Tcv2,(M-K+1) * cv2,(M-K+2) * ... cv2,M *. */
6541 if (TREE_CODE (*t1) != POINTER_TYPE
6542 || TREE_CODE (*t2) != POINTER_TYPE)
6544 *t1 = cp_build_qualified_type (void_type_node,
6545 cp_type_quals (*t1));
6546 *t2 = cp_build_qualified_type (void_type_node,
6547 cp_type_quals (*t2));
6551 quals1 = cp_type_quals (*t1);
6552 quals2 = cp_type_quals (*t2);
6553 *t1 = TREE_TYPE (*t1);
6554 *t2 = TREE_TYPE (*t2);
6555 casts_away_constness_r (t1, t2);
6556 *t1 = build_pointer_type (*t1);
6557 *t2 = build_pointer_type (*t2);
6558 *t1 = cp_build_qualified_type (*t1, quals1);
6559 *t2 = cp_build_qualified_type (*t2, quals2);
6562 /* Returns nonzero if casting from TYPE1 to TYPE2 casts away
6566 casts_away_constness (t1, t2)
6570 if (TREE_CODE (t2) == REFERENCE_TYPE)
6572 /* [expr.const.cast]
6574 Casting from an lvalue of type T1 to an lvalue of type T2
6575 using a reference cast casts away constness if a cast from an
6576 rvalue of type "pointer to T1" to the type "pointer to T2"
6577 casts away constness. */
6578 t1 = (TREE_CODE (t1) == REFERENCE_TYPE
6579 ? TREE_TYPE (t1) : t1);
6580 return casts_away_constness (build_pointer_type (t1),
6581 build_pointer_type (TREE_TYPE (t2)));
6584 if (TYPE_PTRMEM_P (t1) && TYPE_PTRMEM_P (t2))
6585 /* [expr.const.cast]
6587 Casting from an rvalue of type "pointer to data member of X
6588 of type T1" to the type "pointer to data member of Y of type
6589 T2" casts away constness if a cast from an rvalue of type
6590 "pointer to T1" to the type "pointer to T2" casts away
6592 return casts_away_constness
6593 (build_pointer_type (TREE_TYPE (TREE_TYPE (t1))),
6594 build_pointer_type (TREE_TYPE (TREE_TYPE (t2))));
6596 /* Casting away constness is only something that makes sense for
6597 pointer or reference types. */
6598 if (TREE_CODE (t1) != POINTER_TYPE
6599 || TREE_CODE (t2) != POINTER_TYPE)
6602 /* Top-level qualifiers don't matter. */
6603 t1 = TYPE_MAIN_VARIANT (t1);
6604 t2 = TYPE_MAIN_VARIANT (t2);
6605 casts_away_constness_r (&t1, &t2);
6606 if (!can_convert (t2, t1))
6612 /* Returns TYPE with its cv qualifiers removed
6613 TYPE is T cv* .. *cv where T is not a pointer type,
6614 returns T * .. *. (If T is an array type, then the cv qualifiers
6615 above are those of the array members.) */
6618 strip_all_pointer_quals (type)
6621 if (TREE_CODE (type) == POINTER_TYPE)
6622 return build_pointer_type (strip_all_pointer_quals (TREE_TYPE (type)));
6623 else if (TREE_CODE (type) == OFFSET_TYPE)
6624 return build_offset_type (TYPE_OFFSET_BASETYPE (type),
6625 strip_all_pointer_quals (TREE_TYPE (type)));
6627 return TYPE_MAIN_VARIANT (type);