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