tweak comments
[official-gcc.git] / gcc / cp / typeck.c
blobf68b48477151de63c06136419933a19f06dfb440
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 Free Software Foundation, Inc.
4 Hacked by Michael Tiemann (tiemann@cygnus.com)
6 This file is part of GCC.
8 GCC is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2, or (at your option)
11 any later version.
13 GCC is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with GCC; see the file COPYING. If not, write to
20 the Free Software Foundation, 51 Franklin Street, Fifth Floor,
21 Boston, MA 02110-1301, USA. */
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 "target.h"
43 #include "convert.h"
44 #include "c-common.h"
46 static tree pfn_from_ptrmemfunc (tree);
47 static tree convert_for_assignment (tree, tree, const char *, tree, int);
48 static tree cp_pointer_int_sum (enum tree_code, tree, tree);
49 static tree rationalize_conditional_expr (enum tree_code, tree);
50 static int comp_ptr_ttypes_real (tree, tree, int);
51 static bool comp_except_types (tree, tree, bool);
52 static bool comp_array_types (tree, tree, bool);
53 static tree common_base_type (tree, tree);
54 static tree pointer_diff (tree, tree, tree);
55 static tree get_delta_difference (tree, tree, bool, bool);
56 static void casts_away_constness_r (tree *, tree *);
57 static bool casts_away_constness (tree, tree);
58 static void maybe_warn_about_returning_address_of_local (tree);
59 static tree lookup_destructor (tree, tree, tree);
60 static tree convert_arguments (tree, tree, tree, int);
62 /* Do `exp = require_complete_type (exp);' to make sure exp
63 does not have an incomplete type. (That includes void types.)
64 Returns the error_mark_node if the VALUE does not have
65 complete type when this function returns. */
67 tree
68 require_complete_type (tree value)
70 tree type;
72 if (processing_template_decl || value == error_mark_node)
73 return value;
75 if (TREE_CODE (value) == OVERLOAD)
76 type = unknown_type_node;
77 else
78 type = TREE_TYPE (value);
80 if (type == error_mark_node)
81 return error_mark_node;
83 /* First, detect a valid value with a complete type. */
84 if (COMPLETE_TYPE_P (type))
85 return value;
87 if (complete_type_or_else (type, value))
88 return value;
89 else
90 return error_mark_node;
93 /* Try to complete TYPE, if it is incomplete. For example, if TYPE is
94 a template instantiation, do the instantiation. Returns TYPE,
95 whether or not it could be completed, unless something goes
96 horribly wrong, in which case the error_mark_node is returned. */
98 tree
99 complete_type (tree type)
101 if (type == NULL_TREE)
102 /* Rather than crash, we return something sure to cause an error
103 at some point. */
104 return error_mark_node;
106 if (type == error_mark_node || COMPLETE_TYPE_P (type))
108 else if (TREE_CODE (type) == ARRAY_TYPE && TYPE_DOMAIN (type))
110 tree t = complete_type (TREE_TYPE (type));
111 unsigned int needs_constructing, has_nontrivial_dtor;
112 if (COMPLETE_TYPE_P (t) && !dependent_type_p (type))
113 layout_type (type);
114 needs_constructing
115 = TYPE_NEEDS_CONSTRUCTING (TYPE_MAIN_VARIANT (t));
116 has_nontrivial_dtor
117 = TYPE_HAS_NONTRIVIAL_DESTRUCTOR (TYPE_MAIN_VARIANT (t));
118 for (t = TYPE_MAIN_VARIANT (type); t; t = TYPE_NEXT_VARIANT (t))
120 TYPE_NEEDS_CONSTRUCTING (t) = needs_constructing;
121 TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t) = has_nontrivial_dtor;
124 else if (CLASS_TYPE_P (type) && CLASSTYPE_TEMPLATE_INSTANTIATION (type))
125 instantiate_class_template (TYPE_MAIN_VARIANT (type));
127 return type;
130 /* Like complete_type, but issue an error if the TYPE cannot be completed.
131 VALUE is used for informative diagnostics.
132 Returns NULL_TREE if the type cannot be made complete. */
134 tree
135 complete_type_or_else (tree type, tree value)
137 type = complete_type (type);
138 if (type == error_mark_node)
139 /* We already issued an error. */
140 return NULL_TREE;
141 else if (!COMPLETE_TYPE_P (type))
143 cxx_incomplete_type_diagnostic (value, type, 0);
144 return NULL_TREE;
146 else
147 return type;
150 /* Return truthvalue of whether type of EXP is instantiated. */
153 type_unknown_p (tree exp)
155 return (TREE_CODE (exp) == TREE_LIST
156 || TREE_TYPE (exp) == unknown_type_node);
160 /* Return the common type of two parameter lists.
161 We assume that comptypes has already been done and returned 1;
162 if that isn't so, this may crash.
164 As an optimization, free the space we allocate if the parameter
165 lists are already common. */
167 static tree
168 commonparms (tree p1, tree p2)
170 tree oldargs = p1, newargs, n;
171 int i, len;
172 int any_change = 0;
174 len = list_length (p1);
175 newargs = tree_last (p1);
177 if (newargs == void_list_node)
178 i = 1;
179 else
181 i = 0;
182 newargs = 0;
185 for (; i < len; i++)
186 newargs = tree_cons (NULL_TREE, NULL_TREE, newargs);
188 n = newargs;
190 for (i = 0; p1;
191 p1 = TREE_CHAIN (p1), p2 = TREE_CHAIN (p2), n = TREE_CHAIN (n), i++)
193 if (TREE_PURPOSE (p1) && !TREE_PURPOSE (p2))
195 TREE_PURPOSE (n) = TREE_PURPOSE (p1);
196 any_change = 1;
198 else if (! TREE_PURPOSE (p1))
200 if (TREE_PURPOSE (p2))
202 TREE_PURPOSE (n) = TREE_PURPOSE (p2);
203 any_change = 1;
206 else
208 if (1 != simple_cst_equal (TREE_PURPOSE (p1), TREE_PURPOSE (p2)))
209 any_change = 1;
210 TREE_PURPOSE (n) = TREE_PURPOSE (p2);
212 if (TREE_VALUE (p1) != TREE_VALUE (p2))
214 any_change = 1;
215 TREE_VALUE (n) = merge_types (TREE_VALUE (p1), TREE_VALUE (p2));
217 else
218 TREE_VALUE (n) = TREE_VALUE (p1);
220 if (! any_change)
221 return oldargs;
223 return newargs;
226 /* Given a type, perhaps copied for a typedef,
227 find the "original" version of it. */
228 static tree
229 original_type (tree t)
231 while (TYPE_NAME (t) != NULL_TREE)
233 tree x = TYPE_NAME (t);
234 if (TREE_CODE (x) != TYPE_DECL)
235 break;
236 x = DECL_ORIGINAL_TYPE (x);
237 if (x == NULL_TREE)
238 break;
239 t = x;
241 return t;
244 /* T1 and T2 are arithmetic or enumeration types. Return the type
245 that will result from the "usual arithmetic conversions" on T1 and
246 T2 as described in [expr]. */
248 tree
249 type_after_usual_arithmetic_conversions (tree t1, tree t2)
251 enum tree_code code1 = TREE_CODE (t1);
252 enum tree_code code2 = TREE_CODE (t2);
253 tree attributes;
255 /* FIXME: Attributes. */
256 gcc_assert (ARITHMETIC_TYPE_P (t1)
257 || TREE_CODE (t1) == COMPLEX_TYPE
258 || TREE_CODE (t1) == VECTOR_TYPE
259 || TREE_CODE (t1) == ENUMERAL_TYPE);
260 gcc_assert (ARITHMETIC_TYPE_P (t2)
261 || TREE_CODE (t2) == COMPLEX_TYPE
262 || TREE_CODE (t1) == VECTOR_TYPE
263 || TREE_CODE (t2) == ENUMERAL_TYPE);
265 /* In what follows, we slightly generalize the rules given in [expr] so
266 as to deal with `long long' and `complex'. First, merge the
267 attributes. */
268 attributes = (*targetm.merge_type_attributes) (t1, t2);
270 /* If one type is complex, form the common type of the non-complex
271 components, then make that complex. Use T1 or T2 if it is the
272 required type. */
273 if (code1 == COMPLEX_TYPE || code2 == COMPLEX_TYPE)
275 tree subtype1 = code1 == COMPLEX_TYPE ? TREE_TYPE (t1) : t1;
276 tree subtype2 = code2 == COMPLEX_TYPE ? TREE_TYPE (t2) : t2;
277 tree subtype
278 = type_after_usual_arithmetic_conversions (subtype1, subtype2);
280 if (code1 == COMPLEX_TYPE && TREE_TYPE (t1) == subtype)
281 return build_type_attribute_variant (t1, attributes);
282 else if (code2 == COMPLEX_TYPE && TREE_TYPE (t2) == subtype)
283 return build_type_attribute_variant (t2, attributes);
284 else
285 return build_type_attribute_variant (build_complex_type (subtype),
286 attributes);
289 if (code1 == VECTOR_TYPE)
291 /* When we get here we should have two vectors of the same size.
292 Just prefer the unsigned one if present. */
293 if (TYPE_UNSIGNED (t1))
294 return build_type_attribute_variant (t1, attributes);
295 else
296 return build_type_attribute_variant (t2, attributes);
299 /* If only one is real, use it as the result. */
300 if (code1 == REAL_TYPE && code2 != REAL_TYPE)
301 return build_type_attribute_variant (t1, attributes);
302 if (code2 == REAL_TYPE && code1 != REAL_TYPE)
303 return build_type_attribute_variant (t2, attributes);
305 /* Perform the integral promotions. */
306 if (code1 != REAL_TYPE)
308 t1 = type_promotes_to (t1);
309 t2 = type_promotes_to (t2);
312 /* Both real or both integers; use the one with greater precision. */
313 if (TYPE_PRECISION (t1) > TYPE_PRECISION (t2))
314 return build_type_attribute_variant (t1, attributes);
315 else if (TYPE_PRECISION (t2) > TYPE_PRECISION (t1))
316 return build_type_attribute_variant (t2, attributes);
318 /* The types are the same; no need to do anything fancy. */
319 if (TYPE_MAIN_VARIANT (t1) == TYPE_MAIN_VARIANT (t2))
320 return build_type_attribute_variant (t1, attributes);
322 if (code1 != REAL_TYPE)
324 /* If one is a sizetype, use it so size_binop doesn't blow up. */
325 if (TYPE_IS_SIZETYPE (t1) > TYPE_IS_SIZETYPE (t2))
326 return build_type_attribute_variant (t1, attributes);
327 if (TYPE_IS_SIZETYPE (t2) > TYPE_IS_SIZETYPE (t1))
328 return build_type_attribute_variant (t2, attributes);
330 /* If one is unsigned long long, then convert the other to unsigned
331 long long. */
332 if (same_type_p (TYPE_MAIN_VARIANT (t1), long_long_unsigned_type_node)
333 || same_type_p (TYPE_MAIN_VARIANT (t2), long_long_unsigned_type_node))
334 return build_type_attribute_variant (long_long_unsigned_type_node,
335 attributes);
336 /* If one is a long long, and the other is an unsigned long, and
337 long long can represent all the values of an unsigned long, then
338 convert to a long long. Otherwise, convert to an unsigned long
339 long. Otherwise, if either operand is long long, convert the
340 other to long long.
342 Since we're here, we know the TYPE_PRECISION is the same;
343 therefore converting to long long cannot represent all the values
344 of an unsigned long, so we choose unsigned long long in that
345 case. */
346 if (same_type_p (TYPE_MAIN_VARIANT (t1), long_long_integer_type_node)
347 || same_type_p (TYPE_MAIN_VARIANT (t2), long_long_integer_type_node))
349 tree t = ((TYPE_UNSIGNED (t1) || TYPE_UNSIGNED (t2))
350 ? long_long_unsigned_type_node
351 : long_long_integer_type_node);
352 return build_type_attribute_variant (t, attributes);
355 /* Go through the same procedure, but for longs. */
356 if (same_type_p (TYPE_MAIN_VARIANT (t1), long_unsigned_type_node)
357 || same_type_p (TYPE_MAIN_VARIANT (t2), long_unsigned_type_node))
358 return build_type_attribute_variant (long_unsigned_type_node,
359 attributes);
360 if (same_type_p (TYPE_MAIN_VARIANT (t1), long_integer_type_node)
361 || same_type_p (TYPE_MAIN_VARIANT (t2), long_integer_type_node))
363 tree t = ((TYPE_UNSIGNED (t1) || TYPE_UNSIGNED (t2))
364 ? long_unsigned_type_node : long_integer_type_node);
365 return build_type_attribute_variant (t, attributes);
367 /* Otherwise prefer the unsigned one. */
368 if (TYPE_UNSIGNED (t1))
369 return build_type_attribute_variant (t1, attributes);
370 else
371 return build_type_attribute_variant (t2, attributes);
373 else
375 if (same_type_p (TYPE_MAIN_VARIANT (t1), long_double_type_node)
376 || same_type_p (TYPE_MAIN_VARIANT (t2), long_double_type_node))
377 return build_type_attribute_variant (long_double_type_node,
378 attributes);
379 if (same_type_p (TYPE_MAIN_VARIANT (t1), double_type_node)
380 || same_type_p (TYPE_MAIN_VARIANT (t2), double_type_node))
381 return build_type_attribute_variant (double_type_node,
382 attributes);
383 if (same_type_p (TYPE_MAIN_VARIANT (t1), float_type_node)
384 || same_type_p (TYPE_MAIN_VARIANT (t2), float_type_node))
385 return build_type_attribute_variant (float_type_node,
386 attributes);
388 /* Two floating-point types whose TYPE_MAIN_VARIANTs are none of
389 the standard C++ floating-point types. Logic earlier in this
390 function has already eliminated the possibility that
391 TYPE_PRECISION (t2) != TYPE_PRECISION (t1), so there's no
392 compelling reason to choose one or the other. */
393 return build_type_attribute_variant (t1, attributes);
397 /* Subroutine of composite_pointer_type to implement the recursive
398 case. See that function for documentation fo the parameters. */
400 static tree
401 composite_pointer_type_r (tree t1, tree t2, const char* location)
403 tree pointee1;
404 tree pointee2;
405 tree result_type;
406 tree attributes;
408 /* Determine the types pointed to by T1 and T2. */
409 if (TREE_CODE (t1) == POINTER_TYPE)
411 pointee1 = TREE_TYPE (t1);
412 pointee2 = TREE_TYPE (t2);
414 else
416 pointee1 = TYPE_PTRMEM_POINTED_TO_TYPE (t1);
417 pointee2 = TYPE_PTRMEM_POINTED_TO_TYPE (t2);
420 /* [expr.rel]
422 Otherwise, the composite pointer type is a pointer type
423 similar (_conv.qual_) to the type of one of the operands,
424 with a cv-qualification signature (_conv.qual_) that is the
425 union of the cv-qualification signatures of the operand
426 types. */
427 if (same_type_ignoring_top_level_qualifiers_p (pointee1, pointee2))
428 result_type = pointee1;
429 else if ((TREE_CODE (pointee1) == POINTER_TYPE
430 && TREE_CODE (pointee2) == POINTER_TYPE)
431 || (TYPE_PTR_TO_MEMBER_P (pointee1)
432 && TYPE_PTR_TO_MEMBER_P (pointee2)))
433 result_type = composite_pointer_type_r (pointee1, pointee2, location);
434 else
436 pedwarn ("%s between distinct pointer types %qT and %qT "
437 "lacks a cast",
438 location, t1, t2);
439 result_type = void_type_node;
441 result_type = cp_build_qualified_type (result_type,
442 (cp_type_quals (pointee1)
443 | cp_type_quals (pointee2)));
444 /* If the original types were pointers to members, so is the
445 result. */
446 if (TYPE_PTR_TO_MEMBER_P (t1))
448 if (!same_type_p (TYPE_PTRMEM_CLASS_TYPE (t1),
449 TYPE_PTRMEM_CLASS_TYPE (t2)))
450 pedwarn ("%s between distinct pointer types %qT and %qT "
451 "lacks a cast",
452 location, t1, t2);
453 result_type = build_ptrmem_type (TYPE_PTRMEM_CLASS_TYPE (t1),
454 result_type);
456 else
457 result_type = build_pointer_type (result_type);
459 /* Merge the attributes. */
460 attributes = (*targetm.merge_type_attributes) (t1, t2);
461 return build_type_attribute_variant (result_type, attributes);
464 /* Return the composite pointer type (see [expr.rel]) for T1 and T2.
465 ARG1 and ARG2 are the values with those types. The LOCATION is a
466 string describing the current location, in case an error occurs.
468 This routine also implements the computation of a common type for
469 pointers-to-members as per [expr.eq]. */
471 tree
472 composite_pointer_type (tree t1, tree t2, tree arg1, tree arg2,
473 const char* location)
475 tree class1;
476 tree class2;
478 /* [expr.rel]
480 If one operand is a null pointer constant, the composite pointer
481 type is the type of the other operand. */
482 if (null_ptr_cst_p (arg1))
483 return t2;
484 if (null_ptr_cst_p (arg2))
485 return t1;
487 /* We have:
489 [expr.rel]
491 If one of the operands has type "pointer to cv1 void*", then
492 the other has type "pointer to cv2T", and the composite pointer
493 type is "pointer to cv12 void", where cv12 is the union of cv1
494 and cv2.
496 If either type is a pointer to void, make sure it is T1. */
497 if (TREE_CODE (t2) == POINTER_TYPE && VOID_TYPE_P (TREE_TYPE (t2)))
499 tree t;
500 t = t1;
501 t1 = t2;
502 t2 = t;
505 /* Now, if T1 is a pointer to void, merge the qualifiers. */
506 if (TREE_CODE (t1) == POINTER_TYPE && VOID_TYPE_P (TREE_TYPE (t1)))
508 tree attributes;
509 tree result_type;
511 if (pedantic && TYPE_PTRFN_P (t2))
512 pedwarn ("ISO C++ forbids %s between pointer of type %<void *%> "
513 "and pointer-to-function", location);
514 result_type
515 = cp_build_qualified_type (void_type_node,
516 (cp_type_quals (TREE_TYPE (t1))
517 | cp_type_quals (TREE_TYPE (t2))));
518 result_type = build_pointer_type (result_type);
519 /* Merge the attributes. */
520 attributes = (*targetm.merge_type_attributes) (t1, t2);
521 return build_type_attribute_variant (result_type, attributes);
524 if (c_dialect_objc () && TREE_CODE (t1) == POINTER_TYPE
525 && TREE_CODE (t2) == POINTER_TYPE)
527 if (objc_compare_types (t1, t2, -3, NULL_TREE))
528 return t1;
531 /* [expr.eq] permits the application of a pointer conversion to
532 bring the pointers to a common type. */
533 if (TREE_CODE (t1) == POINTER_TYPE && TREE_CODE (t2) == POINTER_TYPE
534 && CLASS_TYPE_P (TREE_TYPE (t1))
535 && CLASS_TYPE_P (TREE_TYPE (t2))
536 && !same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (t1),
537 TREE_TYPE (t2)))
539 class1 = TREE_TYPE (t1);
540 class2 = TREE_TYPE (t2);
542 if (DERIVED_FROM_P (class1, class2))
543 t2 = (build_pointer_type
544 (cp_build_qualified_type (class1, TYPE_QUALS (class2))));
545 else if (DERIVED_FROM_P (class2, class1))
546 t1 = (build_pointer_type
547 (cp_build_qualified_type (class2, TYPE_QUALS (class1))));
548 else
550 error ("%s between distinct pointer types %qT and %qT "
551 "lacks a cast", location, t1, t2);
552 return error_mark_node;
555 /* [expr.eq] permits the application of a pointer-to-member
556 conversion to change the class type of one of the types. */
557 else if (TYPE_PTR_TO_MEMBER_P (t1)
558 && !same_type_p (TYPE_PTRMEM_CLASS_TYPE (t1),
559 TYPE_PTRMEM_CLASS_TYPE (t2)))
561 class1 = TYPE_PTRMEM_CLASS_TYPE (t1);
562 class2 = TYPE_PTRMEM_CLASS_TYPE (t2);
564 if (DERIVED_FROM_P (class1, class2))
565 t1 = build_ptrmem_type (class2, TYPE_PTRMEM_POINTED_TO_TYPE (t1));
566 else if (DERIVED_FROM_P (class2, class1))
567 t2 = build_ptrmem_type (class1, TYPE_PTRMEM_POINTED_TO_TYPE (t2));
568 else
570 error ("%s between distinct pointer-to-member types %qT and %qT "
571 "lacks a cast", location, t1, t2);
572 return error_mark_node;
576 return composite_pointer_type_r (t1, t2, location);
579 /* Return the merged type of two types.
580 We assume that comptypes has already been done and returned 1;
581 if that isn't so, this may crash.
583 This just combines attributes and default arguments; any other
584 differences would cause the two types to compare unalike. */
586 tree
587 merge_types (tree t1, tree t2)
589 enum tree_code code1;
590 enum tree_code code2;
591 tree attributes;
593 /* Save time if the two types are the same. */
594 if (t1 == t2)
595 return t1;
596 if (original_type (t1) == original_type (t2))
597 return t1;
599 /* If one type is nonsense, use the other. */
600 if (t1 == error_mark_node)
601 return t2;
602 if (t2 == error_mark_node)
603 return t1;
605 /* Merge the attributes. */
606 attributes = (*targetm.merge_type_attributes) (t1, t2);
608 if (TYPE_PTRMEMFUNC_P (t1))
609 t1 = TYPE_PTRMEMFUNC_FN_TYPE (t1);
610 if (TYPE_PTRMEMFUNC_P (t2))
611 t2 = TYPE_PTRMEMFUNC_FN_TYPE (t2);
613 code1 = TREE_CODE (t1);
614 code2 = TREE_CODE (t2);
616 switch (code1)
618 case POINTER_TYPE:
619 case REFERENCE_TYPE:
620 /* For two pointers, do this recursively on the target type. */
622 tree target = merge_types (TREE_TYPE (t1), TREE_TYPE (t2));
623 int quals = cp_type_quals (t1);
625 if (code1 == POINTER_TYPE)
626 t1 = build_pointer_type (target);
627 else
628 t1 = build_reference_type (target);
629 t1 = build_type_attribute_variant (t1, attributes);
630 t1 = cp_build_qualified_type (t1, quals);
632 if (TREE_CODE (target) == METHOD_TYPE)
633 t1 = build_ptrmemfunc_type (t1);
635 return t1;
638 case OFFSET_TYPE:
640 int quals;
641 tree pointee;
642 quals = cp_type_quals (t1);
643 pointee = merge_types (TYPE_PTRMEM_POINTED_TO_TYPE (t1),
644 TYPE_PTRMEM_POINTED_TO_TYPE (t2));
645 t1 = build_ptrmem_type (TYPE_PTRMEM_CLASS_TYPE (t1),
646 pointee);
647 t1 = cp_build_qualified_type (t1, quals);
648 break;
651 case ARRAY_TYPE:
653 tree elt = merge_types (TREE_TYPE (t1), TREE_TYPE (t2));
654 /* Save space: see if the result is identical to one of the args. */
655 if (elt == TREE_TYPE (t1) && TYPE_DOMAIN (t1))
656 return build_type_attribute_variant (t1, attributes);
657 if (elt == TREE_TYPE (t2) && TYPE_DOMAIN (t2))
658 return build_type_attribute_variant (t2, attributes);
659 /* Merge the element types, and have a size if either arg has one. */
660 t1 = build_cplus_array_type
661 (elt, TYPE_DOMAIN (TYPE_DOMAIN (t1) ? t1 : t2));
662 break;
665 case FUNCTION_TYPE:
666 /* Function types: prefer the one that specified arg types.
667 If both do, merge the arg types. Also merge the return types. */
669 tree valtype = merge_types (TREE_TYPE (t1), TREE_TYPE (t2));
670 tree p1 = TYPE_ARG_TYPES (t1);
671 tree p2 = TYPE_ARG_TYPES (t2);
672 tree rval, raises;
674 /* Save space: see if the result is identical to one of the args. */
675 if (valtype == TREE_TYPE (t1) && ! p2)
676 return cp_build_type_attribute_variant (t1, attributes);
677 if (valtype == TREE_TYPE (t2) && ! p1)
678 return cp_build_type_attribute_variant (t2, attributes);
680 /* Simple way if one arg fails to specify argument types. */
681 if (p1 == NULL_TREE || TREE_VALUE (p1) == void_type_node)
683 rval = build_function_type (valtype, p2);
684 if ((raises = TYPE_RAISES_EXCEPTIONS (t2)))
685 rval = build_exception_variant (rval, raises);
686 return cp_build_type_attribute_variant (rval, attributes);
688 raises = TYPE_RAISES_EXCEPTIONS (t1);
689 if (p2 == NULL_TREE || TREE_VALUE (p2) == void_type_node)
691 rval = build_function_type (valtype, p1);
692 if (raises)
693 rval = build_exception_variant (rval, raises);
694 return cp_build_type_attribute_variant (rval, attributes);
697 rval = build_function_type (valtype, commonparms (p1, p2));
698 t1 = build_exception_variant (rval, raises);
699 break;
702 case METHOD_TYPE:
704 /* Get this value the long way, since TYPE_METHOD_BASETYPE
705 is just the main variant of this. */
706 tree basetype = TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (t2)));
707 tree raises = TYPE_RAISES_EXCEPTIONS (t1);
708 tree t3;
710 /* If this was a member function type, get back to the
711 original type of type member function (i.e., without
712 the class instance variable up front. */
713 t1 = build_function_type (TREE_TYPE (t1),
714 TREE_CHAIN (TYPE_ARG_TYPES (t1)));
715 t2 = build_function_type (TREE_TYPE (t2),
716 TREE_CHAIN (TYPE_ARG_TYPES (t2)));
717 t3 = merge_types (t1, t2);
718 t3 = build_method_type_directly (basetype, TREE_TYPE (t3),
719 TYPE_ARG_TYPES (t3));
720 t1 = build_exception_variant (t3, raises);
721 break;
724 case TYPENAME_TYPE:
725 /* There is no need to merge attributes into a TYPENAME_TYPE.
726 When the type is instantiated it will have whatever
727 attributes result from the instantiation. */
728 return t1;
730 default:;
732 return cp_build_type_attribute_variant (t1, attributes);
735 /* Return the common type of two types.
736 We assume that comptypes has already been done and returned 1;
737 if that isn't so, this may crash.
739 This is the type for the result of most arithmetic operations
740 if the operands have the given two types. */
742 tree
743 common_type (tree t1, tree t2)
745 enum tree_code code1;
746 enum tree_code code2;
748 /* If one type is nonsense, bail. */
749 if (t1 == error_mark_node || t2 == error_mark_node)
750 return error_mark_node;
752 code1 = TREE_CODE (t1);
753 code2 = TREE_CODE (t2);
755 if ((ARITHMETIC_TYPE_P (t1) || code1 == ENUMERAL_TYPE
756 || code1 == COMPLEX_TYPE || code1 == VECTOR_TYPE)
757 && (ARITHMETIC_TYPE_P (t2) || code2 == ENUMERAL_TYPE
758 || code2 == COMPLEX_TYPE || code2 == VECTOR_TYPE))
759 return type_after_usual_arithmetic_conversions (t1, t2);
761 else if ((TYPE_PTR_P (t1) && TYPE_PTR_P (t2))
762 || (TYPE_PTRMEM_P (t1) && TYPE_PTRMEM_P (t2))
763 || (TYPE_PTRMEMFUNC_P (t1) && TYPE_PTRMEMFUNC_P (t2)))
764 return composite_pointer_type (t1, t2, error_mark_node, error_mark_node,
765 "conversion");
766 else
767 gcc_unreachable ();
770 /* Compare two exception specifier types for exactness or subsetness, if
771 allowed. Returns false for mismatch, true for match (same, or
772 derived and !exact).
774 [except.spec] "If a class X ... objects of class X or any class publicly
775 and unambiguously derived from X. Similarly, if a pointer type Y * ...
776 exceptions of type Y * or that are pointers to any type publicly and
777 unambiguously derived from Y. Otherwise a function only allows exceptions
778 that have the same type ..."
779 This does not mention cv qualifiers and is different to what throw
780 [except.throw] and catch [except.catch] will do. They will ignore the
781 top level cv qualifiers, and allow qualifiers in the pointer to class
782 example.
784 We implement the letter of the standard. */
786 static bool
787 comp_except_types (tree a, tree b, bool exact)
789 if (same_type_p (a, b))
790 return true;
791 else if (!exact)
793 if (cp_type_quals (a) || cp_type_quals (b))
794 return false;
796 if (TREE_CODE (a) == POINTER_TYPE
797 && TREE_CODE (b) == POINTER_TYPE)
799 a = TREE_TYPE (a);
800 b = TREE_TYPE (b);
801 if (cp_type_quals (a) || cp_type_quals (b))
802 return false;
805 if (TREE_CODE (a) != RECORD_TYPE
806 || TREE_CODE (b) != RECORD_TYPE)
807 return false;
809 if (PUBLICLY_UNIQUELY_DERIVED_P (a, b))
810 return true;
812 return false;
815 /* Return true if TYPE1 and TYPE2 are equivalent exception specifiers.
816 If EXACT is false, T2 can be stricter than T1 (according to 15.4/7),
817 otherwise it must be exact. Exception lists are unordered, but
818 we've already filtered out duplicates. Most lists will be in order,
819 we should try to make use of that. */
821 bool
822 comp_except_specs (tree t1, tree t2, bool exact)
824 tree probe;
825 tree base;
826 int length = 0;
828 if (t1 == t2)
829 return true;
831 if (t1 == NULL_TREE) /* T1 is ... */
832 return t2 == NULL_TREE || !exact;
833 if (!TREE_VALUE (t1)) /* t1 is EMPTY */
834 return t2 != NULL_TREE && !TREE_VALUE (t2);
835 if (t2 == NULL_TREE) /* T2 is ... */
836 return false;
837 if (TREE_VALUE (t1) && !TREE_VALUE (t2)) /* T2 is EMPTY, T1 is not */
838 return !exact;
840 /* Neither set is ... or EMPTY, make sure each part of T2 is in T1.
841 Count how many we find, to determine exactness. For exact matching and
842 ordered T1, T2, this is an O(n) operation, otherwise its worst case is
843 O(nm). */
844 for (base = t1; t2 != NULL_TREE; t2 = TREE_CHAIN (t2))
846 for (probe = base; probe != NULL_TREE; probe = TREE_CHAIN (probe))
848 tree a = TREE_VALUE (probe);
849 tree b = TREE_VALUE (t2);
851 if (comp_except_types (a, b, exact))
853 if (probe == base && exact)
854 base = TREE_CHAIN (probe);
855 length++;
856 break;
859 if (probe == NULL_TREE)
860 return false;
862 return !exact || base == NULL_TREE || length == list_length (t1);
865 /* Compare the array types T1 and T2. ALLOW_REDECLARATION is true if
866 [] can match [size]. */
868 static bool
869 comp_array_types (tree t1, tree t2, bool allow_redeclaration)
871 tree d1;
872 tree d2;
873 tree max1, max2;
875 if (t1 == t2)
876 return true;
878 /* The type of the array elements must be the same. */
879 if (!same_type_p (TREE_TYPE (t1), TREE_TYPE (t2)))
880 return false;
882 d1 = TYPE_DOMAIN (t1);
883 d2 = TYPE_DOMAIN (t2);
885 if (d1 == d2)
886 return true;
888 /* If one of the arrays is dimensionless, and the other has a
889 dimension, they are of different types. However, it is valid to
890 write:
892 extern int a[];
893 int a[3];
895 by [basic.link]:
897 declarations for an array object can specify
898 array types that differ by the presence or absence of a major
899 array bound (_dcl.array_). */
900 if (!d1 || !d2)
901 return allow_redeclaration;
903 /* Check that the dimensions are the same. */
905 if (!cp_tree_equal (TYPE_MIN_VALUE (d1), TYPE_MIN_VALUE (d2)))
906 return false;
907 max1 = TYPE_MAX_VALUE (d1);
908 max2 = TYPE_MAX_VALUE (d2);
909 if (processing_template_decl && !abi_version_at_least (2)
910 && !value_dependent_expression_p (max1)
911 && !value_dependent_expression_p (max2))
913 /* With abi-1 we do not fold non-dependent array bounds, (and
914 consequently mangle them incorrectly). We must therefore
915 fold them here, to verify the domains have the same
916 value. */
917 max1 = fold (max1);
918 max2 = fold (max2);
921 if (!cp_tree_equal (max1, max2))
922 return false;
924 return true;
927 /* Return true if T1 and T2 are related as allowed by STRICT. STRICT
928 is a bitwise-or of the COMPARE_* flags. */
930 bool
931 comptypes (tree t1, tree t2, int strict)
933 if (t1 == t2)
934 return true;
936 /* Suppress errors caused by previously reported errors. */
937 if (t1 == error_mark_node || t2 == error_mark_node)
938 return false;
940 gcc_assert (TYPE_P (t1) && TYPE_P (t2));
942 /* TYPENAME_TYPEs should be resolved if the qualifying scope is the
943 current instantiation. */
944 if (TREE_CODE (t1) == TYPENAME_TYPE)
946 tree resolved = resolve_typename_type (t1, /*only_current_p=*/true);
948 if (resolved != error_mark_node)
949 t1 = resolved;
952 if (TREE_CODE (t2) == TYPENAME_TYPE)
954 tree resolved = resolve_typename_type (t2, /*only_current_p=*/true);
956 if (resolved != error_mark_node)
957 t2 = resolved;
960 /* If either type is the internal version of sizetype, use the
961 language version. */
962 if (TREE_CODE (t1) == INTEGER_TYPE && TYPE_IS_SIZETYPE (t1)
963 && TYPE_ORIG_SIZE_TYPE (t1))
964 t1 = TYPE_ORIG_SIZE_TYPE (t1);
966 if (TREE_CODE (t2) == INTEGER_TYPE && TYPE_IS_SIZETYPE (t2)
967 && TYPE_ORIG_SIZE_TYPE (t2))
968 t2 = TYPE_ORIG_SIZE_TYPE (t2);
970 if (TYPE_PTRMEMFUNC_P (t1))
971 t1 = TYPE_PTRMEMFUNC_FN_TYPE (t1);
972 if (TYPE_PTRMEMFUNC_P (t2))
973 t2 = TYPE_PTRMEMFUNC_FN_TYPE (t2);
975 /* Different classes of types can't be compatible. */
976 if (TREE_CODE (t1) != TREE_CODE (t2))
977 return false;
979 /* Qualifiers must match. For array types, we will check when we
980 recur on the array element types. */
981 if (TREE_CODE (t1) != ARRAY_TYPE
982 && TYPE_QUALS (t1) != TYPE_QUALS (t2))
983 return false;
984 if (TYPE_FOR_JAVA (t1) != TYPE_FOR_JAVA (t2))
985 return false;
987 /* Allow for two different type nodes which have essentially the same
988 definition. Note that we already checked for equality of the type
989 qualifiers (just above). */
991 if (TREE_CODE (t1) != ARRAY_TYPE
992 && TYPE_MAIN_VARIANT (t1) == TYPE_MAIN_VARIANT (t2))
993 return true;
995 /* Compare the types. Break out if they could be the same. */
996 switch (TREE_CODE (t1))
998 case TEMPLATE_TEMPLATE_PARM:
999 case BOUND_TEMPLATE_TEMPLATE_PARM:
1000 if (TEMPLATE_TYPE_IDX (t1) != TEMPLATE_TYPE_IDX (t2)
1001 || TEMPLATE_TYPE_LEVEL (t1) != TEMPLATE_TYPE_LEVEL (t2))
1002 return false;
1003 if (!comp_template_parms
1004 (DECL_TEMPLATE_PARMS (TEMPLATE_TEMPLATE_PARM_TEMPLATE_DECL (t1)),
1005 DECL_TEMPLATE_PARMS (TEMPLATE_TEMPLATE_PARM_TEMPLATE_DECL (t2))))
1006 return false;
1007 if (TREE_CODE (t1) == TEMPLATE_TEMPLATE_PARM)
1008 break;
1009 /* Don't check inheritance. */
1010 strict = COMPARE_STRICT;
1011 /* Fall through. */
1013 case RECORD_TYPE:
1014 case UNION_TYPE:
1015 if (TYPE_TEMPLATE_INFO (t1) && TYPE_TEMPLATE_INFO (t2)
1016 && (TYPE_TI_TEMPLATE (t1) == TYPE_TI_TEMPLATE (t2)
1017 || TREE_CODE (t1) == BOUND_TEMPLATE_TEMPLATE_PARM)
1018 && comp_template_args (TYPE_TI_ARGS (t1), TYPE_TI_ARGS (t2)))
1019 break;
1021 if ((strict & COMPARE_BASE) && DERIVED_FROM_P (t1, t2))
1022 break;
1023 else if ((strict & COMPARE_DERIVED) && DERIVED_FROM_P (t2, t1))
1024 break;
1026 return false;
1028 case OFFSET_TYPE:
1029 if (!comptypes (TYPE_OFFSET_BASETYPE (t1), TYPE_OFFSET_BASETYPE (t2),
1030 strict & ~COMPARE_REDECLARATION))
1031 return false;
1032 if (!same_type_p (TREE_TYPE (t1), TREE_TYPE (t2)))
1033 return false;
1034 break;
1036 case POINTER_TYPE:
1037 case REFERENCE_TYPE:
1038 if (TYPE_MODE (t1) != TYPE_MODE (t2)
1039 || TYPE_REF_CAN_ALIAS_ALL (t1) != TYPE_REF_CAN_ALIAS_ALL (t2)
1040 || !same_type_p (TREE_TYPE (t1), TREE_TYPE (t2)))
1041 return false;
1042 break;
1044 case METHOD_TYPE:
1045 case FUNCTION_TYPE:
1046 if (!same_type_p (TREE_TYPE (t1), TREE_TYPE (t2)))
1047 return false;
1048 if (!compparms (TYPE_ARG_TYPES (t1), TYPE_ARG_TYPES (t2)))
1049 return false;
1050 break;
1052 case ARRAY_TYPE:
1053 /* Target types must match incl. qualifiers. */
1054 if (!comp_array_types (t1, t2, !!(strict & COMPARE_REDECLARATION)))
1055 return false;
1056 break;
1058 case TEMPLATE_TYPE_PARM:
1059 if (TEMPLATE_TYPE_IDX (t1) != TEMPLATE_TYPE_IDX (t2)
1060 || TEMPLATE_TYPE_LEVEL (t1) != TEMPLATE_TYPE_LEVEL (t2))
1061 return false;
1062 break;
1064 case TYPENAME_TYPE:
1065 if (!cp_tree_equal (TYPENAME_TYPE_FULLNAME (t1),
1066 TYPENAME_TYPE_FULLNAME (t2)))
1067 return false;
1068 if (!same_type_p (TYPE_CONTEXT (t1), TYPE_CONTEXT (t2)))
1069 return false;
1070 break;
1072 case UNBOUND_CLASS_TEMPLATE:
1073 if (!cp_tree_equal (TYPE_IDENTIFIER (t1), TYPE_IDENTIFIER (t2)))
1074 return false;
1075 if (!same_type_p (TYPE_CONTEXT (t1), TYPE_CONTEXT (t2)))
1076 return false;
1077 break;
1079 case COMPLEX_TYPE:
1080 if (!same_type_p (TREE_TYPE (t1), TREE_TYPE (t2)))
1081 return false;
1082 break;
1084 case VECTOR_TYPE:
1085 if (TYPE_VECTOR_SUBPARTS (t1) != TYPE_VECTOR_SUBPARTS (t2)
1086 || !same_type_p (TREE_TYPE (t1), TREE_TYPE (t2)))
1087 return false;
1088 break;
1090 default:
1091 return false;
1094 /* If we get here, we know that from a target independent POV the
1095 types are the same. Make sure the target attributes are also
1096 the same. */
1097 return targetm.comp_type_attributes (t1, t2);
1100 /* Returns 1 if TYPE1 is at least as qualified as TYPE2. */
1102 bool
1103 at_least_as_qualified_p (tree type1, tree type2)
1105 int q1 = cp_type_quals (type1);
1106 int q2 = cp_type_quals (type2);
1108 /* All qualifiers for TYPE2 must also appear in TYPE1. */
1109 return (q1 & q2) == q2;
1112 /* Returns 1 if TYPE1 is more cv-qualified than TYPE2, -1 if TYPE2 is
1113 more cv-qualified that TYPE1, and 0 otherwise. */
1116 comp_cv_qualification (tree type1, tree type2)
1118 int q1 = cp_type_quals (type1);
1119 int q2 = cp_type_quals (type2);
1121 if (q1 == q2)
1122 return 0;
1124 if ((q1 & q2) == q2)
1125 return 1;
1126 else if ((q1 & q2) == q1)
1127 return -1;
1129 return 0;
1132 /* Returns 1 if the cv-qualification signature of TYPE1 is a proper
1133 subset of the cv-qualification signature of TYPE2, and the types
1134 are similar. Returns -1 if the other way 'round, and 0 otherwise. */
1137 comp_cv_qual_signature (tree type1, tree type2)
1139 if (comp_ptr_ttypes_real (type2, type1, -1))
1140 return 1;
1141 else if (comp_ptr_ttypes_real (type1, type2, -1))
1142 return -1;
1143 else
1144 return 0;
1147 /* If two types share a common base type, return that basetype.
1148 If there is not a unique most-derived base type, this function
1149 returns ERROR_MARK_NODE. */
1151 static tree
1152 common_base_type (tree tt1, tree tt2)
1154 tree best = NULL_TREE;
1155 int i;
1157 /* If one is a baseclass of another, that's good enough. */
1158 if (UNIQUELY_DERIVED_FROM_P (tt1, tt2))
1159 return tt1;
1160 if (UNIQUELY_DERIVED_FROM_P (tt2, tt1))
1161 return tt2;
1163 /* Otherwise, try to find a unique baseclass of TT1
1164 that is shared by TT2, and follow that down. */
1165 for (i = BINFO_N_BASE_BINFOS (TYPE_BINFO (tt1))-1; i >= 0; i--)
1167 tree basetype = BINFO_TYPE (BINFO_BASE_BINFO (TYPE_BINFO (tt1), i));
1168 tree trial = common_base_type (basetype, tt2);
1170 if (trial)
1172 if (trial == error_mark_node)
1173 return trial;
1174 if (best == NULL_TREE)
1175 best = trial;
1176 else if (best != trial)
1177 return error_mark_node;
1181 /* Same for TT2. */
1182 for (i = BINFO_N_BASE_BINFOS (TYPE_BINFO (tt2))-1; i >= 0; i--)
1184 tree basetype = BINFO_TYPE (BINFO_BASE_BINFO (TYPE_BINFO (tt2), i));
1185 tree trial = common_base_type (tt1, basetype);
1187 if (trial)
1189 if (trial == error_mark_node)
1190 return trial;
1191 if (best == NULL_TREE)
1192 best = trial;
1193 else if (best != trial)
1194 return error_mark_node;
1197 return best;
1200 /* Subroutines of `comptypes'. */
1202 /* Return true if two parameter type lists PARMS1 and PARMS2 are
1203 equivalent in the sense that functions with those parameter types
1204 can have equivalent types. The two lists must be equivalent,
1205 element by element. */
1207 bool
1208 compparms (tree parms1, tree parms2)
1210 tree t1, t2;
1212 /* An unspecified parmlist matches any specified parmlist
1213 whose argument types don't need default promotions. */
1215 for (t1 = parms1, t2 = parms2;
1216 t1 || t2;
1217 t1 = TREE_CHAIN (t1), t2 = TREE_CHAIN (t2))
1219 /* If one parmlist is shorter than the other,
1220 they fail to match. */
1221 if (!t1 || !t2)
1222 return false;
1223 if (!same_type_p (TREE_VALUE (t1), TREE_VALUE (t2)))
1224 return false;
1226 return true;
1230 /* Process a sizeof or alignof expression where the operand is a
1231 type. */
1233 tree
1234 cxx_sizeof_or_alignof_type (tree type, enum tree_code op, bool complain)
1236 enum tree_code type_code;
1237 tree value;
1238 const char *op_name;
1240 gcc_assert (op == SIZEOF_EXPR || op == ALIGNOF_EXPR);
1241 if (type == error_mark_node)
1242 return error_mark_node;
1244 if (dependent_type_p (type))
1246 value = build_min (op, size_type_node, type);
1247 TREE_READONLY (value) = 1;
1248 return value;
1251 op_name = operator_name_info[(int) op].name;
1253 type = non_reference (type);
1254 type_code = TREE_CODE (type);
1256 if (type_code == METHOD_TYPE)
1258 if (complain && (pedantic || warn_pointer_arith))
1259 pedwarn ("invalid application of %qs to a member function", op_name);
1260 value = size_one_node;
1262 else
1263 value = c_sizeof_or_alignof_type (complete_type (type),
1264 op == SIZEOF_EXPR,
1265 complain);
1267 return value;
1270 /* Process a sizeof expression where the operand is an expression. */
1272 static tree
1273 cxx_sizeof_expr (tree e)
1275 if (e == error_mark_node)
1276 return error_mark_node;
1278 if (processing_template_decl)
1280 e = build_min (SIZEOF_EXPR, size_type_node, e);
1281 TREE_SIDE_EFFECTS (e) = 0;
1282 TREE_READONLY (e) = 1;
1284 return e;
1287 if (TREE_CODE (e) == COMPONENT_REF
1288 && TREE_CODE (TREE_OPERAND (e, 1)) == FIELD_DECL
1289 && DECL_C_BIT_FIELD (TREE_OPERAND (e, 1)))
1291 error ("invalid application of %<sizeof%> to a bit-field");
1292 e = char_type_node;
1294 else if (is_overloaded_fn (e))
1296 pedwarn ("ISO C++ forbids applying %<sizeof%> to an expression of "
1297 "function type");
1298 e = char_type_node;
1300 else if (type_unknown_p (e))
1302 cxx_incomplete_type_error (e, TREE_TYPE (e));
1303 e = char_type_node;
1305 else
1306 e = TREE_TYPE (e);
1308 return cxx_sizeof_or_alignof_type (e, SIZEOF_EXPR, true);
1311 /* Implement the __alignof keyword: Return the minimum required
1312 alignment of E, measured in bytes. For VAR_DECL's and
1313 FIELD_DECL's return DECL_ALIGN (which can be set from an
1314 "aligned" __attribute__ specification). */
1316 static tree
1317 cxx_alignof_expr (tree e)
1319 tree t;
1321 if (e == error_mark_node)
1322 return error_mark_node;
1324 if (processing_template_decl)
1326 e = build_min (ALIGNOF_EXPR, size_type_node, e);
1327 TREE_SIDE_EFFECTS (e) = 0;
1328 TREE_READONLY (e) = 1;
1330 return e;
1333 if (TREE_CODE (e) == VAR_DECL)
1334 t = size_int (DECL_ALIGN_UNIT (e));
1335 else if (TREE_CODE (e) == COMPONENT_REF
1336 && TREE_CODE (TREE_OPERAND (e, 1)) == FIELD_DECL
1337 && DECL_C_BIT_FIELD (TREE_OPERAND (e, 1)))
1339 error ("invalid application of %<__alignof%> to a bit-field");
1340 t = size_one_node;
1342 else if (TREE_CODE (e) == COMPONENT_REF
1343 && TREE_CODE (TREE_OPERAND (e, 1)) == FIELD_DECL)
1344 t = size_int (DECL_ALIGN_UNIT (TREE_OPERAND (e, 1)));
1345 else if (is_overloaded_fn (e))
1347 pedwarn ("ISO C++ forbids applying %<__alignof%> to an expression of "
1348 "function type");
1349 t = size_one_node;
1351 else if (type_unknown_p (e))
1353 cxx_incomplete_type_error (e, TREE_TYPE (e));
1354 t = size_one_node;
1356 else
1357 return cxx_sizeof_or_alignof_type (TREE_TYPE (e), ALIGNOF_EXPR, true);
1359 return fold_convert (size_type_node, t);
1362 /* Process a sizeof or alignof expression E with code OP where the operand
1363 is an expression. */
1365 tree
1366 cxx_sizeof_or_alignof_expr (tree e, enum tree_code op)
1368 if (op == SIZEOF_EXPR)
1369 return cxx_sizeof_expr (e);
1370 else
1371 return cxx_alignof_expr (e);
1374 /* EXPR is being used in a context that is not a function call.
1375 Enforce:
1377 [expr.ref]
1379 The expression can be used only as the left-hand operand of a
1380 member function call.
1382 [expr.mptr.operator]
1384 If the result of .* or ->* is a function, then that result can be
1385 used only as the operand for the function call operator ().
1387 by issuing an error message if appropriate. Returns true iff EXPR
1388 violates these rules. */
1390 bool
1391 invalid_nonstatic_memfn_p (tree expr)
1393 if (TREE_CODE (TREE_TYPE (expr)) == METHOD_TYPE)
1395 error ("invalid use of non-static member function");
1396 return true;
1398 return false;
1401 /* Perform the conversions in [expr] that apply when an lvalue appears
1402 in an rvalue context: the lvalue-to-rvalue, array-to-pointer, and
1403 function-to-pointer conversions.
1405 In addition manifest constants are replaced by their values. */
1407 tree
1408 decay_conversion (tree exp)
1410 tree type;
1411 enum tree_code code;
1413 type = TREE_TYPE (exp);
1414 code = TREE_CODE (type);
1416 if (type == error_mark_node)
1417 return error_mark_node;
1419 if (type_unknown_p (exp))
1421 cxx_incomplete_type_error (exp, TREE_TYPE (exp));
1422 return error_mark_node;
1425 exp = decl_constant_value (exp);
1427 /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
1428 Leave such NOP_EXPRs, since RHS is being used in non-lvalue context. */
1430 if (code == VOID_TYPE)
1432 error ("void value not ignored as it ought to be");
1433 return error_mark_node;
1435 if (invalid_nonstatic_memfn_p (exp))
1436 return error_mark_node;
1437 if (code == FUNCTION_TYPE || is_overloaded_fn (exp))
1438 return build_unary_op (ADDR_EXPR, exp, 0);
1439 if (code == ARRAY_TYPE)
1441 tree adr;
1442 tree ptrtype;
1444 if (TREE_CODE (exp) == INDIRECT_REF)
1445 return build_nop (build_pointer_type (TREE_TYPE (type)),
1446 TREE_OPERAND (exp, 0));
1448 if (TREE_CODE (exp) == COMPOUND_EXPR)
1450 tree op1 = decay_conversion (TREE_OPERAND (exp, 1));
1451 return build2 (COMPOUND_EXPR, TREE_TYPE (op1),
1452 TREE_OPERAND (exp, 0), op1);
1455 if (!lvalue_p (exp)
1456 && ! (TREE_CODE (exp) == CONSTRUCTOR && TREE_STATIC (exp)))
1458 error ("invalid use of non-lvalue array");
1459 return error_mark_node;
1462 ptrtype = build_pointer_type (TREE_TYPE (type));
1464 if (TREE_CODE (exp) == VAR_DECL)
1466 if (!cxx_mark_addressable (exp))
1467 return error_mark_node;
1468 adr = build_nop (ptrtype, build_address (exp));
1469 return adr;
1471 /* This way is better for a COMPONENT_REF since it can
1472 simplify the offset for a component. */
1473 adr = build_unary_op (ADDR_EXPR, exp, 1);
1474 return cp_convert (ptrtype, adr);
1477 /* [basic.lval]: Class rvalues can have cv-qualified types; non-class
1478 rvalues always have cv-unqualified types. */
1479 if (! CLASS_TYPE_P (type))
1480 exp = cp_convert (TYPE_MAIN_VARIANT (type), exp);
1482 return exp;
1485 tree
1486 default_conversion (tree exp)
1488 exp = decay_conversion (exp);
1490 if (INTEGRAL_OR_ENUMERATION_TYPE_P (TREE_TYPE (exp)))
1491 exp = perform_integral_promotions (exp);
1493 return exp;
1496 /* EXPR is an expression with an integral or enumeration type.
1497 Perform the integral promotions in [conv.prom], and return the
1498 converted value. */
1500 tree
1501 perform_integral_promotions (tree expr)
1503 tree type;
1504 tree promoted_type;
1506 type = TREE_TYPE (expr);
1507 gcc_assert (INTEGRAL_OR_ENUMERATION_TYPE_P (type));
1508 promoted_type = type_promotes_to (type);
1509 if (type != promoted_type)
1510 expr = cp_convert (promoted_type, expr);
1511 return expr;
1514 /* Take the address of an inline function without setting TREE_ADDRESSABLE
1515 or TREE_USED. */
1517 tree
1518 inline_conversion (tree exp)
1520 if (TREE_CODE (exp) == FUNCTION_DECL)
1521 exp = build1 (ADDR_EXPR, build_pointer_type (TREE_TYPE (exp)), exp);
1523 return exp;
1526 /* Returns nonzero iff exp is a STRING_CST or the result of applying
1527 decay_conversion to one. */
1530 string_conv_p (tree totype, tree exp, int warn)
1532 tree t;
1534 if (TREE_CODE (totype) != POINTER_TYPE)
1535 return 0;
1537 t = TREE_TYPE (totype);
1538 if (!same_type_p (t, char_type_node)
1539 && !same_type_p (t, wchar_type_node))
1540 return 0;
1542 if (TREE_CODE (exp) == STRING_CST)
1544 /* Make sure that we don't try to convert between char and wchar_t. */
1545 if (!same_type_p (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (exp))), t))
1546 return 0;
1548 else
1550 /* Is this a string constant which has decayed to 'const char *'? */
1551 t = build_pointer_type (build_qualified_type (t, TYPE_QUAL_CONST));
1552 if (!same_type_p (TREE_TYPE (exp), t))
1553 return 0;
1554 STRIP_NOPS (exp);
1555 if (TREE_CODE (exp) != ADDR_EXPR
1556 || TREE_CODE (TREE_OPERAND (exp, 0)) != STRING_CST)
1557 return 0;
1560 /* This warning is not very useful, as it complains about printf. */
1561 if (warn)
1562 warning (OPT_Wwrite_strings, "deprecated conversion from string constant to %qT'", totype);
1564 return 1;
1567 /* Given a COND_EXPR, MIN_EXPR, or MAX_EXPR in T, return it in a form that we
1568 can, for example, use as an lvalue. This code used to be in
1569 unary_complex_lvalue, but we needed it to deal with `a = (d == c) ? b : c'
1570 expressions, where we're dealing with aggregates. But now it's again only
1571 called from unary_complex_lvalue. The case (in particular) that led to
1572 this was with CODE == ADDR_EXPR, since it's not an lvalue when we'd
1573 get it there. */
1575 static tree
1576 rationalize_conditional_expr (enum tree_code code, tree t)
1578 /* For MIN_EXPR or MAX_EXPR, fold-const.c has arranged things so that
1579 the first operand is always the one to be used if both operands
1580 are equal, so we know what conditional expression this used to be. */
1581 if (TREE_CODE (t) == MIN_EXPR || TREE_CODE (t) == MAX_EXPR)
1583 /* The following code is incorrect if either operand side-effects. */
1584 gcc_assert (!TREE_SIDE_EFFECTS (TREE_OPERAND (t, 0))
1585 && !TREE_SIDE_EFFECTS (TREE_OPERAND (t, 1)));
1586 return
1587 build_conditional_expr (build_x_binary_op ((TREE_CODE (t) == MIN_EXPR
1588 ? LE_EXPR : GE_EXPR),
1589 TREE_OPERAND (t, 0),
1590 TREE_OPERAND (t, 1),
1591 /*overloaded_p=*/NULL),
1592 build_unary_op (code, TREE_OPERAND (t, 0), 0),
1593 build_unary_op (code, TREE_OPERAND (t, 1), 0));
1596 return
1597 build_conditional_expr (TREE_OPERAND (t, 0),
1598 build_unary_op (code, TREE_OPERAND (t, 1), 0),
1599 build_unary_op (code, TREE_OPERAND (t, 2), 0));
1602 /* Given the TYPE of an anonymous union field inside T, return the
1603 FIELD_DECL for the field. If not found return NULL_TREE. Because
1604 anonymous unions can nest, we must also search all anonymous unions
1605 that are directly reachable. */
1607 tree
1608 lookup_anon_field (tree t, tree type)
1610 tree field;
1612 for (field = TYPE_FIELDS (t); field; field = TREE_CHAIN (field))
1614 if (TREE_STATIC (field))
1615 continue;
1616 if (TREE_CODE (field) != FIELD_DECL || DECL_ARTIFICIAL (field))
1617 continue;
1619 /* If we find it directly, return the field. */
1620 if (DECL_NAME (field) == NULL_TREE
1621 && type == TYPE_MAIN_VARIANT (TREE_TYPE (field)))
1623 return field;
1626 /* Otherwise, it could be nested, search harder. */
1627 if (DECL_NAME (field) == NULL_TREE
1628 && ANON_AGGR_TYPE_P (TREE_TYPE (field)))
1630 tree subfield = lookup_anon_field (TREE_TYPE (field), type);
1631 if (subfield)
1632 return subfield;
1635 return NULL_TREE;
1638 /* Build an expression representing OBJECT.MEMBER. OBJECT is an
1639 expression; MEMBER is a DECL or baselink. If ACCESS_PATH is
1640 non-NULL, it indicates the path to the base used to name MEMBER.
1641 If PRESERVE_REFERENCE is true, the expression returned will have
1642 REFERENCE_TYPE if the MEMBER does. Otherwise, the expression
1643 returned will have the type referred to by the reference.
1645 This function does not perform access control; that is either done
1646 earlier by the parser when the name of MEMBER is resolved to MEMBER
1647 itself, or later when overload resolution selects one of the
1648 functions indicated by MEMBER. */
1650 tree
1651 build_class_member_access_expr (tree object, tree member,
1652 tree access_path, bool preserve_reference)
1654 tree object_type;
1655 tree member_scope;
1656 tree result = NULL_TREE;
1658 if (object == error_mark_node || member == error_mark_node)
1659 return error_mark_node;
1661 gcc_assert (DECL_P (member) || BASELINK_P (member));
1663 /* [expr.ref]
1665 The type of the first expression shall be "class object" (of a
1666 complete type). */
1667 object_type = TREE_TYPE (object);
1668 if (!currently_open_class (object_type)
1669 && !complete_type_or_else (object_type, object))
1670 return error_mark_node;
1671 if (!CLASS_TYPE_P (object_type))
1673 error ("request for member %qD in %qE, which is of non-class type %qT",
1674 member, object, object_type);
1675 return error_mark_node;
1678 /* The standard does not seem to actually say that MEMBER must be a
1679 member of OBJECT_TYPE. However, that is clearly what is
1680 intended. */
1681 if (DECL_P (member))
1683 member_scope = DECL_CLASS_CONTEXT (member);
1684 mark_used (member);
1685 if (TREE_DEPRECATED (member))
1686 warn_deprecated_use (member);
1688 else
1689 member_scope = BINFO_TYPE (BASELINK_BINFO (member));
1690 /* If MEMBER is from an anonymous aggregate, MEMBER_SCOPE will
1691 presently be the anonymous union. Go outwards until we find a
1692 type related to OBJECT_TYPE. */
1693 while (ANON_AGGR_TYPE_P (member_scope)
1694 && !same_type_ignoring_top_level_qualifiers_p (member_scope,
1695 object_type))
1696 member_scope = TYPE_CONTEXT (member_scope);
1697 if (!member_scope || !DERIVED_FROM_P (member_scope, object_type))
1699 if (TREE_CODE (member) == FIELD_DECL)
1700 error ("invalid use of nonstatic data member %qE", member);
1701 else
1702 error ("%qD is not a member of %qT", member, object_type);
1703 return error_mark_node;
1706 /* Transform `(a, b).x' into `(*(a, &b)).x', `(a ? b : c).x' into
1707 `(*(a ? &b : &c)).x', and so on. A COND_EXPR is only an lvalue
1708 in the frontend; only _DECLs and _REFs are lvalues in the backend. */
1710 tree temp = unary_complex_lvalue (ADDR_EXPR, object);
1711 if (temp)
1712 object = build_indirect_ref (temp, NULL);
1715 /* In [expr.ref], there is an explicit list of the valid choices for
1716 MEMBER. We check for each of those cases here. */
1717 if (TREE_CODE (member) == VAR_DECL)
1719 /* A static data member. */
1720 result = member;
1721 /* If OBJECT has side-effects, they are supposed to occur. */
1722 if (TREE_SIDE_EFFECTS (object))
1723 result = build2 (COMPOUND_EXPR, TREE_TYPE (result), object, result);
1725 else if (TREE_CODE (member) == FIELD_DECL)
1727 /* A non-static data member. */
1728 bool null_object_p;
1729 int type_quals;
1730 tree member_type;
1732 null_object_p = (TREE_CODE (object) == INDIRECT_REF
1733 && integer_zerop (TREE_OPERAND (object, 0)));
1735 /* Convert OBJECT to the type of MEMBER. */
1736 if (!same_type_p (TYPE_MAIN_VARIANT (object_type),
1737 TYPE_MAIN_VARIANT (member_scope)))
1739 tree binfo;
1740 base_kind kind;
1742 binfo = lookup_base (access_path ? access_path : object_type,
1743 member_scope, ba_unique, &kind);
1744 if (binfo == error_mark_node)
1745 return error_mark_node;
1747 /* It is invalid to try to get to a virtual base of a
1748 NULL object. The most common cause is invalid use of
1749 offsetof macro. */
1750 if (null_object_p && kind == bk_via_virtual)
1752 error ("invalid access to non-static data member %qD of "
1753 "NULL object",
1754 member);
1755 error ("(perhaps the %<offsetof%> macro was used incorrectly)");
1756 return error_mark_node;
1759 /* Convert to the base. */
1760 object = build_base_path (PLUS_EXPR, object, binfo,
1761 /*nonnull=*/1);
1762 /* If we found the base successfully then we should be able
1763 to convert to it successfully. */
1764 gcc_assert (object != error_mark_node);
1767 /* Complain about other invalid uses of offsetof, even though they will
1768 give the right answer. Note that we complain whether or not they
1769 actually used the offsetof macro, since there's no way to know at this
1770 point. So we just give a warning, instead of a pedwarn. */
1771 /* Do not produce this warning for base class field references, because
1772 we know for a fact that didn't come from offsetof. This does occur
1773 in various testsuite cases where a null object is passed where a
1774 vtable access is required. */
1775 if (null_object_p && warn_invalid_offsetof
1776 && CLASSTYPE_NON_POD_P (object_type)
1777 && !DECL_FIELD_IS_BASE (member)
1778 && !skip_evaluation)
1780 warning (0, "invalid access to non-static data member %qD of NULL object",
1781 member);
1782 warning (0, "(perhaps the %<offsetof%> macro was used incorrectly)");
1785 /* If MEMBER is from an anonymous aggregate, we have converted
1786 OBJECT so that it refers to the class containing the
1787 anonymous union. Generate a reference to the anonymous union
1788 itself, and recur to find MEMBER. */
1789 if (ANON_AGGR_TYPE_P (DECL_CONTEXT (member))
1790 /* When this code is called from build_field_call, the
1791 object already has the type of the anonymous union.
1792 That is because the COMPONENT_REF was already
1793 constructed, and was then disassembled before calling
1794 build_field_call. After the function-call code is
1795 cleaned up, this waste can be eliminated. */
1796 && (!same_type_ignoring_top_level_qualifiers_p
1797 (TREE_TYPE (object), DECL_CONTEXT (member))))
1799 tree anonymous_union;
1801 anonymous_union = lookup_anon_field (TREE_TYPE (object),
1802 DECL_CONTEXT (member));
1803 object = build_class_member_access_expr (object,
1804 anonymous_union,
1805 /*access_path=*/NULL_TREE,
1806 preserve_reference);
1809 /* Compute the type of the field, as described in [expr.ref]. */
1810 type_quals = TYPE_UNQUALIFIED;
1811 member_type = TREE_TYPE (member);
1812 if (TREE_CODE (member_type) != REFERENCE_TYPE)
1814 type_quals = (cp_type_quals (member_type)
1815 | cp_type_quals (object_type));
1817 /* A field is const (volatile) if the enclosing object, or the
1818 field itself, is const (volatile). But, a mutable field is
1819 not const, even within a const object. */
1820 if (DECL_MUTABLE_P (member))
1821 type_quals &= ~TYPE_QUAL_CONST;
1822 member_type = cp_build_qualified_type (member_type, type_quals);
1825 result = build3 (COMPONENT_REF, member_type, object, member,
1826 NULL_TREE);
1827 result = fold_if_not_in_template (result);
1829 /* Mark the expression const or volatile, as appropriate. Even
1830 though we've dealt with the type above, we still have to mark the
1831 expression itself. */
1832 if (type_quals & TYPE_QUAL_CONST)
1833 TREE_READONLY (result) = 1;
1834 if (type_quals & TYPE_QUAL_VOLATILE)
1835 TREE_THIS_VOLATILE (result) = 1;
1837 else if (BASELINK_P (member))
1839 /* The member is a (possibly overloaded) member function. */
1840 tree functions;
1841 tree type;
1843 /* If the MEMBER is exactly one static member function, then we
1844 know the type of the expression. Otherwise, we must wait
1845 until overload resolution has been performed. */
1846 functions = BASELINK_FUNCTIONS (member);
1847 if (TREE_CODE (functions) == FUNCTION_DECL
1848 && DECL_STATIC_FUNCTION_P (functions))
1849 type = TREE_TYPE (functions);
1850 else
1851 type = unknown_type_node;
1852 /* Note that we do not convert OBJECT to the BASELINK_BINFO
1853 base. That will happen when the function is called. */
1854 result = build3 (COMPONENT_REF, type, object, member, NULL_TREE);
1856 else if (TREE_CODE (member) == CONST_DECL)
1858 /* The member is an enumerator. */
1859 result = member;
1860 /* If OBJECT has side-effects, they are supposed to occur. */
1861 if (TREE_SIDE_EFFECTS (object))
1862 result = build2 (COMPOUND_EXPR, TREE_TYPE (result),
1863 object, result);
1865 else
1867 error ("invalid use of %qD", member);
1868 return error_mark_node;
1871 if (!preserve_reference)
1872 /* [expr.ref]
1874 If E2 is declared to have type "reference to T", then ... the
1875 type of E1.E2 is T. */
1876 result = convert_from_reference (result);
1878 return result;
1881 /* Return the destructor denoted by OBJECT.SCOPE::~DTOR_NAME, or, if
1882 SCOPE is NULL, by OBJECT.~DTOR_NAME. */
1884 static tree
1885 lookup_destructor (tree object, tree scope, tree dtor_name)
1887 tree object_type = TREE_TYPE (object);
1888 tree dtor_type = TREE_OPERAND (dtor_name, 0);
1889 tree expr;
1891 if (scope && !check_dtor_name (scope, dtor_type))
1893 error ("qualified type %qT does not match destructor name ~%qT",
1894 scope, dtor_type);
1895 return error_mark_node;
1897 if (!DERIVED_FROM_P (dtor_type, TYPE_MAIN_VARIANT (object_type)))
1899 error ("the type being destroyed is %qT, but the destructor refers to %qT",
1900 TYPE_MAIN_VARIANT (object_type), dtor_type);
1901 return error_mark_node;
1903 expr = lookup_member (dtor_type, complete_dtor_identifier,
1904 /*protect=*/1, /*want_type=*/false);
1905 expr = (adjust_result_of_qualified_name_lookup
1906 (expr, dtor_type, object_type));
1907 return expr;
1910 /* An expression of the form "A::template B" has been resolved to
1911 DECL. Issue a diagnostic if B is not a template or template
1912 specialization. */
1914 void
1915 check_template_keyword (tree decl)
1917 /* The standard says:
1919 [temp.names]
1921 If a name prefixed by the keyword template is not a member
1922 template, the program is ill-formed.
1924 DR 228 removed the restriction that the template be a member
1925 template.
1927 DR 96, if accepted would add the further restriction that explicit
1928 template arguments must be provided if the template keyword is
1929 used, but, as of 2005-10-16, that DR is still in "drafting". If
1930 this DR is accepted, then the semantic checks here can be
1931 simplified, as the entity named must in fact be a template
1932 specialization, rather than, as at present, a set of overloaded
1933 functions containing at least one template function. */
1934 if (TREE_CODE (decl) != TEMPLATE_DECL
1935 && TREE_CODE (decl) != TEMPLATE_ID_EXPR)
1937 if (!is_overloaded_fn (decl))
1938 pedwarn ("%qD is not a template", decl);
1939 else
1941 tree fns;
1942 fns = decl;
1943 if (BASELINK_P (fns))
1944 fns = BASELINK_FUNCTIONS (fns);
1945 while (fns)
1947 tree fn = OVL_CURRENT (fns);
1948 if (TREE_CODE (fn) == TEMPLATE_DECL
1949 || TREE_CODE (fn) == TEMPLATE_ID_EXPR)
1950 break;
1951 if (TREE_CODE (fn) == FUNCTION_DECL
1952 && DECL_USE_TEMPLATE (fn)
1953 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (fn)))
1954 break;
1955 fns = OVL_NEXT (fns);
1957 if (!fns)
1958 pedwarn ("%qD is not a template", decl);
1963 /* This function is called by the parser to process a class member
1964 access expression of the form OBJECT.NAME. NAME is a node used by
1965 the parser to represent a name; it is not yet a DECL. It may,
1966 however, be a BASELINK where the BASELINK_FUNCTIONS is a
1967 TEMPLATE_ID_EXPR. Templates must be looked up by the parser, and
1968 there is no reason to do the lookup twice, so the parser keeps the
1969 BASELINK. TEMPLATE_P is true iff NAME was explicitly declared to
1970 be a template via the use of the "A::template B" syntax. */
1972 tree
1973 finish_class_member_access_expr (tree object, tree name, bool template_p)
1975 tree expr;
1976 tree object_type;
1977 tree member;
1978 tree access_path = NULL_TREE;
1979 tree orig_object = object;
1980 tree orig_name = name;
1982 if (object == error_mark_node || name == error_mark_node)
1983 return error_mark_node;
1985 /* If OBJECT is an ObjC class instance, we must obey ObjC access rules. */
1986 if (!objc_is_public (object, name))
1987 return error_mark_node;
1989 object_type = TREE_TYPE (object);
1991 if (processing_template_decl)
1993 if (/* If OBJECT_TYPE is dependent, so is OBJECT.NAME. */
1994 dependent_type_p (object_type)
1995 /* If NAME is just an IDENTIFIER_NODE, then the expression
1996 is dependent. */
1997 || TREE_CODE (object) == IDENTIFIER_NODE
1998 /* If NAME is "f<args>", where either 'f' or 'args' is
1999 dependent, then the expression is dependent. */
2000 || (TREE_CODE (name) == TEMPLATE_ID_EXPR
2001 && dependent_template_id_p (TREE_OPERAND (name, 0),
2002 TREE_OPERAND (name, 1)))
2003 /* If NAME is "T::X" where "T" is dependent, then the
2004 expression is dependent. */
2005 || (TREE_CODE (name) == SCOPE_REF
2006 && TYPE_P (TREE_OPERAND (name, 0))
2007 && dependent_type_p (TREE_OPERAND (name, 0))))
2008 return build_min_nt (COMPONENT_REF, object, name, NULL_TREE);
2009 object = build_non_dependent_expr (object);
2012 /* [expr.ref]
2014 The type of the first expression shall be "class object" (of a
2015 complete type). */
2016 if (!currently_open_class (object_type)
2017 && !complete_type_or_else (object_type, object))
2018 return error_mark_node;
2019 if (!CLASS_TYPE_P (object_type))
2021 error ("request for member %qD in %qE, which is of non-class type %qT",
2022 name, object, object_type);
2023 return error_mark_node;
2026 if (BASELINK_P (name))
2027 /* A member function that has already been looked up. */
2028 member = name;
2029 else
2031 bool is_template_id = false;
2032 tree template_args = NULL_TREE;
2033 tree scope;
2035 if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
2037 is_template_id = true;
2038 template_args = TREE_OPERAND (name, 1);
2039 name = TREE_OPERAND (name, 0);
2041 if (TREE_CODE (name) == OVERLOAD)
2042 name = DECL_NAME (get_first_fn (name));
2043 else if (DECL_P (name))
2044 name = DECL_NAME (name);
2047 if (TREE_CODE (name) == SCOPE_REF)
2049 /* A qualified name. The qualifying class or namespace `S' has
2050 already been looked up; it is either a TYPE or a
2051 NAMESPACE_DECL. The member name is either an IDENTIFIER_NODE
2052 or a BIT_NOT_EXPR. */
2053 scope = TREE_OPERAND (name, 0);
2054 name = TREE_OPERAND (name, 1);
2055 gcc_assert (CLASS_TYPE_P (scope)
2056 || TREE_CODE (scope) == NAMESPACE_DECL);
2057 gcc_assert (TREE_CODE (name) == IDENTIFIER_NODE
2058 || TREE_CODE (name) == BIT_NOT_EXPR);
2060 /* If SCOPE is a namespace, then the qualified name does not
2061 name a member of OBJECT_TYPE. */
2062 if (TREE_CODE (scope) == NAMESPACE_DECL)
2064 error ("%<%D::%D%> is not a member of %qT",
2065 scope, name, object_type);
2066 return error_mark_node;
2069 /* Find the base of OBJECT_TYPE corresponding to SCOPE. */
2070 access_path = lookup_base (object_type, scope, ba_check, NULL);
2071 if (access_path == error_mark_node)
2072 return error_mark_node;
2073 if (!access_path)
2075 error ("%qT is not a base of %qT", scope, object_type);
2076 return error_mark_node;
2079 else
2081 scope = NULL_TREE;
2082 access_path = object_type;
2085 if (TREE_CODE (name) == BIT_NOT_EXPR)
2086 member = lookup_destructor (object, scope, name);
2087 else
2089 /* Look up the member. */
2090 member = lookup_member (access_path, name, /*protect=*/1,
2091 /*want_type=*/false);
2092 if (member == NULL_TREE)
2094 error ("%qD has no member named %qE", object_type, name);
2095 return error_mark_node;
2097 if (member == error_mark_node)
2098 return error_mark_node;
2101 if (is_template_id)
2103 tree template = member;
2105 if (BASELINK_P (template))
2106 template = lookup_template_function (template, template_args);
2107 else
2109 error ("%qD is not a member template function", name);
2110 return error_mark_node;
2115 if (TREE_DEPRECATED (member))
2116 warn_deprecated_use (member);
2118 if (template_p)
2119 check_template_keyword (member);
2121 expr = build_class_member_access_expr (object, member, access_path,
2122 /*preserve_reference=*/false);
2123 if (processing_template_decl && expr != error_mark_node)
2125 if (BASELINK_P (member))
2127 if (TREE_CODE (orig_name) == SCOPE_REF)
2128 BASELINK_QUALIFIED_P (member) = 1;
2129 orig_name = member;
2131 return build_min_non_dep (COMPONENT_REF, expr,
2132 orig_object, orig_name,
2133 NULL_TREE);
2136 return expr;
2139 /* Return an expression for the MEMBER_NAME field in the internal
2140 representation of PTRMEM, a pointer-to-member function. (Each
2141 pointer-to-member function type gets its own RECORD_TYPE so it is
2142 more convenient to access the fields by name than by FIELD_DECL.)
2143 This routine converts the NAME to a FIELD_DECL and then creates the
2144 node for the complete expression. */
2146 tree
2147 build_ptrmemfunc_access_expr (tree ptrmem, tree member_name)
2149 tree ptrmem_type;
2150 tree member;
2151 tree member_type;
2153 /* This code is a stripped down version of
2154 build_class_member_access_expr. It does not work to use that
2155 routine directly because it expects the object to be of class
2156 type. */
2157 ptrmem_type = TREE_TYPE (ptrmem);
2158 gcc_assert (TYPE_PTRMEMFUNC_P (ptrmem_type));
2159 member = lookup_member (ptrmem_type, member_name, /*protect=*/0,
2160 /*want_type=*/false);
2161 member_type = cp_build_qualified_type (TREE_TYPE (member),
2162 cp_type_quals (ptrmem_type));
2163 return fold_build3 (COMPONENT_REF, member_type,
2164 ptrmem, member, NULL_TREE);
2167 /* Given an expression PTR for a pointer, return an expression
2168 for the value pointed to.
2169 ERRORSTRING is the name of the operator to appear in error messages.
2171 This function may need to overload OPERATOR_FNNAME.
2172 Must also handle REFERENCE_TYPEs for C++. */
2174 tree
2175 build_x_indirect_ref (tree expr, const char *errorstring)
2177 tree orig_expr = expr;
2178 tree rval;
2180 if (processing_template_decl)
2182 if (type_dependent_expression_p (expr))
2183 return build_min_nt (INDIRECT_REF, expr);
2184 expr = build_non_dependent_expr (expr);
2187 rval = build_new_op (INDIRECT_REF, LOOKUP_NORMAL, expr, NULL_TREE,
2188 NULL_TREE, /*overloaded_p=*/NULL);
2189 if (!rval)
2190 rval = build_indirect_ref (expr, errorstring);
2192 if (processing_template_decl && rval != error_mark_node)
2193 return build_min_non_dep (INDIRECT_REF, rval, orig_expr);
2194 else
2195 return rval;
2198 tree
2199 build_indirect_ref (tree ptr, const char *errorstring)
2201 tree pointer, type;
2203 if (ptr == error_mark_node)
2204 return error_mark_node;
2206 if (ptr == current_class_ptr)
2207 return current_class_ref;
2209 pointer = (TREE_CODE (TREE_TYPE (ptr)) == REFERENCE_TYPE
2210 ? ptr : decay_conversion (ptr));
2211 type = TREE_TYPE (pointer);
2213 if (POINTER_TYPE_P (type))
2215 /* [expr.unary.op]
2217 If the type of the expression is "pointer to T," the type
2218 of the result is "T."
2220 We must use the canonical variant because certain parts of
2221 the back end, like fold, do pointer comparisons between
2222 types. */
2223 tree t = canonical_type_variant (TREE_TYPE (type));
2225 if (VOID_TYPE_P (t))
2227 /* A pointer to incomplete type (other than cv void) can be
2228 dereferenced [expr.unary.op]/1 */
2229 error ("%qT is not a pointer-to-object type", type);
2230 return error_mark_node;
2232 else if (TREE_CODE (pointer) == ADDR_EXPR
2233 && same_type_p (t, TREE_TYPE (TREE_OPERAND (pointer, 0))))
2234 /* The POINTER was something like `&x'. We simplify `*&x' to
2235 `x'. */
2236 return TREE_OPERAND (pointer, 0);
2237 else
2239 tree ref = build1 (INDIRECT_REF, t, pointer);
2241 /* We *must* set TREE_READONLY when dereferencing a pointer to const,
2242 so that we get the proper error message if the result is used
2243 to assign to. Also, &* is supposed to be a no-op. */
2244 TREE_READONLY (ref) = CP_TYPE_CONST_P (t);
2245 TREE_THIS_VOLATILE (ref) = CP_TYPE_VOLATILE_P (t);
2246 TREE_SIDE_EFFECTS (ref)
2247 = (TREE_THIS_VOLATILE (ref) || TREE_SIDE_EFFECTS (pointer));
2248 return ref;
2251 /* `pointer' won't be an error_mark_node if we were given a
2252 pointer to member, so it's cool to check for this here. */
2253 else if (TYPE_PTR_TO_MEMBER_P (type))
2254 error ("invalid use of %qs on pointer to member", errorstring);
2255 else if (pointer != error_mark_node)
2257 if (errorstring)
2258 error ("invalid type argument of %qs", errorstring);
2259 else
2260 error ("invalid type argument");
2262 return error_mark_node;
2265 /* This handles expressions of the form "a[i]", which denotes
2266 an array reference.
2268 This is logically equivalent in C to *(a+i), but we may do it differently.
2269 If A is a variable or a member, we generate a primitive ARRAY_REF.
2270 This avoids forcing the array out of registers, and can work on
2271 arrays that are not lvalues (for example, members of structures returned
2272 by functions).
2274 If INDEX is of some user-defined type, it must be converted to
2275 integer type. Otherwise, to make a compatible PLUS_EXPR, it
2276 will inherit the type of the array, which will be some pointer type. */
2278 tree
2279 build_array_ref (tree array, tree idx)
2281 if (idx == 0)
2283 error ("subscript missing in array reference");
2284 return error_mark_node;
2287 if (TREE_TYPE (array) == error_mark_node
2288 || TREE_TYPE (idx) == error_mark_node)
2289 return error_mark_node;
2291 /* If ARRAY is a COMPOUND_EXPR or COND_EXPR, move our reference
2292 inside it. */
2293 switch (TREE_CODE (array))
2295 case COMPOUND_EXPR:
2297 tree value = build_array_ref (TREE_OPERAND (array, 1), idx);
2298 return build2 (COMPOUND_EXPR, TREE_TYPE (value),
2299 TREE_OPERAND (array, 0), value);
2302 case COND_EXPR:
2303 return build_conditional_expr
2304 (TREE_OPERAND (array, 0),
2305 build_array_ref (TREE_OPERAND (array, 1), idx),
2306 build_array_ref (TREE_OPERAND (array, 2), idx));
2308 default:
2309 break;
2312 if (TREE_CODE (TREE_TYPE (array)) == ARRAY_TYPE)
2314 tree rval, type;
2316 warn_array_subscript_with_type_char (idx);
2318 if (!INTEGRAL_OR_ENUMERATION_TYPE_P (TREE_TYPE (idx)))
2320 error ("array subscript is not an integer");
2321 return error_mark_node;
2324 /* Apply integral promotions *after* noticing character types.
2325 (It is unclear why we do these promotions -- the standard
2326 does not say that we should. In fact, the natural thing would
2327 seem to be to convert IDX to ptrdiff_t; we're performing
2328 pointer arithmetic.) */
2329 idx = perform_integral_promotions (idx);
2331 /* An array that is indexed by a non-constant
2332 cannot be stored in a register; we must be able to do
2333 address arithmetic on its address.
2334 Likewise an array of elements of variable size. */
2335 if (TREE_CODE (idx) != INTEGER_CST
2336 || (COMPLETE_TYPE_P (TREE_TYPE (TREE_TYPE (array)))
2337 && (TREE_CODE (TYPE_SIZE (TREE_TYPE (TREE_TYPE (array))))
2338 != INTEGER_CST)))
2340 if (!cxx_mark_addressable (array))
2341 return error_mark_node;
2344 /* An array that is indexed by a constant value which is not within
2345 the array bounds cannot be stored in a register either; because we
2346 would get a crash in store_bit_field/extract_bit_field when trying
2347 to access a non-existent part of the register. */
2348 if (TREE_CODE (idx) == INTEGER_CST
2349 && TYPE_DOMAIN (TREE_TYPE (array))
2350 && ! int_fits_type_p (idx, TYPE_DOMAIN (TREE_TYPE (array))))
2352 if (!cxx_mark_addressable (array))
2353 return error_mark_node;
2356 if (pedantic && !lvalue_p (array))
2357 pedwarn ("ISO C++ forbids subscripting non-lvalue array");
2359 /* Note in C++ it is valid to subscript a `register' array, since
2360 it is valid to take the address of something with that
2361 storage specification. */
2362 if (extra_warnings)
2364 tree foo = array;
2365 while (TREE_CODE (foo) == COMPONENT_REF)
2366 foo = TREE_OPERAND (foo, 0);
2367 if (TREE_CODE (foo) == VAR_DECL && DECL_REGISTER (foo))
2368 warning (OPT_Wextra, "subscripting array declared %<register%>");
2371 type = TREE_TYPE (TREE_TYPE (array));
2372 rval = build4 (ARRAY_REF, type, array, idx, NULL_TREE, NULL_TREE);
2373 /* Array ref is const/volatile if the array elements are
2374 or if the array is.. */
2375 TREE_READONLY (rval)
2376 |= (CP_TYPE_CONST_P (type) | TREE_READONLY (array));
2377 TREE_SIDE_EFFECTS (rval)
2378 |= (CP_TYPE_VOLATILE_P (type) | TREE_SIDE_EFFECTS (array));
2379 TREE_THIS_VOLATILE (rval)
2380 |= (CP_TYPE_VOLATILE_P (type) | TREE_THIS_VOLATILE (array));
2381 return require_complete_type (fold_if_not_in_template (rval));
2385 tree ar = default_conversion (array);
2386 tree ind = default_conversion (idx);
2388 /* Put the integer in IND to simplify error checking. */
2389 if (TREE_CODE (TREE_TYPE (ar)) == INTEGER_TYPE)
2391 tree temp = ar;
2392 ar = ind;
2393 ind = temp;
2396 if (ar == error_mark_node)
2397 return ar;
2399 if (TREE_CODE (TREE_TYPE (ar)) != POINTER_TYPE)
2401 error ("subscripted value is neither array nor pointer");
2402 return error_mark_node;
2404 if (TREE_CODE (TREE_TYPE (ind)) != INTEGER_TYPE)
2406 error ("array subscript is not an integer");
2407 return error_mark_node;
2410 return build_indirect_ref (cp_build_binary_op (PLUS_EXPR, ar, ind),
2411 "array indexing");
2415 /* Resolve a pointer to member function. INSTANCE is the object
2416 instance to use, if the member points to a virtual member.
2418 This used to avoid checking for virtual functions if basetype
2419 has no virtual functions, according to an earlier ANSI draft.
2420 With the final ISO C++ rules, such an optimization is
2421 incorrect: A pointer to a derived member can be static_cast
2422 to pointer-to-base-member, as long as the dynamic object
2423 later has the right member. */
2425 tree
2426 get_member_function_from_ptrfunc (tree *instance_ptrptr, tree function)
2428 if (TREE_CODE (function) == OFFSET_REF)
2429 function = TREE_OPERAND (function, 1);
2431 if (TYPE_PTRMEMFUNC_P (TREE_TYPE (function)))
2433 tree idx, delta, e1, e2, e3, vtbl, basetype;
2434 tree fntype = TYPE_PTRMEMFUNC_FN_TYPE (TREE_TYPE (function));
2436 tree instance_ptr = *instance_ptrptr;
2437 tree instance_save_expr = 0;
2438 if (instance_ptr == error_mark_node)
2440 if (TREE_CODE (function) == PTRMEM_CST)
2442 /* Extracting the function address from a pmf is only
2443 allowed with -Wno-pmf-conversions. It only works for
2444 pmf constants. */
2445 e1 = build_addr_func (PTRMEM_CST_MEMBER (function));
2446 e1 = convert (fntype, e1);
2447 return e1;
2449 else
2451 error ("object missing in use of %qE", function);
2452 return error_mark_node;
2456 if (TREE_SIDE_EFFECTS (instance_ptr))
2457 instance_ptr = instance_save_expr = save_expr (instance_ptr);
2459 if (TREE_SIDE_EFFECTS (function))
2460 function = save_expr (function);
2462 /* Start by extracting all the information from the PMF itself. */
2463 e3 = pfn_from_ptrmemfunc (function);
2464 delta = build_ptrmemfunc_access_expr (function, delta_identifier);
2465 idx = build1 (NOP_EXPR, vtable_index_type, e3);
2466 switch (TARGET_PTRMEMFUNC_VBIT_LOCATION)
2468 case ptrmemfunc_vbit_in_pfn:
2469 e1 = cp_build_binary_op (BIT_AND_EXPR, idx, integer_one_node);
2470 idx = cp_build_binary_op (MINUS_EXPR, idx, integer_one_node);
2471 break;
2473 case ptrmemfunc_vbit_in_delta:
2474 e1 = cp_build_binary_op (BIT_AND_EXPR, delta, integer_one_node);
2475 delta = cp_build_binary_op (RSHIFT_EXPR, delta, integer_one_node);
2476 break;
2478 default:
2479 gcc_unreachable ();
2482 /* Convert down to the right base before using the instance. A
2483 special case is that in a pointer to member of class C, C may
2484 be incomplete. In that case, the function will of course be
2485 a member of C, and no conversion is required. In fact,
2486 lookup_base will fail in that case, because incomplete
2487 classes do not have BINFOs. */
2488 basetype = TYPE_METHOD_BASETYPE (TREE_TYPE (fntype));
2489 if (!same_type_ignoring_top_level_qualifiers_p
2490 (basetype, TREE_TYPE (TREE_TYPE (instance_ptr))))
2492 basetype = lookup_base (TREE_TYPE (TREE_TYPE (instance_ptr)),
2493 basetype, ba_check, NULL);
2494 instance_ptr = build_base_path (PLUS_EXPR, instance_ptr, basetype,
2496 if (instance_ptr == error_mark_node)
2497 return error_mark_node;
2499 /* ...and then the delta in the PMF. */
2500 instance_ptr = build2 (PLUS_EXPR, TREE_TYPE (instance_ptr),
2501 instance_ptr, delta);
2503 /* Hand back the adjusted 'this' argument to our caller. */
2504 *instance_ptrptr = instance_ptr;
2506 /* Next extract the vtable pointer from the object. */
2507 vtbl = build1 (NOP_EXPR, build_pointer_type (vtbl_ptr_type_node),
2508 instance_ptr);
2509 vtbl = build_indirect_ref (vtbl, NULL);
2511 /* Finally, extract the function pointer from the vtable. */
2512 e2 = fold_build2 (PLUS_EXPR, TREE_TYPE (vtbl), vtbl, idx);
2513 e2 = build_indirect_ref (e2, NULL);
2514 TREE_CONSTANT (e2) = 1;
2515 TREE_INVARIANT (e2) = 1;
2517 /* When using function descriptors, the address of the
2518 vtable entry is treated as a function pointer. */
2519 if (TARGET_VTABLE_USES_DESCRIPTORS)
2520 e2 = build1 (NOP_EXPR, TREE_TYPE (e2),
2521 build_unary_op (ADDR_EXPR, e2, /*noconvert=*/1));
2523 TREE_TYPE (e2) = TREE_TYPE (e3);
2524 e1 = build_conditional_expr (e1, e2, e3);
2526 /* Make sure this doesn't get evaluated first inside one of the
2527 branches of the COND_EXPR. */
2528 if (instance_save_expr)
2529 e1 = build2 (COMPOUND_EXPR, TREE_TYPE (e1),
2530 instance_save_expr, e1);
2532 function = e1;
2534 return function;
2537 tree
2538 build_function_call (tree function, tree params)
2540 tree fntype, fndecl;
2541 tree coerced_params;
2542 tree name = NULL_TREE;
2543 int is_method;
2544 tree original = function;
2546 /* For Objective-C, convert any calls via a cast to OBJC_TYPE_REF
2547 expressions, like those used for ObjC messenger dispatches. */
2548 function = objc_rewrite_function_call (function, params);
2550 /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
2551 Strip such NOP_EXPRs, since FUNCTION is used in non-lvalue context. */
2552 if (TREE_CODE (function) == NOP_EXPR
2553 && TREE_TYPE (function) == TREE_TYPE (TREE_OPERAND (function, 0)))
2554 function = TREE_OPERAND (function, 0);
2556 if (TREE_CODE (function) == FUNCTION_DECL)
2558 name = DECL_NAME (function);
2560 mark_used (function);
2561 fndecl = function;
2563 /* Convert anything with function type to a pointer-to-function. */
2564 if (pedantic && DECL_MAIN_P (function))
2565 pedwarn ("ISO C++ forbids calling %<::main%> from within program");
2567 /* Differs from default_conversion by not setting TREE_ADDRESSABLE
2568 (because calling an inline function does not mean the function
2569 needs to be separately compiled). */
2571 if (DECL_INLINE (function))
2572 function = inline_conversion (function);
2573 else
2574 function = build_addr_func (function);
2576 else
2578 fndecl = NULL_TREE;
2580 function = build_addr_func (function);
2583 if (function == error_mark_node)
2584 return error_mark_node;
2586 fntype = TREE_TYPE (function);
2588 if (TYPE_PTRMEMFUNC_P (fntype))
2590 error ("must use %<.*%> or %<->*%> to call pointer-to-member "
2591 "function in %<%E (...)%>",
2592 original);
2593 return error_mark_node;
2596 is_method = (TREE_CODE (fntype) == POINTER_TYPE
2597 && TREE_CODE (TREE_TYPE (fntype)) == METHOD_TYPE);
2599 if (!((TREE_CODE (fntype) == POINTER_TYPE
2600 && TREE_CODE (TREE_TYPE (fntype)) == FUNCTION_TYPE)
2601 || is_method
2602 || TREE_CODE (function) == TEMPLATE_ID_EXPR))
2604 error ("%qE cannot be used as a function", original);
2605 return error_mark_node;
2608 /* fntype now gets the type of function pointed to. */
2609 fntype = TREE_TYPE (fntype);
2611 /* Convert the parameters to the types declared in the
2612 function prototype, or apply default promotions. */
2614 coerced_params = convert_arguments (TYPE_ARG_TYPES (fntype),
2615 params, fndecl, LOOKUP_NORMAL);
2616 if (coerced_params == error_mark_node)
2617 return error_mark_node;
2619 /* Check for errors in format strings and inappropriately
2620 null parameters. */
2622 check_function_arguments (TYPE_ATTRIBUTES (fntype), coerced_params,
2623 TYPE_ARG_TYPES (fntype));
2625 return build_cxx_call (function, coerced_params);
2628 /* Convert the actual parameter expressions in the list VALUES
2629 to the types in the list TYPELIST.
2630 If parmdecls is exhausted, or when an element has NULL as its type,
2631 perform the default conversions.
2633 NAME is an IDENTIFIER_NODE or 0. It is used only for error messages.
2635 This is also where warnings about wrong number of args are generated.
2637 Return a list of expressions for the parameters as converted.
2639 Both VALUES and the returned value are chains of TREE_LIST nodes
2640 with the elements of the list in the TREE_VALUE slots of those nodes.
2642 In C++, unspecified trailing parameters can be filled in with their
2643 default arguments, if such were specified. Do so here. */
2645 static tree
2646 convert_arguments (tree typelist, tree values, tree fndecl, int flags)
2648 tree typetail, valtail;
2649 tree result = NULL_TREE;
2650 const char *called_thing = 0;
2651 int i = 0;
2653 /* Argument passing is always copy-initialization. */
2654 flags |= LOOKUP_ONLYCONVERTING;
2656 if (fndecl)
2658 if (TREE_CODE (TREE_TYPE (fndecl)) == METHOD_TYPE)
2660 if (DECL_NAME (fndecl) == NULL_TREE
2661 || IDENTIFIER_HAS_TYPE_VALUE (DECL_NAME (fndecl)))
2662 called_thing = "constructor";
2663 else
2664 called_thing = "member function";
2666 else
2667 called_thing = "function";
2670 for (valtail = values, typetail = typelist;
2671 valtail;
2672 valtail = TREE_CHAIN (valtail), i++)
2674 tree type = typetail ? TREE_VALUE (typetail) : 0;
2675 tree val = TREE_VALUE (valtail);
2677 if (val == error_mark_node)
2678 return error_mark_node;
2680 if (type == void_type_node)
2682 if (fndecl)
2684 error ("too many arguments to %s %q+#D", called_thing, fndecl);
2685 error ("at this point in file");
2687 else
2688 error ("too many arguments to function");
2689 /* In case anybody wants to know if this argument
2690 list is valid. */
2691 if (result)
2692 TREE_TYPE (tree_last (result)) = error_mark_node;
2693 break;
2696 /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
2697 Strip such NOP_EXPRs, since VAL is used in non-lvalue context. */
2698 if (TREE_CODE (val) == NOP_EXPR
2699 && TREE_TYPE (val) == TREE_TYPE (TREE_OPERAND (val, 0))
2700 && (type == 0 || TREE_CODE (type) != REFERENCE_TYPE))
2701 val = TREE_OPERAND (val, 0);
2703 if (type == 0 || TREE_CODE (type) != REFERENCE_TYPE)
2705 if (TREE_CODE (TREE_TYPE (val)) == ARRAY_TYPE
2706 || TREE_CODE (TREE_TYPE (val)) == FUNCTION_TYPE
2707 || TREE_CODE (TREE_TYPE (val)) == METHOD_TYPE)
2708 val = decay_conversion (val);
2711 if (val == error_mark_node)
2712 return error_mark_node;
2714 if (type != 0)
2716 /* Formal parm type is specified by a function prototype. */
2717 tree parmval;
2719 if (!COMPLETE_TYPE_P (complete_type (type)))
2721 if (fndecl)
2722 error ("parameter %P of %qD has incomplete type %qT",
2723 i, fndecl, type);
2724 else
2725 error ("parameter %P has incomplete type %qT", i, type);
2726 parmval = error_mark_node;
2728 else
2730 parmval = convert_for_initialization
2731 (NULL_TREE, type, val, flags,
2732 "argument passing", fndecl, i);
2733 parmval = convert_for_arg_passing (type, parmval);
2736 if (parmval == error_mark_node)
2737 return error_mark_node;
2739 result = tree_cons (NULL_TREE, parmval, result);
2741 else
2743 if (fndecl && DECL_BUILT_IN (fndecl)
2744 && DECL_FUNCTION_CODE (fndecl) == BUILT_IN_CONSTANT_P)
2745 /* Don't do ellipsis conversion for __built_in_constant_p
2746 as this will result in spurious warnings for non-POD
2747 types. */
2748 val = require_complete_type (val);
2749 else
2750 val = convert_arg_to_ellipsis (val);
2752 result = tree_cons (NULL_TREE, val, result);
2755 if (typetail)
2756 typetail = TREE_CHAIN (typetail);
2759 if (typetail != 0 && typetail != void_list_node)
2761 /* See if there are default arguments that can be used. */
2762 if (TREE_PURPOSE (typetail)
2763 && TREE_CODE (TREE_PURPOSE (typetail)) != DEFAULT_ARG)
2765 for (; typetail != void_list_node; ++i)
2767 tree parmval
2768 = convert_default_arg (TREE_VALUE (typetail),
2769 TREE_PURPOSE (typetail),
2770 fndecl, i);
2772 if (parmval == error_mark_node)
2773 return error_mark_node;
2775 result = tree_cons (0, parmval, result);
2776 typetail = TREE_CHAIN (typetail);
2777 /* ends with `...'. */
2778 if (typetail == NULL_TREE)
2779 break;
2782 else
2784 if (fndecl)
2786 error ("too few arguments to %s %q+#D", called_thing, fndecl);
2787 error ("at this point in file");
2789 else
2790 error ("too few arguments to function");
2791 return error_mark_list;
2795 return nreverse (result);
2798 /* Build a binary-operation expression, after performing default
2799 conversions on the operands. CODE is the kind of expression to build. */
2801 tree
2802 build_x_binary_op (enum tree_code code, tree arg1, tree arg2,
2803 bool *overloaded_p)
2805 tree orig_arg1;
2806 tree orig_arg2;
2807 tree expr;
2809 orig_arg1 = arg1;
2810 orig_arg2 = arg2;
2812 if (processing_template_decl)
2814 if (type_dependent_expression_p (arg1)
2815 || type_dependent_expression_p (arg2))
2816 return build_min_nt (code, arg1, arg2);
2817 arg1 = build_non_dependent_expr (arg1);
2818 arg2 = build_non_dependent_expr (arg2);
2821 if (code == DOTSTAR_EXPR)
2822 expr = build_m_component_ref (arg1, arg2);
2823 else
2824 expr = build_new_op (code, LOOKUP_NORMAL, arg1, arg2, NULL_TREE,
2825 overloaded_p);
2827 if (processing_template_decl && expr != error_mark_node)
2828 return build_min_non_dep (code, expr, orig_arg1, orig_arg2);
2830 return expr;
2833 /* Build a binary-operation expression without default conversions.
2834 CODE is the kind of expression to build.
2835 This function differs from `build' in several ways:
2836 the data type of the result is computed and recorded in it,
2837 warnings are generated if arg data types are invalid,
2838 special handling for addition and subtraction of pointers is known,
2839 and some optimization is done (operations on narrow ints
2840 are done in the narrower type when that gives the same result).
2841 Constant folding is also done before the result is returned.
2843 Note that the operands will never have enumeral types
2844 because either they have just had the default conversions performed
2845 or they have both just been converted to some other type in which
2846 the arithmetic is to be done.
2848 C++: must do special pointer arithmetic when implementing
2849 multiple inheritance, and deal with pointer to member functions. */
2851 tree
2852 build_binary_op (enum tree_code code, tree orig_op0, tree orig_op1,
2853 int convert_p ATTRIBUTE_UNUSED)
2855 tree op0, op1;
2856 enum tree_code code0, code1;
2857 tree type0, type1;
2858 const char *invalid_op_diag;
2860 /* Expression code to give to the expression when it is built.
2861 Normally this is CODE, which is what the caller asked for,
2862 but in some special cases we change it. */
2863 enum tree_code resultcode = code;
2865 /* Data type in which the computation is to be performed.
2866 In the simplest cases this is the common type of the arguments. */
2867 tree result_type = NULL;
2869 /* Nonzero means operands have already been type-converted
2870 in whatever way is necessary.
2871 Zero means they need to be converted to RESULT_TYPE. */
2872 int converted = 0;
2874 /* Nonzero means create the expression with this type, rather than
2875 RESULT_TYPE. */
2876 tree build_type = 0;
2878 /* Nonzero means after finally constructing the expression
2879 convert it to this type. */
2880 tree final_type = 0;
2882 tree result;
2884 /* Nonzero if this is an operation like MIN or MAX which can
2885 safely be computed in short if both args are promoted shorts.
2886 Also implies COMMON.
2887 -1 indicates a bitwise operation; this makes a difference
2888 in the exact conditions for when it is safe to do the operation
2889 in a narrower mode. */
2890 int shorten = 0;
2892 /* Nonzero if this is a comparison operation;
2893 if both args are promoted shorts, compare the original shorts.
2894 Also implies COMMON. */
2895 int short_compare = 0;
2897 /* Nonzero if this is a right-shift operation, which can be computed on the
2898 original short and then promoted if the operand is a promoted short. */
2899 int short_shift = 0;
2901 /* Nonzero means set RESULT_TYPE to the common type of the args. */
2902 int common = 0;
2904 /* True if both operands have arithmetic type. */
2905 bool arithmetic_types_p;
2907 /* Apply default conversions. */
2908 op0 = orig_op0;
2909 op1 = orig_op1;
2911 if (code == TRUTH_AND_EXPR || code == TRUTH_ANDIF_EXPR
2912 || code == TRUTH_OR_EXPR || code == TRUTH_ORIF_EXPR
2913 || code == TRUTH_XOR_EXPR)
2915 if (!really_overloaded_fn (op0))
2916 op0 = decay_conversion (op0);
2917 if (!really_overloaded_fn (op1))
2918 op1 = decay_conversion (op1);
2920 else
2922 if (!really_overloaded_fn (op0))
2923 op0 = default_conversion (op0);
2924 if (!really_overloaded_fn (op1))
2925 op1 = default_conversion (op1);
2928 /* Strip NON_LVALUE_EXPRs, etc., since we aren't using as an lvalue. */
2929 STRIP_TYPE_NOPS (op0);
2930 STRIP_TYPE_NOPS (op1);
2932 /* DTRT if one side is an overloaded function, but complain about it. */
2933 if (type_unknown_p (op0))
2935 tree t = instantiate_type (TREE_TYPE (op1), op0, tf_none);
2936 if (t != error_mark_node)
2938 pedwarn ("assuming cast to type %qT from overloaded function",
2939 TREE_TYPE (t));
2940 op0 = t;
2943 if (type_unknown_p (op1))
2945 tree t = instantiate_type (TREE_TYPE (op0), op1, tf_none);
2946 if (t != error_mark_node)
2948 pedwarn ("assuming cast to type %qT from overloaded function",
2949 TREE_TYPE (t));
2950 op1 = t;
2954 type0 = TREE_TYPE (op0);
2955 type1 = TREE_TYPE (op1);
2957 /* The expression codes of the data types of the arguments tell us
2958 whether the arguments are integers, floating, pointers, etc. */
2959 code0 = TREE_CODE (type0);
2960 code1 = TREE_CODE (type1);
2962 /* If an error was already reported for one of the arguments,
2963 avoid reporting another error. */
2965 if (code0 == ERROR_MARK || code1 == ERROR_MARK)
2966 return error_mark_node;
2968 if ((invalid_op_diag
2969 = targetm.invalid_binary_op (code, type0, type1)))
2971 error (invalid_op_diag);
2972 return error_mark_node;
2975 switch (code)
2977 case PLUS_EXPR:
2978 /* Handle the pointer + int case. */
2979 if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
2980 return cp_pointer_int_sum (PLUS_EXPR, op0, op1);
2981 else if (code1 == POINTER_TYPE && code0 == INTEGER_TYPE)
2982 return cp_pointer_int_sum (PLUS_EXPR, op1, op0);
2983 else
2984 common = 1;
2985 break;
2987 case MINUS_EXPR:
2988 /* Subtraction of two similar pointers.
2989 We must subtract them as integers, then divide by object size. */
2990 if (code0 == POINTER_TYPE && code1 == POINTER_TYPE
2991 && same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (type0),
2992 TREE_TYPE (type1)))
2993 return pointer_diff (op0, op1, common_type (type0, type1));
2994 /* Handle pointer minus int. Just like pointer plus int. */
2995 else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
2996 return cp_pointer_int_sum (MINUS_EXPR, op0, op1);
2997 else
2998 common = 1;
2999 break;
3001 case MULT_EXPR:
3002 common = 1;
3003 break;
3005 case TRUNC_DIV_EXPR:
3006 case CEIL_DIV_EXPR:
3007 case FLOOR_DIV_EXPR:
3008 case ROUND_DIV_EXPR:
3009 case EXACT_DIV_EXPR:
3010 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE
3011 || code0 == COMPLEX_TYPE || code0 == VECTOR_TYPE)
3012 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
3013 || code1 == COMPLEX_TYPE || code1 == VECTOR_TYPE))
3015 if (TREE_CODE (op1) == INTEGER_CST && integer_zerop (op1))
3016 warning (OPT_Wdiv_by_zero, "division by zero in %<%E / 0%>", op0);
3017 else if (TREE_CODE (op1) == REAL_CST && real_zerop (op1))
3018 warning (OPT_Wdiv_by_zero, "division by zero in %<%E / 0.%>", op0);
3020 if (code0 == COMPLEX_TYPE || code0 == VECTOR_TYPE)
3021 code0 = TREE_CODE (TREE_TYPE (TREE_TYPE (op0)));
3022 if (code1 == COMPLEX_TYPE || code1 == VECTOR_TYPE)
3023 code1 = TREE_CODE (TREE_TYPE (TREE_TYPE (op1)));
3025 if (!(code0 == INTEGER_TYPE && code1 == INTEGER_TYPE))
3026 resultcode = RDIV_EXPR;
3027 else
3028 /* When dividing two signed integers, we have to promote to int.
3029 unless we divide by a constant != -1. Note that default
3030 conversion will have been performed on the operands at this
3031 point, so we have to dig out the original type to find out if
3032 it was unsigned. */
3033 shorten = ((TREE_CODE (op0) == NOP_EXPR
3034 && TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (op0, 0))))
3035 || (TREE_CODE (op1) == INTEGER_CST
3036 && ! integer_all_onesp (op1)));
3038 common = 1;
3040 break;
3042 case BIT_AND_EXPR:
3043 case BIT_IOR_EXPR:
3044 case BIT_XOR_EXPR:
3045 if ((code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
3046 || (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE))
3047 shorten = -1;
3048 break;
3050 case TRUNC_MOD_EXPR:
3051 case FLOOR_MOD_EXPR:
3052 if (code1 == INTEGER_TYPE && integer_zerop (op1))
3053 warning (OPT_Wdiv_by_zero, "division by zero in %<%E %% 0%>", op0);
3054 else if (code1 == REAL_TYPE && real_zerop (op1))
3055 warning (OPT_Wdiv_by_zero, "division by zero in %<%E %% 0.%>", op0);
3057 if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
3059 /* Although it would be tempting to shorten always here, that loses
3060 on some targets, since the modulo instruction is undefined if the
3061 quotient can't be represented in the computation mode. We shorten
3062 only if unsigned or if dividing by something we know != -1. */
3063 shorten = ((TREE_CODE (op0) == NOP_EXPR
3064 && TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (op0, 0))))
3065 || (TREE_CODE (op1) == INTEGER_CST
3066 && ! integer_all_onesp (op1)));
3067 common = 1;
3069 break;
3071 case TRUTH_ANDIF_EXPR:
3072 case TRUTH_ORIF_EXPR:
3073 case TRUTH_AND_EXPR:
3074 case TRUTH_OR_EXPR:
3075 result_type = boolean_type_node;
3076 break;
3078 /* Shift operations: result has same type as first operand;
3079 always convert second operand to int.
3080 Also set SHORT_SHIFT if shifting rightward. */
3082 case RSHIFT_EXPR:
3083 if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
3085 result_type = type0;
3086 if (TREE_CODE (op1) == INTEGER_CST)
3088 if (tree_int_cst_lt (op1, integer_zero_node))
3089 warning (0, "right shift count is negative");
3090 else
3092 if (! integer_zerop (op1))
3093 short_shift = 1;
3094 if (compare_tree_int (op1, TYPE_PRECISION (type0)) >= 0)
3095 warning (0, "right shift count >= width of type");
3098 /* Convert the shift-count to an integer, regardless of
3099 size of value being shifted. */
3100 if (TYPE_MAIN_VARIANT (TREE_TYPE (op1)) != integer_type_node)
3101 op1 = cp_convert (integer_type_node, op1);
3102 /* Avoid converting op1 to result_type later. */
3103 converted = 1;
3105 break;
3107 case LSHIFT_EXPR:
3108 if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
3110 result_type = type0;
3111 if (TREE_CODE (op1) == INTEGER_CST)
3113 if (tree_int_cst_lt (op1, integer_zero_node))
3114 warning (0, "left shift count is negative");
3115 else if (compare_tree_int (op1, TYPE_PRECISION (type0)) >= 0)
3116 warning (0, "left shift count >= width of type");
3118 /* Convert the shift-count to an integer, regardless of
3119 size of value being shifted. */
3120 if (TYPE_MAIN_VARIANT (TREE_TYPE (op1)) != integer_type_node)
3121 op1 = cp_convert (integer_type_node, op1);
3122 /* Avoid converting op1 to result_type later. */
3123 converted = 1;
3125 break;
3127 case RROTATE_EXPR:
3128 case LROTATE_EXPR:
3129 if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
3131 result_type = type0;
3132 if (TREE_CODE (op1) == INTEGER_CST)
3134 if (tree_int_cst_lt (op1, integer_zero_node))
3135 warning (0, "%s rotate count is negative",
3136 (code == LROTATE_EXPR) ? "left" : "right");
3137 else if (compare_tree_int (op1, TYPE_PRECISION (type0)) >= 0)
3138 warning (0, "%s rotate count >= width of type",
3139 (code == LROTATE_EXPR) ? "left" : "right");
3141 /* Convert the shift-count to an integer, regardless of
3142 size of value being shifted. */
3143 if (TYPE_MAIN_VARIANT (TREE_TYPE (op1)) != integer_type_node)
3144 op1 = cp_convert (integer_type_node, op1);
3146 break;
3148 case EQ_EXPR:
3149 case NE_EXPR:
3150 if (code0 == REAL_TYPE || code1 == REAL_TYPE)
3151 warning (OPT_Wfloat_equal,
3152 "comparing floating point with == or != is unsafe");
3153 if ((TREE_CODE (orig_op0) == STRING_CST && !integer_zerop (op1))
3154 || (TREE_CODE (orig_op1) == STRING_CST && !integer_zerop (op0)))
3155 warning (OPT_Wstring_literal_comparison,
3156 "comparison with string literal");
3158 build_type = boolean_type_node;
3159 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE
3160 || code0 == COMPLEX_TYPE)
3161 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
3162 || code1 == COMPLEX_TYPE))
3163 short_compare = 1;
3164 else if ((code0 == POINTER_TYPE && code1 == POINTER_TYPE)
3165 || (TYPE_PTRMEM_P (type0) && TYPE_PTRMEM_P (type1)))
3166 result_type = composite_pointer_type (type0, type1, op0, op1,
3167 "comparison");
3168 else if ((code0 == POINTER_TYPE || TYPE_PTRMEM_P (type0))
3169 && null_ptr_cst_p (op1))
3170 result_type = type0;
3171 else if ((code1 == POINTER_TYPE || TYPE_PTRMEM_P (type1))
3172 && null_ptr_cst_p (op0))
3173 result_type = type1;
3174 else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
3176 result_type = type0;
3177 error ("ISO C++ forbids comparison between pointer and integer");
3179 else if (code0 == INTEGER_TYPE && code1 == POINTER_TYPE)
3181 result_type = type1;
3182 error ("ISO C++ forbids comparison between pointer and integer");
3184 else if (TYPE_PTRMEMFUNC_P (type0) && null_ptr_cst_p (op1))
3186 op0 = build_ptrmemfunc_access_expr (op0, pfn_identifier);
3187 op1 = cp_convert (TREE_TYPE (op0), integer_zero_node);
3188 result_type = TREE_TYPE (op0);
3190 else if (TYPE_PTRMEMFUNC_P (type1) && null_ptr_cst_p (op0))
3191 return cp_build_binary_op (code, op1, op0);
3192 else if (TYPE_PTRMEMFUNC_P (type0) && TYPE_PTRMEMFUNC_P (type1)
3193 && same_type_p (type0, type1))
3195 /* E will be the final comparison. */
3196 tree e;
3197 /* E1 and E2 are for scratch. */
3198 tree e1;
3199 tree e2;
3200 tree pfn0;
3201 tree pfn1;
3202 tree delta0;
3203 tree delta1;
3205 if (TREE_SIDE_EFFECTS (op0))
3206 op0 = save_expr (op0);
3207 if (TREE_SIDE_EFFECTS (op1))
3208 op1 = save_expr (op1);
3210 /* We generate:
3212 (op0.pfn == op1.pfn
3213 && (!op0.pfn || op0.delta == op1.delta))
3215 The reason for the `!op0.pfn' bit is that a NULL
3216 pointer-to-member is any member with a zero PFN; the
3217 DELTA field is unspecified. */
3218 pfn0 = pfn_from_ptrmemfunc (op0);
3219 pfn1 = pfn_from_ptrmemfunc (op1);
3220 delta0 = build_ptrmemfunc_access_expr (op0,
3221 delta_identifier);
3222 delta1 = build_ptrmemfunc_access_expr (op1,
3223 delta_identifier);
3224 e1 = cp_build_binary_op (EQ_EXPR, delta0, delta1);
3225 e2 = cp_build_binary_op (EQ_EXPR,
3226 pfn0,
3227 cp_convert (TREE_TYPE (pfn0),
3228 integer_zero_node));
3229 e1 = cp_build_binary_op (TRUTH_ORIF_EXPR, e1, e2);
3230 e2 = build2 (EQ_EXPR, boolean_type_node, pfn0, pfn1);
3231 e = cp_build_binary_op (TRUTH_ANDIF_EXPR, e2, e1);
3232 if (code == EQ_EXPR)
3233 return e;
3234 return cp_build_binary_op (EQ_EXPR, e, integer_zero_node);
3236 else
3238 gcc_assert (!TYPE_PTRMEMFUNC_P (type0)
3239 || !same_type_p (TYPE_PTRMEMFUNC_FN_TYPE (type0),
3240 type1));
3241 gcc_assert (!TYPE_PTRMEMFUNC_P (type1)
3242 || !same_type_p (TYPE_PTRMEMFUNC_FN_TYPE (type1),
3243 type0));
3246 break;
3248 case MAX_EXPR:
3249 case MIN_EXPR:
3250 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE)
3251 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE))
3252 shorten = 1;
3253 else if (code0 == POINTER_TYPE && code1 == POINTER_TYPE)
3254 result_type = composite_pointer_type (type0, type1, op0, op1,
3255 "comparison");
3256 break;
3258 case LE_EXPR:
3259 case GE_EXPR:
3260 case LT_EXPR:
3261 case GT_EXPR:
3262 if (TREE_CODE (orig_op0) == STRING_CST
3263 || TREE_CODE (orig_op1) == STRING_CST)
3264 warning (OPT_Wstring_literal_comparison,
3265 "comparison with string literal");
3267 build_type = boolean_type_node;
3268 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE)
3269 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE))
3270 short_compare = 1;
3271 else if (code0 == POINTER_TYPE && code1 == POINTER_TYPE)
3272 result_type = composite_pointer_type (type0, type1, op0, op1,
3273 "comparison");
3274 else if (code0 == POINTER_TYPE && TREE_CODE (op1) == INTEGER_CST
3275 && integer_zerop (op1))
3276 result_type = type0;
3277 else if (code1 == POINTER_TYPE && TREE_CODE (op0) == INTEGER_CST
3278 && integer_zerop (op0))
3279 result_type = type1;
3280 else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
3282 result_type = type0;
3283 pedwarn ("ISO C++ forbids comparison between pointer and integer");
3285 else if (code0 == INTEGER_TYPE && code1 == POINTER_TYPE)
3287 result_type = type1;
3288 pedwarn ("ISO C++ forbids comparison between pointer and integer");
3290 break;
3292 case UNORDERED_EXPR:
3293 case ORDERED_EXPR:
3294 case UNLT_EXPR:
3295 case UNLE_EXPR:
3296 case UNGT_EXPR:
3297 case UNGE_EXPR:
3298 case UNEQ_EXPR:
3299 build_type = integer_type_node;
3300 if (code0 != REAL_TYPE || code1 != REAL_TYPE)
3302 error ("unordered comparison on non-floating point argument");
3303 return error_mark_node;
3305 common = 1;
3306 break;
3308 default:
3309 break;
3312 if (((code0 == INTEGER_TYPE || code0 == REAL_TYPE || code0 == COMPLEX_TYPE)
3313 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
3314 || code1 == COMPLEX_TYPE)))
3315 arithmetic_types_p = 1;
3316 else
3318 arithmetic_types_p = 0;
3319 /* Vector arithmetic is only allowed when both sides are vectors. */
3320 if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE)
3322 if (!tree_int_cst_equal (TYPE_SIZE (type0), TYPE_SIZE (type1))
3323 || !same_scalar_type_ignoring_signedness (TREE_TYPE (type0),
3324 TREE_TYPE (type1)))
3326 binary_op_error (code);
3327 return error_mark_node;
3329 arithmetic_types_p = 1;
3332 /* Determine the RESULT_TYPE, if it is not already known. */
3333 if (!result_type
3334 && arithmetic_types_p
3335 && (shorten || common || short_compare))
3336 result_type = common_type (type0, type1);
3338 if (!result_type)
3340 error ("invalid operands of types %qT and %qT to binary %qO",
3341 TREE_TYPE (orig_op0), TREE_TYPE (orig_op1), code);
3342 return error_mark_node;
3345 /* If we're in a template, the only thing we need to know is the
3346 RESULT_TYPE. */
3347 if (processing_template_decl)
3348 return build2 (resultcode,
3349 build_type ? build_type : result_type,
3350 op0, op1);
3352 if (arithmetic_types_p)
3354 int none_complex = (code0 != COMPLEX_TYPE && code1 != COMPLEX_TYPE);
3356 /* For certain operations (which identify themselves by shorten != 0)
3357 if both args were extended from the same smaller type,
3358 do the arithmetic in that type and then extend.
3360 shorten !=0 and !=1 indicates a bitwise operation.
3361 For them, this optimization is safe only if
3362 both args are zero-extended or both are sign-extended.
3363 Otherwise, we might change the result.
3364 Eg, (short)-1 | (unsigned short)-1 is (int)-1
3365 but calculated in (unsigned short) it would be (unsigned short)-1. */
3367 if (shorten && none_complex)
3369 int unsigned0, unsigned1;
3370 tree arg0 = get_narrower (op0, &unsigned0);
3371 tree arg1 = get_narrower (op1, &unsigned1);
3372 /* UNS is 1 if the operation to be done is an unsigned one. */
3373 int uns = TYPE_UNSIGNED (result_type);
3374 tree type;
3376 final_type = result_type;
3378 /* Handle the case that OP0 does not *contain* a conversion
3379 but it *requires* conversion to FINAL_TYPE. */
3381 if (op0 == arg0 && TREE_TYPE (op0) != final_type)
3382 unsigned0 = TYPE_UNSIGNED (TREE_TYPE (op0));
3383 if (op1 == arg1 && TREE_TYPE (op1) != final_type)
3384 unsigned1 = TYPE_UNSIGNED (TREE_TYPE (op1));
3386 /* Now UNSIGNED0 is 1 if ARG0 zero-extends to FINAL_TYPE. */
3388 /* For bitwise operations, signedness of nominal type
3389 does not matter. Consider only how operands were extended. */
3390 if (shorten == -1)
3391 uns = unsigned0;
3393 /* Note that in all three cases below we refrain from optimizing
3394 an unsigned operation on sign-extended args.
3395 That would not be valid. */
3397 /* Both args variable: if both extended in same way
3398 from same width, do it in that width.
3399 Do it unsigned if args were zero-extended. */
3400 if ((TYPE_PRECISION (TREE_TYPE (arg0))
3401 < TYPE_PRECISION (result_type))
3402 && (TYPE_PRECISION (TREE_TYPE (arg1))
3403 == TYPE_PRECISION (TREE_TYPE (arg0)))
3404 && unsigned0 == unsigned1
3405 && (unsigned0 || !uns))
3406 result_type = c_common_signed_or_unsigned_type
3407 (unsigned0, common_type (TREE_TYPE (arg0), TREE_TYPE (arg1)));
3408 else if (TREE_CODE (arg0) == INTEGER_CST
3409 && (unsigned1 || !uns)
3410 && (TYPE_PRECISION (TREE_TYPE (arg1))
3411 < TYPE_PRECISION (result_type))
3412 && (type = c_common_signed_or_unsigned_type
3413 (unsigned1, TREE_TYPE (arg1)),
3414 int_fits_type_p (arg0, type)))
3415 result_type = type;
3416 else if (TREE_CODE (arg1) == INTEGER_CST
3417 && (unsigned0 || !uns)
3418 && (TYPE_PRECISION (TREE_TYPE (arg0))
3419 < TYPE_PRECISION (result_type))
3420 && (type = c_common_signed_or_unsigned_type
3421 (unsigned0, TREE_TYPE (arg0)),
3422 int_fits_type_p (arg1, type)))
3423 result_type = type;
3426 /* Shifts can be shortened if shifting right. */
3428 if (short_shift)
3430 int unsigned_arg;
3431 tree arg0 = get_narrower (op0, &unsigned_arg);
3433 final_type = result_type;
3435 if (arg0 == op0 && final_type == TREE_TYPE (op0))
3436 unsigned_arg = TYPE_UNSIGNED (TREE_TYPE (op0));
3438 if (TYPE_PRECISION (TREE_TYPE (arg0)) < TYPE_PRECISION (result_type)
3439 /* We can shorten only if the shift count is less than the
3440 number of bits in the smaller type size. */
3441 && compare_tree_int (op1, TYPE_PRECISION (TREE_TYPE (arg0))) < 0
3442 /* If arg is sign-extended and then unsigned-shifted,
3443 we can simulate this with a signed shift in arg's type
3444 only if the extended result is at least twice as wide
3445 as the arg. Otherwise, the shift could use up all the
3446 ones made by sign-extension and bring in zeros.
3447 We can't optimize that case at all, but in most machines
3448 it never happens because available widths are 2**N. */
3449 && (!TYPE_UNSIGNED (final_type)
3450 || unsigned_arg
3451 || (((unsigned) 2 * TYPE_PRECISION (TREE_TYPE (arg0)))
3452 <= TYPE_PRECISION (result_type))))
3454 /* Do an unsigned shift if the operand was zero-extended. */
3455 result_type
3456 = c_common_signed_or_unsigned_type (unsigned_arg,
3457 TREE_TYPE (arg0));
3458 /* Convert value-to-be-shifted to that type. */
3459 if (TREE_TYPE (op0) != result_type)
3460 op0 = cp_convert (result_type, op0);
3461 converted = 1;
3465 /* Comparison operations are shortened too but differently.
3466 They identify themselves by setting short_compare = 1. */
3468 if (short_compare)
3470 /* Don't write &op0, etc., because that would prevent op0
3471 from being kept in a register.
3472 Instead, make copies of the our local variables and
3473 pass the copies by reference, then copy them back afterward. */
3474 tree xop0 = op0, xop1 = op1, xresult_type = result_type;
3475 enum tree_code xresultcode = resultcode;
3476 tree val
3477 = shorten_compare (&xop0, &xop1, &xresult_type, &xresultcode);
3478 if (val != 0)
3479 return cp_convert (boolean_type_node, val);
3480 op0 = xop0, op1 = xop1;
3481 converted = 1;
3482 resultcode = xresultcode;
3485 if ((short_compare || code == MIN_EXPR || code == MAX_EXPR)
3486 && warn_sign_compare
3487 /* Do not warn until the template is instantiated; we cannot
3488 bound the ranges of the arguments until that point. */
3489 && !processing_template_decl)
3491 int op0_signed = !TYPE_UNSIGNED (TREE_TYPE (orig_op0));
3492 int op1_signed = !TYPE_UNSIGNED (TREE_TYPE (orig_op1));
3494 int unsignedp0, unsignedp1;
3495 tree primop0 = get_narrower (op0, &unsignedp0);
3496 tree primop1 = get_narrower (op1, &unsignedp1);
3498 /* Check for comparison of different enum types. */
3499 if (TREE_CODE (TREE_TYPE (orig_op0)) == ENUMERAL_TYPE
3500 && TREE_CODE (TREE_TYPE (orig_op1)) == ENUMERAL_TYPE
3501 && TYPE_MAIN_VARIANT (TREE_TYPE (orig_op0))
3502 != TYPE_MAIN_VARIANT (TREE_TYPE (orig_op1)))
3504 warning (0, "comparison between types %q#T and %q#T",
3505 TREE_TYPE (orig_op0), TREE_TYPE (orig_op1));
3508 /* Give warnings for comparisons between signed and unsigned
3509 quantities that may fail. */
3510 /* Do the checking based on the original operand trees, so that
3511 casts will be considered, but default promotions won't be. */
3513 /* Do not warn if the comparison is being done in a signed type,
3514 since the signed type will only be chosen if it can represent
3515 all the values of the unsigned type. */
3516 if (!TYPE_UNSIGNED (result_type))
3517 /* OK */;
3518 /* Do not warn if both operands are unsigned. */
3519 else if (op0_signed == op1_signed)
3520 /* OK */;
3521 /* Do not warn if the signed quantity is an unsuffixed
3522 integer literal (or some static constant expression
3523 involving such literals or a conditional expression
3524 involving such literals) and it is non-negative. */
3525 else if ((op0_signed && tree_expr_nonnegative_p (orig_op0))
3526 || (op1_signed && tree_expr_nonnegative_p (orig_op1)))
3527 /* OK */;
3528 /* Do not warn if the comparison is an equality operation,
3529 the unsigned quantity is an integral constant and it does
3530 not use the most significant bit of result_type. */
3531 else if ((resultcode == EQ_EXPR || resultcode == NE_EXPR)
3532 && ((op0_signed && TREE_CODE (orig_op1) == INTEGER_CST
3533 && int_fits_type_p (orig_op1, c_common_signed_type
3534 (result_type)))
3535 || (op1_signed && TREE_CODE (orig_op0) == INTEGER_CST
3536 && int_fits_type_p (orig_op0, c_common_signed_type
3537 (result_type)))))
3538 /* OK */;
3539 else
3540 warning (0, "comparison between signed and unsigned integer expressions");
3542 /* Warn if two unsigned values are being compared in a size
3543 larger than their original size, and one (and only one) is the
3544 result of a `~' operator. This comparison will always fail.
3546 Also warn if one operand is a constant, and the constant does not
3547 have all bits set that are set in the ~ operand when it is
3548 extended. */
3550 if ((TREE_CODE (primop0) == BIT_NOT_EXPR)
3551 ^ (TREE_CODE (primop1) == BIT_NOT_EXPR))
3553 if (TREE_CODE (primop0) == BIT_NOT_EXPR)
3554 primop0 = get_narrower (TREE_OPERAND (op0, 0), &unsignedp0);
3555 if (TREE_CODE (primop1) == BIT_NOT_EXPR)
3556 primop1 = get_narrower (TREE_OPERAND (op1, 0), &unsignedp1);
3558 if (host_integerp (primop0, 0) || host_integerp (primop1, 0))
3560 tree primop;
3561 HOST_WIDE_INT constant, mask;
3562 int unsignedp;
3563 unsigned int bits;
3565 if (host_integerp (primop0, 0))
3567 primop = primop1;
3568 unsignedp = unsignedp1;
3569 constant = tree_low_cst (primop0, 0);
3571 else
3573 primop = primop0;
3574 unsignedp = unsignedp0;
3575 constant = tree_low_cst (primop1, 0);
3578 bits = TYPE_PRECISION (TREE_TYPE (primop));
3579 if (bits < TYPE_PRECISION (result_type)
3580 && bits < HOST_BITS_PER_LONG && unsignedp)
3582 mask = (~ (HOST_WIDE_INT) 0) << bits;
3583 if ((mask & constant) != mask)
3584 warning (0, "comparison of promoted ~unsigned with constant");
3587 else if (unsignedp0 && unsignedp1
3588 && (TYPE_PRECISION (TREE_TYPE (primop0))
3589 < TYPE_PRECISION (result_type))
3590 && (TYPE_PRECISION (TREE_TYPE (primop1))
3591 < TYPE_PRECISION (result_type)))
3592 warning (0, "comparison of promoted ~unsigned with unsigned");
3597 /* If CONVERTED is zero, both args will be converted to type RESULT_TYPE.
3598 Then the expression will be built.
3599 It will be given type FINAL_TYPE if that is nonzero;
3600 otherwise, it will be given type RESULT_TYPE. */
3602 /* Issue warnings about peculiar, but valid, uses of NULL. */
3603 if (/* It's reasonable to use pointer values as operands of &&
3604 and ||, so NULL is no exception. */
3605 !(code == TRUTH_ANDIF_EXPR || code == TRUTH_ORIF_EXPR)
3606 && (/* If OP0 is NULL and OP1 is not a pointer, or vice versa. */
3607 (orig_op0 == null_node
3608 && TREE_CODE (TREE_TYPE (op1)) != POINTER_TYPE)
3609 /* Or vice versa. */
3610 || (orig_op1 == null_node
3611 && TREE_CODE (TREE_TYPE (op0)) != POINTER_TYPE)
3612 /* Or, both are NULL and the operation was not a comparison. */
3613 || (orig_op0 == null_node && orig_op1 == null_node
3614 && code != EQ_EXPR && code != NE_EXPR)))
3615 /* Some sort of arithmetic operation involving NULL was
3616 performed. Note that pointer-difference and pointer-addition
3617 have already been handled above, and so we don't end up here in
3618 that case. */
3619 warning (0, "NULL used in arithmetic");
3621 if (! converted)
3623 if (TREE_TYPE (op0) != result_type)
3624 op0 = cp_convert (result_type, op0);
3625 if (TREE_TYPE (op1) != result_type)
3626 op1 = cp_convert (result_type, op1);
3628 if (op0 == error_mark_node || op1 == error_mark_node)
3629 return error_mark_node;
3632 if (build_type == NULL_TREE)
3633 build_type = result_type;
3635 result = build2 (resultcode, build_type, op0, op1);
3636 result = fold_if_not_in_template (result);
3637 if (final_type != 0)
3638 result = cp_convert (final_type, result);
3639 return result;
3642 /* Return a tree for the sum or difference (RESULTCODE says which)
3643 of pointer PTROP and integer INTOP. */
3645 static tree
3646 cp_pointer_int_sum (enum tree_code resultcode, tree ptrop, tree intop)
3648 tree res_type = TREE_TYPE (ptrop);
3650 /* pointer_int_sum() uses size_in_bytes() on the TREE_TYPE(res_type)
3651 in certain circumstance (when it's valid to do so). So we need
3652 to make sure it's complete. We don't need to check here, if we
3653 can actually complete it at all, as those checks will be done in
3654 pointer_int_sum() anyway. */
3655 complete_type (TREE_TYPE (res_type));
3657 return pointer_int_sum (resultcode, ptrop,
3658 fold_if_not_in_template (intop));
3661 /* Return a tree for the difference of pointers OP0 and OP1.
3662 The resulting tree has type int. */
3664 static tree
3665 pointer_diff (tree op0, tree op1, tree ptrtype)
3667 tree result;
3668 tree restype = ptrdiff_type_node;
3669 tree target_type = TREE_TYPE (ptrtype);
3671 if (!complete_type_or_else (target_type, NULL_TREE))
3672 return error_mark_node;
3674 if (pedantic || warn_pointer_arith)
3676 if (TREE_CODE (target_type) == VOID_TYPE)
3677 pedwarn ("ISO C++ forbids using pointer of type %<void *%> in subtraction");
3678 if (TREE_CODE (target_type) == FUNCTION_TYPE)
3679 pedwarn ("ISO C++ forbids using pointer to a function in subtraction");
3680 if (TREE_CODE (target_type) == METHOD_TYPE)
3681 pedwarn ("ISO C++ forbids using pointer to a method in subtraction");
3684 /* First do the subtraction as integers;
3685 then drop through to build the divide operator. */
3687 op0 = cp_build_binary_op (MINUS_EXPR,
3688 cp_convert (restype, op0),
3689 cp_convert (restype, op1));
3691 /* This generates an error if op1 is a pointer to an incomplete type. */
3692 if (!COMPLETE_TYPE_P (TREE_TYPE (TREE_TYPE (op1))))
3693 error ("invalid use of a pointer to an incomplete type in pointer arithmetic");
3695 op1 = (TYPE_PTROB_P (ptrtype)
3696 ? size_in_bytes (target_type)
3697 : integer_one_node);
3699 /* Do the division. */
3701 result = build2 (EXACT_DIV_EXPR, restype, op0, cp_convert (restype, op1));
3702 return fold_if_not_in_template (result);
3705 /* Construct and perhaps optimize a tree representation
3706 for a unary operation. CODE, a tree_code, specifies the operation
3707 and XARG is the operand. */
3709 tree
3710 build_x_unary_op (enum tree_code code, tree xarg)
3712 tree orig_expr = xarg;
3713 tree exp;
3714 int ptrmem = 0;
3716 if (processing_template_decl)
3718 if (type_dependent_expression_p (xarg))
3719 return build_min_nt (code, xarg, NULL_TREE);
3721 xarg = build_non_dependent_expr (xarg);
3724 exp = NULL_TREE;
3726 /* [expr.unary.op] says:
3728 The address of an object of incomplete type can be taken.
3730 (And is just the ordinary address operator, not an overloaded
3731 "operator &".) However, if the type is a template
3732 specialization, we must complete the type at this point so that
3733 an overloaded "operator &" will be available if required. */
3734 if (code == ADDR_EXPR
3735 && TREE_CODE (xarg) != TEMPLATE_ID_EXPR
3736 && ((CLASS_TYPE_P (TREE_TYPE (xarg))
3737 && !COMPLETE_TYPE_P (complete_type (TREE_TYPE (xarg))))
3738 || (TREE_CODE (xarg) == OFFSET_REF)))
3739 /* Don't look for a function. */;
3740 else
3741 exp = build_new_op (code, LOOKUP_NORMAL, xarg, NULL_TREE, NULL_TREE,
3742 /*overloaded_p=*/NULL);
3743 if (!exp && code == ADDR_EXPR)
3745 /* A pointer to member-function can be formed only by saying
3746 &X::mf. */
3747 if (!flag_ms_extensions && TREE_CODE (TREE_TYPE (xarg)) == METHOD_TYPE
3748 && (TREE_CODE (xarg) != OFFSET_REF || !PTRMEM_OK_P (xarg)))
3750 if (TREE_CODE (xarg) != OFFSET_REF
3751 || !TYPE_P (TREE_OPERAND (xarg, 0)))
3753 error ("invalid use of %qE to form a pointer-to-member-function",
3754 xarg);
3755 if (TREE_CODE (xarg) != OFFSET_REF)
3756 inform (" a qualified-id is required");
3757 return error_mark_node;
3759 else
3761 error ("parentheses around %qE cannot be used to form a"
3762 " pointer-to-member-function",
3763 xarg);
3764 PTRMEM_OK_P (xarg) = 1;
3768 if (TREE_CODE (xarg) == OFFSET_REF)
3770 ptrmem = PTRMEM_OK_P (xarg);
3772 if (!ptrmem && !flag_ms_extensions
3773 && TREE_CODE (TREE_TYPE (TREE_OPERAND (xarg, 1))) == METHOD_TYPE)
3775 /* A single non-static member, make sure we don't allow a
3776 pointer-to-member. */
3777 xarg = build2 (OFFSET_REF, TREE_TYPE (xarg),
3778 TREE_OPERAND (xarg, 0),
3779 ovl_cons (TREE_OPERAND (xarg, 1), NULL_TREE));
3780 PTRMEM_OK_P (xarg) = ptrmem;
3783 else if (TREE_CODE (xarg) == TARGET_EXPR)
3784 warning (0, "taking address of temporary");
3785 exp = build_unary_op (ADDR_EXPR, xarg, 0);
3788 if (processing_template_decl && exp != error_mark_node)
3789 exp = build_min_non_dep (code, exp, orig_expr,
3790 /*For {PRE,POST}{INC,DEC}REMENT_EXPR*/NULL_TREE);
3791 if (TREE_CODE (exp) == ADDR_EXPR)
3792 PTRMEM_OK_P (exp) = ptrmem;
3793 return exp;
3796 /* Like c_common_truthvalue_conversion, but handle pointer-to-member
3797 constants, where a null value is represented by an INTEGER_CST of
3798 -1. */
3800 tree
3801 cp_truthvalue_conversion (tree expr)
3803 tree type = TREE_TYPE (expr);
3804 if (TYPE_PTRMEM_P (type))
3805 return build_binary_op (NE_EXPR, expr, integer_zero_node, 1);
3806 else
3807 return c_common_truthvalue_conversion (expr);
3810 /* Just like cp_truthvalue_conversion, but we want a CLEANUP_POINT_EXPR. */
3812 tree
3813 condition_conversion (tree expr)
3815 tree t;
3816 if (processing_template_decl)
3817 return expr;
3818 t = perform_implicit_conversion (boolean_type_node, expr);
3819 t = fold_build_cleanup_point_expr (boolean_type_node, t);
3820 return t;
3823 /* Return an ADDR_EXPR giving the address of T. This function
3824 attempts no optimizations or simplifications; it is a low-level
3825 primitive. */
3827 tree
3828 build_address (tree t)
3830 tree addr;
3832 if (error_operand_p (t) || !cxx_mark_addressable (t))
3833 return error_mark_node;
3835 addr = build1 (ADDR_EXPR, build_pointer_type (TREE_TYPE (t)), t);
3837 return addr;
3840 /* Return a NOP_EXPR converting EXPR to TYPE. */
3842 tree
3843 build_nop (tree type, tree expr)
3845 if (type == error_mark_node || error_operand_p (expr))
3846 return expr;
3847 return build1 (NOP_EXPR, type, expr);
3850 /* C++: Must handle pointers to members.
3852 Perhaps type instantiation should be extended to handle conversion
3853 from aggregates to types we don't yet know we want? (Or are those
3854 cases typically errors which should be reported?)
3856 NOCONVERT nonzero suppresses the default promotions
3857 (such as from short to int). */
3859 tree
3860 build_unary_op (enum tree_code code, tree xarg, int noconvert)
3862 /* No default_conversion here. It causes trouble for ADDR_EXPR. */
3863 tree arg = xarg;
3864 tree argtype = 0;
3865 const char *errstring = NULL;
3866 tree val;
3867 const char *invalid_op_diag;
3869 if (arg == error_mark_node)
3870 return error_mark_node;
3872 if ((invalid_op_diag
3873 = targetm.invalid_unary_op ((code == UNARY_PLUS_EXPR
3874 ? CONVERT_EXPR
3875 : code),
3876 TREE_TYPE (xarg))))
3878 error (invalid_op_diag);
3879 return error_mark_node;
3882 switch (code)
3884 case UNARY_PLUS_EXPR:
3885 case NEGATE_EXPR:
3887 int flags = WANT_ARITH | WANT_ENUM;
3888 /* Unary plus (but not unary minus) is allowed on pointers. */
3889 if (code == UNARY_PLUS_EXPR)
3890 flags |= WANT_POINTER;
3891 arg = build_expr_type_conversion (flags, arg, true);
3892 if (!arg)
3893 errstring = (code == NEGATE_EXPR
3894 ? "wrong type argument to unary minus"
3895 : "wrong type argument to unary plus");
3896 else
3898 if (!noconvert && CP_INTEGRAL_TYPE_P (TREE_TYPE (arg)))
3899 arg = perform_integral_promotions (arg);
3901 /* Make sure the result is not an lvalue: a unary plus or minus
3902 expression is always a rvalue. */
3903 arg = rvalue (arg);
3906 break;
3908 case BIT_NOT_EXPR:
3909 if (TREE_CODE (TREE_TYPE (arg)) == COMPLEX_TYPE)
3911 code = CONJ_EXPR;
3912 if (!noconvert)
3913 arg = default_conversion (arg);
3915 else if (!(arg = build_expr_type_conversion (WANT_INT | WANT_ENUM,
3916 arg, true)))
3917 errstring = "wrong type argument to bit-complement";
3918 else if (!noconvert)
3919 arg = perform_integral_promotions (arg);
3920 break;
3922 case ABS_EXPR:
3923 if (!(arg = build_expr_type_conversion (WANT_ARITH | WANT_ENUM, arg, true)))
3924 errstring = "wrong type argument to abs";
3925 else if (!noconvert)
3926 arg = default_conversion (arg);
3927 break;
3929 case CONJ_EXPR:
3930 /* Conjugating a real value is a no-op, but allow it anyway. */
3931 if (!(arg = build_expr_type_conversion (WANT_ARITH | WANT_ENUM, arg, true)))
3932 errstring = "wrong type argument to conjugation";
3933 else if (!noconvert)
3934 arg = default_conversion (arg);
3935 break;
3937 case TRUTH_NOT_EXPR:
3938 arg = perform_implicit_conversion (boolean_type_node, arg);
3939 val = invert_truthvalue (arg);
3940 if (arg != error_mark_node)
3941 return val;
3942 errstring = "in argument to unary !";
3943 break;
3945 case NOP_EXPR:
3946 break;
3948 case REALPART_EXPR:
3949 if (TREE_CODE (arg) == COMPLEX_CST)
3950 return TREE_REALPART (arg);
3951 else if (TREE_CODE (TREE_TYPE (arg)) == COMPLEX_TYPE)
3953 arg = build1 (REALPART_EXPR, TREE_TYPE (TREE_TYPE (arg)), arg);
3954 return fold_if_not_in_template (arg);
3956 else
3957 return arg;
3959 case IMAGPART_EXPR:
3960 if (TREE_CODE (arg) == COMPLEX_CST)
3961 return TREE_IMAGPART (arg);
3962 else if (TREE_CODE (TREE_TYPE (arg)) == COMPLEX_TYPE)
3964 arg = build1 (IMAGPART_EXPR, TREE_TYPE (TREE_TYPE (arg)), arg);
3965 return fold_if_not_in_template (arg);
3967 else
3968 return cp_convert (TREE_TYPE (arg), integer_zero_node);
3970 case PREINCREMENT_EXPR:
3971 case POSTINCREMENT_EXPR:
3972 case PREDECREMENT_EXPR:
3973 case POSTDECREMENT_EXPR:
3974 /* Handle complex lvalues (when permitted)
3975 by reduction to simpler cases. */
3977 val = unary_complex_lvalue (code, arg);
3978 if (val != 0)
3979 return val;
3981 /* Increment or decrement the real part of the value,
3982 and don't change the imaginary part. */
3983 if (TREE_CODE (TREE_TYPE (arg)) == COMPLEX_TYPE)
3985 tree real, imag;
3987 arg = stabilize_reference (arg);
3988 real = build_unary_op (REALPART_EXPR, arg, 1);
3989 imag = build_unary_op (IMAGPART_EXPR, arg, 1);
3990 return build2 (COMPLEX_EXPR, TREE_TYPE (arg),
3991 build_unary_op (code, real, 1), imag);
3994 /* Report invalid types. */
3996 if (!(arg = build_expr_type_conversion (WANT_ARITH | WANT_POINTER,
3997 arg, true)))
3999 if (code == PREINCREMENT_EXPR)
4000 errstring ="no pre-increment operator for type";
4001 else if (code == POSTINCREMENT_EXPR)
4002 errstring ="no post-increment operator for type";
4003 else if (code == PREDECREMENT_EXPR)
4004 errstring ="no pre-decrement operator for type";
4005 else
4006 errstring ="no post-decrement operator for type";
4007 break;
4010 /* Report something read-only. */
4012 if (CP_TYPE_CONST_P (TREE_TYPE (arg))
4013 || TREE_READONLY (arg))
4014 readonly_error (arg, ((code == PREINCREMENT_EXPR
4015 || code == POSTINCREMENT_EXPR)
4016 ? "increment" : "decrement"),
4020 tree inc;
4021 tree result_type = TREE_TYPE (arg);
4023 arg = get_unwidened (arg, 0);
4024 argtype = TREE_TYPE (arg);
4026 /* ARM $5.2.5 last annotation says this should be forbidden. */
4027 if (TREE_CODE (argtype) == ENUMERAL_TYPE)
4028 pedwarn ("ISO C++ forbids %sing an enum",
4029 (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
4030 ? "increment" : "decrement");
4032 /* Compute the increment. */
4034 if (TREE_CODE (argtype) == POINTER_TYPE)
4036 tree type = complete_type (TREE_TYPE (argtype));
4038 if (!COMPLETE_OR_VOID_TYPE_P (type))
4039 error ("cannot %s a pointer to incomplete type %qT",
4040 ((code == PREINCREMENT_EXPR
4041 || code == POSTINCREMENT_EXPR)
4042 ? "increment" : "decrement"), TREE_TYPE (argtype));
4043 else if ((pedantic || warn_pointer_arith)
4044 && !TYPE_PTROB_P (argtype))
4045 pedwarn ("ISO C++ forbids %sing a pointer of type %qT",
4046 ((code == PREINCREMENT_EXPR
4047 || code == POSTINCREMENT_EXPR)
4048 ? "increment" : "decrement"), argtype);
4049 inc = cxx_sizeof_nowarn (TREE_TYPE (argtype));
4051 else
4052 inc = integer_one_node;
4054 inc = cp_convert (argtype, inc);
4056 /* Handle incrementing a cast-expression. */
4058 switch (TREE_CODE (arg))
4060 case NOP_EXPR:
4061 case CONVERT_EXPR:
4062 case FLOAT_EXPR:
4063 case FIX_TRUNC_EXPR:
4064 case FIX_FLOOR_EXPR:
4065 case FIX_ROUND_EXPR:
4066 case FIX_CEIL_EXPR:
4068 tree incremented, modify, value, compound;
4069 if (! lvalue_p (arg) && pedantic)
4070 pedwarn ("cast to non-reference type used as lvalue");
4071 arg = stabilize_reference (arg);
4072 if (code == PREINCREMENT_EXPR || code == PREDECREMENT_EXPR)
4073 value = arg;
4074 else
4075 value = save_expr (arg);
4076 incremented = build2 (((code == PREINCREMENT_EXPR
4077 || code == POSTINCREMENT_EXPR)
4078 ? PLUS_EXPR : MINUS_EXPR),
4079 argtype, value, inc);
4081 modify = build_modify_expr (arg, NOP_EXPR, incremented);
4082 compound = build2 (COMPOUND_EXPR, TREE_TYPE (arg),
4083 modify, value);
4085 /* Eliminate warning about unused result of + or -. */
4086 TREE_NO_WARNING (compound) = 1;
4087 return compound;
4090 default:
4091 break;
4094 /* Complain about anything else that is not a true lvalue. */
4095 if (!lvalue_or_else (arg, ((code == PREINCREMENT_EXPR
4096 || code == POSTINCREMENT_EXPR)
4097 ? lv_increment : lv_decrement)))
4098 return error_mark_node;
4100 /* Forbid using -- on `bool'. */
4101 if (TREE_TYPE (arg) == boolean_type_node)
4103 if (code == POSTDECREMENT_EXPR || code == PREDECREMENT_EXPR)
4105 error ("invalid use of %<--%> on bool variable %qD", arg);
4106 return error_mark_node;
4108 val = boolean_increment (code, arg);
4110 else
4111 val = build2 (code, TREE_TYPE (arg), arg, inc);
4113 TREE_SIDE_EFFECTS (val) = 1;
4114 return cp_convert (result_type, val);
4117 case ADDR_EXPR:
4118 /* Note that this operation never does default_conversion
4119 regardless of NOCONVERT. */
4121 argtype = lvalue_type (arg);
4123 if (TREE_CODE (arg) == OFFSET_REF)
4124 goto offset_ref;
4126 if (TREE_CODE (argtype) == REFERENCE_TYPE)
4128 tree type = build_pointer_type (TREE_TYPE (argtype));
4129 arg = build1 (CONVERT_EXPR, type, arg);
4130 return arg;
4132 else if (pedantic && DECL_MAIN_P (arg))
4133 /* ARM $3.4 */
4134 pedwarn ("ISO C++ forbids taking address of function %<::main%>");
4136 /* Let &* cancel out to simplify resulting code. */
4137 if (TREE_CODE (arg) == INDIRECT_REF)
4139 /* We don't need to have `current_class_ptr' wrapped in a
4140 NON_LVALUE_EXPR node. */
4141 if (arg == current_class_ref)
4142 return current_class_ptr;
4144 arg = TREE_OPERAND (arg, 0);
4145 if (TREE_CODE (TREE_TYPE (arg)) == REFERENCE_TYPE)
4147 tree type = build_pointer_type (TREE_TYPE (TREE_TYPE (arg)));
4148 arg = build1 (CONVERT_EXPR, type, arg);
4150 else
4151 /* Don't let this be an lvalue. */
4152 arg = rvalue (arg);
4153 return arg;
4156 /* Uninstantiated types are all functions. Taking the
4157 address of a function is a no-op, so just return the
4158 argument. */
4160 gcc_assert (TREE_CODE (arg) != IDENTIFIER_NODE
4161 || !IDENTIFIER_OPNAME_P (arg));
4163 if (TREE_CODE (arg) == COMPONENT_REF && type_unknown_p (arg)
4164 && !really_overloaded_fn (TREE_OPERAND (arg, 1)))
4166 /* They're trying to take the address of a unique non-static
4167 member function. This is ill-formed (except in MS-land),
4168 but let's try to DTRT.
4169 Note: We only handle unique functions here because we don't
4170 want to complain if there's a static overload; non-unique
4171 cases will be handled by instantiate_type. But we need to
4172 handle this case here to allow casts on the resulting PMF.
4173 We could defer this in non-MS mode, but it's easier to give
4174 a useful error here. */
4176 /* Inside constant member functions, the `this' pointer
4177 contains an extra const qualifier. TYPE_MAIN_VARIANT
4178 is used here to remove this const from the diagnostics
4179 and the created OFFSET_REF. */
4180 tree base = TYPE_MAIN_VARIANT (TREE_TYPE (TREE_OPERAND (arg, 0)));
4181 tree fn = get_first_fn (TREE_OPERAND (arg, 1));
4183 if (! flag_ms_extensions)
4185 tree name = DECL_NAME (fn);
4186 if (current_class_type
4187 && TREE_OPERAND (arg, 0) == current_class_ref)
4188 /* An expression like &memfn. */
4189 pedwarn ("ISO C++ forbids taking the address of an unqualified"
4190 " or parenthesized non-static member function to form"
4191 " a pointer to member function. Say %<&%T::%D%>",
4192 base, name);
4193 else
4194 pedwarn ("ISO C++ forbids taking the address of a bound member"
4195 " function to form a pointer to member function."
4196 " Say %<&%T::%D%>",
4197 base, name);
4199 arg = build_offset_ref (base, fn, /*address_p=*/true);
4202 offset_ref:
4203 if (type_unknown_p (arg))
4204 return build1 (ADDR_EXPR, unknown_type_node, arg);
4206 /* Handle complex lvalues (when permitted)
4207 by reduction to simpler cases. */
4208 val = unary_complex_lvalue (code, arg);
4209 if (val != 0)
4210 return val;
4212 switch (TREE_CODE (arg))
4214 case NOP_EXPR:
4215 case CONVERT_EXPR:
4216 case FLOAT_EXPR:
4217 case FIX_TRUNC_EXPR:
4218 case FIX_FLOOR_EXPR:
4219 case FIX_ROUND_EXPR:
4220 case FIX_CEIL_EXPR:
4221 if (! lvalue_p (arg) && pedantic)
4222 pedwarn ("ISO C++ forbids taking the address of a cast to a non-lvalue expression");
4223 break;
4225 case OVERLOAD:
4226 arg = OVL_CURRENT (arg);
4227 break;
4229 case OFFSET_REF:
4230 /* Turn a reference to a non-static data member into a
4231 pointer-to-member. */
4233 tree type;
4234 tree t;
4236 if (!PTRMEM_OK_P (arg))
4237 return build_unary_op (code, arg, 0);
4239 t = TREE_OPERAND (arg, 1);
4240 if (TREE_CODE (TREE_TYPE (t)) == REFERENCE_TYPE)
4242 error ("cannot create pointer to reference member %qD", t);
4243 return error_mark_node;
4246 type = build_ptrmem_type (context_for_name_lookup (t),
4247 TREE_TYPE (t));
4248 t = make_ptrmem_cst (type, TREE_OPERAND (arg, 1));
4249 return t;
4252 default:
4253 break;
4256 /* Anything not already handled and not a true memory reference
4257 is an error. */
4258 if (TREE_CODE (argtype) != FUNCTION_TYPE
4259 && TREE_CODE (argtype) != METHOD_TYPE
4260 && TREE_CODE (arg) != OFFSET_REF
4261 /* Permit users to take the address of a compound-literal
4262 with sufficient simple elements. */
4263 && !(COMPOUND_LITERAL_P (arg) && TREE_STATIC (arg))
4264 && !lvalue_or_else (arg, lv_addressof))
4265 return error_mark_node;
4267 if (argtype != error_mark_node)
4268 argtype = build_pointer_type (argtype);
4270 /* In a template, we are processing a non-dependent expression
4271 so we can just form an ADDR_EXPR with the correct type. */
4272 if (processing_template_decl)
4274 val = build_address (arg);
4275 if (TREE_CODE (arg) == OFFSET_REF)
4276 PTRMEM_OK_P (val) = PTRMEM_OK_P (arg);
4277 return val;
4280 /* If the user has taken the address of the compound literal,
4281 create a variable to contain the value of the literal and
4282 then return the address of that variable. */
4283 if (COMPOUND_LITERAL_P (arg))
4285 tree var;
4286 gcc_assert (TREE_STATIC (arg));
4287 var = create_temporary_var (TREE_TYPE (arg));
4288 TREE_STATIC (var) = 1;
4289 set_compound_literal_name (var);
4290 initialize_artificial_var (var, arg);
4291 arg = pushdecl (var);
4292 /* Since each compound literal is unique, pushdecl should
4293 never find a pre-existing variable with the same
4294 name. */
4295 gcc_assert (arg == var);
4298 if (TREE_CODE (arg) != COMPONENT_REF)
4300 val = build_address (arg);
4301 if (TREE_CODE (arg) == OFFSET_REF)
4302 PTRMEM_OK_P (val) = PTRMEM_OK_P (arg);
4304 else if (TREE_CODE (TREE_OPERAND (arg, 1)) == BASELINK)
4306 tree fn = BASELINK_FUNCTIONS (TREE_OPERAND (arg, 1));
4308 /* We can only get here with a single static member
4309 function. */
4310 gcc_assert (TREE_CODE (fn) == FUNCTION_DECL
4311 && DECL_STATIC_FUNCTION_P (fn));
4312 mark_used (fn);
4313 val = build_address (fn);
4314 if (TREE_SIDE_EFFECTS (TREE_OPERAND (arg, 0)))
4315 /* Do not lose object's side effects. */
4316 val = build2 (COMPOUND_EXPR, TREE_TYPE (val),
4317 TREE_OPERAND (arg, 0), val);
4319 else if (DECL_C_BIT_FIELD (TREE_OPERAND (arg, 1)))
4321 error ("attempt to take address of bit-field structure member %qD",
4322 TREE_OPERAND (arg, 1));
4323 return error_mark_node;
4325 else
4327 tree object = TREE_OPERAND (arg, 0);
4328 tree field = TREE_OPERAND (arg, 1);
4329 gcc_assert (same_type_ignoring_top_level_qualifiers_p
4330 (TREE_TYPE (object), decl_type_context (field)));
4331 val = build_address (arg);
4334 if (TREE_CODE (argtype) == POINTER_TYPE
4335 && TREE_CODE (TREE_TYPE (argtype)) == METHOD_TYPE)
4337 build_ptrmemfunc_type (argtype);
4338 val = build_ptrmemfunc (argtype, val, 0,
4339 /*c_cast_p=*/false);
4342 return val;
4344 default:
4345 break;
4348 if (!errstring)
4350 if (argtype == 0)
4351 argtype = TREE_TYPE (arg);
4352 return fold_if_not_in_template (build1 (code, argtype, arg));
4355 error ("%s", errstring);
4356 return error_mark_node;
4359 /* Apply unary lvalue-demanding operator CODE to the expression ARG
4360 for certain kinds of expressions which are not really lvalues
4361 but which we can accept as lvalues.
4363 If ARG is not a kind of expression we can handle, return
4364 NULL_TREE. */
4366 tree
4367 unary_complex_lvalue (enum tree_code code, tree arg)
4369 /* Inside a template, making these kinds of adjustments is
4370 pointless; we are only concerned with the type of the
4371 expression. */
4372 if (processing_template_decl)
4373 return NULL_TREE;
4375 /* Handle (a, b) used as an "lvalue". */
4376 if (TREE_CODE (arg) == COMPOUND_EXPR)
4378 tree real_result = build_unary_op (code, TREE_OPERAND (arg, 1), 0);
4379 return build2 (COMPOUND_EXPR, TREE_TYPE (real_result),
4380 TREE_OPERAND (arg, 0), real_result);
4383 /* Handle (a ? b : c) used as an "lvalue". */
4384 if (TREE_CODE (arg) == COND_EXPR
4385 || TREE_CODE (arg) == MIN_EXPR || TREE_CODE (arg) == MAX_EXPR)
4386 return rationalize_conditional_expr (code, arg);
4388 /* Handle (a = b), (++a), and (--a) used as an "lvalue". */
4389 if (TREE_CODE (arg) == MODIFY_EXPR
4390 || TREE_CODE (arg) == PREINCREMENT_EXPR
4391 || TREE_CODE (arg) == PREDECREMENT_EXPR)
4393 tree lvalue = TREE_OPERAND (arg, 0);
4394 if (TREE_SIDE_EFFECTS (lvalue))
4396 lvalue = stabilize_reference (lvalue);
4397 arg = build2 (TREE_CODE (arg), TREE_TYPE (arg),
4398 lvalue, TREE_OPERAND (arg, 1));
4400 return unary_complex_lvalue
4401 (code, build2 (COMPOUND_EXPR, TREE_TYPE (lvalue), arg, lvalue));
4404 if (code != ADDR_EXPR)
4405 return NULL_TREE;
4407 /* Handle (a = b) used as an "lvalue" for `&'. */
4408 if (TREE_CODE (arg) == MODIFY_EXPR
4409 || TREE_CODE (arg) == INIT_EXPR)
4411 tree real_result = build_unary_op (code, TREE_OPERAND (arg, 0), 0);
4412 arg = build2 (COMPOUND_EXPR, TREE_TYPE (real_result),
4413 arg, real_result);
4414 TREE_NO_WARNING (arg) = 1;
4415 return arg;
4418 if (TREE_CODE (TREE_TYPE (arg)) == FUNCTION_TYPE
4419 || TREE_CODE (TREE_TYPE (arg)) == METHOD_TYPE
4420 || TREE_CODE (arg) == OFFSET_REF)
4421 return NULL_TREE;
4423 /* We permit compiler to make function calls returning
4424 objects of aggregate type look like lvalues. */
4426 tree targ = arg;
4428 if (TREE_CODE (targ) == SAVE_EXPR)
4429 targ = TREE_OPERAND (targ, 0);
4431 if (TREE_CODE (targ) == CALL_EXPR && IS_AGGR_TYPE (TREE_TYPE (targ)))
4433 if (TREE_CODE (arg) == SAVE_EXPR)
4434 targ = arg;
4435 else
4436 targ = build_cplus_new (TREE_TYPE (arg), arg);
4437 return build1 (ADDR_EXPR, build_pointer_type (TREE_TYPE (arg)), targ);
4440 if (TREE_CODE (arg) == SAVE_EXPR && TREE_CODE (targ) == INDIRECT_REF)
4441 return build3 (SAVE_EXPR, build_pointer_type (TREE_TYPE (arg)),
4442 TREE_OPERAND (targ, 0), current_function_decl, NULL);
4445 /* Don't let anything else be handled specially. */
4446 return NULL_TREE;
4449 /* Mark EXP saying that we need to be able to take the
4450 address of it; it should not be allocated in a register.
4451 Value is true if successful.
4453 C++: we do not allow `current_class_ptr' to be addressable. */
4455 bool
4456 cxx_mark_addressable (tree exp)
4458 tree x = exp;
4460 while (1)
4461 switch (TREE_CODE (x))
4463 case ADDR_EXPR:
4464 case COMPONENT_REF:
4465 case ARRAY_REF:
4466 case REALPART_EXPR:
4467 case IMAGPART_EXPR:
4468 x = TREE_OPERAND (x, 0);
4469 break;
4471 case PARM_DECL:
4472 if (x == current_class_ptr)
4474 error ("cannot take the address of %<this%>, which is an rvalue expression");
4475 TREE_ADDRESSABLE (x) = 1; /* so compiler doesn't die later. */
4476 return true;
4478 /* Fall through. */
4480 case VAR_DECL:
4481 /* Caller should not be trying to mark initialized
4482 constant fields addressable. */
4483 gcc_assert (DECL_LANG_SPECIFIC (x) == 0
4484 || DECL_IN_AGGR_P (x) == 0
4485 || TREE_STATIC (x)
4486 || DECL_EXTERNAL (x));
4487 /* Fall through. */
4489 case CONST_DECL:
4490 case RESULT_DECL:
4491 if (DECL_REGISTER (x) && !TREE_ADDRESSABLE (x)
4492 && !DECL_ARTIFICIAL (x))
4494 if (TREE_CODE (x) == VAR_DECL && DECL_HARD_REGISTER (x))
4496 error
4497 ("address of explicit register variable %qD requested", x);
4498 return false;
4500 else
4501 warning
4502 (OPT_Wextra, "address requested for %qD, which is declared %<register%>", x);
4504 TREE_ADDRESSABLE (x) = 1;
4505 return true;
4507 case FUNCTION_DECL:
4508 TREE_ADDRESSABLE (x) = 1;
4509 return true;
4511 case CONSTRUCTOR:
4512 TREE_ADDRESSABLE (x) = 1;
4513 return true;
4515 case TARGET_EXPR:
4516 TREE_ADDRESSABLE (x) = 1;
4517 cxx_mark_addressable (TREE_OPERAND (x, 0));
4518 return true;
4520 default:
4521 return true;
4525 /* Build and return a conditional expression IFEXP ? OP1 : OP2. */
4527 tree
4528 build_x_conditional_expr (tree ifexp, tree op1, tree op2)
4530 tree orig_ifexp = ifexp;
4531 tree orig_op1 = op1;
4532 tree orig_op2 = op2;
4533 tree expr;
4535 if (processing_template_decl)
4537 /* The standard says that the expression is type-dependent if
4538 IFEXP is type-dependent, even though the eventual type of the
4539 expression doesn't dependent on IFEXP. */
4540 if (type_dependent_expression_p (ifexp)
4541 /* As a GNU extension, the middle operand may be omitted. */
4542 || (op1 && type_dependent_expression_p (op1))
4543 || type_dependent_expression_p (op2))
4544 return build_min_nt (COND_EXPR, ifexp, op1, op2);
4545 ifexp = build_non_dependent_expr (ifexp);
4546 if (op1)
4547 op1 = build_non_dependent_expr (op1);
4548 op2 = build_non_dependent_expr (op2);
4551 expr = build_conditional_expr (ifexp, op1, op2);
4552 if (processing_template_decl && expr != error_mark_node)
4553 return build_min_non_dep (COND_EXPR, expr,
4554 orig_ifexp, orig_op1, orig_op2);
4555 return expr;
4558 /* Given a list of expressions, return a compound expression
4559 that performs them all and returns the value of the last of them. */
4561 tree build_x_compound_expr_from_list (tree list, const char *msg)
4563 tree expr = TREE_VALUE (list);
4565 if (TREE_CHAIN (list))
4567 if (msg)
4568 pedwarn ("%s expression list treated as compound expression", msg);
4570 for (list = TREE_CHAIN (list); list; list = TREE_CHAIN (list))
4571 expr = build_x_compound_expr (expr, TREE_VALUE (list));
4574 return expr;
4577 /* Handle overloading of the ',' operator when needed. */
4579 tree
4580 build_x_compound_expr (tree op1, tree op2)
4582 tree result;
4583 tree orig_op1 = op1;
4584 tree orig_op2 = op2;
4586 if (processing_template_decl)
4588 if (type_dependent_expression_p (op1)
4589 || type_dependent_expression_p (op2))
4590 return build_min_nt (COMPOUND_EXPR, op1, op2);
4591 op1 = build_non_dependent_expr (op1);
4592 op2 = build_non_dependent_expr (op2);
4595 result = build_new_op (COMPOUND_EXPR, LOOKUP_NORMAL, op1, op2, NULL_TREE,
4596 /*overloaded_p=*/NULL);
4597 if (!result)
4598 result = build_compound_expr (op1, op2);
4600 if (processing_template_decl && result != error_mark_node)
4601 return build_min_non_dep (COMPOUND_EXPR, result, orig_op1, orig_op2);
4603 return result;
4606 /* Build a compound expression. */
4608 tree
4609 build_compound_expr (tree lhs, tree rhs)
4611 lhs = convert_to_void (lhs, "left-hand operand of comma");
4613 if (lhs == error_mark_node || rhs == error_mark_node)
4614 return error_mark_node;
4616 if (TREE_CODE (rhs) == TARGET_EXPR)
4618 /* If the rhs is a TARGET_EXPR, then build the compound
4619 expression inside the target_expr's initializer. This
4620 helps the compiler to eliminate unnecessary temporaries. */
4621 tree init = TREE_OPERAND (rhs, 1);
4623 init = build2 (COMPOUND_EXPR, TREE_TYPE (init), lhs, init);
4624 TREE_OPERAND (rhs, 1) = init;
4626 return rhs;
4629 return build2 (COMPOUND_EXPR, TREE_TYPE (rhs), lhs, rhs);
4632 /* Issue a diagnostic message if casting from SRC_TYPE to DEST_TYPE
4633 casts away constness. DIAG_FN gives the function to call if we
4634 need to issue a diagnostic; if it is NULL, no diagnostic will be
4635 issued. DESCRIPTION explains what operation is taking place. */
4637 static void
4638 check_for_casting_away_constness (tree src_type, tree dest_type,
4639 void (*diag_fn)(const char *, ...) ATTRIBUTE_GCC_CXXDIAG(1,2),
4640 const char *description)
4642 if (diag_fn && casts_away_constness (src_type, dest_type))
4643 diag_fn ("%s from type %qT to type %qT casts away constness",
4644 description, src_type, dest_type);
4647 /* Convert EXPR (an expression with pointer-to-member type) to TYPE
4648 (another pointer-to-member type in the same hierarchy) and return
4649 the converted expression. If ALLOW_INVERSE_P is permitted, a
4650 pointer-to-derived may be converted to pointer-to-base; otherwise,
4651 only the other direction is permitted. If C_CAST_P is true, this
4652 conversion is taking place as part of a C-style cast. */
4654 tree
4655 convert_ptrmem (tree type, tree expr, bool allow_inverse_p,
4656 bool c_cast_p)
4658 if (TYPE_PTRMEM_P (type))
4660 tree delta;
4662 if (TREE_CODE (expr) == PTRMEM_CST)
4663 expr = cplus_expand_constant (expr);
4664 delta = get_delta_difference (TYPE_PTRMEM_CLASS_TYPE (TREE_TYPE (expr)),
4665 TYPE_PTRMEM_CLASS_TYPE (type),
4666 allow_inverse_p,
4667 c_cast_p);
4668 if (!integer_zerop (delta))
4669 expr = cp_build_binary_op (PLUS_EXPR,
4670 build_nop (ptrdiff_type_node, expr),
4671 delta);
4672 return build_nop (type, expr);
4674 else
4675 return build_ptrmemfunc (TYPE_PTRMEMFUNC_FN_TYPE (type), expr,
4676 allow_inverse_p, c_cast_p);
4679 /* If EXPR is an INTEGER_CST and ORIG is an arithmetic constant, return
4680 a version of EXPR that has TREE_OVERFLOW and/or TREE_CONSTANT_OVERFLOW
4681 set iff they are set in ORIG. Otherwise, return EXPR unchanged. */
4683 static tree
4684 ignore_overflows (tree expr, tree orig)
4686 if (TREE_CODE (expr) == INTEGER_CST
4687 && CONSTANT_CLASS_P (orig)
4688 && TREE_CODE (orig) != STRING_CST
4689 && (TREE_OVERFLOW (expr) != TREE_OVERFLOW (orig)
4690 || TREE_CONSTANT_OVERFLOW (expr)
4691 != TREE_CONSTANT_OVERFLOW (orig)))
4693 if (!TREE_OVERFLOW (orig) && !TREE_CONSTANT_OVERFLOW (orig))
4694 /* Ensure constant sharing. */
4695 expr = build_int_cst_wide (TREE_TYPE (expr),
4696 TREE_INT_CST_LOW (expr),
4697 TREE_INT_CST_HIGH (expr));
4698 else
4700 /* Avoid clobbering a shared constant. */
4701 expr = copy_node (expr);
4702 TREE_OVERFLOW (expr) = TREE_OVERFLOW (orig);
4703 TREE_CONSTANT_OVERFLOW (expr)
4704 = TREE_CONSTANT_OVERFLOW (orig);
4707 return expr;
4710 /* Perform a static_cast from EXPR to TYPE. When C_CAST_P is true,
4711 this static_cast is being attempted as one of the possible casts
4712 allowed by a C-style cast. (In that case, accessibility of base
4713 classes is not considered, and it is OK to cast away
4714 constness.) Return the result of the cast. *VALID_P is set to
4715 indicate whether or not the cast was valid. */
4717 static tree
4718 build_static_cast_1 (tree type, tree expr, bool c_cast_p,
4719 bool *valid_p)
4721 tree intype;
4722 tree result;
4723 tree orig;
4724 void (*diag_fn)(const char*, ...) ATTRIBUTE_GCC_CXXDIAG(1,2);
4725 const char *desc;
4727 /* Assume the cast is valid. */
4728 *valid_p = true;
4730 intype = TREE_TYPE (expr);
4732 /* Determine what to do when casting away constness. */
4733 if (c_cast_p)
4735 /* C-style casts are allowed to cast away constness. With
4736 WARN_CAST_QUAL, we still want to issue a warning. */
4737 diag_fn = warn_cast_qual ? warning0 : NULL;
4738 desc = "cast";
4740 else
4742 /* A static_cast may not cast away constness. */
4743 diag_fn = error;
4744 desc = "static_cast";
4747 /* [expr.static.cast]
4749 An lvalue of type "cv1 B", where B is a class type, can be cast
4750 to type "reference to cv2 D", where D is a class derived (clause
4751 _class.derived_) from B, if a valid standard conversion from
4752 "pointer to D" to "pointer to B" exists (_conv.ptr_), cv2 is the
4753 same cv-qualification as, or greater cv-qualification than, cv1,
4754 and B is not a virtual base class of D. */
4755 /* We check this case before checking the validity of "TYPE t =
4756 EXPR;" below because for this case:
4758 struct B {};
4759 struct D : public B { D(const B&); };
4760 extern B& b;
4761 void f() { static_cast<const D&>(b); }
4763 we want to avoid constructing a new D. The standard is not
4764 completely clear about this issue, but our interpretation is
4765 consistent with other compilers. */
4766 if (TREE_CODE (type) == REFERENCE_TYPE
4767 && CLASS_TYPE_P (TREE_TYPE (type))
4768 && CLASS_TYPE_P (intype)
4769 && real_lvalue_p (expr)
4770 && DERIVED_FROM_P (intype, TREE_TYPE (type))
4771 && can_convert (build_pointer_type (TYPE_MAIN_VARIANT (intype)),
4772 build_pointer_type (TYPE_MAIN_VARIANT
4773 (TREE_TYPE (type))))
4774 && (c_cast_p
4775 || at_least_as_qualified_p (TREE_TYPE (type), intype)))
4777 tree base;
4779 /* There is a standard conversion from "D*" to "B*" even if "B"
4780 is ambiguous or inaccessible. If this is really a
4781 static_cast, then we check both for inaccessibility and
4782 ambiguity. However, if this is a static_cast being performed
4783 because the user wrote a C-style cast, then accessibility is
4784 not considered. */
4785 base = lookup_base (TREE_TYPE (type), intype,
4786 c_cast_p ? ba_unique : ba_check,
4787 NULL);
4789 /* Convert from "B*" to "D*". This function will check that "B"
4790 is not a virtual base of "D". */
4791 expr = build_base_path (MINUS_EXPR, build_address (expr),
4792 base, /*nonnull=*/false);
4793 /* Convert the pointer to a reference -- but then remember that
4794 there are no expressions with reference type in C++. */
4795 return convert_from_reference (build_nop (type, expr));
4798 orig = expr;
4800 /* [expr.static.cast]
4802 An expression e can be explicitly converted to a type T using a
4803 static_cast of the form static_cast<T>(e) if the declaration T
4804 t(e);" is well-formed, for some invented temporary variable
4805 t. */
4806 result = perform_direct_initialization_if_possible (type, expr,
4807 c_cast_p);
4808 if (result)
4810 result = convert_from_reference (result);
4812 /* Ignore any integer overflow caused by the cast. */
4813 result = ignore_overflows (result, orig);
4815 /* [expr.static.cast]
4817 If T is a reference type, the result is an lvalue; otherwise,
4818 the result is an rvalue. */
4819 if (TREE_CODE (type) != REFERENCE_TYPE)
4820 result = rvalue (result);
4821 return result;
4824 /* [expr.static.cast]
4826 Any expression can be explicitly converted to type cv void. */
4827 if (TREE_CODE (type) == VOID_TYPE)
4828 return convert_to_void (expr, /*implicit=*/NULL);
4830 /* [expr.static.cast]
4832 The inverse of any standard conversion sequence (clause _conv_),
4833 other than the lvalue-to-rvalue (_conv.lval_), array-to-pointer
4834 (_conv.array_), function-to-pointer (_conv.func_), and boolean
4835 (_conv.bool_) conversions, can be performed explicitly using
4836 static_cast subject to the restriction that the explicit
4837 conversion does not cast away constness (_expr.const.cast_), and
4838 the following additional rules for specific cases: */
4839 /* For reference, the conversions not excluded are: integral
4840 promotions, floating point promotion, integral conversions,
4841 floating point conversions, floating-integral conversions,
4842 pointer conversions, and pointer to member conversions. */
4843 /* DR 128
4845 A value of integral _or enumeration_ type can be explicitly
4846 converted to an enumeration type. */
4847 /* The effect of all that is that any conversion between any two
4848 types which are integral, floating, or enumeration types can be
4849 performed. */
4850 if ((INTEGRAL_TYPE_P (type) || SCALAR_FLOAT_TYPE_P (type))
4851 && (INTEGRAL_TYPE_P (intype) || SCALAR_FLOAT_TYPE_P (intype)))
4853 expr = ocp_convert (type, expr, CONV_C_CAST, LOOKUP_NORMAL);
4855 /* Ignore any integer overflow caused by the cast. */
4856 expr = ignore_overflows (expr, orig);
4857 return expr;
4860 if (TYPE_PTR_P (type) && TYPE_PTR_P (intype)
4861 && CLASS_TYPE_P (TREE_TYPE (type))
4862 && CLASS_TYPE_P (TREE_TYPE (intype))
4863 && can_convert (build_pointer_type (TYPE_MAIN_VARIANT
4864 (TREE_TYPE (intype))),
4865 build_pointer_type (TYPE_MAIN_VARIANT
4866 (TREE_TYPE (type)))))
4868 tree base;
4870 if (!c_cast_p)
4871 check_for_casting_away_constness (intype, type, diag_fn, desc);
4872 base = lookup_base (TREE_TYPE (type), TREE_TYPE (intype),
4873 c_cast_p ? ba_unique : ba_check,
4874 NULL);
4875 return build_base_path (MINUS_EXPR, expr, base, /*nonnull=*/false);
4878 if ((TYPE_PTRMEM_P (type) && TYPE_PTRMEM_P (intype))
4879 || (TYPE_PTRMEMFUNC_P (type) && TYPE_PTRMEMFUNC_P (intype)))
4881 tree c1;
4882 tree c2;
4883 tree t1;
4884 tree t2;
4886 c1 = TYPE_PTRMEM_CLASS_TYPE (intype);
4887 c2 = TYPE_PTRMEM_CLASS_TYPE (type);
4889 if (TYPE_PTRMEM_P (type))
4891 t1 = (build_ptrmem_type
4892 (c1,
4893 TYPE_MAIN_VARIANT (TYPE_PTRMEM_POINTED_TO_TYPE (intype))));
4894 t2 = (build_ptrmem_type
4895 (c2,
4896 TYPE_MAIN_VARIANT (TYPE_PTRMEM_POINTED_TO_TYPE (type))));
4898 else
4900 t1 = intype;
4901 t2 = type;
4903 if (can_convert (t1, t2))
4905 if (!c_cast_p)
4906 check_for_casting_away_constness (intype, type, diag_fn,
4907 desc);
4908 return convert_ptrmem (type, expr, /*allow_inverse_p=*/1,
4909 c_cast_p);
4913 /* [expr.static.cast]
4915 An rvalue of type "pointer to cv void" can be explicitly
4916 converted to a pointer to object type. A value of type pointer
4917 to object converted to "pointer to cv void" and back to the
4918 original pointer type will have its original value. */
4919 if (TREE_CODE (intype) == POINTER_TYPE
4920 && VOID_TYPE_P (TREE_TYPE (intype))
4921 && TYPE_PTROB_P (type))
4923 if (!c_cast_p)
4924 check_for_casting_away_constness (intype, type, diag_fn, desc);
4925 return build_nop (type, expr);
4928 *valid_p = false;
4929 return error_mark_node;
4932 /* Return an expression representing static_cast<TYPE>(EXPR). */
4934 tree
4935 build_static_cast (tree type, tree expr)
4937 tree result;
4938 bool valid_p;
4940 if (type == error_mark_node || expr == error_mark_node)
4941 return error_mark_node;
4943 if (processing_template_decl)
4945 expr = build_min (STATIC_CAST_EXPR, type, expr);
4946 /* We don't know if it will or will not have side effects. */
4947 TREE_SIDE_EFFECTS (expr) = 1;
4948 return convert_from_reference (expr);
4951 /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
4952 Strip such NOP_EXPRs if VALUE is being used in non-lvalue context. */
4953 if (TREE_CODE (type) != REFERENCE_TYPE
4954 && TREE_CODE (expr) == NOP_EXPR
4955 && TREE_TYPE (expr) == TREE_TYPE (TREE_OPERAND (expr, 0)))
4956 expr = TREE_OPERAND (expr, 0);
4958 result = build_static_cast_1 (type, expr, /*c_cast_p=*/false, &valid_p);
4959 if (valid_p)
4960 return result;
4962 error ("invalid static_cast from type %qT to type %qT",
4963 TREE_TYPE (expr), type);
4964 return error_mark_node;
4967 /* EXPR is an expression with member function or pointer-to-member
4968 function type. TYPE is a pointer type. Converting EXPR to TYPE is
4969 not permitted by ISO C++, but we accept it in some modes. If we
4970 are not in one of those modes, issue a diagnostic. Return the
4971 converted expression. */
4973 tree
4974 convert_member_func_to_ptr (tree type, tree expr)
4976 tree intype;
4977 tree decl;
4979 intype = TREE_TYPE (expr);
4980 gcc_assert (TYPE_PTRMEMFUNC_P (intype)
4981 || TREE_CODE (intype) == METHOD_TYPE);
4983 if (pedantic || warn_pmf2ptr)
4984 pedwarn ("converting from %qT to %qT", intype, type);
4986 if (TREE_CODE (intype) == METHOD_TYPE)
4987 expr = build_addr_func (expr);
4988 else if (TREE_CODE (expr) == PTRMEM_CST)
4989 expr = build_address (PTRMEM_CST_MEMBER (expr));
4990 else
4992 decl = maybe_dummy_object (TYPE_PTRMEM_CLASS_TYPE (intype), 0);
4993 decl = build_address (decl);
4994 expr = get_member_function_from_ptrfunc (&decl, expr);
4997 return build_nop (type, expr);
5000 /* Return a representation for a reinterpret_cast from EXPR to TYPE.
5001 If C_CAST_P is true, this reinterpret cast is being done as part of
5002 a C-style cast. If VALID_P is non-NULL, *VALID_P is set to
5003 indicate whether or not reinterpret_cast was valid. */
5005 static tree
5006 build_reinterpret_cast_1 (tree type, tree expr, bool c_cast_p,
5007 bool *valid_p)
5009 tree intype;
5011 /* Assume the cast is invalid. */
5012 if (valid_p)
5013 *valid_p = true;
5015 if (type == error_mark_node || error_operand_p (expr))
5016 return error_mark_node;
5018 intype = TREE_TYPE (expr);
5020 /* [expr.reinterpret.cast]
5021 An lvalue expression of type T1 can be cast to the type
5022 "reference to T2" if an expression of type "pointer to T1" can be
5023 explicitly converted to the type "pointer to T2" using a
5024 reinterpret_cast. */
5025 if (TREE_CODE (type) == REFERENCE_TYPE)
5027 if (! real_lvalue_p (expr))
5029 error ("invalid cast of an rvalue expression of type "
5030 "%qT to type %qT",
5031 intype, type);
5032 return error_mark_node;
5035 /* Warn about a reinterpret_cast from "A*" to "B&" if "A" and
5036 "B" are related class types; the reinterpret_cast does not
5037 adjust the pointer. */
5038 if (TYPE_PTR_P (intype)
5039 && (comptypes (TREE_TYPE (intype), TREE_TYPE (type),
5040 COMPARE_BASE | COMPARE_DERIVED)))
5041 warning (0, "casting %qT to %qT does not dereference pointer",
5042 intype, type);
5044 expr = build_unary_op (ADDR_EXPR, expr, 0);
5045 if (expr != error_mark_node)
5046 expr = build_reinterpret_cast_1
5047 (build_pointer_type (TREE_TYPE (type)), expr, c_cast_p,
5048 valid_p);
5049 if (expr != error_mark_node)
5050 expr = build_indirect_ref (expr, 0);
5051 return expr;
5054 /* As a G++ extension, we consider conversions from member
5055 functions, and pointers to member functions to
5056 pointer-to-function and pointer-to-void types. If
5057 -Wno-pmf-conversions has not been specified,
5058 convert_member_func_to_ptr will issue an error message. */
5059 if ((TYPE_PTRMEMFUNC_P (intype)
5060 || TREE_CODE (intype) == METHOD_TYPE)
5061 && TYPE_PTR_P (type)
5062 && (TREE_CODE (TREE_TYPE (type)) == FUNCTION_TYPE
5063 || VOID_TYPE_P (TREE_TYPE (type))))
5064 return convert_member_func_to_ptr (type, expr);
5066 /* If the cast is not to a reference type, the lvalue-to-rvalue,
5067 array-to-pointer, and function-to-pointer conversions are
5068 performed. */
5069 expr = decay_conversion (expr);
5071 /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
5072 Strip such NOP_EXPRs if VALUE is being used in non-lvalue context. */
5073 if (TREE_CODE (expr) == NOP_EXPR
5074 && TREE_TYPE (expr) == TREE_TYPE (TREE_OPERAND (expr, 0)))
5075 expr = TREE_OPERAND (expr, 0);
5077 if (error_operand_p (expr))
5078 return error_mark_node;
5080 intype = TREE_TYPE (expr);
5082 /* [expr.reinterpret.cast]
5083 A pointer can be converted to any integral type large enough to
5084 hold it. */
5085 if (CP_INTEGRAL_TYPE_P (type) && TYPE_PTR_P (intype))
5087 if (TYPE_PRECISION (type) < TYPE_PRECISION (intype))
5088 pedwarn ("cast from %qT to %qT loses precision",
5089 intype, type);
5091 /* [expr.reinterpret.cast]
5092 A value of integral or enumeration type can be explicitly
5093 converted to a pointer. */
5094 else if (TYPE_PTR_P (type) && INTEGRAL_OR_ENUMERATION_TYPE_P (intype))
5095 /* OK */
5097 else if ((TYPE_PTRFN_P (type) && TYPE_PTRFN_P (intype))
5098 || (TYPE_PTRMEMFUNC_P (type) && TYPE_PTRMEMFUNC_P (intype)))
5099 return fold_if_not_in_template (build_nop (type, expr));
5100 else if ((TYPE_PTRMEM_P (type) && TYPE_PTRMEM_P (intype))
5101 || (TYPE_PTROBV_P (type) && TYPE_PTROBV_P (intype)))
5103 tree sexpr = expr;
5105 if (!c_cast_p)
5106 check_for_casting_away_constness (intype, type, error,
5107 "reinterpret_cast");
5108 /* Warn about possible alignment problems. */
5109 if (STRICT_ALIGNMENT && warn_cast_align
5110 && !VOID_TYPE_P (type)
5111 && TREE_CODE (TREE_TYPE (intype)) != FUNCTION_TYPE
5112 && COMPLETE_TYPE_P (TREE_TYPE (type))
5113 && COMPLETE_TYPE_P (TREE_TYPE (intype))
5114 && TYPE_ALIGN (TREE_TYPE (type)) > TYPE_ALIGN (TREE_TYPE (intype)))
5115 warning (0, "cast from %qT to %qT increases required alignment of "
5116 "target type",
5117 intype, type);
5119 /* We need to strip nops here, because the frontend likes to
5120 create (int *)&a for array-to-pointer decay, instead of &a[0]. */
5121 STRIP_NOPS (sexpr);
5122 strict_aliasing_warning (intype, type, sexpr);
5124 return fold_if_not_in_template (build_nop (type, expr));
5126 else if ((TYPE_PTRFN_P (type) && TYPE_PTROBV_P (intype))
5127 || (TYPE_PTRFN_P (intype) && TYPE_PTROBV_P (type)))
5129 if (pedantic)
5130 /* Only issue a warning, as we have always supported this
5131 where possible, and it is necessary in some cases. DR 195
5132 addresses this issue, but as of 2004/10/26 is still in
5133 drafting. */
5134 warning (0, "ISO C++ forbids casting between pointer-to-function and pointer-to-object");
5135 return fold_if_not_in_template (build_nop (type, expr));
5137 else if (TREE_CODE (type) == VECTOR_TYPE)
5138 return fold_if_not_in_template (convert_to_vector (type, expr));
5139 else if (TREE_CODE (intype) == VECTOR_TYPE)
5140 return fold_if_not_in_template (convert_to_integer (type, expr));
5141 else
5143 if (valid_p)
5144 *valid_p = false;
5145 error ("invalid cast from type %qT to type %qT", intype, type);
5146 return error_mark_node;
5149 return cp_convert (type, expr);
5152 tree
5153 build_reinterpret_cast (tree type, tree expr)
5155 if (type == error_mark_node || expr == error_mark_node)
5156 return error_mark_node;
5158 if (processing_template_decl)
5160 tree t = build_min (REINTERPRET_CAST_EXPR, type, expr);
5162 if (!TREE_SIDE_EFFECTS (t)
5163 && type_dependent_expression_p (expr))
5164 /* There might turn out to be side effects inside expr. */
5165 TREE_SIDE_EFFECTS (t) = 1;
5166 return convert_from_reference (t);
5169 return build_reinterpret_cast_1 (type, expr, /*c_cast_p=*/false,
5170 /*valid_p=*/NULL);
5173 /* Perform a const_cast from EXPR to TYPE. If the cast is valid,
5174 return an appropriate expression. Otherwise, return
5175 error_mark_node. If the cast is not valid, and COMPLAIN is true,
5176 then a diagnostic will be issued. If VALID_P is non-NULL, we are
5177 performing a C-style cast, its value upon return will indicate
5178 whether or not the conversion succeeded. */
5180 static tree
5181 build_const_cast_1 (tree dst_type, tree expr, bool complain,
5182 bool *valid_p)
5184 tree src_type;
5185 tree reference_type;
5187 /* Callers are responsible for handling error_mark_node as a
5188 destination type. */
5189 gcc_assert (dst_type != error_mark_node);
5190 /* In a template, callers should be building syntactic
5191 representations of casts, not using this machinery. */
5192 gcc_assert (!processing_template_decl);
5194 /* Assume the conversion is invalid. */
5195 if (valid_p)
5196 *valid_p = false;
5198 if (!POINTER_TYPE_P (dst_type) && !TYPE_PTRMEM_P (dst_type))
5200 if (complain)
5201 error ("invalid use of const_cast with type %qT, "
5202 "which is not a pointer, "
5203 "reference, nor a pointer-to-data-member type", dst_type);
5204 return error_mark_node;
5207 if (TREE_CODE (TREE_TYPE (dst_type)) == FUNCTION_TYPE)
5209 if (complain)
5210 error ("invalid use of const_cast with type %qT, which is a pointer "
5211 "or reference to a function type", dst_type);
5212 return error_mark_node;
5215 src_type = TREE_TYPE (expr);
5216 /* Expressions do not really have reference types. */
5217 if (TREE_CODE (src_type) == REFERENCE_TYPE)
5218 src_type = TREE_TYPE (src_type);
5220 /* [expr.const.cast]
5222 An lvalue of type T1 can be explicitly converted to an lvalue of
5223 type T2 using the cast const_cast<T2&> (where T1 and T2 are object
5224 types) if a pointer to T1 can be explicitly converted to the type
5225 pointer to T2 using a const_cast. */
5226 if (TREE_CODE (dst_type) == REFERENCE_TYPE)
5228 reference_type = dst_type;
5229 if (! real_lvalue_p (expr))
5231 if (complain)
5232 error ("invalid const_cast of an rvalue of type %qT to type %qT",
5233 src_type, dst_type);
5234 return error_mark_node;
5236 dst_type = build_pointer_type (TREE_TYPE (dst_type));
5237 src_type = build_pointer_type (src_type);
5239 else
5241 reference_type = NULL_TREE;
5242 /* If the destination type is not a reference type, the
5243 lvalue-to-rvalue, array-to-pointer, and function-to-pointer
5244 conversions are performed. */
5245 src_type = type_decays_to (src_type);
5246 if (src_type == error_mark_node)
5247 return error_mark_node;
5250 if ((TYPE_PTR_P (src_type) || TYPE_PTRMEM_P (src_type))
5251 && comp_ptr_ttypes_const (dst_type, src_type))
5253 if (valid_p)
5255 *valid_p = true;
5256 /* This cast is actually a C-style cast. Issue a warning if
5257 the user is making a potentially unsafe cast. */
5258 if (warn_cast_qual)
5259 check_for_casting_away_constness (src_type, dst_type,
5260 warning0,
5261 "cast");
5263 if (reference_type)
5265 expr = build_unary_op (ADDR_EXPR, expr, 0);
5266 expr = build_nop (reference_type, expr);
5267 return convert_from_reference (expr);
5269 else
5271 expr = decay_conversion (expr);
5272 /* build_c_cast puts on a NOP_EXPR to make the result not an
5273 lvalue. Strip such NOP_EXPRs if VALUE is being used in
5274 non-lvalue context. */
5275 if (TREE_CODE (expr) == NOP_EXPR
5276 && TREE_TYPE (expr) == TREE_TYPE (TREE_OPERAND (expr, 0)))
5277 expr = TREE_OPERAND (expr, 0);
5278 return build_nop (dst_type, expr);
5282 if (complain)
5283 error ("invalid const_cast from type %qT to type %qT",
5284 src_type, dst_type);
5285 return error_mark_node;
5288 tree
5289 build_const_cast (tree type, tree expr)
5291 if (type == error_mark_node || error_operand_p (expr))
5292 return error_mark_node;
5294 if (processing_template_decl)
5296 tree t = build_min (CONST_CAST_EXPR, type, expr);
5298 if (!TREE_SIDE_EFFECTS (t)
5299 && type_dependent_expression_p (expr))
5300 /* There might turn out to be side effects inside expr. */
5301 TREE_SIDE_EFFECTS (t) = 1;
5302 return convert_from_reference (t);
5305 return build_const_cast_1 (type, expr, /*complain=*/true,
5306 /*valid_p=*/NULL);
5309 /* Build an expression representing an explicit C-style cast to type
5310 TYPE of expression EXPR. */
5312 tree
5313 build_c_cast (tree type, tree expr)
5315 tree value = expr;
5316 tree result;
5317 bool valid_p;
5319 if (type == error_mark_node || error_operand_p (expr))
5320 return error_mark_node;
5322 if (processing_template_decl)
5324 tree t = build_min (CAST_EXPR, type,
5325 tree_cons (NULL_TREE, value, NULL_TREE));
5326 /* We don't know if it will or will not have side effects. */
5327 TREE_SIDE_EFFECTS (t) = 1;
5328 return convert_from_reference (t);
5331 /* Casts to a (pointer to a) specific ObjC class (or 'id' or
5332 'Class') should always be retained, because this information aids
5333 in method lookup. */
5334 if (objc_is_object_ptr (type)
5335 && objc_is_object_ptr (TREE_TYPE (expr)))
5336 return build_nop (type, expr);
5338 /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
5339 Strip such NOP_EXPRs if VALUE is being used in non-lvalue context. */
5340 if (TREE_CODE (type) != REFERENCE_TYPE
5341 && TREE_CODE (value) == NOP_EXPR
5342 && TREE_TYPE (value) == TREE_TYPE (TREE_OPERAND (value, 0)))
5343 value = TREE_OPERAND (value, 0);
5345 if (TREE_CODE (type) == ARRAY_TYPE)
5347 /* Allow casting from T1* to T2[] because Cfront allows it.
5348 NIHCL uses it. It is not valid ISO C++ however. */
5349 if (TREE_CODE (TREE_TYPE (expr)) == POINTER_TYPE)
5351 pedwarn ("ISO C++ forbids casting to an array type %qT", type);
5352 type = build_pointer_type (TREE_TYPE (type));
5354 else
5356 error ("ISO C++ forbids casting to an array type %qT", type);
5357 return error_mark_node;
5361 if (TREE_CODE (type) == FUNCTION_TYPE
5362 || TREE_CODE (type) == METHOD_TYPE)
5364 error ("invalid cast to function type %qT", type);
5365 return error_mark_node;
5368 /* A C-style cast can be a const_cast. */
5369 result = build_const_cast_1 (type, value, /*complain=*/false,
5370 &valid_p);
5371 if (valid_p)
5372 return result;
5374 /* Or a static cast. */
5375 result = build_static_cast_1 (type, value, /*c_cast_p=*/true,
5376 &valid_p);
5377 /* Or a reinterpret_cast. */
5378 if (!valid_p)
5379 result = build_reinterpret_cast_1 (type, value, /*c_cast_p=*/true,
5380 &valid_p);
5381 /* The static_cast or reinterpret_cast may be followed by a
5382 const_cast. */
5383 if (valid_p
5384 /* A valid cast may result in errors if, for example, a
5385 conversion to am ambiguous base class is required. */
5386 && !error_operand_p (result))
5388 tree result_type;
5390 /* Non-class rvalues always have cv-unqualified type. */
5391 if (!CLASS_TYPE_P (type))
5392 type = TYPE_MAIN_VARIANT (type);
5393 result_type = TREE_TYPE (result);
5394 if (!CLASS_TYPE_P (result_type))
5395 result_type = TYPE_MAIN_VARIANT (result_type);
5396 /* If the type of RESULT does not match TYPE, perform a
5397 const_cast to make it match. If the static_cast or
5398 reinterpret_cast succeeded, we will differ by at most
5399 cv-qualification, so the follow-on const_cast is guaranteed
5400 to succeed. */
5401 if (!same_type_p (non_reference (type), non_reference (result_type)))
5403 result = build_const_cast_1 (type, result, false, &valid_p);
5404 gcc_assert (valid_p);
5406 return result;
5409 return error_mark_node;
5412 /* Build an assignment expression of lvalue LHS from value RHS.
5413 MODIFYCODE is the code for a binary operator that we use
5414 to combine the old value of LHS with RHS to get the new value.
5415 Or else MODIFYCODE is NOP_EXPR meaning do a simple assignment.
5417 C++: If MODIFYCODE is INIT_EXPR, then leave references unbashed. */
5419 tree
5420 build_modify_expr (tree lhs, enum tree_code modifycode, tree rhs)
5422 tree result;
5423 tree newrhs = rhs;
5424 tree lhstype = TREE_TYPE (lhs);
5425 tree olhstype = lhstype;
5426 tree olhs = NULL_TREE;
5427 bool plain_assign = (modifycode == NOP_EXPR);
5429 /* Avoid duplicate error messages from operands that had errors. */
5430 if (lhs == error_mark_node || rhs == error_mark_node)
5431 return error_mark_node;
5433 /* Handle control structure constructs used as "lvalues". */
5434 switch (TREE_CODE (lhs))
5436 /* Handle --foo = 5; as these are valid constructs in C++. */
5437 case PREDECREMENT_EXPR:
5438 case PREINCREMENT_EXPR:
5439 if (TREE_SIDE_EFFECTS (TREE_OPERAND (lhs, 0)))
5440 lhs = build2 (TREE_CODE (lhs), TREE_TYPE (lhs),
5441 stabilize_reference (TREE_OPERAND (lhs, 0)),
5442 TREE_OPERAND (lhs, 1));
5443 return build2 (COMPOUND_EXPR, lhstype,
5444 lhs,
5445 build_modify_expr (TREE_OPERAND (lhs, 0),
5446 modifycode, rhs));
5448 /* Handle (a, b) used as an "lvalue". */
5449 case COMPOUND_EXPR:
5450 newrhs = build_modify_expr (TREE_OPERAND (lhs, 1),
5451 modifycode, rhs);
5452 if (newrhs == error_mark_node)
5453 return error_mark_node;
5454 return build2 (COMPOUND_EXPR, lhstype,
5455 TREE_OPERAND (lhs, 0), newrhs);
5457 case MODIFY_EXPR:
5458 if (TREE_SIDE_EFFECTS (TREE_OPERAND (lhs, 0)))
5459 lhs = build2 (TREE_CODE (lhs), TREE_TYPE (lhs),
5460 stabilize_reference (TREE_OPERAND (lhs, 0)),
5461 TREE_OPERAND (lhs, 1));
5462 newrhs = build_modify_expr (TREE_OPERAND (lhs, 0), modifycode, rhs);
5463 if (newrhs == error_mark_node)
5464 return error_mark_node;
5465 return build2 (COMPOUND_EXPR, lhstype, lhs, newrhs);
5467 case MIN_EXPR:
5468 case MAX_EXPR:
5469 /* MIN_EXPR and MAX_EXPR are currently only permitted as lvalues,
5470 when neither operand has side-effects. */
5471 if (!lvalue_or_else (lhs, lv_assign))
5472 return error_mark_node;
5474 gcc_assert (!TREE_SIDE_EFFECTS (TREE_OPERAND (lhs, 0))
5475 && !TREE_SIDE_EFFECTS (TREE_OPERAND (lhs, 1)));
5477 lhs = build3 (COND_EXPR, TREE_TYPE (lhs),
5478 build2 (TREE_CODE (lhs) == MIN_EXPR ? LE_EXPR : GE_EXPR,
5479 boolean_type_node,
5480 TREE_OPERAND (lhs, 0),
5481 TREE_OPERAND (lhs, 1)),
5482 TREE_OPERAND (lhs, 0),
5483 TREE_OPERAND (lhs, 1));
5484 /* Fall through. */
5486 /* Handle (a ? b : c) used as an "lvalue". */
5487 case COND_EXPR:
5489 /* Produce (a ? (b = rhs) : (c = rhs))
5490 except that the RHS goes through a save-expr
5491 so the code to compute it is only emitted once. */
5492 tree cond;
5493 tree preeval = NULL_TREE;
5495 rhs = stabilize_expr (rhs, &preeval);
5497 /* Check this here to avoid odd errors when trying to convert
5498 a throw to the type of the COND_EXPR. */
5499 if (!lvalue_or_else (lhs, lv_assign))
5500 return error_mark_node;
5502 cond = build_conditional_expr
5503 (TREE_OPERAND (lhs, 0),
5504 build_modify_expr (cp_convert (TREE_TYPE (lhs),
5505 TREE_OPERAND (lhs, 1)),
5506 modifycode, rhs),
5507 build_modify_expr (cp_convert (TREE_TYPE (lhs),
5508 TREE_OPERAND (lhs, 2)),
5509 modifycode, rhs));
5511 if (cond == error_mark_node)
5512 return cond;
5513 /* Make sure the code to compute the rhs comes out
5514 before the split. */
5515 if (preeval)
5516 cond = build2 (COMPOUND_EXPR, TREE_TYPE (lhs), preeval, cond);
5517 return cond;
5520 default:
5521 break;
5524 if (modifycode == INIT_EXPR)
5526 if (TREE_CODE (rhs) == CONSTRUCTOR)
5528 if (! same_type_p (TREE_TYPE (rhs), lhstype))
5529 /* Call convert to generate an error; see PR 11063. */
5530 rhs = convert (lhstype, rhs);
5531 result = build2 (INIT_EXPR, lhstype, lhs, rhs);
5532 TREE_SIDE_EFFECTS (result) = 1;
5533 return result;
5535 else if (! IS_AGGR_TYPE (lhstype))
5536 /* Do the default thing. */;
5537 else
5539 result = build_special_member_call (lhs, complete_ctor_identifier,
5540 build_tree_list (NULL_TREE, rhs),
5541 lhstype, LOOKUP_NORMAL);
5542 if (result == NULL_TREE)
5543 return error_mark_node;
5544 return result;
5547 else
5549 lhs = require_complete_type (lhs);
5550 if (lhs == error_mark_node)
5551 return error_mark_node;
5553 if (modifycode == NOP_EXPR)
5555 /* `operator=' is not an inheritable operator. */
5556 if (! IS_AGGR_TYPE (lhstype))
5557 /* Do the default thing. */;
5558 else
5560 result = build_new_op (MODIFY_EXPR, LOOKUP_NORMAL,
5561 lhs, rhs, make_node (NOP_EXPR),
5562 /*overloaded_p=*/NULL);
5563 if (result == NULL_TREE)
5564 return error_mark_node;
5565 return result;
5567 lhstype = olhstype;
5569 else
5571 /* A binary op has been requested. Combine the old LHS
5572 value with the RHS producing the value we should actually
5573 store into the LHS. */
5575 gcc_assert (!PROMOTES_TO_AGGR_TYPE (lhstype, REFERENCE_TYPE));
5576 lhs = stabilize_reference (lhs);
5577 newrhs = cp_build_binary_op (modifycode, lhs, rhs);
5578 if (newrhs == error_mark_node)
5580 error (" in evaluation of %<%Q(%#T, %#T)%>", modifycode,
5581 TREE_TYPE (lhs), TREE_TYPE (rhs));
5582 return error_mark_node;
5585 /* Now it looks like a plain assignment. */
5586 modifycode = NOP_EXPR;
5588 gcc_assert (TREE_CODE (lhstype) != REFERENCE_TYPE);
5589 gcc_assert (TREE_CODE (TREE_TYPE (newrhs)) != REFERENCE_TYPE);
5592 /* The left-hand side must be an lvalue. */
5593 if (!lvalue_or_else (lhs, lv_assign))
5594 return error_mark_node;
5596 /* Warn about modifying something that is `const'. Don't warn if
5597 this is initialization. */
5598 if (modifycode != INIT_EXPR
5599 && (TREE_READONLY (lhs) || CP_TYPE_CONST_P (lhstype)
5600 /* Functions are not modifiable, even though they are
5601 lvalues. */
5602 || TREE_CODE (TREE_TYPE (lhs)) == FUNCTION_TYPE
5603 || TREE_CODE (TREE_TYPE (lhs)) == METHOD_TYPE
5604 /* If it's an aggregate and any field is const, then it is
5605 effectively const. */
5606 || (CLASS_TYPE_P (lhstype)
5607 && C_TYPE_FIELDS_READONLY (lhstype))))
5608 readonly_error (lhs, "assignment", 0);
5610 /* If storing into a structure or union member, it has probably been
5611 given type `int'. Compute the type that would go with the actual
5612 amount of storage the member occupies. */
5614 if (TREE_CODE (lhs) == COMPONENT_REF
5615 && (TREE_CODE (lhstype) == INTEGER_TYPE
5616 || TREE_CODE (lhstype) == REAL_TYPE
5617 || TREE_CODE (lhstype) == ENUMERAL_TYPE))
5619 lhstype = TREE_TYPE (get_unwidened (lhs, 0));
5621 /* If storing in a field that is in actuality a short or narrower
5622 than one, we must store in the field in its actual type. */
5624 if (lhstype != TREE_TYPE (lhs))
5626 /* Avoid warnings converting integral types back into enums for
5627 enum bit fields. */
5628 if (TREE_CODE (lhstype) == INTEGER_TYPE
5629 && TREE_CODE (olhstype) == ENUMERAL_TYPE)
5631 if (TREE_SIDE_EFFECTS (lhs))
5632 lhs = stabilize_reference (lhs);
5633 olhs = lhs;
5635 lhs = copy_node (lhs);
5636 TREE_TYPE (lhs) = lhstype;
5640 /* Convert new value to destination type. */
5642 if (TREE_CODE (lhstype) == ARRAY_TYPE)
5644 int from_array;
5646 if (!same_or_base_type_p (TYPE_MAIN_VARIANT (lhstype),
5647 TYPE_MAIN_VARIANT (TREE_TYPE (rhs))))
5649 error ("incompatible types in assignment of %qT to %qT",
5650 TREE_TYPE (rhs), lhstype);
5651 return error_mark_node;
5654 /* Allow array assignment in compiler-generated code. */
5655 if (! DECL_ARTIFICIAL (current_function_decl))
5656 pedwarn ("ISO C++ forbids assignment of arrays");
5658 from_array = TREE_CODE (TREE_TYPE (newrhs)) == ARRAY_TYPE
5659 ? 1 + (modifycode != INIT_EXPR): 0;
5660 return build_vec_init (lhs, NULL_TREE, newrhs,
5661 /*explicit_default_init_p=*/false,
5662 from_array);
5665 if (modifycode == INIT_EXPR)
5666 newrhs = convert_for_initialization (lhs, lhstype, newrhs, LOOKUP_NORMAL,
5667 "initialization", NULL_TREE, 0);
5668 else
5670 /* Avoid warnings on enum bit fields. */
5671 if (TREE_CODE (olhstype) == ENUMERAL_TYPE
5672 && TREE_CODE (lhstype) == INTEGER_TYPE)
5674 newrhs = convert_for_assignment (olhstype, newrhs, "assignment",
5675 NULL_TREE, 0);
5676 newrhs = convert_force (lhstype, newrhs, 0);
5678 else
5679 newrhs = convert_for_assignment (lhstype, newrhs, "assignment",
5680 NULL_TREE, 0);
5681 if (TREE_CODE (newrhs) == CALL_EXPR
5682 && TYPE_NEEDS_CONSTRUCTING (lhstype))
5683 newrhs = build_cplus_new (lhstype, newrhs);
5685 /* Can't initialize directly from a TARGET_EXPR, since that would
5686 cause the lhs to be constructed twice, and possibly result in
5687 accidental self-initialization. So we force the TARGET_EXPR to be
5688 expanded without a target. */
5689 if (TREE_CODE (newrhs) == TARGET_EXPR)
5690 newrhs = build2 (COMPOUND_EXPR, TREE_TYPE (newrhs), newrhs,
5691 TREE_OPERAND (newrhs, 0));
5694 if (newrhs == error_mark_node)
5695 return error_mark_node;
5697 if (c_dialect_objc () && flag_objc_gc)
5699 result = objc_generate_write_barrier (lhs, modifycode, newrhs);
5701 if (result)
5702 return result;
5705 result = build2 (modifycode == NOP_EXPR ? MODIFY_EXPR : INIT_EXPR,
5706 lhstype, lhs, newrhs);
5708 TREE_SIDE_EFFECTS (result) = 1;
5709 if (!plain_assign)
5710 TREE_NO_WARNING (result) = 1;
5712 /* If we got the LHS in a different type for storing in,
5713 convert the result back to the nominal type of LHS
5714 so that the value we return always has the same type
5715 as the LHS argument. */
5717 if (olhstype == TREE_TYPE (result))
5718 return result;
5719 if (olhs)
5721 result = build2 (COMPOUND_EXPR, olhstype, result, olhs);
5722 TREE_NO_WARNING (result) = 1;
5723 return result;
5725 return convert_for_assignment (olhstype, result, "assignment",
5726 NULL_TREE, 0);
5729 tree
5730 build_x_modify_expr (tree lhs, enum tree_code modifycode, tree rhs)
5732 if (processing_template_decl)
5733 return build_min_nt (MODOP_EXPR, lhs,
5734 build_min_nt (modifycode, NULL_TREE, NULL_TREE), rhs);
5736 if (modifycode != NOP_EXPR)
5738 tree rval = build_new_op (MODIFY_EXPR, LOOKUP_NORMAL, lhs, rhs,
5739 make_node (modifycode),
5740 /*overloaded_p=*/NULL);
5741 if (rval)
5743 TREE_NO_WARNING (rval) = 1;
5744 return rval;
5747 return build_modify_expr (lhs, modifycode, rhs);
5751 /* Get difference in deltas for different pointer to member function
5752 types. Returns an integer constant of type PTRDIFF_TYPE_NODE. If
5753 the conversion is invalid, the constant is zero. If
5754 ALLOW_INVERSE_P is true, then allow reverse conversions as well.
5755 If C_CAST_P is true this conversion is taking place as part of a
5756 C-style cast.
5758 Note that the naming of FROM and TO is kind of backwards; the return
5759 value is what we add to a TO in order to get a FROM. They are named
5760 this way because we call this function to find out how to convert from
5761 a pointer to member of FROM to a pointer to member of TO. */
5763 static tree
5764 get_delta_difference (tree from, tree to,
5765 bool allow_inverse_p,
5766 bool c_cast_p)
5768 tree binfo;
5769 base_kind kind;
5770 tree result;
5772 /* Assume no conversion is required. */
5773 result = integer_zero_node;
5774 binfo = lookup_base (to, from, c_cast_p ? ba_unique : ba_check, &kind);
5775 if (kind == bk_inaccessible || kind == bk_ambig)
5776 error (" in pointer to member function conversion");
5777 else if (binfo)
5779 if (kind != bk_via_virtual)
5780 result = BINFO_OFFSET (binfo);
5781 else
5783 tree virt_binfo = binfo_from_vbase (binfo);
5785 /* This is a reinterpret cast, we choose to do nothing. */
5786 if (allow_inverse_p)
5787 warning (0, "pointer to member cast via virtual base %qT",
5788 BINFO_TYPE (virt_binfo));
5789 else
5790 error ("pointer to member conversion via virtual base %qT",
5791 BINFO_TYPE (virt_binfo));
5794 else if (same_type_ignoring_top_level_qualifiers_p (from, to))
5795 /* Pointer to member of incomplete class is permitted*/;
5796 else if (!allow_inverse_p)
5798 error_not_base_type (from, to);
5799 error (" in pointer to member conversion");
5801 else
5803 binfo = lookup_base (from, to, c_cast_p ? ba_unique : ba_check, &kind);
5804 if (binfo)
5806 if (kind != bk_via_virtual)
5807 result = size_diffop (size_zero_node, BINFO_OFFSET (binfo));
5808 else
5810 /* This is a reinterpret cast, we choose to do nothing. */
5811 tree virt_binfo = binfo_from_vbase (binfo);
5813 warning (0, "pointer to member cast via virtual base %qT",
5814 BINFO_TYPE (virt_binfo));
5819 return fold_if_not_in_template (convert_to_integer (ptrdiff_type_node,
5820 result));
5823 /* Return a constructor for the pointer-to-member-function TYPE using
5824 the other components as specified. */
5826 tree
5827 build_ptrmemfunc1 (tree type, tree delta, tree pfn)
5829 tree u = NULL_TREE;
5830 tree delta_field;
5831 tree pfn_field;
5832 VEC(constructor_elt, gc) *v;
5834 /* Pull the FIELD_DECLs out of the type. */
5835 pfn_field = TYPE_FIELDS (type);
5836 delta_field = TREE_CHAIN (pfn_field);
5838 /* Make sure DELTA has the type we want. */
5839 delta = convert_and_check (delta_type_node, delta);
5841 /* Finish creating the initializer. */
5842 v = VEC_alloc(constructor_elt, gc, 2);
5843 CONSTRUCTOR_APPEND_ELT(v, pfn_field, pfn);
5844 CONSTRUCTOR_APPEND_ELT(v, delta_field, delta);
5845 u = build_constructor (type, v);
5846 TREE_CONSTANT (u) = TREE_CONSTANT (pfn) & TREE_CONSTANT (delta);
5847 TREE_INVARIANT (u) = TREE_INVARIANT (pfn) & TREE_INVARIANT (delta);
5848 TREE_STATIC (u) = (TREE_CONSTANT (u)
5849 && (initializer_constant_valid_p (pfn, TREE_TYPE (pfn))
5850 != NULL_TREE)
5851 && (initializer_constant_valid_p (delta, TREE_TYPE (delta))
5852 != NULL_TREE));
5853 return u;
5856 /* Build a constructor for a pointer to member function. It can be
5857 used to initialize global variables, local variable, or used
5858 as a value in expressions. TYPE is the POINTER to METHOD_TYPE we
5859 want to be.
5861 If FORCE is nonzero, then force this conversion, even if
5862 we would rather not do it. Usually set when using an explicit
5863 cast. A C-style cast is being processed iff C_CAST_P is true.
5865 Return error_mark_node, if something goes wrong. */
5867 tree
5868 build_ptrmemfunc (tree type, tree pfn, int force, bool c_cast_p)
5870 tree fn;
5871 tree pfn_type;
5872 tree to_type;
5874 if (error_operand_p (pfn))
5875 return error_mark_node;
5877 pfn_type = TREE_TYPE (pfn);
5878 to_type = build_ptrmemfunc_type (type);
5880 /* Handle multiple conversions of pointer to member functions. */
5881 if (TYPE_PTRMEMFUNC_P (pfn_type))
5883 tree delta = NULL_TREE;
5884 tree npfn = NULL_TREE;
5885 tree n;
5887 if (!force
5888 && !can_convert_arg (to_type, TREE_TYPE (pfn), pfn, LOOKUP_NORMAL))
5889 error ("invalid conversion to type %qT from type %qT",
5890 to_type, pfn_type);
5892 n = get_delta_difference (TYPE_PTRMEMFUNC_OBJECT_TYPE (pfn_type),
5893 TYPE_PTRMEMFUNC_OBJECT_TYPE (to_type),
5894 force,
5895 c_cast_p);
5897 /* We don't have to do any conversion to convert a
5898 pointer-to-member to its own type. But, we don't want to
5899 just return a PTRMEM_CST if there's an explicit cast; that
5900 cast should make the expression an invalid template argument. */
5901 if (TREE_CODE (pfn) != PTRMEM_CST)
5903 if (same_type_p (to_type, pfn_type))
5904 return pfn;
5905 else if (integer_zerop (n))
5906 return build_reinterpret_cast (to_type, pfn);
5909 if (TREE_SIDE_EFFECTS (pfn))
5910 pfn = save_expr (pfn);
5912 /* Obtain the function pointer and the current DELTA. */
5913 if (TREE_CODE (pfn) == PTRMEM_CST)
5914 expand_ptrmemfunc_cst (pfn, &delta, &npfn);
5915 else
5917 npfn = build_ptrmemfunc_access_expr (pfn, pfn_identifier);
5918 delta = build_ptrmemfunc_access_expr (pfn, delta_identifier);
5921 /* Just adjust the DELTA field. */
5922 gcc_assert (same_type_ignoring_top_level_qualifiers_p
5923 (TREE_TYPE (delta), ptrdiff_type_node));
5924 if (TARGET_PTRMEMFUNC_VBIT_LOCATION == ptrmemfunc_vbit_in_delta)
5925 n = cp_build_binary_op (LSHIFT_EXPR, n, integer_one_node);
5926 delta = cp_build_binary_op (PLUS_EXPR, delta, n);
5927 return build_ptrmemfunc1 (to_type, delta, npfn);
5930 /* Handle null pointer to member function conversions. */
5931 if (integer_zerop (pfn))
5933 pfn = build_c_cast (type, integer_zero_node);
5934 return build_ptrmemfunc1 (to_type,
5935 integer_zero_node,
5936 pfn);
5939 if (type_unknown_p (pfn))
5940 return instantiate_type (type, pfn, tf_warning_or_error);
5942 fn = TREE_OPERAND (pfn, 0);
5943 gcc_assert (TREE_CODE (fn) == FUNCTION_DECL
5944 /* In a template, we will have preserved the
5945 OFFSET_REF. */
5946 || (processing_template_decl && TREE_CODE (fn) == OFFSET_REF));
5947 return make_ptrmem_cst (to_type, fn);
5950 /* Return the DELTA, IDX, PFN, and DELTA2 values for the PTRMEM_CST
5951 given by CST.
5953 ??? There is no consistency as to the types returned for the above
5954 values. Some code acts as if it were a sizetype and some as if it were
5955 integer_type_node. */
5957 void
5958 expand_ptrmemfunc_cst (tree cst, tree *delta, tree *pfn)
5960 tree type = TREE_TYPE (cst);
5961 tree fn = PTRMEM_CST_MEMBER (cst);
5962 tree ptr_class, fn_class;
5964 gcc_assert (TREE_CODE (fn) == FUNCTION_DECL);
5966 /* The class that the function belongs to. */
5967 fn_class = DECL_CONTEXT (fn);
5969 /* The class that we're creating a pointer to member of. */
5970 ptr_class = TYPE_PTRMEMFUNC_OBJECT_TYPE (type);
5972 /* First, calculate the adjustment to the function's class. */
5973 *delta = get_delta_difference (fn_class, ptr_class, /*force=*/0,
5974 /*c_cast_p=*/0);
5976 if (!DECL_VIRTUAL_P (fn))
5977 *pfn = convert (TYPE_PTRMEMFUNC_FN_TYPE (type), build_addr_func (fn));
5978 else
5980 /* If we're dealing with a virtual function, we have to adjust 'this'
5981 again, to point to the base which provides the vtable entry for
5982 fn; the call will do the opposite adjustment. */
5983 tree orig_class = DECL_CONTEXT (fn);
5984 tree binfo = binfo_or_else (orig_class, fn_class);
5985 *delta = build2 (PLUS_EXPR, TREE_TYPE (*delta),
5986 *delta, BINFO_OFFSET (binfo));
5987 *delta = fold_if_not_in_template (*delta);
5989 /* We set PFN to the vtable offset at which the function can be
5990 found, plus one (unless ptrmemfunc_vbit_in_delta, in which
5991 case delta is shifted left, and then incremented). */
5992 *pfn = DECL_VINDEX (fn);
5993 *pfn = build2 (MULT_EXPR, integer_type_node, *pfn,
5994 TYPE_SIZE_UNIT (vtable_entry_type));
5995 *pfn = fold_if_not_in_template (*pfn);
5997 switch (TARGET_PTRMEMFUNC_VBIT_LOCATION)
5999 case ptrmemfunc_vbit_in_pfn:
6000 *pfn = build2 (PLUS_EXPR, integer_type_node, *pfn,
6001 integer_one_node);
6002 *pfn = fold_if_not_in_template (*pfn);
6003 break;
6005 case ptrmemfunc_vbit_in_delta:
6006 *delta = build2 (LSHIFT_EXPR, TREE_TYPE (*delta),
6007 *delta, integer_one_node);
6008 *delta = fold_if_not_in_template (*delta);
6009 *delta = build2 (PLUS_EXPR, TREE_TYPE (*delta),
6010 *delta, integer_one_node);
6011 *delta = fold_if_not_in_template (*delta);
6012 break;
6014 default:
6015 gcc_unreachable ();
6018 *pfn = build_nop (TYPE_PTRMEMFUNC_FN_TYPE (type), *pfn);
6019 *pfn = fold_if_not_in_template (*pfn);
6023 /* Return an expression for PFN from the pointer-to-member function
6024 given by T. */
6026 static tree
6027 pfn_from_ptrmemfunc (tree t)
6029 if (TREE_CODE (t) == PTRMEM_CST)
6031 tree delta;
6032 tree pfn;
6034 expand_ptrmemfunc_cst (t, &delta, &pfn);
6035 if (pfn)
6036 return pfn;
6039 return build_ptrmemfunc_access_expr (t, pfn_identifier);
6042 /* Convert value RHS to type TYPE as preparation for an assignment to
6043 an lvalue of type TYPE. ERRTYPE is a string to use in error
6044 messages: "assignment", "return", etc. If FNDECL is non-NULL, we
6045 are doing the conversion in order to pass the PARMNUMth argument of
6046 FNDECL. */
6048 static tree
6049 convert_for_assignment (tree type, tree rhs,
6050 const char *errtype, tree fndecl, int parmnum)
6052 tree rhstype;
6053 enum tree_code coder;
6055 /* Strip NON_LVALUE_EXPRs since we aren't using as an lvalue. */
6056 if (TREE_CODE (rhs) == NON_LVALUE_EXPR)
6057 rhs = TREE_OPERAND (rhs, 0);
6059 rhstype = TREE_TYPE (rhs);
6060 coder = TREE_CODE (rhstype);
6062 if (TREE_CODE (type) == VECTOR_TYPE && coder == VECTOR_TYPE
6063 && vector_types_convertible_p (type, rhstype))
6064 return convert (type, rhs);
6066 if (rhs == error_mark_node || rhstype == error_mark_node)
6067 return error_mark_node;
6068 if (TREE_CODE (rhs) == TREE_LIST && TREE_VALUE (rhs) == error_mark_node)
6069 return error_mark_node;
6071 /* The RHS of an assignment cannot have void type. */
6072 if (coder == VOID_TYPE)
6074 error ("void value not ignored as it ought to be");
6075 return error_mark_node;
6078 /* Simplify the RHS if possible. */
6079 if (TREE_CODE (rhs) == CONST_DECL)
6080 rhs = DECL_INITIAL (rhs);
6082 if (c_dialect_objc ())
6084 int parmno;
6085 tree rname = fndecl;
6087 if (!strcmp (errtype, "assignment"))
6088 parmno = -1;
6089 else if (!strcmp (errtype, "initialization"))
6090 parmno = -2;
6091 else
6093 tree selector = objc_message_selector ();
6095 parmno = parmnum;
6097 if (selector && parmno > 1)
6099 rname = selector;
6100 parmno -= 1;
6104 if (objc_compare_types (type, rhstype, parmno, rname))
6105 return convert (type, rhs);
6108 /* [expr.ass]
6110 The expression is implicitly converted (clause _conv_) to the
6111 cv-unqualified type of the left operand.
6113 We allow bad conversions here because by the time we get to this point
6114 we are committed to doing the conversion. If we end up doing a bad
6115 conversion, convert_like will complain. */
6116 if (!can_convert_arg_bad (type, rhstype, rhs))
6118 /* When -Wno-pmf-conversions is use, we just silently allow
6119 conversions from pointers-to-members to plain pointers. If
6120 the conversion doesn't work, cp_convert will complain. */
6121 if (!warn_pmf2ptr
6122 && TYPE_PTR_P (type)
6123 && TYPE_PTRMEMFUNC_P (rhstype))
6124 rhs = cp_convert (strip_top_quals (type), rhs);
6125 else
6127 /* If the right-hand side has unknown type, then it is an
6128 overloaded function. Call instantiate_type to get error
6129 messages. */
6130 if (rhstype == unknown_type_node)
6131 instantiate_type (type, rhs, tf_warning_or_error);
6132 else if (fndecl)
6133 error ("cannot convert %qT to %qT for argument %qP to %qD",
6134 rhstype, type, parmnum, fndecl);
6135 else
6136 error ("cannot convert %qT to %qT in %s", rhstype, type, errtype);
6137 return error_mark_node;
6140 if (warn_missing_format_attribute)
6142 const enum tree_code codel = TREE_CODE (type);
6143 if ((codel == POINTER_TYPE || codel == REFERENCE_TYPE)
6144 && coder == codel
6145 && check_missing_format_attribute (type, rhstype))
6146 warning (OPT_Wmissing_format_attribute,
6147 "%s might be a candidate for a format attribute",
6148 errtype);
6151 return perform_implicit_conversion (strip_top_quals (type), rhs);
6154 /* Convert RHS to be of type TYPE.
6155 If EXP is nonzero, it is the target of the initialization.
6156 ERRTYPE is a string to use in error messages.
6158 Two major differences between the behavior of
6159 `convert_for_assignment' and `convert_for_initialization'
6160 are that references are bashed in the former, while
6161 copied in the latter, and aggregates are assigned in
6162 the former (operator=) while initialized in the
6163 latter (X(X&)).
6165 If using constructor make sure no conversion operator exists, if one does
6166 exist, an ambiguity exists.
6168 If flags doesn't include LOOKUP_COMPLAIN, don't complain about anything. */
6170 tree
6171 convert_for_initialization (tree exp, tree type, tree rhs, int flags,
6172 const char *errtype, tree fndecl, int parmnum)
6174 enum tree_code codel = TREE_CODE (type);
6175 tree rhstype;
6176 enum tree_code coder;
6178 /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
6179 Strip such NOP_EXPRs, since RHS is used in non-lvalue context. */
6180 if (TREE_CODE (rhs) == NOP_EXPR
6181 && TREE_TYPE (rhs) == TREE_TYPE (TREE_OPERAND (rhs, 0))
6182 && codel != REFERENCE_TYPE)
6183 rhs = TREE_OPERAND (rhs, 0);
6185 if (rhs == error_mark_node
6186 || (TREE_CODE (rhs) == TREE_LIST && TREE_VALUE (rhs) == error_mark_node))
6187 return error_mark_node;
6189 if ((TREE_CODE (TREE_TYPE (rhs)) == ARRAY_TYPE
6190 && TREE_CODE (type) != ARRAY_TYPE
6191 && (TREE_CODE (type) != REFERENCE_TYPE
6192 || TREE_CODE (TREE_TYPE (type)) != ARRAY_TYPE))
6193 || (TREE_CODE (TREE_TYPE (rhs)) == FUNCTION_TYPE
6194 && (TREE_CODE (type) != REFERENCE_TYPE
6195 || TREE_CODE (TREE_TYPE (type)) != FUNCTION_TYPE))
6196 || TREE_CODE (TREE_TYPE (rhs)) == METHOD_TYPE)
6197 rhs = decay_conversion (rhs);
6199 rhstype = TREE_TYPE (rhs);
6200 coder = TREE_CODE (rhstype);
6202 if (coder == ERROR_MARK)
6203 return error_mark_node;
6205 /* We accept references to incomplete types, so we can
6206 return here before checking if RHS is of complete type. */
6208 if (codel == REFERENCE_TYPE)
6210 /* This should eventually happen in convert_arguments. */
6211 int savew = 0, savee = 0;
6213 if (fndecl)
6214 savew = warningcount, savee = errorcount;
6215 rhs = initialize_reference (type, rhs, /*decl=*/NULL_TREE,
6216 /*cleanup=*/NULL);
6217 if (fndecl)
6219 if (warningcount > savew)
6220 warning (0, "in passing argument %P of %q+D", parmnum, fndecl);
6221 else if (errorcount > savee)
6222 error ("in passing argument %P of %q+D", parmnum, fndecl);
6224 return rhs;
6227 if (exp != 0)
6228 exp = require_complete_type (exp);
6229 if (exp == error_mark_node)
6230 return error_mark_node;
6232 rhstype = non_reference (rhstype);
6234 type = complete_type (type);
6236 if (IS_AGGR_TYPE (type))
6237 return ocp_convert (type, rhs, CONV_IMPLICIT|CONV_FORCE_TEMP, flags);
6239 return convert_for_assignment (type, rhs, errtype, fndecl, parmnum);
6242 /* If RETVAL is the address of, or a reference to, a local variable or
6243 temporary give an appropriate warning. */
6245 static void
6246 maybe_warn_about_returning_address_of_local (tree retval)
6248 tree valtype = TREE_TYPE (DECL_RESULT (current_function_decl));
6249 tree whats_returned = retval;
6251 for (;;)
6253 if (TREE_CODE (whats_returned) == COMPOUND_EXPR)
6254 whats_returned = TREE_OPERAND (whats_returned, 1);
6255 else if (TREE_CODE (whats_returned) == CONVERT_EXPR
6256 || TREE_CODE (whats_returned) == NON_LVALUE_EXPR
6257 || TREE_CODE (whats_returned) == NOP_EXPR)
6258 whats_returned = TREE_OPERAND (whats_returned, 0);
6259 else
6260 break;
6263 if (TREE_CODE (whats_returned) != ADDR_EXPR)
6264 return;
6265 whats_returned = TREE_OPERAND (whats_returned, 0);
6267 if (TREE_CODE (valtype) == REFERENCE_TYPE)
6269 if (TREE_CODE (whats_returned) == AGGR_INIT_EXPR
6270 || TREE_CODE (whats_returned) == TARGET_EXPR)
6272 warning (0, "returning reference to temporary");
6273 return;
6275 if (TREE_CODE (whats_returned) == VAR_DECL
6276 && DECL_NAME (whats_returned)
6277 && TEMP_NAME_P (DECL_NAME (whats_returned)))
6279 warning (0, "reference to non-lvalue returned");
6280 return;
6284 if (DECL_P (whats_returned)
6285 && DECL_NAME (whats_returned)
6286 && DECL_FUNCTION_SCOPE_P (whats_returned)
6287 && !(TREE_STATIC (whats_returned)
6288 || TREE_PUBLIC (whats_returned)))
6290 if (TREE_CODE (valtype) == REFERENCE_TYPE)
6291 warning (0, "reference to local variable %q+D returned",
6292 whats_returned);
6293 else
6294 warning (0, "address of local variable %q+D returned",
6295 whats_returned);
6296 return;
6300 /* Check that returning RETVAL from the current function is valid.
6301 Return an expression explicitly showing all conversions required to
6302 change RETVAL into the function return type, and to assign it to
6303 the DECL_RESULT for the function. Set *NO_WARNING to true if
6304 code reaches end of non-void function warning shouldn't be issued
6305 on this RETURN_EXPR. */
6307 tree
6308 check_return_expr (tree retval, bool *no_warning)
6310 tree result;
6311 /* The type actually returned by the function, after any
6312 promotions. */
6313 tree valtype;
6314 int fn_returns_value_p;
6316 *no_warning = false;
6318 /* A `volatile' function is one that isn't supposed to return, ever.
6319 (This is a G++ extension, used to get better code for functions
6320 that call the `volatile' function.) */
6321 if (TREE_THIS_VOLATILE (current_function_decl))
6322 warning (0, "function declared %<noreturn%> has a %<return%> statement");
6324 /* Check for various simple errors. */
6325 if (DECL_DESTRUCTOR_P (current_function_decl))
6327 if (retval)
6328 error ("returning a value from a destructor");
6329 return NULL_TREE;
6331 else if (DECL_CONSTRUCTOR_P (current_function_decl))
6333 if (in_function_try_handler)
6334 /* If a return statement appears in a handler of the
6335 function-try-block of a constructor, the program is ill-formed. */
6336 error ("cannot return from a handler of a function-try-block of a constructor");
6337 else if (retval)
6338 /* You can't return a value from a constructor. */
6339 error ("returning a value from a constructor");
6340 return NULL_TREE;
6343 if (processing_template_decl)
6345 current_function_returns_value = 1;
6346 return retval;
6349 /* When no explicit return-value is given in a function with a named
6350 return value, the named return value is used. */
6351 result = DECL_RESULT (current_function_decl);
6352 valtype = TREE_TYPE (result);
6353 gcc_assert (valtype != NULL_TREE);
6354 fn_returns_value_p = !VOID_TYPE_P (valtype);
6355 if (!retval && DECL_NAME (result) && fn_returns_value_p)
6356 retval = result;
6358 /* Check for a return statement with no return value in a function
6359 that's supposed to return a value. */
6360 if (!retval && fn_returns_value_p)
6362 pedwarn ("return-statement with no value, in function returning %qT",
6363 valtype);
6364 /* Clear this, so finish_function won't say that we reach the
6365 end of a non-void function (which we don't, we gave a
6366 return!). */
6367 current_function_returns_null = 0;
6368 /* And signal caller that TREE_NO_WARNING should be set on the
6369 RETURN_EXPR to avoid control reaches end of non-void function
6370 warnings in tree-cfg.c. */
6371 *no_warning = true;
6373 /* Check for a return statement with a value in a function that
6374 isn't supposed to return a value. */
6375 else if (retval && !fn_returns_value_p)
6377 if (VOID_TYPE_P (TREE_TYPE (retval)))
6378 /* You can return a `void' value from a function of `void'
6379 type. In that case, we have to evaluate the expression for
6380 its side-effects. */
6381 finish_expr_stmt (retval);
6382 else
6383 pedwarn ("return-statement with a value, in function "
6384 "returning 'void'");
6386 current_function_returns_null = 1;
6388 /* There's really no value to return, after all. */
6389 return NULL_TREE;
6391 else if (!retval)
6392 /* Remember that this function can sometimes return without a
6393 value. */
6394 current_function_returns_null = 1;
6395 else
6396 /* Remember that this function did return a value. */
6397 current_function_returns_value = 1;
6399 /* Check for erroneous operands -- but after giving ourselves a
6400 chance to provide an error about returning a value from a void
6401 function. */
6402 if (error_operand_p (retval))
6404 current_function_return_value = error_mark_node;
6405 return error_mark_node;
6408 /* Only operator new(...) throw(), can return NULL [expr.new/13]. */
6409 if ((DECL_OVERLOADED_OPERATOR_P (current_function_decl) == NEW_EXPR
6410 || DECL_OVERLOADED_OPERATOR_P (current_function_decl) == VEC_NEW_EXPR)
6411 && !TYPE_NOTHROW_P (TREE_TYPE (current_function_decl))
6412 && ! flag_check_new
6413 && null_ptr_cst_p (retval))
6414 warning (0, "%<operator new%> must not return NULL unless it is "
6415 "declared %<throw()%> (or -fcheck-new is in effect)");
6417 /* Effective C++ rule 15. See also start_function. */
6418 if (warn_ecpp
6419 && DECL_NAME (current_function_decl) == ansi_assopname(NOP_EXPR))
6421 bool warn = true;
6423 /* The function return type must be a reference to the current
6424 class. */
6425 if (TREE_CODE (valtype) == REFERENCE_TYPE
6426 && same_type_ignoring_top_level_qualifiers_p
6427 (TREE_TYPE (valtype), TREE_TYPE (current_class_ref)))
6429 /* Returning '*this' is obviously OK. */
6430 if (retval == current_class_ref)
6431 warn = false;
6432 /* If we are calling a function whose return type is the same of
6433 the current class reference, it is ok. */
6434 else if (TREE_CODE (retval) == INDIRECT_REF
6435 && TREE_CODE (TREE_OPERAND (retval, 0)) == CALL_EXPR)
6436 warn = false;
6439 if (warn)
6440 warning (OPT_Weffc__, "%<operator=%> should return a reference to %<*this%>");
6443 /* The fabled Named Return Value optimization, as per [class.copy]/15:
6445 [...] For a function with a class return type, if the expression
6446 in the return statement is the name of a local object, and the cv-
6447 unqualified type of the local object is the same as the function
6448 return type, an implementation is permitted to omit creating the tem-
6449 porary object to hold the function return value [...]
6451 So, if this is a value-returning function that always returns the same
6452 local variable, remember it.
6454 It might be nice to be more flexible, and choose the first suitable
6455 variable even if the function sometimes returns something else, but
6456 then we run the risk of clobbering the variable we chose if the other
6457 returned expression uses the chosen variable somehow. And people expect
6458 this restriction, anyway. (jason 2000-11-19)
6460 See finish_function and finalize_nrv for the rest of this optimization. */
6462 if (fn_returns_value_p && flag_elide_constructors)
6464 if (retval != NULL_TREE
6465 && (current_function_return_value == NULL_TREE
6466 || current_function_return_value == retval)
6467 && TREE_CODE (retval) == VAR_DECL
6468 && DECL_CONTEXT (retval) == current_function_decl
6469 && ! TREE_STATIC (retval)
6470 && (DECL_ALIGN (retval)
6471 >= DECL_ALIGN (DECL_RESULT (current_function_decl)))
6472 && same_type_p ((TYPE_MAIN_VARIANT
6473 (TREE_TYPE (retval))),
6474 (TYPE_MAIN_VARIANT
6475 (TREE_TYPE (TREE_TYPE (current_function_decl))))))
6476 current_function_return_value = retval;
6477 else
6478 current_function_return_value = error_mark_node;
6481 /* We don't need to do any conversions when there's nothing being
6482 returned. */
6483 if (!retval)
6484 return NULL_TREE;
6486 /* Do any required conversions. */
6487 if (retval == result || DECL_CONSTRUCTOR_P (current_function_decl))
6488 /* No conversions are required. */
6490 else
6492 /* The type the function is declared to return. */
6493 tree functype = TREE_TYPE (TREE_TYPE (current_function_decl));
6495 /* The functype's return type will have been set to void, if it
6496 was an incomplete type. Just treat this as 'return;' */
6497 if (VOID_TYPE_P (functype))
6498 return error_mark_node;
6500 /* First convert the value to the function's return type, then
6501 to the type of return value's location to handle the
6502 case that functype is smaller than the valtype. */
6503 retval = convert_for_initialization
6504 (NULL_TREE, functype, retval, LOOKUP_NORMAL|LOOKUP_ONLYCONVERTING,
6505 "return", NULL_TREE, 0);
6506 retval = convert (valtype, retval);
6508 /* If the conversion failed, treat this just like `return;'. */
6509 if (retval == error_mark_node)
6510 return retval;
6511 /* We can't initialize a register from a AGGR_INIT_EXPR. */
6512 else if (! current_function_returns_struct
6513 && TREE_CODE (retval) == TARGET_EXPR
6514 && TREE_CODE (TREE_OPERAND (retval, 1)) == AGGR_INIT_EXPR)
6515 retval = build2 (COMPOUND_EXPR, TREE_TYPE (retval), retval,
6516 TREE_OPERAND (retval, 0));
6517 else
6518 maybe_warn_about_returning_address_of_local (retval);
6521 /* Actually copy the value returned into the appropriate location. */
6522 if (retval && retval != result)
6523 retval = build2 (INIT_EXPR, TREE_TYPE (result), result, retval);
6525 return retval;
6529 /* Returns nonzero if the pointer-type FROM can be converted to the
6530 pointer-type TO via a qualification conversion. If CONSTP is -1,
6531 then we return nonzero if the pointers are similar, and the
6532 cv-qualification signature of FROM is a proper subset of that of TO.
6534 If CONSTP is positive, then all outer pointers have been
6535 const-qualified. */
6537 static int
6538 comp_ptr_ttypes_real (tree to, tree from, int constp)
6540 bool to_more_cv_qualified = false;
6542 for (; ; to = TREE_TYPE (to), from = TREE_TYPE (from))
6544 if (TREE_CODE (to) != TREE_CODE (from))
6545 return 0;
6547 if (TREE_CODE (from) == OFFSET_TYPE
6548 && !same_type_p (TYPE_OFFSET_BASETYPE (from),
6549 TYPE_OFFSET_BASETYPE (to)))
6550 return 0;
6552 /* Const and volatile mean something different for function types,
6553 so the usual checks are not appropriate. */
6554 if (TREE_CODE (to) != FUNCTION_TYPE && TREE_CODE (to) != METHOD_TYPE)
6556 /* In Objective-C++, some types may have been 'volatilized' by
6557 the compiler for EH; when comparing them here, the volatile
6558 qualification must be ignored. */
6559 bool objc_quals_match = objc_type_quals_match (to, from);
6561 if (!at_least_as_qualified_p (to, from) && !objc_quals_match)
6562 return 0;
6564 if (!at_least_as_qualified_p (from, to) && !objc_quals_match)
6566 if (constp == 0)
6567 return 0;
6568 to_more_cv_qualified = true;
6571 if (constp > 0)
6572 constp &= TYPE_READONLY (to);
6575 if (TREE_CODE (to) != POINTER_TYPE && !TYPE_PTRMEM_P (to))
6576 return ((constp >= 0 || to_more_cv_qualified)
6577 && same_type_ignoring_top_level_qualifiers_p (to, from));
6581 /* When comparing, say, char ** to char const **, this function takes
6582 the 'char *' and 'char const *'. Do not pass non-pointer/reference
6583 types to this function. */
6586 comp_ptr_ttypes (tree to, tree from)
6588 return comp_ptr_ttypes_real (to, from, 1);
6591 /* Returns 1 if to and from are (possibly multi-level) pointers to the same
6592 type or inheritance-related types, regardless of cv-quals. */
6595 ptr_reasonably_similar (tree to, tree from)
6597 for (; ; to = TREE_TYPE (to), from = TREE_TYPE (from))
6599 /* Any target type is similar enough to void. */
6600 if (TREE_CODE (to) == VOID_TYPE
6601 || TREE_CODE (from) == VOID_TYPE)
6602 return 1;
6604 if (TREE_CODE (to) != TREE_CODE (from))
6605 return 0;
6607 if (TREE_CODE (from) == OFFSET_TYPE
6608 && comptypes (TYPE_OFFSET_BASETYPE (to),
6609 TYPE_OFFSET_BASETYPE (from),
6610 COMPARE_BASE | COMPARE_DERIVED))
6611 continue;
6613 if (TREE_CODE (to) == VECTOR_TYPE
6614 && vector_types_convertible_p (to, from))
6615 return 1;
6617 if (TREE_CODE (to) == INTEGER_TYPE
6618 && TYPE_PRECISION (to) == TYPE_PRECISION (from))
6619 return 1;
6621 if (TREE_CODE (to) == FUNCTION_TYPE)
6622 return 1;
6624 if (TREE_CODE (to) != POINTER_TYPE)
6625 return comptypes
6626 (TYPE_MAIN_VARIANT (to), TYPE_MAIN_VARIANT (from),
6627 COMPARE_BASE | COMPARE_DERIVED);
6631 /* Return true if TO and FROM (both of which are POINTER_TYPEs or
6632 pointer-to-member types) are the same, ignoring cv-qualification at
6633 all levels. */
6635 bool
6636 comp_ptr_ttypes_const (tree to, tree from)
6638 for (; ; to = TREE_TYPE (to), from = TREE_TYPE (from))
6640 if (TREE_CODE (to) != TREE_CODE (from))
6641 return false;
6643 if (TREE_CODE (from) == OFFSET_TYPE
6644 && same_type_p (TYPE_OFFSET_BASETYPE (from),
6645 TYPE_OFFSET_BASETYPE (to)))
6646 continue;
6648 if (TREE_CODE (to) != POINTER_TYPE)
6649 return same_type_ignoring_top_level_qualifiers_p (to, from);
6653 /* Returns the type qualifiers for this type, including the qualifiers on the
6654 elements for an array type. */
6657 cp_type_quals (tree type)
6659 type = strip_array_types (type);
6660 if (type == error_mark_node)
6661 return TYPE_UNQUALIFIED;
6662 return TYPE_QUALS (type);
6665 /* Returns nonzero if the TYPE contains a mutable member. */
6667 bool
6668 cp_has_mutable_p (tree type)
6670 type = strip_array_types (type);
6672 return CLASS_TYPE_P (type) && CLASSTYPE_HAS_MUTABLE (type);
6675 /* Apply the TYPE_QUALS to the new DECL. */
6676 void
6677 cp_apply_type_quals_to_decl (int type_quals, tree decl)
6679 tree type = TREE_TYPE (decl);
6681 if (type == error_mark_node)
6682 return;
6684 if (TREE_CODE (type) == FUNCTION_TYPE
6685 && type_quals != TYPE_UNQUALIFIED)
6687 /* This was an error in C++98 (cv-qualifiers cannot be added to
6688 a function type), but DR 295 makes the code well-formed by
6689 dropping the extra qualifiers. */
6690 if (pedantic)
6692 tree bad_type = build_qualified_type (type, type_quals);
6693 pedwarn ("ignoring %qV qualifiers added to function type %qT",
6694 bad_type, type);
6697 TREE_TYPE (decl) = TYPE_MAIN_VARIANT (type);
6698 return;
6701 /* Avoid setting TREE_READONLY incorrectly. */
6702 if (/* If the object has a constructor, the constructor may modify
6703 the object. */
6704 TYPE_NEEDS_CONSTRUCTING (type)
6705 /* If the type isn't complete, we don't know yet if it will need
6706 constructing. */
6707 || !COMPLETE_TYPE_P (type)
6708 /* If the type has a mutable component, that component might be
6709 modified. */
6710 || TYPE_HAS_MUTABLE_P (type))
6711 type_quals &= ~TYPE_QUAL_CONST;
6713 c_apply_type_quals_to_decl (type_quals, decl);
6716 /* Subroutine of casts_away_constness. Make T1 and T2 point at
6717 exemplar types such that casting T1 to T2 is casting away constness
6718 if and only if there is no implicit conversion from T1 to T2. */
6720 static void
6721 casts_away_constness_r (tree *t1, tree *t2)
6723 int quals1;
6724 int quals2;
6726 /* [expr.const.cast]
6728 For multi-level pointer to members and multi-level mixed pointers
6729 and pointers to members (conv.qual), the "member" aspect of a
6730 pointer to member level is ignored when determining if a const
6731 cv-qualifier has been cast away. */
6732 /* [expr.const.cast]
6734 For two pointer types:
6736 X1 is T1cv1,1 * ... cv1,N * where T1 is not a pointer type
6737 X2 is T2cv2,1 * ... cv2,M * where T2 is not a pointer type
6738 K is min(N,M)
6740 casting from X1 to X2 casts away constness if, for a non-pointer
6741 type T there does not exist an implicit conversion (clause
6742 _conv_) from:
6744 Tcv1,(N-K+1) * cv1,(N-K+2) * ... cv1,N *
6748 Tcv2,(M-K+1) * cv2,(M-K+2) * ... cv2,M *. */
6749 if ((!TYPE_PTR_P (*t1) && !TYPE_PTRMEM_P (*t1))
6750 || (!TYPE_PTR_P (*t2) && !TYPE_PTRMEM_P (*t2)))
6752 *t1 = cp_build_qualified_type (void_type_node,
6753 cp_type_quals (*t1));
6754 *t2 = cp_build_qualified_type (void_type_node,
6755 cp_type_quals (*t2));
6756 return;
6759 quals1 = cp_type_quals (*t1);
6760 quals2 = cp_type_quals (*t2);
6762 if (TYPE_PTRMEM_P (*t1))
6763 *t1 = TYPE_PTRMEM_POINTED_TO_TYPE (*t1);
6764 else
6765 *t1 = TREE_TYPE (*t1);
6766 if (TYPE_PTRMEM_P (*t2))
6767 *t2 = TYPE_PTRMEM_POINTED_TO_TYPE (*t2);
6768 else
6769 *t2 = TREE_TYPE (*t2);
6771 casts_away_constness_r (t1, t2);
6772 *t1 = build_pointer_type (*t1);
6773 *t2 = build_pointer_type (*t2);
6774 *t1 = cp_build_qualified_type (*t1, quals1);
6775 *t2 = cp_build_qualified_type (*t2, quals2);
6778 /* Returns nonzero if casting from TYPE1 to TYPE2 casts away
6779 constness. */
6781 static bool
6782 casts_away_constness (tree t1, tree t2)
6784 if (TREE_CODE (t2) == REFERENCE_TYPE)
6786 /* [expr.const.cast]
6788 Casting from an lvalue of type T1 to an lvalue of type T2
6789 using a reference cast casts away constness if a cast from an
6790 rvalue of type "pointer to T1" to the type "pointer to T2"
6791 casts away constness. */
6792 t1 = (TREE_CODE (t1) == REFERENCE_TYPE ? TREE_TYPE (t1) : t1);
6793 return casts_away_constness (build_pointer_type (t1),
6794 build_pointer_type (TREE_TYPE (t2)));
6797 if (TYPE_PTRMEM_P (t1) && TYPE_PTRMEM_P (t2))
6798 /* [expr.const.cast]
6800 Casting from an rvalue of type "pointer to data member of X
6801 of type T1" to the type "pointer to data member of Y of type
6802 T2" casts away constness if a cast from an rvalue of type
6803 "pointer to T1" to the type "pointer to T2" casts away
6804 constness. */
6805 return casts_away_constness
6806 (build_pointer_type (TYPE_PTRMEM_POINTED_TO_TYPE (t1)),
6807 build_pointer_type (TYPE_PTRMEM_POINTED_TO_TYPE (t2)));
6809 /* Casting away constness is only something that makes sense for
6810 pointer or reference types. */
6811 if (TREE_CODE (t1) != POINTER_TYPE
6812 || TREE_CODE (t2) != POINTER_TYPE)
6813 return false;
6815 /* Top-level qualifiers don't matter. */
6816 t1 = TYPE_MAIN_VARIANT (t1);
6817 t2 = TYPE_MAIN_VARIANT (t2);
6818 casts_away_constness_r (&t1, &t2);
6819 if (!can_convert (t2, t1))
6820 return true;
6822 return false;
6825 /* If T is a REFERENCE_TYPE return the type to which T refers.
6826 Otherwise, return T itself. */
6828 tree
6829 non_reference (tree t)
6831 if (TREE_CODE (t) == REFERENCE_TYPE)
6832 t = TREE_TYPE (t);
6833 return t;
6837 /* Return nonzero if REF is an lvalue valid for this language;
6838 otherwise, print an error message and return zero. USE says
6839 how the lvalue is being used and so selects the error message. */
6842 lvalue_or_else (tree ref, enum lvalue_use use)
6844 int win = lvalue_p (ref);
6846 if (!win)
6847 lvalue_error (use);
6849 return win;