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