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