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));
1062 return TYPE_VECTOR_SUBPARTS (t1) == TYPE_VECTOR_SUBPARTS (t2)
1063 && same_type_p (TREE_TYPE (t1), TREE_TYPE (t2));
1072 /* Returns 1 if TYPE1 is at least as qualified as TYPE2. */
1075 at_least_as_qualified_p (tree type1, tree type2)
1077 int q1 = cp_type_quals (type1);
1078 int q2 = cp_type_quals (type2);
1080 /* All qualifiers for TYPE2 must also appear in TYPE1. */
1081 return (q1 & q2) == q2;
1084 /* Returns 1 if TYPE1 is more qualified than TYPE2. */
1087 more_qualified_p (tree type1, tree type2)
1089 int q1 = cp_type_quals (type1);
1090 int q2 = cp_type_quals (type2);
1092 return q1 != q2 && (q1 & q2) == q2;
1095 /* Returns 1 if TYPE1 is more cv-qualified than TYPE2, -1 if TYPE2 is
1096 more cv-qualified that TYPE1, and 0 otherwise. */
1099 comp_cv_qualification (tree type1, tree type2)
1101 int q1 = cp_type_quals (type1);
1102 int q2 = cp_type_quals (type2);
1107 if ((q1 & q2) == q2)
1109 else if ((q1 & q2) == q1)
1115 /* Returns 1 if the cv-qualification signature of TYPE1 is a proper
1116 subset of the cv-qualification signature of TYPE2, and the types
1117 are similar. Returns -1 if the other way 'round, and 0 otherwise. */
1120 comp_cv_qual_signature (tree type1, tree type2)
1122 if (comp_ptr_ttypes_real (type2, type1, -1))
1124 else if (comp_ptr_ttypes_real (type1, type2, -1))
1130 /* If two types share a common base type, return that basetype.
1131 If there is not a unique most-derived base type, this function
1132 returns ERROR_MARK_NODE. */
1135 common_base_type (tree tt1, tree tt2)
1137 tree best = NULL_TREE;
1140 /* If one is a baseclass of another, that's good enough. */
1141 if (UNIQUELY_DERIVED_FROM_P (tt1, tt2))
1143 if (UNIQUELY_DERIVED_FROM_P (tt2, tt1))
1146 /* Otherwise, try to find a unique baseclass of TT1
1147 that is shared by TT2, and follow that down. */
1148 for (i = CLASSTYPE_N_BASECLASSES (tt1)-1; i >= 0; i--)
1150 tree basetype = TYPE_BINFO_BASETYPE (tt1, i);
1151 tree trial = common_base_type (basetype, tt2);
1154 if (trial == error_mark_node)
1156 if (best == NULL_TREE)
1158 else if (best != trial)
1159 return error_mark_node;
1164 for (i = CLASSTYPE_N_BASECLASSES (tt2)-1; i >= 0; i--)
1166 tree basetype = TYPE_BINFO_BASETYPE (tt2, i);
1167 tree trial = common_base_type (tt1, basetype);
1170 if (trial == error_mark_node)
1172 if (best == NULL_TREE)
1174 else if (best != trial)
1175 return error_mark_node;
1181 /* Subroutines of `comptypes'. */
1183 /* Return true if two parameter type lists PARMS1 and PARMS2 are
1184 equivalent in the sense that functions with those parameter types
1185 can have equivalent types. The two lists must be equivalent,
1186 element by element. */
1189 compparms (tree parms1, tree parms2)
1193 /* An unspecified parmlist matches any specified parmlist
1194 whose argument types don't need default promotions. */
1196 for (t1 = parms1, t2 = parms2;
1198 t1 = TREE_CHAIN (t1), t2 = TREE_CHAIN (t2))
1200 /* If one parmlist is shorter than the other,
1201 they fail to match. */
1204 if (!same_type_p (TREE_VALUE (t1), TREE_VALUE (t2)))
1211 /* Process a sizeof or alignof expression where the operand is a
1215 cxx_sizeof_or_alignof_type (tree type, enum tree_code op, bool complain)
1217 enum tree_code type_code;
1219 const char *op_name;
1221 my_friendly_assert (op == SIZEOF_EXPR || op == ALIGNOF_EXPR, 20020720);
1222 if (type == error_mark_node)
1223 return error_mark_node;
1225 if (processing_template_decl)
1227 value = build_min (op, size_type_node, type);
1228 TREE_READONLY (value) = 1;
1232 op_name = operator_name_info[(int) op].name;
1234 type = non_reference (type);
1235 type_code = TREE_CODE (type);
1237 if (type_code == METHOD_TYPE)
1239 if (complain && (pedantic || warn_pointer_arith))
1240 pedwarn ("invalid application of `%s' to a member function", op_name);
1241 value = size_one_node;
1244 value = c_sizeof_or_alignof_type (complete_type (type), op, complain);
1249 /* Process a sizeof or alignof expression where the operand is an
1253 cxx_sizeof_or_alignof_expr (tree e, enum tree_code op)
1255 const char *op_name = operator_name_info[(int) op].name;
1257 if (e == error_mark_node)
1258 return error_mark_node;
1260 if (processing_template_decl)
1262 e = build_min (op, size_type_node, e);
1263 TREE_SIDE_EFFECTS (e) = 0;
1264 TREE_READONLY (e) = 1;
1269 if (TREE_CODE (e) == COMPONENT_REF
1270 && TREE_CODE (TREE_OPERAND (e, 1)) == FIELD_DECL
1271 && DECL_C_BIT_FIELD (TREE_OPERAND (e, 1)))
1273 error ("invalid application of `%s' to a bit-field", op_name);
1276 else if (is_overloaded_fn (e))
1278 pedwarn ("ISO C++ forbids applying `%s' to an expression of function type", op_name);
1281 else if (type_unknown_p (e))
1283 cxx_incomplete_type_error (e, TREE_TYPE (e));
1289 return cxx_sizeof_or_alignof_type (e, op, true);
1293 /* Perform the conversions in [expr] that apply when an lvalue appears
1294 in an rvalue context: the lvalue-to-rvalue, array-to-pointer, and
1295 function-to-pointer conversions.
1297 In addition manifest constants are replaced by their values. */
1300 decay_conversion (tree exp)
1303 enum tree_code code;
1305 type = TREE_TYPE (exp);
1306 code = TREE_CODE (type);
1308 if (code == REFERENCE_TYPE)
1310 exp = convert_from_reference (exp);
1311 type = TREE_TYPE (exp);
1312 code = TREE_CODE (type);
1315 if (type == error_mark_node)
1316 return error_mark_node;
1318 if (type_unknown_p (exp))
1320 cxx_incomplete_type_error (exp, TREE_TYPE (exp));
1321 return error_mark_node;
1324 /* Constants can be used directly unless they're not loadable. */
1325 if (TREE_CODE (exp) == CONST_DECL)
1326 exp = DECL_INITIAL (exp);
1327 /* Replace a nonvolatile const static variable with its value. We
1328 don't do this for arrays, though; we want the address of the
1329 first element of the array, not the address of the first element
1330 of its initializing constant. */
1331 else if (code != ARRAY_TYPE)
1333 exp = decl_constant_value (exp);
1334 type = TREE_TYPE (exp);
1337 /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
1338 Leave such NOP_EXPRs, since RHS is being used in non-lvalue context. */
1340 if (code == VOID_TYPE)
1342 error ("void value not ignored as it ought to be");
1343 return error_mark_node;
1345 if (code == METHOD_TYPE)
1347 error ("invalid use of non-static member function");
1348 return error_mark_node;
1350 if (code == FUNCTION_TYPE || is_overloaded_fn (exp))
1351 return build_unary_op (ADDR_EXPR, exp, 0);
1352 if (code == ARRAY_TYPE)
1357 if (TREE_CODE (exp) == INDIRECT_REF)
1358 return build_nop (build_pointer_type (TREE_TYPE (type)),
1359 TREE_OPERAND (exp, 0));
1361 if (TREE_CODE (exp) == COMPOUND_EXPR)
1363 tree op1 = decay_conversion (TREE_OPERAND (exp, 1));
1364 return build (COMPOUND_EXPR, TREE_TYPE (op1),
1365 TREE_OPERAND (exp, 0), op1);
1369 && ! (TREE_CODE (exp) == CONSTRUCTOR && TREE_STATIC (exp)))
1371 error ("invalid use of non-lvalue array");
1372 return error_mark_node;
1375 ptrtype = build_pointer_type (TREE_TYPE (type));
1377 if (TREE_CODE (exp) == VAR_DECL)
1379 if (!cxx_mark_addressable (exp))
1380 return error_mark_node;
1381 adr = build_nop (ptrtype, build_address (exp));
1382 TREE_SIDE_EFFECTS (adr) = 0; /* Default would be, same as EXP. */
1385 /* This way is better for a COMPONENT_REF since it can
1386 simplify the offset for a component. */
1387 adr = build_unary_op (ADDR_EXPR, exp, 1);
1388 return cp_convert (ptrtype, adr);
1391 /* [basic.lval]: Class rvalues can have cv-qualified types; non-class
1392 rvalues always have cv-unqualified types. */
1393 if (! CLASS_TYPE_P (type))
1394 exp = cp_convert (TYPE_MAIN_VARIANT (type), exp);
1400 default_conversion (tree exp)
1402 exp = decay_conversion (exp);
1404 if (INTEGRAL_OR_ENUMERATION_TYPE_P (TREE_TYPE (exp)))
1405 exp = perform_integral_promotions (exp);
1410 /* EXPR is an expression with an integral or enumeration type.
1411 Perform the integral promotions in [conv.prom], and return the
1415 perform_integral_promotions (tree expr)
1420 type = TREE_TYPE (expr);
1421 my_friendly_assert (INTEGRAL_OR_ENUMERATION_TYPE_P (type), 20030703);
1422 promoted_type = type_promotes_to (type);
1423 if (type != promoted_type)
1424 expr = cp_convert (promoted_type, expr);
1428 /* Take the address of an inline function without setting TREE_ADDRESSABLE
1432 inline_conversion (tree exp)
1434 if (TREE_CODE (exp) == FUNCTION_DECL)
1435 exp = build1 (ADDR_EXPR, build_pointer_type (TREE_TYPE (exp)), exp);
1440 /* Returns nonzero iff exp is a STRING_CST or the result of applying
1441 decay_conversion to one. */
1444 string_conv_p (tree totype, tree exp, int warn)
1448 if (! flag_const_strings || TREE_CODE (totype) != POINTER_TYPE)
1451 t = TREE_TYPE (totype);
1452 if (!same_type_p (t, char_type_node)
1453 && !same_type_p (t, wchar_type_node))
1456 if (TREE_CODE (exp) == STRING_CST)
1458 /* Make sure that we don't try to convert between char and wchar_t. */
1459 if (!same_type_p (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (exp))), t))
1464 /* Is this a string constant which has decayed to 'const char *'? */
1465 t = build_pointer_type (build_qualified_type (t, TYPE_QUAL_CONST));
1466 if (!same_type_p (TREE_TYPE (exp), t))
1469 if (TREE_CODE (exp) != ADDR_EXPR
1470 || TREE_CODE (TREE_OPERAND (exp, 0)) != STRING_CST)
1474 /* This warning is not very useful, as it complains about printf. */
1475 if (warn && warn_write_strings)
1476 warning ("deprecated conversion from string constant to `%T'", totype);
1481 /* Given a COND_EXPR, MIN_EXPR, or MAX_EXPR in T, return it in a form that we
1482 can, for example, use as an lvalue. This code used to be in
1483 unary_complex_lvalue, but we needed it to deal with `a = (d == c) ? b : c'
1484 expressions, where we're dealing with aggregates. But now it's again only
1485 called from unary_complex_lvalue. The case (in particular) that led to
1486 this was with CODE == ADDR_EXPR, since it's not an lvalue when we'd
1490 rationalize_conditional_expr (enum tree_code code, tree t)
1492 /* For MIN_EXPR or MAX_EXPR, fold-const.c has arranged things so that
1493 the first operand is always the one to be used if both operands
1494 are equal, so we know what conditional expression this used to be. */
1495 if (TREE_CODE (t) == MIN_EXPR || TREE_CODE (t) == MAX_EXPR)
1498 build_conditional_expr (build_x_binary_op ((TREE_CODE (t) == MIN_EXPR
1499 ? LE_EXPR : GE_EXPR),
1500 TREE_OPERAND (t, 0),
1501 TREE_OPERAND (t, 1),
1502 /*overloaded_p=*/NULL),
1503 build_unary_op (code, TREE_OPERAND (t, 0), 0),
1504 build_unary_op (code, TREE_OPERAND (t, 1), 0));
1508 build_conditional_expr (TREE_OPERAND (t, 0),
1509 build_unary_op (code, TREE_OPERAND (t, 1), 0),
1510 build_unary_op (code, TREE_OPERAND (t, 2), 0));
1513 /* Given the TYPE of an anonymous union field inside T, return the
1514 FIELD_DECL for the field. If not found return NULL_TREE. Because
1515 anonymous unions can nest, we must also search all anonymous unions
1516 that are directly reachable. */
1519 lookup_anon_field (tree t, tree type)
1523 for (field = TYPE_FIELDS (t); field; field = TREE_CHAIN (field))
1525 if (TREE_STATIC (field))
1527 if (TREE_CODE (field) != FIELD_DECL || DECL_ARTIFICIAL (field))
1530 /* If we find it directly, return the field. */
1531 if (DECL_NAME (field) == NULL_TREE
1532 && type == TYPE_MAIN_VARIANT (TREE_TYPE (field)))
1537 /* Otherwise, it could be nested, search harder. */
1538 if (DECL_NAME (field) == NULL_TREE
1539 && ANON_AGGR_TYPE_P (TREE_TYPE (field)))
1541 tree subfield = lookup_anon_field (TREE_TYPE (field), type);
1549 /* Build an expression representing OBJECT.MEMBER. OBJECT is an
1550 expression; MEMBER is a DECL or baselink. If ACCESS_PATH is
1551 non-NULL, it indicates the path to the base used to name MEMBER.
1552 If PRESERVE_REFERENCE is true, the expression returned will have
1553 REFERENCE_TYPE if the MEMBER does. Otherwise, the expression
1554 returned will have the type referred to by the reference.
1556 This function does not perform access control; that is either done
1557 earlier by the parser when the name of MEMBER is resolved to MEMBER
1558 itself, or later when overload resolution selects one of the
1559 functions indicated by MEMBER. */
1562 build_class_member_access_expr (tree object, tree member,
1563 tree access_path, bool preserve_reference)
1567 tree result = NULL_TREE;
1569 if (object == error_mark_node || member == error_mark_node)
1570 return error_mark_node;
1572 if (TREE_CODE (member) == PSEUDO_DTOR_EXPR)
1575 my_friendly_assert (DECL_P (member) || BASELINK_P (member),
1580 The type of the first expression shall be "class object" (of a
1582 object_type = TREE_TYPE (object);
1583 if (!currently_open_class (object_type)
1584 && !complete_type_or_else (object_type, object))
1585 return error_mark_node;
1586 if (!CLASS_TYPE_P (object_type))
1588 error ("request for member `%D' in `%E', which is of non-class type `%T'",
1589 member, object, object_type);
1590 return error_mark_node;
1593 /* The standard does not seem to actually say that MEMBER must be a
1594 member of OBJECT_TYPE. However, that is clearly what is
1596 if (DECL_P (member))
1598 member_scope = DECL_CLASS_CONTEXT (member);
1600 if (TREE_DEPRECATED (member))
1601 warn_deprecated_use (member);
1604 member_scope = BINFO_TYPE (BASELINK_BINFO (member));
1605 /* If MEMBER is from an anonymous aggregate, MEMBER_SCOPE will
1606 presently be the anonymous union. Go outwards until we find a
1607 type related to OBJECT_TYPE. */
1608 while (ANON_AGGR_TYPE_P (member_scope)
1609 && !same_type_ignoring_top_level_qualifiers_p (member_scope,
1611 member_scope = TYPE_CONTEXT (member_scope);
1612 if (!member_scope || !DERIVED_FROM_P (member_scope, object_type))
1614 if (TREE_CODE (member) == FIELD_DECL)
1615 error ("invalid use of nonstatic data member '%E'", member);
1617 error ("`%D' is not a member of `%T'", member, object_type);
1618 return error_mark_node;
1621 /* Transform `(a, b).x' into `(*(a, &b)).x', `(a ? b : c).x' into
1622 `(*(a ? &b : &c)).x', and so on. A COND_EXPR is only an lvalue
1623 in the frontend; only _DECLs and _REFs are lvalues in the backend. */
1625 tree temp = unary_complex_lvalue (ADDR_EXPR, object);
1627 object = build_indirect_ref (temp, NULL);
1630 /* In [expr.ref], there is an explicit list of the valid choices for
1631 MEMBER. We check for each of those cases here. */
1632 if (TREE_CODE (member) == VAR_DECL)
1634 /* A static data member. */
1636 /* If OBJECT has side-effects, they are supposed to occur. */
1637 if (TREE_SIDE_EFFECTS (object))
1638 result = build (COMPOUND_EXPR, TREE_TYPE (result), object, result);
1640 else if (TREE_CODE (member) == FIELD_DECL)
1642 /* A non-static data member. */
1647 null_object_p = (TREE_CODE (object) == INDIRECT_REF
1648 && integer_zerop (TREE_OPERAND (object, 0)));
1650 /* Convert OBJECT to the type of MEMBER. */
1651 if (!same_type_p (TYPE_MAIN_VARIANT (object_type),
1652 TYPE_MAIN_VARIANT (member_scope)))
1657 binfo = lookup_base (access_path ? access_path : object_type,
1658 member_scope, ba_ignore, &kind);
1659 if (binfo == error_mark_node)
1660 return error_mark_node;
1662 /* It is invalid to try to get to a virtual base of a
1663 NULL object. The most common cause is invalid use of
1665 if (null_object_p && kind == bk_via_virtual)
1667 error ("invalid access to non-static data member `%D' of NULL object",
1669 error ("(perhaps the `offsetof' macro was used incorrectly)");
1670 return error_mark_node;
1673 /* Convert to the base. */
1674 object = build_base_path (PLUS_EXPR, object, binfo,
1676 /* If we found the base successfully then we should be able
1677 to convert to it successfully. */
1678 my_friendly_assert (object != error_mark_node,
1682 /* Complain about other invalid uses of offsetof, even though they will
1683 give the right answer. Note that we complain whether or not they
1684 actually used the offsetof macro, since there's no way to know at this
1685 point. So we just give a warning, instead of a pedwarn. */
1686 if (null_object_p && warn_invalid_offsetof
1687 && CLASSTYPE_NON_POD_P (object_type))
1689 warning ("invalid access to non-static data member `%D' of NULL object",
1691 warning ("(perhaps the `offsetof' macro was used incorrectly)");
1694 /* If MEMBER is from an anonymous aggregate, we have converted
1695 OBJECT so that it refers to the class containing the
1696 anonymous union. Generate a reference to the anonymous union
1697 itself, and recur to find MEMBER. */
1698 if (ANON_AGGR_TYPE_P (DECL_CONTEXT (member))
1699 /* When this code is called from build_field_call, the
1700 object already has the type of the anonymous union.
1701 That is because the COMPONENT_REF was already
1702 constructed, and was then disassembled before calling
1703 build_field_call. After the function-call code is
1704 cleaned up, this waste can be eliminated. */
1705 && (!same_type_ignoring_top_level_qualifiers_p
1706 (TREE_TYPE (object), DECL_CONTEXT (member))))
1708 tree anonymous_union;
1710 anonymous_union = lookup_anon_field (TREE_TYPE (object),
1711 DECL_CONTEXT (member));
1712 object = build_class_member_access_expr (object,
1714 /*access_path=*/NULL_TREE,
1715 preserve_reference);
1718 /* Compute the type of the field, as described in [expr.ref]. */
1719 type_quals = TYPE_UNQUALIFIED;
1720 member_type = TREE_TYPE (member);
1721 if (TREE_CODE (member_type) != REFERENCE_TYPE)
1723 type_quals = (cp_type_quals (member_type)
1724 | cp_type_quals (object_type));
1726 /* A field is const (volatile) if the enclosing object, or the
1727 field itself, is const (volatile). But, a mutable field is
1728 not const, even within a const object. */
1729 if (DECL_MUTABLE_P (member))
1730 type_quals &= ~TYPE_QUAL_CONST;
1731 member_type = cp_build_qualified_type (member_type, type_quals);
1734 result = fold (build (COMPONENT_REF, member_type, object, member));
1736 /* Mark the expression const or volatile, as appropriate. Even
1737 though we've dealt with the type above, we still have to mark the
1738 expression itself. */
1739 if (type_quals & TYPE_QUAL_CONST)
1740 TREE_READONLY (result) = 1;
1741 else if (type_quals & TYPE_QUAL_VOLATILE)
1742 TREE_THIS_VOLATILE (result) = 1;
1744 else if (BASELINK_P (member))
1746 /* The member is a (possibly overloaded) member function. */
1750 /* If the MEMBER is exactly one static member function, then we
1751 know the type of the expression. Otherwise, we must wait
1752 until overload resolution has been performed. */
1753 functions = BASELINK_FUNCTIONS (member);
1754 if (TREE_CODE (functions) == FUNCTION_DECL
1755 && DECL_STATIC_FUNCTION_P (functions))
1756 type = TREE_TYPE (functions);
1758 type = unknown_type_node;
1759 /* Note that we do not convert OBJECT to the BASELINK_BINFO
1760 base. That will happen when the function is called. */
1761 result = build (COMPONENT_REF, type, object, member);
1763 else if (TREE_CODE (member) == CONST_DECL)
1765 /* The member is an enumerator. */
1767 /* If OBJECT has side-effects, they are supposed to occur. */
1768 if (TREE_SIDE_EFFECTS (object))
1769 result = build (COMPOUND_EXPR, TREE_TYPE (result),
1774 error ("invalid use of `%D'", member);
1775 return error_mark_node;
1778 if (!preserve_reference)
1781 If E2 is declared to have type "reference to T", then ... the
1782 type of E1.E2 is T. */
1783 result = convert_from_reference (result);
1788 /* Return the destructor denoted by OBJECT.SCOPE::~DTOR_NAME, or, if
1789 SCOPE is NULL, by OBJECT.~DTOR_NAME. */
1792 lookup_destructor (tree object, tree scope, tree dtor_name)
1794 tree object_type = TREE_TYPE (object);
1795 tree dtor_type = TREE_OPERAND (dtor_name, 0);
1798 if (scope && !check_dtor_name (scope, dtor_name))
1800 error ("qualified type `%T' does not match destructor name `~%T'",
1802 return error_mark_node;
1804 if (!DERIVED_FROM_P (dtor_type, TYPE_MAIN_VARIANT (object_type)))
1806 error ("the type being destroyed is `%T', but the destructor refers to `%T'",
1807 TYPE_MAIN_VARIANT (object_type), dtor_type);
1808 return error_mark_node;
1810 if (!TYPE_HAS_DESTRUCTOR (dtor_type))
1811 return build (PSEUDO_DTOR_EXPR, void_type_node, object, scope,
1813 expr = lookup_member (dtor_type, complete_dtor_identifier,
1814 /*protect=*/1, /*want_type=*/false);
1815 expr = (adjust_result_of_qualified_name_lookup
1816 (expr, dtor_type, object_type));
1820 /* This function is called by the parser to process a class member
1821 access expression of the form OBJECT.NAME. NAME is a node used by
1822 the parser to represent a name; it is not yet a DECL. It may,
1823 however, be a BASELINK where the BASELINK_FUNCTIONS is a
1824 TEMPLATE_ID_EXPR. Templates must be looked up by the parser, and
1825 there is no reason to do the lookup twice, so the parser keeps the
1829 finish_class_member_access_expr (tree object, tree name)
1834 tree access_path = NULL_TREE;
1835 tree orig_object = object;
1836 tree orig_name = name;
1838 if (object == error_mark_node || name == error_mark_node)
1839 return error_mark_node;
1841 object_type = TREE_TYPE (object);
1843 if (processing_template_decl)
1845 if (/* If OBJECT_TYPE is dependent, so is OBJECT.NAME. */
1846 dependent_type_p (object_type)
1847 /* If NAME is just an IDENTIFIER_NODE, then the expression
1849 || TREE_CODE (object) == IDENTIFIER_NODE
1850 /* If NAME is "f<args>", where either 'f' or 'args' is
1851 dependent, then the expression is dependent. */
1852 || (TREE_CODE (name) == TEMPLATE_ID_EXPR
1853 && dependent_template_id_p (TREE_OPERAND (name, 0),
1854 TREE_OPERAND (name, 1)))
1855 /* If NAME is "T::X" where "T" is dependent, then the
1856 expression is dependent. */
1857 || (TREE_CODE (name) == SCOPE_REF
1858 && TYPE_P (TREE_OPERAND (name, 0))
1859 && dependent_type_p (TREE_OPERAND (name, 0))))
1860 return build_min_nt (COMPONENT_REF, object, name);
1861 object = build_non_dependent_expr (object);
1864 if (TREE_CODE (object_type) == REFERENCE_TYPE)
1866 object = convert_from_reference (object);
1867 object_type = TREE_TYPE (object);
1872 The type of the first expression shall be "class object" (of a
1874 if (!currently_open_class (object_type)
1875 && !complete_type_or_else (object_type, object))
1876 return error_mark_node;
1877 if (!CLASS_TYPE_P (object_type))
1879 error ("request for member `%D' in `%E', which is of non-class type `%T'",
1880 name, object, object_type);
1881 return error_mark_node;
1884 if (BASELINK_P (name))
1886 /* A member function that has already been looked up. */
1887 my_friendly_assert ((TREE_CODE (BASELINK_FUNCTIONS (name))
1888 == TEMPLATE_ID_EXPR),
1894 bool is_template_id = false;
1895 tree template_args = NULL_TREE;
1898 if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
1900 is_template_id = true;
1901 template_args = TREE_OPERAND (name, 1);
1902 name = TREE_OPERAND (name, 0);
1904 if (TREE_CODE (name) == OVERLOAD)
1905 name = DECL_NAME (get_first_fn (name));
1906 else if (DECL_P (name))
1907 name = DECL_NAME (name);
1910 if (TREE_CODE (name) == SCOPE_REF)
1912 /* A qualified name. The qualifying class or namespace `S' has
1913 already been looked up; it is either a TYPE or a
1914 NAMESPACE_DECL. The member name is either an IDENTIFIER_NODE
1915 or a BIT_NOT_EXPR. */
1916 scope = TREE_OPERAND (name, 0);
1917 name = TREE_OPERAND (name, 1);
1918 my_friendly_assert ((CLASS_TYPE_P (scope)
1919 || TREE_CODE (scope) == NAMESPACE_DECL),
1921 my_friendly_assert ((TREE_CODE (name) == IDENTIFIER_NODE
1922 || TREE_CODE (name) == BIT_NOT_EXPR),
1925 /* If SCOPE is a namespace, then the qualified name does not
1926 name a member of OBJECT_TYPE. */
1927 if (TREE_CODE (scope) == NAMESPACE_DECL)
1929 error ("`%D::%D' is not a member of `%T'",
1930 scope, name, object_type);
1931 return error_mark_node;
1934 /* Find the base of OBJECT_TYPE corresponding to SCOPE. */
1935 access_path = lookup_base (object_type, scope, ba_check, NULL);
1936 if (access_path == error_mark_node)
1937 return error_mark_node;
1940 error ("`%T' is not a base of `%T'", scope, object_type);
1941 return error_mark_node;
1947 access_path = object_type;
1950 if (TREE_CODE (name) == BIT_NOT_EXPR)
1951 member = lookup_destructor (object, scope, name);
1954 /* Look up the member. */
1955 member = lookup_member (access_path, name, /*protect=*/1,
1956 /*want_type=*/false);
1957 if (member == NULL_TREE)
1959 error ("'%D' has no member named '%E'", object_type, name);
1960 return error_mark_node;
1962 if (member == error_mark_node)
1963 return error_mark_node;
1968 tree template = member;
1970 if (BASELINK_P (template))
1971 template = lookup_template_function (template, template_args);
1974 error ("`%D' is not a member template function", name);
1975 return error_mark_node;
1980 if (TREE_DEPRECATED (member))
1981 warn_deprecated_use (member);
1983 expr = build_class_member_access_expr (object, member, access_path,
1984 /*preserve_reference=*/false);
1985 if (processing_template_decl && expr != error_mark_node)
1986 return build_min_non_dep (COMPONENT_REF, expr,
1987 orig_object, orig_name);
1991 /* Return an expression for the MEMBER_NAME field in the internal
1992 representation of PTRMEM, a pointer-to-member function. (Each
1993 pointer-to-member function type gets its own RECORD_TYPE so it is
1994 more convenient to access the fields by name than by FIELD_DECL.)
1995 This routine converts the NAME to a FIELD_DECL and then creates the
1996 node for the complete expression. */
1999 build_ptrmemfunc_access_expr (tree ptrmem, tree member_name)
2005 /* This code is a stripped down version of
2006 build_class_member_access_expr. It does not work to use that
2007 routine directly because it expects the object to be of class
2009 ptrmem_type = TREE_TYPE (ptrmem);
2010 my_friendly_assert (TYPE_PTRMEMFUNC_P (ptrmem_type), 20020804);
2011 member = lookup_member (ptrmem_type, member_name, /*protect=*/0,
2012 /*want_type=*/false);
2013 member_type = cp_build_qualified_type (TREE_TYPE (member),
2014 cp_type_quals (ptrmem_type));
2015 return fold (build (COMPONENT_REF, member_type, ptrmem, member));
2018 /* Given an expression PTR for a pointer, return an expression
2019 for the value pointed to.
2020 ERRORSTRING is the name of the operator to appear in error messages.
2022 This function may need to overload OPERATOR_FNNAME.
2023 Must also handle REFERENCE_TYPEs for C++. */
2026 build_x_indirect_ref (tree expr, const char *errorstring)
2028 tree orig_expr = expr;
2031 if (processing_template_decl)
2033 if (type_dependent_expression_p (expr))
2034 return build_min_nt (INDIRECT_REF, expr);
2035 expr = build_non_dependent_expr (expr);
2038 rval = build_new_op (INDIRECT_REF, LOOKUP_NORMAL, expr, NULL_TREE,
2039 NULL_TREE, /*overloaded_p=*/NULL);
2041 rval = build_indirect_ref (expr, errorstring);
2043 if (processing_template_decl && rval != error_mark_node)
2044 return build_min_non_dep (INDIRECT_REF, rval, orig_expr);
2050 build_indirect_ref (tree ptr, const char *errorstring)
2054 if (ptr == error_mark_node)
2055 return error_mark_node;
2057 if (ptr == current_class_ptr)
2058 return current_class_ref;
2060 pointer = (TREE_CODE (TREE_TYPE (ptr)) == REFERENCE_TYPE
2061 ? ptr : decay_conversion (ptr));
2062 type = TREE_TYPE (pointer);
2064 if (TYPE_PTR_P (type) || TREE_CODE (type) == REFERENCE_TYPE)
2068 If the type of the expression is "pointer to T," the type
2069 of the result is "T."
2071 We must use the canonical variant because certain parts of
2072 the back end, like fold, do pointer comparisons between
2074 tree t = canonical_type_variant (TREE_TYPE (type));
2076 if (VOID_TYPE_P (t))
2078 /* A pointer to incomplete type (other than cv void) can be
2079 dereferenced [expr.unary.op]/1 */
2080 error ("`%T' is not a pointer-to-object type", type);
2081 return error_mark_node;
2083 else if (TREE_CODE (pointer) == ADDR_EXPR
2084 && same_type_p (t, TREE_TYPE (TREE_OPERAND (pointer, 0))))
2085 /* The POINTER was something like `&x'. We simplify `*&x' to
2087 return TREE_OPERAND (pointer, 0);
2090 tree ref = build1 (INDIRECT_REF, t, pointer);
2092 /* We *must* set TREE_READONLY when dereferencing a pointer to const,
2093 so that we get the proper error message if the result is used
2094 to assign to. Also, &* is supposed to be a no-op. */
2095 TREE_READONLY (ref) = CP_TYPE_CONST_P (t);
2096 TREE_THIS_VOLATILE (ref) = CP_TYPE_VOLATILE_P (t);
2097 TREE_SIDE_EFFECTS (ref)
2098 = (TREE_THIS_VOLATILE (ref) || TREE_SIDE_EFFECTS (pointer));
2102 /* `pointer' won't be an error_mark_node if we were given a
2103 pointer to member, so it's cool to check for this here. */
2104 else if (TYPE_PTR_TO_MEMBER_P (type))
2105 error ("invalid use of `%s' on pointer to member", errorstring);
2106 else if (pointer != error_mark_node)
2109 error ("invalid type argument of `%s'", errorstring);
2111 error ("invalid type argument");
2113 return error_mark_node;
2116 /* This handles expressions of the form "a[i]", which denotes
2119 This is logically equivalent in C to *(a+i), but we may do it differently.
2120 If A is a variable or a member, we generate a primitive ARRAY_REF.
2121 This avoids forcing the array out of registers, and can work on
2122 arrays that are not lvalues (for example, members of structures returned
2125 If INDEX is of some user-defined type, it must be converted to
2126 integer type. Otherwise, to make a compatible PLUS_EXPR, it
2127 will inherit the type of the array, which will be some pointer type. */
2130 build_array_ref (tree array, tree idx)
2134 error ("subscript missing in array reference");
2135 return error_mark_node;
2138 if (TREE_TYPE (array) == error_mark_node
2139 || TREE_TYPE (idx) == error_mark_node)
2140 return error_mark_node;
2142 /* If ARRAY is a COMPOUND_EXPR or COND_EXPR, move our reference
2144 switch (TREE_CODE (array))
2148 tree value = build_array_ref (TREE_OPERAND (array, 1), idx);
2149 return build (COMPOUND_EXPR, TREE_TYPE (value),
2150 TREE_OPERAND (array, 0), value);
2154 return build_conditional_expr
2155 (TREE_OPERAND (array, 0),
2156 build_array_ref (TREE_OPERAND (array, 1), idx),
2157 build_array_ref (TREE_OPERAND (array, 2), idx));
2163 if (TREE_CODE (TREE_TYPE (array)) == ARRAY_TYPE
2164 && TREE_CODE (array) != INDIRECT_REF)
2168 /* Subscripting with type char is likely to lose
2169 on a machine where chars are signed.
2170 So warn on any machine, but optionally.
2171 Don't warn for unsigned char since that type is safe.
2172 Don't warn for signed char because anyone who uses that
2173 must have done so deliberately. */
2174 if (warn_char_subscripts
2175 && TYPE_MAIN_VARIANT (TREE_TYPE (idx)) == char_type_node)
2176 warning ("array subscript has type `char'");
2178 if (!INTEGRAL_OR_ENUMERATION_TYPE_P (TREE_TYPE (idx)))
2180 error ("array subscript is not an integer");
2181 return error_mark_node;
2184 /* Apply integral promotions *after* noticing character types.
2185 (It is unclear why we do these promotions -- the standard
2186 does not say that we should. In fact, the natual thing would
2187 seem to be to convert IDX to ptrdiff_t; we're performing
2188 pointer arithmetic.) */
2189 idx = perform_integral_promotions (idx);
2191 /* An array that is indexed by a non-constant
2192 cannot be stored in a register; we must be able to do
2193 address arithmetic on its address.
2194 Likewise an array of elements of variable size. */
2195 if (TREE_CODE (idx) != INTEGER_CST
2196 || (COMPLETE_TYPE_P (TREE_TYPE (TREE_TYPE (array)))
2197 && (TREE_CODE (TYPE_SIZE (TREE_TYPE (TREE_TYPE (array))))
2200 if (!cxx_mark_addressable (array))
2201 return error_mark_node;
2204 /* An array that is indexed by a constant value which is not within
2205 the array bounds cannot be stored in a register either; because we
2206 would get a crash in store_bit_field/extract_bit_field when trying
2207 to access a non-existent part of the register. */
2208 if (TREE_CODE (idx) == INTEGER_CST
2209 && TYPE_DOMAIN (TREE_TYPE (array))
2210 && ! int_fits_type_p (idx, TYPE_DOMAIN (TREE_TYPE (array))))
2212 if (!cxx_mark_addressable (array))
2213 return error_mark_node;
2216 if (pedantic && !lvalue_p (array))
2217 pedwarn ("ISO C++ forbids subscripting non-lvalue array");
2219 /* Note in C++ it is valid to subscript a `register' array, since
2220 it is valid to take the address of something with that
2221 storage specification. */
2225 while (TREE_CODE (foo) == COMPONENT_REF)
2226 foo = TREE_OPERAND (foo, 0);
2227 if (TREE_CODE (foo) == VAR_DECL && DECL_REGISTER (foo))
2228 warning ("subscripting array declared `register'");
2231 type = TREE_TYPE (TREE_TYPE (array));
2232 rval = build (ARRAY_REF, type, array, idx);
2233 /* Array ref is const/volatile if the array elements are
2234 or if the array is.. */
2235 TREE_READONLY (rval)
2236 |= (CP_TYPE_CONST_P (type) | TREE_READONLY (array));
2237 TREE_SIDE_EFFECTS (rval)
2238 |= (CP_TYPE_VOLATILE_P (type) | TREE_SIDE_EFFECTS (array));
2239 TREE_THIS_VOLATILE (rval)
2240 |= (CP_TYPE_VOLATILE_P (type) | TREE_THIS_VOLATILE (array));
2241 return require_complete_type (fold (rval));
2245 tree ar = default_conversion (array);
2246 tree ind = default_conversion (idx);
2248 /* Put the integer in IND to simplify error checking. */
2249 if (TREE_CODE (TREE_TYPE (ar)) == INTEGER_TYPE)
2256 if (ar == error_mark_node)
2259 if (TREE_CODE (TREE_TYPE (ar)) != POINTER_TYPE)
2261 error ("subscripted value is neither array nor pointer");
2262 return error_mark_node;
2264 if (TREE_CODE (TREE_TYPE (ind)) != INTEGER_TYPE)
2266 error ("array subscript is not an integer");
2267 return error_mark_node;
2270 return build_indirect_ref (cp_build_binary_op (PLUS_EXPR, ar, ind),
2275 /* Resolve a pointer to member function. INSTANCE is the object
2276 instance to use, if the member points to a virtual member.
2278 This used to avoid checking for virtual functions if basetype
2279 has no virtual functions, according to an earlier ANSI draft.
2280 With the final ISO C++ rules, such an optimization is
2281 incorrect: A pointer to a derived member can be static_cast
2282 to pointer-to-base-member, as long as the dynamic object
2283 later has the right member. */
2286 get_member_function_from_ptrfunc (tree *instance_ptrptr, tree function)
2288 if (TREE_CODE (function) == OFFSET_REF)
2289 function = TREE_OPERAND (function, 1);
2291 if (TYPE_PTRMEMFUNC_P (TREE_TYPE (function)))
2293 tree idx, delta, e1, e2, e3, vtbl, basetype;
2294 tree fntype = TYPE_PTRMEMFUNC_FN_TYPE (TREE_TYPE (function));
2296 tree instance_ptr = *instance_ptrptr;
2297 tree instance_save_expr = 0;
2298 if (instance_ptr == error_mark_node)
2300 if (TREE_CODE (function) == PTRMEM_CST)
2302 /* Extracting the function address from a pmf is only
2303 allowed with -Wno-pmf-conversions. It only works for
2305 e1 = build_addr_func (PTRMEM_CST_MEMBER (function));
2306 e1 = convert (fntype, e1);
2311 error ("object missing in use of `%E'", function);
2312 return error_mark_node;
2316 if (TREE_SIDE_EFFECTS (instance_ptr))
2317 instance_ptr = instance_save_expr = save_expr (instance_ptr);
2319 if (TREE_SIDE_EFFECTS (function))
2320 function = save_expr (function);
2322 /* Start by extracting all the information from the PMF itself. */
2323 e3 = PFN_FROM_PTRMEMFUNC (function);
2324 delta = build_ptrmemfunc_access_expr (function, delta_identifier);
2325 idx = build1 (NOP_EXPR, vtable_index_type, e3);
2326 switch (TARGET_PTRMEMFUNC_VBIT_LOCATION)
2328 case ptrmemfunc_vbit_in_pfn:
2329 e1 = cp_build_binary_op (BIT_AND_EXPR, idx, integer_one_node);
2330 idx = cp_build_binary_op (MINUS_EXPR, idx, integer_one_node);
2333 case ptrmemfunc_vbit_in_delta:
2334 e1 = cp_build_binary_op (BIT_AND_EXPR, delta, integer_one_node);
2335 delta = cp_build_binary_op (RSHIFT_EXPR, delta, integer_one_node);
2342 /* Convert down to the right base before using the instance. First
2344 basetype = TYPE_METHOD_BASETYPE (TREE_TYPE (fntype));
2345 basetype = lookup_base (TREE_TYPE (TREE_TYPE (instance_ptr)),
2346 basetype, ba_check, NULL);
2347 instance_ptr = build_base_path (PLUS_EXPR, instance_ptr, basetype, 1);
2348 if (instance_ptr == error_mark_node)
2349 return error_mark_node;
2350 /* ...and then the delta in the PMF. */
2351 instance_ptr = build (PLUS_EXPR, TREE_TYPE (instance_ptr),
2352 instance_ptr, delta);
2354 /* Hand back the adjusted 'this' argument to our caller. */
2355 *instance_ptrptr = instance_ptr;
2357 /* Next extract the vtable pointer from the object. */
2358 vtbl = build1 (NOP_EXPR, build_pointer_type (vtbl_ptr_type_node),
2360 vtbl = build_indirect_ref (vtbl, NULL);
2362 /* Finally, extract the function pointer from the vtable. */
2363 e2 = fold (build (PLUS_EXPR, TREE_TYPE (vtbl), vtbl, idx));
2364 e2 = build_indirect_ref (e2, NULL);
2365 TREE_CONSTANT (e2) = 1;
2367 /* When using function descriptors, the address of the
2368 vtable entry is treated as a function pointer. */
2369 if (TARGET_VTABLE_USES_DESCRIPTORS)
2370 e2 = build1 (NOP_EXPR, TREE_TYPE (e2),
2371 build_unary_op (ADDR_EXPR, e2, /*noconvert=*/1));
2373 TREE_TYPE (e2) = TREE_TYPE (e3);
2374 e1 = build_conditional_expr (e1, e2, e3);
2376 /* Make sure this doesn't get evaluated first inside one of the
2377 branches of the COND_EXPR. */
2378 if (instance_save_expr)
2379 e1 = build (COMPOUND_EXPR, TREE_TYPE (e1),
2380 instance_save_expr, e1);
2388 build_function_call (tree function, tree params)
2390 tree fntype, fndecl;
2391 tree coerced_params;
2393 tree name = NULL_TREE;
2395 tree original = function;
2397 /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
2398 Strip such NOP_EXPRs, since FUNCTION is used in non-lvalue context. */
2399 if (TREE_CODE (function) == NOP_EXPR
2400 && TREE_TYPE (function) == TREE_TYPE (TREE_OPERAND (function, 0)))
2401 function = TREE_OPERAND (function, 0);
2403 if (TREE_CODE (function) == FUNCTION_DECL)
2405 name = DECL_NAME (function);
2407 mark_used (function);
2410 /* Convert anything with function type to a pointer-to-function. */
2411 if (pedantic && DECL_MAIN_P (function))
2412 pedwarn ("ISO C++ forbids calling `::main' from within program");
2414 /* Differs from default_conversion by not setting TREE_ADDRESSABLE
2415 (because calling an inline function does not mean the function
2416 needs to be separately compiled). */
2418 if (DECL_INLINE (function))
2419 function = inline_conversion (function);
2421 function = build_addr_func (function);
2427 function = build_addr_func (function);
2430 if (function == error_mark_node)
2431 return error_mark_node;
2433 fntype = TREE_TYPE (function);
2435 if (TYPE_PTRMEMFUNC_P (fntype))
2437 error ("must use .* or ->* to call pointer-to-member function in `%E (...)'",
2439 return error_mark_node;
2442 is_method = (TREE_CODE (fntype) == POINTER_TYPE
2443 && TREE_CODE (TREE_TYPE (fntype)) == METHOD_TYPE);
2445 if (!((TREE_CODE (fntype) == POINTER_TYPE
2446 && TREE_CODE (TREE_TYPE (fntype)) == FUNCTION_TYPE)
2448 || TREE_CODE (function) == TEMPLATE_ID_EXPR))
2450 error ("`%E' cannot be used as a function", original);
2451 return error_mark_node;
2454 /* fntype now gets the type of function pointed to. */
2455 fntype = TREE_TYPE (fntype);
2457 /* Convert the parameters to the types declared in the
2458 function prototype, or apply default promotions. */
2460 coerced_params = convert_arguments (TYPE_ARG_TYPES (fntype),
2461 params, fndecl, LOOKUP_NORMAL);
2462 if (coerced_params == error_mark_node)
2463 return error_mark_node;
2465 /* Check for errors in format strings. */
2468 check_function_format (NULL, TYPE_ATTRIBUTES (fntype), coerced_params);
2470 /* Recognize certain built-in functions so we can make tree-codes
2471 other than CALL_EXPR. We do this when it enables fold-const.c
2472 to do something useful. */
2474 if (TREE_CODE (function) == ADDR_EXPR
2475 && TREE_CODE (TREE_OPERAND (function, 0)) == FUNCTION_DECL
2476 && DECL_BUILT_IN (TREE_OPERAND (function, 0)))
2478 result = expand_tree_builtin (TREE_OPERAND (function, 0),
2479 params, coerced_params);
2484 return build_cxx_call (function, params, coerced_params);
2487 /* Convert the actual parameter expressions in the list VALUES
2488 to the types in the list TYPELIST.
2489 If parmdecls is exhausted, or when an element has NULL as its type,
2490 perform the default conversions.
2492 NAME is an IDENTIFIER_NODE or 0. It is used only for error messages.
2494 This is also where warnings about wrong number of args are generated.
2496 Return a list of expressions for the parameters as converted.
2498 Both VALUES and the returned value are chains of TREE_LIST nodes
2499 with the elements of the list in the TREE_VALUE slots of those nodes.
2501 In C++, unspecified trailing parameters can be filled in with their
2502 default arguments, if such were specified. Do so here. */
2505 convert_arguments (tree typelist, tree values, tree fndecl, int flags)
2507 tree typetail, valtail;
2508 tree result = NULL_TREE;
2509 const char *called_thing = 0;
2512 /* Argument passing is always copy-initialization. */
2513 flags |= LOOKUP_ONLYCONVERTING;
2517 if (TREE_CODE (TREE_TYPE (fndecl)) == METHOD_TYPE)
2519 if (DECL_NAME (fndecl) == NULL_TREE
2520 || IDENTIFIER_HAS_TYPE_VALUE (DECL_NAME (fndecl)))
2521 called_thing = "constructor";
2523 called_thing = "member function";
2526 called_thing = "function";
2529 for (valtail = values, typetail = typelist;
2531 valtail = TREE_CHAIN (valtail), i++)
2533 tree type = typetail ? TREE_VALUE (typetail) : 0;
2534 tree val = TREE_VALUE (valtail);
2536 if (val == error_mark_node)
2537 return error_mark_node;
2539 if (type == void_type_node)
2543 cp_error_at ("too many arguments to %s `%+#D'", called_thing,
2545 error ("at this point in file");
2548 error ("too many arguments to function");
2549 /* In case anybody wants to know if this argument
2552 TREE_TYPE (tree_last (result)) = error_mark_node;
2556 /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
2557 Strip such NOP_EXPRs, since VAL is used in non-lvalue context. */
2558 if (TREE_CODE (val) == NOP_EXPR
2559 && TREE_TYPE (val) == TREE_TYPE (TREE_OPERAND (val, 0))
2560 && (type == 0 || TREE_CODE (type) != REFERENCE_TYPE))
2561 val = TREE_OPERAND (val, 0);
2563 if (type == 0 || TREE_CODE (type) != REFERENCE_TYPE)
2565 if (TREE_CODE (TREE_TYPE (val)) == ARRAY_TYPE
2566 || TREE_CODE (TREE_TYPE (val)) == FUNCTION_TYPE
2567 || TREE_CODE (TREE_TYPE (val)) == METHOD_TYPE)
2568 val = decay_conversion (val);
2571 if (val == error_mark_node)
2572 return error_mark_node;
2576 /* Formal parm type is specified by a function prototype. */
2579 if (!COMPLETE_TYPE_P (complete_type (type)))
2582 error ("parameter %P of `%D' has incomplete type `%T'",
2585 error ("parameter %P has incomplete type `%T'", i, type);
2586 parmval = error_mark_node;
2590 parmval = convert_for_initialization
2591 (NULL_TREE, type, val, flags,
2592 "argument passing", fndecl, i);
2593 parmval = convert_for_arg_passing (type, parmval);
2596 if (parmval == error_mark_node)
2597 return error_mark_node;
2599 result = tree_cons (NULL_TREE, parmval, result);
2603 if (TREE_CODE (TREE_TYPE (val)) == REFERENCE_TYPE)
2604 val = convert_from_reference (val);
2606 if (fndecl && DECL_BUILT_IN (fndecl)
2607 && DECL_FUNCTION_CODE (fndecl) == BUILT_IN_CONSTANT_P)
2608 /* Don't do ellipsis conversion for __built_in_constant_p
2609 as this will result in spurious warnings for non-POD
2611 val = require_complete_type (val);
2613 val = convert_arg_to_ellipsis (val);
2615 result = tree_cons (NULL_TREE, val, result);
2619 typetail = TREE_CHAIN (typetail);
2622 if (typetail != 0 && typetail != void_list_node)
2624 /* See if there are default arguments that can be used. */
2625 if (TREE_PURPOSE (typetail)
2626 && TREE_CODE (TREE_PURPOSE (typetail)) != DEFAULT_ARG)
2628 for (; typetail != void_list_node; ++i)
2631 = convert_default_arg (TREE_VALUE (typetail),
2632 TREE_PURPOSE (typetail),
2635 if (parmval == error_mark_node)
2636 return error_mark_node;
2638 result = tree_cons (0, parmval, result);
2639 typetail = TREE_CHAIN (typetail);
2640 /* ends with `...'. */
2641 if (typetail == NULL_TREE)
2649 cp_error_at ("too few arguments to %s `%+#D'",
2650 called_thing, fndecl);
2651 error ("at this point in file");
2654 error ("too few arguments to function");
2655 return error_mark_list;
2659 return nreverse (result);
2662 /* Build a binary-operation expression, after performing default
2663 conversions on the operands. CODE is the kind of expression to build. */
2666 build_x_binary_op (enum tree_code code, tree arg1, tree arg2,
2676 if (processing_template_decl)
2678 if (type_dependent_expression_p (arg1)
2679 || type_dependent_expression_p (arg2))
2680 return build_min_nt (code, arg1, arg2);
2681 arg1 = build_non_dependent_expr (arg1);
2682 arg2 = build_non_dependent_expr (arg2);
2685 if (code == DOTSTAR_EXPR)
2686 expr = build_m_component_ref (arg1, arg2);
2688 expr = build_new_op (code, LOOKUP_NORMAL, arg1, arg2, NULL_TREE,
2691 if (processing_template_decl && expr != error_mark_node)
2692 return build_min_non_dep (code, expr, orig_arg1, orig_arg2);
2697 /* Build a binary-operation expression without default conversions.
2698 CODE is the kind of expression to build.
2699 This function differs from `build' in several ways:
2700 the data type of the result is computed and recorded in it,
2701 warnings are generated if arg data types are invalid,
2702 special handling for addition and subtraction of pointers is known,
2703 and some optimization is done (operations on narrow ints
2704 are done in the narrower type when that gives the same result).
2705 Constant folding is also done before the result is returned.
2707 Note that the operands will never have enumeral types
2708 because either they have just had the default conversions performed
2709 or they have both just been converted to some other type in which
2710 the arithmetic is to be done.
2712 C++: must do special pointer arithmetic when implementing
2713 multiple inheritance, and deal with pointer to member functions. */
2716 build_binary_op (enum tree_code code, tree orig_op0, tree orig_op1,
2717 int convert_p ATTRIBUTE_UNUSED)
2720 enum tree_code code0, code1;
2723 /* Expression code to give to the expression when it is built.
2724 Normally this is CODE, which is what the caller asked for,
2725 but in some special cases we change it. */
2726 enum tree_code resultcode = code;
2728 /* Data type in which the computation is to be performed.
2729 In the simplest cases this is the common type of the arguments. */
2730 tree result_type = NULL;
2732 /* Nonzero means operands have already been type-converted
2733 in whatever way is necessary.
2734 Zero means they need to be converted to RESULT_TYPE. */
2737 /* Nonzero means create the expression with this type, rather than
2739 tree build_type = 0;
2741 /* Nonzero means after finally constructing the expression
2742 convert it to this type. */
2743 tree final_type = 0;
2745 /* Nonzero if this is an operation like MIN or MAX which can
2746 safely be computed in short if both args are promoted shorts.
2747 Also implies COMMON.
2748 -1 indicates a bitwise operation; this makes a difference
2749 in the exact conditions for when it is safe to do the operation
2750 in a narrower mode. */
2753 /* Nonzero if this is a comparison operation;
2754 if both args are promoted shorts, compare the original shorts.
2755 Also implies COMMON. */
2756 int short_compare = 0;
2758 /* Nonzero if this is a right-shift operation, which can be computed on the
2759 original short and then promoted if the operand is a promoted short. */
2760 int short_shift = 0;
2762 /* Nonzero means set RESULT_TYPE to the common type of the args. */
2765 /* Apply default conversions. */
2769 if (code == TRUTH_AND_EXPR || code == TRUTH_ANDIF_EXPR
2770 || code == TRUTH_OR_EXPR || code == TRUTH_ORIF_EXPR
2771 || code == TRUTH_XOR_EXPR)
2773 if (!really_overloaded_fn (op0))
2774 op0 = decay_conversion (op0);
2775 if (!really_overloaded_fn (op1))
2776 op1 = decay_conversion (op1);
2780 if (!really_overloaded_fn (op0))
2781 op0 = default_conversion (op0);
2782 if (!really_overloaded_fn (op1))
2783 op1 = default_conversion (op1);
2786 /* Strip NON_LVALUE_EXPRs, etc., since we aren't using as an lvalue. */
2787 STRIP_TYPE_NOPS (op0);
2788 STRIP_TYPE_NOPS (op1);
2790 /* DTRT if one side is an overloaded function, but complain about it. */
2791 if (type_unknown_p (op0))
2793 tree t = instantiate_type (TREE_TYPE (op1), op0, tf_none);
2794 if (t != error_mark_node)
2796 pedwarn ("assuming cast to type `%T' from overloaded function",
2801 if (type_unknown_p (op1))
2803 tree t = instantiate_type (TREE_TYPE (op0), op1, tf_none);
2804 if (t != error_mark_node)
2806 pedwarn ("assuming cast to type `%T' from overloaded function",
2812 type0 = TREE_TYPE (op0);
2813 type1 = TREE_TYPE (op1);
2815 /* The expression codes of the data types of the arguments tell us
2816 whether the arguments are integers, floating, pointers, etc. */
2817 code0 = TREE_CODE (type0);
2818 code1 = TREE_CODE (type1);
2820 /* If an error was already reported for one of the arguments,
2821 avoid reporting another error. */
2823 if (code0 == ERROR_MARK || code1 == ERROR_MARK)
2824 return error_mark_node;
2829 /* Handle the pointer + int case. */
2830 if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
2831 return cp_pointer_int_sum (PLUS_EXPR, op0, op1);
2832 else if (code1 == POINTER_TYPE && code0 == INTEGER_TYPE)
2833 return cp_pointer_int_sum (PLUS_EXPR, op1, op0);
2839 /* Subtraction of two similar pointers.
2840 We must subtract them as integers, then divide by object size. */
2841 if (code0 == POINTER_TYPE && code1 == POINTER_TYPE
2842 && same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (type0),
2844 return pointer_diff (op0, op1, common_type (type0, type1));
2845 /* Handle pointer minus int. Just like pointer plus int. */
2846 else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
2847 return cp_pointer_int_sum (MINUS_EXPR, op0, op1);
2856 case TRUNC_DIV_EXPR:
2858 case FLOOR_DIV_EXPR:
2859 case ROUND_DIV_EXPR:
2860 case EXACT_DIV_EXPR:
2861 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE
2862 || code0 == COMPLEX_TYPE)
2863 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
2864 || code1 == COMPLEX_TYPE))
2866 if (TREE_CODE (op1) == INTEGER_CST && integer_zerop (op1))
2867 warning ("division by zero in `%E / 0'", op0);
2868 else if (TREE_CODE (op1) == REAL_CST && real_zerop (op1))
2869 warning ("division by zero in `%E / 0.'", op0);
2871 if (!(code0 == INTEGER_TYPE && code1 == INTEGER_TYPE))
2872 resultcode = RDIV_EXPR;
2874 /* When dividing two signed integers, we have to promote to int.
2875 unless we divide by a constant != -1. Note that default
2876 conversion will have been performed on the operands at this
2877 point, so we have to dig out the original type to find out if
2879 shorten = ((TREE_CODE (op0) == NOP_EXPR
2880 && TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (op0, 0))))
2881 || (TREE_CODE (op1) == INTEGER_CST
2882 && ! integer_all_onesp (op1)));
2891 if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
2895 case TRUNC_MOD_EXPR:
2896 case FLOOR_MOD_EXPR:
2897 if (code1 == INTEGER_TYPE && integer_zerop (op1))
2898 warning ("division by zero in `%E %% 0'", op0);
2899 else if (code1 == REAL_TYPE && real_zerop (op1))
2900 warning ("division by zero in `%E %% 0.'", op0);
2902 if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
2904 /* Although it would be tempting to shorten always here, that loses
2905 on some targets, since the modulo instruction is undefined if the
2906 quotient can't be represented in the computation mode. We shorten
2907 only if unsigned or if dividing by something we know != -1. */
2908 shorten = ((TREE_CODE (op0) == NOP_EXPR
2909 && TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (op0, 0))))
2910 || (TREE_CODE (op1) == INTEGER_CST
2911 && ! integer_all_onesp (op1)));
2916 case TRUTH_ANDIF_EXPR:
2917 case TRUTH_ORIF_EXPR:
2918 case TRUTH_AND_EXPR:
2920 result_type = boolean_type_node;
2923 /* Shift operations: result has same type as first operand;
2924 always convert second operand to int.
2925 Also set SHORT_SHIFT if shifting rightward. */
2928 if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
2930 result_type = type0;
2931 if (TREE_CODE (op1) == INTEGER_CST)
2933 if (tree_int_cst_lt (op1, integer_zero_node))
2934 warning ("right shift count is negative");
2937 if (! integer_zerop (op1))
2939 if (compare_tree_int (op1, TYPE_PRECISION (type0)) >= 0)
2940 warning ("right shift count >= width of type");
2943 /* Convert the shift-count to an integer, regardless of
2944 size of value being shifted. */
2945 if (TYPE_MAIN_VARIANT (TREE_TYPE (op1)) != integer_type_node)
2946 op1 = cp_convert (integer_type_node, op1);
2947 /* Avoid converting op1 to result_type later. */
2953 if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
2955 result_type = type0;
2956 if (TREE_CODE (op1) == INTEGER_CST)
2958 if (tree_int_cst_lt (op1, integer_zero_node))
2959 warning ("left shift count is negative");
2960 else if (compare_tree_int (op1, TYPE_PRECISION (type0)) >= 0)
2961 warning ("left shift count >= width of type");
2963 /* Convert the shift-count to an integer, regardless of
2964 size of value being shifted. */
2965 if (TYPE_MAIN_VARIANT (TREE_TYPE (op1)) != integer_type_node)
2966 op1 = cp_convert (integer_type_node, op1);
2967 /* Avoid converting op1 to result_type later. */
2974 if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
2976 result_type = type0;
2977 if (TREE_CODE (op1) == INTEGER_CST)
2979 if (tree_int_cst_lt (op1, integer_zero_node))
2980 warning ("%s rotate count is negative",
2981 (code == LROTATE_EXPR) ? "left" : "right");
2982 else if (compare_tree_int (op1, TYPE_PRECISION (type0)) >= 0)
2983 warning ("%s rotate count >= width of type",
2984 (code == LROTATE_EXPR) ? "left" : "right");
2986 /* Convert the shift-count to an integer, regardless of
2987 size of value being shifted. */
2988 if (TYPE_MAIN_VARIANT (TREE_TYPE (op1)) != integer_type_node)
2989 op1 = cp_convert (integer_type_node, op1);
2995 if (warn_float_equal && (code0 == REAL_TYPE || code1 == REAL_TYPE))
2996 warning ("comparing floating point with == or != is unsafe");
2998 build_type = boolean_type_node;
2999 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE
3000 || code0 == COMPLEX_TYPE)
3001 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
3002 || code1 == COMPLEX_TYPE))
3004 else if ((code0 == POINTER_TYPE && code1 == POINTER_TYPE)
3005 || (TYPE_PTRMEM_P (type0) && TYPE_PTRMEM_P (type1)))
3006 result_type = composite_pointer_type (type0, type1, op0, op1,
3008 else if ((code0 == POINTER_TYPE || TYPE_PTRMEM_P (type0))
3009 && null_ptr_cst_p (op1))
3010 result_type = type0;
3011 else if ((code1 == POINTER_TYPE || TYPE_PTRMEM_P (type1))
3012 && null_ptr_cst_p (op0))
3013 result_type = type1;
3014 else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
3016 result_type = type0;
3017 error ("ISO C++ forbids comparison between pointer and integer");
3019 else if (code0 == INTEGER_TYPE && code1 == POINTER_TYPE)
3021 result_type = type1;
3022 error ("ISO C++ forbids comparison between pointer and integer");
3024 else if (TYPE_PTRMEMFUNC_P (type0) && null_ptr_cst_p (op1))
3026 op0 = build_ptrmemfunc_access_expr (op0, pfn_identifier);
3027 op1 = cp_convert (TREE_TYPE (op0), integer_zero_node);
3028 result_type = TREE_TYPE (op0);
3030 else if (TYPE_PTRMEMFUNC_P (type1) && null_ptr_cst_p (op0))
3031 return cp_build_binary_op (code, op1, op0);
3032 else if (TYPE_PTRMEMFUNC_P (type0) && TYPE_PTRMEMFUNC_P (type1)
3033 && same_type_p (type0, type1))
3035 /* E will be the final comparison. */
3037 /* E1 and E2 are for scratch. */
3045 if (TREE_SIDE_EFFECTS (op0))
3046 op0 = save_expr (op0);
3047 if (TREE_SIDE_EFFECTS (op1))
3048 op1 = save_expr (op1);
3053 && (!op0.pfn || op0.delta == op1.delta))
3055 The reason for the `!op0.pfn' bit is that a NULL
3056 pointer-to-member is any member with a zero PFN; the
3057 DELTA field is unspecified. */
3058 pfn0 = pfn_from_ptrmemfunc (op0);
3059 pfn1 = pfn_from_ptrmemfunc (op1);
3060 delta0 = build_ptrmemfunc_access_expr (op0,
3062 delta1 = build_ptrmemfunc_access_expr (op1,
3064 e1 = cp_build_binary_op (EQ_EXPR, delta0, delta1);
3065 e2 = cp_build_binary_op (EQ_EXPR,
3067 cp_convert (TREE_TYPE (pfn0),
3068 integer_zero_node));
3069 e1 = cp_build_binary_op (TRUTH_ORIF_EXPR, e1, e2);
3070 e2 = build (EQ_EXPR, boolean_type_node, pfn0, pfn1);
3071 e = cp_build_binary_op (TRUTH_ANDIF_EXPR, e2, e1);
3072 if (code == EQ_EXPR)
3074 return cp_build_binary_op (EQ_EXPR, e, integer_zero_node);
3076 else if ((TYPE_PTRMEMFUNC_P (type0)
3077 && same_type_p (TYPE_PTRMEMFUNC_FN_TYPE (type0), type1))
3078 || (TYPE_PTRMEMFUNC_P (type1)
3079 && same_type_p (TYPE_PTRMEMFUNC_FN_TYPE (type1), type0)))
3085 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE)
3086 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE))
3088 else if (code0 == POINTER_TYPE && code1 == POINTER_TYPE)
3089 result_type = composite_pointer_type (type0, type1, op0, op1,
3097 build_type = boolean_type_node;
3098 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE)
3099 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE))
3101 else if (code0 == POINTER_TYPE && code1 == POINTER_TYPE)
3102 result_type = composite_pointer_type (type0, type1, op0, op1,
3104 else if (code0 == POINTER_TYPE && TREE_CODE (op1) == INTEGER_CST
3105 && integer_zerop (op1))
3106 result_type = type0;
3107 else if (code1 == POINTER_TYPE && TREE_CODE (op0) == INTEGER_CST
3108 && integer_zerop (op0))
3109 result_type = type1;
3110 else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
3112 result_type = type0;
3113 pedwarn ("ISO C++ forbids comparison between pointer and integer");
3115 else if (code0 == INTEGER_TYPE && code1 == POINTER_TYPE)
3117 result_type = type1;
3118 pedwarn ("ISO C++ forbids comparison between pointer and integer");
3122 case UNORDERED_EXPR:
3129 build_type = integer_type_node;
3130 if (code0 != REAL_TYPE || code1 != REAL_TYPE)
3132 error ("unordered comparison on non-floating point argument");
3133 return error_mark_node;
3142 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE || code0 == COMPLEX_TYPE)
3144 (code1 == INTEGER_TYPE || code1 == REAL_TYPE || code1 == COMPLEX_TYPE))
3146 int none_complex = (code0 != COMPLEX_TYPE && code1 != COMPLEX_TYPE);
3148 if (shorten || common || short_compare)
3149 result_type = common_type (type0, type1);
3151 /* For certain operations (which identify themselves by shorten != 0)
3152 if both args were extended from the same smaller type,
3153 do the arithmetic in that type and then extend.
3155 shorten !=0 and !=1 indicates a bitwise operation.
3156 For them, this optimization is safe only if
3157 both args are zero-extended or both are sign-extended.
3158 Otherwise, we might change the result.
3159 Eg, (short)-1 | (unsigned short)-1 is (int)-1
3160 but calculated in (unsigned short) it would be (unsigned short)-1. */
3162 if (shorten && none_complex)
3164 int unsigned0, unsigned1;
3165 tree arg0 = get_narrower (op0, &unsigned0);
3166 tree arg1 = get_narrower (op1, &unsigned1);
3167 /* UNS is 1 if the operation to be done is an unsigned one. */
3168 int uns = TYPE_UNSIGNED (result_type);
3171 final_type = result_type;
3173 /* Handle the case that OP0 does not *contain* a conversion
3174 but it *requires* conversion to FINAL_TYPE. */
3176 if (op0 == arg0 && TREE_TYPE (op0) != final_type)
3177 unsigned0 = TYPE_UNSIGNED (TREE_TYPE (op0));
3178 if (op1 == arg1 && TREE_TYPE (op1) != final_type)
3179 unsigned1 = TYPE_UNSIGNED (TREE_TYPE (op1));
3181 /* Now UNSIGNED0 is 1 if ARG0 zero-extends to FINAL_TYPE. */
3183 /* For bitwise operations, signedness of nominal type
3184 does not matter. Consider only how operands were extended. */
3188 /* Note that in all three cases below we refrain from optimizing
3189 an unsigned operation on sign-extended args.
3190 That would not be valid. */
3192 /* Both args variable: if both extended in same way
3193 from same width, do it in that width.
3194 Do it unsigned if args were zero-extended. */
3195 if ((TYPE_PRECISION (TREE_TYPE (arg0))
3196 < TYPE_PRECISION (result_type))
3197 && (TYPE_PRECISION (TREE_TYPE (arg1))
3198 == TYPE_PRECISION (TREE_TYPE (arg0)))
3199 && unsigned0 == unsigned1
3200 && (unsigned0 || !uns))
3201 result_type = c_common_signed_or_unsigned_type
3202 (unsigned0, common_type (TREE_TYPE (arg0), TREE_TYPE (arg1)));
3203 else if (TREE_CODE (arg0) == INTEGER_CST
3204 && (unsigned1 || !uns)
3205 && (TYPE_PRECISION (TREE_TYPE (arg1))
3206 < TYPE_PRECISION (result_type))
3207 && (type = c_common_signed_or_unsigned_type
3208 (unsigned1, TREE_TYPE (arg1)),
3209 int_fits_type_p (arg0, type)))
3211 else if (TREE_CODE (arg1) == INTEGER_CST
3212 && (unsigned0 || !uns)
3213 && (TYPE_PRECISION (TREE_TYPE (arg0))
3214 < TYPE_PRECISION (result_type))
3215 && (type = c_common_signed_or_unsigned_type
3216 (unsigned0, TREE_TYPE (arg0)),
3217 int_fits_type_p (arg1, type)))
3221 /* Shifts can be shortened if shifting right. */
3226 tree arg0 = get_narrower (op0, &unsigned_arg);
3228 final_type = result_type;
3230 if (arg0 == op0 && final_type == TREE_TYPE (op0))
3231 unsigned_arg = TYPE_UNSIGNED (TREE_TYPE (op0));
3233 if (TYPE_PRECISION (TREE_TYPE (arg0)) < TYPE_PRECISION (result_type)
3234 /* We can shorten only if the shift count is less than the
3235 number of bits in the smaller type size. */
3236 && compare_tree_int (op1, TYPE_PRECISION (TREE_TYPE (arg0))) < 0
3237 /* If arg is sign-extended and then unsigned-shifted,
3238 we can simulate this with a signed shift in arg's type
3239 only if the extended result is at least twice as wide
3240 as the arg. Otherwise, the shift could use up all the
3241 ones made by sign-extension and bring in zeros.
3242 We can't optimize that case at all, but in most machines
3243 it never happens because available widths are 2**N. */
3244 && (!TYPE_UNSIGNED (final_type)
3246 || (((unsigned) 2 * TYPE_PRECISION (TREE_TYPE (arg0)))
3247 <= TYPE_PRECISION (result_type))))
3249 /* Do an unsigned shift if the operand was zero-extended. */
3251 = c_common_signed_or_unsigned_type (unsigned_arg,
3253 /* Convert value-to-be-shifted to that type. */
3254 if (TREE_TYPE (op0) != result_type)
3255 op0 = cp_convert (result_type, op0);
3260 /* Comparison operations are shortened too but differently.
3261 They identify themselves by setting short_compare = 1. */
3265 /* Don't write &op0, etc., because that would prevent op0
3266 from being kept in a register.
3267 Instead, make copies of the our local variables and
3268 pass the copies by reference, then copy them back afterward. */
3269 tree xop0 = op0, xop1 = op1, xresult_type = result_type;
3270 enum tree_code xresultcode = resultcode;
3272 = shorten_compare (&xop0, &xop1, &xresult_type, &xresultcode);
3274 return cp_convert (boolean_type_node, val);
3275 op0 = xop0, op1 = xop1;
3277 resultcode = xresultcode;
3280 if ((short_compare || code == MIN_EXPR || code == MAX_EXPR)
3281 && warn_sign_compare
3282 /* Do not warn until the template is instantiated; we cannot
3283 bound the ranges of the arguments until that point. */
3284 && !processing_template_decl)
3286 int op0_signed = !TYPE_UNSIGNED (TREE_TYPE (orig_op0));
3287 int op1_signed = !TYPE_UNSIGNED (TREE_TYPE (orig_op1));
3289 int unsignedp0, unsignedp1;
3290 tree primop0 = get_narrower (op0, &unsignedp0);
3291 tree primop1 = get_narrower (op1, &unsignedp1);
3293 /* Check for comparison of different enum types. */
3294 if (TREE_CODE (TREE_TYPE (orig_op0)) == ENUMERAL_TYPE
3295 && TREE_CODE (TREE_TYPE (orig_op1)) == ENUMERAL_TYPE
3296 && TYPE_MAIN_VARIANT (TREE_TYPE (orig_op0))
3297 != TYPE_MAIN_VARIANT (TREE_TYPE (orig_op1)))
3299 warning ("comparison between types `%#T' and `%#T'",
3300 TREE_TYPE (orig_op0), TREE_TYPE (orig_op1));
3303 /* Give warnings for comparisons between signed and unsigned
3304 quantities that may fail. */
3305 /* Do the checking based on the original operand trees, so that
3306 casts will be considered, but default promotions won't be. */
3308 /* Do not warn if the comparison is being done in a signed type,
3309 since the signed type will only be chosen if it can represent
3310 all the values of the unsigned type. */
3311 if (!TYPE_UNSIGNED (result_type))
3313 /* Do not warn if both operands are unsigned. */
3314 else if (op0_signed == op1_signed)
3316 /* Do not warn if the signed quantity is an unsuffixed
3317 integer literal (or some static constant expression
3318 involving such literals or a conditional expression
3319 involving such literals) and it is non-negative. */
3320 else if ((op0_signed && tree_expr_nonnegative_p (orig_op0))
3321 || (op1_signed && tree_expr_nonnegative_p (orig_op1)))
3323 /* Do not warn if the comparison is an equality operation,
3324 the unsigned quantity is an integral constant and it does
3325 not use the most significant bit of result_type. */
3326 else if ((resultcode == EQ_EXPR || resultcode == NE_EXPR)
3327 && ((op0_signed && TREE_CODE (orig_op1) == INTEGER_CST
3328 && int_fits_type_p (orig_op1, c_common_signed_type
3330 || (op1_signed && TREE_CODE (orig_op0) == INTEGER_CST
3331 && int_fits_type_p (orig_op0, c_common_signed_type
3335 warning ("comparison between signed and unsigned integer expressions");
3337 /* Warn if two unsigned values are being compared in a size
3338 larger than their original size, and one (and only one) is the
3339 result of a `~' operator. This comparison will always fail.
3341 Also warn if one operand is a constant, and the constant does not
3342 have all bits set that are set in the ~ operand when it is
3345 if ((TREE_CODE (primop0) == BIT_NOT_EXPR)
3346 ^ (TREE_CODE (primop1) == BIT_NOT_EXPR))
3348 if (TREE_CODE (primop0) == BIT_NOT_EXPR)
3349 primop0 = get_narrower (TREE_OPERAND (op0, 0), &unsignedp0);
3350 if (TREE_CODE (primop1) == BIT_NOT_EXPR)
3351 primop1 = get_narrower (TREE_OPERAND (op1, 0), &unsignedp1);
3353 if (host_integerp (primop0, 0) || host_integerp (primop1, 0))
3356 HOST_WIDE_INT constant, mask;
3360 if (host_integerp (primop0, 0))
3363 unsignedp = unsignedp1;
3364 constant = tree_low_cst (primop0, 0);
3369 unsignedp = unsignedp0;
3370 constant = tree_low_cst (primop1, 0);
3373 bits = TYPE_PRECISION (TREE_TYPE (primop));
3374 if (bits < TYPE_PRECISION (result_type)
3375 && bits < HOST_BITS_PER_LONG && unsignedp)
3377 mask = (~ (HOST_WIDE_INT) 0) << bits;
3378 if ((mask & constant) != mask)
3379 warning ("comparison of promoted ~unsigned with constant");
3382 else if (unsignedp0 && unsignedp1
3383 && (TYPE_PRECISION (TREE_TYPE (primop0))
3384 < TYPE_PRECISION (result_type))
3385 && (TYPE_PRECISION (TREE_TYPE (primop1))
3386 < TYPE_PRECISION (result_type)))
3387 warning ("comparison of promoted ~unsigned with unsigned");
3392 /* At this point, RESULT_TYPE must be nonzero to avoid an error message.
3393 If CONVERTED is zero, both args will be converted to type RESULT_TYPE.
3394 Then the expression will be built.
3395 It will be given type FINAL_TYPE if that is nonzero;
3396 otherwise, it will be given type RESULT_TYPE. */
3400 error ("invalid operands of types `%T' and `%T' to binary `%O'",
3401 TREE_TYPE (orig_op0), TREE_TYPE (orig_op1), code);
3402 return error_mark_node;
3405 /* Issue warnings about peculiar, but valid, uses of NULL. */
3406 if (/* It's reasonable to use pointer values as operands of &&
3407 and ||, so NULL is no exception. */
3408 !(code == TRUTH_ANDIF_EXPR || code == TRUTH_ORIF_EXPR)
3409 && (/* If OP0 is NULL and OP1 is not a pointer, or vice versa. */
3410 (orig_op0 == null_node
3411 && TREE_CODE (TREE_TYPE (op1)) != POINTER_TYPE)
3412 /* Or vice versa. */
3413 || (orig_op1 == null_node
3414 && TREE_CODE (TREE_TYPE (op0)) != POINTER_TYPE)
3415 /* Or, both are NULL and the operation was not a comparison. */
3416 || (orig_op0 == null_node && orig_op1 == null_node
3417 && code != EQ_EXPR && code != NE_EXPR)))
3418 /* Some sort of arithmetic operation involving NULL was
3419 performed. Note that pointer-difference and pointer-addition
3420 have already been handled above, and so we don't end up here in
3422 warning ("NULL used in arithmetic");
3426 if (TREE_TYPE (op0) != result_type)
3427 op0 = cp_convert (result_type, op0);
3428 if (TREE_TYPE (op1) != result_type)
3429 op1 = cp_convert (result_type, op1);
3431 if (op0 == error_mark_node || op1 == error_mark_node)
3432 return error_mark_node;
3435 if (build_type == NULL_TREE)
3436 build_type = result_type;
3439 tree result = build (resultcode, build_type, op0, op1);
3442 folded = fold (result);
3443 if (folded == result)
3444 TREE_CONSTANT (folded) = TREE_CONSTANT (op0) & TREE_CONSTANT (op1);
3445 if (final_type != 0)
3446 return cp_convert (final_type, folded);
3451 /* Return a tree for the sum or difference (RESULTCODE says which)
3452 of pointer PTROP and integer INTOP. */
3455 cp_pointer_int_sum (enum tree_code resultcode, tree ptrop, tree intop)
3457 tree res_type = TREE_TYPE (ptrop);
3459 /* pointer_int_sum() uses size_in_bytes() on the TREE_TYPE(res_type)
3460 in certain circumstance (when it's valid to do so). So we need
3461 to make sure it's complete. We don't need to check here, if we
3462 can actually complete it at all, as those checks will be done in
3463 pointer_int_sum() anyway. */
3464 complete_type (TREE_TYPE (res_type));
3466 return pointer_int_sum (resultcode, ptrop, fold (intop));
3469 /* Return a tree for the difference of pointers OP0 and OP1.
3470 The resulting tree has type int. */
3473 pointer_diff (tree op0, tree op1, tree ptrtype)
3475 tree result, folded;
3476 tree restype = ptrdiff_type_node;
3477 tree target_type = TREE_TYPE (ptrtype);
3479 if (!complete_type_or_else (target_type, NULL_TREE))
3480 return error_mark_node;
3482 if (pedantic || warn_pointer_arith)
3484 if (TREE_CODE (target_type) == VOID_TYPE)
3485 pedwarn ("ISO C++ forbids using pointer of type `void *' in subtraction");
3486 if (TREE_CODE (target_type) == FUNCTION_TYPE)
3487 pedwarn ("ISO C++ forbids using pointer to a function in subtraction");
3488 if (TREE_CODE (target_type) == METHOD_TYPE)
3489 pedwarn ("ISO C++ forbids using pointer to a method in subtraction");
3492 /* First do the subtraction as integers;
3493 then drop through to build the divide operator. */
3495 op0 = cp_build_binary_op (MINUS_EXPR,
3496 cp_convert (restype, op0),
3497 cp_convert (restype, op1));
3499 /* This generates an error if op1 is a pointer to an incomplete type. */
3500 if (!COMPLETE_TYPE_P (TREE_TYPE (TREE_TYPE (op1))))
3501 error ("invalid use of a pointer to an incomplete type in pointer arithmetic");
3503 op1 = (TYPE_PTROB_P (ptrtype)
3504 ? size_in_bytes (target_type)
3505 : integer_one_node);
3507 /* Do the division. */
3509 result = build (EXACT_DIV_EXPR, restype, op0, cp_convert (restype, op1));
3511 folded = fold (result);
3512 if (folded == result)
3513 TREE_CONSTANT (folded) = TREE_CONSTANT (op0) & TREE_CONSTANT (op1);
3517 /* Construct and perhaps optimize a tree representation
3518 for a unary operation. CODE, a tree_code, specifies the operation
3519 and XARG is the operand. */
3522 build_x_unary_op (enum tree_code code, tree xarg)
3524 tree orig_expr = xarg;
3528 if (processing_template_decl)
3530 if (type_dependent_expression_p (xarg))
3531 return build_min_nt (code, xarg, NULL_TREE);
3532 xarg = build_non_dependent_expr (xarg);
3537 /* & rec, on incomplete RECORD_TYPEs is the simple opr &, not an
3539 if (code == ADDR_EXPR
3540 && TREE_CODE (xarg) != TEMPLATE_ID_EXPR
3541 && ((IS_AGGR_TYPE_CODE (TREE_CODE (TREE_TYPE (xarg)))
3542 && !COMPLETE_TYPE_P (TREE_TYPE (xarg)))
3543 || (TREE_CODE (xarg) == OFFSET_REF)))
3544 /* Don't look for a function. */;
3546 exp = build_new_op (code, LOOKUP_NORMAL, xarg, NULL_TREE, NULL_TREE,
3547 /*overloaded_p=*/NULL);
3548 if (!exp && code == ADDR_EXPR)
3550 /* A pointer to member-function can be formed only by saying
3552 if (!flag_ms_extensions && TREE_CODE (TREE_TYPE (xarg)) == METHOD_TYPE
3553 && (TREE_CODE (xarg) != OFFSET_REF || !PTRMEM_OK_P (xarg)))
3555 if (TREE_CODE (xarg) != OFFSET_REF)
3557 error ("invalid use of '%E' to form a pointer-to-member-function. Use a qualified-id.",
3559 return error_mark_node;
3563 error ("parenthesis around '%E' cannot be used to form a pointer-to-member-function",
3565 PTRMEM_OK_P (xarg) = 1;
3569 if (TREE_CODE (xarg) == OFFSET_REF)
3571 ptrmem = PTRMEM_OK_P (xarg);
3573 if (!ptrmem && !flag_ms_extensions
3574 && TREE_CODE (TREE_TYPE (TREE_OPERAND (xarg, 1))) == METHOD_TYPE)
3576 /* A single non-static member, make sure we don't allow a
3577 pointer-to-member. */
3578 xarg = build (OFFSET_REF, TREE_TYPE (xarg),
3579 TREE_OPERAND (xarg, 0),
3580 ovl_cons (TREE_OPERAND (xarg, 1), NULL_TREE));
3581 PTRMEM_OK_P (xarg) = ptrmem;
3584 else if (TREE_CODE (xarg) == TARGET_EXPR)
3585 warning ("taking address of temporary");
3586 exp = build_unary_op (ADDR_EXPR, xarg, 0);
3587 if (TREE_CODE (exp) == ADDR_EXPR)
3588 PTRMEM_OK_P (exp) = ptrmem;
3591 if (processing_template_decl && exp != error_mark_node)
3592 return build_min_non_dep (code, exp, orig_expr,
3593 /*For {PRE,POST}{INC,DEC}REMENT_EXPR*/NULL_TREE);
3597 /* Like c_common_truthvalue_conversion, but handle pointer-to-member
3598 constants, where a null value is represented by an INTEGER_CST of
3602 cp_truthvalue_conversion (tree expr)
3604 tree type = TREE_TYPE (expr);
3605 if (TYPE_PTRMEM_P (type))
3606 return build_binary_op (NE_EXPR, expr, integer_zero_node, 1);
3608 return c_common_truthvalue_conversion (expr);
3611 /* Just like cp_truthvalue_conversion, but we want a CLEANUP_POINT_EXPR. */
3614 condition_conversion (tree expr)
3617 if (processing_template_decl)
3619 t = perform_implicit_conversion (boolean_type_node, expr);
3620 t = fold (build1 (CLEANUP_POINT_EXPR, boolean_type_node, t));
3624 /* Return an ADDR_EXPR giving the address of T. This function
3625 attempts no optimizations or simplifications; it is a low-level
3629 build_address (tree t)
3633 if (error_operand_p (t) || !cxx_mark_addressable (t))
3634 return error_mark_node;
3636 addr = build1 (ADDR_EXPR, build_pointer_type (TREE_TYPE (t)), t);
3638 TREE_CONSTANT (addr) = 1;
3643 /* Return a NOP_EXPR converting EXPR to TYPE. */
3646 build_nop (tree type, tree expr)
3650 if (type == error_mark_node || error_operand_p (expr))
3653 nop = build1 (NOP_EXPR, type, expr);
3654 if (TREE_CONSTANT (expr))
3655 TREE_CONSTANT (nop) = 1;
3660 /* C++: Must handle pointers to members.
3662 Perhaps type instantiation should be extended to handle conversion
3663 from aggregates to types we don't yet know we want? (Or are those
3664 cases typically errors which should be reported?)
3666 NOCONVERT nonzero suppresses the default promotions
3667 (such as from short to int). */
3670 build_unary_op (enum tree_code code, tree xarg, int noconvert)
3672 /* No default_conversion here. It causes trouble for ADDR_EXPR. */
3675 const char *errstring = NULL;
3678 if (arg == error_mark_node)
3679 return error_mark_node;
3684 /* This is used for unary plus, because a CONVERT_EXPR
3685 is enough to prevent anybody from looking inside for
3686 associativity, but won't generate any code. */
3687 if (!(arg = build_expr_type_conversion
3688 (WANT_ARITH | WANT_ENUM | WANT_POINTER, arg, true)))
3689 errstring = "wrong type argument to unary plus";
3693 arg = default_conversion (arg);
3694 arg = build1 (NON_LVALUE_EXPR, TREE_TYPE (arg), arg);
3695 TREE_CONSTANT (arg) = TREE_CONSTANT (TREE_OPERAND (arg, 0));
3700 if (!(arg = build_expr_type_conversion (WANT_ARITH | WANT_ENUM, arg, true)))
3701 errstring = "wrong type argument to unary minus";
3702 else if (!noconvert && CP_INTEGRAL_TYPE_P (TREE_TYPE (arg)))
3703 arg = perform_integral_promotions (arg);
3707 if (TREE_CODE (TREE_TYPE (arg)) == COMPLEX_TYPE)
3711 arg = default_conversion (arg);
3713 else if (!(arg = build_expr_type_conversion (WANT_INT | WANT_ENUM,
3715 errstring = "wrong type argument to bit-complement";
3716 else if (!noconvert)
3717 arg = perform_integral_promotions (arg);
3721 if (!(arg = build_expr_type_conversion (WANT_ARITH | WANT_ENUM, arg, true)))
3722 errstring = "wrong type argument to abs";
3723 else if (!noconvert)
3724 arg = default_conversion (arg);
3728 /* Conjugating a real value is a no-op, but allow it anyway. */
3729 if (!(arg = build_expr_type_conversion (WANT_ARITH | WANT_ENUM, arg, true)))
3730 errstring = "wrong type argument to conjugation";
3731 else if (!noconvert)
3732 arg = default_conversion (arg);
3735 case TRUTH_NOT_EXPR:
3736 arg = perform_implicit_conversion (boolean_type_node, arg);
3737 val = invert_truthvalue (arg);
3738 if (arg != error_mark_node)
3740 errstring = "in argument to unary !";
3747 if (TREE_CODE (arg) == COMPLEX_CST)
3748 return TREE_REALPART (arg);
3749 else if (TREE_CODE (TREE_TYPE (arg)) == COMPLEX_TYPE)
3750 return fold (build1 (REALPART_EXPR, TREE_TYPE (TREE_TYPE (arg)), arg));
3755 if (TREE_CODE (arg) == COMPLEX_CST)
3756 return TREE_IMAGPART (arg);
3757 else if (TREE_CODE (TREE_TYPE (arg)) == COMPLEX_TYPE)
3758 return fold (build1 (IMAGPART_EXPR, TREE_TYPE (TREE_TYPE (arg)), arg));
3760 return cp_convert (TREE_TYPE (arg), integer_zero_node);
3762 case PREINCREMENT_EXPR:
3763 case POSTINCREMENT_EXPR:
3764 case PREDECREMENT_EXPR:
3765 case POSTDECREMENT_EXPR:
3766 /* Handle complex lvalues (when permitted)
3767 by reduction to simpler cases. */
3769 val = unary_complex_lvalue (code, arg);
3773 /* Increment or decrement the real part of the value,
3774 and don't change the imaginary part. */
3775 if (TREE_CODE (TREE_TYPE (arg)) == COMPLEX_TYPE)
3779 arg = stabilize_reference (arg);
3780 real = build_unary_op (REALPART_EXPR, arg, 1);
3781 imag = build_unary_op (IMAGPART_EXPR, arg, 1);
3782 return build (COMPLEX_EXPR, TREE_TYPE (arg),
3783 build_unary_op (code, real, 1), imag);
3786 /* Report invalid types. */
3788 if (!(arg = build_expr_type_conversion (WANT_ARITH | WANT_POINTER,
3791 if (code == PREINCREMENT_EXPR)
3792 errstring ="no pre-increment operator for type";
3793 else if (code == POSTINCREMENT_EXPR)
3794 errstring ="no post-increment operator for type";
3795 else if (code == PREDECREMENT_EXPR)
3796 errstring ="no pre-decrement operator for type";
3798 errstring ="no post-decrement operator for type";
3802 /* Report something read-only. */
3804 if (CP_TYPE_CONST_P (TREE_TYPE (arg))
3805 || TREE_READONLY (arg))
3806 readonly_error (arg, ((code == PREINCREMENT_EXPR
3807 || code == POSTINCREMENT_EXPR)
3808 ? "increment" : "decrement"),
3813 tree result_type = TREE_TYPE (arg);
3815 arg = get_unwidened (arg, 0);
3816 argtype = TREE_TYPE (arg);
3818 /* ARM $5.2.5 last annotation says this should be forbidden. */
3819 if (TREE_CODE (argtype) == ENUMERAL_TYPE)
3820 pedwarn ("ISO C++ forbids %sing an enum",
3821 (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
3822 ? "increment" : "decrement");
3824 /* Compute the increment. */
3826 if (TREE_CODE (argtype) == POINTER_TYPE)
3828 tree type = complete_type (TREE_TYPE (argtype));
3830 if (!COMPLETE_OR_VOID_TYPE_P (type))
3831 error ("cannot %s a pointer to incomplete type `%T'",
3832 ((code == PREINCREMENT_EXPR
3833 || code == POSTINCREMENT_EXPR)
3834 ? "increment" : "decrement"), TREE_TYPE (argtype));
3835 else if ((pedantic || warn_pointer_arith)
3836 && !TYPE_PTROB_P (argtype))
3837 pedwarn ("ISO C++ forbids %sing a pointer of type `%T'",
3838 ((code == PREINCREMENT_EXPR
3839 || code == POSTINCREMENT_EXPR)
3840 ? "increment" : "decrement"), argtype);
3841 inc = cxx_sizeof_nowarn (TREE_TYPE (argtype));
3844 inc = integer_one_node;
3846 inc = cp_convert (argtype, inc);
3848 /* Handle incrementing a cast-expression. */
3850 switch (TREE_CODE (arg))
3855 case FIX_TRUNC_EXPR:
3856 case FIX_FLOOR_EXPR:
3857 case FIX_ROUND_EXPR:
3860 tree incremented, modify, value, compound;
3861 if (! lvalue_p (arg) && pedantic)
3862 pedwarn ("cast to non-reference type used as lvalue");
3863 arg = stabilize_reference (arg);
3864 if (code == PREINCREMENT_EXPR || code == PREDECREMENT_EXPR)
3867 value = save_expr (arg);
3868 incremented = build (((code == PREINCREMENT_EXPR
3869 || code == POSTINCREMENT_EXPR)
3870 ? PLUS_EXPR : MINUS_EXPR),
3871 argtype, value, inc);
3873 modify = build_modify_expr (arg, NOP_EXPR, incremented);
3874 compound = build (COMPOUND_EXPR, TREE_TYPE (arg), modify, value);
3876 /* Eliminate warning about unused result of + or -. */
3877 TREE_NO_UNUSED_WARNING (compound) = 1;
3885 /* Complain about anything else that is not a true lvalue. */
3886 if (!lvalue_or_else (arg, ((code == PREINCREMENT_EXPR
3887 || code == POSTINCREMENT_EXPR)
3888 ? "increment" : "decrement")))
3889 return error_mark_node;
3891 /* Forbid using -- on `bool'. */
3892 if (TREE_TYPE (arg) == boolean_type_node)
3894 if (code == POSTDECREMENT_EXPR || code == PREDECREMENT_EXPR)
3896 error ("invalid use of `--' on bool variable `%D'", arg);
3897 return error_mark_node;
3899 val = boolean_increment (code, arg);
3902 val = build (code, TREE_TYPE (arg), arg, inc);
3904 TREE_SIDE_EFFECTS (val) = 1;
3905 return cp_convert (result_type, val);
3909 /* Note that this operation never does default_conversion
3910 regardless of NOCONVERT. */
3912 argtype = lvalue_type (arg);
3914 if (TREE_CODE (arg) == OFFSET_REF)
3917 if (TREE_CODE (argtype) == REFERENCE_TYPE)
3921 build_pointer_type (TREE_TYPE (argtype)), arg);
3922 TREE_CONSTANT (arg) = TREE_CONSTANT (TREE_OPERAND (arg, 0));
3925 else if (pedantic && DECL_MAIN_P (arg))
3927 pedwarn ("ISO C++ forbids taking address of function `::main'");
3929 /* Let &* cancel out to simplify resulting code. */
3930 if (TREE_CODE (arg) == INDIRECT_REF)
3932 /* We don't need to have `current_class_ptr' wrapped in a
3933 NON_LVALUE_EXPR node. */
3934 if (arg == current_class_ref)
3935 return current_class_ptr;
3937 arg = TREE_OPERAND (arg, 0);
3938 if (TREE_CODE (TREE_TYPE (arg)) == REFERENCE_TYPE)
3942 build_pointer_type (TREE_TYPE (TREE_TYPE (arg))), arg);
3943 TREE_CONSTANT (arg) = TREE_CONSTANT (TREE_OPERAND (arg, 0));
3945 else if (lvalue_p (arg))
3946 /* Don't let this be an lvalue. */
3947 return non_lvalue (arg);
3951 /* For &x[y], return x+y. */
3952 if (TREE_CODE (arg) == ARRAY_REF)
3954 if (!cxx_mark_addressable (TREE_OPERAND (arg, 0)))
3955 return error_mark_node;
3956 return cp_build_binary_op (PLUS_EXPR, TREE_OPERAND (arg, 0),
3957 TREE_OPERAND (arg, 1));
3960 /* Uninstantiated types are all functions. Taking the
3961 address of a function is a no-op, so just return the
3964 if (TREE_CODE (arg) == IDENTIFIER_NODE
3965 && IDENTIFIER_OPNAME_P (arg))
3968 /* We don't know the type yet, so just work around the problem.
3969 We know that this will resolve to an lvalue. */
3970 return build1 (ADDR_EXPR, unknown_type_node, arg);
3973 if (TREE_CODE (arg) == COMPONENT_REF && type_unknown_p (arg)
3974 && !really_overloaded_fn (TREE_OPERAND (arg, 1)))
3976 /* They're trying to take the address of a unique non-static
3977 member function. This is ill-formed (except in MS-land),
3978 but let's try to DTRT.
3979 Note: We only handle unique functions here because we don't
3980 want to complain if there's a static overload; non-unique
3981 cases will be handled by instantiate_type. But we need to
3982 handle this case here to allow casts on the resulting PMF.
3983 We could defer this in non-MS mode, but it's easier to give
3984 a useful error here. */
3986 /* Inside constant member functions, the `this' pointer
3987 contains an extra const qualifier. TYPE_MAIN_VARIANT
3988 is used here to remove this const from the diagnostics
3989 and the created OFFSET_REF. */
3990 tree base = TYPE_MAIN_VARIANT (TREE_TYPE (TREE_OPERAND (arg, 0)));
3991 tree name = DECL_NAME (get_first_fn (TREE_OPERAND (arg, 1)));
3993 if (! flag_ms_extensions)
3995 if (current_class_type
3996 && TREE_OPERAND (arg, 0) == current_class_ref)
3997 /* An expression like &memfn. */
3998 pedwarn ("ISO C++ forbids taking the address of an unqualified"
3999 " or parenthesized non-static member function to form"
4000 " a pointer to member function. Say `&%T::%D'",
4003 pedwarn ("ISO C++ forbids taking the address of a bound member"
4004 " function to form a pointer to member function."
4008 arg = build_offset_ref (base, name, /*address_p=*/true);
4012 if (type_unknown_p (arg))
4013 return build1 (ADDR_EXPR, unknown_type_node, arg);
4015 /* Handle complex lvalues (when permitted)
4016 by reduction to simpler cases. */
4017 val = unary_complex_lvalue (code, arg);
4021 switch (TREE_CODE (arg))
4026 case FIX_TRUNC_EXPR:
4027 case FIX_FLOOR_EXPR:
4028 case FIX_ROUND_EXPR:
4030 if (! lvalue_p (arg) && pedantic)
4031 pedwarn ("ISO C++ forbids taking the address of a cast to a non-lvalue expression");
4035 arg = OVL_CURRENT (arg);
4042 /* Allow the address of a constructor if all the elements
4044 if (TREE_CODE (arg) == CONSTRUCTOR && TREE_HAS_CONSTRUCTOR (arg)
4045 && TREE_CONSTANT (arg))
4047 /* Anything not already handled and not a true memory reference