1 /* Build expressions with type checking for C++ compiler.
2 Copyright (C) 1987, 1988, 1989, 1992, 1993, 1994, 1995, 1996, 1997, 1998,
3 1999, 2000, 2001, 2002 Free Software Foundation, Inc.
4 Hacked by Michael Tiemann (tiemann@cygnus.com)
6 This file is part of GNU CC.
8 GNU CC is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2, or (at your option)
13 GNU CC is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with GNU CC; see the file COPYING. If not, write to
20 the Free Software Foundation, 59 Temple Place - Suite 330,
21 Boston, MA 02111-1307, USA. */
24 /* This file is part of the C++ front end.
25 It contains routines to build C++ expressions given their operands,
26 including computing the types of the result, C and C++ specific error
27 checks, and some optimization.
29 There are also routines to build RETURN_STMT nodes and CASE_STMT nodes,
30 and to process initializations in declarations (since they work
31 like a strange sort of assignment). */
43 #include "diagnostic.h"
46 static tree convert_for_assignment PARAMS ((tree, tree, const char *, tree,
48 static tree cp_pointer_int_sum PARAMS ((enum tree_code, tree, tree));
49 static tree rationalize_conditional_expr PARAMS ((enum tree_code, tree));
50 static int comp_target_parms PARAMS ((tree, tree));
51 static int comp_ptr_ttypes_real PARAMS ((tree, tree, int));
52 static int comp_ptr_ttypes_const PARAMS ((tree, tree));
53 static int comp_ptr_ttypes_reinterpret PARAMS ((tree, tree));
54 static int comp_except_types PARAMS ((tree, tree, int));
55 static int comp_array_types PARAMS ((int (*) (tree, tree, int), tree,
57 static tree common_base_type PARAMS ((tree, tree));
58 static tree lookup_anon_field PARAMS ((tree, tree));
59 static tree pointer_diff PARAMS ((tree, tree, tree));
60 static tree build_component_addr PARAMS ((tree, tree));
61 static tree qualify_type_recursive PARAMS ((tree, tree));
62 static tree get_delta_difference PARAMS ((tree, tree, int));
63 static int comp_cv_target_types PARAMS ((tree, tree, int));
64 static void casts_away_constness_r PARAMS ((tree *, tree *));
65 static int casts_away_constness PARAMS ((tree, tree));
66 static void maybe_warn_about_returning_address_of_local PARAMS ((tree));
67 static tree strip_all_pointer_quals PARAMS ((tree));
69 /* Return the target type of TYPE, which means return T for:
70 T*, T&, T[], T (...), and otherwise, just T. */
76 if (TREE_CODE (type) == REFERENCE_TYPE)
77 type = TREE_TYPE (type);
78 while (TREE_CODE (type) == POINTER_TYPE
79 || TREE_CODE (type) == ARRAY_TYPE
80 || TREE_CODE (type) == FUNCTION_TYPE
81 || TREE_CODE (type) == METHOD_TYPE
82 || TREE_CODE (type) == OFFSET_TYPE)
83 type = TREE_TYPE (type);
87 /* Do `exp = require_complete_type (exp);' to make sure exp
88 does not have an incomplete type. (That includes void types.)
89 Returns the error_mark_node if the VALUE does not have
90 complete type when this function returns. */
93 require_complete_type (value)
98 if (processing_template_decl || value == error_mark_node)
101 if (TREE_CODE (value) == OVERLOAD)
102 type = unknown_type_node;
104 type = TREE_TYPE (value);
106 /* First, detect a valid value with a complete type. */
107 if (COMPLETE_TYPE_P (type))
110 /* If we see X::Y, we build an OFFSET_TYPE which has
111 not been laid out. Try to avoid an error by interpreting
112 it as this->X::Y, if reasonable. */
113 if (TREE_CODE (value) == OFFSET_REF
114 && current_class_ref != 0
115 && TREE_OPERAND (value, 0) == current_class_ref)
117 value = resolve_offset_ref (value);
118 return require_complete_type (value);
121 if (complete_type_or_else (type, value))
124 return error_mark_node;
127 /* Try to complete TYPE, if it is incomplete. For example, if TYPE is
128 a template instantiation, do the instantiation. Returns TYPE,
129 whether or not it could be completed, unless something goes
130 horribly wrong, in which case the error_mark_node is returned. */
136 if (type == NULL_TREE)
137 /* Rather than crash, we return something sure to cause an error
139 return error_mark_node;
141 if (type == error_mark_node || COMPLETE_TYPE_P (type))
143 else if (TREE_CODE (type) == ARRAY_TYPE && TYPE_DOMAIN (type))
145 tree t = complete_type (TREE_TYPE (type));
146 if (COMPLETE_TYPE_P (t) && ! processing_template_decl)
148 TYPE_NEEDS_CONSTRUCTING (type)
149 = TYPE_NEEDS_CONSTRUCTING (TYPE_MAIN_VARIANT (t));
150 TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type)
151 = TYPE_HAS_NONTRIVIAL_DESTRUCTOR (TYPE_MAIN_VARIANT (t));
153 else if (CLASS_TYPE_P (type) && CLASSTYPE_TEMPLATE_INSTANTIATION (type))
154 instantiate_class_template (TYPE_MAIN_VARIANT (type));
159 /* Like complete_type, but issue an error if the TYPE cannot be
160 completed. VALUE is used for informative diagnostics. WARN_ONLY
161 will cause a warning message to be printed, instead of an error.
162 Returns NULL_TREE if the type cannot be made complete. */
165 complete_type_or_diagnostic (type, value, warn_only)
170 type = complete_type (type);
171 if (type == error_mark_node)
172 /* We already issued an error. */
174 else if (!COMPLETE_TYPE_P (type))
176 cxx_incomplete_type_diagnostic (value, type, warn_only);
183 /* Return truthvalue of whether type of EXP is instantiated. */
189 return (TREE_CODE (exp) == OVERLOAD
190 || TREE_CODE (exp) == TREE_LIST
191 || TREE_TYPE (exp) == unknown_type_node
192 || (TREE_CODE (TREE_TYPE (exp)) == OFFSET_TYPE
193 && TREE_TYPE (TREE_TYPE (exp)) == unknown_type_node));
196 /* Return a pointer or pointer to member type similar to T1, with a
197 cv-qualification signature that is the union of the cv-qualification
198 signatures of T1 and T2: [expr.rel], [expr.eq]. */
201 qualify_type_recursive (t1, t2)
204 if ((TYPE_PTR_P (t1) && TYPE_PTR_P (t2))
205 || (TYPE_PTRMEM_P (t1) && TYPE_PTRMEM_P (t2)))
207 tree tt1 = TREE_TYPE (t1);
208 tree tt2 = TREE_TYPE (t2);
212 tree attributes = (*targetm.merge_type_attributes) (t1, t2);
214 if (TREE_CODE (tt1) == OFFSET_TYPE)
216 b1 = TYPE_OFFSET_BASETYPE (tt1);
217 tt1 = TREE_TYPE (tt1);
218 tt2 = TREE_TYPE (tt2);
223 type_quals = (cp_type_quals (tt1) | cp_type_quals (tt2));
224 tgt = qualify_type_recursive (tt1, tt2);
225 tgt = cp_build_qualified_type (tgt, type_quals);
227 tgt = build_offset_type (b1, tgt);
228 t1 = build_pointer_type (tgt);
229 t1 = build_type_attribute_variant (t1, attributes);
234 /* Return the common type of two parameter lists.
235 We assume that comptypes has already been done and returned 1;
236 if that isn't so, this may crash.
238 As an optimization, free the space we allocate if the parameter
239 lists are already common. */
245 tree oldargs = p1, newargs, n;
249 len = list_length (p1);
250 newargs = tree_last (p1);
252 if (newargs == void_list_node)
261 newargs = tree_cons (NULL_TREE, NULL_TREE, newargs);
266 p1 = TREE_CHAIN (p1), p2 = TREE_CHAIN (p2), n = TREE_CHAIN (n), i++)
268 if (TREE_PURPOSE (p1) && !TREE_PURPOSE (p2))
270 TREE_PURPOSE (n) = TREE_PURPOSE (p1);
273 else if (! TREE_PURPOSE (p1))
275 if (TREE_PURPOSE (p2))
277 TREE_PURPOSE (n) = TREE_PURPOSE (p2);
283 if (1 != simple_cst_equal (TREE_PURPOSE (p1), TREE_PURPOSE (p2)))
285 TREE_PURPOSE (n) = TREE_PURPOSE (p2);
287 if (TREE_VALUE (p1) != TREE_VALUE (p2))
290 TREE_VALUE (n) = merge_types (TREE_VALUE (p1), TREE_VALUE (p2));
293 TREE_VALUE (n) = TREE_VALUE (p1);
301 /* Given a type, perhaps copied for a typedef,
302 find the "original" version of it. */
307 while (TYPE_NAME (t) != NULL_TREE)
309 tree x = TYPE_NAME (t);
310 if (TREE_CODE (x) != TYPE_DECL)
312 x = DECL_ORIGINAL_TYPE (x);
320 /* T1 and T2 are arithmetic or enumeration types. Return the type
321 that will result from the "usual arithmetic conversions" on T1 and
322 T2 as described in [expr]. */
325 type_after_usual_arithmetic_conversions (t1, t2)
329 enum tree_code code1 = TREE_CODE (t1);
330 enum tree_code code2 = TREE_CODE (t2);
333 /* FIXME: Attributes. */
334 my_friendly_assert (ARITHMETIC_TYPE_P (t1)
335 || TREE_CODE (t1) == COMPLEX_TYPE
336 || TREE_CODE (t1) == ENUMERAL_TYPE,
338 my_friendly_assert (ARITHMETIC_TYPE_P (t2)
339 || TREE_CODE (t2) == COMPLEX_TYPE
340 || TREE_CODE (t2) == ENUMERAL_TYPE,
343 /* In what follows, we slightly generalize the rules given in [expr] so
344 as to deal with `long long' and `complex'. First, merge the
346 attributes = (*targetm.merge_type_attributes) (t1, t2);
348 /* If one type is complex, form the common type of the non-complex
349 components, then make that complex. Use T1 or T2 if it is the
351 if (code1 == COMPLEX_TYPE || code2 == COMPLEX_TYPE)
353 tree subtype1 = code1 == COMPLEX_TYPE ? TREE_TYPE (t1) : t1;
354 tree subtype2 = code2 == COMPLEX_TYPE ? TREE_TYPE (t2) : t2;
356 = type_after_usual_arithmetic_conversions (subtype1, subtype2);
358 if (code1 == COMPLEX_TYPE && TREE_TYPE (t1) == subtype)
359 return build_type_attribute_variant (t1, attributes);
360 else if (code2 == COMPLEX_TYPE && TREE_TYPE (t2) == subtype)
361 return build_type_attribute_variant (t2, attributes);
363 return build_type_attribute_variant (build_complex_type (subtype),
367 /* If only one is real, use it as the result. */
368 if (code1 == REAL_TYPE && code2 != REAL_TYPE)
369 return build_type_attribute_variant (t1, attributes);
370 if (code2 == REAL_TYPE && code1 != REAL_TYPE)
371 return build_type_attribute_variant (t2, attributes);
373 /* Perform the integral promotions. */
374 if (code1 != REAL_TYPE)
376 t1 = type_promotes_to (t1);
377 t2 = type_promotes_to (t2);
380 /* Both real or both integers; use the one with greater precision. */
381 if (TYPE_PRECISION (t1) > TYPE_PRECISION (t2))
382 return build_type_attribute_variant (t1, attributes);
383 else if (TYPE_PRECISION (t2) > TYPE_PRECISION (t1))
384 return build_type_attribute_variant (t2, attributes);
386 /* The types are the same; no need to do anything fancy. */
387 if (TYPE_MAIN_VARIANT (t1) == TYPE_MAIN_VARIANT (t2))
388 return build_type_attribute_variant (t1, attributes);
390 if (code1 != REAL_TYPE)
392 /* If one is a sizetype, use it so size_binop doesn't blow up. */
393 if (TYPE_IS_SIZETYPE (t1) > TYPE_IS_SIZETYPE (t2))
394 return build_type_attribute_variant (t1, attributes);
395 if (TYPE_IS_SIZETYPE (t2) > TYPE_IS_SIZETYPE (t1))
396 return build_type_attribute_variant (t2, attributes);
398 /* If one is unsigned long long, then convert the other to unsigned
400 if (same_type_p (TYPE_MAIN_VARIANT (t1), long_long_unsigned_type_node)
401 || same_type_p (TYPE_MAIN_VARIANT (t2), long_long_unsigned_type_node))
402 return build_type_attribute_variant (long_long_unsigned_type_node,
404 /* If one is a long long, and the other is an unsigned long, and
405 long long can represent all the values of an unsigned long, then
406 convert to a long long. Otherwise, convert to an unsigned long
407 long. Otherwise, if either operand is long long, convert the
410 Since we're here, we know the TYPE_PRECISION is the same;
411 therefore converting to long long cannot represent all the values
412 of an unsigned long, so we choose unsigned long long in that
414 if (same_type_p (TYPE_MAIN_VARIANT (t1), long_long_integer_type_node)
415 || same_type_p (TYPE_MAIN_VARIANT (t2), long_long_integer_type_node))
417 tree t = ((TREE_UNSIGNED (t1) || TREE_UNSIGNED (t2))
418 ? long_long_unsigned_type_node
419 : long_long_integer_type_node);
420 return build_type_attribute_variant (t, attributes);
423 /* Go through the same procedure, but for longs. */
424 if (same_type_p (TYPE_MAIN_VARIANT (t1), long_unsigned_type_node)
425 || same_type_p (TYPE_MAIN_VARIANT (t2), long_unsigned_type_node))
426 return build_type_attribute_variant (long_unsigned_type_node,
428 if (same_type_p (TYPE_MAIN_VARIANT (t1), long_integer_type_node)
429 || same_type_p (TYPE_MAIN_VARIANT (t2), long_integer_type_node))
431 tree t = ((TREE_UNSIGNED (t1) || TREE_UNSIGNED (t2))
432 ? long_unsigned_type_node : long_integer_type_node);
433 return build_type_attribute_variant (t, attributes);
435 /* Otherwise prefer the unsigned one. */
436 if (TREE_UNSIGNED (t1))
437 return build_type_attribute_variant (t1, attributes);
439 return build_type_attribute_variant (t2, attributes);
443 if (same_type_p (TYPE_MAIN_VARIANT (t1), long_double_type_node)
444 || same_type_p (TYPE_MAIN_VARIANT (t2), long_double_type_node))
445 return build_type_attribute_variant (long_double_type_node,
447 if (same_type_p (TYPE_MAIN_VARIANT (t1), double_type_node)
448 || same_type_p (TYPE_MAIN_VARIANT (t2), double_type_node))
449 return build_type_attribute_variant (double_type_node,
451 if (same_type_p (TYPE_MAIN_VARIANT (t1), float_type_node)
452 || same_type_p (TYPE_MAIN_VARIANT (t2), float_type_node))
453 return build_type_attribute_variant (float_type_node,
456 /* Two floating-point types whose TYPE_MAIN_VARIANTs are none of
457 the standard C++ floating-point types. Logic earlier in this
458 function has already eliminated the possibility that
459 TYPE_PRECISION (t2) != TYPE_PRECISION (t1), so there's no
460 compelling reason to choose one or the other. */
461 return build_type_attribute_variant (t1, attributes);
465 /* Return the composite pointer type (see [expr.rel]) for T1 and T2.
466 ARG1 and ARG2 are the values with those types. The LOCATION is a
467 string describing the current location, in case an error occurs. */
470 composite_pointer_type (t1, t2, arg1, arg2, location)
475 const char* location;
482 If one operand is a null pointer constant, the composite pointer
483 type is the type of the other operand. */
484 if (null_ptr_cst_p (arg1))
486 if (null_ptr_cst_p (arg2))
489 /* Deal with pointer-to-member functions in the same way as we deal
490 with pointers to functions. */
491 if (TYPE_PTRMEMFUNC_P (t1))
492 t1 = TYPE_PTRMEMFUNC_FN_TYPE (t1);
493 if (TYPE_PTRMEMFUNC_P (t2))
494 t2 = TYPE_PTRMEMFUNC_FN_TYPE (t2);
496 /* Merge the attributes. */
497 attributes = (*targetm.merge_type_attributes) (t1, t2);
503 If one of the operands has type "pointer to cv1 void*", then
504 the other has type "pointer to cv2T", and the composite pointer
505 type is "pointer to cv12 void", where cv12 is the union of cv1
508 If either type is a pointer to void, make sure it is T1. */
509 if (VOID_TYPE_P (TREE_TYPE (t2)))
516 /* Now, if T1 is a pointer to void, merge the qualifiers. */
517 if (VOID_TYPE_P (TREE_TYPE (t1)))
519 if (pedantic && TYPE_PTRFN_P (t2))
520 pedwarn ("ISO C++ forbids %s between pointer of type `void *' and pointer-to-function", location);
523 result_type = cp_build_qualified_type (void_type_node,
525 | cp_type_quals (t2)));
526 result_type = build_pointer_type (result_type);
530 tree full1 = qualify_type_recursive (t1, t2);
531 tree full2 = qualify_type_recursive (t2, t1);
533 int val = comp_target_types (full1, full2, 1);
541 pedwarn ("%s between distinct pointer types `%T' and `%T' lacks a cast",
543 result_type = ptr_type_node;
547 return build_type_attribute_variant (result_type, attributes);
550 /* Return the merged type of two types.
551 We assume that comptypes has already been done and returned 1;
552 if that isn't so, this may crash.
554 This just combines attributes and default arguments; any other
555 differences would cause the two types to compare unalike. */
561 register enum tree_code code1;
562 register enum tree_code code2;
565 /* Save time if the two types are the same. */
568 if (original_type (t1) == original_type (t2))
571 /* If one type is nonsense, use the other. */
572 if (t1 == error_mark_node)
574 if (t2 == error_mark_node)
577 /* Merge the attributes. */
578 attributes = (*targetm.merge_type_attributes) (t1, t2);
580 /* Treat an enum type as the unsigned integer type of the same width. */
582 if (TYPE_PTRMEMFUNC_P (t1))
583 t1 = TYPE_PTRMEMFUNC_FN_TYPE (t1);
584 if (TYPE_PTRMEMFUNC_P (t2))
585 t2 = TYPE_PTRMEMFUNC_FN_TYPE (t2);
587 code1 = TREE_CODE (t1);
588 code2 = TREE_CODE (t2);
594 /* For two pointers, do this recursively on the target type. */
596 tree target = merge_types (TREE_TYPE (t1), TREE_TYPE (t2));
597 int quals = cp_type_quals (t1);
599 if (code1 == POINTER_TYPE)
600 t1 = build_pointer_type (target);
602 t1 = build_reference_type (target);
603 t1 = build_type_attribute_variant (t1, attributes);
604 t1 = cp_build_qualified_type (t1, quals);
606 if (TREE_CODE (target) == METHOD_TYPE)
607 t1 = build_ptrmemfunc_type (t1);
614 tree base = TYPE_OFFSET_BASETYPE (t1);
615 tree target = merge_types (TREE_TYPE (t1), TREE_TYPE (t2));
616 t1 = build_offset_type (base, target);
622 tree elt = merge_types (TREE_TYPE (t1), TREE_TYPE (t2));
623 /* Save space: see if the result is identical to one of the args. */
624 if (elt == TREE_TYPE (t1) && TYPE_DOMAIN (t1))
625 return build_type_attribute_variant (t1, attributes);
626 if (elt == TREE_TYPE (t2) && TYPE_DOMAIN (t2))
627 return build_type_attribute_variant (t2, attributes);
628 /* Merge the element types, and have a size if either arg has one. */
629 t1 = build_cplus_array_type
630 (elt, TYPE_DOMAIN (TYPE_DOMAIN (t1) ? t1 : t2));
635 /* Function types: prefer the one that specified arg types.
636 If both do, merge the arg types. Also merge the return types. */
638 tree valtype = merge_types (TREE_TYPE (t1), TREE_TYPE (t2));
639 tree p1 = TYPE_ARG_TYPES (t1);
640 tree p2 = TYPE_ARG_TYPES (t2);
643 /* Save space: see if the result is identical to one of the args. */
644 if (valtype == TREE_TYPE (t1) && ! p2)
645 return build_type_attribute_variant (t1, attributes);
646 if (valtype == TREE_TYPE (t2) && ! p1)
647 return build_type_attribute_variant (t2, attributes);
649 /* Simple way if one arg fails to specify argument types. */
650 if (p1 == NULL_TREE || TREE_VALUE (p1) == void_type_node)
652 rval = build_function_type (valtype, p2);
653 if ((raises = TYPE_RAISES_EXCEPTIONS (t2)))
654 rval = build_exception_variant (rval, raises);
655 return build_type_attribute_variant (rval, attributes);
657 raises = TYPE_RAISES_EXCEPTIONS (t1);
658 if (p2 == NULL_TREE || TREE_VALUE (p2) == void_type_node)
660 rval = build_function_type (valtype, p1);
662 rval = build_exception_variant (rval, raises);
663 return build_type_attribute_variant (rval, attributes);
666 rval = build_function_type (valtype, commonparms (p1, p2));
667 t1 = build_exception_variant (rval, raises);
673 /* Get this value the long way, since TYPE_METHOD_BASETYPE
674 is just the main variant of this. */
675 tree basetype = TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (t2)));
676 tree raises = TYPE_RAISES_EXCEPTIONS (t1);
679 /* If this was a member function type, get back to the
680 original type of type member function (i.e., without
681 the class instance variable up front. */
682 t1 = build_function_type (TREE_TYPE (t1),
683 TREE_CHAIN (TYPE_ARG_TYPES (t1)));
684 t2 = build_function_type (TREE_TYPE (t2),
685 TREE_CHAIN (TYPE_ARG_TYPES (t2)));
686 t3 = merge_types (t1, t2);
687 t3 = build_cplus_method_type (basetype, TREE_TYPE (t3),
688 TYPE_ARG_TYPES (t3));
689 t1 = build_exception_variant (t3, raises);
695 return build_type_attribute_variant (t1, attributes);
698 /* Return the common type of two types.
699 We assume that comptypes has already been done and returned 1;
700 if that isn't so, this may crash.
702 This is the type for the result of most arithmetic operations
703 if the operands have the given two types. */
709 enum tree_code code1;
710 enum tree_code code2;
712 /* If one type is nonsense, bail. */
713 if (t1 == error_mark_node || t2 == error_mark_node)
714 return error_mark_node;
716 code1 = TREE_CODE (t1);
717 code2 = TREE_CODE (t2);
719 if ((ARITHMETIC_TYPE_P (t1) || code1 == ENUMERAL_TYPE
720 || code1 == COMPLEX_TYPE)
721 && (ARITHMETIC_TYPE_P (t2) || code2 == ENUMERAL_TYPE
722 || code2 == COMPLEX_TYPE))
723 return type_after_usual_arithmetic_conversions (t1, t2);
725 else if ((TYPE_PTR_P (t1) && TYPE_PTR_P (t2))
726 || (TYPE_PTRMEM_P (t1) && TYPE_PTRMEM_P (t2))
727 || (TYPE_PTRMEMFUNC_P (t1) && TYPE_PTRMEMFUNC_P (t2)))
728 return composite_pointer_type (t1, t2, error_mark_node, error_mark_node,
735 /* Compare two exception specifier types for exactness or subsetness, if
736 allowed. Returns 0 for mismatch, 1 for same, 2 if B is allowed by A.
738 [except.spec] "If a class X ... objects of class X or any class publicly
739 and unambigously derrived from X. Similarly, if a pointer type Y * ...
740 exceptions of type Y * or that are pointers to any type publicly and
741 unambigously derrived from Y. Otherwise a function only allows exceptions
742 that have the same type ..."
743 This does not mention cv qualifiers and is different to what throw
744 [except.throw] and catch [except.catch] will do. They will ignore the
745 top level cv qualifiers, and allow qualifiers in the pointer to class
748 We implement the letter of the standard. */
751 comp_except_types (a, b, exact)
755 if (same_type_p (a, b))
759 if (cp_type_quals (a) || cp_type_quals (b))
762 if (TREE_CODE (a) == POINTER_TYPE
763 && TREE_CODE (b) == POINTER_TYPE)
767 if (cp_type_quals (a) || cp_type_quals (b))
771 if (TREE_CODE (a) != RECORD_TYPE
772 || TREE_CODE (b) != RECORD_TYPE)
775 if (ACCESSIBLY_UNIQUELY_DERIVED_P (a, b))
781 /* Return 1 if TYPE1 and TYPE2 are equivalent exception specifiers.
782 If EXACT is 0, T2 can be stricter than T1 (according to 15.4/7),
783 otherwise it must be exact. Exception lists are unordered, but
784 we've already filtered out duplicates. Most lists will be in order,
785 we should try to make use of that. */
788 comp_except_specs (t1, t2, exact)
799 if (t1 == NULL_TREE) /* T1 is ... */
800 return t2 == NULL_TREE || !exact;
801 if (!TREE_VALUE (t1)) /* t1 is EMPTY */
802 return t2 != NULL_TREE && !TREE_VALUE (t2);
803 if (t2 == NULL_TREE) /* T2 is ... */
805 if (TREE_VALUE (t1) && !TREE_VALUE (t2)) /* T2 is EMPTY, T1 is not */
808 /* Neither set is ... or EMPTY, make sure each part of T2 is in T1.
809 Count how many we find, to determine exactness. For exact matching and
810 ordered T1, T2, this is an O(n) operation, otherwise its worst case is
812 for (base = t1; t2 != NULL_TREE; t2 = TREE_CHAIN (t2))
814 for (probe = base; probe != NULL_TREE; probe = TREE_CHAIN (probe))
816 tree a = TREE_VALUE (probe);
817 tree b = TREE_VALUE (t2);
819 if (comp_except_types (a, b, exact))
821 if (probe == base && exact)
822 base = TREE_CHAIN (probe);
827 if (probe == NULL_TREE)
830 return !exact || base == NULL_TREE || length == list_length (t1);
833 /* Compare the array types T1 and T2, using CMP as the type comparison
834 function for the element types. STRICT is as for comptypes. */
837 comp_array_types (cmp, t1, t2, strict)
838 register int (*cmp) PARAMS ((tree, tree, int));
848 /* The type of the array elements must be the same. */
849 if (!(TREE_TYPE (t1) == TREE_TYPE (t2)
850 || (*cmp) (TREE_TYPE (t1), TREE_TYPE (t2),
851 strict & ~COMPARE_REDECLARATION)))
854 d1 = TYPE_DOMAIN (t1);
855 d2 = TYPE_DOMAIN (t2);
860 /* If one of the arrays is dimensionless, and the other has a
861 dimension, they are of different types. However, it is legal to
869 declarations for an array object can specify
870 array types that differ by the presence or absence of a major
871 array bound (_dcl.array_). */
873 return strict & COMPARE_REDECLARATION;
875 /* Check that the dimensions are the same. */
876 return (cp_tree_equal (TYPE_MIN_VALUE (d1),
878 && cp_tree_equal (TYPE_MAX_VALUE (d1),
879 TYPE_MAX_VALUE (d2)));
882 /* Return 1 if T1 and T2 are compatible types for assignment or
883 various other operations. STRICT is a bitwise-or of the COMPARE_*
887 comptypes (t1, t2, strict)
893 int orig_strict = strict;
895 /* The special exemption for redeclaring array types without an
896 array bound only applies at the top level:
901 is not legal, for example. */
902 strict &= ~COMPARE_REDECLARATION;
904 /* Suppress errors caused by previously reported errors */
908 /* This should never happen. */
909 my_friendly_assert (t1 != error_mark_node, 307);
911 if (t2 == error_mark_node)
914 /* If either type is the internal version of sizetype, return the
916 if (TREE_CODE (t1) == INTEGER_TYPE && TYPE_IS_SIZETYPE (t1)
917 && TYPE_DOMAIN (t1) != 0)
918 t1 = TYPE_DOMAIN (t1);
920 if (TREE_CODE (t2) == INTEGER_TYPE && TYPE_IS_SIZETYPE (t2)
921 && TYPE_DOMAIN (t2) != 0)
922 t2 = TYPE_DOMAIN (t2);
924 if (strict & COMPARE_RELAXED)
926 /* Treat an enum type as the unsigned integer type of the same width. */
928 if (TREE_CODE (t1) == ENUMERAL_TYPE)
929 t1 = c_common_type_for_size (TYPE_PRECISION (t1), 1);
930 if (TREE_CODE (t2) == ENUMERAL_TYPE)
931 t2 = c_common_type_for_size (TYPE_PRECISION (t2), 1);
937 if (TYPE_PTRMEMFUNC_P (t1))
938 t1 = TYPE_PTRMEMFUNC_FN_TYPE (t1);
939 if (TYPE_PTRMEMFUNC_P (t2))
940 t2 = TYPE_PTRMEMFUNC_FN_TYPE (t2);
942 /* Different classes of types can't be compatible. */
943 if (TREE_CODE (t1) != TREE_CODE (t2))
946 /* Qualifiers must match. */
947 if (cp_type_quals (t1) != cp_type_quals (t2))
949 if (strict == COMPARE_STRICT
950 && TYPE_FOR_JAVA (t1) != TYPE_FOR_JAVA (t2))
953 /* Allow for two different type nodes which have essentially the same
954 definition. Note that we already checked for equality of the type
955 qualifiers (just above). */
957 if (TYPE_MAIN_VARIANT (t1) == TYPE_MAIN_VARIANT (t2))
960 if (strict & COMPARE_NO_ATTRIBUTES)
962 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
963 else if (! (attrval = (*targetm.comp_type_attributes) (t1, t2)))
966 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
969 switch (TREE_CODE (t1))
971 case TEMPLATE_TEMPLATE_PARM:
972 case BOUND_TEMPLATE_TEMPLATE_PARM:
973 if (TEMPLATE_TYPE_IDX (t1) != TEMPLATE_TYPE_IDX (t2)
974 || TEMPLATE_TYPE_LEVEL (t1) != TEMPLATE_TYPE_LEVEL (t2))
976 if (! comp_template_parms
977 (DECL_TEMPLATE_PARMS (TEMPLATE_TEMPLATE_PARM_TEMPLATE_DECL (t1)),
978 DECL_TEMPLATE_PARMS (TEMPLATE_TEMPLATE_PARM_TEMPLATE_DECL (t2))))
980 if (TREE_CODE (t1) == TEMPLATE_TEMPLATE_PARM)
982 /* Don't check inheritance. */
983 strict = COMPARE_STRICT;
988 if (TYPE_TEMPLATE_INFO (t1) && TYPE_TEMPLATE_INFO (t2)
989 && (TYPE_TI_TEMPLATE (t1) == TYPE_TI_TEMPLATE (t2)
990 || TREE_CODE (t1) == BOUND_TEMPLATE_TEMPLATE_PARM))
991 val = comp_template_args (TYPE_TI_ARGS (t1),
994 if ((strict & COMPARE_BASE) && DERIVED_FROM_P (t1, t2))
996 else if ((strict & COMPARE_RELAXED) && DERIVED_FROM_P (t2, t1))
1001 val = (comptypes (build_pointer_type (TYPE_OFFSET_BASETYPE (t1)),
1002 build_pointer_type (TYPE_OFFSET_BASETYPE (t2)), strict)
1003 && comptypes (TREE_TYPE (t1), TREE_TYPE (t2), strict));
1007 case REFERENCE_TYPE:
1008 t1 = TREE_TYPE (t1);
1009 t2 = TREE_TYPE (t2);
1010 /* first, check whether the referred types match with the
1011 required level of strictness */
1012 val = comptypes (t1, t2, strict);
1015 if (TREE_CODE (t1) == RECORD_TYPE
1016 && TREE_CODE (t2) == RECORD_TYPE)
1022 val = ((TREE_TYPE (t1) == TREE_TYPE (t2)
1023 || comptypes (TREE_TYPE (t1), TREE_TYPE (t2), strict))
1024 && compparms (TYPE_ARG_TYPES (t1), TYPE_ARG_TYPES (t2)));
1028 /* Target types must match incl. qualifiers. We use ORIG_STRICT
1029 here since this is the one place where
1030 COMPARE_REDECLARATION should be used. */
1031 val = comp_array_types (comptypes, t1, t2, orig_strict);
1034 case TEMPLATE_TYPE_PARM:
1035 return TEMPLATE_TYPE_IDX (t1) == TEMPLATE_TYPE_IDX (t2)
1036 && TEMPLATE_TYPE_LEVEL (t1) == TEMPLATE_TYPE_LEVEL (t2);
1039 if (cp_tree_equal (TYPENAME_TYPE_FULLNAME (t1),
1040 TYPENAME_TYPE_FULLNAME (t2)) < 1)
1042 return same_type_p (TYPE_CONTEXT (t1), TYPE_CONTEXT (t2));
1044 case UNBOUND_CLASS_TEMPLATE:
1045 if (cp_tree_equal (TYPE_IDENTIFIER (t1),
1046 TYPE_IDENTIFIER (t2)) < 1)
1048 return same_type_p (TYPE_CONTEXT (t1), TYPE_CONTEXT (t2));
1051 return same_type_p (TREE_TYPE (t1), TREE_TYPE (t2));
1056 return attrval == 2 && val == 1 ? 2 : val;
1059 /* Subroutine of comp_target-types. Make sure that the cv-quals change
1060 only in the same direction as the target type. */
1063 comp_cv_target_types (ttl, ttr, nptrs)
1069 if (!at_least_as_qualified_p (ttl, ttr)
1070 && !at_least_as_qualified_p (ttr, ttl))
1071 /* The qualifications are incomparable. */
1074 if (TYPE_MAIN_VARIANT (ttl) == TYPE_MAIN_VARIANT (ttr))
1075 return more_qualified_p (ttr, ttl) ? -1 : 1;
1077 t = comp_target_types (ttl, ttr, nptrs);
1078 if ((t == 1 && at_least_as_qualified_p (ttl, ttr))
1079 || (t == -1 && at_least_as_qualified_p (ttr, ttl)))
1085 /* Return 1 or -1 if TTL and TTR are pointers to types that are equivalent,
1086 ignoring their qualifiers, 0 if not. Return 1 means that TTR can be
1087 converted to TTL. Return -1 means that TTL can be converted to TTR but
1090 NPTRS is the number of pointers we can strip off and keep cool.
1091 This is used to permit (for aggr A, aggr B) A, B* to convert to A*,
1092 but to not permit B** to convert to A**.
1094 This should go away. Callers should use can_convert or something
1095 similar instead. (jason 17 Apr 1997) */
1098 comp_target_types (ttl, ttr, nptrs)
1102 ttl = TYPE_MAIN_VARIANT (ttl);
1103 ttr = TYPE_MAIN_VARIANT (ttr);
1104 if (same_type_p (ttl, ttr))
1107 if (TREE_CODE (ttr) != TREE_CODE (ttl))
1110 if ((TREE_CODE (ttr) == POINTER_TYPE
1111 || TREE_CODE (ttr) == REFERENCE_TYPE)
1112 /* If we get a pointer with nptrs == 0, we don't allow any tweaking
1113 of the type pointed to. This is necessary for reference init
1114 semantics. We won't get here from a previous call with nptrs == 1;
1115 for multi-level pointers we end up in comp_ptr_ttypes. */
1118 int is_ptr = TREE_CODE (ttr) == POINTER_TYPE;
1120 ttl = TREE_TYPE (ttl);
1121 ttr = TREE_TYPE (ttr);
1125 if (TREE_CODE (ttl) == UNKNOWN_TYPE
1126 || TREE_CODE (ttr) == UNKNOWN_TYPE)
1128 else if (TREE_CODE (ttl) == VOID_TYPE
1129 && TREE_CODE (ttr) != FUNCTION_TYPE
1130 && TREE_CODE (ttr) != METHOD_TYPE
1131 && TREE_CODE (ttr) != OFFSET_TYPE)
1133 else if (TREE_CODE (ttr) == VOID_TYPE
1134 && TREE_CODE (ttl) != FUNCTION_TYPE
1135 && TREE_CODE (ttl) != METHOD_TYPE
1136 && TREE_CODE (ttl) != OFFSET_TYPE)
1138 else if (TREE_CODE (ttl) == POINTER_TYPE
1139 || TREE_CODE (ttl) == ARRAY_TYPE)
1141 if (comp_ptr_ttypes (ttl, ttr))
1143 else if (comp_ptr_ttypes (ttr, ttl))
1149 /* Const and volatile mean something different for function types,
1150 so the usual checks are not appropriate. */
1151 if (TREE_CODE (ttl) == FUNCTION_TYPE || TREE_CODE (ttl) == METHOD_TYPE)
1152 return comp_target_types (ttl, ttr, nptrs - 1);
1154 return comp_cv_target_types (ttl, ttr, nptrs - 1);
1157 if (TREE_CODE (ttr) == ARRAY_TYPE)
1158 return comp_array_types (comp_target_types, ttl, ttr, COMPARE_STRICT);
1159 else if (TREE_CODE (ttr) == FUNCTION_TYPE || TREE_CODE (ttr) == METHOD_TYPE)
1166 if (!same_type_p (TREE_TYPE (ttl), TREE_TYPE (ttr)))
1171 switch (comp_target_types (TREE_TYPE (ttl), TREE_TYPE (ttr), -1))
1180 argsl = TYPE_ARG_TYPES (ttl);
1181 argsr = TYPE_ARG_TYPES (ttr);
1183 /* Compare 'this' here, not in comp_target_parms. */
1184 if (TREE_CODE (ttr) == METHOD_TYPE)
1186 tree tl = TYPE_METHOD_BASETYPE (ttl);
1187 tree tr = TYPE_METHOD_BASETYPE (ttr);
1189 if (!same_or_base_type_p (tr, tl))
1191 if (same_or_base_type_p (tl, tr))
1197 argsl = TREE_CHAIN (argsl);
1198 argsr = TREE_CHAIN (argsr);
1201 switch (comp_target_parms (argsl, argsr))
1209 return saw_contra ? -1 : 1;
1212 else if (TREE_CODE (ttr) == OFFSET_TYPE)
1216 /* Contravariance: we can assign a pointer to base member to a pointer
1217 to derived member. Note difference from simple pointer case, where
1218 we can pass a pointer to derived to a pointer to base. */
1219 if (same_or_base_type_p (TYPE_OFFSET_BASETYPE (ttr),
1220 TYPE_OFFSET_BASETYPE (ttl)))
1222 else if (same_or_base_type_p (TYPE_OFFSET_BASETYPE (ttl),
1223 TYPE_OFFSET_BASETYPE (ttr)))
1233 ttl = TREE_TYPE (ttl);
1234 ttr = TREE_TYPE (ttr);
1236 if (TREE_CODE (ttl) == POINTER_TYPE
1237 || TREE_CODE (ttl) == ARRAY_TYPE)
1239 if (comp_ptr_ttypes (ttl, ttr))
1245 if (comp_cv_target_types (ttl, ttr, nptrs) == 1)
1250 else if (IS_AGGR_TYPE (ttl))
1254 if (same_or_base_type_p (build_pointer_type (ttl),
1255 build_pointer_type (ttr)))
1257 if (same_or_base_type_p (build_pointer_type (ttr),
1258 build_pointer_type (ttl)))
1266 /* Returns 1 if TYPE1 is at least as qualified as TYPE2. */
1269 at_least_as_qualified_p (type1, type2)
1273 /* All qualifiers for TYPE2 must also appear in TYPE1. */
1274 return ((cp_type_quals (type1) & cp_type_quals (type2))
1275 == cp_type_quals (type2));
1278 /* Returns 1 if TYPE1 is more qualified than TYPE2. */
1281 more_qualified_p (type1, type2)
1285 return (cp_type_quals (type1) != cp_type_quals (type2)
1286 && at_least_as_qualified_p (type1, type2));
1289 /* Returns 1 if TYPE1 is more cv-qualified than TYPE2, -1 if TYPE2 is
1290 more cv-qualified that TYPE1, and 0 otherwise. */
1293 comp_cv_qualification (type1, type2)
1297 if (cp_type_quals (type1) == cp_type_quals (type2))
1300 if (at_least_as_qualified_p (type1, type2))
1303 else if (at_least_as_qualified_p (type2, type1))
1309 /* Returns 1 if the cv-qualification signature of TYPE1 is a proper
1310 subset of the cv-qualification signature of TYPE2, and the types
1311 are similar. Returns -1 if the other way 'round, and 0 otherwise. */
1314 comp_cv_qual_signature (type1, type2)
1318 if (comp_ptr_ttypes_real (type2, type1, -1))
1320 else if (comp_ptr_ttypes_real (type1, type2, -1))
1326 /* If two types share a common base type, return that basetype.
1327 If there is not a unique most-derived base type, this function
1328 returns ERROR_MARK_NODE. */
1331 common_base_type (tt1, tt2)
1334 tree best = NULL_TREE;
1337 /* If one is a baseclass of another, that's good enough. */
1338 if (UNIQUELY_DERIVED_FROM_P (tt1, tt2))
1340 if (UNIQUELY_DERIVED_FROM_P (tt2, tt1))
1343 /* Otherwise, try to find a unique baseclass of TT1
1344 that is shared by TT2, and follow that down. */
1345 for (i = CLASSTYPE_N_BASECLASSES (tt1)-1; i >= 0; i--)
1347 tree basetype = TYPE_BINFO_BASETYPE (tt1, i);
1348 tree trial = common_base_type (basetype, tt2);
1351 if (trial == error_mark_node)
1353 if (best == NULL_TREE)
1355 else if (best != trial)
1356 return error_mark_node;
1361 for (i = CLASSTYPE_N_BASECLASSES (tt2)-1; i >= 0; i--)
1363 tree basetype = TYPE_BINFO_BASETYPE (tt2, i);
1364 tree trial = common_base_type (tt1, basetype);
1367 if (trial == error_mark_node)
1369 if (best == NULL_TREE)
1371 else if (best != trial)
1372 return error_mark_node;
1378 /* Subroutines of `comptypes'. */
1380 /* Return 1 if two parameter type lists PARMS1 and PARMS2 are
1381 equivalent in the sense that functions with those parameter types
1382 can have equivalent types. The two lists must be equivalent,
1385 C++: See comment above about TYPE1, TYPE2. */
1388 compparms (parms1, parms2)
1389 tree parms1, parms2;
1391 register tree t1 = parms1, t2 = parms2;
1393 /* An unspecified parmlist matches any specified parmlist
1394 whose argument types don't need default promotions. */
1398 if (t1 == 0 && t2 == 0)
1400 /* If one parmlist is shorter than the other,
1401 they fail to match. */
1402 if (t1 == 0 || t2 == 0)
1404 if (!same_type_p (TREE_VALUE (t2), TREE_VALUE (t1)))
1407 t1 = TREE_CHAIN (t1);
1408 t2 = TREE_CHAIN (t2);
1412 /* This really wants return whether or not parameter type lists
1413 would make their owning functions assignment compatible or not.
1415 The return value is like for comp_target_types.
1417 This should go away, possibly with the exception of the empty parmlist
1418 conversion; there are no conversions between function types in C++.
1419 (jason 17 Apr 1997) */
1422 comp_target_parms (parms1, parms2)
1423 tree parms1, parms2;
1425 register tree t1 = parms1, t2 = parms2;
1426 int warn_contravariance = 0;
1428 /* In C, an unspecified parmlist matches any specified parmlist
1429 whose argument types don't need default promotions. This is not
1430 true for C++, but let's do it anyway for unfixed headers. */
1432 if (t1 == 0 && t2 != 0)
1434 pedwarn ("ISO C++ prohibits conversion from `%#T' to `(...)'",
1436 return self_promoting_args_p (t2);
1439 return self_promoting_args_p (t1);
1441 for (; t1 || t2; t1 = TREE_CHAIN (t1), t2 = TREE_CHAIN (t2))
1445 /* If one parmlist is shorter than the other,
1446 they fail to match, unless STRICT is <= 0. */
1447 if (t1 == 0 || t2 == 0)
1449 p1 = TREE_VALUE (t1);
1450 p2 = TREE_VALUE (t2);
1451 if (same_type_p (p1, p2))
1457 if ((TREE_CODE (p1) == POINTER_TYPE && TREE_CODE (p2) == POINTER_TYPE)
1458 || (TREE_CODE (p1) == REFERENCE_TYPE
1459 && TREE_CODE (p2) == REFERENCE_TYPE))
1461 /* The following is wrong for contravariance,
1462 but many programs depend on it. */
1463 if (TREE_TYPE (p1) == void_type_node)
1465 if (TREE_TYPE (p2) == void_type_node)
1467 warn_contravariance = 1;
1470 if (IS_AGGR_TYPE (TREE_TYPE (p1))
1471 && !same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (p1),
1475 /* Note backwards order due to contravariance. */
1476 if (comp_target_types (p2, p1, 1) <= 0)
1478 if (comp_target_types (p1, p2, 1) > 0)
1480 warn_contravariance = 1;
1486 return warn_contravariance ? -1 : 1;
1489 /* Compute the value of the `sizeof' operator. */
1495 enum tree_code code = TREE_CODE (type);
1498 if (processing_template_decl)
1499 return build_min_nt (SIZEOF_EXPR, type);
1501 if (code == FUNCTION_TYPE)
1503 if (pedantic || warn_pointer_arith)
1504 pedwarn ("ISO C++ forbids applying `sizeof' to a function type");
1505 size = size_one_node;
1507 else if (code == METHOD_TYPE)
1509 if (pedantic || warn_pointer_arith)
1510 pedwarn ("ISO C++ forbids applying `sizeof' to a member function");
1511 size = size_one_node;
1513 else if (code == VOID_TYPE)
1515 if (pedantic || warn_pointer_arith)
1516 pedwarn ("ISO C++ forbids applying `sizeof' to type `void' which is an incomplete type");
1517 size = size_one_node;
1519 else if (code == ERROR_MARK)
1520 size = size_one_node;
1523 /* ARM $5.3.2: ``When applied to a reference, the result is the
1524 size of the referenced object.'' */
1525 if (code == REFERENCE_TYPE)
1526 type = TREE_TYPE (type);
1528 if (code == OFFSET_TYPE)
1530 error ("`sizeof' applied to non-static member");
1531 size = size_zero_node;
1533 else if (!COMPLETE_TYPE_P (complete_type (type)))
1535 error ("`sizeof' applied to incomplete type `%T'", type);
1536 size = size_zero_node;
1539 /* Convert in case a char is more than one unit. */
1540 size = size_binop (CEIL_DIV_EXPR, TYPE_SIZE_UNIT (type),
1541 size_int (TYPE_PRECISION (char_type_node)
1545 /* SIZE will have an integer type with TYPE_IS_SIZETYPE set.
1546 TYPE_IS_SIZETYPE means that certain things (like overflow) will
1547 never happen. However, this node should really have type
1548 `size_t', which is just a typedef for an ordinary integer type. */
1549 size = fold (build1 (NOP_EXPR, c_size_type_node, size));
1550 my_friendly_assert (!TYPE_IS_SIZETYPE (TREE_TYPE (size)),
1560 if (processing_template_decl)
1561 return build_min_nt (SIZEOF_EXPR, e);
1563 if (TREE_CODE (e) == COMPONENT_REF
1564 && DECL_C_BIT_FIELD (TREE_OPERAND (e, 1)))
1565 error ("sizeof applied to a bit-field");
1566 if (is_overloaded_fn (e))
1568 pedwarn ("ISO C++ forbids applying `sizeof' to an expression of function type");
1569 return c_sizeof (char_type_node);
1571 else if (type_unknown_p (e))
1573 cxx_incomplete_type_error (e, TREE_TYPE (e));
1574 return c_sizeof (char_type_node);
1576 /* It's illegal to say `sizeof (X::i)' for `i' a non-static data
1577 member unless you're in a non-static member of X. So hand off to
1578 resolve_offset_ref. [expr.prim] */
1579 else if (TREE_CODE (e) == OFFSET_REF)
1580 e = resolve_offset_ref (e);
1582 if (e == error_mark_node)
1585 return c_sizeof (TREE_TYPE (e));
1589 c_sizeof_nowarn (type)
1592 enum tree_code code = TREE_CODE (type);
1595 if (code == FUNCTION_TYPE
1596 || code == METHOD_TYPE
1597 || code == VOID_TYPE
1598 || code == ERROR_MARK)
1599 size = size_one_node;
1602 if (code == REFERENCE_TYPE)
1603 type = TREE_TYPE (type);
1605 if (!COMPLETE_TYPE_P (type))
1606 size = size_zero_node;
1608 /* Convert in case a char is more than one unit. */
1609 size = size_binop (CEIL_DIV_EXPR, TYPE_SIZE_UNIT (type),
1610 size_int (TYPE_PRECISION (char_type_node)
1614 /* SIZE will have an integer type with TYPE_IS_SIZETYPE set.
1615 TYPE_IS_SIZETYPE means that certain things (like overflow) will
1616 never happen. However, this node should really have type
1617 `size_t', which is just a typedef for an ordinary integer type. */
1618 size = fold (build1 (NOP_EXPR, c_size_type_node, size));
1619 my_friendly_assert (!TYPE_IS_SIZETYPE (TREE_TYPE (size)),
1624 /* Perform the array-to-pointer and function-to-pointer conversions
1627 In addition, references are converted to lvalues and manifest
1628 constants are replaced by their values. */
1631 decay_conversion (exp)
1635 register enum tree_code code;
1637 if (TREE_CODE (exp) == OFFSET_REF)
1638 exp = resolve_offset_ref (exp);
1640 type = TREE_TYPE (exp);
1641 code = TREE_CODE (type);
1643 if (code == REFERENCE_TYPE)
1645 exp = convert_from_reference (exp);
1646 type = TREE_TYPE (exp);
1647 code = TREE_CODE (type);
1650 if (type == error_mark_node)
1651 return error_mark_node;
1653 if (type_unknown_p (exp))
1655 cxx_incomplete_type_error (exp, TREE_TYPE (exp));
1656 return error_mark_node;
1659 /* Constants can be used directly unless they're not loadable. */
1660 if (TREE_CODE (exp) == CONST_DECL)
1661 exp = DECL_INITIAL (exp);
1662 /* Replace a nonvolatile const static variable with its value. We
1663 don't do this for arrays, though; we want the address of the
1664 first element of the array, not the address of the first element
1665 of its initializing constant. */
1666 else if (code != ARRAY_TYPE)
1668 exp = decl_constant_value (exp);
1669 type = TREE_TYPE (exp);
1672 /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
1673 Leave such NOP_EXPRs, since RHS is being used in non-lvalue context. */
1675 if (code == VOID_TYPE)
1677 error ("void value not ignored as it ought to be");
1678 return error_mark_node;
1680 if (code == METHOD_TYPE)
1682 if (code == FUNCTION_TYPE || is_overloaded_fn (exp))
1683 return build_unary_op (ADDR_EXPR, exp, 0);
1684 if (code == ARRAY_TYPE)
1689 if (TREE_CODE (exp) == INDIRECT_REF)
1691 /* Stripping away the INDIRECT_REF is not the right
1692 thing to do for references... */
1693 tree inner = TREE_OPERAND (exp, 0);
1694 if (TREE_CODE (TREE_TYPE (inner)) == REFERENCE_TYPE)
1696 inner = build1 (CONVERT_EXPR,
1697 build_pointer_type (TREE_TYPE
1698 (TREE_TYPE (inner))),
1700 TREE_CONSTANT (inner) = TREE_CONSTANT (TREE_OPERAND (inner, 0));
1702 return cp_convert (build_pointer_type (TREE_TYPE (type)), inner);
1705 if (TREE_CODE (exp) == COMPOUND_EXPR)
1707 tree op1 = decay_conversion (TREE_OPERAND (exp, 1));
1708 return build (COMPOUND_EXPR, TREE_TYPE (op1),
1709 TREE_OPERAND (exp, 0), op1);
1713 && ! (TREE_CODE (exp) == CONSTRUCTOR && TREE_STATIC (exp)))
1715 error ("invalid use of non-lvalue array");
1716 return error_mark_node;
1719 ptrtype = build_pointer_type (TREE_TYPE (type));
1721 if (TREE_CODE (exp) == VAR_DECL)
1723 /* ??? This is not really quite correct
1724 in that the type of the operand of ADDR_EXPR
1725 is not the target type of the type of the ADDR_EXPR itself.
1726 Question is, can this lossage be avoided? */
1727 adr = build1 (ADDR_EXPR, ptrtype, exp);
1728 if (!cxx_mark_addressable (exp))
1729 return error_mark_node;
1730 TREE_CONSTANT (adr) = staticp (exp);
1731 TREE_SIDE_EFFECTS (adr) = 0; /* Default would be, same as EXP. */
1734 /* This way is better for a COMPONENT_REF since it can
1735 simplify the offset for a component. */
1736 adr = build_unary_op (ADDR_EXPR, exp, 1);
1737 return cp_convert (ptrtype, adr);
1740 /* [basic.lval]: Class rvalues can have cv-qualified types; non-class
1741 rvalues always have cv-unqualified types. */
1742 if (! CLASS_TYPE_P (type))
1743 exp = cp_convert (TYPE_MAIN_VARIANT (type), exp);
1749 default_conversion (exp)
1753 enum tree_code code;
1755 exp = decay_conversion (exp);
1757 type = TREE_TYPE (exp);
1758 code = TREE_CODE (type);
1760 if (INTEGRAL_CODE_P (code))
1762 tree t = type_promotes_to (type);
1764 return cp_convert (t, exp);
1770 /* Take the address of an inline function without setting TREE_ADDRESSABLE
1774 inline_conversion (exp)
1777 if (TREE_CODE (exp) == FUNCTION_DECL)
1778 exp = build1 (ADDR_EXPR, build_pointer_type (TREE_TYPE (exp)), exp);
1783 /* Returns nonzero iff exp is a STRING_CST or the result of applying
1784 decay_conversion to one. */
1787 string_conv_p (totype, exp, warn)
1793 if (! flag_const_strings || TREE_CODE (totype) != POINTER_TYPE)
1796 t = TREE_TYPE (totype);
1797 if (!same_type_p (t, char_type_node)
1798 && !same_type_p (t, wchar_type_node))
1801 if (TREE_CODE (exp) == STRING_CST)
1803 /* Make sure that we don't try to convert between char and wchar_t. */
1804 if (!same_type_p (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (exp))), t))
1809 /* Is this a string constant which has decayed to 'const char *'? */
1810 t = build_pointer_type (build_qualified_type (t, TYPE_QUAL_CONST));
1811 if (!same_type_p (TREE_TYPE (exp), t))
1814 if (TREE_CODE (exp) != ADDR_EXPR
1815 || TREE_CODE (TREE_OPERAND (exp, 0)) != STRING_CST)
1819 /* This warning is not very useful, as it complains about printf. */
1820 if (warn && warn_write_strings)
1821 warning ("deprecated conversion from string constant to `%T'", totype);
1827 build_object_ref (datum, basetype, field)
1828 tree datum, basetype, field;
1831 if (datum == error_mark_node)
1832 return error_mark_node;
1834 dtype = TREE_TYPE (datum);
1835 if (TREE_CODE (dtype) == REFERENCE_TYPE)
1836 dtype = TREE_TYPE (dtype);
1837 if (! IS_AGGR_TYPE_CODE (TREE_CODE (dtype)))
1839 error ("request for member `%T::%D' in expression of non-aggregate type `%T'",
1840 basetype, field, dtype);
1841 return error_mark_node;
1843 else if (is_aggr_type (basetype, 1))
1845 tree binfo = NULL_TREE;
1846 datum = build_scoped_ref (datum, basetype, &binfo);
1847 return build_x_component_ref (datum, field, binfo, 1);
1849 return error_mark_node;
1852 /* Like `build_component_ref, but uses an already found field, and converts
1853 from a reference. Must compute access for current_class_ref.
1857 build_component_ref_1 (datum, field, protect)
1861 return convert_from_reference
1862 (build_component_ref (datum, field, NULL_TREE, protect));
1865 /* Given a COND_EXPR, MIN_EXPR, or MAX_EXPR in T, return it in a form that we
1866 can, for example, use as an lvalue. This code used to be in
1867 unary_complex_lvalue, but we needed it to deal with `a = (d == c) ? b : c'
1868 expressions, where we're dealing with aggregates. But now it's again only
1869 called from unary_complex_lvalue. The case (in particular) that led to
1870 this was with CODE == ADDR_EXPR, since it's not an lvalue when we'd
1874 rationalize_conditional_expr (code, t)
1875 enum tree_code code;
1878 /* For MIN_EXPR or MAX_EXPR, fold-const.c has arranged things so that
1879 the first operand is always the one to be used if both operands
1880 are equal, so we know what conditional expression this used to be. */
1881 if (TREE_CODE (t) == MIN_EXPR || TREE_CODE (t) == MAX_EXPR)
1884 build_conditional_expr (build_x_binary_op ((TREE_CODE (t) == MIN_EXPR
1885 ? LE_EXPR : GE_EXPR),
1886 TREE_OPERAND (t, 0),
1887 TREE_OPERAND (t, 1)),
1888 build_unary_op (code, TREE_OPERAND (t, 0), 0),
1889 build_unary_op (code, TREE_OPERAND (t, 1), 0));
1893 build_conditional_expr (TREE_OPERAND (t, 0),
1894 build_unary_op (code, TREE_OPERAND (t, 1), 0),
1895 build_unary_op (code, TREE_OPERAND (t, 2), 0));
1898 /* Given the TYPE of an anonymous union field inside T, return the
1899 FIELD_DECL for the field. If not found return NULL_TREE. Because
1900 anonymous unions can nest, we must also search all anonymous unions
1901 that are directly reachable. */
1904 lookup_anon_field (t, type)
1909 for (field = TYPE_FIELDS (t); field; field = TREE_CHAIN (field))
1911 if (TREE_STATIC (field))
1913 if (TREE_CODE (field) != FIELD_DECL)
1916 /* If we find it directly, return the field. */
1917 if (DECL_NAME (field) == NULL_TREE
1918 && type == TYPE_MAIN_VARIANT (TREE_TYPE (field)))
1923 /* Otherwise, it could be nested, search harder. */
1924 if (DECL_NAME (field) == NULL_TREE
1925 && ANON_AGGR_TYPE_P (TREE_TYPE (field)))
1927 tree subfield = lookup_anon_field (TREE_TYPE (field), type);
1935 /* Build a COMPONENT_REF for a given DATUM, and it's member COMPONENT.
1936 COMPONENT can be an IDENTIFIER_NODE that is the name of the member
1937 that we are interested in, or it can be a FIELD_DECL. */
1940 build_component_ref (datum, component, basetype_path, protect)
1941 tree datum, component, basetype_path;
1944 register tree basetype;
1945 register enum tree_code code;
1946 register tree field = NULL;
1953 if (processing_template_decl)
1954 return build_min_nt (COMPONENT_REF, datum, component);
1956 if (datum == error_mark_node
1957 || TREE_TYPE (datum) == error_mark_node)
1958 return error_mark_node;
1960 /* BASETYPE holds the type of the class containing the COMPONENT. */
1961 basetype = TYPE_MAIN_VARIANT (TREE_TYPE (datum));
1963 /* If DATUM is a COMPOUND_EXPR or COND_EXPR, move our reference
1965 switch (TREE_CODE (datum))
1969 tree value = build_component_ref (TREE_OPERAND (datum, 1), component,
1970 basetype_path, protect);
1971 return build (COMPOUND_EXPR, TREE_TYPE (value),
1972 TREE_OPERAND (datum, 0), value);
1975 return build_conditional_expr
1976 (TREE_OPERAND (datum, 0),
1977 build_component_ref (TREE_OPERAND (datum, 1), component,
1978 basetype_path, protect),
1979 build_component_ref (TREE_OPERAND (datum, 2), component,
1980 basetype_path, protect));
1983 error ("invalid use of `%D'", datum);
1984 datum = error_mark_node;
1991 code = TREE_CODE (basetype);
1993 if (code == REFERENCE_TYPE)
1995 datum = convert_from_reference (datum);
1996 basetype = TYPE_MAIN_VARIANT (TREE_TYPE (datum));
1997 code = TREE_CODE (basetype);
1999 if (TREE_CODE (datum) == OFFSET_REF)
2001 datum = resolve_offset_ref (datum);
2002 basetype = TYPE_MAIN_VARIANT (TREE_TYPE (datum));
2003 code = TREE_CODE (basetype);
2006 /* First, see if there is a field or component with name COMPONENT. */
2007 if (TREE_CODE (component) == TREE_LIST)
2009 /* I could not trigger this code. MvL */
2012 my_friendly_assert (!(TREE_CHAIN (component) == NULL_TREE
2013 && DECL_CHAIN (TREE_VALUE (component)) == NULL_TREE), 309);
2015 return build (COMPONENT_REF, TREE_TYPE (component), datum, component);
2018 if (! IS_AGGR_TYPE_CODE (code))
2020 if (code != ERROR_MARK)
2021 error ("request for member `%D' in `%E', which is of non-aggregate type `%T'",
2022 component, datum, basetype);
2023 return error_mark_node;
2026 if (!complete_type_or_else (basetype, datum))
2027 return error_mark_node;
2029 if (TREE_CODE (component) == BIT_NOT_EXPR)
2031 if (TYPE_IDENTIFIER (basetype) != TREE_OPERAND (component, 0))
2033 error ("destructor specifier `%T::~%T' must have matching names",
2034 basetype, TREE_OPERAND (component, 0));
2035 return error_mark_node;
2037 if (! TYPE_HAS_DESTRUCTOR (basetype))
2039 error ("type `%T' has no destructor", basetype);
2040 return error_mark_node;
2042 return TREE_VEC_ELT (CLASSTYPE_METHOD_VEC (basetype), 1);
2045 /* Look up component name in the structure type definition. */
2046 if (TYPE_VFIELD (basetype)
2047 && DECL_NAME (TYPE_VFIELD (basetype)) == component)
2048 /* Special-case this because if we use normal lookups in an ambiguous
2049 hierarchy, the compiler will abort (because vptr lookups are
2050 not supposed to be ambiguous. */
2051 field = TYPE_VFIELD (basetype);
2052 else if (TREE_CODE (component) == FIELD_DECL)
2054 else if (TREE_CODE (component) == TYPE_DECL)
2056 error ("invalid use of type decl `%#D' as expression", component);
2057 return error_mark_node;
2059 else if (TREE_CODE (component) == TEMPLATE_DECL)
2061 error ("invalid use of template `%#D' as expression", component);
2062 return error_mark_node;
2066 tree name = component;
2068 if (TREE_CODE (component) == TEMPLATE_ID_EXPR)
2069 name = TREE_OPERAND (component, 0);
2070 else if (TREE_CODE (component) == VAR_DECL)
2071 name = DECL_NAME (component);
2072 if (TREE_CODE (component) == NAMESPACE_DECL)
2073 /* Source is in error, but produce a sensible diagnostic. */
2074 name = DECL_NAME (component);
2075 if (basetype_path == NULL_TREE)
2076 basetype_path = TYPE_BINFO (basetype);
2077 field = lookup_field (basetype_path, name,
2078 protect && !VFIELD_NAME_P (name), 0);
2079 if (field == error_mark_node)
2080 return error_mark_node;
2082 if (field == NULL_TREE)
2084 /* Not found as a data field, look for it as a method. If found,
2085 then if this is the only possible one, return it, else
2086 report ambiguity error. */
2087 tree fndecls = lookup_fnfields (basetype_path, name, 1);
2088 if (fndecls == error_mark_node)
2089 return error_mark_node;
2092 /* If the function is unique and static, we can resolve it
2093 now. Otherwise, we have to wait and see what context it is
2094 used in; a component_ref involving a non-static member
2095 function can only be used in a call (expr.ref). */
2097 if (TREE_CHAIN (fndecls) == NULL_TREE
2098 && TREE_CODE (TREE_VALUE (fndecls)) == FUNCTION_DECL)
2100 if (DECL_STATIC_FUNCTION_P (TREE_VALUE (fndecls)))
2102 tree fndecl = TREE_VALUE (fndecls);
2103 enforce_access (basetype_path, fndecl);
2109 /* A unique non-static member function. Other parts
2110 of the compiler expect something with
2111 unknown_type_node to be really overloaded, so
2113 TREE_VALUE (fndecls)
2114 = ovl_cons (TREE_VALUE (fndecls), NULL_TREE);
2118 fndecls = TREE_VALUE (fndecls);
2120 if (IDENTIFIER_TYPENAME_P (name))
2122 /* We want for a conversion op. We need to remember
2123 the actual type we wanted, in case we got a set of
2124 templated conversion operators back. */
2125 fndecls = ovl_cons (OVL_CURRENT (fndecls),
2126 OVL_NEXT (fndecls));
2127 TREE_TYPE (fndecls) = TREE_TYPE (name);
2129 else if (TREE_CODE (component) == TEMPLATE_ID_EXPR)
2130 fndecls = build_nt (TEMPLATE_ID_EXPR,
2131 fndecls, TREE_OPERAND (component, 1));
2133 ref = build (COMPONENT_REF, unknown_type_node,
2138 error ("`%#T' has no member named `%D'", basetype, name);
2139 return error_mark_node;
2141 else if (TREE_TYPE (field) == error_mark_node)
2142 return error_mark_node;
2144 if (TREE_CODE (field) != FIELD_DECL)
2146 if (TREE_CODE (field) == TYPE_DECL)
2147 pedwarn ("invalid use of type decl `%#D' as expression", field);
2148 else if (DECL_RTL (field) != 0)
2151 TREE_USED (field) = 1;
2153 /* Do evaluate the object when accessing a static member. */
2154 if (TREE_SIDE_EFFECTS (datum))
2155 field = build (COMPOUND_EXPR, TREE_TYPE (field), datum, field);
2161 if (TREE_DEPRECATED (field))
2162 warn_deprecated_use (field);
2165 old_basetype = basetype;
2167 /* See if we have to do any conversions so that we pick up the field from the
2169 if (DECL_FIELD_CONTEXT (field) != basetype)
2171 tree context = DECL_FIELD_CONTEXT (field);
2172 tree base = context;
2173 while (!same_type_p (base, basetype) && TYPE_NAME (base)
2174 && ANON_AGGR_TYPE_P (base))
2175 base = TYPE_CONTEXT (base);
2177 /* Handle base classes here... */
2178 if (base != basetype && TYPE_BASE_CONVS_MAY_REQUIRE_CODE_P (basetype))
2181 tree binfo = lookup_base (TREE_TYPE (datum), base, ba_check, &kind);
2183 /* Complain about use of offsetof which will break. */
2184 if (TREE_CODE (datum) == INDIRECT_REF
2185 && integer_zerop (TREE_OPERAND (datum, 0))
2186 && kind == bk_via_virtual)
2189 invalid offsetof from non-POD type `%#T'; use pointer to member instead",
2191 return error_mark_node;
2193 datum = build_base_path (PLUS_EXPR, datum, binfo, 1);
2194 if (datum == error_mark_node)
2195 return error_mark_node;
2199 /* Handle things from anon unions here... */
2200 if (TYPE_NAME (context) && ANON_AGGR_TYPE_P (context))
2202 tree subfield = lookup_anon_field (basetype, context);
2203 tree subdatum = build_component_ref (datum, subfield,
2204 basetype_path, protect);
2205 return build_component_ref (subdatum, field, basetype_path, protect);
2209 /* Complain about other invalid uses of offsetof, even though they will
2210 give the right answer. Note that we complain whether or not they
2211 actually used the offsetof macro, since there's no way to know at this
2212 point. So we just give a warning, instead of a pedwarn. */
2214 && CLASSTYPE_NON_POD_P (old_basetype)
2215 && TREE_CODE (old_datum) == INDIRECT_REF
2216 && integer_zerop (TREE_OPERAND (old_datum, 0)))
2218 invalid offsetof from non-POD type `%#T'; use pointer to member instead",
2221 /* Compute the type of the field, as described in [expr.ref]. */
2222 type_quals = TYPE_UNQUALIFIED;
2223 field_type = TREE_TYPE (field);
2224 if (TREE_CODE (field_type) == REFERENCE_TYPE)
2225 /* The standard says that the type of the result should be the
2226 type referred to by the reference. But for now, at least, we
2227 do the conversion from reference type later. */
2231 type_quals = (cp_type_quals (field_type)
2232 | cp_type_quals (TREE_TYPE (datum)));
2234 /* A field is const (volatile) if the enclosing object, or the
2235 field itself, is const (volatile). But, a mutable field is
2236 not const, even within a const object. */
2237 if (DECL_MUTABLE_P (field))
2238 type_quals &= ~TYPE_QUAL_CONST;
2239 field_type = cp_build_qualified_type (field_type, type_quals);
2242 ref = fold (build (COMPONENT_REF, field_type, datum, field));
2244 /* Mark the expression const or volatile, as appropriate. Even
2245 though we've dealt with the type above, we still have to mark the
2246 expression itself. */
2247 if (type_quals & TYPE_QUAL_CONST)
2248 TREE_READONLY (ref) = 1;
2249 else if (type_quals & TYPE_QUAL_VOLATILE)
2250 TREE_THIS_VOLATILE (ref) = 1;
2255 /* Variant of build_component_ref for use in expressions, which should
2256 never have REFERENCE_TYPE. */
2259 build_x_component_ref (datum, component, basetype_path, protect)
2260 tree datum, component, basetype_path;
2263 tree t = build_component_ref (datum, component, basetype_path, protect);
2265 if (! processing_template_decl)
2266 t = convert_from_reference (t);
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 /* Build a function call to function FUNCTION with parameters PARAMS.
2523 PARAMS is a list--a chain of TREE_LIST nodes--in which the
2524 TREE_VALUE of each node is a parameter-expression. The PARAMS do
2525 not include any object pointer that may be required. FUNCTION's
2526 data type may be a function type or a pointer-to-function.
2528 For C++: If FUNCTION's data type is a TREE_LIST, then the tree list
2529 is the list of possible methods that FUNCTION could conceivably
2530 be. If the list of methods comes from a class, then it will be
2531 a list of lists (where each element is associated with the class
2532 that produced it), otherwise it will be a simple list (for
2533 functions overloaded in global scope).
2535 In the first case, TREE_VALUE (function) is the head of one of those
2536 lists, and TREE_PURPOSE is the name of the function.
2538 In the second case, TREE_PURPOSE (function) is the function's
2541 DECL is the class instance variable, usually CURRENT_CLASS_REF.
2543 When calling a TEMPLATE_DECL, we don't require a complete return
2547 build_x_function_call (function, params, decl)
2548 tree function, params, decl;
2551 tree template_id = NULL_TREE;
2554 if (function == error_mark_node)
2555 return error_mark_node;
2557 if (processing_template_decl)
2558 return build_min_nt (CALL_EXPR, function, params, NULL_TREE);
2560 /* Save explicit template arguments if found */
2561 if (TREE_CODE (function) == TEMPLATE_ID_EXPR)
2563 template_id = function;
2564 function = TREE_OPERAND (function, 0);
2567 type = TREE_TYPE (function);
2569 if (TREE_CODE (type) == OFFSET_TYPE
2570 && TREE_TYPE (type) == unknown_type_node
2571 && TREE_CODE (function) == TREE_LIST
2572 && TREE_CHAIN (function) == NULL_TREE)
2574 /* Undo (Foo:bar)()... */
2575 type = TYPE_OFFSET_BASETYPE (type);
2576 function = TREE_VALUE (function);
2577 my_friendly_assert (TREE_CODE (function) == TREE_LIST, 999);
2578 my_friendly_assert (TREE_CHAIN (function) == NULL_TREE, 999);
2579 function = TREE_VALUE (function);
2580 if (TREE_CODE (function) == OVERLOAD)
2581 function = OVL_FUNCTION (function);
2582 my_friendly_assert (TREE_CODE (function) == FUNCTION_DECL, 999);
2583 function = DECL_NAME (function);
2584 return build_method_call (decl, function, params,
2585 TYPE_BINFO (type), LOOKUP_NORMAL);
2588 if (TREE_CODE (function) == OFFSET_REF
2589 && TREE_CODE (type) != METHOD_TYPE)
2590 function = resolve_offset_ref (function);
2592 if ((TREE_CODE (function) == FUNCTION_DECL
2593 && DECL_STATIC_FUNCTION_P (function))
2594 || (DECL_FUNCTION_TEMPLATE_P (function)
2595 && DECL_STATIC_FUNCTION_P (DECL_TEMPLATE_RESULT (function))))
2596 return build_member_call (DECL_CONTEXT (function),
2598 ? template_id : DECL_NAME (function),
2601 is_method = ((TREE_CODE (function) == TREE_LIST
2602 && current_class_type != NULL_TREE
2603 && (IDENTIFIER_CLASS_VALUE (TREE_PURPOSE (function))
2605 || (TREE_CODE (function) == OVERLOAD
2606 && DECL_FUNCTION_MEMBER_P (OVL_CURRENT (function)))
2607 || TREE_CODE (function) == IDENTIFIER_NODE
2608 || TREE_CODE (type) == METHOD_TYPE
2609 || TYPE_PTRMEMFUNC_P (type));
2611 /* A friend template. Make it look like a toplevel declaration. */
2612 if (! is_method && TREE_CODE (function) == TEMPLATE_DECL)
2613 function = ovl_cons (function, NULL_TREE);
2615 /* Handle methods, friends, and overloaded functions, respectively. */
2618 tree basetype = NULL_TREE;
2620 if (TREE_CODE (function) == OVERLOAD)
2621 function = OVL_CURRENT (function);
2623 if (TREE_CODE (function) == FUNCTION_DECL
2624 || DECL_FUNCTION_TEMPLATE_P (function))
2626 basetype = DECL_CONTEXT (function);
2628 if (DECL_NAME (function))
2629 function = DECL_NAME (function);
2631 function = TYPE_IDENTIFIER (DECL_CONTEXT (function));
2633 else if (TREE_CODE (function) == TREE_LIST)
2635 my_friendly_assert (TREE_CODE (TREE_VALUE (function))
2636 == FUNCTION_DECL, 312);
2637 basetype = DECL_CONTEXT (TREE_VALUE (function));
2638 function = TREE_PURPOSE (function);
2640 else if (TREE_CODE (function) != IDENTIFIER_NODE)
2642 if (TREE_CODE (function) == OFFSET_REF)
2644 if (TREE_OPERAND (function, 0))
2645 decl = TREE_OPERAND (function, 0);
2647 /* Call via a pointer to member function. */
2648 if (decl == NULL_TREE)
2650 error ("pointer to member function called, but not in class scope");
2651 return error_mark_node;
2653 /* What other type of POINTER_TYPE could this be? */
2654 if (TREE_CODE (TREE_TYPE (function)) != POINTER_TYPE
2655 && ! TYPE_PTRMEMFUNC_P (TREE_TYPE (function))
2656 && TREE_CODE (function) != OFFSET_REF)
2657 function = build (OFFSET_REF, TREE_TYPE (type), NULL_TREE,
2662 /* this is an abbreviated method call.
2663 must go through here in case it is a virtual function.
2664 @@ Perhaps this could be optimized. */
2666 if (basetype && (! current_class_type
2667 || ! DERIVED_FROM_P (basetype, current_class_type)))
2668 return build_member_call (basetype, function, params);
2670 if (decl == NULL_TREE)
2672 if (current_class_type == NULL_TREE)
2674 error ("object missing in call to method `%D'", function);
2675 return error_mark_node;
2677 /* Yow: call from a static member function. */
2678 decl = build_dummy_object (current_class_type);
2681 /* Put back explicit template arguments, if any. */
2683 function = template_id;
2684 return build_method_call (decl, function, params,
2685 NULL_TREE, LOOKUP_NORMAL);
2687 else if (TREE_CODE (function) == COMPONENT_REF
2688 && type == unknown_type_node)
2690 /* Undo what we did in build_component_ref. */
2691 decl = TREE_OPERAND (function, 0);
2692 function = TREE_OPERAND (function, 1);
2694 if (TREE_CODE (function) == OVERLOAD
2695 && TREE_TYPE (function) != unknown_type_node)
2696 /* It was a conversion operator. We can't use DECL_NAME, as
2697 that might refer to a templated function. */
2698 function = mangle_conv_op_name_for_type (TREE_TYPE (function));
2699 else if (TREE_CODE (function) == TEMPLATE_ID_EXPR)
2701 my_friendly_assert (!template_id, 20011228);
2703 template_id = function;
2707 function = DECL_NAME (OVL_CURRENT (function));
2711 TREE_OPERAND (template_id, 0) = function;
2712 function = template_id;
2716 return build_method_call (decl, function, params,
2717 NULL_TREE, LOOKUP_NORMAL);
2719 else if (really_overloaded_fn (function))
2721 if (OVL_FUNCTION (function) == NULL_TREE)
2723 error ("function `%D' declared overloaded, but no definitions appear with which to resolve it?!?",
2724 TREE_PURPOSE (function));
2725 return error_mark_node;
2729 /* Put back explicit template arguments, if any. */
2731 function = template_id;
2732 return build_new_function_call (function, params);
2736 /* Remove a potential OVERLOAD around it */
2737 function = OVL_CURRENT (function);
2740 if (TREE_CODE (function) == OFFSET_REF)
2742 /* If the component is a data element (or a virtual function), we play
2743 games here to make things work. */
2746 if (TREE_OPERAND (function, 0))
2747 decl = TREE_OPERAND (function, 0);
2749 decl = current_class_ref;
2751 decl_addr = build_unary_op (ADDR_EXPR, decl, 0);
2753 /* Sigh. OFFSET_REFs are being used for too many things.
2754 They're being used both for -> and ->*, and we want to resolve
2755 the -> cases here, but leave the ->*. We could use
2756 resolve_offset_ref for those, too, but it would call
2757 get_member_function_from_ptrfunc and decl_addr wouldn't get
2758 updated properly. Nasty. */
2759 if (TREE_CODE (TREE_OPERAND (function, 1)) == FIELD_DECL)
2760 function = resolve_offset_ref (function);
2762 function = TREE_OPERAND (function, 1);
2764 function = get_member_function_from_ptrfunc (&decl_addr, function);
2765 params = tree_cons (NULL_TREE, decl_addr, params);
2766 return build_function_call (function, params);
2769 type = TREE_TYPE (function);
2770 if (type != error_mark_node)
2772 if (TREE_CODE (type) == REFERENCE_TYPE)
2773 type = TREE_TYPE (type);
2775 if (IS_AGGR_TYPE (type))
2776 return build_opfncall (CALL_EXPR, LOOKUP_NORMAL, function, params, NULL_TREE);
2781 tree fntype = TREE_TYPE (function);
2782 tree ctypeptr = NULL_TREE;
2784 /* Explicitly named method? */
2785 if (TREE_CODE (function) == FUNCTION_DECL)
2786 ctypeptr = build_pointer_type (DECL_CLASS_CONTEXT (function));
2787 /* Expression with ptr-to-method type? It could either be a plain
2788 usage, or it might be a case where the ptr-to-method is being
2789 passed in as an argument. */
2790 else if (TYPE_PTRMEMFUNC_P (fntype))
2792 tree rec = TYPE_METHOD_BASETYPE (TREE_TYPE
2793 (TYPE_PTRMEMFUNC_FN_TYPE (fntype)));
2794 ctypeptr = build_pointer_type (rec);
2796 /* Unexpected node type? */
2799 if (decl == NULL_TREE)
2801 if (current_function_decl
2802 && DECL_STATIC_FUNCTION_P (current_function_decl))
2803 error ("invalid call to member function needing `this' in static member function scope");
2805 error ("pointer to member function called, but not in class scope");
2806 return error_mark_node;
2808 if (TREE_CODE (TREE_TYPE (decl)) != POINTER_TYPE
2809 && ! TYPE_PTRMEMFUNC_P (TREE_TYPE (decl)))
2811 tree binfo = lookup_base (TREE_TYPE (decl), TREE_TYPE (ctypeptr),
2814 decl = build_unary_op (ADDR_EXPR, decl, 0);
2815 decl = build_base_path (PLUS_EXPR, decl, binfo, 1);
2818 decl = build_c_cast (ctypeptr, decl);
2819 params = tree_cons (NULL_TREE, decl, params);
2822 return build_function_call (function, params);
2825 /* Resolve a pointer to member function. INSTANCE is the object
2826 instance to use, if the member points to a virtual member.
2828 This used to avoid checking for virtual functions if basetype
2829 has no virtual functions, according to an earlier ANSI draft.
2830 With the final ISO C++ rules, such an optimization is
2831 incorrect: A pointer to a derived member can be static_cast
2832 to pointer-to-base-member, as long as the dynamic object
2833 later has the right member. */
2836 get_member_function_from_ptrfunc (instance_ptrptr, function)
2837 tree *instance_ptrptr;
2840 if (TREE_CODE (function) == OFFSET_REF)
2841 function = TREE_OPERAND (function, 1);
2843 if (TYPE_PTRMEMFUNC_P (TREE_TYPE (function)))
2845 tree idx, delta, e1, e2, e3, vtbl, basetype;
2846 tree fntype = TYPE_PTRMEMFUNC_FN_TYPE (TREE_TYPE (function));
2848 tree instance_ptr = *instance_ptrptr;
2849 if (instance_ptr == error_mark_node)
2851 if (TREE_CODE (function) == PTRMEM_CST)
2853 /* Extracting the function address from a pmf is only
2854 allowed with -Wno-pmf-conversions. It only works for
2856 e1 = build_addr_func (PTRMEM_CST_MEMBER (function));
2857 e1 = convert (fntype, e1);
2862 error ("object missing in use of `%E'", function);
2863 return error_mark_node;
2867 if (TREE_SIDE_EFFECTS (instance_ptr))
2868 instance_ptr = save_expr (instance_ptr);
2870 if (TREE_SIDE_EFFECTS (function))
2871 function = save_expr (function);
2873 /* Start by extracting all the information from the PMF itself. */
2874 e3 = PFN_FROM_PTRMEMFUNC (function);
2875 delta = build_component_ref (function, delta_identifier, NULL_TREE, 0);
2876 idx = build1 (NOP_EXPR, vtable_index_type, e3);
2877 switch (TARGET_PTRMEMFUNC_VBIT_LOCATION)
2879 case ptrmemfunc_vbit_in_pfn:
2880 e1 = cp_build_binary_op (BIT_AND_EXPR, idx, integer_one_node);
2881 idx = cp_build_binary_op (MINUS_EXPR, idx, integer_one_node);
2884 case ptrmemfunc_vbit_in_delta:
2885 e1 = cp_build_binary_op (BIT_AND_EXPR, delta, integer_one_node);
2886 delta = cp_build_binary_op (RSHIFT_EXPR, delta, integer_one_node);
2893 /* Convert down to the right base before using the instance. First
2895 basetype = TYPE_METHOD_BASETYPE (TREE_TYPE (fntype));
2896 basetype = lookup_base (TREE_TYPE (TREE_TYPE (instance_ptr)),
2897 basetype, ba_check, NULL);
2898 instance_ptr = build_base_path (PLUS_EXPR, instance_ptr, basetype, 1);
2899 if (instance_ptr == error_mark_node)
2900 return error_mark_node;
2901 /* ...and then the delta in the PMF. */
2902 instance_ptr = build (PLUS_EXPR, TREE_TYPE (instance_ptr),
2903 instance_ptr, delta);
2905 /* Hand back the adjusted 'this' argument to our caller. */
2906 *instance_ptrptr = instance_ptr;
2908 /* Next extract the vtable pointer from the object. */
2909 vtbl = build1 (NOP_EXPR, build_pointer_type (vtbl_ptr_type_node),
2911 vtbl = build_indirect_ref (vtbl, NULL);
2913 /* Finally, extract the function pointer from the vtable. */
2914 e2 = fold (build (PLUS_EXPR, TREE_TYPE (vtbl), vtbl, idx));
2915 e2 = build_indirect_ref (e2, NULL);
2916 TREE_CONSTANT (e2) = 1;
2918 /* When using function descriptors, the address of the
2919 vtable entry is treated as a function pointer. */
2920 if (TARGET_VTABLE_USES_DESCRIPTORS)
2921 e2 = build1 (NOP_EXPR, TREE_TYPE (e2),
2922 build_unary_op (ADDR_EXPR, e2, /*noconvert=*/1));
2924 TREE_TYPE (e2) = TREE_TYPE (e3);
2925 e1 = build_conditional_expr (e1, e2, e3);
2927 /* Make sure this doesn't get evaluated first inside one of the
2928 branches of the COND_EXPR. */
2929 if (TREE_CODE (instance_ptr) == SAVE_EXPR)
2930 e1 = build (COMPOUND_EXPR, TREE_TYPE (e1),
2939 build_function_call_real (function, params, require_complete, flags)
2940 tree function, params;
2941 int require_complete, flags;
2943 register tree fntype, fndecl;
2944 register tree value_type;
2945 register tree coerced_params;
2947 tree name = NULL_TREE, assembler_name = NULL_TREE;
2949 tree original = function;
2951 /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
2952 Strip such NOP_EXPRs, since FUNCTION is used in non-lvalue context. */
2953 if (TREE_CODE (function) == NOP_EXPR
2954 && TREE_TYPE (function) == TREE_TYPE (TREE_OPERAND (function, 0)))
2955 function = TREE_OPERAND (function, 0);
2957 if (TREE_CODE (function) == FUNCTION_DECL)
2959 name = DECL_NAME (function);
2960 assembler_name = DECL_ASSEMBLER_NAME (function);
2962 mark_used (function);
2965 /* Convert anything with function type to a pointer-to-function. */
2966 if (pedantic && DECL_MAIN_P (function))
2967 pedwarn ("ISO C++ forbids calling `::main' from within program");
2969 /* Differs from default_conversion by not setting TREE_ADDRESSABLE
2970 (because calling an inline function does not mean the function
2971 needs to be separately compiled). */
2973 if (DECL_INLINE (function))
2974 function = inline_conversion (function);
2976 function = build_addr_func (function);
2982 function = build_addr_func (function);
2985 if (function == error_mark_node)
2986 return error_mark_node;
2988 fntype = TREE_TYPE (function);
2990 if (TYPE_PTRMEMFUNC_P (fntype))
2992 error ("must use .* or ->* to call pointer-to-member function in `%E (...)'",
2994 return error_mark_node;
2997 is_method = (TREE_CODE (fntype) == POINTER_TYPE
2998 && TREE_CODE (TREE_TYPE (fntype)) == METHOD_TYPE);
3000 if (!((TREE_CODE (fntype) == POINTER_TYPE
3001 && TREE_CODE (TREE_TYPE (fntype)) == FUNCTION_TYPE)
3003 || TREE_CODE (function) == TEMPLATE_ID_EXPR))
3005 error ("`%E' cannot be used as a function", original);
3006 return error_mark_node;
3009 /* fntype now gets the type of function pointed to. */
3010 fntype = TREE_TYPE (fntype);
3012 /* Convert the parameters to the types declared in the
3013 function prototype, or apply default promotions. */
3015 if (flags & LOOKUP_COMPLAIN)
3016 coerced_params = convert_arguments (TYPE_ARG_TYPES (fntype),
3017 params, fndecl, LOOKUP_NORMAL);
3019 coerced_params = convert_arguments (TYPE_ARG_TYPES (fntype),
3022 if (coerced_params == error_mark_node)
3024 if (flags & LOOKUP_SPECULATIVELY)
3027 return error_mark_node;
3030 /* Check for errors in format strings. */
3033 check_function_format (NULL, TYPE_ATTRIBUTES (fntype), coerced_params);
3035 /* Recognize certain built-in functions so we can make tree-codes
3036 other than CALL_EXPR. We do this when it enables fold-const.c
3037 to do something useful. */
3039 if (TREE_CODE (function) == ADDR_EXPR
3040 && TREE_CODE (TREE_OPERAND (function, 0)) == FUNCTION_DECL
3041 && DECL_BUILT_IN (TREE_OPERAND (function, 0)))
3043 result = expand_tree_builtin (TREE_OPERAND (function, 0),
3044 params, coerced_params);
3049 /* Some built-in function calls will be evaluated at
3050 compile-time in fold (). */
3051 result = fold (build_call (function, coerced_params));
3052 value_type = TREE_TYPE (result);
3054 if (require_complete)
3056 if (TREE_CODE (value_type) == VOID_TYPE)
3058 result = require_complete_type (result);
3060 if (IS_AGGR_TYPE (value_type))
3061 result = build_cplus_new (value_type, result);
3062 return convert_from_reference (result);
3066 build_function_call (function, params)
3067 tree function, params;
3069 return build_function_call_real (function, params, 1, LOOKUP_NORMAL);
3072 /* Convert the actual parameter expressions in the list VALUES
3073 to the types in the list TYPELIST.
3074 If parmdecls is exhausted, or when an element has NULL as its type,
3075 perform the default conversions.
3077 NAME is an IDENTIFIER_NODE or 0. It is used only for error messages.
3079 This is also where warnings about wrong number of args are generated.
3081 Return a list of expressions for the parameters as converted.
3083 Both VALUES and the returned value are chains of TREE_LIST nodes
3084 with the elements of the list in the TREE_VALUE slots of those nodes.
3086 In C++, unspecified trailing parameters can be filled in with their
3087 default arguments, if such were specified. Do so here. */
3090 convert_arguments (typelist, values, fndecl, flags)
3091 tree typelist, values, fndecl;
3094 register tree typetail, valtail;
3095 register tree result = NULL_TREE;
3096 const char *called_thing = 0;
3099 /* Argument passing is always copy-initialization. */
3100 flags |= LOOKUP_ONLYCONVERTING;
3104 if (TREE_CODE (TREE_TYPE (fndecl)) == METHOD_TYPE)
3106 if (DECL_NAME (fndecl) == NULL_TREE
3107 || IDENTIFIER_HAS_TYPE_VALUE (DECL_NAME (fndecl)))
3108 called_thing = "constructor";
3110 called_thing = "member function";
3113 called_thing = "function";
3116 for (valtail = values, typetail = typelist;
3118 valtail = TREE_CHAIN (valtail), i++)
3120 register tree type = typetail ? TREE_VALUE (typetail) : 0;
3121 register tree val = TREE_VALUE (valtail);
3123 if (val == error_mark_node)
3124 return error_mark_node;
3126 if (type == void_type_node)
3130 cp_error_at ("too many arguments to %s `%+#D'", called_thing,
3132 error ("at this point in file");
3135 error ("too many arguments to function");
3136 /* In case anybody wants to know if this argument
3139 TREE_TYPE (tree_last (result)) = error_mark_node;
3143 if (TREE_CODE (val) == OFFSET_REF)
3144 val = resolve_offset_ref (val);
3146 /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
3147 Strip such NOP_EXPRs, since VAL is used in non-lvalue context. */
3148 if (TREE_CODE (val) == NOP_EXPR
3149 && TREE_TYPE (val) == TREE_TYPE (TREE_OPERAND (val, 0))
3150 && (type == 0 || TREE_CODE (type) != REFERENCE_TYPE))
3151 val = TREE_OPERAND (val, 0);
3153 if (type == 0 || TREE_CODE (type) != REFERENCE_TYPE)
3155 if (TREE_CODE (TREE_TYPE (val)) == ARRAY_TYPE
3156 || TREE_CODE (TREE_TYPE (val)) == FUNCTION_TYPE
3157 || TREE_CODE (TREE_TYPE (val)) == METHOD_TYPE)
3158 val = default_conversion (val);
3161 if (val == error_mark_node)
3162 return error_mark_node;
3166 /* Formal parm type is specified by a function prototype. */
3169 if (!COMPLETE_TYPE_P (complete_type (type)))
3171 error ("parameter type of called function is incomplete");
3176 parmval = convert_for_initialization
3177 (NULL_TREE, type, val, flags,
3178 "argument passing", fndecl, i);
3179 if (PROMOTE_PROTOTYPES
3180 && INTEGRAL_TYPE_P (type)
3181 && (TYPE_PRECISION (type)
3182 < TYPE_PRECISION (integer_type_node)))
3183 parmval = default_conversion (parmval);
3186 if (parmval == error_mark_node)
3187 return error_mark_node;
3189 result = tree_cons (NULL_TREE, parmval, result);
3193 if (TREE_CODE (TREE_TYPE (val)) == REFERENCE_TYPE)
3194 val = convert_from_reference (val);
3196 if (fndecl && DECL_BUILT_IN (fndecl)
3197 && DECL_FUNCTION_CODE (fndecl) == BUILT_IN_CONSTANT_P)
3198 /* Don't do ellipsis conversion for __built_in_constant_p
3199 as this will result in spurious warnings for non-POD
3201 val = require_complete_type (val);
3203 val = convert_arg_to_ellipsis (val);
3205 result = tree_cons (NULL_TREE, val, result);
3209 typetail = TREE_CHAIN (typetail);
3212 if (typetail != 0 && typetail != void_list_node)
3214 /* See if there are default arguments that can be used */
3215 if (TREE_PURPOSE (typetail))
3217 for (; typetail != void_list_node; ++i)
3220 = convert_default_arg (TREE_VALUE (typetail),
3221 TREE_PURPOSE (typetail),
3224 if (parmval == error_mark_node)
3225 return error_mark_node;
3227 result = tree_cons (0, parmval, result);
3228 typetail = TREE_CHAIN (typetail);
3229 /* ends with `...'. */
3230 if (typetail == NULL_TREE)
3238 cp_error_at ("too few arguments to %s `%+#D'",
3239 called_thing, fndecl);
3240 error ("at this point in file");
3243 error ("too few arguments to function");
3244 return error_mark_list;
3248 return nreverse (result);
3251 /* Build a binary-operation expression, after performing default
3252 conversions on the operands. CODE is the kind of expression to build. */
3255 build_x_binary_op (code, arg1, arg2)
3256 enum tree_code code;
3259 if (processing_template_decl)
3260 return build_min_nt (code, arg1, arg2);
3262 return build_new_op (code, LOOKUP_NORMAL, arg1, arg2, NULL_TREE);
3265 /* Build a binary-operation expression without default conversions.
3266 CODE is the kind of expression to build.
3267 This function differs from `build' in several ways:
3268 the data type of the result is computed and recorded in it,
3269 warnings are generated if arg data types are invalid,
3270 special handling for addition and subtraction of pointers is known,
3271 and some optimization is done (operations on narrow ints
3272 are done in the narrower type when that gives the same result).
3273 Constant folding is also done before the result is returned.
3275 Note that the operands will never have enumeral types
3276 because either they have just had the default conversions performed
3277 or they have both just been converted to some other type in which
3278 the arithmetic is to be done.
3280 C++: must do special pointer arithmetic when implementing
3281 multiple inheritance, and deal with pointer to member functions. */
3284 build_binary_op (code, orig_op0, orig_op1, convert_p)
3285 enum tree_code code;
3286 tree orig_op0, orig_op1;
3287 int convert_p ATTRIBUTE_UNUSED;
3290 register enum tree_code code0, code1;
3293 /* Expression code to give to the expression when it is built.
3294 Normally this is CODE, which is what the caller asked for,
3295 but in some special cases we change it. */
3296 register enum tree_code resultcode = code;
3298 /* Data type in which the computation is to be performed.
3299 In the simplest cases this is the common type of the arguments. */
3300 register tree result_type = NULL;
3302 /* Nonzero means operands have already been type-converted
3303 in whatever way is necessary.
3304 Zero means they need to be converted to RESULT_TYPE. */
3307 /* Nonzero means create the expression with this type, rather than
3309 tree build_type = 0;
3311 /* Nonzero means after finally constructing the expression
3312 convert it to this type. */
3313 tree final_type = 0;
3315 /* Nonzero if this is an operation like MIN or MAX which can
3316 safely be computed in short if both args are promoted shorts.
3317 Also implies COMMON.
3318 -1 indicates a bitwise operation; this makes a difference
3319 in the exact conditions for when it is safe to do the operation
3320 in a narrower mode. */
3323 /* Nonzero if this is a comparison operation;
3324 if both args are promoted shorts, compare the original shorts.
3325 Also implies COMMON. */
3326 int short_compare = 0;
3328 /* Nonzero if this is a right-shift operation, which can be computed on the
3329 original short and then promoted if the operand is a promoted short. */
3330 int short_shift = 0;
3332 /* Nonzero means set RESULT_TYPE to the common type of the args. */
3335 /* Apply default conversions. */
3339 if (code == TRUTH_AND_EXPR || code == TRUTH_ANDIF_EXPR
3340 || code == TRUTH_OR_EXPR || code == TRUTH_ORIF_EXPR
3341 || code == TRUTH_XOR_EXPR)
3343 if (!really_overloaded_fn (op0))
3344 op0 = decay_conversion (op0);
3345 if (!really_overloaded_fn (op1))
3346 op1 = decay_conversion (op1);
3350 if (!really_overloaded_fn (op0))
3351 op0 = default_conversion (op0);
3352 if (!really_overloaded_fn (op1))
3353 op1 = default_conversion (op1);
3356 /* Strip NON_LVALUE_EXPRs, etc., since we aren't using as an lvalue. */
3357 STRIP_TYPE_NOPS (op0);
3358 STRIP_TYPE_NOPS (op1);
3360 /* DTRT if one side is an overloaded function, but complain about it. */
3361 if (type_unknown_p (op0))
3363 tree t = instantiate_type (TREE_TYPE (op1), op0, tf_none);
3364 if (t != error_mark_node)
3366 pedwarn ("assuming cast to type `%T' from overloaded function",
3371 if (type_unknown_p (op1))
3373 tree t = instantiate_type (TREE_TYPE (op0), op1, tf_none);
3374 if (t != error_mark_node)
3376 pedwarn ("assuming cast to type `%T' from overloaded function",
3382 type0 = TREE_TYPE (op0);
3383 type1 = TREE_TYPE (op1);
3385 /* The expression codes of the data types of the arguments tell us
3386 whether the arguments are integers, floating, pointers, etc. */
3387 code0 = TREE_CODE (type0);
3388 code1 = TREE_CODE (type1);
3390 /* If an error was already reported for one of the arguments,
3391 avoid reporting another error. */
3393 if (code0 == ERROR_MARK || code1 == ERROR_MARK)
3394 return error_mark_node;
3399 /* Handle the pointer + int case. */
3400 if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
3401 return cp_pointer_int_sum (PLUS_EXPR, op0, op1);
3402 else if (code1 == POINTER_TYPE && code0 == INTEGER_TYPE)
3403 return cp_pointer_int_sum (PLUS_EXPR, op1, op0);
3409 /* Subtraction of two similar pointers.
3410 We must subtract them as integers, then divide by object size. */
3411 if (code0 == POINTER_TYPE && code1 == POINTER_TYPE
3412 && comp_target_types (type0, type1, 1))
3413 return pointer_diff (op0, op1, common_type (type0, type1));
3414 /* Handle pointer minus int. Just like pointer plus int. */
3415 else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
3416 return cp_pointer_int_sum (MINUS_EXPR, op0, op1);
3425 case TRUNC_DIV_EXPR:
3427 case FLOOR_DIV_EXPR:
3428 case ROUND_DIV_EXPR:
3429 case EXACT_DIV_EXPR:
3430 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE
3431 || code0 == COMPLEX_TYPE)
3432 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
3433 || code1 == COMPLEX_TYPE))
3435 if (TREE_CODE (op1) == INTEGER_CST && integer_zerop (op1))
3436 warning ("division by zero in `%E / 0'", op0);
3437 else if (TREE_CODE (op1) == REAL_CST && real_zerop (op1))
3438 warning ("division by zero in `%E / 0.'", op0);
3440 if (!(code0 == INTEGER_TYPE && code1 == INTEGER_TYPE))
3441 resultcode = RDIV_EXPR;
3443 /* When dividing two signed integers, we have to promote to int.
3444 unless we divide by a constant != -1. Note that default
3445 conversion will have been performed on the operands at this
3446 point, so we have to dig out the original type to find out if
3448 shorten = ((TREE_CODE (op0) == NOP_EXPR
3449 && TREE_UNSIGNED (TREE_TYPE (TREE_OPERAND (op0, 0))))
3450 || (TREE_CODE (op1) == INTEGER_CST
3451 && ! integer_all_onesp (op1)));
3458 case BIT_ANDTC_EXPR:
3461 if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
3465 case TRUNC_MOD_EXPR:
3466 case FLOOR_MOD_EXPR:
3467 if (code1 == INTEGER_TYPE && integer_zerop (op1))
3468 warning ("division by zero in `%E %% 0'", op0);
3469 else if (code1 == REAL_TYPE && real_zerop (op1))
3470 warning ("division by zero in `%E %% 0.'", op0);
3472 if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
3474 /* Although it would be tempting to shorten always here, that loses
3475 on some targets, since the modulo instruction is undefined if the
3476 quotient can't be represented in the computation mode. We shorten
3477 only if unsigned or if dividing by something we know != -1. */
3478 shorten = ((TREE_CODE (op0) == NOP_EXPR
3479 && TREE_UNSIGNED (TREE_TYPE (TREE_OPERAND (op0, 0))))
3480 || (TREE_CODE (op1) == INTEGER_CST
3481 && ! integer_all_onesp (op1)));
3486 case TRUTH_ANDIF_EXPR:
3487 case TRUTH_ORIF_EXPR:
3488 case TRUTH_AND_EXPR:
3490 result_type = boolean_type_node;
3493 /* Shift operations: result has same type as first operand;
3494 always convert second operand to int.
3495 Also set SHORT_SHIFT if shifting rightward. */
3498 if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
3500 result_type = type0;
3501 if (TREE_CODE (op1) == INTEGER_CST)
3503 if (tree_int_cst_lt (op1, integer_zero_node))
3504 warning ("right shift count is negative");
3507 if (! integer_zerop (op1))
3509 if (compare_tree_int (op1, TYPE_PRECISION (type0)) >= 0)
3510 warning ("right shift count >= width of type");
3513 /* Convert the shift-count to an integer, regardless of
3514 size of value being shifted. */
3515 if (TYPE_MAIN_VARIANT (TREE_TYPE (op1)) != integer_type_node)
3516 op1 = cp_convert (integer_type_node, op1);
3517 /* Avoid converting op1 to result_type later. */
3523 if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
3525 result_type = type0;
3526 if (TREE_CODE (op1) == INTEGER_CST)
3528 if (tree_int_cst_lt (op1, integer_zero_node))
3529 warning ("left shift count is negative");
3530 else if (compare_tree_int (op1, TYPE_PRECISION (type0)) >= 0)
3531 warning ("left shift count >= width of type");
3533 /* Convert the shift-count to an integer, regardless of
3534 size of value being shifted. */
3535 if (TYPE_MAIN_VARIANT (TREE_TYPE (op1)) != integer_type_node)
3536 op1 = cp_convert (integer_type_node, op1);
3537 /* Avoid converting op1 to result_type later. */
3544 if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
3546 result_type = type0;
3547 if (TREE_CODE (op1) == INTEGER_CST)
3549 if (tree_int_cst_lt (op1, integer_zero_node))
3550 warning ("%s rotate count is negative",
3551 (code == LROTATE_EXPR) ? "left" : "right");
3552 else if (compare_tree_int (op1, TYPE_PRECISION (type0)) >= 0)
3553 warning ("%s rotate count >= width of type",
3554 (code == LROTATE_EXPR) ? "left" : "right");
3556 /* Convert the shift-count to an integer, regardless of
3557 size of value being shifted. */
3558 if (TYPE_MAIN_VARIANT (TREE_TYPE (op1)) != integer_type_node)
3559 op1 = cp_convert (integer_type_node, op1);
3565 if (warn_float_equal && (code0 == REAL_TYPE || code1 == REAL_TYPE))
3566 warning ("comparing floating point with == or != is unsafe");
3568 build_type = boolean_type_node;
3569 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE
3570 || code0 == COMPLEX_TYPE)
3571 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
3572 || code1 == COMPLEX_TYPE))
3574 else if (code0 == POINTER_TYPE && code1 == POINTER_TYPE)
3575 result_type = composite_pointer_type (type0, type1, op0, op1,
3577 else if (code0 == POINTER_TYPE && null_ptr_cst_p (op1))
3578 result_type = type0;
3579 else if (code1 == POINTER_TYPE && null_ptr_cst_p (op0))
3580 result_type = type1;
3581 else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
3583 result_type = type0;
3584 error ("ISO C++ forbids comparison between pointer and integer");
3586 else if (code0 == INTEGER_TYPE && code1 == POINTER_TYPE)
3588 result_type = type1;
3589 error ("ISO C++ forbids comparison between pointer and integer");
3591 else if (TYPE_PTRMEMFUNC_P (type0) && null_ptr_cst_p (op1))
3593 op0 = build_component_ref (op0, pfn_identifier, NULL_TREE, 0);
3594 op1 = cp_convert (TREE_TYPE (op0), integer_zero_node);
3595 result_type = TREE_TYPE (op0);
3597 else if (TYPE_PTRMEMFUNC_P (type1) && null_ptr_cst_p (op0))
3598 return cp_build_binary_op (code, op1, op0);
3599 else if (TYPE_PTRMEMFUNC_P (type0) && TYPE_PTRMEMFUNC_P (type1)
3600 && same_type_p (type0, type1))
3602 /* E will be the final comparison. */
3604 /* E1 and E2 are for scratch. */
3612 if (TREE_SIDE_EFFECTS (op0))
3613 op0 = save_expr (op0);
3614 if (TREE_SIDE_EFFECTS (op1))
3615 op1 = save_expr (op1);
3620 && (!op0.pfn || op0.delta == op1.delta))
3622 The reason for the `!op0.pfn' bit is that a NULL
3623 pointer-to-member is any member with a zero PFN; the
3624 DELTA field is unspecified. */
3625 pfn0 = pfn_from_ptrmemfunc (op0);
3626 pfn1 = pfn_from_ptrmemfunc (op1);
3627 delta0 = build_component_ref (op0, delta_identifier,
3629 delta1 = build_component_ref (op1, delta_identifier,
3631 e1 = cp_build_binary_op (EQ_EXPR, delta0, delta1);
3632 e2 = cp_build_binary_op (EQ_EXPR,
3634 cp_convert (TREE_TYPE (pfn0),
3635 integer_zero_node));
3636 e1 = cp_build_binary_op (TRUTH_ORIF_EXPR, e1, e2);
3637 e2 = build (EQ_EXPR, boolean_type_node, pfn0, pfn1);
3638 e = cp_build_binary_op (TRUTH_ANDIF_EXPR, e2, e1);
3639 if (code == EQ_EXPR)
3641 return cp_build_binary_op (EQ_EXPR, e, integer_zero_node);
3643 else if ((TYPE_PTRMEMFUNC_P (type0)
3644 && same_type_p (TYPE_PTRMEMFUNC_FN_TYPE (type0), type1))
3645 || (TYPE_PTRMEMFUNC_P (type1)
3646 && same_type_p (TYPE_PTRMEMFUNC_FN_TYPE (type1), type0)))
3652 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE)
3653 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE))
3655 else if (code0 == POINTER_TYPE && code1 == POINTER_TYPE)
3656 result_type = composite_pointer_type (type0, type1, op0, op1,
3664 build_type = boolean_type_node;
3665 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE)
3666 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE))
3668 else if (code0 == POINTER_TYPE && code1 == POINTER_TYPE)
3669 result_type = composite_pointer_type (type0, type1, op0, op1,
3671 else if (code0 == POINTER_TYPE && TREE_CODE (op1) == INTEGER_CST
3672 && integer_zerop (op1))
3673 result_type = type0;
3674 else if (code1 == POINTER_TYPE && TREE_CODE (op0) == INTEGER_CST
3675 && integer_zerop (op0))
3676 result_type = type1;
3677 else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
3679 result_type = type0;
3680 pedwarn ("ISO C++ forbids comparison between pointer and integer");
3682 else if (code0 == INTEGER_TYPE && code1 == POINTER_TYPE)
3684 result_type = type1;
3685 pedwarn ("ISO C++ forbids comparison between pointer and integer");
3689 case UNORDERED_EXPR:
3696 build_type = integer_type_node;
3697 if (code0 != REAL_TYPE || code1 != REAL_TYPE)
3699 error ("unordered comparison on non-floating point argument");
3700 return error_mark_node;
3709 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE || code0 == COMPLEX_TYPE)
3711 (code1 == INTEGER_TYPE || code1 == REAL_TYPE || code1 == COMPLEX_TYPE))
3713 int none_complex = (code0 != COMPLEX_TYPE && code1 != COMPLEX_TYPE);
3715 if (shorten || common || short_compare)
3716 result_type = common_type (type0, type1);
3718 /* For certain operations (which identify themselves by shorten != 0)
3719 if both args were extended from the same smaller type,
3720 do the arithmetic in that type and then extend.
3722 shorten !=0 and !=1 indicates a bitwise operation.
3723 For them, this optimization is safe only if
3724 both args are zero-extended or both are sign-extended.
3725 Otherwise, we might change the result.
3726 Eg, (short)-1 | (unsigned short)-1 is (int)-1
3727 but calculated in (unsigned short) it would be (unsigned short)-1. */
3729 if (shorten && none_complex)
3731 int unsigned0, unsigned1;
3732 tree arg0 = get_narrower (op0, &unsigned0);
3733 tree arg1 = get_narrower (op1, &unsigned1);
3734 /* UNS is 1 if the operation to be done is an unsigned one. */
3735 int uns = TREE_UNSIGNED (result_type);
3738 final_type = result_type;
3740 /* Handle the case that OP0 does not *contain* a conversion
3741 but it *requires* conversion to FINAL_TYPE. */
3743 if (op0 == arg0 && TREE_TYPE (op0) != final_type)
3744 unsigned0 = TREE_UNSIGNED (TREE_TYPE (op0));
3745 if (op1 == arg1 && TREE_TYPE (op1) != final_type)
3746 unsigned1 = TREE_UNSIGNED (TREE_TYPE (op1));
3748 /* Now UNSIGNED0 is 1 if ARG0 zero-extends to FINAL_TYPE. */
3750 /* For bitwise operations, signedness of nominal type
3751 does not matter. Consider only how operands were extended. */
3755 /* Note that in all three cases below we refrain from optimizing
3756 an unsigned operation on sign-extended args.
3757 That would not be valid. */
3759 /* Both args variable: if both extended in same way
3760 from same width, do it in that width.
3761 Do it unsigned if args were zero-extended. */
3762 if ((TYPE_PRECISION (TREE_TYPE (arg0))
3763 < TYPE_PRECISION (result_type))
3764 && (TYPE_PRECISION (TREE_TYPE (arg1))
3765 == TYPE_PRECISION (TREE_TYPE (arg0)))
3766 && unsigned0 == unsigned1
3767 && (unsigned0 || !uns))
3768 result_type = c_common_signed_or_unsigned_type
3769 (unsigned0, common_type (TREE_TYPE (arg0), TREE_TYPE (arg1)));
3770 else if (TREE_CODE (arg0) == INTEGER_CST
3771 && (unsigned1 || !uns)
3772 && (TYPE_PRECISION (TREE_TYPE (arg1))
3773 < TYPE_PRECISION (result_type))
3774 && (type = c_common_signed_or_unsigned_type
3775 (unsigned1, TREE_TYPE (arg1)),
3776 int_fits_type_p (arg0, type)))
3778 else if (TREE_CODE (arg1) == INTEGER_CST
3779 && (unsigned0 || !uns)
3780 && (TYPE_PRECISION (TREE_TYPE (arg0))
3781 < TYPE_PRECISION (result_type))
3782 && (type = c_common_signed_or_unsigned_type
3783 (unsigned0, TREE_TYPE (arg0)),
3784 int_fits_type_p (arg1, type)))
3788 /* Shifts can be shortened if shifting right. */
3793 tree arg0 = get_narrower (op0, &unsigned_arg);
3795 final_type = result_type;
3797 if (arg0 == op0 && final_type == TREE_TYPE (op0))
3798 unsigned_arg = TREE_UNSIGNED (TREE_TYPE (op0));
3800 if (TYPE_PRECISION (TREE_TYPE (arg0)) < TYPE_PRECISION (result_type)
3801 /* We can shorten only if the shift count is less than the
3802 number of bits in the smaller type size. */
3803 && compare_tree_int (op1, TYPE_PRECISION (TREE_TYPE (arg0))) < 0
3804 /* If arg is sign-extended and then unsigned-shifted,
3805 we can simulate this with a signed shift in arg's type
3806 only if the extended result is at least twice as wide
3807 as the arg. Otherwise, the shift could use up all the
3808 ones made by sign-extension and bring in zeros.
3809 We can't optimize that case at all, but in most machines
3810 it never happens because available widths are 2**N. */
3811 && (!TREE_UNSIGNED (final_type)
3813 || (((unsigned) 2 * TYPE_PRECISION (TREE_TYPE (arg0)))
3814 <= TYPE_PRECISION (result_type))))
3816 /* Do an unsigned shift if the operand was zero-extended. */
3818 = c_common_signed_or_unsigned_type (unsigned_arg,
3820 /* Convert value-to-be-shifted to that type. */
3821 if (TREE_TYPE (op0) != result_type)
3822 op0 = cp_convert (result_type, op0);
3827 /* Comparison operations are shortened too but differently.
3828 They identify themselves by setting short_compare = 1. */
3832 /* Don't write &op0, etc., because that would prevent op0
3833 from being kept in a register.
3834 Instead, make copies of the our local variables and
3835 pass the copies by reference, then copy them back afterward. */
3836 tree xop0 = op0, xop1 = op1, xresult_type = result_type;
3837 enum tree_code xresultcode = resultcode;
3839 = shorten_compare (&xop0, &xop1, &xresult_type, &xresultcode);
3841 return cp_convert (boolean_type_node, val);
3842 op0 = xop0, op1 = xop1;
3844 resultcode = xresultcode;
3847 if ((short_compare || code == MIN_EXPR || code == MAX_EXPR)
3848 && warn_sign_compare)
3850 int op0_signed = ! TREE_UNSIGNED (TREE_TYPE (orig_op0));
3851 int op1_signed = ! TREE_UNSIGNED (TREE_TYPE (orig_op1));
3853 int unsignedp0, unsignedp1;
3854 tree primop0 = get_narrower (op0, &unsignedp0);
3855 tree primop1 = get_narrower (op1, &unsignedp1);
3857 /* Check for comparison of different enum types. */
3858 if (TREE_CODE (TREE_TYPE (orig_op0)) == ENUMERAL_TYPE
3859 && TREE_CODE (TREE_TYPE (orig_op1)) == ENUMERAL_TYPE
3860 && TYPE_MAIN_VARIANT (TREE_TYPE (orig_op0))
3861 != TYPE_MAIN_VARIANT (TREE_TYPE (orig_op1)))
3863 warning ("comparison between types `%#T' and `%#T'",
3864 TREE_TYPE (orig_op0), TREE_TYPE (orig_op1));
3867 /* Give warnings for comparisons between signed and unsigned
3868 quantities that may fail. */
3869 /* Do the checking based on the original operand trees, so that
3870 casts will be considered, but default promotions won't be. */
3872 /* Do not warn if the comparison is being done in a signed type,
3873 since the signed type will only be chosen if it can represent
3874 all the values of the unsigned type. */
3875 if (! TREE_UNSIGNED (result_type))
3877 /* Do not warn if both operands are unsigned. */
3878 else if (op0_signed == op1_signed)
3880 /* Do not warn if the signed quantity is an unsuffixed
3881 integer literal (or some static constant expression
3882 involving such literals or a conditional expression
3883 involving such literals) and it is non-negative. */
3884 else if ((op0_signed && tree_expr_nonnegative_p (orig_op0))
3885 || (op1_signed && tree_expr_nonnegative_p (orig_op1)))
3887 /* Do not warn if the comparison is an equality operation,
3888 the unsigned quantity is an integral constant and it does
3889 not use the most significant bit of result_type. */
3890 else if ((resultcode == EQ_EXPR || resultcode == NE_EXPR)
3891 && ((op0_signed && TREE_CODE (orig_op1) == INTEGER_CST
3892 && int_fits_type_p (orig_op1, c_common_signed_type
3894 || (op1_signed && TREE_CODE (orig_op0) == INTEGER_CST
3895 && int_fits_type_p (orig_op0, c_common_signed_type
3899 warning ("comparison between signed and unsigned integer expressions");
3901 /* Warn if two unsigned values are being compared in a size
3902 larger than their original size, and one (and only one) is the
3903 result of a `~' operator. This comparison will always fail.
3905 Also warn if one operand is a constant, and the constant does not
3906 have all bits set that are set in the ~ operand when it is
3909 if ((TREE_CODE (primop0) == BIT_NOT_EXPR)
3910 ^ (TREE_CODE (primop1) == BIT_NOT_EXPR))
3912 if (TREE_CODE (primop0) == BIT_NOT_EXPR)
3913 primop0 = get_narrower (TREE_OPERAND (op0, 0), &unsignedp0);
3914 if (TREE_CODE (primop1) == BIT_NOT_EXPR)
3915 primop1 = get_narrower (TREE_OPERAND (op1, 0), &unsignedp1);
3917 if (host_integerp (primop0, 0) || host_integerp (primop1, 0))
3920 HOST_WIDE_INT constant, mask;
3924 if (host_integerp (primop0, 0))
3927 unsignedp = unsignedp1;
3928 constant = tree_low_cst (primop0, 0);
3933 unsignedp = unsignedp0;
3934 constant = tree_low_cst (primop1, 0);
3937 bits = TYPE_PRECISION (TREE_TYPE (primop));
3938 if (bits < TYPE_PRECISION (result_type)
3939 && bits < HOST_BITS_PER_LONG && unsignedp)
3941 mask = (~ (HOST_WIDE_INT) 0) << bits;
3942 if ((mask & constant) != mask)
3943 warning ("comparison of promoted ~unsigned with constant");
3946 else if (unsignedp0 && unsignedp1
3947 && (TYPE_PRECISION (TREE_TYPE (primop0))
3948 < TYPE_PRECISION (result_type))
3949 && (TYPE_PRECISION (TREE_TYPE (primop1))
3950 < TYPE_PRECISION (result_type)))
3951 warning ("comparison of promoted ~unsigned with unsigned");
3956 /* At this point, RESULT_TYPE must be nonzero to avoid an error message.
3957 If CONVERTED is zero, both args will be converted to type RESULT_TYPE.
3958 Then the expression will be built.
3959 It will be given type FINAL_TYPE if that is nonzero;
3960 otherwise, it will be given type RESULT_TYPE. */
3964 error ("invalid operands of types `%T' and `%T' to binary `%O'",
3965 TREE_TYPE (orig_op0), TREE_TYPE (orig_op1), code);
3966 return error_mark_node;
3969 /* Issue warnings about peculiar, but legal, uses of NULL. */
3970 if (/* It's reasonable to use pointer values as operands of &&
3971 and ||, so NULL is no exception. */
3972 !(code == TRUTH_ANDIF_EXPR || code == TRUTH_ORIF_EXPR)
3973 && (/* If OP0 is NULL and OP1 is not a pointer, or vice versa. */
3974 (orig_op0 == null_node
3975 && TREE_CODE (TREE_TYPE (op1)) != POINTER_TYPE)
3976 /* Or vice versa. */
3977 || (orig_op1 == null_node
3978 && TREE_CODE (TREE_TYPE (op0)) != POINTER_TYPE)
3979 /* Or, both are NULL and the operation was not a comparison. */
3980 || (orig_op0 == null_node && orig_op1 == null_node
3981 && code != EQ_EXPR && code != NE_EXPR)))
3982 /* Some sort of arithmetic operation involving NULL was
3983 performed. Note that pointer-difference and pointer-addition
3984 have already been handled above, and so we don't end up here in
3986 warning ("NULL used in arithmetic");
3990 if (TREE_TYPE (op0) != result_type)
3991 op0 = cp_convert (result_type, op0);
3992 if (TREE_TYPE (op1) != result_type)
3993 op1 = cp_convert (result_type, op1);
3995 if (op0 == error_mark_node || op1 == error_mark_node)
3996 return error_mark_node;
3999 if (build_type == NULL_TREE)
4000 build_type = result_type;
4003 register tree result = build (resultcode, build_type, op0, op1);
4004 register tree folded;
4006 folded = fold (result);
4007 if (folded == result)
4008 TREE_CONSTANT (folded) = TREE_CONSTANT (op0) & TREE_CONSTANT (op1);
4009 if (final_type != 0)
4010 return cp_convert (final_type, folded);
4015 /* Return a tree for the sum or difference (RESULTCODE says which)
4016 of pointer PTROP and integer INTOP. */
4019 cp_pointer_int_sum (resultcode, ptrop, intop)
4020 enum tree_code resultcode;
4021 register tree ptrop, intop;
4023 tree res_type = TREE_TYPE (ptrop);
4025 /* pointer_int_sum() uses size_in_bytes() on the TREE_TYPE(res_type)
4026 in certain circumstance (when it's valid to do so). So we need
4027 to make sure it's complete. We don't need to check here, if we
4028 can actually complete it at all, as those checks will be done in
4029 pointer_int_sum() anyway. */
4030 complete_type (TREE_TYPE (res_type));
4032 return pointer_int_sum (resultcode, ptrop, fold (intop));
4035 /* Return a tree for the difference of pointers OP0 and OP1.
4036 The resulting tree has type int. */
4039 pointer_diff (op0, op1, ptrtype)
4040 register tree op0, op1;
4041 register tree ptrtype;
4043 register tree result, folded;
4044 tree restype = ptrdiff_type_node;
4045 tree target_type = TREE_TYPE (ptrtype);
4047 if (!complete_type_or_else (target_type, NULL_TREE))
4048 return error_mark_node;
4050 if (pedantic || warn_pointer_arith)
4052 if (TREE_CODE (target_type) == VOID_TYPE)
4053 pedwarn ("ISO C++ forbids using pointer of type `void *' in subtraction");
4054 if (TREE_CODE (target_type) == FUNCTION_TYPE)
4055 pedwarn ("ISO C++ forbids using pointer to a function in subtraction");
4056 if (TREE_CODE (target_type) == METHOD_TYPE)
4057 pedwarn ("ISO C++ forbids using pointer to a method in subtraction");
4058 if (TREE_CODE (target_type) == OFFSET_TYPE)
4059 pedwarn ("ISO C++ forbids using pointer to a member in subtraction");
4062 /* First do the subtraction as integers;
4063 then drop through to build the divide operator. */
4065 op0 = cp_build_binary_op (MINUS_EXPR,
4066 cp_convert (restype, op0),
4067 cp_convert (restype, op1));
4069 /* This generates an error if op1 is a pointer to an incomplete type. */
4070 if (!COMPLETE_TYPE_P (TREE_TYPE (TREE_TYPE (op1))))
4071 error ("invalid use of a pointer to an incomplete type in pointer arithmetic");
4073 op1 = ((TREE_CODE (target_type) == VOID_TYPE
4074 || TREE_CODE (target_type) == FUNCTION_TYPE
4075 || TREE_CODE (target_type) == METHOD_TYPE
4076 || TREE_CODE (target_type) == OFFSET_TYPE)
4078 : size_in_bytes (target_type));
4080 /* Do the division. */
4082 result = build (EXACT_DIV_EXPR, restype, op0, cp_convert (restype, op1));
4084 folded = fold (result);
4085 if (folded == result)
4086 TREE_CONSTANT (folded) = TREE_CONSTANT (op0) & TREE_CONSTANT (op1);
4090 /* Handle the case of taking the address of a COMPONENT_REF.
4091 Called by `build_unary_op'.
4093 ARG is the COMPONENT_REF whose address we want.
4094 ARGTYPE is the pointer type that this address should have. */
4097 build_component_addr (arg, argtype)
4100 tree field = TREE_OPERAND (arg, 1);
4101 tree basetype = decl_type_context (field);
4102 tree rval = build_unary_op (ADDR_EXPR, TREE_OPERAND (arg, 0), 0);
4104 my_friendly_assert (TREE_CODE (field) == FIELD_DECL, 981018);
4106 if (DECL_C_BIT_FIELD (field))
4108 error ("attempt to take address of bit-field structure member `%D'",
4110 return error_mark_node;
4113 if (TREE_CODE (field) == FIELD_DECL
4114 && TYPE_BASE_CONVS_MAY_REQUIRE_CODE_P (basetype))
4116 /* Can't convert directly to ARGTYPE, since that
4117 may have the same pointer type as one of our
4119 tree binfo = lookup_base (TREE_TYPE (TREE_TYPE (rval)), basetype,
4122 rval = build_base_path (PLUS_EXPR, rval, binfo, 1);
4123 rval = build1 (NOP_EXPR, argtype, rval);
4124 TREE_CONSTANT (rval) = TREE_CONSTANT (TREE_OPERAND (rval, 0));
4127 /* This conversion is harmless. */
4128 rval = convert_force (argtype, rval, 0);
4130 return fold (build (PLUS_EXPR, argtype, rval,
4131 cp_convert (argtype, byte_position (field))));
4134 /* Construct and perhaps optimize a tree representation
4135 for a unary operation. CODE, a tree_code, specifies the operation
4136 and XARG is the operand. */
4139 build_x_unary_op (code, xarg)
4140 enum tree_code code;
4146 if (processing_template_decl)
4147 return build_min_nt (code, xarg, NULL_TREE);
4149 /* & rec, on incomplete RECORD_TYPEs is the simple opr &, not an
4151 if (code == ADDR_EXPR
4152 && TREE_CODE (xarg) != TEMPLATE_ID_EXPR
4153 && ((IS_AGGR_TYPE_CODE (TREE_CODE (TREE_TYPE (xarg)))
4154 && !COMPLETE_TYPE_P (TREE_TYPE (xarg)))
4155 || (TREE_CODE (xarg) == OFFSET_REF)))
4156 /* don't look for a function */;
4161 rval = build_new_op (code, LOOKUP_NORMAL, xarg,
4162 NULL_TREE, NULL_TREE);
4163 if (rval || code != ADDR_EXPR)
4166 if (code == ADDR_EXPR)
4168 if (TREE_CODE (xarg) == OFFSET_REF)
4170 ptrmem = PTRMEM_OK_P (xarg);
4172 if (!ptrmem && !flag_ms_extensions
4173 && TREE_CODE (TREE_TYPE (TREE_OPERAND (xarg, 1))) == METHOD_TYPE)
4175 /* A single non-static member, make sure we don't allow a
4176 pointer-to-member. */
4177 xarg = build (OFFSET_REF, TREE_TYPE (xarg),
4178 TREE_OPERAND (xarg, 0),
4179 ovl_cons (TREE_OPERAND (xarg, 1), NULL_TREE));
4180 PTRMEM_OK_P (xarg) = ptrmem;
4184 else if (TREE_CODE (xarg) == TARGET_EXPR)
4185 warning ("taking address of temporary");
4187 exp = build_unary_op (code, xarg, 0);
4188 if (TREE_CODE (exp) == ADDR_EXPR)
4189 PTRMEM_OK_P (exp) = ptrmem;
4194 /* Like c_common_truthvalue_conversion, but handle pointer-to-member
4195 constants, where a null value is represented by an INTEGER_CST of
4199 cp_truthvalue_conversion (expr)
4202 tree type = TREE_TYPE (expr);
4203 if (TYPE_PTRMEM_P (type))
4204 return build_binary_op (NE_EXPR, expr, integer_zero_node, 1);
4206 return c_common_truthvalue_conversion (expr);
4209 /* Just like cp_truthvalue_conversion, but we want a CLEANUP_POINT_EXPR. */
4212 condition_conversion (expr)
4216 if (processing_template_decl)
4218 if (TREE_CODE (expr) == OFFSET_REF)
4219 expr = resolve_offset_ref (expr);
4220 t = perform_implicit_conversion (boolean_type_node, expr);
4221 t = fold (build1 (CLEANUP_POINT_EXPR, boolean_type_node, t));
4225 /* C++: Must handle pointers to members.
4227 Perhaps type instantiation should be extended to handle conversion
4228 from aggregates to types we don't yet know we want? (Or are those
4229 cases typically errors which should be reported?)
4231 NOCONVERT nonzero suppresses the default promotions
4232 (such as from short to int). */
4235 build_unary_op (code, xarg, noconvert)
4236 enum tree_code code;
4240 /* No default_conversion here. It causes trouble for ADDR_EXPR. */
4241 register tree arg = xarg;
4242 register tree argtype = 0;
4243 const char *errstring = NULL;
4246 if (arg == error_mark_node)
4247 return error_mark_node;
4252 /* This is used for unary plus, because a CONVERT_EXPR
4253 is enough to prevent anybody from looking inside for
4254 associativity, but won't generate any code. */
4255 if (!(arg = build_expr_type_conversion
4256 (WANT_ARITH | WANT_ENUM | WANT_POINTER, arg, 1)))
4257 errstring = "wrong type argument to unary plus";
4261 arg = default_conversion (arg);
4262 arg = build1 (NON_LVALUE_EXPR, TREE_TYPE (arg), arg);
4263 TREE_CONSTANT (arg) = TREE_CONSTANT (TREE_OPERAND (arg, 0));
4268 if (!(arg = build_expr_type_conversion (WANT_ARITH | WANT_ENUM, arg, 1)))
4269 errstring = "wrong type argument to unary minus";
4270 else if (!noconvert)
4271 arg = default_conversion (arg);
4275 if (TREE_CODE (TREE_TYPE (arg)) == COMPLEX_TYPE)
4279 arg = default_conversion (arg);
4281 else if (!(arg = build_expr_type_conversion (WANT_INT | WANT_ENUM,
4283 errstring = "wrong type argument to bit-complement";
4284 else if (!noconvert)
4285 arg = default_conversion (arg);
4289 if (!(arg = build_expr_type_conversion (WANT_ARITH | WANT_ENUM, arg, 1)))
4290 errstring = "wrong type argument to abs";
4291 else if (!noconvert)
4292 arg = default_conversion (arg);
4296 /* Conjugating a real value is a no-op, but allow it anyway. */
4297 if (!(arg = build_expr_type_conversion (WANT_ARITH | WANT_ENUM, arg, 1)))
4298 errstring = "wrong type argument to conjugation";
4299 else if (!noconvert)
4300 arg = default_conversion (arg);
4303 case TRUTH_NOT_EXPR:
4304 arg = cp_convert (boolean_type_node, arg);
4305 val = invert_truthvalue (arg);
4306 if (arg != error_mark_node)
4308 errstring = "in argument to unary !";
4315 if (TREE_CODE (arg) == COMPLEX_CST)
4316 return TREE_REALPART (arg);
4317 else if (TREE_CODE (TREE_TYPE (arg)) == COMPLEX_TYPE)
4318 return fold (build1 (REALPART_EXPR, TREE_TYPE (TREE_TYPE (arg)), arg));
4323 if (TREE_CODE (arg) == COMPLEX_CST)
4324 return TREE_IMAGPART (arg);
4325 else if (TREE_CODE (TREE_TYPE (arg)) == COMPLEX_TYPE)
4326 return fold (build1 (IMAGPART_EXPR, TREE_TYPE (TREE_TYPE (arg)), arg));
4328 return cp_convert (TREE_TYPE (arg), integer_zero_node);
4330 case PREINCREMENT_EXPR:
4331 case POSTINCREMENT_EXPR:
4332 case PREDECREMENT_EXPR:
4333 case POSTDECREMENT_EXPR:
4334 /* Handle complex lvalues (when permitted)
4335 by reduction to simpler cases. */
4337 val = unary_complex_lvalue (code, arg);
4341 /* Increment or decrement the real part of the value,
4342 and don't change the imaginary part. */
4343 if (TREE_CODE (TREE_TYPE (arg)) == COMPLEX_TYPE)
4347 arg = stabilize_reference (arg);
4348 real = build_unary_op (REALPART_EXPR, arg, 1);
4349 imag = build_unary_op (IMAGPART_EXPR, arg, 1);
4350 return build (COMPLEX_EXPR, TREE_TYPE (arg),
4351 build_unary_op (code, real, 1), imag);
4354 /* Report invalid types. */
4356 if (!(arg = build_expr_type_conversion (WANT_ARITH | WANT_POINTER,
4359 if (code == PREINCREMENT_EXPR)
4360 errstring ="no pre-increment operator for type";
4361 else if (code == POSTINCREMENT_EXPR)
4362 errstring ="no post-increment operator for type";
4363 else if (code == PREDECREMENT_EXPR)
4364 errstring ="no pre-decrement operator for type";
4366 errstring ="no post-decrement operator for type";
4370 /* Report something read-only. */
4372 if (CP_TYPE_CONST_P (TREE_TYPE (arg))
4373 || TREE_READONLY (arg))
4374 readonly_error (arg, ((code == PREINCREMENT_EXPR
4375 || code == POSTINCREMENT_EXPR)
4376 ? "increment" : "decrement"),
4381 tree result_type = TREE_TYPE (arg);
4383 arg = get_unwidened (arg, 0);
4384 argtype = TREE_TYPE (arg);
4386 /* ARM $5.2.5 last annotation says this should be forbidden. */
4387 if (TREE_CODE (argtype) == ENUMERAL_TYPE)
4388 pedwarn ("ISO C++ forbids %sing an enum",
4389 (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
4390 ? "increment" : "decrement");
4392 /* Compute the increment. */
4394 if (TREE_CODE (argtype) == POINTER_TYPE)
4396 enum tree_code tmp = TREE_CODE (TREE_TYPE (argtype));
4397 tree type = complete_type (TREE_TYPE (argtype));
4399 if (!COMPLETE_OR_VOID_TYPE_P (type))
4400 error ("cannot %s a pointer to incomplete type `%T'",
4401 ((code == PREINCREMENT_EXPR
4402 || code == POSTINCREMENT_EXPR)
4403 ? "increment" : "decrement"), TREE_TYPE (argtype));
4404 else if ((pedantic || warn_pointer_arith)
4405 && (tmp == FUNCTION_TYPE || tmp == METHOD_TYPE
4406 || tmp == VOID_TYPE || tmp == OFFSET_TYPE))
4407 pedwarn ("ISO C++ forbids %sing a pointer of type `%T'",
4408 ((code == PREINCREMENT_EXPR
4409 || code == POSTINCREMENT_EXPR)
4410 ? "increment" : "decrement"), argtype);
4411 inc = c_sizeof_nowarn (TREE_TYPE (argtype));
4414 inc = integer_one_node;
4416 inc = cp_convert (argtype, inc);
4418 /* Handle incrementing a cast-expression. */
4420 switch (TREE_CODE (arg))
4425 case FIX_TRUNC_EXPR:
4426 case FIX_FLOOR_EXPR:
4427 case FIX_ROUND_EXPR:
4430 tree incremented, modify, value, compound;
4431 if (! lvalue_p (arg) && pedantic)
4432 pedwarn ("cast to non-reference type used as lvalue");
4433 arg = stabilize_reference (arg);
4434 if (code == PREINCREMENT_EXPR || code == PREDECREMENT_EXPR)
4437 value = save_expr (arg);
4438 incremented = build (((code == PREINCREMENT_EXPR
4439 || code == POSTINCREMENT_EXPR)
4440 ? PLUS_EXPR : MINUS_EXPR),
4441 argtype, value, inc);
4443 modify = build_modify_expr (arg, NOP_EXPR, incremented);
4444 compound = build (COMPOUND_EXPR, TREE_TYPE (arg), modify, value);
4446 /* Eliminate warning about unused result of + or -. */
4447 TREE_NO_UNUSED_WARNING (compound) = 1;
4455 /* Complain about anything else that is not a true lvalue. */
4456 if (!lvalue_or_else (arg, ((code == PREINCREMENT_EXPR
4457 || code == POSTINCREMENT_EXPR)
4458 ? "increment" : "decrement")))
4459 return error_mark_node;
4461 /* Forbid using -- on `bool'. */
4462 if (TREE_TYPE (arg) == boolean_type_node)
4464 if (code == POSTDECREMENT_EXPR || code == PREDECREMENT_EXPR)
4466 error ("invalid use of `--' on bool variable `%D'", arg);
4467 return error_mark_node;
4470 /* This will only work if someone can convince Kenner to accept
4471 my patch to expand_increment. (jason) */
4472 val = build (code, TREE_TYPE (arg), arg, inc);
4474 val = boolean_increment (code, arg);
4478 val = build (code, TREE_TYPE (arg), arg, inc);
4480 TREE_SIDE_EFFECTS (val) = 1;
4481 return cp_convert (result_type, val);
4485 /* Note that this operation never does default_conversion
4486 regardless of NOCONVERT. */
4488 argtype = lvalue_type (arg);
4489 if (TREE_CODE (argtype) == REFERENCE_TYPE)
4493 build_pointer_type (TREE_TYPE (argtype)), arg);
4494 TREE_CONSTANT (arg) = TREE_CONSTANT (TREE_OPERAND (arg, 0));
4497 else if (pedantic && DECL_MAIN_P (arg))
4499 pedwarn ("ISO C++ forbids taking address of function `::main'");
4501 /* Let &* cancel out to simplify resulting code. */
4502 if (TREE_CODE (arg) == INDIRECT_REF)
4504 /* We don't need to have `current_class_ptr' wrapped in a
4505 NON_LVALUE_EXPR node. */
4506 if (arg == current_class_ref)
4507 return current_class_ptr;
4509 arg = TREE_OPERAND (arg, 0);
4510 if (TREE_CODE (TREE_TYPE (arg)) == REFERENCE_TYPE)
4514 build_pointer_type (TREE_TYPE (TREE_TYPE (arg))), arg);
4515 TREE_CONSTANT (arg) = TREE_CONSTANT (TREE_OPERAND (arg, 0));
4517 else if (lvalue_p (arg))
4518 /* Don't let this be an lvalue. */
4519 return non_lvalue (arg);
4523 /* For &x[y], return x+y */
4524 if (TREE_CODE (arg) == ARRAY_REF)
4526 if (!cxx_mark_addressable (TREE_OPERAND (arg, 0)))
4527 return error_mark_node;
4528 return cp_build_binary_op (PLUS_EXPR, TREE_OPERAND (arg, 0),
4529 TREE_OPERAND (arg, 1));
4532 /* Uninstantiated types are all functions. Taking the
4533 address of a function is a no-op, so just return the
4536 if (TREE_CODE (arg) == IDENTIFIER_NODE
4537 && IDENTIFIER_OPNAME_P (arg))
4540 /* We don't know the type yet, so just work around the problem.
4541 We know that this will resolve to an lvalue. */
4542 return build1 (ADDR_EXPR, unknown_type_node, arg);
4545 if (TREE_CODE (arg) == COMPONENT_REF && type_unknown_p (arg)
4546 && OVL_NEXT (TREE_OPERAND (arg, 1)) == NULL_TREE)
4548 /* They're trying to take the address of a unique non-static
4549 member function. This is ill-formed (except in MS-land),
4550 but let's try to DTRT.
4551 Note: We only handle unique functions here because we don't
4552 want to complain if there's a static overload; non-unique
4553 cases will be handled by instantiate_type. But we need to
4554 handle this case here to allow casts on the resulting PMF.
4555 We could defer this in non-MS mode, but it's easier to give
4556 a useful error here. */
4558 tree base = TREE_TYPE (TREE_OPERAND (arg, 0));
4559 tree name = DECL_NAME (OVL_CURRENT (TREE_OPERAND (arg, 1)));
4561 if (! flag_ms_extensions)
4563 if (current_class_type
4564 && TREE_OPERAND (arg, 0) == current_class_ref)
4565 /* An expression like &memfn. */
4566 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);
4568 pedwarn ("ISO C++ forbids taking the address of a bound member function to form a pointer to member function. Say `&%T::%D'", base, name);
4570 arg = build_offset_ref (base, name);
4573 if (type_unknown_p (arg))
4574 return build1 (ADDR_EXPR, unknown_type_node, arg);
4576 /* Handle complex lvalues (when permitted)
4577 by reduction to simpler cases. */
4578 val = unary_complex_lvalue (code, arg);
4582 switch (TREE_CODE (arg))
4587 case FIX_TRUNC_EXPR:
4588 case FIX_FLOOR_EXPR:
4589 case FIX_ROUND_EXPR:
4591 if (! lvalue_p (arg) && pedantic)
4592 pedwarn ("ISO C++ forbids taking the address of a cast to a non-lvalue expression");
4599 /* Allow the address of a constructor if all the elements
4601 if (TREE_CODE (arg) == CONSTRUCTOR && TREE_HAS_CONSTRUCTOR (arg)
4602 && TREE_CONSTANT (arg))
4604 /* Anything not already handled and not a true memory reference
4606 else if (TREE_CODE (argtype) != FUNCTION_TYPE
4607 && TREE_CODE (argtype) != METHOD_TYPE
4608 && !lvalue_or_else (arg, "unary `&'"))
4609 return error_mark_node;
4611 if (argtype != error_mark_node)
4612 argtype = build_pointer_type (argtype);
4614 if (!cxx_mark_addressable (arg))
4615 return error_mark_node;
4620 if (TREE_CODE (arg) == COMPONENT_REF)
4621 addr = build_component_addr (arg, argtype);
4623 addr = build1 (ADDR_EXPR, argtype, arg);
4625 /* Address of a static or external variable or
4626 function counts as a constant */
4628 TREE_CONSTANT (addr) = 1;
4630 if (TREE_CODE (argtype) == POINTER_TYPE
4631 && TREE_CODE (TREE_TYPE (argtype)) == METHOD_TYPE)
4633 build_ptrmemfunc_type (argtype);
4634 addr = build_ptrmemfunc (argtype, addr, 0);
4647 argtype = TREE_TYPE (arg);
4648 return fold (build1 (code, argtype, arg));
4651 error ("%s", errstring);
4652 return error_mark_node;
4655 /* Apply unary lvalue-demanding operator CODE to the expression ARG
4656 for certain kinds of expressions which are not really lvalues
4657 but which we can accept as lvalues.
4659 If ARG is not a kind of expression we can handle, return zero. */
4662 unary_complex_lvalue (code, arg)
4663 enum tree_code code;
4666 /* Handle (a, b) used as an "lvalue". */
4667 if (TREE_CODE (arg) == COMPOUND_EXPR)
4669 tree real_result = build_unary_op (code, TREE_OPERAND (arg, 1), 0);
4670 return build (COMPOUND_EXPR, TREE_TYPE (real_result),
4671 TREE_OPERAND (arg, 0), real_result);
4674 /* Handle (a ? b : c) used as an "lvalue". */
4675 if (TREE_CODE (arg) == COND_EXPR
4676 || TREE_CODE (arg) == MIN_EXPR || TREE_CODE (arg) == MAX_EXPR)
4677 return rationalize_conditional_expr (code, arg);
4679 /* Handle (a = b), (++a), and (--a) used as an "lvalue". */
4680 if (TREE_CODE (arg) == MODIFY_EXPR
4681 || TREE_CODE (arg) == PREINCREMENT_EXPR
4682 || TREE_CODE (arg) == PREDECREMENT_EXPR)
4684 tree lvalue = TREE_OPERAND (arg, 0);
4685 if (TREE_SIDE_EFFECTS (lvalue))
4687 lvalue = stabilize_reference (lvalue);
4688 arg = build (TREE_CODE (arg), TREE_TYPE (arg),
4689 lvalue, TREE_OPERAND (arg, 1));
4691 return unary_complex_lvalue
4692 (code, build (COMPOUND_EXPR, TREE_TYPE (lvalue), arg, lvalue));
4695 if (code != ADDR_EXPR)
4698 /* Handle (a = b) used as an "lvalue" for `&'. */
4699 if (TREE_CODE (arg) == MODIFY_EXPR
4700 || TREE_CODE (arg) == INIT_EXPR)
4702 tree real_result = build_unary_op (code, TREE_OPERAND (arg, 0), 0);
4703 arg = build (COMPOUND_EXPR, TREE_TYPE (real_result), arg, real_result);
4704 TREE_NO_UNUSED_WARNING (arg) = 1;
4708 if (TREE_CODE (TREE_TYPE (arg)) == FUNCTION_TYPE
4709 || TREE_CODE (TREE_TYPE (arg)) == METHOD_TYPE
4710 || TREE_CODE (TREE_TYPE (arg)) == OFFSET_TYPE)
4712 /* The representation of something of type OFFSET_TYPE
4713 is really the representation of a pointer to it.
4714 Here give the representation its true type. */
4717 my_friendly_assert (TREE_CODE (arg) != SCOPE_REF, 313);
4719 if (TREE_CODE (arg) != OFFSET_REF)
4722 t = TREE_OPERAND (arg, 1);
4724 /* Check all this code for right semantics. */
4725 if (TREE_CODE (t) == FUNCTION_DECL)
4727 if (DECL_DESTRUCTOR_P (t))
4728 error ("taking address of destructor");
4729 return build_unary_op (ADDR_EXPR, t, 0);
4731 if (TREE_CODE (t) == VAR_DECL)
4732 return build_unary_op (ADDR_EXPR, t, 0);
4737 if (TREE_OPERAND (arg, 0)
4738 && ! is_dummy_object (TREE_OPERAND (arg, 0))
4739 && TREE_CODE (t) != FIELD_DECL)
4741 error ("taking address of bound pointer-to-member expression");
4742 return error_mark_node;
4744 if (!PTRMEM_OK_P (arg))
4746 /* This cannot form a pointer to method, so we must
4747 resolve the offset ref, and take the address of the
4748 result. For instance,
4750 arg = resolve_offset_ref (arg);
4752 return build_unary_op (code, arg, 0);
4755 if (TREE_CODE (TREE_TYPE (t)) == REFERENCE_TYPE)
4757 error ("cannot create pointer to reference member `%D'", t);
4758 return error_mark_node;
4761 type = build_offset_type (DECL_FIELD_CONTEXT (t), TREE_TYPE (t));
4762 type = build_pointer_type (type);
4764 t = make_ptrmem_cst (type, TREE_OPERAND (arg, 1));
4770 /* We permit compiler to make function calls returning
4771 objects of aggregate type look like lvalues. */
4775 if (TREE_CODE (targ) == SAVE_EXPR)
4776 targ = TREE_OPERAND (targ, 0);
4778 if (TREE_CODE (targ) == CALL_EXPR && IS_AGGR_TYPE (TREE_TYPE (targ)))
4780 if (TREE_CODE (arg) == SAVE_EXPR)
4783 targ = build_cplus_new (TREE_TYPE (arg), arg);
4784 return build1 (ADDR_EXPR, build_pointer_type (TREE_TYPE (arg)), targ);
4787 if (TREE_CODE (arg) == SAVE_EXPR && TREE_CODE (targ) == INDIRECT_REF)
4788 return build (SAVE_EXPR, build_pointer_type (TREE_TYPE (arg)),
4789 TREE_OPERAND (targ, 0), current_function_decl, NULL);
4792 /* Don't let anything else be handled specially. */
4796 /* Mark EXP saying that we need to be able to take the
4797 address of it; it should not be allocated in a register.
4798 Value is true if successful.
4800 C++: we do not allow `current_class_ptr' to be addressable. */
4803 cxx_mark_addressable (exp)
4806 register tree x = exp;
4809 switch (TREE_CODE (x))
4816 x = TREE_OPERAND (x, 0);
4820 if (x == current_class_ptr)
4822 error ("cannot take the address of `this', which is an rvalue expression");
4823 TREE_ADDRESSABLE (x) = 1; /* so compiler doesn't die later */
4829 /* Caller should not be trying to mark initialized
4830 constant fields addressable. */
4831 my_friendly_assert (DECL_LANG_SPECIFIC (x) == 0
4832 || DECL_IN_AGGR_P (x) == 0
4834 || DECL_EXTERNAL (x), 314);
4839 if (DECL_REGISTER (x) && !TREE_ADDRESSABLE (x)
4840 && !DECL_ARTIFICIAL (x) && extra_warnings)
4841 warning ("address requested for `%D', which is declared `register'",
4843 TREE_ADDRESSABLE (x) = 1;
4844 put_var_into_stack (x);
4848 TREE_ADDRESSABLE (x) = 1;
4849 TREE_ADDRESSABLE (DECL_ASSEMBLER_NAME (x)) = 1;
4853 TREE_ADDRESSABLE (x) = 1;
4857 TREE_ADDRESSABLE (x) = 1;
4858 cxx_mark_addressable (TREE_OPERAND (x, 0));
4866 /* Build and return a conditional expression IFEXP ? OP1 : OP2. */
4869 build_x_conditional_expr (ifexp, op1, op2)
4870 tree ifexp, op1, op2;
4872 if (processing_template_decl)
4873 return build_min_nt (COND_EXPR, ifexp, op1, op2);
4875 return build_conditional_expr (ifexp, op1, op2);
4878 /* Handle overloading of the ',' operator when needed. Otherwise,
4879 this function just builds an expression list. */
4882 build_x_compound_expr (list)
4885 tree rest = TREE_CHAIN (list);
4888 if (processing_template_decl)
4889 return build_min_nt (COMPOUND_EXPR, list, NULL_TREE);
4891 if (rest == NULL_TREE)
4892 return build_compound_expr (list);
4894 result = build_opfncall (COMPOUND_EXPR, LOOKUP_NORMAL,
4895 TREE_VALUE (list), TREE_VALUE (rest), NULL_TREE);
4897 return build_x_compound_expr (tree_cons (NULL_TREE, result,
4898 TREE_CHAIN (rest)));
4900 if (! TREE_SIDE_EFFECTS (TREE_VALUE (list)))
4902 /* FIXME: This test should be in the implicit cast to void of the LHS. */
4903 /* the left-hand operand of a comma expression is like an expression
4904 statement: we should warn if it doesn't have any side-effects,
4905 unless it was explicitly cast to (void). */
4906 if ((extra_warnings || warn_unused_value)
4907 && !(TREE_CODE (TREE_VALUE(list)) == CONVERT_EXPR
4908 && VOID_TYPE_P (TREE_TYPE (TREE_VALUE(list)))))
4909 warning("left-hand operand of comma expression has no effect");
4911 #if 0 /* this requires a gcc backend patch to export warn_if_unused_value */
4912 else if (warn_unused_value)
4913 warn_if_unused_value (TREE_VALUE(list));
4916 return build_compound_expr
4917 (tree_cons (NULL_TREE, TREE_VALUE (list),
4918 build_tree_list (NULL_TREE,
4919 build_x_compound_expr (rest))));
4922 /* Given a list of expressions, return a compound expression
4923 that performs them all and returns the value of the last of them. */
4926 build_compound_expr (list)
4932 TREE_VALUE (list) = decl_constant_value (TREE_VALUE (list));
4934 if (TREE_CHAIN (list) == 0)
4936 /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
4937 Strip such NOP_EXPRs, since LIST is used in non-lvalue context. */
4938 if (TREE_CODE (list) == NOP_EXPR
4939 && TREE_TYPE (list) == TREE_TYPE (TREE_OPERAND (list, 0)))
4940 list = TREE_OPERAND (list, 0);
4942 return TREE_VALUE (list);
4945 first = TREE_VALUE (list);
4946 first = convert_to_void (first, "left-hand operand of comma");
4947 if (first == error_mark_node)
4948 return error_mark_node;
4950 rest = build_compound_expr (TREE_CHAIN (list));
4951 if (rest == error_mark_node)
4952 return error_mark_node;
4954 /* When pedantic, a compound expression cannot be a constant expression. */
4955 if (! TREE_SIDE_EFFECTS (first) && ! pedantic)
4958 return build (COMPOUND_EXPR, TREE_TYPE (rest), first, rest);
4962 build_static_cast (type, expr)
4968 if (type == error_mark_node || expr == error_mark_node)
4969 return error_mark_node;
4971 if (TREE_CODE (expr) == OFFSET_REF)
4972 expr = resolve_offset_ref (expr);
4974 if (processing_template_decl)
4976 tree t = build_min (STATIC_CAST_EXPR, type, expr);
4980 /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
4981 Strip such NOP_EXPRs if VALUE is being used in non-lvalue context. */
4982 if (TREE_CODE (type) != REFERENCE_TYPE
4983 && TREE_CODE (expr) == NOP_EXPR
4984 && TREE_TYPE (expr) == TREE_TYPE (TREE_OPERAND (expr, 0)))
4985 expr = TREE_OPERAND (expr, 0);
4987 if (TREE_CODE (type) == VOID_TYPE)
4989 expr = convert_to_void (expr, /*implicit=*/NULL);
4993 if (TREE_CODE (type) == REFERENCE_TYPE)
4994 return (convert_from_reference
4995 (convert_to_reference (type, expr, CONV_STATIC|CONV_IMPLICIT,
4996 LOOKUP_COMPLAIN, NULL_TREE)));
4998 if (IS_AGGR_TYPE (type))
4999 return build_cplus_new (type, (build_method_call
5000 (NULL_TREE, complete_ctor_identifier,
5001 build_tree_list (NULL_TREE, expr),
5002 TYPE_BINFO (type), LOOKUP_NORMAL)));
5004 intype = TREE_TYPE (expr);
5006 /* FIXME handle casting to array type. */
5009 if (IS_AGGR_TYPE (intype)
5010 ? can_convert_arg (type, intype, expr)
5011 : can_convert_arg (strip_all_pointer_quals (type),
5012 strip_all_pointer_quals (intype), expr))
5013 /* This is a standard conversion. */
5015 else if (TYPE_PTROB_P (type) && TYPE_PTROB_P (intype))
5017 /* They're pointers to objects. They must be aggregates that
5018 are related non-virtually. */
5021 if (IS_AGGR_TYPE (TREE_TYPE (type)) && IS_AGGR_TYPE (TREE_TYPE (intype))
5022 && lookup_base (TREE_TYPE (type), TREE_TYPE (intype),
5023 ba_ignore | ba_quiet, &kind)
5024 && kind != bk_via_virtual)
5027 else if (TYPE_PTRMEM_P (type) && TYPE_PTRMEM_P (intype))
5029 /* They're pointers to members. The pointed to objects must be
5030 the same (ignoring CV qualifiers), and the containing classes
5031 must be related non-virtually. */
5035 (strip_all_pointer_quals (TREE_TYPE (TREE_TYPE (type))),
5036 strip_all_pointer_quals (TREE_TYPE (TREE_TYPE (intype))))
5037 && (lookup_base (TYPE_OFFSET_BASETYPE (TREE_TYPE (intype)),
5038 TYPE_OFFSET_BASETYPE (TREE_TYPE (type)),
5039 ba_ignore | ba_quiet, &kind))
5040 && kind != bk_via_virtual)
5043 else if (TREE_CODE (intype) != BOOLEAN_TYPE
5044 && TREE_CODE (type) != ARRAY_TYPE
5045 && TREE_CODE (type) != FUNCTION_TYPE
5046 && can_convert (intype, strip_all_pointer_quals (type)))
5048 else if (TREE_CODE (intype) == ENUMERAL_TYPE
5049 && TREE_CODE (type) == ENUMERAL_TYPE)
5050 /* DR 128: "A value of integral _or enumeration_ type can be explicitly
5051 converted to an enumeration type."
5052 The integral to enumeration will be accepted by the previous clause.
5053 We need to explicitly check for enumeration to enumeration. */
5056 /* [expr.static.cast]
5058 The static_cast operator shall not be used to cast away
5060 if (ok && casts_away_constness (intype, type))
5062 error ("static_cast from type `%T' to type `%T' casts away constness",
5064 return error_mark_node;
5068 return build_c_cast (type, expr);
5070 error ("invalid static_cast from type `%T' to type `%T'", intype, type);
5071 return error_mark_node;
5075 build_reinterpret_cast (type, expr)
5080 if (type == error_mark_node || expr == error_mark_node)
5081 return error_mark_node;
5083 if (TREE_CODE (expr) == OFFSET_REF)
5084 expr = resolve_offset_ref (expr);
5086 if (processing_template_decl)
5088 tree t = build_min (REINTERPRET_CAST_EXPR, type, expr);
5092 if (TREE_CODE (type) != REFERENCE_TYPE)
5094 expr = decay_conversion (expr);
5096 /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
5097 Strip such NOP_EXPRs if VALUE is being used in non-lvalue context. */
5098 if (TREE_CODE (expr) == NOP_EXPR
5099 && TREE_TYPE (expr) == TREE_TYPE (TREE_OPERAND (expr, 0)))
5100 expr = TREE_OPERAND (expr, 0);
5103 intype = TREE_TYPE (expr);
5105 if (TREE_CODE (type) == REFERENCE_TYPE)
5107 if (! real_lvalue_p (expr))
5109 error ("invalid reinterpret_cast of an rvalue expression of type `%T' to type `%T'", intype, type);
5110 return error_mark_node;
5112 expr = build_unary_op (ADDR_EXPR, expr, 0);
5113 if (expr != error_mark_node)
5114 expr = build_reinterpret_cast
5115 (build_pointer_type (TREE_TYPE (type)), expr);
5116 if (expr != error_mark_node)
5117 expr = build_indirect_ref (expr, 0);
5120 else if (same_type_ignoring_top_level_qualifiers_p (intype, type))
5121 return build_static_cast (type, expr);
5123 if (TYPE_PTR_P (type) && (TREE_CODE (intype) == INTEGER_TYPE
5124 || TREE_CODE (intype) == ENUMERAL_TYPE))
5126 else if (TREE_CODE (type) == INTEGER_TYPE && TYPE_PTR_P (intype))
5128 if (TYPE_PRECISION (type) < TYPE_PRECISION (intype))
5129 pedwarn ("reinterpret_cast from `%T' to `%T' loses precision",
5132 else if ((TYPE_PTRFN_P (type) && TYPE_PTRFN_P (intype))
5133 || (TYPE_PTRMEMFUNC_P (type) && TYPE_PTRMEMFUNC_P (intype)))
5135 expr = decl_constant_value (expr);
5136 return fold (build1 (NOP_EXPR, type, expr));
5138 else if ((TYPE_PTRMEM_P (type) && TYPE_PTRMEM_P (intype))
5139 || (TYPE_PTROBV_P (type) && TYPE_PTROBV_P (intype)))
5141 if (! comp_ptr_ttypes_reinterpret (TREE_TYPE (type), TREE_TYPE (intype)))
5142 pedwarn ("reinterpret_cast from `%T' to `%T' casts away const (or volatile)",
5145 expr = decl_constant_value (expr);
5146 return fold (build1 (NOP_EXPR, type, expr));
5148 else if ((TYPE_PTRFN_P (type) && TYPE_PTROBV_P (intype))
5149 || (TYPE_PTRFN_P (intype) && TYPE_PTROBV_P (type)))
5151 pedwarn ("ISO C++ forbids casting between pointer-to-function and pointer-to-object");
5152 expr = decl_constant_value (expr);
5153 return fold (build1 (NOP_EXPR, type, expr));
5157 error ("invalid reinterpret_cast from type `%T' to type `%T'",
5159 return error_mark_node;
5162 return cp_convert (type, expr);
5166 build_const_cast (type, expr)
5171 if (type == error_mark_node || expr == error_mark_node)
5172 return error_mark_node;
5174 if (TREE_CODE (expr) == OFFSET_REF)
5175 expr = resolve_offset_ref (expr);
5177 if (processing_template_decl)
5179 tree t = build_min (CONST_CAST_EXPR, type, expr);
5183 if (!POINTER_TYPE_P (type))
5184 error ("invalid use of const_cast with type `%T', which is not a pointer, reference, nor a pointer-to-data-member type", type);
5185 else if (TREE_CODE (TREE_TYPE (type)) == FUNCTION_TYPE)
5187 error ("invalid use of const_cast with type `%T', which is a pointer or reference to a function type", type);
5188 return error_mark_node;
5191 if (TREE_CODE (type) != REFERENCE_TYPE)
5193 expr = decay_conversion (expr);
5195 /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
5196 Strip such NOP_EXPRs if VALUE is being used in non-lvalue context. */
5197 if (TREE_CODE (expr) == NOP_EXPR
5198 && TREE_TYPE (expr) == TREE_TYPE (TREE_OPERAND (expr, 0)))
5199 expr = TREE_OPERAND (expr, 0);
5202 intype = TREE_TYPE (expr);
5204 if (same_type_ignoring_top_level_qualifiers_p (intype, type))
5205 return build_static_cast (type, expr);
5206 else if (TREE_CODE (type) == REFERENCE_TYPE)
5208 if (! real_lvalue_p (expr))
5210 error ("invalid const_cast of an rvalue of type `%T' to type `%T'", intype, type);
5211 return error_mark_node;
5214 if (comp_ptr_ttypes_const (TREE_TYPE (type), intype))
5216 expr = build_unary_op (ADDR_EXPR, expr, 0);
5217 expr = build1 (NOP_EXPR, type, expr);
5218 return convert_from_reference (expr);
5221 else if (TREE_CODE (type) == POINTER_TYPE
5222 && TREE_CODE (intype) == POINTER_TYPE
5223 && comp_ptr_ttypes_const (TREE_TYPE (type), TREE_TYPE (intype)))
5224 return cp_convert (type, expr);
5226 error ("invalid const_cast from type `%T' to type `%T'", intype, type);
5227 return error_mark_node;
5230 /* Build an expression representing a cast to type TYPE of expression EXPR.
5232 ALLOW_NONCONVERTING is true if we should allow non-converting constructors
5233 when doing the cast. */
5236 build_c_cast (type, expr)
5239 register tree value = expr;
5242 if (type == error_mark_node || expr == error_mark_node)
5243 return error_mark_node;
5245 if (processing_template_decl)
5247 tree t = build_min (CAST_EXPR, type,
5248 tree_cons (NULL_TREE, value, NULL_TREE));
5252 /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
5253 Strip such NOP_EXPRs if VALUE is being used in non-lvalue context. */
5254 if (TREE_CODE (type) != REFERENCE_TYPE
5255 && TREE_CODE (value) == NOP_EXPR
5256 && TREE_TYPE (value) == TREE_TYPE (TREE_OPERAND (value, 0)))
5257 value = TREE_OPERAND (value, 0);
5259 if (TREE_CODE (value) == OFFSET_REF)
5260 value = resolve_offset_ref (value);
5262 if (TREE_CODE (type) == ARRAY_TYPE)
5264 /* Allow casting from T1* to T2[] because Cfront allows it.
5265 NIHCL uses it. It is not valid ISO C++ however. */
5266 if (TREE_CODE (TREE_TYPE (expr)) == POINTER_TYPE)
5268 pedwarn ("ISO C++ forbids casting to an array type `%T'", type);
5269 type = build_pointer_type (TREE_TYPE (type));
5273 error ("ISO C++ forbids casting to an array type `%T'", type);
5274 return error_mark_node;
5278 if (TREE_CODE (type) == FUNCTION_TYPE
5279 || TREE_CODE (type) == METHOD_TYPE)
5281 error ("invalid cast to function type `%T'", type);
5282 return error_mark_node;
5285 if (TREE_CODE (type) == VOID_TYPE)
5287 /* Conversion to void does not cause any of the normal function to
5288 * pointer, array to pointer and lvalue to rvalue decays. */
5290 value = convert_to_void (value, /*implicit=*/NULL);
5293 /* Convert functions and arrays to pointers and
5294 convert references to their expanded types,
5295 but don't convert any other types. If, however, we are
5296 casting to a class type, there's no reason to do this: the
5297 cast will only succeed if there is a converting constructor,
5298 and the default conversions will be done at that point. In
5299 fact, doing the default conversion here is actually harmful
5303 struct S { S(const A&); };
5305 since we don't want the array-to-pointer conversion done. */
5306 if (!IS_AGGR_TYPE (type))
5308 if (TREE_CODE (TREE_TYPE (value)) == FUNCTION_TYPE
5309 || (TREE_CODE (TREE_TYPE (value)) == METHOD_TYPE
5310 /* Don't do the default conversion on a ->* expression. */
5311 && ! (TREE_CODE (type) == POINTER_TYPE
5312 && bound_pmf_p (value)))
5313 || TREE_CODE (TREE_TYPE (value)) == ARRAY_TYPE
5314 || TREE_CODE (TREE_TYPE (value)) == REFERENCE_TYPE)
5315 value = default_conversion (value);
5317 else if (TREE_CODE (TREE_TYPE (value)) == REFERENCE_TYPE)
5318 /* However, even for class types, we still need to strip away
5319 the reference type, since the call to convert_force below
5320 does not expect the input expression to be of reference
5322 value = convert_from_reference (value);
5324 otype = TREE_TYPE (value);
5326 /* Optionally warn about potentially worrisome casts. */
5329 && TREE_CODE (type) == POINTER_TYPE
5330 && TREE_CODE (otype) == POINTER_TYPE
5331 && !at_least_as_qualified_p (TREE_TYPE (type),
5333 warning ("cast from `%T' to `%T' discards qualifiers from pointer target type",
5336 if (TREE_CODE (type) == INTEGER_TYPE
5337 && TREE_CODE (otype) == POINTER_TYPE
5338 && TYPE_PRECISION (type) != TYPE_PRECISION (otype))
5339 warning ("cast from pointer to integer of different size");
5341 if (TREE_CODE (type) == POINTER_TYPE
5342 && TREE_CODE (otype) == INTEGER_TYPE
5343 && TYPE_PRECISION (type) != TYPE_PRECISION (otype)
5344 /* Don't warn about converting any constant. */
5345 && !TREE_CONSTANT (value))
5346 warning ("cast to pointer from integer of different size");
5348 if (TREE_CODE (type) == REFERENCE_TYPE)
5349 value = (convert_from_reference
5350 (convert_to_reference (type, value, CONV_C_CAST,
5351 LOOKUP_COMPLAIN, NULL_TREE)));
5356 value = decl_constant_value (value);
5359 value = convert_force (type, value, CONV_C_CAST);
5361 /* Ignore any integer overflow caused by the cast. */
5362 if (TREE_CODE (value) == INTEGER_CST)
5364 TREE_OVERFLOW (value) = TREE_OVERFLOW (ovalue);
5365 TREE_CONSTANT_OVERFLOW (value) = TREE_CONSTANT_OVERFLOW (ovalue);
5369 /* Warn about possible alignment problems. Do this here when we will have
5370 instantiated any necessary template types. */
5371 if (STRICT_ALIGNMENT && warn_cast_align
5372 && TREE_CODE (type) == POINTER_TYPE
5373 && TREE_CODE (otype) == POINTER_TYPE
5374 && TREE_CODE (TREE_TYPE (otype)) != VOID_TYPE
5375 && TREE_CODE (TREE_TYPE (otype)) != FUNCTION_TYPE
5376 && COMPLETE_TYPE_P (TREE_TYPE (otype))
5377 && COMPLETE_TYPE_P (TREE_TYPE (type))
5378 && TYPE_ALIGN (TREE_TYPE (type)) > TYPE_ALIGN (TREE_TYPE (otype)))
5379 warning ("cast from `%T' to `%T' increases required alignment of target type",
5382 /* Always produce some operator for an explicit cast,
5383 so we can tell (for -pedantic) that the cast is no lvalue. */
5384 if (TREE_CODE (type) != REFERENCE_TYPE && value == expr
5385 && real_lvalue_p (value))
5386 value = non_lvalue (value);
5391 /* Build an assignment expression of lvalue LHS from value RHS.
5392 MODIFYCODE is the code for a binary operator that we use
5393 to combine the old value of LHS with RHS to get the new value.
5394 Or else MODIFYCODE is NOP_EXPR meaning do a simple assignment.
5396 C++: If MODIFYCODE is INIT_EXPR, then leave references unbashed. */
5399 build_modify_expr (lhs, modifycode, rhs)
5401 enum tree_code modifycode;
5404 register tree result;
5406 tree lhstype = TREE_TYPE (lhs);
5407 tree olhstype = lhstype;
5410 /* Avoid duplicate error messages from operands that had errors. */
5411 if (lhs == error_mark_node || rhs == error_mark_node)
5412 return error_mark_node;
5414 /* Handle control structure constructs used as "lvalues". */
5415 switch (TREE_CODE (lhs))
5417 /* Handle --foo = 5; as these are valid constructs in C++ */
5418 case PREDECREMENT_EXPR:
5419 case PREINCREMENT_EXPR:
5420 if (TREE_SIDE_EFFECTS (TREE_OPERAND (lhs, 0)))
5421 lhs = build (TREE_CODE (lhs), TREE_TYPE (lhs),
5422 stabilize_reference (TREE_OPERAND (lhs, 0)),
5423 TREE_OPERAND (lhs, 1));
5424 return build (COMPOUND_EXPR, lhstype,
5426 build_modify_expr (TREE_OPERAND (lhs, 0),
5429 /* Handle (a, b) used as an "lvalue". */
5431 newrhs = build_modify_expr (TREE_OPERAND (lhs, 1),
5433 if (newrhs == error_mark_node)
5434 return error_mark_node;
5435 return build (COMPOUND_EXPR, lhstype,
5436 TREE_OPERAND (lhs, 0), newrhs);
5439 newrhs = build_modify_expr (TREE_OPERAND (lhs, 0), modifycode, rhs);
5440 if (newrhs == error_mark_node)
5441 return error_mark_node;
5442 return build (COMPOUND_EXPR, lhstype, lhs, newrhs);
5444 /* Handle (a ? b : c) used as an "lvalue". */
5447 /* Produce (a ? (b = rhs) : (c = rhs))
5448 except that the RHS goes through a save-expr
5449 so the code to compute it is only emitted once. */
5452 rhs = save_expr (rhs);
5454 /* Check this here to avoid odd errors when trying to convert
5455 a throw to the type of the COND_EXPR. */
5456 if (!lvalue_or_else (lhs, "assignment"))
5457 return error_mark_node;
5459 cond = build_conditional_expr
5460 (TREE_OPERAND (lhs, 0),
5461 build_modify_expr (cp_convert (TREE_TYPE (lhs),
5462 TREE_OPERAND (lhs, 1)),
5464 build_modify_expr (cp_convert (TREE_TYPE (lhs),
5465 TREE_OPERAND (lhs, 2)),
5468 if (cond == error_mark_node)
5470 /* Make sure the code to compute the rhs comes out
5471 before the split. */
5472 return build (COMPOUND_EXPR, TREE_TYPE (lhs),
5473 /* Cast to void to suppress warning
5474 from warn_if_unused_value. */
5475 cp_convert (void_type_node, rhs), cond);
5479 lhs = resolve_offset_ref (lhs);
5480 if (lhs == error_mark_node)
5481 return error_mark_node;
5482 olhstype = lhstype = TREE_TYPE (lhs);
5488 if (modifycode == INIT_EXPR)
5490 if (TREE_CODE (rhs) == CONSTRUCTOR)
5492 my_friendly_assert (same_type_p (TREE_TYPE (rhs), lhstype),
5494 result = build (INIT_EXPR, lhstype, lhs, rhs);
5495 TREE_SIDE_EFFECTS (result) = 1;
5498 else if (! IS_AGGR_TYPE (lhstype))
5499 /* Do the default thing */;
5502 result = build_method_call (lhs, complete_ctor_identifier,
5503 build_tree_list (NULL_TREE, rhs),
5504 TYPE_BINFO (lhstype), LOOKUP_NORMAL);
5505 if (result == NULL_TREE)
5506 return error_mark_node;
5512 if (TREE_CODE (lhstype) == REFERENCE_TYPE)
5514 lhs = convert_from_reference (lhs);
5515 olhstype = lhstype = TREE_TYPE (lhs);
5517 lhs = require_complete_type (lhs);
5518 if (lhs == error_mark_node)
5519 return error_mark_node;
5521 if (modifycode == NOP_EXPR)
5523 /* `operator=' is not an inheritable operator. */
5524 if (! IS_AGGR_TYPE (lhstype))
5525 /* Do the default thing */;
5528 result = build_opfncall (MODIFY_EXPR, LOOKUP_NORMAL,
5529 lhs, rhs, make_node (NOP_EXPR));
5530 if (result == NULL_TREE)
5531 return error_mark_node;
5538 /* A binary op has been requested. Combine the old LHS
5539 value with the RHS producing the value we should actually
5540 store into the LHS. */
5542 my_friendly_assert (!PROMOTES_TO_AGGR_TYPE (lhstype, REFERENCE_TYPE),
5544 lhs = stabilize_reference (lhs);
5545 newrhs = cp_build_binary_op (modifycode, lhs, rhs);
5546 if (newrhs == error_mark_node)
5548 error (" in evaluation of `%Q(%#T, %#T)'", modifycode,
5549 TREE_TYPE (lhs), TREE_TYPE (rhs));
5550 return error_mark_node;
5553 /* Now it looks like a plain assignment. */
5554 modifycode = NOP_EXPR;
5556 my_friendly_assert (TREE_CODE (lhstype) != REFERENCE_TYPE, 20011220);
5557 my_friendly_assert (TREE_CODE (TREE_TYPE (newrhs)) != REFERENCE_TYPE,
5561 /* Handle a cast used as an "lvalue".
5562 We have already performed any binary operator using the value as cast.
5563 Now convert the result to the cast type of the lhs,
5564 and then true type of the lhs and store it there;
5565 then convert result back to the cast type to be the value
5566 of the assignment. */
5568 switch (TREE_CODE (lhs))
5573 case FIX_TRUNC_EXPR:
5574 case FIX_FLOOR_EXPR:
5575 case FIX_ROUND_EXPR:
5578 tree inner_lhs = TREE_OPERAND (lhs, 0);
5581 if (TREE_CODE (TREE_TYPE (newrhs)) == ARRAY_TYPE
5582 || TREE_CODE (TREE_TYPE (newrhs)) == FUNCTION_TYPE
5583 || TREE_CODE (TREE_TYPE (newrhs)) == METHOD_TYPE
5584 || TREE_CODE (TREE_TYPE (newrhs)) == OFFSET_TYPE)
5585 newrhs = default_conversion (newrhs);
5587 /* ISO C++ 5.4/1: The result is an lvalue if T is a reference
5588 type, otherwise the result is an rvalue. */
5589 if (! lvalue_p (lhs))
5590 pedwarn ("ISO C++ forbids cast to non-reference type used as lvalue");
5592 result = build_modify_expr (inner_lhs, NOP_EXPR,
5593 cp_convert (TREE_TYPE (inner_lhs),
5594 cp_convert (lhstype, newrhs)));
5595 if (result == error_mark_node)
5597 return cp_convert (TREE_TYPE (lhs), result);
5604 /* Now we have handled acceptable kinds of LHS that are not truly lvalues.
5605 Reject anything strange now. */
5607 if (!lvalue_or_else (lhs, "assignment"))
5608 return error_mark_node;
5610 /* Warn about modifying something that is `const'. Don't warn if
5611 this is initialization. */
5612 if (modifycode != INIT_EXPR
5613 && (TREE_READONLY (lhs) || CP_TYPE_CONST_P (lhstype)
5614 /* Functions are not modifiable, even though they are
5616 || TREE_CODE (TREE_TYPE (lhs)) == FUNCTION_TYPE
5617 || TREE_CODE (TREE_TYPE (lhs)) == METHOD_TYPE
5618 /* If it's an aggregate and any field is const, then it is
5619 effectively const. */
5620 || (IS_AGGR_TYPE_CODE (TREE_CODE (lhstype))
5621 && C_TYPE_FIELDS_READONLY (lhstype))))
5622 readonly_error (lhs, "assignment", 0);
5624 /* If storing into a structure or union member, it has probably been
5625 given type `int'. Compute the type that would go with the actual
5626 amount of storage the member occupies. */
5628 if (TREE_CODE (lhs) == COMPONENT_REF
5629 && (TREE_CODE (lhstype) == INTEGER_TYPE
5630 || TREE_CODE (lhstype) == REAL_TYPE
5631 || TREE_CODE (lhstype) == ENUMERAL_TYPE))
5633 lhstype = TREE_TYPE (get_unwidened (lhs, 0));
5635 /* If storing in a field that is in actuality a short or narrower
5636 than one, we must store in the field in its actual type. */
5638 if (lhstype != TREE_TYPE (lhs))
5640 lhs = copy_node (lhs);
5641 TREE_TYPE (lhs) = lhstype;
5645 if (TREE_CODE (lhstype) != REFERENCE_TYPE)
5647 if (TREE_SIDE_EFFECTS (lhs))
5648 lhs = stabilize_reference (lhs);
5649 if (TREE_SIDE_EFFECTS (newrhs))
5650 newrhs = stabilize_reference (newrhs);
5653 /* Convert new value to destination type. */
5655 if (TREE_CODE (lhstype) == ARRAY_TYPE)
5659 if (!same_or_base_type_p (TYPE_MAIN_VARIANT (lhstype),
5660 TYPE_MAIN_VARIANT (TREE_TYPE (rhs))))
5662 error ("incompatible types in assignment of `%T' to `%T'",
5663 TREE_TYPE (rhs), lhstype);
5664 return error_mark_node;
5667 /* Allow array assignment in compiler-generated code. */
5668 if (! DECL_ARTIFICIAL (current_function_decl))
5669 pedwarn ("ISO C++ forbids assignment of arrays");
5671 from_array = TREE_CODE (TREE_TYPE (newrhs)) == ARRAY_TYPE
5672 ? 1 + (modifycode != INIT_EXPR): 0;
5673 return build_vec_init (lhs, newrhs, from_array);
5676 if (modifycode == INIT_EXPR)
5677 newrhs = convert_for_initialization (lhs, lhstype, newrhs, LOOKUP_NORMAL,
5678 "initialization", NULL_TREE, 0);
5681 /* Avoid warnings on enum bit fields. */
5682 if (TREE_CODE (olhstype) == ENUMERAL_TYPE
5683 && TREE_CODE (lhstype) == INTEGER_TYPE)
5685 newrhs = convert_for_assignment (olhstype, newrhs, "assignment",
5687 newrhs = convert_force (lhstype, newrhs, 0);
5690 newrhs = convert_for_assignment (lhstype, newrhs, "assignment",
5692 if (TREE_CODE (newrhs) == CALL_EXPR
5693 && TYPE_NEEDS_CONSTRUCTING (lhstype))
5694 newrhs = build_cplus_new (lhstype, newrhs);
5696 /* Can't initialize directly from a TARGET_EXPR, since that would
5697 cause the lhs to be constructed twice, and possibly result in
5698 accidental self-initialization. So we force the TARGET_EXPR to be
5699 expanded without a target. */
5700 if (TREE_CODE (newrhs) == TARGET_EXPR)
5701 newrhs = build (COMPOUND_EXPR, TREE_TYPE (newrhs), newrhs,
5702 TREE_OPERAND (newrhs, 0));
5705 if (newrhs == error_mark_node)
5706 return error_mark_node;
5708 if (TREE_CODE (newrhs) == COND_EXPR)
5711 tree cond = TREE_OPERAND (newrhs, 0);
5713 if (TREE_SIDE_EFFECTS (lhs))
5714 cond = build_compound_expr (tree_cons
5716 build_tree_list (NULL_TREE, cond)));
5718 /* Cannot have two identical lhs on this one tree (result) as preexpand
5719 calls will rip them out and fill in RTL for them, but when the
5720 rtl is generated, the calls will only be in the first side of the
5721 condition, not on both, or before the conditional jump! (mrs) */
5722 lhs1 = break_out_calls (lhs);
5725 /* If there's no change, the COND_EXPR behaves like any other rhs. */
5726 result = build (modifycode == NOP_EXPR ? MODIFY_EXPR : INIT_EXPR,
5727 lhstype, lhs, newrhs);
5730 tree result_type = TREE_TYPE (newrhs);
5731 /* We have to convert each arm to the proper type because the
5732 types may have been munged by constant folding. */
5734 = build (COND_EXPR, result_type, cond,
5735 build_modify_expr (lhs, modifycode,
5736 cp_convert (result_type,
5737 TREE_OPERAND (newrhs, 1))),
5738 build_modify_expr (lhs1, modifycode,
5739 cp_convert (result_type,
5740 TREE_OPERAND (newrhs, 2))));
5744 result = build (modifycode == NOP_EXPR ? MODIFY_EXPR : INIT_EXPR,
5745 lhstype, lhs, newrhs);
5747 TREE_SIDE_EFFECTS (result) = 1;
5749 /* If we got the LHS in a different type for storing in,
5750 convert the result back to the nominal type of LHS
5751 so that the value we return always has the same type
5752 as the LHS argument. */
5754 if (olhstype == TREE_TYPE (result))
5756 /* Avoid warnings converting integral types back into enums
5757 for enum bit fields. */
5758 if (TREE_CODE (TREE_TYPE (result)) == INTEGER_TYPE
5759 && TREE_CODE (olhstype) == ENUMERAL_TYPE)
5761 result = build (COMPOUND_EXPR, olhstype, result, olhs);
5762 TREE_NO_UNUSED_WARNING (result) = 1;
5765 return convert_for_assignment (olhstype, result, "assignment",
5770 build_x_modify_expr (lhs, modifycode, rhs)
5772 enum tree_code modifycode;
5775 if (processing_template_decl)
5776 return build_min_nt (MODOP_EXPR, lhs,
5777 build_min_nt (modifycode, NULL_TREE, NULL_TREE), rhs);
5779 if (modifycode != NOP_EXPR)
5781 tree rval = build_opfncall (MODIFY_EXPR, LOOKUP_NORMAL, lhs, rhs,
5782 make_node (modifycode));
5786 return build_modify_expr (lhs, modifycode, rhs);
5790 /* Get difference in deltas for different pointer to member function
5791 types. Return integer_zero_node, if FROM cannot be converted to a
5792 TO type. If FORCE is true, then allow reverse conversions as well.
5794 Note that the naming of FROM and TO is kind of backwards; the return
5795 value is what we add to a TO in order to get a FROM. They are named
5796 this way because we call this function to find out how to convert from
5797 a pointer to member of FROM to a pointer to member of TO. */
5800 get_delta_difference (from, to, force)
5804 tree delta = integer_zero_node;
5809 binfo = lookup_base (to, from, ba_check, &kind);
5810 if (kind == bk_inaccessible || kind == bk_ambig)
5812 error (" in pointer to member function conversion");
5819 error_not_base_type (from, to);
5820 error (" in pointer to member conversion");
5823 binfo = lookup_base (from, to, ba_check, &kind);
5826 virt_binfo = binfo_from_vbase (binfo);
5830 /* This is a reinterpret cast, we choose to do nothing. */
5831 warning ("pointer to member cast via virtual base `%T' of `%T'",
5832 BINFO_TYPE (virt_binfo),
5833 BINFO_TYPE (BINFO_INHERITANCE_CHAIN (virt_binfo)));
5836 delta = BINFO_OFFSET (binfo);
5837 delta = cp_convert (ptrdiff_type_node, delta);
5838 delta = cp_build_binary_op (MINUS_EXPR,
5845 virt_binfo = binfo_from_vbase (binfo);
5848 /* This is a reinterpret cast, we choose to do nothing. */
5850 warning ("pointer to member cast via virtual base `%T' of `%T'",
5851 BINFO_TYPE (virt_binfo),
5852 BINFO_TYPE (BINFO_INHERITANCE_CHAIN (virt_binfo)));
5854 error ("pointer to member conversion via virtual base `%T' of `%T'",
5855 BINFO_TYPE (virt_binfo),
5856 BINFO_TYPE (BINFO_INHERITANCE_CHAIN (virt_binfo)));
5859 delta = BINFO_OFFSET (binfo);
5861 return cp_convert (ptrdiff_type_node, delta);
5864 /* Return a constructor for the pointer-to-member-function TYPE using
5865 the other components as specified. */
5868 build_ptrmemfunc1 (type, delta, pfn)
5869 tree type, delta, pfn;
5875 /* Pull the FIELD_DECLs out of the type. */
5876 pfn_field = TYPE_FIELDS (type);
5877 delta_field = TREE_CHAIN (pfn_field);
5879 /* Make sure DELTA has the type we want. */
5880 delta = convert_and_check (delta_type_node, delta);
5882 /* Finish creating the initializer. */
5883 u = tree_cons (pfn_field, pfn,
5884 build_tree_list (delta_field, delta));
5885 u = build (CONSTRUCTOR, type, NULL_TREE, u);
5886 TREE_CONSTANT (u) = TREE_CONSTANT (pfn) && TREE_CONSTANT (delta);
5887 TREE_STATIC (u) = (TREE_CONSTANT (u)
5888 && (initializer_constant_valid_p (pfn, TREE_TYPE (pfn))
5890 && (initializer_constant_valid_p (delta, TREE_TYPE (delta))
5895 /* Build a constructor for a pointer to member function. It can be
5896 used to initialize global variables, local variable, or used
5897 as a value in expressions. TYPE is the POINTER to METHOD_TYPE we
5900 If FORCE is non-zero, then force this conversion, even if
5901 we would rather not do it. Usually set when using an explicit
5904 Return error_mark_node, if something goes wrong. */
5907 build_ptrmemfunc (type, pfn, force)
5912 tree pfn_type = TREE_TYPE (pfn);
5913 tree to_type = build_ptrmemfunc_type (type);
5915 /* Handle multiple conversions of pointer to member functions. */
5916 if (TYPE_PTRMEMFUNC_P (TREE_TYPE (pfn)))
5918 tree delta = NULL_TREE;
5919 tree npfn = NULL_TREE;
5923 && !can_convert_arg (to_type, TREE_TYPE (pfn), pfn))
5924 error ("invalid conversion to type `%T' from type `%T'",
5927 n = get_delta_difference (TYPE_PTRMEMFUNC_OBJECT_TYPE (pfn_type),
5928 TYPE_PTRMEMFUNC_OBJECT_TYPE (to_type),
5931 /* We don't have to do any conversion to convert a
5932 pointer-to-member to its own type. But, we don't want to
5933 just return a PTRMEM_CST if there's an explicit cast; that
5934 cast should make the expression an invalid template argument. */
5935 if (TREE_CODE (pfn) != PTRMEM_CST)
5937 if (same_type_p (to_type, pfn_type))
5939 else if (integer_zerop (n))
5940 return build_reinterpret_cast (to_type, pfn);
5943 if (TREE_SIDE_EFFECTS (pfn))
5944 pfn = save_expr (pfn);
5946 /* Obtain the function pointer and the current DELTA. */
5947 if (TREE_CODE (pfn) == PTRMEM_CST)
5948 expand_ptrmemfunc_cst (pfn, &delta, &npfn);
5951 npfn = build_component_ref (pfn, pfn_identifier, NULL_TREE, 0);
5952 delta = build_component_ref (pfn, delta_identifier, NULL_TREE, 0);
5955 /* Just adjust the DELTA field. */
5956 delta = cp_convert (ptrdiff_type_node, delta);
5957 if (TARGET_PTRMEMFUNC_VBIT_LOCATION == ptrmemfunc_vbit_in_delta)
5958 n = cp_build_binary_op (LSHIFT_EXPR, n, integer_one_node);
5959 delta = cp_build_binary_op (PLUS_EXPR, delta, n);
5960 return build_ptrmemfunc1 (to_type, delta, npfn);
5963 /* Handle null pointer to member function conversions. */
5964 if (integer_zerop (pfn))
5966 pfn = build_c_cast (type, integer_zero_node);
5967 return build_ptrmemfunc1 (to_type,
5972 if (type_unknown_p (pfn))
5973 return instantiate_type (type, pfn, tf_error | tf_warning);
5975 fn = TREE_OPERAND (pfn, 0);
5976 my_friendly_assert (TREE_CODE (fn) == FUNCTION_DECL, 0);
5977 return make_ptrmem_cst (to_type, fn);
5980 /* Return the DELTA, IDX, PFN, and DELTA2 values for the PTRMEM_CST
5983 ??? There is no consistency as to the types returned for the above
5984 values. Some code acts as if its a sizetype and some as if its
5985 integer_type_node. */
5988 expand_ptrmemfunc_cst (cst, delta, pfn)
5993 tree type = TREE_TYPE (cst);
5994 tree fn = PTRMEM_CST_MEMBER (cst);
5995 tree ptr_class, fn_class;
5997 my_friendly_assert (TREE_CODE (fn) == FUNCTION_DECL, 0);
5999 /* The class that the function belongs to. */
6000 fn_class = DECL_CONTEXT (fn);
6002 /* The class that we're creating a pointer to member of. */
6003 ptr_class = TYPE_PTRMEMFUNC_OBJECT_TYPE (type);
6005 /* First, calculate the adjustment to the function's class. */
6006 *delta = get_delta_difference (fn_class, ptr_class, /*force=*/0);
6008 if (!DECL_VIRTUAL_P (fn))
6009 *pfn = convert (TYPE_PTRMEMFUNC_FN_TYPE (type), build_addr_func (fn));
6012 /* If we're dealing with a virtual function, we have to adjust 'this'
6013 again, to point to the base which provides the vtable entry for
6014 fn; the call will do the opposite adjustment. */
6015 tree orig_class = DECL_VIRTUAL_CONTEXT (fn);
6016 tree binfo = binfo_or_else (orig_class, fn_class);
6017 *delta = fold (build (PLUS_EXPR, TREE_TYPE (*delta),
6018 *delta, BINFO_OFFSET (binfo)));
6020 /* We set PFN to the vtable offset at which the function can be
6021 found, plus one (unless ptrmemfunc_vbit_in_delta, in which
6022 case delta is shifted left, and then incremented). */
6023 *pfn = DECL_VINDEX (fn);
6024 *pfn = fold (build (MULT_EXPR, integer_type_node, *pfn,
6025 TYPE_SIZE_UNIT (vtable_entry_type)));
6027 switch (TARGET_PTRMEMFUNC_VBIT_LOCATION)
6029 case ptrmemfunc_vbit_in_pfn:
6030 *pfn = fold (build (PLUS_EXPR, integer_type_node, *pfn,
6034 case ptrmemfunc_vbit_in_delta:
6035 *delta = fold (build (LSHIFT_EXPR, TREE_TYPE (*delta),
6036 *delta, integer_one_node));
6037 *delta = fold (build (PLUS_EXPR, TREE_TYPE (*delta),
6038 *delta, integer_one_node));
6045 *pfn = fold (build1 (NOP_EXPR, TYPE_PTRMEMFUNC_FN_TYPE (type),
6050 /* Return an expression for PFN from the pointer-to-member function
6054 pfn_from_ptrmemfunc (t)
6057 if (TREE_CODE (t) == PTRMEM_CST)
6062 expand_ptrmemfunc_cst (t, &delta, &pfn);
6067 return build_component_ref (t, pfn_identifier, NULL_TREE, 0);
6070 /* Expression EXPR is about to be implicitly converted to TYPE. Warn
6071 if this is a potentially dangerous thing to do. Returns a possibly
6075 dubious_conversion_warnings (type, expr, errtype, fndecl, parmnum)
6078 const char *errtype;
6082 if (TREE_CODE (type) == REFERENCE_TYPE)
6083 type = TREE_TYPE (type);
6085 /* Issue warnings about peculiar, but legal, uses of NULL. */
6086 if (ARITHMETIC_TYPE_P (type) && expr == null_node)
6089 warning ("passing NULL used for non-pointer %s %P of `%D'",
6090 errtype, parmnum, fndecl);
6092 warning ("%s to non-pointer type `%T' from NULL", errtype, type);
6095 /* Warn about assigning a floating-point type to an integer type. */
6096 if (TREE_CODE (TREE_TYPE (expr)) == REAL_TYPE
6097 && TREE_CODE (type) == INTEGER_TYPE)
6100 warning ("passing `%T' for %s %P of `%D'",
6101 TREE_TYPE (expr), errtype, parmnum, fndecl);
6103 warning ("%s to `%T' from `%T'", errtype, type, TREE_TYPE (expr));
6105 /* And warn about assigning a negative value to an unsigned
6107 else if (TREE_UNSIGNED (type) && TREE_CODE (type) != BOOLEAN_TYPE)
6109 if (TREE_CODE (expr) == INTEGER_CST
6110 && TREE_NEGATED_INT (expr))
6113 warning ("passing negative value `%E' for %s %P of `%D'",
6114 expr, errtype, parmnum, fndecl);
6116 warning ("%s of negative value `%E' to `%T'",
6117 errtype, expr, type);
6120 overflow_warning (expr);
6122 if (TREE_CONSTANT (expr))
6128 /* Convert value RHS to type TYPE as preparation for an assignment to
6129 an lvalue of type TYPE. ERRTYPE is a string to use in error
6130 messages: "assignment", "return", etc. If FNDECL is non-NULL, we
6131 are doing the conversion in order to pass the PARMNUMth argument of
6135 convert_for_assignment (type, rhs, errtype, fndecl, parmnum)
6137 const char *errtype;
6141 register enum tree_code codel = TREE_CODE (type);
6142 register tree rhstype;
6143 register enum tree_code coder;
6145 if (codel == OFFSET_TYPE)
6148 if (TREE_CODE (rhs) == OFFSET_REF)
6149 rhs = resolve_offset_ref (rhs);
6151 /* Strip NON_LVALUE_EXPRs since we aren't using as an lvalue. */
6152 if (TREE_CODE (rhs) == NON_LVALUE_EXPR)
6153 rhs = TREE_OPERAND (rhs, 0);
6155 rhstype = TREE_TYPE (rhs);
6156 coder = TREE_CODE (rhstype);
6158 if (rhs == error_mark_node || rhstype == error_mark_node)
6159 return error_mark_node;
6160 if (TREE_CODE (rhs) == TREE_LIST && TREE_VALUE (rhs) == error_mark_node)
6161 return error_mark_node;
6163 rhs = dubious_conversion_warnings (type, rhs, errtype, fndecl, parmnum);
6165 /* The RHS of an assignment cannot have void type. */
6166 if (coder == VOID_TYPE)
6168 error ("void value not ignored as it ought to be");
6169 return error_mark_node;
6172 /* Simplify the RHS if possible. */
6173 if (TREE_CODE (rhs) == CONST_DECL)
6174 rhs = DECL_INITIAL (rhs);
6175 else if (coder != ARRAY_TYPE)
6176 rhs = decl_constant_value (rhs);
6180 The expression is implicitly converted (clause _conv_) to the
6181 cv-unqualified type of the left operand.
6183 We allow bad conversions here because by the time we get to this point
6184 we are committed to doing the conversion. If we end up doing a bad
6185 conversion, convert_like will complain. */
6186 if (!can_convert_arg_bad (type, rhstype, rhs))
6188 /* When -Wno-pmf-conversions is use, we just silently allow
6189 conversions from pointers-to-members to plain pointers. If
6190 the conversion doesn't work, cp_convert will complain. */
6192 && TYPE_PTR_P (type)
6193 && TYPE_PTRMEMFUNC_P (rhstype))
6194 rhs = cp_convert (strip_top_quals (type), rhs);
6197 /* If the right-hand side has unknown type, then it is an
6198 overloaded function. Call instantiate_type to get error
6200 if (rhstype == unknown_type_node)
6201 instantiate_type (type, rhs, tf_error | tf_warning);
6203 error ("cannot convert `%T' to `%T' for argument `%P' to `%D'",
6204 rhstype, type, parmnum, fndecl);
6206 error ("cannot convert `%T' to `%T' in %s", rhstype, type,
6208 return error_mark_node;
6211 return perform_implicit_conversion (strip_top_quals (type), rhs);
6214 /* Convert RHS to be of type TYPE.
6215 If EXP is non-zero, it is the target of the initialization.
6216 ERRTYPE is a string to use in error messages.
6218 Two major differences between the behavior of
6219 `convert_for_assignment' and `convert_for_initialization'
6220 are that references are bashed in the former, while
6221 copied in the latter, and aggregates are assigned in
6222 the former (operator=) while initialized in the
6225 If using constructor make sure no conversion operator exists, if one does
6226 exist, an ambiguity exists.
6228 If flags doesn't include LOOKUP_COMPLAIN, don't complain about anything. */
6231 convert_for_initialization (exp, type, rhs, flags, errtype, fndecl, parmnum)
6232 tree exp, type, rhs;
6234 const char *errtype;
6238 register enum tree_code codel = TREE_CODE (type);
6239 register tree rhstype;
6240 register enum tree_code coder;
6242 /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
6243 Strip such NOP_EXPRs, since RHS is used in non-lvalue context. */
6244 if (TREE_CODE (rhs) == NOP_EXPR
6245 && TREE_TYPE (rhs) == TREE_TYPE (TREE_OPERAND (rhs, 0))
6246 && codel != REFERENCE_TYPE)
6247 rhs = TREE_OPERAND (rhs, 0);
6249 if (rhs == error_mark_node
6250 || (TREE_CODE (rhs) == TREE_LIST && TREE_VALUE (rhs) == error_mark_node))
6251 return error_mark_node;
6253 if (TREE_CODE (rhs) == OFFSET_REF)
6255 rhs = resolve_offset_ref (rhs);
6256 if (rhs == error_mark_node)
6257 return error_mark_node;
6260 if (TREE_CODE (TREE_TYPE (rhs)) == REFERENCE_TYPE)
6261 rhs = convert_from_reference (rhs);
6263 if ((TREE_CODE (TREE_TYPE (rhs)) == ARRAY_TYPE
6264 && TREE_CODE (type) != ARRAY_TYPE
6265 && (TREE_CODE (type) != REFERENCE_TYPE
6266 || TREE_CODE (TREE_TYPE (type)) != ARRAY_TYPE))
6267 || (TREE_CODE (TREE_TYPE (rhs)) == FUNCTION_TYPE
6268 && (TREE_CODE (type) != REFERENCE_TYPE
6269 || TREE_CODE (TREE_TYPE (type)) != FUNCTION_TYPE))
6270 || TREE_CODE (TREE_TYPE (rhs)) == METHOD_TYPE)
6271 rhs = default_conversion (rhs);
6273 rhstype = TREE_TYPE (rhs);
6274 coder = TREE_CODE (rhstype);
6276 if (coder == ERROR_MARK)
6277 return error_mark_node;
6279 /* We accept references to incomplete types, so we can
6280 return here before checking if RHS is of complete type. */
6282 if (codel == REFERENCE_TYPE)
6284 /* This should eventually happen in convert_arguments. */
6285 int savew = 0, savee = 0;
6288 savew = warningcount, savee = errorcount;
6289 rhs = initialize_reference (type, rhs);
6292 if (warningcount > savew)
6293 cp_warning_at ("in passing argument %P of `%+D'", parmnum, fndecl);
6294 else if (errorcount > savee)
6295 cp_error_at ("in passing argument %P of `%+D'", parmnum, fndecl);
6301 exp = require_complete_type (exp);
6302 if (exp == error_mark_node)
6303 return error_mark_node;
6305 if (TREE_CODE (rhstype) == REFERENCE_TYPE)
6306 rhstype = TREE_TYPE (rhstype);
6308 type = complete_type (type);
6310 if (IS_AGGR_TYPE (type))
6311 return ocp_convert (type, rhs, CONV_IMPLICIT|CONV_FORCE_TEMP, flags);
6313 return convert_for_assignment (type, rhs, errtype, fndecl, parmnum);
6316 /* Expand an ASM statement with operands, handling output operands
6317 that are not variables or INDIRECT_REFS by transforming such
6318 cases into cases that expand_asm_operands can handle.
6320 Arguments are same as for expand_asm_operands.
6322 We don't do default conversions on all inputs, because it can screw
6323 up operands that are expected to be in memory. */
6326 c_expand_asm_operands (string, outputs, inputs, clobbers, vol, filename, line)
6327 tree string, outputs, inputs, clobbers;
6329 const char *filename;
6332 int noutputs = list_length (outputs);
6334 /* o[I] is the place that output number I should be written. */
6335 register tree *o = (tree *) alloca (noutputs * sizeof (tree));
6338 /* Record the contents of OUTPUTS before it is modified. */
6339 for (i = 0, tail = outputs; tail; tail = TREE_CHAIN (tail), i++)
6340 o[i] = TREE_VALUE (tail);
6342 /* Generate the ASM_OPERANDS insn;
6343 store into the TREE_VALUEs of OUTPUTS some trees for
6344 where the values were actually stored. */
6345 expand_asm_operands (string, outputs, inputs, clobbers, vol, filename, line);
6347 /* Copy all the intermediate outputs into the specified outputs. */
6348 for (i = 0, tail = outputs; tail; tail = TREE_CHAIN (tail), i++)
6350 if (o[i] != TREE_VALUE (tail))
6352 expand_expr (build_modify_expr (o[i], NOP_EXPR, TREE_VALUE (tail)),
6353 const0_rtx, VOIDmode, EXPAND_NORMAL);
6356 /* Restore the original value so that it's correct the next
6357 time we expand this function. */
6358 TREE_VALUE (tail) = o[i];
6360 /* Detect modification of read-only values.
6361 (Otherwise done by build_modify_expr.) */
6364 tree type = TREE_TYPE (o[i]);
6365 if (CP_TYPE_CONST_P (type)
6366 || (IS_AGGR_TYPE_CODE (TREE_CODE (type))
6367 && C_TYPE_FIELDS_READONLY (type)))
6368 readonly_error (o[i], "modification by `asm'", 1);
6372 /* Those MODIFY_EXPRs could do autoincrements. */
6376 /* If RETVAL is the address of, or a reference to, a local variable or
6377 temporary give an appropraite warning. */
6380 maybe_warn_about_returning_address_of_local (retval)
6383 tree valtype = TREE_TYPE (DECL_RESULT (current_function_decl));
6384 tree whats_returned = retval;
6388 if (TREE_CODE (whats_returned) == COMPOUND_EXPR)
6389 whats_returned = TREE_OPERAND (whats_returned, 1);
6390 else if (TREE_CODE (whats_returned) == CONVERT_EXPR
6391 || TREE_CODE (whats_returned) == NON_LVALUE_EXPR
6392 || TREE_CODE (whats_returned) == NOP_EXPR)
6393 whats_returned = TREE_OPERAND (whats_returned, 0);
6398 if (TREE_CODE (whats_returned) != ADDR_EXPR)
6400 whats_returned = TREE_OPERAND (whats_returned, 0);
6402 if (TREE_CODE (valtype) == REFERENCE_TYPE)
6404 if (TREE_CODE (whats_returned) == AGGR_INIT_EXPR
6405 || TREE_CODE (whats_returned) == TARGET_EXPR)
6407 /* Get the target. */
6408 whats_returned = TREE_OPERAND (whats_returned, 0);
6409 warning ("returning reference to temporary");
6412 if (TREE_CODE (whats_returned) == VAR_DECL
6413 && DECL_NAME (whats_returned)
6414 && TEMP_NAME_P (DECL_NAME (whats_returned)))
6416 warning ("reference to non-lvalue returned");
6421 if (TREE_CODE (whats_returned) == VAR_DECL
6422 && DECL_NAME (whats_returned)
6423 && DECL_FUNCTION_SCOPE_P (whats_returned)
6424 && !(TREE_STATIC (whats_returned)
6425 || TREE_PUBLIC (whats_returned)))
6427 if (TREE_CODE (valtype) == REFERENCE_TYPE)
6428 cp_warning_at ("reference to local variable `%D' returned",
6431 cp_warning_at ("address of local variable `%D' returned",
6437 /* Check that returning RETVAL from the current function is legal.
6438 Return an expression explicitly showing all conversions required to
6439 change RETVAL into the function return type, and to assign it to
6440 the DECL_RESULT for the function. */
6443 check_return_expr (retval)
6447 /* The type actually returned by the function, after any
6450 int fn_returns_value_p;
6452 /* A `volatile' function is one that isn't supposed to return, ever.
6453 (This is a G++ extension, used to get better code for functions
6454 that call the `volatile' function.) */
6455 if (TREE_THIS_VOLATILE (current_function_decl))
6456 warning ("function declared `noreturn' has a `return' statement");
6458 /* Check for various simple errors. */
6459 if (DECL_DESTRUCTOR_P (current_function_decl))
6462 error ("returning a value from a destructor");
6465 else if (DECL_CONSTRUCTOR_P (current_function_decl))
6467 if (in_function_try_handler)
6468 /* If a return statement appears in a handler of the
6469 function-try-block of a constructor, the program is ill-formed. */
6470 error ("cannot return from a handler of a function-try-block of a constructor");
6472 /* You can't return a value from a constructor. */
6473 error ("returning a value from a constructor");
6477 /* When no explicit return-value is given in a function with a named
6478 return value, the named return value is used. */
6479 result = DECL_RESULT (current_function_decl);
6480 valtype = TREE_TYPE (result);
6481 my_friendly_assert (valtype != NULL_TREE, 19990924);
6482 fn_returns_value_p = !VOID_TYPE_P (valtype);
6483 if (!retval && DECL_NAME (result) && fn_returns_value_p)
6486 /* Check for a return statement with no return value in a function
6487 that's supposed to return a value. */
6488 if (!retval && fn_returns_value_p)
6490 pedwarn ("return-statement with no value, in function declared with a non-void return type");
6491 /* Clear this, so finish_function won't say that we reach the
6492 end of a non-void function (which we don't, we gave a
6494 current_function_returns_null = 0;
6496 /* Check for a return statement with a value in a function that
6497 isn't supposed to return a value. */
6498 else if (retval && !fn_returns_value_p)
6500 if (VOID_TYPE_P (TREE_TYPE (retval)))
6501 /* You can return a `void' value from a function of `void'
6502 type. In that case, we have to evaluate the expression for
6503 its side-effects. */
6504 finish_expr_stmt (retval);
6506 pedwarn ("return-statement with a value, in function declared with a void return type");
6508 current_function_returns_null = 1;
6510 /* There's really no value to return, after all. */
6514 /* Remember that this function can sometimes return without a
6516 current_function_returns_null = 1;
6518 /* Remember that this function did return a value. */
6519 current_function_returns_value = 1;
6521 /* Only operator new(...) throw(), can return NULL [expr.new/13]. */
6522 if ((DECL_OVERLOADED_OPERATOR_P (current_function_decl) == NEW_EXPR
6523 || DECL_OVERLOADED_OPERATOR_P (current_function_decl) == VEC_NEW_EXPR)
6524 && !TYPE_NOTHROW_P (TREE_TYPE (current_function_decl))
6526 && null_ptr_cst_p (retval))
6527 warning ("`operator new' must not return NULL unless it is declared `throw()' (or -fcheck-new is in effect)");
6529 /* Effective C++ rule 15. See also start_function. */
6531 && DECL_NAME (current_function_decl) == ansi_assopname(NOP_EXPR)
6532 && retval != current_class_ref)
6533 warning ("`operator=' should return a reference to `*this'");
6535 /* The fabled Named Return Value optimization, as per [class.copy]/15:
6537 [...] For a function with a class return type, if the expression
6538 in the return statement is the name of a local object, and the cv-
6539 unqualified type of the local object is the same as the function
6540 return type, an implementation is permitted to omit creating the tem-
6541 porary object to hold the function return value [...]
6543 So, if this is a value-returning function that always returns the same
6544 local variable, remember it.
6546 It might be nice to be more flexible, and choose the first suitable
6547 variable even if the function sometimes returns something else, but
6548 then we run the risk of clobbering the variable we chose if the other
6549 returned expression uses the chosen variable somehow. And people expect
6550 this restriction, anyway. (jason 2000-11-19)
6552 See finish_function, genrtl_start_function, and declare_return_variable
6553 for other pieces of this optimization. */
6555 if (fn_returns_value_p && flag_elide_constructors)
6557 if (retval != NULL_TREE
6558 && (current_function_return_value == NULL_TREE
6559 || current_function_return_value == retval)
6560 && TREE_CODE (retval) == VAR_DECL
6561 && DECL_CONTEXT (retval) == current_function_decl
6562 && ! TREE_STATIC (retval)
6563 && (DECL_ALIGN (retval)
6564 >= DECL_ALIGN (DECL_RESULT (current_function_decl)))
6565 && same_type_p ((TYPE_MAIN_VARIANT
6566 (TREE_TYPE (retval))),
6568 (TREE_TYPE (TREE_TYPE (current_function_decl))))))
6569 current_function_return_value = retval;
6571 current_function_return_value = error_mark_node;
6574 /* We don't need to do any conversions when there's nothing being
6576 if (!retval || retval == error_mark_node)
6579 /* Do any required conversions. */
6580 if (retval == result || DECL_CONSTRUCTOR_P (current_function_decl))
6581 /* No conversions are required. */
6585 /* The type the function is declared to return. */
6586 tree functype = TREE_TYPE (TREE_TYPE (current_function_decl));
6588 /* First convert the value to the function's return type, then
6589 to the type of return value's location to handle the
6590 case that functype is smaller than the valtype. */
6591 retval = convert_for_initialization
6592 (NULL_TREE, functype, retval, LOOKUP_NORMAL|LOOKUP_ONLYCONVERTING,
6593 "return", NULL_TREE, 0);
6594 retval = convert (valtype, retval);
6596 /* If the conversion failed, treat this just like `return;'. */
6597 if (retval == error_mark_node)
6599 /* We can't initialize a register from a AGGR_INIT_EXPR. */
6600 else if (! current_function_returns_struct
6601 && TREE_CODE (retval) == TARGET_EXPR
6602 && TREE_CODE (TREE_OPERAND (retval, 1)) == AGGR_INIT_EXPR)
6603 retval = build (COMPOUND_EXPR, TREE_TYPE (retval), retval,
6604 TREE_OPERAND (retval, 0));
6606 maybe_warn_about_returning_address_of_local (retval);
6609 /* Actually copy the value returned into the appropriate location. */
6610 if (retval && retval != result)
6611 retval = build (INIT_EXPR, TREE_TYPE (result), result, retval);
6617 /* Returns non-zero if the pointer-type FROM can be converted to the
6618 pointer-type TO via a qualification conversion. If CONSTP is -1,
6619 then we return non-zero if the pointers are similar, and the
6620 cv-qualification signature of FROM is a proper subset of that of TO.
6622 If CONSTP is positive, then all outer pointers have been
6626 comp_ptr_ttypes_real (to, from, constp)
6630 int to_more_cv_qualified = 0;
6632 for (; ; to = TREE_TYPE (to), from = TREE_TYPE (from))
6634 if (TREE_CODE (to) != TREE_CODE (from))
6637 if (TREE_CODE (from) == OFFSET_TYPE
6638 && same_type_p (TYPE_OFFSET_BASETYPE (from),
6639 TYPE_OFFSET_BASETYPE (to)))
6642 /* Const and volatile mean something different for function types,
6643 so the usual checks are not appropriate. */
6644 if (TREE_CODE (to) != FUNCTION_TYPE && TREE_CODE (to) != METHOD_TYPE)
6646 if (!at_least_as_qualified_p (to, from))
6649 if (!at_least_as_qualified_p (from, to))
6654 ++to_more_cv_qualified;
6658 constp &= TYPE_READONLY (to);
6661 if (TREE_CODE (to) != POINTER_TYPE)
6663 same_type_ignoring_top_level_qualifiers_p (to, from)
6664 && (constp >= 0 || to_more_cv_qualified);
6668 /* When comparing, say, char ** to char const **, this function takes the
6669 'char *' and 'char const *'. Do not pass non-pointer types to this
6673 comp_ptr_ttypes (to, from)
6676 return comp_ptr_ttypes_real (to, from, 1);
6679 /* Returns 1 if to and from are (possibly multi-level) pointers to the same
6680 type or inheritance-related types, regardless of cv-quals. */
6683 ptr_reasonably_similar (to, from)
6686 for (; ; to = TREE_TYPE (to), from = TREE_TYPE (from))
6688 /* Any target type is similar enough to void. */
6689 if (TREE_CODE (to) == VOID_TYPE
6690 || TREE_CODE (from) == VOID_TYPE)
6693 if (TREE_CODE (to) != TREE_CODE (from))
6696 if (TREE_CODE (from) == OFFSET_TYPE
6697 && comptypes (TYPE_OFFSET_BASETYPE (to),
6698 TYPE_OFFSET_BASETYPE (from),
6699 COMPARE_BASE | COMPARE_RELAXED))
6702 if (TREE_CODE (to) == INTEGER_TYPE
6703 && TYPE_PRECISION (to) == TYPE_PRECISION (from))
6706 if (TREE_CODE (to) == FUNCTION_TYPE)
6709 if (TREE_CODE (to) != POINTER_TYPE)
6711 (TYPE_MAIN_VARIANT (to), TYPE_MAIN_VARIANT (from),
6712 COMPARE_BASE | COMPARE_RELAXED);
6716 /* Like comp_ptr_ttypes, for const_cast. */
6719 comp_ptr_ttypes_const (to, from)
6722 for (; ; to = TREE_TYPE (to), from = TREE_TYPE (from))
6724 if (TREE_CODE (to) != TREE_CODE (from))
6727 if (TREE_CODE (from) == OFFSET_TYPE
6728 && same_type_p (TYPE_OFFSET_BASETYPE (from),
6729 TYPE_OFFSET_BASETYPE (to)))
6732 if (TREE_CODE (to) != POINTER_TYPE)
6733 return same_type_ignoring_top_level_qualifiers_p (to, from);
6737 /* Like comp_ptr_ttypes, for reinterpret_cast. */
6740 comp_ptr_ttypes_reinterpret (to, from)
6745 for (; ; to = TREE_TYPE (to), from = TREE_TYPE (from))
6747 if (TREE_CODE (from) == OFFSET_TYPE)
6748 from = TREE_TYPE (from);
6749 if (TREE_CODE (to) == OFFSET_TYPE)
6750 to = TREE_TYPE (to);
6752 /* Const and volatile mean something different for function types,
6753 so the usual checks are not appropriate. */
6754 if (TREE_CODE (from) != FUNCTION_TYPE && TREE_CODE (from) != METHOD_TYPE
6755 && TREE_CODE (to) != FUNCTION_TYPE && TREE_CODE (to) != METHOD_TYPE)
6757 if (!at_least_as_qualified_p (to, from))
6761 && !at_least_as_qualified_p (from, to))
6763 constp &= TYPE_READONLY (to);
6766 if (TREE_CODE (from) != POINTER_TYPE
6767 || TREE_CODE (to) != POINTER_TYPE)
6772 /* Returns the type qualifiers for this type, including the qualifiers on the
6773 elements for an array type. */
6776 cp_type_quals (type)
6779 type = strip_array_types (type);
6780 return TYPE_QUALS (type);
6783 /* Returns non-zero if the TYPE contains a mutable member */
6786 cp_has_mutable_p (type)
6789 type = strip_array_types (type);
6791 return CLASS_TYPE_P (type) && CLASSTYPE_HAS_MUTABLE (type);
6794 /* Subroutine of casts_away_constness. Make T1 and T2 point at
6795 exemplar types such that casting T1 to T2 is casting away castness
6796 if and only if there is no implicit conversion from T1 to T2. */
6799 casts_away_constness_r (t1, t2)
6806 /* [expr.const.cast]
6808 For multi-level pointer to members and multi-level mixed pointers
6809 and pointers to members (conv.qual), the "member" aspect of a
6810 pointer to member level is ignored when determining if a const
6811 cv-qualifier has been cast away. */
6812 if (TYPE_PTRMEM_P (*t1))
6813 *t1 = build_pointer_type (TREE_TYPE (TREE_TYPE (*t1)));
6814 if (TYPE_PTRMEM_P (*t2))
6815 *t2 = build_pointer_type (TREE_TYPE (TREE_TYPE (*t2)));
6817 /* [expr.const.cast]
6819 For two pointer types:
6821 X1 is T1cv1,1 * ... cv1,N * where T1 is not a pointer type
6822 X2 is T2cv2,1 * ... cv2,M * where T2 is not a pointer type
6825 casting from X1 to X2 casts away constness if, for a non-pointer
6826 type T there does not exist an implicit conversion (clause
6829 Tcv1,(N-K+1) * cv1,(N-K+2) * ... cv1,N *
6833 Tcv2,(M-K+1) * cv2,(M-K+2) * ... cv2,M *. */
6835 if (TREE_CODE (*t1) != POINTER_TYPE
6836 || TREE_CODE (*t2) != POINTER_TYPE)
6838 *t1 = cp_build_qualified_type (void_type_node,
6839 cp_type_quals (*t1));
6840 *t2 = cp_build_qualified_type (void_type_node,
6841 cp_type_quals (*t2));
6845 quals1 = cp_type_quals (*t1);
6846 quals2 = cp_type_quals (*t2);
6847 *t1 = TREE_TYPE (*t1);
6848 *t2 = TREE_TYPE (*t2);
6849 casts_away_constness_r (t1, t2);
6850 *t1 = build_pointer_type (*t1);
6851 *t2 = build_pointer_type (*t2);
6852 *t1 = cp_build_qualified_type (*t1, quals1);
6853 *t2 = cp_build_qualified_type (*t2, quals2);
6856 /* Returns non-zero if casting from TYPE1 to TYPE2 casts away
6860 casts_away_constness (t1, t2)
6864 if (TREE_CODE (t2) == REFERENCE_TYPE)
6866 /* [expr.const.cast]
6868 Casting from an lvalue of type T1 to an lvalue of type T2
6869 using a reference cast casts away constness if a cast from an
6870 rvalue of type "pointer to T1" to the type "pointer to T2"
6871 casts away constness. */
6872 t1 = (TREE_CODE (t1) == REFERENCE_TYPE
6873 ? TREE_TYPE (t1) : t1);
6874 return casts_away_constness (build_pointer_type (t1),
6875 build_pointer_type (TREE_TYPE (t2)));
6878 if (TYPE_PTRMEM_P (t1) && TYPE_PTRMEM_P (t2))
6879 /* [expr.const.cast]
6881 Casting from an rvalue of type "pointer to data member of X
6882 of type T1" to the type "pointer to data member of Y of type
6883 T2" casts away constness if a cast from an rvalue of type
6884 "pointer to T1" to the type "pointer to T2" casts away
6886 return casts_away_constness
6887 (build_pointer_type (TREE_TYPE (TREE_TYPE (t1))),
6888 build_pointer_type (TREE_TYPE (TREE_TYPE (t2))));
6890 /* Casting away constness is only something that makes sense for
6891 pointer or reference types. */
6892 if (TREE_CODE (t1) != POINTER_TYPE
6893 || TREE_CODE (t2) != POINTER_TYPE)
6896 /* Top-level qualifiers don't matter. */
6897 t1 = TYPE_MAIN_VARIANT (t1);
6898 t2 = TYPE_MAIN_VARIANT (t2);
6899 casts_away_constness_r (&t1, &t2);
6900 if (!can_convert (t2, t1))
6906 /* Returns TYPE with its cv qualifiers removed
6907 TYPE is T cv* .. *cv where T is not a pointer type,
6908 returns T * .. *. (If T is an array type, then the cv qualifiers
6909 above are those of the array members.) */
6912 strip_all_pointer_quals (type)
6915 if (TREE_CODE (type) == POINTER_TYPE)
6916 return build_pointer_type (strip_all_pointer_quals (TREE_TYPE (type)));
6917 else if (TREE_CODE (type) == OFFSET_TYPE)
6918 return build_offset_type (TYPE_OFFSET_BASETYPE (type),
6919 strip_all_pointer_quals (TREE_TYPE (type)));
6921 return TYPE_MAIN_VARIANT (type);