1 /* Build expressions with type checking for C++ compiler.
2 Copyright (C) 1987-2013 Free Software Foundation, Inc.
3 Hacked by Michael Tiemann (tiemann@cygnus.com)
5 This file is part of GCC.
7 GCC is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3, or (at your option)
12 GCC is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING3. If not see
19 <http://www.gnu.org/licenses/>. */
22 /* This file is part of the C++ front end.
23 It contains routines to build C++ expressions given their operands,
24 including computing the types of the result, C and C++ specific error
25 checks, and some optimization. */
29 #include "coretypes.h"
34 #include "diagnostic.h"
38 #include "c-family/c-common.h"
39 #include "c-family/c-objc.h"
42 static tree
pfn_from_ptrmemfunc (tree
);
43 static tree
delta_from_ptrmemfunc (tree
);
44 static tree
convert_for_assignment (tree
, tree
, impl_conv_rhs
, tree
, int,
46 static tree
cp_pointer_int_sum (enum tree_code
, tree
, tree
);
47 static tree
rationalize_conditional_expr (enum tree_code
, tree
,
49 static int comp_ptr_ttypes_real (tree
, tree
, int);
50 static bool comp_except_types (tree
, tree
, bool);
51 static bool comp_array_types (const_tree
, const_tree
, bool);
52 static tree
pointer_diff (tree
, tree
, tree
, tsubst_flags_t
);
53 static tree
get_delta_difference (tree
, tree
, bool, bool, tsubst_flags_t
);
54 static void casts_away_constness_r (tree
*, tree
*, tsubst_flags_t
);
55 static bool casts_away_constness (tree
, tree
, tsubst_flags_t
);
56 static void maybe_warn_about_returning_address_of_local (tree
);
57 static tree
lookup_destructor (tree
, tree
, tree
, tsubst_flags_t
);
58 static void warn_args_num (location_t
, tree
, bool);
59 static int convert_arguments (tree
, vec
<tree
, va_gc
> **, tree
, int,
62 /* Do `exp = require_complete_type (exp);' to make sure exp
63 does not have an incomplete type. (That includes void types.)
64 Returns error_mark_node if the VALUE does not have
65 complete type when this function returns. */
68 require_complete_type_sfinae (tree value
, tsubst_flags_t complain
)
72 if (processing_template_decl
|| value
== error_mark_node
)
75 if (TREE_CODE (value
) == OVERLOAD
)
76 type
= unknown_type_node
;
78 type
= TREE_TYPE (value
);
80 if (type
== error_mark_node
)
81 return error_mark_node
;
83 /* First, detect a valid value with a complete type. */
84 if (COMPLETE_TYPE_P (type
))
87 if (complete_type_or_maybe_complain (type
, value
, complain
))
90 return error_mark_node
;
94 require_complete_type (tree value
)
96 return require_complete_type_sfinae (value
, tf_warning_or_error
);
99 /* Try to complete TYPE, if it is incomplete. For example, if TYPE is
100 a template instantiation, do the instantiation. Returns TYPE,
101 whether or not it could be completed, unless something goes
102 horribly wrong, in which case the error_mark_node is returned. */
105 complete_type (tree type
)
107 if (type
== NULL_TREE
)
108 /* Rather than crash, we return something sure to cause an error
110 return error_mark_node
;
112 if (type
== error_mark_node
|| COMPLETE_TYPE_P (type
))
114 else if (TREE_CODE (type
) == ARRAY_TYPE
&& TYPE_DOMAIN (type
))
116 tree t
= complete_type (TREE_TYPE (type
));
117 unsigned int needs_constructing
, has_nontrivial_dtor
;
118 if (COMPLETE_TYPE_P (t
) && !dependent_type_p (type
))
121 = TYPE_NEEDS_CONSTRUCTING (TYPE_MAIN_VARIANT (t
));
123 = TYPE_HAS_NONTRIVIAL_DESTRUCTOR (TYPE_MAIN_VARIANT (t
));
124 for (t
= TYPE_MAIN_VARIANT (type
); t
; t
= TYPE_NEXT_VARIANT (t
))
126 TYPE_NEEDS_CONSTRUCTING (t
) = needs_constructing
;
127 TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t
) = has_nontrivial_dtor
;
130 else if (CLASS_TYPE_P (type
) && CLASSTYPE_TEMPLATE_INSTANTIATION (type
))
131 instantiate_class_template (TYPE_MAIN_VARIANT (type
));
136 /* Like complete_type, but issue an error if the TYPE cannot be completed.
137 VALUE is used for informative diagnostics.
138 Returns NULL_TREE if the type cannot be made complete. */
141 complete_type_or_maybe_complain (tree type
, tree value
, tsubst_flags_t complain
)
143 type
= complete_type (type
);
144 if (type
== error_mark_node
)
145 /* We already issued an error. */
147 else if (!COMPLETE_TYPE_P (type
))
149 if (complain
& tf_error
)
150 cxx_incomplete_type_diagnostic (value
, type
, DK_ERROR
);
158 complete_type_or_else (tree type
, tree value
)
160 return complete_type_or_maybe_complain (type
, value
, tf_warning_or_error
);
163 /* Return truthvalue of whether type of EXP is instantiated. */
166 type_unknown_p (const_tree exp
)
168 return (TREE_CODE (exp
) == TREE_LIST
169 || TREE_TYPE (exp
) == unknown_type_node
);
173 /* Return the common type of two parameter lists.
174 We assume that comptypes has already been done and returned 1;
175 if that isn't so, this may crash.
177 As an optimization, free the space we allocate if the parameter
178 lists are already common. */
181 commonparms (tree p1
, tree p2
)
183 tree oldargs
= p1
, newargs
, n
;
187 len
= list_length (p1
);
188 newargs
= tree_last (p1
);
190 if (newargs
== void_list_node
)
199 newargs
= tree_cons (NULL_TREE
, NULL_TREE
, newargs
);
204 p1
= TREE_CHAIN (p1
), p2
= TREE_CHAIN (p2
), n
= TREE_CHAIN (n
), i
++)
206 if (TREE_PURPOSE (p1
) && !TREE_PURPOSE (p2
))
208 TREE_PURPOSE (n
) = TREE_PURPOSE (p1
);
211 else if (! TREE_PURPOSE (p1
))
213 if (TREE_PURPOSE (p2
))
215 TREE_PURPOSE (n
) = TREE_PURPOSE (p2
);
221 if (1 != simple_cst_equal (TREE_PURPOSE (p1
), TREE_PURPOSE (p2
)))
223 TREE_PURPOSE (n
) = TREE_PURPOSE (p2
);
225 if (TREE_VALUE (p1
) != TREE_VALUE (p2
))
228 TREE_VALUE (n
) = merge_types (TREE_VALUE (p1
), TREE_VALUE (p2
));
231 TREE_VALUE (n
) = TREE_VALUE (p1
);
239 /* Given a type, perhaps copied for a typedef,
240 find the "original" version of it. */
242 original_type (tree t
)
244 int quals
= cp_type_quals (t
);
245 while (t
!= error_mark_node
246 && TYPE_NAME (t
) != NULL_TREE
)
248 tree x
= TYPE_NAME (t
);
249 if (TREE_CODE (x
) != TYPE_DECL
)
251 x
= DECL_ORIGINAL_TYPE (x
);
256 return cp_build_qualified_type (t
, quals
);
259 /* Return the common type for two arithmetic types T1 and T2 under the
260 usual arithmetic conversions. The default conversions have already
261 been applied, and enumerated types converted to their compatible
265 cp_common_type (tree t1
, tree t2
)
267 enum tree_code code1
= TREE_CODE (t1
);
268 enum tree_code code2
= TREE_CODE (t2
);
272 /* In what follows, we slightly generalize the rules given in [expr] so
273 as to deal with `long long' and `complex'. First, merge the
275 attributes
= (*targetm
.merge_type_attributes
) (t1
, t2
);
277 if (SCOPED_ENUM_P (t1
) || SCOPED_ENUM_P (t2
))
279 if (TYPE_MAIN_VARIANT (t1
) == TYPE_MAIN_VARIANT (t2
))
280 return build_type_attribute_variant (t1
, attributes
);
285 /* FIXME: Attributes. */
286 gcc_assert (ARITHMETIC_TYPE_P (t1
)
287 || TREE_CODE (t1
) == VECTOR_TYPE
288 || UNSCOPED_ENUM_P (t1
));
289 gcc_assert (ARITHMETIC_TYPE_P (t2
)
290 || TREE_CODE (t2
) == VECTOR_TYPE
291 || UNSCOPED_ENUM_P (t2
));
293 /* If one type is complex, form the common type of the non-complex
294 components, then make that complex. Use T1 or T2 if it is the
296 if (code1
== COMPLEX_TYPE
|| code2
== COMPLEX_TYPE
)
298 tree subtype1
= code1
== COMPLEX_TYPE
? TREE_TYPE (t1
) : t1
;
299 tree subtype2
= code2
== COMPLEX_TYPE
? TREE_TYPE (t2
) : t2
;
301 = type_after_usual_arithmetic_conversions (subtype1
, subtype2
);
303 if (code1
== COMPLEX_TYPE
&& TREE_TYPE (t1
) == subtype
)
304 return build_type_attribute_variant (t1
, attributes
);
305 else if (code2
== COMPLEX_TYPE
&& TREE_TYPE (t2
) == subtype
)
306 return build_type_attribute_variant (t2
, attributes
);
308 return build_type_attribute_variant (build_complex_type (subtype
),
312 if (code1
== VECTOR_TYPE
)
314 /* When we get here we should have two vectors of the same size.
315 Just prefer the unsigned one if present. */
316 if (TYPE_UNSIGNED (t1
))
317 return build_type_attribute_variant (t1
, attributes
);
319 return build_type_attribute_variant (t2
, attributes
);
322 /* If only one is real, use it as the result. */
323 if (code1
== REAL_TYPE
&& code2
!= REAL_TYPE
)
324 return build_type_attribute_variant (t1
, attributes
);
325 if (code2
== REAL_TYPE
&& code1
!= REAL_TYPE
)
326 return build_type_attribute_variant (t2
, attributes
);
328 /* Both real or both integers; use the one with greater precision. */
329 if (TYPE_PRECISION (t1
) > TYPE_PRECISION (t2
))
330 return build_type_attribute_variant (t1
, attributes
);
331 else if (TYPE_PRECISION (t2
) > TYPE_PRECISION (t1
))
332 return build_type_attribute_variant (t2
, attributes
);
334 /* The types are the same; no need to do anything fancy. */
335 if (TYPE_MAIN_VARIANT (t1
) == TYPE_MAIN_VARIANT (t2
))
336 return build_type_attribute_variant (t1
, attributes
);
338 if (code1
!= REAL_TYPE
)
340 /* If one is unsigned long long, then convert the other to unsigned
342 if (same_type_p (TYPE_MAIN_VARIANT (t1
), long_long_unsigned_type_node
)
343 || same_type_p (TYPE_MAIN_VARIANT (t2
), long_long_unsigned_type_node
))
344 return build_type_attribute_variant (long_long_unsigned_type_node
,
346 /* If one is a long long, and the other is an unsigned long, and
347 long long can represent all the values of an unsigned long, then
348 convert to a long long. Otherwise, convert to an unsigned long
349 long. Otherwise, if either operand is long long, convert the
352 Since we're here, we know the TYPE_PRECISION is the same;
353 therefore converting to long long cannot represent all the values
354 of an unsigned long, so we choose unsigned long long in that
356 if (same_type_p (TYPE_MAIN_VARIANT (t1
), long_long_integer_type_node
)
357 || same_type_p (TYPE_MAIN_VARIANT (t2
), long_long_integer_type_node
))
359 tree t
= ((TYPE_UNSIGNED (t1
) || TYPE_UNSIGNED (t2
))
360 ? long_long_unsigned_type_node
361 : long_long_integer_type_node
);
362 return build_type_attribute_variant (t
, attributes
);
364 if (int128_integer_type_node
!= NULL_TREE
365 && (same_type_p (TYPE_MAIN_VARIANT (t1
),
366 int128_integer_type_node
)
367 || same_type_p (TYPE_MAIN_VARIANT (t2
),
368 int128_integer_type_node
)))
370 tree t
= ((TYPE_UNSIGNED (t1
) || TYPE_UNSIGNED (t2
))
371 ? int128_unsigned_type_node
372 : int128_integer_type_node
);
373 return build_type_attribute_variant (t
, attributes
);
376 /* Go through the same procedure, but for longs. */
377 if (same_type_p (TYPE_MAIN_VARIANT (t1
), long_unsigned_type_node
)
378 || same_type_p (TYPE_MAIN_VARIANT (t2
), long_unsigned_type_node
))
379 return build_type_attribute_variant (long_unsigned_type_node
,
381 if (same_type_p (TYPE_MAIN_VARIANT (t1
), long_integer_type_node
)
382 || same_type_p (TYPE_MAIN_VARIANT (t2
), long_integer_type_node
))
384 tree t
= ((TYPE_UNSIGNED (t1
) || TYPE_UNSIGNED (t2
))
385 ? long_unsigned_type_node
: long_integer_type_node
);
386 return build_type_attribute_variant (t
, attributes
);
388 /* Otherwise prefer the unsigned one. */
389 if (TYPE_UNSIGNED (t1
))
390 return build_type_attribute_variant (t1
, attributes
);
392 return build_type_attribute_variant (t2
, attributes
);
396 if (same_type_p (TYPE_MAIN_VARIANT (t1
), long_double_type_node
)
397 || same_type_p (TYPE_MAIN_VARIANT (t2
), long_double_type_node
))
398 return build_type_attribute_variant (long_double_type_node
,
400 if (same_type_p (TYPE_MAIN_VARIANT (t1
), double_type_node
)
401 || same_type_p (TYPE_MAIN_VARIANT (t2
), double_type_node
))
402 return build_type_attribute_variant (double_type_node
,
404 if (same_type_p (TYPE_MAIN_VARIANT (t1
), float_type_node
)
405 || same_type_p (TYPE_MAIN_VARIANT (t2
), float_type_node
))
406 return build_type_attribute_variant (float_type_node
,
409 /* Two floating-point types whose TYPE_MAIN_VARIANTs are none of
410 the standard C++ floating-point types. Logic earlier in this
411 function has already eliminated the possibility that
412 TYPE_PRECISION (t2) != TYPE_PRECISION (t1), so there's no
413 compelling reason to choose one or the other. */
414 return build_type_attribute_variant (t1
, attributes
);
418 /* T1 and T2 are arithmetic or enumeration types. Return the type
419 that will result from the "usual arithmetic conversions" on T1 and
420 T2 as described in [expr]. */
423 type_after_usual_arithmetic_conversions (tree t1
, tree t2
)
425 gcc_assert (ARITHMETIC_TYPE_P (t1
)
426 || TREE_CODE (t1
) == VECTOR_TYPE
427 || UNSCOPED_ENUM_P (t1
));
428 gcc_assert (ARITHMETIC_TYPE_P (t2
)
429 || TREE_CODE (t2
) == VECTOR_TYPE
430 || UNSCOPED_ENUM_P (t2
));
432 /* Perform the integral promotions. We do not promote real types here. */
433 if (INTEGRAL_OR_ENUMERATION_TYPE_P (t1
)
434 && INTEGRAL_OR_ENUMERATION_TYPE_P (t2
))
436 t1
= type_promotes_to (t1
);
437 t2
= type_promotes_to (t2
);
440 return cp_common_type (t1
, t2
);
444 composite_pointer_error (diagnostic_t kind
, tree t1
, tree t2
,
445 composite_pointer_operation operation
)
450 emit_diagnostic (kind
, input_location
, 0,
451 "comparison between "
452 "distinct pointer types %qT and %qT lacks a cast",
456 emit_diagnostic (kind
, input_location
, 0,
457 "conversion between "
458 "distinct pointer types %qT and %qT lacks a cast",
461 case CPO_CONDITIONAL_EXPR
:
462 emit_diagnostic (kind
, input_location
, 0,
463 "conditional expression between "
464 "distinct pointer types %qT and %qT lacks a cast",
472 /* Subroutine of composite_pointer_type to implement the recursive
473 case. See that function for documentation of the parameters. */
476 composite_pointer_type_r (tree t1
, tree t2
,
477 composite_pointer_operation operation
,
478 tsubst_flags_t complain
)
485 /* Determine the types pointed to by T1 and T2. */
488 pointee1
= TREE_TYPE (t1
);
489 pointee2
= TREE_TYPE (t2
);
493 pointee1
= TYPE_PTRMEM_POINTED_TO_TYPE (t1
);
494 pointee2
= TYPE_PTRMEM_POINTED_TO_TYPE (t2
);
499 Otherwise, the composite pointer type is a pointer type
500 similar (_conv.qual_) to the type of one of the operands,
501 with a cv-qualification signature (_conv.qual_) that is the
502 union of the cv-qualification signatures of the operand
504 if (same_type_ignoring_top_level_qualifiers_p (pointee1
, pointee2
))
505 result_type
= pointee1
;
506 else if ((TYPE_PTR_P (pointee1
) && TYPE_PTR_P (pointee2
))
507 || (TYPE_PTRMEM_P (pointee1
) && TYPE_PTRMEM_P (pointee2
)))
509 result_type
= composite_pointer_type_r (pointee1
, pointee2
, operation
,
511 if (result_type
== error_mark_node
)
512 return error_mark_node
;
516 if (complain
& tf_error
)
517 composite_pointer_error (DK_PERMERROR
, t1
, t2
, operation
);
519 return error_mark_node
;
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_PTRMEM_P (t1
))
529 if (!same_type_p (TYPE_PTRMEM_CLASS_TYPE (t1
),
530 TYPE_PTRMEM_CLASS_TYPE (t2
)))
532 if (complain
& tf_error
)
533 composite_pointer_error (DK_PERMERROR
, t1
, t2
, operation
);
535 return error_mark_node
;
537 result_type
= build_ptrmem_type (TYPE_PTRMEM_CLASS_TYPE (t1
),
541 result_type
= build_pointer_type (result_type
);
543 /* Merge the attributes. */
544 attributes
= (*targetm
.merge_type_attributes
) (t1
, t2
);
545 return build_type_attribute_variant (result_type
, attributes
);
548 /* Return the composite pointer type (see [expr.rel]) for T1 and T2.
549 ARG1 and ARG2 are the values with those types. The OPERATION is to
550 describe the operation between the pointer types,
551 in case an error occurs.
553 This routine also implements the computation of a common type for
554 pointers-to-members as per [expr.eq]. */
557 composite_pointer_type (tree t1
, tree t2
, tree arg1
, tree arg2
,
558 composite_pointer_operation operation
,
559 tsubst_flags_t complain
)
566 If one operand is a null pointer constant, the composite pointer
567 type is the type of the other operand. */
568 if (null_ptr_cst_p (arg1
))
570 if (null_ptr_cst_p (arg2
))
577 If one of the operands has type "pointer to cv1 void*", then
578 the other has type "pointer to cv2T", and the composite pointer
579 type is "pointer to cv12 void", where cv12 is the union of cv1
582 If either type is a pointer to void, make sure it is T1. */
583 if (TYPE_PTR_P (t2
) && VOID_TYPE_P (TREE_TYPE (t2
)))
591 /* Now, if T1 is a pointer to void, merge the qualifiers. */
592 if (TYPE_PTR_P (t1
) && VOID_TYPE_P (TREE_TYPE (t1
)))
597 if (TYPE_PTRFN_P (t2
) && (complain
& tf_error
))
602 pedwarn (input_location
, OPT_Wpedantic
,
603 "ISO C++ forbids comparison between "
604 "pointer of type %<void *%> and pointer-to-function");
607 pedwarn (input_location
, OPT_Wpedantic
,
608 "ISO C++ forbids conversion between "
609 "pointer of type %<void *%> and pointer-to-function");
611 case CPO_CONDITIONAL_EXPR
:
612 pedwarn (input_location
, OPT_Wpedantic
,
613 "ISO C++ forbids conditional expression between "
614 "pointer of type %<void *%> and pointer-to-function");
621 = cp_build_qualified_type (void_type_node
,
622 (cp_type_quals (TREE_TYPE (t1
))
623 | cp_type_quals (TREE_TYPE (t2
))));
624 result_type
= build_pointer_type (result_type
);
625 /* Merge the attributes. */
626 attributes
= (*targetm
.merge_type_attributes
) (t1
, t2
);
627 return build_type_attribute_variant (result_type
, attributes
);
630 if (c_dialect_objc () && TYPE_PTR_P (t1
)
633 if (objc_have_common_type (t1
, t2
, -3, NULL_TREE
))
634 return objc_common_type (t1
, t2
);
637 /* [expr.eq] permits the application of a pointer conversion to
638 bring the pointers to a common type. */
639 if (TYPE_PTR_P (t1
) && TYPE_PTR_P (t2
)
640 && CLASS_TYPE_P (TREE_TYPE (t1
))
641 && CLASS_TYPE_P (TREE_TYPE (t2
))
642 && !same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (t1
),
645 class1
= TREE_TYPE (t1
);
646 class2
= TREE_TYPE (t2
);
648 if (DERIVED_FROM_P (class1
, class2
))
649 t2
= (build_pointer_type
650 (cp_build_qualified_type (class1
, cp_type_quals (class2
))));
651 else if (DERIVED_FROM_P (class2
, class1
))
652 t1
= (build_pointer_type
653 (cp_build_qualified_type (class2
, cp_type_quals (class1
))));
656 if (complain
& tf_error
)
657 composite_pointer_error (DK_ERROR
, t1
, t2
, operation
);
658 return error_mark_node
;
661 /* [expr.eq] permits the application of a pointer-to-member
662 conversion to change the class type of one of the types. */
663 else if (TYPE_PTRMEM_P (t1
)
664 && !same_type_p (TYPE_PTRMEM_CLASS_TYPE (t1
),
665 TYPE_PTRMEM_CLASS_TYPE (t2
)))
667 class1
= TYPE_PTRMEM_CLASS_TYPE (t1
);
668 class2
= TYPE_PTRMEM_CLASS_TYPE (t2
);
670 if (DERIVED_FROM_P (class1
, class2
))
671 t1
= build_ptrmem_type (class2
, TYPE_PTRMEM_POINTED_TO_TYPE (t1
));
672 else if (DERIVED_FROM_P (class2
, class1
))
673 t2
= build_ptrmem_type (class1
, TYPE_PTRMEM_POINTED_TO_TYPE (t2
));
676 if (complain
& tf_error
)
680 error ("comparison between distinct "
681 "pointer-to-member types %qT and %qT lacks a cast",
685 error ("conversion between distinct "
686 "pointer-to-member types %qT and %qT lacks a cast",
689 case CPO_CONDITIONAL_EXPR
:
690 error ("conditional expression between distinct "
691 "pointer-to-member types %qT and %qT lacks a cast",
697 return error_mark_node
;
701 return composite_pointer_type_r (t1
, t2
, operation
, complain
);
704 /* Return the merged type of two types.
705 We assume that comptypes has already been done and returned 1;
706 if that isn't so, this may crash.
708 This just combines attributes and default arguments; any other
709 differences would cause the two types to compare unalike. */
712 merge_types (tree t1
, tree t2
)
714 enum tree_code code1
;
715 enum tree_code code2
;
718 /* Save time if the two types are the same. */
721 if (original_type (t1
) == original_type (t2
))
724 /* If one type is nonsense, use the other. */
725 if (t1
== error_mark_node
)
727 if (t2
== error_mark_node
)
730 /* Handle merging an auto redeclaration with a previous deduced
735 /* Merge the attributes. */
736 attributes
= (*targetm
.merge_type_attributes
) (t1
, t2
);
738 if (TYPE_PTRMEMFUNC_P (t1
))
739 t1
= TYPE_PTRMEMFUNC_FN_TYPE (t1
);
740 if (TYPE_PTRMEMFUNC_P (t2
))
741 t2
= TYPE_PTRMEMFUNC_FN_TYPE (t2
);
743 code1
= TREE_CODE (t1
);
744 code2
= TREE_CODE (t2
);
747 gcc_assert (code1
== TYPENAME_TYPE
|| code2
== TYPENAME_TYPE
);
748 if (code1
== TYPENAME_TYPE
)
750 t1
= resolve_typename_type (t1
, /*only_current_p=*/true);
751 code1
= TREE_CODE (t1
);
755 t2
= resolve_typename_type (t2
, /*only_current_p=*/true);
756 code2
= TREE_CODE (t2
);
764 /* For two pointers, do this recursively on the target type. */
766 tree target
= merge_types (TREE_TYPE (t1
), TREE_TYPE (t2
));
767 int quals
= cp_type_quals (t1
);
769 if (code1
== POINTER_TYPE
)
770 t1
= build_pointer_type (target
);
772 t1
= cp_build_reference_type (target
, TYPE_REF_IS_RVALUE (t1
));
773 t1
= build_type_attribute_variant (t1
, attributes
);
774 t1
= cp_build_qualified_type (t1
, quals
);
776 if (TREE_CODE (target
) == METHOD_TYPE
)
777 t1
= build_ptrmemfunc_type (t1
);
786 quals
= cp_type_quals (t1
);
787 pointee
= merge_types (TYPE_PTRMEM_POINTED_TO_TYPE (t1
),
788 TYPE_PTRMEM_POINTED_TO_TYPE (t2
));
789 t1
= build_ptrmem_type (TYPE_PTRMEM_CLASS_TYPE (t1
),
791 t1
= cp_build_qualified_type (t1
, quals
);
797 tree elt
= merge_types (TREE_TYPE (t1
), TREE_TYPE (t2
));
798 /* Save space: see if the result is identical to one of the args. */
799 if (elt
== TREE_TYPE (t1
) && TYPE_DOMAIN (t1
))
800 return build_type_attribute_variant (t1
, attributes
);
801 if (elt
== TREE_TYPE (t2
) && TYPE_DOMAIN (t2
))
802 return build_type_attribute_variant (t2
, attributes
);
803 /* Merge the element types, and have a size if either arg has one. */
804 t1
= build_cplus_array_type
805 (elt
, TYPE_DOMAIN (TYPE_DOMAIN (t1
) ? t1
: t2
));
810 /* Function types: prefer the one that specified arg types.
811 If both do, merge the arg types. Also merge the return types. */
813 tree valtype
= merge_types (TREE_TYPE (t1
), TREE_TYPE (t2
));
814 tree p1
= TYPE_ARG_TYPES (t1
);
815 tree p2
= TYPE_ARG_TYPES (t2
);
819 /* Save space: see if the result is identical to one of the args. */
820 if (valtype
== TREE_TYPE (t1
) && ! p2
)
821 return cp_build_type_attribute_variant (t1
, attributes
);
822 if (valtype
== TREE_TYPE (t2
) && ! p1
)
823 return cp_build_type_attribute_variant (t2
, attributes
);
825 /* Simple way if one arg fails to specify argument types. */
826 if (p1
== NULL_TREE
|| TREE_VALUE (p1
) == void_type_node
)
828 else if (p2
== NULL_TREE
|| TREE_VALUE (p2
) == void_type_node
)
831 parms
= commonparms (p1
, p2
);
833 rval
= build_function_type (valtype
, parms
);
834 gcc_assert (type_memfn_quals (t1
) == type_memfn_quals (t2
));
835 gcc_assert (type_memfn_rqual (t1
) == type_memfn_rqual (t2
));
836 rval
= apply_memfn_quals (rval
,
837 type_memfn_quals (t1
),
838 type_memfn_rqual (t1
));
839 raises
= merge_exception_specifiers (TYPE_RAISES_EXCEPTIONS (t1
),
840 TYPE_RAISES_EXCEPTIONS (t2
),
842 t1
= build_exception_variant (rval
, raises
);
848 /* Get this value the long way, since TYPE_METHOD_BASETYPE
849 is just the main variant of this. */
850 tree basetype
= class_of_this_parm (t2
);
851 tree raises
= merge_exception_specifiers (TYPE_RAISES_EXCEPTIONS (t1
),
852 TYPE_RAISES_EXCEPTIONS (t2
),
854 cp_ref_qualifier rqual
= type_memfn_rqual (t1
);
857 /* If this was a member function type, get back to the
858 original type of type member function (i.e., without
859 the class instance variable up front. */
860 t1
= build_function_type (TREE_TYPE (t1
),
861 TREE_CHAIN (TYPE_ARG_TYPES (t1
)));
862 t2
= build_function_type (TREE_TYPE (t2
),
863 TREE_CHAIN (TYPE_ARG_TYPES (t2
)));
864 t3
= merge_types (t1
, t2
);
865 t3
= build_method_type_directly (basetype
, TREE_TYPE (t3
),
866 TYPE_ARG_TYPES (t3
));
867 t1
= build_exception_variant (t3
, raises
);
868 t1
= build_ref_qualified_type (t1
, rqual
);
873 /* There is no need to merge attributes into a TYPENAME_TYPE.
874 When the type is instantiated it will have whatever
875 attributes result from the instantiation. */
881 if (attribute_list_equal (TYPE_ATTRIBUTES (t1
), attributes
))
883 else if (attribute_list_equal (TYPE_ATTRIBUTES (t2
), attributes
))
886 return cp_build_type_attribute_variant (t1
, attributes
);
889 /* Return the ARRAY_TYPE type without its domain. */
892 strip_array_domain (tree type
)
895 gcc_assert (TREE_CODE (type
) == ARRAY_TYPE
);
896 if (TYPE_DOMAIN (type
) == NULL_TREE
)
898 t2
= build_cplus_array_type (TREE_TYPE (type
), NULL_TREE
);
899 return cp_build_type_attribute_variant (t2
, TYPE_ATTRIBUTES (type
));
902 /* Wrapper around cp_common_type that is used by c-common.c and other
903 front end optimizations that remove promotions.
905 Return the common type for two arithmetic types T1 and T2 under the
906 usual arithmetic conversions. The default conversions have already
907 been applied, and enumerated types converted to their compatible
911 common_type (tree t1
, tree t2
)
913 /* If one type is nonsense, use the other */
914 if (t1
== error_mark_node
)
916 if (t2
== error_mark_node
)
919 return cp_common_type (t1
, t2
);
922 /* Return the common type of two pointer types T1 and T2. This is the
923 type for the result of most arithmetic operations if the operands
924 have the given two types.
926 We assume that comp_target_types has already been done and returned
927 nonzero; if that isn't so, this may crash. */
930 common_pointer_type (tree t1
, tree t2
)
932 gcc_assert ((TYPE_PTR_P (t1
) && TYPE_PTR_P (t2
))
933 || (TYPE_PTRDATAMEM_P (t1
) && TYPE_PTRDATAMEM_P (t2
))
934 || (TYPE_PTRMEMFUNC_P (t1
) && TYPE_PTRMEMFUNC_P (t2
)));
936 return composite_pointer_type (t1
, t2
, error_mark_node
, error_mark_node
,
937 CPO_CONVERSION
, tf_warning_or_error
);
940 /* Compare two exception specifier types for exactness or subsetness, if
941 allowed. Returns false for mismatch, true for match (same, or
944 [except.spec] "If a class X ... objects of class X or any class publicly
945 and unambiguously derived from X. Similarly, if a pointer type Y * ...
946 exceptions of type Y * or that are pointers to any type publicly and
947 unambiguously derived from Y. Otherwise a function only allows exceptions
948 that have the same type ..."
949 This does not mention cv qualifiers and is different to what throw
950 [except.throw] and catch [except.catch] will do. They will ignore the
951 top level cv qualifiers, and allow qualifiers in the pointer to class
954 We implement the letter of the standard. */
957 comp_except_types (tree a
, tree b
, bool exact
)
959 if (same_type_p (a
, b
))
963 if (cp_type_quals (a
) || cp_type_quals (b
))
966 if (TYPE_PTR_P (a
) && TYPE_PTR_P (b
))
970 if (cp_type_quals (a
) || cp_type_quals (b
))
974 if (TREE_CODE (a
) != RECORD_TYPE
975 || TREE_CODE (b
) != RECORD_TYPE
)
978 if (publicly_uniquely_derived_p (a
, b
))
984 /* Return true if TYPE1 and TYPE2 are equivalent exception specifiers.
985 If EXACT is ce_derived, T2 can be stricter than T1 (according to 15.4/5).
986 If EXACT is ce_normal, the compatibility rules in 15.4/3 apply.
987 If EXACT is ce_exact, the specs must be exactly the same. Exception lists
988 are unordered, but we've already filtered out duplicates. Most lists will
989 be in order, we should try to make use of that. */
992 comp_except_specs (const_tree t1
, const_tree t2
, int exact
)
1001 /* First handle noexcept. */
1002 if (exact
< ce_exact
)
1004 /* noexcept(false) is compatible with no exception-specification,
1005 and stricter than any spec. */
1006 if (t1
== noexcept_false_spec
)
1007 return t2
== NULL_TREE
|| exact
== ce_derived
;
1008 /* Even a derived noexcept(false) is compatible with no
1009 exception-specification. */
1010 if (t2
== noexcept_false_spec
)
1011 return t1
== NULL_TREE
;
1013 /* Otherwise, if we aren't looking for an exact match, noexcept is
1014 equivalent to throw(). */
1015 if (t1
== noexcept_true_spec
)
1016 t1
= empty_except_spec
;
1017 if (t2
== noexcept_true_spec
)
1018 t2
= empty_except_spec
;
1021 /* If any noexcept is left, it is only comparable to itself;
1022 either we're looking for an exact match or we're redeclaring a
1023 template with dependent noexcept. */
1024 if ((t1
&& TREE_PURPOSE (t1
))
1025 || (t2
&& TREE_PURPOSE (t2
)))
1027 && cp_tree_equal (TREE_PURPOSE (t1
), TREE_PURPOSE (t2
)));
1029 if (t1
== NULL_TREE
) /* T1 is ... */
1030 return t2
== NULL_TREE
|| exact
== ce_derived
;
1031 if (!TREE_VALUE (t1
)) /* t1 is EMPTY */
1032 return t2
!= NULL_TREE
&& !TREE_VALUE (t2
);
1033 if (t2
== NULL_TREE
) /* T2 is ... */
1035 if (TREE_VALUE (t1
) && !TREE_VALUE (t2
)) /* T2 is EMPTY, T1 is not */
1036 return exact
== ce_derived
;
1038 /* Neither set is ... or EMPTY, make sure each part of T2 is in T1.
1039 Count how many we find, to determine exactness. For exact matching and
1040 ordered T1, T2, this is an O(n) operation, otherwise its worst case is
1042 for (base
= t1
; t2
!= NULL_TREE
; t2
= TREE_CHAIN (t2
))
1044 for (probe
= base
; probe
!= NULL_TREE
; probe
= TREE_CHAIN (probe
))
1046 tree a
= TREE_VALUE (probe
);
1047 tree b
= TREE_VALUE (t2
);
1049 if (comp_except_types (a
, b
, exact
))
1051 if (probe
== base
&& exact
> ce_derived
)
1052 base
= TREE_CHAIN (probe
);
1057 if (probe
== NULL_TREE
)
1060 return exact
== ce_derived
|| base
== NULL_TREE
|| length
== list_length (t1
);
1063 /* Compare the array types T1 and T2. ALLOW_REDECLARATION is true if
1064 [] can match [size]. */
1067 comp_array_types (const_tree t1
, const_tree t2
, bool allow_redeclaration
)
1076 /* The type of the array elements must be the same. */
1077 if (!same_type_p (TREE_TYPE (t1
), TREE_TYPE (t2
)))
1080 d1
= TYPE_DOMAIN (t1
);
1081 d2
= TYPE_DOMAIN (t2
);
1086 /* If one of the arrays is dimensionless, and the other has a
1087 dimension, they are of different types. However, it is valid to
1095 declarations for an array object can specify
1096 array types that differ by the presence or absence of a major
1097 array bound (_dcl.array_). */
1099 return allow_redeclaration
;
1101 /* Check that the dimensions are the same. */
1103 if (!cp_tree_equal (TYPE_MIN_VALUE (d1
), TYPE_MIN_VALUE (d2
)))
1105 max1
= TYPE_MAX_VALUE (d1
);
1106 max2
= TYPE_MAX_VALUE (d2
);
1107 if (processing_template_decl
&& !abi_version_at_least (2)
1108 && !value_dependent_expression_p (max1
)
1109 && !value_dependent_expression_p (max2
))
1111 /* With abi-1 we do not fold non-dependent array bounds, (and
1112 consequently mangle them incorrectly). We must therefore
1113 fold them here, to verify the domains have the same
1119 if (!cp_tree_equal (max1
, max2
))
1125 /* Compare the relative position of T1 and T2 into their respective
1126 template parameter list.
1127 T1 and T2 must be template parameter types.
1128 Return TRUE if T1 and T2 have the same position, FALSE otherwise. */
1131 comp_template_parms_position (tree t1
, tree t2
)
1133 tree index1
, index2
;
1134 gcc_assert (t1
&& t2
1135 && TREE_CODE (t1
) == TREE_CODE (t2
)
1136 && (TREE_CODE (t1
) == BOUND_TEMPLATE_TEMPLATE_PARM
1137 || TREE_CODE (t1
) == TEMPLATE_TEMPLATE_PARM
1138 || TREE_CODE (t1
) == TEMPLATE_TYPE_PARM
));
1140 index1
= TEMPLATE_TYPE_PARM_INDEX (TYPE_MAIN_VARIANT (t1
));
1141 index2
= TEMPLATE_TYPE_PARM_INDEX (TYPE_MAIN_VARIANT (t2
));
1143 /* Then compare their relative position. */
1144 if (TEMPLATE_PARM_IDX (index1
) != TEMPLATE_PARM_IDX (index2
)
1145 || TEMPLATE_PARM_LEVEL (index1
) != TEMPLATE_PARM_LEVEL (index2
)
1146 || (TEMPLATE_PARM_PARAMETER_PACK (index1
)
1147 != TEMPLATE_PARM_PARAMETER_PACK (index2
)))
1153 /* Subroutine in comptypes. */
1156 structural_comptypes (tree t1
, tree t2
, int strict
)
1161 /* Suppress errors caused by previously reported errors. */
1162 if (t1
== error_mark_node
|| t2
== error_mark_node
)
1165 gcc_assert (TYPE_P (t1
) && TYPE_P (t2
));
1167 /* TYPENAME_TYPEs should be resolved if the qualifying scope is the
1168 current instantiation. */
1169 if (TREE_CODE (t1
) == TYPENAME_TYPE
)
1170 t1
= resolve_typename_type (t1
, /*only_current_p=*/true);
1172 if (TREE_CODE (t2
) == TYPENAME_TYPE
)
1173 t2
= resolve_typename_type (t2
, /*only_current_p=*/true);
1175 if (TYPE_PTRMEMFUNC_P (t1
))
1176 t1
= TYPE_PTRMEMFUNC_FN_TYPE (t1
);
1177 if (TYPE_PTRMEMFUNC_P (t2
))
1178 t2
= TYPE_PTRMEMFUNC_FN_TYPE (t2
);
1180 /* Different classes of types can't be compatible. */
1181 if (TREE_CODE (t1
) != TREE_CODE (t2
))
1184 /* Qualifiers must match. For array types, we will check when we
1185 recur on the array element types. */
1186 if (TREE_CODE (t1
) != ARRAY_TYPE
1187 && cp_type_quals (t1
) != cp_type_quals (t2
))
1189 if (TREE_CODE (t1
) == FUNCTION_TYPE
1190 && type_memfn_quals (t1
) != type_memfn_quals (t2
))
1192 /* Need to check this before TYPE_MAIN_VARIANT.
1193 FIXME function qualifiers should really change the main variant. */
1194 if ((TREE_CODE (t1
) == FUNCTION_TYPE
1195 || TREE_CODE (t1
) == METHOD_TYPE
)
1196 && type_memfn_rqual (t1
) != type_memfn_rqual (t2
))
1198 if (TYPE_FOR_JAVA (t1
) != TYPE_FOR_JAVA (t2
))
1201 /* Allow for two different type nodes which have essentially the same
1202 definition. Note that we already checked for equality of the type
1203 qualifiers (just above). */
1205 if (TREE_CODE (t1
) != ARRAY_TYPE
1206 && TYPE_MAIN_VARIANT (t1
) == TYPE_MAIN_VARIANT (t2
))
1210 /* Compare the types. Break out if they could be the same. */
1211 switch (TREE_CODE (t1
))
1215 /* All void and bool types are the same. */
1219 case FIXED_POINT_TYPE
:
1221 /* With these nodes, we can't determine type equivalence by
1222 looking at what is stored in the nodes themselves, because
1223 two nodes might have different TYPE_MAIN_VARIANTs but still
1224 represent the same type. For example, wchar_t and int could
1225 have the same properties (TYPE_PRECISION, TYPE_MIN_VALUE,
1226 TYPE_MAX_VALUE, etc.), but have different TYPE_MAIN_VARIANTs
1227 and are distinct types. On the other hand, int and the
1230 typedef int INT __attribute((may_alias));
1232 have identical properties, different TYPE_MAIN_VARIANTs, but
1233 represent the same type. The canonical type system keeps
1234 track of equivalence in this case, so we fall back on it. */
1235 return TYPE_CANONICAL (t1
) == TYPE_CANONICAL (t2
);
1237 case TEMPLATE_TEMPLATE_PARM
:
1238 case BOUND_TEMPLATE_TEMPLATE_PARM
:
1239 if (!comp_template_parms_position (t1
, t2
))
1241 if (!comp_template_parms
1242 (DECL_TEMPLATE_PARMS (TEMPLATE_TEMPLATE_PARM_TEMPLATE_DECL (t1
)),
1243 DECL_TEMPLATE_PARMS (TEMPLATE_TEMPLATE_PARM_TEMPLATE_DECL (t2
))))
1245 if (TREE_CODE (t1
) == TEMPLATE_TEMPLATE_PARM
)
1247 /* Don't check inheritance. */
1248 strict
= COMPARE_STRICT
;
1253 if (TYPE_TEMPLATE_INFO (t1
) && TYPE_TEMPLATE_INFO (t2
)
1254 && (TYPE_TI_TEMPLATE (t1
) == TYPE_TI_TEMPLATE (t2
)
1255 || TREE_CODE (t1
) == BOUND_TEMPLATE_TEMPLATE_PARM
)
1256 && comp_template_args (TYPE_TI_ARGS (t1
), TYPE_TI_ARGS (t2
)))
1259 if ((strict
& COMPARE_BASE
) && DERIVED_FROM_P (t1
, t2
))
1261 else if ((strict
& COMPARE_DERIVED
) && DERIVED_FROM_P (t2
, t1
))
1267 if (!comptypes (TYPE_OFFSET_BASETYPE (t1
), TYPE_OFFSET_BASETYPE (t2
),
1268 strict
& ~COMPARE_REDECLARATION
))
1270 if (!same_type_p (TREE_TYPE (t1
), TREE_TYPE (t2
)))
1274 case REFERENCE_TYPE
:
1275 if (TYPE_REF_IS_RVALUE (t1
) != TYPE_REF_IS_RVALUE (t2
))
1277 /* fall through to checks for pointer types */
1280 if (TYPE_MODE (t1
) != TYPE_MODE (t2
)
1281 || TYPE_REF_CAN_ALIAS_ALL (t1
) != TYPE_REF_CAN_ALIAS_ALL (t2
)
1282 || !same_type_p (TREE_TYPE (t1
), TREE_TYPE (t2
)))
1288 if (!same_type_p (TREE_TYPE (t1
), TREE_TYPE (t2
)))
1290 if (!compparms (TYPE_ARG_TYPES (t1
), TYPE_ARG_TYPES (t2
)))
1295 /* Target types must match incl. qualifiers. */
1296 if (!comp_array_types (t1
, t2
, !!(strict
& COMPARE_REDECLARATION
)))
1300 case TEMPLATE_TYPE_PARM
:
1301 /* If T1 and T2 don't have the same relative position in their
1302 template parameters set, they can't be equal. */
1303 if (!comp_template_parms_position (t1
, t2
))
1308 if (!cp_tree_equal (TYPENAME_TYPE_FULLNAME (t1
),
1309 TYPENAME_TYPE_FULLNAME (t2
)))
1311 /* Qualifiers don't matter on scopes. */
1312 if (!same_type_ignoring_top_level_qualifiers_p (TYPE_CONTEXT (t1
),
1317 case UNBOUND_CLASS_TEMPLATE
:
1318 if (!cp_tree_equal (TYPE_IDENTIFIER (t1
), TYPE_IDENTIFIER (t2
)))
1320 if (!same_type_p (TYPE_CONTEXT (t1
), TYPE_CONTEXT (t2
)))
1325 if (!same_type_p (TREE_TYPE (t1
), TREE_TYPE (t2
)))
1330 if (TYPE_VECTOR_SUBPARTS (t1
) != TYPE_VECTOR_SUBPARTS (t2
)
1331 || !same_type_p (TREE_TYPE (t1
), TREE_TYPE (t2
)))
1335 case TYPE_PACK_EXPANSION
:
1336 return (same_type_p (PACK_EXPANSION_PATTERN (t1
),
1337 PACK_EXPANSION_PATTERN (t2
))
1338 && comp_template_args (PACK_EXPANSION_EXTRA_ARGS (t1
),
1339 PACK_EXPANSION_EXTRA_ARGS (t2
)));
1342 if (DECLTYPE_TYPE_ID_EXPR_OR_MEMBER_ACCESS_P (t1
)
1343 != DECLTYPE_TYPE_ID_EXPR_OR_MEMBER_ACCESS_P (t2
)
1344 || (DECLTYPE_FOR_LAMBDA_CAPTURE (t1
)
1345 != DECLTYPE_FOR_LAMBDA_CAPTURE (t2
))
1346 || (DECLTYPE_FOR_LAMBDA_PROXY (t1
)
1347 != DECLTYPE_FOR_LAMBDA_PROXY (t2
))
1348 || !cp_tree_equal (DECLTYPE_TYPE_EXPR (t1
),
1349 DECLTYPE_TYPE_EXPR (t2
)))
1353 case UNDERLYING_TYPE
:
1354 return same_type_p (UNDERLYING_TYPE_TYPE (t1
),
1355 UNDERLYING_TYPE_TYPE (t2
));
1361 /* If we get here, we know that from a target independent POV the
1362 types are the same. Make sure the target attributes are also
1364 return comp_type_attributes (t1
, t2
);
1367 /* Return true if T1 and T2 are related as allowed by STRICT. STRICT
1368 is a bitwise-or of the COMPARE_* flags. */
1371 comptypes (tree t1
, tree t2
, int strict
)
1373 if (strict
== COMPARE_STRICT
)
1378 if (t1
== error_mark_node
|| t2
== error_mark_node
)
1381 if (TYPE_STRUCTURAL_EQUALITY_P (t1
) || TYPE_STRUCTURAL_EQUALITY_P (t2
))
1382 /* At least one of the types requires structural equality, so
1383 perform a deep check. */
1384 return structural_comptypes (t1
, t2
, strict
);
1386 #ifdef ENABLE_CHECKING
1387 if (USE_CANONICAL_TYPES
)
1389 bool result
= structural_comptypes (t1
, t2
, strict
);
1391 if (result
&& TYPE_CANONICAL (t1
) != TYPE_CANONICAL (t2
))
1392 /* The two types are structurally equivalent, but their
1393 canonical types were different. This is a failure of the
1394 canonical type propagation code.*/
1396 ("canonical types differ for identical types %T and %T",
1398 else if (!result
&& TYPE_CANONICAL (t1
) == TYPE_CANONICAL (t2
))
1399 /* Two types are structurally different, but the canonical
1400 types are the same. This means we were over-eager in
1401 assigning canonical types. */
1403 ("same canonical type node for different types %T and %T",
1409 if (USE_CANONICAL_TYPES
)
1410 return TYPE_CANONICAL (t1
) == TYPE_CANONICAL (t2
);
1413 return structural_comptypes (t1
, t2
, strict
);
1415 else if (strict
== COMPARE_STRUCTURAL
)
1416 return structural_comptypes (t1
, t2
, COMPARE_STRICT
);
1418 return structural_comptypes (t1
, t2
, strict
);
1421 /* Returns nonzero iff TYPE1 and TYPE2 are the same type, ignoring
1422 top-level qualifiers. */
1425 same_type_ignoring_top_level_qualifiers_p (tree type1
, tree type2
)
1427 if (type1
== error_mark_node
|| type2
== error_mark_node
)
1430 return same_type_p (TYPE_MAIN_VARIANT (type1
), TYPE_MAIN_VARIANT (type2
));
1433 /* Returns 1 if TYPE1 is at least as qualified as TYPE2. */
1436 at_least_as_qualified_p (const_tree type1
, const_tree type2
)
1438 int q1
= cp_type_quals (type1
);
1439 int q2
= cp_type_quals (type2
);
1441 /* All qualifiers for TYPE2 must also appear in TYPE1. */
1442 return (q1
& q2
) == q2
;
1445 /* Returns 1 if TYPE1 is more cv-qualified than TYPE2, -1 if TYPE2 is
1446 more cv-qualified that TYPE1, and 0 otherwise. */
1449 comp_cv_qualification (const_tree type1
, const_tree type2
)
1451 int q1
= cp_type_quals (type1
);
1452 int q2
= cp_type_quals (type2
);
1457 if ((q1
& q2
) == q2
)
1459 else if ((q1
& q2
) == q1
)
1465 /* Returns 1 if the cv-qualification signature of TYPE1 is a proper
1466 subset of the cv-qualification signature of TYPE2, and the types
1467 are similar. Returns -1 if the other way 'round, and 0 otherwise. */
1470 comp_cv_qual_signature (tree type1
, tree type2
)
1472 if (comp_ptr_ttypes_real (type2
, type1
, -1))
1474 else if (comp_ptr_ttypes_real (type1
, type2
, -1))
1480 /* Subroutines of `comptypes'. */
1482 /* Return true if two parameter type lists PARMS1 and PARMS2 are
1483 equivalent in the sense that functions with those parameter types
1484 can have equivalent types. The two lists must be equivalent,
1485 element by element. */
1488 compparms (const_tree parms1
, const_tree parms2
)
1492 /* An unspecified parmlist matches any specified parmlist
1493 whose argument types don't need default promotions. */
1495 for (t1
= parms1
, t2
= parms2
;
1497 t1
= TREE_CHAIN (t1
), t2
= TREE_CHAIN (t2
))
1499 /* If one parmlist is shorter than the other,
1500 they fail to match. */
1503 if (!same_type_p (TREE_VALUE (t1
), TREE_VALUE (t2
)))
1510 /* Process a sizeof or alignof expression where the operand is a
1514 cxx_sizeof_or_alignof_type (tree type
, enum tree_code op
, bool complain
)
1519 gcc_assert (op
== SIZEOF_EXPR
|| op
== ALIGNOF_EXPR
);
1520 if (type
== error_mark_node
)
1521 return error_mark_node
;
1523 type
= non_reference (type
);
1524 if (TREE_CODE (type
) == METHOD_TYPE
)
1527 pedwarn (input_location
, OPT_Wpointer_arith
,
1528 "invalid application of %qs to a member function",
1529 operator_name_info
[(int) op
].name
);
1530 value
= size_one_node
;
1533 dependent_p
= dependent_type_p (type
);
1535 complete_type (type
);
1537 /* VLA types will have a non-constant size. In the body of an
1538 uninstantiated template, we don't need to try to compute the
1539 value, because the sizeof expression is not an integral
1540 constant expression in that case. And, if we do try to
1541 compute the value, we'll likely end up with SAVE_EXPRs, which
1542 the template substitution machinery does not expect to see. */
1543 || (processing_template_decl
1544 && COMPLETE_TYPE_P (type
)
1545 && TREE_CODE (TYPE_SIZE (type
)) != INTEGER_CST
))
1547 value
= build_min (op
, size_type_node
, type
);
1548 TREE_READONLY (value
) = 1;
1552 if (cxx_dialect
>= cxx1y
&& array_of_runtime_bound_p (type
))
1554 if (complain
& tf_warning_or_error
)
1555 pedwarn (input_location
, OPT_Wvla
,
1556 "taking sizeof array of runtime bound");
1558 return error_mark_node
;
1561 return c_sizeof_or_alignof_type (input_location
, complete_type (type
),
1566 /* Return the size of the type, without producing any warnings for
1567 types whose size cannot be taken. This routine should be used only
1568 in some other routine that has already produced a diagnostic about
1569 using the size of such a type. */
1571 cxx_sizeof_nowarn (tree type
)
1573 if (TREE_CODE (type
) == FUNCTION_TYPE
1574 || VOID_TYPE_P (type
)
1575 || TREE_CODE (type
) == ERROR_MARK
)
1576 return size_one_node
;
1577 else if (!COMPLETE_TYPE_P (type
))
1578 return size_zero_node
;
1580 return cxx_sizeof_or_alignof_type (type
, SIZEOF_EXPR
, false);
1583 /* Process a sizeof expression where the operand is an expression. */
1586 cxx_sizeof_expr (tree e
, tsubst_flags_t complain
)
1588 if (e
== error_mark_node
)
1589 return error_mark_node
;
1591 if (processing_template_decl
)
1593 e
= build_min (SIZEOF_EXPR
, size_type_node
, e
);
1594 TREE_SIDE_EFFECTS (e
) = 0;
1595 TREE_READONLY (e
) = 1;
1600 /* To get the size of a static data member declared as an array of
1601 unknown bound, we need to instantiate it. */
1603 && VAR_HAD_UNKNOWN_BOUND (e
)
1604 && DECL_TEMPLATE_INSTANTIATION (e
))
1605 instantiate_decl (e
, /*defer_ok*/true, /*expl_inst_mem*/false);
1607 e
= mark_type_use (e
);
1609 if (TREE_CODE (e
) == COMPONENT_REF
1610 && TREE_CODE (TREE_OPERAND (e
, 1)) == FIELD_DECL
1611 && DECL_C_BIT_FIELD (TREE_OPERAND (e
, 1)))
1613 if (complain
& tf_error
)
1614 error ("invalid application of %<sizeof%> to a bit-field");
1616 return error_mark_node
;
1619 else if (is_overloaded_fn (e
))
1621 if (complain
& tf_error
)
1622 permerror (input_location
, "ISO C++ forbids applying %<sizeof%> to an expression of "
1625 return error_mark_node
;
1628 else if (type_unknown_p (e
))
1630 if (complain
& tf_error
)
1631 cxx_incomplete_type_error (e
, TREE_TYPE (e
));
1633 return error_mark_node
;
1639 return cxx_sizeof_or_alignof_type (e
, SIZEOF_EXPR
, complain
& tf_error
);
1642 /* Implement the __alignof keyword: Return the minimum required
1643 alignment of E, measured in bytes. For VAR_DECL's and
1644 FIELD_DECL's return DECL_ALIGN (which can be set from an
1645 "aligned" __attribute__ specification). */
1648 cxx_alignof_expr (tree e
, tsubst_flags_t complain
)
1652 if (e
== error_mark_node
)
1653 return error_mark_node
;
1655 if (processing_template_decl
)
1657 e
= build_min (ALIGNOF_EXPR
, size_type_node
, e
);
1658 TREE_SIDE_EFFECTS (e
) = 0;
1659 TREE_READONLY (e
) = 1;
1664 e
= mark_type_use (e
);
1667 t
= size_int (DECL_ALIGN_UNIT (e
));
1668 else if (TREE_CODE (e
) == COMPONENT_REF
1669 && TREE_CODE (TREE_OPERAND (e
, 1)) == FIELD_DECL
1670 && DECL_C_BIT_FIELD (TREE_OPERAND (e
, 1)))
1672 if (complain
& tf_error
)
1673 error ("invalid application of %<__alignof%> to a bit-field");
1675 return error_mark_node
;
1678 else if (TREE_CODE (e
) == COMPONENT_REF
1679 && TREE_CODE (TREE_OPERAND (e
, 1)) == FIELD_DECL
)
1680 t
= size_int (DECL_ALIGN_UNIT (TREE_OPERAND (e
, 1)));
1681 else if (is_overloaded_fn (e
))
1683 if (complain
& tf_error
)
1684 permerror (input_location
, "ISO C++ forbids applying %<__alignof%> to an expression of "
1687 return error_mark_node
;
1688 if (TREE_CODE (e
) == FUNCTION_DECL
)
1689 t
= size_int (DECL_ALIGN_UNIT (e
));
1693 else if (type_unknown_p (e
))
1695 if (complain
& tf_error
)
1696 cxx_incomplete_type_error (e
, TREE_TYPE (e
));
1698 return error_mark_node
;
1702 return cxx_sizeof_or_alignof_type (TREE_TYPE (e
), ALIGNOF_EXPR
,
1703 complain
& tf_error
);
1705 return fold_convert (size_type_node
, t
);
1708 /* Process a sizeof or alignof expression E with code OP where the operand
1709 is an expression. */
1712 cxx_sizeof_or_alignof_expr (tree e
, enum tree_code op
, bool complain
)
1714 if (op
== SIZEOF_EXPR
)
1715 return cxx_sizeof_expr (e
, complain
? tf_warning_or_error
: tf_none
);
1717 return cxx_alignof_expr (e
, complain
? tf_warning_or_error
: tf_none
);
1720 /* Build a representation of an expression 'alignas(E).' Return the
1721 folded integer value of E if it is an integral constant expression
1722 that resolves to a valid alignment. If E depends on a template
1723 parameter, return a syntactic representation tree of kind
1724 ALIGNOF_EXPR. Otherwise, return an error_mark_node if the
1725 expression is ill formed, or NULL_TREE if E is NULL_TREE. */
1728 cxx_alignas_expr (tree e
)
1730 if (e
== NULL_TREE
|| e
== error_mark_node
1731 || (!TYPE_P (e
) && !require_potential_rvalue_constant_expression (e
)))
1737 When the alignment-specifier is of the form
1738 alignas(type-id ), it shall have the same effect as
1739 alignas(alignof(type-id )). */
1741 return cxx_sizeof_or_alignof_type (e
, ALIGNOF_EXPR
, false);
1743 /* If we reach this point, it means the alignas expression if of
1744 the form "alignas(assignment-expression)", so we should follow
1745 what is stated by [dcl.align]/2. */
1747 if (value_dependent_expression_p (e
))
1748 /* Leave value-dependent expression alone for now. */
1751 e
= fold_non_dependent_expr (e
);
1752 e
= mark_rvalue_use (e
);
1754 /* [dcl.align]/2 says:
1756 the assignment-expression shall be an integral constant
1759 return cxx_constant_value (e
);
1763 /* EXPR is being used in a context that is not a function call.
1768 The expression can be used only as the left-hand operand of a
1769 member function call.
1771 [expr.mptr.operator]
1773 If the result of .* or ->* is a function, then that result can be
1774 used only as the operand for the function call operator ().
1776 by issuing an error message if appropriate. Returns true iff EXPR
1777 violates these rules. */
1780 invalid_nonstatic_memfn_p (tree expr
, tsubst_flags_t complain
)
1782 if (expr
== NULL_TREE
)
1784 /* Don't enforce this in MS mode. */
1785 if (flag_ms_extensions
)
1787 if (is_overloaded_fn (expr
) && !really_overloaded_fn (expr
))
1788 expr
= get_first_fn (expr
);
1789 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (expr
))
1791 if (complain
& tf_error
)
1792 error ("invalid use of non-static member function");
1798 /* If EXP is a reference to a bitfield, and the type of EXP does not
1799 match the declared type of the bitfield, return the declared type
1800 of the bitfield. Otherwise, return NULL_TREE. */
1803 is_bitfield_expr_with_lowered_type (const_tree exp
)
1805 switch (TREE_CODE (exp
))
1808 if (!is_bitfield_expr_with_lowered_type (TREE_OPERAND (exp
, 1)
1809 ? TREE_OPERAND (exp
, 1)
1810 : TREE_OPERAND (exp
, 0)))
1812 return is_bitfield_expr_with_lowered_type (TREE_OPERAND (exp
, 2));
1815 return is_bitfield_expr_with_lowered_type (TREE_OPERAND (exp
, 1));
1819 return is_bitfield_expr_with_lowered_type (TREE_OPERAND (exp
, 0));
1825 field
= TREE_OPERAND (exp
, 1);
1826 if (TREE_CODE (field
) != FIELD_DECL
|| !DECL_BIT_FIELD_TYPE (field
))
1828 if (same_type_ignoring_top_level_qualifiers_p
1829 (TREE_TYPE (exp
), DECL_BIT_FIELD_TYPE (field
)))
1831 return DECL_BIT_FIELD_TYPE (field
);
1835 if (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_OPERAND (exp
, 0)))
1836 == TYPE_MAIN_VARIANT (TREE_TYPE (exp
)))
1837 return is_bitfield_expr_with_lowered_type (TREE_OPERAND (exp
, 0));
1845 /* Like is_bitfield_with_lowered_type, except that if EXP is not a
1846 bitfield with a lowered type, the type of EXP is returned, rather
1850 unlowered_expr_type (const_tree exp
)
1853 tree etype
= TREE_TYPE (exp
);
1855 type
= is_bitfield_expr_with_lowered_type (exp
);
1857 type
= cp_build_qualified_type (type
, cp_type_quals (etype
));
1864 /* Perform the conversions in [expr] that apply when an lvalue appears
1865 in an rvalue context: the lvalue-to-rvalue, array-to-pointer, and
1866 function-to-pointer conversions. In addition, manifest constants
1867 are replaced by their values, and bitfield references are converted
1868 to their declared types. Note that this function does not perform the
1869 lvalue-to-rvalue conversion for class types. If you need that conversion
1870 to for class types, then you probably need to use force_rvalue.
1872 Although the returned value is being used as an rvalue, this
1873 function does not wrap the returned expression in a
1874 NON_LVALUE_EXPR; the caller is expected to be mindful of the fact
1875 that the return value is no longer an lvalue. */
1878 decay_conversion (tree exp
, tsubst_flags_t complain
)
1881 enum tree_code code
;
1882 location_t loc
= EXPR_LOC_OR_HERE (exp
);
1884 type
= TREE_TYPE (exp
);
1885 if (type
== error_mark_node
)
1886 return error_mark_node
;
1888 exp
= mark_rvalue_use (exp
);
1890 exp
= resolve_nondeduced_context (exp
);
1891 if (type_unknown_p (exp
))
1893 if (complain
& tf_error
)
1894 cxx_incomplete_type_error (exp
, TREE_TYPE (exp
));
1895 return error_mark_node
;
1898 code
= TREE_CODE (type
);
1900 /* For an array decl decay_conversion should not try to return its
1902 if (code
!= ARRAY_TYPE
)
1904 /* FIXME remove? at least need to remember that this isn't really a
1905 constant expression if EXP isn't decl_constant_var_p, like with
1906 C_MAYBE_CONST_EXPR. */
1907 exp
= decl_constant_value_safe (exp
);
1908 if (error_operand_p (exp
))
1909 return error_mark_node
;
1912 if (NULLPTR_TYPE_P (type
) && !TREE_SIDE_EFFECTS (exp
))
1913 return nullptr_node
;
1915 /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
1916 Leave such NOP_EXPRs, since RHS is being used in non-lvalue context. */
1917 if (code
== VOID_TYPE
)
1919 if (complain
& tf_error
)
1920 error_at (loc
, "void value not ignored as it ought to be");
1921 return error_mark_node
;
1923 if (invalid_nonstatic_memfn_p (exp
, complain
))
1924 return error_mark_node
;
1925 if (code
== FUNCTION_TYPE
|| is_overloaded_fn (exp
))
1926 return cp_build_addr_expr (exp
, complain
);
1927 if (code
== ARRAY_TYPE
)
1932 if (INDIRECT_REF_P (exp
))
1933 return build_nop (build_pointer_type (TREE_TYPE (type
)),
1934 TREE_OPERAND (exp
, 0));
1936 if (TREE_CODE (exp
) == COMPOUND_EXPR
)
1938 tree op1
= decay_conversion (TREE_OPERAND (exp
, 1), complain
);
1939 if (op1
== error_mark_node
)
1940 return error_mark_node
;
1941 return build2 (COMPOUND_EXPR
, TREE_TYPE (op1
),
1942 TREE_OPERAND (exp
, 0), op1
);
1946 && ! (TREE_CODE (exp
) == CONSTRUCTOR
&& TREE_STATIC (exp
)))
1948 if (complain
& tf_error
)
1949 error_at (loc
, "invalid use of non-lvalue array");
1950 return error_mark_node
;
1953 /* Don't let an array compound literal decay to a pointer. It can
1954 still be used to initialize an array or bind to a reference. */
1955 if (TREE_CODE (exp
) == TARGET_EXPR
)
1957 if (complain
& tf_error
)
1958 error_at (loc
, "taking address of temporary array");
1959 return error_mark_node
;
1962 ptrtype
= build_pointer_type (TREE_TYPE (type
));
1966 if (!cxx_mark_addressable (exp
))
1967 return error_mark_node
;
1968 adr
= build_nop (ptrtype
, build_address (exp
));
1971 /* This way is better for a COMPONENT_REF since it can
1972 simplify the offset for a component. */
1973 adr
= cp_build_addr_expr (exp
, complain
);
1974 return cp_convert (ptrtype
, adr
, complain
);
1977 /* If a bitfield is used in a context where integral promotion
1978 applies, then the caller is expected to have used
1979 default_conversion. That function promotes bitfields correctly
1980 before calling this function. At this point, if we have a
1981 bitfield referenced, we may assume that is not subject to
1982 promotion, and that, therefore, the type of the resulting rvalue
1983 is the declared type of the bitfield. */
1984 exp
= convert_bitfield_to_declared_type (exp
);
1986 /* We do not call rvalue() here because we do not want to wrap EXP
1987 in a NON_LVALUE_EXPR. */
1991 Non-class rvalues always have cv-unqualified types. */
1992 type
= TREE_TYPE (exp
);
1993 if (!CLASS_TYPE_P (type
) && cv_qualified_p (type
))
1994 exp
= build_nop (cv_unqualified (type
), exp
);
1999 /* Perform preparatory conversions, as part of the "usual arithmetic
2000 conversions". In particular, as per [expr]:
2002 Whenever an lvalue expression appears as an operand of an
2003 operator that expects the rvalue for that operand, the
2004 lvalue-to-rvalue, array-to-pointer, or function-to-pointer
2005 standard conversions are applied to convert the expression to an
2008 In addition, we perform integral promotions here, as those are
2009 applied to both operands to a binary operator before determining
2010 what additional conversions should apply. */
2013 cp_default_conversion (tree exp
, tsubst_flags_t complain
)
2015 /* Check for target-specific promotions. */
2016 tree promoted_type
= targetm
.promoted_type (TREE_TYPE (exp
));
2018 exp
= cp_convert (promoted_type
, exp
, complain
);
2019 /* Perform the integral promotions first so that bitfield
2020 expressions (which may promote to "int", even if the bitfield is
2021 declared "unsigned") are promoted correctly. */
2022 else if (INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (TREE_TYPE (exp
)))
2023 exp
= cp_perform_integral_promotions (exp
, complain
);
2024 /* Perform the other conversions. */
2025 exp
= decay_conversion (exp
, complain
);
2033 default_conversion (tree exp
)
2035 return cp_default_conversion (exp
, tf_warning_or_error
);
2038 /* EXPR is an expression with an integral or enumeration type.
2039 Perform the integral promotions in [conv.prom], and return the
2043 cp_perform_integral_promotions (tree expr
, tsubst_flags_t complain
)
2048 expr
= mark_rvalue_use (expr
);
2052 If the bitfield has an enumerated type, it is treated as any
2053 other value of that type for promotion purposes. */
2054 type
= is_bitfield_expr_with_lowered_type (expr
);
2055 if (!type
|| TREE_CODE (type
) != ENUMERAL_TYPE
)
2056 type
= TREE_TYPE (expr
);
2057 gcc_assert (INTEGRAL_OR_ENUMERATION_TYPE_P (type
));
2058 /* Scoped enums don't promote. */
2059 if (SCOPED_ENUM_P (type
))
2061 promoted_type
= type_promotes_to (type
);
2062 if (type
!= promoted_type
)
2063 expr
= cp_convert (promoted_type
, expr
, complain
);
2070 perform_integral_promotions (tree expr
)
2072 return cp_perform_integral_promotions (expr
, tf_warning_or_error
);
2075 /* Returns nonzero iff exp is a STRING_CST or the result of applying
2076 decay_conversion to one. */
2079 string_conv_p (const_tree totype
, const_tree exp
, int warn
)
2083 if (!TYPE_PTR_P (totype
))
2086 t
= TREE_TYPE (totype
);
2087 if (!same_type_p (t
, char_type_node
)
2088 && !same_type_p (t
, char16_type_node
)
2089 && !same_type_p (t
, char32_type_node
)
2090 && !same_type_p (t
, wchar_type_node
))
2093 if (TREE_CODE (exp
) == STRING_CST
)
2095 /* Make sure that we don't try to convert between char and wide chars. */
2096 if (!same_type_p (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (exp
))), t
))
2101 /* Is this a string constant which has decayed to 'const char *'? */
2102 t
= build_pointer_type (cp_build_qualified_type (t
, TYPE_QUAL_CONST
));
2103 if (!same_type_p (TREE_TYPE (exp
), t
))
2106 if (TREE_CODE (exp
) != ADDR_EXPR
2107 || TREE_CODE (TREE_OPERAND (exp
, 0)) != STRING_CST
)
2111 /* This warning is not very useful, as it complains about printf. */
2113 warning (OPT_Wwrite_strings
,
2114 "deprecated conversion from string constant to %qT",
2120 /* Given a COND_EXPR, MIN_EXPR, or MAX_EXPR in T, return it in a form that we
2121 can, for example, use as an lvalue. This code used to be in
2122 unary_complex_lvalue, but we needed it to deal with `a = (d == c) ? b : c'
2123 expressions, where we're dealing with aggregates. But now it's again only
2124 called from unary_complex_lvalue. The case (in particular) that led to
2125 this was with CODE == ADDR_EXPR, since it's not an lvalue when we'd
2129 rationalize_conditional_expr (enum tree_code code
, tree t
,
2130 tsubst_flags_t complain
)
2132 /* For MIN_EXPR or MAX_EXPR, fold-const.c has arranged things so that
2133 the first operand is always the one to be used if both operands
2134 are equal, so we know what conditional expression this used to be. */
2135 if (TREE_CODE (t
) == MIN_EXPR
|| TREE_CODE (t
) == MAX_EXPR
)
2137 tree op0
= TREE_OPERAND (t
, 0);
2138 tree op1
= TREE_OPERAND (t
, 1);
2140 /* The following code is incorrect if either operand side-effects. */
2141 gcc_assert (!TREE_SIDE_EFFECTS (op0
)
2142 && !TREE_SIDE_EFFECTS (op1
));
2144 build_conditional_expr (EXPR_LOC_OR_HERE (t
),
2145 build_x_binary_op (EXPR_LOC_OR_HERE (t
),
2146 (TREE_CODE (t
) == MIN_EXPR
2147 ? LE_EXPR
: GE_EXPR
),
2148 op0
, TREE_CODE (op0
),
2149 op1
, TREE_CODE (op1
),
2152 cp_build_unary_op (code
, op0
, 0, complain
),
2153 cp_build_unary_op (code
, op1
, 0, complain
),
2158 build_conditional_expr (EXPR_LOC_OR_HERE (t
), TREE_OPERAND (t
, 0),
2159 cp_build_unary_op (code
, TREE_OPERAND (t
, 1), 0,
2161 cp_build_unary_op (code
, TREE_OPERAND (t
, 2), 0,
2166 /* Given the TYPE of an anonymous union field inside T, return the
2167 FIELD_DECL for the field. If not found return NULL_TREE. Because
2168 anonymous unions can nest, we must also search all anonymous unions
2169 that are directly reachable. */
2172 lookup_anon_field (tree t
, tree type
)
2176 for (field
= TYPE_FIELDS (t
); field
; field
= DECL_CHAIN (field
))
2178 if (TREE_STATIC (field
))
2180 if (TREE_CODE (field
) != FIELD_DECL
|| DECL_ARTIFICIAL (field
))
2183 /* If we find it directly, return the field. */
2184 if (DECL_NAME (field
) == NULL_TREE
2185 && type
== TYPE_MAIN_VARIANT (TREE_TYPE (field
)))
2190 /* Otherwise, it could be nested, search harder. */
2191 if (DECL_NAME (field
) == NULL_TREE
2192 && ANON_AGGR_TYPE_P (TREE_TYPE (field
)))
2194 tree subfield
= lookup_anon_field (TREE_TYPE (field
), type
);
2202 /* Build an expression representing OBJECT.MEMBER. OBJECT is an
2203 expression; MEMBER is a DECL or baselink. If ACCESS_PATH is
2204 non-NULL, it indicates the path to the base used to name MEMBER.
2205 If PRESERVE_REFERENCE is true, the expression returned will have
2206 REFERENCE_TYPE if the MEMBER does. Otherwise, the expression
2207 returned will have the type referred to by the reference.
2209 This function does not perform access control; that is either done
2210 earlier by the parser when the name of MEMBER is resolved to MEMBER
2211 itself, or later when overload resolution selects one of the
2212 functions indicated by MEMBER. */
2215 build_class_member_access_expr (tree object
, tree member
,
2216 tree access_path
, bool preserve_reference
,
2217 tsubst_flags_t complain
)
2221 tree result
= NULL_TREE
;
2222 tree using_decl
= NULL_TREE
;
2224 if (error_operand_p (object
) || error_operand_p (member
))
2225 return error_mark_node
;
2227 gcc_assert (DECL_P (member
) || BASELINK_P (member
));
2231 The type of the first expression shall be "class object" (of a
2233 object_type
= TREE_TYPE (object
);
2234 if (!currently_open_class (object_type
)
2235 && !complete_type_or_maybe_complain (object_type
, object
, complain
))
2236 return error_mark_node
;
2237 if (!CLASS_TYPE_P (object_type
))
2239 if (complain
& tf_error
)
2241 if (POINTER_TYPE_P (object_type
)
2242 && CLASS_TYPE_P (TREE_TYPE (object_type
)))
2243 error ("request for member %qD in %qE, which is of pointer "
2244 "type %qT (maybe you meant to use %<->%> ?)",
2245 member
, object
, object_type
);
2247 error ("request for member %qD in %qE, which is of non-class "
2248 "type %qT", member
, object
, object_type
);
2250 return error_mark_node
;
2253 /* The standard does not seem to actually say that MEMBER must be a
2254 member of OBJECT_TYPE. However, that is clearly what is
2256 if (DECL_P (member
))
2258 member_scope
= DECL_CLASS_CONTEXT (member
);
2260 if (TREE_DEPRECATED (member
))
2261 warn_deprecated_use (member
, NULL_TREE
);
2264 member_scope
= BINFO_TYPE (BASELINK_ACCESS_BINFO (member
));
2265 /* If MEMBER is from an anonymous aggregate, MEMBER_SCOPE will
2266 presently be the anonymous union. Go outwards until we find a
2267 type related to OBJECT_TYPE. */
2268 while ((ANON_AGGR_TYPE_P (member_scope
) || UNSCOPED_ENUM_P (member_scope
))
2269 && !same_type_ignoring_top_level_qualifiers_p (member_scope
,
2271 member_scope
= TYPE_CONTEXT (member_scope
);
2272 if (!member_scope
|| !DERIVED_FROM_P (member_scope
, object_type
))
2274 if (complain
& tf_error
)
2276 if (TREE_CODE (member
) == FIELD_DECL
)
2277 error ("invalid use of nonstatic data member %qE", member
);
2279 error ("%qD is not a member of %qT", member
, object_type
);
2281 return error_mark_node
;
2284 /* Transform `(a, b).x' into `(*(a, &b)).x', `(a ? b : c).x' into
2285 `(*(a ? &b : &c)).x', and so on. A COND_EXPR is only an lvalue
2286 in the front end; only _DECLs and _REFs are lvalues in the back end. */
2288 tree temp
= unary_complex_lvalue (ADDR_EXPR
, object
);
2290 object
= cp_build_indirect_ref (temp
, RO_NULL
, complain
);
2293 /* In [expr.ref], there is an explicit list of the valid choices for
2294 MEMBER. We check for each of those cases here. */
2297 /* A static data member. */
2299 mark_exp_read (object
);
2300 /* If OBJECT has side-effects, they are supposed to occur. */
2301 if (TREE_SIDE_EFFECTS (object
))
2302 result
= build2 (COMPOUND_EXPR
, TREE_TYPE (result
), object
, result
);
2304 else if (TREE_CODE (member
) == FIELD_DECL
)
2306 /* A non-static data member. */
2311 null_object_p
= (INDIRECT_REF_P (object
)
2312 && integer_zerop (TREE_OPERAND (object
, 0)));
2314 /* Convert OBJECT to the type of MEMBER. */
2315 if (!same_type_p (TYPE_MAIN_VARIANT (object_type
),
2316 TYPE_MAIN_VARIANT (member_scope
)))
2321 binfo
= lookup_base (access_path
? access_path
: object_type
,
2322 member_scope
, ba_unique
, &kind
, complain
);
2323 if (binfo
== error_mark_node
)
2324 return error_mark_node
;
2326 /* It is invalid to try to get to a virtual base of a
2327 NULL object. The most common cause is invalid use of
2329 if (null_object_p
&& kind
== bk_via_virtual
)
2331 if (complain
& tf_error
)
2333 error ("invalid access to non-static data member %qD of "
2336 error ("(perhaps the %<offsetof%> macro was used incorrectly)");
2338 return error_mark_node
;
2341 /* Convert to the base. */
2342 object
= build_base_path (PLUS_EXPR
, object
, binfo
,
2343 /*nonnull=*/1, complain
);
2344 /* If we found the base successfully then we should be able
2345 to convert to it successfully. */
2346 gcc_assert (object
!= error_mark_node
);
2349 /* Complain about other invalid uses of offsetof, even though they will
2350 give the right answer. Note that we complain whether or not they
2351 actually used the offsetof macro, since there's no way to know at this
2352 point. So we just give a warning, instead of a pedwarn. */
2353 /* Do not produce this warning for base class field references, because
2354 we know for a fact that didn't come from offsetof. This does occur
2355 in various testsuite cases where a null object is passed where a
2356 vtable access is required. */
2357 if (null_object_p
&& warn_invalid_offsetof
2358 && CLASSTYPE_NON_STD_LAYOUT (object_type
)
2359 && !DECL_FIELD_IS_BASE (member
)
2360 && cp_unevaluated_operand
== 0
2361 && (complain
& tf_warning
))
2363 warning (OPT_Winvalid_offsetof
,
2364 "invalid access to non-static data member %qD "
2365 " of NULL object", member
);
2366 warning (OPT_Winvalid_offsetof
,
2367 "(perhaps the %<offsetof%> macro was used incorrectly)");
2370 /* If MEMBER is from an anonymous aggregate, we have converted
2371 OBJECT so that it refers to the class containing the
2372 anonymous union. Generate a reference to the anonymous union
2373 itself, and recur to find MEMBER. */
2374 if (ANON_AGGR_TYPE_P (DECL_CONTEXT (member
))
2375 /* When this code is called from build_field_call, the
2376 object already has the type of the anonymous union.
2377 That is because the COMPONENT_REF was already
2378 constructed, and was then disassembled before calling
2379 build_field_call. After the function-call code is
2380 cleaned up, this waste can be eliminated. */
2381 && (!same_type_ignoring_top_level_qualifiers_p
2382 (TREE_TYPE (object
), DECL_CONTEXT (member
))))
2384 tree anonymous_union
;
2386 anonymous_union
= lookup_anon_field (TREE_TYPE (object
),
2387 DECL_CONTEXT (member
));
2388 object
= build_class_member_access_expr (object
,
2390 /*access_path=*/NULL_TREE
,
2395 /* Compute the type of the field, as described in [expr.ref]. */
2396 type_quals
= TYPE_UNQUALIFIED
;
2397 member_type
= TREE_TYPE (member
);
2398 if (TREE_CODE (member_type
) != REFERENCE_TYPE
)
2400 type_quals
= (cp_type_quals (member_type
)
2401 | cp_type_quals (object_type
));
2403 /* A field is const (volatile) if the enclosing object, or the
2404 field itself, is const (volatile). But, a mutable field is
2405 not const, even within a const object. */
2406 if (DECL_MUTABLE_P (member
))
2407 type_quals
&= ~TYPE_QUAL_CONST
;
2408 member_type
= cp_build_qualified_type (member_type
, type_quals
);
2411 result
= build3 (COMPONENT_REF
, member_type
, object
, member
,
2413 result
= fold_if_not_in_template (result
);
2415 /* Mark the expression const or volatile, as appropriate. Even
2416 though we've dealt with the type above, we still have to mark the
2417 expression itself. */
2418 if (type_quals
& TYPE_QUAL_CONST
)
2419 TREE_READONLY (result
) = 1;
2420 if (type_quals
& TYPE_QUAL_VOLATILE
)
2421 TREE_THIS_VOLATILE (result
) = 1;
2423 else if (BASELINK_P (member
))
2425 /* The member is a (possibly overloaded) member function. */
2429 /* If the MEMBER is exactly one static member function, then we
2430 know the type of the expression. Otherwise, we must wait
2431 until overload resolution has been performed. */
2432 functions
= BASELINK_FUNCTIONS (member
);
2433 if (TREE_CODE (functions
) == FUNCTION_DECL
2434 && DECL_STATIC_FUNCTION_P (functions
))
2435 type
= TREE_TYPE (functions
);
2437 type
= unknown_type_node
;
2438 /* Note that we do not convert OBJECT to the BASELINK_BINFO
2439 base. That will happen when the function is called. */
2440 result
= build3 (COMPONENT_REF
, type
, object
, member
, NULL_TREE
);
2442 else if (TREE_CODE (member
) == CONST_DECL
)
2444 /* The member is an enumerator. */
2446 /* If OBJECT has side-effects, they are supposed to occur. */
2447 if (TREE_SIDE_EFFECTS (object
))
2448 result
= build2 (COMPOUND_EXPR
, TREE_TYPE (result
),
2451 else if ((using_decl
= strip_using_decl (member
)) != member
)
2452 result
= build_class_member_access_expr (object
,
2454 access_path
, preserve_reference
,
2458 if (complain
& tf_error
)
2459 error ("invalid use of %qD", member
);
2460 return error_mark_node
;
2463 if (!preserve_reference
)
2466 If E2 is declared to have type "reference to T", then ... the
2467 type of E1.E2 is T. */
2468 result
= convert_from_reference (result
);
2473 /* Return the destructor denoted by OBJECT.SCOPE::DTOR_NAME, or, if
2474 SCOPE is NULL, by OBJECT.DTOR_NAME, where DTOR_NAME is ~type. */
2477 lookup_destructor (tree object
, tree scope
, tree dtor_name
,
2478 tsubst_flags_t complain
)
2480 tree object_type
= TREE_TYPE (object
);
2481 tree dtor_type
= TREE_OPERAND (dtor_name
, 0);
2484 if (scope
&& !check_dtor_name (scope
, dtor_type
))
2486 if (complain
& tf_error
)
2487 error ("qualified type %qT does not match destructor name ~%qT",
2489 return error_mark_node
;
2491 if (is_auto (dtor_type
))
2492 dtor_type
= object_type
;
2493 else if (identifier_p (dtor_type
))
2495 /* In a template, names we can't find a match for are still accepted
2496 destructor names, and we check them here. */
2497 if (check_dtor_name (object_type
, dtor_type
))
2498 dtor_type
= object_type
;
2501 if (complain
& tf_error
)
2502 error ("object type %qT does not match destructor name ~%qT",
2503 object_type
, dtor_type
);
2504 return error_mark_node
;
2508 else if (!DERIVED_FROM_P (dtor_type
, TYPE_MAIN_VARIANT (object_type
)))
2510 if (complain
& tf_error
)
2511 error ("the type being destroyed is %qT, but the destructor "
2512 "refers to %qT", TYPE_MAIN_VARIANT (object_type
), dtor_type
);
2513 return error_mark_node
;
2515 expr
= lookup_member (dtor_type
, complete_dtor_identifier
,
2516 /*protect=*/1, /*want_type=*/false,
2517 tf_warning_or_error
);
2518 expr
= (adjust_result_of_qualified_name_lookup
2519 (expr
, dtor_type
, object_type
));
2520 if (scope
== NULL_TREE
)
2521 /* We need to call adjust_result_of_qualified_name_lookup in case the
2522 destructor names a base class, but we unset BASELINK_QUALIFIED_P so
2523 that we still get virtual function binding. */
2524 BASELINK_QUALIFIED_P (expr
) = false;
2528 /* An expression of the form "A::template B" has been resolved to
2529 DECL. Issue a diagnostic if B is not a template or template
2533 check_template_keyword (tree decl
)
2535 /* The standard says:
2539 If a name prefixed by the keyword template is not a member
2540 template, the program is ill-formed.
2542 DR 228 removed the restriction that the template be a member
2545 DR 96, if accepted would add the further restriction that explicit
2546 template arguments must be provided if the template keyword is
2547 used, but, as of 2005-10-16, that DR is still in "drafting". If
2548 this DR is accepted, then the semantic checks here can be
2549 simplified, as the entity named must in fact be a template
2550 specialization, rather than, as at present, a set of overloaded
2551 functions containing at least one template function. */
2552 if (TREE_CODE (decl
) != TEMPLATE_DECL
2553 && TREE_CODE (decl
) != TEMPLATE_ID_EXPR
)
2555 if (!is_overloaded_fn (decl
))
2556 permerror (input_location
, "%qD is not a template", decl
);
2561 if (BASELINK_P (fns
))
2562 fns
= BASELINK_FUNCTIONS (fns
);
2565 tree fn
= OVL_CURRENT (fns
);
2566 if (TREE_CODE (fn
) == TEMPLATE_DECL
2567 || TREE_CODE (fn
) == TEMPLATE_ID_EXPR
)
2569 if (TREE_CODE (fn
) == FUNCTION_DECL
2570 && DECL_USE_TEMPLATE (fn
)
2571 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (fn
)))
2573 fns
= OVL_NEXT (fns
);
2576 permerror (input_location
, "%qD is not a template", decl
);
2581 /* This function is called by the parser to process a class member
2582 access expression of the form OBJECT.NAME. NAME is a node used by
2583 the parser to represent a name; it is not yet a DECL. It may,
2584 however, be a BASELINK where the BASELINK_FUNCTIONS is a
2585 TEMPLATE_ID_EXPR. Templates must be looked up by the parser, and
2586 there is no reason to do the lookup twice, so the parser keeps the
2587 BASELINK. TEMPLATE_P is true iff NAME was explicitly declared to
2588 be a template via the use of the "A::template B" syntax. */
2591 finish_class_member_access_expr (tree object
, tree name
, bool template_p
,
2592 tsubst_flags_t complain
)
2597 tree access_path
= NULL_TREE
;
2598 tree orig_object
= object
;
2599 tree orig_name
= name
;
2601 if (object
== error_mark_node
|| name
== error_mark_node
)
2602 return error_mark_node
;
2604 /* If OBJECT is an ObjC class instance, we must obey ObjC access rules. */
2605 if (!objc_is_public (object
, name
))
2606 return error_mark_node
;
2608 object_type
= TREE_TYPE (object
);
2610 if (processing_template_decl
)
2612 if (/* If OBJECT_TYPE is dependent, so is OBJECT.NAME. */
2613 dependent_type_p (object_type
)
2614 /* If NAME is just an IDENTIFIER_NODE, then the expression
2616 || identifier_p (object
)
2617 /* If NAME is "f<args>", where either 'f' or 'args' is
2618 dependent, then the expression is dependent. */
2619 || (TREE_CODE (name
) == TEMPLATE_ID_EXPR
2620 && dependent_template_id_p (TREE_OPERAND (name
, 0),
2621 TREE_OPERAND (name
, 1)))
2622 /* If NAME is "T::X" where "T" is dependent, then the
2623 expression is dependent. */
2624 || (TREE_CODE (name
) == SCOPE_REF
2625 && TYPE_P (TREE_OPERAND (name
, 0))
2626 && dependent_type_p (TREE_OPERAND (name
, 0))))
2627 return build_min_nt_loc (UNKNOWN_LOCATION
, COMPONENT_REF
,
2628 object
, name
, NULL_TREE
);
2629 object
= build_non_dependent_expr (object
);
2631 else if (c_dialect_objc ()
2632 && identifier_p (name
)
2633 && (expr
= objc_maybe_build_component_ref (object
, name
)))
2638 The type of the first expression shall be "class object" (of a
2640 if (!currently_open_class (object_type
)
2641 && !complete_type_or_maybe_complain (object_type
, object
, complain
))
2642 return error_mark_node
;
2643 if (!CLASS_TYPE_P (object_type
))
2645 if (complain
& tf_error
)
2647 if (POINTER_TYPE_P (object_type
)
2648 && CLASS_TYPE_P (TREE_TYPE (object_type
)))
2649 error ("request for member %qD in %qE, which is of pointer "
2650 "type %qT (maybe you meant to use %<->%> ?)",
2651 name
, object
, object_type
);
2653 error ("request for member %qD in %qE, which is of non-class "
2654 "type %qT", name
, object
, object_type
);
2656 return error_mark_node
;
2659 if (BASELINK_P (name
))
2660 /* A member function that has already been looked up. */
2664 bool is_template_id
= false;
2665 tree template_args
= NULL_TREE
;
2668 if (TREE_CODE (name
) == TEMPLATE_ID_EXPR
)
2670 is_template_id
= true;
2671 template_args
= TREE_OPERAND (name
, 1);
2672 name
= TREE_OPERAND (name
, 0);
2674 if (TREE_CODE (name
) == OVERLOAD
)
2675 name
= DECL_NAME (get_first_fn (name
));
2676 else if (DECL_P (name
))
2677 name
= DECL_NAME (name
);
2680 if (TREE_CODE (name
) == SCOPE_REF
)
2682 /* A qualified name. The qualifying class or namespace `S'
2683 has already been looked up; it is either a TYPE or a
2685 scope
= TREE_OPERAND (name
, 0);
2686 name
= TREE_OPERAND (name
, 1);
2688 /* If SCOPE is a namespace, then the qualified name does not
2689 name a member of OBJECT_TYPE. */
2690 if (TREE_CODE (scope
) == NAMESPACE_DECL
)
2692 if (complain
& tf_error
)
2693 error ("%<%D::%D%> is not a member of %qT",
2694 scope
, name
, object_type
);
2695 return error_mark_node
;
2698 if (TREE_CODE (scope
) == ENUMERAL_TYPE
)
2700 /* Looking up a member enumerator (c++/56793). */
2701 if (!TYPE_CLASS_SCOPE_P (scope
)
2702 || !DERIVED_FROM_P (TYPE_CONTEXT (scope
), object_type
))
2704 if (complain
& tf_error
)
2705 error ("%<%D::%D%> is not a member of %qT",
2706 scope
, name
, object_type
);
2707 return error_mark_node
;
2709 tree val
= lookup_enumerator (scope
, name
);
2710 if (TREE_SIDE_EFFECTS (object
))
2711 val
= build2 (COMPOUND_EXPR
, TREE_TYPE (val
), object
, val
);
2715 gcc_assert (CLASS_TYPE_P (scope
));
2716 gcc_assert (identifier_p (name
) || TREE_CODE (name
) == BIT_NOT_EXPR
);
2718 if (constructor_name_p (name
, scope
))
2720 if (complain
& tf_error
)
2721 error ("cannot call constructor %<%T::%D%> directly",
2723 return error_mark_node
;
2726 /* Find the base of OBJECT_TYPE corresponding to SCOPE. */
2727 access_path
= lookup_base (object_type
, scope
, ba_check
,
2729 if (access_path
== error_mark_node
)
2730 return error_mark_node
;
2733 if (complain
& tf_error
)
2734 error ("%qT is not a base of %qT", scope
, object_type
);
2735 return error_mark_node
;
2741 access_path
= object_type
;
2744 if (TREE_CODE (name
) == BIT_NOT_EXPR
)
2745 member
= lookup_destructor (object
, scope
, name
, complain
);
2748 /* Look up the member. */
2749 member
= lookup_member (access_path
, name
, /*protect=*/1,
2750 /*want_type=*/false, complain
);
2751 if (member
== NULL_TREE
)
2753 if (complain
& tf_error
)
2754 error ("%qD has no member named %qE",
2755 TREE_CODE (access_path
) == TREE_BINFO
2756 ? TREE_TYPE (access_path
) : object_type
, name
);
2757 return error_mark_node
;
2759 if (member
== error_mark_node
)
2760 return error_mark_node
;
2765 tree templ
= member
;
2767 if (BASELINK_P (templ
))
2768 templ
= lookup_template_function (templ
, template_args
);
2771 if (complain
& tf_error
)
2772 error ("%qD is not a member template function", name
);
2773 return error_mark_node
;
2778 if (TREE_DEPRECATED (member
))
2779 warn_deprecated_use (member
, NULL_TREE
);
2782 check_template_keyword (member
);
2784 expr
= build_class_member_access_expr (object
, member
, access_path
,
2785 /*preserve_reference=*/false,
2787 if (processing_template_decl
&& expr
!= error_mark_node
)
2789 if (BASELINK_P (member
))
2791 if (TREE_CODE (orig_name
) == SCOPE_REF
)
2792 BASELINK_QUALIFIED_P (member
) = 1;
2795 return build_min_non_dep (COMPONENT_REF
, expr
,
2796 orig_object
, orig_name
,
2803 /* Build a COMPONENT_REF of OBJECT and MEMBER with the appropriate
2807 build_simple_component_ref (tree object
, tree member
)
2809 tree type
= cp_build_qualified_type (TREE_TYPE (member
),
2810 cp_type_quals (TREE_TYPE (object
)));
2811 return fold_build3_loc (input_location
,
2812 COMPONENT_REF
, type
,
2813 object
, member
, NULL_TREE
);
2816 /* Return an expression for the MEMBER_NAME field in the internal
2817 representation of PTRMEM, a pointer-to-member function. (Each
2818 pointer-to-member function type gets its own RECORD_TYPE so it is
2819 more convenient to access the fields by name than by FIELD_DECL.)
2820 This routine converts the NAME to a FIELD_DECL and then creates the
2821 node for the complete expression. */
2824 build_ptrmemfunc_access_expr (tree ptrmem
, tree member_name
)
2829 /* This code is a stripped down version of
2830 build_class_member_access_expr. It does not work to use that
2831 routine directly because it expects the object to be of class
2833 ptrmem_type
= TREE_TYPE (ptrmem
);
2834 gcc_assert (TYPE_PTRMEMFUNC_P (ptrmem_type
));
2835 member
= lookup_member (ptrmem_type
, member_name
, /*protect=*/0,
2836 /*want_type=*/false, tf_warning_or_error
);
2837 return build_simple_component_ref (ptrmem
, member
);
2840 /* Given an expression PTR for a pointer, return an expression
2841 for the value pointed to.
2842 ERRORSTRING is the name of the operator to appear in error messages.
2844 This function may need to overload OPERATOR_FNNAME.
2845 Must also handle REFERENCE_TYPEs for C++. */
2848 build_x_indirect_ref (location_t loc
, tree expr
, ref_operator errorstring
,
2849 tsubst_flags_t complain
)
2851 tree orig_expr
= expr
;
2854 if (processing_template_decl
)
2856 /* Retain the type if we know the operand is a pointer. */
2857 if (TREE_TYPE (expr
) && POINTER_TYPE_P (TREE_TYPE (expr
)))
2858 return build_min (INDIRECT_REF
, TREE_TYPE (TREE_TYPE (expr
)), expr
);
2859 if (type_dependent_expression_p (expr
))
2860 return build_min_nt_loc (loc
, INDIRECT_REF
, expr
);
2861 expr
= build_non_dependent_expr (expr
);
2864 rval
= build_new_op (loc
, INDIRECT_REF
, LOOKUP_NORMAL
, expr
,
2865 NULL_TREE
, NULL_TREE
, /*overload=*/NULL
, complain
);
2867 rval
= cp_build_indirect_ref (expr
, errorstring
, complain
);
2869 if (processing_template_decl
&& rval
!= error_mark_node
)
2870 return build_min_non_dep (INDIRECT_REF
, rval
, orig_expr
);
2875 /* Helper function called from c-common. */
2877 build_indirect_ref (location_t
/*loc*/,
2878 tree ptr
, ref_operator errorstring
)
2880 return cp_build_indirect_ref (ptr
, errorstring
, tf_warning_or_error
);
2884 cp_build_indirect_ref (tree ptr
, ref_operator errorstring
,
2885 tsubst_flags_t complain
)
2889 if (ptr
== current_class_ptr
2890 || (TREE_CODE (ptr
) == NOP_EXPR
2891 && TREE_OPERAND (ptr
, 0) == current_class_ptr
2892 && (same_type_ignoring_top_level_qualifiers_p
2893 (TREE_TYPE (ptr
), TREE_TYPE (current_class_ptr
)))))
2894 return current_class_ref
;
2896 pointer
= (TREE_CODE (TREE_TYPE (ptr
)) == REFERENCE_TYPE
2897 ? ptr
: decay_conversion (ptr
, complain
));
2898 if (pointer
== error_mark_node
)
2899 return error_mark_node
;
2901 type
= TREE_TYPE (pointer
);
2903 if (POINTER_TYPE_P (type
))
2907 If the type of the expression is "pointer to T," the type
2908 of the result is "T." */
2909 tree t
= TREE_TYPE (type
);
2911 if (CONVERT_EXPR_P (ptr
)
2912 || TREE_CODE (ptr
) == VIEW_CONVERT_EXPR
)
2914 /* If a warning is issued, mark it to avoid duplicates from
2915 the backend. This only needs to be done at
2916 warn_strict_aliasing > 2. */
2917 if (warn_strict_aliasing
> 2)
2918 if (strict_aliasing_warning (TREE_TYPE (TREE_OPERAND (ptr
, 0)),
2919 type
, TREE_OPERAND (ptr
, 0)))
2920 TREE_NO_WARNING (ptr
) = 1;
2923 if (VOID_TYPE_P (t
))
2925 /* A pointer to incomplete type (other than cv void) can be
2926 dereferenced [expr.unary.op]/1 */
2927 if (complain
& tf_error
)
2928 error ("%qT is not a pointer-to-object type", type
);
2929 return error_mark_node
;
2931 else if (TREE_CODE (pointer
) == ADDR_EXPR
2932 && same_type_p (t
, TREE_TYPE (TREE_OPERAND (pointer
, 0))))
2933 /* The POINTER was something like `&x'. We simplify `*&x' to
2935 return TREE_OPERAND (pointer
, 0);
2938 tree ref
= build1 (INDIRECT_REF
, t
, pointer
);
2940 /* We *must* set TREE_READONLY when dereferencing a pointer to const,
2941 so that we get the proper error message if the result is used
2942 to assign to. Also, &* is supposed to be a no-op. */
2943 TREE_READONLY (ref
) = CP_TYPE_CONST_P (t
);
2944 TREE_THIS_VOLATILE (ref
) = CP_TYPE_VOLATILE_P (t
);
2945 TREE_SIDE_EFFECTS (ref
)
2946 = (TREE_THIS_VOLATILE (ref
) || TREE_SIDE_EFFECTS (pointer
));
2950 else if (!(complain
& tf_error
))
2951 /* Don't emit any errors; we'll just return ERROR_MARK_NODE later. */
2953 /* `pointer' won't be an error_mark_node if we were given a
2954 pointer to member, so it's cool to check for this here. */
2955 else if (TYPE_PTRMEM_P (type
))
2956 switch (errorstring
)
2958 case RO_ARRAY_INDEXING
:
2959 error ("invalid use of array indexing on pointer to member");
2962 error ("invalid use of unary %<*%> on pointer to member");
2964 case RO_IMPLICIT_CONVERSION
:
2965 error ("invalid use of implicit conversion on pointer to member");
2968 error ("left hand operand of %<->*%> must be a pointer to class, "
2969 "but is a pointer to member of type %qT", type
);
2974 else if (pointer
!= error_mark_node
)
2975 invalid_indirection_error (input_location
, type
, errorstring
);
2977 return error_mark_node
;
2980 /* This handles expressions of the form "a[i]", which denotes
2983 This is logically equivalent in C to *(a+i), but we may do it differently.
2984 If A is a variable or a member, we generate a primitive ARRAY_REF.
2985 This avoids forcing the array out of registers, and can work on
2986 arrays that are not lvalues (for example, members of structures returned
2989 If INDEX is of some user-defined type, it must be converted to
2990 integer type. Otherwise, to make a compatible PLUS_EXPR, it
2991 will inherit the type of the array, which will be some pointer type.
2993 LOC is the location to use in building the array reference. */
2996 cp_build_array_ref (location_t loc
, tree array
, tree idx
,
2997 tsubst_flags_t complain
)
3003 if (complain
& tf_error
)
3004 error_at (loc
, "subscript missing in array reference");
3005 return error_mark_node
;
3008 /* If an array's index is an array notation, then its rank cannot be
3009 greater than one. */
3010 if (flag_enable_cilkplus
&& contains_array_notation_expr (idx
))
3014 /* If find_rank returns false, then it should have reported an error,
3015 thus it is unnecessary for repetition. */
3016 if (!find_rank (loc
, idx
, idx
, true, &rank
))
3017 return error_mark_node
;
3020 error_at (loc
, "rank of the array%'s index is greater than 1");
3021 return error_mark_node
;
3024 if (TREE_TYPE (array
) == error_mark_node
3025 || TREE_TYPE (idx
) == error_mark_node
)
3026 return error_mark_node
;
3028 /* If ARRAY is a COMPOUND_EXPR or COND_EXPR, move our reference
3030 switch (TREE_CODE (array
))
3034 tree value
= cp_build_array_ref (loc
, TREE_OPERAND (array
, 1), idx
,
3036 ret
= build2 (COMPOUND_EXPR
, TREE_TYPE (value
),
3037 TREE_OPERAND (array
, 0), value
);
3038 SET_EXPR_LOCATION (ret
, loc
);
3043 ret
= build_conditional_expr
3044 (loc
, TREE_OPERAND (array
, 0),
3045 cp_build_array_ref (loc
, TREE_OPERAND (array
, 1), idx
,
3047 cp_build_array_ref (loc
, TREE_OPERAND (array
, 2), idx
,
3050 protected_set_expr_location (ret
, loc
);
3057 convert_vector_to_pointer_for_subscript (loc
, &array
, idx
);
3059 if (TREE_CODE (TREE_TYPE (array
)) == ARRAY_TYPE
)
3063 warn_array_subscript_with_type_char (idx
);
3065 if (!INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (TREE_TYPE (idx
)))
3067 if (complain
& tf_error
)
3068 error_at (loc
, "array subscript is not an integer");
3069 return error_mark_node
;
3072 /* Apply integral promotions *after* noticing character types.
3073 (It is unclear why we do these promotions -- the standard
3074 does not say that we should. In fact, the natural thing would
3075 seem to be to convert IDX to ptrdiff_t; we're performing
3076 pointer arithmetic.) */
3077 idx
= cp_perform_integral_promotions (idx
, complain
);
3079 /* An array that is indexed by a non-constant
3080 cannot be stored in a register; we must be able to do
3081 address arithmetic on its address.
3082 Likewise an array of elements of variable size. */
3083 if (TREE_CODE (idx
) != INTEGER_CST
3084 || (COMPLETE_TYPE_P (TREE_TYPE (TREE_TYPE (array
)))
3085 && (TREE_CODE (TYPE_SIZE (TREE_TYPE (TREE_TYPE (array
))))
3088 if (!cxx_mark_addressable (array
))
3089 return error_mark_node
;
3092 /* An array that is indexed by a constant value which is not within
3093 the array bounds cannot be stored in a register either; because we
3094 would get a crash in store_bit_field/extract_bit_field when trying
3095 to access a non-existent part of the register. */
3096 if (TREE_CODE (idx
) == INTEGER_CST
3097 && TYPE_DOMAIN (TREE_TYPE (array
))
3098 && ! int_fits_type_p (idx
, TYPE_DOMAIN (TREE_TYPE (array
))))
3100 if (!cxx_mark_addressable (array
))
3101 return error_mark_node
;
3104 if (!lvalue_p (array
) && (complain
& tf_error
))
3105 pedwarn (loc
, OPT_Wpedantic
,
3106 "ISO C++ forbids subscripting non-lvalue array");
3108 /* Note in C++ it is valid to subscript a `register' array, since
3109 it is valid to take the address of something with that
3110 storage specification. */
3114 while (TREE_CODE (foo
) == COMPONENT_REF
)
3115 foo
= TREE_OPERAND (foo
, 0);
3116 if (VAR_P (foo
) && DECL_REGISTER (foo
)
3117 && (complain
& tf_warning
))
3118 warning_at (loc
, OPT_Wextra
,
3119 "subscripting array declared %<register%>");
3122 type
= TREE_TYPE (TREE_TYPE (array
));
3123 rval
= build4 (ARRAY_REF
, type
, array
, idx
, NULL_TREE
, NULL_TREE
);
3124 /* Array ref is const/volatile if the array elements are
3125 or if the array is.. */
3126 TREE_READONLY (rval
)
3127 |= (CP_TYPE_CONST_P (type
) | TREE_READONLY (array
));
3128 TREE_SIDE_EFFECTS (rval
)
3129 |= (CP_TYPE_VOLATILE_P (type
) | TREE_SIDE_EFFECTS (array
));
3130 TREE_THIS_VOLATILE (rval
)
3131 |= (CP_TYPE_VOLATILE_P (type
) | TREE_THIS_VOLATILE (array
));
3132 ret
= require_complete_type_sfinae (fold_if_not_in_template (rval
),
3134 protected_set_expr_location (ret
, loc
);
3139 tree ar
= cp_default_conversion (array
, complain
);
3140 tree ind
= cp_default_conversion (idx
, complain
);
3142 /* Put the integer in IND to simplify error checking. */
3143 if (TREE_CODE (TREE_TYPE (ar
)) == INTEGER_TYPE
)
3150 if (ar
== error_mark_node
|| ind
== error_mark_node
)
3151 return error_mark_node
;
3153 if (!TYPE_PTR_P (TREE_TYPE (ar
)))
3155 if (complain
& tf_error
)
3156 error_at (loc
, "subscripted value is neither array nor pointer");
3157 return error_mark_node
;
3159 if (TREE_CODE (TREE_TYPE (ind
)) != INTEGER_TYPE
)
3161 if (complain
& tf_error
)
3162 error_at (loc
, "array subscript is not an integer");
3163 return error_mark_node
;
3166 warn_array_subscript_with_type_char (idx
);
3168 ret
= cp_build_indirect_ref (cp_build_binary_op (input_location
,
3173 protected_set_expr_location (ret
, loc
);
3178 /* Entry point for Obj-C++. */
3181 build_array_ref (location_t loc
, tree array
, tree idx
)
3183 return cp_build_array_ref (loc
, array
, idx
, tf_warning_or_error
);
3186 /* Resolve a pointer to member function. INSTANCE is the object
3187 instance to use, if the member points to a virtual member.
3189 This used to avoid checking for virtual functions if basetype
3190 has no virtual functions, according to an earlier ANSI draft.
3191 With the final ISO C++ rules, such an optimization is
3192 incorrect: A pointer to a derived member can be static_cast
3193 to pointer-to-base-member, as long as the dynamic object
3194 later has the right member. So now we only do this optimization
3195 when we know the dynamic type of the object. */
3198 get_member_function_from_ptrfunc (tree
*instance_ptrptr
, tree function
,
3199 tsubst_flags_t complain
)
3201 if (TREE_CODE (function
) == OFFSET_REF
)
3202 function
= TREE_OPERAND (function
, 1);
3204 if (TYPE_PTRMEMFUNC_P (TREE_TYPE (function
)))
3206 tree idx
, delta
, e1
, e2
, e3
, vtbl
;
3208 tree fntype
= TYPE_PTRMEMFUNC_FN_TYPE (TREE_TYPE (function
));
3209 tree basetype
= TYPE_METHOD_BASETYPE (TREE_TYPE (fntype
));
3211 tree instance_ptr
= *instance_ptrptr
;
3212 tree instance_save_expr
= 0;
3213 if (instance_ptr
== error_mark_node
)
3215 if (TREE_CODE (function
) == PTRMEM_CST
)
3217 /* Extracting the function address from a pmf is only
3218 allowed with -Wno-pmf-conversions. It only works for
3220 e1
= build_addr_func (PTRMEM_CST_MEMBER (function
), complain
);
3221 e1
= convert (fntype
, e1
);
3226 if (complain
& tf_error
)
3227 error ("object missing in use of %qE", function
);
3228 return error_mark_node
;
3232 /* True if we know that the dynamic type of the object doesn't have
3233 virtual functions, so we can assume the PFN field is a pointer. */
3234 nonvirtual
= (COMPLETE_TYPE_P (basetype
)
3235 && !TYPE_POLYMORPHIC_P (basetype
)
3236 && resolves_to_fixed_type_p (instance_ptr
, 0));
3238 if (TREE_SIDE_EFFECTS (instance_ptr
))
3239 instance_ptr
= instance_save_expr
= save_expr (instance_ptr
);
3241 if (TREE_SIDE_EFFECTS (function
))
3242 function
= save_expr (function
);
3244 /* Start by extracting all the information from the PMF itself. */
3245 e3
= pfn_from_ptrmemfunc (function
);
3246 delta
= delta_from_ptrmemfunc (function
);
3247 idx
= build1 (NOP_EXPR
, vtable_index_type
, e3
);
3248 switch (TARGET_PTRMEMFUNC_VBIT_LOCATION
)
3250 case ptrmemfunc_vbit_in_pfn
:
3251 e1
= cp_build_binary_op (input_location
,
3252 BIT_AND_EXPR
, idx
, integer_one_node
,
3254 idx
= cp_build_binary_op (input_location
,
3255 MINUS_EXPR
, idx
, integer_one_node
,
3257 if (idx
== error_mark_node
)
3258 return error_mark_node
;
3261 case ptrmemfunc_vbit_in_delta
:
3262 e1
= cp_build_binary_op (input_location
,
3263 BIT_AND_EXPR
, delta
, integer_one_node
,
3265 delta
= cp_build_binary_op (input_location
,
3266 RSHIFT_EXPR
, delta
, integer_one_node
,
3268 if (delta
== error_mark_node
)
3269 return error_mark_node
;
3276 if (e1
== error_mark_node
)
3277 return error_mark_node
;
3279 /* Convert down to the right base before using the instance. A
3280 special case is that in a pointer to member of class C, C may
3281 be incomplete. In that case, the function will of course be
3282 a member of C, and no conversion is required. In fact,
3283 lookup_base will fail in that case, because incomplete
3284 classes do not have BINFOs. */
3285 if (!same_type_ignoring_top_level_qualifiers_p
3286 (basetype
, TREE_TYPE (TREE_TYPE (instance_ptr
))))
3288 basetype
= lookup_base (TREE_TYPE (TREE_TYPE (instance_ptr
)),
3289 basetype
, ba_check
, NULL
, complain
);
3290 instance_ptr
= build_base_path (PLUS_EXPR
, instance_ptr
, basetype
,
3292 if (instance_ptr
== error_mark_node
)
3293 return error_mark_node
;
3295 /* ...and then the delta in the PMF. */
3296 instance_ptr
= fold_build_pointer_plus (instance_ptr
, delta
);
3298 /* Hand back the adjusted 'this' argument to our caller. */
3299 *instance_ptrptr
= instance_ptr
;
3302 /* Now just return the pointer. */
3305 /* Next extract the vtable pointer from the object. */
3306 vtbl
= build1 (NOP_EXPR
, build_pointer_type (vtbl_ptr_type_node
),
3308 vtbl
= cp_build_indirect_ref (vtbl
, RO_NULL
, complain
);
3309 if (vtbl
== error_mark_node
)
3310 return error_mark_node
;
3312 /* Finally, extract the function pointer from the vtable. */
3313 e2
= fold_build_pointer_plus_loc (input_location
, vtbl
, idx
);
3314 e2
= cp_build_indirect_ref (e2
, RO_NULL
, complain
);
3315 if (e2
== error_mark_node
)
3316 return error_mark_node
;
3317 TREE_CONSTANT (e2
) = 1;
3319 /* When using function descriptors, the address of the
3320 vtable entry is treated as a function pointer. */
3321 if (TARGET_VTABLE_USES_DESCRIPTORS
)
3322 e2
= build1 (NOP_EXPR
, TREE_TYPE (e2
),
3323 cp_build_addr_expr (e2
, complain
));
3325 e2
= fold_convert (TREE_TYPE (e3
), e2
);
3326 e1
= build_conditional_expr (input_location
, e1
, e2
, e3
, complain
);
3327 if (e1
== error_mark_node
)
3328 return error_mark_node
;
3330 /* Make sure this doesn't get evaluated first inside one of the
3331 branches of the COND_EXPR. */
3332 if (instance_save_expr
)
3333 e1
= build2 (COMPOUND_EXPR
, TREE_TYPE (e1
),
3334 instance_save_expr
, e1
);
3341 /* Used by the C-common bits. */
3343 build_function_call (location_t
/*loc*/,
3344 tree function
, tree params
)
3346 return cp_build_function_call (function
, params
, tf_warning_or_error
);
3349 /* Used by the C-common bits. */
3351 build_function_call_vec (location_t
/*loc*/,
3352 tree function
, vec
<tree
, va_gc
> *params
,
3353 vec
<tree
, va_gc
> * /*origtypes*/)
3355 vec
<tree
, va_gc
> *orig_params
= params
;
3356 tree ret
= cp_build_function_call_vec (function
, ¶ms
,
3357 tf_warning_or_error
);
3359 /* cp_build_function_call_vec can reallocate PARAMS by adding
3360 default arguments. That should never happen here. Verify
3362 gcc_assert (params
== orig_params
);
3367 /* Build a function call using a tree list of arguments. */
3370 cp_build_function_call (tree function
, tree params
, tsubst_flags_t complain
)
3372 vec
<tree
, va_gc
> *vec
;
3375 vec
= make_tree_vector ();
3376 for (; params
!= NULL_TREE
; params
= TREE_CHAIN (params
))
3377 vec_safe_push (vec
, TREE_VALUE (params
));
3378 ret
= cp_build_function_call_vec (function
, &vec
, complain
);
3379 release_tree_vector (vec
);
3383 /* Build a function call using varargs. */
3386 cp_build_function_call_nary (tree function
, tsubst_flags_t complain
, ...)
3388 vec
<tree
, va_gc
> *vec
;
3392 vec
= make_tree_vector ();
3393 va_start (args
, complain
);
3394 for (t
= va_arg (args
, tree
); t
!= NULL_TREE
; t
= va_arg (args
, tree
))
3395 vec_safe_push (vec
, t
);
3397 ret
= cp_build_function_call_vec (function
, &vec
, complain
);
3398 release_tree_vector (vec
);
3402 /* Build a function call using a vector of arguments. PARAMS may be
3403 NULL if there are no parameters. This changes the contents of
3407 cp_build_function_call_vec (tree function
, vec
<tree
, va_gc
> **params
,
3408 tsubst_flags_t complain
)
3410 tree fntype
, fndecl
;
3412 tree original
= function
;
3416 vec
<tree
, va_gc
> *allocated
= NULL
;
3419 /* For Objective-C, convert any calls via a cast to OBJC_TYPE_REF
3420 expressions, like those used for ObjC messenger dispatches. */
3421 if (params
!= NULL
&& !vec_safe_is_empty (*params
))
3422 function
= objc_rewrite_function_call (function
, (**params
)[0]);
3424 /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
3425 Strip such NOP_EXPRs, since FUNCTION is used in non-lvalue context. */
3426 if (TREE_CODE (function
) == NOP_EXPR
3427 && TREE_TYPE (function
) == TREE_TYPE (TREE_OPERAND (function
, 0)))
3428 function
= TREE_OPERAND (function
, 0);
3430 if (TREE_CODE (function
) == FUNCTION_DECL
)
3432 mark_used (function
);
3435 /* Convert anything with function type to a pointer-to-function. */
3436 if (DECL_MAIN_P (function
) && (complain
& tf_error
))
3437 pedwarn (input_location
, OPT_Wpedantic
,
3438 "ISO C++ forbids calling %<::main%> from within program");
3440 function
= build_addr_func (function
, complain
);
3446 function
= build_addr_func (function
, complain
);
3449 if (function
== error_mark_node
)
3450 return error_mark_node
;
3452 fntype
= TREE_TYPE (function
);
3454 if (TYPE_PTRMEMFUNC_P (fntype
))
3456 if (complain
& tf_error
)
3457 error ("must use %<.*%> or %<->*%> to call pointer-to-member "
3458 "function in %<%E (...)%>, e.g. %<(... ->* %E) (...)%>",
3459 original
, original
);
3460 return error_mark_node
;
3463 is_method
= (TYPE_PTR_P (fntype
)
3464 && TREE_CODE (TREE_TYPE (fntype
)) == METHOD_TYPE
);
3466 if (!(TYPE_PTRFN_P (fntype
)
3468 || TREE_CODE (function
) == TEMPLATE_ID_EXPR
))
3470 if (complain
& tf_error
)
3472 if (!flag_diagnostics_show_caret
)
3473 error_at (input_location
,
3474 "%qE cannot be used as a function", original
);
3475 else if (DECL_P (original
))
3476 error_at (input_location
,
3477 "%qD cannot be used as a function", original
);
3479 error_at (input_location
,
3480 "expression cannot be used as a function");
3483 return error_mark_node
;
3486 /* fntype now gets the type of function pointed to. */
3487 fntype
= TREE_TYPE (fntype
);
3488 parm_types
= TYPE_ARG_TYPES (fntype
);
3492 allocated
= make_tree_vector ();
3493 params
= &allocated
;
3496 nargs
= convert_arguments (parm_types
, params
, fndecl
, LOOKUP_NORMAL
,
3499 return error_mark_node
;
3501 argarray
= (*params
)->address ();
3503 /* Check for errors in format strings and inappropriately
3505 check_function_arguments (fntype
, nargs
, argarray
);
3507 ret
= build_cxx_call (function
, nargs
, argarray
, complain
);
3509 if (allocated
!= NULL
)
3510 release_tree_vector (allocated
);
3515 /* Subroutine of convert_arguments.
3516 Warn about wrong number of args are genereted. */
3519 warn_args_num (location_t loc
, tree fndecl
, bool too_many_p
)
3523 if (TREE_CODE (TREE_TYPE (fndecl
)) == METHOD_TYPE
)
3525 if (DECL_NAME (fndecl
) == NULL_TREE
3526 || IDENTIFIER_HAS_TYPE_VALUE (DECL_NAME (fndecl
)))
3529 ? G_("too many arguments to constructor %q#D")
3530 : G_("too few arguments to constructor %q#D"),
3535 ? G_("too many arguments to member function %q#D")
3536 : G_("too few arguments to member function %q#D"),
3542 ? G_("too many arguments to function %q#D")
3543 : G_("too few arguments to function %q#D"),
3545 inform (DECL_SOURCE_LOCATION (fndecl
),
3550 if (c_dialect_objc () && objc_message_selector ())
3553 ? G_("too many arguments to method %q#D")
3554 : G_("too few arguments to method %q#D"),
3555 objc_message_selector ());
3557 error_at (loc
, too_many_p
? G_("too many arguments to function")
3558 : G_("too few arguments to function"));
3562 /* Convert the actual parameter expressions in the list VALUES to the
3563 types in the list TYPELIST. The converted expressions are stored
3564 back in the VALUES vector.
3565 If parmdecls is exhausted, or when an element has NULL as its type,
3566 perform the default conversions.
3568 NAME is an IDENTIFIER_NODE or 0. It is used only for error messages.
3570 This is also where warnings about wrong number of args are generated.
3572 Returns the actual number of arguments processed (which might be less
3573 than the length of the vector), or -1 on error.
3575 In C++, unspecified trailing parameters can be filled in with their
3576 default arguments, if such were specified. Do so here. */
3579 convert_arguments (tree typelist
, vec
<tree
, va_gc
> **values
, tree fndecl
,
3580 int flags
, tsubst_flags_t complain
)
3585 /* Argument passing is always copy-initialization. */
3586 flags
|= LOOKUP_ONLYCONVERTING
;
3588 for (i
= 0, typetail
= typelist
;
3589 i
< vec_safe_length (*values
);
3592 tree type
= typetail
? TREE_VALUE (typetail
) : 0;
3593 tree val
= (**values
)[i
];
3595 if (val
== error_mark_node
|| type
== error_mark_node
)
3598 if (type
== void_type_node
)
3600 if (complain
& tf_error
)
3602 warn_args_num (input_location
, fndecl
, /*too_many_p=*/true);
3609 /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
3610 Strip such NOP_EXPRs, since VAL is used in non-lvalue context. */
3611 if (TREE_CODE (val
) == NOP_EXPR
3612 && TREE_TYPE (val
) == TREE_TYPE (TREE_OPERAND (val
, 0))
3613 && (type
== 0 || TREE_CODE (type
) != REFERENCE_TYPE
))
3614 val
= TREE_OPERAND (val
, 0);
3616 if (type
== 0 || TREE_CODE (type
) != REFERENCE_TYPE
)
3618 if (TREE_CODE (TREE_TYPE (val
)) == ARRAY_TYPE
3619 || TREE_CODE (TREE_TYPE (val
)) == FUNCTION_TYPE
3620 || TREE_CODE (TREE_TYPE (val
)) == METHOD_TYPE
)
3621 val
= decay_conversion (val
, complain
);
3624 if (val
== error_mark_node
)
3629 /* Formal parm type is specified by a function prototype. */
3632 if (!COMPLETE_TYPE_P (complete_type (type
)))
3634 if (complain
& tf_error
)
3637 error ("parameter %P of %qD has incomplete type %qT",
3640 error ("parameter %P has incomplete type %qT", i
, type
);
3642 parmval
= error_mark_node
;
3646 parmval
= convert_for_initialization
3647 (NULL_TREE
, type
, val
, flags
,
3648 ICR_ARGPASS
, fndecl
, i
, complain
);
3649 parmval
= convert_for_arg_passing (type
, parmval
, complain
);
3652 if (parmval
== error_mark_node
)
3655 (**values
)[i
] = parmval
;
3659 if (fndecl
&& magic_varargs_p (fndecl
))
3660 /* Don't do ellipsis conversion for __built_in_constant_p
3661 as this will result in spurious errors for non-trivial
3663 val
= require_complete_type_sfinae (val
, complain
);
3665 val
= convert_arg_to_ellipsis (val
, complain
);
3667 (**values
)[i
] = val
;
3671 typetail
= TREE_CHAIN (typetail
);
3674 if (typetail
!= 0 && typetail
!= void_list_node
)
3676 /* See if there are default arguments that can be used. Because
3677 we hold default arguments in the FUNCTION_TYPE (which is so
3678 wrong), we can see default parameters here from deduced
3679 contexts (and via typeof) for indirect function calls.
3680 Fortunately we know whether we have a function decl to
3681 provide default arguments in a language conformant
3683 if (fndecl
&& TREE_PURPOSE (typetail
)
3684 && TREE_CODE (TREE_PURPOSE (typetail
)) != DEFAULT_ARG
)
3686 for (; typetail
!= void_list_node
; ++i
)
3689 = convert_default_arg (TREE_VALUE (typetail
),
3690 TREE_PURPOSE (typetail
),
3691 fndecl
, i
, complain
);
3693 if (parmval
== error_mark_node
)
3696 vec_safe_push (*values
, parmval
);
3697 typetail
= TREE_CHAIN (typetail
);
3698 /* ends with `...'. */
3699 if (typetail
== NULL_TREE
)
3705 if (complain
& tf_error
)
3706 warn_args_num (input_location
, fndecl
, /*too_many_p=*/false);
3714 /* Build a binary-operation expression, after performing default
3715 conversions on the operands. CODE is the kind of expression to
3716 build. ARG1 and ARG2 are the arguments. ARG1_CODE and ARG2_CODE
3717 are the tree codes which correspond to ARG1 and ARG2 when issuing
3718 warnings about possibly misplaced parentheses. They may differ
3719 from the TREE_CODE of ARG1 and ARG2 if the parser has done constant
3720 folding (e.g., if the parser sees "a | 1 + 1", it may call this
3721 routine with ARG2 being an INTEGER_CST and ARG2_CODE == PLUS_EXPR).
3722 To avoid issuing any parentheses warnings, pass ARG1_CODE and/or
3723 ARG2_CODE as ERROR_MARK. */
3726 build_x_binary_op (location_t loc
, enum tree_code code
, tree arg1
,
3727 enum tree_code arg1_code
, tree arg2
,
3728 enum tree_code arg2_code
, tree
*overload
,
3729 tsubst_flags_t complain
)
3738 if (processing_template_decl
)
3740 if (type_dependent_expression_p (arg1
)
3741 || type_dependent_expression_p (arg2
))
3742 return build_min_nt_loc (loc
, code
, arg1
, arg2
);
3743 arg1
= build_non_dependent_expr (arg1
);
3744 arg2
= build_non_dependent_expr (arg2
);
3747 if (code
== DOTSTAR_EXPR
)
3748 expr
= build_m_component_ref (arg1
, arg2
, complain
);
3750 expr
= build_new_op (loc
, code
, LOOKUP_NORMAL
, arg1
, arg2
, NULL_TREE
,
3751 overload
, complain
);
3753 /* Check for cases such as x+y<<z which users are likely to
3754 misinterpret. But don't warn about obj << x + y, since that is a
3755 common idiom for I/O. */
3756 if (warn_parentheses
3757 && (complain
& tf_warning
)
3758 && !processing_template_decl
3759 && !error_operand_p (arg1
)
3760 && !error_operand_p (arg2
)
3761 && (code
!= LSHIFT_EXPR
3762 || !CLASS_TYPE_P (TREE_TYPE (arg1
))))
3763 warn_about_parentheses (loc
, code
, arg1_code
, orig_arg1
,
3764 arg2_code
, orig_arg2
);
3766 if (processing_template_decl
&& expr
!= error_mark_node
)
3767 return build_min_non_dep (code
, expr
, orig_arg1
, orig_arg2
);
3772 /* Build and return an ARRAY_REF expression. */
3775 build_x_array_ref (location_t loc
, tree arg1
, tree arg2
,
3776 tsubst_flags_t complain
)
3778 tree orig_arg1
= arg1
;
3779 tree orig_arg2
= arg2
;
3782 if (processing_template_decl
)
3784 if (type_dependent_expression_p (arg1
)
3785 || type_dependent_expression_p (arg2
))
3786 return build_min_nt_loc (loc
, ARRAY_REF
, arg1
, arg2
,
3787 NULL_TREE
, NULL_TREE
);
3788 arg1
= build_non_dependent_expr (arg1
);
3789 arg2
= build_non_dependent_expr (arg2
);
3792 expr
= build_new_op (loc
, ARRAY_REF
, LOOKUP_NORMAL
, arg1
, arg2
,
3793 NULL_TREE
, /*overload=*/NULL
, complain
);
3795 if (processing_template_decl
&& expr
!= error_mark_node
)
3796 return build_min_non_dep (ARRAY_REF
, expr
, orig_arg1
, orig_arg2
,
3797 NULL_TREE
, NULL_TREE
);
3801 /* Return whether OP is an expression of enum type cast to integer
3802 type. In C++ even unsigned enum types are cast to signed integer
3803 types. We do not want to issue warnings about comparisons between
3804 signed and unsigned types when one of the types is an enum type.
3805 Those warnings are always false positives in practice. */
3808 enum_cast_to_int (tree op
)
3810 if (TREE_CODE (op
) == NOP_EXPR
3811 && TREE_TYPE (op
) == integer_type_node
3812 && TREE_CODE (TREE_TYPE (TREE_OPERAND (op
, 0))) == ENUMERAL_TYPE
3813 && TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (op
, 0))))
3816 /* The cast may have been pushed into a COND_EXPR. */
3817 if (TREE_CODE (op
) == COND_EXPR
)
3818 return (enum_cast_to_int (TREE_OPERAND (op
, 1))
3819 || enum_cast_to_int (TREE_OPERAND (op
, 2)));
3824 /* For the c-common bits. */
3826 build_binary_op (location_t location
, enum tree_code code
, tree op0
, tree op1
,
3829 return cp_build_binary_op (location
, code
, op0
, op1
, tf_warning_or_error
);
3833 /* Build a binary-operation expression without default conversions.
3834 CODE is the kind of expression to build.
3835 LOCATION is the location_t of the operator in the source code.
3836 This function differs from `build' in several ways:
3837 the data type of the result is computed and recorded in it,
3838 warnings are generated if arg data types are invalid,
3839 special handling for addition and subtraction of pointers is known,
3840 and some optimization is done (operations on narrow ints
3841 are done in the narrower type when that gives the same result).
3842 Constant folding is also done before the result is returned.
3844 Note that the operands will never have enumeral types
3845 because either they have just had the default conversions performed
3846 or they have both just been converted to some other type in which
3847 the arithmetic is to be done.
3849 C++: must do special pointer arithmetic when implementing
3850 multiple inheritance, and deal with pointer to member functions. */
3853 cp_build_binary_op (location_t location
,
3854 enum tree_code code
, tree orig_op0
, tree orig_op1
,
3855 tsubst_flags_t complain
)
3858 enum tree_code code0
, code1
;
3860 const char *invalid_op_diag
;
3862 /* Expression code to give to the expression when it is built.
3863 Normally this is CODE, which is what the caller asked for,
3864 but in some special cases we change it. */
3865 enum tree_code resultcode
= code
;
3867 /* Data type in which the computation is to be performed.
3868 In the simplest cases this is the common type of the arguments. */
3869 tree result_type
= NULL
;
3871 /* Nonzero means operands have already been type-converted
3872 in whatever way is necessary.
3873 Zero means they need to be converted to RESULT_TYPE. */
3876 /* Nonzero means create the expression with this type, rather than
3878 tree build_type
= 0;
3880 /* Nonzero means after finally constructing the expression
3881 convert it to this type. */
3882 tree final_type
= 0;
3886 /* Nonzero if this is an operation like MIN or MAX which can
3887 safely be computed in short if both args are promoted shorts.
3888 Also implies COMMON.
3889 -1 indicates a bitwise operation; this makes a difference
3890 in the exact conditions for when it is safe to do the operation
3891 in a narrower mode. */
3894 /* Nonzero if this is a comparison operation;
3895 if both args are promoted shorts, compare the original shorts.
3896 Also implies COMMON. */
3897 int short_compare
= 0;
3899 /* Nonzero means set RESULT_TYPE to the common type of the args. */
3902 /* True if both operands have arithmetic type. */
3903 bool arithmetic_types_p
;
3905 /* Apply default conversions. */
3909 if (code
== TRUTH_AND_EXPR
|| code
== TRUTH_ANDIF_EXPR
3910 || code
== TRUTH_OR_EXPR
|| code
== TRUTH_ORIF_EXPR
3911 || code
== TRUTH_XOR_EXPR
)
3913 if (!really_overloaded_fn (op0
) && !VOID_TYPE_P (TREE_TYPE (op0
)))
3914 op0
= decay_conversion (op0
, complain
);
3915 if (!really_overloaded_fn (op1
) && !VOID_TYPE_P (TREE_TYPE (op1
)))
3916 op1
= decay_conversion (op1
, complain
);
3920 if (!really_overloaded_fn (op0
) && !VOID_TYPE_P (TREE_TYPE (op0
)))
3921 op0
= cp_default_conversion (op0
, complain
);
3922 if (!really_overloaded_fn (op1
) && !VOID_TYPE_P (TREE_TYPE (op1
)))
3923 op1
= cp_default_conversion (op1
, complain
);
3926 /* Strip NON_LVALUE_EXPRs, etc., since we aren't using as an lvalue. */
3927 STRIP_TYPE_NOPS (op0
);
3928 STRIP_TYPE_NOPS (op1
);
3930 /* DTRT if one side is an overloaded function, but complain about it. */
3931 if (type_unknown_p (op0
))
3933 tree t
= instantiate_type (TREE_TYPE (op1
), op0
, tf_none
);
3934 if (t
!= error_mark_node
)
3936 if (complain
& tf_error
)
3937 permerror (input_location
, "assuming cast to type %qT from overloaded function",
3942 if (type_unknown_p (op1
))
3944 tree t
= instantiate_type (TREE_TYPE (op0
), op1
, tf_none
);
3945 if (t
!= error_mark_node
)
3947 if (complain
& tf_error
)
3948 permerror (input_location
, "assuming cast to type %qT from overloaded function",
3954 type0
= TREE_TYPE (op0
);
3955 type1
= TREE_TYPE (op1
);
3957 /* The expression codes of the data types of the arguments tell us
3958 whether the arguments are integers, floating, pointers, etc. */
3959 code0
= TREE_CODE (type0
);
3960 code1
= TREE_CODE (type1
);
3962 /* If an error was already reported for one of the arguments,
3963 avoid reporting another error. */
3964 if (code0
== ERROR_MARK
|| code1
== ERROR_MARK
)
3965 return error_mark_node
;
3967 if ((invalid_op_diag
3968 = targetm
.invalid_binary_op (code
, type0
, type1
)))
3970 if (complain
& tf_error
)
3971 error (invalid_op_diag
);
3972 return error_mark_node
;
3975 /* Issue warnings about peculiar, but valid, uses of NULL. */
3976 if ((orig_op0
== null_node
|| orig_op1
== null_node
)
3977 /* It's reasonable to use pointer values as operands of &&
3978 and ||, so NULL is no exception. */
3979 && code
!= TRUTH_ANDIF_EXPR
&& code
!= TRUTH_ORIF_EXPR
3980 && ( /* Both are NULL (or 0) and the operation was not a
3981 comparison or a pointer subtraction. */
3982 (null_ptr_cst_p (orig_op0
) && null_ptr_cst_p (orig_op1
)
3983 && code
!= EQ_EXPR
&& code
!= NE_EXPR
&& code
!= MINUS_EXPR
)
3984 /* Or if one of OP0 or OP1 is neither a pointer nor NULL. */
3985 || (!null_ptr_cst_p (orig_op0
)
3986 && !TYPE_PTR_OR_PTRMEM_P (type0
))
3987 || (!null_ptr_cst_p (orig_op1
)
3988 && !TYPE_PTR_OR_PTRMEM_P (type1
)))
3989 && (complain
& tf_warning
))
3991 source_location loc
=
3992 expansion_point_location_if_in_system_header (input_location
);
3994 warning_at (loc
, OPT_Wpointer_arith
, "NULL used in arithmetic");
3997 /* In case when one of the operands of the binary operation is
3998 a vector and another is a scalar -- convert scalar to vector. */
3999 if ((code0
== VECTOR_TYPE
) != (code1
== VECTOR_TYPE
))
4001 enum stv_conv convert_flag
= scalar_to_vector (location
, code
, op0
, op1
,
4002 complain
& tf_error
);
4004 switch (convert_flag
)
4007 return error_mark_node
;
4010 op0
= save_expr (op0
);
4011 op0
= convert (TREE_TYPE (type1
), op0
);
4012 op0
= build_vector_from_val (type1
, op0
);
4013 type0
= TREE_TYPE (op0
);
4014 code0
= TREE_CODE (type0
);
4020 op1
= save_expr (op1
);
4021 op1
= convert (TREE_TYPE (type0
), op1
);
4022 op1
= build_vector_from_val (type0
, op1
);
4023 type1
= TREE_TYPE (op1
);
4024 code1
= TREE_CODE (type1
);
4036 /* Subtraction of two similar pointers.
4037 We must subtract them as integers, then divide by object size. */
4038 if (code0
== POINTER_TYPE
&& code1
== POINTER_TYPE
4039 && same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (type0
),
4041 return pointer_diff (op0
, op1
, common_pointer_type (type0
, type1
),
4043 /* In all other cases except pointer - int, the usual arithmetic
4045 else if (!(code0
== POINTER_TYPE
&& code1
== INTEGER_TYPE
))
4050 /* The pointer - int case is just like pointer + int; fall
4053 if ((code0
== POINTER_TYPE
|| code1
== POINTER_TYPE
)
4054 && (code0
== INTEGER_TYPE
|| code1
== INTEGER_TYPE
))
4058 ptr_operand
= ((code0
== POINTER_TYPE
) ? op0
: op1
);
4059 int_operand
= ((code0
== INTEGER_TYPE
) ? op0
: op1
);
4060 if (processing_template_decl
)
4062 result_type
= TREE_TYPE (ptr_operand
);
4065 return cp_pointer_int_sum (code
,
4076 case TRUNC_DIV_EXPR
:
4078 case FLOOR_DIV_EXPR
:
4079 case ROUND_DIV_EXPR
:
4080 case EXACT_DIV_EXPR
:
4081 if ((code0
== INTEGER_TYPE
|| code0
== REAL_TYPE
4082 || code0
== COMPLEX_TYPE
|| code0
== VECTOR_TYPE
)
4083 && (code1
== INTEGER_TYPE
|| code1
== REAL_TYPE
4084 || code1
== COMPLEX_TYPE
|| code1
== VECTOR_TYPE
))
4086 enum tree_code tcode0
= code0
, tcode1
= code1
;
4087 tree cop1
= fold_non_dependent_expr_sfinae (op1
, tf_none
);
4089 warn_for_div_by_zero (location
, maybe_constant_value (cop1
));
4091 if (tcode0
== COMPLEX_TYPE
|| tcode0
== VECTOR_TYPE
)
4092 tcode0
= TREE_CODE (TREE_TYPE (TREE_TYPE (op0
)));
4093 if (tcode1
== COMPLEX_TYPE
|| tcode1
== VECTOR_TYPE
)
4094 tcode1
= TREE_CODE (TREE_TYPE (TREE_TYPE (op1
)));
4096 if (!(tcode0
== INTEGER_TYPE
&& tcode1
== INTEGER_TYPE
))
4097 resultcode
= RDIV_EXPR
;
4099 /* When dividing two signed integers, we have to promote to int.
4100 unless we divide by a constant != -1. Note that default
4101 conversion will have been performed on the operands at this
4102 point, so we have to dig out the original type to find out if
4104 shorten
= ((TREE_CODE (op0
) == NOP_EXPR
4105 && TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (op0
, 0))))
4106 || (TREE_CODE (op1
) == INTEGER_CST
4107 && ! integer_all_onesp (op1
)));
4116 if ((code0
== INTEGER_TYPE
&& code1
== INTEGER_TYPE
)
4117 || (code0
== VECTOR_TYPE
&& code1
== VECTOR_TYPE
4118 && !VECTOR_FLOAT_TYPE_P (type0
)
4119 && !VECTOR_FLOAT_TYPE_P (type1
)))
4123 case TRUNC_MOD_EXPR
:
4124 case FLOOR_MOD_EXPR
:
4126 tree cop1
= fold_non_dependent_expr_sfinae (op1
, tf_none
);
4128 warn_for_div_by_zero (location
, maybe_constant_value (cop1
));
4131 if (code0
== VECTOR_TYPE
&& code1
== VECTOR_TYPE
4132 && TREE_CODE (TREE_TYPE (type0
)) == INTEGER_TYPE
4133 && TREE_CODE (TREE_TYPE (type1
)) == INTEGER_TYPE
)
4135 else if (code0
== INTEGER_TYPE
&& code1
== INTEGER_TYPE
)
4137 /* Although it would be tempting to shorten always here, that loses
4138 on some targets, since the modulo instruction is undefined if the
4139 quotient can't be represented in the computation mode. We shorten
4140 only if unsigned or if dividing by something we know != -1. */
4141 shorten
= ((TREE_CODE (op0
) == NOP_EXPR
4142 && TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (op0
, 0))))
4143 || (TREE_CODE (op1
) == INTEGER_CST
4144 && ! integer_all_onesp (op1
)));
4149 case TRUTH_ANDIF_EXPR
:
4150 case TRUTH_ORIF_EXPR
:
4151 case TRUTH_AND_EXPR
:
4153 result_type
= boolean_type_node
;
4156 /* Shift operations: result has same type as first operand;
4157 always convert second operand to int.
4158 Also set SHORT_SHIFT if shifting rightward. */
4161 if (code0
== VECTOR_TYPE
&& code1
== INTEGER_TYPE
4162 && TREE_CODE (TREE_TYPE (type0
)) == INTEGER_TYPE
)
4164 result_type
= type0
;
4167 else if (code0
== VECTOR_TYPE
&& code1
== VECTOR_TYPE
4168 && TREE_CODE (TREE_TYPE (type0
)) == INTEGER_TYPE
4169 && TREE_CODE (TREE_TYPE (type1
)) == INTEGER_TYPE
4170 && TYPE_VECTOR_SUBPARTS (type0
) == TYPE_VECTOR_SUBPARTS (type1
))
4172 result_type
= type0
;
4175 else if (code0
== INTEGER_TYPE
&& code1
== INTEGER_TYPE
)
4177 tree const_op1
= fold_non_dependent_expr_sfinae (op1
, tf_none
);
4178 const_op1
= maybe_constant_value (const_op1
);
4179 if (TREE_CODE (const_op1
) != INTEGER_CST
)
4181 result_type
= type0
;
4182 if (TREE_CODE (const_op1
) == INTEGER_CST
)
4184 if (tree_int_cst_lt (const_op1
, integer_zero_node
))
4186 if ((complain
& tf_warning
)
4187 && c_inhibit_evaluation_warnings
== 0)
4188 warning (0, "right shift count is negative");
4192 if (compare_tree_int (const_op1
, TYPE_PRECISION (type0
)) >= 0
4193 && (complain
& tf_warning
)
4194 && c_inhibit_evaluation_warnings
== 0)
4195 warning (0, "right shift count >= width of type");
4198 /* Convert the shift-count to an integer, regardless of
4199 size of value being shifted. */
4200 if (TYPE_MAIN_VARIANT (TREE_TYPE (op1
)) != integer_type_node
)
4201 op1
= cp_convert (integer_type_node
, op1
, complain
);
4202 /* Avoid converting op1 to result_type later. */
4208 if (code0
== VECTOR_TYPE
&& code1
== INTEGER_TYPE
4209 && TREE_CODE (TREE_TYPE (type0
)) == INTEGER_TYPE
)
4211 result_type
= type0
;
4214 else if (code0
== VECTOR_TYPE
&& code1
== VECTOR_TYPE
4215 && TREE_CODE (TREE_TYPE (type0
)) == INTEGER_TYPE
4216 && TREE_CODE (TREE_TYPE (type1
)) == INTEGER_TYPE
4217 && TYPE_VECTOR_SUBPARTS (type0
) == TYPE_VECTOR_SUBPARTS (type1
))
4219 result_type
= type0
;
4222 else if (code0
== INTEGER_TYPE
&& code1
== INTEGER_TYPE
)
4224 tree const_op1
= fold_non_dependent_expr_sfinae (op1
, tf_none
);
4225 const_op1
= maybe_constant_value (const_op1
);
4226 if (TREE_CODE (const_op1
) != INTEGER_CST
)
4228 result_type
= type0
;
4229 if (TREE_CODE (const_op1
) == INTEGER_CST
)
4231 if (tree_int_cst_lt (const_op1
, integer_zero_node
))
4233 if ((complain
& tf_warning
)
4234 && c_inhibit_evaluation_warnings
== 0)
4235 warning (0, "left shift count is negative");
4237 else if (compare_tree_int (const_op1
,
4238 TYPE_PRECISION (type0
)) >= 0)
4240 if ((complain
& tf_warning
)
4241 && c_inhibit_evaluation_warnings
== 0)
4242 warning (0, "left shift count >= width of type");
4245 /* Convert the shift-count to an integer, regardless of
4246 size of value being shifted. */
4247 if (TYPE_MAIN_VARIANT (TREE_TYPE (op1
)) != integer_type_node
)
4248 op1
= cp_convert (integer_type_node
, op1
, complain
);
4249 /* Avoid converting op1 to result_type later. */
4256 if (code0
== INTEGER_TYPE
&& code1
== INTEGER_TYPE
)
4258 result_type
= type0
;
4259 if (TREE_CODE (op1
) == INTEGER_CST
)
4261 if (tree_int_cst_lt (op1
, integer_zero_node
))
4263 if (complain
& tf_warning
)
4264 warning (0, (code
== LROTATE_EXPR
)
4265 ? G_("left rotate count is negative")
4266 : G_("right rotate count is negative"));
4268 else if (compare_tree_int (op1
, TYPE_PRECISION (type0
)) >= 0)
4270 if (complain
& tf_warning
)
4271 warning (0, (code
== LROTATE_EXPR
)
4272 ? G_("left rotate count >= width of type")
4273 : G_("right rotate count >= width of type"));
4276 /* Convert the shift-count to an integer, regardless of
4277 size of value being shifted. */
4278 if (TYPE_MAIN_VARIANT (TREE_TYPE (op1
)) != integer_type_node
)
4279 op1
= cp_convert (integer_type_node
, op1
, complain
);
4285 if (code0
== VECTOR_TYPE
&& code1
== VECTOR_TYPE
)
4286 goto vector_compare
;
4287 if ((complain
& tf_warning
)
4288 && (FLOAT_TYPE_P (type0
) || FLOAT_TYPE_P (type1
)))
4289 warning (OPT_Wfloat_equal
,
4290 "comparing floating point with == or != is unsafe");
4291 if ((complain
& tf_warning
)
4292 && ((TREE_CODE (orig_op0
) == STRING_CST
&& !integer_zerop (op1
))
4293 || (TREE_CODE (orig_op1
) == STRING_CST
&& !integer_zerop (op0
))))
4294 warning (OPT_Waddress
, "comparison with string literal results in unspecified behaviour");
4296 build_type
= boolean_type_node
;
4297 if ((code0
== INTEGER_TYPE
|| code0
== REAL_TYPE
4298 || code0
== COMPLEX_TYPE
|| code0
== ENUMERAL_TYPE
)
4299 && (code1
== INTEGER_TYPE
|| code1
== REAL_TYPE
4300 || code1
== COMPLEX_TYPE
|| code1
== ENUMERAL_TYPE
))
4302 else if ((code0
== POINTER_TYPE
&& code1
== POINTER_TYPE
)
4303 || (TYPE_PTRDATAMEM_P (type0
) && TYPE_PTRDATAMEM_P (type1
)))
4304 result_type
= composite_pointer_type (type0
, type1
, op0
, op1
,
4305 CPO_COMPARISON
, complain
);
4306 else if ((code0
== POINTER_TYPE
|| TYPE_PTRDATAMEM_P (type0
))
4307 && null_ptr_cst_p (op1
))
4309 if (TREE_CODE (op0
) == ADDR_EXPR
4310 && decl_with_nonnull_addr_p (TREE_OPERAND (op0
, 0)))
4312 if ((complain
& tf_warning
)
4313 && c_inhibit_evaluation_warnings
== 0)
4314 warning (OPT_Waddress
, "the address of %qD will never be NULL",
4315 TREE_OPERAND (op0
, 0));
4317 result_type
= type0
;
4319 else if ((code1
== POINTER_TYPE
|| TYPE_PTRDATAMEM_P (type1
))
4320 && null_ptr_cst_p (op0
))
4322 if (TREE_CODE (op1
) == ADDR_EXPR
4323 && decl_with_nonnull_addr_p (TREE_OPERAND (op1
, 0)))
4325 if ((complain
& tf_warning
)
4326 && c_inhibit_evaluation_warnings
== 0)
4327 warning (OPT_Waddress
, "the address of %qD will never be NULL",
4328 TREE_OPERAND (op1
, 0));
4330 result_type
= type1
;
4332 else if (null_ptr_cst_p (op0
) && null_ptr_cst_p (op1
))
4333 /* One of the operands must be of nullptr_t type. */
4334 result_type
= TREE_TYPE (nullptr_node
);
4335 else if (code0
== POINTER_TYPE
&& code1
== INTEGER_TYPE
)
4337 result_type
= type0
;
4338 if (complain
& tf_error
)
4339 permerror (input_location
, "ISO C++ forbids comparison between pointer and integer");
4341 return error_mark_node
;
4343 else if (code0
== INTEGER_TYPE
&& code1
== POINTER_TYPE
)
4345 result_type
= type1
;
4346 if (complain
& tf_error
)
4347 permerror (input_location
, "ISO C++ forbids comparison between pointer and integer");
4349 return error_mark_node
;
4351 else if (TYPE_PTRMEMFUNC_P (type0
) && null_ptr_cst_p (op1
))
4353 if (TARGET_PTRMEMFUNC_VBIT_LOCATION
4354 == ptrmemfunc_vbit_in_delta
)
4356 tree pfn0
, delta0
, e1
, e2
;
4358 if (TREE_SIDE_EFFECTS (op0
))
4359 op0
= save_expr (op0
);
4361 pfn0
= pfn_from_ptrmemfunc (op0
);
4362 delta0
= delta_from_ptrmemfunc (op0
);
4363 e1
= cp_build_binary_op (location
,
4366 build_zero_cst (TREE_TYPE (pfn0
)),
4368 e2
= cp_build_binary_op (location
,
4374 if (complain
& tf_warning
)
4375 maybe_warn_zero_as_null_pointer_constant (op1
, input_location
);
4377 e2
= cp_build_binary_op (location
,
4378 EQ_EXPR
, e2
, integer_zero_node
,
4380 op0
= cp_build_binary_op (location
,
4381 TRUTH_ANDIF_EXPR
, e1
, e2
,
4383 op1
= cp_convert (TREE_TYPE (op0
), integer_one_node
, complain
);
4387 op0
= build_ptrmemfunc_access_expr (op0
, pfn_identifier
);
4388 op1
= cp_convert (TREE_TYPE (op0
), op1
, complain
);
4390 result_type
= TREE_TYPE (op0
);
4392 else if (TYPE_PTRMEMFUNC_P (type1
) && null_ptr_cst_p (op0
))
4393 return cp_build_binary_op (location
, code
, op1
, op0
, complain
);
4394 else if (TYPE_PTRMEMFUNC_P (type0
) && TYPE_PTRMEMFUNC_P (type1
))
4397 /* E will be the final comparison. */
4399 /* E1 and E2 are for scratch. */
4407 type
= composite_pointer_type (type0
, type1
, op0
, op1
,
4408 CPO_COMPARISON
, complain
);
4410 if (!same_type_p (TREE_TYPE (op0
), type
))
4411 op0
= cp_convert_and_check (type
, op0
, complain
);
4412 if (!same_type_p (TREE_TYPE (op1
), type
))
4413 op1
= cp_convert_and_check (type
, op1
, complain
);
4415 if (op0
== error_mark_node
|| op1
== error_mark_node
)
4416 return error_mark_node
;
4418 if (TREE_SIDE_EFFECTS (op0
))
4419 op0
= save_expr (op0
);
4420 if (TREE_SIDE_EFFECTS (op1
))
4421 op1
= save_expr (op1
);
4423 pfn0
= pfn_from_ptrmemfunc (op0
);
4424 pfn1
= pfn_from_ptrmemfunc (op1
);
4425 delta0
= delta_from_ptrmemfunc (op0
);
4426 delta1
= delta_from_ptrmemfunc (op1
);
4427 if (TARGET_PTRMEMFUNC_VBIT_LOCATION
4428 == ptrmemfunc_vbit_in_delta
)
4433 && ((op0.delta == op1.delta)
4434 || (!op0.pfn && op0.delta & 1 == 0
4435 && op1.delta & 1 == 0))
4437 The reason for the `!op0.pfn' bit is that a NULL
4438 pointer-to-member is any member with a zero PFN and
4439 LSB of the DELTA field is 0. */
4441 e1
= cp_build_binary_op (location
, BIT_AND_EXPR
,
4445 e1
= cp_build_binary_op (location
,
4446 EQ_EXPR
, e1
, integer_zero_node
,
4448 e2
= cp_build_binary_op (location
, BIT_AND_EXPR
,
4452 e2
= cp_build_binary_op (location
,
4453 EQ_EXPR
, e2
, integer_zero_node
,
4455 e1
= cp_build_binary_op (location
,
4456 TRUTH_ANDIF_EXPR
, e2
, e1
,
4458 e2
= cp_build_binary_op (location
, EQ_EXPR
,
4460 build_zero_cst (TREE_TYPE (pfn0
)),
4462 e2
= cp_build_binary_op (location
,
4463 TRUTH_ANDIF_EXPR
, e2
, e1
, complain
);
4464 e1
= cp_build_binary_op (location
,
4465 EQ_EXPR
, delta0
, delta1
, complain
);
4466 e1
= cp_build_binary_op (location
,
4467 TRUTH_ORIF_EXPR
, e1
, e2
, complain
);
4474 && (!op0.pfn || op0.delta == op1.delta))
4476 The reason for the `!op0.pfn' bit is that a NULL
4477 pointer-to-member is any member with a zero PFN; the
4478 DELTA field is unspecified. */
4480 e1
= cp_build_binary_op (location
,
4481 EQ_EXPR
, delta0
, delta1
, complain
);
4482 e2
= cp_build_binary_op (location
,
4485 build_zero_cst (TREE_TYPE (pfn0
)),
4487 e1
= cp_build_binary_op (location
,
4488 TRUTH_ORIF_EXPR
, e1
, e2
, complain
);
4490 e2
= build2 (EQ_EXPR
, boolean_type_node
, pfn0
, pfn1
);
4491 e
= cp_build_binary_op (location
,
4492 TRUTH_ANDIF_EXPR
, e2
, e1
, complain
);
4493 if (code
== EQ_EXPR
)
4495 return cp_build_binary_op (location
,
4496 EQ_EXPR
, e
, integer_zero_node
, complain
);
4500 gcc_assert (!TYPE_PTRMEMFUNC_P (type0
)
4501 || !same_type_p (TYPE_PTRMEMFUNC_FN_TYPE (type0
),
4503 gcc_assert (!TYPE_PTRMEMFUNC_P (type1
)
4504 || !same_type_p (TYPE_PTRMEMFUNC_FN_TYPE (type1
),
4512 if ((code0
== INTEGER_TYPE
|| code0
== REAL_TYPE
)
4513 && (code1
== INTEGER_TYPE
|| code1
== REAL_TYPE
))
4515 else if (code0
== POINTER_TYPE
&& code1
== POINTER_TYPE
)
4516 result_type
= composite_pointer_type (type0
, type1
, op0
, op1
,
4517 CPO_COMPARISON
, complain
);
4524 if (TREE_CODE (orig_op0
) == STRING_CST
4525 || TREE_CODE (orig_op1
) == STRING_CST
)
4527 if (complain
& tf_warning
)
4528 warning (OPT_Waddress
, "comparison with string literal results in unspecified behaviour");
4531 if (code0
== VECTOR_TYPE
&& code1
== VECTOR_TYPE
)
4535 if (!same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (type0
),
4538 if (complain
& tf_error
)
4540 error_at (location
, "comparing vectors with different "
4542 inform (location
, "operand types are %qT and %qT",
4545 return error_mark_node
;
4548 if (TYPE_VECTOR_SUBPARTS (type0
) != TYPE_VECTOR_SUBPARTS (type1
))
4550 if (complain
& tf_error
)
4552 error_at (location
, "comparing vectors with different "
4553 "number of elements");
4554 inform (location
, "operand types are %qT and %qT",
4557 return error_mark_node
;
4560 /* Always construct signed integer vector type. */
4561 intt
= c_common_type_for_size (GET_MODE_BITSIZE
4562 (TYPE_MODE (TREE_TYPE (type0
))), 0);
4565 if (complain
& tf_error
)
4566 error_at (location
, "could not find an integer type "
4567 "of the same size as %qT", TREE_TYPE (type0
));
4568 return error_mark_node
;
4570 result_type
= build_opaque_vector_type (intt
,
4571 TYPE_VECTOR_SUBPARTS (type0
));
4575 build_type
= boolean_type_node
;
4576 if ((code0
== INTEGER_TYPE
|| code0
== REAL_TYPE
4577 || code0
== ENUMERAL_TYPE
)
4578 && (code1
== INTEGER_TYPE
|| code1
== REAL_TYPE
4579 || code1
== ENUMERAL_TYPE
))
4581 else if (code0
== POINTER_TYPE
&& code1
== POINTER_TYPE
)
4582 result_type
= composite_pointer_type (type0
, type1
, op0
, op1
,
4583 CPO_COMPARISON
, complain
);
4584 else if (code0
== POINTER_TYPE
&& null_ptr_cst_p (op1
))
4586 result_type
= type0
;
4587 if (extra_warnings
&& (complain
& tf_warning
))
4588 warning (OPT_Wextra
,
4589 "ordered comparison of pointer with integer zero");
4591 else if (code1
== POINTER_TYPE
&& null_ptr_cst_p (op0
))
4593 result_type
= type1
;
4594 if (extra_warnings
&& (complain
& tf_warning
))
4595 warning (OPT_Wextra
,
4596 "ordered comparison of pointer with integer zero");
4598 else if (null_ptr_cst_p (op0
) && null_ptr_cst_p (op1
))
4599 /* One of the operands must be of nullptr_t type. */
4600 result_type
= TREE_TYPE (nullptr_node
);
4601 else if (code0
== POINTER_TYPE
&& code1
== INTEGER_TYPE
)
4603 result_type
= type0
;
4604 if (complain
& tf_error
)
4605 permerror (input_location
, "ISO C++ forbids comparison between pointer and integer");
4607 return error_mark_node
;
4609 else if (code0
== INTEGER_TYPE
&& code1
== POINTER_TYPE
)
4611 result_type
= type1
;
4612 if (complain
& tf_error
)
4613 permerror (input_location
, "ISO C++ forbids comparison between pointer and integer");
4615 return error_mark_node
;
4619 case UNORDERED_EXPR
:
4626 build_type
= integer_type_node
;
4627 if (code0
!= REAL_TYPE
|| code1
!= REAL_TYPE
)
4629 if (complain
& tf_error
)
4630 error ("unordered comparison on non-floating point argument");
4631 return error_mark_node
;
4640 if (((code0
== INTEGER_TYPE
|| code0
== REAL_TYPE
|| code0
== COMPLEX_TYPE
4641 || code0
== ENUMERAL_TYPE
)
4642 && (code1
== INTEGER_TYPE
|| code1
== REAL_TYPE
4643 || code1
== COMPLEX_TYPE
|| code1
== ENUMERAL_TYPE
)))
4644 arithmetic_types_p
= 1;
4647 arithmetic_types_p
= 0;
4648 /* Vector arithmetic is only allowed when both sides are vectors. */
4649 if (code0
== VECTOR_TYPE
&& code1
== VECTOR_TYPE
)
4651 if (!tree_int_cst_equal (TYPE_SIZE (type0
), TYPE_SIZE (type1
))
4652 || !same_scalar_type_ignoring_signedness (TREE_TYPE (type0
),
4655 if (complain
& tf_error
)
4656 binary_op_error (location
, code
, type0
, type1
);
4657 return error_mark_node
;
4659 arithmetic_types_p
= 1;
4662 /* Determine the RESULT_TYPE, if it is not already known. */
4664 && arithmetic_types_p
4665 && (shorten
|| common
|| short_compare
))
4667 result_type
= cp_common_type (type0
, type1
);
4668 if (complain
& tf_warning
)
4669 do_warn_double_promotion (result_type
, type0
, type1
,
4670 "implicit conversion from %qT to %qT "
4671 "to match other operand of binary "
4678 if (complain
& tf_error
)
4679 error ("invalid operands of types %qT and %qT to binary %qO",
4680 TREE_TYPE (orig_op0
), TREE_TYPE (orig_op1
), code
);
4681 return error_mark_node
;
4684 /* If we're in a template, the only thing we need to know is the
4686 if (processing_template_decl
)
4688 /* Since the middle-end checks the type when doing a build2, we
4689 need to build the tree in pieces. This built tree will never
4690 get out of the front-end as we replace it when instantiating
4692 tree tmp
= build2 (resultcode
,
4693 build_type
? build_type
: result_type
,
4695 TREE_OPERAND (tmp
, 0) = op0
;
4699 if (arithmetic_types_p
)
4701 bool first_complex
= (code0
== COMPLEX_TYPE
);
4702 bool second_complex
= (code1
== COMPLEX_TYPE
);
4703 int none_complex
= (!first_complex
&& !second_complex
);
4705 /* Adapted from patch for c/24581. */
4706 if (first_complex
!= second_complex
4707 && (code
== PLUS_EXPR
4708 || code
== MINUS_EXPR
4709 || code
== MULT_EXPR
4710 || (code
== TRUNC_DIV_EXPR
&& first_complex
))
4711 && TREE_CODE (TREE_TYPE (result_type
)) == REAL_TYPE
4712 && flag_signed_zeros
)
4714 /* An operation on mixed real/complex operands must be
4715 handled specially, but the language-independent code can
4716 more easily optimize the plain complex arithmetic if
4717 -fno-signed-zeros. */
4718 tree real_type
= TREE_TYPE (result_type
);
4722 if (TREE_TYPE (op0
) != result_type
)
4723 op0
= cp_convert_and_check (result_type
, op0
, complain
);
4724 if (TREE_TYPE (op1
) != real_type
)
4725 op1
= cp_convert_and_check (real_type
, op1
, complain
);
4729 if (TREE_TYPE (op0
) != real_type
)
4730 op0
= cp_convert_and_check (real_type
, op0
, complain
);
4731 if (TREE_TYPE (op1
) != result_type
)
4732 op1
= cp_convert_and_check (result_type
, op1
, complain
);
4734 if (TREE_CODE (op0
) == ERROR_MARK
|| TREE_CODE (op1
) == ERROR_MARK
)
4735 return error_mark_node
;
4738 op0
= save_expr (op0
);
4739 real
= cp_build_unary_op (REALPART_EXPR
, op0
, 1, complain
);
4740 imag
= cp_build_unary_op (IMAGPART_EXPR
, op0
, 1, complain
);
4744 case TRUNC_DIV_EXPR
:
4745 op1
= save_expr (op1
);
4746 imag
= build2 (resultcode
, real_type
, imag
, op1
);
4750 real
= build2 (resultcode
, real_type
, real
, op1
);
4758 op1
= save_expr (op1
);
4759 real
= cp_build_unary_op (REALPART_EXPR
, op1
, 1, complain
);
4760 imag
= cp_build_unary_op (IMAGPART_EXPR
, op1
, 1, complain
);
4764 op0
= save_expr (op0
);
4765 imag
= build2 (resultcode
, real_type
, op0
, imag
);
4768 real
= build2 (resultcode
, real_type
, op0
, real
);
4771 real
= build2 (resultcode
, real_type
, op0
, real
);
4772 imag
= build1 (NEGATE_EXPR
, real_type
, imag
);
4778 real
= fold_if_not_in_template (real
);
4779 imag
= fold_if_not_in_template (imag
);
4780 result
= build2 (COMPLEX_EXPR
, result_type
, real
, imag
);
4781 result
= fold_if_not_in_template (result
);
4785 /* For certain operations (which identify themselves by shorten != 0)
4786 if both args were extended from the same smaller type,
4787 do the arithmetic in that type and then extend.
4789 shorten !=0 and !=1 indicates a bitwise operation.
4790 For them, this optimization is safe only if
4791 both args are zero-extended or both are sign-extended.
4792 Otherwise, we might change the result.
4793 E.g., (short)-1 | (unsigned short)-1 is (int)-1
4794 but calculated in (unsigned short) it would be (unsigned short)-1. */
4796 if (shorten
&& none_complex
)
4798 final_type
= result_type
;
4799 result_type
= shorten_binary_op (result_type
, op0
, op1
,
4803 /* Comparison operations are shortened too but differently.
4804 They identify themselves by setting short_compare = 1. */
4808 /* Don't write &op0, etc., because that would prevent op0
4809 from being kept in a register.
4810 Instead, make copies of the our local variables and
4811 pass the copies by reference, then copy them back afterward. */
4812 tree xop0
= op0
, xop1
= op1
, xresult_type
= result_type
;
4813 enum tree_code xresultcode
= resultcode
;
4815 = shorten_compare (&xop0
, &xop1
, &xresult_type
, &xresultcode
);
4817 return cp_convert (boolean_type_node
, val
, complain
);
4818 op0
= xop0
, op1
= xop1
;
4820 resultcode
= xresultcode
;
4823 if ((short_compare
|| code
== MIN_EXPR
|| code
== MAX_EXPR
)
4824 && warn_sign_compare
4825 /* Do not warn until the template is instantiated; we cannot
4826 bound the ranges of the arguments until that point. */
4827 && !processing_template_decl
4828 && (complain
& tf_warning
)
4829 && c_inhibit_evaluation_warnings
== 0
4830 /* Even unsigned enum types promote to signed int. We don't
4831 want to issue -Wsign-compare warnings for this case. */
4832 && !enum_cast_to_int (orig_op0
)
4833 && !enum_cast_to_int (orig_op1
))
4835 tree oop0
= maybe_constant_value (orig_op0
);
4836 tree oop1
= maybe_constant_value (orig_op1
);
4838 if (TREE_CODE (oop0
) != INTEGER_CST
)
4840 if (TREE_CODE (oop1
) != INTEGER_CST
)
4842 warn_for_sign_compare (location
, oop0
, oop1
, op0
, op1
,
4843 result_type
, resultcode
);
4847 /* If CONVERTED is zero, both args will be converted to type RESULT_TYPE.
4848 Then the expression will be built.
4849 It will be given type FINAL_TYPE if that is nonzero;
4850 otherwise, it will be given type RESULT_TYPE. */
4853 if (TREE_TYPE (op0
) != result_type
)
4854 op0
= cp_convert_and_check (result_type
, op0
, complain
);
4855 if (TREE_TYPE (op1
) != result_type
)
4856 op1
= cp_convert_and_check (result_type
, op1
, complain
);
4858 if (op0
== error_mark_node
|| op1
== error_mark_node
)
4859 return error_mark_node
;
4862 if (build_type
== NULL_TREE
)
4863 build_type
= result_type
;
4865 result
= build2 (resultcode
, build_type
, op0
, op1
);
4866 result
= fold_if_not_in_template (result
);
4867 if (final_type
!= 0)
4868 result
= cp_convert (final_type
, result
, complain
);
4870 if (TREE_OVERFLOW_P (result
)
4871 && !TREE_OVERFLOW_P (op0
)
4872 && !TREE_OVERFLOW_P (op1
))
4873 overflow_warning (location
, result
);
4878 /* Build a VEC_PERM_EXPR.
4879 This is a simple wrapper for c_build_vec_perm_expr. */
4881 build_x_vec_perm_expr (location_t loc
,
4882 tree arg0
, tree arg1
, tree arg2
,
4883 tsubst_flags_t complain
)
4885 if (processing_template_decl
4886 && (type_dependent_expression_p (arg0
)
4887 || type_dependent_expression_p (arg1
)
4888 || type_dependent_expression_p (arg2
)))
4889 return build_min_nt_loc (loc
, VEC_PERM_EXPR
, arg0
, arg1
, arg2
);
4890 return c_build_vec_perm_expr (loc
, arg0
, arg1
, arg2
, complain
& tf_error
);
4893 /* Return a tree for the sum or difference (RESULTCODE says which)
4894 of pointer PTROP and integer INTOP. */
4897 cp_pointer_int_sum (enum tree_code resultcode
, tree ptrop
, tree intop
)
4899 tree res_type
= TREE_TYPE (ptrop
);
4901 /* pointer_int_sum() uses size_in_bytes() on the TREE_TYPE(res_type)
4902 in certain circumstance (when it's valid to do so). So we need
4903 to make sure it's complete. We don't need to check here, if we
4904 can actually complete it at all, as those checks will be done in
4905 pointer_int_sum() anyway. */
4906 complete_type (TREE_TYPE (res_type
));
4908 return pointer_int_sum (input_location
, resultcode
, ptrop
,
4909 fold_if_not_in_template (intop
));
4912 /* Return a tree for the difference of pointers OP0 and OP1.
4913 The resulting tree has type int. */
4916 pointer_diff (tree op0
, tree op1
, tree ptrtype
, tsubst_flags_t complain
)
4919 tree restype
= ptrdiff_type_node
;
4920 tree target_type
= TREE_TYPE (ptrtype
);
4922 if (!complete_type_or_else (target_type
, NULL_TREE
))
4923 return error_mark_node
;
4925 if (VOID_TYPE_P (target_type
))
4927 if (complain
& tf_error
)
4928 permerror (input_location
, "ISO C++ forbids using pointer of "
4929 "type %<void *%> in subtraction");
4931 return error_mark_node
;
4933 if (TREE_CODE (target_type
) == FUNCTION_TYPE
)
4935 if (complain
& tf_error
)
4936 permerror (input_location
, "ISO C++ forbids using pointer to "
4937 "a function in subtraction");
4939 return error_mark_node
;
4941 if (TREE_CODE (target_type
) == METHOD_TYPE
)
4943 if (complain
& tf_error
)
4944 permerror (input_location
, "ISO C++ forbids using pointer to "
4945 "a method in subtraction");
4947 return error_mark_node
;
4950 /* First do the subtraction as integers;
4951 then drop through to build the divide operator. */
4953 op0
= cp_build_binary_op (input_location
,
4955 cp_convert (restype
, op0
, complain
),
4956 cp_convert (restype
, op1
, complain
),
4959 /* This generates an error if op1 is a pointer to an incomplete type. */
4960 if (!COMPLETE_TYPE_P (TREE_TYPE (TREE_TYPE (op1
))))
4962 if (complain
& tf_error
)
4963 error ("invalid use of a pointer to an incomplete type in "
4964 "pointer arithmetic");
4966 return error_mark_node
;
4969 op1
= (TYPE_PTROB_P (ptrtype
)
4970 ? size_in_bytes (target_type
)
4971 : integer_one_node
);
4973 /* Do the division. */
4975 result
= build2 (EXACT_DIV_EXPR
, restype
, op0
,
4976 cp_convert (restype
, op1
, complain
));
4977 return fold_if_not_in_template (result
);
4980 /* Construct and perhaps optimize a tree representation
4981 for a unary operation. CODE, a tree_code, specifies the operation
4982 and XARG is the operand. */
4985 build_x_unary_op (location_t loc
, enum tree_code code
, tree xarg
,
4986 tsubst_flags_t complain
)
4988 tree orig_expr
= xarg
;
4992 if (processing_template_decl
)
4994 if (type_dependent_expression_p (xarg
))
4995 return build_min_nt_loc (loc
, code
, xarg
, NULL_TREE
);
4997 xarg
= build_non_dependent_expr (xarg
);
5002 /* [expr.unary.op] says:
5004 The address of an object of incomplete type can be taken.
5006 (And is just the ordinary address operator, not an overloaded
5007 "operator &".) However, if the type is a template
5008 specialization, we must complete the type at this point so that
5009 an overloaded "operator &" will be available if required. */
5010 if (code
== ADDR_EXPR
5011 && TREE_CODE (xarg
) != TEMPLATE_ID_EXPR
5012 && ((CLASS_TYPE_P (TREE_TYPE (xarg
))
5013 && !COMPLETE_TYPE_P (complete_type (TREE_TYPE (xarg
))))
5014 || (TREE_CODE (xarg
) == OFFSET_REF
)))
5015 /* Don't look for a function. */;
5017 exp
= build_new_op (loc
, code
, LOOKUP_NORMAL
, xarg
, NULL_TREE
,
5018 NULL_TREE
, /*overload=*/NULL
, complain
);
5019 if (!exp
&& code
== ADDR_EXPR
)
5021 if (is_overloaded_fn (xarg
))
5023 tree fn
= get_first_fn (xarg
);
5024 if (DECL_CONSTRUCTOR_P (fn
) || DECL_DESTRUCTOR_P (fn
))
5026 if (complain
& tf_error
)
5027 error (DECL_CONSTRUCTOR_P (fn
)
5028 ? G_("taking address of constructor %qE")
5029 : G_("taking address of destructor %qE"),
5031 return error_mark_node
;
5035 /* A pointer to member-function can be formed only by saying
5037 if (!flag_ms_extensions
&& TREE_CODE (TREE_TYPE (xarg
)) == METHOD_TYPE
5038 && (TREE_CODE (xarg
) != OFFSET_REF
|| !PTRMEM_OK_P (xarg
)))
5040 if (TREE_CODE (xarg
) != OFFSET_REF
5041 || !TYPE_P (TREE_OPERAND (xarg
, 0)))
5043 if (complain
& tf_error
)
5045 error ("invalid use of %qE to form a "
5046 "pointer-to-member-function", xarg
);
5047 if (TREE_CODE (xarg
) != OFFSET_REF
)
5048 inform (input_location
, " a qualified-id is required");
5050 return error_mark_node
;
5054 if (complain
& tf_error
)
5055 error ("parentheses around %qE cannot be used to form a"
5056 " pointer-to-member-function",
5059 return error_mark_node
;
5060 PTRMEM_OK_P (xarg
) = 1;
5064 if (TREE_CODE (xarg
) == OFFSET_REF
)
5066 ptrmem
= PTRMEM_OK_P (xarg
);
5068 if (!ptrmem
&& !flag_ms_extensions
5069 && TREE_CODE (TREE_TYPE (TREE_OPERAND (xarg
, 1))) == METHOD_TYPE
)
5071 /* A single non-static member, make sure we don't allow a
5072 pointer-to-member. */
5073 xarg
= build2 (OFFSET_REF
, TREE_TYPE (xarg
),
5074 TREE_OPERAND (xarg
, 0),
5075 ovl_cons (TREE_OPERAND (xarg
, 1), NULL_TREE
));
5076 PTRMEM_OK_P (xarg
) = ptrmem
;
5080 exp
= cp_build_addr_expr_strict (xarg
, complain
);
5083 if (processing_template_decl
&& exp
!= error_mark_node
)
5084 exp
= build_min_non_dep (code
, exp
, orig_expr
,
5085 /*For {PRE,POST}{INC,DEC}REMENT_EXPR*/NULL_TREE
);
5086 if (TREE_CODE (exp
) == ADDR_EXPR
)
5087 PTRMEM_OK_P (exp
) = ptrmem
;
5091 /* Like c_common_truthvalue_conversion, but handle pointer-to-member
5092 constants, where a null value is represented by an INTEGER_CST of
5096 cp_truthvalue_conversion (tree expr
)
5098 tree type
= TREE_TYPE (expr
);
5099 if (TYPE_PTRDATAMEM_P (type
))
5100 return build_binary_op (EXPR_LOCATION (expr
),
5101 NE_EXPR
, expr
, nullptr_node
, 1);
5102 else if (TYPE_PTR_P (type
) || TYPE_PTRMEMFUNC_P (type
))
5104 /* With -Wzero-as-null-pointer-constant do not warn for an
5105 'if (p)' or a 'while (!p)', where p is a pointer. */
5107 ++c_inhibit_evaluation_warnings
;
5108 ret
= c_common_truthvalue_conversion (input_location
, expr
);
5109 --c_inhibit_evaluation_warnings
;
5113 return c_common_truthvalue_conversion (input_location
, expr
);
5116 /* Just like cp_truthvalue_conversion, but we want a CLEANUP_POINT_EXPR. */
5119 condition_conversion (tree expr
)
5122 if (processing_template_decl
)
5124 t
= perform_implicit_conversion_flags (boolean_type_node
, expr
,
5125 tf_warning_or_error
, LOOKUP_NORMAL
);
5126 t
= fold_build_cleanup_point_expr (boolean_type_node
, t
);
5130 /* Returns the address of T. This function will fold away
5131 ADDR_EXPR of INDIRECT_REF. */
5134 build_address (tree t
)
5136 if (error_operand_p (t
) || !cxx_mark_addressable (t
))
5137 return error_mark_node
;
5138 t
= build_fold_addr_expr (t
);
5139 if (TREE_CODE (t
) != ADDR_EXPR
)
5144 /* Returns the address of T with type TYPE. */
5147 build_typed_address (tree t
, tree type
)
5149 if (error_operand_p (t
) || !cxx_mark_addressable (t
))
5150 return error_mark_node
;
5151 t
= build_fold_addr_expr_with_type (t
, type
);
5152 if (TREE_CODE (t
) != ADDR_EXPR
)
5157 /* Return a NOP_EXPR converting EXPR to TYPE. */
5160 build_nop (tree type
, tree expr
)
5162 if (type
== error_mark_node
|| error_operand_p (expr
))
5164 return build1 (NOP_EXPR
, type
, expr
);
5167 /* Take the address of ARG, whatever that means under C++ semantics.
5168 If STRICT_LVALUE is true, require an lvalue; otherwise, allow xvalues
5169 and class rvalues as well.
5171 Nothing should call this function directly; instead, callers should use
5172 cp_build_addr_expr or cp_build_addr_expr_strict. */
5175 cp_build_addr_expr_1 (tree arg
, bool strict_lvalue
, tsubst_flags_t complain
)
5180 if (!arg
|| error_operand_p (arg
))
5181 return error_mark_node
;
5183 arg
= mark_lvalue_use (arg
);
5184 argtype
= lvalue_type (arg
);
5186 gcc_assert (!identifier_p (arg
) || !IDENTIFIER_OPNAME_P (arg
));
5188 if (TREE_CODE (arg
) == COMPONENT_REF
&& type_unknown_p (arg
)
5189 && !really_overloaded_fn (TREE_OPERAND (arg
, 1)))
5191 /* They're trying to take the address of a unique non-static
5192 member function. This is ill-formed (except in MS-land),
5193 but let's try to DTRT.
5194 Note: We only handle unique functions here because we don't
5195 want to complain if there's a static overload; non-unique
5196 cases will be handled by instantiate_type. But we need to
5197 handle this case here to allow casts on the resulting PMF.
5198 We could defer this in non-MS mode, but it's easier to give
5199 a useful error here. */
5201 /* Inside constant member functions, the `this' pointer
5202 contains an extra const qualifier. TYPE_MAIN_VARIANT
5203 is used here to remove this const from the diagnostics
5204 and the created OFFSET_REF. */
5205 tree base
= TYPE_MAIN_VARIANT (TREE_TYPE (TREE_OPERAND (arg
, 0)));
5206 tree fn
= get_first_fn (TREE_OPERAND (arg
, 1));
5209 if (! flag_ms_extensions
)
5211 tree name
= DECL_NAME (fn
);
5212 if (!(complain
& tf_error
))
5213 return error_mark_node
;
5214 else if (current_class_type
5215 && TREE_OPERAND (arg
, 0) == current_class_ref
)
5216 /* An expression like &memfn. */
5217 permerror (input_location
, "ISO C++ forbids taking the address of an unqualified"
5218 " or parenthesized non-static member function to form"
5219 " a pointer to member function. Say %<&%T::%D%>",
5222 permerror (input_location
, "ISO C++ forbids taking the address of a bound member"
5223 " function to form a pointer to member function."
5227 arg
= build_offset_ref (base
, fn
, /*address_p=*/true, complain
);
5230 /* Uninstantiated types are all functions. Taking the
5231 address of a function is a no-op, so just return the
5233 if (type_unknown_p (arg
))
5234 return build1 (ADDR_EXPR
, unknown_type_node
, arg
);
5236 if (TREE_CODE (arg
) == OFFSET_REF
)
5237 /* We want a pointer to member; bypass all the code for actually taking
5238 the address of something. */
5241 /* Anything not already handled and not a true memory reference
5243 if (TREE_CODE (argtype
) != FUNCTION_TYPE
5244 && TREE_CODE (argtype
) != METHOD_TYPE
)
5246 cp_lvalue_kind kind
= lvalue_kind (arg
);
5247 if (kind
== clk_none
)
5249 if (complain
& tf_error
)
5250 lvalue_error (input_location
, lv_addressof
);
5251 return error_mark_node
;
5253 if (strict_lvalue
&& (kind
& (clk_rvalueref
|clk_class
)))
5255 if (!(complain
& tf_error
))
5256 return error_mark_node
;
5257 if (kind
& clk_class
)
5258 /* Make this a permerror because we used to accept it. */
5259 permerror (input_location
, "taking address of temporary");
5261 error ("taking address of xvalue (rvalue reference)");
5265 if (TREE_CODE (argtype
) == REFERENCE_TYPE
)
5267 tree type
= build_pointer_type (TREE_TYPE (argtype
));
5268 arg
= build1 (CONVERT_EXPR
, type
, arg
);
5271 else if (pedantic
&& DECL_MAIN_P (arg
))
5274 /* Apparently a lot of autoconf scripts for C++ packages do this,
5275 so only complain if -Wpedantic. */
5276 if (complain
& (flag_pedantic_errors
? tf_error
: tf_warning
))
5277 pedwarn (input_location
, OPT_Wpedantic
,
5278 "ISO C++ forbids taking address of function %<::main%>");
5279 else if (flag_pedantic_errors
)
5280 return error_mark_node
;
5283 /* Let &* cancel out to simplify resulting code. */
5284 if (INDIRECT_REF_P (arg
))
5286 /* We don't need to have `current_class_ptr' wrapped in a
5287 NON_LVALUE_EXPR node. */
5288 if (arg
== current_class_ref
)
5289 return current_class_ptr
;
5291 arg
= TREE_OPERAND (arg
, 0);
5292 if (TREE_CODE (TREE_TYPE (arg
)) == REFERENCE_TYPE
)
5294 tree type
= build_pointer_type (TREE_TYPE (TREE_TYPE (arg
)));
5295 arg
= build1 (CONVERT_EXPR
, type
, arg
);
5298 /* Don't let this be an lvalue. */
5303 /* ??? Cope with user tricks that amount to offsetof. */
5304 if (TREE_CODE (argtype
) != FUNCTION_TYPE
5305 && TREE_CODE (argtype
) != METHOD_TYPE
5306 && argtype
!= unknown_type_node
5307 && (val
= get_base_address (arg
))
5308 && COMPLETE_TYPE_P (TREE_TYPE (val
))
5309 && INDIRECT_REF_P (val
)
5310 && TREE_CONSTANT (TREE_OPERAND (val
, 0)))
5312 tree type
= build_pointer_type (argtype
);
5313 return fold_convert (type
, fold_offsetof_1 (arg
));
5316 /* Handle complex lvalues (when permitted)
5317 by reduction to simpler cases. */
5318 val
= unary_complex_lvalue (ADDR_EXPR
, arg
);
5322 switch (TREE_CODE (arg
))
5326 case FIX_TRUNC_EXPR
:
5327 /* Even if we're not being pedantic, we cannot allow this
5328 extension when we're instantiating in a SFINAE
5330 if (! lvalue_p (arg
) && complain
== tf_none
)
5332 if (complain
& tf_error
)
5333 permerror (input_location
, "ISO C++ forbids taking the address of a cast to a non-lvalue expression");
5335 return error_mark_node
;
5340 arg
= BASELINK_FUNCTIONS (arg
);
5344 arg
= OVL_CURRENT (arg
);
5349 /* Turn a reference to a non-static data member into a
5350 pointer-to-member. */
5355 gcc_assert (PTRMEM_OK_P (arg
));
5357 t
= TREE_OPERAND (arg
, 1);
5358 if (TREE_CODE (TREE_TYPE (t
)) == REFERENCE_TYPE
)
5360 if (complain
& tf_error
)
5361 error ("cannot create pointer to reference member %qD", t
);
5362 return error_mark_node
;
5365 type
= build_ptrmem_type (context_for_name_lookup (t
),
5367 t
= make_ptrmem_cst (type
, TREE_OPERAND (arg
, 1));
5375 if (argtype
!= error_mark_node
)
5377 if (cxx_dialect
>= cxx1y
&& array_of_runtime_bound_p (argtype
))
5379 if (complain
& tf_warning_or_error
)
5380 pedwarn (input_location
, OPT_Wvla
,
5381 "taking address of array of runtime bound");
5383 return error_mark_node
;
5385 argtype
= build_pointer_type (argtype
);
5388 /* In a template, we are processing a non-dependent expression
5389 so we can just form an ADDR_EXPR with the correct type. */
5390 if (processing_template_decl
|| TREE_CODE (arg
) != COMPONENT_REF
)
5392 val
= build_address (arg
);
5393 if (TREE_CODE (arg
) == OFFSET_REF
)
5394 PTRMEM_OK_P (val
) = PTRMEM_OK_P (arg
);
5396 else if (BASELINK_P (TREE_OPERAND (arg
, 1)))
5398 tree fn
= BASELINK_FUNCTIONS (TREE_OPERAND (arg
, 1));
5400 /* We can only get here with a single static member
5402 gcc_assert (TREE_CODE (fn
) == FUNCTION_DECL
5403 && DECL_STATIC_FUNCTION_P (fn
));
5405 val
= build_address (fn
);
5406 if (TREE_SIDE_EFFECTS (TREE_OPERAND (arg
, 0)))
5407 /* Do not lose object's side effects. */
5408 val
= build2 (COMPOUND_EXPR
, TREE_TYPE (val
),
5409 TREE_OPERAND (arg
, 0), val
);
5411 else if (DECL_C_BIT_FIELD (TREE_OPERAND (arg
, 1)))
5413 if (complain
& tf_error
)
5414 error ("attempt to take address of bit-field structure member %qD",
5415 TREE_OPERAND (arg
, 1));
5416 return error_mark_node
;
5420 tree object
= TREE_OPERAND (arg
, 0);
5421 tree field
= TREE_OPERAND (arg
, 1);
5422 gcc_assert (same_type_ignoring_top_level_qualifiers_p
5423 (TREE_TYPE (object
), decl_type_context (field
)));
5424 val
= build_address (arg
);
5427 if (TYPE_PTR_P (argtype
)
5428 && TREE_CODE (TREE_TYPE (argtype
)) == METHOD_TYPE
)
5430 build_ptrmemfunc_type (argtype
);
5431 val
= build_ptrmemfunc (argtype
, val
, 0,
5439 /* Take the address of ARG if it has one, even if it's an rvalue. */
5442 cp_build_addr_expr (tree arg
, tsubst_flags_t complain
)
5444 return cp_build_addr_expr_1 (arg
, 0, complain
);
5447 /* Take the address of ARG, but only if it's an lvalue. */
5450 cp_build_addr_expr_strict (tree arg
, tsubst_flags_t complain
)
5452 return cp_build_addr_expr_1 (arg
, 1, complain
);
5455 /* C++: Must handle pointers to members.
5457 Perhaps type instantiation should be extended to handle conversion
5458 from aggregates to types we don't yet know we want? (Or are those
5459 cases typically errors which should be reported?)
5461 NOCONVERT nonzero suppresses the default promotions
5462 (such as from short to int). */
5465 cp_build_unary_op (enum tree_code code
, tree xarg
, int noconvert
,
5466 tsubst_flags_t complain
)
5468 /* No default_conversion here. It causes trouble for ADDR_EXPR. */
5471 const char *errstring
= NULL
;
5473 const char *invalid_op_diag
;
5475 if (!arg
|| error_operand_p (arg
))
5476 return error_mark_node
;
5478 if ((invalid_op_diag
5479 = targetm
.invalid_unary_op ((code
== UNARY_PLUS_EXPR
5484 if (complain
& tf_error
)
5485 error (invalid_op_diag
);
5486 return error_mark_node
;
5491 case UNARY_PLUS_EXPR
:
5494 int flags
= WANT_ARITH
| WANT_ENUM
;
5495 /* Unary plus (but not unary minus) is allowed on pointers. */
5496 if (code
== UNARY_PLUS_EXPR
)
5497 flags
|= WANT_POINTER
;
5498 arg
= build_expr_type_conversion (flags
, arg
, true);
5500 errstring
= (code
== NEGATE_EXPR
5501 ? _("wrong type argument to unary minus")
5502 : _("wrong type argument to unary plus"));
5505 if (!noconvert
&& CP_INTEGRAL_TYPE_P (TREE_TYPE (arg
)))
5506 arg
= cp_perform_integral_promotions (arg
, complain
);
5508 /* Make sure the result is not an lvalue: a unary plus or minus
5509 expression is always a rvalue. */
5516 if (TREE_CODE (TREE_TYPE (arg
)) == COMPLEX_TYPE
)
5521 arg
= cp_default_conversion (arg
, complain
);
5522 if (arg
== error_mark_node
)
5523 return error_mark_node
;
5526 else if (!(arg
= build_expr_type_conversion (WANT_INT
| WANT_ENUM
5527 | WANT_VECTOR_OR_COMPLEX
,
5529 errstring
= _("wrong type argument to bit-complement");
5530 else if (!noconvert
&& CP_INTEGRAL_TYPE_P (TREE_TYPE (arg
)))
5531 arg
= cp_perform_integral_promotions (arg
, complain
);
5535 if (!(arg
= build_expr_type_conversion (WANT_ARITH
| WANT_ENUM
, arg
, true)))
5536 errstring
= _("wrong type argument to abs");
5537 else if (!noconvert
)
5539 arg
= cp_default_conversion (arg
, complain
);
5540 if (arg
== error_mark_node
)
5541 return error_mark_node
;
5546 /* Conjugating a real value is a no-op, but allow it anyway. */
5547 if (!(arg
= build_expr_type_conversion (WANT_ARITH
| WANT_ENUM
, arg
, true)))
5548 errstring
= _("wrong type argument to conjugation");
5549 else if (!noconvert
)
5551 arg
= cp_default_conversion (arg
, complain
);
5552 if (arg
== error_mark_node
)
5553 return error_mark_node
;
5557 case TRUTH_NOT_EXPR
:
5558 arg
= perform_implicit_conversion (boolean_type_node
, arg
,
5560 val
= invert_truthvalue_loc (input_location
, arg
);
5561 if (arg
!= error_mark_node
)
5563 errstring
= _("in argument to unary !");
5571 arg
= build_real_imag_expr (input_location
, code
, arg
);
5572 if (arg
== error_mark_node
)
5575 return fold_if_not_in_template (arg
);
5577 case PREINCREMENT_EXPR
:
5578 case POSTINCREMENT_EXPR
:
5579 case PREDECREMENT_EXPR
:
5580 case POSTDECREMENT_EXPR
:
5581 /* Handle complex lvalues (when permitted)
5582 by reduction to simpler cases. */
5584 val
= unary_complex_lvalue (code
, arg
);
5588 arg
= mark_lvalue_use (arg
);
5590 /* Increment or decrement the real part of the value,
5591 and don't change the imaginary part. */
5592 if (TREE_CODE (TREE_TYPE (arg
)) == COMPLEX_TYPE
)
5596 arg
= stabilize_reference (arg
);
5597 real
= cp_build_unary_op (REALPART_EXPR
, arg
, 1, complain
);
5598 imag
= cp_build_unary_op (IMAGPART_EXPR
, arg
, 1, complain
);
5599 real
= cp_build_unary_op (code
, real
, 1, complain
);
5600 if (real
== error_mark_node
|| imag
== error_mark_node
)
5601 return error_mark_node
;
5602 return build2 (COMPLEX_EXPR
, TREE_TYPE (arg
),
5606 /* Report invalid types. */
5608 if (!(arg
= build_expr_type_conversion (WANT_ARITH
| WANT_POINTER
,
5611 if (code
== PREINCREMENT_EXPR
)
5612 errstring
= _("no pre-increment operator for type");
5613 else if (code
== POSTINCREMENT_EXPR
)
5614 errstring
= _("no post-increment operator for type");
5615 else if (code
== PREDECREMENT_EXPR
)
5616 errstring
= _("no pre-decrement operator for type");
5618 errstring
= _("no post-decrement operator for type");
5621 else if (arg
== error_mark_node
)
5622 return error_mark_node
;
5624 /* Report something read-only. */
5626 if (CP_TYPE_CONST_P (TREE_TYPE (arg
))
5627 || TREE_READONLY (arg
))
5629 if (complain
& tf_error
)
5630 cxx_readonly_error (arg
, ((code
== PREINCREMENT_EXPR
5631 || code
== POSTINCREMENT_EXPR
)
5632 ? lv_increment
: lv_decrement
));
5634 return error_mark_node
;
5639 tree declared_type
= unlowered_expr_type (arg
);
5641 argtype
= TREE_TYPE (arg
);
5643 /* ARM $5.2.5 last annotation says this should be forbidden. */
5644 if (TREE_CODE (argtype
) == ENUMERAL_TYPE
)
5646 if (complain
& tf_error
)
5647 permerror (input_location
, (code
== PREINCREMENT_EXPR
|| code
== POSTINCREMENT_EXPR
)
5648 ? G_("ISO C++ forbids incrementing an enum")
5649 : G_("ISO C++ forbids decrementing an enum"));
5651 return error_mark_node
;
5654 /* Compute the increment. */
5656 if (TYPE_PTR_P (argtype
))
5658 tree type
= complete_type (TREE_TYPE (argtype
));
5660 if (!COMPLETE_OR_VOID_TYPE_P (type
))
5662 if (complain
& tf_error
)
5663 error (((code
== PREINCREMENT_EXPR
5664 || code
== POSTINCREMENT_EXPR
))
5665 ? G_("cannot increment a pointer to incomplete type %qT")
5666 : G_("cannot decrement a pointer to incomplete type %qT"),
5667 TREE_TYPE (argtype
));
5669 return error_mark_node
;
5671 else if (!TYPE_PTROB_P (argtype
))
5673 if (complain
& tf_error
)
5674 pedwarn (input_location
, OPT_Wpointer_arith
,
5675 (code
== PREINCREMENT_EXPR
5676 || code
== POSTINCREMENT_EXPR
)
5677 ? G_("ISO C++ forbids incrementing a pointer of type %qT")
5678 : G_("ISO C++ forbids decrementing a pointer of type %qT"),
5681 return error_mark_node
;
5684 inc
= cxx_sizeof_nowarn (TREE_TYPE (argtype
));
5687 inc
= integer_one_node
;
5689 inc
= cp_convert (argtype
, inc
, complain
);
5691 /* If 'arg' is an Objective-C PROPERTY_REF expression, then we
5692 need to ask Objective-C to build the increment or decrement
5693 expression for it. */
5694 if (objc_is_property_ref (arg
))
5695 return objc_build_incr_expr_for_property_ref (input_location
, code
,
5698 /* Complain about anything else that is not a true lvalue. */
5699 if (!lvalue_or_else (arg
, ((code
== PREINCREMENT_EXPR
5700 || code
== POSTINCREMENT_EXPR
)
5701 ? lv_increment
: lv_decrement
),
5703 return error_mark_node
;
5705 /* Forbid using -- on `bool'. */
5706 if (TREE_CODE (declared_type
) == BOOLEAN_TYPE
)
5708 if (code
== POSTDECREMENT_EXPR
|| code
== PREDECREMENT_EXPR
)
5710 if (complain
& tf_error
)
5711 error ("invalid use of Boolean expression as operand "
5712 "to %<operator--%>");
5713 return error_mark_node
;
5715 val
= boolean_increment (code
, arg
);
5717 else if (code
== POSTINCREMENT_EXPR
|| code
== POSTDECREMENT_EXPR
)
5718 /* An rvalue has no cv-qualifiers. */
5719 val
= build2 (code
, cv_unqualified (TREE_TYPE (arg
)), arg
, inc
);
5721 val
= build2 (code
, TREE_TYPE (arg
), arg
, inc
);
5723 TREE_SIDE_EFFECTS (val
) = 1;
5728 /* Note that this operation never does default_conversion
5729 regardless of NOCONVERT. */
5730 return cp_build_addr_expr (arg
, complain
);
5739 argtype
= TREE_TYPE (arg
);
5740 return fold_if_not_in_template (build1 (code
, argtype
, arg
));
5743 if (complain
& tf_error
)
5744 error ("%s", errstring
);
5745 return error_mark_node
;
5748 /* Hook for the c-common bits that build a unary op. */
5750 build_unary_op (location_t
/*location*/,
5751 enum tree_code code
, tree xarg
, int noconvert
)
5753 return cp_build_unary_op (code
, xarg
, noconvert
, tf_warning_or_error
);
5756 /* Apply unary lvalue-demanding operator CODE to the expression ARG
5757 for certain kinds of expressions which are not really lvalues
5758 but which we can accept as lvalues.
5760 If ARG is not a kind of expression we can handle, return
5764 unary_complex_lvalue (enum tree_code code
, tree arg
)
5766 /* Inside a template, making these kinds of adjustments is
5767 pointless; we are only concerned with the type of the
5769 if (processing_template_decl
)
5772 /* Handle (a, b) used as an "lvalue". */
5773 if (TREE_CODE (arg
) == COMPOUND_EXPR
)
5775 tree real_result
= cp_build_unary_op (code
, TREE_OPERAND (arg
, 1), 0,
5776 tf_warning_or_error
);
5777 return build2 (COMPOUND_EXPR
, TREE_TYPE (real_result
),
5778 TREE_OPERAND (arg
, 0), real_result
);
5781 /* Handle (a ? b : c) used as an "lvalue". */
5782 if (TREE_CODE (arg
) == COND_EXPR
5783 || TREE_CODE (arg
) == MIN_EXPR
|| TREE_CODE (arg
) == MAX_EXPR
)
5784 return rationalize_conditional_expr (code
, arg
, tf_warning_or_error
);
5786 /* Handle (a = b), (++a), and (--a) used as an "lvalue". */
5787 if (TREE_CODE (arg
) == MODIFY_EXPR
5788 || TREE_CODE (arg
) == PREINCREMENT_EXPR
5789 || TREE_CODE (arg
) == PREDECREMENT_EXPR
)
5791 tree lvalue
= TREE_OPERAND (arg
, 0);
5792 if (TREE_SIDE_EFFECTS (lvalue
))
5794 lvalue
= stabilize_reference (lvalue
);
5795 arg
= build2 (TREE_CODE (arg
), TREE_TYPE (arg
),
5796 lvalue
, TREE_OPERAND (arg
, 1));
5798 return unary_complex_lvalue
5799 (code
, build2 (COMPOUND_EXPR
, TREE_TYPE (lvalue
), arg
, lvalue
));
5802 if (code
!= ADDR_EXPR
)
5805 /* Handle (a = b) used as an "lvalue" for `&'. */
5806 if (TREE_CODE (arg
) == MODIFY_EXPR
5807 || TREE_CODE (arg
) == INIT_EXPR
)
5809 tree real_result
= cp_build_unary_op (code
, TREE_OPERAND (arg
, 0), 0,
5810 tf_warning_or_error
);
5811 arg
= build2 (COMPOUND_EXPR
, TREE_TYPE (real_result
),
5813 TREE_NO_WARNING (arg
) = 1;
5817 if (TREE_CODE (TREE_TYPE (arg
)) == FUNCTION_TYPE
5818 || TREE_CODE (TREE_TYPE (arg
)) == METHOD_TYPE
5819 || TREE_CODE (arg
) == OFFSET_REF
)
5822 /* We permit compiler to make function calls returning
5823 objects of aggregate type look like lvalues. */
5827 if (TREE_CODE (targ
) == SAVE_EXPR
)
5828 targ
= TREE_OPERAND (targ
, 0);
5830 if (TREE_CODE (targ
) == CALL_EXPR
&& MAYBE_CLASS_TYPE_P (TREE_TYPE (targ
)))
5832 if (TREE_CODE (arg
) == SAVE_EXPR
)
5835 targ
= build_cplus_new (TREE_TYPE (arg
), arg
, tf_warning_or_error
);
5836 return build1 (ADDR_EXPR
, build_pointer_type (TREE_TYPE (arg
)), targ
);
5839 if (TREE_CODE (arg
) == SAVE_EXPR
&& INDIRECT_REF_P (targ
))
5840 return build3 (SAVE_EXPR
, build_pointer_type (TREE_TYPE (arg
)),
5841 TREE_OPERAND (targ
, 0), current_function_decl
, NULL
);
5844 /* Don't let anything else be handled specially. */
5848 /* Mark EXP saying that we need to be able to take the
5849 address of it; it should not be allocated in a register.
5850 Value is true if successful.
5852 C++: we do not allow `current_class_ptr' to be addressable. */
5855 cxx_mark_addressable (tree exp
)
5860 switch (TREE_CODE (x
))
5867 x
= TREE_OPERAND (x
, 0);
5871 if (x
== current_class_ptr
)
5873 error ("cannot take the address of %<this%>, which is an rvalue expression");
5874 TREE_ADDRESSABLE (x
) = 1; /* so compiler doesn't die later. */
5880 /* Caller should not be trying to mark initialized
5881 constant fields addressable. */
5882 gcc_assert (DECL_LANG_SPECIFIC (x
) == 0
5883 || DECL_IN_AGGR_P (x
) == 0
5885 || DECL_EXTERNAL (x
));
5889 if (DECL_REGISTER (x
) && !TREE_ADDRESSABLE (x
)
5890 && !DECL_ARTIFICIAL (x
))
5892 if (VAR_P (x
) && DECL_HARD_REGISTER (x
))
5895 ("address of explicit register variable %qD requested", x
);
5898 else if (extra_warnings
)
5900 (OPT_Wextra
, "address requested for %qD, which is declared %<register%>", x
);
5902 TREE_ADDRESSABLE (x
) = 1;
5907 TREE_ADDRESSABLE (x
) = 1;
5911 TREE_ADDRESSABLE (x
) = 1;
5915 TREE_ADDRESSABLE (x
) = 1;
5916 cxx_mark_addressable (TREE_OPERAND (x
, 0));
5924 /* Build and return a conditional expression IFEXP ? OP1 : OP2. */
5927 build_x_conditional_expr (location_t loc
, tree ifexp
, tree op1
, tree op2
,
5928 tsubst_flags_t complain
)
5930 tree orig_ifexp
= ifexp
;
5931 tree orig_op1
= op1
;
5932 tree orig_op2
= op2
;
5935 if (processing_template_decl
)
5937 /* The standard says that the expression is type-dependent if
5938 IFEXP is type-dependent, even though the eventual type of the
5939 expression doesn't dependent on IFEXP. */
5940 if (type_dependent_expression_p (ifexp
)
5941 /* As a GNU extension, the middle operand may be omitted. */
5942 || (op1
&& type_dependent_expression_p (op1
))
5943 || type_dependent_expression_p (op2
))
5944 return build_min_nt_loc (loc
, COND_EXPR
, ifexp
, op1
, op2
);
5945 ifexp
= build_non_dependent_expr (ifexp
);
5947 op1
= build_non_dependent_expr (op1
);
5948 op2
= build_non_dependent_expr (op2
);
5951 expr
= build_conditional_expr (loc
, ifexp
, op1
, op2
, complain
);
5952 if (processing_template_decl
&& expr
!= error_mark_node
5953 && TREE_CODE (expr
) != VEC_COND_EXPR
)
5955 tree min
= build_min_non_dep (COND_EXPR
, expr
,
5956 orig_ifexp
, orig_op1
, orig_op2
);
5957 /* In C++11, remember that the result is an lvalue or xvalue.
5958 In C++98, lvalue_kind can just assume lvalue in a template. */
5959 if (cxx_dialect
>= cxx11
5960 && lvalue_or_rvalue_with_address_p (expr
)
5961 && !lvalue_or_rvalue_with_address_p (min
))
5962 TREE_TYPE (min
) = cp_build_reference_type (TREE_TYPE (min
),
5963 !real_lvalue_p (expr
));
5964 expr
= convert_from_reference (min
);
5969 /* Given a list of expressions, return a compound expression
5970 that performs them all and returns the value of the last of them. */
5973 build_x_compound_expr_from_list (tree list
, expr_list_kind exp
,
5974 tsubst_flags_t complain
)
5976 tree expr
= TREE_VALUE (list
);
5978 if (BRACE_ENCLOSED_INITIALIZER_P (expr
)
5979 && !CONSTRUCTOR_IS_DIRECT_INIT (expr
))
5981 if (complain
& tf_error
)
5982 pedwarn (EXPR_LOC_OR_HERE (expr
), 0, "list-initializer for "
5983 "non-class type must not be parenthesized");
5985 return error_mark_node
;
5988 if (TREE_CHAIN (list
))
5990 if (complain
& tf_error
)
5994 permerror (input_location
, "expression list treated as compound "
5995 "expression in initializer");
5998 permerror (input_location
, "expression list treated as compound "
5999 "expression in mem-initializer");
6002 permerror (input_location
, "expression list treated as compound "
6003 "expression in functional cast");
6009 return error_mark_node
;
6011 for (list
= TREE_CHAIN (list
); list
; list
= TREE_CHAIN (list
))
6012 expr
= build_x_compound_expr (EXPR_LOCATION (TREE_VALUE (list
)),
6013 expr
, TREE_VALUE (list
), complain
);
6019 /* Like build_x_compound_expr_from_list, but using a VEC. */
6022 build_x_compound_expr_from_vec (vec
<tree
, va_gc
> *vec
, const char *msg
,
6023 tsubst_flags_t complain
)
6025 if (vec_safe_is_empty (vec
))
6027 else if (vec
->length () == 1)
6037 if (complain
& tf_error
)
6038 permerror (input_location
,
6039 "%s expression list treated as compound expression",
6042 return error_mark_node
;
6046 for (ix
= 1; vec
->iterate (ix
, &t
); ++ix
)
6047 expr
= build_x_compound_expr (EXPR_LOCATION (t
), expr
,
6054 /* Handle overloading of the ',' operator when needed. */
6057 build_x_compound_expr (location_t loc
, tree op1
, tree op2
,
6058 tsubst_flags_t complain
)
6061 tree orig_op1
= op1
;
6062 tree orig_op2
= op2
;
6064 if (processing_template_decl
)
6066 if (type_dependent_expression_p (op1
)
6067 || type_dependent_expression_p (op2
))
6068 return build_min_nt_loc (loc
, COMPOUND_EXPR
, op1
, op2
);
6069 op1
= build_non_dependent_expr (op1
);
6070 op2
= build_non_dependent_expr (op2
);
6073 result
= build_new_op (loc
, COMPOUND_EXPR
, LOOKUP_NORMAL
, op1
, op2
,
6074 NULL_TREE
, /*overload=*/NULL
, complain
);
6076 result
= cp_build_compound_expr (op1
, op2
, complain
);
6078 if (processing_template_decl
&& result
!= error_mark_node
)
6079 return build_min_non_dep (COMPOUND_EXPR
, result
, orig_op1
, orig_op2
);
6084 /* Like cp_build_compound_expr, but for the c-common bits. */
6087 build_compound_expr (location_t
/*loc*/, tree lhs
, tree rhs
)
6089 return cp_build_compound_expr (lhs
, rhs
, tf_warning_or_error
);
6092 /* Build a compound expression. */
6095 cp_build_compound_expr (tree lhs
, tree rhs
, tsubst_flags_t complain
)
6097 lhs
= convert_to_void (lhs
, ICV_LEFT_OF_COMMA
, complain
);
6099 if (lhs
== error_mark_node
|| rhs
== error_mark_node
)
6100 return error_mark_node
;
6102 if (TREE_CODE (rhs
) == TARGET_EXPR
)
6104 /* If the rhs is a TARGET_EXPR, then build the compound
6105 expression inside the target_expr's initializer. This
6106 helps the compiler to eliminate unnecessary temporaries. */
6107 tree init
= TREE_OPERAND (rhs
, 1);
6109 init
= build2 (COMPOUND_EXPR
, TREE_TYPE (init
), lhs
, init
);
6110 TREE_OPERAND (rhs
, 1) = init
;
6115 if (type_unknown_p (rhs
))
6117 if (complain
& tf_error
)
6118 error ("no context to resolve type of %qE", rhs
);
6119 return error_mark_node
;
6122 return build2 (COMPOUND_EXPR
, TREE_TYPE (rhs
), lhs
, rhs
);
6125 /* Issue a diagnostic message if casting from SRC_TYPE to DEST_TYPE
6126 casts away constness. CAST gives the type of cast. Returns true
6127 if the cast is ill-formed, false if it is well-formed.
6129 ??? This function warns for casting away any qualifier not just
6130 const. We would like to specify exactly what qualifiers are casted
6135 check_for_casting_away_constness (tree src_type
, tree dest_type
,
6136 enum tree_code cast
, tsubst_flags_t complain
)
6138 /* C-style casts are allowed to cast away constness. With
6139 WARN_CAST_QUAL, we still want to issue a warning. */
6140 if (cast
== CAST_EXPR
&& !warn_cast_qual
)
6143 if (!casts_away_constness (src_type
, dest_type
, complain
))
6149 if (complain
& tf_warning
)
6150 warning (OPT_Wcast_qual
,
6151 "cast from type %qT to type %qT casts away qualifiers",
6152 src_type
, dest_type
);
6155 case STATIC_CAST_EXPR
:
6156 if (complain
& tf_error
)
6157 error ("static_cast from type %qT to type %qT casts away qualifiers",
6158 src_type
, dest_type
);
6161 case REINTERPRET_CAST_EXPR
:
6162 if (complain
& tf_error
)
6163 error ("reinterpret_cast from type %qT to type %qT casts away qualifiers",
6164 src_type
, dest_type
);
6173 Warns if the cast from expression EXPR to type TYPE is useless.
6176 maybe_warn_about_useless_cast (tree type
, tree expr
, tsubst_flags_t complain
)
6178 if (warn_useless_cast
6179 && complain
& tf_warning
6180 && c_inhibit_evaluation_warnings
== 0)
6182 if (REFERENCE_REF_P (expr
))
6183 expr
= TREE_OPERAND (expr
, 0);
6185 if ((TREE_CODE (type
) == REFERENCE_TYPE
6186 && (TYPE_REF_IS_RVALUE (type
)
6187 ? xvalue_p (expr
) : real_lvalue_p (expr
))
6188 && same_type_p (TREE_TYPE (expr
), TREE_TYPE (type
)))
6189 || same_type_p (TREE_TYPE (expr
), type
))
6190 warning (OPT_Wuseless_cast
, "useless cast to type %qT", type
);
6194 /* Convert EXPR (an expression with pointer-to-member type) to TYPE
6195 (another pointer-to-member type in the same hierarchy) and return
6196 the converted expression. If ALLOW_INVERSE_P is permitted, a
6197 pointer-to-derived may be converted to pointer-to-base; otherwise,
6198 only the other direction is permitted. If C_CAST_P is true, this
6199 conversion is taking place as part of a C-style cast. */
6202 convert_ptrmem (tree type
, tree expr
, bool allow_inverse_p
,
6203 bool c_cast_p
, tsubst_flags_t complain
)
6205 if (TYPE_PTRDATAMEM_P (type
))
6209 if (TREE_CODE (expr
) == PTRMEM_CST
)
6210 expr
= cplus_expand_constant (expr
);
6211 delta
= get_delta_difference (TYPE_PTRMEM_CLASS_TYPE (TREE_TYPE (expr
)),
6212 TYPE_PTRMEM_CLASS_TYPE (type
),
6214 c_cast_p
, complain
);
6215 if (delta
== error_mark_node
)
6216 return error_mark_node
;
6218 if (!integer_zerop (delta
))
6220 tree cond
, op1
, op2
;
6222 cond
= cp_build_binary_op (input_location
,
6225 build_int_cst (TREE_TYPE (expr
), -1),
6227 op1
= build_nop (ptrdiff_type_node
, expr
);
6228 op2
= cp_build_binary_op (input_location
,
6229 PLUS_EXPR
, op1
, delta
,
6232 expr
= fold_build3_loc (input_location
,
6233 COND_EXPR
, ptrdiff_type_node
, cond
, op1
, op2
);
6237 return build_nop (type
, expr
);
6240 return build_ptrmemfunc (TYPE_PTRMEMFUNC_FN_TYPE (type
), expr
,
6241 allow_inverse_p
, c_cast_p
, complain
);
6244 /* Perform a static_cast from EXPR to TYPE. When C_CAST_P is true,
6245 this static_cast is being attempted as one of the possible casts
6246 allowed by a C-style cast. (In that case, accessibility of base
6247 classes is not considered, and it is OK to cast away
6248 constness.) Return the result of the cast. *VALID_P is set to
6249 indicate whether or not the cast was valid. */
6252 build_static_cast_1 (tree type
, tree expr
, bool c_cast_p
,
6253 bool *valid_p
, tsubst_flags_t complain
)
6259 /* Assume the cast is valid. */
6262 intype
= unlowered_expr_type (expr
);
6264 /* Save casted types in the function's used types hash table. */
6265 used_types_insert (type
);
6267 /* [expr.static.cast]
6269 An lvalue of type "cv1 B", where B is a class type, can be cast
6270 to type "reference to cv2 D", where D is a class derived (clause
6271 _class.derived_) from B, if a valid standard conversion from
6272 "pointer to D" to "pointer to B" exists (_conv.ptr_), cv2 is the
6273 same cv-qualification as, or greater cv-qualification than, cv1,
6274 and B is not a virtual base class of D. */
6275 /* We check this case before checking the validity of "TYPE t =
6276 EXPR;" below because for this case:
6279 struct D : public B { D(const B&); };
6281 void f() { static_cast<const D&>(b); }
6283 we want to avoid constructing a new D. The standard is not
6284 completely clear about this issue, but our interpretation is
6285 consistent with other compilers. */
6286 if (TREE_CODE (type
) == REFERENCE_TYPE
6287 && CLASS_TYPE_P (TREE_TYPE (type
))
6288 && CLASS_TYPE_P (intype
)
6289 && (TYPE_REF_IS_RVALUE (type
) || real_lvalue_p (expr
))
6290 && DERIVED_FROM_P (intype
, TREE_TYPE (type
))
6291 && can_convert (build_pointer_type (TYPE_MAIN_VARIANT (intype
)),
6292 build_pointer_type (TYPE_MAIN_VARIANT
6293 (TREE_TYPE (type
))),
6296 || at_least_as_qualified_p (TREE_TYPE (type
), intype
)))
6300 /* There is a standard conversion from "D*" to "B*" even if "B"
6301 is ambiguous or inaccessible. If this is really a
6302 static_cast, then we check both for inaccessibility and
6303 ambiguity. However, if this is a static_cast being performed
6304 because the user wrote a C-style cast, then accessibility is
6306 base
= lookup_base (TREE_TYPE (type
), intype
,
6307 c_cast_p
? ba_unique
: ba_check
,
6310 /* Convert from "B*" to "D*". This function will check that "B"
6311 is not a virtual base of "D". */
6312 expr
= build_base_path (MINUS_EXPR
, build_address (expr
),
6313 base
, /*nonnull=*/false, complain
);
6314 /* Convert the pointer to a reference -- but then remember that
6315 there are no expressions with reference type in C++.
6317 We call rvalue so that there's an actual tree code
6318 (NON_LVALUE_EXPR) for the static_cast; otherwise, if the operand
6319 is a variable with the same type, the conversion would get folded
6320 away, leaving just the variable and causing lvalue_kind to give
6321 the wrong answer. */
6322 return convert_from_reference (rvalue (cp_fold_convert (type
, expr
)));
6325 /* "A glvalue of type cv1 T1 can be cast to type rvalue reference to
6326 cv2 T2 if cv2 T2 is reference-compatible with cv1 T1 (8.5.3)." */
6327 if (TREE_CODE (type
) == REFERENCE_TYPE
6328 && TYPE_REF_IS_RVALUE (type
)
6329 && (clk
= real_lvalue_p (expr
))
6330 && reference_related_p (TREE_TYPE (type
), intype
)
6331 && (c_cast_p
|| at_least_as_qualified_p (TREE_TYPE (type
), intype
)))
6333 if (clk
== clk_ordinary
)
6335 /* Handle the (non-bit-field) lvalue case here by casting to
6336 lvalue reference and then changing it to an rvalue reference.
6337 Casting an xvalue to rvalue reference will be handled by the
6339 tree lref
= cp_build_reference_type (TREE_TYPE (type
), false);
6340 result
= (perform_direct_initialization_if_possible
6341 (lref
, expr
, c_cast_p
, complain
));
6342 result
= cp_fold_convert (type
, result
);
6343 /* Make sure we don't fold back down to a named rvalue reference,
6344 because that would be an lvalue. */
6345 if (DECL_P (result
))
6346 result
= build1 (NON_LVALUE_EXPR
, type
, result
);
6347 return convert_from_reference (result
);
6350 /* For a bit-field or packed field, bind to a temporary. */
6351 expr
= rvalue (expr
);
6354 /* Resolve overloaded address here rather than once in
6355 implicit_conversion and again in the inverse code below. */
6356 if (TYPE_PTRMEMFUNC_P (type
) && type_unknown_p (expr
))
6358 expr
= instantiate_type (type
, expr
, complain
);
6359 intype
= TREE_TYPE (expr
);
6362 /* [expr.static.cast]
6364 Any expression can be explicitly converted to type cv void. */
6365 if (VOID_TYPE_P (type
))
6366 return convert_to_void (expr
, ICV_CAST
, complain
);
6369 An abstract class shall not be used ... as the type of an explicit
6371 if (abstract_virtuals_error_sfinae (ACU_CAST
, type
, complain
))
6372 return error_mark_node
;
6374 /* [expr.static.cast]
6376 An expression e can be explicitly converted to a type T using a
6377 static_cast of the form static_cast<T>(e) if the declaration T
6378 t(e);" is well-formed, for some invented temporary variable
6380 result
= perform_direct_initialization_if_possible (type
, expr
,
6381 c_cast_p
, complain
);
6384 result
= convert_from_reference (result
);
6386 /* [expr.static.cast]
6388 If T is a reference type, the result is an lvalue; otherwise,
6389 the result is an rvalue. */
6390 if (TREE_CODE (type
) != REFERENCE_TYPE
)
6391 result
= rvalue (result
);
6395 /* [expr.static.cast]
6397 The inverse of any standard conversion sequence (clause _conv_),
6398 other than the lvalue-to-rvalue (_conv.lval_), array-to-pointer
6399 (_conv.array_), function-to-pointer (_conv.func_), and boolean
6400 (_conv.bool_) conversions, can be performed explicitly using
6401 static_cast subject to the restriction that the explicit
6402 conversion does not cast away constness (_expr.const.cast_), and
6403 the following additional rules for specific cases: */
6404 /* For reference, the conversions not excluded are: integral
6405 promotions, floating point promotion, integral conversions,
6406 floating point conversions, floating-integral conversions,
6407 pointer conversions, and pointer to member conversions. */
6410 A value of integral _or enumeration_ type can be explicitly
6411 converted to an enumeration type. */
6412 /* The effect of all that is that any conversion between any two
6413 types which are integral, floating, or enumeration types can be
6415 if ((INTEGRAL_OR_ENUMERATION_TYPE_P (type
)
6416 || SCALAR_FLOAT_TYPE_P (type
))
6417 && (INTEGRAL_OR_ENUMERATION_TYPE_P (intype
)
6418 || SCALAR_FLOAT_TYPE_P (intype
)))
6419 return ocp_convert (type
, expr
, CONV_C_CAST
, LOOKUP_NORMAL
, complain
);
6421 if (TYPE_PTR_P (type
) && TYPE_PTR_P (intype
)
6422 && CLASS_TYPE_P (TREE_TYPE (type
))
6423 && CLASS_TYPE_P (TREE_TYPE (intype
))
6424 && can_convert (build_pointer_type (TYPE_MAIN_VARIANT
6425 (TREE_TYPE (intype
))),
6426 build_pointer_type (TYPE_MAIN_VARIANT
6427 (TREE_TYPE (type
))),
6433 && check_for_casting_away_constness (intype
, type
, STATIC_CAST_EXPR
,
6435 return error_mark_node
;
6436 base
= lookup_base (TREE_TYPE (type
), TREE_TYPE (intype
),
6437 c_cast_p
? ba_unique
: ba_check
,
6439 expr
= build_base_path (MINUS_EXPR
, expr
, base
, /*nonnull=*/false,
6441 return cp_fold_convert(type
, expr
);
6444 if ((TYPE_PTRDATAMEM_P (type
) && TYPE_PTRDATAMEM_P (intype
))
6445 || (TYPE_PTRMEMFUNC_P (type
) && TYPE_PTRMEMFUNC_P (intype
)))
6452 c1
= TYPE_PTRMEM_CLASS_TYPE (intype
);
6453 c2
= TYPE_PTRMEM_CLASS_TYPE (type
);
6455 if (TYPE_PTRDATAMEM_P (type
))
6457 t1
= (build_ptrmem_type
6459 TYPE_MAIN_VARIANT (TYPE_PTRMEM_POINTED_TO_TYPE (intype
))));
6460 t2
= (build_ptrmem_type
6462 TYPE_MAIN_VARIANT (TYPE_PTRMEM_POINTED_TO_TYPE (type
))));
6469 if (can_convert (t1
, t2
, complain
) || can_convert (t2
, t1
, complain
))
6472 && check_for_casting_away_constness (intype
, type
,
6475 return error_mark_node
;
6476 return convert_ptrmem (type
, expr
, /*allow_inverse_p=*/1,
6477 c_cast_p
, complain
);
6481 /* [expr.static.cast]
6483 An rvalue of type "pointer to cv void" can be explicitly
6484 converted to a pointer to object type. A value of type pointer
6485 to object converted to "pointer to cv void" and back to the
6486 original pointer type will have its original value. */
6487 if (TYPE_PTR_P (intype
)
6488 && VOID_TYPE_P (TREE_TYPE (intype
))
6489 && TYPE_PTROB_P (type
))
6492 && check_for_casting_away_constness (intype
, type
, STATIC_CAST_EXPR
,
6494 return error_mark_node
;
6495 return build_nop (type
, expr
);
6499 return error_mark_node
;
6502 /* Return an expression representing static_cast<TYPE>(EXPR). */
6505 build_static_cast (tree type
, tree expr
, tsubst_flags_t complain
)
6510 if (type
== error_mark_node
|| expr
== error_mark_node
)
6511 return error_mark_node
;
6513 if (processing_template_decl
)
6515 expr
= build_min (STATIC_CAST_EXPR
, type
, expr
);
6516 /* We don't know if it will or will not have side effects. */
6517 TREE_SIDE_EFFECTS (expr
) = 1;
6518 return convert_from_reference (expr
);
6521 /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
6522 Strip such NOP_EXPRs if VALUE is being used in non-lvalue context. */
6523 if (TREE_CODE (type
) != REFERENCE_TYPE
6524 && TREE_CODE (expr
) == NOP_EXPR
6525 && TREE_TYPE (expr
) == TREE_TYPE (TREE_OPERAND (expr
, 0)))
6526 expr
= TREE_OPERAND (expr
, 0);
6528 result
= build_static_cast_1 (type
, expr
, /*c_cast_p=*/false, &valid_p
,
6532 if (result
!= error_mark_node
)
6533 maybe_warn_about_useless_cast (type
, expr
, complain
);
6537 if (complain
& tf_error
)
6538 error ("invalid static_cast from type %qT to type %qT",
6539 TREE_TYPE (expr
), type
);
6540 return error_mark_node
;
6543 /* EXPR is an expression with member function or pointer-to-member
6544 function type. TYPE is a pointer type. Converting EXPR to TYPE is
6545 not permitted by ISO C++, but we accept it in some modes. If we
6546 are not in one of those modes, issue a diagnostic. Return the
6547 converted expression. */
6550 convert_member_func_to_ptr (tree type
, tree expr
, tsubst_flags_t complain
)
6555 intype
= TREE_TYPE (expr
);
6556 gcc_assert (TYPE_PTRMEMFUNC_P (intype
)
6557 || TREE_CODE (intype
) == METHOD_TYPE
);
6559 if (!(complain
& tf_warning_or_error
))
6560 return error_mark_node
;
6562 if (pedantic
|| warn_pmf2ptr
)
6563 pedwarn (input_location
, pedantic
? OPT_Wpedantic
: OPT_Wpmf_conversions
,
6564 "converting from %qT to %qT", intype
, type
);
6566 if (TREE_CODE (intype
) == METHOD_TYPE
)
6567 expr
= build_addr_func (expr
, complain
);
6568 else if (TREE_CODE (expr
) == PTRMEM_CST
)
6569 expr
= build_address (PTRMEM_CST_MEMBER (expr
));
6572 decl
= maybe_dummy_object (TYPE_PTRMEM_CLASS_TYPE (intype
), 0);
6573 decl
= build_address (decl
);
6574 expr
= get_member_function_from_ptrfunc (&decl
, expr
, complain
);
6577 if (expr
== error_mark_node
)
6578 return error_mark_node
;
6580 return build_nop (type
, expr
);
6583 /* Return a representation for a reinterpret_cast from EXPR to TYPE.
6584 If C_CAST_P is true, this reinterpret cast is being done as part of
6585 a C-style cast. If VALID_P is non-NULL, *VALID_P is set to
6586 indicate whether or not reinterpret_cast was valid. */
6589 build_reinterpret_cast_1 (tree type
, tree expr
, bool c_cast_p
,
6590 bool *valid_p
, tsubst_flags_t complain
)
6594 /* Assume the cast is invalid. */
6598 if (type
== error_mark_node
|| error_operand_p (expr
))
6599 return error_mark_node
;
6601 intype
= TREE_TYPE (expr
);
6603 /* Save casted types in the function's used types hash table. */
6604 used_types_insert (type
);
6606 /* [expr.reinterpret.cast]
6607 An lvalue expression of type T1 can be cast to the type
6608 "reference to T2" if an expression of type "pointer to T1" can be
6609 explicitly converted to the type "pointer to T2" using a
6610 reinterpret_cast. */
6611 if (TREE_CODE (type
) == REFERENCE_TYPE
)
6613 if (! real_lvalue_p (expr
))
6615 if (complain
& tf_error
)
6616 error ("invalid cast of an rvalue expression of type "
6619 return error_mark_node
;
6622 /* Warn about a reinterpret_cast from "A*" to "B&" if "A" and
6623 "B" are related class types; the reinterpret_cast does not
6624 adjust the pointer. */
6625 if (TYPE_PTR_P (intype
)
6626 && (complain
& tf_warning
)
6627 && (comptypes (TREE_TYPE (intype
), TREE_TYPE (type
),
6628 COMPARE_BASE
| COMPARE_DERIVED
)))
6629 warning (0, "casting %qT to %qT does not dereference pointer",
6632 expr
= cp_build_addr_expr (expr
, complain
);
6634 if (warn_strict_aliasing
> 2)
6635 strict_aliasing_warning (TREE_TYPE (expr
), type
, expr
);
6637 if (expr
!= error_mark_node
)
6638 expr
= build_reinterpret_cast_1
6639 (build_pointer_type (TREE_TYPE (type
)), expr
, c_cast_p
,
6641 if (expr
!= error_mark_node
)
6642 /* cp_build_indirect_ref isn't right for rvalue refs. */
6643 expr
= convert_from_reference (fold_convert (type
, expr
));
6647 /* As a G++ extension, we consider conversions from member
6648 functions, and pointers to member functions to
6649 pointer-to-function and pointer-to-void types. If
6650 -Wno-pmf-conversions has not been specified,
6651 convert_member_func_to_ptr will issue an error message. */
6652 if ((TYPE_PTRMEMFUNC_P (intype
)
6653 || TREE_CODE (intype
) == METHOD_TYPE
)
6654 && TYPE_PTR_P (type
)
6655 && (TREE_CODE (TREE_TYPE (type
)) == FUNCTION_TYPE
6656 || VOID_TYPE_P (TREE_TYPE (type
))))
6657 return convert_member_func_to_ptr (type
, expr
, complain
);
6659 /* If the cast is not to a reference type, the lvalue-to-rvalue,
6660 array-to-pointer, and function-to-pointer conversions are
6662 expr
= decay_conversion (expr
, complain
);
6664 /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
6665 Strip such NOP_EXPRs if VALUE is being used in non-lvalue context. */
6666 if (TREE_CODE (expr
) == NOP_EXPR
6667 && TREE_TYPE (expr
) == TREE_TYPE (TREE_OPERAND (expr
, 0)))
6668 expr
= TREE_OPERAND (expr
, 0);
6670 if (error_operand_p (expr
))
6671 return error_mark_node
;
6673 intype
= TREE_TYPE (expr
);
6675 /* [expr.reinterpret.cast]
6676 A pointer can be converted to any integral type large enough to
6677 hold it. ... A value of type std::nullptr_t can be converted to
6678 an integral type; the conversion has the same meaning and
6679 validity as a conversion of (void*)0 to the integral type. */
6680 if (CP_INTEGRAL_TYPE_P (type
)
6681 && (TYPE_PTR_P (intype
) || NULLPTR_TYPE_P (intype
)))
6683 if (TYPE_PRECISION (type
) < TYPE_PRECISION (intype
))
6685 if (complain
& tf_error
)
6686 permerror (input_location
, "cast from %qT to %qT loses precision",
6689 return error_mark_node
;
6691 if (NULLPTR_TYPE_P (intype
))
6692 return build_int_cst (type
, 0);
6694 /* [expr.reinterpret.cast]
6695 A value of integral or enumeration type can be explicitly
6696 converted to a pointer. */
6697 else if (TYPE_PTR_P (type
) && INTEGRAL_OR_ENUMERATION_TYPE_P (intype
))
6700 else if ((INTEGRAL_OR_ENUMERATION_TYPE_P (type
)
6701 || TYPE_PTR_OR_PTRMEM_P (type
))
6702 && same_type_p (type
, intype
))
6704 return fold_if_not_in_template (build_nop (type
, expr
));
6705 else if ((TYPE_PTRFN_P (type
) && TYPE_PTRFN_P (intype
))
6706 || (TYPE_PTRMEMFUNC_P (type
) && TYPE_PTRMEMFUNC_P (intype
)))
6707 return fold_if_not_in_template (build_nop (type
, expr
));
6708 else if ((TYPE_PTRDATAMEM_P (type
) && TYPE_PTRDATAMEM_P (intype
))
6709 || (TYPE_PTROBV_P (type
) && TYPE_PTROBV_P (intype
)))
6714 && check_for_casting_away_constness (intype
, type
,
6715 REINTERPRET_CAST_EXPR
,
6717 return error_mark_node
;
6718 /* Warn about possible alignment problems. */
6719 if (STRICT_ALIGNMENT
&& warn_cast_align
6720 && (complain
& tf_warning
)
6721 && !VOID_TYPE_P (type
)
6722 && TREE_CODE (TREE_TYPE (intype
)) != FUNCTION_TYPE
6723 && COMPLETE_TYPE_P (TREE_TYPE (type
))
6724 && COMPLETE_TYPE_P (TREE_TYPE (intype
))
6725 && TYPE_ALIGN (TREE_TYPE (type
)) > TYPE_ALIGN (TREE_TYPE (intype
)))
6726 warning (OPT_Wcast_align
, "cast from %qT to %qT "
6727 "increases required alignment of target type", intype
, type
);
6729 /* We need to strip nops here, because the front end likes to
6730 create (int *)&a for array-to-pointer decay, instead of &a[0]. */
6732 if (warn_strict_aliasing
<= 2)
6733 strict_aliasing_warning (intype
, type
, sexpr
);
6735 return fold_if_not_in_template (build_nop (type
, expr
));
6737 else if ((TYPE_PTRFN_P (type
) && TYPE_PTROBV_P (intype
))
6738 || (TYPE_PTRFN_P (intype
) && TYPE_PTROBV_P (type
)))
6740 if (complain
& tf_warning
)
6741 /* C++11 5.2.10 p8 says that "Converting a function pointer to an
6742 object pointer type or vice versa is conditionally-supported." */
6743 warning (OPT_Wconditionally_supported
,
6744 "casting between pointer-to-function and pointer-to-object "
6745 "is conditionally-supported");
6746 return fold_if_not_in_template (build_nop (type
, expr
));
6748 else if (TREE_CODE (type
) == VECTOR_TYPE
)
6749 return fold_if_not_in_template (convert_to_vector (type
, expr
));
6750 else if (TREE_CODE (intype
) == VECTOR_TYPE
6751 && INTEGRAL_OR_ENUMERATION_TYPE_P (type
))
6752 return fold_if_not_in_template (convert_to_integer (type
, expr
));
6757 if (complain
& tf_error
)
6758 error ("invalid cast from type %qT to type %qT", intype
, type
);
6759 return error_mark_node
;
6762 return cp_convert (type
, expr
, complain
);
6766 build_reinterpret_cast (tree type
, tree expr
, tsubst_flags_t complain
)
6770 if (type
== error_mark_node
|| expr
== error_mark_node
)
6771 return error_mark_node
;
6773 if (processing_template_decl
)
6775 tree t
= build_min (REINTERPRET_CAST_EXPR
, type
, expr
);
6777 if (!TREE_SIDE_EFFECTS (t
)
6778 && type_dependent_expression_p (expr
))
6779 /* There might turn out to be side effects inside expr. */
6780 TREE_SIDE_EFFECTS (t
) = 1;
6781 return convert_from_reference (t
);
6784 r
= build_reinterpret_cast_1 (type
, expr
, /*c_cast_p=*/false,
6785 /*valid_p=*/NULL
, complain
);
6786 if (r
!= error_mark_node
)
6787 maybe_warn_about_useless_cast (type
, expr
, complain
);
6791 /* Perform a const_cast from EXPR to TYPE. If the cast is valid,
6792 return an appropriate expression. Otherwise, return
6793 error_mark_node. If the cast is not valid, and COMPLAIN is true,
6794 then a diagnostic will be issued. If VALID_P is non-NULL, we are
6795 performing a C-style cast, its value upon return will indicate
6796 whether or not the conversion succeeded. */
6799 build_const_cast_1 (tree dst_type
, tree expr
, tsubst_flags_t complain
,
6803 tree reference_type
;
6805 /* Callers are responsible for handling error_mark_node as a
6806 destination type. */
6807 gcc_assert (dst_type
!= error_mark_node
);
6808 /* In a template, callers should be building syntactic
6809 representations of casts, not using this machinery. */
6810 gcc_assert (!processing_template_decl
);
6812 /* Assume the conversion is invalid. */
6816 if (!POINTER_TYPE_P (dst_type
) && !TYPE_PTRDATAMEM_P (dst_type
))
6818 if (complain
& tf_error
)
6819 error ("invalid use of const_cast with type %qT, "
6820 "which is not a pointer, "
6821 "reference, nor a pointer-to-data-member type", dst_type
);
6822 return error_mark_node
;
6825 if (TREE_CODE (TREE_TYPE (dst_type
)) == FUNCTION_TYPE
)
6827 if (complain
& tf_error
)
6828 error ("invalid use of const_cast with type %qT, which is a pointer "
6829 "or reference to a function type", dst_type
);
6830 return error_mark_node
;
6833 /* Save casted types in the function's used types hash table. */
6834 used_types_insert (dst_type
);
6836 src_type
= TREE_TYPE (expr
);
6837 /* Expressions do not really have reference types. */
6838 if (TREE_CODE (src_type
) == REFERENCE_TYPE
)
6839 src_type
= TREE_TYPE (src_type
);
6841 /* [expr.const.cast]
6843 For two object types T1 and T2, if a pointer to T1 can be explicitly
6844 converted to the type "pointer to T2" using a const_cast, then the
6845 following conversions can also be made:
6847 -- an lvalue of type T1 can be explicitly converted to an lvalue of
6848 type T2 using the cast const_cast<T2&>;
6850 -- a glvalue of type T1 can be explicitly converted to an xvalue of
6851 type T2 using the cast const_cast<T2&&>; and
6853 -- if T1 is a class type, a prvalue of type T1 can be explicitly
6854 converted to an xvalue of type T2 using the cast const_cast<T2&&>. */
6856 if (TREE_CODE (dst_type
) == REFERENCE_TYPE
)
6858 reference_type
= dst_type
;
6859 if (!TYPE_REF_IS_RVALUE (dst_type
)
6860 ? real_lvalue_p (expr
)
6861 : (CLASS_TYPE_P (TREE_TYPE (dst_type
))
6863 : lvalue_or_rvalue_with_address_p (expr
)))
6867 if (complain
& tf_error
)
6868 error ("invalid const_cast of an rvalue of type %qT to type %qT",
6869 src_type
, dst_type
);
6870 return error_mark_node
;
6872 dst_type
= build_pointer_type (TREE_TYPE (dst_type
));
6873 src_type
= build_pointer_type (src_type
);
6877 reference_type
= NULL_TREE
;
6878 /* If the destination type is not a reference type, the
6879 lvalue-to-rvalue, array-to-pointer, and function-to-pointer
6880 conversions are performed. */
6881 src_type
= type_decays_to (src_type
);
6882 if (src_type
== error_mark_node
)
6883 return error_mark_node
;
6886 if (TYPE_PTR_P (src_type
) || TYPE_PTRDATAMEM_P (src_type
))
6888 if (comp_ptr_ttypes_const (dst_type
, src_type
))
6893 /* This cast is actually a C-style cast. Issue a warning if
6894 the user is making a potentially unsafe cast. */
6895 check_for_casting_away_constness (src_type
, dst_type
,
6896 CAST_EXPR
, complain
);
6900 expr
= cp_build_addr_expr (expr
, complain
);
6901 if (expr
== error_mark_node
)
6902 return error_mark_node
;
6903 expr
= build_nop (reference_type
, expr
);
6904 return convert_from_reference (expr
);
6908 expr
= decay_conversion (expr
, complain
);
6909 if (expr
== error_mark_node
)
6910 return error_mark_node
;
6912 /* build_c_cast puts on a NOP_EXPR to make the result not an
6913 lvalue. Strip such NOP_EXPRs if VALUE is being used in
6914 non-lvalue context. */
6915 if (TREE_CODE (expr
) == NOP_EXPR
6916 && TREE_TYPE (expr
) == TREE_TYPE (TREE_OPERAND (expr
, 0)))
6917 expr
= TREE_OPERAND (expr
, 0);
6918 return build_nop (dst_type
, expr
);
6922 && !at_least_as_qualified_p (TREE_TYPE (dst_type
),
6923 TREE_TYPE (src_type
)))
6924 check_for_casting_away_constness (src_type
, dst_type
, CAST_EXPR
,
6928 if (complain
& tf_error
)
6929 error ("invalid const_cast from type %qT to type %qT",
6930 src_type
, dst_type
);
6931 return error_mark_node
;
6935 build_const_cast (tree type
, tree expr
, tsubst_flags_t complain
)
6939 if (type
== error_mark_node
|| error_operand_p (expr
))
6940 return error_mark_node
;
6942 if (processing_template_decl
)
6944 tree t
= build_min (CONST_CAST_EXPR
, type
, expr
);
6946 if (!TREE_SIDE_EFFECTS (t
)
6947 && type_dependent_expression_p (expr
))
6948 /* There might turn out to be side effects inside expr. */
6949 TREE_SIDE_EFFECTS (t
) = 1;
6950 return convert_from_reference (t
);
6953 r
= build_const_cast_1 (type
, expr
, complain
, /*valid_p=*/NULL
);
6954 if (r
!= error_mark_node
)
6955 maybe_warn_about_useless_cast (type
, expr
, complain
);
6959 /* Like cp_build_c_cast, but for the c-common bits. */
6962 build_c_cast (location_t
/*loc*/, tree type
, tree expr
)
6964 return cp_build_c_cast (type
, expr
, tf_warning_or_error
);
6967 /* Build an expression representing an explicit C-style cast to type
6968 TYPE of expression EXPR. */
6971 cp_build_c_cast (tree type
, tree expr
, tsubst_flags_t complain
)
6977 if (type
== error_mark_node
|| error_operand_p (expr
))
6978 return error_mark_node
;
6980 if (processing_template_decl
)
6982 tree t
= build_min (CAST_EXPR
, type
,
6983 tree_cons (NULL_TREE
, value
, NULL_TREE
));
6984 /* We don't know if it will or will not have side effects. */
6985 TREE_SIDE_EFFECTS (t
) = 1;
6986 return convert_from_reference (t
);
6989 /* Casts to a (pointer to a) specific ObjC class (or 'id' or
6990 'Class') should always be retained, because this information aids
6991 in method lookup. */
6992 if (objc_is_object_ptr (type
)
6993 && objc_is_object_ptr (TREE_TYPE (expr
)))
6994 return build_nop (type
, expr
);
6996 /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
6997 Strip such NOP_EXPRs if VALUE is being used in non-lvalue context. */
6998 if (TREE_CODE (type
) != REFERENCE_TYPE
6999 && TREE_CODE (value
) == NOP_EXPR
7000 && TREE_TYPE (value
) == TREE_TYPE (TREE_OPERAND (value
, 0)))
7001 value
= TREE_OPERAND (value
, 0);
7003 if (TREE_CODE (type
) == ARRAY_TYPE
)
7005 /* Allow casting from T1* to T2[] because Cfront allows it.
7006 NIHCL uses it. It is not valid ISO C++ however. */
7007 if (TYPE_PTR_P (TREE_TYPE (expr
)))
7009 if (complain
& tf_error
)
7010 permerror (input_location
, "ISO C++ forbids casting to an array type %qT", type
);
7012 return error_mark_node
;
7013 type
= build_pointer_type (TREE_TYPE (type
));
7017 if (complain
& tf_error
)
7018 error ("ISO C++ forbids casting to an array type %qT", type
);
7019 return error_mark_node
;
7023 if (TREE_CODE (type
) == FUNCTION_TYPE
7024 || TREE_CODE (type
) == METHOD_TYPE
)
7026 if (complain
& tf_error
)
7027 error ("invalid cast to function type %qT", type
);
7028 return error_mark_node
;
7031 if (TYPE_PTR_P (type
)
7032 && TREE_CODE (TREE_TYPE (value
)) == INTEGER_TYPE
7033 /* Casting to an integer of smaller size is an error detected elsewhere. */
7034 && TYPE_PRECISION (type
) > TYPE_PRECISION (TREE_TYPE (value
))
7035 /* Don't warn about converting any constant. */
7036 && !TREE_CONSTANT (value
))
7037 warning_at (input_location
, OPT_Wint_to_pointer_cast
,
7038 "cast to pointer from integer of different size");
7040 /* A C-style cast can be a const_cast. */
7041 result
= build_const_cast_1 (type
, value
, complain
& tf_warning
,
7045 if (result
!= error_mark_node
)
7046 maybe_warn_about_useless_cast (type
, value
, complain
);
7050 /* Or a static cast. */
7051 result
= build_static_cast_1 (type
, value
, /*c_cast_p=*/true,
7052 &valid_p
, complain
);
7053 /* Or a reinterpret_cast. */
7055 result
= build_reinterpret_cast_1 (type
, value
, /*c_cast_p=*/true,
7056 &valid_p
, complain
);
7057 /* The static_cast or reinterpret_cast may be followed by a
7060 /* A valid cast may result in errors if, for example, a
7061 conversion to an ambiguous base class is required. */
7062 && !error_operand_p (result
))
7066 maybe_warn_about_useless_cast (type
, value
, complain
);
7068 /* Non-class rvalues always have cv-unqualified type. */
7069 if (!CLASS_TYPE_P (type
))
7070 type
= TYPE_MAIN_VARIANT (type
);
7071 result_type
= TREE_TYPE (result
);
7072 if (!CLASS_TYPE_P (result_type
) && TREE_CODE (type
) != REFERENCE_TYPE
)
7073 result_type
= TYPE_MAIN_VARIANT (result_type
);
7074 /* If the type of RESULT does not match TYPE, perform a
7075 const_cast to make it match. If the static_cast or
7076 reinterpret_cast succeeded, we will differ by at most
7077 cv-qualification, so the follow-on const_cast is guaranteed
7079 if (!same_type_p (non_reference (type
), non_reference (result_type
)))
7081 result
= build_const_cast_1 (type
, result
, false, &valid_p
);
7082 gcc_assert (valid_p
);
7087 return error_mark_node
;
7090 /* For use from the C common bits. */
7092 build_modify_expr (location_t
/*location*/,
7093 tree lhs
, tree
/*lhs_origtype*/,
7094 enum tree_code modifycode
,
7095 location_t
/*rhs_location*/, tree rhs
,
7096 tree
/*rhs_origtype*/)
7098 return cp_build_modify_expr (lhs
, modifycode
, rhs
, tf_warning_or_error
);
7101 /* Build an assignment expression of lvalue LHS from value RHS.
7102 MODIFYCODE is the code for a binary operator that we use
7103 to combine the old value of LHS with RHS to get the new value.
7104 Or else MODIFYCODE is NOP_EXPR meaning do a simple assignment.
7106 C++: If MODIFYCODE is INIT_EXPR, then leave references unbashed. */
7109 cp_build_modify_expr (tree lhs
, enum tree_code modifycode
, tree rhs
,
7110 tsubst_flags_t complain
)
7114 tree lhstype
= TREE_TYPE (lhs
);
7115 tree olhstype
= lhstype
;
7116 bool plain_assign
= (modifycode
== NOP_EXPR
);
7118 /* Avoid duplicate error messages from operands that had errors. */
7119 if (error_operand_p (lhs
) || error_operand_p (rhs
))
7120 return error_mark_node
;
7122 /* Handle control structure constructs used as "lvalues". */
7123 switch (TREE_CODE (lhs
))
7125 /* Handle --foo = 5; as these are valid constructs in C++. */
7126 case PREDECREMENT_EXPR
:
7127 case PREINCREMENT_EXPR
:
7128 if (TREE_SIDE_EFFECTS (TREE_OPERAND (lhs
, 0)))
7129 lhs
= build2 (TREE_CODE (lhs
), TREE_TYPE (lhs
),
7130 stabilize_reference (TREE_OPERAND (lhs
, 0)),
7131 TREE_OPERAND (lhs
, 1));
7132 newrhs
= cp_build_modify_expr (TREE_OPERAND (lhs
, 0),
7133 modifycode
, rhs
, complain
);
7134 if (newrhs
== error_mark_node
)
7135 return error_mark_node
;
7136 return build2 (COMPOUND_EXPR
, lhstype
, lhs
, newrhs
);
7138 /* Handle (a, b) used as an "lvalue". */
7140 newrhs
= cp_build_modify_expr (TREE_OPERAND (lhs
, 1),
7141 modifycode
, rhs
, complain
);
7142 if (newrhs
== error_mark_node
)
7143 return error_mark_node
;
7144 return build2 (COMPOUND_EXPR
, lhstype
,
7145 TREE_OPERAND (lhs
, 0), newrhs
);
7148 if (TREE_SIDE_EFFECTS (TREE_OPERAND (lhs
, 0)))
7149 lhs
= build2 (TREE_CODE (lhs
), TREE_TYPE (lhs
),
7150 stabilize_reference (TREE_OPERAND (lhs
, 0)),
7151 TREE_OPERAND (lhs
, 1));
7152 newrhs
= cp_build_modify_expr (TREE_OPERAND (lhs
, 0), modifycode
, rhs
,
7154 if (newrhs
== error_mark_node
)
7155 return error_mark_node
;
7156 return build2 (COMPOUND_EXPR
, lhstype
, lhs
, newrhs
);
7160 /* MIN_EXPR and MAX_EXPR are currently only permitted as lvalues,
7161 when neither operand has side-effects. */
7162 if (!lvalue_or_else (lhs
, lv_assign
, complain
))
7163 return error_mark_node
;
7165 gcc_assert (!TREE_SIDE_EFFECTS (TREE_OPERAND (lhs
, 0))
7166 && !TREE_SIDE_EFFECTS (TREE_OPERAND (lhs
, 1)));
7168 lhs
= build3 (COND_EXPR
, TREE_TYPE (lhs
),
7169 build2 (TREE_CODE (lhs
) == MIN_EXPR
? LE_EXPR
: GE_EXPR
,
7171 TREE_OPERAND (lhs
, 0),
7172 TREE_OPERAND (lhs
, 1)),
7173 TREE_OPERAND (lhs
, 0),
7174 TREE_OPERAND (lhs
, 1));
7177 /* Handle (a ? b : c) used as an "lvalue". */
7180 /* Produce (a ? (b = rhs) : (c = rhs))
7181 except that the RHS goes through a save-expr
7182 so the code to compute it is only emitted once. */
7184 tree preeval
= NULL_TREE
;
7186 if (VOID_TYPE_P (TREE_TYPE (rhs
)))
7188 if (complain
& tf_error
)
7189 error ("void value not ignored as it ought to be");
7190 return error_mark_node
;
7193 rhs
= stabilize_expr (rhs
, &preeval
);
7195 /* Check this here to avoid odd errors when trying to convert
7196 a throw to the type of the COND_EXPR. */
7197 if (!lvalue_or_else (lhs
, lv_assign
, complain
))
7198 return error_mark_node
;
7200 cond
= build_conditional_expr
7201 (input_location
, TREE_OPERAND (lhs
, 0),
7202 cp_build_modify_expr (TREE_OPERAND (lhs
, 1),
7203 modifycode
, rhs
, complain
),
7204 cp_build_modify_expr (TREE_OPERAND (lhs
, 2),
7205 modifycode
, rhs
, complain
),
7208 if (cond
== error_mark_node
)
7210 /* Make sure the code to compute the rhs comes out
7211 before the split. */
7213 cond
= build2 (COMPOUND_EXPR
, TREE_TYPE (lhs
), preeval
, cond
);
7221 if (modifycode
== INIT_EXPR
)
7223 if (BRACE_ENCLOSED_INITIALIZER_P (rhs
))
7224 /* Do the default thing. */;
7225 else if (TREE_CODE (rhs
) == CONSTRUCTOR
)
7227 /* Compound literal. */
7228 if (! same_type_p (TREE_TYPE (rhs
), lhstype
))
7229 /* Call convert to generate an error; see PR 11063. */
7230 rhs
= convert (lhstype
, rhs
);
7231 result
= build2 (INIT_EXPR
, lhstype
, lhs
, rhs
);
7232 TREE_SIDE_EFFECTS (result
) = 1;
7235 else if (! MAYBE_CLASS_TYPE_P (lhstype
))
7236 /* Do the default thing. */;
7239 vec
<tree
, va_gc
> *rhs_vec
= make_tree_vector_single (rhs
);
7240 result
= build_special_member_call (lhs
, complete_ctor_identifier
,
7241 &rhs_vec
, lhstype
, LOOKUP_NORMAL
,
7243 release_tree_vector (rhs_vec
);
7244 if (result
== NULL_TREE
)
7245 return error_mark_node
;
7251 lhs
= require_complete_type_sfinae (lhs
, complain
);
7252 if (lhs
== error_mark_node
)
7253 return error_mark_node
;
7255 if (modifycode
== NOP_EXPR
)
7257 if (c_dialect_objc ())
7259 result
= objc_maybe_build_modify_expr (lhs
, rhs
);
7264 /* `operator=' is not an inheritable operator. */
7265 if (! MAYBE_CLASS_TYPE_P (lhstype
))
7266 /* Do the default thing. */;
7269 result
= build_new_op (input_location
, MODIFY_EXPR
,
7270 LOOKUP_NORMAL
, lhs
, rhs
,
7271 make_node (NOP_EXPR
), /*overload=*/NULL
,
7273 if (result
== NULL_TREE
)
7274 return error_mark_node
;
7281 tree init
= NULL_TREE
;
7283 /* A binary op has been requested. Combine the old LHS
7284 value with the RHS producing the value we should actually
7285 store into the LHS. */
7286 gcc_assert (!((TREE_CODE (lhstype
) == REFERENCE_TYPE
7287 && MAYBE_CLASS_TYPE_P (TREE_TYPE (lhstype
)))
7288 || MAYBE_CLASS_TYPE_P (lhstype
)));
7290 /* Preevaluate the RHS to make sure its evaluation is complete
7291 before the lvalue-to-rvalue conversion of the LHS:
7293 [expr.ass] With respect to an indeterminately-sequenced
7294 function call, the operation of a compound assignment is a
7295 single evaluation. [ Note: Therefore, a function call shall
7296 not intervene between the lvalue-to-rvalue conversion and the
7297 side effect associated with any single compound assignment
7298 operator. -- end note ] */
7299 lhs
= stabilize_reference (lhs
);
7300 if (TREE_SIDE_EFFECTS (rhs
))
7301 rhs
= mark_rvalue_use (rhs
);
7302 rhs
= stabilize_expr (rhs
, &init
);
7303 newrhs
= cp_build_binary_op (input_location
,
7304 modifycode
, lhs
, rhs
,
7306 if (newrhs
== error_mark_node
)
7308 if (complain
& tf_error
)
7309 error (" in evaluation of %<%Q(%#T, %#T)%>", modifycode
,
7310 TREE_TYPE (lhs
), TREE_TYPE (rhs
));
7311 return error_mark_node
;
7315 newrhs
= build2 (COMPOUND_EXPR
, TREE_TYPE (newrhs
), init
, newrhs
);
7317 /* Now it looks like a plain assignment. */
7318 modifycode
= NOP_EXPR
;
7319 if (c_dialect_objc ())
7321 result
= objc_maybe_build_modify_expr (lhs
, newrhs
);
7326 gcc_assert (TREE_CODE (lhstype
) != REFERENCE_TYPE
);
7327 gcc_assert (TREE_CODE (TREE_TYPE (newrhs
)) != REFERENCE_TYPE
);
7330 /* The left-hand side must be an lvalue. */
7331 if (!lvalue_or_else (lhs
, lv_assign
, complain
))
7332 return error_mark_node
;
7334 /* Warn about modifying something that is `const'. Don't warn if
7335 this is initialization. */
7336 if (modifycode
!= INIT_EXPR
7337 && (TREE_READONLY (lhs
) || CP_TYPE_CONST_P (lhstype
)
7338 /* Functions are not modifiable, even though they are
7340 || TREE_CODE (TREE_TYPE (lhs
)) == FUNCTION_TYPE
7341 || TREE_CODE (TREE_TYPE (lhs
)) == METHOD_TYPE
7342 /* If it's an aggregate and any field is const, then it is
7343 effectively const. */
7344 || (CLASS_TYPE_P (lhstype
)
7345 && C_TYPE_FIELDS_READONLY (lhstype
))))
7347 if (complain
& tf_error
)
7348 cxx_readonly_error (lhs
, lv_assign
);
7350 return error_mark_node
;
7353 /* If storing into a structure or union member, it may have been given a
7354 lowered bitfield type. We need to convert to the declared type first,
7355 so retrieve it now. */
7357 olhstype
= unlowered_expr_type (lhs
);
7359 /* Convert new value to destination type. */
7361 if (TREE_CODE (lhstype
) == ARRAY_TYPE
)
7365 if (BRACE_ENCLOSED_INITIALIZER_P (newrhs
))
7367 if (modifycode
!= INIT_EXPR
)
7369 if (complain
& tf_error
)
7370 error ("assigning to an array from an initializer list");
7371 return error_mark_node
;
7373 if (check_array_initializer (lhs
, lhstype
, newrhs
))
7374 return error_mark_node
;
7375 newrhs
= digest_init (lhstype
, newrhs
, complain
);
7376 if (newrhs
== error_mark_node
)
7377 return error_mark_node
;
7380 else if (!same_or_base_type_p (TYPE_MAIN_VARIANT (lhstype
),
7381 TYPE_MAIN_VARIANT (TREE_TYPE (newrhs
))))
7383 if (complain
& tf_error
)
7384 error ("incompatible types in assignment of %qT to %qT",
7385 TREE_TYPE (rhs
), lhstype
);
7386 return error_mark_node
;
7389 /* Allow array assignment in compiler-generated code. */
7390 else if (!current_function_decl
7391 || !DECL_DEFAULTED_FN (current_function_decl
))
7393 /* This routine is used for both initialization and assignment.
7394 Make sure the diagnostic message differentiates the context. */
7395 if (complain
& tf_error
)
7397 if (modifycode
== INIT_EXPR
)
7398 error ("array used as initializer");
7400 error ("invalid array assignment");
7402 return error_mark_node
;
7405 from_array
= TREE_CODE (TREE_TYPE (newrhs
)) == ARRAY_TYPE
7406 ? 1 + (modifycode
!= INIT_EXPR
): 0;
7407 return build_vec_init (lhs
, NULL_TREE
, newrhs
,
7408 /*explicit_value_init_p=*/false,
7409 from_array
, complain
);
7412 if (modifycode
== INIT_EXPR
)
7413 /* Calls with INIT_EXPR are all direct-initialization, so don't set
7414 LOOKUP_ONLYCONVERTING. */
7415 newrhs
= convert_for_initialization (lhs
, olhstype
, newrhs
, LOOKUP_NORMAL
,
7416 ICR_INIT
, NULL_TREE
, 0,
7419 newrhs
= convert_for_assignment (olhstype
, newrhs
, ICR_ASSIGN
,
7420 NULL_TREE
, 0, complain
, LOOKUP_IMPLICIT
);
7422 if (!same_type_p (lhstype
, olhstype
))
7423 newrhs
= cp_convert_and_check (lhstype
, newrhs
, complain
);
7425 if (modifycode
!= INIT_EXPR
)
7427 if (TREE_CODE (newrhs
) == CALL_EXPR
7428 && TYPE_NEEDS_CONSTRUCTING (lhstype
))
7429 newrhs
= build_cplus_new (lhstype
, newrhs
, complain
);
7431 /* Can't initialize directly from a TARGET_EXPR, since that would
7432 cause the lhs to be constructed twice, and possibly result in
7433 accidental self-initialization. So we force the TARGET_EXPR to be
7434 expanded without a target. */
7435 if (TREE_CODE (newrhs
) == TARGET_EXPR
)
7436 newrhs
= build2 (COMPOUND_EXPR
, TREE_TYPE (newrhs
), newrhs
,
7437 TREE_OPERAND (newrhs
, 0));
7440 if (newrhs
== error_mark_node
)
7441 return error_mark_node
;
7443 if (c_dialect_objc () && flag_objc_gc
)
7445 result
= objc_generate_write_barrier (lhs
, modifycode
, newrhs
);
7451 result
= build2 (modifycode
== NOP_EXPR
? MODIFY_EXPR
: INIT_EXPR
,
7452 lhstype
, lhs
, newrhs
);
7454 TREE_SIDE_EFFECTS (result
) = 1;
7456 TREE_NO_WARNING (result
) = 1;
7462 build_x_modify_expr (location_t loc
, tree lhs
, enum tree_code modifycode
,
7463 tree rhs
, tsubst_flags_t complain
)
7465 if (processing_template_decl
)
7466 return build_min_nt_loc (loc
, MODOP_EXPR
, lhs
,
7467 build_min_nt_loc (loc
, modifycode
, NULL_TREE
,
7470 if (modifycode
!= NOP_EXPR
)
7472 tree rval
= build_new_op (loc
, MODIFY_EXPR
, LOOKUP_NORMAL
, lhs
, rhs
,
7473 make_node (modifycode
), /*overload=*/NULL
,
7477 TREE_NO_WARNING (rval
) = 1;
7481 return cp_build_modify_expr (lhs
, modifycode
, rhs
, complain
);
7484 /* Helper function for get_delta_difference which assumes FROM is a base
7485 class of TO. Returns a delta for the conversion of pointer-to-member
7486 of FROM to pointer-to-member of TO. If the conversion is invalid and
7487 tf_error is not set in COMPLAIN returns error_mark_node, otherwise
7488 returns zero. If FROM is not a base class of TO, returns NULL_TREE.
7489 If C_CAST_P is true, this conversion is taking place as part of a
7493 get_delta_difference_1 (tree from
, tree to
, bool c_cast_p
,
7494 tsubst_flags_t complain
)
7499 binfo
= lookup_base (to
, from
, c_cast_p
? ba_unique
: ba_check
,
7502 if (binfo
== error_mark_node
)
7504 if (!(complain
& tf_error
))
7505 return error_mark_node
;
7507 error (" in pointer to member function conversion");
7508 return size_zero_node
;
7512 if (kind
!= bk_via_virtual
)
7513 return BINFO_OFFSET (binfo
);
7515 /* FROM is a virtual base class of TO. Issue an error or warning
7516 depending on whether or not this is a reinterpret cast. */
7518 if (!(complain
& tf_error
))
7519 return error_mark_node
;
7521 error ("pointer to member conversion via virtual base %qT",
7522 BINFO_TYPE (binfo_from_vbase (binfo
)));
7524 return size_zero_node
;
7531 /* Get difference in deltas for different pointer to member function
7532 types. If the conversion is invalid and tf_error is not set in
7533 COMPLAIN, returns error_mark_node, otherwise returns an integer
7534 constant of type PTRDIFF_TYPE_NODE and its value is zero if the
7535 conversion is invalid. If ALLOW_INVERSE_P is true, then allow reverse
7536 conversions as well. If C_CAST_P is true this conversion is taking
7537 place as part of a C-style cast.
7539 Note that the naming of FROM and TO is kind of backwards; the return
7540 value is what we add to a TO in order to get a FROM. They are named
7541 this way because we call this function to find out how to convert from
7542 a pointer to member of FROM to a pointer to member of TO. */
7545 get_delta_difference (tree from
, tree to
,
7546 bool allow_inverse_p
,
7547 bool c_cast_p
, tsubst_flags_t complain
)
7551 if (same_type_ignoring_top_level_qualifiers_p (from
, to
))
7552 /* Pointer to member of incomplete class is permitted*/
7553 result
= size_zero_node
;
7555 result
= get_delta_difference_1 (from
, to
, c_cast_p
, complain
);
7557 if (result
== error_mark_node
)
7558 return error_mark_node
;
7562 if (!allow_inverse_p
)
7564 if (!(complain
& tf_error
))
7565 return error_mark_node
;
7567 error_not_base_type (from
, to
);
7568 error (" in pointer to member conversion");
7569 result
= size_zero_node
;
7573 result
= get_delta_difference_1 (to
, from
, c_cast_p
, complain
);
7575 if (result
== error_mark_node
)
7576 return error_mark_node
;
7579 result
= size_diffop_loc (input_location
,
7580 size_zero_node
, result
);
7583 if (!(complain
& tf_error
))
7584 return error_mark_node
;
7586 error_not_base_type (from
, to
);
7587 error (" in pointer to member conversion");
7588 result
= size_zero_node
;
7593 return fold_if_not_in_template (convert_to_integer (ptrdiff_type_node
,
7597 /* Return a constructor for the pointer-to-member-function TYPE using
7598 the other components as specified. */
7601 build_ptrmemfunc1 (tree type
, tree delta
, tree pfn
)
7606 vec
<constructor_elt
, va_gc
> *v
;
7608 /* Pull the FIELD_DECLs out of the type. */
7609 pfn_field
= TYPE_FIELDS (type
);
7610 delta_field
= DECL_CHAIN (pfn_field
);
7612 /* Make sure DELTA has the type we want. */
7613 delta
= convert_and_check (delta_type_node
, delta
);
7615 /* Convert to the correct target type if necessary. */
7616 pfn
= fold_convert (TREE_TYPE (pfn_field
), pfn
);
7618 /* Finish creating the initializer. */
7620 CONSTRUCTOR_APPEND_ELT(v
, pfn_field
, pfn
);
7621 CONSTRUCTOR_APPEND_ELT(v
, delta_field
, delta
);
7622 u
= build_constructor (type
, v
);
7623 TREE_CONSTANT (u
) = TREE_CONSTANT (pfn
) & TREE_CONSTANT (delta
);
7624 TREE_STATIC (u
) = (TREE_CONSTANT (u
)
7625 && (initializer_constant_valid_p (pfn
, TREE_TYPE (pfn
))
7627 && (initializer_constant_valid_p (delta
, TREE_TYPE (delta
))
7632 /* Build a constructor for a pointer to member function. It can be
7633 used to initialize global variables, local variable, or used
7634 as a value in expressions. TYPE is the POINTER to METHOD_TYPE we
7637 If FORCE is nonzero, then force this conversion, even if
7638 we would rather not do it. Usually set when using an explicit
7639 cast. A C-style cast is being processed iff C_CAST_P is true.
7641 Return error_mark_node, if something goes wrong. */
7644 build_ptrmemfunc (tree type
, tree pfn
, int force
, bool c_cast_p
,
7645 tsubst_flags_t complain
)
7651 if (error_operand_p (pfn
))
7652 return error_mark_node
;
7654 pfn_type
= TREE_TYPE (pfn
);
7655 to_type
= build_ptrmemfunc_type (type
);
7657 /* Handle multiple conversions of pointer to member functions. */
7658 if (TYPE_PTRMEMFUNC_P (pfn_type
))
7660 tree delta
= NULL_TREE
;
7661 tree npfn
= NULL_TREE
;
7665 && !can_convert_arg (to_type
, TREE_TYPE (pfn
), pfn
,
7666 LOOKUP_NORMAL
, complain
))
7668 if (complain
& tf_error
)
7669 error ("invalid conversion to type %qT from type %qT",
7672 return error_mark_node
;
7675 n
= get_delta_difference (TYPE_PTRMEMFUNC_OBJECT_TYPE (pfn_type
),
7676 TYPE_PTRMEMFUNC_OBJECT_TYPE (to_type
),
7678 c_cast_p
, complain
);
7679 if (n
== error_mark_node
)
7680 return error_mark_node
;
7682 /* We don't have to do any conversion to convert a
7683 pointer-to-member to its own type. But, we don't want to
7684 just return a PTRMEM_CST if there's an explicit cast; that
7685 cast should make the expression an invalid template argument. */
7686 if (TREE_CODE (pfn
) != PTRMEM_CST
)
7688 if (same_type_p (to_type
, pfn_type
))
7690 else if (integer_zerop (n
))
7691 return build_reinterpret_cast (to_type
, pfn
,
7695 if (TREE_SIDE_EFFECTS (pfn
))
7696 pfn
= save_expr (pfn
);
7698 /* Obtain the function pointer and the current DELTA. */
7699 if (TREE_CODE (pfn
) == PTRMEM_CST
)
7700 expand_ptrmemfunc_cst (pfn
, &delta
, &npfn
);
7703 npfn
= build_ptrmemfunc_access_expr (pfn
, pfn_identifier
);
7704 delta
= build_ptrmemfunc_access_expr (pfn
, delta_identifier
);
7707 /* Just adjust the DELTA field. */
7708 gcc_assert (same_type_ignoring_top_level_qualifiers_p
7709 (TREE_TYPE (delta
), ptrdiff_type_node
));
7710 if (TARGET_PTRMEMFUNC_VBIT_LOCATION
== ptrmemfunc_vbit_in_delta
)
7711 n
= cp_build_binary_op (input_location
,
7712 LSHIFT_EXPR
, n
, integer_one_node
,
7714 delta
= cp_build_binary_op (input_location
,
7715 PLUS_EXPR
, delta
, n
, complain
);
7716 return build_ptrmemfunc1 (to_type
, delta
, npfn
);
7719 /* Handle null pointer to member function conversions. */
7720 if (null_ptr_cst_p (pfn
))
7722 pfn
= build_c_cast (input_location
, type
, pfn
);
7723 return build_ptrmemfunc1 (to_type
,
7728 if (type_unknown_p (pfn
))
7729 return instantiate_type (type
, pfn
, complain
);
7731 fn
= TREE_OPERAND (pfn
, 0);
7732 gcc_assert (TREE_CODE (fn
) == FUNCTION_DECL
7733 /* In a template, we will have preserved the
7735 || (processing_template_decl
&& TREE_CODE (fn
) == OFFSET_REF
));
7736 return make_ptrmem_cst (to_type
, fn
);
7739 /* Return the DELTA, IDX, PFN, and DELTA2 values for the PTRMEM_CST
7742 ??? There is no consistency as to the types returned for the above
7743 values. Some code acts as if it were a sizetype and some as if it were
7744 integer_type_node. */
7747 expand_ptrmemfunc_cst (tree cst
, tree
*delta
, tree
*pfn
)
7749 tree type
= TREE_TYPE (cst
);
7750 tree fn
= PTRMEM_CST_MEMBER (cst
);
7751 tree ptr_class
, fn_class
;
7753 gcc_assert (TREE_CODE (fn
) == FUNCTION_DECL
);
7755 /* The class that the function belongs to. */
7756 fn_class
= DECL_CONTEXT (fn
);
7758 /* The class that we're creating a pointer to member of. */
7759 ptr_class
= TYPE_PTRMEMFUNC_OBJECT_TYPE (type
);
7761 /* First, calculate the adjustment to the function's class. */
7762 *delta
= get_delta_difference (fn_class
, ptr_class
, /*force=*/0,
7763 /*c_cast_p=*/0, tf_warning_or_error
);
7765 if (!DECL_VIRTUAL_P (fn
))
7766 *pfn
= convert (TYPE_PTRMEMFUNC_FN_TYPE (type
),
7767 build_addr_func (fn
, tf_warning_or_error
));
7770 /* If we're dealing with a virtual function, we have to adjust 'this'
7771 again, to point to the base which provides the vtable entry for
7772 fn; the call will do the opposite adjustment. */
7773 tree orig_class
= DECL_CONTEXT (fn
);
7774 tree binfo
= binfo_or_else (orig_class
, fn_class
);
7775 *delta
= build2 (PLUS_EXPR
, TREE_TYPE (*delta
),
7776 *delta
, BINFO_OFFSET (binfo
));
7777 *delta
= fold_if_not_in_template (*delta
);
7779 /* We set PFN to the vtable offset at which the function can be
7780 found, plus one (unless ptrmemfunc_vbit_in_delta, in which
7781 case delta is shifted left, and then incremented). */
7782 *pfn
= DECL_VINDEX (fn
);
7783 *pfn
= build2 (MULT_EXPR
, integer_type_node
, *pfn
,
7784 TYPE_SIZE_UNIT (vtable_entry_type
));
7785 *pfn
= fold_if_not_in_template (*pfn
);
7787 switch (TARGET_PTRMEMFUNC_VBIT_LOCATION
)
7789 case ptrmemfunc_vbit_in_pfn
:
7790 *pfn
= build2 (PLUS_EXPR
, integer_type_node
, *pfn
,
7792 *pfn
= fold_if_not_in_template (*pfn
);
7795 case ptrmemfunc_vbit_in_delta
:
7796 *delta
= build2 (LSHIFT_EXPR
, TREE_TYPE (*delta
),
7797 *delta
, integer_one_node
);
7798 *delta
= fold_if_not_in_template (*delta
);
7799 *delta
= build2 (PLUS_EXPR
, TREE_TYPE (*delta
),
7800 *delta
, integer_one_node
);
7801 *delta
= fold_if_not_in_template (*delta
);
7808 *pfn
= build_nop (TYPE_PTRMEMFUNC_FN_TYPE (type
), *pfn
);
7809 *pfn
= fold_if_not_in_template (*pfn
);
7813 /* Return an expression for PFN from the pointer-to-member function
7817 pfn_from_ptrmemfunc (tree t
)
7819 if (TREE_CODE (t
) == PTRMEM_CST
)
7824 expand_ptrmemfunc_cst (t
, &delta
, &pfn
);
7829 return build_ptrmemfunc_access_expr (t
, pfn_identifier
);
7832 /* Return an expression for DELTA from the pointer-to-member function
7836 delta_from_ptrmemfunc (tree t
)
7838 if (TREE_CODE (t
) == PTRMEM_CST
)
7843 expand_ptrmemfunc_cst (t
, &delta
, &pfn
);
7848 return build_ptrmemfunc_access_expr (t
, delta_identifier
);
7851 /* Convert value RHS to type TYPE as preparation for an assignment to
7852 an lvalue of type TYPE. ERRTYPE indicates what kind of error the
7853 implicit conversion is. If FNDECL is non-NULL, we are doing the
7854 conversion in order to pass the PARMNUMth argument of FNDECL.
7855 If FNDECL is NULL, we are doing the conversion in function pointer
7856 argument passing, conversion in initialization, etc. */
7859 convert_for_assignment (tree type
, tree rhs
,
7860 impl_conv_rhs errtype
, tree fndecl
, int parmnum
,
7861 tsubst_flags_t complain
, int flags
)
7864 enum tree_code coder
;
7866 /* Strip NON_LVALUE_EXPRs since we aren't using as an lvalue. */
7867 if (TREE_CODE (rhs
) == NON_LVALUE_EXPR
)
7868 rhs
= TREE_OPERAND (rhs
, 0);
7870 rhstype
= TREE_TYPE (rhs
);
7871 coder
= TREE_CODE (rhstype
);
7873 if (TREE_CODE (type
) == VECTOR_TYPE
&& coder
== VECTOR_TYPE
7874 && vector_types_convertible_p (type
, rhstype
, true))
7876 rhs
= mark_rvalue_use (rhs
);
7877 return convert (type
, rhs
);
7880 if (rhs
== error_mark_node
|| rhstype
== error_mark_node
)
7881 return error_mark_node
;
7882 if (TREE_CODE (rhs
) == TREE_LIST
&& TREE_VALUE (rhs
) == error_mark_node
)
7883 return error_mark_node
;
7885 /* The RHS of an assignment cannot have void type. */
7886 if (coder
== VOID_TYPE
)
7888 if (complain
& tf_error
)
7889 error ("void value not ignored as it ought to be");
7890 return error_mark_node
;
7893 if (c_dialect_objc ())
7897 tree rname
= fndecl
;
7908 selector
= objc_message_selector ();
7910 if (selector
&& parmno
> 1)
7917 if (objc_compare_types (type
, rhstype
, parmno
, rname
))
7919 rhs
= mark_rvalue_use (rhs
);
7920 return convert (type
, rhs
);
7926 The expression is implicitly converted (clause _conv_) to the
7927 cv-unqualified type of the left operand.
7929 We allow bad conversions here because by the time we get to this point
7930 we are committed to doing the conversion. If we end up doing a bad
7931 conversion, convert_like will complain. */
7932 if (!can_convert_arg_bad (type
, rhstype
, rhs
, flags
, complain
))
7934 /* When -Wno-pmf-conversions is use, we just silently allow
7935 conversions from pointers-to-members to plain pointers. If
7936 the conversion doesn't work, cp_convert will complain. */
7938 && TYPE_PTR_P (type
)
7939 && TYPE_PTRMEMFUNC_P (rhstype
))
7940 rhs
= cp_convert (strip_top_quals (type
), rhs
, complain
);
7943 if (complain
& tf_error
)
7945 /* If the right-hand side has unknown type, then it is an
7946 overloaded function. Call instantiate_type to get error
7948 if (rhstype
== unknown_type_node
)
7949 instantiate_type (type
, rhs
, tf_warning_or_error
);
7951 error ("cannot convert %qT to %qT for argument %qP to %qD",
7952 rhstype
, type
, parmnum
, fndecl
);
7956 case ICR_DEFAULT_ARGUMENT
:
7957 error ("cannot convert %qT to %qT in default argument",
7961 error ("cannot convert %qT to %qT in argument passing",
7964 case ICR_CONVERTING
:
7965 error ("cannot convert %qT to %qT",
7969 error ("cannot convert %qT to %qT in initialization",
7973 error ("cannot convert %qT to %qT in return",
7977 error ("cannot convert %qT to %qT in assignment",
7984 return error_mark_node
;
7987 if (warn_suggest_attribute_format
)
7989 const enum tree_code codel
= TREE_CODE (type
);
7990 if ((codel
== POINTER_TYPE
|| codel
== REFERENCE_TYPE
)
7992 && check_missing_format_attribute (type
, rhstype
)
7993 && (complain
& tf_warning
))
7997 case ICR_DEFAULT_ARGUMENT
:
7999 warning (OPT_Wsuggest_attribute_format
,
8000 "parameter %qP of %qD might be a candidate "
8001 "for a format attribute", parmnum
, fndecl
);
8003 warning (OPT_Wsuggest_attribute_format
,
8004 "parameter might be a candidate "
8005 "for a format attribute");
8007 case ICR_CONVERTING
:
8008 warning (OPT_Wsuggest_attribute_format
,
8009 "target of conversion might be a candidate "
8010 "for a format attribute");
8013 warning (OPT_Wsuggest_attribute_format
,
8014 "target of initialization might be a candidate "
8015 "for a format attribute");
8018 warning (OPT_Wsuggest_attribute_format
,
8019 "return type might be a candidate "
8020 "for a format attribute");
8023 warning (OPT_Wsuggest_attribute_format
,
8024 "left-hand side of assignment might be a candidate "
8025 "for a format attribute");
8032 /* If -Wparentheses, warn about a = b = c when a has type bool and b
8034 if (warn_parentheses
8035 && TREE_CODE (type
) == BOOLEAN_TYPE
8036 && TREE_CODE (rhs
) == MODIFY_EXPR
8037 && !TREE_NO_WARNING (rhs
)
8038 && TREE_CODE (TREE_TYPE (rhs
)) != BOOLEAN_TYPE
8039 && (complain
& tf_warning
))
8041 location_t loc
= EXPR_LOC_OR_HERE (rhs
);
8043 warning_at (loc
, OPT_Wparentheses
,
8044 "suggest parentheses around assignment used as truth value");
8045 TREE_NO_WARNING (rhs
) = 1;
8048 return perform_implicit_conversion_flags (strip_top_quals (type
), rhs
,
8052 /* Convert RHS to be of type TYPE.
8053 If EXP is nonzero, it is the target of the initialization.
8054 ERRTYPE indicates what kind of error the implicit conversion is.
8056 Two major differences between the behavior of
8057 `convert_for_assignment' and `convert_for_initialization'
8058 are that references are bashed in the former, while
8059 copied in the latter, and aggregates are assigned in
8060 the former (operator=) while initialized in the
8063 If using constructor make sure no conversion operator exists, if one does
8064 exist, an ambiguity exists. */
8067 convert_for_initialization (tree exp
, tree type
, tree rhs
, int flags
,
8068 impl_conv_rhs errtype
, tree fndecl
, int parmnum
,
8069 tsubst_flags_t complain
)
8071 enum tree_code codel
= TREE_CODE (type
);
8073 enum tree_code coder
;
8075 /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
8076 Strip such NOP_EXPRs, since RHS is used in non-lvalue context. */
8077 if (TREE_CODE (rhs
) == NOP_EXPR
8078 && TREE_TYPE (rhs
) == TREE_TYPE (TREE_OPERAND (rhs
, 0))
8079 && codel
!= REFERENCE_TYPE
)
8080 rhs
= TREE_OPERAND (rhs
, 0);
8082 if (type
== error_mark_node
8083 || rhs
== error_mark_node
8084 || (TREE_CODE (rhs
) == TREE_LIST
&& TREE_VALUE (rhs
) == error_mark_node
))
8085 return error_mark_node
;
8087 if ((TREE_CODE (TREE_TYPE (rhs
)) == ARRAY_TYPE
8088 && TREE_CODE (type
) != ARRAY_TYPE
8089 && (TREE_CODE (type
) != REFERENCE_TYPE
8090 || TREE_CODE (TREE_TYPE (type
)) != ARRAY_TYPE
))
8091 || (TREE_CODE (TREE_TYPE (rhs
)) == FUNCTION_TYPE
8092 && !TYPE_REFFN_P (type
))
8093 || TREE_CODE (TREE_TYPE (rhs
)) == METHOD_TYPE
)
8094 rhs
= decay_conversion (rhs
, complain
);
8096 rhstype
= TREE_TYPE (rhs
);
8097 coder
= TREE_CODE (rhstype
);
8099 if (coder
== ERROR_MARK
)
8100 return error_mark_node
;
8102 /* We accept references to incomplete types, so we can
8103 return here before checking if RHS is of complete type. */
8105 if (codel
== REFERENCE_TYPE
)
8107 /* This should eventually happen in convert_arguments. */
8108 int savew
= 0, savee
= 0;
8111 savew
= warningcount
+ werrorcount
, savee
= errorcount
;
8112 rhs
= initialize_reference (type
, rhs
, flags
, complain
);
8115 && (warningcount
+ werrorcount
> savew
|| errorcount
> savee
))
8116 inform (input_location
,
8117 "in passing argument %P of %q+D", parmnum
, fndecl
);
8123 exp
= require_complete_type_sfinae (exp
, complain
);
8124 if (exp
== error_mark_node
)
8125 return error_mark_node
;
8127 rhstype
= non_reference (rhstype
);
8129 type
= complete_type (type
);
8131 if (DIRECT_INIT_EXPR_P (type
, rhs
))
8132 /* Don't try to do copy-initialization if we already have
8133 direct-initialization. */
8136 if (MAYBE_CLASS_TYPE_P (type
))
8137 return perform_implicit_conversion_flags (type
, rhs
, complain
, flags
);
8139 return convert_for_assignment (type
, rhs
, errtype
, fndecl
, parmnum
,
8143 /* If RETVAL is the address of, or a reference to, a local variable or
8144 temporary give an appropriate warning. */
8147 maybe_warn_about_returning_address_of_local (tree retval
)
8149 tree valtype
= TREE_TYPE (DECL_RESULT (current_function_decl
));
8150 tree whats_returned
= retval
;
8154 if (TREE_CODE (whats_returned
) == COMPOUND_EXPR
)
8155 whats_returned
= TREE_OPERAND (whats_returned
, 1);
8156 else if (CONVERT_EXPR_P (whats_returned
)
8157 || TREE_CODE (whats_returned
) == NON_LVALUE_EXPR
)
8158 whats_returned
= TREE_OPERAND (whats_returned
, 0);
8163 if (TREE_CODE (whats_returned
) != ADDR_EXPR
)
8165 whats_returned
= TREE_OPERAND (whats_returned
, 0);
8167 if (TREE_CODE (valtype
) == REFERENCE_TYPE
)
8169 if (TREE_CODE (whats_returned
) == AGGR_INIT_EXPR
8170 || TREE_CODE (whats_returned
) == TARGET_EXPR
)
8172 warning (OPT_Wreturn_local_addr
, "returning reference to temporary");
8175 if (VAR_P (whats_returned
)
8176 && DECL_NAME (whats_returned
)
8177 && TEMP_NAME_P (DECL_NAME (whats_returned
)))
8179 warning (OPT_Wreturn_local_addr
, "reference to non-lvalue returned");
8184 while (TREE_CODE (whats_returned
) == COMPONENT_REF
8185 || TREE_CODE (whats_returned
) == ARRAY_REF
)
8186 whats_returned
= TREE_OPERAND (whats_returned
, 0);
8188 if (DECL_P (whats_returned
)
8189 && DECL_NAME (whats_returned
)
8190 && DECL_FUNCTION_SCOPE_P (whats_returned
)
8191 && !is_capture_proxy (whats_returned
)
8192 && !(TREE_STATIC (whats_returned
)
8193 || TREE_PUBLIC (whats_returned
)))
8195 if (TREE_CODE (valtype
) == REFERENCE_TYPE
)
8196 warning (OPT_Wreturn_local_addr
, "reference to local variable %q+D returned",
8199 warning (OPT_Wreturn_local_addr
, "address of local variable %q+D returned",
8205 /* Check that returning RETVAL from the current function is valid.
8206 Return an expression explicitly showing all conversions required to
8207 change RETVAL into the function return type, and to assign it to
8208 the DECL_RESULT for the function. Set *NO_WARNING to true if
8209 code reaches end of non-void function warning shouldn't be issued
8210 on this RETURN_EXPR. */
8213 check_return_expr (tree retval
, bool *no_warning
)
8216 /* The type actually returned by the function. */
8218 /* The type the function is declared to return, or void if
8219 the declared type is incomplete. */
8221 int fn_returns_value_p
;
8222 bool named_return_value_okay_p
;
8224 *no_warning
= false;
8226 /* A `volatile' function is one that isn't supposed to return, ever.
8227 (This is a G++ extension, used to get better code for functions
8228 that call the `volatile' function.) */
8229 if (TREE_THIS_VOLATILE (current_function_decl
))
8230 warning (0, "function declared %<noreturn%> has a %<return%> statement");
8232 /* Check for various simple errors. */
8233 if (DECL_DESTRUCTOR_P (current_function_decl
))
8236 error ("returning a value from a destructor");
8239 else if (DECL_CONSTRUCTOR_P (current_function_decl
))
8241 if (in_function_try_handler
)
8242 /* If a return statement appears in a handler of the
8243 function-try-block of a constructor, the program is ill-formed. */
8244 error ("cannot return from a handler of a function-try-block of a constructor");
8246 /* You can't return a value from a constructor. */
8247 error ("returning a value from a constructor");
8251 if (processing_template_decl
)
8253 current_function_returns_value
= 1;
8254 if (check_for_bare_parameter_packs (retval
))
8255 retval
= error_mark_node
;
8259 functype
= TREE_TYPE (TREE_TYPE (current_function_decl
));
8261 /* Deduce auto return type from a return statement. */
8262 if (current_function_auto_return_pattern
)
8267 if (!retval
&& !is_auto (current_function_auto_return_pattern
))
8269 /* Give a helpful error message. */
8270 error ("return-statement with no value, in function returning %qT",
8271 current_function_auto_return_pattern
);
8272 inform (input_location
, "only plain %<auto%> return type can be "
8273 "deduced to %<void%>");
8274 type
= error_mark_node
;
8276 else if (retval
&& BRACE_ENCLOSED_INITIALIZER_P (retval
))
8278 error ("returning initializer list");
8279 type
= error_mark_node
;
8284 retval
= void_zero_node
;
8285 auto_node
= type_uses_auto (current_function_auto_return_pattern
);
8286 type
= do_auto_deduction (current_function_auto_return_pattern
,
8290 if (type
== error_mark_node
)
8292 else if (functype
== current_function_auto_return_pattern
)
8293 apply_deduced_return_type (current_function_decl
, type
);
8295 /* A mismatch should have been diagnosed in do_auto_deduction. */
8296 gcc_assert (same_type_p (type
, functype
));
8300 /* When no explicit return-value is given in a function with a named
8301 return value, the named return value is used. */
8302 result
= DECL_RESULT (current_function_decl
);
8303 valtype
= TREE_TYPE (result
);
8304 gcc_assert (valtype
!= NULL_TREE
);
8305 fn_returns_value_p
= !VOID_TYPE_P (valtype
);
8306 if (!retval
&& DECL_NAME (result
) && fn_returns_value_p
)
8309 /* Check for a return statement with no return value in a function
8310 that's supposed to return a value. */
8311 if (!retval
&& fn_returns_value_p
)
8313 if (functype
!= error_mark_node
)
8314 permerror (input_location
, "return-statement with no value, in "
8315 "function returning %qT", valtype
);
8316 /* Remember that this function did return. */
8317 current_function_returns_value
= 1;
8318 /* And signal caller that TREE_NO_WARNING should be set on the
8319 RETURN_EXPR to avoid control reaches end of non-void function
8320 warnings in tree-cfg.c. */
8323 /* Check for a return statement with a value in a function that
8324 isn't supposed to return a value. */
8325 else if (retval
&& !fn_returns_value_p
)
8327 if (VOID_TYPE_P (TREE_TYPE (retval
)))
8328 /* You can return a `void' value from a function of `void'
8329 type. In that case, we have to evaluate the expression for
8330 its side-effects. */
8331 finish_expr_stmt (retval
);
8333 permerror (input_location
, "return-statement with a value, in function "
8334 "returning 'void'");
8335 current_function_returns_null
= 1;
8337 /* There's really no value to return, after all. */
8341 /* Remember that this function can sometimes return without a
8343 current_function_returns_null
= 1;
8345 /* Remember that this function did return a value. */
8346 current_function_returns_value
= 1;
8348 /* Check for erroneous operands -- but after giving ourselves a
8349 chance to provide an error about returning a value from a void
8351 if (error_operand_p (retval
))
8353 current_function_return_value
= error_mark_node
;
8354 return error_mark_node
;
8357 /* Only operator new(...) throw(), can return NULL [expr.new/13]. */
8358 if ((DECL_OVERLOADED_OPERATOR_P (current_function_decl
) == NEW_EXPR
8359 || DECL_OVERLOADED_OPERATOR_P (current_function_decl
) == VEC_NEW_EXPR
)
8360 && !TYPE_NOTHROW_P (TREE_TYPE (current_function_decl
))
8362 && retval
&& null_ptr_cst_p (retval
))
8363 warning (0, "%<operator new%> must not return NULL unless it is "
8364 "declared %<throw()%> (or -fcheck-new is in effect)");
8366 /* Effective C++ rule 15. See also start_function. */
8368 && DECL_NAME (current_function_decl
) == ansi_assopname(NOP_EXPR
))
8372 /* The function return type must be a reference to the current
8374 if (TREE_CODE (valtype
) == REFERENCE_TYPE
8375 && same_type_ignoring_top_level_qualifiers_p
8376 (TREE_TYPE (valtype
), TREE_TYPE (current_class_ref
)))
8378 /* Returning '*this' is obviously OK. */
8379 if (retval
== current_class_ref
)
8381 /* If we are calling a function whose return type is the same of
8382 the current class reference, it is ok. */
8383 else if (INDIRECT_REF_P (retval
)
8384 && TREE_CODE (TREE_OPERAND (retval
, 0)) == CALL_EXPR
)
8389 warning (OPT_Weffc__
, "%<operator=%> should return a reference to %<*this%>");
8392 /* The fabled Named Return Value optimization, as per [class.copy]/15:
8394 [...] For a function with a class return type, if the expression
8395 in the return statement is the name of a local object, and the cv-
8396 unqualified type of the local object is the same as the function
8397 return type, an implementation is permitted to omit creating the tem-
8398 porary object to hold the function return value [...]
8400 So, if this is a value-returning function that always returns the same
8401 local variable, remember it.
8403 It might be nice to be more flexible, and choose the first suitable
8404 variable even if the function sometimes returns something else, but
8405 then we run the risk of clobbering the variable we chose if the other
8406 returned expression uses the chosen variable somehow. And people expect
8407 this restriction, anyway. (jason 2000-11-19)
8409 See finish_function and finalize_nrv for the rest of this optimization. */
8411 named_return_value_okay_p
=
8412 (retval
!= NULL_TREE
8413 /* Must be a local, automatic variable. */
8415 && DECL_CONTEXT (retval
) == current_function_decl
8416 && ! TREE_STATIC (retval
)
8417 /* And not a lambda or anonymous union proxy. */
8418 && !DECL_HAS_VALUE_EXPR_P (retval
)
8419 && (DECL_ALIGN (retval
) <= DECL_ALIGN (result
))
8420 /* The cv-unqualified type of the returned value must be the
8421 same as the cv-unqualified return type of the
8423 && same_type_p ((TYPE_MAIN_VARIANT (TREE_TYPE (retval
))),
8424 (TYPE_MAIN_VARIANT (functype
)))
8425 /* And the returned value must be non-volatile. */
8426 && ! TYPE_VOLATILE (TREE_TYPE (retval
)));
8428 if (fn_returns_value_p
&& flag_elide_constructors
)
8430 if (named_return_value_okay_p
8431 && (current_function_return_value
== NULL_TREE
8432 || current_function_return_value
== retval
))
8433 current_function_return_value
= retval
;
8435 current_function_return_value
= error_mark_node
;
8438 /* We don't need to do any conversions when there's nothing being
8443 /* Do any required conversions. */
8444 if (retval
== result
|| DECL_CONSTRUCTOR_P (current_function_decl
))
8445 /* No conversions are required. */
8449 int flags
= LOOKUP_NORMAL
| LOOKUP_ONLYCONVERTING
;
8451 /* The functype's return type will have been set to void, if it
8452 was an incomplete type. Just treat this as 'return;' */
8453 if (VOID_TYPE_P (functype
))
8454 return error_mark_node
;
8456 /* Under C++0x [12.8/16 class.copy], a returned lvalue is sometimes
8457 treated as an rvalue for the purposes of overload resolution to
8458 favor move constructors over copy constructors.
8460 Note that these conditions are similar to, but not as strict as,
8461 the conditions for the named return value optimization. */
8462 if ((cxx_dialect
!= cxx98
)
8463 && ((VAR_P (retval
) && !DECL_HAS_VALUE_EXPR_P (retval
))
8464 || TREE_CODE (retval
) == PARM_DECL
)
8465 && DECL_CONTEXT (retval
) == current_function_decl
8466 && !TREE_STATIC (retval
)
8467 && same_type_p ((TYPE_MAIN_VARIANT (TREE_TYPE (retval
))),
8468 (TYPE_MAIN_VARIANT (functype
)))
8469 /* This is only interesting for class type. */
8470 && CLASS_TYPE_P (functype
))
8471 flags
= flags
| LOOKUP_PREFER_RVALUE
;
8473 /* First convert the value to the function's return type, then
8474 to the type of return value's location to handle the
8475 case that functype is smaller than the valtype. */
8476 retval
= convert_for_initialization
8477 (NULL_TREE
, functype
, retval
, flags
, ICR_RETURN
, NULL_TREE
, 0,
8478 tf_warning_or_error
);
8479 retval
= convert (valtype
, retval
);
8481 /* If the conversion failed, treat this just like `return;'. */
8482 if (retval
== error_mark_node
)
8484 /* We can't initialize a register from a AGGR_INIT_EXPR. */
8485 else if (! cfun
->returns_struct
8486 && TREE_CODE (retval
) == TARGET_EXPR
8487 && TREE_CODE (TREE_OPERAND (retval
, 1)) == AGGR_INIT_EXPR
)
8488 retval
= build2 (COMPOUND_EXPR
, TREE_TYPE (retval
), retval
,
8489 TREE_OPERAND (retval
, 0));
8491 maybe_warn_about_returning_address_of_local (retval
);
8494 /* Actually copy the value returned into the appropriate location. */
8495 if (retval
&& retval
!= result
)
8496 retval
= build2 (INIT_EXPR
, TREE_TYPE (result
), result
, retval
);
8502 /* Returns nonzero if the pointer-type FROM can be converted to the
8503 pointer-type TO via a qualification conversion. If CONSTP is -1,
8504 then we return nonzero if the pointers are similar, and the
8505 cv-qualification signature of FROM is a proper subset of that of TO.
8507 If CONSTP is positive, then all outer pointers have been
8511 comp_ptr_ttypes_real (tree to
, tree from
, int constp
)
8513 bool to_more_cv_qualified
= false;
8514 bool is_opaque_pointer
= false;
8516 for (; ; to
= TREE_TYPE (to
), from
= TREE_TYPE (from
))
8518 if (TREE_CODE (to
) != TREE_CODE (from
))
8521 if (TREE_CODE (from
) == OFFSET_TYPE
8522 && !same_type_p (TYPE_OFFSET_BASETYPE (from
),
8523 TYPE_OFFSET_BASETYPE (to
)))
8526 /* Const and volatile mean something different for function types,
8527 so the usual checks are not appropriate. */
8528 if (TREE_CODE (to
) != FUNCTION_TYPE
&& TREE_CODE (to
) != METHOD_TYPE
)
8530 if (!at_least_as_qualified_p (to
, from
))
8533 if (!at_least_as_qualified_p (from
, to
))
8537 to_more_cv_qualified
= true;
8541 constp
&= TYPE_READONLY (to
);
8544 if (TREE_CODE (to
) == VECTOR_TYPE
)
8545 is_opaque_pointer
= vector_targets_convertible_p (to
, from
);
8547 if (!TYPE_PTR_P (to
) && !TYPE_PTRDATAMEM_P (to
))
8548 return ((constp
>= 0 || to_more_cv_qualified
)
8549 && (is_opaque_pointer
8550 || same_type_ignoring_top_level_qualifiers_p (to
, from
)));
8554 /* When comparing, say, char ** to char const **, this function takes
8555 the 'char *' and 'char const *'. Do not pass non-pointer/reference
8556 types to this function. */
8559 comp_ptr_ttypes (tree to
, tree from
)
8561 return comp_ptr_ttypes_real (to
, from
, 1);
8564 /* Returns true iff FNTYPE is a non-class type that involves
8565 error_mark_node. We can get FUNCTION_TYPE with buried error_mark_node
8566 if a parameter type is ill-formed. */
8569 error_type_p (const_tree type
)
8573 switch (TREE_CODE (type
))
8579 case REFERENCE_TYPE
:
8581 return error_type_p (TREE_TYPE (type
));
8585 if (error_type_p (TREE_TYPE (type
)))
8587 for (t
= TYPE_ARG_TYPES (type
); t
; t
= TREE_CHAIN (t
))
8588 if (error_type_p (TREE_VALUE (t
)))
8593 if (TYPE_PTRMEMFUNC_P (type
))
8594 return error_type_p (TYPE_PTRMEMFUNC_FN_TYPE (type
));
8602 /* Returns true if to and from are (possibly multi-level) pointers to the same
8603 type or inheritance-related types, regardless of cv-quals. */
8606 ptr_reasonably_similar (const_tree to
, const_tree from
)
8608 for (; ; to
= TREE_TYPE (to
), from
= TREE_TYPE (from
))
8610 /* Any target type is similar enough to void. */
8611 if (VOID_TYPE_P (to
))
8612 return !error_type_p (from
);
8613 if (VOID_TYPE_P (from
))
8614 return !error_type_p (to
);
8616 if (TREE_CODE (to
) != TREE_CODE (from
))
8619 if (TREE_CODE (from
) == OFFSET_TYPE
8620 && comptypes (TYPE_OFFSET_BASETYPE (to
),
8621 TYPE_OFFSET_BASETYPE (from
),
8622 COMPARE_BASE
| COMPARE_DERIVED
))
8625 if (TREE_CODE (to
) == VECTOR_TYPE
8626 && vector_types_convertible_p (to
, from
, false))
8629 if (TREE_CODE (to
) == INTEGER_TYPE
8630 && TYPE_PRECISION (to
) == TYPE_PRECISION (from
))
8633 if (TREE_CODE (to
) == FUNCTION_TYPE
)
8634 return !error_type_p (to
) && !error_type_p (from
);
8636 if (!TYPE_PTR_P (to
))
8638 /* When either type is incomplete avoid DERIVED_FROM_P,
8639 which may call complete_type (c++/57942). */
8640 bool b
= !COMPLETE_TYPE_P (to
) || !COMPLETE_TYPE_P (from
);
8642 (TYPE_MAIN_VARIANT (to
), TYPE_MAIN_VARIANT (from
),
8643 b
? COMPARE_STRICT
: COMPARE_BASE
| COMPARE_DERIVED
);
8648 /* Return true if TO and FROM (both of which are POINTER_TYPEs or
8649 pointer-to-member types) are the same, ignoring cv-qualification at
8653 comp_ptr_ttypes_const (tree to
, tree from
)
8655 bool is_opaque_pointer
= false;
8657 for (; ; to
= TREE_TYPE (to
), from
= TREE_TYPE (from
))
8659 if (TREE_CODE (to
) != TREE_CODE (from
))
8662 if (TREE_CODE (from
) == OFFSET_TYPE
8663 && same_type_p (TYPE_OFFSET_BASETYPE (from
),
8664 TYPE_OFFSET_BASETYPE (to
)))
8667 if (TREE_CODE (to
) == VECTOR_TYPE
)
8668 is_opaque_pointer
= vector_targets_convertible_p (to
, from
);
8670 if (!TYPE_PTR_P (to
))
8671 return (is_opaque_pointer
8672 || same_type_ignoring_top_level_qualifiers_p (to
, from
));
8676 /* Returns the type qualifiers for this type, including the qualifiers on the
8677 elements for an array type. */
8680 cp_type_quals (const_tree type
)
8683 /* This CONST_CAST is okay because strip_array_types returns its
8684 argument unmodified and we assign it to a const_tree. */
8685 type
= strip_array_types (CONST_CAST_TREE (type
));
8686 if (type
== error_mark_node
8687 /* Quals on a FUNCTION_TYPE are memfn quals. */
8688 || TREE_CODE (type
) == FUNCTION_TYPE
)
8689 return TYPE_UNQUALIFIED
;
8690 quals
= TYPE_QUALS (type
);
8691 /* METHOD and REFERENCE_TYPEs should never have quals. */
8692 gcc_assert ((TREE_CODE (type
) != METHOD_TYPE
8693 && TREE_CODE (type
) != REFERENCE_TYPE
)
8694 || ((quals
& (TYPE_QUAL_CONST
|TYPE_QUAL_VOLATILE
))
8695 == TYPE_UNQUALIFIED
));
8699 /* Returns the function-ref-qualifier for TYPE */
8702 type_memfn_rqual (const_tree type
)
8704 gcc_assert (TREE_CODE (type
) == FUNCTION_TYPE
8705 || TREE_CODE (type
) == METHOD_TYPE
);
8707 if (!FUNCTION_REF_QUALIFIED (type
))
8708 return REF_QUAL_NONE
;
8709 else if (FUNCTION_RVALUE_QUALIFIED (type
))
8710 return REF_QUAL_RVALUE
;
8712 return REF_QUAL_LVALUE
;
8715 /* Returns the function-cv-quals for TYPE, which must be a FUNCTION_TYPE or
8719 type_memfn_quals (const_tree type
)
8721 if (TREE_CODE (type
) == FUNCTION_TYPE
)
8722 return TYPE_QUALS (type
);
8723 else if (TREE_CODE (type
) == METHOD_TYPE
)
8724 return cp_type_quals (class_of_this_parm (type
));
8729 /* Returns the FUNCTION_TYPE TYPE with its function-cv-quals changed to
8730 MEMFN_QUALS and its ref-qualifier to RQUAL. */
8733 apply_memfn_quals (tree type
, cp_cv_quals memfn_quals
, cp_ref_qualifier rqual
)
8735 /* Could handle METHOD_TYPE here if necessary. */
8736 gcc_assert (TREE_CODE (type
) == FUNCTION_TYPE
);
8737 if (TYPE_QUALS (type
) == memfn_quals
8738 && type_memfn_rqual (type
) == rqual
)
8741 /* This should really have a different TYPE_MAIN_VARIANT, but that gets
8743 tree result
= build_qualified_type (type
, memfn_quals
);
8744 result
= build_exception_variant (result
, TYPE_RAISES_EXCEPTIONS (type
));
8745 return build_ref_qualified_type (result
, rqual
);
8748 /* Returns nonzero if TYPE is const or volatile. */
8751 cv_qualified_p (const_tree type
)
8753 int quals
= cp_type_quals (type
);
8754 return (quals
& (TYPE_QUAL_CONST
|TYPE_QUAL_VOLATILE
)) != 0;
8757 /* Returns nonzero if the TYPE contains a mutable member. */
8760 cp_has_mutable_p (const_tree type
)
8762 /* This CONST_CAST is okay because strip_array_types returns its
8763 argument unmodified and we assign it to a const_tree. */
8764 type
= strip_array_types (CONST_CAST_TREE(type
));
8766 return CLASS_TYPE_P (type
) && CLASSTYPE_HAS_MUTABLE (type
);
8769 /* Set TREE_READONLY and TREE_VOLATILE on DECL as indicated by the
8770 TYPE_QUALS. For a VAR_DECL, this may be an optimistic
8771 approximation. In particular, consider:
8774 struct S { int i; };
8775 const S s = { f(); }
8777 Here, we will make "s" as TREE_READONLY (because it is declared
8778 "const") -- only to reverse ourselves upon seeing that the
8779 initializer is non-constant. */
8782 cp_apply_type_quals_to_decl (int type_quals
, tree decl
)
8784 tree type
= TREE_TYPE (decl
);
8786 if (type
== error_mark_node
)
8789 if (TREE_CODE (decl
) == TYPE_DECL
)
8792 gcc_assert (!(TREE_CODE (type
) == FUNCTION_TYPE
8793 && type_quals
!= TYPE_UNQUALIFIED
));
8795 /* Avoid setting TREE_READONLY incorrectly. */
8796 /* We used to check TYPE_NEEDS_CONSTRUCTING here, but now a constexpr
8797 constructor can produce constant init, so rely on cp_finish_decl to
8798 clear TREE_READONLY if the variable has non-constant init. */
8800 /* If the type has (or might have) a mutable component, that component
8801 might be modified. */
8802 if (TYPE_HAS_MUTABLE_P (type
) || !COMPLETE_TYPE_P (type
))
8803 type_quals
&= ~TYPE_QUAL_CONST
;
8805 c_apply_type_quals_to_decl (type_quals
, decl
);
8808 /* Subroutine of casts_away_constness. Make T1 and T2 point at
8809 exemplar types such that casting T1 to T2 is casting away constness
8810 if and only if there is no implicit conversion from T1 to T2. */
8813 casts_away_constness_r (tree
*t1
, tree
*t2
, tsubst_flags_t complain
)
8818 /* [expr.const.cast]
8820 For multi-level pointer to members and multi-level mixed pointers
8821 and pointers to members (conv.qual), the "member" aspect of a
8822 pointer to member level is ignored when determining if a const
8823 cv-qualifier has been cast away. */
8824 /* [expr.const.cast]
8826 For two pointer types:
8828 X1 is T1cv1,1 * ... cv1,N * where T1 is not a pointer type
8829 X2 is T2cv2,1 * ... cv2,M * where T2 is not a pointer type
8832 casting from X1 to X2 casts away constness if, for a non-pointer
8833 type T there does not exist an implicit conversion (clause
8836 Tcv1,(N-K+1) * cv1,(N-K+2) * ... cv1,N *
8840 Tcv2,(M-K+1) * cv2,(M-K+2) * ... cv2,M *. */
8841 if ((!TYPE_PTR_P (*t1
) && !TYPE_PTRDATAMEM_P (*t1
))
8842 || (!TYPE_PTR_P (*t2
) && !TYPE_PTRDATAMEM_P (*t2
)))
8844 *t1
= cp_build_qualified_type (void_type_node
,
8845 cp_type_quals (*t1
));
8846 *t2
= cp_build_qualified_type (void_type_node
,
8847 cp_type_quals (*t2
));
8851 quals1
= cp_type_quals (*t1
);
8852 quals2
= cp_type_quals (*t2
);
8854 if (TYPE_PTRDATAMEM_P (*t1
))
8855 *t1
= TYPE_PTRMEM_POINTED_TO_TYPE (*t1
);
8857 *t1
= TREE_TYPE (*t1
);
8858 if (TYPE_PTRDATAMEM_P (*t2
))
8859 *t2
= TYPE_PTRMEM_POINTED_TO_TYPE (*t2
);
8861 *t2
= TREE_TYPE (*t2
);
8863 casts_away_constness_r (t1
, t2
, complain
);
8864 *t1
= build_pointer_type (*t1
);
8865 *t2
= build_pointer_type (*t2
);
8866 *t1
= cp_build_qualified_type (*t1
, quals1
);
8867 *t2
= cp_build_qualified_type (*t2
, quals2
);
8870 /* Returns nonzero if casting from TYPE1 to TYPE2 casts away
8873 ??? This function returns non-zero if casting away qualifiers not
8874 just const. We would like to return to the caller exactly which
8875 qualifiers are casted away to give more accurate diagnostics.
8879 casts_away_constness (tree t1
, tree t2
, tsubst_flags_t complain
)
8881 if (TREE_CODE (t2
) == REFERENCE_TYPE
)
8883 /* [expr.const.cast]
8885 Casting from an lvalue of type T1 to an lvalue of type T2
8886 using a reference cast casts away constness if a cast from an
8887 rvalue of type "pointer to T1" to the type "pointer to T2"
8888 casts away constness. */
8889 t1
= (TREE_CODE (t1
) == REFERENCE_TYPE
? TREE_TYPE (t1
) : t1
);
8890 return casts_away_constness (build_pointer_type (t1
),
8891 build_pointer_type (TREE_TYPE (t2
)),
8895 if (TYPE_PTRDATAMEM_P (t1
) && TYPE_PTRDATAMEM_P (t2
))
8896 /* [expr.const.cast]
8898 Casting from an rvalue of type "pointer to data member of X
8899 of type T1" to the type "pointer to data member of Y of type
8900 T2" casts away constness if a cast from an rvalue of type
8901 "pointer to T1" to the type "pointer to T2" casts away
8903 return casts_away_constness
8904 (build_pointer_type (TYPE_PTRMEM_POINTED_TO_TYPE (t1
)),
8905 build_pointer_type (TYPE_PTRMEM_POINTED_TO_TYPE (t2
)),
8908 /* Casting away constness is only something that makes sense for
8909 pointer or reference types. */
8910 if (!TYPE_PTR_P (t1
) || !TYPE_PTR_P (t2
))
8913 /* Top-level qualifiers don't matter. */
8914 t1
= TYPE_MAIN_VARIANT (t1
);
8915 t2
= TYPE_MAIN_VARIANT (t2
);
8916 casts_away_constness_r (&t1
, &t2
, complain
);
8917 if (!can_convert (t2
, t1
, complain
))
8923 /* If T is a REFERENCE_TYPE return the type to which T refers.
8924 Otherwise, return T itself. */
8927 non_reference (tree t
)
8929 if (t
&& TREE_CODE (t
) == REFERENCE_TYPE
)
8935 /* Return nonzero if REF is an lvalue valid for this language;
8936 otherwise, print an error message and return zero. USE says
8937 how the lvalue is being used and so selects the error message. */
8940 lvalue_or_else (tree ref
, enum lvalue_use use
, tsubst_flags_t complain
)
8942 cp_lvalue_kind kind
= lvalue_kind (ref
);
8944 if (kind
== clk_none
)
8946 if (complain
& tf_error
)
8947 lvalue_error (input_location
, use
);
8950 else if (kind
& (clk_rvalueref
|clk_class
))
8952 if (!(complain
& tf_error
))
8954 if (kind
& clk_class
)
8955 /* Make this a permerror because we used to accept it. */
8956 permerror (input_location
, "using temporary as lvalue");
8958 error ("using xvalue (rvalue reference) as lvalue");
8963 /* Return true if a user-defined literal operator is a raw operator. */
8966 check_raw_literal_operator (const_tree decl
)
8968 tree argtypes
= TYPE_ARG_TYPES (TREE_TYPE (decl
));
8971 bool maybe_raw_p
= false;
8973 /* Count the number and type of arguments and check for ellipsis. */
8974 for (argtype
= argtypes
, arity
= 0;
8975 argtype
&& argtype
!= void_list_node
;
8976 ++arity
, argtype
= TREE_CHAIN (argtype
))
8978 tree t
= TREE_VALUE (argtype
);
8980 if (same_type_p (t
, const_string_type_node
))
8984 return false; /* Found ellipsis. */
8986 if (!maybe_raw_p
|| arity
!= 1)
8993 /* Return true if a user-defined literal operator has one of the allowed
8997 check_literal_operator_args (const_tree decl
,
8998 bool *long_long_unsigned_p
, bool *long_double_p
)
9000 tree argtypes
= TYPE_ARG_TYPES (TREE_TYPE (decl
));
9002 *long_long_unsigned_p
= false;
9003 *long_double_p
= false;
9004 if (processing_template_decl
|| processing_specialization
)
9005 return argtypes
== void_list_node
;
9012 /* Count the number and type of arguments and check for ellipsis. */
9013 for (argtype
= argtypes
, arity
= 0;
9014 argtype
&& argtype
!= void_list_node
;
9015 argtype
= TREE_CHAIN (argtype
))
9017 tree t
= TREE_VALUE (argtype
);
9022 bool maybe_raw_p
= false;
9024 if (cp_type_quals (t
) != TYPE_QUAL_CONST
)
9026 t
= TYPE_MAIN_VARIANT (t
);
9027 if ((maybe_raw_p
= same_type_p (t
, char_type_node
))
9028 || same_type_p (t
, wchar_type_node
)
9029 || same_type_p (t
, char16_type_node
)
9030 || same_type_p (t
, char32_type_node
))
9032 argtype
= TREE_CHAIN (argtype
);
9035 t
= TREE_VALUE (argtype
);
9036 if (maybe_raw_p
&& argtype
== void_list_node
)
9038 else if (same_type_p (t
, size_type_node
))
9047 else if (same_type_p (t
, long_long_unsigned_type_node
))
9050 *long_long_unsigned_p
= true;
9052 else if (same_type_p (t
, long_double_type_node
))
9055 *long_double_p
= true;
9057 else if (same_type_p (t
, char_type_node
))
9059 else if (same_type_p (t
, wchar_type_node
))
9061 else if (same_type_p (t
, char16_type_node
))
9063 else if (same_type_p (t
, char32_type_node
))
9069 return false; /* Found ellipsis. */
9071 if (arity
!= max_arity
)