PR c++/12114
[official-gcc.git] / gcc / cp / typeck.c
blob564b3373aecc69f4a6117bcab15653cd8e9115df
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 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, 59 Temple Place - Suite 330,
21 Boston, MA 02111-1307, 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 There are also routines to build RETURN_STMT nodes and CASE_STMT nodes,
30 and to process initializations in declarations (since they work
31 like a strange sort of assignment). */
33 #include "config.h"
34 #include "system.h"
35 #include "coretypes.h"
36 #include "tm.h"
37 #include "tree.h"
38 #include "rtl.h"
39 #include "expr.h"
40 #include "cp-tree.h"
41 #include "tm_p.h"
42 #include "flags.h"
43 #include "output.h"
44 #include "toplev.h"
45 #include "diagnostic.h"
46 #include "target.h"
47 #include "convert.h"
49 static tree convert_for_assignment (tree, tree, const char *, tree, int);
50 static tree cp_pointer_int_sum (enum tree_code, tree, tree);
51 static tree rationalize_conditional_expr (enum tree_code, tree);
52 static int comp_ptr_ttypes_real (tree, tree, int);
53 static int comp_ptr_ttypes_const (tree, tree);
54 static bool comp_except_types (tree, tree, bool);
55 static bool comp_array_types (tree, tree, bool);
56 static tree common_base_type (tree, tree);
57 static tree lookup_anon_field (tree, tree);
58 static tree pointer_diff (tree, tree, tree);
59 static tree get_delta_difference (tree, tree, int);
60 static void casts_away_constness_r (tree *, tree *);
61 static bool casts_away_constness (tree, tree);
62 static void maybe_warn_about_returning_address_of_local (tree);
63 static tree lookup_destructor (tree, tree, tree);
65 /* Return the target type of TYPE, which means return T for:
66 T*, T&, T[], T (...), and otherwise, just T. */
68 tree
69 target_type (tree type)
71 type = non_reference (type);
72 while (TREE_CODE (type) == POINTER_TYPE
73 || TREE_CODE (type) == ARRAY_TYPE
74 || TREE_CODE (type) == FUNCTION_TYPE
75 || TREE_CODE (type) == METHOD_TYPE
76 || TYPE_PTRMEM_P (type))
77 type = TREE_TYPE (type);
78 return type;
81 /* Do `exp = require_complete_type (exp);' to make sure exp
82 does not have an incomplete type. (That includes void types.)
83 Returns the error_mark_node if the VALUE does not have
84 complete type when this function returns. */
86 tree
87 require_complete_type (tree value)
89 tree type;
91 if (processing_template_decl || value == error_mark_node)
92 return value;
94 if (TREE_CODE (value) == OVERLOAD)
95 type = unknown_type_node;
96 else
97 type = TREE_TYPE (value);
99 /* First, detect a valid value with a complete type. */
100 if (COMPLETE_TYPE_P (type))
101 return value;
103 if (complete_type_or_else (type, value))
104 return value;
105 else
106 return error_mark_node;
109 /* Try to complete TYPE, if it is incomplete. For example, if TYPE is
110 a template instantiation, do the instantiation. Returns TYPE,
111 whether or not it could be completed, unless something goes
112 horribly wrong, in which case the error_mark_node is returned. */
114 tree
115 complete_type (tree type)
117 if (type == NULL_TREE)
118 /* Rather than crash, we return something sure to cause an error
119 at some point. */
120 return error_mark_node;
122 if (type == error_mark_node || COMPLETE_TYPE_P (type))
124 else if (TREE_CODE (type) == ARRAY_TYPE && TYPE_DOMAIN (type))
126 tree t = complete_type (TREE_TYPE (type));
127 if (COMPLETE_TYPE_P (t) && ! processing_template_decl)
128 layout_type (type);
129 TYPE_NEEDS_CONSTRUCTING (type)
130 = TYPE_NEEDS_CONSTRUCTING (TYPE_MAIN_VARIANT (t));
131 TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type)
132 = TYPE_HAS_NONTRIVIAL_DESTRUCTOR (TYPE_MAIN_VARIANT (t));
134 else if (CLASS_TYPE_P (type) && CLASSTYPE_TEMPLATE_INSTANTIATION (type))
135 instantiate_class_template (TYPE_MAIN_VARIANT (type));
137 return type;
140 /* Like complete_type, but issue an error if the TYPE cannot be completed.
141 VALUE is used for informative diagnostics. DIAG_TYPE indicates the type
142 of diagnostic: 0 for an error, 1 for a warning, 2 for a pedwarn.
143 Returns NULL_TREE if the type cannot be made complete. */
145 tree
146 complete_type_or_diagnostic (tree type, tree value, int diag_type)
148 type = complete_type (type);
149 if (type == error_mark_node)
150 /* We already issued an error. */
151 return NULL_TREE;
152 else if (!COMPLETE_TYPE_P (type))
154 cxx_incomplete_type_diagnostic (value, type, diag_type);
155 return NULL_TREE;
157 else
158 return type;
161 /* Return truthvalue of whether type of EXP is instantiated. */
164 type_unknown_p (tree exp)
166 return (TREE_CODE (exp) == OVERLOAD
167 || TREE_CODE (exp) == TREE_LIST
168 || TREE_TYPE (exp) == unknown_type_node);
172 /* Return the common type of two parameter lists.
173 We assume that comptypes has already been done and returned 1;
174 if that isn't so, this may crash.
176 As an optimization, free the space we allocate if the parameter
177 lists are already common. */
179 tree
180 commonparms (tree p1, tree p2)
182 tree oldargs = p1, newargs, n;
183 int i, len;
184 int any_change = 0;
186 len = list_length (p1);
187 newargs = tree_last (p1);
189 if (newargs == void_list_node)
190 i = 1;
191 else
193 i = 0;
194 newargs = 0;
197 for (; i < len; i++)
198 newargs = tree_cons (NULL_TREE, NULL_TREE, newargs);
200 n = newargs;
202 for (i = 0; p1;
203 p1 = TREE_CHAIN (p1), p2 = TREE_CHAIN (p2), n = TREE_CHAIN (n), i++)
205 if (TREE_PURPOSE (p1) && !TREE_PURPOSE (p2))
207 TREE_PURPOSE (n) = TREE_PURPOSE (p1);
208 any_change = 1;
210 else if (! TREE_PURPOSE (p1))
212 if (TREE_PURPOSE (p2))
214 TREE_PURPOSE (n) = TREE_PURPOSE (p2);
215 any_change = 1;
218 else
220 if (1 != simple_cst_equal (TREE_PURPOSE (p1), TREE_PURPOSE (p2)))
221 any_change = 1;
222 TREE_PURPOSE (n) = TREE_PURPOSE (p2);
224 if (TREE_VALUE (p1) != TREE_VALUE (p2))
226 any_change = 1;
227 TREE_VALUE (n) = merge_types (TREE_VALUE (p1), TREE_VALUE (p2));
229 else
230 TREE_VALUE (n) = TREE_VALUE (p1);
232 if (! any_change)
233 return oldargs;
235 return newargs;
238 /* Given a type, perhaps copied for a typedef,
239 find the "original" version of it. */
240 tree
241 original_type (tree t)
243 while (TYPE_NAME (t) != NULL_TREE)
245 tree x = TYPE_NAME (t);
246 if (TREE_CODE (x) != TYPE_DECL)
247 break;
248 x = DECL_ORIGINAL_TYPE (x);
249 if (x == NULL_TREE)
250 break;
251 t = x;
253 return t;
256 /* T1 and T2 are arithmetic or enumeration types. Return the type
257 that will result from the "usual arithmetic conversions" on T1 and
258 T2 as described in [expr]. */
260 tree
261 type_after_usual_arithmetic_conversions (tree t1, tree t2)
263 enum tree_code code1 = TREE_CODE (t1);
264 enum tree_code code2 = TREE_CODE (t2);
265 tree attributes;
267 /* FIXME: Attributes. */
268 my_friendly_assert (ARITHMETIC_TYPE_P (t1)
269 || TREE_CODE (t1) == COMPLEX_TYPE
270 || TREE_CODE (t1) == ENUMERAL_TYPE,
271 19990725);
272 my_friendly_assert (ARITHMETIC_TYPE_P (t2)
273 || TREE_CODE (t2) == COMPLEX_TYPE
274 || TREE_CODE (t2) == ENUMERAL_TYPE,
275 19990725);
277 /* In what follows, we slightly generalize the rules given in [expr] so
278 as to deal with `long long' and `complex'. First, merge the
279 attributes. */
280 attributes = (*targetm.merge_type_attributes) (t1, t2);
282 /* If one type is complex, form the common type of the non-complex
283 components, then make that complex. Use T1 or T2 if it is the
284 required type. */
285 if (code1 == COMPLEX_TYPE || code2 == COMPLEX_TYPE)
287 tree subtype1 = code1 == COMPLEX_TYPE ? TREE_TYPE (t1) : t1;
288 tree subtype2 = code2 == COMPLEX_TYPE ? TREE_TYPE (t2) : t2;
289 tree subtype
290 = type_after_usual_arithmetic_conversions (subtype1, subtype2);
292 if (code1 == COMPLEX_TYPE && TREE_TYPE (t1) == subtype)
293 return build_type_attribute_variant (t1, attributes);
294 else if (code2 == COMPLEX_TYPE && TREE_TYPE (t2) == subtype)
295 return build_type_attribute_variant (t2, attributes);
296 else
297 return build_type_attribute_variant (build_complex_type (subtype),
298 attributes);
301 /* If only one is real, use it as the result. */
302 if (code1 == REAL_TYPE && code2 != REAL_TYPE)
303 return build_type_attribute_variant (t1, attributes);
304 if (code2 == REAL_TYPE && code1 != REAL_TYPE)
305 return build_type_attribute_variant (t2, attributes);
307 /* Perform the integral promotions. */
308 if (code1 != REAL_TYPE)
310 t1 = type_promotes_to (t1);
311 t2 = type_promotes_to (t2);
314 /* Both real or both integers; use the one with greater precision. */
315 if (TYPE_PRECISION (t1) > TYPE_PRECISION (t2))
316 return build_type_attribute_variant (t1, attributes);
317 else if (TYPE_PRECISION (t2) > TYPE_PRECISION (t1))
318 return build_type_attribute_variant (t2, attributes);
320 /* The types are the same; no need to do anything fancy. */
321 if (TYPE_MAIN_VARIANT (t1) == TYPE_MAIN_VARIANT (t2))
322 return build_type_attribute_variant (t1, attributes);
324 if (code1 != REAL_TYPE)
326 /* If one is a sizetype, use it so size_binop doesn't blow up. */
327 if (TYPE_IS_SIZETYPE (t1) > TYPE_IS_SIZETYPE (t2))
328 return build_type_attribute_variant (t1, attributes);
329 if (TYPE_IS_SIZETYPE (t2) > TYPE_IS_SIZETYPE (t1))
330 return build_type_attribute_variant (t2, attributes);
332 /* If one is unsigned long long, then convert the other to unsigned
333 long long. */
334 if (same_type_p (TYPE_MAIN_VARIANT (t1), long_long_unsigned_type_node)
335 || same_type_p (TYPE_MAIN_VARIANT (t2), long_long_unsigned_type_node))
336 return build_type_attribute_variant (long_long_unsigned_type_node,
337 attributes);
338 /* If one is a long long, and the other is an unsigned long, and
339 long long can represent all the values of an unsigned long, then
340 convert to a long long. Otherwise, convert to an unsigned long
341 long. Otherwise, if either operand is long long, convert the
342 other to long long.
344 Since we're here, we know the TYPE_PRECISION is the same;
345 therefore converting to long long cannot represent all the values
346 of an unsigned long, so we choose unsigned long long in that
347 case. */
348 if (same_type_p (TYPE_MAIN_VARIANT (t1), long_long_integer_type_node)
349 || same_type_p (TYPE_MAIN_VARIANT (t2), long_long_integer_type_node))
351 tree t = ((TREE_UNSIGNED (t1) || TREE_UNSIGNED (t2))
352 ? long_long_unsigned_type_node
353 : long_long_integer_type_node);
354 return build_type_attribute_variant (t, attributes);
357 /* Go through the same procedure, but for longs. */
358 if (same_type_p (TYPE_MAIN_VARIANT (t1), long_unsigned_type_node)
359 || same_type_p (TYPE_MAIN_VARIANT (t2), long_unsigned_type_node))
360 return build_type_attribute_variant (long_unsigned_type_node,
361 attributes);
362 if (same_type_p (TYPE_MAIN_VARIANT (t1), long_integer_type_node)
363 || same_type_p (TYPE_MAIN_VARIANT (t2), long_integer_type_node))
365 tree t = ((TREE_UNSIGNED (t1) || TREE_UNSIGNED (t2))
366 ? long_unsigned_type_node : long_integer_type_node);
367 return build_type_attribute_variant (t, attributes);
369 /* Otherwise prefer the unsigned one. */
370 if (TREE_UNSIGNED (t1))
371 return build_type_attribute_variant (t1, attributes);
372 else
373 return build_type_attribute_variant (t2, attributes);
375 else
377 if (same_type_p (TYPE_MAIN_VARIANT (t1), long_double_type_node)
378 || same_type_p (TYPE_MAIN_VARIANT (t2), long_double_type_node))
379 return build_type_attribute_variant (long_double_type_node,
380 attributes);
381 if (same_type_p (TYPE_MAIN_VARIANT (t1), double_type_node)
382 || same_type_p (TYPE_MAIN_VARIANT (t2), double_type_node))
383 return build_type_attribute_variant (double_type_node,
384 attributes);
385 if (same_type_p (TYPE_MAIN_VARIANT (t1), float_type_node)
386 || same_type_p (TYPE_MAIN_VARIANT (t2), float_type_node))
387 return build_type_attribute_variant (float_type_node,
388 attributes);
390 /* Two floating-point types whose TYPE_MAIN_VARIANTs are none of
391 the standard C++ floating-point types. Logic earlier in this
392 function has already eliminated the possibility that
393 TYPE_PRECISION (t2) != TYPE_PRECISION (t1), so there's no
394 compelling reason to choose one or the other. */
395 return build_type_attribute_variant (t1, attributes);
399 /* Subroutine of composite_pointer_type to implement the recursive
400 case. See that function for documentation fo the parameters. */
402 static tree
403 composite_pointer_type_r (tree t1, tree t2, const char* location)
405 tree pointee1;
406 tree pointee2;
407 tree result_type;
408 tree attributes;
410 /* Determine the types pointed to by T1 and T2. */
411 if (TREE_CODE (t1) == POINTER_TYPE)
413 pointee1 = TREE_TYPE (t1);
414 pointee2 = TREE_TYPE (t2);
416 else
418 pointee1 = TYPE_PTRMEM_POINTED_TO_TYPE (t1);
419 pointee2 = TYPE_PTRMEM_POINTED_TO_TYPE (t2);
422 /* [expr.rel]
424 Otherwise, the composite pointer type is a pointer type
425 similar (_conv.qual_) to the type of one of the operands,
426 with a cv-qualification signature (_conv.qual_) that is the
427 union of the cv-qualification signatures of the operand
428 types. */
429 if (same_type_ignoring_top_level_qualifiers_p (pointee1, pointee2))
430 result_type = pointee1;
431 else if ((TREE_CODE (pointee1) == POINTER_TYPE
432 && TREE_CODE (pointee2) == POINTER_TYPE)
433 || (TYPE_PTR_TO_MEMBER_P (pointee1)
434 && TYPE_PTR_TO_MEMBER_P (pointee2)))
435 result_type = composite_pointer_type_r (pointee1, pointee2, location);
436 else
438 pedwarn ("%s between distinct pointer types `%T' and `%T' "
439 "lacks a cast",
440 location, t1, t2);
441 result_type = void_type_node;
443 result_type = cp_build_qualified_type (result_type,
444 (cp_type_quals (pointee1)
445 | cp_type_quals (pointee2)));
446 result_type = build_pointer_type (result_type);
447 /* If the original types were pointers to members, so is the
448 result. */
449 if (TYPE_PTR_TO_MEMBER_P (t1))
451 if (!same_type_p (TYPE_PTRMEM_CLASS_TYPE (t1),
452 TYPE_PTRMEM_CLASS_TYPE (t2)))
453 pedwarn ("%s between distinct pointer types `%T' and `%T' "
454 "lacks a cast",
455 location, t1, t2);
456 result_type = build_ptrmem_type (TYPE_PTRMEM_CLASS_TYPE (t1),
457 result_type);
460 /* Merge the attributes. */
461 attributes = (*targetm.merge_type_attributes) (t1, t2);
462 return build_type_attribute_variant (result_type, attributes);
465 /* Return the composite pointer type (see [expr.rel]) for T1 and T2.
466 ARG1 and ARG2 are the values with those types. The LOCATION is a
467 string describing the current location, in case an error occurs.
469 This routine also implements the computation of a common type for
470 pointers-to-members as per [expr.eq]. */
472 tree
473 composite_pointer_type (tree t1, tree t2, tree arg1, tree arg2,
474 const char* location)
476 tree class1;
477 tree class2;
479 /* [expr.rel]
481 If one operand is a null pointer constant, the composite pointer
482 type is the type of the other operand. */
483 if (null_ptr_cst_p (arg1))
484 return t2;
485 if (null_ptr_cst_p (arg2))
486 return t1;
488 /* We have:
490 [expr.rel]
492 If one of the operands has type "pointer to cv1 void*", then
493 the other has type "pointer to cv2T", and the composite pointer
494 type is "pointer to cv12 void", where cv12 is the union of cv1
495 and cv2.
497 If either type is a pointer to void, make sure it is T1. */
498 if (TREE_CODE (t2) == POINTER_TYPE && VOID_TYPE_P (TREE_TYPE (t2)))
500 tree t;
501 t = t1;
502 t1 = t2;
503 t2 = t;
506 /* Now, if T1 is a pointer to void, merge the qualifiers. */
507 if (TREE_CODE (t1) == POINTER_TYPE && VOID_TYPE_P (TREE_TYPE (t1)))
509 tree attributes;
510 tree result_type;
512 if (pedantic && TYPE_PTRFN_P (t2))
513 pedwarn ("ISO C++ forbids %s between pointer of type `void *' 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 /* [expr.eq] permits the application of a pointer conversion to
525 bring the pointers to a common type. */
526 if (TREE_CODE (t1) == POINTER_TYPE && TREE_CODE (t2) == POINTER_TYPE
527 && CLASS_TYPE_P (TREE_TYPE (t1))
528 && CLASS_TYPE_P (TREE_TYPE (t2))
529 && !same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (t1),
530 TREE_TYPE (t2)))
532 class1 = TREE_TYPE (t1);
533 class2 = TREE_TYPE (t2);
535 if (DERIVED_FROM_P (class1, class2))
536 t2 = (build_pointer_type
537 (cp_build_qualified_type (class1, TYPE_QUALS (class2))));
538 else if (DERIVED_FROM_P (class2, class1))
539 t1 = (build_pointer_type
540 (cp_build_qualified_type (class2, TYPE_QUALS (class1))));
541 else
543 error ("%s between distinct pointer types `%T' and `%T' "
544 "lacks a cast", location, t1, t2);
545 return error_mark_node;
548 /* [expr.eq] permits the application of a pointer-to-member
549 conversion to change the class type of one of the types. */
550 else if (TYPE_PTR_TO_MEMBER_P (t1)
551 && !same_type_p (TYPE_PTRMEM_CLASS_TYPE (t1),
552 TYPE_PTRMEM_CLASS_TYPE (t2)))
554 class1 = TYPE_PTRMEM_CLASS_TYPE (t1);
555 class2 = TYPE_PTRMEM_CLASS_TYPE (t2);
557 if (DERIVED_FROM_P (class1, class2))
558 t1 = build_ptrmem_type (class2, TYPE_PTRMEM_POINTED_TO_TYPE (t1));
559 else if (DERIVED_FROM_P (class2, class1))
560 t2 = build_ptrmem_type (class1, TYPE_PTRMEM_POINTED_TO_TYPE (t2));
561 else
563 error ("%s between distinct pointer-to-member types `%T' and `%T' "
564 "lacks a cast", location, t1, t2);
565 return error_mark_node;
569 return composite_pointer_type_r (t1, t2, location);
572 /* Return the merged type of two types.
573 We assume that comptypes has already been done and returned 1;
574 if that isn't so, this may crash.
576 This just combines attributes and default arguments; any other
577 differences would cause the two types to compare unalike. */
579 tree
580 merge_types (tree t1, tree t2)
582 register enum tree_code code1;
583 register enum tree_code code2;
584 tree attributes;
586 /* Save time if the two types are the same. */
587 if (t1 == t2)
588 return t1;
589 if (original_type (t1) == original_type (t2))
590 return t1;
592 /* If one type is nonsense, use the other. */
593 if (t1 == error_mark_node)
594 return t2;
595 if (t2 == error_mark_node)
596 return t1;
598 /* Merge the attributes. */
599 attributes = (*targetm.merge_type_attributes) (t1, t2);
601 if (TYPE_PTRMEMFUNC_P (t1))
602 t1 = TYPE_PTRMEMFUNC_FN_TYPE (t1);
603 if (TYPE_PTRMEMFUNC_P (t2))
604 t2 = TYPE_PTRMEMFUNC_FN_TYPE (t2);
606 code1 = TREE_CODE (t1);
607 code2 = TREE_CODE (t2);
609 switch (code1)
611 case POINTER_TYPE:
612 case REFERENCE_TYPE:
613 /* For two pointers, do this recursively on the target type. */
615 tree target = merge_types (TREE_TYPE (t1), TREE_TYPE (t2));
616 int quals = cp_type_quals (t1);
618 if (code1 == POINTER_TYPE)
619 t1 = build_pointer_type (target);
620 else
621 t1 = build_reference_type (target);
622 t1 = build_type_attribute_variant (t1, attributes);
623 t1 = cp_build_qualified_type (t1, quals);
625 if (TREE_CODE (target) == METHOD_TYPE)
626 t1 = build_ptrmemfunc_type (t1);
628 return t1;
631 case OFFSET_TYPE:
633 int quals;
634 tree pointee;
635 quals = cp_type_quals (t1);
636 pointee = merge_types (TYPE_PTRMEM_POINTED_TO_TYPE (t1),
637 TYPE_PTRMEM_POINTED_TO_TYPE (t2));
638 t1 = build_ptrmem_type (TYPE_PTRMEM_CLASS_TYPE (t1),
639 pointee);
640 t1 = cp_build_qualified_type (t1, quals);
641 break;
644 case ARRAY_TYPE:
646 tree elt = merge_types (TREE_TYPE (t1), TREE_TYPE (t2));
647 /* Save space: see if the result is identical to one of the args. */
648 if (elt == TREE_TYPE (t1) && TYPE_DOMAIN (t1))
649 return build_type_attribute_variant (t1, attributes);
650 if (elt == TREE_TYPE (t2) && TYPE_DOMAIN (t2))
651 return build_type_attribute_variant (t2, attributes);
652 /* Merge the element types, and have a size if either arg has one. */
653 t1 = build_cplus_array_type
654 (elt, TYPE_DOMAIN (TYPE_DOMAIN (t1) ? t1 : t2));
655 break;
658 case FUNCTION_TYPE:
659 /* Function types: prefer the one that specified arg types.
660 If both do, merge the arg types. Also merge the return types. */
662 tree valtype = merge_types (TREE_TYPE (t1), TREE_TYPE (t2));
663 tree p1 = TYPE_ARG_TYPES (t1);
664 tree p2 = TYPE_ARG_TYPES (t2);
665 tree rval, raises;
667 /* Save space: see if the result is identical to one of the args. */
668 if (valtype == TREE_TYPE (t1) && ! p2)
669 return build_type_attribute_variant (t1, attributes);
670 if (valtype == TREE_TYPE (t2) && ! p1)
671 return build_type_attribute_variant (t2, attributes);
673 /* Simple way if one arg fails to specify argument types. */
674 if (p1 == NULL_TREE || TREE_VALUE (p1) == void_type_node)
676 rval = build_function_type (valtype, p2);
677 if ((raises = TYPE_RAISES_EXCEPTIONS (t2)))
678 rval = build_exception_variant (rval, raises);
679 return build_type_attribute_variant (rval, attributes);
681 raises = TYPE_RAISES_EXCEPTIONS (t1);
682 if (p2 == NULL_TREE || TREE_VALUE (p2) == void_type_node)
684 rval = build_function_type (valtype, p1);
685 if (raises)
686 rval = build_exception_variant (rval, raises);
687 return build_type_attribute_variant (rval, attributes);
690 rval = build_function_type (valtype, commonparms (p1, p2));
691 t1 = build_exception_variant (rval, raises);
692 break;
695 case METHOD_TYPE:
697 /* Get this value the long way, since TYPE_METHOD_BASETYPE
698 is just the main variant of this. */
699 tree basetype = TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (t2)));
700 tree raises = TYPE_RAISES_EXCEPTIONS (t1);
701 tree t3;
703 /* If this was a member function type, get back to the
704 original type of type member function (i.e., without
705 the class instance variable up front. */
706 t1 = build_function_type (TREE_TYPE (t1),
707 TREE_CHAIN (TYPE_ARG_TYPES (t1)));
708 t2 = build_function_type (TREE_TYPE (t2),
709 TREE_CHAIN (TYPE_ARG_TYPES (t2)));
710 t3 = merge_types (t1, t2);
711 t3 = build_method_type_directly (basetype, TREE_TYPE (t3),
712 TYPE_ARG_TYPES (t3));
713 t1 = build_exception_variant (t3, raises);
714 break;
717 default:;
719 return build_type_attribute_variant (t1, attributes);
722 /* Return the common type of two types.
723 We assume that comptypes has already been done and returned 1;
724 if that isn't so, this may crash.
726 This is the type for the result of most arithmetic operations
727 if the operands have the given two types. */
729 tree
730 common_type (tree t1, tree t2)
732 enum tree_code code1;
733 enum tree_code code2;
735 /* If one type is nonsense, bail. */
736 if (t1 == error_mark_node || t2 == error_mark_node)
737 return error_mark_node;
739 code1 = TREE_CODE (t1);
740 code2 = TREE_CODE (t2);
742 if ((ARITHMETIC_TYPE_P (t1) || code1 == ENUMERAL_TYPE
743 || code1 == COMPLEX_TYPE)
744 && (ARITHMETIC_TYPE_P (t2) || code2 == ENUMERAL_TYPE
745 || code2 == COMPLEX_TYPE))
746 return type_after_usual_arithmetic_conversions (t1, t2);
748 else if ((TYPE_PTR_P (t1) && TYPE_PTR_P (t2))
749 || (TYPE_PTRMEM_P (t1) && TYPE_PTRMEM_P (t2))
750 || (TYPE_PTRMEMFUNC_P (t1) && TYPE_PTRMEMFUNC_P (t2)))
751 return composite_pointer_type (t1, t2, error_mark_node, error_mark_node,
752 "conversion");
753 else
754 abort ();
757 /* Compare two exception specifier types for exactness or subsetness, if
758 allowed. Returns false for mismatch, true for match (same, or
759 derived and !exact).
761 [except.spec] "If a class X ... objects of class X or any class publicly
762 and unambiguously derived from X. Similarly, if a pointer type Y * ...
763 exceptions of type Y * or that are pointers to any type publicly and
764 unambiguously derived from Y. Otherwise a function only allows exceptions
765 that have the same type ..."
766 This does not mention cv qualifiers and is different to what throw
767 [except.throw] and catch [except.catch] will do. They will ignore the
768 top level cv qualifiers, and allow qualifiers in the pointer to class
769 example.
771 We implement the letter of the standard. */
773 static bool
774 comp_except_types (tree a, tree b, bool exact)
776 if (same_type_p (a, b))
777 return true;
778 else if (!exact)
780 if (cp_type_quals (a) || cp_type_quals (b))
781 return false;
783 if (TREE_CODE (a) == POINTER_TYPE
784 && TREE_CODE (b) == POINTER_TYPE)
786 a = TREE_TYPE (a);
787 b = TREE_TYPE (b);
788 if (cp_type_quals (a) || cp_type_quals (b))
789 return false;
792 if (TREE_CODE (a) != RECORD_TYPE
793 || TREE_CODE (b) != RECORD_TYPE)
794 return false;
796 if (ACCESSIBLY_UNIQUELY_DERIVED_P (a, b))
797 return true;
799 return false;
802 /* Return true if TYPE1 and TYPE2 are equivalent exception specifiers.
803 If EXACT is false, T2 can be stricter than T1 (according to 15.4/7),
804 otherwise it must be exact. Exception lists are unordered, but
805 we've already filtered out duplicates. Most lists will be in order,
806 we should try to make use of that. */
808 bool
809 comp_except_specs (tree t1, tree t2, bool exact)
811 tree probe;
812 tree base;
813 int length = 0;
815 if (t1 == t2)
816 return true;
818 if (t1 == NULL_TREE) /* T1 is ... */
819 return t2 == NULL_TREE || !exact;
820 if (!TREE_VALUE (t1)) /* t1 is EMPTY */
821 return t2 != NULL_TREE && !TREE_VALUE (t2);
822 if (t2 == NULL_TREE) /* T2 is ... */
823 return false;
824 if (TREE_VALUE (t1) && !TREE_VALUE (t2)) /* T2 is EMPTY, T1 is not */
825 return !exact;
827 /* Neither set is ... or EMPTY, make sure each part of T2 is in T1.
828 Count how many we find, to determine exactness. For exact matching and
829 ordered T1, T2, this is an O(n) operation, otherwise its worst case is
830 O(nm). */
831 for (base = t1; t2 != NULL_TREE; t2 = TREE_CHAIN (t2))
833 for (probe = base; probe != NULL_TREE; probe = TREE_CHAIN (probe))
835 tree a = TREE_VALUE (probe);
836 tree b = TREE_VALUE (t2);
838 if (comp_except_types (a, b, exact))
840 if (probe == base && exact)
841 base = TREE_CHAIN (probe);
842 length++;
843 break;
846 if (probe == NULL_TREE)
847 return false;
849 return !exact || base == NULL_TREE || length == list_length (t1);
852 /* Compare the array types T1 and T2. ALLOW_REDECLARATION is true if
853 [] can match [size]. */
855 static bool
856 comp_array_types (tree t1, tree t2, bool allow_redeclaration)
858 tree d1;
859 tree d2;
861 if (t1 == t2)
862 return true;
864 /* The type of the array elements must be the same. */
865 if (!same_type_p (TREE_TYPE (t1), TREE_TYPE (t2)))
866 return false;
868 d1 = TYPE_DOMAIN (t1);
869 d2 = TYPE_DOMAIN (t2);
871 if (d1 == d2)
872 return true;
874 /* If one of the arrays is dimensionless, and the other has a
875 dimension, they are of different types. However, it is valid to
876 write:
878 extern int a[];
879 int a[3];
881 by [basic.link]:
883 declarations for an array object can specify
884 array types that differ by the presence or absence of a major
885 array bound (_dcl.array_). */
886 if (!d1 || !d2)
887 return allow_redeclaration;
889 /* Check that the dimensions are the same. */
890 return (cp_tree_equal (TYPE_MIN_VALUE (d1), TYPE_MIN_VALUE (d2))
891 && cp_tree_equal (TYPE_MAX_VALUE (d1), TYPE_MAX_VALUE (d2)));
894 /* Return true if T1 and T2 are related as allowed by STRICT. STRICT
895 is a bitwise-or of the COMPARE_* flags. */
897 bool
898 comptypes (tree t1, tree t2, int strict)
900 if (t1 == t2)
901 return true;
903 /* This should never happen. */
904 my_friendly_assert (t1 != error_mark_node, 307);
906 /* Suppress errors caused by previously reported errors */
907 if (t2 == error_mark_node)
908 return false;
910 my_friendly_assert (TYPE_P (t1) && TYPE_P (t2), 20030623);
912 /* TYPENAME_TYPEs should be resolved if the qualifying scope is the
913 current instantiation. */
914 if (TREE_CODE (t1) == TYPENAME_TYPE)
916 tree resolved = resolve_typename_type (t1, /*only_current_p=*/true);
918 if (resolved != error_mark_node)
919 t1 = resolved;
922 if (TREE_CODE (t2) == TYPENAME_TYPE)
924 tree resolved = resolve_typename_type (t2, /*only_current_p=*/true);
926 if (resolved != error_mark_node)
927 t2 = resolved;
930 /* If either type is the internal version of sizetype, use the
931 language version. */
932 if (TREE_CODE (t1) == INTEGER_TYPE && TYPE_IS_SIZETYPE (t1)
933 && TYPE_DOMAIN (t1))
934 t1 = TYPE_DOMAIN (t1);
936 if (TREE_CODE (t2) == INTEGER_TYPE && TYPE_IS_SIZETYPE (t2)
937 && TYPE_DOMAIN (t2))
938 t2 = TYPE_DOMAIN (t2);
940 if (TYPE_PTRMEMFUNC_P (t1))
941 t1 = TYPE_PTRMEMFUNC_FN_TYPE (t1);
942 if (TYPE_PTRMEMFUNC_P (t2))
943 t2 = TYPE_PTRMEMFUNC_FN_TYPE (t2);
945 /* Different classes of types can't be compatible. */
946 if (TREE_CODE (t1) != TREE_CODE (t2))
947 return false;
949 /* Qualifiers must match. */
950 if (cp_type_quals (t1) != cp_type_quals (t2))
951 return false;
952 if (TYPE_FOR_JAVA (t1) != TYPE_FOR_JAVA (t2))
953 return false;
955 /* Allow for two different type nodes which have essentially the same
956 definition. Note that we already checked for equality of the type
957 qualifiers (just above). */
959 if (TYPE_MAIN_VARIANT (t1) == TYPE_MAIN_VARIANT (t2))
960 return true;
962 if (!(*targetm.comp_type_attributes) (t1, t2))
963 return false;
965 switch (TREE_CODE (t1))
967 case TEMPLATE_TEMPLATE_PARM:
968 case BOUND_TEMPLATE_TEMPLATE_PARM:
969 if (TEMPLATE_TYPE_IDX (t1) != TEMPLATE_TYPE_IDX (t2)
970 || TEMPLATE_TYPE_LEVEL (t1) != TEMPLATE_TYPE_LEVEL (t2))
971 return false;
972 if (!comp_template_parms
973 (DECL_TEMPLATE_PARMS (TEMPLATE_TEMPLATE_PARM_TEMPLATE_DECL (t1)),
974 DECL_TEMPLATE_PARMS (TEMPLATE_TEMPLATE_PARM_TEMPLATE_DECL (t2))))
975 return false;
976 if (TREE_CODE (t1) == TEMPLATE_TEMPLATE_PARM)
977 return true;
978 /* Don't check inheritance. */
979 strict = COMPARE_STRICT;
980 /* fall through */
982 case RECORD_TYPE:
983 case UNION_TYPE:
984 if (TYPE_TEMPLATE_INFO (t1) && TYPE_TEMPLATE_INFO (t2)
985 && (TYPE_TI_TEMPLATE (t1) == TYPE_TI_TEMPLATE (t2)
986 || TREE_CODE (t1) == BOUND_TEMPLATE_TEMPLATE_PARM)
987 && comp_template_args (TYPE_TI_ARGS (t1), TYPE_TI_ARGS (t2)))
988 return true;
990 if ((strict & COMPARE_BASE) && DERIVED_FROM_P (t1, t2))
991 return true;
992 else if ((strict & COMPARE_DERIVED) && DERIVED_FROM_P (t2, t1))
993 return true;
995 return false;
997 case OFFSET_TYPE:
998 if (!comptypes (TYPE_OFFSET_BASETYPE (t1), TYPE_OFFSET_BASETYPE (t2),
999 strict & ~COMPARE_REDECLARATION))
1000 return false;
1001 /* FALLTHROUGH*/
1003 case POINTER_TYPE:
1004 case REFERENCE_TYPE:
1005 return same_type_p (TREE_TYPE (t1), TREE_TYPE (t2));
1007 case METHOD_TYPE:
1008 case FUNCTION_TYPE:
1009 if (!same_type_p (TREE_TYPE (t1), TREE_TYPE (t2)))
1010 return false;
1011 return compparms (TYPE_ARG_TYPES (t1), TYPE_ARG_TYPES (t2));
1013 case ARRAY_TYPE:
1014 /* Target types must match incl. qualifiers. */
1015 return comp_array_types (t1, t2, !!(strict & COMPARE_REDECLARATION));
1017 case TEMPLATE_TYPE_PARM:
1018 return (TEMPLATE_TYPE_IDX (t1) == TEMPLATE_TYPE_IDX (t2)
1019 && TEMPLATE_TYPE_LEVEL (t1) == TEMPLATE_TYPE_LEVEL (t2));
1021 case TYPENAME_TYPE:
1022 if (!cp_tree_equal (TYPENAME_TYPE_FULLNAME (t1),
1023 TYPENAME_TYPE_FULLNAME (t2)))
1024 return false;
1025 return same_type_p (TYPE_CONTEXT (t1), TYPE_CONTEXT (t2));
1027 case UNBOUND_CLASS_TEMPLATE:
1028 if (!cp_tree_equal (TYPE_IDENTIFIER (t1), TYPE_IDENTIFIER (t2)))
1029 return false;
1030 return same_type_p (TYPE_CONTEXT (t1), TYPE_CONTEXT (t2));
1032 case COMPLEX_TYPE:
1033 return same_type_p (TREE_TYPE (t1), TREE_TYPE (t2));
1035 default:
1036 break;
1038 return false;
1041 /* Returns 1 if TYPE1 is at least as qualified as TYPE2. */
1043 bool
1044 at_least_as_qualified_p (tree type1, tree type2)
1046 int q1 = cp_type_quals (type1);
1047 int q2 = cp_type_quals (type2);
1049 /* All qualifiers for TYPE2 must also appear in TYPE1. */
1050 return (q1 & q2) == q2;
1053 /* Returns 1 if TYPE1 is more qualified than TYPE2. */
1055 bool
1056 more_qualified_p (tree type1, tree type2)
1058 int q1 = cp_type_quals (type1);
1059 int q2 = cp_type_quals (type2);
1061 return q1 != q2 && (q1 & q2) == q2;
1064 /* Returns 1 if TYPE1 is more cv-qualified than TYPE2, -1 if TYPE2 is
1065 more cv-qualified that TYPE1, and 0 otherwise. */
1068 comp_cv_qualification (tree type1, tree type2)
1070 int q1 = cp_type_quals (type1);
1071 int q2 = cp_type_quals (type2);
1073 if (q1 == q2)
1074 return 0;
1076 if ((q1 & q2) == q2)
1077 return 1;
1078 else if ((q1 & q2) == q1)
1079 return -1;
1081 return 0;
1084 /* Returns 1 if the cv-qualification signature of TYPE1 is a proper
1085 subset of the cv-qualification signature of TYPE2, and the types
1086 are similar. Returns -1 if the other way 'round, and 0 otherwise. */
1089 comp_cv_qual_signature (tree type1, tree type2)
1091 if (comp_ptr_ttypes_real (type2, type1, -1))
1092 return 1;
1093 else if (comp_ptr_ttypes_real (type1, type2, -1))
1094 return -1;
1095 else
1096 return 0;
1099 /* If two types share a common base type, return that basetype.
1100 If there is not a unique most-derived base type, this function
1101 returns ERROR_MARK_NODE. */
1103 static tree
1104 common_base_type (tree tt1, tree tt2)
1106 tree best = NULL_TREE;
1107 int i;
1109 /* If one is a baseclass of another, that's good enough. */
1110 if (UNIQUELY_DERIVED_FROM_P (tt1, tt2))
1111 return tt1;
1112 if (UNIQUELY_DERIVED_FROM_P (tt2, tt1))
1113 return tt2;
1115 /* Otherwise, try to find a unique baseclass of TT1
1116 that is shared by TT2, and follow that down. */
1117 for (i = CLASSTYPE_N_BASECLASSES (tt1)-1; i >= 0; i--)
1119 tree basetype = TYPE_BINFO_BASETYPE (tt1, i);
1120 tree trial = common_base_type (basetype, tt2);
1121 if (trial)
1123 if (trial == error_mark_node)
1124 return trial;
1125 if (best == NULL_TREE)
1126 best = trial;
1127 else if (best != trial)
1128 return error_mark_node;
1132 /* Same for TT2. */
1133 for (i = CLASSTYPE_N_BASECLASSES (tt2)-1; i >= 0; i--)
1135 tree basetype = TYPE_BINFO_BASETYPE (tt2, i);
1136 tree trial = common_base_type (tt1, basetype);
1137 if (trial)
1139 if (trial == error_mark_node)
1140 return trial;
1141 if (best == NULL_TREE)
1142 best = trial;
1143 else if (best != trial)
1144 return error_mark_node;
1147 return best;
1150 /* Subroutines of `comptypes'. */
1152 /* Return true if two parameter type lists PARMS1 and PARMS2 are
1153 equivalent in the sense that functions with those parameter types
1154 can have equivalent types. The two lists must be equivalent,
1155 element by element. */
1157 bool
1158 compparms (tree parms1, tree parms2)
1160 tree t1, t2;
1162 /* An unspecified parmlist matches any specified parmlist
1163 whose argument types don't need default promotions. */
1165 for (t1 = parms1, t2 = parms2;
1166 t1 || t2;
1167 t1 = TREE_CHAIN (t1), t2 = TREE_CHAIN (t2))
1169 /* If one parmlist is shorter than the other,
1170 they fail to match. */
1171 if (!t1 || !t2)
1172 return false;
1173 if (!same_type_p (TREE_VALUE (t2), TREE_VALUE (t1)))
1174 return false;
1176 return true;
1180 tree
1181 cxx_sizeof_or_alignof_type (tree type, enum tree_code op, int complain)
1183 enum tree_code type_code;
1184 tree value;
1185 const char *op_name;
1187 my_friendly_assert (op == SIZEOF_EXPR || op == ALIGNOF_EXPR, 20020720);
1188 if (processing_template_decl)
1189 return build_min (op, size_type_node, type);
1191 op_name = operator_name_info[(int) op].name;
1193 type = non_reference (type);
1194 type_code = TREE_CODE (type);
1196 if (type_code == METHOD_TYPE)
1198 if (complain && (pedantic || warn_pointer_arith))
1199 pedwarn ("invalid application of `%s' to a member function", op_name);
1200 value = size_one_node;
1202 else
1203 value = c_sizeof_or_alignof_type (complete_type (type), op, complain);
1205 return value;
1208 tree
1209 expr_sizeof (tree e)
1211 if (processing_template_decl)
1212 return build_min (SIZEOF_EXPR, size_type_node, e);
1214 if (TREE_CODE (e) == COMPONENT_REF
1215 && DECL_C_BIT_FIELD (TREE_OPERAND (e, 1)))
1216 error ("sizeof applied to a bit-field");
1217 if (is_overloaded_fn (e))
1219 pedwarn ("ISO C++ forbids applying `sizeof' to an expression of function type");
1220 return c_sizeof (char_type_node);
1222 else if (type_unknown_p (e))
1224 cxx_incomplete_type_error (e, TREE_TYPE (e));
1225 return c_sizeof (char_type_node);
1228 if (e == error_mark_node)
1229 return e;
1231 return cxx_sizeof (TREE_TYPE (e));
1235 /* Perform the conversions in [expr] that apply when an lvalue appears
1236 in an rvalue context: the lvalue-to-rvalue, array-to-pointer, and
1237 function-to-pointer conversions.
1239 In addition manifest constants are replaced by their values. */
1241 tree
1242 decay_conversion (tree exp)
1244 register tree type;
1245 register enum tree_code code;
1247 type = TREE_TYPE (exp);
1248 code = TREE_CODE (type);
1250 if (code == REFERENCE_TYPE)
1252 exp = convert_from_reference (exp);
1253 type = TREE_TYPE (exp);
1254 code = TREE_CODE (type);
1257 if (type == error_mark_node)
1258 return error_mark_node;
1260 if (type_unknown_p (exp))
1262 cxx_incomplete_type_error (exp, TREE_TYPE (exp));
1263 return error_mark_node;
1266 /* Constants can be used directly unless they're not loadable. */
1267 if (TREE_CODE (exp) == CONST_DECL)
1268 exp = DECL_INITIAL (exp);
1269 /* Replace a nonvolatile const static variable with its value. We
1270 don't do this for arrays, though; we want the address of the
1271 first element of the array, not the address of the first element
1272 of its initializing constant. */
1273 else if (code != ARRAY_TYPE)
1275 exp = decl_constant_value (exp);
1276 type = TREE_TYPE (exp);
1279 /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
1280 Leave such NOP_EXPRs, since RHS is being used in non-lvalue context. */
1282 if (code == VOID_TYPE)
1284 error ("void value not ignored as it ought to be");
1285 return error_mark_node;
1287 if (code == METHOD_TYPE)
1289 error ("invalid use of non-static member function");
1290 return error_mark_node;
1292 if (code == FUNCTION_TYPE || is_overloaded_fn (exp))
1293 return build_unary_op (ADDR_EXPR, exp, 0);
1294 if (code == ARRAY_TYPE)
1296 register tree adr;
1297 tree ptrtype;
1299 if (TREE_CODE (exp) == INDIRECT_REF)
1300 return build_nop (build_pointer_type (TREE_TYPE (type)),
1301 TREE_OPERAND (exp, 0));
1303 if (TREE_CODE (exp) == COMPOUND_EXPR)
1305 tree op1 = decay_conversion (TREE_OPERAND (exp, 1));
1306 return build (COMPOUND_EXPR, TREE_TYPE (op1),
1307 TREE_OPERAND (exp, 0), op1);
1310 if (!lvalue_p (exp)
1311 && ! (TREE_CODE (exp) == CONSTRUCTOR && TREE_STATIC (exp)))
1313 error ("invalid use of non-lvalue array");
1314 return error_mark_node;
1317 ptrtype = build_pointer_type (TREE_TYPE (type));
1319 if (TREE_CODE (exp) == VAR_DECL)
1321 /* ??? This is not really quite correct
1322 in that the type of the operand of ADDR_EXPR
1323 is not the target type of the type of the ADDR_EXPR itself.
1324 Question is, can this lossage be avoided? */
1325 adr = build1 (ADDR_EXPR, ptrtype, exp);
1326 if (!cxx_mark_addressable (exp))
1327 return error_mark_node;
1328 TREE_CONSTANT (adr) = staticp (exp);
1329 TREE_SIDE_EFFECTS (adr) = 0; /* Default would be, same as EXP. */
1330 return adr;
1332 /* This way is better for a COMPONENT_REF since it can
1333 simplify the offset for a component. */
1334 adr = build_unary_op (ADDR_EXPR, exp, 1);
1335 return cp_convert (ptrtype, adr);
1338 /* [basic.lval]: Class rvalues can have cv-qualified types; non-class
1339 rvalues always have cv-unqualified types. */
1340 if (! CLASS_TYPE_P (type))
1341 exp = cp_convert (TYPE_MAIN_VARIANT (type), exp);
1343 return exp;
1346 tree
1347 default_conversion (tree exp)
1349 exp = decay_conversion (exp);
1351 if (INTEGRAL_OR_ENUMERATION_TYPE_P (TREE_TYPE (exp)))
1352 exp = perform_integral_promotions (exp);
1354 return exp;
1357 /* EXPR is an expression with an integral or enumeration type.
1358 Perform the integral promotions in [conv.prom], and return the
1359 converted value. */
1361 tree
1362 perform_integral_promotions (tree expr)
1364 tree type;
1365 tree promoted_type;
1367 type = TREE_TYPE (expr);
1368 my_friendly_assert (INTEGRAL_OR_ENUMERATION_TYPE_P (type), 20030703);
1369 promoted_type = type_promotes_to (type);
1370 if (type != promoted_type)
1371 expr = cp_convert (promoted_type, expr);
1372 return expr;
1375 /* Take the address of an inline function without setting TREE_ADDRESSABLE
1376 or TREE_USED. */
1378 tree
1379 inline_conversion (tree exp)
1381 if (TREE_CODE (exp) == FUNCTION_DECL)
1382 exp = build1 (ADDR_EXPR, build_pointer_type (TREE_TYPE (exp)), exp);
1384 return exp;
1387 /* Returns nonzero iff exp is a STRING_CST or the result of applying
1388 decay_conversion to one. */
1391 string_conv_p (tree totype, tree exp, int warn)
1393 tree t;
1395 if (! flag_const_strings || TREE_CODE (totype) != POINTER_TYPE)
1396 return 0;
1398 t = TREE_TYPE (totype);
1399 if (!same_type_p (t, char_type_node)
1400 && !same_type_p (t, wchar_type_node))
1401 return 0;
1403 if (TREE_CODE (exp) == STRING_CST)
1405 /* Make sure that we don't try to convert between char and wchar_t. */
1406 if (!same_type_p (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (exp))), t))
1407 return 0;
1409 else
1411 /* Is this a string constant which has decayed to 'const char *'? */
1412 t = build_pointer_type (build_qualified_type (t, TYPE_QUAL_CONST));
1413 if (!same_type_p (TREE_TYPE (exp), t))
1414 return 0;
1415 STRIP_NOPS (exp);
1416 if (TREE_CODE (exp) != ADDR_EXPR
1417 || TREE_CODE (TREE_OPERAND (exp, 0)) != STRING_CST)
1418 return 0;
1421 /* This warning is not very useful, as it complains about printf. */
1422 if (warn && warn_write_strings)
1423 warning ("deprecated conversion from string constant to `%T'", totype);
1425 return 1;
1428 /* Given a COND_EXPR, MIN_EXPR, or MAX_EXPR in T, return it in a form that we
1429 can, for example, use as an lvalue. This code used to be in
1430 unary_complex_lvalue, but we needed it to deal with `a = (d == c) ? b : c'
1431 expressions, where we're dealing with aggregates. But now it's again only
1432 called from unary_complex_lvalue. The case (in particular) that led to
1433 this was with CODE == ADDR_EXPR, since it's not an lvalue when we'd
1434 get it there. */
1436 static tree
1437 rationalize_conditional_expr (enum tree_code code, tree t)
1439 /* For MIN_EXPR or MAX_EXPR, fold-const.c has arranged things so that
1440 the first operand is always the one to be used if both operands
1441 are equal, so we know what conditional expression this used to be. */
1442 if (TREE_CODE (t) == MIN_EXPR || TREE_CODE (t) == MAX_EXPR)
1444 return
1445 build_conditional_expr (build_x_binary_op ((TREE_CODE (t) == MIN_EXPR
1446 ? LE_EXPR : GE_EXPR),
1447 TREE_OPERAND (t, 0),
1448 TREE_OPERAND (t, 1)),
1449 build_unary_op (code, TREE_OPERAND (t, 0), 0),
1450 build_unary_op (code, TREE_OPERAND (t, 1), 0));
1453 return
1454 build_conditional_expr (TREE_OPERAND (t, 0),
1455 build_unary_op (code, TREE_OPERAND (t, 1), 0),
1456 build_unary_op (code, TREE_OPERAND (t, 2), 0));
1459 /* Given the TYPE of an anonymous union field inside T, return the
1460 FIELD_DECL for the field. If not found return NULL_TREE. Because
1461 anonymous unions can nest, we must also search all anonymous unions
1462 that are directly reachable. */
1464 static tree
1465 lookup_anon_field (tree t, tree type)
1467 tree field;
1469 for (field = TYPE_FIELDS (t); field; field = TREE_CHAIN (field))
1471 if (TREE_STATIC (field))
1472 continue;
1473 if (TREE_CODE (field) != FIELD_DECL || DECL_ARTIFICIAL (field))
1474 continue;
1476 /* If we find it directly, return the field. */
1477 if (DECL_NAME (field) == NULL_TREE
1478 && type == TYPE_MAIN_VARIANT (TREE_TYPE (field)))
1480 return field;
1483 /* Otherwise, it could be nested, search harder. */
1484 if (DECL_NAME (field) == NULL_TREE
1485 && ANON_AGGR_TYPE_P (TREE_TYPE (field)))
1487 tree subfield = lookup_anon_field (TREE_TYPE (field), type);
1488 if (subfield)
1489 return subfield;
1492 return NULL_TREE;
1495 /* Build an expression representing OBJECT.MEMBER. OBJECT is an
1496 expression; MEMBER is a DECL or baselink. If ACCESS_PATH is
1497 non-NULL, it indicates the path to the base used to name MEMBER.
1498 If PRESERVE_REFERENCE is true, the expression returned will have
1499 REFERENCE_TYPE if the MEMBER does. Otherwise, the expression
1500 returned will have the type referred to by the reference.
1502 This function does not perform access control; that is either done
1503 earlier by the parser when the name of MEMBER is resolved to MEMBER
1504 itself, or later when overload resolution selects one of the
1505 functions indicated by MEMBER. */
1507 tree
1508 build_class_member_access_expr (tree object, tree member,
1509 tree access_path, bool preserve_reference)
1511 tree object_type;
1512 tree member_scope;
1513 tree result = NULL_TREE;
1515 if (object == error_mark_node || member == error_mark_node)
1516 return error_mark_node;
1518 if (TREE_CODE (member) == PSEUDO_DTOR_EXPR)
1519 return member;
1521 my_friendly_assert (DECL_P (member) || BASELINK_P (member),
1522 20020801);
1524 /* [expr.ref]
1526 The type of the first expression shall be "class object" (of a
1527 complete type). */
1528 object_type = TREE_TYPE (object);
1529 if (!complete_type_or_else (object_type, object))
1530 return error_mark_node;
1531 if (!CLASS_TYPE_P (object_type))
1533 error ("request for member `%D' in `%E', which is of non-class type `%T'",
1534 member, object, object_type);
1535 return error_mark_node;
1538 /* The standard does not seem to actually say that MEMBER must be a
1539 member of OBJECT_TYPE. However, that is clearly what is
1540 intended. */
1541 if (DECL_P (member))
1543 member_scope = DECL_CLASS_CONTEXT (member);
1544 mark_used (member);
1545 if (TREE_DEPRECATED (member))
1546 warn_deprecated_use (member);
1548 else
1549 member_scope = BINFO_TYPE (BASELINK_BINFO (member));
1550 /* If MEMBER is from an anonymous aggregate, MEMBER_SCOPE will
1551 presently be the anonymous union. Go outwards until we find a
1552 type related to OBJECT_TYPE. */
1553 while (ANON_AGGR_TYPE_P (member_scope)
1554 && !same_type_ignoring_top_level_qualifiers_p (member_scope,
1555 object_type))
1556 member_scope = TYPE_CONTEXT (member_scope);
1557 if (!member_scope || !DERIVED_FROM_P (member_scope, object_type))
1559 if (TREE_CODE (member) == FIELD_DECL)
1560 error ("invalid use of nonstatic data member '%E'", member);
1561 else
1562 error ("`%D' is not a member of `%T'", member, object_type);
1563 return error_mark_node;
1566 /* Transform `(a, b).x' into `(*(a, &b)).x', `(a ? b : c).x' into
1567 `(*(a ? &b : &c)).x', and so on. A COND_EXPR is only an lvalue
1568 in the frontend; only _DECLs and _REFs are lvalues in the backend. */
1570 tree temp = unary_complex_lvalue (ADDR_EXPR, object);
1571 if (temp)
1572 object = build_indirect_ref (temp, NULL);
1575 /* In [expr.ref], there is an explicit list of the valid choices for
1576 MEMBER. We check for each of those cases here. */
1577 if (TREE_CODE (member) == VAR_DECL)
1579 /* A static data member. */
1580 result = member;
1581 /* If OBJECT has side-effects, they are supposed to occur. */
1582 if (TREE_SIDE_EFFECTS (object))
1583 result = build (COMPOUND_EXPR, TREE_TYPE (result), object, result);
1585 else if (TREE_CODE (member) == FIELD_DECL)
1587 /* A non-static data member. */
1588 bool null_object_p;
1589 int type_quals;
1590 tree member_type;
1592 null_object_p = (TREE_CODE (object) == INDIRECT_REF
1593 && integer_zerop (TREE_OPERAND (object, 0)));
1595 /* Convert OBJECT to the type of MEMBER. */
1596 if (!same_type_p (TYPE_MAIN_VARIANT (object_type),
1597 TYPE_MAIN_VARIANT (member_scope)))
1599 tree binfo;
1600 base_kind kind;
1602 binfo = lookup_base (access_path ? access_path : object_type,
1603 member_scope, ba_ignore, &kind);
1604 if (binfo == error_mark_node)
1605 return error_mark_node;
1607 /* It is invalid to try to get to a virtual base of a
1608 NULL object. The most common cause is invalid use of
1609 offsetof macro. */
1610 if (null_object_p && kind == bk_via_virtual)
1612 error ("invalid access to non-static data member `%D' of NULL object",
1613 member);
1614 error ("(perhaps the `offsetof' macro was used incorrectly)");
1615 return error_mark_node;
1618 /* Convert to the base. */
1619 object = build_base_path (PLUS_EXPR, object, binfo,
1620 /*nonnull=*/1);
1621 /* If we found the base successfully then we should be able
1622 to convert to it successfully. */
1623 my_friendly_assert (object != error_mark_node,
1624 20020801);
1627 /* Complain about other invalid uses of offsetof, even though they will
1628 give the right answer. Note that we complain whether or not they
1629 actually used the offsetof macro, since there's no way to know at this
1630 point. So we just give a warning, instead of a pedwarn. */
1631 if (null_object_p && warn_invalid_offsetof
1632 && CLASSTYPE_NON_POD_P (object_type))
1634 warning ("invalid access to non-static data member `%D' of NULL object",
1635 member);
1636 warning ("(perhaps the `offsetof' macro was used incorrectly)");
1639 /* If MEMBER is from an anonymous aggregate, we have converted
1640 OBJECT so that it refers to the class containing the
1641 anonymous union. Generate a reference to the anonymous union
1642 itself, and recur to find MEMBER. */
1643 if (ANON_AGGR_TYPE_P (DECL_CONTEXT (member))
1644 /* When this code is called from build_field_call, the
1645 object already has the type of the anonymous union.
1646 That is because the COMPONENT_REF was already
1647 constructed, and was then disassembled before calling
1648 build_field_call. After the function-call code is
1649 cleaned up, this waste can be eliminated. */
1650 && (!same_type_ignoring_top_level_qualifiers_p
1651 (TREE_TYPE (object), DECL_CONTEXT (member))))
1653 tree anonymous_union;
1655 anonymous_union = lookup_anon_field (TREE_TYPE (object),
1656 DECL_CONTEXT (member));
1657 object = build_class_member_access_expr (object,
1658 anonymous_union,
1659 /*access_path=*/NULL_TREE,
1660 preserve_reference);
1663 /* Compute the type of the field, as described in [expr.ref]. */
1664 type_quals = TYPE_UNQUALIFIED;
1665 member_type = TREE_TYPE (member);
1666 if (TREE_CODE (member_type) != REFERENCE_TYPE)
1668 type_quals = (cp_type_quals (member_type)
1669 | cp_type_quals (object_type));
1671 /* A field is const (volatile) if the enclosing object, or the
1672 field itself, is const (volatile). But, a mutable field is
1673 not const, even within a const object. */
1674 if (DECL_MUTABLE_P (member))
1675 type_quals &= ~TYPE_QUAL_CONST;
1676 member_type = cp_build_qualified_type (member_type, type_quals);
1679 result = fold (build (COMPONENT_REF, member_type, object, member));
1681 /* Mark the expression const or volatile, as appropriate. Even
1682 though we've dealt with the type above, we still have to mark the
1683 expression itself. */
1684 if (type_quals & TYPE_QUAL_CONST)
1685 TREE_READONLY (result) = 1;
1686 else if (type_quals & TYPE_QUAL_VOLATILE)
1687 TREE_THIS_VOLATILE (result) = 1;
1689 else if (BASELINK_P (member))
1691 /* The member is a (possibly overloaded) member function. */
1692 tree functions;
1693 tree type;
1695 /* If the MEMBER is exactly one static member function, then we
1696 know the type of the expression. Otherwise, we must wait
1697 until overload resolution has been performed. */
1698 functions = BASELINK_FUNCTIONS (member);
1699 if (TREE_CODE (functions) == FUNCTION_DECL
1700 && DECL_STATIC_FUNCTION_P (functions))
1701 type = TREE_TYPE (functions);
1702 else
1703 type = unknown_type_node;
1704 /* Note that we do not convert OBJECT to the BASELINK_BINFO
1705 base. That will happen when the function is called. */
1706 result = build (COMPONENT_REF, type, object, member);
1708 else if (TREE_CODE (member) == CONST_DECL)
1710 /* The member is an enumerator. */
1711 result = member;
1712 /* If OBJECT has side-effects, they are supposed to occur. */
1713 if (TREE_SIDE_EFFECTS (object))
1714 result = build (COMPOUND_EXPR, TREE_TYPE (result),
1715 object, result);
1717 else
1719 error ("invalid use of `%D'", member);
1720 return error_mark_node;
1723 if (!preserve_reference)
1724 /* [expr.ref]
1726 If E2 is declared to have type "reference to T", then ... the
1727 type of E1.E2 is T. */
1728 result = convert_from_reference (result);
1730 return result;
1733 /* Return the destructor denoted by OBJECT.SCOPE::~DTOR_NAME, or, if
1734 SCOPE is NULL, by OBJECT.~DTOR_NAME. */
1736 static tree
1737 lookup_destructor (tree object, tree scope, tree dtor_name)
1739 tree object_type = TREE_TYPE (object);
1740 tree dtor_type = TREE_OPERAND (dtor_name, 0);
1742 if (scope && !check_dtor_name (scope, dtor_name))
1744 error ("qualified type `%T' does not match destructor name `~%T'",
1745 scope, dtor_type);
1746 return error_mark_node;
1748 if (!same_type_p (dtor_type, TYPE_MAIN_VARIANT (object_type)))
1750 error ("destructor name `%T' does not match type `%T' of expression",
1751 dtor_type, object_type);
1752 return error_mark_node;
1754 if (!TYPE_HAS_DESTRUCTOR (object_type))
1755 return build (PSEUDO_DTOR_EXPR, void_type_node, object, scope,
1756 dtor_type);
1757 return lookup_member (object_type, complete_dtor_identifier,
1758 /*protect=*/1, /*want_type=*/false);
1761 /* This function is called by the parser to process a class member
1762 access expression of the form OBJECT.NAME. NAME is a node used by
1763 the parser to represent a name; it is not yet a DECL. It may,
1764 however, be a BASELINK where the BASELINK_FUNCTIONS is a
1765 TEMPLATE_ID_EXPR. Templates must be looked up by the parser, and
1766 there is no reason to do the lookup twice, so the parser keeps the
1767 BASELINK. */
1769 tree
1770 finish_class_member_access_expr (tree object, tree name)
1772 tree expr;
1773 tree object_type;
1774 tree member;
1775 tree access_path = NULL_TREE;
1776 tree orig_object = object;
1777 tree orig_name = name;
1779 if (object == error_mark_node || name == error_mark_node)
1780 return error_mark_node;
1782 object_type = TREE_TYPE (object);
1784 if (processing_template_decl)
1786 if (/* If OBJECT_TYPE is dependent, so is OBJECT.NAME. */
1787 dependent_type_p (object_type)
1788 /* If NAME is "f<args>", where either 'f' or 'args' is
1789 dependent, then the expression is dependent. */
1790 || (TREE_CODE (name) == TEMPLATE_ID_EXPR
1791 && dependent_template_id_p (TREE_OPERAND (name, 0),
1792 TREE_OPERAND (name, 1)))
1793 /* If NAME is "T::X" where "T" is dependent, then the
1794 expression is dependent. */
1795 || (TREE_CODE (name) == SCOPE_REF
1796 && TYPE_P (TREE_OPERAND (name, 0))
1797 && dependent_type_p (TREE_OPERAND (name, 0))))
1798 return build_min_nt (COMPONENT_REF, object, name);
1799 object = build_non_dependent_expr (object);
1802 if (TREE_CODE (object_type) == REFERENCE_TYPE)
1804 object = convert_from_reference (object);
1805 object_type = TREE_TYPE (object);
1808 /* [expr.ref]
1810 The type of the first expression shall be "class object" (of a
1811 complete type). */
1812 if (!complete_type_or_else (object_type, object))
1813 return error_mark_node;
1814 if (!CLASS_TYPE_P (object_type))
1816 error ("request for member `%D' in `%E', which is of non-class type `%T'",
1817 name, object, object_type);
1818 return error_mark_node;
1821 if (BASELINK_P (name))
1823 /* A member function that has already been looked up. */
1824 my_friendly_assert ((TREE_CODE (BASELINK_FUNCTIONS (name))
1825 == TEMPLATE_ID_EXPR),
1826 20020805);
1827 member = name;
1829 else
1831 bool is_template_id = false;
1832 tree template_args = NULL_TREE;
1833 tree scope;
1835 if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
1837 is_template_id = true;
1838 template_args = TREE_OPERAND (name, 1);
1839 name = TREE_OPERAND (name, 0);
1842 if (TREE_CODE (name) == SCOPE_REF)
1844 /* A qualified name. The qualifying class or namespace `S' has
1845 already been looked up; it is either a TYPE or a
1846 NAMESPACE_DECL. The member name is either an IDENTIFIER_NODE
1847 or a BIT_NOT_EXPR. */
1848 scope = TREE_OPERAND (name, 0);
1849 name = TREE_OPERAND (name, 1);
1850 my_friendly_assert ((CLASS_TYPE_P (scope)
1851 || TREE_CODE (scope) == NAMESPACE_DECL),
1852 20020804);
1853 my_friendly_assert ((TREE_CODE (name) == IDENTIFIER_NODE
1854 || TREE_CODE (name) == BIT_NOT_EXPR),
1855 20020804);
1857 /* If SCOPE is a namespace, then the qualified name does not
1858 name a member of OBJECT_TYPE. */
1859 if (TREE_CODE (scope) == NAMESPACE_DECL)
1861 error ("`%D::%D' is not a member of `%T'",
1862 scope, name, object_type);
1863 return error_mark_node;
1866 /* Find the base of OBJECT_TYPE corresponding to SCOPE. */
1867 access_path = lookup_base (object_type, scope, ba_check, NULL);
1868 if (!access_path || access_path == error_mark_node)
1869 return error_mark_node;
1871 else
1873 scope = NULL_TREE;
1874 access_path = object_type;
1877 if (TREE_CODE (name) == BIT_NOT_EXPR)
1878 member = lookup_destructor (object, scope, name);
1879 else
1881 /* Look up the member. */
1882 member = lookup_member (access_path, name, /*protect=*/1,
1883 /*want_type=*/false);
1884 if (member == NULL_TREE)
1886 error ("'%D' has no member named '%E'", object_type, name);
1887 return error_mark_node;
1889 if (member == error_mark_node)
1890 return error_mark_node;
1893 if (is_template_id)
1895 tree template = member;
1897 if (BASELINK_P (template))
1898 template = lookup_template_function (template, template_args);
1899 else
1901 error ("`%D' is not a member template function", name);
1902 return error_mark_node;
1907 if (TREE_DEPRECATED (member))
1908 warn_deprecated_use (member);
1910 expr = build_class_member_access_expr (object, member, access_path,
1911 /*preserve_reference=*/false);
1912 if (processing_template_decl && expr != error_mark_node)
1913 return build_min (COMPONENT_REF, TREE_TYPE (expr), orig_object,
1914 orig_name);
1915 return expr;
1918 /* Return an expression for the MEMBER_NAME field in the internal
1919 representation of PTRMEM, a pointer-to-member function. (Each
1920 pointer-to-member function type gets its own RECORD_TYPE so it is
1921 more convenient to access the fields by name than by FIELD_DECL.)
1922 This routine converts the NAME to a FIELD_DECL and then creates the
1923 node for the complete expression. */
1925 tree
1926 build_ptrmemfunc_access_expr (tree ptrmem, tree member_name)
1928 tree ptrmem_type;
1929 tree member;
1930 tree member_type;
1932 /* This code is a stripped down version of
1933 build_class_member_access_expr. It does not work to use that
1934 routine directly because it expects the object to be of class
1935 type. */
1936 ptrmem_type = TREE_TYPE (ptrmem);
1937 my_friendly_assert (TYPE_PTRMEMFUNC_P (ptrmem_type), 20020804);
1938 member = lookup_member (ptrmem_type, member_name, /*protect=*/0,
1939 /*want_type=*/false);
1940 member_type = cp_build_qualified_type (TREE_TYPE (member),
1941 cp_type_quals (ptrmem_type));
1942 return fold (build (COMPONENT_REF, member_type, ptrmem, member));
1945 /* Given an expression PTR for a pointer, return an expression
1946 for the value pointed to.
1947 ERRORSTRING is the name of the operator to appear in error messages.
1949 This function may need to overload OPERATOR_FNNAME.
1950 Must also handle REFERENCE_TYPEs for C++. */
1952 tree
1953 build_x_indirect_ref (tree expr, const char *errorstring)
1955 tree orig_expr = expr;
1956 tree rval;
1958 if (processing_template_decl)
1960 if (type_dependent_expression_p (expr))
1961 return build_min_nt (INDIRECT_REF, expr);
1962 expr = build_non_dependent_expr (expr);
1965 rval = build_new_op (INDIRECT_REF, LOOKUP_NORMAL, expr, NULL_TREE,
1966 NULL_TREE);
1967 if (!rval)
1968 rval = build_indirect_ref (expr, errorstring);
1970 if (processing_template_decl && rval != error_mark_node)
1971 return build_min (INDIRECT_REF, TREE_TYPE (rval), orig_expr);
1972 else
1973 return rval;
1976 tree
1977 build_indirect_ref (tree ptr, const char *errorstring)
1979 register tree pointer, type;
1981 if (ptr == error_mark_node)
1982 return error_mark_node;
1984 if (ptr == current_class_ptr)
1985 return current_class_ref;
1987 pointer = (TREE_CODE (TREE_TYPE (ptr)) == REFERENCE_TYPE
1988 ? ptr : decay_conversion (ptr));
1989 type = TREE_TYPE (pointer);
1991 if (TYPE_PTR_P (type) || TREE_CODE (type) == REFERENCE_TYPE)
1993 /* [expr.unary.op]
1995 If the type of the expression is "pointer to T," the type
1996 of the result is "T."
1998 We must use the canonical variant because certain parts of
1999 the back end, like fold, do pointer comparisons between
2000 types. */
2001 tree t = canonical_type_variant (TREE_TYPE (type));
2003 if (VOID_TYPE_P (t))
2005 /* A pointer to incomplete type (other than cv void) can be
2006 dereferenced [expr.unary.op]/1 */
2007 error ("`%T' is not a pointer-to-object type", type);
2008 return error_mark_node;
2010 else if (TREE_CODE (pointer) == ADDR_EXPR
2011 && same_type_p (t, TREE_TYPE (TREE_OPERAND (pointer, 0))))
2012 /* The POINTER was something like `&x'. We simplify `*&x' to
2013 `x'. */
2014 return TREE_OPERAND (pointer, 0);
2015 else
2017 tree ref = build1 (INDIRECT_REF, t, pointer);
2019 /* We *must* set TREE_READONLY when dereferencing a pointer to const,
2020 so that we get the proper error message if the result is used
2021 to assign to. Also, &* is supposed to be a no-op. */
2022 TREE_READONLY (ref) = CP_TYPE_CONST_P (t);
2023 TREE_THIS_VOLATILE (ref) = CP_TYPE_VOLATILE_P (t);
2024 TREE_SIDE_EFFECTS (ref)
2025 = (TREE_THIS_VOLATILE (ref) || TREE_SIDE_EFFECTS (pointer));
2026 return ref;
2029 /* `pointer' won't be an error_mark_node if we were given a
2030 pointer to member, so it's cool to check for this here. */
2031 else if (TYPE_PTR_TO_MEMBER_P (type))
2032 error ("invalid use of `%s' on pointer to member", errorstring);
2033 else if (pointer != error_mark_node)
2035 if (errorstring)
2036 error ("invalid type argument of `%s'", errorstring);
2037 else
2038 error ("invalid type argument");
2040 return error_mark_node;
2043 /* This handles expressions of the form "a[i]", which denotes
2044 an array reference.
2046 This is logically equivalent in C to *(a+i), but we may do it differently.
2047 If A is a variable or a member, we generate a primitive ARRAY_REF.
2048 This avoids forcing the array out of registers, and can work on
2049 arrays that are not lvalues (for example, members of structures returned
2050 by functions).
2052 If INDEX is of some user-defined type, it must be converted to
2053 integer type. Otherwise, to make a compatible PLUS_EXPR, it
2054 will inherit the type of the array, which will be some pointer type. */
2056 tree
2057 build_array_ref (tree array, tree idx)
2059 if (idx == 0)
2061 error ("subscript missing in array reference");
2062 return error_mark_node;
2065 if (TREE_TYPE (array) == error_mark_node
2066 || TREE_TYPE (idx) == error_mark_node)
2067 return error_mark_node;
2069 /* If ARRAY is a COMPOUND_EXPR or COND_EXPR, move our reference
2070 inside it. */
2071 switch (TREE_CODE (array))
2073 case COMPOUND_EXPR:
2075 tree value = build_array_ref (TREE_OPERAND (array, 1), idx);
2076 return build (COMPOUND_EXPR, TREE_TYPE (value),
2077 TREE_OPERAND (array, 0), value);
2080 case COND_EXPR:
2081 return build_conditional_expr
2082 (TREE_OPERAND (array, 0),
2083 build_array_ref (TREE_OPERAND (array, 1), idx),
2084 build_array_ref (TREE_OPERAND (array, 2), idx));
2086 default:
2087 break;
2090 if (TREE_CODE (TREE_TYPE (array)) == ARRAY_TYPE
2091 && TREE_CODE (array) != INDIRECT_REF)
2093 tree rval, type;
2095 /* Subscripting with type char is likely to lose
2096 on a machine where chars are signed.
2097 So warn on any machine, but optionally.
2098 Don't warn for unsigned char since that type is safe.
2099 Don't warn for signed char because anyone who uses that
2100 must have done so deliberately. */
2101 if (warn_char_subscripts
2102 && TYPE_MAIN_VARIANT (TREE_TYPE (idx)) == char_type_node)
2103 warning ("array subscript has type `char'");
2105 if (!INTEGRAL_OR_ENUMERATION_TYPE_P (TREE_TYPE (idx)))
2107 error ("array subscript is not an integer");
2108 return error_mark_node;
2111 /* Apply integral promotions *after* noticing character types.
2112 (It is unclear why we do these promotions -- the standard
2113 does not say that we should. In fact, the natual thing would
2114 seem to be to convert IDX to ptrdiff_t; we're performing
2115 pointer arithmetic.) */
2116 idx = perform_integral_promotions (idx);
2118 /* An array that is indexed by a non-constant
2119 cannot be stored in a register; we must be able to do
2120 address arithmetic on its address.
2121 Likewise an array of elements of variable size. */
2122 if (TREE_CODE (idx) != INTEGER_CST
2123 || (COMPLETE_TYPE_P (TREE_TYPE (TREE_TYPE (array)))
2124 && (TREE_CODE (TYPE_SIZE (TREE_TYPE (TREE_TYPE (array))))
2125 != INTEGER_CST)))
2127 if (!cxx_mark_addressable (array))
2128 return error_mark_node;
2131 /* An array that is indexed by a constant value which is not within
2132 the array bounds cannot be stored in a register either; because we
2133 would get a crash in store_bit_field/extract_bit_field when trying
2134 to access a non-existent part of the register. */
2135 if (TREE_CODE (idx) == INTEGER_CST
2136 && TYPE_VALUES (TREE_TYPE (array))
2137 && ! int_fits_type_p (idx, TYPE_VALUES (TREE_TYPE (array))))
2139 if (!cxx_mark_addressable (array))
2140 return error_mark_node;
2143 if (pedantic && !lvalue_p (array))
2144 pedwarn ("ISO C++ forbids subscripting non-lvalue array");
2146 /* Note in C++ it is valid to subscript a `register' array, since
2147 it is valid to take the address of something with that
2148 storage specification. */
2149 if (extra_warnings)
2151 tree foo = array;
2152 while (TREE_CODE (foo) == COMPONENT_REF)
2153 foo = TREE_OPERAND (foo, 0);
2154 if (TREE_CODE (foo) == VAR_DECL && DECL_REGISTER (foo))
2155 warning ("subscripting array declared `register'");
2158 type = TREE_TYPE (TREE_TYPE (array));
2159 rval = build (ARRAY_REF, type, array, idx);
2160 /* Array ref is const/volatile if the array elements are
2161 or if the array is.. */
2162 TREE_READONLY (rval)
2163 |= (CP_TYPE_CONST_P (type) | TREE_READONLY (array));
2164 TREE_SIDE_EFFECTS (rval)
2165 |= (CP_TYPE_VOLATILE_P (type) | TREE_SIDE_EFFECTS (array));
2166 TREE_THIS_VOLATILE (rval)
2167 |= (CP_TYPE_VOLATILE_P (type) | TREE_THIS_VOLATILE (array));
2168 return require_complete_type (fold (rval));
2172 tree ar = default_conversion (array);
2173 tree ind = default_conversion (idx);
2175 /* Put the integer in IND to simplify error checking. */
2176 if (TREE_CODE (TREE_TYPE (ar)) == INTEGER_TYPE)
2178 tree temp = ar;
2179 ar = ind;
2180 ind = temp;
2183 if (ar == error_mark_node)
2184 return ar;
2186 if (TREE_CODE (TREE_TYPE (ar)) != POINTER_TYPE)
2188 error ("subscripted value is neither array nor pointer");
2189 return error_mark_node;
2191 if (TREE_CODE (TREE_TYPE (ind)) != INTEGER_TYPE)
2193 error ("array subscript is not an integer");
2194 return error_mark_node;
2197 return build_indirect_ref (cp_build_binary_op (PLUS_EXPR, ar, ind),
2198 "array indexing");
2202 /* Resolve a pointer to member function. INSTANCE is the object
2203 instance to use, if the member points to a virtual member.
2205 This used to avoid checking for virtual functions if basetype
2206 has no virtual functions, according to an earlier ANSI draft.
2207 With the final ISO C++ rules, such an optimization is
2208 incorrect: A pointer to a derived member can be static_cast
2209 to pointer-to-base-member, as long as the dynamic object
2210 later has the right member. */
2212 tree
2213 get_member_function_from_ptrfunc (tree *instance_ptrptr, tree function)
2215 if (TREE_CODE (function) == OFFSET_REF)
2216 function = TREE_OPERAND (function, 1);
2218 if (TYPE_PTRMEMFUNC_P (TREE_TYPE (function)))
2220 tree idx, delta, e1, e2, e3, vtbl, basetype;
2221 tree fntype = TYPE_PTRMEMFUNC_FN_TYPE (TREE_TYPE (function));
2223 tree instance_ptr = *instance_ptrptr;
2224 tree instance_save_expr = 0;
2225 if (instance_ptr == error_mark_node)
2227 if (TREE_CODE (function) == PTRMEM_CST)
2229 /* Extracting the function address from a pmf is only
2230 allowed with -Wno-pmf-conversions. It only works for
2231 pmf constants. */
2232 e1 = build_addr_func (PTRMEM_CST_MEMBER (function));
2233 e1 = convert (fntype, e1);
2234 return e1;
2236 else
2238 error ("object missing in use of `%E'", function);
2239 return error_mark_node;
2243 if (TREE_SIDE_EFFECTS (instance_ptr))
2244 instance_ptr = instance_save_expr = save_expr (instance_ptr);
2246 if (TREE_SIDE_EFFECTS (function))
2247 function = save_expr (function);
2249 /* Start by extracting all the information from the PMF itself. */
2250 e3 = PFN_FROM_PTRMEMFUNC (function);
2251 delta = build_ptrmemfunc_access_expr (function, delta_identifier);
2252 idx = build1 (NOP_EXPR, vtable_index_type, e3);
2253 switch (TARGET_PTRMEMFUNC_VBIT_LOCATION)
2255 case ptrmemfunc_vbit_in_pfn:
2256 e1 = cp_build_binary_op (BIT_AND_EXPR, idx, integer_one_node);
2257 idx = cp_build_binary_op (MINUS_EXPR, idx, integer_one_node);
2258 break;
2260 case ptrmemfunc_vbit_in_delta:
2261 e1 = cp_build_binary_op (BIT_AND_EXPR, delta, integer_one_node);
2262 delta = cp_build_binary_op (RSHIFT_EXPR, delta, integer_one_node);
2263 break;
2265 default:
2266 abort ();
2269 /* Convert down to the right base before using the instance. First
2270 use the type... */
2271 basetype = TYPE_METHOD_BASETYPE (TREE_TYPE (fntype));
2272 basetype = lookup_base (TREE_TYPE (TREE_TYPE (instance_ptr)),
2273 basetype, ba_check, NULL);
2274 instance_ptr = build_base_path (PLUS_EXPR, instance_ptr, basetype, 1);
2275 if (instance_ptr == error_mark_node)
2276 return error_mark_node;
2277 /* ...and then the delta in the PMF. */
2278 instance_ptr = build (PLUS_EXPR, TREE_TYPE (instance_ptr),
2279 instance_ptr, delta);
2281 /* Hand back the adjusted 'this' argument to our caller. */
2282 *instance_ptrptr = instance_ptr;
2284 /* Next extract the vtable pointer from the object. */
2285 vtbl = build1 (NOP_EXPR, build_pointer_type (vtbl_ptr_type_node),
2286 instance_ptr);
2287 vtbl = build_indirect_ref (vtbl, NULL);
2289 /* Finally, extract the function pointer from the vtable. */
2290 e2 = fold (build (PLUS_EXPR, TREE_TYPE (vtbl), vtbl, idx));
2291 e2 = build_indirect_ref (e2, NULL);
2292 TREE_CONSTANT (e2) = 1;
2294 /* When using function descriptors, the address of the
2295 vtable entry is treated as a function pointer. */
2296 if (TARGET_VTABLE_USES_DESCRIPTORS)
2297 e2 = build1 (NOP_EXPR, TREE_TYPE (e2),
2298 build_unary_op (ADDR_EXPR, e2, /*noconvert=*/1));
2300 TREE_TYPE (e2) = TREE_TYPE (e3);
2301 e1 = build_conditional_expr (e1, e2, e3);
2303 /* Make sure this doesn't get evaluated first inside one of the
2304 branches of the COND_EXPR. */
2305 if (instance_save_expr)
2306 e1 = build (COMPOUND_EXPR, TREE_TYPE (e1),
2307 instance_save_expr, e1);
2309 function = e1;
2311 return function;
2314 tree
2315 build_function_call (tree function, tree params)
2317 register tree fntype, fndecl;
2318 register tree coerced_params;
2319 tree result;
2320 tree name = NULL_TREE, assembler_name = NULL_TREE;
2321 int is_method;
2322 tree original = function;
2324 /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
2325 Strip such NOP_EXPRs, since FUNCTION is used in non-lvalue context. */
2326 if (TREE_CODE (function) == NOP_EXPR
2327 && TREE_TYPE (function) == TREE_TYPE (TREE_OPERAND (function, 0)))
2328 function = TREE_OPERAND (function, 0);
2330 if (TREE_CODE (function) == FUNCTION_DECL)
2332 name = DECL_NAME (function);
2333 assembler_name = DECL_ASSEMBLER_NAME (function);
2335 mark_used (function);
2336 fndecl = function;
2338 /* Convert anything with function type to a pointer-to-function. */
2339 if (pedantic && DECL_MAIN_P (function))
2340 pedwarn ("ISO C++ forbids calling `::main' from within program");
2342 /* Differs from default_conversion by not setting TREE_ADDRESSABLE
2343 (because calling an inline function does not mean the function
2344 needs to be separately compiled). */
2346 if (DECL_INLINE (function))
2347 function = inline_conversion (function);
2348 else
2349 function = build_addr_func (function);
2351 else
2353 fndecl = NULL_TREE;
2355 function = build_addr_func (function);
2358 if (function == error_mark_node)
2359 return error_mark_node;
2361 fntype = TREE_TYPE (function);
2363 if (TYPE_PTRMEMFUNC_P (fntype))
2365 error ("must use .* or ->* to call pointer-to-member function in `%E (...)'",
2366 original);
2367 return error_mark_node;
2370 is_method = (TREE_CODE (fntype) == POINTER_TYPE
2371 && TREE_CODE (TREE_TYPE (fntype)) == METHOD_TYPE);
2373 if (!((TREE_CODE (fntype) == POINTER_TYPE
2374 && TREE_CODE (TREE_TYPE (fntype)) == FUNCTION_TYPE)
2375 || is_method
2376 || TREE_CODE (function) == TEMPLATE_ID_EXPR))
2378 error ("`%E' cannot be used as a function", original);
2379 return error_mark_node;
2382 /* fntype now gets the type of function pointed to. */
2383 fntype = TREE_TYPE (fntype);
2385 /* Convert the parameters to the types declared in the
2386 function prototype, or apply default promotions. */
2388 coerced_params = convert_arguments (TYPE_ARG_TYPES (fntype),
2389 params, fndecl, LOOKUP_NORMAL);
2390 if (coerced_params == error_mark_node)
2391 return error_mark_node;
2393 /* Check for errors in format strings. */
2395 if (warn_format)
2396 check_function_format (NULL, TYPE_ATTRIBUTES (fntype), coerced_params);
2398 /* Recognize certain built-in functions so we can make tree-codes
2399 other than CALL_EXPR. We do this when it enables fold-const.c
2400 to do something useful. */
2402 if (TREE_CODE (function) == ADDR_EXPR
2403 && TREE_CODE (TREE_OPERAND (function, 0)) == FUNCTION_DECL
2404 && DECL_BUILT_IN (TREE_OPERAND (function, 0)))
2406 result = expand_tree_builtin (TREE_OPERAND (function, 0),
2407 params, coerced_params);
2408 if (result)
2409 return result;
2412 return build_cxx_call (function, params, coerced_params);
2415 /* Convert the actual parameter expressions in the list VALUES
2416 to the types in the list TYPELIST.
2417 If parmdecls is exhausted, or when an element has NULL as its type,
2418 perform the default conversions.
2420 NAME is an IDENTIFIER_NODE or 0. It is used only for error messages.
2422 This is also where warnings about wrong number of args are generated.
2424 Return a list of expressions for the parameters as converted.
2426 Both VALUES and the returned value are chains of TREE_LIST nodes
2427 with the elements of the list in the TREE_VALUE slots of those nodes.
2429 In C++, unspecified trailing parameters can be filled in with their
2430 default arguments, if such were specified. Do so here. */
2432 tree
2433 convert_arguments (tree typelist, tree values, tree fndecl, int flags)
2435 register tree typetail, valtail;
2436 register tree result = NULL_TREE;
2437 const char *called_thing = 0;
2438 int i = 0;
2440 /* Argument passing is always copy-initialization. */
2441 flags |= LOOKUP_ONLYCONVERTING;
2443 if (fndecl)
2445 if (TREE_CODE (TREE_TYPE (fndecl)) == METHOD_TYPE)
2447 if (DECL_NAME (fndecl) == NULL_TREE
2448 || IDENTIFIER_HAS_TYPE_VALUE (DECL_NAME (fndecl)))
2449 called_thing = "constructor";
2450 else
2451 called_thing = "member function";
2453 else
2454 called_thing = "function";
2457 for (valtail = values, typetail = typelist;
2458 valtail;
2459 valtail = TREE_CHAIN (valtail), i++)
2461 register tree type = typetail ? TREE_VALUE (typetail) : 0;
2462 register tree val = TREE_VALUE (valtail);
2464 if (val == error_mark_node)
2465 return error_mark_node;
2467 if (type == void_type_node)
2469 if (fndecl)
2471 cp_error_at ("too many arguments to %s `%+#D'", called_thing,
2472 fndecl);
2473 error ("at this point in file");
2475 else
2476 error ("too many arguments to function");
2477 /* In case anybody wants to know if this argument
2478 list is valid. */
2479 if (result)
2480 TREE_TYPE (tree_last (result)) = error_mark_node;
2481 break;
2484 /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
2485 Strip such NOP_EXPRs, since VAL is used in non-lvalue context. */
2486 if (TREE_CODE (val) == NOP_EXPR
2487 && TREE_TYPE (val) == TREE_TYPE (TREE_OPERAND (val, 0))
2488 && (type == 0 || TREE_CODE (type) != REFERENCE_TYPE))
2489 val = TREE_OPERAND (val, 0);
2491 if (type == 0 || TREE_CODE (type) != REFERENCE_TYPE)
2493 if (TREE_CODE (TREE_TYPE (val)) == ARRAY_TYPE
2494 || TREE_CODE (TREE_TYPE (val)) == FUNCTION_TYPE
2495 || TREE_CODE (TREE_TYPE (val)) == METHOD_TYPE)
2496 val = decay_conversion (val);
2499 if (val == error_mark_node)
2500 return error_mark_node;
2502 if (type != 0)
2504 /* Formal parm type is specified by a function prototype. */
2505 tree parmval;
2507 if (!COMPLETE_TYPE_P (complete_type (type)))
2509 error ("parameter type of called function is incomplete");
2510 parmval = val;
2512 else
2514 parmval = convert_for_initialization
2515 (NULL_TREE, type, val, flags,
2516 "argument passing", fndecl, i);
2517 parmval = convert_for_arg_passing (type, parmval);
2520 if (parmval == error_mark_node)
2521 return error_mark_node;
2523 result = tree_cons (NULL_TREE, parmval, result);
2525 else
2527 if (TREE_CODE (TREE_TYPE (val)) == REFERENCE_TYPE)
2528 val = convert_from_reference (val);
2530 if (fndecl && DECL_BUILT_IN (fndecl)
2531 && DECL_FUNCTION_CODE (fndecl) == BUILT_IN_CONSTANT_P)
2532 /* Don't do ellipsis conversion for __built_in_constant_p
2533 as this will result in spurious warnings for non-POD
2534 types. */
2535 val = require_complete_type (val);
2536 else
2537 val = convert_arg_to_ellipsis (val);
2539 result = tree_cons (NULL_TREE, val, result);
2542 if (typetail)
2543 typetail = TREE_CHAIN (typetail);
2546 if (typetail != 0 && typetail != void_list_node)
2548 /* See if there are default arguments that can be used */
2549 if (TREE_PURPOSE (typetail)
2550 && TREE_CODE (TREE_PURPOSE (typetail)) != DEFAULT_ARG)
2552 for (; typetail != void_list_node; ++i)
2554 tree parmval
2555 = convert_default_arg (TREE_VALUE (typetail),
2556 TREE_PURPOSE (typetail),
2557 fndecl, i);
2559 if (parmval == error_mark_node)
2560 return error_mark_node;
2562 result = tree_cons (0, parmval, result);
2563 typetail = TREE_CHAIN (typetail);
2564 /* ends with `...'. */
2565 if (typetail == NULL_TREE)
2566 break;
2569 else
2571 if (fndecl)
2573 cp_error_at ("too few arguments to %s `%+#D'",
2574 called_thing, fndecl);
2575 error ("at this point in file");
2577 else
2578 error ("too few arguments to function");
2579 return error_mark_list;
2583 return nreverse (result);
2586 /* Build a binary-operation expression, after performing default
2587 conversions on the operands. CODE is the kind of expression to build. */
2589 tree
2590 build_x_binary_op (enum tree_code code, tree arg1, tree arg2)
2592 tree orig_arg1;
2593 tree orig_arg2;
2594 tree expr;
2596 orig_arg1 = arg1;
2597 orig_arg2 = arg2;
2599 if (processing_template_decl)
2601 if (type_dependent_expression_p (arg1)
2602 || type_dependent_expression_p (arg2))
2603 return build_min_nt (code, arg1, arg2);
2604 arg1 = build_non_dependent_expr (arg1);
2605 arg2 = build_non_dependent_expr (arg2);
2608 if (code == DOTSTAR_EXPR)
2609 expr = build_m_component_ref (arg1, arg2);
2610 else
2611 expr = build_new_op (code, LOOKUP_NORMAL, arg1, arg2, NULL_TREE);
2613 if (processing_template_decl && expr != error_mark_node)
2614 return build_min (code, TREE_TYPE (expr), orig_arg1, orig_arg2);
2616 return expr;
2619 /* Build a binary-operation expression without default conversions.
2620 CODE is the kind of expression to build.
2621 This function differs from `build' in several ways:
2622 the data type of the result is computed and recorded in it,
2623 warnings are generated if arg data types are invalid,
2624 special handling for addition and subtraction of pointers is known,
2625 and some optimization is done (operations on narrow ints
2626 are done in the narrower type when that gives the same result).
2627 Constant folding is also done before the result is returned.
2629 Note that the operands will never have enumeral types
2630 because either they have just had the default conversions performed
2631 or they have both just been converted to some other type in which
2632 the arithmetic is to be done.
2634 C++: must do special pointer arithmetic when implementing
2635 multiple inheritance, and deal with pointer to member functions. */
2637 tree
2638 build_binary_op (enum tree_code code, tree orig_op0, tree orig_op1,
2639 int convert_p ATTRIBUTE_UNUSED)
2641 tree op0, op1;
2642 register enum tree_code code0, code1;
2643 tree type0, type1;
2645 /* Expression code to give to the expression when it is built.
2646 Normally this is CODE, which is what the caller asked for,
2647 but in some special cases we change it. */
2648 register enum tree_code resultcode = code;
2650 /* Data type in which the computation is to be performed.
2651 In the simplest cases this is the common type of the arguments. */
2652 register tree result_type = NULL;
2654 /* Nonzero means operands have already been type-converted
2655 in whatever way is necessary.
2656 Zero means they need to be converted to RESULT_TYPE. */
2657 int converted = 0;
2659 /* Nonzero means create the expression with this type, rather than
2660 RESULT_TYPE. */
2661 tree build_type = 0;
2663 /* Nonzero means after finally constructing the expression
2664 convert it to this type. */
2665 tree final_type = 0;
2667 /* Nonzero if this is an operation like MIN or MAX which can
2668 safely be computed in short if both args are promoted shorts.
2669 Also implies COMMON.
2670 -1 indicates a bitwise operation; this makes a difference
2671 in the exact conditions for when it is safe to do the operation
2672 in a narrower mode. */
2673 int shorten = 0;
2675 /* Nonzero if this is a comparison operation;
2676 if both args are promoted shorts, compare the original shorts.
2677 Also implies COMMON. */
2678 int short_compare = 0;
2680 /* Nonzero if this is a right-shift operation, which can be computed on the
2681 original short and then promoted if the operand is a promoted short. */
2682 int short_shift = 0;
2684 /* Nonzero means set RESULT_TYPE to the common type of the args. */
2685 int common = 0;
2687 /* Apply default conversions. */
2688 op0 = orig_op0;
2689 op1 = orig_op1;
2691 if (code == TRUTH_AND_EXPR || code == TRUTH_ANDIF_EXPR
2692 || code == TRUTH_OR_EXPR || code == TRUTH_ORIF_EXPR
2693 || code == TRUTH_XOR_EXPR)
2695 if (!really_overloaded_fn (op0))
2696 op0 = decay_conversion (op0);
2697 if (!really_overloaded_fn (op1))
2698 op1 = decay_conversion (op1);
2700 else
2702 if (!really_overloaded_fn (op0))
2703 op0 = default_conversion (op0);
2704 if (!really_overloaded_fn (op1))
2705 op1 = default_conversion (op1);
2708 /* Strip NON_LVALUE_EXPRs, etc., since we aren't using as an lvalue. */
2709 STRIP_TYPE_NOPS (op0);
2710 STRIP_TYPE_NOPS (op1);
2712 /* DTRT if one side is an overloaded function, but complain about it. */
2713 if (type_unknown_p (op0))
2715 tree t = instantiate_type (TREE_TYPE (op1), op0, tf_none);
2716 if (t != error_mark_node)
2718 pedwarn ("assuming cast to type `%T' from overloaded function",
2719 TREE_TYPE (t));
2720 op0 = t;
2723 if (type_unknown_p (op1))
2725 tree t = instantiate_type (TREE_TYPE (op0), op1, tf_none);
2726 if (t != error_mark_node)
2728 pedwarn ("assuming cast to type `%T' from overloaded function",
2729 TREE_TYPE (t));
2730 op1 = t;
2734 type0 = TREE_TYPE (op0);
2735 type1 = TREE_TYPE (op1);
2737 /* The expression codes of the data types of the arguments tell us
2738 whether the arguments are integers, floating, pointers, etc. */
2739 code0 = TREE_CODE (type0);
2740 code1 = TREE_CODE (type1);
2742 /* If an error was already reported for one of the arguments,
2743 avoid reporting another error. */
2745 if (code0 == ERROR_MARK || code1 == ERROR_MARK)
2746 return error_mark_node;
2748 switch (code)
2750 case PLUS_EXPR:
2751 /* Handle the pointer + int case. */
2752 if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
2753 return cp_pointer_int_sum (PLUS_EXPR, op0, op1);
2754 else if (code1 == POINTER_TYPE && code0 == INTEGER_TYPE)
2755 return cp_pointer_int_sum (PLUS_EXPR, op1, op0);
2756 else
2757 common = 1;
2758 break;
2760 case MINUS_EXPR:
2761 /* Subtraction of two similar pointers.
2762 We must subtract them as integers, then divide by object size. */
2763 if (code0 == POINTER_TYPE && code1 == POINTER_TYPE
2764 && same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (type0),
2765 TREE_TYPE (type1)))
2766 return pointer_diff (op0, op1, common_type (type0, type1));
2767 /* Handle pointer minus int. Just like pointer plus int. */
2768 else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
2769 return cp_pointer_int_sum (MINUS_EXPR, op0, op1);
2770 else
2771 common = 1;
2772 break;
2774 case MULT_EXPR:
2775 common = 1;
2776 break;
2778 case TRUNC_DIV_EXPR:
2779 case CEIL_DIV_EXPR:
2780 case FLOOR_DIV_EXPR:
2781 case ROUND_DIV_EXPR:
2782 case EXACT_DIV_EXPR:
2783 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE
2784 || code0 == COMPLEX_TYPE)
2785 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
2786 || code1 == COMPLEX_TYPE))
2788 if (TREE_CODE (op1) == INTEGER_CST && integer_zerop (op1))
2789 warning ("division by zero in `%E / 0'", op0);
2790 else if (TREE_CODE (op1) == REAL_CST && real_zerop (op1))
2791 warning ("division by zero in `%E / 0.'", op0);
2793 if (!(code0 == INTEGER_TYPE && code1 == INTEGER_TYPE))
2794 resultcode = RDIV_EXPR;
2795 else
2796 /* When dividing two signed integers, we have to promote to int.
2797 unless we divide by a constant != -1. Note that default
2798 conversion will have been performed on the operands at this
2799 point, so we have to dig out the original type to find out if
2800 it was unsigned. */
2801 shorten = ((TREE_CODE (op0) == NOP_EXPR
2802 && TREE_UNSIGNED (TREE_TYPE (TREE_OPERAND (op0, 0))))
2803 || (TREE_CODE (op1) == INTEGER_CST
2804 && ! integer_all_onesp (op1)));
2806 common = 1;
2808 break;
2810 case BIT_AND_EXPR:
2811 case BIT_IOR_EXPR:
2812 case BIT_XOR_EXPR:
2813 if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
2814 shorten = -1;
2815 break;
2817 case TRUNC_MOD_EXPR:
2818 case FLOOR_MOD_EXPR:
2819 if (code1 == INTEGER_TYPE && integer_zerop (op1))
2820 warning ("division by zero in `%E %% 0'", op0);
2821 else if (code1 == REAL_TYPE && real_zerop (op1))
2822 warning ("division by zero in `%E %% 0.'", op0);
2824 if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
2826 /* Although it would be tempting to shorten always here, that loses
2827 on some targets, since the modulo instruction is undefined if the
2828 quotient can't be represented in the computation mode. We shorten
2829 only if unsigned or if dividing by something we know != -1. */
2830 shorten = ((TREE_CODE (op0) == NOP_EXPR
2831 && TREE_UNSIGNED (TREE_TYPE (TREE_OPERAND (op0, 0))))
2832 || (TREE_CODE (op1) == INTEGER_CST
2833 && ! integer_all_onesp (op1)));
2834 common = 1;
2836 break;
2838 case TRUTH_ANDIF_EXPR:
2839 case TRUTH_ORIF_EXPR:
2840 case TRUTH_AND_EXPR:
2841 case TRUTH_OR_EXPR:
2842 result_type = boolean_type_node;
2843 break;
2845 /* Shift operations: result has same type as first operand;
2846 always convert second operand to int.
2847 Also set SHORT_SHIFT if shifting rightward. */
2849 case RSHIFT_EXPR:
2850 if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
2852 result_type = type0;
2853 if (TREE_CODE (op1) == INTEGER_CST)
2855 if (tree_int_cst_lt (op1, integer_zero_node))
2856 warning ("right shift count is negative");
2857 else
2859 if (! integer_zerop (op1))
2860 short_shift = 1;
2861 if (compare_tree_int (op1, TYPE_PRECISION (type0)) >= 0)
2862 warning ("right shift count >= width of type");
2865 /* Convert the shift-count to an integer, regardless of
2866 size of value being shifted. */
2867 if (TYPE_MAIN_VARIANT (TREE_TYPE (op1)) != integer_type_node)
2868 op1 = cp_convert (integer_type_node, op1);
2869 /* Avoid converting op1 to result_type later. */
2870 converted = 1;
2872 break;
2874 case LSHIFT_EXPR:
2875 if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
2877 result_type = type0;
2878 if (TREE_CODE (op1) == INTEGER_CST)
2880 if (tree_int_cst_lt (op1, integer_zero_node))
2881 warning ("left shift count is negative");
2882 else if (compare_tree_int (op1, TYPE_PRECISION (type0)) >= 0)
2883 warning ("left shift count >= width of type");
2885 /* Convert the shift-count to an integer, regardless of
2886 size of value being shifted. */
2887 if (TYPE_MAIN_VARIANT (TREE_TYPE (op1)) != integer_type_node)
2888 op1 = cp_convert (integer_type_node, op1);
2889 /* Avoid converting op1 to result_type later. */
2890 converted = 1;
2892 break;
2894 case RROTATE_EXPR:
2895 case LROTATE_EXPR:
2896 if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
2898 result_type = type0;
2899 if (TREE_CODE (op1) == INTEGER_CST)
2901 if (tree_int_cst_lt (op1, integer_zero_node))
2902 warning ("%s rotate count is negative",
2903 (code == LROTATE_EXPR) ? "left" : "right");
2904 else if (compare_tree_int (op1, TYPE_PRECISION (type0)) >= 0)
2905 warning ("%s rotate count >= width of type",
2906 (code == LROTATE_EXPR) ? "left" : "right");
2908 /* Convert the shift-count to an integer, regardless of
2909 size of value being shifted. */
2910 if (TYPE_MAIN_VARIANT (TREE_TYPE (op1)) != integer_type_node)
2911 op1 = cp_convert (integer_type_node, op1);
2913 break;
2915 case EQ_EXPR:
2916 case NE_EXPR:
2917 if (warn_float_equal && (code0 == REAL_TYPE || code1 == REAL_TYPE))
2918 warning ("comparing floating point with == or != is unsafe");
2920 build_type = boolean_type_node;
2921 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE
2922 || code0 == COMPLEX_TYPE)
2923 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
2924 || code1 == COMPLEX_TYPE))
2925 short_compare = 1;
2926 else if ((code0 == POINTER_TYPE && code1 == POINTER_TYPE)
2927 || (TYPE_PTRMEM_P (type0) && TYPE_PTRMEM_P (type1)))
2928 result_type = composite_pointer_type (type0, type1, op0, op1,
2929 "comparison");
2930 else if ((code0 == POINTER_TYPE || TYPE_PTRMEM_P (type0))
2931 && null_ptr_cst_p (op1))
2932 result_type = type0;
2933 else if ((code1 == POINTER_TYPE || TYPE_PTRMEM_P (type1))
2934 && null_ptr_cst_p (op0))
2935 result_type = type1;
2936 else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
2938 result_type = type0;
2939 error ("ISO C++ forbids comparison between pointer and integer");
2941 else if (code0 == INTEGER_TYPE && code1 == POINTER_TYPE)
2943 result_type = type1;
2944 error ("ISO C++ forbids comparison between pointer and integer");
2946 else if (TYPE_PTRMEMFUNC_P (type0) && null_ptr_cst_p (op1))
2948 op0 = build_ptrmemfunc_access_expr (op0, pfn_identifier);
2949 op1 = cp_convert (TREE_TYPE (op0), integer_zero_node);
2950 result_type = TREE_TYPE (op0);
2952 else if (TYPE_PTRMEMFUNC_P (type1) && null_ptr_cst_p (op0))
2953 return cp_build_binary_op (code, op1, op0);
2954 else if (TYPE_PTRMEMFUNC_P (type0) && TYPE_PTRMEMFUNC_P (type1)
2955 && same_type_p (type0, type1))
2957 /* E will be the final comparison. */
2958 tree e;
2959 /* E1 and E2 are for scratch. */
2960 tree e1;
2961 tree e2;
2962 tree pfn0;
2963 tree pfn1;
2964 tree delta0;
2965 tree delta1;
2967 if (TREE_SIDE_EFFECTS (op0))
2968 op0 = save_expr (op0);
2969 if (TREE_SIDE_EFFECTS (op1))
2970 op1 = save_expr (op1);
2972 /* We generate:
2974 (op0.pfn == op1.pfn
2975 && (!op0.pfn || op0.delta == op1.delta))
2977 The reason for the `!op0.pfn' bit is that a NULL
2978 pointer-to-member is any member with a zero PFN; the
2979 DELTA field is unspecified. */
2980 pfn0 = pfn_from_ptrmemfunc (op0);
2981 pfn1 = pfn_from_ptrmemfunc (op1);
2982 delta0 = build_ptrmemfunc_access_expr (op0,
2983 delta_identifier);
2984 delta1 = build_ptrmemfunc_access_expr (op1,
2985 delta_identifier);
2986 e1 = cp_build_binary_op (EQ_EXPR, delta0, delta1);
2987 e2 = cp_build_binary_op (EQ_EXPR,
2988 pfn0,
2989 cp_convert (TREE_TYPE (pfn0),
2990 integer_zero_node));
2991 e1 = cp_build_binary_op (TRUTH_ORIF_EXPR, e1, e2);
2992 e2 = build (EQ_EXPR, boolean_type_node, pfn0, pfn1);
2993 e = cp_build_binary_op (TRUTH_ANDIF_EXPR, e2, e1);
2994 if (code == EQ_EXPR)
2995 return e;
2996 return cp_build_binary_op (EQ_EXPR, e, integer_zero_node);
2998 else if ((TYPE_PTRMEMFUNC_P (type0)
2999 && same_type_p (TYPE_PTRMEMFUNC_FN_TYPE (type0), type1))
3000 || (TYPE_PTRMEMFUNC_P (type1)
3001 && same_type_p (TYPE_PTRMEMFUNC_FN_TYPE (type1), type0)))
3002 abort ();
3003 break;
3005 case MAX_EXPR:
3006 case MIN_EXPR:
3007 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE)
3008 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE))
3009 shorten = 1;
3010 else if (code0 == POINTER_TYPE && code1 == POINTER_TYPE)
3011 result_type = composite_pointer_type (type0, type1, op0, op1,
3012 "comparison");
3013 break;
3015 case LE_EXPR:
3016 case GE_EXPR:
3017 case LT_EXPR:
3018 case GT_EXPR:
3019 build_type = boolean_type_node;
3020 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE)
3021 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE))
3022 short_compare = 1;
3023 else if (code0 == POINTER_TYPE && code1 == POINTER_TYPE)
3024 result_type = composite_pointer_type (type0, type1, op0, op1,
3025 "comparison");
3026 else if (code0 == POINTER_TYPE && TREE_CODE (op1) == INTEGER_CST
3027 && integer_zerop (op1))
3028 result_type = type0;
3029 else if (code1 == POINTER_TYPE && TREE_CODE (op0) == INTEGER_CST
3030 && integer_zerop (op0))
3031 result_type = type1;
3032 else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
3034 result_type = type0;
3035 pedwarn ("ISO C++ forbids comparison between pointer and integer");
3037 else if (code0 == INTEGER_TYPE && code1 == POINTER_TYPE)
3039 result_type = type1;
3040 pedwarn ("ISO C++ forbids comparison between pointer and integer");
3042 break;
3044 case UNORDERED_EXPR:
3045 case ORDERED_EXPR:
3046 case UNLT_EXPR:
3047 case UNLE_EXPR:
3048 case UNGT_EXPR:
3049 case UNGE_EXPR:
3050 case UNEQ_EXPR:
3051 build_type = integer_type_node;
3052 if (code0 != REAL_TYPE || code1 != REAL_TYPE)
3054 error ("unordered comparison on non-floating point argument");
3055 return error_mark_node;
3057 common = 1;
3058 break;
3060 default:
3061 break;
3064 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE || code0 == COMPLEX_TYPE)
3066 (code1 == INTEGER_TYPE || code1 == REAL_TYPE || code1 == COMPLEX_TYPE))
3068 int none_complex = (code0 != COMPLEX_TYPE && code1 != COMPLEX_TYPE);
3070 if (shorten || common || short_compare)
3071 result_type = common_type (type0, type1);
3073 /* For certain operations (which identify themselves by shorten != 0)
3074 if both args were extended from the same smaller type,
3075 do the arithmetic in that type and then extend.
3077 shorten !=0 and !=1 indicates a bitwise operation.
3078 For them, this optimization is safe only if
3079 both args are zero-extended or both are sign-extended.
3080 Otherwise, we might change the result.
3081 Eg, (short)-1 | (unsigned short)-1 is (int)-1
3082 but calculated in (unsigned short) it would be (unsigned short)-1. */
3084 if (shorten && none_complex)
3086 int unsigned0, unsigned1;
3087 tree arg0 = get_narrower (op0, &unsigned0);
3088 tree arg1 = get_narrower (op1, &unsigned1);
3089 /* UNS is 1 if the operation to be done is an unsigned one. */
3090 int uns = TREE_UNSIGNED (result_type);
3091 tree type;
3093 final_type = result_type;
3095 /* Handle the case that OP0 does not *contain* a conversion
3096 but it *requires* conversion to FINAL_TYPE. */
3098 if (op0 == arg0 && TREE_TYPE (op0) != final_type)
3099 unsigned0 = TREE_UNSIGNED (TREE_TYPE (op0));
3100 if (op1 == arg1 && TREE_TYPE (op1) != final_type)
3101 unsigned1 = TREE_UNSIGNED (TREE_TYPE (op1));
3103 /* Now UNSIGNED0 is 1 if ARG0 zero-extends to FINAL_TYPE. */
3105 /* For bitwise operations, signedness of nominal type
3106 does not matter. Consider only how operands were extended. */
3107 if (shorten == -1)
3108 uns = unsigned0;
3110 /* Note that in all three cases below we refrain from optimizing
3111 an unsigned operation on sign-extended args.
3112 That would not be valid. */
3114 /* Both args variable: if both extended in same way
3115 from same width, do it in that width.
3116 Do it unsigned if args were zero-extended. */
3117 if ((TYPE_PRECISION (TREE_TYPE (arg0))
3118 < TYPE_PRECISION (result_type))
3119 && (TYPE_PRECISION (TREE_TYPE (arg1))
3120 == TYPE_PRECISION (TREE_TYPE (arg0)))
3121 && unsigned0 == unsigned1
3122 && (unsigned0 || !uns))
3123 result_type = c_common_signed_or_unsigned_type
3124 (unsigned0, common_type (TREE_TYPE (arg0), TREE_TYPE (arg1)));
3125 else if (TREE_CODE (arg0) == INTEGER_CST
3126 && (unsigned1 || !uns)
3127 && (TYPE_PRECISION (TREE_TYPE (arg1))
3128 < TYPE_PRECISION (result_type))
3129 && (type = c_common_signed_or_unsigned_type
3130 (unsigned1, TREE_TYPE (arg1)),
3131 int_fits_type_p (arg0, type)))
3132 result_type = type;
3133 else if (TREE_CODE (arg1) == INTEGER_CST
3134 && (unsigned0 || !uns)
3135 && (TYPE_PRECISION (TREE_TYPE (arg0))
3136 < TYPE_PRECISION (result_type))
3137 && (type = c_common_signed_or_unsigned_type
3138 (unsigned0, TREE_TYPE (arg0)),
3139 int_fits_type_p (arg1, type)))
3140 result_type = type;
3143 /* Shifts can be shortened if shifting right. */
3145 if (short_shift)
3147 int unsigned_arg;
3148 tree arg0 = get_narrower (op0, &unsigned_arg);
3150 final_type = result_type;
3152 if (arg0 == op0 && final_type == TREE_TYPE (op0))
3153 unsigned_arg = TREE_UNSIGNED (TREE_TYPE (op0));
3155 if (TYPE_PRECISION (TREE_TYPE (arg0)) < TYPE_PRECISION (result_type)
3156 /* We can shorten only if the shift count is less than the
3157 number of bits in the smaller type size. */
3158 && compare_tree_int (op1, TYPE_PRECISION (TREE_TYPE (arg0))) < 0
3159 /* If arg is sign-extended and then unsigned-shifted,
3160 we can simulate this with a signed shift in arg's type
3161 only if the extended result is at least twice as wide
3162 as the arg. Otherwise, the shift could use up all the
3163 ones made by sign-extension and bring in zeros.
3164 We can't optimize that case at all, but in most machines
3165 it never happens because available widths are 2**N. */
3166 && (!TREE_UNSIGNED (final_type)
3167 || unsigned_arg
3168 || (((unsigned) 2 * TYPE_PRECISION (TREE_TYPE (arg0)))
3169 <= TYPE_PRECISION (result_type))))
3171 /* Do an unsigned shift if the operand was zero-extended. */
3172 result_type
3173 = c_common_signed_or_unsigned_type (unsigned_arg,
3174 TREE_TYPE (arg0));
3175 /* Convert value-to-be-shifted to that type. */
3176 if (TREE_TYPE (op0) != result_type)
3177 op0 = cp_convert (result_type, op0);
3178 converted = 1;
3182 /* Comparison operations are shortened too but differently.
3183 They identify themselves by setting short_compare = 1. */
3185 if (short_compare)
3187 /* Don't write &op0, etc., because that would prevent op0
3188 from being kept in a register.
3189 Instead, make copies of the our local variables and
3190 pass the copies by reference, then copy them back afterward. */
3191 tree xop0 = op0, xop1 = op1, xresult_type = result_type;
3192 enum tree_code xresultcode = resultcode;
3193 tree val
3194 = shorten_compare (&xop0, &xop1, &xresult_type, &xresultcode);
3195 if (val != 0)
3196 return cp_convert (boolean_type_node, val);
3197 op0 = xop0, op1 = xop1;
3198 converted = 1;
3199 resultcode = xresultcode;
3202 if ((short_compare || code == MIN_EXPR || code == MAX_EXPR)
3203 && warn_sign_compare
3204 /* Do not warn until the template is instantiated; we cannot
3205 bound the ranges of the arguments until that point. */
3206 && !processing_template_decl)
3208 int op0_signed = ! TREE_UNSIGNED (TREE_TYPE (orig_op0));
3209 int op1_signed = ! TREE_UNSIGNED (TREE_TYPE (orig_op1));
3211 int unsignedp0, unsignedp1;
3212 tree primop0 = get_narrower (op0, &unsignedp0);
3213 tree primop1 = get_narrower (op1, &unsignedp1);
3215 /* Check for comparison of different enum types. */
3216 if (TREE_CODE (TREE_TYPE (orig_op0)) == ENUMERAL_TYPE
3217 && TREE_CODE (TREE_TYPE (orig_op1)) == ENUMERAL_TYPE
3218 && TYPE_MAIN_VARIANT (TREE_TYPE (orig_op0))
3219 != TYPE_MAIN_VARIANT (TREE_TYPE (orig_op1)))
3221 warning ("comparison between types `%#T' and `%#T'",
3222 TREE_TYPE (orig_op0), TREE_TYPE (orig_op1));
3225 /* Give warnings for comparisons between signed and unsigned
3226 quantities that may fail. */
3227 /* Do the checking based on the original operand trees, so that
3228 casts will be considered, but default promotions won't be. */
3230 /* Do not warn if the comparison is being done in a signed type,
3231 since the signed type will only be chosen if it can represent
3232 all the values of the unsigned type. */
3233 if (! TREE_UNSIGNED (result_type))
3234 /* OK */;
3235 /* Do not warn if both operands are unsigned. */
3236 else if (op0_signed == op1_signed)
3237 /* OK */;
3238 /* Do not warn if the signed quantity is an unsuffixed
3239 integer literal (or some static constant expression
3240 involving such literals or a conditional expression
3241 involving such literals) and it is non-negative. */
3242 else if ((op0_signed && tree_expr_nonnegative_p (orig_op0))
3243 || (op1_signed && tree_expr_nonnegative_p (orig_op1)))
3244 /* OK */;
3245 /* Do not warn if the comparison is an equality operation,
3246 the unsigned quantity is an integral constant and it does
3247 not use the most significant bit of result_type. */
3248 else if ((resultcode == EQ_EXPR || resultcode == NE_EXPR)
3249 && ((op0_signed && TREE_CODE (orig_op1) == INTEGER_CST
3250 && int_fits_type_p (orig_op1, c_common_signed_type
3251 (result_type)))
3252 || (op1_signed && TREE_CODE (orig_op0) == INTEGER_CST
3253 && int_fits_type_p (orig_op0, c_common_signed_type
3254 (result_type)))))
3255 /* OK */;
3256 else
3257 warning ("comparison between signed and unsigned integer expressions");
3259 /* Warn if two unsigned values are being compared in a size
3260 larger than their original size, and one (and only one) is the
3261 result of a `~' operator. This comparison will always fail.
3263 Also warn if one operand is a constant, and the constant does not
3264 have all bits set that are set in the ~ operand when it is
3265 extended. */
3267 if ((TREE_CODE (primop0) == BIT_NOT_EXPR)
3268 ^ (TREE_CODE (primop1) == BIT_NOT_EXPR))
3270 if (TREE_CODE (primop0) == BIT_NOT_EXPR)
3271 primop0 = get_narrower (TREE_OPERAND (op0, 0), &unsignedp0);
3272 if (TREE_CODE (primop1) == BIT_NOT_EXPR)
3273 primop1 = get_narrower (TREE_OPERAND (op1, 0), &unsignedp1);
3275 if (host_integerp (primop0, 0) || host_integerp (primop1, 0))
3277 tree primop;
3278 HOST_WIDE_INT constant, mask;
3279 int unsignedp;
3280 unsigned int bits;
3282 if (host_integerp (primop0, 0))
3284 primop = primop1;
3285 unsignedp = unsignedp1;
3286 constant = tree_low_cst (primop0, 0);
3288 else
3290 primop = primop0;
3291 unsignedp = unsignedp0;
3292 constant = tree_low_cst (primop1, 0);
3295 bits = TYPE_PRECISION (TREE_TYPE (primop));
3296 if (bits < TYPE_PRECISION (result_type)
3297 && bits < HOST_BITS_PER_LONG && unsignedp)
3299 mask = (~ (HOST_WIDE_INT) 0) << bits;
3300 if ((mask & constant) != mask)
3301 warning ("comparison of promoted ~unsigned with constant");
3304 else if (unsignedp0 && unsignedp1
3305 && (TYPE_PRECISION (TREE_TYPE (primop0))
3306 < TYPE_PRECISION (result_type))
3307 && (TYPE_PRECISION (TREE_TYPE (primop1))
3308 < TYPE_PRECISION (result_type)))
3309 warning ("comparison of promoted ~unsigned with unsigned");
3314 /* At this point, RESULT_TYPE must be nonzero to avoid an error message.
3315 If CONVERTED is zero, both args will be converted to type RESULT_TYPE.
3316 Then the expression will be built.
3317 It will be given type FINAL_TYPE if that is nonzero;
3318 otherwise, it will be given type RESULT_TYPE. */
3320 if (!result_type)
3322 error ("invalid operands of types `%T' and `%T' to binary `%O'",
3323 TREE_TYPE (orig_op0), TREE_TYPE (orig_op1), code);
3324 return error_mark_node;
3327 /* Issue warnings about peculiar, but valid, uses of NULL. */
3328 if (/* It's reasonable to use pointer values as operands of &&
3329 and ||, so NULL is no exception. */
3330 !(code == TRUTH_ANDIF_EXPR || code == TRUTH_ORIF_EXPR)
3331 && (/* If OP0 is NULL and OP1 is not a pointer, or vice versa. */
3332 (orig_op0 == null_node
3333 && TREE_CODE (TREE_TYPE (op1)) != POINTER_TYPE)
3334 /* Or vice versa. */
3335 || (orig_op1 == null_node
3336 && TREE_CODE (TREE_TYPE (op0)) != POINTER_TYPE)
3337 /* Or, both are NULL and the operation was not a comparison. */
3338 || (orig_op0 == null_node && orig_op1 == null_node
3339 && code != EQ_EXPR && code != NE_EXPR)))
3340 /* Some sort of arithmetic operation involving NULL was
3341 performed. Note that pointer-difference and pointer-addition
3342 have already been handled above, and so we don't end up here in
3343 that case. */
3344 warning ("NULL used in arithmetic");
3346 if (! converted)
3348 if (TREE_TYPE (op0) != result_type)
3349 op0 = cp_convert (result_type, op0);
3350 if (TREE_TYPE (op1) != result_type)
3351 op1 = cp_convert (result_type, op1);
3353 if (op0 == error_mark_node || op1 == error_mark_node)
3354 return error_mark_node;
3357 if (build_type == NULL_TREE)
3358 build_type = result_type;
3361 register tree result = build (resultcode, build_type, op0, op1);
3362 register tree folded;
3364 folded = fold (result);
3365 if (folded == result)
3366 TREE_CONSTANT (folded) = TREE_CONSTANT (op0) & TREE_CONSTANT (op1);
3367 if (final_type != 0)
3368 return cp_convert (final_type, folded);
3369 return folded;
3373 /* Return a tree for the sum or difference (RESULTCODE says which)
3374 of pointer PTROP and integer INTOP. */
3376 static tree
3377 cp_pointer_int_sum (enum tree_code resultcode, register tree ptrop,
3378 register tree intop)
3380 tree res_type = TREE_TYPE (ptrop);
3382 /* pointer_int_sum() uses size_in_bytes() on the TREE_TYPE(res_type)
3383 in certain circumstance (when it's valid to do so). So we need
3384 to make sure it's complete. We don't need to check here, if we
3385 can actually complete it at all, as those checks will be done in
3386 pointer_int_sum() anyway. */
3387 complete_type (TREE_TYPE (res_type));
3389 return pointer_int_sum (resultcode, ptrop, fold (intop));
3392 /* Return a tree for the difference of pointers OP0 and OP1.
3393 The resulting tree has type int. */
3395 static tree
3396 pointer_diff (register tree op0, register tree op1, register tree ptrtype)
3398 register tree result, folded;
3399 tree restype = ptrdiff_type_node;
3400 tree target_type = TREE_TYPE (ptrtype);
3402 if (!complete_type_or_else (target_type, NULL_TREE))
3403 return error_mark_node;
3405 if (pedantic || warn_pointer_arith)
3407 if (TREE_CODE (target_type) == VOID_TYPE)
3408 pedwarn ("ISO C++ forbids using pointer of type `void *' in subtraction");
3409 if (TREE_CODE (target_type) == FUNCTION_TYPE)
3410 pedwarn ("ISO C++ forbids using pointer to a function in subtraction");
3411 if (TREE_CODE (target_type) == METHOD_TYPE)
3412 pedwarn ("ISO C++ forbids using pointer to a method in subtraction");
3415 /* First do the subtraction as integers;
3416 then drop through to build the divide operator. */
3418 op0 = cp_build_binary_op (MINUS_EXPR,
3419 cp_convert (restype, op0),
3420 cp_convert (restype, op1));
3422 /* This generates an error if op1 is a pointer to an incomplete type. */
3423 if (!COMPLETE_TYPE_P (TREE_TYPE (TREE_TYPE (op1))))
3424 error ("invalid use of a pointer to an incomplete type in pointer arithmetic");
3426 op1 = (TYPE_PTROB_P (ptrtype)
3427 ? size_in_bytes (target_type)
3428 : integer_one_node);
3430 /* Do the division. */
3432 result = build (EXACT_DIV_EXPR, restype, op0, cp_convert (restype, op1));
3434 folded = fold (result);
3435 if (folded == result)
3436 TREE_CONSTANT (folded) = TREE_CONSTANT (op0) & TREE_CONSTANT (op1);
3437 return folded;
3440 /* Construct and perhaps optimize a tree representation
3441 for a unary operation. CODE, a tree_code, specifies the operation
3442 and XARG is the operand. */
3444 tree
3445 build_x_unary_op (enum tree_code code, tree xarg)
3447 tree orig_expr = xarg;
3448 tree exp;
3449 int ptrmem = 0;
3451 if (processing_template_decl)
3453 if (type_dependent_expression_p (xarg))
3454 return build_min_nt (code, xarg, NULL_TREE);
3455 xarg = build_non_dependent_expr (xarg);
3458 exp = NULL_TREE;
3460 /* & rec, on incomplete RECORD_TYPEs is the simple opr &, not an
3461 error message. */
3462 if (code == ADDR_EXPR
3463 && TREE_CODE (xarg) != TEMPLATE_ID_EXPR
3464 && ((IS_AGGR_TYPE_CODE (TREE_CODE (TREE_TYPE (xarg)))
3465 && !COMPLETE_TYPE_P (TREE_TYPE (xarg)))
3466 || (TREE_CODE (xarg) == OFFSET_REF)))
3467 /* don't look for a function */;
3468 else
3469 exp = build_new_op (code, LOOKUP_NORMAL, xarg, NULL_TREE, NULL_TREE);
3470 if (!exp && code == ADDR_EXPR)
3472 /* A pointer to member-function can be formed only by saying
3473 &X::mf. */
3474 if (!flag_ms_extensions && TREE_CODE (TREE_TYPE (xarg)) == METHOD_TYPE
3475 && (TREE_CODE (xarg) != OFFSET_REF || !PTRMEM_OK_P (xarg)))
3477 if (TREE_CODE (xarg) != OFFSET_REF)
3479 error ("invalid use of '%E' to form a pointer-to-member-function. Use a qualified-id.",
3480 xarg);
3481 return error_mark_node;
3483 else
3485 error ("parenthesis around '%E' cannot be used to form a pointer-to-member-function",
3486 xarg);
3487 PTRMEM_OK_P (xarg) = 1;
3491 if (TREE_CODE (xarg) == OFFSET_REF)
3493 ptrmem = PTRMEM_OK_P (xarg);
3495 if (!ptrmem && !flag_ms_extensions
3496 && TREE_CODE (TREE_TYPE (TREE_OPERAND (xarg, 1))) == METHOD_TYPE)
3498 /* A single non-static member, make sure we don't allow a
3499 pointer-to-member. */
3500 xarg = build (OFFSET_REF, TREE_TYPE (xarg),
3501 TREE_OPERAND (xarg, 0),
3502 ovl_cons (TREE_OPERAND (xarg, 1), NULL_TREE));
3503 PTRMEM_OK_P (xarg) = ptrmem;
3506 else if (TREE_CODE (xarg) == TARGET_EXPR)
3507 warning ("taking address of temporary");
3508 exp = build_unary_op (ADDR_EXPR, xarg, 0);
3509 if (TREE_CODE (exp) == ADDR_EXPR)
3510 PTRMEM_OK_P (exp) = ptrmem;
3513 if (processing_template_decl && exp != error_mark_node)
3514 return build_min (code, TREE_TYPE (exp), orig_expr, NULL_TREE);
3515 return exp;
3518 /* Like c_common_truthvalue_conversion, but handle pointer-to-member
3519 constants, where a null value is represented by an INTEGER_CST of
3520 -1. */
3522 tree
3523 cp_truthvalue_conversion (tree expr)
3525 tree type = TREE_TYPE (expr);
3526 if (TYPE_PTRMEM_P (type))
3527 return build_binary_op (NE_EXPR, expr, integer_zero_node, 1);
3528 else
3529 return c_common_truthvalue_conversion (expr);
3532 /* Just like cp_truthvalue_conversion, but we want a CLEANUP_POINT_EXPR. */
3534 tree
3535 condition_conversion (tree expr)
3537 tree t;
3538 if (processing_template_decl)
3539 return expr;
3540 t = perform_implicit_conversion (boolean_type_node, expr);
3541 t = fold (build1 (CLEANUP_POINT_EXPR, boolean_type_node, t));
3542 return t;
3545 /* Return an ADDR_EXPR giving the address of T. This function
3546 attempts no optimizations or simplifications; it is a low-level
3547 primitive. */
3549 tree
3550 build_address (tree t)
3552 tree addr;
3554 if (error_operand_p (t) || !cxx_mark_addressable (t))
3555 return error_mark_node;
3557 addr = build1 (ADDR_EXPR,
3558 build_pointer_type (TREE_TYPE (t)),
3560 if (staticp (t))
3561 TREE_CONSTANT (addr) = 1;
3563 return addr;
3566 /* Return a NOP_EXPR converting EXPR to TYPE. */
3568 tree
3569 build_nop (tree type, tree expr)
3571 tree nop;
3573 if (type == error_mark_node || error_operand_p (expr))
3574 return expr;
3576 nop = build1 (NOP_EXPR, type, expr);
3577 if (TREE_CONSTANT (expr))
3578 TREE_CONSTANT (nop) = 1;
3580 return nop;
3583 /* C++: Must handle pointers to members.
3585 Perhaps type instantiation should be extended to handle conversion
3586 from aggregates to types we don't yet know we want? (Or are those
3587 cases typically errors which should be reported?)
3589 NOCONVERT nonzero suppresses the default promotions
3590 (such as from short to int). */
3592 tree
3593 build_unary_op (enum tree_code code, tree xarg, int noconvert)
3595 /* No default_conversion here. It causes trouble for ADDR_EXPR. */
3596 register tree arg = xarg;
3597 register tree argtype = 0;
3598 const char *errstring = NULL;
3599 tree val;
3601 if (arg == error_mark_node)
3602 return error_mark_node;
3604 switch (code)
3606 case CONVERT_EXPR:
3607 /* This is used for unary plus, because a CONVERT_EXPR
3608 is enough to prevent anybody from looking inside for
3609 associativity, but won't generate any code. */
3610 if (!(arg = build_expr_type_conversion
3611 (WANT_ARITH | WANT_ENUM | WANT_POINTER, arg, true)))
3612 errstring = "wrong type argument to unary plus";
3613 else
3615 if (!noconvert)
3616 arg = default_conversion (arg);
3617 arg = build1 (NON_LVALUE_EXPR, TREE_TYPE (arg), arg);
3618 TREE_CONSTANT (arg) = TREE_CONSTANT (TREE_OPERAND (arg, 0));
3620 break;
3622 case NEGATE_EXPR:
3623 if (!(arg = build_expr_type_conversion (WANT_ARITH | WANT_ENUM, arg, true)))
3624 errstring = "wrong type argument to unary minus";
3625 else if (!noconvert && CP_INTEGRAL_TYPE_P (TREE_TYPE (arg)))
3626 arg = perform_integral_promotions (arg);
3627 break;
3629 case BIT_NOT_EXPR:
3630 if (TREE_CODE (TREE_TYPE (arg)) == COMPLEX_TYPE)
3632 code = CONJ_EXPR;
3633 if (!noconvert)
3634 arg = default_conversion (arg);
3636 else if (!(arg = build_expr_type_conversion (WANT_INT | WANT_ENUM,
3637 arg, true)))
3638 errstring = "wrong type argument to bit-complement";
3639 else if (!noconvert)
3640 arg = perform_integral_promotions (arg);
3641 break;
3643 case ABS_EXPR:
3644 if (!(arg = build_expr_type_conversion (WANT_ARITH | WANT_ENUM, arg, true)))
3645 errstring = "wrong type argument to abs";
3646 else if (!noconvert)
3647 arg = default_conversion (arg);
3648 break;
3650 case CONJ_EXPR:
3651 /* Conjugating a real value is a no-op, but allow it anyway. */
3652 if (!(arg = build_expr_type_conversion (WANT_ARITH | WANT_ENUM, arg, true)))
3653 errstring = "wrong type argument to conjugation";
3654 else if (!noconvert)
3655 arg = default_conversion (arg);
3656 break;
3658 case TRUTH_NOT_EXPR:
3659 arg = perform_implicit_conversion (boolean_type_node, arg);
3660 val = invert_truthvalue (arg);
3661 if (arg != error_mark_node)
3662 return val;
3663 errstring = "in argument to unary !";
3664 break;
3666 case NOP_EXPR:
3667 break;
3669 case REALPART_EXPR:
3670 if (TREE_CODE (arg) == COMPLEX_CST)
3671 return TREE_REALPART (arg);
3672 else if (TREE_CODE (TREE_TYPE (arg)) == COMPLEX_TYPE)
3673 return fold (build1 (REALPART_EXPR, TREE_TYPE (TREE_TYPE (arg)), arg));
3674 else
3675 return arg;
3677 case IMAGPART_EXPR:
3678 if (TREE_CODE (arg) == COMPLEX_CST)
3679 return TREE_IMAGPART (arg);
3680 else if (TREE_CODE (TREE_TYPE (arg)) == COMPLEX_TYPE)
3681 return fold (build1 (IMAGPART_EXPR, TREE_TYPE (TREE_TYPE (arg)), arg));
3682 else
3683 return cp_convert (TREE_TYPE (arg), integer_zero_node);
3685 case PREINCREMENT_EXPR:
3686 case POSTINCREMENT_EXPR:
3687 case PREDECREMENT_EXPR:
3688 case POSTDECREMENT_EXPR:
3689 /* Handle complex lvalues (when permitted)
3690 by reduction to simpler cases. */
3692 val = unary_complex_lvalue (code, arg);
3693 if (val != 0)
3694 return val;
3696 /* Increment or decrement the real part of the value,
3697 and don't change the imaginary part. */
3698 if (TREE_CODE (TREE_TYPE (arg)) == COMPLEX_TYPE)
3700 tree real, imag;
3702 arg = stabilize_reference (arg);
3703 real = build_unary_op (REALPART_EXPR, arg, 1);
3704 imag = build_unary_op (IMAGPART_EXPR, arg, 1);
3705 return build (COMPLEX_EXPR, TREE_TYPE (arg),
3706 build_unary_op (code, real, 1), imag);
3709 /* Report invalid types. */
3711 if (!(arg = build_expr_type_conversion (WANT_ARITH | WANT_POINTER,
3712 arg, true)))
3714 if (code == PREINCREMENT_EXPR)
3715 errstring ="no pre-increment operator for type";
3716 else if (code == POSTINCREMENT_EXPR)
3717 errstring ="no post-increment operator for type";
3718 else if (code == PREDECREMENT_EXPR)
3719 errstring ="no pre-decrement operator for type";
3720 else
3721 errstring ="no post-decrement operator for type";
3722 break;
3725 /* Report something read-only. */
3727 if (CP_TYPE_CONST_P (TREE_TYPE (arg))
3728 || TREE_READONLY (arg))
3729 readonly_error (arg, ((code == PREINCREMENT_EXPR
3730 || code == POSTINCREMENT_EXPR)
3731 ? "increment" : "decrement"),
3735 register tree inc;
3736 tree result_type = TREE_TYPE (arg);
3738 arg = get_unwidened (arg, 0);
3739 argtype = TREE_TYPE (arg);
3741 /* ARM $5.2.5 last annotation says this should be forbidden. */
3742 if (TREE_CODE (argtype) == ENUMERAL_TYPE)
3743 pedwarn ("ISO C++ forbids %sing an enum",
3744 (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
3745 ? "increment" : "decrement");
3747 /* Compute the increment. */
3749 if (TREE_CODE (argtype) == POINTER_TYPE)
3751 tree type = complete_type (TREE_TYPE (argtype));
3753 if (!COMPLETE_OR_VOID_TYPE_P (type))
3754 error ("cannot %s a pointer to incomplete type `%T'",
3755 ((code == PREINCREMENT_EXPR
3756 || code == POSTINCREMENT_EXPR)
3757 ? "increment" : "decrement"), TREE_TYPE (argtype));
3758 else if ((pedantic || warn_pointer_arith)
3759 && !TYPE_PTROB_P (argtype))
3760 pedwarn ("ISO C++ forbids %sing a pointer of type `%T'",
3761 ((code == PREINCREMENT_EXPR
3762 || code == POSTINCREMENT_EXPR)
3763 ? "increment" : "decrement"), argtype);
3764 inc = cxx_sizeof_nowarn (TREE_TYPE (argtype));
3766 else
3767 inc = integer_one_node;
3769 inc = cp_convert (argtype, inc);
3771 /* Handle incrementing a cast-expression. */
3773 switch (TREE_CODE (arg))
3775 case NOP_EXPR:
3776 case CONVERT_EXPR:
3777 case FLOAT_EXPR:
3778 case FIX_TRUNC_EXPR:
3779 case FIX_FLOOR_EXPR:
3780 case FIX_ROUND_EXPR:
3781 case FIX_CEIL_EXPR:
3783 tree incremented, modify, value, compound;
3784 if (! lvalue_p (arg) && pedantic)
3785 pedwarn ("cast to non-reference type used as lvalue");
3786 arg = stabilize_reference (arg);
3787 if (code == PREINCREMENT_EXPR || code == PREDECREMENT_EXPR)
3788 value = arg;
3789 else
3790 value = save_expr (arg);
3791 incremented = build (((code == PREINCREMENT_EXPR
3792 || code == POSTINCREMENT_EXPR)
3793 ? PLUS_EXPR : MINUS_EXPR),
3794 argtype, value, inc);
3796 modify = build_modify_expr (arg, NOP_EXPR, incremented);
3797 compound = build (COMPOUND_EXPR, TREE_TYPE (arg), modify, value);
3799 /* Eliminate warning about unused result of + or -. */
3800 TREE_NO_UNUSED_WARNING (compound) = 1;
3801 return compound;
3804 default:
3805 break;
3808 /* Complain about anything else that is not a true lvalue. */
3809 if (!lvalue_or_else (arg, ((code == PREINCREMENT_EXPR
3810 || code == POSTINCREMENT_EXPR)
3811 ? "increment" : "decrement")))
3812 return error_mark_node;
3814 /* Forbid using -- on `bool'. */
3815 if (TREE_TYPE (arg) == boolean_type_node)
3817 if (code == POSTDECREMENT_EXPR || code == PREDECREMENT_EXPR)
3819 error ("invalid use of `--' on bool variable `%D'", arg);
3820 return error_mark_node;
3822 val = boolean_increment (code, arg);
3824 else
3825 val = build (code, TREE_TYPE (arg), arg, inc);
3827 TREE_SIDE_EFFECTS (val) = 1;
3828 return cp_convert (result_type, val);
3831 case ADDR_EXPR:
3832 /* Note that this operation never does default_conversion
3833 regardless of NOCONVERT. */
3835 argtype = lvalue_type (arg);
3837 if (TREE_CODE (arg) == OFFSET_REF)
3838 goto offset_ref;
3840 if (TREE_CODE (argtype) == REFERENCE_TYPE)
3842 arg = build1
3843 (CONVERT_EXPR,
3844 build_pointer_type (TREE_TYPE (argtype)), arg);
3845 TREE_CONSTANT (arg) = TREE_CONSTANT (TREE_OPERAND (arg, 0));
3846 return arg;
3848 else if (pedantic && DECL_MAIN_P (arg))
3849 /* ARM $3.4 */
3850 pedwarn ("ISO C++ forbids taking address of function `::main'");
3852 /* Let &* cancel out to simplify resulting code. */
3853 if (TREE_CODE (arg) == INDIRECT_REF)
3855 /* We don't need to have `current_class_ptr' wrapped in a
3856 NON_LVALUE_EXPR node. */
3857 if (arg == current_class_ref)
3858 return current_class_ptr;
3860 arg = TREE_OPERAND (arg, 0);
3861 if (TREE_CODE (TREE_TYPE (arg)) == REFERENCE_TYPE)
3863 arg = build1
3864 (CONVERT_EXPR,
3865 build_pointer_type (TREE_TYPE (TREE_TYPE (arg))), arg);
3866 TREE_CONSTANT (arg) = TREE_CONSTANT (TREE_OPERAND (arg, 0));
3868 else if (lvalue_p (arg))
3869 /* Don't let this be an lvalue. */
3870 return non_lvalue (arg);
3871 return arg;
3874 /* For &x[y], return x+y */
3875 if (TREE_CODE (arg) == ARRAY_REF)
3877 if (!cxx_mark_addressable (TREE_OPERAND (arg, 0)))
3878 return error_mark_node;
3879 return cp_build_binary_op (PLUS_EXPR, TREE_OPERAND (arg, 0),
3880 TREE_OPERAND (arg, 1));
3883 /* Uninstantiated types are all functions. Taking the
3884 address of a function is a no-op, so just return the
3885 argument. */
3887 if (TREE_CODE (arg) == IDENTIFIER_NODE
3888 && IDENTIFIER_OPNAME_P (arg))
3890 abort ();
3891 /* We don't know the type yet, so just work around the problem.
3892 We know that this will resolve to an lvalue. */
3893 return build1 (ADDR_EXPR, unknown_type_node, arg);
3896 if (TREE_CODE (arg) == COMPONENT_REF && type_unknown_p (arg)
3897 && !really_overloaded_fn (TREE_OPERAND (arg, 1)))
3899 /* They're trying to take the address of a unique non-static
3900 member function. This is ill-formed (except in MS-land),
3901 but let's try to DTRT.
3902 Note: We only handle unique functions here because we don't
3903 want to complain if there's a static overload; non-unique
3904 cases will be handled by instantiate_type. But we need to
3905 handle this case here to allow casts on the resulting PMF.
3906 We could defer this in non-MS mode, but it's easier to give
3907 a useful error here. */
3909 /* Inside constant member functions, the `this' pointer
3910 contains an extra const qualifier. TYPE_MAIN_VARIANT
3911 is used here to remove this const from the diagnostics
3912 and the created OFFSET_REF. */
3913 tree base = TYPE_MAIN_VARIANT (TREE_TYPE (TREE_OPERAND (arg, 0)));
3914 tree name = DECL_NAME (get_first_fn (TREE_OPERAND (arg, 1)));
3916 if (! flag_ms_extensions)
3918 if (current_class_type
3919 && TREE_OPERAND (arg, 0) == current_class_ref)
3920 /* An expression like &memfn. */
3921 pedwarn ("ISO C++ forbids taking the address of an unqualified"
3922 " or parenthesized non-static member function to form"
3923 " a pointer to member function. Say `&%T::%D'",
3924 base, name);
3925 else
3926 pedwarn ("ISO C++ forbids taking the address of a bound member"
3927 " function to form a pointer to member function."
3928 " Say `&%T::%D'",
3929 base, name);
3931 arg = build_offset_ref (base, name, /*address_p=*/true);
3934 offset_ref:
3935 if (type_unknown_p (arg))
3936 return build1 (ADDR_EXPR, unknown_type_node, arg);
3938 /* Handle complex lvalues (when permitted)
3939 by reduction to simpler cases. */
3940 val = unary_complex_lvalue (code, arg);
3941 if (val != 0)
3942 return val;
3944 switch (TREE_CODE (arg))
3946 case NOP_EXPR:
3947 case CONVERT_EXPR:
3948 case FLOAT_EXPR:
3949 case FIX_TRUNC_EXPR:
3950 case FIX_FLOOR_EXPR:
3951 case FIX_ROUND_EXPR:
3952 case FIX_CEIL_EXPR:
3953 if (! lvalue_p (arg) && pedantic)
3954 pedwarn ("ISO C++ forbids taking the address of a cast to a non-lvalue expression");
3955 break;
3957 default:
3958 break;
3961 /* Allow the address of a constructor if all the elements
3962 are constant. */
3963 if (TREE_CODE (arg) == CONSTRUCTOR && TREE_HAS_CONSTRUCTOR (arg)
3964 && TREE_CONSTANT (arg))
3966 /* Anything not already handled and not a true memory reference
3967 is an error. */
3968 else if (TREE_CODE (argtype) != FUNCTION_TYPE
3969 && TREE_CODE (argtype) != METHOD_TYPE
3970 && !non_cast_lvalue_or_else (arg, "unary `&'"))
3971 return error_mark_node;
3973 if (argtype != error_mark_node)
3974 argtype = build_pointer_type (argtype);
3977 tree addr;
3979 if (TREE_CODE (arg) == COMPONENT_REF
3980 && TREE_CODE (TREE_OPERAND (arg, 1)) == BASELINK)
3981 arg = BASELINK_FUNCTIONS (TREE_OPERAND (arg, 1));
3983 if (TREE_CODE (arg) != COMPONENT_REF)
3984 addr = build_address (arg);
3985 else if (DECL_C_BIT_FIELD (TREE_OPERAND (arg, 1)))
3987 error ("attempt to take address of bit-field structure member `%D'",
3988 TREE_OPERAND (arg, 1));
3989 return error_mark_node;
3991 else
3993 /* Unfortunately we cannot just build an address
3994 expression here, because we would not handle
3995 address-constant-expressions or offsetof correctly. */
3996 tree field = TREE_OPERAND (arg, 1);
3997 tree rval = build_unary_op (ADDR_EXPR, TREE_OPERAND (arg, 0), 0);
3998 tree binfo = lookup_base (TREE_TYPE (TREE_TYPE (rval)),
3999 decl_type_context (field),
4000 ba_check, NULL);
4002 rval = build_base_path (PLUS_EXPR, rval, binfo, 1);
4003 rval = build_nop (argtype, rval);
4004 addr = fold (build (PLUS_EXPR, argtype, rval,
4005 cp_convert (argtype, byte_position (field))));
4008 if (TREE_CODE (argtype) == POINTER_TYPE
4009 && TREE_CODE (TREE_TYPE (argtype)) == METHOD_TYPE)
4011 build_ptrmemfunc_type (argtype);
4012 addr = build_ptrmemfunc (argtype, addr, 0);
4015 return addr;
4018 default:
4019 break;
4022 if (!errstring)
4024 if (argtype == 0)
4025 argtype = TREE_TYPE (arg);
4026 return fold (build1 (code, argtype, arg));
4029 error ("%s", errstring);
4030 return error_mark_node;
4033 /* Apply unary lvalue-demanding operator CODE to the expression ARG
4034 for certain kinds of expressions which are not really lvalues
4035 but which we can accept as lvalues.
4037 If ARG is not a kind of expression we can handle, return zero. */
4039 tree
4040 unary_complex_lvalue (enum tree_code code, tree arg)
4042 /* Handle (a, b) used as an "lvalue". */
4043 if (TREE_CODE (arg) == COMPOUND_EXPR)
4045 tree real_result = build_unary_op (code, TREE_OPERAND (arg, 1), 0);
4046 return build (COMPOUND_EXPR, TREE_TYPE (real_result),
4047 TREE_OPERAND (arg, 0), real_result);
4050 /* Handle (a ? b : c) used as an "lvalue". */
4051 if (TREE_CODE (arg) == COND_EXPR
4052 || TREE_CODE (arg) == MIN_EXPR || TREE_CODE (arg) == MAX_EXPR)
4053 return rationalize_conditional_expr (code, arg);
4055 /* Handle (a = b), (++a), and (--a) used as an "lvalue". */
4056 if (TREE_CODE (arg) == MODIFY_EXPR
4057 || TREE_CODE (arg) == PREINCREMENT_EXPR
4058 || TREE_CODE (arg) == PREDECREMENT_EXPR)
4060 tree lvalue = TREE_OPERAND (arg, 0);
4061 if (TREE_SIDE_EFFECTS (lvalue))
4063 lvalue = stabilize_reference (lvalue);
4064 arg = build (TREE_CODE (arg), TREE_TYPE (arg),
4065 lvalue, TREE_OPERAND (arg, 1));
4067 return unary_complex_lvalue
4068 (code, build (COMPOUND_EXPR, TREE_TYPE (lvalue), arg, lvalue));
4071 if (code != ADDR_EXPR)
4072 return 0;
4074 /* Handle (a = b) used as an "lvalue" for `&'. */
4075 if (TREE_CODE (arg) == MODIFY_EXPR
4076 || TREE_CODE (arg) == INIT_EXPR)
4078 tree real_result = build_unary_op (code, TREE_OPERAND (arg, 0), 0);
4079 arg = build (COMPOUND_EXPR, TREE_TYPE (real_result), arg, real_result);
4080 TREE_NO_UNUSED_WARNING (arg) = 1;
4081 return arg;
4084 if (TREE_CODE (TREE_TYPE (arg)) == FUNCTION_TYPE
4085 || TREE_CODE (TREE_TYPE (arg)) == METHOD_TYPE
4086 || TREE_CODE (arg) == OFFSET_REF)
4088 tree t;
4090 my_friendly_assert (TREE_CODE (arg) != SCOPE_REF, 313);
4092 if (TREE_CODE (arg) != OFFSET_REF)
4093 return 0;
4095 t = TREE_OPERAND (arg, 1);
4097 /* Check all this code for right semantics. */
4098 if (TREE_CODE (t) == FUNCTION_DECL)
4100 if (DECL_DESTRUCTOR_P (t))
4101 error ("taking address of destructor");
4102 return build_unary_op (ADDR_EXPR, t, 0);
4104 if (TREE_CODE (t) == VAR_DECL)
4105 return build_unary_op (ADDR_EXPR, t, 0);
4106 else
4108 tree type;
4110 if (TREE_OPERAND (arg, 0)
4111 && ! is_dummy_object (TREE_OPERAND (arg, 0))
4112 && TREE_CODE (t) != FIELD_DECL)
4114 error ("taking address of bound pointer-to-member expression");
4115 return error_mark_node;
4117 if (!PTRMEM_OK_P (arg))
4118 return build_unary_op (code, arg, 0);
4120 if (TREE_CODE (TREE_TYPE (t)) == REFERENCE_TYPE)
4122 error ("cannot create pointer to reference member `%D'", t);
4123 return error_mark_node;
4126 type = build_ptrmem_type (DECL_FIELD_CONTEXT (t), TREE_TYPE (t));
4127 t = make_ptrmem_cst (type, TREE_OPERAND (arg, 1));
4128 return t;
4133 /* We permit compiler to make function calls returning
4134 objects of aggregate type look like lvalues. */
4136 tree targ = arg;
4138 if (TREE_CODE (targ) == SAVE_EXPR)
4139 targ = TREE_OPERAND (targ, 0);
4141 if (TREE_CODE (targ) == CALL_EXPR && IS_AGGR_TYPE (TREE_TYPE (targ)))
4143 if (TREE_CODE (arg) == SAVE_EXPR)
4144 targ = arg;
4145 else
4146 targ = build_cplus_new (TREE_TYPE (arg), arg);
4147 return build1 (ADDR_EXPR, build_pointer_type (TREE_TYPE (arg)), targ);
4150 if (TREE_CODE (arg) == SAVE_EXPR && TREE_CODE (targ) == INDIRECT_REF)
4151 return build (SAVE_EXPR, build_pointer_type (TREE_TYPE (arg)),
4152 TREE_OPERAND (targ, 0), current_function_decl, NULL);
4155 /* Don't let anything else be handled specially. */
4156 return 0;
4159 /* Mark EXP saying that we need to be able to take the
4160 address of it; it should not be allocated in a register.
4161 Value is true if successful.
4163 C++: we do not allow `current_class_ptr' to be addressable. */
4165 bool
4166 cxx_mark_addressable (tree exp)
4168 register tree x = exp;
4170 while (1)
4171 switch (TREE_CODE (x))
4173 case ADDR_EXPR:
4174 case COMPONENT_REF:
4175 case ARRAY_REF:
4176 case REALPART_EXPR:
4177 case IMAGPART_EXPR:
4178 x = TREE_OPERAND (x, 0);
4179 break;
4181 case PARM_DECL:
4182 if (x == current_class_ptr)
4184 error ("cannot take the address of `this', which is an rvalue expression");
4185 TREE_ADDRESSABLE (x) = 1; /* so compiler doesn't die later */
4186 return true;
4188 /* FALLTHRU */
4190 case VAR_DECL:
4191 /* Caller should not be trying to mark initialized
4192 constant fields addressable. */
4193 my_friendly_assert (DECL_LANG_SPECIFIC (x) == 0
4194 || DECL_IN_AGGR_P (x) == 0
4195 || TREE_STATIC (x)
4196 || DECL_EXTERNAL (x), 314);
4197 /* FALLTHRU */
4199 case CONST_DECL:
4200 case RESULT_DECL:
4201 if (DECL_REGISTER (x) && !TREE_ADDRESSABLE (x)
4202 && !DECL_ARTIFICIAL (x) && extra_warnings)
4203 warning ("address requested for `%D', which is declared `register'",
4205 TREE_ADDRESSABLE (x) = 1;
4206 put_var_into_stack (x, /*rescan=*/true);
4207 return true;
4209 case FUNCTION_DECL:
4210 TREE_ADDRESSABLE (x) = 1;
4211 TREE_ADDRESSABLE (DECL_ASSEMBLER_NAME (x)) = 1;
4212 return true;
4214 case CONSTRUCTOR:
4215 TREE_ADDRESSABLE (x) = 1;
4216 return true;
4218 case TARGET_EXPR:
4219 TREE_ADDRESSABLE (x) = 1;
4220 cxx_mark_addressable (TREE_OPERAND (x, 0));
4221 return true;
4223 default:
4224 return true;
4228 /* Build and return a conditional expression IFEXP ? OP1 : OP2. */
4230 tree
4231 build_x_conditional_expr (tree ifexp, tree op1, tree op2)
4233 tree orig_ifexp = ifexp;
4234 tree orig_op1 = op1;
4235 tree orig_op2 = op2;
4236 tree expr;
4238 if (processing_template_decl)
4240 /* The standard says that the expression is type-dependent if
4241 IFEXP is type-dependent, even though the eventual type of the
4242 expression doesn't dependent on IFEXP. */
4243 if (type_dependent_expression_p (ifexp)
4244 || type_dependent_expression_p (op1)
4245 || type_dependent_expression_p (op2))
4246 return build_min_nt (COND_EXPR, ifexp, op1, op2);
4247 ifexp = build_non_dependent_expr (ifexp);
4248 op1 = build_non_dependent_expr (op1);
4249 op2 = build_non_dependent_expr (op2);
4252 expr = build_conditional_expr (ifexp, op1, op2);
4253 if (processing_template_decl && expr != error_mark_node)
4254 return build_min (COND_EXPR, TREE_TYPE (expr),
4255 orig_ifexp, orig_op1, orig_op2);
4256 return expr;
4259 /* Given a list of expressions, return a compound expression
4260 that performs them all and returns the value of the last of them. */
4262 tree build_x_compound_expr_from_list (tree list, const char *msg)
4264 tree expr = TREE_VALUE (list);
4266 if (TREE_CHAIN (list))
4268 if (msg)
4269 pedwarn ("%s expression list treated as compound expression", msg);
4271 for (list = TREE_CHAIN (list); list; list = TREE_CHAIN (list))
4272 expr = build_x_compound_expr (expr, TREE_VALUE (list));
4275 return expr;
4278 /* Handle overloading of the ',' operator when needed. */
4280 tree
4281 build_x_compound_expr (tree op1, tree op2)
4283 tree result;
4284 tree orig_op1 = op1;
4285 tree orig_op2 = op2;
4287 if (processing_template_decl)
4289 if (type_dependent_expression_p (op1)
4290 || type_dependent_expression_p (op2))
4291 return build_min_nt (COMPOUND_EXPR, op1, op2);
4292 op1 = build_non_dependent_expr (op1);
4293 op2 = build_non_dependent_expr (op2);
4296 result = build_new_op (COMPOUND_EXPR, LOOKUP_NORMAL, op1, op2, NULL_TREE);
4297 if (!result)
4298 result = build_compound_expr (op1, op2);
4300 if (processing_template_decl && result != error_mark_node)
4301 return build_min (COMPOUND_EXPR, TREE_TYPE (result),
4302 orig_op1, orig_op2);
4303 return result;
4306 /* Build a compound expression. */
4308 tree
4309 build_compound_expr (tree lhs, tree rhs)
4311 lhs = decl_constant_value (lhs);
4312 lhs = convert_to_void (lhs, "left-hand operand of comma");
4314 if (lhs == error_mark_node || rhs == error_mark_node)
4315 return error_mark_node;
4317 if (TREE_CODE (rhs) == TARGET_EXPR)
4319 /* If the rhs is a TARGET_EXPR, then build the compound
4320 expression inside the target_expr's initializer. This
4321 helps the compiler to eliminate unncessary temporaries. */
4322 tree init = TREE_OPERAND (rhs, 1);
4324 init = build (COMPOUND_EXPR, TREE_TYPE (init), lhs, init);
4325 TREE_OPERAND (rhs, 1) = init;
4327 return rhs;
4330 return build (COMPOUND_EXPR, TREE_TYPE (rhs), lhs, rhs);
4333 /* Issue an error message if casting from SRC_TYPE to DEST_TYPE casts
4334 away constness. DESCRIPTION explains what operation is taking
4335 place. */
4337 static void
4338 check_for_casting_away_constness (tree src_type, tree dest_type,
4339 const char *description)
4341 if (casts_away_constness (src_type, dest_type))
4342 error ("%s from type `%T' to type `%T' casts away constness",
4343 description, src_type, dest_type);
4346 /* Return an expression representing static_cast<TYPE>(EXPR). */
4348 tree
4349 build_static_cast (tree type, tree expr)
4351 tree intype;
4352 tree result;
4354 if (type == error_mark_node || expr == error_mark_node)
4355 return error_mark_node;
4357 if (processing_template_decl)
4359 tree t = build_min (STATIC_CAST_EXPR, type, expr);
4360 return t;
4363 /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
4364 Strip such NOP_EXPRs if VALUE is being used in non-lvalue context. */
4365 if (TREE_CODE (type) != REFERENCE_TYPE
4366 && TREE_CODE (expr) == NOP_EXPR
4367 && TREE_TYPE (expr) == TREE_TYPE (TREE_OPERAND (expr, 0)))
4368 expr = TREE_OPERAND (expr, 0);
4370 intype = TREE_TYPE (expr);
4372 /* [expr.static.cast]
4374 An lvalue of type "cv1 B", where B is a class type, can be cast
4375 to type "reference to cv2 D", where D is a class derived (clause
4376 _class.derived_) from B, if a valid standard conversion from
4377 "pointer to D" to "pointer to B" exists (_conv.ptr_), cv2 is the
4378 same cv-qualification as, or greater cv-qualification than, cv1,
4379 and B is not a virtual base class of D. */
4380 /* We check this case before checking the validity of "TYPE t =
4381 EXPR;" below because for this case:
4383 struct B {};
4384 struct D : public B { D(const B&); };
4385 extern B& b;
4386 void f() { static_cast<const D&>(b); }
4388 we want to avoid constructing a new D. The standard is not
4389 completely clear about this issue, but our interpretation is
4390 consistent with other compilers. */
4391 if (TREE_CODE (type) == REFERENCE_TYPE
4392 && CLASS_TYPE_P (TREE_TYPE (type))
4393 && CLASS_TYPE_P (intype)
4394 && real_non_cast_lvalue_p (expr)
4395 && DERIVED_FROM_P (intype, TREE_TYPE (type))
4396 && can_convert (build_pointer_type (TYPE_MAIN_VARIANT (intype)),
4397 build_pointer_type (TYPE_MAIN_VARIANT
4398 (TREE_TYPE (type))))
4399 && at_least_as_qualified_p (TREE_TYPE (type), intype))
4401 /* At this point we have checked all of the conditions except
4402 that B is not a virtual base class of D. That will be
4403 checked by build_base_path. */
4404 tree base = lookup_base (TREE_TYPE (type), intype, ba_any, NULL);
4406 /* Convert from B* to D*. */
4407 expr = build_base_path (MINUS_EXPR, build_address (expr),
4408 base, /*nonnull=*/false);
4409 /* Convert the pointer to a reference -- but then remember that
4410 there are no expressions with reference type in C++. */
4411 return convert_from_reference (build_nop (type, expr));
4414 /* [expr.static.cast]
4416 An expression e can be explicitly converted to a type T using a
4417 static_cast of the form static_cast<T>(e) if the declaration T
4418 t(e);" is well-formed, for some invented temporary variable
4419 t. */
4420 result = perform_direct_initialization_if_possible (type, expr);
4421 if (result)
4422 return convert_from_reference (result);
4424 /* [expr.static.cast]
4426 Any expression can be explicitly converted to type cv void. */
4427 if (TREE_CODE (type) == VOID_TYPE)
4428 return convert_to_void (expr, /*implicit=*/NULL);
4430 /* [expr.static.cast]
4432 The inverse of any standard conversion sequence (clause _conv_),
4433 other than the lvalue-to-rvalue (_conv.lval_), array-to-pointer
4434 (_conv.array_), function-to-pointer (_conv.func_), and boolean
4435 (_conv.bool_) conversions, can be performed explicitly using
4436 static_cast subject to the restriction that the explicit
4437 conversion does not cast away constness (_expr.const.cast_), and
4438 the following additional rules for specific cases: */
4439 /* For reference, the conversions not excluded are: integral
4440 promotions, floating point promotion, integral conversions,
4441 floating point conversions, floating-integral conversions,
4442 pointer conversions, and pointer to member conversions. */
4443 if ((ARITHMETIC_TYPE_P (type) && ARITHMETIC_TYPE_P (intype))
4444 /* DR 128
4446 A value of integral _or enumeration_ type can be explicitly
4447 converted to an enumeration type. */
4448 || (INTEGRAL_OR_ENUMERATION_TYPE_P (type)
4449 && INTEGRAL_OR_ENUMERATION_TYPE_P (intype)))
4450 /* Really, build_c_cast should defer to this function rather
4451 than the other way around. */
4452 return build_c_cast (type, expr);
4453 if (TYPE_PTR_P (type) && TYPE_PTR_P (intype)
4454 && CLASS_TYPE_P (TREE_TYPE (type))
4455 && CLASS_TYPE_P (TREE_TYPE (intype))
4456 && can_convert (build_pointer_type (TYPE_MAIN_VARIANT
4457 (TREE_TYPE (intype))),
4458 build_pointer_type (TYPE_MAIN_VARIANT
4459 (TREE_TYPE (type)))))
4461 tree base;
4463 check_for_casting_away_constness (intype, type, "static_cast");
4464 base = lookup_base (TREE_TYPE (type), TREE_TYPE (intype),
4465 ba_check | ba_quiet, NULL);
4466 return build_base_path (MINUS_EXPR, expr, base, /*nonnull=*/false);
4468 if ((TYPE_PTRMEM_P (type) && TYPE_PTRMEM_P (intype))
4469 || (TYPE_PTRMEMFUNC_P (type) && TYPE_PTRMEMFUNC_P (intype)))
4471 tree c1;
4472 tree c2;
4473 tree t1;
4474 tree t2;
4476 c1 = TYPE_PTRMEM_CLASS_TYPE (intype);
4477 c2 = TYPE_PTRMEM_CLASS_TYPE (type);
4479 if (TYPE_PTRMEM_P (type))
4481 t1 = (build_ptrmem_type
4482 (c1,
4483 TYPE_MAIN_VARIANT (TYPE_PTRMEM_POINTED_TO_TYPE (intype))));
4484 t2 = (build_ptrmem_type
4485 (c2,
4486 TYPE_MAIN_VARIANT (TYPE_PTRMEM_POINTED_TO_TYPE (type))));
4488 else
4490 t1 = intype;
4491 t2 = type;
4493 if (can_convert (t1, t2))
4495 check_for_casting_away_constness (intype, type, "static_cast");
4496 if (TYPE_PTRMEM_P (type))
4498 tree delta;
4500 if (TREE_CODE (expr) == PTRMEM_CST)
4501 expr = cplus_expand_constant (expr);
4502 delta = get_delta_difference (c1, c2, /*force=*/1);
4503 if (!integer_zerop (delta))
4504 expr = cp_build_binary_op (PLUS_EXPR,
4505 build_nop (ptrdiff_type_node, expr),
4506 delta);
4507 return build_nop (type, expr);
4509 else
4510 return build_ptrmemfunc (TYPE_PTRMEMFUNC_FN_TYPE (type), expr,
4511 /*force=*/1);
4515 /* [expr.static.cast]
4517 An rvalue of type "pointer to cv void" can be explicitly
4518 converted to a pointer to object type. A value of type pointer
4519 to object converted to "pointer to cv void" and back to the
4520 original pointer type will have its original value. */
4521 if (TREE_CODE (intype) == POINTER_TYPE
4522 && VOID_TYPE_P (TREE_TYPE (intype))
4523 && TYPE_PTROB_P (type))
4525 check_for_casting_away_constness (intype, type, "static_cast");
4526 return build_nop (type, expr);
4529 error ("invalid static_cast from type `%T' to type `%T'", intype, type);
4530 return error_mark_node;
4533 tree
4534 build_reinterpret_cast (tree type, tree expr)
4536 tree intype;
4538 if (type == error_mark_node || expr == error_mark_node)
4539 return error_mark_node;
4541 if (processing_template_decl)
4543 tree t = build_min (REINTERPRET_CAST_EXPR, type, expr);
4544 return t;
4547 if (TREE_CODE (type) != REFERENCE_TYPE)
4549 expr = decay_conversion (expr);
4551 /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
4552 Strip such NOP_EXPRs if VALUE is being used in non-lvalue context. */
4553 if (TREE_CODE (expr) == NOP_EXPR
4554 && TREE_TYPE (expr) == TREE_TYPE (TREE_OPERAND (expr, 0)))
4555 expr = TREE_OPERAND (expr, 0);
4558 intype = TREE_TYPE (expr);
4560 if (TREE_CODE (type) == REFERENCE_TYPE)
4562 if (! real_lvalue_p (expr))
4564 error ("invalid reinterpret_cast of an rvalue expression of type `%T' to type `%T'", intype, type);
4565 return error_mark_node;
4567 expr = build_unary_op (ADDR_EXPR, expr, 0);
4568 if (expr != error_mark_node)
4569 expr = build_reinterpret_cast
4570 (build_pointer_type (TREE_TYPE (type)), expr);
4571 if (expr != error_mark_node)
4572 expr = build_indirect_ref (expr, 0);
4573 return expr;
4575 else if (same_type_ignoring_top_level_qualifiers_p (intype, type))
4576 return build_static_cast (type, expr);
4578 if (TYPE_PTR_P (type) && (TREE_CODE (intype) == INTEGER_TYPE
4579 || TREE_CODE (intype) == ENUMERAL_TYPE))
4580 /* OK */;
4581 else if (TREE_CODE (type) == INTEGER_TYPE && TYPE_PTR_P (intype))
4583 if (TYPE_PRECISION (type) < TYPE_PRECISION (intype))
4584 pedwarn ("reinterpret_cast from `%T' to `%T' loses precision",
4585 intype, type);
4587 else if ((TYPE_PTRFN_P (type) && TYPE_PTRFN_P (intype))
4588 || (TYPE_PTRMEMFUNC_P (type) && TYPE_PTRMEMFUNC_P (intype)))
4590 expr = decl_constant_value (expr);
4591 return fold (build1 (NOP_EXPR, type, expr));
4593 else if ((TYPE_PTRMEM_P (type) && TYPE_PTRMEM_P (intype))
4594 || (TYPE_PTROBV_P (type) && TYPE_PTROBV_P (intype)))
4596 check_for_casting_away_constness (intype, type, "reinterpret_cast");
4597 expr = decl_constant_value (expr);
4598 return fold (build1 (NOP_EXPR, type, expr));
4600 else if ((TYPE_PTRFN_P (type) && TYPE_PTROBV_P (intype))
4601 || (TYPE_PTRFN_P (intype) && TYPE_PTROBV_P (type)))
4603 pedwarn ("ISO C++ forbids casting between pointer-to-function and pointer-to-object");
4604 expr = decl_constant_value (expr);
4605 return fold (build1 (NOP_EXPR, type, expr));
4607 else
4609 error ("invalid reinterpret_cast from type `%T' to type `%T'",
4610 intype, type);
4611 return error_mark_node;
4614 return cp_convert (type, expr);
4617 tree
4618 build_const_cast (tree type, tree expr)
4620 tree intype;
4622 if (type == error_mark_node || expr == error_mark_node)
4623 return error_mark_node;
4625 if (processing_template_decl)
4627 tree t = build_min (CONST_CAST_EXPR, type, expr);
4628 return t;
4631 if (!POINTER_TYPE_P (type))
4632 error ("invalid use of const_cast with type `%T', which is not a pointer, reference, nor a pointer-to-data-member type", type);
4633 else if (TREE_CODE (TREE_TYPE (type)) == FUNCTION_TYPE)
4635 error ("invalid use of const_cast with type `%T', which is a pointer or reference to a function type", type);
4636 return error_mark_node;
4639 if (TREE_CODE (type) != REFERENCE_TYPE)
4641 expr = decay_conversion (expr);
4643 /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
4644 Strip such NOP_EXPRs if VALUE is being used in non-lvalue context. */
4645 if (TREE_CODE (expr) == NOP_EXPR
4646 && TREE_TYPE (expr) == TREE_TYPE (TREE_OPERAND (expr, 0)))
4647 expr = TREE_OPERAND (expr, 0);
4650 intype = TREE_TYPE (expr);
4652 if (same_type_ignoring_top_level_qualifiers_p (intype, type))
4653 return build_static_cast (type, expr);
4654 else if (TREE_CODE (type) == REFERENCE_TYPE)
4656 if (! real_lvalue_p (expr))
4658 error ("invalid const_cast of an rvalue of type `%T' to type `%T'", intype, type);
4659 return error_mark_node;
4662 if (comp_ptr_ttypes_const (TREE_TYPE (type), intype))
4664 expr = build_unary_op (ADDR_EXPR, expr, 0);
4665 expr = build1 (NOP_EXPR, type, expr);
4666 return convert_from_reference (expr);
4669 else if (((TREE_CODE (type) == POINTER_TYPE
4670 && TREE_CODE (intype) == POINTER_TYPE)
4671 || (TYPE_PTRMEM_P (type) && TYPE_PTRMEM_P (intype)))
4672 && comp_ptr_ttypes_const (TREE_TYPE (type), TREE_TYPE (intype)))
4673 return cp_convert (type, expr);
4675 error ("invalid const_cast from type `%T' to type `%T'", intype, type);
4676 return error_mark_node;
4679 /* Build an expression representing a cast to type TYPE of expression EXPR.
4681 ALLOW_NONCONVERTING is true if we should allow non-converting constructors
4682 when doing the cast. */
4684 tree
4685 build_c_cast (tree type, tree expr)
4687 register tree value = expr;
4688 tree otype;
4690 if (type == error_mark_node || expr == error_mark_node)
4691 return error_mark_node;
4693 if (processing_template_decl)
4695 tree t = build_min (CAST_EXPR, type,
4696 tree_cons (NULL_TREE, value, NULL_TREE));
4697 return t;
4700 /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
4701 Strip such NOP_EXPRs if VALUE is being used in non-lvalue context. */
4702 if (TREE_CODE (type) != REFERENCE_TYPE
4703 && TREE_CODE (value) == NOP_EXPR
4704 && TREE_TYPE (value) == TREE_TYPE (TREE_OPERAND (value, 0)))
4705 value = TREE_OPERAND (value, 0);
4707 if (TREE_CODE (type) == ARRAY_TYPE)
4709 /* Allow casting from T1* to T2[] because Cfront allows it.
4710 NIHCL uses it. It is not valid ISO C++ however. */
4711 if (TREE_CODE (TREE_TYPE (expr)) == POINTER_TYPE)
4713 pedwarn ("ISO C++ forbids casting to an array type `%T'", type);
4714 type = build_pointer_type (TREE_TYPE (type));
4716 else
4718 error ("ISO C++ forbids casting to an array type `%T'", type);
4719 return error_mark_node;
4723 if (TREE_CODE (type) == FUNCTION_TYPE
4724 || TREE_CODE (type) == METHOD_TYPE)
4726 error ("invalid cast to function type `%T'", type);
4727 return error_mark_node;
4730 if (TREE_CODE (type) == VOID_TYPE)
4732 /* Conversion to void does not cause any of the normal function to
4733 * pointer, array to pointer and lvalue to rvalue decays. */
4735 value = convert_to_void (value, /*implicit=*/NULL);
4736 return value;
4739 if (!complete_type_or_else (type, NULL_TREE))
4740 return error_mark_node;
4742 /* Convert functions and arrays to pointers and
4743 convert references to their expanded types,
4744 but don't convert any other types. If, however, we are
4745 casting to a class type, there's no reason to do this: the
4746 cast will only succeed if there is a converting constructor,
4747 and the default conversions will be done at that point. In
4748 fact, doing the default conversion here is actually harmful
4749 in cases like this:
4751 typedef int A[2];
4752 struct S { S(const A&); };
4754 since we don't want the array-to-pointer conversion done. */
4755 if (!IS_AGGR_TYPE (type))
4757 if (TREE_CODE (TREE_TYPE (value)) == FUNCTION_TYPE
4758 || (TREE_CODE (TREE_TYPE (value)) == METHOD_TYPE
4759 /* Don't do the default conversion on a ->* expression. */
4760 && ! (TREE_CODE (type) == POINTER_TYPE
4761 && bound_pmf_p (value)))
4762 || TREE_CODE (TREE_TYPE (value)) == ARRAY_TYPE
4763 || TREE_CODE (TREE_TYPE (value)) == REFERENCE_TYPE)
4764 value = decay_conversion (value);
4766 else if (TREE_CODE (TREE_TYPE (value)) == REFERENCE_TYPE)
4767 /* However, even for class types, we still need to strip away
4768 the reference type, since the call to convert_force below
4769 does not expect the input expression to be of reference
4770 type. */
4771 value = convert_from_reference (value);
4773 otype = TREE_TYPE (value);
4775 /* Optionally warn about potentially worrisome casts. */
4777 if (warn_cast_qual
4778 && TREE_CODE (type) == POINTER_TYPE
4779 && TREE_CODE (otype) == POINTER_TYPE
4780 && !at_least_as_qualified_p (TREE_TYPE (type),
4781 TREE_TYPE (otype)))
4782 warning ("cast from `%T' to `%T' discards qualifiers from pointer target type",
4783 otype, type);
4785 if (TREE_CODE (type) == INTEGER_TYPE
4786 && TYPE_PTR_P (otype)
4787 && TYPE_PRECISION (type) != TYPE_PRECISION (otype))
4788 warning ("cast from pointer to integer of different size");
4790 if (TYPE_PTR_P (type)
4791 && TREE_CODE (otype) == INTEGER_TYPE
4792 && TYPE_PRECISION (type) != TYPE_PRECISION (otype)
4793 /* Don't warn about converting any constant. */
4794 && !TREE_CONSTANT (value))
4795 warning ("cast to pointer from integer of different size");
4797 if (TREE_CODE (type) == REFERENCE_TYPE)
4798 value = (convert_from_reference
4799 (convert_to_reference (type, value, CONV_C_CAST,
4800 LOOKUP_COMPLAIN, NULL_TREE)));
4801 else
4803 tree ovalue;
4805 value = decl_constant_value (value);
4807 ovalue = value;
4808 value = convert_force (type, value, CONV_C_CAST);
4810 /* Ignore any integer overflow caused by the cast. */
4811 if (TREE_CODE (value) == INTEGER_CST)
4813 TREE_OVERFLOW (value) = TREE_OVERFLOW (ovalue);
4814 TREE_CONSTANT_OVERFLOW (value) = TREE_CONSTANT_OVERFLOW (ovalue);
4818 /* Warn about possible alignment problems. Do this here when we will have
4819 instantiated any necessary template types. */
4820 if (STRICT_ALIGNMENT && warn_cast_align
4821 && TREE_CODE (type) == POINTER_TYPE
4822 && TREE_CODE (otype) == POINTER_TYPE
4823 && TREE_CODE (TREE_TYPE (otype)) != VOID_TYPE
4824 && TREE_CODE (TREE_TYPE (otype)) != FUNCTION_TYPE
4825 && COMPLETE_TYPE_P (TREE_TYPE (otype))
4826 && COMPLETE_TYPE_P (TREE_TYPE (type))
4827 && TYPE_ALIGN (TREE_TYPE (type)) > TYPE_ALIGN (TREE_TYPE (otype)))
4828 warning ("cast from `%T' to `%T' increases required alignment of target type",
4829 otype, type);
4831 /* Always produce some operator for an explicit cast,
4832 so we can tell (for -pedantic) that the cast is no lvalue. */
4833 if (TREE_CODE (type) != REFERENCE_TYPE && value == expr
4834 && real_lvalue_p (value))
4835 value = non_lvalue (value);
4837 return value;
4840 /* Build an assignment expression of lvalue LHS from value RHS.
4841 MODIFYCODE is the code for a binary operator that we use
4842 to combine the old value of LHS with RHS to get the new value.
4843 Or else MODIFYCODE is NOP_EXPR meaning do a simple assignment.
4845 C++: If MODIFYCODE is INIT_EXPR, then leave references unbashed. */
4847 tree
4848 build_modify_expr (tree lhs, enum tree_code modifycode, tree rhs)
4850 register tree result;
4851 tree newrhs = rhs;
4852 tree lhstype = TREE_TYPE (lhs);
4853 tree olhstype = lhstype;
4854 tree olhs = lhs;
4856 /* Avoid duplicate error messages from operands that had errors. */
4857 if (lhs == error_mark_node || rhs == error_mark_node)
4858 return error_mark_node;
4860 /* Handle control structure constructs used as "lvalues". */
4861 switch (TREE_CODE (lhs))
4863 /* Handle --foo = 5; as these are valid constructs in C++ */
4864 case PREDECREMENT_EXPR:
4865 case PREINCREMENT_EXPR:
4866 if (TREE_SIDE_EFFECTS (TREE_OPERAND (lhs, 0)))
4867 lhs = build (TREE_CODE (lhs), TREE_TYPE (lhs),
4868 stabilize_reference (TREE_OPERAND (lhs, 0)),
4869 TREE_OPERAND (lhs, 1));
4870 return build (COMPOUND_EXPR, lhstype,
4871 lhs,
4872 build_modify_expr (TREE_OPERAND (lhs, 0),
4873 modifycode, rhs));
4875 /* Handle (a, b) used as an "lvalue". */
4876 case COMPOUND_EXPR:
4877 newrhs = build_modify_expr (TREE_OPERAND (lhs, 1),
4878 modifycode, rhs);
4879 if (newrhs == error_mark_node)
4880 return error_mark_node;
4881 return build (COMPOUND_EXPR, lhstype,
4882 TREE_OPERAND (lhs, 0), newrhs);
4884 case MODIFY_EXPR:
4885 if (TREE_SIDE_EFFECTS (TREE_OPERAND (lhs, 0)))
4886 lhs = build (TREE_CODE (lhs), TREE_TYPE (lhs),
4887 stabilize_reference (TREE_OPERAND (lhs, 0)),
4888 TREE_OPERAND (lhs, 1));
4889 newrhs = build_modify_expr (TREE_OPERAND (lhs, 0), modifycode, rhs);
4890 if (newrhs == error_mark_node)
4891 return error_mark_node;
4892 return build (COMPOUND_EXPR, lhstype, lhs, newrhs);
4894 /* Handle (a ? b : c) used as an "lvalue". */
4895 case COND_EXPR:
4897 /* Produce (a ? (b = rhs) : (c = rhs))
4898 except that the RHS goes through a save-expr
4899 so the code to compute it is only emitted once. */
4900 tree cond;
4901 tree preeval = NULL_TREE;
4903 rhs = stabilize_expr (rhs, &preeval);
4905 /* Check this here to avoid odd errors when trying to convert
4906 a throw to the type of the COND_EXPR. */
4907 if (!lvalue_or_else (lhs, "assignment"))
4908 return error_mark_node;
4910 cond = build_conditional_expr
4911 (TREE_OPERAND (lhs, 0),
4912 build_modify_expr (cp_convert (TREE_TYPE (lhs),
4913 TREE_OPERAND (lhs, 1)),
4914 modifycode, rhs),
4915 build_modify_expr (cp_convert (TREE_TYPE (lhs),
4916 TREE_OPERAND (lhs, 2)),
4917 modifycode, rhs));
4919 if (cond == error_mark_node)
4920 return cond;
4921 /* Make sure the code to compute the rhs comes out
4922 before the split. */
4923 return build (COMPOUND_EXPR, TREE_TYPE (lhs), preeval, cond);
4926 default:
4927 break;
4930 if (modifycode == INIT_EXPR)
4932 if (TREE_CODE (rhs) == CONSTRUCTOR)
4934 my_friendly_assert (same_type_p (TREE_TYPE (rhs), lhstype),
4935 20011220);
4936 result = build (INIT_EXPR, lhstype, lhs, rhs);
4937 TREE_SIDE_EFFECTS (result) = 1;
4938 return result;
4940 else if (! IS_AGGR_TYPE (lhstype))
4941 /* Do the default thing */;
4942 else
4944 result = build_special_member_call (lhs, complete_ctor_identifier,
4945 build_tree_list (NULL_TREE, rhs),
4946 TYPE_BINFO (lhstype),
4947 LOOKUP_NORMAL);
4948 if (result == NULL_TREE)
4949 return error_mark_node;
4950 return result;
4953 else
4955 if (TREE_CODE (lhstype) == REFERENCE_TYPE)
4957 lhs = convert_from_reference (lhs);
4958 olhstype = lhstype = TREE_TYPE (lhs);
4960 lhs = require_complete_type (lhs);
4961 if (lhs == error_mark_node)
4962 return error_mark_node;
4964 if (modifycode == NOP_EXPR)
4966 /* `operator=' is not an inheritable operator. */
4967 if (! IS_AGGR_TYPE (lhstype))
4968 /* Do the default thing */;
4969 else
4971 result = build_new_op (MODIFY_EXPR, LOOKUP_NORMAL,
4972 lhs, rhs, make_node (NOP_EXPR));
4973 if (result == NULL_TREE)
4974 return error_mark_node;
4975 return result;
4977 lhstype = olhstype;
4979 else
4981 /* A binary op has been requested. Combine the old LHS
4982 value with the RHS producing the value we should actually
4983 store into the LHS. */
4985 my_friendly_assert (!PROMOTES_TO_AGGR_TYPE (lhstype, REFERENCE_TYPE),
4986 978652);
4987 lhs = stabilize_reference (lhs);
4988 newrhs = cp_build_binary_op (modifycode, lhs, rhs);
4989 if (newrhs == error_mark_node)
4991 error (" in evaluation of `%Q(%#T, %#T)'", modifycode,
4992 TREE_TYPE (lhs), TREE_TYPE (rhs));
4993 return error_mark_node;
4996 /* Now it looks like a plain assignment. */
4997 modifycode = NOP_EXPR;
4999 my_friendly_assert (TREE_CODE (lhstype) != REFERENCE_TYPE, 20011220);
5000 my_friendly_assert (TREE_CODE (TREE_TYPE (newrhs)) != REFERENCE_TYPE,
5001 20011220);
5004 /* Handle a cast used as an "lvalue".
5005 We have already performed any binary operator using the value as cast.
5006 Now convert the result to the cast type of the lhs,
5007 and then true type of the lhs and store it there;
5008 then convert result back to the cast type to be the value
5009 of the assignment. */
5011 switch (TREE_CODE (lhs))
5013 case NOP_EXPR:
5014 case CONVERT_EXPR:
5015 case FLOAT_EXPR:
5016 case FIX_TRUNC_EXPR:
5017 case FIX_FLOOR_EXPR:
5018 case FIX_ROUND_EXPR:
5019 case FIX_CEIL_EXPR:
5021 tree inner_lhs = TREE_OPERAND (lhs, 0);
5022 tree result;
5024 if (TREE_CODE (TREE_TYPE (newrhs)) == ARRAY_TYPE
5025 || TREE_CODE (TREE_TYPE (newrhs)) == FUNCTION_TYPE
5026 || TREE_CODE (TREE_TYPE (newrhs)) == METHOD_TYPE
5027 || TREE_CODE (TREE_TYPE (newrhs)) == OFFSET_TYPE)
5028 newrhs = decay_conversion (newrhs);
5030 /* ISO C++ 5.4/1: The result is an lvalue if T is a reference
5031 type, otherwise the result is an rvalue. */
5032 if (! lvalue_p (lhs))
5033 pedwarn ("ISO C++ forbids cast to non-reference type used as lvalue");
5035 result = build_modify_expr (inner_lhs, NOP_EXPR,
5036 cp_convert (TREE_TYPE (inner_lhs),
5037 cp_convert (lhstype, newrhs)));
5038 if (result == error_mark_node)
5039 return result;
5040 return cp_convert (TREE_TYPE (lhs), result);
5043 default:
5044 break;
5047 /* Now we have handled acceptable kinds of LHS that are not truly lvalues.
5048 Reject anything strange now. */
5050 if (!lvalue_or_else (lhs, "assignment"))
5051 return error_mark_node;
5053 /* Warn about modifying something that is `const'. Don't warn if
5054 this is initialization. */
5055 if (modifycode != INIT_EXPR
5056 && (TREE_READONLY (lhs) || CP_TYPE_CONST_P (lhstype)
5057 /* Functions are not modifiable, even though they are
5058 lvalues. */
5059 || TREE_CODE (TREE_TYPE (lhs)) == FUNCTION_TYPE
5060 || TREE_CODE (TREE_TYPE (lhs)) == METHOD_TYPE
5061 /* If it's an aggregate and any field is const, then it is
5062 effectively const. */
5063 || (CLASS_TYPE_P (lhstype)
5064 && C_TYPE_FIELDS_READONLY (lhstype))))
5065 readonly_error (lhs, "assignment", 0);
5067 /* If storing into a structure or union member, it has probably been
5068 given type `int'. Compute the type that would go with the actual
5069 amount of storage the member occupies. */
5071 if (TREE_CODE (lhs) == COMPONENT_REF
5072 && (TREE_CODE (lhstype) == INTEGER_TYPE
5073 || TREE_CODE (lhstype) == REAL_TYPE
5074 || TREE_CODE (lhstype) == ENUMERAL_TYPE))
5076 lhstype = TREE_TYPE (get_unwidened (lhs, 0));
5078 /* If storing in a field that is in actuality a short or narrower
5079 than one, we must store in the field in its actual type. */
5081 if (lhstype != TREE_TYPE (lhs))
5083 lhs = copy_node (lhs);
5084 TREE_TYPE (lhs) = lhstype;
5088 /* Convert new value to destination type. */
5090 if (TREE_CODE (lhstype) == ARRAY_TYPE)
5092 int from_array;
5094 if (!same_or_base_type_p (TYPE_MAIN_VARIANT (lhstype),
5095 TYPE_MAIN_VARIANT (TREE_TYPE (rhs))))
5097 error ("incompatible types in assignment of `%T' to `%T'",
5098 TREE_TYPE (rhs), lhstype);
5099 return error_mark_node;
5102 /* Allow array assignment in compiler-generated code. */
5103 if (! DECL_ARTIFICIAL (current_function_decl))
5104 pedwarn ("ISO C++ forbids assignment of arrays");
5106 from_array = TREE_CODE (TREE_TYPE (newrhs)) == ARRAY_TYPE
5107 ? 1 + (modifycode != INIT_EXPR): 0;
5108 return build_vec_init (lhs, NULL_TREE, newrhs, from_array);
5111 if (modifycode == INIT_EXPR)
5112 newrhs = convert_for_initialization (lhs, lhstype, newrhs, LOOKUP_NORMAL,
5113 "initialization", NULL_TREE, 0);
5114 else
5116 /* Avoid warnings on enum bit fields. */
5117 if (TREE_CODE (olhstype) == ENUMERAL_TYPE
5118 && TREE_CODE (lhstype) == INTEGER_TYPE)
5120 newrhs = convert_for_assignment (olhstype, newrhs, "assignment",
5121 NULL_TREE, 0);
5122 newrhs = convert_force (lhstype, newrhs, 0);
5124 else
5125 newrhs = convert_for_assignment (lhstype, newrhs, "assignment",
5126 NULL_TREE, 0);
5127 if (TREE_CODE (newrhs) == CALL_EXPR
5128 && TYPE_NEEDS_CONSTRUCTING (lhstype))
5129 newrhs = build_cplus_new (lhstype, newrhs);
5131 /* Can't initialize directly from a TARGET_EXPR, since that would
5132 cause the lhs to be constructed twice, and possibly result in
5133 accidental self-initialization. So we force the TARGET_EXPR to be
5134 expanded without a target. */
5135 if (TREE_CODE (newrhs) == TARGET_EXPR)
5136 newrhs = build (COMPOUND_EXPR, TREE_TYPE (newrhs), newrhs,
5137 TREE_OPERAND (newrhs, 0));
5140 if (newrhs == error_mark_node)
5141 return error_mark_node;
5143 result = build (modifycode == NOP_EXPR ? MODIFY_EXPR : INIT_EXPR,
5144 lhstype, lhs, newrhs);
5146 TREE_SIDE_EFFECTS (result) = 1;
5148 /* If we got the LHS in a different type for storing in,
5149 convert the result back to the nominal type of LHS
5150 so that the value we return always has the same type
5151 as the LHS argument. */
5153 if (olhstype == TREE_TYPE (result))
5154 return result;
5155 /* Avoid warnings converting integral types back into enums
5156 for enum bit fields. */
5157 if (TREE_CODE (TREE_TYPE (result)) == INTEGER_TYPE
5158 && TREE_CODE (olhstype) == ENUMERAL_TYPE)
5160 result = build (COMPOUND_EXPR, olhstype, result, olhs);
5161 TREE_NO_UNUSED_WARNING (result) = 1;
5162 return result;
5164 return convert_for_assignment (olhstype, result, "assignment",
5165 NULL_TREE, 0);
5168 tree
5169 build_x_modify_expr (tree lhs, enum tree_code modifycode, tree rhs)
5171 if (processing_template_decl)
5172 return build_min_nt (MODOP_EXPR, lhs,
5173 build_min_nt (modifycode, NULL_TREE, NULL_TREE), rhs);
5175 if (modifycode != NOP_EXPR)
5177 tree rval = build_new_op (MODIFY_EXPR, LOOKUP_NORMAL, lhs, rhs,
5178 make_node (modifycode));
5179 if (rval)
5180 return rval;
5182 return build_modify_expr (lhs, modifycode, rhs);
5186 /* Get difference in deltas for different pointer to member function
5187 types. Returns an integer constant of type PTRDIFF_TYPE_NODE. If
5188 the conversion is invalid, the constant is zero. If FORCE is true,
5189 then allow reverse conversions as well.
5191 Note that the naming of FROM and TO is kind of backwards; the return
5192 value is what we add to a TO in order to get a FROM. They are named
5193 this way because we call this function to find out how to convert from
5194 a pointer to member of FROM to a pointer to member of TO. */
5196 static tree
5197 get_delta_difference (tree from, tree to, int force)
5199 tree binfo;
5200 tree virt_binfo;
5201 base_kind kind;
5203 binfo = lookup_base (to, from, ba_check, &kind);
5204 if (kind == bk_inaccessible || kind == bk_ambig)
5206 error (" in pointer to member function conversion");
5207 goto error;
5209 if (!binfo)
5211 if (!force)
5213 error_not_base_type (from, to);
5214 error (" in pointer to member conversion");
5215 goto error;
5217 binfo = lookup_base (from, to, ba_check, &kind);
5218 if (!binfo)
5219 goto error;
5220 virt_binfo = binfo_from_vbase (binfo);
5221 if (virt_binfo)
5223 /* This is a reinterpret cast, we choose to do nothing. */
5224 warning ("pointer to member cast via virtual base `%T'",
5225 BINFO_TYPE (virt_binfo));
5226 goto error;
5228 return convert_to_integer (ptrdiff_type_node,
5229 size_diffop (size_zero_node,
5230 BINFO_OFFSET (binfo)));
5233 virt_binfo = binfo_from_vbase (binfo);
5234 if (!virt_binfo)
5235 return convert_to_integer (ptrdiff_type_node, BINFO_OFFSET (binfo));
5237 /* This is a reinterpret cast, we choose to do nothing. */
5238 if (force)
5239 warning ("pointer to member cast via virtual base `%T'",
5240 BINFO_TYPE (virt_binfo));
5241 else
5242 error ("pointer to member conversion via virtual base `%T'",
5243 BINFO_TYPE (virt_binfo));
5245 error:
5246 return convert_to_integer(ptrdiff_type_node, integer_zero_node);
5249 /* Return a constructor for the pointer-to-member-function TYPE using
5250 the other components as specified. */
5252 tree
5253 build_ptrmemfunc1 (tree type, tree delta, tree pfn)
5255 tree u = NULL_TREE;
5256 tree delta_field;
5257 tree pfn_field;
5259 /* Pull the FIELD_DECLs out of the type. */
5260 pfn_field = TYPE_FIELDS (type);
5261 delta_field = TREE_CHAIN (pfn_field);
5263 /* Make sure DELTA has the type we want. */
5264 delta = convert_and_check (delta_type_node, delta);
5266 /* Finish creating the initializer. */
5267 u = tree_cons (pfn_field, pfn,
5268 build_tree_list (delta_field, delta));
5269 u = build_constructor (type, u);
5270 TREE_CONSTANT (u) = TREE_CONSTANT (pfn) && TREE_CONSTANT (delta);
5271 TREE_STATIC (u) = (TREE_CONSTANT (u)
5272 && (initializer_constant_valid_p (pfn, TREE_TYPE (pfn))
5273 != NULL_TREE)
5274 && (initializer_constant_valid_p (delta, TREE_TYPE (delta))
5275 != NULL_TREE));
5276 return u;
5279 /* Build a constructor for a pointer to member function. It can be
5280 used to initialize global variables, local variable, or used
5281 as a value in expressions. TYPE is the POINTER to METHOD_TYPE we
5282 want to be.
5284 If FORCE is nonzero, then force this conversion, even if
5285 we would rather not do it. Usually set when using an explicit
5286 cast.
5288 Return error_mark_node, if something goes wrong. */
5290 tree
5291 build_ptrmemfunc (tree type, tree pfn, int force)
5293 tree fn;
5294 tree pfn_type;
5295 tree to_type;
5297 if (error_operand_p (pfn))
5298 return error_mark_node;
5300 pfn_type = TREE_TYPE (pfn);
5301 to_type = build_ptrmemfunc_type (type);
5303 /* Handle multiple conversions of pointer to member functions. */
5304 if (TYPE_PTRMEMFUNC_P (pfn_type))
5306 tree delta = NULL_TREE;
5307 tree npfn = NULL_TREE;
5308 tree n;
5310 if (!force
5311 && !can_convert_arg (to_type, TREE_TYPE (pfn), pfn))
5312 error ("invalid conversion to type `%T' from type `%T'",
5313 to_type, pfn_type);
5315 n = get_delta_difference (TYPE_PTRMEMFUNC_OBJECT_TYPE (pfn_type),
5316 TYPE_PTRMEMFUNC_OBJECT_TYPE (to_type),
5317 force);
5319 /* We don't have to do any conversion to convert a
5320 pointer-to-member to its own type. But, we don't want to
5321 just return a PTRMEM_CST if there's an explicit cast; that
5322 cast should make the expression an invalid template argument. */
5323 if (TREE_CODE (pfn) != PTRMEM_CST)
5325 if (same_type_p (to_type, pfn_type))
5326 return pfn;
5327 else if (integer_zerop (n))
5328 return build_reinterpret_cast (to_type, pfn);
5331 if (TREE_SIDE_EFFECTS (pfn))
5332 pfn = save_expr (pfn);
5334 /* Obtain the function pointer and the current DELTA. */
5335 if (TREE_CODE (pfn) == PTRMEM_CST)
5336 expand_ptrmemfunc_cst (pfn, &delta, &npfn);
5337 else
5339 npfn = build_ptrmemfunc_access_expr (pfn, pfn_identifier);
5340 delta = build_ptrmemfunc_access_expr (pfn, delta_identifier);
5343 /* Just adjust the DELTA field. */
5344 my_friendly_assert (TREE_TYPE (delta) == ptrdiff_type_node, 20030727);
5345 if (TARGET_PTRMEMFUNC_VBIT_LOCATION == ptrmemfunc_vbit_in_delta)
5346 n = cp_build_binary_op (LSHIFT_EXPR, n, integer_one_node);
5347 delta = cp_build_binary_op (PLUS_EXPR, delta, n);
5348 return build_ptrmemfunc1 (to_type, delta, npfn);
5351 /* Handle null pointer to member function conversions. */
5352 if (integer_zerop (pfn))
5354 pfn = build_c_cast (type, integer_zero_node);
5355 return build_ptrmemfunc1 (to_type,
5356 integer_zero_node,
5357 pfn);
5360 if (type_unknown_p (pfn))
5361 return instantiate_type (type, pfn, tf_error | tf_warning);
5363 fn = TREE_OPERAND (pfn, 0);
5364 my_friendly_assert (TREE_CODE (fn) == FUNCTION_DECL, 0);
5365 return make_ptrmem_cst (to_type, fn);
5368 /* Return the DELTA, IDX, PFN, and DELTA2 values for the PTRMEM_CST
5369 given by CST.
5371 ??? There is no consistency as to the types returned for the above
5372 values. Some code acts as if its a sizetype and some as if its
5373 integer_type_node. */
5375 void
5376 expand_ptrmemfunc_cst (tree cst, tree *delta, tree *pfn)
5378 tree type = TREE_TYPE (cst);
5379 tree fn = PTRMEM_CST_MEMBER (cst);
5380 tree ptr_class, fn_class;
5382 my_friendly_assert (TREE_CODE (fn) == FUNCTION_DECL, 0);
5384 /* The class that the function belongs to. */
5385 fn_class = DECL_CONTEXT (fn);
5387 /* The class that we're creating a pointer to member of. */
5388 ptr_class = TYPE_PTRMEMFUNC_OBJECT_TYPE (type);
5390 /* First, calculate the adjustment to the function's class. */
5391 *delta = get_delta_difference (fn_class, ptr_class, /*force=*/0);
5393 if (!DECL_VIRTUAL_P (fn))
5394 *pfn = convert (TYPE_PTRMEMFUNC_FN_TYPE (type), build_addr_func (fn));
5395 else
5397 /* If we're dealing with a virtual function, we have to adjust 'this'
5398 again, to point to the base which provides the vtable entry for
5399 fn; the call will do the opposite adjustment. */
5400 tree orig_class = DECL_CONTEXT (fn);
5401 tree binfo = binfo_or_else (orig_class, fn_class);
5402 *delta = fold (build (PLUS_EXPR, TREE_TYPE (*delta),
5403 *delta, BINFO_OFFSET (binfo)));
5405 /* We set PFN to the vtable offset at which the function can be
5406 found, plus one (unless ptrmemfunc_vbit_in_delta, in which
5407 case delta is shifted left, and then incremented). */
5408 *pfn = DECL_VINDEX (fn);
5409 *pfn = fold (build (MULT_EXPR, integer_type_node, *pfn,
5410 TYPE_SIZE_UNIT (vtable_entry_type)));
5412 switch (TARGET_PTRMEMFUNC_VBIT_LOCATION)
5414 case ptrmemfunc_vbit_in_pfn:
5415 *pfn = fold (build (PLUS_EXPR, integer_type_node, *pfn,
5416 integer_one_node));
5417 break;
5419 case ptrmemfunc_vbit_in_delta:
5420 *delta = fold (build (LSHIFT_EXPR, TREE_TYPE (*delta),
5421 *delta, integer_one_node));
5422 *delta = fold (build (PLUS_EXPR, TREE_TYPE (*delta),
5423 *delta, integer_one_node));
5424 break;
5426 default:
5427 abort ();
5430 *pfn = fold (build1 (NOP_EXPR, TYPE_PTRMEMFUNC_FN_TYPE (type),
5431 *pfn));
5435 /* Return an expression for PFN from the pointer-to-member function
5436 given by T. */
5438 tree
5439 pfn_from_ptrmemfunc (tree t)
5441 if (TREE_CODE (t) == PTRMEM_CST)
5443 tree delta;
5444 tree pfn;
5446 expand_ptrmemfunc_cst (t, &delta, &pfn);
5447 if (pfn)
5448 return pfn;
5451 return build_ptrmemfunc_access_expr (t, pfn_identifier);
5454 /* Expression EXPR is about to be implicitly converted to TYPE. Warn
5455 if this is a potentially dangerous thing to do. Returns a possibly
5456 marked EXPR. */
5458 tree
5459 dubious_conversion_warnings (tree type, tree expr,
5460 const char *errtype, tree fndecl, int parmnum)
5462 type = non_reference (type);
5464 /* Issue warnings about peculiar, but valid, uses of NULL. */
5465 if (ARITHMETIC_TYPE_P (type) && expr == null_node)
5467 if (fndecl)
5468 warning ("passing NULL used for non-pointer %s %P of `%D'",
5469 errtype, parmnum, fndecl);
5470 else
5471 warning ("%s to non-pointer type `%T' from NULL", errtype, type);
5474 /* Warn about assigning a floating-point type to an integer type. */
5475 if (TREE_CODE (TREE_TYPE (expr)) == REAL_TYPE
5476 && TREE_CODE (type) == INTEGER_TYPE)
5478 if (fndecl)
5479 warning ("passing `%T' for %s %P of `%D'",
5480 TREE_TYPE (expr), errtype, parmnum, fndecl);
5481 else
5482 warning ("%s to `%T' from `%T'", errtype, type, TREE_TYPE (expr));
5484 /* And warn about assigning a negative value to an unsigned
5485 variable. */
5486 else if (TREE_UNSIGNED (type) && TREE_CODE (type) != BOOLEAN_TYPE)
5488 if (TREE_CODE (expr) == INTEGER_CST
5489 && TREE_NEGATED_INT (expr))
5491 if (fndecl)
5492 warning ("passing negative value `%E' for %s %P of `%D'",
5493 expr, errtype, parmnum, fndecl);
5494 else
5495 warning ("%s of negative value `%E' to `%T'",
5496 errtype, expr, type);
5499 overflow_warning (expr);
5501 if (TREE_CONSTANT (expr))
5502 expr = fold (expr);
5504 return expr;
5507 /* Convert value RHS to type TYPE as preparation for an assignment to
5508 an lvalue of type TYPE. ERRTYPE is a string to use in error
5509 messages: "assignment", "return", etc. If FNDECL is non-NULL, we
5510 are doing the conversion in order to pass the PARMNUMth argument of
5511 FNDECL. */
5513 static tree
5514 convert_for_assignment (tree type, tree rhs,
5515 const char *errtype, tree fndecl, int parmnum)
5517 register tree rhstype;
5518 register enum tree_code coder;
5520 /* Strip NON_LVALUE_EXPRs since we aren't using as an lvalue. */
5521 if (TREE_CODE (rhs) == NON_LVALUE_EXPR)
5522 rhs = TREE_OPERAND (rhs, 0);
5524 rhstype = TREE_TYPE (rhs);
5525 coder = TREE_CODE (rhstype);
5527 if (TREE_CODE (type) == VECTOR_TYPE && coder == VECTOR_TYPE
5528 && ((*targetm.vector_opaque_p) (type)
5529 || (*targetm.vector_opaque_p) (rhstype)))
5530 return convert (type, rhs);
5532 if (rhs == error_mark_node || rhstype == error_mark_node)
5533 return error_mark_node;
5534 if (TREE_CODE (rhs) == TREE_LIST && TREE_VALUE (rhs) == error_mark_node)
5535 return error_mark_node;
5537 rhs = dubious_conversion_warnings (type, rhs, errtype, fndecl, parmnum);
5539 /* The RHS of an assignment cannot have void type. */
5540 if (coder == VOID_TYPE)
5542 error ("void value not ignored as it ought to be");
5543 return error_mark_node;
5546 /* Simplify the RHS if possible. */
5547 if (TREE_CODE (rhs) == CONST_DECL)
5548 rhs = DECL_INITIAL (rhs);
5550 /* We do not use decl_constant_value here because of this case:
5552 const char* const s = "s";
5554 The conversion rules for a string literal are more lax than for a
5555 variable; in particular, a string literal can be converted to a
5556 "char *" but the variable "s" cannot be converted in the same
5557 way. If the conversion is allowed, the optimization should be
5558 performed while creating the converted expression. */
5560 /* [expr.ass]
5562 The expression is implicitly converted (clause _conv_) to the
5563 cv-unqualified type of the left operand.
5565 We allow bad conversions here because by the time we get to this point
5566 we are committed to doing the conversion. If we end up doing a bad
5567 conversion, convert_like will complain. */
5568 if (!can_convert_arg_bad (type, rhstype, rhs))
5570 /* When -Wno-pmf-conversions is use, we just silently allow
5571 conversions from pointers-to-members to plain pointers. If
5572 the conversion doesn't work, cp_convert will complain. */
5573 if (!warn_pmf2ptr
5574 && TYPE_PTR_P (type)
5575 && TYPE_PTRMEMFUNC_P (rhstype))
5576 rhs = cp_convert (strip_top_quals (type), rhs);
5577 else
5579 /* If the right-hand side has unknown type, then it is an
5580 overloaded function. Call instantiate_type to get error
5581 messages. */
5582 if (rhstype == unknown_type_node)
5583 instantiate_type (type, rhs, tf_error | tf_warning);
5584 else if (fndecl)
5585 error ("cannot convert `%T' to `%T' for argument `%P' to `%D'",
5586 rhstype, type, parmnum, fndecl);
5587 else
5588 error ("cannot convert `%T' to `%T' in %s", rhstype, type,
5589 errtype);
5590 return error_mark_node;
5593 return perform_implicit_conversion (strip_top_quals (type), rhs);
5596 /* Convert RHS to be of type TYPE.
5597 If EXP is nonzero, it is the target of the initialization.
5598 ERRTYPE is a string to use in error messages.
5600 Two major differences between the behavior of
5601 `convert_for_assignment' and `convert_for_initialization'
5602 are that references are bashed in the former, while
5603 copied in the latter, and aggregates are assigned in
5604 the former (operator=) while initialized in the
5605 latter (X(X&)).
5607 If using constructor make sure no conversion operator exists, if one does
5608 exist, an ambiguity exists.
5610 If flags doesn't include LOOKUP_COMPLAIN, don't complain about anything. */
5612 tree
5613 convert_for_initialization (tree exp, tree type, tree rhs, int flags,
5614 const char *errtype, tree fndecl, int parmnum)
5616 register enum tree_code codel = TREE_CODE (type);
5617 register tree rhstype;
5618 register enum tree_code coder;
5620 /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
5621 Strip such NOP_EXPRs, since RHS is used in non-lvalue context. */
5622 if (TREE_CODE (rhs) == NOP_EXPR
5623 && TREE_TYPE (rhs) == TREE_TYPE (TREE_OPERAND (rhs, 0))
5624 && codel != REFERENCE_TYPE)
5625 rhs = TREE_OPERAND (rhs, 0);
5627 if (rhs == error_mark_node
5628 || (TREE_CODE (rhs) == TREE_LIST && TREE_VALUE (rhs) == error_mark_node))
5629 return error_mark_node;
5631 if (TREE_CODE (TREE_TYPE (rhs)) == REFERENCE_TYPE)
5632 rhs = convert_from_reference (rhs);
5634 if ((TREE_CODE (TREE_TYPE (rhs)) == ARRAY_TYPE
5635 && TREE_CODE (type) != ARRAY_TYPE
5636 && (TREE_CODE (type) != REFERENCE_TYPE
5637 || TREE_CODE (TREE_TYPE (type)) != ARRAY_TYPE))
5638 || (TREE_CODE (TREE_TYPE (rhs)) == FUNCTION_TYPE
5639 && (TREE_CODE (type) != REFERENCE_TYPE
5640 || TREE_CODE (TREE_TYPE (type)) != FUNCTION_TYPE))
5641 || TREE_CODE (TREE_TYPE (rhs)) == METHOD_TYPE)
5642 rhs = decay_conversion (rhs);
5644 rhstype = TREE_TYPE (rhs);
5645 coder = TREE_CODE (rhstype);
5647 if (coder == ERROR_MARK)
5648 return error_mark_node;
5650 /* We accept references to incomplete types, so we can
5651 return here before checking if RHS is of complete type. */
5653 if (codel == REFERENCE_TYPE)
5655 /* This should eventually happen in convert_arguments. */
5656 int savew = 0, savee = 0;
5658 if (fndecl)
5659 savew = warningcount, savee = errorcount;
5660 rhs = initialize_reference (type, rhs, /*decl=*/NULL_TREE,
5661 /*cleanup=*/NULL);
5662 if (fndecl)
5664 if (warningcount > savew)
5665 cp_warning_at ("in passing argument %P of `%+D'", parmnum, fndecl);
5666 else if (errorcount > savee)
5667 cp_error_at ("in passing argument %P of `%+D'", parmnum, fndecl);
5669 return rhs;
5672 if (exp != 0)
5673 exp = require_complete_type (exp);
5674 if (exp == error_mark_node)
5675 return error_mark_node;
5677 rhstype = non_reference (rhstype);
5679 type = complete_type (type);
5681 if (IS_AGGR_TYPE (type))
5682 return ocp_convert (type, rhs, CONV_IMPLICIT|CONV_FORCE_TEMP, flags);
5684 return convert_for_assignment (type, rhs, errtype, fndecl, parmnum);
5687 /* Expand an ASM statement with operands, handling output operands
5688 that are not variables or INDIRECT_REFS by transforming such
5689 cases into cases that expand_asm_operands can handle.
5691 Arguments are same as for expand_asm_operands.
5693 We don't do default conversions on all inputs, because it can screw
5694 up operands that are expected to be in memory. */
5696 void
5697 c_expand_asm_operands (tree string, tree outputs, tree inputs, tree clobbers,
5698 int vol, const char *filename, int line)
5700 int noutputs = list_length (outputs);
5701 register int i;
5702 /* o[I] is the place that output number I should be written. */
5703 register tree *o = alloca (noutputs * sizeof (tree));
5704 register tree tail;
5706 /* Record the contents of OUTPUTS before it is modified. */
5707 for (i = 0, tail = outputs; tail; tail = TREE_CHAIN (tail), i++)
5708 o[i] = TREE_VALUE (tail);
5710 /* Generate the ASM_OPERANDS insn;
5711 store into the TREE_VALUEs of OUTPUTS some trees for
5712 where the values were actually stored. */
5713 expand_asm_operands (string, outputs, inputs, clobbers, vol, filename, line);
5715 /* Copy all the intermediate outputs into the specified outputs. */
5716 for (i = 0, tail = outputs; tail; tail = TREE_CHAIN (tail), i++)
5718 if (o[i] != TREE_VALUE (tail))
5720 expand_expr (build_modify_expr (o[i], NOP_EXPR, TREE_VALUE (tail)),
5721 const0_rtx, VOIDmode, EXPAND_NORMAL);
5722 free_temp_slots ();
5724 /* Restore the original value so that it's correct the next
5725 time we expand this function. */
5726 TREE_VALUE (tail) = o[i];
5728 /* Detect modification of read-only values.
5729 (Otherwise done by build_modify_expr.) */
5730 else
5732 tree type = TREE_TYPE (o[i]);
5733 if (type != error_mark_node
5734 && (CP_TYPE_CONST_P (type)
5735 || (CLASS_TYPE_P (type) && C_TYPE_FIELDS_READONLY (type))))
5736 readonly_error (o[i], "modification by `asm'", 1);
5740 /* Those MODIFY_EXPRs could do autoincrements. */
5741 emit_queue ();
5744 /* If RETVAL is the address of, or a reference to, a local variable or
5745 temporary give an appropriate warning. */
5747 static void
5748 maybe_warn_about_returning_address_of_local (tree retval)
5750 tree valtype = TREE_TYPE (DECL_RESULT (current_function_decl));
5751 tree whats_returned = retval;
5753 for (;;)
5755 if (TREE_CODE (whats_returned) == COMPOUND_EXPR)
5756 whats_returned = TREE_OPERAND (whats_returned, 1);
5757 else if (TREE_CODE (whats_returned) == CONVERT_EXPR
5758 || TREE_CODE (whats_returned) == NON_LVALUE_EXPR
5759 || TREE_CODE (whats_returned) == NOP_EXPR)
5760 whats_returned = TREE_OPERAND (whats_returned, 0);
5761 else
5762 break;
5765 if (TREE_CODE (whats_returned) != ADDR_EXPR)
5766 return;
5767 whats_returned = TREE_OPERAND (whats_returned, 0);
5769 if (TREE_CODE (valtype) == REFERENCE_TYPE)
5771 if (TREE_CODE (whats_returned) == AGGR_INIT_EXPR
5772 || TREE_CODE (whats_returned) == TARGET_EXPR)
5774 warning ("returning reference to temporary");
5775 return;
5777 if (TREE_CODE (whats_returned) == VAR_DECL
5778 && DECL_NAME (whats_returned)
5779 && TEMP_NAME_P (DECL_NAME (whats_returned)))
5781 warning ("reference to non-lvalue returned");
5782 return;
5786 if (TREE_CODE (whats_returned) == VAR_DECL
5787 && DECL_NAME (whats_returned)
5788 && DECL_FUNCTION_SCOPE_P (whats_returned)
5789 && !(TREE_STATIC (whats_returned)
5790 || TREE_PUBLIC (whats_returned)))
5792 if (TREE_CODE (valtype) == REFERENCE_TYPE)
5793 cp_warning_at ("reference to local variable `%D' returned",
5794 whats_returned);
5795 else
5796 cp_warning_at ("address of local variable `%D' returned",
5797 whats_returned);
5798 return;
5802 /* Check that returning RETVAL from the current function is valid.
5803 Return an expression explicitly showing all conversions required to
5804 change RETVAL into the function return type, and to assign it to
5805 the DECL_RESULT for the function. */
5807 tree
5808 check_return_expr (tree retval)
5810 tree result;
5811 /* The type actually returned by the function, after any
5812 promotions. */
5813 tree valtype;
5814 int fn_returns_value_p;
5816 /* A `volatile' function is one that isn't supposed to return, ever.
5817 (This is a G++ extension, used to get better code for functions
5818 that call the `volatile' function.) */
5819 if (TREE_THIS_VOLATILE (current_function_decl))
5820 warning ("function declared `noreturn' has a `return' statement");
5822 /* Check for various simple errors. */
5823 if (DECL_DESTRUCTOR_P (current_function_decl))
5825 if (retval)
5826 error ("returning a value from a destructor");
5827 return NULL_TREE;
5829 else if (DECL_CONSTRUCTOR_P (current_function_decl))
5831 if (in_function_try_handler)
5832 /* If a return statement appears in a handler of the
5833 function-try-block of a constructor, the program is ill-formed. */
5834 error ("cannot return from a handler of a function-try-block of a constructor");
5835 else if (retval)
5836 /* You can't return a value from a constructor. */
5837 error ("returning a value from a constructor");
5838 return NULL_TREE;
5841 if (processing_template_decl)
5843 current_function_returns_value = 1;
5844 return retval;
5847 /* When no explicit return-value is given in a function with a named
5848 return value, the named return value is used. */
5849 result = DECL_RESULT (current_function_decl);
5850 valtype = TREE_TYPE (result);
5851 my_friendly_assert (valtype != NULL_TREE, 19990924);
5852 fn_returns_value_p = !VOID_TYPE_P (valtype);
5853 if (!retval && DECL_NAME (result) && fn_returns_value_p)
5854 retval = result;
5856 /* Check for a return statement with no return value in a function
5857 that's supposed to return a value. */
5858 if (!retval && fn_returns_value_p)
5860 pedwarn ("return-statement with no value, in function returning '%T'",
5861 valtype);
5862 /* Clear this, so finish_function won't say that we reach the
5863 end of a non-void function (which we don't, we gave a
5864 return!). */
5865 current_function_returns_null = 0;
5867 /* Check for a return statement with a value in a function that
5868 isn't supposed to return a value. */
5869 else if (retval && !fn_returns_value_p)
5871 if (VOID_TYPE_P (TREE_TYPE (retval)))
5872 /* You can return a `void' value from a function of `void'
5873 type. In that case, we have to evaluate the expression for
5874 its side-effects. */
5875 finish_expr_stmt (retval);
5876 else
5877 pedwarn ("return-statement with a value, in function "
5878 "returning 'void'");
5880 current_function_returns_null = 1;
5882 /* There's really no value to return, after all. */
5883 return NULL_TREE;
5885 else if (!retval)
5886 /* Remember that this function can sometimes return without a
5887 value. */
5888 current_function_returns_null = 1;
5889 else
5890 /* Remember that this function did return a value. */
5891 current_function_returns_value = 1;
5893 /* Only operator new(...) throw(), can return NULL [expr.new/13]. */
5894 if ((DECL_OVERLOADED_OPERATOR_P (current_function_decl) == NEW_EXPR
5895 || DECL_OVERLOADED_OPERATOR_P (current_function_decl) == VEC_NEW_EXPR)
5896 && !TYPE_NOTHROW_P (TREE_TYPE (current_function_decl))
5897 && ! flag_check_new
5898 && null_ptr_cst_p (retval))
5899 warning ("`operator new' must not return NULL unless it is declared `throw()' (or -fcheck-new is in effect)");
5901 /* Effective C++ rule 15. See also start_function. */
5902 if (warn_ecpp
5903 && DECL_NAME (current_function_decl) == ansi_assopname(NOP_EXPR)
5904 && retval != current_class_ref)
5905 warning ("`operator=' should return a reference to `*this'");
5907 /* The fabled Named Return Value optimization, as per [class.copy]/15:
5909 [...] For a function with a class return type, if the expression
5910 in the return statement is the name of a local object, and the cv-
5911 unqualified type of the local object is the same as the function
5912 return type, an implementation is permitted to omit creating the tem-
5913 porary object to hold the function return value [...]
5915 So, if this is a value-returning function that always returns the same
5916 local variable, remember it.
5918 It might be nice to be more flexible, and choose the first suitable
5919 variable even if the function sometimes returns something else, but
5920 then we run the risk of clobbering the variable we chose if the other
5921 returned expression uses the chosen variable somehow. And people expect
5922 this restriction, anyway. (jason 2000-11-19)
5924 See finish_function, genrtl_start_function, and declare_return_variable
5925 for other pieces of this optimization. */
5927 if (fn_returns_value_p && flag_elide_constructors)
5929 if (retval != NULL_TREE
5930 && (current_function_return_value == NULL_TREE
5931 || current_function_return_value == retval)
5932 && TREE_CODE (retval) == VAR_DECL
5933 && DECL_CONTEXT (retval) == current_function_decl
5934 && ! TREE_STATIC (retval)
5935 && (DECL_ALIGN (retval)
5936 >= DECL_ALIGN (DECL_RESULT (current_function_decl)))
5937 && same_type_p ((TYPE_MAIN_VARIANT
5938 (TREE_TYPE (retval))),
5939 (TYPE_MAIN_VARIANT
5940 (TREE_TYPE (TREE_TYPE (current_function_decl))))))
5941 current_function_return_value = retval;
5942 else
5943 current_function_return_value = error_mark_node;
5946 /* We don't need to do any conversions when there's nothing being
5947 returned. */
5948 if (!retval || retval == error_mark_node)
5949 return retval;
5951 /* Do any required conversions. */
5952 if (retval == result || DECL_CONSTRUCTOR_P (current_function_decl))
5953 /* No conversions are required. */
5955 else
5957 /* The type the function is declared to return. */
5958 tree functype = TREE_TYPE (TREE_TYPE (current_function_decl));
5960 /* First convert the value to the function's return type, then
5961 to the type of return value's location to handle the
5962 case that functype is smaller than the valtype. */
5963 retval = convert_for_initialization
5964 (NULL_TREE, functype, retval, LOOKUP_NORMAL|LOOKUP_ONLYCONVERTING,
5965 "return", NULL_TREE, 0);
5966 retval = convert (valtype, retval);
5968 /* If the conversion failed, treat this just like `return;'. */
5969 if (retval == error_mark_node)
5970 return retval;
5971 /* We can't initialize a register from a AGGR_INIT_EXPR. */
5972 else if (! current_function_returns_struct
5973 && TREE_CODE (retval) == TARGET_EXPR
5974 && TREE_CODE (TREE_OPERAND (retval, 1)) == AGGR_INIT_EXPR)
5975 retval = build (COMPOUND_EXPR, TREE_TYPE (retval), retval,
5976 TREE_OPERAND (retval, 0));
5977 else
5978 maybe_warn_about_returning_address_of_local (retval);
5981 /* Actually copy the value returned into the appropriate location. */
5982 if (retval && retval != result)
5983 retval = build (INIT_EXPR, TREE_TYPE (result), result, retval);
5985 return retval;
5989 /* Returns nonzero if the pointer-type FROM can be converted to the
5990 pointer-type TO via a qualification conversion. If CONSTP is -1,
5991 then we return nonzero if the pointers are similar, and the
5992 cv-qualification signature of FROM is a proper subset of that of TO.
5994 If CONSTP is positive, then all outer pointers have been
5995 const-qualified. */
5997 static int
5998 comp_ptr_ttypes_real (tree to, tree from, int constp)
6000 bool to_more_cv_qualified = false;
6002 for (; ; to = TREE_TYPE (to), from = TREE_TYPE (from))
6004 if (TREE_CODE (to) != TREE_CODE (from))
6005 return 0;
6007 if (TREE_CODE (from) == OFFSET_TYPE
6008 && !same_type_p (TYPE_OFFSET_BASETYPE (from),
6009 TYPE_OFFSET_BASETYPE (to)))
6010 return 0;
6012 /* Const and volatile mean something different for function types,
6013 so the usual checks are not appropriate. */
6014 if (TREE_CODE (to) != FUNCTION_TYPE && TREE_CODE (to) != METHOD_TYPE)
6016 if (!at_least_as_qualified_p (to, from))
6017 return 0;
6019 if (!at_least_as_qualified_p (from, to))
6021 if (constp == 0)
6022 return 0;
6023 to_more_cv_qualified = true;
6026 if (constp > 0)
6027 constp &= TYPE_READONLY (to);
6030 if (TREE_CODE (to) != POINTER_TYPE && !TYPE_PTRMEM_P (to))
6031 return ((constp >= 0 || to_more_cv_qualified)
6032 && same_type_ignoring_top_level_qualifiers_p (to, from));
6036 /* When comparing, say, char ** to char const **, this function takes
6037 the 'char *' and 'char const *'. Do not pass non-pointer/reference
6038 types to this function. */
6041 comp_ptr_ttypes (tree to, tree from)
6043 return comp_ptr_ttypes_real (to, from, 1);
6046 /* Returns 1 if to and from are (possibly multi-level) pointers to the same
6047 type or inheritance-related types, regardless of cv-quals. */
6050 ptr_reasonably_similar (tree to, tree from)
6052 for (; ; to = TREE_TYPE (to), from = TREE_TYPE (from))
6054 /* Any target type is similar enough to void. */
6055 if (TREE_CODE (to) == VOID_TYPE
6056 || TREE_CODE (from) == VOID_TYPE)
6057 return 1;
6059 if (TREE_CODE (to) != TREE_CODE (from))
6060 return 0;
6062 if (TREE_CODE (from) == OFFSET_TYPE
6063 && comptypes (TYPE_OFFSET_BASETYPE (to),
6064 TYPE_OFFSET_BASETYPE (from),
6065 COMPARE_BASE | COMPARE_DERIVED))
6066 continue;
6068 if (TREE_CODE (to) == INTEGER_TYPE
6069 && TYPE_PRECISION (to) == TYPE_PRECISION (from))
6070 return 1;
6072 if (TREE_CODE (to) == FUNCTION_TYPE)
6073 return 1;
6075 if (TREE_CODE (to) != POINTER_TYPE)
6076 return comptypes
6077 (TYPE_MAIN_VARIANT (to), TYPE_MAIN_VARIANT (from),
6078 COMPARE_BASE | COMPARE_DERIVED);
6082 /* Like comp_ptr_ttypes, for const_cast. */
6084 static int
6085 comp_ptr_ttypes_const (tree to, tree from)
6087 for (; ; to = TREE_TYPE (to), from = TREE_TYPE (from))
6089 if (TREE_CODE (to) != TREE_CODE (from))
6090 return 0;
6092 if (TREE_CODE (from) == OFFSET_TYPE
6093 && same_type_p (TYPE_OFFSET_BASETYPE (from),
6094 TYPE_OFFSET_BASETYPE (to)))
6095 continue;
6097 if (TREE_CODE (to) != POINTER_TYPE)
6098 return same_type_ignoring_top_level_qualifiers_p (to, from);
6102 /* Returns the type qualifiers for this type, including the qualifiers on the
6103 elements for an array type. */
6106 cp_type_quals (tree type)
6108 type = strip_array_types (type);
6109 if (type == error_mark_node)
6110 return TYPE_UNQUALIFIED;
6111 return TYPE_QUALS (type);
6114 /* Returns nonzero if the TYPE contains a mutable member */
6116 bool
6117 cp_has_mutable_p (tree type)
6119 type = strip_array_types (type);
6121 return CLASS_TYPE_P (type) && CLASSTYPE_HAS_MUTABLE (type);
6124 /* Subroutine of casts_away_constness. Make T1 and T2 point at
6125 exemplar types such that casting T1 to T2 is casting away castness
6126 if and only if there is no implicit conversion from T1 to T2. */
6128 static void
6129 casts_away_constness_r (tree *t1, tree *t2)
6131 int quals1;
6132 int quals2;
6134 /* [expr.const.cast]
6136 For multi-level pointer to members and multi-level mixed pointers
6137 and pointers to members (conv.qual), the "member" aspect of a
6138 pointer to member level is ignored when determining if a const
6139 cv-qualifier has been cast away. */
6140 if (TYPE_PTRMEM_P (*t1))
6141 *t1 = build_pointer_type (TYPE_PTRMEM_POINTED_TO_TYPE (*t1));
6142 if (TYPE_PTRMEM_P (*t2))
6143 *t2 = build_pointer_type (TYPE_PTRMEM_POINTED_TO_TYPE (*t2));
6145 /* [expr.const.cast]
6147 For two pointer types:
6149 X1 is T1cv1,1 * ... cv1,N * where T1 is not a pointer type
6150 X2 is T2cv2,1 * ... cv2,M * where T2 is not a pointer type
6151 K is min(N,M)
6153 casting from X1 to X2 casts away constness if, for a non-pointer
6154 type T there does not exist an implicit conversion (clause
6155 _conv_) from:
6157 Tcv1,(N-K+1) * cv1,(N-K+2) * ... cv1,N *
6161 Tcv2,(M-K+1) * cv2,(M-K+2) * ... cv2,M *. */
6163 if (TREE_CODE (*t1) != POINTER_TYPE
6164 || TREE_CODE (*t2) != POINTER_TYPE)
6166 *t1 = cp_build_qualified_type (void_type_node,
6167 cp_type_quals (*t1));
6168 *t2 = cp_build_qualified_type (void_type_node,
6169 cp_type_quals (*t2));
6170 return;
6173 quals1 = cp_type_quals (*t1);
6174 quals2 = cp_type_quals (*t2);
6175 *t1 = TREE_TYPE (*t1);
6176 *t2 = TREE_TYPE (*t2);
6177 casts_away_constness_r (t1, t2);
6178 *t1 = build_pointer_type (*t1);
6179 *t2 = build_pointer_type (*t2);
6180 *t1 = cp_build_qualified_type (*t1, quals1);
6181 *t2 = cp_build_qualified_type (*t2, quals2);
6184 /* Returns nonzero if casting from TYPE1 to TYPE2 casts away
6185 constness. */
6187 static bool
6188 casts_away_constness (tree t1, tree t2)
6190 if (TREE_CODE (t2) == REFERENCE_TYPE)
6192 /* [expr.const.cast]
6194 Casting from an lvalue of type T1 to an lvalue of type T2
6195 using a reference cast casts away constness if a cast from an
6196 rvalue of type "pointer to T1" to the type "pointer to T2"
6197 casts away constness. */
6198 t1 = (TREE_CODE (t1) == REFERENCE_TYPE ? TREE_TYPE (t1) : t1);
6199 return casts_away_constness (build_pointer_type (t1),
6200 build_pointer_type (TREE_TYPE (t2)));
6203 if (TYPE_PTRMEM_P (t1) && TYPE_PTRMEM_P (t2))
6204 /* [expr.const.cast]
6206 Casting from an rvalue of type "pointer to data member of X
6207 of type T1" to the type "pointer to data member of Y of type
6208 T2" casts away constness if a cast from an rvalue of type
6209 "pointer to T1" to the type "pointer to T2" casts away
6210 constness. */
6211 return casts_away_constness
6212 (build_pointer_type (TYPE_PTRMEM_POINTED_TO_TYPE (t1)),
6213 build_pointer_type (TYPE_PTRMEM_POINTED_TO_TYPE (t2)));
6215 /* Casting away constness is only something that makes sense for
6216 pointer or reference types. */
6217 if (TREE_CODE (t1) != POINTER_TYPE
6218 || TREE_CODE (t2) != POINTER_TYPE)
6219 return false;
6221 /* Top-level qualifiers don't matter. */
6222 t1 = TYPE_MAIN_VARIANT (t1);
6223 t2 = TYPE_MAIN_VARIANT (t2);
6224 casts_away_constness_r (&t1, &t2);
6225 if (!can_convert (t2, t1))
6226 return true;
6228 return false;
6231 /* If T is a REFERENCE_TYPE return the type to which T refers.
6232 Otherwise, return T itself. */
6234 tree
6235 non_reference (tree t)
6237 if (TREE_CODE (t) == REFERENCE_TYPE)
6238 t = TREE_TYPE (t);
6239 return t;