FSF GCC merge 02/23/03
[official-gcc.git] / gcc / cp / typeck.c
blob06a0b420705b5eae348aa0b352f3a60fb72e783f
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"
48 static tree convert_for_assignment PARAMS ((tree, tree, const char *, tree,
49 int));
50 static tree cp_pointer_int_sum PARAMS ((enum tree_code, tree, tree));
51 static tree rationalize_conditional_expr PARAMS ((enum tree_code, tree));
52 static int comp_target_parms PARAMS ((tree, tree));
53 static int comp_ptr_ttypes_real PARAMS ((tree, tree, int));
54 static int comp_ptr_ttypes_const PARAMS ((tree, tree));
55 static int comp_ptr_ttypes_reinterpret PARAMS ((tree, tree));
56 static int comp_except_types PARAMS ((tree, tree, int));
57 static int comp_array_types PARAMS ((int (*) (tree, tree, int), tree,
58 tree, int));
59 static tree common_base_type PARAMS ((tree, tree));
60 static tree lookup_anon_field PARAMS ((tree, tree));
61 static tree pointer_diff PARAMS ((tree, tree, tree));
62 static tree qualify_type_recursive PARAMS ((tree, tree));
63 static tree get_delta_difference PARAMS ((tree, tree, int));
64 static int comp_cv_target_types PARAMS ((tree, tree, int));
65 static void casts_away_constness_r PARAMS ((tree *, tree *));
66 static int casts_away_constness PARAMS ((tree, tree));
67 static void maybe_warn_about_returning_address_of_local PARAMS ((tree));
68 static tree strip_all_pointer_quals PARAMS ((tree));
69 static tree lookup_destructor (tree, tree, tree);
71 /* Return the target type of TYPE, which means return T for:
72 T*, T&, T[], T (...), and otherwise, just T. */
74 tree
75 target_type (type)
76 tree type;
78 if (TREE_CODE (type) == REFERENCE_TYPE)
79 type = TREE_TYPE (type);
80 while (TREE_CODE (type) == POINTER_TYPE
81 || TREE_CODE (type) == ARRAY_TYPE
82 || TREE_CODE (type) == FUNCTION_TYPE
83 || TREE_CODE (type) == METHOD_TYPE
84 || TREE_CODE (type) == OFFSET_TYPE)
85 type = TREE_TYPE (type);
86 return type;
89 /* Do `exp = require_complete_type (exp);' to make sure exp
90 does not have an incomplete type. (That includes void types.)
91 Returns the error_mark_node if the VALUE does not have
92 complete type when this function returns. */
94 tree
95 require_complete_type (value)
96 tree value;
98 tree type;
100 if (processing_template_decl || value == error_mark_node)
101 return value;
103 if (TREE_CODE (value) == OVERLOAD)
104 type = unknown_type_node;
105 else
106 type = TREE_TYPE (value);
108 /* First, detect a valid value with a complete type. */
109 if (COMPLETE_TYPE_P (type))
110 return value;
112 /* If we see X::Y, we build an OFFSET_TYPE which has
113 not been laid out. Try to avoid an error by interpreting
114 it as this->X::Y, if reasonable. */
115 if (TREE_CODE (value) == OFFSET_REF
116 && current_class_ref != 0
117 && TREE_OPERAND (value, 0) == current_class_ref)
119 value = resolve_offset_ref (value);
120 return require_complete_type (value);
123 if (complete_type_or_else (type, value))
124 return value;
125 else
126 return error_mark_node;
129 /* Try to complete TYPE, if it is incomplete. For example, if TYPE is
130 a template instantiation, do the instantiation. Returns TYPE,
131 whether or not it could be completed, unless something goes
132 horribly wrong, in which case the error_mark_node is returned. */
134 tree
135 complete_type (type)
136 tree type;
138 if (type == NULL_TREE)
139 /* Rather than crash, we return something sure to cause an error
140 at some point. */
141 return error_mark_node;
143 if (type == error_mark_node || COMPLETE_TYPE_P (type))
145 else if (TREE_CODE (type) == ARRAY_TYPE && TYPE_DOMAIN (type))
147 tree t = complete_type (TREE_TYPE (type));
148 if (COMPLETE_TYPE_P (t) && ! processing_template_decl)
149 layout_type (type);
150 TYPE_NEEDS_CONSTRUCTING (type)
151 = TYPE_NEEDS_CONSTRUCTING (TYPE_MAIN_VARIANT (t));
152 TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type)
153 = TYPE_HAS_NONTRIVIAL_DESTRUCTOR (TYPE_MAIN_VARIANT (t));
155 else if (CLASS_TYPE_P (type) && CLASSTYPE_TEMPLATE_INSTANTIATION (type))
156 instantiate_class_template (TYPE_MAIN_VARIANT (type));
158 return type;
161 /* Like complete_type, but issue an error if the TYPE cannot be completed.
162 VALUE is used for informative diagnostics. DIAG_TYPE indicates the type
163 of diagnostic: 0 for an error, 1 for a warning, 2 for a pedwarn.
164 Returns NULL_TREE if the type cannot be made complete. */
166 tree
167 complete_type_or_diagnostic (type, value, diag_type)
168 tree type;
169 tree value;
170 int diag_type;
172 type = complete_type (type);
173 if (type == error_mark_node)
174 /* We already issued an error. */
175 return NULL_TREE;
176 else if (!COMPLETE_TYPE_P (type))
178 cxx_incomplete_type_diagnostic (value, type, diag_type);
179 return NULL_TREE;
181 else
182 return type;
185 /* Return truthvalue of whether type of EXP is instantiated. */
188 type_unknown_p (exp)
189 tree exp;
191 return (TREE_CODE (exp) == OVERLOAD
192 || TREE_CODE (exp) == TREE_LIST
193 || TREE_TYPE (exp) == unknown_type_node
194 || (TREE_CODE (TREE_TYPE (exp)) == OFFSET_TYPE
195 && TREE_TYPE (TREE_TYPE (exp)) == unknown_type_node));
198 /* Return a pointer or pointer to member type similar to T1, with a
199 cv-qualification signature that is the union of the cv-qualification
200 signatures of T1 and T2: [expr.rel], [expr.eq]. */
202 static tree
203 qualify_type_recursive (t1, t2)
204 tree t1, t2;
206 if ((TYPE_PTR_P (t1) && TYPE_PTR_P (t2))
207 || (TYPE_PTRMEM_P (t1) && TYPE_PTRMEM_P (t2)))
209 tree tt1;
210 tree tt2;
211 tree b1;
212 int type_quals;
213 tree tgt;
214 tree attributes = (*targetm.merge_type_attributes) (t1, t2);
216 if (TYPE_PTRMEM_P (t1))
218 b1 = TYPE_PTRMEM_CLASS_TYPE (t1);
219 tt1 = TYPE_PTRMEM_POINTED_TO_TYPE (t1);
220 tt2 = TYPE_PTRMEM_POINTED_TO_TYPE (t2);
222 else
224 b1 = NULL_TREE;
225 tt1 = TREE_TYPE (t1);
226 tt2 = TREE_TYPE (t2);
229 type_quals = (cp_type_quals (tt1) | cp_type_quals (tt2));
230 tgt = qualify_type_recursive (tt1, tt2);
231 tgt = cp_build_qualified_type (tgt, type_quals);
232 if (b1)
233 t1 = build_ptrmem_type (b1, tgt);
234 else
235 t1 = build_pointer_type (tgt);
236 t1 = build_type_attribute_variant (t1, attributes);
238 return t1;
241 /* Return the common type of two parameter lists.
242 We assume that comptypes has already been done and returned 1;
243 if that isn't so, this may crash.
245 As an optimization, free the space we allocate if the parameter
246 lists are already common. */
248 tree
249 commonparms (p1, p2)
250 tree p1, p2;
252 tree oldargs = p1, newargs, n;
253 int i, len;
254 int any_change = 0;
256 len = list_length (p1);
257 newargs = tree_last (p1);
259 if (newargs == void_list_node)
260 i = 1;
261 else
263 i = 0;
264 newargs = 0;
267 for (; i < len; i++)
268 newargs = tree_cons (NULL_TREE, NULL_TREE, newargs);
270 n = newargs;
272 for (i = 0; p1;
273 p1 = TREE_CHAIN (p1), p2 = TREE_CHAIN (p2), n = TREE_CHAIN (n), i++)
275 if (TREE_PURPOSE (p1) && !TREE_PURPOSE (p2))
277 TREE_PURPOSE (n) = TREE_PURPOSE (p1);
278 any_change = 1;
280 else if (! TREE_PURPOSE (p1))
282 if (TREE_PURPOSE (p2))
284 TREE_PURPOSE (n) = TREE_PURPOSE (p2);
285 any_change = 1;
288 else
290 if (1 != simple_cst_equal (TREE_PURPOSE (p1), TREE_PURPOSE (p2)))
291 any_change = 1;
292 TREE_PURPOSE (n) = TREE_PURPOSE (p2);
294 if (TREE_VALUE (p1) != TREE_VALUE (p2))
296 any_change = 1;
297 TREE_VALUE (n) = merge_types (TREE_VALUE (p1), TREE_VALUE (p2));
299 else
300 TREE_VALUE (n) = TREE_VALUE (p1);
302 if (! any_change)
303 return oldargs;
305 return newargs;
308 /* Given a type, perhaps copied for a typedef,
309 find the "original" version of it. */
310 tree
311 original_type (t)
312 tree t;
314 while (TYPE_NAME (t) != NULL_TREE)
316 tree x = TYPE_NAME (t);
317 if (TREE_CODE (x) != TYPE_DECL)
318 break;
319 x = DECL_ORIGINAL_TYPE (x);
320 if (x == NULL_TREE)
321 break;
322 t = x;
324 return t;
327 /* T1 and T2 are arithmetic or enumeration types. Return the type
328 that will result from the "usual arithmetic conversions" on T1 and
329 T2 as described in [expr]. */
331 tree
332 type_after_usual_arithmetic_conversions (t1, t2)
333 tree t1;
334 tree t2;
336 enum tree_code code1 = TREE_CODE (t1);
337 enum tree_code code2 = TREE_CODE (t2);
338 tree attributes;
340 /* FIXME: Attributes. */
341 my_friendly_assert (ARITHMETIC_TYPE_P (t1)
342 || TREE_CODE (t1) == COMPLEX_TYPE
343 || TREE_CODE (t1) == ENUMERAL_TYPE,
344 19990725);
345 my_friendly_assert (ARITHMETIC_TYPE_P (t2)
346 || TREE_CODE (t2) == COMPLEX_TYPE
347 || TREE_CODE (t2) == ENUMERAL_TYPE,
348 19990725);
350 /* In what follows, we slightly generalize the rules given in [expr] so
351 as to deal with `long long' and `complex'. First, merge the
352 attributes. */
353 attributes = (*targetm.merge_type_attributes) (t1, t2);
355 /* If one type is complex, form the common type of the non-complex
356 components, then make that complex. Use T1 or T2 if it is the
357 required type. */
358 if (code1 == COMPLEX_TYPE || code2 == COMPLEX_TYPE)
360 tree subtype1 = code1 == COMPLEX_TYPE ? TREE_TYPE (t1) : t1;
361 tree subtype2 = code2 == COMPLEX_TYPE ? TREE_TYPE (t2) : t2;
362 tree subtype
363 = type_after_usual_arithmetic_conversions (subtype1, subtype2);
365 if (code1 == COMPLEX_TYPE && TREE_TYPE (t1) == subtype)
366 return build_type_attribute_variant (t1, attributes);
367 else if (code2 == COMPLEX_TYPE && TREE_TYPE (t2) == subtype)
368 return build_type_attribute_variant (t2, attributes);
369 else
370 return build_type_attribute_variant (build_complex_type (subtype),
371 attributes);
374 /* If only one is real, use it as the result. */
375 if (code1 == REAL_TYPE && code2 != REAL_TYPE)
376 return build_type_attribute_variant (t1, attributes);
377 if (code2 == REAL_TYPE && code1 != REAL_TYPE)
378 return build_type_attribute_variant (t2, attributes);
380 /* Perform the integral promotions. */
381 if (code1 != REAL_TYPE)
383 t1 = type_promotes_to (t1);
384 t2 = type_promotes_to (t2);
387 /* Both real or both integers; use the one with greater precision. */
388 if (TYPE_PRECISION (t1) > TYPE_PRECISION (t2))
389 return build_type_attribute_variant (t1, attributes);
390 else if (TYPE_PRECISION (t2) > TYPE_PRECISION (t1))
391 return build_type_attribute_variant (t2, attributes);
393 /* The types are the same; no need to do anything fancy. */
394 if (TYPE_MAIN_VARIANT (t1) == TYPE_MAIN_VARIANT (t2))
395 return build_type_attribute_variant (t1, attributes);
397 if (code1 != REAL_TYPE)
399 /* If one is a sizetype, use it so size_binop doesn't blow up. */
400 if (TYPE_IS_SIZETYPE (t1) > TYPE_IS_SIZETYPE (t2))
401 return build_type_attribute_variant (t1, attributes);
402 if (TYPE_IS_SIZETYPE (t2) > TYPE_IS_SIZETYPE (t1))
403 return build_type_attribute_variant (t2, attributes);
405 /* If one is unsigned long long, then convert the other to unsigned
406 long long. */
407 if (same_type_p (TYPE_MAIN_VARIANT (t1), long_long_unsigned_type_node)
408 || same_type_p (TYPE_MAIN_VARIANT (t2), long_long_unsigned_type_node))
409 return build_type_attribute_variant (long_long_unsigned_type_node,
410 attributes);
411 /* If one is a long long, and the other is an unsigned long, and
412 long long can represent all the values of an unsigned long, then
413 convert to a long long. Otherwise, convert to an unsigned long
414 long. Otherwise, if either operand is long long, convert the
415 other to long long.
417 Since we're here, we know the TYPE_PRECISION is the same;
418 therefore converting to long long cannot represent all the values
419 of an unsigned long, so we choose unsigned long long in that
420 case. */
421 if (same_type_p (TYPE_MAIN_VARIANT (t1), long_long_integer_type_node)
422 || same_type_p (TYPE_MAIN_VARIANT (t2), long_long_integer_type_node))
424 tree t = ((TREE_UNSIGNED (t1) || TREE_UNSIGNED (t2))
425 ? long_long_unsigned_type_node
426 : long_long_integer_type_node);
427 return build_type_attribute_variant (t, attributes);
430 /* Go through the same procedure, but for longs. */
431 if (same_type_p (TYPE_MAIN_VARIANT (t1), long_unsigned_type_node)
432 || same_type_p (TYPE_MAIN_VARIANT (t2), long_unsigned_type_node))
433 return build_type_attribute_variant (long_unsigned_type_node,
434 attributes);
435 if (same_type_p (TYPE_MAIN_VARIANT (t1), long_integer_type_node)
436 || same_type_p (TYPE_MAIN_VARIANT (t2), long_integer_type_node))
438 tree t = ((TREE_UNSIGNED (t1) || TREE_UNSIGNED (t2))
439 ? long_unsigned_type_node : long_integer_type_node);
440 return build_type_attribute_variant (t, attributes);
442 /* Otherwise prefer the unsigned one. */
443 if (TREE_UNSIGNED (t1))
444 return build_type_attribute_variant (t1, attributes);
445 else
446 return build_type_attribute_variant (t2, attributes);
448 else
450 if (same_type_p (TYPE_MAIN_VARIANT (t1), long_double_type_node)
451 || same_type_p (TYPE_MAIN_VARIANT (t2), long_double_type_node))
452 return build_type_attribute_variant (long_double_type_node,
453 attributes);
454 if (same_type_p (TYPE_MAIN_VARIANT (t1), double_type_node)
455 || same_type_p (TYPE_MAIN_VARIANT (t2), double_type_node))
456 return build_type_attribute_variant (double_type_node,
457 attributes);
458 if (same_type_p (TYPE_MAIN_VARIANT (t1), float_type_node)
459 || same_type_p (TYPE_MAIN_VARIANT (t2), float_type_node))
460 return build_type_attribute_variant (float_type_node,
461 attributes);
463 /* Two floating-point types whose TYPE_MAIN_VARIANTs are none of
464 the standard C++ floating-point types. Logic earlier in this
465 function has already eliminated the possibility that
466 TYPE_PRECISION (t2) != TYPE_PRECISION (t1), so there's no
467 compelling reason to choose one or the other. */
468 return build_type_attribute_variant (t1, attributes);
472 /* Return the composite pointer type (see [expr.rel]) for T1 and T2.
473 ARG1 and ARG2 are the values with those types. The LOCATION is a
474 string describing the current location, in case an error occurs. */
476 tree
477 composite_pointer_type (t1, t2, arg1, arg2, location)
478 tree t1;
479 tree t2;
480 tree arg1;
481 tree arg2;
482 const char* location;
484 tree result_type;
485 tree attributes;
487 /* [expr.rel]
489 If one operand is a null pointer constant, the composite pointer
490 type is the type of the other operand. */
491 if (null_ptr_cst_p (arg1))
492 return t2;
493 if (null_ptr_cst_p (arg2))
494 return t1;
496 /* Deal with pointer-to-member functions in the same way as we deal
497 with pointers to functions. */
498 if (TYPE_PTRMEMFUNC_P (t1))
499 t1 = TYPE_PTRMEMFUNC_FN_TYPE (t1);
500 if (TYPE_PTRMEMFUNC_P (t2))
501 t2 = TYPE_PTRMEMFUNC_FN_TYPE (t2);
503 /* Merge the attributes. */
504 attributes = (*targetm.merge_type_attributes) (t1, t2);
506 /* We have:
508 [expr.rel]
510 If one of the operands has type "pointer to cv1 void*", then
511 the other has type "pointer to cv2T", and the composite pointer
512 type is "pointer to cv12 void", where cv12 is the union of cv1
513 and cv2.
515 If either type is a pointer to void, make sure it is T1. */
516 if (VOID_TYPE_P (TREE_TYPE (t2)))
518 tree t;
519 t = t1;
520 t1 = t2;
521 t2 = t;
523 /* Now, if T1 is a pointer to void, merge the qualifiers. */
524 if (VOID_TYPE_P (TREE_TYPE (t1)))
526 if (pedantic && TYPE_PTRFN_P (t2))
527 pedwarn ("ISO C++ forbids %s between pointer of type `void *' and pointer-to-function", location);
528 t1 = TREE_TYPE (t1);
529 t2 = TREE_TYPE (t2);
530 result_type = cp_build_qualified_type (void_type_node,
531 (cp_type_quals (t1)
532 | cp_type_quals (t2)));
533 result_type = build_pointer_type (result_type);
535 else
537 tree full1 = qualify_type_recursive (t1, t2);
538 tree full2 = qualify_type_recursive (t2, t1);
540 int val = comp_target_types (full1, full2, 1);
542 if (val > 0)
543 result_type = full1;
544 else if (val < 0)
545 result_type = full2;
546 else
548 pedwarn ("%s between distinct pointer types `%T' and `%T' lacks a cast",
549 location, t1, t2);
550 result_type = ptr_type_node;
554 return build_type_attribute_variant (result_type, attributes);
557 /* Return the merged type of two types.
558 We assume that comptypes has already been done and returned 1;
559 if that isn't so, this may crash.
561 This just combines attributes and default arguments; any other
562 differences would cause the two types to compare unalike. */
564 tree
565 merge_types (t1, t2)
566 tree t1, t2;
568 register enum tree_code code1;
569 register enum tree_code code2;
570 tree attributes;
572 /* Save time if the two types are the same. */
573 if (t1 == t2)
574 return t1;
575 if (original_type (t1) == original_type (t2))
576 return t1;
578 /* If one type is nonsense, use the other. */
579 if (t1 == error_mark_node)
580 return t2;
581 if (t2 == error_mark_node)
582 return t1;
584 /* Merge the attributes. */
585 attributes = (*targetm.merge_type_attributes) (t1, t2);
587 /* Treat an enum type as the unsigned integer type of the same width. */
589 if (TYPE_PTRMEMFUNC_P (t1))
590 t1 = TYPE_PTRMEMFUNC_FN_TYPE (t1);
591 if (TYPE_PTRMEMFUNC_P (t2))
592 t2 = TYPE_PTRMEMFUNC_FN_TYPE (t2);
594 code1 = TREE_CODE (t1);
595 code2 = TREE_CODE (t2);
597 switch (code1)
599 case POINTER_TYPE:
600 case REFERENCE_TYPE:
601 /* For two pointers, do this recursively on the target type. */
603 tree target = merge_types (TREE_TYPE (t1), TREE_TYPE (t2));
604 int quals = cp_type_quals (t1);
606 if (code1 == POINTER_TYPE)
607 t1 = build_pointer_type (target);
608 else
609 t1 = build_reference_type (target);
610 t1 = build_type_attribute_variant (t1, attributes);
611 t1 = cp_build_qualified_type (t1, quals);
613 if (TREE_CODE (target) == METHOD_TYPE)
614 t1 = build_ptrmemfunc_type (t1);
616 return t1;
619 case OFFSET_TYPE:
621 tree base = TYPE_OFFSET_BASETYPE (t1);
622 tree target = merge_types (TREE_TYPE (t1), TREE_TYPE (t2));
623 t1 = build_offset_type (base, target);
624 break;
627 case ARRAY_TYPE:
629 tree elt = merge_types (TREE_TYPE (t1), TREE_TYPE (t2));
630 /* Save space: see if the result is identical to one of the args. */
631 if (elt == TREE_TYPE (t1) && TYPE_DOMAIN (t1))
632 return build_type_attribute_variant (t1, attributes);
633 if (elt == TREE_TYPE (t2) && TYPE_DOMAIN (t2))
634 return build_type_attribute_variant (t2, attributes);
635 /* Merge the element types, and have a size if either arg has one. */
636 t1 = build_cplus_array_type
637 (elt, TYPE_DOMAIN (TYPE_DOMAIN (t1) ? t1 : t2));
638 break;
641 case FUNCTION_TYPE:
642 /* Function types: prefer the one that specified arg types.
643 If both do, merge the arg types. Also merge the return types. */
645 tree valtype = merge_types (TREE_TYPE (t1), TREE_TYPE (t2));
646 tree p1 = TYPE_ARG_TYPES (t1);
647 tree p2 = TYPE_ARG_TYPES (t2);
648 tree rval, raises;
650 /* Save space: see if the result is identical to one of the args. */
651 if (valtype == TREE_TYPE (t1) && ! p2)
652 return build_type_attribute_variant (t1, attributes);
653 if (valtype == TREE_TYPE (t2) && ! p1)
654 return build_type_attribute_variant (t2, attributes);
656 /* Simple way if one arg fails to specify argument types. */
657 if (p1 == NULL_TREE || TREE_VALUE (p1) == void_type_node)
659 rval = build_function_type (valtype, p2);
660 if ((raises = TYPE_RAISES_EXCEPTIONS (t2)))
661 rval = build_exception_variant (rval, raises);
662 return build_type_attribute_variant (rval, attributes);
664 raises = TYPE_RAISES_EXCEPTIONS (t1);
665 if (p2 == NULL_TREE || TREE_VALUE (p2) == void_type_node)
667 rval = build_function_type (valtype, p1);
668 if (raises)
669 rval = build_exception_variant (rval, raises);
670 return build_type_attribute_variant (rval, attributes);
673 rval = build_function_type (valtype, commonparms (p1, p2));
674 t1 = build_exception_variant (rval, raises);
675 break;
678 case METHOD_TYPE:
680 /* Get this value the long way, since TYPE_METHOD_BASETYPE
681 is just the main variant of this. */
682 tree basetype = TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (t2)));
683 tree raises = TYPE_RAISES_EXCEPTIONS (t1);
684 tree t3;
686 /* If this was a member function type, get back to the
687 original type of type member function (i.e., without
688 the class instance variable up front. */
689 t1 = build_function_type (TREE_TYPE (t1),
690 TREE_CHAIN (TYPE_ARG_TYPES (t1)));
691 t2 = build_function_type (TREE_TYPE (t2),
692 TREE_CHAIN (TYPE_ARG_TYPES (t2)));
693 t3 = merge_types (t1, t2);
694 t3 = build_cplus_method_type (basetype, TREE_TYPE (t3),
695 TYPE_ARG_TYPES (t3));
696 t1 = build_exception_variant (t3, raises);
697 break;
700 default:;
702 return build_type_attribute_variant (t1, attributes);
705 /* Return the common type of two types.
706 We assume that comptypes has already been done and returned 1;
707 if that isn't so, this may crash.
709 This is the type for the result of most arithmetic operations
710 if the operands have the given two types. */
712 tree
713 common_type (t1, t2)
714 tree t1, t2;
716 enum tree_code code1;
717 enum tree_code code2;
719 /* If one type is nonsense, bail. */
720 if (t1 == error_mark_node || t2 == error_mark_node)
721 return error_mark_node;
723 code1 = TREE_CODE (t1);
724 code2 = TREE_CODE (t2);
726 if ((ARITHMETIC_TYPE_P (t1) || code1 == ENUMERAL_TYPE
727 || code1 == COMPLEX_TYPE)
728 && (ARITHMETIC_TYPE_P (t2) || code2 == ENUMERAL_TYPE
729 || code2 == COMPLEX_TYPE))
730 return type_after_usual_arithmetic_conversions (t1, t2);
732 else if ((TYPE_PTR_P (t1) && TYPE_PTR_P (t2))
733 || (TYPE_PTRMEM_P (t1) && TYPE_PTRMEM_P (t2))
734 || (TYPE_PTRMEMFUNC_P (t1) && TYPE_PTRMEMFUNC_P (t2)))
735 return composite_pointer_type (t1, t2, error_mark_node, error_mark_node,
736 "conversion");
738 else
739 abort ();
742 /* Compare two exception specifier types for exactness or subsetness, if
743 allowed. Returns 0 for mismatch, 1 for same, 2 if B is allowed by A.
745 [except.spec] "If a class X ... objects of class X or any class publicly
746 and unambigously derrived from X. Similarly, if a pointer type Y * ...
747 exceptions of type Y * or that are pointers to any type publicly and
748 unambigously derrived from Y. Otherwise a function only allows exceptions
749 that have the same type ..."
750 This does not mention cv qualifiers and is different to what throw
751 [except.throw] and catch [except.catch] will do. They will ignore the
752 top level cv qualifiers, and allow qualifiers in the pointer to class
753 example.
755 We implement the letter of the standard. */
757 static int
758 comp_except_types (a, b, exact)
759 tree a, b;
760 int exact;
762 if (same_type_p (a, b))
763 return 1;
764 else if (!exact)
766 if (cp_type_quals (a) || cp_type_quals (b))
767 return 0;
769 if (TREE_CODE (a) == POINTER_TYPE
770 && TREE_CODE (b) == POINTER_TYPE)
772 a = TREE_TYPE (a);
773 b = TREE_TYPE (b);
774 if (cp_type_quals (a) || cp_type_quals (b))
775 return 0;
778 if (TREE_CODE (a) != RECORD_TYPE
779 || TREE_CODE (b) != RECORD_TYPE)
780 return 0;
782 if (ACCESSIBLY_UNIQUELY_DERIVED_P (a, b))
783 return 2;
785 return 0;
788 /* Return 1 if TYPE1 and TYPE2 are equivalent exception specifiers.
789 If EXACT is 0, T2 can be stricter than T1 (according to 15.4/7),
790 otherwise it must be exact. Exception lists are unordered, but
791 we've already filtered out duplicates. Most lists will be in order,
792 we should try to make use of that. */
795 comp_except_specs (t1, t2, exact)
796 tree t1, t2;
797 int exact;
799 tree probe;
800 tree base;
801 int length = 0;
803 if (t1 == t2)
804 return 1;
806 if (t1 == NULL_TREE) /* T1 is ... */
807 return t2 == NULL_TREE || !exact;
808 if (!TREE_VALUE (t1)) /* t1 is EMPTY */
809 return t2 != NULL_TREE && !TREE_VALUE (t2);
810 if (t2 == NULL_TREE) /* T2 is ... */
811 return 0;
812 if (TREE_VALUE (t1) && !TREE_VALUE (t2)) /* T2 is EMPTY, T1 is not */
813 return !exact;
815 /* Neither set is ... or EMPTY, make sure each part of T2 is in T1.
816 Count how many we find, to determine exactness. For exact matching and
817 ordered T1, T2, this is an O(n) operation, otherwise its worst case is
818 O(nm). */
819 for (base = t1; t2 != NULL_TREE; t2 = TREE_CHAIN (t2))
821 for (probe = base; probe != NULL_TREE; probe = TREE_CHAIN (probe))
823 tree a = TREE_VALUE (probe);
824 tree b = TREE_VALUE (t2);
826 if (comp_except_types (a, b, exact))
828 if (probe == base && exact)
829 base = TREE_CHAIN (probe);
830 length++;
831 break;
834 if (probe == NULL_TREE)
835 return 0;
837 return !exact || base == NULL_TREE || length == list_length (t1);
840 /* Compare the array types T1 and T2, using CMP as the type comparison
841 function for the element types. STRICT is as for comptypes. */
843 static int
844 comp_array_types (cmp, t1, t2, strict)
845 register int (*cmp) PARAMS ((tree, tree, int));
846 tree t1, t2;
847 int strict;
849 tree d1;
850 tree d2;
852 if (t1 == t2)
853 return 1;
855 /* The type of the array elements must be the same. */
856 if (!(TREE_TYPE (t1) == TREE_TYPE (t2)
857 || (*cmp) (TREE_TYPE (t1), TREE_TYPE (t2),
858 strict & ~COMPARE_REDECLARATION)))
859 return 0;
861 d1 = TYPE_DOMAIN (t1);
862 d2 = TYPE_DOMAIN (t2);
864 if (d1 == d2)
865 return 1;
867 /* If one of the arrays is dimensionless, and the other has a
868 dimension, they are of different types. However, it is valid to
869 write:
871 extern int a[];
872 int a[3];
874 by [basic.link]:
876 declarations for an array object can specify
877 array types that differ by the presence or absence of a major
878 array bound (_dcl.array_). */
879 if (!d1 || !d2)
880 return strict & COMPARE_REDECLARATION;
882 /* Check that the dimensions are the same. */
883 return (cp_tree_equal (TYPE_MIN_VALUE (d1),
884 TYPE_MIN_VALUE (d2))
885 && cp_tree_equal (TYPE_MAX_VALUE (d1),
886 TYPE_MAX_VALUE (d2)));
889 /* Return 1 if T1 and T2 are compatible types for assignment or
890 various other operations. STRICT is a bitwise-or of the COMPARE_*
891 flags. */
894 comptypes (t1, t2, strict)
895 tree t1;
896 tree t2;
897 int strict;
899 int attrval, val;
900 int orig_strict = strict;
902 /* The special exemption for redeclaring array types without an
903 array bound only applies at the top level:
905 extern int (*i)[];
906 int (*i)[8];
908 is invalid, for example. */
909 strict &= ~COMPARE_REDECLARATION;
911 /* Suppress errors caused by previously reported errors */
912 if (t1 == t2)
913 return 1;
915 /* This should never happen. */
916 my_friendly_assert (t1 != error_mark_node, 307);
918 if (t2 == error_mark_node)
919 return 0;
921 /* If either type is the internal version of sizetype, return the
922 language version. */
923 if (TREE_CODE (t1) == INTEGER_TYPE && TYPE_IS_SIZETYPE (t1)
924 && TYPE_DOMAIN (t1) != 0)
925 t1 = TYPE_DOMAIN (t1);
927 if (TREE_CODE (t2) == INTEGER_TYPE && TYPE_IS_SIZETYPE (t2)
928 && TYPE_DOMAIN (t2) != 0)
929 t2 = TYPE_DOMAIN (t2);
931 if (strict & COMPARE_RELAXED)
933 /* Treat an enum type as the unsigned integer type of the same width. */
935 if (TREE_CODE (t1) == ENUMERAL_TYPE)
936 t1 = c_common_type_for_size (TYPE_PRECISION (t1), 1);
937 if (TREE_CODE (t2) == ENUMERAL_TYPE)
938 t2 = c_common_type_for_size (TYPE_PRECISION (t2), 1);
940 if (t1 == t2)
941 return 1;
944 if (TYPE_PTRMEMFUNC_P (t1))
945 t1 = TYPE_PTRMEMFUNC_FN_TYPE (t1);
946 if (TYPE_PTRMEMFUNC_P (t2))
947 t2 = TYPE_PTRMEMFUNC_FN_TYPE (t2);
949 /* TYPENAME_TYPEs should be resolved if the qualifying scope is the
950 current instantiation. */
951 if (TREE_CODE (t1) == TYPENAME_TYPE)
952 t1 = resolve_typename_type_in_current_instantiation (t1);
953 if (TREE_CODE (t2) == TYPENAME_TYPE)
954 t2 = resolve_typename_type_in_current_instantiation (t2);
956 /* Different classes of types can't be compatible. */
957 if (TREE_CODE (t1) != TREE_CODE (t2))
958 return 0;
960 /* Qualifiers must match. */
961 if (cp_type_quals (t1) != cp_type_quals (t2))
962 return 0;
963 if (strict == COMPARE_STRICT
964 && TYPE_FOR_JAVA (t1) != TYPE_FOR_JAVA (t2))
965 return 0;
967 /* Allow for two different type nodes which have essentially the same
968 definition. Note that we already checked for equality of the type
969 qualifiers (just above). */
971 if (TYPE_MAIN_VARIANT (t1) == TYPE_MAIN_VARIANT (t2))
972 return 1;
974 if (strict & COMPARE_NO_ATTRIBUTES)
975 attrval = 1;
976 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
977 else if (! (attrval = (*targetm.comp_type_attributes) (t1, t2)))
978 return 0;
980 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
981 val = 0;
983 switch (TREE_CODE (t1))
985 case TEMPLATE_TEMPLATE_PARM:
986 case BOUND_TEMPLATE_TEMPLATE_PARM:
987 if (TEMPLATE_TYPE_IDX (t1) != TEMPLATE_TYPE_IDX (t2)
988 || TEMPLATE_TYPE_LEVEL (t1) != TEMPLATE_TYPE_LEVEL (t2))
989 return 0;
990 if (! comp_template_parms
991 (DECL_TEMPLATE_PARMS (TEMPLATE_TEMPLATE_PARM_TEMPLATE_DECL (t1)),
992 DECL_TEMPLATE_PARMS (TEMPLATE_TEMPLATE_PARM_TEMPLATE_DECL (t2))))
993 return 0;
994 if (TREE_CODE (t1) == TEMPLATE_TEMPLATE_PARM)
995 return 1;
996 /* Don't check inheritance. */
997 strict = COMPARE_STRICT;
998 /* fall through */
1000 case RECORD_TYPE:
1001 case UNION_TYPE:
1002 if (TYPE_TEMPLATE_INFO (t1) && TYPE_TEMPLATE_INFO (t2)
1003 && (TYPE_TI_TEMPLATE (t1) == TYPE_TI_TEMPLATE (t2)
1004 || TREE_CODE (t1) == BOUND_TEMPLATE_TEMPLATE_PARM))
1005 val = comp_template_args (TYPE_TI_ARGS (t1),
1006 TYPE_TI_ARGS (t2));
1007 look_hard:
1008 if ((strict & COMPARE_BASE) && DERIVED_FROM_P (t1, t2))
1009 val = 1;
1010 else if ((strict & COMPARE_RELAXED) && DERIVED_FROM_P (t2, t1))
1011 val = 1;
1012 break;
1014 case OFFSET_TYPE:
1015 val = (comptypes (build_pointer_type (TYPE_OFFSET_BASETYPE (t1)),
1016 build_pointer_type (TYPE_OFFSET_BASETYPE (t2)), strict)
1017 && comptypes (TREE_TYPE (t1), TREE_TYPE (t2), strict));
1018 break;
1020 case POINTER_TYPE:
1021 case REFERENCE_TYPE:
1022 t1 = TREE_TYPE (t1);
1023 t2 = TREE_TYPE (t2);
1024 /* first, check whether the referred types match with the
1025 required level of strictness */
1026 val = comptypes (t1, t2, strict);
1027 if (val)
1028 break;
1029 if (TREE_CODE (t1) == RECORD_TYPE
1030 && TREE_CODE (t2) == RECORD_TYPE)
1031 goto look_hard;
1032 break;
1034 case METHOD_TYPE:
1035 case FUNCTION_TYPE:
1036 val = ((TREE_TYPE (t1) == TREE_TYPE (t2)
1037 || comptypes (TREE_TYPE (t1), TREE_TYPE (t2), strict))
1038 && compparms (TYPE_ARG_TYPES (t1), TYPE_ARG_TYPES (t2)));
1039 break;
1041 case ARRAY_TYPE:
1042 /* Target types must match incl. qualifiers. We use ORIG_STRICT
1043 here since this is the one place where
1044 COMPARE_REDECLARATION should be used. */
1045 val = comp_array_types (comptypes, t1, t2, orig_strict);
1046 break;
1048 case TEMPLATE_TYPE_PARM:
1049 return TEMPLATE_TYPE_IDX (t1) == TEMPLATE_TYPE_IDX (t2)
1050 && TEMPLATE_TYPE_LEVEL (t1) == TEMPLATE_TYPE_LEVEL (t2);
1052 case TYPENAME_TYPE:
1053 if (cp_tree_equal (TYPENAME_TYPE_FULLNAME (t1),
1054 TYPENAME_TYPE_FULLNAME (t2)) < 1)
1055 return 0;
1056 return same_type_p (TYPE_CONTEXT (t1), TYPE_CONTEXT (t2));
1058 case UNBOUND_CLASS_TEMPLATE:
1059 if (cp_tree_equal (TYPE_IDENTIFIER (t1),
1060 TYPE_IDENTIFIER (t2)) < 1)
1061 return 0;
1062 return same_type_p (TYPE_CONTEXT (t1), TYPE_CONTEXT (t2));
1064 case COMPLEX_TYPE:
1065 return same_type_p (TREE_TYPE (t1), TREE_TYPE (t2));
1067 default:
1068 break;
1070 return attrval == 2 && val == 1 ? 2 : val;
1073 /* Subroutine of comp_target-types. Make sure that the cv-quals change
1074 only in the same direction as the target type. */
1076 static int
1077 comp_cv_target_types (ttl, ttr, nptrs)
1078 tree ttl, ttr;
1079 int nptrs;
1081 int t;
1083 if (!at_least_as_qualified_p (ttl, ttr)
1084 && !at_least_as_qualified_p (ttr, ttl))
1085 /* The qualifications are incomparable. */
1086 return 0;
1088 if (TYPE_MAIN_VARIANT (ttl) == TYPE_MAIN_VARIANT (ttr))
1089 return more_qualified_p (ttr, ttl) ? -1 : 1;
1091 t = comp_target_types (ttl, ttr, nptrs);
1092 if ((t == 1 && at_least_as_qualified_p (ttl, ttr))
1093 || (t == -1 && at_least_as_qualified_p (ttr, ttl)))
1094 return t;
1096 return 0;
1099 /* Return 1 or -1 if TTL and TTR are pointers to types that are equivalent,
1100 ignoring their qualifiers, 0 if not. Return 1 means that TTR can be
1101 converted to TTL. Return -1 means that TTL can be converted to TTR but
1102 not vice versa.
1104 NPTRS is the number of pointers we can strip off and keep cool.
1105 This is used to permit (for aggr A, aggr B) A, B* to convert to A*,
1106 but to not permit B** to convert to A**.
1108 This should go away. Callers should use can_convert or something
1109 similar instead. (jason 17 Apr 1997) */
1112 comp_target_types (ttl, ttr, nptrs)
1113 tree ttl, ttr;
1114 int nptrs;
1116 ttl = TYPE_MAIN_VARIANT (ttl);
1117 ttr = TYPE_MAIN_VARIANT (ttr);
1118 if (same_type_p (ttl, ttr))
1119 return 1;
1121 if (TREE_CODE (ttr) != TREE_CODE (ttl))
1122 return 0;
1124 if ((TREE_CODE (ttr) == POINTER_TYPE
1125 || TREE_CODE (ttr) == REFERENCE_TYPE)
1126 /* If we get a pointer with nptrs == 0, we don't allow any tweaking
1127 of the type pointed to. This is necessary for reference init
1128 semantics. We won't get here from a previous call with nptrs == 1;
1129 for multi-level pointers we end up in comp_ptr_ttypes. */
1130 && nptrs > 0)
1132 int is_ptr = TREE_CODE (ttr) == POINTER_TYPE;
1134 ttl = TREE_TYPE (ttl);
1135 ttr = TREE_TYPE (ttr);
1137 if (is_ptr)
1139 if (TREE_CODE (ttl) == UNKNOWN_TYPE
1140 || TREE_CODE (ttr) == UNKNOWN_TYPE)
1141 return 1;
1142 else if (TREE_CODE (ttl) == VOID_TYPE
1143 && TREE_CODE (ttr) != FUNCTION_TYPE
1144 && TREE_CODE (ttr) != METHOD_TYPE
1145 && TREE_CODE (ttr) != OFFSET_TYPE)
1146 return 1;
1147 else if (TREE_CODE (ttr) == VOID_TYPE
1148 && TREE_CODE (ttl) != FUNCTION_TYPE
1149 && TREE_CODE (ttl) != METHOD_TYPE
1150 && TREE_CODE (ttl) != OFFSET_TYPE)
1151 return -1;
1152 else if (TREE_CODE (ttl) == POINTER_TYPE
1153 || TREE_CODE (ttl) == ARRAY_TYPE)
1155 if (comp_ptr_ttypes (ttl, ttr))
1156 return 1;
1157 else if (comp_ptr_ttypes (ttr, ttl))
1158 return -1;
1159 return 0;
1163 /* Const and volatile mean something different for function types,
1164 so the usual checks are not appropriate. */
1165 if (TREE_CODE (ttl) == FUNCTION_TYPE || TREE_CODE (ttl) == METHOD_TYPE)
1166 return comp_target_types (ttl, ttr, nptrs - 1);
1168 return comp_cv_target_types (ttl, ttr, nptrs - 1);
1171 if (TREE_CODE (ttr) == ARRAY_TYPE)
1172 return comp_array_types (comp_target_types, ttl, ttr, COMPARE_STRICT);
1173 else if (TREE_CODE (ttr) == FUNCTION_TYPE || TREE_CODE (ttr) == METHOD_TYPE)
1175 tree argsl, argsr;
1176 int saw_contra = 0;
1178 if (pedantic)
1180 if (!same_type_p (TREE_TYPE (ttl), TREE_TYPE (ttr)))
1181 return 0;
1183 else
1185 switch (comp_target_types (TREE_TYPE (ttl), TREE_TYPE (ttr), -1))
1187 case 0:
1188 return 0;
1189 case -1:
1190 saw_contra = 1;
1194 argsl = TYPE_ARG_TYPES (ttl);
1195 argsr = TYPE_ARG_TYPES (ttr);
1197 /* Compare 'this' here, not in comp_target_parms. */
1198 if (TREE_CODE (ttr) == METHOD_TYPE)
1200 tree tl = TYPE_METHOD_BASETYPE (ttl);
1201 tree tr = TYPE_METHOD_BASETYPE (ttr);
1203 if (!same_or_base_type_p (tr, tl))
1205 if (same_or_base_type_p (tl, tr))
1206 saw_contra = 1;
1207 else
1208 return 0;
1211 argsl = TREE_CHAIN (argsl);
1212 argsr = TREE_CHAIN (argsr);
1215 switch (comp_target_parms (argsl, argsr))
1217 case 0:
1218 return 0;
1219 case -1:
1220 saw_contra = 1;
1223 return saw_contra ? -1 : 1;
1225 /* for C++ */
1226 else if (TREE_CODE (ttr) == OFFSET_TYPE)
1228 int base;
1230 /* Contravariance: we can assign a pointer to base member to a pointer
1231 to derived member. Note difference from simple pointer case, where
1232 we can pass a pointer to derived to a pointer to base. */
1233 if (same_or_base_type_p (TYPE_OFFSET_BASETYPE (ttr),
1234 TYPE_OFFSET_BASETYPE (ttl)))
1235 base = 1;
1236 else if (same_or_base_type_p (TYPE_OFFSET_BASETYPE (ttl),
1237 TYPE_OFFSET_BASETYPE (ttr)))
1239 tree tmp = ttl;
1240 ttl = ttr;
1241 ttr = tmp;
1242 base = -1;
1244 else
1245 return 0;
1247 ttl = TREE_TYPE (ttl);
1248 ttr = TREE_TYPE (ttr);
1250 if (TREE_CODE (ttl) == POINTER_TYPE
1251 || TREE_CODE (ttl) == ARRAY_TYPE)
1253 if (comp_ptr_ttypes (ttl, ttr))
1254 return base;
1255 return 0;
1257 else
1259 if (comp_cv_target_types (ttl, ttr, nptrs) == 1)
1260 return base;
1261 return 0;
1264 else if (IS_AGGR_TYPE (ttl))
1266 if (nptrs < 0)
1267 return 0;
1268 if (same_or_base_type_p (build_pointer_type (ttl),
1269 build_pointer_type (ttr)))
1270 return 1;
1271 if (same_or_base_type_p (build_pointer_type (ttr),
1272 build_pointer_type (ttl)))
1273 return -1;
1274 return 0;
1277 return 0;
1280 /* Returns 1 if TYPE1 is at least as qualified as TYPE2. */
1283 at_least_as_qualified_p (type1, type2)
1284 tree type1;
1285 tree type2;
1287 /* All qualifiers for TYPE2 must also appear in TYPE1. */
1288 return ((cp_type_quals (type1) & cp_type_quals (type2))
1289 == cp_type_quals (type2));
1292 /* Returns 1 if TYPE1 is more qualified than TYPE2. */
1295 more_qualified_p (type1, type2)
1296 tree type1;
1297 tree type2;
1299 return (cp_type_quals (type1) != cp_type_quals (type2)
1300 && at_least_as_qualified_p (type1, type2));
1303 /* Returns 1 if TYPE1 is more cv-qualified than TYPE2, -1 if TYPE2 is
1304 more cv-qualified that TYPE1, and 0 otherwise. */
1307 comp_cv_qualification (type1, type2)
1308 tree type1;
1309 tree type2;
1311 if (cp_type_quals (type1) == cp_type_quals (type2))
1312 return 0;
1314 if (at_least_as_qualified_p (type1, type2))
1315 return 1;
1317 else if (at_least_as_qualified_p (type2, type1))
1318 return -1;
1320 return 0;
1323 /* Returns 1 if the cv-qualification signature of TYPE1 is a proper
1324 subset of the cv-qualification signature of TYPE2, and the types
1325 are similar. Returns -1 if the other way 'round, and 0 otherwise. */
1328 comp_cv_qual_signature (type1, type2)
1329 tree type1;
1330 tree type2;
1332 if (comp_ptr_ttypes_real (type2, type1, -1))
1333 return 1;
1334 else if (comp_ptr_ttypes_real (type1, type2, -1))
1335 return -1;
1336 else
1337 return 0;
1340 /* If two types share a common base type, return that basetype.
1341 If there is not a unique most-derived base type, this function
1342 returns ERROR_MARK_NODE. */
1344 static tree
1345 common_base_type (tt1, tt2)
1346 tree tt1, tt2;
1348 tree best = NULL_TREE;
1349 int i;
1351 /* If one is a baseclass of another, that's good enough. */
1352 if (UNIQUELY_DERIVED_FROM_P (tt1, tt2))
1353 return tt1;
1354 if (UNIQUELY_DERIVED_FROM_P (tt2, tt1))
1355 return tt2;
1357 /* Otherwise, try to find a unique baseclass of TT1
1358 that is shared by TT2, and follow that down. */
1359 for (i = CLASSTYPE_N_BASECLASSES (tt1)-1; i >= 0; i--)
1361 tree basetype = TYPE_BINFO_BASETYPE (tt1, i);
1362 tree trial = common_base_type (basetype, tt2);
1363 if (trial)
1365 if (trial == error_mark_node)
1366 return trial;
1367 if (best == NULL_TREE)
1368 best = trial;
1369 else if (best != trial)
1370 return error_mark_node;
1374 /* Same for TT2. */
1375 for (i = CLASSTYPE_N_BASECLASSES (tt2)-1; i >= 0; i--)
1377 tree basetype = TYPE_BINFO_BASETYPE (tt2, i);
1378 tree trial = common_base_type (tt1, basetype);
1379 if (trial)
1381 if (trial == error_mark_node)
1382 return trial;
1383 if (best == NULL_TREE)
1384 best = trial;
1385 else if (best != trial)
1386 return error_mark_node;
1389 return best;
1392 /* Subroutines of `comptypes'. */
1394 /* Return 1 if two parameter type lists PARMS1 and PARMS2 are
1395 equivalent in the sense that functions with those parameter types
1396 can have equivalent types. The two lists must be equivalent,
1397 element by element.
1399 C++: See comment above about TYPE1, TYPE2. */
1402 compparms (parms1, parms2)
1403 tree parms1, parms2;
1405 register tree t1 = parms1, t2 = parms2;
1407 /* An unspecified parmlist matches any specified parmlist
1408 whose argument types don't need default promotions. */
1410 while (1)
1412 if (t1 == 0 && t2 == 0)
1413 return 1;
1414 /* If one parmlist is shorter than the other,
1415 they fail to match. */
1416 if (t1 == 0 || t2 == 0)
1417 return 0;
1418 if (!same_type_p (TREE_VALUE (t2), TREE_VALUE (t1)))
1419 return 0;
1421 t1 = TREE_CHAIN (t1);
1422 t2 = TREE_CHAIN (t2);
1426 /* This really wants return whether or not parameter type lists
1427 would make their owning functions assignment compatible or not.
1429 The return value is like for comp_target_types.
1431 This should go away, possibly with the exception of the empty parmlist
1432 conversion; there are no conversions between function types in C++.
1433 (jason 17 Apr 1997) */
1435 static int
1436 comp_target_parms (parms1, parms2)
1437 tree parms1, parms2;
1439 register tree t1 = parms1, t2 = parms2;
1440 int warn_contravariance = 0;
1442 /* In C, an unspecified parmlist matches any specified parmlist
1443 whose argument types don't need default promotions. This is not
1444 true for C++, but let's do it anyway for unfixed headers. */
1446 if (t1 == 0 && t2 != 0)
1448 pedwarn ("ISO C++ prohibits conversion from `%#T' to `(...)'",
1449 parms2);
1450 return self_promoting_args_p (t2);
1452 if (t2 == 0)
1453 return self_promoting_args_p (t1);
1455 for (; t1 || t2; t1 = TREE_CHAIN (t1), t2 = TREE_CHAIN (t2))
1457 tree p1, p2;
1459 /* If one parmlist is shorter than the other,
1460 they fail to match, unless STRICT is <= 0. */
1461 if (t1 == 0 || t2 == 0)
1462 return 0;
1463 p1 = TREE_VALUE (t1);
1464 p2 = TREE_VALUE (t2);
1465 if (same_type_p (p1, p2))
1466 continue;
1468 if (pedantic)
1469 return 0;
1471 if ((TREE_CODE (p1) == POINTER_TYPE && TREE_CODE (p2) == POINTER_TYPE)
1472 || (TREE_CODE (p1) == REFERENCE_TYPE
1473 && TREE_CODE (p2) == REFERENCE_TYPE))
1475 /* The following is wrong for contravariance,
1476 but many programs depend on it. */
1477 if (TREE_TYPE (p1) == void_type_node)
1478 continue;
1479 if (TREE_TYPE (p2) == void_type_node)
1481 warn_contravariance = 1;
1482 continue;
1484 if (IS_AGGR_TYPE (TREE_TYPE (p1))
1485 && !same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (p1),
1486 TREE_TYPE (p2)))
1487 return 0;
1489 /* Note backwards order due to contravariance. */
1490 if (comp_target_types (p2, p1, 1) <= 0)
1492 if (comp_target_types (p1, p2, 1) > 0)
1494 warn_contravariance = 1;
1495 continue;
1497 return 0;
1500 return warn_contravariance ? -1 : 1;
1503 tree
1504 cxx_sizeof_or_alignof_type (type, op, complain)
1505 tree type;
1506 enum tree_code op;
1507 int complain;
1509 enum tree_code type_code;
1510 tree value;
1511 const char *op_name;
1513 my_friendly_assert (op == SIZEOF_EXPR || op == ALIGNOF_EXPR, 20020720);
1514 if (processing_template_decl)
1515 return build_min_nt (op, type);
1517 op_name = operator_name_info[(int) op].name;
1519 if (TREE_CODE (type) == REFERENCE_TYPE)
1520 type = TREE_TYPE (type);
1521 type_code = TREE_CODE (type);
1523 if (type_code == METHOD_TYPE)
1525 if (complain && (pedantic || warn_pointer_arith))
1526 pedwarn ("invalid application of `%s' to a member function", op_name);
1527 value = size_one_node;
1529 else if (type_code == OFFSET_TYPE)
1531 if (complain)
1532 error ("invalid application of `%s' to non-static member", op_name);
1533 value = size_zero_node;
1535 else
1536 value = c_sizeof_or_alignof_type (complete_type (type), op, complain);
1538 return value;
1541 tree
1542 expr_sizeof (e)
1543 tree e;
1545 if (processing_template_decl)
1546 return build_min_nt (SIZEOF_EXPR, e);
1548 if (TREE_CODE (e) == COMPONENT_REF
1549 && DECL_C_BIT_FIELD (TREE_OPERAND (e, 1)))
1550 error ("sizeof applied to a bit-field");
1551 if (is_overloaded_fn (e))
1553 pedwarn ("ISO C++ forbids applying `sizeof' to an expression of function type");
1554 return c_sizeof (char_type_node);
1556 else if (type_unknown_p (e))
1558 cxx_incomplete_type_error (e, TREE_TYPE (e));
1559 return c_sizeof (char_type_node);
1561 /* It's invalid to say `sizeof (X::i)' for `i' a non-static data
1562 member unless you're in a non-static member of X. So hand off to
1563 resolve_offset_ref. [expr.prim] */
1564 else if (TREE_CODE (e) == OFFSET_REF)
1565 e = resolve_offset_ref (e);
1567 if (e == error_mark_node)
1568 return e;
1570 return cxx_sizeof (TREE_TYPE (e));
1574 /* Perform the array-to-pointer and function-to-pointer conversions
1575 for EXP.
1577 In addition, references are converted to lvalues and manifest
1578 constants are replaced by their values. */
1580 tree
1581 decay_conversion (exp)
1582 tree exp;
1584 register tree type;
1585 register enum tree_code code;
1587 if (TREE_CODE (exp) == OFFSET_REF)
1588 exp = resolve_offset_ref (exp);
1590 type = TREE_TYPE (exp);
1591 code = TREE_CODE (type);
1593 if (code == REFERENCE_TYPE)
1595 exp = convert_from_reference (exp);
1596 type = TREE_TYPE (exp);
1597 code = TREE_CODE (type);
1600 if (type == error_mark_node)
1601 return error_mark_node;
1603 if (type_unknown_p (exp))
1605 cxx_incomplete_type_error (exp, TREE_TYPE (exp));
1606 return error_mark_node;
1609 /* Constants can be used directly unless they're not loadable. */
1610 if (TREE_CODE (exp) == CONST_DECL)
1611 exp = DECL_INITIAL (exp);
1612 /* Replace a nonvolatile const static variable with its value. We
1613 don't do this for arrays, though; we want the address of the
1614 first element of the array, not the address of the first element
1615 of its initializing constant. */
1616 else if (code != ARRAY_TYPE)
1618 exp = decl_constant_value (exp);
1619 type = TREE_TYPE (exp);
1622 /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
1623 Leave such NOP_EXPRs, since RHS is being used in non-lvalue context. */
1625 if (code == VOID_TYPE)
1627 error ("void value not ignored as it ought to be");
1628 return error_mark_node;
1630 if (code == METHOD_TYPE)
1631 abort ();
1632 if (code == FUNCTION_TYPE || is_overloaded_fn (exp))
1633 return build_unary_op (ADDR_EXPR, exp, 0);
1634 if (code == ARRAY_TYPE)
1636 register tree adr;
1637 tree ptrtype;
1639 if (TREE_CODE (exp) == INDIRECT_REF)
1641 /* Stripping away the INDIRECT_REF is not the right
1642 thing to do for references... */
1643 tree inner = TREE_OPERAND (exp, 0);
1644 if (TREE_CODE (TREE_TYPE (inner)) == REFERENCE_TYPE)
1646 inner = build1 (CONVERT_EXPR,
1647 build_pointer_type (TREE_TYPE
1648 (TREE_TYPE (inner))),
1649 inner);
1650 TREE_CONSTANT (inner) = TREE_CONSTANT (TREE_OPERAND (inner, 0));
1652 return cp_convert (build_pointer_type (TREE_TYPE (type)), inner);
1655 if (TREE_CODE (exp) == COMPOUND_EXPR)
1657 tree op1 = decay_conversion (TREE_OPERAND (exp, 1));
1658 return build (COMPOUND_EXPR, TREE_TYPE (op1),
1659 TREE_OPERAND (exp, 0), op1);
1662 if (!lvalue_p (exp)
1663 && ! (TREE_CODE (exp) == CONSTRUCTOR && TREE_STATIC (exp)))
1665 error ("invalid use of non-lvalue array");
1666 return error_mark_node;
1669 ptrtype = build_pointer_type (TREE_TYPE (type));
1671 if (TREE_CODE (exp) == VAR_DECL)
1673 /* ??? This is not really quite correct
1674 in that the type of the operand of ADDR_EXPR
1675 is not the target type of the type of the ADDR_EXPR itself.
1676 Question is, can this lossage be avoided? */
1677 adr = build1 (ADDR_EXPR, ptrtype, exp);
1678 if (!cxx_mark_addressable (exp))
1679 return error_mark_node;
1680 TREE_CONSTANT (adr) = staticp (exp);
1681 TREE_SIDE_EFFECTS (adr) = 0; /* Default would be, same as EXP. */
1682 return adr;
1684 /* This way is better for a COMPONENT_REF since it can
1685 simplify the offset for a component. */
1686 adr = build_unary_op (ADDR_EXPR, exp, 1);
1687 return cp_convert (ptrtype, adr);
1690 /* [basic.lval]: Class rvalues can have cv-qualified types; non-class
1691 rvalues always have cv-unqualified types. */
1692 if (! CLASS_TYPE_P (type))
1693 exp = cp_convert (TYPE_MAIN_VARIANT (type), exp);
1695 return exp;
1698 tree
1699 default_conversion (exp)
1700 tree exp;
1702 tree type;
1703 enum tree_code code;
1705 exp = decay_conversion (exp);
1707 type = TREE_TYPE (exp);
1708 code = TREE_CODE (type);
1710 if (INTEGRAL_CODE_P (code))
1712 tree t = type_promotes_to (type);
1713 if (t != type)
1714 return cp_convert (t, exp);
1717 return exp;
1720 /* Take the address of an inline function without setting TREE_ADDRESSABLE
1721 or TREE_USED. */
1723 tree
1724 inline_conversion (exp)
1725 tree exp;
1727 if (TREE_CODE (exp) == FUNCTION_DECL)
1728 exp = build1 (ADDR_EXPR, build_pointer_type (TREE_TYPE (exp)), exp);
1730 return exp;
1733 /* Returns nonzero iff exp is a STRING_CST or the result of applying
1734 decay_conversion to one. */
1737 string_conv_p (totype, exp, warn)
1738 tree totype, exp;
1739 int warn;
1741 tree t;
1743 if (! flag_const_strings || TREE_CODE (totype) != POINTER_TYPE)
1744 return 0;
1746 t = TREE_TYPE (totype);
1747 if (!same_type_p (t, char_type_node)
1748 && !same_type_p (t, wchar_type_node))
1749 return 0;
1751 if (TREE_CODE (exp) == STRING_CST)
1753 /* Make sure that we don't try to convert between char and wchar_t. */
1754 if (!same_type_p (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (exp))), t))
1755 return 0;
1757 else
1759 /* Is this a string constant which has decayed to 'const char *'? */
1760 t = build_pointer_type (build_qualified_type (t, TYPE_QUAL_CONST));
1761 if (!same_type_p (TREE_TYPE (exp), t))
1762 return 0;
1763 STRIP_NOPS (exp);
1764 if (TREE_CODE (exp) != ADDR_EXPR
1765 || TREE_CODE (TREE_OPERAND (exp, 0)) != STRING_CST)
1766 return 0;
1769 /* This warning is not very useful, as it complains about printf. */
1770 if (warn && warn_write_strings)
1771 warning ("deprecated conversion from string constant to `%T'", totype);
1773 return 1;
1776 /* Given a COND_EXPR, MIN_EXPR, or MAX_EXPR in T, return it in a form that we
1777 can, for example, use as an lvalue. This code used to be in
1778 unary_complex_lvalue, but we needed it to deal with `a = (d == c) ? b : c'
1779 expressions, where we're dealing with aggregates. But now it's again only
1780 called from unary_complex_lvalue. The case (in particular) that led to
1781 this was with CODE == ADDR_EXPR, since it's not an lvalue when we'd
1782 get it there. */
1784 static tree
1785 rationalize_conditional_expr (code, t)
1786 enum tree_code code;
1787 tree t;
1789 /* For MIN_EXPR or MAX_EXPR, fold-const.c has arranged things so that
1790 the first operand is always the one to be used if both operands
1791 are equal, so we know what conditional expression this used to be. */
1792 if (TREE_CODE (t) == MIN_EXPR || TREE_CODE (t) == MAX_EXPR)
1794 return
1795 build_conditional_expr (build_x_binary_op ((TREE_CODE (t) == MIN_EXPR
1796 ? LE_EXPR : GE_EXPR),
1797 TREE_OPERAND (t, 0),
1798 TREE_OPERAND (t, 1)),
1799 build_unary_op (code, TREE_OPERAND (t, 0), 0),
1800 build_unary_op (code, TREE_OPERAND (t, 1), 0));
1803 return
1804 build_conditional_expr (TREE_OPERAND (t, 0),
1805 build_unary_op (code, TREE_OPERAND (t, 1), 0),
1806 build_unary_op (code, TREE_OPERAND (t, 2), 0));
1809 /* Given the TYPE of an anonymous union field inside T, return the
1810 FIELD_DECL for the field. If not found return NULL_TREE. Because
1811 anonymous unions can nest, we must also search all anonymous unions
1812 that are directly reachable. */
1814 static tree
1815 lookup_anon_field (t, type)
1816 tree t, type;
1818 tree field;
1820 for (field = TYPE_FIELDS (t); field; field = TREE_CHAIN (field))
1822 if (TREE_STATIC (field))
1823 continue;
1824 if (TREE_CODE (field) != FIELD_DECL || DECL_ARTIFICIAL (field))
1825 continue;
1827 /* If we find it directly, return the field. */
1828 if (DECL_NAME (field) == NULL_TREE
1829 && type == TYPE_MAIN_VARIANT (TREE_TYPE (field)))
1831 return field;
1834 /* Otherwise, it could be nested, search harder. */
1835 if (DECL_NAME (field) == NULL_TREE
1836 && ANON_AGGR_TYPE_P (TREE_TYPE (field)))
1838 tree subfield = lookup_anon_field (TREE_TYPE (field), type);
1839 if (subfield)
1840 return subfield;
1843 return NULL_TREE;
1846 /* Build an expression representing OBJECT.MEMBER. OBJECT is an
1847 expression; MEMBER is a DECL or baselink. If ACCESS_PATH is
1848 non-NULL, it indicates the path to the base used to name MEMBER.
1849 If PRESERVE_REFERENCE is true, the expression returned will have
1850 REFERENCE_TYPE if the MEMBER does. Otherwise, the expression
1851 returned will have the type referred to by the reference.
1853 This function does not perform access control; that is either done
1854 earlier by the parser when the name of MEMBER is resolved to MEMBER
1855 itself, or later when overload resolution selects one of the
1856 functions indicated by MEMBER. */
1858 tree
1859 build_class_member_access_expr (tree object, tree member,
1860 tree access_path, bool preserve_reference)
1862 tree object_type;
1863 tree member_scope;
1864 tree result = NULL_TREE;
1866 if (object == error_mark_node || member == error_mark_node)
1867 return error_mark_node;
1869 if (TREE_CODE (member) == PSEUDO_DTOR_EXPR)
1870 return member;
1872 my_friendly_assert (DECL_P (member) || BASELINK_P (member),
1873 20020801);
1875 /* [expr.ref]
1877 The type of the first expression shall be "class object" (of a
1878 complete type). */
1879 object_type = TREE_TYPE (object);
1880 if (!complete_type_or_else (object_type, object))
1881 return error_mark_node;
1882 if (!CLASS_TYPE_P (object_type))
1884 error ("request for member `%D' in `%E', which is of non-class type `%T'",
1885 member, object, object_type);
1886 return error_mark_node;
1889 /* The standard does not seem to actually say that MEMBER must be a
1890 member of OBJECT_TYPE. However, that is clearly what is
1891 intended. */
1892 if (DECL_P (member))
1894 member_scope = DECL_CLASS_CONTEXT (member);
1895 mark_used (member);
1896 if (TREE_DEPRECATED (member))
1897 warn_deprecated_use (member);
1899 else
1900 member_scope = BINFO_TYPE (BASELINK_BINFO (member));
1901 /* If MEMBER is from an anonymous aggregate, MEMBER_SCOPE will
1902 presently be the anonymous union. Go outwards until we find a
1903 type related to OBJECT_TYPE. */
1904 while (ANON_AGGR_TYPE_P (member_scope)
1905 && !same_type_ignoring_top_level_qualifiers_p (member_scope,
1906 object_type))
1907 member_scope = TYPE_CONTEXT (member_scope);
1908 if (!member_scope || !DERIVED_FROM_P (member_scope, object_type))
1910 error ("`%D' is not a member of `%T'", member, object_type);
1911 return error_mark_node;
1914 /* Transform `(a, b).x' into `(*(a, &b)).x', `(a ? b : c).x' into
1915 `(*(a ? &b : &c)).x', and so on. A COND_EXPR is only an lvalue
1916 in the frontend; only _DECLs and _REFs are lvalues in the backend. */
1918 tree temp = unary_complex_lvalue (ADDR_EXPR, object);
1919 if (temp)
1920 object = build_indirect_ref (temp, NULL);
1923 /* In [expr.ref], there is an explicit list of the valid choices for
1924 MEMBER. We check for each of those cases here. */
1925 if (TREE_CODE (member) == VAR_DECL)
1927 /* A static data member. */
1928 result = member;
1929 /* If OBJECT has side-effects, they are supposed to occur. */
1930 if (TREE_SIDE_EFFECTS (object))
1931 result = build (COMPOUND_EXPR, TREE_TYPE (result), object, result);
1933 else if (TREE_CODE (member) == FIELD_DECL)
1935 /* A non-static data member. */
1936 bool null_object_p;
1937 int type_quals;
1938 tree member_type;
1940 null_object_p = (TREE_CODE (object) == INDIRECT_REF
1941 && integer_zerop (TREE_OPERAND (object, 0)));
1943 /* Convert OBJECT to the type of MEMBER. */
1944 if (!same_type_p (object_type, member_scope))
1946 tree binfo;
1947 base_kind kind;
1949 binfo = lookup_base (access_path ? access_path : object_type,
1950 member_scope, ba_ignore, &kind);
1951 if (binfo == error_mark_node)
1952 return error_mark_node;
1954 /* It is invalid to use to try to get to a virtual base of a
1955 NULL object. The most common cause is invalid use of
1956 offsetof macro. */
1957 if (null_object_p && kind == bk_via_virtual)
1959 error ("invalid access to non-static data member `%D' of NULL object",
1960 member);
1961 error ("(perhaps the `offsetof' macro was used incorrectly)");
1962 return error_mark_node;
1965 /* Convert to the base. */
1966 object = build_base_path (PLUS_EXPR, object, binfo,
1967 /*nonnull=*/1);
1968 /* If we found the base successfully then we should be able
1969 to convert to it successfully. */
1970 my_friendly_assert (object != error_mark_node,
1971 20020801);
1974 /* Complain about other invalid uses of offsetof, even though they will
1975 give the right answer. Note that we complain whether or not they
1976 actually used the offsetof macro, since there's no way to know at this
1977 point. So we just give a warning, instead of a pedwarn. */
1978 if (null_object_p && CLASSTYPE_NON_POD_P (object_type))
1980 warning ("invalid access to non-static data member `%D' of NULL object",
1981 member);
1982 warning ("(perhaps the `offsetof' macro was used incorrectly)");
1985 /* If MEMBER is from an anonymous aggregate, we have converted
1986 OBJECT so that it refers to the class containing the
1987 anonymous union. Generate a reference to the anonymous union
1988 itself, and recur to find MEMBER. */
1989 if (ANON_AGGR_TYPE_P (DECL_CONTEXT (member))
1990 /* When this code is called from build_field_call, the
1991 object already has the type of the anonymous union.
1992 That is because the COMPONENT_REF was already
1993 constructed, and was then disassembled before calling
1994 build_field_call. After the function-call code is
1995 cleaned up, this waste can be eliminated. */
1996 && (!same_type_ignoring_top_level_qualifiers_p
1997 (TREE_TYPE (object), DECL_CONTEXT (member))))
1999 tree anonymous_union;
2001 anonymous_union = lookup_anon_field (TREE_TYPE (object),
2002 DECL_CONTEXT (member));
2003 object = build_class_member_access_expr (object,
2004 anonymous_union,
2005 /*access_path=*/NULL_TREE,
2006 preserve_reference);
2009 /* Compute the type of the field, as described in [expr.ref]. */
2010 type_quals = TYPE_UNQUALIFIED;
2011 member_type = TREE_TYPE (member);
2012 if (TREE_CODE (member_type) != REFERENCE_TYPE)
2014 type_quals = (cp_type_quals (member_type)
2015 | cp_type_quals (object_type));
2017 /* A field is const (volatile) if the enclosing object, or the
2018 field itself, is const (volatile). But, a mutable field is
2019 not const, even within a const object. */
2020 if (DECL_MUTABLE_P (member))
2021 type_quals &= ~TYPE_QUAL_CONST;
2022 member_type = cp_build_qualified_type (member_type, type_quals);
2025 result = fold (build (COMPONENT_REF, member_type, object, member));
2027 /* Mark the expression const or volatile, as appropriate. Even
2028 though we've dealt with the type above, we still have to mark the
2029 expression itself. */
2030 if (type_quals & TYPE_QUAL_CONST)
2031 TREE_READONLY (result) = 1;
2032 else if (type_quals & TYPE_QUAL_VOLATILE)
2033 TREE_THIS_VOLATILE (result) = 1;
2035 else if (BASELINK_P (member))
2037 /* The member is a (possibly overloaded) member function. */
2038 tree functions;
2039 tree type;
2041 /* If the MEMBER is exactly one static member function, then we
2042 know the type of the expression. Otherwise, we must wait
2043 until overload resolution has been performed. */
2044 functions = BASELINK_FUNCTIONS (member);
2045 if (TREE_CODE (functions) == FUNCTION_DECL
2046 && DECL_STATIC_FUNCTION_P (functions))
2047 type = TREE_TYPE (functions);
2048 else
2049 type = unknown_type_node;
2050 /* Note that we do not convert OBJECT to the BASELINK_BINFO
2051 base. That will happen when the function is called. */
2052 result = build (COMPONENT_REF, type, object, member);
2054 else if (TREE_CODE (member) == CONST_DECL)
2056 /* The member is an enumerator. */
2057 result = member;
2058 /* If OBJECT has side-effects, they are supposed to occur. */
2059 if (TREE_SIDE_EFFECTS (object))
2060 result = build (COMPOUND_EXPR, TREE_TYPE (result),
2061 object, result);
2063 else
2065 error ("invalid use of `%D'", member);
2066 return error_mark_node;
2069 if (!preserve_reference)
2070 /* [expr.ref]
2072 If E2 is declared to have type "reference to T", then ... the
2073 type of E1.E2 is T. */
2074 result = convert_from_reference (result);
2076 return result;
2079 /* Return the destructor denoted by OBJECT.SCOPE::~DTOR_NAME, or, if
2080 SCOPE is NULL, by OBJECT.~DTOR_NAME. */
2082 static tree
2083 lookup_destructor (tree object, tree scope, tree dtor_name)
2085 tree object_type = TREE_TYPE (object);
2086 tree dtor_type = TREE_OPERAND (dtor_name, 0);
2088 if (scope && !check_dtor_name (scope, dtor_name))
2090 error ("qualified type `%T' does not match destructor name `~%T'",
2091 scope, dtor_type);
2092 return error_mark_node;
2094 if (!same_type_p (dtor_type, TYPE_MAIN_VARIANT (object_type)))
2096 error ("destructor name `%T' does not match type `%T' of expression",
2097 dtor_type, object_type);
2098 return error_mark_node;
2100 if (!TYPE_HAS_DESTRUCTOR (object_type))
2101 return build (PSEUDO_DTOR_EXPR, void_type_node, object, scope,
2102 dtor_type);
2103 return lookup_member (object_type, complete_dtor_identifier,
2104 /*protect=*/1, /*want_type=*/false);
2107 /* This function is called by the parser to process a class member
2108 access expression of the form OBJECT.NAME. NAME is a node used by
2109 the parser to represent a name; it is not yet a DECL. It may,
2110 however, be a BASELINK where the BASELINK_FUNCTIONS is a
2111 TEMPLATE_ID_EXPR. Templates must be looked up by the parser, and
2112 there is no reason to do the lookup twice, so the parser keeps the
2113 BASELINK. */
2115 tree
2116 finish_class_member_access_expr (tree object, tree name)
2118 tree object_type;
2119 tree member;
2120 tree access_path = NULL_TREE;
2122 if (object == error_mark_node || name == error_mark_node)
2123 return error_mark_node;
2125 if (processing_template_decl)
2126 return build_min_nt (COMPONENT_REF, object, name);
2128 if (TREE_CODE (object) == OFFSET_REF)
2129 object = resolve_offset_ref (object);
2131 object_type = TREE_TYPE (object);
2132 if (TREE_CODE (object_type) == REFERENCE_TYPE)
2134 object = convert_from_reference (object);
2135 object_type = TREE_TYPE (object);
2138 /* [expr.ref]
2140 The type of the first expression shall be "class object" (of a
2141 complete type). */
2142 if (!complete_type_or_else (object_type, object))
2143 return error_mark_node;
2144 if (!CLASS_TYPE_P (object_type))
2146 error ("request for member `%D' in `%E', which is of non-class type `%T'",
2147 name, object, object_type);
2148 return error_mark_node;
2151 if (BASELINK_P (name))
2153 /* A member function that has already been looked up. */
2154 my_friendly_assert ((TREE_CODE (BASELINK_FUNCTIONS (name))
2155 == TEMPLATE_ID_EXPR),
2156 20020805);
2157 member = name;
2159 else
2161 bool is_template_id = false;
2162 tree template_args = NULL_TREE;
2164 if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
2166 is_template_id = true;
2167 template_args = TREE_OPERAND (name, 1);
2168 name = TREE_OPERAND (name, 0);
2171 if (TREE_CODE (name) == SCOPE_REF)
2173 tree scope;
2175 /* A qualified name. The qualifying class or namespace `S' has
2176 already been looked up; it is either a TYPE or a
2177 NAMESPACE_DECL. The member name is either an IDENTIFIER_NODE
2178 or a BIT_NOT_EXPR. */
2179 scope = TREE_OPERAND (name, 0);
2180 name = TREE_OPERAND (name, 1);
2181 my_friendly_assert ((CLASS_TYPE_P (scope)
2182 || TREE_CODE (scope) == NAMESPACE_DECL),
2183 20020804);
2184 my_friendly_assert ((TREE_CODE (name) == IDENTIFIER_NODE
2185 || TREE_CODE (name) == BIT_NOT_EXPR),
2186 20020804);
2188 /* If SCOPE is a namespace, then the qualified name does not
2189 name a member of OBJECT_TYPE. */
2190 if (TREE_CODE (scope) == NAMESPACE_DECL)
2192 error ("`%D::%D' is not a member of `%T'",
2193 scope, name, object_type);
2194 return error_mark_node;
2197 /* Find the base of OBJECT_TYPE corresponding to SCOPE. */
2198 access_path = lookup_base (object_type, scope, ba_check, NULL);
2199 if (!access_path || access_path == error_mark_node)
2200 return error_mark_node;
2202 if (TREE_CODE (name) == BIT_NOT_EXPR)
2203 member = lookup_destructor (object, scope, name);
2204 else
2206 /* Look up the member. */
2207 member = lookup_member (access_path, name, /*protect=*/1,
2208 /*want_type=*/false);
2209 if (member == NULL_TREE)
2211 error ("'%D' has no member named '%E'", object_type, name);
2212 return error_mark_node;
2214 if (member == error_mark_node)
2215 return error_mark_node;
2218 else if (TREE_CODE (name) == BIT_NOT_EXPR)
2219 member = lookup_destructor (object, /*scope=*/NULL_TREE, name);
2220 else if (TREE_CODE (name) == IDENTIFIER_NODE)
2222 /* An unqualified name. */
2223 member = lookup_member (object_type, name, /*protect=*/1,
2224 /*want_type=*/false);
2225 if (member == NULL_TREE)
2227 error ("'%D' has no member named '%E'", object_type, name);
2228 return error_mark_node;
2230 else if (member == error_mark_node)
2231 return error_mark_node;
2233 else
2235 /* The YACC parser sometimes gives us things that are not names.
2236 These always indicate errors. The recursive-descent parser
2237 does not do this, so this code can go away once that parser
2238 replaces the YACC parser. */
2239 error ("invalid use of `%D'", name);
2240 return error_mark_node;
2243 if (is_template_id)
2245 tree template = member;
2247 if (BASELINK_P (template))
2248 BASELINK_FUNCTIONS (template)
2249 = build_nt (TEMPLATE_ID_EXPR,
2250 BASELINK_FUNCTIONS (template),
2251 template_args);
2252 else
2254 error ("`%D' is not a member template function", name);
2255 return error_mark_node;
2260 if (TREE_DEPRECATED (member))
2261 warn_deprecated_use (member);
2263 return build_class_member_access_expr (object, member, access_path,
2264 /*preserve_reference=*/false);
2267 /* Return an expression for the MEMBER_NAME field in the internal
2268 representation of PTRMEM, a pointer-to-member function. (Each
2269 pointer-to-member function type gets its own RECORD_TYPE so it is
2270 more convenient to access the fields by name than by FIELD_DECL.)
2271 This routine converts the NAME to a FIELD_DECL and then creates the
2272 node for the complete expression. */
2274 tree
2275 build_ptrmemfunc_access_expr (tree ptrmem, tree member_name)
2277 tree ptrmem_type;
2278 tree member;
2279 tree member_type;
2281 /* This code is a stripped down version of
2282 build_class_member_access_expr. It does not work to use that
2283 routine directly because it expects the object to be of class
2284 type. */
2285 ptrmem_type = TREE_TYPE (ptrmem);
2286 my_friendly_assert (TYPE_PTRMEMFUNC_P (ptrmem_type), 20020804);
2287 member = lookup_member (ptrmem_type, member_name, /*protect=*/0,
2288 /*want_type=*/false);
2289 member_type = cp_build_qualified_type (TREE_TYPE (member),
2290 cp_type_quals (ptrmem_type));
2291 return fold (build (COMPONENT_REF, member_type, ptrmem, member));
2294 /* Given an expression PTR for a pointer, return an expression
2295 for the value pointed to.
2296 ERRORSTRING is the name of the operator to appear in error messages.
2298 This function may need to overload OPERATOR_FNNAME.
2299 Must also handle REFERENCE_TYPEs for C++. */
2301 tree
2302 build_x_indirect_ref (ptr, errorstring)
2303 tree ptr;
2304 const char *errorstring;
2306 tree rval;
2308 if (processing_template_decl)
2309 return build_min_nt (INDIRECT_REF, ptr);
2311 rval = build_new_op (INDIRECT_REF, LOOKUP_NORMAL, ptr, NULL_TREE,
2312 NULL_TREE);
2313 if (rval)
2314 return rval;
2315 return build_indirect_ref (ptr, errorstring);
2318 tree
2319 build_indirect_ref (ptr, errorstring)
2320 tree ptr;
2321 const char *errorstring;
2323 register tree pointer, type;
2325 if (ptr == error_mark_node)
2326 return error_mark_node;
2328 if (ptr == current_class_ptr)
2329 return current_class_ref;
2331 pointer = (TREE_CODE (TREE_TYPE (ptr)) == REFERENCE_TYPE
2332 ? ptr : default_conversion (ptr));
2333 type = TREE_TYPE (pointer);
2335 if (TYPE_PTR_P (type) || TREE_CODE (type) == REFERENCE_TYPE)
2337 /* [expr.unary.op]
2339 If the type of the expression is "pointer to T," the type
2340 of the result is "T."
2342 We must use the canonical variant because certain parts of
2343 the back end, like fold, do pointer comparisons between
2344 types. */
2345 tree t = canonical_type_variant (TREE_TYPE (type));
2347 if (VOID_TYPE_P (t))
2349 /* A pointer to incomplete type (other than cv void) can be
2350 dereferenced [expr.unary.op]/1 */
2351 error ("`%T' is not a pointer-to-object type", type);
2352 return error_mark_node;
2354 else if (TREE_CODE (pointer) == ADDR_EXPR
2355 && same_type_p (t, TREE_TYPE (TREE_OPERAND (pointer, 0))))
2356 /* The POINTER was something like `&x'. We simplify `*&x' to
2357 `x'. */
2358 return TREE_OPERAND (pointer, 0);
2359 else
2361 tree ref = build1 (INDIRECT_REF, t, pointer);
2363 /* We *must* set TREE_READONLY when dereferencing a pointer to const,
2364 so that we get the proper error message if the result is used
2365 to assign to. Also, &* is supposed to be a no-op. */
2366 TREE_READONLY (ref) = CP_TYPE_CONST_P (t);
2367 TREE_THIS_VOLATILE (ref) = CP_TYPE_VOLATILE_P (t);
2368 TREE_SIDE_EFFECTS (ref)
2369 = (TREE_THIS_VOLATILE (ref) || TREE_SIDE_EFFECTS (pointer));
2370 return ref;
2373 /* `pointer' won't be an error_mark_node if we were given a
2374 pointer to member, so it's cool to check for this here. */
2375 else if (TYPE_PTRMEM_P (type) || TYPE_PTRMEMFUNC_P (type))
2376 error ("invalid use of `%s' on pointer to member", errorstring);
2377 else if (pointer != error_mark_node)
2379 if (errorstring)
2380 error ("invalid type argument of `%s'", errorstring);
2381 else
2382 error ("invalid type argument");
2384 return error_mark_node;
2387 /* This handles expressions of the form "a[i]", which denotes
2388 an array reference.
2390 This is logically equivalent in C to *(a+i), but we may do it differently.
2391 If A is a variable or a member, we generate a primitive ARRAY_REF.
2392 This avoids forcing the array out of registers, and can work on
2393 arrays that are not lvalues (for example, members of structures returned
2394 by functions).
2396 If INDEX is of some user-defined type, it must be converted to
2397 integer type. Otherwise, to make a compatible PLUS_EXPR, it
2398 will inherit the type of the array, which will be some pointer type. */
2400 tree
2401 build_array_ref (array, idx)
2402 tree array, idx;
2404 if (idx == 0)
2406 error ("subscript missing in array reference");
2407 return error_mark_node;
2410 if (TREE_TYPE (array) == error_mark_node
2411 || TREE_TYPE (idx) == error_mark_node)
2412 return error_mark_node;
2414 /* If ARRAY is a COMPOUND_EXPR or COND_EXPR, move our reference
2415 inside it. */
2416 switch (TREE_CODE (array))
2418 case COMPOUND_EXPR:
2420 tree value = build_array_ref (TREE_OPERAND (array, 1), idx);
2421 return build (COMPOUND_EXPR, TREE_TYPE (value),
2422 TREE_OPERAND (array, 0), value);
2425 case COND_EXPR:
2426 return build_conditional_expr
2427 (TREE_OPERAND (array, 0),
2428 build_array_ref (TREE_OPERAND (array, 1), idx),
2429 build_array_ref (TREE_OPERAND (array, 2), idx));
2431 default:
2432 break;
2435 if (TREE_CODE (TREE_TYPE (array)) == ARRAY_TYPE
2436 && TREE_CODE (array) != INDIRECT_REF)
2438 tree rval, type;
2440 /* Subscripting with type char is likely to lose
2441 on a machine where chars are signed.
2442 So warn on any machine, but optionally.
2443 Don't warn for unsigned char since that type is safe.
2444 Don't warn for signed char because anyone who uses that
2445 must have done so deliberately. */
2446 if (warn_char_subscripts
2447 && TYPE_MAIN_VARIANT (TREE_TYPE (idx)) == char_type_node)
2448 warning ("array subscript has type `char'");
2450 /* Apply default promotions *after* noticing character types. */
2451 idx = default_conversion (idx);
2453 if (TREE_CODE (TREE_TYPE (idx)) != INTEGER_TYPE)
2455 error ("array subscript is not an integer");
2456 return error_mark_node;
2459 /* An array that is indexed by a non-constant
2460 cannot be stored in a register; we must be able to do
2461 address arithmetic on its address.
2462 Likewise an array of elements of variable size. */
2463 if (TREE_CODE (idx) != INTEGER_CST
2464 || (COMPLETE_TYPE_P (TREE_TYPE (TREE_TYPE (array)))
2465 && (TREE_CODE (TYPE_SIZE (TREE_TYPE (TREE_TYPE (array))))
2466 != INTEGER_CST)))
2468 if (!cxx_mark_addressable (array))
2469 return error_mark_node;
2472 /* An array that is indexed by a constant value which is not within
2473 the array bounds cannot be stored in a register either; because we
2474 would get a crash in store_bit_field/extract_bit_field when trying
2475 to access a non-existent part of the register. */
2476 if (TREE_CODE (idx) == INTEGER_CST
2477 && TYPE_VALUES (TREE_TYPE (array))
2478 && ! int_fits_type_p (idx, TYPE_VALUES (TREE_TYPE (array))))
2480 if (!cxx_mark_addressable (array))
2481 return error_mark_node;
2484 if (pedantic && !lvalue_p (array))
2485 pedwarn ("ISO C++ forbids subscripting non-lvalue array");
2487 /* Note in C++ it is valid to subscript a `register' array, since
2488 it is valid to take the address of something with that
2489 storage specification. */
2490 if (extra_warnings)
2492 tree foo = array;
2493 while (TREE_CODE (foo) == COMPONENT_REF)
2494 foo = TREE_OPERAND (foo, 0);
2495 if (TREE_CODE (foo) == VAR_DECL && DECL_REGISTER (foo))
2496 warning ("subscripting array declared `register'");
2499 type = TREE_TYPE (TREE_TYPE (array));
2500 rval = build (ARRAY_REF, type, array, idx);
2501 /* Array ref is const/volatile if the array elements are
2502 or if the array is.. */
2503 TREE_READONLY (rval)
2504 |= (CP_TYPE_CONST_P (type) | TREE_READONLY (array));
2505 TREE_SIDE_EFFECTS (rval)
2506 |= (CP_TYPE_VOLATILE_P (type) | TREE_SIDE_EFFECTS (array));
2507 TREE_THIS_VOLATILE (rval)
2508 |= (CP_TYPE_VOLATILE_P (type) | TREE_THIS_VOLATILE (array));
2509 return require_complete_type (fold (rval));
2513 tree ar = default_conversion (array);
2514 tree ind = default_conversion (idx);
2516 /* Put the integer in IND to simplify error checking. */
2517 if (TREE_CODE (TREE_TYPE (ar)) == INTEGER_TYPE)
2519 tree temp = ar;
2520 ar = ind;
2521 ind = temp;
2524 if (ar == error_mark_node)
2525 return ar;
2527 if (TREE_CODE (TREE_TYPE (ar)) != POINTER_TYPE)
2529 error ("subscripted value is neither array nor pointer");
2530 return error_mark_node;
2532 if (TREE_CODE (TREE_TYPE (ind)) != INTEGER_TYPE)
2534 error ("array subscript is not an integer");
2535 return error_mark_node;
2538 return build_indirect_ref (cp_build_binary_op (PLUS_EXPR, ar, ind),
2539 "array indexing");
2543 /* Resolve a pointer to member function. INSTANCE is the object
2544 instance to use, if the member points to a virtual member.
2546 This used to avoid checking for virtual functions if basetype
2547 has no virtual functions, according to an earlier ANSI draft.
2548 With the final ISO C++ rules, such an optimization is
2549 incorrect: A pointer to a derived member can be static_cast
2550 to pointer-to-base-member, as long as the dynamic object
2551 later has the right member. */
2553 tree
2554 get_member_function_from_ptrfunc (instance_ptrptr, function)
2555 tree *instance_ptrptr;
2556 tree function;
2558 if (TREE_CODE (function) == OFFSET_REF)
2559 function = TREE_OPERAND (function, 1);
2561 if (TYPE_PTRMEMFUNC_P (TREE_TYPE (function)))
2563 tree idx, delta, e1, e2, e3, vtbl, basetype;
2564 tree fntype = TYPE_PTRMEMFUNC_FN_TYPE (TREE_TYPE (function));
2566 tree instance_ptr = *instance_ptrptr;
2567 tree instance_save_expr = 0;
2568 if (instance_ptr == error_mark_node)
2570 if (TREE_CODE (function) == PTRMEM_CST)
2572 /* Extracting the function address from a pmf is only
2573 allowed with -Wno-pmf-conversions. It only works for
2574 pmf constants. */
2575 e1 = build_addr_func (PTRMEM_CST_MEMBER (function));
2576 e1 = convert (fntype, e1);
2577 return e1;
2579 else
2581 error ("object missing in use of `%E'", function);
2582 return error_mark_node;
2586 if (TREE_SIDE_EFFECTS (instance_ptr))
2587 instance_ptr = instance_save_expr = save_expr (instance_ptr);
2589 if (TREE_SIDE_EFFECTS (function))
2590 function = save_expr (function);
2592 /* Start by extracting all the information from the PMF itself. */
2593 e3 = PFN_FROM_PTRMEMFUNC (function);
2594 delta = build_ptrmemfunc_access_expr (function, delta_identifier);
2595 idx = build1 (NOP_EXPR, vtable_index_type, e3);
2596 switch (TARGET_PTRMEMFUNC_VBIT_LOCATION)
2598 case ptrmemfunc_vbit_in_pfn:
2599 e1 = cp_build_binary_op (BIT_AND_EXPR, idx, integer_one_node);
2600 idx = cp_build_binary_op (MINUS_EXPR, idx, integer_one_node);
2601 break;
2603 case ptrmemfunc_vbit_in_delta:
2604 e1 = cp_build_binary_op (BIT_AND_EXPR, delta, integer_one_node);
2605 delta = cp_build_binary_op (RSHIFT_EXPR, delta, integer_one_node);
2606 break;
2608 default:
2609 abort ();
2612 /* Convert down to the right base before using the instance. First
2613 use the type... */
2614 basetype = TYPE_METHOD_BASETYPE (TREE_TYPE (fntype));
2615 basetype = lookup_base (TREE_TYPE (TREE_TYPE (instance_ptr)),
2616 basetype, ba_check, NULL);
2617 instance_ptr = build_base_path (PLUS_EXPR, instance_ptr, basetype, 1);
2618 if (instance_ptr == error_mark_node)
2619 return error_mark_node;
2620 /* ...and then the delta in the PMF. */
2621 instance_ptr = build (PLUS_EXPR, TREE_TYPE (instance_ptr),
2622 instance_ptr, delta);
2624 /* Hand back the adjusted 'this' argument to our caller. */
2625 *instance_ptrptr = instance_ptr;
2627 /* Next extract the vtable pointer from the object. */
2628 vtbl = build1 (NOP_EXPR, build_pointer_type (vtbl_ptr_type_node),
2629 instance_ptr);
2630 vtbl = build_indirect_ref (vtbl, NULL);
2632 /* Finally, extract the function pointer from the vtable. */
2633 e2 = fold (build (PLUS_EXPR, TREE_TYPE (vtbl), vtbl, idx));
2634 e2 = build_indirect_ref (e2, NULL);
2635 TREE_CONSTANT (e2) = 1;
2637 /* When using function descriptors, the address of the
2638 vtable entry is treated as a function pointer. */
2639 if (TARGET_VTABLE_USES_DESCRIPTORS)
2640 e2 = build1 (NOP_EXPR, TREE_TYPE (e2),
2641 build_unary_op (ADDR_EXPR, e2, /*noconvert=*/1));
2643 TREE_TYPE (e2) = TREE_TYPE (e3);
2644 e1 = build_conditional_expr (e1, e2, e3);
2646 /* Make sure this doesn't get evaluated first inside one of the
2647 branches of the COND_EXPR. */
2648 if (instance_save_expr)
2649 e1 = build (COMPOUND_EXPR, TREE_TYPE (e1),
2650 instance_save_expr, e1);
2652 function = e1;
2654 return function;
2657 tree
2658 build_function_call_real (function, params, require_complete, flags)
2659 tree function, params;
2660 int require_complete, flags;
2662 register tree fntype, fndecl;
2663 register tree value_type;
2664 register tree coerced_params;
2665 tree result;
2666 tree name = NULL_TREE, assembler_name = NULL_TREE;
2667 int is_method;
2668 tree original = function;
2670 /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
2671 Strip such NOP_EXPRs, since FUNCTION is used in non-lvalue context. */
2672 if (TREE_CODE (function) == NOP_EXPR
2673 && TREE_TYPE (function) == TREE_TYPE (TREE_OPERAND (function, 0)))
2674 function = TREE_OPERAND (function, 0);
2676 if (TREE_CODE (function) == FUNCTION_DECL)
2678 name = DECL_NAME (function);
2679 assembler_name = DECL_ASSEMBLER_NAME (function);
2681 mark_used (function);
2682 fndecl = function;
2684 /* Convert anything with function type to a pointer-to-function. */
2685 if (pedantic && DECL_MAIN_P (function))
2686 pedwarn ("ISO C++ forbids calling `::main' from within program");
2688 /* Differs from default_conversion by not setting TREE_ADDRESSABLE
2689 (because calling an inline function does not mean the function
2690 needs to be separately compiled). */
2692 if (DECL_INLINE (function))
2693 function = inline_conversion (function);
2694 else
2695 function = build_addr_func (function);
2697 else
2699 fndecl = NULL_TREE;
2701 function = build_addr_func (function);
2704 if (function == error_mark_node)
2705 return error_mark_node;
2707 fntype = TREE_TYPE (function);
2709 if (TYPE_PTRMEMFUNC_P (fntype))
2711 error ("must use .* or ->* to call pointer-to-member function in `%E (...)'",
2712 original);
2713 return error_mark_node;
2716 is_method = (TREE_CODE (fntype) == POINTER_TYPE
2717 && TREE_CODE (TREE_TYPE (fntype)) == METHOD_TYPE);
2719 if (!((TREE_CODE (fntype) == POINTER_TYPE
2720 && TREE_CODE (TREE_TYPE (fntype)) == FUNCTION_TYPE)
2721 || is_method
2722 || TREE_CODE (function) == TEMPLATE_ID_EXPR))
2724 error ("`%E' cannot be used as a function", original);
2725 return error_mark_node;
2728 /* fntype now gets the type of function pointed to. */
2729 fntype = TREE_TYPE (fntype);
2731 /* Convert the parameters to the types declared in the
2732 function prototype, or apply default promotions. */
2734 if (flags & LOOKUP_COMPLAIN)
2735 coerced_params = convert_arguments (TYPE_ARG_TYPES (fntype),
2736 params, fndecl, LOOKUP_NORMAL);
2737 else
2738 coerced_params = convert_arguments (TYPE_ARG_TYPES (fntype),
2739 params, fndecl, 0);
2741 if (coerced_params == error_mark_node)
2743 if (flags & LOOKUP_SPECULATIVELY)
2744 return NULL_TREE;
2745 else
2746 return error_mark_node;
2749 /* Check for errors in format strings. */
2751 if (warn_format)
2752 check_function_format (NULL, TYPE_ATTRIBUTES (fntype), coerced_params);
2754 /* Recognize certain built-in functions so we can make tree-codes
2755 other than CALL_EXPR. We do this when it enables fold-const.c
2756 to do something useful. */
2758 if (TREE_CODE (function) == ADDR_EXPR
2759 && TREE_CODE (TREE_OPERAND (function, 0)) == FUNCTION_DECL
2760 && DECL_BUILT_IN (TREE_OPERAND (function, 0)))
2762 result = expand_tree_builtin (TREE_OPERAND (function, 0),
2763 params, coerced_params);
2764 if (result)
2765 return result;
2768 /* Some built-in function calls will be evaluated at
2769 compile-time in fold (). */
2770 result = fold (build_call (function, coerced_params));
2771 value_type = TREE_TYPE (result);
2773 if (require_complete)
2775 if (TREE_CODE (value_type) == VOID_TYPE)
2776 return result;
2777 result = require_complete_type (result);
2779 if (IS_AGGR_TYPE (value_type))
2780 result = build_cplus_new (value_type, result);
2781 return convert_from_reference (result);
2784 tree
2785 build_function_call (function, params)
2786 tree function, params;
2788 return build_function_call_real (function, params, 1, LOOKUP_NORMAL);
2791 /* Convert the actual parameter expressions in the list VALUES
2792 to the types in the list TYPELIST.
2793 If parmdecls is exhausted, or when an element has NULL as its type,
2794 perform the default conversions.
2796 NAME is an IDENTIFIER_NODE or 0. It is used only for error messages.
2798 This is also where warnings about wrong number of args are generated.
2800 Return a list of expressions for the parameters as converted.
2802 Both VALUES and the returned value are chains of TREE_LIST nodes
2803 with the elements of the list in the TREE_VALUE slots of those nodes.
2805 In C++, unspecified trailing parameters can be filled in with their
2806 default arguments, if such were specified. Do so here. */
2808 tree
2809 convert_arguments (typelist, values, fndecl, flags)
2810 tree typelist, values, fndecl;
2811 int flags;
2813 register tree typetail, valtail;
2814 register tree result = NULL_TREE;
2815 const char *called_thing = 0;
2816 int i = 0;
2818 /* Argument passing is always copy-initialization. */
2819 flags |= LOOKUP_ONLYCONVERTING;
2821 if (fndecl)
2823 if (TREE_CODE (TREE_TYPE (fndecl)) == METHOD_TYPE)
2825 if (DECL_NAME (fndecl) == NULL_TREE
2826 || IDENTIFIER_HAS_TYPE_VALUE (DECL_NAME (fndecl)))
2827 called_thing = "constructor";
2828 else
2829 called_thing = "member function";
2831 else
2832 called_thing = "function";
2835 for (valtail = values, typetail = typelist;
2836 valtail;
2837 valtail = TREE_CHAIN (valtail), i++)
2839 register tree type = typetail ? TREE_VALUE (typetail) : 0;
2840 register tree val = TREE_VALUE (valtail);
2842 if (val == error_mark_node)
2843 return error_mark_node;
2845 if (type == void_type_node)
2847 if (fndecl)
2849 cp_error_at ("too many arguments to %s `%+#D'", called_thing,
2850 fndecl);
2851 error ("at this point in file");
2853 else
2854 error ("too many arguments to function");
2855 /* In case anybody wants to know if this argument
2856 list is valid. */
2857 if (result)
2858 TREE_TYPE (tree_last (result)) = error_mark_node;
2859 break;
2862 if (TREE_CODE (val) == OFFSET_REF)
2863 val = resolve_offset_ref (val);
2865 /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
2866 Strip such NOP_EXPRs, since VAL is used in non-lvalue context. */
2867 if (TREE_CODE (val) == NOP_EXPR
2868 && TREE_TYPE (val) == TREE_TYPE (TREE_OPERAND (val, 0))
2869 && (type == 0 || TREE_CODE (type) != REFERENCE_TYPE))
2870 val = TREE_OPERAND (val, 0);
2872 if (type == 0 || TREE_CODE (type) != REFERENCE_TYPE)
2874 if (TREE_CODE (TREE_TYPE (val)) == ARRAY_TYPE
2875 || TREE_CODE (TREE_TYPE (val)) == FUNCTION_TYPE
2876 || TREE_CODE (TREE_TYPE (val)) == METHOD_TYPE)
2877 val = default_conversion (val);
2880 if (val == error_mark_node)
2881 return error_mark_node;
2883 if (type != 0)
2885 /* Formal parm type is specified by a function prototype. */
2886 tree parmval;
2888 if (!COMPLETE_TYPE_P (complete_type (type)))
2890 error ("parameter type of called function is incomplete");
2891 parmval = val;
2893 else
2895 parmval = convert_for_initialization
2896 (NULL_TREE, type, val, flags,
2897 "argument passing", fndecl, i);
2898 parmval = convert_for_arg_passing (type, parmval);
2901 if (parmval == error_mark_node)
2902 return error_mark_node;
2904 result = tree_cons (NULL_TREE, parmval, result);
2906 else
2908 if (TREE_CODE (TREE_TYPE (val)) == REFERENCE_TYPE)
2909 val = convert_from_reference (val);
2911 if (fndecl && DECL_BUILT_IN (fndecl)
2912 && DECL_FUNCTION_CODE (fndecl) == BUILT_IN_CONSTANT_P)
2913 /* Don't do ellipsis conversion for __built_in_constant_p
2914 as this will result in spurious warnings for non-POD
2915 types. */
2916 val = require_complete_type (val);
2917 else
2918 val = convert_arg_to_ellipsis (val);
2920 result = tree_cons (NULL_TREE, val, result);
2923 if (typetail)
2924 typetail = TREE_CHAIN (typetail);
2927 if (typetail != 0 && typetail != void_list_node)
2929 /* See if there are default arguments that can be used */
2930 if (TREE_PURPOSE (typetail)
2931 && TREE_CODE (TREE_PURPOSE (typetail)) != DEFAULT_ARG)
2933 for (; typetail != void_list_node; ++i)
2935 tree parmval
2936 = convert_default_arg (TREE_VALUE (typetail),
2937 TREE_PURPOSE (typetail),
2938 fndecl, i);
2940 if (parmval == error_mark_node)
2941 return error_mark_node;
2943 result = tree_cons (0, parmval, result);
2944 typetail = TREE_CHAIN (typetail);
2945 /* ends with `...'. */
2946 if (typetail == NULL_TREE)
2947 break;
2950 else
2952 if (fndecl)
2954 cp_error_at ("too few arguments to %s `%+#D'",
2955 called_thing, fndecl);
2956 error ("at this point in file");
2958 else
2959 error ("too few arguments to function");
2960 return error_mark_list;
2964 return nreverse (result);
2967 /* Build a binary-operation expression, after performing default
2968 conversions on the operands. CODE is the kind of expression to build. */
2970 tree
2971 build_x_binary_op (code, arg1, arg2)
2972 enum tree_code code;
2973 tree arg1, arg2;
2975 if (processing_template_decl)
2976 return build_min_nt (code, arg1, arg2);
2978 return build_new_op (code, LOOKUP_NORMAL, arg1, arg2, NULL_TREE);
2981 #if 0
2983 tree
2984 build_template_expr (enum tree_code code, tree op0, tree op1, tree op2)
2986 tree type;
2988 /* If any of the operands is erroneous the result is erroneous too. */
2989 if (error_operand_p (op0)
2990 || (op1 && error_operand_p (op1))
2991 || (op2 && error_operand_p (op2)))
2992 return error_mark_node;
2994 if (dependent_type_p (TREE_TYPE (op0))
2995 || (op1 && dependent_type_p (TREE_TYPE (op1)))
2996 || (op2 && dependent_type_p (TREE_TYPE (op2))))
2997 /* If at least one operand has a dependent type, we cannot
2998 determine the type of the expression until instantiation time. */
2999 type = NULL_TREE;
3000 else
3002 struct z_candidate *cand;
3003 tree op0_type;
3004 tree op1_type;
3005 tree op2_type;
3007 /* None of the operands is dependent, so we can compute the type
3008 of the expression at this point. We must compute the type so
3009 that in things like:
3011 template <int I>
3012 void f() { S<sizeof(I + 3)> s; ... }
3014 we can tell that the type of "s" is non-dependent.
3016 If we're processing a template argument, we do not want to
3017 actually change the operands in any way. Adding conversions,
3018 performing constant folding, etc., would all change mangled
3019 names. For example, in:
3021 template <int I>
3022 void f(S<sizeof(3 + 4 + I)>);
3024 we need to determine that "3 + 4 + I" has type "int", without
3025 actually turning the expression into "7 + I". */
3026 cand = find_overloaded_op (code, op0, op1, op2);
3027 if (cand)
3028 /* If an overloaded operator was found, the expression will
3029 have the type returned by the function. */
3030 type = non_reference (TREE_TYPE (cand->fn));
3031 else
3033 /* There is no overloaded operator so we can just use the
3034 default rules for determining the type of the operand. */
3035 op0_type = TREE_TYPE (op0);
3036 op1_type = op1 ? TREE_TYPE (op1) : NULL_TREE;
3037 op2_type = op2 ? TREE_TYPE (op2) : NULL_TREE;
3038 type = NULL_TREE;
3040 switch (code)
3042 case MODIFY_EXPR:
3043 /* [expr.ass]
3045 The result of the assignment operation is the value
3046 stored in the left operand. */
3047 type = op0_type;
3048 break;
3049 case COMPONENT_REF:
3050 /* Implement this case. */
3051 break;
3052 case POSTINCREMENT_EXPR:
3053 case POSTDECREMENT_EXPR:
3054 /* [expr.post.incr]
3056 The type of the result is the cv-unqualified version
3057 of the type of the operand. */
3058 type = TYPE_MAIN_VARIANT (op0_type);
3059 break;
3060 case PREINCREMENT_EXPR:
3061 case PREDECREMENT_EXPR:
3062 /* [expr.pre.incr]
3064 The value is the new value of the operand. */
3065 type = op0_type;
3066 break;
3067 case INDIRECT_REF:
3068 /* [expr.unary.op]
3070 If the type of the expression is "pointer to T", the
3071 type of the result is "T". */
3072 type = TREE_TYPE (op0_type);
3073 break;
3074 case ADDR_EXPR:
3075 /* [expr.unary.op]
3077 If the type of the expression is "T", the type of the
3078 result is "pointer to T". */
3079 /* FIXME: Handle the pointer-to-member case. */
3080 break;
3081 case MEMBER_REF:
3082 /* FIXME: Implement this case. */
3083 break;
3084 case LSHIFT_EXPR:
3085 case RSHIFT_EXPR:
3086 /* [expr.shift]
3088 The type of the result is that of the promoted left
3089 operand. */
3090 break;
3091 case PLUS_EXPR:
3092 case MINUS_EXPR:
3093 /* FIXME: Be careful of special pointer-arithmetic
3094 cases. */
3095 /* Fall through. */
3096 case MAX_EXPR:
3097 case MIN_EXPR:
3098 /* These are GNU extensions; the result type is computed
3099 as it would be for other arithmetic operators. */
3100 /* Fall through. */
3101 case BIT_AND_EXPR:
3102 case BIT_XOR_EXPR:
3103 case BIT_IOR_EXPR:
3104 case MULT_EXPR:
3105 case TRUNC_DIV_EXPR:
3106 case TRUNC_MOD_EXPR:
3107 /* [expr.bit.and], [expr.xor], [expr.or], [expr.mul]
3109 The usual arithmetic conversions are performed on the
3110 operands and determine the type of the result. */
3111 /* FIXME: Check that this is possible. */
3112 type = type_after_usual_arithmetic_conversions (t1, t2);
3113 break;
3114 case GT_EXPR:
3115 case LT_EXPR:
3116 case GE_EXPR:
3117 case LE_EXPR:
3118 case EQ_EXPR:
3119 case NE_EXPR:
3120 /* [expr.rel]
3122 The type of the result is bool. */
3123 type = boolean_type_node;
3124 break;
3125 case TRUTH_ANDIF_EXPR:
3126 case TRUTH_ORIF_EXPR:
3127 /* [expr.log.and], [expr.log.org]
3129 The result is a bool. */
3130 type = boolean_type_node;
3131 break;
3132 case COND_EXPR:
3133 /* FIXME: Handle special rules for conditioanl
3134 expressions. */
3135 break;
3136 case COMPOUND_EXPR:
3137 type = op1_type;
3138 break;
3139 default:
3140 abort ();
3142 /* If the type of the expression could not be determined,
3143 something is wrong. */
3144 if (!type)
3145 abort ();
3146 /* If the type is erroneous, the expression is erroneous
3147 too. */
3148 if (type == error_mark_node)
3149 return error_mark_node;
3153 return build_min (code, type, op0, op1, op2, NULL_TREE);
3156 #endif
3158 /* Build a binary-operation expression without default conversions.
3159 CODE is the kind of expression to build.
3160 This function differs from `build' in several ways:
3161 the data type of the result is computed and recorded in it,
3162 warnings are generated if arg data types are invalid,
3163 special handling for addition and subtraction of pointers is known,
3164 and some optimization is done (operations on narrow ints
3165 are done in the narrower type when that gives the same result).
3166 Constant folding is also done before the result is returned.
3168 Note that the operands will never have enumeral types
3169 because either they have just had the default conversions performed
3170 or they have both just been converted to some other type in which
3171 the arithmetic is to be done.
3173 C++: must do special pointer arithmetic when implementing
3174 multiple inheritance, and deal with pointer to member functions. */
3176 tree
3177 build_binary_op (code, orig_op0, orig_op1, convert_p)
3178 enum tree_code code;
3179 tree orig_op0, orig_op1;
3180 int convert_p ATTRIBUTE_UNUSED;
3182 tree op0, op1;
3183 register enum tree_code code0, code1;
3184 tree type0, type1;
3186 /* Expression code to give to the expression when it is built.
3187 Normally this is CODE, which is what the caller asked for,
3188 but in some special cases we change it. */
3189 register enum tree_code resultcode = code;
3191 /* Data type in which the computation is to be performed.
3192 In the simplest cases this is the common type of the arguments. */
3193 register tree result_type = NULL;
3195 /* Nonzero means operands have already been type-converted
3196 in whatever way is necessary.
3197 Zero means they need to be converted to RESULT_TYPE. */
3198 int converted = 0;
3200 /* Nonzero means create the expression with this type, rather than
3201 RESULT_TYPE. */
3202 tree build_type = 0;
3204 /* Nonzero means after finally constructing the expression
3205 convert it to this type. */
3206 tree final_type = 0;
3208 /* Nonzero if this is an operation like MIN or MAX which can
3209 safely be computed in short if both args are promoted shorts.
3210 Also implies COMMON.
3211 -1 indicates a bitwise operation; this makes a difference
3212 in the exact conditions for when it is safe to do the operation
3213 in a narrower mode. */
3214 int shorten = 0;
3216 /* Nonzero if this is a comparison operation;
3217 if both args are promoted shorts, compare the original shorts.
3218 Also implies COMMON. */
3219 int short_compare = 0;
3221 /* Nonzero if this is a right-shift operation, which can be computed on the
3222 original short and then promoted if the operand is a promoted short. */
3223 int short_shift = 0;
3225 /* Nonzero means set RESULT_TYPE to the common type of the args. */
3226 int common = 0;
3228 /* Apply default conversions. */
3229 op0 = orig_op0;
3230 op1 = orig_op1;
3232 if (code == TRUTH_AND_EXPR || code == TRUTH_ANDIF_EXPR
3233 || code == TRUTH_OR_EXPR || code == TRUTH_ORIF_EXPR
3234 || code == TRUTH_XOR_EXPR)
3236 if (!really_overloaded_fn (op0))
3237 op0 = decay_conversion (op0);
3238 if (!really_overloaded_fn (op1))
3239 op1 = decay_conversion (op1);
3241 else
3243 if (!really_overloaded_fn (op0))
3244 op0 = default_conversion (op0);
3245 if (!really_overloaded_fn (op1))
3246 op1 = default_conversion (op1);
3249 /* Strip NON_LVALUE_EXPRs, etc., since we aren't using as an lvalue. */
3250 STRIP_TYPE_NOPS (op0);
3251 STRIP_TYPE_NOPS (op1);
3253 /* DTRT if one side is an overloaded function, but complain about it. */
3254 if (type_unknown_p (op0))
3256 tree t = instantiate_type (TREE_TYPE (op1), op0, tf_none);
3257 if (t != error_mark_node)
3259 pedwarn ("assuming cast to type `%T' from overloaded function",
3260 TREE_TYPE (t));
3261 op0 = t;
3264 if (type_unknown_p (op1))
3266 tree t = instantiate_type (TREE_TYPE (op0), op1, tf_none);
3267 if (t != error_mark_node)
3269 pedwarn ("assuming cast to type `%T' from overloaded function",
3270 TREE_TYPE (t));
3271 op1 = t;
3275 type0 = TREE_TYPE (op0);
3276 type1 = TREE_TYPE (op1);
3278 /* The expression codes of the data types of the arguments tell us
3279 whether the arguments are integers, floating, pointers, etc. */
3280 code0 = TREE_CODE (type0);
3281 code1 = TREE_CODE (type1);
3283 /* If an error was already reported for one of the arguments,
3284 avoid reporting another error. */
3286 if (code0 == ERROR_MARK || code1 == ERROR_MARK)
3287 return error_mark_node;
3289 switch (code)
3291 case PLUS_EXPR:
3292 /* Handle the pointer + int case. */
3293 if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
3294 return cp_pointer_int_sum (PLUS_EXPR, op0, op1);
3295 else if (code1 == POINTER_TYPE && code0 == INTEGER_TYPE)
3296 return cp_pointer_int_sum (PLUS_EXPR, op1, op0);
3297 else
3298 common = 1;
3299 break;
3301 case MINUS_EXPR:
3302 /* Subtraction of two similar pointers.
3303 We must subtract them as integers, then divide by object size. */
3304 if (code0 == POINTER_TYPE && code1 == POINTER_TYPE
3305 && comp_target_types (type0, type1, 1))
3306 return pointer_diff (op0, op1, common_type (type0, type1));
3307 /* Handle pointer minus int. Just like pointer plus int. */
3308 else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
3309 return cp_pointer_int_sum (MINUS_EXPR, op0, op1);
3310 else
3311 common = 1;
3312 break;
3314 case MULT_EXPR:
3315 common = 1;
3316 break;
3318 case TRUNC_DIV_EXPR:
3319 case CEIL_DIV_EXPR:
3320 case FLOOR_DIV_EXPR:
3321 case ROUND_DIV_EXPR:
3322 case EXACT_DIV_EXPR:
3323 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE
3324 || code0 == COMPLEX_TYPE)
3325 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
3326 || code1 == COMPLEX_TYPE))
3328 if (TREE_CODE (op1) == INTEGER_CST && integer_zerop (op1))
3329 warning ("division by zero in `%E / 0'", op0);
3330 else if (TREE_CODE (op1) == REAL_CST && real_zerop (op1))
3331 warning ("division by zero in `%E / 0.'", op0);
3333 if (!(code0 == INTEGER_TYPE && code1 == INTEGER_TYPE))
3334 resultcode = RDIV_EXPR;
3335 else
3336 /* When dividing two signed integers, we have to promote to int.
3337 unless we divide by a constant != -1. Note that default
3338 conversion will have been performed on the operands at this
3339 point, so we have to dig out the original type to find out if
3340 it was unsigned. */
3341 shorten = ((TREE_CODE (op0) == NOP_EXPR
3342 && TREE_UNSIGNED (TREE_TYPE (TREE_OPERAND (op0, 0))))
3343 || (TREE_CODE (op1) == INTEGER_CST
3344 && ! integer_all_onesp (op1)));
3346 common = 1;
3348 break;
3350 case BIT_AND_EXPR:
3351 case BIT_ANDTC_EXPR:
3352 case BIT_IOR_EXPR:
3353 case BIT_XOR_EXPR:
3354 if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
3355 shorten = -1;
3356 break;
3358 case TRUNC_MOD_EXPR:
3359 case FLOOR_MOD_EXPR:
3360 if (code1 == INTEGER_TYPE && integer_zerop (op1))
3361 warning ("division by zero in `%E %% 0'", op0);
3362 else if (code1 == REAL_TYPE && real_zerop (op1))
3363 warning ("division by zero in `%E %% 0.'", op0);
3365 if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
3367 /* Although it would be tempting to shorten always here, that loses
3368 on some targets, since the modulo instruction is undefined if the
3369 quotient can't be represented in the computation mode. We shorten
3370 only if unsigned or if dividing by something we know != -1. */
3371 shorten = ((TREE_CODE (op0) == NOP_EXPR
3372 && TREE_UNSIGNED (TREE_TYPE (TREE_OPERAND (op0, 0))))
3373 || (TREE_CODE (op1) == INTEGER_CST
3374 && ! integer_all_onesp (op1)));
3375 common = 1;
3377 break;
3379 case TRUTH_ANDIF_EXPR:
3380 case TRUTH_ORIF_EXPR:
3381 case TRUTH_AND_EXPR:
3382 case TRUTH_OR_EXPR:
3383 result_type = boolean_type_node;
3384 break;
3386 /* Shift operations: result has same type as first operand;
3387 always convert second operand to int.
3388 Also set SHORT_SHIFT if shifting rightward. */
3390 case RSHIFT_EXPR:
3391 if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
3393 result_type = type0;
3394 if (TREE_CODE (op1) == INTEGER_CST)
3396 if (tree_int_cst_lt (op1, integer_zero_node))
3397 warning ("right shift count is negative");
3398 else
3400 if (! integer_zerop (op1))
3401 short_shift = 1;
3402 if (compare_tree_int (op1, TYPE_PRECISION (type0)) >= 0)
3403 warning ("right shift count >= width of type");
3406 /* Convert the shift-count to an integer, regardless of
3407 size of value being shifted. */
3408 if (TYPE_MAIN_VARIANT (TREE_TYPE (op1)) != integer_type_node)
3409 op1 = cp_convert (integer_type_node, op1);
3410 /* Avoid converting op1 to result_type later. */
3411 converted = 1;
3413 break;
3415 case LSHIFT_EXPR:
3416 if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
3418 result_type = type0;
3419 if (TREE_CODE (op1) == INTEGER_CST)
3421 if (tree_int_cst_lt (op1, integer_zero_node))
3422 warning ("left shift count is negative");
3423 else if (compare_tree_int (op1, TYPE_PRECISION (type0)) >= 0)
3424 warning ("left shift count >= width of type");
3426 /* Convert the shift-count to an integer, regardless of
3427 size of value being shifted. */
3428 if (TYPE_MAIN_VARIANT (TREE_TYPE (op1)) != integer_type_node)
3429 op1 = cp_convert (integer_type_node, op1);
3430 /* Avoid converting op1 to result_type later. */
3431 converted = 1;
3433 break;
3435 case RROTATE_EXPR:
3436 case LROTATE_EXPR:
3437 if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
3439 result_type = type0;
3440 if (TREE_CODE (op1) == INTEGER_CST)
3442 if (tree_int_cst_lt (op1, integer_zero_node))
3443 warning ("%s rotate count is negative",
3444 (code == LROTATE_EXPR) ? "left" : "right");
3445 else if (compare_tree_int (op1, TYPE_PRECISION (type0)) >= 0)
3446 warning ("%s rotate count >= width of type",
3447 (code == LROTATE_EXPR) ? "left" : "right");
3449 /* Convert the shift-count to an integer, regardless of
3450 size of value being shifted. */
3451 if (TYPE_MAIN_VARIANT (TREE_TYPE (op1)) != integer_type_node)
3452 op1 = cp_convert (integer_type_node, op1);
3454 break;
3456 case EQ_EXPR:
3457 case NE_EXPR:
3458 if (warn_float_equal && (code0 == REAL_TYPE || code1 == REAL_TYPE))
3459 warning ("comparing floating point with == or != is unsafe");
3461 build_type = boolean_type_node;
3462 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE
3463 || code0 == COMPLEX_TYPE)
3464 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
3465 || code1 == COMPLEX_TYPE))
3466 short_compare = 1;
3467 else if (code0 == POINTER_TYPE && code1 == POINTER_TYPE)
3468 result_type = composite_pointer_type (type0, type1, op0, op1,
3469 "comparison");
3470 else if (code0 == POINTER_TYPE && null_ptr_cst_p (op1))
3471 result_type = type0;
3472 else if (code1 == POINTER_TYPE && null_ptr_cst_p (op0))
3473 result_type = type1;
3474 else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
3476 result_type = type0;
3477 error ("ISO C++ forbids comparison between pointer and integer");
3479 else if (code0 == INTEGER_TYPE && code1 == POINTER_TYPE)
3481 result_type = type1;
3482 error ("ISO C++ forbids comparison between pointer and integer");
3484 else if (TYPE_PTRMEMFUNC_P (type0) && null_ptr_cst_p (op1))
3486 op0 = build_ptrmemfunc_access_expr (op0, pfn_identifier);
3487 op1 = cp_convert (TREE_TYPE (op0), integer_zero_node);
3488 result_type = TREE_TYPE (op0);
3490 else if (TYPE_PTRMEMFUNC_P (type1) && null_ptr_cst_p (op0))
3491 return cp_build_binary_op (code, op1, op0);
3492 else if (TYPE_PTRMEMFUNC_P (type0) && TYPE_PTRMEMFUNC_P (type1)
3493 && same_type_p (type0, type1))
3495 /* E will be the final comparison. */
3496 tree e;
3497 /* E1 and E2 are for scratch. */
3498 tree e1;
3499 tree e2;
3500 tree pfn0;
3501 tree pfn1;
3502 tree delta0;
3503 tree delta1;
3505 if (TREE_SIDE_EFFECTS (op0))
3506 op0 = save_expr (op0);
3507 if (TREE_SIDE_EFFECTS (op1))
3508 op1 = save_expr (op1);
3510 /* We generate:
3512 (op0.pfn == op1.pfn
3513 && (!op0.pfn || op0.delta == op1.delta))
3515 The reason for the `!op0.pfn' bit is that a NULL
3516 pointer-to-member is any member with a zero PFN; the
3517 DELTA field is unspecified. */
3518 pfn0 = pfn_from_ptrmemfunc (op0);
3519 pfn1 = pfn_from_ptrmemfunc (op1);
3520 delta0 = build_ptrmemfunc_access_expr (op0,
3521 delta_identifier);
3522 delta1 = build_ptrmemfunc_access_expr (op1,
3523 delta_identifier);
3524 e1 = cp_build_binary_op (EQ_EXPR, delta0, delta1);
3525 e2 = cp_build_binary_op (EQ_EXPR,
3526 pfn0,
3527 cp_convert (TREE_TYPE (pfn0),
3528 integer_zero_node));
3529 e1 = cp_build_binary_op (TRUTH_ORIF_EXPR, e1, e2);
3530 e2 = build (EQ_EXPR, boolean_type_node, pfn0, pfn1);
3531 e = cp_build_binary_op (TRUTH_ANDIF_EXPR, e2, e1);
3532 if (code == EQ_EXPR)
3533 return e;
3534 return cp_build_binary_op (EQ_EXPR, e, integer_zero_node);
3536 else if ((TYPE_PTRMEMFUNC_P (type0)
3537 && same_type_p (TYPE_PTRMEMFUNC_FN_TYPE (type0), type1))
3538 || (TYPE_PTRMEMFUNC_P (type1)
3539 && same_type_p (TYPE_PTRMEMFUNC_FN_TYPE (type1), type0)))
3540 abort ();
3541 break;
3543 case MAX_EXPR:
3544 case MIN_EXPR:
3545 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE)
3546 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE))
3547 shorten = 1;
3548 else if (code0 == POINTER_TYPE && code1 == POINTER_TYPE)
3549 result_type = composite_pointer_type (type0, type1, op0, op1,
3550 "comparison");
3551 break;
3553 case LE_EXPR:
3554 case GE_EXPR:
3555 case LT_EXPR:
3556 case GT_EXPR:
3557 build_type = boolean_type_node;
3558 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE)
3559 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE))
3560 short_compare = 1;
3561 else if (code0 == POINTER_TYPE && code1 == POINTER_TYPE)
3562 result_type = composite_pointer_type (type0, type1, op0, op1,
3563 "comparison");
3564 else if (code0 == POINTER_TYPE && TREE_CODE (op1) == INTEGER_CST
3565 && integer_zerop (op1))
3566 result_type = type0;
3567 else if (code1 == POINTER_TYPE && TREE_CODE (op0) == INTEGER_CST
3568 && integer_zerop (op0))
3569 result_type = type1;
3570 else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
3572 result_type = type0;
3573 pedwarn ("ISO C++ forbids comparison between pointer and integer");
3575 else if (code0 == INTEGER_TYPE && code1 == POINTER_TYPE)
3577 result_type = type1;
3578 pedwarn ("ISO C++ forbids comparison between pointer and integer");
3580 break;
3582 case UNORDERED_EXPR:
3583 case ORDERED_EXPR:
3584 case UNLT_EXPR:
3585 case UNLE_EXPR:
3586 case UNGT_EXPR:
3587 case UNGE_EXPR:
3588 case UNEQ_EXPR:
3589 build_type = integer_type_node;
3590 if (code0 != REAL_TYPE || code1 != REAL_TYPE)
3592 error ("unordered comparison on non-floating point argument");
3593 return error_mark_node;
3595 common = 1;
3596 break;
3598 default:
3599 break;
3602 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE || code0 == COMPLEX_TYPE)
3604 (code1 == INTEGER_TYPE || code1 == REAL_TYPE || code1 == COMPLEX_TYPE))
3606 int none_complex = (code0 != COMPLEX_TYPE && code1 != COMPLEX_TYPE);
3608 if (shorten || common || short_compare)
3609 result_type = common_type (type0, type1);
3611 /* For certain operations (which identify themselves by shorten != 0)
3612 if both args were extended from the same smaller type,
3613 do the arithmetic in that type and then extend.
3615 shorten !=0 and !=1 indicates a bitwise operation.
3616 For them, this optimization is safe only if
3617 both args are zero-extended or both are sign-extended.
3618 Otherwise, we might change the result.
3619 Eg, (short)-1 | (unsigned short)-1 is (int)-1
3620 but calculated in (unsigned short) it would be (unsigned short)-1. */
3622 if (shorten && none_complex)
3624 int unsigned0, unsigned1;
3625 tree arg0 = get_narrower (op0, &unsigned0);
3626 tree arg1 = get_narrower (op1, &unsigned1);
3627 /* UNS is 1 if the operation to be done is an unsigned one. */
3628 int uns = TREE_UNSIGNED (result_type);
3629 tree type;
3631 final_type = result_type;
3633 /* Handle the case that OP0 does not *contain* a conversion
3634 but it *requires* conversion to FINAL_TYPE. */
3636 if (op0 == arg0 && TREE_TYPE (op0) != final_type)
3637 unsigned0 = TREE_UNSIGNED (TREE_TYPE (op0));
3638 if (op1 == arg1 && TREE_TYPE (op1) != final_type)
3639 unsigned1 = TREE_UNSIGNED (TREE_TYPE (op1));
3641 /* Now UNSIGNED0 is 1 if ARG0 zero-extends to FINAL_TYPE. */
3643 /* For bitwise operations, signedness of nominal type
3644 does not matter. Consider only how operands were extended. */
3645 if (shorten == -1)
3646 uns = unsigned0;
3648 /* Note that in all three cases below we refrain from optimizing
3649 an unsigned operation on sign-extended args.
3650 That would not be valid. */
3652 /* Both args variable: if both extended in same way
3653 from same width, do it in that width.
3654 Do it unsigned if args were zero-extended. */
3655 if ((TYPE_PRECISION (TREE_TYPE (arg0))
3656 < TYPE_PRECISION (result_type))
3657 && (TYPE_PRECISION (TREE_TYPE (arg1))
3658 == TYPE_PRECISION (TREE_TYPE (arg0)))
3659 && unsigned0 == unsigned1
3660 && (unsigned0 || !uns))
3661 result_type = c_common_signed_or_unsigned_type
3662 (unsigned0, common_type (TREE_TYPE (arg0), TREE_TYPE (arg1)));
3663 else if (TREE_CODE (arg0) == INTEGER_CST
3664 && (unsigned1 || !uns)
3665 && (TYPE_PRECISION (TREE_TYPE (arg1))
3666 < TYPE_PRECISION (result_type))
3667 && (type = c_common_signed_or_unsigned_type
3668 (unsigned1, TREE_TYPE (arg1)),
3669 int_fits_type_p (arg0, type)))
3670 result_type = type;
3671 else if (TREE_CODE (arg1) == INTEGER_CST
3672 && (unsigned0 || !uns)
3673 && (TYPE_PRECISION (TREE_TYPE (arg0))
3674 < TYPE_PRECISION (result_type))
3675 && (type = c_common_signed_or_unsigned_type
3676 (unsigned0, TREE_TYPE (arg0)),
3677 int_fits_type_p (arg1, type)))
3678 result_type = type;
3681 /* Shifts can be shortened if shifting right. */
3683 if (short_shift)
3685 int unsigned_arg;
3686 tree arg0 = get_narrower (op0, &unsigned_arg);
3688 final_type = result_type;
3690 if (arg0 == op0 && final_type == TREE_TYPE (op0))
3691 unsigned_arg = TREE_UNSIGNED (TREE_TYPE (op0));
3693 if (TYPE_PRECISION (TREE_TYPE (arg0)) < TYPE_PRECISION (result_type)
3694 /* We can shorten only if the shift count is less than the
3695 number of bits in the smaller type size. */
3696 && compare_tree_int (op1, TYPE_PRECISION (TREE_TYPE (arg0))) < 0
3697 /* If arg is sign-extended and then unsigned-shifted,
3698 we can simulate this with a signed shift in arg's type
3699 only if the extended result is at least twice as wide
3700 as the arg. Otherwise, the shift could use up all the
3701 ones made by sign-extension and bring in zeros.
3702 We can't optimize that case at all, but in most machines
3703 it never happens because available widths are 2**N. */
3704 && (!TREE_UNSIGNED (final_type)
3705 || unsigned_arg
3706 || (((unsigned) 2 * TYPE_PRECISION (TREE_TYPE (arg0)))
3707 <= TYPE_PRECISION (result_type))))
3709 /* Do an unsigned shift if the operand was zero-extended. */
3710 result_type
3711 = c_common_signed_or_unsigned_type (unsigned_arg,
3712 TREE_TYPE (arg0));
3713 /* Convert value-to-be-shifted to that type. */
3714 if (TREE_TYPE (op0) != result_type)
3715 op0 = cp_convert (result_type, op0);
3716 converted = 1;
3720 /* Comparison operations are shortened too but differently.
3721 They identify themselves by setting short_compare = 1. */
3723 if (short_compare)
3725 /* Don't write &op0, etc., because that would prevent op0
3726 from being kept in a register.
3727 Instead, make copies of the our local variables and
3728 pass the copies by reference, then copy them back afterward. */
3729 tree xop0 = op0, xop1 = op1, xresult_type = result_type;
3730 enum tree_code xresultcode = resultcode;
3731 tree val
3732 = shorten_compare (&xop0, &xop1, &xresult_type, &xresultcode);
3733 if (val != 0)
3734 return cp_convert (boolean_type_node, val);
3735 op0 = xop0, op1 = xop1;
3736 converted = 1;
3737 resultcode = xresultcode;
3740 if ((short_compare || code == MIN_EXPR || code == MAX_EXPR)
3741 && warn_sign_compare)
3743 int op0_signed = ! TREE_UNSIGNED (TREE_TYPE (orig_op0));
3744 int op1_signed = ! TREE_UNSIGNED (TREE_TYPE (orig_op1));
3746 int unsignedp0, unsignedp1;
3747 tree primop0 = get_narrower (op0, &unsignedp0);
3748 tree primop1 = get_narrower (op1, &unsignedp1);
3750 /* Check for comparison of different enum types. */
3751 if (TREE_CODE (TREE_TYPE (orig_op0)) == ENUMERAL_TYPE
3752 && TREE_CODE (TREE_TYPE (orig_op1)) == ENUMERAL_TYPE
3753 && TYPE_MAIN_VARIANT (TREE_TYPE (orig_op0))
3754 != TYPE_MAIN_VARIANT (TREE_TYPE (orig_op1)))
3756 warning ("comparison between types `%#T' and `%#T'",
3757 TREE_TYPE (orig_op0), TREE_TYPE (orig_op1));
3760 /* Give warnings for comparisons between signed and unsigned
3761 quantities that may fail. */
3762 /* Do the checking based on the original operand trees, so that
3763 casts will be considered, but default promotions won't be. */
3765 /* Do not warn if the comparison is being done in a signed type,
3766 since the signed type will only be chosen if it can represent
3767 all the values of the unsigned type. */
3768 if (! TREE_UNSIGNED (result_type))
3769 /* OK */;
3770 /* Do not warn if both operands are unsigned. */
3771 else if (op0_signed == op1_signed)
3772 /* OK */;
3773 /* Do not warn if the signed quantity is an unsuffixed
3774 integer literal (or some static constant expression
3775 involving such literals or a conditional expression
3776 involving such literals) and it is non-negative. */
3777 else if ((op0_signed && tree_expr_nonnegative_p (orig_op0))
3778 || (op1_signed && tree_expr_nonnegative_p (orig_op1)))
3779 /* OK */;
3780 /* Do not warn if the comparison is an equality operation,
3781 the unsigned quantity is an integral constant and it does
3782 not use the most significant bit of result_type. */
3783 else if ((resultcode == EQ_EXPR || resultcode == NE_EXPR)
3784 && ((op0_signed && TREE_CODE (orig_op1) == INTEGER_CST
3785 && int_fits_type_p (orig_op1, c_common_signed_type
3786 (result_type)))
3787 || (op1_signed && TREE_CODE (orig_op0) == INTEGER_CST
3788 && int_fits_type_p (orig_op0, c_common_signed_type
3789 (result_type)))))
3790 /* OK */;
3791 else
3792 warning ("comparison between signed and unsigned integer expressions");
3794 /* Warn if two unsigned values are being compared in a size
3795 larger than their original size, and one (and only one) is the
3796 result of a `~' operator. This comparison will always fail.
3798 Also warn if one operand is a constant, and the constant does not
3799 have all bits set that are set in the ~ operand when it is
3800 extended. */
3802 if ((TREE_CODE (primop0) == BIT_NOT_EXPR)
3803 ^ (TREE_CODE (primop1) == BIT_NOT_EXPR))
3805 if (TREE_CODE (primop0) == BIT_NOT_EXPR)
3806 primop0 = get_narrower (TREE_OPERAND (op0, 0), &unsignedp0);
3807 if (TREE_CODE (primop1) == BIT_NOT_EXPR)
3808 primop1 = get_narrower (TREE_OPERAND (op1, 0), &unsignedp1);
3810 if (host_integerp (primop0, 0) || host_integerp (primop1, 0))
3812 tree primop;
3813 HOST_WIDE_INT constant, mask;
3814 int unsignedp;
3815 unsigned int bits;
3817 if (host_integerp (primop0, 0))
3819 primop = primop1;
3820 unsignedp = unsignedp1;
3821 constant = tree_low_cst (primop0, 0);
3823 else
3825 primop = primop0;
3826 unsignedp = unsignedp0;
3827 constant = tree_low_cst (primop1, 0);
3830 bits = TYPE_PRECISION (TREE_TYPE (primop));
3831 if (bits < TYPE_PRECISION (result_type)
3832 && bits < HOST_BITS_PER_LONG && unsignedp)
3834 mask = (~ (HOST_WIDE_INT) 0) << bits;
3835 if ((mask & constant) != mask)
3836 warning ("comparison of promoted ~unsigned with constant");
3839 else if (unsignedp0 && unsignedp1
3840 && (TYPE_PRECISION (TREE_TYPE (primop0))
3841 < TYPE_PRECISION (result_type))
3842 && (TYPE_PRECISION (TREE_TYPE (primop1))
3843 < TYPE_PRECISION (result_type)))
3844 warning ("comparison of promoted ~unsigned with unsigned");
3849 /* At this point, RESULT_TYPE must be nonzero to avoid an error message.
3850 If CONVERTED is zero, both args will be converted to type RESULT_TYPE.
3851 Then the expression will be built.
3852 It will be given type FINAL_TYPE if that is nonzero;
3853 otherwise, it will be given type RESULT_TYPE. */
3855 if (!result_type)
3857 error ("invalid operands of types `%T' and `%T' to binary `%O'",
3858 TREE_TYPE (orig_op0), TREE_TYPE (orig_op1), code);
3859 return error_mark_node;
3862 /* Issue warnings about peculiar, but valid, uses of NULL. */
3863 if (/* It's reasonable to use pointer values as operands of &&
3864 and ||, so NULL is no exception. */
3865 !(code == TRUTH_ANDIF_EXPR || code == TRUTH_ORIF_EXPR)
3866 && (/* If OP0 is NULL and OP1 is not a pointer, or vice versa. */
3867 (orig_op0 == null_node
3868 && TREE_CODE (TREE_TYPE (op1)) != POINTER_TYPE)
3869 /* Or vice versa. */
3870 || (orig_op1 == null_node
3871 && TREE_CODE (TREE_TYPE (op0)) != POINTER_TYPE)
3872 /* Or, both are NULL and the operation was not a comparison. */
3873 || (orig_op0 == null_node && orig_op1 == null_node
3874 && code != EQ_EXPR && code != NE_EXPR)))
3875 /* Some sort of arithmetic operation involving NULL was
3876 performed. Note that pointer-difference and pointer-addition
3877 have already been handled above, and so we don't end up here in
3878 that case. */
3879 warning ("NULL used in arithmetic");
3881 if (! converted)
3883 if (TREE_TYPE (op0) != result_type)
3884 op0 = cp_convert (result_type, op0);
3885 if (TREE_TYPE (op1) != result_type)
3886 op1 = cp_convert (result_type, op1);
3888 if (op0 == error_mark_node || op1 == error_mark_node)
3889 return error_mark_node;
3892 if (build_type == NULL_TREE)
3893 build_type = result_type;
3896 register tree result = build (resultcode, build_type, op0, op1);
3897 register tree folded;
3899 folded = fold (result);
3900 if (folded == result)
3901 TREE_CONSTANT (folded) = TREE_CONSTANT (op0) & TREE_CONSTANT (op1);
3902 if (final_type != 0)
3903 return cp_convert (final_type, folded);
3904 return folded;
3908 /* Return a tree for the sum or difference (RESULTCODE says which)
3909 of pointer PTROP and integer INTOP. */
3911 static tree
3912 cp_pointer_int_sum (resultcode, ptrop, intop)
3913 enum tree_code resultcode;
3914 register tree ptrop, intop;
3916 tree res_type = TREE_TYPE (ptrop);
3918 /* pointer_int_sum() uses size_in_bytes() on the TREE_TYPE(res_type)
3919 in certain circumstance (when it's valid to do so). So we need
3920 to make sure it's complete. We don't need to check here, if we
3921 can actually complete it at all, as those checks will be done in
3922 pointer_int_sum() anyway. */
3923 complete_type (TREE_TYPE (res_type));
3925 return pointer_int_sum (resultcode, ptrop, fold (intop));
3928 /* Return a tree for the difference of pointers OP0 and OP1.
3929 The resulting tree has type int. */
3931 static tree
3932 pointer_diff (op0, op1, ptrtype)
3933 register tree op0, op1;
3934 register tree ptrtype;
3936 register tree result, folded;
3937 tree restype = ptrdiff_type_node;
3938 tree target_type = TREE_TYPE (ptrtype);
3940 if (!complete_type_or_else (target_type, NULL_TREE))
3941 return error_mark_node;
3943 if (pedantic || warn_pointer_arith)
3945 if (TREE_CODE (target_type) == VOID_TYPE)
3946 pedwarn ("ISO C++ forbids using pointer of type `void *' in subtraction");
3947 if (TREE_CODE (target_type) == FUNCTION_TYPE)
3948 pedwarn ("ISO C++ forbids using pointer to a function in subtraction");
3949 if (TREE_CODE (target_type) == METHOD_TYPE)
3950 pedwarn ("ISO C++ forbids using pointer to a method in subtraction");
3951 if (TREE_CODE (target_type) == OFFSET_TYPE)
3952 pedwarn ("ISO C++ forbids using pointer to a member in subtraction");
3955 /* First do the subtraction as integers;
3956 then drop through to build the divide operator. */
3958 op0 = cp_build_binary_op (MINUS_EXPR,
3959 cp_convert (restype, op0),
3960 cp_convert (restype, op1));
3962 /* This generates an error if op1 is a pointer to an incomplete type. */
3963 if (!COMPLETE_TYPE_P (TREE_TYPE (TREE_TYPE (op1))))
3964 error ("invalid use of a pointer to an incomplete type in pointer arithmetic");
3966 op1 = ((TREE_CODE (target_type) == VOID_TYPE
3967 || TREE_CODE (target_type) == FUNCTION_TYPE
3968 || TREE_CODE (target_type) == METHOD_TYPE
3969 || TREE_CODE (target_type) == OFFSET_TYPE)
3970 ? integer_one_node
3971 : size_in_bytes (target_type));
3973 /* Do the division. */
3975 result = build (EXACT_DIV_EXPR, restype, op0, cp_convert (restype, op1));
3977 folded = fold (result);
3978 if (folded == result)
3979 TREE_CONSTANT (folded) = TREE_CONSTANT (op0) & TREE_CONSTANT (op1);
3980 return folded;
3983 /* Construct and perhaps optimize a tree representation
3984 for a unary operation. CODE, a tree_code, specifies the operation
3985 and XARG is the operand. */
3987 tree
3988 build_x_unary_op (code, xarg)
3989 enum tree_code code;
3990 tree xarg;
3992 tree exp;
3993 int ptrmem = 0;
3995 if (processing_template_decl)
3996 return build_min_nt (code, xarg, NULL_TREE);
3998 /* & rec, on incomplete RECORD_TYPEs is the simple opr &, not an
3999 error message. */
4000 if (code == ADDR_EXPR
4001 && TREE_CODE (xarg) != TEMPLATE_ID_EXPR
4002 && ((IS_AGGR_TYPE_CODE (TREE_CODE (TREE_TYPE (xarg)))
4003 && !COMPLETE_TYPE_P (TREE_TYPE (xarg)))
4004 || (TREE_CODE (xarg) == OFFSET_REF)))
4005 /* don't look for a function */;
4006 else
4008 tree rval;
4010 rval = build_new_op (code, LOOKUP_NORMAL, xarg,
4011 NULL_TREE, NULL_TREE);
4012 if (rval || code != ADDR_EXPR)
4013 return rval;
4015 if (code == ADDR_EXPR)
4017 /* A pointer to member-function can be formed only by saying
4018 &X::mf. */
4019 if (!flag_ms_extensions && TREE_CODE (TREE_TYPE (xarg)) == METHOD_TYPE
4020 && (TREE_CODE (xarg) != OFFSET_REF || !PTRMEM_OK_P (xarg)))
4022 if (TREE_CODE (xarg) != OFFSET_REF)
4024 error ("invalid use of '%E' to form a pointer-to-member-function. Use a qualified-id.",
4025 xarg);
4026 return error_mark_node;
4028 else
4030 error ("parenthesis around '%E' cannot be used to form a pointer-to-member-function",
4031 xarg);
4032 PTRMEM_OK_P (xarg) = 1;
4036 if (TREE_CODE (xarg) == OFFSET_REF)
4038 ptrmem = PTRMEM_OK_P (xarg);
4040 if (!ptrmem && !flag_ms_extensions
4041 && TREE_CODE (TREE_TYPE (TREE_OPERAND (xarg, 1))) == METHOD_TYPE)
4043 /* A single non-static member, make sure we don't allow a
4044 pointer-to-member. */
4045 xarg = build (OFFSET_REF, TREE_TYPE (xarg),
4046 TREE_OPERAND (xarg, 0),
4047 ovl_cons (TREE_OPERAND (xarg, 1), NULL_TREE));
4048 PTRMEM_OK_P (xarg) = ptrmem;
4052 else if (TREE_CODE (xarg) == TARGET_EXPR)
4053 warning ("taking address of temporary");
4055 exp = build_unary_op (code, xarg, 0);
4056 if (TREE_CODE (exp) == ADDR_EXPR)
4057 PTRMEM_OK_P (exp) = ptrmem;
4059 return exp;
4062 /* Like c_common_truthvalue_conversion, but handle pointer-to-member
4063 constants, where a null value is represented by an INTEGER_CST of
4064 -1. */
4066 tree
4067 cp_truthvalue_conversion (expr)
4068 tree expr;
4070 tree type = TREE_TYPE (expr);
4071 if (TYPE_PTRMEM_P (type))
4072 return build_binary_op (NE_EXPR, expr, integer_zero_node, 1);
4073 else
4074 return c_common_truthvalue_conversion (expr);
4077 /* Just like cp_truthvalue_conversion, but we want a CLEANUP_POINT_EXPR. */
4079 tree
4080 condition_conversion (expr)
4081 tree expr;
4083 tree t;
4084 if (processing_template_decl)
4085 return expr;
4086 if (TREE_CODE (expr) == OFFSET_REF)
4087 expr = resolve_offset_ref (expr);
4088 t = perform_implicit_conversion (boolean_type_node, expr);
4089 t = fold (build1 (CLEANUP_POINT_EXPR, boolean_type_node, t));
4090 return t;
4093 /* C++: Must handle pointers to members.
4095 Perhaps type instantiation should be extended to handle conversion
4096 from aggregates to types we don't yet know we want? (Or are those
4097 cases typically errors which should be reported?)
4099 NOCONVERT nonzero suppresses the default promotions
4100 (such as from short to int). */
4102 tree
4103 build_unary_op (code, xarg, noconvert)
4104 enum tree_code code;
4105 tree xarg;
4106 int noconvert;
4108 /* No default_conversion here. It causes trouble for ADDR_EXPR. */
4109 register tree arg = xarg;
4110 register tree argtype = 0;
4111 const char *errstring = NULL;
4112 tree val;
4114 if (arg == error_mark_node)
4115 return error_mark_node;
4117 switch (code)
4119 case CONVERT_EXPR:
4120 /* This is used for unary plus, because a CONVERT_EXPR
4121 is enough to prevent anybody from looking inside for
4122 associativity, but won't generate any code. */
4123 if (!(arg = build_expr_type_conversion
4124 (WANT_ARITH | WANT_ENUM | WANT_POINTER, arg, true)))
4125 errstring = "wrong type argument to unary plus";
4126 else
4128 if (!noconvert)
4129 arg = default_conversion (arg);
4130 arg = build1 (NON_LVALUE_EXPR, TREE_TYPE (arg), arg);
4131 TREE_CONSTANT (arg) = TREE_CONSTANT (TREE_OPERAND (arg, 0));
4133 break;
4135 case NEGATE_EXPR:
4136 if (!(arg = build_expr_type_conversion (WANT_ARITH | WANT_ENUM, arg, true)))
4137 errstring = "wrong type argument to unary minus";
4138 else if (!noconvert)
4139 arg = default_conversion (arg);
4140 break;
4142 case BIT_NOT_EXPR:
4143 if (TREE_CODE (TREE_TYPE (arg)) == COMPLEX_TYPE)
4145 code = CONJ_EXPR;
4146 if (!noconvert)
4147 arg = default_conversion (arg);
4149 else if (!(arg = build_expr_type_conversion (WANT_INT | WANT_ENUM,
4150 arg, true)))
4151 errstring = "wrong type argument to bit-complement";
4152 else if (!noconvert)
4153 arg = default_conversion (arg);
4154 break;
4156 case ABS_EXPR:
4157 if (!(arg = build_expr_type_conversion (WANT_ARITH | WANT_ENUM, arg, true)))
4158 errstring = "wrong type argument to abs";
4159 else if (!noconvert)
4160 arg = default_conversion (arg);
4161 break;
4163 case CONJ_EXPR:
4164 /* Conjugating a real value is a no-op, but allow it anyway. */
4165 if (!(arg = build_expr_type_conversion (WANT_ARITH | WANT_ENUM, arg, true)))
4166 errstring = "wrong type argument to conjugation";
4167 else if (!noconvert)
4168 arg = default_conversion (arg);
4169 break;
4171 case TRUTH_NOT_EXPR:
4172 arg = cp_convert (boolean_type_node, arg);
4173 val = invert_truthvalue (arg);
4174 if (arg != error_mark_node)
4175 return val;
4176 errstring = "in argument to unary !";
4177 break;
4179 case NOP_EXPR:
4180 break;
4182 case REALPART_EXPR:
4183 if (TREE_CODE (arg) == COMPLEX_CST)
4184 return TREE_REALPART (arg);
4185 else if (TREE_CODE (TREE_TYPE (arg)) == COMPLEX_TYPE)
4186 return fold (build1 (REALPART_EXPR, TREE_TYPE (TREE_TYPE (arg)), arg));
4187 else
4188 return arg;
4190 case IMAGPART_EXPR:
4191 if (TREE_CODE (arg) == COMPLEX_CST)
4192 return TREE_IMAGPART (arg);
4193 else if (TREE_CODE (TREE_TYPE (arg)) == COMPLEX_TYPE)
4194 return fold (build1 (IMAGPART_EXPR, TREE_TYPE (TREE_TYPE (arg)), arg));
4195 else
4196 return cp_convert (TREE_TYPE (arg), integer_zero_node);
4198 case PREINCREMENT_EXPR:
4199 case POSTINCREMENT_EXPR:
4200 case PREDECREMENT_EXPR:
4201 case POSTDECREMENT_EXPR:
4202 /* Handle complex lvalues (when permitted)
4203 by reduction to simpler cases. */
4205 val = unary_complex_lvalue (code, arg);
4206 if (val != 0)
4207 return val;
4209 /* Increment or decrement the real part of the value,
4210 and don't change the imaginary part. */
4211 if (TREE_CODE (TREE_TYPE (arg)) == COMPLEX_TYPE)
4213 tree real, imag;
4215 arg = stabilize_reference (arg);
4216 real = build_unary_op (REALPART_EXPR, arg, 1);
4217 imag = build_unary_op (IMAGPART_EXPR, arg, 1);
4218 return build (COMPLEX_EXPR, TREE_TYPE (arg),
4219 build_unary_op (code, real, 1), imag);
4222 /* Report invalid types. */
4224 if (!(arg = build_expr_type_conversion (WANT_ARITH | WANT_POINTER,
4225 arg, true)))
4227 if (code == PREINCREMENT_EXPR)
4228 errstring ="no pre-increment operator for type";
4229 else if (code == POSTINCREMENT_EXPR)
4230 errstring ="no post-increment operator for type";
4231 else if (code == PREDECREMENT_EXPR)
4232 errstring ="no pre-decrement operator for type";
4233 else
4234 errstring ="no post-decrement operator for type";
4235 break;
4238 /* Report something read-only. */
4240 if (CP_TYPE_CONST_P (TREE_TYPE (arg))
4241 || TREE_READONLY (arg))
4242 readonly_error (arg, ((code == PREINCREMENT_EXPR
4243 || code == POSTINCREMENT_EXPR)
4244 ? "increment" : "decrement"),
4248 register tree inc;
4249 tree result_type = TREE_TYPE (arg);
4251 arg = get_unwidened (arg, 0);
4252 argtype = TREE_TYPE (arg);
4254 /* ARM $5.2.5 last annotation says this should be forbidden. */
4255 if (TREE_CODE (argtype) == ENUMERAL_TYPE)
4256 pedwarn ("ISO C++ forbids %sing an enum",
4257 (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
4258 ? "increment" : "decrement");
4260 /* Compute the increment. */
4262 if (TREE_CODE (argtype) == POINTER_TYPE)
4264 enum tree_code tmp = TREE_CODE (TREE_TYPE (argtype));
4265 tree type = complete_type (TREE_TYPE (argtype));
4267 if (!COMPLETE_OR_VOID_TYPE_P (type))
4268 error ("cannot %s a pointer to incomplete type `%T'",
4269 ((code == PREINCREMENT_EXPR
4270 || code == POSTINCREMENT_EXPR)
4271 ? "increment" : "decrement"), TREE_TYPE (argtype));
4272 else if ((pedantic || warn_pointer_arith)
4273 && (tmp == FUNCTION_TYPE || tmp == METHOD_TYPE
4274 || tmp == VOID_TYPE || tmp == OFFSET_TYPE))
4275 pedwarn ("ISO C++ forbids %sing a pointer of type `%T'",
4276 ((code == PREINCREMENT_EXPR
4277 || code == POSTINCREMENT_EXPR)
4278 ? "increment" : "decrement"), argtype);
4279 inc = cxx_sizeof_nowarn (TREE_TYPE (argtype));
4281 else
4282 inc = integer_one_node;
4284 inc = cp_convert (argtype, inc);
4286 /* Handle incrementing a cast-expression. */
4288 switch (TREE_CODE (arg))
4290 case NOP_EXPR:
4291 case CONVERT_EXPR:
4292 case FLOAT_EXPR:
4293 case FIX_TRUNC_EXPR:
4294 case FIX_FLOOR_EXPR:
4295 case FIX_ROUND_EXPR:
4296 case FIX_CEIL_EXPR:
4298 tree incremented, modify, value, compound;
4299 if (! lvalue_p (arg) && pedantic)
4300 pedwarn ("cast to non-reference type used as lvalue");
4301 arg = stabilize_reference (arg);
4302 if (code == PREINCREMENT_EXPR || code == PREDECREMENT_EXPR)
4303 value = arg;
4304 else
4305 value = save_expr (arg);
4306 incremented = build (((code == PREINCREMENT_EXPR
4307 || code == POSTINCREMENT_EXPR)
4308 ? PLUS_EXPR : MINUS_EXPR),
4309 argtype, value, inc);
4311 modify = build_modify_expr (arg, NOP_EXPR, incremented);
4312 compound = build (COMPOUND_EXPR, TREE_TYPE (arg), modify, value);
4314 /* Eliminate warning about unused result of + or -. */
4315 TREE_NO_UNUSED_WARNING (compound) = 1;
4316 return compound;
4319 default:
4320 break;
4323 /* Complain about anything else that is not a true lvalue. */
4324 if (!lvalue_or_else (arg, ((code == PREINCREMENT_EXPR
4325 || code == POSTINCREMENT_EXPR)
4326 ? "increment" : "decrement")))
4327 return error_mark_node;
4329 /* Forbid using -- on `bool'. */
4330 if (TREE_TYPE (arg) == boolean_type_node)
4332 if (code == POSTDECREMENT_EXPR || code == PREDECREMENT_EXPR)
4334 error ("invalid use of `--' on bool variable `%D'", arg);
4335 return error_mark_node;
4337 #if 0
4338 /* This will only work if someone can convince Kenner to accept
4339 my patch to expand_increment. (jason) */
4340 val = build (code, TREE_TYPE (arg), arg, inc);
4341 #else
4342 val = boolean_increment (code, arg);
4343 #endif
4345 else
4346 val = build (code, TREE_TYPE (arg), arg, inc);
4348 TREE_SIDE_EFFECTS (val) = 1;
4349 return cp_convert (result_type, val);
4352 case ADDR_EXPR:
4353 /* Note that this operation never does default_conversion
4354 regardless of NOCONVERT. */
4356 argtype = lvalue_type (arg);
4357 if (TREE_CODE (argtype) == REFERENCE_TYPE)
4359 arg = build1
4360 (CONVERT_EXPR,
4361 build_pointer_type (TREE_TYPE (argtype)), arg);
4362 TREE_CONSTANT (arg) = TREE_CONSTANT (TREE_OPERAND (arg, 0));
4363 return arg;
4365 else if (pedantic && DECL_MAIN_P (arg))
4366 /* ARM $3.4 */
4367 pedwarn ("ISO C++ forbids taking address of function `::main'");
4369 /* Let &* cancel out to simplify resulting code. */
4370 if (TREE_CODE (arg) == INDIRECT_REF)
4372 /* We don't need to have `current_class_ptr' wrapped in a
4373 NON_LVALUE_EXPR node. */
4374 if (arg == current_class_ref)
4375 return current_class_ptr;
4377 arg = TREE_OPERAND (arg, 0);
4378 if (TREE_CODE (TREE_TYPE (arg)) == REFERENCE_TYPE)
4380 arg = build1
4381 (CONVERT_EXPR,
4382 build_pointer_type (TREE_TYPE (TREE_TYPE (arg))), arg);
4383 TREE_CONSTANT (arg) = TREE_CONSTANT (TREE_OPERAND (arg, 0));
4385 else if (lvalue_p (arg))
4386 /* Don't let this be an lvalue. */
4387 return non_lvalue (arg);
4388 return arg;
4391 /* For &x[y], return x+y */
4392 if (TREE_CODE (arg) == ARRAY_REF)
4394 if (!cxx_mark_addressable (TREE_OPERAND (arg, 0)))
4395 return error_mark_node;
4396 return cp_build_binary_op (PLUS_EXPR, TREE_OPERAND (arg, 0),
4397 TREE_OPERAND (arg, 1));
4400 /* Uninstantiated types are all functions. Taking the
4401 address of a function is a no-op, so just return the
4402 argument. */
4404 if (TREE_CODE (arg) == IDENTIFIER_NODE
4405 && IDENTIFIER_OPNAME_P (arg))
4407 abort ();
4408 /* We don't know the type yet, so just work around the problem.
4409 We know that this will resolve to an lvalue. */
4410 return build1 (ADDR_EXPR, unknown_type_node, arg);
4413 if (TREE_CODE (arg) == COMPONENT_REF && type_unknown_p (arg)
4414 && !really_overloaded_fn (TREE_OPERAND (arg, 1)))
4416 /* They're trying to take the address of a unique non-static
4417 member function. This is ill-formed (except in MS-land),
4418 but let's try to DTRT.
4419 Note: We only handle unique functions here because we don't
4420 want to complain if there's a static overload; non-unique
4421 cases will be handled by instantiate_type. But we need to
4422 handle this case here to allow casts on the resulting PMF.
4423 We could defer this in non-MS mode, but it's easier to give
4424 a useful error here. */
4426 tree base = TREE_TYPE (TREE_OPERAND (arg, 0));
4427 tree name = DECL_NAME (get_first_fn (TREE_OPERAND (arg, 1)));
4429 if (! flag_ms_extensions)
4431 if (current_class_type
4432 && TREE_OPERAND (arg, 0) == current_class_ref)
4433 /* An expression like &memfn. */
4434 pedwarn ("ISO C++ forbids taking the address of an unqualified or parenthesized non-static member function to form a pointer to member function. Say `&%T::%D'", base, name);
4435 else
4436 pedwarn ("ISO C++ forbids taking the address of a bound member function to form a pointer to member function. Say `&%T::%D'", base, name);
4438 arg = build_offset_ref (base, name);
4441 if (type_unknown_p (arg))
4442 return build1 (ADDR_EXPR, unknown_type_node, arg);
4444 /* Handle complex lvalues (when permitted)
4445 by reduction to simpler cases. */
4446 val = unary_complex_lvalue (code, arg);
4447 if (val != 0)
4448 return val;
4450 switch (TREE_CODE (arg))
4452 case NOP_EXPR:
4453 case CONVERT_EXPR:
4454 case FLOAT_EXPR:
4455 case FIX_TRUNC_EXPR:
4456 case FIX_FLOOR_EXPR:
4457 case FIX_ROUND_EXPR:
4458 case FIX_CEIL_EXPR:
4459 if (! lvalue_p (arg) && pedantic)
4460 pedwarn ("ISO C++ forbids taking the address of a cast to a non-lvalue expression");
4461 break;
4463 default:
4464 break;
4467 /* Allow the address of a constructor if all the elements
4468 are constant. */
4469 if (TREE_CODE (arg) == CONSTRUCTOR && TREE_HAS_CONSTRUCTOR (arg)
4470 && TREE_CONSTANT (arg))
4472 /* Anything not already handled and not a true memory reference
4473 is an error. */
4474 else if (TREE_CODE (argtype) != FUNCTION_TYPE
4475 && TREE_CODE (argtype) != METHOD_TYPE
4476 && !non_cast_lvalue_or_else (arg, "unary `&'"))
4477 return error_mark_node;
4479 if (argtype != error_mark_node)
4480 argtype = build_pointer_type (argtype);
4482 if (!cxx_mark_addressable (arg))
4483 return error_mark_node;
4486 tree addr;
4488 if (TREE_CODE (arg) == COMPONENT_REF
4489 && TREE_CODE (TREE_OPERAND (arg, 1)) == BASELINK)
4490 arg = BASELINK_FUNCTIONS (TREE_OPERAND (arg, 1));
4492 if (TREE_CODE (arg) == COMPONENT_REF
4493 && DECL_C_BIT_FIELD (TREE_OPERAND (arg, 1)))
4495 error ("attempt to take address of bit-field structure member `%D'",
4496 TREE_OPERAND (arg, 1));
4497 return error_mark_node;
4499 else if (TREE_CODE (arg) == COMPONENT_REF
4500 && TREE_CODE (TREE_OPERAND (arg, 0)) == INDIRECT_REF
4501 && (TREE_CODE (TREE_OPERAND (TREE_OPERAND (arg, 0), 0))
4502 == INTEGER_CST))
4504 /* offsetof idiom, fold it. */
4505 tree field = TREE_OPERAND (arg, 1);
4506 tree rval = build_unary_op (ADDR_EXPR, TREE_OPERAND (arg, 0), 0);
4507 tree binfo = lookup_base (TREE_TYPE (TREE_TYPE (rval)),
4508 decl_type_context (field),
4509 ba_check, NULL);
4511 rval = build_base_path (PLUS_EXPR, rval, binfo, 1);
4512 rval = build1 (NOP_EXPR, argtype, rval);
4513 TREE_CONSTANT (rval) = TREE_CONSTANT (TREE_OPERAND (rval, 0));
4514 addr = fold (build (PLUS_EXPR, argtype, rval,
4515 cp_convert (argtype, byte_position (field))));
4517 else
4518 addr = build1 (ADDR_EXPR, argtype, arg);
4520 /* Address of a static or external variable or
4521 function counts as a constant */
4522 if (staticp (arg))
4523 TREE_CONSTANT (addr) = 1;
4525 if (TREE_CODE (argtype) == POINTER_TYPE
4526 && TREE_CODE (TREE_TYPE (argtype)) == METHOD_TYPE)
4528 build_ptrmemfunc_type (argtype);
4529 addr = build_ptrmemfunc (argtype, addr, 0);
4532 return addr;
4535 default:
4536 break;
4539 if (!errstring)
4541 if (argtype == 0)
4542 argtype = TREE_TYPE (arg);
4543 return fold (build1 (code, argtype, arg));
4546 error ("%s", errstring);
4547 return error_mark_node;
4550 /* Apply unary lvalue-demanding operator CODE to the expression ARG
4551 for certain kinds of expressions which are not really lvalues
4552 but which we can accept as lvalues.
4554 If ARG is not a kind of expression we can handle, return zero. */
4556 tree
4557 unary_complex_lvalue (code, arg)
4558 enum tree_code code;
4559 tree arg;
4561 /* Handle (a, b) used as an "lvalue". */
4562 if (TREE_CODE (arg) == COMPOUND_EXPR)
4564 tree real_result = build_unary_op (code, TREE_OPERAND (arg, 1), 0);
4565 return build (COMPOUND_EXPR, TREE_TYPE (real_result),
4566 TREE_OPERAND (arg, 0), real_result);
4569 /* Handle (a ? b : c) used as an "lvalue". */
4570 if (TREE_CODE (arg) == COND_EXPR
4571 || TREE_CODE (arg) == MIN_EXPR || TREE_CODE (arg) == MAX_EXPR)
4572 return rationalize_conditional_expr (code, arg);
4574 /* Handle (a = b), (++a), and (--a) used as an "lvalue". */
4575 if (TREE_CODE (arg) == MODIFY_EXPR
4576 || TREE_CODE (arg) == PREINCREMENT_EXPR
4577 || TREE_CODE (arg) == PREDECREMENT_EXPR)
4579 tree lvalue = TREE_OPERAND (arg, 0);
4580 if (TREE_SIDE_EFFECTS (lvalue))
4582 lvalue = stabilize_reference (lvalue);
4583 arg = build (TREE_CODE (arg), TREE_TYPE (arg),
4584 lvalue, TREE_OPERAND (arg, 1));
4586 return unary_complex_lvalue
4587 (code, build (COMPOUND_EXPR, TREE_TYPE (lvalue), arg, lvalue));
4590 if (code != ADDR_EXPR)
4591 return 0;
4593 /* Handle (a = b) used as an "lvalue" for `&'. */
4594 if (TREE_CODE (arg) == MODIFY_EXPR
4595 || TREE_CODE (arg) == INIT_EXPR)
4597 tree real_result = build_unary_op (code, TREE_OPERAND (arg, 0), 0);
4598 arg = build (COMPOUND_EXPR, TREE_TYPE (real_result), arg, real_result);
4599 TREE_NO_UNUSED_WARNING (arg) = 1;
4600 return arg;
4603 if (TREE_CODE (TREE_TYPE (arg)) == FUNCTION_TYPE
4604 || TREE_CODE (TREE_TYPE (arg)) == METHOD_TYPE
4605 || TREE_CODE (TREE_TYPE (arg)) == OFFSET_TYPE)
4607 /* The representation of something of type OFFSET_TYPE
4608 is really the representation of a pointer to it.
4609 Here give the representation its true type. */
4610 tree t;
4612 my_friendly_assert (TREE_CODE (arg) != SCOPE_REF, 313);
4614 if (TREE_CODE (arg) != OFFSET_REF)
4615 return 0;
4617 t = TREE_OPERAND (arg, 1);
4619 /* Check all this code for right semantics. */
4620 if (TREE_CODE (t) == FUNCTION_DECL)
4622 if (DECL_DESTRUCTOR_P (t))
4623 error ("taking address of destructor");
4624 return build_unary_op (ADDR_EXPR, t, 0);
4626 if (TREE_CODE (t) == VAR_DECL)
4627 return build_unary_op (ADDR_EXPR, t, 0);
4628 else
4630 tree type;
4632 if (TREE_OPERAND (arg, 0)
4633 && ! is_dummy_object (TREE_OPERAND (arg, 0))
4634 && TREE_CODE (t) != FIELD_DECL)
4636 error ("taking address of bound pointer-to-member expression");
4637 return error_mark_node;
4639 if (!PTRMEM_OK_P (arg))
4641 /* This cannot form a pointer to method, so we must
4642 resolve the offset ref, and take the address of the
4643 result. For instance,
4644 &(C::m) */
4645 arg = resolve_offset_ref (arg);
4647 return build_unary_op (code, arg, 0);
4650 if (TREE_CODE (TREE_TYPE (t)) == REFERENCE_TYPE)
4652 error ("cannot create pointer to reference member `%D'", t);
4653 return error_mark_node;
4656 type = build_ptrmem_type (DECL_FIELD_CONTEXT (t), TREE_TYPE (t));
4657 t = make_ptrmem_cst (type, TREE_OPERAND (arg, 1));
4658 return t;
4663 /* We permit compiler to make function calls returning
4664 objects of aggregate type look like lvalues. */
4666 tree targ = arg;
4668 if (TREE_CODE (targ) == SAVE_EXPR)
4669 targ = TREE_OPERAND (targ, 0);
4671 if (TREE_CODE (targ) == CALL_EXPR && IS_AGGR_TYPE (TREE_TYPE (targ)))
4673 if (TREE_CODE (arg) == SAVE_EXPR)
4674 targ = arg;
4675 else
4676 targ = build_cplus_new (TREE_TYPE (arg), arg);
4677 return build1 (ADDR_EXPR, build_pointer_type (TREE_TYPE (arg)), targ);
4680 if (TREE_CODE (arg) == SAVE_EXPR && TREE_CODE (targ) == INDIRECT_REF)
4681 return build (SAVE_EXPR, build_pointer_type (TREE_TYPE (arg)),
4682 TREE_OPERAND (targ, 0), current_function_decl, NULL);
4685 /* Don't let anything else be handled specially. */
4686 return 0;
4689 /* Mark EXP saying that we need to be able to take the
4690 address of it; it should not be allocated in a register.
4691 Value is true if successful.
4693 C++: we do not allow `current_class_ptr' to be addressable. */
4695 bool
4696 cxx_mark_addressable (exp)
4697 tree exp;
4699 register tree x = exp;
4701 while (1)
4702 switch (TREE_CODE (x))
4704 case ADDR_EXPR:
4705 case COMPONENT_REF:
4706 case ARRAY_REF:
4707 case REALPART_EXPR:
4708 case IMAGPART_EXPR:
4709 x = TREE_OPERAND (x, 0);
4710 break;
4712 case PARM_DECL:
4713 if (x == current_class_ptr)
4715 error ("cannot take the address of `this', which is an rvalue expression");
4716 TREE_ADDRESSABLE (x) = 1; /* so compiler doesn't die later */
4717 return true;
4719 /* FALLTHRU */
4721 case VAR_DECL:
4722 /* Caller should not be trying to mark initialized
4723 constant fields addressable. */
4724 my_friendly_assert (DECL_LANG_SPECIFIC (x) == 0
4725 || DECL_IN_AGGR_P (x) == 0
4726 || TREE_STATIC (x)
4727 || DECL_EXTERNAL (x), 314);
4728 /* FALLTHRU */
4730 case CONST_DECL:
4731 case RESULT_DECL:
4732 if (DECL_REGISTER (x) && !TREE_ADDRESSABLE (x)
4733 && !DECL_ARTIFICIAL (x) && extra_warnings)
4734 warning ("address requested for `%D', which is declared `register'",
4736 TREE_ADDRESSABLE (x) = 1;
4737 put_var_into_stack (x);
4738 return true;
4740 case FUNCTION_DECL:
4741 TREE_ADDRESSABLE (x) = 1;
4742 TREE_ADDRESSABLE (DECL_ASSEMBLER_NAME (x)) = 1;
4743 return true;
4745 case CONSTRUCTOR:
4746 TREE_ADDRESSABLE (x) = 1;
4747 return true;
4749 case TARGET_EXPR:
4750 TREE_ADDRESSABLE (x) = 1;
4751 cxx_mark_addressable (TREE_OPERAND (x, 0));
4752 return true;
4754 default:
4755 return true;
4759 /* Build and return a conditional expression IFEXP ? OP1 : OP2. */
4761 tree
4762 build_x_conditional_expr (ifexp, op1, op2)
4763 tree ifexp, op1, op2;
4765 if (processing_template_decl)
4766 return build_min_nt (COND_EXPR, ifexp, op1, op2);
4768 return build_conditional_expr (ifexp, op1, op2);
4771 /* Handle overloading of the ',' operator when needed. Otherwise,
4772 this function just builds an expression list. */
4774 tree
4775 build_x_compound_expr (list)
4776 tree list;
4778 tree rest = TREE_CHAIN (list);
4779 tree result;
4781 if (processing_template_decl)
4782 return build_min_nt (COMPOUND_EXPR, list, NULL_TREE);
4784 if (rest == NULL_TREE)
4785 return build_compound_expr (list);
4787 result = build_new_op (COMPOUND_EXPR, LOOKUP_NORMAL,
4788 TREE_VALUE (list), TREE_VALUE (rest), NULL_TREE);
4789 if (result)
4790 return build_x_compound_expr (tree_cons (NULL_TREE, result,
4791 TREE_CHAIN (rest)));
4793 if (! TREE_SIDE_EFFECTS (TREE_VALUE (list)))
4795 /* FIXME: This test should be in the implicit cast to void of the LHS. */
4796 /* the left-hand operand of a comma expression is like an expression
4797 statement: we should warn if it doesn't have any side-effects,
4798 unless it was explicitly cast to (void). */
4799 if ((extra_warnings || warn_unused_value)
4800 && !(TREE_CODE (TREE_VALUE(list)) == CONVERT_EXPR
4801 && VOID_TYPE_P (TREE_TYPE (TREE_VALUE(list)))))
4802 warning("left-hand operand of comma expression has no effect");
4804 #if 0 /* this requires a gcc backend patch to export warn_if_unused_value */
4805 else if (warn_unused_value)
4806 warn_if_unused_value (TREE_VALUE(list));
4807 #endif
4809 return build_compound_expr
4810 (tree_cons (NULL_TREE, TREE_VALUE (list),
4811 build_tree_list (NULL_TREE,
4812 build_x_compound_expr (rest))));
4815 /* Given a list of expressions, return a compound expression
4816 that performs them all and returns the value of the last of them. */
4818 tree
4819 build_compound_expr (list)
4820 tree list;
4822 register tree rest;
4823 tree first;
4825 TREE_VALUE (list) = decl_constant_value (TREE_VALUE (list));
4827 if (TREE_CHAIN (list) == 0)
4829 /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
4830 Strip such NOP_EXPRs, since LIST is used in non-lvalue context. */
4831 if (TREE_CODE (list) == NOP_EXPR
4832 && TREE_TYPE (list) == TREE_TYPE (TREE_OPERAND (list, 0)))
4833 list = TREE_OPERAND (list, 0);
4835 return TREE_VALUE (list);
4838 first = TREE_VALUE (list);
4839 first = convert_to_void (first, "left-hand operand of comma");
4840 if (first == error_mark_node)
4841 return error_mark_node;
4843 rest = build_compound_expr (TREE_CHAIN (list));
4844 if (rest == error_mark_node)
4845 return error_mark_node;
4847 /* When pedantic, a compound expression cannot be a constant expression. */
4848 if (! TREE_SIDE_EFFECTS (first) && ! pedantic)
4849 return rest;
4851 return build (COMPOUND_EXPR, TREE_TYPE (rest), first, rest);
4854 tree
4855 build_static_cast (type, expr)
4856 tree type, expr;
4858 tree intype;
4859 int ok;
4861 if (type == error_mark_node || expr == error_mark_node)
4862 return error_mark_node;
4864 if (TREE_CODE (expr) == OFFSET_REF)
4865 expr = resolve_offset_ref (expr);
4867 if (processing_template_decl)
4869 tree t = build_min (STATIC_CAST_EXPR, type, expr);
4870 return t;
4873 /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
4874 Strip such NOP_EXPRs if VALUE is being used in non-lvalue context. */
4875 if (TREE_CODE (type) != REFERENCE_TYPE
4876 && TREE_CODE (expr) == NOP_EXPR
4877 && TREE_TYPE (expr) == TREE_TYPE (TREE_OPERAND (expr, 0)))
4878 expr = TREE_OPERAND (expr, 0);
4880 if (TREE_CODE (type) == VOID_TYPE)
4882 expr = convert_to_void (expr, /*implicit=*/NULL);
4883 return expr;
4886 if (TREE_CODE (type) == REFERENCE_TYPE)
4887 return (convert_from_reference
4888 (convert_to_reference (type, expr, CONV_STATIC|CONV_IMPLICIT,
4889 LOOKUP_COMPLAIN, NULL_TREE)));
4891 if (IS_AGGR_TYPE (type))
4892 return build_cplus_new (type, (build_special_member_call
4893 (NULL_TREE, complete_ctor_identifier,
4894 build_tree_list (NULL_TREE, expr),
4895 TYPE_BINFO (type), LOOKUP_NORMAL)));
4897 intype = TREE_TYPE (expr);
4899 /* FIXME handle casting to array type. */
4901 ok = 0;
4902 if (IS_AGGR_TYPE (intype)
4903 ? can_convert_arg (type, intype, expr)
4904 : can_convert_arg (strip_all_pointer_quals (type),
4905 strip_all_pointer_quals (intype), expr))
4906 /* This is a standard conversion. */
4907 ok = 1;
4908 else if (TYPE_PTROB_P (type) && TYPE_PTROB_P (intype))
4910 /* They're pointers to objects. They must be aggregates that
4911 are related non-virtually. */
4912 base_kind kind;
4914 if (IS_AGGR_TYPE (TREE_TYPE (type)) && IS_AGGR_TYPE (TREE_TYPE (intype))
4915 && lookup_base (TREE_TYPE (type), TREE_TYPE (intype),
4916 ba_ignore | ba_quiet, &kind)
4917 && kind != bk_via_virtual)
4918 ok = 1;
4920 else if (TYPE_PTRMEM_P (type) && TYPE_PTRMEM_P (intype))
4922 /* They're pointers to members. The pointed to objects must be
4923 the same (ignoring CV qualifiers), and the containing classes
4924 must be related non-virtually. */
4925 base_kind kind;
4927 if (same_type_p
4928 (strip_all_pointer_quals (TREE_TYPE (TREE_TYPE (type))),
4929 strip_all_pointer_quals (TREE_TYPE (TREE_TYPE (intype))))
4930 && (lookup_base (TYPE_OFFSET_BASETYPE (TREE_TYPE (intype)),
4931 TYPE_OFFSET_BASETYPE (TREE_TYPE (type)),
4932 ba_ignore | ba_quiet, &kind))
4933 && kind != bk_via_virtual)
4934 ok = 1;
4936 else if (TREE_CODE (intype) != BOOLEAN_TYPE
4937 && TREE_CODE (type) != ARRAY_TYPE
4938 && TREE_CODE (type) != FUNCTION_TYPE
4939 && can_convert (intype, strip_all_pointer_quals (type)))
4940 ok = 1;
4941 else if (TREE_CODE (intype) == ENUMERAL_TYPE
4942 && TREE_CODE (type) == ENUMERAL_TYPE)
4943 /* DR 128: "A value of integral _or enumeration_ type can be explicitly
4944 converted to an enumeration type."
4945 The integral to enumeration will be accepted by the previous clause.
4946 We need to explicitly check for enumeration to enumeration. */
4947 ok = 1;
4949 /* [expr.static.cast]
4951 The static_cast operator shall not be used to cast away
4952 constness. */
4953 if (ok && casts_away_constness (intype, type))
4955 error ("static_cast from type `%T' to type `%T' casts away constness",
4956 intype, type);
4957 return error_mark_node;
4960 if (ok)
4961 return build_c_cast (type, expr);
4963 error ("invalid static_cast from type `%T' to type `%T'", intype, type);
4964 return error_mark_node;
4967 tree
4968 build_reinterpret_cast (type, expr)
4969 tree type, expr;
4971 tree intype;
4973 if (type == error_mark_node || expr == error_mark_node)
4974 return error_mark_node;
4976 if (TREE_CODE (expr) == OFFSET_REF)
4977 expr = resolve_offset_ref (expr);
4979 if (processing_template_decl)
4981 tree t = build_min (REINTERPRET_CAST_EXPR, type, expr);
4982 return t;
4985 if (TREE_CODE (type) != REFERENCE_TYPE)
4987 expr = decay_conversion (expr);
4989 /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
4990 Strip such NOP_EXPRs if VALUE is being used in non-lvalue context. */
4991 if (TREE_CODE (expr) == NOP_EXPR
4992 && TREE_TYPE (expr) == TREE_TYPE (TREE_OPERAND (expr, 0)))
4993 expr = TREE_OPERAND (expr, 0);
4996 intype = TREE_TYPE (expr);
4998 if (TREE_CODE (type) == REFERENCE_TYPE)
5000 if (! real_lvalue_p (expr))
5002 error ("invalid reinterpret_cast of an rvalue expression of type `%T' to type `%T'", intype, type);
5003 return error_mark_node;
5005 expr = build_unary_op (ADDR_EXPR, expr, 0);
5006 if (expr != error_mark_node)
5007 expr = build_reinterpret_cast
5008 (build_pointer_type (TREE_TYPE (type)), expr);
5009 if (expr != error_mark_node)
5010 expr = build_indirect_ref (expr, 0);
5011 return expr;
5013 else if (same_type_ignoring_top_level_qualifiers_p (intype, type))
5014 return build_static_cast (type, expr);
5016 if (TYPE_PTR_P (type) && (TREE_CODE (intype) == INTEGER_TYPE
5017 || TREE_CODE (intype) == ENUMERAL_TYPE))
5018 /* OK */;
5019 else if (TREE_CODE (type) == INTEGER_TYPE && TYPE_PTR_P (intype))
5021 if (TYPE_PRECISION (type) < TYPE_PRECISION (intype))
5022 pedwarn ("reinterpret_cast from `%T' to `%T' loses precision",
5023 intype, type);
5025 else if ((TYPE_PTRFN_P (type) && TYPE_PTRFN_P (intype))
5026 || (TYPE_PTRMEMFUNC_P (type) && TYPE_PTRMEMFUNC_P (intype)))
5028 expr = decl_constant_value (expr);
5029 return fold (build1 (NOP_EXPR, type, expr));
5031 else if ((TYPE_PTRMEM_P (type) && TYPE_PTRMEM_P (intype))
5032 || (TYPE_PTROBV_P (type) && TYPE_PTROBV_P (intype)))
5034 if (! comp_ptr_ttypes_reinterpret (TREE_TYPE (type), TREE_TYPE (intype)))
5035 pedwarn ("reinterpret_cast from `%T' to `%T' casts away const (or volatile)",
5036 intype, type);
5038 expr = decl_constant_value (expr);
5039 return fold (build1 (NOP_EXPR, type, expr));
5041 else if ((TYPE_PTRFN_P (type) && TYPE_PTROBV_P (intype))
5042 || (TYPE_PTRFN_P (intype) && TYPE_PTROBV_P (type)))
5044 pedwarn ("ISO C++ forbids casting between pointer-to-function and pointer-to-object");
5045 expr = decl_constant_value (expr);
5046 return fold (build1 (NOP_EXPR, type, expr));
5048 else
5050 error ("invalid reinterpret_cast from type `%T' to type `%T'",
5051 intype, type);
5052 return error_mark_node;
5055 return cp_convert (type, expr);
5058 tree
5059 build_const_cast (type, expr)
5060 tree type, expr;
5062 tree intype;
5064 if (type == error_mark_node || expr == error_mark_node)
5065 return error_mark_node;
5067 if (TREE_CODE (expr) == OFFSET_REF)
5068 expr = resolve_offset_ref (expr);
5070 if (processing_template_decl)
5072 tree t = build_min (CONST_CAST_EXPR, type, expr);
5073 return t;
5076 if (!POINTER_TYPE_P (type))
5077 error ("invalid use of const_cast with type `%T', which is not a pointer, reference, nor a pointer-to-data-member type", type);
5078 else if (TREE_CODE (TREE_TYPE (type)) == FUNCTION_TYPE)
5080 error ("invalid use of const_cast with type `%T', which is a pointer or reference to a function type", type);
5081 return error_mark_node;
5084 if (TREE_CODE (type) != REFERENCE_TYPE)
5086 expr = decay_conversion (expr);
5088 /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
5089 Strip such NOP_EXPRs if VALUE is being used in non-lvalue context. */
5090 if (TREE_CODE (expr) == NOP_EXPR
5091 && TREE_TYPE (expr) == TREE_TYPE (TREE_OPERAND (expr, 0)))
5092 expr = TREE_OPERAND (expr, 0);
5095 intype = TREE_TYPE (expr);
5097 if (same_type_ignoring_top_level_qualifiers_p (intype, type))
5098 return build_static_cast (type, expr);
5099 else if (TREE_CODE (type) == REFERENCE_TYPE)
5101 if (! real_lvalue_p (expr))
5103 error ("invalid const_cast of an rvalue of type `%T' to type `%T'", intype, type);
5104 return error_mark_node;
5107 if (comp_ptr_ttypes_const (TREE_TYPE (type), intype))
5109 expr = build_unary_op (ADDR_EXPR, expr, 0);
5110 expr = build1 (NOP_EXPR, type, expr);
5111 return convert_from_reference (expr);
5114 else if (TREE_CODE (type) == POINTER_TYPE
5115 && TREE_CODE (intype) == POINTER_TYPE
5116 && comp_ptr_ttypes_const (TREE_TYPE (type), TREE_TYPE (intype)))
5117 return cp_convert (type, expr);
5119 error ("invalid const_cast from type `%T' to type `%T'", intype, type);
5120 return error_mark_node;
5123 /* Build an expression representing a cast to type TYPE of expression EXPR.
5125 ALLOW_NONCONVERTING is true if we should allow non-converting constructors
5126 when doing the cast. */
5128 tree
5129 build_c_cast (type, expr)
5130 tree type, expr;
5132 register tree value = expr;
5133 tree otype;
5135 if (type == error_mark_node || expr == error_mark_node)
5136 return error_mark_node;
5138 if (processing_template_decl)
5140 tree t = build_min (CAST_EXPR, type,
5141 tree_cons (NULL_TREE, value, NULL_TREE));
5142 return t;
5145 /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
5146 Strip such NOP_EXPRs if VALUE is being used in non-lvalue context. */
5147 if (TREE_CODE (type) != REFERENCE_TYPE
5148 && TREE_CODE (value) == NOP_EXPR
5149 && TREE_TYPE (value) == TREE_TYPE (TREE_OPERAND (value, 0)))
5150 value = TREE_OPERAND (value, 0);
5152 if (TREE_CODE (value) == OFFSET_REF)
5153 value = resolve_offset_ref (value);
5155 if (TREE_CODE (type) == ARRAY_TYPE)
5157 /* Allow casting from T1* to T2[] because Cfront allows it.
5158 NIHCL uses it. It is not valid ISO C++ however. */
5159 if (TREE_CODE (TREE_TYPE (expr)) == POINTER_TYPE)
5161 pedwarn ("ISO C++ forbids casting to an array type `%T'", type);
5162 type = build_pointer_type (TREE_TYPE (type));
5164 else
5166 error ("ISO C++ forbids casting to an array type `%T'", type);
5167 return error_mark_node;
5171 if (TREE_CODE (type) == FUNCTION_TYPE
5172 || TREE_CODE (type) == METHOD_TYPE)
5174 error ("invalid cast to function type `%T'", type);
5175 return error_mark_node;
5178 if (TREE_CODE (type) == VOID_TYPE)
5180 /* Conversion to void does not cause any of the normal function to
5181 * pointer, array to pointer and lvalue to rvalue decays. */
5183 value = convert_to_void (value, /*implicit=*/NULL);
5184 return value;
5186 /* Convert functions and arrays to pointers and
5187 convert references to their expanded types,
5188 but don't convert any other types. If, however, we are
5189 casting to a class type, there's no reason to do this: the
5190 cast will only succeed if there is a converting constructor,
5191 and the default conversions will be done at that point. In
5192 fact, doing the default conversion here is actually harmful
5193 in cases like this:
5195 typedef int A[2];
5196 struct S { S(const A&); };
5198 since we don't want the array-to-pointer conversion done. */
5199 if (!IS_AGGR_TYPE (type))
5201 if (TREE_CODE (TREE_TYPE (value)) == FUNCTION_TYPE
5202 || (TREE_CODE (TREE_TYPE (value)) == METHOD_TYPE
5203 /* Don't do the default conversion on a ->* expression. */
5204 && ! (TREE_CODE (type) == POINTER_TYPE
5205 && bound_pmf_p (value)))
5206 || TREE_CODE (TREE_TYPE (value)) == ARRAY_TYPE
5207 || TREE_CODE (TREE_TYPE (value)) == REFERENCE_TYPE)
5208 value = default_conversion (value);
5210 else if (TREE_CODE (TREE_TYPE (value)) == REFERENCE_TYPE)
5211 /* However, even for class types, we still need to strip away
5212 the reference type, since the call to convert_force below
5213 does not expect the input expression to be of reference
5214 type. */
5215 value = convert_from_reference (value);
5217 otype = TREE_TYPE (value);
5219 /* Optionally warn about potentially worrisome casts. */
5221 if (warn_cast_qual
5222 && TREE_CODE (type) == POINTER_TYPE
5223 && TREE_CODE (otype) == POINTER_TYPE
5224 && !at_least_as_qualified_p (TREE_TYPE (type),
5225 TREE_TYPE (otype)))
5226 warning ("cast from `%T' to `%T' discards qualifiers from pointer target type",
5227 otype, type);
5229 if (TREE_CODE (type) == INTEGER_TYPE
5230 && TREE_CODE (otype) == POINTER_TYPE
5231 && TYPE_PRECISION (type) != TYPE_PRECISION (otype))
5232 warning ("cast from pointer to integer of different size");
5234 if (TREE_CODE (type) == POINTER_TYPE
5235 && TREE_CODE (otype) == INTEGER_TYPE
5236 && TYPE_PRECISION (type) != TYPE_PRECISION (otype)
5237 /* Don't warn about converting any constant. */
5238 && !TREE_CONSTANT (value))
5239 warning ("cast to pointer from integer of different size");
5241 if (TREE_CODE (type) == REFERENCE_TYPE)
5242 value = (convert_from_reference
5243 (convert_to_reference (type, value, CONV_C_CAST,
5244 LOOKUP_COMPLAIN, NULL_TREE)));
5245 else
5247 tree ovalue;
5249 value = decl_constant_value (value);
5251 ovalue = value;
5252 value = convert_force (type, value, CONV_C_CAST);
5254 /* Ignore any integer overflow caused by the cast. */
5255 if (TREE_CODE (value) == INTEGER_CST)
5257 TREE_OVERFLOW (value) = TREE_OVERFLOW (ovalue);
5258 TREE_CONSTANT_OVERFLOW (value) = TREE_CONSTANT_OVERFLOW (ovalue);
5262 /* Warn about possible alignment problems. Do this here when we will have
5263 instantiated any necessary template types. */
5264 if (STRICT_ALIGNMENT && warn_cast_align
5265 && TREE_CODE (type) == POINTER_TYPE
5266 && TREE_CODE (otype) == POINTER_TYPE
5267 && TREE_CODE (TREE_TYPE (otype)) != VOID_TYPE
5268 && TREE_CODE (TREE_TYPE (otype)) != FUNCTION_TYPE
5269 && COMPLETE_TYPE_P (TREE_TYPE (otype))
5270 && COMPLETE_TYPE_P (TREE_TYPE (type))
5271 && TYPE_ALIGN (TREE_TYPE (type)) > TYPE_ALIGN (TREE_TYPE (otype)))
5272 warning ("cast from `%T' to `%T' increases required alignment of target type",
5273 otype, type);
5275 /* Always produce some operator for an explicit cast,
5276 so we can tell (for -pedantic) that the cast is no lvalue. */
5277 if (TREE_CODE (type) != REFERENCE_TYPE && value == expr
5278 && real_lvalue_p (value))
5279 value = non_lvalue (value);
5281 return value;
5284 /* Build an assignment expression of lvalue LHS from value RHS.
5285 MODIFYCODE is the code for a binary operator that we use
5286 to combine the old value of LHS with RHS to get the new value.
5287 Or else MODIFYCODE is NOP_EXPR meaning do a simple assignment.
5289 C++: If MODIFYCODE is INIT_EXPR, then leave references unbashed. */
5291 tree
5292 build_modify_expr (lhs, modifycode, rhs)
5293 tree lhs;
5294 enum tree_code modifycode;
5295 tree rhs;
5297 register tree result;
5298 tree newrhs = rhs;
5299 tree lhstype = TREE_TYPE (lhs);
5300 tree olhstype = lhstype;
5301 tree olhs = lhs;
5303 /* Avoid duplicate error messages from operands that had errors. */
5304 if (lhs == error_mark_node || rhs == error_mark_node)
5305 return error_mark_node;
5307 /* Handle control structure constructs used as "lvalues". */
5308 switch (TREE_CODE (lhs))
5310 /* Handle --foo = 5; as these are valid constructs in C++ */
5311 case PREDECREMENT_EXPR:
5312 case PREINCREMENT_EXPR:
5313 if (TREE_SIDE_EFFECTS (TREE_OPERAND (lhs, 0)))
5314 lhs = build (TREE_CODE (lhs), TREE_TYPE (lhs),
5315 stabilize_reference (TREE_OPERAND (lhs, 0)),
5316 TREE_OPERAND (lhs, 1));
5317 return build (COMPOUND_EXPR, lhstype,
5318 lhs,
5319 build_modify_expr (TREE_OPERAND (lhs, 0),
5320 modifycode, rhs));
5322 /* Handle (a, b) used as an "lvalue". */
5323 case COMPOUND_EXPR:
5324 newrhs = build_modify_expr (TREE_OPERAND (lhs, 1),
5325 modifycode, rhs);
5326 if (newrhs == error_mark_node)
5327 return error_mark_node;
5328 return build (COMPOUND_EXPR, lhstype,
5329 TREE_OPERAND (lhs, 0), newrhs);
5331 case MODIFY_EXPR:
5332 newrhs = build_modify_expr (TREE_OPERAND (lhs, 0), modifycode, rhs);
5333 if (newrhs == error_mark_node)
5334 return error_mark_node;
5335 return build (COMPOUND_EXPR, lhstype, lhs, newrhs);
5337 /* Handle (a ? b : c) used as an "lvalue". */
5338 case COND_EXPR:
5340 /* Produce (a ? (b = rhs) : (c = rhs))
5341 except that the RHS goes through a save-expr
5342 so the code to compute it is only emitted once. */
5343 tree cond;
5344 tree preeval = NULL_TREE;
5346 rhs = stabilize_expr (rhs, &preeval);
5348 /* Check this here to avoid odd errors when trying to convert
5349 a throw to the type of the COND_EXPR. */
5350 if (!lvalue_or_else (lhs, "assignment"))
5351 return error_mark_node;
5353 cond = build_conditional_expr
5354 (TREE_OPERAND (lhs, 0),
5355 build_modify_expr (cp_convert (TREE_TYPE (lhs),
5356 TREE_OPERAND (lhs, 1)),
5357 modifycode, rhs),
5358 build_modify_expr (cp_convert (TREE_TYPE (lhs),
5359 TREE_OPERAND (lhs, 2)),
5360 modifycode, rhs));
5362 if (cond == error_mark_node)
5363 return cond;
5364 /* Make sure the code to compute the rhs comes out
5365 before the split. */
5366 return build (COMPOUND_EXPR, TREE_TYPE (lhs), preeval, cond);
5369 case OFFSET_REF:
5370 lhs = resolve_offset_ref (lhs);
5371 if (lhs == error_mark_node)
5372 return error_mark_node;
5373 olhstype = lhstype = TREE_TYPE (lhs);
5375 default:
5376 break;
5379 if (modifycode == INIT_EXPR)
5381 if (TREE_CODE (rhs) == CONSTRUCTOR)
5383 my_friendly_assert (same_type_p (TREE_TYPE (rhs), lhstype),
5384 20011220);
5385 result = build (INIT_EXPR, lhstype, lhs, rhs);
5386 TREE_SIDE_EFFECTS (result) = 1;
5387 return result;
5389 else if (! IS_AGGR_TYPE (lhstype))
5390 /* Do the default thing */;
5391 else
5393 result = build_special_member_call (lhs, complete_ctor_identifier,
5394 build_tree_list (NULL_TREE, rhs),
5395 TYPE_BINFO (lhstype),
5396 LOOKUP_NORMAL);
5397 if (result == NULL_TREE)
5398 return error_mark_node;
5399 return result;
5402 else
5404 if (TREE_CODE (lhstype) == REFERENCE_TYPE)
5406 lhs = convert_from_reference (lhs);
5407 olhstype = lhstype = TREE_TYPE (lhs);
5409 lhs = require_complete_type (lhs);
5410 if (lhs == error_mark_node)
5411 return error_mark_node;
5413 if (modifycode == NOP_EXPR)
5415 /* `operator=' is not an inheritable operator. */
5416 if (! IS_AGGR_TYPE (lhstype))
5417 /* Do the default thing */;
5418 else
5420 result = build_new_op (MODIFY_EXPR, LOOKUP_NORMAL,
5421 lhs, rhs, make_node (NOP_EXPR));
5422 if (result == NULL_TREE)
5423 return error_mark_node;
5424 return result;
5426 lhstype = olhstype;
5428 else
5430 /* A binary op has been requested. Combine the old LHS
5431 value with the RHS producing the value we should actually
5432 store into the LHS. */
5434 my_friendly_assert (!PROMOTES_TO_AGGR_TYPE (lhstype, REFERENCE_TYPE),
5435 978652);
5436 lhs = stabilize_reference (lhs);
5437 newrhs = cp_build_binary_op (modifycode, lhs, rhs);
5438 if (newrhs == error_mark_node)
5440 error (" in evaluation of `%Q(%#T, %#T)'", modifycode,
5441 TREE_TYPE (lhs), TREE_TYPE (rhs));
5442 return error_mark_node;
5445 /* Now it looks like a plain assignment. */
5446 modifycode = NOP_EXPR;
5448 my_friendly_assert (TREE_CODE (lhstype) != REFERENCE_TYPE, 20011220);
5449 my_friendly_assert (TREE_CODE (TREE_TYPE (newrhs)) != REFERENCE_TYPE,
5450 20011220);
5453 /* Handle a cast used as an "lvalue".
5454 We have already performed any binary operator using the value as cast.
5455 Now convert the result to the cast type of the lhs,
5456 and then true type of the lhs and store it there;
5457 then convert result back to the cast type to be the value
5458 of the assignment. */
5460 switch (TREE_CODE (lhs))
5462 case NOP_EXPR:
5463 case CONVERT_EXPR:
5464 case FLOAT_EXPR:
5465 case FIX_TRUNC_EXPR:
5466 case FIX_FLOOR_EXPR:
5467 case FIX_ROUND_EXPR:
5468 case FIX_CEIL_EXPR:
5470 tree inner_lhs = TREE_OPERAND (lhs, 0);
5471 tree result;
5473 if (TREE_CODE (TREE_TYPE (newrhs)) == ARRAY_TYPE
5474 || TREE_CODE (TREE_TYPE (newrhs)) == FUNCTION_TYPE
5475 || TREE_CODE (TREE_TYPE (newrhs)) == METHOD_TYPE
5476 || TREE_CODE (TREE_TYPE (newrhs)) == OFFSET_TYPE)
5477 newrhs = default_conversion (newrhs);
5479 /* ISO C++ 5.4/1: The result is an lvalue if T is a reference
5480 type, otherwise the result is an rvalue. */
5481 if (! lvalue_p (lhs))
5482 pedwarn ("ISO C++ forbids cast to non-reference type used as lvalue");
5484 result = build_modify_expr (inner_lhs, NOP_EXPR,
5485 cp_convert (TREE_TYPE (inner_lhs),
5486 cp_convert (lhstype, newrhs)));
5487 if (result == error_mark_node)
5488 return result;
5489 return cp_convert (TREE_TYPE (lhs), result);
5492 default:
5493 break;
5496 /* Now we have handled acceptable kinds of LHS that are not truly lvalues.
5497 Reject anything strange now. */
5499 if (!lvalue_or_else (lhs, "assignment"))
5500 return error_mark_node;
5502 /* Warn about modifying something that is `const'. Don't warn if
5503 this is initialization. */
5504 if (modifycode != INIT_EXPR
5505 && (TREE_READONLY (lhs) || CP_TYPE_CONST_P (lhstype)
5506 /* Functions are not modifiable, even though they are
5507 lvalues. */
5508 || TREE_CODE (TREE_TYPE (lhs)) == FUNCTION_TYPE
5509 || TREE_CODE (TREE_TYPE (lhs)) == METHOD_TYPE
5510 /* If it's an aggregate and any field is const, then it is
5511 effectively const. */
5512 || (CLASS_TYPE_P (lhstype)
5513 && C_TYPE_FIELDS_READONLY (lhstype))))
5514 readonly_error (lhs, "assignment", 0);
5516 /* If storing into a structure or union member, it has probably been
5517 given type `int'. Compute the type that would go with the actual
5518 amount of storage the member occupies. */
5520 if (TREE_CODE (lhs) == COMPONENT_REF
5521 && (TREE_CODE (lhstype) == INTEGER_TYPE
5522 || TREE_CODE (lhstype) == REAL_TYPE
5523 || TREE_CODE (lhstype) == ENUMERAL_TYPE))
5525 lhstype = TREE_TYPE (get_unwidened (lhs, 0));
5527 /* If storing in a field that is in actuality a short or narrower
5528 than one, we must store in the field in its actual type. */
5530 if (lhstype != TREE_TYPE (lhs))
5532 lhs = copy_node (lhs);
5533 TREE_TYPE (lhs) = lhstype;
5537 if (TREE_CODE (lhstype) != REFERENCE_TYPE)
5539 if (TREE_SIDE_EFFECTS (lhs))
5540 lhs = stabilize_reference (lhs);
5541 if (TREE_SIDE_EFFECTS (newrhs))
5542 newrhs = stabilize_reference (newrhs);
5545 /* Convert new value to destination type. */
5547 if (TREE_CODE (lhstype) == ARRAY_TYPE)
5549 int from_array;
5551 if (!same_or_base_type_p (TYPE_MAIN_VARIANT (lhstype),
5552 TYPE_MAIN_VARIANT (TREE_TYPE (rhs))))
5554 error ("incompatible types in assignment of `%T' to `%T'",
5555 TREE_TYPE (rhs), lhstype);
5556 return error_mark_node;
5559 /* Allow array assignment in compiler-generated code. */
5560 if (! DECL_ARTIFICIAL (current_function_decl))
5561 pedwarn ("ISO C++ forbids assignment of arrays");
5563 from_array = TREE_CODE (TREE_TYPE (newrhs)) == ARRAY_TYPE
5564 ? 1 + (modifycode != INIT_EXPR): 0;
5565 return build_vec_init (lhs, NULL_TREE, newrhs, from_array);
5568 if (modifycode == INIT_EXPR)
5569 newrhs = convert_for_initialization (lhs, lhstype, newrhs, LOOKUP_NORMAL,
5570 "initialization", NULL_TREE, 0);
5571 else
5573 /* Avoid warnings on enum bit fields. */
5574 if (TREE_CODE (olhstype) == ENUMERAL_TYPE
5575 && TREE_CODE (lhstype) == INTEGER_TYPE)
5577 newrhs = convert_for_assignment (olhstype, newrhs, "assignment",
5578 NULL_TREE, 0);
5579 newrhs = convert_force (lhstype, newrhs, 0);
5581 else
5582 newrhs = convert_for_assignment (lhstype, newrhs, "assignment",
5583 NULL_TREE, 0);
5584 if (TREE_CODE (newrhs) == CALL_EXPR
5585 && TYPE_NEEDS_CONSTRUCTING (lhstype))
5586 newrhs = build_cplus_new (lhstype, newrhs);
5588 /* Can't initialize directly from a TARGET_EXPR, since that would
5589 cause the lhs to be constructed twice, and possibly result in
5590 accidental self-initialization. So we force the TARGET_EXPR to be
5591 expanded without a target. */
5592 if (TREE_CODE (newrhs) == TARGET_EXPR)
5593 newrhs = build (COMPOUND_EXPR, TREE_TYPE (newrhs), newrhs,
5594 TREE_OPERAND (newrhs, 0));
5597 if (newrhs == error_mark_node)
5598 return error_mark_node;
5600 if (TREE_CODE (newrhs) == COND_EXPR)
5602 tree lhs1;
5603 tree cond = TREE_OPERAND (newrhs, 0);
5605 if (TREE_SIDE_EFFECTS (lhs))
5606 cond = build_compound_expr (tree_cons
5607 (NULL_TREE, lhs,
5608 build_tree_list (NULL_TREE, cond)));
5610 /* Cannot have two identical lhs on this one tree (result) as preexpand
5611 calls will rip them out and fill in RTL for them, but when the
5612 rtl is generated, the calls will only be in the first side of the
5613 condition, not on both, or before the conditional jump! (mrs) */
5614 lhs1 = break_out_calls (lhs);
5616 if (lhs == lhs1)
5617 /* If there's no change, the COND_EXPR behaves like any other rhs. */
5618 result = build (modifycode == NOP_EXPR ? MODIFY_EXPR : INIT_EXPR,
5619 lhstype, lhs, newrhs);
5620 else
5622 tree result_type = TREE_TYPE (newrhs);
5623 /* We have to convert each arm to the proper type because the
5624 types may have been munged by constant folding. */
5625 result
5626 = build (COND_EXPR, result_type, cond,
5627 build_modify_expr (lhs, modifycode,
5628 cp_convert (result_type,
5629 TREE_OPERAND (newrhs, 1))),
5630 build_modify_expr (lhs1, modifycode,
5631 cp_convert (result_type,
5632 TREE_OPERAND (newrhs, 2))));
5635 else
5636 result = build (modifycode == NOP_EXPR ? MODIFY_EXPR : INIT_EXPR,
5637 lhstype, lhs, newrhs);
5639 TREE_SIDE_EFFECTS (result) = 1;
5641 /* If we got the LHS in a different type for storing in,
5642 convert the result back to the nominal type of LHS
5643 so that the value we return always has the same type
5644 as the LHS argument. */
5646 if (olhstype == TREE_TYPE (result))
5647 return result;
5648 /* Avoid warnings converting integral types back into enums
5649 for enum bit fields. */
5650 if (TREE_CODE (TREE_TYPE (result)) == INTEGER_TYPE
5651 && TREE_CODE (olhstype) == ENUMERAL_TYPE)
5653 result = build (COMPOUND_EXPR, olhstype, result, olhs);
5654 TREE_NO_UNUSED_WARNING (result) = 1;
5655 return result;
5657 return convert_for_assignment (olhstype, result, "assignment",
5658 NULL_TREE, 0);
5661 tree
5662 build_x_modify_expr (lhs, modifycode, rhs)
5663 tree lhs;
5664 enum tree_code modifycode;
5665 tree rhs;
5667 if (processing_template_decl)
5668 return build_min_nt (MODOP_EXPR, lhs,
5669 build_min_nt (modifycode, NULL_TREE, NULL_TREE), rhs);
5671 if (modifycode != NOP_EXPR)
5673 tree rval = build_new_op (MODIFY_EXPR, LOOKUP_NORMAL, lhs, rhs,
5674 make_node (modifycode));
5675 if (rval)
5676 return rval;
5678 return build_modify_expr (lhs, modifycode, rhs);
5682 /* Get difference in deltas for different pointer to member function
5683 types. Return integer_zero_node, if FROM cannot be converted to a
5684 TO type. If FORCE is true, then allow reverse conversions as well.
5686 Note that the naming of FROM and TO is kind of backwards; the return
5687 value is what we add to a TO in order to get a FROM. They are named
5688 this way because we call this function to find out how to convert from
5689 a pointer to member of FROM to a pointer to member of TO. */
5691 static tree
5692 get_delta_difference (from, to, force)
5693 tree from, to;
5694 int force;
5696 tree delta = integer_zero_node;
5697 tree binfo;
5698 tree virt_binfo;
5699 base_kind kind;
5701 binfo = lookup_base (to, from, ba_check, &kind);
5702 if (kind == bk_inaccessible || kind == bk_ambig)
5704 error (" in pointer to member function conversion");
5705 return delta;
5707 if (!binfo)
5709 if (!force)
5711 error_not_base_type (from, to);
5712 error (" in pointer to member conversion");
5713 return delta;
5715 binfo = lookup_base (from, to, ba_check, &kind);
5716 if (binfo == 0)
5717 return delta;
5718 virt_binfo = binfo_from_vbase (binfo);
5720 if (virt_binfo)
5722 /* This is a reinterpret cast, we choose to do nothing. */
5723 warning ("pointer to member cast via virtual base `%T'",
5724 BINFO_TYPE (virt_binfo));
5725 return delta;
5727 delta = BINFO_OFFSET (binfo);
5728 delta = cp_convert (ptrdiff_type_node, delta);
5729 delta = cp_build_binary_op (MINUS_EXPR,
5730 integer_zero_node,
5731 delta);
5733 return delta;
5736 virt_binfo = binfo_from_vbase (binfo);
5737 if (virt_binfo)
5739 /* This is a reinterpret cast, we choose to do nothing. */
5740 if (force)
5741 warning ("pointer to member cast via virtual base `%T'",
5742 BINFO_TYPE (virt_binfo));
5743 else
5744 error ("pointer to member conversion via virtual base `%T'",
5745 BINFO_TYPE (virt_binfo));
5746 return delta;
5748 delta = BINFO_OFFSET (binfo);
5750 return cp_convert (ptrdiff_type_node, delta);
5753 /* Return a constructor for the pointer-to-member-function TYPE using
5754 the other components as specified. */
5756 tree
5757 build_ptrmemfunc1 (type, delta, pfn)
5758 tree type, delta, pfn;
5760 tree u = NULL_TREE;
5761 tree delta_field;
5762 tree pfn_field;
5764 /* Pull the FIELD_DECLs out of the type. */
5765 pfn_field = TYPE_FIELDS (type);
5766 delta_field = TREE_CHAIN (pfn_field);
5768 /* Make sure DELTA has the type we want. */
5769 delta = convert_and_check (delta_type_node, delta);
5771 /* Finish creating the initializer. */
5772 u = tree_cons (pfn_field, pfn,
5773 build_tree_list (delta_field, delta));
5774 u = build (CONSTRUCTOR, type, NULL_TREE, u);
5775 TREE_CONSTANT (u) = TREE_CONSTANT (pfn) && TREE_CONSTANT (delta);
5776 TREE_STATIC (u) = (TREE_CONSTANT (u)
5777 && (initializer_constant_valid_p (pfn, TREE_TYPE (pfn))
5778 != NULL_TREE)
5779 && (initializer_constant_valid_p (delta, TREE_TYPE (delta))
5780 != NULL_TREE));
5781 return u;
5784 /* Build a constructor for a pointer to member function. It can be
5785 used to initialize global variables, local variable, or used
5786 as a value in expressions. TYPE is the POINTER to METHOD_TYPE we
5787 want to be.
5789 If FORCE is nonzero, then force this conversion, even if
5790 we would rather not do it. Usually set when using an explicit
5791 cast.
5793 Return error_mark_node, if something goes wrong. */
5795 tree
5796 build_ptrmemfunc (type, pfn, force)
5797 tree type, pfn;
5798 int force;
5800 tree fn;
5801 tree pfn_type = TREE_TYPE (pfn);
5802 tree to_type = build_ptrmemfunc_type (type);
5804 /* Handle multiple conversions of pointer to member functions. */
5805 if (TYPE_PTRMEMFUNC_P (TREE_TYPE (pfn)))
5807 tree delta = NULL_TREE;
5808 tree npfn = NULL_TREE;
5809 tree n;
5811 if (!force
5812 && !can_convert_arg (to_type, TREE_TYPE (pfn), pfn))
5813 error ("invalid conversion to type `%T' from type `%T'",
5814 to_type, pfn_type);
5816 n = get_delta_difference (TYPE_PTRMEMFUNC_OBJECT_TYPE (pfn_type),
5817 TYPE_PTRMEMFUNC_OBJECT_TYPE (to_type),
5818 force);
5820 /* We don't have to do any conversion to convert a
5821 pointer-to-member to its own type. But, we don't want to
5822 just return a PTRMEM_CST if there's an explicit cast; that
5823 cast should make the expression an invalid template argument. */
5824 if (TREE_CODE (pfn) != PTRMEM_CST)
5826 if (same_type_p (to_type, pfn_type))
5827 return pfn;
5828 else if (integer_zerop (n))
5829 return build_reinterpret_cast (to_type, pfn);
5832 if (TREE_SIDE_EFFECTS (pfn))
5833 pfn = save_expr (pfn);
5835 /* Obtain the function pointer and the current DELTA. */
5836 if (TREE_CODE (pfn) == PTRMEM_CST)
5837 expand_ptrmemfunc_cst (pfn, &delta, &npfn);
5838 else
5840 npfn = build_ptrmemfunc_access_expr (pfn, pfn_identifier);
5841 delta = build_ptrmemfunc_access_expr (pfn, delta_identifier);
5844 /* Just adjust the DELTA field. */
5845 delta = cp_convert (ptrdiff_type_node, delta);
5846 if (TARGET_PTRMEMFUNC_VBIT_LOCATION == ptrmemfunc_vbit_in_delta)
5847 n = cp_build_binary_op (LSHIFT_EXPR, n, integer_one_node);
5848 delta = cp_build_binary_op (PLUS_EXPR, delta, n);
5849 return build_ptrmemfunc1 (to_type, delta, npfn);
5852 /* Handle null pointer to member function conversions. */
5853 if (integer_zerop (pfn))
5855 pfn = build_c_cast (type, integer_zero_node);
5856 return build_ptrmemfunc1 (to_type,
5857 integer_zero_node,
5858 pfn);
5861 if (type_unknown_p (pfn))
5862 return instantiate_type (type, pfn, tf_error | tf_warning);
5864 fn = TREE_OPERAND (pfn, 0);
5865 my_friendly_assert (TREE_CODE (fn) == FUNCTION_DECL, 0);
5866 return make_ptrmem_cst (to_type, fn);
5869 /* Return the DELTA, IDX, PFN, and DELTA2 values for the PTRMEM_CST
5870 given by CST.
5872 ??? There is no consistency as to the types returned for the above
5873 values. Some code acts as if its a sizetype and some as if its
5874 integer_type_node. */
5876 void
5877 expand_ptrmemfunc_cst (cst, delta, pfn)
5878 tree cst;
5879 tree *delta;
5880 tree *pfn;
5882 tree type = TREE_TYPE (cst);
5883 tree fn = PTRMEM_CST_MEMBER (cst);
5884 tree ptr_class, fn_class;
5886 my_friendly_assert (TREE_CODE (fn) == FUNCTION_DECL, 0);
5888 /* The class that the function belongs to. */
5889 fn_class = DECL_CONTEXT (fn);
5891 /* The class that we're creating a pointer to member of. */
5892 ptr_class = TYPE_PTRMEMFUNC_OBJECT_TYPE (type);
5894 /* First, calculate the adjustment to the function's class. */
5895 *delta = get_delta_difference (fn_class, ptr_class, /*force=*/0);
5897 if (!DECL_VIRTUAL_P (fn))
5898 *pfn = convert (TYPE_PTRMEMFUNC_FN_TYPE (type), build_addr_func (fn));
5899 else
5901 /* If we're dealing with a virtual function, we have to adjust 'this'
5902 again, to point to the base which provides the vtable entry for
5903 fn; the call will do the opposite adjustment. */
5904 tree orig_class = DECL_CONTEXT (fn);
5905 tree binfo = binfo_or_else (orig_class, fn_class);
5906 *delta = fold (build (PLUS_EXPR, TREE_TYPE (*delta),
5907 *delta, BINFO_OFFSET (binfo)));
5909 /* We set PFN to the vtable offset at which the function can be
5910 found, plus one (unless ptrmemfunc_vbit_in_delta, in which
5911 case delta is shifted left, and then incremented). */
5912 *pfn = DECL_VINDEX (fn);
5913 *pfn = fold (build (MULT_EXPR, integer_type_node, *pfn,
5914 TYPE_SIZE_UNIT (vtable_entry_type)));
5916 switch (TARGET_PTRMEMFUNC_VBIT_LOCATION)
5918 case ptrmemfunc_vbit_in_pfn:
5919 *pfn = fold (build (PLUS_EXPR, integer_type_node, *pfn,
5920 integer_one_node));
5921 break;
5923 case ptrmemfunc_vbit_in_delta:
5924 *delta = fold (build (LSHIFT_EXPR, TREE_TYPE (*delta),
5925 *delta, integer_one_node));
5926 *delta = fold (build (PLUS_EXPR, TREE_TYPE (*delta),
5927 *delta, integer_one_node));
5928 break;
5930 default:
5931 abort ();
5934 *pfn = fold (build1 (NOP_EXPR, TYPE_PTRMEMFUNC_FN_TYPE (type),
5935 *pfn));
5939 /* Return an expression for PFN from the pointer-to-member function
5940 given by T. */
5942 tree
5943 pfn_from_ptrmemfunc (t)
5944 tree t;
5946 if (TREE_CODE (t) == PTRMEM_CST)
5948 tree delta;
5949 tree pfn;
5951 expand_ptrmemfunc_cst (t, &delta, &pfn);
5952 if (pfn)
5953 return pfn;
5956 return build_ptrmemfunc_access_expr (t, pfn_identifier);
5959 /* Expression EXPR is about to be implicitly converted to TYPE. Warn
5960 if this is a potentially dangerous thing to do. Returns a possibly
5961 marked EXPR. */
5963 tree
5964 dubious_conversion_warnings (type, expr, errtype, fndecl, parmnum)
5965 tree type;
5966 tree expr;
5967 const char *errtype;
5968 tree fndecl;
5969 int parmnum;
5971 if (TREE_CODE (type) == REFERENCE_TYPE)
5972 type = TREE_TYPE (type);
5974 /* Issue warnings about peculiar, but valid, uses of NULL. */
5975 if (ARITHMETIC_TYPE_P (type) && expr == null_node)
5977 if (fndecl)
5978 warning ("passing NULL used for non-pointer %s %P of `%D'",
5979 errtype, parmnum, fndecl);
5980 else
5981 warning ("%s to non-pointer type `%T' from NULL", errtype, type);
5984 /* Warn about assigning a floating-point type to an integer type. */
5985 if (TREE_CODE (TREE_TYPE (expr)) == REAL_TYPE
5986 && TREE_CODE (type) == INTEGER_TYPE)
5988 if (fndecl)
5989 warning ("passing `%T' for %s %P of `%D'",
5990 TREE_TYPE (expr), errtype, parmnum, fndecl);
5991 else
5992 warning ("%s to `%T' from `%T'", errtype, type, TREE_TYPE (expr));
5994 /* And warn about assigning a negative value to an unsigned
5995 variable. */
5996 else if (TREE_UNSIGNED (type) && TREE_CODE (type) != BOOLEAN_TYPE)
5998 if (TREE_CODE (expr) == INTEGER_CST
5999 && TREE_NEGATED_INT (expr))
6001 if (fndecl)
6002 warning ("passing negative value `%E' for %s %P of `%D'",
6003 expr, errtype, parmnum, fndecl);
6004 else
6005 warning ("%s of negative value `%E' to `%T'",
6006 errtype, expr, type);
6009 overflow_warning (expr);
6011 if (TREE_CONSTANT (expr))
6012 expr = fold (expr);
6014 return expr;
6017 /* Convert value RHS to type TYPE as preparation for an assignment to
6018 an lvalue of type TYPE. ERRTYPE is a string to use in error
6019 messages: "assignment", "return", etc. If FNDECL is non-NULL, we
6020 are doing the conversion in order to pass the PARMNUMth argument of
6021 FNDECL. */
6023 static tree
6024 convert_for_assignment (type, rhs, errtype, fndecl, parmnum)
6025 tree type, rhs;
6026 const char *errtype;
6027 tree fndecl;
6028 int parmnum;
6030 register enum tree_code codel = TREE_CODE (type);
6031 register tree rhstype;
6032 register enum tree_code coder;
6034 if (codel == OFFSET_TYPE)
6035 abort ();
6037 if (TREE_CODE (rhs) == OFFSET_REF)
6038 rhs = resolve_offset_ref (rhs);
6040 /* Strip NON_LVALUE_EXPRs since we aren't using as an lvalue. */
6041 if (TREE_CODE (rhs) == NON_LVALUE_EXPR)
6042 rhs = TREE_OPERAND (rhs, 0);
6044 rhstype = TREE_TYPE (rhs);
6045 coder = TREE_CODE (rhstype);
6047 if (rhs == error_mark_node || rhstype == error_mark_node)
6048 return error_mark_node;
6049 if (TREE_CODE (rhs) == TREE_LIST && TREE_VALUE (rhs) == error_mark_node)
6050 return error_mark_node;
6052 rhs = dubious_conversion_warnings (type, rhs, errtype, fndecl, parmnum);
6054 /* The RHS of an assignment cannot have void type. */
6055 if (coder == VOID_TYPE)
6057 error ("void value not ignored as it ought to be");
6058 return error_mark_node;
6061 /* Simplify the RHS if possible. */
6062 if (TREE_CODE (rhs) == CONST_DECL)
6063 rhs = DECL_INITIAL (rhs);
6065 /* We do not use decl_constant_value here because of this case:
6067 const char* const s = "s";
6069 The conversion rules for a string literal are more lax than for a
6070 variable; in particular, a string literal can be converted to a
6071 "char *" but the variable "s" cannot be converted in the same
6072 way. If the conversion is allowed, the optimization should be
6073 performed while creating the converted expression. */
6075 /* [expr.ass]
6077 The expression is implicitly converted (clause _conv_) to the
6078 cv-unqualified type of the left operand.
6080 We allow bad conversions here because by the time we get to this point
6081 we are committed to doing the conversion. If we end up doing a bad
6082 conversion, convert_like will complain. */
6083 if (!can_convert_arg_bad (type, rhstype, rhs))
6085 /* When -Wno-pmf-conversions is use, we just silently allow
6086 conversions from pointers-to-members to plain pointers. If
6087 the conversion doesn't work, cp_convert will complain. */
6088 if (!warn_pmf2ptr
6089 && TYPE_PTR_P (type)
6090 && TYPE_PTRMEMFUNC_P (rhstype))
6091 rhs = cp_convert (strip_top_quals (type), rhs);
6092 else
6094 /* If the right-hand side has unknown type, then it is an
6095 overloaded function. Call instantiate_type to get error
6096 messages. */
6097 if (rhstype == unknown_type_node)
6098 instantiate_type (type, rhs, tf_error | tf_warning);
6099 else if (fndecl)
6100 error ("cannot convert `%T' to `%T' for argument `%P' to `%D'",
6101 rhstype, type, parmnum, fndecl);
6102 else
6103 error ("cannot convert `%T' to `%T' in %s", rhstype, type,
6104 errtype);
6105 return error_mark_node;
6108 return perform_implicit_conversion (strip_top_quals (type), rhs);
6111 /* Convert RHS to be of type TYPE.
6112 If EXP is nonzero, it is the target of the initialization.
6113 ERRTYPE is a string to use in error messages.
6115 Two major differences between the behavior of
6116 `convert_for_assignment' and `convert_for_initialization'
6117 are that references are bashed in the former, while
6118 copied in the latter, and aggregates are assigned in
6119 the former (operator=) while initialized in the
6120 latter (X(X&)).
6122 If using constructor make sure no conversion operator exists, if one does
6123 exist, an ambiguity exists.
6125 If flags doesn't include LOOKUP_COMPLAIN, don't complain about anything. */
6127 tree
6128 convert_for_initialization (exp, type, rhs, flags, errtype, fndecl, parmnum)
6129 tree exp, type, rhs;
6130 int flags;
6131 const char *errtype;
6132 tree fndecl;
6133 int parmnum;
6135 register enum tree_code codel = TREE_CODE (type);
6136 register tree rhstype;
6137 register enum tree_code coder;
6139 /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
6140 Strip such NOP_EXPRs, since RHS is used in non-lvalue context. */
6141 if (TREE_CODE (rhs) == NOP_EXPR
6142 && TREE_TYPE (rhs) == TREE_TYPE (TREE_OPERAND (rhs, 0))
6143 && codel != REFERENCE_TYPE)
6144 rhs = TREE_OPERAND (rhs, 0);
6146 if (rhs == error_mark_node
6147 || (TREE_CODE (rhs) == TREE_LIST && TREE_VALUE (rhs) == error_mark_node))
6148 return error_mark_node;
6150 if (TREE_CODE (rhs) == OFFSET_REF)
6152 rhs = resolve_offset_ref (rhs);
6153 if (rhs == error_mark_node)
6154 return error_mark_node;
6157 if (TREE_CODE (TREE_TYPE (rhs)) == REFERENCE_TYPE)
6158 rhs = convert_from_reference (rhs);
6160 if ((TREE_CODE (TREE_TYPE (rhs)) == ARRAY_TYPE
6161 && TREE_CODE (type) != ARRAY_TYPE
6162 && (TREE_CODE (type) != REFERENCE_TYPE
6163 || TREE_CODE (TREE_TYPE (type)) != ARRAY_TYPE))
6164 || (TREE_CODE (TREE_TYPE (rhs)) == FUNCTION_TYPE
6165 && (TREE_CODE (type) != REFERENCE_TYPE
6166 || TREE_CODE (TREE_TYPE (type)) != FUNCTION_TYPE))
6167 || TREE_CODE (TREE_TYPE (rhs)) == METHOD_TYPE)
6168 rhs = default_conversion (rhs);
6170 rhstype = TREE_TYPE (rhs);
6171 coder = TREE_CODE (rhstype);
6173 if (coder == ERROR_MARK)
6174 return error_mark_node;
6176 /* We accept references to incomplete types, so we can
6177 return here before checking if RHS is of complete type. */
6179 if (codel == REFERENCE_TYPE)
6181 /* This should eventually happen in convert_arguments. */
6182 int savew = 0, savee = 0;
6184 if (fndecl)
6185 savew = warningcount, savee = errorcount;
6186 rhs = initialize_reference (type, rhs);
6187 if (fndecl)
6189 if (warningcount > savew)
6190 cp_warning_at ("in passing argument %P of `%+D'", parmnum, fndecl);
6191 else if (errorcount > savee)
6192 cp_error_at ("in passing argument %P of `%+D'", parmnum, fndecl);
6194 return rhs;
6197 if (exp != 0)
6198 exp = require_complete_type (exp);
6199 if (exp == error_mark_node)
6200 return error_mark_node;
6202 if (TREE_CODE (rhstype) == REFERENCE_TYPE)
6203 rhstype = TREE_TYPE (rhstype);
6205 type = complete_type (type);
6207 if (IS_AGGR_TYPE (type))
6208 return ocp_convert (type, rhs, CONV_IMPLICIT|CONV_FORCE_TEMP, flags);
6210 return convert_for_assignment (type, rhs, errtype, fndecl, parmnum);
6213 /* Expand an ASM statement with operands, handling output operands
6214 that are not variables or INDIRECT_REFS by transforming such
6215 cases into cases that expand_asm_operands can handle.
6217 Arguments are same as for expand_asm_operands.
6219 We don't do default conversions on all inputs, because it can screw
6220 up operands that are expected to be in memory. */
6222 void
6223 c_expand_asm_operands (string, outputs, inputs, clobbers, vol, filename, line)
6224 tree string, outputs, inputs, clobbers;
6225 int vol;
6226 const char *filename;
6227 int line;
6229 int noutputs = list_length (outputs);
6230 register int i;
6231 /* o[I] is the place that output number I should be written. */
6232 register tree *o = (tree *) alloca (noutputs * sizeof (tree));
6233 register tree tail;
6235 /* Record the contents of OUTPUTS before it is modified. */
6236 for (i = 0, tail = outputs; tail; tail = TREE_CHAIN (tail), i++)
6237 o[i] = TREE_VALUE (tail);
6239 /* Generate the ASM_OPERANDS insn;
6240 store into the TREE_VALUEs of OUTPUTS some trees for
6241 where the values were actually stored. */
6242 expand_asm_operands (string, outputs, inputs, clobbers, vol, filename, line);
6244 /* Copy all the intermediate outputs into the specified outputs. */
6245 for (i = 0, tail = outputs; tail; tail = TREE_CHAIN (tail), i++)
6247 if (o[i] != TREE_VALUE (tail))
6249 expand_expr (build_modify_expr (o[i], NOP_EXPR, TREE_VALUE (tail)),
6250 const0_rtx, VOIDmode, EXPAND_NORMAL);
6251 free_temp_slots ();
6253 /* Restore the original value so that it's correct the next
6254 time we expand this function. */
6255 TREE_VALUE (tail) = o[i];
6257 /* Detect modification of read-only values.
6258 (Otherwise done by build_modify_expr.) */
6259 else
6261 tree type = TREE_TYPE (o[i]);
6262 if (type != error_mark_node
6263 && (CP_TYPE_CONST_P (type)
6264 || (CLASS_TYPE_P (type) && C_TYPE_FIELDS_READONLY (type))))
6265 readonly_error (o[i], "modification by `asm'", 1);
6269 /* Those MODIFY_EXPRs could do autoincrements. */
6270 emit_queue ();
6273 /* If RETVAL is the address of, or a reference to, a local variable or
6274 temporary give an appropraite warning. */
6276 static void
6277 maybe_warn_about_returning_address_of_local (retval)
6278 tree retval;
6280 tree valtype = TREE_TYPE (DECL_RESULT (current_function_decl));
6281 tree whats_returned = retval;
6283 for (;;)
6285 if (TREE_CODE (whats_returned) == COMPOUND_EXPR)
6286 whats_returned = TREE_OPERAND (whats_returned, 1);
6287 else if (TREE_CODE (whats_returned) == CONVERT_EXPR
6288 || TREE_CODE (whats_returned) == NON_LVALUE_EXPR
6289 || TREE_CODE (whats_returned) == NOP_EXPR)
6290 whats_returned = TREE_OPERAND (whats_returned, 0);
6291 else
6292 break;
6295 if (TREE_CODE (whats_returned) != ADDR_EXPR)
6296 return;
6297 whats_returned = TREE_OPERAND (whats_returned, 0);
6299 if (TREE_CODE (valtype) == REFERENCE_TYPE)
6301 if (TREE_CODE (whats_returned) == AGGR_INIT_EXPR
6302 || TREE_CODE (whats_returned) == TARGET_EXPR)
6304 /* Get the target. */
6305 whats_returned = TREE_OPERAND (whats_returned, 0);
6306 warning ("returning reference to temporary");
6307 return;
6309 if (TREE_CODE (whats_returned) == VAR_DECL
6310 && DECL_NAME (whats_returned)
6311 && TEMP_NAME_P (DECL_NAME (whats_returned)))
6313 warning ("reference to non-lvalue returned");
6314 return;
6318 if (TREE_CODE (whats_returned) == VAR_DECL
6319 && DECL_NAME (whats_returned)
6320 && DECL_FUNCTION_SCOPE_P (whats_returned)
6321 && !(TREE_STATIC (whats_returned)
6322 || TREE_PUBLIC (whats_returned)))
6324 if (TREE_CODE (valtype) == REFERENCE_TYPE)
6325 cp_warning_at ("reference to local variable `%D' returned",
6326 whats_returned);
6327 else
6328 cp_warning_at ("address of local variable `%D' returned",
6329 whats_returned);
6330 return;
6334 /* Check that returning RETVAL from the current function is valid.
6335 Return an expression explicitly showing all conversions required to
6336 change RETVAL into the function return type, and to assign it to
6337 the DECL_RESULT for the function. */
6339 tree
6340 check_return_expr (retval)
6341 tree retval;
6343 tree result;
6344 /* The type actually returned by the function, after any
6345 promotions. */
6346 tree valtype;
6347 int fn_returns_value_p;
6349 /* A `volatile' function is one that isn't supposed to return, ever.
6350 (This is a G++ extension, used to get better code for functions
6351 that call the `volatile' function.) */
6352 if (TREE_THIS_VOLATILE (current_function_decl))
6353 warning ("function declared `noreturn' has a `return' statement");
6355 /* Check for various simple errors. */
6356 if (DECL_DESTRUCTOR_P (current_function_decl))
6358 if (retval)
6359 error ("returning a value from a destructor");
6360 return NULL_TREE;
6362 else if (DECL_CONSTRUCTOR_P (current_function_decl))
6364 if (in_function_try_handler)
6365 /* If a return statement appears in a handler of the
6366 function-try-block of a constructor, the program is ill-formed. */
6367 error ("cannot return from a handler of a function-try-block of a constructor");
6368 else if (retval)
6369 /* You can't return a value from a constructor. */
6370 error ("returning a value from a constructor");
6371 return NULL_TREE;
6374 if (processing_template_decl)
6376 current_function_returns_value = 1;
6377 return retval;
6380 /* When no explicit return-value is given in a function with a named
6381 return value, the named return value is used. */
6382 result = DECL_RESULT (current_function_decl);
6383 valtype = TREE_TYPE (result);
6384 my_friendly_assert (valtype != NULL_TREE, 19990924);
6385 fn_returns_value_p = !VOID_TYPE_P (valtype);
6386 if (!retval && DECL_NAME (result) && fn_returns_value_p)
6387 retval = result;
6389 /* Check for a return statement with no return value in a function
6390 that's supposed to return a value. */
6391 if (!retval && fn_returns_value_p)
6393 pedwarn ("return-statement with no value, in function declared with a non-void return type");
6394 /* Clear this, so finish_function won't say that we reach the
6395 end of a non-void function (which we don't, we gave a
6396 return!). */
6397 current_function_returns_null = 0;
6399 /* Check for a return statement with a value in a function that
6400 isn't supposed to return a value. */
6401 else if (retval && !fn_returns_value_p)
6403 if (VOID_TYPE_P (TREE_TYPE (retval)))
6404 /* You can return a `void' value from a function of `void'
6405 type. In that case, we have to evaluate the expression for
6406 its side-effects. */
6407 finish_expr_stmt (retval);
6408 else
6409 pedwarn ("return-statement with a value, in function declared with a void return type");
6411 current_function_returns_null = 1;
6413 /* There's really no value to return, after all. */
6414 return NULL_TREE;
6416 else if (!retval)
6417 /* Remember that this function can sometimes return without a
6418 value. */
6419 current_function_returns_null = 1;
6420 else
6421 /* Remember that this function did return a value. */
6422 current_function_returns_value = 1;
6424 /* Only operator new(...) throw(), can return NULL [expr.new/13]. */
6425 if ((DECL_OVERLOADED_OPERATOR_P (current_function_decl) == NEW_EXPR
6426 || DECL_OVERLOADED_OPERATOR_P (current_function_decl) == VEC_NEW_EXPR)
6427 && !TYPE_NOTHROW_P (TREE_TYPE (current_function_decl))
6428 && ! flag_check_new
6429 && null_ptr_cst_p (retval))
6430 warning ("`operator new' must not return NULL unless it is declared `throw()' (or -fcheck-new is in effect)");
6432 /* Effective C++ rule 15. See also start_function. */
6433 if (warn_ecpp
6434 && DECL_NAME (current_function_decl) == ansi_assopname(NOP_EXPR)
6435 && retval != current_class_ref)
6436 warning ("`operator=' should return a reference to `*this'");
6438 /* The fabled Named Return Value optimization, as per [class.copy]/15:
6440 [...] For a function with a class return type, if the expression
6441 in the return statement is the name of a local object, and the cv-
6442 unqualified type of the local object is the same as the function
6443 return type, an implementation is permitted to omit creating the tem-
6444 porary object to hold the function return value [...]
6446 So, if this is a value-returning function that always returns the same
6447 local variable, remember it.
6449 It might be nice to be more flexible, and choose the first suitable
6450 variable even if the function sometimes returns something else, but
6451 then we run the risk of clobbering the variable we chose if the other
6452 returned expression uses the chosen variable somehow. And people expect
6453 this restriction, anyway. (jason 2000-11-19)
6455 See finish_function, genrtl_start_function, and declare_return_variable
6456 for other pieces of this optimization. */
6458 if (fn_returns_value_p && flag_elide_constructors)
6460 if (retval != NULL_TREE
6461 && (current_function_return_value == NULL_TREE
6462 || current_function_return_value == retval)
6463 && TREE_CODE (retval) == VAR_DECL
6464 && DECL_CONTEXT (retval) == current_function_decl
6465 && ! TREE_STATIC (retval)
6466 && (DECL_ALIGN (retval)
6467 >= DECL_ALIGN (DECL_RESULT (current_function_decl)))
6468 && same_type_p ((TYPE_MAIN_VARIANT
6469 (TREE_TYPE (retval))),
6470 (TYPE_MAIN_VARIANT
6471 (TREE_TYPE (TREE_TYPE (current_function_decl))))))
6472 current_function_return_value = retval;
6473 else
6474 current_function_return_value = error_mark_node;
6477 /* We don't need to do any conversions when there's nothing being
6478 returned. */
6479 if (!retval || retval == error_mark_node)
6480 return retval;
6482 /* Do any required conversions. */
6483 if (retval == result || DECL_CONSTRUCTOR_P (current_function_decl))
6484 /* No conversions are required. */
6486 else
6488 /* The type the function is declared to return. */
6489 tree functype = TREE_TYPE (TREE_TYPE (current_function_decl));
6491 /* First convert the value to the function's return type, then
6492 to the type of return value's location to handle the
6493 case that functype is smaller than the valtype. */
6494 retval = convert_for_initialization
6495 (NULL_TREE, functype, retval, LOOKUP_NORMAL|LOOKUP_ONLYCONVERTING,
6496 "return", NULL_TREE, 0);
6497 retval = convert (valtype, retval);
6499 /* If the conversion failed, treat this just like `return;'. */
6500 if (retval == error_mark_node)
6501 return retval;
6502 /* We can't initialize a register from a AGGR_INIT_EXPR. */
6503 else if (! current_function_returns_struct
6504 && TREE_CODE (retval) == TARGET_EXPR
6505 && TREE_CODE (TREE_OPERAND (retval, 1)) == AGGR_INIT_EXPR)
6506 retval = build (COMPOUND_EXPR, TREE_TYPE (retval), retval,
6507 TREE_OPERAND (retval, 0));
6508 else
6509 maybe_warn_about_returning_address_of_local (retval);
6512 /* Actually copy the value returned into the appropriate location. */
6513 if (retval && retval != result)
6514 retval = build (INIT_EXPR, TREE_TYPE (result), result, retval);
6516 return retval;
6520 /* Returns nonzero if the pointer-type FROM can be converted to the
6521 pointer-type TO via a qualification conversion. If CONSTP is -1,
6522 then we return nonzero if the pointers are similar, and the
6523 cv-qualification signature of FROM is a proper subset of that of TO.
6525 If CONSTP is positive, then all outer pointers have been
6526 const-qualified. */
6528 static int
6529 comp_ptr_ttypes_real (to, from, constp)
6530 tree to, from;
6531 int constp;
6533 int to_more_cv_qualified = 0;
6535 for (; ; to = TREE_TYPE (to), from = TREE_TYPE (from))
6537 if (TREE_CODE (to) != TREE_CODE (from))
6538 return 0;
6540 if (TREE_CODE (from) == OFFSET_TYPE
6541 && same_type_p (TYPE_OFFSET_BASETYPE (from),
6542 TYPE_OFFSET_BASETYPE (to)))
6543 continue;
6545 /* Const and volatile mean something different for function types,
6546 so the usual checks are not appropriate. */
6547 if (TREE_CODE (to) != FUNCTION_TYPE && TREE_CODE (to) != METHOD_TYPE)
6549 if (!at_least_as_qualified_p (to, from))
6550 return 0;
6552 if (!at_least_as_qualified_p (from, to))
6554 if (constp == 0)
6555 return 0;
6556 else
6557 ++to_more_cv_qualified;
6560 if (constp > 0)
6561 constp &= TYPE_READONLY (to);
6564 if (TREE_CODE (to) != POINTER_TYPE)
6565 return
6566 same_type_ignoring_top_level_qualifiers_p (to, from)
6567 && (constp >= 0 || to_more_cv_qualified);
6571 /* When comparing, say, char ** to char const **, this function takes the
6572 'char *' and 'char const *'. Do not pass non-pointer types to this
6573 function. */
6576 comp_ptr_ttypes (to, from)
6577 tree to, from;
6579 return comp_ptr_ttypes_real (to, from, 1);
6582 /* Returns 1 if to and from are (possibly multi-level) pointers to the same
6583 type or inheritance-related types, regardless of cv-quals. */
6586 ptr_reasonably_similar (to, from)
6587 tree to, from;
6589 for (; ; to = TREE_TYPE (to), from = TREE_TYPE (from))
6591 /* Any target type is similar enough to void. */
6592 if (TREE_CODE (to) == VOID_TYPE
6593 || TREE_CODE (from) == VOID_TYPE)
6594 return 1;
6596 if (TREE_CODE (to) != TREE_CODE (from))
6597 return 0;
6599 if (TREE_CODE (from) == OFFSET_TYPE
6600 && comptypes (TYPE_OFFSET_BASETYPE (to),
6601 TYPE_OFFSET_BASETYPE (from),
6602 COMPARE_BASE | COMPARE_RELAXED))
6603 continue;
6605 if (TREE_CODE (to) == INTEGER_TYPE
6606 && TYPE_PRECISION (to) == TYPE_PRECISION (from))
6607 return 1;
6609 if (TREE_CODE (to) == FUNCTION_TYPE)
6610 return 1;
6612 if (TREE_CODE (to) != POINTER_TYPE)
6613 return comptypes
6614 (TYPE_MAIN_VARIANT (to), TYPE_MAIN_VARIANT (from),
6615 COMPARE_BASE | COMPARE_RELAXED);
6619 /* Like comp_ptr_ttypes, for const_cast. */
6621 static int
6622 comp_ptr_ttypes_const (to, from)
6623 tree to, from;
6625 for (; ; to = TREE_TYPE (to), from = TREE_TYPE (from))
6627 if (TREE_CODE (to) != TREE_CODE (from))
6628 return 0;
6630 if (TREE_CODE (from) == OFFSET_TYPE
6631 && same_type_p (TYPE_OFFSET_BASETYPE (from),
6632 TYPE_OFFSET_BASETYPE (to)))
6633 continue;
6635 if (TREE_CODE (to) != POINTER_TYPE)
6636 return same_type_ignoring_top_level_qualifiers_p (to, from);
6640 /* Like comp_ptr_ttypes, for reinterpret_cast. */
6642 static int
6643 comp_ptr_ttypes_reinterpret (to, from)
6644 tree to, from;
6646 int constp = 1;
6648 for (; ; to = TREE_TYPE (to), from = TREE_TYPE (from))
6650 if (TREE_CODE (from) == OFFSET_TYPE)
6651 from = TREE_TYPE (from);
6652 if (TREE_CODE (to) == OFFSET_TYPE)
6653 to = TREE_TYPE (to);
6655 /* Const and volatile mean something different for function types,
6656 so the usual checks are not appropriate. */
6657 if (TREE_CODE (from) != FUNCTION_TYPE && TREE_CODE (from) != METHOD_TYPE
6658 && TREE_CODE (to) != FUNCTION_TYPE && TREE_CODE (to) != METHOD_TYPE)
6660 if (!at_least_as_qualified_p (to, from))
6661 return 0;
6663 if (! constp
6664 && !at_least_as_qualified_p (from, to))
6665 return 0;
6666 constp &= TYPE_READONLY (to);
6669 if (TREE_CODE (from) != POINTER_TYPE
6670 || TREE_CODE (to) != POINTER_TYPE)
6671 return 1;
6675 /* Returns the type qualifiers for this type, including the qualifiers on the
6676 elements for an array type. */
6679 cp_type_quals (type)
6680 tree type;
6682 type = strip_array_types (type);
6683 if (type == error_mark_node)
6684 return TYPE_UNQUALIFIED;
6685 return TYPE_QUALS (type);
6688 /* Returns nonzero if the TYPE contains a mutable member */
6691 cp_has_mutable_p (type)
6692 tree type;
6694 type = strip_array_types (type);
6696 return CLASS_TYPE_P (type) && CLASSTYPE_HAS_MUTABLE (type);
6699 /* Subroutine of casts_away_constness. Make T1 and T2 point at
6700 exemplar types such that casting T1 to T2 is casting away castness
6701 if and only if there is no implicit conversion from T1 to T2. */
6703 static void
6704 casts_away_constness_r (t1, t2)
6705 tree *t1;
6706 tree *t2;
6708 int quals1;
6709 int quals2;
6711 /* [expr.const.cast]
6713 For multi-level pointer to members and multi-level mixed pointers
6714 and pointers to members (conv.qual), the "member" aspect of a
6715 pointer to member level is ignored when determining if a const
6716 cv-qualifier has been cast away. */
6717 if (TYPE_PTRMEM_P (*t1))
6718 *t1 = build_pointer_type (TREE_TYPE (TREE_TYPE (*t1)));
6719 if (TYPE_PTRMEM_P (*t2))
6720 *t2 = build_pointer_type (TREE_TYPE (TREE_TYPE (*t2)));
6722 /* [expr.const.cast]
6724 For two pointer types:
6726 X1 is T1cv1,1 * ... cv1,N * where T1 is not a pointer type
6727 X2 is T2cv2,1 * ... cv2,M * where T2 is not a pointer type
6728 K is min(N,M)
6730 casting from X1 to X2 casts away constness if, for a non-pointer
6731 type T there does not exist an implicit conversion (clause
6732 _conv_) from:
6734 Tcv1,(N-K+1) * cv1,(N-K+2) * ... cv1,N *
6738 Tcv2,(M-K+1) * cv2,(M-K+2) * ... cv2,M *. */
6740 if (TREE_CODE (*t1) != POINTER_TYPE
6741 || TREE_CODE (*t2) != POINTER_TYPE)
6743 *t1 = cp_build_qualified_type (void_type_node,
6744 cp_type_quals (*t1));
6745 *t2 = cp_build_qualified_type (void_type_node,
6746 cp_type_quals (*t2));
6747 return;
6750 quals1 = cp_type_quals (*t1);
6751 quals2 = cp_type_quals (*t2);
6752 *t1 = TREE_TYPE (*t1);
6753 *t2 = TREE_TYPE (*t2);
6754 casts_away_constness_r (t1, t2);
6755 *t1 = build_pointer_type (*t1);
6756 *t2 = build_pointer_type (*t2);
6757 *t1 = cp_build_qualified_type (*t1, quals1);
6758 *t2 = cp_build_qualified_type (*t2, quals2);
6761 /* Returns nonzero if casting from TYPE1 to TYPE2 casts away
6762 constness. */
6764 static int
6765 casts_away_constness (t1, t2)
6766 tree t1;
6767 tree t2;
6769 if (TREE_CODE (t2) == REFERENCE_TYPE)
6771 /* [expr.const.cast]
6773 Casting from an lvalue of type T1 to an lvalue of type T2
6774 using a reference cast casts away constness if a cast from an
6775 rvalue of type "pointer to T1" to the type "pointer to T2"
6776 casts away constness. */
6777 t1 = (TREE_CODE (t1) == REFERENCE_TYPE
6778 ? TREE_TYPE (t1) : t1);
6779 return casts_away_constness (build_pointer_type (t1),
6780 build_pointer_type (TREE_TYPE (t2)));
6783 if (TYPE_PTRMEM_P (t1) && TYPE_PTRMEM_P (t2))
6784 /* [expr.const.cast]
6786 Casting from an rvalue of type "pointer to data member of X
6787 of type T1" to the type "pointer to data member of Y of type
6788 T2" casts away constness if a cast from an rvalue of type
6789 "pointer to T1" to the type "pointer to T2" casts away
6790 constness. */
6791 return casts_away_constness
6792 (build_pointer_type (TREE_TYPE (TREE_TYPE (t1))),
6793 build_pointer_type (TREE_TYPE (TREE_TYPE (t2))));
6795 /* Casting away constness is only something that makes sense for
6796 pointer or reference types. */
6797 if (TREE_CODE (t1) != POINTER_TYPE
6798 || TREE_CODE (t2) != POINTER_TYPE)
6799 return 0;
6801 /* Top-level qualifiers don't matter. */
6802 t1 = TYPE_MAIN_VARIANT (t1);
6803 t2 = TYPE_MAIN_VARIANT (t2);
6804 casts_away_constness_r (&t1, &t2);
6805 if (!can_convert (t2, t1))
6806 return 1;
6808 return 0;
6811 /* Returns TYPE with its cv qualifiers removed
6812 TYPE is T cv* .. *cv where T is not a pointer type,
6813 returns T * .. *. (If T is an array type, then the cv qualifiers
6814 above are those of the array members.) */
6816 static tree
6817 strip_all_pointer_quals (type)
6818 tree type;
6820 if (TREE_CODE (type) == POINTER_TYPE)
6821 return build_pointer_type (strip_all_pointer_quals (TREE_TYPE (type)));
6822 else if (TREE_CODE (type) == OFFSET_TYPE)
6823 return build_offset_type (TYPE_OFFSET_BASETYPE (type),
6824 strip_all_pointer_quals (TREE_TYPE (type)));
6825 else
6826 return TYPE_MAIN_VARIANT (type);