]> git.ipfire.org Git - thirdparty/gcc.git/blob - gcc/cp/typeck.c
c++: Handle multiple aggregate overloads [PR95319].
[thirdparty/gcc.git] / gcc / cp / typeck.c
1 /* Build expressions with type checking for C++ compiler.
2 Copyright (C) 1987-2020 Free Software Foundation, Inc.
3 Hacked by Michael Tiemann (tiemann@cygnus.com)
4
5 This file is part of GCC.
6
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.
11
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.
16
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/>. */
20
21
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. */
26
27 #include "config.h"
28 #include "system.h"
29 #include "coretypes.h"
30 #include "target.h"
31 #include "cp-tree.h"
32 #include "stor-layout.h"
33 #include "varasm.h"
34 #include "intl.h"
35 #include "convert.h"
36 #include "c-family/c-objc.h"
37 #include "c-family/c-ubsan.h"
38 #include "gcc-rich-location.h"
39 #include "stringpool.h"
40 #include "attribs.h"
41 #include "asan.h"
42 #include "gimplify.h"
43
44 static tree cp_build_addr_expr_strict (tree, tsubst_flags_t);
45 static tree cp_build_function_call (tree, tree, tsubst_flags_t);
46 static tree pfn_from_ptrmemfunc (tree);
47 static tree delta_from_ptrmemfunc (tree);
48 static tree convert_for_assignment (tree, tree, impl_conv_rhs, tree, int,
49 tsubst_flags_t, int);
50 static tree cp_pointer_int_sum (location_t, enum tree_code, tree, tree,
51 tsubst_flags_t);
52 static tree rationalize_conditional_expr (enum tree_code, tree,
53 tsubst_flags_t);
54 static bool comp_ptr_ttypes_real (tree, tree, int);
55 static bool comp_except_types (tree, tree, bool);
56 static bool comp_array_types (const_tree, const_tree, compare_bounds_t, bool);
57 static tree pointer_diff (location_t, tree, tree, tree, tsubst_flags_t, tree *);
58 static tree get_delta_difference (tree, tree, bool, bool, tsubst_flags_t);
59 static void casts_away_constness_r (tree *, tree *, tsubst_flags_t);
60 static bool casts_away_constness (tree, tree, tsubst_flags_t);
61 static bool maybe_warn_about_returning_address_of_local (tree, location_t = UNKNOWN_LOCATION);
62 static void error_args_num (location_t, tree, bool);
63 static int convert_arguments (tree, vec<tree, va_gc> **, tree, int,
64 tsubst_flags_t);
65 static bool is_std_move_p (tree);
66 static bool is_std_forward_p (tree);
67
68 /* Do `exp = require_complete_type (exp);' to make sure exp
69 does not have an incomplete type. (That includes void types.)
70 Returns error_mark_node if the VALUE does not have
71 complete type when this function returns. */
72
73 tree
74 require_complete_type_sfinae (tree value, tsubst_flags_t complain)
75 {
76 tree type;
77
78 if (processing_template_decl || value == error_mark_node)
79 return value;
80
81 if (TREE_CODE (value) == OVERLOAD)
82 type = unknown_type_node;
83 else
84 type = TREE_TYPE (value);
85
86 if (type == error_mark_node)
87 return error_mark_node;
88
89 /* First, detect a valid value with a complete type. */
90 if (COMPLETE_TYPE_P (type))
91 return value;
92
93 if (complete_type_or_maybe_complain (type, value, complain))
94 return value;
95 else
96 return error_mark_node;
97 }
98
99 tree
100 require_complete_type (tree value)
101 {
102 return require_complete_type_sfinae (value, tf_warning_or_error);
103 }
104
105 /* Try to complete TYPE, if it is incomplete. For example, if TYPE is
106 a template instantiation, do the instantiation. Returns TYPE,
107 whether or not it could be completed, unless something goes
108 horribly wrong, in which case the error_mark_node is returned. */
109
110 tree
111 complete_type (tree type)
112 {
113 if (type == NULL_TREE)
114 /* Rather than crash, we return something sure to cause an error
115 at some point. */
116 return error_mark_node;
117
118 if (type == error_mark_node || COMPLETE_TYPE_P (type))
119 ;
120 else if (TREE_CODE (type) == ARRAY_TYPE)
121 {
122 tree t = complete_type (TREE_TYPE (type));
123 unsigned int needs_constructing, has_nontrivial_dtor;
124 if (COMPLETE_TYPE_P (t) && !dependent_type_p (type))
125 layout_type (type);
126 needs_constructing
127 = TYPE_NEEDS_CONSTRUCTING (TYPE_MAIN_VARIANT (t));
128 has_nontrivial_dtor
129 = TYPE_HAS_NONTRIVIAL_DESTRUCTOR (TYPE_MAIN_VARIANT (t));
130 for (t = TYPE_MAIN_VARIANT (type); t; t = TYPE_NEXT_VARIANT (t))
131 {
132 TYPE_NEEDS_CONSTRUCTING (t) = needs_constructing;
133 TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t) = has_nontrivial_dtor;
134 }
135 }
136 else if (CLASS_TYPE_P (type) && CLASSTYPE_TEMPLATE_INSTANTIATION (type))
137 instantiate_class_template (TYPE_MAIN_VARIANT (type));
138
139 return type;
140 }
141
142 /* Like complete_type, but issue an error if the TYPE cannot be completed.
143 VALUE is used for informative diagnostics.
144 Returns NULL_TREE if the type cannot be made complete. */
145
146 tree
147 complete_type_or_maybe_complain (tree type, tree value, tsubst_flags_t complain)
148 {
149 type = complete_type (type);
150 if (type == error_mark_node)
151 /* We already issued an error. */
152 return NULL_TREE;
153 else if (!COMPLETE_TYPE_P (type))
154 {
155 if (complain & tf_error)
156 cxx_incomplete_type_diagnostic (value, type, DK_ERROR);
157 return NULL_TREE;
158 }
159 else
160 return type;
161 }
162
163 tree
164 complete_type_or_else (tree type, tree value)
165 {
166 return complete_type_or_maybe_complain (type, value, tf_warning_or_error);
167 }
168
169 \f
170 /* Return the common type of two parameter lists.
171 We assume that comptypes has already been done and returned 1;
172 if that isn't so, this may crash.
173
174 As an optimization, free the space we allocate if the parameter
175 lists are already common. */
176
177 static tree
178 commonparms (tree p1, tree p2)
179 {
180 tree oldargs = p1, newargs, n;
181 int i, len;
182 int any_change = 0;
183
184 len = list_length (p1);
185 newargs = tree_last (p1);
186
187 if (newargs == void_list_node)
188 i = 1;
189 else
190 {
191 i = 0;
192 newargs = 0;
193 }
194
195 for (; i < len; i++)
196 newargs = tree_cons (NULL_TREE, NULL_TREE, newargs);
197
198 n = newargs;
199
200 for (i = 0; p1;
201 p1 = TREE_CHAIN (p1), p2 = TREE_CHAIN (p2), n = TREE_CHAIN (n), i++)
202 {
203 if (TREE_PURPOSE (p1) && !TREE_PURPOSE (p2))
204 {
205 TREE_PURPOSE (n) = TREE_PURPOSE (p1);
206 any_change = 1;
207 }
208 else if (! TREE_PURPOSE (p1))
209 {
210 if (TREE_PURPOSE (p2))
211 {
212 TREE_PURPOSE (n) = TREE_PURPOSE (p2);
213 any_change = 1;
214 }
215 }
216 else
217 {
218 if (simple_cst_equal (TREE_PURPOSE (p1), TREE_PURPOSE (p2)) != 1)
219 any_change = 1;
220 TREE_PURPOSE (n) = TREE_PURPOSE (p2);
221 }
222 if (TREE_VALUE (p1) != TREE_VALUE (p2))
223 {
224 any_change = 1;
225 TREE_VALUE (n) = merge_types (TREE_VALUE (p1), TREE_VALUE (p2));
226 }
227 else
228 TREE_VALUE (n) = TREE_VALUE (p1);
229 }
230 if (! any_change)
231 return oldargs;
232
233 return newargs;
234 }
235
236 /* Given a type, perhaps copied for a typedef,
237 find the "original" version of it. */
238 static tree
239 original_type (tree t)
240 {
241 int quals = cp_type_quals (t);
242 while (t != error_mark_node
243 && TYPE_NAME (t) != NULL_TREE)
244 {
245 tree x = TYPE_NAME (t);
246 if (TREE_CODE (x) != TYPE_DECL)
247 break;
248 x = DECL_ORIGINAL_TYPE (x);
249 if (x == NULL_TREE)
250 break;
251 t = x;
252 }
253 return cp_build_qualified_type (t, quals);
254 }
255
256 /* Return the common type for two arithmetic types T1 and T2 under the
257 usual arithmetic conversions. The default conversions have already
258 been applied, and enumerated types converted to their compatible
259 integer types. */
260
261 static tree
262 cp_common_type (tree t1, tree t2)
263 {
264 enum tree_code code1 = TREE_CODE (t1);
265 enum tree_code code2 = TREE_CODE (t2);
266 tree attributes;
267 int i;
268
269
270 /* In what follows, we slightly generalize the rules given in [expr] so
271 as to deal with `long long' and `complex'. First, merge the
272 attributes. */
273 attributes = (*targetm.merge_type_attributes) (t1, t2);
274
275 if (SCOPED_ENUM_P (t1) || SCOPED_ENUM_P (t2))
276 {
277 if (TYPE_MAIN_VARIANT (t1) == TYPE_MAIN_VARIANT (t2))
278 return build_type_attribute_variant (t1, attributes);
279 else
280 return NULL_TREE;
281 }
282
283 /* FIXME: Attributes. */
284 gcc_assert (ARITHMETIC_TYPE_P (t1)
285 || VECTOR_TYPE_P (t1)
286 || UNSCOPED_ENUM_P (t1));
287 gcc_assert (ARITHMETIC_TYPE_P (t2)
288 || VECTOR_TYPE_P (t2)
289 || UNSCOPED_ENUM_P (t2));
290
291 /* If one type is complex, form the common type of the non-complex
292 components, then make that complex. Use T1 or T2 if it is the
293 required type. */
294 if (code1 == COMPLEX_TYPE || code2 == COMPLEX_TYPE)
295 {
296 tree subtype1 = code1 == COMPLEX_TYPE ? TREE_TYPE (t1) : t1;
297 tree subtype2 = code2 == COMPLEX_TYPE ? TREE_TYPE (t2) : t2;
298 tree subtype
299 = type_after_usual_arithmetic_conversions (subtype1, subtype2);
300
301 if (code1 == COMPLEX_TYPE && TREE_TYPE (t1) == subtype)
302 return build_type_attribute_variant (t1, attributes);
303 else if (code2 == COMPLEX_TYPE && TREE_TYPE (t2) == subtype)
304 return build_type_attribute_variant (t2, attributes);
305 else
306 return build_type_attribute_variant (build_complex_type (subtype),
307 attributes);
308 }
309
310 if (code1 == VECTOR_TYPE)
311 {
312 /* When we get here we should have two vectors of the same size.
313 Just prefer the unsigned one if present. */
314 if (TYPE_UNSIGNED (t1))
315 return build_type_attribute_variant (t1, attributes);
316 else
317 return build_type_attribute_variant (t2, attributes);
318 }
319
320 /* If only one is real, use it as the result. */
321 if (code1 == REAL_TYPE && code2 != REAL_TYPE)
322 return build_type_attribute_variant (t1, attributes);
323 if (code2 == REAL_TYPE && code1 != REAL_TYPE)
324 return build_type_attribute_variant (t2, attributes);
325
326 /* Both real or both integers; use the one with greater precision. */
327 if (TYPE_PRECISION (t1) > TYPE_PRECISION (t2))
328 return build_type_attribute_variant (t1, attributes);
329 else if (TYPE_PRECISION (t2) > TYPE_PRECISION (t1))
330 return build_type_attribute_variant (t2, attributes);
331
332 /* The types are the same; no need to do anything fancy. */
333 if (TYPE_MAIN_VARIANT (t1) == TYPE_MAIN_VARIANT (t2))
334 return build_type_attribute_variant (t1, attributes);
335
336 if (code1 != REAL_TYPE)
337 {
338 /* If one is unsigned long long, then convert the other to unsigned
339 long long. */
340 if (same_type_p (TYPE_MAIN_VARIANT (t1), long_long_unsigned_type_node)
341 || same_type_p (TYPE_MAIN_VARIANT (t2), long_long_unsigned_type_node))
342 return build_type_attribute_variant (long_long_unsigned_type_node,
343 attributes);
344 /* If one is a long long, and the other is an unsigned long, and
345 long long can represent all the values of an unsigned long, then
346 convert to a long long. Otherwise, convert to an unsigned long
347 long. Otherwise, if either operand is long long, convert the
348 other to long long.
349
350 Since we're here, we know the TYPE_PRECISION is the same;
351 therefore converting to long long cannot represent all the values
352 of an unsigned long, so we choose unsigned long long in that
353 case. */
354 if (same_type_p (TYPE_MAIN_VARIANT (t1), long_long_integer_type_node)
355 || same_type_p (TYPE_MAIN_VARIANT (t2), long_long_integer_type_node))
356 {
357 tree t = ((TYPE_UNSIGNED (t1) || TYPE_UNSIGNED (t2))
358 ? long_long_unsigned_type_node
359 : long_long_integer_type_node);
360 return build_type_attribute_variant (t, attributes);
361 }
362
363 /* Go through the same procedure, but for longs. */
364 if (same_type_p (TYPE_MAIN_VARIANT (t1), long_unsigned_type_node)
365 || same_type_p (TYPE_MAIN_VARIANT (t2), long_unsigned_type_node))
366 return build_type_attribute_variant (long_unsigned_type_node,
367 attributes);
368 if (same_type_p (TYPE_MAIN_VARIANT (t1), long_integer_type_node)
369 || same_type_p (TYPE_MAIN_VARIANT (t2), long_integer_type_node))
370 {
371 tree t = ((TYPE_UNSIGNED (t1) || TYPE_UNSIGNED (t2))
372 ? long_unsigned_type_node : long_integer_type_node);
373 return build_type_attribute_variant (t, attributes);
374 }
375
376 /* For __intN types, either the type is __int128 (and is lower
377 priority than the types checked above, but higher than other
378 128-bit types) or it's known to not be the same size as other
379 types (enforced in toplev.c). Prefer the unsigned type. */
380 for (i = 0; i < NUM_INT_N_ENTS; i ++)
381 {
382 if (int_n_enabled_p [i]
383 && (same_type_p (TYPE_MAIN_VARIANT (t1), int_n_trees[i].signed_type)
384 || same_type_p (TYPE_MAIN_VARIANT (t2), int_n_trees[i].signed_type)
385 || same_type_p (TYPE_MAIN_VARIANT (t1), int_n_trees[i].unsigned_type)
386 || same_type_p (TYPE_MAIN_VARIANT (t2), int_n_trees[i].unsigned_type)))
387 {
388 tree t = ((TYPE_UNSIGNED (t1) || TYPE_UNSIGNED (t2))
389 ? int_n_trees[i].unsigned_type
390 : int_n_trees[i].signed_type);
391 return build_type_attribute_variant (t, attributes);
392 }
393 }
394
395 /* Otherwise prefer the unsigned one. */
396 if (TYPE_UNSIGNED (t1))
397 return build_type_attribute_variant (t1, attributes);
398 else
399 return build_type_attribute_variant (t2, attributes);
400 }
401 else
402 {
403 if (same_type_p (TYPE_MAIN_VARIANT (t1), long_double_type_node)
404 || same_type_p (TYPE_MAIN_VARIANT (t2), long_double_type_node))
405 return build_type_attribute_variant (long_double_type_node,
406 attributes);
407 if (same_type_p (TYPE_MAIN_VARIANT (t1), double_type_node)
408 || same_type_p (TYPE_MAIN_VARIANT (t2), double_type_node))
409 return build_type_attribute_variant (double_type_node,
410 attributes);
411 if (same_type_p (TYPE_MAIN_VARIANT (t1), float_type_node)
412 || same_type_p (TYPE_MAIN_VARIANT (t2), float_type_node))
413 return build_type_attribute_variant (float_type_node,
414 attributes);
415
416 /* Two floating-point types whose TYPE_MAIN_VARIANTs are none of
417 the standard C++ floating-point types. Logic earlier in this
418 function has already eliminated the possibility that
419 TYPE_PRECISION (t2) != TYPE_PRECISION (t1), so there's no
420 compelling reason to choose one or the other. */
421 return build_type_attribute_variant (t1, attributes);
422 }
423 }
424
425 /* T1 and T2 are arithmetic or enumeration types. Return the type
426 that will result from the "usual arithmetic conversions" on T1 and
427 T2 as described in [expr]. */
428
429 tree
430 type_after_usual_arithmetic_conversions (tree t1, tree t2)
431 {
432 gcc_assert (ARITHMETIC_TYPE_P (t1)
433 || VECTOR_TYPE_P (t1)
434 || UNSCOPED_ENUM_P (t1));
435 gcc_assert (ARITHMETIC_TYPE_P (t2)
436 || VECTOR_TYPE_P (t2)
437 || UNSCOPED_ENUM_P (t2));
438
439 /* Perform the integral promotions. We do not promote real types here. */
440 if (INTEGRAL_OR_ENUMERATION_TYPE_P (t1)
441 && INTEGRAL_OR_ENUMERATION_TYPE_P (t2))
442 {
443 t1 = type_promotes_to (t1);
444 t2 = type_promotes_to (t2);
445 }
446
447 return cp_common_type (t1, t2);
448 }
449
450 static void
451 composite_pointer_error (const op_location_t &location,
452 diagnostic_t kind, tree t1, tree t2,
453 composite_pointer_operation operation)
454 {
455 switch (operation)
456 {
457 case CPO_COMPARISON:
458 emit_diagnostic (kind, location, 0,
459 "comparison between "
460 "distinct pointer types %qT and %qT lacks a cast",
461 t1, t2);
462 break;
463 case CPO_CONVERSION:
464 emit_diagnostic (kind, location, 0,
465 "conversion between "
466 "distinct pointer types %qT and %qT lacks a cast",
467 t1, t2);
468 break;
469 case CPO_CONDITIONAL_EXPR:
470 emit_diagnostic (kind, location, 0,
471 "conditional expression between "
472 "distinct pointer types %qT and %qT lacks a cast",
473 t1, t2);
474 break;
475 default:
476 gcc_unreachable ();
477 }
478 }
479
480 /* Subroutine of composite_pointer_type to implement the recursive
481 case. See that function for documentation of the parameters. And ADD_CONST
482 is used to track adding "const" where needed. */
483
484 static tree
485 composite_pointer_type_r (const op_location_t &location,
486 tree t1, tree t2, bool *add_const,
487 composite_pointer_operation operation,
488 tsubst_flags_t complain)
489 {
490 tree pointee1;
491 tree pointee2;
492 tree result_type;
493 tree attributes;
494
495 /* Determine the types pointed to by T1 and T2. */
496 if (TYPE_PTR_P (t1))
497 {
498 pointee1 = TREE_TYPE (t1);
499 pointee2 = TREE_TYPE (t2);
500 }
501 else
502 {
503 pointee1 = TYPE_PTRMEM_POINTED_TO_TYPE (t1);
504 pointee2 = TYPE_PTRMEM_POINTED_TO_TYPE (t2);
505 }
506
507 /* [expr.type]
508
509 If T1 and T2 are similar types, the result is the cv-combined type of
510 T1 and T2. */
511 if (same_type_ignoring_top_level_qualifiers_p (pointee1, pointee2))
512 result_type = pointee1;
513 else if ((TYPE_PTR_P (pointee1) && TYPE_PTR_P (pointee2))
514 || (TYPE_PTRMEM_P (pointee1) && TYPE_PTRMEM_P (pointee2)))
515 {
516 result_type = composite_pointer_type_r (location, pointee1, pointee2,
517 add_const, operation, complain);
518 if (result_type == error_mark_node)
519 return error_mark_node;
520 }
521 else
522 {
523 if (complain & tf_error)
524 composite_pointer_error (location, DK_PERMERROR,
525 t1, t2, operation);
526 else
527 return error_mark_node;
528 result_type = void_type_node;
529 }
530 const int q1 = cp_type_quals (pointee1);
531 const int q2 = cp_type_quals (pointee2);
532 const int quals = q1 | q2;
533 result_type = cp_build_qualified_type (result_type,
534 (quals | (*add_const
535 ? TYPE_QUAL_CONST
536 : TYPE_UNQUALIFIED)));
537 /* The cv-combined type can add "const" as per [conv.qual]/3.3 (except for
538 the TLQ). The reason is that both T1 and T2 can then be converted to the
539 cv-combined type of T1 and T2. */
540 if (quals != q1 || quals != q2)
541 *add_const = true;
542 /* If the original types were pointers to members, so is the
543 result. */
544 if (TYPE_PTRMEM_P (t1))
545 {
546 if (!same_type_p (TYPE_PTRMEM_CLASS_TYPE (t1),
547 TYPE_PTRMEM_CLASS_TYPE (t2)))
548 {
549 if (complain & tf_error)
550 composite_pointer_error (location, DK_PERMERROR,
551 t1, t2, operation);
552 else
553 return error_mark_node;
554 }
555 result_type = build_ptrmem_type (TYPE_PTRMEM_CLASS_TYPE (t1),
556 result_type);
557 }
558 else
559 result_type = build_pointer_type (result_type);
560
561 /* Merge the attributes. */
562 attributes = (*targetm.merge_type_attributes) (t1, t2);
563 return build_type_attribute_variant (result_type, attributes);
564 }
565
566 /* Return the composite pointer type (see [expr.type]) 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.
570
571 This routine also implements the computation of a common type for
572 pointers-to-members as per [expr.eq]. */
573
574 tree
575 composite_pointer_type (const op_location_t &location,
576 tree t1, tree t2, tree arg1, tree arg2,
577 composite_pointer_operation operation,
578 tsubst_flags_t complain)
579 {
580 tree class1;
581 tree class2;
582
583 /* [expr.type]
584
585 If one operand is a null pointer constant, the composite pointer
586 type is the type of the other operand. */
587 if (null_ptr_cst_p (arg1))
588 return t2;
589 if (null_ptr_cst_p (arg2))
590 return t1;
591
592 /* We have:
593
594 [expr.type]
595
596 If one of the operands has type "pointer to cv1 void", then
597 the other has type "pointer to cv2 T", and the composite pointer
598 type is "pointer to cv12 void", where cv12 is the union of cv1
599 and cv2.
600
601 If either type is a pointer to void, make sure it is T1. */
602 if (TYPE_PTR_P (t2) && VOID_TYPE_P (TREE_TYPE (t2)))
603 std::swap (t1, t2);
604
605 /* Now, if T1 is a pointer to void, merge the qualifiers. */
606 if (TYPE_PTR_P (t1) && VOID_TYPE_P (TREE_TYPE (t1)))
607 {
608 tree attributes;
609 tree result_type;
610
611 if (TYPE_PTRFN_P (t2))
612 {
613 if (complain & tf_error)
614 {
615 switch (operation)
616 {
617 case CPO_COMPARISON:
618 pedwarn (location, OPT_Wpedantic,
619 "ISO C++ forbids comparison between pointer "
620 "of type %<void *%> and pointer-to-function");
621 break;
622 case CPO_CONVERSION:
623 pedwarn (location, OPT_Wpedantic,
624 "ISO C++ forbids conversion between pointer "
625 "of type %<void *%> and pointer-to-function");
626 break;
627 case CPO_CONDITIONAL_EXPR:
628 pedwarn (location, OPT_Wpedantic,
629 "ISO C++ forbids conditional expression between "
630 "pointer of type %<void *%> and "
631 "pointer-to-function");
632 break;
633 default:
634 gcc_unreachable ();
635 }
636 }
637 else
638 return error_mark_node;
639 }
640 result_type
641 = cp_build_qualified_type (void_type_node,
642 (cp_type_quals (TREE_TYPE (t1))
643 | cp_type_quals (TREE_TYPE (t2))));
644 result_type = build_pointer_type (result_type);
645 /* Merge the attributes. */
646 attributes = (*targetm.merge_type_attributes) (t1, t2);
647 return build_type_attribute_variant (result_type, attributes);
648 }
649
650 if (c_dialect_objc () && TYPE_PTR_P (t1)
651 && TYPE_PTR_P (t2))
652 {
653 if (objc_have_common_type (t1, t2, -3, NULL_TREE))
654 return objc_common_type (t1, t2);
655 }
656
657 /* if T1 or T2 is "pointer to noexcept function" and the other type is
658 "pointer to function", where the function types are otherwise the same,
659 "pointer to function" */
660 if (fnptr_conv_p (t1, t2))
661 return t1;
662 if (fnptr_conv_p (t2, t1))
663 return t2;
664
665 /* [expr.eq] permits the application of a pointer conversion to
666 bring the pointers to a common type. */
667 if (TYPE_PTR_P (t1) && TYPE_PTR_P (t2)
668 && CLASS_TYPE_P (TREE_TYPE (t1))
669 && CLASS_TYPE_P (TREE_TYPE (t2))
670 && !same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (t1),
671 TREE_TYPE (t2)))
672 {
673 class1 = TREE_TYPE (t1);
674 class2 = TREE_TYPE (t2);
675
676 if (DERIVED_FROM_P (class1, class2))
677 t2 = (build_pointer_type
678 (cp_build_qualified_type (class1, cp_type_quals (class2))));
679 else if (DERIVED_FROM_P (class2, class1))
680 t1 = (build_pointer_type
681 (cp_build_qualified_type (class2, cp_type_quals (class1))));
682 else
683 {
684 if (complain & tf_error)
685 composite_pointer_error (location, DK_ERROR, t1, t2, operation);
686 return error_mark_node;
687 }
688 }
689 /* [expr.eq] permits the application of a pointer-to-member
690 conversion to change the class type of one of the types. */
691 else if (TYPE_PTRMEM_P (t1)
692 && !same_type_p (TYPE_PTRMEM_CLASS_TYPE (t1),
693 TYPE_PTRMEM_CLASS_TYPE (t2)))
694 {
695 class1 = TYPE_PTRMEM_CLASS_TYPE (t1);
696 class2 = TYPE_PTRMEM_CLASS_TYPE (t2);
697
698 if (DERIVED_FROM_P (class1, class2))
699 t1 = build_ptrmem_type (class2, TYPE_PTRMEM_POINTED_TO_TYPE (t1));
700 else if (DERIVED_FROM_P (class2, class1))
701 t2 = build_ptrmem_type (class1, TYPE_PTRMEM_POINTED_TO_TYPE (t2));
702 else
703 {
704 if (complain & tf_error)
705 switch (operation)
706 {
707 case CPO_COMPARISON:
708 error_at (location, "comparison between distinct "
709 "pointer-to-member types %qT and %qT lacks a cast",
710 t1, t2);
711 break;
712 case CPO_CONVERSION:
713 error_at (location, "conversion between distinct "
714 "pointer-to-member types %qT and %qT lacks a cast",
715 t1, t2);
716 break;
717 case CPO_CONDITIONAL_EXPR:
718 error_at (location, "conditional expression between distinct "
719 "pointer-to-member types %qT and %qT lacks a cast",
720 t1, t2);
721 break;
722 default:
723 gcc_unreachable ();
724 }
725 return error_mark_node;
726 }
727 }
728
729 bool add_const = false;
730 return composite_pointer_type_r (location, t1, t2, &add_const, operation,
731 complain);
732 }
733
734 /* Return the merged type of two types.
735 We assume that comptypes has already been done and returned 1;
736 if that isn't so, this may crash.
737
738 This just combines attributes and default arguments; any other
739 differences would cause the two types to compare unalike. */
740
741 tree
742 merge_types (tree t1, tree t2)
743 {
744 enum tree_code code1;
745 enum tree_code code2;
746 tree attributes;
747
748 /* Save time if the two types are the same. */
749 if (t1 == t2)
750 return t1;
751 if (original_type (t1) == original_type (t2))
752 return t1;
753
754 /* If one type is nonsense, use the other. */
755 if (t1 == error_mark_node)
756 return t2;
757 if (t2 == error_mark_node)
758 return t1;
759
760 /* Handle merging an auto redeclaration with a previous deduced
761 return type. */
762 if (is_auto (t1))
763 return t2;
764
765 /* Merge the attributes. */
766 attributes = (*targetm.merge_type_attributes) (t1, t2);
767
768 if (TYPE_PTRMEMFUNC_P (t1))
769 t1 = TYPE_PTRMEMFUNC_FN_TYPE (t1);
770 if (TYPE_PTRMEMFUNC_P (t2))
771 t2 = TYPE_PTRMEMFUNC_FN_TYPE (t2);
772
773 code1 = TREE_CODE (t1);
774 code2 = TREE_CODE (t2);
775 if (code1 != code2)
776 {
777 gcc_assert (code1 == TYPENAME_TYPE || code2 == TYPENAME_TYPE);
778 if (code1 == TYPENAME_TYPE)
779 {
780 t1 = resolve_typename_type (t1, /*only_current_p=*/true);
781 code1 = TREE_CODE (t1);
782 }
783 else
784 {
785 t2 = resolve_typename_type (t2, /*only_current_p=*/true);
786 code2 = TREE_CODE (t2);
787 }
788 }
789
790 switch (code1)
791 {
792 case POINTER_TYPE:
793 case REFERENCE_TYPE:
794 /* For two pointers, do this recursively on the target type. */
795 {
796 tree target = merge_types (TREE_TYPE (t1), TREE_TYPE (t2));
797 int quals = cp_type_quals (t1);
798
799 if (code1 == POINTER_TYPE)
800 {
801 t1 = build_pointer_type (target);
802 if (TREE_CODE (target) == METHOD_TYPE)
803 t1 = build_ptrmemfunc_type (t1);
804 }
805 else
806 t1 = cp_build_reference_type (target, TYPE_REF_IS_RVALUE (t1));
807 t1 = build_type_attribute_variant (t1, attributes);
808 t1 = cp_build_qualified_type (t1, quals);
809
810 return t1;
811 }
812
813 case OFFSET_TYPE:
814 {
815 int quals;
816 tree pointee;
817 quals = cp_type_quals (t1);
818 pointee = merge_types (TYPE_PTRMEM_POINTED_TO_TYPE (t1),
819 TYPE_PTRMEM_POINTED_TO_TYPE (t2));
820 t1 = build_ptrmem_type (TYPE_PTRMEM_CLASS_TYPE (t1),
821 pointee);
822 t1 = cp_build_qualified_type (t1, quals);
823 break;
824 }
825
826 case ARRAY_TYPE:
827 {
828 tree elt = merge_types (TREE_TYPE (t1), TREE_TYPE (t2));
829 /* Save space: see if the result is identical to one of the args. */
830 if (elt == TREE_TYPE (t1) && TYPE_DOMAIN (t1))
831 return build_type_attribute_variant (t1, attributes);
832 if (elt == TREE_TYPE (t2) && TYPE_DOMAIN (t2))
833 return build_type_attribute_variant (t2, attributes);
834 /* Merge the element types, and have a size if either arg has one. */
835 t1 = build_cplus_array_type
836 (elt, TYPE_DOMAIN (TYPE_DOMAIN (t1) ? t1 : t2));
837 break;
838 }
839
840 case FUNCTION_TYPE:
841 /* Function types: prefer the one that specified arg types.
842 If both do, merge the arg types. Also merge the return types. */
843 {
844 tree valtype = merge_types (TREE_TYPE (t1), TREE_TYPE (t2));
845 tree p1 = TYPE_ARG_TYPES (t1);
846 tree p2 = TYPE_ARG_TYPES (t2);
847 tree parms;
848
849 /* Save space: see if the result is identical to one of the args. */
850 if (valtype == TREE_TYPE (t1) && ! p2)
851 return cp_build_type_attribute_variant (t1, attributes);
852 if (valtype == TREE_TYPE (t2) && ! p1)
853 return cp_build_type_attribute_variant (t2, attributes);
854
855 /* Simple way if one arg fails to specify argument types. */
856 if (p1 == NULL_TREE || TREE_VALUE (p1) == void_type_node)
857 parms = p2;
858 else if (p2 == NULL_TREE || TREE_VALUE (p2) == void_type_node)
859 parms = p1;
860 else
861 parms = commonparms (p1, p2);
862
863 cp_cv_quals quals = type_memfn_quals (t1);
864 cp_ref_qualifier rqual = type_memfn_rqual (t1);
865 gcc_assert (quals == type_memfn_quals (t2));
866 gcc_assert (rqual == type_memfn_rqual (t2));
867
868 tree rval = build_function_type (valtype, parms);
869 rval = apply_memfn_quals (rval, quals);
870 tree raises = merge_exception_specifiers (TYPE_RAISES_EXCEPTIONS (t1),
871 TYPE_RAISES_EXCEPTIONS (t2));
872 bool late_return_type_p = TYPE_HAS_LATE_RETURN_TYPE (t1);
873 t1 = build_cp_fntype_variant (rval, rqual, raises, late_return_type_p);
874 break;
875 }
876
877 case METHOD_TYPE:
878 {
879 /* Get this value the long way, since TYPE_METHOD_BASETYPE
880 is just the main variant of this. */
881 tree basetype = class_of_this_parm (t2);
882 tree raises = merge_exception_specifiers (TYPE_RAISES_EXCEPTIONS (t1),
883 TYPE_RAISES_EXCEPTIONS (t2));
884 cp_ref_qualifier rqual = type_memfn_rqual (t1);
885 tree t3;
886 bool late_return_type_1_p = TYPE_HAS_LATE_RETURN_TYPE (t1);
887
888 /* If this was a member function type, get back to the
889 original type of type member function (i.e., without
890 the class instance variable up front. */
891 t1 = build_function_type (TREE_TYPE (t1),
892 TREE_CHAIN (TYPE_ARG_TYPES (t1)));
893 t2 = build_function_type (TREE_TYPE (t2),
894 TREE_CHAIN (TYPE_ARG_TYPES (t2)));
895 t3 = merge_types (t1, t2);
896 t3 = build_method_type_directly (basetype, TREE_TYPE (t3),
897 TYPE_ARG_TYPES (t3));
898 t1 = build_cp_fntype_variant (t3, rqual, raises, late_return_type_1_p);
899 break;
900 }
901
902 case TYPENAME_TYPE:
903 /* There is no need to merge attributes into a TYPENAME_TYPE.
904 When the type is instantiated it will have whatever
905 attributes result from the instantiation. */
906 return t1;
907
908 default:;
909 if (attribute_list_equal (TYPE_ATTRIBUTES (t1), attributes))
910 return t1;
911 else if (attribute_list_equal (TYPE_ATTRIBUTES (t2), attributes))
912 return t2;
913 break;
914 }
915
916 return cp_build_type_attribute_variant (t1, attributes);
917 }
918
919 /* Return the ARRAY_TYPE type without its domain. */
920
921 tree
922 strip_array_domain (tree type)
923 {
924 tree t2;
925 gcc_assert (TREE_CODE (type) == ARRAY_TYPE);
926 if (TYPE_DOMAIN (type) == NULL_TREE)
927 return type;
928 t2 = build_cplus_array_type (TREE_TYPE (type), NULL_TREE);
929 return cp_build_type_attribute_variant (t2, TYPE_ATTRIBUTES (type));
930 }
931
932 /* Wrapper around cp_common_type that is used by c-common.c and other
933 front end optimizations that remove promotions.
934
935 Return the common type for two arithmetic types T1 and T2 under the
936 usual arithmetic conversions. The default conversions have already
937 been applied, and enumerated types converted to their compatible
938 integer types. */
939
940 tree
941 common_type (tree t1, tree t2)
942 {
943 /* If one type is nonsense, use the other */
944 if (t1 == error_mark_node)
945 return t2;
946 if (t2 == error_mark_node)
947 return t1;
948
949 return cp_common_type (t1, t2);
950 }
951
952 /* Return the common type of two pointer types T1 and T2. This is the
953 type for the result of most arithmetic operations if the operands
954 have the given two types.
955
956 We assume that comp_target_types has already been done and returned
957 nonzero; if that isn't so, this may crash. */
958
959 tree
960 common_pointer_type (tree t1, tree t2)
961 {
962 gcc_assert ((TYPE_PTR_P (t1) && TYPE_PTR_P (t2))
963 || (TYPE_PTRDATAMEM_P (t1) && TYPE_PTRDATAMEM_P (t2))
964 || (TYPE_PTRMEMFUNC_P (t1) && TYPE_PTRMEMFUNC_P (t2)));
965
966 return composite_pointer_type (input_location, t1, t2,
967 error_mark_node, error_mark_node,
968 CPO_CONVERSION, tf_warning_or_error);
969 }
970 \f
971 /* Compare two exception specifier types for exactness or subsetness, if
972 allowed. Returns false for mismatch, true for match (same, or
973 derived and !exact).
974
975 [except.spec] "If a class X ... objects of class X or any class publicly
976 and unambiguously derived from X. Similarly, if a pointer type Y * ...
977 exceptions of type Y * or that are pointers to any type publicly and
978 unambiguously derived from Y. Otherwise a function only allows exceptions
979 that have the same type ..."
980 This does not mention cv qualifiers and is different to what throw
981 [except.throw] and catch [except.catch] will do. They will ignore the
982 top level cv qualifiers, and allow qualifiers in the pointer to class
983 example.
984
985 We implement the letter of the standard. */
986
987 static bool
988 comp_except_types (tree a, tree b, bool exact)
989 {
990 if (same_type_p (a, b))
991 return true;
992 else if (!exact)
993 {
994 if (cp_type_quals (a) || cp_type_quals (b))
995 return false;
996
997 if (TYPE_PTR_P (a) && TYPE_PTR_P (b))
998 {
999 a = TREE_TYPE (a);
1000 b = TREE_TYPE (b);
1001 if (cp_type_quals (a) || cp_type_quals (b))
1002 return false;
1003 }
1004
1005 if (TREE_CODE (a) != RECORD_TYPE
1006 || TREE_CODE (b) != RECORD_TYPE)
1007 return false;
1008
1009 if (publicly_uniquely_derived_p (a, b))
1010 return true;
1011 }
1012 return false;
1013 }
1014
1015 /* Return true if TYPE1 and TYPE2 are equivalent exception specifiers.
1016 If EXACT is ce_derived, T2 can be stricter than T1 (according to 15.4/5).
1017 If EXACT is ce_type, the C++17 type compatibility rules apply.
1018 If EXACT is ce_normal, the compatibility rules in 15.4/3 apply.
1019 If EXACT is ce_exact, the specs must be exactly the same. Exception lists
1020 are unordered, but we've already filtered out duplicates. Most lists will
1021 be in order, we should try to make use of that. */
1022
1023 bool
1024 comp_except_specs (const_tree t1, const_tree t2, int exact)
1025 {
1026 const_tree probe;
1027 const_tree base;
1028 int length = 0;
1029
1030 if (t1 == t2)
1031 return true;
1032
1033 /* First handle noexcept. */
1034 if (exact < ce_exact)
1035 {
1036 if (exact == ce_type
1037 && (canonical_eh_spec (CONST_CAST_TREE (t1))
1038 == canonical_eh_spec (CONST_CAST_TREE (t2))))
1039 return true;
1040
1041 /* noexcept(false) is compatible with no exception-specification,
1042 and less strict than any spec. */
1043 if (t1 == noexcept_false_spec)
1044 return t2 == NULL_TREE || exact == ce_derived;
1045 /* Even a derived noexcept(false) is compatible with no
1046 exception-specification. */
1047 if (t2 == noexcept_false_spec)
1048 return t1 == NULL_TREE;
1049
1050 /* Otherwise, if we aren't looking for an exact match, noexcept is
1051 equivalent to throw(). */
1052 if (t1 == noexcept_true_spec)
1053 t1 = empty_except_spec;
1054 if (t2 == noexcept_true_spec)
1055 t2 = empty_except_spec;
1056 }
1057
1058 /* If any noexcept is left, it is only comparable to itself;
1059 either we're looking for an exact match or we're redeclaring a
1060 template with dependent noexcept. */
1061 if ((t1 && TREE_PURPOSE (t1))
1062 || (t2 && TREE_PURPOSE (t2)))
1063 return (t1 && t2
1064 && cp_tree_equal (TREE_PURPOSE (t1), TREE_PURPOSE (t2)));
1065
1066 if (t1 == NULL_TREE) /* T1 is ... */
1067 return t2 == NULL_TREE || exact == ce_derived;
1068 if (!TREE_VALUE (t1)) /* t1 is EMPTY */
1069 return t2 != NULL_TREE && !TREE_VALUE (t2);
1070 if (t2 == NULL_TREE) /* T2 is ... */
1071 return false;
1072 if (TREE_VALUE (t1) && !TREE_VALUE (t2)) /* T2 is EMPTY, T1 is not */
1073 return exact == ce_derived;
1074
1075 /* Neither set is ... or EMPTY, make sure each part of T2 is in T1.
1076 Count how many we find, to determine exactness. For exact matching and
1077 ordered T1, T2, this is an O(n) operation, otherwise its worst case is
1078 O(nm). */
1079 for (base = t1; t2 != NULL_TREE; t2 = TREE_CHAIN (t2))
1080 {
1081 for (probe = base; probe != NULL_TREE; probe = TREE_CHAIN (probe))
1082 {
1083 tree a = TREE_VALUE (probe);
1084 tree b = TREE_VALUE (t2);
1085
1086 if (comp_except_types (a, b, exact))
1087 {
1088 if (probe == base && exact > ce_derived)
1089 base = TREE_CHAIN (probe);
1090 length++;
1091 break;
1092 }
1093 }
1094 if (probe == NULL_TREE)
1095 return false;
1096 }
1097 return exact == ce_derived || base == NULL_TREE || length == list_length (t1);
1098 }
1099
1100 /* Compare the array types T1 and T2. CB says how we should behave when
1101 comparing array bounds: bounds_none doesn't allow dimensionless arrays,
1102 bounds_either says than any array can be [], bounds_first means that
1103 onlt T1 can be an array with unknown bounds. STRICT is true if
1104 qualifiers must match when comparing the types of the array elements. */
1105
1106 static bool
1107 comp_array_types (const_tree t1, const_tree t2, compare_bounds_t cb,
1108 bool strict)
1109 {
1110 tree d1;
1111 tree d2;
1112 tree max1, max2;
1113
1114 if (t1 == t2)
1115 return true;
1116
1117 /* The type of the array elements must be the same. */
1118 if (strict
1119 ? !same_type_p (TREE_TYPE (t1), TREE_TYPE (t2))
1120 : !similar_type_p (TREE_TYPE (t1), TREE_TYPE (t2)))
1121 return false;
1122
1123 d1 = TYPE_DOMAIN (t1);
1124 d2 = TYPE_DOMAIN (t2);
1125
1126 if (d1 == d2)
1127 return true;
1128
1129 /* If one of the arrays is dimensionless, and the other has a
1130 dimension, they are of different types. However, it is valid to
1131 write:
1132
1133 extern int a[];
1134 int a[3];
1135
1136 by [basic.link]:
1137
1138 declarations for an array object can specify
1139 array types that differ by the presence or absence of a major
1140 array bound (_dcl.array_). */
1141 if (!d1 && d2)
1142 return cb >= bounds_either;
1143 else if (d1 && !d2)
1144 return cb == bounds_either;
1145
1146 /* Check that the dimensions are the same. */
1147
1148 if (!cp_tree_equal (TYPE_MIN_VALUE (d1), TYPE_MIN_VALUE (d2)))
1149 return false;
1150 max1 = TYPE_MAX_VALUE (d1);
1151 max2 = TYPE_MAX_VALUE (d2);
1152
1153 if (!cp_tree_equal (max1, max2))
1154 return false;
1155
1156 return true;
1157 }
1158
1159 /* Compare the relative position of T1 and T2 into their respective
1160 template parameter list.
1161 T1 and T2 must be template parameter types.
1162 Return TRUE if T1 and T2 have the same position, FALSE otherwise. */
1163
1164 static bool
1165 comp_template_parms_position (tree t1, tree t2)
1166 {
1167 tree index1, index2;
1168 gcc_assert (t1 && t2
1169 && TREE_CODE (t1) == TREE_CODE (t2)
1170 && (TREE_CODE (t1) == BOUND_TEMPLATE_TEMPLATE_PARM
1171 || TREE_CODE (t1) == TEMPLATE_TEMPLATE_PARM
1172 || TREE_CODE (t1) == TEMPLATE_TYPE_PARM));
1173
1174 index1 = TEMPLATE_TYPE_PARM_INDEX (TYPE_MAIN_VARIANT (t1));
1175 index2 = TEMPLATE_TYPE_PARM_INDEX (TYPE_MAIN_VARIANT (t2));
1176
1177 /* Then compare their relative position. */
1178 if (TEMPLATE_PARM_IDX (index1) != TEMPLATE_PARM_IDX (index2)
1179 || TEMPLATE_PARM_LEVEL (index1) != TEMPLATE_PARM_LEVEL (index2)
1180 || (TEMPLATE_PARM_PARAMETER_PACK (index1)
1181 != TEMPLATE_PARM_PARAMETER_PACK (index2)))
1182 return false;
1183
1184 /* In C++14 we can end up comparing 'auto' to a normal template
1185 parameter. Don't confuse them. */
1186 if (cxx_dialect >= cxx14 && (is_auto (t1) || is_auto (t2)))
1187 return TYPE_IDENTIFIER (t1) == TYPE_IDENTIFIER (t2);
1188
1189 return true;
1190 }
1191
1192 /* Heuristic check if two parameter types can be considered ABI-equivalent. */
1193
1194 static bool
1195 cxx_safe_arg_type_equiv_p (tree t1, tree t2)
1196 {
1197 t1 = TYPE_MAIN_VARIANT (t1);
1198 t2 = TYPE_MAIN_VARIANT (t2);
1199
1200 if (TYPE_PTR_P (t1)
1201 && TYPE_PTR_P (t2))
1202 return true;
1203
1204 /* The signedness of the parameter matters only when an integral
1205 type smaller than int is promoted to int, otherwise only the
1206 precision of the parameter matters.
1207 This check should make sure that the callee does not see
1208 undefined values in argument registers. */
1209 if (INTEGRAL_TYPE_P (t1)
1210 && INTEGRAL_TYPE_P (t2)
1211 && TYPE_PRECISION (t1) == TYPE_PRECISION (t2)
1212 && (TYPE_UNSIGNED (t1) == TYPE_UNSIGNED (t2)
1213 || !targetm.calls.promote_prototypes (NULL_TREE)
1214 || TYPE_PRECISION (t1) >= TYPE_PRECISION (integer_type_node)))
1215 return true;
1216
1217 return same_type_p (t1, t2);
1218 }
1219
1220 /* Check if a type cast between two function types can be considered safe. */
1221
1222 static bool
1223 cxx_safe_function_type_cast_p (tree t1, tree t2)
1224 {
1225 if (TREE_TYPE (t1) == void_type_node &&
1226 TYPE_ARG_TYPES (t1) == void_list_node)
1227 return true;
1228
1229 if (TREE_TYPE (t2) == void_type_node &&
1230 TYPE_ARG_TYPES (t2) == void_list_node)
1231 return true;
1232
1233 if (!cxx_safe_arg_type_equiv_p (TREE_TYPE (t1), TREE_TYPE (t2)))
1234 return false;
1235
1236 for (t1 = TYPE_ARG_TYPES (t1), t2 = TYPE_ARG_TYPES (t2);
1237 t1 && t2;
1238 t1 = TREE_CHAIN (t1), t2 = TREE_CHAIN (t2))
1239 if (!cxx_safe_arg_type_equiv_p (TREE_VALUE (t1), TREE_VALUE (t2)))
1240 return false;
1241
1242 return true;
1243 }
1244
1245 /* Subroutine in comptypes. */
1246
1247 static bool
1248 structural_comptypes (tree t1, tree t2, int strict)
1249 {
1250 if (t1 == t2)
1251 return true;
1252
1253 /* Suppress errors caused by previously reported errors. */
1254 if (t1 == error_mark_node || t2 == error_mark_node)
1255 return false;
1256
1257 gcc_assert (TYPE_P (t1) && TYPE_P (t2));
1258
1259 if (!comparing_specializations)
1260 {
1261 /* TYPENAME_TYPEs should be resolved if the qualifying scope is the
1262 current instantiation. */
1263 if (TREE_CODE (t1) == TYPENAME_TYPE)
1264 t1 = resolve_typename_type (t1, /*only_current_p=*/true);
1265
1266 if (TREE_CODE (t2) == TYPENAME_TYPE)
1267 t2 = resolve_typename_type (t2, /*only_current_p=*/true);
1268 }
1269
1270 if (TYPE_PTRMEMFUNC_P (t1))
1271 t1 = TYPE_PTRMEMFUNC_FN_TYPE (t1);
1272 if (TYPE_PTRMEMFUNC_P (t2))
1273 t2 = TYPE_PTRMEMFUNC_FN_TYPE (t2);
1274
1275 /* Different classes of types can't be compatible. */
1276 if (TREE_CODE (t1) != TREE_CODE (t2))
1277 return false;
1278
1279 /* Qualifiers must match. For array types, we will check when we
1280 recur on the array element types. */
1281 if (TREE_CODE (t1) != ARRAY_TYPE
1282 && cp_type_quals (t1) != cp_type_quals (t2))
1283 return false;
1284 if (TREE_CODE (t1) == FUNCTION_TYPE
1285 && type_memfn_quals (t1) != type_memfn_quals (t2))
1286 return false;
1287 /* Need to check this before TYPE_MAIN_VARIANT.
1288 FIXME function qualifiers should really change the main variant. */
1289 if (FUNC_OR_METHOD_TYPE_P (t1))
1290 {
1291 if (type_memfn_rqual (t1) != type_memfn_rqual (t2))
1292 return false;
1293 if (flag_noexcept_type
1294 && !comp_except_specs (TYPE_RAISES_EXCEPTIONS (t1),
1295 TYPE_RAISES_EXCEPTIONS (t2),
1296 ce_type))
1297 return false;
1298 }
1299
1300 /* Allow for two different type nodes which have essentially the same
1301 definition. Note that we already checked for equality of the type
1302 qualifiers (just above). */
1303
1304 if (TREE_CODE (t1) != ARRAY_TYPE
1305 && TYPE_MAIN_VARIANT (t1) == TYPE_MAIN_VARIANT (t2))
1306 return true;
1307
1308
1309 /* Compare the types. Break out if they could be the same. */
1310 switch (TREE_CODE (t1))
1311 {
1312 case VOID_TYPE:
1313 case BOOLEAN_TYPE:
1314 /* All void and bool types are the same. */
1315 break;
1316
1317 case INTEGER_TYPE:
1318 case FIXED_POINT_TYPE:
1319 case REAL_TYPE:
1320 /* With these nodes, we can't determine type equivalence by
1321 looking at what is stored in the nodes themselves, because
1322 two nodes might have different TYPE_MAIN_VARIANTs but still
1323 represent the same type. For example, wchar_t and int could
1324 have the same properties (TYPE_PRECISION, TYPE_MIN_VALUE,
1325 TYPE_MAX_VALUE, etc.), but have different TYPE_MAIN_VARIANTs
1326 and are distinct types. On the other hand, int and the
1327 following typedef
1328
1329 typedef int INT __attribute((may_alias));
1330
1331 have identical properties, different TYPE_MAIN_VARIANTs, but
1332 represent the same type. The canonical type system keeps
1333 track of equivalence in this case, so we fall back on it. */
1334 return TYPE_CANONICAL (t1) == TYPE_CANONICAL (t2);
1335
1336 case TEMPLATE_TEMPLATE_PARM:
1337 case BOUND_TEMPLATE_TEMPLATE_PARM:
1338 if (!comp_template_parms_position (t1, t2))
1339 return false;
1340 if (!comp_template_parms
1341 (DECL_TEMPLATE_PARMS (TEMPLATE_TEMPLATE_PARM_TEMPLATE_DECL (t1)),
1342 DECL_TEMPLATE_PARMS (TEMPLATE_TEMPLATE_PARM_TEMPLATE_DECL (t2))))
1343 return false;
1344 if (TREE_CODE (t1) == TEMPLATE_TEMPLATE_PARM)
1345 break;
1346 /* Don't check inheritance. */
1347 strict = COMPARE_STRICT;
1348 /* Fall through. */
1349
1350 case RECORD_TYPE:
1351 case UNION_TYPE:
1352 if (TYPE_TEMPLATE_INFO (t1) && TYPE_TEMPLATE_INFO (t2)
1353 && (TYPE_TI_TEMPLATE (t1) == TYPE_TI_TEMPLATE (t2)
1354 || TREE_CODE (t1) == BOUND_TEMPLATE_TEMPLATE_PARM)
1355 && comp_template_args (TYPE_TI_ARGS (t1), TYPE_TI_ARGS (t2)))
1356 break;
1357
1358 if ((strict & COMPARE_BASE) && DERIVED_FROM_P (t1, t2))
1359 break;
1360 else if ((strict & COMPARE_DERIVED) && DERIVED_FROM_P (t2, t1))
1361 break;
1362
1363 return false;
1364
1365 case OFFSET_TYPE:
1366 if (!comptypes (TYPE_OFFSET_BASETYPE (t1), TYPE_OFFSET_BASETYPE (t2),
1367 strict & ~COMPARE_REDECLARATION))
1368 return false;
1369 if (!same_type_p (TREE_TYPE (t1), TREE_TYPE (t2)))
1370 return false;
1371 break;
1372
1373 case REFERENCE_TYPE:
1374 if (TYPE_REF_IS_RVALUE (t1) != TYPE_REF_IS_RVALUE (t2))
1375 return false;
1376 /* fall through to checks for pointer types */
1377 gcc_fallthrough ();
1378
1379 case POINTER_TYPE:
1380 if (TYPE_MODE (t1) != TYPE_MODE (t2)
1381 || !same_type_p (TREE_TYPE (t1), TREE_TYPE (t2)))
1382 return false;
1383 break;
1384
1385 case METHOD_TYPE:
1386 case FUNCTION_TYPE:
1387 if (!same_type_p (TREE_TYPE (t1), TREE_TYPE (t2)))
1388 return false;
1389 if (!compparms (TYPE_ARG_TYPES (t1), TYPE_ARG_TYPES (t2)))
1390 return false;
1391 break;
1392
1393 case ARRAY_TYPE:
1394 /* Target types must match incl. qualifiers. */
1395 if (!comp_array_types (t1, t2, ((strict & COMPARE_REDECLARATION)
1396 ? bounds_either : bounds_none),
1397 /*strict=*/true))
1398 return false;
1399 break;
1400
1401 case TEMPLATE_TYPE_PARM:
1402 /* If T1 and T2 don't have the same relative position in their
1403 template parameters set, they can't be equal. */
1404 if (!comp_template_parms_position (t1, t2))
1405 return false;
1406 /* If T1 and T2 don't represent the same class template deduction,
1407 they aren't equal. */
1408 if (CLASS_PLACEHOLDER_TEMPLATE (t1)
1409 != CLASS_PLACEHOLDER_TEMPLATE (t2))
1410 return false;
1411 /* Constrained 'auto's are distinct from parms that don't have the same
1412 constraints. */
1413 if (!equivalent_placeholder_constraints (t1, t2))
1414 return false;
1415 break;
1416
1417 case TYPENAME_TYPE:
1418 if (!cp_tree_equal (TYPENAME_TYPE_FULLNAME (t1),
1419 TYPENAME_TYPE_FULLNAME (t2)))
1420 return false;
1421 /* Qualifiers don't matter on scopes. */
1422 if (!same_type_ignoring_top_level_qualifiers_p (TYPE_CONTEXT (t1),
1423 TYPE_CONTEXT (t2)))
1424 return false;
1425 break;
1426
1427 case UNBOUND_CLASS_TEMPLATE:
1428 if (!cp_tree_equal (TYPE_IDENTIFIER (t1), TYPE_IDENTIFIER (t2)))
1429 return false;
1430 if (!same_type_p (TYPE_CONTEXT (t1), TYPE_CONTEXT (t2)))
1431 return false;
1432 break;
1433
1434 case COMPLEX_TYPE:
1435 if (!same_type_p (TREE_TYPE (t1), TREE_TYPE (t2)))
1436 return false;
1437 break;
1438
1439 case VECTOR_TYPE:
1440 if (gnu_vector_type_p (t1) != gnu_vector_type_p (t2)
1441 || maybe_ne (TYPE_VECTOR_SUBPARTS (t1), TYPE_VECTOR_SUBPARTS (t2))
1442 || !same_type_p (TREE_TYPE (t1), TREE_TYPE (t2)))
1443 return false;
1444 break;
1445
1446 case TYPE_PACK_EXPANSION:
1447 return (same_type_p (PACK_EXPANSION_PATTERN (t1),
1448 PACK_EXPANSION_PATTERN (t2))
1449 && comp_template_args (PACK_EXPANSION_EXTRA_ARGS (t1),
1450 PACK_EXPANSION_EXTRA_ARGS (t2)));
1451
1452 case DECLTYPE_TYPE:
1453 if (DECLTYPE_TYPE_ID_EXPR_OR_MEMBER_ACCESS_P (t1)
1454 != DECLTYPE_TYPE_ID_EXPR_OR_MEMBER_ACCESS_P (t2)
1455 || (DECLTYPE_FOR_LAMBDA_CAPTURE (t1)
1456 != DECLTYPE_FOR_LAMBDA_CAPTURE (t2))
1457 || (DECLTYPE_FOR_LAMBDA_PROXY (t1)
1458 != DECLTYPE_FOR_LAMBDA_PROXY (t2))
1459 || !cp_tree_equal (DECLTYPE_TYPE_EXPR (t1),
1460 DECLTYPE_TYPE_EXPR (t2)))
1461 return false;
1462 break;
1463
1464 case UNDERLYING_TYPE:
1465 return same_type_p (UNDERLYING_TYPE_TYPE (t1),
1466 UNDERLYING_TYPE_TYPE (t2));
1467
1468 default:
1469 return false;
1470 }
1471
1472 /* Don't treat an alias template specialization with dependent
1473 arguments as equivalent to its underlying type when used as a
1474 template argument; we need them to be distinct so that we
1475 substitute into the specialization arguments at instantiation
1476 time. And aliases can't be equivalent without being ==, so
1477 we don't need to look any deeper. */
1478 if (comparing_specializations)
1479 {
1480 tree dep1 = dependent_alias_template_spec_p (t1, nt_transparent);
1481 tree dep2 = dependent_alias_template_spec_p (t2, nt_transparent);
1482 if ((dep1 || dep2) && dep1 != dep2)
1483 return false;
1484 }
1485
1486 /* If we get here, we know that from a target independent POV the
1487 types are the same. Make sure the target attributes are also
1488 the same. */
1489 return comp_type_attributes (t1, t2);
1490 }
1491
1492 /* Return true if T1 and T2 are related as allowed by STRICT. STRICT
1493 is a bitwise-or of the COMPARE_* flags. */
1494
1495 bool
1496 comptypes (tree t1, tree t2, int strict)
1497 {
1498 gcc_checking_assert (t1 && t2);
1499
1500 /* TYPE_ARGUMENT_PACKS are not really types. */
1501 gcc_checking_assert (TREE_CODE (t1) != TYPE_ARGUMENT_PACK
1502 && TREE_CODE (t2) != TYPE_ARGUMENT_PACK);
1503
1504 if (strict == COMPARE_STRICT && comparing_specializations
1505 && (t1 != TYPE_CANONICAL (t1) || t2 != TYPE_CANONICAL (t2)))
1506 /* If comparing_specializations, treat dependent aliases as distinct. */
1507 strict = COMPARE_STRUCTURAL;
1508
1509 if (strict == COMPARE_STRICT)
1510 {
1511 if (t1 == t2)
1512 return true;
1513
1514 if (t1 == error_mark_node || t2 == error_mark_node)
1515 return false;
1516
1517 if (TYPE_STRUCTURAL_EQUALITY_P (t1) || TYPE_STRUCTURAL_EQUALITY_P (t2))
1518 /* At least one of the types requires structural equality, so
1519 perform a deep check. */
1520 return structural_comptypes (t1, t2, strict);
1521
1522 if (flag_checking && param_use_canonical_types)
1523 {
1524 bool result = structural_comptypes (t1, t2, strict);
1525
1526 if (result && TYPE_CANONICAL (t1) != TYPE_CANONICAL (t2))
1527 /* The two types are structurally equivalent, but their
1528 canonical types were different. This is a failure of the
1529 canonical type propagation code.*/
1530 internal_error
1531 ("canonical types differ for identical types %qT and %qT",
1532 t1, t2);
1533 else if (!result && TYPE_CANONICAL (t1) == TYPE_CANONICAL (t2))
1534 /* Two types are structurally different, but the canonical
1535 types are the same. This means we were over-eager in
1536 assigning canonical types. */
1537 internal_error
1538 ("same canonical type node for different types %qT and %qT",
1539 t1, t2);
1540
1541 return result;
1542 }
1543 if (!flag_checking && param_use_canonical_types)
1544 return TYPE_CANONICAL (t1) == TYPE_CANONICAL (t2);
1545 else
1546 return structural_comptypes (t1, t2, strict);
1547 }
1548 else if (strict == COMPARE_STRUCTURAL)
1549 return structural_comptypes (t1, t2, COMPARE_STRICT);
1550 else
1551 return structural_comptypes (t1, t2, strict);
1552 }
1553
1554 /* Returns nonzero iff TYPE1 and TYPE2 are the same type, ignoring
1555 top-level qualifiers. */
1556
1557 bool
1558 same_type_ignoring_top_level_qualifiers_p (tree type1, tree type2)
1559 {
1560 if (type1 == error_mark_node || type2 == error_mark_node)
1561 return false;
1562 if (type1 == type2)
1563 return true;
1564
1565 type1 = cp_build_qualified_type (type1, TYPE_UNQUALIFIED);
1566 type2 = cp_build_qualified_type (type2, TYPE_UNQUALIFIED);
1567 return same_type_p (type1, type2);
1568 }
1569
1570 /* Returns nonzero iff TYPE1 and TYPE2 are similar, as per [conv.qual]. */
1571
1572 bool
1573 similar_type_p (tree type1, tree type2)
1574 {
1575 if (type1 == error_mark_node || type2 == error_mark_node)
1576 return false;
1577
1578 /* Informally, two types are similar if, ignoring top-level cv-qualification:
1579 * they are the same type; or
1580 * they are both pointers, and the pointed-to types are similar; or
1581 * they are both pointers to member of the same class, and the types of
1582 the pointed-to members are similar; or
1583 * they are both arrays of the same size or both arrays of unknown bound,
1584 and the array element types are similar. */
1585
1586 if (same_type_ignoring_top_level_qualifiers_p (type1, type2))
1587 return true;
1588
1589 if ((TYPE_PTR_P (type1) && TYPE_PTR_P (type2))
1590 || (TYPE_PTRDATAMEM_P (type1) && TYPE_PTRDATAMEM_P (type2))
1591 || (TREE_CODE (type1) == ARRAY_TYPE && TREE_CODE (type2) == ARRAY_TYPE))
1592 return comp_ptr_ttypes_const (type1, type2, bounds_either);
1593
1594 return false;
1595 }
1596
1597 /* Returns 1 if TYPE1 is at least as qualified as TYPE2. */
1598
1599 bool
1600 at_least_as_qualified_p (const_tree type1, const_tree type2)
1601 {
1602 int q1 = cp_type_quals (type1);
1603 int q2 = cp_type_quals (type2);
1604
1605 /* All qualifiers for TYPE2 must also appear in TYPE1. */
1606 return (q1 & q2) == q2;
1607 }
1608
1609 /* Returns 1 if TYPE1 is more cv-qualified than TYPE2, -1 if TYPE2 is
1610 more cv-qualified that TYPE1, and 0 otherwise. */
1611
1612 int
1613 comp_cv_qualification (int q1, int q2)
1614 {
1615 if (q1 == q2)
1616 return 0;
1617
1618 if ((q1 & q2) == q2)
1619 return 1;
1620 else if ((q1 & q2) == q1)
1621 return -1;
1622
1623 return 0;
1624 }
1625
1626 int
1627 comp_cv_qualification (const_tree type1, const_tree type2)
1628 {
1629 int q1 = cp_type_quals (type1);
1630 int q2 = cp_type_quals (type2);
1631 return comp_cv_qualification (q1, q2);
1632 }
1633
1634 /* Returns 1 if the cv-qualification signature of TYPE1 is a proper
1635 subset of the cv-qualification signature of TYPE2, and the types
1636 are similar. Returns -1 if the other way 'round, and 0 otherwise. */
1637
1638 int
1639 comp_cv_qual_signature (tree type1, tree type2)
1640 {
1641 if (comp_ptr_ttypes_real (type2, type1, -1))
1642 return 1;
1643 else if (comp_ptr_ttypes_real (type1, type2, -1))
1644 return -1;
1645 else
1646 return 0;
1647 }
1648 \f
1649 /* Subroutines of `comptypes'. */
1650
1651 /* Return true if two parameter type lists PARMS1 and PARMS2 are
1652 equivalent in the sense that functions with those parameter types
1653 can have equivalent types. The two lists must be equivalent,
1654 element by element. */
1655
1656 bool
1657 compparms (const_tree parms1, const_tree parms2)
1658 {
1659 const_tree t1, t2;
1660
1661 /* An unspecified parmlist matches any specified parmlist
1662 whose argument types don't need default promotions. */
1663
1664 for (t1 = parms1, t2 = parms2;
1665 t1 || t2;
1666 t1 = TREE_CHAIN (t1), t2 = TREE_CHAIN (t2))
1667 {
1668 /* If one parmlist is shorter than the other,
1669 they fail to match. */
1670 if (!t1 || !t2)
1671 return false;
1672 if (!same_type_p (TREE_VALUE (t1), TREE_VALUE (t2)))
1673 return false;
1674 }
1675 return true;
1676 }
1677
1678 \f
1679 /* Process a sizeof or alignof expression where the operand is a
1680 type. STD_ALIGNOF indicates whether an alignof has C++11 (minimum alignment)
1681 or GNU (preferred alignment) semantics; it is ignored if op is
1682 SIZEOF_EXPR. */
1683
1684 tree
1685 cxx_sizeof_or_alignof_type (location_t loc, tree type, enum tree_code op,
1686 bool std_alignof, bool complain)
1687 {
1688 gcc_assert (op == SIZEOF_EXPR || op == ALIGNOF_EXPR);
1689 if (type == error_mark_node)
1690 return error_mark_node;
1691
1692 type = non_reference (type);
1693 if (TREE_CODE (type) == METHOD_TYPE)
1694 {
1695 if (complain)
1696 {
1697 pedwarn (loc, OPT_Wpointer_arith,
1698 "invalid application of %qs to a member function",
1699 OVL_OP_INFO (false, op)->name);
1700 return size_one_node;
1701 }
1702 else
1703 return error_mark_node;
1704 }
1705
1706 bool dependent_p = dependent_type_p (type);
1707 if (!dependent_p)
1708 complete_type (type);
1709 if (dependent_p
1710 /* VLA types will have a non-constant size. In the body of an
1711 uninstantiated template, we don't need to try to compute the
1712 value, because the sizeof expression is not an integral
1713 constant expression in that case. And, if we do try to
1714 compute the value, we'll likely end up with SAVE_EXPRs, which
1715 the template substitution machinery does not expect to see. */
1716 || (processing_template_decl
1717 && COMPLETE_TYPE_P (type)
1718 && TREE_CODE (TYPE_SIZE (type)) != INTEGER_CST))
1719 {
1720 tree value = build_min (op, size_type_node, type);
1721 TREE_READONLY (value) = 1;
1722 if (op == ALIGNOF_EXPR && std_alignof)
1723 ALIGNOF_EXPR_STD_P (value) = true;
1724 SET_EXPR_LOCATION (value, loc);
1725 return value;
1726 }
1727
1728 return c_sizeof_or_alignof_type (loc, complete_type (type),
1729 op == SIZEOF_EXPR, std_alignof,
1730 complain);
1731 }
1732
1733 /* Return the size of the type, without producing any warnings for
1734 types whose size cannot be taken. This routine should be used only
1735 in some other routine that has already produced a diagnostic about
1736 using the size of such a type. */
1737 tree
1738 cxx_sizeof_nowarn (tree type)
1739 {
1740 if (TREE_CODE (type) == FUNCTION_TYPE
1741 || VOID_TYPE_P (type)
1742 || TREE_CODE (type) == ERROR_MARK)
1743 return size_one_node;
1744 else if (!COMPLETE_TYPE_P (type))
1745 return size_zero_node;
1746 else
1747 return cxx_sizeof_or_alignof_type (input_location, type,
1748 SIZEOF_EXPR, false, false);
1749 }
1750
1751 /* Process a sizeof expression where the operand is an expression. */
1752
1753 static tree
1754 cxx_sizeof_expr (location_t loc, tree e, tsubst_flags_t complain)
1755 {
1756 if (e == error_mark_node)
1757 return error_mark_node;
1758
1759 if (instantiation_dependent_uneval_expression_p (e))
1760 {
1761 e = build_min (SIZEOF_EXPR, size_type_node, e);
1762 TREE_SIDE_EFFECTS (e) = 0;
1763 TREE_READONLY (e) = 1;
1764 SET_EXPR_LOCATION (e, loc);
1765
1766 return e;
1767 }
1768
1769 location_t e_loc = cp_expr_loc_or_loc (e, loc);
1770 STRIP_ANY_LOCATION_WRAPPER (e);
1771
1772 /* To get the size of a static data member declared as an array of
1773 unknown bound, we need to instantiate it. */
1774 if (VAR_P (e)
1775 && VAR_HAD_UNKNOWN_BOUND (e)
1776 && DECL_TEMPLATE_INSTANTIATION (e))
1777 instantiate_decl (e, /*defer_ok*/true, /*expl_inst_mem*/false);
1778
1779 if (TREE_CODE (e) == PARM_DECL
1780 && DECL_ARRAY_PARAMETER_P (e)
1781 && (complain & tf_warning))
1782 {
1783 auto_diagnostic_group d;
1784 if (warning_at (e_loc, OPT_Wsizeof_array_argument,
1785 "%<sizeof%> on array function parameter %qE "
1786 "will return size of %qT", e, TREE_TYPE (e)))
1787 inform (DECL_SOURCE_LOCATION (e), "declared here");
1788 }
1789
1790 e = mark_type_use (e);
1791
1792 if (bitfield_p (e))
1793 {
1794 if (complain & tf_error)
1795 error_at (e_loc,
1796 "invalid application of %<sizeof%> to a bit-field");
1797 else
1798 return error_mark_node;
1799 e = char_type_node;
1800 }
1801 else if (is_overloaded_fn (e))
1802 {
1803 if (complain & tf_error)
1804 permerror (e_loc, "ISO C++ forbids applying %<sizeof%> to "
1805 "an expression of function type");
1806 else
1807 return error_mark_node;
1808 e = char_type_node;
1809 }
1810 else if (type_unknown_p (e))
1811 {
1812 if (complain & tf_error)
1813 cxx_incomplete_type_error (e_loc, e, TREE_TYPE (e));
1814 else
1815 return error_mark_node;
1816 e = char_type_node;
1817 }
1818 else
1819 e = TREE_TYPE (e);
1820
1821 return cxx_sizeof_or_alignof_type (loc, e, SIZEOF_EXPR, false,
1822 complain & tf_error);
1823 }
1824
1825 /* Implement the __alignof keyword: Return the minimum required
1826 alignment of E, measured in bytes. For VAR_DECL's and
1827 FIELD_DECL's return DECL_ALIGN (which can be set from an
1828 "aligned" __attribute__ specification). */
1829
1830 static tree
1831 cxx_alignof_expr (location_t loc, tree e, tsubst_flags_t complain)
1832 {
1833 tree t;
1834
1835 if (e == error_mark_node)
1836 return error_mark_node;
1837
1838 if (processing_template_decl)
1839 {
1840 e = build_min (ALIGNOF_EXPR, size_type_node, e);
1841 TREE_SIDE_EFFECTS (e) = 0;
1842 TREE_READONLY (e) = 1;
1843 SET_EXPR_LOCATION (e, loc);
1844
1845 return e;
1846 }
1847
1848 location_t e_loc = cp_expr_loc_or_loc (e, loc);
1849 STRIP_ANY_LOCATION_WRAPPER (e);
1850
1851 e = mark_type_use (e);
1852
1853 if (!verify_type_context (loc, TCTX_ALIGNOF, TREE_TYPE (e),
1854 !(complain & tf_error)))
1855 {
1856 if (!(complain & tf_error))
1857 return error_mark_node;
1858 t = size_one_node;
1859 }
1860 else if (VAR_P (e))
1861 t = size_int (DECL_ALIGN_UNIT (e));
1862 else if (bitfield_p (e))
1863 {
1864 if (complain & tf_error)
1865 error_at (e_loc,
1866 "invalid application of %<__alignof%> to a bit-field");
1867 else
1868 return error_mark_node;
1869 t = size_one_node;
1870 }
1871 else if (TREE_CODE (e) == COMPONENT_REF
1872 && TREE_CODE (TREE_OPERAND (e, 1)) == FIELD_DECL)
1873 t = size_int (DECL_ALIGN_UNIT (TREE_OPERAND (e, 1)));
1874 else if (is_overloaded_fn (e))
1875 {
1876 if (complain & tf_error)
1877 permerror (e_loc, "ISO C++ forbids applying %<__alignof%> to "
1878 "an expression of function type");
1879 else
1880 return error_mark_node;
1881 if (TREE_CODE (e) == FUNCTION_DECL)
1882 t = size_int (DECL_ALIGN_UNIT (e));
1883 else
1884 t = size_one_node;
1885 }
1886 else if (type_unknown_p (e))
1887 {
1888 if (complain & tf_error)
1889 cxx_incomplete_type_error (e_loc, e, TREE_TYPE (e));
1890 else
1891 return error_mark_node;
1892 t = size_one_node;
1893 }
1894 else
1895 return cxx_sizeof_or_alignof_type (loc, TREE_TYPE (e),
1896 ALIGNOF_EXPR, false,
1897 complain & tf_error);
1898
1899 return fold_convert_loc (loc, size_type_node, t);
1900 }
1901
1902 /* Process a sizeof or alignof expression E with code OP where the operand
1903 is an expression. */
1904
1905 tree
1906 cxx_sizeof_or_alignof_expr (location_t loc, tree e, enum tree_code op,
1907 bool complain)
1908 {
1909 if (op == SIZEOF_EXPR)
1910 return cxx_sizeof_expr (loc, e, complain? tf_warning_or_error : tf_none);
1911 else
1912 return cxx_alignof_expr (loc, e, complain? tf_warning_or_error : tf_none);
1913 }
1914
1915 /* Build a representation of an expression 'alignas(E).' Return the
1916 folded integer value of E if it is an integral constant expression
1917 that resolves to a valid alignment. If E depends on a template
1918 parameter, return a syntactic representation tree of kind
1919 ALIGNOF_EXPR. Otherwise, return an error_mark_node if the
1920 expression is ill formed, or NULL_TREE if E is NULL_TREE. */
1921
1922 tree
1923 cxx_alignas_expr (tree e)
1924 {
1925 if (e == NULL_TREE || e == error_mark_node
1926 || (!TYPE_P (e) && !require_potential_rvalue_constant_expression (e)))
1927 return e;
1928
1929 if (TYPE_P (e))
1930 /* [dcl.align]/3:
1931
1932 When the alignment-specifier is of the form
1933 alignas(type-id ), it shall have the same effect as
1934 alignas(alignof(type-id )). */
1935
1936 return cxx_sizeof_or_alignof_type (input_location,
1937 e, ALIGNOF_EXPR, true, false);
1938
1939 /* If we reach this point, it means the alignas expression if of
1940 the form "alignas(assignment-expression)", so we should follow
1941 what is stated by [dcl.align]/2. */
1942
1943 if (value_dependent_expression_p (e))
1944 /* Leave value-dependent expression alone for now. */
1945 return e;
1946
1947 e = instantiate_non_dependent_expr (e);
1948 e = mark_rvalue_use (e);
1949
1950 /* [dcl.align]/2 says:
1951
1952 the assignment-expression shall be an integral constant
1953 expression. */
1954
1955 if (!INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (TREE_TYPE (e)))
1956 {
1957 error ("%<alignas%> argument has non-integral type %qT", TREE_TYPE (e));
1958 return error_mark_node;
1959 }
1960
1961 return cxx_constant_value (e);
1962 }
1963
1964 \f
1965 /* EXPR is being used in a context that is not a function call.
1966 Enforce:
1967
1968 [expr.ref]
1969
1970 The expression can be used only as the left-hand operand of a
1971 member function call.
1972
1973 [expr.mptr.operator]
1974
1975 If the result of .* or ->* is a function, then that result can be
1976 used only as the operand for the function call operator ().
1977
1978 by issuing an error message if appropriate. Returns true iff EXPR
1979 violates these rules. */
1980
1981 bool
1982 invalid_nonstatic_memfn_p (location_t loc, tree expr, tsubst_flags_t complain)
1983 {
1984 if (expr == NULL_TREE)
1985 return false;
1986 /* Don't enforce this in MS mode. */
1987 if (flag_ms_extensions)
1988 return false;
1989 if (is_overloaded_fn (expr) && !really_overloaded_fn (expr))
1990 expr = get_first_fn (expr);
1991 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (expr))
1992 {
1993 if (complain & tf_error)
1994 {
1995 if (DECL_P (expr))
1996 {
1997 error_at (loc, "invalid use of non-static member function %qD",
1998 expr);
1999 inform (DECL_SOURCE_LOCATION (expr), "declared here");
2000 }
2001 else
2002 error_at (loc, "invalid use of non-static member function of "
2003 "type %qT", TREE_TYPE (expr));
2004 }
2005 return true;
2006 }
2007 return false;
2008 }
2009
2010 /* If EXP is a reference to a bitfield, and the type of EXP does not
2011 match the declared type of the bitfield, return the declared type
2012 of the bitfield. Otherwise, return NULL_TREE. */
2013
2014 tree
2015 is_bitfield_expr_with_lowered_type (const_tree exp)
2016 {
2017 switch (TREE_CODE (exp))
2018 {
2019 case COND_EXPR:
2020 if (!is_bitfield_expr_with_lowered_type (TREE_OPERAND (exp, 1)
2021 ? TREE_OPERAND (exp, 1)
2022 : TREE_OPERAND (exp, 0)))
2023 return NULL_TREE;
2024 return is_bitfield_expr_with_lowered_type (TREE_OPERAND (exp, 2));
2025
2026 case COMPOUND_EXPR:
2027 return is_bitfield_expr_with_lowered_type (TREE_OPERAND (exp, 1));
2028
2029 case MODIFY_EXPR:
2030 case SAVE_EXPR:
2031 return is_bitfield_expr_with_lowered_type (TREE_OPERAND (exp, 0));
2032
2033 case COMPONENT_REF:
2034 {
2035 tree field;
2036
2037 field = TREE_OPERAND (exp, 1);
2038 if (TREE_CODE (field) != FIELD_DECL || !DECL_BIT_FIELD_TYPE (field))
2039 return NULL_TREE;
2040 if (same_type_ignoring_top_level_qualifiers_p
2041 (TREE_TYPE (exp), DECL_BIT_FIELD_TYPE (field)))
2042 return NULL_TREE;
2043 return DECL_BIT_FIELD_TYPE (field);
2044 }
2045
2046 case VAR_DECL:
2047 if (DECL_HAS_VALUE_EXPR_P (exp))
2048 return is_bitfield_expr_with_lowered_type (DECL_VALUE_EXPR
2049 (CONST_CAST_TREE (exp)));
2050 return NULL_TREE;
2051
2052 case VIEW_CONVERT_EXPR:
2053 if (location_wrapper_p (exp))
2054 return is_bitfield_expr_with_lowered_type (TREE_OPERAND (exp, 0));
2055 else
2056 return NULL_TREE;
2057
2058 default:
2059 return NULL_TREE;
2060 }
2061 }
2062
2063 /* Like is_bitfield_with_lowered_type, except that if EXP is not a
2064 bitfield with a lowered type, the type of EXP is returned, rather
2065 than NULL_TREE. */
2066
2067 tree
2068 unlowered_expr_type (const_tree exp)
2069 {
2070 tree type;
2071 tree etype = TREE_TYPE (exp);
2072
2073 type = is_bitfield_expr_with_lowered_type (exp);
2074 if (type)
2075 type = cp_build_qualified_type (type, cp_type_quals (etype));
2076 else
2077 type = etype;
2078
2079 return type;
2080 }
2081
2082 /* Perform the conversions in [expr] that apply when an lvalue appears
2083 in an rvalue context: the lvalue-to-rvalue, array-to-pointer, and
2084 function-to-pointer conversions. In addition, bitfield references are
2085 converted to their declared types. Note that this function does not perform
2086 the lvalue-to-rvalue conversion for class types. If you need that conversion
2087 for class types, then you probably need to use force_rvalue.
2088
2089 Although the returned value is being used as an rvalue, this
2090 function does not wrap the returned expression in a
2091 NON_LVALUE_EXPR; the caller is expected to be mindful of the fact
2092 that the return value is no longer an lvalue. */
2093
2094 tree
2095 decay_conversion (tree exp,
2096 tsubst_flags_t complain,
2097 bool reject_builtin /* = true */)
2098 {
2099 tree type;
2100 enum tree_code code;
2101 location_t loc = cp_expr_loc_or_input_loc (exp);
2102
2103 type = TREE_TYPE (exp);
2104 if (type == error_mark_node)
2105 return error_mark_node;
2106
2107 exp = resolve_nondeduced_context_or_error (exp, complain);
2108
2109 code = TREE_CODE (type);
2110
2111 if (error_operand_p (exp))
2112 return error_mark_node;
2113
2114 if (NULLPTR_TYPE_P (type) && !TREE_SIDE_EFFECTS (exp))
2115 {
2116 mark_rvalue_use (exp, loc, reject_builtin);
2117 return nullptr_node;
2118 }
2119
2120 /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
2121 Leave such NOP_EXPRs, since RHS is being used in non-lvalue context. */
2122 if (code == VOID_TYPE)
2123 {
2124 if (complain & tf_error)
2125 error_at (loc, "void value not ignored as it ought to be");
2126 return error_mark_node;
2127 }
2128 if (invalid_nonstatic_memfn_p (loc, exp, complain))
2129 return error_mark_node;
2130 if (code == FUNCTION_TYPE || is_overloaded_fn (exp))
2131 {
2132 exp = mark_lvalue_use (exp);
2133 if (reject_builtin && reject_gcc_builtin (exp, loc))
2134 return error_mark_node;
2135 return cp_build_addr_expr (exp, complain);
2136 }
2137 if (code == ARRAY_TYPE)
2138 {
2139 tree adr;
2140 tree ptrtype;
2141
2142 exp = mark_lvalue_use (exp);
2143
2144 if (INDIRECT_REF_P (exp))
2145 return build_nop (build_pointer_type (TREE_TYPE (type)),
2146 TREE_OPERAND (exp, 0));
2147
2148 if (TREE_CODE (exp) == COMPOUND_EXPR)
2149 {
2150 tree op1 = decay_conversion (TREE_OPERAND (exp, 1), complain);
2151 if (op1 == error_mark_node)
2152 return error_mark_node;
2153 return build2 (COMPOUND_EXPR, TREE_TYPE (op1),
2154 TREE_OPERAND (exp, 0), op1);
2155 }
2156
2157 if (!obvalue_p (exp)
2158 && ! (TREE_CODE (exp) == CONSTRUCTOR && TREE_STATIC (exp)))
2159 {
2160 if (complain & tf_error)
2161 error_at (loc, "invalid use of non-lvalue array");
2162 return error_mark_node;
2163 }
2164
2165 /* Don't let an array compound literal decay to a pointer. It can
2166 still be used to initialize an array or bind to a reference. */
2167 if (TREE_CODE (exp) == TARGET_EXPR)
2168 {
2169 if (complain & tf_error)
2170 error_at (loc, "taking address of temporary array");
2171 return error_mark_node;
2172 }
2173
2174 ptrtype = build_pointer_type (TREE_TYPE (type));
2175
2176 if (VAR_P (exp))
2177 {
2178 if (!cxx_mark_addressable (exp))
2179 return error_mark_node;
2180 adr = build_nop (ptrtype, build_address (exp));
2181 return adr;
2182 }
2183 /* This way is better for a COMPONENT_REF since it can
2184 simplify the offset for a component. */
2185 adr = cp_build_addr_expr (exp, complain);
2186 return cp_convert (ptrtype, adr, complain);
2187 }
2188
2189 /* Otherwise, it's the lvalue-to-rvalue conversion. */
2190 exp = mark_rvalue_use (exp, loc, reject_builtin);
2191
2192 /* If a bitfield is used in a context where integral promotion
2193 applies, then the caller is expected to have used
2194 default_conversion. That function promotes bitfields correctly
2195 before calling this function. At this point, if we have a
2196 bitfield referenced, we may assume that is not subject to
2197 promotion, and that, therefore, the type of the resulting rvalue
2198 is the declared type of the bitfield. */
2199 exp = convert_bitfield_to_declared_type (exp);
2200
2201 /* We do not call rvalue() here because we do not want to wrap EXP
2202 in a NON_LVALUE_EXPR. */
2203
2204 /* [basic.lval]
2205
2206 Non-class rvalues always have cv-unqualified types. */
2207 type = TREE_TYPE (exp);
2208 if (!CLASS_TYPE_P (type) && cv_qualified_p (type))
2209 exp = build_nop (cv_unqualified (type), exp);
2210
2211 if (!complete_type_or_maybe_complain (type, exp, complain))
2212 return error_mark_node;
2213
2214 return exp;
2215 }
2216
2217 /* Perform preparatory conversions, as part of the "usual arithmetic
2218 conversions". In particular, as per [expr]:
2219
2220 Whenever an lvalue expression appears as an operand of an
2221 operator that expects the rvalue for that operand, the
2222 lvalue-to-rvalue, array-to-pointer, or function-to-pointer
2223 standard conversions are applied to convert the expression to an
2224 rvalue.
2225
2226 In addition, we perform integral promotions here, as those are
2227 applied to both operands to a binary operator before determining
2228 what additional conversions should apply. */
2229
2230 static tree
2231 cp_default_conversion (tree exp, tsubst_flags_t complain)
2232 {
2233 /* Check for target-specific promotions. */
2234 tree promoted_type = targetm.promoted_type (TREE_TYPE (exp));
2235 if (promoted_type)
2236 exp = cp_convert (promoted_type, exp, complain);
2237 /* Perform the integral promotions first so that bitfield
2238 expressions (which may promote to "int", even if the bitfield is
2239 declared "unsigned") are promoted correctly. */
2240 else if (INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (TREE_TYPE (exp)))
2241 exp = cp_perform_integral_promotions (exp, complain);
2242 /* Perform the other conversions. */
2243 exp = decay_conversion (exp, complain);
2244
2245 return exp;
2246 }
2247
2248 /* C version. */
2249
2250 tree
2251 default_conversion (tree exp)
2252 {
2253 return cp_default_conversion (exp, tf_warning_or_error);
2254 }
2255
2256 /* EXPR is an expression with an integral or enumeration type.
2257 Perform the integral promotions in [conv.prom], and return the
2258 converted value. */
2259
2260 tree
2261 cp_perform_integral_promotions (tree expr, tsubst_flags_t complain)
2262 {
2263 tree type;
2264 tree promoted_type;
2265
2266 expr = mark_rvalue_use (expr);
2267 if (error_operand_p (expr))
2268 return error_mark_node;
2269
2270 type = TREE_TYPE (expr);
2271
2272 /* [conv.prom]
2273
2274 A prvalue for an integral bit-field (11.3.9) can be converted to a prvalue
2275 of type int if int can represent all the values of the bit-field;
2276 otherwise, it can be converted to unsigned int if unsigned int can
2277 represent all the values of the bit-field. If the bit-field is larger yet,
2278 no integral promotion applies to it. If the bit-field has an enumerated
2279 type, it is treated as any other value of that type for promotion
2280 purposes. */
2281 tree bitfield_type = is_bitfield_expr_with_lowered_type (expr);
2282 if (bitfield_type
2283 && (TREE_CODE (bitfield_type) == ENUMERAL_TYPE
2284 || TYPE_PRECISION (type) > TYPE_PRECISION (integer_type_node)))
2285 type = bitfield_type;
2286
2287 gcc_assert (INTEGRAL_OR_ENUMERATION_TYPE_P (type));
2288 /* Scoped enums don't promote. */
2289 if (SCOPED_ENUM_P (type))
2290 return expr;
2291 promoted_type = type_promotes_to (type);
2292 if (type != promoted_type)
2293 expr = cp_convert (promoted_type, expr, complain);
2294 else if (bitfield_type && bitfield_type != type)
2295 /* Prevent decay_conversion from converting to bitfield_type. */
2296 expr = build_nop (type, expr);
2297 return expr;
2298 }
2299
2300 /* C version. */
2301
2302 tree
2303 perform_integral_promotions (tree expr)
2304 {
2305 return cp_perform_integral_promotions (expr, tf_warning_or_error);
2306 }
2307
2308 /* Returns nonzero iff exp is a STRING_CST or the result of applying
2309 decay_conversion to one. */
2310
2311 int
2312 string_conv_p (const_tree totype, const_tree exp, int warn)
2313 {
2314 tree t;
2315
2316 if (!TYPE_PTR_P (totype))
2317 return 0;
2318
2319 t = TREE_TYPE (totype);
2320 if (!same_type_p (t, char_type_node)
2321 && !same_type_p (t, char8_type_node)
2322 && !same_type_p (t, char16_type_node)
2323 && !same_type_p (t, char32_type_node)
2324 && !same_type_p (t, wchar_type_node))
2325 return 0;
2326
2327 location_t loc = EXPR_LOC_OR_LOC (exp, input_location);
2328
2329 STRIP_ANY_LOCATION_WRAPPER (exp);
2330
2331 if (TREE_CODE (exp) == STRING_CST)
2332 {
2333 /* Make sure that we don't try to convert between char and wide chars. */
2334 if (!same_type_p (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (exp))), t))
2335 return 0;
2336 }
2337 else
2338 {
2339 /* Is this a string constant which has decayed to 'const char *'? */
2340 t = build_pointer_type (cp_build_qualified_type (t, TYPE_QUAL_CONST));
2341 if (!same_type_p (TREE_TYPE (exp), t))
2342 return 0;
2343 STRIP_NOPS (exp);
2344 if (TREE_CODE (exp) != ADDR_EXPR
2345 || TREE_CODE (TREE_OPERAND (exp, 0)) != STRING_CST)
2346 return 0;
2347 }
2348 if (warn)
2349 {
2350 if (cxx_dialect >= cxx11)
2351 pedwarn (loc, OPT_Wwrite_strings,
2352 "ISO C++ forbids converting a string constant to %qT",
2353 totype);
2354 else
2355 warning_at (loc, OPT_Wwrite_strings,
2356 "deprecated conversion from string constant to %qT",
2357 totype);
2358 }
2359
2360 return 1;
2361 }
2362
2363 /* Given a COND_EXPR, MIN_EXPR, or MAX_EXPR in T, return it in a form that we
2364 can, for example, use as an lvalue. This code used to be in
2365 unary_complex_lvalue, but we needed it to deal with `a = (d == c) ? b : c'
2366 expressions, where we're dealing with aggregates. But now it's again only
2367 called from unary_complex_lvalue. The case (in particular) that led to
2368 this was with CODE == ADDR_EXPR, since it's not an lvalue when we'd
2369 get it there. */
2370
2371 static tree
2372 rationalize_conditional_expr (enum tree_code code, tree t,
2373 tsubst_flags_t complain)
2374 {
2375 location_t loc = cp_expr_loc_or_input_loc (t);
2376
2377 /* For MIN_EXPR or MAX_EXPR, fold-const.c has arranged things so that
2378 the first operand is always the one to be used if both operands
2379 are equal, so we know what conditional expression this used to be. */
2380 if (TREE_CODE (t) == MIN_EXPR || TREE_CODE (t) == MAX_EXPR)
2381 {
2382 tree op0 = TREE_OPERAND (t, 0);
2383 tree op1 = TREE_OPERAND (t, 1);
2384
2385 /* The following code is incorrect if either operand side-effects. */
2386 gcc_assert (!TREE_SIDE_EFFECTS (op0)
2387 && !TREE_SIDE_EFFECTS (op1));
2388 return
2389 build_conditional_expr (loc,
2390 build_x_binary_op (loc,
2391 (TREE_CODE (t) == MIN_EXPR
2392 ? LE_EXPR : GE_EXPR),
2393 op0, TREE_CODE (op0),
2394 op1, TREE_CODE (op1),
2395 /*overload=*/NULL,
2396 complain),
2397 cp_build_unary_op (code, op0, false, complain),
2398 cp_build_unary_op (code, op1, false, complain),
2399 complain);
2400 }
2401
2402 tree op1 = TREE_OPERAND (t, 1);
2403 if (TREE_CODE (op1) != THROW_EXPR)
2404 op1 = cp_build_unary_op (code, op1, false, complain);
2405 tree op2 = TREE_OPERAND (t, 2);
2406 if (TREE_CODE (op2) != THROW_EXPR)
2407 op2 = cp_build_unary_op (code, op2, false, complain);
2408
2409 return
2410 build_conditional_expr (loc, TREE_OPERAND (t, 0), op1, op2, complain);
2411 }
2412
2413 /* Given the TYPE of an anonymous union field inside T, return the
2414 FIELD_DECL for the field. If not found return NULL_TREE. Because
2415 anonymous unions can nest, we must also search all anonymous unions
2416 that are directly reachable. */
2417
2418 tree
2419 lookup_anon_field (tree t, tree type)
2420 {
2421 tree field;
2422
2423 t = TYPE_MAIN_VARIANT (t);
2424
2425 for (field = TYPE_FIELDS (t); field; field = DECL_CHAIN (field))
2426 {
2427 if (TREE_STATIC (field))
2428 continue;
2429 if (TREE_CODE (field) != FIELD_DECL || DECL_ARTIFICIAL (field))
2430 continue;
2431
2432 /* If we find it directly, return the field. */
2433 if (DECL_NAME (field) == NULL_TREE
2434 && type == TYPE_MAIN_VARIANT (TREE_TYPE (field)))
2435 {
2436 return field;
2437 }
2438
2439 /* Otherwise, it could be nested, search harder. */
2440 if (DECL_NAME (field) == NULL_TREE
2441 && ANON_AGGR_TYPE_P (TREE_TYPE (field)))
2442 {
2443 tree subfield = lookup_anon_field (TREE_TYPE (field), type);
2444 if (subfield)
2445 return subfield;
2446 }
2447 }
2448 return NULL_TREE;
2449 }
2450
2451 /* Build an expression representing OBJECT.MEMBER. OBJECT is an
2452 expression; MEMBER is a DECL or baselink. If ACCESS_PATH is
2453 non-NULL, it indicates the path to the base used to name MEMBER.
2454 If PRESERVE_REFERENCE is true, the expression returned will have
2455 REFERENCE_TYPE if the MEMBER does. Otherwise, the expression
2456 returned will have the type referred to by the reference.
2457
2458 This function does not perform access control; that is either done
2459 earlier by the parser when the name of MEMBER is resolved to MEMBER
2460 itself, or later when overload resolution selects one of the
2461 functions indicated by MEMBER. */
2462
2463 tree
2464 build_class_member_access_expr (cp_expr object, tree member,
2465 tree access_path, bool preserve_reference,
2466 tsubst_flags_t complain)
2467 {
2468 tree object_type;
2469 tree member_scope;
2470 tree result = NULL_TREE;
2471 tree using_decl = NULL_TREE;
2472
2473 if (error_operand_p (object) || error_operand_p (member))
2474 return error_mark_node;
2475
2476 gcc_assert (DECL_P (member) || BASELINK_P (member));
2477
2478 /* [expr.ref]
2479
2480 The type of the first expression shall be "class object" (of a
2481 complete type). */
2482 object_type = TREE_TYPE (object);
2483 if (!currently_open_class (object_type)
2484 && !complete_type_or_maybe_complain (object_type, object, complain))
2485 return error_mark_node;
2486 if (!CLASS_TYPE_P (object_type))
2487 {
2488 if (complain & tf_error)
2489 {
2490 if (INDIRECT_TYPE_P (object_type)
2491 && CLASS_TYPE_P (TREE_TYPE (object_type)))
2492 error ("request for member %qD in %qE, which is of pointer "
2493 "type %qT (maybe you meant to use %<->%> ?)",
2494 member, object.get_value (), object_type);
2495 else
2496 error ("request for member %qD in %qE, which is of non-class "
2497 "type %qT", member, object.get_value (), object_type);
2498 }
2499 return error_mark_node;
2500 }
2501
2502 /* The standard does not seem to actually say that MEMBER must be a
2503 member of OBJECT_TYPE. However, that is clearly what is
2504 intended. */
2505 if (DECL_P (member))
2506 {
2507 member_scope = DECL_CLASS_CONTEXT (member);
2508 if (!mark_used (member, complain) && !(complain & tf_error))
2509 return error_mark_node;
2510 if (TREE_DEPRECATED (member))
2511 warn_deprecated_use (member, NULL_TREE);
2512 }
2513 else
2514 member_scope = BINFO_TYPE (BASELINK_ACCESS_BINFO (member));
2515 /* If MEMBER is from an anonymous aggregate, MEMBER_SCOPE will
2516 presently be the anonymous union. Go outwards until we find a
2517 type related to OBJECT_TYPE. */
2518 while ((ANON_AGGR_TYPE_P (member_scope) || UNSCOPED_ENUM_P (member_scope))
2519 && !same_type_ignoring_top_level_qualifiers_p (member_scope,
2520 object_type))
2521 member_scope = TYPE_CONTEXT (member_scope);
2522 if (!member_scope || !DERIVED_FROM_P (member_scope, object_type))
2523 {
2524 if (complain & tf_error)
2525 {
2526 if (TREE_CODE (member) == FIELD_DECL)
2527 error ("invalid use of non-static data member %qE", member);
2528 else
2529 error ("%qD is not a member of %qT", member, object_type);
2530 }
2531 return error_mark_node;
2532 }
2533
2534 /* Transform `(a, b).x' into `(*(a, &b)).x', `(a ? b : c).x' into
2535 `(*(a ? &b : &c)).x', and so on. A COND_EXPR is only an lvalue
2536 in the front end; only _DECLs and _REFs are lvalues in the back end. */
2537 {
2538 tree temp = unary_complex_lvalue (ADDR_EXPR, object);
2539 if (temp)
2540 {
2541 temp = cp_build_fold_indirect_ref (temp);
2542 if (xvalue_p (object) && !xvalue_p (temp))
2543 /* Preserve xvalue kind. */
2544 temp = move (temp);
2545 object = temp;
2546 }
2547 }
2548
2549 /* In [expr.ref], there is an explicit list of the valid choices for
2550 MEMBER. We check for each of those cases here. */
2551 if (VAR_P (member))
2552 {
2553 /* A static data member. */
2554 result = member;
2555 mark_exp_read (object);
2556
2557 if (tree wrap = maybe_get_tls_wrapper_call (result))
2558 /* Replace an evaluated use of the thread_local variable with
2559 a call to its wrapper. */
2560 result = wrap;
2561
2562 /* If OBJECT has side-effects, they are supposed to occur. */
2563 if (TREE_SIDE_EFFECTS (object))
2564 result = build2 (COMPOUND_EXPR, TREE_TYPE (result), object, result);
2565 }
2566 else if (TREE_CODE (member) == FIELD_DECL)
2567 {
2568 /* A non-static data member. */
2569 bool null_object_p;
2570 int type_quals;
2571 tree member_type;
2572
2573 if (INDIRECT_REF_P (object))
2574 null_object_p =
2575 integer_zerop (tree_strip_nop_conversions (TREE_OPERAND (object, 0)));
2576 else
2577 null_object_p = false;
2578
2579 /* Convert OBJECT to the type of MEMBER. */
2580 if (!same_type_p (TYPE_MAIN_VARIANT (object_type),
2581 TYPE_MAIN_VARIANT (member_scope)))
2582 {
2583 tree binfo;
2584 base_kind kind;
2585
2586 /* We didn't complain above about a currently open class, but now we
2587 must: we don't know how to refer to a base member before layout is
2588 complete. But still don't complain in a template. */
2589 if (!cp_unevaluated_operand
2590 && !dependent_type_p (object_type)
2591 && !complete_type_or_maybe_complain (object_type, object,
2592 complain))
2593 return error_mark_node;
2594
2595 binfo = lookup_base (access_path ? access_path : object_type,
2596 member_scope, ba_unique, &kind, complain);
2597 if (binfo == error_mark_node)
2598 return error_mark_node;
2599
2600 /* It is invalid to try to get to a virtual base of a
2601 NULL object. The most common cause is invalid use of
2602 offsetof macro. */
2603 if (null_object_p && kind == bk_via_virtual)
2604 {
2605 if (complain & tf_error)
2606 {
2607 error ("invalid access to non-static data member %qD in "
2608 "virtual base of NULL object", member);
2609 }
2610 return error_mark_node;
2611 }
2612
2613 /* Convert to the base. */
2614 object = build_base_path (PLUS_EXPR, object, binfo,
2615 /*nonnull=*/1, complain);
2616 /* If we found the base successfully then we should be able
2617 to convert to it successfully. */
2618 gcc_assert (object != error_mark_node);
2619 }
2620
2621 /* If MEMBER is from an anonymous aggregate, we have converted
2622 OBJECT so that it refers to the class containing the
2623 anonymous union. Generate a reference to the anonymous union
2624 itself, and recur to find MEMBER. */
2625 if (ANON_AGGR_TYPE_P (DECL_CONTEXT (member))
2626 /* When this code is called from build_field_call, the
2627 object already has the type of the anonymous union.
2628 That is because the COMPONENT_REF was already
2629 constructed, and was then disassembled before calling
2630 build_field_call. After the function-call code is
2631 cleaned up, this waste can be eliminated. */
2632 && (!same_type_ignoring_top_level_qualifiers_p
2633 (TREE_TYPE (object), DECL_CONTEXT (member))))
2634 {
2635 tree anonymous_union;
2636
2637 anonymous_union = lookup_anon_field (TREE_TYPE (object),
2638 DECL_CONTEXT (member));
2639 object = build_class_member_access_expr (object,
2640 anonymous_union,
2641 /*access_path=*/NULL_TREE,
2642 preserve_reference,
2643 complain);
2644 }
2645
2646 /* Compute the type of the field, as described in [expr.ref]. */
2647 type_quals = TYPE_UNQUALIFIED;
2648 member_type = TREE_TYPE (member);
2649 if (!TYPE_REF_P (member_type))
2650 {
2651 type_quals = (cp_type_quals (member_type)
2652 | cp_type_quals (object_type));
2653
2654 /* A field is const (volatile) if the enclosing object, or the
2655 field itself, is const (volatile). But, a mutable field is
2656 not const, even within a const object. */
2657 if (DECL_MUTABLE_P (member))
2658 type_quals &= ~TYPE_QUAL_CONST;
2659 member_type = cp_build_qualified_type (member_type, type_quals);
2660 }
2661
2662 result = build3_loc (input_location, COMPONENT_REF, member_type,
2663 object, member, NULL_TREE);
2664
2665 /* Mark the expression const or volatile, as appropriate. Even
2666 though we've dealt with the type above, we still have to mark the
2667 expression itself. */
2668 if (type_quals & TYPE_QUAL_CONST)
2669 TREE_READONLY (result) = 1;
2670 if (type_quals & TYPE_QUAL_VOLATILE)
2671 TREE_THIS_VOLATILE (result) = 1;
2672 }
2673 else if (BASELINK_P (member))
2674 {
2675 /* The member is a (possibly overloaded) member function. */
2676 tree functions;
2677 tree type;
2678
2679 /* If the MEMBER is exactly one static member function, then we
2680 know the type of the expression. Otherwise, we must wait
2681 until overload resolution has been performed. */
2682 functions = BASELINK_FUNCTIONS (member);
2683 if (TREE_CODE (functions) == FUNCTION_DECL
2684 && DECL_STATIC_FUNCTION_P (functions))
2685 type = TREE_TYPE (functions);
2686 else
2687 type = unknown_type_node;
2688 /* Note that we do not convert OBJECT to the BASELINK_BINFO
2689 base. That will happen when the function is called. */
2690 result = build3_loc (input_location, COMPONENT_REF, type, object, member,
2691 NULL_TREE);
2692 }
2693 else if (TREE_CODE (member) == CONST_DECL)
2694 {
2695 /* The member is an enumerator. */
2696 result = member;
2697 /* If OBJECT has side-effects, they are supposed to occur. */
2698 if (TREE_SIDE_EFFECTS (object))
2699 result = build2 (COMPOUND_EXPR, TREE_TYPE (result),
2700 object, result);
2701 }
2702 else if ((using_decl = strip_using_decl (member)) != member)
2703 result = build_class_member_access_expr (object,
2704 using_decl,
2705 access_path, preserve_reference,
2706 complain);
2707 else
2708 {
2709 if (complain & tf_error)
2710 error ("invalid use of %qD", member);
2711 return error_mark_node;
2712 }
2713
2714 if (!preserve_reference)
2715 /* [expr.ref]
2716
2717 If E2 is declared to have type "reference to T", then ... the
2718 type of E1.E2 is T. */
2719 result = convert_from_reference (result);
2720
2721 return result;
2722 }
2723
2724 /* Return the destructor denoted by OBJECT.SCOPE::DTOR_NAME, or, if
2725 SCOPE is NULL, by OBJECT.DTOR_NAME, where DTOR_NAME is ~type. */
2726
2727 tree
2728 lookup_destructor (tree object, tree scope, tree dtor_name,
2729 tsubst_flags_t complain)
2730 {
2731 tree object_type = TREE_TYPE (object);
2732 tree dtor_type = TREE_OPERAND (dtor_name, 0);
2733 tree expr;
2734
2735 /* We've already complained about this destructor. */
2736 if (dtor_type == error_mark_node)
2737 return error_mark_node;
2738
2739 if (scope && !check_dtor_name (scope, dtor_type))
2740 {
2741 if (complain & tf_error)
2742 error ("qualified type %qT does not match destructor name ~%qT",
2743 scope, dtor_type);
2744 return error_mark_node;
2745 }
2746 if (is_auto (dtor_type))
2747 dtor_type = object_type;
2748 else if (identifier_p (dtor_type))
2749 {
2750 /* In a template, names we can't find a match for are still accepted
2751 destructor names, and we check them here. */
2752 if (check_dtor_name (object_type, dtor_type))
2753 dtor_type = object_type;
2754 else
2755 {
2756 if (complain & tf_error)
2757 error ("object type %qT does not match destructor name ~%qT",
2758 object_type, dtor_type);
2759 return error_mark_node;
2760 }
2761
2762 }
2763 else if (!DERIVED_FROM_P (dtor_type, TYPE_MAIN_VARIANT (object_type)))
2764 {
2765 if (complain & tf_error)
2766 error ("the type being destroyed is %qT, but the destructor "
2767 "refers to %qT", TYPE_MAIN_VARIANT (object_type), dtor_type);
2768 return error_mark_node;
2769 }
2770 expr = lookup_member (dtor_type, complete_dtor_identifier,
2771 /*protect=*/1, /*want_type=*/false,
2772 tf_warning_or_error);
2773 if (!expr)
2774 {
2775 if (complain & tf_error)
2776 cxx_incomplete_type_error (dtor_name, dtor_type);
2777 return error_mark_node;
2778 }
2779 expr = (adjust_result_of_qualified_name_lookup
2780 (expr, dtor_type, object_type));
2781 if (scope == NULL_TREE)
2782 /* We need to call adjust_result_of_qualified_name_lookup in case the
2783 destructor names a base class, but we unset BASELINK_QUALIFIED_P so
2784 that we still get virtual function binding. */
2785 BASELINK_QUALIFIED_P (expr) = false;
2786 return expr;
2787 }
2788
2789 /* An expression of the form "A::template B" has been resolved to
2790 DECL. Issue a diagnostic if B is not a template or template
2791 specialization. */
2792
2793 void
2794 check_template_keyword (tree decl)
2795 {
2796 /* The standard says:
2797
2798 [temp.names]
2799
2800 If a name prefixed by the keyword template is not a member
2801 template, the program is ill-formed.
2802
2803 DR 228 removed the restriction that the template be a member
2804 template.
2805
2806 DR 96, if accepted would add the further restriction that explicit
2807 template arguments must be provided if the template keyword is
2808 used, but, as of 2005-10-16, that DR is still in "drafting". If
2809 this DR is accepted, then the semantic checks here can be
2810 simplified, as the entity named must in fact be a template
2811 specialization, rather than, as at present, a set of overloaded
2812 functions containing at least one template function. */
2813 if (TREE_CODE (decl) != TEMPLATE_DECL
2814 && TREE_CODE (decl) != TEMPLATE_ID_EXPR)
2815 {
2816 if (VAR_P (decl))
2817 {
2818 if (DECL_USE_TEMPLATE (decl)
2819 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (decl)))
2820 ;
2821 else
2822 permerror (input_location, "%qD is not a template", decl);
2823 }
2824 else if (!is_overloaded_fn (decl))
2825 permerror (input_location, "%qD is not a template", decl);
2826 else
2827 {
2828 bool found = false;
2829
2830 for (lkp_iterator iter (MAYBE_BASELINK_FUNCTIONS (decl));
2831 !found && iter; ++iter)
2832 {
2833 tree fn = *iter;
2834 if (TREE_CODE (fn) == TEMPLATE_DECL
2835 || TREE_CODE (fn) == TEMPLATE_ID_EXPR
2836 || (TREE_CODE (fn) == FUNCTION_DECL
2837 && DECL_USE_TEMPLATE (fn)
2838 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (fn))))
2839 found = true;
2840 }
2841 if (!found)
2842 permerror (input_location, "%qD is not a template", decl);
2843 }
2844 }
2845 }
2846
2847 /* Record that an access failure occurred on BASETYPE_PATH attempting
2848 to access DECL, where DIAG_DECL should be used for diagnostics. */
2849
2850 void
2851 access_failure_info::record_access_failure (tree basetype_path,
2852 tree decl, tree diag_decl)
2853 {
2854 m_was_inaccessible = true;
2855 m_basetype_path = basetype_path;
2856 m_decl = decl;
2857 m_diag_decl = diag_decl;
2858 }
2859
2860 /* If an access failure was recorded, then attempt to locate an
2861 accessor function for the pertinent field.
2862 Otherwise, return NULL_TREE. */
2863
2864 tree
2865 access_failure_info::get_any_accessor (bool const_p) const
2866 {
2867 if (!was_inaccessible_p ())
2868 return NULL_TREE;
2869
2870 tree accessor
2871 = locate_field_accessor (m_basetype_path, m_diag_decl, const_p);
2872 if (!accessor)
2873 return NULL_TREE;
2874
2875 /* The accessor must itself be accessible for it to be a reasonable
2876 suggestion. */
2877 if (!accessible_p (m_basetype_path, accessor, true))
2878 return NULL_TREE;
2879
2880 return accessor;
2881 }
2882
2883 /* Add a fix-it hint to RICHLOC suggesting the use of ACCESSOR_DECL, by
2884 replacing the primary location in RICHLOC with "accessor()". */
2885
2886 void
2887 access_failure_info::add_fixit_hint (rich_location *richloc,
2888 tree accessor_decl)
2889 {
2890 pretty_printer pp;
2891 pp_printf (&pp, "%s()", IDENTIFIER_POINTER (DECL_NAME (accessor_decl)));
2892 richloc->add_fixit_replace (pp_formatted_text (&pp));
2893 }
2894
2895 /* If an access failure was recorded, then attempt to locate an
2896 accessor function for the pertinent field, and if one is
2897 available, add a note and fix-it hint suggesting using it. */
2898
2899 void
2900 access_failure_info::maybe_suggest_accessor (bool const_p) const
2901 {
2902 tree accessor = get_any_accessor (const_p);
2903 if (accessor == NULL_TREE)
2904 return;
2905 rich_location richloc (line_table, input_location);
2906 add_fixit_hint (&richloc, accessor);
2907 inform (&richloc, "field %q#D can be accessed via %q#D",
2908 m_diag_decl, accessor);
2909 }
2910
2911 /* Subroutine of finish_class_member_access_expr.
2912 Issue an error about NAME not being a member of ACCESS_PATH (or
2913 OBJECT_TYPE), potentially providing a fix-it hint for misspelled
2914 names. */
2915
2916 static void
2917 complain_about_unrecognized_member (tree access_path, tree name,
2918 tree object_type)
2919 {
2920 /* Attempt to provide a hint about misspelled names. */
2921 tree guessed_id = lookup_member_fuzzy (access_path, name,
2922 /*want_type=*/false);
2923 if (guessed_id == NULL_TREE)
2924 {
2925 /* No hint. */
2926 error ("%q#T has no member named %qE",
2927 TREE_CODE (access_path) == TREE_BINFO
2928 ? TREE_TYPE (access_path) : object_type, name);
2929 return;
2930 }
2931
2932 location_t bogus_component_loc = input_location;
2933 gcc_rich_location rich_loc (bogus_component_loc);
2934
2935 /* Check that the guessed name is accessible along access_path. */
2936 access_failure_info afi;
2937 lookup_member (access_path, guessed_id, /*protect=*/1,
2938 /*want_type=*/false, /*complain=*/false,
2939 &afi);
2940 if (afi.was_inaccessible_p ())
2941 {
2942 tree accessor = afi.get_any_accessor (TYPE_READONLY (object_type));
2943 if (accessor)
2944 {
2945 /* The guessed name isn't directly accessible, but can be accessed
2946 via an accessor member function. */
2947 afi.add_fixit_hint (&rich_loc, accessor);
2948 error_at (&rich_loc,
2949 "%q#T has no member named %qE;"
2950 " did you mean %q#D? (accessible via %q#D)",
2951 TREE_CODE (access_path) == TREE_BINFO
2952 ? TREE_TYPE (access_path) : object_type,
2953 name, afi.get_diag_decl (), accessor);
2954 }
2955 else
2956 {
2957 /* The guessed name isn't directly accessible, and no accessor
2958 member function could be found. */
2959 error_at (&rich_loc,
2960 "%q#T has no member named %qE;"
2961 " did you mean %q#D? (not accessible from this context)",
2962 TREE_CODE (access_path) == TREE_BINFO
2963 ? TREE_TYPE (access_path) : object_type,
2964 name, afi.get_diag_decl ());
2965 complain_about_access (afi.get_decl (), afi.get_diag_decl (), false);
2966 }
2967 }
2968 else
2969 {
2970 /* The guessed name is directly accessible; suggest it. */
2971 rich_loc.add_fixit_misspelled_id (bogus_component_loc,
2972 guessed_id);
2973 error_at (&rich_loc,
2974 "%q#T has no member named %qE;"
2975 " did you mean %qE?",
2976 TREE_CODE (access_path) == TREE_BINFO
2977 ? TREE_TYPE (access_path) : object_type,
2978 name, guessed_id);
2979 }
2980 }
2981
2982 /* This function is called by the parser to process a class member
2983 access expression of the form OBJECT.NAME. NAME is a node used by
2984 the parser to represent a name; it is not yet a DECL. It may,
2985 however, be a BASELINK where the BASELINK_FUNCTIONS is a
2986 TEMPLATE_ID_EXPR. Templates must be looked up by the parser, and
2987 there is no reason to do the lookup twice, so the parser keeps the
2988 BASELINK. TEMPLATE_P is true iff NAME was explicitly declared to
2989 be a template via the use of the "A::template B" syntax. */
2990
2991 tree
2992 finish_class_member_access_expr (cp_expr object, tree name, bool template_p,
2993 tsubst_flags_t complain)
2994 {
2995 tree expr;
2996 tree object_type;
2997 tree member;
2998 tree access_path = NULL_TREE;
2999 tree orig_object = object;
3000 tree orig_name = name;
3001
3002 if (object == error_mark_node || name == error_mark_node)
3003 return error_mark_node;
3004
3005 /* If OBJECT is an ObjC class instance, we must obey ObjC access rules. */
3006 if (!objc_is_public (object, name))
3007 return error_mark_node;
3008
3009 object_type = TREE_TYPE (object);
3010
3011 if (processing_template_decl)
3012 {
3013 if (/* If OBJECT is dependent, so is OBJECT.NAME. */
3014 type_dependent_object_expression_p (object)
3015 /* If NAME is "f<args>", where either 'f' or 'args' is
3016 dependent, then the expression is dependent. */
3017 || (TREE_CODE (name) == TEMPLATE_ID_EXPR
3018 && dependent_template_id_p (TREE_OPERAND (name, 0),
3019 TREE_OPERAND (name, 1)))
3020 /* If NAME is "T::X" where "T" is dependent, then the
3021 expression is dependent. */
3022 || (TREE_CODE (name) == SCOPE_REF
3023 && TYPE_P (TREE_OPERAND (name, 0))
3024 && dependent_scope_p (TREE_OPERAND (name, 0)))
3025 /* If NAME is operator T where "T" is dependent, we can't
3026 lookup until we instantiate the T. */
3027 || (TREE_CODE (name) == IDENTIFIER_NODE
3028 && IDENTIFIER_CONV_OP_P (name)
3029 && dependent_type_p (TREE_TYPE (name))))
3030 {
3031 dependent:
3032 return build_min_nt_loc (UNKNOWN_LOCATION, COMPONENT_REF,
3033 orig_object, orig_name, NULL_TREE);
3034 }
3035 object = build_non_dependent_expr (object);
3036 }
3037 else if (c_dialect_objc ()
3038 && identifier_p (name)
3039 && (expr = objc_maybe_build_component_ref (object, name)))
3040 return expr;
3041
3042 /* [expr.ref]
3043
3044 The type of the first expression shall be "class object" (of a
3045 complete type). */
3046 if (!currently_open_class (object_type)
3047 && !complete_type_or_maybe_complain (object_type, object, complain))
3048 return error_mark_node;
3049 if (!CLASS_TYPE_P (object_type))
3050 {
3051 if (complain & tf_error)
3052 {
3053 if (INDIRECT_TYPE_P (object_type)
3054 && CLASS_TYPE_P (TREE_TYPE (object_type)))
3055 error ("request for member %qD in %qE, which is of pointer "
3056 "type %qT (maybe you meant to use %<->%> ?)",
3057 name, object.get_value (), object_type);
3058 else
3059 error ("request for member %qD in %qE, which is of non-class "
3060 "type %qT", name, object.get_value (), object_type);
3061 }
3062 return error_mark_node;
3063 }
3064
3065 if (BASELINK_P (name))
3066 /* A member function that has already been looked up. */
3067 member = name;
3068 else
3069 {
3070 bool is_template_id = false;
3071 tree template_args = NULL_TREE;
3072 tree scope = NULL_TREE;
3073
3074 access_path = object_type;
3075
3076 if (TREE_CODE (name) == SCOPE_REF)
3077 {
3078 /* A qualified name. The qualifying class or namespace `S'
3079 has already been looked up; it is either a TYPE or a
3080 NAMESPACE_DECL. */
3081 scope = TREE_OPERAND (name, 0);
3082 name = TREE_OPERAND (name, 1);
3083
3084 /* If SCOPE is a namespace, then the qualified name does not
3085 name a member of OBJECT_TYPE. */
3086 if (TREE_CODE (scope) == NAMESPACE_DECL)
3087 {
3088 if (complain & tf_error)
3089 error ("%<%D::%D%> is not a member of %qT",
3090 scope, name, object_type);
3091 return error_mark_node;
3092 }
3093 }
3094
3095 if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
3096 {
3097 is_template_id = true;
3098 template_args = TREE_OPERAND (name, 1);
3099 name = TREE_OPERAND (name, 0);
3100
3101 if (!identifier_p (name))
3102 name = OVL_NAME (name);
3103 }
3104
3105 if (scope)
3106 {
3107 if (TREE_CODE (scope) == ENUMERAL_TYPE)
3108 {
3109 gcc_assert (!is_template_id);
3110 /* Looking up a member enumerator (c++/56793). */
3111 if (!TYPE_CLASS_SCOPE_P (scope)
3112 || !DERIVED_FROM_P (TYPE_CONTEXT (scope), object_type))
3113 {
3114 if (complain & tf_error)
3115 error ("%<%D::%D%> is not a member of %qT",
3116 scope, name, object_type);
3117 return error_mark_node;
3118 }
3119 tree val = lookup_enumerator (scope, name);
3120 if (!val)
3121 {
3122 if (complain & tf_error)
3123 error ("%qD is not a member of %qD",
3124 name, scope);
3125 return error_mark_node;
3126 }
3127
3128 if (TREE_SIDE_EFFECTS (object))
3129 val = build2 (COMPOUND_EXPR, TREE_TYPE (val), object, val);
3130 return val;
3131 }
3132
3133 gcc_assert (CLASS_TYPE_P (scope));
3134 gcc_assert (identifier_p (name) || TREE_CODE (name) == BIT_NOT_EXPR);
3135
3136 if (constructor_name_p (name, scope))
3137 {
3138 if (complain & tf_error)
3139 error ("cannot call constructor %<%T::%D%> directly",
3140 scope, name);
3141 return error_mark_node;
3142 }
3143
3144 /* Find the base of OBJECT_TYPE corresponding to SCOPE. */
3145 access_path = lookup_base (object_type, scope, ba_check,
3146 NULL, complain);
3147 if (access_path == error_mark_node)
3148 return error_mark_node;
3149 if (!access_path)
3150 {
3151 if (any_dependent_bases_p (object_type))
3152 goto dependent;
3153 if (complain & tf_error)
3154 error ("%qT is not a base of %qT", scope, object_type);
3155 return error_mark_node;
3156 }
3157 }
3158
3159 if (TREE_CODE (name) == BIT_NOT_EXPR)
3160 {
3161 if (dependent_type_p (object_type))
3162 /* The destructor isn't declared yet. */
3163 goto dependent;
3164 member = lookup_destructor (object, scope, name, complain);
3165 }
3166 else
3167 {
3168 /* Look up the member. */
3169 access_failure_info afi;
3170 member = lookup_member (access_path, name, /*protect=*/1,
3171 /*want_type=*/false, complain,
3172 &afi);
3173 afi.maybe_suggest_accessor (TYPE_READONLY (object_type));
3174 if (member == NULL_TREE)
3175 {
3176 if (dependent_type_p (object_type))
3177 /* Try again at instantiation time. */
3178 goto dependent;
3179 if (complain & tf_error)
3180 complain_about_unrecognized_member (access_path, name,
3181 object_type);
3182 return error_mark_node;
3183 }
3184 if (member == error_mark_node)
3185 return error_mark_node;
3186 if (DECL_P (member)
3187 && any_dependent_type_attributes_p (DECL_ATTRIBUTES (member)))
3188 /* Dependent type attributes on the decl mean that the TREE_TYPE is
3189 wrong, so don't use it. */
3190 goto dependent;
3191 if (TREE_CODE (member) == USING_DECL && DECL_DEPENDENT_P (member))
3192 goto dependent;
3193 }
3194
3195 if (is_template_id)
3196 {
3197 tree templ = member;
3198
3199 if (BASELINK_P (templ))
3200 member = lookup_template_function (templ, template_args);
3201 else if (variable_template_p (templ))
3202 member = (lookup_and_finish_template_variable
3203 (templ, template_args, complain));
3204 else
3205 {
3206 if (complain & tf_error)
3207 error ("%qD is not a member template function", name);
3208 return error_mark_node;
3209 }
3210 }
3211 }
3212
3213 if (TREE_DEPRECATED (member))
3214 warn_deprecated_use (member, NULL_TREE);
3215
3216 if (template_p)
3217 check_template_keyword (member);
3218
3219 expr = build_class_member_access_expr (object, member, access_path,
3220 /*preserve_reference=*/false,
3221 complain);
3222 if (processing_template_decl && expr != error_mark_node)
3223 {
3224 if (BASELINK_P (member))
3225 {
3226 if (TREE_CODE (orig_name) == SCOPE_REF)
3227 BASELINK_QUALIFIED_P (member) = 1;
3228 orig_name = member;
3229 }
3230 return build_min_non_dep (COMPONENT_REF, expr,
3231 orig_object, orig_name,
3232 NULL_TREE);
3233 }
3234
3235 return expr;
3236 }
3237
3238 /* Build a COMPONENT_REF of OBJECT and MEMBER with the appropriate
3239 type. */
3240
3241 tree
3242 build_simple_component_ref (tree object, tree member)
3243 {
3244 tree type = cp_build_qualified_type (TREE_TYPE (member),
3245 cp_type_quals (TREE_TYPE (object)));
3246 return build3_loc (input_location,
3247 COMPONENT_REF, type,
3248 object, member, NULL_TREE);
3249 }
3250
3251 /* Return an expression for the MEMBER_NAME field in the internal
3252 representation of PTRMEM, a pointer-to-member function. (Each
3253 pointer-to-member function type gets its own RECORD_TYPE so it is
3254 more convenient to access the fields by name than by FIELD_DECL.)
3255 This routine converts the NAME to a FIELD_DECL and then creates the
3256 node for the complete expression. */
3257
3258 tree
3259 build_ptrmemfunc_access_expr (tree ptrmem, tree member_name)
3260 {
3261 tree ptrmem_type;
3262 tree member;
3263
3264 if (TREE_CODE (ptrmem) == CONSTRUCTOR)
3265 {
3266 unsigned int ix;
3267 tree index, value;
3268 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (ptrmem),
3269 ix, index, value)
3270 if (index && DECL_P (index) && DECL_NAME (index) == member_name)
3271 return value;
3272 gcc_unreachable ();
3273 }
3274
3275 /* This code is a stripped down version of
3276 build_class_member_access_expr. It does not work to use that
3277 routine directly because it expects the object to be of class
3278 type. */
3279 ptrmem_type = TREE_TYPE (ptrmem);
3280 gcc_assert (TYPE_PTRMEMFUNC_P (ptrmem_type));
3281 for (member = TYPE_FIELDS (ptrmem_type); member;
3282 member = DECL_CHAIN (member))
3283 if (DECL_NAME (member) == member_name)
3284 break;
3285 tree res = build_simple_component_ref (ptrmem, member);
3286
3287 TREE_NO_WARNING (res) = 1;
3288 return res;
3289 }
3290
3291 /* Given an expression PTR for a pointer, return an expression
3292 for the value pointed to.
3293 ERRORSTRING is the name of the operator to appear in error messages.
3294
3295 This function may need to overload OPERATOR_FNNAME.
3296 Must also handle REFERENCE_TYPEs for C++. */
3297
3298 tree
3299 build_x_indirect_ref (location_t loc, tree expr, ref_operator errorstring,
3300 tsubst_flags_t complain)
3301 {
3302 tree orig_expr = expr;
3303 tree rval;
3304 tree overload = NULL_TREE;
3305
3306 if (processing_template_decl)
3307 {
3308 /* Retain the type if we know the operand is a pointer. */
3309 if (TREE_TYPE (expr) && INDIRECT_TYPE_P (TREE_TYPE (expr)))
3310 return build_min (INDIRECT_REF, TREE_TYPE (TREE_TYPE (expr)), expr);
3311 if (type_dependent_expression_p (expr))
3312 return build_min_nt_loc (loc, INDIRECT_REF, expr);
3313 expr = build_non_dependent_expr (expr);
3314 }
3315
3316 rval = build_new_op (loc, INDIRECT_REF, LOOKUP_NORMAL, expr,
3317 NULL_TREE, NULL_TREE, &overload, complain);
3318 if (!rval)
3319 rval = cp_build_indirect_ref (loc, expr, errorstring, complain);
3320
3321 if (processing_template_decl && rval != error_mark_node)
3322 {
3323 if (overload != NULL_TREE)
3324 return (build_min_non_dep_op_overload
3325 (INDIRECT_REF, rval, overload, orig_expr));
3326
3327 return build_min_non_dep (INDIRECT_REF, rval, orig_expr);
3328 }
3329 else
3330 return rval;
3331 }
3332
3333 /* Like c-family strict_aliasing_warning, but don't warn for dependent
3334 types or expressions. */
3335
3336 static bool
3337 cp_strict_aliasing_warning (location_t loc, tree type, tree expr)
3338 {
3339 if (processing_template_decl)
3340 {
3341 tree e = expr;
3342 STRIP_NOPS (e);
3343 if (dependent_type_p (type) || type_dependent_expression_p (e))
3344 return false;
3345 }
3346 return strict_aliasing_warning (loc, type, expr);
3347 }
3348
3349 /* The implementation of the above, and of indirection implied by other
3350 constructs. If DO_FOLD is true, fold away INDIRECT_REF of ADDR_EXPR. */
3351
3352 static tree
3353 cp_build_indirect_ref_1 (location_t loc, tree ptr, ref_operator errorstring,
3354 tsubst_flags_t complain, bool do_fold)
3355 {
3356 tree pointer, type;
3357
3358 /* RO_NULL should only be used with the folding entry points below, not
3359 cp_build_indirect_ref. */
3360 gcc_checking_assert (errorstring != RO_NULL || do_fold);
3361
3362 if (ptr == current_class_ptr
3363 || (TREE_CODE (ptr) == NOP_EXPR
3364 && TREE_OPERAND (ptr, 0) == current_class_ptr
3365 && (same_type_ignoring_top_level_qualifiers_p
3366 (TREE_TYPE (ptr), TREE_TYPE (current_class_ptr)))))
3367 return current_class_ref;
3368
3369 pointer = (TYPE_REF_P (TREE_TYPE (ptr))
3370 ? ptr : decay_conversion (ptr, complain));
3371 if (pointer == error_mark_node)
3372 return error_mark_node;
3373
3374 type = TREE_TYPE (pointer);
3375
3376 if (INDIRECT_TYPE_P (type))
3377 {
3378 /* [expr.unary.op]
3379
3380 If the type of the expression is "pointer to T," the type
3381 of the result is "T." */
3382 tree t = TREE_TYPE (type);
3383
3384 if ((CONVERT_EXPR_P (ptr)
3385 || TREE_CODE (ptr) == VIEW_CONVERT_EXPR)
3386 && (!CLASS_TYPE_P (t) || !CLASSTYPE_EMPTY_P (t)))
3387 {
3388 /* If a warning is issued, mark it to avoid duplicates from
3389 the backend. This only needs to be done at
3390 warn_strict_aliasing > 2. */
3391 if (warn_strict_aliasing > 2
3392 && cp_strict_aliasing_warning (EXPR_LOCATION (ptr),
3393 type, TREE_OPERAND (ptr, 0)))
3394 TREE_NO_WARNING (ptr) = 1;
3395 }
3396
3397 if (VOID_TYPE_P (t))
3398 {
3399 /* A pointer to incomplete type (other than cv void) can be
3400 dereferenced [expr.unary.op]/1 */
3401 if (complain & tf_error)
3402 error_at (loc, "%qT is not a pointer-to-object type", type);
3403 return error_mark_node;
3404 }
3405 else if (do_fold && TREE_CODE (pointer) == ADDR_EXPR
3406 && same_type_p (t, TREE_TYPE (TREE_OPERAND (pointer, 0))))
3407 /* The POINTER was something like `&x'. We simplify `*&x' to
3408 `x'. */
3409 return TREE_OPERAND (pointer, 0);
3410 else
3411 {
3412 tree ref = build1 (INDIRECT_REF, t, pointer);
3413
3414 /* We *must* set TREE_READONLY when dereferencing a pointer to const,
3415 so that we get the proper error message if the result is used
3416 to assign to. Also, &* is supposed to be a no-op. */
3417 TREE_READONLY (ref) = CP_TYPE_CONST_P (t);
3418 TREE_THIS_VOLATILE (ref) = CP_TYPE_VOLATILE_P (t);
3419 TREE_SIDE_EFFECTS (ref)
3420 = (TREE_THIS_VOLATILE (ref) || TREE_SIDE_EFFECTS (pointer));
3421 return ref;
3422 }
3423 }
3424 else if (!(complain & tf_error))
3425 /* Don't emit any errors; we'll just return ERROR_MARK_NODE later. */
3426 ;
3427 /* `pointer' won't be an error_mark_node if we were given a
3428 pointer to member, so it's cool to check for this here. */
3429 else if (TYPE_PTRMEM_P (type))
3430 switch (errorstring)
3431 {
3432 case RO_ARRAY_INDEXING:
3433 error_at (loc,
3434 "invalid use of array indexing on pointer to member");
3435 break;
3436 case RO_UNARY_STAR:
3437 error_at (loc, "invalid use of unary %<*%> on pointer to member");
3438 break;
3439 case RO_IMPLICIT_CONVERSION:
3440 error_at (loc, "invalid use of implicit conversion on pointer "
3441 "to member");
3442 break;
3443 case RO_ARROW_STAR:
3444 error_at (loc, "left hand operand of %<->*%> must be a pointer to "
3445 "class, but is a pointer to member of type %qT", type);
3446 break;
3447 default:
3448 gcc_unreachable ();
3449 }
3450 else if (pointer != error_mark_node)
3451 invalid_indirection_error (loc, type, errorstring);
3452
3453 return error_mark_node;
3454 }
3455
3456 /* Entry point used by c-common, which expects folding. */
3457
3458 tree
3459 build_indirect_ref (location_t loc, tree ptr, ref_operator errorstring)
3460 {
3461 return cp_build_indirect_ref_1 (loc, ptr, errorstring,
3462 tf_warning_or_error, true);
3463 }
3464
3465 /* Entry point used by internal indirection needs that don't correspond to any
3466 syntactic construct. */
3467
3468 tree
3469 cp_build_fold_indirect_ref (tree pointer)
3470 {
3471 return cp_build_indirect_ref_1 (input_location, pointer, RO_NULL,
3472 tf_warning_or_error, true);
3473 }
3474
3475 /* Entry point used by indirection needs that correspond to some syntactic
3476 construct. */
3477
3478 tree
3479 cp_build_indirect_ref (location_t loc, tree ptr, ref_operator errorstring,
3480 tsubst_flags_t complain)
3481 {
3482 return cp_build_indirect_ref_1 (loc, ptr, errorstring, complain, false);
3483 }
3484
3485 /* This handles expressions of the form "a[i]", which denotes
3486 an array reference.
3487
3488 This is logically equivalent in C to *(a+i), but we may do it differently.
3489 If A is a variable or a member, we generate a primitive ARRAY_REF.
3490 This avoids forcing the array out of registers, and can work on
3491 arrays that are not lvalues (for example, members of structures returned
3492 by functions).
3493
3494 If INDEX is of some user-defined type, it must be converted to
3495 integer type. Otherwise, to make a compatible PLUS_EXPR, it
3496 will inherit the type of the array, which will be some pointer type.
3497
3498 LOC is the location to use in building the array reference. */
3499
3500 tree
3501 cp_build_array_ref (location_t loc, tree array, tree idx,
3502 tsubst_flags_t complain)
3503 {
3504 tree ret;
3505
3506 if (idx == 0)
3507 {
3508 if (complain & tf_error)
3509 error_at (loc, "subscript missing in array reference");
3510 return error_mark_node;
3511 }
3512
3513 if (TREE_TYPE (array) == error_mark_node
3514 || TREE_TYPE (idx) == error_mark_node)
3515 return error_mark_node;
3516
3517 /* If ARRAY is a COMPOUND_EXPR or COND_EXPR, move our reference
3518 inside it. */
3519 switch (TREE_CODE (array))
3520 {
3521 case COMPOUND_EXPR:
3522 {
3523 tree value = cp_build_array_ref (loc, TREE_OPERAND (array, 1), idx,
3524 complain);
3525 ret = build2 (COMPOUND_EXPR, TREE_TYPE (value),
3526 TREE_OPERAND (array, 0), value);
3527 SET_EXPR_LOCATION (ret, loc);
3528 return ret;
3529 }
3530
3531 case COND_EXPR:
3532 ret = build_conditional_expr
3533 (loc, TREE_OPERAND (array, 0),
3534 cp_build_array_ref (loc, TREE_OPERAND (array, 1), idx,
3535 complain),
3536 cp_build_array_ref (loc, TREE_OPERAND (array, 2), idx,
3537 complain),
3538 complain);
3539 protected_set_expr_location (ret, loc);
3540 return ret;
3541
3542 default:
3543 break;
3544 }
3545
3546 bool non_lvalue = convert_vector_to_array_for_subscript (loc, &array, idx);
3547
3548 if (TREE_CODE (TREE_TYPE (array)) == ARRAY_TYPE)
3549 {
3550 tree rval, type;
3551
3552 warn_array_subscript_with_type_char (loc, idx);
3553
3554 if (!INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (TREE_TYPE (idx)))
3555 {
3556 if (complain & tf_error)
3557 error_at (loc, "array subscript is not an integer");
3558 return error_mark_node;
3559 }
3560
3561 /* Apply integral promotions *after* noticing character types.
3562 (It is unclear why we do these promotions -- the standard
3563 does not say that we should. In fact, the natural thing would
3564 seem to be to convert IDX to ptrdiff_t; we're performing
3565 pointer arithmetic.) */
3566 idx = cp_perform_integral_promotions (idx, complain);
3567
3568 idx = maybe_constant_value (idx);
3569
3570 /* An array that is indexed by a non-constant
3571 cannot be stored in a register; we must be able to do
3572 address arithmetic on its address.
3573 Likewise an array of elements of variable size. */
3574 if (TREE_CODE (idx) != INTEGER_CST
3575 || (COMPLETE_TYPE_P (TREE_TYPE (TREE_TYPE (array)))
3576 && (TREE_CODE (TYPE_SIZE (TREE_TYPE (TREE_TYPE (array))))
3577 != INTEGER_CST)))
3578 {
3579 if (!cxx_mark_addressable (array, true))
3580 return error_mark_node;
3581 }
3582
3583 /* An array that is indexed by a constant value which is not within
3584 the array bounds cannot be stored in a register either; because we
3585 would get a crash in store_bit_field/extract_bit_field when trying
3586 to access a non-existent part of the register. */
3587 if (TREE_CODE (idx) == INTEGER_CST
3588 && TYPE_DOMAIN (TREE_TYPE (array))
3589 && ! int_fits_type_p (idx, TYPE_DOMAIN (TREE_TYPE (array))))
3590 {
3591 if (!cxx_mark_addressable (array))
3592 return error_mark_node;
3593 }
3594
3595 /* Note in C++ it is valid to subscript a `register' array, since
3596 it is valid to take the address of something with that
3597 storage specification. */
3598 if (extra_warnings)
3599 {
3600 tree foo = array;
3601 while (TREE_CODE (foo) == COMPONENT_REF)
3602 foo = TREE_OPERAND (foo, 0);
3603 if (VAR_P (foo) && DECL_REGISTER (foo)
3604 && (complain & tf_warning))
3605 warning_at (loc, OPT_Wextra,
3606 "subscripting array declared %<register%>");
3607 }
3608
3609 type = TREE_TYPE (TREE_TYPE (array));
3610 rval = build4 (ARRAY_REF, type, array, idx, NULL_TREE, NULL_TREE);
3611 /* Array ref is const/volatile if the array elements are
3612 or if the array is.. */
3613 TREE_READONLY (rval)
3614 |= (CP_TYPE_CONST_P (type) | TREE_READONLY (array));
3615 TREE_SIDE_EFFECTS (rval)
3616 |= (CP_TYPE_VOLATILE_P (type) | TREE_SIDE_EFFECTS (array));
3617 TREE_THIS_VOLATILE (rval)
3618 |= (CP_TYPE_VOLATILE_P (type) | TREE_THIS_VOLATILE (array));
3619 ret = require_complete_type_sfinae (rval, complain);
3620 protected_set_expr_location (ret, loc);
3621 if (non_lvalue)
3622 ret = non_lvalue_loc (loc, ret);
3623 return ret;
3624 }
3625
3626 {
3627 tree ar = cp_default_conversion (array, complain);
3628 tree ind = cp_default_conversion (idx, complain);
3629 tree first = NULL_TREE;
3630
3631 if (flag_strong_eval_order == 2 && TREE_SIDE_EFFECTS (ind))
3632 ar = first = save_expr (ar);
3633
3634 /* Put the integer in IND to simplify error checking. */
3635 if (TREE_CODE (TREE_TYPE (ar)) == INTEGER_TYPE)
3636 std::swap (ar, ind);
3637
3638 if (ar == error_mark_node || ind == error_mark_node)
3639 return error_mark_node;
3640
3641 if (!TYPE_PTR_P (TREE_TYPE (ar)))
3642 {
3643 if (complain & tf_error)
3644 error_at (loc, "subscripted value is neither array nor pointer");
3645 return error_mark_node;
3646 }
3647 if (TREE_CODE (TREE_TYPE (ind)) != INTEGER_TYPE)
3648 {
3649 if (complain & tf_error)
3650 error_at (loc, "array subscript is not an integer");
3651 return error_mark_node;
3652 }
3653
3654 warn_array_subscript_with_type_char (loc, idx);
3655
3656 ret = cp_build_binary_op (input_location, PLUS_EXPR, ar, ind, complain);
3657 if (first)
3658 ret = build2_loc (loc, COMPOUND_EXPR, TREE_TYPE (ret), first, ret);
3659 ret = cp_build_indirect_ref (loc, ret, RO_ARRAY_INDEXING, complain);
3660 protected_set_expr_location (ret, loc);
3661 if (non_lvalue)
3662 ret = non_lvalue_loc (loc, ret);
3663 return ret;
3664 }
3665 }
3666
3667 /* Entry point for Obj-C++. */
3668
3669 tree
3670 build_array_ref (location_t loc, tree array, tree idx)
3671 {
3672 return cp_build_array_ref (loc, array, idx, tf_warning_or_error);
3673 }
3674 \f
3675 /* Resolve a pointer to member function. INSTANCE is the object
3676 instance to use, if the member points to a virtual member.
3677
3678 This used to avoid checking for virtual functions if basetype
3679 has no virtual functions, according to an earlier ANSI draft.
3680 With the final ISO C++ rules, such an optimization is
3681 incorrect: A pointer to a derived member can be static_cast
3682 to pointer-to-base-member, as long as the dynamic object
3683 later has the right member. So now we only do this optimization
3684 when we know the dynamic type of the object. */
3685
3686 tree
3687 get_member_function_from_ptrfunc (tree *instance_ptrptr, tree function,
3688 tsubst_flags_t complain)
3689 {
3690 if (TREE_CODE (function) == OFFSET_REF)
3691 function = TREE_OPERAND (function, 1);
3692
3693 if (TYPE_PTRMEMFUNC_P (TREE_TYPE (function)))
3694 {
3695 tree idx, delta, e1, e2, e3, vtbl;
3696 bool nonvirtual;
3697 tree fntype = TYPE_PTRMEMFUNC_FN_TYPE (TREE_TYPE (function));
3698 tree basetype = TYPE_METHOD_BASETYPE (TREE_TYPE (fntype));
3699
3700 tree instance_ptr = *instance_ptrptr;
3701 tree instance_save_expr = 0;
3702 if (instance_ptr == error_mark_node)
3703 {
3704 if (TREE_CODE (function) == PTRMEM_CST)
3705 {
3706 /* Extracting the function address from a pmf is only
3707 allowed with -Wno-pmf-conversions. It only works for
3708 pmf constants. */
3709 e1 = build_addr_func (PTRMEM_CST_MEMBER (function), complain);
3710 e1 = convert (fntype, e1);
3711 return e1;
3712 }
3713 else
3714 {
3715 if (complain & tf_error)
3716 error ("object missing in use of %qE", function);
3717 return error_mark_node;
3718 }
3719 }
3720
3721 /* True if we know that the dynamic type of the object doesn't have
3722 virtual functions, so we can assume the PFN field is a pointer. */
3723 nonvirtual = (COMPLETE_TYPE_P (basetype)
3724 && !TYPE_POLYMORPHIC_P (basetype)
3725 && resolves_to_fixed_type_p (instance_ptr, 0));
3726
3727 /* If we don't really have an object (i.e. in an ill-formed
3728 conversion from PMF to pointer), we can't resolve virtual
3729 functions anyway. */
3730 if (!nonvirtual && is_dummy_object (instance_ptr))
3731 nonvirtual = true;
3732
3733 if (TREE_SIDE_EFFECTS (instance_ptr))
3734 instance_ptr = instance_save_expr = save_expr (instance_ptr);
3735
3736 if (TREE_SIDE_EFFECTS (function))
3737 function = save_expr (function);
3738
3739 /* Start by extracting all the information from the PMF itself. */
3740 e3 = pfn_from_ptrmemfunc (function);
3741 delta = delta_from_ptrmemfunc (function);
3742 idx = build1 (NOP_EXPR, vtable_index_type, e3);
3743 switch (TARGET_PTRMEMFUNC_VBIT_LOCATION)
3744 {
3745 int flag_sanitize_save;
3746 case ptrmemfunc_vbit_in_pfn:
3747 e1 = cp_build_binary_op (input_location,
3748 BIT_AND_EXPR, idx, integer_one_node,
3749 complain);
3750 idx = cp_build_binary_op (input_location,
3751 MINUS_EXPR, idx, integer_one_node,
3752 complain);
3753 if (idx == error_mark_node)
3754 return error_mark_node;
3755 break;
3756
3757 case ptrmemfunc_vbit_in_delta:
3758 e1 = cp_build_binary_op (input_location,
3759 BIT_AND_EXPR, delta, integer_one_node,
3760 complain);
3761 /* Don't instrument the RSHIFT_EXPR we're about to create because
3762 we're going to use DELTA number of times, and that wouldn't play
3763 well with SAVE_EXPRs therein. */
3764 flag_sanitize_save = flag_sanitize;
3765 flag_sanitize = 0;
3766 delta = cp_build_binary_op (input_location,
3767 RSHIFT_EXPR, delta, integer_one_node,
3768 complain);
3769 flag_sanitize = flag_sanitize_save;
3770 if (delta == error_mark_node)
3771 return error_mark_node;
3772 break;
3773
3774 default:
3775 gcc_unreachable ();
3776 }
3777
3778 if (e1 == error_mark_node)
3779 return error_mark_node;
3780
3781 /* Convert down to the right base before using the instance. A
3782 special case is that in a pointer to member of class C, C may
3783 be incomplete. In that case, the function will of course be
3784 a member of C, and no conversion is required. In fact,
3785 lookup_base will fail in that case, because incomplete
3786 classes do not have BINFOs. */
3787 if (!same_type_ignoring_top_level_qualifiers_p
3788 (basetype, TREE_TYPE (TREE_TYPE (instance_ptr))))
3789 {
3790 basetype = lookup_base (TREE_TYPE (TREE_TYPE (instance_ptr)),
3791 basetype, ba_check, NULL, complain);
3792 instance_ptr = build_base_path (PLUS_EXPR, instance_ptr, basetype,
3793 1, complain);
3794 if (instance_ptr == error_mark_node)
3795 return error_mark_node;
3796 }
3797 /* ...and then the delta in the PMF. */
3798 instance_ptr = fold_build_pointer_plus (instance_ptr, delta);
3799
3800 /* Hand back the adjusted 'this' argument to our caller. */
3801 *instance_ptrptr = instance_ptr;
3802
3803 if (nonvirtual)
3804 /* Now just return the pointer. */
3805 return e3;
3806
3807 /* Next extract the vtable pointer from the object. */
3808 vtbl = build1 (NOP_EXPR, build_pointer_type (vtbl_ptr_type_node),
3809 instance_ptr);
3810 vtbl = cp_build_fold_indirect_ref (vtbl);
3811 if (vtbl == error_mark_node)
3812 return error_mark_node;
3813
3814 /* Finally, extract the function pointer from the vtable. */
3815 e2 = fold_build_pointer_plus_loc (input_location, vtbl, idx);
3816 e2 = cp_build_fold_indirect_ref (e2);
3817 if (e2 == error_mark_node)
3818 return error_mark_node;
3819 TREE_CONSTANT (e2) = 1;
3820
3821 /* When using function descriptors, the address of the
3822 vtable entry is treated as a function pointer. */
3823 if (TARGET_VTABLE_USES_DESCRIPTORS)
3824 e2 = build1 (NOP_EXPR, TREE_TYPE (e2),
3825 cp_build_addr_expr (e2, complain));
3826
3827 e2 = fold_convert (TREE_TYPE (e3), e2);
3828 e1 = build_conditional_expr (input_location, e1, e2, e3, complain);
3829 if (e1 == error_mark_node)
3830 return error_mark_node;
3831
3832 /* Make sure this doesn't get evaluated first inside one of the
3833 branches of the COND_EXPR. */
3834 if (instance_save_expr)
3835 e1 = build2 (COMPOUND_EXPR, TREE_TYPE (e1),
3836 instance_save_expr, e1);
3837
3838 function = e1;
3839 }
3840 return function;
3841 }
3842
3843 /* Used by the C-common bits. */
3844 tree
3845 build_function_call (location_t /*loc*/,
3846 tree function, tree params)
3847 {
3848 return cp_build_function_call (function, params, tf_warning_or_error);
3849 }
3850
3851 /* Used by the C-common bits. */
3852 tree
3853 build_function_call_vec (location_t /*loc*/, vec<location_t> /*arg_loc*/,
3854 tree function, vec<tree, va_gc> *params,
3855 vec<tree, va_gc> * /*origtypes*/, tree orig_function)
3856 {
3857 vec<tree, va_gc> *orig_params = params;
3858 tree ret = cp_build_function_call_vec (function, &params,
3859 tf_warning_or_error, orig_function);
3860
3861 /* cp_build_function_call_vec can reallocate PARAMS by adding
3862 default arguments. That should never happen here. Verify
3863 that. */
3864 gcc_assert (params == orig_params);
3865
3866 return ret;
3867 }
3868
3869 /* Build a function call using a tree list of arguments. */
3870
3871 static tree
3872 cp_build_function_call (tree function, tree params, tsubst_flags_t complain)
3873 {
3874 tree ret;
3875
3876 releasing_vec vec;
3877 for (; params != NULL_TREE; params = TREE_CHAIN (params))
3878 vec_safe_push (vec, TREE_VALUE (params));
3879 ret = cp_build_function_call_vec (function, &vec, complain);
3880 return ret;
3881 }
3882
3883 /* Build a function call using varargs. */
3884
3885 tree
3886 cp_build_function_call_nary (tree function, tsubst_flags_t complain, ...)
3887 {
3888 va_list args;
3889 tree ret, t;
3890
3891 releasing_vec vec;
3892 va_start (args, complain);
3893 for (t = va_arg (args, tree); t != NULL_TREE; t = va_arg (args, tree))
3894 vec_safe_push (vec, t);
3895 va_end (args);
3896 ret = cp_build_function_call_vec (function, &vec, complain);
3897 return ret;
3898 }
3899
3900 /* Build a function call using a vector of arguments.
3901 If FUNCTION is the result of resolving an overloaded target built-in,
3902 ORIG_FNDECL is the original function decl, otherwise it is null.
3903 PARAMS may be NULL if there are no parameters. This changes the
3904 contents of PARAMS. */
3905
3906 tree
3907 cp_build_function_call_vec (tree function, vec<tree, va_gc> **params,
3908 tsubst_flags_t complain, tree orig_fndecl)
3909 {
3910 tree fntype, fndecl;
3911 int is_method;
3912 tree original = function;
3913 int nargs;
3914 tree *argarray;
3915 tree parm_types;
3916 vec<tree, va_gc> *allocated = NULL;
3917 tree ret;
3918
3919 /* For Objective-C, convert any calls via a cast to OBJC_TYPE_REF
3920 expressions, like those used for ObjC messenger dispatches. */
3921 if (params != NULL && !vec_safe_is_empty (*params))
3922 function = objc_rewrite_function_call (function, (**params)[0]);
3923
3924 /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
3925 Strip such NOP_EXPRs, since FUNCTION is used in non-lvalue context. */
3926 if (TREE_CODE (function) == NOP_EXPR
3927 && TREE_TYPE (function) == TREE_TYPE (TREE_OPERAND (function, 0)))
3928 function = TREE_OPERAND (function, 0);
3929
3930 if (TREE_CODE (function) == FUNCTION_DECL)
3931 {
3932 if (!mark_used (function, complain))
3933 return error_mark_node;
3934 fndecl = function;
3935
3936 /* Convert anything with function type to a pointer-to-function. */
3937 if (DECL_MAIN_P (function))
3938 {
3939 if (complain & tf_error)
3940 pedwarn (input_location, OPT_Wpedantic,
3941 "ISO C++ forbids calling %<::main%> from within program");
3942 else
3943 return error_mark_node;
3944 }
3945 function = build_addr_func (function, complain);
3946 }
3947 else
3948 {
3949 fndecl = NULL_TREE;
3950
3951 function = build_addr_func (function, complain);
3952 }
3953
3954 if (function == error_mark_node)
3955 return error_mark_node;
3956
3957 fntype = TREE_TYPE (function);
3958
3959 if (TYPE_PTRMEMFUNC_P (fntype))
3960 {
3961 if (complain & tf_error)
3962 error ("must use %<.*%> or %<->*%> to call pointer-to-member "
3963 "function in %<%E (...)%>, e.g. %<(... ->* %E) (...)%>",
3964 original, original);
3965 return error_mark_node;
3966 }
3967
3968 is_method = (TYPE_PTR_P (fntype)
3969 && TREE_CODE (TREE_TYPE (fntype)) == METHOD_TYPE);
3970
3971 if (!(TYPE_PTRFN_P (fntype)
3972 || is_method
3973 || TREE_CODE (function) == TEMPLATE_ID_EXPR))
3974 {
3975 if (complain & tf_error)
3976 {
3977 if (!flag_diagnostics_show_caret)
3978 error_at (input_location,
3979 "%qE cannot be used as a function", original);
3980 else if (DECL_P (original))
3981 error_at (input_location,
3982 "%qD cannot be used as a function", original);
3983 else
3984 error_at (input_location,
3985 "expression cannot be used as a function");
3986 }
3987
3988 return error_mark_node;
3989 }
3990
3991 /* fntype now gets the type of function pointed to. */
3992 fntype = TREE_TYPE (fntype);
3993 parm_types = TYPE_ARG_TYPES (fntype);
3994
3995 if (params == NULL)
3996 {
3997 allocated = make_tree_vector ();
3998 params = &allocated;
3999 }
4000
4001 nargs = convert_arguments (parm_types, params, fndecl, LOOKUP_NORMAL,
4002 complain);
4003 if (nargs < 0)
4004 return error_mark_node;
4005
4006 argarray = (*params)->address ();
4007
4008 /* Check for errors in format strings and inappropriately
4009 null parameters. */
4010 bool warned_p = check_function_arguments (input_location, fndecl, fntype,
4011 nargs, argarray, NULL);
4012
4013 ret = build_cxx_call (function, nargs, argarray, complain, orig_fndecl);
4014
4015 if (warned_p)
4016 {
4017 tree c = extract_call_expr (ret);
4018 if (TREE_CODE (c) == CALL_EXPR)
4019 TREE_NO_WARNING (c) = 1;
4020 }
4021
4022 if (allocated != NULL)
4023 release_tree_vector (allocated);
4024
4025 return ret;
4026 }
4027 \f
4028 /* Subroutine of convert_arguments.
4029 Print an error message about a wrong number of arguments. */
4030
4031 static void
4032 error_args_num (location_t loc, tree fndecl, bool too_many_p)
4033 {
4034 if (fndecl)
4035 {
4036 if (TREE_CODE (TREE_TYPE (fndecl)) == METHOD_TYPE)
4037 {
4038 if (DECL_NAME (fndecl) == NULL_TREE
4039 || IDENTIFIER_HAS_TYPE_VALUE (DECL_NAME (fndecl)))
4040 error_at (loc,
4041 too_many_p
4042 ? G_("too many arguments to constructor %q#D")
4043 : G_("too few arguments to constructor %q#D"),
4044 fndecl);
4045 else
4046 error_at (loc,
4047 too_many_p
4048 ? G_("too many arguments to member function %q#D")
4049 : G_("too few arguments to member function %q#D"),
4050 fndecl);
4051 }
4052 else
4053 error_at (loc,
4054 too_many_p
4055 ? G_("too many arguments to function %q#D")
4056 : G_("too few arguments to function %q#D"),
4057 fndecl);
4058 if (!DECL_IS_BUILTIN (fndecl))
4059 inform (DECL_SOURCE_LOCATION (fndecl), "declared here");
4060 }
4061 else
4062 {
4063 if (c_dialect_objc () && objc_message_selector ())
4064 error_at (loc,
4065 too_many_p
4066 ? G_("too many arguments to method %q#D")
4067 : G_("too few arguments to method %q#D"),
4068 objc_message_selector ());
4069 else
4070 error_at (loc, too_many_p ? G_("too many arguments to function")
4071 : G_("too few arguments to function"));
4072 }
4073 }
4074
4075 /* Convert the actual parameter expressions in the list VALUES to the
4076 types in the list TYPELIST. The converted expressions are stored
4077 back in the VALUES vector.
4078 If parmdecls is exhausted, or when an element has NULL as its type,
4079 perform the default conversions.
4080
4081 NAME is an IDENTIFIER_NODE or 0. It is used only for error messages.
4082
4083 This is also where warnings about wrong number of args are generated.
4084
4085 Returns the actual number of arguments processed (which might be less
4086 than the length of the vector), or -1 on error.
4087
4088 In C++, unspecified trailing parameters can be filled in with their
4089 default arguments, if such were specified. Do so here. */
4090
4091 static int
4092 convert_arguments (tree typelist, vec<tree, va_gc> **values, tree fndecl,
4093 int flags, tsubst_flags_t complain)
4094 {
4095 tree typetail;
4096 unsigned int i;
4097
4098 /* Argument passing is always copy-initialization. */
4099 flags |= LOOKUP_ONLYCONVERTING;
4100
4101 for (i = 0, typetail = typelist;
4102 i < vec_safe_length (*values);
4103 i++)
4104 {
4105 tree type = typetail ? TREE_VALUE (typetail) : 0;
4106 tree val = (**values)[i];
4107
4108 if (val == error_mark_node || type == error_mark_node)
4109 return -1;
4110
4111 if (type == void_type_node)
4112 {
4113 if (complain & tf_error)
4114 {
4115 error_args_num (input_location, fndecl, /*too_many_p=*/true);
4116 return i;
4117 }
4118 else
4119 return -1;
4120 }
4121
4122 /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
4123 Strip such NOP_EXPRs, since VAL is used in non-lvalue context. */
4124 if (TREE_CODE (val) == NOP_EXPR
4125 && TREE_TYPE (val) == TREE_TYPE (TREE_OPERAND (val, 0))
4126 && (type == 0 || !TYPE_REF_P (type)))
4127 val = TREE_OPERAND (val, 0);
4128
4129 if (type == 0 || !TYPE_REF_P (type))
4130 {
4131 if (TREE_CODE (TREE_TYPE (val)) == ARRAY_TYPE
4132 || FUNC_OR_METHOD_TYPE_P (TREE_TYPE (val)))
4133 val = decay_conversion (val, complain);
4134 }
4135
4136 if (val == error_mark_node)
4137 return -1;
4138
4139 if (type != 0)
4140 {
4141 /* Formal parm type is specified by a function prototype. */
4142 tree parmval;
4143
4144 if (!COMPLETE_TYPE_P (complete_type (type)))
4145 {
4146 if (complain & tf_error)
4147 {
4148 location_t loc = EXPR_LOC_OR_LOC (val, input_location);
4149 if (fndecl)
4150 {
4151 auto_diagnostic_group d;
4152 error_at (loc,
4153 "parameter %P of %qD has incomplete type %qT",
4154 i, fndecl, type);
4155 inform (get_fndecl_argument_location (fndecl, i),
4156 " declared here");
4157 }
4158 else
4159 error_at (loc, "parameter %P has incomplete type %qT", i,
4160 type);
4161 }
4162 parmval = error_mark_node;
4163 }
4164 else
4165 {
4166 parmval = convert_for_initialization
4167 (NULL_TREE, type, val, flags,
4168 ICR_ARGPASS, fndecl, i, complain);
4169 parmval = convert_for_arg_passing (type, parmval, complain);
4170 }
4171
4172 if (parmval == error_mark_node)
4173 return -1;
4174
4175 (**values)[i] = parmval;
4176 }
4177 else
4178 {
4179 if (fndecl && magic_varargs_p (fndecl))
4180 /* Don't do ellipsis conversion for __built_in_constant_p
4181 as this will result in spurious errors for non-trivial
4182 types. */
4183 val = require_complete_type_sfinae (val, complain);
4184 else
4185 val = convert_arg_to_ellipsis (val, complain);
4186
4187 (**values)[i] = val;
4188 }
4189
4190 if (typetail)
4191 typetail = TREE_CHAIN (typetail);
4192 }
4193
4194 if (typetail != 0 && typetail != void_list_node)
4195 {
4196 /* See if there are default arguments that can be used. Because
4197 we hold default arguments in the FUNCTION_TYPE (which is so
4198 wrong), we can see default parameters here from deduced
4199 contexts (and via typeof) for indirect function calls.
4200 Fortunately we know whether we have a function decl to
4201 provide default arguments in a language conformant
4202 manner. */
4203 if (fndecl && TREE_PURPOSE (typetail)
4204 && TREE_CODE (TREE_PURPOSE (typetail)) != DEFERRED_PARSE)
4205 {
4206 for (; typetail != void_list_node; ++i)
4207 {
4208 /* After DR777, with explicit template args we can end up with a
4209 default argument followed by no default argument. */
4210 if (!TREE_PURPOSE (typetail))
4211 break;
4212 tree parmval
4213 = convert_default_arg (TREE_VALUE (typetail),
4214 TREE_PURPOSE (typetail),
4215 fndecl, i, complain);
4216
4217 if (parmval == error_mark_node)
4218 return -1;
4219
4220 vec_safe_push (*values, parmval);
4221 typetail = TREE_CHAIN (typetail);
4222 /* ends with `...'. */
4223 if (typetail == NULL_TREE)
4224 break;
4225 }
4226 }
4227
4228 if (typetail && typetail != void_list_node)
4229 {
4230 if (complain & tf_error)
4231 error_args_num (input_location, fndecl, /*too_many_p=*/false);
4232 return -1;
4233 }
4234 }
4235
4236 return (int) i;
4237 }
4238 \f
4239 /* Build a binary-operation expression, after performing default
4240 conversions on the operands. CODE is the kind of expression to
4241 build. ARG1 and ARG2 are the arguments. ARG1_CODE and ARG2_CODE
4242 are the tree codes which correspond to ARG1 and ARG2 when issuing
4243 warnings about possibly misplaced parentheses. They may differ
4244 from the TREE_CODE of ARG1 and ARG2 if the parser has done constant
4245 folding (e.g., if the parser sees "a | 1 + 1", it may call this
4246 routine with ARG2 being an INTEGER_CST and ARG2_CODE == PLUS_EXPR).
4247 To avoid issuing any parentheses warnings, pass ARG1_CODE and/or
4248 ARG2_CODE as ERROR_MARK. */
4249
4250 tree
4251 build_x_binary_op (const op_location_t &loc, enum tree_code code, tree arg1,
4252 enum tree_code arg1_code, tree arg2,
4253 enum tree_code arg2_code, tree *overload_p,
4254 tsubst_flags_t complain)
4255 {
4256 tree orig_arg1;
4257 tree orig_arg2;
4258 tree expr;
4259 tree overload = NULL_TREE;
4260
4261 orig_arg1 = arg1;
4262 orig_arg2 = arg2;
4263
4264 if (processing_template_decl)
4265 {
4266 if (type_dependent_expression_p (arg1)
4267 || type_dependent_expression_p (arg2))
4268 {
4269 expr = build_min_nt_loc (loc, code, arg1, arg2);
4270 maybe_save_operator_binding (expr);
4271 return expr;
4272 }
4273 arg1 = build_non_dependent_expr (arg1);
4274 arg2 = build_non_dependent_expr (arg2);
4275 }
4276
4277 if (code == DOTSTAR_EXPR)
4278 expr = build_m_component_ref (arg1, arg2, complain);
4279 else
4280 expr = build_new_op (loc, code, LOOKUP_NORMAL, arg1, arg2, NULL_TREE,
4281 &overload, complain);
4282
4283 if (overload_p != NULL)
4284 *overload_p = overload;
4285
4286 /* Check for cases such as x+y<<z which users are likely to
4287 misinterpret. But don't warn about obj << x + y, since that is a
4288 common idiom for I/O. */
4289 if (warn_parentheses
4290 && (complain & tf_warning)
4291 && !processing_template_decl
4292 && !error_operand_p (arg1)
4293 && !error_operand_p (arg2)
4294 && (code != LSHIFT_EXPR
4295 || !CLASS_TYPE_P (TREE_TYPE (arg1))))
4296 warn_about_parentheses (loc, code, arg1_code, orig_arg1,
4297 arg2_code, orig_arg2);
4298
4299 if (processing_template_decl && expr != error_mark_node)
4300 {
4301 if (overload != NULL_TREE)
4302 return (build_min_non_dep_op_overload
4303 (code, expr, overload, orig_arg1, orig_arg2));
4304
4305 return build_min_non_dep (code, expr, orig_arg1, orig_arg2);
4306 }
4307
4308 return expr;
4309 }
4310
4311 /* Build and return an ARRAY_REF expression. */
4312
4313 tree
4314 build_x_array_ref (location_t loc, tree arg1, tree arg2,
4315 tsubst_flags_t complain)
4316 {
4317 tree orig_arg1 = arg1;
4318 tree orig_arg2 = arg2;
4319 tree expr;
4320 tree overload = NULL_TREE;
4321
4322 if (processing_template_decl)
4323 {
4324 if (type_dependent_expression_p (arg1)
4325 || type_dependent_expression_p (arg2))
4326 return build_min_nt_loc (loc, ARRAY_REF, arg1, arg2,
4327 NULL_TREE, NULL_TREE);
4328 arg1 = build_non_dependent_expr (arg1);
4329 arg2 = build_non_dependent_expr (arg2);
4330 }
4331
4332 expr = build_new_op (loc, ARRAY_REF, LOOKUP_NORMAL, arg1, arg2,
4333 NULL_TREE, &overload, complain);
4334
4335 if (processing_template_decl && expr != error_mark_node)
4336 {
4337 if (overload != NULL_TREE)
4338 return (build_min_non_dep_op_overload
4339 (ARRAY_REF, expr, overload, orig_arg1, orig_arg2));
4340
4341 return build_min_non_dep (ARRAY_REF, expr, orig_arg1, orig_arg2,
4342 NULL_TREE, NULL_TREE);
4343 }
4344 return expr;
4345 }
4346
4347 /* Return whether OP is an expression of enum type cast to integer
4348 type. In C++ even unsigned enum types are cast to signed integer
4349 types. We do not want to issue warnings about comparisons between
4350 signed and unsigned types when one of the types is an enum type.
4351 Those warnings are always false positives in practice. */
4352
4353 static bool
4354 enum_cast_to_int (tree op)
4355 {
4356 if (CONVERT_EXPR_P (op)
4357 && TREE_TYPE (op) == integer_type_node
4358 && TREE_CODE (TREE_TYPE (TREE_OPERAND (op, 0))) == ENUMERAL_TYPE
4359 && TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (op, 0))))
4360 return true;
4361
4362 /* The cast may have been pushed into a COND_EXPR. */
4363 if (TREE_CODE (op) == COND_EXPR)
4364 return (enum_cast_to_int (TREE_OPERAND (op, 1))
4365 || enum_cast_to_int (TREE_OPERAND (op, 2)));
4366
4367 return false;
4368 }
4369
4370 /* For the c-common bits. */
4371 tree
4372 build_binary_op (location_t location, enum tree_code code, tree op0, tree op1,
4373 bool /*convert_p*/)
4374 {
4375 return cp_build_binary_op (location, code, op0, op1, tf_warning_or_error);
4376 }
4377
4378 /* Build a vector comparison of ARG0 and ARG1 using CODE opcode
4379 into a value of TYPE type. Comparison is done via VEC_COND_EXPR. */
4380
4381 static tree
4382 build_vec_cmp (tree_code code, tree type,
4383 tree arg0, tree arg1)
4384 {
4385 tree zero_vec = build_zero_cst (type);
4386 tree minus_one_vec = build_minus_one_cst (type);
4387 tree cmp_type = truth_type_for (type);
4388 tree cmp = build2 (code, cmp_type, arg0, arg1);
4389 return build3 (VEC_COND_EXPR, type, cmp, minus_one_vec, zero_vec);
4390 }
4391
4392 /* Possibly warn about an address never being NULL. */
4393
4394 static void
4395 warn_for_null_address (location_t location, tree op, tsubst_flags_t complain)
4396 {
4397 if (!warn_address
4398 || (complain & tf_warning) == 0
4399 || c_inhibit_evaluation_warnings != 0
4400 || TREE_NO_WARNING (op))
4401 return;
4402
4403 tree cop = fold_for_warn (op);
4404
4405 if (TREE_CODE (cop) == ADDR_EXPR
4406 && decl_with_nonnull_addr_p (TREE_OPERAND (cop, 0))
4407 && !TREE_NO_WARNING (cop))
4408 warning_at (location, OPT_Waddress, "the address of %qD will never "
4409 "be NULL", TREE_OPERAND (cop, 0));
4410
4411 if (CONVERT_EXPR_P (op)
4412 && TYPE_REF_P (TREE_TYPE (TREE_OPERAND (op, 0))))
4413 {
4414 tree inner_op = op;
4415 STRIP_NOPS (inner_op);
4416
4417 if (DECL_P (inner_op))
4418 warning_at (location, OPT_Waddress,
4419 "the compiler can assume that the address of "
4420 "%qD will never be NULL", inner_op);
4421 }
4422 }
4423
4424 /* Build a binary-operation expression without default conversions.
4425 CODE is the kind of expression to build.
4426 LOCATION is the location_t of the operator in the source code.
4427 This function differs from `build' in several ways:
4428 the data type of the result is computed and recorded in it,
4429 warnings are generated if arg data types are invalid,
4430 special handling for addition and subtraction of pointers is known,
4431 and some optimization is done (operations on narrow ints
4432 are done in the narrower type when that gives the same result).
4433 Constant folding is also done before the result is returned.
4434
4435 Note that the operands will never have enumeral types
4436 because either they have just had the default conversions performed
4437 or they have both just been converted to some other type in which
4438 the arithmetic is to be done.
4439
4440 C++: must do special pointer arithmetic when implementing
4441 multiple inheritance, and deal with pointer to member functions. */
4442
4443 tree
4444 cp_build_binary_op (const op_location_t &location,
4445 enum tree_code code, tree orig_op0, tree orig_op1,
4446 tsubst_flags_t complain)
4447 {
4448 tree op0, op1;
4449 enum tree_code code0, code1;
4450 tree type0, type1;
4451 const char *invalid_op_diag;
4452
4453 /* Expression code to give to the expression when it is built.
4454 Normally this is CODE, which is what the caller asked for,
4455 but in some special cases we change it. */
4456 enum tree_code resultcode = code;
4457
4458 /* Data type in which the computation is to be performed.
4459 In the simplest cases this is the common type of the arguments. */
4460 tree result_type = NULL_TREE;
4461
4462 /* Nonzero means operands have already been type-converted
4463 in whatever way is necessary.
4464 Zero means they need to be converted to RESULT_TYPE. */
4465 int converted = 0;
4466
4467 /* Nonzero means create the expression with this type, rather than
4468 RESULT_TYPE. */
4469 tree build_type = 0;
4470
4471 /* Nonzero means after finally constructing the expression
4472 convert it to this type. */
4473 tree final_type = 0;
4474
4475 tree result, result_ovl;
4476
4477 /* Nonzero if this is an operation like MIN or MAX which can
4478 safely be computed in short if both args are promoted shorts.
4479 Also implies COMMON.
4480 -1 indicates a bitwise operation; this makes a difference
4481 in the exact conditions for when it is safe to do the operation
4482 in a narrower mode. */
4483 int shorten = 0;
4484
4485 /* Nonzero if this is a comparison operation;
4486 if both args are promoted shorts, compare the original shorts.
4487 Also implies COMMON. */
4488 int short_compare = 0;
4489
4490 /* Nonzero if this is a right-shift operation, which can be computed on the
4491 original short and then promoted if the operand is a promoted short. */
4492 int short_shift = 0;
4493
4494 /* Nonzero means set RESULT_TYPE to the common type of the args. */
4495 int common = 0;
4496
4497 /* True if both operands have arithmetic type. */
4498 bool arithmetic_types_p;
4499
4500 /* Remember whether we're doing / or %. */
4501 bool doing_div_or_mod = false;
4502
4503 /* Remember whether we're doing << or >>. */
4504 bool doing_shift = false;
4505
4506 /* Tree holding instrumentation expression. */
4507 tree instrument_expr = NULL_TREE;
4508
4509 /* Apply default conversions. */
4510 op0 = resolve_nondeduced_context (orig_op0, complain);
4511 op1 = resolve_nondeduced_context (orig_op1, complain);
4512
4513 if (code == TRUTH_AND_EXPR || code == TRUTH_ANDIF_EXPR
4514 || code == TRUTH_OR_EXPR || code == TRUTH_ORIF_EXPR
4515 || code == TRUTH_XOR_EXPR)
4516 {
4517 if (!really_overloaded_fn (op0) && !VOID_TYPE_P (TREE_TYPE (op0)))
4518 op0 = decay_conversion (op0, complain);
4519 if (!really_overloaded_fn (op1) && !VOID_TYPE_P (TREE_TYPE (op1)))
4520 op1 = decay_conversion (op1, complain);
4521 }
4522 else
4523 {
4524 if (!really_overloaded_fn (op0) && !VOID_TYPE_P (TREE_TYPE (op0)))
4525 op0 = cp_default_conversion (op0, complain);
4526 if (!really_overloaded_fn (op1) && !VOID_TYPE_P (TREE_TYPE (op1)))
4527 op1 = cp_default_conversion (op1, complain);
4528 }
4529
4530 /* Strip NON_LVALUE_EXPRs, etc., since we aren't using as an lvalue. */
4531 STRIP_TYPE_NOPS (op0);
4532 STRIP_TYPE_NOPS (op1);
4533
4534 /* DTRT if one side is an overloaded function, but complain about it. */
4535 if (type_unknown_p (op0))
4536 {
4537 tree t = instantiate_type (TREE_TYPE (op1), op0, tf_none);
4538 if (t != error_mark_node)
4539 {
4540 if (complain & tf_error)
4541 permerror (location,
4542 "assuming cast to type %qT from overloaded function",
4543 TREE_TYPE (t));
4544 op0 = t;
4545 }
4546 }
4547 if (type_unknown_p (op1))
4548 {
4549 tree t = instantiate_type (TREE_TYPE (op0), op1, tf_none);
4550 if (t != error_mark_node)
4551 {
4552 if (complain & tf_error)
4553 permerror (location,
4554 "assuming cast to type %qT from overloaded function",
4555 TREE_TYPE (t));
4556 op1 = t;
4557 }
4558 }
4559
4560 type0 = TREE_TYPE (op0);
4561 type1 = TREE_TYPE (op1);
4562
4563 /* The expression codes of the data types of the arguments tell us
4564 whether the arguments are integers, floating, pointers, etc. */
4565 code0 = TREE_CODE (type0);
4566 code1 = TREE_CODE (type1);
4567
4568 /* If an error was already reported for one of the arguments,
4569 avoid reporting another error. */
4570 if (code0 == ERROR_MARK || code1 == ERROR_MARK)
4571 return error_mark_node;
4572
4573 if ((invalid_op_diag
4574 = targetm.invalid_binary_op (code, type0, type1)))
4575 {
4576 if (complain & tf_error)
4577 error (invalid_op_diag);
4578 return error_mark_node;
4579 }
4580
4581 /* Issue warnings about peculiar, but valid, uses of NULL. */
4582 if ((null_node_p (orig_op0) || null_node_p (orig_op1))
4583 /* It's reasonable to use pointer values as operands of &&
4584 and ||, so NULL is no exception. */
4585 && code != TRUTH_ANDIF_EXPR && code != TRUTH_ORIF_EXPR
4586 && ( /* Both are NULL (or 0) and the operation was not a
4587 comparison or a pointer subtraction. */
4588 (null_ptr_cst_p (orig_op0) && null_ptr_cst_p (orig_op1)
4589 && code != EQ_EXPR && code != NE_EXPR && code != MINUS_EXPR)
4590 /* Or if one of OP0 or OP1 is neither a pointer nor NULL. */
4591 || (!null_ptr_cst_p (orig_op0)
4592 && !TYPE_PTR_OR_PTRMEM_P (type0))
4593 || (!null_ptr_cst_p (orig_op1)
4594 && !TYPE_PTR_OR_PTRMEM_P (type1)))
4595 && (complain & tf_warning))
4596 {
4597 location_t loc =
4598 expansion_point_location_if_in_system_header (input_location);
4599
4600 warning_at (loc, OPT_Wpointer_arith, "NULL used in arithmetic");
4601 }
4602
4603 /* In case when one of the operands of the binary operation is
4604 a vector and another is a scalar -- convert scalar to vector. */
4605 if ((gnu_vector_type_p (type0) && code1 != VECTOR_TYPE)
4606 || (gnu_vector_type_p (type1) && code0 != VECTOR_TYPE))
4607 {
4608 enum stv_conv convert_flag = scalar_to_vector (location, code, op0, op1,
4609 complain & tf_error);
4610
4611 switch (convert_flag)
4612 {
4613 case stv_error:
4614 return error_mark_node;
4615 case stv_firstarg:
4616 {
4617 op0 = convert (TREE_TYPE (type1), op0);
4618 op0 = save_expr (op0);
4619 op0 = build_vector_from_val (type1, op0);
4620 type0 = TREE_TYPE (op0);
4621 code0 = TREE_CODE (type0);
4622 converted = 1;
4623 break;
4624 }
4625 case stv_secondarg:
4626 {
4627 op1 = convert (TREE_TYPE (type0), op1);
4628 op1 = save_expr (op1);
4629 op1 = build_vector_from_val (type0, op1);
4630 type1 = TREE_TYPE (op1);
4631 code1 = TREE_CODE (type1);
4632 converted = 1;
4633 break;
4634 }
4635 default:
4636 break;
4637 }
4638 }
4639
4640 switch (code)
4641 {
4642 case MINUS_EXPR:
4643 /* Subtraction of two similar pointers.
4644 We must subtract them as integers, then divide by object size. */
4645 if (code0 == POINTER_TYPE && code1 == POINTER_TYPE
4646 && same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (type0),
4647 TREE_TYPE (type1)))
4648 {
4649 result = pointer_diff (location, op0, op1,
4650 common_pointer_type (type0, type1), complain,
4651 &instrument_expr);
4652 if (instrument_expr != NULL)
4653 result = build2 (COMPOUND_EXPR, TREE_TYPE (result),
4654 instrument_expr, result);
4655
4656 return result;
4657 }
4658 /* In all other cases except pointer - int, the usual arithmetic
4659 rules apply. */
4660 else if (!(code0 == POINTER_TYPE && code1 == INTEGER_TYPE))
4661 {
4662 common = 1;
4663 break;
4664 }
4665 /* The pointer - int case is just like pointer + int; fall
4666 through. */
4667 gcc_fallthrough ();
4668 case PLUS_EXPR:
4669 if ((code0 == POINTER_TYPE || code1 == POINTER_TYPE)
4670 && (code0 == INTEGER_TYPE || code1 == INTEGER_TYPE))
4671 {
4672 tree ptr_operand;
4673 tree int_operand;
4674 ptr_operand = ((code0 == POINTER_TYPE) ? op0 : op1);
4675 int_operand = ((code0 == INTEGER_TYPE) ? op0 : op1);
4676 if (processing_template_decl)
4677 {
4678 result_type = TREE_TYPE (ptr_operand);
4679 break;
4680 }
4681 return cp_pointer_int_sum (location, code,
4682 ptr_operand,
4683 int_operand,
4684 complain);
4685 }
4686 common = 1;
4687 break;
4688
4689 case MULT_EXPR:
4690 common = 1;
4691 break;
4692
4693 case TRUNC_DIV_EXPR:
4694 case CEIL_DIV_EXPR:
4695 case FLOOR_DIV_EXPR:
4696 case ROUND_DIV_EXPR:
4697 case EXACT_DIV_EXPR:
4698 if (TREE_CODE (op0) == SIZEOF_EXPR && TREE_CODE (op1) == SIZEOF_EXPR)
4699 {
4700 tree type0 = TREE_OPERAND (op0, 0);
4701 tree type1 = TREE_OPERAND (op1, 0);
4702 tree first_arg = type0;
4703 if (!TYPE_P (type0))
4704 type0 = TREE_TYPE (type0);
4705 if (!TYPE_P (type1))
4706 type1 = TREE_TYPE (type1);
4707 if (INDIRECT_TYPE_P (type0) && same_type_p (TREE_TYPE (type0), type1))
4708 {
4709 STRIP_ANY_LOCATION_WRAPPER (first_arg);
4710 if (!(TREE_CODE (first_arg) == PARM_DECL
4711 && DECL_ARRAY_PARAMETER_P (first_arg)
4712 && warn_sizeof_array_argument)
4713 && (complain & tf_warning))
4714 {
4715 auto_diagnostic_group d;
4716 if (warning_at (location, OPT_Wsizeof_pointer_div,
4717 "division %<sizeof (%T) / sizeof (%T)%> does "
4718 "not compute the number of array elements",
4719 type0, type1))
4720 if (DECL_P (first_arg))
4721 inform (DECL_SOURCE_LOCATION (first_arg),
4722 "first %<sizeof%> operand was declared here");
4723 }
4724 }
4725 }
4726
4727 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE
4728 || code0 == COMPLEX_TYPE || code0 == VECTOR_TYPE)
4729 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
4730 || code1 == COMPLEX_TYPE || code1 == VECTOR_TYPE))
4731 {
4732 enum tree_code tcode0 = code0, tcode1 = code1;
4733 doing_div_or_mod = true;
4734 warn_for_div_by_zero (location, fold_for_warn (op1));
4735
4736 if (tcode0 == COMPLEX_TYPE || tcode0 == VECTOR_TYPE)
4737 tcode0 = TREE_CODE (TREE_TYPE (TREE_TYPE (op0)));
4738 if (tcode1 == COMPLEX_TYPE || tcode1 == VECTOR_TYPE)
4739 tcode1 = TREE_CODE (TREE_TYPE (TREE_TYPE (op1)));
4740
4741 if (!(tcode0 == INTEGER_TYPE && tcode1 == INTEGER_TYPE))
4742 resultcode = RDIV_EXPR;
4743 else
4744 {
4745 /* When dividing two signed integers, we have to promote to int.
4746 unless we divide by a constant != -1. Note that default
4747 conversion will have been performed on the operands at this
4748 point, so we have to dig out the original type to find out if
4749 it was unsigned. */
4750 tree stripped_op1 = tree_strip_any_location_wrapper (op1);
4751 shorten = ((TREE_CODE (op0) == NOP_EXPR
4752 && TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (op0, 0))))
4753 || (TREE_CODE (stripped_op1) == INTEGER_CST
4754 && ! integer_all_onesp (stripped_op1)));
4755 }
4756
4757 common = 1;
4758 }
4759 break;
4760
4761 case BIT_AND_EXPR:
4762 case BIT_IOR_EXPR:
4763 case BIT_XOR_EXPR:
4764 if ((code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
4765 || (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE
4766 && !VECTOR_FLOAT_TYPE_P (type0)
4767 && !VECTOR_FLOAT_TYPE_P (type1)))
4768 shorten = -1;
4769 break;
4770
4771 case TRUNC_MOD_EXPR:
4772 case FLOOR_MOD_EXPR:
4773 doing_div_or_mod = true;
4774 warn_for_div_by_zero (location, fold_for_warn (op1));
4775
4776 if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE
4777 && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE
4778 && TREE_CODE (TREE_TYPE (type1)) == INTEGER_TYPE)
4779 common = 1;
4780 else if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
4781 {
4782 /* Although it would be tempting to shorten always here, that loses
4783 on some targets, since the modulo instruction is undefined if the
4784 quotient can't be represented in the computation mode. We shorten
4785 only if unsigned or if dividing by something we know != -1. */
4786 tree stripped_op1 = tree_strip_any_location_wrapper (op1);
4787 shorten = ((TREE_CODE (op0) == NOP_EXPR
4788 && TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (op0, 0))))
4789 || (TREE_CODE (stripped_op1) == INTEGER_CST
4790 && ! integer_all_onesp (stripped_op1)));
4791 common = 1;
4792 }
4793 break;
4794
4795 case TRUTH_ANDIF_EXPR:
4796 case TRUTH_ORIF_EXPR:
4797 case TRUTH_AND_EXPR:
4798 case TRUTH_OR_EXPR:
4799 if (!VECTOR_TYPE_P (type0) && gnu_vector_type_p (type1))
4800 {
4801 if (!COMPARISON_CLASS_P (op1))
4802 op1 = cp_build_binary_op (EXPR_LOCATION (op1), NE_EXPR, op1,
4803 build_zero_cst (type1), complain);
4804 if (code == TRUTH_ANDIF_EXPR)
4805 {
4806 tree z = build_zero_cst (TREE_TYPE (op1));
4807 return build_conditional_expr (location, op0, op1, z, complain);
4808 }
4809 else if (code == TRUTH_ORIF_EXPR)
4810 {
4811 tree m1 = build_all_ones_cst (TREE_TYPE (op1));
4812 return build_conditional_expr (location, op0, m1, op1, complain);
4813 }
4814 else
4815 gcc_unreachable ();
4816 }
4817 if (gnu_vector_type_p (type0)
4818 && (!VECTOR_TYPE_P (type1) || gnu_vector_type_p (type1)))
4819 {
4820 if (!COMPARISON_CLASS_P (op0))
4821 op0 = cp_build_binary_op (EXPR_LOCATION (op0), NE_EXPR, op0,
4822 build_zero_cst (type0), complain);
4823 if (!VECTOR_TYPE_P (type1))
4824 {
4825 tree m1 = build_all_ones_cst (TREE_TYPE (op0));
4826 tree z = build_zero_cst (TREE_TYPE (op0));
4827 op1 = build_conditional_expr (location, op1, m1, z, complain);
4828 }
4829 else if (!COMPARISON_CLASS_P (op1))
4830 op1 = cp_build_binary_op (EXPR_LOCATION (op1), NE_EXPR, op1,
4831 build_zero_cst (type1), complain);
4832
4833 if (code == TRUTH_ANDIF_EXPR)
4834 code = BIT_AND_EXPR;
4835 else if (code == TRUTH_ORIF_EXPR)
4836 code = BIT_IOR_EXPR;
4837 else
4838 gcc_unreachable ();
4839
4840 return cp_build_binary_op (location, code, op0, op1, complain);
4841 }
4842
4843 result_type = boolean_type_node;
4844 break;
4845
4846 /* Shift operations: result has same type as first operand;
4847 always convert second operand to int.
4848 Also set SHORT_SHIFT if shifting rightward. */
4849
4850 case RSHIFT_EXPR:
4851 if (gnu_vector_type_p (type0)
4852 && code1 == INTEGER_TYPE
4853 && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE)
4854 {
4855 result_type = type0;
4856 converted = 1;
4857 }
4858 else if (gnu_vector_type_p (type0)
4859 && gnu_vector_type_p (type1)
4860 && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE
4861 && TREE_CODE (TREE_TYPE (type1)) == INTEGER_TYPE
4862 && known_eq (TYPE_VECTOR_SUBPARTS (type0),
4863 TYPE_VECTOR_SUBPARTS (type1)))
4864 {
4865 result_type = type0;
4866 converted = 1;
4867 }
4868 else if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
4869 {
4870 tree const_op1 = fold_for_warn (op1);
4871 if (TREE_CODE (const_op1) != INTEGER_CST)
4872 const_op1 = op1;
4873 result_type = type0;
4874 doing_shift = true;
4875 if (TREE_CODE (const_op1) == INTEGER_CST)
4876 {
4877 if (tree_int_cst_lt (const_op1, integer_zero_node))
4878 {
4879 if ((complain & tf_warning)
4880 && c_inhibit_evaluation_warnings == 0)
4881 warning_at (location, OPT_Wshift_count_negative,
4882 "right shift count is negative");
4883 }
4884 else
4885 {
4886 if (!integer_zerop (const_op1))
4887 short_shift = 1;
4888
4889 if (compare_tree_int (const_op1, TYPE_PRECISION (type0)) >= 0
4890 && (complain & tf_warning)
4891 && c_inhibit_evaluation_warnings == 0)
4892 warning_at (location, OPT_Wshift_count_overflow,
4893 "right shift count >= width of type");
4894 }
4895 }
4896 /* Avoid converting op1 to result_type later. */
4897 converted = 1;
4898 }
4899 break;
4900
4901 case LSHIFT_EXPR:
4902 if (gnu_vector_type_p (type0)
4903 && code1 == INTEGER_TYPE
4904 && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE)
4905 {
4906 result_type = type0;
4907 converted = 1;
4908 }
4909 else if (gnu_vector_type_p (type0)
4910 && gnu_vector_type_p (type1)
4911 && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE
4912 && TREE_CODE (TREE_TYPE (type1)) == INTEGER_TYPE
4913 && known_eq (TYPE_VECTOR_SUBPARTS (type0),
4914 TYPE_VECTOR_SUBPARTS (type1)))
4915 {
4916 result_type = type0;
4917 converted = 1;
4918 }
4919 else if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
4920 {
4921 tree const_op0 = fold_for_warn (op0);
4922 if (TREE_CODE (const_op0) != INTEGER_CST)
4923 const_op0 = op0;
4924 tree const_op1 = fold_for_warn (op1);
4925 if (TREE_CODE (const_op1) != INTEGER_CST)
4926 const_op1 = op1;
4927 result_type = type0;
4928 doing_shift = true;
4929 if (TREE_CODE (const_op0) == INTEGER_CST
4930 && tree_int_cst_sgn (const_op0) < 0
4931 && (complain & tf_warning)
4932 && c_inhibit_evaluation_warnings == 0)
4933 warning_at (location, OPT_Wshift_negative_value,
4934 "left shift of negative value");
4935 if (TREE_CODE (const_op1) == INTEGER_CST)
4936 {
4937 if (tree_int_cst_lt (const_op1, integer_zero_node))
4938 {
4939 if ((complain & tf_warning)
4940 && c_inhibit_evaluation_warnings == 0)
4941 warning_at (location, OPT_Wshift_count_negative,
4942 "left shift count is negative");
4943 }
4944 else if (compare_tree_int (const_op1,
4945 TYPE_PRECISION (type0)) >= 0)
4946 {
4947 if ((complain & tf_warning)
4948 && c_inhibit_evaluation_warnings == 0)
4949 warning_at (location, OPT_Wshift_count_overflow,
4950 "left shift count >= width of type");
4951 }
4952 else if (TREE_CODE (const_op0) == INTEGER_CST
4953 && (complain & tf_warning))
4954 maybe_warn_shift_overflow (location, const_op0, const_op1);
4955 }
4956 /* Avoid converting op1 to result_type later. */
4957 converted = 1;
4958 }
4959 break;
4960
4961 case EQ_EXPR:
4962 case NE_EXPR:
4963 if (gnu_vector_type_p (type0) && gnu_vector_type_p (type1))
4964 goto vector_compare;
4965 if ((complain & tf_warning)
4966 && c_inhibit_evaluation_warnings == 0
4967 && (FLOAT_TYPE_P (type0) || FLOAT_TYPE_P (type1)))
4968 warning_at (location, OPT_Wfloat_equal,
4969 "comparing floating-point with %<==%> "
4970 "or %<!=%> is unsafe");
4971 if (complain & tf_warning)
4972 {
4973 tree stripped_orig_op0 = tree_strip_any_location_wrapper (orig_op0);
4974 tree stripped_orig_op1 = tree_strip_any_location_wrapper (orig_op1);
4975 if ((TREE_CODE (stripped_orig_op0) == STRING_CST
4976 && !integer_zerop (cp_fully_fold (op1)))
4977 || (TREE_CODE (stripped_orig_op1) == STRING_CST
4978 && !integer_zerop (cp_fully_fold (op0))))
4979 warning_at (location, OPT_Waddress,
4980 "comparison with string literal results in "
4981 "unspecified behavior");
4982 }
4983
4984 build_type = boolean_type_node;
4985 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE
4986 || code0 == COMPLEX_TYPE || code0 == ENUMERAL_TYPE)
4987 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
4988 || code1 == COMPLEX_TYPE || code1 == ENUMERAL_TYPE))
4989 short_compare = 1;
4990 else if (((code0 == POINTER_TYPE || TYPE_PTRDATAMEM_P (type0))
4991 && null_ptr_cst_p (orig_op1))
4992 /* Handle, eg, (void*)0 (c++/43906), and more. */
4993 || (code0 == POINTER_TYPE
4994 && TYPE_PTR_P (type1) && integer_zerop (op1)))
4995 {
4996 if (TYPE_PTR_P (type1))
4997 result_type = composite_pointer_type (location,
4998 type0, type1, op0, op1,
4999 CPO_COMPARISON, complain);
5000 else
5001 result_type = type0;
5002
5003 if (char_type_p (TREE_TYPE (orig_op1)))
5004 {
5005 auto_diagnostic_group d;
5006 if (warning_at (location, OPT_Wpointer_compare,
5007 "comparison between pointer and zero character "
5008 "constant"))
5009 inform (location,
5010 "did you mean to dereference the pointer?");
5011 }
5012 warn_for_null_address (location, op0, complain);
5013 }
5014 else if (((code1 == POINTER_TYPE || TYPE_PTRDATAMEM_P (type1))
5015 && null_ptr_cst_p (orig_op0))
5016 /* Handle, eg, (void*)0 (c++/43906), and more. */
5017 || (code1 == POINTER_TYPE
5018 && TYPE_PTR_P (type0) && integer_zerop (op0)))
5019 {
5020 if (TYPE_PTR_P (type0))
5021 result_type = composite_pointer_type (location,
5022 type0, type1, op0, op1,
5023 CPO_COMPARISON, complain);
5024 else
5025 result_type = type1;
5026
5027 if (char_type_p (TREE_TYPE (orig_op0)))
5028 {
5029 auto_diagnostic_group d;
5030 if (warning_at (location, OPT_Wpointer_compare,
5031 "comparison between pointer and zero character "
5032 "constant"))
5033 inform (location,
5034 "did you mean to dereference the pointer?");
5035 }
5036 warn_for_null_address (location, op1, complain);
5037 }
5038 else if ((code0 == POINTER_TYPE && code1 == POINTER_TYPE)
5039 || (TYPE_PTRDATAMEM_P (type0) && TYPE_PTRDATAMEM_P (type1)))
5040 result_type = composite_pointer_type (location,
5041 type0, type1, op0, op1,
5042 CPO_COMPARISON, complain);
5043 else if (null_ptr_cst_p (orig_op0) && null_ptr_cst_p (orig_op1))
5044 /* One of the operands must be of nullptr_t type. */
5045 result_type = TREE_TYPE (nullptr_node);
5046 else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
5047 {
5048 result_type = type0;
5049 if (complain & tf_error)
5050 permerror (location, "ISO C++ forbids comparison between "
5051 "pointer and integer");
5052 else
5053 return error_mark_node;
5054 }
5055 else if (code0 == INTEGER_TYPE && code1 == POINTER_TYPE)
5056 {
5057 result_type = type1;
5058 if (complain & tf_error)
5059 permerror (location, "ISO C++ forbids comparison between "
5060 "pointer and integer");
5061 else
5062 return error_mark_node;
5063 }
5064 else if (TYPE_PTRMEMFUNC_P (type0) && null_ptr_cst_p (orig_op1))
5065 {
5066 if (TARGET_PTRMEMFUNC_VBIT_LOCATION
5067 == ptrmemfunc_vbit_in_delta)
5068 {
5069 tree pfn0, delta0, e1, e2;
5070
5071 if (TREE_SIDE_EFFECTS (op0))
5072 op0 = cp_save_expr (op0);
5073
5074 pfn0 = pfn_from_ptrmemfunc (op0);
5075 delta0 = delta_from_ptrmemfunc (op0);
5076 e1 = cp_build_binary_op (location,
5077 EQ_EXPR,
5078 pfn0,
5079 build_zero_cst (TREE_TYPE (pfn0)),
5080 complain);
5081 e2 = cp_build_binary_op (location,
5082 BIT_AND_EXPR,
5083 delta0,
5084 integer_one_node,
5085 complain);
5086
5087 if (complain & tf_warning)
5088 maybe_warn_zero_as_null_pointer_constant (op1, input_location);
5089
5090 e2 = cp_build_binary_op (location,
5091 EQ_EXPR, e2, integer_zero_node,
5092 complain);
5093 op0 = cp_build_binary_op (location,
5094 TRUTH_ANDIF_EXPR, e1, e2,
5095 complain);
5096 op1 = cp_convert (TREE_TYPE (op0), integer_one_node, complain);
5097 }
5098 else
5099 {
5100 op0 = build_ptrmemfunc_access_expr (op0, pfn_identifier);
5101 op1 = cp_convert (TREE_TYPE (op0), op1, complain);
5102 }
5103 result_type = TREE_TYPE (op0);
5104 }
5105 else if (TYPE_PTRMEMFUNC_P (type1) && null_ptr_cst_p (orig_op0))
5106 return cp_build_binary_op (location, code, op1, op0, complain);
5107 else if (TYPE_PTRMEMFUNC_P (type0) && TYPE_PTRMEMFUNC_P (type1))
5108 {
5109 tree type;
5110 /* E will be the final comparison. */
5111 tree e;
5112 /* E1 and E2 are for scratch. */
5113 tree e1;
5114 tree e2;
5115 tree pfn0;
5116 tree pfn1;
5117 tree delta0;
5118 tree delta1;
5119
5120 type = composite_pointer_type (location, type0, type1, op0, op1,
5121 CPO_COMPARISON, complain);
5122
5123 if (!same_type_p (TREE_TYPE (op0), type))
5124 op0 = cp_convert_and_check (type, op0, complain);
5125 if (!same_type_p (TREE_TYPE (op1), type))
5126 op1 = cp_convert_and_check (type, op1, complain);
5127
5128 if (op0 == error_mark_node || op1 == error_mark_node)
5129 return error_mark_node;
5130
5131 if (TREE_SIDE_EFFECTS (op0))
5132 op0 = save_expr (op0);
5133 if (TREE_SIDE_EFFECTS (op1))
5134 op1 = save_expr (op1);
5135
5136 pfn0 = pfn_from_ptrmemfunc (op0);
5137 pfn0 = cp_fully_fold (pfn0);
5138 /* Avoid -Waddress warnings (c++/64877). */
5139 if (TREE_CODE (pfn0) == ADDR_EXPR)
5140 TREE_NO_WARNING (pfn0) = 1;
5141 pfn1 = pfn_from_ptrmemfunc (op1);
5142 pfn1 = cp_fully_fold (pfn1);
5143 delta0 = delta_from_ptrmemfunc (op0);
5144 delta1 = delta_from_ptrmemfunc (op1);
5145 if (TARGET_PTRMEMFUNC_VBIT_LOCATION
5146 == ptrmemfunc_vbit_in_delta)
5147 {
5148 /* We generate:
5149
5150 (op0.pfn == op1.pfn
5151 && ((op0.delta == op1.delta)
5152 || (!op0.pfn && op0.delta & 1 == 0
5153 && op1.delta & 1 == 0))
5154
5155 The reason for the `!op0.pfn' bit is that a NULL
5156 pointer-to-member is any member with a zero PFN and
5157 LSB of the DELTA field is 0. */
5158
5159 e1 = cp_build_binary_op (location, BIT_AND_EXPR,
5160 delta0,
5161 integer_one_node,
5162 complain);
5163 e1 = cp_build_binary_op (location,
5164 EQ_EXPR, e1, integer_zero_node,
5165 complain);
5166 e2 = cp_build_binary_op (location, BIT_AND_EXPR,
5167 delta1,
5168 integer_one_node,
5169 complain);
5170 e2 = cp_build_binary_op (location,
5171 EQ_EXPR, e2, integer_zero_node,
5172 complain);
5173 e1 = cp_build_binary_op (location,
5174 TRUTH_ANDIF_EXPR, e2, e1,
5175 complain);
5176 e2 = cp_build_binary_op (location, EQ_EXPR,
5177 pfn0,
5178 build_zero_cst (TREE_TYPE (pfn0)),
5179 complain);
5180 e2 = cp_build_binary_op (location,
5181 TRUTH_ANDIF_EXPR, e2, e1, complain);
5182 e1 = cp_build_binary_op (location,
5183 EQ_EXPR, delta0, delta1, complain);
5184 e1 = cp_build_binary_op (location,
5185 TRUTH_ORIF_EXPR, e1, e2, complain);
5186 }
5187 else
5188 {
5189 /* We generate:
5190
5191 (op0.pfn == op1.pfn
5192 && (!op0.pfn || op0.delta == op1.delta))
5193
5194 The reason for the `!op0.pfn' bit is that a NULL
5195 pointer-to-member is any member with a zero PFN; the
5196 DELTA field is unspecified. */
5197
5198 e1 = cp_build_binary_op (location,
5199 EQ_EXPR, delta0, delta1, complain);
5200 e2 = cp_build_binary_op (location,
5201 EQ_EXPR,
5202 pfn0,
5203 build_zero_cst (TREE_TYPE (pfn0)),
5204 complain);
5205 e1 = cp_build_binary_op (location,
5206 TRUTH_ORIF_EXPR, e1, e2, complain);
5207 }
5208 e2 = build2 (EQ_EXPR, boolean_type_node, pfn0, pfn1);
5209 e = cp_build_binary_op (location,
5210 TRUTH_ANDIF_EXPR, e2, e1, complain);
5211 if (code == EQ_EXPR)
5212 return e;
5213 return cp_build_binary_op (location,
5214 EQ_EXPR, e, integer_zero_node, complain);
5215 }
5216 else
5217 {
5218 gcc_assert (!TYPE_PTRMEMFUNC_P (type0)
5219 || !same_type_p (TYPE_PTRMEMFUNC_FN_TYPE (type0),
5220 type1));
5221 gcc_assert (!TYPE_PTRMEMFUNC_P (type1)
5222 || !same_type_p (TYPE_PTRMEMFUNC_FN_TYPE (type1),
5223 type0));
5224 }
5225
5226 break;
5227
5228 case MAX_EXPR:
5229 case MIN_EXPR:
5230 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE)
5231 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE))
5232 shorten = 1;
5233 else if (code0 == POINTER_TYPE && code1 == POINTER_TYPE)
5234 result_type = composite_pointer_type (location,
5235 type0, type1, op0, op1,
5236 CPO_COMPARISON, complain);
5237 break;
5238
5239 case LE_EXPR:
5240 case GE_EXPR:
5241 case LT_EXPR:
5242 case GT_EXPR:
5243 case SPACESHIP_EXPR:
5244 if (TREE_CODE (orig_op0) == STRING_CST
5245 || TREE_CODE (orig_op1) == STRING_CST)
5246 {
5247 if (complain & tf_warning)
5248 warning_at (location, OPT_Waddress,
5249 "comparison with string literal results "
5250 "in unspecified behavior");
5251 }
5252
5253 if (gnu_vector_type_p (type0) && gnu_vector_type_p (type1))
5254 {
5255 vector_compare:
5256 tree intt;
5257 if (!same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (type0),
5258 TREE_TYPE (type1))
5259 && !vector_types_compatible_elements_p (type0, type1))
5260 {
5261 if (complain & tf_error)
5262 {
5263 error_at (location, "comparing vectors with different "
5264 "element types");
5265 inform (location, "operand types are %qT and %qT",
5266 type0, type1);
5267 }
5268 return error_mark_node;
5269 }
5270
5271 if (maybe_ne (TYPE_VECTOR_SUBPARTS (type0),
5272 TYPE_VECTOR_SUBPARTS (type1)))
5273 {
5274 if (complain & tf_error)
5275 {
5276 error_at (location, "comparing vectors with different "
5277 "number of elements");
5278 inform (location, "operand types are %qT and %qT",
5279 type0, type1);
5280 }
5281 return error_mark_node;
5282 }
5283
5284 /* It's not precisely specified how the usual arithmetic
5285 conversions apply to the vector types. Here, we use
5286 the unsigned type if one of the operands is signed and
5287 the other one is unsigned. */
5288 if (TYPE_UNSIGNED (type0) != TYPE_UNSIGNED (type1))
5289 {
5290 if (!TYPE_UNSIGNED (type0))
5291 op0 = build1 (VIEW_CONVERT_EXPR, type1, op0);
5292 else
5293 op1 = build1 (VIEW_CONVERT_EXPR, type0, op1);
5294 warning_at (location, OPT_Wsign_compare, "comparison between "
5295 "types %qT and %qT", type0, type1);
5296 }
5297
5298 if (resultcode == SPACESHIP_EXPR)
5299 {
5300 if (complain & tf_error)
5301 sorry_at (location, "three-way comparison of vectors");
5302 return error_mark_node;
5303 }
5304
5305 /* Always construct signed integer vector type. */
5306 intt = c_common_type_for_size
5307 (GET_MODE_BITSIZE (SCALAR_TYPE_MODE (TREE_TYPE (type0))), 0);
5308 if (!intt)
5309 {
5310 if (complain & tf_error)
5311 error_at (location, "could not find an integer type "
5312 "of the same size as %qT", TREE_TYPE (type0));
5313 return error_mark_node;
5314 }
5315 result_type = build_opaque_vector_type (intt,
5316 TYPE_VECTOR_SUBPARTS (type0));
5317 return build_vec_cmp (resultcode, result_type, op0, op1);
5318 }
5319 build_type = boolean_type_node;
5320 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE
5321 || code0 == ENUMERAL_TYPE)
5322 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
5323 || code1 == ENUMERAL_TYPE))
5324 short_compare = 1;
5325 else if (code0 == POINTER_TYPE && code1 == POINTER_TYPE)
5326 result_type = composite_pointer_type (location,
5327 type0, type1, op0, op1,
5328 CPO_COMPARISON, complain);
5329 else if (code0 == POINTER_TYPE && null_ptr_cst_p (orig_op1))
5330 {
5331 /* Core Issue 1512 made this ill-formed. */
5332 if (complain & tf_error)
5333 error_at (location, "ordered comparison of pointer with "
5334 "integer zero (%qT and %qT)", type0, type1);
5335 return error_mark_node;
5336 }
5337 else if (code1 == POINTER_TYPE && null_ptr_cst_p (orig_op0))
5338 {
5339 /* Core Issue 1512 made this ill-formed. */
5340 if (complain & tf_error)
5341 error_at (location, "ordered comparison of pointer with "
5342 "integer zero (%qT and %qT)", type0, type1);
5343 return error_mark_node;
5344 }
5345 else if (null_ptr_cst_p (orig_op0) && null_ptr_cst_p (orig_op1))
5346 /* One of the operands must be of nullptr_t type. */
5347 result_type = TREE_TYPE (nullptr_node);
5348 else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
5349 {
5350 result_type = type0;
5351 if (complain & tf_error)
5352 permerror (location, "ISO C++ forbids comparison between "
5353 "pointer and integer");
5354 else
5355 return error_mark_node;
5356 }
5357 else if (code0 == INTEGER_TYPE && code1 == POINTER_TYPE)
5358 {
5359 result_type = type1;
5360 if (complain & tf_error)
5361 permerror (location, "ISO C++ forbids comparison between "
5362 "pointer and integer");
5363 else
5364 return error_mark_node;
5365 }
5366
5367 if ((code0 == POINTER_TYPE || code1 == POINTER_TYPE)
5368 && !processing_template_decl
5369 && sanitize_flags_p (SANITIZE_POINTER_COMPARE))
5370 {
5371 op0 = save_expr (op0);
5372 op1 = save_expr (op1);
5373
5374 tree tt = builtin_decl_explicit (BUILT_IN_ASAN_POINTER_COMPARE);
5375 instrument_expr = build_call_expr_loc (location, tt, 2, op0, op1);
5376 }
5377
5378 break;
5379
5380 case UNORDERED_EXPR:
5381 case ORDERED_EXPR:
5382 case UNLT_EXPR:
5383 case UNLE_EXPR:
5384 case UNGT_EXPR:
5385 case UNGE_EXPR:
5386 case UNEQ_EXPR:
5387 build_type = integer_type_node;
5388 if (code0 != REAL_TYPE || code1 != REAL_TYPE)
5389 {
5390 if (complain & tf_error)
5391 error ("unordered comparison on non-floating-point argument");
5392 return error_mark_node;
5393 }
5394 common = 1;
5395 break;
5396
5397 default:
5398 break;
5399 }
5400
5401 if (((code0 == INTEGER_TYPE || code0 == REAL_TYPE || code0 == COMPLEX_TYPE
5402 || code0 == ENUMERAL_TYPE)
5403 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
5404 || code1 == COMPLEX_TYPE || code1 == ENUMERAL_TYPE)))
5405 arithmetic_types_p = 1;
5406 else
5407 {
5408 arithmetic_types_p = 0;
5409 /* Vector arithmetic is only allowed when both sides are vectors. */
5410 if (gnu_vector_type_p (type0) && gnu_vector_type_p (type1))
5411 {
5412 if (!tree_int_cst_equal (TYPE_SIZE (type0), TYPE_SIZE (type1))
5413 || !vector_types_compatible_elements_p (type0, type1))
5414 {
5415 if (complain & tf_error)
5416 {
5417 /* "location" already embeds the locations of the
5418 operands, so we don't need to add them separately
5419 to richloc. */
5420 rich_location richloc (line_table, location);
5421 binary_op_error (&richloc, code, type0, type1);
5422 }
5423 return error_mark_node;
5424 }
5425 arithmetic_types_p = 1;
5426 }
5427 }
5428 /* Determine the RESULT_TYPE, if it is not already known. */
5429 if (!result_type
5430 && arithmetic_types_p
5431 && (shorten || common || short_compare))
5432 {
5433 result_type = cp_common_type (type0, type1);
5434 if (complain & tf_warning)
5435 do_warn_double_promotion (result_type, type0, type1,
5436 "implicit conversion from %qH to %qI "
5437 "to match other operand of binary "
5438 "expression",
5439 location);
5440 }
5441
5442 if (code == SPACESHIP_EXPR)
5443 {
5444 iloc_sentinel s (location);
5445
5446 tree orig_type0 = TREE_TYPE (orig_op0);
5447 tree_code orig_code0 = TREE_CODE (orig_type0);
5448 tree orig_type1 = TREE_TYPE (orig_op1);
5449 tree_code orig_code1 = TREE_CODE (orig_type1);
5450 if (!result_type)
5451 /* Nope. */;
5452 else if ((orig_code0 == BOOLEAN_TYPE) != (orig_code1 == BOOLEAN_TYPE))
5453 /* "If one of the operands is of type bool and the other is not, the
5454 program is ill-formed." */
5455 result_type = NULL_TREE;
5456 else if (code0 == POINTER_TYPE && orig_code0 != POINTER_TYPE
5457 && code1 == POINTER_TYPE && orig_code1 != POINTER_TYPE)
5458 /* We only do array/function-to-pointer conversion if "at least one of
5459 the operands is of pointer type". */
5460 result_type = NULL_TREE;
5461 else if (TYPE_PTRFN_P (result_type) || NULLPTR_TYPE_P (result_type))
5462 /* <=> no longer supports equality relations. */
5463 result_type = NULL_TREE;
5464 else if (orig_code0 == ENUMERAL_TYPE && orig_code1 == ENUMERAL_TYPE
5465 && !(same_type_ignoring_top_level_qualifiers_p
5466 (orig_type0, orig_type1)))
5467 /* "If both operands have arithmetic types, or one operand has integral
5468 type and the other operand has unscoped enumeration type, the usual
5469 arithmetic conversions are applied to the operands." So we don't do
5470 arithmetic conversions if the operands both have enumeral type. */
5471 result_type = NULL_TREE;
5472
5473 if (result_type)
5474 {
5475 build_type = spaceship_type (result_type, complain);
5476 if (build_type == error_mark_node)
5477 return error_mark_node;
5478 }
5479
5480 if (result_type && arithmetic_types_p)
5481 {
5482 /* If a narrowing conversion is required, other than from an integral
5483 type to a floating point type, the program is ill-formed. */
5484 bool ok = true;
5485 if (TREE_CODE (result_type) == REAL_TYPE
5486 && INTEGRAL_OR_ENUMERATION_TYPE_P (TREE_TYPE (orig_op0)))
5487 /* OK */;
5488 else if (!check_narrowing (result_type, orig_op0, complain))
5489 ok = false;
5490 if (TREE_CODE (result_type) == REAL_TYPE
5491 && INTEGRAL_OR_ENUMERATION_TYPE_P (TREE_TYPE (orig_op1)))
5492 /* OK */;
5493 else if (!check_narrowing (result_type, orig_op1, complain))
5494 ok = false;
5495 if (!ok && !(complain & tf_error))
5496 return error_mark_node;
5497 }
5498 }
5499
5500 if (!result_type)
5501 {
5502 if (complain & tf_error)
5503 {
5504 binary_op_rich_location richloc (location,
5505 orig_op0, orig_op1, true);
5506 error_at (&richloc,
5507 "invalid operands of types %qT and %qT to binary %qO",
5508 TREE_TYPE (orig_op0), TREE_TYPE (orig_op1), code);
5509 }
5510 return error_mark_node;
5511 }
5512
5513 /* If we're in a template, the only thing we need to know is the
5514 RESULT_TYPE. */
5515 if (processing_template_decl)
5516 {
5517 /* Since the middle-end checks the type when doing a build2, we
5518 need to build the tree in pieces. This built tree will never
5519 get out of the front-end as we replace it when instantiating
5520 the template. */
5521 tree tmp = build2 (resultcode,
5522 build_type ? build_type : result_type,
5523 NULL_TREE, op1);
5524 TREE_OPERAND (tmp, 0) = op0;
5525 return tmp;
5526 }
5527
5528 /* Remember the original type; RESULT_TYPE might be changed later on
5529 by shorten_binary_op. */
5530 tree orig_type = result_type;
5531
5532 if (arithmetic_types_p)
5533 {
5534 bool first_complex = (code0 == COMPLEX_TYPE);
5535 bool second_complex = (code1 == COMPLEX_TYPE);
5536 int none_complex = (!first_complex && !second_complex);
5537
5538 /* Adapted from patch for c/24581. */
5539 if (first_complex != second_complex
5540 && (code == PLUS_EXPR
5541 || code == MINUS_EXPR
5542 || code == MULT_EXPR
5543 || (code == TRUNC_DIV_EXPR && first_complex))
5544 && TREE_CODE (TREE_TYPE (result_type)) == REAL_TYPE
5545 && flag_signed_zeros)
5546 {
5547 /* An operation on mixed real/complex operands must be
5548 handled specially, but the language-independent code can
5549 more easily optimize the plain complex arithmetic if
5550 -fno-signed-zeros. */
5551 tree real_type = TREE_TYPE (result_type);
5552 tree real, imag;
5553 if (first_complex)
5554 {
5555 if (TREE_TYPE (op0) != result_type)
5556 op0 = cp_convert_and_check (result_type, op0, complain);
5557 if (TREE_TYPE (op1) != real_type)
5558 op1 = cp_convert_and_check (real_type, op1, complain);
5559 }
5560 else
5561 {
5562 if (TREE_TYPE (op0) != real_type)
5563 op0 = cp_convert_and_check (real_type, op0, complain);
5564 if (TREE_TYPE (op1) != result_type)
5565 op1 = cp_convert_and_check (result_type, op1, complain);
5566 }
5567 if (TREE_CODE (op0) == ERROR_MARK || TREE_CODE (op1) == ERROR_MARK)
5568 return error_mark_node;
5569 if (first_complex)
5570 {
5571 op0 = save_expr (op0);
5572 real = cp_build_unary_op (REALPART_EXPR, op0, true, complain);
5573 imag = cp_build_unary_op (IMAGPART_EXPR, op0, true, complain);
5574 switch (code)
5575 {
5576 case MULT_EXPR:
5577 case TRUNC_DIV_EXPR:
5578 op1 = save_expr (op1);
5579 imag = build2 (resultcode, real_type, imag, op1);
5580 /* Fall through. */
5581 case PLUS_EXPR:
5582 case MINUS_EXPR:
5583 real = build2 (resultcode, real_type, real, op1);
5584 break;
5585 default:
5586 gcc_unreachable();
5587 }
5588 }
5589 else
5590 {
5591 op1 = save_expr (op1);
5592 real = cp_build_unary_op (REALPART_EXPR, op1, true, complain);
5593 imag = cp_build_unary_op (IMAGPART_EXPR, op1, true, complain);
5594 switch (code)
5595 {
5596 case MULT_EXPR:
5597 op0 = save_expr (op0);
5598 imag = build2 (resultcode, real_type, op0, imag);
5599 /* Fall through. */
5600 case PLUS_EXPR:
5601 real = build2 (resultcode, real_type, op0, real);
5602 break;
5603 case MINUS_EXPR:
5604 real = build2 (resultcode, real_type, op0, real);
5605 imag = build1 (NEGATE_EXPR, real_type, imag);
5606 break;
5607 default:
5608 gcc_unreachable();
5609 }
5610 }
5611 result = build2 (COMPLEX_EXPR, result_type, real, imag);
5612 return result;
5613 }
5614
5615 /* For certain operations (which identify themselves by shorten != 0)
5616 if both args were extended from the same smaller type,
5617 do the arithmetic in that type and then extend.
5618
5619 shorten !=0 and !=1 indicates a bitwise operation.
5620 For them, this optimization is safe only if
5621 both args are zero-extended or both are sign-extended.
5622 Otherwise, we might change the result.
5623 E.g., (short)-1 | (unsigned short)-1 is (int)-1
5624 but calculated in (unsigned short) it would be (unsigned short)-1. */
5625
5626 if (shorten && none_complex)
5627 {
5628 final_type = result_type;
5629 result_type = shorten_binary_op (result_type, op0, op1,
5630 shorten == -1);
5631 }
5632
5633 /* Shifts can be shortened if shifting right. */
5634
5635 if (short_shift)
5636 {
5637 int unsigned_arg;
5638 tree arg0 = get_narrower (op0, &unsigned_arg);
5639 /* We're not really warning here but when we set short_shift we
5640 used fold_for_warn to fold the operand. */
5641 tree const_op1 = fold_for_warn (op1);
5642
5643 final_type = result_type;
5644
5645 if (arg0 == op0 && final_type == TREE_TYPE (op0))
5646 unsigned_arg = TYPE_UNSIGNED (TREE_TYPE (op0));
5647
5648 if (TYPE_PRECISION (TREE_TYPE (arg0)) < TYPE_PRECISION (result_type)
5649 && tree_int_cst_sgn (const_op1) > 0
5650 /* We can shorten only if the shift count is less than the
5651 number of bits in the smaller type size. */
5652 && compare_tree_int (const_op1,
5653 TYPE_PRECISION (TREE_TYPE (arg0))) < 0
5654 /* We cannot drop an unsigned shift after sign-extension. */
5655 && (!TYPE_UNSIGNED (final_type) || unsigned_arg))
5656 {
5657 /* Do an unsigned shift if the operand was zero-extended. */
5658 result_type
5659 = c_common_signed_or_unsigned_type (unsigned_arg,
5660 TREE_TYPE (arg0));
5661 /* Convert value-to-be-shifted to that type. */
5662 if (TREE_TYPE (op0) != result_type)
5663 op0 = convert (result_type, op0);
5664 converted = 1;
5665 }
5666 }
5667
5668 /* Comparison operations are shortened too but differently.
5669 They identify themselves by setting short_compare = 1. */
5670
5671 if (short_compare)
5672 {
5673 /* We call shorten_compare only for diagnostics. */
5674 tree xop0 = fold_simple (op0);
5675 tree xop1 = fold_simple (op1);
5676 tree xresult_type = result_type;
5677 enum tree_code xresultcode = resultcode;
5678 shorten_compare (location, &xop0, &xop1, &xresult_type,
5679 &xresultcode);
5680 }
5681
5682 if ((short_compare || code == MIN_EXPR || code == MAX_EXPR)
5683 && warn_sign_compare
5684 /* Do not warn until the template is instantiated; we cannot
5685 bound the ranges of the arguments until that point. */
5686 && !processing_template_decl
5687 && (complain & tf_warning)
5688 && c_inhibit_evaluation_warnings == 0
5689 /* Even unsigned enum types promote to signed int. We don't
5690 want to issue -Wsign-compare warnings for this case. */
5691 && !enum_cast_to_int (orig_op0)
5692 && !enum_cast_to_int (orig_op1))
5693 {
5694 warn_for_sign_compare (location, orig_op0, orig_op1, op0, op1,
5695 result_type, resultcode);
5696 }
5697 }
5698
5699 /* If CONVERTED is zero, both args will be converted to type RESULT_TYPE.
5700 Then the expression will be built.
5701 It will be given type FINAL_TYPE if that is nonzero;
5702 otherwise, it will be given type RESULT_TYPE. */
5703 if (! converted)
5704 {
5705 warning_sentinel w (warn_sign_conversion, short_compare);
5706 if (!same_type_p (TREE_TYPE (op0), result_type))
5707 op0 = cp_convert_and_check (result_type, op0, complain);
5708 if (!same_type_p (TREE_TYPE (op1), result_type))
5709 op1 = cp_convert_and_check (result_type, op1, complain);
5710
5711 if (op0 == error_mark_node || op1 == error_mark_node)
5712 return error_mark_node;
5713 }
5714
5715 if (build_type == NULL_TREE)
5716 build_type = result_type;
5717
5718 if (doing_shift
5719 && flag_strong_eval_order == 2
5720 && TREE_SIDE_EFFECTS (op1)
5721 && !processing_template_decl)
5722 {
5723 /* In C++17, in both op0 << op1 and op0 >> op1 op0 is sequenced before
5724 op1, so if op1 has side-effects, use SAVE_EXPR around op0. */
5725 op0 = cp_save_expr (op0);
5726 instrument_expr = op0;
5727 }
5728
5729 if (sanitize_flags_p ((SANITIZE_SHIFT
5730 | SANITIZE_DIVIDE | SANITIZE_FLOAT_DIVIDE))
5731 && current_function_decl != NULL_TREE
5732 && !processing_template_decl
5733 && (doing_div_or_mod || doing_shift))
5734 {
5735 /* OP0 and/or OP1 might have side-effects. */
5736 op0 = cp_save_expr (op0);
5737 op1 = cp_save_expr (op1);
5738 op0 = fold_non_dependent_expr (op0, complain);
5739 op1 = fold_non_dependent_expr (op1, complain);
5740 tree instrument_expr1 = NULL_TREE;
5741 if (doing_div_or_mod
5742 && sanitize_flags_p (SANITIZE_DIVIDE | SANITIZE_FLOAT_DIVIDE))
5743 {
5744 /* For diagnostics we want to use the promoted types without
5745 shorten_binary_op. So convert the arguments to the
5746 original result_type. */
5747 tree cop0 = op0;
5748 tree cop1 = op1;
5749 if (TREE_TYPE (cop0) != orig_type)
5750 cop0 = cp_convert (orig_type, op0, complain);
5751 if (TREE_TYPE (cop1) != orig_type)
5752 cop1 = cp_convert (orig_type, op1, complain);
5753 instrument_expr1 = ubsan_instrument_division (location, cop0, cop1);
5754 }
5755 else if (doing_shift && sanitize_flags_p (SANITIZE_SHIFT))
5756 instrument_expr1 = ubsan_instrument_shift (location, code, op0, op1);
5757 if (instrument_expr != NULL)
5758 instrument_expr = add_stmt_to_compound (instrument_expr,
5759 instrument_expr1);
5760 else
5761 instrument_expr = instrument_expr1;
5762 }
5763
5764 result = build2_loc (location, resultcode, build_type, op0, op1);
5765 if (final_type != 0)
5766 result = cp_convert (final_type, result, complain);
5767
5768 if (instrument_expr != NULL)
5769 result = build2 (COMPOUND_EXPR, TREE_TYPE (result),
5770 instrument_expr, result);
5771
5772 if (!processing_template_decl)
5773 {
5774 op0 = cp_fully_fold (op0);
5775 /* Only consider the second argument if the first isn't overflowed. */
5776 if (!CONSTANT_CLASS_P (op0) || TREE_OVERFLOW_P (op0))
5777 return result;
5778 op1 = cp_fully_fold (op1);
5779 if (!CONSTANT_CLASS_P (op1) || TREE_OVERFLOW_P (op1))
5780 return result;
5781 }
5782 else if (!CONSTANT_CLASS_P (op0) || !CONSTANT_CLASS_P (op1)
5783 || TREE_OVERFLOW_P (op0) || TREE_OVERFLOW_P (op1))
5784 return result;
5785
5786 result_ovl = fold_build2 (resultcode, build_type, op0, op1);
5787 if (TREE_OVERFLOW_P (result_ovl))
5788 overflow_warning (location, result_ovl);
5789
5790 return result;
5791 }
5792
5793 /* Build a VEC_PERM_EXPR.
5794 This is a simple wrapper for c_build_vec_perm_expr. */
5795 tree
5796 build_x_vec_perm_expr (location_t loc,
5797 tree arg0, tree arg1, tree arg2,
5798 tsubst_flags_t complain)
5799 {
5800 tree orig_arg0 = arg0;
5801 tree orig_arg1 = arg1;
5802 tree orig_arg2 = arg2;
5803 if (processing_template_decl)
5804 {
5805 if (type_dependent_expression_p (arg0)
5806 || type_dependent_expression_p (arg1)
5807 || type_dependent_expression_p (arg2))
5808 return build_min_nt_loc (loc, VEC_PERM_EXPR, arg0, arg1, arg2);
5809 arg0 = build_non_dependent_expr (arg0);
5810 if (arg1)
5811 arg1 = build_non_dependent_expr (arg1);
5812 arg2 = build_non_dependent_expr (arg2);
5813 }
5814 tree exp = c_build_vec_perm_expr (loc, arg0, arg1, arg2, complain & tf_error);
5815 if (processing_template_decl && exp != error_mark_node)
5816 return build_min_non_dep (VEC_PERM_EXPR, exp, orig_arg0,
5817 orig_arg1, orig_arg2);
5818 return exp;
5819 }
5820 \f
5821 /* Return a tree for the sum or difference (RESULTCODE says which)
5822 of pointer PTROP and integer INTOP. */
5823
5824 static tree
5825 cp_pointer_int_sum (location_t loc, enum tree_code resultcode, tree ptrop,
5826 tree intop, tsubst_flags_t complain)
5827 {
5828 tree res_type = TREE_TYPE (ptrop);
5829
5830 /* pointer_int_sum() uses size_in_bytes() on the TREE_TYPE(res_type)
5831 in certain circumstance (when it's valid to do so). So we need
5832 to make sure it's complete. We don't need to check here, if we
5833 can actually complete it at all, as those checks will be done in
5834 pointer_int_sum() anyway. */
5835 complete_type (TREE_TYPE (res_type));
5836
5837 return pointer_int_sum (loc, resultcode, ptrop,
5838 intop, complain & tf_warning_or_error);
5839 }
5840
5841 /* Return a tree for the difference of pointers OP0 and OP1.
5842 The resulting tree has type int. If POINTER_SUBTRACT sanitization is
5843 enabled, assign to INSTRUMENT_EXPR call to libsanitizer. */
5844
5845 static tree
5846 pointer_diff (location_t loc, tree op0, tree op1, tree ptrtype,
5847 tsubst_flags_t complain, tree *instrument_expr)
5848 {
5849 tree result, inttype;
5850 tree restype = ptrdiff_type_node;
5851 tree target_type = TREE_TYPE (ptrtype);
5852
5853 if (!complete_type_or_else (target_type, NULL_TREE))
5854 return error_mark_node;
5855
5856 if (VOID_TYPE_P (target_type))
5857 {
5858 if (complain & tf_error)
5859 permerror (loc, "ISO C++ forbids using pointer of "
5860 "type %<void *%> in subtraction");
5861 else
5862 return error_mark_node;
5863 }
5864 if (TREE_CODE (target_type) == FUNCTION_TYPE)
5865 {
5866 if (complain & tf_error)
5867 permerror (loc, "ISO C++ forbids using pointer to "
5868 "a function in subtraction");
5869 else
5870 return error_mark_node;
5871 }
5872 if (TREE_CODE (target_type) == METHOD_TYPE)
5873 {
5874 if (complain & tf_error)
5875 permerror (loc, "ISO C++ forbids using pointer to "
5876 "a method in subtraction");
5877 else
5878 return error_mark_node;
5879 }
5880 else if (!verify_type_context (loc, TCTX_POINTER_ARITH,
5881 TREE_TYPE (TREE_TYPE (op0)),
5882 !(complain & tf_error))
5883 || !verify_type_context (loc, TCTX_POINTER_ARITH,
5884 TREE_TYPE (TREE_TYPE (op1)),
5885 !(complain & tf_error)))
5886 return error_mark_node;
5887
5888 /* Determine integer type result of the subtraction. This will usually
5889 be the same as the result type (ptrdiff_t), but may need to be a wider
5890 type if pointers for the address space are wider than ptrdiff_t. */
5891 if (TYPE_PRECISION (restype) < TYPE_PRECISION (TREE_TYPE (op0)))
5892 inttype = c_common_type_for_size (TYPE_PRECISION (TREE_TYPE (op0)), 0);
5893 else
5894 inttype = restype;
5895
5896 if (!processing_template_decl
5897 && sanitize_flags_p (SANITIZE_POINTER_SUBTRACT))
5898 {
5899 op0 = save_expr (op0);
5900 op1 = save_expr (op1);
5901
5902 tree tt = builtin_decl_explicit (BUILT_IN_ASAN_POINTER_SUBTRACT);
5903 *instrument_expr = build_call_expr_loc (loc, tt, 2, op0, op1);
5904 }
5905
5906 /* First do the subtraction, then build the divide operator
5907 and only convert at the very end.
5908 Do not do default conversions in case restype is a short type. */
5909
5910 /* POINTER_DIFF_EXPR requires a signed integer type of the same size as
5911 pointers. If some platform cannot provide that, or has a larger
5912 ptrdiff_type to support differences larger than half the address
5913 space, cast the pointers to some larger integer type and do the
5914 computations in that type. */
5915 if (TYPE_PRECISION (inttype) > TYPE_PRECISION (TREE_TYPE (op0)))
5916 op0 = cp_build_binary_op (loc,
5917 MINUS_EXPR,
5918 cp_convert (inttype, op0, complain),
5919 cp_convert (inttype, op1, complain),
5920 complain);
5921 else
5922 op0 = build2_loc (loc, POINTER_DIFF_EXPR, inttype, op0, op1);
5923
5924 /* This generates an error if op1 is a pointer to an incomplete type. */
5925 if (!COMPLETE_TYPE_P (TREE_TYPE (TREE_TYPE (op1))))
5926 {
5927 if (complain & tf_error)
5928 error_at (loc, "invalid use of a pointer to an incomplete type in "
5929 "pointer arithmetic");
5930 else
5931 return error_mark_node;
5932 }
5933
5934 if (pointer_to_zero_sized_aggr_p (TREE_TYPE (op1)))
5935 {
5936 if (complain & tf_error)
5937 error_at (loc, "arithmetic on pointer to an empty aggregate");
5938 else
5939 return error_mark_node;
5940 }
5941
5942 op1 = (TYPE_PTROB_P (ptrtype)
5943 ? size_in_bytes_loc (loc, target_type)
5944 : integer_one_node);
5945
5946 /* Do the division. */
5947
5948 result = build2_loc (loc, EXACT_DIV_EXPR, inttype, op0,
5949 cp_convert (inttype, op1, complain));
5950 return cp_convert (restype, result, complain);
5951 }
5952 \f
5953 /* Construct and perhaps optimize a tree representation
5954 for a unary operation. CODE, a tree_code, specifies the operation
5955 and XARG is the operand. */
5956
5957 tree
5958 build_x_unary_op (location_t loc, enum tree_code code, cp_expr xarg,
5959 tsubst_flags_t complain)
5960 {
5961 tree orig_expr = xarg;
5962 tree exp;
5963 int ptrmem = 0;
5964 tree overload = NULL_TREE;
5965
5966 if (processing_template_decl)
5967 {
5968 if (type_dependent_expression_p (xarg))
5969 {
5970 tree e = build_min_nt_loc (loc, code, xarg.get_value (), NULL_TREE);
5971 maybe_save_operator_binding (e);
5972 return e;
5973 }
5974
5975 xarg = build_non_dependent_expr (xarg);
5976 }
5977
5978 exp = NULL_TREE;
5979
5980 /* [expr.unary.op] says:
5981
5982 The address of an object of incomplete type can be taken.
5983
5984 (And is just the ordinary address operator, not an overloaded
5985 "operator &".) However, if the type is a template
5986 specialization, we must complete the type at this point so that
5987 an overloaded "operator &" will be available if required. */
5988 if (code == ADDR_EXPR
5989 && TREE_CODE (xarg) != TEMPLATE_ID_EXPR
5990 && ((CLASS_TYPE_P (TREE_TYPE (xarg))
5991 && !COMPLETE_TYPE_P (complete_type (TREE_TYPE (xarg))))
5992 || (TREE_CODE (xarg) == OFFSET_REF)))
5993 /* Don't look for a function. */;
5994 else
5995 exp = build_new_op (loc, code, LOOKUP_NORMAL, xarg, NULL_TREE,
5996 NULL_TREE, &overload, complain);
5997
5998 if (!exp && code == ADDR_EXPR)
5999 {
6000 if (is_overloaded_fn (xarg))
6001 {
6002 tree fn = get_first_fn (xarg);
6003 if (DECL_CONSTRUCTOR_P (fn) || DECL_DESTRUCTOR_P (fn))
6004 {
6005 if (complain & tf_error)
6006 error_at (loc, DECL_CONSTRUCTOR_P (fn)
6007 ? G_("taking address of constructor %qD")
6008 : G_("taking address of destructor %qD"),
6009 fn);
6010 return error_mark_node;
6011 }
6012 }
6013
6014 /* A pointer to member-function can be formed only by saying
6015 &X::mf. */
6016 if (!flag_ms_extensions && TREE_CODE (TREE_TYPE (xarg)) == METHOD_TYPE
6017 && (TREE_CODE (xarg) != OFFSET_REF || !PTRMEM_OK_P (xarg)))
6018 {
6019 if (TREE_CODE (xarg) != OFFSET_REF
6020 || !TYPE_P (TREE_OPERAND (xarg, 0)))
6021 {
6022 if (complain & tf_error)
6023 {
6024 error_at (loc, "invalid use of %qE to form a "
6025 "pointer-to-member-function", xarg.get_value ());
6026 if (TREE_CODE (xarg) != OFFSET_REF)
6027 inform (loc, " a qualified-id is required");
6028 }
6029 return error_mark_node;
6030 }
6031 else
6032 {
6033 if (complain & tf_error)
6034 error_at (loc, "parentheses around %qE cannot be used to "
6035 "form a pointer-to-member-function",
6036 xarg.get_value ());
6037 else
6038 return error_mark_node;
6039 PTRMEM_OK_P (xarg) = 1;
6040 }
6041 }
6042
6043 if (TREE_CODE (xarg) == OFFSET_REF)
6044 {
6045 ptrmem = PTRMEM_OK_P (xarg);
6046
6047 if (!ptrmem && !flag_ms_extensions
6048 && TREE_CODE (TREE_TYPE (TREE_OPERAND (xarg, 1))) == METHOD_TYPE)
6049 {
6050 /* A single non-static member, make sure we don't allow a
6051 pointer-to-member. */
6052 xarg = build2 (OFFSET_REF, TREE_TYPE (xarg),
6053 TREE_OPERAND (xarg, 0),
6054 ovl_make (TREE_OPERAND (xarg, 1)));
6055 PTRMEM_OK_P (xarg) = ptrmem;
6056 }
6057 }
6058
6059 exp = cp_build_addr_expr_strict (xarg, complain);
6060 }
6061
6062 if (processing_template_decl && exp != error_mark_node)
6063 {
6064 if (overload != NULL_TREE)
6065 return (build_min_non_dep_op_overload
6066 (code, exp, overload, orig_expr, integer_zero_node));
6067
6068 exp = build_min_non_dep (code, exp, orig_expr,
6069 /*For {PRE,POST}{INC,DEC}REMENT_EXPR*/NULL_TREE);
6070 }
6071 if (TREE_CODE (exp) == ADDR_EXPR)
6072 PTRMEM_OK_P (exp) = ptrmem;
6073 return exp;
6074 }
6075
6076 /* Construct and perhaps optimize a tree representation
6077 for __builtin_addressof operation. ARG specifies the operand. */
6078
6079 tree
6080 cp_build_addressof (location_t loc, tree arg, tsubst_flags_t complain)
6081 {
6082 tree orig_expr = arg;
6083
6084 if (processing_template_decl)
6085 {
6086 if (type_dependent_expression_p (arg))
6087 return build_min_nt_loc (loc, ADDRESSOF_EXPR, arg, NULL_TREE);
6088
6089 arg = build_non_dependent_expr (arg);
6090 }
6091
6092 tree exp = cp_build_addr_expr_strict (arg, complain);
6093
6094 if (processing_template_decl && exp != error_mark_node)
6095 exp = build_min_non_dep (ADDRESSOF_EXPR, exp, orig_expr, NULL_TREE);
6096 return exp;
6097 }
6098
6099 /* Like c_common_truthvalue_conversion, but handle pointer-to-member
6100 constants, where a null value is represented by an INTEGER_CST of
6101 -1. */
6102
6103 tree
6104 cp_truthvalue_conversion (tree expr, tsubst_flags_t complain)
6105 {
6106 tree type = TREE_TYPE (expr);
6107 location_t loc = cp_expr_loc_or_input_loc (expr);
6108 if (TYPE_PTR_OR_PTRMEM_P (type)
6109 /* Avoid ICE on invalid use of non-static member function. */
6110 || TREE_CODE (expr) == FUNCTION_DECL)
6111 return cp_build_binary_op (loc, NE_EXPR, expr, nullptr_node, complain);
6112 else
6113 return c_common_truthvalue_conversion (loc, expr);
6114 }
6115
6116 /* Returns EXPR contextually converted to bool. */
6117
6118 tree
6119 contextual_conv_bool (tree expr, tsubst_flags_t complain)
6120 {
6121 return perform_implicit_conversion_flags (boolean_type_node, expr,
6122 complain, LOOKUP_NORMAL);
6123 }
6124
6125 /* Just like cp_truthvalue_conversion, but we want a CLEANUP_POINT_EXPR. This
6126 is a low-level function; most callers should use maybe_convert_cond. */
6127
6128 tree
6129 condition_conversion (tree expr)
6130 {
6131 tree t = contextual_conv_bool (expr, tf_warning_or_error);
6132 if (!processing_template_decl)
6133 t = fold_build_cleanup_point_expr (boolean_type_node, t);
6134 return t;
6135 }
6136
6137 /* Returns the address of T. This function will fold away
6138 ADDR_EXPR of INDIRECT_REF. This is only for low-level usage;
6139 most places should use cp_build_addr_expr instead. */
6140
6141 tree
6142 build_address (tree t)
6143 {
6144 if (error_operand_p (t) || !cxx_mark_addressable (t))
6145 return error_mark_node;
6146 gcc_checking_assert (TREE_CODE (t) != CONSTRUCTOR
6147 || processing_template_decl);
6148 t = build_fold_addr_expr_loc (EXPR_LOCATION (t), t);
6149 if (TREE_CODE (t) != ADDR_EXPR)
6150 t = rvalue (t);
6151 return t;
6152 }
6153
6154 /* Return a NOP_EXPR converting EXPR to TYPE. */
6155
6156 tree
6157 build_nop (tree type, tree expr)
6158 {
6159 if (type == error_mark_node || error_operand_p (expr))
6160 return expr;
6161 return build1_loc (EXPR_LOCATION (expr), NOP_EXPR, type, expr);
6162 }
6163
6164 /* Take the address of ARG, whatever that means under C++ semantics.
6165 If STRICT_LVALUE is true, require an lvalue; otherwise, allow xvalues
6166 and class rvalues as well.
6167
6168 Nothing should call this function directly; instead, callers should use
6169 cp_build_addr_expr or cp_build_addr_expr_strict. */
6170
6171 static tree
6172 cp_build_addr_expr_1 (tree arg, bool strict_lvalue, tsubst_flags_t complain)
6173 {
6174 tree argtype;
6175 tree val;
6176
6177 if (!arg || error_operand_p (arg))
6178 return error_mark_node;
6179
6180 arg = mark_lvalue_use (arg);
6181 if (error_operand_p (arg))
6182 return error_mark_node;
6183
6184 argtype = lvalue_type (arg);
6185 location_t loc = cp_expr_loc_or_input_loc (arg);
6186
6187 gcc_assert (!(identifier_p (arg) && IDENTIFIER_ANY_OP_P (arg)));
6188
6189 if (TREE_CODE (arg) == COMPONENT_REF && type_unknown_p (arg)
6190 && !really_overloaded_fn (arg))
6191 {
6192 /* They're trying to take the address of a unique non-static
6193 member function. This is ill-formed (except in MS-land),
6194 but let's try to DTRT.
6195 Note: We only handle unique functions here because we don't
6196 want to complain if there's a static overload; non-unique
6197 cases will be handled by instantiate_type. But we need to
6198 handle this case here to allow casts on the resulting PMF.
6199 We could defer this in non-MS mode, but it's easier to give
6200 a useful error here. */
6201
6202 /* Inside constant member functions, the `this' pointer
6203 contains an extra const qualifier. TYPE_MAIN_VARIANT
6204 is used here to remove this const from the diagnostics
6205 and the created OFFSET_REF. */
6206 tree base = TYPE_MAIN_VARIANT (TREE_TYPE (TREE_OPERAND (arg, 0)));
6207 tree fn = get_first_fn (TREE_OPERAND (arg, 1));
6208 if (!mark_used (fn, complain) && !(complain & tf_error))
6209 return error_mark_node;
6210
6211 if (! flag_ms_extensions)
6212 {
6213 tree name = DECL_NAME (fn);
6214 if (!(complain & tf_error))
6215 return error_mark_node;
6216 else if (current_class_type
6217 && TREE_OPERAND (arg, 0) == current_class_ref)
6218 /* An expression like &memfn. */
6219 permerror (loc,
6220 "ISO C++ forbids taking the address of an unqualified"
6221 " or parenthesized non-static member function to form"
6222 " a pointer to member function. Say %<&%T::%D%>",
6223 base, name);
6224 else
6225 permerror (loc,
6226 "ISO C++ forbids taking the address of a bound member"
6227 " function to form a pointer to member function."
6228 " Say %<&%T::%D%>",
6229 base, name);
6230 }
6231 arg = build_offset_ref (base, fn, /*address_p=*/true, complain);
6232 }
6233
6234 /* Uninstantiated types are all functions. Taking the
6235 address of a function is a no-op, so just return the
6236 argument. */
6237 if (type_unknown_p (arg))
6238 return build1 (ADDR_EXPR, unknown_type_node, arg);
6239
6240 if (TREE_CODE (arg) == OFFSET_REF)
6241 /* We want a pointer to member; bypass all the code for actually taking
6242 the address of something. */
6243 goto offset_ref;
6244
6245 /* Anything not already handled and not a true memory reference
6246 is an error. */
6247 if (!FUNC_OR_METHOD_TYPE_P (argtype))
6248 {
6249 cp_lvalue_kind kind = lvalue_kind (arg);
6250 if (kind == clk_none)
6251 {
6252 if (complain & tf_error)
6253 lvalue_error (loc, lv_addressof);
6254 return error_mark_node;
6255 }
6256 if (strict_lvalue && (kind & (clk_rvalueref|clk_class)))
6257 {
6258 if (!(complain & tf_error))
6259 return error_mark_node;
6260 /* Make this a permerror because we used to accept it. */
6261 permerror (loc, "taking address of rvalue");
6262 }
6263 }
6264
6265 if (TYPE_REF_P (argtype))
6266 {
6267 tree type = build_pointer_type (TREE_TYPE (argtype));
6268 arg = build1 (CONVERT_EXPR, type, arg);
6269 return arg;
6270 }
6271 else if (pedantic && DECL_MAIN_P (tree_strip_any_location_wrapper (arg)))
6272 {
6273 /* ARM $3.4 */
6274 /* Apparently a lot of autoconf scripts for C++ packages do this,
6275 so only complain if -Wpedantic. */
6276 if (complain & (flag_pedantic_errors ? tf_error : tf_warning))
6277 pedwarn (loc, OPT_Wpedantic,
6278 "ISO C++ forbids taking address of function %<::main%>");
6279 else if (flag_pedantic_errors)
6280 return error_mark_node;
6281 }
6282
6283 /* Let &* cancel out to simplify resulting code. */
6284 if (INDIRECT_REF_P (arg))
6285 {
6286 arg = TREE_OPERAND (arg, 0);
6287 if (TYPE_REF_P (TREE_TYPE (arg)))
6288 {
6289 tree type = build_pointer_type (TREE_TYPE (TREE_TYPE (arg)));
6290 arg = build1 (CONVERT_EXPR, type, arg);
6291 }
6292 else
6293 /* Don't let this be an lvalue. */
6294 arg = rvalue (arg);
6295 return arg;
6296 }
6297
6298 /* Handle complex lvalues (when permitted)
6299 by reduction to simpler cases. */
6300 val = unary_complex_lvalue (ADDR_EXPR, arg);
6301 if (val != 0)
6302 return val;
6303
6304 switch (TREE_CODE (arg))
6305 {
6306 CASE_CONVERT:
6307 case FLOAT_EXPR:
6308 case FIX_TRUNC_EXPR:
6309 /* We should have handled this above in the lvalue_kind check. */
6310 gcc_unreachable ();
6311 break;
6312
6313 case BASELINK:
6314 arg = BASELINK_FUNCTIONS (arg);
6315 /* Fall through. */
6316
6317 case OVERLOAD:
6318 arg = OVL_FIRST (arg);
6319 break;
6320
6321 case OFFSET_REF:
6322 offset_ref:
6323 /* Turn a reference to a non-static data member into a
6324 pointer-to-member. */
6325 {
6326 tree type;
6327 tree t;
6328
6329 gcc_assert (PTRMEM_OK_P (arg));
6330
6331 t = TREE_OPERAND (arg, 1);
6332 if (TYPE_REF_P (TREE_TYPE (t)))
6333 {
6334 if (complain & tf_error)
6335 error_at (loc,
6336 "cannot create pointer to reference member %qD", t);
6337 return error_mark_node;
6338 }
6339
6340 type = build_ptrmem_type (context_for_name_lookup (t),
6341 TREE_TYPE (t));
6342 t = make_ptrmem_cst (type, TREE_OPERAND (arg, 1));
6343 return t;
6344 }
6345
6346 default:
6347 break;
6348 }
6349
6350 if (argtype != error_mark_node)
6351 argtype = build_pointer_type (argtype);
6352
6353 if (bitfield_p (arg))
6354 {
6355 if (complain & tf_error)
6356 error_at (loc, "attempt to take address of bit-field");
6357 return error_mark_node;
6358 }
6359
6360 /* In a template, we are processing a non-dependent expression
6361 so we can just form an ADDR_EXPR with the correct type. */
6362 if (processing_template_decl || TREE_CODE (arg) != COMPONENT_REF)
6363 {
6364 tree stripped_arg = tree_strip_any_location_wrapper (arg);
6365 if (TREE_CODE (stripped_arg) == FUNCTION_DECL
6366 && DECL_IMMEDIATE_FUNCTION_P (stripped_arg)
6367 && cp_unevaluated_operand == 0
6368 && (current_function_decl == NULL_TREE
6369 || !DECL_IMMEDIATE_FUNCTION_P (current_function_decl)))
6370 {
6371 if (complain & tf_error)
6372 error_at (loc, "taking address of an immediate function %qD",
6373 stripped_arg);
6374 return error_mark_node;
6375 }
6376 if (TREE_CODE (stripped_arg) == FUNCTION_DECL
6377 && !mark_used (stripped_arg, complain) && !(complain & tf_error))
6378 return error_mark_node;
6379 val = build_address (arg);
6380 if (TREE_CODE (arg) == OFFSET_REF)
6381 PTRMEM_OK_P (val) = PTRMEM_OK_P (arg);
6382 }
6383 else if (BASELINK_P (TREE_OPERAND (arg, 1)))
6384 {
6385 tree fn = BASELINK_FUNCTIONS (TREE_OPERAND (arg, 1));
6386
6387 /* We can only get here with a single static member
6388 function. */
6389 gcc_assert (TREE_CODE (fn) == FUNCTION_DECL
6390 && DECL_STATIC_FUNCTION_P (fn));
6391 if (!mark_used (fn, complain) && !(complain & tf_error))
6392 return error_mark_node;
6393 val = build_address (fn);
6394 if (TREE_SIDE_EFFECTS (TREE_OPERAND (arg, 0)))
6395 /* Do not lose object's side effects. */
6396 val = build2 (COMPOUND_EXPR, TREE_TYPE (val),
6397 TREE_OPERAND (arg, 0), val);
6398 }
6399 else
6400 {
6401 tree object = TREE_OPERAND (arg, 0);
6402 tree field = TREE_OPERAND (arg, 1);
6403 gcc_assert (same_type_ignoring_top_level_qualifiers_p
6404 (TREE_TYPE (object), decl_type_context (field)));
6405 val = build_address (arg);
6406 }
6407
6408 if (TYPE_PTR_P (argtype)
6409 && TREE_CODE (TREE_TYPE (argtype)) == METHOD_TYPE)
6410 {
6411 build_ptrmemfunc_type (argtype);
6412 val = build_ptrmemfunc (argtype, val, 0,
6413 /*c_cast_p=*/false,
6414 complain);
6415 }
6416
6417 return val;
6418 }
6419
6420 /* Take the address of ARG if it has one, even if it's an rvalue. */
6421
6422 tree
6423 cp_build_addr_expr (tree arg, tsubst_flags_t complain)
6424 {
6425 return cp_build_addr_expr_1 (arg, 0, complain);
6426 }
6427
6428 /* Take the address of ARG, but only if it's an lvalue. */
6429
6430 static tree
6431 cp_build_addr_expr_strict (tree arg, tsubst_flags_t complain)
6432 {
6433 return cp_build_addr_expr_1 (arg, 1, complain);
6434 }
6435
6436 /* C++: Must handle pointers to members.
6437
6438 Perhaps type instantiation should be extended to handle conversion
6439 from aggregates to types we don't yet know we want? (Or are those
6440 cases typically errors which should be reported?)
6441
6442 NOCONVERT suppresses the default promotions (such as from short to int). */
6443
6444 tree
6445 cp_build_unary_op (enum tree_code code, tree xarg, bool noconvert,
6446 tsubst_flags_t complain)
6447 {
6448 /* No default_conversion here. It causes trouble for ADDR_EXPR. */
6449 tree arg = xarg;
6450 location_t location = cp_expr_loc_or_input_loc (arg);
6451 tree argtype = 0;
6452 const char *errstring = NULL;
6453 tree val;
6454 const char *invalid_op_diag;
6455
6456 if (!arg || error_operand_p (arg))
6457 return error_mark_node;
6458
6459 arg = resolve_nondeduced_context (arg, complain);
6460
6461 if ((invalid_op_diag
6462 = targetm.invalid_unary_op ((code == UNARY_PLUS_EXPR
6463 ? CONVERT_EXPR
6464 : code),
6465 TREE_TYPE (arg))))
6466 {
6467 if (complain & tf_error)
6468 error (invalid_op_diag);
6469 return error_mark_node;
6470 }
6471
6472 switch (code)
6473 {
6474 case UNARY_PLUS_EXPR:
6475 case NEGATE_EXPR:
6476 {
6477 int flags = WANT_ARITH | WANT_ENUM;
6478 /* Unary plus (but not unary minus) is allowed on pointers. */
6479 if (code == UNARY_PLUS_EXPR)
6480 flags |= WANT_POINTER;
6481 arg = build_expr_type_conversion (flags, arg, true);
6482 if (!arg)
6483 errstring = (code == NEGATE_EXPR
6484 ? _("wrong type argument to unary minus")
6485 : _("wrong type argument to unary plus"));
6486 else
6487 {
6488 if (!noconvert && INTEGRAL_OR_ENUMERATION_TYPE_P (TREE_TYPE (arg)))
6489 arg = cp_perform_integral_promotions (arg, complain);
6490
6491 /* Make sure the result is not an lvalue: a unary plus or minus
6492 expression is always a rvalue. */
6493 arg = rvalue (arg);
6494 }
6495 }
6496 break;
6497
6498 case BIT_NOT_EXPR:
6499 if (TREE_CODE (TREE_TYPE (arg)) == COMPLEX_TYPE)
6500 {
6501 code = CONJ_EXPR;
6502 if (!noconvert)
6503 {
6504 arg = cp_default_conversion (arg, complain);
6505 if (arg == error_mark_node)
6506 return error_mark_node;
6507 }
6508 }
6509 else if (!(arg = build_expr_type_conversion (WANT_INT | WANT_ENUM
6510 | WANT_VECTOR_OR_COMPLEX,
6511 arg, true)))
6512 errstring = _("wrong type argument to bit-complement");
6513 else if (!noconvert && INTEGRAL_OR_ENUMERATION_TYPE_P (TREE_TYPE (arg)))
6514 {
6515 /* Warn if the expression has boolean value. */
6516 if (TREE_CODE (TREE_TYPE (arg)) == BOOLEAN_TYPE
6517 && (complain & tf_warning)
6518 && warning_at (location, OPT_Wbool_operation,
6519 "%<~%> on an expression of type %<bool%>"))
6520 inform (location, "did you mean to use logical not (%<!%>)?");
6521 arg = cp_perform_integral_promotions (arg, complain);
6522 }
6523 else if (!noconvert && VECTOR_TYPE_P (TREE_TYPE (arg)))
6524 arg = mark_rvalue_use (arg);
6525 break;
6526
6527 case ABS_EXPR:
6528 if (!(arg = build_expr_type_conversion (WANT_ARITH | WANT_ENUM, arg, true)))
6529 errstring = _("wrong type argument to abs");
6530 else if (!noconvert)
6531 {
6532 arg = cp_default_conversion (arg, complain);
6533 if (arg == error_mark_node)
6534 return error_mark_node;
6535 }
6536 break;
6537
6538 case CONJ_EXPR:
6539 /* Conjugating a real value is a no-op, but allow it anyway. */
6540 if (!(arg = build_expr_type_conversion (WANT_ARITH | WANT_ENUM, arg, true)))
6541 errstring = _("wrong type argument to conjugation");
6542 else if (!noconvert)
6543 {
6544 arg = cp_default_conversion (arg, complain);
6545 if (arg == error_mark_node)
6546 return error_mark_node;
6547 }
6548 break;
6549
6550 case TRUTH_NOT_EXPR:
6551 if (gnu_vector_type_p (TREE_TYPE (arg)))
6552 return cp_build_binary_op (input_location, EQ_EXPR, arg,
6553 build_zero_cst (TREE_TYPE (arg)), complain);
6554 arg = perform_implicit_conversion (boolean_type_node, arg,
6555 complain);
6556 val = invert_truthvalue_loc (location, arg);
6557 if (arg != error_mark_node)
6558 return val;
6559 errstring = _("in argument to unary !");
6560 break;
6561
6562 case NOP_EXPR:
6563 break;
6564
6565 case REALPART_EXPR:
6566 case IMAGPART_EXPR:
6567 arg = build_real_imag_expr (input_location, code, arg);
6568 return arg;
6569
6570 case PREINCREMENT_EXPR:
6571 case POSTINCREMENT_EXPR:
6572 case PREDECREMENT_EXPR:
6573 case POSTDECREMENT_EXPR:
6574 /* Handle complex lvalues (when permitted)
6575 by reduction to simpler cases. */
6576
6577 val = unary_complex_lvalue (code, arg);
6578 if (val != 0)
6579 return val;
6580
6581 arg = mark_lvalue_use (arg);
6582
6583 /* Increment or decrement the real part of the value,
6584 and don't change the imaginary part. */
6585 if (TREE_CODE (TREE_TYPE (arg)) == COMPLEX_TYPE)
6586 {
6587 tree real, imag;
6588
6589 arg = cp_stabilize_reference (arg);
6590 real = cp_build_unary_op (REALPART_EXPR, arg, true, complain);
6591 imag = cp_build_unary_op (IMAGPART_EXPR, arg, true, complain);
6592 real = cp_build_unary_op (code, real, true, complain);
6593 if (real == error_mark_node || imag == error_mark_node)
6594 return error_mark_node;
6595 return build2 (COMPLEX_EXPR, TREE_TYPE (arg),
6596 real, imag);
6597 }
6598
6599 /* Report invalid types. */
6600
6601 if (!(arg = build_expr_type_conversion (WANT_ARITH | WANT_POINTER,
6602 arg, true)))
6603 {
6604 if (code == PREINCREMENT_EXPR)
6605 errstring = _("no pre-increment operator for type");
6606 else if (code == POSTINCREMENT_EXPR)
6607 errstring = _("no post-increment operator for type");
6608 else if (code == PREDECREMENT_EXPR)
6609 errstring = _("no pre-decrement operator for type");
6610 else
6611 errstring = _("no post-decrement operator for type");
6612 break;
6613 }
6614 else if (arg == error_mark_node)
6615 return error_mark_node;
6616
6617 /* Report something read-only. */
6618
6619 if (CP_TYPE_CONST_P (TREE_TYPE (arg))
6620 || TREE_READONLY (arg))
6621 {
6622 if (complain & tf_error)
6623 cxx_readonly_error (location, arg,
6624 ((code == PREINCREMENT_EXPR
6625 || code == POSTINCREMENT_EXPR)
6626 ? lv_increment : lv_decrement));
6627 else
6628 return error_mark_node;
6629 }
6630
6631 {
6632 tree inc;
6633 tree declared_type = unlowered_expr_type (arg);
6634
6635 argtype = TREE_TYPE (arg);
6636
6637 /* ARM $5.2.5 last annotation says this should be forbidden. */
6638 if (TREE_CODE (argtype) == ENUMERAL_TYPE)
6639 {
6640 if (complain & tf_error)
6641 permerror (location, (code == PREINCREMENT_EXPR
6642 || code == POSTINCREMENT_EXPR)
6643 ? G_("ISO C++ forbids incrementing an enum")
6644 : G_("ISO C++ forbids decrementing an enum"));
6645 else
6646 return error_mark_node;
6647 }
6648
6649 /* Compute the increment. */
6650
6651 if (TYPE_PTR_P (argtype))
6652 {
6653 tree type = complete_type (TREE_TYPE (argtype));
6654
6655 if (!COMPLETE_OR_VOID_TYPE_P (type))
6656 {
6657 if (complain & tf_error)
6658 error_at (location, ((code == PREINCREMENT_EXPR
6659 || code == POSTINCREMENT_EXPR))
6660 ? G_("cannot increment a pointer to incomplete "
6661 "type %qT")
6662 : G_("cannot decrement a pointer to incomplete "
6663 "type %qT"),
6664 TREE_TYPE (argtype));
6665 else
6666 return error_mark_node;
6667 }
6668 else if (!TYPE_PTROB_P (argtype))
6669 {
6670 if (complain & tf_error)
6671 pedwarn (location, OPT_Wpointer_arith,
6672 (code == PREINCREMENT_EXPR
6673 || code == POSTINCREMENT_EXPR)
6674 ? G_("ISO C++ forbids incrementing a pointer "
6675 "of type %qT")
6676 : G_("ISO C++ forbids decrementing a pointer "
6677 "of type %qT"),
6678 argtype);
6679 else
6680 return error_mark_node;
6681 }
6682 else if (!verify_type_context (location, TCTX_POINTER_ARITH,
6683 TREE_TYPE (argtype),
6684 !(complain & tf_error)))
6685 return error_mark_node;
6686
6687 inc = cxx_sizeof_nowarn (TREE_TYPE (argtype));
6688 }
6689 else
6690 inc = VECTOR_TYPE_P (argtype)
6691 ? build_one_cst (argtype)
6692 : integer_one_node;
6693
6694 inc = cp_convert (argtype, inc, complain);
6695
6696 /* If 'arg' is an Objective-C PROPERTY_REF expression, then we
6697 need to ask Objective-C to build the increment or decrement
6698 expression for it. */
6699 if (objc_is_property_ref (arg))
6700 return objc_build_incr_expr_for_property_ref (input_location, code,
6701 arg, inc);
6702
6703 /* Complain about anything else that is not a true lvalue. */
6704 if (!lvalue_or_else (arg, ((code == PREINCREMENT_EXPR
6705 || code == POSTINCREMENT_EXPR)
6706 ? lv_increment : lv_decrement),
6707 complain))
6708 return error_mark_node;
6709
6710 /* [depr.volatile.type] "Postfix ++ and -- expressions and
6711 prefix ++ and -- expressions of volatile-qualified arithmetic
6712 and pointer types are deprecated." */
6713 if (TREE_THIS_VOLATILE (arg) || CP_TYPE_VOLATILE_P (TREE_TYPE (arg)))
6714 warning_at (location, OPT_Wvolatile,
6715 "%qs expression of %<volatile%>-qualified type is "
6716 "deprecated",
6717 ((code == PREINCREMENT_EXPR
6718 || code == POSTINCREMENT_EXPR)
6719 ? "++" : "--"));
6720
6721 /* Forbid using -- or ++ in C++17 on `bool'. */
6722 if (TREE_CODE (declared_type) == BOOLEAN_TYPE)
6723 {
6724 if (code == POSTDECREMENT_EXPR || code == PREDECREMENT_EXPR)
6725 {
6726 if (complain & tf_error)
6727 error_at (location,
6728 "use of an operand of type %qT in %<operator--%> "
6729 "is forbidden", boolean_type_node);
6730 return error_mark_node;
6731 }
6732 else
6733 {
6734 if (cxx_dialect >= cxx17)
6735 {
6736 if (complain & tf_error)
6737 error_at (location,
6738 "use of an operand of type %qT in "
6739 "%<operator++%> is forbidden in C++17",
6740 boolean_type_node);
6741 return error_mark_node;
6742 }
6743 /* Otherwise, [depr.incr.bool] says this is deprecated. */
6744 else
6745 warning_at (location, OPT_Wdeprecated,
6746 "use of an operand of type %qT "
6747 "in %<operator++%> is deprecated",
6748 boolean_type_node);
6749 }
6750 val = boolean_increment (code, arg);
6751 }
6752 else if (code == POSTINCREMENT_EXPR || code == POSTDECREMENT_EXPR)
6753 /* An rvalue has no cv-qualifiers. */
6754 val = build2 (code, cv_unqualified (TREE_TYPE (arg)), arg, inc);
6755 else
6756 val = build2 (code, TREE_TYPE (arg), arg, inc);
6757
6758 TREE_SIDE_EFFECTS (val) = 1;
6759 return val;
6760 }
6761
6762 case ADDR_EXPR:
6763 /* Note that this operation never does default_conversion
6764 regardless of NOCONVERT. */
6765 return cp_build_addr_expr (arg, complain);
6766
6767 default:
6768 break;
6769 }
6770
6771 if (!errstring)
6772 {
6773 if (argtype == 0)
6774 argtype = TREE_TYPE (arg);
6775 return build1 (code, argtype, arg);
6776 }
6777
6778 if (complain & tf_error)
6779 error_at (location, "%s", errstring);
6780 return error_mark_node;
6781 }
6782
6783 /* Hook for the c-common bits that build a unary op. */
6784 tree
6785 build_unary_op (location_t /*location*/,
6786 enum tree_code code, tree xarg, bool noconvert)
6787 {
6788 return cp_build_unary_op (code, xarg, noconvert, tf_warning_or_error);
6789 }
6790
6791 /* Adjust LVALUE, an MODIFY_EXPR, PREINCREMENT_EXPR or PREDECREMENT_EXPR,
6792 so that it is a valid lvalue even for GENERIC by replacing
6793 (lhs = rhs) with ((lhs = rhs), lhs)
6794 (--lhs) with ((--lhs), lhs)
6795 (++lhs) with ((++lhs), lhs)
6796 and if lhs has side-effects, calling cp_stabilize_reference on it, so
6797 that it can be evaluated multiple times. */
6798
6799 tree
6800 genericize_compound_lvalue (tree lvalue)
6801 {
6802 if (TREE_SIDE_EFFECTS (TREE_OPERAND (lvalue, 0)))
6803 lvalue = build2 (TREE_CODE (lvalue), TREE_TYPE (lvalue),
6804 cp_stabilize_reference (TREE_OPERAND (lvalue, 0)),
6805 TREE_OPERAND (lvalue, 1));
6806 return build2 (COMPOUND_EXPR, TREE_TYPE (TREE_OPERAND (lvalue, 0)),
6807 lvalue, TREE_OPERAND (lvalue, 0));
6808 }
6809
6810 /* Apply unary lvalue-demanding operator CODE to the expression ARG
6811 for certain kinds of expressions which are not really lvalues
6812 but which we can accept as lvalues.
6813
6814 If ARG is not a kind of expression we can handle, return
6815 NULL_TREE. */
6816
6817 tree
6818 unary_complex_lvalue (enum tree_code code, tree arg)
6819 {
6820 /* Inside a template, making these kinds of adjustments is
6821 pointless; we are only concerned with the type of the
6822 expression. */
6823 if (processing_template_decl)
6824 return NULL_TREE;
6825
6826 /* Handle (a, b) used as an "lvalue". */
6827 if (TREE_CODE (arg) == COMPOUND_EXPR)
6828 {
6829 tree real_result = cp_build_unary_op (code, TREE_OPERAND (arg, 1), false,
6830 tf_warning_or_error);
6831 return build2 (COMPOUND_EXPR, TREE_TYPE (real_result),
6832 TREE_OPERAND (arg, 0), real_result);
6833 }
6834
6835 /* Handle (a ? b : c) used as an "lvalue". */
6836 if (TREE_CODE (arg) == COND_EXPR
6837 || TREE_CODE (arg) == MIN_EXPR || TREE_CODE (arg) == MAX_EXPR)
6838 return rationalize_conditional_expr (code, arg, tf_warning_or_error);
6839
6840 /* Handle (a = b), (++a), and (--a) used as an "lvalue". */
6841 if (TREE_CODE (arg) == MODIFY_EXPR
6842 || TREE_CODE (arg) == PREINCREMENT_EXPR
6843 || TREE_CODE (arg) == PREDECREMENT_EXPR)
6844 return unary_complex_lvalue (code, genericize_compound_lvalue (arg));
6845
6846 if (code != ADDR_EXPR)
6847 return NULL_TREE;
6848
6849 /* Handle (a = b) used as an "lvalue" for `&'. */
6850 if (TREE_CODE (arg) == MODIFY_EXPR
6851 || TREE_CODE (arg) == INIT_EXPR)
6852 {
6853 tree real_result = cp_build_unary_op (code, TREE_OPERAND (arg, 0), false,
6854 tf_warning_or_error);
6855 arg = build2 (COMPOUND_EXPR, TREE_TYPE (real_result),
6856 arg, real_result);
6857 TREE_NO_WARNING (arg) = 1;
6858 return arg;
6859 }
6860
6861 if (FUNC_OR_METHOD_TYPE_P (TREE_TYPE (arg))
6862 || TREE_CODE (arg) == OFFSET_REF)
6863 return NULL_TREE;
6864
6865 /* We permit compiler to make function calls returning
6866 objects of aggregate type look like lvalues. */
6867 {
6868 tree targ = arg;
6869
6870 if (TREE_CODE (targ) == SAVE_EXPR)
6871 targ = TREE_OPERAND (targ, 0);
6872
6873 if (TREE_CODE (targ) == CALL_EXPR && MAYBE_CLASS_TYPE_P (TREE_TYPE (targ)))
6874 {
6875 if (TREE_CODE (arg) == SAVE_EXPR)
6876 targ = arg;
6877 else
6878 targ = build_cplus_new (TREE_TYPE (arg), arg, tf_warning_or_error);
6879 return build1 (ADDR_EXPR, build_pointer_type (TREE_TYPE (arg)), targ);
6880 }
6881
6882 if (TREE_CODE (arg) == SAVE_EXPR && INDIRECT_REF_P (targ))
6883 return build3 (SAVE_EXPR, build_pointer_type (TREE_TYPE (arg)),
6884 TREE_OPERAND (targ, 0), current_function_decl, NULL);
6885 }
6886
6887 /* Don't let anything else be handled specially. */
6888 return NULL_TREE;
6889 }
6890 \f
6891 /* Mark EXP saying that we need to be able to take the
6892 address of it; it should not be allocated in a register.
6893 Value is true if successful. ARRAY_REF_P is true if this
6894 is for ARRAY_REF construction - in that case we don't want
6895 to look through VIEW_CONVERT_EXPR from VECTOR_TYPE to ARRAY_TYPE,
6896 it is fine to use ARRAY_REFs for vector subscripts on vector
6897 register variables.
6898
6899 C++: we do not allow `current_class_ptr' to be addressable. */
6900
6901 bool
6902 cxx_mark_addressable (tree exp, bool array_ref_p)
6903 {
6904 tree x = exp;
6905
6906 while (1)
6907 switch (TREE_CODE (x))
6908 {
6909 case VIEW_CONVERT_EXPR:
6910 if (array_ref_p
6911 && TREE_CODE (TREE_TYPE (x)) == ARRAY_TYPE
6912 && VECTOR_TYPE_P (TREE_TYPE (TREE_OPERAND (x, 0))))
6913 return true;
6914 /* FALLTHRU */
6915 case ADDR_EXPR:
6916 case COMPONENT_REF:
6917 case ARRAY_REF:
6918 case REALPART_EXPR:
6919 case IMAGPART_EXPR:
6920 x = TREE_OPERAND (x, 0);
6921 break;
6922
6923 case PARM_DECL:
6924 if (x == current_class_ptr)
6925 {
6926 error ("cannot take the address of %<this%>, which is an rvalue expression");
6927 TREE_ADDRESSABLE (x) = 1; /* so compiler doesn't die later. */
6928 return true;
6929 }
6930 /* Fall through. */
6931
6932 case VAR_DECL:
6933 /* Caller should not be trying to mark initialized
6934 constant fields addressable. */
6935 gcc_assert (DECL_LANG_SPECIFIC (x) == 0
6936 || DECL_IN_AGGR_P (x) == 0
6937 || TREE_STATIC (x)
6938 || DECL_EXTERNAL (x));
6939 /* Fall through. */
6940
6941 case RESULT_DECL:
6942 if (DECL_REGISTER (x) && !TREE_ADDRESSABLE (x)
6943 && !DECL_ARTIFICIAL (x))
6944 {
6945 if (VAR_P (x) && DECL_HARD_REGISTER (x))
6946 {
6947 error
6948 ("address of explicit register variable %qD requested", x);
6949 return false;
6950 }
6951 else if (extra_warnings)
6952 warning
6953 (OPT_Wextra, "address requested for %qD, which is declared %<register%>", x);
6954 }
6955 TREE_ADDRESSABLE (x) = 1;
6956 return true;
6957
6958 case CONST_DECL:
6959 case FUNCTION_DECL:
6960 TREE_ADDRESSABLE (x) = 1;
6961 return true;
6962
6963 case CONSTRUCTOR:
6964 TREE_ADDRESSABLE (x) = 1;
6965 return true;
6966
6967 case TARGET_EXPR:
6968 TREE_ADDRESSABLE (x) = 1;
6969 cxx_mark_addressable (TREE_OPERAND (x, 0));
6970 return true;
6971
6972 default:
6973 return true;
6974 }
6975 }
6976 \f
6977 /* Build and return a conditional expression IFEXP ? OP1 : OP2. */
6978
6979 tree
6980 build_x_conditional_expr (location_t loc, tree ifexp, tree op1, tree op2,
6981 tsubst_flags_t complain)
6982 {
6983 tree orig_ifexp = ifexp;
6984 tree orig_op1 = op1;
6985 tree orig_op2 = op2;
6986 tree expr;
6987
6988 if (processing_template_decl)
6989 {
6990 /* The standard says that the expression is type-dependent if
6991 IFEXP is type-dependent, even though the eventual type of the
6992 expression doesn't dependent on IFEXP. */
6993 if (type_dependent_expression_p (ifexp)
6994 /* As a GNU extension, the middle operand may be omitted. */
6995 || (op1 && type_dependent_expression_p (op1))
6996 || type_dependent_expression_p (op2))
6997 return build_min_nt_loc (loc, COND_EXPR, ifexp, op1, op2);
6998 ifexp = build_non_dependent_expr (ifexp);
6999 if (op1)
7000 op1 = build_non_dependent_expr (op1);
7001 op2 = build_non_dependent_expr (op2);
7002 }
7003
7004 expr = build_conditional_expr (loc, ifexp, op1, op2, complain);
7005 if (processing_template_decl && expr != error_mark_node)
7006 {
7007 tree min = build_min_non_dep (COND_EXPR, expr,
7008 orig_ifexp, orig_op1, orig_op2);
7009 expr = convert_from_reference (min);
7010 }
7011 return expr;
7012 }
7013 \f
7014 /* Given a list of expressions, return a compound expression
7015 that performs them all and returns the value of the last of them. */
7016
7017 tree
7018 build_x_compound_expr_from_list (tree list, expr_list_kind exp,
7019 tsubst_flags_t complain)
7020 {
7021 tree expr = TREE_VALUE (list);
7022
7023 if (BRACE_ENCLOSED_INITIALIZER_P (expr)
7024 && !CONSTRUCTOR_IS_DIRECT_INIT (expr))
7025 {
7026 if (complain & tf_error)
7027 pedwarn (cp_expr_loc_or_input_loc (expr), 0,
7028 "list-initializer for non-class type must not "
7029 "be parenthesized");
7030 else
7031 return error_mark_node;
7032 }
7033
7034 if (TREE_CHAIN (list))
7035 {
7036 if (complain & tf_error)
7037 switch (exp)
7038 {
7039 case ELK_INIT:
7040 permerror (input_location, "expression list treated as compound "
7041 "expression in initializer");
7042 break;
7043 case ELK_MEM_INIT:
7044 permerror (input_location, "expression list treated as compound "
7045 "expression in mem-initializer");
7046 break;
7047 case ELK_FUNC_CAST:
7048 permerror (input_location, "expression list treated as compound "
7049 "expression in functional cast");
7050 break;
7051 default:
7052 gcc_unreachable ();
7053 }
7054 else
7055 return error_mark_node;
7056
7057 for (list = TREE_CHAIN (list); list; list = TREE_CHAIN (list))
7058 expr = build_x_compound_expr (EXPR_LOCATION (TREE_VALUE (list)),
7059 expr, TREE_VALUE (list), complain);
7060 }
7061
7062 return expr;
7063 }
7064
7065 /* Like build_x_compound_expr_from_list, but using a VEC. */
7066
7067 tree
7068 build_x_compound_expr_from_vec (vec<tree, va_gc> *vec, const char *msg,
7069 tsubst_flags_t complain)
7070 {
7071 if (vec_safe_is_empty (vec))
7072 return NULL_TREE;
7073 else if (vec->length () == 1)
7074 return (*vec)[0];
7075 else
7076 {
7077 tree expr;
7078 unsigned int ix;
7079 tree t;
7080
7081 if (msg != NULL)
7082 {
7083 if (complain & tf_error)
7084 permerror (input_location,
7085 "%s expression list treated as compound expression",
7086 msg);
7087 else
7088 return error_mark_node;
7089 }
7090
7091 expr = (*vec)[0];
7092 for (ix = 1; vec->iterate (ix, &t); ++ix)
7093 expr = build_x_compound_expr (EXPR_LOCATION (t), expr,
7094 t, complain);
7095
7096 return expr;
7097 }
7098 }
7099
7100 /* Handle overloading of the ',' operator when needed. */
7101
7102 tree
7103 build_x_compound_expr (location_t loc, tree op1, tree op2,
7104 tsubst_flags_t complain)
7105 {
7106 tree result;
7107 tree orig_op1 = op1;
7108 tree orig_op2 = op2;
7109 tree overload = NULL_TREE;
7110
7111 if (processing_template_decl)
7112 {
7113 if (type_dependent_expression_p (op1)
7114 || type_dependent_expression_p (op2))
7115 return build_min_nt_loc (loc, COMPOUND_EXPR, op1, op2);
7116 op1 = build_non_dependent_expr (op1);
7117 op2 = build_non_dependent_expr (op2);
7118 }
7119
7120 result = build_new_op (loc, COMPOUND_EXPR, LOOKUP_NORMAL, op1, op2,
7121 NULL_TREE, &overload, complain);
7122 if (!result)
7123 result = cp_build_compound_expr (op1, op2, complain);
7124
7125 if (processing_template_decl && result != error_mark_node)
7126 {
7127 if (overload != NULL_TREE)
7128 return (build_min_non_dep_op_overload
7129 (COMPOUND_EXPR, result, overload, orig_op1, orig_op2));
7130
7131 return build_min_non_dep (COMPOUND_EXPR, result, orig_op1, orig_op2);
7132 }
7133
7134 return result;
7135 }
7136
7137 /* Like cp_build_compound_expr, but for the c-common bits. */
7138
7139 tree
7140 build_compound_expr (location_t /*loc*/, tree lhs, tree rhs)
7141 {
7142 return cp_build_compound_expr (lhs, rhs, tf_warning_or_error);
7143 }
7144
7145 /* Build a compound expression. */
7146
7147 tree
7148 cp_build_compound_expr (tree lhs, tree rhs, tsubst_flags_t complain)
7149 {
7150 lhs = convert_to_void (lhs, ICV_LEFT_OF_COMMA, complain);
7151
7152 if (lhs == error_mark_node || rhs == error_mark_node)
7153 return error_mark_node;
7154
7155 if (TREE_CODE (rhs) == TARGET_EXPR)
7156 {
7157 /* If the rhs is a TARGET_EXPR, then build the compound
7158 expression inside the target_expr's initializer. This
7159 helps the compiler to eliminate unnecessary temporaries. */
7160 tree init = TREE_OPERAND (rhs, 1);
7161
7162 init = build2 (COMPOUND_EXPR, TREE_TYPE (init), lhs, init);
7163 TREE_OPERAND (rhs, 1) = init;
7164
7165 return rhs;
7166 }
7167
7168 if (type_unknown_p (rhs))
7169 {
7170 if (complain & tf_error)
7171 error_at (cp_expr_loc_or_input_loc (rhs),
7172 "no context to resolve type of %qE", rhs);
7173 return error_mark_node;
7174 }
7175
7176 return build2 (COMPOUND_EXPR, TREE_TYPE (rhs), lhs, rhs);
7177 }
7178
7179 /* Issue a diagnostic message if casting from SRC_TYPE to DEST_TYPE
7180 casts away constness. CAST gives the type of cast. Returns true
7181 if the cast is ill-formed, false if it is well-formed.
7182
7183 ??? This function warns for casting away any qualifier not just
7184 const. We would like to specify exactly what qualifiers are casted
7185 away.
7186 */
7187
7188 static bool
7189 check_for_casting_away_constness (location_t loc, tree src_type,
7190 tree dest_type, enum tree_code cast,
7191 tsubst_flags_t complain)
7192 {
7193 /* C-style casts are allowed to cast away constness. With
7194 WARN_CAST_QUAL, we still want to issue a warning. */
7195 if (cast == CAST_EXPR && !warn_cast_qual)
7196 return false;
7197
7198 if (!casts_away_constness (src_type, dest_type, complain))
7199 return false;
7200
7201 switch (cast)
7202 {
7203 case CAST_EXPR:
7204 if (complain & tf_warning)
7205 warning_at (loc, OPT_Wcast_qual,
7206 "cast from type %qT to type %qT casts away qualifiers",
7207 src_type, dest_type);
7208 return false;
7209
7210 case STATIC_CAST_EXPR:
7211 if (complain & tf_error)
7212 error_at (loc, "%<static_cast%> from type %qT to type %qT casts "
7213 "away qualifiers",
7214 src_type, dest_type);
7215 return true;
7216
7217 case REINTERPRET_CAST_EXPR:
7218 if (complain & tf_error)
7219 error_at (loc, "%<reinterpret_cast%> from type %qT to type %qT "
7220 "casts away qualifiers",
7221 src_type, dest_type);
7222 return true;
7223
7224 default:
7225 gcc_unreachable();
7226 }
7227 }
7228
7229 /* Warns if the cast from expression EXPR to type TYPE is useless. */
7230 void
7231 maybe_warn_about_useless_cast (location_t loc, tree type, tree expr,
7232 tsubst_flags_t complain)
7233 {
7234 if (warn_useless_cast
7235 && complain & tf_warning)
7236 {
7237 if ((TYPE_REF_P (type)
7238 && (TYPE_REF_IS_RVALUE (type)
7239 ? xvalue_p (expr) : lvalue_p (expr))
7240 && same_type_p (TREE_TYPE (expr), TREE_TYPE (type)))
7241 || same_type_p (TREE_TYPE (expr), type))
7242 warning_at (loc, OPT_Wuseless_cast,
7243 "useless cast to type %q#T", type);
7244 }
7245 }
7246
7247 /* Warns if the cast ignores cv-qualifiers on TYPE. */
7248 static void
7249 maybe_warn_about_cast_ignoring_quals (location_t loc, tree type,
7250 tsubst_flags_t complain)
7251 {
7252 if (warn_ignored_qualifiers
7253 && complain & tf_warning
7254 && !CLASS_TYPE_P (type)
7255 && (cp_type_quals (type) & (TYPE_QUAL_CONST|TYPE_QUAL_VOLATILE)))
7256 warning_at (loc, OPT_Wignored_qualifiers,
7257 "type qualifiers ignored on cast result type");
7258 }
7259
7260 /* Convert EXPR (an expression with pointer-to-member type) to TYPE
7261 (another pointer-to-member type in the same hierarchy) and return
7262 the converted expression. If ALLOW_INVERSE_P is permitted, a
7263 pointer-to-derived may be converted to pointer-to-base; otherwise,
7264 only the other direction is permitted. If C_CAST_P is true, this
7265 conversion is taking place as part of a C-style cast. */
7266
7267 tree
7268 convert_ptrmem (tree type, tree expr, bool allow_inverse_p,
7269 bool c_cast_p, tsubst_flags_t complain)
7270 {
7271 if (same_type_p (type, TREE_TYPE (expr)))
7272 return expr;
7273
7274 if (TYPE_PTRDATAMEM_P (type))
7275 {
7276 tree obase = TYPE_PTRMEM_CLASS_TYPE (TREE_TYPE (expr));
7277 tree nbase = TYPE_PTRMEM_CLASS_TYPE (type);
7278 tree delta = (get_delta_difference
7279 (obase, nbase,
7280 allow_inverse_p, c_cast_p, complain));
7281
7282 if (delta == error_mark_node)
7283 return error_mark_node;
7284
7285 if (!same_type_p (obase, nbase))
7286 {
7287 if (TREE_CODE (expr) == PTRMEM_CST)
7288 expr = cplus_expand_constant (expr);
7289
7290 tree cond = cp_build_binary_op (input_location, EQ_EXPR, expr,
7291 build_int_cst (TREE_TYPE (expr), -1),
7292 complain);
7293 tree op1 = build_nop (ptrdiff_type_node, expr);
7294 tree op2 = cp_build_binary_op (input_location, PLUS_EXPR, op1, delta,
7295 complain);
7296
7297 expr = fold_build3_loc (input_location,
7298 COND_EXPR, ptrdiff_type_node, cond, op1, op2);
7299 }
7300
7301 return build_nop (type, expr);
7302 }
7303 else
7304 return build_ptrmemfunc (TYPE_PTRMEMFUNC_FN_TYPE (type), expr,
7305 allow_inverse_p, c_cast_p, complain);
7306 }
7307
7308 /* Perform a static_cast from EXPR to TYPE. When C_CAST_P is true,
7309 this static_cast is being attempted as one of the possible casts
7310 allowed by a C-style cast. (In that case, accessibility of base
7311 classes is not considered, and it is OK to cast away
7312 constness.) Return the result of the cast. *VALID_P is set to
7313 indicate whether or not the cast was valid. */
7314
7315 static tree
7316 build_static_cast_1 (location_t loc, tree type, tree expr, bool c_cast_p,
7317 bool *valid_p, tsubst_flags_t complain)
7318 {
7319 tree intype;
7320 tree result;
7321 cp_lvalue_kind clk;
7322
7323 /* Assume the cast is valid. */
7324 *valid_p = true;
7325
7326 intype = unlowered_expr_type (expr);
7327
7328 /* Save casted types in the function's used types hash table. */
7329 used_types_insert (type);
7330
7331 /* A prvalue of non-class type is cv-unqualified. */
7332 if (!CLASS_TYPE_P (type))
7333 type = cv_unqualified (type);
7334
7335 /* [expr.static.cast]
7336
7337 An lvalue of type "cv1 B", where B is a class type, can be cast
7338 to type "reference to cv2 D", where D is a class derived (clause
7339 _class.derived_) from B, if a valid standard conversion from
7340 "pointer to D" to "pointer to B" exists (_conv.ptr_), cv2 is the
7341 same cv-qualification as, or greater cv-qualification than, cv1,
7342 and B is not a virtual base class of D. */
7343 /* We check this case before checking the validity of "TYPE t =
7344 EXPR;" below because for this case:
7345
7346 struct B {};
7347 struct D : public B { D(const B&); };
7348 extern B& b;
7349 void f() { static_cast<const D&>(b); }
7350
7351 we want to avoid constructing a new D. The standard is not
7352 completely clear about this issue, but our interpretation is
7353 consistent with other compilers. */
7354 if (TYPE_REF_P (type)
7355 && CLASS_TYPE_P (TREE_TYPE (type))
7356 && CLASS_TYPE_P (intype)
7357 && (TYPE_REF_IS_RVALUE (type) || lvalue_p (expr))
7358 && DERIVED_FROM_P (intype, TREE_TYPE (type))
7359 && can_convert (build_pointer_type (TYPE_MAIN_VARIANT (intype)),
7360 build_pointer_type (TYPE_MAIN_VARIANT
7361 (TREE_TYPE (type))),
7362 complain)
7363 && (c_cast_p
7364 || at_least_as_qualified_p (TREE_TYPE (type), intype)))
7365 {
7366 tree base;
7367
7368 if (processing_template_decl)
7369 return expr;
7370
7371 /* There is a standard conversion from "D*" to "B*" even if "B"
7372 is ambiguous or inaccessible. If this is really a
7373 static_cast, then we check both for inaccessibility and
7374 ambiguity. However, if this is a static_cast being performed
7375 because the user wrote a C-style cast, then accessibility is
7376 not considered. */
7377 base = lookup_base (TREE_TYPE (type), intype,
7378 c_cast_p ? ba_unique : ba_check,
7379 NULL, complain);
7380 expr = cp_build_addr_expr (expr, complain);
7381
7382 if (sanitize_flags_p (SANITIZE_VPTR))
7383 {
7384 tree ubsan_check
7385 = cp_ubsan_maybe_instrument_downcast (loc, type,
7386 intype, expr);
7387 if (ubsan_check)
7388 expr = ubsan_check;
7389 }
7390
7391 /* Convert from "B*" to "D*". This function will check that "B"
7392 is not a virtual base of "D". Even if we don't have a guarantee
7393 that expr is NULL, if the static_cast is to a reference type,
7394 it is UB if it would be NULL, so omit the non-NULL check. */
7395 expr = build_base_path (MINUS_EXPR, expr, base,
7396 /*nonnull=*/flag_delete_null_pointer_checks,
7397 complain);
7398
7399 /* Convert the pointer to a reference -- but then remember that
7400 there are no expressions with reference type in C++.
7401
7402 We call rvalue so that there's an actual tree code
7403 (NON_LVALUE_EXPR) for the static_cast; otherwise, if the operand
7404 is a variable with the same type, the conversion would get folded
7405 away, leaving just the variable and causing lvalue_kind to give
7406 the wrong answer. */
7407 expr = cp_fold_convert (type, expr);
7408
7409 /* When -fsanitize=null, make sure to diagnose reference binding to
7410 NULL even when the reference is converted to pointer later on. */
7411 if (sanitize_flags_p (SANITIZE_NULL)
7412 && TREE_CODE (expr) == COND_EXPR
7413 && TREE_OPERAND (expr, 2)
7414 && TREE_CODE (TREE_OPERAND (expr, 2)) == INTEGER_CST
7415 && TREE_TYPE (TREE_OPERAND (expr, 2)) == type)
7416 ubsan_maybe_instrument_reference (&TREE_OPERAND (expr, 2));
7417
7418 return convert_from_reference (rvalue (expr));
7419 }
7420
7421 /* "A glvalue of type cv1 T1 can be cast to type rvalue reference to
7422 cv2 T2 if cv2 T2 is reference-compatible with cv1 T1 (8.5.3)." */
7423 if (TYPE_REF_P (type)
7424 && TYPE_REF_IS_RVALUE (type)
7425 && (clk = real_lvalue_p (expr))
7426 && reference_compatible_p (TREE_TYPE (type), intype)
7427 && (c_cast_p || at_least_as_qualified_p (TREE_TYPE (type), intype)))
7428 {
7429 if (processing_template_decl)
7430 return expr;
7431 if (clk == clk_ordinary)
7432 {
7433 /* Handle the (non-bit-field) lvalue case here by casting to
7434 lvalue reference and then changing it to an rvalue reference.
7435 Casting an xvalue to rvalue reference will be handled by the
7436 main code path. */
7437 tree lref = cp_build_reference_type (TREE_TYPE (type), false);
7438 result = (perform_direct_initialization_if_possible
7439 (lref, expr, c_cast_p, complain));
7440 result = build1 (NON_LVALUE_EXPR, type, result);
7441 return convert_from_reference (result);
7442 }
7443 else
7444 /* For a bit-field or packed field, bind to a temporary. */
7445 expr = rvalue (expr);
7446 }
7447
7448 /* Resolve overloaded address here rather than once in
7449 implicit_conversion and again in the inverse code below. */
7450 if (TYPE_PTRMEMFUNC_P (type) && type_unknown_p (expr))
7451 {
7452 expr = instantiate_type (type, expr, complain);
7453 intype = TREE_TYPE (expr);
7454 }
7455
7456 /* [expr.static.cast]
7457
7458 Any expression can be explicitly converted to type cv void. */
7459 if (VOID_TYPE_P (type))
7460 return convert_to_void (expr, ICV_CAST, complain);
7461
7462 /* [class.abstract]
7463 An abstract class shall not be used ... as the type of an explicit
7464 conversion. */
7465 if (abstract_virtuals_error_sfinae (ACU_CAST, type, complain))
7466 return error_mark_node;
7467
7468 /* [expr.static.cast]
7469
7470 An expression e can be explicitly converted to a type T using a
7471 static_cast of the form static_cast<T>(e) if the declaration T
7472 t(e);" is well-formed, for some invented temporary variable
7473 t. */
7474 result = perform_direct_initialization_if_possible (type, expr,
7475 c_cast_p, complain);
7476 if (result)
7477 {
7478 if (processing_template_decl)
7479 return expr;
7480
7481 result = convert_from_reference (result);
7482
7483 /* [expr.static.cast]
7484
7485 If T is a reference type, the result is an lvalue; otherwise,
7486 the result is an rvalue. */
7487 if (!TYPE_REF_P (type))
7488 {
7489 result = rvalue (result);
7490
7491 if (result == expr && SCALAR_TYPE_P (type))
7492 /* Leave some record of the cast. */
7493 result = build_nop (type, expr);
7494 }
7495 return result;
7496 }
7497
7498 /* [expr.static.cast]
7499
7500 The inverse of any standard conversion sequence (clause _conv_),
7501 other than the lvalue-to-rvalue (_conv.lval_), array-to-pointer
7502 (_conv.array_), function-to-pointer (_conv.func_), and boolean
7503 (_conv.bool_) conversions, can be performed explicitly using
7504 static_cast subject to the restriction that the explicit
7505 conversion does not cast away constness (_expr.const.cast_), and
7506 the following additional rules for specific cases: */
7507 /* For reference, the conversions not excluded are: integral
7508 promotions, floating-point promotion, integral conversions,
7509 floating-point conversions, floating-integral conversions,
7510 pointer conversions, and pointer to member conversions. */
7511 /* DR 128
7512
7513 A value of integral _or enumeration_ type can be explicitly
7514 converted to an enumeration type. */
7515 /* The effect of all that is that any conversion between any two
7516 types which are integral, floating, or enumeration types can be
7517 performed. */
7518 if ((INTEGRAL_OR_ENUMERATION_TYPE_P (type)
7519 || SCALAR_FLOAT_TYPE_P (type))
7520 && (INTEGRAL_OR_ENUMERATION_TYPE_P (intype)
7521 || SCALAR_FLOAT_TYPE_P (intype)))
7522 {
7523 if (processing_template_decl)
7524 return expr;
7525 return ocp_convert (type, expr, CONV_C_CAST, LOOKUP_NORMAL, complain);
7526 }
7527
7528 if (TYPE_PTR_P (type) && TYPE_PTR_P (intype)
7529 && CLASS_TYPE_P (TREE_TYPE (type))
7530 && CLASS_TYPE_P (TREE_TYPE (intype))
7531 && can_convert (build_pointer_type (TYPE_MAIN_VARIANT
7532 (TREE_TYPE (intype))),
7533 build_pointer_type (TYPE_MAIN_VARIANT
7534 (TREE_TYPE (type))),
7535 complain))
7536 {
7537 tree base;
7538
7539 if (processing_template_decl)
7540 return expr;
7541
7542 if (!c_cast_p
7543 && check_for_casting_away_constness (loc, intype, type,
7544 STATIC_CAST_EXPR,
7545 complain))
7546 return error_mark_node;
7547 base = lookup_base (TREE_TYPE (type), TREE_TYPE (intype),
7548 c_cast_p ? ba_unique : ba_check,
7549 NULL, complain);
7550 expr = build_base_path (MINUS_EXPR, expr, base, /*nonnull=*/false,
7551 complain);
7552
7553 if (sanitize_flags_p (SANITIZE_VPTR))
7554 {
7555 tree ubsan_check
7556 = cp_ubsan_maybe_instrument_downcast (loc, type,
7557 intype, expr);
7558 if (ubsan_check)
7559 expr = ubsan_check;
7560 }
7561
7562 return cp_fold_convert (type, expr);
7563 }
7564
7565 if ((TYPE_PTRDATAMEM_P (type) && TYPE_PTRDATAMEM_P (intype))
7566 || (TYPE_PTRMEMFUNC_P (type) && TYPE_PTRMEMFUNC_P (intype)))
7567 {
7568 tree c1;
7569 tree c2;
7570 tree t1;
7571 tree t2;
7572
7573 c1 = TYPE_PTRMEM_CLASS_TYPE (intype);
7574 c2 = TYPE_PTRMEM_CLASS_TYPE (type);
7575
7576 if (TYPE_PTRDATAMEM_P (type))
7577 {
7578 t1 = (build_ptrmem_type
7579 (c1,
7580 TYPE_MAIN_VARIANT (TYPE_PTRMEM_POINTED_TO_TYPE (intype))));
7581 t2 = (build_ptrmem_type
7582 (c2,
7583 TYPE_MAIN_VARIANT (TYPE_PTRMEM_POINTED_TO_TYPE (type))));
7584 }
7585 else
7586 {
7587 t1 = intype;
7588 t2 = type;
7589 }
7590 if (can_convert (t1, t2, complain) || can_convert (t2, t1, complain))
7591 {
7592 if (!c_cast_p
7593 && check_for_casting_away_constness (loc, intype, type,
7594 STATIC_CAST_EXPR,
7595 complain))
7596 return error_mark_node;
7597 if (processing_template_decl)
7598 return expr;
7599 return convert_ptrmem (type, expr, /*allow_inverse_p=*/1,
7600 c_cast_p, complain);
7601 }
7602 }
7603
7604 /* [expr.static.cast]
7605
7606 An rvalue of type "pointer to cv void" can be explicitly
7607 converted to a pointer to object type. A value of type pointer
7608 to object converted to "pointer to cv void" and back to the
7609 original pointer type will have its original value. */
7610 if (TYPE_PTR_P (intype)
7611 && VOID_TYPE_P (TREE_TYPE (intype))
7612 && TYPE_PTROB_P (type))
7613 {
7614 if (!c_cast_p
7615 && check_for_casting_away_constness (loc, intype, type,
7616 STATIC_CAST_EXPR,
7617 complain))
7618 return error_mark_node;
7619 if (processing_template_decl)
7620 return expr;
7621 return build_nop (type, expr);
7622 }
7623
7624 *valid_p = false;
7625 return error_mark_node;
7626 }
7627
7628 /* Return an expression representing static_cast<TYPE>(EXPR). */
7629
7630 tree
7631 build_static_cast (location_t loc, tree type, tree oexpr,
7632 tsubst_flags_t complain)
7633 {
7634 tree expr = oexpr;
7635 tree result;
7636 bool valid_p;
7637
7638 if (type == error_mark_node || expr == error_mark_node)
7639 return error_mark_node;
7640
7641 bool dependent = (dependent_type_p (type)
7642 || type_dependent_expression_p (expr));
7643 if (dependent)
7644 {
7645 tmpl:
7646 expr = build_min (STATIC_CAST_EXPR, type, oexpr);
7647 /* We don't know if it will or will not have side effects. */
7648 TREE_SIDE_EFFECTS (expr) = 1;
7649 result = convert_from_reference (expr);
7650 protected_set_expr_location (result, loc);
7651 return result;
7652 }
7653 else if (processing_template_decl)
7654 expr = build_non_dependent_expr (expr);
7655
7656 /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
7657 Strip such NOP_EXPRs if VALUE is being used in non-lvalue context. */
7658 if (!TYPE_REF_P (type)
7659 && TREE_CODE (expr) == NOP_EXPR
7660 && TREE_TYPE (expr) == TREE_TYPE (TREE_OPERAND (expr, 0)))
7661 expr = TREE_OPERAND (expr, 0);
7662
7663 result = build_static_cast_1 (loc, type, expr, /*c_cast_p=*/false,
7664 &valid_p, complain);
7665 if (valid_p)
7666 {
7667 if (result != error_mark_node)
7668 {
7669 maybe_warn_about_useless_cast (loc, type, expr, complain);
7670 maybe_warn_about_cast_ignoring_quals (loc, type, complain);
7671 }
7672 if (processing_template_decl)
7673 goto tmpl;
7674 protected_set_expr_location (result, loc);
7675 return result;
7676 }
7677
7678 if (complain & tf_error)
7679 {
7680 error_at (loc, "invalid %<static_cast%> from type %qT to type %qT",
7681 TREE_TYPE (expr), type);
7682 if ((TYPE_PTR_P (type) || TYPE_REF_P (type))
7683 && CLASS_TYPE_P (TREE_TYPE (type))
7684 && !COMPLETE_TYPE_P (TREE_TYPE (type)))
7685 inform (DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (TREE_TYPE (type))),
7686 "class type %qT is incomplete", TREE_TYPE (type));
7687 tree expr_type = TREE_TYPE (expr);
7688 if (TYPE_PTR_P (expr_type))
7689 expr_type = TREE_TYPE (expr_type);
7690 if (CLASS_TYPE_P (expr_type) && !COMPLETE_TYPE_P (expr_type))
7691 inform (DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (expr_type)),
7692 "class type %qT is incomplete", expr_type);
7693 }
7694 return error_mark_node;
7695 }
7696
7697 /* EXPR is an expression with member function or pointer-to-member
7698 function type. TYPE is a pointer type. Converting EXPR to TYPE is
7699 not permitted by ISO C++, but we accept it in some modes. If we
7700 are not in one of those modes, issue a diagnostic. Return the
7701 converted expression. */
7702
7703 tree
7704 convert_member_func_to_ptr (tree type, tree expr, tsubst_flags_t complain)
7705 {
7706 tree intype;
7707 tree decl;
7708
7709 intype = TREE_TYPE (expr);
7710 gcc_assert (TYPE_PTRMEMFUNC_P (intype)
7711 || TREE_CODE (intype) == METHOD_TYPE);
7712
7713 if (!(complain & tf_warning_or_error))
7714 return error_mark_node;
7715
7716 if (pedantic || warn_pmf2ptr)
7717 pedwarn (input_location, pedantic ? OPT_Wpedantic : OPT_Wpmf_conversions,
7718 "converting from %qH to %qI", intype, type);
7719
7720 if (TREE_CODE (intype) == METHOD_TYPE)
7721 expr = build_addr_func (expr, complain);
7722 else if (TREE_CODE (expr) == PTRMEM_CST)
7723 expr = build_address (PTRMEM_CST_MEMBER (expr));
7724 else
7725 {
7726 decl = maybe_dummy_object (TYPE_PTRMEM_CLASS_TYPE (intype), 0);
7727 decl = build_address (decl);
7728 expr = get_member_function_from_ptrfunc (&decl, expr, complain);
7729 }
7730
7731 if (expr == error_mark_node)
7732 return error_mark_node;
7733
7734 return build_nop (type, expr);
7735 }
7736
7737 /* Build a NOP_EXPR to TYPE, but mark it as a reinterpret_cast so that
7738 constexpr evaluation knows to reject it. */
7739
7740 static tree
7741 build_nop_reinterpret (tree type, tree expr)
7742 {
7743 tree ret = build_nop (type, expr);
7744 if (ret != expr)
7745 REINTERPRET_CAST_P (ret) = true;
7746 return ret;
7747 }
7748
7749 /* Return a representation for a reinterpret_cast from EXPR to TYPE.
7750 If C_CAST_P is true, this reinterpret cast is being done as part of
7751 a C-style cast. If VALID_P is non-NULL, *VALID_P is set to
7752 indicate whether or not reinterpret_cast was valid. */
7753
7754 static tree
7755 build_reinterpret_cast_1 (location_t loc, tree type, tree expr,
7756 bool c_cast_p, bool *valid_p,
7757 tsubst_flags_t complain)
7758 {
7759 tree intype;
7760
7761 /* Assume the cast is invalid. */
7762 if (valid_p)
7763 *valid_p = true;
7764
7765 if (type == error_mark_node || error_operand_p (expr))
7766 return error_mark_node;
7767
7768 intype = TREE_TYPE (expr);
7769
7770 /* Save casted types in the function's used types hash table. */
7771 used_types_insert (type);
7772
7773 /* A prvalue of non-class type is cv-unqualified. */
7774 if (!CLASS_TYPE_P (type))
7775 type = cv_unqualified (type);
7776
7777 /* [expr.reinterpret.cast]
7778 A glvalue expression of type T1 can be cast to the type
7779 "reference to T2" if an expression of type "pointer to T1" can be
7780 explicitly converted to the type "pointer to T2" using a
7781 reinterpret_cast. */
7782 if (TYPE_REF_P (type))
7783 {
7784 if (TYPE_REF_IS_RVALUE (type) && !VOID_TYPE_P (intype))
7785 {
7786 if (!obvalue_p (expr))
7787 /* Perform the temporary materialization conversion. */
7788 expr = get_target_expr_sfinae (expr, complain);
7789 }
7790 else if (!lvalue_p (expr))
7791 {
7792 if (complain & tf_error)
7793 error_at (loc, "invalid cast of an rvalue expression of type "
7794 "%qT to type %qT",
7795 intype, type);
7796 return error_mark_node;
7797 }
7798
7799 /* Warn about a reinterpret_cast from "A*" to "B&" if "A" and
7800 "B" are related class types; the reinterpret_cast does not
7801 adjust the pointer. */
7802 if (TYPE_PTR_P (intype)
7803 && (complain & tf_warning)
7804 && (comptypes (TREE_TYPE (intype), TREE_TYPE (type),
7805 COMPARE_BASE | COMPARE_DERIVED)))
7806 warning_at (loc, 0, "casting %qT to %qT does not dereference pointer",
7807 intype, type);
7808
7809 expr = cp_build_addr_expr (expr, complain);
7810
7811 if (warn_strict_aliasing > 2)
7812 cp_strict_aliasing_warning (EXPR_LOCATION (expr), type, expr);
7813
7814 if (expr != error_mark_node)
7815 expr = build_reinterpret_cast_1
7816 (loc, build_pointer_type (TREE_TYPE (type)), expr, c_cast_p,
7817 valid_p, complain);
7818 if (expr != error_mark_node)
7819 /* cp_build_indirect_ref isn't right for rvalue refs. */
7820 expr = convert_from_reference (fold_convert (type, expr));
7821 return expr;
7822 }
7823
7824 /* As a G++ extension, we consider conversions from member
7825 functions, and pointers to member functions to
7826 pointer-to-function and pointer-to-void types. If
7827 -Wno-pmf-conversions has not been specified,
7828 convert_member_func_to_ptr will issue an error message. */
7829 if ((TYPE_PTRMEMFUNC_P (intype)
7830 || TREE_CODE (intype) == METHOD_TYPE)
7831 && TYPE_PTR_P (type)
7832 && (TREE_CODE (TREE_TYPE (type)) == FUNCTION_TYPE
7833 || VOID_TYPE_P (TREE_TYPE (type))))
7834 return convert_member_func_to_ptr (type, expr, complain);
7835
7836 /* If the cast is not to a reference type, the lvalue-to-rvalue,
7837 array-to-pointer, and function-to-pointer conversions are
7838 performed. */
7839 expr = decay_conversion (expr, complain);
7840
7841 /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
7842 Strip such NOP_EXPRs if VALUE is being used in non-lvalue context. */
7843 if (TREE_CODE (expr) == NOP_EXPR
7844 && TREE_TYPE (expr) == TREE_TYPE (TREE_OPERAND (expr, 0)))
7845 expr = TREE_OPERAND (expr, 0);
7846
7847 if (error_operand_p (expr))
7848 return error_mark_node;
7849
7850 intype = TREE_TYPE (expr);
7851
7852 /* [expr.reinterpret.cast]
7853 A pointer can be converted to any integral type large enough to
7854 hold it. ... A value of type std::nullptr_t can be converted to
7855 an integral type; the conversion has the same meaning and
7856 validity as a conversion of (void*)0 to the integral type. */
7857 if (CP_INTEGRAL_TYPE_P (type)
7858 && (TYPE_PTR_P (intype) || NULLPTR_TYPE_P (intype)))
7859 {
7860 if (TYPE_PRECISION (type) < TYPE_PRECISION (intype))
7861 {
7862 if (complain & tf_error)
7863 permerror (loc, "cast from %qH to %qI loses precision",
7864 intype, type);
7865 else
7866 return error_mark_node;
7867 }
7868 if (NULLPTR_TYPE_P (intype))
7869 return build_int_cst (type, 0);
7870 }
7871 /* [expr.reinterpret.cast]
7872 A value of integral or enumeration type can be explicitly
7873 converted to a pointer. */
7874 else if (TYPE_PTR_P (type) && INTEGRAL_OR_ENUMERATION_TYPE_P (intype))
7875 /* OK */
7876 ;
7877 else if ((INTEGRAL_OR_ENUMERATION_TYPE_P (type)
7878 || TYPE_PTR_OR_PTRMEM_P (type))
7879 && same_type_p (type, intype))
7880 /* DR 799 */
7881 return rvalue (expr);
7882 else if (TYPE_PTRFN_P (type) && TYPE_PTRFN_P (intype))
7883 {
7884 if ((complain & tf_warning)
7885 && !cxx_safe_function_type_cast_p (TREE_TYPE (type),
7886 TREE_TYPE (intype)))
7887 warning_at (loc, OPT_Wcast_function_type,
7888 "cast between incompatible function types"
7889 " from %qH to %qI", intype, type);
7890 return build_nop_reinterpret (type, expr);
7891 }
7892 else if (TYPE_PTRMEMFUNC_P (type) && TYPE_PTRMEMFUNC_P (intype))
7893 {
7894 if ((complain & tf_warning)
7895 && !cxx_safe_function_type_cast_p
7896 (TREE_TYPE (TYPE_PTRMEMFUNC_FN_TYPE_RAW (type)),
7897 TREE_TYPE (TYPE_PTRMEMFUNC_FN_TYPE_RAW (intype))))
7898 warning_at (loc, OPT_Wcast_function_type,
7899 "cast between incompatible pointer to member types"
7900 " from %qH to %qI", intype, type);
7901 return build_nop_reinterpret (type, expr);
7902 }
7903 else if ((TYPE_PTRDATAMEM_P (type) && TYPE_PTRDATAMEM_P (intype))
7904 || (TYPE_PTROBV_P (type) && TYPE_PTROBV_P (intype)))
7905 {
7906 if (!c_cast_p
7907 && check_for_casting_away_constness (loc, intype, type,
7908 REINTERPRET_CAST_EXPR,
7909 complain))
7910 return error_mark_node;
7911 /* Warn about possible alignment problems. */
7912 if ((STRICT_ALIGNMENT || warn_cast_align == 2)
7913 && (complain & tf_warning)
7914 && !VOID_TYPE_P (type)
7915 && TREE_CODE (TREE_TYPE (intype)) != FUNCTION_TYPE
7916 && COMPLETE_TYPE_P (TREE_TYPE (type))
7917 && COMPLETE_TYPE_P (TREE_TYPE (intype))
7918 && min_align_of_type (TREE_TYPE (type))
7919 > min_align_of_type (TREE_TYPE (intype)))
7920 warning_at (loc, OPT_Wcast_align, "cast from %qH to %qI "
7921 "increases required alignment of target type",
7922 intype, type);
7923
7924 if (warn_strict_aliasing <= 2)
7925 /* strict_aliasing_warning STRIP_NOPs its expr. */
7926 cp_strict_aliasing_warning (EXPR_LOCATION (expr), type, expr);
7927
7928 return build_nop_reinterpret (type, expr);
7929 }
7930 else if ((TYPE_PTRFN_P (type) && TYPE_PTROBV_P (intype))
7931 || (TYPE_PTRFN_P (intype) && TYPE_PTROBV_P (type)))
7932 {
7933 if (complain & tf_warning)
7934 /* C++11 5.2.10 p8 says that "Converting a function pointer to an
7935 object pointer type or vice versa is conditionally-supported." */
7936 warning_at (loc, OPT_Wconditionally_supported,
7937 "casting between pointer-to-function and "
7938 "pointer-to-object is conditionally-supported");
7939 return build_nop_reinterpret (type, expr);
7940 }
7941 else if (gnu_vector_type_p (type))
7942 return convert_to_vector (type, rvalue (expr));
7943 else if (gnu_vector_type_p (intype)
7944 && INTEGRAL_OR_ENUMERATION_TYPE_P (type))
7945 return convert_to_integer_nofold (type, expr);
7946 else
7947 {
7948 if (valid_p)
7949 *valid_p = false;
7950 if (complain & tf_error)
7951 error_at (loc, "invalid cast from type %qT to type %qT",
7952 intype, type);
7953 return error_mark_node;
7954 }
7955
7956 expr = cp_convert (type, expr, complain);
7957 if (TREE_CODE (expr) == NOP_EXPR)
7958 /* Mark any nop_expr that created as a reintepret_cast. */
7959 REINTERPRET_CAST_P (expr) = true;
7960 return expr;
7961 }
7962
7963 tree
7964 build_reinterpret_cast (location_t loc, tree type, tree expr,
7965 tsubst_flags_t complain)
7966 {
7967 tree r;
7968
7969 if (type == error_mark_node || expr == error_mark_node)
7970 return error_mark_node;
7971
7972 if (processing_template_decl)
7973 {
7974 tree t = build_min (REINTERPRET_CAST_EXPR, type, expr);
7975
7976 if (!TREE_SIDE_EFFECTS (t)
7977 && type_dependent_expression_p (expr))
7978 /* There might turn out to be side effects inside expr. */
7979 TREE_SIDE_EFFECTS (t) = 1;
7980 r = convert_from_reference (t);
7981 protected_set_expr_location (r, loc);
7982 return r;
7983 }
7984
7985 r = build_reinterpret_cast_1 (loc, type, expr, /*c_cast_p=*/false,
7986 /*valid_p=*/NULL, complain);
7987 if (r != error_mark_node)
7988 {
7989 maybe_warn_about_useless_cast (loc, type, expr, complain);
7990 maybe_warn_about_cast_ignoring_quals (loc, type, complain);
7991 }
7992 protected_set_expr_location (r, loc);
7993 return r;
7994 }
7995
7996 /* Perform a const_cast from EXPR to TYPE. If the cast is valid,
7997 return an appropriate expression. Otherwise, return
7998 error_mark_node. If the cast is not valid, and COMPLAIN is true,
7999 then a diagnostic will be issued. If VALID_P is non-NULL, we are
8000 performing a C-style cast, its value upon return will indicate
8001 whether or not the conversion succeeded. */
8002
8003 static tree
8004 build_const_cast_1 (location_t loc, tree dst_type, tree expr,
8005 tsubst_flags_t complain, bool *valid_p)
8006 {
8007 tree src_type;
8008 tree reference_type;
8009
8010 /* Callers are responsible for handling error_mark_node as a
8011 destination type. */
8012 gcc_assert (dst_type != error_mark_node);
8013 /* In a template, callers should be building syntactic
8014 representations of casts, not using this machinery. */
8015 gcc_assert (!processing_template_decl);
8016
8017 /* Assume the conversion is invalid. */
8018 if (valid_p)
8019 *valid_p = false;
8020
8021 if (!INDIRECT_TYPE_P (dst_type) && !TYPE_PTRDATAMEM_P (dst_type))
8022 {
8023 if (complain & tf_error)
8024 error_at (loc, "invalid use of %<const_cast%> with type %qT, "
8025 "which is not a pointer, reference, "
8026 "nor a pointer-to-data-member type", dst_type);
8027 return error_mark_node;
8028 }
8029
8030 if (TREE_CODE (TREE_TYPE (dst_type)) == FUNCTION_TYPE)
8031 {
8032 if (complain & tf_error)
8033 error_at (loc, "invalid use of %<const_cast%> with type %qT, "
8034 "which is a pointer or reference to a function type",
8035 dst_type);
8036 return error_mark_node;
8037 }
8038
8039 /* A prvalue of non-class type is cv-unqualified. */
8040 dst_type = cv_unqualified (dst_type);
8041
8042 /* Save casted types in the function's used types hash table. */
8043 used_types_insert (dst_type);
8044
8045 src_type = TREE_TYPE (expr);
8046 /* Expressions do not really have reference types. */
8047 if (TYPE_REF_P (src_type))
8048 src_type = TREE_TYPE (src_type);
8049
8050 /* [expr.const.cast]
8051
8052 For two object types T1 and T2, if a pointer to T1 can be explicitly
8053 converted to the type "pointer to T2" using a const_cast, then the
8054 following conversions can also be made:
8055
8056 -- an lvalue of type T1 can be explicitly converted to an lvalue of
8057 type T2 using the cast const_cast<T2&>;
8058
8059 -- a glvalue of type T1 can be explicitly converted to an xvalue of
8060 type T2 using the cast const_cast<T2&&>; and
8061
8062 -- if T1 is a class type, a prvalue of type T1 can be explicitly
8063 converted to an xvalue of type T2 using the cast const_cast<T2&&>. */
8064
8065 if (TYPE_REF_P (dst_type))
8066 {
8067 reference_type = dst_type;
8068 if (!TYPE_REF_IS_RVALUE (dst_type)
8069 ? lvalue_p (expr)
8070 : obvalue_p (expr))
8071 /* OK. */;
8072 else
8073 {
8074 if (complain & tf_error)
8075 error_at (loc, "invalid %<const_cast%> of an rvalue of type %qT "
8076 "to type %qT",
8077 src_type, dst_type);
8078 return error_mark_node;
8079 }
8080 dst_type = build_pointer_type (TREE_TYPE (dst_type));
8081 src_type = build_pointer_type (src_type);
8082 }
8083 else
8084 {
8085 reference_type = NULL_TREE;
8086 /* If the destination type is not a reference type, the
8087 lvalue-to-rvalue, array-to-pointer, and function-to-pointer
8088 conversions are performed. */
8089 src_type = type_decays_to (src_type);
8090 if (src_type == error_mark_node)
8091 return error_mark_node;
8092 }
8093
8094 if (TYPE_PTR_P (src_type) || TYPE_PTRDATAMEM_P (src_type))
8095 {
8096 if (comp_ptr_ttypes_const (dst_type, src_type, bounds_none))
8097 {
8098 if (valid_p)
8099 {
8100 *valid_p = true;
8101 /* This cast is actually a C-style cast. Issue a warning if
8102 the user is making a potentially unsafe cast. */
8103 check_for_casting_away_constness (loc, src_type, dst_type,
8104 CAST_EXPR, complain);
8105 /* ??? comp_ptr_ttypes_const ignores TYPE_ALIGN. */
8106 if ((STRICT_ALIGNMENT || warn_cast_align == 2)
8107 && (complain & tf_warning)
8108 && min_align_of_type (TREE_TYPE (dst_type))
8109 > min_align_of_type (TREE_TYPE (src_type)))
8110 warning_at (loc, OPT_Wcast_align, "cast from %qH to %qI "
8111 "increases required alignment of target type",
8112 src_type, dst_type);
8113 }
8114 if (reference_type)
8115 {
8116 expr = cp_build_addr_expr (expr, complain);
8117 if (expr == error_mark_node)
8118 return error_mark_node;
8119 expr = build_nop (reference_type, expr);
8120 return convert_from_reference (expr);
8121 }
8122 else
8123 {
8124 expr = decay_conversion (expr, complain);
8125 if (expr == error_mark_node)
8126 return error_mark_node;
8127
8128 /* build_c_cast puts on a NOP_EXPR to make the result not an
8129 lvalue. Strip such NOP_EXPRs if VALUE is being used in
8130 non-lvalue context. */
8131 if (TREE_CODE (expr) == NOP_EXPR
8132 && TREE_TYPE (expr) == TREE_TYPE (TREE_OPERAND (expr, 0)))
8133 expr = TREE_OPERAND (expr, 0);
8134 return build_nop (dst_type, expr);
8135 }
8136 }
8137 else if (valid_p
8138 && !at_least_as_qualified_p (TREE_TYPE (dst_type),
8139 TREE_TYPE (src_type)))
8140 check_for_casting_away_constness (loc, src_type, dst_type,
8141 CAST_EXPR, complain);
8142 }
8143
8144 if (complain & tf_error)
8145 error_at (loc, "invalid %<const_cast%> from type %qT to type %qT",
8146 src_type, dst_type);
8147 return error_mark_node;
8148 }
8149
8150 tree
8151 build_const_cast (location_t loc, tree type, tree expr,
8152 tsubst_flags_t complain)
8153 {
8154 tree r;
8155
8156 if (type == error_mark_node || error_operand_p (expr))
8157 return error_mark_node;
8158
8159 if (processing_template_decl)
8160 {
8161 tree t = build_min (CONST_CAST_EXPR, type, expr);
8162
8163 if (!TREE_SIDE_EFFECTS (t)
8164 && type_dependent_expression_p (expr))
8165 /* There might turn out to be side effects inside expr. */
8166 TREE_SIDE_EFFECTS (t) = 1;
8167 r = convert_from_reference (t);
8168 protected_set_expr_location (r, loc);
8169 return r;
8170 }
8171
8172 r = build_const_cast_1 (loc, type, expr, complain, /*valid_p=*/NULL);
8173 if (r != error_mark_node)
8174 {
8175 maybe_warn_about_useless_cast (loc, type, expr, complain);
8176 maybe_warn_about_cast_ignoring_quals (loc, type, complain);
8177 }
8178 protected_set_expr_location (r, loc);
8179 return r;
8180 }
8181
8182 /* Like cp_build_c_cast, but for the c-common bits. */
8183
8184 tree
8185 build_c_cast (location_t loc, tree type, tree expr)
8186 {
8187 return cp_build_c_cast (loc, type, expr, tf_warning_or_error);
8188 }
8189
8190 /* Like the "build_c_cast" used for c-common, but using cp_expr to
8191 preserve location information even for tree nodes that don't
8192 support it. */
8193
8194 cp_expr
8195 build_c_cast (location_t loc, tree type, cp_expr expr)
8196 {
8197 cp_expr result = cp_build_c_cast (loc, type, expr, tf_warning_or_error);
8198 result.set_location (loc);
8199 return result;
8200 }
8201
8202 /* Build an expression representing an explicit C-style cast to type
8203 TYPE of expression EXPR. */
8204
8205 tree
8206 cp_build_c_cast (location_t loc, tree type, tree expr,
8207 tsubst_flags_t complain)
8208 {
8209 tree value = expr;
8210 tree result;
8211 bool valid_p;
8212
8213 if (type == error_mark_node || error_operand_p (expr))
8214 return error_mark_node;
8215
8216 if (processing_template_decl)
8217 {
8218 tree t = build_min (CAST_EXPR, type,
8219 tree_cons (NULL_TREE, value, NULL_TREE));
8220 /* We don't know if it will or will not have side effects. */
8221 TREE_SIDE_EFFECTS (t) = 1;
8222 return convert_from_reference (t);
8223 }
8224
8225 /* Casts to a (pointer to a) specific ObjC class (or 'id' or
8226 'Class') should always be retained, because this information aids
8227 in method lookup. */
8228 if (objc_is_object_ptr (type)
8229 && objc_is_object_ptr (TREE_TYPE (expr)))
8230 return build_nop (type, expr);
8231
8232 /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
8233 Strip such NOP_EXPRs if VALUE is being used in non-lvalue context. */
8234 if (!TYPE_REF_P (type)
8235 && TREE_CODE (value) == NOP_EXPR
8236 && TREE_TYPE (value) == TREE_TYPE (TREE_OPERAND (value, 0)))
8237 value = TREE_OPERAND (value, 0);
8238
8239 if (TREE_CODE (type) == ARRAY_TYPE)
8240 {
8241 /* Allow casting from T1* to T2[] because Cfront allows it.
8242 NIHCL uses it. It is not valid ISO C++ however. */
8243 if (TYPE_PTR_P (TREE_TYPE (expr)))
8244 {
8245 if (complain & tf_error)
8246 permerror (loc, "ISO C++ forbids casting to an array type %qT",
8247 type);
8248 else
8249 return error_mark_node;
8250 type = build_pointer_type (TREE_TYPE (type));
8251 }
8252 else
8253 {
8254 if (complain & tf_error)
8255 error_at (loc, "ISO C++ forbids casting to an array type %qT",
8256 type);
8257 return error_mark_node;
8258 }
8259 }
8260
8261 if (FUNC_OR_METHOD_TYPE_P (type))
8262 {
8263 if (complain & tf_error)
8264 error_at (loc, "invalid cast to function type %qT", type);
8265 return error_mark_node;
8266 }
8267
8268 if (TYPE_PTR_P (type)
8269 && TREE_CODE (TREE_TYPE (value)) == INTEGER_TYPE
8270 /* Casting to an integer of smaller size is an error detected elsewhere. */
8271 && TYPE_PRECISION (type) > TYPE_PRECISION (TREE_TYPE (value))
8272 /* Don't warn about converting any constant. */
8273 && !TREE_CONSTANT (value))
8274 warning_at (loc, OPT_Wint_to_pointer_cast,
8275 "cast to pointer from integer of different size");
8276
8277 /* A C-style cast can be a const_cast. */
8278 result = build_const_cast_1 (loc, type, value, complain & tf_warning,
8279 &valid_p);
8280 if (valid_p)
8281 {
8282 if (result != error_mark_node)
8283 {
8284 maybe_warn_about_useless_cast (loc, type, value, complain);
8285 maybe_warn_about_cast_ignoring_quals (loc, type, complain);
8286 }
8287 return result;
8288 }
8289
8290 /* Or a static cast. */
8291 result = build_static_cast_1 (loc, type, value, /*c_cast_p=*/true,
8292 &valid_p, complain);
8293 /* Or a reinterpret_cast. */
8294 if (!valid_p)
8295 result = build_reinterpret_cast_1 (loc, type, value, /*c_cast_p=*/true,
8296 &valid_p, complain);
8297 /* The static_cast or reinterpret_cast may be followed by a
8298 const_cast. */
8299 if (valid_p
8300 /* A valid cast may result in errors if, for example, a
8301 conversion to an ambiguous base class is required. */
8302 && !error_operand_p (result))
8303 {
8304 tree result_type;
8305
8306 maybe_warn_about_useless_cast (loc, type, value, complain);
8307 maybe_warn_about_cast_ignoring_quals (loc, type, complain);
8308
8309 /* Non-class rvalues always have cv-unqualified type. */
8310 if (!CLASS_TYPE_P (type))
8311 type = TYPE_MAIN_VARIANT (type);
8312 result_type = TREE_TYPE (result);
8313 if (!CLASS_TYPE_P (result_type) && !TYPE_REF_P (type))
8314 result_type = TYPE_MAIN_VARIANT (result_type);
8315 /* If the type of RESULT does not match TYPE, perform a
8316 const_cast to make it match. If the static_cast or
8317 reinterpret_cast succeeded, we will differ by at most
8318 cv-qualification, so the follow-on const_cast is guaranteed
8319 to succeed. */
8320 if (!same_type_p (non_reference (type), non_reference (result_type)))
8321 {
8322 result = build_const_cast_1 (loc, type, result, false, &valid_p);
8323 gcc_assert (valid_p);
8324 }
8325 return result;
8326 }
8327
8328 return error_mark_node;
8329 }
8330 \f
8331 /* For use from the C common bits. */
8332 tree
8333 build_modify_expr (location_t location,
8334 tree lhs, tree /*lhs_origtype*/,
8335 enum tree_code modifycode,
8336 location_t /*rhs_location*/, tree rhs,
8337 tree /*rhs_origtype*/)
8338 {
8339 return cp_build_modify_expr (location, lhs, modifycode, rhs,
8340 tf_warning_or_error);
8341 }
8342
8343 /* Build an assignment expression of lvalue LHS from value RHS.
8344 MODIFYCODE is the code for a binary operator that we use
8345 to combine the old value of LHS with RHS to get the new value.
8346 Or else MODIFYCODE is NOP_EXPR meaning do a simple assignment.
8347
8348 C++: If MODIFYCODE is INIT_EXPR, then leave references unbashed. */
8349
8350 tree
8351 cp_build_modify_expr (location_t loc, tree lhs, enum tree_code modifycode,
8352 tree rhs, tsubst_flags_t complain)
8353 {
8354 lhs = mark_lvalue_use_nonread (lhs);
8355
8356 tree result = NULL_TREE;
8357 tree newrhs = rhs;
8358 tree lhstype = TREE_TYPE (lhs);
8359 tree olhs = lhs;
8360 tree olhstype = lhstype;
8361 bool plain_assign = (modifycode == NOP_EXPR);
8362 bool compound_side_effects_p = false;
8363 tree preeval = NULL_TREE;
8364
8365 /* Avoid duplicate error messages from operands that had errors. */
8366 if (error_operand_p (lhs) || error_operand_p (rhs))
8367 return error_mark_node;
8368
8369 while (TREE_CODE (lhs) == COMPOUND_EXPR)
8370 {
8371 if (TREE_SIDE_EFFECTS (TREE_OPERAND (lhs, 0)))
8372 compound_side_effects_p = true;
8373 lhs = TREE_OPERAND (lhs, 1);
8374 }
8375
8376 /* Handle control structure constructs used as "lvalues". Note that we
8377 leave COMPOUND_EXPR on the LHS because it is sequenced after the RHS. */
8378 switch (TREE_CODE (lhs))
8379 {
8380 /* Handle --foo = 5; as these are valid constructs in C++. */
8381 case PREDECREMENT_EXPR:
8382 case PREINCREMENT_EXPR:
8383 if (compound_side_effects_p)
8384 newrhs = rhs = stabilize_expr (rhs, &preeval);
8385 lhs = genericize_compound_lvalue (lhs);
8386 maybe_add_compound:
8387 /* If we had (bar, --foo) = 5; or (bar, (baz, --foo)) = 5;
8388 and looked through the COMPOUND_EXPRs, readd them now around
8389 the resulting lhs. */
8390 if (TREE_CODE (olhs) == COMPOUND_EXPR)
8391 {
8392 lhs = build2 (COMPOUND_EXPR, lhstype, TREE_OPERAND (olhs, 0), lhs);
8393 tree *ptr = &TREE_OPERAND (lhs, 1);
8394 for (olhs = TREE_OPERAND (olhs, 1);
8395 TREE_CODE (olhs) == COMPOUND_EXPR;
8396 olhs = TREE_OPERAND (olhs, 1))
8397 {
8398 *ptr = build2 (COMPOUND_EXPR, lhstype,
8399 TREE_OPERAND (olhs, 0), *ptr);
8400 ptr = &TREE_OPERAND (*ptr, 1);
8401 }
8402 }
8403 break;
8404
8405 case MODIFY_EXPR:
8406 if (compound_side_effects_p)
8407 newrhs = rhs = stabilize_expr (rhs, &preeval);
8408 lhs = genericize_compound_lvalue (lhs);
8409 goto maybe_add_compound;
8410
8411 case MIN_EXPR:
8412 case MAX_EXPR:
8413 /* MIN_EXPR and MAX_EXPR are currently only permitted as lvalues,
8414 when neither operand has side-effects. */
8415 if (!lvalue_or_else (lhs, lv_assign, complain))
8416 return error_mark_node;
8417
8418 gcc_assert (!TREE_SIDE_EFFECTS (TREE_OPERAND (lhs, 0))
8419 && !TREE_SIDE_EFFECTS (TREE_OPERAND (lhs, 1)));
8420
8421 lhs = build3 (COND_EXPR, TREE_TYPE (lhs),
8422 build2 (TREE_CODE (lhs) == MIN_EXPR ? LE_EXPR : GE_EXPR,
8423 boolean_type_node,
8424 TREE_OPERAND (lhs, 0),
8425 TREE_OPERAND (lhs, 1)),
8426 TREE_OPERAND (lhs, 0),
8427 TREE_OPERAND (lhs, 1));
8428 gcc_fallthrough ();
8429
8430 /* Handle (a ? b : c) used as an "lvalue". */
8431 case COND_EXPR:
8432 {
8433 /* Produce (a ? (b = rhs) : (c = rhs))
8434 except that the RHS goes through a save-expr
8435 so the code to compute it is only emitted once. */
8436 if (VOID_TYPE_P (TREE_TYPE (rhs)))
8437 {
8438 if (complain & tf_error)
8439 error_at (cp_expr_loc_or_loc (rhs, loc),
8440 "void value not ignored as it ought to be");
8441 return error_mark_node;
8442 }
8443
8444 rhs = stabilize_expr (rhs, &preeval);
8445
8446 /* Check this here to avoid odd errors when trying to convert
8447 a throw to the type of the COND_EXPR. */
8448 if (!lvalue_or_else (lhs, lv_assign, complain))
8449 return error_mark_node;
8450
8451 tree op1 = TREE_OPERAND (lhs, 1);
8452 if (TREE_CODE (op1) != THROW_EXPR)
8453 op1 = cp_build_modify_expr (loc, op1, modifycode, rhs, complain);
8454 /* When sanitizing undefined behavior, even when rhs doesn't need
8455 stabilization at this point, the sanitization might add extra
8456 SAVE_EXPRs in there and so make sure there is no tree sharing
8457 in the rhs, otherwise those SAVE_EXPRs will have initialization
8458 only in one of the two branches. */
8459 if (sanitize_flags_p (SANITIZE_UNDEFINED
8460 | SANITIZE_UNDEFINED_NONDEFAULT))
8461 rhs = unshare_expr (rhs);
8462 tree op2 = TREE_OPERAND (lhs, 2);
8463 if (TREE_CODE (op2) != THROW_EXPR)
8464 op2 = cp_build_modify_expr (loc, op2, modifycode, rhs, complain);
8465 tree cond = build_conditional_expr (input_location,
8466 TREE_OPERAND (lhs, 0), op1, op2,
8467 complain);
8468
8469 if (cond == error_mark_node)
8470 return cond;
8471 /* If we had (e, (a ? b : c)) = d; or (e, (f, (a ? b : c))) = d;
8472 and looked through the COMPOUND_EXPRs, readd them now around
8473 the resulting cond before adding the preevaluated rhs. */
8474 if (TREE_CODE (olhs) == COMPOUND_EXPR)
8475 {
8476 cond = build2 (COMPOUND_EXPR, TREE_TYPE (cond),
8477 TREE_OPERAND (olhs, 0), cond);
8478 tree *ptr = &TREE_OPERAND (cond, 1);
8479 for (olhs = TREE_OPERAND (olhs, 1);
8480 TREE_CODE (olhs) == COMPOUND_EXPR;
8481 olhs = TREE_OPERAND (olhs, 1))
8482 {
8483 *ptr = build2 (COMPOUND_EXPR, TREE_TYPE (cond),
8484 TREE_OPERAND (olhs, 0), *ptr);
8485 ptr = &TREE_OPERAND (*ptr, 1);
8486 }
8487 }
8488 /* Make sure the code to compute the rhs comes out
8489 before the split. */
8490 result = cond;
8491 goto ret;
8492 }
8493
8494 default:
8495 lhs = olhs;
8496 break;
8497 }
8498
8499 if (modifycode == INIT_EXPR)
8500 {
8501 if (BRACE_ENCLOSED_INITIALIZER_P (rhs))
8502 /* Do the default thing. */;
8503 else if (TREE_CODE (rhs) == CONSTRUCTOR)
8504 {
8505 /* Compound literal. */
8506 if (! same_type_p (TREE_TYPE (rhs), lhstype))
8507 /* Call convert to generate an error; see PR 11063. */
8508 rhs = convert (lhstype, rhs);
8509 result = build2 (INIT_EXPR, lhstype, lhs, rhs);
8510 TREE_SIDE_EFFECTS (result) = 1;
8511 goto ret;
8512 }
8513 else if (! MAYBE_CLASS_TYPE_P (lhstype))
8514 /* Do the default thing. */;
8515 else
8516 {
8517 releasing_vec rhs_vec = make_tree_vector_single (rhs);
8518 result = build_special_member_call (lhs, complete_ctor_identifier,
8519 &rhs_vec, lhstype, LOOKUP_NORMAL,
8520 complain);
8521 if (result == NULL_TREE)
8522 return error_mark_node;
8523 goto ret;
8524 }
8525 }
8526 else
8527 {
8528 lhs = require_complete_type_sfinae (lhs, complain);
8529 if (lhs == error_mark_node)
8530 return error_mark_node;
8531
8532 if (modifycode == NOP_EXPR)
8533 {
8534 if (c_dialect_objc ())
8535 {
8536 result = objc_maybe_build_modify_expr (lhs, rhs);
8537 if (result)
8538 goto ret;
8539 }
8540
8541 /* `operator=' is not an inheritable operator. */
8542 if (! MAYBE_CLASS_TYPE_P (lhstype))
8543 /* Do the default thing. */;
8544 else
8545 {
8546 result = build_new_op (input_location, MODIFY_EXPR,
8547 LOOKUP_NORMAL, lhs, rhs,
8548 make_node (NOP_EXPR), /*overload=*/NULL,
8549 complain);
8550 if (result == NULL_TREE)
8551 return error_mark_node;
8552 goto ret;
8553 }
8554 lhstype = olhstype;
8555 }
8556 else
8557 {
8558 tree init = NULL_TREE;
8559
8560 /* A binary op has been requested. Combine the old LHS
8561 value with the RHS producing the value we should actually
8562 store into the LHS. */
8563 gcc_assert (!((TYPE_REF_P (lhstype)
8564 && MAYBE_CLASS_TYPE_P (TREE_TYPE (lhstype)))
8565 || MAYBE_CLASS_TYPE_P (lhstype)));
8566
8567 /* An expression of the form E1 op= E2. [expr.ass] says:
8568 "Such expressions are deprecated if E1 has volatile-qualified
8569 type." We warn here rather than in cp_genericize_r because
8570 for compound assignments we are supposed to warn even if the
8571 assignment is a discarded-value expression. */
8572 if (TREE_THIS_VOLATILE (lhs) || CP_TYPE_VOLATILE_P (lhstype))
8573 warning_at (loc, OPT_Wvolatile,
8574 "compound assignment with %<volatile%>-qualified left "
8575 "operand is deprecated");
8576 /* Preevaluate the RHS to make sure its evaluation is complete
8577 before the lvalue-to-rvalue conversion of the LHS:
8578
8579 [expr.ass] With respect to an indeterminately-sequenced
8580 function call, the operation of a compound assignment is a
8581 single evaluation. [ Note: Therefore, a function call shall
8582 not intervene between the lvalue-to-rvalue conversion and the
8583 side effect associated with any single compound assignment
8584 operator. -- end note ] */
8585 lhs = cp_stabilize_reference (lhs);
8586 rhs = decay_conversion (rhs, complain);
8587 if (rhs == error_mark_node)
8588 return error_mark_node;
8589 rhs = stabilize_expr (rhs, &init);
8590 newrhs = cp_build_binary_op (loc, modifycode, lhs, rhs, complain);
8591 if (newrhs == error_mark_node)
8592 {
8593 if (complain & tf_error)
8594 inform (loc, " in evaluation of %<%Q(%#T, %#T)%>",
8595 modifycode, TREE_TYPE (lhs), TREE_TYPE (rhs));
8596 return error_mark_node;
8597 }
8598
8599 if (init)
8600 newrhs = build2 (COMPOUND_EXPR, TREE_TYPE (newrhs), init, newrhs);
8601
8602 /* Now it looks like a plain assignment. */
8603 modifycode = NOP_EXPR;
8604 if (c_dialect_objc ())
8605 {
8606 result = objc_maybe_build_modify_expr (lhs, newrhs);
8607 if (result)
8608 goto ret;
8609 }
8610 }
8611 gcc_assert (!TYPE_REF_P (lhstype));
8612 gcc_assert (!TYPE_REF_P (TREE_TYPE (newrhs)));
8613 }
8614
8615 /* The left-hand side must be an lvalue. */
8616 if (!lvalue_or_else (lhs, lv_assign, complain))
8617 return error_mark_node;
8618
8619 /* Warn about modifying something that is `const'. Don't warn if
8620 this is initialization. */
8621 if (modifycode != INIT_EXPR
8622 && (TREE_READONLY (lhs) || CP_TYPE_CONST_P (lhstype)
8623 /* Functions are not modifiable, even though they are
8624 lvalues. */
8625 || FUNC_OR_METHOD_TYPE_P (TREE_TYPE (lhs))
8626 /* If it's an aggregate and any field is const, then it is
8627 effectively const. */
8628 || (CLASS_TYPE_P (lhstype)
8629 && C_TYPE_FIELDS_READONLY (lhstype))))
8630 {
8631 if (complain & tf_error)
8632 cxx_readonly_error (loc, lhs, lv_assign);
8633 return error_mark_node;
8634 }
8635
8636 /* If storing into a structure or union member, it may have been given a
8637 lowered bitfield type. We need to convert to the declared type first,
8638 so retrieve it now. */
8639
8640 olhstype = unlowered_expr_type (lhs);
8641
8642 /* Convert new value to destination type. */
8643
8644 if (TREE_CODE (lhstype) == ARRAY_TYPE)
8645 {
8646 int from_array;
8647
8648 if (BRACE_ENCLOSED_INITIALIZER_P (newrhs))
8649 {
8650 if (modifycode != INIT_EXPR)
8651 {
8652 if (complain & tf_error)
8653 error_at (loc,
8654 "assigning to an array from an initializer list");
8655 return error_mark_node;
8656 }
8657 if (check_array_initializer (lhs, lhstype, newrhs))
8658 return error_mark_node;
8659 newrhs = digest_init (lhstype, newrhs, complain);
8660 if (newrhs == error_mark_node)
8661 return error_mark_node;
8662 }
8663
8664 /* C++11 8.5/17: "If the destination type is an array of characters,
8665 an array of char16_t, an array of char32_t, or an array of wchar_t,
8666 and the initializer is a string literal...". */
8667 else if ((TREE_CODE (tree_strip_any_location_wrapper (newrhs))
8668 == STRING_CST)
8669 && char_type_p (TREE_TYPE (TYPE_MAIN_VARIANT (lhstype)))
8670 && modifycode == INIT_EXPR)
8671 {
8672 newrhs = digest_init (lhstype, newrhs, complain);
8673 if (newrhs == error_mark_node)
8674 return error_mark_node;
8675 }
8676
8677 else if (!same_or_base_type_p (TYPE_MAIN_VARIANT (lhstype),
8678 TYPE_MAIN_VARIANT (TREE_TYPE (newrhs))))
8679 {
8680 if (complain & tf_error)
8681 error_at (loc, "incompatible types in assignment of %qT to %qT",
8682 TREE_TYPE (rhs), lhstype);
8683 return error_mark_node;
8684 }
8685
8686 /* Allow array assignment in compiler-generated code. */
8687 else if (!current_function_decl
8688 || !DECL_DEFAULTED_FN (current_function_decl))
8689 {
8690 /* This routine is used for both initialization and assignment.
8691 Make sure the diagnostic message differentiates the context. */
8692 if (complain & tf_error)
8693 {
8694 if (modifycode == INIT_EXPR)
8695 error_at (loc, "array used as initializer");
8696 else
8697 error_at (loc, "invalid array assignment");
8698 }
8699 return error_mark_node;
8700 }
8701
8702 from_array = TREE_CODE (TREE_TYPE (newrhs)) == ARRAY_TYPE
8703 ? 1 + (modifycode != INIT_EXPR): 0;
8704 result = build_vec_init (lhs, NULL_TREE, newrhs,
8705 /*explicit_value_init_p=*/false,
8706 from_array, complain);
8707 goto ret;
8708 }
8709
8710 if (modifycode == INIT_EXPR)
8711 /* Calls with INIT_EXPR are all direct-initialization, so don't set
8712 LOOKUP_ONLYCONVERTING. */
8713 newrhs = convert_for_initialization (lhs, olhstype, newrhs, LOOKUP_NORMAL,
8714 ICR_INIT, NULL_TREE, 0,
8715 complain);
8716 else
8717 newrhs = convert_for_assignment (olhstype, newrhs, ICR_ASSIGN,
8718 NULL_TREE, 0, complain, LOOKUP_IMPLICIT);
8719
8720 if (!same_type_p (lhstype, olhstype))
8721 newrhs = cp_convert_and_check (lhstype, newrhs, complain);
8722
8723 if (modifycode != INIT_EXPR)
8724 {
8725 if (TREE_CODE (newrhs) == CALL_EXPR
8726 && TYPE_NEEDS_CONSTRUCTING (lhstype))
8727 newrhs = build_cplus_new (lhstype, newrhs, complain);
8728
8729 /* Can't initialize directly from a TARGET_EXPR, since that would
8730 cause the lhs to be constructed twice, and possibly result in
8731 accidental self-initialization. So we force the TARGET_EXPR to be
8732 expanded without a target. */
8733 if (TREE_CODE (newrhs) == TARGET_EXPR)
8734 newrhs = build2 (COMPOUND_EXPR, TREE_TYPE (newrhs), newrhs,
8735 TREE_OPERAND (newrhs, 0));
8736 }
8737
8738 if (newrhs == error_mark_node)
8739 return error_mark_node;
8740
8741 if (c_dialect_objc () && flag_objc_gc)
8742 {
8743 result = objc_generate_write_barrier (lhs, modifycode, newrhs);
8744
8745 if (result)
8746 goto ret;
8747 }
8748
8749 result = build2_loc (loc, modifycode == NOP_EXPR ? MODIFY_EXPR : INIT_EXPR,
8750 lhstype, lhs, newrhs);
8751
8752 TREE_SIDE_EFFECTS (result) = 1;
8753 if (!plain_assign)
8754 TREE_NO_WARNING (result) = 1;
8755
8756 ret:
8757 if (preeval)
8758 result = build2 (COMPOUND_EXPR, TREE_TYPE (result), preeval, result);
8759 return result;
8760 }
8761
8762 cp_expr
8763 build_x_modify_expr (location_t loc, tree lhs, enum tree_code modifycode,
8764 tree rhs, tsubst_flags_t complain)
8765 {
8766 tree orig_lhs = lhs;
8767 tree orig_rhs = rhs;
8768 tree overload = NULL_TREE;
8769 tree op = build_nt (modifycode, NULL_TREE, NULL_TREE);
8770
8771 if (lhs == error_mark_node || rhs == error_mark_node)
8772 return cp_expr (error_mark_node, loc);
8773
8774 if (processing_template_decl)
8775 {
8776 if (modifycode == NOP_EXPR
8777 || type_dependent_expression_p (lhs)
8778 || type_dependent_expression_p (rhs))
8779 return build_min_nt_loc (loc, MODOP_EXPR, lhs,
8780 build_min_nt_loc (loc, modifycode, NULL_TREE,
8781 NULL_TREE), rhs);
8782
8783 lhs = build_non_dependent_expr (lhs);
8784 rhs = build_non_dependent_expr (rhs);
8785 }
8786
8787 if (modifycode != NOP_EXPR)
8788 {
8789 tree rval = build_new_op (loc, MODIFY_EXPR, LOOKUP_NORMAL,
8790 lhs, rhs, op, &overload, complain);
8791 if (rval)
8792 {
8793 if (rval == error_mark_node)
8794 return rval;
8795 TREE_NO_WARNING (rval) = 1;
8796 if (processing_template_decl)
8797 {
8798 if (overload != NULL_TREE)
8799 return (build_min_non_dep_op_overload
8800 (MODIFY_EXPR, rval, overload, orig_lhs, orig_rhs));
8801
8802 return (build_min_non_dep
8803 (MODOP_EXPR, rval, orig_lhs, op, orig_rhs));
8804 }
8805 return rval;
8806 }
8807 }
8808 return cp_build_modify_expr (loc, lhs, modifycode, rhs, complain);
8809 }
8810
8811 /* Helper function for get_delta_difference which assumes FROM is a base
8812 class of TO. Returns a delta for the conversion of pointer-to-member
8813 of FROM to pointer-to-member of TO. If the conversion is invalid and
8814 tf_error is not set in COMPLAIN returns error_mark_node, otherwise
8815 returns zero. If FROM is not a base class of TO, returns NULL_TREE.
8816 If C_CAST_P is true, this conversion is taking place as part of a
8817 C-style cast. */
8818
8819 static tree
8820 get_delta_difference_1 (tree from, tree to, bool c_cast_p,
8821 tsubst_flags_t complain)
8822 {
8823 tree binfo;
8824 base_kind kind;
8825
8826 binfo = lookup_base (to, from, c_cast_p ? ba_unique : ba_check,
8827 &kind, complain);
8828
8829 if (binfo == error_mark_node)
8830 {
8831 if (!(complain & tf_error))
8832 return error_mark_node;
8833
8834 inform (input_location, " in pointer to member function conversion");
8835 return size_zero_node;
8836 }
8837 else if (binfo)
8838 {
8839 if (kind != bk_via_virtual)
8840 return BINFO_OFFSET (binfo);
8841 else
8842 /* FROM is a virtual base class of TO. Issue an error or warning
8843 depending on whether or not this is a reinterpret cast. */
8844 {
8845 if (!(complain & tf_error))
8846 return error_mark_node;
8847
8848 error ("pointer to member conversion via virtual base %qT",
8849 BINFO_TYPE (binfo_from_vbase (binfo)));
8850
8851 return size_zero_node;
8852 }
8853 }
8854 else
8855 return NULL_TREE;
8856 }
8857
8858 /* Get difference in deltas for different pointer to member function
8859 types. If the conversion is invalid and tf_error is not set in
8860 COMPLAIN, returns error_mark_node, otherwise returns an integer
8861 constant of type PTRDIFF_TYPE_NODE and its value is zero if the
8862 conversion is invalid. If ALLOW_INVERSE_P is true, then allow reverse
8863 conversions as well. If C_CAST_P is true this conversion is taking
8864 place as part of a C-style cast.
8865
8866 Note that the naming of FROM and TO is kind of backwards; the return
8867 value is what we add to a TO in order to get a FROM. They are named
8868 this way because we call this function to find out how to convert from
8869 a pointer to member of FROM to a pointer to member of TO. */
8870
8871 static tree
8872 get_delta_difference (tree from, tree to,
8873 bool allow_inverse_p,
8874 bool c_cast_p, tsubst_flags_t complain)
8875 {
8876 tree result;
8877
8878 if (same_type_ignoring_top_level_qualifiers_p (from, to))
8879 /* Pointer to member of incomplete class is permitted*/
8880 result = size_zero_node;
8881 else
8882 result = get_delta_difference_1 (from, to, c_cast_p, complain);
8883
8884 if (result == error_mark_node)
8885 return error_mark_node;
8886
8887 if (!result)
8888 {
8889 if (!allow_inverse_p)
8890 {
8891 if (!(complain & tf_error))
8892 return error_mark_node;
8893
8894 error_not_base_type (from, to);
8895 inform (input_location, " in pointer to member conversion");
8896 result = size_zero_node;
8897 }
8898 else
8899 {
8900 result = get_delta_difference_1 (to, from, c_cast_p, complain);
8901
8902 if (result == error_mark_node)
8903 return error_mark_node;
8904
8905 if (result)
8906 result = size_diffop_loc (input_location,
8907 size_zero_node, result);
8908 else
8909 {
8910 if (!(complain & tf_error))
8911 return error_mark_node;
8912
8913 error_not_base_type (from, to);
8914 inform (input_location, " in pointer to member conversion");
8915 result = size_zero_node;
8916 }
8917 }
8918 }
8919
8920 return convert_to_integer (ptrdiff_type_node, result);
8921 }
8922
8923 /* Return a constructor for the pointer-to-member-function TYPE using
8924 the other components as specified. */
8925
8926 tree
8927 build_ptrmemfunc1 (tree type, tree delta, tree pfn)
8928 {
8929 tree u = NULL_TREE;
8930 tree delta_field;
8931 tree pfn_field;
8932 vec<constructor_elt, va_gc> *v;
8933
8934 /* Pull the FIELD_DECLs out of the type. */
8935 pfn_field = TYPE_FIELDS (type);
8936 delta_field = DECL_CHAIN (pfn_field);
8937
8938 /* Make sure DELTA has the type we want. */
8939 delta = convert_and_check (input_location, delta_type_node, delta);
8940
8941 /* Convert to the correct target type if necessary. */
8942 pfn = fold_convert (TREE_TYPE (pfn_field), pfn);
8943
8944 /* Finish creating the initializer. */
8945 vec_alloc (v, 2);
8946 CONSTRUCTOR_APPEND_ELT(v, pfn_field, pfn);
8947 CONSTRUCTOR_APPEND_ELT(v, delta_field, delta);
8948 u = build_constructor (type, v);
8949 TREE_CONSTANT (u) = TREE_CONSTANT (pfn) & TREE_CONSTANT (delta);
8950 TREE_STATIC (u) = (TREE_CONSTANT (u)
8951 && (initializer_constant_valid_p (pfn, TREE_TYPE (pfn))
8952 != NULL_TREE)
8953 && (initializer_constant_valid_p (delta, TREE_TYPE (delta))
8954 != NULL_TREE));
8955 return u;
8956 }
8957
8958 /* Build a constructor for a pointer to member function. It can be
8959 used to initialize global variables, local variable, or used
8960 as a value in expressions. TYPE is the POINTER to METHOD_TYPE we
8961 want to be.
8962
8963 If FORCE is nonzero, then force this conversion, even if
8964 we would rather not do it. Usually set when using an explicit
8965 cast. A C-style cast is being processed iff C_CAST_P is true.
8966
8967 Return error_mark_node, if something goes wrong. */
8968
8969 tree
8970 build_ptrmemfunc (tree type, tree pfn, int force, bool c_cast_p,
8971 tsubst_flags_t complain)
8972 {
8973 tree fn;
8974 tree pfn_type;
8975 tree to_type;
8976
8977 if (error_operand_p (pfn))
8978 return error_mark_node;
8979
8980 pfn_type = TREE_TYPE (pfn);
8981 to_type = build_ptrmemfunc_type (type);
8982
8983 /* Handle multiple conversions of pointer to member functions. */
8984 if (TYPE_PTRMEMFUNC_P (pfn_type))
8985 {
8986 tree delta = NULL_TREE;
8987 tree npfn = NULL_TREE;
8988 tree n;
8989
8990 if (!force
8991 && !can_convert_arg (to_type, TREE_TYPE (pfn), pfn,
8992 LOOKUP_NORMAL, complain))
8993 {
8994 if (complain & tf_error)
8995 error ("invalid conversion to type %qT from type %qT",
8996 to_type, pfn_type);
8997 else
8998 return error_mark_node;
8999 }
9000
9001 n = get_delta_difference (TYPE_PTRMEMFUNC_OBJECT_TYPE (pfn_type),
9002 TYPE_PTRMEMFUNC_OBJECT_TYPE (to_type),
9003 force,
9004 c_cast_p, complain);
9005 if (n == error_mark_node)
9006 return error_mark_node;
9007
9008 /* We don't have to do any conversion to convert a
9009 pointer-to-member to its own type. But, we don't want to
9010 just return a PTRMEM_CST if there's an explicit cast; that
9011 cast should make the expression an invalid template argument. */
9012 if (TREE_CODE (pfn) != PTRMEM_CST)
9013 {
9014 if (same_type_p (to_type, pfn_type))
9015 return pfn;
9016 else if (integer_zerop (n) && TREE_CODE (pfn) != CONSTRUCTOR)
9017 return build_reinterpret_cast (input_location, to_type, pfn,
9018 complain);
9019 }
9020
9021 if (TREE_SIDE_EFFECTS (pfn))
9022 pfn = save_expr (pfn);
9023
9024 /* Obtain the function pointer and the current DELTA. */
9025 if (TREE_CODE (pfn) == PTRMEM_CST)
9026 expand_ptrmemfunc_cst (pfn, &delta, &npfn);
9027 else
9028 {
9029 npfn = build_ptrmemfunc_access_expr (pfn, pfn_identifier);
9030 delta = build_ptrmemfunc_access_expr (pfn, delta_identifier);
9031 }
9032
9033 /* Just adjust the DELTA field. */
9034 gcc_assert (same_type_ignoring_top_level_qualifiers_p
9035 (TREE_TYPE (delta), ptrdiff_type_node));
9036 if (!integer_zerop (n))
9037 {
9038 if (TARGET_PTRMEMFUNC_VBIT_LOCATION == ptrmemfunc_vbit_in_delta)
9039 n = cp_build_binary_op (input_location,
9040 LSHIFT_EXPR, n, integer_one_node,
9041 complain);
9042 delta = cp_build_binary_op (input_location,
9043 PLUS_EXPR, delta, n, complain);
9044 }
9045 return build_ptrmemfunc1 (to_type, delta, npfn);
9046 }
9047
9048 /* Handle null pointer to member function conversions. */
9049 if (null_ptr_cst_p (pfn))
9050 {
9051 pfn = cp_build_c_cast (input_location, type, pfn, complain);
9052 return build_ptrmemfunc1 (to_type,
9053 integer_zero_node,
9054 pfn);
9055 }
9056
9057 if (type_unknown_p (pfn))
9058 return instantiate_type (type, pfn, complain);
9059
9060 fn = TREE_OPERAND (pfn, 0);
9061 gcc_assert (TREE_CODE (fn) == FUNCTION_DECL
9062 /* In a template, we will have preserved the
9063 OFFSET_REF. */
9064 || (processing_template_decl && TREE_CODE (fn) == OFFSET_REF));
9065 return make_ptrmem_cst (to_type, fn);
9066 }
9067
9068 /* Return the DELTA, IDX, PFN, and DELTA2 values for the PTRMEM_CST
9069 given by CST.
9070
9071 ??? There is no consistency as to the types returned for the above
9072 values. Some code acts as if it were a sizetype and some as if it were
9073 integer_type_node. */
9074
9075 void
9076 expand_ptrmemfunc_cst (tree cst, tree *delta, tree *pfn)
9077 {
9078 tree type = TREE_TYPE (cst);
9079 tree fn = PTRMEM_CST_MEMBER (cst);
9080 tree ptr_class, fn_class;
9081
9082 gcc_assert (TREE_CODE (fn) == FUNCTION_DECL);
9083
9084 /* The class that the function belongs to. */
9085 fn_class = DECL_CONTEXT (fn);
9086
9087 /* The class that we're creating a pointer to member of. */
9088 ptr_class = TYPE_PTRMEMFUNC_OBJECT_TYPE (type);
9089
9090 /* First, calculate the adjustment to the function's class. */
9091 *delta = get_delta_difference (fn_class, ptr_class, /*force=*/0,
9092 /*c_cast_p=*/0, tf_warning_or_error);
9093
9094 if (!DECL_VIRTUAL_P (fn))
9095 *pfn = convert (TYPE_PTRMEMFUNC_FN_TYPE (type),
9096 build_addr_func (fn, tf_warning_or_error));
9097 else
9098 {
9099 /* If we're dealing with a virtual function, we have to adjust 'this'
9100 again, to point to the base which provides the vtable entry for
9101 fn; the call will do the opposite adjustment. */
9102 tree orig_class = DECL_CONTEXT (fn);
9103 tree binfo = binfo_or_else (orig_class, fn_class);
9104 *delta = fold_build2 (PLUS_EXPR, TREE_TYPE (*delta),
9105 *delta, BINFO_OFFSET (binfo));
9106
9107 /* We set PFN to the vtable offset at which the function can be
9108 found, plus one (unless ptrmemfunc_vbit_in_delta, in which
9109 case delta is shifted left, and then incremented). */
9110 *pfn = DECL_VINDEX (fn);
9111 *pfn = fold_build2 (MULT_EXPR, integer_type_node, *pfn,
9112 TYPE_SIZE_UNIT (vtable_entry_type));
9113
9114 switch (TARGET_PTRMEMFUNC_VBIT_LOCATION)
9115 {
9116 case ptrmemfunc_vbit_in_pfn:
9117 *pfn = fold_build2 (PLUS_EXPR, integer_type_node, *pfn,
9118 integer_one_node);
9119 break;
9120
9121 case ptrmemfunc_vbit_in_delta:
9122 *delta = fold_build2 (LSHIFT_EXPR, TREE_TYPE (*delta),
9123 *delta, integer_one_node);
9124 *delta = fold_build2 (PLUS_EXPR, TREE_TYPE (*delta),
9125 *delta, integer_one_node);
9126 break;
9127
9128 default:
9129 gcc_unreachable ();
9130 }
9131
9132 *pfn = fold_convert (TYPE_PTRMEMFUNC_FN_TYPE (type), *pfn);
9133 }
9134 }
9135
9136 /* Return an expression for PFN from the pointer-to-member function
9137 given by T. */
9138
9139 static tree
9140 pfn_from_ptrmemfunc (tree t)
9141 {
9142 if (TREE_CODE (t) == PTRMEM_CST)
9143 {
9144 tree delta;
9145 tree pfn;
9146
9147 expand_ptrmemfunc_cst (t, &delta, &pfn);
9148 if (pfn)
9149 return pfn;
9150 }
9151
9152 return build_ptrmemfunc_access_expr (t, pfn_identifier);
9153 }
9154
9155 /* Return an expression for DELTA from the pointer-to-member function
9156 given by T. */
9157
9158 static tree
9159 delta_from_ptrmemfunc (tree t)
9160 {
9161 if (TREE_CODE (t) == PTRMEM_CST)
9162 {
9163 tree delta;
9164 tree pfn;
9165
9166 expand_ptrmemfunc_cst (t, &delta, &pfn);
9167 if (delta)
9168 return delta;
9169 }
9170
9171 return build_ptrmemfunc_access_expr (t, delta_identifier);
9172 }
9173
9174 /* Convert value RHS to type TYPE as preparation for an assignment to
9175 an lvalue of type TYPE. ERRTYPE indicates what kind of error the
9176 implicit conversion is. If FNDECL is non-NULL, we are doing the
9177 conversion in order to pass the PARMNUMth argument of FNDECL.
9178 If FNDECL is NULL, we are doing the conversion in function pointer
9179 argument passing, conversion in initialization, etc. */
9180
9181 static tree
9182 convert_for_assignment (tree type, tree rhs,
9183 impl_conv_rhs errtype, tree fndecl, int parmnum,
9184 tsubst_flags_t complain, int flags)
9185 {
9186 tree rhstype;
9187 enum tree_code coder;
9188
9189 location_t rhs_loc = EXPR_LOC_OR_LOC (rhs, input_location);
9190 bool has_loc = EXPR_LOCATION (rhs) != UNKNOWN_LOCATION;
9191 /* Strip NON_LVALUE_EXPRs since we aren't using as an lvalue,
9192 but preserve location wrappers. */
9193 if (TREE_CODE (rhs) == NON_LVALUE_EXPR
9194 && !location_wrapper_p (rhs))
9195 rhs = TREE_OPERAND (rhs, 0);
9196
9197 /* Handle [dcl.init.list] direct-list-initialization from
9198 single element of enumeration with a fixed underlying type. */
9199 if (is_direct_enum_init (type, rhs))
9200 {
9201 tree elt = CONSTRUCTOR_ELT (rhs, 0)->value;
9202 if (check_narrowing (ENUM_UNDERLYING_TYPE (type), elt, complain))
9203 {
9204 warning_sentinel w (warn_useless_cast);
9205 warning_sentinel w2 (warn_ignored_qualifiers);
9206 rhs = cp_build_c_cast (rhs_loc, type, elt, complain);
9207 }
9208 else
9209 rhs = error_mark_node;
9210 }
9211
9212 rhstype = TREE_TYPE (rhs);
9213 coder = TREE_CODE (rhstype);
9214
9215 if (VECTOR_TYPE_P (type) && coder == VECTOR_TYPE
9216 && vector_types_convertible_p (type, rhstype, true))
9217 {
9218 rhs = mark_rvalue_use (rhs);
9219 return convert (type, rhs);
9220 }
9221
9222 if (rhs == error_mark_node || rhstype == error_mark_node)
9223 return error_mark_node;
9224 if (TREE_CODE (rhs) == TREE_LIST && TREE_VALUE (rhs) == error_mark_node)
9225 return error_mark_node;
9226
9227 /* The RHS of an assignment cannot have void type. */
9228 if (coder == VOID_TYPE)
9229 {
9230 if (complain & tf_error)
9231 error_at (rhs_loc, "void value not ignored as it ought to be");
9232 return error_mark_node;
9233 }
9234
9235 if (c_dialect_objc ())
9236 {
9237 int parmno;
9238 tree selector;
9239 tree rname = fndecl;
9240
9241 switch (errtype)
9242 {
9243 case ICR_ASSIGN:
9244 parmno = -1;
9245 break;
9246 case ICR_INIT:
9247 parmno = -2;
9248 break;
9249 default:
9250 selector = objc_message_selector ();
9251 parmno = parmnum;
9252 if (selector && parmno > 1)
9253 {
9254 rname = selector;
9255 parmno -= 1;
9256 }
9257 }
9258
9259 if (objc_compare_types (type, rhstype, parmno, rname))
9260 {
9261 rhs = mark_rvalue_use (rhs);
9262 return convert (type, rhs);
9263 }
9264 }
9265
9266 /* [expr.ass]
9267
9268 The expression is implicitly converted (clause _conv_) to the
9269 cv-unqualified type of the left operand.
9270
9271 We allow bad conversions here because by the time we get to this point
9272 we are committed to doing the conversion. If we end up doing a bad
9273 conversion, convert_like will complain. */
9274 if (!can_convert_arg_bad (type, rhstype, rhs, flags, complain))
9275 {
9276 /* When -Wno-pmf-conversions is use, we just silently allow
9277 conversions from pointers-to-members to plain pointers. If
9278 the conversion doesn't work, cp_convert will complain. */
9279 if (!warn_pmf2ptr
9280 && TYPE_PTR_P (type)
9281 && TYPE_PTRMEMFUNC_P (rhstype))
9282 rhs = cp_convert (strip_top_quals (type), rhs, complain);
9283 else
9284 {
9285 if (complain & tf_error)
9286 {
9287 /* If the right-hand side has unknown type, then it is an
9288 overloaded function. Call instantiate_type to get error
9289 messages. */
9290 if (rhstype == unknown_type_node)
9291 {
9292 tree r = instantiate_type (type, rhs, tf_warning_or_error);
9293 /* -fpermissive might allow this; recurse. */
9294 if (!seen_error ())
9295 return convert_for_assignment (type, r, errtype, fndecl,
9296 parmnum, complain, flags);
9297 }
9298 else if (fndecl)
9299 complain_about_bad_argument (rhs_loc,
9300 rhstype, type,
9301 fndecl, parmnum);
9302 else
9303 {
9304 range_label_for_type_mismatch label (rhstype, type);
9305 gcc_rich_location richloc (rhs_loc, has_loc ? &label : NULL);
9306 switch (errtype)
9307 {
9308 case ICR_DEFAULT_ARGUMENT:
9309 error_at (&richloc,
9310 "cannot convert %qH to %qI in default argument",
9311 rhstype, type);
9312 break;
9313 case ICR_ARGPASS:
9314 error_at (&richloc,
9315 "cannot convert %qH to %qI in argument passing",
9316 rhstype, type);
9317 break;
9318 case ICR_CONVERTING:
9319 error_at (&richloc, "cannot convert %qH to %qI",
9320 rhstype, type);
9321 break;
9322 case ICR_INIT:
9323 error_at (&richloc,
9324 "cannot convert %qH to %qI in initialization",
9325 rhstype, type);
9326 break;
9327 case ICR_RETURN:
9328 error_at (&richloc, "cannot convert %qH to %qI in return",
9329 rhstype, type);
9330 break;
9331 case ICR_ASSIGN:
9332 error_at (&richloc,
9333 "cannot convert %qH to %qI in assignment",
9334 rhstype, type);
9335 break;
9336 default:
9337 gcc_unreachable();
9338 }
9339 }
9340 if (TYPE_PTR_P (rhstype)
9341 && TYPE_PTR_P (type)
9342 && CLASS_TYPE_P (TREE_TYPE (rhstype))
9343 && CLASS_TYPE_P (TREE_TYPE (type))
9344 && !COMPLETE_TYPE_P (TREE_TYPE (rhstype)))
9345 inform (DECL_SOURCE_LOCATION (TYPE_MAIN_DECL
9346 (TREE_TYPE (rhstype))),
9347 "class type %qT is incomplete", TREE_TYPE (rhstype));
9348 }
9349 return error_mark_node;
9350 }
9351 }
9352 if (warn_suggest_attribute_format)
9353 {
9354 const enum tree_code codel = TREE_CODE (type);
9355 if ((codel == POINTER_TYPE || codel == REFERENCE_TYPE)
9356 && coder == codel
9357 && check_missing_format_attribute (type, rhstype)
9358 && (complain & tf_warning))
9359 switch (errtype)
9360 {
9361 case ICR_ARGPASS:
9362 case ICR_DEFAULT_ARGUMENT:
9363 if (fndecl)
9364 warning (OPT_Wsuggest_attribute_format,
9365 "parameter %qP of %qD might be a candidate "
9366 "for a format attribute", parmnum, fndecl);
9367 else
9368 warning (OPT_Wsuggest_attribute_format,
9369 "parameter might be a candidate "
9370 "for a format attribute");
9371 break;
9372 case ICR_CONVERTING:
9373 warning (OPT_Wsuggest_attribute_format,
9374 "target of conversion might be a candidate "
9375 "for a format attribute");
9376 break;
9377 case ICR_INIT:
9378 warning (OPT_Wsuggest_attribute_format,
9379 "target of initialization might be a candidate "
9380 "for a format attribute");
9381 break;
9382 case ICR_RETURN:
9383 warning (OPT_Wsuggest_attribute_format,
9384 "return type might be a candidate "
9385 "for a format attribute");
9386 break;
9387 case ICR_ASSIGN:
9388 warning (OPT_Wsuggest_attribute_format,
9389 "left-hand side of assignment might be a candidate "
9390 "for a format attribute");
9391 break;
9392 default:
9393 gcc_unreachable();
9394 }
9395 }
9396
9397 /* If -Wparentheses, warn about a = b = c when a has type bool and b
9398 does not. */
9399 if (warn_parentheses
9400 && TREE_CODE (type) == BOOLEAN_TYPE
9401 && TREE_CODE (rhs) == MODIFY_EXPR
9402 && !TREE_NO_WARNING (rhs)
9403 && TREE_CODE (TREE_TYPE (rhs)) != BOOLEAN_TYPE
9404 && (complain & tf_warning)
9405 && warning_at (rhs_loc, OPT_Wparentheses,
9406 "suggest parentheses around assignment used as "
9407 "truth value"))
9408 TREE_NO_WARNING (rhs) = 1;
9409
9410 if (complain & tf_warning)
9411 warn_for_address_or_pointer_of_packed_member (type, rhs);
9412
9413 return perform_implicit_conversion_flags (strip_top_quals (type), rhs,
9414 complain, flags);
9415 }
9416
9417 /* Convert RHS to be of type TYPE.
9418 If EXP is nonzero, it is the target of the initialization.
9419 ERRTYPE indicates what kind of error the implicit conversion is.
9420
9421 Two major differences between the behavior of
9422 `convert_for_assignment' and `convert_for_initialization'
9423 are that references are bashed in the former, while
9424 copied in the latter, and aggregates are assigned in
9425 the former (operator=) while initialized in the
9426 latter (X(X&)).
9427
9428 If using constructor make sure no conversion operator exists, if one does
9429 exist, an ambiguity exists. */
9430
9431 tree
9432 convert_for_initialization (tree exp, tree type, tree rhs, int flags,
9433 impl_conv_rhs errtype, tree fndecl, int parmnum,
9434 tsubst_flags_t complain)
9435 {
9436 enum tree_code codel = TREE_CODE (type);
9437 tree rhstype;
9438 enum tree_code coder;
9439
9440 /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
9441 Strip such NOP_EXPRs, since RHS is used in non-lvalue context. */
9442 if (TREE_CODE (rhs) == NOP_EXPR
9443 && TREE_TYPE (rhs) == TREE_TYPE (TREE_OPERAND (rhs, 0))
9444 && codel != REFERENCE_TYPE)
9445 rhs = TREE_OPERAND (rhs, 0);
9446
9447 if (type == error_mark_node
9448 || rhs == error_mark_node
9449 || (TREE_CODE (rhs) == TREE_LIST && TREE_VALUE (rhs) == error_mark_node))
9450 return error_mark_node;
9451
9452 if (MAYBE_CLASS_TYPE_P (non_reference (type)))
9453 ;
9454 else if ((TREE_CODE (TREE_TYPE (rhs)) == ARRAY_TYPE
9455 && TREE_CODE (type) != ARRAY_TYPE
9456 && (!TYPE_REF_P (type)
9457 || TREE_CODE (TREE_TYPE (type)) != ARRAY_TYPE))
9458 || (TREE_CODE (TREE_TYPE (rhs)) == FUNCTION_TYPE
9459 && !TYPE_REFFN_P (type))
9460 || TREE_CODE (TREE_TYPE (rhs)) == METHOD_TYPE)
9461 rhs = decay_conversion (rhs, complain);
9462
9463 rhstype = TREE_TYPE (rhs);
9464 coder = TREE_CODE (rhstype);
9465
9466 if (coder == ERROR_MARK)
9467 return error_mark_node;
9468
9469 /* We accept references to incomplete types, so we can
9470 return here before checking if RHS is of complete type. */
9471
9472 if (codel == REFERENCE_TYPE)
9473 {
9474 auto_diagnostic_group d;
9475 /* This should eventually happen in convert_arguments. */
9476 int savew = 0, savee = 0;
9477
9478 if (fndecl)
9479 savew = warningcount + werrorcount, savee = errorcount;
9480 rhs = initialize_reference (type, rhs, flags, complain);
9481
9482 if (fndecl
9483 && (warningcount + werrorcount > savew || errorcount > savee))
9484 inform (get_fndecl_argument_location (fndecl, parmnum),
9485 "in passing argument %P of %qD", parmnum, fndecl);
9486 return rhs;
9487 }
9488
9489 if (exp != 0)
9490 exp = require_complete_type_sfinae (exp, complain);
9491 if (exp == error_mark_node)
9492 return error_mark_node;
9493
9494 type = complete_type (type);
9495
9496 if (DIRECT_INIT_EXPR_P (type, rhs))
9497 /* Don't try to do copy-initialization if we already have
9498 direct-initialization. */
9499 return rhs;
9500
9501 if (MAYBE_CLASS_TYPE_P (type))
9502 return perform_implicit_conversion_flags (type, rhs, complain, flags);
9503
9504 return convert_for_assignment (type, rhs, errtype, fndecl, parmnum,
9505 complain, flags);
9506 }
9507 \f
9508 /* If RETVAL is the address of, or a reference to, a local variable or
9509 temporary give an appropriate warning and return true. */
9510
9511 static bool
9512 maybe_warn_about_returning_address_of_local (tree retval, location_t loc)
9513 {
9514 tree valtype = TREE_TYPE (DECL_RESULT (current_function_decl));
9515 tree whats_returned = fold_for_warn (retval);
9516 if (!loc)
9517 loc = cp_expr_loc_or_input_loc (retval);
9518
9519 for (;;)
9520 {
9521 if (TREE_CODE (whats_returned) == COMPOUND_EXPR)
9522 whats_returned = TREE_OPERAND (whats_returned, 1);
9523 else if (CONVERT_EXPR_P (whats_returned)
9524 || TREE_CODE (whats_returned) == NON_LVALUE_EXPR)
9525 whats_returned = TREE_OPERAND (whats_returned, 0);
9526 else
9527 break;
9528 }
9529
9530 if (TREE_CODE (whats_returned) == TARGET_EXPR
9531 && is_std_init_list (TREE_TYPE (whats_returned)))
9532 {
9533 tree init = TARGET_EXPR_INITIAL (whats_returned);
9534 if (TREE_CODE (init) == CONSTRUCTOR)
9535 /* Pull out the array address. */
9536 whats_returned = CONSTRUCTOR_ELT (init, 0)->value;
9537 else if (TREE_CODE (init) == INDIRECT_REF)
9538 /* The source of a trivial copy looks like *(T*)&var. */
9539 whats_returned = TREE_OPERAND (init, 0);
9540 else
9541 return false;
9542 STRIP_NOPS (whats_returned);
9543 }
9544
9545 /* As a special case, we handle a call to std::move or std::forward. */
9546 if (TREE_CODE (whats_returned) == CALL_EXPR
9547 && (is_std_move_p (whats_returned)
9548 || is_std_forward_p (whats_returned)))
9549 {
9550 tree arg = CALL_EXPR_ARG (whats_returned, 0);
9551 return maybe_warn_about_returning_address_of_local (arg, loc);
9552 }
9553
9554 if (TREE_CODE (whats_returned) != ADDR_EXPR)
9555 return false;
9556 whats_returned = TREE_OPERAND (whats_returned, 0);
9557
9558 while (TREE_CODE (whats_returned) == COMPONENT_REF
9559 || TREE_CODE (whats_returned) == ARRAY_REF)
9560 whats_returned = TREE_OPERAND (whats_returned, 0);
9561
9562 if (TREE_CODE (whats_returned) == AGGR_INIT_EXPR
9563 || TREE_CODE (whats_returned) == TARGET_EXPR)
9564 {
9565 if (TYPE_REF_P (valtype))
9566 warning_at (loc, OPT_Wreturn_local_addr,
9567 "returning reference to temporary");
9568 else if (is_std_init_list (valtype))
9569 warning_at (loc, OPT_Winit_list_lifetime,
9570 "returning temporary %<initializer_list%> does not extend "
9571 "the lifetime of the underlying array");
9572 return true;
9573 }
9574
9575 STRIP_ANY_LOCATION_WRAPPER (whats_returned);
9576
9577 if (DECL_P (whats_returned)
9578 && DECL_NAME (whats_returned)
9579 && DECL_FUNCTION_SCOPE_P (whats_returned)
9580 && !is_capture_proxy (whats_returned)
9581 && !(TREE_STATIC (whats_returned)
9582 || TREE_PUBLIC (whats_returned)))
9583 {
9584 if (VAR_P (whats_returned)
9585 && DECL_DECOMPOSITION_P (whats_returned)
9586 && DECL_DECOMP_BASE (whats_returned)
9587 && DECL_HAS_VALUE_EXPR_P (whats_returned))
9588 {
9589 /* When returning address of a structured binding, if the structured
9590 binding is not a reference, continue normally, if it is a
9591 reference, recurse on the initializer of the structured
9592 binding. */
9593 tree base = DECL_DECOMP_BASE (whats_returned);
9594 if (TYPE_REF_P (TREE_TYPE (base)))
9595 {
9596 if (tree init = DECL_INITIAL (base))
9597 return maybe_warn_about_returning_address_of_local (init, loc);
9598 else
9599 return false;
9600 }
9601 }
9602 bool w = false;
9603 auto_diagnostic_group d;
9604 if (TYPE_REF_P (valtype))
9605 w = warning_at (loc, OPT_Wreturn_local_addr,
9606 "reference to local variable %qD returned",
9607 whats_returned);
9608 else if (is_std_init_list (valtype))
9609 w = warning_at (loc, OPT_Winit_list_lifetime,
9610 "returning local %<initializer_list%> variable %qD "
9611 "does not extend the lifetime of the underlying array",
9612 whats_returned);
9613 else if (POINTER_TYPE_P (valtype)
9614 && TREE_CODE (whats_returned) == LABEL_DECL)
9615 w = warning_at (loc, OPT_Wreturn_local_addr,
9616 "address of label %qD returned",
9617 whats_returned);
9618 else if (POINTER_TYPE_P (valtype))
9619 w = warning_at (loc, OPT_Wreturn_local_addr,
9620 "address of local variable %qD returned",
9621 whats_returned);
9622 if (w)
9623 inform (DECL_SOURCE_LOCATION (whats_returned),
9624 "declared here");
9625 return true;
9626 }
9627
9628 return false;
9629 }
9630
9631 /* Returns true if DECL is in the std namespace. */
9632
9633 bool
9634 decl_in_std_namespace_p (tree decl)
9635 {
9636 while (decl)
9637 {
9638 decl = decl_namespace_context (decl);
9639 if (DECL_NAMESPACE_STD_P (decl))
9640 return true;
9641 /* Allow inline namespaces inside of std namespace, e.g. with
9642 --enable-symvers=gnu-versioned-namespace std::forward would be
9643 actually std::_8::forward. */
9644 if (!DECL_NAMESPACE_INLINE_P (decl))
9645 return false;
9646 decl = CP_DECL_CONTEXT (decl);
9647 }
9648 return false;
9649 }
9650
9651 /* Returns true if FN, a CALL_EXPR, is a call to std::forward. */
9652
9653 static bool
9654 is_std_forward_p (tree fn)
9655 {
9656 /* std::forward only takes one argument. */
9657 if (call_expr_nargs (fn) != 1)
9658 return false;
9659
9660 tree fndecl = cp_get_callee_fndecl_nofold (fn);
9661 if (!decl_in_std_namespace_p (fndecl))
9662 return false;
9663
9664 tree name = DECL_NAME (fndecl);
9665 return name && id_equal (name, "forward");
9666 }
9667
9668 /* Returns true if FN, a CALL_EXPR, is a call to std::move. */
9669
9670 static bool
9671 is_std_move_p (tree fn)
9672 {
9673 /* std::move only takes one argument. */
9674 if (call_expr_nargs (fn) != 1)
9675 return false;
9676
9677 tree fndecl = cp_get_callee_fndecl_nofold (fn);
9678 if (!decl_in_std_namespace_p (fndecl))
9679 return false;
9680
9681 tree name = DECL_NAME (fndecl);
9682 return name && id_equal (name, "move");
9683 }
9684
9685 /* Returns true if RETVAL is a good candidate for the NRVO as per
9686 [class.copy.elision]. FUNCTYPE is the type the function is declared
9687 to return. */
9688
9689 static bool
9690 can_do_nrvo_p (tree retval, tree functype)
9691 {
9692 if (functype == error_mark_node)
9693 return false;
9694 if (retval)
9695 STRIP_ANY_LOCATION_WRAPPER (retval);
9696 tree result = DECL_RESULT (current_function_decl);
9697 return (retval != NULL_TREE
9698 && !processing_template_decl
9699 /* Must be a local, automatic variable. */
9700 && VAR_P (retval)
9701 && DECL_CONTEXT (retval) == current_function_decl
9702 && !TREE_STATIC (retval)
9703 /* And not a lambda or anonymous union proxy. */
9704 && !DECL_HAS_VALUE_EXPR_P (retval)
9705 && (DECL_ALIGN (retval) <= DECL_ALIGN (result))
9706 /* The cv-unqualified type of the returned value must be the
9707 same as the cv-unqualified return type of the
9708 function. */
9709 && same_type_p ((TYPE_MAIN_VARIANT (TREE_TYPE (retval))),
9710 (TYPE_MAIN_VARIANT (functype)))
9711 /* And the returned value must be non-volatile. */
9712 && !TYPE_VOLATILE (TREE_TYPE (retval)));
9713 }
9714
9715 /* Returns true if we should treat RETVAL, an expression being returned,
9716 as if it were designated by an rvalue. See [class.copy.elision].
9717 PARM_P is true if a function parameter is OK in this context. */
9718
9719 bool
9720 treat_lvalue_as_rvalue_p (tree retval, bool parm_ok)
9721 {
9722 STRIP_ANY_LOCATION_WRAPPER (retval);
9723 return ((cxx_dialect != cxx98)
9724 && ((VAR_P (retval) && !DECL_HAS_VALUE_EXPR_P (retval))
9725 || (parm_ok && TREE_CODE (retval) == PARM_DECL))
9726 && DECL_CONTEXT (retval) == current_function_decl
9727 && !TREE_STATIC (retval));
9728 }
9729
9730 /* Warn about wrong usage of std::move in a return statement. RETVAL
9731 is the expression we are returning; FUNCTYPE is the type the function
9732 is declared to return. */
9733
9734 static void
9735 maybe_warn_pessimizing_move (tree retval, tree functype)
9736 {
9737 if (!(warn_pessimizing_move || warn_redundant_move))
9738 return;
9739
9740 location_t loc = cp_expr_loc_or_input_loc (retval);
9741
9742 /* C++98 doesn't know move. */
9743 if (cxx_dialect < cxx11)
9744 return;
9745
9746 /* Wait until instantiation time, since we can't gauge if we should do
9747 the NRVO until then. */
9748 if (processing_template_decl)
9749 return;
9750
9751 /* This is only interesting for class types. */
9752 if (!CLASS_TYPE_P (functype))
9753 return;
9754
9755 /* We're looking for *std::move<T&> ((T &) &arg). */
9756 if (REFERENCE_REF_P (retval)
9757 && TREE_CODE (TREE_OPERAND (retval, 0)) == CALL_EXPR)
9758 {
9759 tree fn = TREE_OPERAND (retval, 0);
9760 if (is_std_move_p (fn))
9761 {
9762 tree arg = CALL_EXPR_ARG (fn, 0);
9763 if (TREE_CODE (arg) != NOP_EXPR)
9764 return;
9765 arg = TREE_OPERAND (arg, 0);
9766 if (TREE_CODE (arg) != ADDR_EXPR)
9767 return;
9768 arg = TREE_OPERAND (arg, 0);
9769 arg = convert_from_reference (arg);
9770 /* Warn if we could do copy elision were it not for the move. */
9771 if (can_do_nrvo_p (arg, functype))
9772 {
9773 auto_diagnostic_group d;
9774 if (warning_at (loc, OPT_Wpessimizing_move,
9775 "moving a local object in a return statement "
9776 "prevents copy elision"))
9777 inform (loc, "remove %<std::move%> call");
9778 }
9779 /* Warn if the move is redundant. It is redundant when we would
9780 do maybe-rvalue overload resolution even without std::move. */
9781 else if (warn_redundant_move
9782 && treat_lvalue_as_rvalue_p (arg, /*parm_ok*/true))
9783 {
9784 /* Make sure that the overload resolution would actually succeed
9785 if we removed the std::move call. */
9786 tree t = convert_for_initialization (NULL_TREE, functype,
9787 move (arg),
9788 (LOOKUP_NORMAL
9789 | LOOKUP_ONLYCONVERTING
9790 | LOOKUP_PREFER_RVALUE),
9791 ICR_RETURN, NULL_TREE, 0,
9792 tf_none);
9793 /* If this worked, implicit rvalue would work, so the call to
9794 std::move is redundant. */
9795 if (t != error_mark_node)
9796 {
9797 auto_diagnostic_group d;
9798 if (warning_at (loc, OPT_Wredundant_move,
9799 "redundant move in return statement"))
9800 inform (loc, "remove %<std::move%> call");
9801 }
9802 }
9803 }
9804 }
9805 }
9806
9807 /* Check that returning RETVAL from the current function is valid.
9808 Return an expression explicitly showing all conversions required to
9809 change RETVAL into the function return type, and to assign it to
9810 the DECL_RESULT for the function. Set *NO_WARNING to true if
9811 code reaches end of non-void function warning shouldn't be issued
9812 on this RETURN_EXPR. */
9813
9814 tree
9815 check_return_expr (tree retval, bool *no_warning)
9816 {
9817 tree result;
9818 /* The type actually returned by the function. */
9819 tree valtype;
9820 /* The type the function is declared to return, or void if
9821 the declared type is incomplete. */
9822 tree functype;
9823 int fn_returns_value_p;
9824 location_t loc = cp_expr_loc_or_input_loc (retval);
9825
9826 *no_warning = false;
9827
9828 /* A `volatile' function is one that isn't supposed to return, ever.
9829 (This is a G++ extension, used to get better code for functions
9830 that call the `volatile' function.) */
9831 if (TREE_THIS_VOLATILE (current_function_decl))
9832 warning (0, "function declared %<noreturn%> has a %<return%> statement");
9833
9834 /* Check for various simple errors. */
9835 if (DECL_DESTRUCTOR_P (current_function_decl))
9836 {
9837 if (retval)
9838 error_at (loc, "returning a value from a destructor");
9839 return NULL_TREE;
9840 }
9841 else if (DECL_CONSTRUCTOR_P (current_function_decl))
9842 {
9843 if (in_function_try_handler)
9844 /* If a return statement appears in a handler of the
9845 function-try-block of a constructor, the program is ill-formed. */
9846 error ("cannot return from a handler of a function-try-block of a constructor");
9847 else if (retval)
9848 /* You can't return a value from a constructor. */
9849 error_at (loc, "returning a value from a constructor");
9850 return NULL_TREE;
9851 }
9852
9853 const tree saved_retval = retval;
9854
9855 if (processing_template_decl)
9856 {
9857 current_function_returns_value = 1;
9858
9859 if (check_for_bare_parameter_packs (retval))
9860 return error_mark_node;
9861
9862 /* If one of the types might be void, we can't tell whether we're
9863 returning a value. */
9864 if ((WILDCARD_TYPE_P (TREE_TYPE (DECL_RESULT (current_function_decl)))
9865 && !FNDECL_USED_AUTO (current_function_decl))
9866 || (retval != NULL_TREE
9867 && (TREE_TYPE (retval) == NULL_TREE
9868 || WILDCARD_TYPE_P (TREE_TYPE (retval)))))
9869 goto dependent;
9870 }
9871
9872 functype = TREE_TYPE (TREE_TYPE (current_function_decl));
9873
9874 /* Deduce auto return type from a return statement. */
9875 if (FNDECL_USED_AUTO (current_function_decl))
9876 {
9877 tree pattern = DECL_SAVED_AUTO_RETURN_TYPE (current_function_decl);
9878 tree auto_node;
9879 tree type;
9880
9881 if (!retval && !is_auto (pattern))
9882 {
9883 /* Give a helpful error message. */
9884 error ("return-statement with no value, in function returning %qT",
9885 pattern);
9886 inform (input_location, "only plain %<auto%> return type can be "
9887 "deduced to %<void%>");
9888 type = error_mark_node;
9889 }
9890 else if (retval && BRACE_ENCLOSED_INITIALIZER_P (retval))
9891 {
9892 error ("returning initializer list");
9893 type = error_mark_node;
9894 }
9895 else
9896 {
9897 if (!retval)
9898 retval = void_node;
9899 auto_node = type_uses_auto (pattern);
9900 type = do_auto_deduction (pattern, retval, auto_node,
9901 tf_warning_or_error, adc_return_type);
9902 }
9903
9904 if (type == error_mark_node)
9905 /* Leave it. */;
9906 else if (functype == pattern)
9907 apply_deduced_return_type (current_function_decl, type);
9908 else if (!same_type_p (type, functype))
9909 {
9910 if (LAMBDA_FUNCTION_P (current_function_decl))
9911 error_at (loc, "inconsistent types %qT and %qT deduced for "
9912 "lambda return type", functype, type);
9913 else
9914 error_at (loc, "inconsistent deduction for auto return type: "
9915 "%qT and then %qT", functype, type);
9916 }
9917 functype = type;
9918 }
9919
9920 result = DECL_RESULT (current_function_decl);
9921 valtype = TREE_TYPE (result);
9922 gcc_assert (valtype != NULL_TREE);
9923 fn_returns_value_p = !VOID_TYPE_P (valtype);
9924
9925 /* Check for a return statement with no return value in a function
9926 that's supposed to return a value. */
9927 if (!retval && fn_returns_value_p)
9928 {
9929 if (functype != error_mark_node)
9930 permerror (input_location, "return-statement with no value, in "
9931 "function returning %qT", valtype);
9932 /* Remember that this function did return. */
9933 current_function_returns_value = 1;
9934 /* And signal caller that TREE_NO_WARNING should be set on the
9935 RETURN_EXPR to avoid control reaches end of non-void function
9936 warnings in tree-cfg.c. */
9937 *no_warning = true;
9938 }
9939 /* Check for a return statement with a value in a function that
9940 isn't supposed to return a value. */
9941 else if (retval && !fn_returns_value_p)
9942 {
9943 if (VOID_TYPE_P (TREE_TYPE (retval)))
9944 /* You can return a `void' value from a function of `void'
9945 type. In that case, we have to evaluate the expression for
9946 its side-effects. */
9947 finish_expr_stmt (retval);
9948 else if (retval != error_mark_node)
9949 permerror (loc, "return-statement with a value, in function "
9950 "returning %qT", valtype);
9951 current_function_returns_null = 1;
9952
9953 /* There's really no value to return, after all. */
9954 return NULL_TREE;
9955 }
9956 else if (!retval)
9957 /* Remember that this function can sometimes return without a
9958 value. */
9959 current_function_returns_null = 1;
9960 else
9961 /* Remember that this function did return a value. */
9962 current_function_returns_value = 1;
9963
9964 /* Check for erroneous operands -- but after giving ourselves a
9965 chance to provide an error about returning a value from a void
9966 function. */
9967 if (error_operand_p (retval))
9968 {
9969 current_function_return_value = error_mark_node;
9970 return error_mark_node;
9971 }
9972
9973 /* Only operator new(...) throw(), can return NULL [expr.new/13]. */
9974 if (IDENTIFIER_NEW_OP_P (DECL_NAME (current_function_decl))
9975 && !TYPE_NOTHROW_P (TREE_TYPE (current_function_decl))
9976 && ! flag_check_new
9977 && retval && null_ptr_cst_p (retval))
9978 warning (0, "%<operator new%> must not return NULL unless it is "
9979 "declared %<throw()%> (or %<-fcheck-new%> is in effect)");
9980
9981 /* Effective C++ rule 15. See also start_function. */
9982 if (warn_ecpp
9983 && DECL_NAME (current_function_decl) == assign_op_identifier
9984 && !type_dependent_expression_p (retval))
9985 {
9986 bool warn = true;
9987
9988 /* The function return type must be a reference to the current
9989 class. */
9990 if (TYPE_REF_P (valtype)
9991 && same_type_ignoring_top_level_qualifiers_p
9992 (TREE_TYPE (valtype), TREE_TYPE (current_class_ref)))
9993 {
9994 /* Returning '*this' is obviously OK. */
9995 if (retval == current_class_ref)
9996 warn = false;
9997 /* If we are calling a function whose return type is the same of
9998 the current class reference, it is ok. */
9999 else if (INDIRECT_REF_P (retval)
10000 && TREE_CODE (TREE_OPERAND (retval, 0)) == CALL_EXPR)
10001 warn = false;
10002 }
10003
10004 if (warn)
10005 warning_at (loc, OPT_Weffc__,
10006 "%<operator=%> should return a reference to %<*this%>");
10007 }
10008
10009 if (dependent_type_p (functype)
10010 || type_dependent_expression_p (retval))
10011 {
10012 dependent:
10013 /* We should not have changed the return value. */
10014 gcc_assert (retval == saved_retval);
10015 return retval;
10016 }
10017
10018 /* The fabled Named Return Value optimization, as per [class.copy]/15:
10019
10020 [...] For a function with a class return type, if the expression
10021 in the return statement is the name of a local object, and the cv-
10022 unqualified type of the local object is the same as the function
10023 return type, an implementation is permitted to omit creating the tem-
10024 porary object to hold the function return value [...]
10025
10026 So, if this is a value-returning function that always returns the same
10027 local variable, remember it.
10028
10029 It might be nice to be more flexible, and choose the first suitable
10030 variable even if the function sometimes returns something else, but
10031 then we run the risk of clobbering the variable we chose if the other
10032 returned expression uses the chosen variable somehow. And people expect
10033 this restriction, anyway. (jason 2000-11-19)
10034
10035 See finish_function and finalize_nrv for the rest of this optimization. */
10036 if (retval)
10037 STRIP_ANY_LOCATION_WRAPPER (retval);
10038
10039 bool named_return_value_okay_p = can_do_nrvo_p (retval, functype);
10040 if (fn_returns_value_p && flag_elide_constructors)
10041 {
10042 if (named_return_value_okay_p
10043 && (current_function_return_value == NULL_TREE
10044 || current_function_return_value == retval))
10045 current_function_return_value = retval;
10046 else
10047 current_function_return_value = error_mark_node;
10048 }
10049
10050 /* We don't need to do any conversions when there's nothing being
10051 returned. */
10052 if (!retval)
10053 return NULL_TREE;
10054
10055 if (!named_return_value_okay_p)
10056 maybe_warn_pessimizing_move (retval, functype);
10057
10058 /* Do any required conversions. */
10059 if (retval == result || DECL_CONSTRUCTOR_P (current_function_decl))
10060 /* No conversions are required. */
10061 ;
10062 else
10063 {
10064 int flags = LOOKUP_NORMAL | LOOKUP_ONLYCONVERTING;
10065
10066 /* The functype's return type will have been set to void, if it
10067 was an incomplete type. Just treat this as 'return;' */
10068 if (VOID_TYPE_P (functype))
10069 return error_mark_node;
10070
10071 /* If we had an id-expression obfuscated by force_paren_expr, we need
10072 to undo it so we can try to treat it as an rvalue below. */
10073 retval = maybe_undo_parenthesized_ref (retval);
10074
10075 if (processing_template_decl)
10076 retval = build_non_dependent_expr (retval);
10077
10078 /* Under C++11 [12.8/32 class.copy], a returned lvalue is sometimes
10079 treated as an rvalue for the purposes of overload resolution to
10080 favor move constructors over copy constructors.
10081
10082 Note that these conditions are similar to, but not as strict as,
10083 the conditions for the named return value optimization. */
10084 bool converted = false;
10085 if (treat_lvalue_as_rvalue_p (retval, /*parm_ok*/true)
10086 /* This is only interesting for class type. */
10087 && CLASS_TYPE_P (functype))
10088 {
10089 tree moved = move (retval);
10090 moved = convert_for_initialization
10091 (NULL_TREE, functype, moved, flags|LOOKUP_PREFER_RVALUE,
10092 ICR_RETURN, NULL_TREE, 0, tf_none);
10093 if (moved != error_mark_node)
10094 {
10095 retval = moved;
10096 converted = true;
10097 }
10098 }
10099
10100 /* The call in a (lambda) thunk needs no conversions. */
10101 if (TREE_CODE (retval) == CALL_EXPR
10102 && CALL_FROM_THUNK_P (retval))
10103 converted = true;
10104
10105 /* First convert the value to the function's return type, then
10106 to the type of return value's location to handle the
10107 case that functype is smaller than the valtype. */
10108 if (!converted)
10109 retval = convert_for_initialization
10110 (NULL_TREE, functype, retval, flags, ICR_RETURN, NULL_TREE, 0,
10111 tf_warning_or_error);
10112 retval = convert (valtype, retval);
10113
10114 /* If the conversion failed, treat this just like `return;'. */
10115 if (retval == error_mark_node)
10116 return retval;
10117 /* We can't initialize a register from a AGGR_INIT_EXPR. */
10118 else if (! cfun->returns_struct
10119 && TREE_CODE (retval) == TARGET_EXPR
10120 && TREE_CODE (TREE_OPERAND (retval, 1)) == AGGR_INIT_EXPR)
10121 retval = build2 (COMPOUND_EXPR, TREE_TYPE (retval), retval,
10122 TREE_OPERAND (retval, 0));
10123 else if (!processing_template_decl
10124 && maybe_warn_about_returning_address_of_local (retval, loc)
10125 && INDIRECT_TYPE_P (valtype))
10126 retval = build2 (COMPOUND_EXPR, TREE_TYPE (retval), retval,
10127 build_zero_cst (TREE_TYPE (retval)));
10128 }
10129
10130 if (processing_template_decl)
10131 return saved_retval;
10132
10133 /* Actually copy the value returned into the appropriate location. */
10134 if (retval && retval != result)
10135 retval = build2 (INIT_EXPR, TREE_TYPE (result), result, retval);
10136
10137 if (tree set = maybe_set_retval_sentinel ())
10138 retval = build2 (COMPOUND_EXPR, void_type_node, retval, set);
10139
10140 return retval;
10141 }
10142
10143 \f
10144 /* Returns nonzero if the pointer-type FROM can be converted to the
10145 pointer-type TO via a qualification conversion. If CONSTP is -1,
10146 then we return nonzero if the pointers are similar, and the
10147 cv-qualification signature of FROM is a proper subset of that of TO.
10148
10149 If CONSTP is positive, then all outer pointers have been
10150 const-qualified. */
10151
10152 static bool
10153 comp_ptr_ttypes_real (tree to, tree from, int constp)
10154 {
10155 bool to_more_cv_qualified = false;
10156 bool is_opaque_pointer = false;
10157
10158 for (; ; to = TREE_TYPE (to), from = TREE_TYPE (from))
10159 {
10160 if (TREE_CODE (to) != TREE_CODE (from))
10161 return false;
10162
10163 if (TREE_CODE (from) == OFFSET_TYPE
10164 && !same_type_p (TYPE_OFFSET_BASETYPE (from),
10165 TYPE_OFFSET_BASETYPE (to)))
10166 return false;
10167
10168 /* Const and volatile mean something different for function and
10169 array types, so the usual checks are not appropriate. We'll
10170 check the array type elements in further iterations. */
10171 if (!FUNC_OR_METHOD_TYPE_P (to) && TREE_CODE (to) != ARRAY_TYPE)
10172 {
10173 if (!at_least_as_qualified_p (to, from))
10174 return false;
10175
10176 if (!at_least_as_qualified_p (from, to))
10177 {
10178 if (constp == 0)
10179 return false;
10180 to_more_cv_qualified = true;
10181 }
10182
10183 if (constp > 0)
10184 constp &= TYPE_READONLY (to);
10185 }
10186
10187 if (VECTOR_TYPE_P (to))
10188 is_opaque_pointer = vector_targets_convertible_p (to, from);
10189
10190 /* P0388R4 allows a conversion from int[N] to int[] but not the
10191 other way round. When both arrays have bounds but they do
10192 not match, then no conversion is possible. */
10193 if (TREE_CODE (to) == ARRAY_TYPE
10194 && !comp_array_types (to, from, bounds_first, /*strict=*/false))
10195 return false;
10196
10197 if (!TYPE_PTR_P (to)
10198 && !TYPE_PTRDATAMEM_P (to)
10199 /* CWG 330 says we need to look through arrays. */
10200 && TREE_CODE (to) != ARRAY_TYPE)
10201 return ((constp >= 0 || to_more_cv_qualified)
10202 && (is_opaque_pointer
10203 || same_type_ignoring_top_level_qualifiers_p (to, from)));
10204 }
10205 }
10206
10207 /* When comparing, say, char ** to char const **, this function takes
10208 the 'char *' and 'char const *'. Do not pass non-pointer/reference
10209 types to this function. */
10210
10211 int
10212 comp_ptr_ttypes (tree to, tree from)
10213 {
10214 return comp_ptr_ttypes_real (to, from, 1);
10215 }
10216
10217 /* Returns true iff FNTYPE is a non-class type that involves
10218 error_mark_node. We can get FUNCTION_TYPE with buried error_mark_node
10219 if a parameter type is ill-formed. */
10220
10221 bool
10222 error_type_p (const_tree type)
10223 {
10224 tree t;
10225
10226 switch (TREE_CODE (type))
10227 {
10228 case ERROR_MARK:
10229 return true;
10230
10231 case POINTER_TYPE:
10232 case REFERENCE_TYPE:
10233 case OFFSET_TYPE:
10234 return error_type_p (TREE_TYPE (type));
10235
10236 case FUNCTION_TYPE:
10237 case METHOD_TYPE:
10238 if (error_type_p (TREE_TYPE (type)))
10239 return true;
10240 for (t = TYPE_ARG_TYPES (type); t; t = TREE_CHAIN (t))
10241 if (error_type_p (TREE_VALUE (t)))
10242 return true;
10243 return false;
10244
10245 case RECORD_TYPE:
10246 if (TYPE_PTRMEMFUNC_P (type))
10247 return error_type_p (TYPE_PTRMEMFUNC_FN_TYPE (type));
10248 return false;
10249
10250 default:
10251 return false;
10252 }
10253 }
10254
10255 /* Returns true if to and from are (possibly multi-level) pointers to the same
10256 type or inheritance-related types, regardless of cv-quals. */
10257
10258 bool
10259 ptr_reasonably_similar (const_tree to, const_tree from)
10260 {
10261 for (; ; to = TREE_TYPE (to), from = TREE_TYPE (from))
10262 {
10263 /* Any target type is similar enough to void. */
10264 if (VOID_TYPE_P (to))
10265 return !error_type_p (from);
10266 if (VOID_TYPE_P (from))
10267 return !error_type_p (to);
10268
10269 if (TREE_CODE (to) != TREE_CODE (from))
10270 return false;
10271
10272 if (TREE_CODE (from) == OFFSET_TYPE
10273 && comptypes (TYPE_OFFSET_BASETYPE (to),
10274 TYPE_OFFSET_BASETYPE (from),
10275 COMPARE_BASE | COMPARE_DERIVED))
10276 continue;
10277
10278 if (VECTOR_TYPE_P (to)
10279 && vector_types_convertible_p (to, from, false))
10280 return true;
10281
10282 if (TREE_CODE (to) == INTEGER_TYPE
10283 && TYPE_PRECISION (to) == TYPE_PRECISION (from))
10284 return true;
10285
10286 if (TREE_CODE (to) == FUNCTION_TYPE)
10287 return !error_type_p (to) && !error_type_p (from);
10288
10289 if (!TYPE_PTR_P (to))
10290 {
10291 /* When either type is incomplete avoid DERIVED_FROM_P,
10292 which may call complete_type (c++/57942). */
10293 bool b = !COMPLETE_TYPE_P (to) || !COMPLETE_TYPE_P (from);
10294 return comptypes
10295 (TYPE_MAIN_VARIANT (to), TYPE_MAIN_VARIANT (from),
10296 b ? COMPARE_STRICT : COMPARE_BASE | COMPARE_DERIVED);
10297 }
10298 }
10299 }
10300
10301 /* Return true if TO and FROM (both of which are POINTER_TYPEs or
10302 pointer-to-member types) are the same, ignoring cv-qualification at
10303 all levels. CB says how we should behave when comparing array bounds. */
10304
10305 bool
10306 comp_ptr_ttypes_const (tree to, tree from, compare_bounds_t cb)
10307 {
10308 bool is_opaque_pointer = false;
10309
10310 for (; ; to = TREE_TYPE (to), from = TREE_TYPE (from))
10311 {
10312 if (TREE_CODE (to) != TREE_CODE (from))
10313 return false;
10314
10315 if (TREE_CODE (from) == OFFSET_TYPE
10316 && same_type_p (TYPE_OFFSET_BASETYPE (from),
10317 TYPE_OFFSET_BASETYPE (to)))
10318 continue;
10319
10320 if (VECTOR_TYPE_P (to))
10321 is_opaque_pointer = vector_targets_convertible_p (to, from);
10322
10323 if (TREE_CODE (to) == ARRAY_TYPE
10324 /* Ignore cv-qualification, but if we see e.g. int[3] and int[4],
10325 we must fail. */
10326 && !comp_array_types (to, from, cb, /*strict=*/false))
10327 return false;
10328
10329 /* CWG 330 says we need to look through arrays. */
10330 if (!TYPE_PTR_P (to) && TREE_CODE (to) != ARRAY_TYPE)
10331 return (is_opaque_pointer
10332 || same_type_ignoring_top_level_qualifiers_p (to, from));
10333 }
10334 }
10335
10336 /* Returns the type qualifiers for this type, including the qualifiers on the
10337 elements for an array type. */
10338
10339 int
10340 cp_type_quals (const_tree type)
10341 {
10342 int quals;
10343 /* This CONST_CAST is okay because strip_array_types returns its
10344 argument unmodified and we assign it to a const_tree. */
10345 type = strip_array_types (CONST_CAST_TREE (type));
10346 if (type == error_mark_node
10347 /* Quals on a FUNCTION_TYPE are memfn quals. */
10348 || TREE_CODE (type) == FUNCTION_TYPE)
10349 return TYPE_UNQUALIFIED;
10350 quals = TYPE_QUALS (type);
10351 /* METHOD and REFERENCE_TYPEs should never have quals. */
10352 gcc_assert ((TREE_CODE (type) != METHOD_TYPE
10353 && !TYPE_REF_P (type))
10354 || ((quals & (TYPE_QUAL_CONST|TYPE_QUAL_VOLATILE))
10355 == TYPE_UNQUALIFIED));
10356 return quals;
10357 }
10358
10359 /* Returns the function-ref-qualifier for TYPE */
10360
10361 cp_ref_qualifier
10362 type_memfn_rqual (const_tree type)
10363 {
10364 gcc_assert (FUNC_OR_METHOD_TYPE_P (type));
10365
10366 if (!FUNCTION_REF_QUALIFIED (type))
10367 return REF_QUAL_NONE;
10368 else if (FUNCTION_RVALUE_QUALIFIED (type))
10369 return REF_QUAL_RVALUE;
10370 else
10371 return REF_QUAL_LVALUE;
10372 }
10373
10374 /* Returns the function-cv-quals for TYPE, which must be a FUNCTION_TYPE or
10375 METHOD_TYPE. */
10376
10377 int
10378 type_memfn_quals (const_tree type)
10379 {
10380 if (TREE_CODE (type) == FUNCTION_TYPE)
10381 return TYPE_QUALS (type);
10382 else if (TREE_CODE (type) == METHOD_TYPE)
10383 return cp_type_quals (class_of_this_parm (type));
10384 else
10385 gcc_unreachable ();
10386 }
10387
10388 /* Returns the FUNCTION_TYPE TYPE with its function-cv-quals changed to
10389 MEMFN_QUALS and its ref-qualifier to RQUAL. */
10390
10391 tree
10392 apply_memfn_quals (tree type, cp_cv_quals memfn_quals, cp_ref_qualifier rqual)
10393 {
10394 /* Could handle METHOD_TYPE here if necessary. */
10395 gcc_assert (TREE_CODE (type) == FUNCTION_TYPE);
10396 if (TYPE_QUALS (type) == memfn_quals
10397 && type_memfn_rqual (type) == rqual)
10398 return type;
10399
10400 /* This should really have a different TYPE_MAIN_VARIANT, but that gets
10401 complex. */
10402 tree result = build_qualified_type (type, memfn_quals);
10403 return build_ref_qualified_type (result, rqual);
10404 }
10405
10406 /* Returns nonzero if TYPE is const or volatile. */
10407
10408 bool
10409 cv_qualified_p (const_tree type)
10410 {
10411 int quals = cp_type_quals (type);
10412 return (quals & (TYPE_QUAL_CONST|TYPE_QUAL_VOLATILE)) != 0;
10413 }
10414
10415 /* Returns nonzero if the TYPE contains a mutable member. */
10416
10417 bool
10418 cp_has_mutable_p (const_tree type)
10419 {
10420 /* This CONST_CAST is okay because strip_array_types returns its
10421 argument unmodified and we assign it to a const_tree. */
10422 type = strip_array_types (CONST_CAST_TREE(type));
10423
10424 return CLASS_TYPE_P (type) && CLASSTYPE_HAS_MUTABLE (type);
10425 }
10426
10427 /* Set TREE_READONLY and TREE_VOLATILE on DECL as indicated by the
10428 TYPE_QUALS. For a VAR_DECL, this may be an optimistic
10429 approximation. In particular, consider:
10430
10431 int f();
10432 struct S { int i; };
10433 const S s = { f(); }
10434
10435 Here, we will make "s" as TREE_READONLY (because it is declared
10436 "const") -- only to reverse ourselves upon seeing that the
10437 initializer is non-constant. */
10438
10439 void
10440 cp_apply_type_quals_to_decl (int type_quals, tree decl)
10441 {
10442 tree type = TREE_TYPE (decl);
10443
10444 if (type == error_mark_node)
10445 return;
10446
10447 if (TREE_CODE (decl) == TYPE_DECL)
10448 return;
10449
10450 gcc_assert (!(TREE_CODE (type) == FUNCTION_TYPE
10451 && type_quals != TYPE_UNQUALIFIED));
10452
10453 /* Avoid setting TREE_READONLY incorrectly. */
10454 /* We used to check TYPE_NEEDS_CONSTRUCTING here, but now a constexpr
10455 constructor can produce constant init, so rely on cp_finish_decl to
10456 clear TREE_READONLY if the variable has non-constant init. */
10457
10458 /* If the type has (or might have) a mutable component, that component
10459 might be modified. */
10460 if (TYPE_HAS_MUTABLE_P (type) || !COMPLETE_TYPE_P (type))
10461 type_quals &= ~TYPE_QUAL_CONST;
10462
10463 c_apply_type_quals_to_decl (type_quals, decl);
10464 }
10465
10466 /* Subroutine of casts_away_constness. Make T1 and T2 point at
10467 exemplar types such that casting T1 to T2 is casting away constness
10468 if and only if there is no implicit conversion from T1 to T2. */
10469
10470 static void
10471 casts_away_constness_r (tree *t1, tree *t2, tsubst_flags_t complain)
10472 {
10473 int quals1;
10474 int quals2;
10475
10476 /* [expr.const.cast]
10477
10478 For multi-level pointer to members and multi-level mixed pointers
10479 and pointers to members (conv.qual), the "member" aspect of a
10480 pointer to member level is ignored when determining if a const
10481 cv-qualifier has been cast away. */
10482 /* [expr.const.cast]
10483
10484 For two pointer types:
10485
10486 X1 is T1cv1,1 * ... cv1,N * where T1 is not a pointer type
10487 X2 is T2cv2,1 * ... cv2,M * where T2 is not a pointer type
10488 K is min(N,M)
10489
10490 casting from X1 to X2 casts away constness if, for a non-pointer
10491 type T there does not exist an implicit conversion (clause
10492 _conv_) from:
10493
10494 Tcv1,(N-K+1) * cv1,(N-K+2) * ... cv1,N *
10495
10496 to
10497
10498 Tcv2,(M-K+1) * cv2,(M-K+2) * ... cv2,M *. */
10499 if ((!TYPE_PTR_P (*t1) && !TYPE_PTRDATAMEM_P (*t1))
10500 || (!TYPE_PTR_P (*t2) && !TYPE_PTRDATAMEM_P (*t2)))
10501 {
10502 *t1 = cp_build_qualified_type (void_type_node,
10503 cp_type_quals (*t1));
10504 *t2 = cp_build_qualified_type (void_type_node,
10505 cp_type_quals (*t2));
10506 return;
10507 }
10508
10509 quals1 = cp_type_quals (*t1);
10510 quals2 = cp_type_quals (*t2);
10511
10512 if (TYPE_PTRDATAMEM_P (*t1))
10513 *t1 = TYPE_PTRMEM_POINTED_TO_TYPE (*t1);
10514 else
10515 *t1 = TREE_TYPE (*t1);
10516 if (TYPE_PTRDATAMEM_P (*t2))
10517 *t2 = TYPE_PTRMEM_POINTED_TO_TYPE (*t2);
10518 else
10519 *t2 = TREE_TYPE (*t2);
10520
10521 casts_away_constness_r (t1, t2, complain);
10522 *t1 = build_pointer_type (*t1);
10523 *t2 = build_pointer_type (*t2);
10524 *t1 = cp_build_qualified_type (*t1, quals1);
10525 *t2 = cp_build_qualified_type (*t2, quals2);
10526 }
10527
10528 /* Returns nonzero if casting from TYPE1 to TYPE2 casts away
10529 constness.
10530
10531 ??? This function returns non-zero if casting away qualifiers not
10532 just const. We would like to return to the caller exactly which
10533 qualifiers are casted away to give more accurate diagnostics.
10534 */
10535
10536 static bool
10537 casts_away_constness (tree t1, tree t2, tsubst_flags_t complain)
10538 {
10539 if (TYPE_REF_P (t2))
10540 {
10541 /* [expr.const.cast]
10542
10543 Casting from an lvalue of type T1 to an lvalue of type T2
10544 using a reference cast casts away constness if a cast from an
10545 rvalue of type "pointer to T1" to the type "pointer to T2"
10546 casts away constness. */
10547 t1 = (TYPE_REF_P (t1) ? TREE_TYPE (t1) : t1);
10548 return casts_away_constness (build_pointer_type (t1),
10549 build_pointer_type (TREE_TYPE (t2)),
10550 complain);
10551 }
10552
10553 if (TYPE_PTRDATAMEM_P (t1) && TYPE_PTRDATAMEM_P (t2))
10554 /* [expr.const.cast]
10555
10556 Casting from an rvalue of type "pointer to data member of X
10557 of type T1" to the type "pointer to data member of Y of type
10558 T2" casts away constness if a cast from an rvalue of type
10559 "pointer to T1" to the type "pointer to T2" casts away
10560 constness. */
10561 return casts_away_constness
10562 (build_pointer_type (TYPE_PTRMEM_POINTED_TO_TYPE (t1)),
10563 build_pointer_type (TYPE_PTRMEM_POINTED_TO_TYPE (t2)),
10564 complain);
10565
10566 /* Casting away constness is only something that makes sense for
10567 pointer or reference types. */
10568 if (!TYPE_PTR_P (t1) || !TYPE_PTR_P (t2))
10569 return false;
10570
10571 /* Top-level qualifiers don't matter. */
10572 t1 = TYPE_MAIN_VARIANT (t1);
10573 t2 = TYPE_MAIN_VARIANT (t2);
10574 casts_away_constness_r (&t1, &t2, complain);
10575 if (!can_convert (t2, t1, complain))
10576 return true;
10577
10578 return false;
10579 }
10580
10581 /* If T is a REFERENCE_TYPE return the type to which T refers.
10582 Otherwise, return T itself. */
10583
10584 tree
10585 non_reference (tree t)
10586 {
10587 if (t && TYPE_REF_P (t))
10588 t = TREE_TYPE (t);
10589 return t;
10590 }
10591
10592
10593 /* Return nonzero if REF is an lvalue valid for this language;
10594 otherwise, print an error message and return zero. USE says
10595 how the lvalue is being used and so selects the error message. */
10596
10597 int
10598 lvalue_or_else (tree ref, enum lvalue_use use, tsubst_flags_t complain)
10599 {
10600 cp_lvalue_kind kind = lvalue_kind (ref);
10601
10602 if (kind == clk_none)
10603 {
10604 if (complain & tf_error)
10605 lvalue_error (cp_expr_loc_or_input_loc (ref), use);
10606 return 0;
10607 }
10608 else if (kind & (clk_rvalueref|clk_class))
10609 {
10610 if (!(complain & tf_error))
10611 return 0;
10612 /* Make this a permerror because we used to accept it. */
10613 permerror (cp_expr_loc_or_input_loc (ref),
10614 "using rvalue as lvalue");
10615 }
10616 return 1;
10617 }
10618
10619 /* Return true if a user-defined literal operator is a raw operator. */
10620
10621 bool
10622 check_raw_literal_operator (const_tree decl)
10623 {
10624 tree argtypes = TYPE_ARG_TYPES (TREE_TYPE (decl));
10625 tree argtype;
10626 int arity;
10627 bool maybe_raw_p = false;
10628
10629 /* Count the number and type of arguments and check for ellipsis. */
10630 for (argtype = argtypes, arity = 0;
10631 argtype && argtype != void_list_node;
10632 ++arity, argtype = TREE_CHAIN (argtype))
10633 {
10634 tree t = TREE_VALUE (argtype);
10635
10636 if (same_type_p (t, const_string_type_node))
10637 maybe_raw_p = true;
10638 }
10639 if (!argtype)
10640 return false; /* Found ellipsis. */
10641
10642 if (!maybe_raw_p || arity != 1)
10643 return false;
10644
10645 return true;
10646 }
10647
10648
10649 /* Return true if a user-defined literal operator has one of the allowed
10650 argument types. */
10651
10652 bool
10653 check_literal_operator_args (const_tree decl,
10654 bool *long_long_unsigned_p, bool *long_double_p)
10655 {
10656 tree argtypes = TYPE_ARG_TYPES (TREE_TYPE (decl));
10657
10658 *long_long_unsigned_p = false;
10659 *long_double_p = false;
10660 if (processing_template_decl || processing_specialization)
10661 return argtypes == void_list_node;
10662 else
10663 {
10664 tree argtype;
10665 int arity;
10666 int max_arity = 2;
10667
10668 /* Count the number and type of arguments and check for ellipsis. */
10669 for (argtype = argtypes, arity = 0;
10670 argtype && argtype != void_list_node;
10671 argtype = TREE_CHAIN (argtype))
10672 {
10673 tree t = TREE_VALUE (argtype);
10674 ++arity;
10675
10676 if (TYPE_PTR_P (t))
10677 {
10678 bool maybe_raw_p = false;
10679 t = TREE_TYPE (t);
10680 if (cp_type_quals (t) != TYPE_QUAL_CONST)
10681 return false;
10682 t = TYPE_MAIN_VARIANT (t);
10683 if ((maybe_raw_p = same_type_p (t, char_type_node))
10684 || same_type_p (t, wchar_type_node)
10685 || same_type_p (t, char8_type_node)
10686 || same_type_p (t, char16_type_node)
10687 || same_type_p (t, char32_type_node))
10688 {
10689 argtype = TREE_CHAIN (argtype);
10690 if (!argtype)
10691 return false;
10692 t = TREE_VALUE (argtype);
10693 if (maybe_raw_p && argtype == void_list_node)
10694 return true;
10695 else if (same_type_p (t, size_type_node))
10696 {
10697 ++arity;
10698 continue;
10699 }
10700 else
10701 return false;
10702 }
10703 }
10704 else if (same_type_p (t, long_long_unsigned_type_node))
10705 {
10706 max_arity = 1;
10707 *long_long_unsigned_p = true;
10708 }
10709 else if (same_type_p (t, long_double_type_node))
10710 {
10711 max_arity = 1;
10712 *long_double_p = true;
10713 }
10714 else if (same_type_p (t, char_type_node))
10715 max_arity = 1;
10716 else if (same_type_p (t, wchar_type_node))
10717 max_arity = 1;
10718 else if (same_type_p (t, char8_type_node))
10719 max_arity = 1;
10720 else if (same_type_p (t, char16_type_node))
10721 max_arity = 1;
10722 else if (same_type_p (t, char32_type_node))
10723 max_arity = 1;
10724 else
10725 return false;
10726 }
10727 if (!argtype)
10728 return false; /* Found ellipsis. */
10729
10730 if (arity != max_arity)
10731 return false;
10732
10733 return true;
10734 }
10735 }
10736
10737 /* Always returns false since unlike C90, C++ has no concept of implicit
10738 function declarations. */
10739
10740 bool
10741 c_decl_implicit (const_tree)
10742 {
10743 return false;
10744 }