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, 2003, 2004 Free Software Foundation, Inc.
4 Hacked by Michael Tiemann (tiemann@cygnus.com)
6 This file is part of GCC.
8 GCC 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 GCC 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 GCC; see the file COPYING. If not, write to
20 the Free Software Foundation, 59 Temple Place - Suite 330,
21 Boston, MA 02111-1307, USA. */
24 /* This file is part of the C++ front end.
25 It contains routines to build C++ expressions given their operands,
26 including computing the types of the result, C and C++ specific error
27 checks, and some optimization.
29 There are also routines to build RETURN_STMT nodes and CASE_STMT nodes,
30 and to process initializations in declarations (since they work
31 like a strange sort of assignment). */
35 #include "coretypes.h"
45 #include "diagnostic.h"
49 static tree convert_for_assignment (tree, tree, const char *, tree, int);
50 static tree cp_pointer_int_sum (enum tree_code, tree, tree);
51 static tree rationalize_conditional_expr (enum tree_code, tree);
52 static int comp_ptr_ttypes_real (tree, tree, int);
53 static int comp_ptr_ttypes_const (tree, tree);
54 static bool comp_except_types (tree, tree, bool);
55 static bool comp_array_types (tree, tree, bool);
56 static tree common_base_type (tree, tree);
57 static tree lookup_anon_field (tree, tree);
58 static tree pointer_diff (tree, tree, tree);
59 static tree get_delta_difference (tree, tree, int);
60 static void casts_away_constness_r (tree *, tree *);
61 static bool casts_away_constness (tree, tree);
62 static void maybe_warn_about_returning_address_of_local (tree);
63 static tree lookup_destructor (tree, tree, tree);
65 /* Return the target type of TYPE, which means return T for:
66 T*, T&, T[], T (...), and otherwise, just T. */
69 target_type (tree type)
71 type = non_reference (type);
72 while (TREE_CODE (type) == POINTER_TYPE
73 || TREE_CODE (type) == ARRAY_TYPE
74 || TREE_CODE (type) == FUNCTION_TYPE
75 || TREE_CODE (type) == METHOD_TYPE
76 || TYPE_PTRMEM_P (type))
77 type = TREE_TYPE (type);
81 /* Do `exp = require_complete_type (exp);' to make sure exp
82 does not have an incomplete type. (That includes void types.)
83 Returns the error_mark_node if the VALUE does not have
84 complete type when this function returns. */
87 require_complete_type (tree value)
91 if (processing_template_decl || value == error_mark_node)
94 if (TREE_CODE (value) == OVERLOAD)
95 type = unknown_type_node;
97 type = TREE_TYPE (value);
99 /* First, detect a valid value with a complete type. */
100 if (COMPLETE_TYPE_P (type))
103 if (complete_type_or_else (type, value))
106 return error_mark_node;
109 /* Try to complete TYPE, if it is incomplete. For example, if TYPE is
110 a template instantiation, do the instantiation. Returns TYPE,
111 whether or not it could be completed, unless something goes
112 horribly wrong, in which case the error_mark_node is returned. */
115 complete_type (tree type)
117 if (type == NULL_TREE)
118 /* Rather than crash, we return something sure to cause an error
120 return error_mark_node;
122 if (type == error_mark_node || COMPLETE_TYPE_P (type))
124 else if (TREE_CODE (type) == ARRAY_TYPE && TYPE_DOMAIN (type))
126 tree t = complete_type (TREE_TYPE (type));
127 if (COMPLETE_TYPE_P (t) && ! processing_template_decl)
129 TYPE_NEEDS_CONSTRUCTING (type)
130 = TYPE_NEEDS_CONSTRUCTING (TYPE_MAIN_VARIANT (t));
131 TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type)
132 = TYPE_HAS_NONTRIVIAL_DESTRUCTOR (TYPE_MAIN_VARIANT (t));
134 else if (CLASS_TYPE_P (type) && CLASSTYPE_TEMPLATE_INSTANTIATION (type))
135 instantiate_class_template (TYPE_MAIN_VARIANT (type));
140 /* Like complete_type, but issue an error if the TYPE cannot be completed.
141 VALUE is used for informative diagnostics. DIAG_TYPE indicates the type
142 of diagnostic: 0 for an error, 1 for a warning, 2 for a pedwarn.
143 Returns NULL_TREE if the type cannot be made complete. */
146 complete_type_or_diagnostic (tree type, tree value, int diag_type)
148 type = complete_type (type);
149 if (type == error_mark_node)
150 /* We already issued an error. */
152 else if (!COMPLETE_TYPE_P (type))
154 cxx_incomplete_type_diagnostic (value, type, diag_type);
161 /* Return truthvalue of whether type of EXP is instantiated. */
164 type_unknown_p (tree exp)
166 return (TREE_CODE (exp) == TREE_LIST
167 || TREE_TYPE (exp) == unknown_type_node);
171 /* Return the common type of two parameter lists.
172 We assume that comptypes has already been done and returned 1;
173 if that isn't so, this may crash.
175 As an optimization, free the space we allocate if the parameter
176 lists are already common. */
179 commonparms (tree p1, tree p2)
181 tree oldargs = p1, newargs, n;
185 len = list_length (p1);
186 newargs = tree_last (p1);
188 if (newargs == void_list_node)
197 newargs = tree_cons (NULL_TREE, NULL_TREE, newargs);
202 p1 = TREE_CHAIN (p1), p2 = TREE_CHAIN (p2), n = TREE_CHAIN (n), i++)
204 if (TREE_PURPOSE (p1) && !TREE_PURPOSE (p2))
206 TREE_PURPOSE (n) = TREE_PURPOSE (p1);
209 else if (! TREE_PURPOSE (p1))
211 if (TREE_PURPOSE (p2))
213 TREE_PURPOSE (n) = TREE_PURPOSE (p2);
219 if (1 != simple_cst_equal (TREE_PURPOSE (p1), TREE_PURPOSE (p2)))
221 TREE_PURPOSE (n) = TREE_PURPOSE (p2);
223 if (TREE_VALUE (p1) != TREE_VALUE (p2))
226 TREE_VALUE (n) = merge_types (TREE_VALUE (p1), TREE_VALUE (p2));
229 TREE_VALUE (n) = TREE_VALUE (p1);
237 /* Given a type, perhaps copied for a typedef,
238 find the "original" version of it. */
240 original_type (tree t)
242 while (TYPE_NAME (t) != NULL_TREE)
244 tree x = TYPE_NAME (t);
245 if (TREE_CODE (x) != TYPE_DECL)
247 x = DECL_ORIGINAL_TYPE (x);
255 /* T1 and T2 are arithmetic or enumeration types. Return the type
256 that will result from the "usual arithmetic conversions" on T1 and
257 T2 as described in [expr]. */
260 type_after_usual_arithmetic_conversions (tree t1, tree t2)
262 enum tree_code code1 = TREE_CODE (t1);
263 enum tree_code code2 = TREE_CODE (t2);
266 /* FIXME: Attributes. */
267 my_friendly_assert (ARITHMETIC_TYPE_P (t1)
268 || TREE_CODE (t1) == COMPLEX_TYPE
269 || TREE_CODE (t1) == ENUMERAL_TYPE,
271 my_friendly_assert (ARITHMETIC_TYPE_P (t2)
272 || TREE_CODE (t2) == COMPLEX_TYPE
273 || TREE_CODE (t2) == ENUMERAL_TYPE,
276 /* In what follows, we slightly generalize the rules given in [expr] so
277 as to deal with `long long' and `complex'. First, merge the
279 attributes = (*targetm.merge_type_attributes) (t1, t2);
281 /* If one type is complex, form the common type of the non-complex
282 components, then make that complex. Use T1 or T2 if it is the
284 if (code1 == COMPLEX_TYPE || code2 == COMPLEX_TYPE)
286 tree subtype1 = code1 == COMPLEX_TYPE ? TREE_TYPE (t1) : t1;
287 tree subtype2 = code2 == COMPLEX_TYPE ? TREE_TYPE (t2) : t2;
289 = type_after_usual_arithmetic_conversions (subtype1, subtype2);
291 if (code1 == COMPLEX_TYPE && TREE_TYPE (t1) == subtype)
292 return build_type_attribute_variant (t1, attributes);
293 else if (code2 == COMPLEX_TYPE && TREE_TYPE (t2) == subtype)
294 return build_type_attribute_variant (t2, attributes);
296 return build_type_attribute_variant (build_complex_type (subtype),
300 /* If only one is real, use it as the result. */
301 if (code1 == REAL_TYPE && code2 != REAL_TYPE)
302 return build_type_attribute_variant (t1, attributes);
303 if (code2 == REAL_TYPE && code1 != REAL_TYPE)
304 return build_type_attribute_variant (t2, attributes);
306 /* Perform the integral promotions. */
307 if (code1 != REAL_TYPE)
309 t1 = type_promotes_to (t1);
310 t2 = type_promotes_to (t2);
313 /* Both real or both integers; use the one with greater precision. */
314 if (TYPE_PRECISION (t1) > TYPE_PRECISION (t2))
315 return build_type_attribute_variant (t1, attributes);
316 else if (TYPE_PRECISION (t2) > TYPE_PRECISION (t1))
317 return build_type_attribute_variant (t2, attributes);
319 /* The types are the same; no need to do anything fancy. */
320 if (TYPE_MAIN_VARIANT (t1) == TYPE_MAIN_VARIANT (t2))
321 return build_type_attribute_variant (t1, attributes);
323 if (code1 != REAL_TYPE)
325 /* If one is a sizetype, use it so size_binop doesn't blow up. */
326 if (TYPE_IS_SIZETYPE (t1) > TYPE_IS_SIZETYPE (t2))
327 return build_type_attribute_variant (t1, attributes);
328 if (TYPE_IS_SIZETYPE (t2) > TYPE_IS_SIZETYPE (t1))
329 return build_type_attribute_variant (t2, attributes);
331 /* If one is unsigned long long, then convert the other to unsigned
333 if (same_type_p (TYPE_MAIN_VARIANT (t1), long_long_unsigned_type_node)
334 || same_type_p (TYPE_MAIN_VARIANT (t2), long_long_unsigned_type_node))
335 return build_type_attribute_variant (long_long_unsigned_type_node,
337 /* If one is a long long, and the other is an unsigned long, and
338 long long can represent all the values of an unsigned long, then
339 convert to a long long. Otherwise, convert to an unsigned long
340 long. Otherwise, if either operand is long long, convert the
343 Since we're here, we know the TYPE_PRECISION is the same;
344 therefore converting to long long cannot represent all the values
345 of an unsigned long, so we choose unsigned long long in that
347 if (same_type_p (TYPE_MAIN_VARIANT (t1), long_long_integer_type_node)
348 || same_type_p (TYPE_MAIN_VARIANT (t2), long_long_integer_type_node))
350 tree t = ((TYPE_UNSIGNED (t1) || TYPE_UNSIGNED (t2))
351 ? long_long_unsigned_type_node
352 : long_long_integer_type_node);
353 return build_type_attribute_variant (t, attributes);
356 /* Go through the same procedure, but for longs. */
357 if (same_type_p (TYPE_MAIN_VARIANT (t1), long_unsigned_type_node)
358 || same_type_p (TYPE_MAIN_VARIANT (t2), long_unsigned_type_node))
359 return build_type_attribute_variant (long_unsigned_type_node,
361 if (same_type_p (TYPE_MAIN_VARIANT (t1), long_integer_type_node)
362 || same_type_p (TYPE_MAIN_VARIANT (t2), long_integer_type_node))
364 tree t = ((TYPE_UNSIGNED (t1) || TYPE_UNSIGNED (t2))
365 ? long_unsigned_type_node : long_integer_type_node);
366 return build_type_attribute_variant (t, attributes);
368 /* Otherwise prefer the unsigned one. */
369 if (TYPE_UNSIGNED (t1))
370 return build_type_attribute_variant (t1, attributes);
372 return build_type_attribute_variant (t2, attributes);
376 if (same_type_p (TYPE_MAIN_VARIANT (t1), long_double_type_node)
377 || same_type_p (TYPE_MAIN_VARIANT (t2), long_double_type_node))
378 return build_type_attribute_variant (long_double_type_node,
380 if (same_type_p (TYPE_MAIN_VARIANT (t1), double_type_node)
381 || same_type_p (TYPE_MAIN_VARIANT (t2), double_type_node))
382 return build_type_attribute_variant (double_type_node,
384 if (same_type_p (TYPE_MAIN_VARIANT (t1), float_type_node)
385 || same_type_p (TYPE_MAIN_VARIANT (t2), float_type_node))
386 return build_type_attribute_variant (float_type_node,
389 /* Two floating-point types whose TYPE_MAIN_VARIANTs are none of
390 the standard C++ floating-point types. Logic earlier in this
391 function has already eliminated the possibility that
392 TYPE_PRECISION (t2) != TYPE_PRECISION (t1), so there's no
393 compelling reason to choose one or the other. */
394 return build_type_attribute_variant (t1, attributes);
398 /* Subroutine of composite_pointer_type to implement the recursive
399 case. See that function for documentation fo the parameters. */
402 composite_pointer_type_r (tree t1, tree t2, const char* location)
409 /* Determine the types pointed to by T1 and T2. */
410 if (TREE_CODE (t1) == POINTER_TYPE)
412 pointee1 = TREE_TYPE (t1);
413 pointee2 = TREE_TYPE (t2);
417 pointee1 = TYPE_PTRMEM_POINTED_TO_TYPE (t1);
418 pointee2 = TYPE_PTRMEM_POINTED_TO_TYPE (t2);
423 Otherwise, the composite pointer type is a pointer type
424 similar (_conv.qual_) to the type of one of the operands,
425 with a cv-qualification signature (_conv.qual_) that is the
426 union of the cv-qualification signatures of the operand
428 if (same_type_ignoring_top_level_qualifiers_p (pointee1, pointee2))
429 result_type = pointee1;
430 else if ((TREE_CODE (pointee1) == POINTER_TYPE
431 && TREE_CODE (pointee2) == POINTER_TYPE)
432 || (TYPE_PTR_TO_MEMBER_P (pointee1)
433 && TYPE_PTR_TO_MEMBER_P (pointee2)))
434 result_type = composite_pointer_type_r (pointee1, pointee2, location);
437 pedwarn ("%s between distinct pointer types `%T' and `%T' "
440 result_type = void_type_node;
442 result_type = cp_build_qualified_type (result_type,
443 (cp_type_quals (pointee1)
444 | cp_type_quals (pointee2)));
445 /* If the original types were pointers to members, so is the
447 if (TYPE_PTR_TO_MEMBER_P (t1))
449 if (!same_type_p (TYPE_PTRMEM_CLASS_TYPE (t1),
450 TYPE_PTRMEM_CLASS_TYPE (t2)))
451 pedwarn ("%s between distinct pointer types `%T' and `%T' "
454 result_type = build_ptrmem_type (TYPE_PTRMEM_CLASS_TYPE (t1),
458 result_type = build_pointer_type (result_type);
460 /* Merge the attributes. */
461 attributes = (*targetm.merge_type_attributes) (t1, t2);
462 return build_type_attribute_variant (result_type, 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.
469 This routine also implements the computation of a common type for
470 pointers-to-members as per [expr.eq]. */
473 composite_pointer_type (tree t1, tree t2, tree arg1, tree arg2,
474 const char* location)
481 If one operand is a null pointer constant, the composite pointer
482 type is the type of the other operand. */
483 if (null_ptr_cst_p (arg1))
485 if (null_ptr_cst_p (arg2))
492 If one of the operands has type "pointer to cv1 void*", then
493 the other has type "pointer to cv2T", and the composite pointer
494 type is "pointer to cv12 void", where cv12 is the union of cv1
497 If either type is a pointer to void, make sure it is T1. */
498 if (TREE_CODE (t2) == POINTER_TYPE && VOID_TYPE_P (TREE_TYPE (t2)))
506 /* Now, if T1 is a pointer to void, merge the qualifiers. */
507 if (TREE_CODE (t1) == POINTER_TYPE && VOID_TYPE_P (TREE_TYPE (t1)))
512 if (pedantic && TYPE_PTRFN_P (t2))
513 pedwarn ("ISO C++ forbids %s between pointer of type `void *' and pointer-to-function", location);
515 = cp_build_qualified_type (void_type_node,
516 (cp_type_quals (TREE_TYPE (t1))
517 | cp_type_quals (TREE_TYPE (t2))));
518 result_type = build_pointer_type (result_type);
519 /* Merge the attributes. */
520 attributes = (*targetm.merge_type_attributes) (t1, t2);
521 return build_type_attribute_variant (result_type, attributes);
524 /* [expr.eq] permits the application of a pointer conversion to
525 bring the pointers to a common type. */
526 if (TREE_CODE (t1) == POINTER_TYPE && TREE_CODE (t2) == POINTER_TYPE
527 && CLASS_TYPE_P (TREE_TYPE (t1))
528 && CLASS_TYPE_P (TREE_TYPE (t2))
529 && !same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (t1),
532 class1 = TREE_TYPE (t1);
533 class2 = TREE_TYPE (t2);
535 if (DERIVED_FROM_P (class1, class2))
536 t2 = (build_pointer_type
537 (cp_build_qualified_type (class1, TYPE_QUALS (class2))));
538 else if (DERIVED_FROM_P (class2, class1))
539 t1 = (build_pointer_type
540 (cp_build_qualified_type (class2, TYPE_QUALS (class1))));
543 error ("%s between distinct pointer types `%T' and `%T' "
544 "lacks a cast", location, t1, t2);
545 return error_mark_node;
548 /* [expr.eq] permits the application of a pointer-to-member
549 conversion to change the class type of one of the types. */
550 else if (TYPE_PTR_TO_MEMBER_P (t1)
551 && !same_type_p (TYPE_PTRMEM_CLASS_TYPE (t1),
552 TYPE_PTRMEM_CLASS_TYPE (t2)))
554 class1 = TYPE_PTRMEM_CLASS_TYPE (t1);
555 class2 = TYPE_PTRMEM_CLASS_TYPE (t2);
557 if (DERIVED_FROM_P (class1, class2))
558 t1 = build_ptrmem_type (class2, TYPE_PTRMEM_POINTED_TO_TYPE (t1));
559 else if (DERIVED_FROM_P (class2, class1))
560 t2 = build_ptrmem_type (class1, TYPE_PTRMEM_POINTED_TO_TYPE (t2));
563 error ("%s between distinct pointer-to-member types `%T' and `%T' "
564 "lacks a cast", location, t1, t2);
565 return error_mark_node;
569 return composite_pointer_type_r (t1, t2, location);
572 /* Return the merged type of two types.
573 We assume that comptypes has already been done and returned 1;
574 if that isn't so, this may crash.
576 This just combines attributes and default arguments; any other
577 differences would cause the two types to compare unalike. */
580 merge_types (tree t1, tree t2)
582 enum tree_code code1;
583 enum tree_code code2;
586 /* Save time if the two types are the same. */
589 if (original_type (t1) == original_type (t2))
592 /* If one type is nonsense, use the other. */
593 if (t1 == error_mark_node)
595 if (t2 == error_mark_node)
598 /* Merge the attributes. */
599 attributes = (*targetm.merge_type_attributes) (t1, t2);
601 if (TYPE_PTRMEMFUNC_P (t1))
602 t1 = TYPE_PTRMEMFUNC_FN_TYPE (t1);
603 if (TYPE_PTRMEMFUNC_P (t2))
604 t2 = TYPE_PTRMEMFUNC_FN_TYPE (t2);
606 code1 = TREE_CODE (t1);
607 code2 = TREE_CODE (t2);
613 /* For two pointers, do this recursively on the target type. */
615 tree target = merge_types (TREE_TYPE (t1), TREE_TYPE (t2));
616 int quals = cp_type_quals (t1);
618 if (code1 == POINTER_TYPE)
619 t1 = build_pointer_type (target);
621 t1 = build_reference_type (target);
622 t1 = build_type_attribute_variant (t1, attributes);
623 t1 = cp_build_qualified_type (t1, quals);
625 if (TREE_CODE (target) == METHOD_TYPE)
626 t1 = build_ptrmemfunc_type (t1);
635 quals = cp_type_quals (t1);
636 pointee = merge_types (TYPE_PTRMEM_POINTED_TO_TYPE (t1),
637 TYPE_PTRMEM_POINTED_TO_TYPE (t2));
638 t1 = build_ptrmem_type (TYPE_PTRMEM_CLASS_TYPE (t1),
640 t1 = cp_build_qualified_type (t1, quals);
646 tree elt = merge_types (TREE_TYPE (t1), TREE_TYPE (t2));
647 /* Save space: see if the result is identical to one of the args. */
648 if (elt == TREE_TYPE (t1) && TYPE_DOMAIN (t1))
649 return build_type_attribute_variant (t1, attributes);
650 if (elt == TREE_TYPE (t2) && TYPE_DOMAIN (t2))
651 return build_type_attribute_variant (t2, attributes);
652 /* Merge the element types, and have a size if either arg has one. */
653 t1 = build_cplus_array_type
654 (elt, TYPE_DOMAIN (TYPE_DOMAIN (t1) ? t1 : t2));
659 /* Function types: prefer the one that specified arg types.
660 If both do, merge the arg types. Also merge the return types. */
662 tree valtype = merge_types (TREE_TYPE (t1), TREE_TYPE (t2));
663 tree p1 = TYPE_ARG_TYPES (t1);
664 tree p2 = TYPE_ARG_TYPES (t2);
667 /* Save space: see if the result is identical to one of the args. */
668 if (valtype == TREE_TYPE (t1) && ! p2)
669 return cp_build_type_attribute_variant (t1, attributes);
670 if (valtype == TREE_TYPE (t2) && ! p1)
671 return cp_build_type_attribute_variant (t2, attributes);
673 /* Simple way if one arg fails to specify argument types. */
674 if (p1 == NULL_TREE || TREE_VALUE (p1) == void_type_node)
676 rval = build_function_type (valtype, p2);
677 if ((raises = TYPE_RAISES_EXCEPTIONS (t2)))
678 rval = build_exception_variant (rval, raises);
679 return cp_build_type_attribute_variant (rval, attributes);
681 raises = TYPE_RAISES_EXCEPTIONS (t1);
682 if (p2 == NULL_TREE || TREE_VALUE (p2) == void_type_node)
684 rval = build_function_type (valtype, p1);
686 rval = build_exception_variant (rval, raises);
687 return cp_build_type_attribute_variant (rval, attributes);
690 rval = build_function_type (valtype, commonparms (p1, p2));
691 t1 = build_exception_variant (rval, raises);
697 /* Get this value the long way, since TYPE_METHOD_BASETYPE
698 is just the main variant of this. */
699 tree basetype = TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (t2)));
700 tree raises = TYPE_RAISES_EXCEPTIONS (t1);
703 /* If this was a member function type, get back to the
704 original type of type member function (i.e., without
705 the class instance variable up front. */
706 t1 = build_function_type (TREE_TYPE (t1),
707 TREE_CHAIN (TYPE_ARG_TYPES (t1)));
708 t2 = build_function_type (TREE_TYPE (t2),
709 TREE_CHAIN (TYPE_ARG_TYPES (t2)));
710 t3 = merge_types (t1, t2);
711 t3 = build_method_type_directly (basetype, TREE_TYPE (t3),
712 TYPE_ARG_TYPES (t3));
713 t1 = build_exception_variant (t3, raises);
718 /* There is no need to merge attributes into a TYPENAME_TYPE.
719 When the type is instantiated it will have whatever
720 attributes result from the instantiation. */
725 return cp_build_type_attribute_variant (t1, attributes);
728 /* Return the common type of two types.
729 We assume that comptypes has already been done and returned 1;
730 if that isn't so, this may crash.
732 This is the type for the result of most arithmetic operations
733 if the operands have the given two types. */
736 common_type (tree t1, tree t2)
738 enum tree_code code1;
739 enum tree_code code2;
741 /* If one type is nonsense, bail. */
742 if (t1 == error_mark_node || t2 == error_mark_node)
743 return error_mark_node;
745 code1 = TREE_CODE (t1);
746 code2 = TREE_CODE (t2);
748 if ((ARITHMETIC_TYPE_P (t1) || code1 == ENUMERAL_TYPE
749 || code1 == COMPLEX_TYPE)
750 && (ARITHMETIC_TYPE_P (t2) || code2 == ENUMERAL_TYPE
751 || code2 == COMPLEX_TYPE))
752 return type_after_usual_arithmetic_conversions (t1, t2);
754 else if ((TYPE_PTR_P (t1) && TYPE_PTR_P (t2))
755 || (TYPE_PTRMEM_P (t1) && TYPE_PTRMEM_P (t2))
756 || (TYPE_PTRMEMFUNC_P (t1) && TYPE_PTRMEMFUNC_P (t2)))
757 return composite_pointer_type (t1, t2, error_mark_node, error_mark_node,
763 /* Compare two exception specifier types for exactness or subsetness, if
764 allowed. Returns false for mismatch, true for match (same, or
767 [except.spec] "If a class X ... objects of class X or any class publicly
768 and unambiguously derived from X. Similarly, if a pointer type Y * ...
769 exceptions of type Y * or that are pointers to any type publicly and
770 unambiguously derived from Y. Otherwise a function only allows exceptions
771 that have the same type ..."
772 This does not mention cv qualifiers and is different to what throw
773 [except.throw] and catch [except.catch] will do. They will ignore the
774 top level cv qualifiers, and allow qualifiers in the pointer to class
777 We implement the letter of the standard. */
780 comp_except_types (tree a, tree b, bool exact)
782 if (same_type_p (a, b))
786 if (cp_type_quals (a) || cp_type_quals (b))
789 if (TREE_CODE (a) == POINTER_TYPE
790 && TREE_CODE (b) == POINTER_TYPE)
794 if (cp_type_quals (a) || cp_type_quals (b))
798 if (TREE_CODE (a) != RECORD_TYPE
799 || TREE_CODE (b) != RECORD_TYPE)
802 if (ACCESSIBLY_UNIQUELY_DERIVED_P (a, b))
808 /* Return true if TYPE1 and TYPE2 are equivalent exception specifiers.
809 If EXACT is false, T2 can be stricter than T1 (according to 15.4/7),
810 otherwise it must be exact. Exception lists are unordered, but
811 we've already filtered out duplicates. Most lists will be in order,
812 we should try to make use of that. */
815 comp_except_specs (tree t1, tree t2, bool exact)
824 if (t1 == NULL_TREE) /* T1 is ... */
825 return t2 == NULL_TREE || !exact;
826 if (!TREE_VALUE (t1)) /* t1 is EMPTY */
827 return t2 != NULL_TREE && !TREE_VALUE (t2);
828 if (t2 == NULL_TREE) /* T2 is ... */
830 if (TREE_VALUE (t1) && !TREE_VALUE (t2)) /* T2 is EMPTY, T1 is not */
833 /* Neither set is ... or EMPTY, make sure each part of T2 is in T1.
834 Count how many we find, to determine exactness. For exact matching and
835 ordered T1, T2, this is an O(n) operation, otherwise its worst case is
837 for (base = t1; t2 != NULL_TREE; t2 = TREE_CHAIN (t2))
839 for (probe = base; probe != NULL_TREE; probe = TREE_CHAIN (probe))
841 tree a = TREE_VALUE (probe);
842 tree b = TREE_VALUE (t2);
844 if (comp_except_types (a, b, exact))
846 if (probe == base && exact)
847 base = TREE_CHAIN (probe);
852 if (probe == NULL_TREE)
855 return !exact || base == NULL_TREE || length == list_length (t1);
858 /* Compare the array types T1 and T2. ALLOW_REDECLARATION is true if
859 [] can match [size]. */
862 comp_array_types (tree t1, tree t2, bool allow_redeclaration)
871 /* The type of the array elements must be the same. */
872 if (!same_type_p (TREE_TYPE (t1), TREE_TYPE (t2)))
875 d1 = TYPE_DOMAIN (t1);
876 d2 = TYPE_DOMAIN (t2);
881 /* If one of the arrays is dimensionless, and the other has a
882 dimension, they are of different types. However, it is valid to
890 declarations for an array object can specify
891 array types that differ by the presence or absence of a major
892 array bound (_dcl.array_). */
894 return allow_redeclaration;
896 /* Check that the dimensions are the same. */
898 if (!cp_tree_equal (TYPE_MIN_VALUE (d1), TYPE_MIN_VALUE (d2)))
900 max1 = TYPE_MAX_VALUE (d1);
901 max2 = TYPE_MAX_VALUE (d2);
902 if (processing_template_decl && !abi_version_at_least (2)
903 && !value_dependent_expression_p (max1)
904 && !value_dependent_expression_p (max2))
906 /* With abi-1 we do not fold non-dependent array bounds, (and
907 consequently mangle them incorrectly). We must therefore
908 fold them here, to verify the domains have the same
914 if (!cp_tree_equal (max1, max2))
920 /* Return true if T1 and T2 are related as allowed by STRICT. STRICT
921 is a bitwise-or of the COMPARE_* flags. */
924 comptypes (tree t1, tree t2, int strict)
929 /* Suppress errors caused by previously reported errors. */
930 if (t1 == error_mark_node || t2 == error_mark_node)
933 my_friendly_assert (TYPE_P (t1) && TYPE_P (t2), 20030623);
935 /* TYPENAME_TYPEs should be resolved if the qualifying scope is the
936 current instantiation. */
937 if (TREE_CODE (t1) == TYPENAME_TYPE)
939 tree resolved = resolve_typename_type (t1, /*only_current_p=*/true);
941 if (resolved != error_mark_node)
945 if (TREE_CODE (t2) == TYPENAME_TYPE)
947 tree resolved = resolve_typename_type (t2, /*only_current_p=*/true);
949 if (resolved != error_mark_node)
953 /* If either type is the internal version of sizetype, use the
955 if (TREE_CODE (t1) == INTEGER_TYPE && TYPE_IS_SIZETYPE (t1)
956 && TYPE_ORIG_SIZE_TYPE (t1))
957 t1 = TYPE_ORIG_SIZE_TYPE (t1);
959 if (TREE_CODE (t2) == INTEGER_TYPE && TYPE_IS_SIZETYPE (t2)
960 && TYPE_ORIG_SIZE_TYPE (t2))
961 t2 = TYPE_ORIG_SIZE_TYPE (t2);
963 if (TYPE_PTRMEMFUNC_P (t1))
964 t1 = TYPE_PTRMEMFUNC_FN_TYPE (t1);
965 if (TYPE_PTRMEMFUNC_P (t2))
966 t2 = TYPE_PTRMEMFUNC_FN_TYPE (t2);
968 /* Different classes of types can't be compatible. */
969 if (TREE_CODE (t1) != TREE_CODE (t2))
972 /* Qualifiers must match. For array types, we will check when we
973 recur on the array element types. */
974 if (TREE_CODE (t1) != ARRAY_TYPE
975 && TYPE_QUALS (t1) != TYPE_QUALS (t2))
977 if (TYPE_FOR_JAVA (t1) != TYPE_FOR_JAVA (t2))
980 /* Allow for two different type nodes which have essentially the same
981 definition. Note that we already checked for equality of the type
982 qualifiers (just above). */
984 if (TREE_CODE (t1) != ARRAY_TYPE
985 && TYPE_MAIN_VARIANT (t1) == TYPE_MAIN_VARIANT (t2))
988 if (!(*targetm.comp_type_attributes) (t1, t2))
991 switch (TREE_CODE (t1))
993 case TEMPLATE_TEMPLATE_PARM:
994 case BOUND_TEMPLATE_TEMPLATE_PARM:
995 if (TEMPLATE_TYPE_IDX (t1) != TEMPLATE_TYPE_IDX (t2)
996 || TEMPLATE_TYPE_LEVEL (t1) != TEMPLATE_TYPE_LEVEL (t2))
998 if (!comp_template_parms
999 (DECL_TEMPLATE_PARMS (TEMPLATE_TEMPLATE_PARM_TEMPLATE_DECL (t1)),
1000 DECL_TEMPLATE_PARMS (TEMPLATE_TEMPLATE_PARM_TEMPLATE_DECL (t2))))
1002 if (TREE_CODE (t1) == TEMPLATE_TEMPLATE_PARM)
1004 /* Don't check inheritance. */
1005 strict = COMPARE_STRICT;
1010 if (TYPE_TEMPLATE_INFO (t1) && TYPE_TEMPLATE_INFO (t2)
1011 && (TYPE_TI_TEMPLATE (t1) == TYPE_TI_TEMPLATE (t2)
1012 || TREE_CODE (t1) == BOUND_TEMPLATE_TEMPLATE_PARM)
1013 && comp_template_args (TYPE_TI_ARGS (t1), TYPE_TI_ARGS (t2)))
1016 if ((strict & COMPARE_BASE) && DERIVED_FROM_P (t1, t2))
1018 else if ((strict & COMPARE_DERIVED) && DERIVED_FROM_P (t2, t1))
1024 if (!comptypes (TYPE_OFFSET_BASETYPE (t1), TYPE_OFFSET_BASETYPE (t2),
1025 strict & ~COMPARE_REDECLARATION))
1030 case REFERENCE_TYPE:
1031 return same_type_p (TREE_TYPE (t1), TREE_TYPE (t2));
1035 if (!same_type_p (TREE_TYPE (t1), TREE_TYPE (t2)))
1037 return compparms (TYPE_ARG_TYPES (t1), TYPE_ARG_TYPES (t2));
1040 /* Target types must match incl. qualifiers. */
1041 return comp_array_types (t1, t2, !!(strict & COMPARE_REDECLARATION));
1043 case TEMPLATE_TYPE_PARM:
1044 return (TEMPLATE_TYPE_IDX (t1) == TEMPLATE_TYPE_IDX (t2)
1045 && TEMPLATE_TYPE_LEVEL (t1) == TEMPLATE_TYPE_LEVEL (t2));
1048 if (!cp_tree_equal (TYPENAME_TYPE_FULLNAME (t1),
1049 TYPENAME_TYPE_FULLNAME (t2)))
1051 return same_type_p (TYPE_CONTEXT (t1), TYPE_CONTEXT (t2));
1053 case UNBOUND_CLASS_TEMPLATE:
1054 if (!cp_tree_equal (TYPE_IDENTIFIER (t1), TYPE_IDENTIFIER (t2)))
1056 return same_type_p (TYPE_CONTEXT (t1), TYPE_CONTEXT (t2));
1059 return same_type_p (TREE_TYPE (t1), TREE_TYPE (t2));
1067 /* Returns 1 if TYPE1 is at least as qualified as TYPE2. */
1070 at_least_as_qualified_p (tree type1, tree type2)
1072 int q1 = cp_type_quals (type1);
1073 int q2 = cp_type_quals (type2);
1075 /* All qualifiers for TYPE2 must also appear in TYPE1. */
1076 return (q1 & q2) == q2;
1079 /* Returns 1 if TYPE1 is more qualified than TYPE2. */
1082 more_qualified_p (tree type1, tree type2)
1084 int q1 = cp_type_quals (type1);
1085 int q2 = cp_type_quals (type2);
1087 return q1 != q2 && (q1 & q2) == q2;
1090 /* Returns 1 if TYPE1 is more cv-qualified than TYPE2, -1 if TYPE2 is
1091 more cv-qualified that TYPE1, and 0 otherwise. */
1094 comp_cv_qualification (tree type1, tree type2)
1096 int q1 = cp_type_quals (type1);
1097 int q2 = cp_type_quals (type2);
1102 if ((q1 & q2) == q2)
1104 else if ((q1 & q2) == q1)
1110 /* Returns 1 if the cv-qualification signature of TYPE1 is a proper
1111 subset of the cv-qualification signature of TYPE2, and the types
1112 are similar. Returns -1 if the other way 'round, and 0 otherwise. */
1115 comp_cv_qual_signature (tree type1, tree type2)
1117 if (comp_ptr_ttypes_real (type2, type1, -1))
1119 else if (comp_ptr_ttypes_real (type1, type2, -1))
1125 /* If two types share a common base type, return that basetype.
1126 If there is not a unique most-derived base type, this function
1127 returns ERROR_MARK_NODE. */
1130 common_base_type (tree tt1, tree tt2)
1132 tree best = NULL_TREE;
1135 /* If one is a baseclass of another, that's good enough. */
1136 if (UNIQUELY_DERIVED_FROM_P (tt1, tt2))
1138 if (UNIQUELY_DERIVED_FROM_P (tt2, tt1))
1141 /* Otherwise, try to find a unique baseclass of TT1
1142 that is shared by TT2, and follow that down. */
1143 for (i = CLASSTYPE_N_BASECLASSES (tt1)-1; i >= 0; i--)
1145 tree basetype = TYPE_BINFO_BASETYPE (tt1, i);
1146 tree trial = common_base_type (basetype, tt2);
1149 if (trial == error_mark_node)
1151 if (best == NULL_TREE)
1153 else if (best != trial)
1154 return error_mark_node;
1159 for (i = CLASSTYPE_N_BASECLASSES (tt2)-1; i >= 0; i--)
1161 tree basetype = TYPE_BINFO_BASETYPE (tt2, i);
1162 tree trial = common_base_type (tt1, basetype);
1165 if (trial == error_mark_node)
1167 if (best == NULL_TREE)
1169 else if (best != trial)
1170 return error_mark_node;
1176 /* Subroutines of `comptypes'. */
1178 /* Return true if two parameter type lists PARMS1 and PARMS2 are
1179 equivalent in the sense that functions with those parameter types
1180 can have equivalent types. The two lists must be equivalent,
1181 element by element. */
1184 compparms (tree parms1, tree parms2)
1188 /* An unspecified parmlist matches any specified parmlist
1189 whose argument types don't need default promotions. */
1191 for (t1 = parms1, t2 = parms2;
1193 t1 = TREE_CHAIN (t1), t2 = TREE_CHAIN (t2))
1195 /* If one parmlist is shorter than the other,
1196 they fail to match. */
1199 if (!same_type_p (TREE_VALUE (t1), TREE_VALUE (t2)))
1206 /* Process a sizeof or alignof expression where the operand is a
1210 cxx_sizeof_or_alignof_type (tree type, enum tree_code op, bool complain)
1212 enum tree_code type_code;
1214 const char *op_name;
1216 my_friendly_assert (op == SIZEOF_EXPR || op == ALIGNOF_EXPR, 20020720);
1217 if (type == error_mark_node)
1218 return error_mark_node;
1220 if (processing_template_decl)
1222 value = build_min (op, size_type_node, type);
1223 TREE_READONLY (value) = 1;
1227 op_name = operator_name_info[(int) op].name;
1229 type = non_reference (type);
1230 type_code = TREE_CODE (type);
1232 if (type_code == METHOD_TYPE)
1234 if (complain && (pedantic || warn_pointer_arith))
1235 pedwarn ("invalid application of `%s' to a member function", op_name);
1236 value = size_one_node;
1239 value = c_sizeof_or_alignof_type (complete_type (type), op, complain);
1244 /* Process a sizeof or alignof expression where the operand is an
1248 cxx_sizeof_or_alignof_expr (tree e, enum tree_code op)
1250 const char *op_name = operator_name_info[(int) op].name;
1252 if (e == error_mark_node)
1253 return error_mark_node;
1255 if (processing_template_decl)
1257 e = build_min (op, size_type_node, e);
1258 TREE_SIDE_EFFECTS (e) = 0;
1259 TREE_READONLY (e) = 1;
1264 if (TREE_CODE (e) == COMPONENT_REF
1265 && TREE_CODE (TREE_OPERAND (e, 1)) == FIELD_DECL
1266 && DECL_C_BIT_FIELD (TREE_OPERAND (e, 1)))
1268 error ("invalid application of `%s' to a bit-field", op_name);
1271 else if (is_overloaded_fn (e))
1273 pedwarn ("ISO C++ forbids applying `%s' to an expression of function type", op_name);
1276 else if (type_unknown_p (e))
1278 cxx_incomplete_type_error (e, TREE_TYPE (e));
1284 return cxx_sizeof_or_alignof_type (e, op, true);
1288 /* Perform the conversions in [expr] that apply when an lvalue appears
1289 in an rvalue context: the lvalue-to-rvalue, array-to-pointer, and
1290 function-to-pointer conversions.
1292 In addition manifest constants are replaced by their values. */
1295 decay_conversion (tree exp)
1298 enum tree_code code;
1300 type = TREE_TYPE (exp);
1301 code = TREE_CODE (type);
1303 if (code == REFERENCE_TYPE)
1305 exp = convert_from_reference (exp);
1306 type = TREE_TYPE (exp);
1307 code = TREE_CODE (type);
1310 if (type == error_mark_node)
1311 return error_mark_node;
1313 if (type_unknown_p (exp))
1315 cxx_incomplete_type_error (exp, TREE_TYPE (exp));
1316 return error_mark_node;
1319 /* Constants can be used directly unless they're not loadable. */
1320 if (TREE_CODE (exp) == CONST_DECL)
1321 exp = DECL_INITIAL (exp);
1322 /* Replace a nonvolatile const static variable with its value. We
1323 don't do this for arrays, though; we want the address of the
1324 first element of the array, not the address of the first element
1325 of its initializing constant. */
1326 else if (code != ARRAY_TYPE)
1328 exp = decl_constant_value (exp);
1329 type = TREE_TYPE (exp);
1332 /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
1333 Leave such NOP_EXPRs, since RHS is being used in non-lvalue context. */
1335 if (code == VOID_TYPE)
1337 error ("void value not ignored as it ought to be");
1338 return error_mark_node;
1340 if (code == METHOD_TYPE)
1342 error ("invalid use of non-static member function");
1343 return error_mark_node;
1345 if (code == FUNCTION_TYPE || is_overloaded_fn (exp))
1346 return build_unary_op (ADDR_EXPR, exp, 0);
1347 if (code == ARRAY_TYPE)
1352 if (TREE_CODE (exp) == INDIRECT_REF)
1353 return build_nop (build_pointer_type (TREE_TYPE (type)),
1354 TREE_OPERAND (exp, 0));
1356 if (TREE_CODE (exp) == COMPOUND_EXPR)
1358 tree op1 = decay_conversion (TREE_OPERAND (exp, 1));
1359 return build (COMPOUND_EXPR, TREE_TYPE (op1),
1360 TREE_OPERAND (exp, 0), op1);
1364 && ! (TREE_CODE (exp) == CONSTRUCTOR && TREE_STATIC (exp)))
1366 error ("invalid use of non-lvalue array");
1367 return error_mark_node;
1370 ptrtype = build_pointer_type (TREE_TYPE (type));
1372 if (TREE_CODE (exp) == VAR_DECL)
1374 if (!cxx_mark_addressable (exp))
1375 return error_mark_node;
1376 adr = build_nop (ptrtype, build_address (exp));
1377 TREE_SIDE_EFFECTS (adr) = 0; /* Default would be, same as EXP. */
1380 /* This way is better for a COMPONENT_REF since it can
1381 simplify the offset for a component. */
1382 adr = build_unary_op (ADDR_EXPR, exp, 1);
1383 return cp_convert (ptrtype, adr);
1386 /* [basic.lval]: Class rvalues can have cv-qualified types; non-class
1387 rvalues always have cv-unqualified types. */
1388 if (! CLASS_TYPE_P (type))
1389 exp = cp_convert (TYPE_MAIN_VARIANT (type), exp);
1395 default_conversion (tree exp)
1397 exp = decay_conversion (exp);
1399 if (INTEGRAL_OR_ENUMERATION_TYPE_P (TREE_TYPE (exp)))
1400 exp = perform_integral_promotions (exp);
1405 /* EXPR is an expression with an integral or enumeration type.
1406 Perform the integral promotions in [conv.prom], and return the
1410 perform_integral_promotions (tree expr)
1415 type = TREE_TYPE (expr);
1416 my_friendly_assert (INTEGRAL_OR_ENUMERATION_TYPE_P (type), 20030703);
1417 promoted_type = type_promotes_to (type);
1418 if (type != promoted_type)
1419 expr = cp_convert (promoted_type, expr);
1423 /* Take the address of an inline function without setting TREE_ADDRESSABLE
1427 inline_conversion (tree exp)
1429 if (TREE_CODE (exp) == FUNCTION_DECL)
1430 exp = build1 (ADDR_EXPR, build_pointer_type (TREE_TYPE (exp)), exp);
1435 /* Returns nonzero iff exp is a STRING_CST or the result of applying
1436 decay_conversion to one. */
1439 string_conv_p (tree totype, tree exp, int warn)
1443 if (! flag_const_strings || TREE_CODE (totype) != POINTER_TYPE)
1446 t = TREE_TYPE (totype);
1447 if (!same_type_p (t, char_type_node)
1448 && !same_type_p (t, wchar_type_node))
1451 if (TREE_CODE (exp) == STRING_CST)
1453 /* Make sure that we don't try to convert between char and wchar_t. */
1454 if (!same_type_p (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (exp))), t))
1459 /* Is this a string constant which has decayed to 'const char *'? */
1460 t = build_pointer_type (build_qualified_type (t, TYPE_QUAL_CONST));
1461 if (!same_type_p (TREE_TYPE (exp), t))
1464 if (TREE_CODE (exp) != ADDR_EXPR
1465 || TREE_CODE (TREE_OPERAND (exp, 0)) != STRING_CST)
1469 /* This warning is not very useful, as it complains about printf. */
1470 if (warn && warn_write_strings)
1471 warning ("deprecated conversion from string constant to `%T'", totype);
1476 /* Given a COND_EXPR, MIN_EXPR, or MAX_EXPR in T, return it in a form that we
1477 can, for example, use as an lvalue. This code used to be in
1478 unary_complex_lvalue, but we needed it to deal with `a = (d == c) ? b : c'
1479 expressions, where we're dealing with aggregates. But now it's again only
1480 called from unary_complex_lvalue. The case (in particular) that led to
1481 this was with CODE == ADDR_EXPR, since it's not an lvalue when we'd
1485 rationalize_conditional_expr (enum tree_code code, tree t)
1487 /* For MIN_EXPR or MAX_EXPR, fold-const.c has arranged things so that
1488 the first operand is always the one to be used if both operands
1489 are equal, so we know what conditional expression this used to be. */
1490 if (TREE_CODE (t) == MIN_EXPR || TREE_CODE (t) == MAX_EXPR)
1493 build_conditional_expr (build_x_binary_op ((TREE_CODE (t) == MIN_EXPR
1494 ? LE_EXPR : GE_EXPR),
1495 TREE_OPERAND (t, 0),
1496 TREE_OPERAND (t, 1),
1497 /*overloaded_p=*/NULL),
1498 build_unary_op (code, TREE_OPERAND (t, 0), 0),
1499 build_unary_op (code, TREE_OPERAND (t, 1), 0));
1503 build_conditional_expr (TREE_OPERAND (t, 0),
1504 build_unary_op (code, TREE_OPERAND (t, 1), 0),
1505 build_unary_op (code, TREE_OPERAND (t, 2), 0));
1508 /* Given the TYPE of an anonymous union field inside T, return the
1509 FIELD_DECL for the field. If not found return NULL_TREE. Because
1510 anonymous unions can nest, we must also search all anonymous unions
1511 that are directly reachable. */
1514 lookup_anon_field (tree t, tree type)
1518 for (field = TYPE_FIELDS (t); field; field = TREE_CHAIN (field))
1520 if (TREE_STATIC (field))
1522 if (TREE_CODE (field) != FIELD_DECL || DECL_ARTIFICIAL (field))
1525 /* If we find it directly, return the field. */
1526 if (DECL_NAME (field) == NULL_TREE
1527 && type == TYPE_MAIN_VARIANT (TREE_TYPE (field)))
1532 /* Otherwise, it could be nested, search harder. */
1533 if (DECL_NAME (field) == NULL_TREE
1534 && ANON_AGGR_TYPE_P (TREE_TYPE (field)))
1536 tree subfield = lookup_anon_field (TREE_TYPE (field), type);
1544 /* Build an expression representing OBJECT.MEMBER. OBJECT is an
1545 expression; MEMBER is a DECL or baselink. If ACCESS_PATH is
1546 non-NULL, it indicates the path to the base used to name MEMBER.
1547 If PRESERVE_REFERENCE is true, the expression returned will have
1548 REFERENCE_TYPE if the MEMBER does. Otherwise, the expression
1549 returned will have the type referred to by the reference.
1551 This function does not perform access control; that is either done
1552 earlier by the parser when the name of MEMBER is resolved to MEMBER
1553 itself, or later when overload resolution selects one of the
1554 functions indicated by MEMBER. */
1557 build_class_member_access_expr (tree object, tree member,
1558 tree access_path, bool preserve_reference)
1562 tree result = NULL_TREE;
1564 if (object == error_mark_node || member == error_mark_node)
1565 return error_mark_node;
1567 if (TREE_CODE (member) == PSEUDO_DTOR_EXPR)
1570 my_friendly_assert (DECL_P (member) || BASELINK_P (member),
1575 The type of the first expression shall be "class object" (of a
1577 object_type = TREE_TYPE (object);
1578 if (!currently_open_class (object_type)
1579 && !complete_type_or_else (object_type, object))
1580 return error_mark_node;
1581 if (!CLASS_TYPE_P (object_type))
1583 error ("request for member `%D' in `%E', which is of non-class type `%T'",
1584 member, object, object_type);
1585 return error_mark_node;
1588 /* The standard does not seem to actually say that MEMBER must be a
1589 member of OBJECT_TYPE. However, that is clearly what is
1591 if (DECL_P (member))
1593 member_scope = DECL_CLASS_CONTEXT (member);
1595 if (TREE_DEPRECATED (member))
1596 warn_deprecated_use (member);
1599 member_scope = BINFO_TYPE (BASELINK_BINFO (member));
1600 /* If MEMBER is from an anonymous aggregate, MEMBER_SCOPE will
1601 presently be the anonymous union. Go outwards until we find a
1602 type related to OBJECT_TYPE. */
1603 while (ANON_AGGR_TYPE_P (member_scope)
1604 && !same_type_ignoring_top_level_qualifiers_p (member_scope,
1606 member_scope = TYPE_CONTEXT (member_scope);
1607 if (!member_scope || !DERIVED_FROM_P (member_scope, object_type))
1609 if (TREE_CODE (member) == FIELD_DECL)
1610 error ("invalid use of nonstatic data member '%E'", member);
1612 error ("`%D' is not a member of `%T'", member, object_type);
1613 return error_mark_node;
1616 /* Transform `(a, b).x' into `(*(a, &b)).x', `(a ? b : c).x' into
1617 `(*(a ? &b : &c)).x', and so on. A COND_EXPR is only an lvalue
1618 in the frontend; only _DECLs and _REFs are lvalues in the backend. */
1620 tree temp = unary_complex_lvalue (ADDR_EXPR, object);
1622 object = build_indirect_ref (temp, NULL);
1625 /* In [expr.ref], there is an explicit list of the valid choices for
1626 MEMBER. We check for each of those cases here. */
1627 if (TREE_CODE (member) == VAR_DECL)
1629 /* A static data member. */
1631 /* If OBJECT has side-effects, they are supposed to occur. */
1632 if (TREE_SIDE_EFFECTS (object))
1633 result = build (COMPOUND_EXPR, TREE_TYPE (result), object, result);
1635 else if (TREE_CODE (member) == FIELD_DECL)
1637 /* A non-static data member. */
1642 null_object_p = (TREE_CODE (object) == INDIRECT_REF
1643 && integer_zerop (TREE_OPERAND (object, 0)));
1645 /* Convert OBJECT to the type of MEMBER. */
1646 if (!same_type_p (TYPE_MAIN_VARIANT (object_type),
1647 TYPE_MAIN_VARIANT (member_scope)))
1652 binfo = lookup_base (access_path ? access_path : object_type,
1653 member_scope, ba_ignore, &kind);
1654 if (binfo == error_mark_node)
1655 return error_mark_node;
1657 /* It is invalid to try to get to a virtual base of a
1658 NULL object. The most common cause is invalid use of
1660 if (null_object_p && kind == bk_via_virtual)
1662 error ("invalid access to non-static data member `%D' of NULL object",
1664 error ("(perhaps the `offsetof' macro was used incorrectly)");
1665 return error_mark_node;
1668 /* Convert to the base. */
1669 object = build_base_path (PLUS_EXPR, object, binfo,
1671 /* If we found the base successfully then we should be able
1672 to convert to it successfully. */
1673 my_friendly_assert (object != error_mark_node,
1677 /* Complain about other invalid uses of offsetof, even though they will
1678 give the right answer. Note that we complain whether or not they
1679 actually used the offsetof macro, since there's no way to know at this
1680 point. So we just give a warning, instead of a pedwarn. */
1681 if (null_object_p && warn_invalid_offsetof
1682 && CLASSTYPE_NON_POD_P (object_type))
1684 warning ("invalid access to non-static data member `%D' of NULL object",
1686 warning ("(perhaps the `offsetof' macro was used incorrectly)");
1689 /* If MEMBER is from an anonymous aggregate, we have converted
1690 OBJECT so that it refers to the class containing the
1691 anonymous union. Generate a reference to the anonymous union
1692 itself, and recur to find MEMBER. */
1693 if (ANON_AGGR_TYPE_P (DECL_CONTEXT (member))
1694 /* When this code is called from build_field_call, the
1695 object already has the type of the anonymous union.
1696 That is because the COMPONENT_REF was already
1697 constructed, and was then disassembled before calling
1698 build_field_call. After the function-call code is
1699 cleaned up, this waste can be eliminated. */
1700 && (!same_type_ignoring_top_level_qualifiers_p
1701 (TREE_TYPE (object), DECL_CONTEXT (member))))
1703 tree anonymous_union;
1705 anonymous_union = lookup_anon_field (TREE_TYPE (object),
1706 DECL_CONTEXT (member));
1707 object = build_class_member_access_expr (object,
1709 /*access_path=*/NULL_TREE,
1710 preserve_reference);
1713 /* Compute the type of the field, as described in [expr.ref]. */
1714 type_quals = TYPE_UNQUALIFIED;
1715 member_type = TREE_TYPE (member);
1716 if (TREE_CODE (member_type) != REFERENCE_TYPE)
1718 type_quals = (cp_type_quals (member_type)
1719 | cp_type_quals (object_type));
1721 /* A field is const (volatile) if the enclosing object, or the
1722 field itself, is const (volatile). But, a mutable field is
1723 not const, even within a const object. */
1724 if (DECL_MUTABLE_P (member))
1725 type_quals &= ~TYPE_QUAL_CONST;
1726 member_type = cp_build_qualified_type (member_type, type_quals);
1729 result = fold (build (COMPONENT_REF, member_type, object, member));
1731 /* Mark the expression const or volatile, as appropriate. Even
1732 though we've dealt with the type above, we still have to mark the
1733 expression itself. */
1734 if (type_quals & TYPE_QUAL_CONST)
1735 TREE_READONLY (result) = 1;
1736 else if (type_quals & TYPE_QUAL_VOLATILE)
1737 TREE_THIS_VOLATILE (result) = 1;
1739 else if (BASELINK_P (member))
1741 /* The member is a (possibly overloaded) member function. */
1745 /* If the MEMBER is exactly one static member function, then we
1746 know the type of the expression. Otherwise, we must wait
1747 until overload resolution has been performed. */
1748 functions = BASELINK_FUNCTIONS (member);
1749 if (TREE_CODE (functions) == FUNCTION_DECL
1750 && DECL_STATIC_FUNCTION_P (functions))
1751 type = TREE_TYPE (functions);
1753 type = unknown_type_node;
1754 /* Note that we do not convert OBJECT to the BASELINK_BINFO
1755 base. That will happen when the function is called. */
1756 result = build (COMPONENT_REF, type, object, member);
1758 else if (TREE_CODE (member) == CONST_DECL)
1760 /* The member is an enumerator. */
1762 /* If OBJECT has side-effects, they are supposed to occur. */
1763 if (TREE_SIDE_EFFECTS (object))
1764 result = build (COMPOUND_EXPR, TREE_TYPE (result),
1769 error ("invalid use of `%D'", member);
1770 return error_mark_node;
1773 if (!preserve_reference)
1776 If E2 is declared to have type "reference to T", then ... the
1777 type of E1.E2 is T. */
1778 result = convert_from_reference (result);
1783 /* Return the destructor denoted by OBJECT.SCOPE::~DTOR_NAME, or, if
1784 SCOPE is NULL, by OBJECT.~DTOR_NAME. */
1787 lookup_destructor (tree object, tree scope, tree dtor_name)
1789 tree object_type = TREE_TYPE (object);
1790 tree dtor_type = TREE_OPERAND (dtor_name, 0);
1793 if (scope && !check_dtor_name (scope, dtor_name))
1795 error ("qualified type `%T' does not match destructor name `~%T'",
1797 return error_mark_node;
1799 if (!DERIVED_FROM_P (dtor_type, TYPE_MAIN_VARIANT (object_type)))
1801 error ("the type being destroyed is `%T', but the destructor refers to `%T'",
1802 TYPE_MAIN_VARIANT (object_type), dtor_type);
1803 return error_mark_node;
1805 if (!TYPE_HAS_DESTRUCTOR (dtor_type))
1806 return build (PSEUDO_DTOR_EXPR, void_type_node, object, scope,
1808 expr = lookup_member (dtor_type, complete_dtor_identifier,
1809 /*protect=*/1, /*want_type=*/false);
1810 expr = (adjust_result_of_qualified_name_lookup
1811 (expr, dtor_type, object_type));
1815 /* This function is called by the parser to process a class member
1816 access expression of the form OBJECT.NAME. NAME is a node used by
1817 the parser to represent a name; it is not yet a DECL. It may,
1818 however, be a BASELINK where the BASELINK_FUNCTIONS is a
1819 TEMPLATE_ID_EXPR. Templates must be looked up by the parser, and
1820 there is no reason to do the lookup twice, so the parser keeps the
1824 finish_class_member_access_expr (tree object, tree name)
1829 tree access_path = NULL_TREE;
1830 tree orig_object = object;
1831 tree orig_name = name;
1833 if (object == error_mark_node || name == error_mark_node)
1834 return error_mark_node;
1836 object_type = TREE_TYPE (object);
1838 if (processing_template_decl)
1840 if (/* If OBJECT_TYPE is dependent, so is OBJECT.NAME. */
1841 dependent_type_p (object_type)
1842 /* If NAME is just an IDENTIFIER_NODE, then the expression
1844 || TREE_CODE (object) == IDENTIFIER_NODE
1845 /* If NAME is "f<args>", where either 'f' or 'args' is
1846 dependent, then the expression is dependent. */
1847 || (TREE_CODE (name) == TEMPLATE_ID_EXPR
1848 && dependent_template_id_p (TREE_OPERAND (name, 0),
1849 TREE_OPERAND (name, 1)))
1850 /* If NAME is "T::X" where "T" is dependent, then the
1851 expression is dependent. */
1852 || (TREE_CODE (name) == SCOPE_REF
1853 && TYPE_P (TREE_OPERAND (name, 0))
1854 && dependent_type_p (TREE_OPERAND (name, 0))))
1855 return build_min_nt (COMPONENT_REF, object, name);
1856 object = build_non_dependent_expr (object);
1859 if (TREE_CODE (object_type) == REFERENCE_TYPE)
1861 object = convert_from_reference (object);
1862 object_type = TREE_TYPE (object);
1867 The type of the first expression shall be "class object" (of a
1869 if (!currently_open_class (object_type)
1870 && !complete_type_or_else (object_type, object))
1871 return error_mark_node;
1872 if (!CLASS_TYPE_P (object_type))
1874 error ("request for member `%D' in `%E', which is of non-class type `%T'",
1875 name, object, object_type);
1876 return error_mark_node;
1879 if (BASELINK_P (name))
1881 /* A member function that has already been looked up. */
1882 my_friendly_assert ((TREE_CODE (BASELINK_FUNCTIONS (name))
1883 == TEMPLATE_ID_EXPR),
1889 bool is_template_id = false;
1890 tree template_args = NULL_TREE;
1893 if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
1895 is_template_id = true;
1896 template_args = TREE_OPERAND (name, 1);
1897 name = TREE_OPERAND (name, 0);
1899 if (TREE_CODE (name) == OVERLOAD)
1900 name = DECL_NAME (get_first_fn (name));
1901 else if (DECL_P (name))
1902 name = DECL_NAME (name);
1905 if (TREE_CODE (name) == SCOPE_REF)
1907 /* A qualified name. The qualifying class or namespace `S' has
1908 already been looked up; it is either a TYPE or a
1909 NAMESPACE_DECL. The member name is either an IDENTIFIER_NODE
1910 or a BIT_NOT_EXPR. */
1911 scope = TREE_OPERAND (name, 0);
1912 name = TREE_OPERAND (name, 1);
1913 my_friendly_assert ((CLASS_TYPE_P (scope)
1914 || TREE_CODE (scope) == NAMESPACE_DECL),
1916 my_friendly_assert ((TREE_CODE (name) == IDENTIFIER_NODE
1917 || TREE_CODE (name) == BIT_NOT_EXPR),
1920 /* If SCOPE is a namespace, then the qualified name does not
1921 name a member of OBJECT_TYPE. */
1922 if (TREE_CODE (scope) == NAMESPACE_DECL)
1924 error ("`%D::%D' is not a member of `%T'",
1925 scope, name, object_type);
1926 return error_mark_node;
1929 /* Find the base of OBJECT_TYPE corresponding to SCOPE. */
1930 access_path = lookup_base (object_type, scope, ba_check, NULL);
1931 if (access_path == error_mark_node)
1932 return error_mark_node;
1935 error ("`%T' is not a base of `%T'", scope, object_type);
1936 return error_mark_node;
1942 access_path = object_type;
1945 if (TREE_CODE (name) == BIT_NOT_EXPR)
1946 member = lookup_destructor (object, scope, name);
1949 /* Look up the member. */
1950 member = lookup_member (access_path, name, /*protect=*/1,
1951 /*want_type=*/false);
1952 if (member == NULL_TREE)
1954 error ("'%D' has no member named '%E'", object_type, name);
1955 return error_mark_node;
1957 if (member == error_mark_node)
1958 return error_mark_node;
1963 tree template = member;
1965 if (BASELINK_P (template))
1966 template = lookup_template_function (template, template_args);
1969 error ("`%D' is not a member template function", name);
1970 return error_mark_node;
1975 if (TREE_DEPRECATED (member))
1976 warn_deprecated_use (member);
1978 expr = build_class_member_access_expr (object, member, access_path,
1979 /*preserve_reference=*/false);
1980 if (processing_template_decl && expr != error_mark_node)
1981 return build_min_non_dep (COMPONENT_REF, expr,
1982 orig_object, orig_name);
1986 /* Return an expression for the MEMBER_NAME field in the internal
1987 representation of PTRMEM, a pointer-to-member function. (Each
1988 pointer-to-member function type gets its own RECORD_TYPE so it is
1989 more convenient to access the fields by name than by FIELD_DECL.)
1990 This routine converts the NAME to a FIELD_DECL and then creates the
1991 node for the complete expression. */
1994 build_ptrmemfunc_access_expr (tree ptrmem, tree member_name)
2000 /* This code is a stripped down version of
2001 build_class_member_access_expr. It does not work to use that
2002 routine directly because it expects the object to be of class
2004 ptrmem_type = TREE_TYPE (ptrmem);
2005 my_friendly_assert (TYPE_PTRMEMFUNC_P (ptrmem_type), 20020804);
2006 member = lookup_member (ptrmem_type, member_name, /*protect=*/0,
2007 /*want_type=*/false);
2008 member_type = cp_build_qualified_type (TREE_TYPE (member),
2009 cp_type_quals (ptrmem_type));
2010 return fold (build (COMPONENT_REF, member_type, ptrmem, member));
2013 /* Given an expression PTR for a pointer, return an expression
2014 for the value pointed to.
2015 ERRORSTRING is the name of the operator to appear in error messages.
2017 This function may need to overload OPERATOR_FNNAME.
2018 Must also handle REFERENCE_TYPEs for C++. */
2021 build_x_indirect_ref (tree expr, const char *errorstring)
2023 tree orig_expr = expr;
2026 if (processing_template_decl)
2028 if (type_dependent_expression_p (expr))
2029 return build_min_nt (INDIRECT_REF, expr);
2030 expr = build_non_dependent_expr (expr);
2033 rval = build_new_op (INDIRECT_REF, LOOKUP_NORMAL, expr, NULL_TREE,
2034 NULL_TREE, /*overloaded_p=*/NULL);
2036 rval = build_indirect_ref (expr, errorstring);
2038 if (processing_template_decl && rval != error_mark_node)
2039 return build_min_non_dep (INDIRECT_REF, rval, orig_expr);
2045 build_indirect_ref (tree ptr, const char *errorstring)
2049 if (ptr == error_mark_node)
2050 return error_mark_node;
2052 if (ptr == current_class_ptr)
2053 return current_class_ref;
2055 pointer = (TREE_CODE (TREE_TYPE (ptr)) == REFERENCE_TYPE
2056 ? ptr : decay_conversion (ptr));
2057 type = TREE_TYPE (pointer);
2059 if (TYPE_PTR_P (type) || TREE_CODE (type) == REFERENCE_TYPE)
2063 If the type of the expression is "pointer to T," the type
2064 of the result is "T."
2066 We must use the canonical variant because certain parts of
2067 the back end, like fold, do pointer comparisons between
2069 tree t = canonical_type_variant (TREE_TYPE (type));
2071 if (VOID_TYPE_P (t))
2073 /* A pointer to incomplete type (other than cv void) can be
2074 dereferenced [expr.unary.op]/1 */
2075 error ("`%T' is not a pointer-to-object type", type);
2076 return error_mark_node;
2078 else if (TREE_CODE (pointer) == ADDR_EXPR
2079 && same_type_p (t, TREE_TYPE (TREE_OPERAND (pointer, 0))))
2080 /* The POINTER was something like `&x'. We simplify `*&x' to
2082 return TREE_OPERAND (pointer, 0);
2085 tree ref = build1 (INDIRECT_REF, t, pointer);
2087 /* We *must* set TREE_READONLY when dereferencing a pointer to const,
2088 so that we get the proper error message if the result is used
2089 to assign to. Also, &* is supposed to be a no-op. */
2090 TREE_READONLY (ref) = CP_TYPE_CONST_P (t);
2091 TREE_THIS_VOLATILE (ref) = CP_TYPE_VOLATILE_P (t);
2092 TREE_SIDE_EFFECTS (ref)
2093 = (TREE_THIS_VOLATILE (ref) || TREE_SIDE_EFFECTS (pointer));
2097 /* `pointer' won't be an error_mark_node if we were given a
2098 pointer to member, so it's cool to check for this here. */
2099 else if (TYPE_PTR_TO_MEMBER_P (type))
2100 error ("invalid use of `%s' on pointer to member", errorstring);
2101 else if (pointer != error_mark_node)
2104 error ("invalid type argument of `%s'", errorstring);
2106 error ("invalid type argument");
2108 return error_mark_node;
2111 /* This handles expressions of the form "a[i]", which denotes
2114 This is logically equivalent in C to *(a+i), but we may do it differently.
2115 If A is a variable or a member, we generate a primitive ARRAY_REF.
2116 This avoids forcing the array out of registers, and can work on
2117 arrays that are not lvalues (for example, members of structures returned
2120 If INDEX is of some user-defined type, it must be converted to
2121 integer type. Otherwise, to make a compatible PLUS_EXPR, it
2122 will inherit the type of the array, which will be some pointer type. */
2125 build_array_ref (tree array, tree idx)
2129 error ("subscript missing in array reference");
2130 return error_mark_node;
2133 if (TREE_TYPE (array) == error_mark_node
2134 || TREE_TYPE (idx) == error_mark_node)
2135 return error_mark_node;
2137 /* If ARRAY is a COMPOUND_EXPR or COND_EXPR, move our reference
2139 switch (TREE_CODE (array))
2143 tree value = build_array_ref (TREE_OPERAND (array, 1), idx);
2144 return build (COMPOUND_EXPR, TREE_TYPE (value),
2145 TREE_OPERAND (array, 0), value);
2149 return build_conditional_expr
2150 (TREE_OPERAND (array, 0),
2151 build_array_ref (TREE_OPERAND (array, 1), idx),
2152 build_array_ref (TREE_OPERAND (array, 2), idx));
2158 if (TREE_CODE (TREE_TYPE (array)) == ARRAY_TYPE
2159 && TREE_CODE (array) != INDIRECT_REF)
2163 /* Subscripting with type char is likely to lose
2164 on a machine where chars are signed.
2165 So warn on any machine, but optionally.
2166 Don't warn for unsigned char since that type is safe.
2167 Don't warn for signed char because anyone who uses that
2168 must have done so deliberately. */
2169 if (warn_char_subscripts
2170 && TYPE_MAIN_VARIANT (TREE_TYPE (idx)) == char_type_node)
2171 warning ("array subscript has type `char'");
2173 if (!INTEGRAL_OR_ENUMERATION_TYPE_P (TREE_TYPE (idx)))
2175 error ("array subscript is not an integer");
2176 return error_mark_node;
2179 /* Apply integral promotions *after* noticing character types.
2180 (It is unclear why we do these promotions -- the standard
2181 does not say that we should. In fact, the natual thing would
2182 seem to be to convert IDX to ptrdiff_t; we're performing
2183 pointer arithmetic.) */
2184 idx = perform_integral_promotions (idx);
2186 /* An array that is indexed by a non-constant
2187 cannot be stored in a register; we must be able to do
2188 address arithmetic on its address.
2189 Likewise an array of elements of variable size. */
2190 if (TREE_CODE (idx) != INTEGER_CST
2191 || (COMPLETE_TYPE_P (TREE_TYPE (TREE_TYPE (array)))
2192 && (TREE_CODE (TYPE_SIZE (TREE_TYPE (TREE_TYPE (array))))
2195 if (!cxx_mark_addressable (array))
2196 return error_mark_node;
2199 /* An array that is indexed by a constant value which is not within
2200 the array bounds cannot be stored in a register either; because we
2201 would get a crash in store_bit_field/extract_bit_field when trying
2202 to access a non-existent part of the register. */
2203 if (TREE_CODE (idx) == INTEGER_CST
2204 && TYPE_DOMAIN (TREE_TYPE (array))
2205 && ! int_fits_type_p (idx, TYPE_DOMAIN (TREE_TYPE (array))))
2207 if (!cxx_mark_addressable (array))
2208 return error_mark_node;
2211 if (pedantic && !lvalue_p (array))
2212 pedwarn ("ISO C++ forbids subscripting non-lvalue array");
2214 /* Note in C++ it is valid to subscript a `register' array, since
2215 it is valid to take the address of something with that
2216 storage specification. */
2220 while (TREE_CODE (foo) == COMPONENT_REF)
2221 foo = TREE_OPERAND (foo, 0);
2222 if (TREE_CODE (foo) == VAR_DECL && DECL_REGISTER (foo))
2223 warning ("subscripting array declared `register'");
2226 type = TREE_TYPE (TREE_TYPE (array));
2227 rval = build (ARRAY_REF, type, array, idx);
2228 /* Array ref is const/volatile if the array elements are
2229 or if the array is.. */
2230 TREE_READONLY (rval)
2231 |= (CP_TYPE_CONST_P (type) | TREE_READONLY (array));
2232 TREE_SIDE_EFFECTS (rval)
2233 |= (CP_TYPE_VOLATILE_P (type) | TREE_SIDE_EFFECTS (array));
2234 TREE_THIS_VOLATILE (rval)
2235 |= (CP_TYPE_VOLATILE_P (type) | TREE_THIS_VOLATILE (array));
2236 return require_complete_type (fold (rval));
2240 tree ar = default_conversion (array);
2241 tree ind = default_conversion (idx);
2243 /* Put the integer in IND to simplify error checking. */
2244 if (TREE_CODE (TREE_TYPE (ar)) == INTEGER_TYPE)
2251 if (ar == error_mark_node)
2254 if (TREE_CODE (TREE_TYPE (ar)) != POINTER_TYPE)
2256 error ("subscripted value is neither array nor pointer");
2257 return error_mark_node;
2259 if (TREE_CODE (TREE_TYPE (ind)) != INTEGER_TYPE)
2261 error ("array subscript is not an integer");
2262 return error_mark_node;
2265 return build_indirect_ref (cp_build_binary_op (PLUS_EXPR, ar, ind),
2270 /* Resolve a pointer to member function. INSTANCE is the object
2271 instance to use, if the member points to a virtual member.
2273 This used to avoid checking for virtual functions if basetype
2274 has no virtual functions, according to an earlier ANSI draft.
2275 With the final ISO C++ rules, such an optimization is
2276 incorrect: A pointer to a derived member can be static_cast
2277 to pointer-to-base-member, as long as the dynamic object
2278 later has the right member. */
2281 get_member_function_from_ptrfunc (tree *instance_ptrptr, tree function)
2283 if (TREE_CODE (function) == OFFSET_REF)
2284 function = TREE_OPERAND (function, 1);
2286 if (TYPE_PTRMEMFUNC_P (TREE_TYPE (function)))
2288 tree idx, delta, e1, e2, e3, vtbl, basetype;
2289 tree fntype = TYPE_PTRMEMFUNC_FN_TYPE (TREE_TYPE (function));
2291 tree instance_ptr = *instance_ptrptr;
2292 tree instance_save_expr = 0;
2293 if (instance_ptr == error_mark_node)
2295 if (TREE_CODE (function) == PTRMEM_CST)
2297 /* Extracting the function address from a pmf is only
2298 allowed with -Wno-pmf-conversions. It only works for
2300 e1 = build_addr_func (PTRMEM_CST_MEMBER (function));
2301 e1 = convert (fntype, e1);
2306 error ("object missing in use of `%E'", function);
2307 return error_mark_node;
2311 if (TREE_SIDE_EFFECTS (instance_ptr))
2312 instance_ptr = instance_save_expr = save_expr (instance_ptr);
2314 if (TREE_SIDE_EFFECTS (function))
2315 function = save_expr (function);
2317 /* Start by extracting all the information from the PMF itself. */
2318 e3 = PFN_FROM_PTRMEMFUNC (function);
2319 delta = build_ptrmemfunc_access_expr (function, delta_identifier);
2320 idx = build1 (NOP_EXPR, vtable_index_type, e3);
2321 switch (TARGET_PTRMEMFUNC_VBIT_LOCATION)
2323 case ptrmemfunc_vbit_in_pfn:
2324 e1 = cp_build_binary_op (BIT_AND_EXPR, idx, integer_one_node);
2325 idx = cp_build_binary_op (MINUS_EXPR, idx, integer_one_node);
2328 case ptrmemfunc_vbit_in_delta:
2329 e1 = cp_build_binary_op (BIT_AND_EXPR, delta, integer_one_node);
2330 delta = cp_build_binary_op (RSHIFT_EXPR, delta, integer_one_node);
2337 /* Convert down to the right base before using the instance. First
2339 basetype = TYPE_METHOD_BASETYPE (TREE_TYPE (fntype));
2340 basetype = lookup_base (TREE_TYPE (TREE_TYPE (instance_ptr)),
2341 basetype, ba_check, NULL);
2342 instance_ptr = build_base_path (PLUS_EXPR, instance_ptr, basetype, 1);
2343 if (instance_ptr == error_mark_node)
2344 return error_mark_node;
2345 /* ...and then the delta in the PMF. */
2346 instance_ptr = build (PLUS_EXPR, TREE_TYPE (instance_ptr),
2347 instance_ptr, delta);
2349 /* Hand back the adjusted 'this' argument to our caller. */
2350 *instance_ptrptr = instance_ptr;
2352 /* Next extract the vtable pointer from the object. */
2353 vtbl = build1 (NOP_EXPR, build_pointer_type (vtbl_ptr_type_node),
2355 vtbl = build_indirect_ref (vtbl, NULL);
2357 /* Finally, extract the function pointer from the vtable. */
2358 e2 = fold (build (PLUS_EXPR, TREE_TYPE (vtbl), vtbl, idx));
2359 e2 = build_indirect_ref (e2, NULL);
2360 TREE_CONSTANT (e2) = 1;
2362 /* When using function descriptors, the address of the
2363 vtable entry is treated as a function pointer. */
2364 if (TARGET_VTABLE_USES_DESCRIPTORS)
2365 e2 = build1 (NOP_EXPR, TREE_TYPE (e2),
2366 build_unary_op (ADDR_EXPR, e2, /*noconvert=*/1));
2368 TREE_TYPE (e2) = TREE_TYPE (e3);
2369 e1 = build_conditional_expr (e1, e2, e3);
2371 /* Make sure this doesn't get evaluated first inside one of the
2372 branches of the COND_EXPR. */
2373 if (instance_save_expr)
2374 e1 = build (COMPOUND_EXPR, TREE_TYPE (e1),
2375 instance_save_expr, e1);
2383 build_function_call (tree function, tree params)
2385 tree fntype, fndecl;
2386 tree coerced_params;
2388 tree name = NULL_TREE;
2390 tree original = function;
2392 /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
2393 Strip such NOP_EXPRs, since FUNCTION is used in non-lvalue context. */
2394 if (TREE_CODE (function) == NOP_EXPR
2395 && TREE_TYPE (function) == TREE_TYPE (TREE_OPERAND (function, 0)))
2396 function = TREE_OPERAND (function, 0);
2398 if (TREE_CODE (function) == FUNCTION_DECL)
2400 name = DECL_NAME (function);
2402 mark_used (function);
2405 /* Convert anything with function type to a pointer-to-function. */
2406 if (pedantic && DECL_MAIN_P (function))
2407 pedwarn ("ISO C++ forbids calling `::main' from within program");
2409 /* Differs from default_conversion by not setting TREE_ADDRESSABLE
2410 (because calling an inline function does not mean the function
2411 needs to be separately compiled). */
2413 if (DECL_INLINE (function))
2414 function = inline_conversion (function);
2416 function = build_addr_func (function);
2422 function = build_addr_func (function);
2425 if (function == error_mark_node)
2426 return error_mark_node;
2428 fntype = TREE_TYPE (function);
2430 if (TYPE_PTRMEMFUNC_P (fntype))
2432 error ("must use .* or ->* to call pointer-to-member function in `%E (...)'",
2434 return error_mark_node;
2437 is_method = (TREE_CODE (fntype) == POINTER_TYPE
2438 && TREE_CODE (TREE_TYPE (fntype)) == METHOD_TYPE);
2440 if (!((TREE_CODE (fntype) == POINTER_TYPE
2441 && TREE_CODE (TREE_TYPE (fntype)) == FUNCTION_TYPE)
2443 || TREE_CODE (function) == TEMPLATE_ID_EXPR))
2445 error ("`%E' cannot be used as a function", original);
2446 return error_mark_node;
2449 /* fntype now gets the type of function pointed to. */
2450 fntype = TREE_TYPE (fntype);
2452 /* Convert the parameters to the types declared in the
2453 function prototype, or apply default promotions. */
2455 coerced_params = convert_arguments (TYPE_ARG_TYPES (fntype),
2456 params, fndecl, LOOKUP_NORMAL);
2457 if (coerced_params == error_mark_node)
2458 return error_mark_node;
2460 /* Check for errors in format strings. */
2463 check_function_format (NULL, TYPE_ATTRIBUTES (fntype), coerced_params);
2465 /* Recognize certain built-in functions so we can make tree-codes
2466 other than CALL_EXPR. We do this when it enables fold-const.c
2467 to do something useful. */
2469 if (TREE_CODE (function) == ADDR_EXPR
2470 && TREE_CODE (TREE_OPERAND (function, 0)) == FUNCTION_DECL
2471 && DECL_BUILT_IN (TREE_OPERAND (function, 0)))
2473 result = expand_tree_builtin (TREE_OPERAND (function, 0),
2474 params, coerced_params);
2479 return build_cxx_call (function, params, coerced_params);
2482 /* Convert the actual parameter expressions in the list VALUES
2483 to the types in the list TYPELIST.
2484 If parmdecls is exhausted, or when an element has NULL as its type,
2485 perform the default conversions.
2487 NAME is an IDENTIFIER_NODE or 0. It is used only for error messages.
2489 This is also where warnings about wrong number of args are generated.
2491 Return a list of expressions for the parameters as converted.
2493 Both VALUES and the returned value are chains of TREE_LIST nodes
2494 with the elements of the list in the TREE_VALUE slots of those nodes.
2496 In C++, unspecified trailing parameters can be filled in with their
2497 default arguments, if such were specified. Do so here. */
2500 convert_arguments (tree typelist, tree values, tree fndecl, int flags)
2502 tree typetail, valtail;
2503 tree result = NULL_TREE;
2504 const char *called_thing = 0;
2507 /* Argument passing is always copy-initialization. */
2508 flags |= LOOKUP_ONLYCONVERTING;
2512 if (TREE_CODE (TREE_TYPE (fndecl)) == METHOD_TYPE)
2514 if (DECL_NAME (fndecl) == NULL_TREE
2515 || IDENTIFIER_HAS_TYPE_VALUE (DECL_NAME (fndecl)))
2516 called_thing = "constructor";
2518 called_thing = "member function";
2521 called_thing = "function";
2524 for (valtail = values, typetail = typelist;
2526 valtail = TREE_CHAIN (valtail), i++)
2528 tree type = typetail ? TREE_VALUE (typetail) : 0;
2529 tree val = TREE_VALUE (valtail);
2531 if (val == error_mark_node)
2532 return error_mark_node;
2534 if (type == void_type_node)
2538 cp_error_at ("too many arguments to %s `%+#D'", called_thing,
2540 error ("at this point in file");
2543 error ("too many arguments to function");
2544 /* In case anybody wants to know if this argument
2547 TREE_TYPE (tree_last (result)) = error_mark_node;
2551 /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
2552 Strip such NOP_EXPRs, since VAL is used in non-lvalue context. */
2553 if (TREE_CODE (val) == NOP_EXPR
2554 && TREE_TYPE (val) == TREE_TYPE (TREE_OPERAND (val, 0))
2555 && (type == 0 || TREE_CODE (type) != REFERENCE_TYPE))
2556 val = TREE_OPERAND (val, 0);
2558 if (type == 0 || TREE_CODE (type) != REFERENCE_TYPE)
2560 if (TREE_CODE (TREE_TYPE (val)) == ARRAY_TYPE
2561 || TREE_CODE (TREE_TYPE (val)) == FUNCTION_TYPE
2562 || TREE_CODE (TREE_TYPE (val)) == METHOD_TYPE)
2563 val = decay_conversion (val);
2566 if (val == error_mark_node)
2567 return error_mark_node;
2571 /* Formal parm type is specified by a function prototype. */
2574 if (!COMPLETE_TYPE_P (complete_type (type)))
2577 error ("parameter %P of `%D' has incomplete type `%T'",
2580 error ("parameter %P has incomplete type `%T'", i, type);
2581 parmval = error_mark_node;
2585 parmval = convert_for_initialization
2586 (NULL_TREE, type, val, flags,
2587 "argument passing", fndecl, i);
2588 parmval = convert_for_arg_passing (type, parmval);
2591 if (parmval == error_mark_node)
2592 return error_mark_node;
2594 result = tree_cons (NULL_TREE, parmval, result);
2598 if (TREE_CODE (TREE_TYPE (val)) == REFERENCE_TYPE)
2599 val = convert_from_reference (val);
2601 if (fndecl && DECL_BUILT_IN (fndecl)
2602 && DECL_FUNCTION_CODE (fndecl) == BUILT_IN_CONSTANT_P)
2603 /* Don't do ellipsis conversion for __built_in_constant_p
2604 as this will result in spurious warnings for non-POD
2606 val = require_complete_type (val);
2608 val = convert_arg_to_ellipsis (val);
2610 result = tree_cons (NULL_TREE, val, result);
2614 typetail = TREE_CHAIN (typetail);
2617 if (typetail != 0 && typetail != void_list_node)
2619 /* See if there are default arguments that can be used. */
2620 if (TREE_PURPOSE (typetail)
2621 && TREE_CODE (TREE_PURPOSE (typetail)) != DEFAULT_ARG)
2623 for (; typetail != void_list_node; ++i)
2626 = convert_default_arg (TREE_VALUE (typetail),
2627 TREE_PURPOSE (typetail),
2630 if (parmval == error_mark_node)
2631 return error_mark_node;
2633 result = tree_cons (0, parmval, result);
2634 typetail = TREE_CHAIN (typetail);
2635 /* ends with `...'. */
2636 if (typetail == NULL_TREE)
2644 cp_error_at ("too few arguments to %s `%+#D'",
2645 called_thing, fndecl);
2646 error ("at this point in file");
2649 error ("too few arguments to function");
2650 return error_mark_list;
2654 return nreverse (result);
2657 /* Build a binary-operation expression, after performing default
2658 conversions on the operands. CODE is the kind of expression to build. */
2661 build_x_binary_op (enum tree_code code, tree arg1, tree arg2,
2671 if (processing_template_decl)
2673 if (type_dependent_expression_p (arg1)
2674 || type_dependent_expression_p (arg2))
2675 return build_min_nt (code, arg1, arg2);
2676 arg1 = build_non_dependent_expr (arg1);
2677 arg2 = build_non_dependent_expr (arg2);
2680 if (code == DOTSTAR_EXPR)
2681 expr = build_m_component_ref (arg1, arg2);
2683 expr = build_new_op (code, LOOKUP_NORMAL, arg1, arg2, NULL_TREE,
2686 if (processing_template_decl && expr != error_mark_node)
2687 return build_min_non_dep (code, expr, orig_arg1, orig_arg2);
2692 /* Build a binary-operation expression without default conversions.
2693 CODE is the kind of expression to build.
2694 This function differs from `build' in several ways:
2695 the data type of the result is computed and recorded in it,
2696 warnings are generated if arg data types are invalid,
2697 special handling for addition and subtraction of pointers is known,
2698 and some optimization is done (operations on narrow ints
2699 are done in the narrower type when that gives the same result).
2700 Constant folding is also done before the result is returned.
2702 Note that the operands will never have enumeral types
2703 because either they have just had the default conversions performed
2704 or they have both just been converted to some other type in which
2705 the arithmetic is to be done.
2707 C++: must do special pointer arithmetic when implementing
2708 multiple inheritance, and deal with pointer to member functions. */
2711 build_binary_op (enum tree_code code, tree orig_op0, tree orig_op1,
2712 int convert_p ATTRIBUTE_UNUSED)
2715 enum tree_code code0, code1;
2718 /* Expression code to give to the expression when it is built.
2719 Normally this is CODE, which is what the caller asked for,
2720 but in some special cases we change it. */
2721 enum tree_code resultcode = code;
2723 /* Data type in which the computation is to be performed.
2724 In the simplest cases this is the common type of the arguments. */
2725 tree result_type = NULL;
2727 /* Nonzero means operands have already been type-converted
2728 in whatever way is necessary.
2729 Zero means they need to be converted to RESULT_TYPE. */
2732 /* Nonzero means create the expression with this type, rather than
2734 tree build_type = 0;
2736 /* Nonzero means after finally constructing the expression
2737 convert it to this type. */
2738 tree final_type = 0;
2740 /* Nonzero if this is an operation like MIN or MAX which can
2741 safely be computed in short if both args are promoted shorts.
2742 Also implies COMMON.
2743 -1 indicates a bitwise operation; this makes a difference
2744 in the exact conditions for when it is safe to do the operation
2745 in a narrower mode. */
2748 /* Nonzero if this is a comparison operation;
2749 if both args are promoted shorts, compare the original shorts.
2750 Also implies COMMON. */
2751 int short_compare = 0;
2753 /* Nonzero if this is a right-shift operation, which can be computed on the
2754 original short and then promoted if the operand is a promoted short. */
2755 int short_shift = 0;
2757 /* Nonzero means set RESULT_TYPE to the common type of the args. */
2760 /* Apply default conversions. */
2764 if (code == TRUTH_AND_EXPR || code == TRUTH_ANDIF_EXPR
2765 || code == TRUTH_OR_EXPR || code == TRUTH_ORIF_EXPR
2766 || code == TRUTH_XOR_EXPR)
2768 if (!really_overloaded_fn (op0))
2769 op0 = decay_conversion (op0);
2770 if (!really_overloaded_fn (op1))
2771 op1 = decay_conversion (op1);
2775 if (!really_overloaded_fn (op0))
2776 op0 = default_conversion (op0);
2777 if (!really_overloaded_fn (op1))
2778 op1 = default_conversion (op1);
2781 /* Strip NON_LVALUE_EXPRs, etc., since we aren't using as an lvalue. */
2782 STRIP_TYPE_NOPS (op0);
2783 STRIP_TYPE_NOPS (op1);
2785 /* DTRT if one side is an overloaded function, but complain about it. */
2786 if (type_unknown_p (op0))
2788 tree t = instantiate_type (TREE_TYPE (op1), op0, tf_none);
2789 if (t != error_mark_node)
2791 pedwarn ("assuming cast to type `%T' from overloaded function",
2796 if (type_unknown_p (op1))
2798 tree t = instantiate_type (TREE_TYPE (op0), op1, tf_none);
2799 if (t != error_mark_node)
2801 pedwarn ("assuming cast to type `%T' from overloaded function",
2807 type0 = TREE_TYPE (op0);
2808 type1 = TREE_TYPE (op1);
2810 /* The expression codes of the data types of the arguments tell us
2811 whether the arguments are integers, floating, pointers, etc. */
2812 code0 = TREE_CODE (type0);
2813 code1 = TREE_CODE (type1);
2815 /* If an error was already reported for one of the arguments,
2816 avoid reporting another error. */
2818 if (code0 == ERROR_MARK || code1 == ERROR_MARK)
2819 return error_mark_node;
2824 /* Handle the pointer + int case. */
2825 if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
2826 return cp_pointer_int_sum (PLUS_EXPR, op0, op1);
2827 else if (code1 == POINTER_TYPE && code0 == INTEGER_TYPE)
2828 return cp_pointer_int_sum (PLUS_EXPR, op1, op0);
2834 /* Subtraction of two similar pointers.
2835 We must subtract them as integers, then divide by object size. */
2836 if (code0 == POINTER_TYPE && code1 == POINTER_TYPE
2837 && same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (type0),
2839 return pointer_diff (op0, op1, common_type (type0, type1));
2840 /* Handle pointer minus int. Just like pointer plus int. */
2841 else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
2842 return cp_pointer_int_sum (MINUS_EXPR, op0, op1);
2851 case TRUNC_DIV_EXPR:
2853 case FLOOR_DIV_EXPR:
2854 case ROUND_DIV_EXPR:
2855 case EXACT_DIV_EXPR:
2856 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE
2857 || code0 == COMPLEX_TYPE)
2858 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
2859 || code1 == COMPLEX_TYPE))
2861 if (TREE_CODE (op1) == INTEGER_CST && integer_zerop (op1))
2862 warning ("division by zero in `%E / 0'", op0);
2863 else if (TREE_CODE (op1) == REAL_CST && real_zerop (op1))
2864 warning ("division by zero in `%E / 0.'", op0);
2866 if (!(code0 == INTEGER_TYPE && code1 == INTEGER_TYPE))
2867 resultcode = RDIV_EXPR;
2869 /* When dividing two signed integers, we have to promote to int.
2870 unless we divide by a constant != -1. Note that default
2871 conversion will have been performed on the operands at this
2872 point, so we have to dig out the original type to find out if
2874 shorten = ((TREE_CODE (op0) == NOP_EXPR
2875 && TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (op0, 0))))
2876 || (TREE_CODE (op1) == INTEGER_CST
2877 && ! integer_all_onesp (op1)));
2886 if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
2890 case TRUNC_MOD_EXPR:
2891 case FLOOR_MOD_EXPR:
2892 if (code1 == INTEGER_TYPE && integer_zerop (op1))
2893 warning ("division by zero in `%E %% 0'", op0);
2894 else if (code1 == REAL_TYPE && real_zerop (op1))
2895 warning ("division by zero in `%E %% 0.'", op0);
2897 if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
2899 /* Although it would be tempting to shorten always here, that loses
2900 on some targets, since the modulo instruction is undefined if the
2901 quotient can't be represented in the computation mode. We shorten
2902 only if unsigned or if dividing by something we know != -1. */
2903 shorten = ((TREE_CODE (op0) == NOP_EXPR
2904 && TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (op0, 0))))
2905 || (TREE_CODE (op1) == INTEGER_CST
2906 && ! integer_all_onesp (op1)));
2911 case TRUTH_ANDIF_EXPR:
2912 case TRUTH_ORIF_EXPR:
2913 case TRUTH_AND_EXPR:
2915 result_type = boolean_type_node;
2918 /* Shift operations: result has same type as first operand;
2919 always convert second operand to int.
2920 Also set SHORT_SHIFT if shifting rightward. */
2923 if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
2925 result_type = type0;
2926 if (TREE_CODE (op1) == INTEGER_CST)
2928 if (tree_int_cst_lt (op1, integer_zero_node))
2929 warning ("right shift count is negative");
2932 if (! integer_zerop (op1))
2934 if (compare_tree_int (op1, TYPE_PRECISION (type0)) >= 0)
2935 warning ("right shift count >= width of type");
2938 /* Convert the shift-count to an integer, regardless of
2939 size of value being shifted. */
2940 if (TYPE_MAIN_VARIANT (TREE_TYPE (op1)) != integer_type_node)
2941 op1 = cp_convert (integer_type_node, op1);
2942 /* Avoid converting op1 to result_type later. */
2948 if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
2950 result_type = type0;
2951 if (TREE_CODE (op1) == INTEGER_CST)
2953 if (tree_int_cst_lt (op1, integer_zero_node))
2954 warning ("left shift count is negative");
2955 else if (compare_tree_int (op1, TYPE_PRECISION (type0)) >= 0)
2956 warning ("left shift count >= width of type");
2958 /* Convert the shift-count to an integer, regardless of
2959 size of value being shifted. */
2960 if (TYPE_MAIN_VARIANT (TREE_TYPE (op1)) != integer_type_node)
2961 op1 = cp_convert (integer_type_node, op1);
2962 /* Avoid converting op1 to result_type later. */
2969 if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
2971 result_type = type0;
2972 if (TREE_CODE (op1) == INTEGER_CST)
2974 if (tree_int_cst_lt (op1, integer_zero_node))
2975 warning ("%s rotate count is negative",
2976 (code == LROTATE_EXPR) ? "left" : "right");
2977 else if (compare_tree_int (op1, TYPE_PRECISION (type0)) >= 0)
2978 warning ("%s rotate count >= width of type",
2979 (code == LROTATE_EXPR) ? "left" : "right");
2981 /* Convert the shift-count to an integer, regardless of
2982 size of value being shifted. */
2983 if (TYPE_MAIN_VARIANT (TREE_TYPE (op1)) != integer_type_node)
2984 op1 = cp_convert (integer_type_node, op1);
2990 if (warn_float_equal && (code0 == REAL_TYPE || code1 == REAL_TYPE))
2991 warning ("comparing floating point with == or != is unsafe");
2993 build_type = boolean_type_node;
2994 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE
2995 || code0 == COMPLEX_TYPE)
2996 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
2997 || code1 == COMPLEX_TYPE))
2999 else if ((code0 == POINTER_TYPE && code1 == POINTER_TYPE)
3000 || (TYPE_PTRMEM_P (type0) && TYPE_PTRMEM_P (type1)))
3001 result_type = composite_pointer_type (type0, type1, op0, op1,
3003 else if ((code0 == POINTER_TYPE || TYPE_PTRMEM_P (type0))
3004 && null_ptr_cst_p (op1))
3005 result_type = type0;
3006 else if ((code1 == POINTER_TYPE || TYPE_PTRMEM_P (type1))
3007 && null_ptr_cst_p (op0))
3008 result_type = type1;
3009 else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
3011 result_type = type0;
3012 error ("ISO C++ forbids comparison between pointer and integer");
3014 else if (code0 == INTEGER_TYPE && code1 == POINTER_TYPE)
3016 result_type = type1;
3017 error ("ISO C++ forbids comparison between pointer and integer");
3019 else if (TYPE_PTRMEMFUNC_P (type0) && null_ptr_cst_p (op1))
3021 op0 = build_ptrmemfunc_access_expr (op0, pfn_identifier);
3022 op1 = cp_convert (TREE_TYPE (op0), integer_zero_node);
3023 result_type = TREE_TYPE (op0);
3025 else if (TYPE_PTRMEMFUNC_P (type1) && null_ptr_cst_p (op0))
3026 return cp_build_binary_op (code, op1, op0);
3027 else if (TYPE_PTRMEMFUNC_P (type0) && TYPE_PTRMEMFUNC_P (type1)
3028 && same_type_p (type0, type1))
3030 /* E will be the final comparison. */
3032 /* E1 and E2 are for scratch. */
3040 if (TREE_SIDE_EFFECTS (op0))
3041 op0 = save_expr (op0);
3042 if (TREE_SIDE_EFFECTS (op1))
3043 op1 = save_expr (op1);
3048 && (!op0.pfn || op0.delta == op1.delta))
3050 The reason for the `!op0.pfn' bit is that a NULL
3051 pointer-to-member is any member with a zero PFN; the
3052 DELTA field is unspecified. */
3053 pfn0 = pfn_from_ptrmemfunc (op0);
3054 pfn1 = pfn_from_ptrmemfunc (op1);
3055 delta0 = build_ptrmemfunc_access_expr (op0,
3057 delta1 = build_ptrmemfunc_access_expr (op1,
3059 e1 = cp_build_binary_op (EQ_EXPR, delta0, delta1);
3060 e2 = cp_build_binary_op (EQ_EXPR,
3062 cp_convert (TREE_TYPE (pfn0),
3063 integer_zero_node));
3064 e1 = cp_build_binary_op (TRUTH_ORIF_EXPR, e1, e2);
3065 e2 = build (EQ_EXPR, boolean_type_node, pfn0, pfn1);
3066 e = cp_build_binary_op (TRUTH_ANDIF_EXPR, e2, e1);
3067 if (code == EQ_EXPR)
3069 return cp_build_binary_op (EQ_EXPR, e, integer_zero_node);
3071 else if ((TYPE_PTRMEMFUNC_P (type0)
3072 && same_type_p (TYPE_PTRMEMFUNC_FN_TYPE (type0), type1))
3073 || (TYPE_PTRMEMFUNC_P (type1)
3074 && same_type_p (TYPE_PTRMEMFUNC_FN_TYPE (type1), type0)))
3080 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE)
3081 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE))
3083 else if (code0 == POINTER_TYPE && code1 == POINTER_TYPE)
3084 result_type = composite_pointer_type (type0, type1, op0, op1,
3092 build_type = boolean_type_node;
3093 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE)
3094 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE))
3096 else if (code0 == POINTER_TYPE && code1 == POINTER_TYPE)
3097 result_type = composite_pointer_type (type0, type1, op0, op1,
3099 else if (code0 == POINTER_TYPE && TREE_CODE (op1) == INTEGER_CST
3100 && integer_zerop (op1))
3101 result_type = type0;
3102 else if (code1 == POINTER_TYPE && TREE_CODE (op0) == INTEGER_CST
3103 && integer_zerop (op0))
3104 result_type = type1;
3105 else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
3107 result_type = type0;
3108 pedwarn ("ISO C++ forbids comparison between pointer and integer");
3110 else if (code0 == INTEGER_TYPE && code1 == POINTER_TYPE)
3112 result_type = type1;
3113 pedwarn ("ISO C++ forbids comparison between pointer and integer");
3117 case UNORDERED_EXPR:
3124 build_type = integer_type_node;
3125 if (code0 != REAL_TYPE || code1 != REAL_TYPE)
3127 error ("unordered comparison on non-floating point argument");
3128 return error_mark_node;
3137 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE || code0 == COMPLEX_TYPE)
3139 (code1 == INTEGER_TYPE || code1 == REAL_TYPE || code1 == COMPLEX_TYPE))
3141 int none_complex = (code0 != COMPLEX_TYPE && code1 != COMPLEX_TYPE);
3143 if (shorten || common || short_compare)
3144 result_type = common_type (type0, type1);
3146 /* For certain operations (which identify themselves by shorten != 0)
3147 if both args were extended from the same smaller type,
3148 do the arithmetic in that type and then extend.
3150 shorten !=0 and !=1 indicates a bitwise operation.
3151 For them, this optimization is safe only if
3152 both args are zero-extended or both are sign-extended.
3153 Otherwise, we might change the result.
3154 Eg, (short)-1 | (unsigned short)-1 is (int)-1
3155 but calculated in (unsigned short) it would be (unsigned short)-1. */
3157 if (shorten && none_complex)
3159 int unsigned0, unsigned1;
3160 tree arg0 = get_narrower (op0, &unsigned0);
3161 tree arg1 = get_narrower (op1, &unsigned1);
3162 /* UNS is 1 if the operation to be done is an unsigned one. */
3163 int uns = TYPE_UNSIGNED (result_type);
3166 final_type = result_type;
3168 /* Handle the case that OP0 does not *contain* a conversion
3169 but it *requires* conversion to FINAL_TYPE. */
3171 if (op0 == arg0 && TREE_TYPE (op0) != final_type)
3172 unsigned0 = TYPE_UNSIGNED (TREE_TYPE (op0));
3173 if (op1 == arg1 && TREE_TYPE (op1) != final_type)
3174 unsigned1 = TYPE_UNSIGNED (TREE_TYPE (op1));
3176 /* Now UNSIGNED0 is 1 if ARG0 zero-extends to FINAL_TYPE. */
3178 /* For bitwise operations, signedness of nominal type
3179 does not matter. Consider only how operands were extended. */
3183 /* Note that in all three cases below we refrain from optimizing
3184 an unsigned operation on sign-extended args.
3185 That would not be valid. */
3187 /* Both args variable: if both extended in same way
3188 from same width, do it in that width.
3189 Do it unsigned if args were zero-extended. */
3190 if ((TYPE_PRECISION (TREE_TYPE (arg0))
3191 < TYPE_PRECISION (result_type))
3192 && (TYPE_PRECISION (TREE_TYPE (arg1))
3193 == TYPE_PRECISION (TREE_TYPE (arg0)))
3194 && unsigned0 == unsigned1
3195 && (unsigned0 || !uns))
3196 result_type = c_common_signed_or_unsigned_type
3197 (unsigned0, common_type (TREE_TYPE (arg0), TREE_TYPE (arg1)));
3198 else if (TREE_CODE (arg0) == INTEGER_CST
3199 && (unsigned1 || !uns)
3200 && (TYPE_PRECISION (TREE_TYPE (arg1))
3201 < TYPE_PRECISION (result_type))
3202 && (type = c_common_signed_or_unsigned_type
3203 (unsigned1, TREE_TYPE (arg1)),
3204 int_fits_type_p (arg0, type)))
3206 else if (TREE_CODE (arg1) == INTEGER_CST
3207 && (unsigned0 || !uns)
3208 && (TYPE_PRECISION (TREE_TYPE (arg0))
3209 < TYPE_PRECISION (result_type))
3210 && (type = c_common_signed_or_unsigned_type
3211 (unsigned0, TREE_TYPE (arg0)),
3212 int_fits_type_p (arg1, type)))
3216 /* Shifts can be shortened if shifting right. */
3221 tree arg0 = get_narrower (op0, &unsigned_arg);
3223 final_type = result_type;
3225 if (arg0 == op0 && final_type == TREE_TYPE (op0))
3226 unsigned_arg = TYPE_UNSIGNED (TREE_TYPE (op0));
3228 if (TYPE_PRECISION (TREE_TYPE (arg0)) < TYPE_PRECISION (result_type)
3229 /* We can shorten only if the shift count is less than the
3230 number of bits in the smaller type size. */
3231 && compare_tree_int (op1, TYPE_PRECISION (TREE_TYPE (arg0))) < 0
3232 /* If arg is sign-extended and then unsigned-shifted,
3233 we can simulate this with a signed shift in arg's type
3234 only if the extended result is at least twice as wide
3235 as the arg. Otherwise, the shift could use up all the
3236 ones made by sign-extension and bring in zeros.
3237 We can't optimize that case at all, but in most machines
3238 it never happens because available widths are 2**N. */
3239 && (!TYPE_UNSIGNED (final_type)
3241 || (((unsigned) 2 * TYPE_PRECISION (TREE_TYPE (arg0)))
3242 <= TYPE_PRECISION (result_type))))
3244 /* Do an unsigned shift if the operand was zero-extended. */
3246 = c_common_signed_or_unsigned_type (unsigned_arg,
3248 /* Convert value-to-be-shifted to that type. */
3249 if (TREE_TYPE (op0) != result_type)
3250 op0 = cp_convert (result_type, op0);
3255 /* Comparison operations are shortened too but differently.
3256 They identify themselves by setting short_compare = 1. */
3260 /* Don't write &op0, etc., because that would prevent op0
3261 from being kept in a register.
3262 Instead, make copies of the our local variables and
3263 pass the copies by reference, then copy them back afterward. */
3264 tree xop0 = op0, xop1 = op1, xresult_type = result_type;
3265 enum tree_code xresultcode = resultcode;
3267 = shorten_compare (&xop0, &xop1, &xresult_type, &xresultcode);
3269 return cp_convert (boolean_type_node, val);
3270 op0 = xop0, op1 = xop1;
3272 resultcode = xresultcode;
3275 if ((short_compare || code == MIN_EXPR || code == MAX_EXPR)
3276 && warn_sign_compare
3277 /* Do not warn until the template is instantiated; we cannot
3278 bound the ranges of the arguments until that point. */
3279 && !processing_template_decl)
3281 int op0_signed = !TYPE_UNSIGNED (TREE_TYPE (orig_op0));
3282 int op1_signed = !TYPE_UNSIGNED (TREE_TYPE (orig_op1));
3284 int unsignedp0, unsignedp1;
3285 tree primop0 = get_narrower (op0, &unsignedp0);
3286 tree primop1 = get_narrower (op1, &unsignedp1);
3288 /* Check for comparison of different enum types. */
3289 if (TREE_CODE (TREE_TYPE (orig_op0)) == ENUMERAL_TYPE
3290 && TREE_CODE (TREE_TYPE (orig_op1)) == ENUMERAL_TYPE
3291 && TYPE_MAIN_VARIANT (TREE_TYPE (orig_op0))
3292 != TYPE_MAIN_VARIANT (TREE_TYPE (orig_op1)))
3294 warning ("comparison between types `%#T' and `%#T'",
3295 TREE_TYPE (orig_op0), TREE_TYPE (orig_op1));
3298 /* Give warnings for comparisons between signed and unsigned
3299 quantities that may fail. */
3300 /* Do the checking based on the original operand trees, so that
3301 casts will be considered, but default promotions won't be. */
3303 /* Do not warn if the comparison is being done in a signed type,
3304 since the signed type will only be chosen if it can represent
3305 all the values of the unsigned type. */
3306 if (!TYPE_UNSIGNED (result_type))
3308 /* Do not warn if both operands are unsigned. */
3309 else if (op0_signed == op1_signed)
3311 /* Do not warn if the signed quantity is an unsuffixed
3312 integer literal (or some static constant expression
3313 involving such literals or a conditional expression
3314 involving such literals) and it is non-negative. */
3315 else if ((op0_signed && tree_expr_nonnegative_p (orig_op0))
3316 || (op1_signed && tree_expr_nonnegative_p (orig_op1)))
3318 /* Do not warn if the comparison is an equality operation,
3319 the unsigned quantity is an integral constant and it does
3320 not use the most significant bit of result_type. */
3321 else if ((resultcode == EQ_EXPR || resultcode == NE_EXPR)
3322 && ((op0_signed && TREE_CODE (orig_op1) == INTEGER_CST
3323 && int_fits_type_p (orig_op1, c_common_signed_type
3325 || (op1_signed && TREE_CODE (orig_op0) == INTEGER_CST
3326 && int_fits_type_p (orig_op0, c_common_signed_type
3330 warning ("comparison between signed and unsigned integer expressions");
3332 /* Warn if two unsigned values are being compared in a size
3333 larger than their original size, and one (and only one) is the
3334 result of a `~' operator. This comparison will always fail.
3336 Also warn if one operand is a constant, and the constant does not
3337 have all bits set that are set in the ~ operand when it is
3340 if ((TREE_CODE (primop0) == BIT_NOT_EXPR)
3341 ^ (TREE_CODE (primop1) == BIT_NOT_EXPR))
3343 if (TREE_CODE (primop0) == BIT_NOT_EXPR)
3344 primop0 = get_narrower (TREE_OPERAND (op0, 0), &unsignedp0);
3345 if (TREE_CODE (primop1) == BIT_NOT_EXPR)
3346 primop1 = get_narrower (TREE_OPERAND (op1, 0), &unsignedp1);
3348 if (host_integerp (primop0, 0) || host_integerp (primop1, 0))
3351 HOST_WIDE_INT constant, mask;
3355 if (host_integerp (primop0, 0))
3358 unsignedp = unsignedp1;
3359 constant = tree_low_cst (primop0, 0);
3364 unsignedp = unsignedp0;
3365 constant = tree_low_cst (primop1, 0);
3368 bits = TYPE_PRECISION (TREE_TYPE (primop));
3369 if (bits < TYPE_PRECISION (result_type)
3370 && bits < HOST_BITS_PER_LONG && unsignedp)
3372 mask = (~ (HOST_WIDE_INT) 0) << bits;
3373 if ((mask & constant) != mask)
3374 warning ("comparison of promoted ~unsigned with constant");
3377 else if (unsignedp0 && unsignedp1
3378 && (TYPE_PRECISION (TREE_TYPE (primop0))
3379 < TYPE_PRECISION (result_type))
3380 && (TYPE_PRECISION (TREE_TYPE (primop1))
3381 < TYPE_PRECISION (result_type)))
3382 warning ("comparison of promoted ~unsigned with unsigned");
3387 /* At this point, RESULT_TYPE must be nonzero to avoid an error message.
3388 If CONVERTED is zero, both args will be converted to type RESULT_TYPE.
3389 Then the expression will be built.
3390 It will be given type FINAL_TYPE if that is nonzero;
3391 otherwise, it will be given type RESULT_TYPE. */
3395 error ("invalid operands of types `%T' and `%T' to binary `%O'",
3396 TREE_TYPE (orig_op0), TREE_TYPE (orig_op1), code);
3397 return error_mark_node;
3400 /* Issue warnings about peculiar, but valid, uses of NULL. */
3401 if (/* It's reasonable to use pointer values as operands of &&
3402 and ||, so NULL is no exception. */
3403 !(code == TRUTH_ANDIF_EXPR || code == TRUTH_ORIF_EXPR)
3404 && (/* If OP0 is NULL and OP1 is not a pointer, or vice versa. */
3405 (orig_op0 == null_node
3406 && TREE_CODE (TREE_TYPE (op1)) != POINTER_TYPE)
3407 /* Or vice versa. */
3408 || (orig_op1 == null_node
3409 && TREE_CODE (TREE_TYPE (op0)) != POINTER_TYPE)
3410 /* Or, both are NULL and the operation was not a comparison. */
3411 || (orig_op0 == null_node && orig_op1 == null_node
3412 && code != EQ_EXPR && code != NE_EXPR)))
3413 /* Some sort of arithmetic operation involving NULL was
3414 performed. Note that pointer-difference and pointer-addition
3415 have already been handled above, and so we don't end up here in
3417 warning ("NULL used in arithmetic");
3421 if (TREE_TYPE (op0) != result_type)
3422 op0 = cp_convert (result_type, op0);
3423 if (TREE_TYPE (op1) != result_type)
3424 op1 = cp_convert (result_type, op1);
3426 if (op0 == error_mark_node || op1 == error_mark_node)
3427 return error_mark_node;
3430 if (build_type == NULL_TREE)
3431 build_type = result_type;
3434 tree result = build (resultcode, build_type, op0, op1);
3437 folded = fold (result);
3438 if (folded == result)
3439 TREE_CONSTANT (folded) = TREE_CONSTANT (op0) & TREE_CONSTANT (op1);
3440 if (final_type != 0)
3441 return cp_convert (final_type, folded);
3446 /* Return a tree for the sum or difference (RESULTCODE says which)
3447 of pointer PTROP and integer INTOP. */
3450 cp_pointer_int_sum (enum tree_code resultcode, tree ptrop, tree intop)
3452 tree res_type = TREE_TYPE (ptrop);
3454 /* pointer_int_sum() uses size_in_bytes() on the TREE_TYPE(res_type)
3455 in certain circumstance (when it's valid to do so). So we need
3456 to make sure it's complete. We don't need to check here, if we
3457 can actually complete it at all, as those checks will be done in
3458 pointer_int_sum() anyway. */
3459 complete_type (TREE_TYPE (res_type));
3461 return pointer_int_sum (resultcode, ptrop, fold (intop));
3464 /* Return a tree for the difference of pointers OP0 and OP1.
3465 The resulting tree has type int. */
3468 pointer_diff (tree op0, tree op1, tree ptrtype)
3470 tree result, folded;
3471 tree restype = ptrdiff_type_node;
3472 tree target_type = TREE_TYPE (ptrtype);
3474 if (!complete_type_or_else (target_type, NULL_TREE))
3475 return error_mark_node;
3477 if (pedantic || warn_pointer_arith)
3479 if (TREE_CODE (target_type) == VOID_TYPE)
3480 pedwarn ("ISO C++ forbids using pointer of type `void *' in subtraction");
3481 if (TREE_CODE (target_type) == FUNCTION_TYPE)
3482 pedwarn ("ISO C++ forbids using pointer to a function in subtraction");
3483 if (TREE_CODE (target_type) == METHOD_TYPE)
3484 pedwarn ("ISO C++ forbids using pointer to a method in subtraction");
3487 /* First do the subtraction as integers;
3488 then drop through to build the divide operator. */
3490 op0 = cp_build_binary_op (MINUS_EXPR,
3491 cp_convert (restype, op0),
3492 cp_convert (restype, op1));
3494 /* This generates an error if op1 is a pointer to an incomplete type. */
3495 if (!COMPLETE_TYPE_P (TREE_TYPE (TREE_TYPE (op1))))
3496 error ("invalid use of a pointer to an incomplete type in pointer arithmetic");
3498 op1 = (TYPE_PTROB_P (ptrtype)
3499 ? size_in_bytes (target_type)
3500 : integer_one_node);
3502 /* Do the division. */
3504 result = build (EXACT_DIV_EXPR, restype, op0, cp_convert (restype, op1));
3506 folded = fold (result);
3507 if (folded == result)
3508 TREE_CONSTANT (folded) = TREE_CONSTANT (op0) & TREE_CONSTANT (op1);
3512 /* Construct and perhaps optimize a tree representation
3513 for a unary operation. CODE, a tree_code, specifies the operation
3514 and XARG is the operand. */
3517 build_x_unary_op (enum tree_code code, tree xarg)
3519 tree orig_expr = xarg;
3523 if (processing_template_decl)
3525 if (type_dependent_expression_p (xarg))
3526 return build_min_nt (code, xarg, NULL_TREE);
3527 xarg = build_non_dependent_expr (xarg);
3532 /* & rec, on incomplete RECORD_TYPEs is the simple opr &, not an
3534 if (code == ADDR_EXPR
3535 && TREE_CODE (xarg) != TEMPLATE_ID_EXPR
3536 && ((IS_AGGR_TYPE_CODE (TREE_CODE (TREE_TYPE (xarg)))
3537 && !COMPLETE_TYPE_P (TREE_TYPE (xarg)))
3538 || (TREE_CODE (xarg) == OFFSET_REF)))
3539 /* Don't look for a function. */;
3541 exp = build_new_op (code, LOOKUP_NORMAL, xarg, NULL_TREE, NULL_TREE,
3542 /*overloaded_p=*/NULL);
3543 if (!exp && code == ADDR_EXPR)
3545 /* A pointer to member-function can be formed only by saying
3547 if (!flag_ms_extensions && TREE_CODE (TREE_TYPE (xarg)) == METHOD_TYPE
3548 && (TREE_CODE (xarg) != OFFSET_REF || !PTRMEM_OK_P (xarg)))
3550 if (TREE_CODE (xarg) != OFFSET_REF)
3552 error ("invalid use of '%E' to form a pointer-to-member-function. Use a qualified-id.",
3554 return error_mark_node;
3558 error ("parenthesis around '%E' cannot be used to form a pointer-to-member-function",
3560 PTRMEM_OK_P (xarg) = 1;
3564 if (TREE_CODE (xarg) == OFFSET_REF)
3566 ptrmem = PTRMEM_OK_P (xarg);
3568 if (!ptrmem && !flag_ms_extensions
3569 && TREE_CODE (TREE_TYPE (TREE_OPERAND (xarg, 1))) == METHOD_TYPE)
3571 /* A single non-static member, make sure we don't allow a
3572 pointer-to-member. */
3573 xarg = build (OFFSET_REF, TREE_TYPE (xarg),
3574 TREE_OPERAND (xarg, 0),
3575 ovl_cons (TREE_OPERAND (xarg, 1), NULL_TREE));
3576 PTRMEM_OK_P (xarg) = ptrmem;
3579 else if (TREE_CODE (xarg) == TARGET_EXPR)
3580 warning ("taking address of temporary");
3581 exp = build_unary_op (ADDR_EXPR, xarg, 0);
3582 if (TREE_CODE (exp) == ADDR_EXPR)
3583 PTRMEM_OK_P (exp) = ptrmem;
3586 if (processing_template_decl && exp != error_mark_node)
3587 return build_min_non_dep (code, exp, orig_expr,
3588 /*For {PRE,POST}{INC,DEC}REMENT_EXPR*/NULL_TREE);
3592 /* Like c_common_truthvalue_conversion, but handle pointer-to-member
3593 constants, where a null value is represented by an INTEGER_CST of
3597 cp_truthvalue_conversion (tree expr)
3599 tree type = TREE_TYPE (expr);
3600 if (TYPE_PTRMEM_P (type))
3601 return build_binary_op (NE_EXPR, expr, integer_zero_node, 1);
3603 return c_common_truthvalue_conversion (expr);
3606 /* Just like cp_truthvalue_conversion, but we want a CLEANUP_POINT_EXPR. */
3609 condition_conversion (tree expr)
3612 if (processing_template_decl)
3614 t = perform_implicit_conversion (boolean_type_node, expr);
3615 t = fold (build1 (CLEANUP_POINT_EXPR, boolean_type_node, t));
3619 /* Return an ADDR_EXPR giving the address of T. This function
3620 attempts no optimizations or simplifications; it is a low-level
3624 build_address (tree t)
3628 if (error_operand_p (t) || !cxx_mark_addressable (t))
3629 return error_mark_node;
3631 addr = build1 (ADDR_EXPR, build_pointer_type (TREE_TYPE (t)), t);
3633 TREE_CONSTANT (addr) = 1;
3638 /* Return a NOP_EXPR converting EXPR to TYPE. */
3641 build_nop (tree type, tree expr)
3645 if (type == error_mark_node || error_operand_p (expr))
3648 nop = build1 (NOP_EXPR, type, expr);
3649 if (TREE_CONSTANT (expr))
3650 TREE_CONSTANT (nop) = 1;
3655 /* C++: Must handle pointers to members.
3657 Perhaps type instantiation should be extended to handle conversion
3658 from aggregates to types we don't yet know we want? (Or are those
3659 cases typically errors which should be reported?)
3661 NOCONVERT nonzero suppresses the default promotions
3662 (such as from short to int). */
3665 build_unary_op (enum tree_code code, tree xarg, int noconvert)
3667 /* No default_conversion here. It causes trouble for ADDR_EXPR. */
3670 const char *errstring = NULL;
3673 if (arg == error_mark_node)
3674 return error_mark_node;
3679 /* This is used for unary plus, because a CONVERT_EXPR
3680 is enough to prevent anybody from looking inside for
3681 associativity, but won't generate any code. */
3682 if (!(arg = build_expr_type_conversion
3683 (WANT_ARITH | WANT_ENUM | WANT_POINTER, arg, true)))
3684 errstring = "wrong type argument to unary plus";
3688 arg = default_conversion (arg);
3689 arg = build1 (NON_LVALUE_EXPR, TREE_TYPE (arg), arg);
3690 TREE_CONSTANT (arg) = TREE_CONSTANT (TREE_OPERAND (arg, 0));
3695 if (!(arg = build_expr_type_conversion (WANT_ARITH | WANT_ENUM, arg, true)))
3696 errstring = "wrong type argument to unary minus";
3697 else if (!noconvert && CP_INTEGRAL_TYPE_P (TREE_TYPE (arg)))
3698 arg = perform_integral_promotions (arg);
3702 if (TREE_CODE (TREE_TYPE (arg)) == COMPLEX_TYPE)
3706 arg = default_conversion (arg);
3708 else if (!(arg = build_expr_type_conversion (WANT_INT | WANT_ENUM,
3710 errstring = "wrong type argument to bit-complement";
3711 else if (!noconvert)
3712 arg = perform_integral_promotions (arg);
3716 if (!(arg = build_expr_type_conversion (WANT_ARITH | WANT_ENUM, arg, true)))
3717 errstring = "wrong type argument to abs";
3718 else if (!noconvert)
3719 arg = default_conversion (arg);
3723 /* Conjugating a real value is a no-op, but allow it anyway. */
3724 if (!(arg = build_expr_type_conversion (WANT_ARITH | WANT_ENUM, arg, true)))
3725 errstring = "wrong type argument to conjugation";
3726 else if (!noconvert)
3727 arg = default_conversion (arg);
3730 case TRUTH_NOT_EXPR:
3731 arg = perform_implicit_conversion (boolean_type_node, arg);
3732 val = invert_truthvalue (arg);
3733 if (arg != error_mark_node)
3735 errstring = "in argument to unary !";
3742 if (TREE_CODE (arg) == COMPLEX_CST)
3743 return TREE_REALPART (arg);
3744 else if (TREE_CODE (TREE_TYPE (arg)) == COMPLEX_TYPE)
3745 return fold (build1 (REALPART_EXPR, TREE_TYPE (TREE_TYPE (arg)), arg));
3750 if (TREE_CODE (arg) == COMPLEX_CST)
3751 return TREE_IMAGPART (arg);
3752 else if (TREE_CODE (TREE_TYPE (arg)) == COMPLEX_TYPE)
3753 return fold (build1 (IMAGPART_EXPR, TREE_TYPE (TREE_TYPE (arg)), arg));
3755 return cp_convert (TREE_TYPE (arg), integer_zero_node);
3757 case PREINCREMENT_EXPR:
3758 case POSTINCREMENT_EXPR:
3759 case PREDECREMENT_EXPR:
3760 case POSTDECREMENT_EXPR:
3761 /* Handle complex lvalues (when permitted)
3762 by reduction to simpler cases. */
3764 val = unary_complex_lvalue (code, arg);
3768 /* Increment or decrement the real part of the value,
3769 and don't change the imaginary part. */
3770 if (TREE_CODE (TREE_TYPE (arg)) == COMPLEX_TYPE)
3774 arg = stabilize_reference (arg);
3775 real = build_unary_op (REALPART_EXPR, arg, 1);
3776 imag = build_unary_op (IMAGPART_EXPR, arg, 1);
3777 return build (COMPLEX_EXPR, TREE_TYPE (arg),
3778 build_unary_op (code, real, 1), imag);
3781 /* Report invalid types. */
3783 if (!(arg = build_expr_type_conversion (WANT_ARITH | WANT_POINTER,
3786 if (code == PREINCREMENT_EXPR)
3787 errstring ="no pre-increment operator for type";
3788 else if (code == POSTINCREMENT_EXPR)
3789 errstring ="no post-increment operator for type";
3790 else if (code == PREDECREMENT_EXPR)
3791 errstring ="no pre-decrement operator for type";
3793 errstring ="no post-decrement operator for type";
3797 /* Report something read-only. */
3799 if (CP_TYPE_CONST_P (TREE_TYPE (arg))
3800 || TREE_READONLY (arg))
3801 readonly_error (arg, ((code == PREINCREMENT_EXPR
3802 || code == POSTINCREMENT_EXPR)
3803 ? "increment" : "decrement"),
3808 tree result_type = TREE_TYPE (arg);
3810 arg = get_unwidened (arg, 0);
3811 argtype = TREE_TYPE (arg);
3813 /* ARM $5.2.5 last annotation says this should be forbidden. */
3814 if (TREE_CODE (argtype) == ENUMERAL_TYPE)
3815 pedwarn ("ISO C++ forbids %sing an enum",
3816 (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
3817 ? "increment" : "decrement");
3819 /* Compute the increment. */
3821 if (TREE_CODE (argtype) == POINTER_TYPE)
3823 tree type = complete_type (TREE_TYPE (argtype));
3825 if (!COMPLETE_OR_VOID_TYPE_P (type))
3826 error ("cannot %s a pointer to incomplete type `%T'",
3827 ((code == PREINCREMENT_EXPR
3828 || code == POSTINCREMENT_EXPR)
3829 ? "increment" : "decrement"), TREE_TYPE (argtype));
3830 else if ((pedantic || warn_pointer_arith)
3831 && !TYPE_PTROB_P (argtype))
3832 pedwarn ("ISO C++ forbids %sing a pointer of type `%T'",
3833 ((code == PREINCREMENT_EXPR
3834 || code == POSTINCREMENT_EXPR)
3835 ? "increment" : "decrement"), argtype);
3836 inc = cxx_sizeof_nowarn (TREE_TYPE (argtype));
3839 inc = integer_one_node;
3841 inc = cp_convert (argtype, inc);
3843 /* Handle incrementing a cast-expression. */
3845 switch (TREE_CODE (arg))
3850 case FIX_TRUNC_EXPR:
3851 case FIX_FLOOR_EXPR:
3852 case FIX_ROUND_EXPR:
3855 tree incremented, modify, value, compound;
3856 if (! lvalue_p (arg) && pedantic)
3857 pedwarn ("cast to non-reference type used as lvalue");
3858 arg = stabilize_reference (arg);
3859 if (code == PREINCREMENT_EXPR || code == PREDECREMENT_EXPR)
3862 value = save_expr (arg);
3863 incremented = build (((code == PREINCREMENT_EXPR
3864 || code == POSTINCREMENT_EXPR)
3865 ? PLUS_EXPR : MINUS_EXPR),
3866 argtype, value, inc);
3868 modify = build_modify_expr (arg, NOP_EXPR, incremented);
3869 compound = build (COMPOUND_EXPR, TREE_TYPE (arg), modify, value);
3871 /* Eliminate warning about unused result of + or -. */
3872 TREE_NO_UNUSED_WARNING (compound) = 1;
3880 /* Complain about anything else that is not a true lvalue. */
3881 if (!lvalue_or_else (arg, ((code == PREINCREMENT_EXPR
3882 || code == POSTINCREMENT_EXPR)
3883 ? "increment" : "decrement")))
3884 return error_mark_node;
3886 /* Forbid using -- on `bool'. */
3887 if (TREE_TYPE (arg) == boolean_type_node)
3889 if (code == POSTDECREMENT_EXPR || code == PREDECREMENT_EXPR)
3891 error ("invalid use of `--' on bool variable `%D'", arg);
3892 return error_mark_node;
3894 val = boolean_increment (code, arg);
3897 val = build (code, TREE_TYPE (arg), arg, inc);
3899 TREE_SIDE_EFFECTS (val) = 1;
3900 return cp_convert (result_type, val);
3904 /* Note that this operation never does default_conversion
3905 regardless of NOCONVERT. */
3907 argtype = lvalue_type (arg);
3909 if (TREE_CODE (arg) == OFFSET_REF)
3912 if (TREE_CODE (argtype) == REFERENCE_TYPE)
3916 build_pointer_type (TREE_TYPE (argtype)), arg);
3917 TREE_CONSTANT (arg) = TREE_CONSTANT (TREE_OPERAND (arg, 0));
3920 else if (pedantic && DECL_MAIN_P (arg))
3922 pedwarn ("ISO C++ forbids taking address of function `::main'");
3924 /* Let &* cancel out to simplify resulting code. */
3925 if (TREE_CODE (arg) == INDIRECT_REF)
3927 /* We don't need to have `current_class_ptr' wrapped in a
3928 NON_LVALUE_EXPR node. */
3929 if (arg == current_class_ref)
3930 return current_class_ptr;
3932 arg = TREE_OPERAND (arg, 0);
3933 if (TREE_CODE (TREE_TYPE (arg)) == REFERENCE_TYPE)
3937 build_pointer_type (TREE_TYPE (TREE_TYPE (arg))), arg);
3938 TREE_CONSTANT (arg) = TREE_CONSTANT (TREE_OPERAND (arg, 0));
3940 else if (lvalue_p (arg))
3941 /* Don't let this be an lvalue. */
3942 return non_lvalue (arg);
3946 /* For &x[y], return x+y. */
3947 if (TREE_CODE (arg) == ARRAY_REF)
3949 if (!cxx_mark_addressable (TREE_OPERAND (arg, 0)))
3950 return error_mark_node;
3951 return cp_build_binary_op (PLUS_EXPR, TREE_OPERAND (arg, 0),
3952 TREE_OPERAND (arg, 1));
3955 /* Uninstantiated types are all functions. Taking the
3956 address of a function is a no-op, so just return the
3959 if (TREE_CODE (arg) == IDENTIFIER_NODE
3960 && IDENTIFIER_OPNAME_P (arg))
3963 /* We don't know the type yet, so just work around the problem.
3964 We know that this will resolve to an lvalue. */
3965 return build1 (ADDR_EXPR, unknown_type_node, arg);
3968 if (TREE_CODE (arg) == COMPONENT_REF && type_unknown_p (arg)
3969 && !really_overloaded_fn (TREE_OPERAND (arg, 1)))
3971 /* They're trying to take the address of a unique non-static
3972 member function. This is ill-formed (except in MS-land),
3973 but let's try to DTRT.
3974 Note: We only handle unique functions here because we don't
3975 want to complain if there's a static overload; non-unique
3976 cases will be handled by instantiate_type. But we need to
3977 handle this case here to allow casts on the resulting PMF.
3978 We could defer this in non-MS mode, but it's easier to give
3979 a useful error here. */
3981 /* Inside constant member functions, the `this' pointer
3982 contains an extra const qualifier. TYPE_MAIN_VARIANT
3983 is used here to remove this const from the diagnostics
3984 and the created OFFSET_REF. */
3985 tree base = TYPE_MAIN_VARIANT (TREE_TYPE (TREE_OPERAND (arg, 0)));
3986 tree name = DECL_NAME (get_first_fn (TREE_OPERAND (arg, 1)));
3988 if (! flag_ms_extensions)
3990 if (current_class_type
3991 && TREE_OPERAND (arg, 0) == current_class_ref)
3992 /* An expression like &memfn. */
3993 pedwarn ("ISO C++ forbids taking the address of an unqualified"
3994 " or parenthesized non-static member function to form"
3995 " a pointer to member function. Say `&%T::%D'",
3998 pedwarn ("ISO C++ forbids taking the address of a bound member"
3999 " function to form a pointer to member function."
4003 arg = build_offset_ref (base, name, /*address_p=*/true);
4007 if (type_unknown_p (arg))
4008 return build1 (ADDR_EXPR, unknown_type_node, arg);
4010 /* Handle complex lvalues (when permitted)
4011 by reduction to simpler cases. */
4012 val = unary_complex_lvalue (code, arg);
4016 switch (TREE_CODE (arg))
4021 case FIX_TRUNC_EXPR:
4022 case FIX_FLOOR_EXPR:
4023 case FIX_ROUND_EXPR:
4025 if (! lvalue_p (arg) && pedantic)
4026 pedwarn ("ISO C++ forbids taking the address of a cast to a non-lvalue expression");
4030 arg = OVL_CURRENT (arg);
4037 /* Allow the address of a constructor if all the elements