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