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