2012-11-28 Oleg Raikhman <oleg@adapteva.com>
[official-gcc.git] / gcc / cp / typeck.c
blob1cbab61716703478a24d59f4653c12897a6a6b08
1 /* Build expressions with type checking for C++ compiler.
2 Copyright (C) 1987, 1988, 1989, 1992, 1993, 1994, 1995, 1996, 1997, 1998,
3 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010,
4 2011, 2012
5 Free Software Foundation, Inc.
6 Hacked by Michael Tiemann (tiemann@cygnus.com)
8 This file is part of GCC.
10 GCC is free software; you can redistribute it and/or modify
11 it under the terms of the GNU General Public License as published by
12 the Free Software Foundation; either version 3, or (at your option)
13 any later version.
15 GCC is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU General Public License for more details.
20 You should have received a copy of the GNU General Public License
21 along with GCC; see the file COPYING3. If not see
22 <http://www.gnu.org/licenses/>. */
25 /* This file is part of the C++ front end.
26 It contains routines to build C++ expressions given their operands,
27 including computing the types of the result, C and C++ specific error
28 checks, and some optimization. */
30 #include "config.h"
31 #include "system.h"
32 #include "coretypes.h"
33 #include "tm.h"
34 #include "tree.h"
35 #include "cp-tree.h"
36 #include "flags.h"
37 #include "diagnostic.h"
38 #include "intl.h"
39 #include "target.h"
40 #include "convert.h"
41 #include "c-family/c-common.h"
42 #include "c-family/c-objc.h"
43 #include "params.h"
45 static tree pfn_from_ptrmemfunc (tree);
46 static tree delta_from_ptrmemfunc (tree);
47 static tree convert_for_assignment (tree, tree, impl_conv_rhs, tree, int,
48 tsubst_flags_t, int);
49 static tree cp_pointer_int_sum (enum tree_code, tree, tree);
50 static tree rationalize_conditional_expr (enum tree_code, tree,
51 tsubst_flags_t);
52 static int comp_ptr_ttypes_real (tree, tree, int);
53 static bool comp_except_types (tree, tree, bool);
54 static bool comp_array_types (const_tree, const_tree, bool);
55 static tree pointer_diff (tree, tree, tree, tsubst_flags_t);
56 static tree get_delta_difference (tree, tree, bool, bool, tsubst_flags_t);
57 static void casts_away_constness_r (tree *, tree *, tsubst_flags_t);
58 static bool casts_away_constness (tree, tree, tsubst_flags_t);
59 static void maybe_warn_about_returning_address_of_local (tree);
60 static tree lookup_destructor (tree, tree, tree);
61 static void warn_args_num (location_t, tree, bool);
62 static int convert_arguments (tree, vec<tree, va_gc> **, tree, int,
63 tsubst_flags_t);
65 /* Do `exp = require_complete_type (exp);' to make sure exp
66 does not have an incomplete type. (That includes void types.)
67 Returns error_mark_node if the VALUE does not have
68 complete type when this function returns. */
70 tree
71 require_complete_type_sfinae (tree value, tsubst_flags_t complain)
73 tree type;
75 if (processing_template_decl || value == error_mark_node)
76 return value;
78 if (TREE_CODE (value) == OVERLOAD)
79 type = unknown_type_node;
80 else
81 type = TREE_TYPE (value);
83 if (type == error_mark_node)
84 return error_mark_node;
86 /* First, detect a valid value with a complete type. */
87 if (COMPLETE_TYPE_P (type))
88 return value;
90 if (complete_type_or_maybe_complain (type, value, complain))
91 return value;
92 else
93 return error_mark_node;
96 tree
97 require_complete_type (tree value)
99 return require_complete_type_sfinae (value, tf_warning_or_error);
102 /* Try to complete TYPE, if it is incomplete. For example, if TYPE is
103 a template instantiation, do the instantiation. Returns TYPE,
104 whether or not it could be completed, unless something goes
105 horribly wrong, in which case the error_mark_node is returned. */
107 tree
108 complete_type (tree type)
110 if (type == NULL_TREE)
111 /* Rather than crash, we return something sure to cause an error
112 at some point. */
113 return error_mark_node;
115 if (type == error_mark_node || COMPLETE_TYPE_P (type))
117 else if (TREE_CODE (type) == ARRAY_TYPE && TYPE_DOMAIN (type))
119 tree t = complete_type (TREE_TYPE (type));
120 unsigned int needs_constructing, has_nontrivial_dtor;
121 if (COMPLETE_TYPE_P (t) && !dependent_type_p (type))
122 layout_type (type);
123 needs_constructing
124 = TYPE_NEEDS_CONSTRUCTING (TYPE_MAIN_VARIANT (t));
125 has_nontrivial_dtor
126 = TYPE_HAS_NONTRIVIAL_DESTRUCTOR (TYPE_MAIN_VARIANT (t));
127 for (t = TYPE_MAIN_VARIANT (type); t; t = TYPE_NEXT_VARIANT (t))
129 TYPE_NEEDS_CONSTRUCTING (t) = needs_constructing;
130 TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t) = has_nontrivial_dtor;
133 else if (CLASS_TYPE_P (type) && CLASSTYPE_TEMPLATE_INSTANTIATION (type))
134 instantiate_class_template (TYPE_MAIN_VARIANT (type));
136 return type;
139 /* Like complete_type, but issue an error if the TYPE cannot be completed.
140 VALUE is used for informative diagnostics.
141 Returns NULL_TREE if the type cannot be made complete. */
143 tree
144 complete_type_or_maybe_complain (tree type, tree value, tsubst_flags_t complain)
146 type = complete_type (type);
147 if (type == error_mark_node)
148 /* We already issued an error. */
149 return NULL_TREE;
150 else if (!COMPLETE_TYPE_P (type))
152 if (complain & tf_error)
153 cxx_incomplete_type_diagnostic (value, type, DK_ERROR);
154 return NULL_TREE;
156 else
157 return type;
160 tree
161 complete_type_or_else (tree type, tree value)
163 return complete_type_or_maybe_complain (type, value, tf_warning_or_error);
166 /* Return truthvalue of whether type of EXP is instantiated. */
169 type_unknown_p (const_tree exp)
171 return (TREE_CODE (exp) == TREE_LIST
172 || TREE_TYPE (exp) == unknown_type_node);
176 /* Return the common type of two parameter lists.
177 We assume that comptypes has already been done and returned 1;
178 if that isn't so, this may crash.
180 As an optimization, free the space we allocate if the parameter
181 lists are already common. */
183 static tree
184 commonparms (tree p1, tree p2)
186 tree oldargs = p1, newargs, n;
187 int i, len;
188 int any_change = 0;
190 len = list_length (p1);
191 newargs = tree_last (p1);
193 if (newargs == void_list_node)
194 i = 1;
195 else
197 i = 0;
198 newargs = 0;
201 for (; i < len; i++)
202 newargs = tree_cons (NULL_TREE, NULL_TREE, newargs);
204 n = newargs;
206 for (i = 0; p1;
207 p1 = TREE_CHAIN (p1), p2 = TREE_CHAIN (p2), n = TREE_CHAIN (n), i++)
209 if (TREE_PURPOSE (p1) && !TREE_PURPOSE (p2))
211 TREE_PURPOSE (n) = TREE_PURPOSE (p1);
212 any_change = 1;
214 else if (! TREE_PURPOSE (p1))
216 if (TREE_PURPOSE (p2))
218 TREE_PURPOSE (n) = TREE_PURPOSE (p2);
219 any_change = 1;
222 else
224 if (1 != simple_cst_equal (TREE_PURPOSE (p1), TREE_PURPOSE (p2)))
225 any_change = 1;
226 TREE_PURPOSE (n) = TREE_PURPOSE (p2);
228 if (TREE_VALUE (p1) != TREE_VALUE (p2))
230 any_change = 1;
231 TREE_VALUE (n) = merge_types (TREE_VALUE (p1), TREE_VALUE (p2));
233 else
234 TREE_VALUE (n) = TREE_VALUE (p1);
236 if (! any_change)
237 return oldargs;
239 return newargs;
242 /* Given a type, perhaps copied for a typedef,
243 find the "original" version of it. */
244 static tree
245 original_type (tree t)
247 int quals = cp_type_quals (t);
248 while (t != error_mark_node
249 && TYPE_NAME (t) != NULL_TREE)
251 tree x = TYPE_NAME (t);
252 if (TREE_CODE (x) != TYPE_DECL)
253 break;
254 x = DECL_ORIGINAL_TYPE (x);
255 if (x == NULL_TREE)
256 break;
257 t = x;
259 return cp_build_qualified_type (t, quals);
262 /* Return the common type for two arithmetic types T1 and T2 under the
263 usual arithmetic conversions. The default conversions have already
264 been applied, and enumerated types converted to their compatible
265 integer types. */
267 static tree
268 cp_common_type (tree t1, tree t2)
270 enum tree_code code1 = TREE_CODE (t1);
271 enum tree_code code2 = TREE_CODE (t2);
272 tree attributes;
275 /* In what follows, we slightly generalize the rules given in [expr] so
276 as to deal with `long long' and `complex'. First, merge the
277 attributes. */
278 attributes = (*targetm.merge_type_attributes) (t1, t2);
280 if (SCOPED_ENUM_P (t1) || SCOPED_ENUM_P (t2))
282 if (TYPE_MAIN_VARIANT (t1) == TYPE_MAIN_VARIANT (t2))
283 return build_type_attribute_variant (t1, attributes);
284 else
285 return NULL_TREE;
288 /* FIXME: Attributes. */
289 gcc_assert (ARITHMETIC_TYPE_P (t1)
290 || TREE_CODE (t1) == VECTOR_TYPE
291 || UNSCOPED_ENUM_P (t1));
292 gcc_assert (ARITHMETIC_TYPE_P (t2)
293 || TREE_CODE (t2) == VECTOR_TYPE
294 || UNSCOPED_ENUM_P (t2));
296 /* If one type is complex, form the common type of the non-complex
297 components, then make that complex. Use T1 or T2 if it is the
298 required type. */
299 if (code1 == COMPLEX_TYPE || code2 == COMPLEX_TYPE)
301 tree subtype1 = code1 == COMPLEX_TYPE ? TREE_TYPE (t1) : t1;
302 tree subtype2 = code2 == COMPLEX_TYPE ? TREE_TYPE (t2) : t2;
303 tree subtype
304 = type_after_usual_arithmetic_conversions (subtype1, subtype2);
306 if (code1 == COMPLEX_TYPE && TREE_TYPE (t1) == subtype)
307 return build_type_attribute_variant (t1, attributes);
308 else if (code2 == COMPLEX_TYPE && TREE_TYPE (t2) == subtype)
309 return build_type_attribute_variant (t2, attributes);
310 else
311 return build_type_attribute_variant (build_complex_type (subtype),
312 attributes);
315 if (code1 == VECTOR_TYPE)
317 /* When we get here we should have two vectors of the same size.
318 Just prefer the unsigned one if present. */
319 if (TYPE_UNSIGNED (t1))
320 return build_type_attribute_variant (t1, attributes);
321 else
322 return build_type_attribute_variant (t2, attributes);
325 /* If only one is real, use it as the result. */
326 if (code1 == REAL_TYPE && code2 != REAL_TYPE)
327 return build_type_attribute_variant (t1, attributes);
328 if (code2 == REAL_TYPE && code1 != REAL_TYPE)
329 return build_type_attribute_variant (t2, attributes);
331 /* Both real or both integers; use the one with greater precision. */
332 if (TYPE_PRECISION (t1) > TYPE_PRECISION (t2))
333 return build_type_attribute_variant (t1, attributes);
334 else if (TYPE_PRECISION (t2) > TYPE_PRECISION (t1))
335 return build_type_attribute_variant (t2, attributes);
337 /* The types are the same; no need to do anything fancy. */
338 if (TYPE_MAIN_VARIANT (t1) == TYPE_MAIN_VARIANT (t2))
339 return build_type_attribute_variant (t1, attributes);
341 if (code1 != REAL_TYPE)
343 /* If one is unsigned long long, then convert the other to unsigned
344 long long. */
345 if (same_type_p (TYPE_MAIN_VARIANT (t1), long_long_unsigned_type_node)
346 || same_type_p (TYPE_MAIN_VARIANT (t2), long_long_unsigned_type_node))
347 return build_type_attribute_variant (long_long_unsigned_type_node,
348 attributes);
349 /* If one is a long long, and the other is an unsigned long, and
350 long long can represent all the values of an unsigned long, then
351 convert to a long long. Otherwise, convert to an unsigned long
352 long. Otherwise, if either operand is long long, convert the
353 other to long long.
355 Since we're here, we know the TYPE_PRECISION is the same;
356 therefore converting to long long cannot represent all the values
357 of an unsigned long, so we choose unsigned long long in that
358 case. */
359 if (same_type_p (TYPE_MAIN_VARIANT (t1), long_long_integer_type_node)
360 || same_type_p (TYPE_MAIN_VARIANT (t2), long_long_integer_type_node))
362 tree t = ((TYPE_UNSIGNED (t1) || TYPE_UNSIGNED (t2))
363 ? long_long_unsigned_type_node
364 : long_long_integer_type_node);
365 return build_type_attribute_variant (t, attributes);
367 if (int128_integer_type_node != NULL_TREE
368 && (same_type_p (TYPE_MAIN_VARIANT (t1),
369 int128_integer_type_node)
370 || same_type_p (TYPE_MAIN_VARIANT (t2),
371 int128_integer_type_node)))
373 tree t = ((TYPE_UNSIGNED (t1) || TYPE_UNSIGNED (t2))
374 ? int128_unsigned_type_node
375 : int128_integer_type_node);
376 return build_type_attribute_variant (t, attributes);
379 /* Go through the same procedure, but for longs. */
380 if (same_type_p (TYPE_MAIN_VARIANT (t1), long_unsigned_type_node)
381 || same_type_p (TYPE_MAIN_VARIANT (t2), long_unsigned_type_node))
382 return build_type_attribute_variant (long_unsigned_type_node,
383 attributes);
384 if (same_type_p (TYPE_MAIN_VARIANT (t1), long_integer_type_node)
385 || same_type_p (TYPE_MAIN_VARIANT (t2), long_integer_type_node))
387 tree t = ((TYPE_UNSIGNED (t1) || TYPE_UNSIGNED (t2))
388 ? long_unsigned_type_node : long_integer_type_node);
389 return build_type_attribute_variant (t, attributes);
391 /* Otherwise prefer the unsigned one. */
392 if (TYPE_UNSIGNED (t1))
393 return build_type_attribute_variant (t1, attributes);
394 else
395 return build_type_attribute_variant (t2, attributes);
397 else
399 if (same_type_p (TYPE_MAIN_VARIANT (t1), long_double_type_node)
400 || same_type_p (TYPE_MAIN_VARIANT (t2), long_double_type_node))
401 return build_type_attribute_variant (long_double_type_node,
402 attributes);
403 if (same_type_p (TYPE_MAIN_VARIANT (t1), double_type_node)
404 || same_type_p (TYPE_MAIN_VARIANT (t2), double_type_node))
405 return build_type_attribute_variant (double_type_node,
406 attributes);
407 if (same_type_p (TYPE_MAIN_VARIANT (t1), float_type_node)
408 || same_type_p (TYPE_MAIN_VARIANT (t2), float_type_node))
409 return build_type_attribute_variant (float_type_node,
410 attributes);
412 /* Two floating-point types whose TYPE_MAIN_VARIANTs are none of
413 the standard C++ floating-point types. Logic earlier in this
414 function has already eliminated the possibility that
415 TYPE_PRECISION (t2) != TYPE_PRECISION (t1), so there's no
416 compelling reason to choose one or the other. */
417 return build_type_attribute_variant (t1, attributes);
421 /* T1 and T2 are arithmetic or enumeration types. Return the type
422 that will result from the "usual arithmetic conversions" on T1 and
423 T2 as described in [expr]. */
425 tree
426 type_after_usual_arithmetic_conversions (tree t1, tree t2)
428 gcc_assert (ARITHMETIC_TYPE_P (t1)
429 || TREE_CODE (t1) == VECTOR_TYPE
430 || UNSCOPED_ENUM_P (t1));
431 gcc_assert (ARITHMETIC_TYPE_P (t2)
432 || TREE_CODE (t2) == VECTOR_TYPE
433 || UNSCOPED_ENUM_P (t2));
435 /* Perform the integral promotions. We do not promote real types here. */
436 if (INTEGRAL_OR_ENUMERATION_TYPE_P (t1)
437 && INTEGRAL_OR_ENUMERATION_TYPE_P (t2))
439 t1 = type_promotes_to (t1);
440 t2 = type_promotes_to (t2);
443 return cp_common_type (t1, t2);
446 static void
447 composite_pointer_error (diagnostic_t kind, tree t1, tree t2,
448 composite_pointer_operation operation)
450 switch (operation)
452 case CPO_COMPARISON:
453 emit_diagnostic (kind, input_location, 0,
454 "comparison between "
455 "distinct pointer types %qT and %qT lacks a cast",
456 t1, t2);
457 break;
458 case CPO_CONVERSION:
459 emit_diagnostic (kind, input_location, 0,
460 "conversion between "
461 "distinct pointer types %qT and %qT lacks a cast",
462 t1, t2);
463 break;
464 case CPO_CONDITIONAL_EXPR:
465 emit_diagnostic (kind, input_location, 0,
466 "conditional expression between "
467 "distinct pointer types %qT and %qT lacks a cast",
468 t1, t2);
469 break;
470 default:
471 gcc_unreachable ();
475 /* Subroutine of composite_pointer_type to implement the recursive
476 case. See that function for documentation of the parameters. */
478 static tree
479 composite_pointer_type_r (tree t1, tree t2,
480 composite_pointer_operation operation,
481 tsubst_flags_t complain)
483 tree pointee1;
484 tree pointee2;
485 tree result_type;
486 tree attributes;
488 /* Determine the types pointed to by T1 and T2. */
489 if (TREE_CODE (t1) == POINTER_TYPE)
491 pointee1 = TREE_TYPE (t1);
492 pointee2 = TREE_TYPE (t2);
494 else
496 pointee1 = TYPE_PTRMEM_POINTED_TO_TYPE (t1);
497 pointee2 = TYPE_PTRMEM_POINTED_TO_TYPE (t2);
500 /* [expr.rel]
502 Otherwise, the composite pointer type is a pointer type
503 similar (_conv.qual_) to the type of one of the operands,
504 with a cv-qualification signature (_conv.qual_) that is the
505 union of the cv-qualification signatures of the operand
506 types. */
507 if (same_type_ignoring_top_level_qualifiers_p (pointee1, pointee2))
508 result_type = pointee1;
509 else if ((TREE_CODE (pointee1) == POINTER_TYPE
510 && TREE_CODE (pointee2) == POINTER_TYPE)
511 || (TYPE_PTRMEM_P (pointee1) && TYPE_PTRMEM_P (pointee2)))
513 result_type = composite_pointer_type_r (pointee1, pointee2, operation,
514 complain);
515 if (result_type == error_mark_node)
516 return error_mark_node;
518 else
520 if (complain & tf_error)
521 composite_pointer_error (DK_PERMERROR, t1, t2, operation);
522 else
523 return error_mark_node;
524 result_type = void_type_node;
526 result_type = cp_build_qualified_type (result_type,
527 (cp_type_quals (pointee1)
528 | cp_type_quals (pointee2)));
529 /* If the original types were pointers to members, so is the
530 result. */
531 if (TYPE_PTRMEM_P (t1))
533 if (!same_type_p (TYPE_PTRMEM_CLASS_TYPE (t1),
534 TYPE_PTRMEM_CLASS_TYPE (t2)))
536 if (complain & tf_error)
537 composite_pointer_error (DK_PERMERROR, t1, t2, operation);
538 else
539 return error_mark_node;
541 result_type = build_ptrmem_type (TYPE_PTRMEM_CLASS_TYPE (t1),
542 result_type);
544 else
545 result_type = build_pointer_type (result_type);
547 /* Merge the attributes. */
548 attributes = (*targetm.merge_type_attributes) (t1, t2);
549 return build_type_attribute_variant (result_type, attributes);
552 /* Return the composite pointer type (see [expr.rel]) for T1 and T2.
553 ARG1 and ARG2 are the values with those types. The OPERATION is to
554 describe the operation between the pointer types,
555 in case an error occurs.
557 This routine also implements the computation of a common type for
558 pointers-to-members as per [expr.eq]. */
560 tree
561 composite_pointer_type (tree t1, tree t2, tree arg1, tree arg2,
562 composite_pointer_operation operation,
563 tsubst_flags_t complain)
565 tree class1;
566 tree class2;
568 /* [expr.rel]
570 If one operand is a null pointer constant, the composite pointer
571 type is the type of the other operand. */
572 if (null_ptr_cst_p (arg1))
573 return t2;
574 if (null_ptr_cst_p (arg2))
575 return t1;
577 /* We have:
579 [expr.rel]
581 If one of the operands has type "pointer to cv1 void*", then
582 the other has type "pointer to cv2T", and the composite pointer
583 type is "pointer to cv12 void", where cv12 is the union of cv1
584 and cv2.
586 If either type is a pointer to void, make sure it is T1. */
587 if (TREE_CODE (t2) == POINTER_TYPE && VOID_TYPE_P (TREE_TYPE (t2)))
589 tree t;
590 t = t1;
591 t1 = t2;
592 t2 = t;
595 /* Now, if T1 is a pointer to void, merge the qualifiers. */
596 if (TREE_CODE (t1) == POINTER_TYPE && VOID_TYPE_P (TREE_TYPE (t1)))
598 tree attributes;
599 tree result_type;
601 if (TYPE_PTRFN_P (t2) && (complain & tf_error))
603 switch (operation)
605 case CPO_COMPARISON:
606 pedwarn (input_location, OPT_Wpedantic,
607 "ISO C++ forbids comparison between "
608 "pointer of type %<void *%> and pointer-to-function");
609 break;
610 case CPO_CONVERSION:
611 pedwarn (input_location, OPT_Wpedantic,
612 "ISO C++ forbids conversion between "
613 "pointer of type %<void *%> and pointer-to-function");
614 break;
615 case CPO_CONDITIONAL_EXPR:
616 pedwarn (input_location, OPT_Wpedantic,
617 "ISO C++ forbids conditional expression between "
618 "pointer of type %<void *%> and pointer-to-function");
619 break;
620 default:
621 gcc_unreachable ();
624 result_type
625 = cp_build_qualified_type (void_type_node,
626 (cp_type_quals (TREE_TYPE (t1))
627 | cp_type_quals (TREE_TYPE (t2))));
628 result_type = build_pointer_type (result_type);
629 /* Merge the attributes. */
630 attributes = (*targetm.merge_type_attributes) (t1, t2);
631 return build_type_attribute_variant (result_type, attributes);
634 if (c_dialect_objc () && TREE_CODE (t1) == POINTER_TYPE
635 && TREE_CODE (t2) == POINTER_TYPE)
637 if (objc_have_common_type (t1, t2, -3, NULL_TREE))
638 return objc_common_type (t1, t2);
641 /* [expr.eq] permits the application of a pointer conversion to
642 bring the pointers to a common type. */
643 if (TREE_CODE (t1) == POINTER_TYPE && TREE_CODE (t2) == POINTER_TYPE
644 && CLASS_TYPE_P (TREE_TYPE (t1))
645 && CLASS_TYPE_P (TREE_TYPE (t2))
646 && !same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (t1),
647 TREE_TYPE (t2)))
649 class1 = TREE_TYPE (t1);
650 class2 = TREE_TYPE (t2);
652 if (DERIVED_FROM_P (class1, class2))
653 t2 = (build_pointer_type
654 (cp_build_qualified_type (class1, cp_type_quals (class2))));
655 else if (DERIVED_FROM_P (class2, class1))
656 t1 = (build_pointer_type
657 (cp_build_qualified_type (class2, cp_type_quals (class1))));
658 else
660 if (complain & tf_error)
661 composite_pointer_error (DK_ERROR, t1, t2, operation);
662 return error_mark_node;
665 /* [expr.eq] permits the application of a pointer-to-member
666 conversion to change the class type of one of the types. */
667 else if (TYPE_PTRMEM_P (t1)
668 && !same_type_p (TYPE_PTRMEM_CLASS_TYPE (t1),
669 TYPE_PTRMEM_CLASS_TYPE (t2)))
671 class1 = TYPE_PTRMEM_CLASS_TYPE (t1);
672 class2 = TYPE_PTRMEM_CLASS_TYPE (t2);
674 if (DERIVED_FROM_P (class1, class2))
675 t1 = build_ptrmem_type (class2, TYPE_PTRMEM_POINTED_TO_TYPE (t1));
676 else if (DERIVED_FROM_P (class2, class1))
677 t2 = build_ptrmem_type (class1, TYPE_PTRMEM_POINTED_TO_TYPE (t2));
678 else
680 if (complain & tf_error)
681 switch (operation)
683 case CPO_COMPARISON:
684 error ("comparison between distinct "
685 "pointer-to-member types %qT and %qT lacks a cast",
686 t1, t2);
687 break;
688 case CPO_CONVERSION:
689 error ("conversion between distinct "
690 "pointer-to-member types %qT and %qT lacks a cast",
691 t1, t2);
692 break;
693 case CPO_CONDITIONAL_EXPR:
694 error ("conditional expression between distinct "
695 "pointer-to-member types %qT and %qT lacks a cast",
696 t1, t2);
697 break;
698 default:
699 gcc_unreachable ();
701 return error_mark_node;
705 return composite_pointer_type_r (t1, t2, operation, complain);
708 /* Return the merged type of two types.
709 We assume that comptypes has already been done and returned 1;
710 if that isn't so, this may crash.
712 This just combines attributes and default arguments; any other
713 differences would cause the two types to compare unalike. */
715 tree
716 merge_types (tree t1, tree t2)
718 enum tree_code code1;
719 enum tree_code code2;
720 tree attributes;
722 /* Save time if the two types are the same. */
723 if (t1 == t2)
724 return t1;
725 if (original_type (t1) == original_type (t2))
726 return t1;
728 /* If one type is nonsense, use the other. */
729 if (t1 == error_mark_node)
730 return t2;
731 if (t2 == error_mark_node)
732 return t1;
734 /* Handle merging an auto redeclaration with a previous deduced
735 return type. */
736 if (is_auto (t1))
737 return t2;
739 /* Merge the attributes. */
740 attributes = (*targetm.merge_type_attributes) (t1, t2);
742 if (TYPE_PTRMEMFUNC_P (t1))
743 t1 = TYPE_PTRMEMFUNC_FN_TYPE (t1);
744 if (TYPE_PTRMEMFUNC_P (t2))
745 t2 = TYPE_PTRMEMFUNC_FN_TYPE (t2);
747 code1 = TREE_CODE (t1);
748 code2 = TREE_CODE (t2);
749 if (code1 != code2)
751 gcc_assert (code1 == TYPENAME_TYPE || code2 == TYPENAME_TYPE);
752 if (code1 == TYPENAME_TYPE)
754 t1 = resolve_typename_type (t1, /*only_current_p=*/true);
755 code1 = TREE_CODE (t1);
757 else
759 t2 = resolve_typename_type (t2, /*only_current_p=*/true);
760 code2 = TREE_CODE (t2);
764 switch (code1)
766 case POINTER_TYPE:
767 case REFERENCE_TYPE:
768 /* For two pointers, do this recursively on the target type. */
770 tree target = merge_types (TREE_TYPE (t1), TREE_TYPE (t2));
771 int quals = cp_type_quals (t1);
773 if (code1 == POINTER_TYPE)
774 t1 = build_pointer_type (target);
775 else
776 t1 = cp_build_reference_type (target, TYPE_REF_IS_RVALUE (t1));
777 t1 = build_type_attribute_variant (t1, attributes);
778 t1 = cp_build_qualified_type (t1, quals);
780 if (TREE_CODE (target) == METHOD_TYPE)
781 t1 = build_ptrmemfunc_type (t1);
783 return t1;
786 case OFFSET_TYPE:
788 int quals;
789 tree pointee;
790 quals = cp_type_quals (t1);
791 pointee = merge_types (TYPE_PTRMEM_POINTED_TO_TYPE (t1),
792 TYPE_PTRMEM_POINTED_TO_TYPE (t2));
793 t1 = build_ptrmem_type (TYPE_PTRMEM_CLASS_TYPE (t1),
794 pointee);
795 t1 = cp_build_qualified_type (t1, quals);
796 break;
799 case ARRAY_TYPE:
801 tree elt = merge_types (TREE_TYPE (t1), TREE_TYPE (t2));
802 /* Save space: see if the result is identical to one of the args. */
803 if (elt == TREE_TYPE (t1) && TYPE_DOMAIN (t1))
804 return build_type_attribute_variant (t1, attributes);
805 if (elt == TREE_TYPE (t2) && TYPE_DOMAIN (t2))
806 return build_type_attribute_variant (t2, attributes);
807 /* Merge the element types, and have a size if either arg has one. */
808 t1 = build_cplus_array_type
809 (elt, TYPE_DOMAIN (TYPE_DOMAIN (t1) ? t1 : t2));
810 break;
813 case FUNCTION_TYPE:
814 /* Function types: prefer the one that specified arg types.
815 If both do, merge the arg types. Also merge the return types. */
817 tree valtype = merge_types (TREE_TYPE (t1), TREE_TYPE (t2));
818 tree p1 = TYPE_ARG_TYPES (t1);
819 tree p2 = TYPE_ARG_TYPES (t2);
820 tree parms;
821 tree rval, raises;
823 /* Save space: see if the result is identical to one of the args. */
824 if (valtype == TREE_TYPE (t1) && ! p2)
825 return cp_build_type_attribute_variant (t1, attributes);
826 if (valtype == TREE_TYPE (t2) && ! p1)
827 return cp_build_type_attribute_variant (t2, attributes);
829 /* Simple way if one arg fails to specify argument types. */
830 if (p1 == NULL_TREE || TREE_VALUE (p1) == void_type_node)
831 parms = p2;
832 else if (p2 == NULL_TREE || TREE_VALUE (p2) == void_type_node)
833 parms = p1;
834 else
835 parms = commonparms (p1, p2);
837 rval = build_function_type (valtype, parms);
838 gcc_assert (type_memfn_quals (t1) == type_memfn_quals (t2));
839 rval = apply_memfn_quals (rval, type_memfn_quals (t1));
840 raises = merge_exception_specifiers (TYPE_RAISES_EXCEPTIONS (t1),
841 TYPE_RAISES_EXCEPTIONS (t2),
842 NULL_TREE);
843 t1 = build_exception_variant (rval, raises);
844 break;
847 case METHOD_TYPE:
849 /* Get this value the long way, since TYPE_METHOD_BASETYPE
850 is just the main variant of this. */
851 tree basetype = class_of_this_parm (t2);
852 tree raises = merge_exception_specifiers (TYPE_RAISES_EXCEPTIONS (t1),
853 TYPE_RAISES_EXCEPTIONS (t2),
854 NULL_TREE);
855 tree t3;
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 break;
871 case TYPENAME_TYPE:
872 /* There is no need to merge attributes into a TYPENAME_TYPE.
873 When the type is instantiated it will have whatever
874 attributes result from the instantiation. */
875 return t1;
877 default:;
880 if (attribute_list_equal (TYPE_ATTRIBUTES (t1), attributes))
881 return t1;
882 else if (attribute_list_equal (TYPE_ATTRIBUTES (t2), attributes))
883 return t2;
884 else
885 return cp_build_type_attribute_variant (t1, attributes);
888 /* Return the ARRAY_TYPE type without its domain. */
890 tree
891 strip_array_domain (tree type)
893 tree t2;
894 gcc_assert (TREE_CODE (type) == ARRAY_TYPE);
895 if (TYPE_DOMAIN (type) == NULL_TREE)
896 return type;
897 t2 = build_cplus_array_type (TREE_TYPE (type), NULL_TREE);
898 return cp_build_type_attribute_variant (t2, TYPE_ATTRIBUTES (type));
901 /* Wrapper around cp_common_type that is used by c-common.c and other
902 front end optimizations that remove promotions.
904 Return the common type for two arithmetic types T1 and T2 under the
905 usual arithmetic conversions. The default conversions have already
906 been applied, and enumerated types converted to their compatible
907 integer types. */
909 tree
910 common_type (tree t1, tree t2)
912 /* If one type is nonsense, use the other */
913 if (t1 == error_mark_node)
914 return t2;
915 if (t2 == error_mark_node)
916 return t1;
918 return cp_common_type (t1, t2);
921 /* Return the common type of two pointer types T1 and T2. This is the
922 type for the result of most arithmetic operations if the operands
923 have the given two types.
925 We assume that comp_target_types has already been done and returned
926 nonzero; if that isn't so, this may crash. */
928 tree
929 common_pointer_type (tree t1, tree t2)
931 gcc_assert ((TYPE_PTR_P (t1) && TYPE_PTR_P (t2))
932 || (TYPE_PTRDATAMEM_P (t1) && TYPE_PTRDATAMEM_P (t2))
933 || (TYPE_PTRMEMFUNC_P (t1) && TYPE_PTRMEMFUNC_P (t2)));
935 return composite_pointer_type (t1, t2, error_mark_node, error_mark_node,
936 CPO_CONVERSION, tf_warning_or_error);
939 /* Compare two exception specifier types for exactness or subsetness, if
940 allowed. Returns false for mismatch, true for match (same, or
941 derived and !exact).
943 [except.spec] "If a class X ... objects of class X or any class publicly
944 and unambiguously derived from X. Similarly, if a pointer type Y * ...
945 exceptions of type Y * or that are pointers to any type publicly and
946 unambiguously derived from Y. Otherwise a function only allows exceptions
947 that have the same type ..."
948 This does not mention cv qualifiers and is different to what throw
949 [except.throw] and catch [except.catch] will do. They will ignore the
950 top level cv qualifiers, and allow qualifiers in the pointer to class
951 example.
953 We implement the letter of the standard. */
955 static bool
956 comp_except_types (tree a, tree b, bool exact)
958 if (same_type_p (a, b))
959 return true;
960 else if (!exact)
962 if (cp_type_quals (a) || cp_type_quals (b))
963 return false;
965 if (TREE_CODE (a) == POINTER_TYPE
966 && TREE_CODE (b) == POINTER_TYPE)
968 a = TREE_TYPE (a);
969 b = TREE_TYPE (b);
970 if (cp_type_quals (a) || cp_type_quals (b))
971 return false;
974 if (TREE_CODE (a) != RECORD_TYPE
975 || TREE_CODE (b) != RECORD_TYPE)
976 return false;
978 if (publicly_uniquely_derived_p (a, b))
979 return true;
981 return false;
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. */
991 bool
992 comp_except_specs (const_tree t1, const_tree t2, int exact)
994 const_tree probe;
995 const_tree base;
996 int length = 0;
998 if (t1 == t2)
999 return true;
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)))
1026 return (t1 && 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 ... */
1034 return false;
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
1041 O(nm). */
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);
1053 length++;
1054 break;
1057 if (probe == NULL_TREE)
1058 return false;
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]. */
1066 static bool
1067 comp_array_types (const_tree t1, const_tree t2, bool allow_redeclaration)
1069 tree d1;
1070 tree d2;
1071 tree max1, max2;
1073 if (t1 == t2)
1074 return true;
1076 /* The type of the array elements must be the same. */
1077 if (!same_type_p (TREE_TYPE (t1), TREE_TYPE (t2)))
1078 return false;
1080 d1 = TYPE_DOMAIN (t1);
1081 d2 = TYPE_DOMAIN (t2);
1083 if (d1 == d2)
1084 return true;
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
1088 write:
1090 extern int a[];
1091 int a[3];
1093 by [basic.link]:
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_). */
1098 if (!d1 || !d2)
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)))
1104 return false;
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
1114 value. */
1115 max1 = fold (max1);
1116 max2 = fold (max2);
1119 if (!cp_tree_equal (max1, max2))
1120 return false;
1122 return true;
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. */
1130 static bool
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)))
1148 return false;
1150 return true;
1153 /* Subroutine in comptypes. */
1155 static bool
1156 structural_comptypes (tree t1, tree t2, int strict)
1158 if (t1 == t2)
1159 return true;
1161 /* Suppress errors caused by previously reported errors. */
1162 if (t1 == error_mark_node || t2 == error_mark_node)
1163 return false;
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))
1182 return false;
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))
1188 return false;
1189 if (TREE_CODE (t1) == FUNCTION_TYPE
1190 && type_memfn_quals (t1) != type_memfn_quals (t2))
1191 return false;
1192 if (TYPE_FOR_JAVA (t1) != TYPE_FOR_JAVA (t2))
1193 return false;
1195 /* Allow for two different type nodes which have essentially the same
1196 definition. Note that we already checked for equality of the type
1197 qualifiers (just above). */
1199 if (TREE_CODE (t1) != ARRAY_TYPE
1200 && TYPE_MAIN_VARIANT (t1) == TYPE_MAIN_VARIANT (t2))
1201 return true;
1204 /* Compare the types. Break out if they could be the same. */
1205 switch (TREE_CODE (t1))
1207 case VOID_TYPE:
1208 case BOOLEAN_TYPE:
1209 /* All void and bool types are the same. */
1210 break;
1212 case INTEGER_TYPE:
1213 case FIXED_POINT_TYPE:
1214 case REAL_TYPE:
1215 /* With these nodes, we can't determine type equivalence by
1216 looking at what is stored in the nodes themselves, because
1217 two nodes might have different TYPE_MAIN_VARIANTs but still
1218 represent the same type. For example, wchar_t and int could
1219 have the same properties (TYPE_PRECISION, TYPE_MIN_VALUE,
1220 TYPE_MAX_VALUE, etc.), but have different TYPE_MAIN_VARIANTs
1221 and are distinct types. On the other hand, int and the
1222 following typedef
1224 typedef int INT __attribute((may_alias));
1226 have identical properties, different TYPE_MAIN_VARIANTs, but
1227 represent the same type. The canonical type system keeps
1228 track of equivalence in this case, so we fall back on it. */
1229 return TYPE_CANONICAL (t1) == TYPE_CANONICAL (t2);
1231 case TEMPLATE_TEMPLATE_PARM:
1232 case BOUND_TEMPLATE_TEMPLATE_PARM:
1233 if (!comp_template_parms_position (t1, t2))
1234 return false;
1235 if (!comp_template_parms
1236 (DECL_TEMPLATE_PARMS (TEMPLATE_TEMPLATE_PARM_TEMPLATE_DECL (t1)),
1237 DECL_TEMPLATE_PARMS (TEMPLATE_TEMPLATE_PARM_TEMPLATE_DECL (t2))))
1238 return false;
1239 if (TREE_CODE (t1) == TEMPLATE_TEMPLATE_PARM)
1240 break;
1241 /* Don't check inheritance. */
1242 strict = COMPARE_STRICT;
1243 /* Fall through. */
1245 case RECORD_TYPE:
1246 case UNION_TYPE:
1247 if (TYPE_TEMPLATE_INFO (t1) && TYPE_TEMPLATE_INFO (t2)
1248 && (TYPE_TI_TEMPLATE (t1) == TYPE_TI_TEMPLATE (t2)
1249 || TREE_CODE (t1) == BOUND_TEMPLATE_TEMPLATE_PARM)
1250 && comp_template_args (TYPE_TI_ARGS (t1), TYPE_TI_ARGS (t2)))
1251 break;
1253 if ((strict & COMPARE_BASE) && DERIVED_FROM_P (t1, t2))
1254 break;
1255 else if ((strict & COMPARE_DERIVED) && DERIVED_FROM_P (t2, t1))
1256 break;
1258 return false;
1260 case OFFSET_TYPE:
1261 if (!comptypes (TYPE_OFFSET_BASETYPE (t1), TYPE_OFFSET_BASETYPE (t2),
1262 strict & ~COMPARE_REDECLARATION))
1263 return false;
1264 if (!same_type_p (TREE_TYPE (t1), TREE_TYPE (t2)))
1265 return false;
1266 break;
1268 case REFERENCE_TYPE:
1269 if (TYPE_REF_IS_RVALUE (t1) != TYPE_REF_IS_RVALUE (t2))
1270 return false;
1271 /* fall through to checks for pointer types */
1273 case POINTER_TYPE:
1274 if (TYPE_MODE (t1) != TYPE_MODE (t2)
1275 || TYPE_REF_CAN_ALIAS_ALL (t1) != TYPE_REF_CAN_ALIAS_ALL (t2)
1276 || !same_type_p (TREE_TYPE (t1), TREE_TYPE (t2)))
1277 return false;
1278 break;
1280 case METHOD_TYPE:
1281 case FUNCTION_TYPE:
1282 if (!same_type_p (TREE_TYPE (t1), TREE_TYPE (t2)))
1283 return false;
1284 if (!compparms (TYPE_ARG_TYPES (t1), TYPE_ARG_TYPES (t2)))
1285 return false;
1286 break;
1288 case ARRAY_TYPE:
1289 /* Target types must match incl. qualifiers. */
1290 if (!comp_array_types (t1, t2, !!(strict & COMPARE_REDECLARATION)))
1291 return false;
1292 break;
1294 case TEMPLATE_TYPE_PARM:
1295 /* If T1 and T2 don't have the same relative position in their
1296 template parameters set, they can't be equal. */
1297 if (!comp_template_parms_position (t1, t2))
1298 return false;
1299 break;
1301 case TYPENAME_TYPE:
1302 if (!cp_tree_equal (TYPENAME_TYPE_FULLNAME (t1),
1303 TYPENAME_TYPE_FULLNAME (t2)))
1304 return false;
1305 /* Qualifiers don't matter on scopes. */
1306 if (!same_type_ignoring_top_level_qualifiers_p (TYPE_CONTEXT (t1),
1307 TYPE_CONTEXT (t2)))
1308 return false;
1309 break;
1311 case UNBOUND_CLASS_TEMPLATE:
1312 if (!cp_tree_equal (TYPE_IDENTIFIER (t1), TYPE_IDENTIFIER (t2)))
1313 return false;
1314 if (!same_type_p (TYPE_CONTEXT (t1), TYPE_CONTEXT (t2)))
1315 return false;
1316 break;
1318 case COMPLEX_TYPE:
1319 if (!same_type_p (TREE_TYPE (t1), TREE_TYPE (t2)))
1320 return false;
1321 break;
1323 case VECTOR_TYPE:
1324 if (TYPE_VECTOR_SUBPARTS (t1) != TYPE_VECTOR_SUBPARTS (t2)
1325 || !same_type_p (TREE_TYPE (t1), TREE_TYPE (t2)))
1326 return false;
1327 break;
1329 case TYPE_PACK_EXPANSION:
1330 return (same_type_p (PACK_EXPANSION_PATTERN (t1),
1331 PACK_EXPANSION_PATTERN (t2))
1332 && comp_template_args (PACK_EXPANSION_EXTRA_ARGS (t1),
1333 PACK_EXPANSION_EXTRA_ARGS (t2)));
1335 case DECLTYPE_TYPE:
1336 if (DECLTYPE_TYPE_ID_EXPR_OR_MEMBER_ACCESS_P (t1)
1337 != DECLTYPE_TYPE_ID_EXPR_OR_MEMBER_ACCESS_P (t2)
1338 || (DECLTYPE_FOR_LAMBDA_CAPTURE (t1)
1339 != DECLTYPE_FOR_LAMBDA_CAPTURE (t2))
1340 || (DECLTYPE_FOR_LAMBDA_PROXY (t1)
1341 != DECLTYPE_FOR_LAMBDA_PROXY (t2))
1342 || !cp_tree_equal (DECLTYPE_TYPE_EXPR (t1),
1343 DECLTYPE_TYPE_EXPR (t2)))
1344 return false;
1345 break;
1347 case UNDERLYING_TYPE:
1348 return same_type_p (UNDERLYING_TYPE_TYPE (t1),
1349 UNDERLYING_TYPE_TYPE (t2));
1351 default:
1352 return false;
1355 /* If we get here, we know that from a target independent POV the
1356 types are the same. Make sure the target attributes are also
1357 the same. */
1358 return comp_type_attributes (t1, t2);
1361 /* Return true if T1 and T2 are related as allowed by STRICT. STRICT
1362 is a bitwise-or of the COMPARE_* flags. */
1364 bool
1365 comptypes (tree t1, tree t2, int strict)
1367 if (strict == COMPARE_STRICT)
1369 if (t1 == t2)
1370 return true;
1372 if (t1 == error_mark_node || t2 == error_mark_node)
1373 return false;
1375 if (TYPE_STRUCTURAL_EQUALITY_P (t1) || TYPE_STRUCTURAL_EQUALITY_P (t2))
1376 /* At least one of the types requires structural equality, so
1377 perform a deep check. */
1378 return structural_comptypes (t1, t2, strict);
1380 #ifdef ENABLE_CHECKING
1381 if (USE_CANONICAL_TYPES)
1383 bool result = structural_comptypes (t1, t2, strict);
1385 if (result && TYPE_CANONICAL (t1) != TYPE_CANONICAL (t2))
1386 /* The two types are structurally equivalent, but their
1387 canonical types were different. This is a failure of the
1388 canonical type propagation code.*/
1389 internal_error
1390 ("canonical types differ for identical types %T and %T",
1391 t1, t2);
1392 else if (!result && TYPE_CANONICAL (t1) == TYPE_CANONICAL (t2))
1393 /* Two types are structurally different, but the canonical
1394 types are the same. This means we were over-eager in
1395 assigning canonical types. */
1396 internal_error
1397 ("same canonical type node for different types %T and %T",
1398 t1, t2);
1400 return result;
1402 #else
1403 if (USE_CANONICAL_TYPES)
1404 return TYPE_CANONICAL (t1) == TYPE_CANONICAL (t2);
1405 #endif
1406 else
1407 return structural_comptypes (t1, t2, strict);
1409 else if (strict == COMPARE_STRUCTURAL)
1410 return structural_comptypes (t1, t2, COMPARE_STRICT);
1411 else
1412 return structural_comptypes (t1, t2, strict);
1415 /* Returns nonzero iff TYPE1 and TYPE2 are the same type, ignoring
1416 top-level qualifiers. */
1418 bool
1419 same_type_ignoring_top_level_qualifiers_p (tree type1, tree type2)
1421 if (type1 == error_mark_node || type2 == error_mark_node)
1422 return false;
1424 return same_type_p (TYPE_MAIN_VARIANT (type1), TYPE_MAIN_VARIANT (type2));
1427 /* Returns 1 if TYPE1 is at least as qualified as TYPE2. */
1429 bool
1430 at_least_as_qualified_p (const_tree type1, const_tree type2)
1432 int q1 = cp_type_quals (type1);
1433 int q2 = cp_type_quals (type2);
1435 /* All qualifiers for TYPE2 must also appear in TYPE1. */
1436 return (q1 & q2) == q2;
1439 /* Returns 1 if TYPE1 is more cv-qualified than TYPE2, -1 if TYPE2 is
1440 more cv-qualified that TYPE1, and 0 otherwise. */
1443 comp_cv_qualification (const_tree type1, const_tree type2)
1445 int q1 = cp_type_quals (type1);
1446 int q2 = cp_type_quals (type2);
1448 if (q1 == q2)
1449 return 0;
1451 if ((q1 & q2) == q2)
1452 return 1;
1453 else if ((q1 & q2) == q1)
1454 return -1;
1456 return 0;
1459 /* Returns 1 if the cv-qualification signature of TYPE1 is a proper
1460 subset of the cv-qualification signature of TYPE2, and the types
1461 are similar. Returns -1 if the other way 'round, and 0 otherwise. */
1464 comp_cv_qual_signature (tree type1, tree type2)
1466 if (comp_ptr_ttypes_real (type2, type1, -1))
1467 return 1;
1468 else if (comp_ptr_ttypes_real (type1, type2, -1))
1469 return -1;
1470 else
1471 return 0;
1474 /* Subroutines of `comptypes'. */
1476 /* Return true if two parameter type lists PARMS1 and PARMS2 are
1477 equivalent in the sense that functions with those parameter types
1478 can have equivalent types. The two lists must be equivalent,
1479 element by element. */
1481 bool
1482 compparms (const_tree parms1, const_tree parms2)
1484 const_tree t1, t2;
1486 /* An unspecified parmlist matches any specified parmlist
1487 whose argument types don't need default promotions. */
1489 for (t1 = parms1, t2 = parms2;
1490 t1 || t2;
1491 t1 = TREE_CHAIN (t1), t2 = TREE_CHAIN (t2))
1493 /* If one parmlist is shorter than the other,
1494 they fail to match. */
1495 if (!t1 || !t2)
1496 return false;
1497 if (!same_type_p (TREE_VALUE (t1), TREE_VALUE (t2)))
1498 return false;
1500 return true;
1504 /* Process a sizeof or alignof expression where the operand is a
1505 type. */
1507 tree
1508 cxx_sizeof_or_alignof_type (tree type, enum tree_code op, bool complain)
1510 tree value;
1511 bool dependent_p;
1513 gcc_assert (op == SIZEOF_EXPR || op == ALIGNOF_EXPR);
1514 if (type == error_mark_node)
1515 return error_mark_node;
1517 type = non_reference (type);
1518 if (TREE_CODE (type) == METHOD_TYPE)
1520 if (complain)
1521 pedwarn (input_location, pedantic ? OPT_Wpedantic : OPT_Wpointer_arith,
1522 "invalid application of %qs to a member function",
1523 operator_name_info[(int) op].name);
1524 value = size_one_node;
1527 dependent_p = dependent_type_p (type);
1528 if (!dependent_p)
1529 complete_type (type);
1530 if (dependent_p
1531 /* VLA types will have a non-constant size. In the body of an
1532 uninstantiated template, we don't need to try to compute the
1533 value, because the sizeof expression is not an integral
1534 constant expression in that case. And, if we do try to
1535 compute the value, we'll likely end up with SAVE_EXPRs, which
1536 the template substitution machinery does not expect to see. */
1537 || (processing_template_decl
1538 && COMPLETE_TYPE_P (type)
1539 && TREE_CODE (TYPE_SIZE (type)) != INTEGER_CST))
1541 value = build_min (op, size_type_node, type);
1542 TREE_READONLY (value) = 1;
1543 return value;
1546 return c_sizeof_or_alignof_type (input_location, complete_type (type),
1547 op == SIZEOF_EXPR,
1548 complain);
1551 /* Return the size of the type, without producing any warnings for
1552 types whose size cannot be taken. This routine should be used only
1553 in some other routine that has already produced a diagnostic about
1554 using the size of such a type. */
1555 tree
1556 cxx_sizeof_nowarn (tree type)
1558 if (TREE_CODE (type) == FUNCTION_TYPE
1559 || TREE_CODE (type) == VOID_TYPE
1560 || TREE_CODE (type) == ERROR_MARK)
1561 return size_one_node;
1562 else if (!COMPLETE_TYPE_P (type))
1563 return size_zero_node;
1564 else
1565 return cxx_sizeof_or_alignof_type (type, SIZEOF_EXPR, false);
1568 /* Process a sizeof expression where the operand is an expression. */
1570 static tree
1571 cxx_sizeof_expr (tree e, tsubst_flags_t complain)
1573 if (e == error_mark_node)
1574 return error_mark_node;
1576 if (processing_template_decl)
1578 e = build_min (SIZEOF_EXPR, size_type_node, e);
1579 TREE_SIDE_EFFECTS (e) = 0;
1580 TREE_READONLY (e) = 1;
1582 return e;
1585 /* To get the size of a static data member declared as an array of
1586 unknown bound, we need to instantiate it. */
1587 if (TREE_CODE (e) == VAR_DECL
1588 && VAR_HAD_UNKNOWN_BOUND (e)
1589 && DECL_TEMPLATE_INSTANTIATION (e))
1590 instantiate_decl (e, /*defer_ok*/true, /*expl_inst_mem*/false);
1592 e = mark_type_use (e);
1594 if (TREE_CODE (e) == COMPONENT_REF
1595 && TREE_CODE (TREE_OPERAND (e, 1)) == FIELD_DECL
1596 && DECL_C_BIT_FIELD (TREE_OPERAND (e, 1)))
1598 if (complain & tf_error)
1599 error ("invalid application of %<sizeof%> to a bit-field");
1600 else
1601 return error_mark_node;
1602 e = char_type_node;
1604 else if (is_overloaded_fn (e))
1606 if (complain & tf_error)
1607 permerror (input_location, "ISO C++ forbids applying %<sizeof%> to an expression of "
1608 "function type");
1609 else
1610 return error_mark_node;
1611 e = char_type_node;
1613 else if (type_unknown_p (e))
1615 if (complain & tf_error)
1616 cxx_incomplete_type_error (e, TREE_TYPE (e));
1617 else
1618 return error_mark_node;
1619 e = char_type_node;
1621 else
1622 e = TREE_TYPE (e);
1624 return cxx_sizeof_or_alignof_type (e, SIZEOF_EXPR, complain & tf_error);
1627 /* Implement the __alignof keyword: Return the minimum required
1628 alignment of E, measured in bytes. For VAR_DECL's and
1629 FIELD_DECL's return DECL_ALIGN (which can be set from an
1630 "aligned" __attribute__ specification). */
1632 static tree
1633 cxx_alignof_expr (tree e, tsubst_flags_t complain)
1635 tree t;
1637 if (e == error_mark_node)
1638 return error_mark_node;
1640 if (processing_template_decl)
1642 e = build_min (ALIGNOF_EXPR, size_type_node, e);
1643 TREE_SIDE_EFFECTS (e) = 0;
1644 TREE_READONLY (e) = 1;
1646 return e;
1649 e = mark_type_use (e);
1651 if (TREE_CODE (e) == VAR_DECL)
1652 t = size_int (DECL_ALIGN_UNIT (e));
1653 else if (TREE_CODE (e) == COMPONENT_REF
1654 && TREE_CODE (TREE_OPERAND (e, 1)) == FIELD_DECL
1655 && DECL_C_BIT_FIELD (TREE_OPERAND (e, 1)))
1657 if (complain & tf_error)
1658 error ("invalid application of %<__alignof%> to a bit-field");
1659 else
1660 return error_mark_node;
1661 t = size_one_node;
1663 else if (TREE_CODE (e) == COMPONENT_REF
1664 && TREE_CODE (TREE_OPERAND (e, 1)) == FIELD_DECL)
1665 t = size_int (DECL_ALIGN_UNIT (TREE_OPERAND (e, 1)));
1666 else if (is_overloaded_fn (e))
1668 if (complain & tf_error)
1669 permerror (input_location, "ISO C++ forbids applying %<__alignof%> to an expression of "
1670 "function type");
1671 else
1672 return error_mark_node;
1673 if (TREE_CODE (e) == FUNCTION_DECL)
1674 t = size_int (DECL_ALIGN_UNIT (e));
1675 else
1676 t = size_one_node;
1678 else if (type_unknown_p (e))
1680 if (complain & tf_error)
1681 cxx_incomplete_type_error (e, TREE_TYPE (e));
1682 else
1683 return error_mark_node;
1684 t = size_one_node;
1686 else
1687 return cxx_sizeof_or_alignof_type (TREE_TYPE (e), ALIGNOF_EXPR,
1688 complain & tf_error);
1690 return fold_convert (size_type_node, t);
1693 /* Process a sizeof or alignof expression E with code OP where the operand
1694 is an expression. */
1696 tree
1697 cxx_sizeof_or_alignof_expr (tree e, enum tree_code op, bool complain)
1699 if (op == SIZEOF_EXPR)
1700 return cxx_sizeof_expr (e, complain? tf_warning_or_error : tf_none);
1701 else
1702 return cxx_alignof_expr (e, complain? tf_warning_or_error : tf_none);
1705 /* Build a representation of an expression 'alignas(E).' Return the
1706 folded integer value of E if it is an integral constant expression
1707 that resolves to a valid alignment. If E depends on a template
1708 parameter, return a syntactic representation tree of kind
1709 ALIGNOF_EXPR. Otherwise, return an error_mark_node if the
1710 expression is ill formed, or NULL_TREE if E is NULL_TREE. */
1712 tree
1713 cxx_alignas_expr (tree e)
1715 if (e == NULL_TREE || e == error_mark_node
1716 || (!TYPE_P (e) && !require_potential_rvalue_constant_expression (e)))
1717 return e;
1719 if (TYPE_P (e))
1720 /* [dcl.align]/3:
1722 When the alignment-specifier is of the form
1723 alignas(type-id ), it shall have the same effect as
1724 alignas( alignof(type-id )). */
1726 return cxx_sizeof_or_alignof_type (e, ALIGNOF_EXPR, false);
1729 /* If we reach this point, it means the alignas expression if of
1730 the form "alignas(assignment-expression)", so we should follow
1731 what is stated by [dcl.align]/2. */
1733 e = mark_rvalue_use (e);
1735 /* [dcl.align]/2 says:
1737 the assignment-expression shall be an integral constant
1738 expression. */
1740 e = fold_non_dependent_expr (e);
1741 if (value_dependent_expression_p (e))
1742 /* Leave value-dependent expression alone for now. */;
1743 else
1744 e = cxx_constant_value (e);
1746 if (e == NULL_TREE
1747 || e == error_mark_node
1748 || TREE_CODE (e) != INTEGER_CST)
1749 return error_mark_node;
1751 return e;
1755 /* EXPR is being used in a context that is not a function call.
1756 Enforce:
1758 [expr.ref]
1760 The expression can be used only as the left-hand operand of a
1761 member function call.
1763 [expr.mptr.operator]
1765 If the result of .* or ->* is a function, then that result can be
1766 used only as the operand for the function call operator ().
1768 by issuing an error message if appropriate. Returns true iff EXPR
1769 violates these rules. */
1771 bool
1772 invalid_nonstatic_memfn_p (const_tree expr, tsubst_flags_t complain)
1774 if (expr && DECL_NONSTATIC_MEMBER_FUNCTION_P (expr))
1776 if (complain & tf_error)
1777 error ("invalid use of non-static member function");
1778 return true;
1780 return false;
1783 /* If EXP is a reference to a bitfield, and the type of EXP does not
1784 match the declared type of the bitfield, return the declared type
1785 of the bitfield. Otherwise, return NULL_TREE. */
1787 tree
1788 is_bitfield_expr_with_lowered_type (const_tree exp)
1790 switch (TREE_CODE (exp))
1792 case COND_EXPR:
1793 if (!is_bitfield_expr_with_lowered_type (TREE_OPERAND (exp, 1)
1794 ? TREE_OPERAND (exp, 1)
1795 : TREE_OPERAND (exp, 0)))
1796 return NULL_TREE;
1797 return is_bitfield_expr_with_lowered_type (TREE_OPERAND (exp, 2));
1799 case COMPOUND_EXPR:
1800 return is_bitfield_expr_with_lowered_type (TREE_OPERAND (exp, 1));
1802 case MODIFY_EXPR:
1803 case SAVE_EXPR:
1804 return is_bitfield_expr_with_lowered_type (TREE_OPERAND (exp, 0));
1806 case COMPONENT_REF:
1808 tree field;
1810 field = TREE_OPERAND (exp, 1);
1811 if (TREE_CODE (field) != FIELD_DECL || !DECL_BIT_FIELD_TYPE (field))
1812 return NULL_TREE;
1813 if (same_type_ignoring_top_level_qualifiers_p
1814 (TREE_TYPE (exp), DECL_BIT_FIELD_TYPE (field)))
1815 return NULL_TREE;
1816 return DECL_BIT_FIELD_TYPE (field);
1819 CASE_CONVERT:
1820 if (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_OPERAND (exp, 0)))
1821 == TYPE_MAIN_VARIANT (TREE_TYPE (exp)))
1822 return is_bitfield_expr_with_lowered_type (TREE_OPERAND (exp, 0));
1823 /* Fallthrough. */
1825 default:
1826 return NULL_TREE;
1830 /* Like is_bitfield_with_lowered_type, except that if EXP is not a
1831 bitfield with a lowered type, the type of EXP is returned, rather
1832 than NULL_TREE. */
1834 tree
1835 unlowered_expr_type (const_tree exp)
1837 tree type;
1838 tree etype = TREE_TYPE (exp);
1840 type = is_bitfield_expr_with_lowered_type (exp);
1841 if (type)
1842 type = cp_build_qualified_type (type, cp_type_quals (etype));
1843 else
1844 type = etype;
1846 return type;
1849 /* Perform the conversions in [expr] that apply when an lvalue appears
1850 in an rvalue context: the lvalue-to-rvalue, array-to-pointer, and
1851 function-to-pointer conversions. In addition, manifest constants
1852 are replaced by their values, and bitfield references are converted
1853 to their declared types. Note that this function does not perform the
1854 lvalue-to-rvalue conversion for class types. If you need that conversion
1855 to for class types, then you probably need to use force_rvalue.
1857 Although the returned value is being used as an rvalue, this
1858 function does not wrap the returned expression in a
1859 NON_LVALUE_EXPR; the caller is expected to be mindful of the fact
1860 that the return value is no longer an lvalue. */
1862 tree
1863 decay_conversion (tree exp, tsubst_flags_t complain)
1865 tree type;
1866 enum tree_code code;
1867 location_t loc = EXPR_LOC_OR_HERE (exp);
1869 type = TREE_TYPE (exp);
1870 if (type == error_mark_node)
1871 return error_mark_node;
1873 exp = mark_rvalue_use (exp);
1875 exp = resolve_nondeduced_context (exp);
1876 if (type_unknown_p (exp))
1878 if (complain & tf_error)
1879 cxx_incomplete_type_error (exp, TREE_TYPE (exp));
1880 return error_mark_node;
1883 /* FIXME remove? at least need to remember that this isn't really a
1884 constant expression if EXP isn't decl_constant_var_p, like with
1885 C_MAYBE_CONST_EXPR. */
1886 exp = decl_constant_value_safe (exp);
1887 if (error_operand_p (exp))
1888 return error_mark_node;
1890 if (NULLPTR_TYPE_P (type) && !TREE_SIDE_EFFECTS (exp))
1891 return nullptr_node;
1893 /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
1894 Leave such NOP_EXPRs, since RHS is being used in non-lvalue context. */
1895 code = TREE_CODE (type);
1896 if (code == VOID_TYPE)
1898 if (complain & tf_error)
1899 error_at (loc, "void value not ignored as it ought to be");
1900 return error_mark_node;
1902 if (invalid_nonstatic_memfn_p (exp, complain))
1903 return error_mark_node;
1904 if (code == FUNCTION_TYPE || is_overloaded_fn (exp))
1905 return cp_build_addr_expr (exp, complain);
1906 if (code == ARRAY_TYPE)
1908 tree adr;
1909 tree ptrtype;
1911 if (TREE_CODE (exp) == INDIRECT_REF)
1912 return build_nop (build_pointer_type (TREE_TYPE (type)),
1913 TREE_OPERAND (exp, 0));
1915 if (TREE_CODE (exp) == COMPOUND_EXPR)
1917 tree op1 = decay_conversion (TREE_OPERAND (exp, 1), complain);
1918 if (op1 == error_mark_node)
1919 return error_mark_node;
1920 return build2 (COMPOUND_EXPR, TREE_TYPE (op1),
1921 TREE_OPERAND (exp, 0), op1);
1924 if (!lvalue_p (exp)
1925 && ! (TREE_CODE (exp) == CONSTRUCTOR && TREE_STATIC (exp)))
1927 if (complain & tf_error)
1928 error_at (loc, "invalid use of non-lvalue array");
1929 return error_mark_node;
1932 /* Don't let an array compound literal decay to a pointer. It can
1933 still be used to initialize an array or bind to a reference. */
1934 if (TREE_CODE (exp) == TARGET_EXPR)
1936 if (complain & tf_error)
1937 error_at (loc, "taking address of temporary array");
1938 return error_mark_node;
1941 ptrtype = build_pointer_type (TREE_TYPE (type));
1943 if (TREE_CODE (exp) == VAR_DECL)
1945 if (!cxx_mark_addressable (exp))
1946 return error_mark_node;
1947 adr = build_nop (ptrtype, build_address (exp));
1948 return adr;
1950 /* This way is better for a COMPONENT_REF since it can
1951 simplify the offset for a component. */
1952 adr = cp_build_addr_expr (exp, complain);
1953 return cp_convert (ptrtype, adr, complain);
1956 /* If a bitfield is used in a context where integral promotion
1957 applies, then the caller is expected to have used
1958 default_conversion. That function promotes bitfields correctly
1959 before calling this function. At this point, if we have a
1960 bitfield referenced, we may assume that is not subject to
1961 promotion, and that, therefore, the type of the resulting rvalue
1962 is the declared type of the bitfield. */
1963 exp = convert_bitfield_to_declared_type (exp);
1965 /* We do not call rvalue() here because we do not want to wrap EXP
1966 in a NON_LVALUE_EXPR. */
1968 /* [basic.lval]
1970 Non-class rvalues always have cv-unqualified types. */
1971 type = TREE_TYPE (exp);
1972 if (!CLASS_TYPE_P (type) && cv_qualified_p (type))
1973 exp = build_nop (cv_unqualified (type), exp);
1975 return exp;
1978 /* Perform preparatory conversions, as part of the "usual arithmetic
1979 conversions". In particular, as per [expr]:
1981 Whenever an lvalue expression appears as an operand of an
1982 operator that expects the rvalue for that operand, the
1983 lvalue-to-rvalue, array-to-pointer, or function-to-pointer
1984 standard conversions are applied to convert the expression to an
1985 rvalue.
1987 In addition, we perform integral promotions here, as those are
1988 applied to both operands to a binary operator before determining
1989 what additional conversions should apply. */
1991 static tree
1992 cp_default_conversion (tree exp, tsubst_flags_t complain)
1994 /* Check for target-specific promotions. */
1995 tree promoted_type = targetm.promoted_type (TREE_TYPE (exp));
1996 if (promoted_type)
1997 exp = cp_convert (promoted_type, exp, complain);
1998 /* Perform the integral promotions first so that bitfield
1999 expressions (which may promote to "int", even if the bitfield is
2000 declared "unsigned") are promoted correctly. */
2001 else if (INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (TREE_TYPE (exp)))
2002 exp = cp_perform_integral_promotions (exp, complain);
2003 /* Perform the other conversions. */
2004 exp = decay_conversion (exp, complain);
2006 return exp;
2009 /* C version. */
2011 tree
2012 default_conversion (tree exp)
2014 return cp_default_conversion (exp, tf_warning_or_error);
2017 /* EXPR is an expression with an integral or enumeration type.
2018 Perform the integral promotions in [conv.prom], and return the
2019 converted value. */
2021 tree
2022 cp_perform_integral_promotions (tree expr, tsubst_flags_t complain)
2024 tree type;
2025 tree promoted_type;
2027 expr = mark_rvalue_use (expr);
2029 /* [conv.prom]
2031 If the bitfield has an enumerated type, it is treated as any
2032 other value of that type for promotion purposes. */
2033 type = is_bitfield_expr_with_lowered_type (expr);
2034 if (!type || TREE_CODE (type) != ENUMERAL_TYPE)
2035 type = TREE_TYPE (expr);
2036 gcc_assert (INTEGRAL_OR_ENUMERATION_TYPE_P (type));
2037 /* Scoped enums don't promote. */
2038 if (SCOPED_ENUM_P (type))
2039 return expr;
2040 promoted_type = type_promotes_to (type);
2041 if (type != promoted_type)
2042 expr = cp_convert (promoted_type, expr, complain);
2043 return expr;
2046 /* C version. */
2048 tree
2049 perform_integral_promotions (tree expr)
2051 return cp_perform_integral_promotions (expr, tf_warning_or_error);
2054 /* Returns nonzero iff exp is a STRING_CST or the result of applying
2055 decay_conversion to one. */
2058 string_conv_p (const_tree totype, const_tree exp, int warn)
2060 tree t;
2062 if (TREE_CODE (totype) != POINTER_TYPE)
2063 return 0;
2065 t = TREE_TYPE (totype);
2066 if (!same_type_p (t, char_type_node)
2067 && !same_type_p (t, char16_type_node)
2068 && !same_type_p (t, char32_type_node)
2069 && !same_type_p (t, wchar_type_node))
2070 return 0;
2072 if (TREE_CODE (exp) == STRING_CST)
2074 /* Make sure that we don't try to convert between char and wide chars. */
2075 if (!same_type_p (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (exp))), t))
2076 return 0;
2078 else
2080 /* Is this a string constant which has decayed to 'const char *'? */
2081 t = build_pointer_type (cp_build_qualified_type (t, TYPE_QUAL_CONST));
2082 if (!same_type_p (TREE_TYPE (exp), t))
2083 return 0;
2084 STRIP_NOPS (exp);
2085 if (TREE_CODE (exp) != ADDR_EXPR
2086 || TREE_CODE (TREE_OPERAND (exp, 0)) != STRING_CST)
2087 return 0;
2090 /* This warning is not very useful, as it complains about printf. */
2091 if (warn)
2092 warning (OPT_Wwrite_strings,
2093 "deprecated conversion from string constant to %qT",
2094 totype);
2096 return 1;
2099 /* Given a COND_EXPR, MIN_EXPR, or MAX_EXPR in T, return it in a form that we
2100 can, for example, use as an lvalue. This code used to be in
2101 unary_complex_lvalue, but we needed it to deal with `a = (d == c) ? b : c'
2102 expressions, where we're dealing with aggregates. But now it's again only
2103 called from unary_complex_lvalue. The case (in particular) that led to
2104 this was with CODE == ADDR_EXPR, since it's not an lvalue when we'd
2105 get it there. */
2107 static tree
2108 rationalize_conditional_expr (enum tree_code code, tree t,
2109 tsubst_flags_t complain)
2111 /* For MIN_EXPR or MAX_EXPR, fold-const.c has arranged things so that
2112 the first operand is always the one to be used if both operands
2113 are equal, so we know what conditional expression this used to be. */
2114 if (TREE_CODE (t) == MIN_EXPR || TREE_CODE (t) == MAX_EXPR)
2116 tree op0 = TREE_OPERAND (t, 0);
2117 tree op1 = TREE_OPERAND (t, 1);
2119 /* The following code is incorrect if either operand side-effects. */
2120 gcc_assert (!TREE_SIDE_EFFECTS (op0)
2121 && !TREE_SIDE_EFFECTS (op1));
2122 return
2123 build_conditional_expr (build_x_binary_op (input_location,
2124 (TREE_CODE (t) == MIN_EXPR
2125 ? LE_EXPR : GE_EXPR),
2126 op0, TREE_CODE (op0),
2127 op1, TREE_CODE (op1),
2128 /*overload=*/NULL,
2129 complain),
2130 cp_build_unary_op (code, op0, 0, complain),
2131 cp_build_unary_op (code, op1, 0, complain),
2132 complain);
2135 return
2136 build_conditional_expr (TREE_OPERAND (t, 0),
2137 cp_build_unary_op (code, TREE_OPERAND (t, 1), 0,
2138 complain),
2139 cp_build_unary_op (code, TREE_OPERAND (t, 2), 0,
2140 complain),
2141 complain);
2144 /* Given the TYPE of an anonymous union field inside T, return the
2145 FIELD_DECL for the field. If not found return NULL_TREE. Because
2146 anonymous unions can nest, we must also search all anonymous unions
2147 that are directly reachable. */
2149 tree
2150 lookup_anon_field (tree t, tree type)
2152 tree field;
2154 for (field = TYPE_FIELDS (t); field; field = DECL_CHAIN (field))
2156 if (TREE_STATIC (field))
2157 continue;
2158 if (TREE_CODE (field) != FIELD_DECL || DECL_ARTIFICIAL (field))
2159 continue;
2161 /* If we find it directly, return the field. */
2162 if (DECL_NAME (field) == NULL_TREE
2163 && type == TYPE_MAIN_VARIANT (TREE_TYPE (field)))
2165 return field;
2168 /* Otherwise, it could be nested, search harder. */
2169 if (DECL_NAME (field) == NULL_TREE
2170 && ANON_AGGR_TYPE_P (TREE_TYPE (field)))
2172 tree subfield = lookup_anon_field (TREE_TYPE (field), type);
2173 if (subfield)
2174 return subfield;
2177 return NULL_TREE;
2180 /* Build an expression representing OBJECT.MEMBER. OBJECT is an
2181 expression; MEMBER is a DECL or baselink. If ACCESS_PATH is
2182 non-NULL, it indicates the path to the base used to name MEMBER.
2183 If PRESERVE_REFERENCE is true, the expression returned will have
2184 REFERENCE_TYPE if the MEMBER does. Otherwise, the expression
2185 returned will have the type referred to by the reference.
2187 This function does not perform access control; that is either done
2188 earlier by the parser when the name of MEMBER is resolved to MEMBER
2189 itself, or later when overload resolution selects one of the
2190 functions indicated by MEMBER. */
2192 tree
2193 build_class_member_access_expr (tree object, tree member,
2194 tree access_path, bool preserve_reference,
2195 tsubst_flags_t complain)
2197 tree object_type;
2198 tree member_scope;
2199 tree result = NULL_TREE;
2200 tree using_decl = NULL_TREE;
2202 if (error_operand_p (object) || error_operand_p (member))
2203 return error_mark_node;
2205 gcc_assert (DECL_P (member) || BASELINK_P (member));
2207 /* [expr.ref]
2209 The type of the first expression shall be "class object" (of a
2210 complete type). */
2211 object_type = TREE_TYPE (object);
2212 if (!currently_open_class (object_type)
2213 && !complete_type_or_maybe_complain (object_type, object, complain))
2214 return error_mark_node;
2215 if (!CLASS_TYPE_P (object_type))
2217 if (complain & tf_error)
2219 if (POINTER_TYPE_P (object_type)
2220 && CLASS_TYPE_P (TREE_TYPE (object_type)))
2221 error ("request for member %qD in %qE, which is of pointer "
2222 "type %qT (maybe you meant to use %<->%> ?)",
2223 member, object, object_type);
2224 else
2225 error ("request for member %qD in %qE, which is of non-class "
2226 "type %qT", member, object, object_type);
2228 return error_mark_node;
2231 /* The standard does not seem to actually say that MEMBER must be a
2232 member of OBJECT_TYPE. However, that is clearly what is
2233 intended. */
2234 if (DECL_P (member))
2236 member_scope = DECL_CLASS_CONTEXT (member);
2237 mark_used (member);
2238 if (TREE_DEPRECATED (member))
2239 warn_deprecated_use (member, NULL_TREE);
2241 else
2242 member_scope = BINFO_TYPE (BASELINK_ACCESS_BINFO (member));
2243 /* If MEMBER is from an anonymous aggregate, MEMBER_SCOPE will
2244 presently be the anonymous union. Go outwards until we find a
2245 type related to OBJECT_TYPE. */
2246 while ((ANON_AGGR_TYPE_P (member_scope) || UNSCOPED_ENUM_P (member_scope))
2247 && !same_type_ignoring_top_level_qualifiers_p (member_scope,
2248 object_type))
2249 member_scope = TYPE_CONTEXT (member_scope);
2250 if (!member_scope || !DERIVED_FROM_P (member_scope, object_type))
2252 if (complain & tf_error)
2254 if (TREE_CODE (member) == FIELD_DECL)
2255 error ("invalid use of nonstatic data member %qE", member);
2256 else
2257 error ("%qD is not a member of %qT", member, object_type);
2259 return error_mark_node;
2262 /* Transform `(a, b).x' into `(*(a, &b)).x', `(a ? b : c).x' into
2263 `(*(a ? &b : &c)).x', and so on. A COND_EXPR is only an lvalue
2264 in the front end; only _DECLs and _REFs are lvalues in the back end. */
2266 tree temp = unary_complex_lvalue (ADDR_EXPR, object);
2267 if (temp)
2268 object = cp_build_indirect_ref (temp, RO_NULL, complain);
2271 /* In [expr.ref], there is an explicit list of the valid choices for
2272 MEMBER. We check for each of those cases here. */
2273 if (TREE_CODE (member) == VAR_DECL)
2275 /* A static data member. */
2276 result = member;
2277 mark_exp_read (object);
2278 /* If OBJECT has side-effects, they are supposed to occur. */
2279 if (TREE_SIDE_EFFECTS (object))
2280 result = build2 (COMPOUND_EXPR, TREE_TYPE (result), object, result);
2282 else if (TREE_CODE (member) == FIELD_DECL)
2284 /* A non-static data member. */
2285 bool null_object_p;
2286 int type_quals;
2287 tree member_type;
2289 null_object_p = (TREE_CODE (object) == INDIRECT_REF
2290 && integer_zerop (TREE_OPERAND (object, 0)));
2292 /* Convert OBJECT to the type of MEMBER. */
2293 if (!same_type_p (TYPE_MAIN_VARIANT (object_type),
2294 TYPE_MAIN_VARIANT (member_scope)))
2296 tree binfo;
2297 base_kind kind;
2299 binfo = lookup_base (access_path ? access_path : object_type,
2300 member_scope, ba_unique, &kind, complain);
2301 if (binfo == error_mark_node)
2302 return error_mark_node;
2304 /* It is invalid to try to get to a virtual base of a
2305 NULL object. The most common cause is invalid use of
2306 offsetof macro. */
2307 if (null_object_p && kind == bk_via_virtual)
2309 if (complain & tf_error)
2311 error ("invalid access to non-static data member %qD of "
2312 "NULL object",
2313 member);
2314 error ("(perhaps the %<offsetof%> macro was used incorrectly)");
2316 return error_mark_node;
2319 /* Convert to the base. */
2320 object = build_base_path (PLUS_EXPR, object, binfo,
2321 /*nonnull=*/1, complain);
2322 /* If we found the base successfully then we should be able
2323 to convert to it successfully. */
2324 gcc_assert (object != error_mark_node);
2327 /* Complain about other invalid uses of offsetof, even though they will
2328 give the right answer. Note that we complain whether or not they
2329 actually used the offsetof macro, since there's no way to know at this
2330 point. So we just give a warning, instead of a pedwarn. */
2331 /* Do not produce this warning for base class field references, because
2332 we know for a fact that didn't come from offsetof. This does occur
2333 in various testsuite cases where a null object is passed where a
2334 vtable access is required. */
2335 if (null_object_p && warn_invalid_offsetof
2336 && CLASSTYPE_NON_STD_LAYOUT (object_type)
2337 && !DECL_FIELD_IS_BASE (member)
2338 && cp_unevaluated_operand == 0
2339 && (complain & tf_warning))
2341 warning (OPT_Winvalid_offsetof,
2342 "invalid access to non-static data member %qD "
2343 " of NULL object", member);
2344 warning (OPT_Winvalid_offsetof,
2345 "(perhaps the %<offsetof%> macro was used incorrectly)");
2348 /* If MEMBER is from an anonymous aggregate, we have converted
2349 OBJECT so that it refers to the class containing the
2350 anonymous union. Generate a reference to the anonymous union
2351 itself, and recur to find MEMBER. */
2352 if (ANON_AGGR_TYPE_P (DECL_CONTEXT (member))
2353 /* When this code is called from build_field_call, the
2354 object already has the type of the anonymous union.
2355 That is because the COMPONENT_REF was already
2356 constructed, and was then disassembled before calling
2357 build_field_call. After the function-call code is
2358 cleaned up, this waste can be eliminated. */
2359 && (!same_type_ignoring_top_level_qualifiers_p
2360 (TREE_TYPE (object), DECL_CONTEXT (member))))
2362 tree anonymous_union;
2364 anonymous_union = lookup_anon_field (TREE_TYPE (object),
2365 DECL_CONTEXT (member));
2366 object = build_class_member_access_expr (object,
2367 anonymous_union,
2368 /*access_path=*/NULL_TREE,
2369 preserve_reference,
2370 complain);
2373 /* Compute the type of the field, as described in [expr.ref]. */
2374 type_quals = TYPE_UNQUALIFIED;
2375 member_type = TREE_TYPE (member);
2376 if (TREE_CODE (member_type) != REFERENCE_TYPE)
2378 type_quals = (cp_type_quals (member_type)
2379 | cp_type_quals (object_type));
2381 /* A field is const (volatile) if the enclosing object, or the
2382 field itself, is const (volatile). But, a mutable field is
2383 not const, even within a const object. */
2384 if (DECL_MUTABLE_P (member))
2385 type_quals &= ~TYPE_QUAL_CONST;
2386 member_type = cp_build_qualified_type (member_type, type_quals);
2389 result = build3 (COMPONENT_REF, member_type, object, member,
2390 NULL_TREE);
2391 result = fold_if_not_in_template (result);
2393 /* Mark the expression const or volatile, as appropriate. Even
2394 though we've dealt with the type above, we still have to mark the
2395 expression itself. */
2396 if (type_quals & TYPE_QUAL_CONST)
2397 TREE_READONLY (result) = 1;
2398 if (type_quals & TYPE_QUAL_VOLATILE)
2399 TREE_THIS_VOLATILE (result) = 1;
2401 else if (BASELINK_P (member))
2403 /* The member is a (possibly overloaded) member function. */
2404 tree functions;
2405 tree type;
2407 /* If the MEMBER is exactly one static member function, then we
2408 know the type of the expression. Otherwise, we must wait
2409 until overload resolution has been performed. */
2410 functions = BASELINK_FUNCTIONS (member);
2411 if (TREE_CODE (functions) == FUNCTION_DECL
2412 && DECL_STATIC_FUNCTION_P (functions))
2413 type = TREE_TYPE (functions);
2414 else
2415 type = unknown_type_node;
2416 /* Note that we do not convert OBJECT to the BASELINK_BINFO
2417 base. That will happen when the function is called. */
2418 result = build3 (COMPONENT_REF, type, object, member, NULL_TREE);
2420 else if (TREE_CODE (member) == CONST_DECL)
2422 /* The member is an enumerator. */
2423 result = member;
2424 /* If OBJECT has side-effects, they are supposed to occur. */
2425 if (TREE_SIDE_EFFECTS (object))
2426 result = build2 (COMPOUND_EXPR, TREE_TYPE (result),
2427 object, result);
2429 else if ((using_decl = strip_using_decl (member)) != member)
2430 result = build_class_member_access_expr (object,
2431 using_decl,
2432 access_path, preserve_reference,
2433 complain);
2434 else
2436 if (complain & tf_error)
2437 error ("invalid use of %qD", member);
2438 return error_mark_node;
2441 if (!preserve_reference)
2442 /* [expr.ref]
2444 If E2 is declared to have type "reference to T", then ... the
2445 type of E1.E2 is T. */
2446 result = convert_from_reference (result);
2448 return result;
2451 /* Return the destructor denoted by OBJECT.SCOPE::DTOR_NAME, or, if
2452 SCOPE is NULL, by OBJECT.DTOR_NAME, where DTOR_NAME is ~type. */
2454 static tree
2455 lookup_destructor (tree object, tree scope, tree dtor_name)
2457 tree object_type = TREE_TYPE (object);
2458 tree dtor_type = TREE_OPERAND (dtor_name, 0);
2459 tree expr;
2461 if (scope && !check_dtor_name (scope, dtor_type))
2463 error ("qualified type %qT does not match destructor name ~%qT",
2464 scope, dtor_type);
2465 return error_mark_node;
2467 if (TREE_CODE (dtor_type) == IDENTIFIER_NODE)
2469 /* In a template, names we can't find a match for are still accepted
2470 destructor names, and we check them here. */
2471 if (check_dtor_name (object_type, dtor_type))
2472 dtor_type = object_type;
2473 else
2475 error ("object type %qT does not match destructor name ~%qT",
2476 object_type, dtor_type);
2477 return error_mark_node;
2481 else if (!DERIVED_FROM_P (dtor_type, TYPE_MAIN_VARIANT (object_type)))
2483 error ("the type being destroyed is %qT, but the destructor refers to %qT",
2484 TYPE_MAIN_VARIANT (object_type), dtor_type);
2485 return error_mark_node;
2487 expr = lookup_member (dtor_type, complete_dtor_identifier,
2488 /*protect=*/1, /*want_type=*/false,
2489 tf_warning_or_error);
2490 expr = (adjust_result_of_qualified_name_lookup
2491 (expr, dtor_type, object_type));
2492 if (scope == NULL_TREE)
2493 /* We need to call adjust_result_of_qualified_name_lookup in case the
2494 destructor names a base class, but we unset BASELINK_QUALIFIED_P so
2495 that we still get virtual function binding. */
2496 BASELINK_QUALIFIED_P (expr) = false;
2497 return expr;
2500 /* An expression of the form "A::template B" has been resolved to
2501 DECL. Issue a diagnostic if B is not a template or template
2502 specialization. */
2504 void
2505 check_template_keyword (tree decl)
2507 /* The standard says:
2509 [temp.names]
2511 If a name prefixed by the keyword template is not a member
2512 template, the program is ill-formed.
2514 DR 228 removed the restriction that the template be a member
2515 template.
2517 DR 96, if accepted would add the further restriction that explicit
2518 template arguments must be provided if the template keyword is
2519 used, but, as of 2005-10-16, that DR is still in "drafting". If
2520 this DR is accepted, then the semantic checks here can be
2521 simplified, as the entity named must in fact be a template
2522 specialization, rather than, as at present, a set of overloaded
2523 functions containing at least one template function. */
2524 if (TREE_CODE (decl) != TEMPLATE_DECL
2525 && TREE_CODE (decl) != TEMPLATE_ID_EXPR)
2527 if (!is_overloaded_fn (decl))
2528 permerror (input_location, "%qD is not a template", decl);
2529 else
2531 tree fns;
2532 fns = decl;
2533 if (BASELINK_P (fns))
2534 fns = BASELINK_FUNCTIONS (fns);
2535 while (fns)
2537 tree fn = OVL_CURRENT (fns);
2538 if (TREE_CODE (fn) == TEMPLATE_DECL
2539 || TREE_CODE (fn) == TEMPLATE_ID_EXPR)
2540 break;
2541 if (TREE_CODE (fn) == FUNCTION_DECL
2542 && DECL_USE_TEMPLATE (fn)
2543 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (fn)))
2544 break;
2545 fns = OVL_NEXT (fns);
2547 if (!fns)
2548 permerror (input_location, "%qD is not a template", decl);
2553 /* This function is called by the parser to process a class member
2554 access expression of the form OBJECT.NAME. NAME is a node used by
2555 the parser to represent a name; it is not yet a DECL. It may,
2556 however, be a BASELINK where the BASELINK_FUNCTIONS is a
2557 TEMPLATE_ID_EXPR. Templates must be looked up by the parser, and
2558 there is no reason to do the lookup twice, so the parser keeps the
2559 BASELINK. TEMPLATE_P is true iff NAME was explicitly declared to
2560 be a template via the use of the "A::template B" syntax. */
2562 tree
2563 finish_class_member_access_expr (tree object, tree name, bool template_p,
2564 tsubst_flags_t complain)
2566 tree expr;
2567 tree object_type;
2568 tree member;
2569 tree access_path = NULL_TREE;
2570 tree orig_object = object;
2571 tree orig_name = name;
2573 if (object == error_mark_node || name == error_mark_node)
2574 return error_mark_node;
2576 /* If OBJECT is an ObjC class instance, we must obey ObjC access rules. */
2577 if (!objc_is_public (object, name))
2578 return error_mark_node;
2580 object_type = TREE_TYPE (object);
2582 if (processing_template_decl)
2584 if (/* If OBJECT_TYPE is dependent, so is OBJECT.NAME. */
2585 dependent_type_p (object_type)
2586 /* If NAME is just an IDENTIFIER_NODE, then the expression
2587 is dependent. */
2588 || TREE_CODE (object) == IDENTIFIER_NODE
2589 /* If NAME is "f<args>", where either 'f' or 'args' is
2590 dependent, then the expression is dependent. */
2591 || (TREE_CODE (name) == TEMPLATE_ID_EXPR
2592 && dependent_template_id_p (TREE_OPERAND (name, 0),
2593 TREE_OPERAND (name, 1)))
2594 /* If NAME is "T::X" where "T" is dependent, then the
2595 expression is dependent. */
2596 || (TREE_CODE (name) == SCOPE_REF
2597 && TYPE_P (TREE_OPERAND (name, 0))
2598 && dependent_type_p (TREE_OPERAND (name, 0))))
2599 return build_min_nt_loc (UNKNOWN_LOCATION, COMPONENT_REF,
2600 object, name, NULL_TREE);
2601 object = build_non_dependent_expr (object);
2603 else if (c_dialect_objc ()
2604 && TREE_CODE (name) == IDENTIFIER_NODE
2605 && (expr = objc_maybe_build_component_ref (object, name)))
2606 return expr;
2608 /* [expr.ref]
2610 The type of the first expression shall be "class object" (of a
2611 complete type). */
2612 if (!currently_open_class (object_type)
2613 && !complete_type_or_maybe_complain (object_type, object, complain))
2614 return error_mark_node;
2615 if (!CLASS_TYPE_P (object_type))
2617 if (complain & tf_error)
2619 if (POINTER_TYPE_P (object_type)
2620 && CLASS_TYPE_P (TREE_TYPE (object_type)))
2621 error ("request for member %qD in %qE, which is of pointer "
2622 "type %qT (maybe you meant to use %<->%> ?)",
2623 name, object, object_type);
2624 else
2625 error ("request for member %qD in %qE, which is of non-class "
2626 "type %qT", name, object, object_type);
2628 return error_mark_node;
2631 if (BASELINK_P (name))
2632 /* A member function that has already been looked up. */
2633 member = name;
2634 else
2636 bool is_template_id = false;
2637 tree template_args = NULL_TREE;
2638 tree scope;
2640 if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
2642 is_template_id = true;
2643 template_args = TREE_OPERAND (name, 1);
2644 name = TREE_OPERAND (name, 0);
2646 if (TREE_CODE (name) == OVERLOAD)
2647 name = DECL_NAME (get_first_fn (name));
2648 else if (DECL_P (name))
2649 name = DECL_NAME (name);
2652 if (TREE_CODE (name) == SCOPE_REF)
2654 /* A qualified name. The qualifying class or namespace `S'
2655 has already been looked up; it is either a TYPE or a
2656 NAMESPACE_DECL. */
2657 scope = TREE_OPERAND (name, 0);
2658 name = TREE_OPERAND (name, 1);
2660 /* If SCOPE is a namespace, then the qualified name does not
2661 name a member of OBJECT_TYPE. */
2662 if (TREE_CODE (scope) == NAMESPACE_DECL)
2664 if (complain & tf_error)
2665 error ("%<%D::%D%> is not a member of %qT",
2666 scope, name, object_type);
2667 return error_mark_node;
2670 gcc_assert (CLASS_TYPE_P (scope));
2671 gcc_assert (TREE_CODE (name) == IDENTIFIER_NODE
2672 || TREE_CODE (name) == BIT_NOT_EXPR);
2674 if (constructor_name_p (name, scope))
2676 if (complain & tf_error)
2677 error ("cannot call constructor %<%T::%D%> directly",
2678 scope, name);
2679 return error_mark_node;
2682 /* Find the base of OBJECT_TYPE corresponding to SCOPE. */
2683 access_path = lookup_base (object_type, scope, ba_check,
2684 NULL, complain);
2685 if (access_path == error_mark_node)
2686 return error_mark_node;
2687 if (!access_path)
2689 if (complain & tf_error)
2690 error ("%qT is not a base of %qT", scope, object_type);
2691 return error_mark_node;
2694 else
2696 scope = NULL_TREE;
2697 access_path = object_type;
2700 if (TREE_CODE (name) == BIT_NOT_EXPR)
2701 member = lookup_destructor (object, scope, name);
2702 else
2704 /* Look up the member. */
2705 member = lookup_member (access_path, name, /*protect=*/1,
2706 /*want_type=*/false, complain);
2707 if (member == NULL_TREE)
2709 if (complain & tf_error)
2710 error ("%qD has no member named %qE",
2711 TREE_CODE (access_path) == TREE_BINFO
2712 ? TREE_TYPE (access_path) : object_type, name);
2713 return error_mark_node;
2715 if (member == error_mark_node)
2716 return error_mark_node;
2719 if (is_template_id)
2721 tree templ = member;
2723 if (BASELINK_P (templ))
2724 templ = lookup_template_function (templ, template_args);
2725 else
2727 if (complain & tf_error)
2728 error ("%qD is not a member template function", name);
2729 return error_mark_node;
2734 if (TREE_DEPRECATED (member))
2735 warn_deprecated_use (member, NULL_TREE);
2737 if (template_p)
2738 check_template_keyword (member);
2740 expr = build_class_member_access_expr (object, member, access_path,
2741 /*preserve_reference=*/false,
2742 complain);
2743 if (processing_template_decl && expr != error_mark_node)
2745 if (BASELINK_P (member))
2747 if (TREE_CODE (orig_name) == SCOPE_REF)
2748 BASELINK_QUALIFIED_P (member) = 1;
2749 orig_name = member;
2751 return build_min_non_dep (COMPONENT_REF, expr,
2752 orig_object, orig_name,
2753 NULL_TREE);
2756 return expr;
2759 /* Return an expression for the MEMBER_NAME field in the internal
2760 representation of PTRMEM, a pointer-to-member function. (Each
2761 pointer-to-member function type gets its own RECORD_TYPE so it is
2762 more convenient to access the fields by name than by FIELD_DECL.)
2763 This routine converts the NAME to a FIELD_DECL and then creates the
2764 node for the complete expression. */
2766 tree
2767 build_ptrmemfunc_access_expr (tree ptrmem, tree member_name)
2769 tree ptrmem_type;
2770 tree member;
2771 tree member_type;
2773 /* This code is a stripped down version of
2774 build_class_member_access_expr. It does not work to use that
2775 routine directly because it expects the object to be of class
2776 type. */
2777 ptrmem_type = TREE_TYPE (ptrmem);
2778 gcc_assert (TYPE_PTRMEMFUNC_P (ptrmem_type));
2779 member = lookup_member (ptrmem_type, member_name, /*protect=*/0,
2780 /*want_type=*/false, tf_warning_or_error);
2781 member_type = cp_build_qualified_type (TREE_TYPE (member),
2782 cp_type_quals (ptrmem_type));
2783 return fold_build3_loc (input_location,
2784 COMPONENT_REF, member_type,
2785 ptrmem, member, NULL_TREE);
2788 /* Given an expression PTR for a pointer, return an expression
2789 for the value pointed to.
2790 ERRORSTRING is the name of the operator to appear in error messages.
2792 This function may need to overload OPERATOR_FNNAME.
2793 Must also handle REFERENCE_TYPEs for C++. */
2795 tree
2796 build_x_indirect_ref (location_t loc, tree expr, ref_operator errorstring,
2797 tsubst_flags_t complain)
2799 tree orig_expr = expr;
2800 tree rval;
2802 if (processing_template_decl)
2804 /* Retain the type if we know the operand is a pointer. */
2805 if (TREE_TYPE (expr) && POINTER_TYPE_P (TREE_TYPE (expr)))
2806 return build_min (INDIRECT_REF, TREE_TYPE (TREE_TYPE (expr)), expr);
2807 if (type_dependent_expression_p (expr))
2808 return build_min_nt_loc (loc, INDIRECT_REF, expr);
2809 expr = build_non_dependent_expr (expr);
2812 rval = build_new_op (loc, INDIRECT_REF, LOOKUP_NORMAL, expr,
2813 NULL_TREE, NULL_TREE, /*overload=*/NULL, complain);
2814 if (!rval)
2815 rval = cp_build_indirect_ref (expr, errorstring, complain);
2817 if (processing_template_decl && rval != error_mark_node)
2818 return build_min_non_dep (INDIRECT_REF, rval, orig_expr);
2819 else
2820 return rval;
2823 /* Helper function called from c-common. */
2824 tree
2825 build_indirect_ref (location_t /*loc*/,
2826 tree ptr, ref_operator errorstring)
2828 return cp_build_indirect_ref (ptr, errorstring, tf_warning_or_error);
2831 tree
2832 cp_build_indirect_ref (tree ptr, ref_operator errorstring,
2833 tsubst_flags_t complain)
2835 tree pointer, type;
2837 if (ptr == current_class_ptr)
2838 return current_class_ref;
2840 pointer = (TREE_CODE (TREE_TYPE (ptr)) == REFERENCE_TYPE
2841 ? ptr : decay_conversion (ptr, complain));
2842 if (pointer == error_mark_node)
2843 return error_mark_node;
2845 type = TREE_TYPE (pointer);
2847 if (POINTER_TYPE_P (type))
2849 /* [expr.unary.op]
2851 If the type of the expression is "pointer to T," the type
2852 of the result is "T." */
2853 tree t = TREE_TYPE (type);
2855 if (CONVERT_EXPR_P (ptr)
2856 || TREE_CODE (ptr) == VIEW_CONVERT_EXPR)
2858 /* If a warning is issued, mark it to avoid duplicates from
2859 the backend. This only needs to be done at
2860 warn_strict_aliasing > 2. */
2861 if (warn_strict_aliasing > 2)
2862 if (strict_aliasing_warning (TREE_TYPE (TREE_OPERAND (ptr, 0)),
2863 type, TREE_OPERAND (ptr, 0)))
2864 TREE_NO_WARNING (ptr) = 1;
2867 if (VOID_TYPE_P (t))
2869 /* A pointer to incomplete type (other than cv void) can be
2870 dereferenced [expr.unary.op]/1 */
2871 if (complain & tf_error)
2872 error ("%qT is not a pointer-to-object type", type);
2873 return error_mark_node;
2875 else if (TREE_CODE (pointer) == ADDR_EXPR
2876 && same_type_p (t, TREE_TYPE (TREE_OPERAND (pointer, 0))))
2877 /* The POINTER was something like `&x'. We simplify `*&x' to
2878 `x'. */
2879 return TREE_OPERAND (pointer, 0);
2880 else
2882 tree ref = build1 (INDIRECT_REF, t, pointer);
2884 /* We *must* set TREE_READONLY when dereferencing a pointer to const,
2885 so that we get the proper error message if the result is used
2886 to assign to. Also, &* is supposed to be a no-op. */
2887 TREE_READONLY (ref) = CP_TYPE_CONST_P (t);
2888 TREE_THIS_VOLATILE (ref) = CP_TYPE_VOLATILE_P (t);
2889 TREE_SIDE_EFFECTS (ref)
2890 = (TREE_THIS_VOLATILE (ref) || TREE_SIDE_EFFECTS (pointer));
2891 return ref;
2894 else if (!(complain & tf_error))
2895 /* Don't emit any errors; we'll just return ERROR_MARK_NODE later. */
2897 /* `pointer' won't be an error_mark_node if we were given a
2898 pointer to member, so it's cool to check for this here. */
2899 else if (TYPE_PTRMEM_P (type))
2900 switch (errorstring)
2902 case RO_ARRAY_INDEXING:
2903 error ("invalid use of array indexing on pointer to member");
2904 break;
2905 case RO_UNARY_STAR:
2906 error ("invalid use of unary %<*%> on pointer to member");
2907 break;
2908 case RO_IMPLICIT_CONVERSION:
2909 error ("invalid use of implicit conversion on pointer to member");
2910 break;
2911 case RO_ARROW_STAR:
2912 error ("left hand operand of %<->*%> must be a pointer to class, "
2913 "but is a pointer to member of type %qT", type);
2914 break;
2915 default:
2916 gcc_unreachable ();
2918 else if (pointer != error_mark_node)
2919 invalid_indirection_error (input_location, type, errorstring);
2921 return error_mark_node;
2924 /* This handles expressions of the form "a[i]", which denotes
2925 an array reference.
2927 This is logically equivalent in C to *(a+i), but we may do it differently.
2928 If A is a variable or a member, we generate a primitive ARRAY_REF.
2929 This avoids forcing the array out of registers, and can work on
2930 arrays that are not lvalues (for example, members of structures returned
2931 by functions).
2933 If INDEX is of some user-defined type, it must be converted to
2934 integer type. Otherwise, to make a compatible PLUS_EXPR, it
2935 will inherit the type of the array, which will be some pointer type.
2937 LOC is the location to use in building the array reference. */
2939 tree
2940 cp_build_array_ref (location_t loc, tree array, tree idx,
2941 tsubst_flags_t complain)
2943 tree ret;
2945 if (idx == 0)
2947 if (complain & tf_error)
2948 error_at (loc, "subscript missing in array reference");
2949 return error_mark_node;
2952 if (TREE_TYPE (array) == error_mark_node
2953 || TREE_TYPE (idx) == error_mark_node)
2954 return error_mark_node;
2956 /* If ARRAY is a COMPOUND_EXPR or COND_EXPR, move our reference
2957 inside it. */
2958 switch (TREE_CODE (array))
2960 case COMPOUND_EXPR:
2962 tree value = cp_build_array_ref (loc, TREE_OPERAND (array, 1), idx,
2963 complain);
2964 ret = build2 (COMPOUND_EXPR, TREE_TYPE (value),
2965 TREE_OPERAND (array, 0), value);
2966 SET_EXPR_LOCATION (ret, loc);
2967 return ret;
2970 case COND_EXPR:
2971 ret = build_conditional_expr
2972 (TREE_OPERAND (array, 0),
2973 cp_build_array_ref (loc, TREE_OPERAND (array, 1), idx,
2974 complain),
2975 cp_build_array_ref (loc, TREE_OPERAND (array, 2), idx,
2976 complain),
2977 complain);
2978 protected_set_expr_location (ret, loc);
2979 return ret;
2981 default:
2982 break;
2985 convert_vector_to_pointer_for_subscript (loc, &array, idx);
2987 if (TREE_CODE (TREE_TYPE (array)) == ARRAY_TYPE)
2989 tree rval, type;
2991 warn_array_subscript_with_type_char (idx);
2993 if (!INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (TREE_TYPE (idx)))
2995 if (complain & tf_error)
2996 error_at (loc, "array subscript is not an integer");
2997 return error_mark_node;
3000 /* Apply integral promotions *after* noticing character types.
3001 (It is unclear why we do these promotions -- the standard
3002 does not say that we should. In fact, the natural thing would
3003 seem to be to convert IDX to ptrdiff_t; we're performing
3004 pointer arithmetic.) */
3005 idx = cp_perform_integral_promotions (idx, complain);
3007 /* An array that is indexed by a non-constant
3008 cannot be stored in a register; we must be able to do
3009 address arithmetic on its address.
3010 Likewise an array of elements of variable size. */
3011 if (TREE_CODE (idx) != INTEGER_CST
3012 || (COMPLETE_TYPE_P (TREE_TYPE (TREE_TYPE (array)))
3013 && (TREE_CODE (TYPE_SIZE (TREE_TYPE (TREE_TYPE (array))))
3014 != INTEGER_CST)))
3016 if (!cxx_mark_addressable (array))
3017 return error_mark_node;
3020 /* An array that is indexed by a constant value which is not within
3021 the array bounds cannot be stored in a register either; because we
3022 would get a crash in store_bit_field/extract_bit_field when trying
3023 to access a non-existent part of the register. */
3024 if (TREE_CODE (idx) == INTEGER_CST
3025 && TYPE_DOMAIN (TREE_TYPE (array))
3026 && ! int_fits_type_p (idx, TYPE_DOMAIN (TREE_TYPE (array))))
3028 if (!cxx_mark_addressable (array))
3029 return error_mark_node;
3032 if (!lvalue_p (array) && (complain & tf_error))
3033 pedwarn (loc, OPT_Wpedantic,
3034 "ISO C++ forbids subscripting non-lvalue array");
3036 /* Note in C++ it is valid to subscript a `register' array, since
3037 it is valid to take the address of something with that
3038 storage specification. */
3039 if (extra_warnings)
3041 tree foo = array;
3042 while (TREE_CODE (foo) == COMPONENT_REF)
3043 foo = TREE_OPERAND (foo, 0);
3044 if (TREE_CODE (foo) == VAR_DECL && DECL_REGISTER (foo)
3045 && (complain & tf_warning))
3046 warning_at (loc, OPT_Wextra,
3047 "subscripting array declared %<register%>");
3050 type = TREE_TYPE (TREE_TYPE (array));
3051 rval = build4 (ARRAY_REF, type, array, idx, NULL_TREE, NULL_TREE);
3052 /* Array ref is const/volatile if the array elements are
3053 or if the array is.. */
3054 TREE_READONLY (rval)
3055 |= (CP_TYPE_CONST_P (type) | TREE_READONLY (array));
3056 TREE_SIDE_EFFECTS (rval)
3057 |= (CP_TYPE_VOLATILE_P (type) | TREE_SIDE_EFFECTS (array));
3058 TREE_THIS_VOLATILE (rval)
3059 |= (CP_TYPE_VOLATILE_P (type) | TREE_THIS_VOLATILE (array));
3060 ret = require_complete_type_sfinae (fold_if_not_in_template (rval),
3061 complain);
3062 protected_set_expr_location (ret, loc);
3063 return ret;
3067 tree ar = cp_default_conversion (array, complain);
3068 tree ind = cp_default_conversion (idx, complain);
3070 /* Put the integer in IND to simplify error checking. */
3071 if (TREE_CODE (TREE_TYPE (ar)) == INTEGER_TYPE)
3073 tree temp = ar;
3074 ar = ind;
3075 ind = temp;
3078 if (ar == error_mark_node || ind == error_mark_node)
3079 return error_mark_node;
3081 if (TREE_CODE (TREE_TYPE (ar)) != POINTER_TYPE)
3083 if (complain & tf_error)
3084 error_at (loc, "subscripted value is neither array nor pointer");
3085 return error_mark_node;
3087 if (TREE_CODE (TREE_TYPE (ind)) != INTEGER_TYPE)
3089 if (complain & tf_error)
3090 error_at (loc, "array subscript is not an integer");
3091 return error_mark_node;
3094 warn_array_subscript_with_type_char (idx);
3096 ret = cp_build_indirect_ref (cp_build_binary_op (input_location,
3097 PLUS_EXPR, ar, ind,
3098 complain),
3099 RO_ARRAY_INDEXING,
3100 complain);
3101 protected_set_expr_location (ret, loc);
3102 return ret;
3106 /* Entry point for Obj-C++. */
3108 tree
3109 build_array_ref (location_t loc, tree array, tree idx)
3111 return cp_build_array_ref (loc, array, idx, tf_warning_or_error);
3114 /* Resolve a pointer to member function. INSTANCE is the object
3115 instance to use, if the member points to a virtual member.
3117 This used to avoid checking for virtual functions if basetype
3118 has no virtual functions, according to an earlier ANSI draft.
3119 With the final ISO C++ rules, such an optimization is
3120 incorrect: A pointer to a derived member can be static_cast
3121 to pointer-to-base-member, as long as the dynamic object
3122 later has the right member. */
3124 tree
3125 get_member_function_from_ptrfunc (tree *instance_ptrptr, tree function,
3126 tsubst_flags_t complain)
3128 if (TREE_CODE (function) == OFFSET_REF)
3129 function = TREE_OPERAND (function, 1);
3131 if (TYPE_PTRMEMFUNC_P (TREE_TYPE (function)))
3133 tree idx, delta, e1, e2, e3, vtbl, basetype;
3134 tree fntype = TYPE_PTRMEMFUNC_FN_TYPE (TREE_TYPE (function));
3136 tree instance_ptr = *instance_ptrptr;
3137 tree instance_save_expr = 0;
3138 if (instance_ptr == error_mark_node)
3140 if (TREE_CODE (function) == PTRMEM_CST)
3142 /* Extracting the function address from a pmf is only
3143 allowed with -Wno-pmf-conversions. It only works for
3144 pmf constants. */
3145 e1 = build_addr_func (PTRMEM_CST_MEMBER (function), complain);
3146 e1 = convert (fntype, e1);
3147 return e1;
3149 else
3151 if (complain & tf_error)
3152 error ("object missing in use of %qE", function);
3153 return error_mark_node;
3157 if (TREE_SIDE_EFFECTS (instance_ptr))
3158 instance_ptr = instance_save_expr = save_expr (instance_ptr);
3160 if (TREE_SIDE_EFFECTS (function))
3161 function = save_expr (function);
3163 /* Start by extracting all the information from the PMF itself. */
3164 e3 = pfn_from_ptrmemfunc (function);
3165 delta = delta_from_ptrmemfunc (function);
3166 idx = build1 (NOP_EXPR, vtable_index_type, e3);
3167 switch (TARGET_PTRMEMFUNC_VBIT_LOCATION)
3169 case ptrmemfunc_vbit_in_pfn:
3170 e1 = cp_build_binary_op (input_location,
3171 BIT_AND_EXPR, idx, integer_one_node,
3172 complain);
3173 idx = cp_build_binary_op (input_location,
3174 MINUS_EXPR, idx, integer_one_node,
3175 complain);
3176 if (idx == error_mark_node)
3177 return error_mark_node;
3178 break;
3180 case ptrmemfunc_vbit_in_delta:
3181 e1 = cp_build_binary_op (input_location,
3182 BIT_AND_EXPR, delta, integer_one_node,
3183 complain);
3184 delta = cp_build_binary_op (input_location,
3185 RSHIFT_EXPR, delta, integer_one_node,
3186 complain);
3187 if (delta == error_mark_node)
3188 return error_mark_node;
3189 break;
3191 default:
3192 gcc_unreachable ();
3195 if (e1 == error_mark_node)
3196 return error_mark_node;
3198 /* Convert down to the right base before using the instance. A
3199 special case is that in a pointer to member of class C, C may
3200 be incomplete. In that case, the function will of course be
3201 a member of C, and no conversion is required. In fact,
3202 lookup_base will fail in that case, because incomplete
3203 classes do not have BINFOs. */
3204 basetype = TYPE_METHOD_BASETYPE (TREE_TYPE (fntype));
3205 if (!same_type_ignoring_top_level_qualifiers_p
3206 (basetype, TREE_TYPE (TREE_TYPE (instance_ptr))))
3208 basetype = lookup_base (TREE_TYPE (TREE_TYPE (instance_ptr)),
3209 basetype, ba_check, NULL, complain);
3210 instance_ptr = build_base_path (PLUS_EXPR, instance_ptr, basetype,
3211 1, complain);
3212 if (instance_ptr == error_mark_node)
3213 return error_mark_node;
3215 /* ...and then the delta in the PMF. */
3216 instance_ptr = fold_build_pointer_plus (instance_ptr, delta);
3218 /* Hand back the adjusted 'this' argument to our caller. */
3219 *instance_ptrptr = instance_ptr;
3221 /* Next extract the vtable pointer from the object. */
3222 vtbl = build1 (NOP_EXPR, build_pointer_type (vtbl_ptr_type_node),
3223 instance_ptr);
3224 vtbl = cp_build_indirect_ref (vtbl, RO_NULL, complain);
3225 if (vtbl == error_mark_node)
3226 return error_mark_node;
3228 /* If the object is not dynamic the access invokes undefined
3229 behavior. As it is not executed in this case silence the
3230 spurious warnings it may provoke. */
3231 TREE_NO_WARNING (vtbl) = 1;
3233 /* Finally, extract the function pointer from the vtable. */
3234 e2 = fold_build_pointer_plus_loc (input_location, vtbl, idx);
3235 e2 = cp_build_indirect_ref (e2, RO_NULL, complain);
3236 if (e2 == error_mark_node)
3237 return error_mark_node;
3238 TREE_CONSTANT (e2) = 1;
3240 /* When using function descriptors, the address of the
3241 vtable entry is treated as a function pointer. */
3242 if (TARGET_VTABLE_USES_DESCRIPTORS)
3243 e2 = build1 (NOP_EXPR, TREE_TYPE (e2),
3244 cp_build_addr_expr (e2, complain));
3246 e2 = fold_convert (TREE_TYPE (e3), e2);
3247 e1 = build_conditional_expr (e1, e2, e3, complain);
3248 if (e1 == error_mark_node)
3249 return error_mark_node;
3251 /* Make sure this doesn't get evaluated first inside one of the
3252 branches of the COND_EXPR. */
3253 if (instance_save_expr)
3254 e1 = build2 (COMPOUND_EXPR, TREE_TYPE (e1),
3255 instance_save_expr, e1);
3257 function = e1;
3259 return function;
3262 /* Used by the C-common bits. */
3263 tree
3264 build_function_call (location_t /*loc*/,
3265 tree function, tree params)
3267 return cp_build_function_call (function, params, tf_warning_or_error);
3270 /* Used by the C-common bits. */
3271 tree
3272 build_function_call_vec (location_t /*loc*/,
3273 tree function, vec<tree, va_gc> *params,
3274 vec<tree, va_gc> * /*origtypes*/)
3276 vec<tree, va_gc> *orig_params = params;
3277 tree ret = cp_build_function_call_vec (function, &params,
3278 tf_warning_or_error);
3280 /* cp_build_function_call_vec can reallocate PARAMS by adding
3281 default arguments. That should never happen here. Verify
3282 that. */
3283 gcc_assert (params == orig_params);
3285 return ret;
3288 /* Build a function call using a tree list of arguments. */
3290 tree
3291 cp_build_function_call (tree function, tree params, tsubst_flags_t complain)
3293 vec<tree, va_gc> *vec;
3294 tree ret;
3296 vec = make_tree_vector ();
3297 for (; params != NULL_TREE; params = TREE_CHAIN (params))
3298 vec_safe_push (vec, TREE_VALUE (params));
3299 ret = cp_build_function_call_vec (function, &vec, complain);
3300 release_tree_vector (vec);
3301 return ret;
3304 /* Build a function call using varargs. */
3306 tree
3307 cp_build_function_call_nary (tree function, tsubst_flags_t complain, ...)
3309 vec<tree, va_gc> *vec;
3310 va_list args;
3311 tree ret, t;
3313 vec = make_tree_vector ();
3314 va_start (args, complain);
3315 for (t = va_arg (args, tree); t != NULL_TREE; t = va_arg (args, tree))
3316 vec_safe_push (vec, t);
3317 va_end (args);
3318 ret = cp_build_function_call_vec (function, &vec, complain);
3319 release_tree_vector (vec);
3320 return ret;
3323 /* Build a function call using a vector of arguments. PARAMS may be
3324 NULL if there are no parameters. This changes the contents of
3325 PARAMS. */
3327 tree
3328 cp_build_function_call_vec (tree function, vec<tree, va_gc> **params,
3329 tsubst_flags_t complain)
3331 tree fntype, fndecl;
3332 int is_method;
3333 tree original = function;
3334 int nargs;
3335 tree *argarray;
3336 tree parm_types;
3337 vec<tree, va_gc> *allocated = NULL;
3338 tree ret;
3340 /* For Objective-C, convert any calls via a cast to OBJC_TYPE_REF
3341 expressions, like those used for ObjC messenger dispatches. */
3342 if (params != NULL && !vec_safe_is_empty (*params))
3343 function = objc_rewrite_function_call (function, (**params)[0]);
3345 /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
3346 Strip such NOP_EXPRs, since FUNCTION is used in non-lvalue context. */
3347 if (TREE_CODE (function) == NOP_EXPR
3348 && TREE_TYPE (function) == TREE_TYPE (TREE_OPERAND (function, 0)))
3349 function = TREE_OPERAND (function, 0);
3351 if (TREE_CODE (function) == FUNCTION_DECL)
3353 mark_used (function);
3354 fndecl = function;
3356 /* Convert anything with function type to a pointer-to-function. */
3357 if (DECL_MAIN_P (function) && (complain & tf_error))
3358 pedwarn (input_location, OPT_Wpedantic,
3359 "ISO C++ forbids calling %<::main%> from within program");
3361 function = build_addr_func (function, complain);
3363 else
3365 fndecl = NULL_TREE;
3367 function = build_addr_func (function, complain);
3370 if (function == error_mark_node)
3371 return error_mark_node;
3373 fntype = TREE_TYPE (function);
3375 if (TYPE_PTRMEMFUNC_P (fntype))
3377 if (complain & tf_error)
3378 error ("must use %<.*%> or %<->*%> to call pointer-to-member "
3379 "function in %<%E (...)%>, e.g. %<(... ->* %E) (...)%>",
3380 original, original);
3381 return error_mark_node;
3384 is_method = (TREE_CODE (fntype) == POINTER_TYPE
3385 && TREE_CODE (TREE_TYPE (fntype)) == METHOD_TYPE);
3387 if (!((TREE_CODE (fntype) == POINTER_TYPE
3388 && TREE_CODE (TREE_TYPE (fntype)) == FUNCTION_TYPE)
3389 || is_method
3390 || TREE_CODE (function) == TEMPLATE_ID_EXPR))
3392 if (complain & tf_error)
3394 if (!flag_diagnostics_show_caret)
3395 error_at (input_location,
3396 "%qE cannot be used as a function", original);
3397 else if (DECL_P (original))
3398 error_at (input_location,
3399 "%qD cannot be used as a function", original);
3400 else
3401 error_at (input_location,
3402 "expression cannot be used as a function");
3405 return error_mark_node;
3408 /* fntype now gets the type of function pointed to. */
3409 fntype = TREE_TYPE (fntype);
3410 parm_types = TYPE_ARG_TYPES (fntype);
3412 if (params == NULL)
3414 allocated = make_tree_vector ();
3415 params = &allocated;
3418 nargs = convert_arguments (parm_types, params, fndecl, LOOKUP_NORMAL,
3419 complain);
3420 if (nargs < 0)
3421 return error_mark_node;
3423 argarray = (*params)->address ();
3425 /* Check for errors in format strings and inappropriately
3426 null parameters. */
3427 check_function_arguments (fntype, nargs, argarray);
3429 ret = build_cxx_call (function, nargs, argarray, complain);
3431 if (allocated != NULL)
3432 release_tree_vector (allocated);
3434 return ret;
3437 /* Subroutine of convert_arguments.
3438 Warn about wrong number of args are genereted. */
3440 static void
3441 warn_args_num (location_t loc, tree fndecl, bool too_many_p)
3443 if (fndecl)
3445 if (TREE_CODE (TREE_TYPE (fndecl)) == METHOD_TYPE)
3447 if (DECL_NAME (fndecl) == NULL_TREE
3448 || IDENTIFIER_HAS_TYPE_VALUE (DECL_NAME (fndecl)))
3449 error_at (loc,
3450 too_many_p
3451 ? G_("too many arguments to constructor %q#D")
3452 : G_("too few arguments to constructor %q#D"),
3453 fndecl);
3454 else
3455 error_at (loc,
3456 too_many_p
3457 ? G_("too many arguments to member function %q#D")
3458 : G_("too few arguments to member function %q#D"),
3459 fndecl);
3461 else
3462 error_at (loc,
3463 too_many_p
3464 ? G_("too many arguments to function %q#D")
3465 : G_("too few arguments to function %q#D"),
3466 fndecl);
3467 inform (DECL_SOURCE_LOCATION (fndecl),
3468 "declared here");
3470 else
3472 if (c_dialect_objc () && objc_message_selector ())
3473 error_at (loc,
3474 too_many_p
3475 ? G_("too many arguments to method %q#D")
3476 : G_("too few arguments to method %q#D"),
3477 objc_message_selector ());
3478 else
3479 error_at (loc, too_many_p ? G_("too many arguments to function")
3480 : G_("too few arguments to function"));
3484 /* Convert the actual parameter expressions in the list VALUES to the
3485 types in the list TYPELIST. The converted expressions are stored
3486 back in the VALUES vector.
3487 If parmdecls is exhausted, or when an element has NULL as its type,
3488 perform the default conversions.
3490 NAME is an IDENTIFIER_NODE or 0. It is used only for error messages.
3492 This is also where warnings about wrong number of args are generated.
3494 Returns the actual number of arguments processed (which might be less
3495 than the length of the vector), or -1 on error.
3497 In C++, unspecified trailing parameters can be filled in with their
3498 default arguments, if such were specified. Do so here. */
3500 static int
3501 convert_arguments (tree typelist, vec<tree, va_gc> **values, tree fndecl,
3502 int flags, tsubst_flags_t complain)
3504 tree typetail;
3505 unsigned int i;
3507 /* Argument passing is always copy-initialization. */
3508 flags |= LOOKUP_ONLYCONVERTING;
3510 for (i = 0, typetail = typelist;
3511 i < vec_safe_length (*values);
3512 i++)
3514 tree type = typetail ? TREE_VALUE (typetail) : 0;
3515 tree val = (**values)[i];
3517 if (val == error_mark_node || type == error_mark_node)
3518 return -1;
3520 if (type == void_type_node)
3522 if (complain & tf_error)
3524 warn_args_num (input_location, fndecl, /*too_many_p=*/true);
3525 return i;
3527 else
3528 return -1;
3531 /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
3532 Strip such NOP_EXPRs, since VAL is used in non-lvalue context. */
3533 if (TREE_CODE (val) == NOP_EXPR
3534 && TREE_TYPE (val) == TREE_TYPE (TREE_OPERAND (val, 0))
3535 && (type == 0 || TREE_CODE (type) != REFERENCE_TYPE))
3536 val = TREE_OPERAND (val, 0);
3538 if (type == 0 || TREE_CODE (type) != REFERENCE_TYPE)
3540 if (TREE_CODE (TREE_TYPE (val)) == ARRAY_TYPE
3541 || TREE_CODE (TREE_TYPE (val)) == FUNCTION_TYPE
3542 || TREE_CODE (TREE_TYPE (val)) == METHOD_TYPE)
3543 val = decay_conversion (val, complain);
3546 if (val == error_mark_node)
3547 return -1;
3549 if (type != 0)
3551 /* Formal parm type is specified by a function prototype. */
3552 tree parmval;
3554 if (!COMPLETE_TYPE_P (complete_type (type)))
3556 if (complain & tf_error)
3558 if (fndecl)
3559 error ("parameter %P of %qD has incomplete type %qT",
3560 i, fndecl, type);
3561 else
3562 error ("parameter %P has incomplete type %qT", i, type);
3564 parmval = error_mark_node;
3566 else
3568 parmval = convert_for_initialization
3569 (NULL_TREE, type, val, flags,
3570 ICR_ARGPASS, fndecl, i, complain);
3571 parmval = convert_for_arg_passing (type, parmval, complain);
3574 if (parmval == error_mark_node)
3575 return -1;
3577 (**values)[i] = parmval;
3579 else
3581 if (fndecl && DECL_BUILT_IN (fndecl)
3582 && DECL_FUNCTION_CODE (fndecl) == BUILT_IN_CONSTANT_P)
3583 /* Don't do ellipsis conversion for __built_in_constant_p
3584 as this will result in spurious errors for non-trivial
3585 types. */
3586 val = require_complete_type_sfinae (val, complain);
3587 else
3588 val = convert_arg_to_ellipsis (val, complain);
3590 (**values)[i] = val;
3593 if (typetail)
3594 typetail = TREE_CHAIN (typetail);
3597 if (typetail != 0 && typetail != void_list_node)
3599 /* See if there are default arguments that can be used. Because
3600 we hold default arguments in the FUNCTION_TYPE (which is so
3601 wrong), we can see default parameters here from deduced
3602 contexts (and via typeof) for indirect function calls.
3603 Fortunately we know whether we have a function decl to
3604 provide default arguments in a language conformant
3605 manner. */
3606 if (fndecl && TREE_PURPOSE (typetail)
3607 && TREE_CODE (TREE_PURPOSE (typetail)) != DEFAULT_ARG)
3609 for (; typetail != void_list_node; ++i)
3611 tree parmval
3612 = convert_default_arg (TREE_VALUE (typetail),
3613 TREE_PURPOSE (typetail),
3614 fndecl, i, complain);
3616 if (parmval == error_mark_node)
3617 return -1;
3619 vec_safe_push (*values, parmval);
3620 typetail = TREE_CHAIN (typetail);
3621 /* ends with `...'. */
3622 if (typetail == NULL_TREE)
3623 break;
3626 else
3628 if (complain & tf_error)
3629 warn_args_num (input_location, fndecl, /*too_many_p=*/false);
3630 return -1;
3634 return (int) i;
3637 /* Build a binary-operation expression, after performing default
3638 conversions on the operands. CODE is the kind of expression to
3639 build. ARG1 and ARG2 are the arguments. ARG1_CODE and ARG2_CODE
3640 are the tree codes which correspond to ARG1 and ARG2 when issuing
3641 warnings about possibly misplaced parentheses. They may differ
3642 from the TREE_CODE of ARG1 and ARG2 if the parser has done constant
3643 folding (e.g., if the parser sees "a | 1 + 1", it may call this
3644 routine with ARG2 being an INTEGER_CST and ARG2_CODE == PLUS_EXPR).
3645 To avoid issuing any parentheses warnings, pass ARG1_CODE and/or
3646 ARG2_CODE as ERROR_MARK. */
3648 tree
3649 build_x_binary_op (location_t loc, enum tree_code code, tree arg1,
3650 enum tree_code arg1_code, tree arg2,
3651 enum tree_code arg2_code, tree *overload,
3652 tsubst_flags_t complain)
3654 tree orig_arg1;
3655 tree orig_arg2;
3656 tree expr;
3658 orig_arg1 = arg1;
3659 orig_arg2 = arg2;
3661 if (processing_template_decl)
3663 if (type_dependent_expression_p (arg1)
3664 || type_dependent_expression_p (arg2))
3665 return build_min_nt_loc (loc, code, arg1, arg2);
3666 arg1 = build_non_dependent_expr (arg1);
3667 arg2 = build_non_dependent_expr (arg2);
3670 if (code == DOTSTAR_EXPR)
3671 expr = build_m_component_ref (arg1, arg2, complain);
3672 else
3673 expr = build_new_op (loc, code, LOOKUP_NORMAL, arg1, arg2, NULL_TREE,
3674 overload, complain);
3676 /* Check for cases such as x+y<<z which users are likely to
3677 misinterpret. But don't warn about obj << x + y, since that is a
3678 common idiom for I/O. */
3679 if (warn_parentheses
3680 && (complain & tf_warning)
3681 && !processing_template_decl
3682 && !error_operand_p (arg1)
3683 && !error_operand_p (arg2)
3684 && (code != LSHIFT_EXPR
3685 || !CLASS_TYPE_P (TREE_TYPE (arg1))))
3686 warn_about_parentheses (loc, code, arg1_code, orig_arg1,
3687 arg2_code, orig_arg2);
3689 if (processing_template_decl && expr != error_mark_node)
3690 return build_min_non_dep (code, expr, orig_arg1, orig_arg2);
3692 return expr;
3695 /* Build and return an ARRAY_REF expression. */
3697 tree
3698 build_x_array_ref (location_t loc, tree arg1, tree arg2,
3699 tsubst_flags_t complain)
3701 tree orig_arg1 = arg1;
3702 tree orig_arg2 = arg2;
3703 tree expr;
3705 if (processing_template_decl)
3707 if (type_dependent_expression_p (arg1)
3708 || type_dependent_expression_p (arg2))
3709 return build_min_nt_loc (loc, ARRAY_REF, arg1, arg2,
3710 NULL_TREE, NULL_TREE);
3711 arg1 = build_non_dependent_expr (arg1);
3712 arg2 = build_non_dependent_expr (arg2);
3715 expr = build_new_op (loc, ARRAY_REF, LOOKUP_NORMAL, arg1, arg2,
3716 NULL_TREE, /*overload=*/NULL, complain);
3718 if (processing_template_decl && expr != error_mark_node)
3719 return build_min_non_dep (ARRAY_REF, expr, orig_arg1, orig_arg2,
3720 NULL_TREE, NULL_TREE);
3721 return expr;
3724 /* Return whether OP is an expression of enum type cast to integer
3725 type. In C++ even unsigned enum types are cast to signed integer
3726 types. We do not want to issue warnings about comparisons between
3727 signed and unsigned types when one of the types is an enum type.
3728 Those warnings are always false positives in practice. */
3730 static bool
3731 enum_cast_to_int (tree op)
3733 if (TREE_CODE (op) == NOP_EXPR
3734 && TREE_TYPE (op) == integer_type_node
3735 && TREE_CODE (TREE_TYPE (TREE_OPERAND (op, 0))) == ENUMERAL_TYPE
3736 && TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (op, 0))))
3737 return true;
3739 /* The cast may have been pushed into a COND_EXPR. */
3740 if (TREE_CODE (op) == COND_EXPR)
3741 return (enum_cast_to_int (TREE_OPERAND (op, 1))
3742 || enum_cast_to_int (TREE_OPERAND (op, 2)));
3744 return false;
3747 /* For the c-common bits. */
3748 tree
3749 build_binary_op (location_t location, enum tree_code code, tree op0, tree op1,
3750 int /*convert_p*/)
3752 return cp_build_binary_op (location, code, op0, op1, tf_warning_or_error);
3756 /* Build a binary-operation expression without default conversions.
3757 CODE is the kind of expression to build.
3758 LOCATION is the location_t of the operator in the source code.
3759 This function differs from `build' in several ways:
3760 the data type of the result is computed and recorded in it,
3761 warnings are generated if arg data types are invalid,
3762 special handling for addition and subtraction of pointers is known,
3763 and some optimization is done (operations on narrow ints
3764 are done in the narrower type when that gives the same result).
3765 Constant folding is also done before the result is returned.
3767 Note that the operands will never have enumeral types
3768 because either they have just had the default conversions performed
3769 or they have both just been converted to some other type in which
3770 the arithmetic is to be done.
3772 C++: must do special pointer arithmetic when implementing
3773 multiple inheritance, and deal with pointer to member functions. */
3775 tree
3776 cp_build_binary_op (location_t location,
3777 enum tree_code code, tree orig_op0, tree orig_op1,
3778 tsubst_flags_t complain)
3780 tree op0, op1;
3781 enum tree_code code0, code1;
3782 tree type0, type1;
3783 const char *invalid_op_diag;
3785 /* Expression code to give to the expression when it is built.
3786 Normally this is CODE, which is what the caller asked for,
3787 but in some special cases we change it. */
3788 enum tree_code resultcode = code;
3790 /* Data type in which the computation is to be performed.
3791 In the simplest cases this is the common type of the arguments. */
3792 tree result_type = NULL;
3794 /* Nonzero means operands have already been type-converted
3795 in whatever way is necessary.
3796 Zero means they need to be converted to RESULT_TYPE. */
3797 int converted = 0;
3799 /* Nonzero means create the expression with this type, rather than
3800 RESULT_TYPE. */
3801 tree build_type = 0;
3803 /* Nonzero means after finally constructing the expression
3804 convert it to this type. */
3805 tree final_type = 0;
3807 tree result;
3809 /* Nonzero if this is an operation like MIN or MAX which can
3810 safely be computed in short if both args are promoted shorts.
3811 Also implies COMMON.
3812 -1 indicates a bitwise operation; this makes a difference
3813 in the exact conditions for when it is safe to do the operation
3814 in a narrower mode. */
3815 int shorten = 0;
3817 /* Nonzero if this is a comparison operation;
3818 if both args are promoted shorts, compare the original shorts.
3819 Also implies COMMON. */
3820 int short_compare = 0;
3822 /* Nonzero means set RESULT_TYPE to the common type of the args. */
3823 int common = 0;
3825 /* True if both operands have arithmetic type. */
3826 bool arithmetic_types_p;
3828 /* Apply default conversions. */
3829 op0 = orig_op0;
3830 op1 = orig_op1;
3832 if (code == TRUTH_AND_EXPR || code == TRUTH_ANDIF_EXPR
3833 || code == TRUTH_OR_EXPR || code == TRUTH_ORIF_EXPR
3834 || code == TRUTH_XOR_EXPR)
3836 if (!really_overloaded_fn (op0) && !VOID_TYPE_P (TREE_TYPE (op0)))
3837 op0 = decay_conversion (op0, complain);
3838 if (!really_overloaded_fn (op1) && !VOID_TYPE_P (TREE_TYPE (op1)))
3839 op1 = decay_conversion (op1, complain);
3841 else
3843 if (!really_overloaded_fn (op0) && !VOID_TYPE_P (TREE_TYPE (op0)))
3844 op0 = cp_default_conversion (op0, complain);
3845 if (!really_overloaded_fn (op1) && !VOID_TYPE_P (TREE_TYPE (op1)))
3846 op1 = cp_default_conversion (op1, complain);
3849 /* Strip NON_LVALUE_EXPRs, etc., since we aren't using as an lvalue. */
3850 STRIP_TYPE_NOPS (op0);
3851 STRIP_TYPE_NOPS (op1);
3853 /* DTRT if one side is an overloaded function, but complain about it. */
3854 if (type_unknown_p (op0))
3856 tree t = instantiate_type (TREE_TYPE (op1), op0, tf_none);
3857 if (t != error_mark_node)
3859 if (complain & tf_error)
3860 permerror (input_location, "assuming cast to type %qT from overloaded function",
3861 TREE_TYPE (t));
3862 op0 = t;
3865 if (type_unknown_p (op1))
3867 tree t = instantiate_type (TREE_TYPE (op0), op1, tf_none);
3868 if (t != error_mark_node)
3870 if (complain & tf_error)
3871 permerror (input_location, "assuming cast to type %qT from overloaded function",
3872 TREE_TYPE (t));
3873 op1 = t;
3877 type0 = TREE_TYPE (op0);
3878 type1 = TREE_TYPE (op1);
3880 /* The expression codes of the data types of the arguments tell us
3881 whether the arguments are integers, floating, pointers, etc. */
3882 code0 = TREE_CODE (type0);
3883 code1 = TREE_CODE (type1);
3885 /* If an error was already reported for one of the arguments,
3886 avoid reporting another error. */
3887 if (code0 == ERROR_MARK || code1 == ERROR_MARK)
3888 return error_mark_node;
3890 if ((invalid_op_diag
3891 = targetm.invalid_binary_op (code, type0, type1)))
3893 error (invalid_op_diag);
3894 return error_mark_node;
3897 /* Issue warnings about peculiar, but valid, uses of NULL. */
3898 if ((orig_op0 == null_node || orig_op1 == null_node)
3899 /* It's reasonable to use pointer values as operands of &&
3900 and ||, so NULL is no exception. */
3901 && code != TRUTH_ANDIF_EXPR && code != TRUTH_ORIF_EXPR
3902 && ( /* Both are NULL (or 0) and the operation was not a
3903 comparison or a pointer subtraction. */
3904 (null_ptr_cst_p (orig_op0) && null_ptr_cst_p (orig_op1)
3905 && code != EQ_EXPR && code != NE_EXPR && code != MINUS_EXPR)
3906 /* Or if one of OP0 or OP1 is neither a pointer nor NULL. */
3907 || (!null_ptr_cst_p (orig_op0)
3908 && !TYPE_PTR_OR_PTRMEM_P (type0))
3909 || (!null_ptr_cst_p (orig_op1)
3910 && !TYPE_PTR_OR_PTRMEM_P (type1)))
3911 && (complain & tf_warning))
3913 source_location loc =
3914 expansion_point_location_if_in_system_header (input_location);
3916 warning_at (loc, OPT_Wpointer_arith, "NULL used in arithmetic");
3919 /* In case when one of the operands of the binary operation is
3920 a vector and another is a scalar -- convert scalar to vector. */
3921 if ((code0 == VECTOR_TYPE) != (code1 == VECTOR_TYPE))
3923 enum stv_conv convert_flag = scalar_to_vector (location, code, op0, op1,
3924 complain & tf_error);
3926 switch (convert_flag)
3928 case stv_error:
3929 return error_mark_node;
3930 case stv_firstarg:
3932 op0 = convert (TREE_TYPE (type1), op0);
3933 op0 = build_vector_from_val (type1, op0);
3934 type0 = TREE_TYPE (op0);
3935 code0 = TREE_CODE (type0);
3936 converted = 1;
3937 break;
3939 case stv_secondarg:
3941 op1 = convert (TREE_TYPE (type0), op1);
3942 op1 = build_vector_from_val (type0, op1);
3943 type1 = TREE_TYPE (op1);
3944 code1 = TREE_CODE (type1);
3945 converted = 1;
3946 break;
3948 default:
3949 break;
3953 switch (code)
3955 case MINUS_EXPR:
3956 /* Subtraction of two similar pointers.
3957 We must subtract them as integers, then divide by object size. */
3958 if (code0 == POINTER_TYPE && code1 == POINTER_TYPE
3959 && same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (type0),
3960 TREE_TYPE (type1)))
3961 return pointer_diff (op0, op1, common_pointer_type (type0, type1),
3962 complain);
3963 /* In all other cases except pointer - int, the usual arithmetic
3964 rules apply. */
3965 else if (!(code0 == POINTER_TYPE && code1 == INTEGER_TYPE))
3967 common = 1;
3968 break;
3970 /* The pointer - int case is just like pointer + int; fall
3971 through. */
3972 case PLUS_EXPR:
3973 if ((code0 == POINTER_TYPE || code1 == POINTER_TYPE)
3974 && (code0 == INTEGER_TYPE || code1 == INTEGER_TYPE))
3976 tree ptr_operand;
3977 tree int_operand;
3978 ptr_operand = ((code0 == POINTER_TYPE) ? op0 : op1);
3979 int_operand = ((code0 == INTEGER_TYPE) ? op0 : op1);
3980 if (processing_template_decl)
3982 result_type = TREE_TYPE (ptr_operand);
3983 break;
3985 return cp_pointer_int_sum (code,
3986 ptr_operand,
3987 int_operand);
3989 common = 1;
3990 break;
3992 case MULT_EXPR:
3993 common = 1;
3994 break;
3996 case TRUNC_DIV_EXPR:
3997 case CEIL_DIV_EXPR:
3998 case FLOOR_DIV_EXPR:
3999 case ROUND_DIV_EXPR:
4000 case EXACT_DIV_EXPR:
4001 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE
4002 || code0 == COMPLEX_TYPE || code0 == VECTOR_TYPE)
4003 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
4004 || code1 == COMPLEX_TYPE || code1 == VECTOR_TYPE))
4006 enum tree_code tcode0 = code0, tcode1 = code1;
4008 warn_for_div_by_zero (location, op1);
4010 if (tcode0 == COMPLEX_TYPE || tcode0 == VECTOR_TYPE)
4011 tcode0 = TREE_CODE (TREE_TYPE (TREE_TYPE (op0)));
4012 if (tcode1 == COMPLEX_TYPE || tcode1 == VECTOR_TYPE)
4013 tcode1 = TREE_CODE (TREE_TYPE (TREE_TYPE (op1)));
4015 if (!(tcode0 == INTEGER_TYPE && tcode1 == INTEGER_TYPE))
4016 resultcode = RDIV_EXPR;
4017 else
4018 /* When dividing two signed integers, we have to promote to int.
4019 unless we divide by a constant != -1. Note that default
4020 conversion will have been performed on the operands at this
4021 point, so we have to dig out the original type to find out if
4022 it was unsigned. */
4023 shorten = ((TREE_CODE (op0) == NOP_EXPR
4024 && TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (op0, 0))))
4025 || (TREE_CODE (op1) == INTEGER_CST
4026 && ! integer_all_onesp (op1)));
4028 common = 1;
4030 break;
4032 case BIT_AND_EXPR:
4033 case BIT_IOR_EXPR:
4034 case BIT_XOR_EXPR:
4035 if ((code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
4036 || (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE
4037 && !VECTOR_FLOAT_TYPE_P (type0)
4038 && !VECTOR_FLOAT_TYPE_P (type1)))
4039 shorten = -1;
4040 break;
4042 case TRUNC_MOD_EXPR:
4043 case FLOOR_MOD_EXPR:
4044 warn_for_div_by_zero (location, op1);
4046 if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE
4047 && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE
4048 && TREE_CODE (TREE_TYPE (type1)) == INTEGER_TYPE)
4049 common = 1;
4050 else if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
4052 /* Although it would be tempting to shorten always here, that loses
4053 on some targets, since the modulo instruction is undefined if the
4054 quotient can't be represented in the computation mode. We shorten
4055 only if unsigned or if dividing by something we know != -1. */
4056 shorten = ((TREE_CODE (op0) == NOP_EXPR
4057 && TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (op0, 0))))
4058 || (TREE_CODE (op1) == INTEGER_CST
4059 && ! integer_all_onesp (op1)));
4060 common = 1;
4062 break;
4064 case TRUTH_ANDIF_EXPR:
4065 case TRUTH_ORIF_EXPR:
4066 case TRUTH_AND_EXPR:
4067 case TRUTH_OR_EXPR:
4068 result_type = boolean_type_node;
4069 break;
4071 /* Shift operations: result has same type as first operand;
4072 always convert second operand to int.
4073 Also set SHORT_SHIFT if shifting rightward. */
4075 case RSHIFT_EXPR:
4076 if (code0 == VECTOR_TYPE && code1 == INTEGER_TYPE
4077 && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE)
4079 result_type = type0;
4080 converted = 1;
4082 else if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE
4083 && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE
4084 && TREE_CODE (TREE_TYPE (type1)) == INTEGER_TYPE
4085 && TYPE_VECTOR_SUBPARTS (type0) == TYPE_VECTOR_SUBPARTS (type1))
4087 result_type = type0;
4088 converted = 1;
4090 else if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
4092 result_type = type0;
4093 if (TREE_CODE (op1) == INTEGER_CST)
4095 if (tree_int_cst_lt (op1, integer_zero_node))
4097 if ((complain & tf_warning)
4098 && c_inhibit_evaluation_warnings == 0)
4099 warning (0, "right shift count is negative");
4101 else
4103 if (compare_tree_int (op1, TYPE_PRECISION (type0)) >= 0
4104 && (complain & tf_warning)
4105 && c_inhibit_evaluation_warnings == 0)
4106 warning (0, "right shift count >= width of type");
4109 /* Convert the shift-count to an integer, regardless of
4110 size of value being shifted. */
4111 if (TYPE_MAIN_VARIANT (TREE_TYPE (op1)) != integer_type_node)
4112 op1 = cp_convert (integer_type_node, op1, complain);
4113 /* Avoid converting op1 to result_type later. */
4114 converted = 1;
4116 break;
4118 case LSHIFT_EXPR:
4119 if (code0 == VECTOR_TYPE && code1 == INTEGER_TYPE
4120 && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE)
4122 result_type = type0;
4123 converted = 1;
4125 else if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE
4126 && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE
4127 && TREE_CODE (TREE_TYPE (type1)) == INTEGER_TYPE
4128 && TYPE_VECTOR_SUBPARTS (type0) == TYPE_VECTOR_SUBPARTS (type1))
4130 result_type = type0;
4131 converted = 1;
4133 else if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
4135 result_type = type0;
4136 if (TREE_CODE (op1) == INTEGER_CST)
4138 if (tree_int_cst_lt (op1, integer_zero_node))
4140 if ((complain & tf_warning)
4141 && c_inhibit_evaluation_warnings == 0)
4142 warning (0, "left shift count is negative");
4144 else if (compare_tree_int (op1, TYPE_PRECISION (type0)) >= 0)
4146 if ((complain & tf_warning)
4147 && c_inhibit_evaluation_warnings == 0)
4148 warning (0, "left shift count >= width of type");
4151 /* Convert the shift-count to an integer, regardless of
4152 size of value being shifted. */
4153 if (TYPE_MAIN_VARIANT (TREE_TYPE (op1)) != integer_type_node)
4154 op1 = cp_convert (integer_type_node, op1, complain);
4155 /* Avoid converting op1 to result_type later. */
4156 converted = 1;
4158 break;
4160 case RROTATE_EXPR:
4161 case LROTATE_EXPR:
4162 if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
4164 result_type = type0;
4165 if (TREE_CODE (op1) == INTEGER_CST)
4167 if (tree_int_cst_lt (op1, integer_zero_node))
4169 if (complain & tf_warning)
4170 warning (0, (code == LROTATE_EXPR)
4171 ? G_("left rotate count is negative")
4172 : G_("right rotate count is negative"));
4174 else if (compare_tree_int (op1, TYPE_PRECISION (type0)) >= 0)
4176 if (complain & tf_warning)
4177 warning (0, (code == LROTATE_EXPR)
4178 ? G_("left rotate count >= width of type")
4179 : G_("right rotate count >= width of type"));
4182 /* Convert the shift-count to an integer, regardless of
4183 size of value being shifted. */
4184 if (TYPE_MAIN_VARIANT (TREE_TYPE (op1)) != integer_type_node)
4185 op1 = cp_convert (integer_type_node, op1, complain);
4187 break;
4189 case EQ_EXPR:
4190 case NE_EXPR:
4191 if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE)
4192 goto vector_compare;
4193 if ((complain & tf_warning)
4194 && (FLOAT_TYPE_P (type0) || FLOAT_TYPE_P (type1)))
4195 warning (OPT_Wfloat_equal,
4196 "comparing floating point with == or != is unsafe");
4197 if ((complain & tf_warning)
4198 && ((TREE_CODE (orig_op0) == STRING_CST && !integer_zerop (op1))
4199 || (TREE_CODE (orig_op1) == STRING_CST && !integer_zerop (op0))))
4200 warning (OPT_Waddress, "comparison with string literal results in unspecified behaviour");
4202 build_type = boolean_type_node;
4203 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE
4204 || code0 == COMPLEX_TYPE || code0 == ENUMERAL_TYPE)
4205 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
4206 || code1 == COMPLEX_TYPE || code1 == ENUMERAL_TYPE))
4207 short_compare = 1;
4208 else if ((code0 == POINTER_TYPE && code1 == POINTER_TYPE)
4209 || (TYPE_PTRDATAMEM_P (type0) && TYPE_PTRDATAMEM_P (type1)))
4210 result_type = composite_pointer_type (type0, type1, op0, op1,
4211 CPO_COMPARISON, complain);
4212 else if ((code0 == POINTER_TYPE || TYPE_PTRDATAMEM_P (type0))
4213 && null_ptr_cst_p (op1))
4215 if (TREE_CODE (op0) == ADDR_EXPR
4216 && decl_with_nonnull_addr_p (TREE_OPERAND (op0, 0)))
4218 if ((complain & tf_warning)
4219 && c_inhibit_evaluation_warnings == 0)
4220 warning (OPT_Waddress, "the address of %qD will never be NULL",
4221 TREE_OPERAND (op0, 0));
4223 result_type = type0;
4225 else if ((code1 == POINTER_TYPE || TYPE_PTRDATAMEM_P (type1))
4226 && null_ptr_cst_p (op0))
4228 if (TREE_CODE (op1) == ADDR_EXPR
4229 && decl_with_nonnull_addr_p (TREE_OPERAND (op1, 0)))
4231 if ((complain & tf_warning)
4232 && c_inhibit_evaluation_warnings == 0)
4233 warning (OPT_Waddress, "the address of %qD will never be NULL",
4234 TREE_OPERAND (op1, 0));
4236 result_type = type1;
4238 else if (null_ptr_cst_p (op0) && null_ptr_cst_p (op1))
4239 /* One of the operands must be of nullptr_t type. */
4240 result_type = TREE_TYPE (nullptr_node);
4241 else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
4243 result_type = type0;
4244 if (complain & tf_error)
4245 permerror (input_location, "ISO C++ forbids comparison between pointer and integer");
4246 else
4247 return error_mark_node;
4249 else if (code0 == INTEGER_TYPE && code1 == POINTER_TYPE)
4251 result_type = type1;
4252 if (complain & tf_error)
4253 permerror (input_location, "ISO C++ forbids comparison between pointer and integer");
4254 else
4255 return error_mark_node;
4257 else if (TYPE_PTRMEMFUNC_P (type0) && null_ptr_cst_p (op1))
4259 if (TARGET_PTRMEMFUNC_VBIT_LOCATION
4260 == ptrmemfunc_vbit_in_delta)
4262 tree pfn0, delta0, e1, e2;
4264 if (TREE_SIDE_EFFECTS (op0))
4265 op0 = save_expr (op0);
4267 pfn0 = pfn_from_ptrmemfunc (op0);
4268 delta0 = delta_from_ptrmemfunc (op0);
4269 e1 = cp_build_binary_op (location,
4270 EQ_EXPR,
4271 pfn0,
4272 build_zero_cst (TREE_TYPE (pfn0)),
4273 complain);
4274 e2 = cp_build_binary_op (location,
4275 BIT_AND_EXPR,
4276 delta0,
4277 integer_one_node,
4278 complain);
4280 if ((complain & tf_warning)
4281 && c_inhibit_evaluation_warnings == 0
4282 && !NULLPTR_TYPE_P (TREE_TYPE (op1)))
4283 warning (OPT_Wzero_as_null_pointer_constant,
4284 "zero as null pointer constant");
4286 e2 = cp_build_binary_op (location,
4287 EQ_EXPR, e2, integer_zero_node,
4288 complain);
4289 op0 = cp_build_binary_op (location,
4290 TRUTH_ANDIF_EXPR, e1, e2,
4291 complain);
4292 op1 = cp_convert (TREE_TYPE (op0), integer_one_node, complain);
4294 else
4296 op0 = build_ptrmemfunc_access_expr (op0, pfn_identifier);
4297 op1 = cp_convert (TREE_TYPE (op0), op1, complain);
4299 result_type = TREE_TYPE (op0);
4301 else if (TYPE_PTRMEMFUNC_P (type1) && null_ptr_cst_p (op0))
4302 return cp_build_binary_op (location, code, op1, op0, complain);
4303 else if (TYPE_PTRMEMFUNC_P (type0) && TYPE_PTRMEMFUNC_P (type1))
4305 tree type;
4306 /* E will be the final comparison. */
4307 tree e;
4308 /* E1 and E2 are for scratch. */
4309 tree e1;
4310 tree e2;
4311 tree pfn0;
4312 tree pfn1;
4313 tree delta0;
4314 tree delta1;
4316 type = composite_pointer_type (type0, type1, op0, op1,
4317 CPO_COMPARISON, complain);
4319 if (!same_type_p (TREE_TYPE (op0), type))
4320 op0 = cp_convert_and_check (type, op0, complain);
4321 if (!same_type_p (TREE_TYPE (op1), type))
4322 op1 = cp_convert_and_check (type, op1, complain);
4324 if (op0 == error_mark_node || op1 == error_mark_node)
4325 return error_mark_node;
4327 if (TREE_SIDE_EFFECTS (op0))
4328 op0 = save_expr (op0);
4329 if (TREE_SIDE_EFFECTS (op1))
4330 op1 = save_expr (op1);
4332 pfn0 = pfn_from_ptrmemfunc (op0);
4333 pfn1 = pfn_from_ptrmemfunc (op1);
4334 delta0 = delta_from_ptrmemfunc (op0);
4335 delta1 = delta_from_ptrmemfunc (op1);
4336 if (TARGET_PTRMEMFUNC_VBIT_LOCATION
4337 == ptrmemfunc_vbit_in_delta)
4339 /* We generate:
4341 (op0.pfn == op1.pfn
4342 && ((op0.delta == op1.delta)
4343 || (!op0.pfn && op0.delta & 1 == 0
4344 && op1.delta & 1 == 0))
4346 The reason for the `!op0.pfn' bit is that a NULL
4347 pointer-to-member is any member with a zero PFN and
4348 LSB of the DELTA field is 0. */
4350 e1 = cp_build_binary_op (location, BIT_AND_EXPR,
4351 delta0,
4352 integer_one_node,
4353 complain);
4354 e1 = cp_build_binary_op (location,
4355 EQ_EXPR, e1, integer_zero_node,
4356 complain);
4357 e2 = cp_build_binary_op (location, BIT_AND_EXPR,
4358 delta1,
4359 integer_one_node,
4360 complain);
4361 e2 = cp_build_binary_op (location,
4362 EQ_EXPR, e2, integer_zero_node,
4363 complain);
4364 e1 = cp_build_binary_op (location,
4365 TRUTH_ANDIF_EXPR, e2, e1,
4366 complain);
4367 e2 = cp_build_binary_op (location, EQ_EXPR,
4368 pfn0,
4369 build_zero_cst (TREE_TYPE (pfn0)),
4370 complain);
4371 e2 = cp_build_binary_op (location,
4372 TRUTH_ANDIF_EXPR, e2, e1, complain);
4373 e1 = cp_build_binary_op (location,
4374 EQ_EXPR, delta0, delta1, complain);
4375 e1 = cp_build_binary_op (location,
4376 TRUTH_ORIF_EXPR, e1, e2, complain);
4378 else
4380 /* We generate:
4382 (op0.pfn == op1.pfn
4383 && (!op0.pfn || op0.delta == op1.delta))
4385 The reason for the `!op0.pfn' bit is that a NULL
4386 pointer-to-member is any member with a zero PFN; the
4387 DELTA field is unspecified. */
4389 e1 = cp_build_binary_op (location,
4390 EQ_EXPR, delta0, delta1, complain);
4391 e2 = cp_build_binary_op (location,
4392 EQ_EXPR,
4393 pfn0,
4394 build_zero_cst (TREE_TYPE (pfn0)),
4395 complain);
4396 e1 = cp_build_binary_op (location,
4397 TRUTH_ORIF_EXPR, e1, e2, complain);
4399 e2 = build2 (EQ_EXPR, boolean_type_node, pfn0, pfn1);
4400 e = cp_build_binary_op (location,
4401 TRUTH_ANDIF_EXPR, e2, e1, complain);
4402 if (code == EQ_EXPR)
4403 return e;
4404 return cp_build_binary_op (location,
4405 EQ_EXPR, e, integer_zero_node, complain);
4407 else
4409 gcc_assert (!TYPE_PTRMEMFUNC_P (type0)
4410 || !same_type_p (TYPE_PTRMEMFUNC_FN_TYPE (type0),
4411 type1));
4412 gcc_assert (!TYPE_PTRMEMFUNC_P (type1)
4413 || !same_type_p (TYPE_PTRMEMFUNC_FN_TYPE (type1),
4414 type0));
4417 break;
4419 case MAX_EXPR:
4420 case MIN_EXPR:
4421 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE)
4422 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE))
4423 shorten = 1;
4424 else if (code0 == POINTER_TYPE && code1 == POINTER_TYPE)
4425 result_type = composite_pointer_type (type0, type1, op0, op1,
4426 CPO_COMPARISON, complain);
4427 break;
4429 case LE_EXPR:
4430 case GE_EXPR:
4431 case LT_EXPR:
4432 case GT_EXPR:
4433 if (TREE_CODE (orig_op0) == STRING_CST
4434 || TREE_CODE (orig_op1) == STRING_CST)
4436 if (complain & tf_warning)
4437 warning (OPT_Waddress, "comparison with string literal results in unspecified behaviour");
4440 if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE)
4442 vector_compare:
4443 tree intt;
4444 if (!same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (type0),
4445 TREE_TYPE (type1)))
4447 error_at (location, "comparing vectors with different "
4448 "element types");
4449 inform (location, "operand types are %qT and %qT", type0, type1);
4450 return error_mark_node;
4453 if (TYPE_VECTOR_SUBPARTS (type0) != TYPE_VECTOR_SUBPARTS (type1))
4455 error_at (location, "comparing vectors with different "
4456 "number of elements");
4457 inform (location, "operand types are %qT and %qT", type0, type1);
4458 return error_mark_node;
4461 /* Always construct signed integer vector type. */
4462 intt = c_common_type_for_size (GET_MODE_BITSIZE
4463 (TYPE_MODE (TREE_TYPE (type0))), 0);
4464 result_type = build_opaque_vector_type (intt,
4465 TYPE_VECTOR_SUBPARTS (type0));
4466 converted = 1;
4467 break;
4469 build_type = boolean_type_node;
4470 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE
4471 || code0 == ENUMERAL_TYPE)
4472 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
4473 || code1 == ENUMERAL_TYPE))
4474 short_compare = 1;
4475 else if (code0 == POINTER_TYPE && code1 == POINTER_TYPE)
4476 result_type = composite_pointer_type (type0, type1, op0, op1,
4477 CPO_COMPARISON, complain);
4478 else if (code0 == POINTER_TYPE && null_ptr_cst_p (op1))
4480 result_type = type0;
4481 if (extra_warnings && (complain & tf_warning))
4482 warning (OPT_Wextra,
4483 "ordered comparison of pointer with integer zero");
4485 else if (code1 == POINTER_TYPE && null_ptr_cst_p (op0))
4487 result_type = type1;
4488 if (extra_warnings && (complain & tf_warning))
4489 warning (OPT_Wextra,
4490 "ordered comparison of pointer with integer zero");
4492 else if (null_ptr_cst_p (op0) && null_ptr_cst_p (op1))
4493 /* One of the operands must be of nullptr_t type. */
4494 result_type = TREE_TYPE (nullptr_node);
4495 else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
4497 result_type = type0;
4498 if (complain & tf_error)
4499 permerror (input_location, "ISO C++ forbids comparison between pointer and integer");
4500 else
4501 return error_mark_node;
4503 else if (code0 == INTEGER_TYPE && code1 == POINTER_TYPE)
4505 result_type = type1;
4506 if (complain & tf_error)
4507 permerror (input_location, "ISO C++ forbids comparison between pointer and integer");
4508 else
4509 return error_mark_node;
4511 break;
4513 case UNORDERED_EXPR:
4514 case ORDERED_EXPR:
4515 case UNLT_EXPR:
4516 case UNLE_EXPR:
4517 case UNGT_EXPR:
4518 case UNGE_EXPR:
4519 case UNEQ_EXPR:
4520 build_type = integer_type_node;
4521 if (code0 != REAL_TYPE || code1 != REAL_TYPE)
4523 if (complain & tf_error)
4524 error ("unordered comparison on non-floating point argument");
4525 return error_mark_node;
4527 common = 1;
4528 break;
4530 default:
4531 break;
4534 if (((code0 == INTEGER_TYPE || code0 == REAL_TYPE || code0 == COMPLEX_TYPE
4535 || code0 == ENUMERAL_TYPE)
4536 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
4537 || code1 == COMPLEX_TYPE || code1 == ENUMERAL_TYPE)))
4538 arithmetic_types_p = 1;
4539 else
4541 arithmetic_types_p = 0;
4542 /* Vector arithmetic is only allowed when both sides are vectors. */
4543 if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE)
4545 if (!tree_int_cst_equal (TYPE_SIZE (type0), TYPE_SIZE (type1))
4546 || !same_scalar_type_ignoring_signedness (TREE_TYPE (type0),
4547 TREE_TYPE (type1)))
4549 binary_op_error (location, code, type0, type1);
4550 return error_mark_node;
4552 arithmetic_types_p = 1;
4555 /* Determine the RESULT_TYPE, if it is not already known. */
4556 if (!result_type
4557 && arithmetic_types_p
4558 && (shorten || common || short_compare))
4560 result_type = cp_common_type (type0, type1);
4561 do_warn_double_promotion (result_type, type0, type1,
4562 "implicit conversion from %qT to %qT "
4563 "to match other operand of binary "
4564 "expression",
4565 location);
4568 if (!result_type)
4570 if (complain & tf_error)
4571 error ("invalid operands of types %qT and %qT to binary %qO",
4572 TREE_TYPE (orig_op0), TREE_TYPE (orig_op1), code);
4573 return error_mark_node;
4576 /* If we're in a template, the only thing we need to know is the
4577 RESULT_TYPE. */
4578 if (processing_template_decl)
4580 /* Since the middle-end checks the type when doing a build2, we
4581 need to build the tree in pieces. This built tree will never
4582 get out of the front-end as we replace it when instantiating
4583 the template. */
4584 tree tmp = build2 (resultcode,
4585 build_type ? build_type : result_type,
4586 NULL_TREE, op1);
4587 TREE_OPERAND (tmp, 0) = op0;
4588 return tmp;
4591 if (arithmetic_types_p)
4593 bool first_complex = (code0 == COMPLEX_TYPE);
4594 bool second_complex = (code1 == COMPLEX_TYPE);
4595 int none_complex = (!first_complex && !second_complex);
4597 /* Adapted from patch for c/24581. */
4598 if (first_complex != second_complex
4599 && (code == PLUS_EXPR
4600 || code == MINUS_EXPR
4601 || code == MULT_EXPR
4602 || (code == TRUNC_DIV_EXPR && first_complex))
4603 && TREE_CODE (TREE_TYPE (result_type)) == REAL_TYPE
4604 && flag_signed_zeros)
4606 /* An operation on mixed real/complex operands must be
4607 handled specially, but the language-independent code can
4608 more easily optimize the plain complex arithmetic if
4609 -fno-signed-zeros. */
4610 tree real_type = TREE_TYPE (result_type);
4611 tree real, imag;
4612 if (first_complex)
4614 if (TREE_TYPE (op0) != result_type)
4615 op0 = cp_convert_and_check (result_type, op0, complain);
4616 if (TREE_TYPE (op1) != real_type)
4617 op1 = cp_convert_and_check (real_type, op1, complain);
4619 else
4621 if (TREE_TYPE (op0) != real_type)
4622 op0 = cp_convert_and_check (real_type, op0, complain);
4623 if (TREE_TYPE (op1) != result_type)
4624 op1 = cp_convert_and_check (result_type, op1, complain);
4626 if (TREE_CODE (op0) == ERROR_MARK || TREE_CODE (op1) == ERROR_MARK)
4627 return error_mark_node;
4628 if (first_complex)
4630 op0 = save_expr (op0);
4631 real = cp_build_unary_op (REALPART_EXPR, op0, 1, complain);
4632 imag = cp_build_unary_op (IMAGPART_EXPR, op0, 1, complain);
4633 switch (code)
4635 case MULT_EXPR:
4636 case TRUNC_DIV_EXPR:
4637 op1 = save_expr (op1);
4638 imag = build2 (resultcode, real_type, imag, op1);
4639 /* Fall through. */
4640 case PLUS_EXPR:
4641 case MINUS_EXPR:
4642 real = build2 (resultcode, real_type, real, op1);
4643 break;
4644 default:
4645 gcc_unreachable();
4648 else
4650 op1 = save_expr (op1);
4651 real = cp_build_unary_op (REALPART_EXPR, op1, 1, complain);
4652 imag = cp_build_unary_op (IMAGPART_EXPR, op1, 1, complain);
4653 switch (code)
4655 case MULT_EXPR:
4656 op0 = save_expr (op0);
4657 imag = build2 (resultcode, real_type, op0, imag);
4658 /* Fall through. */
4659 case PLUS_EXPR:
4660 real = build2 (resultcode, real_type, op0, real);
4661 break;
4662 case MINUS_EXPR:
4663 real = build2 (resultcode, real_type, op0, real);
4664 imag = build1 (NEGATE_EXPR, real_type, imag);
4665 break;
4666 default:
4667 gcc_unreachable();
4670 real = fold_if_not_in_template (real);
4671 imag = fold_if_not_in_template (imag);
4672 result = build2 (COMPLEX_EXPR, result_type, real, imag);
4673 result = fold_if_not_in_template (result);
4674 return result;
4677 /* For certain operations (which identify themselves by shorten != 0)
4678 if both args were extended from the same smaller type,
4679 do the arithmetic in that type and then extend.
4681 shorten !=0 and !=1 indicates a bitwise operation.
4682 For them, this optimization is safe only if
4683 both args are zero-extended or both are sign-extended.
4684 Otherwise, we might change the result.
4685 E.g., (short)-1 | (unsigned short)-1 is (int)-1
4686 but calculated in (unsigned short) it would be (unsigned short)-1. */
4688 if (shorten && none_complex)
4690 final_type = result_type;
4691 result_type = shorten_binary_op (result_type, op0, op1,
4692 shorten == -1);
4695 /* Comparison operations are shortened too but differently.
4696 They identify themselves by setting short_compare = 1. */
4698 if (short_compare)
4700 /* Don't write &op0, etc., because that would prevent op0
4701 from being kept in a register.
4702 Instead, make copies of the our local variables and
4703 pass the copies by reference, then copy them back afterward. */
4704 tree xop0 = op0, xop1 = op1, xresult_type = result_type;
4705 enum tree_code xresultcode = resultcode;
4706 tree val
4707 = shorten_compare (&xop0, &xop1, &xresult_type, &xresultcode);
4708 if (val != 0)
4709 return cp_convert (boolean_type_node, val, complain);
4710 op0 = xop0, op1 = xop1;
4711 converted = 1;
4712 resultcode = xresultcode;
4715 if ((short_compare || code == MIN_EXPR || code == MAX_EXPR)
4716 && warn_sign_compare
4717 /* Do not warn until the template is instantiated; we cannot
4718 bound the ranges of the arguments until that point. */
4719 && !processing_template_decl
4720 && (complain & tf_warning)
4721 && c_inhibit_evaluation_warnings == 0
4722 /* Even unsigned enum types promote to signed int. We don't
4723 want to issue -Wsign-compare warnings for this case. */
4724 && !enum_cast_to_int (orig_op0)
4725 && !enum_cast_to_int (orig_op1))
4727 tree oop0 = maybe_constant_value (orig_op0);
4728 tree oop1 = maybe_constant_value (orig_op1);
4730 if (TREE_CODE (oop0) != INTEGER_CST)
4731 oop0 = orig_op0;
4732 if (TREE_CODE (oop1) != INTEGER_CST)
4733 oop1 = orig_op1;
4734 warn_for_sign_compare (location, oop0, oop1, op0, op1,
4735 result_type, resultcode);
4739 /* If CONVERTED is zero, both args will be converted to type RESULT_TYPE.
4740 Then the expression will be built.
4741 It will be given type FINAL_TYPE if that is nonzero;
4742 otherwise, it will be given type RESULT_TYPE. */
4743 if (! converted)
4745 if (TREE_TYPE (op0) != result_type)
4746 op0 = cp_convert_and_check (result_type, op0, complain);
4747 if (TREE_TYPE (op1) != result_type)
4748 op1 = cp_convert_and_check (result_type, op1, complain);
4750 if (op0 == error_mark_node || op1 == error_mark_node)
4751 return error_mark_node;
4754 if (build_type == NULL_TREE)
4755 build_type = result_type;
4757 result = build2 (resultcode, build_type, op0, op1);
4758 result = fold_if_not_in_template (result);
4759 if (final_type != 0)
4760 result = cp_convert (final_type, result, complain);
4762 if (TREE_OVERFLOW_P (result)
4763 && !TREE_OVERFLOW_P (op0)
4764 && !TREE_OVERFLOW_P (op1))
4765 overflow_warning (location, result);
4767 return result;
4770 /* Return a tree for the sum or difference (RESULTCODE says which)
4771 of pointer PTROP and integer INTOP. */
4773 static tree
4774 cp_pointer_int_sum (enum tree_code resultcode, tree ptrop, tree intop)
4776 tree res_type = TREE_TYPE (ptrop);
4778 /* pointer_int_sum() uses size_in_bytes() on the TREE_TYPE(res_type)
4779 in certain circumstance (when it's valid to do so). So we need
4780 to make sure it's complete. We don't need to check here, if we
4781 can actually complete it at all, as those checks will be done in
4782 pointer_int_sum() anyway. */
4783 complete_type (TREE_TYPE (res_type));
4785 return pointer_int_sum (input_location, resultcode, ptrop,
4786 fold_if_not_in_template (intop));
4789 /* Return a tree for the difference of pointers OP0 and OP1.
4790 The resulting tree has type int. */
4792 static tree
4793 pointer_diff (tree op0, tree op1, tree ptrtype, tsubst_flags_t complain)
4795 tree result;
4796 tree restype = ptrdiff_type_node;
4797 tree target_type = TREE_TYPE (ptrtype);
4799 if (!complete_type_or_else (target_type, NULL_TREE))
4800 return error_mark_node;
4802 if (TREE_CODE (target_type) == VOID_TYPE)
4804 if (complain & tf_error)
4805 permerror (input_location, "ISO C++ forbids using pointer of "
4806 "type %<void *%> in subtraction");
4807 else
4808 return error_mark_node;
4810 if (TREE_CODE (target_type) == FUNCTION_TYPE)
4812 if (complain & tf_error)
4813 permerror (input_location, "ISO C++ forbids using pointer to "
4814 "a function in subtraction");
4815 else
4816 return error_mark_node;
4818 if (TREE_CODE (target_type) == METHOD_TYPE)
4820 if (complain & tf_error)
4821 permerror (input_location, "ISO C++ forbids using pointer to "
4822 "a method in subtraction");
4823 else
4824 return error_mark_node;
4827 /* First do the subtraction as integers;
4828 then drop through to build the divide operator. */
4830 op0 = cp_build_binary_op (input_location,
4831 MINUS_EXPR,
4832 cp_convert (restype, op0, complain),
4833 cp_convert (restype, op1, complain),
4834 complain);
4836 /* This generates an error if op1 is a pointer to an incomplete type. */
4837 if (!COMPLETE_TYPE_P (TREE_TYPE (TREE_TYPE (op1))))
4839 if (complain & tf_error)
4840 error ("invalid use of a pointer to an incomplete type in "
4841 "pointer arithmetic");
4842 else
4843 return error_mark_node;
4846 op1 = (TYPE_PTROB_P (ptrtype)
4847 ? size_in_bytes (target_type)
4848 : integer_one_node);
4850 /* Do the division. */
4852 result = build2 (EXACT_DIV_EXPR, restype, op0,
4853 cp_convert (restype, op1, complain));
4854 return fold_if_not_in_template (result);
4857 /* Construct and perhaps optimize a tree representation
4858 for a unary operation. CODE, a tree_code, specifies the operation
4859 and XARG is the operand. */
4861 tree
4862 build_x_unary_op (location_t loc, enum tree_code code, tree xarg,
4863 tsubst_flags_t complain)
4865 tree orig_expr = xarg;
4866 tree exp;
4867 int ptrmem = 0;
4869 if (processing_template_decl)
4871 if (type_dependent_expression_p (xarg))
4872 return build_min_nt_loc (loc, code, xarg, NULL_TREE);
4874 xarg = build_non_dependent_expr (xarg);
4877 exp = NULL_TREE;
4879 /* [expr.unary.op] says:
4881 The address of an object of incomplete type can be taken.
4883 (And is just the ordinary address operator, not an overloaded
4884 "operator &".) However, if the type is a template
4885 specialization, we must complete the type at this point so that
4886 an overloaded "operator &" will be available if required. */
4887 if (code == ADDR_EXPR
4888 && TREE_CODE (xarg) != TEMPLATE_ID_EXPR
4889 && ((CLASS_TYPE_P (TREE_TYPE (xarg))
4890 && !COMPLETE_TYPE_P (complete_type (TREE_TYPE (xarg))))
4891 || (TREE_CODE (xarg) == OFFSET_REF)))
4892 /* Don't look for a function. */;
4893 else
4894 exp = build_new_op (loc, code, LOOKUP_NORMAL, xarg, NULL_TREE,
4895 NULL_TREE, /*overload=*/NULL, complain);
4896 if (!exp && code == ADDR_EXPR)
4898 if (is_overloaded_fn (xarg))
4900 tree fn = get_first_fn (xarg);
4901 if (DECL_CONSTRUCTOR_P (fn) || DECL_DESTRUCTOR_P (fn))
4903 error (DECL_CONSTRUCTOR_P (fn)
4904 ? G_("taking address of constructor %qE")
4905 : G_("taking address of destructor %qE"),
4906 xarg);
4907 return error_mark_node;
4911 /* A pointer to member-function can be formed only by saying
4912 &X::mf. */
4913 if (!flag_ms_extensions && TREE_CODE (TREE_TYPE (xarg)) == METHOD_TYPE
4914 && (TREE_CODE (xarg) != OFFSET_REF || !PTRMEM_OK_P (xarg)))
4916 if (TREE_CODE (xarg) != OFFSET_REF
4917 || !TYPE_P (TREE_OPERAND (xarg, 0)))
4919 error ("invalid use of %qE to form a pointer-to-member-function",
4920 xarg);
4921 if (TREE_CODE (xarg) != OFFSET_REF)
4922 inform (input_location, " a qualified-id is required");
4923 return error_mark_node;
4925 else
4927 error ("parentheses around %qE cannot be used to form a"
4928 " pointer-to-member-function",
4929 xarg);
4930 PTRMEM_OK_P (xarg) = 1;
4934 if (TREE_CODE (xarg) == OFFSET_REF)
4936 ptrmem = PTRMEM_OK_P (xarg);
4938 if (!ptrmem && !flag_ms_extensions
4939 && TREE_CODE (TREE_TYPE (TREE_OPERAND (xarg, 1))) == METHOD_TYPE)
4941 /* A single non-static member, make sure we don't allow a
4942 pointer-to-member. */
4943 xarg = build2 (OFFSET_REF, TREE_TYPE (xarg),
4944 TREE_OPERAND (xarg, 0),
4945 ovl_cons (TREE_OPERAND (xarg, 1), NULL_TREE));
4946 PTRMEM_OK_P (xarg) = ptrmem;
4950 exp = cp_build_addr_expr_strict (xarg, complain);
4953 if (processing_template_decl && exp != error_mark_node)
4954 exp = build_min_non_dep (code, exp, orig_expr,
4955 /*For {PRE,POST}{INC,DEC}REMENT_EXPR*/NULL_TREE);
4956 if (TREE_CODE (exp) == ADDR_EXPR)
4957 PTRMEM_OK_P (exp) = ptrmem;
4958 return exp;
4961 /* Like c_common_truthvalue_conversion, but handle pointer-to-member
4962 constants, where a null value is represented by an INTEGER_CST of
4963 -1. */
4965 tree
4966 cp_truthvalue_conversion (tree expr)
4968 tree type = TREE_TYPE (expr);
4969 if (TYPE_PTRDATAMEM_P (type))
4970 return build_binary_op (EXPR_LOCATION (expr),
4971 NE_EXPR, expr, nullptr_node, 1);
4972 else if (TYPE_PTR_P (type) || TYPE_PTRMEMFUNC_P (type))
4974 /* With -Wzero-as-null-pointer-constant do not warn for an
4975 'if (p)' or a 'while (!p)', where p is a pointer. */
4976 tree ret;
4977 ++c_inhibit_evaluation_warnings;
4978 ret = c_common_truthvalue_conversion (input_location, expr);
4979 --c_inhibit_evaluation_warnings;
4980 return ret;
4982 else
4983 return c_common_truthvalue_conversion (input_location, expr);
4986 /* Just like cp_truthvalue_conversion, but we want a CLEANUP_POINT_EXPR. */
4988 tree
4989 condition_conversion (tree expr)
4991 tree t;
4992 if (processing_template_decl)
4993 return expr;
4994 t = perform_implicit_conversion_flags (boolean_type_node, expr,
4995 tf_warning_or_error, LOOKUP_NORMAL);
4996 t = fold_build_cleanup_point_expr (boolean_type_node, t);
4997 return t;
5000 /* Returns the address of T. This function will fold away
5001 ADDR_EXPR of INDIRECT_REF. */
5003 tree
5004 build_address (tree t)
5006 if (error_operand_p (t) || !cxx_mark_addressable (t))
5007 return error_mark_node;
5008 t = build_fold_addr_expr (t);
5009 if (TREE_CODE (t) != ADDR_EXPR)
5010 t = rvalue (t);
5011 return t;
5014 /* Returns the address of T with type TYPE. */
5016 tree
5017 build_typed_address (tree t, tree type)
5019 if (error_operand_p (t) || !cxx_mark_addressable (t))
5020 return error_mark_node;
5021 t = build_fold_addr_expr_with_type (t, type);
5022 if (TREE_CODE (t) != ADDR_EXPR)
5023 t = rvalue (t);
5024 return t;
5027 /* Return a NOP_EXPR converting EXPR to TYPE. */
5029 tree
5030 build_nop (tree type, tree expr)
5032 if (type == error_mark_node || error_operand_p (expr))
5033 return expr;
5034 return build1 (NOP_EXPR, type, expr);
5037 /* Take the address of ARG, whatever that means under C++ semantics.
5038 If STRICT_LVALUE is true, require an lvalue; otherwise, allow xvalues
5039 and class rvalues as well.
5041 Nothing should call this function directly; instead, callers should use
5042 cp_build_addr_expr or cp_build_addr_expr_strict. */
5044 static tree
5045 cp_build_addr_expr_1 (tree arg, bool strict_lvalue, tsubst_flags_t complain)
5047 tree argtype;
5048 tree val;
5050 if (!arg || error_operand_p (arg))
5051 return error_mark_node;
5053 arg = mark_lvalue_use (arg);
5054 argtype = lvalue_type (arg);
5056 gcc_assert (TREE_CODE (arg) != IDENTIFIER_NODE
5057 || !IDENTIFIER_OPNAME_P (arg));
5059 if (TREE_CODE (arg) == COMPONENT_REF && type_unknown_p (arg)
5060 && !really_overloaded_fn (TREE_OPERAND (arg, 1)))
5062 /* They're trying to take the address of a unique non-static
5063 member function. This is ill-formed (except in MS-land),
5064 but let's try to DTRT.
5065 Note: We only handle unique functions here because we don't
5066 want to complain if there's a static overload; non-unique
5067 cases will be handled by instantiate_type. But we need to
5068 handle this case here to allow casts on the resulting PMF.
5069 We could defer this in non-MS mode, but it's easier to give
5070 a useful error here. */
5072 /* Inside constant member functions, the `this' pointer
5073 contains an extra const qualifier. TYPE_MAIN_VARIANT
5074 is used here to remove this const from the diagnostics
5075 and the created OFFSET_REF. */
5076 tree base = TYPE_MAIN_VARIANT (TREE_TYPE (TREE_OPERAND (arg, 0)));
5077 tree fn = get_first_fn (TREE_OPERAND (arg, 1));
5078 mark_used (fn);
5080 if (! flag_ms_extensions)
5082 tree name = DECL_NAME (fn);
5083 if (!(complain & tf_error))
5084 return error_mark_node;
5085 else if (current_class_type
5086 && TREE_OPERAND (arg, 0) == current_class_ref)
5087 /* An expression like &memfn. */
5088 permerror (input_location, "ISO C++ forbids taking the address of an unqualified"
5089 " or parenthesized non-static member function to form"
5090 " a pointer to member function. Say %<&%T::%D%>",
5091 base, name);
5092 else
5093 permerror (input_location, "ISO C++ forbids taking the address of a bound member"
5094 " function to form a pointer to member function."
5095 " Say %<&%T::%D%>",
5096 base, name);
5098 arg = build_offset_ref (base, fn, /*address_p=*/true);
5101 /* Uninstantiated types are all functions. Taking the
5102 address of a function is a no-op, so just return the
5103 argument. */
5104 if (type_unknown_p (arg))
5105 return build1 (ADDR_EXPR, unknown_type_node, arg);
5107 if (TREE_CODE (arg) == OFFSET_REF)
5108 /* We want a pointer to member; bypass all the code for actually taking
5109 the address of something. */
5110 goto offset_ref;
5112 /* Anything not already handled and not a true memory reference
5113 is an error. */
5114 if (TREE_CODE (argtype) != FUNCTION_TYPE
5115 && TREE_CODE (argtype) != METHOD_TYPE)
5117 cp_lvalue_kind kind = lvalue_kind (arg);
5118 if (kind == clk_none)
5120 if (complain & tf_error)
5121 lvalue_error (input_location, lv_addressof);
5122 return error_mark_node;
5124 if (strict_lvalue && (kind & (clk_rvalueref|clk_class)))
5126 if (!(complain & tf_error))
5127 return error_mark_node;
5128 if (kind & clk_class)
5129 /* Make this a permerror because we used to accept it. */
5130 permerror (input_location, "taking address of temporary");
5131 else
5132 error ("taking address of xvalue (rvalue reference)");
5136 if (TREE_CODE (argtype) == REFERENCE_TYPE)
5138 tree type = build_pointer_type (TREE_TYPE (argtype));
5139 arg = build1 (CONVERT_EXPR, type, arg);
5140 return arg;
5142 else if (pedantic && DECL_MAIN_P (arg))
5144 /* ARM $3.4 */
5145 /* Apparently a lot of autoconf scripts for C++ packages do this,
5146 so only complain if -Wpedantic. */
5147 if (complain & (flag_pedantic_errors ? tf_error : tf_warning))
5148 pedwarn (input_location, OPT_Wpedantic,
5149 "ISO C++ forbids taking address of function %<::main%>");
5150 else if (flag_pedantic_errors)
5151 return error_mark_node;
5154 /* Let &* cancel out to simplify resulting code. */
5155 if (TREE_CODE (arg) == INDIRECT_REF)
5157 /* We don't need to have `current_class_ptr' wrapped in a
5158 NON_LVALUE_EXPR node. */
5159 if (arg == current_class_ref)
5160 return current_class_ptr;
5162 arg = TREE_OPERAND (arg, 0);
5163 if (TREE_CODE (TREE_TYPE (arg)) == REFERENCE_TYPE)
5165 tree type = build_pointer_type (TREE_TYPE (TREE_TYPE (arg)));
5166 arg = build1 (CONVERT_EXPR, type, arg);
5168 else
5169 /* Don't let this be an lvalue. */
5170 arg = rvalue (arg);
5171 return arg;
5174 /* ??? Cope with user tricks that amount to offsetof. */
5175 if (TREE_CODE (argtype) != FUNCTION_TYPE
5176 && TREE_CODE (argtype) != METHOD_TYPE
5177 && argtype != unknown_type_node
5178 && (val = get_base_address (arg))
5179 && COMPLETE_TYPE_P (TREE_TYPE (val))
5180 && TREE_CODE (val) == INDIRECT_REF
5181 && TREE_CONSTANT (TREE_OPERAND (val, 0)))
5183 tree type = build_pointer_type (argtype);
5184 return fold_convert (type, fold_offsetof_1 (arg));
5187 /* Handle complex lvalues (when permitted)
5188 by reduction to simpler cases. */
5189 val = unary_complex_lvalue (ADDR_EXPR, arg);
5190 if (val != 0)
5191 return val;
5193 switch (TREE_CODE (arg))
5195 CASE_CONVERT:
5196 case FLOAT_EXPR:
5197 case FIX_TRUNC_EXPR:
5198 /* Even if we're not being pedantic, we cannot allow this
5199 extension when we're instantiating in a SFINAE
5200 context. */
5201 if (! lvalue_p (arg) && complain == tf_none)
5203 if (complain & tf_error)
5204 permerror (input_location, "ISO C++ forbids taking the address of a cast to a non-lvalue expression");
5205 else
5206 return error_mark_node;
5208 break;
5210 case BASELINK:
5211 arg = BASELINK_FUNCTIONS (arg);
5212 /* Fall through. */
5214 case OVERLOAD:
5215 arg = OVL_CURRENT (arg);
5216 break;
5218 case OFFSET_REF:
5219 offset_ref:
5220 /* Turn a reference to a non-static data member into a
5221 pointer-to-member. */
5223 tree type;
5224 tree t;
5226 gcc_assert (PTRMEM_OK_P (arg));
5228 t = TREE_OPERAND (arg, 1);
5229 if (TREE_CODE (TREE_TYPE (t)) == REFERENCE_TYPE)
5231 if (complain & tf_error)
5232 error ("cannot create pointer to reference member %qD", t);
5233 return error_mark_node;
5236 type = build_ptrmem_type (context_for_name_lookup (t),
5237 TREE_TYPE (t));
5238 t = make_ptrmem_cst (type, TREE_OPERAND (arg, 1));
5239 return t;
5242 default:
5243 break;
5246 if (argtype != error_mark_node)
5247 argtype = build_pointer_type (argtype);
5249 /* In a template, we are processing a non-dependent expression
5250 so we can just form an ADDR_EXPR with the correct type. */
5251 if (processing_template_decl || TREE_CODE (arg) != COMPONENT_REF)
5253 val = build_address (arg);
5254 if (TREE_CODE (arg) == OFFSET_REF)
5255 PTRMEM_OK_P (val) = PTRMEM_OK_P (arg);
5257 else if (BASELINK_P (TREE_OPERAND (arg, 1)))
5259 tree fn = BASELINK_FUNCTIONS (TREE_OPERAND (arg, 1));
5261 /* We can only get here with a single static member
5262 function. */
5263 gcc_assert (TREE_CODE (fn) == FUNCTION_DECL
5264 && DECL_STATIC_FUNCTION_P (fn));
5265 mark_used (fn);
5266 val = build_address (fn);
5267 if (TREE_SIDE_EFFECTS (TREE_OPERAND (arg, 0)))
5268 /* Do not lose object's side effects. */
5269 val = build2 (COMPOUND_EXPR, TREE_TYPE (val),
5270 TREE_OPERAND (arg, 0), val);
5272 else if (DECL_C_BIT_FIELD (TREE_OPERAND (arg, 1)))
5274 if (complain & tf_error)
5275 error ("attempt to take address of bit-field structure member %qD",
5276 TREE_OPERAND (arg, 1));
5277 return error_mark_node;
5279 else
5281 tree object = TREE_OPERAND (arg, 0);
5282 tree field = TREE_OPERAND (arg, 1);
5283 gcc_assert (same_type_ignoring_top_level_qualifiers_p
5284 (TREE_TYPE (object), decl_type_context (field)));
5285 val = build_address (arg);
5288 if (TREE_CODE (argtype) == POINTER_TYPE
5289 && TREE_CODE (TREE_TYPE (argtype)) == METHOD_TYPE)
5291 build_ptrmemfunc_type (argtype);
5292 val = build_ptrmemfunc (argtype, val, 0,
5293 /*c_cast_p=*/false,
5294 complain);
5297 return val;
5300 /* Take the address of ARG if it has one, even if it's an rvalue. */
5302 tree
5303 cp_build_addr_expr (tree arg, tsubst_flags_t complain)
5305 return cp_build_addr_expr_1 (arg, 0, complain);
5308 /* Take the address of ARG, but only if it's an lvalue. */
5310 tree
5311 cp_build_addr_expr_strict (tree arg, tsubst_flags_t complain)
5313 return cp_build_addr_expr_1 (arg, 1, complain);
5316 /* C++: Must handle pointers to members.
5318 Perhaps type instantiation should be extended to handle conversion
5319 from aggregates to types we don't yet know we want? (Or are those
5320 cases typically errors which should be reported?)
5322 NOCONVERT nonzero suppresses the default promotions
5323 (such as from short to int). */
5325 tree
5326 cp_build_unary_op (enum tree_code code, tree xarg, int noconvert,
5327 tsubst_flags_t complain)
5329 /* No default_conversion here. It causes trouble for ADDR_EXPR. */
5330 tree arg = xarg;
5331 tree argtype = 0;
5332 const char *errstring = NULL;
5333 tree val;
5334 const char *invalid_op_diag;
5336 if (!arg || error_operand_p (arg))
5337 return error_mark_node;
5339 if ((invalid_op_diag
5340 = targetm.invalid_unary_op ((code == UNARY_PLUS_EXPR
5341 ? CONVERT_EXPR
5342 : code),
5343 TREE_TYPE (xarg))))
5345 error (invalid_op_diag);
5346 return error_mark_node;
5349 switch (code)
5351 case UNARY_PLUS_EXPR:
5352 case NEGATE_EXPR:
5354 int flags = WANT_ARITH | WANT_ENUM;
5355 /* Unary plus (but not unary minus) is allowed on pointers. */
5356 if (code == UNARY_PLUS_EXPR)
5357 flags |= WANT_POINTER;
5358 arg = build_expr_type_conversion (flags, arg, true);
5359 if (!arg)
5360 errstring = (code == NEGATE_EXPR
5361 ? _("wrong type argument to unary minus")
5362 : _("wrong type argument to unary plus"));
5363 else
5365 if (!noconvert && CP_INTEGRAL_TYPE_P (TREE_TYPE (arg)))
5366 arg = cp_perform_integral_promotions (arg, complain);
5368 /* Make sure the result is not an lvalue: a unary plus or minus
5369 expression is always a rvalue. */
5370 arg = rvalue (arg);
5373 break;
5375 case BIT_NOT_EXPR:
5376 if (TREE_CODE (TREE_TYPE (arg)) == COMPLEX_TYPE)
5378 code = CONJ_EXPR;
5379 if (!noconvert)
5381 arg = cp_default_conversion (arg, complain);
5382 if (arg == error_mark_node)
5383 return error_mark_node;
5386 else if (!(arg = build_expr_type_conversion (WANT_INT | WANT_ENUM
5387 | WANT_VECTOR_OR_COMPLEX,
5388 arg, true)))
5389 errstring = _("wrong type argument to bit-complement");
5390 else if (!noconvert && CP_INTEGRAL_TYPE_P (TREE_TYPE (arg)))
5391 arg = cp_perform_integral_promotions (arg, complain);
5392 break;
5394 case ABS_EXPR:
5395 if (!(arg = build_expr_type_conversion (WANT_ARITH | WANT_ENUM, arg, true)))
5396 errstring = _("wrong type argument to abs");
5397 else if (!noconvert)
5399 arg = cp_default_conversion (arg, complain);
5400 if (arg == error_mark_node)
5401 return error_mark_node;
5403 break;
5405 case CONJ_EXPR:
5406 /* Conjugating a real value is a no-op, but allow it anyway. */
5407 if (!(arg = build_expr_type_conversion (WANT_ARITH | WANT_ENUM, arg, true)))
5408 errstring = _("wrong type argument to conjugation");
5409 else if (!noconvert)
5411 arg = cp_default_conversion (arg, complain);
5412 if (arg == error_mark_node)
5413 return error_mark_node;
5415 break;
5417 case TRUTH_NOT_EXPR:
5418 arg = perform_implicit_conversion (boolean_type_node, arg,
5419 complain);
5420 val = invert_truthvalue_loc (input_location, arg);
5421 if (arg != error_mark_node)
5422 return val;
5423 errstring = _("in argument to unary !");
5424 break;
5426 case NOP_EXPR:
5427 break;
5429 case REALPART_EXPR:
5430 case IMAGPART_EXPR:
5431 arg = build_real_imag_expr (input_location, code, arg);
5432 if (arg == error_mark_node)
5433 return arg;
5434 else
5435 return fold_if_not_in_template (arg);
5437 case PREINCREMENT_EXPR:
5438 case POSTINCREMENT_EXPR:
5439 case PREDECREMENT_EXPR:
5440 case POSTDECREMENT_EXPR:
5441 /* Handle complex lvalues (when permitted)
5442 by reduction to simpler cases. */
5444 val = unary_complex_lvalue (code, arg);
5445 if (val != 0)
5446 return val;
5448 arg = mark_lvalue_use (arg);
5450 /* Increment or decrement the real part of the value,
5451 and don't change the imaginary part. */
5452 if (TREE_CODE (TREE_TYPE (arg)) == COMPLEX_TYPE)
5454 tree real, imag;
5456 arg = stabilize_reference (arg);
5457 real = cp_build_unary_op (REALPART_EXPR, arg, 1, complain);
5458 imag = cp_build_unary_op (IMAGPART_EXPR, arg, 1, complain);
5459 real = cp_build_unary_op (code, real, 1, complain);
5460 if (real == error_mark_node || imag == error_mark_node)
5461 return error_mark_node;
5462 return build2 (COMPLEX_EXPR, TREE_TYPE (arg),
5463 real, imag);
5466 /* Report invalid types. */
5468 if (!(arg = build_expr_type_conversion (WANT_ARITH | WANT_POINTER,
5469 arg, true)))
5471 if (code == PREINCREMENT_EXPR)
5472 errstring = _("no pre-increment operator for type");
5473 else if (code == POSTINCREMENT_EXPR)
5474 errstring = _("no post-increment operator for type");
5475 else if (code == PREDECREMENT_EXPR)
5476 errstring = _("no pre-decrement operator for type");
5477 else
5478 errstring = _("no post-decrement operator for type");
5479 break;
5481 else if (arg == error_mark_node)
5482 return error_mark_node;
5484 /* Report something read-only. */
5486 if (CP_TYPE_CONST_P (TREE_TYPE (arg))
5487 || TREE_READONLY (arg))
5489 if (complain & tf_error)
5490 cxx_readonly_error (arg, ((code == PREINCREMENT_EXPR
5491 || code == POSTINCREMENT_EXPR)
5492 ? lv_increment : lv_decrement));
5493 else
5494 return error_mark_node;
5498 tree inc;
5499 tree declared_type = unlowered_expr_type (arg);
5501 argtype = TREE_TYPE (arg);
5503 /* ARM $5.2.5 last annotation says this should be forbidden. */
5504 if (TREE_CODE (argtype) == ENUMERAL_TYPE)
5506 if (complain & tf_error)
5507 permerror (input_location, (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
5508 ? G_("ISO C++ forbids incrementing an enum")
5509 : G_("ISO C++ forbids decrementing an enum"));
5510 else
5511 return error_mark_node;
5514 /* Compute the increment. */
5516 if (TREE_CODE (argtype) == POINTER_TYPE)
5518 tree type = complete_type (TREE_TYPE (argtype));
5520 if (!COMPLETE_OR_VOID_TYPE_P (type))
5522 if (complain & tf_error)
5523 error (((code == PREINCREMENT_EXPR
5524 || code == POSTINCREMENT_EXPR))
5525 ? G_("cannot increment a pointer to incomplete type %qT")
5526 : G_("cannot decrement a pointer to incomplete type %qT"),
5527 TREE_TYPE (argtype));
5528 else
5529 return error_mark_node;
5531 else if ((pedantic || warn_pointer_arith)
5532 && !TYPE_PTROB_P (argtype))
5534 if (complain & tf_error)
5535 permerror (input_location, (code == PREINCREMENT_EXPR
5536 || code == POSTINCREMENT_EXPR)
5537 ? G_("ISO C++ forbids incrementing a pointer of type %qT")
5538 : G_("ISO C++ forbids decrementing a pointer of type %qT"),
5539 argtype);
5540 else
5541 return error_mark_node;
5544 inc = cxx_sizeof_nowarn (TREE_TYPE (argtype));
5546 else
5547 inc = integer_one_node;
5549 inc = cp_convert (argtype, inc, complain);
5551 /* If 'arg' is an Objective-C PROPERTY_REF expression, then we
5552 need to ask Objective-C to build the increment or decrement
5553 expression for it. */
5554 if (objc_is_property_ref (arg))
5555 return objc_build_incr_expr_for_property_ref (input_location, code,
5556 arg, inc);
5558 /* Complain about anything else that is not a true lvalue. */
5559 if (!lvalue_or_else (arg, ((code == PREINCREMENT_EXPR
5560 || code == POSTINCREMENT_EXPR)
5561 ? lv_increment : lv_decrement),
5562 complain))
5563 return error_mark_node;
5565 /* Forbid using -- on `bool'. */
5566 if (TREE_CODE (declared_type) == BOOLEAN_TYPE)
5568 if (code == POSTDECREMENT_EXPR || code == PREDECREMENT_EXPR)
5570 if (complain & tf_error)
5571 error ("invalid use of Boolean expression as operand "
5572 "to %<operator--%>");
5573 return error_mark_node;
5575 val = boolean_increment (code, arg);
5577 else if (code == POSTINCREMENT_EXPR || code == POSTDECREMENT_EXPR)
5578 /* An rvalue has no cv-qualifiers. */
5579 val = build2 (code, cv_unqualified (TREE_TYPE (arg)), arg, inc);
5580 else
5581 val = build2 (code, TREE_TYPE (arg), arg, inc);
5583 TREE_SIDE_EFFECTS (val) = 1;
5584 return val;
5587 case ADDR_EXPR:
5588 /* Note that this operation never does default_conversion
5589 regardless of NOCONVERT. */
5590 return cp_build_addr_expr (arg, complain);
5592 default:
5593 break;
5596 if (!errstring)
5598 if (argtype == 0)
5599 argtype = TREE_TYPE (arg);
5600 return fold_if_not_in_template (build1 (code, argtype, arg));
5603 if (complain & tf_error)
5604 error ("%s", errstring);
5605 return error_mark_node;
5608 /* Hook for the c-common bits that build a unary op. */
5609 tree
5610 build_unary_op (location_t /*location*/,
5611 enum tree_code code, tree xarg, int noconvert)
5613 return cp_build_unary_op (code, xarg, noconvert, tf_warning_or_error);
5616 /* Apply unary lvalue-demanding operator CODE to the expression ARG
5617 for certain kinds of expressions which are not really lvalues
5618 but which we can accept as lvalues.
5620 If ARG is not a kind of expression we can handle, return
5621 NULL_TREE. */
5623 tree
5624 unary_complex_lvalue (enum tree_code code, tree arg)
5626 /* Inside a template, making these kinds of adjustments is
5627 pointless; we are only concerned with the type of the
5628 expression. */
5629 if (processing_template_decl)
5630 return NULL_TREE;
5632 /* Handle (a, b) used as an "lvalue". */
5633 if (TREE_CODE (arg) == COMPOUND_EXPR)
5635 tree real_result = cp_build_unary_op (code, TREE_OPERAND (arg, 1), 0,
5636 tf_warning_or_error);
5637 return build2 (COMPOUND_EXPR, TREE_TYPE (real_result),
5638 TREE_OPERAND (arg, 0), real_result);
5641 /* Handle (a ? b : c) used as an "lvalue". */
5642 if (TREE_CODE (arg) == COND_EXPR
5643 || TREE_CODE (arg) == MIN_EXPR || TREE_CODE (arg) == MAX_EXPR)
5644 return rationalize_conditional_expr (code, arg, tf_warning_or_error);
5646 /* Handle (a = b), (++a), and (--a) used as an "lvalue". */
5647 if (TREE_CODE (arg) == MODIFY_EXPR
5648 || TREE_CODE (arg) == PREINCREMENT_EXPR
5649 || TREE_CODE (arg) == PREDECREMENT_EXPR)
5651 tree lvalue = TREE_OPERAND (arg, 0);
5652 if (TREE_SIDE_EFFECTS (lvalue))
5654 lvalue = stabilize_reference (lvalue);
5655 arg = build2 (TREE_CODE (arg), TREE_TYPE (arg),
5656 lvalue, TREE_OPERAND (arg, 1));
5658 return unary_complex_lvalue
5659 (code, build2 (COMPOUND_EXPR, TREE_TYPE (lvalue), arg, lvalue));
5662 if (code != ADDR_EXPR)
5663 return NULL_TREE;
5665 /* Handle (a = b) used as an "lvalue" for `&'. */
5666 if (TREE_CODE (arg) == MODIFY_EXPR
5667 || TREE_CODE (arg) == INIT_EXPR)
5669 tree real_result = cp_build_unary_op (code, TREE_OPERAND (arg, 0), 0,
5670 tf_warning_or_error);
5671 arg = build2 (COMPOUND_EXPR, TREE_TYPE (real_result),
5672 arg, real_result);
5673 TREE_NO_WARNING (arg) = 1;
5674 return arg;
5677 if (TREE_CODE (TREE_TYPE (arg)) == FUNCTION_TYPE
5678 || TREE_CODE (TREE_TYPE (arg)) == METHOD_TYPE
5679 || TREE_CODE (arg) == OFFSET_REF)
5680 return NULL_TREE;
5682 /* We permit compiler to make function calls returning
5683 objects of aggregate type look like lvalues. */
5685 tree targ = arg;
5687 if (TREE_CODE (targ) == SAVE_EXPR)
5688 targ = TREE_OPERAND (targ, 0);
5690 if (TREE_CODE (targ) == CALL_EXPR && MAYBE_CLASS_TYPE_P (TREE_TYPE (targ)))
5692 if (TREE_CODE (arg) == SAVE_EXPR)
5693 targ = arg;
5694 else
5695 targ = build_cplus_new (TREE_TYPE (arg), arg, tf_warning_or_error);
5696 return build1 (ADDR_EXPR, build_pointer_type (TREE_TYPE (arg)), targ);
5699 if (TREE_CODE (arg) == SAVE_EXPR && TREE_CODE (targ) == INDIRECT_REF)
5700 return build3 (SAVE_EXPR, build_pointer_type (TREE_TYPE (arg)),
5701 TREE_OPERAND (targ, 0), current_function_decl, NULL);
5704 /* Don't let anything else be handled specially. */
5705 return NULL_TREE;
5708 /* Mark EXP saying that we need to be able to take the
5709 address of it; it should not be allocated in a register.
5710 Value is true if successful.
5712 C++: we do not allow `current_class_ptr' to be addressable. */
5714 bool
5715 cxx_mark_addressable (tree exp)
5717 tree x = exp;
5719 while (1)
5720 switch (TREE_CODE (x))
5722 case ADDR_EXPR:
5723 case COMPONENT_REF:
5724 case ARRAY_REF:
5725 case REALPART_EXPR:
5726 case IMAGPART_EXPR:
5727 x = TREE_OPERAND (x, 0);
5728 break;
5730 case PARM_DECL:
5731 if (x == current_class_ptr)
5733 error ("cannot take the address of %<this%>, which is an rvalue expression");
5734 TREE_ADDRESSABLE (x) = 1; /* so compiler doesn't die later. */
5735 return true;
5737 /* Fall through. */
5739 case VAR_DECL:
5740 /* Caller should not be trying to mark initialized
5741 constant fields addressable. */
5742 gcc_assert (DECL_LANG_SPECIFIC (x) == 0
5743 || DECL_IN_AGGR_P (x) == 0
5744 || TREE_STATIC (x)
5745 || DECL_EXTERNAL (x));
5746 /* Fall through. */
5748 case RESULT_DECL:
5749 if (DECL_REGISTER (x) && !TREE_ADDRESSABLE (x)
5750 && !DECL_ARTIFICIAL (x))
5752 if (TREE_CODE (x) == VAR_DECL && DECL_HARD_REGISTER (x))
5754 error
5755 ("address of explicit register variable %qD requested", x);
5756 return false;
5758 else if (extra_warnings)
5759 warning
5760 (OPT_Wextra, "address requested for %qD, which is declared %<register%>", x);
5762 TREE_ADDRESSABLE (x) = 1;
5763 return true;
5765 case CONST_DECL:
5766 case FUNCTION_DECL:
5767 TREE_ADDRESSABLE (x) = 1;
5768 return true;
5770 case CONSTRUCTOR:
5771 TREE_ADDRESSABLE (x) = 1;
5772 return true;
5774 case TARGET_EXPR:
5775 TREE_ADDRESSABLE (x) = 1;
5776 cxx_mark_addressable (TREE_OPERAND (x, 0));
5777 return true;
5779 default:
5780 return true;
5784 /* Build and return a conditional expression IFEXP ? OP1 : OP2. */
5786 tree
5787 build_x_conditional_expr (location_t loc, tree ifexp, tree op1, tree op2,
5788 tsubst_flags_t complain)
5790 tree orig_ifexp = ifexp;
5791 tree orig_op1 = op1;
5792 tree orig_op2 = op2;
5793 tree expr;
5795 if (processing_template_decl)
5797 /* The standard says that the expression is type-dependent if
5798 IFEXP is type-dependent, even though the eventual type of the
5799 expression doesn't dependent on IFEXP. */
5800 if (type_dependent_expression_p (ifexp)
5801 /* As a GNU extension, the middle operand may be omitted. */
5802 || (op1 && type_dependent_expression_p (op1))
5803 || type_dependent_expression_p (op2))
5804 return build_min_nt_loc (loc, COND_EXPR, ifexp, op1, op2);
5805 ifexp = build_non_dependent_expr (ifexp);
5806 if (op1)
5807 op1 = build_non_dependent_expr (op1);
5808 op2 = build_non_dependent_expr (op2);
5811 expr = build_conditional_expr (ifexp, op1, op2, complain);
5812 if (processing_template_decl && expr != error_mark_node
5813 && TREE_CODE (expr) != VEC_COND_EXPR)
5815 tree min = build_min_non_dep (COND_EXPR, expr,
5816 orig_ifexp, orig_op1, orig_op2);
5817 /* In C++11, remember that the result is an lvalue or xvalue.
5818 In C++98, lvalue_kind can just assume lvalue in a template. */
5819 if (cxx_dialect >= cxx0x
5820 && lvalue_or_rvalue_with_address_p (expr)
5821 && !lvalue_or_rvalue_with_address_p (min))
5822 TREE_TYPE (min) = cp_build_reference_type (TREE_TYPE (min),
5823 !real_lvalue_p (expr));
5824 expr = convert_from_reference (min);
5826 return expr;
5829 /* Given a list of expressions, return a compound expression
5830 that performs them all and returns the value of the last of them. */
5832 tree
5833 build_x_compound_expr_from_list (tree list, expr_list_kind exp,
5834 tsubst_flags_t complain)
5836 tree expr = TREE_VALUE (list);
5838 if (BRACE_ENCLOSED_INITIALIZER_P (expr)
5839 && !CONSTRUCTOR_IS_DIRECT_INIT (expr))
5841 if (complain & tf_error)
5842 pedwarn (EXPR_LOC_OR_HERE (expr), 0, "list-initializer for "
5843 "non-class type must not be parenthesized");
5844 else
5845 return error_mark_node;
5848 if (TREE_CHAIN (list))
5850 if (complain & tf_error)
5851 switch (exp)
5853 case ELK_INIT:
5854 permerror (input_location, "expression list treated as compound "
5855 "expression in initializer");
5856 break;
5857 case ELK_MEM_INIT:
5858 permerror (input_location, "expression list treated as compound "
5859 "expression in mem-initializer");
5860 break;
5861 case ELK_FUNC_CAST:
5862 permerror (input_location, "expression list treated as compound "
5863 "expression in functional cast");
5864 break;
5865 default:
5866 gcc_unreachable ();
5868 else
5869 return error_mark_node;
5871 for (list = TREE_CHAIN (list); list; list = TREE_CHAIN (list))
5872 expr = build_x_compound_expr (EXPR_LOCATION (TREE_VALUE (list)),
5873 expr, TREE_VALUE (list), complain);
5876 return expr;
5879 /* Like build_x_compound_expr_from_list, but using a VEC. */
5881 tree
5882 build_x_compound_expr_from_vec (vec<tree, va_gc> *vec, const char *msg,
5883 tsubst_flags_t complain)
5885 if (vec_safe_is_empty (vec))
5886 return NULL_TREE;
5887 else if (vec->length () == 1)
5888 return (*vec)[0];
5889 else
5891 tree expr;
5892 unsigned int ix;
5893 tree t;
5895 if (msg != NULL)
5897 if (complain & tf_error)
5898 permerror (input_location,
5899 "%s expression list treated as compound expression",
5900 msg);
5901 else
5902 return error_mark_node;
5905 expr = (*vec)[0];
5906 for (ix = 1; vec->iterate (ix, &t); ++ix)
5907 expr = build_x_compound_expr (EXPR_LOCATION (t), expr,
5908 t, complain);
5910 return expr;
5914 /* Handle overloading of the ',' operator when needed. */
5916 tree
5917 build_x_compound_expr (location_t loc, tree op1, tree op2,
5918 tsubst_flags_t complain)
5920 tree result;
5921 tree orig_op1 = op1;
5922 tree orig_op2 = op2;
5924 if (processing_template_decl)
5926 if (type_dependent_expression_p (op1)
5927 || type_dependent_expression_p (op2))
5928 return build_min_nt_loc (loc, COMPOUND_EXPR, op1, op2);
5929 op1 = build_non_dependent_expr (op1);
5930 op2 = build_non_dependent_expr (op2);
5933 result = build_new_op (loc, COMPOUND_EXPR, LOOKUP_NORMAL, op1, op2,
5934 NULL_TREE, /*overload=*/NULL, complain);
5935 if (!result)
5936 result = cp_build_compound_expr (op1, op2, complain);
5938 if (processing_template_decl && result != error_mark_node)
5939 return build_min_non_dep (COMPOUND_EXPR, result, orig_op1, orig_op2);
5941 return result;
5944 /* Like cp_build_compound_expr, but for the c-common bits. */
5946 tree
5947 build_compound_expr (location_t /*loc*/, tree lhs, tree rhs)
5949 return cp_build_compound_expr (lhs, rhs, tf_warning_or_error);
5952 /* Build a compound expression. */
5954 tree
5955 cp_build_compound_expr (tree lhs, tree rhs, tsubst_flags_t complain)
5957 lhs = convert_to_void (lhs, ICV_LEFT_OF_COMMA, complain);
5959 if (lhs == error_mark_node || rhs == error_mark_node)
5960 return error_mark_node;
5962 if (TREE_CODE (rhs) == TARGET_EXPR)
5964 /* If the rhs is a TARGET_EXPR, then build the compound
5965 expression inside the target_expr's initializer. This
5966 helps the compiler to eliminate unnecessary temporaries. */
5967 tree init = TREE_OPERAND (rhs, 1);
5969 init = build2 (COMPOUND_EXPR, TREE_TYPE (init), lhs, init);
5970 TREE_OPERAND (rhs, 1) = init;
5972 return rhs;
5975 if (type_unknown_p (rhs))
5977 error ("no context to resolve type of %qE", rhs);
5978 return error_mark_node;
5981 return build2 (COMPOUND_EXPR, TREE_TYPE (rhs), lhs, rhs);
5984 /* Issue a diagnostic message if casting from SRC_TYPE to DEST_TYPE
5985 casts away constness. CAST gives the type of cast. Returns true
5986 if the cast is ill-formed, false if it is well-formed.
5988 ??? This function warns for casting away any qualifier not just
5989 const. We would like to specify exactly what qualifiers are casted
5990 away.
5993 static bool
5994 check_for_casting_away_constness (tree src_type, tree dest_type,
5995 enum tree_code cast, tsubst_flags_t complain)
5997 /* C-style casts are allowed to cast away constness. With
5998 WARN_CAST_QUAL, we still want to issue a warning. */
5999 if (cast == CAST_EXPR && !warn_cast_qual)
6000 return false;
6002 if (!casts_away_constness (src_type, dest_type, complain))
6003 return false;
6005 switch (cast)
6007 case CAST_EXPR:
6008 if (complain & tf_warning)
6009 warning (OPT_Wcast_qual,
6010 "cast from type %qT to type %qT casts away qualifiers",
6011 src_type, dest_type);
6012 return false;
6014 case STATIC_CAST_EXPR:
6015 if (complain & tf_error)
6016 error ("static_cast from type %qT to type %qT casts away qualifiers",
6017 src_type, dest_type);
6018 return true;
6020 case REINTERPRET_CAST_EXPR:
6021 if (complain & tf_error)
6022 error ("reinterpret_cast from type %qT to type %qT casts away qualifiers",
6023 src_type, dest_type);
6024 return true;
6026 default:
6027 gcc_unreachable();
6032 Warns if the cast from expression EXPR to type TYPE is useless.
6034 void
6035 maybe_warn_about_useless_cast (tree type, tree expr, tsubst_flags_t complain)
6037 if (warn_useless_cast
6038 && complain & tf_warning
6039 && c_inhibit_evaluation_warnings == 0)
6041 if (REFERENCE_REF_P (expr))
6042 expr = TREE_OPERAND (expr, 0);
6044 if ((TREE_CODE (type) == REFERENCE_TYPE
6045 && (TYPE_REF_IS_RVALUE (type)
6046 ? xvalue_p (expr) : real_lvalue_p (expr))
6047 && same_type_p (TREE_TYPE (expr), TREE_TYPE (type)))
6048 || same_type_p (TREE_TYPE (expr), type))
6049 warning (OPT_Wuseless_cast, "useless cast to type %qT", type);
6053 /* Convert EXPR (an expression with pointer-to-member type) to TYPE
6054 (another pointer-to-member type in the same hierarchy) and return
6055 the converted expression. If ALLOW_INVERSE_P is permitted, a
6056 pointer-to-derived may be converted to pointer-to-base; otherwise,
6057 only the other direction is permitted. If C_CAST_P is true, this
6058 conversion is taking place as part of a C-style cast. */
6060 tree
6061 convert_ptrmem (tree type, tree expr, bool allow_inverse_p,
6062 bool c_cast_p, tsubst_flags_t complain)
6064 if (TYPE_PTRDATAMEM_P (type))
6066 tree delta;
6068 if (TREE_CODE (expr) == PTRMEM_CST)
6069 expr = cplus_expand_constant (expr);
6070 delta = get_delta_difference (TYPE_PTRMEM_CLASS_TYPE (TREE_TYPE (expr)),
6071 TYPE_PTRMEM_CLASS_TYPE (type),
6072 allow_inverse_p,
6073 c_cast_p, complain);
6074 if (delta == error_mark_node)
6075 return error_mark_node;
6077 if (!integer_zerop (delta))
6079 tree cond, op1, op2;
6081 cond = cp_build_binary_op (input_location,
6082 EQ_EXPR,
6083 expr,
6084 build_int_cst (TREE_TYPE (expr), -1),
6085 complain);
6086 op1 = build_nop (ptrdiff_type_node, expr);
6087 op2 = cp_build_binary_op (input_location,
6088 PLUS_EXPR, op1, delta,
6089 complain);
6091 expr = fold_build3_loc (input_location,
6092 COND_EXPR, ptrdiff_type_node, cond, op1, op2);
6096 return build_nop (type, expr);
6098 else
6099 return build_ptrmemfunc (TYPE_PTRMEMFUNC_FN_TYPE (type), expr,
6100 allow_inverse_p, c_cast_p, complain);
6103 /* Perform a static_cast from EXPR to TYPE. When C_CAST_P is true,
6104 this static_cast is being attempted as one of the possible casts
6105 allowed by a C-style cast. (In that case, accessibility of base
6106 classes is not considered, and it is OK to cast away
6107 constness.) Return the result of the cast. *VALID_P is set to
6108 indicate whether or not the cast was valid. */
6110 static tree
6111 build_static_cast_1 (tree type, tree expr, bool c_cast_p,
6112 bool *valid_p, tsubst_flags_t complain)
6114 tree intype;
6115 tree result;
6116 cp_lvalue_kind clk;
6118 /* Assume the cast is valid. */
6119 *valid_p = true;
6121 intype = unlowered_expr_type (expr);
6123 /* Save casted types in the function's used types hash table. */
6124 used_types_insert (type);
6126 /* [expr.static.cast]
6128 An lvalue of type "cv1 B", where B is a class type, can be cast
6129 to type "reference to cv2 D", where D is a class derived (clause
6130 _class.derived_) from B, if a valid standard conversion from
6131 "pointer to D" to "pointer to B" exists (_conv.ptr_), cv2 is the
6132 same cv-qualification as, or greater cv-qualification than, cv1,
6133 and B is not a virtual base class of D. */
6134 /* We check this case before checking the validity of "TYPE t =
6135 EXPR;" below because for this case:
6137 struct B {};
6138 struct D : public B { D(const B&); };
6139 extern B& b;
6140 void f() { static_cast<const D&>(b); }
6142 we want to avoid constructing a new D. The standard is not
6143 completely clear about this issue, but our interpretation is
6144 consistent with other compilers. */
6145 if (TREE_CODE (type) == REFERENCE_TYPE
6146 && CLASS_TYPE_P (TREE_TYPE (type))
6147 && CLASS_TYPE_P (intype)
6148 && (TYPE_REF_IS_RVALUE (type) || real_lvalue_p (expr))
6149 && DERIVED_FROM_P (intype, TREE_TYPE (type))
6150 && can_convert (build_pointer_type (TYPE_MAIN_VARIANT (intype)),
6151 build_pointer_type (TYPE_MAIN_VARIANT
6152 (TREE_TYPE (type))),
6153 complain)
6154 && (c_cast_p
6155 || at_least_as_qualified_p (TREE_TYPE (type), intype)))
6157 tree base;
6159 /* There is a standard conversion from "D*" to "B*" even if "B"
6160 is ambiguous or inaccessible. If this is really a
6161 static_cast, then we check both for inaccessibility and
6162 ambiguity. However, if this is a static_cast being performed
6163 because the user wrote a C-style cast, then accessibility is
6164 not considered. */
6165 base = lookup_base (TREE_TYPE (type), intype,
6166 c_cast_p ? ba_unique : ba_check,
6167 NULL, complain);
6169 /* Convert from "B*" to "D*". This function will check that "B"
6170 is not a virtual base of "D". */
6171 expr = build_base_path (MINUS_EXPR, build_address (expr),
6172 base, /*nonnull=*/false, complain);
6173 /* Convert the pointer to a reference -- but then remember that
6174 there are no expressions with reference type in C++.
6176 We call rvalue so that there's an actual tree code
6177 (NON_LVALUE_EXPR) for the static_cast; otherwise, if the operand
6178 is a variable with the same type, the conversion would get folded
6179 away, leaving just the variable and causing lvalue_kind to give
6180 the wrong answer. */
6181 return convert_from_reference (rvalue (cp_fold_convert (type, expr)));
6184 /* "A glvalue of type cv1 T1 can be cast to type rvalue reference to
6185 cv2 T2 if cv2 T2 is reference-compatible with cv1 T1 (8.5.3)." */
6186 if (TREE_CODE (type) == REFERENCE_TYPE
6187 && TYPE_REF_IS_RVALUE (type)
6188 && (clk = real_lvalue_p (expr))
6189 && reference_related_p (TREE_TYPE (type), intype)
6190 && (c_cast_p || at_least_as_qualified_p (TREE_TYPE (type), intype)))
6192 if (clk == clk_ordinary)
6194 /* Handle the (non-bit-field) lvalue case here by casting to
6195 lvalue reference and then changing it to an rvalue reference.
6196 Casting an xvalue to rvalue reference will be handled by the
6197 main code path. */
6198 tree lref = cp_build_reference_type (TREE_TYPE (type), false);
6199 result = (perform_direct_initialization_if_possible
6200 (lref, expr, c_cast_p, complain));
6201 result = cp_fold_convert (type, result);
6202 /* Make sure we don't fold back down to a named rvalue reference,
6203 because that would be an lvalue. */
6204 if (DECL_P (result))
6205 result = build1 (NON_LVALUE_EXPR, type, result);
6206 return convert_from_reference (result);
6208 else
6209 /* For a bit-field or packed field, bind to a temporary. */
6210 expr = rvalue (expr);
6213 /* Resolve overloaded address here rather than once in
6214 implicit_conversion and again in the inverse code below. */
6215 if (TYPE_PTRMEMFUNC_P (type) && type_unknown_p (expr))
6217 expr = instantiate_type (type, expr, complain);
6218 intype = TREE_TYPE (expr);
6221 /* [expr.static.cast]
6223 Any expression can be explicitly converted to type cv void. */
6224 if (TREE_CODE (type) == VOID_TYPE)
6225 return convert_to_void (expr, ICV_CAST, complain);
6227 /* [expr.static.cast]
6229 An expression e can be explicitly converted to a type T using a
6230 static_cast of the form static_cast<T>(e) if the declaration T
6231 t(e);" is well-formed, for some invented temporary variable
6232 t. */
6233 result = perform_direct_initialization_if_possible (type, expr,
6234 c_cast_p, complain);
6235 if (result)
6237 result = convert_from_reference (result);
6239 /* [expr.static.cast]
6241 If T is a reference type, the result is an lvalue; otherwise,
6242 the result is an rvalue. */
6243 if (TREE_CODE (type) != REFERENCE_TYPE)
6244 result = rvalue (result);
6245 return result;
6248 /* [expr.static.cast]
6250 The inverse of any standard conversion sequence (clause _conv_),
6251 other than the lvalue-to-rvalue (_conv.lval_), array-to-pointer
6252 (_conv.array_), function-to-pointer (_conv.func_), and boolean
6253 (_conv.bool_) conversions, can be performed explicitly using
6254 static_cast subject to the restriction that the explicit
6255 conversion does not cast away constness (_expr.const.cast_), and
6256 the following additional rules for specific cases: */
6257 /* For reference, the conversions not excluded are: integral
6258 promotions, floating point promotion, integral conversions,
6259 floating point conversions, floating-integral conversions,
6260 pointer conversions, and pointer to member conversions. */
6261 /* DR 128
6263 A value of integral _or enumeration_ type can be explicitly
6264 converted to an enumeration type. */
6265 /* The effect of all that is that any conversion between any two
6266 types which are integral, floating, or enumeration types can be
6267 performed. */
6268 if ((INTEGRAL_OR_ENUMERATION_TYPE_P (type)
6269 || SCALAR_FLOAT_TYPE_P (type))
6270 && (INTEGRAL_OR_ENUMERATION_TYPE_P (intype)
6271 || SCALAR_FLOAT_TYPE_P (intype)))
6272 return ocp_convert (type, expr, CONV_C_CAST, LOOKUP_NORMAL, complain);
6274 if (TYPE_PTR_P (type) && TYPE_PTR_P (intype)
6275 && CLASS_TYPE_P (TREE_TYPE (type))
6276 && CLASS_TYPE_P (TREE_TYPE (intype))
6277 && can_convert (build_pointer_type (TYPE_MAIN_VARIANT
6278 (TREE_TYPE (intype))),
6279 build_pointer_type (TYPE_MAIN_VARIANT
6280 (TREE_TYPE (type))),
6281 complain))
6283 tree base;
6285 if (!c_cast_p
6286 && check_for_casting_away_constness (intype, type, STATIC_CAST_EXPR,
6287 complain))
6288 return error_mark_node;
6289 base = lookup_base (TREE_TYPE (type), TREE_TYPE (intype),
6290 c_cast_p ? ba_unique : ba_check,
6291 NULL, complain);
6292 expr = build_base_path (MINUS_EXPR, expr, base, /*nonnull=*/false,
6293 complain);
6294 return cp_fold_convert(type, expr);
6297 if ((TYPE_PTRDATAMEM_P (type) && TYPE_PTRDATAMEM_P (intype))
6298 || (TYPE_PTRMEMFUNC_P (type) && TYPE_PTRMEMFUNC_P (intype)))
6300 tree c1;
6301 tree c2;
6302 tree t1;
6303 tree t2;
6305 c1 = TYPE_PTRMEM_CLASS_TYPE (intype);
6306 c2 = TYPE_PTRMEM_CLASS_TYPE (type);
6308 if (TYPE_PTRDATAMEM_P (type))
6310 t1 = (build_ptrmem_type
6311 (c1,
6312 TYPE_MAIN_VARIANT (TYPE_PTRMEM_POINTED_TO_TYPE (intype))));
6313 t2 = (build_ptrmem_type
6314 (c2,
6315 TYPE_MAIN_VARIANT (TYPE_PTRMEM_POINTED_TO_TYPE (type))));
6317 else
6319 t1 = intype;
6320 t2 = type;
6322 if (can_convert (t1, t2, complain) || can_convert (t2, t1, complain))
6324 if (!c_cast_p
6325 && check_for_casting_away_constness (intype, type,
6326 STATIC_CAST_EXPR,
6327 complain))
6328 return error_mark_node;
6329 return convert_ptrmem (type, expr, /*allow_inverse_p=*/1,
6330 c_cast_p, complain);
6334 /* [expr.static.cast]
6336 An rvalue of type "pointer to cv void" can be explicitly
6337 converted to a pointer to object type. A value of type pointer
6338 to object converted to "pointer to cv void" and back to the
6339 original pointer type will have its original value. */
6340 if (TREE_CODE (intype) == POINTER_TYPE
6341 && VOID_TYPE_P (TREE_TYPE (intype))
6342 && TYPE_PTROB_P (type))
6344 if (!c_cast_p
6345 && check_for_casting_away_constness (intype, type, STATIC_CAST_EXPR,
6346 complain))
6347 return error_mark_node;
6348 return build_nop (type, expr);
6351 *valid_p = false;
6352 return error_mark_node;
6355 /* Return an expression representing static_cast<TYPE>(EXPR). */
6357 tree
6358 build_static_cast (tree type, tree expr, tsubst_flags_t complain)
6360 tree result;
6361 bool valid_p;
6363 if (type == error_mark_node || expr == error_mark_node)
6364 return error_mark_node;
6366 if (processing_template_decl)
6368 expr = build_min (STATIC_CAST_EXPR, type, expr);
6369 /* We don't know if it will or will not have side effects. */
6370 TREE_SIDE_EFFECTS (expr) = 1;
6371 return convert_from_reference (expr);
6374 /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
6375 Strip such NOP_EXPRs if VALUE is being used in non-lvalue context. */
6376 if (TREE_CODE (type) != REFERENCE_TYPE
6377 && TREE_CODE (expr) == NOP_EXPR
6378 && TREE_TYPE (expr) == TREE_TYPE (TREE_OPERAND (expr, 0)))
6379 expr = TREE_OPERAND (expr, 0);
6381 result = build_static_cast_1 (type, expr, /*c_cast_p=*/false, &valid_p,
6382 complain);
6383 if (valid_p)
6385 if (result != error_mark_node)
6386 maybe_warn_about_useless_cast (type, expr, complain);
6387 return result;
6390 if (complain & tf_error)
6391 error ("invalid static_cast from type %qT to type %qT",
6392 TREE_TYPE (expr), type);
6393 return error_mark_node;
6396 /* EXPR is an expression with member function or pointer-to-member
6397 function type. TYPE is a pointer type. Converting EXPR to TYPE is
6398 not permitted by ISO C++, but we accept it in some modes. If we
6399 are not in one of those modes, issue a diagnostic. Return the
6400 converted expression. */
6402 tree
6403 convert_member_func_to_ptr (tree type, tree expr, tsubst_flags_t complain)
6405 tree intype;
6406 tree decl;
6408 intype = TREE_TYPE (expr);
6409 gcc_assert (TYPE_PTRMEMFUNC_P (intype)
6410 || TREE_CODE (intype) == METHOD_TYPE);
6412 if (!(complain & tf_warning_or_error))
6413 return error_mark_node;
6415 if (pedantic || warn_pmf2ptr)
6416 pedwarn (input_location, pedantic ? OPT_Wpedantic : OPT_Wpmf_conversions,
6417 "converting from %qT to %qT", intype, type);
6419 if (TREE_CODE (intype) == METHOD_TYPE)
6420 expr = build_addr_func (expr, complain);
6421 else if (TREE_CODE (expr) == PTRMEM_CST)
6422 expr = build_address (PTRMEM_CST_MEMBER (expr));
6423 else
6425 decl = maybe_dummy_object (TYPE_PTRMEM_CLASS_TYPE (intype), 0);
6426 decl = build_address (decl);
6427 expr = get_member_function_from_ptrfunc (&decl, expr, complain);
6430 if (expr == error_mark_node)
6431 return error_mark_node;
6433 return build_nop (type, expr);
6436 /* Return a representation for a reinterpret_cast from EXPR to TYPE.
6437 If C_CAST_P is true, this reinterpret cast is being done as part of
6438 a C-style cast. If VALID_P is non-NULL, *VALID_P is set to
6439 indicate whether or not reinterpret_cast was valid. */
6441 static tree
6442 build_reinterpret_cast_1 (tree type, tree expr, bool c_cast_p,
6443 bool *valid_p, tsubst_flags_t complain)
6445 tree intype;
6447 /* Assume the cast is invalid. */
6448 if (valid_p)
6449 *valid_p = true;
6451 if (type == error_mark_node || error_operand_p (expr))
6452 return error_mark_node;
6454 intype = TREE_TYPE (expr);
6456 /* Save casted types in the function's used types hash table. */
6457 used_types_insert (type);
6459 /* [expr.reinterpret.cast]
6460 An lvalue expression of type T1 can be cast to the type
6461 "reference to T2" if an expression of type "pointer to T1" can be
6462 explicitly converted to the type "pointer to T2" using a
6463 reinterpret_cast. */
6464 if (TREE_CODE (type) == REFERENCE_TYPE)
6466 if (! real_lvalue_p (expr))
6468 if (complain & tf_error)
6469 error ("invalid cast of an rvalue expression of type "
6470 "%qT to type %qT",
6471 intype, type);
6472 return error_mark_node;
6475 /* Warn about a reinterpret_cast from "A*" to "B&" if "A" and
6476 "B" are related class types; the reinterpret_cast does not
6477 adjust the pointer. */
6478 if (TYPE_PTR_P (intype)
6479 && (complain & tf_warning)
6480 && (comptypes (TREE_TYPE (intype), TREE_TYPE (type),
6481 COMPARE_BASE | COMPARE_DERIVED)))
6482 warning (0, "casting %qT to %qT does not dereference pointer",
6483 intype, type);
6485 expr = cp_build_addr_expr (expr, complain);
6487 if (warn_strict_aliasing > 2)
6488 strict_aliasing_warning (TREE_TYPE (expr), type, expr);
6490 if (expr != error_mark_node)
6491 expr = build_reinterpret_cast_1
6492 (build_pointer_type (TREE_TYPE (type)), expr, c_cast_p,
6493 valid_p, complain);
6494 if (expr != error_mark_node)
6495 /* cp_build_indirect_ref isn't right for rvalue refs. */
6496 expr = convert_from_reference (fold_convert (type, expr));
6497 return expr;
6500 /* As a G++ extension, we consider conversions from member
6501 functions, and pointers to member functions to
6502 pointer-to-function and pointer-to-void types. If
6503 -Wno-pmf-conversions has not been specified,
6504 convert_member_func_to_ptr will issue an error message. */
6505 if ((TYPE_PTRMEMFUNC_P (intype)
6506 || TREE_CODE (intype) == METHOD_TYPE)
6507 && TYPE_PTR_P (type)
6508 && (TREE_CODE (TREE_TYPE (type)) == FUNCTION_TYPE
6509 || VOID_TYPE_P (TREE_TYPE (type))))
6510 return convert_member_func_to_ptr (type, expr, complain);
6512 /* If the cast is not to a reference type, the lvalue-to-rvalue,
6513 array-to-pointer, and function-to-pointer conversions are
6514 performed. */
6515 expr = decay_conversion (expr, complain);
6517 /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
6518 Strip such NOP_EXPRs if VALUE is being used in non-lvalue context. */
6519 if (TREE_CODE (expr) == NOP_EXPR
6520 && TREE_TYPE (expr) == TREE_TYPE (TREE_OPERAND (expr, 0)))
6521 expr = TREE_OPERAND (expr, 0);
6523 if (error_operand_p (expr))
6524 return error_mark_node;
6526 intype = TREE_TYPE (expr);
6528 /* [expr.reinterpret.cast]
6529 A pointer can be converted to any integral type large enough to
6530 hold it. ... A value of type std::nullptr_t can be converted to
6531 an integral type; the conversion has the same meaning and
6532 validity as a conversion of (void*)0 to the integral type. */
6533 if (CP_INTEGRAL_TYPE_P (type)
6534 && (TYPE_PTR_P (intype) || NULLPTR_TYPE_P (intype)))
6536 if (TYPE_PRECISION (type) < TYPE_PRECISION (intype))
6538 if (complain & tf_error)
6539 permerror (input_location, "cast from %qT to %qT loses precision",
6540 intype, type);
6541 else
6542 return error_mark_node;
6544 if (NULLPTR_TYPE_P (intype))
6545 return build_int_cst (type, 0);
6547 /* [expr.reinterpret.cast]
6548 A value of integral or enumeration type can be explicitly
6549 converted to a pointer. */
6550 else if (TYPE_PTR_P (type) && INTEGRAL_OR_ENUMERATION_TYPE_P (intype))
6551 /* OK */
6553 else if ((INTEGRAL_OR_ENUMERATION_TYPE_P (type)
6554 || TYPE_PTR_OR_PTRMEM_P (type))
6555 && same_type_p (type, intype))
6556 /* DR 799 */
6557 return fold_if_not_in_template (build_nop (type, expr));
6558 else if ((TYPE_PTRFN_P (type) && TYPE_PTRFN_P (intype))
6559 || (TYPE_PTRMEMFUNC_P (type) && TYPE_PTRMEMFUNC_P (intype)))
6560 return fold_if_not_in_template (build_nop (type, expr));
6561 else if ((TYPE_PTRDATAMEM_P (type) && TYPE_PTRDATAMEM_P (intype))
6562 || (TYPE_PTROBV_P (type) && TYPE_PTROBV_P (intype)))
6564 tree sexpr = expr;
6566 if (!c_cast_p
6567 && check_for_casting_away_constness (intype, type,
6568 REINTERPRET_CAST_EXPR,
6569 complain))
6570 return error_mark_node;
6571 /* Warn about possible alignment problems. */
6572 if (STRICT_ALIGNMENT && warn_cast_align
6573 && (complain & tf_warning)
6574 && !VOID_TYPE_P (type)
6575 && TREE_CODE (TREE_TYPE (intype)) != FUNCTION_TYPE
6576 && COMPLETE_TYPE_P (TREE_TYPE (type))
6577 && COMPLETE_TYPE_P (TREE_TYPE (intype))
6578 && TYPE_ALIGN (TREE_TYPE (type)) > TYPE_ALIGN (TREE_TYPE (intype)))
6579 warning (OPT_Wcast_align, "cast from %qT to %qT "
6580 "increases required alignment of target type", intype, type);
6582 /* We need to strip nops here, because the front end likes to
6583 create (int *)&a for array-to-pointer decay, instead of &a[0]. */
6584 STRIP_NOPS (sexpr);
6585 if (warn_strict_aliasing <= 2)
6586 strict_aliasing_warning (intype, type, sexpr);
6588 return fold_if_not_in_template (build_nop (type, expr));
6590 else if ((TYPE_PTRFN_P (type) && TYPE_PTROBV_P (intype))
6591 || (TYPE_PTRFN_P (intype) && TYPE_PTROBV_P (type)))
6593 if (pedantic && (complain & tf_warning))
6594 /* Only issue a warning, as we have always supported this
6595 where possible, and it is necessary in some cases. DR 195
6596 addresses this issue, but as of 2004/10/26 is still in
6597 drafting. */
6598 warning (0, "ISO C++ forbids casting between pointer-to-function and pointer-to-object");
6599 return fold_if_not_in_template (build_nop (type, expr));
6601 else if (TREE_CODE (type) == VECTOR_TYPE)
6602 return fold_if_not_in_template (convert_to_vector (type, expr));
6603 else if (TREE_CODE (intype) == VECTOR_TYPE
6604 && INTEGRAL_OR_ENUMERATION_TYPE_P (type))
6605 return fold_if_not_in_template (convert_to_integer (type, expr));
6606 else
6608 if (valid_p)
6609 *valid_p = false;
6610 if (complain & tf_error)
6611 error ("invalid cast from type %qT to type %qT", intype, type);
6612 return error_mark_node;
6615 return cp_convert (type, expr, complain);
6618 tree
6619 build_reinterpret_cast (tree type, tree expr, tsubst_flags_t complain)
6621 tree r;
6623 if (type == error_mark_node || expr == error_mark_node)
6624 return error_mark_node;
6626 if (processing_template_decl)
6628 tree t = build_min (REINTERPRET_CAST_EXPR, type, expr);
6630 if (!TREE_SIDE_EFFECTS (t)
6631 && type_dependent_expression_p (expr))
6632 /* There might turn out to be side effects inside expr. */
6633 TREE_SIDE_EFFECTS (t) = 1;
6634 return convert_from_reference (t);
6637 r = build_reinterpret_cast_1 (type, expr, /*c_cast_p=*/false,
6638 /*valid_p=*/NULL, complain);
6639 if (r != error_mark_node)
6640 maybe_warn_about_useless_cast (type, expr, complain);
6641 return r;
6644 /* Perform a const_cast from EXPR to TYPE. If the cast is valid,
6645 return an appropriate expression. Otherwise, return
6646 error_mark_node. If the cast is not valid, and COMPLAIN is true,
6647 then a diagnostic will be issued. If VALID_P is non-NULL, we are
6648 performing a C-style cast, its value upon return will indicate
6649 whether or not the conversion succeeded. */
6651 static tree
6652 build_const_cast_1 (tree dst_type, tree expr, tsubst_flags_t complain,
6653 bool *valid_p)
6655 tree src_type;
6656 tree reference_type;
6658 /* Callers are responsible for handling error_mark_node as a
6659 destination type. */
6660 gcc_assert (dst_type != error_mark_node);
6661 /* In a template, callers should be building syntactic
6662 representations of casts, not using this machinery. */
6663 gcc_assert (!processing_template_decl);
6665 /* Assume the conversion is invalid. */
6666 if (valid_p)
6667 *valid_p = false;
6669 if (!POINTER_TYPE_P (dst_type) && !TYPE_PTRDATAMEM_P (dst_type))
6671 if (complain & tf_error)
6672 error ("invalid use of const_cast with type %qT, "
6673 "which is not a pointer, "
6674 "reference, nor a pointer-to-data-member type", dst_type);
6675 return error_mark_node;
6678 if (TREE_CODE (TREE_TYPE (dst_type)) == FUNCTION_TYPE)
6680 if (complain & tf_error)
6681 error ("invalid use of const_cast with type %qT, which is a pointer "
6682 "or reference to a function type", dst_type);
6683 return error_mark_node;
6686 /* Save casted types in the function's used types hash table. */
6687 used_types_insert (dst_type);
6689 src_type = TREE_TYPE (expr);
6690 /* Expressions do not really have reference types. */
6691 if (TREE_CODE (src_type) == REFERENCE_TYPE)
6692 src_type = TREE_TYPE (src_type);
6694 /* [expr.const.cast]
6696 For two object types T1 and T2, if a pointer to T1 can be explicitly
6697 converted to the type "pointer to T2" using a const_cast, then the
6698 following conversions can also be made:
6700 -- an lvalue of type T1 can be explicitly converted to an lvalue of
6701 type T2 using the cast const_cast<T2&>;
6703 -- a glvalue of type T1 can be explicitly converted to an xvalue of
6704 type T2 using the cast const_cast<T2&&>; and
6706 -- if T1 is a class type, a prvalue of type T1 can be explicitly
6707 converted to an xvalue of type T2 using the cast const_cast<T2&&>. */
6709 if (TREE_CODE (dst_type) == REFERENCE_TYPE)
6711 reference_type = dst_type;
6712 if (!TYPE_REF_IS_RVALUE (dst_type)
6713 ? real_lvalue_p (expr)
6714 : (CLASS_TYPE_P (TREE_TYPE (dst_type))
6715 ? lvalue_p (expr)
6716 : lvalue_or_rvalue_with_address_p (expr)))
6717 /* OK. */;
6718 else
6720 if (complain & tf_error)
6721 error ("invalid const_cast of an rvalue of type %qT to type %qT",
6722 src_type, dst_type);
6723 return error_mark_node;
6725 dst_type = build_pointer_type (TREE_TYPE (dst_type));
6726 src_type = build_pointer_type (src_type);
6728 else
6730 reference_type = NULL_TREE;
6731 /* If the destination type is not a reference type, the
6732 lvalue-to-rvalue, array-to-pointer, and function-to-pointer
6733 conversions are performed. */
6734 src_type = type_decays_to (src_type);
6735 if (src_type == error_mark_node)
6736 return error_mark_node;
6739 if (TYPE_PTR_P (src_type) || TYPE_PTRDATAMEM_P (src_type))
6741 if (comp_ptr_ttypes_const (dst_type, src_type))
6743 if (valid_p)
6745 *valid_p = true;
6746 /* This cast is actually a C-style cast. Issue a warning if
6747 the user is making a potentially unsafe cast. */
6748 check_for_casting_away_constness (src_type, dst_type,
6749 CAST_EXPR, complain);
6751 if (reference_type)
6753 expr = cp_build_addr_expr (expr, complain);
6754 if (expr == error_mark_node)
6755 return error_mark_node;
6756 expr = build_nop (reference_type, expr);
6757 return convert_from_reference (expr);
6759 else
6761 expr = decay_conversion (expr, complain);
6762 if (expr == error_mark_node)
6763 return error_mark_node;
6765 /* build_c_cast puts on a NOP_EXPR to make the result not an
6766 lvalue. Strip such NOP_EXPRs if VALUE is being used in
6767 non-lvalue context. */
6768 if (TREE_CODE (expr) == NOP_EXPR
6769 && TREE_TYPE (expr) == TREE_TYPE (TREE_OPERAND (expr, 0)))
6770 expr = TREE_OPERAND (expr, 0);
6771 return build_nop (dst_type, expr);
6774 else if (valid_p
6775 && !at_least_as_qualified_p (TREE_TYPE (dst_type),
6776 TREE_TYPE (src_type)))
6777 check_for_casting_away_constness (src_type, dst_type, CAST_EXPR,
6778 complain);
6781 if (complain & tf_error)
6782 error ("invalid const_cast from type %qT to type %qT",
6783 src_type, dst_type);
6784 return error_mark_node;
6787 tree
6788 build_const_cast (tree type, tree expr, tsubst_flags_t complain)
6790 tree r;
6792 if (type == error_mark_node || error_operand_p (expr))
6793 return error_mark_node;
6795 if (processing_template_decl)
6797 tree t = build_min (CONST_CAST_EXPR, type, expr);
6799 if (!TREE_SIDE_EFFECTS (t)
6800 && type_dependent_expression_p (expr))
6801 /* There might turn out to be side effects inside expr. */
6802 TREE_SIDE_EFFECTS (t) = 1;
6803 return convert_from_reference (t);
6806 r = build_const_cast_1 (type, expr, complain, /*valid_p=*/NULL);
6807 if (r != error_mark_node)
6808 maybe_warn_about_useless_cast (type, expr, complain);
6809 return r;
6812 /* Like cp_build_c_cast, but for the c-common bits. */
6814 tree
6815 build_c_cast (location_t /*loc*/, tree type, tree expr)
6817 return cp_build_c_cast (type, expr, tf_warning_or_error);
6820 /* Build an expression representing an explicit C-style cast to type
6821 TYPE of expression EXPR. */
6823 tree
6824 cp_build_c_cast (tree type, tree expr, tsubst_flags_t complain)
6826 tree value = expr;
6827 tree result;
6828 bool valid_p;
6830 if (type == error_mark_node || error_operand_p (expr))
6831 return error_mark_node;
6833 if (processing_template_decl)
6835 tree t = build_min (CAST_EXPR, type,
6836 tree_cons (NULL_TREE, value, NULL_TREE));
6837 /* We don't know if it will or will not have side effects. */
6838 TREE_SIDE_EFFECTS (t) = 1;
6839 return convert_from_reference (t);
6842 /* Casts to a (pointer to a) specific ObjC class (or 'id' or
6843 'Class') should always be retained, because this information aids
6844 in method lookup. */
6845 if (objc_is_object_ptr (type)
6846 && objc_is_object_ptr (TREE_TYPE (expr)))
6847 return build_nop (type, expr);
6849 /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
6850 Strip such NOP_EXPRs if VALUE is being used in non-lvalue context. */
6851 if (TREE_CODE (type) != REFERENCE_TYPE
6852 && TREE_CODE (value) == NOP_EXPR
6853 && TREE_TYPE (value) == TREE_TYPE (TREE_OPERAND (value, 0)))
6854 value = TREE_OPERAND (value, 0);
6856 if (TREE_CODE (type) == ARRAY_TYPE)
6858 /* Allow casting from T1* to T2[] because Cfront allows it.
6859 NIHCL uses it. It is not valid ISO C++ however. */
6860 if (TREE_CODE (TREE_TYPE (expr)) == POINTER_TYPE)
6862 if (complain & tf_error)
6863 permerror (input_location, "ISO C++ forbids casting to an array type %qT", type);
6864 else
6865 return error_mark_node;
6866 type = build_pointer_type (TREE_TYPE (type));
6868 else
6870 if (complain & tf_error)
6871 error ("ISO C++ forbids casting to an array type %qT", type);
6872 return error_mark_node;
6876 if (TREE_CODE (type) == FUNCTION_TYPE
6877 || TREE_CODE (type) == METHOD_TYPE)
6879 if (complain & tf_error)
6880 error ("invalid cast to function type %qT", type);
6881 return error_mark_node;
6884 if (TREE_CODE (type) == POINTER_TYPE
6885 && TREE_CODE (TREE_TYPE (value)) == INTEGER_TYPE
6886 /* Casting to an integer of smaller size is an error detected elsewhere. */
6887 && TYPE_PRECISION (type) > TYPE_PRECISION (TREE_TYPE (value))
6888 /* Don't warn about converting any constant. */
6889 && !TREE_CONSTANT (value))
6890 warning_at (input_location, OPT_Wint_to_pointer_cast,
6891 "cast to pointer from integer of different size");
6893 /* A C-style cast can be a const_cast. */
6894 result = build_const_cast_1 (type, value, complain & tf_warning,
6895 &valid_p);
6896 if (valid_p)
6898 if (result != error_mark_node)
6899 maybe_warn_about_useless_cast (type, value, complain);
6900 return result;
6903 /* Or a static cast. */
6904 result = build_static_cast_1 (type, value, /*c_cast_p=*/true,
6905 &valid_p, complain);
6906 /* Or a reinterpret_cast. */
6907 if (!valid_p)
6908 result = build_reinterpret_cast_1 (type, value, /*c_cast_p=*/true,
6909 &valid_p, complain);
6910 /* The static_cast or reinterpret_cast may be followed by a
6911 const_cast. */
6912 if (valid_p
6913 /* A valid cast may result in errors if, for example, a
6914 conversion to an ambiguous base class is required. */
6915 && !error_operand_p (result))
6917 tree result_type;
6919 maybe_warn_about_useless_cast (type, value, complain);
6921 /* Non-class rvalues always have cv-unqualified type. */
6922 if (!CLASS_TYPE_P (type))
6923 type = TYPE_MAIN_VARIANT (type);
6924 result_type = TREE_TYPE (result);
6925 if (!CLASS_TYPE_P (result_type) && TREE_CODE (type) != REFERENCE_TYPE)
6926 result_type = TYPE_MAIN_VARIANT (result_type);
6927 /* If the type of RESULT does not match TYPE, perform a
6928 const_cast to make it match. If the static_cast or
6929 reinterpret_cast succeeded, we will differ by at most
6930 cv-qualification, so the follow-on const_cast is guaranteed
6931 to succeed. */
6932 if (!same_type_p (non_reference (type), non_reference (result_type)))
6934 result = build_const_cast_1 (type, result, false, &valid_p);
6935 gcc_assert (valid_p);
6937 return result;
6940 return error_mark_node;
6943 /* For use from the C common bits. */
6944 tree
6945 build_modify_expr (location_t /*location*/,
6946 tree lhs, tree /*lhs_origtype*/,
6947 enum tree_code modifycode,
6948 location_t /*rhs_location*/, tree rhs,
6949 tree /*rhs_origtype*/)
6951 return cp_build_modify_expr (lhs, modifycode, rhs, tf_warning_or_error);
6954 /* Build an assignment expression of lvalue LHS from value RHS.
6955 MODIFYCODE is the code for a binary operator that we use
6956 to combine the old value of LHS with RHS to get the new value.
6957 Or else MODIFYCODE is NOP_EXPR meaning do a simple assignment.
6959 C++: If MODIFYCODE is INIT_EXPR, then leave references unbashed. */
6961 tree
6962 cp_build_modify_expr (tree lhs, enum tree_code modifycode, tree rhs,
6963 tsubst_flags_t complain)
6965 tree result;
6966 tree newrhs = rhs;
6967 tree lhstype = TREE_TYPE (lhs);
6968 tree olhstype = lhstype;
6969 bool plain_assign = (modifycode == NOP_EXPR);
6971 /* Avoid duplicate error messages from operands that had errors. */
6972 if (error_operand_p (lhs) || error_operand_p (rhs))
6973 return error_mark_node;
6975 /* Handle control structure constructs used as "lvalues". */
6976 switch (TREE_CODE (lhs))
6978 /* Handle --foo = 5; as these are valid constructs in C++. */
6979 case PREDECREMENT_EXPR:
6980 case PREINCREMENT_EXPR:
6981 if (TREE_SIDE_EFFECTS (TREE_OPERAND (lhs, 0)))
6982 lhs = build2 (TREE_CODE (lhs), TREE_TYPE (lhs),
6983 stabilize_reference (TREE_OPERAND (lhs, 0)),
6984 TREE_OPERAND (lhs, 1));
6985 newrhs = cp_build_modify_expr (TREE_OPERAND (lhs, 0),
6986 modifycode, rhs, complain);
6987 if (newrhs == error_mark_node)
6988 return error_mark_node;
6989 return build2 (COMPOUND_EXPR, lhstype, lhs, newrhs);
6991 /* Handle (a, b) used as an "lvalue". */
6992 case COMPOUND_EXPR:
6993 newrhs = cp_build_modify_expr (TREE_OPERAND (lhs, 1),
6994 modifycode, rhs, complain);
6995 if (newrhs == error_mark_node)
6996 return error_mark_node;
6997 return build2 (COMPOUND_EXPR, lhstype,
6998 TREE_OPERAND (lhs, 0), newrhs);
7000 case MODIFY_EXPR:
7001 if (TREE_SIDE_EFFECTS (TREE_OPERAND (lhs, 0)))
7002 lhs = build2 (TREE_CODE (lhs), TREE_TYPE (lhs),
7003 stabilize_reference (TREE_OPERAND (lhs, 0)),
7004 TREE_OPERAND (lhs, 1));
7005 newrhs = cp_build_modify_expr (TREE_OPERAND (lhs, 0), modifycode, rhs,
7006 complain);
7007 if (newrhs == error_mark_node)
7008 return error_mark_node;
7009 return build2 (COMPOUND_EXPR, lhstype, lhs, newrhs);
7011 case MIN_EXPR:
7012 case MAX_EXPR:
7013 /* MIN_EXPR and MAX_EXPR are currently only permitted as lvalues,
7014 when neither operand has side-effects. */
7015 if (!lvalue_or_else (lhs, lv_assign, complain))
7016 return error_mark_node;
7018 gcc_assert (!TREE_SIDE_EFFECTS (TREE_OPERAND (lhs, 0))
7019 && !TREE_SIDE_EFFECTS (TREE_OPERAND (lhs, 1)));
7021 lhs = build3 (COND_EXPR, TREE_TYPE (lhs),
7022 build2 (TREE_CODE (lhs) == MIN_EXPR ? LE_EXPR : GE_EXPR,
7023 boolean_type_node,
7024 TREE_OPERAND (lhs, 0),
7025 TREE_OPERAND (lhs, 1)),
7026 TREE_OPERAND (lhs, 0),
7027 TREE_OPERAND (lhs, 1));
7028 /* Fall through. */
7030 /* Handle (a ? b : c) used as an "lvalue". */
7031 case COND_EXPR:
7033 /* Produce (a ? (b = rhs) : (c = rhs))
7034 except that the RHS goes through a save-expr
7035 so the code to compute it is only emitted once. */
7036 tree cond;
7037 tree preeval = NULL_TREE;
7039 if (VOID_TYPE_P (TREE_TYPE (rhs)))
7041 if (complain & tf_error)
7042 error ("void value not ignored as it ought to be");
7043 return error_mark_node;
7046 rhs = stabilize_expr (rhs, &preeval);
7048 /* Check this here to avoid odd errors when trying to convert
7049 a throw to the type of the COND_EXPR. */
7050 if (!lvalue_or_else (lhs, lv_assign, complain))
7051 return error_mark_node;
7053 cond = build_conditional_expr
7054 (TREE_OPERAND (lhs, 0),
7055 cp_build_modify_expr (TREE_OPERAND (lhs, 1),
7056 modifycode, rhs, complain),
7057 cp_build_modify_expr (TREE_OPERAND (lhs, 2),
7058 modifycode, rhs, complain),
7059 complain);
7061 if (cond == error_mark_node)
7062 return cond;
7063 /* Make sure the code to compute the rhs comes out
7064 before the split. */
7065 if (preeval)
7066 cond = build2 (COMPOUND_EXPR, TREE_TYPE (lhs), preeval, cond);
7067 return cond;
7070 default:
7071 break;
7074 if (modifycode == INIT_EXPR)
7076 if (BRACE_ENCLOSED_INITIALIZER_P (rhs))
7077 /* Do the default thing. */;
7078 else if (TREE_CODE (rhs) == CONSTRUCTOR)
7080 /* Compound literal. */
7081 if (! same_type_p (TREE_TYPE (rhs), lhstype))
7082 /* Call convert to generate an error; see PR 11063. */
7083 rhs = convert (lhstype, rhs);
7084 result = build2 (INIT_EXPR, lhstype, lhs, rhs);
7085 TREE_SIDE_EFFECTS (result) = 1;
7086 return result;
7088 else if (! MAYBE_CLASS_TYPE_P (lhstype))
7089 /* Do the default thing. */;
7090 else
7092 vec<tree, va_gc> *rhs_vec = make_tree_vector_single (rhs);
7093 result = build_special_member_call (lhs, complete_ctor_identifier,
7094 &rhs_vec, lhstype, LOOKUP_NORMAL,
7095 complain);
7096 release_tree_vector (rhs_vec);
7097 if (result == NULL_TREE)
7098 return error_mark_node;
7099 return result;
7102 else
7104 lhs = require_complete_type_sfinae (lhs, complain);
7105 if (lhs == error_mark_node)
7106 return error_mark_node;
7108 if (modifycode == NOP_EXPR)
7110 if (c_dialect_objc ())
7112 result = objc_maybe_build_modify_expr (lhs, rhs);
7113 if (result)
7114 return result;
7117 /* `operator=' is not an inheritable operator. */
7118 if (! MAYBE_CLASS_TYPE_P (lhstype))
7119 /* Do the default thing. */;
7120 else
7122 result = build_new_op (input_location, MODIFY_EXPR,
7123 LOOKUP_NORMAL, lhs, rhs,
7124 make_node (NOP_EXPR), /*overload=*/NULL,
7125 complain);
7126 if (result == NULL_TREE)
7127 return error_mark_node;
7128 return result;
7130 lhstype = olhstype;
7132 else
7134 tree init = NULL_TREE;
7136 /* A binary op has been requested. Combine the old LHS
7137 value with the RHS producing the value we should actually
7138 store into the LHS. */
7139 gcc_assert (!((TREE_CODE (lhstype) == REFERENCE_TYPE
7140 && MAYBE_CLASS_TYPE_P (TREE_TYPE (lhstype)))
7141 || MAYBE_CLASS_TYPE_P (lhstype)));
7143 /* Preevaluate the RHS to make sure its evaluation is complete
7144 before the lvalue-to-rvalue conversion of the LHS:
7146 [expr.ass] With respect to an indeterminately-sequenced
7147 function call, the operation of a compound assignment is a
7148 single evaluation. [ Note: Therefore, a function call shall
7149 not intervene between the lvalue-to-rvalue conversion and the
7150 side effect associated with any single compound assignment
7151 operator. -- end note ] */
7152 lhs = stabilize_reference (lhs);
7153 if (TREE_SIDE_EFFECTS (rhs))
7154 rhs = mark_rvalue_use (rhs);
7155 rhs = stabilize_expr (rhs, &init);
7156 newrhs = cp_build_binary_op (input_location,
7157 modifycode, lhs, rhs,
7158 complain);
7159 if (newrhs == error_mark_node)
7161 if (complain & tf_error)
7162 error (" in evaluation of %<%Q(%#T, %#T)%>", modifycode,
7163 TREE_TYPE (lhs), TREE_TYPE (rhs));
7164 return error_mark_node;
7167 if (init)
7168 newrhs = build2 (COMPOUND_EXPR, TREE_TYPE (newrhs), init, newrhs);
7170 /* Now it looks like a plain assignment. */
7171 modifycode = NOP_EXPR;
7172 if (c_dialect_objc ())
7174 result = objc_maybe_build_modify_expr (lhs, newrhs);
7175 if (result)
7176 return result;
7179 gcc_assert (TREE_CODE (lhstype) != REFERENCE_TYPE);
7180 gcc_assert (TREE_CODE (TREE_TYPE (newrhs)) != REFERENCE_TYPE);
7183 /* The left-hand side must be an lvalue. */
7184 if (!lvalue_or_else (lhs, lv_assign, complain))
7185 return error_mark_node;
7187 /* Warn about modifying something that is `const'. Don't warn if
7188 this is initialization. */
7189 if (modifycode != INIT_EXPR
7190 && (TREE_READONLY (lhs) || CP_TYPE_CONST_P (lhstype)
7191 /* Functions are not modifiable, even though they are
7192 lvalues. */
7193 || TREE_CODE (TREE_TYPE (lhs)) == FUNCTION_TYPE
7194 || TREE_CODE (TREE_TYPE (lhs)) == METHOD_TYPE
7195 /* If it's an aggregate and any field is const, then it is
7196 effectively const. */
7197 || (CLASS_TYPE_P (lhstype)
7198 && C_TYPE_FIELDS_READONLY (lhstype))))
7200 if (complain & tf_error)
7201 cxx_readonly_error (lhs, lv_assign);
7202 else
7203 return error_mark_node;
7206 /* If storing into a structure or union member, it may have been given a
7207 lowered bitfield type. We need to convert to the declared type first,
7208 so retrieve it now. */
7210 olhstype = unlowered_expr_type (lhs);
7212 /* Convert new value to destination type. */
7214 if (TREE_CODE (lhstype) == ARRAY_TYPE)
7216 int from_array;
7218 if (BRACE_ENCLOSED_INITIALIZER_P (newrhs))
7220 if (modifycode != INIT_EXPR)
7222 if (complain & tf_error)
7223 error ("assigning to an array from an initializer list");
7224 return error_mark_node;
7226 if (check_array_initializer (lhs, lhstype, newrhs))
7227 return error_mark_node;
7228 newrhs = digest_init (lhstype, newrhs, complain);
7229 if (newrhs == error_mark_node)
7230 return error_mark_node;
7233 else if (!same_or_base_type_p (TYPE_MAIN_VARIANT (lhstype),
7234 TYPE_MAIN_VARIANT (TREE_TYPE (newrhs))))
7236 if (complain & tf_error)
7237 error ("incompatible types in assignment of %qT to %qT",
7238 TREE_TYPE (rhs), lhstype);
7239 return error_mark_node;
7242 /* Allow array assignment in compiler-generated code. */
7243 else if (!current_function_decl
7244 || !DECL_DEFAULTED_FN (current_function_decl))
7246 /* This routine is used for both initialization and assignment.
7247 Make sure the diagnostic message differentiates the context. */
7248 if (complain & tf_error)
7250 if (modifycode == INIT_EXPR)
7251 error ("array used as initializer");
7252 else
7253 error ("invalid array assignment");
7255 return error_mark_node;
7258 from_array = TREE_CODE (TREE_TYPE (newrhs)) == ARRAY_TYPE
7259 ? 1 + (modifycode != INIT_EXPR): 0;
7260 return build_vec_init (lhs, NULL_TREE, newrhs,
7261 /*explicit_value_init_p=*/false,
7262 from_array, complain);
7265 if (modifycode == INIT_EXPR)
7266 /* Calls with INIT_EXPR are all direct-initialization, so don't set
7267 LOOKUP_ONLYCONVERTING. */
7268 newrhs = convert_for_initialization (lhs, olhstype, newrhs, LOOKUP_NORMAL,
7269 ICR_INIT, NULL_TREE, 0,
7270 complain);
7271 else
7272 newrhs = convert_for_assignment (olhstype, newrhs, ICR_ASSIGN,
7273 NULL_TREE, 0, complain, LOOKUP_IMPLICIT);
7275 if (!same_type_p (lhstype, olhstype))
7276 newrhs = cp_convert_and_check (lhstype, newrhs, complain);
7278 if (modifycode != INIT_EXPR)
7280 if (TREE_CODE (newrhs) == CALL_EXPR
7281 && TYPE_NEEDS_CONSTRUCTING (lhstype))
7282 newrhs = build_cplus_new (lhstype, newrhs, complain);
7284 /* Can't initialize directly from a TARGET_EXPR, since that would
7285 cause the lhs to be constructed twice, and possibly result in
7286 accidental self-initialization. So we force the TARGET_EXPR to be
7287 expanded without a target. */
7288 if (TREE_CODE (newrhs) == TARGET_EXPR)
7289 newrhs = build2 (COMPOUND_EXPR, TREE_TYPE (newrhs), newrhs,
7290 TREE_OPERAND (newrhs, 0));
7293 if (newrhs == error_mark_node)
7294 return error_mark_node;
7296 if (c_dialect_objc () && flag_objc_gc)
7298 result = objc_generate_write_barrier (lhs, modifycode, newrhs);
7300 if (result)
7301 return result;
7304 result = build2 (modifycode == NOP_EXPR ? MODIFY_EXPR : INIT_EXPR,
7305 lhstype, lhs, newrhs);
7307 TREE_SIDE_EFFECTS (result) = 1;
7308 if (!plain_assign)
7309 TREE_NO_WARNING (result) = 1;
7311 return result;
7314 tree
7315 build_x_modify_expr (location_t loc, tree lhs, enum tree_code modifycode,
7316 tree rhs, tsubst_flags_t complain)
7318 if (processing_template_decl)
7319 return build_min_nt_loc (loc, MODOP_EXPR, lhs,
7320 build_min_nt_loc (loc, modifycode, NULL_TREE,
7321 NULL_TREE), rhs);
7323 if (modifycode != NOP_EXPR)
7325 tree rval = build_new_op (loc, MODIFY_EXPR, LOOKUP_NORMAL, lhs, rhs,
7326 make_node (modifycode), /*overload=*/NULL,
7327 complain);
7328 if (rval)
7330 TREE_NO_WARNING (rval) = 1;
7331 return rval;
7334 return cp_build_modify_expr (lhs, modifycode, rhs, complain);
7337 /* Helper function for get_delta_difference which assumes FROM is a base
7338 class of TO. Returns a delta for the conversion of pointer-to-member
7339 of FROM to pointer-to-member of TO. If the conversion is invalid and
7340 tf_error is not set in COMPLAIN returns error_mark_node, otherwise
7341 returns zero. If FROM is not a base class of TO, returns NULL_TREE.
7342 If C_CAST_P is true, this conversion is taking place as part of a
7343 C-style cast. */
7345 static tree
7346 get_delta_difference_1 (tree from, tree to, bool c_cast_p,
7347 tsubst_flags_t complain)
7349 tree binfo;
7350 base_kind kind;
7352 binfo = lookup_base (to, from, c_cast_p ? ba_unique : ba_check,
7353 &kind, complain);
7355 if (binfo == error_mark_node)
7357 if (!(complain & tf_error))
7358 return error_mark_node;
7360 error (" in pointer to member function conversion");
7361 return size_zero_node;
7363 else if (binfo)
7365 if (kind != bk_via_virtual)
7366 return BINFO_OFFSET (binfo);
7367 else
7368 /* FROM is a virtual base class of TO. Issue an error or warning
7369 depending on whether or not this is a reinterpret cast. */
7371 if (!(complain & tf_error))
7372 return error_mark_node;
7374 error ("pointer to member conversion via virtual base %qT",
7375 BINFO_TYPE (binfo_from_vbase (binfo)));
7377 return size_zero_node;
7380 else
7381 return NULL_TREE;
7384 /* Get difference in deltas for different pointer to member function
7385 types. If the conversion is invalid and tf_error is not set in
7386 COMPLAIN, returns error_mark_node, otherwise returns an integer
7387 constant of type PTRDIFF_TYPE_NODE and its value is zero if the
7388 conversion is invalid. If ALLOW_INVERSE_P is true, then allow reverse
7389 conversions as well. If C_CAST_P is true this conversion is taking
7390 place as part of a C-style cast.
7392 Note that the naming of FROM and TO is kind of backwards; the return
7393 value is what we add to a TO in order to get a FROM. They are named
7394 this way because we call this function to find out how to convert from
7395 a pointer to member of FROM to a pointer to member of TO. */
7397 static tree
7398 get_delta_difference (tree from, tree to,
7399 bool allow_inverse_p,
7400 bool c_cast_p, tsubst_flags_t complain)
7402 tree result;
7404 if (same_type_ignoring_top_level_qualifiers_p (from, to))
7405 /* Pointer to member of incomplete class is permitted*/
7406 result = size_zero_node;
7407 else
7408 result = get_delta_difference_1 (from, to, c_cast_p, complain);
7410 if (result == error_mark_node)
7411 return error_mark_node;
7413 if (!result)
7415 if (!allow_inverse_p)
7417 if (!(complain & tf_error))
7418 return error_mark_node;
7420 error_not_base_type (from, to);
7421 error (" in pointer to member conversion");
7422 result = size_zero_node;
7424 else
7426 result = get_delta_difference_1 (to, from, c_cast_p, complain);
7428 if (result == error_mark_node)
7429 return error_mark_node;
7431 if (result)
7432 result = size_diffop_loc (input_location,
7433 size_zero_node, result);
7434 else
7436 if (!(complain & tf_error))
7437 return error_mark_node;
7439 error_not_base_type (from, to);
7440 error (" in pointer to member conversion");
7441 result = size_zero_node;
7446 return fold_if_not_in_template (convert_to_integer (ptrdiff_type_node,
7447 result));
7450 /* Return a constructor for the pointer-to-member-function TYPE using
7451 the other components as specified. */
7453 tree
7454 build_ptrmemfunc1 (tree type, tree delta, tree pfn)
7456 tree u = NULL_TREE;
7457 tree delta_field;
7458 tree pfn_field;
7459 vec<constructor_elt, va_gc> *v;
7461 /* Pull the FIELD_DECLs out of the type. */
7462 pfn_field = TYPE_FIELDS (type);
7463 delta_field = DECL_CHAIN (pfn_field);
7465 /* Make sure DELTA has the type we want. */
7466 delta = convert_and_check (delta_type_node, delta);
7468 /* Convert to the correct target type if necessary. */
7469 pfn = fold_convert (TREE_TYPE (pfn_field), pfn);
7471 /* Finish creating the initializer. */
7472 vec_alloc (v, 2);
7473 CONSTRUCTOR_APPEND_ELT(v, pfn_field, pfn);
7474 CONSTRUCTOR_APPEND_ELT(v, delta_field, delta);
7475 u = build_constructor (type, v);
7476 TREE_CONSTANT (u) = TREE_CONSTANT (pfn) & TREE_CONSTANT (delta);
7477 TREE_STATIC (u) = (TREE_CONSTANT (u)
7478 && (initializer_constant_valid_p (pfn, TREE_TYPE (pfn))
7479 != NULL_TREE)
7480 && (initializer_constant_valid_p (delta, TREE_TYPE (delta))
7481 != NULL_TREE));
7482 return u;
7485 /* Build a constructor for a pointer to member function. It can be
7486 used to initialize global variables, local variable, or used
7487 as a value in expressions. TYPE is the POINTER to METHOD_TYPE we
7488 want to be.
7490 If FORCE is nonzero, then force this conversion, even if
7491 we would rather not do it. Usually set when using an explicit
7492 cast. A C-style cast is being processed iff C_CAST_P is true.
7494 Return error_mark_node, if something goes wrong. */
7496 tree
7497 build_ptrmemfunc (tree type, tree pfn, int force, bool c_cast_p,
7498 tsubst_flags_t complain)
7500 tree fn;
7501 tree pfn_type;
7502 tree to_type;
7504 if (error_operand_p (pfn))
7505 return error_mark_node;
7507 pfn_type = TREE_TYPE (pfn);
7508 to_type = build_ptrmemfunc_type (type);
7510 /* Handle multiple conversions of pointer to member functions. */
7511 if (TYPE_PTRMEMFUNC_P (pfn_type))
7513 tree delta = NULL_TREE;
7514 tree npfn = NULL_TREE;
7515 tree n;
7517 if (!force
7518 && !can_convert_arg (to_type, TREE_TYPE (pfn), pfn,
7519 LOOKUP_NORMAL, complain))
7520 error ("invalid conversion to type %qT from type %qT",
7521 to_type, pfn_type);
7523 n = get_delta_difference (TYPE_PTRMEMFUNC_OBJECT_TYPE (pfn_type),
7524 TYPE_PTRMEMFUNC_OBJECT_TYPE (to_type),
7525 force,
7526 c_cast_p, complain);
7527 if (n == error_mark_node)
7528 return error_mark_node;
7530 /* We don't have to do any conversion to convert a
7531 pointer-to-member to its own type. But, we don't want to
7532 just return a PTRMEM_CST if there's an explicit cast; that
7533 cast should make the expression an invalid template argument. */
7534 if (TREE_CODE (pfn) != PTRMEM_CST)
7536 if (same_type_p (to_type, pfn_type))
7537 return pfn;
7538 else if (integer_zerop (n))
7539 return build_reinterpret_cast (to_type, pfn,
7540 complain);
7543 if (TREE_SIDE_EFFECTS (pfn))
7544 pfn = save_expr (pfn);
7546 /* Obtain the function pointer and the current DELTA. */
7547 if (TREE_CODE (pfn) == PTRMEM_CST)
7548 expand_ptrmemfunc_cst (pfn, &delta, &npfn);
7549 else
7551 npfn = build_ptrmemfunc_access_expr (pfn, pfn_identifier);
7552 delta = build_ptrmemfunc_access_expr (pfn, delta_identifier);
7555 /* Just adjust the DELTA field. */
7556 gcc_assert (same_type_ignoring_top_level_qualifiers_p
7557 (TREE_TYPE (delta), ptrdiff_type_node));
7558 if (TARGET_PTRMEMFUNC_VBIT_LOCATION == ptrmemfunc_vbit_in_delta)
7559 n = cp_build_binary_op (input_location,
7560 LSHIFT_EXPR, n, integer_one_node,
7561 complain);
7562 delta = cp_build_binary_op (input_location,
7563 PLUS_EXPR, delta, n, complain);
7564 return build_ptrmemfunc1 (to_type, delta, npfn);
7567 /* Handle null pointer to member function conversions. */
7568 if (null_ptr_cst_p (pfn))
7570 pfn = build_c_cast (input_location, type, nullptr_node);
7571 return build_ptrmemfunc1 (to_type,
7572 integer_zero_node,
7573 pfn);
7576 if (type_unknown_p (pfn))
7577 return instantiate_type (type, pfn, complain);
7579 fn = TREE_OPERAND (pfn, 0);
7580 gcc_assert (TREE_CODE (fn) == FUNCTION_DECL
7581 /* In a template, we will have preserved the
7582 OFFSET_REF. */
7583 || (processing_template_decl && TREE_CODE (fn) == OFFSET_REF));
7584 return make_ptrmem_cst (to_type, fn);
7587 /* Return the DELTA, IDX, PFN, and DELTA2 values for the PTRMEM_CST
7588 given by CST.
7590 ??? There is no consistency as to the types returned for the above
7591 values. Some code acts as if it were a sizetype and some as if it were
7592 integer_type_node. */
7594 void
7595 expand_ptrmemfunc_cst (tree cst, tree *delta, tree *pfn)
7597 tree type = TREE_TYPE (cst);
7598 tree fn = PTRMEM_CST_MEMBER (cst);
7599 tree ptr_class, fn_class;
7601 gcc_assert (TREE_CODE (fn) == FUNCTION_DECL);
7603 /* The class that the function belongs to. */
7604 fn_class = DECL_CONTEXT (fn);
7606 /* The class that we're creating a pointer to member of. */
7607 ptr_class = TYPE_PTRMEMFUNC_OBJECT_TYPE (type);
7609 /* First, calculate the adjustment to the function's class. */
7610 *delta = get_delta_difference (fn_class, ptr_class, /*force=*/0,
7611 /*c_cast_p=*/0, tf_warning_or_error);
7613 if (!DECL_VIRTUAL_P (fn))
7614 *pfn = convert (TYPE_PTRMEMFUNC_FN_TYPE (type),
7615 build_addr_func (fn, tf_warning_or_error));
7616 else
7618 /* If we're dealing with a virtual function, we have to adjust 'this'
7619 again, to point to the base which provides the vtable entry for
7620 fn; the call will do the opposite adjustment. */
7621 tree orig_class = DECL_CONTEXT (fn);
7622 tree binfo = binfo_or_else (orig_class, fn_class);
7623 *delta = build2 (PLUS_EXPR, TREE_TYPE (*delta),
7624 *delta, BINFO_OFFSET (binfo));
7625 *delta = fold_if_not_in_template (*delta);
7627 /* We set PFN to the vtable offset at which the function can be
7628 found, plus one (unless ptrmemfunc_vbit_in_delta, in which
7629 case delta is shifted left, and then incremented). */
7630 *pfn = DECL_VINDEX (fn);
7631 *pfn = build2 (MULT_EXPR, integer_type_node, *pfn,
7632 TYPE_SIZE_UNIT (vtable_entry_type));
7633 *pfn = fold_if_not_in_template (*pfn);
7635 switch (TARGET_PTRMEMFUNC_VBIT_LOCATION)
7637 case ptrmemfunc_vbit_in_pfn:
7638 *pfn = build2 (PLUS_EXPR, integer_type_node, *pfn,
7639 integer_one_node);
7640 *pfn = fold_if_not_in_template (*pfn);
7641 break;
7643 case ptrmemfunc_vbit_in_delta:
7644 *delta = build2 (LSHIFT_EXPR, TREE_TYPE (*delta),
7645 *delta, integer_one_node);
7646 *delta = fold_if_not_in_template (*delta);
7647 *delta = build2 (PLUS_EXPR, TREE_TYPE (*delta),
7648 *delta, integer_one_node);
7649 *delta = fold_if_not_in_template (*delta);
7650 break;
7652 default:
7653 gcc_unreachable ();
7656 *pfn = build_nop (TYPE_PTRMEMFUNC_FN_TYPE (type), *pfn);
7657 *pfn = fold_if_not_in_template (*pfn);
7661 /* Return an expression for PFN from the pointer-to-member function
7662 given by T. */
7664 static tree
7665 pfn_from_ptrmemfunc (tree t)
7667 if (TREE_CODE (t) == PTRMEM_CST)
7669 tree delta;
7670 tree pfn;
7672 expand_ptrmemfunc_cst (t, &delta, &pfn);
7673 if (pfn)
7674 return pfn;
7677 return build_ptrmemfunc_access_expr (t, pfn_identifier);
7680 /* Return an expression for DELTA from the pointer-to-member function
7681 given by T. */
7683 static tree
7684 delta_from_ptrmemfunc (tree t)
7686 if (TREE_CODE (t) == PTRMEM_CST)
7688 tree delta;
7689 tree pfn;
7691 expand_ptrmemfunc_cst (t, &delta, &pfn);
7692 if (delta)
7693 return delta;
7696 return build_ptrmemfunc_access_expr (t, delta_identifier);
7699 /* Convert value RHS to type TYPE as preparation for an assignment to
7700 an lvalue of type TYPE. ERRTYPE indicates what kind of error the
7701 implicit conversion is. If FNDECL is non-NULL, we are doing the
7702 conversion in order to pass the PARMNUMth argument of FNDECL.
7703 If FNDECL is NULL, we are doing the conversion in function pointer
7704 argument passing, conversion in initialization, etc. */
7706 static tree
7707 convert_for_assignment (tree type, tree rhs,
7708 impl_conv_rhs errtype, tree fndecl, int parmnum,
7709 tsubst_flags_t complain, int flags)
7711 tree rhstype;
7712 enum tree_code coder;
7714 /* Strip NON_LVALUE_EXPRs since we aren't using as an lvalue. */
7715 if (TREE_CODE (rhs) == NON_LVALUE_EXPR)
7716 rhs = TREE_OPERAND (rhs, 0);
7718 rhstype = TREE_TYPE (rhs);
7719 coder = TREE_CODE (rhstype);
7721 if (TREE_CODE (type) == VECTOR_TYPE && coder == VECTOR_TYPE
7722 && vector_types_convertible_p (type, rhstype, true))
7724 rhs = mark_rvalue_use (rhs);
7725 return convert (type, rhs);
7728 if (rhs == error_mark_node || rhstype == error_mark_node)
7729 return error_mark_node;
7730 if (TREE_CODE (rhs) == TREE_LIST && TREE_VALUE (rhs) == error_mark_node)
7731 return error_mark_node;
7733 /* The RHS of an assignment cannot have void type. */
7734 if (coder == VOID_TYPE)
7736 if (complain & tf_error)
7737 error ("void value not ignored as it ought to be");
7738 return error_mark_node;
7741 if (c_dialect_objc ())
7743 int parmno;
7744 tree selector;
7745 tree rname = fndecl;
7747 switch (errtype)
7749 case ICR_ASSIGN:
7750 parmno = -1;
7751 break;
7752 case ICR_INIT:
7753 parmno = -2;
7754 break;
7755 default:
7756 selector = objc_message_selector ();
7757 parmno = parmnum;
7758 if (selector && parmno > 1)
7760 rname = selector;
7761 parmno -= 1;
7765 if (objc_compare_types (type, rhstype, parmno, rname))
7767 rhs = mark_rvalue_use (rhs);
7768 return convert (type, rhs);
7772 /* [expr.ass]
7774 The expression is implicitly converted (clause _conv_) to the
7775 cv-unqualified type of the left operand.
7777 We allow bad conversions here because by the time we get to this point
7778 we are committed to doing the conversion. If we end up doing a bad
7779 conversion, convert_like will complain. */
7780 if (!can_convert_arg_bad (type, rhstype, rhs, flags, complain))
7782 /* When -Wno-pmf-conversions is use, we just silently allow
7783 conversions from pointers-to-members to plain pointers. If
7784 the conversion doesn't work, cp_convert will complain. */
7785 if (!warn_pmf2ptr
7786 && TYPE_PTR_P (type)
7787 && TYPE_PTRMEMFUNC_P (rhstype))
7788 rhs = cp_convert (strip_top_quals (type), rhs, complain);
7789 else
7791 if (complain & tf_error)
7793 /* If the right-hand side has unknown type, then it is an
7794 overloaded function. Call instantiate_type to get error
7795 messages. */
7796 if (rhstype == unknown_type_node)
7797 instantiate_type (type, rhs, tf_warning_or_error);
7798 else if (fndecl)
7799 error ("cannot convert %qT to %qT for argument %qP to %qD",
7800 rhstype, type, parmnum, fndecl);
7801 else
7802 switch (errtype)
7804 case ICR_DEFAULT_ARGUMENT:
7805 error ("cannot convert %qT to %qT in default argument",
7806 rhstype, type);
7807 break;
7808 case ICR_ARGPASS:
7809 error ("cannot convert %qT to %qT in argument passing",
7810 rhstype, type);
7811 break;
7812 case ICR_CONVERTING:
7813 error ("cannot convert %qT to %qT",
7814 rhstype, type);
7815 break;
7816 case ICR_INIT:
7817 error ("cannot convert %qT to %qT in initialization",
7818 rhstype, type);
7819 break;
7820 case ICR_RETURN:
7821 error ("cannot convert %qT to %qT in return",
7822 rhstype, type);
7823 break;
7824 case ICR_ASSIGN:
7825 error ("cannot convert %qT to %qT in assignment",
7826 rhstype, type);
7827 break;
7828 default:
7829 gcc_unreachable();
7832 return error_mark_node;
7835 if (warn_suggest_attribute_format)
7837 const enum tree_code codel = TREE_CODE (type);
7838 if ((codel == POINTER_TYPE || codel == REFERENCE_TYPE)
7839 && coder == codel
7840 && check_missing_format_attribute (type, rhstype)
7841 && (complain & tf_warning))
7842 switch (errtype)
7844 case ICR_ARGPASS:
7845 case ICR_DEFAULT_ARGUMENT:
7846 if (fndecl)
7847 warning (OPT_Wsuggest_attribute_format,
7848 "parameter %qP of %qD might be a candidate "
7849 "for a format attribute", parmnum, fndecl);
7850 else
7851 warning (OPT_Wsuggest_attribute_format,
7852 "parameter might be a candidate "
7853 "for a format attribute");
7854 break;
7855 case ICR_CONVERTING:
7856 warning (OPT_Wsuggest_attribute_format,
7857 "target of conversion might be a candidate "
7858 "for a format attribute");
7859 break;
7860 case ICR_INIT:
7861 warning (OPT_Wsuggest_attribute_format,
7862 "target of initialization might be a candidate "
7863 "for a format attribute");
7864 break;
7865 case ICR_RETURN:
7866 warning (OPT_Wsuggest_attribute_format,
7867 "return type might be a candidate "
7868 "for a format attribute");
7869 break;
7870 case ICR_ASSIGN:
7871 warning (OPT_Wsuggest_attribute_format,
7872 "left-hand side of assignment might be a candidate "
7873 "for a format attribute");
7874 break;
7875 default:
7876 gcc_unreachable();
7880 /* If -Wparentheses, warn about a = b = c when a has type bool and b
7881 does not. */
7882 if (warn_parentheses
7883 && TREE_CODE (type) == BOOLEAN_TYPE
7884 && TREE_CODE (rhs) == MODIFY_EXPR
7885 && !TREE_NO_WARNING (rhs)
7886 && TREE_CODE (TREE_TYPE (rhs)) != BOOLEAN_TYPE
7887 && (complain & tf_warning))
7889 location_t loc = EXPR_LOC_OR_HERE (rhs);
7891 warning_at (loc, OPT_Wparentheses,
7892 "suggest parentheses around assignment used as truth value");
7893 TREE_NO_WARNING (rhs) = 1;
7896 return perform_implicit_conversion_flags (strip_top_quals (type), rhs,
7897 complain, flags);
7900 /* Convert RHS to be of type TYPE.
7901 If EXP is nonzero, it is the target of the initialization.
7902 ERRTYPE indicates what kind of error the implicit conversion is.
7904 Two major differences between the behavior of
7905 `convert_for_assignment' and `convert_for_initialization'
7906 are that references are bashed in the former, while
7907 copied in the latter, and aggregates are assigned in
7908 the former (operator=) while initialized in the
7909 latter (X(X&)).
7911 If using constructor make sure no conversion operator exists, if one does
7912 exist, an ambiguity exists. */
7914 tree
7915 convert_for_initialization (tree exp, tree type, tree rhs, int flags,
7916 impl_conv_rhs errtype, tree fndecl, int parmnum,
7917 tsubst_flags_t complain)
7919 enum tree_code codel = TREE_CODE (type);
7920 tree rhstype;
7921 enum tree_code coder;
7923 /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
7924 Strip such NOP_EXPRs, since RHS is used in non-lvalue context. */
7925 if (TREE_CODE (rhs) == NOP_EXPR
7926 && TREE_TYPE (rhs) == TREE_TYPE (TREE_OPERAND (rhs, 0))
7927 && codel != REFERENCE_TYPE)
7928 rhs = TREE_OPERAND (rhs, 0);
7930 if (type == error_mark_node
7931 || rhs == error_mark_node
7932 || (TREE_CODE (rhs) == TREE_LIST && TREE_VALUE (rhs) == error_mark_node))
7933 return error_mark_node;
7935 if ((TREE_CODE (TREE_TYPE (rhs)) == ARRAY_TYPE
7936 && TREE_CODE (type) != ARRAY_TYPE
7937 && (TREE_CODE (type) != REFERENCE_TYPE
7938 || TREE_CODE (TREE_TYPE (type)) != ARRAY_TYPE))
7939 || (TREE_CODE (TREE_TYPE (rhs)) == FUNCTION_TYPE
7940 && (TREE_CODE (type) != REFERENCE_TYPE
7941 || TREE_CODE (TREE_TYPE (type)) != FUNCTION_TYPE))
7942 || TREE_CODE (TREE_TYPE (rhs)) == METHOD_TYPE)
7943 rhs = decay_conversion (rhs, complain);
7945 rhstype = TREE_TYPE (rhs);
7946 coder = TREE_CODE (rhstype);
7948 if (coder == ERROR_MARK)
7949 return error_mark_node;
7951 /* We accept references to incomplete types, so we can
7952 return here before checking if RHS is of complete type. */
7954 if (codel == REFERENCE_TYPE)
7956 /* This should eventually happen in convert_arguments. */
7957 int savew = 0, savee = 0;
7959 if (fndecl)
7960 savew = warningcount, savee = errorcount;
7961 rhs = initialize_reference (type, rhs, flags, complain);
7962 if (fndecl)
7964 if (warningcount > savew)
7965 warning (0, "in passing argument %P of %q+D", parmnum, fndecl);
7966 else if (errorcount > savee)
7967 error ("in passing argument %P of %q+D", parmnum, fndecl);
7969 return rhs;
7972 if (exp != 0)
7973 exp = require_complete_type_sfinae (exp, complain);
7974 if (exp == error_mark_node)
7975 return error_mark_node;
7977 rhstype = non_reference (rhstype);
7979 type = complete_type (type);
7981 if (DIRECT_INIT_EXPR_P (type, rhs))
7982 /* Don't try to do copy-initialization if we already have
7983 direct-initialization. */
7984 return rhs;
7986 if (MAYBE_CLASS_TYPE_P (type))
7987 return perform_implicit_conversion_flags (type, rhs, complain, flags);
7989 return convert_for_assignment (type, rhs, errtype, fndecl, parmnum,
7990 complain, flags);
7993 /* If RETVAL is the address of, or a reference to, a local variable or
7994 temporary give an appropriate warning. */
7996 static void
7997 maybe_warn_about_returning_address_of_local (tree retval)
7999 tree valtype = TREE_TYPE (DECL_RESULT (current_function_decl));
8000 tree whats_returned = retval;
8002 for (;;)
8004 if (TREE_CODE (whats_returned) == COMPOUND_EXPR)
8005 whats_returned = TREE_OPERAND (whats_returned, 1);
8006 else if (CONVERT_EXPR_P (whats_returned)
8007 || TREE_CODE (whats_returned) == NON_LVALUE_EXPR)
8008 whats_returned = TREE_OPERAND (whats_returned, 0);
8009 else
8010 break;
8013 if (TREE_CODE (whats_returned) != ADDR_EXPR)
8014 return;
8015 whats_returned = TREE_OPERAND (whats_returned, 0);
8017 if (TREE_CODE (valtype) == REFERENCE_TYPE)
8019 if (TREE_CODE (whats_returned) == AGGR_INIT_EXPR
8020 || TREE_CODE (whats_returned) == TARGET_EXPR)
8022 warning (OPT_Wreturn_local_addr, "returning reference to temporary");
8023 return;
8025 if (TREE_CODE (whats_returned) == VAR_DECL
8026 && DECL_NAME (whats_returned)
8027 && TEMP_NAME_P (DECL_NAME (whats_returned)))
8029 warning (OPT_Wreturn_local_addr, "reference to non-lvalue returned");
8030 return;
8034 while (TREE_CODE (whats_returned) == COMPONENT_REF
8035 || TREE_CODE (whats_returned) == ARRAY_REF)
8036 whats_returned = TREE_OPERAND (whats_returned, 0);
8038 if (DECL_P (whats_returned)
8039 && DECL_NAME (whats_returned)
8040 && DECL_FUNCTION_SCOPE_P (whats_returned)
8041 && !(TREE_STATIC (whats_returned)
8042 || TREE_PUBLIC (whats_returned)))
8044 if (TREE_CODE (valtype) == REFERENCE_TYPE)
8045 warning (OPT_Wreturn_local_addr, "reference to local variable %q+D returned",
8046 whats_returned);
8047 else
8048 warning (OPT_Wreturn_local_addr, "address of local variable %q+D returned",
8049 whats_returned);
8050 return;
8054 /* Check that returning RETVAL from the current function is valid.
8055 Return an expression explicitly showing all conversions required to
8056 change RETVAL into the function return type, and to assign it to
8057 the DECL_RESULT for the function. Set *NO_WARNING to true if
8058 code reaches end of non-void function warning shouldn't be issued
8059 on this RETURN_EXPR. */
8061 tree
8062 check_return_expr (tree retval, bool *no_warning)
8064 tree result;
8065 /* The type actually returned by the function. */
8066 tree valtype;
8067 /* The type the function is declared to return, or void if
8068 the declared type is incomplete. */
8069 tree functype;
8070 int fn_returns_value_p;
8071 bool named_return_value_okay_p;
8073 *no_warning = false;
8075 /* A `volatile' function is one that isn't supposed to return, ever.
8076 (This is a G++ extension, used to get better code for functions
8077 that call the `volatile' function.) */
8078 if (TREE_THIS_VOLATILE (current_function_decl))
8079 warning (0, "function declared %<noreturn%> has a %<return%> statement");
8081 /* Check for various simple errors. */
8082 if (DECL_DESTRUCTOR_P (current_function_decl))
8084 if (retval)
8085 error ("returning a value from a destructor");
8086 return NULL_TREE;
8088 else if (DECL_CONSTRUCTOR_P (current_function_decl))
8090 if (in_function_try_handler)
8091 /* If a return statement appears in a handler of the
8092 function-try-block of a constructor, the program is ill-formed. */
8093 error ("cannot return from a handler of a function-try-block of a constructor");
8094 else if (retval)
8095 /* You can't return a value from a constructor. */
8096 error ("returning a value from a constructor");
8097 return NULL_TREE;
8100 if (processing_template_decl)
8102 current_function_returns_value = 1;
8103 if (check_for_bare_parameter_packs (retval))
8104 retval = error_mark_node;
8105 return retval;
8108 functype = TREE_TYPE (TREE_TYPE (current_function_decl));
8110 /* Deduce auto return type from a return statement. */
8111 if (current_function_auto_return_pattern)
8113 tree auto_node;
8114 tree type;
8116 if (!retval && !is_auto (current_function_auto_return_pattern))
8118 /* Give a helpful error message. */
8119 error ("return-statement with no value, in function returning %qT",
8120 current_function_auto_return_pattern);
8121 inform (input_location, "only plain %<auto%> return type can be "
8122 "deduced to %<void%>");
8123 type = error_mark_node;
8125 else
8127 if (!retval)
8128 retval = void_zero_node;
8129 auto_node = type_uses_auto (current_function_auto_return_pattern);
8130 type = do_auto_deduction (current_function_auto_return_pattern,
8131 retval, auto_node);
8134 if (type == error_mark_node)
8135 /* Leave it. */;
8136 else if (functype == current_function_auto_return_pattern)
8137 apply_deduced_return_type (current_function_decl, type);
8138 else
8139 /* A mismatch should have been diagnosed in do_auto_deduction. */
8140 gcc_assert (same_type_p (type, functype));
8141 functype = type;
8144 /* When no explicit return-value is given in a function with a named
8145 return value, the named return value is used. */
8146 result = DECL_RESULT (current_function_decl);
8147 valtype = TREE_TYPE (result);
8148 gcc_assert (valtype != NULL_TREE);
8149 fn_returns_value_p = !VOID_TYPE_P (valtype);
8150 if (!retval && DECL_NAME (result) && fn_returns_value_p)
8151 retval = result;
8153 /* Check for a return statement with no return value in a function
8154 that's supposed to return a value. */
8155 if (!retval && fn_returns_value_p)
8157 if (functype != error_mark_node)
8158 permerror (input_location, "return-statement with no value, in "
8159 "function returning %qT", valtype);
8160 /* Remember that this function did return. */
8161 current_function_returns_value = 1;
8162 /* And signal caller that TREE_NO_WARNING should be set on the
8163 RETURN_EXPR to avoid control reaches end of non-void function
8164 warnings in tree-cfg.c. */
8165 *no_warning = true;
8167 /* Check for a return statement with a value in a function that
8168 isn't supposed to return a value. */
8169 else if (retval && !fn_returns_value_p)
8171 if (VOID_TYPE_P (TREE_TYPE (retval)))
8172 /* You can return a `void' value from a function of `void'
8173 type. In that case, we have to evaluate the expression for
8174 its side-effects. */
8175 finish_expr_stmt (retval);
8176 else
8177 permerror (input_location, "return-statement with a value, in function "
8178 "returning 'void'");
8179 current_function_returns_null = 1;
8181 /* There's really no value to return, after all. */
8182 return NULL_TREE;
8184 else if (!retval)
8185 /* Remember that this function can sometimes return without a
8186 value. */
8187 current_function_returns_null = 1;
8188 else
8189 /* Remember that this function did return a value. */
8190 current_function_returns_value = 1;
8192 /* Check for erroneous operands -- but after giving ourselves a
8193 chance to provide an error about returning a value from a void
8194 function. */
8195 if (error_operand_p (retval))
8197 current_function_return_value = error_mark_node;
8198 return error_mark_node;
8201 /* Only operator new(...) throw(), can return NULL [expr.new/13]. */
8202 if ((DECL_OVERLOADED_OPERATOR_P (current_function_decl) == NEW_EXPR
8203 || DECL_OVERLOADED_OPERATOR_P (current_function_decl) == VEC_NEW_EXPR)
8204 && !TYPE_NOTHROW_P (TREE_TYPE (current_function_decl))
8205 && ! flag_check_new
8206 && retval && null_ptr_cst_p (retval))
8207 warning (0, "%<operator new%> must not return NULL unless it is "
8208 "declared %<throw()%> (or -fcheck-new is in effect)");
8210 /* Effective C++ rule 15. See also start_function. */
8211 if (warn_ecpp
8212 && DECL_NAME (current_function_decl) == ansi_assopname(NOP_EXPR))
8214 bool warn = true;
8216 /* The function return type must be a reference to the current
8217 class. */
8218 if (TREE_CODE (valtype) == REFERENCE_TYPE
8219 && same_type_ignoring_top_level_qualifiers_p
8220 (TREE_TYPE (valtype), TREE_TYPE (current_class_ref)))
8222 /* Returning '*this' is obviously OK. */
8223 if (retval == current_class_ref)
8224 warn = false;
8225 /* If we are calling a function whose return type is the same of
8226 the current class reference, it is ok. */
8227 else if (TREE_CODE (retval) == INDIRECT_REF
8228 && TREE_CODE (TREE_OPERAND (retval, 0)) == CALL_EXPR)
8229 warn = false;
8232 if (warn)
8233 warning (OPT_Weffc__, "%<operator=%> should return a reference to %<*this%>");
8236 /* The fabled Named Return Value optimization, as per [class.copy]/15:
8238 [...] For a function with a class return type, if the expression
8239 in the return statement is the name of a local object, and the cv-
8240 unqualified type of the local object is the same as the function
8241 return type, an implementation is permitted to omit creating the tem-
8242 porary object to hold the function return value [...]
8244 So, if this is a value-returning function that always returns the same
8245 local variable, remember it.
8247 It might be nice to be more flexible, and choose the first suitable
8248 variable even if the function sometimes returns something else, but
8249 then we run the risk of clobbering the variable we chose if the other
8250 returned expression uses the chosen variable somehow. And people expect
8251 this restriction, anyway. (jason 2000-11-19)
8253 See finish_function and finalize_nrv for the rest of this optimization. */
8255 named_return_value_okay_p =
8256 (retval != NULL_TREE
8257 /* Must be a local, automatic variable. */
8258 && TREE_CODE (retval) == VAR_DECL
8259 && DECL_CONTEXT (retval) == current_function_decl
8260 && ! TREE_STATIC (retval)
8261 && ! DECL_ANON_UNION_VAR_P (retval)
8262 && (DECL_ALIGN (retval) >= DECL_ALIGN (result))
8263 /* The cv-unqualified type of the returned value must be the
8264 same as the cv-unqualified return type of the
8265 function. */
8266 && same_type_p ((TYPE_MAIN_VARIANT (TREE_TYPE (retval))),
8267 (TYPE_MAIN_VARIANT (functype)))
8268 /* And the returned value must be non-volatile. */
8269 && ! TYPE_VOLATILE (TREE_TYPE (retval)));
8271 if (fn_returns_value_p && flag_elide_constructors)
8273 if (named_return_value_okay_p
8274 && (current_function_return_value == NULL_TREE
8275 || current_function_return_value == retval))
8276 current_function_return_value = retval;
8277 else
8278 current_function_return_value = error_mark_node;
8281 /* We don't need to do any conversions when there's nothing being
8282 returned. */
8283 if (!retval)
8284 return NULL_TREE;
8286 /* Do any required conversions. */
8287 if (retval == result || DECL_CONSTRUCTOR_P (current_function_decl))
8288 /* No conversions are required. */
8290 else
8292 int flags = LOOKUP_NORMAL | LOOKUP_ONLYCONVERTING;
8294 /* The functype's return type will have been set to void, if it
8295 was an incomplete type. Just treat this as 'return;' */
8296 if (VOID_TYPE_P (functype))
8297 return error_mark_node;
8299 /* Under C++0x [12.8/16 class.copy], a returned lvalue is sometimes
8300 treated as an rvalue for the purposes of overload resolution to
8301 favor move constructors over copy constructors.
8303 Note that these conditions are similar to, but not as strict as,
8304 the conditions for the named return value optimization. */
8305 if ((cxx_dialect != cxx98)
8306 && (TREE_CODE (retval) == VAR_DECL
8307 || TREE_CODE (retval) == PARM_DECL)
8308 && DECL_CONTEXT (retval) == current_function_decl
8309 && !TREE_STATIC (retval)
8310 && same_type_p ((TYPE_MAIN_VARIANT (TREE_TYPE (retval))),
8311 (TYPE_MAIN_VARIANT (functype)))
8312 /* This is only interesting for class type. */
8313 && CLASS_TYPE_P (functype))
8314 flags = flags | LOOKUP_PREFER_RVALUE;
8316 /* First convert the value to the function's return type, then
8317 to the type of return value's location to handle the
8318 case that functype is smaller than the valtype. */
8319 retval = convert_for_initialization
8320 (NULL_TREE, functype, retval, flags, ICR_RETURN, NULL_TREE, 0,
8321 tf_warning_or_error);
8322 retval = convert (valtype, retval);
8324 /* If the conversion failed, treat this just like `return;'. */
8325 if (retval == error_mark_node)
8326 return retval;
8327 /* We can't initialize a register from a AGGR_INIT_EXPR. */
8328 else if (! cfun->returns_struct
8329 && TREE_CODE (retval) == TARGET_EXPR
8330 && TREE_CODE (TREE_OPERAND (retval, 1)) == AGGR_INIT_EXPR)
8331 retval = build2 (COMPOUND_EXPR, TREE_TYPE (retval), retval,
8332 TREE_OPERAND (retval, 0));
8333 else
8334 maybe_warn_about_returning_address_of_local (retval);
8337 /* Actually copy the value returned into the appropriate location. */
8338 if (retval && retval != result)
8339 retval = build2 (INIT_EXPR, TREE_TYPE (result), result, retval);
8341 return retval;
8345 /* Returns nonzero if the pointer-type FROM can be converted to the
8346 pointer-type TO via a qualification conversion. If CONSTP is -1,
8347 then we return nonzero if the pointers are similar, and the
8348 cv-qualification signature of FROM is a proper subset of that of TO.
8350 If CONSTP is positive, then all outer pointers have been
8351 const-qualified. */
8353 static int
8354 comp_ptr_ttypes_real (tree to, tree from, int constp)
8356 bool to_more_cv_qualified = false;
8357 bool is_opaque_pointer = false;
8359 for (; ; to = TREE_TYPE (to), from = TREE_TYPE (from))
8361 if (TREE_CODE (to) != TREE_CODE (from))
8362 return 0;
8364 if (TREE_CODE (from) == OFFSET_TYPE
8365 && !same_type_p (TYPE_OFFSET_BASETYPE (from),
8366 TYPE_OFFSET_BASETYPE (to)))
8367 return 0;
8369 /* Const and volatile mean something different for function types,
8370 so the usual checks are not appropriate. */
8371 if (TREE_CODE (to) != FUNCTION_TYPE && TREE_CODE (to) != METHOD_TYPE)
8373 if (!at_least_as_qualified_p (to, from))
8374 return 0;
8376 if (!at_least_as_qualified_p (from, to))
8378 if (constp == 0)
8379 return 0;
8380 to_more_cv_qualified = true;
8383 if (constp > 0)
8384 constp &= TYPE_READONLY (to);
8387 if (TREE_CODE (to) == VECTOR_TYPE)
8388 is_opaque_pointer = vector_targets_convertible_p (to, from);
8390 if (TREE_CODE (to) != POINTER_TYPE && !TYPE_PTRDATAMEM_P (to))
8391 return ((constp >= 0 || to_more_cv_qualified)
8392 && (is_opaque_pointer
8393 || same_type_ignoring_top_level_qualifiers_p (to, from)));
8397 /* When comparing, say, char ** to char const **, this function takes
8398 the 'char *' and 'char const *'. Do not pass non-pointer/reference
8399 types to this function. */
8402 comp_ptr_ttypes (tree to, tree from)
8404 return comp_ptr_ttypes_real (to, from, 1);
8407 /* Returns true iff FNTYPE is a non-class type that involves
8408 error_mark_node. We can get FUNCTION_TYPE with buried error_mark_node
8409 if a parameter type is ill-formed. */
8411 bool
8412 error_type_p (const_tree type)
8414 tree t;
8416 switch (TREE_CODE (type))
8418 case ERROR_MARK:
8419 return true;
8421 case POINTER_TYPE:
8422 case REFERENCE_TYPE:
8423 case OFFSET_TYPE:
8424 return error_type_p (TREE_TYPE (type));
8426 case FUNCTION_TYPE:
8427 case METHOD_TYPE:
8428 if (error_type_p (TREE_TYPE (type)))
8429 return true;
8430 for (t = TYPE_ARG_TYPES (type); t; t = TREE_CHAIN (t))
8431 if (error_type_p (TREE_VALUE (t)))
8432 return true;
8433 return false;
8435 case RECORD_TYPE:
8436 if (TYPE_PTRMEMFUNC_P (type))
8437 return error_type_p (TYPE_PTRMEMFUNC_FN_TYPE (type));
8438 return false;
8440 default:
8441 return false;
8445 /* Returns 1 if to and from are (possibly multi-level) pointers to the same
8446 type or inheritance-related types, regardless of cv-quals. */
8449 ptr_reasonably_similar (const_tree to, const_tree from)
8451 for (; ; to = TREE_TYPE (to), from = TREE_TYPE (from))
8453 /* Any target type is similar enough to void. */
8454 if (TREE_CODE (to) == VOID_TYPE)
8455 return !error_type_p (from);
8456 if (TREE_CODE (from) == VOID_TYPE)
8457 return !error_type_p (to);
8459 if (TREE_CODE (to) != TREE_CODE (from))
8460 return 0;
8462 if (TREE_CODE (from) == OFFSET_TYPE
8463 && comptypes (TYPE_OFFSET_BASETYPE (to),
8464 TYPE_OFFSET_BASETYPE (from),
8465 COMPARE_BASE | COMPARE_DERIVED))
8466 continue;
8468 if (TREE_CODE (to) == VECTOR_TYPE
8469 && vector_types_convertible_p (to, from, false))
8470 return 1;
8472 if (TREE_CODE (to) == INTEGER_TYPE
8473 && TYPE_PRECISION (to) == TYPE_PRECISION (from))
8474 return 1;
8476 if (TREE_CODE (to) == FUNCTION_TYPE)
8477 return !error_type_p (to) && !error_type_p (from);
8479 if (TREE_CODE (to) != POINTER_TYPE)
8480 return comptypes
8481 (TYPE_MAIN_VARIANT (to), TYPE_MAIN_VARIANT (from),
8482 COMPARE_BASE | COMPARE_DERIVED);
8486 /* Return true if TO and FROM (both of which are POINTER_TYPEs or
8487 pointer-to-member types) are the same, ignoring cv-qualification at
8488 all levels. */
8490 bool
8491 comp_ptr_ttypes_const (tree to, tree from)
8493 bool is_opaque_pointer = false;
8495 for (; ; to = TREE_TYPE (to), from = TREE_TYPE (from))
8497 if (TREE_CODE (to) != TREE_CODE (from))
8498 return false;
8500 if (TREE_CODE (from) == OFFSET_TYPE
8501 && same_type_p (TYPE_OFFSET_BASETYPE (from),
8502 TYPE_OFFSET_BASETYPE (to)))
8503 continue;
8505 if (TREE_CODE (to) == VECTOR_TYPE)
8506 is_opaque_pointer = vector_targets_convertible_p (to, from);
8508 if (TREE_CODE (to) != POINTER_TYPE)
8509 return (is_opaque_pointer
8510 || same_type_ignoring_top_level_qualifiers_p (to, from));
8514 /* Returns the type qualifiers for this type, including the qualifiers on the
8515 elements for an array type. */
8518 cp_type_quals (const_tree type)
8520 int quals;
8521 /* This CONST_CAST is okay because strip_array_types returns its
8522 argument unmodified and we assign it to a const_tree. */
8523 type = strip_array_types (CONST_CAST_TREE (type));
8524 if (type == error_mark_node
8525 /* Quals on a FUNCTION_TYPE are memfn quals. */
8526 || TREE_CODE (type) == FUNCTION_TYPE)
8527 return TYPE_UNQUALIFIED;
8528 quals = TYPE_QUALS (type);
8529 /* METHOD and REFERENCE_TYPEs should never have quals. */
8530 gcc_assert ((TREE_CODE (type) != METHOD_TYPE
8531 && TREE_CODE (type) != REFERENCE_TYPE)
8532 || ((quals & (TYPE_QUAL_CONST|TYPE_QUAL_VOLATILE))
8533 == TYPE_UNQUALIFIED));
8534 return quals;
8537 /* Returns the function-cv-quals for TYPE, which must be a FUNCTION_TYPE or
8538 METHOD_TYPE. */
8541 type_memfn_quals (const_tree type)
8543 if (TREE_CODE (type) == FUNCTION_TYPE)
8544 return TYPE_QUALS (type);
8545 else if (TREE_CODE (type) == METHOD_TYPE)
8546 return cp_type_quals (class_of_this_parm (type));
8547 else
8548 gcc_unreachable ();
8551 /* Returns the FUNCTION_TYPE TYPE with its function-cv-quals changed to
8552 MEMFN_QUALS. */
8554 tree
8555 apply_memfn_quals (tree type, cp_cv_quals memfn_quals)
8557 /* Could handle METHOD_TYPE here if necessary. */
8558 gcc_assert (TREE_CODE (type) == FUNCTION_TYPE);
8559 if (TYPE_QUALS (type) == memfn_quals)
8560 return type;
8561 /* This should really have a different TYPE_MAIN_VARIANT, but that gets
8562 complex. */
8563 return build_qualified_type (type, memfn_quals);
8566 /* Returns nonzero if TYPE is const or volatile. */
8568 bool
8569 cv_qualified_p (const_tree type)
8571 int quals = cp_type_quals (type);
8572 return (quals & (TYPE_QUAL_CONST|TYPE_QUAL_VOLATILE)) != 0;
8575 /* Returns nonzero if the TYPE contains a mutable member. */
8577 bool
8578 cp_has_mutable_p (const_tree type)
8580 /* This CONST_CAST is okay because strip_array_types returns its
8581 argument unmodified and we assign it to a const_tree. */
8582 type = strip_array_types (CONST_CAST_TREE(type));
8584 return CLASS_TYPE_P (type) && CLASSTYPE_HAS_MUTABLE (type);
8587 /* Set TREE_READONLY and TREE_VOLATILE on DECL as indicated by the
8588 TYPE_QUALS. For a VAR_DECL, this may be an optimistic
8589 approximation. In particular, consider:
8591 int f();
8592 struct S { int i; };
8593 const S s = { f(); }
8595 Here, we will make "s" as TREE_READONLY (because it is declared
8596 "const") -- only to reverse ourselves upon seeing that the
8597 initializer is non-constant. */
8599 void
8600 cp_apply_type_quals_to_decl (int type_quals, tree decl)
8602 tree type = TREE_TYPE (decl);
8604 if (type == error_mark_node)
8605 return;
8607 if (TREE_CODE (decl) == TYPE_DECL)
8608 return;
8610 gcc_assert (!(TREE_CODE (type) == FUNCTION_TYPE
8611 && type_quals != TYPE_UNQUALIFIED));
8613 /* Avoid setting TREE_READONLY incorrectly. */
8614 /* We used to check TYPE_NEEDS_CONSTRUCTING here, but now a constexpr
8615 constructor can produce constant init, so rely on cp_finish_decl to
8616 clear TREE_READONLY if the variable has non-constant init. */
8618 /* If the type has (or might have) a mutable component, that component
8619 might be modified. */
8620 if (TYPE_HAS_MUTABLE_P (type) || !COMPLETE_TYPE_P (type))
8621 type_quals &= ~TYPE_QUAL_CONST;
8623 c_apply_type_quals_to_decl (type_quals, decl);
8626 /* Subroutine of casts_away_constness. Make T1 and T2 point at
8627 exemplar types such that casting T1 to T2 is casting away constness
8628 if and only if there is no implicit conversion from T1 to T2. */
8630 static void
8631 casts_away_constness_r (tree *t1, tree *t2, tsubst_flags_t complain)
8633 int quals1;
8634 int quals2;
8636 /* [expr.const.cast]
8638 For multi-level pointer to members and multi-level mixed pointers
8639 and pointers to members (conv.qual), the "member" aspect of a
8640 pointer to member level is ignored when determining if a const
8641 cv-qualifier has been cast away. */
8642 /* [expr.const.cast]
8644 For two pointer types:
8646 X1 is T1cv1,1 * ... cv1,N * where T1 is not a pointer type
8647 X2 is T2cv2,1 * ... cv2,M * where T2 is not a pointer type
8648 K is min(N,M)
8650 casting from X1 to X2 casts away constness if, for a non-pointer
8651 type T there does not exist an implicit conversion (clause
8652 _conv_) from:
8654 Tcv1,(N-K+1) * cv1,(N-K+2) * ... cv1,N *
8658 Tcv2,(M-K+1) * cv2,(M-K+2) * ... cv2,M *. */
8659 if ((!TYPE_PTR_P (*t1) && !TYPE_PTRDATAMEM_P (*t1))
8660 || (!TYPE_PTR_P (*t2) && !TYPE_PTRDATAMEM_P (*t2)))
8662 *t1 = cp_build_qualified_type (void_type_node,
8663 cp_type_quals (*t1));
8664 *t2 = cp_build_qualified_type (void_type_node,
8665 cp_type_quals (*t2));
8666 return;
8669 quals1 = cp_type_quals (*t1);
8670 quals2 = cp_type_quals (*t2);
8672 if (TYPE_PTRDATAMEM_P (*t1))
8673 *t1 = TYPE_PTRMEM_POINTED_TO_TYPE (*t1);
8674 else
8675 *t1 = TREE_TYPE (*t1);
8676 if (TYPE_PTRDATAMEM_P (*t2))
8677 *t2 = TYPE_PTRMEM_POINTED_TO_TYPE (*t2);
8678 else
8679 *t2 = TREE_TYPE (*t2);
8681 casts_away_constness_r (t1, t2, complain);
8682 *t1 = build_pointer_type (*t1);
8683 *t2 = build_pointer_type (*t2);
8684 *t1 = cp_build_qualified_type (*t1, quals1);
8685 *t2 = cp_build_qualified_type (*t2, quals2);
8688 /* Returns nonzero if casting from TYPE1 to TYPE2 casts away
8689 constness.
8691 ??? This function returns non-zero if casting away qualifiers not
8692 just const. We would like to return to the caller exactly which
8693 qualifiers are casted away to give more accurate diagnostics.
8696 static bool
8697 casts_away_constness (tree t1, tree t2, tsubst_flags_t complain)
8699 if (TREE_CODE (t2) == REFERENCE_TYPE)
8701 /* [expr.const.cast]
8703 Casting from an lvalue of type T1 to an lvalue of type T2
8704 using a reference cast casts away constness if a cast from an
8705 rvalue of type "pointer to T1" to the type "pointer to T2"
8706 casts away constness. */
8707 t1 = (TREE_CODE (t1) == REFERENCE_TYPE ? TREE_TYPE (t1) : t1);
8708 return casts_away_constness (build_pointer_type (t1),
8709 build_pointer_type (TREE_TYPE (t2)),
8710 complain);
8713 if (TYPE_PTRDATAMEM_P (t1) && TYPE_PTRDATAMEM_P (t2))
8714 /* [expr.const.cast]
8716 Casting from an rvalue of type "pointer to data member of X
8717 of type T1" to the type "pointer to data member of Y of type
8718 T2" casts away constness if a cast from an rvalue of type
8719 "pointer to T1" to the type "pointer to T2" casts away
8720 constness. */
8721 return casts_away_constness
8722 (build_pointer_type (TYPE_PTRMEM_POINTED_TO_TYPE (t1)),
8723 build_pointer_type (TYPE_PTRMEM_POINTED_TO_TYPE (t2)),
8724 complain);
8726 /* Casting away constness is only something that makes sense for
8727 pointer or reference types. */
8728 if (TREE_CODE (t1) != POINTER_TYPE
8729 || TREE_CODE (t2) != POINTER_TYPE)
8730 return false;
8732 /* Top-level qualifiers don't matter. */
8733 t1 = TYPE_MAIN_VARIANT (t1);
8734 t2 = TYPE_MAIN_VARIANT (t2);
8735 casts_away_constness_r (&t1, &t2, complain);
8736 if (!can_convert (t2, t1, complain))
8737 return true;
8739 return false;
8742 /* If T is a REFERENCE_TYPE return the type to which T refers.
8743 Otherwise, return T itself. */
8745 tree
8746 non_reference (tree t)
8748 if (t && TREE_CODE (t) == REFERENCE_TYPE)
8749 t = TREE_TYPE (t);
8750 return t;
8754 /* Return nonzero if REF is an lvalue valid for this language;
8755 otherwise, print an error message and return zero. USE says
8756 how the lvalue is being used and so selects the error message. */
8759 lvalue_or_else (tree ref, enum lvalue_use use, tsubst_flags_t complain)
8761 cp_lvalue_kind kind = lvalue_kind (ref);
8763 if (kind == clk_none)
8765 if (complain & tf_error)
8766 lvalue_error (input_location, use);
8767 return 0;
8769 else if (kind & (clk_rvalueref|clk_class))
8771 if (!(complain & tf_error))
8772 return 0;
8773 if (kind & clk_class)
8774 /* Make this a permerror because we used to accept it. */
8775 permerror (input_location, "using temporary as lvalue");
8776 else
8777 error ("using xvalue (rvalue reference) as lvalue");
8779 return 1;
8782 /* Return true if a user-defined literal operator is a raw operator. */
8784 bool
8785 check_raw_literal_operator (const_tree decl)
8787 tree argtypes = TYPE_ARG_TYPES (TREE_TYPE (decl));
8788 tree argtype;
8789 int arity;
8790 bool maybe_raw_p = false;
8792 /* Count the number and type of arguments and check for ellipsis. */
8793 for (argtype = argtypes, arity = 0;
8794 argtype && argtype != void_list_node;
8795 ++arity, argtype = TREE_CHAIN (argtype))
8797 tree t = TREE_VALUE (argtype);
8799 if (same_type_p (t, const_string_type_node))
8800 maybe_raw_p = true;
8802 if (!argtype)
8803 return false; /* Found ellipsis. */
8805 if (!maybe_raw_p || arity != 1)
8806 return false;
8808 return true;
8812 /* Return true if a user-defined literal operator has one of the allowed
8813 argument types. */
8815 bool
8816 check_literal_operator_args (const_tree decl,
8817 bool *long_long_unsigned_p, bool *long_double_p)
8819 tree argtypes = TYPE_ARG_TYPES (TREE_TYPE (decl));
8821 *long_long_unsigned_p = false;
8822 *long_double_p = false;
8823 if (processing_template_decl || processing_specialization)
8824 return argtypes == void_list_node;
8825 else
8827 tree argtype;
8828 int arity;
8829 int max_arity = 2;
8831 /* Count the number and type of arguments and check for ellipsis. */
8832 for (argtype = argtypes, arity = 0;
8833 argtype && argtype != void_list_node;
8834 argtype = TREE_CHAIN (argtype))
8836 tree t = TREE_VALUE (argtype);
8837 ++arity;
8839 if (TREE_CODE (t) == POINTER_TYPE)
8841 bool maybe_raw_p = false;
8842 t = TREE_TYPE (t);
8843 if (cp_type_quals (t) != TYPE_QUAL_CONST)
8844 return false;
8845 t = TYPE_MAIN_VARIANT (t);
8846 if ((maybe_raw_p = same_type_p (t, char_type_node))
8847 || same_type_p (t, wchar_type_node)
8848 || same_type_p (t, char16_type_node)
8849 || same_type_p (t, char32_type_node))
8851 argtype = TREE_CHAIN (argtype);
8852 if (!argtype)
8853 return false;
8854 t = TREE_VALUE (argtype);
8855 if (maybe_raw_p && argtype == void_list_node)
8856 return true;
8857 else if (same_type_p (t, size_type_node))
8859 ++arity;
8860 continue;
8862 else
8863 return false;
8866 else if (same_type_p (t, long_long_unsigned_type_node))
8868 max_arity = 1;
8869 *long_long_unsigned_p = true;
8871 else if (same_type_p (t, long_double_type_node))
8873 max_arity = 1;
8874 *long_double_p = true;
8876 else if (same_type_p (t, char_type_node))
8877 max_arity = 1;
8878 else if (same_type_p (t, wchar_type_node))
8879 max_arity = 1;
8880 else if (same_type_p (t, char16_type_node))
8881 max_arity = 1;
8882 else if (same_type_p (t, char32_type_node))
8883 max_arity = 1;
8884 else
8885 return false;
8887 if (!argtype)
8888 return false; /* Found ellipsis. */
8890 if (arity != max_arity)
8891 return false;
8893 return true;