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