2015-06-23 Paolo Carlini <paolo.carlini@oracle.com>
[official-gcc.git] / gcc / cp / typeck.c
blob5b3fdfbf76b79ee345084e66d47348cdf2cfcf1b
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 "alias.h"
32 #include "symtab.h"
33 #include "tree.h"
34 #include "fold-const.h"
35 #include "stor-layout.h"
36 #include "varasm.h"
37 #include "cp-tree.h"
38 #include "flags.h"
39 #include "diagnostic.h"
40 #include "intl.h"
41 #include "target.h"
42 #include "convert.h"
43 #include "c-family/c-common.h"
44 #include "c-family/c-objc.h"
45 #include "c-family/c-ubsan.h"
46 #include "params.h"
48 static tree cp_build_addr_expr_strict (tree, tsubst_flags_t);
49 static tree cp_build_function_call (tree, tree, tsubst_flags_t);
50 static tree pfn_from_ptrmemfunc (tree);
51 static tree delta_from_ptrmemfunc (tree);
52 static tree convert_for_assignment (tree, tree, impl_conv_rhs, tree, int,
53 tsubst_flags_t, int);
54 static tree cp_pointer_int_sum (enum tree_code, tree, tree, tsubst_flags_t);
55 static tree rationalize_conditional_expr (enum tree_code, tree,
56 tsubst_flags_t);
57 static int comp_ptr_ttypes_real (tree, tree, int);
58 static bool comp_except_types (tree, tree, bool);
59 static bool comp_array_types (const_tree, const_tree, bool);
60 static tree pointer_diff (tree, tree, tree, tsubst_flags_t);
61 static tree get_delta_difference (tree, tree, bool, bool, tsubst_flags_t);
62 static void casts_away_constness_r (tree *, tree *, tsubst_flags_t);
63 static bool casts_away_constness (tree, tree, tsubst_flags_t);
64 static bool maybe_warn_about_returning_address_of_local (tree);
65 static tree lookup_destructor (tree, tree, tree, tsubst_flags_t);
66 static void warn_args_num (location_t, tree, bool);
67 static int convert_arguments (tree, vec<tree, va_gc> **, tree, int,
68 tsubst_flags_t);
70 /* Do `exp = require_complete_type (exp);' to make sure exp
71 does not have an incomplete type. (That includes void types.)
72 Returns error_mark_node if the VALUE does not have
73 complete type when this function returns. */
75 tree
76 require_complete_type_sfinae (tree value, tsubst_flags_t complain)
78 tree type;
80 if (processing_template_decl || value == error_mark_node)
81 return value;
83 if (TREE_CODE (value) == OVERLOAD)
84 type = unknown_type_node;
85 else
86 type = TREE_TYPE (value);
88 if (type == error_mark_node)
89 return error_mark_node;
91 /* First, detect a valid value with a complete type. */
92 if (COMPLETE_TYPE_P (type))
93 return value;
95 if (complete_type_or_maybe_complain (type, value, complain))
96 return value;
97 else
98 return error_mark_node;
101 tree
102 require_complete_type (tree value)
104 return require_complete_type_sfinae (value, tf_warning_or_error);
107 /* Try to complete TYPE, if it is incomplete. For example, if TYPE is
108 a template instantiation, do the instantiation. Returns TYPE,
109 whether or not it could be completed, unless something goes
110 horribly wrong, in which case the error_mark_node is returned. */
112 tree
113 complete_type (tree type)
115 if (type == NULL_TREE)
116 /* Rather than crash, we return something sure to cause an error
117 at some point. */
118 return error_mark_node;
120 if (type == error_mark_node || COMPLETE_TYPE_P (type))
122 else if (TREE_CODE (type) == ARRAY_TYPE && TYPE_DOMAIN (type))
124 tree t = complete_type (TREE_TYPE (type));
125 unsigned int needs_constructing, has_nontrivial_dtor;
126 if (COMPLETE_TYPE_P (t) && !dependent_type_p (type))
127 layout_type (type);
128 needs_constructing
129 = TYPE_NEEDS_CONSTRUCTING (TYPE_MAIN_VARIANT (t));
130 has_nontrivial_dtor
131 = TYPE_HAS_NONTRIVIAL_DESTRUCTOR (TYPE_MAIN_VARIANT (t));
132 for (t = TYPE_MAIN_VARIANT (type); t; t = TYPE_NEXT_VARIANT (t))
134 TYPE_NEEDS_CONSTRUCTING (t) = needs_constructing;
135 TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t) = has_nontrivial_dtor;
138 else if (CLASS_TYPE_P (type) && CLASSTYPE_TEMPLATE_INSTANTIATION (type))
139 instantiate_class_template (TYPE_MAIN_VARIANT (type));
141 return type;
144 /* Like complete_type, but issue an error if the TYPE cannot be completed.
145 VALUE is used for informative diagnostics.
146 Returns NULL_TREE if the type cannot be made complete. */
148 tree
149 complete_type_or_maybe_complain (tree type, tree value, tsubst_flags_t complain)
151 type = complete_type (type);
152 if (type == error_mark_node)
153 /* We already issued an error. */
154 return NULL_TREE;
155 else if (!COMPLETE_TYPE_P (type))
157 if (complain & tf_error)
158 cxx_incomplete_type_diagnostic (value, type, DK_ERROR);
159 return NULL_TREE;
161 else
162 return type;
165 tree
166 complete_type_or_else (tree type, tree value)
168 return complete_type_or_maybe_complain (type, value, tf_warning_or_error);
171 /* Return truthvalue of whether type of EXP is instantiated. */
174 type_unknown_p (const_tree exp)
176 return (TREE_CODE (exp) == TREE_LIST
177 || TREE_TYPE (exp) == unknown_type_node);
181 /* Return the common type of two parameter lists.
182 We assume that comptypes has already been done and returned 1;
183 if that isn't so, this may crash.
185 As an optimization, free the space we allocate if the parameter
186 lists are already common. */
188 static tree
189 commonparms (tree p1, tree p2)
191 tree oldargs = p1, newargs, n;
192 int i, len;
193 int any_change = 0;
195 len = list_length (p1);
196 newargs = tree_last (p1);
198 if (newargs == void_list_node)
199 i = 1;
200 else
202 i = 0;
203 newargs = 0;
206 for (; i < len; i++)
207 newargs = tree_cons (NULL_TREE, NULL_TREE, newargs);
209 n = newargs;
211 for (i = 0; p1;
212 p1 = TREE_CHAIN (p1), p2 = TREE_CHAIN (p2), n = TREE_CHAIN (n), i++)
214 if (TREE_PURPOSE (p1) && !TREE_PURPOSE (p2))
216 TREE_PURPOSE (n) = TREE_PURPOSE (p1);
217 any_change = 1;
219 else if (! TREE_PURPOSE (p1))
221 if (TREE_PURPOSE (p2))
223 TREE_PURPOSE (n) = TREE_PURPOSE (p2);
224 any_change = 1;
227 else
229 if (1 != simple_cst_equal (TREE_PURPOSE (p1), TREE_PURPOSE (p2)))
230 any_change = 1;
231 TREE_PURPOSE (n) = TREE_PURPOSE (p2);
233 if (TREE_VALUE (p1) != TREE_VALUE (p2))
235 any_change = 1;
236 TREE_VALUE (n) = merge_types (TREE_VALUE (p1), TREE_VALUE (p2));
238 else
239 TREE_VALUE (n) = TREE_VALUE (p1);
241 if (! any_change)
242 return oldargs;
244 return newargs;
247 /* Given a type, perhaps copied for a typedef,
248 find the "original" version of it. */
249 static tree
250 original_type (tree t)
252 int quals = cp_type_quals (t);
253 while (t != error_mark_node
254 && TYPE_NAME (t) != NULL_TREE)
256 tree x = TYPE_NAME (t);
257 if (TREE_CODE (x) != TYPE_DECL)
258 break;
259 x = DECL_ORIGINAL_TYPE (x);
260 if (x == NULL_TREE)
261 break;
262 t = x;
264 return cp_build_qualified_type (t, quals);
267 /* Return the common type for two arithmetic types T1 and T2 under the
268 usual arithmetic conversions. The default conversions have already
269 been applied, and enumerated types converted to their compatible
270 integer types. */
272 static tree
273 cp_common_type (tree t1, tree t2)
275 enum tree_code code1 = TREE_CODE (t1);
276 enum tree_code code2 = TREE_CODE (t2);
277 tree attributes;
278 int i;
281 /* In what follows, we slightly generalize the rules given in [expr] so
282 as to deal with `long long' and `complex'. First, merge the
283 attributes. */
284 attributes = (*targetm.merge_type_attributes) (t1, t2);
286 if (SCOPED_ENUM_P (t1) || SCOPED_ENUM_P (t2))
288 if (TYPE_MAIN_VARIANT (t1) == TYPE_MAIN_VARIANT (t2))
289 return build_type_attribute_variant (t1, attributes);
290 else
291 return NULL_TREE;
294 /* FIXME: Attributes. */
295 gcc_assert (ARITHMETIC_TYPE_P (t1)
296 || TREE_CODE (t1) == VECTOR_TYPE
297 || UNSCOPED_ENUM_P (t1));
298 gcc_assert (ARITHMETIC_TYPE_P (t2)
299 || TREE_CODE (t2) == VECTOR_TYPE
300 || UNSCOPED_ENUM_P (t2));
302 /* If one type is complex, form the common type of the non-complex
303 components, then make that complex. Use T1 or T2 if it is the
304 required type. */
305 if (code1 == COMPLEX_TYPE || code2 == COMPLEX_TYPE)
307 tree subtype1 = code1 == COMPLEX_TYPE ? TREE_TYPE (t1) : t1;
308 tree subtype2 = code2 == COMPLEX_TYPE ? TREE_TYPE (t2) : t2;
309 tree subtype
310 = type_after_usual_arithmetic_conversions (subtype1, subtype2);
312 if (code1 == COMPLEX_TYPE && TREE_TYPE (t1) == subtype)
313 return build_type_attribute_variant (t1, attributes);
314 else if (code2 == COMPLEX_TYPE && TREE_TYPE (t2) == subtype)
315 return build_type_attribute_variant (t2, attributes);
316 else
317 return build_type_attribute_variant (build_complex_type (subtype),
318 attributes);
321 if (code1 == VECTOR_TYPE)
323 /* When we get here we should have two vectors of the same size.
324 Just prefer the unsigned one if present. */
325 if (TYPE_UNSIGNED (t1))
326 return build_type_attribute_variant (t1, attributes);
327 else
328 return build_type_attribute_variant (t2, attributes);
331 /* If only one is real, use it as the result. */
332 if (code1 == REAL_TYPE && code2 != REAL_TYPE)
333 return build_type_attribute_variant (t1, attributes);
334 if (code2 == REAL_TYPE && code1 != REAL_TYPE)
335 return build_type_attribute_variant (t2, attributes);
337 /* Both real or both integers; use the one with greater precision. */
338 if (TYPE_PRECISION (t1) > TYPE_PRECISION (t2))
339 return build_type_attribute_variant (t1, attributes);
340 else if (TYPE_PRECISION (t2) > TYPE_PRECISION (t1))
341 return build_type_attribute_variant (t2, attributes);
343 /* The types are the same; no need to do anything fancy. */
344 if (TYPE_MAIN_VARIANT (t1) == TYPE_MAIN_VARIANT (t2))
345 return build_type_attribute_variant (t1, attributes);
347 if (code1 != REAL_TYPE)
349 /* If one is unsigned long long, then convert the other to unsigned
350 long long. */
351 if (same_type_p (TYPE_MAIN_VARIANT (t1), long_long_unsigned_type_node)
352 || same_type_p (TYPE_MAIN_VARIANT (t2), long_long_unsigned_type_node))
353 return build_type_attribute_variant (long_long_unsigned_type_node,
354 attributes);
355 /* If one is a long long, and the other is an unsigned long, and
356 long long can represent all the values of an unsigned long, then
357 convert to a long long. Otherwise, convert to an unsigned long
358 long. Otherwise, if either operand is long long, convert the
359 other to long long.
361 Since we're here, we know the TYPE_PRECISION is the same;
362 therefore converting to long long cannot represent all the values
363 of an unsigned long, so we choose unsigned long long in that
364 case. */
365 if (same_type_p (TYPE_MAIN_VARIANT (t1), long_long_integer_type_node)
366 || same_type_p (TYPE_MAIN_VARIANT (t2), long_long_integer_type_node))
368 tree t = ((TYPE_UNSIGNED (t1) || TYPE_UNSIGNED (t2))
369 ? long_long_unsigned_type_node
370 : long_long_integer_type_node);
371 return build_type_attribute_variant (t, attributes);
374 /* Go through the same procedure, but for longs. */
375 if (same_type_p (TYPE_MAIN_VARIANT (t1), long_unsigned_type_node)
376 || same_type_p (TYPE_MAIN_VARIANT (t2), long_unsigned_type_node))
377 return build_type_attribute_variant (long_unsigned_type_node,
378 attributes);
379 if (same_type_p (TYPE_MAIN_VARIANT (t1), long_integer_type_node)
380 || same_type_p (TYPE_MAIN_VARIANT (t2), long_integer_type_node))
382 tree t = ((TYPE_UNSIGNED (t1) || TYPE_UNSIGNED (t2))
383 ? long_unsigned_type_node : long_integer_type_node);
384 return build_type_attribute_variant (t, attributes);
387 /* For __intN types, either the type is __int128 (and is lower
388 priority than the types checked above, but higher than other
389 128-bit types) or it's known to not be the same size as other
390 types (enforced in toplev.c). Prefer the unsigned type. */
391 for (i = 0; i < NUM_INT_N_ENTS; i ++)
393 if (int_n_enabled_p [i]
394 && (same_type_p (TYPE_MAIN_VARIANT (t1), int_n_trees[i].signed_type)
395 || same_type_p (TYPE_MAIN_VARIANT (t2), int_n_trees[i].signed_type)
396 || same_type_p (TYPE_MAIN_VARIANT (t1), int_n_trees[i].unsigned_type)
397 || same_type_p (TYPE_MAIN_VARIANT (t2), int_n_trees[i].unsigned_type)))
399 tree t = ((TYPE_UNSIGNED (t1) || TYPE_UNSIGNED (t2))
400 ? int_n_trees[i].unsigned_type
401 : int_n_trees[i].signed_type);
402 return build_type_attribute_variant (t, attributes);
406 /* Otherwise prefer the unsigned one. */
407 if (TYPE_UNSIGNED (t1))
408 return build_type_attribute_variant (t1, attributes);
409 else
410 return build_type_attribute_variant (t2, attributes);
412 else
414 if (same_type_p (TYPE_MAIN_VARIANT (t1), long_double_type_node)
415 || same_type_p (TYPE_MAIN_VARIANT (t2), long_double_type_node))
416 return build_type_attribute_variant (long_double_type_node,
417 attributes);
418 if (same_type_p (TYPE_MAIN_VARIANT (t1), double_type_node)
419 || same_type_p (TYPE_MAIN_VARIANT (t2), double_type_node))
420 return build_type_attribute_variant (double_type_node,
421 attributes);
422 if (same_type_p (TYPE_MAIN_VARIANT (t1), float_type_node)
423 || same_type_p (TYPE_MAIN_VARIANT (t2), float_type_node))
424 return build_type_attribute_variant (float_type_node,
425 attributes);
427 /* Two floating-point types whose TYPE_MAIN_VARIANTs are none of
428 the standard C++ floating-point types. Logic earlier in this
429 function has already eliminated the possibility that
430 TYPE_PRECISION (t2) != TYPE_PRECISION (t1), so there's no
431 compelling reason to choose one or the other. */
432 return build_type_attribute_variant (t1, attributes);
436 /* T1 and T2 are arithmetic or enumeration types. Return the type
437 that will result from the "usual arithmetic conversions" on T1 and
438 T2 as described in [expr]. */
440 tree
441 type_after_usual_arithmetic_conversions (tree t1, tree t2)
443 gcc_assert (ARITHMETIC_TYPE_P (t1)
444 || TREE_CODE (t1) == VECTOR_TYPE
445 || UNSCOPED_ENUM_P (t1));
446 gcc_assert (ARITHMETIC_TYPE_P (t2)
447 || TREE_CODE (t2) == VECTOR_TYPE
448 || UNSCOPED_ENUM_P (t2));
450 /* Perform the integral promotions. We do not promote real types here. */
451 if (INTEGRAL_OR_ENUMERATION_TYPE_P (t1)
452 && INTEGRAL_OR_ENUMERATION_TYPE_P (t2))
454 t1 = type_promotes_to (t1);
455 t2 = type_promotes_to (t2);
458 return cp_common_type (t1, t2);
461 static void
462 composite_pointer_error (diagnostic_t kind, tree t1, tree t2,
463 composite_pointer_operation operation)
465 switch (operation)
467 case CPO_COMPARISON:
468 emit_diagnostic (kind, input_location, 0,
469 "comparison between "
470 "distinct pointer types %qT and %qT lacks a cast",
471 t1, t2);
472 break;
473 case CPO_CONVERSION:
474 emit_diagnostic (kind, input_location, 0,
475 "conversion between "
476 "distinct pointer types %qT and %qT lacks a cast",
477 t1, t2);
478 break;
479 case CPO_CONDITIONAL_EXPR:
480 emit_diagnostic (kind, input_location, 0,
481 "conditional expression between "
482 "distinct pointer types %qT and %qT lacks a cast",
483 t1, t2);
484 break;
485 default:
486 gcc_unreachable ();
490 /* Subroutine of composite_pointer_type to implement the recursive
491 case. See that function for documentation of the parameters. */
493 static tree
494 composite_pointer_type_r (tree t1, tree t2,
495 composite_pointer_operation operation,
496 tsubst_flags_t complain)
498 tree pointee1;
499 tree pointee2;
500 tree result_type;
501 tree attributes;
503 /* Determine the types pointed to by T1 and T2. */
504 if (TYPE_PTR_P (t1))
506 pointee1 = TREE_TYPE (t1);
507 pointee2 = TREE_TYPE (t2);
509 else
511 pointee1 = TYPE_PTRMEM_POINTED_TO_TYPE (t1);
512 pointee2 = TYPE_PTRMEM_POINTED_TO_TYPE (t2);
515 /* [expr.rel]
517 Otherwise, the composite pointer type is a pointer type
518 similar (_conv.qual_) to the type of one of the operands,
519 with a cv-qualification signature (_conv.qual_) that is the
520 union of the cv-qualification signatures of the operand
521 types. */
522 if (same_type_ignoring_top_level_qualifiers_p (pointee1, pointee2))
523 result_type = pointee1;
524 else if ((TYPE_PTR_P (pointee1) && TYPE_PTR_P (pointee2))
525 || (TYPE_PTRMEM_P (pointee1) && TYPE_PTRMEM_P (pointee2)))
527 result_type = composite_pointer_type_r (pointee1, pointee2, operation,
528 complain);
529 if (result_type == error_mark_node)
530 return error_mark_node;
532 else
534 if (complain & tf_error)
535 composite_pointer_error (DK_PERMERROR, t1, t2, operation);
536 else
537 return error_mark_node;
538 result_type = void_type_node;
540 result_type = cp_build_qualified_type (result_type,
541 (cp_type_quals (pointee1)
542 | cp_type_quals (pointee2)));
543 /* If the original types were pointers to members, so is the
544 result. */
545 if (TYPE_PTRMEM_P (t1))
547 if (!same_type_p (TYPE_PTRMEM_CLASS_TYPE (t1),
548 TYPE_PTRMEM_CLASS_TYPE (t2)))
550 if (complain & tf_error)
551 composite_pointer_error (DK_PERMERROR, t1, t2, operation);
552 else
553 return error_mark_node;
555 result_type = build_ptrmem_type (TYPE_PTRMEM_CLASS_TYPE (t1),
556 result_type);
558 else
559 result_type = build_pointer_type (result_type);
561 /* Merge the attributes. */
562 attributes = (*targetm.merge_type_attributes) (t1, t2);
563 return build_type_attribute_variant (result_type, attributes);
566 /* Return the composite pointer type (see [expr.rel]) for T1 and T2.
567 ARG1 and ARG2 are the values with those types. The OPERATION is to
568 describe the operation between the pointer types,
569 in case an error occurs.
571 This routine also implements the computation of a common type for
572 pointers-to-members as per [expr.eq]. */
574 tree
575 composite_pointer_type (tree t1, tree t2, tree arg1, tree arg2,
576 composite_pointer_operation operation,
577 tsubst_flags_t complain)
579 tree class1;
580 tree class2;
582 /* [expr.rel]
584 If one operand is a null pointer constant, the composite pointer
585 type is the type of the other operand. */
586 if (null_ptr_cst_p (arg1))
587 return t2;
588 if (null_ptr_cst_p (arg2))
589 return t1;
591 /* We have:
593 [expr.rel]
595 If one of the operands has type "pointer to cv1 void*", then
596 the other has type "pointer to cv2T", and the composite pointer
597 type is "pointer to cv12 void", where cv12 is the union of cv1
598 and cv2.
600 If either type is a pointer to void, make sure it is T1. */
601 if (TYPE_PTR_P (t2) && VOID_TYPE_P (TREE_TYPE (t2)))
602 std::swap (t1, t2);
604 /* Now, if T1 is a pointer to void, merge the qualifiers. */
605 if (TYPE_PTR_P (t1) && VOID_TYPE_P (TREE_TYPE (t1)))
607 tree attributes;
608 tree result_type;
610 if (TYPE_PTRFN_P (t2))
612 if (complain & tf_error)
614 switch (operation)
616 case CPO_COMPARISON:
617 pedwarn (input_location, OPT_Wpedantic,
618 "ISO C++ forbids comparison between pointer "
619 "of type %<void *%> and pointer-to-function");
620 break;
621 case CPO_CONVERSION:
622 pedwarn (input_location, OPT_Wpedantic,
623 "ISO C++ forbids conversion between pointer "
624 "of type %<void *%> and pointer-to-function");
625 break;
626 case CPO_CONDITIONAL_EXPR:
627 pedwarn (input_location, OPT_Wpedantic,
628 "ISO C++ forbids conditional expression between "
629 "pointer of type %<void *%> and "
630 "pointer-to-function");
631 break;
632 default:
633 gcc_unreachable ();
636 else
637 return error_mark_node;
639 result_type
640 = cp_build_qualified_type (void_type_node,
641 (cp_type_quals (TREE_TYPE (t1))
642 | cp_type_quals (TREE_TYPE (t2))));
643 result_type = build_pointer_type (result_type);
644 /* Merge the attributes. */
645 attributes = (*targetm.merge_type_attributes) (t1, t2);
646 return build_type_attribute_variant (result_type, attributes);
649 if (c_dialect_objc () && TYPE_PTR_P (t1)
650 && TYPE_PTR_P (t2))
652 if (objc_have_common_type (t1, t2, -3, NULL_TREE))
653 return objc_common_type (t1, t2);
656 /* [expr.eq] permits the application of a pointer conversion to
657 bring the pointers to a common type. */
658 if (TYPE_PTR_P (t1) && TYPE_PTR_P (t2)
659 && CLASS_TYPE_P (TREE_TYPE (t1))
660 && CLASS_TYPE_P (TREE_TYPE (t2))
661 && !same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (t1),
662 TREE_TYPE (t2)))
664 class1 = TREE_TYPE (t1);
665 class2 = TREE_TYPE (t2);
667 if (DERIVED_FROM_P (class1, class2))
668 t2 = (build_pointer_type
669 (cp_build_qualified_type (class1, cp_type_quals (class2))));
670 else if (DERIVED_FROM_P (class2, class1))
671 t1 = (build_pointer_type
672 (cp_build_qualified_type (class2, cp_type_quals (class1))));
673 else
675 if (complain & tf_error)
676 composite_pointer_error (DK_ERROR, t1, t2, operation);
677 return error_mark_node;
680 /* [expr.eq] permits the application of a pointer-to-member
681 conversion to change the class type of one of the types. */
682 else if (TYPE_PTRMEM_P (t1)
683 && !same_type_p (TYPE_PTRMEM_CLASS_TYPE (t1),
684 TYPE_PTRMEM_CLASS_TYPE (t2)))
686 class1 = TYPE_PTRMEM_CLASS_TYPE (t1);
687 class2 = TYPE_PTRMEM_CLASS_TYPE (t2);
689 if (DERIVED_FROM_P (class1, class2))
690 t1 = build_ptrmem_type (class2, TYPE_PTRMEM_POINTED_TO_TYPE (t1));
691 else if (DERIVED_FROM_P (class2, class1))
692 t2 = build_ptrmem_type (class1, TYPE_PTRMEM_POINTED_TO_TYPE (t2));
693 else
695 if (complain & tf_error)
696 switch (operation)
698 case CPO_COMPARISON:
699 error ("comparison between distinct "
700 "pointer-to-member types %qT and %qT lacks a cast",
701 t1, t2);
702 break;
703 case CPO_CONVERSION:
704 error ("conversion between distinct "
705 "pointer-to-member types %qT and %qT lacks a cast",
706 t1, t2);
707 break;
708 case CPO_CONDITIONAL_EXPR:
709 error ("conditional expression between distinct "
710 "pointer-to-member types %qT and %qT lacks a cast",
711 t1, t2);
712 break;
713 default:
714 gcc_unreachable ();
716 return error_mark_node;
720 return composite_pointer_type_r (t1, t2, operation, complain);
723 /* Return the merged type of two types.
724 We assume that comptypes has already been done and returned 1;
725 if that isn't so, this may crash.
727 This just combines attributes and default arguments; any other
728 differences would cause the two types to compare unalike. */
730 tree
731 merge_types (tree t1, tree t2)
733 enum tree_code code1;
734 enum tree_code code2;
735 tree attributes;
737 /* Save time if the two types are the same. */
738 if (t1 == t2)
739 return t1;
740 if (original_type (t1) == original_type (t2))
741 return t1;
743 /* If one type is nonsense, use the other. */
744 if (t1 == error_mark_node)
745 return t2;
746 if (t2 == error_mark_node)
747 return t1;
749 /* Handle merging an auto redeclaration with a previous deduced
750 return type. */
751 if (is_auto (t1))
752 return t2;
754 /* Merge the attributes. */
755 attributes = (*targetm.merge_type_attributes) (t1, t2);
757 if (TYPE_PTRMEMFUNC_P (t1))
758 t1 = TYPE_PTRMEMFUNC_FN_TYPE (t1);
759 if (TYPE_PTRMEMFUNC_P (t2))
760 t2 = TYPE_PTRMEMFUNC_FN_TYPE (t2);
762 code1 = TREE_CODE (t1);
763 code2 = TREE_CODE (t2);
764 if (code1 != code2)
766 gcc_assert (code1 == TYPENAME_TYPE || code2 == TYPENAME_TYPE);
767 if (code1 == TYPENAME_TYPE)
769 t1 = resolve_typename_type (t1, /*only_current_p=*/true);
770 code1 = TREE_CODE (t1);
772 else
774 t2 = resolve_typename_type (t2, /*only_current_p=*/true);
775 code2 = TREE_CODE (t2);
779 switch (code1)
781 case POINTER_TYPE:
782 case REFERENCE_TYPE:
783 /* For two pointers, do this recursively on the target type. */
785 tree target = merge_types (TREE_TYPE (t1), TREE_TYPE (t2));
786 int quals = cp_type_quals (t1);
788 if (code1 == POINTER_TYPE)
790 t1 = build_pointer_type (target);
791 if (TREE_CODE (target) == METHOD_TYPE)
792 t1 = build_ptrmemfunc_type (t1);
794 else
795 t1 = cp_build_reference_type (target, TYPE_REF_IS_RVALUE (t1));
796 t1 = build_type_attribute_variant (t1, attributes);
797 t1 = cp_build_qualified_type (t1, quals);
799 return t1;
802 case OFFSET_TYPE:
804 int quals;
805 tree pointee;
806 quals = cp_type_quals (t1);
807 pointee = merge_types (TYPE_PTRMEM_POINTED_TO_TYPE (t1),
808 TYPE_PTRMEM_POINTED_TO_TYPE (t2));
809 t1 = build_ptrmem_type (TYPE_PTRMEM_CLASS_TYPE (t1),
810 pointee);
811 t1 = cp_build_qualified_type (t1, quals);
812 break;
815 case ARRAY_TYPE:
817 tree elt = merge_types (TREE_TYPE (t1), TREE_TYPE (t2));
818 /* Save space: see if the result is identical to one of the args. */
819 if (elt == TREE_TYPE (t1) && TYPE_DOMAIN (t1))
820 return build_type_attribute_variant (t1, attributes);
821 if (elt == TREE_TYPE (t2) && TYPE_DOMAIN (t2))
822 return build_type_attribute_variant (t2, attributes);
823 /* Merge the element types, and have a size if either arg has one. */
824 t1 = build_cplus_array_type
825 (elt, TYPE_DOMAIN (TYPE_DOMAIN (t1) ? t1 : t2));
826 break;
829 case FUNCTION_TYPE:
830 /* Function types: prefer the one that specified arg types.
831 If both do, merge the arg types. Also merge the return types. */
833 tree valtype = merge_types (TREE_TYPE (t1), TREE_TYPE (t2));
834 tree p1 = TYPE_ARG_TYPES (t1);
835 tree p2 = TYPE_ARG_TYPES (t2);
836 tree parms;
837 tree rval, raises;
838 bool late_return_type_p = TYPE_HAS_LATE_RETURN_TYPE (t1);
840 /* Save space: see if the result is identical to one of the args. */
841 if (valtype == TREE_TYPE (t1) && ! p2)
842 return cp_build_type_attribute_variant (t1, attributes);
843 if (valtype == TREE_TYPE (t2) && ! p1)
844 return cp_build_type_attribute_variant (t2, attributes);
846 /* Simple way if one arg fails to specify argument types. */
847 if (p1 == NULL_TREE || TREE_VALUE (p1) == void_type_node)
848 parms = p2;
849 else if (p2 == NULL_TREE || TREE_VALUE (p2) == void_type_node)
850 parms = p1;
851 else
852 parms = commonparms (p1, p2);
854 rval = build_function_type (valtype, parms);
855 gcc_assert (type_memfn_quals (t1) == type_memfn_quals (t2));
856 gcc_assert (type_memfn_rqual (t1) == type_memfn_rqual (t2));
857 rval = apply_memfn_quals (rval,
858 type_memfn_quals (t1),
859 type_memfn_rqual (t1));
860 raises = merge_exception_specifiers (TYPE_RAISES_EXCEPTIONS (t1),
861 TYPE_RAISES_EXCEPTIONS (t2));
862 t1 = build_exception_variant (rval, raises);
863 if (late_return_type_p)
864 TYPE_HAS_LATE_RETURN_TYPE (t1) = 1;
865 break;
868 case METHOD_TYPE:
870 /* Get this value the long way, since TYPE_METHOD_BASETYPE
871 is just the main variant of this. */
872 tree basetype = class_of_this_parm (t2);
873 tree raises = merge_exception_specifiers (TYPE_RAISES_EXCEPTIONS (t1),
874 TYPE_RAISES_EXCEPTIONS (t2));
875 cp_ref_qualifier rqual = type_memfn_rqual (t1);
876 tree t3;
877 bool late_return_type_1_p = TYPE_HAS_LATE_RETURN_TYPE (t1);
878 bool late_return_type_2_p = TYPE_HAS_LATE_RETURN_TYPE (t2);
880 /* If this was a member function type, get back to the
881 original type of type member function (i.e., without
882 the class instance variable up front. */
883 t1 = build_function_type (TREE_TYPE (t1),
884 TREE_CHAIN (TYPE_ARG_TYPES (t1)));
885 t2 = build_function_type (TREE_TYPE (t2),
886 TREE_CHAIN (TYPE_ARG_TYPES (t2)));
887 t3 = merge_types (t1, t2);
888 t3 = build_method_type_directly (basetype, TREE_TYPE (t3),
889 TYPE_ARG_TYPES (t3));
890 t1 = build_exception_variant (t3, raises);
891 t1 = build_ref_qualified_type (t1, rqual);
892 if (late_return_type_1_p)
893 TYPE_HAS_LATE_RETURN_TYPE (t1) = 1;
894 if (late_return_type_2_p)
895 TYPE_HAS_LATE_RETURN_TYPE (t2) = 1;
896 break;
899 case TYPENAME_TYPE:
900 /* There is no need to merge attributes into a TYPENAME_TYPE.
901 When the type is instantiated it will have whatever
902 attributes result from the instantiation. */
903 return t1;
905 default:;
908 if (attribute_list_equal (TYPE_ATTRIBUTES (t1), attributes))
909 return t1;
910 else if (attribute_list_equal (TYPE_ATTRIBUTES (t2), attributes))
911 return t2;
912 else
913 return cp_build_type_attribute_variant (t1, attributes);
916 /* Return the ARRAY_TYPE type without its domain. */
918 tree
919 strip_array_domain (tree type)
921 tree t2;
922 gcc_assert (TREE_CODE (type) == ARRAY_TYPE);
923 if (TYPE_DOMAIN (type) == NULL_TREE)
924 return type;
925 t2 = build_cplus_array_type (TREE_TYPE (type), NULL_TREE);
926 return cp_build_type_attribute_variant (t2, TYPE_ATTRIBUTES (type));
929 /* Wrapper around cp_common_type that is used by c-common.c and other
930 front end optimizations that remove promotions.
932 Return the common type for two arithmetic types T1 and T2 under the
933 usual arithmetic conversions. The default conversions have already
934 been applied, and enumerated types converted to their compatible
935 integer types. */
937 tree
938 common_type (tree t1, tree t2)
940 /* If one type is nonsense, use the other */
941 if (t1 == error_mark_node)
942 return t2;
943 if (t2 == error_mark_node)
944 return t1;
946 return cp_common_type (t1, t2);
949 /* Return the common type of two pointer types T1 and T2. This is the
950 type for the result of most arithmetic operations if the operands
951 have the given two types.
953 We assume that comp_target_types has already been done and returned
954 nonzero; if that isn't so, this may crash. */
956 tree
957 common_pointer_type (tree t1, tree t2)
959 gcc_assert ((TYPE_PTR_P (t1) && TYPE_PTR_P (t2))
960 || (TYPE_PTRDATAMEM_P (t1) && TYPE_PTRDATAMEM_P (t2))
961 || (TYPE_PTRMEMFUNC_P (t1) && TYPE_PTRMEMFUNC_P (t2)));
963 return composite_pointer_type (t1, t2, error_mark_node, error_mark_node,
964 CPO_CONVERSION, tf_warning_or_error);
967 /* Compare two exception specifier types for exactness or subsetness, if
968 allowed. Returns false for mismatch, true for match (same, or
969 derived and !exact).
971 [except.spec] "If a class X ... objects of class X or any class publicly
972 and unambiguously derived from X. Similarly, if a pointer type Y * ...
973 exceptions of type Y * or that are pointers to any type publicly and
974 unambiguously derived from Y. Otherwise a function only allows exceptions
975 that have the same type ..."
976 This does not mention cv qualifiers and is different to what throw
977 [except.throw] and catch [except.catch] will do. They will ignore the
978 top level cv qualifiers, and allow qualifiers in the pointer to class
979 example.
981 We implement the letter of the standard. */
983 static bool
984 comp_except_types (tree a, tree b, bool exact)
986 if (same_type_p (a, b))
987 return true;
988 else if (!exact)
990 if (cp_type_quals (a) || cp_type_quals (b))
991 return false;
993 if (TYPE_PTR_P (a) && TYPE_PTR_P (b))
995 a = TREE_TYPE (a);
996 b = TREE_TYPE (b);
997 if (cp_type_quals (a) || cp_type_quals (b))
998 return false;
1001 if (TREE_CODE (a) != RECORD_TYPE
1002 || TREE_CODE (b) != RECORD_TYPE)
1003 return false;
1005 if (publicly_uniquely_derived_p (a, b))
1006 return true;
1008 return false;
1011 /* Return true if TYPE1 and TYPE2 are equivalent exception specifiers.
1012 If EXACT is ce_derived, T2 can be stricter than T1 (according to 15.4/5).
1013 If EXACT is ce_normal, the compatibility rules in 15.4/3 apply.
1014 If EXACT is ce_exact, the specs must be exactly the same. Exception lists
1015 are unordered, but we've already filtered out duplicates. Most lists will
1016 be in order, we should try to make use of that. */
1018 bool
1019 comp_except_specs (const_tree t1, const_tree t2, int exact)
1021 const_tree probe;
1022 const_tree base;
1023 int length = 0;
1025 if (t1 == t2)
1026 return true;
1028 /* First handle noexcept. */
1029 if (exact < ce_exact)
1031 /* noexcept(false) is compatible with no exception-specification,
1032 and stricter than any spec. */
1033 if (t1 == noexcept_false_spec)
1034 return t2 == NULL_TREE || exact == ce_derived;
1035 /* Even a derived noexcept(false) is compatible with no
1036 exception-specification. */
1037 if (t2 == noexcept_false_spec)
1038 return t1 == NULL_TREE;
1040 /* Otherwise, if we aren't looking for an exact match, noexcept is
1041 equivalent to throw(). */
1042 if (t1 == noexcept_true_spec)
1043 t1 = empty_except_spec;
1044 if (t2 == noexcept_true_spec)
1045 t2 = empty_except_spec;
1048 /* If any noexcept is left, it is only comparable to itself;
1049 either we're looking for an exact match or we're redeclaring a
1050 template with dependent noexcept. */
1051 if ((t1 && TREE_PURPOSE (t1))
1052 || (t2 && TREE_PURPOSE (t2)))
1053 return (t1 && t2
1054 && cp_tree_equal (TREE_PURPOSE (t1), TREE_PURPOSE (t2)));
1056 if (t1 == NULL_TREE) /* T1 is ... */
1057 return t2 == NULL_TREE || exact == ce_derived;
1058 if (!TREE_VALUE (t1)) /* t1 is EMPTY */
1059 return t2 != NULL_TREE && !TREE_VALUE (t2);
1060 if (t2 == NULL_TREE) /* T2 is ... */
1061 return false;
1062 if (TREE_VALUE (t1) && !TREE_VALUE (t2)) /* T2 is EMPTY, T1 is not */
1063 return exact == ce_derived;
1065 /* Neither set is ... or EMPTY, make sure each part of T2 is in T1.
1066 Count how many we find, to determine exactness. For exact matching and
1067 ordered T1, T2, this is an O(n) operation, otherwise its worst case is
1068 O(nm). */
1069 for (base = t1; t2 != NULL_TREE; t2 = TREE_CHAIN (t2))
1071 for (probe = base; probe != NULL_TREE; probe = TREE_CHAIN (probe))
1073 tree a = TREE_VALUE (probe);
1074 tree b = TREE_VALUE (t2);
1076 if (comp_except_types (a, b, exact))
1078 if (probe == base && exact > ce_derived)
1079 base = TREE_CHAIN (probe);
1080 length++;
1081 break;
1084 if (probe == NULL_TREE)
1085 return false;
1087 return exact == ce_derived || base == NULL_TREE || length == list_length (t1);
1090 /* Compare the array types T1 and T2. ALLOW_REDECLARATION is true if
1091 [] can match [size]. */
1093 static bool
1094 comp_array_types (const_tree t1, const_tree t2, bool allow_redeclaration)
1096 tree d1;
1097 tree d2;
1098 tree max1, max2;
1100 if (t1 == t2)
1101 return true;
1103 /* The type of the array elements must be the same. */
1104 if (!same_type_p (TREE_TYPE (t1), TREE_TYPE (t2)))
1105 return false;
1107 d1 = TYPE_DOMAIN (t1);
1108 d2 = TYPE_DOMAIN (t2);
1110 if (d1 == d2)
1111 return true;
1113 /* If one of the arrays is dimensionless, and the other has a
1114 dimension, they are of different types. However, it is valid to
1115 write:
1117 extern int a[];
1118 int a[3];
1120 by [basic.link]:
1122 declarations for an array object can specify
1123 array types that differ by the presence or absence of a major
1124 array bound (_dcl.array_). */
1125 if (!d1 || !d2)
1126 return allow_redeclaration;
1128 /* Check that the dimensions are the same. */
1130 if (!cp_tree_equal (TYPE_MIN_VALUE (d1), TYPE_MIN_VALUE (d2)))
1131 return false;
1132 max1 = TYPE_MAX_VALUE (d1);
1133 max2 = TYPE_MAX_VALUE (d2);
1135 if (!cp_tree_equal (max1, max2))
1136 return false;
1138 return true;
1141 /* Compare the relative position of T1 and T2 into their respective
1142 template parameter list.
1143 T1 and T2 must be template parameter types.
1144 Return TRUE if T1 and T2 have the same position, FALSE otherwise. */
1146 static bool
1147 comp_template_parms_position (tree t1, tree t2)
1149 tree index1, index2;
1150 gcc_assert (t1 && t2
1151 && TREE_CODE (t1) == TREE_CODE (t2)
1152 && (TREE_CODE (t1) == BOUND_TEMPLATE_TEMPLATE_PARM
1153 || TREE_CODE (t1) == TEMPLATE_TEMPLATE_PARM
1154 || TREE_CODE (t1) == TEMPLATE_TYPE_PARM));
1156 index1 = TEMPLATE_TYPE_PARM_INDEX (TYPE_MAIN_VARIANT (t1));
1157 index2 = TEMPLATE_TYPE_PARM_INDEX (TYPE_MAIN_VARIANT (t2));
1159 /* Then compare their relative position. */
1160 if (TEMPLATE_PARM_IDX (index1) != TEMPLATE_PARM_IDX (index2)
1161 || TEMPLATE_PARM_LEVEL (index1) != TEMPLATE_PARM_LEVEL (index2)
1162 || (TEMPLATE_PARM_PARAMETER_PACK (index1)
1163 != TEMPLATE_PARM_PARAMETER_PACK (index2)))
1164 return false;
1166 /* In C++14 we can end up comparing 'auto' to a normal template
1167 parameter. Don't confuse them. */
1168 if (cxx_dialect >= cxx14 && (is_auto (t1) || is_auto (t2)))
1169 return TYPE_IDENTIFIER (t1) == TYPE_IDENTIFIER (t2);
1171 return true;
1174 /* Subroutine in comptypes. */
1176 static bool
1177 structural_comptypes (tree t1, tree t2, int strict)
1179 if (t1 == t2)
1180 return true;
1182 /* Suppress errors caused by previously reported errors. */
1183 if (t1 == error_mark_node || t2 == error_mark_node)
1184 return false;
1186 gcc_assert (TYPE_P (t1) && TYPE_P (t2));
1188 /* TYPENAME_TYPEs should be resolved if the qualifying scope is the
1189 current instantiation. */
1190 if (TREE_CODE (t1) == TYPENAME_TYPE)
1191 t1 = resolve_typename_type (t1, /*only_current_p=*/true);
1193 if (TREE_CODE (t2) == TYPENAME_TYPE)
1194 t2 = resolve_typename_type (t2, /*only_current_p=*/true);
1196 if (TYPE_PTRMEMFUNC_P (t1))
1197 t1 = TYPE_PTRMEMFUNC_FN_TYPE (t1);
1198 if (TYPE_PTRMEMFUNC_P (t2))
1199 t2 = TYPE_PTRMEMFUNC_FN_TYPE (t2);
1201 /* Different classes of types can't be compatible. */
1202 if (TREE_CODE (t1) != TREE_CODE (t2))
1203 return false;
1205 /* Qualifiers must match. For array types, we will check when we
1206 recur on the array element types. */
1207 if (TREE_CODE (t1) != ARRAY_TYPE
1208 && cp_type_quals (t1) != cp_type_quals (t2))
1209 return false;
1210 if (TREE_CODE (t1) == FUNCTION_TYPE
1211 && type_memfn_quals (t1) != type_memfn_quals (t2))
1212 return false;
1213 /* Need to check this before TYPE_MAIN_VARIANT.
1214 FIXME function qualifiers should really change the main variant. */
1215 if ((TREE_CODE (t1) == FUNCTION_TYPE
1216 || TREE_CODE (t1) == METHOD_TYPE)
1217 && type_memfn_rqual (t1) != type_memfn_rqual (t2))
1218 return false;
1219 if (TYPE_FOR_JAVA (t1) != TYPE_FOR_JAVA (t2))
1220 return false;
1222 /* Allow for two different type nodes which have essentially the same
1223 definition. Note that we already checked for equality of the type
1224 qualifiers (just above). */
1226 if (TREE_CODE (t1) != ARRAY_TYPE
1227 && TYPE_MAIN_VARIANT (t1) == TYPE_MAIN_VARIANT (t2))
1228 return true;
1231 /* Compare the types. Break out if they could be the same. */
1232 switch (TREE_CODE (t1))
1234 case VOID_TYPE:
1235 case BOOLEAN_TYPE:
1236 /* All void and bool types are the same. */
1237 break;
1239 case INTEGER_TYPE:
1240 case FIXED_POINT_TYPE:
1241 case REAL_TYPE:
1242 /* With these nodes, we can't determine type equivalence by
1243 looking at what is stored in the nodes themselves, because
1244 two nodes might have different TYPE_MAIN_VARIANTs but still
1245 represent the same type. For example, wchar_t and int could
1246 have the same properties (TYPE_PRECISION, TYPE_MIN_VALUE,
1247 TYPE_MAX_VALUE, etc.), but have different TYPE_MAIN_VARIANTs
1248 and are distinct types. On the other hand, int and the
1249 following typedef
1251 typedef int INT __attribute((may_alias));
1253 have identical properties, different TYPE_MAIN_VARIANTs, but
1254 represent the same type. The canonical type system keeps
1255 track of equivalence in this case, so we fall back on it. */
1256 return TYPE_CANONICAL (t1) == TYPE_CANONICAL (t2);
1258 case TEMPLATE_TEMPLATE_PARM:
1259 case BOUND_TEMPLATE_TEMPLATE_PARM:
1260 if (!comp_template_parms_position (t1, t2))
1261 return false;
1262 if (!comp_template_parms
1263 (DECL_TEMPLATE_PARMS (TEMPLATE_TEMPLATE_PARM_TEMPLATE_DECL (t1)),
1264 DECL_TEMPLATE_PARMS (TEMPLATE_TEMPLATE_PARM_TEMPLATE_DECL (t2))))
1265 return false;
1266 if (TREE_CODE (t1) == TEMPLATE_TEMPLATE_PARM)
1267 break;
1268 /* Don't check inheritance. */
1269 strict = COMPARE_STRICT;
1270 /* Fall through. */
1272 case RECORD_TYPE:
1273 case UNION_TYPE:
1274 if (TYPE_TEMPLATE_INFO (t1) && TYPE_TEMPLATE_INFO (t2)
1275 && (TYPE_TI_TEMPLATE (t1) == TYPE_TI_TEMPLATE (t2)
1276 || TREE_CODE (t1) == BOUND_TEMPLATE_TEMPLATE_PARM)
1277 && comp_template_args (TYPE_TI_ARGS (t1), TYPE_TI_ARGS (t2)))
1278 break;
1280 if ((strict & COMPARE_BASE) && DERIVED_FROM_P (t1, t2))
1281 break;
1282 else if ((strict & COMPARE_DERIVED) && DERIVED_FROM_P (t2, t1))
1283 break;
1285 return false;
1287 case OFFSET_TYPE:
1288 if (!comptypes (TYPE_OFFSET_BASETYPE (t1), TYPE_OFFSET_BASETYPE (t2),
1289 strict & ~COMPARE_REDECLARATION))
1290 return false;
1291 if (!same_type_p (TREE_TYPE (t1), TREE_TYPE (t2)))
1292 return false;
1293 break;
1295 case REFERENCE_TYPE:
1296 if (TYPE_REF_IS_RVALUE (t1) != TYPE_REF_IS_RVALUE (t2))
1297 return false;
1298 /* fall through to checks for pointer types */
1300 case POINTER_TYPE:
1301 if (TYPE_MODE (t1) != TYPE_MODE (t2)
1302 || !same_type_p (TREE_TYPE (t1), TREE_TYPE (t2)))
1303 return false;
1304 break;
1306 case METHOD_TYPE:
1307 case FUNCTION_TYPE:
1308 if (!same_type_p (TREE_TYPE (t1), TREE_TYPE (t2)))
1309 return false;
1310 if (!compparms (TYPE_ARG_TYPES (t1), TYPE_ARG_TYPES (t2)))
1311 return false;
1312 break;
1314 case ARRAY_TYPE:
1315 /* Target types must match incl. qualifiers. */
1316 if (!comp_array_types (t1, t2, !!(strict & COMPARE_REDECLARATION)))
1317 return false;
1318 break;
1320 case TEMPLATE_TYPE_PARM:
1321 /* If T1 and T2 don't have the same relative position in their
1322 template parameters set, they can't be equal. */
1323 if (!comp_template_parms_position (t1, t2))
1324 return false;
1325 break;
1327 case TYPENAME_TYPE:
1328 if (!cp_tree_equal (TYPENAME_TYPE_FULLNAME (t1),
1329 TYPENAME_TYPE_FULLNAME (t2)))
1330 return false;
1331 /* Qualifiers don't matter on scopes. */
1332 if (!same_type_ignoring_top_level_qualifiers_p (TYPE_CONTEXT (t1),
1333 TYPE_CONTEXT (t2)))
1334 return false;
1335 break;
1337 case UNBOUND_CLASS_TEMPLATE:
1338 if (!cp_tree_equal (TYPE_IDENTIFIER (t1), TYPE_IDENTIFIER (t2)))
1339 return false;
1340 if (!same_type_p (TYPE_CONTEXT (t1), TYPE_CONTEXT (t2)))
1341 return false;
1342 break;
1344 case COMPLEX_TYPE:
1345 if (!same_type_p (TREE_TYPE (t1), TREE_TYPE (t2)))
1346 return false;
1347 break;
1349 case VECTOR_TYPE:
1350 if (TYPE_VECTOR_SUBPARTS (t1) != TYPE_VECTOR_SUBPARTS (t2)
1351 || !same_type_p (TREE_TYPE (t1), TREE_TYPE (t2)))
1352 return false;
1353 break;
1355 case TYPE_PACK_EXPANSION:
1356 return (same_type_p (PACK_EXPANSION_PATTERN (t1),
1357 PACK_EXPANSION_PATTERN (t2))
1358 && comp_template_args (PACK_EXPANSION_EXTRA_ARGS (t1),
1359 PACK_EXPANSION_EXTRA_ARGS (t2)));
1361 case DECLTYPE_TYPE:
1362 if (DECLTYPE_TYPE_ID_EXPR_OR_MEMBER_ACCESS_P (t1)
1363 != DECLTYPE_TYPE_ID_EXPR_OR_MEMBER_ACCESS_P (t2)
1364 || (DECLTYPE_FOR_LAMBDA_CAPTURE (t1)
1365 != DECLTYPE_FOR_LAMBDA_CAPTURE (t2))
1366 || (DECLTYPE_FOR_LAMBDA_PROXY (t1)
1367 != DECLTYPE_FOR_LAMBDA_PROXY (t2))
1368 || !cp_tree_equal (DECLTYPE_TYPE_EXPR (t1),
1369 DECLTYPE_TYPE_EXPR (t2)))
1370 return false;
1371 break;
1373 case UNDERLYING_TYPE:
1374 return same_type_p (UNDERLYING_TYPE_TYPE (t1),
1375 UNDERLYING_TYPE_TYPE (t2));
1377 default:
1378 return false;
1381 /* If we get here, we know that from a target independent POV the
1382 types are the same. Make sure the target attributes are also
1383 the same. */
1384 return comp_type_attributes (t1, t2);
1387 /* Return true if T1 and T2 are related as allowed by STRICT. STRICT
1388 is a bitwise-or of the COMPARE_* flags. */
1390 bool
1391 comptypes (tree t1, tree t2, int strict)
1393 if (strict == COMPARE_STRICT)
1395 if (t1 == t2)
1396 return true;
1398 if (t1 == error_mark_node || t2 == error_mark_node)
1399 return false;
1401 if (TYPE_STRUCTURAL_EQUALITY_P (t1) || TYPE_STRUCTURAL_EQUALITY_P (t2))
1402 /* At least one of the types requires structural equality, so
1403 perform a deep check. */
1404 return structural_comptypes (t1, t2, strict);
1406 #ifdef ENABLE_CHECKING
1407 if (USE_CANONICAL_TYPES)
1409 bool result = structural_comptypes (t1, t2, strict);
1411 if (result && TYPE_CANONICAL (t1) != TYPE_CANONICAL (t2))
1412 /* The two types are structurally equivalent, but their
1413 canonical types were different. This is a failure of the
1414 canonical type propagation code.*/
1415 internal_error
1416 ("canonical types differ for identical types %T and %T",
1417 t1, t2);
1418 else if (!result && TYPE_CANONICAL (t1) == TYPE_CANONICAL (t2))
1419 /* Two types are structurally different, but the canonical
1420 types are the same. This means we were over-eager in
1421 assigning canonical types. */
1422 internal_error
1423 ("same canonical type node for different types %T and %T",
1424 t1, t2);
1426 return result;
1428 #else
1429 if (USE_CANONICAL_TYPES)
1430 return TYPE_CANONICAL (t1) == TYPE_CANONICAL (t2);
1431 #endif
1432 else
1433 return structural_comptypes (t1, t2, strict);
1435 else if (strict == COMPARE_STRUCTURAL)
1436 return structural_comptypes (t1, t2, COMPARE_STRICT);
1437 else
1438 return structural_comptypes (t1, t2, strict);
1441 /* Returns nonzero iff TYPE1 and TYPE2 are the same type, ignoring
1442 top-level qualifiers. */
1444 bool
1445 same_type_ignoring_top_level_qualifiers_p (tree type1, tree type2)
1447 if (type1 == error_mark_node || type2 == error_mark_node)
1448 return false;
1450 return same_type_p (TYPE_MAIN_VARIANT (type1), TYPE_MAIN_VARIANT (type2));
1453 /* Returns 1 if TYPE1 is at least as qualified as TYPE2. */
1455 bool
1456 at_least_as_qualified_p (const_tree type1, const_tree type2)
1458 int q1 = cp_type_quals (type1);
1459 int q2 = cp_type_quals (type2);
1461 /* All qualifiers for TYPE2 must also appear in TYPE1. */
1462 return (q1 & q2) == q2;
1465 /* Returns 1 if TYPE1 is more cv-qualified than TYPE2, -1 if TYPE2 is
1466 more cv-qualified that TYPE1, and 0 otherwise. */
1469 comp_cv_qualification (int q1, int q2)
1471 if (q1 == q2)
1472 return 0;
1474 if ((q1 & q2) == q2)
1475 return 1;
1476 else if ((q1 & q2) == q1)
1477 return -1;
1479 return 0;
1483 comp_cv_qualification (const_tree type1, const_tree type2)
1485 int q1 = cp_type_quals (type1);
1486 int q2 = cp_type_quals (type2);
1487 return comp_cv_qualification (q1, q2);
1490 /* Returns 1 if the cv-qualification signature of TYPE1 is a proper
1491 subset of the cv-qualification signature of TYPE2, and the types
1492 are similar. Returns -1 if the other way 'round, and 0 otherwise. */
1495 comp_cv_qual_signature (tree type1, tree type2)
1497 if (comp_ptr_ttypes_real (type2, type1, -1))
1498 return 1;
1499 else if (comp_ptr_ttypes_real (type1, type2, -1))
1500 return -1;
1501 else
1502 return 0;
1505 /* Subroutines of `comptypes'. */
1507 /* Return true if two parameter type lists PARMS1 and PARMS2 are
1508 equivalent in the sense that functions with those parameter types
1509 can have equivalent types. The two lists must be equivalent,
1510 element by element. */
1512 bool
1513 compparms (const_tree parms1, const_tree parms2)
1515 const_tree t1, t2;
1517 /* An unspecified parmlist matches any specified parmlist
1518 whose argument types don't need default promotions. */
1520 for (t1 = parms1, t2 = parms2;
1521 t1 || t2;
1522 t1 = TREE_CHAIN (t1), t2 = TREE_CHAIN (t2))
1524 /* If one parmlist is shorter than the other,
1525 they fail to match. */
1526 if (!t1 || !t2)
1527 return false;
1528 if (!same_type_p (TREE_VALUE (t1), TREE_VALUE (t2)))
1529 return false;
1531 return true;
1535 /* Process a sizeof or alignof expression where the operand is a
1536 type. */
1538 tree
1539 cxx_sizeof_or_alignof_type (tree type, enum tree_code op, bool complain)
1541 tree value;
1542 bool dependent_p;
1544 gcc_assert (op == SIZEOF_EXPR || op == ALIGNOF_EXPR);
1545 if (type == error_mark_node)
1546 return error_mark_node;
1548 type = non_reference (type);
1549 if (TREE_CODE (type) == METHOD_TYPE)
1551 if (complain)
1552 pedwarn (input_location, OPT_Wpointer_arith,
1553 "invalid application of %qs to a member function",
1554 operator_name_info[(int) op].name);
1555 else
1556 return error_mark_node;
1557 value = size_one_node;
1560 dependent_p = dependent_type_p (type);
1561 if (!dependent_p)
1562 complete_type (type);
1563 if (dependent_p
1564 /* VLA types will have a non-constant size. In the body of an
1565 uninstantiated template, we don't need to try to compute the
1566 value, because the sizeof expression is not an integral
1567 constant expression in that case. And, if we do try to
1568 compute the value, we'll likely end up with SAVE_EXPRs, which
1569 the template substitution machinery does not expect to see. */
1570 || (processing_template_decl
1571 && COMPLETE_TYPE_P (type)
1572 && TREE_CODE (TYPE_SIZE (type)) != INTEGER_CST))
1574 value = build_min (op, size_type_node, type);
1575 TREE_READONLY (value) = 1;
1576 return value;
1579 return c_sizeof_or_alignof_type (input_location, complete_type (type),
1580 op == SIZEOF_EXPR, false,
1581 complain);
1584 /* Return the size of the type, without producing any warnings for
1585 types whose size cannot be taken. This routine should be used only
1586 in some other routine that has already produced a diagnostic about
1587 using the size of such a type. */
1588 tree
1589 cxx_sizeof_nowarn (tree type)
1591 if (TREE_CODE (type) == FUNCTION_TYPE
1592 || VOID_TYPE_P (type)
1593 || TREE_CODE (type) == ERROR_MARK)
1594 return size_one_node;
1595 else if (!COMPLETE_TYPE_P (type))
1596 return size_zero_node;
1597 else
1598 return cxx_sizeof_or_alignof_type (type, SIZEOF_EXPR, false);
1601 /* Process a sizeof expression where the operand is an expression. */
1603 static tree
1604 cxx_sizeof_expr (tree e, tsubst_flags_t complain)
1606 if (e == error_mark_node)
1607 return error_mark_node;
1609 if (processing_template_decl)
1611 e = build_min (SIZEOF_EXPR, size_type_node, e);
1612 TREE_SIDE_EFFECTS (e) = 0;
1613 TREE_READONLY (e) = 1;
1615 return e;
1618 /* To get the size of a static data member declared as an array of
1619 unknown bound, we need to instantiate it. */
1620 if (VAR_P (e)
1621 && VAR_HAD_UNKNOWN_BOUND (e)
1622 && DECL_TEMPLATE_INSTANTIATION (e))
1623 instantiate_decl (e, /*defer_ok*/true, /*expl_inst_mem*/false);
1625 if (TREE_CODE (e) == PARM_DECL
1626 && DECL_ARRAY_PARAMETER_P (e)
1627 && (complain & tf_warning))
1629 if (warning (OPT_Wsizeof_array_argument, "%<sizeof%> on array function "
1630 "parameter %qE will return size of %qT", e, TREE_TYPE (e)))
1631 inform (DECL_SOURCE_LOCATION (e), "declared here");
1634 e = mark_type_use (e);
1636 if (TREE_CODE (e) == COMPONENT_REF
1637 && TREE_CODE (TREE_OPERAND (e, 1)) == FIELD_DECL
1638 && DECL_C_BIT_FIELD (TREE_OPERAND (e, 1)))
1640 if (complain & tf_error)
1641 error ("invalid application of %<sizeof%> to a bit-field");
1642 else
1643 return error_mark_node;
1644 e = char_type_node;
1646 else if (is_overloaded_fn (e))
1648 if (complain & tf_error)
1649 permerror (input_location, "ISO C++ forbids applying %<sizeof%> to an expression of "
1650 "function type");
1651 else
1652 return error_mark_node;
1653 e = char_type_node;
1655 else if (type_unknown_p (e))
1657 if (complain & tf_error)
1658 cxx_incomplete_type_error (e, TREE_TYPE (e));
1659 else
1660 return error_mark_node;
1661 e = char_type_node;
1663 else
1664 e = TREE_TYPE (e);
1666 return cxx_sizeof_or_alignof_type (e, SIZEOF_EXPR, complain & tf_error);
1669 /* Implement the __alignof keyword: Return the minimum required
1670 alignment of E, measured in bytes. For VAR_DECL's and
1671 FIELD_DECL's return DECL_ALIGN (which can be set from an
1672 "aligned" __attribute__ specification). */
1674 static tree
1675 cxx_alignof_expr (tree e, tsubst_flags_t complain)
1677 tree t;
1679 if (e == error_mark_node)
1680 return error_mark_node;
1682 if (processing_template_decl)
1684 e = build_min (ALIGNOF_EXPR, size_type_node, e);
1685 TREE_SIDE_EFFECTS (e) = 0;
1686 TREE_READONLY (e) = 1;
1688 return e;
1691 e = mark_type_use (e);
1693 if (VAR_P (e))
1694 t = size_int (DECL_ALIGN_UNIT (e));
1695 else if (TREE_CODE (e) == COMPONENT_REF
1696 && TREE_CODE (TREE_OPERAND (e, 1)) == FIELD_DECL
1697 && DECL_C_BIT_FIELD (TREE_OPERAND (e, 1)))
1699 if (complain & tf_error)
1700 error ("invalid application of %<__alignof%> to a bit-field");
1701 else
1702 return error_mark_node;
1703 t = size_one_node;
1705 else if (TREE_CODE (e) == COMPONENT_REF
1706 && TREE_CODE (TREE_OPERAND (e, 1)) == FIELD_DECL)
1707 t = size_int (DECL_ALIGN_UNIT (TREE_OPERAND (e, 1)));
1708 else if (is_overloaded_fn (e))
1710 if (complain & tf_error)
1711 permerror (input_location, "ISO C++ forbids applying %<__alignof%> to an expression of "
1712 "function type");
1713 else
1714 return error_mark_node;
1715 if (TREE_CODE (e) == FUNCTION_DECL)
1716 t = size_int (DECL_ALIGN_UNIT (e));
1717 else
1718 t = size_one_node;
1720 else if (type_unknown_p (e))
1722 if (complain & tf_error)
1723 cxx_incomplete_type_error (e, TREE_TYPE (e));
1724 else
1725 return error_mark_node;
1726 t = size_one_node;
1728 else
1729 return cxx_sizeof_or_alignof_type (TREE_TYPE (e), ALIGNOF_EXPR,
1730 complain & tf_error);
1732 return fold_convert (size_type_node, t);
1735 /* Process a sizeof or alignof expression E with code OP where the operand
1736 is an expression. */
1738 tree
1739 cxx_sizeof_or_alignof_expr (tree e, enum tree_code op, bool complain)
1741 if (op == SIZEOF_EXPR)
1742 return cxx_sizeof_expr (e, complain? tf_warning_or_error : tf_none);
1743 else
1744 return cxx_alignof_expr (e, complain? tf_warning_or_error : tf_none);
1747 /* Build a representation of an expression 'alignas(E).' Return the
1748 folded integer value of E if it is an integral constant expression
1749 that resolves to a valid alignment. If E depends on a template
1750 parameter, return a syntactic representation tree of kind
1751 ALIGNOF_EXPR. Otherwise, return an error_mark_node if the
1752 expression is ill formed, or NULL_TREE if E is NULL_TREE. */
1754 tree
1755 cxx_alignas_expr (tree e)
1757 if (e == NULL_TREE || e == error_mark_node
1758 || (!TYPE_P (e) && !require_potential_rvalue_constant_expression (e)))
1759 return e;
1761 if (TYPE_P (e))
1762 /* [dcl.align]/3:
1764 When the alignment-specifier is of the form
1765 alignas(type-id ), it shall have the same effect as
1766 alignas(alignof(type-id )). */
1768 return cxx_sizeof_or_alignof_type (e, ALIGNOF_EXPR, false);
1770 /* If we reach this point, it means the alignas expression if of
1771 the form "alignas(assignment-expression)", so we should follow
1772 what is stated by [dcl.align]/2. */
1774 if (value_dependent_expression_p (e))
1775 /* Leave value-dependent expression alone for now. */
1776 return e;
1778 e = instantiate_non_dependent_expr (e);
1779 e = mark_rvalue_use (e);
1781 /* [dcl.align]/2 says:
1783 the assignment-expression shall be an integral constant
1784 expression. */
1786 return cxx_constant_value (e);
1790 /* EXPR is being used in a context that is not a function call.
1791 Enforce:
1793 [expr.ref]
1795 The expression can be used only as the left-hand operand of a
1796 member function call.
1798 [expr.mptr.operator]
1800 If the result of .* or ->* is a function, then that result can be
1801 used only as the operand for the function call operator ().
1803 by issuing an error message if appropriate. Returns true iff EXPR
1804 violates these rules. */
1806 bool
1807 invalid_nonstatic_memfn_p (location_t loc, tree expr, tsubst_flags_t complain)
1809 if (expr == NULL_TREE)
1810 return false;
1811 /* Don't enforce this in MS mode. */
1812 if (flag_ms_extensions)
1813 return false;
1814 if (is_overloaded_fn (expr) && !really_overloaded_fn (expr))
1815 expr = get_first_fn (expr);
1816 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (expr))
1818 if (complain & tf_error)
1820 if (DECL_P (expr))
1822 error_at (loc, "invalid use of non-static member function %qD",
1823 expr);
1824 inform (DECL_SOURCE_LOCATION (expr), "declared here");
1826 else
1827 error_at (loc, "invalid use of non-static member function of "
1828 "type %qT", TREE_TYPE (expr));
1830 return true;
1832 return false;
1835 /* If EXP is a reference to a bitfield, and the type of EXP does not
1836 match the declared type of the bitfield, return the declared type
1837 of the bitfield. Otherwise, return NULL_TREE. */
1839 tree
1840 is_bitfield_expr_with_lowered_type (const_tree exp)
1842 switch (TREE_CODE (exp))
1844 case COND_EXPR:
1845 if (!is_bitfield_expr_with_lowered_type (TREE_OPERAND (exp, 1)
1846 ? TREE_OPERAND (exp, 1)
1847 : TREE_OPERAND (exp, 0)))
1848 return NULL_TREE;
1849 return is_bitfield_expr_with_lowered_type (TREE_OPERAND (exp, 2));
1851 case COMPOUND_EXPR:
1852 return is_bitfield_expr_with_lowered_type (TREE_OPERAND (exp, 1));
1854 case MODIFY_EXPR:
1855 case SAVE_EXPR:
1856 return is_bitfield_expr_with_lowered_type (TREE_OPERAND (exp, 0));
1858 case COMPONENT_REF:
1860 tree field;
1862 field = TREE_OPERAND (exp, 1);
1863 if (TREE_CODE (field) != FIELD_DECL || !DECL_BIT_FIELD_TYPE (field))
1864 return NULL_TREE;
1865 if (same_type_ignoring_top_level_qualifiers_p
1866 (TREE_TYPE (exp), DECL_BIT_FIELD_TYPE (field)))
1867 return NULL_TREE;
1868 return DECL_BIT_FIELD_TYPE (field);
1871 CASE_CONVERT:
1872 if (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_OPERAND (exp, 0)))
1873 == TYPE_MAIN_VARIANT (TREE_TYPE (exp)))
1874 return is_bitfield_expr_with_lowered_type (TREE_OPERAND (exp, 0));
1875 /* Fallthrough. */
1877 default:
1878 return NULL_TREE;
1882 /* Like is_bitfield_with_lowered_type, except that if EXP is not a
1883 bitfield with a lowered type, the type of EXP is returned, rather
1884 than NULL_TREE. */
1886 tree
1887 unlowered_expr_type (const_tree exp)
1889 tree type;
1890 tree etype = TREE_TYPE (exp);
1892 type = is_bitfield_expr_with_lowered_type (exp);
1893 if (type)
1894 type = cp_build_qualified_type (type, cp_type_quals (etype));
1895 else
1896 type = etype;
1898 return type;
1901 /* Perform the conversions in [expr] that apply when an lvalue appears
1902 in an rvalue context: the lvalue-to-rvalue, array-to-pointer, and
1903 function-to-pointer conversions. In addition, manifest constants
1904 are replaced by their values, and bitfield references are converted
1905 to their declared types. Note that this function does not perform the
1906 lvalue-to-rvalue conversion for class types. If you need that conversion
1907 to for class types, then you probably need to use force_rvalue.
1909 Although the returned value is being used as an rvalue, this
1910 function does not wrap the returned expression in a
1911 NON_LVALUE_EXPR; the caller is expected to be mindful of the fact
1912 that the return value is no longer an lvalue. */
1914 tree
1915 decay_conversion (tree exp, tsubst_flags_t complain)
1917 tree type;
1918 enum tree_code code;
1919 location_t loc = EXPR_LOC_OR_LOC (exp, input_location);
1921 type = TREE_TYPE (exp);
1922 if (type == error_mark_node)
1923 return error_mark_node;
1925 exp = mark_rvalue_use (exp);
1927 exp = resolve_nondeduced_context (exp);
1928 if (type_unknown_p (exp))
1930 if (complain & tf_error)
1931 cxx_incomplete_type_error (exp, TREE_TYPE (exp));
1932 return error_mark_node;
1935 code = TREE_CODE (type);
1937 /* FIXME remove for delayed folding. */
1938 exp = scalar_constant_value (exp);
1939 if (error_operand_p (exp))
1940 return error_mark_node;
1942 if (NULLPTR_TYPE_P (type) && !TREE_SIDE_EFFECTS (exp))
1943 return nullptr_node;
1945 /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
1946 Leave such NOP_EXPRs, since RHS is being used in non-lvalue context. */
1947 if (code == VOID_TYPE)
1949 if (complain & tf_error)
1950 error_at (loc, "void value not ignored as it ought to be");
1951 return error_mark_node;
1953 if (invalid_nonstatic_memfn_p (loc, exp, complain))
1954 return error_mark_node;
1955 if (code == FUNCTION_TYPE || is_overloaded_fn (exp))
1956 return cp_build_addr_expr (exp, complain);
1957 if (code == ARRAY_TYPE)
1959 tree adr;
1960 tree ptrtype;
1962 if (INDIRECT_REF_P (exp))
1963 return build_nop (build_pointer_type (TREE_TYPE (type)),
1964 TREE_OPERAND (exp, 0));
1966 if (TREE_CODE (exp) == COMPOUND_EXPR)
1968 tree op1 = decay_conversion (TREE_OPERAND (exp, 1), complain);
1969 if (op1 == error_mark_node)
1970 return error_mark_node;
1971 return build2 (COMPOUND_EXPR, TREE_TYPE (op1),
1972 TREE_OPERAND (exp, 0), op1);
1975 if (!lvalue_p (exp)
1976 && ! (TREE_CODE (exp) == CONSTRUCTOR && TREE_STATIC (exp)))
1978 if (complain & tf_error)
1979 error_at (loc, "invalid use of non-lvalue array");
1980 return error_mark_node;
1983 /* Don't let an array compound literal decay to a pointer. It can
1984 still be used to initialize an array or bind to a reference. */
1985 if (TREE_CODE (exp) == TARGET_EXPR)
1987 if (complain & tf_error)
1988 error_at (loc, "taking address of temporary array");
1989 return error_mark_node;
1992 ptrtype = build_pointer_type (TREE_TYPE (type));
1994 if (VAR_P (exp))
1996 if (!cxx_mark_addressable (exp))
1997 return error_mark_node;
1998 adr = build_nop (ptrtype, build_address (exp));
1999 return adr;
2001 /* This way is better for a COMPONENT_REF since it can
2002 simplify the offset for a component. */
2003 adr = cp_build_addr_expr (exp, complain);
2004 return cp_convert (ptrtype, adr, complain);
2007 /* If a bitfield is used in a context where integral promotion
2008 applies, then the caller is expected to have used
2009 default_conversion. That function promotes bitfields correctly
2010 before calling this function. At this point, if we have a
2011 bitfield referenced, we may assume that is not subject to
2012 promotion, and that, therefore, the type of the resulting rvalue
2013 is the declared type of the bitfield. */
2014 exp = convert_bitfield_to_declared_type (exp);
2016 /* We do not call rvalue() here because we do not want to wrap EXP
2017 in a NON_LVALUE_EXPR. */
2019 /* [basic.lval]
2021 Non-class rvalues always have cv-unqualified types. */
2022 type = TREE_TYPE (exp);
2023 if (!CLASS_TYPE_P (type) && cv_qualified_p (type))
2024 exp = build_nop (cv_unqualified (type), exp);
2026 return exp;
2029 /* Perform preparatory conversions, as part of the "usual arithmetic
2030 conversions". In particular, as per [expr]:
2032 Whenever an lvalue expression appears as an operand of an
2033 operator that expects the rvalue for that operand, the
2034 lvalue-to-rvalue, array-to-pointer, or function-to-pointer
2035 standard conversions are applied to convert the expression to an
2036 rvalue.
2038 In addition, we perform integral promotions here, as those are
2039 applied to both operands to a binary operator before determining
2040 what additional conversions should apply. */
2042 static tree
2043 cp_default_conversion (tree exp, tsubst_flags_t complain)
2045 /* Check for target-specific promotions. */
2046 tree promoted_type = targetm.promoted_type (TREE_TYPE (exp));
2047 if (promoted_type)
2048 exp = cp_convert (promoted_type, exp, complain);
2049 /* Perform the integral promotions first so that bitfield
2050 expressions (which may promote to "int", even if the bitfield is
2051 declared "unsigned") are promoted correctly. */
2052 else if (INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (TREE_TYPE (exp)))
2053 exp = cp_perform_integral_promotions (exp, complain);
2054 /* Perform the other conversions. */
2055 exp = decay_conversion (exp, complain);
2057 return exp;
2060 /* C version. */
2062 tree
2063 default_conversion (tree exp)
2065 return cp_default_conversion (exp, tf_warning_or_error);
2068 /* EXPR is an expression with an integral or enumeration type.
2069 Perform the integral promotions in [conv.prom], and return the
2070 converted value. */
2072 tree
2073 cp_perform_integral_promotions (tree expr, tsubst_flags_t complain)
2075 tree type;
2076 tree promoted_type;
2078 expr = mark_rvalue_use (expr);
2080 /* [conv.prom]
2082 If the bitfield has an enumerated type, it is treated as any
2083 other value of that type for promotion purposes. */
2084 type = is_bitfield_expr_with_lowered_type (expr);
2085 if (!type || TREE_CODE (type) != ENUMERAL_TYPE)
2086 type = TREE_TYPE (expr);
2087 gcc_assert (INTEGRAL_OR_ENUMERATION_TYPE_P (type));
2088 /* Scoped enums don't promote. */
2089 if (SCOPED_ENUM_P (type))
2090 return expr;
2091 promoted_type = type_promotes_to (type);
2092 if (type != promoted_type)
2093 expr = cp_convert (promoted_type, expr, complain);
2094 return expr;
2097 /* C version. */
2099 tree
2100 perform_integral_promotions (tree expr)
2102 return cp_perform_integral_promotions (expr, tf_warning_or_error);
2105 /* Returns nonzero iff exp is a STRING_CST or the result of applying
2106 decay_conversion to one. */
2109 string_conv_p (const_tree totype, const_tree exp, int warn)
2111 tree t;
2113 if (!TYPE_PTR_P (totype))
2114 return 0;
2116 t = TREE_TYPE (totype);
2117 if (!same_type_p (t, char_type_node)
2118 && !same_type_p (t, char16_type_node)
2119 && !same_type_p (t, char32_type_node)
2120 && !same_type_p (t, wchar_type_node))
2121 return 0;
2123 if (TREE_CODE (exp) == STRING_CST)
2125 /* Make sure that we don't try to convert between char and wide chars. */
2126 if (!same_type_p (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (exp))), t))
2127 return 0;
2129 else
2131 /* Is this a string constant which has decayed to 'const char *'? */
2132 t = build_pointer_type (cp_build_qualified_type (t, TYPE_QUAL_CONST));
2133 if (!same_type_p (TREE_TYPE (exp), t))
2134 return 0;
2135 STRIP_NOPS (exp);
2136 if (TREE_CODE (exp) != ADDR_EXPR
2137 || TREE_CODE (TREE_OPERAND (exp, 0)) != STRING_CST)
2138 return 0;
2140 if (warn)
2142 if (cxx_dialect >= cxx11)
2143 pedwarn (input_location,
2144 pedantic ? OPT_Wpedantic : OPT_Wwrite_strings,
2145 "ISO C++ forbids converting a string constant to %qT",
2146 totype);
2147 else
2148 warning (OPT_Wwrite_strings,
2149 "deprecated conversion from string constant to %qT",
2150 totype);
2153 return 1;
2156 /* Given a COND_EXPR, MIN_EXPR, or MAX_EXPR in T, return it in a form that we
2157 can, for example, use as an lvalue. This code used to be in
2158 unary_complex_lvalue, but we needed it to deal with `a = (d == c) ? b : c'
2159 expressions, where we're dealing with aggregates. But now it's again only
2160 called from unary_complex_lvalue. The case (in particular) that led to
2161 this was with CODE == ADDR_EXPR, since it's not an lvalue when we'd
2162 get it there. */
2164 static tree
2165 rationalize_conditional_expr (enum tree_code code, tree t,
2166 tsubst_flags_t complain)
2168 location_t loc = EXPR_LOC_OR_LOC (t, input_location);
2170 /* For MIN_EXPR or MAX_EXPR, fold-const.c has arranged things so that
2171 the first operand is always the one to be used if both operands
2172 are equal, so we know what conditional expression this used to be. */
2173 if (TREE_CODE (t) == MIN_EXPR || TREE_CODE (t) == MAX_EXPR)
2175 tree op0 = TREE_OPERAND (t, 0);
2176 tree op1 = TREE_OPERAND (t, 1);
2178 /* The following code is incorrect if either operand side-effects. */
2179 gcc_assert (!TREE_SIDE_EFFECTS (op0)
2180 && !TREE_SIDE_EFFECTS (op1));
2181 return
2182 build_conditional_expr (loc,
2183 build_x_binary_op (loc,
2184 (TREE_CODE (t) == MIN_EXPR
2185 ? LE_EXPR : GE_EXPR),
2186 op0, TREE_CODE (op0),
2187 op1, TREE_CODE (op1),
2188 /*overload=*/NULL,
2189 complain),
2190 cp_build_unary_op (code, op0, 0, complain),
2191 cp_build_unary_op (code, op1, 0, complain),
2192 complain);
2195 return
2196 build_conditional_expr (loc, TREE_OPERAND (t, 0),
2197 cp_build_unary_op (code, TREE_OPERAND (t, 1), 0,
2198 complain),
2199 cp_build_unary_op (code, TREE_OPERAND (t, 2), 0,
2200 complain),
2201 complain);
2204 /* Given the TYPE of an anonymous union field inside T, return the
2205 FIELD_DECL for the field. If not found return NULL_TREE. Because
2206 anonymous unions can nest, we must also search all anonymous unions
2207 that are directly reachable. */
2209 tree
2210 lookup_anon_field (tree t, tree type)
2212 tree field;
2214 t = TYPE_MAIN_VARIANT (t);
2216 for (field = TYPE_FIELDS (t); field; field = DECL_CHAIN (field))
2218 if (TREE_STATIC (field))
2219 continue;
2220 if (TREE_CODE (field) != FIELD_DECL || DECL_ARTIFICIAL (field))
2221 continue;
2223 /* If we find it directly, return the field. */
2224 if (DECL_NAME (field) == NULL_TREE
2225 && type == TYPE_MAIN_VARIANT (TREE_TYPE (field)))
2227 return field;
2230 /* Otherwise, it could be nested, search harder. */
2231 if (DECL_NAME (field) == NULL_TREE
2232 && ANON_AGGR_TYPE_P (TREE_TYPE (field)))
2234 tree subfield = lookup_anon_field (TREE_TYPE (field), type);
2235 if (subfield)
2236 return subfield;
2239 return NULL_TREE;
2242 /* Build an expression representing OBJECT.MEMBER. OBJECT is an
2243 expression; MEMBER is a DECL or baselink. If ACCESS_PATH is
2244 non-NULL, it indicates the path to the base used to name MEMBER.
2245 If PRESERVE_REFERENCE is true, the expression returned will have
2246 REFERENCE_TYPE if the MEMBER does. Otherwise, the expression
2247 returned will have the type referred to by the reference.
2249 This function does not perform access control; that is either done
2250 earlier by the parser when the name of MEMBER is resolved to MEMBER
2251 itself, or later when overload resolution selects one of the
2252 functions indicated by MEMBER. */
2254 tree
2255 build_class_member_access_expr (tree object, tree member,
2256 tree access_path, bool preserve_reference,
2257 tsubst_flags_t complain)
2259 tree object_type;
2260 tree member_scope;
2261 tree result = NULL_TREE;
2262 tree using_decl = NULL_TREE;
2264 if (error_operand_p (object) || error_operand_p (member))
2265 return error_mark_node;
2267 gcc_assert (DECL_P (member) || BASELINK_P (member));
2269 /* [expr.ref]
2271 The type of the first expression shall be "class object" (of a
2272 complete type). */
2273 object_type = TREE_TYPE (object);
2274 if (!currently_open_class (object_type)
2275 && !complete_type_or_maybe_complain (object_type, object, complain))
2276 return error_mark_node;
2277 if (!CLASS_TYPE_P (object_type))
2279 if (complain & tf_error)
2281 if (POINTER_TYPE_P (object_type)
2282 && CLASS_TYPE_P (TREE_TYPE (object_type)))
2283 error ("request for member %qD in %qE, which is of pointer "
2284 "type %qT (maybe you meant to use %<->%> ?)",
2285 member, object, object_type);
2286 else
2287 error ("request for member %qD in %qE, which is of non-class "
2288 "type %qT", member, object, object_type);
2290 return error_mark_node;
2293 /* The standard does not seem to actually say that MEMBER must be a
2294 member of OBJECT_TYPE. However, that is clearly what is
2295 intended. */
2296 if (DECL_P (member))
2298 member_scope = DECL_CLASS_CONTEXT (member);
2299 if (!mark_used (member, complain) && !(complain & tf_error))
2300 return error_mark_node;
2301 if (TREE_DEPRECATED (member))
2302 warn_deprecated_use (member, NULL_TREE);
2304 else
2305 member_scope = BINFO_TYPE (BASELINK_ACCESS_BINFO (member));
2306 /* If MEMBER is from an anonymous aggregate, MEMBER_SCOPE will
2307 presently be the anonymous union. Go outwards until we find a
2308 type related to OBJECT_TYPE. */
2309 while ((ANON_AGGR_TYPE_P (member_scope) || UNSCOPED_ENUM_P (member_scope))
2310 && !same_type_ignoring_top_level_qualifiers_p (member_scope,
2311 object_type))
2312 member_scope = TYPE_CONTEXT (member_scope);
2313 if (!member_scope || !DERIVED_FROM_P (member_scope, object_type))
2315 if (complain & tf_error)
2317 if (TREE_CODE (member) == FIELD_DECL)
2318 error ("invalid use of nonstatic data member %qE", member);
2319 else
2320 error ("%qD is not a member of %qT", member, object_type);
2322 return error_mark_node;
2325 /* Transform `(a, b).x' into `(*(a, &b)).x', `(a ? b : c).x' into
2326 `(*(a ? &b : &c)).x', and so on. A COND_EXPR is only an lvalue
2327 in the front end; only _DECLs and _REFs are lvalues in the back end. */
2329 tree temp = unary_complex_lvalue (ADDR_EXPR, object);
2330 if (temp)
2331 object = cp_build_indirect_ref (temp, RO_NULL, complain);
2334 /* In [expr.ref], there is an explicit list of the valid choices for
2335 MEMBER. We check for each of those cases here. */
2336 if (VAR_P (member))
2338 /* A static data member. */
2339 result = member;
2340 mark_exp_read (object);
2341 /* If OBJECT has side-effects, they are supposed to occur. */
2342 if (TREE_SIDE_EFFECTS (object))
2343 result = build2 (COMPOUND_EXPR, TREE_TYPE (result), object, result);
2345 else if (TREE_CODE (member) == FIELD_DECL)
2347 /* A non-static data member. */
2348 bool null_object_p;
2349 int type_quals;
2350 tree member_type;
2352 null_object_p = (INDIRECT_REF_P (object)
2353 && integer_zerop (TREE_OPERAND (object, 0)));
2355 /* Convert OBJECT to the type of MEMBER. */
2356 if (!same_type_p (TYPE_MAIN_VARIANT (object_type),
2357 TYPE_MAIN_VARIANT (member_scope)))
2359 tree binfo;
2360 base_kind kind;
2362 binfo = lookup_base (access_path ? access_path : object_type,
2363 member_scope, ba_unique, &kind, complain);
2364 if (binfo == error_mark_node)
2365 return error_mark_node;
2367 /* It is invalid to try to get to a virtual base of a
2368 NULL object. The most common cause is invalid use of
2369 offsetof macro. */
2370 if (null_object_p && kind == bk_via_virtual)
2372 if (complain & tf_error)
2374 error ("invalid access to non-static data member %qD in "
2375 "virtual base of NULL object", member);
2377 return error_mark_node;
2380 /* Convert to the base. */
2381 object = build_base_path (PLUS_EXPR, object, binfo,
2382 /*nonnull=*/1, complain);
2383 /* If we found the base successfully then we should be able
2384 to convert to it successfully. */
2385 gcc_assert (object != error_mark_node);
2388 /* If MEMBER is from an anonymous aggregate, we have converted
2389 OBJECT so that it refers to the class containing the
2390 anonymous union. Generate a reference to the anonymous union
2391 itself, and recur to find MEMBER. */
2392 if (ANON_AGGR_TYPE_P (DECL_CONTEXT (member))
2393 /* When this code is called from build_field_call, the
2394 object already has the type of the anonymous union.
2395 That is because the COMPONENT_REF was already
2396 constructed, and was then disassembled before calling
2397 build_field_call. After the function-call code is
2398 cleaned up, this waste can be eliminated. */
2399 && (!same_type_ignoring_top_level_qualifiers_p
2400 (TREE_TYPE (object), DECL_CONTEXT (member))))
2402 tree anonymous_union;
2404 anonymous_union = lookup_anon_field (TREE_TYPE (object),
2405 DECL_CONTEXT (member));
2406 object = build_class_member_access_expr (object,
2407 anonymous_union,
2408 /*access_path=*/NULL_TREE,
2409 preserve_reference,
2410 complain);
2413 /* Compute the type of the field, as described in [expr.ref]. */
2414 type_quals = TYPE_UNQUALIFIED;
2415 member_type = TREE_TYPE (member);
2416 if (TREE_CODE (member_type) != REFERENCE_TYPE)
2418 type_quals = (cp_type_quals (member_type)
2419 | cp_type_quals (object_type));
2421 /* A field is const (volatile) if the enclosing object, or the
2422 field itself, is const (volatile). But, a mutable field is
2423 not const, even within a const object. */
2424 if (DECL_MUTABLE_P (member))
2425 type_quals &= ~TYPE_QUAL_CONST;
2426 member_type = cp_build_qualified_type (member_type, type_quals);
2429 result = build3_loc (input_location, COMPONENT_REF, member_type,
2430 object, member, NULL_TREE);
2431 result = fold_if_not_in_template (result);
2433 /* Mark the expression const or volatile, as appropriate. Even
2434 though we've dealt with the type above, we still have to mark the
2435 expression itself. */
2436 if (type_quals & TYPE_QUAL_CONST)
2437 TREE_READONLY (result) = 1;
2438 if (type_quals & TYPE_QUAL_VOLATILE)
2439 TREE_THIS_VOLATILE (result) = 1;
2441 else if (BASELINK_P (member))
2443 /* The member is a (possibly overloaded) member function. */
2444 tree functions;
2445 tree type;
2447 /* If the MEMBER is exactly one static member function, then we
2448 know the type of the expression. Otherwise, we must wait
2449 until overload resolution has been performed. */
2450 functions = BASELINK_FUNCTIONS (member);
2451 if (TREE_CODE (functions) == FUNCTION_DECL
2452 && DECL_STATIC_FUNCTION_P (functions))
2453 type = TREE_TYPE (functions);
2454 else
2455 type = unknown_type_node;
2456 /* Note that we do not convert OBJECT to the BASELINK_BINFO
2457 base. That will happen when the function is called. */
2458 result = build3 (COMPONENT_REF, type, object, member, NULL_TREE);
2460 else if (TREE_CODE (member) == CONST_DECL)
2462 /* The member is an enumerator. */
2463 result = member;
2464 /* If OBJECT has side-effects, they are supposed to occur. */
2465 if (TREE_SIDE_EFFECTS (object))
2466 result = build2 (COMPOUND_EXPR, TREE_TYPE (result),
2467 object, result);
2469 else if ((using_decl = strip_using_decl (member)) != member)
2470 result = build_class_member_access_expr (object,
2471 using_decl,
2472 access_path, preserve_reference,
2473 complain);
2474 else
2476 if (complain & tf_error)
2477 error ("invalid use of %qD", member);
2478 return error_mark_node;
2481 if (!preserve_reference)
2482 /* [expr.ref]
2484 If E2 is declared to have type "reference to T", then ... the
2485 type of E1.E2 is T. */
2486 result = convert_from_reference (result);
2488 return result;
2491 /* Return the destructor denoted by OBJECT.SCOPE::DTOR_NAME, or, if
2492 SCOPE is NULL, by OBJECT.DTOR_NAME, where DTOR_NAME is ~type. */
2494 static tree
2495 lookup_destructor (tree object, tree scope, tree dtor_name,
2496 tsubst_flags_t complain)
2498 tree object_type = TREE_TYPE (object);
2499 tree dtor_type = TREE_OPERAND (dtor_name, 0);
2500 tree expr;
2502 /* We've already complained about this destructor. */
2503 if (dtor_type == error_mark_node)
2504 return error_mark_node;
2506 if (scope && !check_dtor_name (scope, dtor_type))
2508 if (complain & tf_error)
2509 error ("qualified type %qT does not match destructor name ~%qT",
2510 scope, dtor_type);
2511 return error_mark_node;
2513 if (is_auto (dtor_type))
2514 dtor_type = object_type;
2515 else if (identifier_p (dtor_type))
2517 /* In a template, names we can't find a match for are still accepted
2518 destructor names, and we check them here. */
2519 if (check_dtor_name (object_type, dtor_type))
2520 dtor_type = object_type;
2521 else
2523 if (complain & tf_error)
2524 error ("object type %qT does not match destructor name ~%qT",
2525 object_type, dtor_type);
2526 return error_mark_node;
2530 else if (!DERIVED_FROM_P (dtor_type, TYPE_MAIN_VARIANT (object_type)))
2532 if (complain & tf_error)
2533 error ("the type being destroyed is %qT, but the destructor "
2534 "refers to %qT", TYPE_MAIN_VARIANT (object_type), dtor_type);
2535 return error_mark_node;
2537 expr = lookup_member (dtor_type, complete_dtor_identifier,
2538 /*protect=*/1, /*want_type=*/false,
2539 tf_warning_or_error);
2540 if (!expr)
2542 if (complain & tf_error)
2543 cxx_incomplete_type_error (dtor_name, dtor_type);
2544 return error_mark_node;
2546 expr = (adjust_result_of_qualified_name_lookup
2547 (expr, dtor_type, object_type));
2548 if (scope == NULL_TREE)
2549 /* We need to call adjust_result_of_qualified_name_lookup in case the
2550 destructor names a base class, but we unset BASELINK_QUALIFIED_P so
2551 that we still get virtual function binding. */
2552 BASELINK_QUALIFIED_P (expr) = false;
2553 return expr;
2556 /* An expression of the form "A::template B" has been resolved to
2557 DECL. Issue a diagnostic if B is not a template or template
2558 specialization. */
2560 void
2561 check_template_keyword (tree decl)
2563 /* The standard says:
2565 [temp.names]
2567 If a name prefixed by the keyword template is not a member
2568 template, the program is ill-formed.
2570 DR 228 removed the restriction that the template be a member
2571 template.
2573 DR 96, if accepted would add the further restriction that explicit
2574 template arguments must be provided if the template keyword is
2575 used, but, as of 2005-10-16, that DR is still in "drafting". If
2576 this DR is accepted, then the semantic checks here can be
2577 simplified, as the entity named must in fact be a template
2578 specialization, rather than, as at present, a set of overloaded
2579 functions containing at least one template function. */
2580 if (TREE_CODE (decl) != TEMPLATE_DECL
2581 && TREE_CODE (decl) != TEMPLATE_ID_EXPR)
2583 if (!is_overloaded_fn (decl))
2584 permerror (input_location, "%qD is not a template", decl);
2585 else
2587 tree fns;
2588 fns = decl;
2589 if (BASELINK_P (fns))
2590 fns = BASELINK_FUNCTIONS (fns);
2591 while (fns)
2593 tree fn = OVL_CURRENT (fns);
2594 if (TREE_CODE (fn) == TEMPLATE_DECL
2595 || TREE_CODE (fn) == TEMPLATE_ID_EXPR)
2596 break;
2597 if (TREE_CODE (fn) == FUNCTION_DECL
2598 && DECL_USE_TEMPLATE (fn)
2599 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (fn)))
2600 break;
2601 fns = OVL_NEXT (fns);
2603 if (!fns)
2604 permerror (input_location, "%qD is not a template", decl);
2609 /* This function is called by the parser to process a class member
2610 access expression of the form OBJECT.NAME. NAME is a node used by
2611 the parser to represent a name; it is not yet a DECL. It may,
2612 however, be a BASELINK where the BASELINK_FUNCTIONS is a
2613 TEMPLATE_ID_EXPR. Templates must be looked up by the parser, and
2614 there is no reason to do the lookup twice, so the parser keeps the
2615 BASELINK. TEMPLATE_P is true iff NAME was explicitly declared to
2616 be a template via the use of the "A::template B" syntax. */
2618 tree
2619 finish_class_member_access_expr (tree object, tree name, bool template_p,
2620 tsubst_flags_t complain)
2622 tree expr;
2623 tree object_type;
2624 tree member;
2625 tree access_path = NULL_TREE;
2626 tree orig_object = object;
2627 tree orig_name = name;
2629 if (object == error_mark_node || name == error_mark_node)
2630 return error_mark_node;
2632 /* If OBJECT is an ObjC class instance, we must obey ObjC access rules. */
2633 if (!objc_is_public (object, name))
2634 return error_mark_node;
2636 object_type = TREE_TYPE (object);
2638 if (processing_template_decl)
2640 if (/* If OBJECT_TYPE is dependent, so is OBJECT.NAME. */
2641 dependent_type_p (object_type)
2642 /* If NAME is just an IDENTIFIER_NODE, then the expression
2643 is dependent. */
2644 || identifier_p (object)
2645 /* If NAME is "f<args>", where either 'f' or 'args' is
2646 dependent, then the expression is dependent. */
2647 || (TREE_CODE (name) == TEMPLATE_ID_EXPR
2648 && dependent_template_id_p (TREE_OPERAND (name, 0),
2649 TREE_OPERAND (name, 1)))
2650 /* If NAME is "T::X" where "T" is dependent, then the
2651 expression is dependent. */
2652 || (TREE_CODE (name) == SCOPE_REF
2653 && TYPE_P (TREE_OPERAND (name, 0))
2654 && dependent_type_p (TREE_OPERAND (name, 0))))
2655 return build_min_nt_loc (UNKNOWN_LOCATION, COMPONENT_REF,
2656 object, name, NULL_TREE);
2657 object = build_non_dependent_expr (object);
2659 else if (c_dialect_objc ()
2660 && identifier_p (name)
2661 && (expr = objc_maybe_build_component_ref (object, name)))
2662 return expr;
2664 /* [expr.ref]
2666 The type of the first expression shall be "class object" (of a
2667 complete type). */
2668 if (!currently_open_class (object_type)
2669 && !complete_type_or_maybe_complain (object_type, object, complain))
2670 return error_mark_node;
2671 if (!CLASS_TYPE_P (object_type))
2673 if (complain & tf_error)
2675 if (POINTER_TYPE_P (object_type)
2676 && CLASS_TYPE_P (TREE_TYPE (object_type)))
2677 error ("request for member %qD in %qE, which is of pointer "
2678 "type %qT (maybe you meant to use %<->%> ?)",
2679 name, object, object_type);
2680 else
2681 error ("request for member %qD in %qE, which is of non-class "
2682 "type %qT", name, object, object_type);
2684 return error_mark_node;
2687 if (BASELINK_P (name))
2688 /* A member function that has already been looked up. */
2689 member = name;
2690 else
2692 bool is_template_id = false;
2693 tree template_args = NULL_TREE;
2694 tree scope;
2696 if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
2698 is_template_id = true;
2699 template_args = TREE_OPERAND (name, 1);
2700 name = TREE_OPERAND (name, 0);
2702 if (TREE_CODE (name) == OVERLOAD)
2703 name = DECL_NAME (get_first_fn (name));
2704 else if (DECL_P (name))
2705 name = DECL_NAME (name);
2708 if (TREE_CODE (name) == SCOPE_REF)
2710 /* A qualified name. The qualifying class or namespace `S'
2711 has already been looked up; it is either a TYPE or a
2712 NAMESPACE_DECL. */
2713 scope = TREE_OPERAND (name, 0);
2714 name = TREE_OPERAND (name, 1);
2716 /* If SCOPE is a namespace, then the qualified name does not
2717 name a member of OBJECT_TYPE. */
2718 if (TREE_CODE (scope) == NAMESPACE_DECL)
2720 if (complain & tf_error)
2721 error ("%<%D::%D%> is not a member of %qT",
2722 scope, name, object_type);
2723 return error_mark_node;
2726 if (TREE_CODE (scope) == ENUMERAL_TYPE)
2728 /* Looking up a member enumerator (c++/56793). */
2729 if (!TYPE_CLASS_SCOPE_P (scope)
2730 || !DERIVED_FROM_P (TYPE_CONTEXT (scope), object_type))
2732 if (complain & tf_error)
2733 error ("%<%D::%D%> is not a member of %qT",
2734 scope, name, object_type);
2735 return error_mark_node;
2737 tree val = lookup_enumerator (scope, name);
2738 if (!val)
2740 if (complain & tf_error)
2741 error ("%qD is not a member of %qD",
2742 name, scope);
2743 return error_mark_node;
2746 if (TREE_SIDE_EFFECTS (object))
2747 val = build2 (COMPOUND_EXPR, TREE_TYPE (val), object, val);
2748 return val;
2751 gcc_assert (CLASS_TYPE_P (scope));
2752 gcc_assert (identifier_p (name) || TREE_CODE (name) == BIT_NOT_EXPR);
2754 if (constructor_name_p (name, scope))
2756 if (complain & tf_error)
2757 error ("cannot call constructor %<%T::%D%> directly",
2758 scope, name);
2759 return error_mark_node;
2762 /* Find the base of OBJECT_TYPE corresponding to SCOPE. */
2763 access_path = lookup_base (object_type, scope, ba_check,
2764 NULL, complain);
2765 if (access_path == error_mark_node)
2766 return error_mark_node;
2767 if (!access_path)
2769 if (complain & tf_error)
2770 error ("%qT is not a base of %qT", scope, object_type);
2771 return error_mark_node;
2774 else
2776 scope = NULL_TREE;
2777 access_path = object_type;
2780 if (TREE_CODE (name) == BIT_NOT_EXPR)
2781 member = lookup_destructor (object, scope, name, complain);
2782 else
2784 /* Look up the member. */
2785 member = lookup_member (access_path, name, /*protect=*/1,
2786 /*want_type=*/false, complain);
2787 if (member == NULL_TREE)
2789 if (complain & tf_error)
2790 error ("%q#T has no member named %qE",
2791 TREE_CODE (access_path) == TREE_BINFO
2792 ? TREE_TYPE (access_path) : object_type, name);
2793 return error_mark_node;
2795 if (member == error_mark_node)
2796 return error_mark_node;
2799 if (is_template_id)
2801 tree templ = member;
2803 if (BASELINK_P (templ))
2804 templ = lookup_template_function (templ, template_args);
2805 else
2807 if (complain & tf_error)
2808 error ("%qD is not a member template function", name);
2809 return error_mark_node;
2814 if (TREE_DEPRECATED (member))
2815 warn_deprecated_use (member, NULL_TREE);
2817 if (template_p)
2818 check_template_keyword (member);
2820 expr = build_class_member_access_expr (object, member, access_path,
2821 /*preserve_reference=*/false,
2822 complain);
2823 if (processing_template_decl && expr != error_mark_node)
2825 if (BASELINK_P (member))
2827 if (TREE_CODE (orig_name) == SCOPE_REF)
2828 BASELINK_QUALIFIED_P (member) = 1;
2829 orig_name = member;
2831 return build_min_non_dep (COMPONENT_REF, expr,
2832 orig_object, orig_name,
2833 NULL_TREE);
2836 return expr;
2839 /* Build a COMPONENT_REF of OBJECT and MEMBER with the appropriate
2840 type. */
2842 tree
2843 build_simple_component_ref (tree object, tree member)
2845 tree type = cp_build_qualified_type (TREE_TYPE (member),
2846 cp_type_quals (TREE_TYPE (object)));
2847 return fold_build3_loc (input_location,
2848 COMPONENT_REF, type,
2849 object, member, NULL_TREE);
2852 /* Return an expression for the MEMBER_NAME field in the internal
2853 representation of PTRMEM, a pointer-to-member function. (Each
2854 pointer-to-member function type gets its own RECORD_TYPE so it is
2855 more convenient to access the fields by name than by FIELD_DECL.)
2856 This routine converts the NAME to a FIELD_DECL and then creates the
2857 node for the complete expression. */
2859 tree
2860 build_ptrmemfunc_access_expr (tree ptrmem, tree member_name)
2862 tree ptrmem_type;
2863 tree member;
2865 /* This code is a stripped down version of
2866 build_class_member_access_expr. It does not work to use that
2867 routine directly because it expects the object to be of class
2868 type. */
2869 ptrmem_type = TREE_TYPE (ptrmem);
2870 gcc_assert (TYPE_PTRMEMFUNC_P (ptrmem_type));
2871 for (member = TYPE_FIELDS (ptrmem_type); member;
2872 member = DECL_CHAIN (member))
2873 if (DECL_NAME (member) == member_name)
2874 break;
2875 return build_simple_component_ref (ptrmem, member);
2878 /* Given an expression PTR for a pointer, return an expression
2879 for the value pointed to.
2880 ERRORSTRING is the name of the operator to appear in error messages.
2882 This function may need to overload OPERATOR_FNNAME.
2883 Must also handle REFERENCE_TYPEs for C++. */
2885 tree
2886 build_x_indirect_ref (location_t loc, tree expr, ref_operator errorstring,
2887 tsubst_flags_t complain)
2889 tree orig_expr = expr;
2890 tree rval;
2892 if (processing_template_decl)
2894 /* Retain the type if we know the operand is a pointer. */
2895 if (TREE_TYPE (expr) && POINTER_TYPE_P (TREE_TYPE (expr)))
2896 return build_min (INDIRECT_REF, TREE_TYPE (TREE_TYPE (expr)), expr);
2897 if (type_dependent_expression_p (expr))
2898 return build_min_nt_loc (loc, INDIRECT_REF, expr);
2899 expr = build_non_dependent_expr (expr);
2902 rval = build_new_op (loc, INDIRECT_REF, LOOKUP_NORMAL, expr,
2903 NULL_TREE, NULL_TREE, /*overload=*/NULL, complain);
2904 if (!rval)
2905 rval = cp_build_indirect_ref (expr, errorstring, complain);
2907 if (processing_template_decl && rval != error_mark_node)
2908 return build_min_non_dep (INDIRECT_REF, rval, orig_expr);
2909 else
2910 return rval;
2913 /* Helper function called from c-common. */
2914 tree
2915 build_indirect_ref (location_t /*loc*/,
2916 tree ptr, ref_operator errorstring)
2918 return cp_build_indirect_ref (ptr, errorstring, tf_warning_or_error);
2921 tree
2922 cp_build_indirect_ref (tree ptr, ref_operator errorstring,
2923 tsubst_flags_t complain)
2925 tree pointer, type;
2927 if (ptr == current_class_ptr
2928 || (TREE_CODE (ptr) == NOP_EXPR
2929 && TREE_OPERAND (ptr, 0) == current_class_ptr
2930 && (same_type_ignoring_top_level_qualifiers_p
2931 (TREE_TYPE (ptr), TREE_TYPE (current_class_ptr)))))
2932 return current_class_ref;
2934 pointer = (TREE_CODE (TREE_TYPE (ptr)) == REFERENCE_TYPE
2935 ? ptr : decay_conversion (ptr, complain));
2936 if (pointer == error_mark_node)
2937 return error_mark_node;
2939 type = TREE_TYPE (pointer);
2941 if (POINTER_TYPE_P (type))
2943 /* [expr.unary.op]
2945 If the type of the expression is "pointer to T," the type
2946 of the result is "T." */
2947 tree t = TREE_TYPE (type);
2949 if ((CONVERT_EXPR_P (ptr)
2950 || TREE_CODE (ptr) == VIEW_CONVERT_EXPR)
2951 && (!CLASS_TYPE_P (t) || !CLASSTYPE_EMPTY_P (t)))
2953 /* If a warning is issued, mark it to avoid duplicates from
2954 the backend. This only needs to be done at
2955 warn_strict_aliasing > 2. */
2956 if (warn_strict_aliasing > 2)
2957 if (strict_aliasing_warning (TREE_TYPE (TREE_OPERAND (ptr, 0)),
2958 type, TREE_OPERAND (ptr, 0)))
2959 TREE_NO_WARNING (ptr) = 1;
2962 if (VOID_TYPE_P (t))
2964 /* A pointer to incomplete type (other than cv void) can be
2965 dereferenced [expr.unary.op]/1 */
2966 if (complain & tf_error)
2967 error ("%qT is not a pointer-to-object type", type);
2968 return error_mark_node;
2970 else if (TREE_CODE (pointer) == ADDR_EXPR
2971 && same_type_p (t, TREE_TYPE (TREE_OPERAND (pointer, 0))))
2972 /* The POINTER was something like `&x'. We simplify `*&x' to
2973 `x'. */
2974 return TREE_OPERAND (pointer, 0);
2975 else
2977 tree ref = build1 (INDIRECT_REF, t, pointer);
2979 /* We *must* set TREE_READONLY when dereferencing a pointer to const,
2980 so that we get the proper error message if the result is used
2981 to assign to. Also, &* is supposed to be a no-op. */
2982 TREE_READONLY (ref) = CP_TYPE_CONST_P (t);
2983 TREE_THIS_VOLATILE (ref) = CP_TYPE_VOLATILE_P (t);
2984 TREE_SIDE_EFFECTS (ref)
2985 = (TREE_THIS_VOLATILE (ref) || TREE_SIDE_EFFECTS (pointer));
2986 return ref;
2989 else if (!(complain & tf_error))
2990 /* Don't emit any errors; we'll just return ERROR_MARK_NODE later. */
2992 /* `pointer' won't be an error_mark_node if we were given a
2993 pointer to member, so it's cool to check for this here. */
2994 else if (TYPE_PTRMEM_P (type))
2995 switch (errorstring)
2997 case RO_ARRAY_INDEXING:
2998 error ("invalid use of array indexing on pointer to member");
2999 break;
3000 case RO_UNARY_STAR:
3001 error ("invalid use of unary %<*%> on pointer to member");
3002 break;
3003 case RO_IMPLICIT_CONVERSION:
3004 error ("invalid use of implicit conversion on pointer to member");
3005 break;
3006 case RO_ARROW_STAR:
3007 error ("left hand operand of %<->*%> must be a pointer to class, "
3008 "but is a pointer to member of type %qT", type);
3009 break;
3010 default:
3011 gcc_unreachable ();
3013 else if (pointer != error_mark_node)
3014 invalid_indirection_error (input_location, type, errorstring);
3016 return error_mark_node;
3019 /* This handles expressions of the form "a[i]", which denotes
3020 an array reference.
3022 This is logically equivalent in C to *(a+i), but we may do it differently.
3023 If A is a variable or a member, we generate a primitive ARRAY_REF.
3024 This avoids forcing the array out of registers, and can work on
3025 arrays that are not lvalues (for example, members of structures returned
3026 by functions).
3028 If INDEX is of some user-defined type, it must be converted to
3029 integer type. Otherwise, to make a compatible PLUS_EXPR, it
3030 will inherit the type of the array, which will be some pointer type.
3032 LOC is the location to use in building the array reference. */
3034 tree
3035 cp_build_array_ref (location_t loc, tree array, tree idx,
3036 tsubst_flags_t complain)
3038 tree ret;
3040 if (idx == 0)
3042 if (complain & tf_error)
3043 error_at (loc, "subscript missing in array reference");
3044 return error_mark_node;
3047 /* If an array's index is an array notation, then its rank cannot be
3048 greater than one. */
3049 if (flag_cilkplus && contains_array_notation_expr (idx))
3051 size_t rank = 0;
3053 /* If find_rank returns false, then it should have reported an error,
3054 thus it is unnecessary for repetition. */
3055 if (!find_rank (loc, idx, idx, true, &rank))
3056 return error_mark_node;
3057 if (rank > 1)
3059 error_at (loc, "rank of the array%'s index is greater than 1");
3060 return error_mark_node;
3063 if (TREE_TYPE (array) == error_mark_node
3064 || TREE_TYPE (idx) == error_mark_node)
3065 return error_mark_node;
3067 /* If ARRAY is a COMPOUND_EXPR or COND_EXPR, move our reference
3068 inside it. */
3069 switch (TREE_CODE (array))
3071 case COMPOUND_EXPR:
3073 tree value = cp_build_array_ref (loc, TREE_OPERAND (array, 1), idx,
3074 complain);
3075 ret = build2 (COMPOUND_EXPR, TREE_TYPE (value),
3076 TREE_OPERAND (array, 0), value);
3077 SET_EXPR_LOCATION (ret, loc);
3078 return ret;
3081 case COND_EXPR:
3082 ret = build_conditional_expr
3083 (loc, TREE_OPERAND (array, 0),
3084 cp_build_array_ref (loc, TREE_OPERAND (array, 1), idx,
3085 complain),
3086 cp_build_array_ref (loc, TREE_OPERAND (array, 2), idx,
3087 complain),
3088 complain);
3089 protected_set_expr_location (ret, loc);
3090 return ret;
3092 default:
3093 break;
3096 bool non_lvalue
3097 = convert_vector_to_pointer_for_subscript (loc, &array, idx);
3099 if (TREE_CODE (TREE_TYPE (array)) == ARRAY_TYPE)
3101 tree rval, type;
3103 warn_array_subscript_with_type_char (loc, idx);
3105 if (!INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (TREE_TYPE (idx)))
3107 if (complain & tf_error)
3108 error_at (loc, "array subscript is not an integer");
3109 return error_mark_node;
3112 /* Apply integral promotions *after* noticing character types.
3113 (It is unclear why we do these promotions -- the standard
3114 does not say that we should. In fact, the natural thing would
3115 seem to be to convert IDX to ptrdiff_t; we're performing
3116 pointer arithmetic.) */
3117 idx = cp_perform_integral_promotions (idx, complain);
3119 /* An array that is indexed by a non-constant
3120 cannot be stored in a register; we must be able to do
3121 address arithmetic on its address.
3122 Likewise an array of elements of variable size. */
3123 if (TREE_CODE (idx) != INTEGER_CST
3124 || (COMPLETE_TYPE_P (TREE_TYPE (TREE_TYPE (array)))
3125 && (TREE_CODE (TYPE_SIZE (TREE_TYPE (TREE_TYPE (array))))
3126 != INTEGER_CST)))
3128 if (!cxx_mark_addressable (array))
3129 return error_mark_node;
3132 /* An array that is indexed by a constant value which is not within
3133 the array bounds cannot be stored in a register either; because we
3134 would get a crash in store_bit_field/extract_bit_field when trying
3135 to access a non-existent part of the register. */
3136 if (TREE_CODE (idx) == INTEGER_CST
3137 && TYPE_DOMAIN (TREE_TYPE (array))
3138 && ! int_fits_type_p (idx, TYPE_DOMAIN (TREE_TYPE (array))))
3140 if (!cxx_mark_addressable (array))
3141 return error_mark_node;
3144 /* Note in C++ it is valid to subscript a `register' array, since
3145 it is valid to take the address of something with that
3146 storage specification. */
3147 if (extra_warnings)
3149 tree foo = array;
3150 while (TREE_CODE (foo) == COMPONENT_REF)
3151 foo = TREE_OPERAND (foo, 0);
3152 if (VAR_P (foo) && DECL_REGISTER (foo)
3153 && (complain & tf_warning))
3154 warning_at (loc, OPT_Wextra,
3155 "subscripting array declared %<register%>");
3158 type = TREE_TYPE (TREE_TYPE (array));
3159 rval = build4 (ARRAY_REF, type, array, idx, NULL_TREE, NULL_TREE);
3160 /* Array ref is const/volatile if the array elements are
3161 or if the array is.. */
3162 TREE_READONLY (rval)
3163 |= (CP_TYPE_CONST_P (type) | TREE_READONLY (array));
3164 TREE_SIDE_EFFECTS (rval)
3165 |= (CP_TYPE_VOLATILE_P (type) | TREE_SIDE_EFFECTS (array));
3166 TREE_THIS_VOLATILE (rval)
3167 |= (CP_TYPE_VOLATILE_P (type) | TREE_THIS_VOLATILE (array));
3168 ret = require_complete_type_sfinae (fold_if_not_in_template (rval),
3169 complain);
3170 protected_set_expr_location (ret, loc);
3171 if (non_lvalue)
3172 ret = non_lvalue_loc (loc, ret);
3173 return ret;
3177 tree ar = cp_default_conversion (array, complain);
3178 tree ind = cp_default_conversion (idx, complain);
3180 /* Put the integer in IND to simplify error checking. */
3181 if (TREE_CODE (TREE_TYPE (ar)) == INTEGER_TYPE)
3182 std::swap (ar, ind);
3184 if (ar == error_mark_node || ind == error_mark_node)
3185 return error_mark_node;
3187 if (!TYPE_PTR_P (TREE_TYPE (ar)))
3189 if (complain & tf_error)
3190 error_at (loc, "subscripted value is neither array nor pointer");
3191 return error_mark_node;
3193 if (TREE_CODE (TREE_TYPE (ind)) != INTEGER_TYPE)
3195 if (complain & tf_error)
3196 error_at (loc, "array subscript is not an integer");
3197 return error_mark_node;
3200 warn_array_subscript_with_type_char (loc, idx);
3202 ret = cp_build_indirect_ref (cp_build_binary_op (input_location,
3203 PLUS_EXPR, ar, ind,
3204 complain),
3205 RO_ARRAY_INDEXING,
3206 complain);
3207 protected_set_expr_location (ret, loc);
3208 if (non_lvalue)
3209 ret = non_lvalue_loc (loc, ret);
3210 return ret;
3214 /* Entry point for Obj-C++. */
3216 tree
3217 build_array_ref (location_t loc, tree array, tree idx)
3219 return cp_build_array_ref (loc, array, idx, tf_warning_or_error);
3222 /* Resolve a pointer to member function. INSTANCE is the object
3223 instance to use, if the member points to a virtual member.
3225 This used to avoid checking for virtual functions if basetype
3226 has no virtual functions, according to an earlier ANSI draft.
3227 With the final ISO C++ rules, such an optimization is
3228 incorrect: A pointer to a derived member can be static_cast
3229 to pointer-to-base-member, as long as the dynamic object
3230 later has the right member. So now we only do this optimization
3231 when we know the dynamic type of the object. */
3233 tree
3234 get_member_function_from_ptrfunc (tree *instance_ptrptr, tree function,
3235 tsubst_flags_t complain)
3237 if (TREE_CODE (function) == OFFSET_REF)
3238 function = TREE_OPERAND (function, 1);
3240 if (TYPE_PTRMEMFUNC_P (TREE_TYPE (function)))
3242 tree idx, delta, e1, e2, e3, vtbl;
3243 bool nonvirtual;
3244 tree fntype = TYPE_PTRMEMFUNC_FN_TYPE (TREE_TYPE (function));
3245 tree basetype = TYPE_METHOD_BASETYPE (TREE_TYPE (fntype));
3247 tree instance_ptr = *instance_ptrptr;
3248 tree instance_save_expr = 0;
3249 if (instance_ptr == error_mark_node)
3251 if (TREE_CODE (function) == PTRMEM_CST)
3253 /* Extracting the function address from a pmf is only
3254 allowed with -Wno-pmf-conversions. It only works for
3255 pmf constants. */
3256 e1 = build_addr_func (PTRMEM_CST_MEMBER (function), complain);
3257 e1 = convert (fntype, e1);
3258 return e1;
3260 else
3262 if (complain & tf_error)
3263 error ("object missing in use of %qE", function);
3264 return error_mark_node;
3268 /* True if we know that the dynamic type of the object doesn't have
3269 virtual functions, so we can assume the PFN field is a pointer. */
3270 nonvirtual = (COMPLETE_TYPE_P (basetype)
3271 && !TYPE_POLYMORPHIC_P (basetype)
3272 && resolves_to_fixed_type_p (instance_ptr, 0));
3274 /* If we don't really have an object (i.e. in an ill-formed
3275 conversion from PMF to pointer), we can't resolve virtual
3276 functions anyway. */
3277 if (!nonvirtual && is_dummy_object (instance_ptr))
3278 nonvirtual = true;
3280 if (TREE_SIDE_EFFECTS (instance_ptr))
3281 instance_ptr = instance_save_expr = save_expr (instance_ptr);
3283 if (TREE_SIDE_EFFECTS (function))
3284 function = save_expr (function);
3286 /* Start by extracting all the information from the PMF itself. */
3287 e3 = pfn_from_ptrmemfunc (function);
3288 delta = delta_from_ptrmemfunc (function);
3289 idx = build1 (NOP_EXPR, vtable_index_type, e3);
3290 switch (TARGET_PTRMEMFUNC_VBIT_LOCATION)
3292 case ptrmemfunc_vbit_in_pfn:
3293 e1 = cp_build_binary_op (input_location,
3294 BIT_AND_EXPR, idx, integer_one_node,
3295 complain);
3296 idx = cp_build_binary_op (input_location,
3297 MINUS_EXPR, idx, integer_one_node,
3298 complain);
3299 if (idx == error_mark_node)
3300 return error_mark_node;
3301 break;
3303 case ptrmemfunc_vbit_in_delta:
3304 e1 = cp_build_binary_op (input_location,
3305 BIT_AND_EXPR, delta, integer_one_node,
3306 complain);
3307 delta = cp_build_binary_op (input_location,
3308 RSHIFT_EXPR, delta, integer_one_node,
3309 complain);
3310 if (delta == error_mark_node)
3311 return error_mark_node;
3312 break;
3314 default:
3315 gcc_unreachable ();
3318 if (e1 == error_mark_node)
3319 return error_mark_node;
3321 /* Convert down to the right base before using the instance. A
3322 special case is that in a pointer to member of class C, C may
3323 be incomplete. In that case, the function will of course be
3324 a member of C, and no conversion is required. In fact,
3325 lookup_base will fail in that case, because incomplete
3326 classes do not have BINFOs. */
3327 if (!same_type_ignoring_top_level_qualifiers_p
3328 (basetype, TREE_TYPE (TREE_TYPE (instance_ptr))))
3330 basetype = lookup_base (TREE_TYPE (TREE_TYPE (instance_ptr)),
3331 basetype, ba_check, NULL, complain);
3332 instance_ptr = build_base_path (PLUS_EXPR, instance_ptr, basetype,
3333 1, complain);
3334 if (instance_ptr == error_mark_node)
3335 return error_mark_node;
3337 /* ...and then the delta in the PMF. */
3338 instance_ptr = fold_build_pointer_plus (instance_ptr, delta);
3340 /* Hand back the adjusted 'this' argument to our caller. */
3341 *instance_ptrptr = instance_ptr;
3343 if (nonvirtual)
3344 /* Now just return the pointer. */
3345 return e3;
3347 /* Next extract the vtable pointer from the object. */
3348 vtbl = build1 (NOP_EXPR, build_pointer_type (vtbl_ptr_type_node),
3349 instance_ptr);
3350 vtbl = cp_build_indirect_ref (vtbl, RO_NULL, complain);
3351 if (vtbl == error_mark_node)
3352 return error_mark_node;
3354 /* Finally, extract the function pointer from the vtable. */
3355 e2 = fold_build_pointer_plus_loc (input_location, vtbl, idx);
3356 e2 = cp_build_indirect_ref (e2, RO_NULL, complain);
3357 if (e2 == error_mark_node)
3358 return error_mark_node;
3359 TREE_CONSTANT (e2) = 1;
3361 /* When using function descriptors, the address of the
3362 vtable entry is treated as a function pointer. */
3363 if (TARGET_VTABLE_USES_DESCRIPTORS)
3364 e2 = build1 (NOP_EXPR, TREE_TYPE (e2),
3365 cp_build_addr_expr (e2, complain));
3367 e2 = fold_convert (TREE_TYPE (e3), e2);
3368 e1 = build_conditional_expr (input_location, e1, e2, e3, complain);
3369 if (e1 == error_mark_node)
3370 return error_mark_node;
3372 /* Make sure this doesn't get evaluated first inside one of the
3373 branches of the COND_EXPR. */
3374 if (instance_save_expr)
3375 e1 = build2 (COMPOUND_EXPR, TREE_TYPE (e1),
3376 instance_save_expr, e1);
3378 function = e1;
3380 return function;
3383 /* Used by the C-common bits. */
3384 tree
3385 build_function_call (location_t /*loc*/,
3386 tree function, tree params)
3388 return cp_build_function_call (function, params, tf_warning_or_error);
3391 /* Used by the C-common bits. */
3392 tree
3393 build_function_call_vec (location_t /*loc*/, vec<location_t> /*arg_loc*/,
3394 tree function, vec<tree, va_gc> *params,
3395 vec<tree, va_gc> * /*origtypes*/)
3397 vec<tree, va_gc> *orig_params = params;
3398 tree ret = cp_build_function_call_vec (function, &params,
3399 tf_warning_or_error);
3401 /* cp_build_function_call_vec can reallocate PARAMS by adding
3402 default arguments. That should never happen here. Verify
3403 that. */
3404 gcc_assert (params == orig_params);
3406 return ret;
3409 /* Build a function call using a tree list of arguments. */
3411 static tree
3412 cp_build_function_call (tree function, tree params, tsubst_flags_t complain)
3414 vec<tree, va_gc> *vec;
3415 tree ret;
3417 vec = make_tree_vector ();
3418 for (; params != NULL_TREE; params = TREE_CHAIN (params))
3419 vec_safe_push (vec, TREE_VALUE (params));
3420 ret = cp_build_function_call_vec (function, &vec, complain);
3421 release_tree_vector (vec);
3422 return ret;
3425 /* Build a function call using varargs. */
3427 tree
3428 cp_build_function_call_nary (tree function, tsubst_flags_t complain, ...)
3430 vec<tree, va_gc> *vec;
3431 va_list args;
3432 tree ret, t;
3434 vec = make_tree_vector ();
3435 va_start (args, complain);
3436 for (t = va_arg (args, tree); t != NULL_TREE; t = va_arg (args, tree))
3437 vec_safe_push (vec, t);
3438 va_end (args);
3439 ret = cp_build_function_call_vec (function, &vec, complain);
3440 release_tree_vector (vec);
3441 return ret;
3444 /* Build a function call using a vector of arguments. PARAMS may be
3445 NULL if there are no parameters. This changes the contents of
3446 PARAMS. */
3448 tree
3449 cp_build_function_call_vec (tree function, vec<tree, va_gc> **params,
3450 tsubst_flags_t complain)
3452 tree fntype, fndecl;
3453 int is_method;
3454 tree original = function;
3455 int nargs;
3456 tree *argarray;
3457 tree parm_types;
3458 vec<tree, va_gc> *allocated = NULL;
3459 tree ret;
3461 /* For Objective-C, convert any calls via a cast to OBJC_TYPE_REF
3462 expressions, like those used for ObjC messenger dispatches. */
3463 if (params != NULL && !vec_safe_is_empty (*params))
3464 function = objc_rewrite_function_call (function, (**params)[0]);
3466 /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
3467 Strip such NOP_EXPRs, since FUNCTION is used in non-lvalue context. */
3468 if (TREE_CODE (function) == NOP_EXPR
3469 && TREE_TYPE (function) == TREE_TYPE (TREE_OPERAND (function, 0)))
3470 function = TREE_OPERAND (function, 0);
3472 if (TREE_CODE (function) == FUNCTION_DECL)
3474 if (!mark_used (function, complain) && !(complain & tf_error))
3475 return error_mark_node;
3476 fndecl = function;
3478 /* Convert anything with function type to a pointer-to-function. */
3479 if (DECL_MAIN_P (function))
3481 if (complain & tf_error)
3482 pedwarn (input_location, OPT_Wpedantic,
3483 "ISO C++ forbids calling %<::main%> from within program");
3484 else
3485 return error_mark_node;
3487 function = build_addr_func (function, complain);
3489 else
3491 fndecl = NULL_TREE;
3493 function = build_addr_func (function, complain);
3496 if (function == error_mark_node)
3497 return error_mark_node;
3499 fntype = TREE_TYPE (function);
3501 if (TYPE_PTRMEMFUNC_P (fntype))
3503 if (complain & tf_error)
3504 error ("must use %<.*%> or %<->*%> to call pointer-to-member "
3505 "function in %<%E (...)%>, e.g. %<(... ->* %E) (...)%>",
3506 original, original);
3507 return error_mark_node;
3510 is_method = (TYPE_PTR_P (fntype)
3511 && TREE_CODE (TREE_TYPE (fntype)) == METHOD_TYPE);
3513 if (!(TYPE_PTRFN_P (fntype)
3514 || is_method
3515 || TREE_CODE (function) == TEMPLATE_ID_EXPR))
3517 if (complain & tf_error)
3519 if (!flag_diagnostics_show_caret)
3520 error_at (input_location,
3521 "%qE cannot be used as a function", original);
3522 else if (DECL_P (original))
3523 error_at (input_location,
3524 "%qD cannot be used as a function", original);
3525 else
3526 error_at (input_location,
3527 "expression cannot be used as a function");
3530 return error_mark_node;
3533 /* fntype now gets the type of function pointed to. */
3534 fntype = TREE_TYPE (fntype);
3535 parm_types = TYPE_ARG_TYPES (fntype);
3537 if (params == NULL)
3539 allocated = make_tree_vector ();
3540 params = &allocated;
3543 nargs = convert_arguments (parm_types, params, fndecl, LOOKUP_NORMAL,
3544 complain);
3545 if (nargs < 0)
3546 return error_mark_node;
3548 argarray = (*params)->address ();
3550 /* Check for errors in format strings and inappropriately
3551 null parameters. */
3552 check_function_arguments (fntype, nargs, argarray);
3554 ret = build_cxx_call (function, nargs, argarray, complain);
3556 if (allocated != NULL)
3557 release_tree_vector (allocated);
3559 return ret;
3562 /* Subroutine of convert_arguments.
3563 Warn about wrong number of args are genereted. */
3565 static void
3566 warn_args_num (location_t loc, tree fndecl, bool too_many_p)
3568 if (fndecl)
3570 if (TREE_CODE (TREE_TYPE (fndecl)) == METHOD_TYPE)
3572 if (DECL_NAME (fndecl) == NULL_TREE
3573 || IDENTIFIER_HAS_TYPE_VALUE (DECL_NAME (fndecl)))
3574 error_at (loc,
3575 too_many_p
3576 ? G_("too many arguments to constructor %q#D")
3577 : G_("too few arguments to constructor %q#D"),
3578 fndecl);
3579 else
3580 error_at (loc,
3581 too_many_p
3582 ? G_("too many arguments to member function %q#D")
3583 : G_("too few arguments to member function %q#D"),
3584 fndecl);
3586 else
3587 error_at (loc,
3588 too_many_p
3589 ? G_("too many arguments to function %q#D")
3590 : G_("too few arguments to function %q#D"),
3591 fndecl);
3592 if (!DECL_IS_BUILTIN (fndecl))
3593 inform (DECL_SOURCE_LOCATION (fndecl), "declared here");
3595 else
3597 if (c_dialect_objc () && objc_message_selector ())
3598 error_at (loc,
3599 too_many_p
3600 ? G_("too many arguments to method %q#D")
3601 : G_("too few arguments to method %q#D"),
3602 objc_message_selector ());
3603 else
3604 error_at (loc, too_many_p ? G_("too many arguments to function")
3605 : G_("too few arguments to function"));
3609 /* Convert the actual parameter expressions in the list VALUES to the
3610 types in the list TYPELIST. The converted expressions are stored
3611 back in the VALUES vector.
3612 If parmdecls is exhausted, or when an element has NULL as its type,
3613 perform the default conversions.
3615 NAME is an IDENTIFIER_NODE or 0. It is used only for error messages.
3617 This is also where warnings about wrong number of args are generated.
3619 Returns the actual number of arguments processed (which might be less
3620 than the length of the vector), or -1 on error.
3622 In C++, unspecified trailing parameters can be filled in with their
3623 default arguments, if such were specified. Do so here. */
3625 static int
3626 convert_arguments (tree typelist, vec<tree, va_gc> **values, tree fndecl,
3627 int flags, tsubst_flags_t complain)
3629 tree typetail;
3630 unsigned int i;
3632 /* Argument passing is always copy-initialization. */
3633 flags |= LOOKUP_ONLYCONVERTING;
3635 for (i = 0, typetail = typelist;
3636 i < vec_safe_length (*values);
3637 i++)
3639 tree type = typetail ? TREE_VALUE (typetail) : 0;
3640 tree val = (**values)[i];
3642 if (val == error_mark_node || type == error_mark_node)
3643 return -1;
3645 if (type == void_type_node)
3647 if (complain & tf_error)
3649 warn_args_num (input_location, fndecl, /*too_many_p=*/true);
3650 return i;
3652 else
3653 return -1;
3656 /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
3657 Strip such NOP_EXPRs, since VAL is used in non-lvalue context. */
3658 if (TREE_CODE (val) == NOP_EXPR
3659 && TREE_TYPE (val) == TREE_TYPE (TREE_OPERAND (val, 0))
3660 && (type == 0 || TREE_CODE (type) != REFERENCE_TYPE))
3661 val = TREE_OPERAND (val, 0);
3663 if (type == 0 || TREE_CODE (type) != REFERENCE_TYPE)
3665 if (TREE_CODE (TREE_TYPE (val)) == ARRAY_TYPE
3666 || TREE_CODE (TREE_TYPE (val)) == FUNCTION_TYPE
3667 || TREE_CODE (TREE_TYPE (val)) == METHOD_TYPE)
3668 val = decay_conversion (val, complain);
3671 if (val == error_mark_node)
3672 return -1;
3674 if (type != 0)
3676 /* Formal parm type is specified by a function prototype. */
3677 tree parmval;
3679 if (!COMPLETE_TYPE_P (complete_type (type)))
3681 if (complain & tf_error)
3683 if (fndecl)
3684 error ("parameter %P of %qD has incomplete type %qT",
3685 i, fndecl, type);
3686 else
3687 error ("parameter %P has incomplete type %qT", i, type);
3689 parmval = error_mark_node;
3691 else
3693 parmval = convert_for_initialization
3694 (NULL_TREE, type, val, flags,
3695 ICR_ARGPASS, fndecl, i, complain);
3696 parmval = convert_for_arg_passing (type, parmval, complain);
3699 if (parmval == error_mark_node)
3700 return -1;
3702 (**values)[i] = parmval;
3704 else
3706 if (fndecl && magic_varargs_p (fndecl))
3707 /* Don't do ellipsis conversion for __built_in_constant_p
3708 as this will result in spurious errors for non-trivial
3709 types. */
3710 val = require_complete_type_sfinae (val, complain);
3711 else
3712 val = convert_arg_to_ellipsis (val, complain);
3714 (**values)[i] = val;
3717 if (typetail)
3718 typetail = TREE_CHAIN (typetail);
3721 if (typetail != 0 && typetail != void_list_node)
3723 /* See if there are default arguments that can be used. Because
3724 we hold default arguments in the FUNCTION_TYPE (which is so
3725 wrong), we can see default parameters here from deduced
3726 contexts (and via typeof) for indirect function calls.
3727 Fortunately we know whether we have a function decl to
3728 provide default arguments in a language conformant
3729 manner. */
3730 if (fndecl && TREE_PURPOSE (typetail)
3731 && TREE_CODE (TREE_PURPOSE (typetail)) != DEFAULT_ARG)
3733 for (; typetail != void_list_node; ++i)
3735 tree parmval
3736 = convert_default_arg (TREE_VALUE (typetail),
3737 TREE_PURPOSE (typetail),
3738 fndecl, i, complain);
3740 if (parmval == error_mark_node)
3741 return -1;
3743 vec_safe_push (*values, parmval);
3744 typetail = TREE_CHAIN (typetail);
3745 /* ends with `...'. */
3746 if (typetail == NULL_TREE)
3747 break;
3750 else
3752 if (complain & tf_error)
3753 warn_args_num (input_location, fndecl, /*too_many_p=*/false);
3754 return -1;
3758 return (int) i;
3761 /* Build a binary-operation expression, after performing default
3762 conversions on the operands. CODE is the kind of expression to
3763 build. ARG1 and ARG2 are the arguments. ARG1_CODE and ARG2_CODE
3764 are the tree codes which correspond to ARG1 and ARG2 when issuing
3765 warnings about possibly misplaced parentheses. They may differ
3766 from the TREE_CODE of ARG1 and ARG2 if the parser has done constant
3767 folding (e.g., if the parser sees "a | 1 + 1", it may call this
3768 routine with ARG2 being an INTEGER_CST and ARG2_CODE == PLUS_EXPR).
3769 To avoid issuing any parentheses warnings, pass ARG1_CODE and/or
3770 ARG2_CODE as ERROR_MARK. */
3772 tree
3773 build_x_binary_op (location_t loc, enum tree_code code, tree arg1,
3774 enum tree_code arg1_code, tree arg2,
3775 enum tree_code arg2_code, tree *overload,
3776 tsubst_flags_t complain)
3778 tree orig_arg1;
3779 tree orig_arg2;
3780 tree expr;
3782 orig_arg1 = arg1;
3783 orig_arg2 = arg2;
3785 if (processing_template_decl)
3787 if (type_dependent_expression_p (arg1)
3788 || type_dependent_expression_p (arg2))
3789 return build_min_nt_loc (loc, code, arg1, arg2);
3790 arg1 = build_non_dependent_expr (arg1);
3791 arg2 = build_non_dependent_expr (arg2);
3794 if (code == DOTSTAR_EXPR)
3795 expr = build_m_component_ref (arg1, arg2, complain);
3796 else
3797 expr = build_new_op (loc, code, LOOKUP_NORMAL, arg1, arg2, NULL_TREE,
3798 overload, complain);
3800 /* Check for cases such as x+y<<z which users are likely to
3801 misinterpret. But don't warn about obj << x + y, since that is a
3802 common idiom for I/O. */
3803 if (warn_parentheses
3804 && (complain & tf_warning)
3805 && !processing_template_decl
3806 && !error_operand_p (arg1)
3807 && !error_operand_p (arg2)
3808 && (code != LSHIFT_EXPR
3809 || !CLASS_TYPE_P (TREE_TYPE (arg1))))
3810 warn_about_parentheses (loc, code, arg1_code, orig_arg1,
3811 arg2_code, orig_arg2);
3813 if (processing_template_decl && expr != error_mark_node)
3814 return build_min_non_dep (code, expr, orig_arg1, orig_arg2);
3816 return expr;
3819 /* Build and return an ARRAY_REF expression. */
3821 tree
3822 build_x_array_ref (location_t loc, tree arg1, tree arg2,
3823 tsubst_flags_t complain)
3825 tree orig_arg1 = arg1;
3826 tree orig_arg2 = arg2;
3827 tree expr;
3829 if (processing_template_decl)
3831 if (type_dependent_expression_p (arg1)
3832 || type_dependent_expression_p (arg2))
3833 return build_min_nt_loc (loc, ARRAY_REF, arg1, arg2,
3834 NULL_TREE, NULL_TREE);
3835 arg1 = build_non_dependent_expr (arg1);
3836 arg2 = build_non_dependent_expr (arg2);
3839 expr = build_new_op (loc, ARRAY_REF, LOOKUP_NORMAL, arg1, arg2,
3840 NULL_TREE, /*overload=*/NULL, complain);
3842 if (processing_template_decl && expr != error_mark_node)
3843 return build_min_non_dep (ARRAY_REF, expr, orig_arg1, orig_arg2,
3844 NULL_TREE, NULL_TREE);
3845 return expr;
3848 /* Return whether OP is an expression of enum type cast to integer
3849 type. In C++ even unsigned enum types are cast to signed integer
3850 types. We do not want to issue warnings about comparisons between
3851 signed and unsigned types when one of the types is an enum type.
3852 Those warnings are always false positives in practice. */
3854 static bool
3855 enum_cast_to_int (tree op)
3857 if (CONVERT_EXPR_P (op)
3858 && TREE_TYPE (op) == integer_type_node
3859 && TREE_CODE (TREE_TYPE (TREE_OPERAND (op, 0))) == ENUMERAL_TYPE
3860 && TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (op, 0))))
3861 return true;
3863 /* The cast may have been pushed into a COND_EXPR. */
3864 if (TREE_CODE (op) == COND_EXPR)
3865 return (enum_cast_to_int (TREE_OPERAND (op, 1))
3866 || enum_cast_to_int (TREE_OPERAND (op, 2)));
3868 return false;
3871 /* For the c-common bits. */
3872 tree
3873 build_binary_op (location_t location, enum tree_code code, tree op0, tree op1,
3874 int /*convert_p*/)
3876 return cp_build_binary_op (location, code, op0, op1, tf_warning_or_error);
3880 /* Build a binary-operation expression without default conversions.
3881 CODE is the kind of expression to build.
3882 LOCATION is the location_t of the operator in the source code.
3883 This function differs from `build' in several ways:
3884 the data type of the result is computed and recorded in it,
3885 warnings are generated if arg data types are invalid,
3886 special handling for addition and subtraction of pointers is known,
3887 and some optimization is done (operations on narrow ints
3888 are done in the narrower type when that gives the same result).
3889 Constant folding is also done before the result is returned.
3891 Note that the operands will never have enumeral types
3892 because either they have just had the default conversions performed
3893 or they have both just been converted to some other type in which
3894 the arithmetic is to be done.
3896 C++: must do special pointer arithmetic when implementing
3897 multiple inheritance, and deal with pointer to member functions. */
3899 tree
3900 cp_build_binary_op (location_t location,
3901 enum tree_code code, tree orig_op0, tree orig_op1,
3902 tsubst_flags_t complain)
3904 tree op0, op1;
3905 enum tree_code code0, code1;
3906 tree type0, type1;
3907 const char *invalid_op_diag;
3909 /* Expression code to give to the expression when it is built.
3910 Normally this is CODE, which is what the caller asked for,
3911 but in some special cases we change it. */
3912 enum tree_code resultcode = code;
3914 /* Data type in which the computation is to be performed.
3915 In the simplest cases this is the common type of the arguments. */
3916 tree result_type = NULL;
3918 /* Nonzero means operands have already been type-converted
3919 in whatever way is necessary.
3920 Zero means they need to be converted to RESULT_TYPE. */
3921 int converted = 0;
3923 /* Nonzero means create the expression with this type, rather than
3924 RESULT_TYPE. */
3925 tree build_type = 0;
3927 /* Nonzero means after finally constructing the expression
3928 convert it to this type. */
3929 tree final_type = 0;
3931 tree result;
3932 tree orig_type = NULL;
3934 /* Nonzero if this is an operation like MIN or MAX which can
3935 safely be computed in short if both args are promoted shorts.
3936 Also implies COMMON.
3937 -1 indicates a bitwise operation; this makes a difference
3938 in the exact conditions for when it is safe to do the operation
3939 in a narrower mode. */
3940 int shorten = 0;
3942 /* Nonzero if this is a comparison operation;
3943 if both args are promoted shorts, compare the original shorts.
3944 Also implies COMMON. */
3945 int short_compare = 0;
3947 /* Nonzero means set RESULT_TYPE to the common type of the args. */
3948 int common = 0;
3950 /* True if both operands have arithmetic type. */
3951 bool arithmetic_types_p;
3953 /* Apply default conversions. */
3954 op0 = orig_op0;
3955 op1 = orig_op1;
3957 /* Remember whether we're doing / or %. */
3958 bool doing_div_or_mod = false;
3960 /* Remember whether we're doing << or >>. */
3961 bool doing_shift = false;
3963 /* Tree holding instrumentation expression. */
3964 tree instrument_expr = NULL;
3966 if (code == TRUTH_AND_EXPR || code == TRUTH_ANDIF_EXPR
3967 || code == TRUTH_OR_EXPR || code == TRUTH_ORIF_EXPR
3968 || code == TRUTH_XOR_EXPR)
3970 if (!really_overloaded_fn (op0) && !VOID_TYPE_P (TREE_TYPE (op0)))
3971 op0 = decay_conversion (op0, complain);
3972 if (!really_overloaded_fn (op1) && !VOID_TYPE_P (TREE_TYPE (op1)))
3973 op1 = decay_conversion (op1, complain);
3975 else
3977 if (!really_overloaded_fn (op0) && !VOID_TYPE_P (TREE_TYPE (op0)))
3978 op0 = cp_default_conversion (op0, complain);
3979 if (!really_overloaded_fn (op1) && !VOID_TYPE_P (TREE_TYPE (op1)))
3980 op1 = cp_default_conversion (op1, complain);
3983 /* Strip NON_LVALUE_EXPRs, etc., since we aren't using as an lvalue. */
3984 STRIP_TYPE_NOPS (op0);
3985 STRIP_TYPE_NOPS (op1);
3987 /* DTRT if one side is an overloaded function, but complain about it. */
3988 if (type_unknown_p (op0))
3990 tree t = instantiate_type (TREE_TYPE (op1), op0, tf_none);
3991 if (t != error_mark_node)
3993 if (complain & tf_error)
3994 permerror (input_location, "assuming cast to type %qT from overloaded function",
3995 TREE_TYPE (t));
3996 op0 = t;
3999 if (type_unknown_p (op1))
4001 tree t = instantiate_type (TREE_TYPE (op0), op1, tf_none);
4002 if (t != error_mark_node)
4004 if (complain & tf_error)
4005 permerror (input_location, "assuming cast to type %qT from overloaded function",
4006 TREE_TYPE (t));
4007 op1 = t;
4011 type0 = TREE_TYPE (op0);
4012 type1 = TREE_TYPE (op1);
4014 /* The expression codes of the data types of the arguments tell us
4015 whether the arguments are integers, floating, pointers, etc. */
4016 code0 = TREE_CODE (type0);
4017 code1 = TREE_CODE (type1);
4019 /* If an error was already reported for one of the arguments,
4020 avoid reporting another error. */
4021 if (code0 == ERROR_MARK || code1 == ERROR_MARK)
4022 return error_mark_node;
4024 if ((invalid_op_diag
4025 = targetm.invalid_binary_op (code, type0, type1)))
4027 if (complain & tf_error)
4028 error (invalid_op_diag);
4029 return error_mark_node;
4032 /* Issue warnings about peculiar, but valid, uses of NULL. */
4033 if ((orig_op0 == null_node || orig_op1 == null_node)
4034 /* It's reasonable to use pointer values as operands of &&
4035 and ||, so NULL is no exception. */
4036 && code != TRUTH_ANDIF_EXPR && code != TRUTH_ORIF_EXPR
4037 && ( /* Both are NULL (or 0) and the operation was not a
4038 comparison or a pointer subtraction. */
4039 (null_ptr_cst_p (orig_op0) && null_ptr_cst_p (orig_op1)
4040 && code != EQ_EXPR && code != NE_EXPR && code != MINUS_EXPR)
4041 /* Or if one of OP0 or OP1 is neither a pointer nor NULL. */
4042 || (!null_ptr_cst_p (orig_op0)
4043 && !TYPE_PTR_OR_PTRMEM_P (type0))
4044 || (!null_ptr_cst_p (orig_op1)
4045 && !TYPE_PTR_OR_PTRMEM_P (type1)))
4046 && (complain & tf_warning))
4048 source_location loc =
4049 expansion_point_location_if_in_system_header (input_location);
4051 warning_at (loc, OPT_Wpointer_arith, "NULL used in arithmetic");
4054 /* In case when one of the operands of the binary operation is
4055 a vector and another is a scalar -- convert scalar to vector. */
4056 if ((code0 == VECTOR_TYPE) != (code1 == VECTOR_TYPE))
4058 enum stv_conv convert_flag = scalar_to_vector (location, code, op0, op1,
4059 complain & tf_error);
4061 switch (convert_flag)
4063 case stv_error:
4064 return error_mark_node;
4065 case stv_firstarg:
4067 op0 = convert (TREE_TYPE (type1), op0);
4068 op0 = save_expr (op0);
4069 op0 = build_vector_from_val (type1, op0);
4070 type0 = TREE_TYPE (op0);
4071 code0 = TREE_CODE (type0);
4072 converted = 1;
4073 break;
4075 case stv_secondarg:
4077 op1 = convert (TREE_TYPE (type0), op1);
4078 op1 = save_expr (op1);
4079 op1 = build_vector_from_val (type0, op1);
4080 type1 = TREE_TYPE (op1);
4081 code1 = TREE_CODE (type1);
4082 converted = 1;
4083 break;
4085 default:
4086 break;
4090 switch (code)
4092 case MINUS_EXPR:
4093 /* Subtraction of two similar pointers.
4094 We must subtract them as integers, then divide by object size. */
4095 if (code0 == POINTER_TYPE && code1 == POINTER_TYPE
4096 && same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (type0),
4097 TREE_TYPE (type1)))
4098 return pointer_diff (op0, op1, common_pointer_type (type0, type1),
4099 complain);
4100 /* In all other cases except pointer - int, the usual arithmetic
4101 rules apply. */
4102 else if (!(code0 == POINTER_TYPE && code1 == INTEGER_TYPE))
4104 common = 1;
4105 break;
4107 /* The pointer - int case is just like pointer + int; fall
4108 through. */
4109 case PLUS_EXPR:
4110 if ((code0 == POINTER_TYPE || code1 == POINTER_TYPE)
4111 && (code0 == INTEGER_TYPE || code1 == INTEGER_TYPE))
4113 tree ptr_operand;
4114 tree int_operand;
4115 ptr_operand = ((code0 == POINTER_TYPE) ? op0 : op1);
4116 int_operand = ((code0 == INTEGER_TYPE) ? op0 : op1);
4117 if (processing_template_decl)
4119 result_type = TREE_TYPE (ptr_operand);
4120 break;
4122 return cp_pointer_int_sum (code,
4123 ptr_operand,
4124 int_operand,
4125 complain);
4127 common = 1;
4128 break;
4130 case MULT_EXPR:
4131 common = 1;
4132 break;
4134 case TRUNC_DIV_EXPR:
4135 case CEIL_DIV_EXPR:
4136 case FLOOR_DIV_EXPR:
4137 case ROUND_DIV_EXPR:
4138 case EXACT_DIV_EXPR:
4139 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE
4140 || code0 == COMPLEX_TYPE || code0 == VECTOR_TYPE)
4141 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
4142 || code1 == COMPLEX_TYPE || code1 == VECTOR_TYPE))
4144 enum tree_code tcode0 = code0, tcode1 = code1;
4145 tree cop1 = fold_non_dependent_expr (op1);
4146 doing_div_or_mod = true;
4147 warn_for_div_by_zero (location, cop1);
4149 if (tcode0 == COMPLEX_TYPE || tcode0 == VECTOR_TYPE)
4150 tcode0 = TREE_CODE (TREE_TYPE (TREE_TYPE (op0)));
4151 if (tcode1 == COMPLEX_TYPE || tcode1 == VECTOR_TYPE)
4152 tcode1 = TREE_CODE (TREE_TYPE (TREE_TYPE (op1)));
4154 if (!(tcode0 == INTEGER_TYPE && tcode1 == INTEGER_TYPE))
4155 resultcode = RDIV_EXPR;
4156 else
4157 /* When dividing two signed integers, we have to promote to int.
4158 unless we divide by a constant != -1. Note that default
4159 conversion will have been performed on the operands at this
4160 point, so we have to dig out the original type to find out if
4161 it was unsigned. */
4162 shorten = ((TREE_CODE (op0) == NOP_EXPR
4163 && TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (op0, 0))))
4164 || (TREE_CODE (op1) == INTEGER_CST
4165 && ! integer_all_onesp (op1)));
4167 common = 1;
4169 break;
4171 case BIT_AND_EXPR:
4172 case BIT_IOR_EXPR:
4173 case BIT_XOR_EXPR:
4174 if ((code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
4175 || (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE
4176 && !VECTOR_FLOAT_TYPE_P (type0)
4177 && !VECTOR_FLOAT_TYPE_P (type1)))
4178 shorten = -1;
4179 break;
4181 case TRUNC_MOD_EXPR:
4182 case FLOOR_MOD_EXPR:
4184 tree cop1 = fold_non_dependent_expr (op1);
4185 doing_div_or_mod = true;
4186 warn_for_div_by_zero (location, cop1);
4189 if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE
4190 && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE
4191 && TREE_CODE (TREE_TYPE (type1)) == INTEGER_TYPE)
4192 common = 1;
4193 else if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
4195 /* Although it would be tempting to shorten always here, that loses
4196 on some targets, since the modulo instruction is undefined if the
4197 quotient can't be represented in the computation mode. We shorten
4198 only if unsigned or if dividing by something we know != -1. */
4199 shorten = ((TREE_CODE (op0) == NOP_EXPR
4200 && TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (op0, 0))))
4201 || (TREE_CODE (op1) == INTEGER_CST
4202 && ! integer_all_onesp (op1)));
4203 common = 1;
4205 break;
4207 case TRUTH_ANDIF_EXPR:
4208 case TRUTH_ORIF_EXPR:
4209 case TRUTH_AND_EXPR:
4210 case TRUTH_OR_EXPR:
4211 if (!VECTOR_TYPE_P (type0) && VECTOR_TYPE_P (type1))
4213 if (!COMPARISON_CLASS_P (op1))
4214 op1 = cp_build_binary_op (EXPR_LOCATION (op1), NE_EXPR, op1,
4215 build_zero_cst (type1), complain);
4216 if (code == TRUTH_ANDIF_EXPR)
4218 tree z = build_zero_cst (TREE_TYPE (op1));
4219 return build_conditional_expr (location, op0, op1, z, complain);
4221 else if (code == TRUTH_ORIF_EXPR)
4223 tree m1 = build_all_ones_cst (TREE_TYPE (op1));
4224 return build_conditional_expr (location, op0, m1, op1, complain);
4226 else
4227 gcc_unreachable ();
4229 if (VECTOR_TYPE_P (type0))
4231 if (!COMPARISON_CLASS_P (op0))
4232 op0 = cp_build_binary_op (EXPR_LOCATION (op0), NE_EXPR, op0,
4233 build_zero_cst (type0), complain);
4234 if (!VECTOR_TYPE_P (type1))
4236 tree m1 = build_all_ones_cst (TREE_TYPE (op0));
4237 tree z = build_zero_cst (TREE_TYPE (op0));
4238 op1 = build_conditional_expr (location, op1, z, m1, complain);
4240 else if (!COMPARISON_CLASS_P (op1))
4241 op1 = cp_build_binary_op (EXPR_LOCATION (op1), NE_EXPR, op1,
4242 build_zero_cst (type1), complain);
4244 if (code == TRUTH_ANDIF_EXPR)
4245 code = BIT_AND_EXPR;
4246 else if (code == TRUTH_ORIF_EXPR)
4247 code = BIT_IOR_EXPR;
4248 else
4249 gcc_unreachable ();
4251 return cp_build_binary_op (location, code, op0, op1, complain);
4254 result_type = boolean_type_node;
4255 break;
4257 /* Shift operations: result has same type as first operand;
4258 always convert second operand to int.
4259 Also set SHORT_SHIFT if shifting rightward. */
4261 case RSHIFT_EXPR:
4262 if (code0 == VECTOR_TYPE && code1 == INTEGER_TYPE
4263 && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE)
4265 result_type = type0;
4266 converted = 1;
4268 else if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE
4269 && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE
4270 && TREE_CODE (TREE_TYPE (type1)) == INTEGER_TYPE
4271 && TYPE_VECTOR_SUBPARTS (type0) == TYPE_VECTOR_SUBPARTS (type1))
4273 result_type = type0;
4274 converted = 1;
4276 else if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
4278 tree const_op1 = fold_non_dependent_expr (op1);
4279 if (TREE_CODE (const_op1) != INTEGER_CST)
4280 const_op1 = op1;
4281 result_type = type0;
4282 doing_shift = true;
4283 if (TREE_CODE (const_op1) == INTEGER_CST)
4285 if (tree_int_cst_lt (const_op1, integer_zero_node))
4287 if ((complain & tf_warning)
4288 && c_inhibit_evaluation_warnings == 0)
4289 warning (OPT_Wshift_count_negative,
4290 "right shift count is negative");
4292 else
4294 if (compare_tree_int (const_op1, TYPE_PRECISION (type0)) >= 0
4295 && (complain & tf_warning)
4296 && c_inhibit_evaluation_warnings == 0)
4297 warning (OPT_Wshift_count_overflow,
4298 "right shift count >= width of type");
4301 /* Avoid converting op1 to result_type later. */
4302 converted = 1;
4304 break;
4306 case LSHIFT_EXPR:
4307 if (code0 == VECTOR_TYPE && code1 == INTEGER_TYPE
4308 && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE)
4310 result_type = type0;
4311 converted = 1;
4313 else if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE
4314 && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE
4315 && TREE_CODE (TREE_TYPE (type1)) == INTEGER_TYPE
4316 && TYPE_VECTOR_SUBPARTS (type0) == TYPE_VECTOR_SUBPARTS (type1))
4318 result_type = type0;
4319 converted = 1;
4321 else if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
4323 tree const_op0 = fold_non_dependent_expr (op0);
4324 if (TREE_CODE (const_op0) != INTEGER_CST)
4325 const_op0 = op0;
4326 tree const_op1 = fold_non_dependent_expr (op1);
4327 if (TREE_CODE (const_op1) != INTEGER_CST)
4328 const_op1 = op1;
4329 result_type = type0;
4330 doing_shift = true;
4331 if (TREE_CODE (const_op0) == INTEGER_CST
4332 && tree_int_cst_sgn (const_op0) < 0
4333 && (complain & tf_warning)
4334 && c_inhibit_evaluation_warnings == 0)
4335 warning (OPT_Wshift_negative_value,
4336 "left shift of negative value");
4337 if (TREE_CODE (const_op1) == INTEGER_CST)
4339 if (tree_int_cst_lt (const_op1, integer_zero_node))
4341 if ((complain & tf_warning)
4342 && c_inhibit_evaluation_warnings == 0)
4343 warning (OPT_Wshift_count_negative,
4344 "left shift count is negative");
4346 else if (compare_tree_int (const_op1,
4347 TYPE_PRECISION (type0)) >= 0)
4349 if ((complain & tf_warning)
4350 && c_inhibit_evaluation_warnings == 0)
4351 warning (OPT_Wshift_count_overflow,
4352 "left shift count >= width of type");
4355 /* Avoid converting op1 to result_type later. */
4356 converted = 1;
4358 break;
4360 case RROTATE_EXPR:
4361 case LROTATE_EXPR:
4362 if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
4364 result_type = type0;
4365 if (TREE_CODE (op1) == INTEGER_CST)
4367 if (tree_int_cst_lt (op1, integer_zero_node))
4369 if (complain & tf_warning)
4370 warning (0, (code == LROTATE_EXPR)
4371 ? G_("left rotate count is negative")
4372 : G_("right rotate count is negative"));
4374 else if (compare_tree_int (op1, TYPE_PRECISION (type0)) >= 0)
4376 if (complain & tf_warning)
4377 warning (0, (code == LROTATE_EXPR)
4378 ? G_("left rotate count >= width of type")
4379 : G_("right rotate count >= width of type"));
4382 /* Convert the shift-count to an integer, regardless of
4383 size of value being shifted. */
4384 if (TYPE_MAIN_VARIANT (TREE_TYPE (op1)) != integer_type_node)
4385 op1 = cp_convert (integer_type_node, op1, complain);
4387 break;
4389 case EQ_EXPR:
4390 case NE_EXPR:
4391 if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE)
4392 goto vector_compare;
4393 if ((complain & tf_warning)
4394 && (FLOAT_TYPE_P (type0) || FLOAT_TYPE_P (type1)))
4395 warning (OPT_Wfloat_equal,
4396 "comparing floating point with == or != is unsafe");
4397 if ((complain & tf_warning)
4398 && ((TREE_CODE (orig_op0) == STRING_CST && !integer_zerop (op1))
4399 || (TREE_CODE (orig_op1) == STRING_CST && !integer_zerop (op0))))
4400 warning (OPT_Waddress, "comparison with string literal results in unspecified behaviour");
4402 build_type = boolean_type_node;
4403 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE
4404 || code0 == COMPLEX_TYPE || code0 == ENUMERAL_TYPE)
4405 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
4406 || code1 == COMPLEX_TYPE || code1 == ENUMERAL_TYPE))
4407 short_compare = 1;
4408 else if (((code0 == POINTER_TYPE || TYPE_PTRDATAMEM_P (type0))
4409 && null_ptr_cst_p (op1))
4410 /* Handle, eg, (void*)0 (c++/43906), and more. */
4411 || (code0 == POINTER_TYPE
4412 && TYPE_PTR_P (type1) && integer_zerop (op1)))
4414 if (TYPE_PTR_P (type1))
4415 result_type = composite_pointer_type (type0, type1, op0, op1,
4416 CPO_COMPARISON, complain);
4417 else
4418 result_type = type0;
4420 if (TREE_CODE (op0) == ADDR_EXPR
4421 && decl_with_nonnull_addr_p (TREE_OPERAND (op0, 0)))
4423 if ((complain & tf_warning)
4424 && c_inhibit_evaluation_warnings == 0
4425 && !TREE_NO_WARNING (op0))
4426 warning (OPT_Waddress, "the address of %qD will never be NULL",
4427 TREE_OPERAND (op0, 0));
4430 if (CONVERT_EXPR_P (op0)
4431 && TREE_CODE (TREE_TYPE (TREE_OPERAND (op0, 0)))
4432 == REFERENCE_TYPE)
4434 tree inner_op0 = op0;
4435 STRIP_NOPS (inner_op0);
4437 if ((complain & tf_warning)
4438 && c_inhibit_evaluation_warnings == 0
4439 && !TREE_NO_WARNING (op0)
4440 && DECL_P (inner_op0))
4441 warning_at (location, OPT_Waddress,
4442 "the compiler can assume that the address of "
4443 "%qD will never be NULL",
4444 inner_op0);
4447 else if (((code1 == POINTER_TYPE || TYPE_PTRDATAMEM_P (type1))
4448 && null_ptr_cst_p (op0))
4449 /* Handle, eg, (void*)0 (c++/43906), and more. */
4450 || (code1 == POINTER_TYPE
4451 && TYPE_PTR_P (type0) && integer_zerop (op0)))
4453 if (TYPE_PTR_P (type0))
4454 result_type = composite_pointer_type (type0, type1, op0, op1,
4455 CPO_COMPARISON, complain);
4456 else
4457 result_type = type1;
4459 if (TREE_CODE (op1) == ADDR_EXPR
4460 && decl_with_nonnull_addr_p (TREE_OPERAND (op1, 0)))
4462 if ((complain & tf_warning)
4463 && c_inhibit_evaluation_warnings == 0
4464 && !TREE_NO_WARNING (op1))
4465 warning (OPT_Waddress, "the address of %qD will never be NULL",
4466 TREE_OPERAND (op1, 0));
4469 if (CONVERT_EXPR_P (op1)
4470 && TREE_CODE (TREE_TYPE (TREE_OPERAND (op1, 0)))
4471 == REFERENCE_TYPE)
4473 tree inner_op1 = op1;
4474 STRIP_NOPS (inner_op1);
4476 if ((complain & tf_warning)
4477 && c_inhibit_evaluation_warnings == 0
4478 && !TREE_NO_WARNING (op1)
4479 && DECL_P (inner_op1))
4480 warning_at (location, OPT_Waddress,
4481 "the compiler can assume that the address of "
4482 "%qD will never be NULL",
4483 inner_op1);
4486 else if ((code0 == POINTER_TYPE && code1 == POINTER_TYPE)
4487 || (TYPE_PTRDATAMEM_P (type0) && TYPE_PTRDATAMEM_P (type1)))
4488 result_type = composite_pointer_type (type0, type1, op0, op1,
4489 CPO_COMPARISON, complain);
4490 else if (null_ptr_cst_p (op0) && null_ptr_cst_p (op1))
4491 /* One of the operands must be of nullptr_t type. */
4492 result_type = TREE_TYPE (nullptr_node);
4493 else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
4495 result_type = type0;
4496 if (complain & tf_error)
4497 permerror (input_location, "ISO C++ forbids comparison between pointer and integer");
4498 else
4499 return error_mark_node;
4501 else if (code0 == INTEGER_TYPE && code1 == POINTER_TYPE)
4503 result_type = type1;
4504 if (complain & tf_error)
4505 permerror (input_location, "ISO C++ forbids comparison between pointer and integer");
4506 else
4507 return error_mark_node;
4509 else if (TYPE_PTRMEMFUNC_P (type0) && null_ptr_cst_p (op1))
4511 if (TARGET_PTRMEMFUNC_VBIT_LOCATION
4512 == ptrmemfunc_vbit_in_delta)
4514 tree pfn0, delta0, e1, e2;
4516 if (TREE_SIDE_EFFECTS (op0))
4517 op0 = save_expr (op0);
4519 pfn0 = pfn_from_ptrmemfunc (op0);
4520 delta0 = delta_from_ptrmemfunc (op0);
4521 e1 = cp_build_binary_op (location,
4522 EQ_EXPR,
4523 pfn0,
4524 build_zero_cst (TREE_TYPE (pfn0)),
4525 complain);
4526 e2 = cp_build_binary_op (location,
4527 BIT_AND_EXPR,
4528 delta0,
4529 integer_one_node,
4530 complain);
4532 if (complain & tf_warning)
4533 maybe_warn_zero_as_null_pointer_constant (op1, input_location);
4535 e2 = cp_build_binary_op (location,
4536 EQ_EXPR, e2, integer_zero_node,
4537 complain);
4538 op0 = cp_build_binary_op (location,
4539 TRUTH_ANDIF_EXPR, e1, e2,
4540 complain);
4541 op1 = cp_convert (TREE_TYPE (op0), integer_one_node, complain);
4543 else
4545 op0 = build_ptrmemfunc_access_expr (op0, pfn_identifier);
4546 op1 = cp_convert (TREE_TYPE (op0), op1, complain);
4548 result_type = TREE_TYPE (op0);
4550 else if (TYPE_PTRMEMFUNC_P (type1) && null_ptr_cst_p (op0))
4551 return cp_build_binary_op (location, code, op1, op0, complain);
4552 else if (TYPE_PTRMEMFUNC_P (type0) && TYPE_PTRMEMFUNC_P (type1))
4554 tree type;
4555 /* E will be the final comparison. */
4556 tree e;
4557 /* E1 and E2 are for scratch. */
4558 tree e1;
4559 tree e2;
4560 tree pfn0;
4561 tree pfn1;
4562 tree delta0;
4563 tree delta1;
4565 type = composite_pointer_type (type0, type1, op0, op1,
4566 CPO_COMPARISON, complain);
4568 if (!same_type_p (TREE_TYPE (op0), type))
4569 op0 = cp_convert_and_check (type, op0, complain);
4570 if (!same_type_p (TREE_TYPE (op1), type))
4571 op1 = cp_convert_and_check (type, op1, complain);
4573 if (op0 == error_mark_node || op1 == error_mark_node)
4574 return error_mark_node;
4576 if (TREE_SIDE_EFFECTS (op0))
4577 op0 = save_expr (op0);
4578 if (TREE_SIDE_EFFECTS (op1))
4579 op1 = save_expr (op1);
4581 pfn0 = pfn_from_ptrmemfunc (op0);
4582 /* Avoid -Waddress warnings (c++/64877). */
4583 if (TREE_CODE (pfn0) == ADDR_EXPR)
4584 TREE_NO_WARNING (pfn0) = 1;
4585 pfn1 = pfn_from_ptrmemfunc (op1);
4586 delta0 = delta_from_ptrmemfunc (op0);
4587 delta1 = delta_from_ptrmemfunc (op1);
4588 if (TARGET_PTRMEMFUNC_VBIT_LOCATION
4589 == ptrmemfunc_vbit_in_delta)
4591 /* We generate:
4593 (op0.pfn == op1.pfn
4594 && ((op0.delta == op1.delta)
4595 || (!op0.pfn && op0.delta & 1 == 0
4596 && op1.delta & 1 == 0))
4598 The reason for the `!op0.pfn' bit is that a NULL
4599 pointer-to-member is any member with a zero PFN and
4600 LSB of the DELTA field is 0. */
4602 e1 = cp_build_binary_op (location, BIT_AND_EXPR,
4603 delta0,
4604 integer_one_node,
4605 complain);
4606 e1 = cp_build_binary_op (location,
4607 EQ_EXPR, e1, integer_zero_node,
4608 complain);
4609 e2 = cp_build_binary_op (location, BIT_AND_EXPR,
4610 delta1,
4611 integer_one_node,
4612 complain);
4613 e2 = cp_build_binary_op (location,
4614 EQ_EXPR, e2, integer_zero_node,
4615 complain);
4616 e1 = cp_build_binary_op (location,
4617 TRUTH_ANDIF_EXPR, e2, e1,
4618 complain);
4619 e2 = cp_build_binary_op (location, EQ_EXPR,
4620 pfn0,
4621 build_zero_cst (TREE_TYPE (pfn0)),
4622 complain);
4623 e2 = cp_build_binary_op (location,
4624 TRUTH_ANDIF_EXPR, e2, e1, complain);
4625 e1 = cp_build_binary_op (location,
4626 EQ_EXPR, delta0, delta1, complain);
4627 e1 = cp_build_binary_op (location,
4628 TRUTH_ORIF_EXPR, e1, e2, complain);
4630 else
4632 /* We generate:
4634 (op0.pfn == op1.pfn
4635 && (!op0.pfn || op0.delta == op1.delta))
4637 The reason for the `!op0.pfn' bit is that a NULL
4638 pointer-to-member is any member with a zero PFN; the
4639 DELTA field is unspecified. */
4641 e1 = cp_build_binary_op (location,
4642 EQ_EXPR, delta0, delta1, complain);
4643 e2 = cp_build_binary_op (location,
4644 EQ_EXPR,
4645 pfn0,
4646 build_zero_cst (TREE_TYPE (pfn0)),
4647 complain);
4648 e1 = cp_build_binary_op (location,
4649 TRUTH_ORIF_EXPR, e1, e2, complain);
4651 e2 = build2 (EQ_EXPR, boolean_type_node, pfn0, pfn1);
4652 e = cp_build_binary_op (location,
4653 TRUTH_ANDIF_EXPR, e2, e1, complain);
4654 if (code == EQ_EXPR)
4655 return e;
4656 return cp_build_binary_op (location,
4657 EQ_EXPR, e, integer_zero_node, complain);
4659 else
4661 gcc_assert (!TYPE_PTRMEMFUNC_P (type0)
4662 || !same_type_p (TYPE_PTRMEMFUNC_FN_TYPE (type0),
4663 type1));
4664 gcc_assert (!TYPE_PTRMEMFUNC_P (type1)
4665 || !same_type_p (TYPE_PTRMEMFUNC_FN_TYPE (type1),
4666 type0));
4669 break;
4671 case MAX_EXPR:
4672 case MIN_EXPR:
4673 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE)
4674 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE))
4675 shorten = 1;
4676 else if (code0 == POINTER_TYPE && code1 == POINTER_TYPE)
4677 result_type = composite_pointer_type (type0, type1, op0, op1,
4678 CPO_COMPARISON, complain);
4679 break;
4681 case LE_EXPR:
4682 case GE_EXPR:
4683 case LT_EXPR:
4684 case GT_EXPR:
4685 if (TREE_CODE (orig_op0) == STRING_CST
4686 || TREE_CODE (orig_op1) == STRING_CST)
4688 if (complain & tf_warning)
4689 warning (OPT_Waddress, "comparison with string literal results in unspecified behaviour");
4692 if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE)
4694 vector_compare:
4695 tree intt;
4696 if (!same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (type0),
4697 TREE_TYPE (type1))
4698 && !vector_types_compatible_elements_p (type0, type1))
4700 if (complain & tf_error)
4702 error_at (location, "comparing vectors with different "
4703 "element types");
4704 inform (location, "operand types are %qT and %qT",
4705 type0, type1);
4707 return error_mark_node;
4710 if (TYPE_VECTOR_SUBPARTS (type0) != TYPE_VECTOR_SUBPARTS (type1))
4712 if (complain & tf_error)
4714 error_at (location, "comparing vectors with different "
4715 "number of elements");
4716 inform (location, "operand types are %qT and %qT",
4717 type0, type1);
4719 return error_mark_node;
4722 /* Always construct signed integer vector type. */
4723 intt = c_common_type_for_size (GET_MODE_BITSIZE
4724 (TYPE_MODE (TREE_TYPE (type0))), 0);
4725 if (!intt)
4727 if (complain & tf_error)
4728 error_at (location, "could not find an integer type "
4729 "of the same size as %qT", TREE_TYPE (type0));
4730 return error_mark_node;
4732 result_type = build_opaque_vector_type (intt,
4733 TYPE_VECTOR_SUBPARTS (type0));
4734 converted = 1;
4735 break;
4737 build_type = boolean_type_node;
4738 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE
4739 || code0 == ENUMERAL_TYPE)
4740 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
4741 || code1 == ENUMERAL_TYPE))
4742 short_compare = 1;
4743 else if (code0 == POINTER_TYPE && code1 == POINTER_TYPE)
4744 result_type = composite_pointer_type (type0, type1, op0, op1,
4745 CPO_COMPARISON, complain);
4746 else if (code0 == POINTER_TYPE && null_ptr_cst_p (op1))
4748 result_type = type0;
4749 if (extra_warnings && (complain & tf_warning))
4750 warning (OPT_Wextra,
4751 "ordered comparison of pointer with integer zero");
4753 else if (code1 == POINTER_TYPE && null_ptr_cst_p (op0))
4755 result_type = type1;
4756 if (extra_warnings && (complain & tf_warning))
4757 warning (OPT_Wextra,
4758 "ordered comparison of pointer with integer zero");
4760 else if (null_ptr_cst_p (op0) && null_ptr_cst_p (op1))
4761 /* One of the operands must be of nullptr_t type. */
4762 result_type = TREE_TYPE (nullptr_node);
4763 else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
4765 result_type = type0;
4766 if (complain & tf_error)
4767 permerror (input_location, "ISO C++ forbids comparison between pointer and integer");
4768 else
4769 return error_mark_node;
4771 else if (code0 == INTEGER_TYPE && code1 == POINTER_TYPE)
4773 result_type = type1;
4774 if (complain & tf_error)
4775 permerror (input_location, "ISO C++ forbids comparison between pointer and integer");
4776 else
4777 return error_mark_node;
4779 break;
4781 case UNORDERED_EXPR:
4782 case ORDERED_EXPR:
4783 case UNLT_EXPR:
4784 case UNLE_EXPR:
4785 case UNGT_EXPR:
4786 case UNGE_EXPR:
4787 case UNEQ_EXPR:
4788 build_type = integer_type_node;
4789 if (code0 != REAL_TYPE || code1 != REAL_TYPE)
4791 if (complain & tf_error)
4792 error ("unordered comparison on non-floating point argument");
4793 return error_mark_node;
4795 common = 1;
4796 break;
4798 default:
4799 break;
4802 if (((code0 == INTEGER_TYPE || code0 == REAL_TYPE || code0 == COMPLEX_TYPE
4803 || code0 == ENUMERAL_TYPE)
4804 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
4805 || code1 == COMPLEX_TYPE || code1 == ENUMERAL_TYPE)))
4806 arithmetic_types_p = 1;
4807 else
4809 arithmetic_types_p = 0;
4810 /* Vector arithmetic is only allowed when both sides are vectors. */
4811 if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE)
4813 if (!tree_int_cst_equal (TYPE_SIZE (type0), TYPE_SIZE (type1))
4814 || !vector_types_compatible_elements_p (type0, type1))
4816 if (complain & tf_error)
4817 binary_op_error (location, code, type0, type1);
4818 return error_mark_node;
4820 arithmetic_types_p = 1;
4823 /* Determine the RESULT_TYPE, if it is not already known. */
4824 if (!result_type
4825 && arithmetic_types_p
4826 && (shorten || common || short_compare))
4828 result_type = cp_common_type (type0, type1);
4829 if (complain & tf_warning)
4830 do_warn_double_promotion (result_type, type0, type1,
4831 "implicit conversion from %qT to %qT "
4832 "to match other operand of binary "
4833 "expression",
4834 location);
4837 if (!result_type)
4839 if (complain & tf_error)
4840 error ("invalid operands of types %qT and %qT to binary %qO",
4841 TREE_TYPE (orig_op0), TREE_TYPE (orig_op1), code);
4842 return error_mark_node;
4845 /* If we're in a template, the only thing we need to know is the
4846 RESULT_TYPE. */
4847 if (processing_template_decl)
4849 /* Since the middle-end checks the type when doing a build2, we
4850 need to build the tree in pieces. This built tree will never
4851 get out of the front-end as we replace it when instantiating
4852 the template. */
4853 tree tmp = build2 (resultcode,
4854 build_type ? build_type : result_type,
4855 NULL_TREE, op1);
4856 TREE_OPERAND (tmp, 0) = op0;
4857 return tmp;
4860 if (arithmetic_types_p)
4862 bool first_complex = (code0 == COMPLEX_TYPE);
4863 bool second_complex = (code1 == COMPLEX_TYPE);
4864 int none_complex = (!first_complex && !second_complex);
4866 /* Adapted from patch for c/24581. */
4867 if (first_complex != second_complex
4868 && (code == PLUS_EXPR
4869 || code == MINUS_EXPR
4870 || code == MULT_EXPR
4871 || (code == TRUNC_DIV_EXPR && first_complex))
4872 && TREE_CODE (TREE_TYPE (result_type)) == REAL_TYPE
4873 && flag_signed_zeros)
4875 /* An operation on mixed real/complex operands must be
4876 handled specially, but the language-independent code can
4877 more easily optimize the plain complex arithmetic if
4878 -fno-signed-zeros. */
4879 tree real_type = TREE_TYPE (result_type);
4880 tree real, imag;
4881 if (first_complex)
4883 if (TREE_TYPE (op0) != result_type)
4884 op0 = cp_convert_and_check (result_type, op0, complain);
4885 if (TREE_TYPE (op1) != real_type)
4886 op1 = cp_convert_and_check (real_type, op1, complain);
4888 else
4890 if (TREE_TYPE (op0) != real_type)
4891 op0 = cp_convert_and_check (real_type, op0, complain);
4892 if (TREE_TYPE (op1) != result_type)
4893 op1 = cp_convert_and_check (result_type, op1, complain);
4895 if (TREE_CODE (op0) == ERROR_MARK || TREE_CODE (op1) == ERROR_MARK)
4896 return error_mark_node;
4897 if (first_complex)
4899 op0 = save_expr (op0);
4900 real = cp_build_unary_op (REALPART_EXPR, op0, 1, complain);
4901 imag = cp_build_unary_op (IMAGPART_EXPR, op0, 1, complain);
4902 switch (code)
4904 case MULT_EXPR:
4905 case TRUNC_DIV_EXPR:
4906 op1 = save_expr (op1);
4907 imag = build2 (resultcode, real_type, imag, op1);
4908 /* Fall through. */
4909 case PLUS_EXPR:
4910 case MINUS_EXPR:
4911 real = build2 (resultcode, real_type, real, op1);
4912 break;
4913 default:
4914 gcc_unreachable();
4917 else
4919 op1 = save_expr (op1);
4920 real = cp_build_unary_op (REALPART_EXPR, op1, 1, complain);
4921 imag = cp_build_unary_op (IMAGPART_EXPR, op1, 1, complain);
4922 switch (code)
4924 case MULT_EXPR:
4925 op0 = save_expr (op0);
4926 imag = build2 (resultcode, real_type, op0, imag);
4927 /* Fall through. */
4928 case PLUS_EXPR:
4929 real = build2 (resultcode, real_type, op0, real);
4930 break;
4931 case MINUS_EXPR:
4932 real = build2 (resultcode, real_type, op0, real);
4933 imag = build1 (NEGATE_EXPR, real_type, imag);
4934 break;
4935 default:
4936 gcc_unreachable();
4939 real = fold_if_not_in_template (real);
4940 imag = fold_if_not_in_template (imag);
4941 result = build2 (COMPLEX_EXPR, result_type, real, imag);
4942 result = fold_if_not_in_template (result);
4943 return result;
4946 /* For certain operations (which identify themselves by shorten != 0)
4947 if both args were extended from the same smaller type,
4948 do the arithmetic in that type and then extend.
4950 shorten !=0 and !=1 indicates a bitwise operation.
4951 For them, this optimization is safe only if
4952 both args are zero-extended or both are sign-extended.
4953 Otherwise, we might change the result.
4954 E.g., (short)-1 | (unsigned short)-1 is (int)-1
4955 but calculated in (unsigned short) it would be (unsigned short)-1. */
4957 if (shorten && none_complex)
4959 orig_type = result_type;
4960 final_type = result_type;
4961 result_type = shorten_binary_op (result_type, op0, op1,
4962 shorten == -1);
4965 /* Comparison operations are shortened too but differently.
4966 They identify themselves by setting short_compare = 1. */
4968 if (short_compare)
4970 /* Don't write &op0, etc., because that would prevent op0
4971 from being kept in a register.
4972 Instead, make copies of the our local variables and
4973 pass the copies by reference, then copy them back afterward. */
4974 tree xop0 = op0, xop1 = op1, xresult_type = result_type;
4975 enum tree_code xresultcode = resultcode;
4976 tree val
4977 = shorten_compare (location, &xop0, &xop1, &xresult_type,
4978 &xresultcode);
4979 if (val != 0)
4980 return cp_convert (boolean_type_node, val, complain);
4981 op0 = xop0, op1 = xop1;
4982 converted = 1;
4983 resultcode = xresultcode;
4986 if ((short_compare || code == MIN_EXPR || code == MAX_EXPR)
4987 && warn_sign_compare
4988 /* Do not warn until the template is instantiated; we cannot
4989 bound the ranges of the arguments until that point. */
4990 && !processing_template_decl
4991 && (complain & tf_warning)
4992 && c_inhibit_evaluation_warnings == 0
4993 /* Even unsigned enum types promote to signed int. We don't
4994 want to issue -Wsign-compare warnings for this case. */
4995 && !enum_cast_to_int (orig_op0)
4996 && !enum_cast_to_int (orig_op1))
4998 tree oop0 = maybe_constant_value (orig_op0);
4999 tree oop1 = maybe_constant_value (orig_op1);
5001 if (TREE_CODE (oop0) != INTEGER_CST)
5002 oop0 = orig_op0;
5003 if (TREE_CODE (oop1) != INTEGER_CST)
5004 oop1 = orig_op1;
5005 warn_for_sign_compare (location, oop0, oop1, op0, op1,
5006 result_type, resultcode);
5010 /* If CONVERTED is zero, both args will be converted to type RESULT_TYPE.
5011 Then the expression will be built.
5012 It will be given type FINAL_TYPE if that is nonzero;
5013 otherwise, it will be given type RESULT_TYPE. */
5014 if (! converted)
5016 if (TREE_TYPE (op0) != result_type)
5017 op0 = cp_convert_and_check (result_type, op0, complain);
5018 if (TREE_TYPE (op1) != result_type)
5019 op1 = cp_convert_and_check (result_type, op1, complain);
5021 if (op0 == error_mark_node || op1 == error_mark_node)
5022 return error_mark_node;
5025 if (build_type == NULL_TREE)
5026 build_type = result_type;
5028 if ((flag_sanitize & (SANITIZE_SHIFT | SANITIZE_DIVIDE
5029 | SANITIZE_FLOAT_DIVIDE))
5030 && !processing_template_decl
5031 && do_ubsan_in_current_function ()
5032 && (doing_div_or_mod || doing_shift))
5034 /* OP0 and/or OP1 might have side-effects. */
5035 op0 = cp_save_expr (op0);
5036 op1 = cp_save_expr (op1);
5037 op0 = fold_non_dependent_expr (op0);
5038 op1 = fold_non_dependent_expr (op1);
5039 if (doing_div_or_mod && (flag_sanitize & (SANITIZE_DIVIDE
5040 | SANITIZE_FLOAT_DIVIDE)))
5042 /* For diagnostics we want to use the promoted types without
5043 shorten_binary_op. So convert the arguments to the
5044 original result_type. */
5045 tree cop0 = op0;
5046 tree cop1 = op1;
5047 if (orig_type != NULL && result_type != orig_type)
5049 cop0 = cp_convert (orig_type, op0, complain);
5050 cop1 = cp_convert (orig_type, op1, complain);
5052 instrument_expr = ubsan_instrument_division (location, cop0, cop1);
5054 else if (doing_shift && (flag_sanitize & SANITIZE_SHIFT))
5055 instrument_expr = ubsan_instrument_shift (location, code, op0, op1);
5058 result = build2 (resultcode, build_type, op0, op1);
5059 result = fold_if_not_in_template (result);
5060 if (final_type != 0)
5061 result = cp_convert (final_type, result, complain);
5063 if (TREE_OVERFLOW_P (result)
5064 && !TREE_OVERFLOW_P (op0)
5065 && !TREE_OVERFLOW_P (op1))
5066 overflow_warning (location, result);
5068 if (instrument_expr != NULL)
5069 result = fold_build2 (COMPOUND_EXPR, TREE_TYPE (result),
5070 instrument_expr, result);
5072 return result;
5075 /* Build a VEC_PERM_EXPR.
5076 This is a simple wrapper for c_build_vec_perm_expr. */
5077 tree
5078 build_x_vec_perm_expr (location_t loc,
5079 tree arg0, tree arg1, tree arg2,
5080 tsubst_flags_t complain)
5082 tree orig_arg0 = arg0;
5083 tree orig_arg1 = arg1;
5084 tree orig_arg2 = arg2;
5085 if (processing_template_decl)
5087 if (type_dependent_expression_p (arg0)
5088 || type_dependent_expression_p (arg1)
5089 || type_dependent_expression_p (arg2))
5090 return build_min_nt_loc (loc, VEC_PERM_EXPR, arg0, arg1, arg2);
5091 arg0 = build_non_dependent_expr (arg0);
5092 if (arg1)
5093 arg1 = build_non_dependent_expr (arg1);
5094 arg2 = build_non_dependent_expr (arg2);
5096 tree exp = c_build_vec_perm_expr (loc, arg0, arg1, arg2, complain & tf_error);
5097 if (processing_template_decl && exp != error_mark_node)
5098 return build_min_non_dep (VEC_PERM_EXPR, exp, orig_arg0,
5099 orig_arg1, orig_arg2);
5100 return exp;
5103 /* Return a tree for the sum or difference (RESULTCODE says which)
5104 of pointer PTROP and integer INTOP. */
5106 static tree
5107 cp_pointer_int_sum (enum tree_code resultcode, tree ptrop, tree intop,
5108 tsubst_flags_t complain)
5110 tree res_type = TREE_TYPE (ptrop);
5112 /* pointer_int_sum() uses size_in_bytes() on the TREE_TYPE(res_type)
5113 in certain circumstance (when it's valid to do so). So we need
5114 to make sure it's complete. We don't need to check here, if we
5115 can actually complete it at all, as those checks will be done in
5116 pointer_int_sum() anyway. */
5117 complete_type (TREE_TYPE (res_type));
5119 return pointer_int_sum (input_location, resultcode, ptrop,
5120 fold_if_not_in_template (intop),
5121 complain & tf_warning_or_error);
5124 /* Return a tree for the difference of pointers OP0 and OP1.
5125 The resulting tree has type int. */
5127 static tree
5128 pointer_diff (tree op0, tree op1, tree ptrtype, tsubst_flags_t complain)
5130 tree result;
5131 tree restype = ptrdiff_type_node;
5132 tree target_type = TREE_TYPE (ptrtype);
5134 if (!complete_type_or_else (target_type, NULL_TREE))
5135 return error_mark_node;
5137 if (VOID_TYPE_P (target_type))
5139 if (complain & tf_error)
5140 permerror (input_location, "ISO C++ forbids using pointer of "
5141 "type %<void *%> in subtraction");
5142 else
5143 return error_mark_node;
5145 if (TREE_CODE (target_type) == FUNCTION_TYPE)
5147 if (complain & tf_error)
5148 permerror (input_location, "ISO C++ forbids using pointer to "
5149 "a function in subtraction");
5150 else
5151 return error_mark_node;
5153 if (TREE_CODE (target_type) == METHOD_TYPE)
5155 if (complain & tf_error)
5156 permerror (input_location, "ISO C++ forbids using pointer to "
5157 "a method in subtraction");
5158 else
5159 return error_mark_node;
5162 /* First do the subtraction as integers;
5163 then drop through to build the divide operator. */
5165 op0 = cp_build_binary_op (input_location,
5166 MINUS_EXPR,
5167 cp_convert (restype, op0, complain),
5168 cp_convert (restype, op1, complain),
5169 complain);
5171 /* This generates an error if op1 is a pointer to an incomplete type. */
5172 if (!COMPLETE_TYPE_P (TREE_TYPE (TREE_TYPE (op1))))
5174 if (complain & tf_error)
5175 error ("invalid use of a pointer to an incomplete type in "
5176 "pointer arithmetic");
5177 else
5178 return error_mark_node;
5181 if (pointer_to_zero_sized_aggr_p (TREE_TYPE (op1)))
5183 if (complain & tf_error)
5184 error ("arithmetic on pointer to an empty aggregate");
5185 else
5186 return error_mark_node;
5189 op1 = (TYPE_PTROB_P (ptrtype)
5190 ? size_in_bytes (target_type)
5191 : integer_one_node);
5193 /* Do the division. */
5195 result = build2 (EXACT_DIV_EXPR, restype, op0,
5196 cp_convert (restype, op1, complain));
5197 return fold_if_not_in_template (result);
5200 /* Construct and perhaps optimize a tree representation
5201 for a unary operation. CODE, a tree_code, specifies the operation
5202 and XARG is the operand. */
5204 tree
5205 build_x_unary_op (location_t loc, enum tree_code code, tree xarg,
5206 tsubst_flags_t complain)
5208 tree orig_expr = xarg;
5209 tree exp;
5210 int ptrmem = 0;
5212 if (processing_template_decl)
5214 if (type_dependent_expression_p (xarg))
5215 return build_min_nt_loc (loc, code, xarg, NULL_TREE);
5217 xarg = build_non_dependent_expr (xarg);
5220 exp = NULL_TREE;
5222 /* [expr.unary.op] says:
5224 The address of an object of incomplete type can be taken.
5226 (And is just the ordinary address operator, not an overloaded
5227 "operator &".) However, if the type is a template
5228 specialization, we must complete the type at this point so that
5229 an overloaded "operator &" will be available if required. */
5230 if (code == ADDR_EXPR
5231 && TREE_CODE (xarg) != TEMPLATE_ID_EXPR
5232 && ((CLASS_TYPE_P (TREE_TYPE (xarg))
5233 && !COMPLETE_TYPE_P (complete_type (TREE_TYPE (xarg))))
5234 || (TREE_CODE (xarg) == OFFSET_REF)))
5235 /* Don't look for a function. */;
5236 else
5237 exp = build_new_op (loc, code, LOOKUP_NORMAL, xarg, NULL_TREE,
5238 NULL_TREE, /*overload=*/NULL, complain);
5239 if (!exp && code == ADDR_EXPR)
5241 if (is_overloaded_fn (xarg))
5243 tree fn = get_first_fn (xarg);
5244 if (DECL_CONSTRUCTOR_P (fn) || DECL_DESTRUCTOR_P (fn))
5246 if (complain & tf_error)
5247 error (DECL_CONSTRUCTOR_P (fn)
5248 ? G_("taking address of constructor %qE")
5249 : G_("taking address of destructor %qE"),
5250 xarg);
5251 return error_mark_node;
5255 /* A pointer to member-function can be formed only by saying
5256 &X::mf. */
5257 if (!flag_ms_extensions && TREE_CODE (TREE_TYPE (xarg)) == METHOD_TYPE
5258 && (TREE_CODE (xarg) != OFFSET_REF || !PTRMEM_OK_P (xarg)))
5260 if (TREE_CODE (xarg) != OFFSET_REF
5261 || !TYPE_P (TREE_OPERAND (xarg, 0)))
5263 if (complain & tf_error)
5265 error ("invalid use of %qE to form a "
5266 "pointer-to-member-function", xarg);
5267 if (TREE_CODE (xarg) != OFFSET_REF)
5268 inform (input_location, " a qualified-id is required");
5270 return error_mark_node;
5272 else
5274 if (complain & tf_error)
5275 error ("parentheses around %qE cannot be used to form a"
5276 " pointer-to-member-function",
5277 xarg);
5278 else
5279 return error_mark_node;
5280 PTRMEM_OK_P (xarg) = 1;
5284 if (TREE_CODE (xarg) == OFFSET_REF)
5286 ptrmem = PTRMEM_OK_P (xarg);
5288 if (!ptrmem && !flag_ms_extensions
5289 && TREE_CODE (TREE_TYPE (TREE_OPERAND (xarg, 1))) == METHOD_TYPE)
5291 /* A single non-static member, make sure we don't allow a
5292 pointer-to-member. */
5293 xarg = build2 (OFFSET_REF, TREE_TYPE (xarg),
5294 TREE_OPERAND (xarg, 0),
5295 ovl_cons (TREE_OPERAND (xarg, 1), NULL_TREE));
5296 PTRMEM_OK_P (xarg) = ptrmem;
5300 exp = cp_build_addr_expr_strict (xarg, complain);
5303 if (processing_template_decl && exp != error_mark_node)
5304 exp = build_min_non_dep (code, exp, orig_expr,
5305 /*For {PRE,POST}{INC,DEC}REMENT_EXPR*/NULL_TREE);
5306 if (TREE_CODE (exp) == ADDR_EXPR)
5307 PTRMEM_OK_P (exp) = ptrmem;
5308 return exp;
5311 /* Like c_common_truthvalue_conversion, but handle pointer-to-member
5312 constants, where a null value is represented by an INTEGER_CST of
5313 -1. */
5315 tree
5316 cp_truthvalue_conversion (tree expr)
5318 tree type = TREE_TYPE (expr);
5319 if (TYPE_PTRDATAMEM_P (type)
5320 /* Avoid ICE on invalid use of non-static member function. */
5321 || TREE_CODE (expr) == FUNCTION_DECL)
5322 return build_binary_op (EXPR_LOCATION (expr),
5323 NE_EXPR, expr, nullptr_node, 1);
5324 else if (TYPE_PTR_P (type) || TYPE_PTRMEMFUNC_P (type))
5326 /* With -Wzero-as-null-pointer-constant do not warn for an
5327 'if (p)' or a 'while (!p)', where p is a pointer. */
5328 tree ret;
5329 ++c_inhibit_evaluation_warnings;
5330 ret = c_common_truthvalue_conversion (input_location, expr);
5331 --c_inhibit_evaluation_warnings;
5332 return ret;
5334 else
5335 return c_common_truthvalue_conversion (input_location, expr);
5338 /* Just like cp_truthvalue_conversion, but we want a CLEANUP_POINT_EXPR. */
5340 tree
5341 condition_conversion (tree expr)
5343 tree t;
5344 if (processing_template_decl)
5345 return expr;
5346 t = perform_implicit_conversion_flags (boolean_type_node, expr,
5347 tf_warning_or_error, LOOKUP_NORMAL);
5348 t = fold_build_cleanup_point_expr (boolean_type_node, t);
5349 return t;
5352 /* Returns the address of T. This function will fold away
5353 ADDR_EXPR of INDIRECT_REF. */
5355 tree
5356 build_address (tree t)
5358 if (error_operand_p (t) || !cxx_mark_addressable (t))
5359 return error_mark_node;
5360 gcc_checking_assert (TREE_CODE (t) != CONSTRUCTOR);
5361 t = build_fold_addr_expr (t);
5362 if (TREE_CODE (t) != ADDR_EXPR)
5363 t = rvalue (t);
5364 return t;
5367 /* Return a NOP_EXPR converting EXPR to TYPE. */
5369 tree
5370 build_nop (tree type, tree expr)
5372 if (type == error_mark_node || error_operand_p (expr))
5373 return expr;
5374 return build1 (NOP_EXPR, type, expr);
5377 /* Take the address of ARG, whatever that means under C++ semantics.
5378 If STRICT_LVALUE is true, require an lvalue; otherwise, allow xvalues
5379 and class rvalues as well.
5381 Nothing should call this function directly; instead, callers should use
5382 cp_build_addr_expr or cp_build_addr_expr_strict. */
5384 static tree
5385 cp_build_addr_expr_1 (tree arg, bool strict_lvalue, tsubst_flags_t complain)
5387 tree argtype;
5388 tree val;
5390 if (!arg || error_operand_p (arg))
5391 return error_mark_node;
5393 arg = mark_lvalue_use (arg);
5394 argtype = lvalue_type (arg);
5396 gcc_assert (!identifier_p (arg) || !IDENTIFIER_OPNAME_P (arg));
5398 if (TREE_CODE (arg) == COMPONENT_REF && type_unknown_p (arg)
5399 && !really_overloaded_fn (TREE_OPERAND (arg, 1)))
5401 /* They're trying to take the address of a unique non-static
5402 member function. This is ill-formed (except in MS-land),
5403 but let's try to DTRT.
5404 Note: We only handle unique functions here because we don't
5405 want to complain if there's a static overload; non-unique
5406 cases will be handled by instantiate_type. But we need to
5407 handle this case here to allow casts on the resulting PMF.
5408 We could defer this in non-MS mode, but it's easier to give
5409 a useful error here. */
5411 /* Inside constant member functions, the `this' pointer
5412 contains an extra const qualifier. TYPE_MAIN_VARIANT
5413 is used here to remove this const from the diagnostics
5414 and the created OFFSET_REF. */
5415 tree base = TYPE_MAIN_VARIANT (TREE_TYPE (TREE_OPERAND (arg, 0)));
5416 tree fn = get_first_fn (TREE_OPERAND (arg, 1));
5417 if (!mark_used (fn, complain) && !(complain & tf_error))
5418 return error_mark_node;
5420 if (! flag_ms_extensions)
5422 tree name = DECL_NAME (fn);
5423 if (!(complain & tf_error))
5424 return error_mark_node;
5425 else if (current_class_type
5426 && TREE_OPERAND (arg, 0) == current_class_ref)
5427 /* An expression like &memfn. */
5428 permerror (input_location, "ISO C++ forbids taking the address of an unqualified"
5429 " or parenthesized non-static member function to form"
5430 " a pointer to member function. Say %<&%T::%D%>",
5431 base, name);
5432 else
5433 permerror (input_location, "ISO C++ forbids taking the address of a bound member"
5434 " function to form a pointer to member function."
5435 " Say %<&%T::%D%>",
5436 base, name);
5438 arg = build_offset_ref (base, fn, /*address_p=*/true, complain);
5441 /* Uninstantiated types are all functions. Taking the
5442 address of a function is a no-op, so just return the
5443 argument. */
5444 if (type_unknown_p (arg))
5445 return build1 (ADDR_EXPR, unknown_type_node, arg);
5447 if (TREE_CODE (arg) == OFFSET_REF)
5448 /* We want a pointer to member; bypass all the code for actually taking
5449 the address of something. */
5450 goto offset_ref;
5452 /* Anything not already handled and not a true memory reference
5453 is an error. */
5454 if (TREE_CODE (argtype) != FUNCTION_TYPE
5455 && TREE_CODE (argtype) != METHOD_TYPE)
5457 cp_lvalue_kind kind = lvalue_kind (arg);
5458 if (kind == clk_none)
5460 if (complain & tf_error)
5461 lvalue_error (input_location, lv_addressof);
5462 return error_mark_node;
5464 if (strict_lvalue && (kind & (clk_rvalueref|clk_class)))
5466 if (!(complain & tf_error))
5467 return error_mark_node;
5468 if (kind & clk_class)
5469 /* Make this a permerror because we used to accept it. */
5470 permerror (input_location, "taking address of temporary");
5471 else
5472 error ("taking address of xvalue (rvalue reference)");
5476 if (TREE_CODE (argtype) == REFERENCE_TYPE)
5478 tree type = build_pointer_type (TREE_TYPE (argtype));
5479 arg = build1 (CONVERT_EXPR, type, arg);
5480 return arg;
5482 else if (pedantic && DECL_MAIN_P (arg))
5484 /* ARM $3.4 */
5485 /* Apparently a lot of autoconf scripts for C++ packages do this,
5486 so only complain if -Wpedantic. */
5487 if (complain & (flag_pedantic_errors ? tf_error : tf_warning))
5488 pedwarn (input_location, OPT_Wpedantic,
5489 "ISO C++ forbids taking address of function %<::main%>");
5490 else if (flag_pedantic_errors)
5491 return error_mark_node;
5494 /* Let &* cancel out to simplify resulting code. */
5495 if (INDIRECT_REF_P (arg))
5497 /* We don't need to have `current_class_ptr' wrapped in a
5498 NON_LVALUE_EXPR node. */
5499 if (arg == current_class_ref)
5500 return current_class_ptr;
5502 arg = TREE_OPERAND (arg, 0);
5503 if (TREE_CODE (TREE_TYPE (arg)) == REFERENCE_TYPE)
5505 tree type = build_pointer_type (TREE_TYPE (TREE_TYPE (arg)));
5506 arg = build1 (CONVERT_EXPR, type, arg);
5508 else
5509 /* Don't let this be an lvalue. */
5510 arg = rvalue (arg);
5511 return arg;
5514 /* ??? Cope with user tricks that amount to offsetof. */
5515 if (TREE_CODE (argtype) != FUNCTION_TYPE
5516 && TREE_CODE (argtype) != METHOD_TYPE
5517 && argtype != unknown_type_node
5518 && (val = get_base_address (arg))
5519 && COMPLETE_TYPE_P (TREE_TYPE (val))
5520 && INDIRECT_REF_P (val)
5521 && TREE_CONSTANT (TREE_OPERAND (val, 0)))
5523 tree type = build_pointer_type (argtype);
5524 return fold_convert (type, fold_offsetof_1 (arg));
5527 /* Handle complex lvalues (when permitted)
5528 by reduction to simpler cases. */
5529 val = unary_complex_lvalue (ADDR_EXPR, arg);
5530 if (val != 0)
5531 return val;
5533 switch (TREE_CODE (arg))
5535 CASE_CONVERT:
5536 case FLOAT_EXPR:
5537 case FIX_TRUNC_EXPR:
5538 /* Even if we're not being pedantic, we cannot allow this
5539 extension when we're instantiating in a SFINAE
5540 context. */
5541 if (! lvalue_p (arg) && complain == tf_none)
5543 if (complain & tf_error)
5544 permerror (input_location, "ISO C++ forbids taking the address of a cast to a non-lvalue expression");
5545 else
5546 return error_mark_node;
5548 break;
5550 case BASELINK:
5551 arg = BASELINK_FUNCTIONS (arg);
5552 /* Fall through. */
5554 case OVERLOAD:
5555 arg = OVL_CURRENT (arg);
5556 break;
5558 case OFFSET_REF:
5559 offset_ref:
5560 /* Turn a reference to a non-static data member into a
5561 pointer-to-member. */
5563 tree type;
5564 tree t;
5566 gcc_assert (PTRMEM_OK_P (arg));
5568 t = TREE_OPERAND (arg, 1);
5569 if (TREE_CODE (TREE_TYPE (t)) == REFERENCE_TYPE)
5571 if (complain & tf_error)
5572 error ("cannot create pointer to reference member %qD", t);
5573 return error_mark_node;
5576 type = build_ptrmem_type (context_for_name_lookup (t),
5577 TREE_TYPE (t));
5578 t = make_ptrmem_cst (type, TREE_OPERAND (arg, 1));
5579 return t;
5582 default:
5583 break;
5586 if (argtype != error_mark_node)
5587 argtype = build_pointer_type (argtype);
5589 /* In a template, we are processing a non-dependent expression
5590 so we can just form an ADDR_EXPR with the correct type. */
5591 if (processing_template_decl || TREE_CODE (arg) != COMPONENT_REF)
5593 val = build_address (arg);
5594 if (TREE_CODE (arg) == OFFSET_REF)
5595 PTRMEM_OK_P (val) = PTRMEM_OK_P (arg);
5597 else if (BASELINK_P (TREE_OPERAND (arg, 1)))
5599 tree fn = BASELINK_FUNCTIONS (TREE_OPERAND (arg, 1));
5601 /* We can only get here with a single static member
5602 function. */
5603 gcc_assert (TREE_CODE (fn) == FUNCTION_DECL
5604 && DECL_STATIC_FUNCTION_P (fn));
5605 if (!mark_used (fn, complain) && !(complain & tf_error))
5606 return error_mark_node;
5607 val = build_address (fn);
5608 if (TREE_SIDE_EFFECTS (TREE_OPERAND (arg, 0)))
5609 /* Do not lose object's side effects. */
5610 val = build2 (COMPOUND_EXPR, TREE_TYPE (val),
5611 TREE_OPERAND (arg, 0), val);
5613 else if (DECL_C_BIT_FIELD (TREE_OPERAND (arg, 1)))
5615 if (complain & tf_error)
5616 error ("attempt to take address of bit-field structure member %qD",
5617 TREE_OPERAND (arg, 1));
5618 return error_mark_node;
5620 else
5622 tree object = TREE_OPERAND (arg, 0);
5623 tree field = TREE_OPERAND (arg, 1);
5624 gcc_assert (same_type_ignoring_top_level_qualifiers_p
5625 (TREE_TYPE (object), decl_type_context (field)));
5626 val = build_address (arg);
5629 if (TYPE_PTR_P (argtype)
5630 && TREE_CODE (TREE_TYPE (argtype)) == METHOD_TYPE)
5632 build_ptrmemfunc_type (argtype);
5633 val = build_ptrmemfunc (argtype, val, 0,
5634 /*c_cast_p=*/false,
5635 complain);
5638 return val;
5641 /* Take the address of ARG if it has one, even if it's an rvalue. */
5643 tree
5644 cp_build_addr_expr (tree arg, tsubst_flags_t complain)
5646 return cp_build_addr_expr_1 (arg, 0, complain);
5649 /* Take the address of ARG, but only if it's an lvalue. */
5651 static tree
5652 cp_build_addr_expr_strict (tree arg, tsubst_flags_t complain)
5654 return cp_build_addr_expr_1 (arg, 1, complain);
5657 /* C++: Must handle pointers to members.
5659 Perhaps type instantiation should be extended to handle conversion
5660 from aggregates to types we don't yet know we want? (Or are those
5661 cases typically errors which should be reported?)
5663 NOCONVERT nonzero suppresses the default promotions
5664 (such as from short to int). */
5666 tree
5667 cp_build_unary_op (enum tree_code code, tree xarg, int noconvert,
5668 tsubst_flags_t complain)
5670 /* No default_conversion here. It causes trouble for ADDR_EXPR. */
5671 tree arg = xarg;
5672 tree argtype = 0;
5673 const char *errstring = NULL;
5674 tree val;
5675 const char *invalid_op_diag;
5677 if (!arg || error_operand_p (arg))
5678 return error_mark_node;
5680 if ((invalid_op_diag
5681 = targetm.invalid_unary_op ((code == UNARY_PLUS_EXPR
5682 ? CONVERT_EXPR
5683 : code),
5684 TREE_TYPE (xarg))))
5686 if (complain & tf_error)
5687 error (invalid_op_diag);
5688 return error_mark_node;
5691 switch (code)
5693 case UNARY_PLUS_EXPR:
5694 case NEGATE_EXPR:
5696 int flags = WANT_ARITH | WANT_ENUM;
5697 /* Unary plus (but not unary minus) is allowed on pointers. */
5698 if (code == UNARY_PLUS_EXPR)
5699 flags |= WANT_POINTER;
5700 arg = build_expr_type_conversion (flags, arg, true);
5701 if (!arg)
5702 errstring = (code == NEGATE_EXPR
5703 ? _("wrong type argument to unary minus")
5704 : _("wrong type argument to unary plus"));
5705 else
5707 if (!noconvert && CP_INTEGRAL_TYPE_P (TREE_TYPE (arg)))
5708 arg = cp_perform_integral_promotions (arg, complain);
5710 /* Make sure the result is not an lvalue: a unary plus or minus
5711 expression is always a rvalue. */
5712 arg = rvalue (arg);
5715 break;
5717 case BIT_NOT_EXPR:
5718 if (TREE_CODE (TREE_TYPE (arg)) == COMPLEX_TYPE)
5720 code = CONJ_EXPR;
5721 if (!noconvert)
5723 arg = cp_default_conversion (arg, complain);
5724 if (arg == error_mark_node)
5725 return error_mark_node;
5728 else if (!(arg = build_expr_type_conversion (WANT_INT | WANT_ENUM
5729 | WANT_VECTOR_OR_COMPLEX,
5730 arg, true)))
5731 errstring = _("wrong type argument to bit-complement");
5732 else if (!noconvert && CP_INTEGRAL_TYPE_P (TREE_TYPE (arg)))
5733 arg = cp_perform_integral_promotions (arg, complain);
5734 break;
5736 case ABS_EXPR:
5737 if (!(arg = build_expr_type_conversion (WANT_ARITH | WANT_ENUM, arg, true)))
5738 errstring = _("wrong type argument to abs");
5739 else if (!noconvert)
5741 arg = cp_default_conversion (arg, complain);
5742 if (arg == error_mark_node)
5743 return error_mark_node;
5745 break;
5747 case CONJ_EXPR:
5748 /* Conjugating a real value is a no-op, but allow it anyway. */
5749 if (!(arg = build_expr_type_conversion (WANT_ARITH | WANT_ENUM, arg, true)))
5750 errstring = _("wrong type argument to conjugation");
5751 else if (!noconvert)
5753 arg = cp_default_conversion (arg, complain);
5754 if (arg == error_mark_node)
5755 return error_mark_node;
5757 break;
5759 case TRUTH_NOT_EXPR:
5760 if (VECTOR_TYPE_P (TREE_TYPE (arg)))
5761 return cp_build_binary_op (input_location, EQ_EXPR, arg,
5762 build_zero_cst (TREE_TYPE (arg)), complain);
5763 arg = perform_implicit_conversion (boolean_type_node, arg,
5764 complain);
5765 val = invert_truthvalue_loc (input_location, arg);
5766 if (arg != error_mark_node)
5767 return val;
5768 errstring = _("in argument to unary !");
5769 break;
5771 case NOP_EXPR:
5772 break;
5774 case REALPART_EXPR:
5775 case IMAGPART_EXPR:
5776 arg = build_real_imag_expr (input_location, code, arg);
5777 if (arg == error_mark_node)
5778 return arg;
5779 else
5780 return fold_if_not_in_template (arg);
5782 case PREINCREMENT_EXPR:
5783 case POSTINCREMENT_EXPR:
5784 case PREDECREMENT_EXPR:
5785 case POSTDECREMENT_EXPR:
5786 /* Handle complex lvalues (when permitted)
5787 by reduction to simpler cases. */
5789 val = unary_complex_lvalue (code, arg);
5790 if (val != 0)
5791 return val;
5793 arg = mark_lvalue_use (arg);
5795 /* Increment or decrement the real part of the value,
5796 and don't change the imaginary part. */
5797 if (TREE_CODE (TREE_TYPE (arg)) == COMPLEX_TYPE)
5799 tree real, imag;
5801 arg = stabilize_reference (arg);
5802 real = cp_build_unary_op (REALPART_EXPR, arg, 1, complain);
5803 imag = cp_build_unary_op (IMAGPART_EXPR, arg, 1, complain);
5804 real = cp_build_unary_op (code, real, 1, complain);
5805 if (real == error_mark_node || imag == error_mark_node)
5806 return error_mark_node;
5807 return build2 (COMPLEX_EXPR, TREE_TYPE (arg),
5808 real, imag);
5811 /* Report invalid types. */
5813 if (!(arg = build_expr_type_conversion (WANT_ARITH | WANT_POINTER,
5814 arg, true)))
5816 if (code == PREINCREMENT_EXPR)
5817 errstring = _("no pre-increment operator for type");
5818 else if (code == POSTINCREMENT_EXPR)
5819 errstring = _("no post-increment operator for type");
5820 else if (code == PREDECREMENT_EXPR)
5821 errstring = _("no pre-decrement operator for type");
5822 else
5823 errstring = _("no post-decrement operator for type");
5824 break;
5826 else if (arg == error_mark_node)
5827 return error_mark_node;
5829 /* Report something read-only. */
5831 if (CP_TYPE_CONST_P (TREE_TYPE (arg))
5832 || TREE_READONLY (arg))
5834 if (complain & tf_error)
5835 cxx_readonly_error (arg, ((code == PREINCREMENT_EXPR
5836 || code == POSTINCREMENT_EXPR)
5837 ? lv_increment : lv_decrement));
5838 else
5839 return error_mark_node;
5843 tree inc;
5844 tree declared_type = unlowered_expr_type (arg);
5846 argtype = TREE_TYPE (arg);
5848 /* ARM $5.2.5 last annotation says this should be forbidden. */
5849 if (TREE_CODE (argtype) == ENUMERAL_TYPE)
5851 if (complain & tf_error)
5852 permerror (input_location, (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
5853 ? G_("ISO C++ forbids incrementing an enum")
5854 : G_("ISO C++ forbids decrementing an enum"));
5855 else
5856 return error_mark_node;
5859 /* Compute the increment. */
5861 if (TYPE_PTR_P (argtype))
5863 tree type = complete_type (TREE_TYPE (argtype));
5865 if (!COMPLETE_OR_VOID_TYPE_P (type))
5867 if (complain & tf_error)
5868 error (((code == PREINCREMENT_EXPR
5869 || code == POSTINCREMENT_EXPR))
5870 ? G_("cannot increment a pointer to incomplete type %qT")
5871 : G_("cannot decrement a pointer to incomplete type %qT"),
5872 TREE_TYPE (argtype));
5873 else
5874 return error_mark_node;
5876 else if (!TYPE_PTROB_P (argtype))
5878 if (complain & tf_error)
5879 pedwarn (input_location, OPT_Wpointer_arith,
5880 (code == PREINCREMENT_EXPR
5881 || code == POSTINCREMENT_EXPR)
5882 ? G_("ISO C++ forbids incrementing a pointer of type %qT")
5883 : G_("ISO C++ forbids decrementing a pointer of type %qT"),
5884 argtype);
5885 else
5886 return error_mark_node;
5889 inc = cxx_sizeof_nowarn (TREE_TYPE (argtype));
5891 else
5892 inc = VECTOR_TYPE_P (argtype)
5893 ? build_one_cst (argtype)
5894 : integer_one_node;
5896 inc = cp_convert (argtype, inc, complain);
5898 /* If 'arg' is an Objective-C PROPERTY_REF expression, then we
5899 need to ask Objective-C to build the increment or decrement
5900 expression for it. */
5901 if (objc_is_property_ref (arg))
5902 return objc_build_incr_expr_for_property_ref (input_location, code,
5903 arg, inc);
5905 /* Complain about anything else that is not a true lvalue. */
5906 if (!lvalue_or_else (arg, ((code == PREINCREMENT_EXPR
5907 || code == POSTINCREMENT_EXPR)
5908 ? lv_increment : lv_decrement),
5909 complain))
5910 return error_mark_node;
5912 /* Forbid using -- on `bool'. */
5913 if (TREE_CODE (declared_type) == BOOLEAN_TYPE)
5915 if (code == POSTDECREMENT_EXPR || code == PREDECREMENT_EXPR)
5917 if (complain & tf_error)
5918 error ("invalid use of Boolean expression as operand "
5919 "to %<operator--%>");
5920 return error_mark_node;
5922 val = boolean_increment (code, arg);
5924 else if (code == POSTINCREMENT_EXPR || code == POSTDECREMENT_EXPR)
5925 /* An rvalue has no cv-qualifiers. */
5926 val = build2 (code, cv_unqualified (TREE_TYPE (arg)), arg, inc);
5927 else
5928 val = build2 (code, TREE_TYPE (arg), arg, inc);
5930 TREE_SIDE_EFFECTS (val) = 1;
5931 return val;
5934 case ADDR_EXPR:
5935 /* Note that this operation never does default_conversion
5936 regardless of NOCONVERT. */
5937 return cp_build_addr_expr (arg, complain);
5939 default:
5940 break;
5943 if (!errstring)
5945 if (argtype == 0)
5946 argtype = TREE_TYPE (arg);
5947 return fold_if_not_in_template (build1 (code, argtype, arg));
5950 if (complain & tf_error)
5951 error ("%s", errstring);
5952 return error_mark_node;
5955 /* Hook for the c-common bits that build a unary op. */
5956 tree
5957 build_unary_op (location_t /*location*/,
5958 enum tree_code code, tree xarg, int noconvert)
5960 return cp_build_unary_op (code, xarg, noconvert, tf_warning_or_error);
5963 /* Apply unary lvalue-demanding operator CODE to the expression ARG
5964 for certain kinds of expressions which are not really lvalues
5965 but which we can accept as lvalues.
5967 If ARG is not a kind of expression we can handle, return
5968 NULL_TREE. */
5970 tree
5971 unary_complex_lvalue (enum tree_code code, tree arg)
5973 /* Inside a template, making these kinds of adjustments is
5974 pointless; we are only concerned with the type of the
5975 expression. */
5976 if (processing_template_decl)
5977 return NULL_TREE;
5979 /* Handle (a, b) used as an "lvalue". */
5980 if (TREE_CODE (arg) == COMPOUND_EXPR)
5982 tree real_result = cp_build_unary_op (code, TREE_OPERAND (arg, 1), 0,
5983 tf_warning_or_error);
5984 return build2 (COMPOUND_EXPR, TREE_TYPE (real_result),
5985 TREE_OPERAND (arg, 0), real_result);
5988 /* Handle (a ? b : c) used as an "lvalue". */
5989 if (TREE_CODE (arg) == COND_EXPR
5990 || TREE_CODE (arg) == MIN_EXPR || TREE_CODE (arg) == MAX_EXPR)
5991 return rationalize_conditional_expr (code, arg, tf_warning_or_error);
5993 /* Handle (a = b), (++a), and (--a) used as an "lvalue". */
5994 if (TREE_CODE (arg) == MODIFY_EXPR
5995 || TREE_CODE (arg) == PREINCREMENT_EXPR
5996 || TREE_CODE (arg) == PREDECREMENT_EXPR)
5998 tree lvalue = TREE_OPERAND (arg, 0);
5999 if (TREE_SIDE_EFFECTS (lvalue))
6001 lvalue = stabilize_reference (lvalue);
6002 arg = build2 (TREE_CODE (arg), TREE_TYPE (arg),
6003 lvalue, TREE_OPERAND (arg, 1));
6005 return unary_complex_lvalue
6006 (code, build2 (COMPOUND_EXPR, TREE_TYPE (lvalue), arg, lvalue));
6009 if (code != ADDR_EXPR)
6010 return NULL_TREE;
6012 /* Handle (a = b) used as an "lvalue" for `&'. */
6013 if (TREE_CODE (arg) == MODIFY_EXPR
6014 || TREE_CODE (arg) == INIT_EXPR)
6016 tree real_result = cp_build_unary_op (code, TREE_OPERAND (arg, 0), 0,
6017 tf_warning_or_error);
6018 arg = build2 (COMPOUND_EXPR, TREE_TYPE (real_result),
6019 arg, real_result);
6020 TREE_NO_WARNING (arg) = 1;
6021 return arg;
6024 if (TREE_CODE (TREE_TYPE (arg)) == FUNCTION_TYPE
6025 || TREE_CODE (TREE_TYPE (arg)) == METHOD_TYPE
6026 || TREE_CODE (arg) == OFFSET_REF)
6027 return NULL_TREE;
6029 /* We permit compiler to make function calls returning
6030 objects of aggregate type look like lvalues. */
6032 tree targ = arg;
6034 if (TREE_CODE (targ) == SAVE_EXPR)
6035 targ = TREE_OPERAND (targ, 0);
6037 if (TREE_CODE (targ) == CALL_EXPR && MAYBE_CLASS_TYPE_P (TREE_TYPE (targ)))
6039 if (TREE_CODE (arg) == SAVE_EXPR)
6040 targ = arg;
6041 else
6042 targ = build_cplus_new (TREE_TYPE (arg), arg, tf_warning_or_error);
6043 return build1 (ADDR_EXPR, build_pointer_type (TREE_TYPE (arg)), targ);
6046 if (TREE_CODE (arg) == SAVE_EXPR && INDIRECT_REF_P (targ))
6047 return build3 (SAVE_EXPR, build_pointer_type (TREE_TYPE (arg)),
6048 TREE_OPERAND (targ, 0), current_function_decl, NULL);
6051 /* Don't let anything else be handled specially. */
6052 return NULL_TREE;
6055 /* Mark EXP saying that we need to be able to take the
6056 address of it; it should not be allocated in a register.
6057 Value is true if successful.
6059 C++: we do not allow `current_class_ptr' to be addressable. */
6061 bool
6062 cxx_mark_addressable (tree exp)
6064 tree x = exp;
6066 while (1)
6067 switch (TREE_CODE (x))
6069 case ADDR_EXPR:
6070 case COMPONENT_REF:
6071 case ARRAY_REF:
6072 case REALPART_EXPR:
6073 case IMAGPART_EXPR:
6074 x = TREE_OPERAND (x, 0);
6075 break;
6077 case PARM_DECL:
6078 if (x == current_class_ptr)
6080 error ("cannot take the address of %<this%>, which is an rvalue expression");
6081 TREE_ADDRESSABLE (x) = 1; /* so compiler doesn't die later. */
6082 return true;
6084 /* Fall through. */
6086 case VAR_DECL:
6087 /* Caller should not be trying to mark initialized
6088 constant fields addressable. */
6089 gcc_assert (DECL_LANG_SPECIFIC (x) == 0
6090 || DECL_IN_AGGR_P (x) == 0
6091 || TREE_STATIC (x)
6092 || DECL_EXTERNAL (x));
6093 /* Fall through. */
6095 case RESULT_DECL:
6096 if (DECL_REGISTER (x) && !TREE_ADDRESSABLE (x)
6097 && !DECL_ARTIFICIAL (x))
6099 if (VAR_P (x) && DECL_HARD_REGISTER (x))
6101 error
6102 ("address of explicit register variable %qD requested", x);
6103 return false;
6105 else if (extra_warnings)
6106 warning
6107 (OPT_Wextra, "address requested for %qD, which is declared %<register%>", x);
6109 TREE_ADDRESSABLE (x) = 1;
6110 return true;
6112 case CONST_DECL:
6113 case FUNCTION_DECL:
6114 TREE_ADDRESSABLE (x) = 1;
6115 return true;
6117 case CONSTRUCTOR:
6118 TREE_ADDRESSABLE (x) = 1;
6119 return true;
6121 case TARGET_EXPR:
6122 TREE_ADDRESSABLE (x) = 1;
6123 cxx_mark_addressable (TREE_OPERAND (x, 0));
6124 return true;
6126 default:
6127 return true;
6131 /* Build and return a conditional expression IFEXP ? OP1 : OP2. */
6133 tree
6134 build_x_conditional_expr (location_t loc, tree ifexp, tree op1, tree op2,
6135 tsubst_flags_t complain)
6137 tree orig_ifexp = ifexp;
6138 tree orig_op1 = op1;
6139 tree orig_op2 = op2;
6140 tree expr;
6142 if (processing_template_decl)
6144 /* The standard says that the expression is type-dependent if
6145 IFEXP is type-dependent, even though the eventual type of the
6146 expression doesn't dependent on IFEXP. */
6147 if (type_dependent_expression_p (ifexp)
6148 /* As a GNU extension, the middle operand may be omitted. */
6149 || (op1 && type_dependent_expression_p (op1))
6150 || type_dependent_expression_p (op2))
6151 return build_min_nt_loc (loc, COND_EXPR, ifexp, op1, op2);
6152 ifexp = build_non_dependent_expr (ifexp);
6153 if (op1)
6154 op1 = build_non_dependent_expr (op1);
6155 op2 = build_non_dependent_expr (op2);
6158 expr = build_conditional_expr (loc, ifexp, op1, op2, complain);
6159 if (processing_template_decl && expr != error_mark_node
6160 && TREE_CODE (expr) != VEC_COND_EXPR)
6162 tree min = build_min_non_dep (COND_EXPR, expr,
6163 orig_ifexp, orig_op1, orig_op2);
6164 /* In C++11, remember that the result is an lvalue or xvalue.
6165 In C++98, lvalue_kind can just assume lvalue in a template. */
6166 if (cxx_dialect >= cxx11
6167 && lvalue_or_rvalue_with_address_p (expr)
6168 && !lvalue_or_rvalue_with_address_p (min))
6169 TREE_TYPE (min) = cp_build_reference_type (TREE_TYPE (min),
6170 !real_lvalue_p (expr));
6171 expr = convert_from_reference (min);
6173 return expr;
6176 /* Given a list of expressions, return a compound expression
6177 that performs them all and returns the value of the last of them. */
6179 tree
6180 build_x_compound_expr_from_list (tree list, expr_list_kind exp,
6181 tsubst_flags_t complain)
6183 tree expr = TREE_VALUE (list);
6185 if (BRACE_ENCLOSED_INITIALIZER_P (expr)
6186 && !CONSTRUCTOR_IS_DIRECT_INIT (expr))
6188 if (complain & tf_error)
6189 pedwarn (EXPR_LOC_OR_LOC (expr, input_location), 0,
6190 "list-initializer for non-class type must not "
6191 "be parenthesized");
6192 else
6193 return error_mark_node;
6196 if (TREE_CHAIN (list))
6198 if (complain & tf_error)
6199 switch (exp)
6201 case ELK_INIT:
6202 permerror (input_location, "expression list treated as compound "
6203 "expression in initializer");
6204 break;
6205 case ELK_MEM_INIT:
6206 permerror (input_location, "expression list treated as compound "
6207 "expression in mem-initializer");
6208 break;
6209 case ELK_FUNC_CAST:
6210 permerror (input_location, "expression list treated as compound "
6211 "expression in functional cast");
6212 break;
6213 default:
6214 gcc_unreachable ();
6216 else
6217 return error_mark_node;
6219 for (list = TREE_CHAIN (list); list; list = TREE_CHAIN (list))
6220 expr = build_x_compound_expr (EXPR_LOCATION (TREE_VALUE (list)),
6221 expr, TREE_VALUE (list), complain);
6224 return expr;
6227 /* Like build_x_compound_expr_from_list, but using a VEC. */
6229 tree
6230 build_x_compound_expr_from_vec (vec<tree, va_gc> *vec, const char *msg,
6231 tsubst_flags_t complain)
6233 if (vec_safe_is_empty (vec))
6234 return NULL_TREE;
6235 else if (vec->length () == 1)
6236 return (*vec)[0];
6237 else
6239 tree expr;
6240 unsigned int ix;
6241 tree t;
6243 if (msg != NULL)
6245 if (complain & tf_error)
6246 permerror (input_location,
6247 "%s expression list treated as compound expression",
6248 msg);
6249 else
6250 return error_mark_node;
6253 expr = (*vec)[0];
6254 for (ix = 1; vec->iterate (ix, &t); ++ix)
6255 expr = build_x_compound_expr (EXPR_LOCATION (t), expr,
6256 t, complain);
6258 return expr;
6262 /* Handle overloading of the ',' operator when needed. */
6264 tree
6265 build_x_compound_expr (location_t loc, tree op1, tree op2,
6266 tsubst_flags_t complain)
6268 tree result;
6269 tree orig_op1 = op1;
6270 tree orig_op2 = op2;
6272 if (processing_template_decl)
6274 if (type_dependent_expression_p (op1)
6275 || type_dependent_expression_p (op2))
6276 return build_min_nt_loc (loc, COMPOUND_EXPR, op1, op2);
6277 op1 = build_non_dependent_expr (op1);
6278 op2 = build_non_dependent_expr (op2);
6281 result = build_new_op (loc, COMPOUND_EXPR, LOOKUP_NORMAL, op1, op2,
6282 NULL_TREE, /*overload=*/NULL, complain);
6283 if (!result)
6284 result = cp_build_compound_expr (op1, op2, complain);
6286 if (processing_template_decl && result != error_mark_node)
6287 return build_min_non_dep (COMPOUND_EXPR, result, orig_op1, orig_op2);
6289 return result;
6292 /* Like cp_build_compound_expr, but for the c-common bits. */
6294 tree
6295 build_compound_expr (location_t /*loc*/, tree lhs, tree rhs)
6297 return cp_build_compound_expr (lhs, rhs, tf_warning_or_error);
6300 /* Build a compound expression. */
6302 tree
6303 cp_build_compound_expr (tree lhs, tree rhs, tsubst_flags_t complain)
6305 lhs = convert_to_void (lhs, ICV_LEFT_OF_COMMA, complain);
6307 if (lhs == error_mark_node || rhs == error_mark_node)
6308 return error_mark_node;
6310 if (flag_cilkplus
6311 && (TREE_CODE (lhs) == CILK_SPAWN_STMT
6312 || TREE_CODE (rhs) == CILK_SPAWN_STMT))
6314 location_t loc = (EXPR_HAS_LOCATION (lhs) ? EXPR_LOCATION (lhs)
6315 : EXPR_LOCATION (rhs));
6316 error_at (loc,
6317 "spawned function call cannot be part of a comma expression");
6318 return error_mark_node;
6321 if (TREE_CODE (rhs) == TARGET_EXPR)
6323 /* If the rhs is a TARGET_EXPR, then build the compound
6324 expression inside the target_expr's initializer. This
6325 helps the compiler to eliminate unnecessary temporaries. */
6326 tree init = TREE_OPERAND (rhs, 1);
6328 init = build2 (COMPOUND_EXPR, TREE_TYPE (init), lhs, init);
6329 TREE_OPERAND (rhs, 1) = init;
6331 return rhs;
6334 if (type_unknown_p (rhs))
6336 if (complain & tf_error)
6337 error ("no context to resolve type of %qE", rhs);
6338 return error_mark_node;
6341 return build2 (COMPOUND_EXPR, TREE_TYPE (rhs), lhs, rhs);
6344 /* Issue a diagnostic message if casting from SRC_TYPE to DEST_TYPE
6345 casts away constness. CAST gives the type of cast. Returns true
6346 if the cast is ill-formed, false if it is well-formed.
6348 ??? This function warns for casting away any qualifier not just
6349 const. We would like to specify exactly what qualifiers are casted
6350 away.
6353 static bool
6354 check_for_casting_away_constness (tree src_type, tree dest_type,
6355 enum tree_code cast, tsubst_flags_t complain)
6357 /* C-style casts are allowed to cast away constness. With
6358 WARN_CAST_QUAL, we still want to issue a warning. */
6359 if (cast == CAST_EXPR && !warn_cast_qual)
6360 return false;
6362 if (!casts_away_constness (src_type, dest_type, complain))
6363 return false;
6365 switch (cast)
6367 case CAST_EXPR:
6368 if (complain & tf_warning)
6369 warning (OPT_Wcast_qual,
6370 "cast from type %qT to type %qT casts away qualifiers",
6371 src_type, dest_type);
6372 return false;
6374 case STATIC_CAST_EXPR:
6375 if (complain & tf_error)
6376 error ("static_cast from type %qT to type %qT casts away qualifiers",
6377 src_type, dest_type);
6378 return true;
6380 case REINTERPRET_CAST_EXPR:
6381 if (complain & tf_error)
6382 error ("reinterpret_cast from type %qT to type %qT casts away qualifiers",
6383 src_type, dest_type);
6384 return true;
6386 default:
6387 gcc_unreachable();
6392 Warns if the cast from expression EXPR to type TYPE is useless.
6394 void
6395 maybe_warn_about_useless_cast (tree type, tree expr, tsubst_flags_t complain)
6397 if (warn_useless_cast
6398 && complain & tf_warning)
6400 if ((TREE_CODE (type) == REFERENCE_TYPE
6401 && (TYPE_REF_IS_RVALUE (type)
6402 ? xvalue_p (expr) : real_lvalue_p (expr))
6403 && same_type_p (TREE_TYPE (expr), TREE_TYPE (type)))
6404 || same_type_p (TREE_TYPE (expr), type))
6405 warning (OPT_Wuseless_cast, "useless cast to type %qT", type);
6409 /* Convert EXPR (an expression with pointer-to-member type) to TYPE
6410 (another pointer-to-member type in the same hierarchy) and return
6411 the converted expression. If ALLOW_INVERSE_P is permitted, a
6412 pointer-to-derived may be converted to pointer-to-base; otherwise,
6413 only the other direction is permitted. If C_CAST_P is true, this
6414 conversion is taking place as part of a C-style cast. */
6416 tree
6417 convert_ptrmem (tree type, tree expr, bool allow_inverse_p,
6418 bool c_cast_p, tsubst_flags_t complain)
6420 if (TYPE_PTRDATAMEM_P (type))
6422 tree delta;
6424 if (TREE_CODE (expr) == PTRMEM_CST)
6425 expr = cplus_expand_constant (expr);
6426 delta = get_delta_difference (TYPE_PTRMEM_CLASS_TYPE (TREE_TYPE (expr)),
6427 TYPE_PTRMEM_CLASS_TYPE (type),
6428 allow_inverse_p,
6429 c_cast_p, complain);
6430 if (delta == error_mark_node)
6431 return error_mark_node;
6433 if (!integer_zerop (delta))
6435 tree cond, op1, op2;
6437 cond = cp_build_binary_op (input_location,
6438 EQ_EXPR,
6439 expr,
6440 build_int_cst (TREE_TYPE (expr), -1),
6441 complain);
6442 op1 = build_nop (ptrdiff_type_node, expr);
6443 op2 = cp_build_binary_op (input_location,
6444 PLUS_EXPR, op1, delta,
6445 complain);
6447 expr = fold_build3_loc (input_location,
6448 COND_EXPR, ptrdiff_type_node, cond, op1, op2);
6452 return build_nop (type, expr);
6454 else
6455 return build_ptrmemfunc (TYPE_PTRMEMFUNC_FN_TYPE (type), expr,
6456 allow_inverse_p, c_cast_p, complain);
6459 /* Perform a static_cast from EXPR to TYPE. When C_CAST_P is true,
6460 this static_cast is being attempted as one of the possible casts
6461 allowed by a C-style cast. (In that case, accessibility of base
6462 classes is not considered, and it is OK to cast away
6463 constness.) Return the result of the cast. *VALID_P is set to
6464 indicate whether or not the cast was valid. */
6466 static tree
6467 build_static_cast_1 (tree type, tree expr, bool c_cast_p,
6468 bool *valid_p, tsubst_flags_t complain)
6470 tree intype;
6471 tree result;
6472 cp_lvalue_kind clk;
6474 /* Assume the cast is valid. */
6475 *valid_p = true;
6477 intype = unlowered_expr_type (expr);
6479 /* Save casted types in the function's used types hash table. */
6480 used_types_insert (type);
6482 /* [expr.static.cast]
6484 An lvalue of type "cv1 B", where B is a class type, can be cast
6485 to type "reference to cv2 D", where D is a class derived (clause
6486 _class.derived_) from B, if a valid standard conversion from
6487 "pointer to D" to "pointer to B" exists (_conv.ptr_), cv2 is the
6488 same cv-qualification as, or greater cv-qualification than, cv1,
6489 and B is not a virtual base class of D. */
6490 /* We check this case before checking the validity of "TYPE t =
6491 EXPR;" below because for this case:
6493 struct B {};
6494 struct D : public B { D(const B&); };
6495 extern B& b;
6496 void f() { static_cast<const D&>(b); }
6498 we want to avoid constructing a new D. The standard is not
6499 completely clear about this issue, but our interpretation is
6500 consistent with other compilers. */
6501 if (TREE_CODE (type) == REFERENCE_TYPE
6502 && CLASS_TYPE_P (TREE_TYPE (type))
6503 && CLASS_TYPE_P (intype)
6504 && (TYPE_REF_IS_RVALUE (type) || real_lvalue_p (expr))
6505 && DERIVED_FROM_P (intype, TREE_TYPE (type))
6506 && can_convert (build_pointer_type (TYPE_MAIN_VARIANT (intype)),
6507 build_pointer_type (TYPE_MAIN_VARIANT
6508 (TREE_TYPE (type))),
6509 complain)
6510 && (c_cast_p
6511 || at_least_as_qualified_p (TREE_TYPE (type), intype)))
6513 tree base;
6515 /* There is a standard conversion from "D*" to "B*" even if "B"
6516 is ambiguous or inaccessible. If this is really a
6517 static_cast, then we check both for inaccessibility and
6518 ambiguity. However, if this is a static_cast being performed
6519 because the user wrote a C-style cast, then accessibility is
6520 not considered. */
6521 base = lookup_base (TREE_TYPE (type), intype,
6522 c_cast_p ? ba_unique : ba_check,
6523 NULL, complain);
6524 expr = build_address (expr);
6526 if (flag_sanitize & SANITIZE_VPTR)
6528 tree ubsan_check
6529 = cp_ubsan_maybe_instrument_downcast (input_location, type, expr);
6530 if (ubsan_check)
6531 expr = ubsan_check;
6534 /* Convert from "B*" to "D*". This function will check that "B"
6535 is not a virtual base of "D". */
6536 expr = build_base_path (MINUS_EXPR, expr, base, /*nonnull=*/false,
6537 complain);
6539 /* Convert the pointer to a reference -- but then remember that
6540 there are no expressions with reference type in C++.
6542 We call rvalue so that there's an actual tree code
6543 (NON_LVALUE_EXPR) for the static_cast; otherwise, if the operand
6544 is a variable with the same type, the conversion would get folded
6545 away, leaving just the variable and causing lvalue_kind to give
6546 the wrong answer. */
6547 return convert_from_reference (rvalue (cp_fold_convert (type, expr)));
6550 /* "A glvalue of type cv1 T1 can be cast to type rvalue reference to
6551 cv2 T2 if cv2 T2 is reference-compatible with cv1 T1 (8.5.3)." */
6552 if (TREE_CODE (type) == REFERENCE_TYPE
6553 && TYPE_REF_IS_RVALUE (type)
6554 && (clk = real_lvalue_p (expr))
6555 && reference_related_p (TREE_TYPE (type), intype)
6556 && (c_cast_p || at_least_as_qualified_p (TREE_TYPE (type), intype)))
6558 if (clk == clk_ordinary)
6560 /* Handle the (non-bit-field) lvalue case here by casting to
6561 lvalue reference and then changing it to an rvalue reference.
6562 Casting an xvalue to rvalue reference will be handled by the
6563 main code path. */
6564 tree lref = cp_build_reference_type (TREE_TYPE (type), false);
6565 result = (perform_direct_initialization_if_possible
6566 (lref, expr, c_cast_p, complain));
6567 result = cp_fold_convert (type, result);
6568 /* Make sure we don't fold back down to a named rvalue reference,
6569 because that would be an lvalue. */
6570 if (DECL_P (result))
6571 result = build1 (NON_LVALUE_EXPR, type, result);
6572 return convert_from_reference (result);
6574 else
6575 /* For a bit-field or packed field, bind to a temporary. */
6576 expr = rvalue (expr);
6579 /* Resolve overloaded address here rather than once in
6580 implicit_conversion and again in the inverse code below. */
6581 if (TYPE_PTRMEMFUNC_P (type) && type_unknown_p (expr))
6583 expr = instantiate_type (type, expr, complain);
6584 intype = TREE_TYPE (expr);
6587 /* [expr.static.cast]
6589 Any expression can be explicitly converted to type cv void. */
6590 if (VOID_TYPE_P (type))
6591 return convert_to_void (expr, ICV_CAST, complain);
6593 /* [class.abstract]
6594 An abstract class shall not be used ... as the type of an explicit
6595 conversion. */
6596 if (abstract_virtuals_error_sfinae (ACU_CAST, type, complain))
6597 return error_mark_node;
6599 /* [expr.static.cast]
6601 An expression e can be explicitly converted to a type T using a
6602 static_cast of the form static_cast<T>(e) if the declaration T
6603 t(e);" is well-formed, for some invented temporary variable
6604 t. */
6605 result = perform_direct_initialization_if_possible (type, expr,
6606 c_cast_p, complain);
6607 if (result)
6609 result = convert_from_reference (result);
6611 /* [expr.static.cast]
6613 If T is a reference type, the result is an lvalue; otherwise,
6614 the result is an rvalue. */
6615 if (TREE_CODE (type) != REFERENCE_TYPE)
6616 result = rvalue (result);
6617 return result;
6620 /* [expr.static.cast]
6622 The inverse of any standard conversion sequence (clause _conv_),
6623 other than the lvalue-to-rvalue (_conv.lval_), array-to-pointer
6624 (_conv.array_), function-to-pointer (_conv.func_), and boolean
6625 (_conv.bool_) conversions, can be performed explicitly using
6626 static_cast subject to the restriction that the explicit
6627 conversion does not cast away constness (_expr.const.cast_), and
6628 the following additional rules for specific cases: */
6629 /* For reference, the conversions not excluded are: integral
6630 promotions, floating point promotion, integral conversions,
6631 floating point conversions, floating-integral conversions,
6632 pointer conversions, and pointer to member conversions. */
6633 /* DR 128
6635 A value of integral _or enumeration_ type can be explicitly
6636 converted to an enumeration type. */
6637 /* The effect of all that is that any conversion between any two
6638 types which are integral, floating, or enumeration types can be
6639 performed. */
6640 if ((INTEGRAL_OR_ENUMERATION_TYPE_P (type)
6641 || SCALAR_FLOAT_TYPE_P (type))
6642 && (INTEGRAL_OR_ENUMERATION_TYPE_P (intype)
6643 || SCALAR_FLOAT_TYPE_P (intype)))
6644 return ocp_convert (type, expr, CONV_C_CAST, LOOKUP_NORMAL, complain);
6646 if (TYPE_PTR_P (type) && TYPE_PTR_P (intype)
6647 && CLASS_TYPE_P (TREE_TYPE (type))
6648 && CLASS_TYPE_P (TREE_TYPE (intype))
6649 && can_convert (build_pointer_type (TYPE_MAIN_VARIANT
6650 (TREE_TYPE (intype))),
6651 build_pointer_type (TYPE_MAIN_VARIANT
6652 (TREE_TYPE (type))),
6653 complain))
6655 tree base;
6657 if (!c_cast_p
6658 && check_for_casting_away_constness (intype, type, STATIC_CAST_EXPR,
6659 complain))
6660 return error_mark_node;
6661 base = lookup_base (TREE_TYPE (type), TREE_TYPE (intype),
6662 c_cast_p ? ba_unique : ba_check,
6663 NULL, complain);
6664 expr = build_base_path (MINUS_EXPR, expr, base, /*nonnull=*/false,
6665 complain);
6667 if (flag_sanitize & SANITIZE_VPTR)
6669 tree ubsan_check
6670 = cp_ubsan_maybe_instrument_downcast (input_location, type, expr);
6671 if (ubsan_check)
6672 expr = ubsan_check;
6675 return cp_fold_convert (type, expr);
6678 if ((TYPE_PTRDATAMEM_P (type) && TYPE_PTRDATAMEM_P (intype))
6679 || (TYPE_PTRMEMFUNC_P (type) && TYPE_PTRMEMFUNC_P (intype)))
6681 tree c1;
6682 tree c2;
6683 tree t1;
6684 tree t2;
6686 c1 = TYPE_PTRMEM_CLASS_TYPE (intype);
6687 c2 = TYPE_PTRMEM_CLASS_TYPE (type);
6689 if (TYPE_PTRDATAMEM_P (type))
6691 t1 = (build_ptrmem_type
6692 (c1,
6693 TYPE_MAIN_VARIANT (TYPE_PTRMEM_POINTED_TO_TYPE (intype))));
6694 t2 = (build_ptrmem_type
6695 (c2,
6696 TYPE_MAIN_VARIANT (TYPE_PTRMEM_POINTED_TO_TYPE (type))));
6698 else
6700 t1 = intype;
6701 t2 = type;
6703 if (can_convert (t1, t2, complain) || can_convert (t2, t1, complain))
6705 if (!c_cast_p
6706 && check_for_casting_away_constness (intype, type,
6707 STATIC_CAST_EXPR,
6708 complain))
6709 return error_mark_node;
6710 return convert_ptrmem (type, expr, /*allow_inverse_p=*/1,
6711 c_cast_p, complain);
6715 /* [expr.static.cast]
6717 An rvalue of type "pointer to cv void" can be explicitly
6718 converted to a pointer to object type. A value of type pointer
6719 to object converted to "pointer to cv void" and back to the
6720 original pointer type will have its original value. */
6721 if (TYPE_PTR_P (intype)
6722 && VOID_TYPE_P (TREE_TYPE (intype))
6723 && TYPE_PTROB_P (type))
6725 if (!c_cast_p
6726 && check_for_casting_away_constness (intype, type, STATIC_CAST_EXPR,
6727 complain))
6728 return error_mark_node;
6729 return build_nop (type, expr);
6732 *valid_p = false;
6733 return error_mark_node;
6736 /* Return an expression representing static_cast<TYPE>(EXPR). */
6738 tree
6739 build_static_cast (tree type, tree expr, tsubst_flags_t complain)
6741 tree result;
6742 bool valid_p;
6744 if (type == error_mark_node || expr == error_mark_node)
6745 return error_mark_node;
6747 if (processing_template_decl)
6749 expr = build_min (STATIC_CAST_EXPR, type, expr);
6750 /* We don't know if it will or will not have side effects. */
6751 TREE_SIDE_EFFECTS (expr) = 1;
6752 return convert_from_reference (expr);
6755 /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
6756 Strip such NOP_EXPRs if VALUE is being used in non-lvalue context. */
6757 if (TREE_CODE (type) != REFERENCE_TYPE
6758 && TREE_CODE (expr) == NOP_EXPR
6759 && TREE_TYPE (expr) == TREE_TYPE (TREE_OPERAND (expr, 0)))
6760 expr = TREE_OPERAND (expr, 0);
6762 result = build_static_cast_1 (type, expr, /*c_cast_p=*/false, &valid_p,
6763 complain);
6764 if (valid_p)
6766 if (result != error_mark_node)
6767 maybe_warn_about_useless_cast (type, expr, complain);
6768 return result;
6771 if (complain & tf_error)
6772 error ("invalid static_cast from type %qT to type %qT",
6773 TREE_TYPE (expr), type);
6774 return error_mark_node;
6777 /* EXPR is an expression with member function or pointer-to-member
6778 function type. TYPE is a pointer type. Converting EXPR to TYPE is
6779 not permitted by ISO C++, but we accept it in some modes. If we
6780 are not in one of those modes, issue a diagnostic. Return the
6781 converted expression. */
6783 tree
6784 convert_member_func_to_ptr (tree type, tree expr, tsubst_flags_t complain)
6786 tree intype;
6787 tree decl;
6789 intype = TREE_TYPE (expr);
6790 gcc_assert (TYPE_PTRMEMFUNC_P (intype)
6791 || TREE_CODE (intype) == METHOD_TYPE);
6793 if (!(complain & tf_warning_or_error))
6794 return error_mark_node;
6796 if (pedantic || warn_pmf2ptr)
6797 pedwarn (input_location, pedantic ? OPT_Wpedantic : OPT_Wpmf_conversions,
6798 "converting from %qT to %qT", intype, type);
6800 if (TREE_CODE (intype) == METHOD_TYPE)
6801 expr = build_addr_func (expr, complain);
6802 else if (TREE_CODE (expr) == PTRMEM_CST)
6803 expr = build_address (PTRMEM_CST_MEMBER (expr));
6804 else
6806 decl = maybe_dummy_object (TYPE_PTRMEM_CLASS_TYPE (intype), 0);
6807 decl = build_address (decl);
6808 expr = get_member_function_from_ptrfunc (&decl, expr, complain);
6811 if (expr == error_mark_node)
6812 return error_mark_node;
6814 return build_nop (type, expr);
6817 /* Return a representation for a reinterpret_cast from EXPR to TYPE.
6818 If C_CAST_P is true, this reinterpret cast is being done as part of
6819 a C-style cast. If VALID_P is non-NULL, *VALID_P is set to
6820 indicate whether or not reinterpret_cast was valid. */
6822 static tree
6823 build_reinterpret_cast_1 (tree type, tree expr, bool c_cast_p,
6824 bool *valid_p, tsubst_flags_t complain)
6826 tree intype;
6828 /* Assume the cast is invalid. */
6829 if (valid_p)
6830 *valid_p = true;
6832 if (type == error_mark_node || error_operand_p (expr))
6833 return error_mark_node;
6835 intype = TREE_TYPE (expr);
6837 /* Save casted types in the function's used types hash table. */
6838 used_types_insert (type);
6840 /* [expr.reinterpret.cast]
6841 An lvalue expression of type T1 can be cast to the type
6842 "reference to T2" if an expression of type "pointer to T1" can be
6843 explicitly converted to the type "pointer to T2" using a
6844 reinterpret_cast. */
6845 if (TREE_CODE (type) == REFERENCE_TYPE)
6847 if (! real_lvalue_p (expr))
6849 if (complain & tf_error)
6850 error ("invalid cast of an rvalue expression of type "
6851 "%qT to type %qT",
6852 intype, type);
6853 return error_mark_node;
6856 /* Warn about a reinterpret_cast from "A*" to "B&" if "A" and
6857 "B" are related class types; the reinterpret_cast does not
6858 adjust the pointer. */
6859 if (TYPE_PTR_P (intype)
6860 && (complain & tf_warning)
6861 && (comptypes (TREE_TYPE (intype), TREE_TYPE (type),
6862 COMPARE_BASE | COMPARE_DERIVED)))
6863 warning (0, "casting %qT to %qT does not dereference pointer",
6864 intype, type);
6866 expr = cp_build_addr_expr (expr, complain);
6868 if (warn_strict_aliasing > 2)
6869 strict_aliasing_warning (TREE_TYPE (expr), type, expr);
6871 if (expr != error_mark_node)
6872 expr = build_reinterpret_cast_1
6873 (build_pointer_type (TREE_TYPE (type)), expr, c_cast_p,
6874 valid_p, complain);
6875 if (expr != error_mark_node)
6876 /* cp_build_indirect_ref isn't right for rvalue refs. */
6877 expr = convert_from_reference (fold_convert (type, expr));
6878 return expr;
6881 /* As a G++ extension, we consider conversions from member
6882 functions, and pointers to member functions to
6883 pointer-to-function and pointer-to-void types. If
6884 -Wno-pmf-conversions has not been specified,
6885 convert_member_func_to_ptr will issue an error message. */
6886 if ((TYPE_PTRMEMFUNC_P (intype)
6887 || TREE_CODE (intype) == METHOD_TYPE)
6888 && TYPE_PTR_P (type)
6889 && (TREE_CODE (TREE_TYPE (type)) == FUNCTION_TYPE
6890 || VOID_TYPE_P (TREE_TYPE (type))))
6891 return convert_member_func_to_ptr (type, expr, complain);
6893 /* If the cast is not to a reference type, the lvalue-to-rvalue,
6894 array-to-pointer, and function-to-pointer conversions are
6895 performed. */
6896 expr = decay_conversion (expr, complain);
6898 /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
6899 Strip such NOP_EXPRs if VALUE is being used in non-lvalue context. */
6900 if (TREE_CODE (expr) == NOP_EXPR
6901 && TREE_TYPE (expr) == TREE_TYPE (TREE_OPERAND (expr, 0)))
6902 expr = TREE_OPERAND (expr, 0);
6904 if (error_operand_p (expr))
6905 return error_mark_node;
6907 intype = TREE_TYPE (expr);
6909 /* [expr.reinterpret.cast]
6910 A pointer can be converted to any integral type large enough to
6911 hold it. ... A value of type std::nullptr_t can be converted to
6912 an integral type; the conversion has the same meaning and
6913 validity as a conversion of (void*)0 to the integral type. */
6914 if (CP_INTEGRAL_TYPE_P (type)
6915 && (TYPE_PTR_P (intype) || NULLPTR_TYPE_P (intype)))
6917 if (TYPE_PRECISION (type) < TYPE_PRECISION (intype))
6919 if (complain & tf_error)
6920 permerror (input_location, "cast from %qT to %qT loses precision",
6921 intype, type);
6922 else
6923 return error_mark_node;
6925 if (NULLPTR_TYPE_P (intype))
6926 return build_int_cst (type, 0);
6928 /* [expr.reinterpret.cast]
6929 A value of integral or enumeration type can be explicitly
6930 converted to a pointer. */
6931 else if (TYPE_PTR_P (type) && INTEGRAL_OR_ENUMERATION_TYPE_P (intype))
6932 /* OK */
6934 else if ((INTEGRAL_OR_ENUMERATION_TYPE_P (type)
6935 || TYPE_PTR_OR_PTRMEM_P (type))
6936 && same_type_p (type, intype))
6937 /* DR 799 */
6938 return rvalue (expr);
6939 else if ((TYPE_PTRFN_P (type) && TYPE_PTRFN_P (intype))
6940 || (TYPE_PTRMEMFUNC_P (type) && TYPE_PTRMEMFUNC_P (intype)))
6941 return fold_if_not_in_template (build_nop (type, expr));
6942 else if ((TYPE_PTRDATAMEM_P (type) && TYPE_PTRDATAMEM_P (intype))
6943 || (TYPE_PTROBV_P (type) && TYPE_PTROBV_P (intype)))
6945 tree sexpr = expr;
6947 if (!c_cast_p
6948 && check_for_casting_away_constness (intype, type,
6949 REINTERPRET_CAST_EXPR,
6950 complain))
6951 return error_mark_node;
6952 /* Warn about possible alignment problems. */
6953 if (STRICT_ALIGNMENT && warn_cast_align
6954 && (complain & tf_warning)
6955 && !VOID_TYPE_P (type)
6956 && TREE_CODE (TREE_TYPE (intype)) != FUNCTION_TYPE
6957 && COMPLETE_TYPE_P (TREE_TYPE (type))
6958 && COMPLETE_TYPE_P (TREE_TYPE (intype))
6959 && TYPE_ALIGN (TREE_TYPE (type)) > TYPE_ALIGN (TREE_TYPE (intype)))
6960 warning (OPT_Wcast_align, "cast from %qT to %qT "
6961 "increases required alignment of target type", intype, type);
6963 /* We need to strip nops here, because the front end likes to
6964 create (int *)&a for array-to-pointer decay, instead of &a[0]. */
6965 STRIP_NOPS (sexpr);
6966 if (warn_strict_aliasing <= 2)
6967 strict_aliasing_warning (intype, type, sexpr);
6969 return fold_if_not_in_template (build_nop (type, expr));
6971 else if ((TYPE_PTRFN_P (type) && TYPE_PTROBV_P (intype))
6972 || (TYPE_PTRFN_P (intype) && TYPE_PTROBV_P (type)))
6974 if (complain & tf_warning)
6975 /* C++11 5.2.10 p8 says that "Converting a function pointer to an
6976 object pointer type or vice versa is conditionally-supported." */
6977 warning (OPT_Wconditionally_supported,
6978 "casting between pointer-to-function and pointer-to-object "
6979 "is conditionally-supported");
6980 return fold_if_not_in_template (build_nop (type, expr));
6982 else if (TREE_CODE (type) == VECTOR_TYPE)
6983 return fold_if_not_in_template (convert_to_vector (type, expr));
6984 else if (TREE_CODE (intype) == VECTOR_TYPE
6985 && INTEGRAL_OR_ENUMERATION_TYPE_P (type))
6986 return fold_if_not_in_template (convert_to_integer (type, expr));
6987 else
6989 if (valid_p)
6990 *valid_p = false;
6991 if (complain & tf_error)
6992 error ("invalid cast from type %qT to type %qT", intype, type);
6993 return error_mark_node;
6996 return cp_convert (type, expr, complain);
6999 tree
7000 build_reinterpret_cast (tree type, tree expr, tsubst_flags_t complain)
7002 tree r;
7004 if (type == error_mark_node || expr == error_mark_node)
7005 return error_mark_node;
7007 if (processing_template_decl)
7009 tree t = build_min (REINTERPRET_CAST_EXPR, type, expr);
7011 if (!TREE_SIDE_EFFECTS (t)
7012 && type_dependent_expression_p (expr))
7013 /* There might turn out to be side effects inside expr. */
7014 TREE_SIDE_EFFECTS (t) = 1;
7015 return convert_from_reference (t);
7018 r = build_reinterpret_cast_1 (type, expr, /*c_cast_p=*/false,
7019 /*valid_p=*/NULL, complain);
7020 if (r != error_mark_node)
7021 maybe_warn_about_useless_cast (type, expr, complain);
7022 return r;
7025 /* Perform a const_cast from EXPR to TYPE. If the cast is valid,
7026 return an appropriate expression. Otherwise, return
7027 error_mark_node. If the cast is not valid, and COMPLAIN is true,
7028 then a diagnostic will be issued. If VALID_P is non-NULL, we are
7029 performing a C-style cast, its value upon return will indicate
7030 whether or not the conversion succeeded. */
7032 static tree
7033 build_const_cast_1 (tree dst_type, tree expr, tsubst_flags_t complain,
7034 bool *valid_p)
7036 tree src_type;
7037 tree reference_type;
7039 /* Callers are responsible for handling error_mark_node as a
7040 destination type. */
7041 gcc_assert (dst_type != error_mark_node);
7042 /* In a template, callers should be building syntactic
7043 representations of casts, not using this machinery. */
7044 gcc_assert (!processing_template_decl);
7046 /* Assume the conversion is invalid. */
7047 if (valid_p)
7048 *valid_p = false;
7050 if (!POINTER_TYPE_P (dst_type) && !TYPE_PTRDATAMEM_P (dst_type))
7052 if (complain & tf_error)
7053 error ("invalid use of const_cast with type %qT, "
7054 "which is not a pointer, "
7055 "reference, nor a pointer-to-data-member type", dst_type);
7056 return error_mark_node;
7059 if (TREE_CODE (TREE_TYPE (dst_type)) == FUNCTION_TYPE)
7061 if (complain & tf_error)
7062 error ("invalid use of const_cast with type %qT, which is a pointer "
7063 "or reference to a function type", dst_type);
7064 return error_mark_node;
7067 /* Save casted types in the function's used types hash table. */
7068 used_types_insert (dst_type);
7070 src_type = TREE_TYPE (expr);
7071 /* Expressions do not really have reference types. */
7072 if (TREE_CODE (src_type) == REFERENCE_TYPE)
7073 src_type = TREE_TYPE (src_type);
7075 /* [expr.const.cast]
7077 For two object types T1 and T2, if a pointer to T1 can be explicitly
7078 converted to the type "pointer to T2" using a const_cast, then the
7079 following conversions can also be made:
7081 -- an lvalue of type T1 can be explicitly converted to an lvalue of
7082 type T2 using the cast const_cast<T2&>;
7084 -- a glvalue of type T1 can be explicitly converted to an xvalue of
7085 type T2 using the cast const_cast<T2&&>; and
7087 -- if T1 is a class type, a prvalue of type T1 can be explicitly
7088 converted to an xvalue of type T2 using the cast const_cast<T2&&>. */
7090 if (TREE_CODE (dst_type) == REFERENCE_TYPE)
7092 reference_type = dst_type;
7093 if (!TYPE_REF_IS_RVALUE (dst_type)
7094 ? real_lvalue_p (expr)
7095 : (CLASS_TYPE_P (TREE_TYPE (dst_type))
7096 ? lvalue_p (expr)
7097 : lvalue_or_rvalue_with_address_p (expr)))
7098 /* OK. */;
7099 else
7101 if (complain & tf_error)
7102 error ("invalid const_cast of an rvalue of type %qT to type %qT",
7103 src_type, dst_type);
7104 return error_mark_node;
7106 dst_type = build_pointer_type (TREE_TYPE (dst_type));
7107 src_type = build_pointer_type (src_type);
7109 else
7111 reference_type = NULL_TREE;
7112 /* If the destination type is not a reference type, the
7113 lvalue-to-rvalue, array-to-pointer, and function-to-pointer
7114 conversions are performed. */
7115 src_type = type_decays_to (src_type);
7116 if (src_type == error_mark_node)
7117 return error_mark_node;
7120 if (TYPE_PTR_P (src_type) || TYPE_PTRDATAMEM_P (src_type))
7122 if (comp_ptr_ttypes_const (dst_type, src_type))
7124 if (valid_p)
7126 *valid_p = true;
7127 /* This cast is actually a C-style cast. Issue a warning if
7128 the user is making a potentially unsafe cast. */
7129 check_for_casting_away_constness (src_type, dst_type,
7130 CAST_EXPR, complain);
7132 if (reference_type)
7134 expr = cp_build_addr_expr (expr, complain);
7135 if (expr == error_mark_node)
7136 return error_mark_node;
7137 expr = build_nop (reference_type, expr);
7138 return convert_from_reference (expr);
7140 else
7142 expr = decay_conversion (expr, complain);
7143 if (expr == error_mark_node)
7144 return error_mark_node;
7146 /* build_c_cast puts on a NOP_EXPR to make the result not an
7147 lvalue. Strip such NOP_EXPRs if VALUE is being used in
7148 non-lvalue context. */
7149 if (TREE_CODE (expr) == NOP_EXPR
7150 && TREE_TYPE (expr) == TREE_TYPE (TREE_OPERAND (expr, 0)))
7151 expr = TREE_OPERAND (expr, 0);
7152 return build_nop (dst_type, expr);
7155 else if (valid_p
7156 && !at_least_as_qualified_p (TREE_TYPE (dst_type),
7157 TREE_TYPE (src_type)))
7158 check_for_casting_away_constness (src_type, dst_type, CAST_EXPR,
7159 complain);
7162 if (complain & tf_error)
7163 error ("invalid const_cast from type %qT to type %qT",
7164 src_type, dst_type);
7165 return error_mark_node;
7168 tree
7169 build_const_cast (tree type, tree expr, tsubst_flags_t complain)
7171 tree r;
7173 if (type == error_mark_node || error_operand_p (expr))
7174 return error_mark_node;
7176 if (processing_template_decl)
7178 tree t = build_min (CONST_CAST_EXPR, type, expr);
7180 if (!TREE_SIDE_EFFECTS (t)
7181 && type_dependent_expression_p (expr))
7182 /* There might turn out to be side effects inside expr. */
7183 TREE_SIDE_EFFECTS (t) = 1;
7184 return convert_from_reference (t);
7187 r = build_const_cast_1 (type, expr, complain, /*valid_p=*/NULL);
7188 if (r != error_mark_node)
7189 maybe_warn_about_useless_cast (type, expr, complain);
7190 return r;
7193 /* Like cp_build_c_cast, but for the c-common bits. */
7195 tree
7196 build_c_cast (location_t /*loc*/, tree type, tree expr)
7198 return cp_build_c_cast (type, expr, tf_warning_or_error);
7201 /* Build an expression representing an explicit C-style cast to type
7202 TYPE of expression EXPR. */
7204 tree
7205 cp_build_c_cast (tree type, tree expr, tsubst_flags_t complain)
7207 tree value = expr;
7208 tree result;
7209 bool valid_p;
7211 if (type == error_mark_node || error_operand_p (expr))
7212 return error_mark_node;
7214 if (processing_template_decl)
7216 tree t = build_min (CAST_EXPR, type,
7217 tree_cons (NULL_TREE, value, NULL_TREE));
7218 /* We don't know if it will or will not have side effects. */
7219 TREE_SIDE_EFFECTS (t) = 1;
7220 return convert_from_reference (t);
7223 /* Casts to a (pointer to a) specific ObjC class (or 'id' or
7224 'Class') should always be retained, because this information aids
7225 in method lookup. */
7226 if (objc_is_object_ptr (type)
7227 && objc_is_object_ptr (TREE_TYPE (expr)))
7228 return build_nop (type, expr);
7230 /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
7231 Strip such NOP_EXPRs if VALUE is being used in non-lvalue context. */
7232 if (TREE_CODE (type) != REFERENCE_TYPE
7233 && TREE_CODE (value) == NOP_EXPR
7234 && TREE_TYPE (value) == TREE_TYPE (TREE_OPERAND (value, 0)))
7235 value = TREE_OPERAND (value, 0);
7237 if (TREE_CODE (type) == ARRAY_TYPE)
7239 /* Allow casting from T1* to T2[] because Cfront allows it.
7240 NIHCL uses it. It is not valid ISO C++ however. */
7241 if (TYPE_PTR_P (TREE_TYPE (expr)))
7243 if (complain & tf_error)
7244 permerror (input_location, "ISO C++ forbids casting to an array type %qT", type);
7245 else
7246 return error_mark_node;
7247 type = build_pointer_type (TREE_TYPE (type));
7249 else
7251 if (complain & tf_error)
7252 error ("ISO C++ forbids casting to an array type %qT", type);
7253 return error_mark_node;
7257 if (TREE_CODE (type) == FUNCTION_TYPE
7258 || TREE_CODE (type) == METHOD_TYPE)
7260 if (complain & tf_error)
7261 error ("invalid cast to function type %qT", type);
7262 return error_mark_node;
7265 if (TYPE_PTR_P (type)
7266 && TREE_CODE (TREE_TYPE (value)) == INTEGER_TYPE
7267 /* Casting to an integer of smaller size is an error detected elsewhere. */
7268 && TYPE_PRECISION (type) > TYPE_PRECISION (TREE_TYPE (value))
7269 /* Don't warn about converting any constant. */
7270 && !TREE_CONSTANT (value))
7271 warning_at (input_location, OPT_Wint_to_pointer_cast,
7272 "cast to pointer from integer of different size");
7274 /* A C-style cast can be a const_cast. */
7275 result = build_const_cast_1 (type, value, complain & tf_warning,
7276 &valid_p);
7277 if (valid_p)
7279 if (result != error_mark_node)
7280 maybe_warn_about_useless_cast (type, value, complain);
7281 return result;
7284 /* Or a static cast. */
7285 result = build_static_cast_1 (type, value, /*c_cast_p=*/true,
7286 &valid_p, complain);
7287 /* Or a reinterpret_cast. */
7288 if (!valid_p)
7289 result = build_reinterpret_cast_1 (type, value, /*c_cast_p=*/true,
7290 &valid_p, complain);
7291 /* The static_cast or reinterpret_cast may be followed by a
7292 const_cast. */
7293 if (valid_p
7294 /* A valid cast may result in errors if, for example, a
7295 conversion to an ambiguous base class is required. */
7296 && !error_operand_p (result))
7298 tree result_type;
7300 maybe_warn_about_useless_cast (type, value, complain);
7302 /* Non-class rvalues always have cv-unqualified type. */
7303 if (!CLASS_TYPE_P (type))
7304 type = TYPE_MAIN_VARIANT (type);
7305 result_type = TREE_TYPE (result);
7306 if (!CLASS_TYPE_P (result_type) && TREE_CODE (type) != REFERENCE_TYPE)
7307 result_type = TYPE_MAIN_VARIANT (result_type);
7308 /* If the type of RESULT does not match TYPE, perform a
7309 const_cast to make it match. If the static_cast or
7310 reinterpret_cast succeeded, we will differ by at most
7311 cv-qualification, so the follow-on const_cast is guaranteed
7312 to succeed. */
7313 if (!same_type_p (non_reference (type), non_reference (result_type)))
7315 result = build_const_cast_1 (type, result, false, &valid_p);
7316 gcc_assert (valid_p);
7318 return result;
7321 return error_mark_node;
7324 /* For use from the C common bits. */
7325 tree
7326 build_modify_expr (location_t /*location*/,
7327 tree lhs, tree /*lhs_origtype*/,
7328 enum tree_code modifycode,
7329 location_t /*rhs_location*/, tree rhs,
7330 tree /*rhs_origtype*/)
7332 return cp_build_modify_expr (lhs, modifycode, rhs, tf_warning_or_error);
7335 /* Build an assignment expression of lvalue LHS from value RHS.
7336 MODIFYCODE is the code for a binary operator that we use
7337 to combine the old value of LHS with RHS to get the new value.
7338 Or else MODIFYCODE is NOP_EXPR meaning do a simple assignment.
7340 C++: If MODIFYCODE is INIT_EXPR, then leave references unbashed. */
7342 tree
7343 cp_build_modify_expr (tree lhs, enum tree_code modifycode, tree rhs,
7344 tsubst_flags_t complain)
7346 tree result;
7347 tree newrhs = rhs;
7348 tree lhstype = TREE_TYPE (lhs);
7349 tree olhstype = lhstype;
7350 bool plain_assign = (modifycode == NOP_EXPR);
7352 /* Avoid duplicate error messages from operands that had errors. */
7353 if (error_operand_p (lhs) || error_operand_p (rhs))
7354 return error_mark_node;
7356 /* Handle control structure constructs used as "lvalues". */
7357 switch (TREE_CODE (lhs))
7359 /* Handle --foo = 5; as these are valid constructs in C++. */
7360 case PREDECREMENT_EXPR:
7361 case PREINCREMENT_EXPR:
7362 if (TREE_SIDE_EFFECTS (TREE_OPERAND (lhs, 0)))
7363 lhs = build2 (TREE_CODE (lhs), TREE_TYPE (lhs),
7364 stabilize_reference (TREE_OPERAND (lhs, 0)),
7365 TREE_OPERAND (lhs, 1));
7366 newrhs = cp_build_modify_expr (TREE_OPERAND (lhs, 0),
7367 modifycode, rhs, complain);
7368 if (newrhs == error_mark_node)
7369 return error_mark_node;
7370 return build2 (COMPOUND_EXPR, lhstype, lhs, newrhs);
7372 /* Handle (a, b) used as an "lvalue". */
7373 case COMPOUND_EXPR:
7374 newrhs = cp_build_modify_expr (TREE_OPERAND (lhs, 1),
7375 modifycode, rhs, complain);
7376 if (newrhs == error_mark_node)
7377 return error_mark_node;
7378 return build2 (COMPOUND_EXPR, lhstype,
7379 TREE_OPERAND (lhs, 0), newrhs);
7381 case MODIFY_EXPR:
7382 if (TREE_SIDE_EFFECTS (TREE_OPERAND (lhs, 0)))
7383 lhs = build2 (TREE_CODE (lhs), TREE_TYPE (lhs),
7384 stabilize_reference (TREE_OPERAND (lhs, 0)),
7385 TREE_OPERAND (lhs, 1));
7386 newrhs = cp_build_modify_expr (TREE_OPERAND (lhs, 0), modifycode, rhs,
7387 complain);
7388 if (newrhs == error_mark_node)
7389 return error_mark_node;
7390 return build2 (COMPOUND_EXPR, lhstype, lhs, newrhs);
7392 case MIN_EXPR:
7393 case MAX_EXPR:
7394 /* MIN_EXPR and MAX_EXPR are currently only permitted as lvalues,
7395 when neither operand has side-effects. */
7396 if (!lvalue_or_else (lhs, lv_assign, complain))
7397 return error_mark_node;
7399 gcc_assert (!TREE_SIDE_EFFECTS (TREE_OPERAND (lhs, 0))
7400 && !TREE_SIDE_EFFECTS (TREE_OPERAND (lhs, 1)));
7402 lhs = build3 (COND_EXPR, TREE_TYPE (lhs),
7403 build2 (TREE_CODE (lhs) == MIN_EXPR ? LE_EXPR : GE_EXPR,
7404 boolean_type_node,
7405 TREE_OPERAND (lhs, 0),
7406 TREE_OPERAND (lhs, 1)),
7407 TREE_OPERAND (lhs, 0),
7408 TREE_OPERAND (lhs, 1));
7409 /* Fall through. */
7411 /* Handle (a ? b : c) used as an "lvalue". */
7412 case COND_EXPR:
7414 /* Produce (a ? (b = rhs) : (c = rhs))
7415 except that the RHS goes through a save-expr
7416 so the code to compute it is only emitted once. */
7417 tree cond;
7418 tree preeval = NULL_TREE;
7420 if (VOID_TYPE_P (TREE_TYPE (rhs)))
7422 if (complain & tf_error)
7423 error ("void value not ignored as it ought to be");
7424 return error_mark_node;
7427 rhs = stabilize_expr (rhs, &preeval);
7429 /* Check this here to avoid odd errors when trying to convert
7430 a throw to the type of the COND_EXPR. */
7431 if (!lvalue_or_else (lhs, lv_assign, complain))
7432 return error_mark_node;
7434 cond = build_conditional_expr
7435 (input_location, TREE_OPERAND (lhs, 0),
7436 cp_build_modify_expr (TREE_OPERAND (lhs, 1),
7437 modifycode, rhs, complain),
7438 cp_build_modify_expr (TREE_OPERAND (lhs, 2),
7439 modifycode, rhs, complain),
7440 complain);
7442 if (cond == error_mark_node)
7443 return cond;
7444 /* Make sure the code to compute the rhs comes out
7445 before the split. */
7446 if (preeval)
7447 cond = build2 (COMPOUND_EXPR, TREE_TYPE (lhs), preeval, cond);
7448 return cond;
7451 default:
7452 break;
7455 if (modifycode == INIT_EXPR)
7457 if (BRACE_ENCLOSED_INITIALIZER_P (rhs))
7458 /* Do the default thing. */;
7459 else if (TREE_CODE (rhs) == CONSTRUCTOR)
7461 /* Compound literal. */
7462 if (! same_type_p (TREE_TYPE (rhs), lhstype))
7463 /* Call convert to generate an error; see PR 11063. */
7464 rhs = convert (lhstype, rhs);
7465 result = build2 (INIT_EXPR, lhstype, lhs, rhs);
7466 TREE_SIDE_EFFECTS (result) = 1;
7467 return result;
7469 else if (! MAYBE_CLASS_TYPE_P (lhstype))
7470 /* Do the default thing. */;
7471 else
7473 vec<tree, va_gc> *rhs_vec = make_tree_vector_single (rhs);
7474 result = build_special_member_call (lhs, complete_ctor_identifier,
7475 &rhs_vec, lhstype, LOOKUP_NORMAL,
7476 complain);
7477 release_tree_vector (rhs_vec);
7478 if (result == NULL_TREE)
7479 return error_mark_node;
7480 return result;
7483 else
7485 lhs = require_complete_type_sfinae (lhs, complain);
7486 if (lhs == error_mark_node)
7487 return error_mark_node;
7489 if (modifycode == NOP_EXPR)
7491 if (c_dialect_objc ())
7493 result = objc_maybe_build_modify_expr (lhs, rhs);
7494 if (result)
7495 return result;
7498 /* `operator=' is not an inheritable operator. */
7499 if (! MAYBE_CLASS_TYPE_P (lhstype))
7500 /* Do the default thing. */;
7501 else
7503 result = build_new_op (input_location, MODIFY_EXPR,
7504 LOOKUP_NORMAL, lhs, rhs,
7505 make_node (NOP_EXPR), /*overload=*/NULL,
7506 complain);
7507 if (result == NULL_TREE)
7508 return error_mark_node;
7509 return result;
7511 lhstype = olhstype;
7513 else
7515 tree init = NULL_TREE;
7517 /* A binary op has been requested. Combine the old LHS
7518 value with the RHS producing the value we should actually
7519 store into the LHS. */
7520 gcc_assert (!((TREE_CODE (lhstype) == REFERENCE_TYPE
7521 && MAYBE_CLASS_TYPE_P (TREE_TYPE (lhstype)))
7522 || MAYBE_CLASS_TYPE_P (lhstype)));
7524 /* Preevaluate the RHS to make sure its evaluation is complete
7525 before the lvalue-to-rvalue conversion of the LHS:
7527 [expr.ass] With respect to an indeterminately-sequenced
7528 function call, the operation of a compound assignment is a
7529 single evaluation. [ Note: Therefore, a function call shall
7530 not intervene between the lvalue-to-rvalue conversion and the
7531 side effect associated with any single compound assignment
7532 operator. -- end note ] */
7533 lhs = stabilize_reference (lhs);
7534 rhs = rvalue (rhs);
7535 rhs = stabilize_expr (rhs, &init);
7536 newrhs = cp_build_binary_op (input_location,
7537 modifycode, lhs, rhs,
7538 complain);
7539 if (newrhs == error_mark_node)
7541 if (complain & tf_error)
7542 error (" in evaluation of %<%Q(%#T, %#T)%>", modifycode,
7543 TREE_TYPE (lhs), TREE_TYPE (rhs));
7544 return error_mark_node;
7547 if (init)
7548 newrhs = build2 (COMPOUND_EXPR, TREE_TYPE (newrhs), init, newrhs);
7550 /* Now it looks like a plain assignment. */
7551 modifycode = NOP_EXPR;
7552 if (c_dialect_objc ())
7554 result = objc_maybe_build_modify_expr (lhs, newrhs);
7555 if (result)
7556 return result;
7559 gcc_assert (TREE_CODE (lhstype) != REFERENCE_TYPE);
7560 gcc_assert (TREE_CODE (TREE_TYPE (newrhs)) != REFERENCE_TYPE);
7563 /* The left-hand side must be an lvalue. */
7564 if (!lvalue_or_else (lhs, lv_assign, complain))
7565 return error_mark_node;
7567 /* Warn about modifying something that is `const'. Don't warn if
7568 this is initialization. */
7569 if (modifycode != INIT_EXPR
7570 && (TREE_READONLY (lhs) || CP_TYPE_CONST_P (lhstype)
7571 /* Functions are not modifiable, even though they are
7572 lvalues. */
7573 || TREE_CODE (TREE_TYPE (lhs)) == FUNCTION_TYPE
7574 || TREE_CODE (TREE_TYPE (lhs)) == METHOD_TYPE
7575 /* If it's an aggregate and any field is const, then it is
7576 effectively const. */
7577 || (CLASS_TYPE_P (lhstype)
7578 && C_TYPE_FIELDS_READONLY (lhstype))))
7580 if (complain & tf_error)
7581 cxx_readonly_error (lhs, lv_assign);
7582 else
7583 return error_mark_node;
7586 /* If storing into a structure or union member, it may have been given a
7587 lowered bitfield type. We need to convert to the declared type first,
7588 so retrieve it now. */
7590 olhstype = unlowered_expr_type (lhs);
7592 /* Convert new value to destination type. */
7594 if (TREE_CODE (lhstype) == ARRAY_TYPE)
7596 int from_array;
7598 if (BRACE_ENCLOSED_INITIALIZER_P (newrhs))
7600 if (modifycode != INIT_EXPR)
7602 if (complain & tf_error)
7603 error ("assigning to an array from an initializer list");
7604 return error_mark_node;
7606 if (check_array_initializer (lhs, lhstype, newrhs))
7607 return error_mark_node;
7608 newrhs = digest_init (lhstype, newrhs, complain);
7609 if (newrhs == error_mark_node)
7610 return error_mark_node;
7613 /* C++11 8.5/17: "If the destination type is an array of characters,
7614 an array of char16_t, an array of char32_t, or an array of wchar_t,
7615 and the initializer is a string literal...". */
7616 else if (TREE_CODE (newrhs) == STRING_CST
7617 && char_type_p (TREE_TYPE (TYPE_MAIN_VARIANT (lhstype)))
7618 && modifycode == INIT_EXPR)
7620 newrhs = digest_init (lhstype, newrhs, complain);
7621 if (newrhs == error_mark_node)
7622 return error_mark_node;
7625 else if (!same_or_base_type_p (TYPE_MAIN_VARIANT (lhstype),
7626 TYPE_MAIN_VARIANT (TREE_TYPE (newrhs))))
7628 if (complain & tf_error)
7629 error ("incompatible types in assignment of %qT to %qT",
7630 TREE_TYPE (rhs), lhstype);
7631 return error_mark_node;
7634 /* Allow array assignment in compiler-generated code. */
7635 else if (!current_function_decl
7636 || !DECL_DEFAULTED_FN (current_function_decl))
7638 /* This routine is used for both initialization and assignment.
7639 Make sure the diagnostic message differentiates the context. */
7640 if (complain & tf_error)
7642 if (modifycode == INIT_EXPR)
7643 error ("array used as initializer");
7644 else
7645 error ("invalid array assignment");
7647 return error_mark_node;
7650 from_array = TREE_CODE (TREE_TYPE (newrhs)) == ARRAY_TYPE
7651 ? 1 + (modifycode != INIT_EXPR): 0;
7652 return build_vec_init (lhs, NULL_TREE, newrhs,
7653 /*explicit_value_init_p=*/false,
7654 from_array, complain);
7657 if (modifycode == INIT_EXPR)
7658 /* Calls with INIT_EXPR are all direct-initialization, so don't set
7659 LOOKUP_ONLYCONVERTING. */
7660 newrhs = convert_for_initialization (lhs, olhstype, newrhs, LOOKUP_NORMAL,
7661 ICR_INIT, NULL_TREE, 0,
7662 complain);
7663 else
7664 newrhs = convert_for_assignment (olhstype, newrhs, ICR_ASSIGN,
7665 NULL_TREE, 0, complain, LOOKUP_IMPLICIT);
7667 if (!same_type_p (lhstype, olhstype))
7668 newrhs = cp_convert_and_check (lhstype, newrhs, complain);
7670 if (modifycode != INIT_EXPR)
7672 if (TREE_CODE (newrhs) == CALL_EXPR
7673 && TYPE_NEEDS_CONSTRUCTING (lhstype))
7674 newrhs = build_cplus_new (lhstype, newrhs, complain);
7676 /* Can't initialize directly from a TARGET_EXPR, since that would
7677 cause the lhs to be constructed twice, and possibly result in
7678 accidental self-initialization. So we force the TARGET_EXPR to be
7679 expanded without a target. */
7680 if (TREE_CODE (newrhs) == TARGET_EXPR)
7681 newrhs = build2 (COMPOUND_EXPR, TREE_TYPE (newrhs), newrhs,
7682 TREE_OPERAND (newrhs, 0));
7685 if (newrhs == error_mark_node)
7686 return error_mark_node;
7688 if (c_dialect_objc () && flag_objc_gc)
7690 result = objc_generate_write_barrier (lhs, modifycode, newrhs);
7692 if (result)
7693 return result;
7696 result = build2 (modifycode == NOP_EXPR ? MODIFY_EXPR : INIT_EXPR,
7697 lhstype, lhs, newrhs);
7699 TREE_SIDE_EFFECTS (result) = 1;
7700 if (!plain_assign)
7701 TREE_NO_WARNING (result) = 1;
7703 return result;
7706 tree
7707 build_x_modify_expr (location_t loc, tree lhs, enum tree_code modifycode,
7708 tree rhs, tsubst_flags_t complain)
7710 if (processing_template_decl)
7711 return build_min_nt_loc (loc, MODOP_EXPR, lhs,
7712 build_min_nt_loc (loc, modifycode, NULL_TREE,
7713 NULL_TREE), rhs);
7715 if (modifycode != NOP_EXPR)
7717 tree rval = build_new_op (loc, MODIFY_EXPR, LOOKUP_NORMAL, lhs, rhs,
7718 make_node (modifycode), /*overload=*/NULL,
7719 complain);
7720 if (rval)
7722 TREE_NO_WARNING (rval) = 1;
7723 return rval;
7726 return cp_build_modify_expr (lhs, modifycode, rhs, complain);
7729 /* Helper function for get_delta_difference which assumes FROM is a base
7730 class of TO. Returns a delta for the conversion of pointer-to-member
7731 of FROM to pointer-to-member of TO. If the conversion is invalid and
7732 tf_error is not set in COMPLAIN returns error_mark_node, otherwise
7733 returns zero. If FROM is not a base class of TO, returns NULL_TREE.
7734 If C_CAST_P is true, this conversion is taking place as part of a
7735 C-style cast. */
7737 static tree
7738 get_delta_difference_1 (tree from, tree to, bool c_cast_p,
7739 tsubst_flags_t complain)
7741 tree binfo;
7742 base_kind kind;
7744 binfo = lookup_base (to, from, c_cast_p ? ba_unique : ba_check,
7745 &kind, complain);
7747 if (binfo == error_mark_node)
7749 if (!(complain & tf_error))
7750 return error_mark_node;
7752 error (" in pointer to member function conversion");
7753 return size_zero_node;
7755 else if (binfo)
7757 if (kind != bk_via_virtual)
7758 return BINFO_OFFSET (binfo);
7759 else
7760 /* FROM is a virtual base class of TO. Issue an error or warning
7761 depending on whether or not this is a reinterpret cast. */
7763 if (!(complain & tf_error))
7764 return error_mark_node;
7766 error ("pointer to member conversion via virtual base %qT",
7767 BINFO_TYPE (binfo_from_vbase (binfo)));
7769 return size_zero_node;
7772 else
7773 return NULL_TREE;
7776 /* Get difference in deltas for different pointer to member function
7777 types. If the conversion is invalid and tf_error is not set in
7778 COMPLAIN, returns error_mark_node, otherwise returns an integer
7779 constant of type PTRDIFF_TYPE_NODE and its value is zero if the
7780 conversion is invalid. If ALLOW_INVERSE_P is true, then allow reverse
7781 conversions as well. If C_CAST_P is true this conversion is taking
7782 place as part of a C-style cast.
7784 Note that the naming of FROM and TO is kind of backwards; the return
7785 value is what we add to a TO in order to get a FROM. They are named
7786 this way because we call this function to find out how to convert from
7787 a pointer to member of FROM to a pointer to member of TO. */
7789 static tree
7790 get_delta_difference (tree from, tree to,
7791 bool allow_inverse_p,
7792 bool c_cast_p, tsubst_flags_t complain)
7794 tree result;
7796 if (same_type_ignoring_top_level_qualifiers_p (from, to))
7797 /* Pointer to member of incomplete class is permitted*/
7798 result = size_zero_node;
7799 else
7800 result = get_delta_difference_1 (from, to, c_cast_p, complain);
7802 if (result == error_mark_node)
7803 return error_mark_node;
7805 if (!result)
7807 if (!allow_inverse_p)
7809 if (!(complain & tf_error))
7810 return error_mark_node;
7812 error_not_base_type (from, to);
7813 error (" in pointer to member conversion");
7814 result = size_zero_node;
7816 else
7818 result = get_delta_difference_1 (to, from, c_cast_p, complain);
7820 if (result == error_mark_node)
7821 return error_mark_node;
7823 if (result)
7824 result = size_diffop_loc (input_location,
7825 size_zero_node, result);
7826 else
7828 if (!(complain & tf_error))
7829 return error_mark_node;
7831 error_not_base_type (from, to);
7832 error (" in pointer to member conversion");
7833 result = size_zero_node;
7838 return fold_if_not_in_template (convert_to_integer (ptrdiff_type_node,
7839 result));
7842 /* Return a constructor for the pointer-to-member-function TYPE using
7843 the other components as specified. */
7845 tree
7846 build_ptrmemfunc1 (tree type, tree delta, tree pfn)
7848 tree u = NULL_TREE;
7849 tree delta_field;
7850 tree pfn_field;
7851 vec<constructor_elt, va_gc> *v;
7853 /* Pull the FIELD_DECLs out of the type. */
7854 pfn_field = TYPE_FIELDS (type);
7855 delta_field = DECL_CHAIN (pfn_field);
7857 /* Make sure DELTA has the type we want. */
7858 delta = convert_and_check (input_location, delta_type_node, delta);
7860 /* Convert to the correct target type if necessary. */
7861 pfn = fold_convert (TREE_TYPE (pfn_field), pfn);
7863 /* Finish creating the initializer. */
7864 vec_alloc (v, 2);
7865 CONSTRUCTOR_APPEND_ELT(v, pfn_field, pfn);
7866 CONSTRUCTOR_APPEND_ELT(v, delta_field, delta);
7867 u = build_constructor (type, v);
7868 TREE_CONSTANT (u) = TREE_CONSTANT (pfn) & TREE_CONSTANT (delta);
7869 TREE_STATIC (u) = (TREE_CONSTANT (u)
7870 && (initializer_constant_valid_p (pfn, TREE_TYPE (pfn))
7871 != NULL_TREE)
7872 && (initializer_constant_valid_p (delta, TREE_TYPE (delta))
7873 != NULL_TREE));
7874 return u;
7877 /* Build a constructor for a pointer to member function. It can be
7878 used to initialize global variables, local variable, or used
7879 as a value in expressions. TYPE is the POINTER to METHOD_TYPE we
7880 want to be.
7882 If FORCE is nonzero, then force this conversion, even if
7883 we would rather not do it. Usually set when using an explicit
7884 cast. A C-style cast is being processed iff C_CAST_P is true.
7886 Return error_mark_node, if something goes wrong. */
7888 tree
7889 build_ptrmemfunc (tree type, tree pfn, int force, bool c_cast_p,
7890 tsubst_flags_t complain)
7892 tree fn;
7893 tree pfn_type;
7894 tree to_type;
7896 if (error_operand_p (pfn))
7897 return error_mark_node;
7899 pfn_type = TREE_TYPE (pfn);
7900 to_type = build_ptrmemfunc_type (type);
7902 /* Handle multiple conversions of pointer to member functions. */
7903 if (TYPE_PTRMEMFUNC_P (pfn_type))
7905 tree delta = NULL_TREE;
7906 tree npfn = NULL_TREE;
7907 tree n;
7909 if (!force
7910 && !can_convert_arg (to_type, TREE_TYPE (pfn), pfn,
7911 LOOKUP_NORMAL, complain))
7913 if (complain & tf_error)
7914 error ("invalid conversion to type %qT from type %qT",
7915 to_type, pfn_type);
7916 else
7917 return error_mark_node;
7920 n = get_delta_difference (TYPE_PTRMEMFUNC_OBJECT_TYPE (pfn_type),
7921 TYPE_PTRMEMFUNC_OBJECT_TYPE (to_type),
7922 force,
7923 c_cast_p, complain);
7924 if (n == error_mark_node)
7925 return error_mark_node;
7927 /* We don't have to do any conversion to convert a
7928 pointer-to-member to its own type. But, we don't want to
7929 just return a PTRMEM_CST if there's an explicit cast; that
7930 cast should make the expression an invalid template argument. */
7931 if (TREE_CODE (pfn) != PTRMEM_CST)
7933 if (same_type_p (to_type, pfn_type))
7934 return pfn;
7935 else if (integer_zerop (n))
7936 return build_reinterpret_cast (to_type, pfn,
7937 complain);
7940 if (TREE_SIDE_EFFECTS (pfn))
7941 pfn = save_expr (pfn);
7943 /* Obtain the function pointer and the current DELTA. */
7944 if (TREE_CODE (pfn) == PTRMEM_CST)
7945 expand_ptrmemfunc_cst (pfn, &delta, &npfn);
7946 else
7948 npfn = build_ptrmemfunc_access_expr (pfn, pfn_identifier);
7949 delta = build_ptrmemfunc_access_expr (pfn, delta_identifier);
7952 /* Just adjust the DELTA field. */
7953 gcc_assert (same_type_ignoring_top_level_qualifiers_p
7954 (TREE_TYPE (delta), ptrdiff_type_node));
7955 if (TARGET_PTRMEMFUNC_VBIT_LOCATION == ptrmemfunc_vbit_in_delta)
7956 n = cp_build_binary_op (input_location,
7957 LSHIFT_EXPR, n, integer_one_node,
7958 complain);
7959 delta = cp_build_binary_op (input_location,
7960 PLUS_EXPR, delta, n, complain);
7961 return build_ptrmemfunc1 (to_type, delta, npfn);
7964 /* Handle null pointer to member function conversions. */
7965 if (null_ptr_cst_p (pfn))
7967 pfn = cp_build_c_cast (type, pfn, complain);
7968 return build_ptrmemfunc1 (to_type,
7969 integer_zero_node,
7970 pfn);
7973 if (type_unknown_p (pfn))
7974 return instantiate_type (type, pfn, complain);
7976 fn = TREE_OPERAND (pfn, 0);
7977 gcc_assert (TREE_CODE (fn) == FUNCTION_DECL
7978 /* In a template, we will have preserved the
7979 OFFSET_REF. */
7980 || (processing_template_decl && TREE_CODE (fn) == OFFSET_REF));
7981 return make_ptrmem_cst (to_type, fn);
7984 /* Return the DELTA, IDX, PFN, and DELTA2 values for the PTRMEM_CST
7985 given by CST.
7987 ??? There is no consistency as to the types returned for the above
7988 values. Some code acts as if it were a sizetype and some as if it were
7989 integer_type_node. */
7991 void
7992 expand_ptrmemfunc_cst (tree cst, tree *delta, tree *pfn)
7994 tree type = TREE_TYPE (cst);
7995 tree fn = PTRMEM_CST_MEMBER (cst);
7996 tree ptr_class, fn_class;
7998 gcc_assert (TREE_CODE (fn) == FUNCTION_DECL);
8000 /* The class that the function belongs to. */
8001 fn_class = DECL_CONTEXT (fn);
8003 /* The class that we're creating a pointer to member of. */
8004 ptr_class = TYPE_PTRMEMFUNC_OBJECT_TYPE (type);
8006 /* First, calculate the adjustment to the function's class. */
8007 *delta = get_delta_difference (fn_class, ptr_class, /*force=*/0,
8008 /*c_cast_p=*/0, tf_warning_or_error);
8010 if (!DECL_VIRTUAL_P (fn))
8011 *pfn = convert (TYPE_PTRMEMFUNC_FN_TYPE (type),
8012 build_addr_func (fn, tf_warning_or_error));
8013 else
8015 /* If we're dealing with a virtual function, we have to adjust 'this'
8016 again, to point to the base which provides the vtable entry for
8017 fn; the call will do the opposite adjustment. */
8018 tree orig_class = DECL_CONTEXT (fn);
8019 tree binfo = binfo_or_else (orig_class, fn_class);
8020 *delta = build2 (PLUS_EXPR, TREE_TYPE (*delta),
8021 *delta, BINFO_OFFSET (binfo));
8022 *delta = fold_if_not_in_template (*delta);
8024 /* We set PFN to the vtable offset at which the function can be
8025 found, plus one (unless ptrmemfunc_vbit_in_delta, in which
8026 case delta is shifted left, and then incremented). */
8027 *pfn = DECL_VINDEX (fn);
8028 *pfn = build2 (MULT_EXPR, integer_type_node, *pfn,
8029 TYPE_SIZE_UNIT (vtable_entry_type));
8030 *pfn = fold_if_not_in_template (*pfn);
8032 switch (TARGET_PTRMEMFUNC_VBIT_LOCATION)
8034 case ptrmemfunc_vbit_in_pfn:
8035 *pfn = build2 (PLUS_EXPR, integer_type_node, *pfn,
8036 integer_one_node);
8037 *pfn = fold_if_not_in_template (*pfn);
8038 break;
8040 case ptrmemfunc_vbit_in_delta:
8041 *delta = build2 (LSHIFT_EXPR, TREE_TYPE (*delta),
8042 *delta, integer_one_node);
8043 *delta = fold_if_not_in_template (*delta);
8044 *delta = build2 (PLUS_EXPR, TREE_TYPE (*delta),
8045 *delta, integer_one_node);
8046 *delta = fold_if_not_in_template (*delta);
8047 break;
8049 default:
8050 gcc_unreachable ();
8053 *pfn = build_nop (TYPE_PTRMEMFUNC_FN_TYPE (type), *pfn);
8054 *pfn = fold_if_not_in_template (*pfn);
8058 /* Return an expression for PFN from the pointer-to-member function
8059 given by T. */
8061 static tree
8062 pfn_from_ptrmemfunc (tree t)
8064 if (TREE_CODE (t) == PTRMEM_CST)
8066 tree delta;
8067 tree pfn;
8069 expand_ptrmemfunc_cst (t, &delta, &pfn);
8070 if (pfn)
8071 return pfn;
8074 return build_ptrmemfunc_access_expr (t, pfn_identifier);
8077 /* Return an expression for DELTA from the pointer-to-member function
8078 given by T. */
8080 static tree
8081 delta_from_ptrmemfunc (tree t)
8083 if (TREE_CODE (t) == PTRMEM_CST)
8085 tree delta;
8086 tree pfn;
8088 expand_ptrmemfunc_cst (t, &delta, &pfn);
8089 if (delta)
8090 return delta;
8093 return build_ptrmemfunc_access_expr (t, delta_identifier);
8096 /* Convert value RHS to type TYPE as preparation for an assignment to
8097 an lvalue of type TYPE. ERRTYPE indicates what kind of error the
8098 implicit conversion is. If FNDECL is non-NULL, we are doing the
8099 conversion in order to pass the PARMNUMth argument of FNDECL.
8100 If FNDECL is NULL, we are doing the conversion in function pointer
8101 argument passing, conversion in initialization, etc. */
8103 static tree
8104 convert_for_assignment (tree type, tree rhs,
8105 impl_conv_rhs errtype, tree fndecl, int parmnum,
8106 tsubst_flags_t complain, int flags)
8108 tree rhstype;
8109 enum tree_code coder;
8111 /* Strip NON_LVALUE_EXPRs since we aren't using as an lvalue. */
8112 if (TREE_CODE (rhs) == NON_LVALUE_EXPR)
8113 rhs = TREE_OPERAND (rhs, 0);
8115 rhstype = TREE_TYPE (rhs);
8116 coder = TREE_CODE (rhstype);
8118 if (TREE_CODE (type) == VECTOR_TYPE && coder == VECTOR_TYPE
8119 && vector_types_convertible_p (type, rhstype, true))
8121 rhs = mark_rvalue_use (rhs);
8122 return convert (type, rhs);
8125 if (rhs == error_mark_node || rhstype == error_mark_node)
8126 return error_mark_node;
8127 if (TREE_CODE (rhs) == TREE_LIST && TREE_VALUE (rhs) == error_mark_node)
8128 return error_mark_node;
8130 /* The RHS of an assignment cannot have void type. */
8131 if (coder == VOID_TYPE)
8133 if (complain & tf_error)
8134 error ("void value not ignored as it ought to be");
8135 return error_mark_node;
8138 if (c_dialect_objc ())
8140 int parmno;
8141 tree selector;
8142 tree rname = fndecl;
8144 switch (errtype)
8146 case ICR_ASSIGN:
8147 parmno = -1;
8148 break;
8149 case ICR_INIT:
8150 parmno = -2;
8151 break;
8152 default:
8153 selector = objc_message_selector ();
8154 parmno = parmnum;
8155 if (selector && parmno > 1)
8157 rname = selector;
8158 parmno -= 1;
8162 if (objc_compare_types (type, rhstype, parmno, rname))
8164 rhs = mark_rvalue_use (rhs);
8165 return convert (type, rhs);
8169 /* [expr.ass]
8171 The expression is implicitly converted (clause _conv_) to the
8172 cv-unqualified type of the left operand.
8174 We allow bad conversions here because by the time we get to this point
8175 we are committed to doing the conversion. If we end up doing a bad
8176 conversion, convert_like will complain. */
8177 if (!can_convert_arg_bad (type, rhstype, rhs, flags, complain))
8179 /* When -Wno-pmf-conversions is use, we just silently allow
8180 conversions from pointers-to-members to plain pointers. If
8181 the conversion doesn't work, cp_convert will complain. */
8182 if (!warn_pmf2ptr
8183 && TYPE_PTR_P (type)
8184 && TYPE_PTRMEMFUNC_P (rhstype))
8185 rhs = cp_convert (strip_top_quals (type), rhs, complain);
8186 else
8188 if (complain & tf_error)
8190 /* If the right-hand side has unknown type, then it is an
8191 overloaded function. Call instantiate_type to get error
8192 messages. */
8193 if (rhstype == unknown_type_node)
8194 instantiate_type (type, rhs, tf_warning_or_error);
8195 else if (fndecl)
8196 error ("cannot convert %qT to %qT for argument %qP to %qD",
8197 rhstype, type, parmnum, fndecl);
8198 else
8199 switch (errtype)
8201 case ICR_DEFAULT_ARGUMENT:
8202 error ("cannot convert %qT to %qT in default argument",
8203 rhstype, type);
8204 break;
8205 case ICR_ARGPASS:
8206 error ("cannot convert %qT to %qT in argument passing",
8207 rhstype, type);
8208 break;
8209 case ICR_CONVERTING:
8210 error ("cannot convert %qT to %qT",
8211 rhstype, type);
8212 break;
8213 case ICR_INIT:
8214 error ("cannot convert %qT to %qT in initialization",
8215 rhstype, type);
8216 break;
8217 case ICR_RETURN:
8218 error ("cannot convert %qT to %qT in return",
8219 rhstype, type);
8220 break;
8221 case ICR_ASSIGN:
8222 error ("cannot convert %qT to %qT in assignment",
8223 rhstype, type);
8224 break;
8225 default:
8226 gcc_unreachable();
8228 if (TYPE_PTR_P (rhstype)
8229 && TYPE_PTR_P (type)
8230 && CLASS_TYPE_P (TREE_TYPE (rhstype))
8231 && CLASS_TYPE_P (TREE_TYPE (type))
8232 && !COMPLETE_TYPE_P (TREE_TYPE (rhstype)))
8233 inform (DECL_SOURCE_LOCATION (TYPE_MAIN_DECL
8234 (TREE_TYPE (rhstype))),
8235 "class type %qT is incomplete", TREE_TYPE (rhstype));
8237 return error_mark_node;
8240 if (warn_suggest_attribute_format)
8242 const enum tree_code codel = TREE_CODE (type);
8243 if ((codel == POINTER_TYPE || codel == REFERENCE_TYPE)
8244 && coder == codel
8245 && check_missing_format_attribute (type, rhstype)
8246 && (complain & tf_warning))
8247 switch (errtype)
8249 case ICR_ARGPASS:
8250 case ICR_DEFAULT_ARGUMENT:
8251 if (fndecl)
8252 warning (OPT_Wsuggest_attribute_format,
8253 "parameter %qP of %qD might be a candidate "
8254 "for a format attribute", parmnum, fndecl);
8255 else
8256 warning (OPT_Wsuggest_attribute_format,
8257 "parameter might be a candidate "
8258 "for a format attribute");
8259 break;
8260 case ICR_CONVERTING:
8261 warning (OPT_Wsuggest_attribute_format,
8262 "target of conversion might be a candidate "
8263 "for a format attribute");
8264 break;
8265 case ICR_INIT:
8266 warning (OPT_Wsuggest_attribute_format,
8267 "target of initialization might be a candidate "
8268 "for a format attribute");
8269 break;
8270 case ICR_RETURN:
8271 warning (OPT_Wsuggest_attribute_format,
8272 "return type might be a candidate "
8273 "for a format attribute");
8274 break;
8275 case ICR_ASSIGN:
8276 warning (OPT_Wsuggest_attribute_format,
8277 "left-hand side of assignment might be a candidate "
8278 "for a format attribute");
8279 break;
8280 default:
8281 gcc_unreachable();
8285 /* If -Wparentheses, warn about a = b = c when a has type bool and b
8286 does not. */
8287 if (warn_parentheses
8288 && TREE_CODE (type) == BOOLEAN_TYPE
8289 && TREE_CODE (rhs) == MODIFY_EXPR
8290 && !TREE_NO_WARNING (rhs)
8291 && TREE_CODE (TREE_TYPE (rhs)) != BOOLEAN_TYPE
8292 && (complain & tf_warning))
8294 location_t loc = EXPR_LOC_OR_LOC (rhs, input_location);
8296 warning_at (loc, OPT_Wparentheses,
8297 "suggest parentheses around assignment used as truth value");
8298 TREE_NO_WARNING (rhs) = 1;
8301 return perform_implicit_conversion_flags (strip_top_quals (type), rhs,
8302 complain, flags);
8305 /* Convert RHS to be of type TYPE.
8306 If EXP is nonzero, it is the target of the initialization.
8307 ERRTYPE indicates what kind of error the implicit conversion is.
8309 Two major differences between the behavior of
8310 `convert_for_assignment' and `convert_for_initialization'
8311 are that references are bashed in the former, while
8312 copied in the latter, and aggregates are assigned in
8313 the former (operator=) while initialized in the
8314 latter (X(X&)).
8316 If using constructor make sure no conversion operator exists, if one does
8317 exist, an ambiguity exists. */
8319 tree
8320 convert_for_initialization (tree exp, tree type, tree rhs, int flags,
8321 impl_conv_rhs errtype, tree fndecl, int parmnum,
8322 tsubst_flags_t complain)
8324 enum tree_code codel = TREE_CODE (type);
8325 tree rhstype;
8326 enum tree_code coder;
8328 /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
8329 Strip such NOP_EXPRs, since RHS is used in non-lvalue context. */
8330 if (TREE_CODE (rhs) == NOP_EXPR
8331 && TREE_TYPE (rhs) == TREE_TYPE (TREE_OPERAND (rhs, 0))
8332 && codel != REFERENCE_TYPE)
8333 rhs = TREE_OPERAND (rhs, 0);
8335 if (type == error_mark_node
8336 || rhs == error_mark_node
8337 || (TREE_CODE (rhs) == TREE_LIST && TREE_VALUE (rhs) == error_mark_node))
8338 return error_mark_node;
8340 if ((TREE_CODE (TREE_TYPE (rhs)) == ARRAY_TYPE
8341 && TREE_CODE (type) != ARRAY_TYPE
8342 && (TREE_CODE (type) != REFERENCE_TYPE
8343 || TREE_CODE (TREE_TYPE (type)) != ARRAY_TYPE))
8344 || (TREE_CODE (TREE_TYPE (rhs)) == FUNCTION_TYPE
8345 && !TYPE_REFFN_P (type))
8346 || TREE_CODE (TREE_TYPE (rhs)) == METHOD_TYPE)
8347 rhs = decay_conversion (rhs, complain);
8349 rhstype = TREE_TYPE (rhs);
8350 coder = TREE_CODE (rhstype);
8352 if (coder == ERROR_MARK)
8353 return error_mark_node;
8355 /* We accept references to incomplete types, so we can
8356 return here before checking if RHS is of complete type. */
8358 if (codel == REFERENCE_TYPE)
8360 /* This should eventually happen in convert_arguments. */
8361 int savew = 0, savee = 0;
8363 if (fndecl)
8364 savew = warningcount + werrorcount, savee = errorcount;
8365 rhs = initialize_reference (type, rhs, flags, complain);
8367 if (fndecl
8368 && (warningcount + werrorcount > savew || errorcount > savee))
8369 inform (input_location,
8370 "in passing argument %P of %q+D", parmnum, fndecl);
8372 return rhs;
8375 if (exp != 0)
8376 exp = require_complete_type_sfinae (exp, complain);
8377 if (exp == error_mark_node)
8378 return error_mark_node;
8380 rhstype = non_reference (rhstype);
8382 type = complete_type (type);
8384 if (DIRECT_INIT_EXPR_P (type, rhs))
8385 /* Don't try to do copy-initialization if we already have
8386 direct-initialization. */
8387 return rhs;
8389 if (MAYBE_CLASS_TYPE_P (type))
8390 return perform_implicit_conversion_flags (type, rhs, complain, flags);
8392 return convert_for_assignment (type, rhs, errtype, fndecl, parmnum,
8393 complain, flags);
8396 /* If RETVAL is the address of, or a reference to, a local variable or
8397 temporary give an appropriate warning and return true. */
8399 static bool
8400 maybe_warn_about_returning_address_of_local (tree retval)
8402 tree valtype = TREE_TYPE (DECL_RESULT (current_function_decl));
8403 tree whats_returned = retval;
8405 for (;;)
8407 if (TREE_CODE (whats_returned) == COMPOUND_EXPR)
8408 whats_returned = TREE_OPERAND (whats_returned, 1);
8409 else if (CONVERT_EXPR_P (whats_returned)
8410 || TREE_CODE (whats_returned) == NON_LVALUE_EXPR)
8411 whats_returned = TREE_OPERAND (whats_returned, 0);
8412 else
8413 break;
8416 if (TREE_CODE (whats_returned) != ADDR_EXPR)
8417 return false;
8418 whats_returned = TREE_OPERAND (whats_returned, 0);
8420 while (TREE_CODE (whats_returned) == COMPONENT_REF
8421 || TREE_CODE (whats_returned) == ARRAY_REF)
8422 whats_returned = TREE_OPERAND (whats_returned, 0);
8424 if (TREE_CODE (valtype) == REFERENCE_TYPE)
8426 if (TREE_CODE (whats_returned) == AGGR_INIT_EXPR
8427 || TREE_CODE (whats_returned) == TARGET_EXPR)
8429 warning (OPT_Wreturn_local_addr, "returning reference to temporary");
8430 return true;
8432 if (VAR_P (whats_returned)
8433 && DECL_NAME (whats_returned)
8434 && TEMP_NAME_P (DECL_NAME (whats_returned)))
8436 warning (OPT_Wreturn_local_addr, "reference to non-lvalue returned");
8437 return true;
8441 if (DECL_P (whats_returned)
8442 && DECL_NAME (whats_returned)
8443 && DECL_FUNCTION_SCOPE_P (whats_returned)
8444 && !is_capture_proxy (whats_returned)
8445 && !(TREE_STATIC (whats_returned)
8446 || TREE_PUBLIC (whats_returned)))
8448 if (TREE_CODE (valtype) == REFERENCE_TYPE)
8449 warning (OPT_Wreturn_local_addr, "reference to local variable %q+D returned",
8450 whats_returned);
8451 else if (TREE_CODE (whats_returned) == LABEL_DECL)
8452 warning (OPT_Wreturn_local_addr, "address of label %q+D returned",
8453 whats_returned);
8454 else
8455 warning (OPT_Wreturn_local_addr, "address of local variable %q+D "
8456 "returned", whats_returned);
8457 return true;
8460 return false;
8463 /* Check that returning RETVAL from the current function is valid.
8464 Return an expression explicitly showing all conversions required to
8465 change RETVAL into the function return type, and to assign it to
8466 the DECL_RESULT for the function. Set *NO_WARNING to true if
8467 code reaches end of non-void function warning shouldn't be issued
8468 on this RETURN_EXPR. */
8470 tree
8471 check_return_expr (tree retval, bool *no_warning)
8473 tree result;
8474 /* The type actually returned by the function. */
8475 tree valtype;
8476 /* The type the function is declared to return, or void if
8477 the declared type is incomplete. */
8478 tree functype;
8479 int fn_returns_value_p;
8480 bool named_return_value_okay_p;
8482 *no_warning = false;
8484 if (flag_cilkplus && retval && contains_cilk_spawn_stmt (retval))
8486 error_at (EXPR_LOCATION (retval), "use of %<_Cilk_spawn%> in a return "
8487 "statement is not allowed");
8488 return NULL_TREE;
8491 /* A `volatile' function is one that isn't supposed to return, ever.
8492 (This is a G++ extension, used to get better code for functions
8493 that call the `volatile' function.) */
8494 if (TREE_THIS_VOLATILE (current_function_decl))
8495 warning (0, "function declared %<noreturn%> has a %<return%> statement");
8497 /* Check for various simple errors. */
8498 if (DECL_DESTRUCTOR_P (current_function_decl))
8500 if (retval)
8501 error ("returning a value from a destructor");
8502 return NULL_TREE;
8504 else if (DECL_CONSTRUCTOR_P (current_function_decl))
8506 if (in_function_try_handler)
8507 /* If a return statement appears in a handler of the
8508 function-try-block of a constructor, the program is ill-formed. */
8509 error ("cannot return from a handler of a function-try-block of a constructor");
8510 else if (retval)
8511 /* You can't return a value from a constructor. */
8512 error ("returning a value from a constructor");
8513 return NULL_TREE;
8516 if (processing_template_decl)
8518 current_function_returns_value = 1;
8519 if (check_for_bare_parameter_packs (retval))
8520 retval = error_mark_node;
8521 return retval;
8524 functype = TREE_TYPE (TREE_TYPE (current_function_decl));
8526 /* Deduce auto return type from a return statement. */
8527 if (current_function_auto_return_pattern)
8529 tree auto_node;
8530 tree type;
8532 if (!retval && !is_auto (current_function_auto_return_pattern))
8534 /* Give a helpful error message. */
8535 error ("return-statement with no value, in function returning %qT",
8536 current_function_auto_return_pattern);
8537 inform (input_location, "only plain %<auto%> return type can be "
8538 "deduced to %<void%>");
8539 type = error_mark_node;
8541 else if (retval && BRACE_ENCLOSED_INITIALIZER_P (retval))
8543 error ("returning initializer list");
8544 type = error_mark_node;
8546 else
8548 if (!retval)
8549 retval = void_node;
8550 auto_node = type_uses_auto (current_function_auto_return_pattern);
8551 type = do_auto_deduction (current_function_auto_return_pattern,
8552 retval, auto_node);
8555 if (type == error_mark_node)
8556 /* Leave it. */;
8557 else if (functype == current_function_auto_return_pattern)
8558 apply_deduced_return_type (current_function_decl, type);
8559 else
8560 /* A mismatch should have been diagnosed in do_auto_deduction. */
8561 gcc_assert (same_type_p (type, functype));
8562 functype = type;
8565 /* When no explicit return-value is given in a function with a named
8566 return value, the named return value is used. */
8567 result = DECL_RESULT (current_function_decl);
8568 valtype = TREE_TYPE (result);
8569 gcc_assert (valtype != NULL_TREE);
8570 fn_returns_value_p = !VOID_TYPE_P (valtype);
8571 if (!retval && DECL_NAME (result) && fn_returns_value_p)
8572 retval = result;
8574 /* Check for a return statement with no return value in a function
8575 that's supposed to return a value. */
8576 if (!retval && fn_returns_value_p)
8578 if (functype != error_mark_node)
8579 permerror (input_location, "return-statement with no value, in "
8580 "function returning %qT", valtype);
8581 /* Remember that this function did return. */
8582 current_function_returns_value = 1;
8583 /* And signal caller that TREE_NO_WARNING should be set on the
8584 RETURN_EXPR to avoid control reaches end of non-void function
8585 warnings in tree-cfg.c. */
8586 *no_warning = true;
8588 /* Check for a return statement with a value in a function that
8589 isn't supposed to return a value. */
8590 else if (retval && !fn_returns_value_p)
8592 if (VOID_TYPE_P (TREE_TYPE (retval)))
8593 /* You can return a `void' value from a function of `void'
8594 type. In that case, we have to evaluate the expression for
8595 its side-effects. */
8596 finish_expr_stmt (retval);
8597 else
8598 permerror (input_location, "return-statement with a value, in function "
8599 "returning 'void'");
8600 current_function_returns_null = 1;
8602 /* There's really no value to return, after all. */
8603 return NULL_TREE;
8605 else if (!retval)
8606 /* Remember that this function can sometimes return without a
8607 value. */
8608 current_function_returns_null = 1;
8609 else
8610 /* Remember that this function did return a value. */
8611 current_function_returns_value = 1;
8613 /* Check for erroneous operands -- but after giving ourselves a
8614 chance to provide an error about returning a value from a void
8615 function. */
8616 if (error_operand_p (retval))
8618 current_function_return_value = error_mark_node;
8619 return error_mark_node;
8622 /* Only operator new(...) throw(), can return NULL [expr.new/13]. */
8623 if ((DECL_OVERLOADED_OPERATOR_P (current_function_decl) == NEW_EXPR
8624 || DECL_OVERLOADED_OPERATOR_P (current_function_decl) == VEC_NEW_EXPR)
8625 && !TYPE_NOTHROW_P (TREE_TYPE (current_function_decl))
8626 && ! flag_check_new
8627 && retval && null_ptr_cst_p (retval))
8628 warning (0, "%<operator new%> must not return NULL unless it is "
8629 "declared %<throw()%> (or -fcheck-new is in effect)");
8631 /* Effective C++ rule 15. See also start_function. */
8632 if (warn_ecpp
8633 && DECL_NAME (current_function_decl) == ansi_assopname(NOP_EXPR))
8635 bool warn = true;
8637 /* The function return type must be a reference to the current
8638 class. */
8639 if (TREE_CODE (valtype) == REFERENCE_TYPE
8640 && same_type_ignoring_top_level_qualifiers_p
8641 (TREE_TYPE (valtype), TREE_TYPE (current_class_ref)))
8643 /* Returning '*this' is obviously OK. */
8644 if (retval == current_class_ref)
8645 warn = false;
8646 /* If we are calling a function whose return type is the same of
8647 the current class reference, it is ok. */
8648 else if (INDIRECT_REF_P (retval)
8649 && TREE_CODE (TREE_OPERAND (retval, 0)) == CALL_EXPR)
8650 warn = false;
8653 if (warn)
8654 warning (OPT_Weffc__, "%<operator=%> should return a reference to %<*this%>");
8657 /* The fabled Named Return Value optimization, as per [class.copy]/15:
8659 [...] For a function with a class return type, if the expression
8660 in the return statement is the name of a local object, and the cv-
8661 unqualified type of the local object is the same as the function
8662 return type, an implementation is permitted to omit creating the tem-
8663 porary object to hold the function return value [...]
8665 So, if this is a value-returning function that always returns the same
8666 local variable, remember it.
8668 It might be nice to be more flexible, and choose the first suitable
8669 variable even if the function sometimes returns something else, but
8670 then we run the risk of clobbering the variable we chose if the other
8671 returned expression uses the chosen variable somehow. And people expect
8672 this restriction, anyway. (jason 2000-11-19)
8674 See finish_function and finalize_nrv for the rest of this optimization. */
8676 named_return_value_okay_p =
8677 (retval != NULL_TREE
8678 /* Must be a local, automatic variable. */
8679 && VAR_P (retval)
8680 && DECL_CONTEXT (retval) == current_function_decl
8681 && ! TREE_STATIC (retval)
8682 /* And not a lambda or anonymous union proxy. */
8683 && !DECL_HAS_VALUE_EXPR_P (retval)
8684 && (DECL_ALIGN (retval) <= DECL_ALIGN (result))
8685 /* The cv-unqualified type of the returned value must be the
8686 same as the cv-unqualified return type of the
8687 function. */
8688 && same_type_p ((TYPE_MAIN_VARIANT (TREE_TYPE (retval))),
8689 (TYPE_MAIN_VARIANT (functype)))
8690 /* And the returned value must be non-volatile. */
8691 && ! TYPE_VOLATILE (TREE_TYPE (retval)));
8693 if (fn_returns_value_p && flag_elide_constructors)
8695 if (named_return_value_okay_p
8696 && (current_function_return_value == NULL_TREE
8697 || current_function_return_value == retval))
8698 current_function_return_value = retval;
8699 else
8700 current_function_return_value = error_mark_node;
8703 /* We don't need to do any conversions when there's nothing being
8704 returned. */
8705 if (!retval)
8706 return NULL_TREE;
8708 /* Do any required conversions. */
8709 if (retval == result || DECL_CONSTRUCTOR_P (current_function_decl))
8710 /* No conversions are required. */
8712 else
8714 int flags = LOOKUP_NORMAL | LOOKUP_ONLYCONVERTING;
8716 /* The functype's return type will have been set to void, if it
8717 was an incomplete type. Just treat this as 'return;' */
8718 if (VOID_TYPE_P (functype))
8719 return error_mark_node;
8721 /* If we had an id-expression obfuscated by force_paren_expr, we need
8722 to undo it so we can try to treat it as an rvalue below. */
8723 if (cxx_dialect >= cxx14
8724 && INDIRECT_REF_P (retval)
8725 && REF_PARENTHESIZED_P (retval))
8727 retval = TREE_OPERAND (retval, 0);
8728 while (TREE_CODE (retval) == NON_LVALUE_EXPR
8729 || TREE_CODE (retval) == NOP_EXPR)
8730 retval = TREE_OPERAND (retval, 0);
8731 gcc_assert (TREE_CODE (retval) == ADDR_EXPR);
8732 retval = TREE_OPERAND (retval, 0);
8735 /* Under C++11 [12.8/32 class.copy], a returned lvalue is sometimes
8736 treated as an rvalue for the purposes of overload resolution to
8737 favor move constructors over copy constructors.
8739 Note that these conditions are similar to, but not as strict as,
8740 the conditions for the named return value optimization. */
8741 if ((cxx_dialect != cxx98)
8742 && ((VAR_P (retval) && !DECL_HAS_VALUE_EXPR_P (retval))
8743 || TREE_CODE (retval) == PARM_DECL)
8744 && DECL_CONTEXT (retval) == current_function_decl
8745 && !TREE_STATIC (retval)
8746 /* This is only interesting for class type. */
8747 && CLASS_TYPE_P (functype))
8748 flags = flags | LOOKUP_PREFER_RVALUE;
8750 /* First convert the value to the function's return type, then
8751 to the type of return value's location to handle the
8752 case that functype is smaller than the valtype. */
8753 retval = convert_for_initialization
8754 (NULL_TREE, functype, retval, flags, ICR_RETURN, NULL_TREE, 0,
8755 tf_warning_or_error);
8756 retval = convert (valtype, retval);
8758 /* If the conversion failed, treat this just like `return;'. */
8759 if (retval == error_mark_node)
8760 return retval;
8761 /* We can't initialize a register from a AGGR_INIT_EXPR. */
8762 else if (! cfun->returns_struct
8763 && TREE_CODE (retval) == TARGET_EXPR
8764 && TREE_CODE (TREE_OPERAND (retval, 1)) == AGGR_INIT_EXPR)
8765 retval = build2 (COMPOUND_EXPR, TREE_TYPE (retval), retval,
8766 TREE_OPERAND (retval, 0));
8767 else if (maybe_warn_about_returning_address_of_local (retval))
8768 retval = build2 (COMPOUND_EXPR, TREE_TYPE (retval), retval,
8769 build_zero_cst (TREE_TYPE (retval)));
8772 /* Actually copy the value returned into the appropriate location. */
8773 if (retval && retval != result)
8774 retval = build2 (INIT_EXPR, TREE_TYPE (result), result, retval);
8776 return retval;
8780 /* Returns nonzero if the pointer-type FROM can be converted to the
8781 pointer-type TO via a qualification conversion. If CONSTP is -1,
8782 then we return nonzero if the pointers are similar, and the
8783 cv-qualification signature of FROM is a proper subset of that of TO.
8785 If CONSTP is positive, then all outer pointers have been
8786 const-qualified. */
8788 static int
8789 comp_ptr_ttypes_real (tree to, tree from, int constp)
8791 bool to_more_cv_qualified = false;
8792 bool is_opaque_pointer = false;
8794 for (; ; to = TREE_TYPE (to), from = TREE_TYPE (from))
8796 if (TREE_CODE (to) != TREE_CODE (from))
8797 return 0;
8799 if (TREE_CODE (from) == OFFSET_TYPE
8800 && !same_type_p (TYPE_OFFSET_BASETYPE (from),
8801 TYPE_OFFSET_BASETYPE (to)))
8802 return 0;
8804 /* Const and volatile mean something different for function types,
8805 so the usual checks are not appropriate. */
8806 if (TREE_CODE (to) != FUNCTION_TYPE && TREE_CODE (to) != METHOD_TYPE)
8808 if (!at_least_as_qualified_p (to, from))
8809 return 0;
8811 if (!at_least_as_qualified_p (from, to))
8813 if (constp == 0)
8814 return 0;
8815 to_more_cv_qualified = true;
8818 if (constp > 0)
8819 constp &= TYPE_READONLY (to);
8822 if (TREE_CODE (to) == VECTOR_TYPE)
8823 is_opaque_pointer = vector_targets_convertible_p (to, from);
8825 if (!TYPE_PTR_P (to) && !TYPE_PTRDATAMEM_P (to))
8826 return ((constp >= 0 || to_more_cv_qualified)
8827 && (is_opaque_pointer
8828 || same_type_ignoring_top_level_qualifiers_p (to, from)));
8832 /* When comparing, say, char ** to char const **, this function takes
8833 the 'char *' and 'char const *'. Do not pass non-pointer/reference
8834 types to this function. */
8837 comp_ptr_ttypes (tree to, tree from)
8839 return comp_ptr_ttypes_real (to, from, 1);
8842 /* Returns true iff FNTYPE is a non-class type that involves
8843 error_mark_node. We can get FUNCTION_TYPE with buried error_mark_node
8844 if a parameter type is ill-formed. */
8846 bool
8847 error_type_p (const_tree type)
8849 tree t;
8851 switch (TREE_CODE (type))
8853 case ERROR_MARK:
8854 return true;
8856 case POINTER_TYPE:
8857 case REFERENCE_TYPE:
8858 case OFFSET_TYPE:
8859 return error_type_p (TREE_TYPE (type));
8861 case FUNCTION_TYPE:
8862 case METHOD_TYPE:
8863 if (error_type_p (TREE_TYPE (type)))
8864 return true;
8865 for (t = TYPE_ARG_TYPES (type); t; t = TREE_CHAIN (t))
8866 if (error_type_p (TREE_VALUE (t)))
8867 return true;
8868 return false;
8870 case RECORD_TYPE:
8871 if (TYPE_PTRMEMFUNC_P (type))
8872 return error_type_p (TYPE_PTRMEMFUNC_FN_TYPE (type));
8873 return false;
8875 default:
8876 return false;
8880 /* Returns true if to and from are (possibly multi-level) pointers to the same
8881 type or inheritance-related types, regardless of cv-quals. */
8883 bool
8884 ptr_reasonably_similar (const_tree to, const_tree from)
8886 for (; ; to = TREE_TYPE (to), from = TREE_TYPE (from))
8888 /* Any target type is similar enough to void. */
8889 if (VOID_TYPE_P (to))
8890 return !error_type_p (from);
8891 if (VOID_TYPE_P (from))
8892 return !error_type_p (to);
8894 if (TREE_CODE (to) != TREE_CODE (from))
8895 return false;
8897 if (TREE_CODE (from) == OFFSET_TYPE
8898 && comptypes (TYPE_OFFSET_BASETYPE (to),
8899 TYPE_OFFSET_BASETYPE (from),
8900 COMPARE_BASE | COMPARE_DERIVED))
8901 continue;
8903 if (TREE_CODE (to) == VECTOR_TYPE
8904 && vector_types_convertible_p (to, from, false))
8905 return true;
8907 if (TREE_CODE (to) == INTEGER_TYPE
8908 && TYPE_PRECISION (to) == TYPE_PRECISION (from))
8909 return true;
8911 if (TREE_CODE (to) == FUNCTION_TYPE)
8912 return !error_type_p (to) && !error_type_p (from);
8914 if (!TYPE_PTR_P (to))
8916 /* When either type is incomplete avoid DERIVED_FROM_P,
8917 which may call complete_type (c++/57942). */
8918 bool b = !COMPLETE_TYPE_P (to) || !COMPLETE_TYPE_P (from);
8919 return comptypes
8920 (TYPE_MAIN_VARIANT (to), TYPE_MAIN_VARIANT (from),
8921 b ? COMPARE_STRICT : COMPARE_BASE | COMPARE_DERIVED);
8926 /* Return true if TO and FROM (both of which are POINTER_TYPEs or
8927 pointer-to-member types) are the same, ignoring cv-qualification at
8928 all levels. */
8930 bool
8931 comp_ptr_ttypes_const (tree to, tree from)
8933 bool is_opaque_pointer = false;
8935 for (; ; to = TREE_TYPE (to), from = TREE_TYPE (from))
8937 if (TREE_CODE (to) != TREE_CODE (from))
8938 return false;
8940 if (TREE_CODE (from) == OFFSET_TYPE
8941 && same_type_p (TYPE_OFFSET_BASETYPE (from),
8942 TYPE_OFFSET_BASETYPE (to)))
8943 continue;
8945 if (TREE_CODE (to) == VECTOR_TYPE)
8946 is_opaque_pointer = vector_targets_convertible_p (to, from);
8948 if (!TYPE_PTR_P (to))
8949 return (is_opaque_pointer
8950 || same_type_ignoring_top_level_qualifiers_p (to, from));
8954 /* Returns the type qualifiers for this type, including the qualifiers on the
8955 elements for an array type. */
8958 cp_type_quals (const_tree type)
8960 int quals;
8961 /* This CONST_CAST is okay because strip_array_types returns its
8962 argument unmodified and we assign it to a const_tree. */
8963 type = strip_array_types (CONST_CAST_TREE (type));
8964 if (type == error_mark_node
8965 /* Quals on a FUNCTION_TYPE are memfn quals. */
8966 || TREE_CODE (type) == FUNCTION_TYPE)
8967 return TYPE_UNQUALIFIED;
8968 quals = TYPE_QUALS (type);
8969 /* METHOD and REFERENCE_TYPEs should never have quals. */
8970 gcc_assert ((TREE_CODE (type) != METHOD_TYPE
8971 && TREE_CODE (type) != REFERENCE_TYPE)
8972 || ((quals & (TYPE_QUAL_CONST|TYPE_QUAL_VOLATILE))
8973 == TYPE_UNQUALIFIED));
8974 return quals;
8977 /* Returns the function-ref-qualifier for TYPE */
8979 cp_ref_qualifier
8980 type_memfn_rqual (const_tree type)
8982 gcc_assert (TREE_CODE (type) == FUNCTION_TYPE
8983 || TREE_CODE (type) == METHOD_TYPE);
8985 if (!FUNCTION_REF_QUALIFIED (type))
8986 return REF_QUAL_NONE;
8987 else if (FUNCTION_RVALUE_QUALIFIED (type))
8988 return REF_QUAL_RVALUE;
8989 else
8990 return REF_QUAL_LVALUE;
8993 /* Returns the function-cv-quals for TYPE, which must be a FUNCTION_TYPE or
8994 METHOD_TYPE. */
8997 type_memfn_quals (const_tree type)
8999 if (TREE_CODE (type) == FUNCTION_TYPE)
9000 return TYPE_QUALS (type);
9001 else if (TREE_CODE (type) == METHOD_TYPE)
9002 return cp_type_quals (class_of_this_parm (type));
9003 else
9004 gcc_unreachable ();
9007 /* Returns the FUNCTION_TYPE TYPE with its function-cv-quals changed to
9008 MEMFN_QUALS and its ref-qualifier to RQUAL. */
9010 tree
9011 apply_memfn_quals (tree type, cp_cv_quals memfn_quals, cp_ref_qualifier rqual)
9013 /* Could handle METHOD_TYPE here if necessary. */
9014 gcc_assert (TREE_CODE (type) == FUNCTION_TYPE);
9015 if (TYPE_QUALS (type) == memfn_quals
9016 && type_memfn_rqual (type) == rqual)
9017 return type;
9019 /* This should really have a different TYPE_MAIN_VARIANT, but that gets
9020 complex. */
9021 tree result = build_qualified_type (type, memfn_quals);
9022 if (tree canon = TYPE_CANONICAL (result))
9023 if (canon != result)
9024 /* check_qualified_type doesn't check the ref-qualifier, so make sure
9025 TYPE_CANONICAL is correct. */
9026 TYPE_CANONICAL (result)
9027 = build_ref_qualified_type (canon, type_memfn_rqual (result));
9028 result = build_exception_variant (result, TYPE_RAISES_EXCEPTIONS (type));
9029 return build_ref_qualified_type (result, rqual);
9032 /* Returns nonzero if TYPE is const or volatile. */
9034 bool
9035 cv_qualified_p (const_tree type)
9037 int quals = cp_type_quals (type);
9038 return (quals & (TYPE_QUAL_CONST|TYPE_QUAL_VOLATILE)) != 0;
9041 /* Returns nonzero if the TYPE contains a mutable member. */
9043 bool
9044 cp_has_mutable_p (const_tree type)
9046 /* This CONST_CAST is okay because strip_array_types returns its
9047 argument unmodified and we assign it to a const_tree. */
9048 type = strip_array_types (CONST_CAST_TREE(type));
9050 return CLASS_TYPE_P (type) && CLASSTYPE_HAS_MUTABLE (type);
9053 /* Set TREE_READONLY and TREE_VOLATILE on DECL as indicated by the
9054 TYPE_QUALS. For a VAR_DECL, this may be an optimistic
9055 approximation. In particular, consider:
9057 int f();
9058 struct S { int i; };
9059 const S s = { f(); }
9061 Here, we will make "s" as TREE_READONLY (because it is declared
9062 "const") -- only to reverse ourselves upon seeing that the
9063 initializer is non-constant. */
9065 void
9066 cp_apply_type_quals_to_decl (int type_quals, tree decl)
9068 tree type = TREE_TYPE (decl);
9070 if (type == error_mark_node)
9071 return;
9073 if (TREE_CODE (decl) == TYPE_DECL)
9074 return;
9076 gcc_assert (!(TREE_CODE (type) == FUNCTION_TYPE
9077 && type_quals != TYPE_UNQUALIFIED));
9079 /* Avoid setting TREE_READONLY incorrectly. */
9080 /* We used to check TYPE_NEEDS_CONSTRUCTING here, but now a constexpr
9081 constructor can produce constant init, so rely on cp_finish_decl to
9082 clear TREE_READONLY if the variable has non-constant init. */
9084 /* If the type has (or might have) a mutable component, that component
9085 might be modified. */
9086 if (TYPE_HAS_MUTABLE_P (type) || !COMPLETE_TYPE_P (type))
9087 type_quals &= ~TYPE_QUAL_CONST;
9089 c_apply_type_quals_to_decl (type_quals, decl);
9092 /* Subroutine of casts_away_constness. Make T1 and T2 point at
9093 exemplar types such that casting T1 to T2 is casting away constness
9094 if and only if there is no implicit conversion from T1 to T2. */
9096 static void
9097 casts_away_constness_r (tree *t1, tree *t2, tsubst_flags_t complain)
9099 int quals1;
9100 int quals2;
9102 /* [expr.const.cast]
9104 For multi-level pointer to members and multi-level mixed pointers
9105 and pointers to members (conv.qual), the "member" aspect of a
9106 pointer to member level is ignored when determining if a const
9107 cv-qualifier has been cast away. */
9108 /* [expr.const.cast]
9110 For two pointer types:
9112 X1 is T1cv1,1 * ... cv1,N * where T1 is not a pointer type
9113 X2 is T2cv2,1 * ... cv2,M * where T2 is not a pointer type
9114 K is min(N,M)
9116 casting from X1 to X2 casts away constness if, for a non-pointer
9117 type T there does not exist an implicit conversion (clause
9118 _conv_) from:
9120 Tcv1,(N-K+1) * cv1,(N-K+2) * ... cv1,N *
9124 Tcv2,(M-K+1) * cv2,(M-K+2) * ... cv2,M *. */
9125 if ((!TYPE_PTR_P (*t1) && !TYPE_PTRDATAMEM_P (*t1))
9126 || (!TYPE_PTR_P (*t2) && !TYPE_PTRDATAMEM_P (*t2)))
9128 *t1 = cp_build_qualified_type (void_type_node,
9129 cp_type_quals (*t1));
9130 *t2 = cp_build_qualified_type (void_type_node,
9131 cp_type_quals (*t2));
9132 return;
9135 quals1 = cp_type_quals (*t1);
9136 quals2 = cp_type_quals (*t2);
9138 if (TYPE_PTRDATAMEM_P (*t1))
9139 *t1 = TYPE_PTRMEM_POINTED_TO_TYPE (*t1);
9140 else
9141 *t1 = TREE_TYPE (*t1);
9142 if (TYPE_PTRDATAMEM_P (*t2))
9143 *t2 = TYPE_PTRMEM_POINTED_TO_TYPE (*t2);
9144 else
9145 *t2 = TREE_TYPE (*t2);
9147 casts_away_constness_r (t1, t2, complain);
9148 *t1 = build_pointer_type (*t1);
9149 *t2 = build_pointer_type (*t2);
9150 *t1 = cp_build_qualified_type (*t1, quals1);
9151 *t2 = cp_build_qualified_type (*t2, quals2);
9154 /* Returns nonzero if casting from TYPE1 to TYPE2 casts away
9155 constness.
9157 ??? This function returns non-zero if casting away qualifiers not
9158 just const. We would like to return to the caller exactly which
9159 qualifiers are casted away to give more accurate diagnostics.
9162 static bool
9163 casts_away_constness (tree t1, tree t2, tsubst_flags_t complain)
9165 if (TREE_CODE (t2) == REFERENCE_TYPE)
9167 /* [expr.const.cast]
9169 Casting from an lvalue of type T1 to an lvalue of type T2
9170 using a reference cast casts away constness if a cast from an
9171 rvalue of type "pointer to T1" to the type "pointer to T2"
9172 casts away constness. */
9173 t1 = (TREE_CODE (t1) == REFERENCE_TYPE ? TREE_TYPE (t1) : t1);
9174 return casts_away_constness (build_pointer_type (t1),
9175 build_pointer_type (TREE_TYPE (t2)),
9176 complain);
9179 if (TYPE_PTRDATAMEM_P (t1) && TYPE_PTRDATAMEM_P (t2))
9180 /* [expr.const.cast]
9182 Casting from an rvalue of type "pointer to data member of X
9183 of type T1" to the type "pointer to data member of Y of type
9184 T2" casts away constness if a cast from an rvalue of type
9185 "pointer to T1" to the type "pointer to T2" casts away
9186 constness. */
9187 return casts_away_constness
9188 (build_pointer_type (TYPE_PTRMEM_POINTED_TO_TYPE (t1)),
9189 build_pointer_type (TYPE_PTRMEM_POINTED_TO_TYPE (t2)),
9190 complain);
9192 /* Casting away constness is only something that makes sense for
9193 pointer or reference types. */
9194 if (!TYPE_PTR_P (t1) || !TYPE_PTR_P (t2))
9195 return false;
9197 /* Top-level qualifiers don't matter. */
9198 t1 = TYPE_MAIN_VARIANT (t1);
9199 t2 = TYPE_MAIN_VARIANT (t2);
9200 casts_away_constness_r (&t1, &t2, complain);
9201 if (!can_convert (t2, t1, complain))
9202 return true;
9204 return false;
9207 /* If T is a REFERENCE_TYPE return the type to which T refers.
9208 Otherwise, return T itself. */
9210 tree
9211 non_reference (tree t)
9213 if (t && TREE_CODE (t) == REFERENCE_TYPE)
9214 t = TREE_TYPE (t);
9215 return t;
9219 /* Return nonzero if REF is an lvalue valid for this language;
9220 otherwise, print an error message and return zero. USE says
9221 how the lvalue is being used and so selects the error message. */
9224 lvalue_or_else (tree ref, enum lvalue_use use, tsubst_flags_t complain)
9226 cp_lvalue_kind kind = lvalue_kind (ref);
9228 if (kind == clk_none)
9230 if (complain & tf_error)
9231 lvalue_error (input_location, use);
9232 return 0;
9234 else if (kind & (clk_rvalueref|clk_class))
9236 if (!(complain & tf_error))
9237 return 0;
9238 if (kind & clk_class)
9239 /* Make this a permerror because we used to accept it. */
9240 permerror (input_location, "using temporary as lvalue");
9241 else
9242 error ("using xvalue (rvalue reference) as lvalue");
9244 return 1;
9247 /* Return true if a user-defined literal operator is a raw operator. */
9249 bool
9250 check_raw_literal_operator (const_tree decl)
9252 tree argtypes = TYPE_ARG_TYPES (TREE_TYPE (decl));
9253 tree argtype;
9254 int arity;
9255 bool maybe_raw_p = false;
9257 /* Count the number and type of arguments and check for ellipsis. */
9258 for (argtype = argtypes, arity = 0;
9259 argtype && argtype != void_list_node;
9260 ++arity, argtype = TREE_CHAIN (argtype))
9262 tree t = TREE_VALUE (argtype);
9264 if (same_type_p (t, const_string_type_node))
9265 maybe_raw_p = true;
9267 if (!argtype)
9268 return false; /* Found ellipsis. */
9270 if (!maybe_raw_p || arity != 1)
9271 return false;
9273 return true;
9277 /* Return true if a user-defined literal operator has one of the allowed
9278 argument types. */
9280 bool
9281 check_literal_operator_args (const_tree decl,
9282 bool *long_long_unsigned_p, bool *long_double_p)
9284 tree argtypes = TYPE_ARG_TYPES (TREE_TYPE (decl));
9286 *long_long_unsigned_p = false;
9287 *long_double_p = false;
9288 if (processing_template_decl || processing_specialization)
9289 return argtypes == void_list_node;
9290 else
9292 tree argtype;
9293 int arity;
9294 int max_arity = 2;
9296 /* Count the number and type of arguments and check for ellipsis. */
9297 for (argtype = argtypes, arity = 0;
9298 argtype && argtype != void_list_node;
9299 argtype = TREE_CHAIN (argtype))
9301 tree t = TREE_VALUE (argtype);
9302 ++arity;
9304 if (TYPE_PTR_P (t))
9306 bool maybe_raw_p = false;
9307 t = TREE_TYPE (t);
9308 if (cp_type_quals (t) != TYPE_QUAL_CONST)
9309 return false;
9310 t = TYPE_MAIN_VARIANT (t);
9311 if ((maybe_raw_p = same_type_p (t, char_type_node))
9312 || same_type_p (t, wchar_type_node)
9313 || same_type_p (t, char16_type_node)
9314 || same_type_p (t, char32_type_node))
9316 argtype = TREE_CHAIN (argtype);
9317 if (!argtype)
9318 return false;
9319 t = TREE_VALUE (argtype);
9320 if (maybe_raw_p && argtype == void_list_node)
9321 return true;
9322 else if (same_type_p (t, size_type_node))
9324 ++arity;
9325 continue;
9327 else
9328 return false;
9331 else if (same_type_p (t, long_long_unsigned_type_node))
9333 max_arity = 1;
9334 *long_long_unsigned_p = true;
9336 else if (same_type_p (t, long_double_type_node))
9338 max_arity = 1;
9339 *long_double_p = true;
9341 else if (same_type_p (t, char_type_node))
9342 max_arity = 1;
9343 else if (same_type_p (t, wchar_type_node))
9344 max_arity = 1;
9345 else if (same_type_p (t, char16_type_node))
9346 max_arity = 1;
9347 else if (same_type_p (t, char32_type_node))
9348 max_arity = 1;
9349 else
9350 return false;
9352 if (!argtype)
9353 return false; /* Found ellipsis. */
9355 if (arity != max_arity)
9356 return false;
9358 return true;