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
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)))
513 result_type = composite_pointer_type_r (pointee1, pointee2, operation,
517 if (complain & tf_error)
518 composite_pointer_error (DK_PERMERROR, t1, t2, operation);
520 result_type = void_type_node;
522 result_type = cp_build_qualified_type (result_type,
523 (cp_type_quals (pointee1)
524 | cp_type_quals (pointee2)));
525 /* If the original types were pointers to members, so is the
527 if (TYPE_PTR_TO_MEMBER_P (t1))
529 if (!same_type_p (TYPE_PTRMEM_CLASS_TYPE (t1),
530 TYPE_PTRMEM_CLASS_TYPE (t2))
531 && (complain & tf_error))
532 composite_pointer_error (DK_PERMERROR, t1, t2, operation);
533 result_type = build_ptrmem_type (TYPE_PTRMEM_CLASS_TYPE (t1),
537 result_type = build_pointer_type (result_type);
539 /* Merge the attributes. */
540 attributes = (*targetm.merge_type_attributes) (t1, t2);
541 return build_type_attribute_variant (result_type, attributes);
544 /* Return the composite pointer type (see [expr.rel]) for T1 and T2.
545 ARG1 and ARG2 are the values with those types. The OPERATION is to
546 describe the operation between the pointer types,
547 in case an error occurs.
549 This routine also implements the computation of a common type for
550 pointers-to-members as per [expr.eq]. */
553 composite_pointer_type (tree t1, tree t2, tree arg1, tree arg2,
554 composite_pointer_operation operation,
555 tsubst_flags_t complain)
562 If one operand is a null pointer constant, the composite pointer
563 type is the type of the other operand. */
564 if (null_ptr_cst_p (arg1))
566 if (null_ptr_cst_p (arg2))
573 If one of the operands has type "pointer to cv1 void*", then
574 the other has type "pointer to cv2T", and the composite pointer
575 type is "pointer to cv12 void", where cv12 is the union of cv1
578 If either type is a pointer to void, make sure it is T1. */
579 if (TREE_CODE (t2) == POINTER_TYPE && VOID_TYPE_P (TREE_TYPE (t2)))
587 /* Now, if T1 is a pointer to void, merge the qualifiers. */
588 if (TREE_CODE (t1) == POINTER_TYPE && VOID_TYPE_P (TREE_TYPE (t1)))
593 if (TYPE_PTRFN_P (t2) && (complain & tf_error))
598 pedwarn (input_location, OPT_pedantic,
599 "ISO C++ forbids comparison between "
600 "pointer of type %<void *%> and pointer-to-function");
603 pedwarn (input_location, OPT_pedantic,
604 "ISO C++ forbids conversion between "
605 "pointer of type %<void *%> and pointer-to-function");
607 case CPO_CONDITIONAL_EXPR:
608 pedwarn (input_location, OPT_pedantic,
609 "ISO C++ forbids conditional expression between "
610 "pointer of type %<void *%> and pointer-to-function");
617 = cp_build_qualified_type (void_type_node,
618 (cp_type_quals (TREE_TYPE (t1))
619 | cp_type_quals (TREE_TYPE (t2))));
620 result_type = build_pointer_type (result_type);
621 /* Merge the attributes. */
622 attributes = (*targetm.merge_type_attributes) (t1, t2);
623 return build_type_attribute_variant (result_type, attributes);
626 if (c_dialect_objc () && TREE_CODE (t1) == POINTER_TYPE
627 && TREE_CODE (t2) == POINTER_TYPE)
629 if (objc_have_common_type (t1, t2, -3, NULL_TREE))
630 return objc_common_type (t1, t2);
633 /* [expr.eq] permits the application of a pointer conversion to
634 bring the pointers to a common type. */
635 if (TREE_CODE (t1) == POINTER_TYPE && TREE_CODE (t2) == POINTER_TYPE
636 && CLASS_TYPE_P (TREE_TYPE (t1))
637 && CLASS_TYPE_P (TREE_TYPE (t2))
638 && !same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (t1),
641 class1 = TREE_TYPE (t1);
642 class2 = TREE_TYPE (t2);
644 if (DERIVED_FROM_P (class1, class2))
645 t2 = (build_pointer_type
646 (cp_build_qualified_type (class1, cp_type_quals (class2))));
647 else if (DERIVED_FROM_P (class2, class1))
648 t1 = (build_pointer_type
649 (cp_build_qualified_type (class2, cp_type_quals (class1))));
652 if (complain & tf_error)
653 composite_pointer_error (DK_ERROR, t1, t2, operation);
654 return error_mark_node;
657 /* [expr.eq] permits the application of a pointer-to-member
658 conversion to change the class type of one of the types. */
659 else if (TYPE_PTR_TO_MEMBER_P (t1)
660 && !same_type_p (TYPE_PTRMEM_CLASS_TYPE (t1),
661 TYPE_PTRMEM_CLASS_TYPE (t2)))
663 class1 = TYPE_PTRMEM_CLASS_TYPE (t1);
664 class2 = TYPE_PTRMEM_CLASS_TYPE (t2);
666 if (DERIVED_FROM_P (class1, class2))
667 t1 = build_ptrmem_type (class2, TYPE_PTRMEM_POINTED_TO_TYPE (t1));
668 else if (DERIVED_FROM_P (class2, class1))
669 t2 = build_ptrmem_type (class1, TYPE_PTRMEM_POINTED_TO_TYPE (t2));
672 if (complain & tf_error)
676 error ("comparison between distinct "
677 "pointer-to-member types %qT and %qT lacks a cast",
681 error ("conversion between distinct "
682 "pointer-to-member types %qT and %qT lacks a cast",
685 case CPO_CONDITIONAL_EXPR:
686 error ("conditional expression between distinct "
687 "pointer-to-member types %qT and %qT lacks a cast",
693 return error_mark_node;
697 return composite_pointer_type_r (t1, t2, operation, complain);
700 /* Return the merged type of two types.
701 We assume that comptypes has already been done and returned 1;
702 if that isn't so, this may crash.
704 This just combines attributes and default arguments; any other
705 differences would cause the two types to compare unalike. */
708 merge_types (tree t1, tree t2)
710 enum tree_code code1;
711 enum tree_code code2;
714 /* Save time if the two types are the same. */
717 if (original_type (t1) == original_type (t2))
720 /* If one type is nonsense, use the other. */
721 if (t1 == error_mark_node)
723 if (t2 == error_mark_node)
726 /* Merge the attributes. */
727 attributes = (*targetm.merge_type_attributes) (t1, t2);
729 if (TYPE_PTRMEMFUNC_P (t1))
730 t1 = TYPE_PTRMEMFUNC_FN_TYPE (t1);
731 if (TYPE_PTRMEMFUNC_P (t2))
732 t2 = TYPE_PTRMEMFUNC_FN_TYPE (t2);
734 code1 = TREE_CODE (t1);
735 code2 = TREE_CODE (t2);
738 gcc_assert (code1 == TYPENAME_TYPE || code2 == TYPENAME_TYPE);
739 if (code1 == TYPENAME_TYPE)
741 t1 = resolve_typename_type (t1, /*only_current_p=*/true);
742 code1 = TREE_CODE (t1);
746 t2 = resolve_typename_type (t2, /*only_current_p=*/true);
747 code2 = TREE_CODE (t2);
755 /* For two pointers, do this recursively on the target type. */
757 tree target = merge_types (TREE_TYPE (t1), TREE_TYPE (t2));
758 int quals = cp_type_quals (t1);
760 if (code1 == POINTER_TYPE)
761 t1 = build_pointer_type (target);
763 t1 = cp_build_reference_type (target, TYPE_REF_IS_RVALUE (t1));
764 t1 = build_type_attribute_variant (t1, attributes);
765 t1 = cp_build_qualified_type (t1, quals);
767 if (TREE_CODE (target) == METHOD_TYPE)
768 t1 = build_ptrmemfunc_type (t1);
777 quals = cp_type_quals (t1);
778 pointee = merge_types (TYPE_PTRMEM_POINTED_TO_TYPE (t1),
779 TYPE_PTRMEM_POINTED_TO_TYPE (t2));
780 t1 = build_ptrmem_type (TYPE_PTRMEM_CLASS_TYPE (t1),
782 t1 = cp_build_qualified_type (t1, quals);
788 tree elt = merge_types (TREE_TYPE (t1), TREE_TYPE (t2));
789 /* Save space: see if the result is identical to one of the args. */
790 if (elt == TREE_TYPE (t1) && TYPE_DOMAIN (t1))
791 return build_type_attribute_variant (t1, attributes);
792 if (elt == TREE_TYPE (t2) && TYPE_DOMAIN (t2))
793 return build_type_attribute_variant (t2, attributes);
794 /* Merge the element types, and have a size if either arg has one. */
795 t1 = build_cplus_array_type
796 (elt, TYPE_DOMAIN (TYPE_DOMAIN (t1) ? t1 : t2));
801 /* Function types: prefer the one that specified arg types.
802 If both do, merge the arg types. Also merge the return types. */
804 tree valtype = merge_types (TREE_TYPE (t1), TREE_TYPE (t2));
805 tree p1 = TYPE_ARG_TYPES (t1);
806 tree p2 = TYPE_ARG_TYPES (t2);
810 /* Save space: see if the result is identical to one of the args. */
811 if (valtype == TREE_TYPE (t1) && ! p2)
812 return cp_build_type_attribute_variant (t1, attributes);
813 if (valtype == TREE_TYPE (t2) && ! p1)
814 return cp_build_type_attribute_variant (t2, attributes);
816 /* Simple way if one arg fails to specify argument types. */
817 if (p1 == NULL_TREE || TREE_VALUE (p1) == void_type_node)
819 else if (p2 == NULL_TREE || TREE_VALUE (p2) == void_type_node)
822 parms = commonparms (p1, p2);
824 rval = build_function_type (valtype, parms);
825 gcc_assert (type_memfn_quals (t1) == type_memfn_quals (t2));
826 rval = apply_memfn_quals (rval, type_memfn_quals (t1));
827 raises = merge_exception_specifiers (TYPE_RAISES_EXCEPTIONS (t1),
828 TYPE_RAISES_EXCEPTIONS (t2));
829 t1 = build_exception_variant (rval, raises);
835 /* Get this value the long way, since TYPE_METHOD_BASETYPE
836 is just the main variant of this. */
837 tree basetype = TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (t2)));
838 tree raises = merge_exception_specifiers (TYPE_RAISES_EXCEPTIONS (t1),
839 TYPE_RAISES_EXCEPTIONS (t2));
842 /* If this was a member function type, get back to the
843 original type of type member function (i.e., without
844 the class instance variable up front. */
845 t1 = build_function_type (TREE_TYPE (t1),
846 TREE_CHAIN (TYPE_ARG_TYPES (t1)));
847 t2 = build_function_type (TREE_TYPE (t2),
848 TREE_CHAIN (TYPE_ARG_TYPES (t2)));
849 t3 = merge_types (t1, t2);
850 t3 = build_method_type_directly (basetype, TREE_TYPE (t3),
851 TYPE_ARG_TYPES (t3));
852 t1 = build_exception_variant (t3, raises);
857 /* There is no need to merge attributes into a TYPENAME_TYPE.
858 When the type is instantiated it will have whatever
859 attributes result from the instantiation. */
865 if (attribute_list_equal (TYPE_ATTRIBUTES (t1), attributes))
867 else if (attribute_list_equal (TYPE_ATTRIBUTES (t2), attributes))
870 return cp_build_type_attribute_variant (t1, attributes);
873 /* Return the ARRAY_TYPE type without its domain. */
876 strip_array_domain (tree type)
879 gcc_assert (TREE_CODE (type) == ARRAY_TYPE);
880 if (TYPE_DOMAIN (type) == NULL_TREE)
882 t2 = build_cplus_array_type (TREE_TYPE (type), NULL_TREE);
883 return cp_build_type_attribute_variant (t2, TYPE_ATTRIBUTES (type));
886 /* Wrapper around cp_common_type that is used by c-common.c and other
887 front end optimizations that remove promotions.
889 Return the common type for two arithmetic types T1 and T2 under the
890 usual arithmetic conversions. The default conversions have already
891 been applied, and enumerated types converted to their compatible
895 common_type (tree t1, tree t2)
897 /* If one type is nonsense, use the other */
898 if (t1 == error_mark_node)
900 if (t2 == error_mark_node)
903 return cp_common_type (t1, t2);
906 /* Return the common type of two pointer types T1 and T2. This is the
907 type for the result of most arithmetic operations if the operands
908 have the given two types.
910 We assume that comp_target_types has already been done and returned
911 nonzero; if that isn't so, this may crash. */
914 common_pointer_type (tree t1, tree t2)
916 gcc_assert ((TYPE_PTR_P (t1) && TYPE_PTR_P (t2))
917 || (TYPE_PTRMEM_P (t1) && TYPE_PTRMEM_P (t2))
918 || (TYPE_PTRMEMFUNC_P (t1) && TYPE_PTRMEMFUNC_P (t2)));
920 return composite_pointer_type (t1, t2, error_mark_node, error_mark_node,
921 CPO_CONVERSION, tf_warning_or_error);
924 /* Compare two exception specifier types for exactness or subsetness, if
925 allowed. Returns false for mismatch, true for match (same, or
928 [except.spec] "If a class X ... objects of class X or any class publicly
929 and unambiguously derived from X. Similarly, if a pointer type Y * ...
930 exceptions of type Y * or that are pointers to any type publicly and
931 unambiguously derived from Y. Otherwise a function only allows exceptions
932 that have the same type ..."
933 This does not mention cv qualifiers and is different to what throw
934 [except.throw] and catch [except.catch] will do. They will ignore the
935 top level cv qualifiers, and allow qualifiers in the pointer to class
938 We implement the letter of the standard. */
941 comp_except_types (tree a, tree b, bool exact)
943 if (same_type_p (a, b))
947 if (cp_type_quals (a) || cp_type_quals (b))
950 if (TREE_CODE (a) == POINTER_TYPE
951 && TREE_CODE (b) == POINTER_TYPE)
955 if (cp_type_quals (a) || cp_type_quals (b))
959 if (TREE_CODE (a) != RECORD_TYPE
960 || TREE_CODE (b) != RECORD_TYPE)
963 if (PUBLICLY_UNIQUELY_DERIVED_P (a, b))
969 /* Return true if TYPE1 and TYPE2 are equivalent exception specifiers.
970 If EXACT is ce_derived, T2 can be stricter than T1 (according to 15.4/5).
971 If EXACT is ce_normal, the compatibility rules in 15.4/3 apply.
972 If EXACT is ce_exact, the specs must be exactly the same. Exception lists
973 are unordered, but we've already filtered out duplicates. Most lists will
974 be in order, we should try to make use of that. */
977 comp_except_specs (const_tree t1, const_tree t2, int exact)
986 /* First handle noexcept. */
987 if (exact < ce_exact)
989 /* noexcept(false) is compatible with any throwing dynamic-exc-spec
990 and stricter than any spec. */
991 if (t1 == noexcept_false_spec)
992 return !nothrow_spec_p (t2) || exact == ce_derived;
993 /* Even a derived noexcept(false) is compatible with a throwing
995 if (t2 == noexcept_false_spec)
996 return !nothrow_spec_p (t1);
998 /* Otherwise, if we aren't looking for an exact match, noexcept is
999 equivalent to throw(). */
1000 if (t1 == noexcept_true_spec)
1001 t1 = empty_except_spec;
1002 if (t2 == noexcept_true_spec)
1003 t2 = empty_except_spec;
1006 /* If any noexcept is left, it is only comparable to itself;
1007 either we're looking for an exact match or we're redeclaring a
1008 template with dependent noexcept. */
1009 if ((t1 && TREE_PURPOSE (t1))
1010 || (t2 && TREE_PURPOSE (t2)))
1012 && cp_tree_equal (TREE_PURPOSE (t1), TREE_PURPOSE (t2)));
1014 if (t1 == NULL_TREE) /* T1 is ... */
1015 return t2 == NULL_TREE || exact == ce_derived;
1016 if (!TREE_VALUE (t1)) /* t1 is EMPTY */
1017 return t2 != NULL_TREE && !TREE_VALUE (t2);
1018 if (t2 == NULL_TREE) /* T2 is ... */
1020 if (TREE_VALUE (t1) && !TREE_VALUE (t2)) /* T2 is EMPTY, T1 is not */
1021 return exact == ce_derived;
1023 /* Neither set is ... or EMPTY, make sure each part of T2 is in T1.
1024 Count how many we find, to determine exactness. For exact matching and
1025 ordered T1, T2, this is an O(n) operation, otherwise its worst case is
1027 for (base = t1; t2 != NULL_TREE; t2 = TREE_CHAIN (t2))
1029 for (probe = base; probe != NULL_TREE; probe = TREE_CHAIN (probe))
1031 tree a = TREE_VALUE (probe);
1032 tree b = TREE_VALUE (t2);
1034 if (comp_except_types (a, b, exact))
1036 if (probe == base && exact > ce_derived)
1037 base = TREE_CHAIN (probe);
1042 if (probe == NULL_TREE)
1045 return exact == ce_derived || base == NULL_TREE || length == list_length (t1);
1048 /* Compare the array types T1 and T2. ALLOW_REDECLARATION is true if
1049 [] can match [size]. */
1052 comp_array_types (const_tree t1, const_tree t2, bool allow_redeclaration)
1061 /* The type of the array elements must be the same. */
1062 if (!same_type_p (TREE_TYPE (t1), TREE_TYPE (t2)))
1065 d1 = TYPE_DOMAIN (t1);
1066 d2 = TYPE_DOMAIN (t2);
1071 /* If one of the arrays is dimensionless, and the other has a
1072 dimension, they are of different types. However, it is valid to
1080 declarations for an array object can specify
1081 array types that differ by the presence or absence of a major
1082 array bound (_dcl.array_). */
1084 return allow_redeclaration;
1086 /* Check that the dimensions are the same. */
1088 if (!cp_tree_equal (TYPE_MIN_VALUE (d1), TYPE_MIN_VALUE (d2)))
1090 max1 = TYPE_MAX_VALUE (d1);
1091 max2 = TYPE_MAX_VALUE (d2);
1092 if (processing_template_decl && !abi_version_at_least (2)
1093 && !value_dependent_expression_p (max1)
1094 && !value_dependent_expression_p (max2))
1096 /* With abi-1 we do not fold non-dependent array bounds, (and
1097 consequently mangle them incorrectly). We must therefore
1098 fold them here, to verify the domains have the same
1104 if (!cp_tree_equal (max1, max2))
1110 /* Compare the relative position of T1 and T2 into their respective
1111 template parameter list.
1112 T1 and T2 must be template parameter types.
1113 Return TRUE if T1 and T2 have the same position, FALSE otherwise. */
1116 comp_template_parms_position (tree t1, tree t2)
1118 tree index1, index2;
1119 gcc_assert (t1 && t2
1120 && TREE_CODE (t1) == TREE_CODE (t2)
1121 && (TREE_CODE (t1) == BOUND_TEMPLATE_TEMPLATE_PARM
1122 || TREE_CODE (t1) == TEMPLATE_TEMPLATE_PARM
1123 || TREE_CODE (t1) == TEMPLATE_TYPE_PARM));
1125 index1 = TEMPLATE_TYPE_PARM_INDEX (TYPE_MAIN_VARIANT (t1));
1126 index2 = TEMPLATE_TYPE_PARM_INDEX (TYPE_MAIN_VARIANT (t2));
1128 /* If T1 and T2 belong to template parm lists of different size,
1129 let's assume they are different. */
1130 if (TEMPLATE_PARM_NUM_SIBLINGS (index1)
1131 != TEMPLATE_PARM_NUM_SIBLINGS (index2))
1134 /* Then compare their relative position. */
1135 if (TEMPLATE_PARM_IDX (index1) != TEMPLATE_PARM_IDX (index2)
1136 || TEMPLATE_PARM_LEVEL (index1) != TEMPLATE_PARM_LEVEL (index2)
1137 || (TEMPLATE_PARM_PARAMETER_PACK (index1)
1138 != TEMPLATE_PARM_PARAMETER_PACK (index2)))
1144 /* Subroutine in comptypes. */
1147 structural_comptypes (tree t1, tree t2, int strict)
1152 /* Suppress errors caused by previously reported errors. */
1153 if (t1 == error_mark_node || t2 == error_mark_node)
1156 gcc_assert (TYPE_P (t1) && TYPE_P (t2));
1158 /* TYPENAME_TYPEs should be resolved if the qualifying scope is the
1159 current instantiation. */
1160 if (TREE_CODE (t1) == TYPENAME_TYPE)
1161 t1 = resolve_typename_type (t1, /*only_current_p=*/true);
1163 if (TREE_CODE (t2) == TYPENAME_TYPE)
1164 t2 = resolve_typename_type (t2, /*only_current_p=*/true);
1166 if (TYPE_PTRMEMFUNC_P (t1))
1167 t1 = TYPE_PTRMEMFUNC_FN_TYPE (t1);
1168 if (TYPE_PTRMEMFUNC_P (t2))
1169 t2 = TYPE_PTRMEMFUNC_FN_TYPE (t2);
1171 /* Different classes of types can't be compatible. */
1172 if (TREE_CODE (t1) != TREE_CODE (t2))
1175 /* Qualifiers must match. For array types, we will check when we
1176 recur on the array element types. */
1177 if (TREE_CODE (t1) != ARRAY_TYPE
1178 && cp_type_quals (t1) != cp_type_quals (t2))
1180 if (TREE_CODE (t1) == FUNCTION_TYPE
1181 && type_memfn_quals (t1) != type_memfn_quals (t2))
1183 if (TYPE_FOR_JAVA (t1) != TYPE_FOR_JAVA (t2))
1186 /* Allow for two different type nodes which have essentially the same
1187 definition. Note that we already checked for equality of the type
1188 qualifiers (just above). */
1190 if (TREE_CODE (t1) != ARRAY_TYPE
1191 && TYPE_MAIN_VARIANT (t1) == TYPE_MAIN_VARIANT (t2))
1195 /* Compare the types. Break out if they could be the same. */
1196 switch (TREE_CODE (t1))
1200 /* All void and bool types are the same. */
1204 case FIXED_POINT_TYPE:
1206 /* With these nodes, we can't determine type equivalence by
1207 looking at what is stored in the nodes themselves, because
1208 two nodes might have different TYPE_MAIN_VARIANTs but still
1209 represent the same type. For example, wchar_t and int could
1210 have the same properties (TYPE_PRECISION, TYPE_MIN_VALUE,
1211 TYPE_MAX_VALUE, etc.), but have different TYPE_MAIN_VARIANTs
1212 and are distinct types. On the other hand, int and the
1215 typedef int INT __attribute((may_alias));
1217 have identical properties, different TYPE_MAIN_VARIANTs, but
1218 represent the same type. The canonical type system keeps
1219 track of equivalence in this case, so we fall back on it. */
1220 return TYPE_CANONICAL (t1) == TYPE_CANONICAL (t2);
1222 case TEMPLATE_TEMPLATE_PARM:
1223 case BOUND_TEMPLATE_TEMPLATE_PARM:
1224 if (!comp_template_parms_position (t1, t2))
1226 if (!comp_template_parms
1227 (DECL_TEMPLATE_PARMS (TEMPLATE_TEMPLATE_PARM_TEMPLATE_DECL (t1)),
1228 DECL_TEMPLATE_PARMS (TEMPLATE_TEMPLATE_PARM_TEMPLATE_DECL (t2))))
1230 if (TREE_CODE (t1) == TEMPLATE_TEMPLATE_PARM)
1232 /* Don't check inheritance. */
1233 strict = COMPARE_STRICT;
1238 if (TYPE_TEMPLATE_INFO (t1) && TYPE_TEMPLATE_INFO (t2)
1239 && (TYPE_TI_TEMPLATE (t1) == TYPE_TI_TEMPLATE (t2)
1240 || TREE_CODE (t1) == BOUND_TEMPLATE_TEMPLATE_PARM)
1241 && comp_template_args (TYPE_TI_ARGS (t1), TYPE_TI_ARGS (t2)))
1244 if ((strict & COMPARE_BASE) && DERIVED_FROM_P (t1, t2))
1246 else if ((strict & COMPARE_DERIVED) && DERIVED_FROM_P (t2, t1))
1252 if (!comptypes (TYPE_OFFSET_BASETYPE (t1), TYPE_OFFSET_BASETYPE (t2),
1253 strict & ~COMPARE_REDECLARATION))
1255 if (!same_type_p (TREE_TYPE (t1), TREE_TYPE (t2)))
1259 case REFERENCE_TYPE:
1260 if (TYPE_REF_IS_RVALUE (t1) != TYPE_REF_IS_RVALUE (t2))
1262 /* fall through to checks for pointer types */
1265 if (TYPE_MODE (t1) != TYPE_MODE (t2)
1266 || TYPE_REF_CAN_ALIAS_ALL (t1) != TYPE_REF_CAN_ALIAS_ALL (t2)
1267 || !same_type_p (TREE_TYPE (t1), TREE_TYPE (t2)))
1273 if (!same_type_p (TREE_TYPE (t1), TREE_TYPE (t2)))
1275 if (!compparms (TYPE_ARG_TYPES (t1), TYPE_ARG_TYPES (t2)))
1280 /* Target types must match incl. qualifiers. */
1281 if (!comp_array_types (t1, t2, !!(strict & COMPARE_REDECLARATION)))
1285 case TEMPLATE_TYPE_PARM:
1286 /* If T1 and T2 don't have the same relative position in their
1287 template parameters set, they can't be equal. */
1288 if (!comp_template_parms_position (t1, t2))
1293 if (!cp_tree_equal (TYPENAME_TYPE_FULLNAME (t1),
1294 TYPENAME_TYPE_FULLNAME (t2)))
1296 if (!same_type_p (TYPE_CONTEXT (t1), TYPE_CONTEXT (t2)))
1300 case UNBOUND_CLASS_TEMPLATE:
1301 if (!cp_tree_equal (TYPE_IDENTIFIER (t1), TYPE_IDENTIFIER (t2)))
1303 if (!same_type_p (TYPE_CONTEXT (t1), TYPE_CONTEXT (t2)))
1308 if (!same_type_p (TREE_TYPE (t1), TREE_TYPE (t2)))
1313 if (TYPE_VECTOR_SUBPARTS (t1) != TYPE_VECTOR_SUBPARTS (t2)
1314 || !same_type_p (TREE_TYPE (t1), TREE_TYPE (t2)))
1318 case TYPE_PACK_EXPANSION:
1319 return same_type_p (PACK_EXPANSION_PATTERN (t1),
1320 PACK_EXPANSION_PATTERN (t2));
1323 if (DECLTYPE_TYPE_ID_EXPR_OR_MEMBER_ACCESS_P (t1)
1324 != DECLTYPE_TYPE_ID_EXPR_OR_MEMBER_ACCESS_P (t2)
1325 || (DECLTYPE_FOR_LAMBDA_CAPTURE (t1)
1326 != DECLTYPE_FOR_LAMBDA_CAPTURE (t2))
1327 || (DECLTYPE_FOR_LAMBDA_RETURN (t1)
1328 != DECLTYPE_FOR_LAMBDA_RETURN (t2))
1329 || !cp_tree_equal (DECLTYPE_TYPE_EXPR (t1),
1330 DECLTYPE_TYPE_EXPR (t2)))
1338 /* If we get here, we know that from a target independent POV the
1339 types are the same. Make sure the target attributes are also
1341 return targetm.comp_type_attributes (t1, t2);
1344 /* Return true if T1 and T2 are related as allowed by STRICT. STRICT
1345 is a bitwise-or of the COMPARE_* flags. */
1348 comptypes (tree t1, tree t2, int strict)
1350 if (strict == COMPARE_STRICT)
1355 if (t1 == error_mark_node || t2 == error_mark_node)
1358 if (TYPE_STRUCTURAL_EQUALITY_P (t1) || TYPE_STRUCTURAL_EQUALITY_P (t2))
1359 /* At least one of the types requires structural equality, so
1360 perform a deep check. */
1361 return structural_comptypes (t1, t2, strict);
1363 #ifdef ENABLE_CHECKING
1364 if (USE_CANONICAL_TYPES)
1366 bool result = structural_comptypes (t1, t2, strict);
1368 if (result && TYPE_CANONICAL (t1) != TYPE_CANONICAL (t2))
1369 /* The two types are structurally equivalent, but their
1370 canonical types were different. This is a failure of the
1371 canonical type propagation code.*/
1373 ("canonical types differ for identical types %T and %T",
1375 else if (!result && TYPE_CANONICAL (t1) == TYPE_CANONICAL (t2))
1376 /* Two types are structurally different, but the canonical
1377 types are the same. This means we were over-eager in
1378 assigning canonical types. */
1380 ("same canonical type node for different types %T and %T",
1386 if (USE_CANONICAL_TYPES)
1387 return TYPE_CANONICAL (t1) == TYPE_CANONICAL (t2);
1390 return structural_comptypes (t1, t2, strict);
1392 else if (strict == COMPARE_STRUCTURAL)
1393 return structural_comptypes (t1, t2, COMPARE_STRICT);
1395 return structural_comptypes (t1, t2, strict);
1398 /* Returns nonzero iff TYPE1 and TYPE2 are the same type, ignoring
1399 top-level qualifiers. */
1402 same_type_ignoring_top_level_qualifiers_p (tree type1, tree type2)
1404 if (type1 == error_mark_node || type2 == error_mark_node)
1407 return same_type_p (TYPE_MAIN_VARIANT (type1), TYPE_MAIN_VARIANT (type2));
1410 /* Returns 1 if TYPE1 is at least as qualified as TYPE2. */
1413 at_least_as_qualified_p (const_tree type1, const_tree type2)
1415 int q1 = cp_type_quals (type1);
1416 int q2 = cp_type_quals (type2);
1418 /* All qualifiers for TYPE2 must also appear in TYPE1. */
1419 return (q1 & q2) == q2;
1422 /* Returns 1 if TYPE1 is more cv-qualified than TYPE2, -1 if TYPE2 is
1423 more cv-qualified that TYPE1, and 0 otherwise. */
1426 comp_cv_qualification (const_tree type1, const_tree type2)
1428 int q1 = cp_type_quals (type1);
1429 int q2 = cp_type_quals (type2);
1434 if ((q1 & q2) == q2)
1436 else if ((q1 & q2) == q1)
1442 /* Returns 1 if the cv-qualification signature of TYPE1 is a proper
1443 subset of the cv-qualification signature of TYPE2, and the types
1444 are similar. Returns -1 if the other way 'round, and 0 otherwise. */
1447 comp_cv_qual_signature (tree type1, tree type2)
1449 if (comp_ptr_ttypes_real (type2, type1, -1))
1451 else if (comp_ptr_ttypes_real (type1, type2, -1))
1457 /* Subroutines of `comptypes'. */
1459 /* Return true if two parameter type lists PARMS1 and PARMS2 are
1460 equivalent in the sense that functions with those parameter types
1461 can have equivalent types. The two lists must be equivalent,
1462 element by element. */
1465 compparms (const_tree parms1, const_tree parms2)
1469 /* An unspecified parmlist matches any specified parmlist
1470 whose argument types don't need default promotions. */
1472 for (t1 = parms1, t2 = parms2;
1474 t1 = TREE_CHAIN (t1), t2 = TREE_CHAIN (t2))
1476 /* If one parmlist is shorter than the other,
1477 they fail to match. */
1480 if (!same_type_p (TREE_VALUE (t1), TREE_VALUE (t2)))
1487 /* Process a sizeof or alignof expression where the operand is a
1491 cxx_sizeof_or_alignof_type (tree type, enum tree_code op, bool complain)
1496 gcc_assert (op == SIZEOF_EXPR || op == ALIGNOF_EXPR);
1497 if (type == error_mark_node)
1498 return error_mark_node;
1500 type = non_reference (type);
1501 if (TREE_CODE (type) == METHOD_TYPE)
1504 pedwarn (input_location, pedantic ? OPT_pedantic : OPT_Wpointer_arith,
1505 "invalid application of %qs to a member function",
1506 operator_name_info[(int) op].name);
1507 value = size_one_node;
1510 dependent_p = dependent_type_p (type);
1512 complete_type (type);
1514 /* VLA types will have a non-constant size. In the body of an
1515 uninstantiated template, we don't need to try to compute the
1516 value, because the sizeof expression is not an integral
1517 constant expression in that case. And, if we do try to
1518 compute the value, we'll likely end up with SAVE_EXPRs, which
1519 the template substitution machinery does not expect to see. */
1520 || (processing_template_decl
1521 && COMPLETE_TYPE_P (type)
1522 && TREE_CODE (TYPE_SIZE (type)) != INTEGER_CST))
1524 value = build_min (op, size_type_node, type);
1525 TREE_READONLY (value) = 1;
1529 return c_sizeof_or_alignof_type (input_location, complete_type (type),
1534 /* Return the size of the type, without producing any warnings for
1535 types whose size cannot be taken. This routine should be used only
1536 in some other routine that has already produced a diagnostic about
1537 using the size of such a type. */
1539 cxx_sizeof_nowarn (tree type)
1541 if (TREE_CODE (type) == FUNCTION_TYPE
1542 || TREE_CODE (type) == VOID_TYPE
1543 || TREE_CODE (type) == ERROR_MARK)
1544 return size_one_node;
1545 else if (!COMPLETE_TYPE_P (type))
1546 return size_zero_node;
1548 return cxx_sizeof_or_alignof_type (type, SIZEOF_EXPR, false);
1551 /* Process a sizeof expression where the operand is an expression. */
1554 cxx_sizeof_expr (tree e, tsubst_flags_t complain)
1556 if (e == error_mark_node)
1557 return error_mark_node;
1559 if (processing_template_decl)
1561 e = build_min (SIZEOF_EXPR, size_type_node, e);
1562 TREE_SIDE_EFFECTS (e) = 0;
1563 TREE_READONLY (e) = 1;
1568 /* To get the size of a static data member declared as an array of
1569 unknown bound, we need to instantiate it. */
1570 if (TREE_CODE (e) == VAR_DECL
1571 && VAR_HAD_UNKNOWN_BOUND (e)
1572 && DECL_TEMPLATE_INSTANTIATION (e))
1573 instantiate_decl (e, /*defer_ok*/true, /*expl_inst_mem*/false);
1575 e = mark_type_use (e);
1577 if (TREE_CODE (e) == COMPONENT_REF
1578 && TREE_CODE (TREE_OPERAND (e, 1)) == FIELD_DECL
1579 && DECL_C_BIT_FIELD (TREE_OPERAND (e, 1)))
1581 if (complain & tf_error)
1582 error ("invalid application of %<sizeof%> to a bit-field");
1584 return error_mark_node;
1587 else if (is_overloaded_fn (e))
1589 if (complain & tf_error)
1590 permerror (input_location, "ISO C++ forbids applying %<sizeof%> to an expression of "
1593 return error_mark_node;
1596 else if (type_unknown_p (e))
1598 if (complain & tf_error)
1599 cxx_incomplete_type_error (e, TREE_TYPE (e));
1601 return error_mark_node;
1607 return cxx_sizeof_or_alignof_type (e, SIZEOF_EXPR, complain & tf_error);
1610 /* Implement the __alignof keyword: Return the minimum required
1611 alignment of E, measured in bytes. For VAR_DECL's and
1612 FIELD_DECL's return DECL_ALIGN (which can be set from an
1613 "aligned" __attribute__ specification). */
1616 cxx_alignof_expr (tree e, tsubst_flags_t complain)
1620 if (e == error_mark_node)
1621 return error_mark_node;
1623 if (processing_template_decl)
1625 e = build_min (ALIGNOF_EXPR, size_type_node, e);
1626 TREE_SIDE_EFFECTS (e) = 0;
1627 TREE_READONLY (e) = 1;
1632 e = mark_type_use (e);
1634 if (TREE_CODE (e) == VAR_DECL)
1635 t = size_int (DECL_ALIGN_UNIT (e));
1636 else if (TREE_CODE (e) == COMPONENT_REF
1637 && TREE_CODE (TREE_OPERAND (e, 1)) == FIELD_DECL
1638 && DECL_C_BIT_FIELD (TREE_OPERAND (e, 1)))
1640 if (complain & tf_error)
1641 error ("invalid application of %<__alignof%> to a bit-field");
1643 return error_mark_node;
1646 else if (TREE_CODE (e) == COMPONENT_REF
1647 && TREE_CODE (TREE_OPERAND (e, 1)) == FIELD_DECL)
1648 t = size_int (DECL_ALIGN_UNIT (TREE_OPERAND (e, 1)));
1649 else if (is_overloaded_fn (e))
1651 if (complain & tf_error)
1652 permerror (input_location, "ISO C++ forbids applying %<__alignof%> to an expression of "
1655 return error_mark_node;
1656 if (TREE_CODE (e) == FUNCTION_DECL)
1657 t = size_int (DECL_ALIGN_UNIT (e));
1661 else if (type_unknown_p (e))
1663 if (complain & tf_error)
1664 cxx_incomplete_type_error (e, TREE_TYPE (e));
1666 return error_mark_node;
1670 return cxx_sizeof_or_alignof_type (TREE_TYPE (e), ALIGNOF_EXPR,
1671 complain & tf_error);
1673 return fold_convert (size_type_node, t);
1676 /* Process a sizeof or alignof expression E with code OP where the operand
1677 is an expression. */
1680 cxx_sizeof_or_alignof_expr (tree e, enum tree_code op, bool complain)
1682 if (op == SIZEOF_EXPR)
1683 return cxx_sizeof_expr (e, complain? tf_warning_or_error : tf_none);
1685 return cxx_alignof_expr (e, complain? tf_warning_or_error : tf_none);
1688 /* EXPR is being used in a context that is not a function call.
1693 The expression can be used only as the left-hand operand of a
1694 member function call.
1696 [expr.mptr.operator]
1698 If the result of .* or ->* is a function, then that result can be
1699 used only as the operand for the function call operator ().
1701 by issuing an error message if appropriate. Returns true iff EXPR
1702 violates these rules. */
1705 invalid_nonstatic_memfn_p (const_tree expr, tsubst_flags_t complain)
1707 if (expr && DECL_NONSTATIC_MEMBER_FUNCTION_P (expr))
1709 if (complain & tf_error)
1710 error ("invalid use of non-static member function");
1716 /* If EXP is a reference to a bitfield, and the type of EXP does not
1717 match the declared type of the bitfield, return the declared type
1718 of the bitfield. Otherwise, return NULL_TREE. */
1721 is_bitfield_expr_with_lowered_type (const_tree exp)
1723 switch (TREE_CODE (exp))
1726 if (!is_bitfield_expr_with_lowered_type (TREE_OPERAND (exp, 1)
1727 ? TREE_OPERAND (exp, 1)
1728 : TREE_OPERAND (exp, 0)))
1730 return is_bitfield_expr_with_lowered_type (TREE_OPERAND (exp, 2));
1733 return is_bitfield_expr_with_lowered_type (TREE_OPERAND (exp, 1));
1737 return is_bitfield_expr_with_lowered_type (TREE_OPERAND (exp, 0));
1743 field = TREE_OPERAND (exp, 1);
1744 if (TREE_CODE (field) != FIELD_DECL || !DECL_BIT_FIELD_TYPE (field))
1746 if (same_type_ignoring_top_level_qualifiers_p
1747 (TREE_TYPE (exp), DECL_BIT_FIELD_TYPE (field)))
1749 return DECL_BIT_FIELD_TYPE (field);
1753 if (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_OPERAND (exp, 0)))
1754 == TYPE_MAIN_VARIANT (TREE_TYPE (exp)))
1755 return is_bitfield_expr_with_lowered_type (TREE_OPERAND (exp, 0));
1763 /* Like is_bitfield_with_lowered_type, except that if EXP is not a
1764 bitfield with a lowered type, the type of EXP is returned, rather
1768 unlowered_expr_type (const_tree exp)
1772 type = is_bitfield_expr_with_lowered_type (exp);
1774 type = TREE_TYPE (exp);
1779 /* Perform the conversions in [expr] that apply when an lvalue appears
1780 in an rvalue context: the lvalue-to-rvalue, array-to-pointer, and
1781 function-to-pointer conversions. In addition, manifest constants
1782 are replaced by their values, and bitfield references are converted
1783 to their declared types. Note that this function does not perform the
1784 lvalue-to-rvalue conversion for class types. If you need that conversion
1785 to for class types, then you probably need to use force_rvalue.
1787 Although the returned value is being used as an rvalue, this
1788 function does not wrap the returned expression in a
1789 NON_LVALUE_EXPR; the caller is expected to be mindful of the fact
1790 that the return value is no longer an lvalue. */
1793 decay_conversion (tree exp)
1796 enum tree_code code;
1798 type = TREE_TYPE (exp);
1799 if (type == error_mark_node)
1800 return error_mark_node;
1802 exp = mark_rvalue_use (exp);
1804 exp = resolve_nondeduced_context (exp);
1805 if (type_unknown_p (exp))
1807 cxx_incomplete_type_error (exp, TREE_TYPE (exp));
1808 return error_mark_node;
1811 /* FIXME remove? at least need to remember that this isn't really a
1812 constant expression if EXP isn't decl_constant_var_p, like with
1813 C_MAYBE_CONST_EXPR. */
1814 exp = decl_constant_value (exp);
1815 if (error_operand_p (exp))
1816 return error_mark_node;
1818 if (NULLPTR_TYPE_P (type))
1819 return nullptr_node;
1821 /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
1822 Leave such NOP_EXPRs, since RHS is being used in non-lvalue context. */
1823 code = TREE_CODE (type);
1824 if (code == VOID_TYPE)
1826 error ("void value not ignored as it ought to be");
1827 return error_mark_node;
1829 if (invalid_nonstatic_memfn_p (exp, tf_warning_or_error))
1830 return error_mark_node;
1831 if (code == FUNCTION_TYPE || is_overloaded_fn (exp))
1832 return cp_build_addr_expr (exp, tf_warning_or_error);
1833 if (code == ARRAY_TYPE)
1838 if (TREE_CODE (exp) == INDIRECT_REF)
1839 return build_nop (build_pointer_type (TREE_TYPE (type)),
1840 TREE_OPERAND (exp, 0));
1842 if (TREE_CODE (exp) == COMPOUND_EXPR)
1844 tree op1 = decay_conversion (TREE_OPERAND (exp, 1));
1845 return build2 (COMPOUND_EXPR, TREE_TYPE (op1),
1846 TREE_OPERAND (exp, 0), op1);
1850 && ! (TREE_CODE (exp) == CONSTRUCTOR && TREE_STATIC (exp)))
1852 error ("invalid use of non-lvalue array");
1853 return error_mark_node;
1856 ptrtype = build_pointer_type (TREE_TYPE (type));
1858 if (TREE_CODE (exp) == VAR_DECL)
1860 if (!cxx_mark_addressable (exp))
1861 return error_mark_node;
1862 adr = build_nop (ptrtype, build_address (exp));
1865 /* This way is better for a COMPONENT_REF since it can
1866 simplify the offset for a component. */
1867 adr = cp_build_addr_expr (exp, tf_warning_or_error);
1868 return cp_convert (ptrtype, adr);
1871 /* If a bitfield is used in a context where integral promotion
1872 applies, then the caller is expected to have used
1873 default_conversion. That function promotes bitfields correctly
1874 before calling this function. At this point, if we have a
1875 bitfield referenced, we may assume that is not subject to
1876 promotion, and that, therefore, the type of the resulting rvalue
1877 is the declared type of the bitfield. */
1878 exp = convert_bitfield_to_declared_type (exp);
1880 /* We do not call rvalue() here because we do not want to wrap EXP
1881 in a NON_LVALUE_EXPR. */
1885 Non-class rvalues always have cv-unqualified types. */
1886 type = TREE_TYPE (exp);
1887 if (!CLASS_TYPE_P (type) && cv_qualified_p (type))
1888 exp = build_nop (cv_unqualified (type), exp);
1893 /* Perform preparatory conversions, as part of the "usual arithmetic
1894 conversions". In particular, as per [expr]:
1896 Whenever an lvalue expression appears as an operand of an
1897 operator that expects the rvalue for that operand, the
1898 lvalue-to-rvalue, array-to-pointer, or function-to-pointer
1899 standard conversions are applied to convert the expression to an
1902 In addition, we perform integral promotions here, as those are
1903 applied to both operands to a binary operator before determining
1904 what additional conversions should apply. */
1907 default_conversion (tree exp)
1909 /* Check for target-specific promotions. */
1910 tree promoted_type = targetm.promoted_type (TREE_TYPE (exp));
1912 exp = cp_convert (promoted_type, exp);
1913 /* Perform the integral promotions first so that bitfield
1914 expressions (which may promote to "int", even if the bitfield is
1915 declared "unsigned") are promoted correctly. */
1916 else if (INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (TREE_TYPE (exp)))
1917 exp = perform_integral_promotions (exp);
1918 /* Perform the other conversions. */
1919 exp = decay_conversion (exp);
1924 /* EXPR is an expression with an integral or enumeration type.
1925 Perform the integral promotions in [conv.prom], and return the
1929 perform_integral_promotions (tree expr)
1934 expr = mark_rvalue_use (expr);
1938 If the bitfield has an enumerated type, it is treated as any
1939 other value of that type for promotion purposes. */
1940 type = is_bitfield_expr_with_lowered_type (expr);
1941 if (!type || TREE_CODE (type) != ENUMERAL_TYPE)
1942 type = TREE_TYPE (expr);
1943 gcc_assert (INTEGRAL_OR_ENUMERATION_TYPE_P (type));
1944 promoted_type = type_promotes_to (type);
1945 if (type != promoted_type)
1946 expr = cp_convert (promoted_type, expr);
1950 /* Returns nonzero iff exp is a STRING_CST or the result of applying
1951 decay_conversion to one. */
1954 string_conv_p (const_tree totype, const_tree exp, int warn)
1958 if (TREE_CODE (totype) != POINTER_TYPE)
1961 t = TREE_TYPE (totype);
1962 if (!same_type_p (t, char_type_node)
1963 && !same_type_p (t, char16_type_node)
1964 && !same_type_p (t, char32_type_node)
1965 && !same_type_p (t, wchar_type_node))
1968 if (TREE_CODE (exp) == STRING_CST)
1970 /* Make sure that we don't try to convert between char and wide chars. */
1971 if (!same_type_p (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (exp))), t))
1976 /* Is this a string constant which has decayed to 'const char *'? */
1977 t = build_pointer_type (cp_build_qualified_type (t, TYPE_QUAL_CONST));
1978 if (!same_type_p (TREE_TYPE (exp), t))
1981 if (TREE_CODE (exp) != ADDR_EXPR
1982 || TREE_CODE (TREE_OPERAND (exp, 0)) != STRING_CST)
1986 /* This warning is not very useful, as it complains about printf. */
1988 warning (OPT_Wwrite_strings,
1989 "deprecated conversion from string constant to %qT",
1995 /* Given a COND_EXPR, MIN_EXPR, or MAX_EXPR in T, return it in a form that we
1996 can, for example, use as an lvalue. This code used to be in
1997 unary_complex_lvalue, but we needed it to deal with `a = (d == c) ? b : c'
1998 expressions, where we're dealing with aggregates. But now it's again only
1999 called from unary_complex_lvalue. The case (in particular) that led to
2000 this was with CODE == ADDR_EXPR, since it's not an lvalue when we'd
2004 rationalize_conditional_expr (enum tree_code code, tree t,
2005 tsubst_flags_t complain)
2007 /* For MIN_EXPR or MAX_EXPR, fold-const.c has arranged things so that
2008 the first operand is always the one to be used if both operands
2009 are equal, so we know what conditional expression this used to be. */
2010 if (TREE_CODE (t) == MIN_EXPR || TREE_CODE (t) == MAX_EXPR)
2012 tree op0 = TREE_OPERAND (t, 0);
2013 tree op1 = TREE_OPERAND (t, 1);
2015 /* The following code is incorrect if either operand side-effects. */
2016 gcc_assert (!TREE_SIDE_EFFECTS (op0)
2017 && !TREE_SIDE_EFFECTS (op1));
2019 build_conditional_expr (build_x_binary_op ((TREE_CODE (t) == MIN_EXPR
2020 ? LE_EXPR : GE_EXPR),
2021 op0, TREE_CODE (op0),
2022 op1, TREE_CODE (op1),
2023 /*overloaded_p=*/NULL,
2025 cp_build_unary_op (code, op0, 0, complain),
2026 cp_build_unary_op (code, op1, 0, complain),
2031 build_conditional_expr (TREE_OPERAND (t, 0),
2032 cp_build_unary_op (code, TREE_OPERAND (t, 1), 0,
2034 cp_build_unary_op (code, TREE_OPERAND (t, 2), 0,
2039 /* Given the TYPE of an anonymous union field inside T, return the
2040 FIELD_DECL for the field. If not found return NULL_TREE. Because
2041 anonymous unions can nest, we must also search all anonymous unions
2042 that are directly reachable. */
2045 lookup_anon_field (tree t, tree type)
2049 for (field = TYPE_FIELDS (t); field; field = DECL_CHAIN (field))
2051 if (TREE_STATIC (field))
2053 if (TREE_CODE (field) != FIELD_DECL || DECL_ARTIFICIAL (field))
2056 /* If we find it directly, return the field. */
2057 if (DECL_NAME (field) == NULL_TREE
2058 && type == TYPE_MAIN_VARIANT (TREE_TYPE (field)))
2063 /* Otherwise, it could be nested, search harder. */
2064 if (DECL_NAME (field) == NULL_TREE
2065 && ANON_AGGR_TYPE_P (TREE_TYPE (field)))
2067 tree subfield = lookup_anon_field (TREE_TYPE (field), type);
2075 /* Build an expression representing OBJECT.MEMBER. OBJECT is an
2076 expression; MEMBER is a DECL or baselink. If ACCESS_PATH is
2077 non-NULL, it indicates the path to the base used to name MEMBER.
2078 If PRESERVE_REFERENCE is true, the expression returned will have
2079 REFERENCE_TYPE if the MEMBER does. Otherwise, the expression
2080 returned will have the type referred to by the reference.
2082 This function does not perform access control; that is either done
2083 earlier by the parser when the name of MEMBER is resolved to MEMBER
2084 itself, or later when overload resolution selects one of the
2085 functions indicated by MEMBER. */
2088 build_class_member_access_expr (tree object, tree member,
2089 tree access_path, bool preserve_reference,
2090 tsubst_flags_t complain)
2094 tree result = NULL_TREE;
2096 if (error_operand_p (object) || error_operand_p (member))
2097 return error_mark_node;
2099 gcc_assert (DECL_P (member) || BASELINK_P (member));
2103 The type of the first expression shall be "class object" (of a
2105 object_type = TREE_TYPE (object);
2106 if (!currently_open_class (object_type)
2107 && !complete_type_or_maybe_complain (object_type, object, complain))
2108 return error_mark_node;
2109 if (!CLASS_TYPE_P (object_type))
2111 if (complain & tf_error)
2112 error ("request for member %qD in %qE, which is of non-class type %qT",
2113 member, object, object_type);
2114 return error_mark_node;
2117 /* The standard does not seem to actually say that MEMBER must be a
2118 member of OBJECT_TYPE. However, that is clearly what is
2120 if (DECL_P (member))
2122 member_scope = DECL_CLASS_CONTEXT (member);
2124 if (TREE_DEPRECATED (member))
2125 warn_deprecated_use (member, NULL_TREE);
2128 member_scope = BINFO_TYPE (BASELINK_ACCESS_BINFO (member));
2129 /* If MEMBER is from an anonymous aggregate, MEMBER_SCOPE will
2130 presently be the anonymous union. Go outwards until we find a
2131 type related to OBJECT_TYPE. */
2132 while (ANON_AGGR_TYPE_P (member_scope)
2133 && !same_type_ignoring_top_level_qualifiers_p (member_scope,
2135 member_scope = TYPE_CONTEXT (member_scope);
2136 if (!member_scope || !DERIVED_FROM_P (member_scope, object_type))
2138 if (complain & tf_error)
2140 if (TREE_CODE (member) == FIELD_DECL)
2141 error ("invalid use of nonstatic data member %qE", member);
2143 error ("%qD is not a member of %qT", member, object_type);
2145 return error_mark_node;
2148 /* Transform `(a, b).x' into `(*(a, &b)).x', `(a ? b : c).x' into
2149 `(*(a ? &b : &c)).x', and so on. A COND_EXPR is only an lvalue
2150 in the front end; only _DECLs and _REFs are lvalues in the back end. */
2152 tree temp = unary_complex_lvalue (ADDR_EXPR, object);
2154 object = cp_build_indirect_ref (temp, RO_NULL, complain);
2157 /* In [expr.ref], there is an explicit list of the valid choices for
2158 MEMBER. We check for each of those cases here. */
2159 if (TREE_CODE (member) == VAR_DECL)
2161 /* A static data member. */
2163 mark_exp_read (object);
2164 /* If OBJECT has side-effects, they are supposed to occur. */
2165 if (TREE_SIDE_EFFECTS (object))
2166 result = build2 (COMPOUND_EXPR, TREE_TYPE (result), object, result);
2168 else if (TREE_CODE (member) == FIELD_DECL)
2170 /* A non-static data member. */
2175 null_object_p = (TREE_CODE (object) == INDIRECT_REF
2176 && integer_zerop (TREE_OPERAND (object, 0)));
2178 /* Convert OBJECT to the type of MEMBER. */
2179 if (!same_type_p (TYPE_MAIN_VARIANT (object_type),
2180 TYPE_MAIN_VARIANT (member_scope)))
2185 binfo = lookup_base (access_path ? access_path : object_type,
2186 member_scope, ba_unique, &kind);
2187 if (binfo == error_mark_node)
2188 return error_mark_node;
2190 /* It is invalid to try to get to a virtual base of a
2191 NULL object. The most common cause is invalid use of
2193 if (null_object_p && kind == bk_via_virtual)
2195 if (complain & tf_error)
2197 error ("invalid access to non-static data member %qD of "
2200 error ("(perhaps the %<offsetof%> macro was used incorrectly)");
2202 return error_mark_node;
2205 /* Convert to the base. */
2206 object = build_base_path (PLUS_EXPR, object, binfo,
2208 /* If we found the base successfully then we should be able
2209 to convert to it successfully. */
2210 gcc_assert (object != error_mark_node);
2213 /* Complain about other invalid uses of offsetof, even though they will
2214 give the right answer. Note that we complain whether or not they
2215 actually used the offsetof macro, since there's no way to know at this
2216 point. So we just give a warning, instead of a pedwarn. */
2217 /* Do not produce this warning for base class field references, because
2218 we know for a fact that didn't come from offsetof. This does occur
2219 in various testsuite cases where a null object is passed where a
2220 vtable access is required. */
2221 if (null_object_p && warn_invalid_offsetof
2222 && CLASSTYPE_NON_STD_LAYOUT (object_type)
2223 && !DECL_FIELD_IS_BASE (member)
2224 && cp_unevaluated_operand == 0
2225 && (complain & tf_warning))
2227 warning (OPT_Winvalid_offsetof,
2228 "invalid access to non-static data member %qD "
2229 " of NULL object", member);
2230 warning (OPT_Winvalid_offsetof,
2231 "(perhaps the %<offsetof%> macro was used incorrectly)");
2234 /* If MEMBER is from an anonymous aggregate, we have converted
2235 OBJECT so that it refers to the class containing the
2236 anonymous union. Generate a reference to the anonymous union
2237 itself, and recur to find MEMBER. */
2238 if (ANON_AGGR_TYPE_P (DECL_CONTEXT (member))
2239 /* When this code is called from build_field_call, the
2240 object already has the type of the anonymous union.
2241 That is because the COMPONENT_REF was already
2242 constructed, and was then disassembled before calling
2243 build_field_call. After the function-call code is
2244 cleaned up, this waste can be eliminated. */
2245 && (!same_type_ignoring_top_level_qualifiers_p
2246 (TREE_TYPE (object), DECL_CONTEXT (member))))
2248 tree anonymous_union;
2250 anonymous_union = lookup_anon_field (TREE_TYPE (object),
2251 DECL_CONTEXT (member));
2252 object = build_class_member_access_expr (object,
2254 /*access_path=*/NULL_TREE,
2259 /* Compute the type of the field, as described in [expr.ref]. */
2260 type_quals = TYPE_UNQUALIFIED;
2261 member_type = TREE_TYPE (member);
2262 if (TREE_CODE (member_type) != REFERENCE_TYPE)
2264 type_quals = (cp_type_quals (member_type)
2265 | cp_type_quals (object_type));
2267 /* A field is const (volatile) if the enclosing object, or the
2268 field itself, is const (volatile). But, a mutable field is
2269 not const, even within a const object. */
2270 if (DECL_MUTABLE_P (member))
2271 type_quals &= ~TYPE_QUAL_CONST;
2272 member_type = cp_build_qualified_type (member_type, type_quals);
2275 result = build3 (COMPONENT_REF, member_type, object, member,
2277 result = fold_if_not_in_template (result);
2279 /* Mark the expression const or volatile, as appropriate. Even
2280 though we've dealt with the type above, we still have to mark the
2281 expression itself. */
2282 if (type_quals & TYPE_QUAL_CONST)
2283 TREE_READONLY (result) = 1;
2284 if (type_quals & TYPE_QUAL_VOLATILE)
2285 TREE_THIS_VOLATILE (result) = 1;
2287 else if (BASELINK_P (member))
2289 /* The member is a (possibly overloaded) member function. */
2293 /* If the MEMBER is exactly one static member function, then we
2294 know the type of the expression. Otherwise, we must wait
2295 until overload resolution has been performed. */
2296 functions = BASELINK_FUNCTIONS (member);
2297 if (TREE_CODE (functions) == FUNCTION_DECL
2298 && DECL_STATIC_FUNCTION_P (functions))
2299 type = TREE_TYPE (functions);
2301 type = unknown_type_node;
2302 /* Note that we do not convert OBJECT to the BASELINK_BINFO
2303 base. That will happen when the function is called. */
2304 result = build3 (COMPONENT_REF, type, object, member, NULL_TREE);
2306 else if (TREE_CODE (member) == CONST_DECL)
2308 /* The member is an enumerator. */
2310 /* If OBJECT has side-effects, they are supposed to occur. */
2311 if (TREE_SIDE_EFFECTS (object))
2312 result = build2 (COMPOUND_EXPR, TREE_TYPE (result),
2317 if (complain & tf_error)
2318 error ("invalid use of %qD", member);
2319 return error_mark_node;
2322 if (!preserve_reference)
2325 If E2 is declared to have type "reference to T", then ... the
2326 type of E1.E2 is T. */
2327 result = convert_from_reference (result);
2332 /* Return the destructor denoted by OBJECT.SCOPE::DTOR_NAME, or, if
2333 SCOPE is NULL, by OBJECT.DTOR_NAME, where DTOR_NAME is ~type. */
2336 lookup_destructor (tree object, tree scope, tree dtor_name)
2338 tree object_type = TREE_TYPE (object);
2339 tree dtor_type = TREE_OPERAND (dtor_name, 0);
2342 if (scope && !check_dtor_name (scope, dtor_type))
2344 error ("qualified type %qT does not match destructor name ~%qT",
2346 return error_mark_node;
2348 if (TREE_CODE (dtor_type) == IDENTIFIER_NODE)
2350 /* In a template, names we can't find a match for are still accepted
2351 destructor names, and we check them here. */
2352 if (check_dtor_name (object_type, dtor_type))
2353 dtor_type = object_type;
2356 error ("object type %qT does not match destructor name ~%qT",
2357 object_type, dtor_type);
2358 return error_mark_node;
2362 else if (!DERIVED_FROM_P (dtor_type, TYPE_MAIN_VARIANT (object_type)))
2364 error ("the type being destroyed is %qT, but the destructor refers to %qT",
2365 TYPE_MAIN_VARIANT (object_type), dtor_type);
2366 return error_mark_node;
2368 expr = lookup_member (dtor_type, complete_dtor_identifier,
2369 /*protect=*/1, /*want_type=*/false);
2370 expr = (adjust_result_of_qualified_name_lookup
2371 (expr, dtor_type, object_type));
2375 /* An expression of the form "A::template B" has been resolved to
2376 DECL. Issue a diagnostic if B is not a template or template
2380 check_template_keyword (tree decl)
2382 /* The standard says:
2386 If a name prefixed by the keyword template is not a member
2387 template, the program is ill-formed.
2389 DR 228 removed the restriction that the template be a member
2392 DR 96, if accepted would add the further restriction that explicit
2393 template arguments must be provided if the template keyword is
2394 used, but, as of 2005-10-16, that DR is still in "drafting". If
2395 this DR is accepted, then the semantic checks here can be
2396 simplified, as the entity named must in fact be a template
2397 specialization, rather than, as at present, a set of overloaded
2398 functions containing at least one template function. */
2399 if (TREE_CODE (decl) != TEMPLATE_DECL
2400 && TREE_CODE (decl) != TEMPLATE_ID_EXPR)
2402 if (!is_overloaded_fn (decl))
2403 permerror (input_location, "%qD is not a template", decl);
2408 if (BASELINK_P (fns))
2409 fns = BASELINK_FUNCTIONS (fns);
2412 tree fn = OVL_CURRENT (fns);
2413 if (TREE_CODE (fn) == TEMPLATE_DECL
2414 || TREE_CODE (fn) == TEMPLATE_ID_EXPR)
2416 if (TREE_CODE (fn) == FUNCTION_DECL
2417 && DECL_USE_TEMPLATE (fn)
2418 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (fn)))
2420 fns = OVL_NEXT (fns);
2423 permerror (input_location, "%qD is not a template", decl);
2428 /* This function is called by the parser to process a class member
2429 access expression of the form OBJECT.NAME. NAME is a node used by
2430 the parser to represent a name; it is not yet a DECL. It may,
2431 however, be a BASELINK where the BASELINK_FUNCTIONS is a
2432 TEMPLATE_ID_EXPR. Templates must be looked up by the parser, and
2433 there is no reason to do the lookup twice, so the parser keeps the
2434 BASELINK. TEMPLATE_P is true iff NAME was explicitly declared to
2435 be a template via the use of the "A::template B" syntax. */
2438 finish_class_member_access_expr (tree object, tree name, bool template_p,
2439 tsubst_flags_t complain)
2444 tree access_path = NULL_TREE;
2445 tree orig_object = object;
2446 tree orig_name = name;
2448 if (object == error_mark_node || name == error_mark_node)
2449 return error_mark_node;
2451 /* If OBJECT is an ObjC class instance, we must obey ObjC access rules. */
2452 if (!objc_is_public (object, name))
2453 return error_mark_node;
2455 object_type = TREE_TYPE (object);
2457 if (processing_template_decl)
2459 if (/* If OBJECT_TYPE is dependent, so is OBJECT.NAME. */
2460 dependent_type_p (object_type)
2461 /* If NAME is just an IDENTIFIER_NODE, then the expression
2463 || TREE_CODE (object) == IDENTIFIER_NODE
2464 /* If NAME is "f<args>", where either 'f' or 'args' is
2465 dependent, then the expression is dependent. */
2466 || (TREE_CODE (name) == TEMPLATE_ID_EXPR
2467 && dependent_template_id_p (TREE_OPERAND (name, 0),
2468 TREE_OPERAND (name, 1)))
2469 /* If NAME is "T::X" where "T" is dependent, then the
2470 expression is dependent. */
2471 || (TREE_CODE (name) == SCOPE_REF
2472 && TYPE_P (TREE_OPERAND (name, 0))
2473 && dependent_type_p (TREE_OPERAND (name, 0))))
2474 return build_min_nt (COMPONENT_REF, object, name, NULL_TREE);
2475 object = build_non_dependent_expr (object);
2477 else if (c_dialect_objc ()
2478 && TREE_CODE (name) == IDENTIFIER_NODE
2479 && (expr = objc_maybe_build_component_ref (object, name)))
2484 The type of the first expression shall be "class object" (of a
2486 if (!currently_open_class (object_type)
2487 && !complete_type_or_maybe_complain (object_type, object, complain))
2488 return error_mark_node;
2489 if (!CLASS_TYPE_P (object_type))
2491 if (complain & tf_error)
2492 error ("request for member %qD in %qE, which is of non-class type %qT",
2493 name, object, object_type);
2494 return error_mark_node;
2497 if (BASELINK_P (name))
2498 /* A member function that has already been looked up. */
2502 bool is_template_id = false;
2503 tree template_args = NULL_TREE;
2506 if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
2508 is_template_id = true;
2509 template_args = TREE_OPERAND (name, 1);
2510 name = TREE_OPERAND (name, 0);
2512 if (TREE_CODE (name) == OVERLOAD)
2513 name = DECL_NAME (get_first_fn (name));
2514 else if (DECL_P (name))
2515 name = DECL_NAME (name);
2518 if (TREE_CODE (name) == SCOPE_REF)
2520 /* A qualified name. The qualifying class or namespace `S'
2521 has already been looked up; it is either a TYPE or a
2523 scope = TREE_OPERAND (name, 0);
2524 name = TREE_OPERAND (name, 1);
2526 /* If SCOPE is a namespace, then the qualified name does not
2527 name a member of OBJECT_TYPE. */
2528 if (TREE_CODE (scope) == NAMESPACE_DECL)
2530 if (complain & tf_error)
2531 error ("%<%D::%D%> is not a member of %qT",
2532 scope, name, object_type);
2533 return error_mark_node;
2536 gcc_assert (CLASS_TYPE_P (scope));
2537 gcc_assert (TREE_CODE (name) == IDENTIFIER_NODE
2538 || TREE_CODE (name) == BIT_NOT_EXPR);
2540 if (constructor_name_p (name, scope))
2542 if (complain & tf_error)
2543 error ("cannot call constructor %<%T::%D%> directly",
2545 return error_mark_node;
2548 /* Find the base of OBJECT_TYPE corresponding to SCOPE. */
2549 access_path = lookup_base (object_type, scope, ba_check, NULL);
2550 if (access_path == error_mark_node)
2551 return error_mark_node;
2554 if (complain & tf_error)
2555 error ("%qT is not a base of %qT", scope, object_type);
2556 return error_mark_node;
2562 access_path = object_type;
2565 if (TREE_CODE (name) == BIT_NOT_EXPR)
2566 member = lookup_destructor (object, scope, name);
2569 /* Look up the member. */
2570 member = lookup_member (access_path, name, /*protect=*/1,
2571 /*want_type=*/false);
2572 if (member == NULL_TREE)
2574 if (complain & tf_error)
2575 error ("%qD has no member named %qE", object_type, name);
2576 return error_mark_node;
2578 if (member == error_mark_node)
2579 return error_mark_node;
2584 tree templ = member;
2586 if (BASELINK_P (templ))
2587 templ = lookup_template_function (templ, template_args);
2590 if (complain & tf_error)
2591 error ("%qD is not a member template function", name);
2592 return error_mark_node;
2597 if (TREE_DEPRECATED (member))
2598 warn_deprecated_use (member, NULL_TREE);
2601 check_template_keyword (member);
2603 expr = build_class_member_access_expr (object, member, access_path,
2604 /*preserve_reference=*/false,
2606 if (processing_template_decl && expr != error_mark_node)
2608 if (BASELINK_P (member))
2610 if (TREE_CODE (orig_name) == SCOPE_REF)
2611 BASELINK_QUALIFIED_P (member) = 1;
2614 return build_min_non_dep (COMPONENT_REF, expr,
2615 orig_object, orig_name,
2622 /* Return an expression for the MEMBER_NAME field in the internal
2623 representation of PTRMEM, a pointer-to-member function. (Each
2624 pointer-to-member function type gets its own RECORD_TYPE so it is
2625 more convenient to access the fields by name than by FIELD_DECL.)
2626 This routine converts the NAME to a FIELD_DECL and then creates the
2627 node for the complete expression. */
2630 build_ptrmemfunc_access_expr (tree ptrmem, tree member_name)
2636 /* This code is a stripped down version of
2637 build_class_member_access_expr. It does not work to use that
2638 routine directly because it expects the object to be of class
2640 ptrmem_type = TREE_TYPE (ptrmem);
2641 gcc_assert (TYPE_PTRMEMFUNC_P (ptrmem_type));
2642 member = lookup_member (ptrmem_type, member_name, /*protect=*/0,
2643 /*want_type=*/false);
2644 member_type = cp_build_qualified_type (TREE_TYPE (member),
2645 cp_type_quals (ptrmem_type));
2646 return fold_build3_loc (input_location,
2647 COMPONENT_REF, member_type,
2648 ptrmem, member, NULL_TREE);
2651 /* Given an expression PTR for a pointer, return an expression
2652 for the value pointed to.
2653 ERRORSTRING is the name of the operator to appear in error messages.
2655 This function may need to overload OPERATOR_FNNAME.
2656 Must also handle REFERENCE_TYPEs for C++. */
2659 build_x_indirect_ref (tree expr, ref_operator errorstring,
2660 tsubst_flags_t complain)
2662 tree orig_expr = expr;
2665 if (processing_template_decl)
2667 /* Retain the type if we know the operand is a pointer so that
2668 describable_type doesn't make auto deduction break. */
2669 if (TREE_TYPE (expr) && POINTER_TYPE_P (TREE_TYPE (expr)))
2670 return build_min (INDIRECT_REF, TREE_TYPE (TREE_TYPE (expr)), expr);
2671 if (type_dependent_expression_p (expr))
2672 return build_min_nt (INDIRECT_REF, expr);
2673 expr = build_non_dependent_expr (expr);
2676 rval = build_new_op (INDIRECT_REF, LOOKUP_NORMAL, expr, NULL_TREE,
2677 NULL_TREE, /*overloaded_p=*/NULL, complain);
2679 rval = cp_build_indirect_ref (expr, errorstring, complain);
2681 if (processing_template_decl && rval != error_mark_node)
2682 return build_min_non_dep (INDIRECT_REF, rval, orig_expr);
2687 /* Helper function called from c-common. */
2689 build_indirect_ref (location_t loc __attribute__ ((__unused__)),
2690 tree ptr, ref_operator errorstring)
2692 return cp_build_indirect_ref (ptr, errorstring, tf_warning_or_error);
2696 cp_build_indirect_ref (tree ptr, ref_operator errorstring,
2697 tsubst_flags_t complain)
2701 if (ptr == error_mark_node)
2702 return error_mark_node;
2704 if (ptr == current_class_ptr)
2705 return current_class_ref;
2707 pointer = (TREE_CODE (TREE_TYPE (ptr)) == REFERENCE_TYPE
2708 ? ptr : decay_conversion (ptr));
2709 type = TREE_TYPE (pointer);
2711 if (POINTER_TYPE_P (type))
2715 If the type of the expression is "pointer to T," the type
2716 of the result is "T." */
2717 tree t = TREE_TYPE (type);
2719 if (CONVERT_EXPR_P (ptr)
2720 || TREE_CODE (ptr) == VIEW_CONVERT_EXPR)
2722 /* If a warning is issued, mark it to avoid duplicates from
2723 the backend. This only needs to be done at
2724 warn_strict_aliasing > 2. */
2725 if (warn_strict_aliasing > 2)
2726 if (strict_aliasing_warning (TREE_TYPE (TREE_OPERAND (ptr, 0)),
2727 type, TREE_OPERAND (ptr, 0)))
2728 TREE_NO_WARNING (ptr) = 1;
2731 if (VOID_TYPE_P (t))
2733 /* A pointer to incomplete type (other than cv void) can be
2734 dereferenced [expr.unary.op]/1 */
2735 if (complain & tf_error)
2736 error ("%qT is not a pointer-to-object type", type);
2737 return error_mark_node;
2739 else if (TREE_CODE (pointer) == ADDR_EXPR
2740 && same_type_p (t, TREE_TYPE (TREE_OPERAND (pointer, 0))))
2741 /* The POINTER was something like `&x'. We simplify `*&x' to
2743 return TREE_OPERAND (pointer, 0);
2746 tree ref = build1 (INDIRECT_REF, t, pointer);
2748 /* We *must* set TREE_READONLY when dereferencing a pointer to const,
2749 so that we get the proper error message if the result is used
2750 to assign to. Also, &* is supposed to be a no-op. */
2751 TREE_READONLY (ref) = CP_TYPE_CONST_P (t);
2752 TREE_THIS_VOLATILE (ref) = CP_TYPE_VOLATILE_P (t);
2753 TREE_SIDE_EFFECTS (ref)
2754 = (TREE_THIS_VOLATILE (ref) || TREE_SIDE_EFFECTS (pointer));
2758 else if (!(complain & tf_error))
2759 /* Don't emit any errors; we'll just return ERROR_MARK_NODE later. */
2761 /* `pointer' won't be an error_mark_node if we were given a
2762 pointer to member, so it's cool to check for this here. */
2763 else if (TYPE_PTR_TO_MEMBER_P (type))
2764 switch (errorstring)
2766 case RO_ARRAY_INDEXING:
2767 error ("invalid use of array indexing on pointer to member");
2770 error ("invalid use of unary %<*%> on pointer to member");
2772 case RO_IMPLICIT_CONVERSION:
2773 error ("invalid use of implicit conversion on pointer to member");
2778 else if (pointer != error_mark_node)
2779 switch (errorstring)
2782 error ("invalid type argument");
2784 case RO_ARRAY_INDEXING:
2785 error ("invalid type argument of array indexing");
2788 error ("invalid type argument of unary %<*%>");
2790 case RO_IMPLICIT_CONVERSION:
2791 error ("invalid type argument of implicit conversion");
2796 return error_mark_node;
2799 /* This handles expressions of the form "a[i]", which denotes
2802 This is logically equivalent in C to *(a+i), but we may do it differently.
2803 If A is a variable or a member, we generate a primitive ARRAY_REF.
2804 This avoids forcing the array out of registers, and can work on
2805 arrays that are not lvalues (for example, members of structures returned
2808 If INDEX is of some user-defined type, it must be converted to
2809 integer type. Otherwise, to make a compatible PLUS_EXPR, it
2810 will inherit the type of the array, which will be some pointer type.
2812 LOC is the location to use in building the array reference. */
2815 cp_build_array_ref (location_t loc, tree array, tree idx,
2816 tsubst_flags_t complain)
2822 if (complain & tf_error)
2823 error_at (loc, "subscript missing in array reference");
2824 return error_mark_node;
2827 if (TREE_TYPE (array) == error_mark_node
2828 || TREE_TYPE (idx) == error_mark_node)
2829 return error_mark_node;
2831 /* If ARRAY is a COMPOUND_EXPR or COND_EXPR, move our reference
2833 switch (TREE_CODE (array))
2837 tree value = cp_build_array_ref (loc, TREE_OPERAND (array, 1), idx,
2839 ret = build2 (COMPOUND_EXPR, TREE_TYPE (value),
2840 TREE_OPERAND (array, 0), value);
2841 SET_EXPR_LOCATION (ret, loc);
2846 ret = build_conditional_expr
2847 (TREE_OPERAND (array, 0),
2848 cp_build_array_ref (loc, TREE_OPERAND (array, 1), idx,
2850 cp_build_array_ref (loc, TREE_OPERAND (array, 2), idx,
2852 tf_warning_or_error);
2853 protected_set_expr_location (ret, loc);
2860 if (TREE_CODE (TREE_TYPE (array)) == ARRAY_TYPE)
2864 warn_array_subscript_with_type_char (idx);
2866 if (!INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (TREE_TYPE (idx)))
2868 if (complain & tf_error)
2869 error_at (loc, "array subscript is not an integer");
2870 return error_mark_node;
2873 /* Apply integral promotions *after* noticing character types.
2874 (It is unclear why we do these promotions -- the standard
2875 does not say that we should. In fact, the natural thing would
2876 seem to be to convert IDX to ptrdiff_t; we're performing
2877 pointer arithmetic.) */
2878 idx = perform_integral_promotions (idx);
2880 /* An array that is indexed by a non-constant
2881 cannot be stored in a register; we must be able to do
2882 address arithmetic on its address.
2883 Likewise an array of elements of variable size. */
2884 if (TREE_CODE (idx) != INTEGER_CST
2885 || (COMPLETE_TYPE_P (TREE_TYPE (TREE_TYPE (array)))
2886 && (TREE_CODE (TYPE_SIZE (TREE_TYPE (TREE_TYPE (array))))
2889 if (!cxx_mark_addressable (array))
2890 return error_mark_node;
2893 /* An array that is indexed by a constant value which is not within
2894 the array bounds cannot be stored in a register either; because we
2895 would get a crash in store_bit_field/extract_bit_field when trying
2896 to access a non-existent part of the register. */
2897 if (TREE_CODE (idx) == INTEGER_CST
2898 && TYPE_DOMAIN (TREE_TYPE (array))
2899 && ! int_fits_type_p (idx, TYPE_DOMAIN (TREE_TYPE (array))))
2901 if (!cxx_mark_addressable (array))
2902 return error_mark_node;
2905 if (!lvalue_p (array) && (complain & tf_error))
2906 pedwarn (loc, OPT_pedantic,
2907 "ISO C++ forbids subscripting non-lvalue array");
2909 /* Note in C++ it is valid to subscript a `register' array, since
2910 it is valid to take the address of something with that
2911 storage specification. */
2915 while (TREE_CODE (foo) == COMPONENT_REF)
2916 foo = TREE_OPERAND (foo, 0);
2917 if (TREE_CODE (foo) == VAR_DECL && DECL_REGISTER (foo)
2918 && (complain & tf_warning))
2919 warning_at (loc, OPT_Wextra,
2920 "subscripting array declared %<register%>");
2923 type = TREE_TYPE (TREE_TYPE (array));
2924 rval = build4 (ARRAY_REF, type, array, idx, NULL_TREE, NULL_TREE);
2925 /* Array ref is const/volatile if the array elements are
2926 or if the array is.. */
2927 TREE_READONLY (rval)
2928 |= (CP_TYPE_CONST_P (type) | TREE_READONLY (array));
2929 TREE_SIDE_EFFECTS (rval)
2930 |= (CP_TYPE_VOLATILE_P (type) | TREE_SIDE_EFFECTS (array));
2931 TREE_THIS_VOLATILE (rval)
2932 |= (CP_TYPE_VOLATILE_P (type) | TREE_THIS_VOLATILE (array));
2933 ret = require_complete_type_sfinae (fold_if_not_in_template (rval),
2935 protected_set_expr_location (ret, loc);
2940 tree ar = default_conversion (array);
2941 tree ind = default_conversion (idx);
2943 /* Put the integer in IND to simplify error checking. */
2944 if (TREE_CODE (TREE_TYPE (ar)) == INTEGER_TYPE)
2951 if (ar == error_mark_node)
2954 if (TREE_CODE (TREE_TYPE (ar)) != POINTER_TYPE)
2956 if (complain & tf_error)
2957 error_at (loc, "subscripted value is neither array nor pointer");
2958 return error_mark_node;
2960 if (TREE_CODE (TREE_TYPE (ind)) != INTEGER_TYPE)
2962 if (complain & tf_error)
2963 error_at (loc, "array subscript is not an integer");
2964 return error_mark_node;
2967 warn_array_subscript_with_type_char (idx);
2969 ret = cp_build_indirect_ref (cp_build_binary_op (input_location,
2974 protected_set_expr_location (ret, loc);
2979 /* Entry point for Obj-C++. */
2982 build_array_ref (location_t loc, tree array, tree idx)
2984 return cp_build_array_ref (loc, array, idx, tf_warning_or_error);
2987 /* Resolve a pointer to member function. INSTANCE is the object
2988 instance to use, if the member points to a virtual member.
2990 This used to avoid checking for virtual functions if basetype
2991 has no virtual functions, according to an earlier ANSI draft.
2992 With the final ISO C++ rules, such an optimization is
2993 incorrect: A pointer to a derived member can be static_cast
2994 to pointer-to-base-member, as long as the dynamic object
2995 later has the right member. */
2998 get_member_function_from_ptrfunc (tree *instance_ptrptr, tree function)
3000 if (TREE_CODE (function) == OFFSET_REF)
3001 function = TREE_OPERAND (function, 1);
3003 if (TYPE_PTRMEMFUNC_P (TREE_TYPE (function)))
3005 tree idx, delta, e1, e2, e3, vtbl, basetype;
3006 tree fntype = TYPE_PTRMEMFUNC_FN_TYPE (TREE_TYPE (function));
3008 tree instance_ptr = *instance_ptrptr;
3009 tree instance_save_expr = 0;
3010 if (instance_ptr == error_mark_node)
3012 if (TREE_CODE (function) == PTRMEM_CST)
3014 /* Extracting the function address from a pmf is only
3015 allowed with -Wno-pmf-conversions. It only works for
3017 e1 = build_addr_func (PTRMEM_CST_MEMBER (function));
3018 e1 = convert (fntype, e1);
3023 error ("object missing in use of %qE", function);
3024 return error_mark_node;
3028 if (TREE_SIDE_EFFECTS (instance_ptr))
3029 instance_ptr = instance_save_expr = save_expr (instance_ptr);
3031 if (TREE_SIDE_EFFECTS (function))
3032 function = save_expr (function);
3034 /* Start by extracting all the information from the PMF itself. */
3035 e3 = pfn_from_ptrmemfunc (function);
3036 delta = delta_from_ptrmemfunc (function);
3037 idx = build1 (NOP_EXPR, vtable_index_type, e3);
3038 switch (TARGET_PTRMEMFUNC_VBIT_LOCATION)
3040 case ptrmemfunc_vbit_in_pfn:
3041 e1 = cp_build_binary_op (input_location,
3042 BIT_AND_EXPR, idx, integer_one_node,
3043 tf_warning_or_error);
3044 idx = cp_build_binary_op (input_location,
3045 MINUS_EXPR, idx, integer_one_node,
3046 tf_warning_or_error);
3049 case ptrmemfunc_vbit_in_delta:
3050 e1 = cp_build_binary_op (input_location,
3051 BIT_AND_EXPR, delta, integer_one_node,
3052 tf_warning_or_error);
3053 delta = cp_build_binary_op (input_location,
3054 RSHIFT_EXPR, delta, integer_one_node,
3055 tf_warning_or_error);
3062 /* Convert down to the right base before using the instance. A
3063 special case is that in a pointer to member of class C, C may
3064 be incomplete. In that case, the function will of course be
3065 a member of C, and no conversion is required. In fact,
3066 lookup_base will fail in that case, because incomplete
3067 classes do not have BINFOs. */
3068 basetype = TYPE_METHOD_BASETYPE (TREE_TYPE (fntype));
3069 if (!same_type_ignoring_top_level_qualifiers_p
3070 (basetype, TREE_TYPE (TREE_TYPE (instance_ptr))))
3072 basetype = lookup_base (TREE_TYPE (TREE_TYPE (instance_ptr)),
3073 basetype, ba_check, NULL);
3074 instance_ptr = build_base_path (PLUS_EXPR, instance_ptr, basetype,
3076 if (instance_ptr == error_mark_node)
3077 return error_mark_node;
3079 /* ...and then the delta in the PMF. */
3080 instance_ptr = build2 (POINTER_PLUS_EXPR, TREE_TYPE (instance_ptr),
3081 instance_ptr, fold_convert (sizetype, delta));
3083 /* Hand back the adjusted 'this' argument to our caller. */
3084 *instance_ptrptr = instance_ptr;
3086 /* Next extract the vtable pointer from the object. */
3087 vtbl = build1 (NOP_EXPR, build_pointer_type (vtbl_ptr_type_node),
3089 vtbl = cp_build_indirect_ref (vtbl, RO_NULL, tf_warning_or_error);
3090 /* If the object is not dynamic the access invokes undefined
3091 behavior. As it is not executed in this case silence the
3092 spurious warnings it may provoke. */
3093 TREE_NO_WARNING (vtbl) = 1;
3095 /* Finally, extract the function pointer from the vtable. */
3096 e2 = fold_build2_loc (input_location,
3097 POINTER_PLUS_EXPR, TREE_TYPE (vtbl), vtbl,
3098 fold_convert (sizetype, idx));
3099 e2 = cp_build_indirect_ref (e2, RO_NULL, tf_warning_or_error);
3100 TREE_CONSTANT (e2) = 1;
3102 /* When using function descriptors, the address of the
3103 vtable entry is treated as a function pointer. */
3104 if (TARGET_VTABLE_USES_DESCRIPTORS)
3105 e2 = build1 (NOP_EXPR, TREE_TYPE (e2),
3106 cp_build_addr_expr (e2, tf_warning_or_error));
3108 e2 = fold_convert (TREE_TYPE (e3), e2);
3109 e1 = build_conditional_expr (e1, e2, e3, tf_warning_or_error);
3111 /* Make sure this doesn't get evaluated first inside one of the
3112 branches of the COND_EXPR. */
3113 if (instance_save_expr)
3114 e1 = build2 (COMPOUND_EXPR, TREE_TYPE (e1),
3115 instance_save_expr, e1);
3122 /* Used by the C-common bits. */
3124 build_function_call (location_t loc ATTRIBUTE_UNUSED,
3125 tree function, tree params)
3127 return cp_build_function_call (function, params, tf_warning_or_error);
3130 /* Used by the C-common bits. */
3132 build_function_call_vec (location_t loc ATTRIBUTE_UNUSED,
3133 tree function, VEC(tree,gc) *params,
3134 VEC(tree,gc) *origtypes ATTRIBUTE_UNUSED)
3136 VEC(tree,gc) *orig_params = params;
3137 tree ret = cp_build_function_call_vec (function, ¶ms,
3138 tf_warning_or_error);
3140 /* cp_build_function_call_vec can reallocate PARAMS by adding
3141 default arguments. That should never happen here. Verify
3143 gcc_assert (params == orig_params);
3148 /* Build a function call using a tree list of arguments. */
3151 cp_build_function_call (tree function, tree params, tsubst_flags_t complain)
3156 vec = make_tree_vector ();
3157 for (; params != NULL_TREE; params = TREE_CHAIN (params))
3158 VEC_safe_push (tree, gc, vec, TREE_VALUE (params));
3159 ret = cp_build_function_call_vec (function, &vec, complain);
3160 release_tree_vector (vec);
3164 /* Build a function call using varargs. */
3167 cp_build_function_call_nary (tree function, tsubst_flags_t complain, ...)
3173 vec = make_tree_vector ();
3174 va_start (args, complain);
3175 for (t = va_arg (args, tree); t != NULL_TREE; t = va_arg (args, tree))
3176 VEC_safe_push (tree, gc, vec, t);
3178 ret = cp_build_function_call_vec (function, &vec, complain);
3179 release_tree_vector (vec);
3183 /* Build a function call using a vector of arguments. PARAMS may be
3184 NULL if there are no parameters. This changes the contents of
3188 cp_build_function_call_vec (tree function, VEC(tree,gc) **params,
3189 tsubst_flags_t complain)
3191 tree fntype, fndecl;
3193 tree original = function;
3197 VEC(tree,gc) *allocated = NULL;
3200 /* For Objective-C, convert any calls via a cast to OBJC_TYPE_REF
3201 expressions, like those used for ObjC messenger dispatches. */
3202 if (params != NULL && !VEC_empty (tree, *params))
3203 function = objc_rewrite_function_call (function,
3204 VEC_index (tree, *params, 0));
3206 /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
3207 Strip such NOP_EXPRs, since FUNCTION is used in non-lvalue context. */
3208 if (TREE_CODE (function) == NOP_EXPR
3209 && TREE_TYPE (function) == TREE_TYPE (TREE_OPERAND (function, 0)))
3210 function = TREE_OPERAND (function, 0);
3212 if (TREE_CODE (function) == FUNCTION_DECL)
3214 mark_used (function);
3217 /* Convert anything with function type to a pointer-to-function. */
3218 if (DECL_MAIN_P (function) && (complain & tf_error))
3219 pedwarn (input_location, OPT_pedantic,
3220 "ISO C++ forbids calling %<::main%> from within program");
3222 function = build_addr_func (function);
3228 function = build_addr_func (function);
3231 if (function == error_mark_node)
3232 return error_mark_node;
3234 fntype = TREE_TYPE (function);
3236 if (TYPE_PTRMEMFUNC_P (fntype))
3238 if (complain & tf_error)
3239 error ("must use %<.*%> or %<->*%> to call pointer-to-member "
3240 "function in %<%E (...)%>, e.g. %<(... ->* %E) (...)%>",
3241 original, original);
3242 return error_mark_node;
3245 is_method = (TREE_CODE (fntype) == POINTER_TYPE
3246 && TREE_CODE (TREE_TYPE (fntype)) == METHOD_TYPE);
3248 if (!((TREE_CODE (fntype) == POINTER_TYPE
3249 && TREE_CODE (TREE_TYPE (fntype)) == FUNCTION_TYPE)
3251 || TREE_CODE (function) == TEMPLATE_ID_EXPR))
3253 if (complain & tf_error)
3254 error ("%qE cannot be used as a function", original);
3255 return error_mark_node;
3258 /* fntype now gets the type of function pointed to. */
3259 fntype = TREE_TYPE (fntype);
3260 parm_types = TYPE_ARG_TYPES (fntype);
3264 allocated = make_tree_vector ();
3265 params = &allocated;
3268 nargs = convert_arguments (parm_types, params, fndecl, LOOKUP_NORMAL,
3271 return error_mark_node;
3273 argarray = VEC_address (tree, *params);
3275 /* Check for errors in format strings and inappropriately
3277 check_function_arguments (TYPE_ATTRIBUTES (fntype), nargs, argarray,
3280 ret = build_cxx_call (function, nargs, argarray);
3282 if (allocated != NULL)
3283 release_tree_vector (allocated);
3288 /* Subroutine of convert_arguments.
3289 Warn about wrong number of args are genereted. */
3292 warn_args_num (location_t loc, tree fndecl, bool too_many_p)
3296 if (TREE_CODE (TREE_TYPE (fndecl)) == METHOD_TYPE)
3298 if (DECL_NAME (fndecl) == NULL_TREE
3299 || IDENTIFIER_HAS_TYPE_VALUE (DECL_NAME (fndecl)))
3302 ? G_("too many arguments to constructor %q#D")
3303 : G_("too few arguments to constructor %q#D"),
3308 ? G_("too many arguments to member function %q#D")
3309 : G_("too few arguments to member function %q#D"),
3315 ? G_("too many arguments to function %q#D")
3316 : G_("too few arguments to function %q#D"),
3318 inform (DECL_SOURCE_LOCATION (fndecl),
3323 if (c_dialect_objc () && objc_message_selector ())
3326 ? G_("too many arguments to method %q#D")
3327 : G_("too few arguments to method %q#D"),
3328 objc_message_selector ());
3330 error_at (loc, too_many_p ? G_("too many arguments to function")
3331 : G_("too few arguments to function"));
3335 /* Convert the actual parameter expressions in the list VALUES to the
3336 types in the list TYPELIST. The converted expressions are stored
3337 back in the VALUES vector.
3338 If parmdecls is exhausted, or when an element has NULL as its type,
3339 perform the default conversions.
3341 NAME is an IDENTIFIER_NODE or 0. It is used only for error messages.
3343 This is also where warnings about wrong number of args are generated.
3345 Returns the actual number of arguments processed (which might be less
3346 than the length of the vector), or -1 on error.
3348 In C++, unspecified trailing parameters can be filled in with their
3349 default arguments, if such were specified. Do so here. */
3352 convert_arguments (tree typelist, VEC(tree,gc) **values, tree fndecl,
3353 int flags, tsubst_flags_t complain)
3358 /* Argument passing is always copy-initialization. */
3359 flags |= LOOKUP_ONLYCONVERTING;
3361 for (i = 0, typetail = typelist;
3362 i < VEC_length (tree, *values);
3365 tree type = typetail ? TREE_VALUE (typetail) : 0;
3366 tree val = VEC_index (tree, *values, i);
3368 if (val == error_mark_node || type == error_mark_node)
3371 if (type == void_type_node)
3373 if (complain & tf_error)
3375 warn_args_num (input_location, fndecl, /*too_many_p=*/true);
3382 /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
3383 Strip such NOP_EXPRs, since VAL is used in non-lvalue context. */
3384 if (TREE_CODE (val) == NOP_EXPR
3385 && TREE_TYPE (val) == TREE_TYPE (TREE_OPERAND (val, 0))
3386 && (type == 0 || TREE_CODE (type) != REFERENCE_TYPE))
3387 val = TREE_OPERAND (val, 0);
3389 if (type == 0 || TREE_CODE (type) != REFERENCE_TYPE)
3391 if (TREE_CODE (TREE_TYPE (val)) == ARRAY_TYPE
3392 || TREE_CODE (TREE_TYPE (val)) == FUNCTION_TYPE
3393 || TREE_CODE (TREE_TYPE (val)) == METHOD_TYPE)
3394 val = decay_conversion (val);
3397 if (val == error_mark_node)
3402 /* Formal parm type is specified by a function prototype. */
3405 if (!COMPLETE_TYPE_P (complete_type (type)))
3407 if (complain & tf_error)
3410 error ("parameter %P of %qD has incomplete type %qT",
3413 error ("parameter %P has incomplete type %qT", i, type);
3415 parmval = error_mark_node;
3419 parmval = convert_for_initialization
3420 (NULL_TREE, type, val, flags,
3421 ICR_ARGPASS, fndecl, i, complain);
3422 parmval = convert_for_arg_passing (type, parmval);
3425 if (parmval == error_mark_node)
3428 VEC_replace (tree, *values, i, parmval);
3432 if (fndecl && DECL_BUILT_IN (fndecl)
3433 && DECL_FUNCTION_CODE (fndecl) == BUILT_IN_CONSTANT_P)
3434 /* Don't do ellipsis conversion for __built_in_constant_p
3435 as this will result in spurious errors for non-trivial
3437 val = require_complete_type_sfinae (val, complain);
3439 val = convert_arg_to_ellipsis (val);
3441 VEC_replace (tree, *values, i, val);
3445 typetail = TREE_CHAIN (typetail);
3448 if (typetail != 0 && typetail != void_list_node)
3450 /* See if there are default arguments that can be used. Because
3451 we hold default arguments in the FUNCTION_TYPE (which is so
3452 wrong), we can see default parameters here from deduced
3453 contexts (and via typeof) for indirect function calls.
3454 Fortunately we know whether we have a function decl to
3455 provide default arguments in a language conformant
3457 if (fndecl && TREE_PURPOSE (typetail)
3458 && TREE_CODE (TREE_PURPOSE (typetail)) != DEFAULT_ARG)
3460 for (; typetail != void_list_node; ++i)
3463 = convert_default_arg (TREE_VALUE (typetail),
3464 TREE_PURPOSE (typetail),
3467 if (parmval == error_mark_node)
3470 VEC_safe_push (tree, gc, *values, parmval);
3471 typetail = TREE_CHAIN (typetail);
3472 /* ends with `...'. */
3473 if (typetail == NULL_TREE)