gcc/
[official-gcc.git] / gcc / cp / typeck.c
blobfb0ffd1438d151c5cdffe3a700298d197bb93165
1 /* Build expressions with type checking for C++ compiler.
2 Copyright (C) 1987-2015 Free Software Foundation, Inc.
3 Hacked by Michael Tiemann (tiemann@cygnus.com)
5 This file is part of GCC.
7 GCC is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3, or (at your option)
10 any later version.
12 GCC is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING3. If not see
19 <http://www.gnu.org/licenses/>. */
22 /* This file is part of the C++ front end.
23 It contains routines to build C++ expressions given their operands,
24 including computing the types of the result, C and C++ specific error
25 checks, and some optimization. */
27 #include "config.h"
28 #include "system.h"
29 #include "coretypes.h"
30 #include "tm.h"
31 #include "tree.h"
32 #include "stor-layout.h"
33 #include "varasm.h"
34 #include "cp-tree.h"
35 #include "flags.h"
36 #include "diagnostic.h"
37 #include "intl.h"
38 #include "target.h"
39 #include "convert.h"
40 #include "c-family/c-common.h"
41 #include "c-family/c-objc.h"
42 #include "c-family/c-ubsan.h"
43 #include "params.h"
45 static tree cp_build_addr_expr_strict (tree, tsubst_flags_t);
46 static tree cp_build_function_call (tree, tree, tsubst_flags_t);
47 static tree pfn_from_ptrmemfunc (tree);
48 static tree delta_from_ptrmemfunc (tree);
49 static tree convert_for_assignment (tree, tree, impl_conv_rhs, tree, int,
50 tsubst_flags_t, int);
51 static tree cp_pointer_int_sum (enum tree_code, tree, tree, tsubst_flags_t);
52 static tree rationalize_conditional_expr (enum tree_code, tree,
53 tsubst_flags_t);
54 static int comp_ptr_ttypes_real (tree, tree, int);
55 static bool comp_except_types (tree, tree, bool);
56 static bool comp_array_types (const_tree, const_tree, bool);
57 static tree pointer_diff (tree, tree, tree, tsubst_flags_t);
58 static tree get_delta_difference (tree, tree, bool, bool, tsubst_flags_t);
59 static void casts_away_constness_r (tree *, tree *, tsubst_flags_t);
60 static bool casts_away_constness (tree, tree, tsubst_flags_t);
61 static bool maybe_warn_about_returning_address_of_local (tree);
62 static tree lookup_destructor (tree, tree, tree, tsubst_flags_t);
63 static void warn_args_num (location_t, tree, bool);
64 static int convert_arguments (tree, vec<tree, va_gc> **, tree, int,
65 tsubst_flags_t);
67 /* Do `exp = require_complete_type (exp);' to make sure exp
68 does not have an incomplete type. (That includes void types.)
69 Returns error_mark_node if the VALUE does not have
70 complete type when this function returns. */
72 tree
73 require_complete_type_sfinae (tree value, tsubst_flags_t complain)
75 tree type;
77 if (processing_template_decl || value == error_mark_node)
78 return value;
80 if (TREE_CODE (value) == OVERLOAD)
81 type = unknown_type_node;
82 else
83 type = TREE_TYPE (value);
85 if (type == error_mark_node)
86 return error_mark_node;
88 /* First, detect a valid value with a complete type. */
89 if (COMPLETE_TYPE_P (type))
90 return value;
92 if (complete_type_or_maybe_complain (type, value, complain))
93 return value;
94 else
95 return error_mark_node;
98 tree
99 require_complete_type (tree value)
101 return require_complete_type_sfinae (value, tf_warning_or_error);
104 /* Try to complete TYPE, if it is incomplete. For example, if TYPE is
105 a template instantiation, do the instantiation. Returns TYPE,
106 whether or not it could be completed, unless something goes
107 horribly wrong, in which case the error_mark_node is returned. */
109 tree
110 complete_type (tree type)
112 if (type == NULL_TREE)
113 /* Rather than crash, we return something sure to cause an error
114 at some point. */
115 return error_mark_node;
117 if (type == error_mark_node || COMPLETE_TYPE_P (type))
119 else if (TREE_CODE (type) == ARRAY_TYPE && TYPE_DOMAIN (type))
121 tree t = complete_type (TREE_TYPE (type));
122 unsigned int needs_constructing, has_nontrivial_dtor;
123 if (COMPLETE_TYPE_P (t) && !dependent_type_p (type))
124 layout_type (type);
125 needs_constructing
126 = TYPE_NEEDS_CONSTRUCTING (TYPE_MAIN_VARIANT (t));
127 has_nontrivial_dtor
128 = TYPE_HAS_NONTRIVIAL_DESTRUCTOR (TYPE_MAIN_VARIANT (t));
129 for (t = TYPE_MAIN_VARIANT (type); t; t = TYPE_NEXT_VARIANT (t))
131 TYPE_NEEDS_CONSTRUCTING (t) = needs_constructing;
132 TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t) = has_nontrivial_dtor;
135 else if (CLASS_TYPE_P (type) && CLASSTYPE_TEMPLATE_INSTANTIATION (type))
136 instantiate_class_template (TYPE_MAIN_VARIANT (type));
138 return type;
141 /* Like complete_type, but issue an error if the TYPE cannot be completed.
142 VALUE is used for informative diagnostics.
143 Returns NULL_TREE if the type cannot be made complete. */
145 tree
146 complete_type_or_maybe_complain (tree type, tree value, tsubst_flags_t complain)
148 type = complete_type (type);
149 if (type == error_mark_node)
150 /* We already issued an error. */
151 return NULL_TREE;
152 else if (!COMPLETE_TYPE_P (type))
154 if (complain & tf_error)
155 cxx_incomplete_type_diagnostic (value, type, DK_ERROR);
156 return NULL_TREE;
158 else
159 return type;
162 tree
163 complete_type_or_else (tree type, tree value)
165 return complete_type_or_maybe_complain (type, value, tf_warning_or_error);
168 /* Return truthvalue of whether type of EXP is instantiated. */
171 type_unknown_p (const_tree exp)
173 return (TREE_CODE (exp) == TREE_LIST
174 || TREE_TYPE (exp) == unknown_type_node);
178 /* Return the common type of two parameter lists.
179 We assume that comptypes has already been done and returned 1;
180 if that isn't so, this may crash.
182 As an optimization, free the space we allocate if the parameter
183 lists are already common. */
185 static tree
186 commonparms (tree p1, tree p2)
188 tree oldargs = p1, newargs, n;
189 int i, len;
190 int any_change = 0;
192 len = list_length (p1);
193 newargs = tree_last (p1);
195 if (newargs == void_list_node)
196 i = 1;
197 else
199 i = 0;
200 newargs = 0;
203 for (; i < len; i++)
204 newargs = tree_cons (NULL_TREE, NULL_TREE, newargs);
206 n = newargs;
208 for (i = 0; p1;
209 p1 = TREE_CHAIN (p1), p2 = TREE_CHAIN (p2), n = TREE_CHAIN (n), i++)
211 if (TREE_PURPOSE (p1) && !TREE_PURPOSE (p2))
213 TREE_PURPOSE (n) = TREE_PURPOSE (p1);
214 any_change = 1;
216 else if (! TREE_PURPOSE (p1))
218 if (TREE_PURPOSE (p2))
220 TREE_PURPOSE (n) = TREE_PURPOSE (p2);
221 any_change = 1;
224 else
226 if (1 != simple_cst_equal (TREE_PURPOSE (p1), TREE_PURPOSE (p2)))
227 any_change = 1;
228 TREE_PURPOSE (n) = TREE_PURPOSE (p2);
230 if (TREE_VALUE (p1) != TREE_VALUE (p2))
232 any_change = 1;
233 TREE_VALUE (n) = merge_types (TREE_VALUE (p1), TREE_VALUE (p2));
235 else
236 TREE_VALUE (n) = TREE_VALUE (p1);
238 if (! any_change)
239 return oldargs;
241 return newargs;
244 /* Given a type, perhaps copied for a typedef,
245 find the "original" version of it. */
246 static tree
247 original_type (tree t)
249 int quals = cp_type_quals (t);
250 while (t != error_mark_node
251 && TYPE_NAME (t) != NULL_TREE)
253 tree x = TYPE_NAME (t);
254 if (TREE_CODE (x) != TYPE_DECL)
255 break;
256 x = DECL_ORIGINAL_TYPE (x);
257 if (x == NULL_TREE)
258 break;
259 t = x;
261 return cp_build_qualified_type (t, quals);
264 /* Return the common type for two arithmetic types T1 and T2 under the
265 usual arithmetic conversions. The default conversions have already
266 been applied, and enumerated types converted to their compatible
267 integer types. */
269 static tree
270 cp_common_type (tree t1, tree t2)
272 enum tree_code code1 = TREE_CODE (t1);
273 enum tree_code code2 = TREE_CODE (t2);
274 tree attributes;
275 int i;
278 /* In what follows, we slightly generalize the rules given in [expr] so
279 as to deal with `long long' and `complex'. First, merge the
280 attributes. */
281 attributes = (*targetm.merge_type_attributes) (t1, t2);
283 if (SCOPED_ENUM_P (t1) || SCOPED_ENUM_P (t2))
285 if (TYPE_MAIN_VARIANT (t1) == TYPE_MAIN_VARIANT (t2))
286 return build_type_attribute_variant (t1, attributes);
287 else
288 return NULL_TREE;
291 /* FIXME: Attributes. */
292 gcc_assert (ARITHMETIC_TYPE_P (t1)
293 || TREE_CODE (t1) == VECTOR_TYPE
294 || UNSCOPED_ENUM_P (t1));
295 gcc_assert (ARITHMETIC_TYPE_P (t2)
296 || TREE_CODE (t2) == VECTOR_TYPE
297 || UNSCOPED_ENUM_P (t2));
299 /* If one type is complex, form the common type of the non-complex
300 components, then make that complex. Use T1 or T2 if it is the
301 required type. */
302 if (code1 == COMPLEX_TYPE || code2 == COMPLEX_TYPE)
304 tree subtype1 = code1 == COMPLEX_TYPE ? TREE_TYPE (t1) : t1;
305 tree subtype2 = code2 == COMPLEX_TYPE ? TREE_TYPE (t2) : t2;
306 tree subtype
307 = type_after_usual_arithmetic_conversions (subtype1, subtype2);
309 if (code1 == COMPLEX_TYPE && TREE_TYPE (t1) == subtype)
310 return build_type_attribute_variant (t1, attributes);
311 else if (code2 == COMPLEX_TYPE && TREE_TYPE (t2) == subtype)
312 return build_type_attribute_variant (t2, attributes);
313 else
314 return build_type_attribute_variant (build_complex_type (subtype),
315 attributes);
318 if (code1 == VECTOR_TYPE)
320 /* When we get here we should have two vectors of the same size.
321 Just prefer the unsigned one if present. */
322 if (TYPE_UNSIGNED (t1))
323 return build_type_attribute_variant (t1, attributes);
324 else
325 return build_type_attribute_variant (t2, attributes);
328 /* If only one is real, use it as the result. */
329 if (code1 == REAL_TYPE && code2 != REAL_TYPE)
330 return build_type_attribute_variant (t1, attributes);
331 if (code2 == REAL_TYPE && code1 != REAL_TYPE)
332 return build_type_attribute_variant (t2, attributes);
334 /* Both real or both integers; use the one with greater precision. */
335 if (TYPE_PRECISION (t1) > TYPE_PRECISION (t2))
336 return build_type_attribute_variant (t1, attributes);
337 else if (TYPE_PRECISION (t2) > TYPE_PRECISION (t1))
338 return build_type_attribute_variant (t2, attributes);
340 /* The types are the same; no need to do anything fancy. */
341 if (TYPE_MAIN_VARIANT (t1) == TYPE_MAIN_VARIANT (t2))
342 return build_type_attribute_variant (t1, attributes);
344 if (code1 != REAL_TYPE)
346 /* If one is unsigned long long, then convert the other to unsigned
347 long long. */
348 if (same_type_p (TYPE_MAIN_VARIANT (t1), long_long_unsigned_type_node)
349 || same_type_p (TYPE_MAIN_VARIANT (t2), long_long_unsigned_type_node))
350 return build_type_attribute_variant (long_long_unsigned_type_node,
351 attributes);
352 /* If one is a long long, and the other is an unsigned long, and
353 long long can represent all the values of an unsigned long, then
354 convert to a long long. Otherwise, convert to an unsigned long
355 long. Otherwise, if either operand is long long, convert the
356 other to long long.
358 Since we're here, we know the TYPE_PRECISION is the same;
359 therefore converting to long long cannot represent all the values
360 of an unsigned long, so we choose unsigned long long in that
361 case. */
362 if (same_type_p (TYPE_MAIN_VARIANT (t1), long_long_integer_type_node)
363 || same_type_p (TYPE_MAIN_VARIANT (t2), long_long_integer_type_node))
365 tree t = ((TYPE_UNSIGNED (t1) || TYPE_UNSIGNED (t2))
366 ? long_long_unsigned_type_node
367 : long_long_integer_type_node);
368 return build_type_attribute_variant (t, attributes);
371 /* Go through the same procedure, but for longs. */
372 if (same_type_p (TYPE_MAIN_VARIANT (t1), long_unsigned_type_node)
373 || same_type_p (TYPE_MAIN_VARIANT (t2), long_unsigned_type_node))
374 return build_type_attribute_variant (long_unsigned_type_node,
375 attributes);
376 if (same_type_p (TYPE_MAIN_VARIANT (t1), long_integer_type_node)
377 || same_type_p (TYPE_MAIN_VARIANT (t2), long_integer_type_node))
379 tree t = ((TYPE_UNSIGNED (t1) || TYPE_UNSIGNED (t2))
380 ? long_unsigned_type_node : long_integer_type_node);
381 return build_type_attribute_variant (t, attributes);
384 /* For __intN types, either the type is __int128 (and is lower
385 priority than the types checked above, but higher than other
386 128-bit types) or it's known to not be the same size as other
387 types (enforced in toplev.c). Prefer the unsigned type. */
388 for (i = 0; i < NUM_INT_N_ENTS; i ++)
390 if (int_n_enabled_p [i]
391 && (same_type_p (TYPE_MAIN_VARIANT (t1), int_n_trees[i].signed_type)
392 || same_type_p (TYPE_MAIN_VARIANT (t2), int_n_trees[i].signed_type)
393 || same_type_p (TYPE_MAIN_VARIANT (t1), int_n_trees[i].unsigned_type)
394 || same_type_p (TYPE_MAIN_VARIANT (t2), int_n_trees[i].unsigned_type)))
396 tree t = ((TYPE_UNSIGNED (t1) || TYPE_UNSIGNED (t2))
397 ? int_n_trees[i].unsigned_type
398 : int_n_trees[i].signed_type);
399 return build_type_attribute_variant (t, attributes);
403 /* Otherwise prefer the unsigned one. */
404 if (TYPE_UNSIGNED (t1))
405 return build_type_attribute_variant (t1, attributes);
406 else
407 return build_type_attribute_variant (t2, attributes);
409 else
411 if (same_type_p (TYPE_MAIN_VARIANT (t1), long_double_type_node)
412 || same_type_p (TYPE_MAIN_VARIANT (t2), long_double_type_node))
413 return build_type_attribute_variant (long_double_type_node,
414 attributes);
415 if (same_type_p (TYPE_MAIN_VARIANT (t1), double_type_node)
416 || same_type_p (TYPE_MAIN_VARIANT (t2), double_type_node))
417 return build_type_attribute_variant (double_type_node,
418 attributes);
419 if (same_type_p (TYPE_MAIN_VARIANT (t1), float_type_node)
420 || same_type_p (TYPE_MAIN_VARIANT (t2), float_type_node))
421 return build_type_attribute_variant (float_type_node,
422 attributes);
424 /* Two floating-point types whose TYPE_MAIN_VARIANTs are none of
425 the standard C++ floating-point types. Logic earlier in this
426 function has already eliminated the possibility that
427 TYPE_PRECISION (t2) != TYPE_PRECISION (t1), so there's no
428 compelling reason to choose one or the other. */
429 return build_type_attribute_variant (t1, attributes);
433 /* T1 and T2 are arithmetic or enumeration types. Return the type
434 that will result from the "usual arithmetic conversions" on T1 and
435 T2 as described in [expr]. */
437 tree
438 type_after_usual_arithmetic_conversions (tree t1, tree t2)
440 gcc_assert (ARITHMETIC_TYPE_P (t1)
441 || TREE_CODE (t1) == VECTOR_TYPE
442 || UNSCOPED_ENUM_P (t1));
443 gcc_assert (ARITHMETIC_TYPE_P (t2)
444 || TREE_CODE (t2) == VECTOR_TYPE
445 || UNSCOPED_ENUM_P (t2));
447 /* Perform the integral promotions. We do not promote real types here. */
448 if (INTEGRAL_OR_ENUMERATION_TYPE_P (t1)
449 && INTEGRAL_OR_ENUMERATION_TYPE_P (t2))
451 t1 = type_promotes_to (t1);
452 t2 = type_promotes_to (t2);
455 return cp_common_type (t1, t2);
458 static void
459 composite_pointer_error (diagnostic_t kind, tree t1, tree t2,
460 composite_pointer_operation operation)
462 switch (operation)
464 case CPO_COMPARISON:
465 emit_diagnostic (kind, input_location, 0,
466 "comparison between "
467 "distinct pointer types %qT and %qT lacks a cast",
468 t1, t2);
469 break;
470 case CPO_CONVERSION:
471 emit_diagnostic (kind, input_location, 0,
472 "conversion between "
473 "distinct pointer types %qT and %qT lacks a cast",
474 t1, t2);
475 break;
476 case CPO_CONDITIONAL_EXPR:
477 emit_diagnostic (kind, input_location, 0,
478 "conditional expression between "
479 "distinct pointer types %qT and %qT lacks a cast",
480 t1, t2);
481 break;
482 default:
483 gcc_unreachable ();
487 /* Subroutine of composite_pointer_type to implement the recursive
488 case. See that function for documentation of the parameters. */
490 static tree
491 composite_pointer_type_r (tree t1, tree t2,
492 composite_pointer_operation operation,
493 tsubst_flags_t complain)
495 tree pointee1;
496 tree pointee2;
497 tree result_type;
498 tree attributes;
500 /* Determine the types pointed to by T1 and T2. */
501 if (TYPE_PTR_P (t1))
503 pointee1 = TREE_TYPE (t1);
504 pointee2 = TREE_TYPE (t2);
506 else
508 pointee1 = TYPE_PTRMEM_POINTED_TO_TYPE (t1);
509 pointee2 = TYPE_PTRMEM_POINTED_TO_TYPE (t2);
512 /* [expr.rel]
514 Otherwise, the composite pointer type is a pointer type
515 similar (_conv.qual_) to the type of one of the operands,
516 with a cv-qualification signature (_conv.qual_) that is the
517 union of the cv-qualification signatures of the operand
518 types. */
519 if (same_type_ignoring_top_level_qualifiers_p (pointee1, pointee2))
520 result_type = pointee1;
521 else if ((TYPE_PTR_P (pointee1) && TYPE_PTR_P (pointee2))
522 || (TYPE_PTRMEM_P (pointee1) && TYPE_PTRMEM_P (pointee2)))
524 result_type = composite_pointer_type_r (pointee1, pointee2, operation,
525 complain);
526 if (result_type == error_mark_node)
527 return error_mark_node;
529 else
531 if (complain & tf_error)
532 composite_pointer_error (DK_PERMERROR, t1, t2, operation);
533 else
534 return error_mark_node;
535 result_type = void_type_node;
537 result_type = cp_build_qualified_type (result_type,
538 (cp_type_quals (pointee1)
539 | cp_type_quals (pointee2)));
540 /* If the original types were pointers to members, so is the
541 result. */
542 if (TYPE_PTRMEM_P (t1))
544 if (!same_type_p (TYPE_PTRMEM_CLASS_TYPE (t1),
545 TYPE_PTRMEM_CLASS_TYPE (t2)))
547 if (complain & tf_error)
548 composite_pointer_error (DK_PERMERROR, t1, t2, operation);
549 else
550 return error_mark_node;
552 result_type = build_ptrmem_type (TYPE_PTRMEM_CLASS_TYPE (t1),
553 result_type);
555 else
556 result_type = build_pointer_type (result_type);
558 /* Merge the attributes. */
559 attributes = (*targetm.merge_type_attributes) (t1, t2);
560 return build_type_attribute_variant (result_type, attributes);
563 /* Return the composite pointer type (see [expr.rel]) for T1 and T2.
564 ARG1 and ARG2 are the values with those types. The OPERATION is to
565 describe the operation between the pointer types,
566 in case an error occurs.
568 This routine also implements the computation of a common type for
569 pointers-to-members as per [expr.eq]. */
571 tree
572 composite_pointer_type (tree t1, tree t2, tree arg1, tree arg2,
573 composite_pointer_operation operation,
574 tsubst_flags_t complain)
576 tree class1;
577 tree class2;
579 /* [expr.rel]
581 If one operand is a null pointer constant, the composite pointer
582 type is the type of the other operand. */
583 if (null_ptr_cst_p (arg1))
584 return t2;
585 if (null_ptr_cst_p (arg2))
586 return t1;
588 /* We have:
590 [expr.rel]
592 If one of the operands has type "pointer to cv1 void*", then
593 the other has type "pointer to cv2T", and the composite pointer
594 type is "pointer to cv12 void", where cv12 is the union of cv1
595 and cv2.
597 If either type is a pointer to void, make sure it is T1. */
598 if (TYPE_PTR_P (t2) && VOID_TYPE_P (TREE_TYPE (t2)))
600 tree t;
601 t = t1;
602 t1 = t2;
603 t2 = t;
606 /* Now, if T1 is a pointer to void, merge the qualifiers. */
607 if (TYPE_PTR_P (t1) && VOID_TYPE_P (TREE_TYPE (t1)))
609 tree attributes;
610 tree result_type;
612 if (TYPE_PTRFN_P (t2))
614 if (complain & tf_error)
616 switch (operation)
618 case CPO_COMPARISON:
619 pedwarn (input_location, OPT_Wpedantic,
620 "ISO C++ forbids comparison between pointer "
621 "of type %<void *%> and pointer-to-function");
622 break;
623 case CPO_CONVERSION:
624 pedwarn (input_location, OPT_Wpedantic,
625 "ISO C++ forbids conversion between pointer "
626 "of type %<void *%> and pointer-to-function");
627 break;
628 case CPO_CONDITIONAL_EXPR:
629 pedwarn (input_location, OPT_Wpedantic,
630 "ISO C++ forbids conditional expression between "
631 "pointer of type %<void *%> and "
632 "pointer-to-function");
633 break;
634 default:
635 gcc_unreachable ();
638 else
639 return error_mark_node;
641 result_type
642 = cp_build_qualified_type (void_type_node,
643 (cp_type_quals (TREE_TYPE (t1))
644 | cp_type_quals (TREE_TYPE (t2))));
645 result_type = build_pointer_type (result_type);
646 /* Merge the attributes. */
647 attributes = (*targetm.merge_type_attributes) (t1, t2);
648 return build_type_attribute_variant (result_type, attributes);
651 if (c_dialect_objc () && TYPE_PTR_P (t1)
652 && TYPE_PTR_P (t2))
654 if (objc_have_common_type (t1, t2, -3, NULL_TREE))
655 return objc_common_type (t1, t2);
658 /* [expr.eq] permits the application of a pointer conversion to
659 bring the pointers to a common type. */
660 if (TYPE_PTR_P (t1) && TYPE_PTR_P (t2)
661 && CLASS_TYPE_P (TREE_TYPE (t1))
662 && CLASS_TYPE_P (TREE_TYPE (t2))
663 && !same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (t1),
664 TREE_TYPE (t2)))
666 class1 = TREE_TYPE (t1);
667 class2 = TREE_TYPE (t2);
669 if (DERIVED_FROM_P (class1, class2))
670 t2 = (build_pointer_type
671 (cp_build_qualified_type (class1, cp_type_quals (class2))));
672 else if (DERIVED_FROM_P (class2, class1))
673 t1 = (build_pointer_type
674 (cp_build_qualified_type (class2, cp_type_quals (class1))));
675 else
677 if (complain & tf_error)
678 composite_pointer_error (DK_ERROR, t1, t2, operation);
679 return error_mark_node;
682 /* [expr.eq] permits the application of a pointer-to-member
683 conversion to change the class type of one of the types. */
684 else if (TYPE_PTRMEM_P (t1)
685 && !same_type_p (TYPE_PTRMEM_CLASS_TYPE (t1),
686 TYPE_PTRMEM_CLASS_TYPE (t2)))
688 class1 = TYPE_PTRMEM_CLASS_TYPE (t1);
689 class2 = TYPE_PTRMEM_CLASS_TYPE (t2);
691 if (DERIVED_FROM_P (class1, class2))
692 t1 = build_ptrmem_type (class2, TYPE_PTRMEM_POINTED_TO_TYPE (t1));
693 else if (DERIVED_FROM_P (class2, class1))
694 t2 = build_ptrmem_type (class1, TYPE_PTRMEM_POINTED_TO_TYPE (t2));
695 else
697 if (complain & tf_error)
698 switch (operation)
700 case CPO_COMPARISON:
701 error ("comparison between distinct "
702 "pointer-to-member types %qT and %qT lacks a cast",
703 t1, t2);
704 break;
705 case CPO_CONVERSION:
706 error ("conversion between distinct "
707 "pointer-to-member types %qT and %qT lacks a cast",
708 t1, t2);
709 break;
710 case CPO_CONDITIONAL_EXPR:
711 error ("conditional expression between distinct "
712 "pointer-to-member types %qT and %qT lacks a cast",
713 t1, t2);
714 break;
715 default:
716 gcc_unreachable ();
718 return error_mark_node;
722 return composite_pointer_type_r (t1, t2, operation, complain);
725 /* Return the merged type of two types.
726 We assume that comptypes has already been done and returned 1;
727 if that isn't so, this may crash.
729 This just combines attributes and default arguments; any other
730 differences would cause the two types to compare unalike. */
732 tree
733 merge_types (tree t1, tree t2)
735 enum tree_code code1;
736 enum tree_code code2;
737 tree attributes;
739 /* Save time if the two types are the same. */
740 if (t1 == t2)
741 return t1;
742 if (original_type (t1) == original_type (t2))
743 return t1;
745 /* If one type is nonsense, use the other. */
746 if (t1 == error_mark_node)
747 return t2;
748 if (t2 == error_mark_node)
749 return t1;
751 /* Handle merging an auto redeclaration with a previous deduced
752 return type. */
753 if (is_auto (t1))
754 return t2;
756 /* Merge the attributes. */
757 attributes = (*targetm.merge_type_attributes) (t1, t2);
759 if (TYPE_PTRMEMFUNC_P (t1))
760 t1 = TYPE_PTRMEMFUNC_FN_TYPE (t1);
761 if (TYPE_PTRMEMFUNC_P (t2))
762 t2 = TYPE_PTRMEMFUNC_FN_TYPE (t2);
764 code1 = TREE_CODE (t1);
765 code2 = TREE_CODE (t2);
766 if (code1 != code2)
768 gcc_assert (code1 == TYPENAME_TYPE || code2 == TYPENAME_TYPE);
769 if (code1 == TYPENAME_TYPE)
771 t1 = resolve_typename_type (t1, /*only_current_p=*/true);
772 code1 = TREE_CODE (t1);
774 else
776 t2 = resolve_typename_type (t2, /*only_current_p=*/true);
777 code2 = TREE_CODE (t2);
781 switch (code1)
783 case POINTER_TYPE:
784 case REFERENCE_TYPE:
785 /* For two pointers, do this recursively on the target type. */
787 tree target = merge_types (TREE_TYPE (t1), TREE_TYPE (t2));
788 int quals = cp_type_quals (t1);
790 if (code1 == POINTER_TYPE)
791 t1 = build_pointer_type (target);
792 else
793 t1 = cp_build_reference_type (target, TYPE_REF_IS_RVALUE (t1));
794 t1 = build_type_attribute_variant (t1, attributes);
795 t1 = cp_build_qualified_type (t1, quals);
797 if (TREE_CODE (target) == METHOD_TYPE)
798 t1 = build_ptrmemfunc_type (t1);
800 return t1;
803 case OFFSET_TYPE:
805 int quals;
806 tree pointee;
807 quals = cp_type_quals (t1);
808 pointee = merge_types (TYPE_PTRMEM_POINTED_TO_TYPE (t1),
809 TYPE_PTRMEM_POINTED_TO_TYPE (t2));
810 t1 = build_ptrmem_type (TYPE_PTRMEM_CLASS_TYPE (t1),
811 pointee);
812 t1 = cp_build_qualified_type (t1, quals);
813 break;
816 case ARRAY_TYPE:
818 tree elt = merge_types (TREE_TYPE (t1), TREE_TYPE (t2));
819 /* Save space: see if the result is identical to one of the args. */
820 if (elt == TREE_TYPE (t1) && TYPE_DOMAIN (t1))
821 return build_type_attribute_variant (t1, attributes);
822 if (elt == TREE_TYPE (t2) && TYPE_DOMAIN (t2))
823 return build_type_attribute_variant (t2, attributes);
824 /* Merge the element types, and have a size if either arg has one. */
825 t1 = build_cplus_array_type
826 (elt, TYPE_DOMAIN (TYPE_DOMAIN (t1) ? t1 : t2));
827 break;
830 case FUNCTION_TYPE:
831 /* Function types: prefer the one that specified arg types.
832 If both do, merge the arg types. Also merge the return types. */
834 tree valtype = merge_types (TREE_TYPE (t1), TREE_TYPE (t2));
835 tree p1 = TYPE_ARG_TYPES (t1);
836 tree p2 = TYPE_ARG_TYPES (t2);
837 tree parms;
838 tree rval, raises;
839 bool late_return_type_p = TYPE_HAS_LATE_RETURN_TYPE (t1);
841 /* Save space: see if the result is identical to one of the args. */
842 if (valtype == TREE_TYPE (t1) && ! p2)
843 return cp_build_type_attribute_variant (t1, attributes);
844 if (valtype == TREE_TYPE (t2) && ! p1)
845 return cp_build_type_attribute_variant (t2, attributes);
847 /* Simple way if one arg fails to specify argument types. */
848 if (p1 == NULL_TREE || TREE_VALUE (p1) == void_type_node)
849 parms = p2;
850 else if (p2 == NULL_TREE || TREE_VALUE (p2) == void_type_node)
851 parms = p1;
852 else
853 parms = commonparms (p1, p2);
855 rval = build_function_type (valtype, parms);
856 gcc_assert (type_memfn_quals (t1) == type_memfn_quals (t2));
857 gcc_assert (type_memfn_rqual (t1) == type_memfn_rqual (t2));
858 rval = apply_memfn_quals (rval,
859 type_memfn_quals (t1),
860 type_memfn_rqual (t1));
861 raises = merge_exception_specifiers (TYPE_RAISES_EXCEPTIONS (t1),
862 TYPE_RAISES_EXCEPTIONS (t2));
863 t1 = build_exception_variant (rval, raises);
864 if (late_return_type_p)
865 TYPE_HAS_LATE_RETURN_TYPE (t1) = 1;
866 break;
869 case METHOD_TYPE:
871 /* Get this value the long way, since TYPE_METHOD_BASETYPE
872 is just the main variant of this. */
873 tree basetype = class_of_this_parm (t2);
874 tree raises = merge_exception_specifiers (TYPE_RAISES_EXCEPTIONS (t1),
875 TYPE_RAISES_EXCEPTIONS (t2));
876 cp_ref_qualifier rqual = type_memfn_rqual (t1);
877 tree t3;
878 bool late_return_type_1_p = TYPE_HAS_LATE_RETURN_TYPE (t1);
879 bool late_return_type_2_p = TYPE_HAS_LATE_RETURN_TYPE (t2);
881 /* If this was a member function type, get back to the
882 original type of type member function (i.e., without
883 the class instance variable up front. */
884 t1 = build_function_type (TREE_TYPE (t1),
885 TREE_CHAIN (TYPE_ARG_TYPES (t1)));
886 t2 = build_function_type (TREE_TYPE (t2),
887 TREE_CHAIN (TYPE_ARG_TYPES (t2)));
888 t3 = merge_types (t1, t2);
889 t3 = build_method_type_directly (basetype, TREE_TYPE (t3),
890 TYPE_ARG_TYPES (t3));
891 t1 = build_exception_variant (t3, raises);
892 t1 = build_ref_qualified_type (t1, rqual);
893 if (late_return_type_1_p)
894 TYPE_HAS_LATE_RETURN_TYPE (t1) = 1;
895 if (late_return_type_2_p)
896 TYPE_HAS_LATE_RETURN_TYPE (t2) = 1;
897 break;
900 case TYPENAME_TYPE:
901 /* There is no need to merge attributes into a TYPENAME_TYPE.
902 When the type is instantiated it will have whatever
903 attributes result from the instantiation. */
904 return t1;
906 default:;
909 if (attribute_list_equal (TYPE_ATTRIBUTES (t1), attributes))
910 return t1;
911 else if (attribute_list_equal (TYPE_ATTRIBUTES (t2), attributes))
912 return t2;
913 else
914 return cp_build_type_attribute_variant (t1, attributes);
917 /* Return the ARRAY_TYPE type without its domain. */
919 tree
920 strip_array_domain (tree type)
922 tree t2;
923 gcc_assert (TREE_CODE (type) == ARRAY_TYPE);
924 if (TYPE_DOMAIN (type) == NULL_TREE)
925 return type;
926 t2 = build_cplus_array_type (TREE_TYPE (type), NULL_TREE);
927 return cp_build_type_attribute_variant (t2, TYPE_ATTRIBUTES (type));
930 /* Wrapper around cp_common_type that is used by c-common.c and other
931 front end optimizations that remove promotions.
933 Return the common type for two arithmetic types T1 and T2 under the
934 usual arithmetic conversions. The default conversions have already
935 been applied, and enumerated types converted to their compatible
936 integer types. */
938 tree
939 common_type (tree t1, tree t2)
941 /* If one type is nonsense, use the other */
942 if (t1 == error_mark_node)
943 return t2;
944 if (t2 == error_mark_node)
945 return t1;
947 return cp_common_type (t1, t2);
950 /* Return the common type of two pointer types T1 and T2. This is the
951 type for the result of most arithmetic operations if the operands
952 have the given two types.
954 We assume that comp_target_types has already been done and returned
955 nonzero; if that isn't so, this may crash. */
957 tree
958 common_pointer_type (tree t1, tree t2)
960 gcc_assert ((TYPE_PTR_P (t1) && TYPE_PTR_P (t2))
961 || (TYPE_PTRDATAMEM_P (t1) && TYPE_PTRDATAMEM_P (t2))
962 || (TYPE_PTRMEMFUNC_P (t1) && TYPE_PTRMEMFUNC_P (t2)));
964 return composite_pointer_type (t1, t2, error_mark_node, error_mark_node,
965 CPO_CONVERSION, tf_warning_or_error);
968 /* Compare two exception specifier types for exactness or subsetness, if
969 allowed. Returns false for mismatch, true for match (same, or
970 derived and !exact).
972 [except.spec] "If a class X ... objects of class X or any class publicly
973 and unambiguously derived from X. Similarly, if a pointer type Y * ...
974 exceptions of type Y * or that are pointers to any type publicly and
975 unambiguously derived from Y. Otherwise a function only allows exceptions
976 that have the same type ..."
977 This does not mention cv qualifiers and is different to what throw
978 [except.throw] and catch [except.catch] will do. They will ignore the
979 top level cv qualifiers, and allow qualifiers in the pointer to class
980 example.
982 We implement the letter of the standard. */
984 static bool
985 comp_except_types (tree a, tree b, bool exact)
987 if (same_type_p (a, b))
988 return true;
989 else if (!exact)
991 if (cp_type_quals (a) || cp_type_quals (b))
992 return false;
994 if (TYPE_PTR_P (a) && TYPE_PTR_P (b))
996 a = TREE_TYPE (a);
997 b = TREE_TYPE (b);
998 if (cp_type_quals (a) || cp_type_quals (b))
999 return false;
1002 if (TREE_CODE (a) != RECORD_TYPE
1003 || TREE_CODE (b) != RECORD_TYPE)
1004 return false;
1006 if (publicly_uniquely_derived_p (a, b))
1007 return true;
1009 return false;
1012 /* Return true if TYPE1 and TYPE2 are equivalent exception specifiers.
1013 If EXACT is ce_derived, T2 can be stricter than T1 (according to 15.4/5).
1014 If EXACT is ce_normal, the compatibility rules in 15.4/3 apply.
1015 If EXACT is ce_exact, the specs must be exactly the same. Exception lists
1016 are unordered, but we've already filtered out duplicates. Most lists will
1017 be in order, we should try to make use of that. */
1019 bool
1020 comp_except_specs (const_tree t1, const_tree t2, int exact)
1022 const_tree probe;
1023 const_tree base;
1024 int length = 0;
1026 if (t1 == t2)
1027 return true;
1029 /* First handle noexcept. */
1030 if (exact < ce_exact)
1032 /* noexcept(false) is compatible with no exception-specification,
1033 and stricter than any spec. */
1034 if (t1 == noexcept_false_spec)
1035 return t2 == NULL_TREE || exact == ce_derived;
1036 /* Even a derived noexcept(false) is compatible with no
1037 exception-specification. */
1038 if (t2 == noexcept_false_spec)
1039 return t1 == NULL_TREE;
1041 /* Otherwise, if we aren't looking for an exact match, noexcept is
1042 equivalent to throw(). */
1043 if (t1 == noexcept_true_spec)
1044 t1 = empty_except_spec;
1045 if (t2 == noexcept_true_spec)
1046 t2 = empty_except_spec;
1049 /* If any noexcept is left, it is only comparable to itself;
1050 either we're looking for an exact match or we're redeclaring a
1051 template with dependent noexcept. */
1052 if ((t1 && TREE_PURPOSE (t1))
1053 || (t2 && TREE_PURPOSE (t2)))
1054 return (t1 && t2
1055 && cp_tree_equal (TREE_PURPOSE (t1), TREE_PURPOSE (t2)));
1057 if (t1 == NULL_TREE) /* T1 is ... */
1058 return t2 == NULL_TREE || exact == ce_derived;
1059 if (!TREE_VALUE (t1)) /* t1 is EMPTY */
1060 return t2 != NULL_TREE && !TREE_VALUE (t2);
1061 if (t2 == NULL_TREE) /* T2 is ... */
1062 return false;
1063 if (TREE_VALUE (t1) && !TREE_VALUE (t2)) /* T2 is EMPTY, T1 is not */
1064 return exact == ce_derived;
1066 /* Neither set is ... or EMPTY, make sure each part of T2 is in T1.
1067 Count how many we find, to determine exactness. For exact matching and
1068 ordered T1, T2, this is an O(n) operation, otherwise its worst case is
1069 O(nm). */
1070 for (base = t1; t2 != NULL_TREE; t2 = TREE_CHAIN (t2))
1072 for (probe = base; probe != NULL_TREE; probe = TREE_CHAIN (probe))
1074 tree a = TREE_VALUE (probe);
1075 tree b = TREE_VALUE (t2);
1077 if (comp_except_types (a, b, exact))
1079 if (probe == base && exact > ce_derived)
1080 base = TREE_CHAIN (probe);
1081 length++;
1082 break;
1085 if (probe == NULL_TREE)
1086 return false;
1088 return exact == ce_derived || base == NULL_TREE || length == list_length (t1);
1091 /* Compare the array types T1 and T2. ALLOW_REDECLARATION is true if
1092 [] can match [size]. */
1094 static bool
1095 comp_array_types (const_tree t1, const_tree t2, bool allow_redeclaration)
1097 tree d1;
1098 tree d2;
1099 tree max1, max2;
1101 if (t1 == t2)
1102 return true;
1104 /* The type of the array elements must be the same. */
1105 if (!same_type_p (TREE_TYPE (t1), TREE_TYPE (t2)))
1106 return false;
1108 d1 = TYPE_DOMAIN (t1);
1109 d2 = TYPE_DOMAIN (t2);
1111 if (d1 == d2)
1112 return true;
1114 /* If one of the arrays is dimensionless, and the other has a
1115 dimension, they are of different types. However, it is valid to
1116 write:
1118 extern int a[];
1119 int a[3];
1121 by [basic.link]:
1123 declarations for an array object can specify
1124 array types that differ by the presence or absence of a major
1125 array bound (_dcl.array_). */
1126 if (!d1 || !d2)
1127 return allow_redeclaration;
1129 /* Check that the dimensions are the same. */
1131 if (!cp_tree_equal (TYPE_MIN_VALUE (d1), TYPE_MIN_VALUE (d2)))
1132 return false;
1133 max1 = TYPE_MAX_VALUE (d1);
1134 max2 = TYPE_MAX_VALUE (d2);
1136 if (!cp_tree_equal (max1, max2))
1137 return false;
1139 return true;
1142 /* Compare the relative position of T1 and T2 into their respective
1143 template parameter list.
1144 T1 and T2 must be template parameter types.
1145 Return TRUE if T1 and T2 have the same position, FALSE otherwise. */
1147 static bool
1148 comp_template_parms_position (tree t1, tree t2)
1150 tree index1, index2;
1151 gcc_assert (t1 && t2
1152 && TREE_CODE (t1) == TREE_CODE (t2)
1153 && (TREE_CODE (t1) == BOUND_TEMPLATE_TEMPLATE_PARM
1154 || TREE_CODE (t1) == TEMPLATE_TEMPLATE_PARM
1155 || TREE_CODE (t1) == TEMPLATE_TYPE_PARM));
1157 index1 = TEMPLATE_TYPE_PARM_INDEX (TYPE_MAIN_VARIANT (t1));
1158 index2 = TEMPLATE_TYPE_PARM_INDEX (TYPE_MAIN_VARIANT (t2));
1160 /* Then compare their relative position. */
1161 if (TEMPLATE_PARM_IDX (index1) != TEMPLATE_PARM_IDX (index2)
1162 || TEMPLATE_PARM_LEVEL (index1) != TEMPLATE_PARM_LEVEL (index2)
1163 || (TEMPLATE_PARM_PARAMETER_PACK (index1)
1164 != TEMPLATE_PARM_PARAMETER_PACK (index2)))
1165 return false;
1167 /* In C++14 we can end up comparing 'auto' to a normal template
1168 parameter. Don't confuse them. */
1169 if (cxx_dialect >= cxx14 && (is_auto (t1) || is_auto (t2)))
1170 return TYPE_IDENTIFIER (t1) == TYPE_IDENTIFIER (t2);
1172 return true;
1175 /* Subroutine in comptypes. */
1177 static bool
1178 structural_comptypes (tree t1, tree t2, int strict)
1180 if (t1 == t2)
1181 return true;
1183 /* Suppress errors caused by previously reported errors. */
1184 if (t1 == error_mark_node || t2 == error_mark_node)
1185 return false;
1187 gcc_assert (TYPE_P (t1) && TYPE_P (t2));
1189 /* TYPENAME_TYPEs should be resolved if the qualifying scope is the
1190 current instantiation. */
1191 if (TREE_CODE (t1) == TYPENAME_TYPE)
1192 t1 = resolve_typename_type (t1, /*only_current_p=*/true);
1194 if (TREE_CODE (t2) == TYPENAME_TYPE)
1195 t2 = resolve_typename_type (t2, /*only_current_p=*/true);
1197 if (TYPE_PTRMEMFUNC_P (t1))
1198 t1 = TYPE_PTRMEMFUNC_FN_TYPE (t1);
1199 if (TYPE_PTRMEMFUNC_P (t2))
1200 t2 = TYPE_PTRMEMFUNC_FN_TYPE (t2);
1202 /* Different classes of types can't be compatible. */
1203 if (TREE_CODE (t1) != TREE_CODE (t2))
1204 return false;
1206 /* Qualifiers must match. For array types, we will check when we
1207 recur on the array element types. */
1208 if (TREE_CODE (t1) != ARRAY_TYPE
1209 && cp_type_quals (t1) != cp_type_quals (t2))
1210 return false;
1211 if (TREE_CODE (t1) == FUNCTION_TYPE
1212 && type_memfn_quals (t1) != type_memfn_quals (t2))
1213 return false;
1214 /* Need to check this before TYPE_MAIN_VARIANT.
1215 FIXME function qualifiers should really change the main variant. */
1216 if ((TREE_CODE (t1) == FUNCTION_TYPE
1217 || TREE_CODE (t1) == METHOD_TYPE)
1218 && type_memfn_rqual (t1) != type_memfn_rqual (t2))
1219 return false;
1220 if (TYPE_FOR_JAVA (t1) != TYPE_FOR_JAVA (t2))
1221 return false;
1223 /* Allow for two different type nodes which have essentially the same
1224 definition. Note that we already checked for equality of the type
1225 qualifiers (just above). */
1227 if (TREE_CODE (t1) != ARRAY_TYPE
1228 && TYPE_MAIN_VARIANT (t1) == TYPE_MAIN_VARIANT (t2))
1229 return true;
1232 /* Compare the types. Break out if they could be the same. */
1233 switch (TREE_CODE (t1))
1235 case VOID_TYPE:
1236 case BOOLEAN_TYPE:
1237 /* All void and bool types are the same. */
1238 break;
1240 case INTEGER_TYPE:
1241 case FIXED_POINT_TYPE:
1242 case REAL_TYPE:
1243 /* With these nodes, we can't determine type equivalence by
1244 looking at what is stored in the nodes themselves, because
1245 two nodes might have different TYPE_MAIN_VARIANTs but still
1246 represent the same type. For example, wchar_t and int could
1247 have the same properties (TYPE_PRECISION, TYPE_MIN_VALUE,
1248 TYPE_MAX_VALUE, etc.), but have different TYPE_MAIN_VARIANTs
1249 and are distinct types. On the other hand, int and the
1250 following typedef
1252 typedef int INT __attribute((may_alias));
1254 have identical properties, different TYPE_MAIN_VARIANTs, but
1255 represent the same type. The canonical type system keeps
1256 track of equivalence in this case, so we fall back on it. */
1257 return TYPE_CANONICAL (t1) == TYPE_CANONICAL (t2);
1259 case TEMPLATE_TEMPLATE_PARM:
1260 case BOUND_TEMPLATE_TEMPLATE_PARM:
1261 if (!comp_template_parms_position (t1, t2))
1262 return false;
1263 if (!comp_template_parms
1264 (DECL_TEMPLATE_PARMS (TEMPLATE_TEMPLATE_PARM_TEMPLATE_DECL (t1)),
1265 DECL_TEMPLATE_PARMS (TEMPLATE_TEMPLATE_PARM_TEMPLATE_DECL (t2))))
1266 return false;
1267 if (TREE_CODE (t1) == TEMPLATE_TEMPLATE_PARM)
1268 break;
1269 /* Don't check inheritance. */
1270 strict = COMPARE_STRICT;
1271 /* Fall through. */
1273 case RECORD_TYPE:
1274 case UNION_TYPE:
1275 if (TYPE_TEMPLATE_INFO (t1) && TYPE_TEMPLATE_INFO (t2)
1276 && (TYPE_TI_TEMPLATE (t1) == TYPE_TI_TEMPLATE (t2)
1277 || TREE_CODE (t1) == BOUND_TEMPLATE_TEMPLATE_PARM)
1278 && comp_template_args (TYPE_TI_ARGS (t1), TYPE_TI_ARGS (t2)))
1279 break;
1281 if ((strict & COMPARE_BASE) && DERIVED_FROM_P (t1, t2))
1282 break;
1283 else if ((strict & COMPARE_DERIVED) && DERIVED_FROM_P (t2, t1))
1284 break;
1286 return false;
1288 case OFFSET_TYPE:
1289 if (!comptypes (TYPE_OFFSET_BASETYPE (t1), TYPE_OFFSET_BASETYPE (t2),
1290 strict & ~COMPARE_REDECLARATION))
1291 return false;
1292 if (!same_type_p (TREE_TYPE (t1), TREE_TYPE (t2)))
1293 return false;
1294 break;
1296 case REFERENCE_TYPE:
1297 if (TYPE_REF_IS_RVALUE (t1) != TYPE_REF_IS_RVALUE (t2))
1298 return false;
1299 /* fall through to checks for pointer types */
1301 case POINTER_TYPE:
1302 if (TYPE_MODE (t1) != TYPE_MODE (t2)
1303 || TYPE_REF_CAN_ALIAS_ALL (t1) != TYPE_REF_CAN_ALIAS_ALL (t2)
1304 || !same_type_p (TREE_TYPE (t1), TREE_TYPE (t2)))
1305 return false;
1306 break;
1308 case METHOD_TYPE:
1309 case FUNCTION_TYPE:
1310 if (!same_type_p (TREE_TYPE (t1), TREE_TYPE (t2)))
1311 return false;
1312 if (!compparms (TYPE_ARG_TYPES (t1), TYPE_ARG_TYPES (t2)))
1313 return false;
1314 break;
1316 case ARRAY_TYPE:
1317 /* Target types must match incl. qualifiers. */
1318 if (!comp_array_types (t1, t2, !!(strict & COMPARE_REDECLARATION)))
1319 return false;
1320 break;
1322 case TEMPLATE_TYPE_PARM:
1323 /* If T1 and T2 don't have the same relative position in their
1324 template parameters set, they can't be equal. */
1325 if (!comp_template_parms_position (t1, t2))
1326 return false;
1327 break;
1329 case TYPENAME_TYPE:
1330 if (!cp_tree_equal (TYPENAME_TYPE_FULLNAME (t1),
1331 TYPENAME_TYPE_FULLNAME (t2)))
1332 return false;
1333 /* Qualifiers don't matter on scopes. */
1334 if (!same_type_ignoring_top_level_qualifiers_p (TYPE_CONTEXT (t1),
1335 TYPE_CONTEXT (t2)))
1336 return false;
1337 break;
1339 case UNBOUND_CLASS_TEMPLATE:
1340 if (!cp_tree_equal (TYPE_IDENTIFIER (t1), TYPE_IDENTIFIER (t2)))
1341 return false;
1342 if (!same_type_p (TYPE_CONTEXT (t1), TYPE_CONTEXT (t2)))
1343 return false;
1344 break;
1346 case COMPLEX_TYPE:
1347 if (!same_type_p (TREE_TYPE (t1), TREE_TYPE (t2)))
1348 return false;
1349 break;
1351 case VECTOR_TYPE:
1352 if (TYPE_VECTOR_SUBPARTS (t1) != TYPE_VECTOR_SUBPARTS (t2)
1353 || !same_type_p (TREE_TYPE (t1), TREE_TYPE (t2)))
1354 return false;
1355 break;
1357 case TYPE_PACK_EXPANSION:
1358 return (same_type_p (PACK_EXPANSION_PATTERN (t1),
1359 PACK_EXPANSION_PATTERN (t2))
1360 && comp_template_args (PACK_EXPANSION_EXTRA_ARGS (t1),
1361 PACK_EXPANSION_EXTRA_ARGS (t2)));
1363 case DECLTYPE_TYPE:
1364 if (DECLTYPE_TYPE_ID_EXPR_OR_MEMBER_ACCESS_P (t1)
1365 != DECLTYPE_TYPE_ID_EXPR_OR_MEMBER_ACCESS_P (t2)
1366 || (DECLTYPE_FOR_LAMBDA_CAPTURE (t1)
1367 != DECLTYPE_FOR_LAMBDA_CAPTURE (t2))
1368 || (DECLTYPE_FOR_LAMBDA_PROXY (t1)
1369 != DECLTYPE_FOR_LAMBDA_PROXY (t2))
1370 || !cp_tree_equal (DECLTYPE_TYPE_EXPR (t1),
1371 DECLTYPE_TYPE_EXPR (t2)))
1372 return false;
1373 break;
1375 case UNDERLYING_TYPE:
1376 return same_type_p (UNDERLYING_TYPE_TYPE (t1),
1377 UNDERLYING_TYPE_TYPE (t2));
1379 default:
1380 return false;
1383 /* If we get here, we know that from a target independent POV the
1384 types are the same. Make sure the target attributes are also
1385 the same. */
1386 return comp_type_attributes (t1, t2);
1389 /* Return true if T1 and T2 are related as allowed by STRICT. STRICT
1390 is a bitwise-or of the COMPARE_* flags. */
1392 bool
1393 comptypes (tree t1, tree t2, int strict)
1395 if (strict == COMPARE_STRICT)
1397 if (t1 == t2)
1398 return true;
1400 if (t1 == error_mark_node || t2 == error_mark_node)
1401 return false;
1403 if (TYPE_STRUCTURAL_EQUALITY_P (t1) || TYPE_STRUCTURAL_EQUALITY_P (t2))
1404 /* At least one of the types requires structural equality, so
1405 perform a deep check. */
1406 return structural_comptypes (t1, t2, strict);
1408 #ifdef ENABLE_CHECKING
1409 if (USE_CANONICAL_TYPES)
1411 bool result = structural_comptypes (t1, t2, strict);
1413 if (result && TYPE_CANONICAL (t1) != TYPE_CANONICAL (t2))
1414 /* The two types are structurally equivalent, but their
1415 canonical types were different. This is a failure of the
1416 canonical type propagation code.*/
1417 internal_error
1418 ("canonical types differ for identical types %T and %T",
1419 t1, t2);
1420 else if (!result && TYPE_CANONICAL (t1) == TYPE_CANONICAL (t2))
1421 /* Two types are structurally different, but the canonical
1422 types are the same. This means we were over-eager in
1423 assigning canonical types. */
1424 internal_error
1425 ("same canonical type node for different types %T and %T",
1426 t1, t2);
1428 return result;
1430 #else
1431 if (USE_CANONICAL_TYPES)
1432 return TYPE_CANONICAL (t1) == TYPE_CANONICAL (t2);
1433 #endif
1434 else
1435 return structural_comptypes (t1, t2, strict);
1437 else if (strict == COMPARE_STRUCTURAL)
1438 return structural_comptypes (t1, t2, COMPARE_STRICT);
1439 else
1440 return structural_comptypes (t1, t2, strict);
1443 /* Returns nonzero iff TYPE1 and TYPE2 are the same type, ignoring
1444 top-level qualifiers. */
1446 bool
1447 same_type_ignoring_top_level_qualifiers_p (tree type1, tree type2)
1449 if (type1 == error_mark_node || type2 == error_mark_node)
1450 return false;
1452 return same_type_p (TYPE_MAIN_VARIANT (type1), TYPE_MAIN_VARIANT (type2));
1455 /* Returns 1 if TYPE1 is at least as qualified as TYPE2. */
1457 bool
1458 at_least_as_qualified_p (const_tree type1, const_tree type2)
1460 int q1 = cp_type_quals (type1);
1461 int q2 = cp_type_quals (type2);
1463 /* All qualifiers for TYPE2 must also appear in TYPE1. */
1464 return (q1 & q2) == q2;
1467 /* Returns 1 if TYPE1 is more cv-qualified than TYPE2, -1 if TYPE2 is
1468 more cv-qualified that TYPE1, and 0 otherwise. */
1471 comp_cv_qualification (int q1, int q2)
1473 if (q1 == q2)
1474 return 0;
1476 if ((q1 & q2) == q2)
1477 return 1;
1478 else if ((q1 & q2) == q1)
1479 return -1;
1481 return 0;
1485 comp_cv_qualification (const_tree type1, const_tree type2)
1487 int q1 = cp_type_quals (type1);
1488 int q2 = cp_type_quals (type2);
1489 return comp_cv_qualification (q1, q2);
1492 /* Returns 1 if the cv-qualification signature of TYPE1 is a proper
1493 subset of the cv-qualification signature of TYPE2, and the types
1494 are similar. Returns -1 if the other way 'round, and 0 otherwise. */
1497 comp_cv_qual_signature (tree type1, tree type2)
1499 if (comp_ptr_ttypes_real (type2, type1, -1))
1500 return 1;
1501 else if (comp_ptr_ttypes_real (type1, type2, -1))
1502 return -1;
1503 else
1504 return 0;
1507 /* Subroutines of `comptypes'. */
1509 /* Return true if two parameter type lists PARMS1 and PARMS2 are
1510 equivalent in the sense that functions with those parameter types
1511 can have equivalent types. The two lists must be equivalent,
1512 element by element. */
1514 bool
1515 compparms (const_tree parms1, const_tree parms2)
1517 const_tree t1, t2;
1519 /* An unspecified parmlist matches any specified parmlist
1520 whose argument types don't need default promotions. */
1522 for (t1 = parms1, t2 = parms2;
1523 t1 || t2;
1524 t1 = TREE_CHAIN (t1), t2 = TREE_CHAIN (t2))
1526 /* If one parmlist is shorter than the other,
1527 they fail to match. */
1528 if (!t1 || !t2)
1529 return false;
1530 if (!same_type_p (TREE_VALUE (t1), TREE_VALUE (t2)))
1531 return false;
1533 return true;
1537 /* Process a sizeof or alignof expression where the operand is a
1538 type. */
1540 tree
1541 cxx_sizeof_or_alignof_type (tree type, enum tree_code op, bool complain)
1543 tree value;
1544 bool dependent_p;
1546 gcc_assert (op == SIZEOF_EXPR || op == ALIGNOF_EXPR);
1547 if (type == error_mark_node)
1548 return error_mark_node;
1550 type = non_reference (type);
1551 if (TREE_CODE (type) == METHOD_TYPE)
1553 if (complain)
1554 pedwarn (input_location, OPT_Wpointer_arith,
1555 "invalid application of %qs to a member function",
1556 operator_name_info[(int) op].name);
1557 else
1558 return error_mark_node;
1559 value = size_one_node;
1562 dependent_p = dependent_type_p (type);
1563 if (!dependent_p)
1564 complete_type (type);
1565 if (dependent_p
1566 /* VLA types will have a non-constant size. In the body of an
1567 uninstantiated template, we don't need to try to compute the
1568 value, because the sizeof expression is not an integral
1569 constant expression in that case. And, if we do try to
1570 compute the value, we'll likely end up with SAVE_EXPRs, which
1571 the template substitution machinery does not expect to see. */
1572 || (processing_template_decl
1573 && COMPLETE_TYPE_P (type)
1574 && TREE_CODE (TYPE_SIZE (type)) != INTEGER_CST))
1576 value = build_min (op, size_type_node, type);
1577 TREE_READONLY (value) = 1;
1578 return value;
1581 return c_sizeof_or_alignof_type (input_location, complete_type (type),
1582 op == SIZEOF_EXPR, false,
1583 complain);
1586 /* Return the size of the type, without producing any warnings for
1587 types whose size cannot be taken. This routine should be used only
1588 in some other routine that has already produced a diagnostic about
1589 using the size of such a type. */
1590 tree
1591 cxx_sizeof_nowarn (tree type)
1593 if (TREE_CODE (type) == FUNCTION_TYPE
1594 || VOID_TYPE_P (type)
1595 || TREE_CODE (type) == ERROR_MARK)
1596 return size_one_node;
1597 else if (!COMPLETE_TYPE_P (type))
1598 return size_zero_node;
1599 else
1600 return cxx_sizeof_or_alignof_type (type, SIZEOF_EXPR, false);
1603 /* Process a sizeof expression where the operand is an expression. */
1605 static tree
1606 cxx_sizeof_expr (tree e, tsubst_flags_t complain)
1608 if (e == error_mark_node)
1609 return error_mark_node;
1611 if (processing_template_decl)
1613 e = build_min (SIZEOF_EXPR, size_type_node, e);
1614 TREE_SIDE_EFFECTS (e) = 0;
1615 TREE_READONLY (e) = 1;
1617 return e;
1620 /* To get the size of a static data member declared as an array of
1621 unknown bound, we need to instantiate it. */
1622 if (VAR_P (e)
1623 && VAR_HAD_UNKNOWN_BOUND (e)
1624 && DECL_TEMPLATE_INSTANTIATION (e))
1625 instantiate_decl (e, /*defer_ok*/true, /*expl_inst_mem*/false);
1627 if (TREE_CODE (e) == PARM_DECL
1628 && DECL_ARRAY_PARAMETER_P (e)
1629 && (complain & tf_warning))
1631 if (warning (OPT_Wsizeof_array_argument, "%<sizeof%> on array function "
1632 "parameter %qE will return size of %qT", e, TREE_TYPE (e)))
1633 inform (DECL_SOURCE_LOCATION (e), "declared here");
1636 e = mark_type_use (e);
1638 if (TREE_CODE (e) == COMPONENT_REF
1639 && TREE_CODE (TREE_OPERAND (e, 1)) == FIELD_DECL
1640 && DECL_C_BIT_FIELD (TREE_OPERAND (e, 1)))
1642 if (complain & tf_error)
1643 error ("invalid application of %<sizeof%> to a bit-field");
1644 else
1645 return error_mark_node;
1646 e = char_type_node;
1648 else if (is_overloaded_fn (e))
1650 if (complain & tf_error)
1651 permerror (input_location, "ISO C++ forbids applying %<sizeof%> to an expression of "
1652 "function type");
1653 else
1654 return error_mark_node;
1655 e = char_type_node;
1657 else if (type_unknown_p (e))
1659 if (complain & tf_error)
1660 cxx_incomplete_type_error (e, TREE_TYPE (e));
1661 else
1662 return error_mark_node;
1663 e = char_type_node;
1665 else
1666 e = TREE_TYPE (e);
1668 return cxx_sizeof_or_alignof_type (e, SIZEOF_EXPR, complain & tf_error);
1671 /* Implement the __alignof keyword: Return the minimum required
1672 alignment of E, measured in bytes. For VAR_DECL's and
1673 FIELD_DECL's return DECL_ALIGN (which can be set from an
1674 "aligned" __attribute__ specification). */
1676 static tree
1677 cxx_alignof_expr (tree e, tsubst_flags_t complain)
1679 tree t;
1681 if (e == error_mark_node)
1682 return error_mark_node;
1684 if (processing_template_decl)
1686 e = build_min (ALIGNOF_EXPR, size_type_node, e);
1687 TREE_SIDE_EFFECTS (e) = 0;
1688 TREE_READONLY (e) = 1;
1690 return e;
1693 e = mark_type_use (e);
1695 if (VAR_P (e))
1696 t = size_int (DECL_ALIGN_UNIT (e));
1697 else if (TREE_CODE (e) == COMPONENT_REF
1698 && TREE_CODE (TREE_OPERAND (e, 1)) == FIELD_DECL
1699 && DECL_C_BIT_FIELD (TREE_OPERAND (e, 1)))
1701 if (complain & tf_error)
1702 error ("invalid application of %<__alignof%> to a bit-field");
1703 else
1704 return error_mark_node;
1705 t = size_one_node;
1707 else if (TREE_CODE (e) == COMPONENT_REF
1708 && TREE_CODE (TREE_OPERAND (e, 1)) == FIELD_DECL)
1709 t = size_int (DECL_ALIGN_UNIT (TREE_OPERAND (e, 1)));
1710 else if (is_overloaded_fn (e))
1712 if (complain & tf_error)
1713 permerror (input_location, "ISO C++ forbids applying %<__alignof%> to an expression of "
1714 "function type");
1715 else
1716 return error_mark_node;
1717 if (TREE_CODE (e) == FUNCTION_DECL)
1718 t = size_int (DECL_ALIGN_UNIT (e));
1719 else
1720 t = size_one_node;
1722 else if (type_unknown_p (e))
1724 if (complain & tf_error)
1725 cxx_incomplete_type_error (e, TREE_TYPE (e));
1726 else
1727 return error_mark_node;
1728 t = size_one_node;
1730 else
1731 return cxx_sizeof_or_alignof_type (TREE_TYPE (e), ALIGNOF_EXPR,
1732 complain & tf_error);
1734 return fold_convert (size_type_node, t);
1737 /* Process a sizeof or alignof expression E with code OP where the operand
1738 is an expression. */
1740 tree
1741 cxx_sizeof_or_alignof_expr (tree e, enum tree_code op, bool complain)
1743 if (op == SIZEOF_EXPR)
1744 return cxx_sizeof_expr (e, complain? tf_warning_or_error : tf_none);
1745 else
1746 return cxx_alignof_expr (e, complain? tf_warning_or_error : tf_none);
1749 /* Build a representation of an expression 'alignas(E).' Return the
1750 folded integer value of E if it is an integral constant expression
1751 that resolves to a valid alignment. If E depends on a template
1752 parameter, return a syntactic representation tree of kind
1753 ALIGNOF_EXPR. Otherwise, return an error_mark_node if the
1754 expression is ill formed, or NULL_TREE if E is NULL_TREE. */
1756 tree
1757 cxx_alignas_expr (tree e)
1759 if (e == NULL_TREE || e == error_mark_node
1760 || (!TYPE_P (e) && !require_potential_rvalue_constant_expression (e)))
1761 return e;
1763 if (TYPE_P (e))
1764 /* [dcl.align]/3:
1766 When the alignment-specifier is of the form
1767 alignas(type-id ), it shall have the same effect as
1768 alignas(alignof(type-id )). */
1770 return cxx_sizeof_or_alignof_type (e, ALIGNOF_EXPR, false);
1772 /* If we reach this point, it means the alignas expression if of
1773 the form "alignas(assignment-expression)", so we should follow
1774 what is stated by [dcl.align]/2. */
1776 if (value_dependent_expression_p (e))
1777 /* Leave value-dependent expression alone for now. */
1778 return e;
1780 e = instantiate_non_dependent_expr (e);
1781 e = mark_rvalue_use (e);
1783 /* [dcl.align]/2 says:
1785 the assignment-expression shall be an integral constant
1786 expression. */
1788 return cxx_constant_value (e);
1792 /* EXPR is being used in a context that is not a function call.
1793 Enforce:
1795 [expr.ref]
1797 The expression can be used only as the left-hand operand of a
1798 member function call.
1800 [expr.mptr.operator]
1802 If the result of .* or ->* is a function, then that result can be
1803 used only as the operand for the function call operator ().
1805 by issuing an error message if appropriate. Returns true iff EXPR
1806 violates these rules. */
1808 bool
1809 invalid_nonstatic_memfn_p (tree expr, tsubst_flags_t complain)
1811 if (expr == NULL_TREE)
1812 return false;
1813 /* Don't enforce this in MS mode. */
1814 if (flag_ms_extensions)
1815 return false;
1816 if (is_overloaded_fn (expr) && !really_overloaded_fn (expr))
1817 expr = get_first_fn (expr);
1818 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (expr))
1820 if (complain & tf_error)
1821 error ("invalid use of non-static member function");
1822 return true;
1824 return false;
1827 /* If EXP is a reference to a bitfield, and the type of EXP does not
1828 match the declared type of the bitfield, return the declared type
1829 of the bitfield. Otherwise, return NULL_TREE. */
1831 tree
1832 is_bitfield_expr_with_lowered_type (const_tree exp)
1834 switch (TREE_CODE (exp))
1836 case COND_EXPR:
1837 if (!is_bitfield_expr_with_lowered_type (TREE_OPERAND (exp, 1)
1838 ? TREE_OPERAND (exp, 1)
1839 : TREE_OPERAND (exp, 0)))
1840 return NULL_TREE;
1841 return is_bitfield_expr_with_lowered_type (TREE_OPERAND (exp, 2));
1843 case COMPOUND_EXPR:
1844 return is_bitfield_expr_with_lowered_type (TREE_OPERAND (exp, 1));
1846 case MODIFY_EXPR:
1847 case SAVE_EXPR:
1848 return is_bitfield_expr_with_lowered_type (TREE_OPERAND (exp, 0));
1850 case COMPONENT_REF:
1852 tree field;
1854 field = TREE_OPERAND (exp, 1);
1855 if (TREE_CODE (field) != FIELD_DECL || !DECL_BIT_FIELD_TYPE (field))
1856 return NULL_TREE;
1857 if (same_type_ignoring_top_level_qualifiers_p
1858 (TREE_TYPE (exp), DECL_BIT_FIELD_TYPE (field)))
1859 return NULL_TREE;
1860 return DECL_BIT_FIELD_TYPE (field);
1863 CASE_CONVERT:
1864 if (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_OPERAND (exp, 0)))
1865 == TYPE_MAIN_VARIANT (TREE_TYPE (exp)))
1866 return is_bitfield_expr_with_lowered_type (TREE_OPERAND (exp, 0));
1867 /* Fallthrough. */
1869 default:
1870 return NULL_TREE;
1874 /* Like is_bitfield_with_lowered_type, except that if EXP is not a
1875 bitfield with a lowered type, the type of EXP is returned, rather
1876 than NULL_TREE. */
1878 tree
1879 unlowered_expr_type (const_tree exp)
1881 tree type;
1882 tree etype = TREE_TYPE (exp);
1884 type = is_bitfield_expr_with_lowered_type (exp);
1885 if (type)
1886 type = cp_build_qualified_type (type, cp_type_quals (etype));
1887 else
1888 type = etype;
1890 return type;
1893 /* Perform the conversions in [expr] that apply when an lvalue appears
1894 in an rvalue context: the lvalue-to-rvalue, array-to-pointer, and
1895 function-to-pointer conversions. In addition, manifest constants
1896 are replaced by their values, and bitfield references are converted
1897 to their declared types. Note that this function does not perform the
1898 lvalue-to-rvalue conversion for class types. If you need that conversion
1899 to for class types, then you probably need to use force_rvalue.
1901 Although the returned value is being used as an rvalue, this
1902 function does not wrap the returned expression in a
1903 NON_LVALUE_EXPR; the caller is expected to be mindful of the fact
1904 that the return value is no longer an lvalue. */
1906 tree
1907 decay_conversion (tree exp, tsubst_flags_t complain)
1909 tree type;
1910 enum tree_code code;
1911 location_t loc = EXPR_LOC_OR_LOC (exp, input_location);
1913 type = TREE_TYPE (exp);
1914 if (type == error_mark_node)
1915 return error_mark_node;
1917 exp = mark_rvalue_use (exp);
1919 exp = resolve_nondeduced_context (exp);
1920 if (type_unknown_p (exp))
1922 if (complain & tf_error)
1923 cxx_incomplete_type_error (exp, TREE_TYPE (exp));
1924 return error_mark_node;
1927 code = TREE_CODE (type);
1929 /* FIXME remove for delayed folding. */
1930 exp = scalar_constant_value (exp);
1931 if (error_operand_p (exp))
1932 return error_mark_node;
1934 if (NULLPTR_TYPE_P (type) && !TREE_SIDE_EFFECTS (exp))
1935 return nullptr_node;
1937 /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
1938 Leave such NOP_EXPRs, since RHS is being used in non-lvalue context. */
1939 if (code == VOID_TYPE)
1941 if (complain & tf_error)
1942 error_at (loc, "void value not ignored as it ought to be");
1943 return error_mark_node;
1945 if (invalid_nonstatic_memfn_p (exp, complain))
1946 return error_mark_node;
1947 if (code == FUNCTION_TYPE || is_overloaded_fn (exp))
1948 return cp_build_addr_expr (exp, complain);
1949 if (code == ARRAY_TYPE)
1951 tree adr;
1952 tree ptrtype;
1954 if (INDIRECT_REF_P (exp))
1955 return build_nop (build_pointer_type (TREE_TYPE (type)),
1956 TREE_OPERAND (exp, 0));
1958 if (TREE_CODE (exp) == COMPOUND_EXPR)
1960 tree op1 = decay_conversion (TREE_OPERAND (exp, 1), complain);
1961 if (op1 == error_mark_node)
1962 return error_mark_node;
1963 return build2 (COMPOUND_EXPR, TREE_TYPE (op1),
1964 TREE_OPERAND (exp, 0), op1);
1967 if (!lvalue_p (exp)
1968 && ! (TREE_CODE (exp) == CONSTRUCTOR && TREE_STATIC (exp)))
1970 if (complain & tf_error)
1971 error_at (loc, "invalid use of non-lvalue array");
1972 return error_mark_node;
1975 /* Don't let an array compound literal decay to a pointer. It can
1976 still be used to initialize an array or bind to a reference. */
1977 if (TREE_CODE (exp) == TARGET_EXPR)
1979 if (complain & tf_error)
1980 error_at (loc, "taking address of temporary array");
1981 return error_mark_node;
1984 ptrtype = build_pointer_type (TREE_TYPE (type));
1986 if (VAR_P (exp))
1988 if (!cxx_mark_addressable (exp))
1989 return error_mark_node;
1990 adr = build_nop (ptrtype, build_address (exp));
1991 return adr;
1993 /* This way is better for a COMPONENT_REF since it can
1994 simplify the offset for a component. */
1995 adr = cp_build_addr_expr (exp, complain);
1996 return cp_convert (ptrtype, adr, complain);
1999 /* If a bitfield is used in a context where integral promotion
2000 applies, then the caller is expected to have used
2001 default_conversion. That function promotes bitfields correctly
2002 before calling this function. At this point, if we have a
2003 bitfield referenced, we may assume that is not subject to
2004 promotion, and that, therefore, the type of the resulting rvalue
2005 is the declared type of the bitfield. */
2006 exp = convert_bitfield_to_declared_type (exp);
2008 /* We do not call rvalue() here because we do not want to wrap EXP
2009 in a NON_LVALUE_EXPR. */
2011 /* [basic.lval]
2013 Non-class rvalues always have cv-unqualified types. */
2014 type = TREE_TYPE (exp);
2015 if (!CLASS_TYPE_P (type) && cv_qualified_p (type))
2016 exp = build_nop (cv_unqualified (type), exp);
2018 return exp;
2021 /* Perform preparatory conversions, as part of the "usual arithmetic
2022 conversions". In particular, as per [expr]:
2024 Whenever an lvalue expression appears as an operand of an
2025 operator that expects the rvalue for that operand, the
2026 lvalue-to-rvalue, array-to-pointer, or function-to-pointer
2027 standard conversions are applied to convert the expression to an
2028 rvalue.
2030 In addition, we perform integral promotions here, as those are
2031 applied to both operands to a binary operator before determining
2032 what additional conversions should apply. */
2034 static tree
2035 cp_default_conversion (tree exp, tsubst_flags_t complain)
2037 /* Check for target-specific promotions. */
2038 tree promoted_type = targetm.promoted_type (TREE_TYPE (exp));
2039 if (promoted_type)
2040 exp = cp_convert (promoted_type, exp, complain);
2041 /* Perform the integral promotions first so that bitfield
2042 expressions (which may promote to "int", even if the bitfield is
2043 declared "unsigned") are promoted correctly. */
2044 else if (INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (TREE_TYPE (exp)))
2045 exp = cp_perform_integral_promotions (exp, complain);
2046 /* Perform the other conversions. */
2047 exp = decay_conversion (exp, complain);
2049 return exp;
2052 /* C version. */
2054 tree
2055 default_conversion (tree exp)
2057 return cp_default_conversion (exp, tf_warning_or_error);
2060 /* EXPR is an expression with an integral or enumeration type.
2061 Perform the integral promotions in [conv.prom], and return the
2062 converted value. */
2064 tree
2065 cp_perform_integral_promotions (tree expr, tsubst_flags_t complain)
2067 tree type;
2068 tree promoted_type;
2070 expr = mark_rvalue_use (expr);
2072 /* [conv.prom]
2074 If the bitfield has an enumerated type, it is treated as any
2075 other value of that type for promotion purposes. */
2076 type = is_bitfield_expr_with_lowered_type (expr);
2077 if (!type || TREE_CODE (type) != ENUMERAL_TYPE)
2078 type = TREE_TYPE (expr);
2079 gcc_assert (INTEGRAL_OR_ENUMERATION_TYPE_P (type));
2080 /* Scoped enums don't promote. */
2081 if (SCOPED_ENUM_P (type))
2082 return expr;
2083 promoted_type = type_promotes_to (type);
2084 if (type != promoted_type)
2085 expr = cp_convert (promoted_type, expr, complain);
2086 return expr;
2089 /* C version. */
2091 tree
2092 perform_integral_promotions (tree expr)
2094 return cp_perform_integral_promotions (expr, tf_warning_or_error);
2097 /* Returns nonzero iff exp is a STRING_CST or the result of applying
2098 decay_conversion to one. */
2101 string_conv_p (const_tree totype, const_tree exp, int warn)
2103 tree t;
2105 if (!TYPE_PTR_P (totype))
2106 return 0;
2108 t = TREE_TYPE (totype);
2109 if (!same_type_p (t, char_type_node)
2110 && !same_type_p (t, char16_type_node)
2111 && !same_type_p (t, char32_type_node)
2112 && !same_type_p (t, wchar_type_node))
2113 return 0;
2115 if (TREE_CODE (exp) == STRING_CST)
2117 /* Make sure that we don't try to convert between char and wide chars. */
2118 if (!same_type_p (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (exp))), t))
2119 return 0;
2121 else
2123 /* Is this a string constant which has decayed to 'const char *'? */
2124 t = build_pointer_type (cp_build_qualified_type (t, TYPE_QUAL_CONST));
2125 if (!same_type_p (TREE_TYPE (exp), t))
2126 return 0;
2127 STRIP_NOPS (exp);
2128 if (TREE_CODE (exp) != ADDR_EXPR
2129 || TREE_CODE (TREE_OPERAND (exp, 0)) != STRING_CST)
2130 return 0;
2132 if (warn)
2134 if (cxx_dialect >= cxx11)
2135 pedwarn (input_location,
2136 pedantic ? OPT_Wpedantic : OPT_Wwrite_strings,
2137 "ISO C++ forbids converting a string constant to %qT",
2138 totype);
2139 else
2140 warning (OPT_Wwrite_strings,
2141 "deprecated conversion from string constant to %qT",
2142 totype);
2145 return 1;
2148 /* Given a COND_EXPR, MIN_EXPR, or MAX_EXPR in T, return it in a form that we
2149 can, for example, use as an lvalue. This code used to be in
2150 unary_complex_lvalue, but we needed it to deal with `a = (d == c) ? b : c'
2151 expressions, where we're dealing with aggregates. But now it's again only
2152 called from unary_complex_lvalue. The case (in particular) that led to
2153 this was with CODE == ADDR_EXPR, since it's not an lvalue when we'd
2154 get it there. */
2156 static tree
2157 rationalize_conditional_expr (enum tree_code code, tree t,
2158 tsubst_flags_t complain)
2160 location_t loc = EXPR_LOC_OR_LOC (t, input_location);
2162 /* For MIN_EXPR or MAX_EXPR, fold-const.c has arranged things so that
2163 the first operand is always the one to be used if both operands
2164 are equal, so we know what conditional expression this used to be. */
2165 if (TREE_CODE (t) == MIN_EXPR || TREE_CODE (t) == MAX_EXPR)
2167 tree op0 = TREE_OPERAND (t, 0);
2168 tree op1 = TREE_OPERAND (t, 1);
2170 /* The following code is incorrect if either operand side-effects. */
2171 gcc_assert (!TREE_SIDE_EFFECTS (op0)
2172 && !TREE_SIDE_EFFECTS (op1));
2173 return
2174 build_conditional_expr (loc,
2175 build_x_binary_op (loc,
2176 (TREE_CODE (t) == MIN_EXPR
2177 ? LE_EXPR : GE_EXPR),
2178 op0, TREE_CODE (op0),
2179 op1, TREE_CODE (op1),
2180 /*overload=*/NULL,
2181 complain),
2182 cp_build_unary_op (code, op0, 0, complain),
2183 cp_build_unary_op (code, op1, 0, complain),
2184 complain);
2187 return
2188 build_conditional_expr (loc, TREE_OPERAND (t, 0),
2189 cp_build_unary_op (code, TREE_OPERAND (t, 1), 0,
2190 complain),
2191 cp_build_unary_op (code, TREE_OPERAND (t, 2), 0,
2192 complain),
2193 complain);
2196 /* Given the TYPE of an anonymous union field inside T, return the
2197 FIELD_DECL for the field. If not found return NULL_TREE. Because
2198 anonymous unions can nest, we must also search all anonymous unions
2199 that are directly reachable. */
2201 tree
2202 lookup_anon_field (tree t, tree type)
2204 tree field;
2206 for (field = TYPE_FIELDS (t); field; field = DECL_CHAIN (field))
2208 if (TREE_STATIC (field))
2209 continue;
2210 if (TREE_CODE (field) != FIELD_DECL || DECL_ARTIFICIAL (field))
2211 continue;
2213 /* If we find it directly, return the field. */
2214 if (DECL_NAME (field) == NULL_TREE
2215 && type == TYPE_MAIN_VARIANT (TREE_TYPE (field)))
2217 return field;
2220 /* Otherwise, it could be nested, search harder. */
2221 if (DECL_NAME (field) == NULL_TREE
2222 && ANON_AGGR_TYPE_P (TREE_TYPE (field)))
2224 tree subfield = lookup_anon_field (TREE_TYPE (field), type);
2225 if (subfield)
2226 return subfield;
2229 return NULL_TREE;
2232 /* Build an expression representing OBJECT.MEMBER. OBJECT is an
2233 expression; MEMBER is a DECL or baselink. If ACCESS_PATH is
2234 non-NULL, it indicates the path to the base used to name MEMBER.
2235 If PRESERVE_REFERENCE is true, the expression returned will have
2236 REFERENCE_TYPE if the MEMBER does. Otherwise, the expression
2237 returned will have the type referred to by the reference.
2239 This function does not perform access control; that is either done
2240 earlier by the parser when the name of MEMBER is resolved to MEMBER
2241 itself, or later when overload resolution selects one of the
2242 functions indicated by MEMBER. */
2244 tree
2245 build_class_member_access_expr (tree object, tree member,
2246 tree access_path, bool preserve_reference,
2247 tsubst_flags_t complain)
2249 tree object_type;
2250 tree member_scope;
2251 tree result = NULL_TREE;
2252 tree using_decl = NULL_TREE;
2254 if (error_operand_p (object) || error_operand_p (member))
2255 return error_mark_node;
2257 gcc_assert (DECL_P (member) || BASELINK_P (member));
2259 /* [expr.ref]
2261 The type of the first expression shall be "class object" (of a
2262 complete type). */
2263 object_type = TREE_TYPE (object);
2264 if (!currently_open_class (object_type)
2265 && !complete_type_or_maybe_complain (object_type, object, complain))
2266 return error_mark_node;
2267 if (!CLASS_TYPE_P (object_type))
2269 if (complain & tf_error)
2271 if (POINTER_TYPE_P (object_type)
2272 && CLASS_TYPE_P (TREE_TYPE (object_type)))
2273 error ("request for member %qD in %qE, which is of pointer "
2274 "type %qT (maybe you meant to use %<->%> ?)",
2275 member, object, object_type);
2276 else
2277 error ("request for member %qD in %qE, which is of non-class "
2278 "type %qT", member, object, object_type);
2280 return error_mark_node;
2283 /* The standard does not seem to actually say that MEMBER must be a
2284 member of OBJECT_TYPE. However, that is clearly what is
2285 intended. */
2286 if (DECL_P (member))
2288 member_scope = DECL_CLASS_CONTEXT (member);
2289 mark_used (member);
2290 if (TREE_DEPRECATED (member))
2291 warn_deprecated_use (member, NULL_TREE);
2293 else
2294 member_scope = BINFO_TYPE (BASELINK_ACCESS_BINFO (member));
2295 /* If MEMBER is from an anonymous aggregate, MEMBER_SCOPE will
2296 presently be the anonymous union. Go outwards until we find a
2297 type related to OBJECT_TYPE. */
2298 while ((ANON_AGGR_TYPE_P (member_scope) || UNSCOPED_ENUM_P (member_scope))
2299 && !same_type_ignoring_top_level_qualifiers_p (member_scope,
2300 object_type))
2301 member_scope = TYPE_CONTEXT (member_scope);
2302 if (!member_scope || !DERIVED_FROM_P (member_scope, object_type))
2304 if (complain & tf_error)
2306 if (TREE_CODE (member) == FIELD_DECL)
2307 error ("invalid use of nonstatic data member %qE", member);
2308 else
2309 error ("%qD is not a member of %qT", member, object_type);
2311 return error_mark_node;
2314 /* Transform `(a, b).x' into `(*(a, &b)).x', `(a ? b : c).x' into
2315 `(*(a ? &b : &c)).x', and so on. A COND_EXPR is only an lvalue
2316 in the front end; only _DECLs and _REFs are lvalues in the back end. */
2318 tree temp = unary_complex_lvalue (ADDR_EXPR, object);
2319 if (temp)
2320 object = cp_build_indirect_ref (temp, RO_NULL, complain);
2323 /* In [expr.ref], there is an explicit list of the valid choices for
2324 MEMBER. We check for each of those cases here. */
2325 if (VAR_P (member))
2327 /* A static data member. */
2328 result = member;
2329 mark_exp_read (object);
2330 /* If OBJECT has side-effects, they are supposed to occur. */
2331 if (TREE_SIDE_EFFECTS (object))
2332 result = build2 (COMPOUND_EXPR, TREE_TYPE (result), object, result);
2334 else if (TREE_CODE (member) == FIELD_DECL)
2336 /* A non-static data member. */
2337 bool null_object_p;
2338 int type_quals;
2339 tree member_type;
2341 null_object_p = (INDIRECT_REF_P (object)
2342 && integer_zerop (TREE_OPERAND (object, 0)));
2344 /* Convert OBJECT to the type of MEMBER. */
2345 if (!same_type_p (TYPE_MAIN_VARIANT (object_type),
2346 TYPE_MAIN_VARIANT (member_scope)))
2348 tree binfo;
2349 base_kind kind;
2351 binfo = lookup_base (access_path ? access_path : object_type,
2352 member_scope, ba_unique, &kind, complain);
2353 if (binfo == error_mark_node)
2354 return error_mark_node;
2356 /* It is invalid to try to get to a virtual base of a
2357 NULL object. The most common cause is invalid use of
2358 offsetof macro. */
2359 if (null_object_p && kind == bk_via_virtual)
2361 if (complain & tf_error)
2363 error ("invalid access to non-static data member %qD in "
2364 "virtual base of NULL object", member);
2366 return error_mark_node;
2369 /* Convert to the base. */
2370 object = build_base_path (PLUS_EXPR, object, binfo,
2371 /*nonnull=*/1, complain);
2372 /* If we found the base successfully then we should be able
2373 to convert to it successfully. */
2374 gcc_assert (object != error_mark_node);
2377 /* If MEMBER is from an anonymous aggregate, we have converted
2378 OBJECT so that it refers to the class containing the
2379 anonymous union. Generate a reference to the anonymous union
2380 itself, and recur to find MEMBER. */
2381 if (ANON_AGGR_TYPE_P (DECL_CONTEXT (member))
2382 /* When this code is called from build_field_call, the
2383 object already has the type of the anonymous union.
2384 That is because the COMPONENT_REF was already
2385 constructed, and was then disassembled before calling
2386 build_field_call. After the function-call code is
2387 cleaned up, this waste can be eliminated. */
2388 && (!same_type_ignoring_top_level_qualifiers_p
2389 (TREE_TYPE (object), DECL_CONTEXT (member))))
2391 tree anonymous_union;
2393 anonymous_union = lookup_anon_field (TREE_TYPE (object),
2394 DECL_CONTEXT (member));
2395 object = build_class_member_access_expr (object,
2396 anonymous_union,
2397 /*access_path=*/NULL_TREE,
2398 preserve_reference,
2399 complain);
2402 /* Compute the type of the field, as described in [expr.ref]. */
2403 type_quals = TYPE_UNQUALIFIED;
2404 member_type = TREE_TYPE (member);
2405 if (TREE_CODE (member_type) != REFERENCE_TYPE)
2407 type_quals = (cp_type_quals (member_type)
2408 | cp_type_quals (object_type));
2410 /* A field is const (volatile) if the enclosing object, or the
2411 field itself, is const (volatile). But, a mutable field is
2412 not const, even within a const object. */
2413 if (DECL_MUTABLE_P (member))
2414 type_quals &= ~TYPE_QUAL_CONST;
2415 member_type = cp_build_qualified_type (member_type, type_quals);
2418 result = build3 (COMPONENT_REF, member_type, object, member,
2419 NULL_TREE);
2420 result = fold_if_not_in_template (result);
2422 /* Mark the expression const or volatile, as appropriate. Even
2423 though we've dealt with the type above, we still have to mark the
2424 expression itself. */
2425 if (type_quals & TYPE_QUAL_CONST)
2426 TREE_READONLY (result) = 1;
2427 if (type_quals & TYPE_QUAL_VOLATILE)
2428 TREE_THIS_VOLATILE (result) = 1;
2430 else if (BASELINK_P (member))
2432 /* The member is a (possibly overloaded) member function. */
2433 tree functions;
2434 tree type;
2436 /* If the MEMBER is exactly one static member function, then we
2437 know the type of the expression. Otherwise, we must wait
2438 until overload resolution has been performed. */
2439 functions = BASELINK_FUNCTIONS (member);
2440 if (TREE_CODE (functions) == FUNCTION_DECL
2441 && DECL_STATIC_FUNCTION_P (functions))
2442 type = TREE_TYPE (functions);
2443 else
2444 type = unknown_type_node;
2445 /* Note that we do not convert OBJECT to the BASELINK_BINFO
2446 base. That will happen when the function is called. */
2447 result = build3 (COMPONENT_REF, type, object, member, NULL_TREE);
2449 else if (TREE_CODE (member) == CONST_DECL)
2451 /* The member is an enumerator. */
2452 result = member;
2453 /* If OBJECT has side-effects, they are supposed to occur. */
2454 if (TREE_SIDE_EFFECTS (object))
2455 result = build2 (COMPOUND_EXPR, TREE_TYPE (result),
2456 object, result);
2458 else if ((using_decl = strip_using_decl (member)) != member)
2459 result = build_class_member_access_expr (object,
2460 using_decl,
2461 access_path, preserve_reference,
2462 complain);
2463 else
2465 if (complain & tf_error)
2466 error ("invalid use of %qD", member);
2467 return error_mark_node;
2470 if (!preserve_reference)
2471 /* [expr.ref]
2473 If E2 is declared to have type "reference to T", then ... the
2474 type of E1.E2 is T. */
2475 result = convert_from_reference (result);
2477 return result;
2480 /* Return the destructor denoted by OBJECT.SCOPE::DTOR_NAME, or, if
2481 SCOPE is NULL, by OBJECT.DTOR_NAME, where DTOR_NAME is ~type. */
2483 static tree
2484 lookup_destructor (tree object, tree scope, tree dtor_name,
2485 tsubst_flags_t complain)
2487 tree object_type = TREE_TYPE (object);
2488 tree dtor_type = TREE_OPERAND (dtor_name, 0);
2489 tree expr;
2491 /* We've already complained about this destructor. */
2492 if (dtor_type == error_mark_node)
2493 return error_mark_node;
2495 if (scope && !check_dtor_name (scope, dtor_type))
2497 if (complain & tf_error)
2498 error ("qualified type %qT does not match destructor name ~%qT",
2499 scope, dtor_type);
2500 return error_mark_node;
2502 if (is_auto (dtor_type))
2503 dtor_type = object_type;
2504 else if (identifier_p (dtor_type))
2506 /* In a template, names we can't find a match for are still accepted
2507 destructor names, and we check them here. */
2508 if (check_dtor_name (object_type, dtor_type))
2509 dtor_type = object_type;
2510 else
2512 if (complain & tf_error)
2513 error ("object type %qT does not match destructor name ~%qT",
2514 object_type, dtor_type);
2515 return error_mark_node;
2519 else if (!DERIVED_FROM_P (dtor_type, TYPE_MAIN_VARIANT (object_type)))
2521 if (complain & tf_error)
2522 error ("the type being destroyed is %qT, but the destructor "
2523 "refers to %qT", TYPE_MAIN_VARIANT (object_type), dtor_type);
2524 return error_mark_node;
2526 expr = lookup_member (dtor_type, complete_dtor_identifier,
2527 /*protect=*/1, /*want_type=*/false,
2528 tf_warning_or_error);
2529 if (!expr)
2531 if (complain & tf_error)
2532 cxx_incomplete_type_error (dtor_name, dtor_type);
2533 return error_mark_node;
2535 expr = (adjust_result_of_qualified_name_lookup
2536 (expr, dtor_type, object_type));
2537 if (scope == NULL_TREE)
2538 /* We need to call adjust_result_of_qualified_name_lookup in case the
2539 destructor names a base class, but we unset BASELINK_QUALIFIED_P so
2540 that we still get virtual function binding. */
2541 BASELINK_QUALIFIED_P (expr) = false;
2542 return expr;
2545 /* An expression of the form "A::template B" has been resolved to
2546 DECL. Issue a diagnostic if B is not a template or template
2547 specialization. */
2549 void
2550 check_template_keyword (tree decl)
2552 /* The standard says:
2554 [temp.names]
2556 If a name prefixed by the keyword template is not a member
2557 template, the program is ill-formed.
2559 DR 228 removed the restriction that the template be a member
2560 template.
2562 DR 96, if accepted would add the further restriction that explicit
2563 template arguments must be provided if the template keyword is
2564 used, but, as of 2005-10-16, that DR is still in "drafting". If
2565 this DR is accepted, then the semantic checks here can be
2566 simplified, as the entity named must in fact be a template
2567 specialization, rather than, as at present, a set of overloaded
2568 functions containing at least one template function. */
2569 if (TREE_CODE (decl) != TEMPLATE_DECL
2570 && TREE_CODE (decl) != TEMPLATE_ID_EXPR)
2572 if (!is_overloaded_fn (decl))
2573 permerror (input_location, "%qD is not a template", decl);
2574 else
2576 tree fns;
2577 fns = decl;
2578 if (BASELINK_P (fns))
2579 fns = BASELINK_FUNCTIONS (fns);
2580 while (fns)
2582 tree fn = OVL_CURRENT (fns);
2583 if (TREE_CODE (fn) == TEMPLATE_DECL
2584 || TREE_CODE (fn) == TEMPLATE_ID_EXPR)
2585 break;
2586 if (TREE_CODE (fn) == FUNCTION_DECL
2587 && DECL_USE_TEMPLATE (fn)
2588 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (fn)))
2589 break;
2590 fns = OVL_NEXT (fns);
2592 if (!fns)
2593 permerror (input_location, "%qD is not a template", decl);
2598 /* This function is called by the parser to process a class member
2599 access expression of the form OBJECT.NAME. NAME is a node used by
2600 the parser to represent a name; it is not yet a DECL. It may,
2601 however, be a BASELINK where the BASELINK_FUNCTIONS is a
2602 TEMPLATE_ID_EXPR. Templates must be looked up by the parser, and
2603 there is no reason to do the lookup twice, so the parser keeps the
2604 BASELINK. TEMPLATE_P is true iff NAME was explicitly declared to
2605 be a template via the use of the "A::template B" syntax. */
2607 tree
2608 finish_class_member_access_expr (tree object, tree name, bool template_p,
2609 tsubst_flags_t complain)
2611 tree expr;
2612 tree object_type;
2613 tree member;
2614 tree access_path = NULL_TREE;
2615 tree orig_object = object;
2616 tree orig_name = name;
2618 if (object == error_mark_node || name == error_mark_node)
2619 return error_mark_node;
2621 /* If OBJECT is an ObjC class instance, we must obey ObjC access rules. */
2622 if (!objc_is_public (object, name))
2623 return error_mark_node;
2625 object_type = TREE_TYPE (object);
2627 if (processing_template_decl)
2629 if (/* If OBJECT_TYPE is dependent, so is OBJECT.NAME. */
2630 dependent_type_p (object_type)
2631 /* If NAME is just an IDENTIFIER_NODE, then the expression
2632 is dependent. */
2633 || identifier_p (object)
2634 /* If NAME is "f<args>", where either 'f' or 'args' is
2635 dependent, then the expression is dependent. */
2636 || (TREE_CODE (name) == TEMPLATE_ID_EXPR
2637 && dependent_template_id_p (TREE_OPERAND (name, 0),
2638 TREE_OPERAND (name, 1)))
2639 /* If NAME is "T::X" where "T" is dependent, then the
2640 expression is dependent. */
2641 || (TREE_CODE (name) == SCOPE_REF
2642 && TYPE_P (TREE_OPERAND (name, 0))
2643 && dependent_type_p (TREE_OPERAND (name, 0))))
2644 return build_min_nt_loc (UNKNOWN_LOCATION, COMPONENT_REF,
2645 object, name, NULL_TREE);
2646 object = build_non_dependent_expr (object);
2648 else if (c_dialect_objc ()
2649 && identifier_p (name)
2650 && (expr = objc_maybe_build_component_ref (object, name)))
2651 return expr;
2653 /* [expr.ref]
2655 The type of the first expression shall be "class object" (of a
2656 complete type). */
2657 if (!currently_open_class (object_type)
2658 && !complete_type_or_maybe_complain (object_type, object, complain))
2659 return error_mark_node;
2660 if (!CLASS_TYPE_P (object_type))
2662 if (complain & tf_error)
2664 if (POINTER_TYPE_P (object_type)
2665 && CLASS_TYPE_P (TREE_TYPE (object_type)))
2666 error ("request for member %qD in %qE, which is of pointer "
2667 "type %qT (maybe you meant to use %<->%> ?)",
2668 name, object, object_type);
2669 else
2670 error ("request for member %qD in %qE, which is of non-class "
2671 "type %qT", name, object, object_type);
2673 return error_mark_node;
2676 if (BASELINK_P (name))
2677 /* A member function that has already been looked up. */
2678 member = name;
2679 else
2681 bool is_template_id = false;
2682 tree template_args = NULL_TREE;
2683 tree scope;
2685 if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
2687 is_template_id = true;
2688 template_args = TREE_OPERAND (name, 1);
2689 name = TREE_OPERAND (name, 0);
2691 if (TREE_CODE (name) == OVERLOAD)
2692 name = DECL_NAME (get_first_fn (name));
2693 else if (DECL_P (name))
2694 name = DECL_NAME (name);
2697 if (TREE_CODE (name) == SCOPE_REF)
2699 /* A qualified name. The qualifying class or namespace `S'
2700 has already been looked up; it is either a TYPE or a
2701 NAMESPACE_DECL. */
2702 scope = TREE_OPERAND (name, 0);
2703 name = TREE_OPERAND (name, 1);
2705 /* If SCOPE is a namespace, then the qualified name does not
2706 name a member of OBJECT_TYPE. */
2707 if (TREE_CODE (scope) == NAMESPACE_DECL)
2709 if (complain & tf_error)
2710 error ("%<%D::%D%> is not a member of %qT",
2711 scope, name, object_type);
2712 return error_mark_node;
2715 if (TREE_CODE (scope) == ENUMERAL_TYPE)
2717 /* Looking up a member enumerator (c++/56793). */
2718 if (!TYPE_CLASS_SCOPE_P (scope)
2719 || !DERIVED_FROM_P (TYPE_CONTEXT (scope), object_type))
2721 if (complain & tf_error)
2722 error ("%<%D::%D%> is not a member of %qT",
2723 scope, name, object_type);
2724 return error_mark_node;
2726 tree val = lookup_enumerator (scope, name);
2727 if (TREE_SIDE_EFFECTS (object))
2728 val = build2 (COMPOUND_EXPR, TREE_TYPE (val), object, val);
2729 return val;
2732 gcc_assert (CLASS_TYPE_P (scope));
2733 gcc_assert (identifier_p (name) || TREE_CODE (name) == BIT_NOT_EXPR);
2735 if (constructor_name_p (name, scope))
2737 if (complain & tf_error)
2738 error ("cannot call constructor %<%T::%D%> directly",
2739 scope, name);
2740 return error_mark_node;
2743 /* Find the base of OBJECT_TYPE corresponding to SCOPE. */
2744 access_path = lookup_base (object_type, scope, ba_check,
2745 NULL, complain);
2746 if (access_path == error_mark_node)
2747 return error_mark_node;
2748 if (!access_path)
2750 if (complain & tf_error)
2751 error ("%qT is not a base of %qT", scope, object_type);
2752 return error_mark_node;
2755 else
2757 scope = NULL_TREE;
2758 access_path = object_type;
2761 if (TREE_CODE (name) == BIT_NOT_EXPR)
2762 member = lookup_destructor (object, scope, name, complain);
2763 else
2765 /* Look up the member. */
2766 member = lookup_member (access_path, name, /*protect=*/1,
2767 /*want_type=*/false, complain);
2768 if (member == NULL_TREE)
2770 if (complain & tf_error)
2771 error ("%q#T has no member named %qE",
2772 TREE_CODE (access_path) == TREE_BINFO
2773 ? TREE_TYPE (access_path) : object_type, name);
2774 return error_mark_node;
2776 if (member == error_mark_node)
2777 return error_mark_node;
2780 if (is_template_id)
2782 tree templ = member;
2784 if (BASELINK_P (templ))
2785 templ = lookup_template_function (templ, template_args);
2786 else
2788 if (complain & tf_error)
2789 error ("%qD is not a member template function", name);
2790 return error_mark_node;
2795 if (TREE_DEPRECATED (member))
2796 warn_deprecated_use (member, NULL_TREE);
2798 if (template_p)
2799 check_template_keyword (member);
2801 expr = build_class_member_access_expr (object, member, access_path,
2802 /*preserve_reference=*/false,
2803 complain);
2804 if (processing_template_decl && expr != error_mark_node)
2806 if (BASELINK_P (member))
2808 if (TREE_CODE (orig_name) == SCOPE_REF)
2809 BASELINK_QUALIFIED_P (member) = 1;
2810 orig_name = member;
2812 return build_min_non_dep (COMPONENT_REF, expr,
2813 orig_object, orig_name,
2814 NULL_TREE);
2817 return expr;
2820 /* Build a COMPONENT_REF of OBJECT and MEMBER with the appropriate
2821 type. */
2823 tree
2824 build_simple_component_ref (tree object, tree member)
2826 tree type = cp_build_qualified_type (TREE_TYPE (member),
2827 cp_type_quals (TREE_TYPE (object)));
2828 return fold_build3_loc (input_location,
2829 COMPONENT_REF, type,
2830 object, member, NULL_TREE);
2833 /* Return an expression for the MEMBER_NAME field in the internal
2834 representation of PTRMEM, a pointer-to-member function. (Each
2835 pointer-to-member function type gets its own RECORD_TYPE so it is
2836 more convenient to access the fields by name than by FIELD_DECL.)
2837 This routine converts the NAME to a FIELD_DECL and then creates the
2838 node for the complete expression. */
2840 tree
2841 build_ptrmemfunc_access_expr (tree ptrmem, tree member_name)
2843 tree ptrmem_type;
2844 tree member;
2846 /* This code is a stripped down version of
2847 build_class_member_access_expr. It does not work to use that
2848 routine directly because it expects the object to be of class
2849 type. */
2850 ptrmem_type = TREE_TYPE (ptrmem);
2851 gcc_assert (TYPE_PTRMEMFUNC_P (ptrmem_type));
2852 for (member = TYPE_FIELDS (ptrmem_type); member;
2853 member = DECL_CHAIN (member))
2854 if (DECL_NAME (member) == member_name)
2855 break;
2856 return build_simple_component_ref (ptrmem, member);
2859 /* Given an expression PTR for a pointer, return an expression
2860 for the value pointed to.
2861 ERRORSTRING is the name of the operator to appear in error messages.
2863 This function may need to overload OPERATOR_FNNAME.
2864 Must also handle REFERENCE_TYPEs for C++. */
2866 tree
2867 build_x_indirect_ref (location_t loc, tree expr, ref_operator errorstring,
2868 tsubst_flags_t complain)
2870 tree orig_expr = expr;
2871 tree rval;
2873 if (processing_template_decl)
2875 /* Retain the type if we know the operand is a pointer. */
2876 if (TREE_TYPE (expr) && POINTER_TYPE_P (TREE_TYPE (expr)))
2877 return build_min (INDIRECT_REF, TREE_TYPE (TREE_TYPE (expr)), expr);
2878 if (type_dependent_expression_p (expr))
2879 return build_min_nt_loc (loc, INDIRECT_REF, expr);
2880 expr = build_non_dependent_expr (expr);
2883 rval = build_new_op (loc, INDIRECT_REF, LOOKUP_NORMAL, expr,
2884 NULL_TREE, NULL_TREE, /*overload=*/NULL, complain);
2885 if (!rval)
2886 rval = cp_build_indirect_ref (expr, errorstring, complain);
2888 if (processing_template_decl && rval != error_mark_node)
2889 return build_min_non_dep (INDIRECT_REF, rval, orig_expr);
2890 else
2891 return rval;
2894 /* Helper function called from c-common. */
2895 tree
2896 build_indirect_ref (location_t /*loc*/,
2897 tree ptr, ref_operator errorstring)
2899 return cp_build_indirect_ref (ptr, errorstring, tf_warning_or_error);
2902 tree
2903 cp_build_indirect_ref (tree ptr, ref_operator errorstring,
2904 tsubst_flags_t complain)
2906 tree pointer, type;
2908 if (ptr == current_class_ptr
2909 || (TREE_CODE (ptr) == NOP_EXPR
2910 && TREE_OPERAND (ptr, 0) == current_class_ptr
2911 && (same_type_ignoring_top_level_qualifiers_p
2912 (TREE_TYPE (ptr), TREE_TYPE (current_class_ptr)))))
2913 return current_class_ref;
2915 pointer = (TREE_CODE (TREE_TYPE (ptr)) == REFERENCE_TYPE
2916 ? ptr : decay_conversion (ptr, complain));
2917 if (pointer == error_mark_node)
2918 return error_mark_node;
2920 type = TREE_TYPE (pointer);
2922 if (POINTER_TYPE_P (type))
2924 /* [expr.unary.op]
2926 If the type of the expression is "pointer to T," the type
2927 of the result is "T." */
2928 tree t = TREE_TYPE (type);
2930 if ((CONVERT_EXPR_P (ptr)
2931 || TREE_CODE (ptr) == VIEW_CONVERT_EXPR)
2932 && (!CLASS_TYPE_P (t) || !CLASSTYPE_EMPTY_P (t)))
2934 /* If a warning is issued, mark it to avoid duplicates from
2935 the backend. This only needs to be done at
2936 warn_strict_aliasing > 2. */
2937 if (warn_strict_aliasing > 2)
2938 if (strict_aliasing_warning (TREE_TYPE (TREE_OPERAND (ptr, 0)),
2939 type, TREE_OPERAND (ptr, 0)))
2940 TREE_NO_WARNING (ptr) = 1;
2943 if (VOID_TYPE_P (t))
2945 /* A pointer to incomplete type (other than cv void) can be
2946 dereferenced [expr.unary.op]/1 */
2947 if (complain & tf_error)
2948 error ("%qT is not a pointer-to-object type", type);
2949 return error_mark_node;
2951 else if (TREE_CODE (pointer) == ADDR_EXPR
2952 && same_type_p (t, TREE_TYPE (TREE_OPERAND (pointer, 0))))
2953 /* The POINTER was something like `&x'. We simplify `*&x' to
2954 `x'. */
2955 return TREE_OPERAND (pointer, 0);
2956 else
2958 tree ref = build1 (INDIRECT_REF, t, pointer);
2960 /* We *must* set TREE_READONLY when dereferencing a pointer to const,
2961 so that we get the proper error message if the result is used
2962 to assign to. Also, &* is supposed to be a no-op. */
2963 TREE_READONLY (ref) = CP_TYPE_CONST_P (t);
2964 TREE_THIS_VOLATILE (ref) = CP_TYPE_VOLATILE_P (t);
2965 TREE_SIDE_EFFECTS (ref)
2966 = (TREE_THIS_VOLATILE (ref) || TREE_SIDE_EFFECTS (pointer));
2967 return ref;
2970 else if (!(complain & tf_error))
2971 /* Don't emit any errors; we'll just return ERROR_MARK_NODE later. */
2973 /* `pointer' won't be an error_mark_node if we were given a
2974 pointer to member, so it's cool to check for this here. */
2975 else if (TYPE_PTRMEM_P (type))
2976 switch (errorstring)
2978 case RO_ARRAY_INDEXING:
2979 error ("invalid use of array indexing on pointer to member");
2980 break;
2981 case RO_UNARY_STAR:
2982 error ("invalid use of unary %<*%> on pointer to member");
2983 break;
2984 case RO_IMPLICIT_CONVERSION:
2985 error ("invalid use of implicit conversion on pointer to member");
2986 break;
2987 case RO_ARROW_STAR:
2988 error ("left hand operand of %<->*%> must be a pointer to class, "
2989 "but is a pointer to member of type %qT", type);
2990 break;
2991 default:
2992 gcc_unreachable ();
2994 else if (pointer != error_mark_node)
2995 invalid_indirection_error (input_location, type, errorstring);
2997 return error_mark_node;
3000 /* This handles expressions of the form "a[i]", which denotes
3001 an array reference.
3003 This is logically equivalent in C to *(a+i), but we may do it differently.
3004 If A is a variable or a member, we generate a primitive ARRAY_REF.
3005 This avoids forcing the array out of registers, and can work on
3006 arrays that are not lvalues (for example, members of structures returned
3007 by functions).
3009 If INDEX is of some user-defined type, it must be converted to
3010 integer type. Otherwise, to make a compatible PLUS_EXPR, it
3011 will inherit the type of the array, which will be some pointer type.
3013 LOC is the location to use in building the array reference. */
3015 tree
3016 cp_build_array_ref (location_t loc, tree array, tree idx,
3017 tsubst_flags_t complain)
3019 tree ret;
3021 if (idx == 0)
3023 if (complain & tf_error)
3024 error_at (loc, "subscript missing in array reference");
3025 return error_mark_node;
3028 /* If an array's index is an array notation, then its rank cannot be
3029 greater than one. */
3030 if (flag_cilkplus && contains_array_notation_expr (idx))
3032 size_t rank = 0;
3034 /* If find_rank returns false, then it should have reported an error,
3035 thus it is unnecessary for repetition. */
3036 if (!find_rank (loc, idx, idx, true, &rank))
3037 return error_mark_node;
3038 if (rank > 1)
3040 error_at (loc, "rank of the array%'s index is greater than 1");
3041 return error_mark_node;
3044 if (TREE_TYPE (array) == error_mark_node
3045 || TREE_TYPE (idx) == error_mark_node)
3046 return error_mark_node;
3048 /* If ARRAY is a COMPOUND_EXPR or COND_EXPR, move our reference
3049 inside it. */
3050 switch (TREE_CODE (array))
3052 case COMPOUND_EXPR:
3054 tree value = cp_build_array_ref (loc, TREE_OPERAND (array, 1), idx,
3055 complain);
3056 ret = build2 (COMPOUND_EXPR, TREE_TYPE (value),
3057 TREE_OPERAND (array, 0), value);
3058 SET_EXPR_LOCATION (ret, loc);
3059 return ret;
3062 case COND_EXPR:
3063 ret = build_conditional_expr
3064 (loc, TREE_OPERAND (array, 0),
3065 cp_build_array_ref (loc, TREE_OPERAND (array, 1), idx,
3066 complain),
3067 cp_build_array_ref (loc, TREE_OPERAND (array, 2), idx,
3068 complain),
3069 complain);
3070 protected_set_expr_location (ret, loc);
3071 return ret;
3073 default:
3074 break;
3077 bool non_lvalue
3078 = convert_vector_to_pointer_for_subscript (loc, &array, idx);
3080 if (TREE_CODE (TREE_TYPE (array)) == ARRAY_TYPE)
3082 tree rval, type;
3084 warn_array_subscript_with_type_char (loc, idx);
3086 if (!INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (TREE_TYPE (idx)))
3088 if (complain & tf_error)
3089 error_at (loc, "array subscript is not an integer");
3090 return error_mark_node;
3093 /* Apply integral promotions *after* noticing character types.
3094 (It is unclear why we do these promotions -- the standard
3095 does not say that we should. In fact, the natural thing would
3096 seem to be to convert IDX to ptrdiff_t; we're performing
3097 pointer arithmetic.) */
3098 idx = cp_perform_integral_promotions (idx, complain);
3100 /* An array that is indexed by a non-constant
3101 cannot be stored in a register; we must be able to do
3102 address arithmetic on its address.
3103 Likewise an array of elements of variable size. */
3104 if (TREE_CODE (idx) != INTEGER_CST
3105 || (COMPLETE_TYPE_P (TREE_TYPE (TREE_TYPE (array)))
3106 && (TREE_CODE (TYPE_SIZE (TREE_TYPE (TREE_TYPE (array))))
3107 != INTEGER_CST)))
3109 if (!cxx_mark_addressable (array))
3110 return error_mark_node;
3113 /* An array that is indexed by a constant value which is not within
3114 the array bounds cannot be stored in a register either; because we
3115 would get a crash in store_bit_field/extract_bit_field when trying
3116 to access a non-existent part of the register. */
3117 if (TREE_CODE (idx) == INTEGER_CST
3118 && TYPE_DOMAIN (TREE_TYPE (array))
3119 && ! int_fits_type_p (idx, TYPE_DOMAIN (TREE_TYPE (array))))
3121 if (!cxx_mark_addressable (array))
3122 return error_mark_node;
3125 if (!lvalue_p (array))
3127 if (complain & tf_error)
3128 pedwarn (loc, OPT_Wpedantic,
3129 "ISO C++ forbids subscripting non-lvalue array");
3130 else
3131 return error_mark_node;
3134 /* Note in C++ it is valid to subscript a `register' array, since
3135 it is valid to take the address of something with that
3136 storage specification. */
3137 if (extra_warnings)
3139 tree foo = array;
3140 while (TREE_CODE (foo) == COMPONENT_REF)
3141 foo = TREE_OPERAND (foo, 0);
3142 if (VAR_P (foo) && DECL_REGISTER (foo)
3143 && (complain & tf_warning))
3144 warning_at (loc, OPT_Wextra,
3145 "subscripting array declared %<register%>");
3148 type = TREE_TYPE (TREE_TYPE (array));
3149 rval = build4 (ARRAY_REF, type, array, idx, NULL_TREE, NULL_TREE);
3150 /* Array ref is const/volatile if the array elements are
3151 or if the array is.. */
3152 TREE_READONLY (rval)
3153 |= (CP_TYPE_CONST_P (type) | TREE_READONLY (array));
3154 TREE_SIDE_EFFECTS (rval)
3155 |= (CP_TYPE_VOLATILE_P (type) | TREE_SIDE_EFFECTS (array));
3156 TREE_THIS_VOLATILE (rval)
3157 |= (CP_TYPE_VOLATILE_P (type) | TREE_THIS_VOLATILE (array));
3158 ret = require_complete_type_sfinae (fold_if_not_in_template (rval),
3159 complain);
3160 protected_set_expr_location (ret, loc);
3161 if (non_lvalue)
3162 ret = non_lvalue_loc (loc, ret);
3163 return ret;
3167 tree ar = cp_default_conversion (array, complain);
3168 tree ind = cp_default_conversion (idx, complain);
3170 /* Put the integer in IND to simplify error checking. */
3171 if (TREE_CODE (TREE_TYPE (ar)) == INTEGER_TYPE)
3173 tree temp = ar;
3174 ar = ind;
3175 ind = temp;
3178 if (ar == error_mark_node || ind == error_mark_node)
3179 return error_mark_node;
3181 if (!TYPE_PTR_P (TREE_TYPE (ar)))
3183 if (complain & tf_error)
3184 error_at (loc, "subscripted value is neither array nor pointer");
3185 return error_mark_node;
3187 if (TREE_CODE (TREE_TYPE (ind)) != INTEGER_TYPE)
3189 if (complain & tf_error)
3190 error_at (loc, "array subscript is not an integer");
3191 return error_mark_node;
3194 warn_array_subscript_with_type_char (loc, idx);
3196 ret = cp_build_indirect_ref (cp_build_binary_op (input_location,
3197 PLUS_EXPR, ar, ind,
3198 complain),
3199 RO_ARRAY_INDEXING,
3200 complain);
3201 protected_set_expr_location (ret, loc);
3202 if (non_lvalue)
3203 ret = non_lvalue_loc (loc, ret);
3204 return ret;
3208 /* Entry point for Obj-C++. */
3210 tree
3211 build_array_ref (location_t loc, tree array, tree idx)
3213 return cp_build_array_ref (loc, array, idx, tf_warning_or_error);
3216 /* Resolve a pointer to member function. INSTANCE is the object
3217 instance to use, if the member points to a virtual member.
3219 This used to avoid checking for virtual functions if basetype
3220 has no virtual functions, according to an earlier ANSI draft.
3221 With the final ISO C++ rules, such an optimization is
3222 incorrect: A pointer to a derived member can be static_cast
3223 to pointer-to-base-member, as long as the dynamic object
3224 later has the right member. So now we only do this optimization
3225 when we know the dynamic type of the object. */
3227 tree
3228 get_member_function_from_ptrfunc (tree *instance_ptrptr, tree function,
3229 tsubst_flags_t complain)
3231 if (TREE_CODE (function) == OFFSET_REF)
3232 function = TREE_OPERAND (function, 1);
3234 if (TYPE_PTRMEMFUNC_P (TREE_TYPE (function)))
3236 tree idx, delta, e1, e2, e3, vtbl;
3237 bool nonvirtual;
3238 tree fntype = TYPE_PTRMEMFUNC_FN_TYPE (TREE_TYPE (function));
3239 tree basetype = TYPE_METHOD_BASETYPE (TREE_TYPE (fntype));
3241 tree instance_ptr = *instance_ptrptr;
3242 tree instance_save_expr = 0;
3243 if (instance_ptr == error_mark_node)
3245 if (TREE_CODE (function) == PTRMEM_CST)
3247 /* Extracting the function address from a pmf is only
3248 allowed with -Wno-pmf-conversions. It only works for
3249 pmf constants. */
3250 e1 = build_addr_func (PTRMEM_CST_MEMBER (function), complain);
3251 e1 = convert (fntype, e1);
3252 return e1;
3254 else
3256 if (complain & tf_error)
3257 error ("object missing in use of %qE", function);
3258 return error_mark_node;
3262 /* True if we know that the dynamic type of the object doesn't have
3263 virtual functions, so we can assume the PFN field is a pointer. */
3264 nonvirtual = (COMPLETE_TYPE_P (basetype)
3265 && !TYPE_POLYMORPHIC_P (basetype)
3266 && resolves_to_fixed_type_p (instance_ptr, 0));
3268 /* If we don't really have an object (i.e. in an ill-formed
3269 conversion from PMF to pointer), we can't resolve virtual
3270 functions anyway. */
3271 if (!nonvirtual && is_dummy_object (instance_ptr))
3272 nonvirtual = true;
3274 if (TREE_SIDE_EFFECTS (instance_ptr))
3275 instance_ptr = instance_save_expr = save_expr (instance_ptr);
3277 if (TREE_SIDE_EFFECTS (function))
3278 function = save_expr (function);
3280 /* Start by extracting all the information from the PMF itself. */
3281 e3 = pfn_from_ptrmemfunc (function);
3282 delta = delta_from_ptrmemfunc (function);
3283 idx = build1 (NOP_EXPR, vtable_index_type, e3);
3284 switch (TARGET_PTRMEMFUNC_VBIT_LOCATION)
3286 case ptrmemfunc_vbit_in_pfn:
3287 e1 = cp_build_binary_op (input_location,
3288 BIT_AND_EXPR, idx, integer_one_node,
3289 complain);
3290 idx = cp_build_binary_op (input_location,
3291 MINUS_EXPR, idx, integer_one_node,
3292 complain);
3293 if (idx == error_mark_node)
3294 return error_mark_node;
3295 break;
3297 case ptrmemfunc_vbit_in_delta:
3298 e1 = cp_build_binary_op (input_location,
3299 BIT_AND_EXPR, delta, integer_one_node,
3300 complain);
3301 delta = cp_build_binary_op (input_location,
3302 RSHIFT_EXPR, delta, integer_one_node,
3303 complain);
3304 if (delta == error_mark_node)
3305 return error_mark_node;
3306 break;
3308 default:
3309 gcc_unreachable ();
3312 if (e1 == error_mark_node)
3313 return error_mark_node;
3315 /* Convert down to the right base before using the instance. A
3316 special case is that in a pointer to member of class C, C may
3317 be incomplete. In that case, the function will of course be
3318 a member of C, and no conversion is required. In fact,
3319 lookup_base will fail in that case, because incomplete
3320 classes do not have BINFOs. */
3321 if (!same_type_ignoring_top_level_qualifiers_p
3322 (basetype, TREE_TYPE (TREE_TYPE (instance_ptr))))
3324 basetype = lookup_base (TREE_TYPE (TREE_TYPE (instance_ptr)),
3325 basetype, ba_check, NULL, complain);
3326 instance_ptr = build_base_path (PLUS_EXPR, instance_ptr, basetype,
3327 1, complain);
3328 if (instance_ptr == error_mark_node)
3329 return error_mark_node;
3331 /* ...and then the delta in the PMF. */
3332 instance_ptr = fold_build_pointer_plus (instance_ptr, delta);
3334 /* Hand back the adjusted 'this' argument to our caller. */
3335 *instance_ptrptr = instance_ptr;
3337 if (nonvirtual)
3338 /* Now just return the pointer. */
3339 return e3;
3341 /* Next extract the vtable pointer from the object. */
3342 vtbl = build1 (NOP_EXPR, build_pointer_type (vtbl_ptr_type_node),
3343 instance_ptr);
3344 vtbl = cp_build_indirect_ref (vtbl, RO_NULL, complain);
3345 if (vtbl == error_mark_node)
3346 return error_mark_node;
3348 /* Finally, extract the function pointer from the vtable. */
3349 e2 = fold_build_pointer_plus_loc (input_location, vtbl, idx);
3350 e2 = cp_build_indirect_ref (e2, RO_NULL, complain);
3351 if (e2 == error_mark_node)
3352 return error_mark_node;
3353 TREE_CONSTANT (e2) = 1;
3355 /* When using function descriptors, the address of the
3356 vtable entry is treated as a function pointer. */
3357 if (TARGET_VTABLE_USES_DESCRIPTORS)
3358 e2 = build1 (NOP_EXPR, TREE_TYPE (e2),
3359 cp_build_addr_expr (e2, complain));
3361 e2 = fold_convert (TREE_TYPE (e3), e2);
3362 e1 = build_conditional_expr (input_location, e1, e2, e3, complain);
3363 if (e1 == error_mark_node)
3364 return error_mark_node;
3366 /* Make sure this doesn't get evaluated first inside one of the
3367 branches of the COND_EXPR. */
3368 if (instance_save_expr)
3369 e1 = build2 (COMPOUND_EXPR, TREE_TYPE (e1),
3370 instance_save_expr, e1);
3372 function = e1;
3374 return function;
3377 /* Used by the C-common bits. */
3378 tree
3379 build_function_call (location_t /*loc*/,
3380 tree function, tree params)
3382 return cp_build_function_call (function, params, tf_warning_or_error);
3385 /* Used by the C-common bits. */
3386 tree
3387 build_function_call_vec (location_t /*loc*/, vec<location_t> /*arg_loc*/,
3388 tree function, vec<tree, va_gc> *params,
3389 vec<tree, va_gc> * /*origtypes*/)
3391 vec<tree, va_gc> *orig_params = params;
3392 tree ret = cp_build_function_call_vec (function, &params,
3393 tf_warning_or_error);
3395 /* cp_build_function_call_vec can reallocate PARAMS by adding
3396 default arguments. That should never happen here. Verify
3397 that. */
3398 gcc_assert (params == orig_params);
3400 return ret;
3403 /* Build a function call using a tree list of arguments. */
3405 static tree
3406 cp_build_function_call (tree function, tree params, tsubst_flags_t complain)
3408 vec<tree, va_gc> *vec;
3409 tree ret;
3411 vec = make_tree_vector ();
3412 for (; params != NULL_TREE; params = TREE_CHAIN (params))
3413 vec_safe_push (vec, TREE_VALUE (params));
3414 ret = cp_build_function_call_vec (function, &vec, complain);
3415 release_tree_vector (vec);
3416 return ret;
3419 /* Build a function call using varargs. */
3421 tree
3422 cp_build_function_call_nary (tree function, tsubst_flags_t complain, ...)
3424 vec<tree, va_gc> *vec;
3425 va_list args;
3426 tree ret, t;
3428 vec = make_tree_vector ();
3429 va_start (args, complain);
3430 for (t = va_arg (args, tree); t != NULL_TREE; t = va_arg (args, tree))
3431 vec_safe_push (vec, t);
3432 va_end (args);
3433 ret = cp_build_function_call_vec (function, &vec, complain);
3434 release_tree_vector (vec);
3435 return ret;
3438 /* Build a function call using a vector of arguments. PARAMS may be
3439 NULL if there are no parameters. This changes the contents of
3440 PARAMS. */
3442 tree
3443 cp_build_function_call_vec (tree function, vec<tree, va_gc> **params,
3444 tsubst_flags_t complain)
3446 tree fntype, fndecl;
3447 int is_method;
3448 tree original = function;
3449 int nargs;
3450 tree *argarray;
3451 tree parm_types;
3452 vec<tree, va_gc> *allocated = NULL;
3453 tree ret;
3455 /* For Objective-C, convert any calls via a cast to OBJC_TYPE_REF
3456 expressions, like those used for ObjC messenger dispatches. */
3457 if (params != NULL && !vec_safe_is_empty (*params))
3458 function = objc_rewrite_function_call (function, (**params)[0]);
3460 /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
3461 Strip such NOP_EXPRs, since FUNCTION is used in non-lvalue context. */
3462 if (TREE_CODE (function) == NOP_EXPR
3463 && TREE_TYPE (function) == TREE_TYPE (TREE_OPERAND (function, 0)))
3464 function = TREE_OPERAND (function, 0);
3466 if (TREE_CODE (function) == FUNCTION_DECL)
3468 mark_used (function);
3469 fndecl = function;
3471 /* Convert anything with function type to a pointer-to-function. */
3472 if (DECL_MAIN_P (function))
3474 if (complain & tf_error)
3475 pedwarn (input_location, OPT_Wpedantic,
3476 "ISO C++ forbids calling %<::main%> from within program");
3477 else
3478 return error_mark_node;
3480 function = build_addr_func (function, complain);
3482 else
3484 fndecl = NULL_TREE;
3486 function = build_addr_func (function, complain);
3489 if (function == error_mark_node)
3490 return error_mark_node;
3492 fntype = TREE_TYPE (function);
3494 if (TYPE_PTRMEMFUNC_P (fntype))
3496 if (complain & tf_error)
3497 error ("must use %<.*%> or %<->*%> to call pointer-to-member "
3498 "function in %<%E (...)%>, e.g. %<(... ->* %E) (...)%>",
3499 original, original);
3500 return error_mark_node;
3503 is_method = (TYPE_PTR_P (fntype)
3504 && TREE_CODE (TREE_TYPE (fntype)) == METHOD_TYPE);
3506 if (!(TYPE_PTRFN_P (fntype)
3507 || is_method
3508 || TREE_CODE (function) == TEMPLATE_ID_EXPR))
3510 if (complain & tf_error)
3512 if (!flag_diagnostics_show_caret)
3513 error_at (input_location,
3514 "%qE cannot be used as a function", original);
3515 else if (DECL_P (original))
3516 error_at (input_location,
3517 "%qD cannot be used as a function", original);
3518 else
3519 error_at (input_location,
3520 "expression cannot be used as a function");
3523 return error_mark_node;
3526 /* fntype now gets the type of function pointed to. */
3527 fntype = TREE_TYPE (fntype);
3528 parm_types = TYPE_ARG_TYPES (fntype);
3530 if (params == NULL)
3532 allocated = make_tree_vector ();
3533 params = &allocated;
3536 nargs = convert_arguments (parm_types, params, fndecl, LOOKUP_NORMAL,
3537 complain);
3538 if (nargs < 0)
3539 return error_mark_node;
3541 argarray = (*params)->address ();
3543 /* Check for errors in format strings and inappropriately
3544 null parameters. */
3545 check_function_arguments (fntype, nargs, argarray);
3547 ret = build_cxx_call (function, nargs, argarray, complain);
3549 if (allocated != NULL)
3550 release_tree_vector (allocated);
3552 return ret;
3555 /* Subroutine of convert_arguments.
3556 Warn about wrong number of args are genereted. */
3558 static void
3559 warn_args_num (location_t loc, tree fndecl, bool too_many_p)
3561 if (fndecl)
3563 if (TREE_CODE (TREE_TYPE (fndecl)) == METHOD_TYPE)
3565 if (DECL_NAME (fndecl) == NULL_TREE
3566 || IDENTIFIER_HAS_TYPE_VALUE (DECL_NAME (fndecl)))
3567 error_at (loc,
3568 too_many_p
3569 ? G_("too many arguments to constructor %q#D")
3570 : G_("too few arguments to constructor %q#D"),
3571 fndecl);
3572 else
3573 error_at (loc,
3574 too_many_p
3575 ? G_("too many arguments to member function %q#D")
3576 : G_("too few arguments to member function %q#D"),
3577 fndecl);
3579 else
3580 error_at (loc,
3581 too_many_p
3582 ? G_("too many arguments to function %q#D")
3583 : G_("too few arguments to function %q#D"),
3584 fndecl);
3585 inform (DECL_SOURCE_LOCATION (fndecl),
3586 "declared here");
3588 else
3590 if (c_dialect_objc () && objc_message_selector ())
3591 error_at (loc,
3592 too_many_p
3593 ? G_("too many arguments to method %q#D")
3594 : G_("too few arguments to method %q#D"),
3595 objc_message_selector ());
3596 else
3597 error_at (loc, too_many_p ? G_("too many arguments to function")
3598 : G_("too few arguments to function"));
3602 /* Convert the actual parameter expressions in the list VALUES to the
3603 types in the list TYPELIST. The converted expressions are stored
3604 back in the VALUES vector.
3605 If parmdecls is exhausted, or when an element has NULL as its type,
3606 perform the default conversions.
3608 NAME is an IDENTIFIER_NODE or 0. It is used only for error messages.
3610 This is also where warnings about wrong number of args are generated.
3612 Returns the actual number of arguments processed (which might be less
3613 than the length of the vector), or -1 on error.
3615 In C++, unspecified trailing parameters can be filled in with their
3616 default arguments, if such were specified. Do so here. */
3618 static int
3619 convert_arguments (tree typelist, vec<tree, va_gc> **values, tree fndecl,
3620 int flags, tsubst_flags_t complain)
3622 tree typetail;
3623 unsigned int i;
3625 /* Argument passing is always copy-initialization. */
3626 flags |= LOOKUP_ONLYCONVERTING;
3628 for (i = 0, typetail = typelist;
3629 i < vec_safe_length (*values);
3630 i++)
3632 tree type = typetail ? TREE_VALUE (typetail) : 0;
3633 tree val = (**values)[i];
3635 if (val == error_mark_node || type == error_mark_node)
3636 return -1;
3638 if (type == void_type_node)
3640 if (complain & tf_error)
3642 warn_args_num (input_location, fndecl, /*too_many_p=*/true);
3643 return i;
3645 else
3646 return -1;
3649 /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
3650 Strip such NOP_EXPRs, since VAL is used in non-lvalue context. */
3651 if (TREE_CODE (val) == NOP_EXPR
3652 && TREE_TYPE (val) == TREE_TYPE (TREE_OPERAND (val, 0))
3653 && (type == 0 || TREE_CODE (type) != REFERENCE_TYPE))
3654 val = TREE_OPERAND (val, 0);
3656 if (type == 0 || TREE_CODE (type) != REFERENCE_TYPE)
3658 if (TREE_CODE (TREE_TYPE (val)) == ARRAY_TYPE
3659 || TREE_CODE (TREE_TYPE (val)) == FUNCTION_TYPE
3660 || TREE_CODE (TREE_TYPE (val)) == METHOD_TYPE)
3661 val = decay_conversion (val, complain);
3664 if (val == error_mark_node)
3665 return -1;
3667 if (type != 0)
3669 /* Formal parm type is specified by a function prototype. */
3670 tree parmval;
3672 if (!COMPLETE_TYPE_P (complete_type (type)))
3674 if (complain & tf_error)
3676 if (fndecl)
3677 error ("parameter %P of %qD has incomplete type %qT",
3678 i, fndecl, type);
3679 else
3680 error ("parameter %P has incomplete type %qT", i, type);
3682 parmval = error_mark_node;
3684 else
3686 parmval = convert_for_initialization
3687 (NULL_TREE, type, val, flags,
3688 ICR_ARGPASS, fndecl, i, complain);
3689 parmval = convert_for_arg_passing (type, parmval, complain);
3692 if (parmval == error_mark_node)
3693 return -1;
3695 (**values)[i] = parmval;
3697 else
3699 if (fndecl && magic_varargs_p (fndecl))
3700 /* Don't do ellipsis conversion for __built_in_constant_p
3701 as this will result in spurious errors for non-trivial
3702 types. */
3703 val = require_complete_type_sfinae (val, complain);
3704 else
3705 val = convert_arg_to_ellipsis (val, complain);
3707 (**values)[i] = val;
3710 if (typetail)
3711 typetail = TREE_CHAIN (typetail);
3714 if (typetail != 0 && typetail != void_list_node)
3716 /* See if there are default arguments that can be used. Because
3717 we hold default arguments in the FUNCTION_TYPE (which is so
3718 wrong), we can see default parameters here from deduced
3719 contexts (and via typeof) for indirect function calls.
3720 Fortunately we know whether we have a function decl to
3721 provide default arguments in a language conformant
3722 manner. */
3723 if (fndecl && TREE_PURPOSE (typetail)
3724 && TREE_CODE (TREE_PURPOSE (typetail)) != DEFAULT_ARG)
3726 for (; typetail != void_list_node; ++i)
3728 tree parmval
3729 = convert_default_arg (TREE_VALUE (typetail),
3730 TREE_PURPOSE (typetail),
3731 fndecl, i, complain);
3733 if (parmval == error_mark_node)
3734 return -1;
3736 vec_safe_push (*values, parmval);
3737 typetail = TREE_CHAIN (typetail);
3738 /* ends with `...'. */
3739 if (typetail == NULL_TREE)
3740 break;
3743 else
3745 if (complain & tf_error)
3746 warn_args_num (input_location, fndecl, /*too_many_p=*/false);
3747 return -1;
3751 return (int) i;
3754 /* Build a binary-operation expression, after performing default
3755 conversions on the operands. CODE is the kind of expression to
3756 build. ARG1 and ARG2 are the arguments. ARG1_CODE and ARG2_CODE
3757 are the tree codes which correspond to ARG1 and ARG2 when issuing
3758 warnings about possibly misplaced parentheses. They may differ
3759 from the TREE_CODE of ARG1 and ARG2 if the parser has done constant
3760 folding (e.g., if the parser sees "a | 1 + 1", it may call this
3761 routine with ARG2 being an INTEGER_CST and ARG2_CODE == PLUS_EXPR).
3762 To avoid issuing any parentheses warnings, pass ARG1_CODE and/or
3763 ARG2_CODE as ERROR_MARK. */
3765 tree
3766 build_x_binary_op (location_t loc, enum tree_code code, tree arg1,
3767 enum tree_code arg1_code, tree arg2,
3768 enum tree_code arg2_code, tree *overload,
3769 tsubst_flags_t complain)
3771 tree orig_arg1;
3772 tree orig_arg2;
3773 tree expr;
3775 orig_arg1 = arg1;
3776 orig_arg2 = arg2;
3778 if (processing_template_decl)
3780 if (type_dependent_expression_p (arg1)
3781 || type_dependent_expression_p (arg2))
3782 return build_min_nt_loc (loc, code, arg1, arg2);
3783 arg1 = build_non_dependent_expr (arg1);
3784 arg2 = build_non_dependent_expr (arg2);
3787 if (code == DOTSTAR_EXPR)
3788 expr = build_m_component_ref (arg1, arg2, complain);
3789 else
3790 expr = build_new_op (loc, code, LOOKUP_NORMAL, arg1, arg2, NULL_TREE,
3791 overload, complain);
3793 /* Check for cases such as x+y<<z which users are likely to
3794 misinterpret. But don't warn about obj << x + y, since that is a
3795 common idiom for I/O. */
3796 if (warn_parentheses
3797 && (complain & tf_warning)
3798 && !processing_template_decl
3799 && !error_operand_p (arg1)
3800 && !error_operand_p (arg2)
3801 && (code != LSHIFT_EXPR
3802 || !CLASS_TYPE_P (TREE_TYPE (arg1))))
3803 warn_about_parentheses (loc, code, arg1_code, orig_arg1,
3804 arg2_code, orig_arg2);
3806 if (processing_template_decl && expr != error_mark_node)
3807 return build_min_non_dep (code, expr, orig_arg1, orig_arg2);
3809 return expr;
3812 /* Build and return an ARRAY_REF expression. */
3814 tree
3815 build_x_array_ref (location_t loc, tree arg1, tree arg2,
3816 tsubst_flags_t complain)
3818 tree orig_arg1 = arg1;
3819 tree orig_arg2 = arg2;
3820 tree expr;
3822 if (processing_template_decl)
3824 if (type_dependent_expression_p (arg1)
3825 || type_dependent_expression_p (arg2))
3826 return build_min_nt_loc (loc, ARRAY_REF, arg1, arg2,
3827 NULL_TREE, NULL_TREE);
3828 arg1 = build_non_dependent_expr (arg1);
3829 arg2 = build_non_dependent_expr (arg2);
3832 expr = build_new_op (loc, ARRAY_REF, LOOKUP_NORMAL, arg1, arg2,
3833 NULL_TREE, /*overload=*/NULL, complain);
3835 if (processing_template_decl && expr != error_mark_node)
3836 return build_min_non_dep (ARRAY_REF, expr, orig_arg1, orig_arg2,
3837 NULL_TREE, NULL_TREE);
3838 return expr;
3841 /* Return whether OP is an expression of enum type cast to integer
3842 type. In C++ even unsigned enum types are cast to signed integer
3843 types. We do not want to issue warnings about comparisons between
3844 signed and unsigned types when one of the types is an enum type.
3845 Those warnings are always false positives in practice. */
3847 static bool
3848 enum_cast_to_int (tree op)
3850 if (CONVERT_EXPR_P (op)
3851 && TREE_TYPE (op) == integer_type_node
3852 && TREE_CODE (TREE_TYPE (TREE_OPERAND (op, 0))) == ENUMERAL_TYPE
3853 && TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (op, 0))))
3854 return true;
3856 /* The cast may have been pushed into a COND_EXPR. */
3857 if (TREE_CODE (op) == COND_EXPR)
3858 return (enum_cast_to_int (TREE_OPERAND (op, 1))
3859 || enum_cast_to_int (TREE_OPERAND (op, 2)));
3861 return false;
3864 /* For the c-common bits. */
3865 tree
3866 build_binary_op (location_t location, enum tree_code code, tree op0, tree op1,
3867 int /*convert_p*/)
3869 return cp_build_binary_op (location, code, op0, op1, tf_warning_or_error);
3873 /* Build a binary-operation expression without default conversions.
3874 CODE is the kind of expression to build.
3875 LOCATION is the location_t of the operator in the source code.
3876 This function differs from `build' in several ways:
3877 the data type of the result is computed and recorded in it,
3878 warnings are generated if arg data types are invalid,
3879 special handling for addition and subtraction of pointers is known,
3880 and some optimization is done (operations on narrow ints
3881 are done in the narrower type when that gives the same result).
3882 Constant folding is also done before the result is returned.
3884 Note that the operands will never have enumeral types
3885 because either they have just had the default conversions performed
3886 or they have both just been converted to some other type in which
3887 the arithmetic is to be done.
3889 C++: must do special pointer arithmetic when implementing
3890 multiple inheritance, and deal with pointer to member functions. */
3892 tree
3893 cp_build_binary_op (location_t location,
3894 enum tree_code code, tree orig_op0, tree orig_op1,
3895 tsubst_flags_t complain)
3897 tree op0, op1;
3898 enum tree_code code0, code1;
3899 tree type0, type1;
3900 const char *invalid_op_diag;
3902 /* Expression code to give to the expression when it is built.
3903 Normally this is CODE, which is what the caller asked for,
3904 but in some special cases we change it. */
3905 enum tree_code resultcode = code;
3907 /* Data type in which the computation is to be performed.
3908 In the simplest cases this is the common type of the arguments. */
3909 tree result_type = NULL;
3911 /* Nonzero means operands have already been type-converted
3912 in whatever way is necessary.
3913 Zero means they need to be converted to RESULT_TYPE. */
3914 int converted = 0;
3916 /* Nonzero means create the expression with this type, rather than
3917 RESULT_TYPE. */
3918 tree build_type = 0;
3920 /* Nonzero means after finally constructing the expression
3921 convert it to this type. */
3922 tree final_type = 0;
3924 tree result;
3925 tree orig_type = NULL;
3927 /* Nonzero if this is an operation like MIN or MAX which can
3928 safely be computed in short if both args are promoted shorts.
3929 Also implies COMMON.
3930 -1 indicates a bitwise operation; this makes a difference
3931 in the exact conditions for when it is safe to do the operation
3932 in a narrower mode. */
3933 int shorten = 0;
3935 /* Nonzero if this is a comparison operation;
3936 if both args are promoted shorts, compare the original shorts.
3937 Also implies COMMON. */
3938 int short_compare = 0;
3940 /* Nonzero means set RESULT_TYPE to the common type of the args. */
3941 int common = 0;
3943 /* True if both operands have arithmetic type. */
3944 bool arithmetic_types_p;
3946 /* Apply default conversions. */
3947 op0 = orig_op0;
3948 op1 = orig_op1;
3950 /* Remember whether we're doing / or %. */
3951 bool doing_div_or_mod = false;
3953 /* Remember whether we're doing << or >>. */
3954 bool doing_shift = false;
3956 /* Tree holding instrumentation expression. */
3957 tree instrument_expr = NULL;
3959 if (code == TRUTH_AND_EXPR || code == TRUTH_ANDIF_EXPR
3960 || code == TRUTH_OR_EXPR || code == TRUTH_ORIF_EXPR
3961 || code == TRUTH_XOR_EXPR)
3963 if (!really_overloaded_fn (op0) && !VOID_TYPE_P (TREE_TYPE (op0)))
3964 op0 = decay_conversion (op0, complain);
3965 if (!really_overloaded_fn (op1) && !VOID_TYPE_P (TREE_TYPE (op1)))
3966 op1 = decay_conversion (op1, complain);
3968 else
3970 if (!really_overloaded_fn (op0) && !VOID_TYPE_P (TREE_TYPE (op0)))
3971 op0 = cp_default_conversion (op0, complain);
3972 if (!really_overloaded_fn (op1) && !VOID_TYPE_P (TREE_TYPE (op1)))
3973 op1 = cp_default_conversion (op1, complain);
3976 /* Strip NON_LVALUE_EXPRs, etc., since we aren't using as an lvalue. */
3977 STRIP_TYPE_NOPS (op0);
3978 STRIP_TYPE_NOPS (op1);
3980 /* DTRT if one side is an overloaded function, but complain about it. */
3981 if (type_unknown_p (op0))
3983 tree t = instantiate_type (TREE_TYPE (op1), op0, tf_none);
3984 if (t != error_mark_node)
3986 if (complain & tf_error)
3987 permerror (input_location, "assuming cast to type %qT from overloaded function",
3988 TREE_TYPE (t));
3989 op0 = t;
3992 if (type_unknown_p (op1))
3994 tree t = instantiate_type (TREE_TYPE (op0), op1, tf_none);
3995 if (t != error_mark_node)
3997 if (complain & tf_error)
3998 permerror (input_location, "assuming cast to type %qT from overloaded function",
3999 TREE_TYPE (t));
4000 op1 = t;
4004 type0 = TREE_TYPE (op0);
4005 type1 = TREE_TYPE (op1);
4007 /* The expression codes of the data types of the arguments tell us
4008 whether the arguments are integers, floating, pointers, etc. */
4009 code0 = TREE_CODE (type0);
4010 code1 = TREE_CODE (type1);
4012 /* If an error was already reported for one of the arguments,
4013 avoid reporting another error. */
4014 if (code0 == ERROR_MARK || code1 == ERROR_MARK)
4015 return error_mark_node;
4017 if ((invalid_op_diag
4018 = targetm.invalid_binary_op (code, type0, type1)))
4020 if (complain & tf_error)
4021 error (invalid_op_diag);
4022 return error_mark_node;
4025 /* Issue warnings about peculiar, but valid, uses of NULL. */
4026 if ((orig_op0 == null_node || orig_op1 == null_node)
4027 /* It's reasonable to use pointer values as operands of &&
4028 and ||, so NULL is no exception. */
4029 && code != TRUTH_ANDIF_EXPR && code != TRUTH_ORIF_EXPR
4030 && ( /* Both are NULL (or 0) and the operation was not a
4031 comparison or a pointer subtraction. */
4032 (null_ptr_cst_p (orig_op0) && null_ptr_cst_p (orig_op1)
4033 && code != EQ_EXPR && code != NE_EXPR && code != MINUS_EXPR)
4034 /* Or if one of OP0 or OP1 is neither a pointer nor NULL. */
4035 || (!null_ptr_cst_p (orig_op0)
4036 && !TYPE_PTR_OR_PTRMEM_P (type0))
4037 || (!null_ptr_cst_p (orig_op1)
4038 && !TYPE_PTR_OR_PTRMEM_P (type1)))
4039 && (complain & tf_warning))
4041 source_location loc =
4042 expansion_point_location_if_in_system_header (input_location);
4044 warning_at (loc, OPT_Wpointer_arith, "NULL used in arithmetic");
4047 /* In case when one of the operands of the binary operation is
4048 a vector and another is a scalar -- convert scalar to vector. */
4049 if ((code0 == VECTOR_TYPE) != (code1 == VECTOR_TYPE))
4051 enum stv_conv convert_flag = scalar_to_vector (location, code, op0, op1,
4052 complain & tf_error);
4054 switch (convert_flag)
4056 case stv_error:
4057 return error_mark_node;
4058 case stv_firstarg:
4060 op0 = convert (TREE_TYPE (type1), op0);
4061 op0 = save_expr (op0);
4062 op0 = build_vector_from_val (type1, op0);
4063 type0 = TREE_TYPE (op0);
4064 code0 = TREE_CODE (type0);
4065 converted = 1;
4066 break;
4068 case stv_secondarg:
4070 op1 = convert (TREE_TYPE (type0), op1);
4071 op1 = save_expr (op1);
4072 op1 = build_vector_from_val (type0, op1);
4073 type1 = TREE_TYPE (op1);
4074 code1 = TREE_CODE (type1);
4075 converted = 1;
4076 break;
4078 default:
4079 break;
4083 switch (code)
4085 case MINUS_EXPR:
4086 /* Subtraction of two similar pointers.
4087 We must subtract them as integers, then divide by object size. */
4088 if (code0 == POINTER_TYPE && code1 == POINTER_TYPE
4089 && same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (type0),
4090 TREE_TYPE (type1)))
4091 return pointer_diff (op0, op1, common_pointer_type (type0, type1),
4092 complain);
4093 /* In all other cases except pointer - int, the usual arithmetic
4094 rules apply. */
4095 else if (!(code0 == POINTER_TYPE && code1 == INTEGER_TYPE))
4097 common = 1;
4098 break;
4100 /* The pointer - int case is just like pointer + int; fall
4101 through. */
4102 case PLUS_EXPR:
4103 if ((code0 == POINTER_TYPE || code1 == POINTER_TYPE)
4104 && (code0 == INTEGER_TYPE || code1 == INTEGER_TYPE))
4106 tree ptr_operand;
4107 tree int_operand;
4108 ptr_operand = ((code0 == POINTER_TYPE) ? op0 : op1);
4109 int_operand = ((code0 == INTEGER_TYPE) ? op0 : op1);
4110 if (processing_template_decl)
4112 result_type = TREE_TYPE (ptr_operand);
4113 break;
4115 return cp_pointer_int_sum (code,
4116 ptr_operand,
4117 int_operand,
4118 complain);
4120 common = 1;
4121 break;
4123 case MULT_EXPR:
4124 common = 1;
4125 break;
4127 case TRUNC_DIV_EXPR:
4128 case CEIL_DIV_EXPR:
4129 case FLOOR_DIV_EXPR:
4130 case ROUND_DIV_EXPR:
4131 case EXACT_DIV_EXPR:
4132 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE
4133 || code0 == COMPLEX_TYPE || code0 == VECTOR_TYPE)
4134 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
4135 || code1 == COMPLEX_TYPE || code1 == VECTOR_TYPE))
4137 enum tree_code tcode0 = code0, tcode1 = code1;
4138 tree cop1 = fold_non_dependent_expr (op1);
4139 doing_div_or_mod = true;
4140 warn_for_div_by_zero (location, cop1);
4142 if (tcode0 == COMPLEX_TYPE || tcode0 == VECTOR_TYPE)
4143 tcode0 = TREE_CODE (TREE_TYPE (TREE_TYPE (op0)));
4144 if (tcode1 == COMPLEX_TYPE || tcode1 == VECTOR_TYPE)
4145 tcode1 = TREE_CODE (TREE_TYPE (TREE_TYPE (op1)));
4147 if (!(tcode0 == INTEGER_TYPE && tcode1 == INTEGER_TYPE))
4148 resultcode = RDIV_EXPR;
4149 else
4150 /* When dividing two signed integers, we have to promote to int.
4151 unless we divide by a constant != -1. Note that default
4152 conversion will have been performed on the operands at this
4153 point, so we have to dig out the original type to find out if
4154 it was unsigned. */
4155 shorten = ((TREE_CODE (op0) == NOP_EXPR
4156 && TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (op0, 0))))
4157 || (TREE_CODE (op1) == INTEGER_CST
4158 && ! integer_all_onesp (op1)));
4160 common = 1;
4162 break;
4164 case BIT_AND_EXPR:
4165 case BIT_IOR_EXPR:
4166 case BIT_XOR_EXPR:
4167 if ((code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
4168 || (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE
4169 && !VECTOR_FLOAT_TYPE_P (type0)
4170 && !VECTOR_FLOAT_TYPE_P (type1)))
4171 shorten = -1;
4172 break;
4174 case TRUNC_MOD_EXPR:
4175 case FLOOR_MOD_EXPR:
4177 tree cop1 = fold_non_dependent_expr (op1);
4178 doing_div_or_mod = true;
4179 warn_for_div_by_zero (location, cop1);
4182 if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE
4183 && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE
4184 && TREE_CODE (TREE_TYPE (type1)) == INTEGER_TYPE)
4185 common = 1;
4186 else if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
4188 /* Although it would be tempting to shorten always here, that loses
4189 on some targets, since the modulo instruction is undefined if the
4190 quotient can't be represented in the computation mode. We shorten
4191 only if unsigned or if dividing by something we know != -1. */
4192 shorten = ((TREE_CODE (op0) == NOP_EXPR
4193 && TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (op0, 0))))
4194 || (TREE_CODE (op1) == INTEGER_CST
4195 && ! integer_all_onesp (op1)));
4196 common = 1;
4198 break;
4200 case TRUTH_ANDIF_EXPR:
4201 case TRUTH_ORIF_EXPR:
4202 case TRUTH_AND_EXPR:
4203 case TRUTH_OR_EXPR:
4204 if (!VECTOR_TYPE_P (type0) && VECTOR_TYPE_P (type1))
4206 if (!COMPARISON_CLASS_P (op1))
4207 op1 = cp_build_binary_op (EXPR_LOCATION (op1), NE_EXPR, op1,
4208 build_zero_cst (type1), complain);
4209 if (code == TRUTH_ANDIF_EXPR)
4211 tree z = build_zero_cst (TREE_TYPE (op1));
4212 return build_conditional_expr (location, op0, op1, z, complain);
4214 else if (code == TRUTH_ORIF_EXPR)
4216 tree m1 = build_all_ones_cst (TREE_TYPE (op1));
4217 return build_conditional_expr (location, op0, m1, op1, complain);
4219 else
4220 gcc_unreachable ();
4222 if (VECTOR_TYPE_P (type0))
4224 if (!COMPARISON_CLASS_P (op0))
4225 op0 = cp_build_binary_op (EXPR_LOCATION (op0), NE_EXPR, op0,
4226 build_zero_cst (type0), complain);
4227 if (!VECTOR_TYPE_P (type1))
4229 tree m1 = build_all_ones_cst (TREE_TYPE (op0));
4230 tree z = build_zero_cst (TREE_TYPE (op0));
4231 op1 = build_conditional_expr (location, op1, z, m1, complain);
4233 else if (!COMPARISON_CLASS_P (op1))
4234 op1 = cp_build_binary_op (EXPR_LOCATION (op1), NE_EXPR, op1,
4235 build_zero_cst (type1), complain);
4237 if (code == TRUTH_ANDIF_EXPR)
4238 code = BIT_AND_EXPR;
4239 else if (code == TRUTH_ORIF_EXPR)
4240 code = BIT_IOR_EXPR;
4241 else
4242 gcc_unreachable ();
4244 return cp_build_binary_op (location, code, op0, op1, complain);
4247 result_type = boolean_type_node;
4248 break;
4250 /* Shift operations: result has same type as first operand;
4251 always convert second operand to int.
4252 Also set SHORT_SHIFT if shifting rightward. */
4254 case RSHIFT_EXPR:
4255 if (code0 == VECTOR_TYPE && code1 == INTEGER_TYPE
4256 && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE)
4258 result_type = type0;
4259 converted = 1;
4261 else if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE
4262 && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE
4263 && TREE_CODE (TREE_TYPE (type1)) == INTEGER_TYPE
4264 && TYPE_VECTOR_SUBPARTS (type0) == TYPE_VECTOR_SUBPARTS (type1))
4266 result_type = type0;
4267 converted = 1;
4269 else if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
4271 tree const_op1 = fold_non_dependent_expr (op1);
4272 if (TREE_CODE (const_op1) != INTEGER_CST)
4273 const_op1 = op1;
4274 result_type = type0;
4275 doing_shift = true;
4276 if (TREE_CODE (const_op1) == INTEGER_CST)
4278 if (tree_int_cst_lt (const_op1, integer_zero_node))
4280 if ((complain & tf_warning)
4281 && c_inhibit_evaluation_warnings == 0)
4282 warning (OPT_Wshift_count_negative,
4283 "right shift count is negative");
4285 else
4287 if (compare_tree_int (const_op1, TYPE_PRECISION (type0)) >= 0
4288 && (complain & tf_warning)
4289 && c_inhibit_evaluation_warnings == 0)
4290 warning (OPT_Wshift_count_overflow,
4291 "right shift count >= width of type");
4294 /* Avoid converting op1 to result_type later. */
4295 converted = 1;
4297 break;
4299 case LSHIFT_EXPR:
4300 if (code0 == VECTOR_TYPE && code1 == INTEGER_TYPE
4301 && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE)
4303 result_type = type0;
4304 converted = 1;
4306 else if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE
4307 && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE
4308 && TREE_CODE (TREE_TYPE (type1)) == INTEGER_TYPE
4309 && TYPE_VECTOR_SUBPARTS (type0) == TYPE_VECTOR_SUBPARTS (type1))
4311 result_type = type0;
4312 converted = 1;
4314 else if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
4316 tree const_op1 = fold_non_dependent_expr (op1);
4317 if (TREE_CODE (const_op1) != INTEGER_CST)
4318 const_op1 = op1;
4319 result_type = type0;
4320 doing_shift = true;
4321 if (TREE_CODE (const_op1) == INTEGER_CST)
4323 if (tree_int_cst_lt (const_op1, integer_zero_node))
4325 if ((complain & tf_warning)
4326 && c_inhibit_evaluation_warnings == 0)
4327 warning (OPT_Wshift_count_negative,
4328 "left shift count is negative");
4330 else if (compare_tree_int (const_op1,
4331 TYPE_PRECISION (type0)) >= 0)
4333 if ((complain & tf_warning)
4334 && c_inhibit_evaluation_warnings == 0)
4335 warning (OPT_Wshift_count_overflow,
4336 "left shift count >= width of type");
4339 /* Avoid converting op1 to result_type later. */
4340 converted = 1;
4342 break;
4344 case RROTATE_EXPR:
4345 case LROTATE_EXPR:
4346 if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
4348 result_type = type0;
4349 if (TREE_CODE (op1) == INTEGER_CST)
4351 if (tree_int_cst_lt (op1, integer_zero_node))
4353 if (complain & tf_warning)
4354 warning (0, (code == LROTATE_EXPR)
4355 ? G_("left rotate count is negative")
4356 : G_("right rotate count is negative"));
4358 else if (compare_tree_int (op1, TYPE_PRECISION (type0)) >= 0)
4360 if (complain & tf_warning)
4361 warning (0, (code == LROTATE_EXPR)
4362 ? G_("left rotate count >= width of type")
4363 : G_("right rotate count >= width of type"));
4366 /* Convert the shift-count to an integer, regardless of
4367 size of value being shifted. */
4368 if (TYPE_MAIN_VARIANT (TREE_TYPE (op1)) != integer_type_node)
4369 op1 = cp_convert (integer_type_node, op1, complain);
4371 break;
4373 case EQ_EXPR:
4374 case NE_EXPR:
4375 if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE)
4376 goto vector_compare;
4377 if ((complain & tf_warning)
4378 && (FLOAT_TYPE_P (type0) || FLOAT_TYPE_P (type1)))
4379 warning (OPT_Wfloat_equal,
4380 "comparing floating point with == or != is unsafe");
4381 if ((complain & tf_warning)
4382 && ((TREE_CODE (orig_op0) == STRING_CST && !integer_zerop (op1))
4383 || (TREE_CODE (orig_op1) == STRING_CST && !integer_zerop (op0))))
4384 warning (OPT_Waddress, "comparison with string literal results in unspecified behaviour");
4386 build_type = boolean_type_node;
4387 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE
4388 || code0 == COMPLEX_TYPE || code0 == ENUMERAL_TYPE)
4389 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
4390 || code1 == COMPLEX_TYPE || code1 == ENUMERAL_TYPE))
4391 short_compare = 1;
4392 else if (((code0 == POINTER_TYPE || TYPE_PTRDATAMEM_P (type0))
4393 && null_ptr_cst_p (op1))
4394 /* Handle, eg, (void*)0 (c++/43906), and more. */
4395 || (code0 == POINTER_TYPE
4396 && TYPE_PTR_P (type1) && integer_zerop (op1)))
4398 if (TYPE_PTR_P (type1))
4399 result_type = composite_pointer_type (type0, type1, op0, op1,
4400 CPO_COMPARISON, complain);
4401 else
4402 result_type = type0;
4404 if (TREE_CODE (op0) == ADDR_EXPR
4405 && decl_with_nonnull_addr_p (TREE_OPERAND (op0, 0)))
4407 if ((complain & tf_warning)
4408 && c_inhibit_evaluation_warnings == 0)
4409 warning (OPT_Waddress, "the address of %qD will never be NULL",
4410 TREE_OPERAND (op0, 0));
4413 else if (((code1 == POINTER_TYPE || TYPE_PTRDATAMEM_P (type1))
4414 && null_ptr_cst_p (op0))
4415 /* Handle, eg, (void*)0 (c++/43906), and more. */
4416 || (code1 == POINTER_TYPE
4417 && TYPE_PTR_P (type0) && integer_zerop (op0)))
4419 if (TYPE_PTR_P (type0))
4420 result_type = composite_pointer_type (type0, type1, op0, op1,
4421 CPO_COMPARISON, complain);
4422 else
4423 result_type = type1;
4425 if (TREE_CODE (op1) == ADDR_EXPR
4426 && decl_with_nonnull_addr_p (TREE_OPERAND (op1, 0)))
4428 if ((complain & tf_warning)
4429 && c_inhibit_evaluation_warnings == 0)
4430 warning (OPT_Waddress, "the address of %qD will never be NULL",
4431 TREE_OPERAND (op1, 0));
4434 else if ((code0 == POINTER_TYPE && code1 == POINTER_TYPE)
4435 || (TYPE_PTRDATAMEM_P (type0) && TYPE_PTRDATAMEM_P (type1)))
4436 result_type = composite_pointer_type (type0, type1, op0, op1,
4437 CPO_COMPARISON, complain);
4438 else if (null_ptr_cst_p (op0) && null_ptr_cst_p (op1))
4439 /* One of the operands must be of nullptr_t type. */
4440 result_type = TREE_TYPE (nullptr_node);
4441 else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
4443 result_type = type0;
4444 if (complain & tf_error)
4445 permerror (input_location, "ISO C++ forbids comparison between pointer and integer");
4446 else
4447 return error_mark_node;
4449 else if (code0 == INTEGER_TYPE && code1 == POINTER_TYPE)
4451 result_type = type1;
4452 if (complain & tf_error)
4453 permerror (input_location, "ISO C++ forbids comparison between pointer and integer");
4454 else
4455 return error_mark_node;
4457 else if (TYPE_PTRMEMFUNC_P (type0) && null_ptr_cst_p (op1))
4459 if (TARGET_PTRMEMFUNC_VBIT_LOCATION
4460 == ptrmemfunc_vbit_in_delta)
4462 tree pfn0, delta0, e1, e2;
4464 if (TREE_SIDE_EFFECTS (op0))
4465 op0 = save_expr (op0);
4467 pfn0 = pfn_from_ptrmemfunc (op0);
4468 delta0 = delta_from_ptrmemfunc (op0);
4469 e1 = cp_build_binary_op (location,
4470 EQ_EXPR,
4471 pfn0,
4472 build_zero_cst (TREE_TYPE (pfn0)),
4473 complain);
4474 e2 = cp_build_binary_op (location,
4475 BIT_AND_EXPR,
4476 delta0,
4477 integer_one_node,
4478 complain);
4480 if (complain & tf_warning)
4481 maybe_warn_zero_as_null_pointer_constant (op1, input_location);
4483 e2 = cp_build_binary_op (location,
4484 EQ_EXPR, e2, integer_zero_node,
4485 complain);
4486 op0 = cp_build_binary_op (location,
4487 TRUTH_ANDIF_EXPR, e1, e2,
4488 complain);
4489 op1 = cp_convert (TREE_TYPE (op0), integer_one_node, complain);
4491 else
4493 op0 = build_ptrmemfunc_access_expr (op0, pfn_identifier);
4494 op1 = cp_convert (TREE_TYPE (op0), op1, complain);
4496 result_type = TREE_TYPE (op0);
4498 else if (TYPE_PTRMEMFUNC_P (type1) && null_ptr_cst_p (op0))
4499 return cp_build_binary_op (location, code, op1, op0, complain);
4500 else if (TYPE_PTRMEMFUNC_P (type0) && TYPE_PTRMEMFUNC_P (type1))
4502 tree type;
4503 /* E will be the final comparison. */
4504 tree e;
4505 /* E1 and E2 are for scratch. */
4506 tree e1;
4507 tree e2;
4508 tree pfn0;
4509 tree pfn1;
4510 tree delta0;
4511 tree delta1;
4513 type = composite_pointer_type (type0, type1, op0, op1,
4514 CPO_COMPARISON, complain);
4516 if (!same_type_p (TREE_TYPE (op0), type))
4517 op0 = cp_convert_and_check (type, op0, complain);
4518 if (!same_type_p (TREE_TYPE (op1), type))
4519 op1 = cp_convert_and_check (type, op1, complain);
4521 if (op0 == error_mark_node || op1 == error_mark_node)
4522 return error_mark_node;
4524 if (TREE_SIDE_EFFECTS (op0))
4525 op0 = save_expr (op0);
4526 if (TREE_SIDE_EFFECTS (op1))
4527 op1 = save_expr (op1);
4529 pfn0 = pfn_from_ptrmemfunc (op0);
4530 pfn1 = pfn_from_ptrmemfunc (op1);
4531 delta0 = delta_from_ptrmemfunc (op0);
4532 delta1 = delta_from_ptrmemfunc (op1);
4533 if (TARGET_PTRMEMFUNC_VBIT_LOCATION
4534 == ptrmemfunc_vbit_in_delta)
4536 /* We generate:
4538 (op0.pfn == op1.pfn
4539 && ((op0.delta == op1.delta)
4540 || (!op0.pfn && op0.delta & 1 == 0
4541 && op1.delta & 1 == 0))
4543 The reason for the `!op0.pfn' bit is that a NULL
4544 pointer-to-member is any member with a zero PFN and
4545 LSB of the DELTA field is 0. */
4547 e1 = cp_build_binary_op (location, BIT_AND_EXPR,
4548 delta0,
4549 integer_one_node,
4550 complain);
4551 e1 = cp_build_binary_op (location,
4552 EQ_EXPR, e1, integer_zero_node,
4553 complain);
4554 e2 = cp_build_binary_op (location, BIT_AND_EXPR,
4555 delta1,
4556 integer_one_node,
4557 complain);
4558 e2 = cp_build_binary_op (location,
4559 EQ_EXPR, e2, integer_zero_node,
4560 complain);
4561 e1 = cp_build_binary_op (location,
4562 TRUTH_ANDIF_EXPR, e2, e1,
4563 complain);
4564 e2 = cp_build_binary_op (location, EQ_EXPR,
4565 pfn0,
4566 build_zero_cst (TREE_TYPE (pfn0)),
4567 complain);
4568 e2 = cp_build_binary_op (location,
4569 TRUTH_ANDIF_EXPR, e2, e1, complain);
4570 e1 = cp_build_binary_op (location,
4571 EQ_EXPR, delta0, delta1, complain);
4572 e1 = cp_build_binary_op (location,
4573 TRUTH_ORIF_EXPR, e1, e2, complain);
4575 else
4577 /* We generate:
4579 (op0.pfn == op1.pfn
4580 && (!op0.pfn || op0.delta == op1.delta))
4582 The reason for the `!op0.pfn' bit is that a NULL
4583 pointer-to-member is any member with a zero PFN; the
4584 DELTA field is unspecified. */
4586 e1 = cp_build_binary_op (location,
4587 EQ_EXPR, delta0, delta1, complain);
4588 e2 = cp_build_binary_op (location,
4589 EQ_EXPR,
4590 pfn0,
4591 build_zero_cst (TREE_TYPE (pfn0)),
4592 complain);
4593 e1 = cp_build_binary_op (location,
4594 TRUTH_ORIF_EXPR, e1, e2, complain);
4596 e2 = build2 (EQ_EXPR, boolean_type_node, pfn0, pfn1);
4597 e = cp_build_binary_op (location,
4598 TRUTH_ANDIF_EXPR, e2, e1, complain);
4599 if (code == EQ_EXPR)
4600 return e;
4601 return cp_build_binary_op (location,
4602 EQ_EXPR, e, integer_zero_node, complain);
4604 else
4606 gcc_assert (!TYPE_PTRMEMFUNC_P (type0)
4607 || !same_type_p (TYPE_PTRMEMFUNC_FN_TYPE (type0),
4608 type1));
4609 gcc_assert (!TYPE_PTRMEMFUNC_P (type1)
4610 || !same_type_p (TYPE_PTRMEMFUNC_FN_TYPE (type1),
4611 type0));
4614 break;
4616 case MAX_EXPR:
4617 case MIN_EXPR:
4618 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE)
4619 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE))
4620 shorten = 1;
4621 else if (code0 == POINTER_TYPE && code1 == POINTER_TYPE)
4622 result_type = composite_pointer_type (type0, type1, op0, op1,
4623 CPO_COMPARISON, complain);
4624 break;
4626 case LE_EXPR:
4627 case GE_EXPR:
4628 case LT_EXPR:
4629 case GT_EXPR:
4630 if (TREE_CODE (orig_op0) == STRING_CST
4631 || TREE_CODE (orig_op1) == STRING_CST)
4633 if (complain & tf_warning)
4634 warning (OPT_Waddress, "comparison with string literal results in unspecified behaviour");
4637 if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE)
4639 vector_compare:
4640 tree intt;
4641 if (!same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (type0),
4642 TREE_TYPE (type1))
4643 && !vector_types_compatible_elements_p (type0, type1))
4645 if (complain & tf_error)
4647 error_at (location, "comparing vectors with different "
4648 "element types");
4649 inform (location, "operand types are %qT and %qT",
4650 type0, type1);
4652 return error_mark_node;
4655 if (TYPE_VECTOR_SUBPARTS (type0) != TYPE_VECTOR_SUBPARTS (type1))
4657 if (complain & tf_error)
4659 error_at (location, "comparing vectors with different "
4660 "number of elements");
4661 inform (location, "operand types are %qT and %qT",
4662 type0, type1);
4664 return error_mark_node;
4667 /* Always construct signed integer vector type. */
4668 intt = c_common_type_for_size (GET_MODE_BITSIZE
4669 (TYPE_MODE (TREE_TYPE (type0))), 0);
4670 if (!intt)
4672 if (complain & tf_error)
4673 error_at (location, "could not find an integer type "
4674 "of the same size as %qT", TREE_TYPE (type0));
4675 return error_mark_node;
4677 result_type = build_opaque_vector_type (intt,
4678 TYPE_VECTOR_SUBPARTS (type0));
4679 converted = 1;
4680 break;
4682 build_type = boolean_type_node;
4683 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE
4684 || code0 == ENUMERAL_TYPE)
4685 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
4686 || code1 == ENUMERAL_TYPE))
4687 short_compare = 1;
4688 else if (code0 == POINTER_TYPE && code1 == POINTER_TYPE)
4689 result_type = composite_pointer_type (type0, type1, op0, op1,
4690 CPO_COMPARISON, complain);
4691 else if (code0 == POINTER_TYPE && null_ptr_cst_p (op1))
4693 result_type = type0;
4694 if (extra_warnings && (complain & tf_warning))
4695 warning (OPT_Wextra,
4696 "ordered comparison of pointer with integer zero");
4698 else if (code1 == POINTER_TYPE && null_ptr_cst_p (op0))
4700 result_type = type1;
4701 if (extra_warnings && (complain & tf_warning))
4702 warning (OPT_Wextra,
4703 "ordered comparison of pointer with integer zero");
4705 else if (null_ptr_cst_p (op0) && null_ptr_cst_p (op1))
4706 /* One of the operands must be of nullptr_t type. */
4707 result_type = TREE_TYPE (nullptr_node);
4708 else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
4710 result_type = type0;
4711 if (complain & tf_error)
4712 permerror (input_location, "ISO C++ forbids comparison between pointer and integer");
4713 else
4714 return error_mark_node;
4716 else if (code0 == INTEGER_TYPE && code1 == POINTER_TYPE)
4718 result_type = type1;
4719 if (complain & tf_error)
4720 permerror (input_location, "ISO C++ forbids comparison between pointer and integer");
4721 else
4722 return error_mark_node;
4724 break;
4726 case UNORDERED_EXPR:
4727 case ORDERED_EXPR:
4728 case UNLT_EXPR:
4729 case UNLE_EXPR:
4730 case UNGT_EXPR:
4731 case UNGE_EXPR:
4732 case UNEQ_EXPR:
4733 build_type = integer_type_node;
4734 if (code0 != REAL_TYPE || code1 != REAL_TYPE)
4736 if (complain & tf_error)
4737 error ("unordered comparison on non-floating point argument");
4738 return error_mark_node;
4740 common = 1;
4741 break;
4743 default:
4744 break;
4747 if (((code0 == INTEGER_TYPE || code0 == REAL_TYPE || code0 == COMPLEX_TYPE
4748 || code0 == ENUMERAL_TYPE)
4749 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
4750 || code1 == COMPLEX_TYPE || code1 == ENUMERAL_TYPE)))
4751 arithmetic_types_p = 1;
4752 else
4754 arithmetic_types_p = 0;
4755 /* Vector arithmetic is only allowed when both sides are vectors. */
4756 if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE)
4758 if (!tree_int_cst_equal (TYPE_SIZE (type0), TYPE_SIZE (type1))
4759 || !vector_types_compatible_elements_p (type0, type1))
4761 if (complain & tf_error)
4762 binary_op_error (location, code, type0, type1);
4763 return error_mark_node;
4765 arithmetic_types_p = 1;
4768 /* Determine the RESULT_TYPE, if it is not already known. */
4769 if (!result_type
4770 && arithmetic_types_p
4771 && (shorten || common || short_compare))
4773 result_type = cp_common_type (type0, type1);
4774 if (complain & tf_warning)
4775 do_warn_double_promotion (result_type, type0, type1,
4776 "implicit conversion from %qT to %qT "
4777 "to match other operand of binary "
4778 "expression",
4779 location);
4782 if (!result_type)
4784 if (complain & tf_error)
4785 error ("invalid operands of types %qT and %qT to binary %qO",
4786 TREE_TYPE (orig_op0), TREE_TYPE (orig_op1), code);
4787 return error_mark_node;
4790 /* If we're in a template, the only thing we need to know is the
4791 RESULT_TYPE. */
4792 if (processing_template_decl)
4794 /* Since the middle-end checks the type when doing a build2, we
4795 need to build the tree in pieces. This built tree will never
4796 get out of the front-end as we replace it when instantiating
4797 the template. */
4798 tree tmp = build2 (resultcode,
4799 build_type ? build_type : result_type,
4800 NULL_TREE, op1);
4801 TREE_OPERAND (tmp, 0) = op0;
4802 return tmp;
4805 if (arithmetic_types_p)
4807 bool first_complex = (code0 == COMPLEX_TYPE);
4808 bool second_complex = (code1 == COMPLEX_TYPE);
4809 int none_complex = (!first_complex && !second_complex);
4811 /* Adapted from patch for c/24581. */
4812 if (first_complex != second_complex
4813 && (code == PLUS_EXPR
4814 || code == MINUS_EXPR
4815 || code == MULT_EXPR
4816 || (code == TRUNC_DIV_EXPR && first_complex))
4817 && TREE_CODE (TREE_TYPE (result_type)) == REAL_TYPE
4818 && flag_signed_zeros)
4820 /* An operation on mixed real/complex operands must be
4821 handled specially, but the language-independent code can
4822 more easily optimize the plain complex arithmetic if
4823 -fno-signed-zeros. */
4824 tree real_type = TREE_TYPE (result_type);
4825 tree real, imag;
4826 if (first_complex)
4828 if (TREE_TYPE (op0) != result_type)
4829 op0 = cp_convert_and_check (result_type, op0, complain);
4830 if (TREE_TYPE (op1) != real_type)
4831 op1 = cp_convert_and_check (real_type, op1, complain);
4833 else
4835 if (TREE_TYPE (op0) != real_type)
4836 op0 = cp_convert_and_check (real_type, op0, complain);
4837 if (TREE_TYPE (op1) != result_type)
4838 op1 = cp_convert_and_check (result_type, op1, complain);
4840 if (TREE_CODE (op0) == ERROR_MARK || TREE_CODE (op1) == ERROR_MARK)
4841 return error_mark_node;
4842 if (first_complex)
4844 op0 = save_expr (op0);
4845 real = cp_build_unary_op (REALPART_EXPR, op0, 1, complain);
4846 imag = cp_build_unary_op (IMAGPART_EXPR, op0, 1, complain);
4847 switch (code)
4849 case MULT_EXPR:
4850 case TRUNC_DIV_EXPR:
4851 op1 = save_expr (op1);
4852 imag = build2 (resultcode, real_type, imag, op1);
4853 /* Fall through. */
4854 case PLUS_EXPR:
4855 case MINUS_EXPR:
4856 real = build2 (resultcode, real_type, real, op1);
4857 break;
4858 default:
4859 gcc_unreachable();
4862 else
4864 op1 = save_expr (op1);
4865 real = cp_build_unary_op (REALPART_EXPR, op1, 1, complain);
4866 imag = cp_build_unary_op (IMAGPART_EXPR, op1, 1, complain);
4867 switch (code)
4869 case MULT_EXPR:
4870 op0 = save_expr (op0);
4871 imag = build2 (resultcode, real_type, op0, imag);
4872 /* Fall through. */
4873 case PLUS_EXPR:
4874 real = build2 (resultcode, real_type, op0, real);
4875 break;
4876 case MINUS_EXPR:
4877 real = build2 (resultcode, real_type, op0, real);
4878 imag = build1 (NEGATE_EXPR, real_type, imag);
4879 break;
4880 default:
4881 gcc_unreachable();
4884 real = fold_if_not_in_template (real);
4885 imag = fold_if_not_in_template (imag);
4886 result = build2 (COMPLEX_EXPR, result_type, real, imag);
4887 result = fold_if_not_in_template (result);
4888 return result;
4891 /* For certain operations (which identify themselves by shorten != 0)
4892 if both args were extended from the same smaller type,
4893 do the arithmetic in that type and then extend.
4895 shorten !=0 and !=1 indicates a bitwise operation.
4896 For them, this optimization is safe only if
4897 both args are zero-extended or both are sign-extended.
4898 Otherwise, we might change the result.
4899 E.g., (short)-1 | (unsigned short)-1 is (int)-1
4900 but calculated in (unsigned short) it would be (unsigned short)-1. */
4902 if (shorten && none_complex)
4904 orig_type = result_type;
4905 final_type = result_type;
4906 result_type = shorten_binary_op (result_type, op0, op1,
4907 shorten == -1);
4910 /* Comparison operations are shortened too but differently.
4911 They identify themselves by setting short_compare = 1. */
4913 if (short_compare)
4915 /* Don't write &op0, etc., because that would prevent op0
4916 from being kept in a register.
4917 Instead, make copies of the our local variables and
4918 pass the copies by reference, then copy them back afterward. */
4919 tree xop0 = op0, xop1 = op1, xresult_type = result_type;
4920 enum tree_code xresultcode = resultcode;
4921 tree val
4922 = shorten_compare (location, &xop0, &xop1, &xresult_type,
4923 &xresultcode);
4924 if (val != 0)
4925 return cp_convert (boolean_type_node, val, complain);
4926 op0 = xop0, op1 = xop1;
4927 converted = 1;
4928 resultcode = xresultcode;
4931 if ((short_compare || code == MIN_EXPR || code == MAX_EXPR)
4932 && warn_sign_compare
4933 /* Do not warn until the template is instantiated; we cannot
4934 bound the ranges of the arguments until that point. */
4935 && !processing_template_decl
4936 && (complain & tf_warning)
4937 && c_inhibit_evaluation_warnings == 0
4938 /* Even unsigned enum types promote to signed int. We don't
4939 want to issue -Wsign-compare warnings for this case. */
4940 && !enum_cast_to_int (orig_op0)
4941 && !enum_cast_to_int (orig_op1))
4943 tree oop0 = maybe_constant_value (orig_op0);
4944 tree oop1 = maybe_constant_value (orig_op1);
4946 if (TREE_CODE (oop0) != INTEGER_CST)
4947 oop0 = orig_op0;
4948 if (TREE_CODE (oop1) != INTEGER_CST)
4949 oop1 = orig_op1;
4950 warn_for_sign_compare (location, oop0, oop1, op0, op1,
4951 result_type, resultcode);
4955 /* If CONVERTED is zero, both args will be converted to type RESULT_TYPE.
4956 Then the expression will be built.
4957 It will be given type FINAL_TYPE if that is nonzero;
4958 otherwise, it will be given type RESULT_TYPE. */
4959 if (! converted)
4961 if (TREE_TYPE (op0) != result_type)
4962 op0 = cp_convert_and_check (result_type, op0, complain);
4963 if (TREE_TYPE (op1) != result_type)
4964 op1 = cp_convert_and_check (result_type, op1, complain);
4966 if (op0 == error_mark_node || op1 == error_mark_node)
4967 return error_mark_node;
4970 if (build_type == NULL_TREE)
4971 build_type = result_type;
4973 if ((flag_sanitize & (SANITIZE_SHIFT | SANITIZE_DIVIDE
4974 | SANITIZE_FLOAT_DIVIDE))
4975 && !processing_template_decl
4976 && do_ubsan_in_current_function ()
4977 && (doing_div_or_mod || doing_shift))
4979 /* OP0 and/or OP1 might have side-effects. */
4980 op0 = cp_save_expr (op0);
4981 op1 = cp_save_expr (op1);
4982 op0 = fold_non_dependent_expr (op0);
4983 op1 = fold_non_dependent_expr (op1);
4984 if (doing_div_or_mod && (flag_sanitize & (SANITIZE_DIVIDE
4985 | SANITIZE_FLOAT_DIVIDE)))
4987 /* For diagnostics we want to use the promoted types without
4988 shorten_binary_op. So convert the arguments to the
4989 original result_type. */
4990 tree cop0 = op0;
4991 tree cop1 = op1;
4992 if (orig_type != NULL && result_type != orig_type)
4994 cop0 = cp_convert (orig_type, op0, complain);
4995 cop1 = cp_convert (orig_type, op1, complain);
4997 instrument_expr = ubsan_instrument_division (location, cop0, cop1);
4999 else if (doing_shift && (flag_sanitize & SANITIZE_SHIFT))
5000 instrument_expr = ubsan_instrument_shift (location, code, op0, op1);
5003 result = build2 (resultcode, build_type, op0, op1);
5004 result = fold_if_not_in_template (result);
5005 if (final_type != 0)
5006 result = cp_convert (final_type, result, complain);
5008 if (TREE_OVERFLOW_P (result)
5009 && !TREE_OVERFLOW_P (op0)
5010 && !TREE_OVERFLOW_P (op1))
5011 overflow_warning (location, result);
5013 if (instrument_expr != NULL)
5014 result = fold_build2 (COMPOUND_EXPR, TREE_TYPE (result),
5015 instrument_expr, result);
5017 return result;
5020 /* Build a VEC_PERM_EXPR.
5021 This is a simple wrapper for c_build_vec_perm_expr. */
5022 tree
5023 build_x_vec_perm_expr (location_t loc,
5024 tree arg0, tree arg1, tree arg2,
5025 tsubst_flags_t complain)
5027 tree orig_arg0 = arg0;
5028 tree orig_arg1 = arg1;
5029 tree orig_arg2 = arg2;
5030 if (processing_template_decl)
5032 if (type_dependent_expression_p (arg0)
5033 || type_dependent_expression_p (arg1)
5034 || type_dependent_expression_p (arg2))
5035 return build_min_nt_loc (loc, VEC_PERM_EXPR, arg0, arg1, arg2);
5036 arg0 = build_non_dependent_expr (arg0);
5037 if (arg1)
5038 arg1 = build_non_dependent_expr (arg1);
5039 arg2 = build_non_dependent_expr (arg2);
5041 tree exp = c_build_vec_perm_expr (loc, arg0, arg1, arg2, complain & tf_error);
5042 if (processing_template_decl && exp != error_mark_node)
5043 return build_min_non_dep (VEC_PERM_EXPR, exp, orig_arg0,
5044 orig_arg1, orig_arg2);
5045 return exp;
5048 /* Return a tree for the sum or difference (RESULTCODE says which)
5049 of pointer PTROP and integer INTOP. */
5051 static tree
5052 cp_pointer_int_sum (enum tree_code resultcode, tree ptrop, tree intop,
5053 tsubst_flags_t complain)
5055 tree res_type = TREE_TYPE (ptrop);
5057 /* pointer_int_sum() uses size_in_bytes() on the TREE_TYPE(res_type)
5058 in certain circumstance (when it's valid to do so). So we need
5059 to make sure it's complete. We don't need to check here, if we
5060 can actually complete it at all, as those checks will be done in
5061 pointer_int_sum() anyway. */
5062 complete_type (TREE_TYPE (res_type));
5064 return pointer_int_sum (input_location, resultcode, ptrop,
5065 fold_if_not_in_template (intop),
5066 complain & tf_warning_or_error);
5069 /* Return a tree for the difference of pointers OP0 and OP1.
5070 The resulting tree has type int. */
5072 static tree
5073 pointer_diff (tree op0, tree op1, tree ptrtype, tsubst_flags_t complain)
5075 tree result;
5076 tree restype = ptrdiff_type_node;
5077 tree target_type = TREE_TYPE (ptrtype);
5079 if (!complete_type_or_else (target_type, NULL_TREE))
5080 return error_mark_node;
5082 if (VOID_TYPE_P (target_type))
5084 if (complain & tf_error)
5085 permerror (input_location, "ISO C++ forbids using pointer of "
5086 "type %<void *%> in subtraction");
5087 else
5088 return error_mark_node;
5090 if (TREE_CODE (target_type) == FUNCTION_TYPE)
5092 if (complain & tf_error)
5093 permerror (input_location, "ISO C++ forbids using pointer to "
5094 "a function in subtraction");
5095 else
5096 return error_mark_node;
5098 if (TREE_CODE (target_type) == METHOD_TYPE)
5100 if (complain & tf_error)
5101 permerror (input_location, "ISO C++ forbids using pointer to "
5102 "a method in subtraction");
5103 else
5104 return error_mark_node;
5107 /* First do the subtraction as integers;
5108 then drop through to build the divide operator. */
5110 op0 = cp_build_binary_op (input_location,
5111 MINUS_EXPR,
5112 cp_convert (restype, op0, complain),
5113 cp_convert (restype, op1, complain),
5114 complain);
5116 /* This generates an error if op1 is a pointer to an incomplete type. */
5117 if (!COMPLETE_TYPE_P (TREE_TYPE (TREE_TYPE (op1))))
5119 if (complain & tf_error)
5120 error ("invalid use of a pointer to an incomplete type in "
5121 "pointer arithmetic");
5122 else
5123 return error_mark_node;
5126 if (pointer_to_zero_sized_aggr_p (TREE_TYPE (op1)))
5128 if (complain & tf_error)
5129 error ("arithmetic on pointer to an empty aggregate");
5130 else
5131 return error_mark_node;
5134 op1 = (TYPE_PTROB_P (ptrtype)
5135 ? size_in_bytes (target_type)
5136 : integer_one_node);
5138 /* Do the division. */
5140 result = build2 (EXACT_DIV_EXPR, restype, op0,
5141 cp_convert (restype, op1, complain));
5142 return fold_if_not_in_template (result);
5145 /* Construct and perhaps optimize a tree representation
5146 for a unary operation. CODE, a tree_code, specifies the operation
5147 and XARG is the operand. */
5149 tree
5150 build_x_unary_op (location_t loc, enum tree_code code, tree xarg,
5151 tsubst_flags_t complain)
5153 tree orig_expr = xarg;
5154 tree exp;
5155 int ptrmem = 0;
5157 if (processing_template_decl)
5159 if (type_dependent_expression_p (xarg))
5160 return build_min_nt_loc (loc, code, xarg, NULL_TREE);
5162 xarg = build_non_dependent_expr (xarg);
5165 exp = NULL_TREE;
5167 /* [expr.unary.op] says:
5169 The address of an object of incomplete type can be taken.
5171 (And is just the ordinary address operator, not an overloaded
5172 "operator &".) However, if the type is a template
5173 specialization, we must complete the type at this point so that
5174 an overloaded "operator &" will be available if required. */
5175 if (code == ADDR_EXPR
5176 && TREE_CODE (xarg) != TEMPLATE_ID_EXPR
5177 && ((CLASS_TYPE_P (TREE_TYPE (xarg))
5178 && !COMPLETE_TYPE_P (complete_type (TREE_TYPE (xarg))))
5179 || (TREE_CODE (xarg) == OFFSET_REF)))
5180 /* Don't look for a function. */;
5181 else
5182 exp = build_new_op (loc, code, LOOKUP_NORMAL, xarg, NULL_TREE,
5183 NULL_TREE, /*overload=*/NULL, complain);
5184 if (!exp && code == ADDR_EXPR)
5186 if (is_overloaded_fn (xarg))
5188 tree fn = get_first_fn (xarg);
5189 if (DECL_CONSTRUCTOR_P (fn) || DECL_DESTRUCTOR_P (fn))
5191 if (complain & tf_error)
5192 error (DECL_CONSTRUCTOR_P (fn)
5193 ? G_("taking address of constructor %qE")
5194 : G_("taking address of destructor %qE"),
5195 xarg);
5196 return error_mark_node;
5200 /* A pointer to member-function can be formed only by saying
5201 &X::mf. */
5202 if (!flag_ms_extensions && TREE_CODE (TREE_TYPE (xarg)) == METHOD_TYPE
5203 && (TREE_CODE (xarg) != OFFSET_REF || !PTRMEM_OK_P (xarg)))
5205 if (TREE_CODE (xarg) != OFFSET_REF
5206 || !TYPE_P (TREE_OPERAND (xarg, 0)))
5208 if (complain & tf_error)
5210 error ("invalid use of %qE to form a "
5211 "pointer-to-member-function", xarg);
5212 if (TREE_CODE (xarg) != OFFSET_REF)
5213 inform (input_location, " a qualified-id is required");
5215 return error_mark_node;
5217 else
5219 if (complain & tf_error)
5220 error ("parentheses around %qE cannot be used to form a"
5221 " pointer-to-member-function",
5222 xarg);
5223 else
5224 return error_mark_node;
5225 PTRMEM_OK_P (xarg) = 1;
5229 if (TREE_CODE (xarg) == OFFSET_REF)
5231 ptrmem = PTRMEM_OK_P (xarg);
5233 if (!ptrmem && !flag_ms_extensions
5234 && TREE_CODE (TREE_TYPE (TREE_OPERAND (xarg, 1))) == METHOD_TYPE)
5236 /* A single non-static member, make sure we don't allow a
5237 pointer-to-member. */
5238 xarg = build2 (OFFSET_REF, TREE_TYPE (xarg),
5239 TREE_OPERAND (xarg, 0),
5240 ovl_cons (TREE_OPERAND (xarg, 1), NULL_TREE));
5241 PTRMEM_OK_P (xarg) = ptrmem;
5245 exp = cp_build_addr_expr_strict (xarg, complain);
5248 if (processing_template_decl && exp != error_mark_node)
5249 exp = build_min_non_dep (code, exp, orig_expr,
5250 /*For {PRE,POST}{INC,DEC}REMENT_EXPR*/NULL_TREE);
5251 if (TREE_CODE (exp) == ADDR_EXPR)
5252 PTRMEM_OK_P (exp) = ptrmem;
5253 return exp;
5256 /* Like c_common_truthvalue_conversion, but handle pointer-to-member
5257 constants, where a null value is represented by an INTEGER_CST of
5258 -1. */
5260 tree
5261 cp_truthvalue_conversion (tree expr)
5263 tree type = TREE_TYPE (expr);
5264 if (TYPE_PTRDATAMEM_P (type)
5265 /* Avoid ICE on invalid use of non-static member function. */
5266 || TREE_CODE (expr) == FUNCTION_DECL)
5267 return build_binary_op (EXPR_LOCATION (expr),
5268 NE_EXPR, expr, nullptr_node, 1);
5269 else if (TYPE_PTR_P (type) || TYPE_PTRMEMFUNC_P (type))
5271 /* With -Wzero-as-null-pointer-constant do not warn for an
5272 'if (p)' or a 'while (!p)', where p is a pointer. */
5273 tree ret;
5274 ++c_inhibit_evaluation_warnings;
5275 ret = c_common_truthvalue_conversion (input_location, expr);
5276 --c_inhibit_evaluation_warnings;
5277 return ret;
5279 else
5280 return c_common_truthvalue_conversion (input_location, expr);
5283 /* Just like cp_truthvalue_conversion, but we want a CLEANUP_POINT_EXPR. */
5285 tree
5286 condition_conversion (tree expr)
5288 tree t;
5289 if (processing_template_decl)
5290 return expr;
5291 t = perform_implicit_conversion_flags (boolean_type_node, expr,
5292 tf_warning_or_error, LOOKUP_NORMAL);
5293 t = fold_build_cleanup_point_expr (boolean_type_node, t);
5294 return t;
5297 /* Returns the address of T. This function will fold away
5298 ADDR_EXPR of INDIRECT_REF. */
5300 tree
5301 build_address (tree t)
5303 if (error_operand_p (t) || !cxx_mark_addressable (t))
5304 return error_mark_node;
5305 gcc_checking_assert (TREE_CODE (t) != CONSTRUCTOR);
5306 t = build_fold_addr_expr (t);
5307 if (TREE_CODE (t) != ADDR_EXPR)
5308 t = rvalue (t);
5309 return t;
5312 /* Return a NOP_EXPR converting EXPR to TYPE. */
5314 tree
5315 build_nop (tree type, tree expr)
5317 if (type == error_mark_node || error_operand_p (expr))
5318 return expr;
5319 return build1 (NOP_EXPR, type, expr);
5322 /* Take the address of ARG, whatever that means under C++ semantics.
5323 If STRICT_LVALUE is true, require an lvalue; otherwise, allow xvalues
5324 and class rvalues as well.
5326 Nothing should call this function directly; instead, callers should use
5327 cp_build_addr_expr or cp_build_addr_expr_strict. */
5329 static tree
5330 cp_build_addr_expr_1 (tree arg, bool strict_lvalue, tsubst_flags_t complain)
5332 tree argtype;
5333 tree val;
5335 if (!arg || error_operand_p (arg))
5336 return error_mark_node;
5338 arg = mark_lvalue_use (arg);
5339 argtype = lvalue_type (arg);
5341 gcc_assert (!identifier_p (arg) || !IDENTIFIER_OPNAME_P (arg));
5343 if (TREE_CODE (arg) == COMPONENT_REF && type_unknown_p (arg)
5344 && !really_overloaded_fn (TREE_OPERAND (arg, 1)))
5346 /* They're trying to take the address of a unique non-static
5347 member function. This is ill-formed (except in MS-land),
5348 but let's try to DTRT.
5349 Note: We only handle unique functions here because we don't
5350 want to complain if there's a static overload; non-unique
5351 cases will be handled by instantiate_type. But we need to
5352 handle this case here to allow casts on the resulting PMF.
5353 We could defer this in non-MS mode, but it's easier to give
5354 a useful error here. */
5356 /* Inside constant member functions, the `this' pointer
5357 contains an extra const qualifier. TYPE_MAIN_VARIANT
5358 is used here to remove this const from the diagnostics
5359 and the created OFFSET_REF. */
5360 tree base = TYPE_MAIN_VARIANT (TREE_TYPE (TREE_OPERAND (arg, 0)));
5361 tree fn = get_first_fn (TREE_OPERAND (arg, 1));
5362 mark_used (fn);
5364 if (! flag_ms_extensions)
5366 tree name = DECL_NAME (fn);
5367 if (!(complain & tf_error))
5368 return error_mark_node;
5369 else if (current_class_type
5370 && TREE_OPERAND (arg, 0) == current_class_ref)
5371 /* An expression like &memfn. */
5372 permerror (input_location, "ISO C++ forbids taking the address of an unqualified"
5373 " or parenthesized non-static member function to form"
5374 " a pointer to member function. Say %<&%T::%D%>",
5375 base, name);
5376 else
5377 permerror (input_location, "ISO C++ forbids taking the address of a bound member"
5378 " function to form a pointer to member function."
5379 " Say %<&%T::%D%>",
5380 base, name);
5382 arg = build_offset_ref (base, fn, /*address_p=*/true, complain);
5385 /* Uninstantiated types are all functions. Taking the
5386 address of a function is a no-op, so just return the
5387 argument. */
5388 if (type_unknown_p (arg))
5389 return build1 (ADDR_EXPR, unknown_type_node, arg);
5391 if (TREE_CODE (arg) == OFFSET_REF)
5392 /* We want a pointer to member; bypass all the code for actually taking
5393 the address of something. */
5394 goto offset_ref;
5396 /* Anything not already handled and not a true memory reference
5397 is an error. */
5398 if (TREE_CODE (argtype) != FUNCTION_TYPE
5399 && TREE_CODE (argtype) != METHOD_TYPE)
5401 cp_lvalue_kind kind = lvalue_kind (arg);
5402 if (kind == clk_none)
5404 if (complain & tf_error)
5405 lvalue_error (input_location, lv_addressof);
5406 return error_mark_node;
5408 if (strict_lvalue && (kind & (clk_rvalueref|clk_class)))
5410 if (!(complain & tf_error))
5411 return error_mark_node;
5412 if (kind & clk_class)
5413 /* Make this a permerror because we used to accept it. */
5414 permerror (input_location, "taking address of temporary");
5415 else
5416 error ("taking address of xvalue (rvalue reference)");
5420 if (TREE_CODE (argtype) == REFERENCE_TYPE)
5422 tree type = build_pointer_type (TREE_TYPE (argtype));
5423 arg = build1 (CONVERT_EXPR, type, arg);
5424 return arg;
5426 else if (pedantic && DECL_MAIN_P (arg))
5428 /* ARM $3.4 */
5429 /* Apparently a lot of autoconf scripts for C++ packages do this,
5430 so only complain if -Wpedantic. */
5431 if (complain & (flag_pedantic_errors ? tf_error : tf_warning))
5432 pedwarn (input_location, OPT_Wpedantic,
5433 "ISO C++ forbids taking address of function %<::main%>");
5434 else if (flag_pedantic_errors)
5435 return error_mark_node;
5438 /* Let &* cancel out to simplify resulting code. */
5439 if (INDIRECT_REF_P (arg))
5441 /* We don't need to have `current_class_ptr' wrapped in a
5442 NON_LVALUE_EXPR node. */
5443 if (arg == current_class_ref)
5444 return current_class_ptr;
5446 arg = TREE_OPERAND (arg, 0);
5447 if (TREE_CODE (TREE_TYPE (arg)) == REFERENCE_TYPE)
5449 tree type = build_pointer_type (TREE_TYPE (TREE_TYPE (arg)));
5450 arg = build1 (CONVERT_EXPR, type, arg);
5452 else
5453 /* Don't let this be an lvalue. */
5454 arg = rvalue (arg);
5455 return arg;
5458 /* ??? Cope with user tricks that amount to offsetof. */
5459 if (TREE_CODE (argtype) != FUNCTION_TYPE
5460 && TREE_CODE (argtype) != METHOD_TYPE
5461 && argtype != unknown_type_node
5462 && (val = get_base_address (arg))
5463 && COMPLETE_TYPE_P (TREE_TYPE (val))
5464 && INDIRECT_REF_P (val)
5465 && TREE_CONSTANT (TREE_OPERAND (val, 0)))
5467 tree type = build_pointer_type (argtype);
5468 return fold_convert (type, fold_offsetof_1 (arg));
5471 /* Handle complex lvalues (when permitted)
5472 by reduction to simpler cases. */
5473 val = unary_complex_lvalue (ADDR_EXPR, arg);
5474 if (val != 0)
5475 return val;
5477 switch (TREE_CODE (arg))
5479 CASE_CONVERT:
5480 case FLOAT_EXPR:
5481 case FIX_TRUNC_EXPR:
5482 /* Even if we're not being pedantic, we cannot allow this
5483 extension when we're instantiating in a SFINAE
5484 context. */
5485 if (! lvalue_p (arg) && complain == tf_none)
5487 if (complain & tf_error)
5488 permerror (input_location, "ISO C++ forbids taking the address of a cast to a non-lvalue expression");
5489 else
5490 return error_mark_node;
5492 break;
5494 case BASELINK:
5495 arg = BASELINK_FUNCTIONS (arg);
5496 /* Fall through. */
5498 case OVERLOAD:
5499 arg = OVL_CURRENT (arg);
5500 break;
5502 case OFFSET_REF:
5503 offset_ref:
5504 /* Turn a reference to a non-static data member into a
5505 pointer-to-member. */
5507 tree type;
5508 tree t;
5510 gcc_assert (PTRMEM_OK_P (arg));
5512 t = TREE_OPERAND (arg, 1);
5513 if (TREE_CODE (TREE_TYPE (t)) == REFERENCE_TYPE)
5515 if (complain & tf_error)
5516 error ("cannot create pointer to reference member %qD", t);
5517 return error_mark_node;
5520 type = build_ptrmem_type (context_for_name_lookup (t),
5521 TREE_TYPE (t));
5522 t = make_ptrmem_cst (type, TREE_OPERAND (arg, 1));
5523 return t;
5526 default:
5527 break;
5530 if (argtype != error_mark_node)
5531 argtype = build_pointer_type (argtype);
5533 /* In a template, we are processing a non-dependent expression
5534 so we can just form an ADDR_EXPR with the correct type. */
5535 if (processing_template_decl || TREE_CODE (arg) != COMPONENT_REF)
5537 val = build_address (arg);
5538 if (TREE_CODE (arg) == OFFSET_REF)
5539 PTRMEM_OK_P (val) = PTRMEM_OK_P (arg);
5541 else if (BASELINK_P (TREE_OPERAND (arg, 1)))
5543 tree fn = BASELINK_FUNCTIONS (TREE_OPERAND (arg, 1));
5545 /* We can only get here with a single static member
5546 function. */
5547 gcc_assert (TREE_CODE (fn) == FUNCTION_DECL
5548 && DECL_STATIC_FUNCTION_P (fn));
5549 mark_used (fn);
5550 val = build_address (fn);
5551 if (TREE_SIDE_EFFECTS (TREE_OPERAND (arg, 0)))
5552 /* Do not lose object's side effects. */
5553 val = build2 (COMPOUND_EXPR, TREE_TYPE (val),
5554 TREE_OPERAND (arg, 0), val);
5556 else if (DECL_C_BIT_FIELD (TREE_OPERAND (arg, 1)))
5558 if (complain & tf_error)
5559 error ("attempt to take address of bit-field structure member %qD",
5560 TREE_OPERAND (arg, 1));
5561 return error_mark_node;
5563 else
5565 tree object = TREE_OPERAND (arg, 0);
5566 tree field = TREE_OPERAND (arg, 1);
5567 gcc_assert (same_type_ignoring_top_level_qualifiers_p
5568 (TREE_TYPE (object), decl_type_context (field)));
5569 val = build_address (arg);
5572 if (TYPE_PTR_P (argtype)
5573 && TREE_CODE (TREE_TYPE (argtype)) == METHOD_TYPE)
5575 build_ptrmemfunc_type (argtype);
5576 val = build_ptrmemfunc (argtype, val, 0,
5577 /*c_cast_p=*/false,
5578 complain);
5581 return val;
5584 /* Take the address of ARG if it has one, even if it's an rvalue. */
5586 tree
5587 cp_build_addr_expr (tree arg, tsubst_flags_t complain)
5589 return cp_build_addr_expr_1 (arg, 0, complain);
5592 /* Take the address of ARG, but only if it's an lvalue. */
5594 static tree
5595 cp_build_addr_expr_strict (tree arg, tsubst_flags_t complain)
5597 return cp_build_addr_expr_1 (arg, 1, complain);
5600 /* C++: Must handle pointers to members.
5602 Perhaps type instantiation should be extended to handle conversion
5603 from aggregates to types we don't yet know we want? (Or are those
5604 cases typically errors which should be reported?)
5606 NOCONVERT nonzero suppresses the default promotions
5607 (such as from short to int). */
5609 tree
5610 cp_build_unary_op (enum tree_code code, tree xarg, int noconvert,
5611 tsubst_flags_t complain)
5613 /* No default_conversion here. It causes trouble for ADDR_EXPR. */
5614 tree arg = xarg;
5615 tree argtype = 0;
5616 const char *errstring = NULL;
5617 tree val;
5618 const char *invalid_op_diag;
5620 if (!arg || error_operand_p (arg))
5621 return error_mark_node;
5623 if ((invalid_op_diag
5624 = targetm.invalid_unary_op ((code == UNARY_PLUS_EXPR
5625 ? CONVERT_EXPR
5626 : code),
5627 TREE_TYPE (xarg))))
5629 if (complain & tf_error)
5630 error (invalid_op_diag);
5631 return error_mark_node;
5634 switch (code)
5636 case UNARY_PLUS_EXPR:
5637 case NEGATE_EXPR:
5639 int flags = WANT_ARITH | WANT_ENUM;
5640 /* Unary plus (but not unary minus) is allowed on pointers. */
5641 if (code == UNARY_PLUS_EXPR)
5642 flags |= WANT_POINTER;
5643 arg = build_expr_type_conversion (flags, arg, true);
5644 if (!arg)
5645 errstring = (code == NEGATE_EXPR
5646 ? _("wrong type argument to unary minus")
5647 : _("wrong type argument to unary plus"));
5648 else
5650 if (!noconvert && CP_INTEGRAL_TYPE_P (TREE_TYPE (arg)))
5651 arg = cp_perform_integral_promotions (arg, complain);
5653 /* Make sure the result is not an lvalue: a unary plus or minus
5654 expression is always a rvalue. */
5655 arg = rvalue (arg);
5658 break;
5660 case BIT_NOT_EXPR:
5661 if (TREE_CODE (TREE_TYPE (arg)) == COMPLEX_TYPE)
5663 code = CONJ_EXPR;
5664 if (!noconvert)
5666 arg = cp_default_conversion (arg, complain);
5667 if (arg == error_mark_node)
5668 return error_mark_node;
5671 else if (!(arg = build_expr_type_conversion (WANT_INT | WANT_ENUM
5672 | WANT_VECTOR_OR_COMPLEX,
5673 arg, true)))
5674 errstring = _("wrong type argument to bit-complement");
5675 else if (!noconvert && CP_INTEGRAL_TYPE_P (TREE_TYPE (arg)))
5676 arg = cp_perform_integral_promotions (arg, complain);
5677 break;
5679 case ABS_EXPR:
5680 if (!(arg = build_expr_type_conversion (WANT_ARITH | WANT_ENUM, arg, true)))
5681 errstring = _("wrong type argument to abs");
5682 else if (!noconvert)
5684 arg = cp_default_conversion (arg, complain);
5685 if (arg == error_mark_node)
5686 return error_mark_node;
5688 break;
5690 case CONJ_EXPR:
5691 /* Conjugating a real value is a no-op, but allow it anyway. */
5692 if (!(arg = build_expr_type_conversion (WANT_ARITH | WANT_ENUM, arg, true)))
5693 errstring = _("wrong type argument to conjugation");
5694 else if (!noconvert)
5696 arg = cp_default_conversion (arg, complain);
5697 if (arg == error_mark_node)
5698 return error_mark_node;
5700 break;
5702 case TRUTH_NOT_EXPR:
5703 if (VECTOR_TYPE_P (TREE_TYPE (arg)))
5704 return cp_build_binary_op (input_location, EQ_EXPR, arg,
5705 build_zero_cst (TREE_TYPE (arg)), complain);
5706 arg = perform_implicit_conversion (boolean_type_node, arg,
5707 complain);
5708 val = invert_truthvalue_loc (input_location, arg);
5709 if (arg != error_mark_node)
5710 return val;
5711 errstring = _("in argument to unary !");
5712 break;
5714 case NOP_EXPR:
5715 break;
5717 case REALPART_EXPR:
5718 case IMAGPART_EXPR:
5719 arg = build_real_imag_expr (input_location, code, arg);
5720 if (arg == error_mark_node)
5721 return arg;
5722 else
5723 return fold_if_not_in_template (arg);
5725 case PREINCREMENT_EXPR:
5726 case POSTINCREMENT_EXPR:
5727 case PREDECREMENT_EXPR:
5728 case POSTDECREMENT_EXPR:
5729 /* Handle complex lvalues (when permitted)
5730 by reduction to simpler cases. */
5732 val = unary_complex_lvalue (code, arg);
5733 if (val != 0)
5734 return val;
5736 arg = mark_lvalue_use (arg);
5738 /* Increment or decrement the real part of the value,
5739 and don't change the imaginary part. */
5740 if (TREE_CODE (TREE_TYPE (arg)) == COMPLEX_TYPE)
5742 tree real, imag;
5744 arg = stabilize_reference (arg);
5745 real = cp_build_unary_op (REALPART_EXPR, arg, 1, complain);
5746 imag = cp_build_unary_op (IMAGPART_EXPR, arg, 1, complain);
5747 real = cp_build_unary_op (code, real, 1, complain);
5748 if (real == error_mark_node || imag == error_mark_node)
5749 return error_mark_node;
5750 return build2 (COMPLEX_EXPR, TREE_TYPE (arg),
5751 real, imag);
5754 /* Report invalid types. */
5756 if (!(arg = build_expr_type_conversion (WANT_ARITH | WANT_POINTER,
5757 arg, true)))
5759 if (code == PREINCREMENT_EXPR)
5760 errstring = _("no pre-increment operator for type");
5761 else if (code == POSTINCREMENT_EXPR)
5762 errstring = _("no post-increment operator for type");
5763 else if (code == PREDECREMENT_EXPR)
5764 errstring = _("no pre-decrement operator for type");
5765 else
5766 errstring = _("no post-decrement operator for type");
5767 break;
5769 else if (arg == error_mark_node)
5770 return error_mark_node;
5772 /* Report something read-only. */
5774 if (CP_TYPE_CONST_P (TREE_TYPE (arg))
5775 || TREE_READONLY (arg))
5777 if (complain & tf_error)
5778 cxx_readonly_error (arg, ((code == PREINCREMENT_EXPR
5779 || code == POSTINCREMENT_EXPR)
5780 ? lv_increment : lv_decrement));
5781 else
5782 return error_mark_node;
5786 tree inc;
5787 tree declared_type = unlowered_expr_type (arg);
5789 argtype = TREE_TYPE (arg);
5791 /* ARM $5.2.5 last annotation says this should be forbidden. */
5792 if (TREE_CODE (argtype) == ENUMERAL_TYPE)
5794 if (complain & tf_error)
5795 permerror (input_location, (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
5796 ? G_("ISO C++ forbids incrementing an enum")
5797 : G_("ISO C++ forbids decrementing an enum"));
5798 else
5799 return error_mark_node;
5802 /* Compute the increment. */
5804 if (TYPE_PTR_P (argtype))
5806 tree type = complete_type (TREE_TYPE (argtype));
5808 if (!COMPLETE_OR_VOID_TYPE_P (type))
5810 if (complain & tf_error)
5811 error (((code == PREINCREMENT_EXPR
5812 || code == POSTINCREMENT_EXPR))
5813 ? G_("cannot increment a pointer to incomplete type %qT")
5814 : G_("cannot decrement a pointer to incomplete type %qT"),
5815 TREE_TYPE (argtype));
5816 else
5817 return error_mark_node;
5819 else if (!TYPE_PTROB_P (argtype))
5821 if (complain & tf_error)
5822 pedwarn (input_location, OPT_Wpointer_arith,
5823 (code == PREINCREMENT_EXPR
5824 || code == POSTINCREMENT_EXPR)
5825 ? G_("ISO C++ forbids incrementing a pointer of type %qT")
5826 : G_("ISO C++ forbids decrementing a pointer of type %qT"),
5827 argtype);
5828 else
5829 return error_mark_node;
5832 inc = cxx_sizeof_nowarn (TREE_TYPE (argtype));
5834 else
5835 inc = VECTOR_TYPE_P (argtype)
5836 ? build_one_cst (argtype)
5837 : integer_one_node;
5839 inc = cp_convert (argtype, inc, complain);
5841 /* If 'arg' is an Objective-C PROPERTY_REF expression, then we
5842 need to ask Objective-C to build the increment or decrement
5843 expression for it. */
5844 if (objc_is_property_ref (arg))
5845 return objc_build_incr_expr_for_property_ref (input_location, code,
5846 arg, inc);
5848 /* Complain about anything else that is not a true lvalue. */
5849 if (!lvalue_or_else (arg, ((code == PREINCREMENT_EXPR
5850 || code == POSTINCREMENT_EXPR)
5851 ? lv_increment : lv_decrement),
5852 complain))
5853 return error_mark_node;
5855 /* Forbid using -- on `bool'. */
5856 if (TREE_CODE (declared_type) == BOOLEAN_TYPE)
5858 if (code == POSTDECREMENT_EXPR || code == PREDECREMENT_EXPR)
5860 if (complain & tf_error)
5861 error ("invalid use of Boolean expression as operand "
5862 "to %<operator--%>");
5863 return error_mark_node;
5865 val = boolean_increment (code, arg);
5867 else if (code == POSTINCREMENT_EXPR || code == POSTDECREMENT_EXPR)
5868 /* An rvalue has no cv-qualifiers. */
5869 val = build2 (code, cv_unqualified (TREE_TYPE (arg)), arg, inc);
5870 else
5871 val = build2 (code, TREE_TYPE (arg), arg, inc);
5873 TREE_SIDE_EFFECTS (val) = 1;
5874 return val;
5877 case ADDR_EXPR:
5878 /* Note that this operation never does default_conversion
5879 regardless of NOCONVERT. */
5880 return cp_build_addr_expr (arg, complain);
5882 default:
5883 break;
5886 if (!errstring)
5888 if (argtype == 0)
5889 argtype = TREE_TYPE (arg);
5890 return fold_if_not_in_template (build1 (code, argtype, arg));
5893 if (complain & tf_error)
5894 error ("%s", errstring);
5895 return error_mark_node;
5898 /* Hook for the c-common bits that build a unary op. */
5899 tree
5900 build_unary_op (location_t /*location*/,
5901 enum tree_code code, tree xarg, int noconvert)
5903 return cp_build_unary_op (code, xarg, noconvert, tf_warning_or_error);
5906 /* Apply unary lvalue-demanding operator CODE to the expression ARG
5907 for certain kinds of expressions which are not really lvalues
5908 but which we can accept as lvalues.
5910 If ARG is not a kind of expression we can handle, return
5911 NULL_TREE. */
5913 tree
5914 unary_complex_lvalue (enum tree_code code, tree arg)
5916 /* Inside a template, making these kinds of adjustments is
5917 pointless; we are only concerned with the type of the
5918 expression. */
5919 if (processing_template_decl)
5920 return NULL_TREE;
5922 /* Handle (a, b) used as an "lvalue". */
5923 if (TREE_CODE (arg) == COMPOUND_EXPR)
5925 tree real_result = cp_build_unary_op (code, TREE_OPERAND (arg, 1), 0,
5926 tf_warning_or_error);
5927 return build2 (COMPOUND_EXPR, TREE_TYPE (real_result),
5928 TREE_OPERAND (arg, 0), real_result);
5931 /* Handle (a ? b : c) used as an "lvalue". */
5932 if (TREE_CODE (arg) == COND_EXPR
5933 || TREE_CODE (arg) == MIN_EXPR || TREE_CODE (arg) == MAX_EXPR)
5934 return rationalize_conditional_expr (code, arg, tf_warning_or_error);
5936 /* Handle (a = b), (++a), and (--a) used as an "lvalue". */
5937 if (TREE_CODE (arg) == MODIFY_EXPR
5938 || TREE_CODE (arg) == PREINCREMENT_EXPR
5939 || TREE_CODE (arg) == PREDECREMENT_EXPR)
5941 tree lvalue = TREE_OPERAND (arg, 0);
5942 if (TREE_SIDE_EFFECTS (lvalue))
5944 lvalue = stabilize_reference (lvalue);
5945 arg = build2 (TREE_CODE (arg), TREE_TYPE (arg),
5946 lvalue, TREE_OPERAND (arg, 1));
5948 return unary_complex_lvalue
5949 (code, build2 (COMPOUND_EXPR, TREE_TYPE (lvalue), arg, lvalue));
5952 if (code != ADDR_EXPR)
5953 return NULL_TREE;
5955 /* Handle (a = b) used as an "lvalue" for `&'. */
5956 if (TREE_CODE (arg) == MODIFY_EXPR
5957 || TREE_CODE (arg) == INIT_EXPR)
5959 tree real_result = cp_build_unary_op (code, TREE_OPERAND (arg, 0), 0,
5960 tf_warning_or_error);
5961 arg = build2 (COMPOUND_EXPR, TREE_TYPE (real_result),
5962 arg, real_result);
5963 TREE_NO_WARNING (arg) = 1;
5964 return arg;
5967 if (TREE_CODE (TREE_TYPE (arg)) == FUNCTION_TYPE
5968 || TREE_CODE (TREE_TYPE (arg)) == METHOD_TYPE
5969 || TREE_CODE (arg) == OFFSET_REF)
5970 return NULL_TREE;
5972 /* We permit compiler to make function calls returning
5973 objects of aggregate type look like lvalues. */
5975 tree targ = arg;
5977 if (TREE_CODE (targ) == SAVE_EXPR)
5978 targ = TREE_OPERAND (targ, 0);
5980 if (TREE_CODE (targ) == CALL_EXPR && MAYBE_CLASS_TYPE_P (TREE_TYPE (targ)))
5982 if (TREE_CODE (arg) == SAVE_EXPR)
5983 targ = arg;
5984 else
5985 targ = build_cplus_new (TREE_TYPE (arg), arg, tf_warning_or_error);
5986 return build1 (ADDR_EXPR, build_pointer_type (TREE_TYPE (arg)), targ);
5989 if (TREE_CODE (arg) == SAVE_EXPR && INDIRECT_REF_P (targ))
5990 return build3 (SAVE_EXPR, build_pointer_type (TREE_TYPE (arg)),
5991 TREE_OPERAND (targ, 0), current_function_decl, NULL);
5994 /* Don't let anything else be handled specially. */
5995 return NULL_TREE;
5998 /* Mark EXP saying that we need to be able to take the
5999 address of it; it should not be allocated in a register.
6000 Value is true if successful.
6002 C++: we do not allow `current_class_ptr' to be addressable. */
6004 bool
6005 cxx_mark_addressable (tree exp)
6007 tree x = exp;
6009 while (1)
6010 switch (TREE_CODE (x))
6012 case ADDR_EXPR:
6013 case COMPONENT_REF:
6014 case ARRAY_REF:
6015 case REALPART_EXPR:
6016 case IMAGPART_EXPR:
6017 x = TREE_OPERAND (x, 0);
6018 break;
6020 case PARM_DECL:
6021 if (x == current_class_ptr)
6023 error ("cannot take the address of %<this%>, which is an rvalue expression");
6024 TREE_ADDRESSABLE (x) = 1; /* so compiler doesn't die later. */
6025 return true;
6027 /* Fall through. */
6029 case VAR_DECL:
6030 /* Caller should not be trying to mark initialized
6031 constant fields addressable. */
6032 gcc_assert (DECL_LANG_SPECIFIC (x) == 0
6033 || DECL_IN_AGGR_P (x) == 0
6034 || TREE_STATIC (x)
6035 || DECL_EXTERNAL (x));
6036 /* Fall through. */
6038 case RESULT_DECL:
6039 if (DECL_REGISTER (x) && !TREE_ADDRESSABLE (x)
6040 && !DECL_ARTIFICIAL (x))
6042 if (VAR_P (x) && DECL_HARD_REGISTER (x))
6044 error
6045 ("address of explicit register variable %qD requested", x);
6046 return false;
6048 else if (extra_warnings)
6049 warning
6050 (OPT_Wextra, "address requested for %qD, which is declared %<register%>", x);
6052 TREE_ADDRESSABLE (x) = 1;
6053 return true;
6055 case CONST_DECL:
6056 case FUNCTION_DECL:
6057 TREE_ADDRESSABLE (x) = 1;
6058 return true;
6060 case CONSTRUCTOR:
6061 TREE_ADDRESSABLE (x) = 1;
6062 return true;
6064 case TARGET_EXPR:
6065 TREE_ADDRESSABLE (x) = 1;
6066 cxx_mark_addressable (TREE_OPERAND (x, 0));
6067 return true;
6069 default:
6070 return true;
6074 /* Build and return a conditional expression IFEXP ? OP1 : OP2. */
6076 tree
6077 build_x_conditional_expr (location_t loc, tree ifexp, tree op1, tree op2,
6078 tsubst_flags_t complain)
6080 tree orig_ifexp = ifexp;
6081 tree orig_op1 = op1;
6082 tree orig_op2 = op2;
6083 tree expr;
6085 if (processing_template_decl)
6087 /* The standard says that the expression is type-dependent if
6088 IFEXP is type-dependent, even though the eventual type of the
6089 expression doesn't dependent on IFEXP. */
6090 if (type_dependent_expression_p (ifexp)
6091 /* As a GNU extension, the middle operand may be omitted. */
6092 || (op1 && type_dependent_expression_p (op1))
6093 || type_dependent_expression_p (op2))
6094 return build_min_nt_loc (loc, COND_EXPR, ifexp, op1, op2);
6095 ifexp = build_non_dependent_expr (ifexp);
6096 if (op1)
6097 op1 = build_non_dependent_expr (op1);
6098 op2 = build_non_dependent_expr (op2);
6101 expr = build_conditional_expr (loc, ifexp, op1, op2, complain);
6102 if (processing_template_decl && expr != error_mark_node
6103 && TREE_CODE (expr) != VEC_COND_EXPR)
6105 tree min = build_min_non_dep (COND_EXPR, expr,
6106 orig_ifexp, orig_op1, orig_op2);
6107 /* In C++11, remember that the result is an lvalue or xvalue.
6108 In C++98, lvalue_kind can just assume lvalue in a template. */
6109 if (cxx_dialect >= cxx11
6110 && lvalue_or_rvalue_with_address_p (expr)
6111 && !lvalue_or_rvalue_with_address_p (min))
6112 TREE_TYPE (min) = cp_build_reference_type (TREE_TYPE (min),
6113 !real_lvalue_p (expr));
6114 expr = convert_from_reference (min);
6116 return expr;
6119 /* Given a list of expressions, return a compound expression
6120 that performs them all and returns the value of the last of them. */
6122 tree
6123 build_x_compound_expr_from_list (tree list, expr_list_kind exp,
6124 tsubst_flags_t complain)
6126 tree expr = TREE_VALUE (list);
6128 if (BRACE_ENCLOSED_INITIALIZER_P (expr)
6129 && !CONSTRUCTOR_IS_DIRECT_INIT (expr))
6131 if (complain & tf_error)
6132 pedwarn (EXPR_LOC_OR_LOC (expr, input_location), 0,
6133 "list-initializer for non-class type must not "
6134 "be parenthesized");
6135 else
6136 return error_mark_node;
6139 if (TREE_CHAIN (list))
6141 if (complain & tf_error)
6142 switch (exp)
6144 case ELK_INIT:
6145 permerror (input_location, "expression list treated as compound "
6146 "expression in initializer");
6147 break;
6148 case ELK_MEM_INIT:
6149 permerror (input_location, "expression list treated as compound "
6150 "expression in mem-initializer");
6151 break;
6152 case ELK_FUNC_CAST:
6153 permerror (input_location, "expression list treated as compound "
6154 "expression in functional cast");
6155 break;
6156 default:
6157 gcc_unreachable ();
6159 else
6160 return error_mark_node;
6162 for (list = TREE_CHAIN (list); list; list = TREE_CHAIN (list))
6163 expr = build_x_compound_expr (EXPR_LOCATION (TREE_VALUE (list)),
6164 expr, TREE_VALUE (list), complain);
6167 return expr;
6170 /* Like build_x_compound_expr_from_list, but using a VEC. */
6172 tree
6173 build_x_compound_expr_from_vec (vec<tree, va_gc> *vec, const char *msg,
6174 tsubst_flags_t complain)
6176 if (vec_safe_is_empty (vec))
6177 return NULL_TREE;
6178 else if (vec->length () == 1)
6179 return (*vec)[0];
6180 else
6182 tree expr;
6183 unsigned int ix;
6184 tree t;
6186 if (msg != NULL)
6188 if (complain & tf_error)
6189 permerror (input_location,
6190 "%s expression list treated as compound expression",
6191 msg);
6192 else
6193 return error_mark_node;
6196 expr = (*vec)[0];
6197 for (ix = 1; vec->iterate (ix, &t); ++ix)
6198 expr = build_x_compound_expr (EXPR_LOCATION (t), expr,
6199 t, complain);
6201 return expr;
6205 /* Handle overloading of the ',' operator when needed. */
6207 tree
6208 build_x_compound_expr (location_t loc, tree op1, tree op2,
6209 tsubst_flags_t complain)
6211 tree result;
6212 tree orig_op1 = op1;
6213 tree orig_op2 = op2;
6215 if (processing_template_decl)
6217 if (type_dependent_expression_p (op1)
6218 || type_dependent_expression_p (op2))
6219 return build_min_nt_loc (loc, COMPOUND_EXPR, op1, op2);
6220 op1 = build_non_dependent_expr (op1);
6221 op2 = build_non_dependent_expr (op2);
6224 result = build_new_op (loc, COMPOUND_EXPR, LOOKUP_NORMAL, op1, op2,
6225 NULL_TREE, /*overload=*/NULL, complain);
6226 if (!result)
6227 result = cp_build_compound_expr (op1, op2, complain);
6229 if (processing_template_decl && result != error_mark_node)
6230 return build_min_non_dep (COMPOUND_EXPR, result, orig_op1, orig_op2);
6232 return result;
6235 /* Like cp_build_compound_expr, but for the c-common bits. */
6237 tree
6238 build_compound_expr (location_t /*loc*/, tree lhs, tree rhs)
6240 return cp_build_compound_expr (lhs, rhs, tf_warning_or_error);
6243 /* Build a compound expression. */
6245 tree
6246 cp_build_compound_expr (tree lhs, tree rhs, tsubst_flags_t complain)
6248 lhs = convert_to_void (lhs, ICV_LEFT_OF_COMMA, complain);
6250 if (lhs == error_mark_node || rhs == error_mark_node)
6251 return error_mark_node;
6253 if (flag_cilkplus
6254 && (TREE_CODE (lhs) == CILK_SPAWN_STMT
6255 || TREE_CODE (rhs) == CILK_SPAWN_STMT))
6257 location_t loc = (EXPR_HAS_LOCATION (lhs) ? EXPR_LOCATION (lhs)
6258 : EXPR_LOCATION (rhs));
6259 error_at (loc,
6260 "spawned function call cannot be part of a comma expression");
6261 return error_mark_node;
6264 if (TREE_CODE (rhs) == TARGET_EXPR)
6266 /* If the rhs is a TARGET_EXPR, then build the compound
6267 expression inside the target_expr's initializer. This
6268 helps the compiler to eliminate unnecessary temporaries. */
6269 tree init = TREE_OPERAND (rhs, 1);
6271 init = build2 (COMPOUND_EXPR, TREE_TYPE (init), lhs, init);
6272 TREE_OPERAND (rhs, 1) = init;
6274 return rhs;
6277 if (type_unknown_p (rhs))
6279 if (complain & tf_error)
6280 error ("no context to resolve type of %qE", rhs);
6281 return error_mark_node;
6284 return build2 (COMPOUND_EXPR, TREE_TYPE (rhs), lhs, rhs);
6287 /* Issue a diagnostic message if casting from SRC_TYPE to DEST_TYPE
6288 casts away constness. CAST gives the type of cast. Returns true
6289 if the cast is ill-formed, false if it is well-formed.
6291 ??? This function warns for casting away any qualifier not just
6292 const. We would like to specify exactly what qualifiers are casted
6293 away.
6296 static bool
6297 check_for_casting_away_constness (tree src_type, tree dest_type,
6298 enum tree_code cast, tsubst_flags_t complain)
6300 /* C-style casts are allowed to cast away constness. With
6301 WARN_CAST_QUAL, we still want to issue a warning. */
6302 if (cast == CAST_EXPR && !warn_cast_qual)
6303 return false;
6305 if (!casts_away_constness (src_type, dest_type, complain))
6306 return false;
6308 switch (cast)
6310 case CAST_EXPR:
6311 if (complain & tf_warning)
6312 warning (OPT_Wcast_qual,
6313 "cast from type %qT to type %qT casts away qualifiers",
6314 src_type, dest_type);
6315 return false;
6317 case STATIC_CAST_EXPR:
6318 if (complain & tf_error)
6319 error ("static_cast from type %qT to type %qT casts away qualifiers",
6320 src_type, dest_type);
6321 return true;
6323 case REINTERPRET_CAST_EXPR:
6324 if (complain & tf_error)
6325 error ("reinterpret_cast from type %qT to type %qT casts away qualifiers",
6326 src_type, dest_type);
6327 return true;
6329 default:
6330 gcc_unreachable();
6335 Warns if the cast from expression EXPR to type TYPE is useless.
6337 void
6338 maybe_warn_about_useless_cast (tree type, tree expr, tsubst_flags_t complain)
6340 if (warn_useless_cast
6341 && complain & tf_warning)
6343 if ((TREE_CODE (type) == REFERENCE_TYPE
6344 && (TYPE_REF_IS_RVALUE (type)
6345 ? xvalue_p (expr) : real_lvalue_p (expr))
6346 && same_type_p (TREE_TYPE (expr), TREE_TYPE (type)))
6347 || same_type_p (TREE_TYPE (expr), type))
6348 warning (OPT_Wuseless_cast, "useless cast to type %qT", type);
6352 /* Convert EXPR (an expression with pointer-to-member type) to TYPE
6353 (another pointer-to-member type in the same hierarchy) and return
6354 the converted expression. If ALLOW_INVERSE_P is permitted, a
6355 pointer-to-derived may be converted to pointer-to-base; otherwise,
6356 only the other direction is permitted. If C_CAST_P is true, this
6357 conversion is taking place as part of a C-style cast. */
6359 tree
6360 convert_ptrmem (tree type, tree expr, bool allow_inverse_p,
6361 bool c_cast_p, tsubst_flags_t complain)
6363 if (TYPE_PTRDATAMEM_P (type))
6365 tree delta;
6367 if (TREE_CODE (expr) == PTRMEM_CST)
6368 expr = cplus_expand_constant (expr);
6369 delta = get_delta_difference (TYPE_PTRMEM_CLASS_TYPE (TREE_TYPE (expr)),
6370 TYPE_PTRMEM_CLASS_TYPE (type),
6371 allow_inverse_p,
6372 c_cast_p, complain);
6373 if (delta == error_mark_node)
6374 return error_mark_node;
6376 if (!integer_zerop (delta))
6378 tree cond, op1, op2;
6380 cond = cp_build_binary_op (input_location,
6381 EQ_EXPR,
6382 expr,
6383 build_int_cst (TREE_TYPE (expr), -1),
6384 complain);
6385 op1 = build_nop (ptrdiff_type_node, expr);
6386 op2 = cp_build_binary_op (input_location,
6387 PLUS_EXPR, op1, delta,
6388 complain);
6390 expr = fold_build3_loc (input_location,
6391 COND_EXPR, ptrdiff_type_node, cond, op1, op2);
6395 return build_nop (type, expr);
6397 else
6398 return build_ptrmemfunc (TYPE_PTRMEMFUNC_FN_TYPE (type), expr,
6399 allow_inverse_p, c_cast_p, complain);
6402 /* Perform a static_cast from EXPR to TYPE. When C_CAST_P is true,
6403 this static_cast is being attempted as one of the possible casts
6404 allowed by a C-style cast. (In that case, accessibility of base
6405 classes is not considered, and it is OK to cast away
6406 constness.) Return the result of the cast. *VALID_P is set to
6407 indicate whether or not the cast was valid. */
6409 static tree
6410 build_static_cast_1 (tree type, tree expr, bool c_cast_p,
6411 bool *valid_p, tsubst_flags_t complain)
6413 tree intype;
6414 tree result;
6415 cp_lvalue_kind clk;
6417 /* Assume the cast is valid. */
6418 *valid_p = true;
6420 intype = unlowered_expr_type (expr);
6422 /* Save casted types in the function's used types hash table. */
6423 used_types_insert (type);
6425 /* [expr.static.cast]
6427 An lvalue of type "cv1 B", where B is a class type, can be cast
6428 to type "reference to cv2 D", where D is a class derived (clause
6429 _class.derived_) from B, if a valid standard conversion from
6430 "pointer to D" to "pointer to B" exists (_conv.ptr_), cv2 is the
6431 same cv-qualification as, or greater cv-qualification than, cv1,
6432 and B is not a virtual base class of D. */
6433 /* We check this case before checking the validity of "TYPE t =
6434 EXPR;" below because for this case:
6436 struct B {};
6437 struct D : public B { D(const B&); };
6438 extern B& b;
6439 void f() { static_cast<const D&>(b); }
6441 we want to avoid constructing a new D. The standard is not
6442 completely clear about this issue, but our interpretation is
6443 consistent with other compilers. */
6444 if (TREE_CODE (type) == REFERENCE_TYPE
6445 && CLASS_TYPE_P (TREE_TYPE (type))
6446 && CLASS_TYPE_P (intype)
6447 && (TYPE_REF_IS_RVALUE (type) || real_lvalue_p (expr))
6448 && DERIVED_FROM_P (intype, TREE_TYPE (type))
6449 && can_convert (build_pointer_type (TYPE_MAIN_VARIANT (intype)),
6450 build_pointer_type (TYPE_MAIN_VARIANT
6451 (TREE_TYPE (type))),
6452 complain)
6453 && (c_cast_p
6454 || at_least_as_qualified_p (TREE_TYPE (type), intype)))
6456 tree base;
6458 /* There is a standard conversion from "D*" to "B*" even if "B"
6459 is ambiguous or inaccessible. If this is really a
6460 static_cast, then we check both for inaccessibility and
6461 ambiguity. However, if this is a static_cast being performed
6462 because the user wrote a C-style cast, then accessibility is
6463 not considered. */
6464 base = lookup_base (TREE_TYPE (type), intype,
6465 c_cast_p ? ba_unique : ba_check,
6466 NULL, complain);
6468 /* Convert from "B*" to "D*". This function will check that "B"
6469 is not a virtual base of "D". */
6470 expr = build_base_path (MINUS_EXPR, build_address (expr),
6471 base, /*nonnull=*/false, complain);
6472 /* Convert the pointer to a reference -- but then remember that
6473 there are no expressions with reference type in C++.
6475 We call rvalue so that there's an actual tree code
6476 (NON_LVALUE_EXPR) for the static_cast; otherwise, if the operand
6477 is a variable with the same type, the conversion would get folded
6478 away, leaving just the variable and causing lvalue_kind to give
6479 the wrong answer. */
6480 return convert_from_reference (rvalue (cp_fold_convert (type, expr)));
6483 /* "A glvalue of type cv1 T1 can be cast to type rvalue reference to
6484 cv2 T2 if cv2 T2 is reference-compatible with cv1 T1 (8.5.3)." */
6485 if (TREE_CODE (type) == REFERENCE_TYPE
6486 && TYPE_REF_IS_RVALUE (type)
6487 && (clk = real_lvalue_p (expr))
6488 && reference_related_p (TREE_TYPE (type), intype)
6489 && (c_cast_p || at_least_as_qualified_p (TREE_TYPE (type), intype)))
6491 if (clk == clk_ordinary)
6493 /* Handle the (non-bit-field) lvalue case here by casting to
6494 lvalue reference and then changing it to an rvalue reference.
6495 Casting an xvalue to rvalue reference will be handled by the
6496 main code path. */
6497 tree lref = cp_build_reference_type (TREE_TYPE (type), false);
6498 result = (perform_direct_initialization_if_possible
6499 (lref, expr, c_cast_p, complain));
6500 result = cp_fold_convert (type, result);
6501 /* Make sure we don't fold back down to a named rvalue reference,
6502 because that would be an lvalue. */
6503 if (DECL_P (result))
6504 result = build1 (NON_LVALUE_EXPR, type, result);
6505 return convert_from_reference (result);
6507 else
6508 /* For a bit-field or packed field, bind to a temporary. */
6509 expr = rvalue (expr);
6512 /* Resolve overloaded address here rather than once in
6513 implicit_conversion and again in the inverse code below. */
6514 if (TYPE_PTRMEMFUNC_P (type) && type_unknown_p (expr))
6516 expr = instantiate_type (type, expr, complain);
6517 intype = TREE_TYPE (expr);
6520 /* [expr.static.cast]
6522 Any expression can be explicitly converted to type cv void. */
6523 if (VOID_TYPE_P (type))
6524 return convert_to_void (expr, ICV_CAST, complain);
6526 /* [class.abstract]
6527 An abstract class shall not be used ... as the type of an explicit
6528 conversion. */
6529 if (abstract_virtuals_error_sfinae (ACU_CAST, type, complain))
6530 return error_mark_node;
6532 /* [expr.static.cast]
6534 An expression e can be explicitly converted to a type T using a
6535 static_cast of the form static_cast<T>(e) if the declaration T
6536 t(e);" is well-formed, for some invented temporary variable
6537 t. */
6538 result = perform_direct_initialization_if_possible (type, expr,
6539 c_cast_p, complain);
6540 if (result)
6542 result = convert_from_reference (result);
6544 /* [expr.static.cast]
6546 If T is a reference type, the result is an lvalue; otherwise,
6547 the result is an rvalue. */
6548 if (TREE_CODE (type) != REFERENCE_TYPE)
6549 result = rvalue (result);
6550 return result;
6553 /* [expr.static.cast]
6555 The inverse of any standard conversion sequence (clause _conv_),
6556 other than the lvalue-to-rvalue (_conv.lval_), array-to-pointer
6557 (_conv.array_), function-to-pointer (_conv.func_), and boolean
6558 (_conv.bool_) conversions, can be performed explicitly using
6559 static_cast subject to the restriction that the explicit
6560 conversion does not cast away constness (_expr.const.cast_), and
6561 the following additional rules for specific cases: */
6562 /* For reference, the conversions not excluded are: integral
6563 promotions, floating point promotion, integral conversions,
6564 floating point conversions, floating-integral conversions,
6565 pointer conversions, and pointer to member conversions. */
6566 /* DR 128
6568 A value of integral _or enumeration_ type can be explicitly
6569 converted to an enumeration type. */
6570 /* The effect of all that is that any conversion between any two
6571 types which are integral, floating, or enumeration types can be
6572 performed. */
6573 if ((INTEGRAL_OR_ENUMERATION_TYPE_P (type)
6574 || SCALAR_FLOAT_TYPE_P (type))
6575 && (INTEGRAL_OR_ENUMERATION_TYPE_P (intype)
6576 || SCALAR_FLOAT_TYPE_P (intype)))
6577 return ocp_convert (type, expr, CONV_C_CAST, LOOKUP_NORMAL, complain);
6579 if (TYPE_PTR_P (type) && TYPE_PTR_P (intype)
6580 && CLASS_TYPE_P (TREE_TYPE (type))
6581 && CLASS_TYPE_P (TREE_TYPE (intype))
6582 && can_convert (build_pointer_type (TYPE_MAIN_VARIANT
6583 (TREE_TYPE (intype))),
6584 build_pointer_type (TYPE_MAIN_VARIANT
6585 (TREE_TYPE (type))),
6586 complain))
6588 tree base;
6590 if (!c_cast_p
6591 && check_for_casting_away_constness (intype, type, STATIC_CAST_EXPR,
6592 complain))
6593 return error_mark_node;
6594 base = lookup_base (TREE_TYPE (type), TREE_TYPE (intype),
6595 c_cast_p ? ba_unique : ba_check,
6596 NULL, complain);
6597 expr = build_base_path (MINUS_EXPR, expr, base, /*nonnull=*/false,
6598 complain);
6599 return cp_fold_convert(type, expr);
6602 if ((TYPE_PTRDATAMEM_P (type) && TYPE_PTRDATAMEM_P (intype))
6603 || (TYPE_PTRMEMFUNC_P (type) && TYPE_PTRMEMFUNC_P (intype)))
6605 tree c1;
6606 tree c2;
6607 tree t1;
6608 tree t2;
6610 c1 = TYPE_PTRMEM_CLASS_TYPE (intype);
6611 c2 = TYPE_PTRMEM_CLASS_TYPE (type);
6613 if (TYPE_PTRDATAMEM_P (type))
6615 t1 = (build_ptrmem_type
6616 (c1,
6617 TYPE_MAIN_VARIANT (TYPE_PTRMEM_POINTED_TO_TYPE (intype))));
6618 t2 = (build_ptrmem_type
6619 (c2,
6620 TYPE_MAIN_VARIANT (TYPE_PTRMEM_POINTED_TO_TYPE (type))));
6622 else
6624 t1 = intype;
6625 t2 = type;
6627 if (can_convert (t1, t2, complain) || can_convert (t2, t1, complain))
6629 if (!c_cast_p
6630 && check_for_casting_away_constness (intype, type,
6631 STATIC_CAST_EXPR,
6632 complain))
6633 return error_mark_node;
6634 return convert_ptrmem (type, expr, /*allow_inverse_p=*/1,
6635 c_cast_p, complain);
6639 /* [expr.static.cast]
6641 An rvalue of type "pointer to cv void" can be explicitly
6642 converted to a pointer to object type. A value of type pointer
6643 to object converted to "pointer to cv void" and back to the
6644 original pointer type will have its original value. */
6645 if (TYPE_PTR_P (intype)
6646 && VOID_TYPE_P (TREE_TYPE (intype))
6647 && TYPE_PTROB_P (type))
6649 if (!c_cast_p
6650 && check_for_casting_away_constness (intype, type, STATIC_CAST_EXPR,
6651 complain))
6652 return error_mark_node;
6653 return build_nop (type, expr);
6656 *valid_p = false;
6657 return error_mark_node;
6660 /* Return an expression representing static_cast<TYPE>(EXPR). */
6662 tree
6663 build_static_cast (tree type, tree expr, tsubst_flags_t complain)
6665 tree result;
6666 bool valid_p;
6668 if (type == error_mark_node || expr == error_mark_node)
6669 return error_mark_node;
6671 if (processing_template_decl)
6673 expr = build_min (STATIC_CAST_EXPR, type, expr);
6674 /* We don't know if it will or will not have side effects. */
6675 TREE_SIDE_EFFECTS (expr) = 1;
6676 return convert_from_reference (expr);
6679 /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
6680 Strip such NOP_EXPRs if VALUE is being used in non-lvalue context. */
6681 if (TREE_CODE (type) != REFERENCE_TYPE
6682 && TREE_CODE (expr) == NOP_EXPR
6683 && TREE_TYPE (expr) == TREE_TYPE (TREE_OPERAND (expr, 0)))
6684 expr = TREE_OPERAND (expr, 0);
6686 result = build_static_cast_1 (type, expr, /*c_cast_p=*/false, &valid_p,
6687 complain);
6688 if (valid_p)
6690 if (result != error_mark_node)
6691 maybe_warn_about_useless_cast (type, expr, complain);
6692 return result;
6695 if (complain & tf_error)
6696 error ("invalid static_cast from type %qT to type %qT",
6697 TREE_TYPE (expr), type);
6698 return error_mark_node;
6701 /* EXPR is an expression with member function or pointer-to-member
6702 function type. TYPE is a pointer type. Converting EXPR to TYPE is
6703 not permitted by ISO C++, but we accept it in some modes. If we
6704 are not in one of those modes, issue a diagnostic. Return the
6705 converted expression. */
6707 tree
6708 convert_member_func_to_ptr (tree type, tree expr, tsubst_flags_t complain)
6710 tree intype;
6711 tree decl;
6713 intype = TREE_TYPE (expr);
6714 gcc_assert (TYPE_PTRMEMFUNC_P (intype)
6715 || TREE_CODE (intype) == METHOD_TYPE);
6717 if (!(complain & tf_warning_or_error))
6718 return error_mark_node;
6720 if (pedantic || warn_pmf2ptr)
6721 pedwarn (input_location, pedantic ? OPT_Wpedantic : OPT_Wpmf_conversions,
6722 "converting from %qT to %qT", intype, type);
6724 if (TREE_CODE (intype) == METHOD_TYPE)
6725 expr = build_addr_func (expr, complain);
6726 else if (TREE_CODE (expr) == PTRMEM_CST)
6727 expr = build_address (PTRMEM_CST_MEMBER (expr));
6728 else
6730 decl = maybe_dummy_object (TYPE_PTRMEM_CLASS_TYPE (intype), 0);
6731 decl = build_address (decl);
6732 expr = get_member_function_from_ptrfunc (&decl, expr, complain);
6735 if (expr == error_mark_node)
6736 return error_mark_node;
6738 return build_nop (type, expr);
6741 /* Return a representation for a reinterpret_cast from EXPR to TYPE.
6742 If C_CAST_P is true, this reinterpret cast is being done as part of
6743 a C-style cast. If VALID_P is non-NULL, *VALID_P is set to
6744 indicate whether or not reinterpret_cast was valid. */
6746 static tree
6747 build_reinterpret_cast_1 (tree type, tree expr, bool c_cast_p,
6748 bool *valid_p, tsubst_flags_t complain)
6750 tree intype;
6752 /* Assume the cast is invalid. */
6753 if (valid_p)
6754 *valid_p = true;
6756 if (type == error_mark_node || error_operand_p (expr))
6757 return error_mark_node;
6759 intype = TREE_TYPE (expr);
6761 /* Save casted types in the function's used types hash table. */
6762 used_types_insert (type);
6764 /* [expr.reinterpret.cast]
6765 An lvalue expression of type T1 can be cast to the type
6766 "reference to T2" if an expression of type "pointer to T1" can be
6767 explicitly converted to the type "pointer to T2" using a
6768 reinterpret_cast. */
6769 if (TREE_CODE (type) == REFERENCE_TYPE)
6771 if (! real_lvalue_p (expr))
6773 if (complain & tf_error)
6774 error ("invalid cast of an rvalue expression of type "
6775 "%qT to type %qT",
6776 intype, type);
6777 return error_mark_node;
6780 /* Warn about a reinterpret_cast from "A*" to "B&" if "A" and
6781 "B" are related class types; the reinterpret_cast does not
6782 adjust the pointer. */
6783 if (TYPE_PTR_P (intype)
6784 && (complain & tf_warning)
6785 && (comptypes (TREE_TYPE (intype), TREE_TYPE (type),
6786 COMPARE_BASE | COMPARE_DERIVED)))
6787 warning (0, "casting %qT to %qT does not dereference pointer",
6788 intype, type);
6790 expr = cp_build_addr_expr (expr, complain);
6792 if (warn_strict_aliasing > 2)
6793 strict_aliasing_warning (TREE_TYPE (expr), type, expr);
6795 if (expr != error_mark_node)
6796 expr = build_reinterpret_cast_1
6797 (build_pointer_type (TREE_TYPE (type)), expr, c_cast_p,
6798 valid_p, complain);
6799 if (expr != error_mark_node)
6800 /* cp_build_indirect_ref isn't right for rvalue refs. */
6801 expr = convert_from_reference (fold_convert (type, expr));
6802 return expr;
6805 /* As a G++ extension, we consider conversions from member
6806 functions, and pointers to member functions to
6807 pointer-to-function and pointer-to-void types. If
6808 -Wno-pmf-conversions has not been specified,
6809 convert_member_func_to_ptr will issue an error message. */
6810 if ((TYPE_PTRMEMFUNC_P (intype)
6811 || TREE_CODE (intype) == METHOD_TYPE)
6812 && TYPE_PTR_P (type)
6813 && (TREE_CODE (TREE_TYPE (type)) == FUNCTION_TYPE
6814 || VOID_TYPE_P (TREE_TYPE (type))))
6815 return convert_member_func_to_ptr (type, expr, complain);
6817 /* If the cast is not to a reference type, the lvalue-to-rvalue,
6818 array-to-pointer, and function-to-pointer conversions are
6819 performed. */
6820 expr = decay_conversion (expr, complain);
6822 /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
6823 Strip such NOP_EXPRs if VALUE is being used in non-lvalue context. */
6824 if (TREE_CODE (expr) == NOP_EXPR
6825 && TREE_TYPE (expr) == TREE_TYPE (TREE_OPERAND (expr, 0)))
6826 expr = TREE_OPERAND (expr, 0);
6828 if (error_operand_p (expr))
6829 return error_mark_node;
6831 intype = TREE_TYPE (expr);
6833 /* [expr.reinterpret.cast]
6834 A pointer can be converted to any integral type large enough to
6835 hold it. ... A value of type std::nullptr_t can be converted to
6836 an integral type; the conversion has the same meaning and
6837 validity as a conversion of (void*)0 to the integral type. */
6838 if (CP_INTEGRAL_TYPE_P (type)
6839 && (TYPE_PTR_P (intype) || NULLPTR_TYPE_P (intype)))
6841 if (TYPE_PRECISION (type) < TYPE_PRECISION (intype))
6843 if (complain & tf_error)
6844 permerror (input_location, "cast from %qT to %qT loses precision",
6845 intype, type);
6846 else
6847 return error_mark_node;
6849 if (NULLPTR_TYPE_P (intype))
6850 return build_int_cst (type, 0);
6852 /* [expr.reinterpret.cast]
6853 A value of integral or enumeration type can be explicitly
6854 converted to a pointer. */
6855 else if (TYPE_PTR_P (type) && INTEGRAL_OR_ENUMERATION_TYPE_P (intype))
6856 /* OK */
6858 else if ((INTEGRAL_OR_ENUMERATION_TYPE_P (type)
6859 || TYPE_PTR_OR_PTRMEM_P (type))
6860 && same_type_p (type, intype))
6861 /* DR 799 */
6862 return rvalue (expr);
6863 else if ((TYPE_PTRFN_P (type) && TYPE_PTRFN_P (intype))
6864 || (TYPE_PTRMEMFUNC_P (type) && TYPE_PTRMEMFUNC_P (intype)))
6865 return fold_if_not_in_template (build_nop (type, expr));
6866 else if ((TYPE_PTRDATAMEM_P (type) && TYPE_PTRDATAMEM_P (intype))
6867 || (TYPE_PTROBV_P (type) && TYPE_PTROBV_P (intype)))
6869 tree sexpr = expr;
6871 if (!c_cast_p
6872 && check_for_casting_away_constness (intype, type,
6873 REINTERPRET_CAST_EXPR,
6874 complain))
6875 return error_mark_node;
6876 /* Warn about possible alignment problems. */
6877 if (STRICT_ALIGNMENT && warn_cast_align
6878 && (complain & tf_warning)
6879 && !VOID_TYPE_P (type)
6880 && TREE_CODE (TREE_TYPE (intype)) != FUNCTION_TYPE
6881 && COMPLETE_TYPE_P (TREE_TYPE (type))
6882 && COMPLETE_TYPE_P (TREE_TYPE (intype))
6883 && TYPE_ALIGN (TREE_TYPE (type)) > TYPE_ALIGN (TREE_TYPE (intype)))
6884 warning (OPT_Wcast_align, "cast from %qT to %qT "
6885 "increases required alignment of target type", intype, type);
6887 /* We need to strip nops here, because the front end likes to
6888 create (int *)&a for array-to-pointer decay, instead of &a[0]. */
6889 STRIP_NOPS (sexpr);
6890 if (warn_strict_aliasing <= 2)
6891 strict_aliasing_warning (intype, type, sexpr);
6893 return fold_if_not_in_template (build_nop (type, expr));
6895 else if ((TYPE_PTRFN_P (type) && TYPE_PTROBV_P (intype))
6896 || (TYPE_PTRFN_P (intype) && TYPE_PTROBV_P (type)))
6898 if (complain & tf_warning)
6899 /* C++11 5.2.10 p8 says that "Converting a function pointer to an
6900 object pointer type or vice versa is conditionally-supported." */
6901 warning (OPT_Wconditionally_supported,
6902 "casting between pointer-to-function and pointer-to-object "
6903 "is conditionally-supported");
6904 return fold_if_not_in_template (build_nop (type, expr));
6906 else if (TREE_CODE (type) == VECTOR_TYPE)
6907 return fold_if_not_in_template (convert_to_vector (type, expr));
6908 else if (TREE_CODE (intype) == VECTOR_TYPE
6909 && INTEGRAL_OR_ENUMERATION_TYPE_P (type))
6910 return fold_if_not_in_template (convert_to_integer (type, expr));
6911 else
6913 if (valid_p)
6914 *valid_p = false;
6915 if (complain & tf_error)
6916 error ("invalid cast from type %qT to type %qT", intype, type);
6917 return error_mark_node;
6920 return cp_convert (type, expr, complain);
6923 tree
6924 build_reinterpret_cast (tree type, tree expr, tsubst_flags_t complain)
6926 tree r;
6928 if (type == error_mark_node || expr == error_mark_node)
6929 return error_mark_node;
6931 if (processing_template_decl)
6933 tree t = build_min (REINTERPRET_CAST_EXPR, type, expr);
6935 if (!TREE_SIDE_EFFECTS (t)
6936 && type_dependent_expression_p (expr))
6937 /* There might turn out to be side effects inside expr. */
6938 TREE_SIDE_EFFECTS (t) = 1;
6939 return convert_from_reference (t);
6942 r = build_reinterpret_cast_1 (type, expr, /*c_cast_p=*/false,
6943 /*valid_p=*/NULL, complain);
6944 if (r != error_mark_node)
6945 maybe_warn_about_useless_cast (type, expr, complain);
6946 return r;
6949 /* Perform a const_cast from EXPR to TYPE. If the cast is valid,
6950 return an appropriate expression. Otherwise, return
6951 error_mark_node. If the cast is not valid, and COMPLAIN is true,
6952 then a diagnostic will be issued. If VALID_P is non-NULL, we are
6953 performing a C-style cast, its value upon return will indicate
6954 whether or not the conversion succeeded. */
6956 static tree
6957 build_const_cast_1 (tree dst_type, tree expr, tsubst_flags_t complain,
6958 bool *valid_p)
6960 tree src_type;
6961 tree reference_type;
6963 /* Callers are responsible for handling error_mark_node as a
6964 destination type. */
6965 gcc_assert (dst_type != error_mark_node);
6966 /* In a template, callers should be building syntactic
6967 representations of casts, not using this machinery. */
6968 gcc_assert (!processing_template_decl);
6970 /* Assume the conversion is invalid. */
6971 if (valid_p)
6972 *valid_p = false;
6974 if (!POINTER_TYPE_P (dst_type) && !TYPE_PTRDATAMEM_P (dst_type))
6976 if (complain & tf_error)
6977 error ("invalid use of const_cast with type %qT, "
6978 "which is not a pointer, "
6979 "reference, nor a pointer-to-data-member type", dst_type);
6980 return error_mark_node;
6983 if (TREE_CODE (TREE_TYPE (dst_type)) == FUNCTION_TYPE)
6985 if (complain & tf_error)
6986 error ("invalid use of const_cast with type %qT, which is a pointer "
6987 "or reference to a function type", dst_type);
6988 return error_mark_node;
6991 /* Save casted types in the function's used types hash table. */
6992 used_types_insert (dst_type);
6994 src_type = TREE_TYPE (expr);
6995 /* Expressions do not really have reference types. */
6996 if (TREE_CODE (src_type) == REFERENCE_TYPE)
6997 src_type = TREE_TYPE (src_type);
6999 /* [expr.const.cast]
7001 For two object types T1 and T2, if a pointer to T1 can be explicitly
7002 converted to the type "pointer to T2" using a const_cast, then the
7003 following conversions can also be made:
7005 -- an lvalue of type T1 can be explicitly converted to an lvalue of
7006 type T2 using the cast const_cast<T2&>;
7008 -- a glvalue of type T1 can be explicitly converted to an xvalue of
7009 type T2 using the cast const_cast<T2&&>; and
7011 -- if T1 is a class type, a prvalue of type T1 can be explicitly
7012 converted to an xvalue of type T2 using the cast const_cast<T2&&>. */
7014 if (TREE_CODE (dst_type) == REFERENCE_TYPE)
7016 reference_type = dst_type;
7017 if (!TYPE_REF_IS_RVALUE (dst_type)
7018 ? real_lvalue_p (expr)
7019 : (CLASS_TYPE_P (TREE_TYPE (dst_type))
7020 ? lvalue_p (expr)
7021 : lvalue_or_rvalue_with_address_p (expr)))
7022 /* OK. */;
7023 else
7025 if (complain & tf_error)
7026 error ("invalid const_cast of an rvalue of type %qT to type %qT",
7027 src_type, dst_type);
7028 return error_mark_node;
7030 dst_type = build_pointer_type (TREE_TYPE (dst_type));
7031 src_type = build_pointer_type (src_type);
7033 else
7035 reference_type = NULL_TREE;
7036 /* If the destination type is not a reference type, the
7037 lvalue-to-rvalue, array-to-pointer, and function-to-pointer
7038 conversions are performed. */
7039 src_type = type_decays_to (src_type);
7040 if (src_type == error_mark_node)
7041 return error_mark_node;
7044 if (TYPE_PTR_P (src_type) || TYPE_PTRDATAMEM_P (src_type))
7046 if (comp_ptr_ttypes_const (dst_type, src_type))
7048 if (valid_p)
7050 *valid_p = true;
7051 /* This cast is actually a C-style cast. Issue a warning if
7052 the user is making a potentially unsafe cast. */
7053 check_for_casting_away_constness (src_type, dst_type,
7054 CAST_EXPR, complain);
7056 if (reference_type)
7058 expr = cp_build_addr_expr (expr, complain);
7059 if (expr == error_mark_node)
7060 return error_mark_node;
7061 expr = build_nop (reference_type, expr);
7062 return convert_from_reference (expr);
7064 else
7066 expr = decay_conversion (expr, complain);
7067 if (expr == error_mark_node)
7068 return error_mark_node;
7070 /* build_c_cast puts on a NOP_EXPR to make the result not an
7071 lvalue. Strip such NOP_EXPRs if VALUE is being used in
7072 non-lvalue context. */
7073 if (TREE_CODE (expr) == NOP_EXPR
7074 && TREE_TYPE (expr) == TREE_TYPE (TREE_OPERAND (expr, 0)))
7075 expr = TREE_OPERAND (expr, 0);
7076 return build_nop (dst_type, expr);
7079 else if (valid_p
7080 && !at_least_as_qualified_p (TREE_TYPE (dst_type),
7081 TREE_TYPE (src_type)))
7082 check_for_casting_away_constness (src_type, dst_type, CAST_EXPR,
7083 complain);
7086 if (complain & tf_error)
7087 error ("invalid const_cast from type %qT to type %qT",
7088 src_type, dst_type);
7089 return error_mark_node;
7092 tree
7093 build_const_cast (tree type, tree expr, tsubst_flags_t complain)
7095 tree r;
7097 if (type == error_mark_node || error_operand_p (expr))
7098 return error_mark_node;
7100 if (processing_template_decl)
7102 tree t = build_min (CONST_CAST_EXPR, type, expr);
7104 if (!TREE_SIDE_EFFECTS (t)
7105 && type_dependent_expression_p (expr))
7106 /* There might turn out to be side effects inside expr. */
7107 TREE_SIDE_EFFECTS (t) = 1;
7108 return convert_from_reference (t);
7111 r = build_const_cast_1 (type, expr, complain, /*valid_p=*/NULL);
7112 if (r != error_mark_node)
7113 maybe_warn_about_useless_cast (type, expr, complain);
7114 return r;
7117 /* Like cp_build_c_cast, but for the c-common bits. */
7119 tree
7120 build_c_cast (location_t /*loc*/, tree type, tree expr)
7122 return cp_build_c_cast (type, expr, tf_warning_or_error);
7125 /* Build an expression representing an explicit C-style cast to type
7126 TYPE of expression EXPR. */
7128 tree
7129 cp_build_c_cast (tree type, tree expr, tsubst_flags_t complain)
7131 tree value = expr;
7132 tree result;
7133 bool valid_p;
7135 if (type == error_mark_node || error_operand_p (expr))
7136 return error_mark_node;
7138 if (processing_template_decl)
7140 tree t = build_min (CAST_EXPR, type,
7141 tree_cons (NULL_TREE, value, NULL_TREE));
7142 /* We don't know if it will or will not have side effects. */
7143 TREE_SIDE_EFFECTS (t) = 1;
7144 return convert_from_reference (t);
7147 /* Casts to a (pointer to a) specific ObjC class (or 'id' or
7148 'Class') should always be retained, because this information aids
7149 in method lookup. */
7150 if (objc_is_object_ptr (type)
7151 && objc_is_object_ptr (TREE_TYPE (expr)))
7152 return build_nop (type, expr);
7154 /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
7155 Strip such NOP_EXPRs if VALUE is being used in non-lvalue context. */
7156 if (TREE_CODE (type) != REFERENCE_TYPE
7157 && TREE_CODE (value) == NOP_EXPR
7158 && TREE_TYPE (value) == TREE_TYPE (TREE_OPERAND (value, 0)))
7159 value = TREE_OPERAND (value, 0);
7161 if (TREE_CODE (type) == ARRAY_TYPE)
7163 /* Allow casting from T1* to T2[] because Cfront allows it.
7164 NIHCL uses it. It is not valid ISO C++ however. */
7165 if (TYPE_PTR_P (TREE_TYPE (expr)))
7167 if (complain & tf_error)
7168 permerror (input_location, "ISO C++ forbids casting to an array type %qT", type);
7169 else
7170 return error_mark_node;
7171 type = build_pointer_type (TREE_TYPE (type));
7173 else
7175 if (complain & tf_error)
7176 error ("ISO C++ forbids casting to an array type %qT", type);
7177 return error_mark_node;
7181 if (TREE_CODE (type) == FUNCTION_TYPE
7182 || TREE_CODE (type) == METHOD_TYPE)
7184 if (complain & tf_error)
7185 error ("invalid cast to function type %qT", type);
7186 return error_mark_node;
7189 if (TYPE_PTR_P (type)
7190 && TREE_CODE (TREE_TYPE (value)) == INTEGER_TYPE
7191 /* Casting to an integer of smaller size is an error detected elsewhere. */
7192 && TYPE_PRECISION (type) > TYPE_PRECISION (TREE_TYPE (value))
7193 /* Don't warn about converting any constant. */
7194 && !TREE_CONSTANT (value))
7195 warning_at (input_location, OPT_Wint_to_pointer_cast,
7196 "cast to pointer from integer of different size");
7198 /* A C-style cast can be a const_cast. */
7199 result = build_const_cast_1 (type, value, complain & tf_warning,
7200 &valid_p);
7201 if (valid_p)
7203 if (result != error_mark_node)
7204 maybe_warn_about_useless_cast (type, value, complain);
7205 return result;
7208 /* Or a static cast. */
7209 result = build_static_cast_1 (type, value, /*c_cast_p=*/true,
7210 &valid_p, complain);
7211 /* Or a reinterpret_cast. */
7212 if (!valid_p)
7213 result = build_reinterpret_cast_1 (type, value, /*c_cast_p=*/true,
7214 &valid_p, complain);
7215 /* The static_cast or reinterpret_cast may be followed by a
7216 const_cast. */
7217 if (valid_p
7218 /* A valid cast may result in errors if, for example, a
7219 conversion to an ambiguous base class is required. */
7220 && !error_operand_p (result))
7222 tree result_type;
7224 maybe_warn_about_useless_cast (type, value, complain);
7226 /* Non-class rvalues always have cv-unqualified type. */
7227 if (!CLASS_TYPE_P (type))
7228 type = TYPE_MAIN_VARIANT (type);
7229 result_type = TREE_TYPE (result);
7230 if (!CLASS_TYPE_P (result_type) && TREE_CODE (type) != REFERENCE_TYPE)
7231 result_type = TYPE_MAIN_VARIANT (result_type);
7232 /* If the type of RESULT does not match TYPE, perform a
7233 const_cast to make it match. If the static_cast or
7234 reinterpret_cast succeeded, we will differ by at most
7235 cv-qualification, so the follow-on const_cast is guaranteed
7236 to succeed. */
7237 if (!same_type_p (non_reference (type), non_reference (result_type)))
7239 result = build_const_cast_1 (type, result, false, &valid_p);
7240 gcc_assert (valid_p);
7242 return result;
7245 return error_mark_node;
7248 /* For use from the C common bits. */
7249 tree
7250 build_modify_expr (location_t /*location*/,
7251 tree lhs, tree /*lhs_origtype*/,
7252 enum tree_code modifycode,
7253 location_t /*rhs_location*/, tree rhs,
7254 tree /*rhs_origtype*/)
7256 return cp_build_modify_expr (lhs, modifycode, rhs, tf_warning_or_error);
7259 /* Build an assignment expression of lvalue LHS from value RHS.
7260 MODIFYCODE is the code for a binary operator that we use
7261 to combine the old value of LHS with RHS to get the new value.
7262 Or else MODIFYCODE is NOP_EXPR meaning do a simple assignment.
7264 C++: If MODIFYCODE is INIT_EXPR, then leave references unbashed. */
7266 tree
7267 cp_build_modify_expr (tree lhs, enum tree_code modifycode, tree rhs,
7268 tsubst_flags_t complain)
7270 tree result;
7271 tree newrhs = rhs;
7272 tree lhstype = TREE_TYPE (lhs);
7273 tree olhstype = lhstype;
7274 bool plain_assign = (modifycode == NOP_EXPR);
7276 /* Avoid duplicate error messages from operands that had errors. */
7277 if (error_operand_p (lhs) || error_operand_p (rhs))
7278 return error_mark_node;
7280 /* Handle control structure constructs used as "lvalues". */
7281 switch (TREE_CODE (lhs))
7283 /* Handle --foo = 5; as these are valid constructs in C++. */
7284 case PREDECREMENT_EXPR:
7285 case PREINCREMENT_EXPR:
7286 if (TREE_SIDE_EFFECTS (TREE_OPERAND (lhs, 0)))
7287 lhs = build2 (TREE_CODE (lhs), TREE_TYPE (lhs),
7288 stabilize_reference (TREE_OPERAND (lhs, 0)),
7289 TREE_OPERAND (lhs, 1));
7290 newrhs = cp_build_modify_expr (TREE_OPERAND (lhs, 0),
7291 modifycode, rhs, complain);
7292 if (newrhs == error_mark_node)
7293 return error_mark_node;
7294 return build2 (COMPOUND_EXPR, lhstype, lhs, newrhs);
7296 /* Handle (a, b) used as an "lvalue". */
7297 case COMPOUND_EXPR:
7298 newrhs = cp_build_modify_expr (TREE_OPERAND (lhs, 1),
7299 modifycode, rhs, complain);
7300 if (newrhs == error_mark_node)
7301 return error_mark_node;
7302 return build2 (COMPOUND_EXPR, lhstype,
7303 TREE_OPERAND (lhs, 0), newrhs);
7305 case MODIFY_EXPR:
7306 if (TREE_SIDE_EFFECTS (TREE_OPERAND (lhs, 0)))
7307 lhs = build2 (TREE_CODE (lhs), TREE_TYPE (lhs),
7308 stabilize_reference (TREE_OPERAND (lhs, 0)),
7309 TREE_OPERAND (lhs, 1));
7310 newrhs = cp_build_modify_expr (TREE_OPERAND (lhs, 0), modifycode, rhs,
7311 complain);
7312 if (newrhs == error_mark_node)
7313 return error_mark_node;
7314 return build2 (COMPOUND_EXPR, lhstype, lhs, newrhs);
7316 case MIN_EXPR:
7317 case MAX_EXPR:
7318 /* MIN_EXPR and MAX_EXPR are currently only permitted as lvalues,
7319 when neither operand has side-effects. */
7320 if (!lvalue_or_else (lhs, lv_assign, complain))
7321 return error_mark_node;
7323 gcc_assert (!TREE_SIDE_EFFECTS (TREE_OPERAND (lhs, 0))
7324 && !TREE_SIDE_EFFECTS (TREE_OPERAND (lhs, 1)));
7326 lhs = build3 (COND_EXPR, TREE_TYPE (lhs),
7327 build2 (TREE_CODE (lhs) == MIN_EXPR ? LE_EXPR : GE_EXPR,
7328 boolean_type_node,
7329 TREE_OPERAND (lhs, 0),
7330 TREE_OPERAND (lhs, 1)),
7331 TREE_OPERAND (lhs, 0),
7332 TREE_OPERAND (lhs, 1));
7333 /* Fall through. */
7335 /* Handle (a ? b : c) used as an "lvalue". */
7336 case COND_EXPR:
7338 /* Produce (a ? (b = rhs) : (c = rhs))
7339 except that the RHS goes through a save-expr
7340 so the code to compute it is only emitted once. */
7341 tree cond;
7342 tree preeval = NULL_TREE;
7344 if (VOID_TYPE_P (TREE_TYPE (rhs)))
7346 if (complain & tf_error)
7347 error ("void value not ignored as it ought to be");
7348 return error_mark_node;
7351 rhs = stabilize_expr (rhs, &preeval);
7353 /* Check this here to avoid odd errors when trying to convert
7354 a throw to the type of the COND_EXPR. */
7355 if (!lvalue_or_else (lhs, lv_assign, complain))
7356 return error_mark_node;
7358 cond = build_conditional_expr
7359 (input_location, TREE_OPERAND (lhs, 0),
7360 cp_build_modify_expr (TREE_OPERAND (lhs, 1),
7361 modifycode, rhs, complain),
7362 cp_build_modify_expr (TREE_OPERAND (lhs, 2),
7363 modifycode, rhs, complain),
7364 complain);
7366 if (cond == error_mark_node)
7367 return cond;
7368 /* Make sure the code to compute the rhs comes out
7369 before the split. */
7370 if (preeval)
7371 cond = build2 (COMPOUND_EXPR, TREE_TYPE (lhs), preeval, cond);
7372 return cond;
7375 default:
7376 break;
7379 if (modifycode == INIT_EXPR)
7381 if (BRACE_ENCLOSED_INITIALIZER_P (rhs))
7382 /* Do the default thing. */;
7383 else if (TREE_CODE (rhs) == CONSTRUCTOR)
7385 /* Compound literal. */
7386 if (! same_type_p (TREE_TYPE (rhs), lhstype))
7387 /* Call convert to generate an error; see PR 11063. */
7388 rhs = convert (lhstype, rhs);
7389 result = build2 (INIT_EXPR, lhstype, lhs, rhs);
7390 TREE_SIDE_EFFECTS (result) = 1;
7391 return result;
7393 else if (! MAYBE_CLASS_TYPE_P (lhstype))
7394 /* Do the default thing. */;
7395 else
7397 vec<tree, va_gc> *rhs_vec = make_tree_vector_single (rhs);
7398 result = build_special_member_call (lhs, complete_ctor_identifier,
7399 &rhs_vec, lhstype, LOOKUP_NORMAL,
7400 complain);
7401 release_tree_vector (rhs_vec);
7402 if (result == NULL_TREE)
7403 return error_mark_node;
7404 return result;
7407 else
7409 lhs = require_complete_type_sfinae (lhs, complain);
7410 if (lhs == error_mark_node)
7411 return error_mark_node;
7413 if (modifycode == NOP_EXPR)
7415 if (c_dialect_objc ())
7417 result = objc_maybe_build_modify_expr (lhs, rhs);
7418 if (result)
7419 return result;
7422 /* `operator=' is not an inheritable operator. */
7423 if (! MAYBE_CLASS_TYPE_P (lhstype))
7424 /* Do the default thing. */;
7425 else
7427 result = build_new_op (input_location, MODIFY_EXPR,
7428 LOOKUP_NORMAL, lhs, rhs,
7429 make_node (NOP_EXPR), /*overload=*/NULL,
7430 complain);
7431 if (result == NULL_TREE)
7432 return error_mark_node;
7433 return result;
7435 lhstype = olhstype;
7437 else
7439 tree init = NULL_TREE;
7441 /* A binary op has been requested. Combine the old LHS
7442 value with the RHS producing the value we should actually
7443 store into the LHS. */
7444 gcc_assert (!((TREE_CODE (lhstype) == REFERENCE_TYPE
7445 && MAYBE_CLASS_TYPE_P (TREE_TYPE (lhstype)))
7446 || MAYBE_CLASS_TYPE_P (lhstype)));
7448 /* Preevaluate the RHS to make sure its evaluation is complete
7449 before the lvalue-to-rvalue conversion of the LHS:
7451 [expr.ass] With respect to an indeterminately-sequenced
7452 function call, the operation of a compound assignment is a
7453 single evaluation. [ Note: Therefore, a function call shall
7454 not intervene between the lvalue-to-rvalue conversion and the
7455 side effect associated with any single compound assignment
7456 operator. -- end note ] */
7457 lhs = stabilize_reference (lhs);
7458 rhs = rvalue (rhs);
7459 rhs = stabilize_expr (rhs, &init);
7460 newrhs = cp_build_binary_op (input_location,
7461 modifycode, lhs, rhs,
7462 complain);
7463 if (newrhs == error_mark_node)
7465 if (complain & tf_error)
7466 error (" in evaluation of %<%Q(%#T, %#T)%>", modifycode,
7467 TREE_TYPE (lhs), TREE_TYPE (rhs));
7468 return error_mark_node;
7471 if (init)
7472 newrhs = build2 (COMPOUND_EXPR, TREE_TYPE (newrhs), init, newrhs);
7474 /* Now it looks like a plain assignment. */
7475 modifycode = NOP_EXPR;
7476 if (c_dialect_objc ())
7478 result = objc_maybe_build_modify_expr (lhs, newrhs);
7479 if (result)
7480 return result;
7483 gcc_assert (TREE_CODE (lhstype) != REFERENCE_TYPE);
7484 gcc_assert (TREE_CODE (TREE_TYPE (newrhs)) != REFERENCE_TYPE);
7487 /* The left-hand side must be an lvalue. */
7488 if (!lvalue_or_else (lhs, lv_assign, complain))
7489 return error_mark_node;
7491 /* Warn about modifying something that is `const'. Don't warn if
7492 this is initialization. */
7493 if (modifycode != INIT_EXPR
7494 && (TREE_READONLY (lhs) || CP_TYPE_CONST_P (lhstype)
7495 /* Functions are not modifiable, even though they are
7496 lvalues. */
7497 || TREE_CODE (TREE_TYPE (lhs)) == FUNCTION_TYPE
7498 || TREE_CODE (TREE_TYPE (lhs)) == METHOD_TYPE
7499 /* If it's an aggregate and any field is const, then it is
7500 effectively const. */
7501 || (CLASS_TYPE_P (lhstype)
7502 && C_TYPE_FIELDS_READONLY (lhstype))))
7504 if (complain & tf_error)
7505 cxx_readonly_error (lhs, lv_assign);
7506 else
7507 return error_mark_node;
7510 /* If storing into a structure or union member, it may have been given a
7511 lowered bitfield type. We need to convert to the declared type first,
7512 so retrieve it now. */
7514 olhstype = unlowered_expr_type (lhs);
7516 /* Convert new value to destination type. */
7518 if (TREE_CODE (lhstype) == ARRAY_TYPE)
7520 int from_array;
7522 if (BRACE_ENCLOSED_INITIALIZER_P (newrhs))
7524 if (modifycode != INIT_EXPR)
7526 if (complain & tf_error)
7527 error ("assigning to an array from an initializer list");
7528 return error_mark_node;
7530 if (check_array_initializer (lhs, lhstype, newrhs))
7531 return error_mark_node;
7532 newrhs = digest_init (lhstype, newrhs, complain);
7533 if (newrhs == error_mark_node)
7534 return error_mark_node;
7537 /* C++11 8.5/17: "If the destination type is an array of characters,
7538 an array of char16_t, an array of char32_t, or an array of wchar_t,
7539 and the initializer is a string literal...". */
7540 else if (TREE_CODE (newrhs) == STRING_CST
7541 && char_type_p (TREE_TYPE (TYPE_MAIN_VARIANT (lhstype)))
7542 && modifycode == INIT_EXPR)
7544 newrhs = digest_init (lhstype, newrhs, complain);
7545 if (newrhs == error_mark_node)
7546 return error_mark_node;
7549 else if (!same_or_base_type_p (TYPE_MAIN_VARIANT (lhstype),
7550 TYPE_MAIN_VARIANT (TREE_TYPE (newrhs))))
7552 if (complain & tf_error)
7553 error ("incompatible types in assignment of %qT to %qT",
7554 TREE_TYPE (rhs), lhstype);
7555 return error_mark_node;
7558 /* Allow array assignment in compiler-generated code. */
7559 else if (!current_function_decl
7560 || !DECL_DEFAULTED_FN (current_function_decl))
7562 /* This routine is used for both initialization and assignment.
7563 Make sure the diagnostic message differentiates the context. */
7564 if (complain & tf_error)
7566 if (modifycode == INIT_EXPR)
7567 error ("array used as initializer");
7568 else
7569 error ("invalid array assignment");
7571 return error_mark_node;
7574 from_array = TREE_CODE (TREE_TYPE (newrhs)) == ARRAY_TYPE
7575 ? 1 + (modifycode != INIT_EXPR): 0;
7576 return build_vec_init (lhs, NULL_TREE, newrhs,
7577 /*explicit_value_init_p=*/false,
7578 from_array, complain);
7581 if (modifycode == INIT_EXPR)
7582 /* Calls with INIT_EXPR are all direct-initialization, so don't set
7583 LOOKUP_ONLYCONVERTING. */
7584 newrhs = convert_for_initialization (lhs, olhstype, newrhs, LOOKUP_NORMAL,
7585 ICR_INIT, NULL_TREE, 0,
7586 complain);
7587 else
7588 newrhs = convert_for_assignment (olhstype, newrhs, ICR_ASSIGN,
7589 NULL_TREE, 0, complain, LOOKUP_IMPLICIT);
7591 if (!same_type_p (lhstype, olhstype))
7592 newrhs = cp_convert_and_check (lhstype, newrhs, complain);
7594 if (modifycode != INIT_EXPR)
7596 if (TREE_CODE (newrhs) == CALL_EXPR
7597 && TYPE_NEEDS_CONSTRUCTING (lhstype))
7598 newrhs = build_cplus_new (lhstype, newrhs, complain);
7600 /* Can't initialize directly from a TARGET_EXPR, since that would
7601 cause the lhs to be constructed twice, and possibly result in
7602 accidental self-initialization. So we force the TARGET_EXPR to be
7603 expanded without a target. */
7604 if (TREE_CODE (newrhs) == TARGET_EXPR)
7605 newrhs = build2 (COMPOUND_EXPR, TREE_TYPE (newrhs), newrhs,
7606 TREE_OPERAND (newrhs, 0));
7609 if (newrhs == error_mark_node)
7610 return error_mark_node;
7612 if (c_dialect_objc () && flag_objc_gc)
7614 result = objc_generate_write_barrier (lhs, modifycode, newrhs);
7616 if (result)
7617 return result;
7620 result = build2 (modifycode == NOP_EXPR ? MODIFY_EXPR : INIT_EXPR,
7621 lhstype, lhs, newrhs);
7623 TREE_SIDE_EFFECTS (result) = 1;
7624 if (!plain_assign)
7625 TREE_NO_WARNING (result) = 1;
7627 return result;
7630 tree
7631 build_x_modify_expr (location_t loc, tree lhs, enum tree_code modifycode,
7632 tree rhs, tsubst_flags_t complain)
7634 if (processing_template_decl)
7635 return build_min_nt_loc (loc, MODOP_EXPR, lhs,
7636 build_min_nt_loc (loc, modifycode, NULL_TREE,
7637 NULL_TREE), rhs);
7639 if (modifycode != NOP_EXPR)
7641 tree rval = build_new_op (loc, MODIFY_EXPR, LOOKUP_NORMAL, lhs, rhs,
7642 make_node (modifycode), /*overload=*/NULL,
7643 complain);
7644 if (rval)
7646 TREE_NO_WARNING (rval) = 1;
7647 return rval;
7650 return cp_build_modify_expr (lhs, modifycode, rhs, complain);
7653 /* Helper function for get_delta_difference which assumes FROM is a base
7654 class of TO. Returns a delta for the conversion of pointer-to-member
7655 of FROM to pointer-to-member of TO. If the conversion is invalid and
7656 tf_error is not set in COMPLAIN returns error_mark_node, otherwise
7657 returns zero. If FROM is not a base class of TO, returns NULL_TREE.
7658 If C_CAST_P is true, this conversion is taking place as part of a
7659 C-style cast. */
7661 static tree
7662 get_delta_difference_1 (tree from, tree to, bool c_cast_p,
7663 tsubst_flags_t complain)
7665 tree binfo;
7666 base_kind kind;
7668 binfo = lookup_base (to, from, c_cast_p ? ba_unique : ba_check,
7669 &kind, complain);
7671 if (binfo == error_mark_node)
7673 if (!(complain & tf_error))
7674 return error_mark_node;
7676 error (" in pointer to member function conversion");
7677 return size_zero_node;
7679 else if (binfo)
7681 if (kind != bk_via_virtual)
7682 return BINFO_OFFSET (binfo);
7683 else
7684 /* FROM is a virtual base class of TO. Issue an error or warning
7685 depending on whether or not this is a reinterpret cast. */
7687 if (!(complain & tf_error))
7688 return error_mark_node;
7690 error ("pointer to member conversion via virtual base %qT",
7691 BINFO_TYPE (binfo_from_vbase (binfo)));
7693 return size_zero_node;
7696 else
7697 return NULL_TREE;
7700 /* Get difference in deltas for different pointer to member function
7701 types. If the conversion is invalid and tf_error is not set in
7702 COMPLAIN, returns error_mark_node, otherwise returns an integer
7703 constant of type PTRDIFF_TYPE_NODE and its value is zero if the
7704 conversion is invalid. If ALLOW_INVERSE_P is true, then allow reverse
7705 conversions as well. If C_CAST_P is true this conversion is taking
7706 place as part of a C-style cast.
7708 Note that the naming of FROM and TO is kind of backwards; the return
7709 value is what we add to a TO in order to get a FROM. They are named
7710 this way because we call this function to find out how to convert from
7711 a pointer to member of FROM to a pointer to member of TO. */
7713 static tree
7714 get_delta_difference (tree from, tree to,
7715 bool allow_inverse_p,
7716 bool c_cast_p, tsubst_flags_t complain)
7718 tree result;
7720 if (same_type_ignoring_top_level_qualifiers_p (from, to))
7721 /* Pointer to member of incomplete class is permitted*/
7722 result = size_zero_node;
7723 else
7724 result = get_delta_difference_1 (from, to, c_cast_p, complain);
7726 if (result == error_mark_node)
7727 return error_mark_node;
7729 if (!result)
7731 if (!allow_inverse_p)
7733 if (!(complain & tf_error))
7734 return error_mark_node;
7736 error_not_base_type (from, to);
7737 error (" in pointer to member conversion");
7738 result = size_zero_node;
7740 else
7742 result = get_delta_difference_1 (to, from, c_cast_p, complain);
7744 if (result == error_mark_node)
7745 return error_mark_node;
7747 if (result)
7748 result = size_diffop_loc (input_location,
7749 size_zero_node, result);
7750 else
7752 if (!(complain & tf_error))
7753 return error_mark_node;
7755 error_not_base_type (from, to);
7756 error (" in pointer to member conversion");
7757 result = size_zero_node;
7762 return fold_if_not_in_template (convert_to_integer (ptrdiff_type_node,
7763 result));
7766 /* Return a constructor for the pointer-to-member-function TYPE using
7767 the other components as specified. */
7769 tree
7770 build_ptrmemfunc1 (tree type, tree delta, tree pfn)
7772 tree u = NULL_TREE;
7773 tree delta_field;
7774 tree pfn_field;
7775 vec<constructor_elt, va_gc> *v;
7777 /* Pull the FIELD_DECLs out of the type. */
7778 pfn_field = TYPE_FIELDS (type);
7779 delta_field = DECL_CHAIN (pfn_field);
7781 /* Make sure DELTA has the type we want. */
7782 delta = convert_and_check (input_location, delta_type_node, delta);
7784 /* Convert to the correct target type if necessary. */
7785 pfn = fold_convert (TREE_TYPE (pfn_field), pfn);
7787 /* Finish creating the initializer. */
7788 vec_alloc (v, 2);
7789 CONSTRUCTOR_APPEND_ELT(v, pfn_field, pfn);
7790 CONSTRUCTOR_APPEND_ELT(v, delta_field, delta);
7791 u = build_constructor (type, v);
7792 TREE_CONSTANT (u) = TREE_CONSTANT (pfn) & TREE_CONSTANT (delta);
7793 TREE_STATIC (u) = (TREE_CONSTANT (u)
7794 && (initializer_constant_valid_p (pfn, TREE_TYPE (pfn))
7795 != NULL_TREE)
7796 && (initializer_constant_valid_p (delta, TREE_TYPE (delta))
7797 != NULL_TREE));
7798 return u;
7801 /* Build a constructor for a pointer to member function. It can be
7802 used to initialize global variables, local variable, or used
7803 as a value in expressions. TYPE is the POINTER to METHOD_TYPE we
7804 want to be.
7806 If FORCE is nonzero, then force this conversion, even if
7807 we would rather not do it. Usually set when using an explicit
7808 cast. A C-style cast is being processed iff C_CAST_P is true.
7810 Return error_mark_node, if something goes wrong. */
7812 tree
7813 build_ptrmemfunc (tree type, tree pfn, int force, bool c_cast_p,
7814 tsubst_flags_t complain)
7816 tree fn;
7817 tree pfn_type;
7818 tree to_type;
7820 if (error_operand_p (pfn))
7821 return error_mark_node;
7823 pfn_type = TREE_TYPE (pfn);
7824 to_type = build_ptrmemfunc_type (type);
7826 /* Handle multiple conversions of pointer to member functions. */
7827 if (TYPE_PTRMEMFUNC_P (pfn_type))
7829 tree delta = NULL_TREE;
7830 tree npfn = NULL_TREE;
7831 tree n;
7833 if (!force
7834 && !can_convert_arg (to_type, TREE_TYPE (pfn), pfn,
7835 LOOKUP_NORMAL, complain))
7837 if (complain & tf_error)
7838 error ("invalid conversion to type %qT from type %qT",
7839 to_type, pfn_type);
7840 else
7841 return error_mark_node;
7844 n = get_delta_difference (TYPE_PTRMEMFUNC_OBJECT_TYPE (pfn_type),
7845 TYPE_PTRMEMFUNC_OBJECT_TYPE (to_type),
7846 force,
7847 c_cast_p, complain);
7848 if (n == error_mark_node)
7849 return error_mark_node;
7851 /* We don't have to do any conversion to convert a
7852 pointer-to-member to its own type. But, we don't want to
7853 just return a PTRMEM_CST if there's an explicit cast; that
7854 cast should make the expression an invalid template argument. */
7855 if (TREE_CODE (pfn) != PTRMEM_CST)
7857 if (same_type_p (to_type, pfn_type))
7858 return pfn;
7859 else if (integer_zerop (n))
7860 return build_reinterpret_cast (to_type, pfn,
7861 complain);
7864 if (TREE_SIDE_EFFECTS (pfn))
7865 pfn = save_expr (pfn);
7867 /* Obtain the function pointer and the current DELTA. */
7868 if (TREE_CODE (pfn) == PTRMEM_CST)
7869 expand_ptrmemfunc_cst (pfn, &delta, &npfn);
7870 else
7872 npfn = build_ptrmemfunc_access_expr (pfn, pfn_identifier);
7873 delta = build_ptrmemfunc_access_expr (pfn, delta_identifier);
7876 /* Just adjust the DELTA field. */
7877 gcc_assert (same_type_ignoring_top_level_qualifiers_p
7878 (TREE_TYPE (delta), ptrdiff_type_node));
7879 if (TARGET_PTRMEMFUNC_VBIT_LOCATION == ptrmemfunc_vbit_in_delta)
7880 n = cp_build_binary_op (input_location,
7881 LSHIFT_EXPR, n, integer_one_node,
7882 complain);
7883 delta = cp_build_binary_op (input_location,
7884 PLUS_EXPR, delta, n, complain);
7885 return build_ptrmemfunc1 (to_type, delta, npfn);
7888 /* Handle null pointer to member function conversions. */
7889 if (null_ptr_cst_p (pfn))
7891 pfn = cp_build_c_cast (type, pfn, complain);
7892 return build_ptrmemfunc1 (to_type,
7893 integer_zero_node,
7894 pfn);
7897 if (type_unknown_p (pfn))
7898 return instantiate_type (type, pfn, complain);
7900 fn = TREE_OPERAND (pfn, 0);
7901 gcc_assert (TREE_CODE (fn) == FUNCTION_DECL
7902 /* In a template, we will have preserved the
7903 OFFSET_REF. */
7904 || (processing_template_decl && TREE_CODE (fn) == OFFSET_REF));
7905 return make_ptrmem_cst (to_type, fn);
7908 /* Return the DELTA, IDX, PFN, and DELTA2 values for the PTRMEM_CST
7909 given by CST.
7911 ??? There is no consistency as to the types returned for the above
7912 values. Some code acts as if it were a sizetype and some as if it were
7913 integer_type_node. */
7915 void
7916 expand_ptrmemfunc_cst (tree cst, tree *delta, tree *pfn)
7918 tree type = TREE_TYPE (cst);
7919 tree fn = PTRMEM_CST_MEMBER (cst);
7920 tree ptr_class, fn_class;
7922 gcc_assert (TREE_CODE (fn) == FUNCTION_DECL);
7924 /* The class that the function belongs to. */
7925 fn_class = DECL_CONTEXT (fn);
7927 /* The class that we're creating a pointer to member of. */
7928 ptr_class = TYPE_PTRMEMFUNC_OBJECT_TYPE (type);
7930 /* First, calculate the adjustment to the function's class. */
7931 *delta = get_delta_difference (fn_class, ptr_class, /*force=*/0,
7932 /*c_cast_p=*/0, tf_warning_or_error);
7934 if (!DECL_VIRTUAL_P (fn))
7935 *pfn = convert (TYPE_PTRMEMFUNC_FN_TYPE (type),
7936 build_addr_func (fn, tf_warning_or_error));
7937 else
7939 /* If we're dealing with a virtual function, we have to adjust 'this'
7940 again, to point to the base which provides the vtable entry for
7941 fn; the call will do the opposite adjustment. */
7942 tree orig_class = DECL_CONTEXT (fn);
7943 tree binfo = binfo_or_else (orig_class, fn_class);
7944 *delta = build2 (PLUS_EXPR, TREE_TYPE (*delta),
7945 *delta, BINFO_OFFSET (binfo));
7946 *delta = fold_if_not_in_template (*delta);
7948 /* We set PFN to the vtable offset at which the function can be
7949 found, plus one (unless ptrmemfunc_vbit_in_delta, in which
7950 case delta is shifted left, and then incremented). */
7951 *pfn = DECL_VINDEX (fn);
7952 *pfn = build2 (MULT_EXPR, integer_type_node, *pfn,
7953 TYPE_SIZE_UNIT (vtable_entry_type));
7954 *pfn = fold_if_not_in_template (*pfn);
7956 switch (TARGET_PTRMEMFUNC_VBIT_LOCATION)
7958 case ptrmemfunc_vbit_in_pfn:
7959 *pfn = build2 (PLUS_EXPR, integer_type_node, *pfn,
7960 integer_one_node);
7961 *pfn = fold_if_not_in_template (*pfn);
7962 break;
7964 case ptrmemfunc_vbit_in_delta:
7965 *delta = build2 (LSHIFT_EXPR, TREE_TYPE (*delta),
7966 *delta, integer_one_node);
7967 *delta = fold_if_not_in_template (*delta);
7968 *delta = build2 (PLUS_EXPR, TREE_TYPE (*delta),
7969 *delta, integer_one_node);
7970 *delta = fold_if_not_in_template (*delta);
7971 break;
7973 default:
7974 gcc_unreachable ();
7977 *pfn = build_nop (TYPE_PTRMEMFUNC_FN_TYPE (type), *pfn);
7978 *pfn = fold_if_not_in_template (*pfn);
7982 /* Return an expression for PFN from the pointer-to-member function
7983 given by T. */
7985 static tree
7986 pfn_from_ptrmemfunc (tree t)
7988 if (TREE_CODE (t) == PTRMEM_CST)
7990 tree delta;
7991 tree pfn;
7993 expand_ptrmemfunc_cst (t, &delta, &pfn);
7994 if (pfn)
7995 return pfn;
7998 return build_ptrmemfunc_access_expr (t, pfn_identifier);
8001 /* Return an expression for DELTA from the pointer-to-member function
8002 given by T. */
8004 static tree
8005 delta_from_ptrmemfunc (tree t)
8007 if (TREE_CODE (t) == PTRMEM_CST)
8009 tree delta;
8010 tree pfn;
8012 expand_ptrmemfunc_cst (t, &delta, &pfn);
8013 if (delta)
8014 return delta;
8017 return build_ptrmemfunc_access_expr (t, delta_identifier);
8020 /* Convert value RHS to type TYPE as preparation for an assignment to
8021 an lvalue of type TYPE. ERRTYPE indicates what kind of error the
8022 implicit conversion is. If FNDECL is non-NULL, we are doing the
8023 conversion in order to pass the PARMNUMth argument of FNDECL.
8024 If FNDECL is NULL, we are doing the conversion in function pointer
8025 argument passing, conversion in initialization, etc. */
8027 static tree
8028 convert_for_assignment (tree type, tree rhs,
8029 impl_conv_rhs errtype, tree fndecl, int parmnum,
8030 tsubst_flags_t complain, int flags)
8032 tree rhstype;
8033 enum tree_code coder;
8035 /* Strip NON_LVALUE_EXPRs since we aren't using as an lvalue. */
8036 if (TREE_CODE (rhs) == NON_LVALUE_EXPR)
8037 rhs = TREE_OPERAND (rhs, 0);
8039 rhstype = TREE_TYPE (rhs);
8040 coder = TREE_CODE (rhstype);
8042 if (TREE_CODE (type) == VECTOR_TYPE && coder == VECTOR_TYPE
8043 && vector_types_convertible_p (type, rhstype, true))
8045 rhs = mark_rvalue_use (rhs);
8046 return convert (type, rhs);
8049 if (rhs == error_mark_node || rhstype == error_mark_node)
8050 return error_mark_node;
8051 if (TREE_CODE (rhs) == TREE_LIST && TREE_VALUE (rhs) == error_mark_node)
8052 return error_mark_node;
8054 /* The RHS of an assignment cannot have void type. */
8055 if (coder == VOID_TYPE)
8057 if (complain & tf_error)
8058 error ("void value not ignored as it ought to be");
8059 return error_mark_node;
8062 if (c_dialect_objc ())
8064 int parmno;
8065 tree selector;
8066 tree rname = fndecl;
8068 switch (errtype)
8070 case ICR_ASSIGN:
8071 parmno = -1;
8072 break;
8073 case ICR_INIT:
8074 parmno = -2;
8075 break;
8076 default:
8077 selector = objc_message_selector ();
8078 parmno = parmnum;
8079 if (selector && parmno > 1)
8081 rname = selector;
8082 parmno -= 1;
8086 if (objc_compare_types (type, rhstype, parmno, rname))
8088 rhs = mark_rvalue_use (rhs);
8089 return convert (type, rhs);
8093 /* [expr.ass]
8095 The expression is implicitly converted (clause _conv_) to the
8096 cv-unqualified type of the left operand.
8098 We allow bad conversions here because by the time we get to this point
8099 we are committed to doing the conversion. If we end up doing a bad
8100 conversion, convert_like will complain. */
8101 if (!can_convert_arg_bad (type, rhstype, rhs, flags, complain))
8103 /* When -Wno-pmf-conversions is use, we just silently allow
8104 conversions from pointers-to-members to plain pointers. If
8105 the conversion doesn't work, cp_convert will complain. */
8106 if (!warn_pmf2ptr
8107 && TYPE_PTR_P (type)
8108 && TYPE_PTRMEMFUNC_P (rhstype))
8109 rhs = cp_convert (strip_top_quals (type), rhs, complain);
8110 else
8112 if (complain & tf_error)
8114 /* If the right-hand side has unknown type, then it is an
8115 overloaded function. Call instantiate_type to get error
8116 messages. */
8117 if (rhstype == unknown_type_node)
8118 instantiate_type (type, rhs, tf_warning_or_error);
8119 else if (fndecl)
8120 error ("cannot convert %qT to %qT for argument %qP to %qD",
8121 rhstype, type, parmnum, fndecl);
8122 else
8123 switch (errtype)
8125 case ICR_DEFAULT_ARGUMENT:
8126 error ("cannot convert %qT to %qT in default argument",
8127 rhstype, type);
8128 break;
8129 case ICR_ARGPASS:
8130 error ("cannot convert %qT to %qT in argument passing",
8131 rhstype, type);
8132 break;
8133 case ICR_CONVERTING:
8134 error ("cannot convert %qT to %qT",
8135 rhstype, type);
8136 break;
8137 case ICR_INIT:
8138 error ("cannot convert %qT to %qT in initialization",
8139 rhstype, type);
8140 break;
8141 case ICR_RETURN:
8142 error ("cannot convert %qT to %qT in return",
8143 rhstype, type);
8144 break;
8145 case ICR_ASSIGN:
8146 error ("cannot convert %qT to %qT in assignment",
8147 rhstype, type);
8148 break;
8149 default:
8150 gcc_unreachable();
8152 if (TYPE_PTR_P (rhstype)
8153 && TYPE_PTR_P (type)
8154 && CLASS_TYPE_P (TREE_TYPE (rhstype))
8155 && CLASS_TYPE_P (TREE_TYPE (type))
8156 && !COMPLETE_TYPE_P (TREE_TYPE (rhstype)))
8157 inform (DECL_SOURCE_LOCATION (TYPE_MAIN_DECL
8158 (TREE_TYPE (rhstype))),
8159 "class type %qT is incomplete", TREE_TYPE (rhstype));
8161 return error_mark_node;
8164 if (warn_suggest_attribute_format)
8166 const enum tree_code codel = TREE_CODE (type);
8167 if ((codel == POINTER_TYPE || codel == REFERENCE_TYPE)
8168 && coder == codel
8169 && check_missing_format_attribute (type, rhstype)
8170 && (complain & tf_warning))
8171 switch (errtype)
8173 case ICR_ARGPASS:
8174 case ICR_DEFAULT_ARGUMENT:
8175 if (fndecl)
8176 warning (OPT_Wsuggest_attribute_format,
8177 "parameter %qP of %qD might be a candidate "
8178 "for a format attribute", parmnum, fndecl);
8179 else
8180 warning (OPT_Wsuggest_attribute_format,
8181 "parameter might be a candidate "
8182 "for a format attribute");
8183 break;
8184 case ICR_CONVERTING:
8185 warning (OPT_Wsuggest_attribute_format,
8186 "target of conversion might be a candidate "
8187 "for a format attribute");
8188 break;
8189 case ICR_INIT:
8190 warning (OPT_Wsuggest_attribute_format,
8191 "target of initialization might be a candidate "
8192 "for a format attribute");
8193 break;
8194 case ICR_RETURN:
8195 warning (OPT_Wsuggest_attribute_format,
8196 "return type might be a candidate "
8197 "for a format attribute");
8198 break;
8199 case ICR_ASSIGN:
8200 warning (OPT_Wsuggest_attribute_format,
8201 "left-hand side of assignment might be a candidate "
8202 "for a format attribute");
8203 break;
8204 default:
8205 gcc_unreachable();
8209 /* If -Wparentheses, warn about a = b = c when a has type bool and b
8210 does not. */
8211 if (warn_parentheses
8212 && TREE_CODE (type) == BOOLEAN_TYPE
8213 && TREE_CODE (rhs) == MODIFY_EXPR
8214 && !TREE_NO_WARNING (rhs)
8215 && TREE_CODE (TREE_TYPE (rhs)) != BOOLEAN_TYPE
8216 && (complain & tf_warning))
8218 location_t loc = EXPR_LOC_OR_LOC (rhs, input_location);
8220 warning_at (loc, OPT_Wparentheses,
8221 "suggest parentheses around assignment used as truth value");
8222 TREE_NO_WARNING (rhs) = 1;
8225 return perform_implicit_conversion_flags (strip_top_quals (type), rhs,
8226 complain, flags);
8229 /* Convert RHS to be of type TYPE.
8230 If EXP is nonzero, it is the target of the initialization.
8231 ERRTYPE indicates what kind of error the implicit conversion is.
8233 Two major differences between the behavior of
8234 `convert_for_assignment' and `convert_for_initialization'
8235 are that references are bashed in the former, while
8236 copied in the latter, and aggregates are assigned in
8237 the former (operator=) while initialized in the
8238 latter (X(X&)).
8240 If using constructor make sure no conversion operator exists, if one does
8241 exist, an ambiguity exists. */
8243 tree
8244 convert_for_initialization (tree exp, tree type, tree rhs, int flags,
8245 impl_conv_rhs errtype, tree fndecl, int parmnum,
8246 tsubst_flags_t complain)
8248 enum tree_code codel = TREE_CODE (type);
8249 tree rhstype;
8250 enum tree_code coder;
8252 /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
8253 Strip such NOP_EXPRs, since RHS is used in non-lvalue context. */
8254 if (TREE_CODE (rhs) == NOP_EXPR
8255 && TREE_TYPE (rhs) == TREE_TYPE (TREE_OPERAND (rhs, 0))
8256 && codel != REFERENCE_TYPE)
8257 rhs = TREE_OPERAND (rhs, 0);
8259 if (type == error_mark_node
8260 || rhs == error_mark_node
8261 || (TREE_CODE (rhs) == TREE_LIST && TREE_VALUE (rhs) == error_mark_node))
8262 return error_mark_node;
8264 if ((TREE_CODE (TREE_TYPE (rhs)) == ARRAY_TYPE
8265 && TREE_CODE (type) != ARRAY_TYPE
8266 && (TREE_CODE (type) != REFERENCE_TYPE
8267 || TREE_CODE (TREE_TYPE (type)) != ARRAY_TYPE))
8268 || (TREE_CODE (TREE_TYPE (rhs)) == FUNCTION_TYPE
8269 && !TYPE_REFFN_P (type))
8270 || TREE_CODE (TREE_TYPE (rhs)) == METHOD_TYPE)
8271 rhs = decay_conversion (rhs, complain);
8273 rhstype = TREE_TYPE (rhs);
8274 coder = TREE_CODE (rhstype);
8276 if (coder == ERROR_MARK)
8277 return error_mark_node;
8279 /* We accept references to incomplete types, so we can
8280 return here before checking if RHS is of complete type. */
8282 if (codel == REFERENCE_TYPE)
8284 /* This should eventually happen in convert_arguments. */
8285 int savew = 0, savee = 0;
8287 if (fndecl)
8288 savew = warningcount + werrorcount, savee = errorcount;
8289 rhs = initialize_reference (type, rhs, flags, complain);
8291 if (fndecl
8292 && (warningcount + werrorcount > savew || errorcount > savee))
8293 inform (input_location,
8294 "in passing argument %P of %q+D", parmnum, fndecl);
8296 return rhs;
8299 if (exp != 0)
8300 exp = require_complete_type_sfinae (exp, complain);
8301 if (exp == error_mark_node)
8302 return error_mark_node;
8304 rhstype = non_reference (rhstype);
8306 type = complete_type (type);
8308 if (DIRECT_INIT_EXPR_P (type, rhs))
8309 /* Don't try to do copy-initialization if we already have
8310 direct-initialization. */
8311 return rhs;
8313 if (MAYBE_CLASS_TYPE_P (type))
8314 return perform_implicit_conversion_flags (type, rhs, complain, flags);
8316 return convert_for_assignment (type, rhs, errtype, fndecl, parmnum,
8317 complain, flags);
8320 /* If RETVAL is the address of, or a reference to, a local variable or
8321 temporary give an appropriate warning and return true. */
8323 static bool
8324 maybe_warn_about_returning_address_of_local (tree retval)
8326 tree valtype = TREE_TYPE (DECL_RESULT (current_function_decl));
8327 tree whats_returned = retval;
8329 for (;;)
8331 if (TREE_CODE (whats_returned) == COMPOUND_EXPR)
8332 whats_returned = TREE_OPERAND (whats_returned, 1);
8333 else if (CONVERT_EXPR_P (whats_returned)
8334 || TREE_CODE (whats_returned) == NON_LVALUE_EXPR)
8335 whats_returned = TREE_OPERAND (whats_returned, 0);
8336 else
8337 break;
8340 if (TREE_CODE (whats_returned) != ADDR_EXPR)
8341 return false;
8342 whats_returned = TREE_OPERAND (whats_returned, 0);
8344 while (TREE_CODE (whats_returned) == COMPONENT_REF
8345 || TREE_CODE (whats_returned) == ARRAY_REF)
8346 whats_returned = TREE_OPERAND (whats_returned, 0);
8348 if (TREE_CODE (valtype) == REFERENCE_TYPE)
8350 if (TREE_CODE (whats_returned) == AGGR_INIT_EXPR
8351 || TREE_CODE (whats_returned) == TARGET_EXPR)
8353 warning (OPT_Wreturn_local_addr, "returning reference to temporary");
8354 return true;
8356 if (VAR_P (whats_returned)
8357 && DECL_NAME (whats_returned)
8358 && TEMP_NAME_P (DECL_NAME (whats_returned)))
8360 warning (OPT_Wreturn_local_addr, "reference to non-lvalue returned");
8361 return true;
8365 if (DECL_P (whats_returned)
8366 && DECL_NAME (whats_returned)
8367 && DECL_FUNCTION_SCOPE_P (whats_returned)
8368 && !is_capture_proxy (whats_returned)
8369 && !(TREE_STATIC (whats_returned)
8370 || TREE_PUBLIC (whats_returned)))
8372 if (TREE_CODE (valtype) == REFERENCE_TYPE)
8373 warning (OPT_Wreturn_local_addr, "reference to local variable %q+D returned",
8374 whats_returned);
8375 else if (TREE_CODE (whats_returned) == LABEL_DECL)
8376 warning (OPT_Wreturn_local_addr, "address of label %q+D returned",
8377 whats_returned);
8378 else
8379 warning (OPT_Wreturn_local_addr, "address of local variable %q+D "
8380 "returned", whats_returned);
8381 return true;
8384 return false;
8387 /* Check that returning RETVAL from the current function is valid.
8388 Return an expression explicitly showing all conversions required to
8389 change RETVAL into the function return type, and to assign it to
8390 the DECL_RESULT for the function. Set *NO_WARNING to true if
8391 code reaches end of non-void function warning shouldn't be issued
8392 on this RETURN_EXPR. */
8394 tree
8395 check_return_expr (tree retval, bool *no_warning)
8397 tree result;
8398 /* The type actually returned by the function. */
8399 tree valtype;
8400 /* The type the function is declared to return, or void if
8401 the declared type is incomplete. */
8402 tree functype;
8403 int fn_returns_value_p;
8404 bool named_return_value_okay_p;
8406 *no_warning = false;
8408 if (flag_cilkplus && retval && contains_cilk_spawn_stmt (retval))
8410 error_at (EXPR_LOCATION (retval), "use of %<_Cilk_spawn%> in a return "
8411 "statement is not allowed");
8412 return NULL_TREE;
8415 /* A `volatile' function is one that isn't supposed to return, ever.
8416 (This is a G++ extension, used to get better code for functions
8417 that call the `volatile' function.) */
8418 if (TREE_THIS_VOLATILE (current_function_decl))
8419 warning (0, "function declared %<noreturn%> has a %<return%> statement");
8421 /* Check for various simple errors. */
8422 if (DECL_DESTRUCTOR_P (current_function_decl))
8424 if (retval)
8425 error ("returning a value from a destructor");
8426 return NULL_TREE;
8428 else if (DECL_CONSTRUCTOR_P (current_function_decl))
8430 if (in_function_try_handler)
8431 /* If a return statement appears in a handler of the
8432 function-try-block of a constructor, the program is ill-formed. */
8433 error ("cannot return from a handler of a function-try-block of a constructor");
8434 else if (retval)
8435 /* You can't return a value from a constructor. */
8436 error ("returning a value from a constructor");
8437 return NULL_TREE;
8440 if (processing_template_decl)
8442 current_function_returns_value = 1;
8443 if (check_for_bare_parameter_packs (retval))
8444 retval = error_mark_node;
8445 return retval;
8448 functype = TREE_TYPE (TREE_TYPE (current_function_decl));
8450 /* Deduce auto return type from a return statement. */
8451 if (current_function_auto_return_pattern)
8453 tree auto_node;
8454 tree type;
8456 if (!retval && !is_auto (current_function_auto_return_pattern))
8458 /* Give a helpful error message. */
8459 error ("return-statement with no value, in function returning %qT",
8460 current_function_auto_return_pattern);
8461 inform (input_location, "only plain %<auto%> return type can be "
8462 "deduced to %<void%>");
8463 type = error_mark_node;
8465 else if (retval && BRACE_ENCLOSED_INITIALIZER_P (retval))
8467 error ("returning initializer list");
8468 type = error_mark_node;
8470 else
8472 if (!retval)
8473 retval = void_node;
8474 auto_node = type_uses_auto (current_function_auto_return_pattern);
8475 type = do_auto_deduction (current_function_auto_return_pattern,
8476 retval, auto_node);
8479 if (type == error_mark_node)
8480 /* Leave it. */;
8481 else if (functype == current_function_auto_return_pattern)
8482 apply_deduced_return_type (current_function_decl, type);
8483 else
8484 /* A mismatch should have been diagnosed in do_auto_deduction. */
8485 gcc_assert (same_type_p (type, functype));
8486 functype = type;
8489 /* When no explicit return-value is given in a function with a named
8490 return value, the named return value is used. */
8491 result = DECL_RESULT (current_function_decl);
8492 valtype = TREE_TYPE (result);
8493 gcc_assert (valtype != NULL_TREE);
8494 fn_returns_value_p = !VOID_TYPE_P (valtype);
8495 if (!retval && DECL_NAME (result) && fn_returns_value_p)
8496 retval = result;
8498 /* Check for a return statement with no return value in a function
8499 that's supposed to return a value. */
8500 if (!retval && fn_returns_value_p)
8502 if (functype != error_mark_node)
8503 permerror (input_location, "return-statement with no value, in "
8504 "function returning %qT", valtype);
8505 /* Remember that this function did return. */
8506 current_function_returns_value = 1;
8507 /* And signal caller that TREE_NO_WARNING should be set on the
8508 RETURN_EXPR to avoid control reaches end of non-void function
8509 warnings in tree-cfg.c. */
8510 *no_warning = true;
8512 /* Check for a return statement with a value in a function that
8513 isn't supposed to return a value. */
8514 else if (retval && !fn_returns_value_p)
8516 if (VOID_TYPE_P (TREE_TYPE (retval)))
8517 /* You can return a `void' value from a function of `void'
8518 type. In that case, we have to evaluate the expression for
8519 its side-effects. */
8520 finish_expr_stmt (retval);
8521 else
8522 permerror (input_location, "return-statement with a value, in function "
8523 "returning 'void'");
8524 current_function_returns_null = 1;
8526 /* There's really no value to return, after all. */
8527 return NULL_TREE;
8529 else if (!retval)
8530 /* Remember that this function can sometimes return without a
8531 value. */
8532 current_function_returns_null = 1;
8533 else
8534 /* Remember that this function did return a value. */
8535 current_function_returns_value = 1;
8537 /* Check for erroneous operands -- but after giving ourselves a
8538 chance to provide an error about returning a value from a void
8539 function. */
8540 if (error_operand_p (retval))
8542 current_function_return_value = error_mark_node;
8543 return error_mark_node;
8546 /* Only operator new(...) throw(), can return NULL [expr.new/13]. */
8547 if ((DECL_OVERLOADED_OPERATOR_P (current_function_decl) == NEW_EXPR
8548 || DECL_OVERLOADED_OPERATOR_P (current_function_decl) == VEC_NEW_EXPR)
8549 && !TYPE_NOTHROW_P (TREE_TYPE (current_function_decl))
8550 && ! flag_check_new
8551 && retval && null_ptr_cst_p (retval))
8552 warning (0, "%<operator new%> must not return NULL unless it is "
8553 "declared %<throw()%> (or -fcheck-new is in effect)");
8555 /* Effective C++ rule 15. See also start_function. */
8556 if (warn_ecpp
8557 && DECL_NAME (current_function_decl) == ansi_assopname(NOP_EXPR))
8559 bool warn = true;
8561 /* The function return type must be a reference to the current
8562 class. */
8563 if (TREE_CODE (valtype) == REFERENCE_TYPE
8564 && same_type_ignoring_top_level_qualifiers_p
8565 (TREE_TYPE (valtype), TREE_TYPE (current_class_ref)))
8567 /* Returning '*this' is obviously OK. */
8568 if (retval == current_class_ref)
8569 warn = false;
8570 /* If we are calling a function whose return type is the same of
8571 the current class reference, it is ok. */
8572 else if (INDIRECT_REF_P (retval)
8573 && TREE_CODE (TREE_OPERAND (retval, 0)) == CALL_EXPR)
8574 warn = false;
8577 if (warn)
8578 warning (OPT_Weffc__, "%<operator=%> should return a reference to %<*this%>");
8581 /* The fabled Named Return Value optimization, as per [class.copy]/15:
8583 [...] For a function with a class return type, if the expression
8584 in the return statement is the name of a local object, and the cv-
8585 unqualified type of the local object is the same as the function
8586 return type, an implementation is permitted to omit creating the tem-
8587 porary object to hold the function return value [...]
8589 So, if this is a value-returning function that always returns the same
8590 local variable, remember it.
8592 It might be nice to be more flexible, and choose the first suitable
8593 variable even if the function sometimes returns something else, but
8594 then we run the risk of clobbering the variable we chose if the other
8595 returned expression uses the chosen variable somehow. And people expect
8596 this restriction, anyway. (jason 2000-11-19)
8598 See finish_function and finalize_nrv for the rest of this optimization. */
8600 named_return_value_okay_p =
8601 (retval != NULL_TREE
8602 /* Must be a local, automatic variable. */
8603 && VAR_P (retval)
8604 && DECL_CONTEXT (retval) == current_function_decl
8605 && ! TREE_STATIC (retval)
8606 /* And not a lambda or anonymous union proxy. */
8607 && !DECL_HAS_VALUE_EXPR_P (retval)
8608 && (DECL_ALIGN (retval) <= DECL_ALIGN (result))
8609 /* The cv-unqualified type of the returned value must be the
8610 same as the cv-unqualified return type of the
8611 function. */
8612 && same_type_p ((TYPE_MAIN_VARIANT (TREE_TYPE (retval))),
8613 (TYPE_MAIN_VARIANT (functype)))
8614 /* And the returned value must be non-volatile. */
8615 && ! TYPE_VOLATILE (TREE_TYPE (retval)));
8617 if (fn_returns_value_p && flag_elide_constructors)
8619 if (named_return_value_okay_p
8620 && (current_function_return_value == NULL_TREE
8621 || current_function_return_value == retval))
8622 current_function_return_value = retval;
8623 else
8624 current_function_return_value = error_mark_node;
8627 /* We don't need to do any conversions when there's nothing being
8628 returned. */
8629 if (!retval)
8630 return NULL_TREE;
8632 /* Do any required conversions. */
8633 if (retval == result || DECL_CONSTRUCTOR_P (current_function_decl))
8634 /* No conversions are required. */
8636 else
8638 int flags = LOOKUP_NORMAL | LOOKUP_ONLYCONVERTING;
8640 /* The functype's return type will have been set to void, if it
8641 was an incomplete type. Just treat this as 'return;' */
8642 if (VOID_TYPE_P (functype))
8643 return error_mark_node;
8645 /* If we had an id-expression obfuscated by force_paren_expr, we need
8646 to undo it so we can try to treat it as an rvalue below. */
8647 if (cxx_dialect >= cxx14
8648 && INDIRECT_REF_P (retval)
8649 && REF_PARENTHESIZED_P (retval))
8651 retval = TREE_OPERAND (retval, 0);
8652 while (TREE_CODE (retval) == NON_LVALUE_EXPR
8653 || TREE_CODE (retval) == NOP_EXPR)
8654 retval = TREE_OPERAND (retval, 0);
8655 gcc_assert (TREE_CODE (retval) == ADDR_EXPR);
8656 retval = TREE_OPERAND (retval, 0);
8659 /* Under C++11 [12.8/32 class.copy], a returned lvalue is sometimes
8660 treated as an rvalue for the purposes of overload resolution to
8661 favor move constructors over copy constructors.
8663 Note that these conditions are similar to, but not as strict as,
8664 the conditions for the named return value optimization. */
8665 if ((cxx_dialect != cxx98)
8666 && ((VAR_P (retval) && !DECL_HAS_VALUE_EXPR_P (retval))
8667 || TREE_CODE (retval) == PARM_DECL)
8668 && DECL_CONTEXT (retval) == current_function_decl
8669 && !TREE_STATIC (retval)
8670 /* This is only interesting for class type. */
8671 && CLASS_TYPE_P (functype))
8672 flags = flags | LOOKUP_PREFER_RVALUE;
8674 /* First convert the value to the function's return type, then
8675 to the type of return value's location to handle the
8676 case that functype is smaller than the valtype. */
8677 retval = convert_for_initialization
8678 (NULL_TREE, functype, retval, flags, ICR_RETURN, NULL_TREE, 0,
8679 tf_warning_or_error);
8680 retval = convert (valtype, retval);
8682 /* If the conversion failed, treat this just like `return;'. */
8683 if (retval == error_mark_node)
8684 return retval;
8685 /* We can't initialize a register from a AGGR_INIT_EXPR. */
8686 else if (! cfun->returns_struct
8687 && TREE_CODE (retval) == TARGET_EXPR
8688 && TREE_CODE (TREE_OPERAND (retval, 1)) == AGGR_INIT_EXPR)
8689 retval = build2 (COMPOUND_EXPR, TREE_TYPE (retval), retval,
8690 TREE_OPERAND (retval, 0));
8691 else if (maybe_warn_about_returning_address_of_local (retval))
8692 retval = build2 (COMPOUND_EXPR, TREE_TYPE (retval), retval,
8693 build_zero_cst (TREE_TYPE (retval)));
8696 /* Actually copy the value returned into the appropriate location. */
8697 if (retval && retval != result)
8698 retval = build2 (INIT_EXPR, TREE_TYPE (result), result, retval);
8700 return retval;
8704 /* Returns nonzero if the pointer-type FROM can be converted to the
8705 pointer-type TO via a qualification conversion. If CONSTP is -1,
8706 then we return nonzero if the pointers are similar, and the
8707 cv-qualification signature of FROM is a proper subset of that of TO.
8709 If CONSTP is positive, then all outer pointers have been
8710 const-qualified. */
8712 static int
8713 comp_ptr_ttypes_real (tree to, tree from, int constp)
8715 bool to_more_cv_qualified = false;
8716 bool is_opaque_pointer = false;
8718 for (; ; to = TREE_TYPE (to), from = TREE_TYPE (from))
8720 if (TREE_CODE (to) != TREE_CODE (from))
8721 return 0;
8723 if (TREE_CODE (from) == OFFSET_TYPE
8724 && !same_type_p (TYPE_OFFSET_BASETYPE (from),
8725 TYPE_OFFSET_BASETYPE (to)))
8726 return 0;
8728 /* Const and volatile mean something different for function types,
8729 so the usual checks are not appropriate. */
8730 if (TREE_CODE (to) != FUNCTION_TYPE && TREE_CODE (to) != METHOD_TYPE)
8732 if (!at_least_as_qualified_p (to, from))
8733 return 0;
8735 if (!at_least_as_qualified_p (from, to))
8737 if (constp == 0)
8738 return 0;
8739 to_more_cv_qualified = true;
8742 if (constp > 0)
8743 constp &= TYPE_READONLY (to);
8746 if (TREE_CODE (to) == VECTOR_TYPE)
8747 is_opaque_pointer = vector_targets_convertible_p (to, from);
8749 if (!TYPE_PTR_P (to) && !TYPE_PTRDATAMEM_P (to))
8750 return ((constp >= 0 || to_more_cv_qualified)
8751 && (is_opaque_pointer
8752 || same_type_ignoring_top_level_qualifiers_p (to, from)));
8756 /* When comparing, say, char ** to char const **, this function takes
8757 the 'char *' and 'char const *'. Do not pass non-pointer/reference
8758 types to this function. */
8761 comp_ptr_ttypes (tree to, tree from)
8763 return comp_ptr_ttypes_real (to, from, 1);
8766 /* Returns true iff FNTYPE is a non-class type that involves
8767 error_mark_node. We can get FUNCTION_TYPE with buried error_mark_node
8768 if a parameter type is ill-formed. */
8770 bool
8771 error_type_p (const_tree type)
8773 tree t;
8775 switch (TREE_CODE (type))
8777 case ERROR_MARK:
8778 return true;
8780 case POINTER_TYPE:
8781 case REFERENCE_TYPE:
8782 case OFFSET_TYPE:
8783 return error_type_p (TREE_TYPE (type));
8785 case FUNCTION_TYPE:
8786 case METHOD_TYPE:
8787 if (error_type_p (TREE_TYPE (type)))
8788 return true;
8789 for (t = TYPE_ARG_TYPES (type); t; t = TREE_CHAIN (t))
8790 if (error_type_p (TREE_VALUE (t)))
8791 return true;
8792 return false;
8794 case RECORD_TYPE:
8795 if (TYPE_PTRMEMFUNC_P (type))
8796 return error_type_p (TYPE_PTRMEMFUNC_FN_TYPE (type));
8797 return false;
8799 default:
8800 return false;
8804 /* Returns true if to and from are (possibly multi-level) pointers to the same
8805 type or inheritance-related types, regardless of cv-quals. */
8807 bool
8808 ptr_reasonably_similar (const_tree to, const_tree from)
8810 for (; ; to = TREE_TYPE (to), from = TREE_TYPE (from))
8812 /* Any target type is similar enough to void. */
8813 if (VOID_TYPE_P (to))
8814 return !error_type_p (from);
8815 if (VOID_TYPE_P (from))
8816 return !error_type_p (to);
8818 if (TREE_CODE (to) != TREE_CODE (from))
8819 return false;
8821 if (TREE_CODE (from) == OFFSET_TYPE
8822 && comptypes (TYPE_OFFSET_BASETYPE (to),
8823 TYPE_OFFSET_BASETYPE (from),
8824 COMPARE_BASE | COMPARE_DERIVED))
8825 continue;
8827 if (TREE_CODE (to) == VECTOR_TYPE
8828 && vector_types_convertible_p (to, from, false))
8829 return true;
8831 if (TREE_CODE (to) == INTEGER_TYPE
8832 && TYPE_PRECISION (to) == TYPE_PRECISION (from))
8833 return true;
8835 if (TREE_CODE (to) == FUNCTION_TYPE)
8836 return !error_type_p (to) && !error_type_p (from);
8838 if (!TYPE_PTR_P (to))
8840 /* When either type is incomplete avoid DERIVED_FROM_P,
8841 which may call complete_type (c++/57942). */
8842 bool b = !COMPLETE_TYPE_P (to) || !COMPLETE_TYPE_P (from);
8843 return comptypes
8844 (TYPE_MAIN_VARIANT (to), TYPE_MAIN_VARIANT (from),
8845 b ? COMPARE_STRICT : COMPARE_BASE | COMPARE_DERIVED);
8850 /* Return true if TO and FROM (both of which are POINTER_TYPEs or
8851 pointer-to-member types) are the same, ignoring cv-qualification at
8852 all levels. */
8854 bool
8855 comp_ptr_ttypes_const (tree to, tree from)
8857 bool is_opaque_pointer = false;
8859 for (; ; to = TREE_TYPE (to), from = TREE_TYPE (from))
8861 if (TREE_CODE (to) != TREE_CODE (from))
8862 return false;
8864 if (TREE_CODE (from) == OFFSET_TYPE
8865 && same_type_p (TYPE_OFFSET_BASETYPE (from),
8866 TYPE_OFFSET_BASETYPE (to)))
8867 continue;
8869 if (TREE_CODE (to) == VECTOR_TYPE)
8870 is_opaque_pointer = vector_targets_convertible_p (to, from);
8872 if (!TYPE_PTR_P (to))
8873 return (is_opaque_pointer
8874 || same_type_ignoring_top_level_qualifiers_p (to, from));
8878 /* Returns the type qualifiers for this type, including the qualifiers on the
8879 elements for an array type. */
8882 cp_type_quals (const_tree type)
8884 int quals;
8885 /* This CONST_CAST is okay because strip_array_types returns its
8886 argument unmodified and we assign it to a const_tree. */
8887 type = strip_array_types (CONST_CAST_TREE (type));
8888 if (type == error_mark_node
8889 /* Quals on a FUNCTION_TYPE are memfn quals. */
8890 || TREE_CODE (type) == FUNCTION_TYPE)
8891 return TYPE_UNQUALIFIED;
8892 quals = TYPE_QUALS (type);
8893 /* METHOD and REFERENCE_TYPEs should never have quals. */
8894 gcc_assert ((TREE_CODE (type) != METHOD_TYPE
8895 && TREE_CODE (type) != REFERENCE_TYPE)
8896 || ((quals & (TYPE_QUAL_CONST|TYPE_QUAL_VOLATILE))
8897 == TYPE_UNQUALIFIED));
8898 return quals;
8901 /* Returns the function-ref-qualifier for TYPE */
8903 cp_ref_qualifier
8904 type_memfn_rqual (const_tree type)
8906 gcc_assert (TREE_CODE (type) == FUNCTION_TYPE
8907 || TREE_CODE (type) == METHOD_TYPE);
8909 if (!FUNCTION_REF_QUALIFIED (type))
8910 return REF_QUAL_NONE;
8911 else if (FUNCTION_RVALUE_QUALIFIED (type))
8912 return REF_QUAL_RVALUE;
8913 else
8914 return REF_QUAL_LVALUE;
8917 /* Returns the function-cv-quals for TYPE, which must be a FUNCTION_TYPE or
8918 METHOD_TYPE. */
8921 type_memfn_quals (const_tree type)
8923 if (TREE_CODE (type) == FUNCTION_TYPE)
8924 return TYPE_QUALS (type);
8925 else if (TREE_CODE (type) == METHOD_TYPE)
8926 return cp_type_quals (class_of_this_parm (type));
8927 else
8928 gcc_unreachable ();
8931 /* Returns the FUNCTION_TYPE TYPE with its function-cv-quals changed to
8932 MEMFN_QUALS and its ref-qualifier to RQUAL. */
8934 tree
8935 apply_memfn_quals (tree type, cp_cv_quals memfn_quals, cp_ref_qualifier rqual)
8937 /* Could handle METHOD_TYPE here if necessary. */
8938 gcc_assert (TREE_CODE (type) == FUNCTION_TYPE);
8939 if (TYPE_QUALS (type) == memfn_quals
8940 && type_memfn_rqual (type) == rqual)
8941 return type;
8943 /* This should really have a different TYPE_MAIN_VARIANT, but that gets
8944 complex. */
8945 tree result = build_qualified_type (type, memfn_quals);
8946 if (tree canon = TYPE_CANONICAL (result))
8947 if (canon != result)
8948 /* check_qualified_type doesn't check the ref-qualifier, so make sure
8949 TYPE_CANONICAL is correct. */
8950 TYPE_CANONICAL (result)
8951 = build_ref_qualified_type (canon, type_memfn_rqual (result));
8952 result = build_exception_variant (result, TYPE_RAISES_EXCEPTIONS (type));
8953 return build_ref_qualified_type (result, rqual);
8956 /* Returns nonzero if TYPE is const or volatile. */
8958 bool
8959 cv_qualified_p (const_tree type)
8961 int quals = cp_type_quals (type);
8962 return (quals & (TYPE_QUAL_CONST|TYPE_QUAL_VOLATILE)) != 0;
8965 /* Returns nonzero if the TYPE contains a mutable member. */
8967 bool
8968 cp_has_mutable_p (const_tree type)
8970 /* This CONST_CAST is okay because strip_array_types returns its
8971 argument unmodified and we assign it to a const_tree. */
8972 type = strip_array_types (CONST_CAST_TREE(type));
8974 return CLASS_TYPE_P (type) && CLASSTYPE_HAS_MUTABLE (type);
8977 /* Set TREE_READONLY and TREE_VOLATILE on DECL as indicated by the
8978 TYPE_QUALS. For a VAR_DECL, this may be an optimistic
8979 approximation. In particular, consider:
8981 int f();
8982 struct S { int i; };
8983 const S s = { f(); }
8985 Here, we will make "s" as TREE_READONLY (because it is declared
8986 "const") -- only to reverse ourselves upon seeing that the
8987 initializer is non-constant. */
8989 void
8990 cp_apply_type_quals_to_decl (int type_quals, tree decl)
8992 tree type = TREE_TYPE (decl);
8994 if (type == error_mark_node)
8995 return;
8997 if (TREE_CODE (decl) == TYPE_DECL)
8998 return;
9000 gcc_assert (!(TREE_CODE (type) == FUNCTION_TYPE
9001 && type_quals != TYPE_UNQUALIFIED));
9003 /* Avoid setting TREE_READONLY incorrectly. */
9004 /* We used to check TYPE_NEEDS_CONSTRUCTING here, but now a constexpr
9005 constructor can produce constant init, so rely on cp_finish_decl to
9006 clear TREE_READONLY if the variable has non-constant init. */
9008 /* If the type has (or might have) a mutable component, that component
9009 might be modified. */
9010 if (TYPE_HAS_MUTABLE_P (type) || !COMPLETE_TYPE_P (type))
9011 type_quals &= ~TYPE_QUAL_CONST;
9013 c_apply_type_quals_to_decl (type_quals, decl);
9016 /* Subroutine of casts_away_constness. Make T1 and T2 point at
9017 exemplar types such that casting T1 to T2 is casting away constness
9018 if and only if there is no implicit conversion from T1 to T2. */
9020 static void
9021 casts_away_constness_r (tree *t1, tree *t2, tsubst_flags_t complain)
9023 int quals1;
9024 int quals2;
9026 /* [expr.const.cast]
9028 For multi-level pointer to members and multi-level mixed pointers
9029 and pointers to members (conv.qual), the "member" aspect of a
9030 pointer to member level is ignored when determining if a const
9031 cv-qualifier has been cast away. */
9032 /* [expr.const.cast]
9034 For two pointer types:
9036 X1 is T1cv1,1 * ... cv1,N * where T1 is not a pointer type
9037 X2 is T2cv2,1 * ... cv2,M * where T2 is not a pointer type
9038 K is min(N,M)
9040 casting from X1 to X2 casts away constness if, for a non-pointer
9041 type T there does not exist an implicit conversion (clause
9042 _conv_) from:
9044 Tcv1,(N-K+1) * cv1,(N-K+2) * ... cv1,N *
9048 Tcv2,(M-K+1) * cv2,(M-K+2) * ... cv2,M *. */
9049 if ((!TYPE_PTR_P (*t1) && !TYPE_PTRDATAMEM_P (*t1))
9050 || (!TYPE_PTR_P (*t2) && !TYPE_PTRDATAMEM_P (*t2)))
9052 *t1 = cp_build_qualified_type (void_type_node,
9053 cp_type_quals (*t1));
9054 *t2 = cp_build_qualified_type (void_type_node,
9055 cp_type_quals (*t2));
9056 return;
9059 quals1 = cp_type_quals (*t1);
9060 quals2 = cp_type_quals (*t2);
9062 if (TYPE_PTRDATAMEM_P (*t1))
9063 *t1 = TYPE_PTRMEM_POINTED_TO_TYPE (*t1);
9064 else
9065 *t1 = TREE_TYPE (*t1);
9066 if (TYPE_PTRDATAMEM_P (*t2))
9067 *t2 = TYPE_PTRMEM_POINTED_TO_TYPE (*t2);
9068 else
9069 *t2 = TREE_TYPE (*t2);
9071 casts_away_constness_r (t1, t2, complain);
9072 *t1 = build_pointer_type (*t1);
9073 *t2 = build_pointer_type (*t2);
9074 *t1 = cp_build_qualified_type (*t1, quals1);
9075 *t2 = cp_build_qualified_type (*t2, quals2);
9078 /* Returns nonzero if casting from TYPE1 to TYPE2 casts away
9079 constness.
9081 ??? This function returns non-zero if casting away qualifiers not
9082 just const. We would like to return to the caller exactly which
9083 qualifiers are casted away to give more accurate diagnostics.
9086 static bool
9087 casts_away_constness (tree t1, tree t2, tsubst_flags_t complain)
9089 if (TREE_CODE (t2) == REFERENCE_TYPE)
9091 /* [expr.const.cast]
9093 Casting from an lvalue of type T1 to an lvalue of type T2
9094 using a reference cast casts away constness if a cast from an
9095 rvalue of type "pointer to T1" to the type "pointer to T2"
9096 casts away constness. */
9097 t1 = (TREE_CODE (t1) == REFERENCE_TYPE ? TREE_TYPE (t1) : t1);
9098 return casts_away_constness (build_pointer_type (t1),
9099 build_pointer_type (TREE_TYPE (t2)),
9100 complain);
9103 if (TYPE_PTRDATAMEM_P (t1) && TYPE_PTRDATAMEM_P (t2))
9104 /* [expr.const.cast]
9106 Casting from an rvalue of type "pointer to data member of X
9107 of type T1" to the type "pointer to data member of Y of type
9108 T2" casts away constness if a cast from an rvalue of type
9109 "pointer to T1" to the type "pointer to T2" casts away
9110 constness. */
9111 return casts_away_constness
9112 (build_pointer_type (TYPE_PTRMEM_POINTED_TO_TYPE (t1)),
9113 build_pointer_type (TYPE_PTRMEM_POINTED_TO_TYPE (t2)),
9114 complain);
9116 /* Casting away constness is only something that makes sense for
9117 pointer or reference types. */
9118 if (!TYPE_PTR_P (t1) || !TYPE_PTR_P (t2))
9119 return false;
9121 /* Top-level qualifiers don't matter. */
9122 t1 = TYPE_MAIN_VARIANT (t1);
9123 t2 = TYPE_MAIN_VARIANT (t2);
9124 casts_away_constness_r (&t1, &t2, complain);
9125 if (!can_convert (t2, t1, complain))
9126 return true;
9128 return false;
9131 /* If T is a REFERENCE_TYPE return the type to which T refers.
9132 Otherwise, return T itself. */
9134 tree
9135 non_reference (tree t)
9137 if (t && TREE_CODE (t) == REFERENCE_TYPE)
9138 t = TREE_TYPE (t);
9139 return t;
9143 /* Return nonzero if REF is an lvalue valid for this language;
9144 otherwise, print an error message and return zero. USE says
9145 how the lvalue is being used and so selects the error message. */
9148 lvalue_or_else (tree ref, enum lvalue_use use, tsubst_flags_t complain)
9150 cp_lvalue_kind kind = lvalue_kind (ref);
9152 if (kind == clk_none)
9154 if (complain & tf_error)
9155 lvalue_error (input_location, use);
9156 return 0;
9158 else if (kind & (clk_rvalueref|clk_class))
9160 if (!(complain & tf_error))
9161 return 0;
9162 if (kind & clk_class)
9163 /* Make this a permerror because we used to accept it. */
9164 permerror (input_location, "using temporary as lvalue");
9165 else
9166 error ("using xvalue (rvalue reference) as lvalue");
9168 return 1;
9171 /* Return true if a user-defined literal operator is a raw operator. */
9173 bool
9174 check_raw_literal_operator (const_tree decl)
9176 tree argtypes = TYPE_ARG_TYPES (TREE_TYPE (decl));
9177 tree argtype;
9178 int arity;
9179 bool maybe_raw_p = false;
9181 /* Count the number and type of arguments and check for ellipsis. */
9182 for (argtype = argtypes, arity = 0;
9183 argtype && argtype != void_list_node;
9184 ++arity, argtype = TREE_CHAIN (argtype))
9186 tree t = TREE_VALUE (argtype);
9188 if (same_type_p (t, const_string_type_node))
9189 maybe_raw_p = true;
9191 if (!argtype)
9192 return false; /* Found ellipsis. */
9194 if (!maybe_raw_p || arity != 1)
9195 return false;
9197 return true;
9201 /* Return true if a user-defined literal operator has one of the allowed
9202 argument types. */
9204 bool
9205 check_literal_operator_args (const_tree decl,
9206 bool *long_long_unsigned_p, bool *long_double_p)
9208 tree argtypes = TYPE_ARG_TYPES (TREE_TYPE (decl));
9210 *long_long_unsigned_p = false;
9211 *long_double_p = false;
9212 if (processing_template_decl || processing_specialization)
9213 return argtypes == void_list_node;
9214 else
9216 tree argtype;
9217 int arity;
9218 int max_arity = 2;
9220 /* Count the number and type of arguments and check for ellipsis. */
9221 for (argtype = argtypes, arity = 0;
9222 argtype && argtype != void_list_node;
9223 argtype = TREE_CHAIN (argtype))
9225 tree t = TREE_VALUE (argtype);
9226 ++arity;
9228 if (TYPE_PTR_P (t))
9230 bool maybe_raw_p = false;
9231 t = TREE_TYPE (t);
9232 if (cp_type_quals (t) != TYPE_QUAL_CONST)
9233 return false;
9234 t = TYPE_MAIN_VARIANT (t);
9235 if ((maybe_raw_p = same_type_p (t, char_type_node))
9236 || same_type_p (t, wchar_type_node)
9237 || same_type_p (t, char16_type_node)
9238 || same_type_p (t, char32_type_node))
9240 argtype = TREE_CHAIN (argtype);
9241 if (!argtype)
9242 return false;
9243 t = TREE_VALUE (argtype);
9244 if (maybe_raw_p && argtype == void_list_node)
9245 return true;
9246 else if (same_type_p (t, size_type_node))
9248 ++arity;
9249 continue;
9251 else
9252 return false;
9255 else if (same_type_p (t, long_long_unsigned_type_node))
9257 max_arity = 1;
9258 *long_long_unsigned_p = true;
9260 else if (same_type_p (t, long_double_type_node))
9262 max_arity = 1;
9263 *long_double_p = true;
9265 else if (same_type_p (t, char_type_node))
9266 max_arity = 1;
9267 else if (same_type_p (t, wchar_type_node))
9268 max_arity = 1;
9269 else if (same_type_p (t, char16_type_node))
9270 max_arity = 1;
9271 else if (same_type_p (t, char32_type_node))
9272 max_arity = 1;
9273 else
9274 return false;
9276 if (!argtype)
9277 return false; /* Found ellipsis. */
9279 if (arity != max_arity)
9280 return false;
9282 return true;