re PR c++/42567 ([C++0x] ICE with auto in type_unification_real, at cp/pt.c:13310)
[official-gcc.git] / gcc / cp / typeck.c
blobf17d9c52519e2f23ad4150749f86f60e7331d71b
1 /* Build expressions with type checking for C++ compiler.
2 Copyright (C) 1987, 1988, 1989, 1992, 1993, 1994, 1995, 1996, 1997, 1998,
3 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009
4 Free Software Foundation, Inc.
5 Hacked by Michael Tiemann (tiemann@cygnus.com)
7 This file is part of GCC.
9 GCC is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 3, or (at your option)
12 any later version.
14 GCC is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
19 You should have received a copy of the GNU General Public License
20 along with GCC; see the file COPYING3. If not see
21 <http://www.gnu.org/licenses/>. */
24 /* This file is part of the C++ front end.
25 It contains routines to build C++ expressions given their operands,
26 including computing the types of the result, C and C++ specific error
27 checks, and some optimization. */
29 #include "config.h"
30 #include "system.h"
31 #include "coretypes.h"
32 #include "tm.h"
33 #include "tree.h"
34 #include "rtl.h"
35 #include "expr.h"
36 #include "cp-tree.h"
37 #include "tm_p.h"
38 #include "flags.h"
39 #include "output.h"
40 #include "toplev.h"
41 #include "diagnostic.h"
42 #include "intl.h"
43 #include "target.h"
44 #include "convert.h"
45 #include "c-common.h"
46 #include "params.h"
48 static tree pfn_from_ptrmemfunc (tree);
49 static tree delta_from_ptrmemfunc (tree);
50 static tree convert_for_assignment (tree, tree, const char *, tree, int,
51 tsubst_flags_t, int);
52 static tree cp_pointer_int_sum (enum tree_code, tree, tree);
53 static tree rationalize_conditional_expr (enum tree_code, tree,
54 tsubst_flags_t);
55 static int comp_ptr_ttypes_real (tree, tree, int);
56 static bool comp_except_types (tree, tree, bool);
57 static bool comp_array_types (const_tree, const_tree, bool);
58 static tree pointer_diff (tree, tree, tree);
59 static tree get_delta_difference (tree, tree, bool, bool);
60 static void casts_away_constness_r (tree *, tree *);
61 static bool casts_away_constness (tree, tree);
62 static void maybe_warn_about_returning_address_of_local (tree);
63 static tree lookup_destructor (tree, tree, tree);
64 static int convert_arguments (tree, VEC(tree,gc) **, tree, int,
65 tsubst_flags_t);
67 /* Do `exp = require_complete_type (exp);' to make sure exp
68 does not have an incomplete type. (That includes void types.)
69 Returns the error_mark_node if the VALUE does not have
70 complete type when this function returns. */
72 tree
73 require_complete_type (tree value)
75 tree type;
77 if (processing_template_decl || value == error_mark_node)
78 return value;
80 if (TREE_CODE (value) == OVERLOAD)
81 type = unknown_type_node;
82 else
83 type = TREE_TYPE (value);
85 if (type == error_mark_node)
86 return error_mark_node;
88 /* First, detect a valid value with a complete type. */
89 if (COMPLETE_TYPE_P (type))
90 return value;
92 if (complete_type_or_else (type, value))
93 return value;
94 else
95 return error_mark_node;
98 /* Try to complete TYPE, if it is incomplete. For example, if TYPE is
99 a template instantiation, do the instantiation. Returns TYPE,
100 whether or not it could be completed, unless something goes
101 horribly wrong, in which case the error_mark_node is returned. */
103 tree
104 complete_type (tree type)
106 if (type == NULL_TREE)
107 /* Rather than crash, we return something sure to cause an error
108 at some point. */
109 return error_mark_node;
111 if (type == error_mark_node || COMPLETE_TYPE_P (type))
113 else if (TREE_CODE (type) == ARRAY_TYPE && TYPE_DOMAIN (type))
115 tree t = complete_type (TREE_TYPE (type));
116 unsigned int needs_constructing, has_nontrivial_dtor;
117 if (COMPLETE_TYPE_P (t) && !dependent_type_p (type))
118 layout_type (type);
119 needs_constructing
120 = TYPE_NEEDS_CONSTRUCTING (TYPE_MAIN_VARIANT (t));
121 has_nontrivial_dtor
122 = TYPE_HAS_NONTRIVIAL_DESTRUCTOR (TYPE_MAIN_VARIANT (t));
123 for (t = TYPE_MAIN_VARIANT (type); t; t = TYPE_NEXT_VARIANT (t))
125 TYPE_NEEDS_CONSTRUCTING (t) = needs_constructing;
126 TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t) = has_nontrivial_dtor;
129 else if (CLASS_TYPE_P (type) && CLASSTYPE_TEMPLATE_INSTANTIATION (type))
130 instantiate_class_template (TYPE_MAIN_VARIANT (type));
132 return type;
135 /* Like complete_type, but issue an error if the TYPE cannot be completed.
136 VALUE is used for informative diagnostics.
137 Returns NULL_TREE if the type cannot be made complete. */
139 tree
140 complete_type_or_else (tree type, tree value)
142 type = complete_type (type);
143 if (type == error_mark_node)
144 /* We already issued an error. */
145 return NULL_TREE;
146 else if (!COMPLETE_TYPE_P (type))
148 cxx_incomplete_type_diagnostic (value, type, DK_ERROR);
149 return NULL_TREE;
151 else
152 return type;
155 /* Return truthvalue of whether type of EXP is instantiated. */
158 type_unknown_p (const_tree exp)
160 return (TREE_CODE (exp) == TREE_LIST
161 || TREE_TYPE (exp) == unknown_type_node);
165 /* Return the common type of two parameter lists.
166 We assume that comptypes has already been done and returned 1;
167 if that isn't so, this may crash.
169 As an optimization, free the space we allocate if the parameter
170 lists are already common. */
172 static tree
173 commonparms (tree p1, tree p2)
175 tree oldargs = p1, newargs, n;
176 int i, len;
177 int any_change = 0;
179 len = list_length (p1);
180 newargs = tree_last (p1);
182 if (newargs == void_list_node)
183 i = 1;
184 else
186 i = 0;
187 newargs = 0;
190 for (; i < len; i++)
191 newargs = tree_cons (NULL_TREE, NULL_TREE, newargs);
193 n = newargs;
195 for (i = 0; p1;
196 p1 = TREE_CHAIN (p1), p2 = TREE_CHAIN (p2), n = TREE_CHAIN (n), i++)
198 if (TREE_PURPOSE (p1) && !TREE_PURPOSE (p2))
200 TREE_PURPOSE (n) = TREE_PURPOSE (p1);
201 any_change = 1;
203 else if (! TREE_PURPOSE (p1))
205 if (TREE_PURPOSE (p2))
207 TREE_PURPOSE (n) = TREE_PURPOSE (p2);
208 any_change = 1;
211 else
213 if (1 != simple_cst_equal (TREE_PURPOSE (p1), TREE_PURPOSE (p2)))
214 any_change = 1;
215 TREE_PURPOSE (n) = TREE_PURPOSE (p2);
217 if (TREE_VALUE (p1) != TREE_VALUE (p2))
219 any_change = 1;
220 TREE_VALUE (n) = merge_types (TREE_VALUE (p1), TREE_VALUE (p2));
222 else
223 TREE_VALUE (n) = TREE_VALUE (p1);
225 if (! any_change)
226 return oldargs;
228 return newargs;
231 /* Given a type, perhaps copied for a typedef,
232 find the "original" version of it. */
233 static tree
234 original_type (tree t)
236 int quals = cp_type_quals (t);
237 while (t != error_mark_node
238 && TYPE_NAME (t) != NULL_TREE)
240 tree x = TYPE_NAME (t);
241 if (TREE_CODE (x) != TYPE_DECL)
242 break;
243 x = DECL_ORIGINAL_TYPE (x);
244 if (x == NULL_TREE)
245 break;
246 t = x;
248 return cp_build_qualified_type (t, quals);
251 /* Return the common type for two arithmetic types T1 and T2 under the
252 usual arithmetic conversions. The default conversions have already
253 been applied, and enumerated types converted to their compatible
254 integer types. */
256 static tree
257 cp_common_type (tree t1, tree t2)
259 enum tree_code code1 = TREE_CODE (t1);
260 enum tree_code code2 = TREE_CODE (t2);
261 tree attributes;
263 /* In what follows, we slightly generalize the rules given in [expr] so
264 as to deal with `long long' and `complex'. First, merge the
265 attributes. */
266 attributes = (*targetm.merge_type_attributes) (t1, t2);
268 if (SCOPED_ENUM_P (t1) || SCOPED_ENUM_P (t2))
270 if (TYPE_MAIN_VARIANT (t1) == TYPE_MAIN_VARIANT (t2))
271 return build_type_attribute_variant (t1, attributes);
272 else
273 return NULL_TREE;
276 /* FIXME: Attributes. */
277 gcc_assert (ARITHMETIC_TYPE_P (t1)
278 || TREE_CODE (t1) == VECTOR_TYPE
279 || UNSCOPED_ENUM_P (t1));
280 gcc_assert (ARITHMETIC_TYPE_P (t2)
281 || TREE_CODE (t2) == VECTOR_TYPE
282 || UNSCOPED_ENUM_P (t2));
284 /* If one type is complex, form the common type of the non-complex
285 components, then make that complex. Use T1 or T2 if it is the
286 required type. */
287 if (code1 == COMPLEX_TYPE || code2 == COMPLEX_TYPE)
289 tree subtype1 = code1 == COMPLEX_TYPE ? TREE_TYPE (t1) : t1;
290 tree subtype2 = code2 == COMPLEX_TYPE ? TREE_TYPE (t2) : t2;
291 tree subtype
292 = type_after_usual_arithmetic_conversions (subtype1, subtype2);
294 if (code1 == COMPLEX_TYPE && TREE_TYPE (t1) == subtype)
295 return build_type_attribute_variant (t1, attributes);
296 else if (code2 == COMPLEX_TYPE && TREE_TYPE (t2) == subtype)
297 return build_type_attribute_variant (t2, attributes);
298 else
299 return build_type_attribute_variant (build_complex_type (subtype),
300 attributes);
303 if (code1 == VECTOR_TYPE)
305 /* When we get here we should have two vectors of the same size.
306 Just prefer the unsigned one if present. */
307 if (TYPE_UNSIGNED (t1))
308 return build_type_attribute_variant (t1, attributes);
309 else
310 return build_type_attribute_variant (t2, attributes);
313 /* If only one is real, use it as the result. */
314 if (code1 == REAL_TYPE && code2 != REAL_TYPE)
315 return build_type_attribute_variant (t1, attributes);
316 if (code2 == REAL_TYPE && code1 != REAL_TYPE)
317 return build_type_attribute_variant (t2, attributes);
319 /* Both real or both integers; use the one with greater precision. */
320 if (TYPE_PRECISION (t1) > TYPE_PRECISION (t2))
321 return build_type_attribute_variant (t1, attributes);
322 else if (TYPE_PRECISION (t2) > TYPE_PRECISION (t1))
323 return build_type_attribute_variant (t2, attributes);
325 /* The types are the same; no need to do anything fancy. */
326 if (TYPE_MAIN_VARIANT (t1) == TYPE_MAIN_VARIANT (t2))
327 return build_type_attribute_variant (t1, attributes);
329 if (code1 != REAL_TYPE)
331 /* If one is unsigned long long, then convert the other to unsigned
332 long long. */
333 if (same_type_p (TYPE_MAIN_VARIANT (t1), long_long_unsigned_type_node)
334 || same_type_p (TYPE_MAIN_VARIANT (t2), long_long_unsigned_type_node))
335 return build_type_attribute_variant (long_long_unsigned_type_node,
336 attributes);
337 /* If one is a long long, and the other is an unsigned long, and
338 long long can represent all the values of an unsigned long, then
339 convert to a long long. Otherwise, convert to an unsigned long
340 long. Otherwise, if either operand is long long, convert the
341 other to long long.
343 Since we're here, we know the TYPE_PRECISION is the same;
344 therefore converting to long long cannot represent all the values
345 of an unsigned long, so we choose unsigned long long in that
346 case. */
347 if (same_type_p (TYPE_MAIN_VARIANT (t1), long_long_integer_type_node)
348 || same_type_p (TYPE_MAIN_VARIANT (t2), long_long_integer_type_node))
350 tree t = ((TYPE_UNSIGNED (t1) || TYPE_UNSIGNED (t2))
351 ? long_long_unsigned_type_node
352 : long_long_integer_type_node);
353 return build_type_attribute_variant (t, attributes);
356 /* Go through the same procedure, but for longs. */
357 if (same_type_p (TYPE_MAIN_VARIANT (t1), long_unsigned_type_node)
358 || same_type_p (TYPE_MAIN_VARIANT (t2), long_unsigned_type_node))
359 return build_type_attribute_variant (long_unsigned_type_node,
360 attributes);
361 if (same_type_p (TYPE_MAIN_VARIANT (t1), long_integer_type_node)
362 || same_type_p (TYPE_MAIN_VARIANT (t2), long_integer_type_node))
364 tree t = ((TYPE_UNSIGNED (t1) || TYPE_UNSIGNED (t2))
365 ? long_unsigned_type_node : long_integer_type_node);
366 return build_type_attribute_variant (t, attributes);
368 /* Otherwise prefer the unsigned one. */
369 if (TYPE_UNSIGNED (t1))
370 return build_type_attribute_variant (t1, attributes);
371 else
372 return build_type_attribute_variant (t2, attributes);
374 else
376 if (same_type_p (TYPE_MAIN_VARIANT (t1), long_double_type_node)
377 || same_type_p (TYPE_MAIN_VARIANT (t2), long_double_type_node))
378 return build_type_attribute_variant (long_double_type_node,
379 attributes);
380 if (same_type_p (TYPE_MAIN_VARIANT (t1), double_type_node)
381 || same_type_p (TYPE_MAIN_VARIANT (t2), double_type_node))
382 return build_type_attribute_variant (double_type_node,
383 attributes);
384 if (same_type_p (TYPE_MAIN_VARIANT (t1), float_type_node)
385 || same_type_p (TYPE_MAIN_VARIANT (t2), float_type_node))
386 return build_type_attribute_variant (float_type_node,
387 attributes);
389 /* Two floating-point types whose TYPE_MAIN_VARIANTs are none of
390 the standard C++ floating-point types. Logic earlier in this
391 function has already eliminated the possibility that
392 TYPE_PRECISION (t2) != TYPE_PRECISION (t1), so there's no
393 compelling reason to choose one or the other. */
394 return build_type_attribute_variant (t1, attributes);
398 /* T1 and T2 are arithmetic or enumeration types. Return the type
399 that will result from the "usual arithmetic conversions" on T1 and
400 T2 as described in [expr]. */
402 tree
403 type_after_usual_arithmetic_conversions (tree t1, tree t2)
405 gcc_assert (ARITHMETIC_TYPE_P (t1)
406 || TREE_CODE (t1) == VECTOR_TYPE
407 || UNSCOPED_ENUM_P (t1));
408 gcc_assert (ARITHMETIC_TYPE_P (t2)
409 || TREE_CODE (t2) == VECTOR_TYPE
410 || UNSCOPED_ENUM_P (t2));
412 /* Perform the integral promotions. We do not promote real types here. */
413 if (INTEGRAL_OR_ENUMERATION_TYPE_P (t1)
414 && INTEGRAL_OR_ENUMERATION_TYPE_P (t2))
416 t1 = type_promotes_to (t1);
417 t2 = type_promotes_to (t2);
420 return cp_common_type (t1, t2);
423 /* Subroutine of composite_pointer_type to implement the recursive
424 case. See that function for documentation of the parameters. */
426 static tree
427 composite_pointer_type_r (tree t1, tree t2,
428 composite_pointer_operation operation,
429 tsubst_flags_t complain)
431 tree pointee1;
432 tree pointee2;
433 tree result_type;
434 tree attributes;
436 /* Determine the types pointed to by T1 and T2. */
437 if (TREE_CODE (t1) == POINTER_TYPE)
439 pointee1 = TREE_TYPE (t1);
440 pointee2 = TREE_TYPE (t2);
442 else
444 pointee1 = TYPE_PTRMEM_POINTED_TO_TYPE (t1);
445 pointee2 = TYPE_PTRMEM_POINTED_TO_TYPE (t2);
448 /* [expr.rel]
450 Otherwise, the composite pointer type is a pointer type
451 similar (_conv.qual_) to the type of one of the operands,
452 with a cv-qualification signature (_conv.qual_) that is the
453 union of the cv-qualification signatures of the operand
454 types. */
455 if (same_type_ignoring_top_level_qualifiers_p (pointee1, pointee2))
456 result_type = pointee1;
457 else if ((TREE_CODE (pointee1) == POINTER_TYPE
458 && TREE_CODE (pointee2) == POINTER_TYPE)
459 || (TYPE_PTR_TO_MEMBER_P (pointee1)
460 && TYPE_PTR_TO_MEMBER_P (pointee2)))
461 result_type = composite_pointer_type_r (pointee1, pointee2, operation,
462 complain);
463 else
465 if (complain & tf_error)
467 switch (operation)
469 case CPO_COMPARISON:
470 permerror (input_location, "comparison between "
471 "distinct pointer types %qT and %qT lacks a cast",
472 t1, t2);
473 break;
474 case CPO_CONVERSION:
475 permerror (input_location, "conversion between "
476 "distinct pointer types %qT and %qT lacks a cast",
477 t1, t2);
478 break;
479 case CPO_CONDITIONAL_EXPR:
480 permerror (input_location, "conditional expression between "
481 "distinct pointer types %qT and %qT lacks a cast",
482 t1, t2);
483 break;
484 default:
485 gcc_unreachable ();
488 result_type = void_type_node;
490 result_type = cp_build_qualified_type (result_type,
491 (cp_type_quals (pointee1)
492 | cp_type_quals (pointee2)));
493 /* If the original types were pointers to members, so is the
494 result. */
495 if (TYPE_PTR_TO_MEMBER_P (t1))
497 if (!same_type_p (TYPE_PTRMEM_CLASS_TYPE (t1),
498 TYPE_PTRMEM_CLASS_TYPE (t2))
499 && (complain & tf_error))
501 switch (operation)
503 case CPO_COMPARISON:
504 permerror (input_location, "comparison between "
505 "distinct pointer types %qT and %qT lacks a cast",
506 t1, t2);
507 break;
508 case CPO_CONVERSION:
509 permerror (input_location, "conversion between "
510 "distinct pointer types %qT and %qT lacks a cast",
511 t1, t2);
512 break;
513 case CPO_CONDITIONAL_EXPR:
514 permerror (input_location, "conditional expression between "
515 "distinct pointer types %qT and %qT lacks a cast",
516 t1, t2);
517 break;
518 default:
519 gcc_unreachable ();
522 result_type = build_ptrmem_type (TYPE_PTRMEM_CLASS_TYPE (t1),
523 result_type);
525 else
526 result_type = build_pointer_type (result_type);
528 /* Merge the attributes. */
529 attributes = (*targetm.merge_type_attributes) (t1, t2);
530 return build_type_attribute_variant (result_type, attributes);
533 /* Return the composite pointer type (see [expr.rel]) for T1 and T2.
534 ARG1 and ARG2 are the values with those types. The OPERATION is to
535 describe the operation between the pointer types,
536 in case an error occurs.
538 This routine also implements the computation of a common type for
539 pointers-to-members as per [expr.eq]. */
541 tree
542 composite_pointer_type (tree t1, tree t2, tree arg1, tree arg2,
543 composite_pointer_operation operation,
544 tsubst_flags_t complain)
546 tree class1;
547 tree class2;
549 /* [expr.rel]
551 If one operand is a null pointer constant, the composite pointer
552 type is the type of the other operand. */
553 if (null_ptr_cst_p (arg1))
554 return t2;
555 if (null_ptr_cst_p (arg2))
556 return t1;
558 /* We have:
560 [expr.rel]
562 If one of the operands has type "pointer to cv1 void*", then
563 the other has type "pointer to cv2T", and the composite pointer
564 type is "pointer to cv12 void", where cv12 is the union of cv1
565 and cv2.
567 If either type is a pointer to void, make sure it is T1. */
568 if (TREE_CODE (t2) == POINTER_TYPE && VOID_TYPE_P (TREE_TYPE (t2)))
570 tree t;
571 t = t1;
572 t1 = t2;
573 t2 = t;
576 /* Now, if T1 is a pointer to void, merge the qualifiers. */
577 if (TREE_CODE (t1) == POINTER_TYPE && VOID_TYPE_P (TREE_TYPE (t1)))
579 tree attributes;
580 tree result_type;
582 if (TYPE_PTRFN_P (t2) && (complain & tf_error))
584 switch (operation)
586 case CPO_COMPARISON:
587 pedwarn (input_location, OPT_pedantic,
588 "ISO C++ forbids comparison between "
589 "pointer of type %<void *%> and pointer-to-function");
590 break;
591 case CPO_CONVERSION:
592 pedwarn (input_location, OPT_pedantic,
593 "ISO C++ forbids conversion between "
594 "pointer of type %<void *%> and pointer-to-function");
595 break;
596 case CPO_CONDITIONAL_EXPR:
597 pedwarn (input_location, OPT_pedantic,
598 "ISO C++ forbids conditional expression between "
599 "pointer of type %<void *%> and pointer-to-function");
600 break;
601 default:
602 gcc_unreachable ();
605 result_type
606 = cp_build_qualified_type (void_type_node,
607 (cp_type_quals (TREE_TYPE (t1))
608 | cp_type_quals (TREE_TYPE (t2))));
609 result_type = build_pointer_type (result_type);
610 /* Merge the attributes. */
611 attributes = (*targetm.merge_type_attributes) (t1, t2);
612 return build_type_attribute_variant (result_type, attributes);
615 if (c_dialect_objc () && TREE_CODE (t1) == POINTER_TYPE
616 && TREE_CODE (t2) == POINTER_TYPE)
618 if (objc_compare_types (t1, t2, -3, NULL_TREE))
619 return t1;
622 /* [expr.eq] permits the application of a pointer conversion to
623 bring the pointers to a common type. */
624 if (TREE_CODE (t1) == POINTER_TYPE && TREE_CODE (t2) == POINTER_TYPE
625 && CLASS_TYPE_P (TREE_TYPE (t1))
626 && CLASS_TYPE_P (TREE_TYPE (t2))
627 && !same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (t1),
628 TREE_TYPE (t2)))
630 class1 = TREE_TYPE (t1);
631 class2 = TREE_TYPE (t2);
633 if (DERIVED_FROM_P (class1, class2))
634 t2 = (build_pointer_type
635 (cp_build_qualified_type (class1, TYPE_QUALS (class2))));
636 else if (DERIVED_FROM_P (class2, class1))
637 t1 = (build_pointer_type
638 (cp_build_qualified_type (class2, TYPE_QUALS (class1))));
639 else
641 if (complain & tf_error)
642 switch (operation)
644 case CPO_COMPARISON:
645 error ("comparison between distinct "
646 "pointer types %qT and %qT lacks a cast", t1, t2);
647 break;
648 case CPO_CONVERSION:
649 error ("conversion between distinct "
650 "pointer types %qT and %qT lacks a cast", t1, t2);
651 break;
652 case CPO_CONDITIONAL_EXPR:
653 error ("conditional expression between distinct "
654 "pointer types %qT and %qT lacks a cast", t1, t2);
655 break;
656 default:
657 gcc_unreachable ();
659 return error_mark_node;
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_PTR_TO_MEMBER_P (t1)
665 && !same_type_p (TYPE_PTRMEM_CLASS_TYPE (t1),
666 TYPE_PTRMEM_CLASS_TYPE (t2)))
668 class1 = TYPE_PTRMEM_CLASS_TYPE (t1);
669 class2 = TYPE_PTRMEM_CLASS_TYPE (t2);
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
677 if (complain & tf_error)
678 switch (operation)
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 ();
698 return error_mark_node;
702 return composite_pointer_type_r (t1, t2, operation, complain);
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.
709 This just combines attributes and default arguments; any other
710 differences would cause the two types to compare unalike. */
712 tree
713 merge_types (tree t1, tree t2)
715 enum tree_code code1;
716 enum tree_code code2;
717 tree attributes;
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;
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;
731 /* Merge the attributes. */
732 attributes = (*targetm.merge_type_attributes) (t1, t2);
734 if (TYPE_PTRMEMFUNC_P (t1))
735 t1 = TYPE_PTRMEMFUNC_FN_TYPE (t1);
736 if (TYPE_PTRMEMFUNC_P (t2))
737 t2 = TYPE_PTRMEMFUNC_FN_TYPE (t2);
739 code1 = TREE_CODE (t1);
740 code2 = TREE_CODE (t2);
741 if (code1 != code2)
743 gcc_assert (code1 == TYPENAME_TYPE || code2 == TYPENAME_TYPE);
744 if (code1 == TYPENAME_TYPE)
746 t1 = resolve_typename_type (t1, /*only_current_p=*/true);
747 code1 = TREE_CODE (t1);
749 else
751 t2 = resolve_typename_type (t2, /*only_current_p=*/true);
752 code2 = TREE_CODE (t2);
756 switch (code1)
758 case POINTER_TYPE:
759 case REFERENCE_TYPE:
760 /* For two pointers, do this recursively on the target type. */
762 tree target = merge_types (TREE_TYPE (t1), TREE_TYPE (t2));
763 int quals = cp_type_quals (t1);
765 if (code1 == POINTER_TYPE)
766 t1 = build_pointer_type (target);
767 else
768 t1 = cp_build_reference_type (target, TYPE_REF_IS_RVALUE (t1));
769 t1 = build_type_attribute_variant (t1, attributes);
770 t1 = cp_build_qualified_type (t1, quals);
772 if (TREE_CODE (target) == METHOD_TYPE)
773 t1 = build_ptrmemfunc_type (t1);
775 return t1;
778 case OFFSET_TYPE:
780 int quals;
781 tree pointee;
782 quals = cp_type_quals (t1);
783 pointee = merge_types (TYPE_PTRMEM_POINTED_TO_TYPE (t1),
784 TYPE_PTRMEM_POINTED_TO_TYPE (t2));
785 t1 = build_ptrmem_type (TYPE_PTRMEM_CLASS_TYPE (t1),
786 pointee);
787 t1 = cp_build_qualified_type (t1, quals);
788 break;
791 case ARRAY_TYPE:
793 tree elt = merge_types (TREE_TYPE (t1), TREE_TYPE (t2));
794 /* Save space: see if the result is identical to one of the args. */
795 if (elt == TREE_TYPE (t1) && TYPE_DOMAIN (t1))
796 return build_type_attribute_variant (t1, attributes);
797 if (elt == TREE_TYPE (t2) && TYPE_DOMAIN (t2))
798 return build_type_attribute_variant (t2, attributes);
799 /* Merge the element types, and have a size if either arg has one. */
800 t1 = build_cplus_array_type
801 (elt, TYPE_DOMAIN (TYPE_DOMAIN (t1) ? t1 : t2));
802 break;
805 case FUNCTION_TYPE:
806 /* Function types: prefer the one that specified arg types.
807 If both do, merge the arg types. Also merge the return types. */
809 tree valtype = merge_types (TREE_TYPE (t1), TREE_TYPE (t2));
810 tree p1 = TYPE_ARG_TYPES (t1);
811 tree p2 = TYPE_ARG_TYPES (t2);
812 tree rval, raises;
814 /* Save space: see if the result is identical to one of the args. */
815 if (valtype == TREE_TYPE (t1) && ! p2)
816 return cp_build_type_attribute_variant (t1, attributes);
817 if (valtype == TREE_TYPE (t2) && ! p1)
818 return cp_build_type_attribute_variant (t2, attributes);
820 /* Simple way if one arg fails to specify argument types. */
821 if (p1 == NULL_TREE || TREE_VALUE (p1) == void_type_node)
823 rval = build_function_type (valtype, p2);
824 if ((raises = TYPE_RAISES_EXCEPTIONS (t2)))
825 rval = build_exception_variant (rval, raises);
826 return cp_build_type_attribute_variant (rval, attributes);
828 raises = TYPE_RAISES_EXCEPTIONS (t1);
829 if (p2 == NULL_TREE || TREE_VALUE (p2) == void_type_node)
831 rval = build_function_type (valtype, p1);
832 if (raises)
833 rval = build_exception_variant (rval, raises);
834 return cp_build_type_attribute_variant (rval, attributes);
837 rval = build_function_type (valtype, commonparms (p1, p2));
838 t1 = build_exception_variant (rval, raises);
839 break;
842 case METHOD_TYPE:
844 /* Get this value the long way, since TYPE_METHOD_BASETYPE
845 is just the main variant of this. */
846 tree basetype = TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (t2)));
847 tree raises = TYPE_RAISES_EXCEPTIONS (t1);
848 tree t3;
850 /* If this was a member function type, get back to the
851 original type of type member function (i.e., without
852 the class instance variable up front. */
853 t1 = build_function_type (TREE_TYPE (t1),
854 TREE_CHAIN (TYPE_ARG_TYPES (t1)));
855 t2 = build_function_type (TREE_TYPE (t2),
856 TREE_CHAIN (TYPE_ARG_TYPES (t2)));
857 t3 = merge_types (t1, t2);
858 t3 = build_method_type_directly (basetype, TREE_TYPE (t3),
859 TYPE_ARG_TYPES (t3));
860 t1 = build_exception_variant (t3, raises);
861 break;
864 case TYPENAME_TYPE:
865 /* There is no need to merge attributes into a TYPENAME_TYPE.
866 When the type is instantiated it will have whatever
867 attributes result from the instantiation. */
868 return t1;
870 default:;
873 if (attribute_list_equal (TYPE_ATTRIBUTES (t1), attributes))
874 return t1;
875 else if (attribute_list_equal (TYPE_ATTRIBUTES (t2), attributes))
876 return t2;
877 else
878 return cp_build_type_attribute_variant (t1, attributes);
881 /* Wrapper around cp_common_type that is used by c-common.c and other
882 front end optimizations that remove promotions.
884 Return the common type for two arithmetic types T1 and T2 under the
885 usual arithmetic conversions. The default conversions have already
886 been applied, and enumerated types converted to their compatible
887 integer types. */
889 tree
890 common_type (tree t1, tree t2)
892 /* If one type is nonsense, use the other */
893 if (t1 == error_mark_node)
894 return t2;
895 if (t2 == error_mark_node)
896 return t1;
898 return cp_common_type (t1, t2);
901 /* Return the common type of two pointer types T1 and T2. This is the
902 type for the result of most arithmetic operations if the operands
903 have the given two types.
905 We assume that comp_target_types has already been done and returned
906 nonzero; if that isn't so, this may crash. */
908 tree
909 common_pointer_type (tree t1, tree t2)
911 gcc_assert ((TYPE_PTR_P (t1) && TYPE_PTR_P (t2))
912 || (TYPE_PTRMEM_P (t1) && TYPE_PTRMEM_P (t2))
913 || (TYPE_PTRMEMFUNC_P (t1) && TYPE_PTRMEMFUNC_P (t2)));
915 return composite_pointer_type (t1, t2, error_mark_node, error_mark_node,
916 CPO_CONVERSION, tf_warning_or_error);
919 /* Compare two exception specifier types for exactness or subsetness, if
920 allowed. Returns false for mismatch, true for match (same, or
921 derived and !exact).
923 [except.spec] "If a class X ... objects of class X or any class publicly
924 and unambiguously derived from X. Similarly, if a pointer type Y * ...
925 exceptions of type Y * or that are pointers to any type publicly and
926 unambiguously derived from Y. Otherwise a function only allows exceptions
927 that have the same type ..."
928 This does not mention cv qualifiers and is different to what throw
929 [except.throw] and catch [except.catch] will do. They will ignore the
930 top level cv qualifiers, and allow qualifiers in the pointer to class
931 example.
933 We implement the letter of the standard. */
935 static bool
936 comp_except_types (tree a, tree b, bool exact)
938 if (same_type_p (a, b))
939 return true;
940 else if (!exact)
942 if (cp_type_quals (a) || cp_type_quals (b))
943 return false;
945 if (TREE_CODE (a) == POINTER_TYPE
946 && TREE_CODE (b) == POINTER_TYPE)
948 a = TREE_TYPE (a);
949 b = TREE_TYPE (b);
950 if (cp_type_quals (a) || cp_type_quals (b))
951 return false;
954 if (TREE_CODE (a) != RECORD_TYPE
955 || TREE_CODE (b) != RECORD_TYPE)
956 return false;
958 if (PUBLICLY_UNIQUELY_DERIVED_P (a, b))
959 return true;
961 return false;
964 /* Return true if TYPE1 and TYPE2 are equivalent exception specifiers.
965 If EXACT is false, T2 can be stricter than T1 (according to 15.4/7),
966 otherwise it must be exact. Exception lists are unordered, but
967 we've already filtered out duplicates. Most lists will be in order,
968 we should try to make use of that. */
970 bool
971 comp_except_specs (const_tree t1, const_tree t2, bool exact)
973 const_tree probe;
974 const_tree base;
975 int length = 0;
977 if (t1 == t2)
978 return true;
980 if (t1 == NULL_TREE) /* T1 is ... */
981 return t2 == NULL_TREE || !exact;
982 if (!TREE_VALUE (t1)) /* t1 is EMPTY */
983 return t2 != NULL_TREE && !TREE_VALUE (t2);
984 if (t2 == NULL_TREE) /* T2 is ... */
985 return false;
986 if (TREE_VALUE (t1) && !TREE_VALUE (t2)) /* T2 is EMPTY, T1 is not */
987 return !exact;
989 /* Neither set is ... or EMPTY, make sure each part of T2 is in T1.
990 Count how many we find, to determine exactness. For exact matching and
991 ordered T1, T2, this is an O(n) operation, otherwise its worst case is
992 O(nm). */
993 for (base = t1; t2 != NULL_TREE; t2 = TREE_CHAIN (t2))
995 for (probe = base; probe != NULL_TREE; probe = TREE_CHAIN (probe))
997 tree a = TREE_VALUE (probe);
998 tree b = TREE_VALUE (t2);
1000 if (comp_except_types (a, b, exact))
1002 if (probe == base && exact)
1003 base = TREE_CHAIN (probe);
1004 length++;
1005 break;
1008 if (probe == NULL_TREE)
1009 return false;
1011 return !exact || base == NULL_TREE || length == list_length (t1);
1014 /* Compare the array types T1 and T2. ALLOW_REDECLARATION is true if
1015 [] can match [size]. */
1017 static bool
1018 comp_array_types (const_tree t1, const_tree t2, bool allow_redeclaration)
1020 tree d1;
1021 tree d2;
1022 tree max1, max2;
1024 if (t1 == t2)
1025 return true;
1027 /* The type of the array elements must be the same. */
1028 if (!same_type_p (TREE_TYPE (t1), TREE_TYPE (t2)))
1029 return false;
1031 d1 = TYPE_DOMAIN (t1);
1032 d2 = TYPE_DOMAIN (t2);
1034 if (d1 == d2)
1035 return true;
1037 /* If one of the arrays is dimensionless, and the other has a
1038 dimension, they are of different types. However, it is valid to
1039 write:
1041 extern int a[];
1042 int a[3];
1044 by [basic.link]:
1046 declarations for an array object can specify
1047 array types that differ by the presence or absence of a major
1048 array bound (_dcl.array_). */
1049 if (!d1 || !d2)
1050 return allow_redeclaration;
1052 /* Check that the dimensions are the same. */
1054 if (!cp_tree_equal (TYPE_MIN_VALUE (d1), TYPE_MIN_VALUE (d2)))
1055 return false;
1056 max1 = TYPE_MAX_VALUE (d1);
1057 max2 = TYPE_MAX_VALUE (d2);
1058 if (processing_template_decl && !abi_version_at_least (2)
1059 && !value_dependent_expression_p (max1)
1060 && !value_dependent_expression_p (max2))
1062 /* With abi-1 we do not fold non-dependent array bounds, (and
1063 consequently mangle them incorrectly). We must therefore
1064 fold them here, to verify the domains have the same
1065 value. */
1066 max1 = fold (max1);
1067 max2 = fold (max2);
1070 if (!cp_tree_equal (max1, max2))
1071 return false;
1073 return true;
1076 /* Compare the relative position of T1 and T2 into their respective
1077 template parameter list.
1078 T1 and T2 must be template parameter types.
1079 Return TRUE if T1 and T2 have the same position, FALSE otherwise. */
1081 static bool
1082 comp_template_parms_position (tree t1, tree t2)
1084 gcc_assert (t1 && t2
1085 && TREE_CODE (t1) == TREE_CODE (t2)
1086 && (TREE_CODE (t1) == BOUND_TEMPLATE_TEMPLATE_PARM
1087 || TREE_CODE (t1) == TEMPLATE_TEMPLATE_PARM
1088 || TREE_CODE (t1) == TEMPLATE_TYPE_PARM));
1090 if (TEMPLATE_TYPE_IDX (t1) != TEMPLATE_TYPE_IDX (t2)
1091 || TEMPLATE_TYPE_LEVEL (t1) != TEMPLATE_TYPE_LEVEL (t2)
1092 || (TEMPLATE_TYPE_PARAMETER_PACK (t1)
1093 != TEMPLATE_TYPE_PARAMETER_PACK (t2)))
1094 return false;
1096 return true;
1099 /* Subroutine of structural_comptypes.
1100 Compare the template type parameters T1 and T2.
1101 Return TRUE if we are sure they can't be equal, FALSE otherwise. */
1103 static bool
1104 incompatible_template_type_parms_p (tree t1, tree t2)
1106 tree decl1, tparms1 = NULL_TREE,
1107 decl2, tparms2 = NULL_TREE;
1109 gcc_assert (t1 && TREE_CODE (t1) == TEMPLATE_TYPE_PARM
1110 && t2 && TREE_CODE (t2) == TEMPLATE_TYPE_PARM);
1112 /* If T1 and T2 don't have the same relative position in their
1113 template parameters set, they can't be equal. */
1114 if (!comp_template_parms_position (t1, t2))
1115 return true;
1117 if (!typedef_variant_p (t1) && !typedef_variant_p (t2))
1118 /* If neither T1 nor T2 is a typedef we cannot know more
1119 about their incompatibility than what comp_template_parms_position
1120 told us above. If we try to keep going nonetheless, the call to
1121 comp_template_parms at the end of this function might lead to an
1122 infinite recursion. */
1123 return false;
1125 decl1 = TYPE_NAME (t1);
1126 decl2 = TYPE_NAME (t2);
1127 if (decl1 == NULL_TREE || decl2 == NULL_TREE || decl1 == decl2)
1128 return false ;
1130 /* So if we reach this point, it means either T1 or T2 is a typedef variant.
1131 Let's compare their template parameters. */
1133 /* If T1 is not a typedef, there possibly is a delay between the
1134 creation of DECL1 and the setting of DECL_CONTEXT (DECL1) to its
1135 template decl so DECL_CONTEXT (DECL1) can be empty for
1136 a little while. */
1137 if (DECL_CONTEXT (decl1))
1139 if (TREE_CODE (DECL_CONTEXT (decl1)) == TEMPLATE_DECL)
1140 tparms1 = DECL_TEMPLATE_PARMS (DECL_CONTEXT (decl1));
1141 else
1142 /* T1 is a typedef variant type. Get the parms of its context. */
1143 tparms1 =
1144 DECL_TEMPLATE_PARMS (TI_TEMPLATE
1145 (get_template_info (DECL_CONTEXT (decl1))));
1148 /* Do the same thing for DECL2. */
1149 if (DECL_CONTEXT (decl2))
1151 if (TREE_CODE (DECL_CONTEXT (decl2)) == TEMPLATE_DECL)
1152 tparms2 = DECL_TEMPLATE_PARMS (DECL_CONTEXT (decl2));
1153 else
1154 tparms2 =
1155 DECL_TEMPLATE_PARMS (TI_TEMPLATE
1156 (get_template_info (DECL_CONTEXT (decl2))));
1159 if (tparms1 == NULL_TREE
1160 || tparms2 == NULL_TREE
1161 || tparms1 == tparms2)
1162 return false;
1164 /* And now compare the mighty template parms! */
1165 return !comp_template_parms (tparms1, tparms2);
1168 /* Subroutine in comptypes. */
1170 static bool
1171 structural_comptypes (tree t1, tree t2, int strict)
1173 if (t1 == t2)
1174 return true;
1176 /* Suppress errors caused by previously reported errors. */
1177 if (t1 == error_mark_node || t2 == error_mark_node)
1178 return false;
1180 gcc_assert (TYPE_P (t1) && TYPE_P (t2));
1182 /* TYPENAME_TYPEs should be resolved if the qualifying scope is the
1183 current instantiation. */
1184 if (TREE_CODE (t1) == TYPENAME_TYPE)
1185 t1 = resolve_typename_type (t1, /*only_current_p=*/true);
1187 if (TREE_CODE (t2) == TYPENAME_TYPE)
1188 t2 = resolve_typename_type (t2, /*only_current_p=*/true);
1190 if (TYPE_PTRMEMFUNC_P (t1))
1191 t1 = TYPE_PTRMEMFUNC_FN_TYPE (t1);
1192 if (TYPE_PTRMEMFUNC_P (t2))
1193 t2 = TYPE_PTRMEMFUNC_FN_TYPE (t2);
1195 /* Different classes of types can't be compatible. */
1196 if (TREE_CODE (t1) != TREE_CODE (t2))
1197 return false;
1199 /* Qualifiers must match. For array types, we will check when we
1200 recur on the array element types. */
1201 if (TREE_CODE (t1) != ARRAY_TYPE
1202 && TYPE_QUALS (t1) != TYPE_QUALS (t2))
1203 return false;
1204 if (TYPE_FOR_JAVA (t1) != TYPE_FOR_JAVA (t2))
1205 return false;
1207 /* Allow for two different type nodes which have essentially the same
1208 definition. Note that we already checked for equality of the type
1209 qualifiers (just above). */
1211 if (TREE_CODE (t1) != ARRAY_TYPE
1212 && TYPE_MAIN_VARIANT (t1) == TYPE_MAIN_VARIANT (t2))
1213 return true;
1215 /* Compare the types. Break out if they could be the same. */
1216 switch (TREE_CODE (t1))
1218 case VOID_TYPE:
1219 case BOOLEAN_TYPE:
1220 /* All void and bool types are the same. */
1221 break;
1223 case INTEGER_TYPE:
1224 case FIXED_POINT_TYPE:
1225 case REAL_TYPE:
1226 /* With these nodes, we can't determine type equivalence by
1227 looking at what is stored in the nodes themselves, because
1228 two nodes might have different TYPE_MAIN_VARIANTs but still
1229 represent the same type. For example, wchar_t and int could
1230 have the same properties (TYPE_PRECISION, TYPE_MIN_VALUE,
1231 TYPE_MAX_VALUE, etc.), but have different TYPE_MAIN_VARIANTs
1232 and are distinct types. On the other hand, int and the
1233 following typedef
1235 typedef int INT __attribute((may_alias));
1237 have identical properties, different TYPE_MAIN_VARIANTs, but
1238 represent the same type. The canonical type system keeps
1239 track of equivalence in this case, so we fall back on it. */
1240 return TYPE_CANONICAL (t1) == TYPE_CANONICAL (t2);
1242 case TEMPLATE_TEMPLATE_PARM:
1243 case BOUND_TEMPLATE_TEMPLATE_PARM:
1244 if (!comp_template_parms_position (t1, t2))
1245 return false;
1246 if (!comp_template_parms
1247 (DECL_TEMPLATE_PARMS (TEMPLATE_TEMPLATE_PARM_TEMPLATE_DECL (t1)),
1248 DECL_TEMPLATE_PARMS (TEMPLATE_TEMPLATE_PARM_TEMPLATE_DECL (t2))))
1249 return false;
1250 if (TREE_CODE (t1) == TEMPLATE_TEMPLATE_PARM)
1251 break;
1252 /* Don't check inheritance. */
1253 strict = COMPARE_STRICT;
1254 /* Fall through. */
1256 case RECORD_TYPE:
1257 case UNION_TYPE:
1258 if (TYPE_TEMPLATE_INFO (t1) && TYPE_TEMPLATE_INFO (t2)
1259 && (TYPE_TI_TEMPLATE (t1) == TYPE_TI_TEMPLATE (t2)
1260 || TREE_CODE (t1) == BOUND_TEMPLATE_TEMPLATE_PARM)
1261 && comp_template_args (TYPE_TI_ARGS (t1), TYPE_TI_ARGS (t2)))
1262 break;
1264 if ((strict & COMPARE_BASE) && DERIVED_FROM_P (t1, t2))
1265 break;
1266 else if ((strict & COMPARE_DERIVED) && DERIVED_FROM_P (t2, t1))
1267 break;
1269 return false;
1271 case OFFSET_TYPE:
1272 if (!comptypes (TYPE_OFFSET_BASETYPE (t1), TYPE_OFFSET_BASETYPE (t2),
1273 strict & ~COMPARE_REDECLARATION))
1274 return false;
1275 if (!same_type_p (TREE_TYPE (t1), TREE_TYPE (t2)))
1276 return false;
1277 break;
1279 case REFERENCE_TYPE:
1280 if (TYPE_REF_IS_RVALUE (t1) != TYPE_REF_IS_RVALUE (t2))
1281 return false;
1282 /* fall through to checks for pointer types */
1284 case POINTER_TYPE:
1285 if (TYPE_MODE (t1) != TYPE_MODE (t2)
1286 || TYPE_REF_CAN_ALIAS_ALL (t1) != TYPE_REF_CAN_ALIAS_ALL (t2)
1287 || !same_type_p (TREE_TYPE (t1), TREE_TYPE (t2)))
1288 return false;
1289 break;
1291 case METHOD_TYPE:
1292 case FUNCTION_TYPE:
1293 if (!same_type_p (TREE_TYPE (t1), TREE_TYPE (t2)))
1294 return false;
1295 if (!compparms (TYPE_ARG_TYPES (t1), TYPE_ARG_TYPES (t2)))
1296 return false;
1297 break;
1299 case ARRAY_TYPE:
1300 /* Target types must match incl. qualifiers. */
1301 if (!comp_array_types (t1, t2, !!(strict & COMPARE_REDECLARATION)))
1302 return false;
1303 break;
1305 case TEMPLATE_TYPE_PARM:
1306 if (incompatible_template_type_parms_p (t1, t2))
1307 return false;
1308 break;
1310 case TYPENAME_TYPE:
1311 if (!cp_tree_equal (TYPENAME_TYPE_FULLNAME (t1),
1312 TYPENAME_TYPE_FULLNAME (t2)))
1313 return false;
1314 if (!same_type_p (TYPE_CONTEXT (t1), TYPE_CONTEXT (t2)))
1315 return false;
1316 break;
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;
1325 case COMPLEX_TYPE:
1326 if (!same_type_p (TREE_TYPE (t1), TREE_TYPE (t2)))
1327 return false;
1328 break;
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;
1336 case TYPE_PACK_EXPANSION:
1337 return same_type_p (PACK_EXPANSION_PATTERN (t1),
1338 PACK_EXPANSION_PATTERN (t2));
1340 case DECLTYPE_TYPE:
1341 if (DECLTYPE_TYPE_ID_EXPR_OR_MEMBER_ACCESS_P (t1)
1342 != DECLTYPE_TYPE_ID_EXPR_OR_MEMBER_ACCESS_P (t2)
1343 || (DECLTYPE_FOR_LAMBDA_CAPTURE (t1)
1344 != DECLTYPE_FOR_LAMBDA_CAPTURE (t2))
1345 || (DECLTYPE_FOR_LAMBDA_RETURN (t1)
1346 != DECLTYPE_FOR_LAMBDA_RETURN (t2))
1347 || !cp_tree_equal (DECLTYPE_TYPE_EXPR (t1),
1348 DECLTYPE_TYPE_EXPR (t2)))
1349 return false;
1350 break;
1352 default:
1353 return false;
1356 /* If we get here, we know that from a target independent POV the
1357 types are the same. Make sure the target attributes are also
1358 the same. */
1359 return targetm.comp_type_attributes (t1, t2);
1362 /* Return true if T1 and T2 are related as allowed by STRICT. STRICT
1363 is a bitwise-or of the COMPARE_* flags. */
1365 bool
1366 comptypes (tree t1, tree t2, int strict)
1368 if (strict == COMPARE_STRICT)
1370 if (t1 == t2)
1371 return true;
1373 if (t1 == error_mark_node || t2 == error_mark_node)
1374 return false;
1376 if (TYPE_STRUCTURAL_EQUALITY_P (t1) || TYPE_STRUCTURAL_EQUALITY_P (t2))
1377 /* At least one of the types requires structural equality, so
1378 perform a deep check. */
1379 return structural_comptypes (t1, t2, strict);
1381 #ifdef ENABLE_CHECKING
1382 if (USE_CANONICAL_TYPES)
1384 bool result = structural_comptypes (t1, t2, strict);
1386 if (result && TYPE_CANONICAL (t1) != TYPE_CANONICAL (t2))
1387 /* The two types are structurally equivalent, but their
1388 canonical types were different. This is a failure of the
1389 canonical type propagation code.*/
1390 internal_error
1391 ("canonical types differ for identical types %T and %T",
1392 t1, t2);
1393 else if (!result && TYPE_CANONICAL (t1) == TYPE_CANONICAL (t2))
1394 /* Two types are structurally different, but the canonical
1395 types are the same. This means we were over-eager in
1396 assigning canonical types. */
1397 internal_error
1398 ("same canonical type node for different types %T and %T",
1399 t1, t2);
1401 return result;
1403 #else
1404 if (USE_CANONICAL_TYPES)
1405 return TYPE_CANONICAL (t1) == TYPE_CANONICAL (t2);
1406 #endif
1407 else
1408 return structural_comptypes (t1, t2, strict);
1410 else if (strict == COMPARE_STRUCTURAL)
1411 return structural_comptypes (t1, t2, COMPARE_STRICT);
1412 else
1413 return structural_comptypes (t1, t2, strict);
1416 /* Returns 1 if TYPE1 is at least as qualified as TYPE2. */
1418 bool
1419 at_least_as_qualified_p (const_tree type1, const_tree type2)
1421 int q1 = cp_type_quals (type1);
1422 int q2 = cp_type_quals (type2);
1424 /* All qualifiers for TYPE2 must also appear in TYPE1. */
1425 return (q1 & q2) == q2;
1428 /* Returns 1 if TYPE1 is more cv-qualified than TYPE2, -1 if TYPE2 is
1429 more cv-qualified that TYPE1, and 0 otherwise. */
1432 comp_cv_qualification (const_tree type1, const_tree type2)
1434 int q1 = cp_type_quals (type1);
1435 int q2 = cp_type_quals (type2);
1437 if (q1 == q2)
1438 return 0;
1440 if ((q1 & q2) == q2)
1441 return 1;
1442 else if ((q1 & q2) == q1)
1443 return -1;
1445 return 0;
1448 /* Returns 1 if the cv-qualification signature of TYPE1 is a proper
1449 subset of the cv-qualification signature of TYPE2, and the types
1450 are similar. Returns -1 if the other way 'round, and 0 otherwise. */
1453 comp_cv_qual_signature (tree type1, tree type2)
1455 if (comp_ptr_ttypes_real (type2, type1, -1))
1456 return 1;
1457 else if (comp_ptr_ttypes_real (type1, type2, -1))
1458 return -1;
1459 else
1460 return 0;
1463 /* Subroutines of `comptypes'. */
1465 /* Return true if two parameter type lists PARMS1 and PARMS2 are
1466 equivalent in the sense that functions with those parameter types
1467 can have equivalent types. The two lists must be equivalent,
1468 element by element. */
1470 bool
1471 compparms (const_tree parms1, const_tree parms2)
1473 const_tree t1, t2;
1475 /* An unspecified parmlist matches any specified parmlist
1476 whose argument types don't need default promotions. */
1478 for (t1 = parms1, t2 = parms2;
1479 t1 || t2;
1480 t1 = TREE_CHAIN (t1), t2 = TREE_CHAIN (t2))
1482 /* If one parmlist is shorter than the other,
1483 they fail to match. */
1484 if (!t1 || !t2)
1485 return false;
1486 if (!same_type_p (TREE_VALUE (t1), TREE_VALUE (t2)))
1487 return false;
1489 return true;
1493 /* Process a sizeof or alignof expression where the operand is a
1494 type. */
1496 tree
1497 cxx_sizeof_or_alignof_type (tree type, enum tree_code op, bool complain)
1499 tree value;
1500 bool dependent_p;
1502 gcc_assert (op == SIZEOF_EXPR || op == ALIGNOF_EXPR);
1503 if (type == error_mark_node)
1504 return error_mark_node;
1506 type = non_reference (type);
1507 if (TREE_CODE (type) == METHOD_TYPE)
1509 if (complain)
1510 pedwarn (input_location, pedantic ? OPT_pedantic : OPT_Wpointer_arith,
1511 "invalid application of %qs to a member function",
1512 operator_name_info[(int) op].name);
1513 value = size_one_node;
1516 dependent_p = dependent_type_p (type);
1517 if (!dependent_p)
1518 complete_type (type);
1519 if (dependent_p
1520 /* VLA types will have a non-constant size. In the body of an
1521 uninstantiated template, we don't need to try to compute the
1522 value, because the sizeof expression is not an integral
1523 constant expression in that case. And, if we do try to
1524 compute the value, we'll likely end up with SAVE_EXPRs, which
1525 the template substitution machinery does not expect to see. */
1526 || (processing_template_decl
1527 && COMPLETE_TYPE_P (type)
1528 && TREE_CODE (TYPE_SIZE (type)) != INTEGER_CST))
1530 value = build_min (op, size_type_node, type);
1531 TREE_READONLY (value) = 1;
1532 return value;
1535 return c_sizeof_or_alignof_type (input_location, complete_type (type),
1536 op == SIZEOF_EXPR,
1537 complain);
1540 /* Return the size of the type, without producing any warnings for
1541 types whose size cannot be taken. This routine should be used only
1542 in some other routine that has already produced a diagnostic about
1543 using the size of such a type. */
1544 tree
1545 cxx_sizeof_nowarn (tree type)
1547 if (TREE_CODE (type) == FUNCTION_TYPE
1548 || TREE_CODE (type) == VOID_TYPE
1549 || TREE_CODE (type) == ERROR_MARK)
1550 return size_one_node;
1551 else if (!COMPLETE_TYPE_P (type))
1552 return size_zero_node;
1553 else
1554 return cxx_sizeof_or_alignof_type (type, SIZEOF_EXPR, false);
1557 /* Process a sizeof expression where the operand is an expression. */
1559 static tree
1560 cxx_sizeof_expr (tree e, tsubst_flags_t complain)
1562 if (e == error_mark_node)
1563 return error_mark_node;
1565 if (processing_template_decl)
1567 e = build_min (SIZEOF_EXPR, size_type_node, e);
1568 TREE_SIDE_EFFECTS (e) = 0;
1569 TREE_READONLY (e) = 1;
1571 return e;
1574 if (TREE_CODE (e) == COMPONENT_REF
1575 && TREE_CODE (TREE_OPERAND (e, 1)) == FIELD_DECL
1576 && DECL_C_BIT_FIELD (TREE_OPERAND (e, 1)))
1578 if (complain & tf_error)
1579 error ("invalid application of %<sizeof%> to a bit-field");
1580 else
1581 return error_mark_node;
1582 e = char_type_node;
1584 else if (is_overloaded_fn (e))
1586 if (complain & tf_error)
1587 permerror (input_location, "ISO C++ forbids applying %<sizeof%> to an expression of "
1588 "function type");
1589 else
1590 return error_mark_node;
1591 e = char_type_node;
1593 else if (type_unknown_p (e))
1595 if (complain & tf_error)
1596 cxx_incomplete_type_error (e, TREE_TYPE (e));
1597 else
1598 return error_mark_node;
1599 e = char_type_node;
1601 else
1602 e = TREE_TYPE (e);
1604 return cxx_sizeof_or_alignof_type (e, SIZEOF_EXPR, complain & tf_error);
1607 /* Implement the __alignof keyword: Return the minimum required
1608 alignment of E, measured in bytes. For VAR_DECL's and
1609 FIELD_DECL's return DECL_ALIGN (which can be set from an
1610 "aligned" __attribute__ specification). */
1612 static tree
1613 cxx_alignof_expr (tree e, tsubst_flags_t complain)
1615 tree t;
1617 if (e == error_mark_node)
1618 return error_mark_node;
1620 if (processing_template_decl)
1622 e = build_min (ALIGNOF_EXPR, size_type_node, e);
1623 TREE_SIDE_EFFECTS (e) = 0;
1624 TREE_READONLY (e) = 1;
1626 return e;
1629 if (TREE_CODE (e) == VAR_DECL)
1630 t = size_int (DECL_ALIGN_UNIT (e));
1631 else if (TREE_CODE (e) == COMPONENT_REF
1632 && TREE_CODE (TREE_OPERAND (e, 1)) == FIELD_DECL
1633 && DECL_C_BIT_FIELD (TREE_OPERAND (e, 1)))
1635 if (complain & tf_error)
1636 error ("invalid application of %<__alignof%> to a bit-field");
1637 else
1638 return error_mark_node;
1639 t = size_one_node;
1641 else if (TREE_CODE (e) == COMPONENT_REF
1642 && TREE_CODE (TREE_OPERAND (e, 1)) == FIELD_DECL)
1643 t = size_int (DECL_ALIGN_UNIT (TREE_OPERAND (e, 1)));
1644 else if (is_overloaded_fn (e))
1646 if (complain & tf_error)
1647 permerror (input_location, "ISO C++ forbids applying %<__alignof%> to an expression of "
1648 "function type");
1649 else
1650 return error_mark_node;
1651 if (TREE_CODE (e) == FUNCTION_DECL)
1652 t = size_int (DECL_ALIGN_UNIT (e));
1653 else
1654 t = size_one_node;
1656 else if (type_unknown_p (e))
1658 if (complain & tf_error)
1659 cxx_incomplete_type_error (e, TREE_TYPE (e));
1660 else
1661 return error_mark_node;
1662 t = size_one_node;
1664 else
1665 return cxx_sizeof_or_alignof_type (TREE_TYPE (e), ALIGNOF_EXPR,
1666 complain & tf_error);
1668 return fold_convert (size_type_node, t);
1671 /* Process a sizeof or alignof expression E with code OP where the operand
1672 is an expression. */
1674 tree
1675 cxx_sizeof_or_alignof_expr (tree e, enum tree_code op, bool complain)
1677 if (op == SIZEOF_EXPR)
1678 return cxx_sizeof_expr (e, complain? tf_warning_or_error : tf_none);
1679 else
1680 return cxx_alignof_expr (e, complain? tf_warning_or_error : tf_none);
1683 /* EXPR is being used in a context that is not a function call.
1684 Enforce:
1686 [expr.ref]
1688 The expression can be used only as the left-hand operand of a
1689 member function call.
1691 [expr.mptr.operator]
1693 If the result of .* or ->* is a function, then that result can be
1694 used only as the operand for the function call operator ().
1696 by issuing an error message if appropriate. Returns true iff EXPR
1697 violates these rules. */
1699 bool
1700 invalid_nonstatic_memfn_p (const_tree expr, tsubst_flags_t complain)
1702 if (expr && DECL_NONSTATIC_MEMBER_FUNCTION_P (expr))
1704 if (complain & tf_error)
1705 error ("invalid use of non-static member function");
1706 return true;
1708 return false;
1711 /* If EXP is a reference to a bitfield, and the type of EXP does not
1712 match the declared type of the bitfield, return the declared type
1713 of the bitfield. Otherwise, return NULL_TREE. */
1715 tree
1716 is_bitfield_expr_with_lowered_type (const_tree exp)
1718 switch (TREE_CODE (exp))
1720 case COND_EXPR:
1721 if (!is_bitfield_expr_with_lowered_type (TREE_OPERAND (exp, 1)
1722 ? TREE_OPERAND (exp, 1)
1723 : TREE_OPERAND (exp, 0)))
1724 return NULL_TREE;
1725 return is_bitfield_expr_with_lowered_type (TREE_OPERAND (exp, 2));
1727 case COMPOUND_EXPR:
1728 return is_bitfield_expr_with_lowered_type (TREE_OPERAND (exp, 1));
1730 case MODIFY_EXPR:
1731 case SAVE_EXPR:
1732 return is_bitfield_expr_with_lowered_type (TREE_OPERAND (exp, 0));
1734 case COMPONENT_REF:
1736 tree field;
1738 field = TREE_OPERAND (exp, 1);
1739 if (TREE_CODE (field) != FIELD_DECL || !DECL_BIT_FIELD_TYPE (field))
1740 return NULL_TREE;
1741 if (same_type_ignoring_top_level_qualifiers_p
1742 (TREE_TYPE (exp), DECL_BIT_FIELD_TYPE (field)))
1743 return NULL_TREE;
1744 return DECL_BIT_FIELD_TYPE (field);
1747 CASE_CONVERT:
1748 if (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_OPERAND (exp, 0)))
1749 == TYPE_MAIN_VARIANT (TREE_TYPE (exp)))
1750 return is_bitfield_expr_with_lowered_type (TREE_OPERAND (exp, 0));
1751 /* Fallthrough. */
1753 default:
1754 return NULL_TREE;
1758 /* Like is_bitfield_with_lowered_type, except that if EXP is not a
1759 bitfield with a lowered type, the type of EXP is returned, rather
1760 than NULL_TREE. */
1762 tree
1763 unlowered_expr_type (const_tree exp)
1765 tree type;
1767 type = is_bitfield_expr_with_lowered_type (exp);
1768 if (!type)
1769 type = TREE_TYPE (exp);
1771 return type;
1774 /* Perform the conversions in [expr] that apply when an lvalue appears
1775 in an rvalue context: the lvalue-to-rvalue, array-to-pointer, and
1776 function-to-pointer conversions. In addition, manifest constants
1777 are replaced by their values, and bitfield references are converted
1778 to their declared types.
1780 Although the returned value is being used as an rvalue, this
1781 function does not wrap the returned expression in a
1782 NON_LVALUE_EXPR; the caller is expected to be mindful of the fact
1783 that the return value is no longer an lvalue. */
1785 tree
1786 decay_conversion (tree exp)
1788 tree type;
1789 enum tree_code code;
1791 type = TREE_TYPE (exp);
1792 if (type == error_mark_node)
1793 return error_mark_node;
1795 exp = resolve_nondeduced_context (exp);
1796 if (type_unknown_p (exp))
1798 cxx_incomplete_type_error (exp, TREE_TYPE (exp));
1799 return error_mark_node;
1802 exp = decl_constant_value (exp);
1803 if (error_operand_p (exp))
1804 return error_mark_node;
1806 /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
1807 Leave such NOP_EXPRs, since RHS is being used in non-lvalue context. */
1808 code = TREE_CODE (type);
1809 if (code == VOID_TYPE)
1811 error ("void value not ignored as it ought to be");
1812 return error_mark_node;
1814 if (invalid_nonstatic_memfn_p (exp, tf_warning_or_error))
1815 return error_mark_node;
1816 if (code == FUNCTION_TYPE || is_overloaded_fn (exp))
1817 return cp_build_unary_op (ADDR_EXPR, exp, 0, tf_warning_or_error);
1818 if (code == ARRAY_TYPE)
1820 tree adr;
1821 tree ptrtype;
1823 if (TREE_CODE (exp) == INDIRECT_REF)
1824 return build_nop (build_pointer_type (TREE_TYPE (type)),
1825 TREE_OPERAND (exp, 0));
1827 if (TREE_CODE (exp) == COMPOUND_EXPR)
1829 tree op1 = decay_conversion (TREE_OPERAND (exp, 1));
1830 return build2 (COMPOUND_EXPR, TREE_TYPE (op1),
1831 TREE_OPERAND (exp, 0), op1);
1834 if (!lvalue_p (exp)
1835 && ! (TREE_CODE (exp) == CONSTRUCTOR && TREE_STATIC (exp)))
1837 error ("invalid use of non-lvalue array");
1838 return error_mark_node;
1841 ptrtype = build_pointer_type (TREE_TYPE (type));
1843 if (TREE_CODE (exp) == VAR_DECL)
1845 if (!cxx_mark_addressable (exp))
1846 return error_mark_node;
1847 adr = build_nop (ptrtype, build_address (exp));
1848 return adr;
1850 /* This way is better for a COMPONENT_REF since it can
1851 simplify the offset for a component. */
1852 adr = cp_build_unary_op (ADDR_EXPR, exp, 1, tf_warning_or_error);
1853 return cp_convert (ptrtype, adr);
1856 /* If a bitfield is used in a context where integral promotion
1857 applies, then the caller is expected to have used
1858 default_conversion. That function promotes bitfields correctly
1859 before calling this function. At this point, if we have a
1860 bitfield referenced, we may assume that is not subject to
1861 promotion, and that, therefore, the type of the resulting rvalue
1862 is the declared type of the bitfield. */
1863 exp = convert_bitfield_to_declared_type (exp);
1865 /* We do not call rvalue() here because we do not want to wrap EXP
1866 in a NON_LVALUE_EXPR. */
1868 /* [basic.lval]
1870 Non-class rvalues always have cv-unqualified types. */
1871 type = TREE_TYPE (exp);
1872 if (!CLASS_TYPE_P (type) && cv_qualified_p (type))
1873 exp = build_nop (cv_unqualified (type), exp);
1875 return exp;
1878 /* Perform preparatory conversions, as part of the "usual arithmetic
1879 conversions". In particular, as per [expr]:
1881 Whenever an lvalue expression appears as an operand of an
1882 operator that expects the rvalue for that operand, the
1883 lvalue-to-rvalue, array-to-pointer, or function-to-pointer
1884 standard conversions are applied to convert the expression to an
1885 rvalue.
1887 In addition, we perform integral promotions here, as those are
1888 applied to both operands to a binary operator before determining
1889 what additional conversions should apply. */
1891 tree
1892 default_conversion (tree exp)
1894 /* Check for target-specific promotions. */
1895 tree promoted_type = targetm.promoted_type (TREE_TYPE (exp));
1896 if (promoted_type)
1897 exp = cp_convert (promoted_type, exp);
1898 /* Perform the integral promotions first so that bitfield
1899 expressions (which may promote to "int", even if the bitfield is
1900 declared "unsigned") are promoted correctly. */
1901 else if (INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (TREE_TYPE (exp)))
1902 exp = perform_integral_promotions (exp);
1903 /* Perform the other conversions. */
1904 exp = decay_conversion (exp);
1906 return exp;
1909 /* EXPR is an expression with an integral or enumeration type.
1910 Perform the integral promotions in [conv.prom], and return the
1911 converted value. */
1913 tree
1914 perform_integral_promotions (tree expr)
1916 tree type;
1917 tree promoted_type;
1919 /* [conv.prom]
1921 If the bitfield has an enumerated type, it is treated as any
1922 other value of that type for promotion purposes. */
1923 type = is_bitfield_expr_with_lowered_type (expr);
1924 if (!type || TREE_CODE (type) != ENUMERAL_TYPE)
1925 type = TREE_TYPE (expr);
1926 gcc_assert (INTEGRAL_OR_ENUMERATION_TYPE_P (type));
1927 promoted_type = type_promotes_to (type);
1928 if (type != promoted_type)
1929 expr = cp_convert (promoted_type, expr);
1930 return expr;
1933 /* Returns nonzero iff exp is a STRING_CST or the result of applying
1934 decay_conversion to one. */
1937 string_conv_p (const_tree totype, const_tree exp, int warn)
1939 tree t;
1941 if (TREE_CODE (totype) != POINTER_TYPE)
1942 return 0;
1944 t = TREE_TYPE (totype);
1945 if (!same_type_p (t, char_type_node)
1946 && !same_type_p (t, char16_type_node)
1947 && !same_type_p (t, char32_type_node)
1948 && !same_type_p (t, wchar_type_node))
1949 return 0;
1951 if (TREE_CODE (exp) == STRING_CST)
1953 /* Make sure that we don't try to convert between char and wide chars. */
1954 if (!same_type_p (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (exp))), t))
1955 return 0;
1957 else
1959 /* Is this a string constant which has decayed to 'const char *'? */
1960 t = build_pointer_type (build_qualified_type (t, TYPE_QUAL_CONST));
1961 if (!same_type_p (TREE_TYPE (exp), t))
1962 return 0;
1963 STRIP_NOPS (exp);
1964 if (TREE_CODE (exp) != ADDR_EXPR
1965 || TREE_CODE (TREE_OPERAND (exp, 0)) != STRING_CST)
1966 return 0;
1969 /* This warning is not very useful, as it complains about printf. */
1970 if (warn)
1971 warning (OPT_Wwrite_strings,
1972 "deprecated conversion from string constant to %qT",
1973 totype);
1975 return 1;
1978 /* Given a COND_EXPR, MIN_EXPR, or MAX_EXPR in T, return it in a form that we
1979 can, for example, use as an lvalue. This code used to be in
1980 unary_complex_lvalue, but we needed it to deal with `a = (d == c) ? b : c'
1981 expressions, where we're dealing with aggregates. But now it's again only
1982 called from unary_complex_lvalue. The case (in particular) that led to
1983 this was with CODE == ADDR_EXPR, since it's not an lvalue when we'd
1984 get it there. */
1986 static tree
1987 rationalize_conditional_expr (enum tree_code code, tree t,
1988 tsubst_flags_t complain)
1990 /* For MIN_EXPR or MAX_EXPR, fold-const.c has arranged things so that
1991 the first operand is always the one to be used if both operands
1992 are equal, so we know what conditional expression this used to be. */
1993 if (TREE_CODE (t) == MIN_EXPR || TREE_CODE (t) == MAX_EXPR)
1995 tree op0 = TREE_OPERAND (t, 0);
1996 tree op1 = TREE_OPERAND (t, 1);
1998 /* The following code is incorrect if either operand side-effects. */
1999 gcc_assert (!TREE_SIDE_EFFECTS (op0)
2000 && !TREE_SIDE_EFFECTS (op1));
2001 return
2002 build_conditional_expr (build_x_binary_op ((TREE_CODE (t) == MIN_EXPR
2003 ? LE_EXPR : GE_EXPR),
2004 op0, TREE_CODE (op0),
2005 op1, TREE_CODE (op1),
2006 /*overloaded_p=*/NULL,
2007 complain),
2008 cp_build_unary_op (code, op0, 0, complain),
2009 cp_build_unary_op (code, op1, 0, complain),
2010 complain);
2013 return
2014 build_conditional_expr (TREE_OPERAND (t, 0),
2015 cp_build_unary_op (code, TREE_OPERAND (t, 1), 0,
2016 complain),
2017 cp_build_unary_op (code, TREE_OPERAND (t, 2), 0,
2018 complain),
2019 complain);
2022 /* Given the TYPE of an anonymous union field inside T, return the
2023 FIELD_DECL for the field. If not found return NULL_TREE. Because
2024 anonymous unions can nest, we must also search all anonymous unions
2025 that are directly reachable. */
2027 tree
2028 lookup_anon_field (tree t, tree type)
2030 tree field;
2032 for (field = TYPE_FIELDS (t); field; field = TREE_CHAIN (field))
2034 if (TREE_STATIC (field))
2035 continue;
2036 if (TREE_CODE (field) != FIELD_DECL || DECL_ARTIFICIAL (field))
2037 continue;
2039 /* If we find it directly, return the field. */
2040 if (DECL_NAME (field) == NULL_TREE
2041 && type == TYPE_MAIN_VARIANT (TREE_TYPE (field)))
2043 return field;
2046 /* Otherwise, it could be nested, search harder. */
2047 if (DECL_NAME (field) == NULL_TREE
2048 && ANON_AGGR_TYPE_P (TREE_TYPE (field)))
2050 tree subfield = lookup_anon_field (TREE_TYPE (field), type);
2051 if (subfield)
2052 return subfield;
2055 return NULL_TREE;
2058 /* Build an expression representing OBJECT.MEMBER. OBJECT is an
2059 expression; MEMBER is a DECL or baselink. If ACCESS_PATH is
2060 non-NULL, it indicates the path to the base used to name MEMBER.
2061 If PRESERVE_REFERENCE is true, the expression returned will have
2062 REFERENCE_TYPE if the MEMBER does. Otherwise, the expression
2063 returned will have the type referred to by the reference.
2065 This function does not perform access control; that is either done
2066 earlier by the parser when the name of MEMBER is resolved to MEMBER
2067 itself, or later when overload resolution selects one of the
2068 functions indicated by MEMBER. */
2070 tree
2071 build_class_member_access_expr (tree object, tree member,
2072 tree access_path, bool preserve_reference,
2073 tsubst_flags_t complain)
2075 tree object_type;
2076 tree member_scope;
2077 tree result = NULL_TREE;
2079 if (error_operand_p (object) || error_operand_p (member))
2080 return error_mark_node;
2082 gcc_assert (DECL_P (member) || BASELINK_P (member));
2084 /* [expr.ref]
2086 The type of the first expression shall be "class object" (of a
2087 complete type). */
2088 object_type = TREE_TYPE (object);
2089 if (!currently_open_class (object_type)
2090 && !complete_type_or_else (object_type, object))
2091 return error_mark_node;
2092 if (!CLASS_TYPE_P (object_type))
2094 if (complain & tf_error)
2095 error ("request for member %qD in %qE, which is of non-class type %qT",
2096 member, object, object_type);
2097 return error_mark_node;
2100 /* The standard does not seem to actually say that MEMBER must be a
2101 member of OBJECT_TYPE. However, that is clearly what is
2102 intended. */
2103 if (DECL_P (member))
2105 member_scope = DECL_CLASS_CONTEXT (member);
2106 mark_used (member);
2107 if (TREE_DEPRECATED (member))
2108 warn_deprecated_use (member, NULL_TREE);
2110 else
2111 member_scope = BINFO_TYPE (BASELINK_ACCESS_BINFO (member));
2112 /* If MEMBER is from an anonymous aggregate, MEMBER_SCOPE will
2113 presently be the anonymous union. Go outwards until we find a
2114 type related to OBJECT_TYPE. */
2115 while (ANON_AGGR_TYPE_P (member_scope)
2116 && !same_type_ignoring_top_level_qualifiers_p (member_scope,
2117 object_type))
2118 member_scope = TYPE_CONTEXT (member_scope);
2119 if (!member_scope || !DERIVED_FROM_P (member_scope, object_type))
2121 if (complain & tf_error)
2123 if (TREE_CODE (member) == FIELD_DECL)
2124 error ("invalid use of nonstatic data member %qE", member);
2125 else
2126 error ("%qD is not a member of %qT", member, object_type);
2128 return error_mark_node;
2131 /* Transform `(a, b).x' into `(*(a, &b)).x', `(a ? b : c).x' into
2132 `(*(a ? &b : &c)).x', and so on. A COND_EXPR is only an lvalue
2133 in the front end; only _DECLs and _REFs are lvalues in the back end. */
2135 tree temp = unary_complex_lvalue (ADDR_EXPR, object);
2136 if (temp)
2137 object = cp_build_indirect_ref (temp, RO_NULL, complain);
2140 /* In [expr.ref], there is an explicit list of the valid choices for
2141 MEMBER. We check for each of those cases here. */
2142 if (TREE_CODE (member) == VAR_DECL)
2144 /* A static data member. */
2145 result = member;
2146 /* If OBJECT has side-effects, they are supposed to occur. */
2147 if (TREE_SIDE_EFFECTS (object))
2148 result = build2 (COMPOUND_EXPR, TREE_TYPE (result), object, result);
2150 else if (TREE_CODE (member) == FIELD_DECL)
2152 /* A non-static data member. */
2153 bool null_object_p;
2154 int type_quals;
2155 tree member_type;
2157 null_object_p = (TREE_CODE (object) == INDIRECT_REF
2158 && integer_zerop (TREE_OPERAND (object, 0)));
2160 /* Convert OBJECT to the type of MEMBER. */
2161 if (!same_type_p (TYPE_MAIN_VARIANT (object_type),
2162 TYPE_MAIN_VARIANT (member_scope)))
2164 tree binfo;
2165 base_kind kind;
2167 binfo = lookup_base (access_path ? access_path : object_type,
2168 member_scope, ba_unique, &kind);
2169 if (binfo == error_mark_node)
2170 return error_mark_node;
2172 /* It is invalid to try to get to a virtual base of a
2173 NULL object. The most common cause is invalid use of
2174 offsetof macro. */
2175 if (null_object_p && kind == bk_via_virtual)
2177 if (complain & tf_error)
2179 error ("invalid access to non-static data member %qD of "
2180 "NULL object",
2181 member);
2182 error ("(perhaps the %<offsetof%> macro was used incorrectly)");
2184 return error_mark_node;
2187 /* Convert to the base. */
2188 object = build_base_path (PLUS_EXPR, object, binfo,
2189 /*nonnull=*/1);
2190 /* If we found the base successfully then we should be able
2191 to convert to it successfully. */
2192 gcc_assert (object != error_mark_node);
2195 /* Complain about other invalid uses of offsetof, even though they will
2196 give the right answer. Note that we complain whether or not they
2197 actually used the offsetof macro, since there's no way to know at this
2198 point. So we just give a warning, instead of a pedwarn. */
2199 /* Do not produce this warning for base class field references, because
2200 we know for a fact that didn't come from offsetof. This does occur
2201 in various testsuite cases where a null object is passed where a
2202 vtable access is required. */
2203 if (null_object_p && warn_invalid_offsetof
2204 && CLASSTYPE_NON_STD_LAYOUT (object_type)
2205 && !DECL_FIELD_IS_BASE (member)
2206 && cp_unevaluated_operand == 0
2207 && (complain & tf_warning))
2209 warning (OPT_Winvalid_offsetof,
2210 "invalid access to non-static data member %qD "
2211 " of NULL object", member);
2212 warning (OPT_Winvalid_offsetof,
2213 "(perhaps the %<offsetof%> macro was used incorrectly)");
2216 /* If MEMBER is from an anonymous aggregate, we have converted
2217 OBJECT so that it refers to the class containing the
2218 anonymous union. Generate a reference to the anonymous union
2219 itself, and recur to find MEMBER. */
2220 if (ANON_AGGR_TYPE_P (DECL_CONTEXT (member))
2221 /* When this code is called from build_field_call, the
2222 object already has the type of the anonymous union.
2223 That is because the COMPONENT_REF was already
2224 constructed, and was then disassembled before calling
2225 build_field_call. After the function-call code is
2226 cleaned up, this waste can be eliminated. */
2227 && (!same_type_ignoring_top_level_qualifiers_p
2228 (TREE_TYPE (object), DECL_CONTEXT (member))))
2230 tree anonymous_union;
2232 anonymous_union = lookup_anon_field (TREE_TYPE (object),
2233 DECL_CONTEXT (member));
2234 object = build_class_member_access_expr (object,
2235 anonymous_union,
2236 /*access_path=*/NULL_TREE,
2237 preserve_reference,
2238 complain);
2241 /* Compute the type of the field, as described in [expr.ref]. */
2242 type_quals = TYPE_UNQUALIFIED;
2243 member_type = TREE_TYPE (member);
2244 if (TREE_CODE (member_type) != REFERENCE_TYPE)
2246 type_quals = (cp_type_quals (member_type)
2247 | cp_type_quals (object_type));
2249 /* A field is const (volatile) if the enclosing object, or the
2250 field itself, is const (volatile). But, a mutable field is
2251 not const, even within a const object. */
2252 if (DECL_MUTABLE_P (member))
2253 type_quals &= ~TYPE_QUAL_CONST;
2254 member_type = cp_build_qualified_type (member_type, type_quals);
2257 result = build3 (COMPONENT_REF, member_type, object, member,
2258 NULL_TREE);
2259 result = fold_if_not_in_template (result);
2261 /* Mark the expression const or volatile, as appropriate. Even
2262 though we've dealt with the type above, we still have to mark the
2263 expression itself. */
2264 if (type_quals & TYPE_QUAL_CONST)
2265 TREE_READONLY (result) = 1;
2266 if (type_quals & TYPE_QUAL_VOLATILE)
2267 TREE_THIS_VOLATILE (result) = 1;
2269 else if (BASELINK_P (member))
2271 /* The member is a (possibly overloaded) member function. */
2272 tree functions;
2273 tree type;
2275 /* If the MEMBER is exactly one static member function, then we
2276 know the type of the expression. Otherwise, we must wait
2277 until overload resolution has been performed. */
2278 functions = BASELINK_FUNCTIONS (member);
2279 if (TREE_CODE (functions) == FUNCTION_DECL
2280 && DECL_STATIC_FUNCTION_P (functions))
2281 type = TREE_TYPE (functions);
2282 else
2283 type = unknown_type_node;
2284 /* Note that we do not convert OBJECT to the BASELINK_BINFO
2285 base. That will happen when the function is called. */
2286 result = build3 (COMPONENT_REF, type, object, member, NULL_TREE);
2288 else if (TREE_CODE (member) == CONST_DECL)
2290 /* The member is an enumerator. */
2291 result = member;
2292 /* If OBJECT has side-effects, they are supposed to occur. */
2293 if (TREE_SIDE_EFFECTS (object))
2294 result = build2 (COMPOUND_EXPR, TREE_TYPE (result),
2295 object, result);
2297 else
2299 if (complain & tf_error)
2300 error ("invalid use of %qD", member);
2301 return error_mark_node;
2304 if (!preserve_reference)
2305 /* [expr.ref]
2307 If E2 is declared to have type "reference to T", then ... the
2308 type of E1.E2 is T. */
2309 result = convert_from_reference (result);
2311 return result;
2314 /* Return the destructor denoted by OBJECT.SCOPE::DTOR_NAME, or, if
2315 SCOPE is NULL, by OBJECT.DTOR_NAME, where DTOR_NAME is ~type. */
2317 static tree
2318 lookup_destructor (tree object, tree scope, tree dtor_name)
2320 tree object_type = TREE_TYPE (object);
2321 tree dtor_type = TREE_OPERAND (dtor_name, 0);
2322 tree expr;
2324 if (scope && !check_dtor_name (scope, dtor_type))
2326 error ("qualified type %qT does not match destructor name ~%qT",
2327 scope, dtor_type);
2328 return error_mark_node;
2330 if (TREE_CODE (dtor_type) == IDENTIFIER_NODE)
2332 /* In a template, names we can't find a match for are still accepted
2333 destructor names, and we check them here. */
2334 if (check_dtor_name (object_type, dtor_type))
2335 dtor_type = object_type;
2336 else
2338 error ("object type %qT does not match destructor name ~%qT",
2339 object_type, dtor_type);
2340 return error_mark_node;
2344 else if (!DERIVED_FROM_P (dtor_type, TYPE_MAIN_VARIANT (object_type)))
2346 error ("the type being destroyed is %qT, but the destructor refers to %qT",
2347 TYPE_MAIN_VARIANT (object_type), dtor_type);
2348 return error_mark_node;
2350 expr = lookup_member (dtor_type, complete_dtor_identifier,
2351 /*protect=*/1, /*want_type=*/false);
2352 expr = (adjust_result_of_qualified_name_lookup
2353 (expr, dtor_type, object_type));
2354 return expr;
2357 /* An expression of the form "A::template B" has been resolved to
2358 DECL. Issue a diagnostic if B is not a template or template
2359 specialization. */
2361 void
2362 check_template_keyword (tree decl)
2364 /* The standard says:
2366 [temp.names]
2368 If a name prefixed by the keyword template is not a member
2369 template, the program is ill-formed.
2371 DR 228 removed the restriction that the template be a member
2372 template.
2374 DR 96, if accepted would add the further restriction that explicit
2375 template arguments must be provided if the template keyword is
2376 used, but, as of 2005-10-16, that DR is still in "drafting". If
2377 this DR is accepted, then the semantic checks here can be
2378 simplified, as the entity named must in fact be a template
2379 specialization, rather than, as at present, a set of overloaded
2380 functions containing at least one template function. */
2381 if (TREE_CODE (decl) != TEMPLATE_DECL
2382 && TREE_CODE (decl) != TEMPLATE_ID_EXPR)
2384 if (!is_overloaded_fn (decl))
2385 permerror (input_location, "%qD is not a template", decl);
2386 else
2388 tree fns;
2389 fns = decl;
2390 if (BASELINK_P (fns))
2391 fns = BASELINK_FUNCTIONS (fns);
2392 while (fns)
2394 tree fn = OVL_CURRENT (fns);
2395 if (TREE_CODE (fn) == TEMPLATE_DECL
2396 || TREE_CODE (fn) == TEMPLATE_ID_EXPR)
2397 break;
2398 if (TREE_CODE (fn) == FUNCTION_DECL
2399 && DECL_USE_TEMPLATE (fn)
2400 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (fn)))
2401 break;
2402 fns = OVL_NEXT (fns);
2404 if (!fns)
2405 permerror (input_location, "%qD is not a template", decl);
2410 /* This function is called by the parser to process a class member
2411 access expression of the form OBJECT.NAME. NAME is a node used by
2412 the parser to represent a name; it is not yet a DECL. It may,
2413 however, be a BASELINK where the BASELINK_FUNCTIONS is a
2414 TEMPLATE_ID_EXPR. Templates must be looked up by the parser, and
2415 there is no reason to do the lookup twice, so the parser keeps the
2416 BASELINK. TEMPLATE_P is true iff NAME was explicitly declared to
2417 be a template via the use of the "A::template B" syntax. */
2419 tree
2420 finish_class_member_access_expr (tree object, tree name, bool template_p,
2421 tsubst_flags_t complain)
2423 tree expr;
2424 tree object_type;
2425 tree member;
2426 tree access_path = NULL_TREE;
2427 tree orig_object = object;
2428 tree orig_name = name;
2430 if (object == error_mark_node || name == error_mark_node)
2431 return error_mark_node;
2433 /* If OBJECT is an ObjC class instance, we must obey ObjC access rules. */
2434 if (!objc_is_public (object, name))
2435 return error_mark_node;
2437 object_type = TREE_TYPE (object);
2439 if (processing_template_decl)
2441 if (/* If OBJECT_TYPE is dependent, so is OBJECT.NAME. */
2442 dependent_type_p (object_type)
2443 /* If NAME is just an IDENTIFIER_NODE, then the expression
2444 is dependent. */
2445 || TREE_CODE (object) == IDENTIFIER_NODE
2446 /* If NAME is "f<args>", where either 'f' or 'args' is
2447 dependent, then the expression is dependent. */
2448 || (TREE_CODE (name) == TEMPLATE_ID_EXPR
2449 && dependent_template_id_p (TREE_OPERAND (name, 0),
2450 TREE_OPERAND (name, 1)))
2451 /* If NAME is "T::X" where "T" is dependent, then the
2452 expression is dependent. */
2453 || (TREE_CODE (name) == SCOPE_REF
2454 && TYPE_P (TREE_OPERAND (name, 0))
2455 && dependent_type_p (TREE_OPERAND (name, 0))))
2456 return build_min_nt (COMPONENT_REF, object, name, NULL_TREE);
2457 object = build_non_dependent_expr (object);
2460 /* [expr.ref]
2462 The type of the first expression shall be "class object" (of a
2463 complete type). */
2464 if (!currently_open_class (object_type)
2465 && !complete_type_or_else (object_type, object))
2466 return error_mark_node;
2467 if (!CLASS_TYPE_P (object_type))
2469 if (complain & tf_error)
2470 error ("request for member %qD in %qE, which is of non-class type %qT",
2471 name, object, object_type);
2472 return error_mark_node;
2475 if (BASELINK_P (name))
2476 /* A member function that has already been looked up. */
2477 member = name;
2478 else
2480 bool is_template_id = false;
2481 tree template_args = NULL_TREE;
2482 tree scope;
2484 if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
2486 is_template_id = true;
2487 template_args = TREE_OPERAND (name, 1);
2488 name = TREE_OPERAND (name, 0);
2490 if (TREE_CODE (name) == OVERLOAD)
2491 name = DECL_NAME (get_first_fn (name));
2492 else if (DECL_P (name))
2493 name = DECL_NAME (name);
2496 if (TREE_CODE (name) == SCOPE_REF)
2498 /* A qualified name. The qualifying class or namespace `S'
2499 has already been looked up; it is either a TYPE or a
2500 NAMESPACE_DECL. */
2501 scope = TREE_OPERAND (name, 0);
2502 name = TREE_OPERAND (name, 1);
2504 /* If SCOPE is a namespace, then the qualified name does not
2505 name a member of OBJECT_TYPE. */
2506 if (TREE_CODE (scope) == NAMESPACE_DECL)
2508 if (complain & tf_error)
2509 error ("%<%D::%D%> is not a member of %qT",
2510 scope, name, object_type);
2511 return error_mark_node;
2514 gcc_assert (CLASS_TYPE_P (scope));
2515 gcc_assert (TREE_CODE (name) == IDENTIFIER_NODE
2516 || TREE_CODE (name) == BIT_NOT_EXPR);
2518 if (constructor_name_p (name, scope))
2520 if (complain & tf_error)
2521 error ("cannot call constructor %<%T::%D%> directly",
2522 scope, name);
2523 return error_mark_node;
2526 /* Find the base of OBJECT_TYPE corresponding to SCOPE. */
2527 access_path = lookup_base (object_type, scope, ba_check, NULL);
2528 if (access_path == error_mark_node)
2529 return error_mark_node;
2530 if (!access_path)
2532 if (complain & tf_error)
2533 error ("%qT is not a base of %qT", scope, object_type);
2534 return error_mark_node;
2537 else
2539 scope = NULL_TREE;
2540 access_path = object_type;
2543 if (TREE_CODE (name) == BIT_NOT_EXPR)
2544 member = lookup_destructor (object, scope, name);
2545 else
2547 /* Look up the member. */
2548 member = lookup_member (access_path, name, /*protect=*/1,
2549 /*want_type=*/false);
2550 if (member == NULL_TREE)
2552 if (complain & tf_error)
2553 error ("%qD has no member named %qE", object_type, name);
2554 return error_mark_node;
2556 if (member == error_mark_node)
2557 return error_mark_node;
2560 if (is_template_id)
2562 tree templ = member;
2564 if (BASELINK_P (templ))
2565 templ = lookup_template_function (templ, template_args);
2566 else
2568 if (complain & tf_error)
2569 error ("%qD is not a member template function", name);
2570 return error_mark_node;
2575 if (TREE_DEPRECATED (member))
2576 warn_deprecated_use (member, NULL_TREE);
2578 if (template_p)
2579 check_template_keyword (member);
2581 expr = build_class_member_access_expr (object, member, access_path,
2582 /*preserve_reference=*/false,
2583 complain);
2584 if (processing_template_decl && expr != error_mark_node)
2586 if (BASELINK_P (member))
2588 if (TREE_CODE (orig_name) == SCOPE_REF)
2589 BASELINK_QUALIFIED_P (member) = 1;
2590 orig_name = member;
2592 return build_min_non_dep (COMPONENT_REF, expr,
2593 orig_object, orig_name,
2594 NULL_TREE);
2597 return expr;
2600 /* Return an expression for the MEMBER_NAME field in the internal
2601 representation of PTRMEM, a pointer-to-member function. (Each
2602 pointer-to-member function type gets its own RECORD_TYPE so it is
2603 more convenient to access the fields by name than by FIELD_DECL.)
2604 This routine converts the NAME to a FIELD_DECL and then creates the
2605 node for the complete expression. */
2607 tree
2608 build_ptrmemfunc_access_expr (tree ptrmem, tree member_name)
2610 tree ptrmem_type;
2611 tree member;
2612 tree member_type;
2614 /* This code is a stripped down version of
2615 build_class_member_access_expr. It does not work to use that
2616 routine directly because it expects the object to be of class
2617 type. */
2618 ptrmem_type = TREE_TYPE (ptrmem);
2619 gcc_assert (TYPE_PTRMEMFUNC_P (ptrmem_type));
2620 member = lookup_member (ptrmem_type, member_name, /*protect=*/0,
2621 /*want_type=*/false);
2622 member_type = cp_build_qualified_type (TREE_TYPE (member),
2623 cp_type_quals (ptrmem_type));
2624 return fold_build3_loc (input_location,
2625 COMPONENT_REF, member_type,
2626 ptrmem, member, NULL_TREE);
2629 /* Given an expression PTR for a pointer, return an expression
2630 for the value pointed to.
2631 ERRORSTRING is the name of the operator to appear in error messages.
2633 This function may need to overload OPERATOR_FNNAME.
2634 Must also handle REFERENCE_TYPEs for C++. */
2636 tree
2637 build_x_indirect_ref (tree expr, ref_operator errorstring,
2638 tsubst_flags_t complain)
2640 tree orig_expr = expr;
2641 tree rval;
2643 if (processing_template_decl)
2645 /* Retain the type if we know the operand is a pointer so that
2646 describable_type doesn't make auto deduction break. */
2647 if (TREE_TYPE (expr) && POINTER_TYPE_P (TREE_TYPE (expr)))
2648 return build_min (INDIRECT_REF, TREE_TYPE (TREE_TYPE (expr)), expr);
2649 if (type_dependent_expression_p (expr))
2650 return build_min_nt (INDIRECT_REF, expr);
2651 expr = build_non_dependent_expr (expr);
2654 rval = build_new_op (INDIRECT_REF, LOOKUP_NORMAL, expr, NULL_TREE,
2655 NULL_TREE, /*overloaded_p=*/NULL, complain);
2656 if (!rval)
2657 rval = cp_build_indirect_ref (expr, errorstring, complain);
2659 if (processing_template_decl && rval != error_mark_node)
2660 return build_min_non_dep (INDIRECT_REF, rval, orig_expr);
2661 else
2662 return rval;
2665 /* Helper function called from c-common. */
2666 tree
2667 build_indirect_ref (location_t loc __attribute__ ((__unused__)),
2668 tree ptr, ref_operator errorstring)
2670 return cp_build_indirect_ref (ptr, errorstring, tf_warning_or_error);
2673 tree
2674 cp_build_indirect_ref (tree ptr, ref_operator errorstring,
2675 tsubst_flags_t complain)
2677 tree pointer, type;
2679 if (ptr == error_mark_node)
2680 return error_mark_node;
2682 if (ptr == current_class_ptr)
2683 return current_class_ref;
2685 pointer = (TREE_CODE (TREE_TYPE (ptr)) == REFERENCE_TYPE
2686 ? ptr : decay_conversion (ptr));
2687 type = TREE_TYPE (pointer);
2689 if (POINTER_TYPE_P (type))
2691 /* [expr.unary.op]
2693 If the type of the expression is "pointer to T," the type
2694 of the result is "T." */
2695 tree t = TREE_TYPE (type);
2697 if (CONVERT_EXPR_P (ptr)
2698 || TREE_CODE (ptr) == VIEW_CONVERT_EXPR)
2700 /* If a warning is issued, mark it to avoid duplicates from
2701 the backend. This only needs to be done at
2702 warn_strict_aliasing > 2. */
2703 if (warn_strict_aliasing > 2)
2704 if (strict_aliasing_warning (TREE_TYPE (TREE_OPERAND (ptr, 0)),
2705 type, TREE_OPERAND (ptr, 0)))
2706 TREE_NO_WARNING (ptr) = 1;
2709 if (VOID_TYPE_P (t))
2711 /* A pointer to incomplete type (other than cv void) can be
2712 dereferenced [expr.unary.op]/1 */
2713 if (complain & tf_error)
2714 error ("%qT is not a pointer-to-object type", type);
2715 return error_mark_node;
2717 else if (TREE_CODE (pointer) == ADDR_EXPR
2718 && same_type_p (t, TREE_TYPE (TREE_OPERAND (pointer, 0))))
2719 /* The POINTER was something like `&x'. We simplify `*&x' to
2720 `x'. */
2721 return TREE_OPERAND (pointer, 0);
2722 else
2724 tree ref = build1 (INDIRECT_REF, t, pointer);
2726 /* We *must* set TREE_READONLY when dereferencing a pointer to const,
2727 so that we get the proper error message if the result is used
2728 to assign to. Also, &* is supposed to be a no-op. */
2729 TREE_READONLY (ref) = CP_TYPE_CONST_P (t);
2730 TREE_THIS_VOLATILE (ref) = CP_TYPE_VOLATILE_P (t);
2731 TREE_SIDE_EFFECTS (ref)
2732 = (TREE_THIS_VOLATILE (ref) || TREE_SIDE_EFFECTS (pointer));
2733 return ref;
2736 else if (!(complain & tf_error))
2737 /* Don't emit any errors; we'll just return ERROR_MARK_NODE later. */
2739 /* `pointer' won't be an error_mark_node if we were given a
2740 pointer to member, so it's cool to check for this here. */
2741 else if (TYPE_PTR_TO_MEMBER_P (type))
2742 switch (errorstring)
2744 case RO_ARRAY_INDEXING:
2745 error ("invalid use of array indexing on pointer to member");
2746 break;
2747 case RO_UNARY_STAR:
2748 error ("invalid use of unary %<*%> on pointer to member");
2749 break;
2750 case RO_IMPLICIT_CONVERSION:
2751 error ("invalid use of implicit conversion on pointer to member");
2752 break;
2753 default:
2754 gcc_unreachable ();
2756 else if (pointer != error_mark_node)
2757 switch (errorstring)
2759 case RO_NULL:
2760 error ("invalid type argument");
2761 break;
2762 case RO_ARRAY_INDEXING:
2763 error ("invalid type argument of array indexing");
2764 break;
2765 case RO_UNARY_STAR:
2766 error ("invalid type argument of unary %<*%>");
2767 break;
2768 case RO_IMPLICIT_CONVERSION:
2769 error ("invalid type argument of implicit conversion");
2770 break;
2771 default:
2772 gcc_unreachable ();
2774 return error_mark_node;
2777 /* This handles expressions of the form "a[i]", which denotes
2778 an array reference.
2780 This is logically equivalent in C to *(a+i), but we may do it differently.
2781 If A is a variable or a member, we generate a primitive ARRAY_REF.
2782 This avoids forcing the array out of registers, and can work on
2783 arrays that are not lvalues (for example, members of structures returned
2784 by functions).
2786 If INDEX is of some user-defined type, it must be converted to
2787 integer type. Otherwise, to make a compatible PLUS_EXPR, it
2788 will inherit the type of the array, which will be some pointer type.
2790 LOC is the location to use in building the array reference. */
2792 tree
2793 build_array_ref (location_t loc, tree array, tree idx)
2795 tree ret;
2797 if (idx == 0)
2799 error_at (loc, "subscript missing in array reference");
2800 return error_mark_node;
2803 if (TREE_TYPE (array) == error_mark_node
2804 || TREE_TYPE (idx) == error_mark_node)
2805 return error_mark_node;
2807 /* If ARRAY is a COMPOUND_EXPR or COND_EXPR, move our reference
2808 inside it. */
2809 switch (TREE_CODE (array))
2811 case COMPOUND_EXPR:
2813 tree value = build_array_ref (loc, TREE_OPERAND (array, 1), idx);
2814 ret = build2 (COMPOUND_EXPR, TREE_TYPE (value),
2815 TREE_OPERAND (array, 0), value);
2816 SET_EXPR_LOCATION (ret, loc);
2817 return ret;
2820 case COND_EXPR:
2821 ret = build_conditional_expr
2822 (TREE_OPERAND (array, 0),
2823 build_array_ref (loc, TREE_OPERAND (array, 1), idx),
2824 build_array_ref (loc, TREE_OPERAND (array, 2), idx),
2825 tf_warning_or_error);
2826 protected_set_expr_location (ret, loc);
2827 return ret;
2829 default:
2830 break;
2833 if (TREE_CODE (TREE_TYPE (array)) == ARRAY_TYPE)
2835 tree rval, type;
2837 warn_array_subscript_with_type_char (idx);
2839 if (!INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (TREE_TYPE (idx)))
2841 error_at (loc, "array subscript is not an integer");
2842 return error_mark_node;
2845 /* Apply integral promotions *after* noticing character types.
2846 (It is unclear why we do these promotions -- the standard
2847 does not say that we should. In fact, the natural thing would
2848 seem to be to convert IDX to ptrdiff_t; we're performing
2849 pointer arithmetic.) */
2850 idx = perform_integral_promotions (idx);
2852 /* An array that is indexed by a non-constant
2853 cannot be stored in a register; we must be able to do
2854 address arithmetic on its address.
2855 Likewise an array of elements of variable size. */
2856 if (TREE_CODE (idx) != INTEGER_CST
2857 || (COMPLETE_TYPE_P (TREE_TYPE (TREE_TYPE (array)))
2858 && (TREE_CODE (TYPE_SIZE (TREE_TYPE (TREE_TYPE (array))))
2859 != INTEGER_CST)))
2861 if (!cxx_mark_addressable (array))
2862 return error_mark_node;
2865 /* An array that is indexed by a constant value which is not within
2866 the array bounds cannot be stored in a register either; because we
2867 would get a crash in store_bit_field/extract_bit_field when trying
2868 to access a non-existent part of the register. */
2869 if (TREE_CODE (idx) == INTEGER_CST
2870 && TYPE_DOMAIN (TREE_TYPE (array))
2871 && ! int_fits_type_p (idx, TYPE_DOMAIN (TREE_TYPE (array))))
2873 if (!cxx_mark_addressable (array))
2874 return error_mark_node;
2877 if (!lvalue_p (array))
2878 pedwarn (loc, OPT_pedantic,
2879 "ISO C++ forbids subscripting non-lvalue array");
2881 /* Note in C++ it is valid to subscript a `register' array, since
2882 it is valid to take the address of something with that
2883 storage specification. */
2884 if (extra_warnings)
2886 tree foo = array;
2887 while (TREE_CODE (foo) == COMPONENT_REF)
2888 foo = TREE_OPERAND (foo, 0);
2889 if (TREE_CODE (foo) == VAR_DECL && DECL_REGISTER (foo))
2890 warning_at (loc, OPT_Wextra,
2891 "subscripting array declared %<register%>");
2894 type = TREE_TYPE (TREE_TYPE (array));
2895 rval = build4 (ARRAY_REF, type, array, idx, NULL_TREE, NULL_TREE);
2896 /* Array ref is const/volatile if the array elements are
2897 or if the array is.. */
2898 TREE_READONLY (rval)
2899 |= (CP_TYPE_CONST_P (type) | TREE_READONLY (array));
2900 TREE_SIDE_EFFECTS (rval)
2901 |= (CP_TYPE_VOLATILE_P (type) | TREE_SIDE_EFFECTS (array));
2902 TREE_THIS_VOLATILE (rval)
2903 |= (CP_TYPE_VOLATILE_P (type) | TREE_THIS_VOLATILE (array));
2904 ret = require_complete_type (fold_if_not_in_template (rval));
2905 protected_set_expr_location (ret, loc);
2906 return ret;
2910 tree ar = default_conversion (array);
2911 tree ind = default_conversion (idx);
2913 /* Put the integer in IND to simplify error checking. */
2914 if (TREE_CODE (TREE_TYPE (ar)) == INTEGER_TYPE)
2916 tree temp = ar;
2917 ar = ind;
2918 ind = temp;
2921 if (ar == error_mark_node)
2922 return ar;
2924 if (TREE_CODE (TREE_TYPE (ar)) != POINTER_TYPE)
2926 error_at (loc, "subscripted value is neither array nor pointer");
2927 return error_mark_node;
2929 if (TREE_CODE (TREE_TYPE (ind)) != INTEGER_TYPE)
2931 error_at (loc, "array subscript is not an integer");
2932 return error_mark_node;
2935 warn_array_subscript_with_type_char (idx);
2937 ret = cp_build_indirect_ref (cp_build_binary_op (input_location,
2938 PLUS_EXPR, ar, ind,
2939 tf_warning_or_error),
2940 RO_ARRAY_INDEXING,
2941 tf_warning_or_error);
2942 protected_set_expr_location (ret, loc);
2943 return ret;
2947 /* Resolve a pointer to member function. INSTANCE is the object
2948 instance to use, if the member points to a virtual member.
2950 This used to avoid checking for virtual functions if basetype
2951 has no virtual functions, according to an earlier ANSI draft.
2952 With the final ISO C++ rules, such an optimization is
2953 incorrect: A pointer to a derived member can be static_cast
2954 to pointer-to-base-member, as long as the dynamic object
2955 later has the right member. */
2957 tree
2958 get_member_function_from_ptrfunc (tree *instance_ptrptr, tree function)
2960 if (TREE_CODE (function) == OFFSET_REF)
2961 function = TREE_OPERAND (function, 1);
2963 if (TYPE_PTRMEMFUNC_P (TREE_TYPE (function)))
2965 tree idx, delta, e1, e2, e3, vtbl, basetype;
2966 tree fntype = TYPE_PTRMEMFUNC_FN_TYPE (TREE_TYPE (function));
2968 tree instance_ptr = *instance_ptrptr;
2969 tree instance_save_expr = 0;
2970 if (instance_ptr == error_mark_node)
2972 if (TREE_CODE (function) == PTRMEM_CST)
2974 /* Extracting the function address from a pmf is only
2975 allowed with -Wno-pmf-conversions. It only works for
2976 pmf constants. */
2977 e1 = build_addr_func (PTRMEM_CST_MEMBER (function));
2978 e1 = convert (fntype, e1);
2979 return e1;
2981 else
2983 error ("object missing in use of %qE", function);
2984 return error_mark_node;
2988 if (TREE_SIDE_EFFECTS (instance_ptr))
2989 instance_ptr = instance_save_expr = save_expr (instance_ptr);
2991 if (TREE_SIDE_EFFECTS (function))
2992 function = save_expr (function);
2994 /* Start by extracting all the information from the PMF itself. */
2995 e3 = pfn_from_ptrmemfunc (function);
2996 delta = delta_from_ptrmemfunc (function);
2997 idx = build1 (NOP_EXPR, vtable_index_type, e3);
2998 switch (TARGET_PTRMEMFUNC_VBIT_LOCATION)
3000 case ptrmemfunc_vbit_in_pfn:
3001 e1 = cp_build_binary_op (input_location,
3002 BIT_AND_EXPR, idx, integer_one_node,
3003 tf_warning_or_error);
3004 idx = cp_build_binary_op (input_location,
3005 MINUS_EXPR, idx, integer_one_node,
3006 tf_warning_or_error);
3007 break;
3009 case ptrmemfunc_vbit_in_delta:
3010 e1 = cp_build_binary_op (input_location,
3011 BIT_AND_EXPR, delta, integer_one_node,
3012 tf_warning_or_error);
3013 delta = cp_build_binary_op (input_location,
3014 RSHIFT_EXPR, delta, integer_one_node,
3015 tf_warning_or_error);
3016 break;
3018 default:
3019 gcc_unreachable ();
3022 /* Convert down to the right base before using the instance. A
3023 special case is that in a pointer to member of class C, C may
3024 be incomplete. In that case, the function will of course be
3025 a member of C, and no conversion is required. In fact,
3026 lookup_base will fail in that case, because incomplete
3027 classes do not have BINFOs. */
3028 basetype = TYPE_METHOD_BASETYPE (TREE_TYPE (fntype));
3029 if (!same_type_ignoring_top_level_qualifiers_p
3030 (basetype, TREE_TYPE (TREE_TYPE (instance_ptr))))
3032 basetype = lookup_base (TREE_TYPE (TREE_TYPE (instance_ptr)),
3033 basetype, ba_check, NULL);
3034 instance_ptr = build_base_path (PLUS_EXPR, instance_ptr, basetype,
3036 if (instance_ptr == error_mark_node)
3037 return error_mark_node;
3039 /* ...and then the delta in the PMF. */
3040 instance_ptr = build2 (POINTER_PLUS_EXPR, TREE_TYPE (instance_ptr),
3041 instance_ptr, fold_convert (sizetype, delta));
3043 /* Hand back the adjusted 'this' argument to our caller. */
3044 *instance_ptrptr = instance_ptr;
3046 /* Next extract the vtable pointer from the object. */
3047 vtbl = build1 (NOP_EXPR, build_pointer_type (vtbl_ptr_type_node),
3048 instance_ptr);
3049 vtbl = cp_build_indirect_ref (vtbl, RO_NULL, tf_warning_or_error);
3050 /* If the object is not dynamic the access invokes undefined
3051 behavior. As it is not executed in this case silence the
3052 spurious warnings it may provoke. */
3053 TREE_NO_WARNING (vtbl) = 1;
3055 /* Finally, extract the function pointer from the vtable. */
3056 e2 = fold_build2_loc (input_location,
3057 POINTER_PLUS_EXPR, TREE_TYPE (vtbl), vtbl,
3058 fold_convert (sizetype, idx));
3059 e2 = cp_build_indirect_ref (e2, RO_NULL, tf_warning_or_error);
3060 TREE_CONSTANT (e2) = 1;
3062 /* When using function descriptors, the address of the
3063 vtable entry is treated as a function pointer. */
3064 if (TARGET_VTABLE_USES_DESCRIPTORS)
3065 e2 = build1 (NOP_EXPR, TREE_TYPE (e2),
3066 cp_build_unary_op (ADDR_EXPR, e2, /*noconvert=*/1,
3067 tf_warning_or_error));
3069 e2 = fold_convert (TREE_TYPE (e3), e2);
3070 e1 = build_conditional_expr (e1, e2, e3, tf_warning_or_error);
3072 /* Make sure this doesn't get evaluated first inside one of the
3073 branches of the COND_EXPR. */
3074 if (instance_save_expr)
3075 e1 = build2 (COMPOUND_EXPR, TREE_TYPE (e1),
3076 instance_save_expr, e1);
3078 function = e1;
3080 return function;
3083 /* Used by the C-common bits. */
3084 tree
3085 build_function_call (location_t loc ATTRIBUTE_UNUSED,
3086 tree function, tree params)
3088 return cp_build_function_call (function, params, tf_warning_or_error);
3091 /* Used by the C-common bits. */
3092 tree
3093 build_function_call_vec (location_t loc ATTRIBUTE_UNUSED,
3094 tree function, VEC(tree,gc) *params,
3095 VEC(tree,gc) *origtypes ATTRIBUTE_UNUSED)
3097 VEC(tree,gc) *orig_params = params;
3098 tree ret = cp_build_function_call_vec (function, &params,
3099 tf_warning_or_error);
3101 /* cp_build_function_call_vec can reallocate PARAMS by adding
3102 default arguments. That should never happen here. Verify
3103 that. */
3104 gcc_assert (params == orig_params);
3106 return ret;
3109 /* Build a function call using a tree list of arguments. */
3111 tree
3112 cp_build_function_call (tree function, tree params, tsubst_flags_t complain)
3114 VEC(tree,gc) *vec;
3115 tree ret;
3117 vec = make_tree_vector ();
3118 for (; params != NULL_TREE; params = TREE_CHAIN (params))
3119 VEC_safe_push (tree, gc, vec, TREE_VALUE (params));
3120 ret = cp_build_function_call_vec (function, &vec, complain);
3121 release_tree_vector (vec);
3122 return ret;
3125 /* Build a function call using a vector of arguments. PARAMS may be
3126 NULL if there are no parameters. This changes the contents of
3127 PARAMS. */
3129 tree
3130 cp_build_function_call_vec (tree function, VEC(tree,gc) **params,
3131 tsubst_flags_t complain)
3133 tree fntype, fndecl;
3134 int is_method;
3135 tree original = function;
3136 int nargs;
3137 tree *argarray;
3138 tree parm_types;
3139 VEC(tree,gc) *allocated = NULL;
3140 tree ret;
3142 /* For Objective-C, convert any calls via a cast to OBJC_TYPE_REF
3143 expressions, like those used for ObjC messenger dispatches. */
3144 if (params != NULL && !VEC_empty (tree, *params))
3145 function = objc_rewrite_function_call (function,
3146 VEC_index (tree, *params, 0));
3148 /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
3149 Strip such NOP_EXPRs, since FUNCTION is used in non-lvalue context. */
3150 if (TREE_CODE (function) == NOP_EXPR
3151 && TREE_TYPE (function) == TREE_TYPE (TREE_OPERAND (function, 0)))
3152 function = TREE_OPERAND (function, 0);
3154 if (TREE_CODE (function) == FUNCTION_DECL)
3156 mark_used (function);
3157 fndecl = function;
3159 /* Convert anything with function type to a pointer-to-function. */
3160 if (DECL_MAIN_P (function) && (complain & tf_error))
3161 pedwarn (input_location, OPT_pedantic,
3162 "ISO C++ forbids calling %<::main%> from within program");
3164 function = build_addr_func (function);
3166 else
3168 fndecl = NULL_TREE;
3170 function = build_addr_func (function);
3173 if (function == error_mark_node)
3174 return error_mark_node;
3176 fntype = TREE_TYPE (function);
3178 if (TYPE_PTRMEMFUNC_P (fntype))
3180 if (complain & tf_error)
3181 error ("must use %<.*%> or %<->*%> to call pointer-to-member "
3182 "function in %<%E (...)%>, e.g. %<(... ->* %E) (...)%>",
3183 original, original);
3184 return error_mark_node;
3187 is_method = (TREE_CODE (fntype) == POINTER_TYPE
3188 && TREE_CODE (TREE_TYPE (fntype)) == METHOD_TYPE);
3190 if (!((TREE_CODE (fntype) == POINTER_TYPE
3191 && TREE_CODE (TREE_TYPE (fntype)) == FUNCTION_TYPE)
3192 || is_method
3193 || TREE_CODE (function) == TEMPLATE_ID_EXPR))
3195 if (complain & tf_error)
3196 error ("%qE cannot be used as a function", original);
3197 return error_mark_node;
3200 /* fntype now gets the type of function pointed to. */
3201 fntype = TREE_TYPE (fntype);
3202 parm_types = TYPE_ARG_TYPES (fntype);
3204 if (params == NULL)
3206 allocated = make_tree_vector ();
3207 params = &allocated;
3210 nargs = convert_arguments (parm_types, params, fndecl, LOOKUP_NORMAL,
3211 complain);
3212 if (nargs < 0)
3213 return error_mark_node;
3215 argarray = VEC_address (tree, *params);
3217 /* Check for errors in format strings and inappropriately
3218 null parameters. */
3219 check_function_arguments (TYPE_ATTRIBUTES (fntype), nargs, argarray,
3220 parm_types);
3222 ret = build_cxx_call (function, nargs, argarray);
3224 if (allocated != NULL)
3225 release_tree_vector (allocated);
3227 return ret;
3230 /* Convert the actual parameter expressions in the list VALUES to the
3231 types in the list TYPELIST. The converted expressions are stored
3232 back in the VALUES vector.
3233 If parmdecls is exhausted, or when an element has NULL as its type,
3234 perform the default conversions.
3236 NAME is an IDENTIFIER_NODE or 0. It is used only for error messages.
3238 This is also where warnings about wrong number of args are generated.
3240 Returns the actual number of arguments processed (which might be less
3241 than the length of the vector), or -1 on error.
3243 In C++, unspecified trailing parameters can be filled in with their
3244 default arguments, if such were specified. Do so here. */
3246 static int
3247 convert_arguments (tree typelist, VEC(tree,gc) **values, tree fndecl,
3248 int flags, tsubst_flags_t complain)
3250 tree typetail;
3251 const char *called_thing = 0;
3252 unsigned int i;
3254 /* Argument passing is always copy-initialization. */
3255 flags |= LOOKUP_ONLYCONVERTING;
3257 if (fndecl)
3259 if (TREE_CODE (TREE_TYPE (fndecl)) == METHOD_TYPE)
3261 if (DECL_NAME (fndecl) == NULL_TREE
3262 || IDENTIFIER_HAS_TYPE_VALUE (DECL_NAME (fndecl)))
3263 called_thing = "constructor";
3264 else
3265 called_thing = "member function";
3267 else
3268 called_thing = "function";
3271 for (i = 0, typetail = typelist;
3272 i < VEC_length (tree, *values);
3273 i++)
3275 tree type = typetail ? TREE_VALUE (typetail) : 0;
3276 tree val = VEC_index (tree, *values, i);
3278 if (val == error_mark_node || type == error_mark_node)
3279 return -1;
3281 if (type == void_type_node)
3283 if (complain & tf_error)
3285 if (fndecl)
3287 error ("too many arguments to %s %q+#D",
3288 called_thing, fndecl);
3289 error ("at this point in file");
3291 else
3292 error ("too many arguments to function");
3293 return i;
3295 else
3296 return -1;
3299 /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
3300 Strip such NOP_EXPRs, since VAL is used in non-lvalue context. */
3301 if (TREE_CODE (val) == NOP_EXPR
3302 && TREE_TYPE (val) == TREE_TYPE (TREE_OPERAND (val, 0))
3303 && (type == 0 || TREE_CODE (type) != REFERENCE_TYPE))
3304 val = TREE_OPERAND (val, 0);
3306 if (type == 0 || TREE_CODE (type) != REFERENCE_TYPE)
3308 if (TREE_CODE (TREE_TYPE (val)) == ARRAY_TYPE
3309 || TREE_CODE (TREE_TYPE (val)) == FUNCTION_TYPE
3310 || TREE_CODE (TREE_TYPE (val)) == METHOD_TYPE)
3311 val = decay_conversion (val);
3314 if (val == error_mark_node)
3315 return -1;
3317 if (type != 0)
3319 /* Formal parm type is specified by a function prototype. */
3320 tree parmval;
3322 if (!COMPLETE_TYPE_P (complete_type (type)))
3324 if (complain & tf_error)
3326 if (fndecl)
3327 error ("parameter %P of %qD has incomplete type %qT",
3328 i, fndecl, type);
3329 else
3330 error ("parameter %P has incomplete type %qT", i, type);
3332 parmval = error_mark_node;
3334 else
3336 parmval = convert_for_initialization
3337 (NULL_TREE, type, val, flags,
3338 "argument passing", fndecl, i, complain);
3339 parmval = convert_for_arg_passing (type, parmval);
3342 if (parmval == error_mark_node)
3343 return -1;
3345 VEC_replace (tree, *values, i, parmval);
3347 else
3349 if (fndecl && DECL_BUILT_IN (fndecl)
3350 && DECL_FUNCTION_CODE (fndecl) == BUILT_IN_CONSTANT_P)
3351 /* Don't do ellipsis conversion for __built_in_constant_p
3352 as this will result in spurious errors for non-trivial
3353 types. */
3354 val = require_complete_type (val);
3355 else
3356 val = convert_arg_to_ellipsis (val);
3358 VEC_replace (tree, *values, i, val);
3361 if (typetail)
3362 typetail = TREE_CHAIN (typetail);
3365 if (typetail != 0 && typetail != void_list_node)
3367 /* See if there are default arguments that can be used. Because
3368 we hold default arguments in the FUNCTION_TYPE (which is so
3369 wrong), we can see default parameters here from deduced
3370 contexts (and via typeof) for indirect function calls.
3371 Fortunately we know whether we have a function decl to
3372 provide default arguments in a language conformant
3373 manner. */
3374 if (fndecl && TREE_PURPOSE (typetail)
3375 && TREE_CODE (TREE_PURPOSE (typetail)) != DEFAULT_ARG)
3377 for (; typetail != void_list_node; ++i)
3379 tree parmval
3380 = convert_default_arg (TREE_VALUE (typetail),
3381 TREE_PURPOSE (typetail),
3382 fndecl, i);
3384 if (parmval == error_mark_node)
3385 return -1;
3387 VEC_safe_push (tree, gc, *values, parmval);
3388 typetail = TREE_CHAIN (typetail);
3389 /* ends with `...'. */
3390 if (typetail == NULL_TREE)
3391 break;
3394 else
3396 if (complain & tf_error)
3398 if (fndecl)
3400 error ("too few arguments to %s %q+#D",
3401 called_thing, fndecl);
3402 error ("at this point in file");
3404 else
3405 error ("too few arguments to function");
3407 return -1;
3411 return (int) i;
3414 /* Build a binary-operation expression, after performing default
3415 conversions on the operands. CODE is the kind of expression to
3416 build. ARG1 and ARG2 are the arguments. ARG1_CODE and ARG2_CODE
3417 are the tree codes which correspond to ARG1 and ARG2 when issuing
3418 warnings about possibly misplaced parentheses. They may differ
3419 from the TREE_CODE of ARG1 and ARG2 if the parser has done constant
3420 folding (e.g., if the parser sees "a | 1 + 1", it may call this
3421 routine with ARG2 being an INTEGER_CST and ARG2_CODE == PLUS_EXPR).
3422 To avoid issuing any parentheses warnings, pass ARG1_CODE and/or
3423 ARG2_CODE as ERROR_MARK. */
3425 tree
3426 build_x_binary_op (enum tree_code code, tree arg1, enum tree_code arg1_code,
3427 tree arg2, enum tree_code arg2_code, bool *overloaded_p,
3428 tsubst_flags_t complain)
3430 tree orig_arg1;
3431 tree orig_arg2;
3432 tree expr;
3434 orig_arg1 = arg1;
3435 orig_arg2 = arg2;
3437 if (processing_template_decl)
3439 if (type_dependent_expression_p (arg1)
3440 || type_dependent_expression_p (arg2))
3441 return build_min_nt (code, arg1, arg2);
3442 arg1 = build_non_dependent_expr (arg1);
3443 arg2 = build_non_dependent_expr (arg2);
3446 if (code == DOTSTAR_EXPR)
3447 expr = build_m_component_ref (arg1, arg2);
3448 else
3449 expr = build_new_op (code, LOOKUP_NORMAL, arg1, arg2, NULL_TREE,
3450 overloaded_p, complain);
3452 /* Check for cases such as x+y<<z which users are likely to
3453 misinterpret. But don't warn about obj << x + y, since that is a
3454 common idiom for I/O. */
3455 if (warn_parentheses
3456 && (complain & tf_warning)
3457 && !processing_template_decl
3458 && !error_operand_p (arg1)
3459 && !error_operand_p (arg2)
3460 && (code != LSHIFT_EXPR
3461 || !CLASS_TYPE_P (TREE_TYPE (arg1))))
3462 warn_about_parentheses (code, arg1_code, orig_arg1, arg2_code, orig_arg2);
3464 if (processing_template_decl && expr != error_mark_node)
3465 return build_min_non_dep (code, expr, orig_arg1, orig_arg2);
3467 return expr;
3470 /* Build and return an ARRAY_REF expression. */
3472 tree
3473 build_x_array_ref (tree arg1, tree arg2, tsubst_flags_t complain)
3475 tree orig_arg1 = arg1;
3476 tree orig_arg2 = arg2;
3477 tree expr;
3479 if (processing_template_decl)
3481 if (type_dependent_expression_p (arg1)
3482 || type_dependent_expression_p (arg2))
3483 return build_min_nt (ARRAY_REF, arg1, arg2,
3484 NULL_TREE, NULL_TREE);
3485 arg1 = build_non_dependent_expr (arg1);
3486 arg2 = build_non_dependent_expr (arg2);
3489 expr = build_new_op (ARRAY_REF, LOOKUP_NORMAL, arg1, arg2, NULL_TREE,
3490 /*overloaded_p=*/NULL, complain);
3492 if (processing_template_decl && expr != error_mark_node)
3493 return build_min_non_dep (ARRAY_REF, expr, orig_arg1, orig_arg2,
3494 NULL_TREE, NULL_TREE);
3495 return expr;
3498 /* For the c-common bits. */
3499 tree
3500 build_binary_op (location_t location, enum tree_code code, tree op0, tree op1,
3501 int convert_p ATTRIBUTE_UNUSED)
3503 return cp_build_binary_op (location, code, op0, op1, tf_warning_or_error);
3507 /* Build a binary-operation expression without default conversions.
3508 CODE is the kind of expression to build.
3509 LOCATION is the location_t of the operator in the source code.
3510 This function differs from `build' in several ways:
3511 the data type of the result is computed and recorded in it,
3512 warnings are generated if arg data types are invalid,
3513 special handling for addition and subtraction of pointers is known,
3514 and some optimization is done (operations on narrow ints
3515 are done in the narrower type when that gives the same result).
3516 Constant folding is also done before the result is returned.
3518 Note that the operands will never have enumeral types
3519 because either they have just had the default conversions performed
3520 or they have both just been converted to some other type in which
3521 the arithmetic is to be done.
3523 C++: must do special pointer arithmetic when implementing
3524 multiple inheritance, and deal with pointer to member functions. */
3526 tree
3527 cp_build_binary_op (location_t location,
3528 enum tree_code code, tree orig_op0, tree orig_op1,
3529 tsubst_flags_t complain)
3531 tree op0, op1;
3532 enum tree_code code0, code1;
3533 tree type0, type1;
3534 const char *invalid_op_diag;
3536 /* Expression code to give to the expression when it is built.
3537 Normally this is CODE, which is what the caller asked for,
3538 but in some special cases we change it. */
3539 enum tree_code resultcode = code;
3541 /* Data type in which the computation is to be performed.
3542 In the simplest cases this is the common type of the arguments. */
3543 tree result_type = NULL;
3545 /* Nonzero means operands have already been type-converted
3546 in whatever way is necessary.
3547 Zero means they need to be converted to RESULT_TYPE. */
3548 int converted = 0;
3550 /* Nonzero means create the expression with this type, rather than
3551 RESULT_TYPE. */
3552 tree build_type = 0;
3554 /* Nonzero means after finally constructing the expression
3555 convert it to this type. */
3556 tree final_type = 0;
3558 tree result;
3560 /* Nonzero if this is an operation like MIN or MAX which can
3561 safely be computed in short if both args are promoted shorts.
3562 Also implies COMMON.
3563 -1 indicates a bitwise operation; this makes a difference
3564 in the exact conditions for when it is safe to do the operation
3565 in a narrower mode. */
3566 int shorten = 0;
3568 /* Nonzero if this is a comparison operation;
3569 if both args are promoted shorts, compare the original shorts.
3570 Also implies COMMON. */
3571 int short_compare = 0;
3573 /* Nonzero means set RESULT_TYPE to the common type of the args. */
3574 int common = 0;
3576 /* True if both operands have arithmetic type. */
3577 bool arithmetic_types_p;
3579 /* Apply default conversions. */
3580 op0 = orig_op0;
3581 op1 = orig_op1;
3583 if (code == TRUTH_AND_EXPR || code == TRUTH_ANDIF_EXPR
3584 || code == TRUTH_OR_EXPR || code == TRUTH_ORIF_EXPR
3585 || code == TRUTH_XOR_EXPR)
3587 if (!really_overloaded_fn (op0))
3588 op0 = decay_conversion (op0);
3589 if (!really_overloaded_fn (op1))
3590 op1 = decay_conversion (op1);
3592 else
3594 if (!really_overloaded_fn (op0))
3595 op0 = default_conversion (op0);
3596 if (!really_overloaded_fn (op1))
3597 op1 = default_conversion (op1);
3600 /* Strip NON_LVALUE_EXPRs, etc., since we aren't using as an lvalue. */
3601 STRIP_TYPE_NOPS (op0);
3602 STRIP_TYPE_NOPS (op1);
3604 /* DTRT if one side is an overloaded function, but complain about it. */
3605 if (type_unknown_p (op0))
3607 tree t = instantiate_type (TREE_TYPE (op1), op0, tf_none);
3608 if (t != error_mark_node)
3610 if (complain & tf_error)
3611 permerror (input_location, "assuming cast to type %qT from overloaded function",
3612 TREE_TYPE (t));
3613 op0 = t;
3616 if (type_unknown_p (op1))
3618 tree t = instantiate_type (TREE_TYPE (op0), op1, tf_none);
3619 if (t != error_mark_node)
3621 if (complain & tf_error)
3622 permerror (input_location, "assuming cast to type %qT from overloaded function",
3623 TREE_TYPE (t));
3624 op1 = t;
3628 type0 = TREE_TYPE (op0);
3629 type1 = TREE_TYPE (op1);
3631 /* The expression codes of the data types of the arguments tell us
3632 whether the arguments are integers, floating, pointers, etc. */
3633 code0 = TREE_CODE (type0);
3634 code1 = TREE_CODE (type1);
3636 /* If an error was already reported for one of the arguments,
3637 avoid reporting another error. */
3638 if (code0 == ERROR_MARK || code1 == ERROR_MARK)
3639 return error_mark_node;
3641 if ((invalid_op_diag
3642 = targetm.invalid_binary_op (code, type0, type1)))
3644 error (invalid_op_diag);
3645 return error_mark_node;
3648 /* Issue warnings about peculiar, but valid, uses of NULL. */
3649 if ((orig_op0 == null_node || orig_op1 == null_node)
3650 /* It's reasonable to use pointer values as operands of &&
3651 and ||, so NULL is no exception. */
3652 && code != TRUTH_ANDIF_EXPR && code != TRUTH_ORIF_EXPR
3653 && ( /* Both are NULL (or 0) and the operation was not a
3654 comparison or a pointer subtraction. */
3655 (null_ptr_cst_p (orig_op0) && null_ptr_cst_p (orig_op1)
3656 && code != EQ_EXPR && code != NE_EXPR && code != MINUS_EXPR)
3657 /* Or if one of OP0 or OP1 is neither a pointer nor NULL. */
3658 || (!null_ptr_cst_p (orig_op0)
3659 && !TYPE_PTR_P (type0) && !TYPE_PTR_TO_MEMBER_P (type0))
3660 || (!null_ptr_cst_p (orig_op1)
3661 && !TYPE_PTR_P (type1) && !TYPE_PTR_TO_MEMBER_P (type1)))
3662 && (complain & tf_warning))
3663 /* Some sort of arithmetic operation involving NULL was
3664 performed. */
3665 warning (OPT_Wpointer_arith, "NULL used in arithmetic");
3667 switch (code)
3669 case MINUS_EXPR:
3670 /* Subtraction of two similar pointers.
3671 We must subtract them as integers, then divide by object size. */
3672 if (code0 == POINTER_TYPE && code1 == POINTER_TYPE
3673 && same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (type0),
3674 TREE_TYPE (type1)))
3675 return pointer_diff (op0, op1, common_pointer_type (type0, type1));
3676 /* In all other cases except pointer - int, the usual arithmetic
3677 rules apply. */
3678 else if (!(code0 == POINTER_TYPE && code1 == INTEGER_TYPE))
3680 common = 1;
3681 break;
3683 /* The pointer - int case is just like pointer + int; fall
3684 through. */
3685 case PLUS_EXPR:
3686 if ((code0 == POINTER_TYPE || code1 == POINTER_TYPE)
3687 && (code0 == INTEGER_TYPE || code1 == INTEGER_TYPE))
3689 tree ptr_operand;
3690 tree int_operand;
3691 ptr_operand = ((code0 == POINTER_TYPE) ? op0 : op1);
3692 int_operand = ((code0 == INTEGER_TYPE) ? op0 : op1);
3693 if (processing_template_decl)
3695 result_type = TREE_TYPE (ptr_operand);
3696 break;
3698 return cp_pointer_int_sum (code,
3699 ptr_operand,
3700 int_operand);
3702 common = 1;
3703 break;
3705 case MULT_EXPR:
3706 common = 1;
3707 break;
3709 case TRUNC_DIV_EXPR:
3710 case CEIL_DIV_EXPR:
3711 case FLOOR_DIV_EXPR:
3712 case ROUND_DIV_EXPR:
3713 case EXACT_DIV_EXPR:
3714 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE
3715 || code0 == COMPLEX_TYPE || code0 == VECTOR_TYPE)
3716 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
3717 || code1 == COMPLEX_TYPE || code1 == VECTOR_TYPE))
3719 enum tree_code tcode0 = code0, tcode1 = code1;
3721 warn_for_div_by_zero (location, op1);
3723 if (tcode0 == COMPLEX_TYPE || tcode0 == VECTOR_TYPE)
3724 tcode0 = TREE_CODE (TREE_TYPE (TREE_TYPE (op0)));
3725 if (tcode1 == COMPLEX_TYPE || tcode1 == VECTOR_TYPE)
3726 tcode1 = TREE_CODE (TREE_TYPE (TREE_TYPE (op1)));
3728 if (!(tcode0 == INTEGER_TYPE && tcode1 == INTEGER_TYPE))
3729 resultcode = RDIV_EXPR;
3730 else
3731 /* When dividing two signed integers, we have to promote to int.
3732 unless we divide by a constant != -1. Note that default
3733 conversion will have been performed on the operands at this
3734 point, so we have to dig out the original type to find out if
3735 it was unsigned. */
3736 shorten = ((TREE_CODE (op0) == NOP_EXPR
3737 && TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (op0, 0))))
3738 || (TREE_CODE (op1) == INTEGER_CST
3739 && ! integer_all_onesp (op1)));
3741 common = 1;
3743 break;
3745 case BIT_AND_EXPR:
3746 case BIT_IOR_EXPR:
3747 case BIT_XOR_EXPR:
3748 if ((code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
3749 || (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE
3750 && !VECTOR_FLOAT_TYPE_P (type0)
3751 && !VECTOR_FLOAT_TYPE_P (type1)))
3752 shorten = -1;
3753 break;
3755 case TRUNC_MOD_EXPR:
3756 case FLOOR_MOD_EXPR:
3757 warn_for_div_by_zero (location, op1);
3759 if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE
3760 && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE
3761 && TREE_CODE (TREE_TYPE (type1)) == INTEGER_TYPE)
3762 common = 1;
3763 else if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
3765 /* Although it would be tempting to shorten always here, that loses
3766 on some targets, since the modulo instruction is undefined if the
3767 quotient can't be represented in the computation mode. We shorten
3768 only if unsigned or if dividing by something we know != -1. */
3769 shorten = ((TREE_CODE (op0) == NOP_EXPR
3770 && TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (op0, 0))))
3771 || (TREE_CODE (op1) == INTEGER_CST
3772 && ! integer_all_onesp (op1)));
3773 common = 1;
3775 break;
3777 case TRUTH_ANDIF_EXPR:
3778 case TRUTH_ORIF_EXPR:
3779 case TRUTH_AND_EXPR:
3780 case TRUTH_OR_EXPR:
3781 result_type = boolean_type_node;
3782 break;
3784 /* Shift operations: result has same type as first operand;
3785 always convert second operand to int.
3786 Also set SHORT_SHIFT if shifting rightward. */
3788 case RSHIFT_EXPR:
3789 if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
3791 result_type = type0;
3792 if (TREE_CODE (op1) == INTEGER_CST)
3794 if (tree_int_cst_lt (op1, integer_zero_node))
3796 if ((complain & tf_warning)
3797 && c_inhibit_evaluation_warnings == 0)
3798 warning (0, "right shift count is negative");
3800 else
3802 if (compare_tree_int (op1, TYPE_PRECISION (type0)) >= 0
3803 && (complain & tf_warning)
3804 && c_inhibit_evaluation_warnings == 0)
3805 warning (0, "right shift count >= width of type");
3808 /* Convert the shift-count to an integer, regardless of
3809 size of value being shifted. */
3810 if (TYPE_MAIN_VARIANT (TREE_TYPE (op1)) != integer_type_node)
3811 op1 = cp_convert (integer_type_node, op1);
3812 /* Avoid converting op1 to result_type later. */
3813 converted = 1;
3815 break;
3817 case LSHIFT_EXPR:
3818 if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
3820 result_type = type0;
3821 if (TREE_CODE (op1) == INTEGER_CST)
3823 if (tree_int_cst_lt (op1, integer_zero_node))
3825 if ((complain & tf_warning)
3826 && c_inhibit_evaluation_warnings == 0)
3827 warning (0, "left shift count is negative");
3829 else if (compare_tree_int (op1, TYPE_PRECISION (type0)) >= 0)
3831 if ((complain & tf_warning)
3832 && c_inhibit_evaluation_warnings == 0)
3833 warning (0, "left shift count >= width of type");
3836 /* Convert the shift-count to an integer, regardless of
3837 size of value being shifted. */
3838 if (TYPE_MAIN_VARIANT (TREE_TYPE (op1)) != integer_type_node)
3839 op1 = cp_convert (integer_type_node, op1);
3840 /* Avoid converting op1 to result_type later. */
3841 converted = 1;
3843 break;
3845 case RROTATE_EXPR:
3846 case LROTATE_EXPR:
3847 if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
3849 result_type = type0;
3850 if (TREE_CODE (op1) == INTEGER_CST)
3852 if (tree_int_cst_lt (op1, integer_zero_node))
3854 if (complain & tf_warning)
3855 warning (0, (code == LROTATE_EXPR)
3856 ? G_("left rotate count is negative")
3857 : G_("right rotate count is negative"));
3859 else if (compare_tree_int (op1, TYPE_PRECISION (type0)) >= 0)
3861 if (complain & tf_warning)
3862 warning (0, (code == LROTATE_EXPR)
3863 ? G_("left rotate count >= width of type")
3864 : G_("right rotate count >= width of type"));
3867 /* Convert the shift-count to an integer, regardless of
3868 size of value being shifted. */
3869 if (TYPE_MAIN_VARIANT (TREE_TYPE (op1)) != integer_type_node)
3870 op1 = cp_convert (integer_type_node, op1);
3872 break;
3874 case EQ_EXPR:
3875 case NE_EXPR:
3876 if ((complain & tf_warning)
3877 && (FLOAT_TYPE_P (type0) || FLOAT_TYPE_P (type1)))
3878 warning (OPT_Wfloat_equal,
3879 "comparing floating point with == or != is unsafe");
3880 if ((complain & tf_warning)
3881 && ((TREE_CODE (orig_op0) == STRING_CST && !integer_zerop (op1))
3882 || (TREE_CODE (orig_op1) == STRING_CST && !integer_zerop (op0))))
3883 warning (OPT_Waddress, "comparison with string literal results in unspecified behaviour");
3885 build_type = boolean_type_node;
3886 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE
3887 || code0 == COMPLEX_TYPE || code0 == ENUMERAL_TYPE)
3888 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
3889 || code1 == COMPLEX_TYPE || code1 == ENUMERAL_TYPE))
3890 short_compare = 1;
3891 else if ((code0 == POINTER_TYPE && code1 == POINTER_TYPE)
3892 || (TYPE_PTRMEM_P (type0) && TYPE_PTRMEM_P (type1)))
3893 result_type = composite_pointer_type (type0, type1, op0, op1,
3894 CPO_COMPARISON, complain);
3895 else if ((code0 == POINTER_TYPE || TYPE_PTRMEM_P (type0))
3896 && null_ptr_cst_p (op1))
3898 if (TREE_CODE (op0) == ADDR_EXPR
3899 && decl_with_nonnull_addr_p (TREE_OPERAND (op0, 0)))
3901 if (complain & tf_warning)
3902 warning (OPT_Waddress, "the address of %qD will never be NULL",
3903 TREE_OPERAND (op0, 0));
3905 result_type = type0;
3907 else if ((code1 == POINTER_TYPE || TYPE_PTRMEM_P (type1))
3908 && null_ptr_cst_p (op0))
3910 if (TREE_CODE (op1) == ADDR_EXPR
3911 && decl_with_nonnull_addr_p (TREE_OPERAND (op1, 0)))
3913 if (complain & tf_warning)
3914 warning (OPT_Waddress, "the address of %qD will never be NULL",
3915 TREE_OPERAND (op1, 0));
3917 result_type = type1;
3919 else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
3921 result_type = type0;
3922 if (complain & tf_error)
3923 permerror (input_location, "ISO C++ forbids comparison between pointer and integer");
3924 else
3925 return error_mark_node;
3927 else if (code0 == INTEGER_TYPE && code1 == POINTER_TYPE)
3929 result_type = type1;
3930 if (complain & tf_error)
3931 permerror (input_location, "ISO C++ forbids comparison between pointer and integer");
3932 else
3933 return error_mark_node;
3935 else if (TYPE_PTRMEMFUNC_P (type0) && null_ptr_cst_p (op1))
3937 if (TARGET_PTRMEMFUNC_VBIT_LOCATION
3938 == ptrmemfunc_vbit_in_delta)
3940 tree pfn0 = pfn_from_ptrmemfunc (op0);
3941 tree delta0 = delta_from_ptrmemfunc (op0);
3942 tree e1 = cp_build_binary_op (location,
3943 EQ_EXPR,
3944 pfn0,
3945 fold_convert (TREE_TYPE (pfn0),
3946 integer_zero_node),
3947 complain);
3948 tree e2 = cp_build_binary_op (location,
3949 BIT_AND_EXPR,
3950 delta0,
3951 integer_one_node,
3952 complain);
3953 e2 = cp_build_binary_op (location,
3954 EQ_EXPR, e2, integer_zero_node,
3955 complain);
3956 op0 = cp_build_binary_op (location,
3957 TRUTH_ANDIF_EXPR, e1, e2,
3958 complain);
3959 op1 = cp_convert (TREE_TYPE (op0), integer_one_node);
3961 else
3963 op0 = build_ptrmemfunc_access_expr (op0, pfn_identifier);
3964 op1 = cp_convert (TREE_TYPE (op0), integer_zero_node);
3966 result_type = TREE_TYPE (op0);
3968 else if (TYPE_PTRMEMFUNC_P (type1) && null_ptr_cst_p (op0))
3969 return cp_build_binary_op (location, code, op1, op0, complain);
3970 else if (TYPE_PTRMEMFUNC_P (type0) && TYPE_PTRMEMFUNC_P (type1))
3972 tree type;
3973 /* E will be the final comparison. */
3974 tree e;
3975 /* E1 and E2 are for scratch. */
3976 tree e1;
3977 tree e2;
3978 tree pfn0;
3979 tree pfn1;
3980 tree delta0;
3981 tree delta1;
3983 type = composite_pointer_type (type0, type1, op0, op1,
3984 CPO_COMPARISON, complain);
3986 if (!same_type_p (TREE_TYPE (op0), type))
3987 op0 = cp_convert_and_check (type, op0);
3988 if (!same_type_p (TREE_TYPE (op1), type))
3989 op1 = cp_convert_and_check (type, op1);
3991 if (op0 == error_mark_node || op1 == error_mark_node)
3992 return error_mark_node;
3994 if (TREE_SIDE_EFFECTS (op0))
3995 op0 = save_expr (op0);
3996 if (TREE_SIDE_EFFECTS (op1))
3997 op1 = save_expr (op1);
3999 pfn0 = pfn_from_ptrmemfunc (op0);
4000 pfn1 = pfn_from_ptrmemfunc (op1);
4001 delta0 = delta_from_ptrmemfunc (op0);
4002 delta1 = delta_from_ptrmemfunc (op1);
4003 if (TARGET_PTRMEMFUNC_VBIT_LOCATION
4004 == ptrmemfunc_vbit_in_delta)
4006 /* We generate:
4008 (op0.pfn == op1.pfn
4009 && ((op0.delta == op1.delta)
4010 || (!op0.pfn && op0.delta & 1 == 0
4011 && op1.delta & 1 == 0))
4013 The reason for the `!op0.pfn' bit is that a NULL
4014 pointer-to-member is any member with a zero PFN and
4015 LSB of the DELTA field is 0. */
4017 e1 = cp_build_binary_op (location, BIT_AND_EXPR,
4018 delta0,
4019 integer_one_node,
4020 complain);
4021 e1 = cp_build_binary_op (location,
4022 EQ_EXPR, e1, integer_zero_node,
4023 complain);
4024 e2 = cp_build_binary_op (location, BIT_AND_EXPR,
4025 delta1,
4026 integer_one_node,
4027 complain);
4028 e2 = cp_build_binary_op (location,
4029 EQ_EXPR, e2, integer_zero_node,
4030 complain);
4031 e1 = cp_build_binary_op (location,
4032 TRUTH_ANDIF_EXPR, e2, e1,
4033 complain);
4034 e2 = cp_build_binary_op (location, EQ_EXPR,
4035 pfn0,
4036 fold_convert (TREE_TYPE (pfn0),
4037 integer_zero_node),
4038 complain);
4039 e2 = cp_build_binary_op (location,
4040 TRUTH_ANDIF_EXPR, e2, e1, complain);
4041 e1 = cp_build_binary_op (location,
4042 EQ_EXPR, delta0, delta1, complain);
4043 e1 = cp_build_binary_op (location,
4044 TRUTH_ORIF_EXPR, e1, e2, complain);
4046 else
4048 /* We generate:
4050 (op0.pfn == op1.pfn
4051 && (!op0.pfn || op0.delta == op1.delta))
4053 The reason for the `!op0.pfn' bit is that a NULL
4054 pointer-to-member is any member with a zero PFN; the
4055 DELTA field is unspecified. */
4057 e1 = cp_build_binary_op (location,
4058 EQ_EXPR, delta0, delta1, complain);
4059 e2 = cp_build_binary_op (location,
4060 EQ_EXPR,
4061 pfn0,
4062 fold_convert (TREE_TYPE (pfn0),
4063 integer_zero_node),
4064 complain);
4065 e1 = cp_build_binary_op (location,
4066 TRUTH_ORIF_EXPR, e1, e2, complain);
4068 e2 = build2 (EQ_EXPR, boolean_type_node, pfn0, pfn1);
4069 e = cp_build_binary_op (location,
4070 TRUTH_ANDIF_EXPR, e2, e1, complain);
4071 if (code == EQ_EXPR)
4072 return e;
4073 return cp_build_binary_op (location,
4074 EQ_EXPR, e, integer_zero_node, complain);
4076 else
4078 gcc_assert (!TYPE_PTRMEMFUNC_P (type0)
4079 || !same_type_p (TYPE_PTRMEMFUNC_FN_TYPE (type0),
4080 type1));
4081 gcc_assert (!TYPE_PTRMEMFUNC_P (type1)
4082 || !same_type_p (TYPE_PTRMEMFUNC_FN_TYPE (type1),
4083 type0));
4086 break;
4088 case MAX_EXPR:
4089 case MIN_EXPR:
4090 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE)
4091 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE))
4092 shorten = 1;
4093 else if (code0 == POINTER_TYPE && code1 == POINTER_TYPE)
4094 result_type = composite_pointer_type (type0, type1, op0, op1,
4095 CPO_COMPARISON, complain);
4096 break;
4098 case LE_EXPR:
4099 case GE_EXPR:
4100 case LT_EXPR:
4101 case GT_EXPR:
4102 if (TREE_CODE (orig_op0) == STRING_CST
4103 || TREE_CODE (orig_op1) == STRING_CST)
4105 if (complain & tf_warning)
4106 warning (OPT_Waddress, "comparison with string literal results in unspecified behaviour");
4109 build_type = boolean_type_node;
4110 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE)
4111 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE))
4112 short_compare = 1;
4113 else if (code0 == POINTER_TYPE && code1 == POINTER_TYPE)
4114 result_type = composite_pointer_type (type0, type1, op0, op1,
4115 CPO_COMPARISON, complain);
4116 else if (code0 == POINTER_TYPE && TREE_CODE (op1) == INTEGER_CST
4117 && integer_zerop (op1))
4118 result_type = type0;
4119 else if (code1 == POINTER_TYPE && TREE_CODE (op0) == INTEGER_CST
4120 && integer_zerop (op0))
4121 result_type = type1;
4122 else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
4124 result_type = type0;
4125 if (complain & tf_error)
4126 permerror (input_location, "ISO C++ forbids comparison between pointer and integer");
4127 else
4128 return error_mark_node;
4130 else if (code0 == INTEGER_TYPE && code1 == POINTER_TYPE)
4132 result_type = type1;
4133 if (complain & tf_error)
4134 permerror (input_location, "ISO C++ forbids comparison between pointer and integer");
4135 else
4136 return error_mark_node;
4138 break;
4140 case UNORDERED_EXPR:
4141 case ORDERED_EXPR:
4142 case UNLT_EXPR:
4143 case UNLE_EXPR:
4144 case UNGT_EXPR:
4145 case UNGE_EXPR:
4146 case UNEQ_EXPR:
4147 build_type = integer_type_node;
4148 if (code0 != REAL_TYPE || code1 != REAL_TYPE)
4150 if (complain & tf_error)
4151 error ("unordered comparison on non-floating point argument");
4152 return error_mark_node;
4154 common = 1;
4155 break;
4157 default:
4158 break;
4161 if (((code0 == INTEGER_TYPE || code0 == REAL_TYPE || code0 == COMPLEX_TYPE
4162 || code0 == ENUMERAL_TYPE)
4163 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
4164 || code1 == COMPLEX_TYPE || code1 == ENUMERAL_TYPE)))
4165 arithmetic_types_p = 1;
4166 else
4168 arithmetic_types_p = 0;
4169 /* Vector arithmetic is only allowed when both sides are vectors. */
4170 if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE)
4172 if (!tree_int_cst_equal (TYPE_SIZE (type0), TYPE_SIZE (type1))
4173 || !same_scalar_type_ignoring_signedness (TREE_TYPE (type0),
4174 TREE_TYPE (type1)))
4176 binary_op_error (location, code, type0, type1);
4177 return error_mark_node;
4179 arithmetic_types_p = 1;
4182 /* Determine the RESULT_TYPE, if it is not already known. */
4183 if (!result_type
4184 && arithmetic_types_p
4185 && (shorten || common || short_compare))
4186 result_type = cp_common_type (type0, type1);
4188 if (!result_type)
4190 if (complain & tf_error)
4191 error ("invalid operands of types %qT and %qT to binary %qO",
4192 TREE_TYPE (orig_op0), TREE_TYPE (orig_op1), code);
4193 return error_mark_node;
4196 /* If we're in a template, the only thing we need to know is the
4197 RESULT_TYPE. */
4198 if (processing_template_decl)
4200 /* Since the middle-end checks the type when doing a build2, we
4201 need to build the tree in pieces. This built tree will never
4202 get out of the front-end as we replace it when instantiating
4203 the template. */
4204 tree tmp = build2 (resultcode,
4205 build_type ? build_type : result_type,
4206 NULL_TREE, op1);
4207 TREE_OPERAND (tmp, 0) = op0;
4208 return tmp;
4211 if (arithmetic_types_p)
4213 int none_complex = (code0 != COMPLEX_TYPE && code1 != COMPLEX_TYPE);
4215 /* For certain operations (which identify themselves by shorten != 0)
4216 if both args were extended from the same smaller type,
4217 do the arithmetic in that type and then extend.
4219 shorten !=0 and !=1 indicates a bitwise operation.
4220 For them, this optimization is safe only if
4221 both args are zero-extended or both are sign-extended.
4222 Otherwise, we might change the result.
4223 E.g., (short)-1 | (unsigned short)-1 is (int)-1
4224 but calculated in (unsigned short) it would be (unsigned short)-1. */
4226 if (shorten && none_complex)
4228 final_type = result_type;
4229 result_type = shorten_binary_op (result_type, op0, op1,
4230 shorten == -1);
4233 /* Comparison operations are shortened too but differently.
4234 They identify themselves by setting short_compare = 1. */
4236 if (short_compare)
4238 /* Don't write &op0, etc., because that would prevent op0
4239 from being kept in a register.
4240 Instead, make copies of the our local variables and
4241 pass the copies by reference, then copy them back afterward. */
4242 tree xop0 = op0, xop1 = op1, xresult_type = result_type;
4243 enum tree_code xresultcode = resultcode;
4244 tree val
4245 = shorten_compare (&xop0, &xop1, &xresult_type, &xresultcode);
4246 if (val != 0)
4247 return cp_convert (boolean_type_node, val);
4248 op0 = xop0, op1 = xop1;
4249 converted = 1;
4250 resultcode = xresultcode;
4253 if ((short_compare || code == MIN_EXPR || code == MAX_EXPR)
4254 && warn_sign_compare
4255 && !TREE_NO_WARNING (orig_op0)
4256 && !TREE_NO_WARNING (orig_op1)
4257 /* Do not warn until the template is instantiated; we cannot
4258 bound the ranges of the arguments until that point. */
4259 && !processing_template_decl
4260 && (complain & tf_warning)
4261 && c_inhibit_evaluation_warnings == 0)
4263 warn_for_sign_compare (location, orig_op0, orig_op1, op0, op1,
4264 result_type, resultcode);
4268 /* If CONVERTED is zero, both args will be converted to type RESULT_TYPE.
4269 Then the expression will be built.
4270 It will be given type FINAL_TYPE if that is nonzero;
4271 otherwise, it will be given type RESULT_TYPE. */
4272 if (! converted)
4274 if (TREE_TYPE (op0) != result_type)
4275 op0 = cp_convert_and_check (result_type, op0);
4276 if (TREE_TYPE (op1) != result_type)
4277 op1 = cp_convert_and_check (result_type, op1);
4279 if (op0 == error_mark_node || op1 == error_mark_node)
4280 return error_mark_node;
4283 if (build_type == NULL_TREE)
4284 build_type = result_type;
4286 result = build2 (resultcode, build_type, op0, op1);
4287 result = fold_if_not_in_template (result);
4288 if (final_type != 0)
4289 result = cp_convert (final_type, result);
4291 if (TREE_OVERFLOW_P (result)
4292 && !TREE_OVERFLOW_P (op0)
4293 && !TREE_OVERFLOW_P (op1))
4294 overflow_warning (location, result);
4296 return result;
4299 /* Return a tree for the sum or difference (RESULTCODE says which)
4300 of pointer PTROP and integer INTOP. */
4302 static tree
4303 cp_pointer_int_sum (enum tree_code resultcode, tree ptrop, tree intop)
4305 tree res_type = TREE_TYPE (ptrop);
4307 /* pointer_int_sum() uses size_in_bytes() on the TREE_TYPE(res_type)
4308 in certain circumstance (when it's valid to do so). So we need
4309 to make sure it's complete. We don't need to check here, if we
4310 can actually complete it at all, as those checks will be done in
4311 pointer_int_sum() anyway. */
4312 complete_type (TREE_TYPE (res_type));
4314 return pointer_int_sum (input_location, resultcode, ptrop,
4315 fold_if_not_in_template (intop));
4318 /* Return a tree for the difference of pointers OP0 and OP1.
4319 The resulting tree has type int. */
4321 static tree
4322 pointer_diff (tree op0, tree op1, tree ptrtype)
4324 tree result;
4325 tree restype = ptrdiff_type_node;
4326 tree target_type = TREE_TYPE (ptrtype);
4328 if (!complete_type_or_else (target_type, NULL_TREE))
4329 return error_mark_node;
4331 if (TREE_CODE (target_type) == VOID_TYPE)
4332 permerror (input_location, "ISO C++ forbids using pointer of type %<void *%> in subtraction");
4333 if (TREE_CODE (target_type) == FUNCTION_TYPE)
4334 permerror (input_location, "ISO C++ forbids using pointer to a function in subtraction");
4335 if (TREE_CODE (target_type) == METHOD_TYPE)
4336 permerror (input_location, "ISO C++ forbids using pointer to a method in subtraction");
4338 /* First do the subtraction as integers;
4339 then drop through to build the divide operator. */
4341 op0 = cp_build_binary_op (input_location,
4342 MINUS_EXPR,
4343 cp_convert (restype, op0),
4344 cp_convert (restype, op1),
4345 tf_warning_or_error);
4347 /* This generates an error if op1 is a pointer to an incomplete type. */
4348 if (!COMPLETE_TYPE_P (TREE_TYPE (TREE_TYPE (op1))))
4349 error ("invalid use of a pointer to an incomplete type in pointer arithmetic");
4351 op1 = (TYPE_PTROB_P (ptrtype)
4352 ? size_in_bytes (target_type)
4353 : integer_one_node);
4355 /* Do the division. */
4357 result = build2 (EXACT_DIV_EXPR, restype, op0, cp_convert (restype, op1));
4358 return fold_if_not_in_template (result);
4361 /* Construct and perhaps optimize a tree representation
4362 for a unary operation. CODE, a tree_code, specifies the operation
4363 and XARG is the operand. */
4365 tree
4366 build_x_unary_op (enum tree_code code, tree xarg, tsubst_flags_t complain)
4368 tree orig_expr = xarg;
4369 tree exp;
4370 int ptrmem = 0;
4372 if (processing_template_decl)
4374 if (type_dependent_expression_p (xarg))
4375 return build_min_nt (code, xarg, NULL_TREE);
4377 xarg = build_non_dependent_expr (xarg);
4380 exp = NULL_TREE;
4382 /* [expr.unary.op] says:
4384 The address of an object of incomplete type can be taken.
4386 (And is just the ordinary address operator, not an overloaded
4387 "operator &".) However, if the type is a template
4388 specialization, we must complete the type at this point so that
4389 an overloaded "operator &" will be available if required. */
4390 if (code == ADDR_EXPR
4391 && TREE_CODE (xarg) != TEMPLATE_ID_EXPR
4392 && ((CLASS_TYPE_P (TREE_TYPE (xarg))
4393 && !COMPLETE_TYPE_P (complete_type (TREE_TYPE (xarg))))
4394 || (TREE_CODE (xarg) == OFFSET_REF)))
4395 /* Don't look for a function. */;
4396 else
4397 exp = build_new_op (code, LOOKUP_NORMAL, xarg, NULL_TREE, NULL_TREE,
4398 /*overloaded_p=*/NULL, complain);
4399 if (!exp && code == ADDR_EXPR)
4401 if (is_overloaded_fn (xarg))
4403 tree fn = get_first_fn (xarg);
4404 if (DECL_CONSTRUCTOR_P (fn) || DECL_DESTRUCTOR_P (fn))
4406 error (DECL_CONSTRUCTOR_P (fn)
4407 ? G_("taking address of constructor %qE")
4408 : G_("taking address of destructor %qE"),
4409 xarg);
4410 return error_mark_node;
4414 /* A pointer to member-function can be formed only by saying
4415 &X::mf. */
4416 if (!flag_ms_extensions && TREE_CODE (TREE_TYPE (xarg)) == METHOD_TYPE
4417 && (TREE_CODE (xarg) != OFFSET_REF || !PTRMEM_OK_P (xarg)))
4419 if (TREE_CODE (xarg) != OFFSET_REF
4420 || !TYPE_P (TREE_OPERAND (xarg, 0)))
4422 error ("invalid use of %qE to form a pointer-to-member-function",
4423 xarg);
4424 if (TREE_CODE (xarg) != OFFSET_REF)
4425 inform (input_location, " a qualified-id is required");
4426 return error_mark_node;
4428 else
4430 error ("parentheses around %qE cannot be used to form a"
4431 " pointer-to-member-function",
4432 xarg);
4433 PTRMEM_OK_P (xarg) = 1;
4437 if (TREE_CODE (xarg) == OFFSET_REF)
4439 ptrmem = PTRMEM_OK_P (xarg);
4441 if (!ptrmem && !flag_ms_extensions
4442 && TREE_CODE (TREE_TYPE (TREE_OPERAND (xarg, 1))) == METHOD_TYPE)
4444 /* A single non-static member, make sure we don't allow a
4445 pointer-to-member. */
4446 xarg = build2 (OFFSET_REF, TREE_TYPE (xarg),
4447 TREE_OPERAND (xarg, 0),
4448 ovl_cons (TREE_OPERAND (xarg, 1), NULL_TREE));
4449 PTRMEM_OK_P (xarg) = ptrmem;
4452 else if (TREE_CODE (xarg) == TARGET_EXPR && (complain & tf_warning))
4453 warning (0, "taking address of temporary");
4454 exp = cp_build_unary_op (ADDR_EXPR, xarg, 0, complain);
4457 if (processing_template_decl && exp != error_mark_node)
4458 exp = build_min_non_dep (code, exp, orig_expr,
4459 /*For {PRE,POST}{INC,DEC}REMENT_EXPR*/NULL_TREE);
4460 if (TREE_CODE (exp) == ADDR_EXPR)
4461 PTRMEM_OK_P (exp) = ptrmem;
4462 return exp;
4465 /* Like c_common_truthvalue_conversion, but handle pointer-to-member
4466 constants, where a null value is represented by an INTEGER_CST of
4467 -1. */
4469 tree
4470 cp_truthvalue_conversion (tree expr)
4472 tree type = TREE_TYPE (expr);
4473 if (TYPE_PTRMEM_P (type))
4474 return build_binary_op (EXPR_LOCATION (expr),
4475 NE_EXPR, expr, integer_zero_node, 1);
4476 else
4477 return c_common_truthvalue_conversion (input_location, expr);
4480 /* Just like cp_truthvalue_conversion, but we want a CLEANUP_POINT_EXPR. */
4482 tree
4483 condition_conversion (tree expr)
4485 tree t;
4486 if (processing_template_decl)
4487 return expr;
4488 t = perform_implicit_conversion_flags (boolean_type_node, expr,
4489 tf_warning_or_error, LOOKUP_NORMAL);
4490 t = fold_build_cleanup_point_expr (boolean_type_node, t);
4491 return t;
4494 /* Returns the address of T. This function will fold away
4495 ADDR_EXPR of INDIRECT_REF. */
4497 tree
4498 build_address (tree t)
4500 if (error_operand_p (t) || !cxx_mark_addressable (t))
4501 return error_mark_node;
4502 t = build_fold_addr_expr (t);
4503 if (TREE_CODE (t) != ADDR_EXPR)
4504 t = rvalue (t);
4505 return t;
4508 /* Returns the address of T with type TYPE. */
4510 tree
4511 build_typed_address (tree t, tree type)
4513 if (error_operand_p (t) || !cxx_mark_addressable (t))
4514 return error_mark_node;
4515 t = build_fold_addr_expr_with_type (t, type);
4516 if (TREE_CODE (t) != ADDR_EXPR)
4517 t = rvalue (t);
4518 return t;
4521 /* Return a NOP_EXPR converting EXPR to TYPE. */
4523 tree
4524 build_nop (tree type, tree expr)
4526 if (type == error_mark_node || error_operand_p (expr))
4527 return expr;
4528 return build1 (NOP_EXPR, type, expr);
4531 /* C++: Must handle pointers to members.
4533 Perhaps type instantiation should be extended to handle conversion
4534 from aggregates to types we don't yet know we want? (Or are those
4535 cases typically errors which should be reported?)
4537 NOCONVERT nonzero suppresses the default promotions
4538 (such as from short to int). */
4540 tree
4541 cp_build_unary_op (enum tree_code code, tree xarg, int noconvert,
4542 tsubst_flags_t complain)
4544 /* No default_conversion here. It causes trouble for ADDR_EXPR. */
4545 tree arg = xarg;
4546 tree argtype = 0;
4547 const char *errstring = NULL;
4548 tree val;
4549 const char *invalid_op_diag;
4551 if (error_operand_p (arg))
4552 return error_mark_node;
4554 if ((invalid_op_diag
4555 = targetm.invalid_unary_op ((code == UNARY_PLUS_EXPR
4556 ? CONVERT_EXPR
4557 : code),
4558 TREE_TYPE (xarg))))
4560 error (invalid_op_diag);
4561 return error_mark_node;
4564 switch (code)
4566 case UNARY_PLUS_EXPR:
4567 case NEGATE_EXPR:
4569 int flags = WANT_ARITH | WANT_ENUM;
4570 /* Unary plus (but not unary minus) is allowed on pointers. */
4571 if (code == UNARY_PLUS_EXPR)
4572 flags |= WANT_POINTER;
4573 arg = build_expr_type_conversion (flags, arg, true);
4574 if (!arg)
4575 errstring = (code == NEGATE_EXPR
4576 ? _("wrong type argument to unary minus")
4577 : _("wrong type argument to unary plus"));
4578 else
4580 if (!noconvert && CP_INTEGRAL_TYPE_P (TREE_TYPE (arg)))
4581 arg = perform_integral_promotions (arg);
4583 /* Make sure the result is not an lvalue: a unary plus or minus
4584 expression is always a rvalue. */
4585 arg = rvalue (arg);
4588 break;
4590 case BIT_NOT_EXPR:
4591 if (TREE_CODE (TREE_TYPE (arg)) == COMPLEX_TYPE)
4593 code = CONJ_EXPR;
4594 if (!noconvert)
4595 arg = default_conversion (arg);
4597 else if (!(arg = build_expr_type_conversion (WANT_INT | WANT_ENUM
4598 | WANT_VECTOR,
4599 arg, true)))
4600 errstring = _("wrong type argument to bit-complement");
4601 else if (!noconvert && CP_INTEGRAL_TYPE_P (TREE_TYPE (arg)))
4602 arg = perform_integral_promotions (arg);
4603 break;
4605 case ABS_EXPR:
4606 if (!(arg = build_expr_type_conversion (WANT_ARITH | WANT_ENUM, arg, true)))
4607 errstring = _("wrong type argument to abs");
4608 else if (!noconvert)
4609 arg = default_conversion (arg);
4610 break;
4612 case CONJ_EXPR:
4613 /* Conjugating a real value is a no-op, but allow it anyway. */
4614 if (!(arg = build_expr_type_conversion (WANT_ARITH | WANT_ENUM, arg, true)))
4615 errstring = _("wrong type argument to conjugation");
4616 else if (!noconvert)
4617 arg = default_conversion (arg);
4618 break;
4620 case TRUTH_NOT_EXPR:
4621 arg = perform_implicit_conversion (boolean_type_node, arg,
4622 complain);
4623 val = invert_truthvalue_loc (input_location, arg);
4624 if (arg != error_mark_node)
4625 return val;
4626 errstring = _("in argument to unary !");
4627 break;
4629 case NOP_EXPR:
4630 break;
4632 case REALPART_EXPR:
4633 if (TREE_CODE (arg) == COMPLEX_CST)
4634 return TREE_REALPART (arg);
4635 else if (TREE_CODE (TREE_TYPE (arg)) == COMPLEX_TYPE)
4637 arg = build1 (REALPART_EXPR, TREE_TYPE (TREE_TYPE (arg)), arg);
4638 return fold_if_not_in_template (arg);
4640 else
4641 return arg;
4643 case IMAGPART_EXPR:
4644 if (TREE_CODE (arg) == COMPLEX_CST)
4645 return TREE_IMAGPART (arg);
4646 else if (TREE_CODE (TREE_TYPE (arg)) == COMPLEX_TYPE)
4648 arg = build1 (IMAGPART_EXPR, TREE_TYPE (TREE_TYPE (arg)), arg);
4649 return fold_if_not_in_template (arg);
4651 else
4652 return cp_convert (TREE_TYPE (arg), integer_zero_node);
4654 case PREINCREMENT_EXPR:
4655 case POSTINCREMENT_EXPR:
4656 case PREDECREMENT_EXPR:
4657 case POSTDECREMENT_EXPR:
4658 /* Handle complex lvalues (when permitted)
4659 by reduction to simpler cases. */
4661 val = unary_complex_lvalue (code, arg);
4662 if (val != 0)
4663 return val;
4665 /* Increment or decrement the real part of the value,
4666 and don't change the imaginary part. */
4667 if (TREE_CODE (TREE_TYPE (arg)) == COMPLEX_TYPE)
4669 tree real, imag;
4671 arg = stabilize_reference (arg);
4672 real = cp_build_unary_op (REALPART_EXPR, arg, 1, complain);
4673 imag = cp_build_unary_op (IMAGPART_EXPR, arg, 1, complain);
4674 real = cp_build_unary_op (code, real, 1, complain);
4675 if (real == error_mark_node || imag == error_mark_node)
4676 return error_mark_node;
4677 return build2 (COMPLEX_EXPR, TREE_TYPE (arg),
4678 real, imag);
4681 /* Report invalid types. */
4683 if (!(arg = build_expr_type_conversion (WANT_ARITH | WANT_POINTER,
4684 arg, true)))
4686 if (code == PREINCREMENT_EXPR)
4687 errstring = _("no pre-increment operator for type");
4688 else if (code == POSTINCREMENT_EXPR)
4689 errstring = _("no post-increment operator for type");
4690 else if (code == PREDECREMENT_EXPR)
4691 errstring = _("no pre-decrement operator for type");
4692 else
4693 errstring = _("no post-decrement operator for type");
4694 break;
4696 else if (arg == error_mark_node)
4697 return error_mark_node;
4699 /* Report something read-only. */
4701 if (CP_TYPE_CONST_P (TREE_TYPE (arg))
4702 || TREE_READONLY (arg))
4704 if (complain & tf_error)
4705 readonly_error (arg, ((code == PREINCREMENT_EXPR
4706 || code == POSTINCREMENT_EXPR)
4707 ? REK_INCREMENT : REK_DECREMENT));
4708 else
4709 return error_mark_node;
4713 tree inc;
4714 tree declared_type = unlowered_expr_type (arg);
4716 argtype = TREE_TYPE (arg);
4718 /* ARM $5.2.5 last annotation says this should be forbidden. */
4719 if (TREE_CODE (argtype) == ENUMERAL_TYPE)
4721 if (complain & tf_error)
4722 permerror (input_location, (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
4723 ? G_("ISO C++ forbids incrementing an enum")
4724 : G_("ISO C++ forbids decrementing an enum"));
4725 else
4726 return error_mark_node;
4729 /* Compute the increment. */
4731 if (TREE_CODE (argtype) == POINTER_TYPE)
4733 tree type = complete_type (TREE_TYPE (argtype));
4735 if (!COMPLETE_OR_VOID_TYPE_P (type))
4737 if (complain & tf_error)
4738 error (((code == PREINCREMENT_EXPR
4739 || code == POSTINCREMENT_EXPR))
4740 ? G_("cannot increment a pointer to incomplete type %qT")
4741 : G_("cannot decrement a pointer to incomplete type %qT"),
4742 TREE_TYPE (argtype));
4743 else
4744 return error_mark_node;
4746 else if ((pedantic || warn_pointer_arith)
4747 && !TYPE_PTROB_P (argtype))
4749 if (complain & tf_error)
4750 permerror (input_location, (code == PREINCREMENT_EXPR
4751 || code == POSTINCREMENT_EXPR)
4752 ? G_("ISO C++ forbids incrementing a pointer of type %qT")
4753 : G_("ISO C++ forbids decrementing a pointer of type %qT"),
4754 argtype);
4755 else
4756 return error_mark_node;
4759 inc = cxx_sizeof_nowarn (TREE_TYPE (argtype));
4761 else
4762 inc = integer_one_node;
4764 inc = cp_convert (argtype, inc);
4766 /* Complain about anything else that is not a true lvalue. */
4767 if (!lvalue_or_else (arg, ((code == PREINCREMENT_EXPR
4768 || code == POSTINCREMENT_EXPR)
4769 ? lv_increment : lv_decrement),
4770 complain))
4771 return error_mark_node;
4773 /* Forbid using -- on `bool'. */
4774 if (TREE_CODE (declared_type) == BOOLEAN_TYPE)
4776 if (code == POSTDECREMENT_EXPR || code == PREDECREMENT_EXPR)
4778 if (complain & tf_error)
4779 error ("invalid use of Boolean expression as operand "
4780 "to %<operator--%>");
4781 return error_mark_node;
4783 val = boolean_increment (code, arg);
4785 else
4786 val = build2 (code, TREE_TYPE (arg), arg, inc);
4788 TREE_SIDE_EFFECTS (val) = 1;
4789 return val;
4792 case ADDR_EXPR:
4793 /* Note that this operation never does default_conversion
4794 regardless of NOCONVERT. */
4796 argtype = lvalue_type (arg);
4798 if (TREE_CODE (arg) == OFFSET_REF)
4799 goto offset_ref;
4801 if (TREE_CODE (argtype) == REFERENCE_TYPE)
4803 tree type = build_pointer_type (TREE_TYPE (argtype));
4804 arg = build1 (CONVERT_EXPR, type, arg);
4805 return arg;
4807 else if (pedantic && DECL_MAIN_P (arg))
4809 /* ARM $3.4 */
4810 /* Apparently a lot of autoconf scripts for C++ packages do this,
4811 so only complain if -pedantic. */
4812 if (complain & (flag_pedantic_errors ? tf_error : tf_warning))
4813 pedwarn (input_location, OPT_pedantic,
4814 "ISO C++ forbids taking address of function %<::main%>");
4815 else if (flag_pedantic_errors)
4816 return error_mark_node;
4819 /* Let &* cancel out to simplify resulting code. */
4820 if (TREE_CODE (arg) == INDIRECT_REF)
4822 /* We don't need to have `current_class_ptr' wrapped in a
4823 NON_LVALUE_EXPR node. */
4824 if (arg == current_class_ref)
4825 return current_class_ptr;
4827 arg = TREE_OPERAND (arg, 0);
4828 if (TREE_CODE (TREE_TYPE (arg)) == REFERENCE_TYPE)
4830 tree type = build_pointer_type (TREE_TYPE (TREE_TYPE (arg)));
4831 arg = build1 (CONVERT_EXPR, type, arg);
4833 else
4834 /* Don't let this be an lvalue. */
4835 arg = rvalue (arg);
4836 return arg;
4839 /* Uninstantiated types are all functions. Taking the
4840 address of a function is a no-op, so just return the
4841 argument. */
4843 gcc_assert (TREE_CODE (arg) != IDENTIFIER_NODE
4844 || !IDENTIFIER_OPNAME_P (arg));
4846 if (TREE_CODE (arg) == COMPONENT_REF && type_unknown_p (arg)
4847 && !really_overloaded_fn (TREE_OPERAND (arg, 1)))
4849 /* They're trying to take the address of a unique non-static
4850 member function. This is ill-formed (except in MS-land),
4851 but let's try to DTRT.
4852 Note: We only handle unique functions here because we don't
4853 want to complain if there's a static overload; non-unique
4854 cases will be handled by instantiate_type. But we need to
4855 handle this case here to allow casts on the resulting PMF.
4856 We could defer this in non-MS mode, but it's easier to give
4857 a useful error here. */
4859 /* Inside constant member functions, the `this' pointer
4860 contains an extra const qualifier. TYPE_MAIN_VARIANT
4861 is used here to remove this const from the diagnostics
4862 and the created OFFSET_REF. */
4863 tree base = TYPE_MAIN_VARIANT (TREE_TYPE (TREE_OPERAND (arg, 0)));
4864 tree fn = get_first_fn (TREE_OPERAND (arg, 1));
4865 mark_used (fn);
4867 if (! flag_ms_extensions)
4869 tree name = DECL_NAME (fn);
4870 if (!(complain & tf_error))
4871 return error_mark_node;
4872 else if (current_class_type
4873 && TREE_OPERAND (arg, 0) == current_class_ref)
4874 /* An expression like &memfn. */
4875 permerror (input_location, "ISO C++ forbids taking the address of an unqualified"
4876 " or parenthesized non-static member function to form"
4877 " a pointer to member function. Say %<&%T::%D%>",
4878 base, name);
4879 else
4880 permerror (input_location, "ISO C++ forbids taking the address of a bound member"
4881 " function to form a pointer to member function."
4882 " Say %<&%T::%D%>",
4883 base, name);
4885 arg = build_offset_ref (base, fn, /*address_p=*/true);
4888 offset_ref:
4889 if (type_unknown_p (arg))
4890 return build1 (ADDR_EXPR, unknown_type_node, arg);
4892 /* Handle complex lvalues (when permitted)
4893 by reduction to simpler cases. */
4894 val = unary_complex_lvalue (code, arg);
4895 if (val != 0)
4896 return val;
4898 switch (TREE_CODE (arg))
4900 CASE_CONVERT:
4901 case FLOAT_EXPR:
4902 case FIX_TRUNC_EXPR:
4903 /* Even if we're not being pedantic, we cannot allow this
4904 extension when we're instantiating in a SFINAE
4905 context. */
4906 if (! lvalue_p (arg) && complain == tf_none)
4908 if (complain & tf_error)
4909 permerror (input_location, "ISO C++ forbids taking the address of a cast to a non-lvalue expression");
4910 else
4911 return error_mark_node;
4913 break;
4915 case BASELINK:
4916 arg = BASELINK_FUNCTIONS (arg);
4917 /* Fall through. */
4919 case OVERLOAD:
4920 arg = OVL_CURRENT (arg);
4921 break;
4923 case OFFSET_REF:
4924 /* Turn a reference to a non-static data member into a
4925 pointer-to-member. */
4927 tree type;
4928 tree t;
4930 if (!PTRMEM_OK_P (arg))
4931 return cp_build_unary_op (code, arg, 0, complain);
4933 t = TREE_OPERAND (arg, 1);
4934 if (TREE_CODE (TREE_TYPE (t)) == REFERENCE_TYPE)
4936 if (complain & tf_error)
4937 error ("cannot create pointer to reference member %qD", t);
4938 return error_mark_node;
4941 type = build_ptrmem_type (context_for_name_lookup (t),
4942 TREE_TYPE (t));
4943 t = make_ptrmem_cst (type, TREE_OPERAND (arg, 1));
4944 return t;
4947 default:
4948 break;
4951 /* Anything not already handled and not a true memory reference
4952 is an error. */
4953 if (TREE_CODE (argtype) != FUNCTION_TYPE
4954 && TREE_CODE (argtype) != METHOD_TYPE
4955 && TREE_CODE (arg) != OFFSET_REF
4956 && !lvalue_or_else (arg, lv_addressof, complain))
4957 return error_mark_node;
4959 if (argtype != error_mark_node)
4960 argtype = build_pointer_type (argtype);
4962 /* In a template, we are processing a non-dependent expression
4963 so we can just form an ADDR_EXPR with the correct type. */
4964 if (processing_template_decl || TREE_CODE (arg) != COMPONENT_REF)
4966 val = build_address (arg);
4967 if (TREE_CODE (arg) == OFFSET_REF)
4968 PTRMEM_OK_P (val) = PTRMEM_OK_P (arg);
4970 else if (TREE_CODE (TREE_OPERAND (arg, 1)) == BASELINK)
4972 tree fn = BASELINK_FUNCTIONS (TREE_OPERAND (arg, 1));
4974 /* We can only get here with a single static member
4975 function. */
4976 gcc_assert (TREE_CODE (fn) == FUNCTION_DECL
4977 && DECL_STATIC_FUNCTION_P (fn));
4978 mark_used (fn);
4979 val = build_address (fn);
4980 if (TREE_SIDE_EFFECTS (TREE_OPERAND (arg, 0)))
4981 /* Do not lose object's side effects. */
4982 val = build2 (COMPOUND_EXPR, TREE_TYPE (val),
4983 TREE_OPERAND (arg, 0), val);
4985 else if (DECL_C_BIT_FIELD (TREE_OPERAND (arg, 1)))
4987 if (complain & tf_error)
4988 error ("attempt to take address of bit-field structure member %qD",
4989 TREE_OPERAND (arg, 1));
4990 return error_mark_node;
4992 else
4994 tree object = TREE_OPERAND (arg, 0);
4995 tree field = TREE_OPERAND (arg, 1);
4996 gcc_assert (same_type_ignoring_top_level_qualifiers_p
4997 (TREE_TYPE (object), decl_type_context (field)));
4998 val = build_address (arg);
5001 if (TREE_CODE (argtype) == POINTER_TYPE
5002 && TREE_CODE (TREE_TYPE (argtype)) == METHOD_TYPE)
5004 build_ptrmemfunc_type (argtype);
5005 val = build_ptrmemfunc (argtype, val, 0,
5006 /*c_cast_p=*/false);
5009 return val;
5011 default:
5012 break;
5015 if (!errstring)
5017 if (argtype == 0)
5018 argtype = TREE_TYPE (arg);
5019 return fold_if_not_in_template (build1 (code, argtype, arg));
5022 if (complain & tf_error)
5023 error ("%s", errstring);
5024 return error_mark_node;
5027 /* Hook for the c-common bits that build a unary op. */
5028 tree
5029 build_unary_op (location_t location ATTRIBUTE_UNUSED,
5030 enum tree_code code, tree xarg, int noconvert)
5032 return cp_build_unary_op (code, xarg, noconvert, tf_warning_or_error);
5035 /* Apply unary lvalue-demanding operator CODE to the expression ARG
5036 for certain kinds of expressions which are not really lvalues
5037 but which we can accept as lvalues.
5039 If ARG is not a kind of expression we can handle, return
5040 NULL_TREE. */
5042 tree
5043 unary_complex_lvalue (enum tree_code code, tree arg)
5045 /* Inside a template, making these kinds of adjustments is
5046 pointless; we are only concerned with the type of the
5047 expression. */
5048 if (processing_template_decl)
5049 return NULL_TREE;
5051 /* Handle (a, b) used as an "lvalue". */
5052 if (TREE_CODE (arg) == COMPOUND_EXPR)
5054 tree real_result = cp_build_unary_op (code, TREE_OPERAND (arg, 1), 0,
5055 tf_warning_or_error);
5056 return build2 (COMPOUND_EXPR, TREE_TYPE (real_result),
5057 TREE_OPERAND (arg, 0), real_result);
5060 /* Handle (a ? b : c) used as an "lvalue". */
5061 if (TREE_CODE (arg) == COND_EXPR
5062 || TREE_CODE (arg) == MIN_EXPR || TREE_CODE (arg) == MAX_EXPR)
5063 return rationalize_conditional_expr (code, arg, tf_warning_or_error);
5065 /* Handle (a = b), (++a), and (--a) used as an "lvalue". */
5066 if (TREE_CODE (arg) == MODIFY_EXPR
5067 || TREE_CODE (arg) == PREINCREMENT_EXPR
5068 || TREE_CODE (arg) == PREDECREMENT_EXPR)
5070 tree lvalue = TREE_OPERAND (arg, 0);
5071 if (TREE_SIDE_EFFECTS (lvalue))
5073 lvalue = stabilize_reference (lvalue);
5074 arg = build2 (TREE_CODE (arg), TREE_TYPE (arg),
5075 lvalue, TREE_OPERAND (arg, 1));
5077 return unary_complex_lvalue
5078 (code, build2 (COMPOUND_EXPR, TREE_TYPE (lvalue), arg, lvalue));
5081 if (code != ADDR_EXPR)
5082 return NULL_TREE;
5084 /* Handle (a = b) used as an "lvalue" for `&'. */
5085 if (TREE_CODE (arg) == MODIFY_EXPR
5086 || TREE_CODE (arg) == INIT_EXPR)
5088 tree real_result = cp_build_unary_op (code, TREE_OPERAND (arg, 0), 0,
5089 tf_warning_or_error);
5090 arg = build2 (COMPOUND_EXPR, TREE_TYPE (real_result),
5091 arg, real_result);
5092 TREE_NO_WARNING (arg) = 1;
5093 return arg;
5096 if (TREE_CODE (TREE_TYPE (arg)) == FUNCTION_TYPE
5097 || TREE_CODE (TREE_TYPE (arg)) == METHOD_TYPE
5098 || TREE_CODE (arg) == OFFSET_REF)
5099 return NULL_TREE;
5101 /* We permit compiler to make function calls returning
5102 objects of aggregate type look like lvalues. */
5104 tree targ = arg;
5106 if (TREE_CODE (targ) == SAVE_EXPR)
5107 targ = TREE_OPERAND (targ, 0);
5109 if (TREE_CODE (targ) == CALL_EXPR && MAYBE_CLASS_TYPE_P (TREE_TYPE (targ)))
5111 if (TREE_CODE (arg) == SAVE_EXPR)
5112 targ = arg;
5113 else
5114 targ = build_cplus_new (TREE_TYPE (arg), arg);
5115 return build1 (ADDR_EXPR, build_pointer_type (TREE_TYPE (arg)), targ);
5118 if (TREE_CODE (arg) == SAVE_EXPR && TREE_CODE (targ) == INDIRECT_REF)
5119 return build3 (SAVE_EXPR, build_pointer_type (TREE_TYPE (arg)),
5120 TREE_OPERAND (targ, 0), current_function_decl, NULL);
5123 /* Don't let anything else be handled specially. */
5124 return NULL_TREE;
5127 /* Mark EXP saying that we need to be able to take the
5128 address of it; it should not be allocated in a register.
5129 Value is true if successful.
5131 C++: we do not allow `current_class_ptr' to be addressable. */
5133 bool
5134 cxx_mark_addressable (tree exp)
5136 tree x = exp;
5138 while (1)
5139 switch (TREE_CODE (x))
5141 case ADDR_EXPR:
5142 case COMPONENT_REF:
5143 case ARRAY_REF:
5144 case REALPART_EXPR:
5145 case IMAGPART_EXPR:
5146 x = TREE_OPERAND (x, 0);
5147 break;
5149 case PARM_DECL:
5150 if (x == current_class_ptr)
5152 error ("cannot take the address of %<this%>, which is an rvalue expression");
5153 TREE_ADDRESSABLE (x) = 1; /* so compiler doesn't die later. */
5154 return true;
5156 /* Fall through. */
5158 case VAR_DECL:
5159 /* Caller should not be trying to mark initialized
5160 constant fields addressable. */
5161 gcc_assert (DECL_LANG_SPECIFIC (x) == 0
5162 || DECL_IN_AGGR_P (x) == 0
5163 || TREE_STATIC (x)
5164 || DECL_EXTERNAL (x));
5165 /* Fall through. */
5167 case CONST_DECL:
5168 case RESULT_DECL:
5169 if (DECL_REGISTER (x) && !TREE_ADDRESSABLE (x)
5170 && !DECL_ARTIFICIAL (x))
5172 if (TREE_CODE (x) == VAR_DECL && DECL_HARD_REGISTER (x))
5174 error
5175 ("address of explicit register variable %qD requested", x);
5176 return false;
5178 else if (extra_warnings)
5179 warning
5180 (OPT_Wextra, "address requested for %qD, which is declared %<register%>", x);
5182 TREE_ADDRESSABLE (x) = 1;
5183 return true;
5185 case FUNCTION_DECL:
5186 TREE_ADDRESSABLE (x) = 1;
5187 return true;
5189 case CONSTRUCTOR:
5190 TREE_ADDRESSABLE (x) = 1;
5191 return true;
5193 case TARGET_EXPR:
5194 TREE_ADDRESSABLE (x) = 1;
5195 cxx_mark_addressable (TREE_OPERAND (x, 0));
5196 return true;
5198 default:
5199 return true;
5203 /* Build and return a conditional expression IFEXP ? OP1 : OP2. */
5205 tree
5206 build_x_conditional_expr (tree ifexp, tree op1, tree op2,
5207 tsubst_flags_t complain)
5209 tree orig_ifexp = ifexp;
5210 tree orig_op1 = op1;
5211 tree orig_op2 = op2;
5212 tree expr;
5214 if (processing_template_decl)
5216 /* The standard says that the expression is type-dependent if
5217 IFEXP is type-dependent, even though the eventual type of the
5218 expression doesn't dependent on IFEXP. */
5219 if (type_dependent_expression_p (ifexp)
5220 /* As a GNU extension, the middle operand may be omitted. */
5221 || (op1 && type_dependent_expression_p (op1))
5222 || type_dependent_expression_p (op2))
5223 return build_min_nt (COND_EXPR, ifexp, op1, op2);
5224 ifexp = build_non_dependent_expr (ifexp);
5225 if (op1)
5226 op1 = build_non_dependent_expr (op1);
5227 op2 = build_non_dependent_expr (op2);
5230 expr = build_conditional_expr (ifexp, op1, op2, complain);
5231 if (processing_template_decl && expr != error_mark_node)
5232 return build_min_non_dep (COND_EXPR, expr,
5233 orig_ifexp, orig_op1, orig_op2);
5234 return expr;
5237 /* Given a list of expressions, return a compound expression
5238 that performs them all and returns the value of the last of them. */
5240 tree build_x_compound_expr_from_list (tree list, const char *msg)
5242 tree expr = TREE_VALUE (list);
5244 if (TREE_CHAIN (list))
5246 if (msg)
5247 permerror (input_location, "%s expression list treated as compound expression", msg);
5249 for (list = TREE_CHAIN (list); list; list = TREE_CHAIN (list))
5250 expr = build_x_compound_expr (expr, TREE_VALUE (list),
5251 tf_warning_or_error);
5254 return expr;
5257 /* Like build_x_compound_expr_from_list, but using a VEC. */
5259 tree
5260 build_x_compound_expr_from_vec (VEC(tree,gc) *vec, const char *msg)
5262 if (VEC_empty (tree, vec))
5263 return NULL_TREE;
5264 else if (VEC_length (tree, vec) == 1)
5265 return VEC_index (tree, vec, 0);
5266 else
5268 tree expr;
5269 unsigned int ix;
5270 tree t;
5272 if (msg != NULL)
5273 permerror (input_location,
5274 "%s expression list treated as compound expression",
5275 msg);
5277 expr = VEC_index (tree, vec, 0);
5278 for (ix = 1; VEC_iterate (tree, vec, ix, t); ++ix)
5279 expr = build_x_compound_expr (expr, t, tf_warning_or_error);
5281 return expr;
5285 /* Handle overloading of the ',' operator when needed. */
5287 tree
5288 build_x_compound_expr (tree op1, tree op2, tsubst_flags_t complain)
5290 tree result;
5291 tree orig_op1 = op1;
5292 tree orig_op2 = op2;
5294 if (processing_template_decl)
5296 if (type_dependent_expression_p (op1)
5297 || type_dependent_expression_p (op2))
5298 return build_min_nt (COMPOUND_EXPR, op1, op2);
5299 op1 = build_non_dependent_expr (op1);
5300 op2 = build_non_dependent_expr (op2);
5303 result = build_new_op (COMPOUND_EXPR, LOOKUP_NORMAL, op1, op2, NULL_TREE,
5304 /*overloaded_p=*/NULL, complain);
5305 if (!result)
5306 result = cp_build_compound_expr (op1, op2, complain);
5308 if (processing_template_decl && result != error_mark_node)
5309 return build_min_non_dep (COMPOUND_EXPR, result, orig_op1, orig_op2);
5311 return result;
5314 /* Like cp_build_compound_expr, but for the c-common bits. */
5316 tree
5317 build_compound_expr (location_t loc ATTRIBUTE_UNUSED, tree lhs, tree rhs)
5319 return cp_build_compound_expr (lhs, rhs, tf_warning_or_error);
5322 /* Build a compound expression. */
5324 tree
5325 cp_build_compound_expr (tree lhs, tree rhs, tsubst_flags_t complain)
5327 lhs = convert_to_void (lhs, "left-hand operand of comma", complain);
5329 if (lhs == error_mark_node || rhs == error_mark_node)
5330 return error_mark_node;
5332 if (TREE_CODE (rhs) == TARGET_EXPR)
5334 /* If the rhs is a TARGET_EXPR, then build the compound
5335 expression inside the target_expr's initializer. This
5336 helps the compiler to eliminate unnecessary temporaries. */
5337 tree init = TREE_OPERAND (rhs, 1);
5339 init = build2 (COMPOUND_EXPR, TREE_TYPE (init), lhs, init);
5340 TREE_OPERAND (rhs, 1) = init;
5342 return rhs;
5345 if (type_unknown_p (rhs))
5347 error ("no context to resolve type of %qE", rhs);
5348 return error_mark_node;
5351 return build2 (COMPOUND_EXPR, TREE_TYPE (rhs), lhs, rhs);
5354 /* Issue a diagnostic message if casting from SRC_TYPE to DEST_TYPE
5355 casts away constness. CAST gives the type of cast.
5357 ??? This function warns for casting away any qualifier not just
5358 const. We would like to specify exactly what qualifiers are casted
5359 away.
5362 static void
5363 check_for_casting_away_constness (tree src_type, tree dest_type,
5364 enum tree_code cast)
5366 /* C-style casts are allowed to cast away constness. With
5367 WARN_CAST_QUAL, we still want to issue a warning. */
5368 if (cast == CAST_EXPR && !warn_cast_qual)
5369 return;
5371 if (!casts_away_constness (src_type, dest_type))
5372 return;
5374 switch (cast)
5376 case CAST_EXPR:
5377 warning (OPT_Wcast_qual,
5378 "cast from type %qT to type %qT casts away qualifiers",
5379 src_type, dest_type);
5380 return;
5382 case STATIC_CAST_EXPR:
5383 error ("static_cast from type %qT to type %qT casts away qualifiers",
5384 src_type, dest_type);
5385 return;
5387 case REINTERPRET_CAST_EXPR:
5388 error ("reinterpret_cast from type %qT to type %qT casts away qualifiers",
5389 src_type, dest_type);
5390 return;
5391 default:
5392 gcc_unreachable();
5396 /* Convert EXPR (an expression with pointer-to-member type) to TYPE
5397 (another pointer-to-member type in the same hierarchy) and return
5398 the converted expression. If ALLOW_INVERSE_P is permitted, a
5399 pointer-to-derived may be converted to pointer-to-base; otherwise,
5400 only the other direction is permitted. If C_CAST_P is true, this
5401 conversion is taking place as part of a C-style cast. */
5403 tree
5404 convert_ptrmem (tree type, tree expr, bool allow_inverse_p,
5405 bool c_cast_p)
5407 if (TYPE_PTRMEM_P (type))
5409 tree delta;
5411 if (TREE_CODE (expr) == PTRMEM_CST)
5412 expr = cplus_expand_constant (expr);
5413 delta = get_delta_difference (TYPE_PTRMEM_CLASS_TYPE (TREE_TYPE (expr)),
5414 TYPE_PTRMEM_CLASS_TYPE (type),
5415 allow_inverse_p,
5416 c_cast_p);
5417 if (!integer_zerop (delta))
5419 tree cond, op1, op2;
5421 cond = cp_build_binary_op (input_location,
5422 EQ_EXPR,
5423 expr,
5424 build_int_cst (TREE_TYPE (expr), -1),
5425 tf_warning_or_error);
5426 op1 = build_nop (ptrdiff_type_node, expr);
5427 op2 = cp_build_binary_op (input_location,
5428 PLUS_EXPR, op1, delta,
5429 tf_warning_or_error);
5431 expr = fold_build3_loc (input_location,
5432 COND_EXPR, ptrdiff_type_node, cond, op1, op2);
5436 return build_nop (type, expr);
5438 else
5439 return build_ptrmemfunc (TYPE_PTRMEMFUNC_FN_TYPE (type), expr,
5440 allow_inverse_p, c_cast_p);
5443 /* If EXPR is an INTEGER_CST and ORIG is an arithmetic constant, return
5444 a version of EXPR that has TREE_OVERFLOW set if it is set in ORIG.
5445 Otherwise, return EXPR unchanged. */
5447 static tree
5448 ignore_overflows (tree expr, tree orig)
5450 if (TREE_CODE (expr) == INTEGER_CST
5451 && CONSTANT_CLASS_P (orig)
5452 && TREE_CODE (orig) != STRING_CST
5453 && TREE_OVERFLOW (expr) != TREE_OVERFLOW (orig))
5455 if (!TREE_OVERFLOW (orig))
5456 /* Ensure constant sharing. */
5457 expr = build_int_cst_wide (TREE_TYPE (expr),
5458 TREE_INT_CST_LOW (expr),
5459 TREE_INT_CST_HIGH (expr));
5460 else
5462 /* Avoid clobbering a shared constant. */
5463 expr = copy_node (expr);
5464 TREE_OVERFLOW (expr) = TREE_OVERFLOW (orig);
5467 return expr;
5470 /* Perform a static_cast from EXPR to TYPE. When C_CAST_P is true,
5471 this static_cast is being attempted as one of the possible casts
5472 allowed by a C-style cast. (In that case, accessibility of base
5473 classes is not considered, and it is OK to cast away
5474 constness.) Return the result of the cast. *VALID_P is set to
5475 indicate whether or not the cast was valid. */
5477 static tree
5478 build_static_cast_1 (tree type, tree expr, bool c_cast_p,
5479 bool *valid_p, tsubst_flags_t complain)
5481 tree intype;
5482 tree result;
5483 tree orig;
5485 /* Assume the cast is valid. */
5486 *valid_p = true;
5488 intype = TREE_TYPE (expr);
5490 /* Save casted types in the function's used types hash table. */
5491 used_types_insert (type);
5493 /* [expr.static.cast]
5495 An lvalue of type "cv1 B", where B is a class type, can be cast
5496 to type "reference to cv2 D", where D is a class derived (clause
5497 _class.derived_) from B, if a valid standard conversion from
5498 "pointer to D" to "pointer to B" exists (_conv.ptr_), cv2 is the
5499 same cv-qualification as, or greater cv-qualification than, cv1,
5500 and B is not a virtual base class of D. */
5501 /* We check this case before checking the validity of "TYPE t =
5502 EXPR;" below because for this case:
5504 struct B {};
5505 struct D : public B { D(const B&); };
5506 extern B& b;
5507 void f() { static_cast<const D&>(b); }
5509 we want to avoid constructing a new D. The standard is not
5510 completely clear about this issue, but our interpretation is
5511 consistent with other compilers. */
5512 if (TREE_CODE (type) == REFERENCE_TYPE
5513 && CLASS_TYPE_P (TREE_TYPE (type))
5514 && CLASS_TYPE_P (intype)
5515 && (TYPE_REF_IS_RVALUE (type) || real_lvalue_p (expr))
5516 && DERIVED_FROM_P (intype, TREE_TYPE (type))
5517 && can_convert (build_pointer_type (TYPE_MAIN_VARIANT (intype)),
5518 build_pointer_type (TYPE_MAIN_VARIANT
5519 (TREE_TYPE (type))))
5520 && (c_cast_p
5521 || at_least_as_qualified_p (TREE_TYPE (type), intype)))
5523 tree base;
5525 /* There is a standard conversion from "D*" to "B*" even if "B"
5526 is ambiguous or inaccessible. If this is really a
5527 static_cast, then we check both for inaccessibility and
5528 ambiguity. However, if this is a static_cast being performed
5529 because the user wrote a C-style cast, then accessibility is
5530 not considered. */
5531 base = lookup_base (TREE_TYPE (type), intype,
5532 c_cast_p ? ba_unique : ba_check,
5533 NULL);
5535 /* Convert from "B*" to "D*". This function will check that "B"
5536 is not a virtual base of "D". */
5537 expr = build_base_path (MINUS_EXPR, build_address (expr),
5538 base, /*nonnull=*/false);
5539 /* Convert the pointer to a reference -- but then remember that
5540 there are no expressions with reference type in C++. */
5541 return convert_from_reference (cp_fold_convert (type, expr));
5544 /* "An lvalue of type cv1 T1 can be cast to type rvalue reference to
5545 cv2 T2 if cv2 T2 is reference-compatible with cv1 T1 (8.5.3)." */
5546 if (TREE_CODE (type) == REFERENCE_TYPE
5547 && TYPE_REF_IS_RVALUE (type)
5548 && real_lvalue_p (expr)
5549 && reference_related_p (TREE_TYPE (type), intype)
5550 && (c_cast_p || at_least_as_qualified_p (TREE_TYPE (type), intype)))
5552 expr = build_typed_address (expr, type);
5553 return convert_from_reference (expr);
5556 orig = expr;
5558 /* Resolve overloaded address here rather than once in
5559 implicit_conversion and again in the inverse code below. */
5560 if (TYPE_PTRMEMFUNC_P (type) && type_unknown_p (expr))
5562 expr = instantiate_type (type, expr, complain);
5563 intype = TREE_TYPE (expr);
5566 /* [expr.static.cast]
5568 An expression e can be explicitly converted to a type T using a
5569 static_cast of the form static_cast<T>(e) if the declaration T
5570 t(e);" is well-formed, for some invented temporary variable
5571 t. */
5572 result = perform_direct_initialization_if_possible (type, expr,
5573 c_cast_p, complain);
5574 if (result)
5576 result = convert_from_reference (result);
5578 /* Ignore any integer overflow caused by the cast. */
5579 result = ignore_overflows (result, orig);
5581 /* [expr.static.cast]
5583 If T is a reference type, the result is an lvalue; otherwise,
5584 the result is an rvalue. */
5585 if (TREE_CODE (type) != REFERENCE_TYPE)
5586 result = rvalue (result);
5587 return result;
5590 /* [expr.static.cast]
5592 Any expression can be explicitly converted to type cv void. */
5593 if (TREE_CODE (type) == VOID_TYPE)
5594 return convert_to_void (expr, /*implicit=*/NULL, complain);
5596 /* [expr.static.cast]
5598 The inverse of any standard conversion sequence (clause _conv_),
5599 other than the lvalue-to-rvalue (_conv.lval_), array-to-pointer
5600 (_conv.array_), function-to-pointer (_conv.func_), and boolean
5601 (_conv.bool_) conversions, can be performed explicitly using
5602 static_cast subject to the restriction that the explicit
5603 conversion does not cast away constness (_expr.const.cast_), and
5604 the following additional rules for specific cases: */
5605 /* For reference, the conversions not excluded are: integral
5606 promotions, floating point promotion, integral conversions,
5607 floating point conversions, floating-integral conversions,
5608 pointer conversions, and pointer to member conversions. */
5609 /* DR 128
5611 A value of integral _or enumeration_ type can be explicitly
5612 converted to an enumeration type. */
5613 /* The effect of all that is that any conversion between any two
5614 types which are integral, floating, or enumeration types can be
5615 performed. */
5616 if ((INTEGRAL_OR_ENUMERATION_TYPE_P (type)
5617 || SCALAR_FLOAT_TYPE_P (type))
5618 && (INTEGRAL_OR_ENUMERATION_TYPE_P (intype)
5619 || SCALAR_FLOAT_TYPE_P (intype)))
5621 expr = ocp_convert (type, expr, CONV_C_CAST, LOOKUP_NORMAL);
5623 /* Ignore any integer overflow caused by the cast. */
5624 expr = ignore_overflows (expr, orig);
5625 return expr;
5628 if (TYPE_PTR_P (type) && TYPE_PTR_P (intype)
5629 && CLASS_TYPE_P (TREE_TYPE (type))
5630 && CLASS_TYPE_P (TREE_TYPE (intype))
5631 && can_convert (build_pointer_type (TYPE_MAIN_VARIANT
5632 (TREE_TYPE (intype))),
5633 build_pointer_type (TYPE_MAIN_VARIANT
5634 (TREE_TYPE (type)))))
5636 tree base;
5638 if (!c_cast_p)
5639 check_for_casting_away_constness (intype, type, STATIC_CAST_EXPR);
5640 base = lookup_base (TREE_TYPE (type), TREE_TYPE (intype),
5641 c_cast_p ? ba_unique : ba_check,
5642 NULL);
5643 return build_base_path (MINUS_EXPR, expr, base, /*nonnull=*/false);
5646 if ((TYPE_PTRMEM_P (type) && TYPE_PTRMEM_P (intype))
5647 || (TYPE_PTRMEMFUNC_P (type) && TYPE_PTRMEMFUNC_P (intype)))
5649 tree c1;
5650 tree c2;
5651 tree t1;
5652 tree t2;
5654 c1 = TYPE_PTRMEM_CLASS_TYPE (intype);
5655 c2 = TYPE_PTRMEM_CLASS_TYPE (type);
5657 if (TYPE_PTRMEM_P (type))
5659 t1 = (build_ptrmem_type
5660 (c1,
5661 TYPE_MAIN_VARIANT (TYPE_PTRMEM_POINTED_TO_TYPE (intype))));
5662 t2 = (build_ptrmem_type
5663 (c2,
5664 TYPE_MAIN_VARIANT (TYPE_PTRMEM_POINTED_TO_TYPE (type))));
5666 else
5668 t1 = intype;
5669 t2 = type;
5671 if (can_convert (t1, t2) || can_convert (t2, t1))
5673 if (!c_cast_p)
5674 check_for_casting_away_constness (intype, type, STATIC_CAST_EXPR);
5675 return convert_ptrmem (type, expr, /*allow_inverse_p=*/1,
5676 c_cast_p);
5680 /* [expr.static.cast]
5682 An rvalue of type "pointer to cv void" can be explicitly
5683 converted to a pointer to object type. A value of type pointer
5684 to object converted to "pointer to cv void" and back to the
5685 original pointer type will have its original value. */
5686 if (TREE_CODE (intype) == POINTER_TYPE
5687 && VOID_TYPE_P (TREE_TYPE (intype))
5688 && TYPE_PTROB_P (type))
5690 if (!c_cast_p)
5691 check_for_casting_away_constness (intype, type, STATIC_CAST_EXPR);
5692 return build_nop (type, expr);
5695 *valid_p = false;
5696 return error_mark_node;
5699 /* Return an expression representing static_cast<TYPE>(EXPR). */
5701 tree
5702 build_static_cast (tree type, tree expr, tsubst_flags_t complain)
5704 tree result;
5705 bool valid_p;
5707 if (type == error_mark_node || expr == error_mark_node)
5708 return error_mark_node;
5710 if (processing_template_decl)
5712 expr = build_min (STATIC_CAST_EXPR, type, expr);
5713 /* We don't know if it will or will not have side effects. */
5714 TREE_SIDE_EFFECTS (expr) = 1;
5715 return convert_from_reference (expr);
5718 /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
5719 Strip such NOP_EXPRs if VALUE is being used in non-lvalue context. */
5720 if (TREE_CODE (type) != REFERENCE_TYPE
5721 && TREE_CODE (expr) == NOP_EXPR
5722 && TREE_TYPE (expr) == TREE_TYPE (TREE_OPERAND (expr, 0)))
5723 expr = TREE_OPERAND (expr, 0);
5725 result = build_static_cast_1 (type, expr, /*c_cast_p=*/false, &valid_p,
5726 complain);
5727 if (valid_p)
5728 return result;
5730 if (complain & tf_error)
5731 error ("invalid static_cast from type %qT to type %qT",
5732 TREE_TYPE (expr), type);
5733 return error_mark_node;
5736 /* EXPR is an expression with member function or pointer-to-member
5737 function type. TYPE is a pointer type. Converting EXPR to TYPE is
5738 not permitted by ISO C++, but we accept it in some modes. If we
5739 are not in one of those modes, issue a diagnostic. Return the
5740 converted expression. */
5742 tree
5743 convert_member_func_to_ptr (tree type, tree expr)
5745 tree intype;
5746 tree decl;
5748 intype = TREE_TYPE (expr);
5749 gcc_assert (TYPE_PTRMEMFUNC_P (intype)
5750 || TREE_CODE (intype) == METHOD_TYPE);
5752 if (pedantic || warn_pmf2ptr)
5753 pedwarn (input_location, pedantic ? OPT_pedantic : OPT_Wpmf_conversions,
5754 "converting from %qT to %qT", intype, type);
5756 if (TREE_CODE (intype) == METHOD_TYPE)
5757 expr = build_addr_func (expr);
5758 else if (TREE_CODE (expr) == PTRMEM_CST)
5759 expr = build_address (PTRMEM_CST_MEMBER (expr));
5760 else
5762 decl = maybe_dummy_object (TYPE_PTRMEM_CLASS_TYPE (intype), 0);
5763 decl = build_address (decl);
5764 expr = get_member_function_from_ptrfunc (&decl, expr);
5767 return build_nop (type, expr);
5770 /* Return a representation for a reinterpret_cast from EXPR to TYPE.
5771 If C_CAST_P is true, this reinterpret cast is being done as part of
5772 a C-style cast. If VALID_P is non-NULL, *VALID_P is set to
5773 indicate whether or not reinterpret_cast was valid. */
5775 static tree
5776 build_reinterpret_cast_1 (tree type, tree expr, bool c_cast_p,
5777 bool *valid_p, tsubst_flags_t complain)
5779 tree intype;
5781 /* Assume the cast is invalid. */
5782 if (valid_p)
5783 *valid_p = true;
5785 if (type == error_mark_node || error_operand_p (expr))
5786 return error_mark_node;
5788 intype = TREE_TYPE (expr);
5790 /* Save casted types in the function's used types hash table. */
5791 used_types_insert (type);
5793 /* [expr.reinterpret.cast]
5794 An lvalue expression of type T1 can be cast to the type
5795 "reference to T2" if an expression of type "pointer to T1" can be
5796 explicitly converted to the type "pointer to T2" using a
5797 reinterpret_cast. */
5798 if (TREE_CODE (type) == REFERENCE_TYPE)
5800 if (! real_lvalue_p (expr))
5802 if (complain & tf_error)
5803 error ("invalid cast of an rvalue expression of type "
5804 "%qT to type %qT",
5805 intype, type);
5806 return error_mark_node;
5809 /* Warn about a reinterpret_cast from "A*" to "B&" if "A" and
5810 "B" are related class types; the reinterpret_cast does not
5811 adjust the pointer. */
5812 if (TYPE_PTR_P (intype)
5813 && (complain & tf_warning)
5814 && (comptypes (TREE_TYPE (intype), TREE_TYPE (type),
5815 COMPARE_BASE | COMPARE_DERIVED)))
5816 warning (0, "casting %qT to %qT does not dereference pointer",
5817 intype, type);
5819 expr = cp_build_unary_op (ADDR_EXPR, expr, 0, complain);
5821 if (warn_strict_aliasing > 2)
5822 strict_aliasing_warning (TREE_TYPE (expr), type, expr);
5824 if (expr != error_mark_node)
5825 expr = build_reinterpret_cast_1
5826 (build_pointer_type (TREE_TYPE (type)), expr, c_cast_p,
5827 valid_p, complain);
5828 if (expr != error_mark_node)
5829 /* cp_build_indirect_ref isn't right for rvalue refs. */
5830 expr = convert_from_reference (fold_convert (type, expr));
5831 return expr;
5834 /* As a G++ extension, we consider conversions from member
5835 functions, and pointers to member functions to
5836 pointer-to-function and pointer-to-void types. If
5837 -Wno-pmf-conversions has not been specified,
5838 convert_member_func_to_ptr will issue an error message. */
5839 if ((TYPE_PTRMEMFUNC_P (intype)
5840 || TREE_CODE (intype) == METHOD_TYPE)
5841 && TYPE_PTR_P (type)
5842 && (TREE_CODE (TREE_TYPE (type)) == FUNCTION_TYPE
5843 || VOID_TYPE_P (TREE_TYPE (type))))
5844 return convert_member_func_to_ptr (type, expr);
5846 /* If the cast is not to a reference type, the lvalue-to-rvalue,
5847 array-to-pointer, and function-to-pointer conversions are
5848 performed. */
5849 expr = decay_conversion (expr);
5851 /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
5852 Strip such NOP_EXPRs if VALUE is being used in non-lvalue context. */
5853 if (TREE_CODE (expr) == NOP_EXPR
5854 && TREE_TYPE (expr) == TREE_TYPE (TREE_OPERAND (expr, 0)))
5855 expr = TREE_OPERAND (expr, 0);
5857 if (error_operand_p (expr))
5858 return error_mark_node;
5860 intype = TREE_TYPE (expr);
5862 /* [expr.reinterpret.cast]
5863 A pointer can be converted to any integral type large enough to
5864 hold it. */
5865 if (CP_INTEGRAL_TYPE_P (type) && TYPE_PTR_P (intype))
5867 if (TYPE_PRECISION (type) < TYPE_PRECISION (intype))
5869 if (complain & tf_error)
5870 permerror (input_location, "cast from %qT to %qT loses precision",
5871 intype, type);
5872 else
5873 return error_mark_node;
5876 /* [expr.reinterpret.cast]
5877 A value of integral or enumeration type can be explicitly
5878 converted to a pointer. */
5879 else if (TYPE_PTR_P (type) && INTEGRAL_OR_ENUMERATION_TYPE_P (intype))
5880 /* OK */
5882 else if ((TYPE_PTRFN_P (type) && TYPE_PTRFN_P (intype))
5883 || (TYPE_PTRMEMFUNC_P (type) && TYPE_PTRMEMFUNC_P (intype)))
5884 return fold_if_not_in_template (build_nop (type, expr));
5885 else if ((TYPE_PTRMEM_P (type) && TYPE_PTRMEM_P (intype))
5886 || (TYPE_PTROBV_P (type) && TYPE_PTROBV_P (intype)))
5888 tree sexpr = expr;
5890 if (!c_cast_p)
5891 check_for_casting_away_constness (intype, type, REINTERPRET_CAST_EXPR);
5892 /* Warn about possible alignment problems. */
5893 if (STRICT_ALIGNMENT && warn_cast_align
5894 && (complain & tf_warning)
5895 && !VOID_TYPE_P (type)
5896 && TREE_CODE (TREE_TYPE (intype)) != FUNCTION_TYPE
5897 && COMPLETE_TYPE_P (TREE_TYPE (type))
5898 && COMPLETE_TYPE_P (TREE_TYPE (intype))
5899 && TYPE_ALIGN (TREE_TYPE (type)) > TYPE_ALIGN (TREE_TYPE (intype)))
5900 warning (OPT_Wcast_align, "cast from %qT to %qT "
5901 "increases required alignment of target type", intype, type);
5903 /* We need to strip nops here, because the front end likes to
5904 create (int *)&a for array-to-pointer decay, instead of &a[0]. */
5905 STRIP_NOPS (sexpr);
5906 if (warn_strict_aliasing <= 2)
5907 strict_aliasing_warning (intype, type, sexpr);
5909 return fold_if_not_in_template (build_nop (type, expr));
5911 else if ((TYPE_PTRFN_P (type) && TYPE_PTROBV_P (intype))
5912 || (TYPE_PTRFN_P (intype) && TYPE_PTROBV_P (type)))
5914 if (pedantic && (complain & tf_warning))
5915 /* Only issue a warning, as we have always supported this
5916 where possible, and it is necessary in some cases. DR 195
5917 addresses this issue, but as of 2004/10/26 is still in
5918 drafting. */
5919 warning (0, "ISO C++ forbids casting between pointer-to-function and pointer-to-object");
5920 return fold_if_not_in_template (build_nop (type, expr));
5922 else if (TREE_CODE (type) == VECTOR_TYPE)
5923 return fold_if_not_in_template (convert_to_vector (type, expr));
5924 else if (TREE_CODE (intype) == VECTOR_TYPE
5925 && INTEGRAL_OR_ENUMERATION_TYPE_P (type))
5926 return fold_if_not_in_template (convert_to_integer (type, expr));
5927 else
5929 if (valid_p)
5930 *valid_p = false;
5931 if (complain & tf_error)
5932 error ("invalid cast from type %qT to type %qT", intype, type);
5933 return error_mark_node;
5936 return cp_convert (type, expr);
5939 tree
5940 build_reinterpret_cast (tree type, tree expr, tsubst_flags_t complain)
5942 if (type == error_mark_node || expr == error_mark_node)
5943 return error_mark_node;
5945 if (processing_template_decl)
5947 tree t = build_min (REINTERPRET_CAST_EXPR, type, expr);
5949 if (!TREE_SIDE_EFFECTS (t)
5950 && type_dependent_expression_p (expr))
5951 /* There might turn out to be side effects inside expr. */
5952 TREE_SIDE_EFFECTS (t) = 1;
5953 return convert_from_reference (t);
5956 return build_reinterpret_cast_1 (type, expr, /*c_cast_p=*/false,
5957 /*valid_p=*/NULL, complain);
5960 /* Perform a const_cast from EXPR to TYPE. If the cast is valid,
5961 return an appropriate expression. Otherwise, return
5962 error_mark_node. If the cast is not valid, and COMPLAIN is true,
5963 then a diagnostic will be issued. If VALID_P is non-NULL, we are
5964 performing a C-style cast, its value upon return will indicate
5965 whether or not the conversion succeeded. */
5967 static tree
5968 build_const_cast_1 (tree dst_type, tree expr, bool complain,
5969 bool *valid_p)
5971 tree src_type;
5972 tree reference_type;
5974 /* Callers are responsible for handling error_mark_node as a
5975 destination type. */
5976 gcc_assert (dst_type != error_mark_node);
5977 /* In a template, callers should be building syntactic
5978 representations of casts, not using this machinery. */
5979 gcc_assert (!processing_template_decl);
5981 /* Assume the conversion is invalid. */
5982 if (valid_p)
5983 *valid_p = false;
5985 if (!POINTER_TYPE_P (dst_type) && !TYPE_PTRMEM_P (dst_type))
5987 if (complain)
5988 error ("invalid use of const_cast with type %qT, "
5989 "which is not a pointer, "
5990 "reference, nor a pointer-to-data-member type", dst_type);
5991 return error_mark_node;
5994 if (TREE_CODE (TREE_TYPE (dst_type)) == FUNCTION_TYPE)
5996 if (complain)
5997 error ("invalid use of const_cast with type %qT, which is a pointer "
5998 "or reference to a function type", dst_type);
5999 return error_mark_node;
6002 /* Save casted types in the function's used types hash table. */
6003 used_types_insert (dst_type);
6005 src_type = TREE_TYPE (expr);
6006 /* Expressions do not really have reference types. */
6007 if (TREE_CODE (src_type) == REFERENCE_TYPE)
6008 src_type = TREE_TYPE (src_type);
6010 /* [expr.const.cast]
6012 An lvalue of type T1 can be explicitly converted to an lvalue of
6013 type T2 using the cast const_cast<T2&> (where T1 and T2 are object
6014 types) if a pointer to T1 can be explicitly converted to the type
6015 pointer to T2 using a const_cast. */
6016 if (TREE_CODE (dst_type) == REFERENCE_TYPE)
6018 reference_type = dst_type;
6019 if (! real_lvalue_p (expr))
6021 if (complain)
6022 error ("invalid const_cast of an rvalue of type %qT to type %qT",
6023 src_type, dst_type);
6024 return error_mark_node;
6026 dst_type = build_pointer_type (TREE_TYPE (dst_type));
6027 src_type = build_pointer_type (src_type);
6029 else
6031 reference_type = NULL_TREE;
6032 /* If the destination type is not a reference type, the
6033 lvalue-to-rvalue, array-to-pointer, and function-to-pointer
6034 conversions are performed. */
6035 src_type = type_decays_to (src_type);
6036 if (src_type == error_mark_node)
6037 return error_mark_node;
6040 if ((TYPE_PTR_P (src_type) || TYPE_PTRMEM_P (src_type))
6041 && comp_ptr_ttypes_const (dst_type, src_type))
6043 if (valid_p)
6045 *valid_p = true;
6046 /* This cast is actually a C-style cast. Issue a warning if
6047 the user is making a potentially unsafe cast. */
6048 check_for_casting_away_constness (src_type, dst_type, CAST_EXPR);
6050 if (reference_type)
6052 expr = cp_build_unary_op (ADDR_EXPR, expr, 0,
6053 complain? tf_warning_or_error : tf_none);
6054 expr = build_nop (reference_type, expr);
6055 return convert_from_reference (expr);
6057 else
6059 expr = decay_conversion (expr);
6060 /* build_c_cast puts on a NOP_EXPR to make the result not an
6061 lvalue. Strip such NOP_EXPRs if VALUE is being used in
6062 non-lvalue context. */
6063 if (TREE_CODE (expr) == NOP_EXPR
6064 && TREE_TYPE (expr) == TREE_TYPE (TREE_OPERAND (expr, 0)))
6065 expr = TREE_OPERAND (expr, 0);
6066 return build_nop (dst_type, expr);
6070 if (complain)
6071 error ("invalid const_cast from type %qT to type %qT",
6072 src_type, dst_type);
6073 return error_mark_node;
6076 tree
6077 build_const_cast (tree type, tree expr, tsubst_flags_t complain)
6079 if (type == error_mark_node || error_operand_p (expr))
6080 return error_mark_node;
6082 if (processing_template_decl)
6084 tree t = build_min (CONST_CAST_EXPR, type, expr);
6086 if (!TREE_SIDE_EFFECTS (t)
6087 && type_dependent_expression_p (expr))
6088 /* There might turn out to be side effects inside expr. */
6089 TREE_SIDE_EFFECTS (t) = 1;
6090 return convert_from_reference (t);
6093 return build_const_cast_1 (type, expr, complain & tf_error,
6094 /*valid_p=*/NULL);
6097 /* Like cp_build_c_cast, but for the c-common bits. */
6099 tree
6100 build_c_cast (location_t loc ATTRIBUTE_UNUSED, tree type, tree expr)
6102 return cp_build_c_cast (type, expr, tf_warning_or_error);
6105 /* Build an expression representing an explicit C-style cast to type
6106 TYPE of expression EXPR. */
6108 tree
6109 cp_build_c_cast (tree type, tree expr, tsubst_flags_t complain)
6111 tree value = expr;
6112 tree result;
6113 bool valid_p;
6115 if (type == error_mark_node || error_operand_p (expr))
6116 return error_mark_node;
6118 if (processing_template_decl)
6120 tree t = build_min (CAST_EXPR, type,
6121 tree_cons (NULL_TREE, value, NULL_TREE));
6122 /* We don't know if it will or will not have side effects. */
6123 TREE_SIDE_EFFECTS (t) = 1;
6124 return convert_from_reference (t);
6127 /* Casts to a (pointer to a) specific ObjC class (or 'id' or
6128 'Class') should always be retained, because this information aids
6129 in method lookup. */
6130 if (objc_is_object_ptr (type)
6131 && objc_is_object_ptr (TREE_TYPE (expr)))
6132 return build_nop (type, expr);
6134 /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
6135 Strip such NOP_EXPRs if VALUE is being used in non-lvalue context. */
6136 if (TREE_CODE (type) != REFERENCE_TYPE
6137 && TREE_CODE (value) == NOP_EXPR
6138 && TREE_TYPE (value) == TREE_TYPE (TREE_OPERAND (value, 0)))
6139 value = TREE_OPERAND (value, 0);
6141 if (TREE_CODE (type) == ARRAY_TYPE)
6143 /* Allow casting from T1* to T2[] because Cfront allows it.
6144 NIHCL uses it. It is not valid ISO C++ however. */
6145 if (TREE_CODE (TREE_TYPE (expr)) == POINTER_TYPE)
6147 if (complain & tf_error)
6148 permerror (input_location, "ISO C++ forbids casting to an array type %qT", type);
6149 else
6150 return error_mark_node;
6151 type = build_pointer_type (TREE_TYPE (type));
6153 else
6155 if (complain & tf_error)
6156 error ("ISO C++ forbids casting to an array type %qT", type);
6157 return error_mark_node;
6161 if (TREE_CODE (type) == FUNCTION_TYPE
6162 || TREE_CODE (type) == METHOD_TYPE)
6164 if (complain & tf_error)
6165 error ("invalid cast to function type %qT", type);
6166 return error_mark_node;
6169 /* A C-style cast can be a const_cast. */
6170 result = build_const_cast_1 (type, value, /*complain=*/false,
6171 &valid_p);
6172 if (valid_p)
6173 return result;
6175 /* Or a static cast. */
6176 result = build_static_cast_1 (type, value, /*c_cast_p=*/true,
6177 &valid_p, complain);
6178 /* Or a reinterpret_cast. */
6179 if (!valid_p)
6180 result = build_reinterpret_cast_1 (type, value, /*c_cast_p=*/true,
6181 &valid_p, complain);
6182 /* The static_cast or reinterpret_cast may be followed by a
6183 const_cast. */
6184 if (valid_p
6185 /* A valid cast may result in errors if, for example, a
6186 conversion to am ambiguous base class is required. */
6187 && !error_operand_p (result))
6189 tree result_type;
6191 /* Non-class rvalues always have cv-unqualified type. */
6192 if (!CLASS_TYPE_P (type))
6193 type = TYPE_MAIN_VARIANT (type);
6194 result_type = TREE_TYPE (result);
6195 if (!CLASS_TYPE_P (result_type))
6196 result_type = TYPE_MAIN_VARIANT (result_type);
6197 /* If the type of RESULT does not match TYPE, perform a
6198 const_cast to make it match. If the static_cast or
6199 reinterpret_cast succeeded, we will differ by at most
6200 cv-qualification, so the follow-on const_cast is guaranteed
6201 to succeed. */
6202 if (!same_type_p (non_reference (type), non_reference (result_type)))
6204 result = build_const_cast_1 (type, result, false, &valid_p);
6205 gcc_assert (valid_p);
6207 return result;
6210 return error_mark_node;
6213 /* For use from the C common bits. */
6214 tree
6215 build_modify_expr (location_t location ATTRIBUTE_UNUSED,
6216 tree lhs, tree lhs_origtype ATTRIBUTE_UNUSED,
6217 enum tree_code modifycode,
6218 location_t rhs_location ATTRIBUTE_UNUSED, tree rhs,
6219 tree rhs_origtype ATTRIBUTE_UNUSED)
6221 return cp_build_modify_expr (lhs, modifycode, rhs, tf_warning_or_error);
6224 /* Build an assignment expression of lvalue LHS from value RHS.
6225 MODIFYCODE is the code for a binary operator that we use
6226 to combine the old value of LHS with RHS to get the new value.
6227 Or else MODIFYCODE is NOP_EXPR meaning do a simple assignment.
6229 C++: If MODIFYCODE is INIT_EXPR, then leave references unbashed. */
6231 tree
6232 cp_build_modify_expr (tree lhs, enum tree_code modifycode, tree rhs,
6233 tsubst_flags_t complain)
6235 tree result;
6236 tree newrhs = rhs;
6237 tree lhstype = TREE_TYPE (lhs);
6238 tree olhstype = lhstype;
6239 bool plain_assign = (modifycode == NOP_EXPR);
6241 /* Avoid duplicate error messages from operands that had errors. */
6242 if (error_operand_p (lhs) || error_operand_p (rhs))
6243 return error_mark_node;
6245 /* Handle control structure constructs used as "lvalues". */
6246 switch (TREE_CODE (lhs))
6248 /* Handle --foo = 5; as these are valid constructs in C++. */
6249 case PREDECREMENT_EXPR:
6250 case PREINCREMENT_EXPR:
6251 if (TREE_SIDE_EFFECTS (TREE_OPERAND (lhs, 0)))
6252 lhs = build2 (TREE_CODE (lhs), TREE_TYPE (lhs),
6253 stabilize_reference (TREE_OPERAND (lhs, 0)),
6254 TREE_OPERAND (lhs, 1));
6255 newrhs = cp_build_modify_expr (TREE_OPERAND (lhs, 0),
6256 modifycode, rhs, complain);
6257 if (newrhs == error_mark_node)
6258 return error_mark_node;
6259 return build2 (COMPOUND_EXPR, lhstype, lhs, newrhs);
6261 /* Handle (a, b) used as an "lvalue". */
6262 case COMPOUND_EXPR:
6263 newrhs = cp_build_modify_expr (TREE_OPERAND (lhs, 1),
6264 modifycode, rhs, complain);
6265 if (newrhs == error_mark_node)
6266 return error_mark_node;
6267 return build2 (COMPOUND_EXPR, lhstype,
6268 TREE_OPERAND (lhs, 0), newrhs);
6270 case MODIFY_EXPR:
6271 if (TREE_SIDE_EFFECTS (TREE_OPERAND (lhs, 0)))
6272 lhs = build2 (TREE_CODE (lhs), TREE_TYPE (lhs),
6273 stabilize_reference (TREE_OPERAND (lhs, 0)),
6274 TREE_OPERAND (lhs, 1));
6275 newrhs = cp_build_modify_expr (TREE_OPERAND (lhs, 0), modifycode, rhs,
6276 complain);
6277 if (newrhs == error_mark_node)
6278 return error_mark_node;
6279 return build2 (COMPOUND_EXPR, lhstype, lhs, newrhs);
6281 case MIN_EXPR:
6282 case MAX_EXPR:
6283 /* MIN_EXPR and MAX_EXPR are currently only permitted as lvalues,
6284 when neither operand has side-effects. */
6285 if (!lvalue_or_else (lhs, lv_assign, complain))
6286 return error_mark_node;
6288 gcc_assert (!TREE_SIDE_EFFECTS (TREE_OPERAND (lhs, 0))
6289 && !TREE_SIDE_EFFECTS (TREE_OPERAND (lhs, 1)));
6291 lhs = build3 (COND_EXPR, TREE_TYPE (lhs),
6292 build2 (TREE_CODE (lhs) == MIN_EXPR ? LE_EXPR : GE_EXPR,
6293 boolean_type_node,
6294 TREE_OPERAND (lhs, 0),
6295 TREE_OPERAND (lhs, 1)),
6296 TREE_OPERAND (lhs, 0),
6297 TREE_OPERAND (lhs, 1));
6298 /* Fall through. */
6300 /* Handle (a ? b : c) used as an "lvalue". */
6301 case COND_EXPR:
6303 /* Produce (a ? (b = rhs) : (c = rhs))
6304 except that the RHS goes through a save-expr
6305 so the code to compute it is only emitted once. */
6306 tree cond;
6307 tree preeval = NULL_TREE;
6309 if (VOID_TYPE_P (TREE_TYPE (rhs)))
6311 if (complain & tf_error)
6312 error ("void value not ignored as it ought to be");
6313 return error_mark_node;
6316 rhs = stabilize_expr (rhs, &preeval);
6318 /* Check this here to avoid odd errors when trying to convert
6319 a throw to the type of the COND_EXPR. */
6320 if (!lvalue_or_else (lhs, lv_assign, complain))
6321 return error_mark_node;
6323 cond = build_conditional_expr
6324 (TREE_OPERAND (lhs, 0),
6325 cp_build_modify_expr (TREE_OPERAND (lhs, 1),
6326 modifycode, rhs, complain),
6327 cp_build_modify_expr (TREE_OPERAND (lhs, 2),
6328 modifycode, rhs, complain),
6329 complain);
6331 if (cond == error_mark_node)
6332 return cond;
6333 /* Make sure the code to compute the rhs comes out
6334 before the split. */
6335 if (preeval)
6336 cond = build2 (COMPOUND_EXPR, TREE_TYPE (lhs), preeval, cond);
6337 return cond;
6340 default:
6341 break;
6344 if (modifycode == INIT_EXPR)
6346 if (BRACE_ENCLOSED_INITIALIZER_P (rhs))
6347 /* Do the default thing. */;
6348 else if (TREE_CODE (rhs) == CONSTRUCTOR)
6350 /* Compound literal. */
6351 if (! same_type_p (TREE_TYPE (rhs), lhstype))
6352 /* Call convert to generate an error; see PR 11063. */
6353 rhs = convert (lhstype, rhs);
6354 result = build2 (INIT_EXPR, lhstype, lhs, rhs);
6355 TREE_SIDE_EFFECTS (result) = 1;
6356 return result;
6358 else if (! MAYBE_CLASS_TYPE_P (lhstype))
6359 /* Do the default thing. */;
6360 else
6362 VEC(tree,gc) *rhs_vec = make_tree_vector_single (rhs);
6363 result = build_special_member_call (lhs, complete_ctor_identifier,
6364 &rhs_vec, lhstype, LOOKUP_NORMAL,
6365 complain);
6366 release_tree_vector (rhs_vec);
6367 if (result == NULL_TREE)
6368 return error_mark_node;
6369 return result;
6372 else
6374 lhs = require_complete_type (lhs);
6375 if (lhs == error_mark_node)
6376 return error_mark_node;
6378 if (modifycode == NOP_EXPR)
6380 /* `operator=' is not an inheritable operator. */
6381 if (! MAYBE_CLASS_TYPE_P (lhstype))
6382 /* Do the default thing. */;
6383 else
6385 result = build_new_op (MODIFY_EXPR, LOOKUP_NORMAL,
6386 lhs, rhs, make_node (NOP_EXPR),
6387 /*overloaded_p=*/NULL,
6388 complain);
6389 if (result == NULL_TREE)
6390 return error_mark_node;
6391 return result;
6393 lhstype = olhstype;
6395 else
6397 /* A binary op has been requested. Combine the old LHS
6398 value with the RHS producing the value we should actually
6399 store into the LHS. */
6400 gcc_assert (!((TREE_CODE (lhstype) == REFERENCE_TYPE
6401 && MAYBE_CLASS_TYPE_P (TREE_TYPE (lhstype)))
6402 || MAYBE_CLASS_TYPE_P (lhstype)));
6404 lhs = stabilize_reference (lhs);
6405 newrhs = cp_build_binary_op (input_location,
6406 modifycode, lhs, rhs,
6407 complain);
6408 if (newrhs == error_mark_node)
6410 if (complain & tf_error)
6411 error (" in evaluation of %<%Q(%#T, %#T)%>", modifycode,
6412 TREE_TYPE (lhs), TREE_TYPE (rhs));
6413 return error_mark_node;
6416 /* Now it looks like a plain assignment. */
6417 modifycode = NOP_EXPR;
6419 gcc_assert (TREE_CODE (lhstype) != REFERENCE_TYPE);
6420 gcc_assert (TREE_CODE (TREE_TYPE (newrhs)) != REFERENCE_TYPE);
6423 /* The left-hand side must be an lvalue. */
6424 if (!lvalue_or_else (lhs, lv_assign, complain))
6425 return error_mark_node;
6427 /* Warn about modifying something that is `const'. Don't warn if
6428 this is initialization. */
6429 if (modifycode != INIT_EXPR
6430 && (TREE_READONLY (lhs) || CP_TYPE_CONST_P (lhstype)
6431 /* Functions are not modifiable, even though they are
6432 lvalues. */
6433 || TREE_CODE (TREE_TYPE (lhs)) == FUNCTION_TYPE
6434 || TREE_CODE (TREE_TYPE (lhs)) == METHOD_TYPE
6435 /* If it's an aggregate and any field is const, then it is
6436 effectively const. */
6437 || (CLASS_TYPE_P (lhstype)
6438 && C_TYPE_FIELDS_READONLY (lhstype))))
6440 if (complain & tf_error)
6441 readonly_error (lhs, REK_ASSIGNMENT);
6442 else
6443 return error_mark_node;
6446 /* If storing into a structure or union member, it may have been given a
6447 lowered bitfield type. We need to convert to the declared type first,
6448 so retrieve it now. */
6450 olhstype = unlowered_expr_type (lhs);
6452 /* Convert new value to destination type. */
6454 if (TREE_CODE (lhstype) == ARRAY_TYPE)
6456 int from_array;
6458 if (BRACE_ENCLOSED_INITIALIZER_P (newrhs))
6460 if (check_array_initializer (lhs, lhstype, newrhs))
6461 return error_mark_node;
6462 newrhs = digest_init (lhstype, newrhs);
6465 else if (!same_or_base_type_p (TYPE_MAIN_VARIANT (lhstype),
6466 TYPE_MAIN_VARIANT (TREE_TYPE (newrhs))))
6468 if (complain & tf_error)
6469 error ("incompatible types in assignment of %qT to %qT",
6470 TREE_TYPE (rhs), lhstype);
6471 return error_mark_node;
6474 /* Allow array assignment in compiler-generated code. */
6475 else if (!current_function_decl
6476 || !DECL_ARTIFICIAL (current_function_decl))
6478 /* This routine is used for both initialization and assignment.
6479 Make sure the diagnostic message differentiates the context. */
6480 if (complain & tf_error)
6482 if (modifycode == INIT_EXPR)
6483 error ("array used as initializer");
6484 else
6485 error ("invalid array assignment");
6487 return error_mark_node;
6490 from_array = TREE_CODE (TREE_TYPE (newrhs)) == ARRAY_TYPE
6491 ? 1 + (modifycode != INIT_EXPR): 0;
6492 return build_vec_init (lhs, NULL_TREE, newrhs,
6493 /*explicit_value_init_p=*/false,
6494 from_array, complain);
6497 if (modifycode == INIT_EXPR)
6498 /* Calls with INIT_EXPR are all direct-initialization, so don't set
6499 LOOKUP_ONLYCONVERTING. */
6500 newrhs = convert_for_initialization (lhs, olhstype, newrhs, LOOKUP_NORMAL,
6501 "initialization", NULL_TREE, 0,
6502 complain);
6503 else
6504 newrhs = convert_for_assignment (olhstype, newrhs, "assignment",
6505 NULL_TREE, 0, complain, LOOKUP_IMPLICIT);
6507 if (!same_type_p (lhstype, olhstype))
6508 newrhs = cp_convert_and_check (lhstype, newrhs);
6510 if (modifycode != INIT_EXPR)
6512 if (TREE_CODE (newrhs) == CALL_EXPR
6513 && TYPE_NEEDS_CONSTRUCTING (lhstype))
6514 newrhs = build_cplus_new (lhstype, newrhs);
6516 /* Can't initialize directly from a TARGET_EXPR, since that would
6517 cause the lhs to be constructed twice, and possibly result in
6518 accidental self-initialization. So we force the TARGET_EXPR to be
6519 expanded without a target. */
6520 if (TREE_CODE (newrhs) == TARGET_EXPR)
6521 newrhs = build2 (COMPOUND_EXPR, TREE_TYPE (newrhs), newrhs,
6522 TREE_OPERAND (newrhs, 0));
6525 if (newrhs == error_mark_node)
6526 return error_mark_node;
6528 if (c_dialect_objc () && flag_objc_gc)
6530 result = objc_generate_write_barrier (lhs, modifycode, newrhs);
6532 if (result)
6533 return result;
6536 result = build2 (modifycode == NOP_EXPR ? MODIFY_EXPR : INIT_EXPR,
6537 lhstype, lhs, newrhs);
6539 TREE_SIDE_EFFECTS (result) = 1;
6540 if (!plain_assign)
6541 TREE_NO_WARNING (result) = 1;
6543 return result;
6546 tree
6547 build_x_modify_expr (tree lhs, enum tree_code modifycode, tree rhs,
6548 tsubst_flags_t complain)
6550 if (processing_template_decl)
6551 return build_min_nt (MODOP_EXPR, lhs,
6552 build_min_nt (modifycode, NULL_TREE, NULL_TREE), rhs);
6554 if (modifycode != NOP_EXPR)
6556 tree rval = build_new_op (MODIFY_EXPR, LOOKUP_NORMAL, lhs, rhs,
6557 make_node (modifycode),
6558 /*overloaded_p=*/NULL,
6559 complain);
6560 if (rval)
6562 TREE_NO_WARNING (rval) = 1;
6563 return rval;
6566 return cp_build_modify_expr (lhs, modifycode, rhs, complain);
6569 /* Helper function for get_delta_difference which assumes FROM is a base
6570 class of TO. Returns a delta for the conversion of pointer-to-member
6571 of FROM to pointer-to-member of TO. If the conversion is invalid,
6572 returns zero. If FROM is not a base class of TO, returns NULL_TREE.
6573 If C_CAST_P is true, this conversion is taking place as part of a C-style
6574 cast. */
6576 static tree
6577 get_delta_difference_1 (tree from, tree to, bool c_cast_p)
6579 tree binfo;
6580 base_kind kind;
6582 binfo = lookup_base (to, from, c_cast_p ? ba_unique : ba_check, &kind);
6583 if (kind == bk_inaccessible || kind == bk_ambig)
6585 error (" in pointer to member function conversion");
6586 return size_zero_node;
6588 else if (binfo)
6590 if (kind != bk_via_virtual)
6591 return BINFO_OFFSET (binfo);
6592 else
6593 /* FROM is a virtual base class of TO. Issue an error or warning
6594 depending on whether or not this is a reinterpret cast. */
6596 error ("pointer to member conversion via virtual base %qT",
6597 BINFO_TYPE (binfo_from_vbase (binfo)));
6599 return size_zero_node;
6602 else
6603 return NULL_TREE;
6606 /* Get difference in deltas for different pointer to member function
6607 types. Returns an integer constant of type PTRDIFF_TYPE_NODE. If
6608 the conversion is invalid, the constant is zero. If
6609 ALLOW_INVERSE_P is true, then allow reverse conversions as well.
6610 If C_CAST_P is true this conversion is taking place as part of a
6611 C-style cast.
6613 Note that the naming of FROM and TO is kind of backwards; the return
6614 value is what we add to a TO in order to get a FROM. They are named
6615 this way because we call this function to find out how to convert from
6616 a pointer to member of FROM to a pointer to member of TO. */
6618 static tree
6619 get_delta_difference (tree from, tree to,
6620 bool allow_inverse_p,
6621 bool c_cast_p)
6623 tree result;
6625 if (same_type_ignoring_top_level_qualifiers_p (from, to))
6626 /* Pointer to member of incomplete class is permitted*/
6627 result = size_zero_node;
6628 else
6629 result = get_delta_difference_1 (from, to, c_cast_p);
6631 if (!result)
6633 if (!allow_inverse_p)
6635 error_not_base_type (from, to);
6636 error (" in pointer to member conversion");
6637 result = size_zero_node;
6639 else
6641 result = get_delta_difference_1 (to, from, c_cast_p);
6643 if (result)
6644 result = size_diffop_loc (input_location,
6645 size_zero_node, result);
6646 else
6648 error_not_base_type (from, to);
6649 error (" in pointer to member conversion");
6650 result = size_zero_node;
6655 return fold_if_not_in_template (convert_to_integer (ptrdiff_type_node,
6656 result));
6659 /* Return a constructor for the pointer-to-member-function TYPE using
6660 the other components as specified. */
6662 tree
6663 build_ptrmemfunc1 (tree type, tree delta, tree pfn)
6665 tree u = NULL_TREE;
6666 tree delta_field;
6667 tree pfn_field;
6668 VEC(constructor_elt, gc) *v;
6670 /* Pull the FIELD_DECLs out of the type. */
6671 pfn_field = TYPE_FIELDS (type);
6672 delta_field = TREE_CHAIN (pfn_field);
6674 /* Make sure DELTA has the type we want. */
6675 delta = convert_and_check (delta_type_node, delta);
6677 /* Convert to the correct target type if necessary. */
6678 pfn = fold_convert (TREE_TYPE (pfn_field), pfn);
6680 /* Finish creating the initializer. */
6681 v = VEC_alloc(constructor_elt, gc, 2);
6682 CONSTRUCTOR_APPEND_ELT(v, pfn_field, pfn);
6683 CONSTRUCTOR_APPEND_ELT(v, delta_field, delta);
6684 u = build_constructor (type, v);
6685 TREE_CONSTANT (u) = TREE_CONSTANT (pfn) & TREE_CONSTANT (delta);
6686 TREE_STATIC (u) = (TREE_CONSTANT (u)
6687 && (initializer_constant_valid_p (pfn, TREE_TYPE (pfn))
6688 != NULL_TREE)
6689 && (initializer_constant_valid_p (delta, TREE_TYPE (delta))
6690 != NULL_TREE));
6691 return u;
6694 /* Build a constructor for a pointer to member function. It can be
6695 used to initialize global variables, local variable, or used
6696 as a value in expressions. TYPE is the POINTER to METHOD_TYPE we
6697 want to be.
6699 If FORCE is nonzero, then force this conversion, even if
6700 we would rather not do it. Usually set when using an explicit
6701 cast. A C-style cast is being processed iff C_CAST_P is true.
6703 Return error_mark_node, if something goes wrong. */
6705 tree
6706 build_ptrmemfunc (tree type, tree pfn, int force, bool c_cast_p)
6708 tree fn;
6709 tree pfn_type;
6710 tree to_type;
6712 if (error_operand_p (pfn))
6713 return error_mark_node;
6715 pfn_type = TREE_TYPE (pfn);
6716 to_type = build_ptrmemfunc_type (type);
6718 /* Handle multiple conversions of pointer to member functions. */
6719 if (TYPE_PTRMEMFUNC_P (pfn_type))
6721 tree delta = NULL_TREE;
6722 tree npfn = NULL_TREE;
6723 tree n;
6725 if (!force
6726 && !can_convert_arg (to_type, TREE_TYPE (pfn), pfn, LOOKUP_NORMAL))
6727 error ("invalid conversion to type %qT from type %qT",
6728 to_type, pfn_type);
6730 n = get_delta_difference (TYPE_PTRMEMFUNC_OBJECT_TYPE (pfn_type),
6731 TYPE_PTRMEMFUNC_OBJECT_TYPE (to_type),
6732 force,
6733 c_cast_p);
6735 /* We don't have to do any conversion to convert a
6736 pointer-to-member to its own type. But, we don't want to
6737 just return a PTRMEM_CST if there's an explicit cast; that
6738 cast should make the expression an invalid template argument. */
6739 if (TREE_CODE (pfn) != PTRMEM_CST)
6741 if (same_type_p (to_type, pfn_type))
6742 return pfn;
6743 else if (integer_zerop (n))
6744 return build_reinterpret_cast (to_type, pfn,
6745 tf_warning_or_error);
6748 if (TREE_SIDE_EFFECTS (pfn))
6749 pfn = save_expr (pfn);
6751 /* Obtain the function pointer and the current DELTA. */
6752 if (TREE_CODE (pfn) == PTRMEM_CST)
6753 expand_ptrmemfunc_cst (pfn, &delta, &npfn);
6754 else
6756 npfn = build_ptrmemfunc_access_expr (pfn, pfn_identifier);
6757 delta = build_ptrmemfunc_access_expr (pfn, delta_identifier);
6760 /* Just adjust the DELTA field. */
6761 gcc_assert (same_type_ignoring_top_level_qualifiers_p
6762 (TREE_TYPE (delta), ptrdiff_type_node));
6763 if (TARGET_PTRMEMFUNC_VBIT_LOCATION == ptrmemfunc_vbit_in_delta)
6764 n = cp_build_binary_op (input_location,
6765 LSHIFT_EXPR, n, integer_one_node,
6766 tf_warning_or_error);
6767 delta = cp_build_binary_op (input_location,
6768 PLUS_EXPR, delta, n, tf_warning_or_error);
6769 return build_ptrmemfunc1 (to_type, delta, npfn);
6772 /* Handle null pointer to member function conversions. */
6773 if (integer_zerop (pfn))
6775 pfn = build_c_cast (input_location, type, integer_zero_node);
6776 return build_ptrmemfunc1 (to_type,
6777 integer_zero_node,
6778 pfn);
6781 if (type_unknown_p (pfn))
6782 return instantiate_type (type, pfn, tf_warning_or_error);
6784 fn = TREE_OPERAND (pfn, 0);
6785 gcc_assert (TREE_CODE (fn) == FUNCTION_DECL
6786 /* In a template, we will have preserved the
6787 OFFSET_REF. */
6788 || (processing_template_decl && TREE_CODE (fn) == OFFSET_REF));
6789 return make_ptrmem_cst (to_type, fn);
6792 /* Return the DELTA, IDX, PFN, and DELTA2 values for the PTRMEM_CST
6793 given by CST.
6795 ??? There is no consistency as to the types returned for the above
6796 values. Some code acts as if it were a sizetype and some as if it were
6797 integer_type_node. */
6799 void
6800 expand_ptrmemfunc_cst (tree cst, tree *delta, tree *pfn)
6802 tree type = TREE_TYPE (cst);
6803 tree fn = PTRMEM_CST_MEMBER (cst);
6804 tree ptr_class, fn_class;
6806 gcc_assert (TREE_CODE (fn) == FUNCTION_DECL);
6808 /* The class that the function belongs to. */
6809 fn_class = DECL_CONTEXT (fn);
6811 /* The class that we're creating a pointer to member of. */
6812 ptr_class = TYPE_PTRMEMFUNC_OBJECT_TYPE (type);
6814 /* First, calculate the adjustment to the function's class. */
6815 *delta = get_delta_difference (fn_class, ptr_class, /*force=*/0,
6816 /*c_cast_p=*/0);
6818 if (!DECL_VIRTUAL_P (fn))
6819 *pfn = convert (TYPE_PTRMEMFUNC_FN_TYPE (type), build_addr_func (fn));
6820 else
6822 /* If we're dealing with a virtual function, we have to adjust 'this'
6823 again, to point to the base which provides the vtable entry for
6824 fn; the call will do the opposite adjustment. */
6825 tree orig_class = DECL_CONTEXT (fn);
6826 tree binfo = binfo_or_else (orig_class, fn_class);
6827 *delta = build2 (PLUS_EXPR, TREE_TYPE (*delta),
6828 *delta, BINFO_OFFSET (binfo));
6829 *delta = fold_if_not_in_template (*delta);
6831 /* We set PFN to the vtable offset at which the function can be
6832 found, plus one (unless ptrmemfunc_vbit_in_delta, in which
6833 case delta is shifted left, and then incremented). */
6834 *pfn = DECL_VINDEX (fn);
6835 *pfn = build2 (MULT_EXPR, integer_type_node, *pfn,
6836 TYPE_SIZE_UNIT (vtable_entry_type));
6837 *pfn = fold_if_not_in_template (*pfn);
6839 switch (TARGET_PTRMEMFUNC_VBIT_LOCATION)
6841 case ptrmemfunc_vbit_in_pfn:
6842 *pfn = build2 (PLUS_EXPR, integer_type_node, *pfn,
6843 integer_one_node);
6844 *pfn = fold_if_not_in_template (*pfn);
6845 break;
6847 case ptrmemfunc_vbit_in_delta:
6848 *delta = build2 (LSHIFT_EXPR, TREE_TYPE (*delta),
6849 *delta, integer_one_node);
6850 *delta = fold_if_not_in_template (*delta);
6851 *delta = build2 (PLUS_EXPR, TREE_TYPE (*delta),
6852 *delta, integer_one_node);
6853 *delta = fold_if_not_in_template (*delta);
6854 break;
6856 default:
6857 gcc_unreachable ();
6860 *pfn = build_nop (TYPE_PTRMEMFUNC_FN_TYPE (type), *pfn);
6861 *pfn = fold_if_not_in_template (*pfn);
6865 /* Return an expression for PFN from the pointer-to-member function
6866 given by T. */
6868 static tree
6869 pfn_from_ptrmemfunc (tree t)
6871 if (TREE_CODE (t) == PTRMEM_CST)
6873 tree delta;
6874 tree pfn;
6876 expand_ptrmemfunc_cst (t, &delta, &pfn);
6877 if (pfn)
6878 return pfn;
6881 return build_ptrmemfunc_access_expr (t, pfn_identifier);
6884 /* Return an expression for DELTA from the pointer-to-member function
6885 given by T. */
6887 static tree
6888 delta_from_ptrmemfunc (tree t)
6890 if (TREE_CODE (t) == PTRMEM_CST)
6892 tree delta;
6893 tree pfn;
6895 expand_ptrmemfunc_cst (t, &delta, &pfn);
6896 if (delta)
6897 return delta;
6900 return build_ptrmemfunc_access_expr (t, delta_identifier);
6903 /* Convert value RHS to type TYPE as preparation for an assignment to
6904 an lvalue of type TYPE. ERRTYPE is a string to use in error
6905 messages: "assignment", "return", etc. If FNDECL is non-NULL, we
6906 are doing the conversion in order to pass the PARMNUMth argument of
6907 FNDECL. */
6909 static tree
6910 convert_for_assignment (tree type, tree rhs,
6911 const char *errtype, tree fndecl, int parmnum,
6912 tsubst_flags_t complain, int flags)
6914 tree rhstype;
6915 enum tree_code coder;
6917 /* Strip NON_LVALUE_EXPRs since we aren't using as an lvalue. */
6918 if (TREE_CODE (rhs) == NON_LVALUE_EXPR)
6919 rhs = TREE_OPERAND (rhs, 0);
6921 rhstype = TREE_TYPE (rhs);
6922 coder = TREE_CODE (rhstype);
6924 if (TREE_CODE (type) == VECTOR_TYPE && coder == VECTOR_TYPE
6925 && vector_types_convertible_p (type, rhstype, true))
6926 return convert (type, rhs);
6928 if (rhs == error_mark_node || rhstype == error_mark_node)
6929 return error_mark_node;
6930 if (TREE_CODE (rhs) == TREE_LIST && TREE_VALUE (rhs) == error_mark_node)
6931 return error_mark_node;
6933 /* The RHS of an assignment cannot have void type. */
6934 if (coder == VOID_TYPE)
6936 if (complain & tf_error)
6937 error ("void value not ignored as it ought to be");
6938 return error_mark_node;
6941 /* Simplify the RHS if possible. */
6942 if (TREE_CODE (rhs) == CONST_DECL)
6943 rhs = DECL_INITIAL (rhs);
6945 if (c_dialect_objc ())
6947 int parmno;
6948 tree rname = fndecl;
6950 if (!strcmp (errtype, "assignment"))
6951 parmno = -1;
6952 else if (!strcmp (errtype, "initialization"))
6953 parmno = -2;
6954 else
6956 tree selector = objc_message_selector ();
6958 parmno = parmnum;
6960 if (selector && parmno > 1)
6962 rname = selector;
6963 parmno -= 1;
6967 if (objc_compare_types (type, rhstype, parmno, rname))
6968 return convert (type, rhs);
6971 /* [expr.ass]
6973 The expression is implicitly converted (clause _conv_) to the
6974 cv-unqualified type of the left operand.
6976 We allow bad conversions here because by the time we get to this point
6977 we are committed to doing the conversion. If we end up doing a bad
6978 conversion, convert_like will complain. */
6979 if (!can_convert_arg_bad (type, rhstype, rhs, flags))
6981 /* When -Wno-pmf-conversions is use, we just silently allow
6982 conversions from pointers-to-members to plain pointers. If
6983 the conversion doesn't work, cp_convert will complain. */
6984 if (!warn_pmf2ptr
6985 && TYPE_PTR_P (type)
6986 && TYPE_PTRMEMFUNC_P (rhstype))
6987 rhs = cp_convert (strip_top_quals (type), rhs);
6988 else
6990 if (complain & tf_error)
6992 /* If the right-hand side has unknown type, then it is an
6993 overloaded function. Call instantiate_type to get error
6994 messages. */
6995 if (rhstype == unknown_type_node)
6996 instantiate_type (type, rhs, tf_warning_or_error);
6997 else if (fndecl)
6998 error ("cannot convert %qT to %qT for argument %qP to %qD",
6999 rhstype, type, parmnum, fndecl);
7000 else
7001 error ("cannot convert %qT to %qT in %s", rhstype, type,
7002 errtype);
7004 return error_mark_node;
7007 if (warn_missing_format_attribute)
7009 const enum tree_code codel = TREE_CODE (type);
7010 if ((codel == POINTER_TYPE || codel == REFERENCE_TYPE)
7011 && coder == codel
7012 && check_missing_format_attribute (type, rhstype)
7013 && (complain & tf_warning))
7014 warning (OPT_Wmissing_format_attribute,
7015 "%s might be a candidate for a format attribute",
7016 errtype);
7019 /* If -Wparentheses, warn about a = b = c when a has type bool and b
7020 does not. */
7021 if (warn_parentheses
7022 && TREE_CODE (type) == BOOLEAN_TYPE
7023 && TREE_CODE (rhs) == MODIFY_EXPR
7024 && !TREE_NO_WARNING (rhs)
7025 && TREE_CODE (TREE_TYPE (rhs)) != BOOLEAN_TYPE
7026 && (complain & tf_warning))
7028 location_t loc = EXPR_HAS_LOCATION (rhs)
7029 ? EXPR_LOCATION (rhs) : input_location;
7031 warning_at (loc, OPT_Wparentheses,
7032 "suggest parentheses around assignment used as truth value");
7033 TREE_NO_WARNING (rhs) = 1;
7036 return perform_implicit_conversion_flags (strip_top_quals (type), rhs,
7037 complain, flags);
7040 /* Convert RHS to be of type TYPE.
7041 If EXP is nonzero, it is the target of the initialization.
7042 ERRTYPE is a string to use in error messages.
7044 Two major differences between the behavior of
7045 `convert_for_assignment' and `convert_for_initialization'
7046 are that references are bashed in the former, while
7047 copied in the latter, and aggregates are assigned in
7048 the former (operator=) while initialized in the
7049 latter (X(X&)).
7051 If using constructor make sure no conversion operator exists, if one does
7052 exist, an ambiguity exists.
7054 If flags doesn't include LOOKUP_COMPLAIN, don't complain about anything. */
7056 tree
7057 convert_for_initialization (tree exp, tree type, tree rhs, int flags,
7058 const char *errtype, tree fndecl, int parmnum,
7059 tsubst_flags_t complain)
7061 enum tree_code codel = TREE_CODE (type);
7062 tree rhstype;
7063 enum tree_code coder;
7065 /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
7066 Strip such NOP_EXPRs, since RHS is used in non-lvalue context. */
7067 if (TREE_CODE (rhs) == NOP_EXPR
7068 && TREE_TYPE (rhs) == TREE_TYPE (TREE_OPERAND (rhs, 0))
7069 && codel != REFERENCE_TYPE)
7070 rhs = TREE_OPERAND (rhs, 0);
7072 if (type == error_mark_node
7073 || rhs == error_mark_node
7074 || (TREE_CODE (rhs) == TREE_LIST && TREE_VALUE (rhs) == error_mark_node))
7075 return error_mark_node;
7077 if ((TREE_CODE (TREE_TYPE (rhs)) == ARRAY_TYPE
7078 && TREE_CODE (type) != ARRAY_TYPE
7079 && (TREE_CODE (type) != REFERENCE_TYPE
7080 || TREE_CODE (TREE_TYPE (type)) != ARRAY_TYPE))
7081 || (TREE_CODE (TREE_TYPE (rhs)) == FUNCTION_TYPE
7082 && (TREE_CODE (type) != REFERENCE_TYPE
7083 || TREE_CODE (TREE_TYPE (type)) != FUNCTION_TYPE))
7084 || TREE_CODE (TREE_TYPE (rhs)) == METHOD_TYPE)
7085 rhs = decay_conversion (rhs);
7087 rhstype = TREE_TYPE (rhs);
7088 coder = TREE_CODE (rhstype);
7090 if (coder == ERROR_MARK)
7091 return error_mark_node;
7093 /* We accept references to incomplete types, so we can
7094 return here before checking if RHS is of complete type. */
7096 if (codel == REFERENCE_TYPE)
7098 /* This should eventually happen in convert_arguments. */
7099 int savew = 0, savee = 0;
7101 if (fndecl)
7102 savew = warningcount, savee = errorcount;
7103 rhs = initialize_reference (type, rhs, /*decl=*/NULL_TREE,
7104 /*cleanup=*/NULL, complain);
7105 if (fndecl)
7107 if (warningcount > savew)
7108 warning (0, "in passing argument %P of %q+D", parmnum, fndecl);
7109 else if (errorcount > savee)
7110 error ("in passing argument %P of %q+D", parmnum, fndecl);
7112 return rhs;
7115 if (exp != 0)
7116 exp = require_complete_type (exp);
7117 if (exp == error_mark_node)
7118 return error_mark_node;
7120 rhstype = non_reference (rhstype);
7122 type = complete_type (type);
7124 if (DIRECT_INIT_EXPR_P (type, rhs))
7125 /* Don't try to do copy-initialization if we already have
7126 direct-initialization. */
7127 return rhs;
7129 if (MAYBE_CLASS_TYPE_P (type))
7130 return ocp_convert (type, rhs, CONV_IMPLICIT|CONV_FORCE_TEMP, flags);
7132 return convert_for_assignment (type, rhs, errtype, fndecl, parmnum,
7133 complain, flags);
7136 /* If RETVAL is the address of, or a reference to, a local variable or
7137 temporary give an appropriate warning. */
7139 static void
7140 maybe_warn_about_returning_address_of_local (tree retval)
7142 tree valtype = TREE_TYPE (DECL_RESULT (current_function_decl));
7143 tree whats_returned = retval;
7145 for (;;)
7147 if (TREE_CODE (whats_returned) == COMPOUND_EXPR)
7148 whats_returned = TREE_OPERAND (whats_returned, 1);
7149 else if (CONVERT_EXPR_P (whats_returned)
7150 || TREE_CODE (whats_returned) == NON_LVALUE_EXPR)
7151 whats_returned = TREE_OPERAND (whats_returned, 0);
7152 else
7153 break;
7156 if (TREE_CODE (whats_returned) != ADDR_EXPR)
7157 return;
7158 whats_returned = TREE_OPERAND (whats_returned, 0);
7160 if (TREE_CODE (valtype) == REFERENCE_TYPE)
7162 if (TREE_CODE (whats_returned) == AGGR_INIT_EXPR
7163 || TREE_CODE (whats_returned) == TARGET_EXPR)
7165 warning (0, "returning reference to temporary");
7166 return;
7168 if (TREE_CODE (whats_returned) == VAR_DECL
7169 && DECL_NAME (whats_returned)
7170 && TEMP_NAME_P (DECL_NAME (whats_returned)))
7172 warning (0, "reference to non-lvalue returned");
7173 return;
7177 while (TREE_CODE (whats_returned) == COMPONENT_REF
7178 || TREE_CODE (whats_returned) == ARRAY_REF)
7179 whats_returned = TREE_OPERAND (whats_returned, 0);
7181 if (DECL_P (whats_returned)
7182 && DECL_NAME (whats_returned)
7183 && DECL_FUNCTION_SCOPE_P (whats_returned)
7184 && !(TREE_STATIC (whats_returned)
7185 || TREE_PUBLIC (whats_returned)))
7187 if (TREE_CODE (valtype) == REFERENCE_TYPE)
7188 warning (0, "reference to local variable %q+D returned",
7189 whats_returned);
7190 else
7191 warning (0, "address of local variable %q+D returned",
7192 whats_returned);
7193 return;
7197 /* Check that returning RETVAL from the current function is valid.
7198 Return an expression explicitly showing all conversions required to
7199 change RETVAL into the function return type, and to assign it to
7200 the DECL_RESULT for the function. Set *NO_WARNING to true if
7201 code reaches end of non-void function warning shouldn't be issued
7202 on this RETURN_EXPR. */
7204 tree
7205 check_return_expr (tree retval, bool *no_warning)
7207 tree result;
7208 /* The type actually returned by the function, after any
7209 promotions. */
7210 tree valtype;
7211 int fn_returns_value_p;
7212 bool named_return_value_okay_p;
7214 *no_warning = false;
7216 /* A `volatile' function is one that isn't supposed to return, ever.
7217 (This is a G++ extension, used to get better code for functions
7218 that call the `volatile' function.) */
7219 if (TREE_THIS_VOLATILE (current_function_decl))
7220 warning (0, "function declared %<noreturn%> has a %<return%> statement");
7222 /* Check for various simple errors. */
7223 if (DECL_DESTRUCTOR_P (current_function_decl))
7225 if (retval)
7226 error ("returning a value from a destructor");
7227 return NULL_TREE;
7229 else if (DECL_CONSTRUCTOR_P (current_function_decl))
7231 if (in_function_try_handler)
7232 /* If a return statement appears in a handler of the
7233 function-try-block of a constructor, the program is ill-formed. */
7234 error ("cannot return from a handler of a function-try-block of a constructor");
7235 else if (retval)
7236 /* You can't return a value from a constructor. */
7237 error ("returning a value from a constructor");
7238 return NULL_TREE;
7241 /* As an extension, deduce lambda return type from a return statement
7242 anywhere in the body. */
7243 if (retval && LAMBDA_FUNCTION_P (current_function_decl))
7245 tree lambda = CLASSTYPE_LAMBDA_EXPR (current_class_type);
7246 if (LAMBDA_EXPR_DEDUCE_RETURN_TYPE_P (lambda))
7248 tree type = lambda_return_type (retval);
7249 tree oldtype = LAMBDA_EXPR_RETURN_TYPE (lambda);
7251 if (VOID_TYPE_P (type))
7252 { /* Nothing. */ }
7253 else if (oldtype == NULL_TREE)
7255 pedwarn (input_location, OPT_pedantic, "lambda return type "
7256 "can only be deduced when the return statement is "
7257 "the only statement in the function body");
7258 apply_lambda_return_type (lambda, type);
7260 else if (!same_type_p (type, oldtype))
7261 error ("inconsistent types %qT and %qT deduced for "
7262 "lambda return type", type, oldtype);
7266 if (processing_template_decl)
7268 current_function_returns_value = 1;
7269 if (check_for_bare_parameter_packs (retval))
7270 retval = error_mark_node;
7271 return retval;
7274 /* When no explicit return-value is given in a function with a named
7275 return value, the named return value is used. */
7276 result = DECL_RESULT (current_function_decl);
7277 valtype = TREE_TYPE (result);
7278 gcc_assert (valtype != NULL_TREE);
7279 fn_returns_value_p = !VOID_TYPE_P (valtype);
7280 if (!retval && DECL_NAME (result) && fn_returns_value_p)
7281 retval = result;
7283 /* Check for a return statement with no return value in a function
7284 that's supposed to return a value. */
7285 if (!retval && fn_returns_value_p)
7287 permerror (input_location, "return-statement with no value, in function returning %qT",
7288 valtype);
7289 /* Clear this, so finish_function won't say that we reach the
7290 end of a non-void function (which we don't, we gave a
7291 return!). */
7292 current_function_returns_null = 0;
7293 /* And signal caller that TREE_NO_WARNING should be set on the
7294 RETURN_EXPR to avoid control reaches end of non-void function
7295 warnings in tree-cfg.c. */
7296 *no_warning = true;
7298 /* Check for a return statement with a value in a function that
7299 isn't supposed to return a value. */
7300 else if (retval && !fn_returns_value_p)
7302 if (VOID_TYPE_P (TREE_TYPE (retval)))
7303 /* You can return a `void' value from a function of `void'
7304 type. In that case, we have to evaluate the expression for
7305 its side-effects. */
7306 finish_expr_stmt (retval);
7307 else
7308 permerror (input_location, "return-statement with a value, in function "
7309 "returning 'void'");
7310 current_function_returns_null = 1;
7312 /* There's really no value to return, after all. */
7313 return NULL_TREE;
7315 else if (!retval)
7316 /* Remember that this function can sometimes return without a
7317 value. */
7318 current_function_returns_null = 1;
7319 else
7320 /* Remember that this function did return a value. */
7321 current_function_returns_value = 1;
7323 /* Check for erroneous operands -- but after giving ourselves a
7324 chance to provide an error about returning a value from a void
7325 function. */
7326 if (error_operand_p (retval))
7328 current_function_return_value = error_mark_node;
7329 return error_mark_node;
7332 /* Only operator new(...) throw(), can return NULL [expr.new/13]. */
7333 if ((DECL_OVERLOADED_OPERATOR_P (current_function_decl) == NEW_EXPR
7334 || DECL_OVERLOADED_OPERATOR_P (current_function_decl) == VEC_NEW_EXPR)
7335 && !TYPE_NOTHROW_P (TREE_TYPE (current_function_decl))
7336 && ! flag_check_new
7337 && retval && null_ptr_cst_p (retval))
7338 warning (0, "%<operator new%> must not return NULL unless it is "
7339 "declared %<throw()%> (or -fcheck-new is in effect)");
7341 /* Effective C++ rule 15. See also start_function. */
7342 if (warn_ecpp
7343 && DECL_NAME (current_function_decl) == ansi_assopname(NOP_EXPR))
7345 bool warn = true;
7347 /* The function return type must be a reference to the current
7348 class. */
7349 if (TREE_CODE (valtype) == REFERENCE_TYPE
7350 && same_type_ignoring_top_level_qualifiers_p
7351 (TREE_TYPE (valtype), TREE_TYPE (current_class_ref)))
7353 /* Returning '*this' is obviously OK. */
7354 if (retval == current_class_ref)
7355 warn = false;
7356 /* If we are calling a function whose return type is the same of
7357 the current class reference, it is ok. */
7358 else if (TREE_CODE (retval) == INDIRECT_REF
7359 && TREE_CODE (TREE_OPERAND (retval, 0)) == CALL_EXPR)
7360 warn = false;
7363 if (warn)
7364 warning (OPT_Weffc__, "%<operator=%> should return a reference to %<*this%>");
7367 /* The fabled Named Return Value optimization, as per [class.copy]/15:
7369 [...] For a function with a class return type, if the expression
7370 in the return statement is the name of a local object, and the cv-
7371 unqualified type of the local object is the same as the function
7372 return type, an implementation is permitted to omit creating the tem-
7373 porary object to hold the function return value [...]
7375 So, if this is a value-returning function that always returns the same
7376 local variable, remember it.
7378 It might be nice to be more flexible, and choose the first suitable
7379 variable even if the function sometimes returns something else, but
7380 then we run the risk of clobbering the variable we chose if the other
7381 returned expression uses the chosen variable somehow. And people expect
7382 this restriction, anyway. (jason 2000-11-19)
7384 See finish_function and finalize_nrv for the rest of this optimization. */
7386 named_return_value_okay_p =
7387 (retval != NULL_TREE
7388 /* Must be a local, automatic variable. */
7389 && TREE_CODE (retval) == VAR_DECL
7390 && DECL_CONTEXT (retval) == current_function_decl
7391 && ! TREE_STATIC (retval)
7392 && ! DECL_ANON_UNION_VAR_P (retval)
7393 && (DECL_ALIGN (retval)
7394 >= DECL_ALIGN (DECL_RESULT (current_function_decl)))
7395 /* The cv-unqualified type of the returned value must be the
7396 same as the cv-unqualified return type of the
7397 function. */
7398 && same_type_p ((TYPE_MAIN_VARIANT (TREE_TYPE (retval))),
7399 (TYPE_MAIN_VARIANT
7400 (TREE_TYPE (TREE_TYPE (current_function_decl)))))
7401 /* And the returned value must be non-volatile. */
7402 && ! TYPE_VOLATILE (TREE_TYPE (retval)));
7404 if (fn_returns_value_p && flag_elide_constructors)
7406 if (named_return_value_okay_p
7407 && (current_function_return_value == NULL_TREE
7408 || current_function_return_value == retval))
7409 current_function_return_value = retval;
7410 else
7411 current_function_return_value = error_mark_node;
7414 /* We don't need to do any conversions when there's nothing being
7415 returned. */
7416 if (!retval)
7417 return NULL_TREE;
7419 /* Do any required conversions. */
7420 if (retval == result || DECL_CONSTRUCTOR_P (current_function_decl))
7421 /* No conversions are required. */
7423 else
7425 /* The type the function is declared to return. */
7426 tree functype = TREE_TYPE (TREE_TYPE (current_function_decl));
7427 int flags = LOOKUP_NORMAL | LOOKUP_ONLYCONVERTING;
7429 /* The functype's return type will have been set to void, if it
7430 was an incomplete type. Just treat this as 'return;' */
7431 if (VOID_TYPE_P (functype))
7432 return error_mark_node;
7434 /* Under C++0x [12.8/16 class.copy], a returned lvalue is sometimes
7435 treated as an rvalue for the purposes of overload resolution to
7436 favor move constructors over copy constructors. */
7437 if ((cxx_dialect != cxx98)
7438 && named_return_value_okay_p
7439 /* The variable must not have the `volatile' qualifier. */
7440 && !(cp_type_quals (TREE_TYPE (retval)) & TYPE_QUAL_VOLATILE)
7441 /* The return type must be a class type. */
7442 && CLASS_TYPE_P (TREE_TYPE (TREE_TYPE (current_function_decl))))
7443 flags = flags | LOOKUP_PREFER_RVALUE;
7445 /* First convert the value to the function's return type, then
7446 to the type of return value's location to handle the
7447 case that functype is smaller than the valtype. */
7448 retval = convert_for_initialization
7449 (NULL_TREE, functype, retval, flags, "return", NULL_TREE, 0,
7450 tf_warning_or_error);
7451 retval = convert (valtype, retval);
7453 /* If the conversion failed, treat this just like `return;'. */
7454 if (retval == error_mark_node)
7455 return retval;
7456 /* We can't initialize a register from a AGGR_INIT_EXPR. */
7457 else if (! cfun->returns_struct
7458 && TREE_CODE (retval) == TARGET_EXPR
7459 && TREE_CODE (TREE_OPERAND (retval, 1)) == AGGR_INIT_EXPR)
7460 retval = build2 (COMPOUND_EXPR, TREE_TYPE (retval), retval,
7461 TREE_OPERAND (retval, 0));
7462 else
7463 maybe_warn_about_returning_address_of_local (retval);
7466 /* Actually copy the value returned into the appropriate location. */
7467 if (retval && retval != result)
7468 retval = build2 (INIT_EXPR, TREE_TYPE (result), result, retval);
7470 return retval;
7474 /* Returns nonzero if the pointer-type FROM can be converted to the
7475 pointer-type TO via a qualification conversion. If CONSTP is -1,
7476 then we return nonzero if the pointers are similar, and the
7477 cv-qualification signature of FROM is a proper subset of that of TO.
7479 If CONSTP is positive, then all outer pointers have been
7480 const-qualified. */
7482 static int
7483 comp_ptr_ttypes_real (tree to, tree from, int constp)
7485 bool to_more_cv_qualified = false;
7486 bool is_opaque_pointer = false;
7488 for (; ; to = TREE_TYPE (to), from = TREE_TYPE (from))
7490 if (TREE_CODE (to) != TREE_CODE (from))
7491 return 0;
7493 if (TREE_CODE (from) == OFFSET_TYPE
7494 && !same_type_p (TYPE_OFFSET_BASETYPE (from),
7495 TYPE_OFFSET_BASETYPE (to)))
7496 return 0;
7498 /* Const and volatile mean something different for function types,
7499 so the usual checks are not appropriate. */
7500 if (TREE_CODE (to) != FUNCTION_TYPE && TREE_CODE (to) != METHOD_TYPE)
7502 /* In Objective-C++, some types may have been 'volatilized' by
7503 the compiler for EH; when comparing them here, the volatile
7504 qualification must be ignored. */
7505 bool objc_quals_match = objc_type_quals_match (to, from);
7507 if (!at_least_as_qualified_p (to, from) && !objc_quals_match)
7508 return 0;
7510 if (!at_least_as_qualified_p (from, to) && !objc_quals_match)
7512 if (constp == 0)
7513 return 0;
7514 to_more_cv_qualified = true;
7517 if (constp > 0)
7518 constp &= TYPE_READONLY (to);
7521 if (TREE_CODE (to) == VECTOR_TYPE)
7522 is_opaque_pointer = vector_targets_convertible_p (to, from);
7524 if (TREE_CODE (to) != POINTER_TYPE && !TYPE_PTRMEM_P (to))
7525 return ((constp >= 0 || to_more_cv_qualified)
7526 && (is_opaque_pointer
7527 || same_type_ignoring_top_level_qualifiers_p (to, from)));
7531 /* When comparing, say, char ** to char const **, this function takes
7532 the 'char *' and 'char const *'. Do not pass non-pointer/reference
7533 types to this function. */
7536 comp_ptr_ttypes (tree to, tree from)
7538 return comp_ptr_ttypes_real (to, from, 1);
7541 /* Returns true iff FNTYPE is a non-class type that involves
7542 error_mark_node. We can get FUNCTION_TYPE with buried error_mark_node
7543 if a parameter type is ill-formed. */
7545 bool
7546 error_type_p (const_tree type)
7548 tree t;
7550 switch (TREE_CODE (type))
7552 case ERROR_MARK:
7553 return true;
7555 case POINTER_TYPE:
7556 case REFERENCE_TYPE:
7557 case OFFSET_TYPE:
7558 return error_type_p (TREE_TYPE (type));
7560 case FUNCTION_TYPE:
7561 case METHOD_TYPE:
7562 if (error_type_p (TREE_TYPE (type)))
7563 return true;
7564 for (t = TYPE_ARG_TYPES (type); t; t = TREE_CHAIN (t))
7565 if (error_type_p (TREE_VALUE (t)))
7566 return true;
7567 return false;
7569 case RECORD_TYPE:
7570 if (TYPE_PTRMEMFUNC_P (type))
7571 return error_type_p (TYPE_PTRMEMFUNC_FN_TYPE (type));
7572 return false;
7574 default:
7575 return false;
7579 /* Returns 1 if to and from are (possibly multi-level) pointers to the same
7580 type or inheritance-related types, regardless of cv-quals. */
7583 ptr_reasonably_similar (const_tree to, const_tree from)
7585 for (; ; to = TREE_TYPE (to), from = TREE_TYPE (from))
7587 /* Any target type is similar enough to void. */
7588 if (TREE_CODE (to) == VOID_TYPE)
7589 return !error_type_p (from);
7590 if (TREE_CODE (from) == VOID_TYPE)
7591 return !error_type_p (to);
7593 if (TREE_CODE (to) != TREE_CODE (from))
7594 return 0;
7596 if (TREE_CODE (from) == OFFSET_TYPE
7597 && comptypes (TYPE_OFFSET_BASETYPE (to),
7598 TYPE_OFFSET_BASETYPE (from),
7599 COMPARE_BASE | COMPARE_DERIVED))
7600 continue;
7602 if (TREE_CODE (to) == VECTOR_TYPE
7603 && vector_types_convertible_p (to, from, false))
7604 return 1;
7606 if (TREE_CODE (to) == INTEGER_TYPE
7607 && TYPE_PRECISION (to) == TYPE_PRECISION (from))
7608 return 1;
7610 if (TREE_CODE (to) == FUNCTION_TYPE)
7611 return !error_type_p (to) && !error_type_p (from);
7613 if (TREE_CODE (to) != POINTER_TYPE)
7614 return comptypes
7615 (TYPE_MAIN_VARIANT (to), TYPE_MAIN_VARIANT (from),
7616 COMPARE_BASE | COMPARE_DERIVED);
7620 /* Return true if TO and FROM (both of which are POINTER_TYPEs or
7621 pointer-to-member types) are the same, ignoring cv-qualification at
7622 all levels. */
7624 bool
7625 comp_ptr_ttypes_const (tree to, tree from)
7627 bool is_opaque_pointer = false;
7629 for (; ; to = TREE_TYPE (to), from = TREE_TYPE (from))
7631 if (TREE_CODE (to) != TREE_CODE (from))
7632 return false;
7634 if (TREE_CODE (from) == OFFSET_TYPE
7635 && same_type_p (TYPE_OFFSET_BASETYPE (from),
7636 TYPE_OFFSET_BASETYPE (to)))
7637 continue;
7639 if (TREE_CODE (to) == VECTOR_TYPE)
7640 is_opaque_pointer = vector_targets_convertible_p (to, from);
7642 if (TREE_CODE (to) != POINTER_TYPE)
7643 return (is_opaque_pointer
7644 || same_type_ignoring_top_level_qualifiers_p (to, from));
7648 /* Returns the type qualifiers for this type, including the qualifiers on the
7649 elements for an array type. */
7652 cp_type_quals (const_tree type)
7654 /* This CONST_CAST is okay because strip_array_types returns its
7655 argument unmodified and we assign it to a const_tree. */
7656 type = strip_array_types (CONST_CAST_TREE(type));
7657 if (type == error_mark_node)
7658 return TYPE_UNQUALIFIED;
7659 return TYPE_QUALS (type);
7662 /* Returns nonzero if the TYPE is const from a C++ perspective: look inside
7663 arrays. */
7665 bool
7666 cp_type_readonly (const_tree type)
7668 /* This CONST_CAST is okay because strip_array_types returns its
7669 argument unmodified and we assign it to a const_tree. */
7670 type = strip_array_types (CONST_CAST_TREE(type));
7671 return TYPE_READONLY (type);
7674 /* Returns nonzero if TYPE is const or volatile. */
7676 bool
7677 cv_qualified_p (const_tree type)
7679 int quals = cp_type_quals (type);
7680 return (quals & (TYPE_QUAL_CONST|TYPE_QUAL_VOLATILE)) != 0;
7683 /* Returns nonzero if the TYPE contains a mutable member. */
7685 bool
7686 cp_has_mutable_p (const_tree type)
7688 /* This CONST_CAST is okay because strip_array_types returns its
7689 argument unmodified and we assign it to a const_tree. */
7690 type = strip_array_types (CONST_CAST_TREE(type));
7692 return CLASS_TYPE_P (type) && CLASSTYPE_HAS_MUTABLE (type);
7695 /* Set TREE_READONLY and TREE_VOLATILE on DECL as indicated by the
7696 TYPE_QUALS. For a VAR_DECL, this may be an optimistic
7697 approximation. In particular, consider:
7699 int f();
7700 struct S { int i; };
7701 const S s = { f(); }
7703 Here, we will make "s" as TREE_READONLY (because it is declared
7704 "const") -- only to reverse ourselves upon seeing that the
7705 initializer is non-constant. */
7707 void
7708 cp_apply_type_quals_to_decl (int type_quals, tree decl)
7710 tree type = TREE_TYPE (decl);
7712 if (type == error_mark_node)
7713 return;
7715 if (TREE_CODE (decl) == TYPE_DECL)
7716 return;
7718 if (TREE_CODE (type) == FUNCTION_TYPE
7719 && type_quals != TYPE_UNQUALIFIED)
7721 /* This was an error in C++98 (cv-qualifiers cannot be added to
7722 a function type), but DR 295 makes the code well-formed by
7723 dropping the extra qualifiers. */
7724 if (pedantic)
7726 tree bad_type = build_qualified_type (type, type_quals);
7727 pedwarn (input_location, OPT_pedantic,
7728 "ignoring %qV qualifiers added to function type %qT",
7729 bad_type, type);
7732 TREE_TYPE (decl) = TYPE_MAIN_VARIANT (type);
7733 return;
7736 /* Avoid setting TREE_READONLY incorrectly. */
7737 if (/* If the object has a constructor, the constructor may modify
7738 the object. */
7739 TYPE_NEEDS_CONSTRUCTING (type)
7740 /* If the type isn't complete, we don't know yet if it will need
7741 constructing. */
7742 || !COMPLETE_TYPE_P (type)
7743 /* If the type has a mutable component, that component might be
7744 modified. */
7745 || TYPE_HAS_MUTABLE_P (type))
7746 type_quals &= ~TYPE_QUAL_CONST;
7748 c_apply_type_quals_to_decl (type_quals, decl);
7751 /* Subroutine of casts_away_constness. Make T1 and T2 point at
7752 exemplar types such that casting T1 to T2 is casting away constness
7753 if and only if there is no implicit conversion from T1 to T2. */
7755 static void
7756 casts_away_constness_r (tree *t1, tree *t2)
7758 int quals1;
7759 int quals2;
7761 /* [expr.const.cast]
7763 For multi-level pointer to members and multi-level mixed pointers
7764 and pointers to members (conv.qual), the "member" aspect of a
7765 pointer to member level is ignored when determining if a const
7766 cv-qualifier has been cast away. */
7767 /* [expr.const.cast]
7769 For two pointer types:
7771 X1 is T1cv1,1 * ... cv1,N * where T1 is not a pointer type
7772 X2 is T2cv2,1 * ... cv2,M * where T2 is not a pointer type
7773 K is min(N,M)
7775 casting from X1 to X2 casts away constness if, for a non-pointer
7776 type T there does not exist an implicit conversion (clause
7777 _conv_) from:
7779 Tcv1,(N-K+1) * cv1,(N-K+2) * ... cv1,N *
7783 Tcv2,(M-K+1) * cv2,(M-K+2) * ... cv2,M *. */
7784 if ((!TYPE_PTR_P (*t1) && !TYPE_PTRMEM_P (*t1))
7785 || (!TYPE_PTR_P (*t2) && !TYPE_PTRMEM_P (*t2)))
7787 *t1 = cp_build_qualified_type (void_type_node,
7788 cp_type_quals (*t1));
7789 *t2 = cp_build_qualified_type (void_type_node,
7790 cp_type_quals (*t2));
7791 return;
7794 quals1 = cp_type_quals (*t1);
7795 quals2 = cp_type_quals (*t2);
7797 if (TYPE_PTRMEM_P (*t1))
7798 *t1 = TYPE_PTRMEM_POINTED_TO_TYPE (*t1);
7799 else
7800 *t1 = TREE_TYPE (*t1);
7801 if (TYPE_PTRMEM_P (*t2))
7802 *t2 = TYPE_PTRMEM_POINTED_TO_TYPE (*t2);
7803 else
7804 *t2 = TREE_TYPE (*t2);
7806 casts_away_constness_r (t1, t2);
7807 *t1 = build_pointer_type (*t1);
7808 *t2 = build_pointer_type (*t2);
7809 *t1 = cp_build_qualified_type (*t1, quals1);
7810 *t2 = cp_build_qualified_type (*t2, quals2);
7813 /* Returns nonzero if casting from TYPE1 to TYPE2 casts away
7814 constness.
7816 ??? This function returns non-zero if casting away qualifiers not
7817 just const. We would like to return to the caller exactly which
7818 qualifiers are casted away to give more accurate diagnostics.
7821 static bool
7822 casts_away_constness (tree t1, tree t2)
7824 if (TREE_CODE (t2) == REFERENCE_TYPE)
7826 /* [expr.const.cast]
7828 Casting from an lvalue of type T1 to an lvalue of type T2
7829 using a reference cast casts away constness if a cast from an
7830 rvalue of type "pointer to T1" to the type "pointer to T2"
7831 casts away constness. */
7832 t1 = (TREE_CODE (t1) == REFERENCE_TYPE ? TREE_TYPE (t1) : t1);
7833 return casts_away_constness (build_pointer_type (t1),
7834 build_pointer_type (TREE_TYPE (t2)));
7837 if (TYPE_PTRMEM_P (t1) && TYPE_PTRMEM_P (t2))
7838 /* [expr.const.cast]
7840 Casting from an rvalue of type "pointer to data member of X
7841 of type T1" to the type "pointer to data member of Y of type
7842 T2" casts away constness if a cast from an rvalue of type
7843 "pointer to T1" to the type "pointer to T2" casts away
7844 constness. */
7845 return casts_away_constness
7846 (build_pointer_type (TYPE_PTRMEM_POINTED_TO_TYPE (t1)),
7847 build_pointer_type (TYPE_PTRMEM_POINTED_TO_TYPE (t2)));
7849 /* Casting away constness is only something that makes sense for
7850 pointer or reference types. */
7851 if (TREE_CODE (t1) != POINTER_TYPE
7852 || TREE_CODE (t2) != POINTER_TYPE)
7853 return false;
7855 /* Top-level qualifiers don't matter. */
7856 t1 = TYPE_MAIN_VARIANT (t1);
7857 t2 = TYPE_MAIN_VARIANT (t2);
7858 casts_away_constness_r (&t1, &t2);
7859 if (!can_convert (t2, t1))
7860 return true;
7862 return false;
7865 /* If T is a REFERENCE_TYPE return the type to which T refers.
7866 Otherwise, return T itself. */
7868 tree
7869 non_reference (tree t)
7871 if (TREE_CODE (t) == REFERENCE_TYPE)
7872 t = TREE_TYPE (t);
7873 return t;
7877 /* Return nonzero if REF is an lvalue valid for this language;
7878 otherwise, print an error message and return zero. USE says
7879 how the lvalue is being used and so selects the error message. */
7882 lvalue_or_else (tree ref, enum lvalue_use use, tsubst_flags_t complain)
7884 int win = lvalue_p (ref);
7886 if (!win && (complain & tf_error))
7887 lvalue_error (use);
7889 return win;