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, 2005, 2006, 2007, 2008, 2009, 2010, 2011
4 Free Software Foundation, Inc.
5 Hacked by Michael Tiemann (tiemann@cygnus.com)
7 This file is part of GCC.
9 GCC is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 3, or (at your option)
14 GCC is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
19 You should have received a copy of the GNU General Public License
20 along with GCC; see the file COPYING3. If not see
21 <http://www.gnu.org/licenses/>. */
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. */
31 #include "coretypes.h"
37 #include "diagnostic.h"
41 #include "c-family/c-common.h"
42 #include "c-family/c-objc.h"
45 static tree pfn_from_ptrmemfunc (tree);
46 static tree delta_from_ptrmemfunc (tree);
47 static tree convert_for_assignment (tree, tree, impl_conv_rhs, tree, int,
49 static tree cp_pointer_int_sum (enum tree_code, tree, tree);
50 static tree rationalize_conditional_expr (enum tree_code, tree,
52 static int comp_ptr_ttypes_real (tree, tree, int);
53 static bool comp_except_types (tree, tree, bool);
54 static bool comp_array_types (const_tree, const_tree, bool);
55 static tree pointer_diff (tree, tree, tree);
56 static tree get_delta_difference (tree, tree, bool, bool, tsubst_flags_t);
57 static void casts_away_constness_r (tree *, tree *);
58 static bool casts_away_constness (tree, tree);
59 static void maybe_warn_about_returning_address_of_local (tree);
60 static tree lookup_destructor (tree, tree, tree);
61 static void warn_args_num (location_t, tree, bool);
62 static int convert_arguments (tree, VEC(tree,gc) **, tree, int,
65 /* Do `exp = require_complete_type (exp);' to make sure exp
66 does not have an incomplete type. (That includes void types.)
67 Returns error_mark_node if the VALUE does not have
68 complete type when this function returns. */
71 require_complete_type_sfinae (tree value, tsubst_flags_t complain)
75 if (processing_template_decl || value == error_mark_node)
78 if (TREE_CODE (value) == OVERLOAD)
79 type = unknown_type_node;
81 type = TREE_TYPE (value);
83 if (type == error_mark_node)
84 return error_mark_node;
86 /* First, detect a valid value with a complete type. */
87 if (COMPLETE_TYPE_P (type))
90 if (complete_type_or_maybe_complain (type, value, complain))
93 return error_mark_node;
97 require_complete_type (tree value)
99 return require_complete_type_sfinae (value, tf_warning_or_error);
102 /* Try to complete TYPE, if it is incomplete. For example, if TYPE is
103 a template instantiation, do the instantiation. Returns TYPE,
104 whether or not it could be completed, unless something goes
105 horribly wrong, in which case the error_mark_node is returned. */
108 complete_type (tree type)
110 if (type == NULL_TREE)
111 /* Rather than crash, we return something sure to cause an error
113 return error_mark_node;
115 if (type == error_mark_node || COMPLETE_TYPE_P (type))
117 else if (TREE_CODE (type) == ARRAY_TYPE && TYPE_DOMAIN (type))
119 tree t = complete_type (TREE_TYPE (type));
120 unsigned int needs_constructing, has_nontrivial_dtor;
121 if (COMPLETE_TYPE_P (t) && !dependent_type_p (type))
124 = TYPE_NEEDS_CONSTRUCTING (TYPE_MAIN_VARIANT (t));
126 = TYPE_HAS_NONTRIVIAL_DESTRUCTOR (TYPE_MAIN_VARIANT (t));
127 for (t = TYPE_MAIN_VARIANT (type); t; t = TYPE_NEXT_VARIANT (t))
129 TYPE_NEEDS_CONSTRUCTING (t) = needs_constructing;
130 TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t) = has_nontrivial_dtor;
133 else if (CLASS_TYPE_P (type) && CLASSTYPE_TEMPLATE_INSTANTIATION (type))
134 instantiate_class_template (TYPE_MAIN_VARIANT (type));
139 /* Like complete_type, but issue an error if the TYPE cannot be completed.
140 VALUE is used for informative diagnostics.
141 Returns NULL_TREE if the type cannot be made complete. */
144 complete_type_or_maybe_complain (tree type, tree value, tsubst_flags_t complain)
146 type = complete_type (type);
147 if (type == error_mark_node)
148 /* We already issued an error. */
150 else if (!COMPLETE_TYPE_P (type))
152 if (complain & tf_error)
153 cxx_incomplete_type_diagnostic (value, type, DK_ERROR);
161 complete_type_or_else (tree type, tree value)
163 return complete_type_or_maybe_complain (type, value, tf_warning_or_error);
166 /* Return truthvalue of whether type of EXP is instantiated. */
169 type_unknown_p (const_tree exp)
171 return (TREE_CODE (exp) == TREE_LIST
172 || TREE_TYPE (exp) == unknown_type_node);
176 /* Return the common type of two parameter lists.
177 We assume that comptypes has already been done and returned 1;
178 if that isn't so, this may crash.
180 As an optimization, free the space we allocate if the parameter
181 lists are already common. */
184 commonparms (tree p1, tree p2)
186 tree oldargs = p1, newargs, n;
190 len = list_length (p1);
191 newargs = tree_last (p1);
193 if (newargs == void_list_node)
202 newargs = tree_cons (NULL_TREE, NULL_TREE, newargs);
207 p1 = TREE_CHAIN (p1), p2 = TREE_CHAIN (p2), n = TREE_CHAIN (n), i++)
209 if (TREE_PURPOSE (p1) && !TREE_PURPOSE (p2))
211 TREE_PURPOSE (n) = TREE_PURPOSE (p1);
214 else if (! TREE_PURPOSE (p1))
216 if (TREE_PURPOSE (p2))
218 TREE_PURPOSE (n) = TREE_PURPOSE (p2);
224 if (1 != simple_cst_equal (TREE_PURPOSE (p1), TREE_PURPOSE (p2)))
226 TREE_PURPOSE (n) = TREE_PURPOSE (p2);
228 if (TREE_VALUE (p1) != TREE_VALUE (p2))
231 TREE_VALUE (n) = merge_types (TREE_VALUE (p1), TREE_VALUE (p2));
234 TREE_VALUE (n) = TREE_VALUE (p1);
242 /* Given a type, perhaps copied for a typedef,
243 find the "original" version of it. */
245 original_type (tree t)
247 int quals = cp_type_quals (t);
248 while (t != error_mark_node
249 && TYPE_NAME (t) != NULL_TREE)
251 tree x = TYPE_NAME (t);
252 if (TREE_CODE (x) != TYPE_DECL)
254 x = DECL_ORIGINAL_TYPE (x);
259 return cp_build_qualified_type (t, quals);
262 /* Return the common type for two arithmetic types T1 and T2 under the
263 usual arithmetic conversions. The default conversions have already
264 been applied, and enumerated types converted to their compatible
268 cp_common_type (tree t1, tree t2)
270 enum tree_code code1 = TREE_CODE (t1);
271 enum tree_code code2 = TREE_CODE (t2);
275 /* In what follows, we slightly generalize the rules given in [expr] so
276 as to deal with `long long' and `complex'. First, merge the
278 attributes = (*targetm.merge_type_attributes) (t1, t2);
280 if (SCOPED_ENUM_P (t1) || SCOPED_ENUM_P (t2))
282 if (TYPE_MAIN_VARIANT (t1) == TYPE_MAIN_VARIANT (t2))
283 return build_type_attribute_variant (t1, attributes);
288 /* FIXME: Attributes. */
289 gcc_assert (ARITHMETIC_TYPE_P (t1)
290 || TREE_CODE (t1) == VECTOR_TYPE
291 || UNSCOPED_ENUM_P (t1));
292 gcc_assert (ARITHMETIC_TYPE_P (t2)
293 || TREE_CODE (t2) == VECTOR_TYPE
294 || UNSCOPED_ENUM_P (t2));
296 /* If one type is complex, form the common type of the non-complex
297 components, then make that complex. Use T1 or T2 if it is the
299 if (code1 == COMPLEX_TYPE || code2 == COMPLEX_TYPE)
301 tree subtype1 = code1 == COMPLEX_TYPE ? TREE_TYPE (t1) : t1;
302 tree subtype2 = code2 == COMPLEX_TYPE ? TREE_TYPE (t2) : t2;
304 = type_after_usual_arithmetic_conversions (subtype1, subtype2);
306 if (code1 == COMPLEX_TYPE && TREE_TYPE (t1) == subtype)
307 return build_type_attribute_variant (t1, attributes);
308 else if (code2 == COMPLEX_TYPE && TREE_TYPE (t2) == subtype)
309 return build_type_attribute_variant (t2, attributes);
311 return build_type_attribute_variant (build_complex_type (subtype),
315 if (code1 == VECTOR_TYPE)
317 /* When we get here we should have two vectors of the same size.
318 Just prefer the unsigned one if present. */
319 if (TYPE_UNSIGNED (t1))
320 return build_type_attribute_variant (t1, attributes);
322 return build_type_attribute_variant (t2, attributes);
325 /* If only one is real, use it as the result. */
326 if (code1 == REAL_TYPE && code2 != REAL_TYPE)
327 return build_type_attribute_variant (t1, attributes);
328 if (code2 == REAL_TYPE && code1 != REAL_TYPE)
329 return build_type_attribute_variant (t2, attributes);
331 /* Both real or both integers; use the one with greater precision. */
332 if (TYPE_PRECISION (t1) > TYPE_PRECISION (t2))
333 return build_type_attribute_variant (t1, attributes);
334 else if (TYPE_PRECISION (t2) > TYPE_PRECISION (t1))
335 return build_type_attribute_variant (t2, attributes);
337 /* The types are the same; no need to do anything fancy. */
338 if (TYPE_MAIN_VARIANT (t1) == TYPE_MAIN_VARIANT (t2))
339 return build_type_attribute_variant (t1, attributes);
341 if (code1 != REAL_TYPE)
343 /* If one is unsigned long long, then convert the other to unsigned
345 if (same_type_p (TYPE_MAIN_VARIANT (t1), long_long_unsigned_type_node)
346 || same_type_p (TYPE_MAIN_VARIANT (t2), long_long_unsigned_type_node))
347 return build_type_attribute_variant (long_long_unsigned_type_node,
349 /* If one is a long long, and the other is an unsigned long, and
350 long long can represent all the values of an unsigned long, then
351 convert to a long long. Otherwise, convert to an unsigned long
352 long. Otherwise, if either operand is long long, convert the
355 Since we're here, we know the TYPE_PRECISION is the same;
356 therefore converting to long long cannot represent all the values
357 of an unsigned long, so we choose unsigned long long in that
359 if (same_type_p (TYPE_MAIN_VARIANT (t1), long_long_integer_type_node)
360 || same_type_p (TYPE_MAIN_VARIANT (t2), long_long_integer_type_node))
362 tree t = ((TYPE_UNSIGNED (t1) || TYPE_UNSIGNED (t2))
363 ? long_long_unsigned_type_node
364 : long_long_integer_type_node);
365 return build_type_attribute_variant (t, attributes);
367 if (int128_integer_type_node != NULL_TREE
368 && (same_type_p (TYPE_MAIN_VARIANT (t1),
369 int128_integer_type_node)
370 || same_type_p (TYPE_MAIN_VARIANT (t2),
371 int128_integer_type_node)))
373 tree t = ((TYPE_UNSIGNED (t1) || TYPE_UNSIGNED (t2))
374 ? int128_unsigned_type_node
375 : int128_integer_type_node);
376 return build_type_attribute_variant (t, attributes);
379 /* Go through the same procedure, but for longs. */
380 if (same_type_p (TYPE_MAIN_VARIANT (t1), long_unsigned_type_node)
381 || same_type_p (TYPE_MAIN_VARIANT (t2), long_unsigned_type_node))
382 return build_type_attribute_variant (long_unsigned_type_node,
384 if (same_type_p (TYPE_MAIN_VARIANT (t1), long_integer_type_node)
385 || same_type_p (TYPE_MAIN_VARIANT (t2), long_integer_type_node))
387 tree t = ((TYPE_UNSIGNED (t1) || TYPE_UNSIGNED (t2))
388 ? long_unsigned_type_node : long_integer_type_node);
389 return build_type_attribute_variant (t, attributes);
391 /* Otherwise prefer the unsigned one. */
392 if (TYPE_UNSIGNED (t1))
393 return build_type_attribute_variant (t1, attributes);
395 return build_type_attribute_variant (t2, attributes);
399 if (same_type_p (TYPE_MAIN_VARIANT (t1), long_double_type_node)
400 || same_type_p (TYPE_MAIN_VARIANT (t2), long_double_type_node))
401 return build_type_attribute_variant (long_double_type_node,
403 if (same_type_p (TYPE_MAIN_VARIANT (t1), double_type_node)
404 || same_type_p (TYPE_MAIN_VARIANT (t2), double_type_node))
405 return build_type_attribute_variant (double_type_node,
407 if (same_type_p (TYPE_MAIN_VARIANT (t1), float_type_node)
408 || same_type_p (TYPE_MAIN_VARIANT (t2), float_type_node))
409 return build_type_attribute_variant (float_type_node,
412 /* Two floating-point types whose TYPE_MAIN_VARIANTs are none of
413 the standard C++ floating-point types. Logic earlier in this
414 function has already eliminated the possibility that
415 TYPE_PRECISION (t2) != TYPE_PRECISION (t1), so there's no
416 compelling reason to choose one or the other. */
417 return build_type_attribute_variant (t1, attributes);
421 /* T1 and T2 are arithmetic or enumeration types. Return the type
422 that will result from the "usual arithmetic conversions" on T1 and
423 T2 as described in [expr]. */
426 type_after_usual_arithmetic_conversions (tree t1, tree t2)
428 gcc_assert (ARITHMETIC_TYPE_P (t1)
429 || TREE_CODE (t1) == VECTOR_TYPE
430 || UNSCOPED_ENUM_P (t1));
431 gcc_assert (ARITHMETIC_TYPE_P (t2)
432 || TREE_CODE (t2) == VECTOR_TYPE
433 || UNSCOPED_ENUM_P (t2));
435 /* Perform the integral promotions. We do not promote real types here. */
436 if (INTEGRAL_OR_ENUMERATION_TYPE_P (t1)
437 && INTEGRAL_OR_ENUMERATION_TYPE_P (t2))
439 t1 = type_promotes_to (t1);
440 t2 = type_promotes_to (t2);
443 return cp_common_type (t1, t2);
447 composite_pointer_error (diagnostic_t kind, tree t1, tree t2,
448 composite_pointer_operation operation)
453 emit_diagnostic (kind, input_location, 0,
454 "comparison between "
455 "distinct pointer types %qT and %qT lacks a cast",
459 emit_diagnostic (kind, input_location, 0,
460 "conversion between "
461 "distinct pointer types %qT and %qT lacks a cast",
464 case CPO_CONDITIONAL_EXPR:
465 emit_diagnostic (kind, input_location, 0,
466 "conditional expression between "
467 "distinct pointer types %qT and %qT lacks a cast",
475 /* Subroutine of composite_pointer_type to implement the recursive
476 case. See that function for documentation of the parameters. */
479 composite_pointer_type_r (tree t1, tree t2,
480 composite_pointer_operation operation,
481 tsubst_flags_t complain)
488 /* Determine the types pointed to by T1 and T2. */
489 if (TREE_CODE (t1) == POINTER_TYPE)
491 pointee1 = TREE_TYPE (t1);
492 pointee2 = TREE_TYPE (t2);
496 pointee1 = TYPE_PTRMEM_POINTED_TO_TYPE (t1);
497 pointee2 = TYPE_PTRMEM_POINTED_TO_TYPE (t2);
502 Otherwise, the composite pointer type is a pointer type
503 similar (_conv.qual_) to the type of one of the operands,
504 with a cv-qualification signature (_conv.qual_) that is the
505 union of the cv-qualification signatures of the operand
507 if (same_type_ignoring_top_level_qualifiers_p (pointee1, pointee2))
508 result_type = pointee1;
509 else if ((TREE_CODE (pointee1) == POINTER_TYPE
510 && TREE_CODE (pointee2) == POINTER_TYPE)
511 || (TYPE_PTR_TO_MEMBER_P (pointee1)
512 && TYPE_PTR_TO_MEMBER_P (pointee2)))
514 result_type = composite_pointer_type_r (pointee1, pointee2, operation,
516 if (result_type == error_mark_node)
517 return error_mark_node;
521 if (complain & tf_error)
522 composite_pointer_error (DK_PERMERROR, t1, t2, operation);
524 return error_mark_node;
525 result_type = void_type_node;
527 result_type = cp_build_qualified_type (result_type,
528 (cp_type_quals (pointee1)
529 | cp_type_quals (pointee2)));
530 /* If the original types were pointers to members, so is the
532 if (TYPE_PTR_TO_MEMBER_P (t1))
534 if (!same_type_p (TYPE_PTRMEM_CLASS_TYPE (t1),
535 TYPE_PTRMEM_CLASS_TYPE (t2)))
537 if (complain & tf_error)
538 composite_pointer_error (DK_PERMERROR, t1, t2, operation);
540 return error_mark_node;
542 result_type = build_ptrmem_type (TYPE_PTRMEM_CLASS_TYPE (t1),
546 result_type = build_pointer_type (result_type);
548 /* Merge the attributes. */
549 attributes = (*targetm.merge_type_attributes) (t1, t2);
550 return build_type_attribute_variant (result_type, attributes);
553 /* Return the composite pointer type (see [expr.rel]) for T1 and T2.
554 ARG1 and ARG2 are the values with those types. The OPERATION is to
555 describe the operation between the pointer types,
556 in case an error occurs.
558 This routine also implements the computation of a common type for
559 pointers-to-members as per [expr.eq]. */
562 composite_pointer_type (tree t1, tree t2, tree arg1, tree arg2,
563 composite_pointer_operation operation,
564 tsubst_flags_t complain)
571 If one operand is a null pointer constant, the composite pointer
572 type is the type of the other operand. */
573 if (null_ptr_cst_p (arg1))
575 if (null_ptr_cst_p (arg2))
582 If one of the operands has type "pointer to cv1 void*", then
583 the other has type "pointer to cv2T", and the composite pointer
584 type is "pointer to cv12 void", where cv12 is the union of cv1
587 If either type is a pointer to void, make sure it is T1. */
588 if (TREE_CODE (t2) == POINTER_TYPE && VOID_TYPE_P (TREE_TYPE (t2)))
596 /* Now, if T1 is a pointer to void, merge the qualifiers. */
597 if (TREE_CODE (t1) == POINTER_TYPE && VOID_TYPE_P (TREE_TYPE (t1)))
602 if (TYPE_PTRFN_P (t2) && (complain & tf_error))
607 pedwarn (input_location, OPT_pedantic,
608 "ISO C++ forbids comparison between "
609 "pointer of type %<void *%> and pointer-to-function");
612 pedwarn (input_location, OPT_pedantic,
613 "ISO C++ forbids conversion between "
614 "pointer of type %<void *%> and pointer-to-function");
616 case CPO_CONDITIONAL_EXPR:
617 pedwarn (input_location, OPT_pedantic,
618 "ISO C++ forbids conditional expression between "
619 "pointer of type %<void *%> and pointer-to-function");
626 = cp_build_qualified_type (void_type_node,
627 (cp_type_quals (TREE_TYPE (t1))
628 | cp_type_quals (TREE_TYPE (t2))));
629 result_type = build_pointer_type (result_type);
630 /* Merge the attributes. */
631 attributes = (*targetm.merge_type_attributes) (t1, t2);
632 return build_type_attribute_variant (result_type, attributes);
635 if (c_dialect_objc () && TREE_CODE (t1) == POINTER_TYPE
636 && TREE_CODE (t2) == POINTER_TYPE)
638 if (objc_have_common_type (t1, t2, -3, NULL_TREE))
639 return objc_common_type (t1, t2);
642 /* [expr.eq] permits the application of a pointer conversion to
643 bring the pointers to a common type. */
644 if (TREE_CODE (t1) == POINTER_TYPE && TREE_CODE (t2) == POINTER_TYPE
645 && CLASS_TYPE_P (TREE_TYPE (t1))
646 && CLASS_TYPE_P (TREE_TYPE (t2))
647 && !same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (t1),
650 class1 = TREE_TYPE (t1);
651 class2 = TREE_TYPE (t2);
653 if (DERIVED_FROM_P (class1, class2))
654 t2 = (build_pointer_type
655 (cp_build_qualified_type (class1, cp_type_quals (class2))));
656 else if (DERIVED_FROM_P (class2, class1))
657 t1 = (build_pointer_type
658 (cp_build_qualified_type (class2, cp_type_quals (class1))));
661 if (complain & tf_error)
662 composite_pointer_error (DK_ERROR, t1, t2, operation);
663 return error_mark_node;
666 /* [expr.eq] permits the application of a pointer-to-member
667 conversion to change the class type of one of the types. */
668 else if (TYPE_PTR_TO_MEMBER_P (t1)
669 && !same_type_p (TYPE_PTRMEM_CLASS_TYPE (t1),
670 TYPE_PTRMEM_CLASS_TYPE (t2)))
672 class1 = TYPE_PTRMEM_CLASS_TYPE (t1);
673 class2 = TYPE_PTRMEM_CLASS_TYPE (t2);
675 if (DERIVED_FROM_P (class1, class2))
676 t1 = build_ptrmem_type (class2, TYPE_PTRMEM_POINTED_TO_TYPE (t1));
677 else if (DERIVED_FROM_P (class2, class1))
678 t2 = build_ptrmem_type (class1, TYPE_PTRMEM_POINTED_TO_TYPE (t2));
681 if (complain & tf_error)
685 error ("comparison between distinct "
686 "pointer-to-member types %qT and %qT lacks a cast",
690 error ("conversion between distinct "
691 "pointer-to-member types %qT and %qT lacks a cast",
694 case CPO_CONDITIONAL_EXPR:
695 error ("conditional expression between distinct "
696 "pointer-to-member types %qT and %qT lacks a cast",
702 return error_mark_node;
706 return composite_pointer_type_r (t1, t2, operation, complain);
709 /* Return the merged type of two types.
710 We assume that comptypes has already been done and returned 1;
711 if that isn't so, this may crash.
713 This just combines attributes and default arguments; any other
714 differences would cause the two types to compare unalike. */
717 merge_types (tree t1, tree t2)
719 enum tree_code code1;
720 enum tree_code code2;
723 /* Save time if the two types are the same. */
726 if (original_type (t1) == original_type (t2))
729 /* If one type is nonsense, use the other. */
730 if (t1 == error_mark_node)
732 if (t2 == error_mark_node)
735 /* Merge the attributes. */
736 attributes = (*targetm.merge_type_attributes) (t1, t2);
738 if (TYPE_PTRMEMFUNC_P (t1))
739 t1 = TYPE_PTRMEMFUNC_FN_TYPE (t1);
740 if (TYPE_PTRMEMFUNC_P (t2))
741 t2 = TYPE_PTRMEMFUNC_FN_TYPE (t2);
743 code1 = TREE_CODE (t1);
744 code2 = TREE_CODE (t2);
747 gcc_assert (code1 == TYPENAME_TYPE || code2 == TYPENAME_TYPE);
748 if (code1 == TYPENAME_TYPE)
750 t1 = resolve_typename_type (t1, /*only_current_p=*/true);
751 code1 = TREE_CODE (t1);
755 t2 = resolve_typename_type (t2, /*only_current_p=*/true);
756 code2 = TREE_CODE (t2);
764 /* For two pointers, do this recursively on the target type. */
766 tree target = merge_types (TREE_TYPE (t1), TREE_TYPE (t2));
767 int quals = cp_type_quals (t1);
769 if (code1 == POINTER_TYPE)
770 t1 = build_pointer_type (target);
772 t1 = cp_build_reference_type (target, TYPE_REF_IS_RVALUE (t1));
773 t1 = build_type_attribute_variant (t1, attributes);
774 t1 = cp_build_qualified_type (t1, quals);
776 if (TREE_CODE (target) == METHOD_TYPE)
777 t1 = build_ptrmemfunc_type (t1);
786 quals = cp_type_quals (t1);
787 pointee = merge_types (TYPE_PTRMEM_POINTED_TO_TYPE (t1),
788 TYPE_PTRMEM_POINTED_TO_TYPE (t2));
789 t1 = build_ptrmem_type (TYPE_PTRMEM_CLASS_TYPE (t1),
791 t1 = cp_build_qualified_type (t1, quals);
797 tree elt = merge_types (TREE_TYPE (t1), TREE_TYPE (t2));
798 /* Save space: see if the result is identical to one of the args. */
799 if (elt == TREE_TYPE (t1) && TYPE_DOMAIN (t1))
800 return build_type_attribute_variant (t1, attributes);
801 if (elt == TREE_TYPE (t2) && TYPE_DOMAIN (t2))
802 return build_type_attribute_variant (t2, attributes);
803 /* Merge the element types, and have a size if either arg has one. */
804 t1 = build_cplus_array_type
805 (elt, TYPE_DOMAIN (TYPE_DOMAIN (t1) ? t1 : t2));
810 /* Function types: prefer the one that specified arg types.
811 If both do, merge the arg types. Also merge the return types. */
813 tree valtype = merge_types (TREE_TYPE (t1), TREE_TYPE (t2));
814 tree p1 = TYPE_ARG_TYPES (t1);
815 tree p2 = TYPE_ARG_TYPES (t2);
819 /* Save space: see if the result is identical to one of the args. */
820 if (valtype == TREE_TYPE (t1) && ! p2)
821 return cp_build_type_attribute_variant (t1, attributes);
822 if (valtype == TREE_TYPE (t2) && ! p1)
823 return cp_build_type_attribute_variant (t2, attributes);
825 /* Simple way if one arg fails to specify argument types. */
826 if (p1 == NULL_TREE || TREE_VALUE (p1) == void_type_node)
828 else if (p2 == NULL_TREE || TREE_VALUE (p2) == void_type_node)
831 parms = commonparms (p1, p2);
833 rval = build_function_type (valtype, parms);
834 gcc_assert (type_memfn_quals (t1) == type_memfn_quals (t2));
835 rval = apply_memfn_quals (rval, type_memfn_quals (t1));
836 raises = merge_exception_specifiers (TYPE_RAISES_EXCEPTIONS (t1),
837 TYPE_RAISES_EXCEPTIONS (t2),
839 t1 = build_exception_variant (rval, raises);
845 /* Get this value the long way, since TYPE_METHOD_BASETYPE
846 is just the main variant of this. */
847 tree basetype = class_of_this_parm (t2);
848 tree raises = merge_exception_specifiers (TYPE_RAISES_EXCEPTIONS (t1),
849 TYPE_RAISES_EXCEPTIONS (t2),
853 /* If this was a member function type, get back to the
854 original type of type member function (i.e., without
855 the class instance variable up front. */
856 t1 = build_function_type (TREE_TYPE (t1),
857 TREE_CHAIN (TYPE_ARG_TYPES (t1)));
858 t2 = build_function_type (TREE_TYPE (t2),
859 TREE_CHAIN (TYPE_ARG_TYPES (t2)));
860 t3 = merge_types (t1, t2);
861 t3 = build_method_type_directly (basetype, TREE_TYPE (t3),
862 TYPE_ARG_TYPES (t3));
863 t1 = build_exception_variant (t3, raises);
868 /* There is no need to merge attributes into a TYPENAME_TYPE.
869 When the type is instantiated it will have whatever
870 attributes result from the instantiation. */
876 if (attribute_list_equal (TYPE_ATTRIBUTES (t1), attributes))
878 else if (attribute_list_equal (TYPE_ATTRIBUTES (t2), attributes))
881 return cp_build_type_attribute_variant (t1, attributes);
884 /* Return the ARRAY_TYPE type without its domain. */
887 strip_array_domain (tree type)
890 gcc_assert (TREE_CODE (type) == ARRAY_TYPE);
891 if (TYPE_DOMAIN (type) == NULL_TREE)
893 t2 = build_cplus_array_type (TREE_TYPE (type), NULL_TREE);
894 return cp_build_type_attribute_variant (t2, TYPE_ATTRIBUTES (type));
897 /* Wrapper around cp_common_type that is used by c-common.c and other
898 front end optimizations that remove promotions.
900 Return the common type for two arithmetic types T1 and T2 under the
901 usual arithmetic conversions. The default conversions have already
902 been applied, and enumerated types converted to their compatible
906 common_type (tree t1, tree t2)
908 /* If one type is nonsense, use the other */
909 if (t1 == error_mark_node)
911 if (t2 == error_mark_node)
914 return cp_common_type (t1, t2);
917 /* Return the common type of two pointer types T1 and T2. This is the
918 type for the result of most arithmetic operations if the operands
919 have the given two types.
921 We assume that comp_target_types has already been done and returned
922 nonzero; if that isn't so, this may crash. */
925 common_pointer_type (tree t1, tree t2)
927 gcc_assert ((TYPE_PTR_P (t1) && TYPE_PTR_P (t2))
928 || (TYPE_PTRMEM_P (t1) && TYPE_PTRMEM_P (t2))
929 || (TYPE_PTRMEMFUNC_P (t1) && TYPE_PTRMEMFUNC_P (t2)));
931 return composite_pointer_type (t1, t2, error_mark_node, error_mark_node,
932 CPO_CONVERSION, tf_warning_or_error);
935 /* Compare two exception specifier types for exactness or subsetness, if
936 allowed. Returns false for mismatch, true for match (same, or
939 [except.spec] "If a class X ... objects of class X or any class publicly
940 and unambiguously derived from X. Similarly, if a pointer type Y * ...
941 exceptions of type Y * or that are pointers to any type publicly and
942 unambiguously derived from Y. Otherwise a function only allows exceptions
943 that have the same type ..."
944 This does not mention cv qualifiers and is different to what throw
945 [except.throw] and catch [except.catch] will do. They will ignore the
946 top level cv qualifiers, and allow qualifiers in the pointer to class
949 We implement the letter of the standard. */
952 comp_except_types (tree a, tree b, bool exact)
954 if (same_type_p (a, b))
958 if (cp_type_quals (a) || cp_type_quals (b))
961 if (TREE_CODE (a) == POINTER_TYPE
962 && TREE_CODE (b) == POINTER_TYPE)
966 if (cp_type_quals (a) || cp_type_quals (b))
970 if (TREE_CODE (a) != RECORD_TYPE
971 || TREE_CODE (b) != RECORD_TYPE)
974 if (PUBLICLY_UNIQUELY_DERIVED_P (a, b))
980 /* Return true if TYPE1 and TYPE2 are equivalent exception specifiers.
981 If EXACT is ce_derived, T2 can be stricter than T1 (according to 15.4/5).
982 If EXACT is ce_normal, the compatibility rules in 15.4/3 apply.
983 If EXACT is ce_exact, the specs must be exactly the same. Exception lists
984 are unordered, but we've already filtered out duplicates. Most lists will
985 be in order, we should try to make use of that. */
988 comp_except_specs (const_tree t1, const_tree t2, int exact)
997 /* First handle noexcept. */
998 if (exact < ce_exact)
1000 /* noexcept(false) is compatible with no exception-specification,
1001 and stricter than any spec. */
1002 if (t1 == noexcept_false_spec)
1003 return t2 == NULL_TREE || exact == ce_derived;
1004 /* Even a derived noexcept(false) is compatible with no
1005 exception-specification. */
1006 if (t2 == noexcept_false_spec)
1007 return t1 == NULL_TREE;
1009 /* Otherwise, if we aren't looking for an exact match, noexcept is
1010 equivalent to throw(). */
1011 if (t1 == noexcept_true_spec)
1012 t1 = empty_except_spec;
1013 if (t2 == noexcept_true_spec)
1014 t2 = empty_except_spec;
1017 /* If any noexcept is left, it is only comparable to itself;
1018 either we're looking for an exact match or we're redeclaring a
1019 template with dependent noexcept. */
1020 if ((t1 && TREE_PURPOSE (t1))
1021 || (t2 && TREE_PURPOSE (t2)))
1023 && cp_tree_equal (TREE_PURPOSE (t1), TREE_PURPOSE (t2)));
1025 if (t1 == NULL_TREE) /* T1 is ... */
1026 return t2 == NULL_TREE || exact == ce_derived;
1027 if (!TREE_VALUE (t1)) /* t1 is EMPTY */
1028 return t2 != NULL_TREE && !TREE_VALUE (t2);
1029 if (t2 == NULL_TREE) /* T2 is ... */
1031 if (TREE_VALUE (t1) && !TREE_VALUE (t2)) /* T2 is EMPTY, T1 is not */
1032 return exact == ce_derived;
1034 /* Neither set is ... or EMPTY, make sure each part of T2 is in T1.
1035 Count how many we find, to determine exactness. For exact matching and
1036 ordered T1, T2, this is an O(n) operation, otherwise its worst case is
1038 for (base = t1; t2 != NULL_TREE; t2 = TREE_CHAIN (t2))
1040 for (probe = base; probe != NULL_TREE; probe = TREE_CHAIN (probe))
1042 tree a = TREE_VALUE (probe);
1043 tree b = TREE_VALUE (t2);
1045 if (comp_except_types (a, b, exact))
1047 if (probe == base && exact > ce_derived)
1048 base = TREE_CHAIN (probe);
1053 if (probe == NULL_TREE)
1056 return exact == ce_derived || base == NULL_TREE || length == list_length (t1);
1059 /* Compare the array types T1 and T2. ALLOW_REDECLARATION is true if
1060 [] can match [size]. */
1063 comp_array_types (const_tree t1, const_tree t2, bool allow_redeclaration)
1072 /* The type of the array elements must be the same. */
1073 if (!same_type_p (TREE_TYPE (t1), TREE_TYPE (t2)))
1076 d1 = TYPE_DOMAIN (t1);
1077 d2 = TYPE_DOMAIN (t2);
1082 /* If one of the arrays is dimensionless, and the other has a
1083 dimension, they are of different types. However, it is valid to
1091 declarations for an array object can specify
1092 array types that differ by the presence or absence of a major
1093 array bound (_dcl.array_). */
1095 return allow_redeclaration;
1097 /* Check that the dimensions are the same. */
1099 if (!cp_tree_equal (TYPE_MIN_VALUE (d1), TYPE_MIN_VALUE (d2)))
1101 max1 = TYPE_MAX_VALUE (d1);
1102 max2 = TYPE_MAX_VALUE (d2);
1103 if (processing_template_decl && !abi_version_at_least (2)
1104 && !value_dependent_expression_p (max1)
1105 && !value_dependent_expression_p (max2))
1107 /* With abi-1 we do not fold non-dependent array bounds, (and
1108 consequently mangle them incorrectly). We must therefore
1109 fold them here, to verify the domains have the same
1115 if (!cp_tree_equal (max1, max2))
1121 /* Compare the relative position of T1 and T2 into their respective
1122 template parameter list.
1123 T1 and T2 must be template parameter types.
1124 Return TRUE if T1 and T2 have the same position, FALSE otherwise. */
1127 comp_template_parms_position (tree t1, tree t2)
1129 tree index1, index2;
1130 gcc_assert (t1 && t2
1131 && TREE_CODE (t1) == TREE_CODE (t2)
1132 && (TREE_CODE (t1) == BOUND_TEMPLATE_TEMPLATE_PARM
1133 || TREE_CODE (t1) == TEMPLATE_TEMPLATE_PARM
1134 || TREE_CODE (t1) == TEMPLATE_TYPE_PARM));
1136 index1 = TEMPLATE_TYPE_PARM_INDEX (TYPE_MAIN_VARIANT (t1));
1137 index2 = TEMPLATE_TYPE_PARM_INDEX (TYPE_MAIN_VARIANT (t2));
1139 /* If T1 and T2 belong to template parm lists of different size,
1140 let's assume they are different. */
1141 if (TEMPLATE_PARM_NUM_SIBLINGS (index1)
1142 != TEMPLATE_PARM_NUM_SIBLINGS (index2))
1145 /* Then compare their relative position. */
1146 if (TEMPLATE_PARM_IDX (index1) != TEMPLATE_PARM_IDX (index2)
1147 || TEMPLATE_PARM_LEVEL (index1) != TEMPLATE_PARM_LEVEL (index2)
1148 || (TEMPLATE_PARM_PARAMETER_PACK (index1)
1149 != TEMPLATE_PARM_PARAMETER_PACK (index2)))
1155 /* Subroutine in comptypes. */
1158 structural_comptypes (tree t1, tree t2, int strict)
1163 /* Suppress errors caused by previously reported errors. */
1164 if (t1 == error_mark_node || t2 == error_mark_node)
1167 gcc_assert (TYPE_P (t1) && TYPE_P (t2));
1169 /* TYPENAME_TYPEs should be resolved if the qualifying scope is the
1170 current instantiation. */
1171 if (TREE_CODE (t1) == TYPENAME_TYPE)
1172 t1 = resolve_typename_type (t1, /*only_current_p=*/true);
1174 if (TREE_CODE (t2) == TYPENAME_TYPE)
1175 t2 = resolve_typename_type (t2, /*only_current_p=*/true);
1177 if (TYPE_PTRMEMFUNC_P (t1))
1178 t1 = TYPE_PTRMEMFUNC_FN_TYPE (t1);
1179 if (TYPE_PTRMEMFUNC_P (t2))
1180 t2 = TYPE_PTRMEMFUNC_FN_TYPE (t2);
1182 /* Different classes of types can't be compatible. */
1183 if (TREE_CODE (t1) != TREE_CODE (t2))
1186 /* Qualifiers must match. For array types, we will check when we
1187 recur on the array element types. */
1188 if (TREE_CODE (t1) != ARRAY_TYPE
1189 && cp_type_quals (t1) != cp_type_quals (t2))
1191 if (TREE_CODE (t1) == FUNCTION_TYPE
1192 && type_memfn_quals (t1) != type_memfn_quals (t2))
1194 if (TYPE_FOR_JAVA (t1) != TYPE_FOR_JAVA (t2))
1197 /* Allow for two different type nodes which have essentially the same
1198 definition. Note that we already checked for equality of the type
1199 qualifiers (just above). */
1201 if (TREE_CODE (t1) != ARRAY_TYPE
1202 && TYPE_MAIN_VARIANT (t1) == TYPE_MAIN_VARIANT (t2))
1206 /* Compare the types. Break out if they could be the same. */
1207 switch (TREE_CODE (t1))
1211 /* All void and bool types are the same. */
1215 case FIXED_POINT_TYPE:
1217 /* With these nodes, we can't determine type equivalence by
1218 looking at what is stored in the nodes themselves, because
1219 two nodes might have different TYPE_MAIN_VARIANTs but still
1220 represent the same type. For example, wchar_t and int could
1221 have the same properties (TYPE_PRECISION, TYPE_MIN_VALUE,
1222 TYPE_MAX_VALUE, etc.), but have different TYPE_MAIN_VARIANTs
1223 and are distinct types. On the other hand, int and the
1226 typedef int INT __attribute((may_alias));
1228 have identical properties, different TYPE_MAIN_VARIANTs, but
1229 represent the same type. The canonical type system keeps
1230 track of equivalence in this case, so we fall back on it. */
1231 return TYPE_CANONICAL (t1) == TYPE_CANONICAL (t2);
1233 case TEMPLATE_TEMPLATE_PARM:
1234 case BOUND_TEMPLATE_TEMPLATE_PARM:
1235 if (!comp_template_parms_position (t1, t2))
1237 if (!comp_template_parms
1238 (DECL_TEMPLATE_PARMS (TEMPLATE_TEMPLATE_PARM_TEMPLATE_DECL (t1)),
1239 DECL_TEMPLATE_PARMS (TEMPLATE_TEMPLATE_PARM_TEMPLATE_DECL (t2))))
1241 if (TREE_CODE (t1) == TEMPLATE_TEMPLATE_PARM)
1243 /* Don't check inheritance. */
1244 strict = COMPARE_STRICT;
1249 if (TYPE_TEMPLATE_INFO (t1) && TYPE_TEMPLATE_INFO (t2)
1250 && (TYPE_TI_TEMPLATE (t1) == TYPE_TI_TEMPLATE (t2)
1251 || TREE_CODE (t1) == BOUND_TEMPLATE_TEMPLATE_PARM)
1252 && comp_template_args (TYPE_TI_ARGS (t1), TYPE_TI_ARGS (t2)))
1255 if ((strict & COMPARE_BASE) && DERIVED_FROM_P (t1, t2))
1257 else if ((strict & COMPARE_DERIVED) && DERIVED_FROM_P (t2, t1))
1263 if (!comptypes (TYPE_OFFSET_BASETYPE (t1), TYPE_OFFSET_BASETYPE (t2),
1264 strict & ~COMPARE_REDECLARATION))
1266 if (!same_type_p (TREE_TYPE (t1), TREE_TYPE (t2)))
1270 case REFERENCE_TYPE:
1271 if (TYPE_REF_IS_RVALUE (t1) != TYPE_REF_IS_RVALUE (t2))
1273 /* fall through to checks for pointer types */
1276 if (TYPE_MODE (t1) != TYPE_MODE (t2)
1277 || TYPE_REF_CAN_ALIAS_ALL (t1) != TYPE_REF_CAN_ALIAS_ALL (t2)
1278 || !same_type_p (TREE_TYPE (t1), TREE_TYPE (t2)))
1284 if (!same_type_p (TREE_TYPE (t1), TREE_TYPE (t2)))
1286 if (!compparms (TYPE_ARG_TYPES (t1), TYPE_ARG_TYPES (t2)))
1291 /* Target types must match incl. qualifiers. */
1292 if (!comp_array_types (t1, t2, !!(strict & COMPARE_REDECLARATION)))
1296 case TEMPLATE_TYPE_PARM:
1297 /* If T1 and T2 don't have the same relative position in their
1298 template parameters set, they can't be equal. */
1299 if (!comp_template_parms_position (t1, t2))
1304 if (!cp_tree_equal (TYPENAME_TYPE_FULLNAME (t1),
1305 TYPENAME_TYPE_FULLNAME (t2)))
1307 /* Qualifiers don't matter on scopes. */
1308 if (!same_type_ignoring_top_level_qualifiers_p (TYPE_CONTEXT (t1),
1313 case UNBOUND_CLASS_TEMPLATE:
1314 if (!cp_tree_equal (TYPE_IDENTIFIER (t1), TYPE_IDENTIFIER (t2)))
1316 if (!same_type_p (TYPE_CONTEXT (t1), TYPE_CONTEXT (t2)))
1321 if (!same_type_p (TREE_TYPE (t1), TREE_TYPE (t2)))
1326 if (TYPE_VECTOR_SUBPARTS (t1) != TYPE_VECTOR_SUBPARTS (t2)
1327 || !same_type_p (TREE_TYPE (t1), TREE_TYPE (t2)))
1331 case TYPE_PACK_EXPANSION:
1332 return (same_type_p (PACK_EXPANSION_PATTERN (t1),
1333 PACK_EXPANSION_PATTERN (t2))
1334 && comp_template_args (PACK_EXPANSION_EXTRA_ARGS (t1),
1335 PACK_EXPANSION_EXTRA_ARGS (t2)));
1338 if (DECLTYPE_TYPE_ID_EXPR_OR_MEMBER_ACCESS_P (t1)
1339 != DECLTYPE_TYPE_ID_EXPR_OR_MEMBER_ACCESS_P (t2)
1340 || (DECLTYPE_FOR_LAMBDA_CAPTURE (t1)
1341 != DECLTYPE_FOR_LAMBDA_CAPTURE (t2))
1342 || (DECLTYPE_FOR_LAMBDA_PROXY (t1)
1343 != DECLTYPE_FOR_LAMBDA_PROXY (t2))
1344 || !cp_tree_equal (DECLTYPE_TYPE_EXPR (t1),
1345 DECLTYPE_TYPE_EXPR (t2)))
1349 case UNDERLYING_TYPE:
1350 return same_type_p (UNDERLYING_TYPE_TYPE (t1),
1351 UNDERLYING_TYPE_TYPE (t2));
1357 /* If we get here, we know that from a target independent POV the
1358 types are the same. Make sure the target attributes are also
1360 return comp_type_attributes (t1, t2);
1363 /* Return true if T1 and T2 are related as allowed by STRICT. STRICT
1364 is a bitwise-or of the COMPARE_* flags. */
1367 comptypes (tree t1, tree t2, int strict)
1369 if (strict == COMPARE_STRICT)
1374 if (t1 == error_mark_node || t2 == error_mark_node)
1377 if (TYPE_STRUCTURAL_EQUALITY_P (t1) || TYPE_STRUCTURAL_EQUALITY_P (t2))
1378 /* At least one of the types requires structural equality, so
1379 perform a deep check. */
1380 return structural_comptypes (t1, t2, strict);
1382 #ifdef ENABLE_CHECKING
1383 if (USE_CANONICAL_TYPES)
1385 bool result = structural_comptypes (t1, t2, strict);
1387 if (result && TYPE_CANONICAL (t1) != TYPE_CANONICAL (t2))
1388 /* The two types are structurally equivalent, but their
1389 canonical types were different. This is a failure of the
1390 canonical type propagation code.*/
1392 ("canonical types differ for identical types %T and %T",
1394 else if (!result && TYPE_CANONICAL (t1) == TYPE_CANONICAL (t2))
1395 /* Two types are structurally different, but the canonical
1396 types are the same. This means we were over-eager in
1397 assigning canonical types. */
1399 ("same canonical type node for different types %T and %T",
1405 if (USE_CANONICAL_TYPES)
1406 return TYPE_CANONICAL (t1) == TYPE_CANONICAL (t2);
1409 return structural_comptypes (t1, t2, strict);
1411 else if (strict == COMPARE_STRUCTURAL)
1412 return structural_comptypes (t1, t2, COMPARE_STRICT);
1414 return structural_comptypes (t1, t2, strict);
1417 /* Returns nonzero iff TYPE1 and TYPE2 are the same type, ignoring
1418 top-level qualifiers. */
1421 same_type_ignoring_top_level_qualifiers_p (tree type1, tree type2)
1423 if (type1 == error_mark_node || type2 == error_mark_node)
1426 return same_type_p (TYPE_MAIN_VARIANT (type1), TYPE_MAIN_VARIANT (type2));
1429 /* Returns 1 if TYPE1 is at least as qualified as TYPE2. */
1432 at_least_as_qualified_p (const_tree type1, const_tree type2)
1434 int q1 = cp_type_quals (type1);
1435 int q2 = cp_type_quals (type2);
1437 /* All qualifiers for TYPE2 must also appear in TYPE1. */
1438 return (q1 & q2) == q2;
1441 /* Returns 1 if TYPE1 is more cv-qualified than TYPE2, -1 if TYPE2 is
1442 more cv-qualified that TYPE1, and 0 otherwise. */
1445 comp_cv_qualification (const_tree type1, const_tree type2)
1447 int q1 = cp_type_quals (type1);
1448 int q2 = cp_type_quals (type2);
1453 if ((q1 & q2) == q2)
1455 else if ((q1 & q2) == q1)
1461 /* Returns 1 if the cv-qualification signature of TYPE1 is a proper
1462 subset of the cv-qualification signature of TYPE2, and the types
1463 are similar. Returns -1 if the other way 'round, and 0 otherwise. */
1466 comp_cv_qual_signature (tree type1, tree type2)
1468 if (comp_ptr_ttypes_real (type2, type1, -1))
1470 else if (comp_ptr_ttypes_real (type1, type2, -1))
1476 /* Subroutines of `comptypes'. */
1478 /* Return true if two parameter type lists PARMS1 and PARMS2 are
1479 equivalent in the sense that functions with those parameter types
1480 can have equivalent types. The two lists must be equivalent,
1481 element by element. */
1484 compparms (const_tree parms1, const_tree parms2)
1488 /* An unspecified parmlist matches any specified parmlist
1489 whose argument types don't need default promotions. */
1491 for (t1 = parms1, t2 = parms2;
1493 t1 = TREE_CHAIN (t1), t2 = TREE_CHAIN (t2))
1495 /* If one parmlist is shorter than the other,
1496 they fail to match. */
1499 if (!same_type_p (TREE_VALUE (t1), TREE_VALUE (t2)))
1506 /* Process a sizeof or alignof expression where the operand is a
1510 cxx_sizeof_or_alignof_type (tree type, enum tree_code op, bool complain)
1515 gcc_assert (op == SIZEOF_EXPR || op == ALIGNOF_EXPR);
1516 if (type == error_mark_node)
1517 return error_mark_node;
1519 type = non_reference (type);
1520 if (TREE_CODE (type) == METHOD_TYPE)
1523 pedwarn (input_location, pedantic ? OPT_pedantic : OPT_Wpointer_arith,
1524 "invalid application of %qs to a member function",
1525 operator_name_info[(int) op].name);
1526 value = size_one_node;
1529 dependent_p = dependent_type_p (type);
1531 complete_type (type);
1533 /* VLA types will have a non-constant size. In the body of an
1534 uninstantiated template, we don't need to try to compute the
1535 value, because the sizeof expression is not an integral
1536 constant expression in that case. And, if we do try to
1537 compute the value, we'll likely end up with SAVE_EXPRs, which
1538 the template substitution machinery does not expect to see. */
1539 || (processing_template_decl
1540 && COMPLETE_TYPE_P (type)
1541 && TREE_CODE (TYPE_SIZE (type)) != INTEGER_CST))
1543 value = build_min (op, size_type_node, type);
1544 TREE_READONLY (value) = 1;
1548 return c_sizeof_or_alignof_type (input_location, complete_type (type),
1553 /* Return the size of the type, without producing any warnings for
1554 types whose size cannot be taken. This routine should be used only
1555 in some other routine that has already produced a diagnostic about
1556 using the size of such a type. */
1558 cxx_sizeof_nowarn (tree type)
1560 if (TREE_CODE (type) == FUNCTION_TYPE
1561 || TREE_CODE (type) == VOID_TYPE
1562 || TREE_CODE (type) == ERROR_MARK)
1563 return size_one_node;
1564 else if (!COMPLETE_TYPE_P (type))
1565 return size_zero_node;
1567 return cxx_sizeof_or_alignof_type (type, SIZEOF_EXPR, false);
1570 /* Process a sizeof expression where the operand is an expression. */
1573 cxx_sizeof_expr (tree e, tsubst_flags_t complain)
1575 if (e == error_mark_node)
1576 return error_mark_node;
1578 if (processing_template_decl)
1580 e = build_min (SIZEOF_EXPR, size_type_node, e);
1581 TREE_SIDE_EFFECTS (e) = 0;
1582 TREE_READONLY (e) = 1;
1587 /* To get the size of a static data member declared as an array of
1588 unknown bound, we need to instantiate it. */
1589 if (TREE_CODE (e) == VAR_DECL
1590 && VAR_HAD_UNKNOWN_BOUND (e)
1591 && DECL_TEMPLATE_INSTANTIATION (e))
1592 instantiate_decl (e, /*defer_ok*/true, /*expl_inst_mem*/false);
1594 e = mark_type_use (e);
1596 if (TREE_CODE (e) == COMPONENT_REF
1597 && TREE_CODE (TREE_OPERAND (e, 1)) == FIELD_DECL
1598 && DECL_C_BIT_FIELD (TREE_OPERAND (e, 1)))
1600 if (complain & tf_error)
1601 error ("invalid application of %<sizeof%> to a bit-field");
1603 return error_mark_node;
1606 else if (is_overloaded_fn (e))
1608 if (complain & tf_error)
1609 permerror (input_location, "ISO C++ forbids applying %<sizeof%> to an expression of "
1612 return error_mark_node;
1615 else if (type_unknown_p (e))
1617 if (complain & tf_error)
1618 cxx_incomplete_type_error (e, TREE_TYPE (e));
1620 return error_mark_node;
1626 return cxx_sizeof_or_alignof_type (e, SIZEOF_EXPR, complain & tf_error);
1629 /* Implement the __alignof keyword: Return the minimum required
1630 alignment of E, measured in bytes. For VAR_DECL's and
1631 FIELD_DECL's return DECL_ALIGN (which can be set from an
1632 "aligned" __attribute__ specification). */
1635 cxx_alignof_expr (tree e, tsubst_flags_t complain)
1639 if (e == error_mark_node)
1640 return error_mark_node;
1642 if (processing_template_decl)
1644 e = build_min (ALIGNOF_EXPR, size_type_node, e);
1645 TREE_SIDE_EFFECTS (e) = 0;
1646 TREE_READONLY (e) = 1;
1651 e = mark_type_use (e);
1653 if (TREE_CODE (e) == VAR_DECL)
1654 t = size_int (DECL_ALIGN_UNIT (e));
1655 else if (TREE_CODE (e) == COMPONENT_REF
1656 && TREE_CODE (TREE_OPERAND (e, 1)) == FIELD_DECL
1657 && DECL_C_BIT_FIELD (TREE_OPERAND (e, 1)))
1659 if (complain & tf_error)
1660 error ("invalid application of %<__alignof%> to a bit-field");
1662 return error_mark_node;
1665 else if (TREE_CODE (e) == COMPONENT_REF
1666 && TREE_CODE (TREE_OPERAND (e, 1)) == FIELD_DECL)
1667 t = size_int (DECL_ALIGN_UNIT (TREE_OPERAND (e, 1)));
1668 else if (is_overloaded_fn (e))
1670 if (complain & tf_error)
1671 permerror (input_location, "ISO C++ forbids applying %<__alignof%> to an expression of "
1674 return error_mark_node;
1675 if (TREE_CODE (e) == FUNCTION_DECL)
1676 t = size_int (DECL_ALIGN_UNIT (e));
1680 else if (type_unknown_p (e))
1682 if (complain & tf_error)
1683 cxx_incomplete_type_error (e, TREE_TYPE (e));
1685 return error_mark_node;
1689 return cxx_sizeof_or_alignof_type (TREE_TYPE (e), ALIGNOF_EXPR,
1690 complain & tf_error);
1692 return fold_convert (size_type_node, t);
1695 /* Process a sizeof or alignof expression E with code OP where the operand
1696 is an expression. */
1699 cxx_sizeof_or_alignof_expr (tree e, enum tree_code op, bool complain)
1701 if (op == SIZEOF_EXPR)
1702 return cxx_sizeof_expr (e, complain? tf_warning_or_error : tf_none);
1704 return cxx_alignof_expr (e, complain? tf_warning_or_error : tf_none);
1707 /* EXPR is being used in a context that is not a function call.
1712 The expression can be used only as the left-hand operand of a
1713 member function call.
1715 [expr.mptr.operator]
1717 If the result of .* or ->* is a function, then that result can be
1718 used only as the operand for the function call operator ().
1720 by issuing an error message if appropriate. Returns true iff EXPR
1721 violates these rules. */
1724 invalid_nonstatic_memfn_p (const_tree expr, tsubst_flags_t complain)
1726 if (expr && DECL_NONSTATIC_MEMBER_FUNCTION_P (expr))
1728 if (complain & tf_error)
1729 error ("invalid use of non-static member function");
1735 /* If EXP is a reference to a bitfield, and the type of EXP does not
1736 match the declared type of the bitfield, return the declared type
1737 of the bitfield. Otherwise, return NULL_TREE. */
1740 is_bitfield_expr_with_lowered_type (const_tree exp)
1742 switch (TREE_CODE (exp))
1745 if (!is_bitfield_expr_with_lowered_type (TREE_OPERAND (exp, 1)
1746 ? TREE_OPERAND (exp, 1)
1747 : TREE_OPERAND (exp, 0)))
1749 return is_bitfield_expr_with_lowered_type (TREE_OPERAND (exp, 2));
1752 return is_bitfield_expr_with_lowered_type (TREE_OPERAND (exp, 1));
1756 return is_bitfield_expr_with_lowered_type (TREE_OPERAND (exp, 0));
1762 field = TREE_OPERAND (exp, 1);
1763 if (TREE_CODE (field) != FIELD_DECL || !DECL_BIT_FIELD_TYPE (field))
1765 if (same_type_ignoring_top_level_qualifiers_p
1766 (TREE_TYPE (exp), DECL_BIT_FIELD_TYPE (field)))
1768 return DECL_BIT_FIELD_TYPE (field);
1772 if (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_OPERAND (exp, 0)))
1773 == TYPE_MAIN_VARIANT (TREE_TYPE (exp)))
1774 return is_bitfield_expr_with_lowered_type (TREE_OPERAND (exp, 0));
1782 /* Like is_bitfield_with_lowered_type, except that if EXP is not a
1783 bitfield with a lowered type, the type of EXP is returned, rather
1787 unlowered_expr_type (const_tree exp)
1790 tree etype = TREE_TYPE (exp);
1792 type = is_bitfield_expr_with_lowered_type (exp);
1794 type = cp_build_qualified_type (type, cp_type_quals (etype));
1801 /* Perform the conversions in [expr] that apply when an lvalue appears
1802 in an rvalue context: the lvalue-to-rvalue, array-to-pointer, and
1803 function-to-pointer conversions. In addition, manifest constants
1804 are replaced by their values, and bitfield references are converted
1805 to their declared types. Note that this function does not perform the
1806 lvalue-to-rvalue conversion for class types. If you need that conversion
1807 to for class types, then you probably need to use force_rvalue.
1809 Although the returned value is being used as an rvalue, this
1810 function does not wrap the returned expression in a
1811 NON_LVALUE_EXPR; the caller is expected to be mindful of the fact
1812 that the return value is no longer an lvalue. */
1815 decay_conversion (tree exp)
1818 enum tree_code code;
1820 type = TREE_TYPE (exp);
1821 if (type == error_mark_node)
1822 return error_mark_node;
1824 exp = mark_rvalue_use (exp);
1826 exp = resolve_nondeduced_context (exp);
1827 if (type_unknown_p (exp))
1829 cxx_incomplete_type_error (exp, TREE_TYPE (exp));
1830 return error_mark_node;
1833 /* FIXME remove? at least need to remember that this isn't really a
1834 constant expression if EXP isn't decl_constant_var_p, like with
1835 C_MAYBE_CONST_EXPR. */
1836 exp = decl_constant_value_safe (exp);
1837 if (error_operand_p (exp))
1838 return error_mark_node;
1840 if (NULLPTR_TYPE_P (type))
1841 return nullptr_node;
1843 /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
1844 Leave such NOP_EXPRs, since RHS is being used in non-lvalue context. */
1845 code = TREE_CODE (type);
1846 if (code == VOID_TYPE)
1848 error ("void value not ignored as it ought to be");
1849 return error_mark_node;
1851 if (invalid_nonstatic_memfn_p (exp, tf_warning_or_error))
1852 return error_mark_node;
1853 if (code == FUNCTION_TYPE || is_overloaded_fn (exp))
1854 return cp_build_addr_expr (exp, tf_warning_or_error);
1855 if (code == ARRAY_TYPE)
1860 if (TREE_CODE (exp) == INDIRECT_REF)
1861 return build_nop (build_pointer_type (TREE_TYPE (type)),
1862 TREE_OPERAND (exp, 0));
1864 if (TREE_CODE (exp) == COMPOUND_EXPR)
1866 tree op1 = decay_conversion (TREE_OPERAND (exp, 1));
1867 return build2 (COMPOUND_EXPR, TREE_TYPE (op1),
1868 TREE_OPERAND (exp, 0), op1);
1872 && ! (TREE_CODE (exp) == CONSTRUCTOR && TREE_STATIC (exp)))
1874 error ("invalid use of non-lvalue array");
1875 return error_mark_node;
1878 ptrtype = build_pointer_type (TREE_TYPE (type));
1880 if (TREE_CODE (exp) == VAR_DECL)
1882 if (!cxx_mark_addressable (exp))
1883 return error_mark_node;
1884 adr = build_nop (ptrtype, build_address (exp));
1887 /* This way is better for a COMPONENT_REF since it can
1888 simplify the offset for a component. */
1889 adr = cp_build_addr_expr (exp, tf_warning_or_error);
1890 return cp_convert (ptrtype, adr);
1893 /* If a bitfield is used in a context where integral promotion
1894 applies, then the caller is expected to have used
1895 default_conversion. That function promotes bitfields correctly
1896 before calling this function. At this point, if we have a
1897 bitfield referenced, we may assume that is not subject to
1898 promotion, and that, therefore, the type of the resulting rvalue
1899 is the declared type of the bitfield. */
1900 exp = convert_bitfield_to_declared_type (exp);
1902 /* We do not call rvalue() here because we do not want to wrap EXP
1903 in a NON_LVALUE_EXPR. */
1907 Non-class rvalues always have cv-unqualified types. */
1908 type = TREE_TYPE (exp);
1909 if (!CLASS_TYPE_P (type) && cv_qualified_p (type))
1910 exp = build_nop (cv_unqualified (type), exp);
1915 /* Perform preparatory conversions, as part of the "usual arithmetic
1916 conversions". In particular, as per [expr]:
1918 Whenever an lvalue expression appears as an operand of an
1919 operator that expects the rvalue for that operand, the
1920 lvalue-to-rvalue, array-to-pointer, or function-to-pointer
1921 standard conversions are applied to convert the expression to an
1924 In addition, we perform integral promotions here, as those are
1925 applied to both operands to a binary operator before determining
1926 what additional conversions should apply. */
1929 default_conversion (tree exp)
1931 /* Check for target-specific promotions. */
1932 tree promoted_type = targetm.promoted_type (TREE_TYPE (exp));
1934 exp = cp_convert (promoted_type, exp);
1935 /* Perform the integral promotions first so that bitfield
1936 expressions (which may promote to "int", even if the bitfield is
1937 declared "unsigned") are promoted correctly. */
1938 else if (INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (TREE_TYPE (exp)))
1939 exp = perform_integral_promotions (exp);
1940 /* Perform the other conversions. */
1941 exp = decay_conversion (exp);
1946 /* EXPR is an expression with an integral or enumeration type.
1947 Perform the integral promotions in [conv.prom], and return the
1951 perform_integral_promotions (tree expr)
1956 expr = mark_rvalue_use (expr);
1960 If the bitfield has an enumerated type, it is treated as any
1961 other value of that type for promotion purposes. */
1962 type = is_bitfield_expr_with_lowered_type (expr);
1963 if (!type || TREE_CODE (type) != ENUMERAL_TYPE)
1964 type = TREE_TYPE (expr);
1965 gcc_assert (INTEGRAL_OR_ENUMERATION_TYPE_P (type));
1966 /* Scoped enums don't promote. */
1967 if (SCOPED_ENUM_P (type))
1969 promoted_type = type_promotes_to (type);
1970 if (type != promoted_type)
1971 expr = cp_convert (promoted_type, expr);
1975 /* Returns nonzero iff exp is a STRING_CST or the result of applying
1976 decay_conversion to one. */
1979 string_conv_p (const_tree totype, const_tree exp, int warn)
1983 if (TREE_CODE (totype) != POINTER_TYPE)
1986 t = TREE_TYPE (totype);
1987 if (!same_type_p (t, char_type_node)
1988 && !same_type_p (t, char16_type_node)
1989 && !same_type_p (t, char32_type_node)
1990 && !same_type_p (t, wchar_type_node))
1993 if (TREE_CODE (exp) == STRING_CST)
1995 /* Make sure that we don't try to convert between char and wide chars. */
1996 if (!same_type_p (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (exp))), t))
2001 /* Is this a string constant which has decayed to 'const char *'? */
2002 t = build_pointer_type (cp_build_qualified_type (t, TYPE_QUAL_CONST));
2003 if (!same_type_p (TREE_TYPE (exp), t))
2006 if (TREE_CODE (exp) != ADDR_EXPR
2007 || TREE_CODE (TREE_OPERAND (exp, 0)) != STRING_CST)
2011 /* This warning is not very useful, as it complains about printf. */
2013 warning (OPT_Wwrite_strings,
2014 "deprecated conversion from string constant to %qT",
2020 /* Given a COND_EXPR, MIN_EXPR, or MAX_EXPR in T, return it in a form that we
2021 can, for example, use as an lvalue. This code used to be in
2022 unary_complex_lvalue, but we needed it to deal with `a = (d == c) ? b : c'
2023 expressions, where we're dealing with aggregates. But now it's again only
2024 called from unary_complex_lvalue. The case (in particular) that led to
2025 this was with CODE == ADDR_EXPR, since it's not an lvalue when we'd
2029 rationalize_conditional_expr (enum tree_code code, tree t,
2030 tsubst_flags_t complain)
2032 /* For MIN_EXPR or MAX_EXPR, fold-const.c has arranged things so that
2033 the first operand is always the one to be used if both operands
2034 are equal, so we know what conditional expression this used to be. */
2035 if (TREE_CODE (t) == MIN_EXPR || TREE_CODE (t) == MAX_EXPR)
2037 tree op0 = TREE_OPERAND (t, 0);
2038 tree op1 = TREE_OPERAND (t, 1);
2040 /* The following code is incorrect if either operand side-effects. */
2041 gcc_assert (!TREE_SIDE_EFFECTS (op0)
2042 && !TREE_SIDE_EFFECTS (op1));
2044 build_conditional_expr (build_x_binary_op ((TREE_CODE (t) == MIN_EXPR
2045 ? LE_EXPR : GE_EXPR),
2046 op0, TREE_CODE (op0),
2047 op1, TREE_CODE (op1),
2050 cp_build_unary_op (code, op0, 0, complain),
2051 cp_build_unary_op (code, op1, 0, complain),
2056 build_conditional_expr (TREE_OPERAND (t, 0),
2057 cp_build_unary_op (code, TREE_OPERAND (t, 1), 0,
2059 cp_build_unary_op (code, TREE_OPERAND (t, 2), 0,
2064 /* Given the TYPE of an anonymous union field inside T, return the
2065 FIELD_DECL for the field. If not found return NULL_TREE. Because
2066 anonymous unions can nest, we must also search all anonymous unions
2067 that are directly reachable. */
2070 lookup_anon_field (tree t, tree type)
2074 for (field = TYPE_FIELDS (t); field; field = DECL_CHAIN (field))
2076 if (TREE_STATIC (field))
2078 if (TREE_CODE (field) != FIELD_DECL || DECL_ARTIFICIAL (field))
2081 /* If we find it directly, return the field. */
2082 if (DECL_NAME (field) == NULL_TREE
2083 && type == TYPE_MAIN_VARIANT (TREE_TYPE (field)))
2088 /* Otherwise, it could be nested, search harder. */
2089 if (DECL_NAME (field) == NULL_TREE
2090 && ANON_AGGR_TYPE_P (TREE_TYPE (field)))
2092 tree subfield = lookup_anon_field (TREE_TYPE (field), type);
2100 /* Build an expression representing OBJECT.MEMBER. OBJECT is an
2101 expression; MEMBER is a DECL or baselink. If ACCESS_PATH is
2102 non-NULL, it indicates the path to the base used to name MEMBER.
2103 If PRESERVE_REFERENCE is true, the expression returned will have
2104 REFERENCE_TYPE if the MEMBER does. Otherwise, the expression
2105 returned will have the type referred to by the reference.
2107 This function does not perform access control; that is either done
2108 earlier by the parser when the name of MEMBER is resolved to MEMBER
2109 itself, or later when overload resolution selects one of the
2110 functions indicated by MEMBER. */
2113 build_class_member_access_expr (tree object, tree member,
2114 tree access_path, bool preserve_reference,
2115 tsubst_flags_t complain)
2119 tree result = NULL_TREE;
2120 tree using_decl = NULL_TREE;
2122 if (error_operand_p (object) || error_operand_p (member))
2123 return error_mark_node;
2125 gcc_assert (DECL_P (member) || BASELINK_P (member));
2129 The type of the first expression shall be "class object" (of a
2131 object_type = TREE_TYPE (object);
2132 if (!currently_open_class (object_type)
2133 && !complete_type_or_maybe_complain (object_type, object, complain))
2134 return error_mark_node;
2135 if (!CLASS_TYPE_P (object_type))
2137 if (complain & tf_error)
2139 if (POINTER_TYPE_P (object_type)
2140 && CLASS_TYPE_P (TREE_TYPE (object_type)))
2141 error ("request for member %qD in %qE, which is of pointer "
2142 "type %qT (maybe you meant to use %<->%> ?)",
2143 member, object, object_type);
2145 error ("request for member %qD in %qE, which is of non-class "
2146 "type %qT", member, object, object_type);
2148 return error_mark_node;
2151 /* The standard does not seem to actually say that MEMBER must be a
2152 member of OBJECT_TYPE. However, that is clearly what is
2154 if (DECL_P (member))
2156 member_scope = DECL_CLASS_CONTEXT (member);
2158 if (TREE_DEPRECATED (member))
2159 warn_deprecated_use (member, NULL_TREE);
2162 member_scope = BINFO_TYPE (BASELINK_ACCESS_BINFO (member));
2163 /* If MEMBER is from an anonymous aggregate, MEMBER_SCOPE will
2164 presently be the anonymous union. Go outwards until we find a
2165 type related to OBJECT_TYPE. */
2166 while (ANON_AGGR_TYPE_P (member_scope)
2167 && !same_type_ignoring_top_level_qualifiers_p (member_scope,
2169 member_scope = TYPE_CONTEXT (member_scope);
2170 if (!member_scope || !DERIVED_FROM_P (member_scope, object_type))
2172 if (complain & tf_error)
2174 if (TREE_CODE (member) == FIELD_DECL)
2175 error ("invalid use of nonstatic data member %qE", member);
2177 error ("%qD is not a member of %qT", member, object_type);
2179 return error_mark_node;
2182 /* Transform `(a, b).x' into `(*(a, &b)).x', `(a ? b : c).x' into
2183 `(*(a ? &b : &c)).x', and so on. A COND_EXPR is only an lvalue
2184 in the front end; only _DECLs and _REFs are lvalues in the back end. */
2186 tree temp = unary_complex_lvalue (ADDR_EXPR, object);
2188 object = cp_build_indirect_ref (temp, RO_NULL, complain);
2191 /* In [expr.ref], there is an explicit list of the valid choices for
2192 MEMBER. We check for each of those cases here. */
2193 if (TREE_CODE (member) == VAR_DECL)
2195 /* A static data member. */
2197 mark_exp_read (object);
2198 /* If OBJECT has side-effects, they are supposed to occur. */
2199 if (TREE_SIDE_EFFECTS (object))
2200 result = build2 (COMPOUND_EXPR, TREE_TYPE (result), object, result);
2202 else if (TREE_CODE (member) == FIELD_DECL)
2204 /* A non-static data member. */
2209 null_object_p = (TREE_CODE (object) == INDIRECT_REF
2210 && integer_zerop (TREE_OPERAND (object, 0)));
2212 /* Convert OBJECT to the type of MEMBER. */
2213 if (!same_type_p (TYPE_MAIN_VARIANT (object_type),
2214 TYPE_MAIN_VARIANT (member_scope)))
2219 binfo = lookup_base (access_path ? access_path : object_type,
2220 member_scope, ba_unique, &kind);
2221 if (binfo == error_mark_node)
2222 return error_mark_node;
2224 /* It is invalid to try to get to a virtual base of a
2225 NULL object. The most common cause is invalid use of
2227 if (null_object_p && kind == bk_via_virtual)
2229 if (complain & tf_error)
2231 error ("invalid access to non-static data member %qD of "
2234 error ("(perhaps the %<offsetof%> macro was used incorrectly)");
2236 return error_mark_node;
2239 /* Convert to the base. */
2240 object = build_base_path (PLUS_EXPR, object, binfo,
2241 /*nonnull=*/1, complain);
2242 /* If we found the base successfully then we should be able
2243 to convert to it successfully. */
2244 gcc_assert (object != error_mark_node);
2247 /* Complain about other invalid uses of offsetof, even though they will
2248 give the right answer. Note that we complain whether or not they
2249 actually used the offsetof macro, since there's no way to know at this
2250 point. So we just give a warning, instead of a pedwarn. */
2251 /* Do not produce this warning for base class field references, because
2252 we know for a fact that didn't come from offsetof. This does occur
2253 in various testsuite cases where a null object is passed where a
2254 vtable access is required. */
2255 if (null_object_p && warn_invalid_offsetof
2256 && CLASSTYPE_NON_STD_LAYOUT (object_type)
2257 && !DECL_FIELD_IS_BASE (member)
2258 && cp_unevaluated_operand == 0
2259 && (complain & tf_warning))
2261 warning (OPT_Winvalid_offsetof,
2262 "invalid access to non-static data member %qD "
2263 " of NULL object", member);
2264 warning (OPT_Winvalid_offsetof,
2265 "(perhaps the %<offsetof%> macro was used incorrectly)");
2268 /* If MEMBER is from an anonymous aggregate, we have converted
2269 OBJECT so that it refers to the class containing the
2270 anonymous union. Generate a reference to the anonymous union
2271 itself, and recur to find MEMBER. */
2272 if (ANON_AGGR_TYPE_P (DECL_CONTEXT (member))
2273 /* When this code is called from build_field_call, the
2274 object already has the type of the anonymous union.
2275 That is because the COMPONENT_REF was already
2276 constructed, and was then disassembled before calling
2277 build_field_call. After the function-call code is
2278 cleaned up, this waste can be eliminated. */
2279 && (!same_type_ignoring_top_level_qualifiers_p
2280 (TREE_TYPE (object), DECL_CONTEXT (member))))
2282 tree anonymous_union;
2284 anonymous_union = lookup_anon_field (TREE_TYPE (object),
2285 DECL_CONTEXT (member));
2286 object = build_class_member_access_expr (object,
2288 /*access_path=*/NULL_TREE,
2293 /* Compute the type of the field, as described in [expr.ref]. */
2294 type_quals = TYPE_UNQUALIFIED;
2295 member_type = TREE_TYPE (member);
2296 if (TREE_CODE (member_type) != REFERENCE_TYPE)
2298 type_quals = (cp_type_quals (member_type)
2299 | cp_type_quals (object_type));
2301 /* A field is const (volatile) if the enclosing object, or the
2302 field itself, is const (volatile). But, a mutable field is
2303 not const, even within a const object. */
2304 if (DECL_MUTABLE_P (member))
2305 type_quals &= ~TYPE_QUAL_CONST;
2306 member_type = cp_build_qualified_type (member_type, type_quals);
2309 result = build3 (COMPONENT_REF, member_type, object, member,
2311 result = fold_if_not_in_template (result);
2313 /* Mark the expression const or volatile, as appropriate. Even
2314 though we've dealt with the type above, we still have to mark the
2315 expression itself. */
2316 if (type_quals & TYPE_QUAL_CONST)
2317 TREE_READONLY (result) = 1;
2318 if (type_quals & TYPE_QUAL_VOLATILE)
2319 TREE_THIS_VOLATILE (result) = 1;
2321 else if (BASELINK_P (member))
2323 /* The member is a (possibly overloaded) member function. */
2327 /* If the MEMBER is exactly one static member function, then we
2328 know the type of the expression. Otherwise, we must wait
2329 until overload resolution has been performed. */
2330 functions = BASELINK_FUNCTIONS (member);
2331 if (TREE_CODE (functions) == FUNCTION_DECL
2332 && DECL_STATIC_FUNCTION_P (functions))
2333 type = TREE_TYPE (functions);
2335 type = unknown_type_node;
2336 /* Note that we do not convert OBJECT to the BASELINK_BINFO
2337 base. That will happen when the function is called. */
2338 result = build3 (COMPONENT_REF, type, object, member, NULL_TREE);
2340 else if (TREE_CODE (member) == CONST_DECL)
2342 /* The member is an enumerator. */
2344 /* If OBJECT has side-effects, they are supposed to occur. */
2345 if (TREE_SIDE_EFFECTS (object))
2346 result = build2 (COMPOUND_EXPR, TREE_TYPE (result),
2349 else if ((using_decl = strip_using_decl (member)) != member)
2350 result = build_class_member_access_expr (object,
2352 access_path, preserve_reference,
2356 if (complain & tf_error)
2357 error ("invalid use of %qD", member);
2358 return error_mark_node;
2361 if (!preserve_reference)
2364 If E2 is declared to have type "reference to T", then ... the
2365 type of E1.E2 is T. */
2366 result = convert_from_reference (result);
2371 /* Return the destructor denoted by OBJECT.SCOPE::DTOR_NAME, or, if
2372 SCOPE is NULL, by OBJECT.DTOR_NAME, where DTOR_NAME is ~type. */
2375 lookup_destructor (tree object, tree scope, tree dtor_name)
2377 tree object_type = TREE_TYPE (object);
2378 tree dtor_type = TREE_OPERAND (dtor_name, 0);
2381 if (scope && !check_dtor_name (scope, dtor_type))
2383 error ("qualified type %qT does not match destructor name ~%qT",
2385 return error_mark_node;
2387 if (TREE_CODE (dtor_type) == IDENTIFIER_NODE)
2389 /* In a template, names we can't find a match for are still accepted
2390 destructor names, and we check them here. */
2391 if (check_dtor_name (object_type, dtor_type))
2392 dtor_type = object_type;
2395 error ("object type %qT does not match destructor name ~%qT",
2396 object_type, dtor_type);
2397 return error_mark_node;
2401 else if (!DERIVED_FROM_P (dtor_type, TYPE_MAIN_VARIANT (object_type)))
2403 error ("the type being destroyed is %qT, but the destructor refers to %qT",
2404 TYPE_MAIN_VARIANT (object_type), dtor_type);
2405 return error_mark_node;
2407 expr = lookup_member (dtor_type, complete_dtor_identifier,
2408 /*protect=*/1, /*want_type=*/false,
2409 tf_warning_or_error);
2410 expr = (adjust_result_of_qualified_name_lookup
2411 (expr, dtor_type, object_type));
2415 /* An expression of the form "A::template B" has been resolved to
2416 DECL. Issue a diagnostic if B is not a template or template
2420 check_template_keyword (tree decl)
2422 /* The standard says:
2426 If a name prefixed by the keyword template is not a member
2427 template, the program is ill-formed.
2429 DR 228 removed the restriction that the template be a member
2432 DR 96, if accepted would add the further restriction that explicit
2433 template arguments must be provided if the template keyword is
2434 used, but, as of 2005-10-16, that DR is still in "drafting". If
2435 this DR is accepted, then the semantic checks here can be
2436 simplified, as the entity named must in fact be a template
2437 specialization, rather than, as at present, a set of overloaded
2438 functions containing at least one template function. */
2439 if (TREE_CODE (decl) != TEMPLATE_DECL
2440 && TREE_CODE (decl) != TEMPLATE_ID_EXPR)
2442 if (!is_overloaded_fn (decl))
2443 permerror (input_location, "%qD is not a template", decl);
2448 if (BASELINK_P (fns))
2449 fns = BASELINK_FUNCTIONS (fns);
2452 tree fn = OVL_CURRENT (fns);
2453 if (TREE_CODE (fn) == TEMPLATE_DECL
2454 || TREE_CODE (fn) == TEMPLATE_ID_EXPR)
2456 if (TREE_CODE (fn) == FUNCTION_DECL
2457 && DECL_USE_TEMPLATE (fn)
2458 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (fn)))
2460 fns = OVL_NEXT (fns);
2463 permerror (input_location, "%qD is not a template", decl);
2468 /* This function is called by the parser to process a class member
2469 access expression of the form OBJECT.NAME. NAME is a node used by
2470 the parser to represent a name; it is not yet a DECL. It may,
2471 however, be a BASELINK where the BASELINK_FUNCTIONS is a
2472 TEMPLATE_ID_EXPR. Templates must be looked up by the parser, and
2473 there is no reason to do the lookup twice, so the parser keeps the
2474 BASELINK. TEMPLATE_P is true iff NAME was explicitly declared to
2475 be a template via the use of the "A::template B" syntax. */
2478 finish_class_member_access_expr (tree object, tree name, bool template_p,
2479 tsubst_flags_t complain)
2484 tree access_path = NULL_TREE;
2485 tree orig_object = object;
2486 tree orig_name = name;
2488 if (object == error_mark_node || name == error_mark_node)
2489 return error_mark_node;
2491 /* If OBJECT is an ObjC class instance, we must obey ObjC access rules. */
2492 if (!objc_is_public (object, name))
2493 return error_mark_node;
2495 object_type = TREE_TYPE (object);
2497 if (processing_template_decl)
2499 if (/* If OBJECT_TYPE is dependent, so is OBJECT.NAME. */
2500 dependent_type_p (object_type)
2501 /* If NAME is just an IDENTIFIER_NODE, then the expression
2503 || TREE_CODE (object) == IDENTIFIER_NODE
2504 /* If NAME is "f<args>", where either 'f' or 'args' is
2505 dependent, then the expression is dependent. */
2506 || (TREE_CODE (name) == TEMPLATE_ID_EXPR
2507 && dependent_template_id_p (TREE_OPERAND (name, 0),
2508 TREE_OPERAND (name, 1)))
2509 /* If NAME is "T::X" where "T" is dependent, then the
2510 expression is dependent. */
2511 || (TREE_CODE (name) == SCOPE_REF
2512 && TYPE_P (TREE_OPERAND (name, 0))
2513 && dependent_type_p (TREE_OPERAND (name, 0))))
2514 return build_min_nt (COMPONENT_REF, object, name, NULL_TREE);
2515 object = build_non_dependent_expr (object);
2517 else if (c_dialect_objc ()
2518 && TREE_CODE (name) == IDENTIFIER_NODE
2519 && (expr = objc_maybe_build_component_ref (object, name)))
2524 The type of the first expression shall be "class object" (of a
2526 if (!currently_open_class (object_type)
2527 && !complete_type_or_maybe_complain (object_type, object, complain))
2528 return error_mark_node;
2529 if (!CLASS_TYPE_P (object_type))
2531 if (complain & tf_error)
2533 if (POINTER_TYPE_P (object_type)
2534 && CLASS_TYPE_P (TREE_TYPE (object_type)))
2535 error ("request for member %qD in %qE, which is of pointer "
2536 "type %qT (maybe you meant to use %<->%> ?)",
2537 name, object, object_type);
2539 error ("request for member %qD in %qE, which is of non-class "
2540 "type %qT", name, object, object_type);
2542 return error_mark_node;
2545 if (BASELINK_P (name))
2546 /* A member function that has already been looked up. */
2550 bool is_template_id = false;
2551 tree template_args = NULL_TREE;
2554 if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
2556 is_template_id = true;
2557 template_args = TREE_OPERAND (name, 1);
2558 name = TREE_OPERAND (name, 0);
2560 if (TREE_CODE (name) == OVERLOAD)
2561 name = DECL_NAME (get_first_fn (name));
2562 else if (DECL_P (name))
2563 name = DECL_NAME (name);
2566 if (TREE_CODE (name) == SCOPE_REF)
2568 /* A qualified name. The qualifying class or namespace `S'
2569 has already been looked up; it is either a TYPE or a
2571 scope = TREE_OPERAND (name, 0);
2572 name = TREE_OPERAND (name, 1);
2574 /* If SCOPE is a namespace, then the qualified name does not
2575 name a member of OBJECT_TYPE. */
2576 if (TREE_CODE (scope) == NAMESPACE_DECL)
2578 if (complain & tf_error)
2579 error ("%<%D::%D%> is not a member of %qT",
2580 scope, name, object_type);
2581 return error_mark_node;
2584 gcc_assert (CLASS_TYPE_P (scope));
2585 gcc_assert (TREE_CODE (name) == IDENTIFIER_NODE
2586 || TREE_CODE (name) == BIT_NOT_EXPR);
2588 if (constructor_name_p (name, scope))
2590 if (complain & tf_error)
2591 error ("cannot call constructor %<%T::%D%> directly",
2593 return error_mark_node;
2596 /* Find the base of OBJECT_TYPE corresponding to SCOPE. */
2597 access_path = lookup_base (object_type, scope, ba_check, NULL);
2598 if (access_path == error_mark_node)
2599 return error_mark_node;
2602 if (complain & tf_error)
2603 error ("%qT is not a base of %qT", scope, object_type);
2604 return error_mark_node;
2610 access_path = object_type;
2613 if (TREE_CODE (name) == BIT_NOT_EXPR)
2614 member = lookup_destructor (object, scope, name);
2617 /* Look up the member. */
2618 member = lookup_member (access_path, name, /*protect=*/1,
2619 /*want_type=*/false, complain);
2620 if (member == NULL_TREE)
2622 if (complain & tf_error)
2623 error ("%qD has no member named %qE",
2624 TREE_CODE (access_path) == TREE_BINFO
2625 ? TREE_TYPE (access_path) : object_type, name);
2626 return error_mark_node;
2628 if (member == error_mark_node)
2629 return error_mark_node;
2634 tree templ = member;
2636 if (BASELINK_P (templ))
2637 templ = lookup_template_function (templ, template_args);
2640 if (complain & tf_error)
2641 error ("%qD is not a member template function", name);
2642 return error_mark_node;
2647 if (TREE_DEPRECATED (member))
2648 warn_deprecated_use (member, NULL_TREE);
2651 check_template_keyword (member);
2653 expr = build_class_member_access_expr (object, member, access_path,
2654 /*preserve_reference=*/false,
2656 if (processing_template_decl && expr != error_mark_node)
2658 if (BASELINK_P (member))
2660 if (TREE_CODE (orig_name) == SCOPE_REF)
2661 BASELINK_QUALIFIED_P (member) = 1;
2664 return build_min_non_dep (COMPONENT_REF, expr,
2665 orig_object, orig_name,
2672 /* Return an expression for the MEMBER_NAME field in the internal
2673 representation of PTRMEM, a pointer-to-member function. (Each
2674 pointer-to-member function type gets its own RECORD_TYPE so it is
2675 more convenient to access the fields by name than by FIELD_DECL.)
2676 This routine converts the NAME to a FIELD_DECL and then creates the
2677 node for the complete expression. */
2680 build_ptrmemfunc_access_expr (tree ptrmem, tree member_name)
2686 /* This code is a stripped down version of
2687 build_class_member_access_expr. It does not work to use that
2688 routine directly because it expects the object to be of class
2690 ptrmem_type = TREE_TYPE (ptrmem);
2691 gcc_assert (TYPE_PTRMEMFUNC_P (ptrmem_type));
2692 member = lookup_member (ptrmem_type, member_name, /*protect=*/0,
2693 /*want_type=*/false, tf_warning_or_error);
2694 member_type = cp_build_qualified_type (TREE_TYPE (member),
2695 cp_type_quals (ptrmem_type));
2696 return fold_build3_loc (input_location,
2697 COMPONENT_REF, member_type,
2698 ptrmem, member, NULL_TREE);
2701 /* Given an expression PTR for a pointer, return an expression
2702 for the value pointed to.
2703 ERRORSTRING is the name of the operator to appear in error messages.
2705 This function may need to overload OPERATOR_FNNAME.
2706 Must also handle REFERENCE_TYPEs for C++. */
2709 build_x_indirect_ref (tree expr, ref_operator errorstring,
2710 tsubst_flags_t complain)
2712 tree orig_expr = expr;
2715 if (processing_template_decl)
2717 /* Retain the type if we know the operand is a pointer. */
2718 if (TREE_TYPE (expr) && POINTER_TYPE_P (TREE_TYPE (expr)))
2719 return build_min (INDIRECT_REF, TREE_TYPE (TREE_TYPE (expr)), expr);
2720 if (type_dependent_expression_p (expr))
2721 return build_min_nt (INDIRECT_REF, expr);
2722 expr = build_non_dependent_expr (expr);
2725 rval = build_new_op (INDIRECT_REF, LOOKUP_NORMAL, expr, NULL_TREE,
2726 NULL_TREE, /*overload=*/NULL, complain);
2728 rval = cp_build_indirect_ref (expr, errorstring, complain);
2730 if (processing_template_decl && rval != error_mark_node)
2731 return build_min_non_dep (INDIRECT_REF, rval, orig_expr);
2736 /* Helper function called from c-common. */
2738 build_indirect_ref (location_t loc ATTRIBUTE_UNUSED,
2739 tree ptr, ref_operator errorstring)
2741 return cp_build_indirect_ref (ptr, errorstring, tf_warning_or_error);
2745 cp_build_indirect_ref (tree ptr, ref_operator errorstring,
2746 tsubst_flags_t complain)
2750 if (ptr == error_mark_node)
2751 return error_mark_node;
2753 if (ptr == current_class_ptr)
2754 return current_class_ref;
2756 pointer = (TREE_CODE (TREE_TYPE (ptr)) == REFERENCE_TYPE
2757 ? ptr : decay_conversion (ptr));
2758 type = TREE_TYPE (pointer);
2760 if (POINTER_TYPE_P (type))
2764 If the type of the expression is "pointer to T," the type
2765 of the result is "T." */
2766 tree t = TREE_TYPE (type);
2768 if (CONVERT_EXPR_P (ptr)
2769 || TREE_CODE (ptr) == VIEW_CONVERT_EXPR)
2771 /* If a warning is issued, mark it to avoid duplicates from
2772 the backend. This only needs to be done at
2773 warn_strict_aliasing > 2. */
2774 if (warn_strict_aliasing > 2)
2775 if (strict_aliasing_warning (TREE_TYPE (TREE_OPERAND (ptr, 0)),
2776 type, TREE_OPERAND (ptr, 0)))
2777 TREE_NO_WARNING (ptr) = 1;
2780 if (VOID_TYPE_P (t))
2782 /* A pointer to incomplete type (other than cv void) can be
2783 dereferenced [expr.unary.op]/1 */
2784 if (complain & tf_error)
2785 error ("%qT is not a pointer-to-object type", type);
2786 return error_mark_node;
2788 else if (TREE_CODE (pointer) == ADDR_EXPR
2789 && same_type_p (t, TREE_TYPE (TREE_OPERAND (pointer, 0))))
2790 /* The POINTER was something like `&x'. We simplify `*&x' to
2792 return TREE_OPERAND (pointer, 0);
2795 tree ref = build1 (INDIRECT_REF, t, pointer);
2797 /* We *must* set TREE_READONLY when dereferencing a pointer to const,
2798 so that we get the proper error message if the result is used
2799 to assign to. Also, &* is supposed to be a no-op. */
2800 TREE_READONLY (ref) = CP_TYPE_CONST_P (t);
2801 TREE_THIS_VOLATILE (ref) = CP_TYPE_VOLATILE_P (t);
2802 TREE_SIDE_EFFECTS (ref)
2803 = (TREE_THIS_VOLATILE (ref) || TREE_SIDE_EFFECTS (pointer));
2807 else if (!(complain & tf_error))
2808 /* Don't emit any errors; we'll just return ERROR_MARK_NODE later. */
2810 /* `pointer' won't be an error_mark_node if we were given a
2811 pointer to member, so it's cool to check for this here. */
2812 else if (TYPE_PTR_TO_MEMBER_P (type))
2813 switch (errorstring)
2815 case RO_ARRAY_INDEXING:
2816 error ("invalid use of array indexing on pointer to member");
2819 error ("invalid use of unary %<*%> on pointer to member");
2821 case RO_IMPLICIT_CONVERSION:
2822 error ("invalid use of implicit conversion on pointer to member");
2827 else if (pointer != error_mark_node)
2828 invalid_indirection_error (input_location, type, errorstring);
2830 return error_mark_node;
2833 /* This handles expressions of the form "a[i]", which denotes
2836 This is logically equivalent in C to *(a+i), but we may do it differently.
2837 If A is a variable or a member, we generate a primitive ARRAY_REF.
2838 This avoids forcing the array out of registers, and can work on
2839 arrays that are not lvalues (for example, members of structures returned
2842 If INDEX is of some user-defined type, it must be converted to
2843 integer type. Otherwise, to make a compatible PLUS_EXPR, it
2844 will inherit the type of the array, which will be some pointer type.
2846 LOC is the location to use in building the array reference. */
2849 cp_build_array_ref (location_t loc, tree array, tree idx,
2850 tsubst_flags_t complain)
2856 if (complain & tf_error)
2857 error_at (loc, "subscript missing in array reference");
2858 return error_mark_node;
2861 if (TREE_TYPE (array) == error_mark_node
2862 || TREE_TYPE (idx) == error_mark_node)
2863 return error_mark_node;
2865 /* If ARRAY is a COMPOUND_EXPR or COND_EXPR, move our reference
2867 switch (TREE_CODE (array))
2871 tree value = cp_build_array_ref (loc, TREE_OPERAND (array, 1), idx,
2873 ret = build2 (COMPOUND_EXPR, TREE_TYPE (value),
2874 TREE_OPERAND (array, 0), value);
2875 SET_EXPR_LOCATION (ret, loc);
2880 ret = build_conditional_expr
2881 (TREE_OPERAND (array, 0),
2882 cp_build_array_ref (loc, TREE_OPERAND (array, 1), idx,
2884 cp_build_array_ref (loc, TREE_OPERAND (array, 2), idx,
2886 tf_warning_or_error);
2887 protected_set_expr_location (ret, loc);
2894 if (TREE_CODE (TREE_TYPE (array)) == ARRAY_TYPE)
2898 warn_array_subscript_with_type_char (idx);
2900 if (!INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (TREE_TYPE (idx)))
2902 if (complain & tf_error)
2903 error_at (loc, "array subscript is not an integer");
2904 return error_mark_node;
2907 /* Apply integral promotions *after* noticing character types.
2908 (It is unclear why we do these promotions -- the standard
2909 does not say that we should. In fact, the natural thing would
2910 seem to be to convert IDX to ptrdiff_t; we're performing
2911 pointer arithmetic.) */
2912 idx = perform_integral_promotions (idx);
2914 /* An array that is indexed by a non-constant
2915 cannot be stored in a register; we must be able to do
2916 address arithmetic on its address.
2917 Likewise an array of elements of variable size. */
2918 if (TREE_CODE (idx) != INTEGER_CST
2919 || (COMPLETE_TYPE_P (TREE_TYPE (TREE_TYPE (array)))
2920 && (TREE_CODE (TYPE_SIZE (TREE_TYPE (TREE_TYPE (array))))
2923 if (!cxx_mark_addressable (array))
2924 return error_mark_node;
2927 /* An array that is indexed by a constant value which is not within
2928 the array bounds cannot be stored in a register either; because we
2929 would get a crash in store_bit_field/extract_bit_field when trying
2930 to access a non-existent part of the register. */
2931 if (TREE_CODE (idx) == INTEGER_CST
2932 && TYPE_DOMAIN (TREE_TYPE (array))
2933 && ! int_fits_type_p (idx, TYPE_DOMAIN (TREE_TYPE (array))))
2935 if (!cxx_mark_addressable (array))
2936 return error_mark_node;
2939 if (!lvalue_p (array) && (complain & tf_error))
2940 pedwarn (loc, OPT_pedantic,
2941 "ISO C++ forbids subscripting non-lvalue array");
2943 /* Note in C++ it is valid to subscript a `register' array, since
2944 it is valid to take the address of something with that
2945 storage specification. */
2949 while (TREE_CODE (foo) == COMPONENT_REF)
2950 foo = TREE_OPERAND (foo, 0);
2951 if (TREE_CODE (foo) == VAR_DECL && DECL_REGISTER (foo)
2952 && (complain & tf_warning))
2953 warning_at (loc, OPT_Wextra,
2954 "subscripting array declared %<register%>");
2957 type = TREE_TYPE (TREE_TYPE (array));
2958 rval = build4 (ARRAY_REF, type, array, idx, NULL_TREE, NULL_TREE);
2959 /* Array ref is const/volatile if the array elements are
2960 or if the array is.. */
2961 TREE_READONLY (rval)
2962 |= (CP_TYPE_CONST_P (type) | TREE_READONLY (array));
2963 TREE_SIDE_EFFECTS (rval)
2964 |= (CP_TYPE_VOLATILE_P (type) | TREE_SIDE_EFFECTS (array));
2965 TREE_THIS_VOLATILE (rval)
2966 |= (CP_TYPE_VOLATILE_P (type) | TREE_THIS_VOLATILE (array));
2967 ret = require_complete_type_sfinae (fold_if_not_in_template (rval),
2969 protected_set_expr_location (ret, loc);
2974 tree ar = default_conversion (array);
2975 tree ind = default_conversion (idx);
2977 /* Put the integer in IND to simplify error checking. */
2978 if (TREE_CODE (TREE_TYPE (ar)) == INTEGER_TYPE)
2985 if (ar == error_mark_node)
2988 if (TREE_CODE (TREE_TYPE (ar)) != POINTER_TYPE)
2990 if (complain & tf_error)
2991 error_at (loc, "subscripted value is neither array nor pointer");
2992 return error_mark_node;
2994 if (TREE_CODE (TREE_TYPE (ind)) != INTEGER_TYPE)
2996 if (complain & tf_error)
2997 error_at (loc, "array subscript is not an integer");
2998 return error_mark_node;
3001 warn_array_subscript_with_type_char (idx);
3003 ret = cp_build_indirect_ref (cp_build_binary_op (input_location,
3008 protected_set_expr_location (ret, loc);
3013 /* Entry point for Obj-C++. */
3016 build_array_ref (location_t loc, tree array, tree idx)
3018 return cp_build_array_ref (loc, array, idx, tf_warning_or_error);
3021 /* Resolve a pointer to member function. INSTANCE is the object
3022 instance to use, if the member points to a virtual member.
3024 This used to avoid checking for virtual functions if basetype
3025 has no virtual functions, according to an earlier ANSI draft.
3026 With the final ISO C++ rules, such an optimization is
3027 incorrect: A pointer to a derived member can be static_cast
3028 to pointer-to-base-member, as long as the dynamic object
3029 later has the right member. */
3032 get_member_function_from_ptrfunc (tree *instance_ptrptr, tree function)
3034 if (TREE_CODE (function) == OFFSET_REF)
3035 function = TREE_OPERAND (function, 1);
3037 if (TYPE_PTRMEMFUNC_P (TREE_TYPE (function)))
3039 tree idx, delta, e1, e2, e3, vtbl, basetype;
3040 tree fntype = TYPE_PTRMEMFUNC_FN_TYPE (TREE_TYPE (function));
3042 tree instance_ptr = *instance_ptrptr;
3043 tree instance_save_expr = 0;
3044 if (instance_ptr == error_mark_node)
3046 if (TREE_CODE (function) == PTRMEM_CST)
3048 /* Extracting the function address from a pmf is only
3049 allowed with -Wno-pmf-conversions. It only works for
3051 e1 = build_addr_func (PTRMEM_CST_MEMBER (function));
3052 e1 = convert (fntype, e1);
3057 error ("object missing in use of %qE", function);
3058 return error_mark_node;
3062 if (TREE_SIDE_EFFECTS (instance_ptr))
3063 instance_ptr = instance_save_expr = save_expr (instance_ptr);
3065 if (TREE_SIDE_EFFECTS (function))
3066 function = save_expr (function);
3068 /* Start by extracting all the information from the PMF itself. */
3069 e3 = pfn_from_ptrmemfunc (function);
3070 delta = delta_from_ptrmemfunc (function);
3071 idx = build1 (NOP_EXPR, vtable_index_type, e3);
3072 switch (TARGET_PTRMEMFUNC_VBIT_LOCATION)
3074 case ptrmemfunc_vbit_in_pfn:
3075 e1 = cp_build_binary_op (input_location,
3076 BIT_AND_EXPR, idx, integer_one_node,
3077 tf_warning_or_error);
3078 idx = cp_build_binary_op (input_location,
3079 MINUS_EXPR, idx, integer_one_node,
3080 tf_warning_or_error);
3083 case ptrmemfunc_vbit_in_delta:
3084 e1 = cp_build_binary_op (input_location,
3085 BIT_AND_EXPR, delta, integer_one_node,
3086 tf_warning_or_error);
3087 delta = cp_build_binary_op (input_location,
3088 RSHIFT_EXPR, delta, integer_one_node,
3089 tf_warning_or_error);
3096 /* Convert down to the right base before using the instance. A
3097 special case is that in a pointer to member of class C, C may
3098 be incomplete. In that case, the function will of course be
3099 a member of C, and no conversion is required. In fact,
3100 lookup_base will fail in that case, because incomplete
3101 classes do not have BINFOs. */
3102 basetype = TYPE_METHOD_BASETYPE (TREE_TYPE (fntype));
3103 if (!same_type_ignoring_top_level_qualifiers_p
3104 (basetype, TREE_TYPE (TREE_TYPE (instance_ptr))))
3106 basetype = lookup_base (TREE_TYPE (TREE_TYPE (instance_ptr)),
3107 basetype, ba_check, NULL);
3108 instance_ptr = build_base_path (PLUS_EXPR, instance_ptr, basetype,
3109 1, tf_warning_or_error);
3110 if (instance_ptr == error_mark_node)
3111 return error_mark_node;
3113 /* ...and then the delta in the PMF. */
3114 instance_ptr = fold_build_pointer_plus (instance_ptr, delta);
3116 /* Hand back the adjusted 'this' argument to our caller. */
3117 *instance_ptrptr = instance_ptr;
3119 /* Next extract the vtable pointer from the object. */
3120 vtbl = build1 (NOP_EXPR, build_pointer_type (vtbl_ptr_type_node),
3122 vtbl = cp_build_indirect_ref (vtbl, RO_NULL, tf_warning_or_error);
3123 /* If the object is not dynamic the access invokes undefined
3124 behavior. As it is not executed in this case silence the
3125 spurious warnings it may provoke. */
3126 TREE_NO_WARNING (vtbl) = 1;
3128 /* Finally, extract the function pointer from the vtable. */
3129 e2 = fold_build_pointer_plus_loc (input_location, vtbl, idx);
3130 e2 = cp_build_indirect_ref (e2, RO_NULL, tf_warning_or_error);
3131 TREE_CONSTANT (e2) = 1;
3133 /* When using function descriptors, the address of the
3134 vtable entry is treated as a function pointer. */
3135 if (TARGET_VTABLE_USES_DESCRIPTORS)
3136 e2 = build1 (NOP_EXPR, TREE_TYPE (e2),
3137 cp_build_addr_expr (e2, tf_warning_or_error));
3139 e2 = fold_convert (TREE_TYPE (e3), e2);
3140 e1 = build_conditional_expr (e1, e2, e3, tf_warning_or_error);
3142 /* Make sure this doesn't get evaluated first inside one of the
3143 branches of the COND_EXPR. */
3144 if (instance_save_expr)
3145 e1 = build2 (COMPOUND_EXPR, TREE_TYPE (e1),
3146 instance_save_expr, e1);
3153 /* Used by the C-common bits. */
3155 build_function_call (location_t loc ATTRIBUTE_UNUSED,
3156 tree function, tree params)
3158 return cp_build_function_call (function, params, tf_warning_or_error);
3161 /* Used by the C-common bits. */
3163 build_function_call_vec (location_t loc ATTRIBUTE_UNUSED,
3164 tree function, VEC(tree,gc) *params,
3165 VEC(tree,gc) *origtypes ATTRIBUTE_UNUSED)
3167 VEC(tree,gc) *orig_params = params;
3168 tree ret = cp_build_function_call_vec (function, ¶ms,
3169 tf_warning_or_error);
3171 /* cp_build_function_call_vec can reallocate PARAMS by adding
3172 default arguments. That should never happen here. Verify
3174 gcc_assert (params == orig_params);
3179 /* Build a function call using a tree list of arguments. */
3182 cp_build_function_call (tree function, tree params, tsubst_flags_t complain)
3187 vec = make_tree_vector ();
3188 for (; params != NULL_TREE; params = TREE_CHAIN (params))
3189 VEC_safe_push (tree, gc, vec, TREE_VALUE (params));
3190 ret = cp_build_function_call_vec (function, &vec, complain);
3191 release_tree_vector (vec);
3195 /* Build a function call using varargs. */
3198 cp_build_function_call_nary (tree function, tsubst_flags_t complain, ...)
3204 vec = make_tree_vector ();
3205 va_start (args, complain);
3206 for (t = va_arg (args, tree); t != NULL_TREE; t = va_arg (args, tree))
3207 VEC_safe_push (tree, gc, vec, t);
3209 ret = cp_build_function_call_vec (function, &vec, complain);
3210 release_tree_vector (vec);
3214 /* Build a function call using a vector of arguments. PARAMS may be
3215 NULL if there are no parameters. This changes the contents of
3219 cp_build_function_call_vec (tree function, VEC(tree,gc) **params,
3220 tsubst_flags_t complain)
3222 tree fntype, fndecl;
3224 tree original = function;
3228 VEC(tree,gc) *allocated = NULL;
3231 /* For Objective-C, convert any calls via a cast to OBJC_TYPE_REF
3232 expressions, like those used for ObjC messenger dispatches. */
3233 if (params != NULL && !VEC_empty (tree, *params))
3234 function = objc_rewrite_function_call (function,
3235 VEC_index (tree, *params, 0));
3237 /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
3238 Strip such NOP_EXPRs, since FUNCTION is used in non-lvalue context. */
3239 if (TREE_CODE (function) == NOP_EXPR
3240 && TREE_TYPE (function) == TREE_TYPE (TREE_OPERAND (function, 0)))
3241 function = TREE_OPERAND (function, 0);
3243 if (TREE_CODE (function) == FUNCTION_DECL)
3245 mark_used (function);
3248 /* Convert anything with function type to a pointer-to-function. */
3249 if (DECL_MAIN_P (function) && (complain & tf_error))
3250 pedwarn (input_location, OPT_pedantic,
3251 "ISO C++ forbids calling %<::main%> from within program");
3253 function = build_addr_func (function);
3259 function = build_addr_func (function);
3262 if (function == error_mark_node)
3263 return error_mark_node;
3265 fntype = TREE_TYPE (function);
3267 if (TYPE_PTRMEMFUNC_P (fntype))
3269 if (complain & tf_error)
3270 error ("must use %<.*%> or %<->*%> to call pointer-to-member "
3271 "function in %<%E (...)%>, e.g. %<(... ->* %E) (...)%>",
3272 original, original);
3273 return error_mark_node;
3276 is_method = (TREE_CODE (fntype) == POINTER_TYPE
3277 && TREE_CODE (TREE_TYPE (fntype)) == METHOD_TYPE);
3279 if (!((TREE_CODE (fntype) == POINTER_TYPE
3280 && TREE_CODE (TREE_TYPE (fntype)) == FUNCTION_TYPE)
3282 || TREE_CODE (function) == TEMPLATE_ID_EXPR))
3284 if (complain & tf_error)
3285 error ("%qE cannot be used as a function", original);
3286 return error_mark_node;
3289 /* fntype now gets the type of function pointed to. */
3290 fntype = TREE_TYPE (fntype);
3291 parm_types = TYPE_ARG_TYPES (fntype);
3295 allocated = make_tree_vector ();
3296 params = &allocated;
3299 nargs = convert_arguments (parm_types, params, fndecl, LOOKUP_NORMAL,
3302 return error_mark_node;
3304 argarray = VEC_address (tree, *params);
3306 /* Check for errors in format strings and inappropriately
3308 check_function_arguments (fntype, nargs, argarray);
3310 ret = build_cxx_call (function, nargs, argarray);
3312 if (allocated != NULL)
3313 release_tree_vector (allocated);
3318 /* Subroutine of convert_arguments.
3319 Warn about wrong number of args are genereted. */
3322 warn_args_num (location_t loc, tree fndecl, bool too_many_p)
3326 if (TREE_CODE (TREE_TYPE (fndecl)) == METHOD_TYPE)
3328 if (DECL_NAME (fndecl) == NULL_TREE
3329 || IDENTIFIER_HAS_TYPE_VALUE (DECL_NAME (fndecl)))
3332 ? G_("too many arguments to constructor %q#D")
3333 : G_("too few arguments to constructor %q#D"),
3338 ? G_("too many arguments to member function %q#D")
3339 : G_("too few arguments to member function %q#D"),
3345 ? G_("too many arguments to function %q#D")
3346 : G_("too few arguments to function %q#D"),
3348 inform (DECL_SOURCE_LOCATION (fndecl),
3353 if (c_dialect_objc () && objc_message_selector ())
3356 ? G_("too many arguments to method %q#D")
3357 : G_("too few arguments to method %q#D"),
3358 objc_message_selector ());
3360 error_at (loc, too_many_p ? G_("too many arguments to function")
3361 : G_("too few arguments to function"));
3365 /* Convert the actual parameter expressions in the list VALUES to the
3366 types in the list TYPELIST. The converted expressions are stored
3367 back in the VALUES vector.
3368 If parmdecls is exhausted, or when an element has NULL as its type,
3369 perform the default conversions.
3371 NAME is an IDENTIFIER_NODE or 0. It is used only for error messages.
3373 This is also where warnings about wrong number of args are generated.
3375 Returns the actual number of arguments processed (which might be less
3376 than the length of the vector), or -1 on error.
3378 In C++, unspecified trailing parameters can be filled in with their
3379 default arguments, if such were specified. Do so here. */
3382 convert_arguments (tree typelist, VEC(tree,gc) **values, tree fndecl,
3383 int flags, tsubst_flags_t complain)
3388 /* Argument passing is always copy-initialization. */
3389 flags |= LOOKUP_ONLYCONVERTING;
3391 for (i = 0, typetail = typelist;
3392 i < VEC_length (tree, *values);
3395 tree type = typetail ? TREE_VALUE (typetail) : 0;
3396 tree val = VEC_index (tree, *values, i);
3398 if (val == error_mark_node || type == error_mark_node)
3401 if (type == void_type_node)
3403 if (complain & tf_error)
3405 warn_args_num (input_location, fndecl, /*too_many_p=*/true);
3412 /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
3413 Strip such NOP_EXPRs, since VAL is used in non-lvalue context. */
3414 if (TREE_CODE (val) == NOP_EXPR
3415 && TREE_TYPE (val) == TREE_TYPE (TREE_OPERAND (val, 0))
3416 && (type == 0 || TREE_CODE (type) != REFERENCE_TYPE))
3417 val = TREE_OPERAND (val, 0);
3419 if (type == 0 || TREE_CODE (type) != REFERENCE_TYPE)
3421 if (TREE_CODE (TREE_TYPE (val)) == ARRAY_TYPE
3422 || TREE_CODE (TREE_TYPE (val)) == FUNCTION_TYPE
3423 || TREE_CODE (TREE_TYPE (val)) == METHOD_TYPE)
3424 val = decay_conversion (val);
3427 if (val == error_mark_node)
3432 /* Formal parm type is specified by a function prototype. */
3435 if (!COMPLETE_TYPE_P (complete_type (type)))
3437 if (complain & tf_error)
3440 error ("parameter %P of %qD has incomplete type %qT",
3443 error ("parameter %P has incomplete type %qT", i, type);
3445 parmval = error_mark_node;
3449 parmval = convert_for_initialization
3450 (NULL_TREE, type, val, flags,
3451 ICR_ARGPASS, fndecl, i, complain);
3452 parmval = convert_for_arg_passing (type, parmval);
3455 if (parmval == error_mark_node)
3458 VEC_replace (tree, *values, i, parmval);
3462 if (fndecl && DECL_BUILT_IN (fndecl)
3463 && DECL_FUNCTION_CODE (fndecl) == BUILT_IN_CONSTANT_P)
3464 /* Don't do ellipsis conversion for __built_in_constant_p
3465 as this will result in spurious errors for non-trivial
3467 val = require_complete_type_sfinae (val, complain);
3469 val = convert_arg_to_ellipsis (val);
3471 VEC_replace (tree, *values, i, val);
3475 typetail = TREE_CHAIN (typetail);
3478 if (typetail != 0 && typetail != void_list_node)
3480 /* See if there are default arguments that can be used. Because
3481 we hold default arguments in the FUNCTION_TYPE (which is so
3482 wrong), we can see default parameters here from deduced
3483 contexts (and via typeof) for indirect function calls.
3484 Fortunately we know whether we have a function decl to
3485 provide default arguments in a language conformant
3487 if (fndecl && TREE_PURPOSE (typetail)
3488 && TREE_CODE (TREE_PURPOSE (typetail)) != DEFAULT_ARG)
3490 for (; typetail != void_list_node; ++i)
3493 = convert_default_arg (TREE_VALUE (typetail),
3494 TREE_PURPOSE (typetail),
3497 if (parmval == error_mark_node)
3500 VEC_safe_push (tree, gc, *values, parmval);
3501 typetail = TREE_CHAIN (typetail);
3502 /* ends with `...'. */
3503 if (typetail == NULL_TREE)
3509 if (complain & tf_error)
3510 warn_args_num (input_location, fndecl, /*too_many_p=*/false);
3518 /* Build a binary-operation expression, after performing default
3519 conversions on the operands. CODE is the kind of expression to
3520 build. ARG1 and ARG2 are the arguments. ARG1_CODE and ARG2_CODE
3521 are the tree codes which correspond to ARG1 and ARG2 when issuing
3522 warnings about possibly misplaced parentheses. They may differ
3523 from the TREE_CODE of ARG1 and ARG2 if the parser has done constant
3524 folding (e.g., if the parser sees "a | 1 + 1", it may call this
3525 routine with ARG2 being an INTEGER_CST and ARG2_CODE == PLUS_EXPR).
3526 To avoid issuing any parentheses warnings, pass ARG1_CODE and/or
3527 ARG2_CODE as ERROR_MARK. */
3530 build_x_binary_op (enum tree_code code, tree arg1, enum tree_code arg1_code,
3531 tree arg2, enum tree_code arg2_code, tree *overload,
3532 tsubst_flags_t complain)
3541 if (processing_template_decl)
3543 if (type_dependent_expression_p (arg1)
3544 || type_dependent_expression_p (arg2))
3545 return build_min_nt (code, arg1, arg2);
3546 arg1 = build_non_dependent_expr (arg1);
3547 arg2 = build_non_dependent_expr (arg2);
3550 if (code == DOTSTAR_EXPR)
3551 expr = build_m_component_ref (arg1, arg2);
3553 expr = build_new_op (code, LOOKUP_NORMAL, arg1, arg2, NULL_TREE,
3554 overload, complain);
3556 /* Check for cases such as x+y<<z which users are likely to
3557 misinterpret. But don't warn about obj << x + y, since that is a
3558 common idiom for I/O. */
3559 if (warn_parentheses
3560 && (complain & tf_warning)
3561 && !processing_template_decl
3562 && !error_operand_p (arg1)
3563 && !error_operand_p (arg2)
3564 && (code != LSHIFT_EXPR
3565 || !CLASS_TYPE_P (TREE_TYPE (arg1))))
3566 warn_about_parentheses (code, arg1_code, orig_arg1, arg2_code, orig_arg2);
3568 if (processing_template_decl && expr != error_mark_node)
3569 return build_min_non_dep (code, expr, orig_arg1, orig_arg2);
3574 /* Build and return an ARRAY_REF expression. */
3577 build_x_array_ref (tree arg1, tree arg2, tsubst_flags_t complain)
3579 tree orig_arg1 = arg1;
3580 tree orig_arg2 = arg2;
3583 if (processing_template_decl)
3585 if (type_dependent_expression_p (arg1)
3586 || type_dependent_expression_p (arg2))
3587 return build_min_nt (ARRAY_REF, arg1, arg2,
3588 NULL_TREE, NULL_TREE);
3589 arg1 = build_non_dependent_expr (arg1);
3590 arg2 = build_non_dependent_expr (arg2);
3593 expr = build_new_op (ARRAY_REF, LOOKUP_NORMAL, arg1, arg2, NULL_TREE,
3594 /*overload=*/NULL, complain);
3596 if (processing_template_decl && expr != error_mark_node)
3597 return build_min_non_dep (ARRAY_REF, expr, orig_arg1, orig_arg2,
3598 NULL_TREE, NULL_TREE);
3602 /* For the c-common bits. */
3604 build_binary_op (location_t location, enum tree_code code, tree op0, tree op1,
3605 int convert_p ATTRIBUTE_UNUSED)
3607 return cp_build_binary_op (location, code, op0, op1, tf_warning_or_error);
3611 /* Build a binary-operation expression without default conversions.
3612 CODE is the kind of expression to build.
3613 LOCATION is the location_t of the operator in the source code.
3614 This function differs from `build' in several ways:
3615 the data type of the result is computed and recorded in it,
3616 warnings are generated if arg data types are invalid,
3617 special handling for addition and subtraction of pointers is known,
3618 and some optimization is done (operations on narrow ints
3619 are done in the narrower type when that gives the same result).
3620 Constant folding is also done before the result is returned.
3622 Note that the operands will never have enumeral types
3623 because either they have just had the default conversions performed
3624 or they have both just been converted to some other type in which
3625 the arithmetic is to be done.
3627 C++: must do special pointer arithmetic when implementing
3628 multiple inheritance, and deal with pointer to member functions. */
3631 cp_build_binary_op (location_t location,
3632 enum tree_code code, tree orig_op0, tree orig_op1,
3633 tsubst_flags_t complain)
3636 enum tree_code code0, code1;
3638 const char *invalid_op_diag;
3640 /* Expression code to give to the expression when it is built.
3641 Normally this is CODE, which is what the caller asked for,
3642 but in some special cases we change it. */
3643 enum tree_code resultcode = code;
3645 /* Data type in which the computation is to be performed.
3646 In the simplest cases this is the common type of the arguments. */
3647 tree result_type = NULL;
3649 /* Nonzero means operands have already been type-converted
3650 in whatever way is necessary.
3651 Zero means they need to be converted to RESULT_TYPE. */
3654 /* Nonzero means create the expression with this type, rather than
3656 tree build_type = 0;
3658 /* Nonzero means after finally constructing the expression
3659 convert it to this type. */
3660 tree final_type = 0;
3664 /* Nonzero if this is an operation like MIN or MAX which can
3665 safely be computed in short if both args are promoted shorts.
3666 Also implies COMMON.
3667 -1 indicates a bitwise operation; this makes a difference
3668 in the exact conditions for when it is safe to do the operation
3669 in a narrower mode. */
3672 /* Nonzero if this is a comparison operation;
3673 if both args are promoted shorts, compare the original shorts.
3674 Also implies COMMON. */
3675 int short_compare = 0;
3677 /* Nonzero means set RESULT_TYPE to the common type of the args. */
3680 /* True if both operands have arithmetic type. */
3681 bool arithmetic_types_p;
3683 /* Apply default conversions. */
3687 if (code == TRUTH_AND_EXPR || code == TRUTH_ANDIF_EXPR
3688 || code == TRUTH_OR_EXPR || code == TRUTH_ORIF_EXPR
3689 || code == TRUTH_XOR_EXPR)
3691 if (!really_overloaded_fn (op0) && !VOID_TYPE_P (TREE_TYPE (op0)))
3692 op0 = decay_conversion (op0);
3693 if (!really_overloaded_fn (op1) && !VOID_TYPE_P (TREE_TYPE (op1)))
3694 op1 = decay_conversion (op1);
3698 if (!really_overloaded_fn (op0) && !VOID_TYPE_P (TREE_TYPE (op0)))
3699 op0 = default_conversion (op0);
3700 if (!really_overloaded_fn (op1) && !VOID_TYPE_P (TREE_TYPE (op1)))
3701 op1 = default_conversion (op1);
3704 /* Strip NON_LVALUE_EXPRs, etc., since we aren't using as an lvalue. */
3705 STRIP_TYPE_NOPS (op0);
3706 STRIP_TYPE_NOPS (op1);
3708 /* DTRT if one side is an overloaded function, but complain about it. */
3709 if (type_unknown_p (op0))
3711 tree t = instantiate_type (TREE_TYPE (op1), op0, tf_none);
3712 if (t != error_mark_node)
3714 if (complain & tf_error)
3715 permerror (input_location, "assuming cast to type %qT from overloaded function",
3720 if (type_unknown_p (op1))
3722 tree t = instantiate_type (TREE_TYPE (op0), op1, tf_none);
3723 if (t != error_mark_node)
3725 if (complain & tf_error)
3726 permerror (input_location, "assuming cast to type %qT from overloaded function",
3732 type0 = TREE_TYPE (op0);
3733 type1 = TREE_TYPE (op1);
3735 /* The expression codes of the data types of the arguments tell us
3736 whether the arguments are integers, floating, pointers, etc. */
3737 code0 = TREE_CODE (type0);
3738 code1 = TREE_CODE (type1);
3740 /* If an error was already reported for one of the arguments,
3741 avoid reporting another error. */
3742 if (code0 == ERROR_MARK || code1 == ERROR_MARK)
3743 return error_mark_node;
3745 if ((invalid_op_diag
3746 = targetm.invalid_binary_op (code, type0, type1)))
3748 error (invalid_op_diag);
3749 return error_mark_node;
3752 /* Issue warnings about peculiar, but valid, uses of NULL. */
3753 if ((orig_op0 == null_node || orig_op1 == null_node)
3754 /* It's reasonable to use pointer values as operands of &&
3755 and ||, so NULL is no exception. */
3756 && code != TRUTH_ANDIF_EXPR && code != TRUTH_ORIF_EXPR
3757 && ( /* Both are NULL (or 0) and the operation was not a
3758 comparison or a pointer subtraction. */
3759 (null_ptr_cst_p (orig_op0) && null_ptr_cst_p (orig_op1)
3760 && code != EQ_EXPR && code != NE_EXPR && code != MINUS_EXPR)
3761 /* Or if one of OP0 or OP1 is neither a pointer nor NULL. */
3762 || (!null_ptr_cst_p (orig_op0)
3763 && !TYPE_PTR_P (type0) && !TYPE_PTR_TO_MEMBER_P (type0))
3764 || (!null_ptr_cst_p (orig_op1)
3765 && !TYPE_PTR_P (type1) && !TYPE_PTR_TO_MEMBER_P (type1)))
3766 && (complain & tf_warning))
3767 /* Some sort of arithmetic operation involving NULL was
3769 warning (OPT_Wpointer_arith, "NULL used in arithmetic");
3774 /* Subtraction of two similar pointers.
3775 We must subtract them as integers, then divide by object size. */
3776 if (code0 == POINTER_TYPE && code1 == POINTER_TYPE
3777 && same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (type0),
3779 return pointer_diff (op0, op1, common_pointer_type (type0, type1));
3780 /* In all other cases except pointer - int, the usual arithmetic
3782 else if (!(code0 == POINTER_TYPE && code1 == INTEGER_TYPE))
3787 /* The pointer - int case is just like pointer + int; fall
3790 if ((code0 == POINTER_TYPE || code1 == POINTER_TYPE)
3791 && (code0 == INTEGER_TYPE || code1 == INTEGER_TYPE))
3795 ptr_operand = ((code0 == POINTER_TYPE) ? op0 : op1);
3796 int_operand = ((code0 == INTEGER_TYPE) ? op0 : op1);
3797 if (processing_template_decl)
3799 result_type = TREE_TYPE (ptr_operand);
3802 return cp_pointer_int_sum (code,
3813 case TRUNC_DIV_EXPR:
3815 case FLOOR_DIV_EXPR:
3816 case ROUND_DIV_EXPR:
3817 case EXACT_DIV_EXPR:
3818 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE
3819 || code0 == COMPLEX_TYPE || code0 == VECTOR_TYPE)
3820 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
3821 || code1 == COMPLEX_TYPE || code1 == VECTOR_TYPE))
3823 enum tree_code tcode0 = code0, tcode1 = code1;
3825 warn_for_div_by_zero (location, op1);
3827 if (tcode0 == COMPLEX_TYPE || tcode0 == VECTOR_TYPE)
3828 tcode0 = TREE_CODE (TREE_TYPE (TREE_TYPE (op0)));
3829 if (tcode1 == COMPLEX_TYPE || tcode1 == VECTOR_TYPE)
3830 tcode1 = TREE_CODE (TREE_TYPE (TREE_TYPE (op1)));
3832 if (!(tcode0 == INTEGER_TYPE && tcode1 == INTEGER_TYPE))
3833 resultcode = RDIV_EXPR;
3835 /* When dividing two signed integers, we have to promote to int.
3836 unless we divide by a constant != -1. Note that default
3837 conversion will have been performed on the operands at this
3838 point, so we have to dig out the original type to find out if
3840 shorten = ((TREE_CODE (op0) == NOP_EXPR
3841 && TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (op0, 0))))
3842 || (TREE_CODE (op1) == INTEGER_CST
3843 && ! integer_all_onesp (op1)));
3852 if ((code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
3853 || (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE
3854 && !VECTOR_FLOAT_TYPE_P (type0)
3855 && !VECTOR_FLOAT_TYPE_P (type1)))
3859 case TRUNC_MOD_EXPR:
3860 case FLOOR_MOD_EXPR:
3861 warn_for_div_by_zero (location, op1);
3863 if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE
3864 && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE
3865 && TREE_CODE (TREE_TYPE (type1)) == INTEGER_TYPE)
3867 else if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
3869 /* Although it would be tempting to shorten always here, that loses
3870 on some targets, since the modulo instruction is undefined if the
3871 quotient can't be represented in the computation mode. We shorten
3872 only if unsigned or if dividing by something we know != -1. */
3873 shorten = ((TREE_CODE (op0) == NOP_EXPR
3874 && TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (op0, 0))))
3875 || (TREE_CODE (op1) == INTEGER_CST
3876 && ! integer_all_onesp (op1)));
3881 case TRUTH_ANDIF_EXPR:
3882 case TRUTH_ORIF_EXPR:
3883 case TRUTH_AND_EXPR:
3885 result_type = boolean_type_node;
3888 /* Shift operations: result has same type as first operand;
3889 always convert second operand to int.
3890 Also set SHORT_SHIFT if shifting rightward. */
3893 if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
3895 result_type = type0;
3896 if (TREE_CODE (op1) == INTEGER_CST)
3898 if (tree_int_cst_lt (op1, integer_zero_node))
3900 if ((complain & tf_warning)
3901 && c_inhibit_evaluation_warnings == 0)
3902 warning (0, "right shift count is negative");
3906 if (compare_tree_int (op1, TYPE_PRECISION (type0)) >= 0
3907 && (complain & tf_warning)
3908 && c_inhibit_evaluation_warnings == 0)
3909 warning (0, "right shift count >= width of type");
3912 /* Convert the shift-count to an integer, regardless of
3913 size of value being shifted. */
3914 if (TYPE_MAIN_VARIANT (TREE_TYPE (op1)) != integer_type_node)
3915 op1 = cp_convert (integer_type_node, op1);
3916 /* Avoid converting op1 to result_type later. */
3922 if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
3924 result_type = type0;
3925 if (TREE_CODE (op1) == INTEGER_CST)
3927 if (tree_int_cst_lt (op1, integer_zero_node))
3929 if ((complain & tf_warning)
3930 && c_inhibit_evaluation_warnings == 0)
3931 warning (0, "left shift count is negative");
3933 else if (compare_tree_int (op1, TYPE_PRECISION (type0)) >= 0)
3935 if ((complain & tf_warning)
3936 && c_inhibit_evaluation_warnings == 0)
3937 warning (0, "left shift count >= width of type");
3940 /* Convert the shift-count to an integer, regardless of
3941 size of value being shifted. */
3942 if (TYPE_MAIN_VARIANT (TREE_TYPE (op1)) != integer_type_node)
3943 op1 = cp_convert (integer_type_node, op1);
3944 /* Avoid converting op1 to result_type later. */
3951 if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
3953 result_type = type0;
3954 if (TREE_CODE (op1) == INTEGER_CST)
3956 if (tree_int_cst_lt (op1, integer_zero_node))
3958 if (complain & tf_warning)
3959 warning (0, (code == LROTATE_EXPR)
3960 ? G_("left rotate count is negative")
3961 : G_("right rotate count is negative"));
3963 else if (compare_tree_int (op1, TYPE_PRECISION (type0)) >= 0)
3965 if (complain & tf_warning)
3966 warning (0, (code == LROTATE_EXPR)
3967 ? G_("left rotate count >= width of type")
3968 : G_("right rotate count >= width of type"));
3971 /* Convert the shift-count to an integer, regardless of
3972 size of value being shifted. */
3973 if (TYPE_MAIN_VARIANT (TREE_TYPE (op1)) != integer_type_node)
3974 op1 = cp_convert (integer_type_node, op1);
3980 if ((complain & tf_warning)
3981 && (FLOAT_TYPE_P (type0) || FLOAT_TYPE_P (type1)))
3982 warning (OPT_Wfloat_equal,
3983 "comparing floating point with == or != is unsafe");
3984 if ((complain & tf_warning)
3985 && ((TREE_CODE (orig_op0) == STRING_CST && !integer_zerop (op1))
3986 || (TREE_CODE (orig_op1) == STRING_CST && !integer_zerop (op0))))
3987 warning (OPT_Waddress, "comparison with string literal results in unspecified behaviour");
3989 build_type = boolean_type_node;
3990 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE
3991 || code0 == COMPLEX_TYPE || code0 == ENUMERAL_TYPE)
3992 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
3993 || code1 == COMPLEX_TYPE || code1 == ENUMERAL_TYPE))
3995 else if ((code0 == POINTER_TYPE && code1 == POINTER_TYPE)
3996 || (TYPE_PTRMEM_P (type0) && TYPE_PTRMEM_P (type1)))
3997 result_type = composite_pointer_type (type0, type1, op0, op1,
3998 CPO_COMPARISON, complain);
3999 else if ((code0 == POINTER_TYPE || TYPE_PTRMEM_P (type0))
4000 && null_ptr_cst_p (op1))
4002 if (TREE_CODE (op0) == ADDR_EXPR
4003 && decl_with_nonnull_addr_p (TREE_OPERAND (op0, 0)))
4005 if (complain & tf_warning)
4006 warning (OPT_Waddress, "the address of %qD will never be NULL",
4007 TREE_OPERAND (op0, 0));
4009 result_type = type0;
4011 else if ((code1 == POINTER_TYPE || TYPE_PTRMEM_P (type1))
4012 && null_ptr_cst_p (op0))
4014 if (TREE_CODE (op1) == ADDR_EXPR
4015 && decl_with_nonnull_addr_p (TREE_OPERAND (op1, 0)))
4017 if (complain & tf_warning)
4018 warning (OPT_Waddress, "the address of %qD will never be NULL",
4019 TREE_OPERAND (op1, 0));
4021 result_type = type1;
4023 else if (null_ptr_cst_p (op0) && null_ptr_cst_p (op1))
4024 /* One of the operands must be of nullptr_t type. */
4025 result_type = TREE_TYPE (nullptr_node);
4026 else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
4028 result_type = type0;
4029 if (complain & tf_error)
4030 permerror (input_location, "ISO C++ forbids comparison between pointer and integer");
4032 return error_mark_node;
4034 else if (code0 == INTEGER_TYPE && code1 == POINTER_TYPE)
4036 result_type = type1;
4037 if (complain & tf_error)