Daily bump.
[official-gcc.git] / gcc / cp / typeck.c
blob5e69b98fc9dabe0829108f2b6167516732db9505
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 Free Software Foundation, Inc.
4 Hacked by Michael Tiemann (tiemann@cygnus.com)
6 This file is part of GNU CC.
8 GNU CC 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 GNU CC 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 GNU CC; 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 "tree.h"
36 #include "rtl.h"
37 #include "expr.h"
38 #include "cp-tree.h"
39 #include "tm_p.h"
40 #include "flags.h"
41 #include "output.h"
42 #include "toplev.h"
43 #include "diagnostic.h"
44 #include "target.h"
46 static tree convert_for_assignment PARAMS ((tree, tree, const char *, tree,
47 int));
48 static tree pointer_int_sum PARAMS ((enum tree_code, tree, tree));
49 static tree rationalize_conditional_expr PARAMS ((enum tree_code, tree));
50 static int comp_target_parms PARAMS ((tree, tree));
51 static int comp_ptr_ttypes_real PARAMS ((tree, tree, int));
52 static int comp_ptr_ttypes_const PARAMS ((tree, tree));
53 static int comp_ptr_ttypes_reinterpret PARAMS ((tree, tree));
54 static int comp_except_types PARAMS ((tree, tree, int));
55 static int comp_array_types PARAMS ((int (*) (tree, tree, int), tree,
56 tree, int));
57 static tree common_base_type PARAMS ((tree, tree));
58 static tree lookup_anon_field PARAMS ((tree, tree));
59 static tree pointer_diff PARAMS ((tree, tree, tree));
60 static tree build_component_addr PARAMS ((tree, tree));
61 static tree qualify_type_recursive PARAMS ((tree, tree));
62 static tree get_delta_difference PARAMS ((tree, tree, int));
63 static int comp_cv_target_types PARAMS ((tree, tree, int));
64 static void casts_away_constness_r PARAMS ((tree *, tree *));
65 static int casts_away_constness PARAMS ((tree, tree));
66 static void maybe_warn_about_returning_address_of_local PARAMS ((tree));
67 static tree strip_all_pointer_quals PARAMS ((tree));
69 /* Return the target type of TYPE, which means return T for:
70 T*, T&, T[], T (...), and otherwise, just T. */
72 tree
73 target_type (type)
74 tree type;
76 if (TREE_CODE (type) == REFERENCE_TYPE)
77 type = TREE_TYPE (type);
78 while (TREE_CODE (type) == POINTER_TYPE
79 || TREE_CODE (type) == ARRAY_TYPE
80 || TREE_CODE (type) == FUNCTION_TYPE
81 || TREE_CODE (type) == METHOD_TYPE
82 || TREE_CODE (type) == OFFSET_TYPE)
83 type = TREE_TYPE (type);
84 return type;
87 /* Do `exp = require_complete_type (exp);' to make sure exp
88 does not have an incomplete type. (That includes void types.)
89 Returns the error_mark_node if the VALUE does not have
90 complete type when this function returns. */
92 tree
93 require_complete_type (value)
94 tree value;
96 tree type;
98 if (processing_template_decl || value == error_mark_node)
99 return value;
101 if (TREE_CODE (value) == OVERLOAD)
102 type = unknown_type_node;
103 else
104 type = TREE_TYPE (value);
106 /* First, detect a valid value with a complete type. */
107 if (COMPLETE_TYPE_P (type))
108 return value;
110 /* If we see X::Y, we build an OFFSET_TYPE which has
111 not been laid out. Try to avoid an error by interpreting
112 it as this->X::Y, if reasonable. */
113 if (TREE_CODE (value) == OFFSET_REF
114 && current_class_ref != 0
115 && TREE_OPERAND (value, 0) == current_class_ref)
117 value = resolve_offset_ref (value);
118 return require_complete_type (value);
121 if (complete_type_or_else (type, value))
122 return value;
123 else
124 return error_mark_node;
127 /* Try to complete TYPE, if it is incomplete. For example, if TYPE is
128 a template instantiation, do the instantiation. Returns TYPE,
129 whether or not it could be completed, unless something goes
130 horribly wrong, in which case the error_mark_node is returned. */
132 tree
133 complete_type (type)
134 tree type;
136 if (type == NULL_TREE)
137 /* Rather than crash, we return something sure to cause an error
138 at some point. */
139 return error_mark_node;
141 if (type == error_mark_node || COMPLETE_TYPE_P (type))
143 else if (TREE_CODE (type) == ARRAY_TYPE && TYPE_DOMAIN (type))
145 tree t = complete_type (TREE_TYPE (type));
146 if (COMPLETE_TYPE_P (t) && ! processing_template_decl)
147 layout_type (type);
148 TYPE_NEEDS_CONSTRUCTING (type)
149 = TYPE_NEEDS_CONSTRUCTING (TYPE_MAIN_VARIANT (t));
150 TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type)
151 = TYPE_HAS_NONTRIVIAL_DESTRUCTOR (TYPE_MAIN_VARIANT (t));
153 else if (CLASS_TYPE_P (type) && CLASSTYPE_TEMPLATE_INSTANTIATION (type))
154 instantiate_class_template (TYPE_MAIN_VARIANT (type));
156 return type;
159 /* Like complete_type, but issue an error if the TYPE cannot be
160 completed. VALUE is used for informative diagnostics.
161 Returns NULL_TREE if the type cannot be made complete. */
163 tree
164 complete_type_or_else (type, value)
165 tree type;
166 tree value;
168 type = complete_type (type);
169 if (type == error_mark_node)
170 /* We already issued an error. */
171 return NULL_TREE;
172 else if (!COMPLETE_TYPE_P (type))
174 incomplete_type_error (value, type);
175 return NULL_TREE;
177 else
178 return type;
181 /* Return truthvalue of whether type of EXP is instantiated. */
184 type_unknown_p (exp)
185 tree exp;
187 return (TREE_CODE (exp) == OVERLOAD
188 || TREE_CODE (exp) == TREE_LIST
189 || TREE_TYPE (exp) == unknown_type_node
190 || (TREE_CODE (TREE_TYPE (exp)) == OFFSET_TYPE
191 && TREE_TYPE (TREE_TYPE (exp)) == unknown_type_node));
194 /* Return a pointer or pointer to member type similar to T1, with a
195 cv-qualification signature that is the union of the cv-qualification
196 signatures of T1 and T2: [expr.rel], [expr.eq]. */
198 static tree
199 qualify_type_recursive (t1, t2)
200 tree t1, t2;
202 if ((TYPE_PTR_P (t1) && TYPE_PTR_P (t2))
203 || (TYPE_PTRMEM_P (t1) && TYPE_PTRMEM_P (t2)))
205 tree tt1 = TREE_TYPE (t1);
206 tree tt2 = TREE_TYPE (t2);
207 tree b1;
208 int type_quals;
209 tree tgt;
210 tree attributes = (*targetm.merge_type_attributes) (t1, t2);
212 if (TREE_CODE (tt1) == OFFSET_TYPE)
214 b1 = TYPE_OFFSET_BASETYPE (tt1);
215 tt1 = TREE_TYPE (tt1);
216 tt2 = TREE_TYPE (tt2);
218 else
219 b1 = NULL_TREE;
221 type_quals = (cp_type_quals (tt1) | cp_type_quals (tt2));
222 tgt = qualify_type_recursive (tt1, tt2);
223 tgt = cp_build_qualified_type (tgt, type_quals);
224 if (b1)
225 tgt = build_offset_type (b1, tgt);
226 t1 = build_pointer_type (tgt);
227 t1 = build_type_attribute_variant (t1, attributes);
229 return t1;
232 /* Return the common type of two parameter lists.
233 We assume that comptypes has already been done and returned 1;
234 if that isn't so, this may crash.
236 As an optimization, free the space we allocate if the parameter
237 lists are already common. */
239 tree
240 commonparms (p1, p2)
241 tree p1, p2;
243 tree oldargs = p1, newargs, n;
244 int i, len;
245 int any_change = 0;
247 len = list_length (p1);
248 newargs = tree_last (p1);
250 if (newargs == void_list_node)
251 i = 1;
252 else
254 i = 0;
255 newargs = 0;
258 for (; i < len; i++)
259 newargs = tree_cons (NULL_TREE, NULL_TREE, newargs);
261 n = newargs;
263 for (i = 0; p1;
264 p1 = TREE_CHAIN (p1), p2 = TREE_CHAIN (p2), n = TREE_CHAIN (n), i++)
266 if (TREE_PURPOSE (p1) && !TREE_PURPOSE (p2))
268 TREE_PURPOSE (n) = TREE_PURPOSE (p1);
269 any_change = 1;
271 else if (! TREE_PURPOSE (p1))
273 if (TREE_PURPOSE (p2))
275 TREE_PURPOSE (n) = TREE_PURPOSE (p2);
276 any_change = 1;
279 else
281 if (1 != simple_cst_equal (TREE_PURPOSE (p1), TREE_PURPOSE (p2)))
282 any_change = 1;
283 TREE_PURPOSE (n) = TREE_PURPOSE (p2);
285 if (TREE_VALUE (p1) != TREE_VALUE (p2))
287 any_change = 1;
288 TREE_VALUE (n) = common_type (TREE_VALUE (p1), TREE_VALUE (p2));
290 else
291 TREE_VALUE (n) = TREE_VALUE (p1);
293 if (! any_change)
294 return oldargs;
296 return newargs;
299 /* Given a type, perhaps copied for a typedef,
300 find the "original" version of it. */
301 tree
302 original_type (t)
303 tree t;
305 while (TYPE_NAME (t) != NULL_TREE)
307 tree x = TYPE_NAME (t);
308 if (TREE_CODE (x) != TYPE_DECL)
309 break;
310 x = DECL_ORIGINAL_TYPE (x);
311 if (x == NULL_TREE)
312 break;
313 t = x;
315 return t;
318 /* T1 and T2 are arithmetic or enumeration types. Return the type
319 that will result from the "usual arithmetic conversions" on T1 and
320 T2 as described in [expr]. */
322 tree
323 type_after_usual_arithmetic_conversions (t1, t2)
324 tree t1;
325 tree t2;
327 enum tree_code code1 = TREE_CODE (t1);
328 enum tree_code code2 = TREE_CODE (t2);
329 tree attributes;
331 /* FIXME: Attributes. */
332 my_friendly_assert (ARITHMETIC_TYPE_P (t1)
333 || TREE_CODE (t1) == ENUMERAL_TYPE,
334 19990725);
335 my_friendly_assert (ARITHMETIC_TYPE_P (t2)
336 || TREE_CODE (t2) == ENUMERAL_TYPE,
337 19990725);
339 /* In what follows, we slightly generalize the rules given in [expr]
340 so as to deal with `long long'. First, merge the attributes. */
341 attributes = (*targetm.merge_type_attributes) (t1, t2);
343 /* If only one is real, use it as the result. */
344 if (code1 == REAL_TYPE && code2 != REAL_TYPE)
345 return build_type_attribute_variant (t1, attributes);
346 if (code2 == REAL_TYPE && code1 != REAL_TYPE)
347 return build_type_attribute_variant (t2, attributes);
349 /* Perform the integral promotions. */
350 if (code1 != REAL_TYPE)
352 t1 = type_promotes_to (t1);
353 t2 = type_promotes_to (t2);
356 /* Both real or both integers; use the one with greater precision. */
357 if (TYPE_PRECISION (t1) > TYPE_PRECISION (t2))
358 return build_type_attribute_variant (t1, attributes);
359 else if (TYPE_PRECISION (t2) > TYPE_PRECISION (t1))
360 return build_type_attribute_variant (t2, attributes);
362 if (code1 != REAL_TYPE)
364 /* If one is a sizetype, use it so size_binop doesn't blow up. */
365 if (TYPE_IS_SIZETYPE (t1) > TYPE_IS_SIZETYPE (t2))
366 return build_type_attribute_variant (t1, attributes);
367 if (TYPE_IS_SIZETYPE (t2) > TYPE_IS_SIZETYPE (t1))
368 return build_type_attribute_variant (t2, attributes);
370 /* If one is unsigned long long, then convert the other to unsigned
371 long long. */
372 if (same_type_p (TYPE_MAIN_VARIANT (t1), long_long_unsigned_type_node)
373 || same_type_p (TYPE_MAIN_VARIANT (t2), long_long_unsigned_type_node))
374 return build_type_attribute_variant (long_long_unsigned_type_node,
375 attributes);
376 /* If one is a long long, and the other is an unsigned long, and
377 long long can represent all the values of an unsigned long, then
378 convert to a long long. Otherwise, convert to an unsigned long
379 long. Otherwise, if either operand is long long, convert the
380 other to long long.
382 Since we're here, we know the TYPE_PRECISION is the same;
383 therefore converting to long long cannot represent all the values
384 of an unsigned long, so we choose unsigned long long in that
385 case. */
386 if (same_type_p (TYPE_MAIN_VARIANT (t1), long_long_integer_type_node)
387 || same_type_p (TYPE_MAIN_VARIANT (t2), long_long_integer_type_node))
389 tree t = ((TREE_UNSIGNED (t1) || TREE_UNSIGNED (t2))
390 ? long_long_unsigned_type_node
391 : long_long_integer_type_node);
392 return build_type_attribute_variant (t, attributes);
395 /* Go through the same procedure, but for longs. */
396 if (same_type_p (TYPE_MAIN_VARIANT (t1), long_unsigned_type_node)
397 || same_type_p (TYPE_MAIN_VARIANT (t2), long_unsigned_type_node))
398 return build_type_attribute_variant (long_unsigned_type_node,
399 attributes);
400 if (same_type_p (TYPE_MAIN_VARIANT (t1), long_integer_type_node)
401 || same_type_p (TYPE_MAIN_VARIANT (t2), long_integer_type_node))
403 tree t = ((TREE_UNSIGNED (t1) || TREE_UNSIGNED (t2))
404 ? long_unsigned_type_node : long_integer_type_node);
405 return build_type_attribute_variant (t, attributes);
407 /* Otherwise prefer the unsigned one. */
408 if (TREE_UNSIGNED (t1))
409 return build_type_attribute_variant (t1, attributes);
410 else
411 return build_type_attribute_variant (t2, attributes);
413 else
415 if (same_type_p (TYPE_MAIN_VARIANT (t1), long_double_type_node)
416 || same_type_p (TYPE_MAIN_VARIANT (t2), long_double_type_node))
417 return build_type_attribute_variant (long_double_type_node,
418 attributes);
419 if (same_type_p (TYPE_MAIN_VARIANT (t1), double_type_node)
420 || same_type_p (TYPE_MAIN_VARIANT (t2), double_type_node))
421 return build_type_attribute_variant (double_type_node,
422 attributes);
423 else
424 return build_type_attribute_variant (float_type_node,
425 attributes);
429 /* Return the composite pointer type (see [expr.rel]) for T1 and T2.
430 ARG1 and ARG2 are the values with those types. The LOCATION is a
431 string describing the current location, in case an error occurs. */
433 tree
434 composite_pointer_type (t1, t2, arg1, arg2, location)
435 tree t1;
436 tree t2;
437 tree arg1;
438 tree arg2;
439 const char* location;
441 tree result_type;
443 /* [expr.rel]
445 If one operand is a null pointer constant, the composite pointer
446 type is the type of the other operand. */
447 if (null_ptr_cst_p (arg1))
448 return t2;
449 if (null_ptr_cst_p (arg2))
450 return t1;
452 /* Deal with pointer-to-member functions in the same way as we deal
453 with pointers to functions. */
454 if (TYPE_PTRMEMFUNC_P (t1))
455 t1 = TYPE_PTRMEMFUNC_FN_TYPE (t1);
456 if (TYPE_PTRMEMFUNC_P (t2))
457 t2 = TYPE_PTRMEMFUNC_FN_TYPE (t2);
459 /* We have:
461 [expr.rel]
463 If one of the operands has type "pointer to cv1 void*", then
464 the other has type "pointer to cv2T", and the composite pointer
465 type is "pointer to cv12 void", where cv12 is the union of cv1
466 and cv2.
468 If either type is a pointer to void, make sure it is T1. */
469 if (VOID_TYPE_P (TREE_TYPE (t2)))
471 tree t;
472 t = t1;
473 t1 = t2;
474 t2 = t;
476 /* Now, if T1 is a pointer to void, merge the qualifiers. */
477 if (VOID_TYPE_P (TREE_TYPE (t1)))
479 if (pedantic && TYPE_PTRFN_P (t2))
480 pedwarn ("ISO C++ forbids %s between pointer of type `void *' and pointer-to-function", location);
481 t1 = TREE_TYPE (t1);
482 t2 = TREE_TYPE (t2);
483 result_type = cp_build_qualified_type (void_type_node,
484 (cp_type_quals (t1)
485 | cp_type_quals (t2)));
486 result_type = build_pointer_type (result_type);
488 else
490 tree full1 = qualify_type_recursive (t1, t2);
491 tree full2 = qualify_type_recursive (t2, t1);
493 int val = comp_target_types (full1, full2, 1);
495 if (val > 0)
496 result_type = full1;
497 else if (val < 0)
498 result_type = full2;
499 else
501 pedwarn ("%s between distinct pointer types `%T' and `%T' lacks a cast",
502 location, t1, t2);
503 result_type = ptr_type_node;
507 return result_type;
510 /* Return the common type of two types.
511 We assume that comptypes has already been done and returned 1;
512 if that isn't so, this may crash.
514 This is the type for the result of most arithmetic operations
515 if the operands have the given two types.
517 We do not deal with enumeral types here because they have already been
518 converted to integer types. */
520 tree
521 common_type (t1, t2)
522 tree t1, t2;
524 register enum tree_code code1;
525 register enum tree_code code2;
526 tree attributes;
528 /* Save time if the two types are the same. */
529 if (t1 == t2)
530 return t1;
531 t1 = original_type (t1);
532 t2 = original_type (t2);
533 if (t1 == t2)
534 return t1;
536 /* If one type is nonsense, use the other. */
537 if (t1 == error_mark_node)
538 return t2;
539 if (t2 == error_mark_node)
540 return t1;
542 if ((ARITHMETIC_TYPE_P (t1) || TREE_CODE (t1) == ENUMERAL_TYPE)
543 && (ARITHMETIC_TYPE_P (t2) || TREE_CODE (t2) == ENUMERAL_TYPE))
544 return type_after_usual_arithmetic_conversions (t1, t2);
546 /* Merge the attributes. */
547 attributes = (*targetm.merge_type_attributes) (t1, t2);
549 /* Treat an enum type as the unsigned integer type of the same width. */
551 if (TREE_CODE (t1) == ENUMERAL_TYPE)
552 t1 = type_for_size (TYPE_PRECISION (t1), 1);
553 if (TREE_CODE (t2) == ENUMERAL_TYPE)
554 t2 = type_for_size (TYPE_PRECISION (t2), 1);
556 if (TYPE_PTRMEMFUNC_P (t1))
557 t1 = TYPE_PTRMEMFUNC_FN_TYPE (t1);
558 if (TYPE_PTRMEMFUNC_P (t2))
559 t2 = TYPE_PTRMEMFUNC_FN_TYPE (t2);
561 code1 = TREE_CODE (t1);
562 code2 = TREE_CODE (t2);
564 /* If one type is complex, form the common type of the non-complex
565 components, then make that complex. Use T1 or T2 if it is the
566 required type. */
567 if (code1 == COMPLEX_TYPE || code2 == COMPLEX_TYPE)
569 tree subtype1 = code1 == COMPLEX_TYPE ? TREE_TYPE (t1) : t1;
570 tree subtype2 = code2 == COMPLEX_TYPE ? TREE_TYPE (t2) : t2;
571 tree subtype = common_type (subtype1, subtype2);
573 if (code1 == COMPLEX_TYPE && TREE_TYPE (t1) == subtype)
574 return build_type_attribute_variant (t1, attributes);
575 else if (code2 == COMPLEX_TYPE && TREE_TYPE (t2) == subtype)
576 return build_type_attribute_variant (t2, attributes);
577 else
578 return build_type_attribute_variant (build_complex_type (subtype),
579 attributes);
582 switch (code1)
584 case INTEGER_TYPE:
585 case REAL_TYPE:
586 /* We should have called type_after_usual_arithmetic_conversions
587 above. */
588 abort ();
589 break;
591 case POINTER_TYPE:
592 case REFERENCE_TYPE:
593 /* For two pointers, do this recursively on the target type,
594 and combine the qualifiers of the two types' targets. */
595 /* This code was turned off; I don't know why.
596 But ISO C++ specifies doing this with the qualifiers.
597 So I turned it on again. */
599 tree tt1 = TREE_TYPE (t1);
600 tree tt2 = TREE_TYPE (t2);
601 tree b1, b2;
602 int type_quals;
603 tree target;
605 if (TREE_CODE (tt1) == OFFSET_TYPE)
607 b1 = TYPE_OFFSET_BASETYPE (tt1);
608 b2 = TYPE_OFFSET_BASETYPE (tt2);
609 tt1 = TREE_TYPE (tt1);
610 tt2 = TREE_TYPE (tt2);
612 else
613 b1 = b2 = NULL_TREE;
615 type_quals = (cp_type_quals (tt1) | cp_type_quals (tt2));
616 tt1 = TYPE_MAIN_VARIANT (tt1);
617 tt2 = TYPE_MAIN_VARIANT (tt2);
619 if (tt1 == tt2)
620 target = tt1;
621 else if (VOID_TYPE_P (tt1) || VOID_TYPE_P (tt2))
622 target = void_type_node;
623 else if (tt1 == unknown_type_node)
624 target = tt2;
625 else if (tt2 == unknown_type_node)
626 target = tt1;
627 else
628 target = common_type (tt1, tt2);
630 target = cp_build_qualified_type (target, type_quals);
632 if (b1)
634 if (same_type_p (b1, b2)
635 || (DERIVED_FROM_P (b1, b2) && binfo_or_else (b1, b2)))
636 target = build_offset_type (b2, target);
637 else if (binfo_or_else (b2, b1))
638 target = build_offset_type (b1, target);
641 if (code1 == POINTER_TYPE)
642 t1 = build_pointer_type (target);
643 else
644 t1 = build_reference_type (target);
645 t1 = build_type_attribute_variant (t1, attributes);
647 if (TREE_CODE (target) == METHOD_TYPE)
648 t1 = build_ptrmemfunc_type (t1);
650 return t1;
653 case ARRAY_TYPE:
655 tree elt = common_type (TREE_TYPE (t1), TREE_TYPE (t2));
656 /* Save space: see if the result is identical to one of the args. */
657 if (elt == TREE_TYPE (t1) && TYPE_DOMAIN (t1))
658 return build_type_attribute_variant (t1, attributes);
659 if (elt == TREE_TYPE (t2) && TYPE_DOMAIN (t2))
660 return build_type_attribute_variant (t2, attributes);
661 /* Merge the element types, and have a size if either arg has one. */
662 t1 = build_cplus_array_type
663 (elt, TYPE_DOMAIN (TYPE_DOMAIN (t1) ? t1 : t2));
664 return build_type_attribute_variant (t1, attributes);
667 case FUNCTION_TYPE:
668 /* Function types: prefer the one that specified arg types.
669 If both do, merge the arg types. Also merge the return types. */
671 tree valtype = common_type (TREE_TYPE (t1), TREE_TYPE (t2));
672 tree p1 = TYPE_ARG_TYPES (t1);
673 tree p2 = TYPE_ARG_TYPES (t2);
674 tree rval, raises;
676 /* Save space: see if the result is identical to one of the args. */
677 if (valtype == TREE_TYPE (t1) && ! p2)
678 return build_type_attribute_variant (t1, attributes);
679 if (valtype == TREE_TYPE (t2) && ! p1)
680 return build_type_attribute_variant (t2, attributes);
682 /* Simple way if one arg fails to specify argument types. */
683 if (p1 == NULL_TREE || TREE_VALUE (p1) == void_type_node)
685 rval = build_function_type (valtype, p2);
686 if ((raises = TYPE_RAISES_EXCEPTIONS (t2)))
687 rval = build_exception_variant (rval, raises);
688 return build_type_attribute_variant (rval, attributes);
690 raises = TYPE_RAISES_EXCEPTIONS (t1);
691 if (p2 == NULL_TREE || TREE_VALUE (p2) == void_type_node)
693 rval = build_function_type (valtype, p1);
694 if (raises)
695 rval = build_exception_variant (rval, raises);
696 return build_type_attribute_variant (rval, attributes);
699 rval = build_function_type (valtype, commonparms (p1, p2));
700 rval = build_exception_variant (rval, raises);
701 return build_type_attribute_variant (rval, attributes);
704 case RECORD_TYPE:
705 case UNION_TYPE:
706 t1 = TYPE_MAIN_VARIANT (t1);
707 t2 = TYPE_MAIN_VARIANT (t2);
709 if (DERIVED_FROM_P (t1, t2) && binfo_or_else (t1, t2))
710 return build_type_attribute_variant (t1, attributes);
711 else if (binfo_or_else (t2, t1))
712 return build_type_attribute_variant (t2, attributes);
713 else
715 compiler_error ("common_type called with uncommon aggregate types");
716 return error_mark_node;
719 case METHOD_TYPE:
720 if (TREE_CODE (TREE_TYPE (t1)) == TREE_CODE (TREE_TYPE (t2)))
722 /* Get this value the long way, since TYPE_METHOD_BASETYPE
723 is just the main variant of this. */
724 tree basetype;
725 tree raises, t3;
727 tree b1 = TYPE_OFFSET_BASETYPE (t1);
728 tree b2 = TYPE_OFFSET_BASETYPE (t2);
730 if (same_type_p (b1, b2)
731 || (DERIVED_FROM_P (b1, b2) && binfo_or_else (b1, b2)))
732 basetype = TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (t2)));
733 else
735 if (binfo_or_else (b2, b1) == NULL_TREE)
736 compiler_error ("common_type called with uncommon method types");
737 basetype = TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (t1)));
740 raises = TYPE_RAISES_EXCEPTIONS (t1);
742 /* If this was a member function type, get back to the
743 original type of type member function (i.e., without
744 the class instance variable up front. */
745 t1 = build_function_type (TREE_TYPE (t1),
746 TREE_CHAIN (TYPE_ARG_TYPES (t1)));
747 t2 = build_function_type (TREE_TYPE (t2),
748 TREE_CHAIN (TYPE_ARG_TYPES (t2)));
749 t3 = common_type (t1, t2);
750 t3 = build_cplus_method_type (basetype, TREE_TYPE (t3),
751 TYPE_ARG_TYPES (t3));
752 t1 = build_exception_variant (t3, raises);
754 else
755 compiler_error ("common_type called with uncommon method types");
757 return build_type_attribute_variant (t1, attributes);
759 case OFFSET_TYPE:
760 /* Pointers to members should now be handled by the POINTER_TYPE
761 case above. */
762 abort ();
764 default:
765 return build_type_attribute_variant (t1, attributes);
769 /* Compare two exception specifier types for exactness or subsetness, if
770 allowed. Returns 0 for mismatch, 1 for same, 2 if B is allowed by A.
772 [except.spec] "If a class X ... objects of class X or any class publicly
773 and unambigously derrived from X. Similarly, if a pointer type Y * ...
774 exceptions of type Y * or that are pointers to any type publicly and
775 unambigously derrived from Y. Otherwise a function only allows exceptions
776 that have the same type ..."
777 This does not mention cv qualifiers and is different to what throw
778 [except.throw] and catch [except.catch] will do. They will ignore the
779 top level cv qualifiers, and allow qualifiers in the pointer to class
780 example.
782 We implement the letter of the standard. */
784 static int
785 comp_except_types (a, b, exact)
786 tree a, b;
787 int exact;
789 if (same_type_p (a, b))
790 return 1;
791 else if (!exact)
793 if (cp_type_quals (a) || cp_type_quals (b))
794 return 0;
796 if (TREE_CODE (a) == POINTER_TYPE
797 && TREE_CODE (b) == POINTER_TYPE)
799 a = TREE_TYPE (a);
800 b = TREE_TYPE (b);
801 if (cp_type_quals (a) || cp_type_quals (b))
802 return 0;
805 if (TREE_CODE (a) != RECORD_TYPE
806 || TREE_CODE (b) != RECORD_TYPE)
807 return 0;
809 if (ACCESSIBLY_UNIQUELY_DERIVED_P (a, b))
810 return 2;
812 return 0;
815 /* Return 1 if TYPE1 and TYPE2 are equivalent exception specifiers.
816 If EXACT is 0, T2 can be a subset of T1 (according to 15.4/7),
817 otherwise it must be exact. Exception lists are unordered, but
818 we've already filtered out duplicates. Most lists will be in order,
819 we should try to make use of that. */
822 comp_except_specs (t1, t2, exact)
823 tree t1, t2;
824 int exact;
826 tree probe;
827 tree base;
828 int length = 0;
830 if (t1 == t2)
831 return 1;
833 if (t1 == NULL_TREE) /* T1 is ... */
834 return t2 == NULL_TREE || !exact;
835 if (!TREE_VALUE (t1)) /* t1 is EMPTY */
836 return t2 != NULL_TREE && !TREE_VALUE (t2);
837 if (t2 == NULL_TREE) /* T2 is ... */
838 return 0;
839 if (TREE_VALUE(t1) && !TREE_VALUE (t2)) /* T2 is EMPTY, T1 is not */
840 return !exact;
842 /* Neither set is ... or EMPTY, make sure each part of T2 is in T1.
843 Count how many we find, to determine exactness. For exact matching and
844 ordered T1, T2, this is an O(n) operation, otherwise its worst case is
845 O(nm). */
846 for (base = t1; t2 != NULL_TREE; t2 = TREE_CHAIN (t2))
848 for (probe = base; probe != NULL_TREE; probe = TREE_CHAIN (probe))
850 tree a = TREE_VALUE (probe);
851 tree b = TREE_VALUE (t2);
853 if (comp_except_types (a, b, exact))
855 if (probe == base && exact)
856 base = TREE_CHAIN (probe);
857 length++;
858 break;
861 if (probe == NULL_TREE)
862 return 0;
864 return !exact || base == NULL_TREE || length == list_length (t1);
867 /* Compare the array types T1 and T2, using CMP as the type comparison
868 function for the element types. STRICT is as for comptypes. */
870 static int
871 comp_array_types (cmp, t1, t2, strict)
872 register int (*cmp) PARAMS ((tree, tree, int));
873 tree t1, t2;
874 int strict;
876 tree d1;
877 tree d2;
879 if (t1 == t2)
880 return 1;
882 /* The type of the array elements must be the same. */
883 if (!(TREE_TYPE (t1) == TREE_TYPE (t2)
884 || (*cmp) (TREE_TYPE (t1), TREE_TYPE (t2),
885 strict & ~COMPARE_REDECLARATION)))
886 return 0;
888 d1 = TYPE_DOMAIN (t1);
889 d2 = TYPE_DOMAIN (t2);
891 if (d1 == d2)
892 return 1;
894 /* If one of the arrays is dimensionless, and the other has a
895 dimension, they are of different types. However, it is legal to
896 write:
898 extern int a[];
899 int a[3];
901 by [basic.link]:
903 declarations for an array object can specify
904 array types that differ by the presence or absence of a major
905 array bound (_dcl.array_). */
906 if (!d1 || !d2)
907 return strict & COMPARE_REDECLARATION;
909 /* Check that the dimensions are the same. */
910 return (cp_tree_equal (TYPE_MIN_VALUE (d1),
911 TYPE_MIN_VALUE (d2))
912 && cp_tree_equal (TYPE_MAX_VALUE (d1),
913 TYPE_MAX_VALUE (d2)));
916 /* Return 1 if T1 and T2 are compatible types for assignment or
917 various other operations. STRICT is a bitwise-or of the COMPARE_*
918 flags. */
921 comptypes (t1, t2, strict)
922 tree t1;
923 tree t2;
924 int strict;
926 int attrval, val;
927 int orig_strict = strict;
929 /* The special exemption for redeclaring array types without an
930 array bound only applies at the top level:
932 extern int (*i)[];
933 int (*i)[8];
935 is not legal, for example. */
936 strict &= ~COMPARE_REDECLARATION;
938 /* Suppress errors caused by previously reported errors */
939 if (t1 == t2)
940 return 1;
942 /* This should never happen. */
943 my_friendly_assert (t1 != error_mark_node, 307);
945 if (t2 == error_mark_node)
946 return 0;
948 /* If either type is the internal version of sizetype, return the
949 language version. */
950 if (TREE_CODE (t1) == INTEGER_TYPE && TYPE_IS_SIZETYPE (t1)
951 && TYPE_DOMAIN (t1) != 0)
952 t1 = TYPE_DOMAIN (t1);
954 if (TREE_CODE (t2) == INTEGER_TYPE && TYPE_IS_SIZETYPE (t2)
955 && TYPE_DOMAIN (t2) != 0)
956 t2 = TYPE_DOMAIN (t2);
958 if (strict & COMPARE_RELAXED)
960 /* Treat an enum type as the unsigned integer type of the same width. */
962 if (TREE_CODE (t1) == ENUMERAL_TYPE)
963 t1 = type_for_size (TYPE_PRECISION (t1), 1);
964 if (TREE_CODE (t2) == ENUMERAL_TYPE)
965 t2 = type_for_size (TYPE_PRECISION (t2), 1);
967 if (t1 == t2)
968 return 1;
971 if (TYPE_PTRMEMFUNC_P (t1))
972 t1 = TYPE_PTRMEMFUNC_FN_TYPE (t1);
973 if (TYPE_PTRMEMFUNC_P (t2))
974 t2 = TYPE_PTRMEMFUNC_FN_TYPE (t2);
976 /* Different classes of types can't be compatible. */
977 if (TREE_CODE (t1) != TREE_CODE (t2))
978 return 0;
980 /* Qualifiers must match. */
981 if (cp_type_quals (t1) != cp_type_quals (t2))
982 return 0;
983 if (strict == COMPARE_STRICT
984 && TYPE_FOR_JAVA (t1) != TYPE_FOR_JAVA (t2))
985 return 0;
987 /* Allow for two different type nodes which have essentially the same
988 definition. Note that we already checked for equality of the type
989 qualifiers (just above). */
991 if (TYPE_MAIN_VARIANT (t1) == TYPE_MAIN_VARIANT (t2))
992 return 1;
994 if (strict & COMPARE_NO_ATTRIBUTES)
995 attrval = 1;
996 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
997 else if (! (attrval = (*targetm.comp_type_attributes) (t1, t2)))
998 return 0;
1000 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
1001 val = 0;
1003 switch (TREE_CODE (t1))
1005 case TEMPLATE_TEMPLATE_PARM:
1006 case BOUND_TEMPLATE_TEMPLATE_PARM:
1007 if (TEMPLATE_TYPE_IDX (t1) != TEMPLATE_TYPE_IDX (t2)
1008 || TEMPLATE_TYPE_LEVEL (t1) != TEMPLATE_TYPE_LEVEL (t2))
1009 return 0;
1010 if (! comp_template_parms
1011 (DECL_TEMPLATE_PARMS (TEMPLATE_TEMPLATE_PARM_TEMPLATE_DECL (t1)),
1012 DECL_TEMPLATE_PARMS (TEMPLATE_TEMPLATE_PARM_TEMPLATE_DECL (t2))))
1013 return 0;
1014 if (TREE_CODE (t1) == TEMPLATE_TEMPLATE_PARM)
1015 return 1;
1016 /* Don't check inheritance. */
1017 strict = COMPARE_STRICT;
1018 /* fall through */
1020 case RECORD_TYPE:
1021 case UNION_TYPE:
1022 if (TYPE_TEMPLATE_INFO (t1) && TYPE_TEMPLATE_INFO (t2)
1023 && (TYPE_TI_TEMPLATE (t1) == TYPE_TI_TEMPLATE (t2)
1024 || TREE_CODE (t1) == BOUND_TEMPLATE_TEMPLATE_PARM))
1025 val = comp_template_args (TYPE_TI_ARGS (t1),
1026 TYPE_TI_ARGS (t2));
1027 look_hard:
1028 if ((strict & COMPARE_BASE) && DERIVED_FROM_P (t1, t2))
1029 val = 1;
1030 else if ((strict & COMPARE_RELAXED) && DERIVED_FROM_P (t2, t1))
1031 val = 1;
1032 break;
1034 case OFFSET_TYPE:
1035 val = (comptypes (build_pointer_type (TYPE_OFFSET_BASETYPE (t1)),
1036 build_pointer_type (TYPE_OFFSET_BASETYPE (t2)), strict)
1037 && comptypes (TREE_TYPE (t1), TREE_TYPE (t2), strict));
1038 break;
1040 case METHOD_TYPE:
1041 if (! comp_except_specs (TYPE_RAISES_EXCEPTIONS (t1),
1042 TYPE_RAISES_EXCEPTIONS (t2), 1))
1043 return 0;
1045 /* This case is anti-symmetrical!
1046 One can pass a base member (or member function)
1047 to something expecting a derived member (or member function),
1048 but not vice-versa! */
1050 val = (comptypes (TREE_TYPE (t1), TREE_TYPE (t2), strict)
1051 && compparms (TYPE_ARG_TYPES (t1), TYPE_ARG_TYPES (t2)));
1052 break;
1054 case POINTER_TYPE:
1055 case REFERENCE_TYPE:
1056 t1 = TREE_TYPE (t1);
1057 t2 = TREE_TYPE (t2);
1058 /* first, check whether the referred types match with the
1059 required level of strictness */
1060 val = comptypes (t1, t2, strict);
1061 if (val)
1062 break;
1063 if (TREE_CODE (t1) == RECORD_TYPE
1064 && TREE_CODE (t2) == RECORD_TYPE)
1065 goto look_hard;
1066 break;
1068 case FUNCTION_TYPE:
1069 if (! comp_except_specs (TYPE_RAISES_EXCEPTIONS (t1),
1070 TYPE_RAISES_EXCEPTIONS (t2), 1))
1071 return 0;
1073 val = ((TREE_TYPE (t1) == TREE_TYPE (t2)
1074 || comptypes (TREE_TYPE (t1), TREE_TYPE (t2), strict))
1075 && compparms (TYPE_ARG_TYPES (t1), TYPE_ARG_TYPES (t2)));
1076 break;
1078 case ARRAY_TYPE:
1079 /* Target types must match incl. qualifiers. We use ORIG_STRICT
1080 here since this is the one place where
1081 COMPARE_REDECLARATION should be used. */
1082 val = comp_array_types (comptypes, t1, t2, orig_strict);
1083 break;
1085 case TEMPLATE_TYPE_PARM:
1086 return TEMPLATE_TYPE_IDX (t1) == TEMPLATE_TYPE_IDX (t2)
1087 && TEMPLATE_TYPE_LEVEL (t1) == TEMPLATE_TYPE_LEVEL (t2);
1089 case TYPENAME_TYPE:
1090 if (cp_tree_equal (TYPENAME_TYPE_FULLNAME (t1),
1091 TYPENAME_TYPE_FULLNAME (t2)) < 1)
1092 return 0;
1093 return same_type_p (TYPE_CONTEXT (t1), TYPE_CONTEXT (t2));
1095 case UNBOUND_CLASS_TEMPLATE:
1096 if (cp_tree_equal (TYPE_IDENTIFIER (t1),
1097 TYPE_IDENTIFIER (t2)) < 1)
1098 return 0;
1099 return same_type_p (TYPE_CONTEXT (t1), TYPE_CONTEXT (t2));
1101 case COMPLEX_TYPE:
1102 return same_type_p (TREE_TYPE (t1), TREE_TYPE (t2));
1104 default:
1105 break;
1107 return attrval == 2 && val == 1 ? 2 : val;
1110 /* Subroutine of comp_target-types. Make sure that the cv-quals change
1111 only in the same direction as the target type. */
1113 static int
1114 comp_cv_target_types (ttl, ttr, nptrs)
1115 tree ttl, ttr;
1116 int nptrs;
1118 int t;
1120 if (!at_least_as_qualified_p (ttl, ttr)
1121 && !at_least_as_qualified_p (ttr, ttl))
1122 /* The qualifications are incomparable. */
1123 return 0;
1125 if (TYPE_MAIN_VARIANT (ttl) == TYPE_MAIN_VARIANT (ttr))
1126 return more_qualified_p (ttr, ttl) ? -1 : 1;
1128 t = comp_target_types (ttl, ttr, nptrs);
1129 if ((t == 1 && at_least_as_qualified_p (ttl, ttr))
1130 || (t == -1 && at_least_as_qualified_p (ttr, ttl)))
1131 return t;
1133 return 0;
1136 /* Return 1 or -1 if TTL and TTR are pointers to types that are equivalent,
1137 ignoring their qualifiers, 0 if not. Return 1 means that TTR can be
1138 converted to TTL. Return -1 means that TTL can be converted to TTR but
1139 not vice versa.
1141 NPTRS is the number of pointers we can strip off and keep cool.
1142 This is used to permit (for aggr A, aggr B) A, B* to convert to A*,
1143 but to not permit B** to convert to A**.
1145 This should go away. Callers should use can_convert or something
1146 similar instead. (jason 17 Apr 1997) */
1149 comp_target_types (ttl, ttr, nptrs)
1150 tree ttl, ttr;
1151 int nptrs;
1153 ttl = TYPE_MAIN_VARIANT (ttl);
1154 ttr = TYPE_MAIN_VARIANT (ttr);
1155 if (same_type_p (ttl, ttr))
1156 return 1;
1158 if (TREE_CODE (ttr) != TREE_CODE (ttl))
1159 return 0;
1161 if ((TREE_CODE (ttr) == POINTER_TYPE
1162 || TREE_CODE (ttr) == REFERENCE_TYPE)
1163 /* If we get a pointer with nptrs == 0, we don't allow any tweaking
1164 of the type pointed to. This is necessary for reference init
1165 semantics. We won't get here from a previous call with nptrs == 1;
1166 for multi-level pointers we end up in comp_ptr_ttypes. */
1167 && nptrs > 0)
1169 int is_ptr = TREE_CODE (ttr) == POINTER_TYPE;
1171 ttl = TREE_TYPE (ttl);
1172 ttr = TREE_TYPE (ttr);
1174 if (is_ptr)
1176 if (TREE_CODE (ttl) == UNKNOWN_TYPE
1177 || TREE_CODE (ttr) == UNKNOWN_TYPE)
1178 return 1;
1179 else if (TREE_CODE (ttl) == VOID_TYPE
1180 && TREE_CODE (ttr) != FUNCTION_TYPE
1181 && TREE_CODE (ttr) != METHOD_TYPE
1182 && TREE_CODE (ttr) != OFFSET_TYPE)
1183 return 1;
1184 else if (TREE_CODE (ttr) == VOID_TYPE
1185 && TREE_CODE (ttl) != FUNCTION_TYPE
1186 && TREE_CODE (ttl) != METHOD_TYPE
1187 && TREE_CODE (ttl) != OFFSET_TYPE)
1188 return -1;
1189 else if (TREE_CODE (ttl) == POINTER_TYPE
1190 || TREE_CODE (ttl) == ARRAY_TYPE)
1192 if (comp_ptr_ttypes (ttl, ttr))
1193 return 1;
1194 else if (comp_ptr_ttypes (ttr, ttl))
1195 return -1;
1196 return 0;
1200 /* Const and volatile mean something different for function types,
1201 so the usual checks are not appropriate. */
1202 if (TREE_CODE (ttl) == FUNCTION_TYPE || TREE_CODE (ttl) == METHOD_TYPE)
1203 return comp_target_types (ttl, ttr, nptrs - 1);
1205 return comp_cv_target_types (ttl, ttr, nptrs - 1);
1208 if (TREE_CODE (ttr) == ARRAY_TYPE)
1209 return comp_array_types (comp_target_types, ttl, ttr, COMPARE_STRICT);
1210 else if (TREE_CODE (ttr) == FUNCTION_TYPE || TREE_CODE (ttr) == METHOD_TYPE)
1212 tree argsl, argsr;
1213 int saw_contra = 0;
1215 if (pedantic)
1217 if (!same_type_p (TREE_TYPE (ttl), TREE_TYPE (ttr)))
1218 return 0;
1220 else
1222 switch (comp_target_types (TREE_TYPE (ttl), TREE_TYPE (ttr), -1))
1224 case 0:
1225 return 0;
1226 case -1:
1227 saw_contra = 1;
1231 argsl = TYPE_ARG_TYPES (ttl);
1232 argsr = TYPE_ARG_TYPES (ttr);
1234 /* Compare 'this' here, not in comp_target_parms. */
1235 if (TREE_CODE (ttr) == METHOD_TYPE)
1237 tree tl = TYPE_METHOD_BASETYPE (ttl);
1238 tree tr = TYPE_METHOD_BASETYPE (ttr);
1240 if (!same_or_base_type_p (tr, tl))
1242 if (same_or_base_type_p (tl, tr))
1243 saw_contra = 1;
1244 else
1245 return 0;
1248 argsl = TREE_CHAIN (argsl);
1249 argsr = TREE_CHAIN (argsr);
1252 switch (comp_target_parms (argsl, argsr))
1254 case 0:
1255 return 0;
1256 case -1:
1257 saw_contra = 1;
1260 return saw_contra ? -1 : 1;
1262 /* for C++ */
1263 else if (TREE_CODE (ttr) == OFFSET_TYPE)
1265 int base;
1267 /* Contravariance: we can assign a pointer to base member to a pointer
1268 to derived member. Note difference from simple pointer case, where
1269 we can pass a pointer to derived to a pointer to base. */
1270 if (same_or_base_type_p (TYPE_OFFSET_BASETYPE (ttr),
1271 TYPE_OFFSET_BASETYPE (ttl)))
1272 base = 1;
1273 else if (same_or_base_type_p (TYPE_OFFSET_BASETYPE (ttl),
1274 TYPE_OFFSET_BASETYPE (ttr)))
1276 tree tmp = ttl;
1277 ttl = ttr;
1278 ttr = tmp;
1279 base = -1;
1281 else
1282 return 0;
1284 ttl = TREE_TYPE (ttl);
1285 ttr = TREE_TYPE (ttr);
1287 if (TREE_CODE (ttl) == POINTER_TYPE
1288 || TREE_CODE (ttl) == ARRAY_TYPE)
1290 if (comp_ptr_ttypes (ttl, ttr))
1291 return base;
1292 return 0;
1294 else
1296 if (comp_cv_target_types (ttl, ttr, nptrs) == 1)
1297 return base;
1298 return 0;
1301 else if (IS_AGGR_TYPE (ttl))
1303 if (nptrs < 0)
1304 return 0;
1305 if (same_or_base_type_p (build_pointer_type (ttl),
1306 build_pointer_type (ttr)))
1307 return 1;
1308 if (same_or_base_type_p (build_pointer_type (ttr),
1309 build_pointer_type (ttl)))
1310 return -1;
1311 return 0;
1314 return 0;
1317 /* Returns 1 if TYPE1 is at least as qualified as TYPE2. */
1320 at_least_as_qualified_p (type1, type2)
1321 tree type1;
1322 tree type2;
1324 /* All qualifiers for TYPE2 must also appear in TYPE1. */
1325 return ((cp_type_quals (type1) & cp_type_quals (type2))
1326 == cp_type_quals (type2));
1329 /* Returns 1 if TYPE1 is more qualified than TYPE2. */
1332 more_qualified_p (type1, type2)
1333 tree type1;
1334 tree type2;
1336 return (cp_type_quals (type1) != cp_type_quals (type2)
1337 && at_least_as_qualified_p (type1, type2));
1340 /* Returns 1 if TYPE1 is more cv-qualified than TYPE2, -1 if TYPE2 is
1341 more cv-qualified that TYPE1, and 0 otherwise. */
1344 comp_cv_qualification (type1, type2)
1345 tree type1;
1346 tree type2;
1348 if (cp_type_quals (type1) == cp_type_quals (type2))
1349 return 0;
1351 if (at_least_as_qualified_p (type1, type2))
1352 return 1;
1354 else if (at_least_as_qualified_p (type2, type1))
1355 return -1;
1357 return 0;
1360 /* Returns 1 if the cv-qualification signature of TYPE1 is a proper
1361 subset of the cv-qualification signature of TYPE2, and the types
1362 are similar. Returns -1 if the other way 'round, and 0 otherwise. */
1365 comp_cv_qual_signature (type1, type2)
1366 tree type1;
1367 tree type2;
1369 if (comp_ptr_ttypes_real (type2, type1, -1))
1370 return 1;
1371 else if (comp_ptr_ttypes_real (type1, type2, -1))
1372 return -1;
1373 else
1374 return 0;
1377 /* If two types share a common base type, return that basetype.
1378 If there is not a unique most-derived base type, this function
1379 returns ERROR_MARK_NODE. */
1381 static tree
1382 common_base_type (tt1, tt2)
1383 tree tt1, tt2;
1385 tree best = NULL_TREE;
1386 int i;
1388 /* If one is a baseclass of another, that's good enough. */
1389 if (UNIQUELY_DERIVED_FROM_P (tt1, tt2))
1390 return tt1;
1391 if (UNIQUELY_DERIVED_FROM_P (tt2, tt1))
1392 return tt2;
1394 /* Otherwise, try to find a unique baseclass of TT1
1395 that is shared by TT2, and follow that down. */
1396 for (i = CLASSTYPE_N_BASECLASSES (tt1)-1; i >= 0; i--)
1398 tree basetype = TYPE_BINFO_BASETYPE (tt1, i);
1399 tree trial = common_base_type (basetype, tt2);
1400 if (trial)
1402 if (trial == error_mark_node)
1403 return trial;
1404 if (best == NULL_TREE)
1405 best = trial;
1406 else if (best != trial)
1407 return error_mark_node;
1411 /* Same for TT2. */
1412 for (i = CLASSTYPE_N_BASECLASSES (tt2)-1; i >= 0; i--)
1414 tree basetype = TYPE_BINFO_BASETYPE (tt2, i);
1415 tree trial = common_base_type (tt1, basetype);
1416 if (trial)
1418 if (trial == error_mark_node)
1419 return trial;
1420 if (best == NULL_TREE)
1421 best = trial;
1422 else if (best != trial)
1423 return error_mark_node;
1426 return best;
1429 /* Subroutines of `comptypes'. */
1431 /* Return 1 if two parameter type lists PARMS1 and PARMS2 are
1432 equivalent in the sense that functions with those parameter types
1433 can have equivalent types. The two lists must be equivalent,
1434 element by element.
1436 C++: See comment above about TYPE1, TYPE2. */
1439 compparms (parms1, parms2)
1440 tree parms1, parms2;
1442 register tree t1 = parms1, t2 = parms2;
1444 /* An unspecified parmlist matches any specified parmlist
1445 whose argument types don't need default promotions. */
1447 while (1)
1449 if (t1 == 0 && t2 == 0)
1450 return 1;
1451 /* If one parmlist is shorter than the other,
1452 they fail to match. */
1453 if (t1 == 0 || t2 == 0)
1454 return 0;
1455 if (!same_type_p (TREE_VALUE (t2), TREE_VALUE (t1)))
1456 return 0;
1458 t1 = TREE_CHAIN (t1);
1459 t2 = TREE_CHAIN (t2);
1463 /* This really wants return whether or not parameter type lists
1464 would make their owning functions assignment compatible or not.
1466 The return value is like for comp_target_types.
1468 This should go away, possibly with the exception of the empty parmlist
1469 conversion; there are no conversions between function types in C++.
1470 (jason 17 Apr 1997) */
1472 static int
1473 comp_target_parms (parms1, parms2)
1474 tree parms1, parms2;
1476 register tree t1 = parms1, t2 = parms2;
1477 int warn_contravariance = 0;
1479 /* In C, an unspecified parmlist matches any specified parmlist
1480 whose argument types don't need default promotions. This is not
1481 true for C++, but let's do it anyway for unfixed headers. */
1483 if (t1 == 0 && t2 != 0)
1485 pedwarn ("ISO C++ prohibits conversion from `%#T' to `(...)'",
1486 parms2);
1487 return self_promoting_args_p (t2);
1489 if (t2 == 0)
1490 return self_promoting_args_p (t1);
1492 for (; t1 || t2; t1 = TREE_CHAIN (t1), t2 = TREE_CHAIN (t2))
1494 tree p1, p2;
1496 /* If one parmlist is shorter than the other,
1497 they fail to match, unless STRICT is <= 0. */
1498 if (t1 == 0 || t2 == 0)
1499 return 0;
1500 p1 = TREE_VALUE (t1);
1501 p2 = TREE_VALUE (t2);
1502 if (same_type_p (p1, p2))
1503 continue;
1505 if (pedantic)
1506 return 0;
1508 if ((TREE_CODE (p1) == POINTER_TYPE && TREE_CODE (p2) == POINTER_TYPE)
1509 || (TREE_CODE (p1) == REFERENCE_TYPE
1510 && TREE_CODE (p2) == REFERENCE_TYPE))
1512 /* The following is wrong for contravariance,
1513 but many programs depend on it. */
1514 if (TREE_TYPE (p1) == void_type_node)
1515 continue;
1516 if (TREE_TYPE (p2) == void_type_node)
1518 warn_contravariance = 1;
1519 continue;
1521 if (IS_AGGR_TYPE (TREE_TYPE (p1))
1522 && !same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (p1),
1523 TREE_TYPE (p2)))
1524 return 0;
1526 /* Note backwards order due to contravariance. */
1527 if (comp_target_types (p2, p1, 1) <= 0)
1529 if (comp_target_types (p1, p2, 1) > 0)
1531 warn_contravariance = 1;
1532 continue;
1534 return 0;
1537 return warn_contravariance ? -1 : 1;
1540 /* Compute the value of the `sizeof' operator. */
1542 tree
1543 c_sizeof (type)
1544 tree type;
1546 enum tree_code code = TREE_CODE (type);
1547 tree size;
1549 if (processing_template_decl)
1550 return build_min_nt (SIZEOF_EXPR, type);
1552 if (code == FUNCTION_TYPE)
1554 if (pedantic || warn_pointer_arith)
1555 pedwarn ("ISO C++ forbids applying `sizeof' to a function type");
1556 size = size_one_node;
1558 else if (code == METHOD_TYPE)
1560 if (pedantic || warn_pointer_arith)
1561 pedwarn ("ISO C++ forbids applying `sizeof' to a member function");
1562 size = size_one_node;
1564 else if (code == VOID_TYPE)
1566 if (pedantic || warn_pointer_arith)
1567 pedwarn ("ISO C++ forbids applying `sizeof' to type `void' which is an incomplete type");
1568 size = size_one_node;
1570 else if (code == ERROR_MARK)
1571 size = size_one_node;
1572 else
1574 /* ARM $5.3.2: ``When applied to a reference, the result is the
1575 size of the referenced object.'' */
1576 if (code == REFERENCE_TYPE)
1577 type = TREE_TYPE (type);
1579 if (code == OFFSET_TYPE)
1581 error ("`sizeof' applied to non-static member");
1582 size = size_zero_node;
1584 else if (!COMPLETE_TYPE_P (complete_type (type)))
1586 error ("`sizeof' applied to incomplete type `%T'", type);
1587 size = size_zero_node;
1589 else
1590 /* Convert in case a char is more than one unit. */
1591 size = size_binop (CEIL_DIV_EXPR, TYPE_SIZE_UNIT (type),
1592 size_int (TYPE_PRECISION (char_type_node)
1593 / BITS_PER_UNIT));
1596 /* SIZE will have an integer type with TYPE_IS_SIZETYPE set.
1597 TYPE_IS_SIZETYPE means that certain things (like overflow) will
1598 never happen. However, this node should really have type
1599 `size_t', which is just a typedef for an ordinary integer type. */
1600 size = fold (build1 (NOP_EXPR, c_size_type_node, size));
1601 my_friendly_assert (!TYPE_IS_SIZETYPE (TREE_TYPE (size)),
1602 20001021);
1603 return size;
1607 tree
1608 expr_sizeof (e)
1609 tree e;
1611 if (processing_template_decl)
1612 return build_min_nt (SIZEOF_EXPR, e);
1614 if (TREE_CODE (e) == COMPONENT_REF
1615 && DECL_C_BIT_FIELD (TREE_OPERAND (e, 1)))
1616 error ("sizeof applied to a bit-field");
1617 if (is_overloaded_fn (e))
1619 pedwarn ("ISO C++ forbids applying `sizeof' to an expression of function type");
1620 return c_sizeof (char_type_node);
1622 else if (type_unknown_p (e))
1624 incomplete_type_error (e, TREE_TYPE (e));
1625 return c_sizeof (char_type_node);
1627 /* It's illegal to say `sizeof (X::i)' for `i' a non-static data
1628 member unless you're in a non-static member of X. So hand off to
1629 resolve_offset_ref. [expr.prim] */
1630 else if (TREE_CODE (e) == OFFSET_REF)
1631 e = resolve_offset_ref (e);
1633 if (e == error_mark_node)
1634 return e;
1636 return c_sizeof (TREE_TYPE (e));
1639 tree
1640 c_sizeof_nowarn (type)
1641 tree type;
1643 enum tree_code code = TREE_CODE (type);
1644 tree size;
1646 if (code == FUNCTION_TYPE
1647 || code == METHOD_TYPE
1648 || code == VOID_TYPE
1649 || code == ERROR_MARK)
1650 size = size_one_node;
1651 else
1653 if (code == REFERENCE_TYPE)
1654 type = TREE_TYPE (type);
1656 if (!COMPLETE_TYPE_P (type))
1657 size = size_zero_node;
1658 else
1659 /* Convert in case a char is more than one unit. */
1660 size = size_binop (CEIL_DIV_EXPR, TYPE_SIZE_UNIT (type),
1661 size_int (TYPE_PRECISION (char_type_node)
1662 / BITS_PER_UNIT));
1665 /* SIZE will have an integer type with TYPE_IS_SIZETYPE set.
1666 TYPE_IS_SIZETYPE means that certain things (like overflow) will
1667 never happen. However, this node should really have type
1668 `size_t', which is just a typedef for an ordinary integer type. */
1669 size = fold (build1 (NOP_EXPR, c_size_type_node, size));
1670 my_friendly_assert (!TYPE_IS_SIZETYPE (TREE_TYPE (size)),
1671 20001021);
1672 return size;
1675 /* Perform the array-to-pointer and function-to-pointer conversions
1676 for EXP.
1678 In addition, references are converted to lvalues and manifest
1679 constants are replaced by their values. */
1681 tree
1682 decay_conversion (exp)
1683 tree exp;
1685 register tree type;
1686 register enum tree_code code;
1688 if (TREE_CODE (exp) == OFFSET_REF)
1689 exp = resolve_offset_ref (exp);
1691 type = TREE_TYPE (exp);
1692 code = TREE_CODE (type);
1694 if (code == REFERENCE_TYPE)
1696 exp = convert_from_reference (exp);
1697 type = TREE_TYPE (exp);
1698 code = TREE_CODE (type);
1701 if (type == error_mark_node)
1702 return error_mark_node;
1704 if (type_unknown_p (exp))
1706 incomplete_type_error (exp, TREE_TYPE (exp));
1707 return error_mark_node;
1710 /* Constants can be used directly unless they're not loadable. */
1711 if (TREE_CODE (exp) == CONST_DECL)
1712 exp = DECL_INITIAL (exp);
1713 /* Replace a nonvolatile const static variable with its value. We
1714 don't do this for arrays, though; we want the address of the
1715 first element of the array, not the address of the first element
1716 of its initializing constant. */
1717 else if (code != ARRAY_TYPE)
1719 exp = decl_constant_value (exp);
1720 type = TREE_TYPE (exp);
1723 /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
1724 Leave such NOP_EXPRs, since RHS is being used in non-lvalue context. */
1726 if (code == VOID_TYPE)
1728 error ("void value not ignored as it ought to be");
1729 return error_mark_node;
1731 if (code == METHOD_TYPE)
1732 abort ();
1733 if (code == FUNCTION_TYPE || is_overloaded_fn (exp))
1734 return build_unary_op (ADDR_EXPR, exp, 0);
1735 if (code == ARRAY_TYPE)
1737 register tree adr;
1738 tree ptrtype;
1740 if (TREE_CODE (exp) == INDIRECT_REF)
1742 /* Stripping away the INDIRECT_REF is not the right
1743 thing to do for references... */
1744 tree inner = TREE_OPERAND (exp, 0);
1745 if (TREE_CODE (TREE_TYPE (inner)) == REFERENCE_TYPE)
1747 inner = build1 (CONVERT_EXPR,
1748 build_pointer_type (TREE_TYPE
1749 (TREE_TYPE (inner))),
1750 inner);
1751 TREE_CONSTANT (inner) = TREE_CONSTANT (TREE_OPERAND (inner, 0));
1753 return cp_convert (build_pointer_type (TREE_TYPE (type)), inner);
1756 if (TREE_CODE (exp) == COMPOUND_EXPR)
1758 tree op1 = decay_conversion (TREE_OPERAND (exp, 1));
1759 return build (COMPOUND_EXPR, TREE_TYPE (op1),
1760 TREE_OPERAND (exp, 0), op1);
1763 if (!lvalue_p (exp)
1764 && ! (TREE_CODE (exp) == CONSTRUCTOR && TREE_STATIC (exp)))
1766 error ("invalid use of non-lvalue array");
1767 return error_mark_node;
1770 ptrtype = build_pointer_type (TREE_TYPE (type));
1772 if (TREE_CODE (exp) == VAR_DECL)
1774 /* ??? This is not really quite correct
1775 in that the type of the operand of ADDR_EXPR
1776 is not the target type of the type of the ADDR_EXPR itself.
1777 Question is, can this lossage be avoided? */
1778 adr = build1 (ADDR_EXPR, ptrtype, exp);
1779 if (mark_addressable (exp) == 0)
1780 return error_mark_node;
1781 TREE_CONSTANT (adr) = staticp (exp);
1782 TREE_SIDE_EFFECTS (adr) = 0; /* Default would be, same as EXP. */
1783 return adr;
1785 /* This way is better for a COMPONENT_REF since it can
1786 simplify the offset for a component. */
1787 adr = build_unary_op (ADDR_EXPR, exp, 1);
1788 return cp_convert (ptrtype, adr);
1791 /* [basic.lval]: Class rvalues can have cv-qualified types; non-class
1792 rvalues always have cv-unqualified types. */
1793 if (! CLASS_TYPE_P (type))
1794 exp = cp_convert (TYPE_MAIN_VARIANT (type), exp);
1796 return exp;
1799 tree
1800 default_conversion (exp)
1801 tree exp;
1803 tree type;
1804 enum tree_code code;
1806 exp = decay_conversion (exp);
1808 type = TREE_TYPE (exp);
1809 code = TREE_CODE (type);
1811 if (INTEGRAL_CODE_P (code))
1813 tree t = type_promotes_to (type);
1814 if (t != type)
1815 return cp_convert (t, exp);
1818 return exp;
1821 /* Take the address of an inline function without setting TREE_ADDRESSABLE
1822 or TREE_USED. */
1824 tree
1825 inline_conversion (exp)
1826 tree exp;
1828 if (TREE_CODE (exp) == FUNCTION_DECL)
1829 exp = build1 (ADDR_EXPR, build_pointer_type (TREE_TYPE (exp)), exp);
1831 return exp;
1834 /* Returns nonzero iff exp is a STRING_CST or the result of applying
1835 decay_conversion to one. */
1838 string_conv_p (totype, exp, warn)
1839 tree totype, exp;
1840 int warn;
1842 tree t;
1844 if (! flag_const_strings || TREE_CODE (totype) != POINTER_TYPE)
1845 return 0;
1847 t = TREE_TYPE (totype);
1848 if (!same_type_p (t, char_type_node)
1849 && !same_type_p (t, wchar_type_node))
1850 return 0;
1852 if (TREE_CODE (exp) == STRING_CST)
1854 /* Make sure that we don't try to convert between char and wchar_t. */
1855 if (!same_type_p (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (exp))), t))
1856 return 0;
1858 else
1860 /* Is this a string constant which has decayed to 'const char *'? */
1861 t = build_pointer_type (build_qualified_type (t, TYPE_QUAL_CONST));
1862 if (!same_type_p (TREE_TYPE (exp), t))
1863 return 0;
1864 STRIP_NOPS (exp);
1865 if (TREE_CODE (exp) != ADDR_EXPR
1866 || TREE_CODE (TREE_OPERAND (exp, 0)) != STRING_CST)
1867 return 0;
1870 /* This warning is not very useful, as it complains about printf. */
1871 if (warn && warn_write_strings)
1872 warning ("deprecated conversion from string constant to `%T'", totype);
1874 return 1;
1877 tree
1878 build_object_ref (datum, basetype, field)
1879 tree datum, basetype, field;
1881 tree dtype;
1882 if (datum == error_mark_node)
1883 return error_mark_node;
1885 dtype = TREE_TYPE (datum);
1886 if (TREE_CODE (dtype) == REFERENCE_TYPE)
1887 dtype = TREE_TYPE (dtype);
1888 if (! IS_AGGR_TYPE_CODE (TREE_CODE (dtype)))
1890 error ("request for member `%T::%D' in expression of non-aggregate type `%T'",
1891 basetype, field, dtype);
1892 return error_mark_node;
1894 else if (is_aggr_type (basetype, 1))
1896 tree binfo = binfo_or_else (basetype, dtype);
1897 if (binfo)
1898 return build_x_component_ref (build_scoped_ref (datum, basetype),
1899 field, binfo, 1);
1901 return error_mark_node;
1904 /* Like `build_component_ref, but uses an already found field, and converts
1905 from a reference. Must compute access for current_class_ref.
1906 Otherwise, ok. */
1908 tree
1909 build_component_ref_1 (datum, field, protect)
1910 tree datum, field;
1911 int protect;
1913 return convert_from_reference
1914 (build_component_ref (datum, field, NULL_TREE, protect));
1917 /* Given a COND_EXPR, MIN_EXPR, or MAX_EXPR in T, return it in a form that we
1918 can, for example, use as an lvalue. This code used to be in
1919 unary_complex_lvalue, but we needed it to deal with `a = (d == c) ? b : c'
1920 expressions, where we're dealing with aggregates. But now it's again only
1921 called from unary_complex_lvalue. The case (in particular) that led to
1922 this was with CODE == ADDR_EXPR, since it's not an lvalue when we'd
1923 get it there. */
1925 static tree
1926 rationalize_conditional_expr (code, t)
1927 enum tree_code code;
1928 tree t;
1930 /* For MIN_EXPR or MAX_EXPR, fold-const.c has arranged things so that
1931 the first operand is always the one to be used if both operands
1932 are equal, so we know what conditional expression this used to be. */
1933 if (TREE_CODE (t) == MIN_EXPR || TREE_CODE (t) == MAX_EXPR)
1935 return
1936 build_conditional_expr (build_x_binary_op ((TREE_CODE (t) == MIN_EXPR
1937 ? LE_EXPR : GE_EXPR),
1938 TREE_OPERAND (t, 0),
1939 TREE_OPERAND (t, 1)),
1940 build_unary_op (code, TREE_OPERAND (t, 0), 0),
1941 build_unary_op (code, TREE_OPERAND (t, 1), 0));
1944 return
1945 build_conditional_expr (TREE_OPERAND (t, 0),
1946 build_unary_op (code, TREE_OPERAND (t, 1), 0),
1947 build_unary_op (code, TREE_OPERAND (t, 2), 0));
1950 /* Given the TYPE of an anonymous union field inside T, return the
1951 FIELD_DECL for the field. If not found return NULL_TREE. Because
1952 anonymous unions can nest, we must also search all anonymous unions
1953 that are directly reachable. */
1955 static tree
1956 lookup_anon_field (t, type)
1957 tree t, type;
1959 tree field;
1961 for (field = TYPE_FIELDS (t); field; field = TREE_CHAIN (field))
1963 if (TREE_STATIC (field))
1964 continue;
1965 if (TREE_CODE (field) != FIELD_DECL)
1966 continue;
1968 /* If we find it directly, return the field. */
1969 if (DECL_NAME (field) == NULL_TREE
1970 && type == TYPE_MAIN_VARIANT (TREE_TYPE (field)))
1972 return field;
1975 /* Otherwise, it could be nested, search harder. */
1976 if (DECL_NAME (field) == NULL_TREE
1977 && ANON_AGGR_TYPE_P (TREE_TYPE (field)))
1979 tree subfield = lookup_anon_field (TREE_TYPE (field), type);
1980 if (subfield)
1981 return subfield;
1984 return NULL_TREE;
1987 /* Build a COMPONENT_REF for a given DATUM, and it's member COMPONENT.
1988 COMPONENT can be an IDENTIFIER_NODE that is the name of the member
1989 that we are interested in, or it can be a FIELD_DECL. */
1991 tree
1992 build_component_ref (datum, component, basetype_path, protect)
1993 tree datum, component, basetype_path;
1994 int protect;
1996 register tree basetype;
1997 register enum tree_code code;
1998 register tree field = NULL;
1999 register tree ref;
2000 tree field_type;
2001 int type_quals;
2003 if (processing_template_decl)
2004 return build_min_nt (COMPONENT_REF, datum, component);
2006 if (datum == error_mark_node
2007 || TREE_TYPE (datum) == error_mark_node)
2008 return error_mark_node;
2010 /* BASETYPE holds the type of the class containing the COMPONENT. */
2011 basetype = TYPE_MAIN_VARIANT (TREE_TYPE (datum));
2013 /* If DATUM is a COMPOUND_EXPR or COND_EXPR, move our reference
2014 inside it. */
2015 switch (TREE_CODE (datum))
2017 case COMPOUND_EXPR:
2019 tree value = build_component_ref (TREE_OPERAND (datum, 1), component,
2020 basetype_path, protect);
2021 return build (COMPOUND_EXPR, TREE_TYPE (value),
2022 TREE_OPERAND (datum, 0), value);
2024 case COND_EXPR:
2025 return build_conditional_expr
2026 (TREE_OPERAND (datum, 0),
2027 build_component_ref (TREE_OPERAND (datum, 1), component,
2028 basetype_path, protect),
2029 build_component_ref (TREE_OPERAND (datum, 2), component,
2030 basetype_path, protect));
2032 case TEMPLATE_DECL:
2033 error ("invalid use of `%D'", datum);
2034 datum = error_mark_node;
2035 break;
2037 default:
2038 break;
2041 code = TREE_CODE (basetype);
2043 if (code == REFERENCE_TYPE)
2045 datum = convert_from_reference (datum);
2046 basetype = TYPE_MAIN_VARIANT (TREE_TYPE (datum));
2047 code = TREE_CODE (basetype);
2049 if (TREE_CODE (datum) == OFFSET_REF)
2051 datum = resolve_offset_ref (datum);
2052 basetype = TYPE_MAIN_VARIANT (TREE_TYPE (datum));
2053 code = TREE_CODE (basetype);
2056 /* First, see if there is a field or component with name COMPONENT. */
2057 if (TREE_CODE (component) == TREE_LIST)
2059 /* I could not trigger this code. MvL */
2060 abort ();
2061 #ifdef DEAD
2062 my_friendly_assert (!(TREE_CHAIN (component) == NULL_TREE
2063 && DECL_CHAIN (TREE_VALUE (component)) == NULL_TREE), 309);
2064 #endif
2065 return build (COMPONENT_REF, TREE_TYPE (component), datum, component);
2068 if (! IS_AGGR_TYPE_CODE (code))
2070 if (code != ERROR_MARK)
2071 error ("request for member `%D' in `%E', which is of non-aggregate type `%T'",
2072 component, datum, basetype);
2073 return error_mark_node;
2076 if (!complete_type_or_else (basetype, datum))
2077 return error_mark_node;
2079 if (TREE_CODE (component) == BIT_NOT_EXPR)
2081 if (TYPE_IDENTIFIER (basetype) != TREE_OPERAND (component, 0))
2083 error ("destructor specifier `%T::~%T' must have matching names",
2084 basetype, TREE_OPERAND (component, 0));
2085 return error_mark_node;
2087 if (! TYPE_HAS_DESTRUCTOR (basetype))
2089 error ("type `%T' has no destructor", basetype);
2090 return error_mark_node;
2092 return TREE_VEC_ELT (CLASSTYPE_METHOD_VEC (basetype), 1);
2095 /* Look up component name in the structure type definition. */
2096 if (TYPE_VFIELD (basetype)
2097 && DECL_NAME (TYPE_VFIELD (basetype)) == component)
2098 /* Special-case this because if we use normal lookups in an ambiguous
2099 hierarchy, the compiler will abort (because vptr lookups are
2100 not supposed to be ambiguous. */
2101 field = TYPE_VFIELD (basetype);
2102 else if (TREE_CODE (component) == FIELD_DECL)
2103 field = component;
2104 else if (TREE_CODE (component) == TYPE_DECL)
2106 error ("invalid use of type decl `%#D' as expression", component);
2107 return error_mark_node;
2109 else if (TREE_CODE (component) == TEMPLATE_DECL)
2111 error ("invalid use of template `%#D' as expression", component);
2112 return error_mark_node;
2114 else
2116 tree name = component;
2118 if (TREE_CODE (component) == TEMPLATE_ID_EXPR)
2119 name = TREE_OPERAND (component, 0);
2120 else if (TREE_CODE (component) == VAR_DECL)
2121 name = DECL_NAME (component);
2122 if (TREE_CODE (component) == NAMESPACE_DECL)
2123 /* Source is in error, but produce a sensible diagnostic. */
2124 name = DECL_NAME (component);
2125 if (basetype_path == NULL_TREE)
2126 basetype_path = TYPE_BINFO (basetype);
2127 field = lookup_field (basetype_path, name,
2128 protect && !VFIELD_NAME_P (name), 0);
2129 if (field == error_mark_node)
2130 return error_mark_node;
2132 if (field == NULL_TREE)
2134 /* Not found as a data field, look for it as a method. If found,
2135 then if this is the only possible one, return it, else
2136 report ambiguity error. */
2137 tree fndecls = lookup_fnfields (basetype_path, name, 1);
2138 if (fndecls == error_mark_node)
2139 return error_mark_node;
2140 if (fndecls)
2142 /* If the function is unique and static, we can resolve it
2143 now. Otherwise, we have to wait and see what context it is
2144 used in; a component_ref involving a non-static member
2145 function can only be used in a call (expr.ref). */
2147 if (TREE_CHAIN (fndecls) == NULL_TREE
2148 && TREE_CODE (TREE_VALUE (fndecls)) == FUNCTION_DECL)
2150 if (DECL_STATIC_FUNCTION_P (TREE_VALUE (fndecls)))
2152 tree fndecl = TREE_VALUE (fndecls);
2153 enforce_access (basetype_path, fndecl);
2154 mark_used (fndecl);
2155 return fndecl;
2157 else
2159 /* A unique non-static member function. Other parts
2160 of the compiler expect something with
2161 unknown_type_node to be really overloaded, so
2162 let's oblige. */
2163 TREE_VALUE (fndecls)
2164 = ovl_cons (TREE_VALUE (fndecls), NULL_TREE);
2168 fndecls = TREE_VALUE (fndecls);
2170 if (TREE_CODE (component) == TEMPLATE_ID_EXPR)
2171 fndecls = build_nt (TEMPLATE_ID_EXPR,
2172 fndecls, TREE_OPERAND (component, 1));
2174 ref = build (COMPONENT_REF, unknown_type_node,
2175 datum, fndecls);
2176 return ref;
2179 error ("`%#T' has no member named `%D'", basetype, name);
2180 return error_mark_node;
2182 else if (TREE_TYPE (field) == error_mark_node)
2183 return error_mark_node;
2185 if (TREE_CODE (field) != FIELD_DECL)
2187 if (TREE_CODE (field) == TYPE_DECL)
2188 pedwarn ("invalid use of type decl `%#D' as expression", field);
2189 else if (DECL_RTL (field) != 0)
2190 mark_used (field);
2191 else
2192 TREE_USED (field) = 1;
2194 /* Do evaluate the object when accessing a static member. */
2195 if (TREE_SIDE_EFFECTS (datum))
2196 field = build (COMPOUND_EXPR, TREE_TYPE (field), datum, field);
2198 return field;
2202 if (TREE_DEPRECATED (field))
2203 warn_deprecated_use (field);
2205 /* See if we have to do any conversions so that we pick up the field from the
2206 right context. */
2207 if (DECL_FIELD_CONTEXT (field) != basetype)
2209 tree context = DECL_FIELD_CONTEXT (field);
2210 tree base = context;
2211 while (!same_type_p (base, basetype) && TYPE_NAME (base)
2212 && ANON_AGGR_TYPE_P (base))
2213 base = TYPE_CONTEXT (base);
2215 /* Handle base classes here... */
2216 if (base != basetype && TYPE_BASE_CONVS_MAY_REQUIRE_CODE_P (basetype))
2218 tree binfo = lookup_base (TREE_TYPE (datum), base, ba_check, NULL);
2220 if (TREE_CODE (datum) == INDIRECT_REF
2221 && integer_zerop (TREE_OPERAND (datum, 0)))
2223 error ("invalid reference to NULL ptr, use ptr-to-member instead");
2224 return error_mark_node;
2226 datum = build_base_path (PLUS_EXPR, datum, binfo, 1);
2227 if (datum == error_mark_node)
2228 return error_mark_node;
2230 basetype = base;
2232 /* Handle things from anon unions here... */
2233 if (TYPE_NAME (context) && ANON_AGGR_TYPE_P (context))
2235 tree subfield = lookup_anon_field (basetype, context);
2236 tree subdatum = build_component_ref (datum, subfield,
2237 basetype_path, protect);
2238 return build_component_ref (subdatum, field, basetype_path, protect);
2242 /* Compute the type of the field, as described in [expr.ref]. */
2243 type_quals = TYPE_UNQUALIFIED;
2244 field_type = TREE_TYPE (field);
2245 if (TREE_CODE (field_type) == REFERENCE_TYPE)
2246 /* The standard says that the type of the result should be the
2247 type referred to by the reference. But for now, at least, we
2248 do the conversion from reference type later. */
2250 else
2252 type_quals = (cp_type_quals (field_type)
2253 | cp_type_quals (TREE_TYPE (datum)));
2255 /* A field is const (volatile) if the enclosing object, or the
2256 field itself, is const (volatile). But, a mutable field is
2257 not const, even within a const object. */
2258 if (DECL_MUTABLE_P (field))
2259 type_quals &= ~TYPE_QUAL_CONST;
2260 field_type = cp_build_qualified_type (field_type, type_quals);
2263 ref = fold (build (COMPONENT_REF, field_type, datum, field));
2265 /* Mark the expression const or volatile, as appropriate. Even
2266 though we've dealt with the type above, we still have to mark the
2267 expression itself. */
2268 if (type_quals & TYPE_QUAL_CONST)
2269 TREE_READONLY (ref) = 1;
2270 else if (type_quals & TYPE_QUAL_VOLATILE)
2271 TREE_THIS_VOLATILE (ref) = 1;
2273 return ref;
2276 /* Variant of build_component_ref for use in expressions, which should
2277 never have REFERENCE_TYPE. */
2279 tree
2280 build_x_component_ref (datum, component, basetype_path, protect)
2281 tree datum, component, basetype_path;
2282 int protect;
2284 tree t = build_component_ref (datum, component, basetype_path, protect);
2286 if (! processing_template_decl)
2287 t = convert_from_reference (t);
2289 return t;
2292 /* Given an expression PTR for a pointer, return an expression
2293 for the value pointed to.
2294 ERRORSTRING is the name of the operator to appear in error messages.
2296 This function may need to overload OPERATOR_FNNAME.
2297 Must also handle REFERENCE_TYPEs for C++. */
2299 tree
2300 build_x_indirect_ref (ptr, errorstring)
2301 tree ptr;
2302 const char *errorstring;
2304 tree rval;
2306 if (processing_template_decl)
2307 return build_min_nt (INDIRECT_REF, ptr);
2309 rval = build_opfncall (INDIRECT_REF, LOOKUP_NORMAL, ptr, NULL_TREE,
2310 NULL_TREE);
2311 if (rval)
2312 return rval;
2313 return build_indirect_ref (ptr, errorstring);
2316 tree
2317 build_indirect_ref (ptr, errorstring)
2318 tree ptr;
2319 const char *errorstring;
2321 register tree pointer, type;
2323 if (ptr == error_mark_node)
2324 return error_mark_node;
2326 if (ptr == current_class_ptr)
2327 return current_class_ref;
2329 pointer = (TREE_CODE (TREE_TYPE (ptr)) == REFERENCE_TYPE
2330 ? ptr : default_conversion (ptr));
2331 type = TREE_TYPE (pointer);
2333 if (TYPE_PTR_P (type) || TREE_CODE (type) == REFERENCE_TYPE)
2335 /* [expr.unary.op]
2337 If the type of the expression is "pointer to T," the type
2338 of the result is "T."
2340 We must use the canonical variant because certain parts of
2341 the back end, like fold, do pointer comparisons between
2342 types. */
2343 tree t = canonical_type_variant (TREE_TYPE (type));
2345 if (VOID_TYPE_P (t))
2347 /* A pointer to incomplete type (other than cv void) can be
2348 dereferenced [expr.unary.op]/1 */
2349 error ("`%T' is not a pointer-to-object type", type);
2350 return error_mark_node;
2352 else if (TREE_CODE (pointer) == ADDR_EXPR
2353 && !flag_volatile
2354 && same_type_p (t, TREE_TYPE (TREE_OPERAND (pointer, 0))))
2355 /* The POINTER was something like `&x'. We simplify `*&x' to
2356 `x'. */
2357 return TREE_OPERAND (pointer, 0);
2358 else
2360 tree ref = build1 (INDIRECT_REF, t, pointer);
2362 /* We *must* set TREE_READONLY when dereferencing a pointer to const,
2363 so that we get the proper error message if the result is used
2364 to assign to. Also, &* is supposed to be a no-op. */
2365 TREE_READONLY (ref) = CP_TYPE_CONST_P (t);
2366 TREE_THIS_VOLATILE (ref) = CP_TYPE_VOLATILE_P (t);
2367 TREE_SIDE_EFFECTS (ref)
2368 = (TREE_THIS_VOLATILE (ref) || TREE_SIDE_EFFECTS (pointer)
2369 || flag_volatile);
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 (mark_addressable (array) == 0)
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 (mark_addressable (array) == 0)
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 /* Build a function call to function FUNCTION with parameters PARAMS.
2544 PARAMS is a list--a chain of TREE_LIST nodes--in which the
2545 TREE_VALUE of each node is a parameter-expression. The PARAMS do
2546 not include any object pointer that may be required. FUNCTION's
2547 data type may be a function type or a pointer-to-function.
2549 For C++: If FUNCTION's data type is a TREE_LIST, then the tree list
2550 is the list of possible methods that FUNCTION could conceivably
2551 be. If the list of methods comes from a class, then it will be
2552 a list of lists (where each element is associated with the class
2553 that produced it), otherwise it will be a simple list (for
2554 functions overloaded in global scope).
2556 In the first case, TREE_VALUE (function) is the head of one of those
2557 lists, and TREE_PURPOSE is the name of the function.
2559 In the second case, TREE_PURPOSE (function) is the function's
2560 name directly.
2562 DECL is the class instance variable, usually CURRENT_CLASS_REF.
2564 When calling a TEMPLATE_DECL, we don't require a complete return
2565 type. */
2567 tree
2568 build_x_function_call (function, params, decl)
2569 tree function, params, decl;
2571 tree type;
2572 tree template_id = NULL_TREE;
2573 int is_method;
2575 if (function == error_mark_node)
2576 return error_mark_node;
2578 if (processing_template_decl)
2579 return build_min_nt (CALL_EXPR, function, params, NULL_TREE);
2581 /* Save explicit template arguments if found */
2582 if (TREE_CODE (function) == TEMPLATE_ID_EXPR)
2584 template_id = function;
2585 function = TREE_OPERAND (function, 0);
2588 type = TREE_TYPE (function);
2590 if (TREE_CODE (type) == OFFSET_TYPE
2591 && TREE_TYPE (type) == unknown_type_node
2592 && TREE_CODE (function) == TREE_LIST
2593 && TREE_CHAIN (function) == NULL_TREE)
2595 /* Undo (Foo:bar)()... */
2596 type = TYPE_OFFSET_BASETYPE (type);
2597 function = TREE_VALUE (function);
2598 my_friendly_assert (TREE_CODE (function) == TREE_LIST, 999);
2599 my_friendly_assert (TREE_CHAIN (function) == NULL_TREE, 999);
2600 function = TREE_VALUE (function);
2601 if (TREE_CODE (function) == OVERLOAD)
2602 function = OVL_FUNCTION (function);
2603 my_friendly_assert (TREE_CODE (function) == FUNCTION_DECL, 999);
2604 function = DECL_NAME (function);
2605 return build_method_call (decl, function, params,
2606 TYPE_BINFO (type), LOOKUP_NORMAL);
2609 if (TREE_CODE (function) == OFFSET_REF
2610 && TREE_CODE (type) != METHOD_TYPE)
2611 function = resolve_offset_ref (function);
2613 if ((TREE_CODE (function) == FUNCTION_DECL
2614 && DECL_STATIC_FUNCTION_P (function))
2615 || (DECL_FUNCTION_TEMPLATE_P (function)
2616 && DECL_STATIC_FUNCTION_P (DECL_TEMPLATE_RESULT (function))))
2617 return build_member_call (DECL_CONTEXT (function),
2618 template_id
2619 ? template_id : DECL_NAME (function),
2620 params);
2622 is_method = ((TREE_CODE (function) == TREE_LIST
2623 && current_class_type != NULL_TREE
2624 && (IDENTIFIER_CLASS_VALUE (TREE_PURPOSE (function))
2625 == function))
2626 || (TREE_CODE (function) == OVERLOAD
2627 && DECL_FUNCTION_MEMBER_P (OVL_CURRENT (function)))
2628 || TREE_CODE (function) == IDENTIFIER_NODE
2629 || TREE_CODE (type) == METHOD_TYPE
2630 || TYPE_PTRMEMFUNC_P (type));
2632 /* A friend template. Make it look like a toplevel declaration. */
2633 if (! is_method && TREE_CODE (function) == TEMPLATE_DECL)
2634 function = ovl_cons (function, NULL_TREE);
2636 /* Handle methods, friends, and overloaded functions, respectively. */
2637 if (is_method)
2639 tree basetype = NULL_TREE;
2641 if (TREE_CODE (function) == OVERLOAD)
2642 function = OVL_CURRENT (function);
2644 if (TREE_CODE (function) == FUNCTION_DECL
2645 || DECL_FUNCTION_TEMPLATE_P (function))
2647 basetype = DECL_CONTEXT (function);
2649 if (DECL_NAME (function))
2650 function = DECL_NAME (function);
2651 else
2652 function = TYPE_IDENTIFIER (DECL_CONTEXT (function));
2654 else if (TREE_CODE (function) == TREE_LIST)
2656 my_friendly_assert (TREE_CODE (TREE_VALUE (function))
2657 == FUNCTION_DECL, 312);
2658 basetype = DECL_CONTEXT (TREE_VALUE (function));
2659 function = TREE_PURPOSE (function);
2661 else if (TREE_CODE (function) != IDENTIFIER_NODE)
2663 if (TREE_CODE (function) == OFFSET_REF)
2665 if (TREE_OPERAND (function, 0))
2666 decl = TREE_OPERAND (function, 0);
2668 /* Call via a pointer to member function. */
2669 if (decl == NULL_TREE)
2671 error ("pointer to member function called, but not in class scope");
2672 return error_mark_node;
2674 /* What other type of POINTER_TYPE could this be? */
2675 if (TREE_CODE (TREE_TYPE (function)) != POINTER_TYPE
2676 && ! TYPE_PTRMEMFUNC_P (TREE_TYPE (function))
2677 && TREE_CODE (function) != OFFSET_REF)
2678 function = build (OFFSET_REF, TREE_TYPE (type), NULL_TREE,
2679 function);
2680 goto do_x_function;
2683 /* this is an abbreviated method call.
2684 must go through here in case it is a virtual function.
2685 @@ Perhaps this could be optimized. */
2687 if (basetype && (! current_class_type
2688 || ! DERIVED_FROM_P (basetype, current_class_type)))
2689 return build_member_call (basetype, function, params);
2691 if (decl == NULL_TREE)
2693 if (current_class_type == NULL_TREE)
2695 error ("object missing in call to method `%D'", function);
2696 return error_mark_node;
2698 /* Yow: call from a static member function. */
2699 decl = build_dummy_object (current_class_type);
2702 /* Put back explicit template arguments, if any. */
2703 if (template_id)
2704 function = template_id;
2705 return build_method_call (decl, function, params,
2706 NULL_TREE, LOOKUP_NORMAL);
2708 else if (TREE_CODE (function) == COMPONENT_REF
2709 && type == unknown_type_node)
2711 /* Undo what we did in build_component_ref. */
2712 decl = TREE_OPERAND (function, 0);
2713 function = TREE_OPERAND (function, 1);
2715 if (TREE_CODE (function) == TEMPLATE_ID_EXPR)
2717 my_friendly_assert (!template_id, 20011228);
2719 template_id = function;
2721 else
2723 function = DECL_NAME (OVL_CURRENT (function));
2725 if (template_id)
2727 TREE_OPERAND (template_id, 0) = function;
2728 function = template_id;
2732 return build_method_call (decl, function, params,
2733 NULL_TREE, LOOKUP_NORMAL);
2735 else if (really_overloaded_fn (function))
2737 if (OVL_FUNCTION (function) == NULL_TREE)
2739 error ("function `%D' declared overloaded, but no definitions appear with which to resolve it?!?",
2740 TREE_PURPOSE (function));
2741 return error_mark_node;
2743 else
2745 /* Put back explicit template arguments, if any. */
2746 if (template_id)
2747 function = template_id;
2748 return build_new_function_call (function, params);
2751 else
2752 /* Remove a potential OVERLOAD around it */
2753 function = OVL_CURRENT (function);
2755 do_x_function:
2756 if (TREE_CODE (function) == OFFSET_REF)
2758 /* If the component is a data element (or a virtual function), we play
2759 games here to make things work. */
2760 tree decl_addr;
2762 if (TREE_OPERAND (function, 0))
2763 decl = TREE_OPERAND (function, 0);
2764 else
2765 decl = current_class_ref;
2767 decl_addr = build_unary_op (ADDR_EXPR, decl, 0);
2769 /* Sigh. OFFSET_REFs are being used for too many things.
2770 They're being used both for -> and ->*, and we want to resolve
2771 the -> cases here, but leave the ->*. We could use
2772 resolve_offset_ref for those, too, but it would call
2773 get_member_function_from_ptrfunc and decl_addr wouldn't get
2774 updated properly. Nasty. */
2775 if (TREE_CODE (TREE_OPERAND (function, 1)) == FIELD_DECL)
2776 function = resolve_offset_ref (function);
2777 else
2778 function = TREE_OPERAND (function, 1);
2780 function = get_member_function_from_ptrfunc (&decl_addr, function);
2781 params = tree_cons (NULL_TREE, decl_addr, params);
2782 return build_function_call (function, params);
2785 type = TREE_TYPE (function);
2786 if (type != error_mark_node)
2788 if (TREE_CODE (type) == REFERENCE_TYPE)
2789 type = TREE_TYPE (type);
2791 if (IS_AGGR_TYPE (type))
2792 return build_opfncall (CALL_EXPR, LOOKUP_NORMAL, function, params, NULL_TREE);
2795 if (is_method)
2797 tree fntype = TREE_TYPE (function);
2798 tree ctypeptr = NULL_TREE;
2800 /* Explicitly named method? */
2801 if (TREE_CODE (function) == FUNCTION_DECL)
2802 ctypeptr = build_pointer_type (DECL_CLASS_CONTEXT (function));
2803 /* Expression with ptr-to-method type? It could either be a plain
2804 usage, or it might be a case where the ptr-to-method is being
2805 passed in as an argument. */
2806 else if (TYPE_PTRMEMFUNC_P (fntype))
2808 tree rec = TYPE_METHOD_BASETYPE (TREE_TYPE
2809 (TYPE_PTRMEMFUNC_FN_TYPE (fntype)));
2810 ctypeptr = build_pointer_type (rec);
2812 /* Unexpected node type? */
2813 else
2814 abort ();
2815 if (decl == NULL_TREE)
2817 if (current_function_decl
2818 && DECL_STATIC_FUNCTION_P (current_function_decl))
2819 error ("invalid call to member function needing `this' in static member function scope");
2820 else
2821 error ("pointer to member function called, but not in class scope");
2822 return error_mark_node;
2824 if (TREE_CODE (TREE_TYPE (decl)) != POINTER_TYPE
2825 && ! TYPE_PTRMEMFUNC_P (TREE_TYPE (decl)))
2827 tree binfo = lookup_base (TREE_TYPE (decl), TREE_TYPE (ctypeptr),
2828 ba_check, NULL);
2830 decl = build_unary_op (ADDR_EXPR, decl, 0);
2831 decl = build_base_path (PLUS_EXPR, decl, binfo, 1);
2833 else
2834 decl = build_c_cast (ctypeptr, decl);
2835 params = tree_cons (NULL_TREE, decl, params);
2838 return build_function_call (function, params);
2841 /* Resolve a pointer to member function. INSTANCE is the object
2842 instance to use, if the member points to a virtual member. */
2844 tree
2845 get_member_function_from_ptrfunc (instance_ptrptr, function)
2846 tree *instance_ptrptr;
2847 tree function;
2849 if (TREE_CODE (function) == OFFSET_REF)
2850 function = TREE_OPERAND (function, 1);
2852 if (TYPE_PTRMEMFUNC_P (TREE_TYPE (function)))
2854 tree fntype, idx, e1, delta, delta2, e2, e3, vtbl;
2855 tree instance, basetype;
2857 tree instance_ptr = *instance_ptrptr;
2859 if (instance_ptr == error_mark_node
2860 && TREE_CODE (function) == PTRMEM_CST)
2862 /* Extracting the function address from a pmf is only
2863 allowed with -Wno-pmf-conversions. It only works for
2864 pmf constants. */
2865 e1 = build_addr_func (PTRMEM_CST_MEMBER (function));
2866 e1 = convert (TYPE_PTRMEMFUNC_FN_TYPE (TREE_TYPE (function)), e1);
2867 return e1;
2870 if (TREE_SIDE_EFFECTS (instance_ptr))
2871 instance_ptr = save_expr (instance_ptr);
2873 if (TREE_SIDE_EFFECTS (function))
2874 function = save_expr (function);
2876 fntype = TYPE_PTRMEMFUNC_FN_TYPE (TREE_TYPE (function));
2877 basetype = TYPE_METHOD_BASETYPE (TREE_TYPE (fntype));
2879 /* Convert down to the right base, before using the instance. */
2880 instance = lookup_base (TREE_TYPE (TREE_TYPE (instance_ptr)), basetype,
2881 ba_check, NULL);
2882 instance = build_base_path (PLUS_EXPR, instance_ptr, instance, 1);
2883 if (instance == error_mark_node && instance_ptr != error_mark_node)
2884 return instance;
2886 e3 = PFN_FROM_PTRMEMFUNC (function);
2888 vtbl = build1 (NOP_EXPR, build_pointer_type (ptr_type_node), instance);
2889 TREE_CONSTANT (vtbl) = TREE_CONSTANT (instance);
2891 delta = cp_convert (ptrdiff_type_node,
2892 build_component_ref (function, delta_identifier,
2893 NULL_TREE, 0));
2895 /* This used to avoid checking for virtual functions if basetype
2896 has no virtual functions, according to an earlier ANSI draft.
2897 With the final ISO C++ rules, such an optimization is
2898 incorrect: A pointer to a derived member can be static_cast
2899 to pointer-to-base-member, as long as the dynamic object
2900 later has the right member. */
2902 /* Promoting idx before saving it improves performance on RISC
2903 targets. Without promoting, the first compare used
2904 load-with-sign-extend, while the second used normal load then
2905 shift to sign-extend. An optimizer flaw, perhaps, but it's
2906 easier to make this change. */
2907 switch (TARGET_PTRMEMFUNC_VBIT_LOCATION)
2909 case ptrmemfunc_vbit_in_pfn:
2910 idx = cp_build_binary_op (TRUNC_DIV_EXPR,
2911 build1 (NOP_EXPR, vtable_index_type, e3),
2912 TYPE_SIZE_UNIT (vtable_entry_type));
2913 e1 = cp_build_binary_op (BIT_AND_EXPR,
2914 build1 (NOP_EXPR, vtable_index_type, e3),
2915 integer_one_node);
2916 break;
2918 case ptrmemfunc_vbit_in_delta:
2919 idx = build1 (NOP_EXPR, vtable_index_type, e3);
2920 e1 = cp_build_binary_op (BIT_AND_EXPR,
2921 delta, integer_one_node);
2922 delta = cp_build_binary_op (RSHIFT_EXPR,
2923 build1 (NOP_EXPR, vtable_index_type,
2924 delta),
2925 integer_one_node);
2926 break;
2928 default:
2929 abort ();
2932 /* DELTA2 is the amount by which to adjust the `this' pointer
2933 to find the vtbl. */
2934 delta2 = delta;
2935 vtbl = build
2936 (PLUS_EXPR,
2937 build_pointer_type (build_pointer_type (vtable_entry_type)),
2938 vtbl, cp_convert (ptrdiff_type_node, delta2));
2939 vtbl = build_indirect_ref (vtbl, NULL);
2940 e2 = build_array_ref (vtbl, idx);
2942 /* When using function descriptors, the address of the
2943 vtable entry is treated as a function pointer. */
2944 if (TARGET_VTABLE_USES_DESCRIPTORS)
2945 e2 = build1 (NOP_EXPR, TREE_TYPE (e2),
2946 build_unary_op (ADDR_EXPR, e2, /*noconvert=*/1));
2948 TREE_TYPE (e2) = TREE_TYPE (e3);
2949 e1 = build_conditional_expr (e1, e2, e3);
2951 /* Make sure this doesn't get evaluated first inside one of the
2952 branches of the COND_EXPR. */
2953 if (TREE_CODE (instance_ptr) == SAVE_EXPR)
2954 e1 = build (COMPOUND_EXPR, TREE_TYPE (e1),
2955 instance_ptr, e1);
2957 *instance_ptrptr = build (PLUS_EXPR, TREE_TYPE (instance_ptr),
2958 instance_ptr, delta);
2960 if (instance_ptr == error_mark_node
2961 && TREE_CODE (e1) != ADDR_EXPR
2962 && TREE_CODE (TREE_OPERAND (e1, 0)) != FUNCTION_DECL)
2963 error ("object missing in `%E'", function);
2965 function = e1;
2967 return function;
2970 tree
2971 build_function_call_real (function, params, require_complete, flags)
2972 tree function, params;
2973 int require_complete, flags;
2975 register tree fntype, fndecl;
2976 register tree value_type;
2977 register tree coerced_params;
2978 tree result;
2979 tree name = NULL_TREE, assembler_name = NULL_TREE;
2980 int is_method;
2981 tree original = function;
2983 /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
2984 Strip such NOP_EXPRs, since FUNCTION is used in non-lvalue context. */
2985 if (TREE_CODE (function) == NOP_EXPR
2986 && TREE_TYPE (function) == TREE_TYPE (TREE_OPERAND (function, 0)))
2987 function = TREE_OPERAND (function, 0);
2989 if (TREE_CODE (function) == FUNCTION_DECL)
2991 name = DECL_NAME (function);
2992 assembler_name = DECL_ASSEMBLER_NAME (function);
2994 GNU_xref_call (current_function_decl,
2995 IDENTIFIER_POINTER (name ? name
2996 : TYPE_IDENTIFIER (DECL_CLASS_CONTEXT
2997 (function))));
2998 mark_used (function);
2999 fndecl = function;
3001 /* Convert anything with function type to a pointer-to-function. */
3002 if (pedantic && DECL_MAIN_P (function))
3003 pedwarn ("ISO C++ forbids calling `::main' from within program");
3005 /* Differs from default_conversion by not setting TREE_ADDRESSABLE
3006 (because calling an inline function does not mean the function
3007 needs to be separately compiled). */
3009 if (DECL_INLINE (function))
3010 function = inline_conversion (function);
3011 else
3012 function = build_addr_func (function);
3014 else
3016 fndecl = NULL_TREE;
3018 function = build_addr_func (function);
3021 if (function == error_mark_node)
3022 return error_mark_node;
3024 fntype = TREE_TYPE (function);
3026 if (TYPE_PTRMEMFUNC_P (fntype))
3028 error ("must use .* or ->* to call pointer-to-member function in `%E (...)'",
3029 original);
3030 return error_mark_node;
3033 is_method = (TREE_CODE (fntype) == POINTER_TYPE
3034 && TREE_CODE (TREE_TYPE (fntype)) == METHOD_TYPE);
3036 if (!((TREE_CODE (fntype) == POINTER_TYPE
3037 && TREE_CODE (TREE_TYPE (fntype)) == FUNCTION_TYPE)
3038 || is_method
3039 || TREE_CODE (function) == TEMPLATE_ID_EXPR))
3041 error ("`%E' cannot be used as a function", original);
3042 return error_mark_node;
3045 /* fntype now gets the type of function pointed to. */
3046 fntype = TREE_TYPE (fntype);
3048 /* Convert the parameters to the types declared in the
3049 function prototype, or apply default promotions. */
3051 if (flags & LOOKUP_COMPLAIN)
3052 coerced_params = convert_arguments (TYPE_ARG_TYPES (fntype),
3053 params, fndecl, LOOKUP_NORMAL);
3054 else
3055 coerced_params = convert_arguments (TYPE_ARG_TYPES (fntype),
3056 params, fndecl, 0);
3058 if (coerced_params == error_mark_node)
3060 if (flags & LOOKUP_SPECULATIVELY)
3061 return NULL_TREE;
3062 else
3063 return error_mark_node;
3066 /* Check for errors in format strings. */
3068 if (warn_format)
3069 check_function_format (NULL, TYPE_ATTRIBUTES (fntype), coerced_params);
3071 /* Recognize certain built-in functions so we can make tree-codes
3072 other than CALL_EXPR. We do this when it enables fold-const.c
3073 to do something useful. */
3075 if (TREE_CODE (function) == ADDR_EXPR
3076 && TREE_CODE (TREE_OPERAND (function, 0)) == FUNCTION_DECL
3077 && DECL_BUILT_IN (TREE_OPERAND (function, 0)))
3079 result = expand_tree_builtin (TREE_OPERAND (function, 0),
3080 params, coerced_params);
3081 if (result)
3082 return result;
3085 /* Some built-in function calls will be evaluated at
3086 compile-time in fold (). */
3087 result = fold (build_call (function, coerced_params));
3088 value_type = TREE_TYPE (result);
3090 if (require_complete)
3092 if (TREE_CODE (value_type) == VOID_TYPE)
3093 return result;
3094 result = require_complete_type (result);
3096 if (IS_AGGR_TYPE (value_type))
3097 result = build_cplus_new (value_type, result);
3098 return convert_from_reference (result);
3101 tree
3102 build_function_call (function, params)
3103 tree function, params;
3105 return build_function_call_real (function, params, 1, LOOKUP_NORMAL);
3108 /* Convert the actual parameter expressions in the list VALUES
3109 to the types in the list TYPELIST.
3110 If parmdecls is exhausted, or when an element has NULL as its type,
3111 perform the default conversions.
3113 NAME is an IDENTIFIER_NODE or 0. It is used only for error messages.
3115 This is also where warnings about wrong number of args are generated.
3117 Return a list of expressions for the parameters as converted.
3119 Both VALUES and the returned value are chains of TREE_LIST nodes
3120 with the elements of the list in the TREE_VALUE slots of those nodes.
3122 In C++, unspecified trailing parameters can be filled in with their
3123 default arguments, if such were specified. Do so here. */
3125 tree
3126 convert_arguments (typelist, values, fndecl, flags)
3127 tree typelist, values, fndecl;
3128 int flags;
3130 register tree typetail, valtail;
3131 register tree result = NULL_TREE;
3132 const char *called_thing = 0;
3133 int i = 0;
3135 /* Argument passing is always copy-initialization. */
3136 flags |= LOOKUP_ONLYCONVERTING;
3138 if (fndecl)
3140 if (TREE_CODE (TREE_TYPE (fndecl)) == METHOD_TYPE)
3142 if (DECL_NAME (fndecl) == NULL_TREE
3143 || IDENTIFIER_HAS_TYPE_VALUE (DECL_NAME (fndecl)))
3144 called_thing = "constructor";
3145 else
3146 called_thing = "member function";
3148 else
3149 called_thing = "function";
3152 for (valtail = values, typetail = typelist;
3153 valtail;
3154 valtail = TREE_CHAIN (valtail), i++)
3156 register tree type = typetail ? TREE_VALUE (typetail) : 0;
3157 register tree val = TREE_VALUE (valtail);
3159 if (val == error_mark_node)
3160 return error_mark_node;
3162 if (type == void_type_node)
3164 if (fndecl)
3166 cp_error_at ("too many arguments to %s `%+#D'", called_thing,
3167 fndecl);
3168 error ("at this point in file");
3170 else
3171 error ("too many arguments to function");
3172 /* In case anybody wants to know if this argument
3173 list is valid. */
3174 if (result)
3175 TREE_TYPE (tree_last (result)) = error_mark_node;
3176 break;
3179 if (TREE_CODE (val) == OFFSET_REF)
3180 val = resolve_offset_ref (val);
3182 /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
3183 Strip such NOP_EXPRs, since VAL is used in non-lvalue context. */
3184 if (TREE_CODE (val) == NOP_EXPR
3185 && TREE_TYPE (val) == TREE_TYPE (TREE_OPERAND (val, 0))
3186 && (type == 0 || TREE_CODE (type) != REFERENCE_TYPE))
3187 val = TREE_OPERAND (val, 0);
3189 if (type == 0 || TREE_CODE (type) != REFERENCE_TYPE)
3191 if (TREE_CODE (TREE_TYPE (val)) == ARRAY_TYPE
3192 || TREE_CODE (TREE_TYPE (val)) == FUNCTION_TYPE
3193 || TREE_CODE (TREE_TYPE (val)) == METHOD_TYPE)
3194 val = default_conversion (val);
3197 if (val == error_mark_node)
3198 return error_mark_node;
3200 if (type != 0)
3202 /* Formal parm type is specified by a function prototype. */
3203 tree parmval;
3205 if (!COMPLETE_TYPE_P (complete_type (type)))
3207 error ("parameter type of called function is incomplete");
3208 parmval = val;
3210 else
3212 parmval = convert_for_initialization
3213 (NULL_TREE, type, val, flags,
3214 "argument passing", fndecl, i);
3215 if (PROMOTE_PROTOTYPES
3216 && INTEGRAL_TYPE_P (type)
3217 && (TYPE_PRECISION (type)
3218 < TYPE_PRECISION (integer_type_node)))
3219 parmval = default_conversion (parmval);
3222 if (parmval == error_mark_node)
3223 return error_mark_node;
3225 result = tree_cons (NULL_TREE, parmval, result);
3227 else
3229 if (TREE_CODE (TREE_TYPE (val)) == REFERENCE_TYPE)
3230 val = convert_from_reference (val);
3232 if (fndecl && DECL_BUILT_IN (fndecl)
3233 && DECL_FUNCTION_CODE (fndecl) == BUILT_IN_CONSTANT_P)
3234 /* Don't do ellipsis conversion for __built_in_constant_p
3235 as this will result in spurious warnings for non-POD
3236 types. */
3237 val = require_complete_type (val);
3238 else
3239 val = convert_arg_to_ellipsis (val);
3241 result = tree_cons (NULL_TREE, val, result);
3244 if (typetail)
3245 typetail = TREE_CHAIN (typetail);
3248 if (typetail != 0 && typetail != void_list_node)
3250 /* See if there are default arguments that can be used */
3251 if (TREE_PURPOSE (typetail))
3253 for (; typetail != void_list_node; ++i)
3255 tree parmval
3256 = convert_default_arg (TREE_VALUE (typetail),
3257 TREE_PURPOSE (typetail),
3258 fndecl, i);
3260 if (parmval == error_mark_node)
3261 return error_mark_node;
3263 result = tree_cons (0, parmval, result);
3264 typetail = TREE_CHAIN (typetail);
3265 /* ends with `...'. */
3266 if (typetail == NULL_TREE)
3267 break;
3270 else
3272 if (fndecl)
3274 cp_error_at ("too few arguments to %s `%+#D'",
3275 called_thing, fndecl);
3276 error ("at this point in file");
3278 else
3279 error ("too few arguments to function");
3280 return error_mark_list;
3284 return nreverse (result);
3287 /* Build a binary-operation expression, after performing default
3288 conversions on the operands. CODE is the kind of expression to build. */
3290 tree
3291 build_x_binary_op (code, arg1, arg2)
3292 enum tree_code code;
3293 tree arg1, arg2;
3295 if (processing_template_decl)
3296 return build_min_nt (code, arg1, arg2);
3298 return build_new_op (code, LOOKUP_NORMAL, arg1, arg2, NULL_TREE);
3301 /* Build a binary-operation expression without default conversions.
3302 CODE is the kind of expression to build.
3303 This function differs from `build' in several ways:
3304 the data type of the result is computed and recorded in it,
3305 warnings are generated if arg data types are invalid,
3306 special handling for addition and subtraction of pointers is known,
3307 and some optimization is done (operations on narrow ints
3308 are done in the narrower type when that gives the same result).
3309 Constant folding is also done before the result is returned.
3311 Note that the operands will never have enumeral types
3312 because either they have just had the default conversions performed
3313 or they have both just been converted to some other type in which
3314 the arithmetic is to be done.
3316 C++: must do special pointer arithmetic when implementing
3317 multiple inheritance, and deal with pointer to member functions. */
3319 tree
3320 build_binary_op (code, orig_op0, orig_op1, convert_p)
3321 enum tree_code code;
3322 tree orig_op0, orig_op1;
3323 int convert_p ATTRIBUTE_UNUSED;
3325 tree op0, op1;
3326 register enum tree_code code0, code1;
3327 tree type0, type1;
3329 /* Expression code to give to the expression when it is built.
3330 Normally this is CODE, which is what the caller asked for,
3331 but in some special cases we change it. */
3332 register enum tree_code resultcode = code;
3334 /* Data type in which the computation is to be performed.
3335 In the simplest cases this is the common type of the arguments. */
3336 register tree result_type = NULL;
3338 /* Nonzero means operands have already been type-converted
3339 in whatever way is necessary.
3340 Zero means they need to be converted to RESULT_TYPE. */
3341 int converted = 0;
3343 /* Nonzero means create the expression with this type, rather than
3344 RESULT_TYPE. */
3345 tree build_type = 0;
3347 /* Nonzero means after finally constructing the expression
3348 convert it to this type. */
3349 tree final_type = 0;
3351 /* Nonzero if this is an operation like MIN or MAX which can
3352 safely be computed in short if both args are promoted shorts.
3353 Also implies COMMON.
3354 -1 indicates a bitwise operation; this makes a difference
3355 in the exact conditions for when it is safe to do the operation
3356 in a narrower mode. */
3357 int shorten = 0;
3359 /* Nonzero if this is a comparison operation;
3360 if both args are promoted shorts, compare the original shorts.
3361 Also implies COMMON. */
3362 int short_compare = 0;
3364 /* Nonzero if this is a right-shift operation, which can be computed on the
3365 original short and then promoted if the operand is a promoted short. */
3366 int short_shift = 0;
3368 /* Nonzero means set RESULT_TYPE to the common type of the args. */
3369 int common = 0;
3371 /* Apply default conversions. */
3372 op0 = orig_op0;
3373 op1 = orig_op1;
3375 if (code == TRUTH_AND_EXPR || code == TRUTH_ANDIF_EXPR
3376 || code == TRUTH_OR_EXPR || code == TRUTH_ORIF_EXPR
3377 || code == TRUTH_XOR_EXPR)
3379 if (!really_overloaded_fn (op0))
3380 op0 = decay_conversion (op0);
3381 if (!really_overloaded_fn (op1))
3382 op1 = decay_conversion (op1);
3384 else
3386 if (!really_overloaded_fn (op0))
3387 op0 = default_conversion (op0);
3388 if (!really_overloaded_fn (op1))
3389 op1 = default_conversion (op1);
3392 /* Strip NON_LVALUE_EXPRs, etc., since we aren't using as an lvalue. */
3393 STRIP_TYPE_NOPS (op0);
3394 STRIP_TYPE_NOPS (op1);
3396 /* DTRT if one side is an overloaded function, but complain about it. */
3397 if (type_unknown_p (op0))
3399 tree t = instantiate_type (TREE_TYPE (op1), op0, itf_none);
3400 if (t != error_mark_node)
3402 pedwarn ("assuming cast to type `%T' from overloaded function",
3403 TREE_TYPE (t));
3404 op0 = t;
3407 if (type_unknown_p (op1))
3409 tree t = instantiate_type (TREE_TYPE (op0), op1, itf_none);
3410 if (t != error_mark_node)
3412 pedwarn ("assuming cast to type `%T' from overloaded function",
3413 TREE_TYPE (t));
3414 op1 = t;
3418 type0 = TREE_TYPE (op0);
3419 type1 = TREE_TYPE (op1);
3421 /* The expression codes of the data types of the arguments tell us
3422 whether the arguments are integers, floating, pointers, etc. */
3423 code0 = TREE_CODE (type0);
3424 code1 = TREE_CODE (type1);
3426 /* If an error was already reported for one of the arguments,
3427 avoid reporting another error. */
3429 if (code0 == ERROR_MARK || code1 == ERROR_MARK)
3430 return error_mark_node;
3432 switch (code)
3434 case PLUS_EXPR:
3435 /* Handle the pointer + int case. */
3436 if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
3437 return pointer_int_sum (PLUS_EXPR, op0, op1);
3438 else if (code1 == POINTER_TYPE && code0 == INTEGER_TYPE)
3439 return pointer_int_sum (PLUS_EXPR, op1, op0);
3440 else
3441 common = 1;
3442 break;
3444 case MINUS_EXPR:
3445 /* Subtraction of two similar pointers.
3446 We must subtract them as integers, then divide by object size. */
3447 if (code0 == POINTER_TYPE && code1 == POINTER_TYPE
3448 && comp_target_types (type0, type1, 1))
3449 return pointer_diff (op0, op1, common_type (type0, type1));
3450 /* Handle pointer minus int. Just like pointer plus int. */
3451 else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
3452 return pointer_int_sum (MINUS_EXPR, op0, op1);
3453 else
3454 common = 1;
3455 break;
3457 case MULT_EXPR:
3458 common = 1;
3459 break;
3461 case TRUNC_DIV_EXPR:
3462 case CEIL_DIV_EXPR:
3463 case FLOOR_DIV_EXPR:
3464 case ROUND_DIV_EXPR:
3465 case EXACT_DIV_EXPR:
3466 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE
3467 || code0 == COMPLEX_TYPE)
3468 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
3469 || code1 == COMPLEX_TYPE))
3471 if (TREE_CODE (op1) == INTEGER_CST && integer_zerop (op1))
3472 warning ("division by zero in `%E / 0'", op0);
3473 else if (TREE_CODE (op1) == REAL_CST && real_zerop (op1))
3474 warning ("division by zero in `%E / 0.'", op0);
3476 if (!(code0 == INTEGER_TYPE && code1 == INTEGER_TYPE))
3477 resultcode = RDIV_EXPR;
3478 else
3479 /* When dividing two signed integers, we have to promote to int.
3480 unless we divide by a constant != -1. Note that default
3481 conversion will have been performed on the operands at this
3482 point, so we have to dig out the original type to find out if
3483 it was unsigned. */
3484 shorten = ((TREE_CODE (op0) == NOP_EXPR
3485 && TREE_UNSIGNED (TREE_TYPE (TREE_OPERAND (op0, 0))))
3486 || (TREE_CODE (op1) == INTEGER_CST
3487 && ! integer_all_onesp (op1)));
3489 common = 1;
3491 break;
3493 case BIT_AND_EXPR:
3494 case BIT_ANDTC_EXPR:
3495 case BIT_IOR_EXPR:
3496 case BIT_XOR_EXPR:
3497 if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
3498 shorten = -1;
3499 /* If one operand is a constant, and the other is a short type
3500 that has been converted to an int,
3501 really do the work in the short type and then convert the
3502 result to int. If we are lucky, the constant will be 0 or 1
3503 in the short type, making the entire operation go away. */
3504 if (TREE_CODE (op0) == INTEGER_CST
3505 && TREE_CODE (op1) == NOP_EXPR
3506 && (TYPE_PRECISION (type1)
3507 > TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (op1, 0))))
3508 && TREE_UNSIGNED (TREE_TYPE (TREE_OPERAND (op1, 0))))
3510 final_type = result_type;
3511 op1 = TREE_OPERAND (op1, 0);
3512 result_type = TREE_TYPE (op1);
3514 if (TREE_CODE (op1) == INTEGER_CST
3515 && TREE_CODE (op0) == NOP_EXPR
3516 && (TYPE_PRECISION (type0)
3517 > TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (op0, 0))))
3518 && TREE_UNSIGNED (TREE_TYPE (TREE_OPERAND (op0, 0))))
3520 final_type = result_type;
3521 op0 = TREE_OPERAND (op0, 0);
3522 result_type = TREE_TYPE (op0);
3524 break;
3526 case TRUNC_MOD_EXPR:
3527 case FLOOR_MOD_EXPR:
3528 if (code1 == INTEGER_TYPE && integer_zerop (op1))
3529 warning ("division by zero in `%E %% 0'", op0);
3530 else if (code1 == REAL_TYPE && real_zerop (op1))
3531 warning ("division by zero in `%E %% 0.'", op0);
3533 if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
3535 /* Although it would be tempting to shorten always here, that loses
3536 on some targets, since the modulo instruction is undefined if the
3537 quotient can't be represented in the computation mode. We shorten
3538 only if unsigned or if dividing by something we know != -1. */
3539 shorten = ((TREE_CODE (op0) == NOP_EXPR
3540 && TREE_UNSIGNED (TREE_TYPE (TREE_OPERAND (op0, 0))))
3541 || (TREE_CODE (op1) == INTEGER_CST
3542 && ! integer_all_onesp (op1)));
3543 common = 1;
3545 break;
3547 case TRUTH_ANDIF_EXPR:
3548 case TRUTH_ORIF_EXPR:
3549 case TRUTH_AND_EXPR:
3550 case TRUTH_OR_EXPR:
3551 result_type = boolean_type_node;
3552 break;
3554 /* Shift operations: result has same type as first operand;
3555 always convert second operand to int.
3556 Also set SHORT_SHIFT if shifting rightward. */
3558 case RSHIFT_EXPR:
3559 if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
3561 result_type = type0;
3562 if (TREE_CODE (op1) == INTEGER_CST)
3564 if (tree_int_cst_lt (op1, integer_zero_node))
3565 warning ("right shift count is negative");
3566 else
3568 if (! integer_zerop (op1))
3569 short_shift = 1;
3570 if (compare_tree_int (op1, TYPE_PRECISION (type0)) >= 0)
3571 warning ("right shift count >= width of type");
3574 /* Convert the shift-count to an integer, regardless of
3575 size of value being shifted. */
3576 if (TYPE_MAIN_VARIANT (TREE_TYPE (op1)) != integer_type_node)
3577 op1 = cp_convert (integer_type_node, op1);
3578 /* Avoid converting op1 to result_type later. */
3579 converted = 1;
3581 break;
3583 case LSHIFT_EXPR:
3584 if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
3586 result_type = type0;
3587 if (TREE_CODE (op1) == INTEGER_CST)
3589 if (tree_int_cst_lt (op1, integer_zero_node))
3590 warning ("left shift count is negative");
3591 else if (compare_tree_int (op1, TYPE_PRECISION (type0)) >= 0)
3592 warning ("left shift count >= width of type");
3594 /* Convert the shift-count to an integer, regardless of
3595 size of value being shifted. */
3596 if (TYPE_MAIN_VARIANT (TREE_TYPE (op1)) != integer_type_node)
3597 op1 = cp_convert (integer_type_node, op1);
3598 /* Avoid converting op1 to result_type later. */
3599 converted = 1;
3601 break;
3603 case RROTATE_EXPR:
3604 case LROTATE_EXPR:
3605 if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
3607 result_type = type0;
3608 if (TREE_CODE (op1) == INTEGER_CST)
3610 if (tree_int_cst_lt (op1, integer_zero_node))
3611 warning ("%s rotate count is negative",
3612 (code == LROTATE_EXPR) ? "left" : "right");
3613 else if (compare_tree_int (op1, TYPE_PRECISION (type0)) >= 0)
3614 warning ("%s rotate count >= width of type",
3615 (code == LROTATE_EXPR) ? "left" : "right");
3617 /* Convert the shift-count to an integer, regardless of
3618 size of value being shifted. */
3619 if (TYPE_MAIN_VARIANT (TREE_TYPE (op1)) != integer_type_node)
3620 op1 = cp_convert (integer_type_node, op1);
3622 break;
3624 case EQ_EXPR:
3625 case NE_EXPR:
3626 if (warn_float_equal && (code0 == REAL_TYPE || code1 == REAL_TYPE))
3627 warning ("comparing floating point with == or != is unsafe");
3629 build_type = boolean_type_node;
3630 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE
3631 || code0 == COMPLEX_TYPE)
3632 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
3633 || code1 == COMPLEX_TYPE))
3634 short_compare = 1;
3635 else if (code0 == POINTER_TYPE && code1 == POINTER_TYPE)
3636 result_type = composite_pointer_type (type0, type1, op0, op1,
3637 "comparison");
3638 else if (code0 == POINTER_TYPE && null_ptr_cst_p (op1))
3639 result_type = type0;
3640 else if (code1 == POINTER_TYPE && null_ptr_cst_p (op0))
3641 result_type = type1;
3642 else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
3644 result_type = type0;
3645 error ("ISO C++ forbids comparison between pointer and integer");
3647 else if (code0 == INTEGER_TYPE && code1 == POINTER_TYPE)
3649 result_type = type1;
3650 error ("ISO C++ forbids comparison between pointer and integer");
3652 else if (TYPE_PTRMEMFUNC_P (type0) && null_ptr_cst_p (op1))
3654 op0 = build_component_ref (op0, pfn_identifier, NULL_TREE, 0);
3655 op1 = cp_convert (TREE_TYPE (op0), integer_zero_node);
3656 result_type = TREE_TYPE (op0);
3658 else if (TYPE_PTRMEMFUNC_P (type1) && null_ptr_cst_p (op0))
3659 return cp_build_binary_op (code, op1, op0);
3660 else if (TYPE_PTRMEMFUNC_P (type0) && TYPE_PTRMEMFUNC_P (type1)
3661 && same_type_p (type0, type1))
3663 /* E will be the final comparison. */
3664 tree e;
3665 /* E1 and E2 are for scratch. */
3666 tree e1;
3667 tree e2;
3668 tree pfn0;
3669 tree pfn1;
3670 tree delta0;
3671 tree delta1;
3673 if (TREE_SIDE_EFFECTS (op0))
3674 op0 = save_expr (op0);
3675 if (TREE_SIDE_EFFECTS (op1))
3676 op1 = save_expr (op1);
3678 /* We generate:
3680 (op0.pfn == op1.pfn
3681 && (!op0.pfn || op0.delta == op1.delta))
3683 The reason for the `!op0.pfn' bit is that a NULL
3684 pointer-to-member is any member with a zero PFN; the
3685 DELTA field is unspecified. */
3686 pfn0 = pfn_from_ptrmemfunc (op0);
3687 pfn1 = pfn_from_ptrmemfunc (op1);
3688 delta0 = build_component_ref (op0, delta_identifier,
3689 NULL_TREE, 0);
3690 delta1 = build_component_ref (op1, delta_identifier,
3691 NULL_TREE, 0);
3692 e1 = cp_build_binary_op (EQ_EXPR, delta0, delta1);
3693 e2 = cp_build_binary_op (EQ_EXPR,
3694 pfn0,
3695 cp_convert (TREE_TYPE (pfn0),
3696 integer_zero_node));
3697 e1 = cp_build_binary_op (TRUTH_ORIF_EXPR, e1, e2);
3698 e2 = build (EQ_EXPR, boolean_type_node, pfn0, pfn1);
3699 e = cp_build_binary_op (TRUTH_ANDIF_EXPR, e2, e1);
3700 if (code == EQ_EXPR)
3701 return e;
3702 return cp_build_binary_op (EQ_EXPR, e, integer_zero_node);
3704 else if ((TYPE_PTRMEMFUNC_P (type0)
3705 && same_type_p (TYPE_PTRMEMFUNC_FN_TYPE (type0), type1))
3706 || (TYPE_PTRMEMFUNC_P (type1)
3707 && same_type_p (TYPE_PTRMEMFUNC_FN_TYPE (type1), type0)))
3708 abort ();
3709 break;
3711 case MAX_EXPR:
3712 case MIN_EXPR:
3713 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE)
3714 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE))
3715 shorten = 1;
3716 else if (code0 == POINTER_TYPE && code1 == POINTER_TYPE)
3717 result_type = composite_pointer_type (type0, type1, op0, op1,
3718 "comparison");
3719 break;
3721 case LE_EXPR:
3722 case GE_EXPR:
3723 case LT_EXPR:
3724 case GT_EXPR:
3725 build_type = boolean_type_node;
3726 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE)
3727 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE))
3728 short_compare = 1;
3729 else if (code0 == POINTER_TYPE && code1 == POINTER_TYPE)
3730 result_type = composite_pointer_type (type0, type1, op0, op1,
3731 "comparison");
3732 else if (code0 == POINTER_TYPE && TREE_CODE (op1) == INTEGER_CST
3733 && integer_zerop (op1))
3734 result_type = type0;
3735 else if (code1 == POINTER_TYPE && TREE_CODE (op0) == INTEGER_CST
3736 && integer_zerop (op0))
3737 result_type = type1;
3738 else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
3740 result_type = type0;
3741 pedwarn ("ISO C++ forbids comparison between pointer and integer");
3743 else if (code0 == INTEGER_TYPE && code1 == POINTER_TYPE)
3745 result_type = type1;
3746 pedwarn ("ISO C++ forbids comparison between pointer and integer");
3748 break;
3750 case UNORDERED_EXPR:
3751 case ORDERED_EXPR:
3752 case UNLT_EXPR:
3753 case UNLE_EXPR:
3754 case UNGT_EXPR:
3755 case UNGE_EXPR:
3756 case UNEQ_EXPR:
3757 build_type = integer_type_node;
3758 if (code0 != REAL_TYPE || code1 != REAL_TYPE)
3760 error ("unordered comparison on non-floating point argument");
3761 return error_mark_node;
3763 common = 1;
3764 break;
3766 default:
3767 break;
3770 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE || code0 == COMPLEX_TYPE)
3772 (code1 == INTEGER_TYPE || code1 == REAL_TYPE || code1 == COMPLEX_TYPE))
3774 int none_complex = (code0 != COMPLEX_TYPE && code1 != COMPLEX_TYPE);
3776 if (shorten || common || short_compare)
3777 result_type = common_type (type0, type1);
3779 /* For certain operations (which identify themselves by shorten != 0)
3780 if both args were extended from the same smaller type,
3781 do the arithmetic in that type and then extend.
3783 shorten !=0 and !=1 indicates a bitwise operation.
3784 For them, this optimization is safe only if
3785 both args are zero-extended or both are sign-extended.
3786 Otherwise, we might change the result.
3787 Eg, (short)-1 | (unsigned short)-1 is (int)-1
3788 but calculated in (unsigned short) it would be (unsigned short)-1. */
3790 if (shorten && none_complex)
3792 int unsigned0, unsigned1;
3793 tree arg0 = get_narrower (op0, &unsigned0);
3794 tree arg1 = get_narrower (op1, &unsigned1);
3795 /* UNS is 1 if the operation to be done is an unsigned one. */
3796 int uns = TREE_UNSIGNED (result_type);
3797 tree type;
3799 final_type = result_type;
3801 /* Handle the case that OP0 does not *contain* a conversion
3802 but it *requires* conversion to FINAL_TYPE. */
3804 if (op0 == arg0 && TREE_TYPE (op0) != final_type)
3805 unsigned0 = TREE_UNSIGNED (TREE_TYPE (op0));
3806 if (op1 == arg1 && TREE_TYPE (op1) != final_type)
3807 unsigned1 = TREE_UNSIGNED (TREE_TYPE (op1));
3809 /* Now UNSIGNED0 is 1 if ARG0 zero-extends to FINAL_TYPE. */
3811 /* For bitwise operations, signedness of nominal type
3812 does not matter. Consider only how operands were extended. */
3813 if (shorten == -1)
3814 uns = unsigned0;
3816 /* Note that in all three cases below we refrain from optimizing
3817 an unsigned operation on sign-extended args.
3818 That would not be valid. */
3820 /* Both args variable: if both extended in same way
3821 from same width, do it in that width.
3822 Do it unsigned if args were zero-extended. */
3823 if ((TYPE_PRECISION (TREE_TYPE (arg0))
3824 < TYPE_PRECISION (result_type))
3825 && (TYPE_PRECISION (TREE_TYPE (arg1))
3826 == TYPE_PRECISION (TREE_TYPE (arg0)))
3827 && unsigned0 == unsigned1
3828 && (unsigned0 || !uns))
3829 result_type
3830 = signed_or_unsigned_type (unsigned0,
3831 common_type (TREE_TYPE (arg0),
3832 TREE_TYPE (arg1)));
3833 else if (TREE_CODE (arg0) == INTEGER_CST
3834 && (unsigned1 || !uns)
3835 && (TYPE_PRECISION (TREE_TYPE (arg1))
3836 < TYPE_PRECISION (result_type))
3837 && (type = signed_or_unsigned_type (unsigned1,
3838 TREE_TYPE (arg1)),
3839 int_fits_type_p (arg0, type)))
3840 result_type = type;
3841 else if (TREE_CODE (arg1) == INTEGER_CST
3842 && (unsigned0 || !uns)
3843 && (TYPE_PRECISION (TREE_TYPE (arg0))
3844 < TYPE_PRECISION (result_type))
3845 && (type = signed_or_unsigned_type (unsigned0,
3846 TREE_TYPE (arg0)),
3847 int_fits_type_p (arg1, type)))
3848 result_type = type;
3851 /* Shifts can be shortened if shifting right. */
3853 if (short_shift)
3855 int unsigned_arg;
3856 tree arg0 = get_narrower (op0, &unsigned_arg);
3858 final_type = result_type;
3860 if (arg0 == op0 && final_type == TREE_TYPE (op0))
3861 unsigned_arg = TREE_UNSIGNED (TREE_TYPE (op0));
3863 if (TYPE_PRECISION (TREE_TYPE (arg0)) < TYPE_PRECISION (result_type)
3864 /* We can shorten only if the shift count is less than the
3865 number of bits in the smaller type size. */
3866 && compare_tree_int (op1, TYPE_PRECISION (TREE_TYPE (arg0))) < 0
3867 /* If arg is sign-extended and then unsigned-shifted,
3868 we can simulate this with a signed shift in arg's type
3869 only if the extended result is at least twice as wide
3870 as the arg. Otherwise, the shift could use up all the
3871 ones made by sign-extension and bring in zeros.
3872 We can't optimize that case at all, but in most machines
3873 it never happens because available widths are 2**N. */
3874 && (!TREE_UNSIGNED (final_type)
3875 || unsigned_arg
3876 || (((unsigned) 2 * TYPE_PRECISION (TREE_TYPE (arg0)))
3877 <= TYPE_PRECISION (result_type))))
3879 /* Do an unsigned shift if the operand was zero-extended. */
3880 result_type
3881 = signed_or_unsigned_type (unsigned_arg,
3882 TREE_TYPE (arg0));
3883 /* Convert value-to-be-shifted to that type. */
3884 if (TREE_TYPE (op0) != result_type)
3885 op0 = cp_convert (result_type, op0);
3886 converted = 1;
3890 /* Comparison operations are shortened too but differently.
3891 They identify themselves by setting short_compare = 1. */
3893 if (short_compare)
3895 /* Don't write &op0, etc., because that would prevent op0
3896 from being kept in a register.
3897 Instead, make copies of the our local variables and
3898 pass the copies by reference, then copy them back afterward. */
3899 tree xop0 = op0, xop1 = op1, xresult_type = result_type;
3900 enum tree_code xresultcode = resultcode;
3901 tree val
3902 = shorten_compare (&xop0, &xop1, &xresult_type, &xresultcode);
3903 if (val != 0)
3904 return cp_convert (boolean_type_node, val);
3905 op0 = xop0, op1 = xop1;
3906 converted = 1;
3907 resultcode = xresultcode;
3910 if ((short_compare || code == MIN_EXPR || code == MAX_EXPR)
3911 && warn_sign_compare)
3913 int op0_signed = ! TREE_UNSIGNED (TREE_TYPE (orig_op0));
3914 int op1_signed = ! TREE_UNSIGNED (TREE_TYPE (orig_op1));
3916 int unsignedp0, unsignedp1;
3917 tree primop0 = get_narrower (op0, &unsignedp0);
3918 tree primop1 = get_narrower (op1, &unsignedp1);
3920 /* Check for comparison of different enum types. */
3921 if (TREE_CODE (TREE_TYPE (orig_op0)) == ENUMERAL_TYPE
3922 && TREE_CODE (TREE_TYPE (orig_op1)) == ENUMERAL_TYPE
3923 && TYPE_MAIN_VARIANT (TREE_TYPE (orig_op0))
3924 != TYPE_MAIN_VARIANT (TREE_TYPE (orig_op1)))
3926 warning ("comparison between types `%#T' and `%#T'",
3927 TREE_TYPE (orig_op0), TREE_TYPE (orig_op1));
3930 /* Give warnings for comparisons between signed and unsigned
3931 quantities that may fail. */
3932 /* Do the checking based on the original operand trees, so that
3933 casts will be considered, but default promotions won't be. */
3935 /* Do not warn if the comparison is being done in a signed type,
3936 since the signed type will only be chosen if it can represent
3937 all the values of the unsigned type. */
3938 if (! TREE_UNSIGNED (result_type))
3939 /* OK */;
3940 /* Do not warn if both operands are unsigned. */
3941 else if (op0_signed == op1_signed)
3942 /* OK */;
3943 /* Do not warn if the signed quantity is an unsuffixed
3944 integer literal (or some static constant expression
3945 involving such literals or a conditional expression
3946 involving such literals) and it is non-negative. */
3947 else if ((op0_signed && tree_expr_nonnegative_p (orig_op0))
3948 || (op1_signed && tree_expr_nonnegative_p (orig_op1)))
3949 /* OK */;
3950 /* Do not warn if the comparison is an equality operation,
3951 the unsigned quantity is an integral constant and it does
3952 not use the most significant bit of result_type. */
3953 else if ((resultcode == EQ_EXPR || resultcode == NE_EXPR)
3954 && ((op0_signed && TREE_CODE (orig_op1) == INTEGER_CST
3955 && int_fits_type_p (orig_op1,
3956 signed_type (result_type)))
3957 || (op1_signed && TREE_CODE (orig_op0) == INTEGER_CST
3958 && int_fits_type_p (orig_op0,
3959 signed_type (result_type)))))
3960 /* OK */;
3961 else
3962 warning ("comparison between signed and unsigned integer expressions");
3964 /* Warn if two unsigned values are being compared in a size
3965 larger than their original size, and one (and only one) is the
3966 result of a `~' operator. This comparison will always fail.
3968 Also warn if one operand is a constant, and the constant does not
3969 have all bits set that are set in the ~ operand when it is
3970 extended. */
3972 if ((TREE_CODE (primop0) == BIT_NOT_EXPR)
3973 ^ (TREE_CODE (primop1) == BIT_NOT_EXPR))
3975 if (TREE_CODE (primop0) == BIT_NOT_EXPR)
3976 primop0 = get_narrower (TREE_OPERAND (op0, 0), &unsignedp0);
3977 if (TREE_CODE (primop1) == BIT_NOT_EXPR)
3978 primop1 = get_narrower (TREE_OPERAND (op1, 0), &unsignedp1);
3980 if (host_integerp (primop0, 0) || host_integerp (primop1, 0))
3982 tree primop;
3983 HOST_WIDE_INT constant, mask;
3984 int unsignedp;
3985 unsigned int bits;
3987 if (host_integerp (primop0, 0))
3989 primop = primop1;
3990 unsignedp = unsignedp1;
3991 constant = tree_low_cst (primop0, 0);
3993 else
3995 primop = primop0;
3996 unsignedp = unsignedp0;
3997 constant = tree_low_cst (primop1, 0);
4000 bits = TYPE_PRECISION (TREE_TYPE (primop));
4001 if (bits < TYPE_PRECISION (result_type)
4002 && bits < HOST_BITS_PER_LONG && unsignedp)
4004 mask = (~ (HOST_WIDE_INT) 0) << bits;
4005 if ((mask & constant) != mask)
4006 warning ("comparison of promoted ~unsigned with constant");
4009 else if (unsignedp0 && unsignedp1
4010 && (TYPE_PRECISION (TREE_TYPE (primop0))
4011 < TYPE_PRECISION (result_type))
4012 && (TYPE_PRECISION (TREE_TYPE (primop1))
4013 < TYPE_PRECISION (result_type)))
4014 warning ("comparison of promoted ~unsigned with unsigned");
4019 /* At this point, RESULT_TYPE must be nonzero to avoid an error message.
4020 If CONVERTED is zero, both args will be converted to type RESULT_TYPE.
4021 Then the expression will be built.
4022 It will be given type FINAL_TYPE if that is nonzero;
4023 otherwise, it will be given type RESULT_TYPE. */
4025 if (!result_type)
4027 error ("invalid operands of types `%T' and `%T' to binary `%O'",
4028 TREE_TYPE (orig_op0), TREE_TYPE (orig_op1), code);
4029 return error_mark_node;
4032 /* Issue warnings about peculiar, but legal, uses of NULL. */
4033 if (/* It's reasonable to use pointer values as operands of &&
4034 and ||, so NULL is no exception. */
4035 !(code == TRUTH_ANDIF_EXPR || code == TRUTH_ORIF_EXPR)
4036 && (/* If OP0 is NULL and OP1 is not a pointer, or vice versa. */
4037 (orig_op0 == null_node
4038 && TREE_CODE (TREE_TYPE (op1)) != POINTER_TYPE)
4039 /* Or vice versa. */
4040 || (orig_op1 == null_node
4041 && TREE_CODE (TREE_TYPE (op0)) != POINTER_TYPE)
4042 /* Or, both are NULL and the operation was not a comparison. */
4043 || (orig_op0 == null_node && orig_op1 == null_node
4044 && code != EQ_EXPR && code != NE_EXPR)))
4045 /* Some sort of arithmetic operation involving NULL was
4046 performed. Note that pointer-difference and pointer-addition
4047 have already been handled above, and so we don't end up here in
4048 that case. */
4049 warning ("NULL used in arithmetic");
4051 if (! converted)
4053 if (TREE_TYPE (op0) != result_type)
4054 op0 = cp_convert (result_type, op0);
4055 if (TREE_TYPE (op1) != result_type)
4056 op1 = cp_convert (result_type, op1);
4058 if (op0 == error_mark_node || op1 == error_mark_node)
4059 return error_mark_node;
4062 if (build_type == NULL_TREE)
4063 build_type = result_type;
4066 register tree result = build (resultcode, build_type, op0, op1);
4067 register tree folded;
4069 folded = fold (result);
4070 if (folded == result)
4071 TREE_CONSTANT (folded) = TREE_CONSTANT (op0) & TREE_CONSTANT (op1);
4072 if (final_type != 0)
4073 return cp_convert (final_type, folded);
4074 return folded;
4078 /* Return a tree for the sum or difference (RESULTCODE says which)
4079 of pointer PTROP and integer INTOP. */
4081 static tree
4082 pointer_int_sum (resultcode, ptrop, intop)
4083 enum tree_code resultcode;
4084 register tree ptrop, intop;
4086 tree size_exp;
4088 register tree result;
4089 register tree folded = fold (intop);
4091 /* The result is a pointer of the same type that is being added. */
4093 register tree result_type = TREE_TYPE (ptrop);
4095 if (!complete_type_or_else (result_type, ptrop))
4096 return error_mark_node;
4098 if (TREE_CODE (TREE_TYPE (result_type)) == VOID_TYPE)
4100 if (pedantic || warn_pointer_arith)
4101 pedwarn ("ISO C++ forbids using pointer of type `void *' in pointer arithmetic");
4102 size_exp = integer_one_node;
4104 else if (TREE_CODE (TREE_TYPE (result_type)) == FUNCTION_TYPE)
4106 if (pedantic || warn_pointer_arith)
4107 pedwarn ("ISO C++ forbids using a pointer-to-function in pointer arithmetic");
4108 size_exp = integer_one_node;
4110 else if (TREE_CODE (TREE_TYPE (result_type)) == METHOD_TYPE)
4112 if (pedantic || warn_pointer_arith)
4113 pedwarn ("ISO C++ forbids using a pointer to member function in pointer arithmetic");
4114 size_exp = integer_one_node;
4116 else if (TREE_CODE (TREE_TYPE (result_type)) == OFFSET_TYPE)
4118 if (pedantic || warn_pointer_arith)
4119 pedwarn ("ISO C++ forbids using pointer to a member in pointer arithmetic");
4120 size_exp = integer_one_node;
4122 else
4123 size_exp = size_in_bytes (complete_type (TREE_TYPE (result_type)));
4125 /* Needed to make OOPS V2R3 work. */
4126 intop = folded;
4127 if (integer_zerop (intop))
4128 return ptrop;
4130 /* If what we are about to multiply by the size of the elements
4131 contains a constant term, apply distributive law
4132 and multiply that constant term separately.
4133 This helps produce common subexpressions. */
4135 if ((TREE_CODE (intop) == PLUS_EXPR || TREE_CODE (intop) == MINUS_EXPR)
4136 && ! TREE_CONSTANT (intop)
4137 && TREE_CONSTANT (TREE_OPERAND (intop, 1))
4138 && TREE_CONSTANT (size_exp))
4140 enum tree_code subcode = resultcode;
4141 if (TREE_CODE (intop) == MINUS_EXPR)
4142 subcode = (subcode == PLUS_EXPR ? MINUS_EXPR : PLUS_EXPR);
4143 ptrop = cp_build_binary_op (subcode, ptrop, TREE_OPERAND (intop, 1));
4144 intop = TREE_OPERAND (intop, 0);
4147 /* Convert the integer argument to a type the same size as sizetype
4148 so the multiply won't overflow spuriously. */
4150 if (TYPE_PRECISION (TREE_TYPE (intop)) != TYPE_PRECISION (sizetype))
4151 intop = cp_convert (type_for_size (TYPE_PRECISION (sizetype), 0), intop);
4153 /* Replace the integer argument with a suitable product by the object size.
4154 Do this multiplication as signed, then convert to the appropriate
4155 pointer type (actually unsigned integral). */
4157 intop = cp_convert (result_type,
4158 cp_build_binary_op (MULT_EXPR, intop,
4159 cp_convert (TREE_TYPE (intop),
4160 size_exp)));
4162 /* Create the sum or difference. */
4164 result = build (resultcode, result_type, ptrop, intop);
4166 folded = fold (result);
4167 if (folded == result)
4168 TREE_CONSTANT (folded) = TREE_CONSTANT (ptrop) & TREE_CONSTANT (intop);
4169 return folded;
4172 /* Return a tree for the difference of pointers OP0 and OP1.
4173 The resulting tree has type int. */
4175 static tree
4176 pointer_diff (op0, op1, ptrtype)
4177 register tree op0, op1;
4178 register tree ptrtype;
4180 register tree result, folded;
4181 tree restype = ptrdiff_type_node;
4182 tree target_type = TREE_TYPE (ptrtype);
4184 if (!complete_type_or_else (target_type, NULL_TREE))
4185 return error_mark_node;
4187 if (pedantic || warn_pointer_arith)
4189 if (TREE_CODE (target_type) == VOID_TYPE)
4190 pedwarn ("ISO C++ forbids using pointer of type `void *' in subtraction");
4191 if (TREE_CODE (target_type) == FUNCTION_TYPE)
4192 pedwarn ("ISO C++ forbids using pointer to a function in subtraction");
4193 if (TREE_CODE (target_type) == METHOD_TYPE)
4194 pedwarn ("ISO C++ forbids using pointer to a method in subtraction");
4195 if (TREE_CODE (target_type) == OFFSET_TYPE)
4196 pedwarn ("ISO C++ forbids using pointer to a member in subtraction");
4199 /* First do the subtraction as integers;
4200 then drop through to build the divide operator. */
4202 op0 = cp_build_binary_op (MINUS_EXPR,
4203 cp_convert (restype, op0),
4204 cp_convert (restype, op1));
4206 /* This generates an error if op1 is a pointer to an incomplete type. */
4207 if (!COMPLETE_TYPE_P (TREE_TYPE (TREE_TYPE (op1))))
4208 error ("invalid use of a pointer to an incomplete type in pointer arithmetic");
4210 op1 = ((TREE_CODE (target_type) == VOID_TYPE
4211 || TREE_CODE (target_type) == FUNCTION_TYPE
4212 || TREE_CODE (target_type) == METHOD_TYPE
4213 || TREE_CODE (target_type) == OFFSET_TYPE)
4214 ? integer_one_node
4215 : size_in_bytes (target_type));
4217 /* Do the division. */
4219 result = build (EXACT_DIV_EXPR, restype, op0, cp_convert (restype, op1));
4221 folded = fold (result);
4222 if (folded == result)
4223 TREE_CONSTANT (folded) = TREE_CONSTANT (op0) & TREE_CONSTANT (op1);
4224 return folded;
4227 /* Handle the case of taking the address of a COMPONENT_REF.
4228 Called by `build_unary_op'.
4230 ARG is the COMPONENT_REF whose address we want.
4231 ARGTYPE is the pointer type that this address should have. */
4233 static tree
4234 build_component_addr (arg, argtype)
4235 tree arg, argtype;
4237 tree field = TREE_OPERAND (arg, 1);
4238 tree basetype = decl_type_context (field);
4239 tree rval = build_unary_op (ADDR_EXPR, TREE_OPERAND (arg, 0), 0);
4241 my_friendly_assert (TREE_CODE (field) == FIELD_DECL, 981018);
4243 if (DECL_C_BIT_FIELD (field))
4245 error ("attempt to take address of bit-field structure member `%D'",
4246 field);
4247 return error_mark_node;
4250 if (TREE_CODE (field) == FIELD_DECL
4251 && TYPE_BASE_CONVS_MAY_REQUIRE_CODE_P (basetype))
4253 /* Can't convert directly to ARGTYPE, since that
4254 may have the same pointer type as one of our
4255 baseclasses. */
4256 tree binfo = lookup_base (TREE_TYPE (TREE_TYPE (rval)), basetype,
4257 ba_check, NULL);
4259 rval = build_base_path (PLUS_EXPR, rval, binfo, 1);
4260 rval = build1 (NOP_EXPR, argtype, rval);
4261 TREE_CONSTANT (rval) = TREE_CONSTANT (TREE_OPERAND (rval, 0));
4263 else
4264 /* This conversion is harmless. */
4265 rval = convert_force (argtype, rval, 0);
4267 return fold (build (PLUS_EXPR, argtype, rval,
4268 cp_convert (argtype, byte_position (field))));
4271 /* Construct and perhaps optimize a tree representation
4272 for a unary operation. CODE, a tree_code, specifies the operation
4273 and XARG is the operand. */
4275 tree
4276 build_x_unary_op (code, xarg)
4277 enum tree_code code;
4278 tree xarg;
4280 tree exp;
4281 int ptrmem = 0;
4283 if (processing_template_decl)
4284 return build_min_nt (code, xarg, NULL_TREE);
4286 /* & rec, on incomplete RECORD_TYPEs is the simple opr &, not an
4287 error message. */
4288 if (code == ADDR_EXPR
4289 && TREE_CODE (xarg) != TEMPLATE_ID_EXPR
4290 && ((IS_AGGR_TYPE_CODE (TREE_CODE (TREE_TYPE (xarg)))
4291 && !COMPLETE_TYPE_P (TREE_TYPE (xarg)))
4292 || (TREE_CODE (xarg) == OFFSET_REF)))
4293 /* don't look for a function */;
4294 else
4296 tree rval;
4298 rval = build_new_op (code, LOOKUP_NORMAL, xarg,
4299 NULL_TREE, NULL_TREE);
4300 if (rval || code != ADDR_EXPR)
4301 return rval;
4303 if (code == ADDR_EXPR)
4305 if (TREE_CODE (xarg) == OFFSET_REF)
4307 ptrmem = PTRMEM_OK_P (xarg);
4309 if (!ptrmem && !flag_ms_extensions
4310 && TREE_CODE (TREE_TYPE (TREE_OPERAND (xarg, 1))) == METHOD_TYPE)
4312 /* A single non-static member, make sure we don't allow a
4313 pointer-to-member. */
4314 xarg = build (OFFSET_REF, TREE_TYPE (xarg),
4315 TREE_OPERAND (xarg, 0),
4316 ovl_cons (TREE_OPERAND (xarg, 1), NULL_TREE));
4317 PTRMEM_OK_P (xarg) = ptrmem;
4321 else if (TREE_CODE (xarg) == TARGET_EXPR)
4322 warning ("taking address of temporary");
4324 exp = build_unary_op (code, xarg, 0);
4325 if (TREE_CODE (exp) == ADDR_EXPR)
4326 PTRMEM_OK_P (exp) = ptrmem;
4328 return exp;
4331 /* Like truthvalue_conversion, but handle pointer-to-member constants, where
4332 a null value is represented by an INTEGER_CST of -1. */
4334 tree
4335 cp_truthvalue_conversion (expr)
4336 tree expr;
4338 tree type = TREE_TYPE (expr);
4339 if (TYPE_PTRMEM_P (type))
4340 return build_binary_op (NE_EXPR, expr, integer_zero_node, 1);
4341 else
4342 return truthvalue_conversion (expr);
4345 /* Just like cp_truthvalue_conversion, but we want a CLEANUP_POINT_EXPR. */
4347 tree
4348 condition_conversion (expr)
4349 tree expr;
4351 tree t;
4352 if (processing_template_decl)
4353 return expr;
4354 if (TREE_CODE (expr) == OFFSET_REF)
4355 expr = resolve_offset_ref (expr);
4356 t = perform_implicit_conversion (boolean_type_node, expr);
4357 t = fold (build1 (CLEANUP_POINT_EXPR, boolean_type_node, t));
4358 return t;
4361 /* C++: Must handle pointers to members.
4363 Perhaps type instantiation should be extended to handle conversion
4364 from aggregates to types we don't yet know we want? (Or are those
4365 cases typically errors which should be reported?)
4367 NOCONVERT nonzero suppresses the default promotions
4368 (such as from short to int). */
4370 tree
4371 build_unary_op (code, xarg, noconvert)
4372 enum tree_code code;
4373 tree xarg;
4374 int noconvert;
4376 /* No default_conversion here. It causes trouble for ADDR_EXPR. */
4377 register tree arg = xarg;
4378 register tree argtype = 0;
4379 const char *errstring = NULL;
4380 tree val;
4382 if (arg == error_mark_node)
4383 return error_mark_node;
4385 switch (code)
4387 case CONVERT_EXPR:
4388 /* This is used for unary plus, because a CONVERT_EXPR
4389 is enough to prevent anybody from looking inside for
4390 associativity, but won't generate any code. */
4391 if (!(arg = build_expr_type_conversion
4392 (WANT_ARITH | WANT_ENUM | WANT_POINTER, arg, 1)))
4393 errstring = "wrong type argument to unary plus";
4394 else
4396 if (!noconvert)
4397 arg = default_conversion (arg);
4398 arg = build1 (NON_LVALUE_EXPR, TREE_TYPE (arg), arg);
4399 TREE_CONSTANT (arg) = TREE_CONSTANT (TREE_OPERAND (arg, 0));
4401 break;
4403 case NEGATE_EXPR:
4404 if (!(arg = build_expr_type_conversion (WANT_ARITH | WANT_ENUM, arg, 1)))
4405 errstring = "wrong type argument to unary minus";
4406 else if (!noconvert)
4407 arg = default_conversion (arg);
4408 break;
4410 case BIT_NOT_EXPR:
4411 if (TREE_CODE (TREE_TYPE (arg)) == COMPLEX_TYPE)
4413 code = CONJ_EXPR;
4414 if (!noconvert)
4415 arg = default_conversion (arg);
4417 else if (!(arg = build_expr_type_conversion (WANT_INT | WANT_ENUM,
4418 arg, 1)))
4419 errstring = "wrong type argument to bit-complement";
4420 else if (!noconvert)
4421 arg = default_conversion (arg);
4422 break;
4424 case ABS_EXPR:
4425 if (!(arg = build_expr_type_conversion (WANT_ARITH | WANT_ENUM, arg, 1)))
4426 errstring = "wrong type argument to abs";
4427 else if (!noconvert)
4428 arg = default_conversion (arg);
4429 break;
4431 case CONJ_EXPR:
4432 /* Conjugating a real value is a no-op, but allow it anyway. */
4433 if (!(arg = build_expr_type_conversion (WANT_ARITH | WANT_ENUM, arg, 1)))
4434 errstring = "wrong type argument to conjugation";
4435 else if (!noconvert)
4436 arg = default_conversion (arg);
4437 break;
4439 case TRUTH_NOT_EXPR:
4440 arg = cp_convert (boolean_type_node, arg);
4441 val = invert_truthvalue (arg);
4442 if (arg != error_mark_node)
4443 return val;
4444 errstring = "in argument to unary !";
4445 break;
4447 case NOP_EXPR:
4448 break;
4450 case REALPART_EXPR:
4451 if (TREE_CODE (arg) == COMPLEX_CST)
4452 return TREE_REALPART (arg);
4453 else if (TREE_CODE (TREE_TYPE (arg)) == COMPLEX_TYPE)
4454 return fold (build1 (REALPART_EXPR, TREE_TYPE (TREE_TYPE (arg)), arg));
4455 else
4456 return arg;
4458 case IMAGPART_EXPR:
4459 if (TREE_CODE (arg) == COMPLEX_CST)
4460 return TREE_IMAGPART (arg);
4461 else if (TREE_CODE (TREE_TYPE (arg)) == COMPLEX_TYPE)
4462 return fold (build1 (IMAGPART_EXPR, TREE_TYPE (TREE_TYPE (arg)), arg));
4463 else
4464 return cp_convert (TREE_TYPE (arg), integer_zero_node);
4466 case PREINCREMENT_EXPR:
4467 case POSTINCREMENT_EXPR:
4468 case PREDECREMENT_EXPR:
4469 case POSTDECREMENT_EXPR:
4470 /* Handle complex lvalues (when permitted)
4471 by reduction to simpler cases. */
4473 val = unary_complex_lvalue (code, arg);
4474 if (val != 0)
4475 return val;
4477 /* Increment or decrement the real part of the value,
4478 and don't change the imaginary part. */
4479 if (TREE_CODE (TREE_TYPE (arg)) == COMPLEX_TYPE)
4481 tree real, imag;
4483 arg = stabilize_reference (arg);
4484 real = build_unary_op (REALPART_EXPR, arg, 1);
4485 imag = build_unary_op (IMAGPART_EXPR, arg, 1);
4486 return build (COMPLEX_EXPR, TREE_TYPE (arg),
4487 build_unary_op (code, real, 1), imag);
4490 /* Report invalid types. */
4492 if (!(arg = build_expr_type_conversion (WANT_ARITH | WANT_POINTER,
4493 arg, 1)))
4495 if (code == PREINCREMENT_EXPR)
4496 errstring ="no pre-increment operator for type";
4497 else if (code == POSTINCREMENT_EXPR)
4498 errstring ="no post-increment operator for type";
4499 else if (code == PREDECREMENT_EXPR)
4500 errstring ="no pre-decrement operator for type";
4501 else
4502 errstring ="no post-decrement operator for type";
4503 break;
4506 /* Report something read-only. */
4508 if (CP_TYPE_CONST_P (TREE_TYPE (arg))
4509 || TREE_READONLY (arg))
4510 readonly_error (arg, ((code == PREINCREMENT_EXPR
4511 || code == POSTINCREMENT_EXPR)
4512 ? "increment" : "decrement"),
4516 register tree inc;
4517 tree result_type = TREE_TYPE (arg);
4519 arg = get_unwidened (arg, 0);
4520 argtype = TREE_TYPE (arg);
4522 /* ARM $5.2.5 last annotation says this should be forbidden. */
4523 if (TREE_CODE (argtype) == ENUMERAL_TYPE)
4524 pedwarn ("ISO C++ forbids %sing an enum",
4525 (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
4526 ? "increment" : "decrement");
4528 /* Compute the increment. */
4530 if (TREE_CODE (argtype) == POINTER_TYPE)
4532 enum tree_code tmp = TREE_CODE (TREE_TYPE (argtype));
4533 tree type = complete_type (TREE_TYPE (argtype));
4535 if (!COMPLETE_OR_VOID_TYPE_P (type))
4536 error ("cannot %s a pointer to incomplete type `%T'",
4537 ((code == PREINCREMENT_EXPR
4538 || code == POSTINCREMENT_EXPR)
4539 ? "increment" : "decrement"), TREE_TYPE (argtype));
4540 else if ((pedantic || warn_pointer_arith)
4541 && (tmp == FUNCTION_TYPE || tmp == METHOD_TYPE
4542 || tmp == VOID_TYPE || tmp == OFFSET_TYPE))
4543 pedwarn ("ISO C++ forbids %sing a pointer of type `%T'",
4544 ((code == PREINCREMENT_EXPR
4545 || code == POSTINCREMENT_EXPR)
4546 ? "increment" : "decrement"), argtype);
4547 inc = c_sizeof_nowarn (TREE_TYPE (argtype));
4549 else
4550 inc = integer_one_node;
4552 inc = cp_convert (argtype, inc);
4554 /* Handle incrementing a cast-expression. */
4556 switch (TREE_CODE (arg))
4558 case NOP_EXPR:
4559 case CONVERT_EXPR:
4560 case FLOAT_EXPR:
4561 case FIX_TRUNC_EXPR:
4562 case FIX_FLOOR_EXPR:
4563 case FIX_ROUND_EXPR:
4564 case FIX_CEIL_EXPR:
4566 tree incremented, modify, value, compound;
4567 if (! lvalue_p (arg) && pedantic)
4568 pedwarn ("cast to non-reference type used as lvalue");
4569 arg = stabilize_reference (arg);
4570 if (code == PREINCREMENT_EXPR || code == PREDECREMENT_EXPR)
4571 value = arg;
4572 else
4573 value = save_expr (arg);
4574 incremented = build (((code == PREINCREMENT_EXPR
4575 || code == POSTINCREMENT_EXPR)
4576 ? PLUS_EXPR : MINUS_EXPR),
4577 argtype, value, inc);
4579 modify = build_modify_expr (arg, NOP_EXPR, incremented);
4580 compound = build (COMPOUND_EXPR, TREE_TYPE (arg), modify, value);
4582 /* Eliminate warning about unused result of + or -. */
4583 TREE_NO_UNUSED_WARNING (compound) = 1;
4584 return compound;
4587 default:
4588 break;
4591 /* Complain about anything else that is not a true lvalue. */
4592 if (!lvalue_or_else (arg, ((code == PREINCREMENT_EXPR
4593 || code == POSTINCREMENT_EXPR)
4594 ? "increment" : "decrement")))
4595 return error_mark_node;
4597 /* Forbid using -- on `bool'. */
4598 if (TREE_TYPE (arg) == boolean_type_node)
4600 if (code == POSTDECREMENT_EXPR || code == PREDECREMENT_EXPR)
4602 error ("invalid use of `--' on bool variable `%D'", arg);
4603 return error_mark_node;
4605 #if 0
4606 /* This will only work if someone can convince Kenner to accept
4607 my patch to expand_increment. (jason) */
4608 val = build (code, TREE_TYPE (arg), arg, inc);
4609 #else
4610 val = boolean_increment (code, arg);
4611 #endif
4613 else
4614 val = build (code, TREE_TYPE (arg), arg, inc);
4616 TREE_SIDE_EFFECTS (val) = 1;
4617 return cp_convert (result_type, val);
4620 case ADDR_EXPR:
4621 /* Note that this operation never does default_conversion
4622 regardless of NOCONVERT. */
4624 argtype = lvalue_type (arg);
4625 if (TREE_CODE (argtype) == REFERENCE_TYPE)
4627 arg = build1
4628 (CONVERT_EXPR,
4629 build_pointer_type (TREE_TYPE (argtype)), arg);
4630 TREE_CONSTANT (arg) = TREE_CONSTANT (TREE_OPERAND (arg, 0));
4631 return arg;
4633 else if (pedantic && DECL_MAIN_P (arg))
4634 /* ARM $3.4 */
4635 pedwarn ("ISO C++ forbids taking address of function `::main'");
4637 /* Let &* cancel out to simplify resulting code. */
4638 if (TREE_CODE (arg) == INDIRECT_REF)
4640 /* We don't need to have `current_class_ptr' wrapped in a
4641 NON_LVALUE_EXPR node. */
4642 if (arg == current_class_ref)
4643 return current_class_ptr;
4645 arg = TREE_OPERAND (arg, 0);
4646 if (TREE_CODE (TREE_TYPE (arg)) == REFERENCE_TYPE)
4648 arg = build1
4649 (CONVERT_EXPR,
4650 build_pointer_type (TREE_TYPE (TREE_TYPE (arg))), arg);
4651 TREE_CONSTANT (arg) = TREE_CONSTANT (TREE_OPERAND (arg, 0));
4653 else if (lvalue_p (arg))
4654 /* Don't let this be an lvalue. */
4655 return non_lvalue (arg);
4656 return arg;
4659 /* For &x[y], return x+y */
4660 if (TREE_CODE (arg) == ARRAY_REF)
4662 if (mark_addressable (TREE_OPERAND (arg, 0)) == 0)
4663 return error_mark_node;
4664 return cp_build_binary_op (PLUS_EXPR, TREE_OPERAND (arg, 0),
4665 TREE_OPERAND (arg, 1));
4668 /* Uninstantiated types are all functions. Taking the
4669 address of a function is a no-op, so just return the
4670 argument. */
4672 if (TREE_CODE (arg) == IDENTIFIER_NODE
4673 && IDENTIFIER_OPNAME_P (arg))
4675 abort ();
4676 /* We don't know the type yet, so just work around the problem.
4677 We know that this will resolve to an lvalue. */
4678 return build1 (ADDR_EXPR, unknown_type_node, arg);
4681 if (TREE_CODE (arg) == COMPONENT_REF && type_unknown_p (arg)
4682 && OVL_NEXT (TREE_OPERAND (arg, 1)) == NULL_TREE)
4684 /* They're trying to take the address of a unique non-static
4685 member function. This is ill-formed (except in MS-land),
4686 but let's try to DTRT.
4687 Note: We only handle unique functions here because we don't
4688 want to complain if there's a static overload; non-unique
4689 cases will be handled by instantiate_type. But we need to
4690 handle this case here to allow casts on the resulting PMF.
4691 We could defer this in non-MS mode, but it's easier to give
4692 a useful error here. */
4694 tree base = TREE_TYPE (TREE_OPERAND (arg, 0));
4695 tree name = DECL_NAME (OVL_CURRENT (TREE_OPERAND (arg, 1)));
4697 if (! flag_ms_extensions)
4699 if (current_class_type
4700 && TREE_OPERAND (arg, 0) == current_class_ref)
4701 /* An expression like &memfn. */
4702 pedwarn ("ISO C++ forbids taking the address of an unqualified non-static member function to form a pointer to member function. Say `&%T::%D'", base, name);
4703 else
4704 pedwarn ("ISO C++ forbids taking the address of a bound member function to form a pointer to member function. Say `&%T::%D'", base, name);
4706 arg = build_offset_ref (base, name);
4709 if (type_unknown_p (arg))
4710 return build1 (ADDR_EXPR, unknown_type_node, arg);
4712 /* Handle complex lvalues (when permitted)
4713 by reduction to simpler cases. */
4714 val = unary_complex_lvalue (code, arg);
4715 if (val != 0)
4716 return val;
4718 switch (TREE_CODE (arg))
4720 case NOP_EXPR:
4721 case CONVERT_EXPR:
4722 case FLOAT_EXPR:
4723 case FIX_TRUNC_EXPR:
4724 case FIX_FLOOR_EXPR:
4725 case FIX_ROUND_EXPR:
4726 case FIX_CEIL_EXPR:
4727 if (! lvalue_p (arg) && pedantic)
4728 pedwarn ("ISO C++ forbids taking the address of a cast to a non-lvalue expression");
4729 break;
4731 default:
4732 break;
4735 /* Allow the address of a constructor if all the elements
4736 are constant. */
4737 if (TREE_CODE (arg) == CONSTRUCTOR && TREE_HAS_CONSTRUCTOR (arg)
4738 && TREE_CONSTANT (arg))
4740 /* Anything not already handled and not a true memory reference
4741 is an error. */
4742 else if (TREE_CODE (argtype) != FUNCTION_TYPE
4743 && TREE_CODE (argtype) != METHOD_TYPE
4744 && !lvalue_or_else (arg, "unary `&'"))
4745 return error_mark_node;
4747 if (argtype != error_mark_node)
4748 argtype = build_pointer_type (argtype);
4750 if (mark_addressable (arg) == 0)
4751 return error_mark_node;
4754 tree addr;
4756 if (TREE_CODE (arg) == COMPONENT_REF)
4757 addr = build_component_addr (arg, argtype);
4758 else
4759 addr = build1 (ADDR_EXPR, argtype, arg);
4761 /* Address of a static or external variable or
4762 function counts as a constant */
4763 if (staticp (arg))
4764 TREE_CONSTANT (addr) = 1;
4766 if (TREE_CODE (argtype) == POINTER_TYPE
4767 && TREE_CODE (TREE_TYPE (argtype)) == METHOD_TYPE)
4769 build_ptrmemfunc_type (argtype);
4770 addr = build_ptrmemfunc (argtype, addr, 0);
4773 return addr;
4776 default:
4777 break;
4780 if (!errstring)
4782 if (argtype == 0)
4783 argtype = TREE_TYPE (arg);
4784 return fold (build1 (code, argtype, arg));
4787 error ("%s", errstring);
4788 return error_mark_node;
4791 /* Apply unary lvalue-demanding operator CODE to the expression ARG
4792 for certain kinds of expressions which are not really lvalues
4793 but which we can accept as lvalues.
4795 If ARG is not a kind of expression we can handle, return zero. */
4797 tree
4798 unary_complex_lvalue (code, arg)
4799 enum tree_code code;
4800 tree arg;
4802 /* Handle (a, b) used as an "lvalue". */
4803 if (TREE_CODE (arg) == COMPOUND_EXPR)
4805 tree real_result = build_unary_op (code, TREE_OPERAND (arg, 1), 0);
4806 return build (COMPOUND_EXPR, TREE_TYPE (real_result),
4807 TREE_OPERAND (arg, 0), real_result);
4810 /* Handle (a ? b : c) used as an "lvalue". */
4811 if (TREE_CODE (arg) == COND_EXPR
4812 || TREE_CODE (arg) == MIN_EXPR || TREE_CODE (arg) == MAX_EXPR)
4813 return rationalize_conditional_expr (code, arg);
4815 /* Handle (a = b), (++a), and (--a) used as an "lvalue". */
4816 if (TREE_CODE (arg) == MODIFY_EXPR
4817 || TREE_CODE (arg) == PREINCREMENT_EXPR
4818 || TREE_CODE (arg) == PREDECREMENT_EXPR)
4820 tree lvalue = TREE_OPERAND (arg, 0);
4821 if (TREE_SIDE_EFFECTS (lvalue))
4823 lvalue = stabilize_reference (lvalue);
4824 arg = build (TREE_CODE (arg), TREE_TYPE (arg),
4825 lvalue, TREE_OPERAND (arg, 1));
4827 return unary_complex_lvalue
4828 (code, build (COMPOUND_EXPR, TREE_TYPE (lvalue), arg, lvalue));
4831 if (code != ADDR_EXPR)
4832 return 0;
4834 /* Handle (a = b) used as an "lvalue" for `&'. */
4835 if (TREE_CODE (arg) == MODIFY_EXPR
4836 || TREE_CODE (arg) == INIT_EXPR)
4838 tree real_result = build_unary_op (code, TREE_OPERAND (arg, 0), 0);
4839 arg = build (COMPOUND_EXPR, TREE_TYPE (real_result), arg, real_result);
4840 TREE_NO_UNUSED_WARNING (arg) = 1;
4841 return arg;
4844 if (TREE_CODE (TREE_TYPE (arg)) == FUNCTION_TYPE
4845 || TREE_CODE (TREE_TYPE (arg)) == METHOD_TYPE
4846 || TREE_CODE (TREE_TYPE (arg)) == OFFSET_TYPE)
4848 /* The representation of something of type OFFSET_TYPE
4849 is really the representation of a pointer to it.
4850 Here give the representation its true type. */
4851 tree t;
4853 my_friendly_assert (TREE_CODE (arg) != SCOPE_REF, 313);
4855 if (TREE_CODE (arg) != OFFSET_REF)
4856 return 0;
4858 t = TREE_OPERAND (arg, 1);
4860 /* Check all this code for right semantics. */
4861 if (TREE_CODE (t) == FUNCTION_DECL)
4863 if (DECL_DESTRUCTOR_P (t))
4864 error ("taking address of destructor");
4865 return build_unary_op (ADDR_EXPR, t, 0);
4867 if (TREE_CODE (t) == VAR_DECL)
4868 return build_unary_op (ADDR_EXPR, t, 0);
4869 else
4871 tree type;
4873 if (TREE_OPERAND (arg, 0)
4874 && ! is_dummy_object (TREE_OPERAND (arg, 0))
4875 && TREE_CODE (t) != FIELD_DECL)
4877 error ("taking address of bound pointer-to-member expression");
4878 return error_mark_node;
4880 if (!PTRMEM_OK_P (arg))
4882 /* This cannot form a pointer to method, so we must
4883 resolve the offset ref, and take the address of the
4884 result. For instance,
4885 &(C::m) */
4886 arg = resolve_offset_ref (arg);
4888 return build_unary_op (code, arg, 0);
4891 if (TREE_CODE (TREE_TYPE (t)) == REFERENCE_TYPE)
4893 error ("cannot create pointer to reference member `%D'", t);
4894 return error_mark_node;
4897 type = build_offset_type (DECL_FIELD_CONTEXT (t), TREE_TYPE (t));
4898 type = build_pointer_type (type);
4900 t = make_ptrmem_cst (type, TREE_OPERAND (arg, 1));
4901 return t;
4906 /* We permit compiler to make function calls returning
4907 objects of aggregate type look like lvalues. */
4909 tree targ = arg;
4911 if (TREE_CODE (targ) == SAVE_EXPR)
4912 targ = TREE_OPERAND (targ, 0);
4914 if (TREE_CODE (targ) == CALL_EXPR && IS_AGGR_TYPE (TREE_TYPE (targ)))
4916 if (TREE_CODE (arg) == SAVE_EXPR)
4917 targ = arg;
4918 else
4919 targ = build_cplus_new (TREE_TYPE (arg), arg);
4920 return build1 (ADDR_EXPR, build_pointer_type (TREE_TYPE (arg)), targ);
4923 if (TREE_CODE (arg) == SAVE_EXPR && TREE_CODE (targ) == INDIRECT_REF)
4924 return build (SAVE_EXPR, build_pointer_type (TREE_TYPE (arg)),
4925 TREE_OPERAND (targ, 0), current_function_decl, NULL);
4928 /* Don't let anything else be handled specially. */
4929 return 0;
4932 /* Mark EXP saying that we need to be able to take the
4933 address of it; it should not be allocated in a register.
4934 Value is 1 if successful.
4936 C++: we do not allow `current_class_ptr' to be addressable. */
4939 mark_addressable (exp)
4940 tree exp;
4942 register tree x = exp;
4944 if (TREE_ADDRESSABLE (x) == 1)
4945 return 1;
4947 while (1)
4948 switch (TREE_CODE (x))
4950 case ADDR_EXPR:
4951 case COMPONENT_REF:
4952 case ARRAY_REF:
4953 case REALPART_EXPR:
4954 case IMAGPART_EXPR:
4955 x = TREE_OPERAND (x, 0);
4956 break;
4958 case PARM_DECL:
4959 if (x == current_class_ptr)
4961 error ("cannot take the address of `this', which is an rvalue expression");
4962 TREE_ADDRESSABLE (x) = 1; /* so compiler doesn't die later */
4963 return 1;
4965 case VAR_DECL:
4966 /* Caller should not be trying to mark initialized
4967 constant fields addressable. */
4968 my_friendly_assert (DECL_LANG_SPECIFIC (x) == 0
4969 || DECL_IN_AGGR_P (x) == 0
4970 || TREE_STATIC (x)
4971 || DECL_EXTERNAL (x), 314);
4973 case CONST_DECL:
4974 case RESULT_DECL:
4975 if (DECL_REGISTER (x) && !TREE_ADDRESSABLE (x)
4976 && !DECL_ARTIFICIAL (x) && extra_warnings)
4977 warning ("address requested for `%D', which is declared `register'",
4979 TREE_ADDRESSABLE (x) = 1;
4980 return 1;
4982 case FUNCTION_DECL:
4983 TREE_ADDRESSABLE (x) = 1;
4984 TREE_ADDRESSABLE (DECL_ASSEMBLER_NAME (x)) = 1;
4985 return 1;
4987 case CONSTRUCTOR:
4988 TREE_ADDRESSABLE (x) = 1;
4989 return 1;
4991 case TARGET_EXPR:
4992 TREE_ADDRESSABLE (x) = 1;
4993 mark_addressable (TREE_OPERAND (x, 0));
4994 return 1;
4996 default:
4997 return 1;
5001 /* Build and return a conditional expression IFEXP ? OP1 : OP2. */
5003 tree
5004 build_x_conditional_expr (ifexp, op1, op2)
5005 tree ifexp, op1, op2;
5007 if (processing_template_decl)
5008 return build_min_nt (COND_EXPR, ifexp, op1, op2);
5010 return build_conditional_expr (ifexp, op1, op2);
5013 /* Handle overloading of the ',' operator when needed. Otherwise,
5014 this function just builds an expression list. */
5016 tree
5017 build_x_compound_expr (list)
5018 tree list;
5020 tree rest = TREE_CHAIN (list);
5021 tree result;
5023 if (processing_template_decl)
5024 return build_min_nt (COMPOUND_EXPR, list, NULL_TREE);
5026 if (rest == NULL_TREE)
5027 return build_compound_expr (list);
5029 result = build_opfncall (COMPOUND_EXPR, LOOKUP_NORMAL,
5030 TREE_VALUE (list), TREE_VALUE (rest), NULL_TREE);
5031 if (result)
5032 return build_x_compound_expr (tree_cons (NULL_TREE, result,
5033 TREE_CHAIN (rest)));
5035 if (! TREE_SIDE_EFFECTS (TREE_VALUE (list)))
5037 /* FIXME: This test should be in the implicit cast to void of the LHS. */
5038 /* the left-hand operand of a comma expression is like an expression
5039 statement: we should warn if it doesn't have any side-effects,
5040 unless it was explicitly cast to (void). */
5041 if ((extra_warnings || warn_unused_value)
5042 && !(TREE_CODE (TREE_VALUE(list)) == CONVERT_EXPR
5043 && VOID_TYPE_P (TREE_TYPE (TREE_VALUE(list)))))
5044 warning("left-hand operand of comma expression has no effect");
5046 #if 0 /* this requires a gcc backend patch to export warn_if_unused_value */
5047 else if (warn_unused_value)
5048 warn_if_unused_value (TREE_VALUE(list));
5049 #endif
5051 return build_compound_expr
5052 (tree_cons (NULL_TREE, TREE_VALUE (list),
5053 build_tree_list (NULL_TREE,
5054 build_x_compound_expr (rest))));
5057 /* Given a list of expressions, return a compound expression
5058 that performs them all and returns the value of the last of them. */
5060 tree
5061 build_compound_expr (list)
5062 tree list;
5064 register tree rest;
5065 tree first;
5067 TREE_VALUE (list) = decl_constant_value (TREE_VALUE (list));
5069 if (TREE_CHAIN (list) == 0)
5071 /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
5072 Strip such NOP_EXPRs, since LIST is used in non-lvalue context. */
5073 if (TREE_CODE (list) == NOP_EXPR
5074 && TREE_TYPE (list) == TREE_TYPE (TREE_OPERAND (list, 0)))
5075 list = TREE_OPERAND (list, 0);
5077 return TREE_VALUE (list);
5080 first = TREE_VALUE (list);
5081 first = convert_to_void (first, "left-hand operand of comma");
5082 if (first == error_mark_node)
5083 return error_mark_node;
5085 rest = build_compound_expr (TREE_CHAIN (list));
5086 if (rest == error_mark_node)
5087 return error_mark_node;
5089 /* When pedantic, a compound expression cannot be a constant expression. */
5090 if (! TREE_SIDE_EFFECTS (first) && ! pedantic)
5091 return rest;
5093 return build (COMPOUND_EXPR, TREE_TYPE (rest), first, rest);
5096 tree
5097 build_static_cast (type, expr)
5098 tree type, expr;
5100 tree intype;
5101 int ok;
5103 if (type == error_mark_node || expr == error_mark_node)
5104 return error_mark_node;
5106 if (TREE_CODE (expr) == OFFSET_REF)
5107 expr = resolve_offset_ref (expr);
5109 if (processing_template_decl)
5111 tree t = build_min (STATIC_CAST_EXPR, type, expr);
5112 return t;
5115 /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
5116 Strip such NOP_EXPRs if VALUE is being used in non-lvalue context. */
5117 if (TREE_CODE (type) != REFERENCE_TYPE
5118 && TREE_CODE (expr) == NOP_EXPR
5119 && TREE_TYPE (expr) == TREE_TYPE (TREE_OPERAND (expr, 0)))
5120 expr = TREE_OPERAND (expr, 0);
5122 if (TREE_CODE (type) == VOID_TYPE)
5124 expr = convert_to_void (expr, /*implicit=*/NULL);
5125 return expr;
5128 if (TREE_CODE (type) == REFERENCE_TYPE)
5129 return (convert_from_reference
5130 (convert_to_reference (type, expr, CONV_STATIC|CONV_IMPLICIT,
5131 LOOKUP_COMPLAIN, NULL_TREE)));
5133 if (IS_AGGR_TYPE (type))
5134 return build_cplus_new (type, (build_method_call
5135 (NULL_TREE, complete_ctor_identifier,
5136 build_tree_list (NULL_TREE, expr),
5137 TYPE_BINFO (type), LOOKUP_NORMAL)));
5139 intype = TREE_TYPE (expr);
5141 /* FIXME handle casting to array type. */
5143 ok = 0;
5144 if (IS_AGGR_TYPE (intype)
5145 ? can_convert_arg (type, intype, expr)
5146 : can_convert_arg (strip_all_pointer_quals (type),
5147 strip_all_pointer_quals (intype), expr))
5148 /* This is a standard conversion. */
5149 ok = 1;
5150 else if (TYPE_PTROB_P (type) && TYPE_PTROB_P (intype))
5152 /* They're pointers to objects. They must be aggregates that
5153 are related non-virtually. */
5154 base_kind kind;
5156 if (IS_AGGR_TYPE (TREE_TYPE (type)) && IS_AGGR_TYPE (TREE_TYPE (intype))
5157 && lookup_base (TREE_TYPE (type), TREE_TYPE (intype),
5158 ba_ignore | ba_quiet, &kind)
5159 && kind != bk_via_virtual)
5160 ok = 1;
5162 else if (TREE_CODE (intype) != BOOLEAN_TYPE
5163 && TREE_CODE (type) != ARRAY_TYPE
5164 && TREE_CODE (type) != FUNCTION_TYPE
5165 && can_convert (intype, strip_all_pointer_quals (type)))
5166 ok = 1;
5167 else if (TREE_CODE (intype) == ENUMERAL_TYPE
5168 && TREE_CODE (type) == ENUMERAL_TYPE)
5169 /* DR 128: "A value of integral _or enumeration_ type can be explicitly
5170 converted to an enumeration type."
5171 The integral to enumeration will be accepted by the previous clause.
5172 We need to explicitly check for enumeration to enumeration. */
5173 ok = 1;
5175 /* [expr.static.cast]
5177 The static_cast operator shall not be used to cast away
5178 constness. */
5179 if (ok && casts_away_constness (intype, type))
5181 error ("static_cast from type `%T' to type `%T' casts away constness",
5182 intype, type);
5183 return error_mark_node;
5186 if (ok)
5187 return build_c_cast (type, expr);
5189 error ("invalid static_cast from type `%T' to type `%T'", intype, type);
5190 return error_mark_node;
5193 tree
5194 build_reinterpret_cast (type, expr)
5195 tree type, expr;
5197 tree intype;
5199 if (type == error_mark_node || expr == error_mark_node)
5200 return error_mark_node;
5202 if (TREE_CODE (expr) == OFFSET_REF)
5203 expr = resolve_offset_ref (expr);
5205 if (processing_template_decl)
5207 tree t = build_min (REINTERPRET_CAST_EXPR, type, expr);
5208 return t;
5211 if (TREE_CODE (type) != REFERENCE_TYPE)
5213 expr = decay_conversion (expr);
5215 /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
5216 Strip such NOP_EXPRs if VALUE is being used in non-lvalue context. */
5217 if (TREE_CODE (expr) == NOP_EXPR
5218 && TREE_TYPE (expr) == TREE_TYPE (TREE_OPERAND (expr, 0)))
5219 expr = TREE_OPERAND (expr, 0);
5222 intype = TREE_TYPE (expr);
5224 if (TREE_CODE (type) == REFERENCE_TYPE)
5226 if (! real_lvalue_p (expr))
5228 error ("invalid reinterpret_cast of an rvalue expression of type `%T' to type `%T'", intype, type);
5229 return error_mark_node;
5231 expr = build_unary_op (ADDR_EXPR, expr, 0);
5232 if (expr != error_mark_node)
5233 expr = build_reinterpret_cast
5234 (build_pointer_type (TREE_TYPE (type)), expr);
5235 if (expr != error_mark_node)
5236 expr = build_indirect_ref (expr, 0);
5237 return expr;
5239 else if (same_type_ignoring_top_level_qualifiers_p (intype, type))
5240 return build_static_cast (type, expr);
5242 if (TYPE_PTR_P (type) && (TREE_CODE (intype) == INTEGER_TYPE
5243 || TREE_CODE (intype) == ENUMERAL_TYPE))
5244 /* OK */;
5245 else if (TREE_CODE (type) == INTEGER_TYPE && TYPE_PTR_P (intype))
5247 if (TYPE_PRECISION (type) < TYPE_PRECISION (intype))
5248 pedwarn ("reinterpret_cast from `%T' to `%T' loses precision",
5249 intype, type);
5251 else if ((TYPE_PTRFN_P (type) && TYPE_PTRFN_P (intype))
5252 || (TYPE_PTRMEMFUNC_P (type) && TYPE_PTRMEMFUNC_P (intype)))
5254 expr = decl_constant_value (expr);
5255 return fold (build1 (NOP_EXPR, type, expr));
5257 else if ((TYPE_PTRMEM_P (type) && TYPE_PTRMEM_P (intype))
5258 || (TYPE_PTROBV_P (type) && TYPE_PTROBV_P (intype)))
5260 if (! comp_ptr_ttypes_reinterpret (TREE_TYPE (type), TREE_TYPE (intype)))
5261 pedwarn ("reinterpret_cast from `%T' to `%T' casts away const (or volatile)",
5262 intype, type);
5264 expr = decl_constant_value (expr);
5265 return fold (build1 (NOP_EXPR, type, expr));
5267 else if ((TYPE_PTRFN_P (type) && TYPE_PTROBV_P (intype))
5268 || (TYPE_PTRFN_P (intype) && TYPE_PTROBV_P (type)))
5270 pedwarn ("ISO C++ forbids casting between pointer-to-function and pointer-to-object");
5271 expr = decl_constant_value (expr);
5272 return fold (build1 (NOP_EXPR, type, expr));
5274 else
5276 error ("invalid reinterpret_cast from type `%T' to type `%T'",
5277 intype, type);
5278 return error_mark_node;
5281 return cp_convert (type, expr);
5284 tree
5285 build_const_cast (type, expr)
5286 tree type, expr;
5288 tree intype;
5290 if (type == error_mark_node || expr == error_mark_node)
5291 return error_mark_node;
5293 if (TREE_CODE (expr) == OFFSET_REF)
5294 expr = resolve_offset_ref (expr);
5296 if (processing_template_decl)
5298 tree t = build_min (CONST_CAST_EXPR, type, expr);
5299 return t;
5302 if (!POINTER_TYPE_P (type))
5303 error ("invalid use of const_cast with type `%T', which is not a pointer, reference, nor a pointer-to-data-member type", type);
5304 else if (TREE_CODE (TREE_TYPE (type)) == FUNCTION_TYPE)
5306 error ("invalid use of const_cast with type `%T', which is a pointer or reference to a function type", type);
5307 return error_mark_node;
5310 if (TREE_CODE (type) != REFERENCE_TYPE)
5312 expr = decay_conversion (expr);
5314 /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
5315 Strip such NOP_EXPRs if VALUE is being used in non-lvalue context. */
5316 if (TREE_CODE (expr) == NOP_EXPR
5317 && TREE_TYPE (expr) == TREE_TYPE (TREE_OPERAND (expr, 0)))
5318 expr = TREE_OPERAND (expr, 0);
5321 intype = TREE_TYPE (expr);
5323 if (same_type_ignoring_top_level_qualifiers_p (intype, type))
5324 return build_static_cast (type, expr);
5325 else if (TREE_CODE (type) == REFERENCE_TYPE)
5327 if (! real_lvalue_p (expr))
5329 error ("invalid const_cast of an rvalue of type `%T' to type `%T'", intype, type);
5330 return error_mark_node;
5333 if (comp_ptr_ttypes_const (TREE_TYPE (type), intype))
5335 expr = build_unary_op (ADDR_EXPR, expr, 0);
5336 expr = build1 (NOP_EXPR, type, expr);
5337 return convert_from_reference (expr);
5340 else if (TREE_CODE (type) == POINTER_TYPE
5341 && TREE_CODE (intype) == POINTER_TYPE
5342 && comp_ptr_ttypes_const (TREE_TYPE (type), TREE_TYPE (intype)))
5343 return cp_convert (type, expr);
5345 error ("invalid const_cast from type `%T' to type `%T'", intype, type);
5346 return error_mark_node;
5349 /* Build an expression representing a cast to type TYPE of expression EXPR.
5351 ALLOW_NONCONVERTING is true if we should allow non-converting constructors
5352 when doing the cast. */
5354 tree
5355 build_c_cast (type, expr)
5356 tree type, expr;
5358 register tree value = expr;
5359 tree otype;
5361 if (type == error_mark_node || expr == error_mark_node)
5362 return error_mark_node;
5364 if (processing_template_decl)
5366 tree t = build_min (CAST_EXPR, type,
5367 tree_cons (NULL_TREE, value, NULL_TREE));
5368 return t;
5371 /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
5372 Strip such NOP_EXPRs if VALUE is being used in non-lvalue context. */
5373 if (TREE_CODE (type) != REFERENCE_TYPE
5374 && TREE_CODE (value) == NOP_EXPR
5375 && TREE_TYPE (value) == TREE_TYPE (TREE_OPERAND (value, 0)))
5376 value = TREE_OPERAND (value, 0);
5378 if (TREE_CODE (value) == OFFSET_REF)
5379 value = resolve_offset_ref (value);
5381 if (TREE_CODE (type) == ARRAY_TYPE)
5383 /* Allow casting from T1* to T2[] because Cfront allows it.
5384 NIHCL uses it. It is not valid ISO C++ however. */
5385 if (TREE_CODE (TREE_TYPE (expr)) == POINTER_TYPE)
5387 pedwarn ("ISO C++ forbids casting to an array type `%T'", type);
5388 type = build_pointer_type (TREE_TYPE (type));
5390 else
5392 error ("ISO C++ forbids casting to an array type `%T'", type);
5393 return error_mark_node;
5397 if (TREE_CODE (type) == FUNCTION_TYPE
5398 || TREE_CODE (type) == METHOD_TYPE)
5400 error ("invalid cast to function type `%T'", type);
5401 return error_mark_node;
5404 if (TREE_CODE (type) == VOID_TYPE)
5406 /* Conversion to void does not cause any of the normal function to
5407 * pointer, array to pointer and lvalue to rvalue decays. */
5409 value = convert_to_void (value, /*implicit=*/NULL);
5410 return value;
5412 /* Convert functions and arrays to pointers and
5413 convert references to their expanded types,
5414 but don't convert any other types. If, however, we are
5415 casting to a class type, there's no reason to do this: the
5416 cast will only succeed if there is a converting constructor,
5417 and the default conversions will be done at that point. In
5418 fact, doing the default conversion here is actually harmful
5419 in cases like this:
5421 typedef int A[2];
5422 struct S { S(const A&); };
5424 since we don't want the array-to-pointer conversion done. */
5425 if (!IS_AGGR_TYPE (type))
5427 if (TREE_CODE (TREE_TYPE (value)) == FUNCTION_TYPE
5428 || (TREE_CODE (TREE_TYPE (value)) == METHOD_TYPE
5429 /* Don't do the default conversion on a ->* expression. */
5430 && ! (TREE_CODE (type) == POINTER_TYPE
5431 && bound_pmf_p (value)))
5432 || TREE_CODE (TREE_TYPE (value)) == ARRAY_TYPE
5433 || TREE_CODE (TREE_TYPE (value)) == REFERENCE_TYPE)
5434 value = default_conversion (value);
5436 else if (TREE_CODE (TREE_TYPE (value)) == REFERENCE_TYPE)
5437 /* However, even for class types, we still need to strip away
5438 the reference type, since the call to convert_force below
5439 does not expect the input expression to be of reference
5440 type. */
5441 value = convert_from_reference (value);
5443 otype = TREE_TYPE (value);
5445 /* Optionally warn about potentially worrisome casts. */
5447 if (warn_cast_qual
5448 && TREE_CODE (type) == POINTER_TYPE
5449 && TREE_CODE (otype) == POINTER_TYPE
5450 && !at_least_as_qualified_p (TREE_TYPE (type),
5451 TREE_TYPE (otype)))
5452 warning ("cast from `%T' to `%T' discards qualifiers from pointer target type",
5453 otype, type);
5455 if (TREE_CODE (type) == INTEGER_TYPE
5456 && TREE_CODE (otype) == POINTER_TYPE
5457 && TYPE_PRECISION (type) != TYPE_PRECISION (otype))
5458 warning ("cast from pointer to integer of different size");
5460 if (TREE_CODE (type) == POINTER_TYPE
5461 && TREE_CODE (otype) == INTEGER_TYPE
5462 && TYPE_PRECISION (type) != TYPE_PRECISION (otype)
5463 /* Don't warn about converting any constant. */
5464 && !TREE_CONSTANT (value))
5465 warning ("cast to pointer from integer of different size");
5467 if (TREE_CODE (type) == REFERENCE_TYPE)
5468 value = (convert_from_reference
5469 (convert_to_reference (type, value, CONV_C_CAST,
5470 LOOKUP_COMPLAIN, NULL_TREE)));
5471 else
5473 tree ovalue;
5475 value = decl_constant_value (value);
5477 ovalue = value;
5478 value = convert_force (type, value, CONV_C_CAST);
5480 /* Ignore any integer overflow caused by the cast. */
5481 if (TREE_CODE (value) == INTEGER_CST)
5483 TREE_OVERFLOW (value) = TREE_OVERFLOW (ovalue);
5484 TREE_CONSTANT_OVERFLOW (value) = TREE_CONSTANT_OVERFLOW (ovalue);
5488 /* Warn about possible alignment problems. Do this here when we will have
5489 instantiated any necessary template types. */
5490 if (STRICT_ALIGNMENT && warn_cast_align
5491 && TREE_CODE (type) == POINTER_TYPE
5492 && TREE_CODE (otype) == POINTER_TYPE
5493 && TREE_CODE (TREE_TYPE (otype)) != VOID_TYPE
5494 && TREE_CODE (TREE_TYPE (otype)) != FUNCTION_TYPE
5495 && COMPLETE_TYPE_P (TREE_TYPE (otype))
5496 && COMPLETE_TYPE_P (TREE_TYPE (type))
5497 && TYPE_ALIGN (TREE_TYPE (type)) > TYPE_ALIGN (TREE_TYPE (otype)))
5498 warning ("cast from `%T' to `%T' increases required alignment of target type",
5499 otype, type);
5501 /* Always produce some operator for an explicit cast,
5502 so we can tell (for -pedantic) that the cast is no lvalue. */
5503 if (TREE_CODE (type) != REFERENCE_TYPE && value == expr
5504 && real_lvalue_p (value))
5505 value = non_lvalue (value);
5507 return value;
5510 /* Build an assignment expression of lvalue LHS from value RHS.
5511 MODIFYCODE is the code for a binary operator that we use
5512 to combine the old value of LHS with RHS to get the new value.
5513 Or else MODIFYCODE is NOP_EXPR meaning do a simple assignment.
5515 C++: If MODIFYCODE is INIT_EXPR, then leave references unbashed. */
5517 tree
5518 build_modify_expr (lhs, modifycode, rhs)
5519 tree lhs;
5520 enum tree_code modifycode;
5521 tree rhs;
5523 register tree result;
5524 tree newrhs = rhs;
5525 tree lhstype = TREE_TYPE (lhs);
5526 tree olhstype = lhstype;
5527 tree olhs = lhs;
5529 /* Avoid duplicate error messages from operands that had errors. */
5530 if (lhs == error_mark_node || rhs == error_mark_node)
5531 return error_mark_node;
5533 /* Handle control structure constructs used as "lvalues". */
5534 switch (TREE_CODE (lhs))
5536 /* Handle --foo = 5; as these are valid constructs in C++ */
5537 case PREDECREMENT_EXPR:
5538 case PREINCREMENT_EXPR:
5539 if (TREE_SIDE_EFFECTS (TREE_OPERAND (lhs, 0)))
5540 lhs = build (TREE_CODE (lhs), TREE_TYPE (lhs),
5541 stabilize_reference (TREE_OPERAND (lhs, 0)),
5542 TREE_OPERAND (lhs, 1));
5543 return build (COMPOUND_EXPR, lhstype,
5544 lhs,
5545 build_modify_expr (TREE_OPERAND (lhs, 0),
5546 modifycode, rhs));
5548 /* Handle (a, b) used as an "lvalue". */
5549 case COMPOUND_EXPR:
5550 newrhs = build_modify_expr (TREE_OPERAND (lhs, 1),
5551 modifycode, rhs);
5552 if (newrhs == error_mark_node)
5553 return error_mark_node;
5554 return build (COMPOUND_EXPR, lhstype,
5555 TREE_OPERAND (lhs, 0), newrhs);
5557 case MODIFY_EXPR:
5558 newrhs = build_modify_expr (TREE_OPERAND (lhs, 0), modifycode, rhs);
5559 if (newrhs == error_mark_node)
5560 return error_mark_node;
5561 return build (COMPOUND_EXPR, lhstype, lhs, newrhs);
5563 /* Handle (a ? b : c) used as an "lvalue". */
5564 case COND_EXPR:
5566 /* Produce (a ? (b = rhs) : (c = rhs))
5567 except that the RHS goes through a save-expr
5568 so the code to compute it is only emitted once. */
5569 tree cond;
5571 rhs = save_expr (rhs);
5573 /* Check this here to avoid odd errors when trying to convert
5574 a throw to the type of the COND_EXPR. */
5575 if (!lvalue_or_else (lhs, "assignment"))
5576 return error_mark_node;
5578 cond = build_conditional_expr
5579 (TREE_OPERAND (lhs, 0),
5580 build_modify_expr (cp_convert (TREE_TYPE (lhs),
5581 TREE_OPERAND (lhs, 1)),
5582 modifycode, rhs),
5583 build_modify_expr (cp_convert (TREE_TYPE (lhs),
5584 TREE_OPERAND (lhs, 2)),
5585 modifycode, rhs));
5587 if (cond == error_mark_node)
5588 return cond;
5589 /* Make sure the code to compute the rhs comes out
5590 before the split. */
5591 return build (COMPOUND_EXPR, TREE_TYPE (lhs),
5592 /* Cast to void to suppress warning
5593 from warn_if_unused_value. */
5594 cp_convert (void_type_node, rhs), cond);
5597 case OFFSET_REF:
5598 lhs = resolve_offset_ref (lhs);
5599 if (lhs == error_mark_node)
5600 return error_mark_node;
5601 olhstype = lhstype = TREE_TYPE (lhs);
5603 default:
5604 break;
5607 if (modifycode == INIT_EXPR)
5609 if (TREE_CODE (rhs) == CONSTRUCTOR)
5611 my_friendly_assert (same_type_p (TREE_TYPE (rhs), lhstype),
5612 20011220);
5613 result = build (INIT_EXPR, lhstype, lhs, rhs);
5614 TREE_SIDE_EFFECTS (result) = 1;
5615 return result;
5617 else if (! IS_AGGR_TYPE (lhstype))
5618 /* Do the default thing */;
5619 else
5621 result = build_method_call (lhs, complete_ctor_identifier,
5622 build_tree_list (NULL_TREE, rhs),
5623 TYPE_BINFO (lhstype), LOOKUP_NORMAL);
5624 if (result == NULL_TREE)
5625 return error_mark_node;
5626 return result;
5629 else
5631 if (TREE_CODE (lhstype) == REFERENCE_TYPE)
5633 lhs = convert_from_reference (lhs);
5634 olhstype = lhstype = TREE_TYPE (lhs);
5636 lhs = require_complete_type (lhs);
5637 if (lhs == error_mark_node)
5638 return error_mark_node;
5640 if (modifycode == NOP_EXPR)
5642 /* `operator=' is not an inheritable operator. */
5643 if (! IS_AGGR_TYPE (lhstype))
5644 /* Do the default thing */;
5645 else
5647 result = build_opfncall (MODIFY_EXPR, LOOKUP_NORMAL,
5648 lhs, rhs, make_node (NOP_EXPR));
5649 if (result == NULL_TREE)
5650 return error_mark_node;
5651 return result;
5653 lhstype = olhstype;
5655 else
5657 /* A binary op has been requested. Combine the old LHS
5658 value with the RHS producing the value we should actually
5659 store into the LHS. */
5661 my_friendly_assert (!PROMOTES_TO_AGGR_TYPE (lhstype, REFERENCE_TYPE),
5662 978652);
5663 lhs = stabilize_reference (lhs);
5664 newrhs = cp_build_binary_op (modifycode, lhs, rhs);
5665 if (newrhs == error_mark_node)
5667 error (" in evaluation of `%Q(%#T, %#T)'", modifycode,
5668 TREE_TYPE (lhs), TREE_TYPE (rhs));
5669 return error_mark_node;
5672 /* Now it looks like a plain assignment. */
5673 modifycode = NOP_EXPR;
5675 my_friendly_assert (TREE_CODE (lhstype) != REFERENCE_TYPE, 20011220);
5676 my_friendly_assert (TREE_CODE (TREE_TYPE (newrhs)) != REFERENCE_TYPE,
5677 20011220);
5680 /* Handle a cast used as an "lvalue".
5681 We have already performed any binary operator using the value as cast.
5682 Now convert the result to the cast type of the lhs,
5683 and then true type of the lhs and store it there;
5684 then convert result back to the cast type to be the value
5685 of the assignment. */
5687 switch (TREE_CODE (lhs))
5689 case NOP_EXPR:
5690 case CONVERT_EXPR:
5691 case FLOAT_EXPR:
5692 case FIX_TRUNC_EXPR:
5693 case FIX_FLOOR_EXPR:
5694 case FIX_ROUND_EXPR:
5695 case FIX_CEIL_EXPR:
5697 tree inner_lhs = TREE_OPERAND (lhs, 0);
5698 tree result;
5700 if (TREE_CODE (TREE_TYPE (newrhs)) == ARRAY_TYPE
5701 || TREE_CODE (TREE_TYPE (newrhs)) == FUNCTION_TYPE
5702 || TREE_CODE (TREE_TYPE (newrhs)) == METHOD_TYPE
5703 || TREE_CODE (TREE_TYPE (newrhs)) == OFFSET_TYPE)
5704 newrhs = default_conversion (newrhs);
5706 /* ISO C++ 5.4/1: The result is an lvalue if T is a reference
5707 type, otherwise the result is an rvalue. */
5708 if (! lvalue_p (lhs))
5709 pedwarn ("ISO C++ forbids cast to non-reference type used as lvalue");
5711 result = build_modify_expr (inner_lhs, NOP_EXPR,
5712 cp_convert (TREE_TYPE (inner_lhs),
5713 cp_convert (lhstype, newrhs)));
5714 if (result == error_mark_node)
5715 return result;
5716 return cp_convert (TREE_TYPE (lhs), result);
5719 default:
5720 break;
5723 /* Now we have handled acceptable kinds of LHS that are not truly lvalues.
5724 Reject anything strange now. */
5726 if (!lvalue_or_else (lhs, "assignment"))
5727 return error_mark_node;
5729 GNU_xref_assign (lhs);
5731 /* Warn about modifying something that is `const'. Don't warn if
5732 this is initialization. */
5733 if (modifycode != INIT_EXPR
5734 && (TREE_READONLY (lhs) || CP_TYPE_CONST_P (lhstype)
5735 /* Functions are not modifiable, even though they are
5736 lvalues. */
5737 || TREE_CODE (TREE_TYPE (lhs)) == FUNCTION_TYPE
5738 || TREE_CODE (TREE_TYPE (lhs)) == METHOD_TYPE
5739 /* If it's an aggregate and any field is const, then it is
5740 effectively const. */
5741 || (IS_AGGR_TYPE_CODE (TREE_CODE (lhstype))
5742 && C_TYPE_FIELDS_READONLY (lhstype))))
5743 readonly_error (lhs, "assignment", 0);
5745 /* If storing into a structure or union member, it has probably been
5746 given type `int'. Compute the type that would go with the actual
5747 amount of storage the member occupies. */
5749 if (TREE_CODE (lhs) == COMPONENT_REF
5750 && (TREE_CODE (lhstype) == INTEGER_TYPE
5751 || TREE_CODE (lhstype) == REAL_TYPE
5752 || TREE_CODE (lhstype) == ENUMERAL_TYPE))
5754 lhstype = TREE_TYPE (get_unwidened (lhs, 0));
5756 /* If storing in a field that is in actuality a short or narrower
5757 than one, we must store in the field in its actual type. */
5759 if (lhstype != TREE_TYPE (lhs))
5761 lhs = copy_node (lhs);
5762 TREE_TYPE (lhs) = lhstype;
5766 if (TREE_CODE (lhstype) != REFERENCE_TYPE)
5768 if (TREE_SIDE_EFFECTS (lhs))
5769 lhs = stabilize_reference (lhs);
5770 if (TREE_SIDE_EFFECTS (newrhs))
5771 newrhs = stabilize_reference (newrhs);
5774 /* Convert new value to destination type. */
5776 if (TREE_CODE (lhstype) == ARRAY_TYPE)
5778 int from_array;
5780 if (!same_or_base_type_p (lhstype, TREE_TYPE (rhs)))
5782 error ("incompatible types in assignment of `%T' to `%T'",
5783 TREE_TYPE (rhs), lhstype);
5784 return error_mark_node;
5787 /* Allow array assignment in compiler-generated code. */
5788 if (pedantic && ! DECL_ARTIFICIAL (current_function_decl))
5789 pedwarn ("ISO C++ forbids assignment of arrays");
5791 from_array = TREE_CODE (TREE_TYPE (newrhs)) == ARRAY_TYPE
5792 ? 1 + (modifycode != INIT_EXPR): 0;
5793 return build_vec_init (lhs, newrhs, from_array);
5796 if (modifycode == INIT_EXPR)
5797 newrhs = convert_for_initialization (lhs, lhstype, newrhs, LOOKUP_NORMAL,
5798 "initialization", NULL_TREE, 0);
5799 else
5801 /* Avoid warnings on enum bit fields. */
5802 if (TREE_CODE (olhstype) == ENUMERAL_TYPE
5803 && TREE_CODE (lhstype) == INTEGER_TYPE)
5805 newrhs = convert_for_assignment (olhstype, newrhs, "assignment",
5806 NULL_TREE, 0);
5807 newrhs = convert_force (lhstype, newrhs, 0);
5809 else
5810 newrhs = convert_for_assignment (lhstype, newrhs, "assignment",
5811 NULL_TREE, 0);
5812 if (TREE_CODE (newrhs) == CALL_EXPR
5813 && TYPE_NEEDS_CONSTRUCTING (lhstype))
5814 newrhs = build_cplus_new (lhstype, newrhs);
5816 /* Can't initialize directly from a TARGET_EXPR, since that would
5817 cause the lhs to be constructed twice, and possibly result in
5818 accidental self-initialization. So we force the TARGET_EXPR to be
5819 expanded without a target. */
5820 if (TREE_CODE (newrhs) == TARGET_EXPR)
5821 newrhs = build (COMPOUND_EXPR, TREE_TYPE (newrhs), newrhs,
5822 TREE_OPERAND (newrhs, 0));
5825 if (newrhs == error_mark_node)
5826 return error_mark_node;
5828 if (TREE_CODE (newrhs) == COND_EXPR)
5830 tree lhs1;
5831 tree cond = TREE_OPERAND (newrhs, 0);
5833 if (TREE_SIDE_EFFECTS (lhs))
5834 cond = build_compound_expr (tree_cons
5835 (NULL_TREE, lhs,
5836 build_tree_list (NULL_TREE, cond)));
5838 /* Cannot have two identical lhs on this one tree (result) as preexpand
5839 calls will rip them out and fill in RTL for them, but when the
5840 rtl is generated, the calls will only be in the first side of the
5841 condition, not on both, or before the conditional jump! (mrs) */
5842 lhs1 = break_out_calls (lhs);
5844 if (lhs == lhs1)
5845 /* If there's no change, the COND_EXPR behaves like any other rhs. */
5846 result = build (modifycode == NOP_EXPR ? MODIFY_EXPR : INIT_EXPR,
5847 lhstype, lhs, newrhs);
5848 else
5850 tree result_type = TREE_TYPE (newrhs);
5851 /* We have to convert each arm to the proper type because the
5852 types may have been munged by constant folding. */
5853 result
5854 = build (COND_EXPR, result_type, cond,
5855 build_modify_expr (lhs, modifycode,
5856 cp_convert (result_type,
5857 TREE_OPERAND (newrhs, 1))),
5858 build_modify_expr (lhs1, modifycode,
5859 cp_convert (result_type,
5860 TREE_OPERAND (newrhs, 2))));
5863 else
5864 result = build (modifycode == NOP_EXPR ? MODIFY_EXPR : INIT_EXPR,
5865 lhstype, lhs, newrhs);
5867 TREE_SIDE_EFFECTS (result) = 1;
5869 /* If we got the LHS in a different type for storing in,
5870 convert the result back to the nominal type of LHS
5871 so that the value we return always has the same type
5872 as the LHS argument. */
5874 if (olhstype == TREE_TYPE (result))
5875 return result;
5876 /* Avoid warnings converting integral types back into enums
5877 for enum bit fields. */
5878 if (TREE_CODE (TREE_TYPE (result)) == INTEGER_TYPE
5879 && TREE_CODE (olhstype) == ENUMERAL_TYPE)
5881 result = build (COMPOUND_EXPR, olhstype, result, olhs);
5882 TREE_NO_UNUSED_WARNING (result) = 1;
5883 return result;
5885 return convert_for_assignment (olhstype, result, "assignment",
5886 NULL_TREE, 0);
5889 tree
5890 build_x_modify_expr (lhs, modifycode, rhs)
5891 tree lhs;
5892 enum tree_code modifycode;
5893 tree rhs;
5895 if (processing_template_decl)
5896 return build_min_nt (MODOP_EXPR, lhs,
5897 build_min_nt (modifycode, NULL_TREE, NULL_TREE), rhs);
5899 if (modifycode != NOP_EXPR)
5901 tree rval = build_opfncall (MODIFY_EXPR, LOOKUP_NORMAL, lhs, rhs,
5902 make_node (modifycode));
5903 if (rval)
5904 return rval;
5906 return build_modify_expr (lhs, modifycode, rhs);
5910 /* Get difference in deltas for different pointer to member function
5911 types. Return integer_zero_node, if FROM cannot be converted to a
5912 TO type. If FORCE is true, then allow reverse conversions as well.
5914 Note that the naming of FROM and TO is kind of backwards; the return
5915 value is what we add to a TO in order to get a FROM. They are named
5916 this way because we call this function to find out how to convert from
5917 a pointer to member of FROM to a pointer to member of TO. */
5919 static tree
5920 get_delta_difference (from, to, force)
5921 tree from, to;
5922 int force;
5924 tree delta = integer_zero_node;
5925 tree binfo;
5926 tree virt_binfo;
5927 base_kind kind;
5929 binfo = lookup_base (to, from, ba_check, &kind);
5930 if (kind == bk_inaccessible || kind == bk_ambig)
5932 error (" in pointer to member function conversion");
5933 return delta;
5935 if (!binfo)
5937 if (!force)
5939 error_not_base_type (from, to);
5940 error (" in pointer to member conversion");
5941 return delta;
5943 binfo = lookup_base (from, to, ba_check, &kind);
5944 if (binfo == 0)
5945 return delta;
5946 virt_binfo = binfo_from_vbase (binfo);
5948 if (virt_binfo)
5950 /* This is a reinterpret cast, we choose to do nothing. */
5951 warning ("pointer to member cast via virtual base `%T' of `%T'",
5952 BINFO_TYPE (virt_binfo),
5953 BINFO_TYPE (BINFO_INHERITANCE_CHAIN (virt_binfo)));
5954 return delta;
5956 delta = BINFO_OFFSET (binfo);
5957 delta = cp_convert (ptrdiff_type_node, delta);
5958 delta = cp_build_binary_op (MINUS_EXPR,
5959 integer_zero_node,
5960 delta);
5962 return delta;
5965 virt_binfo = binfo_from_vbase (binfo);
5966 if (virt_binfo)
5968 /* This is a reinterpret cast, we choose to do nothing. */
5969 if (force)
5970 warning ("pointer to member cast via virtual base `%T' of `%T'",
5971 BINFO_TYPE (virt_binfo),
5972 BINFO_TYPE (BINFO_INHERITANCE_CHAIN (virt_binfo)));
5973 else
5974 error ("pointer to member conversion via virtual base `%T' of `%T'",
5975 BINFO_TYPE (virt_binfo),
5976 BINFO_TYPE (BINFO_INHERITANCE_CHAIN (virt_binfo)));
5977 return delta;
5979 delta = BINFO_OFFSET (binfo);
5981 return cp_convert (ptrdiff_type_node, delta);
5984 /* Return a constructor for the pointer-to-member-function TYPE using
5985 the other components as specified. */
5987 tree
5988 build_ptrmemfunc1 (type, delta, pfn)
5989 tree type, delta, pfn;
5991 tree u = NULL_TREE;
5992 tree delta_field;
5993 tree pfn_field;
5995 /* Pull the FIELD_DECLs out of the type. */
5996 pfn_field = TYPE_FIELDS (type);
5997 delta_field = TREE_CHAIN (pfn_field);
5999 /* Make sure DELTA has the type we want. */
6000 delta = convert_and_check (delta_type_node, delta);
6002 /* Finish creating the initializer. */
6003 u = tree_cons (pfn_field, pfn,
6004 build_tree_list (delta_field, delta));
6005 u = build (CONSTRUCTOR, type, NULL_TREE, u);
6006 TREE_CONSTANT (u) = TREE_CONSTANT (pfn) && TREE_CONSTANT (delta);
6007 TREE_STATIC (u) = (TREE_CONSTANT (u)
6008 && (initializer_constant_valid_p (pfn, TREE_TYPE (pfn))
6009 != NULL_TREE)
6010 && (initializer_constant_valid_p (delta, TREE_TYPE (delta))
6011 != NULL_TREE));
6012 return u;
6015 /* Build a constructor for a pointer to member function. It can be
6016 used to initialize global variables, local variable, or used
6017 as a value in expressions. TYPE is the POINTER to METHOD_TYPE we
6018 want to be.
6020 If FORCE is non-zero, then force this conversion, even if
6021 we would rather not do it. Usually set when using an explicit
6022 cast.
6024 Return error_mark_node, if something goes wrong. */
6026 tree
6027 build_ptrmemfunc (type, pfn, force)
6028 tree type, pfn;
6029 int force;
6031 tree fn;
6032 tree pfn_type = TREE_TYPE (pfn);
6033 tree to_type = build_ptrmemfunc_type (type);
6035 /* Handle multiple conversions of pointer to member functions. */
6036 if (TYPE_PTRMEMFUNC_P (TREE_TYPE (pfn)))
6038 tree delta = NULL_TREE;
6039 tree npfn = NULL_TREE;
6040 tree n;
6042 if (!force
6043 && !can_convert_arg (to_type, TREE_TYPE (pfn), pfn))
6044 error ("invalid conversion to type `%T' from type `%T'",
6045 to_type, pfn_type);
6047 n = get_delta_difference (TYPE_PTRMEMFUNC_OBJECT_TYPE (pfn_type),
6048 TYPE_PTRMEMFUNC_OBJECT_TYPE (to_type),
6049 force);
6051 /* We don't have to do any conversion to convert a
6052 pointer-to-member to its own type. But, we don't want to
6053 just return a PTRMEM_CST if there's an explicit cast; that
6054 cast should make the expression an invalid template argument. */
6055 if (TREE_CODE (pfn) != PTRMEM_CST)
6057 if (same_type_p (to_type, pfn_type))
6058 return pfn;
6059 else if (integer_zerop (n))
6060 return build_reinterpret_cast (to_type, pfn);
6063 if (TREE_SIDE_EFFECTS (pfn))
6064 pfn = save_expr (pfn);
6066 /* Obtain the function pointer and the current DELTA. */
6067 if (TREE_CODE (pfn) == PTRMEM_CST)
6068 expand_ptrmemfunc_cst (pfn, &delta, &npfn);
6069 else
6071 npfn = build_component_ref (pfn, pfn_identifier, NULL_TREE, 0);
6072 delta = build_component_ref (pfn, delta_identifier, NULL_TREE, 0);
6075 /* Just adjust the DELTA field. */
6076 delta = cp_convert (ptrdiff_type_node, delta);
6077 if (TARGET_PTRMEMFUNC_VBIT_LOCATION == ptrmemfunc_vbit_in_delta)
6078 n = cp_build_binary_op (LSHIFT_EXPR, n, integer_one_node);
6079 delta = cp_build_binary_op (PLUS_EXPR, delta, n);
6080 return build_ptrmemfunc1 (to_type, delta, npfn);
6083 /* Handle null pointer to member function conversions. */
6084 if (integer_zerop (pfn))
6086 pfn = build_c_cast (type, integer_zero_node);
6087 return build_ptrmemfunc1 (to_type,
6088 integer_zero_node,
6089 pfn);
6092 if (type_unknown_p (pfn))
6093 return instantiate_type (type, pfn, itf_complain);
6095 fn = TREE_OPERAND (pfn, 0);
6096 my_friendly_assert (TREE_CODE (fn) == FUNCTION_DECL, 0);
6097 return make_ptrmem_cst (to_type, fn);
6100 /* Return the DELTA, IDX, PFN, and DELTA2 values for the PTRMEM_CST
6101 given by CST.
6103 ??? There is no consistency as to the types returned for the above
6104 values. Some code acts as if its a sizetype and some as if its
6105 integer_type_node. */
6107 void
6108 expand_ptrmemfunc_cst (cst, delta, pfn)
6109 tree cst;
6110 tree *delta;
6111 tree *pfn;
6113 tree type = TREE_TYPE (cst);
6114 tree fn = PTRMEM_CST_MEMBER (cst);
6115 tree ptr_class, fn_class;
6117 my_friendly_assert (TREE_CODE (fn) == FUNCTION_DECL, 0);
6119 /* The class that the function belongs to. */
6120 fn_class = DECL_CONTEXT (fn);
6122 /* The class that we're creating a pointer to member of. */
6123 ptr_class = TYPE_PTRMEMFUNC_OBJECT_TYPE (type);
6125 /* First, calculate the adjustment to the function's class. */
6126 *delta = get_delta_difference (fn_class, ptr_class, /*force=*/0);
6128 if (!DECL_VIRTUAL_P (fn))
6129 *pfn = convert (TYPE_PTRMEMFUNC_FN_TYPE (type), build_addr_func (fn));
6130 else
6132 /* If we're dealing with a virtual function, we have to adjust 'this'
6133 again, to point to the base which provides the vtable entry for
6134 fn; the call will do the opposite adjustment. */
6135 tree orig_class = DECL_VIRTUAL_CONTEXT (fn);
6136 tree binfo = binfo_or_else (orig_class, fn_class);
6137 *delta = fold (build (PLUS_EXPR, TREE_TYPE (*delta),
6138 *delta, BINFO_OFFSET (binfo)));
6140 /* We set PFN to the vtable offset at which the function can be
6141 found, plus one (unless ptrmemfunc_vbit_in_delta, in which
6142 case delta is shifted left, and then incremented). */
6143 *pfn = DECL_VINDEX (fn);
6144 *pfn = fold (build (MULT_EXPR, integer_type_node, *pfn,
6145 TYPE_SIZE_UNIT (vtable_entry_type)));
6147 switch (TARGET_PTRMEMFUNC_VBIT_LOCATION)
6149 case ptrmemfunc_vbit_in_pfn:
6150 *pfn = fold (build (PLUS_EXPR, integer_type_node, *pfn,
6151 integer_one_node));
6152 break;
6154 case ptrmemfunc_vbit_in_delta:
6155 *delta = fold (build (LSHIFT_EXPR, TREE_TYPE (*delta),
6156 *delta, integer_one_node));
6157 *delta = fold (build (PLUS_EXPR, TREE_TYPE (*delta),
6158 *delta, integer_one_node));
6159 break;
6161 default:
6162 abort ();
6165 *pfn = fold (build1 (NOP_EXPR, TYPE_PTRMEMFUNC_FN_TYPE (type),
6166 *pfn));
6170 /* Return an expression for PFN from the pointer-to-member function
6171 given by T. */
6173 tree
6174 pfn_from_ptrmemfunc (t)
6175 tree t;
6177 if (TREE_CODE (t) == PTRMEM_CST)
6179 tree delta;
6180 tree pfn;
6182 expand_ptrmemfunc_cst (t, &delta, &pfn);
6183 if (pfn)
6184 return pfn;
6187 return build_component_ref (t, pfn_identifier, NULL_TREE, 0);
6190 /* Expression EXPR is about to be implicitly converted to TYPE. Warn
6191 if this is a potentially dangerous thing to do. Returns a possibly
6192 marked EXPR. */
6194 tree
6195 dubious_conversion_warnings (type, expr, errtype, fndecl, parmnum)
6196 tree type;
6197 tree expr;
6198 const char *errtype;
6199 tree fndecl;
6200 int parmnum;
6202 if (TREE_CODE (type) == REFERENCE_TYPE)
6203 type = TREE_TYPE (type);
6205 /* Issue warnings about peculiar, but legal, uses of NULL. */
6206 if (ARITHMETIC_TYPE_P (type) && expr == null_node)
6208 if (fndecl)
6209 warning ("passing NULL used for non-pointer %s %P of `%D'",
6210 errtype, parmnum, fndecl);
6211 else
6212 warning ("%s to non-pointer type `%T' from NULL", errtype, type);
6215 /* Warn about assigning a floating-point type to an integer type. */
6216 if (TREE_CODE (TREE_TYPE (expr)) == REAL_TYPE
6217 && TREE_CODE (type) == INTEGER_TYPE)
6219 if (fndecl)
6220 warning ("passing `%T' for %s %P of `%D'",
6221 TREE_TYPE (expr), errtype, parmnum, fndecl);
6222 else
6223 warning ("%s to `%T' from `%T'", errtype, type, TREE_TYPE (expr));
6225 /* And warn about assigning a negative value to an unsigned
6226 variable. */
6227 else if (TREE_UNSIGNED (type) && TREE_CODE (type) != BOOLEAN_TYPE)
6229 if (TREE_CODE (expr) == INTEGER_CST
6230 && TREE_NEGATED_INT (expr))
6232 if (fndecl)
6233 warning ("passing negative value `%E' for %s %P of `%D'",
6234 expr, errtype, parmnum, fndecl);
6235 else
6236 warning ("%s of negative value `%E' to `%T'",
6237 errtype, expr, type);
6240 overflow_warning (expr);
6242 if (TREE_CONSTANT (expr))
6243 expr = fold (expr);
6245 return expr;
6248 /* Convert value RHS to type TYPE as preparation for an assignment to
6249 an lvalue of type TYPE. ERRTYPE is a string to use in error
6250 messages: "assignment", "return", etc. If FNDECL is non-NULL, we
6251 are doing the conversion in order to pass the PARMNUMth argument of
6252 FNDECL. */
6254 static tree
6255 convert_for_assignment (type, rhs, errtype, fndecl, parmnum)
6256 tree type, rhs;
6257 const char *errtype;
6258 tree fndecl;
6259 int parmnum;
6261 register enum tree_code codel = TREE_CODE (type);
6262 register tree rhstype;
6263 register enum tree_code coder;
6265 if (codel == OFFSET_TYPE)
6266 abort ();
6268 if (TREE_CODE (rhs) == OFFSET_REF)
6269 rhs = resolve_offset_ref (rhs);
6271 /* Strip NON_LVALUE_EXPRs since we aren't using as an lvalue. */
6272 if (TREE_CODE (rhs) == NON_LVALUE_EXPR)
6273 rhs = TREE_OPERAND (rhs, 0);
6275 rhstype = TREE_TYPE (rhs);
6276 coder = TREE_CODE (rhstype);
6278 if (rhs == error_mark_node || rhstype == error_mark_node)
6279 return error_mark_node;
6280 if (TREE_CODE (rhs) == TREE_LIST && TREE_VALUE (rhs) == error_mark_node)
6281 return error_mark_node;
6283 rhs = dubious_conversion_warnings (type, rhs, errtype, fndecl, parmnum);
6285 /* The RHS of an assignment cannot have void type. */
6286 if (coder == VOID_TYPE)
6288 error ("void value not ignored as it ought to be");
6289 return error_mark_node;
6292 /* Simplify the RHS if possible. */
6293 if (TREE_CODE (rhs) == CONST_DECL)
6294 rhs = DECL_INITIAL (rhs);
6295 else if (coder != ARRAY_TYPE)
6296 rhs = decl_constant_value (rhs);
6298 /* [expr.ass]
6300 The expression is implicitly converted (clause _conv_) to the
6301 cv-unqualified type of the left operand.
6303 We allow bad conversions here because by the time we get to this point
6304 we are committed to doing the conversion. If we end up doing a bad
6305 conversion, convert_like will complain. */
6306 if (!can_convert_arg_bad (type, rhstype, rhs))
6308 /* When -Wno-pmf-conversions is use, we just silently allow
6309 conversions from pointers-to-members to plain pointers. If
6310 the conversion doesn't work, cp_convert will complain. */
6311 if (!warn_pmf2ptr
6312 && TYPE_PTR_P (type)
6313 && TYPE_PTRMEMFUNC_P (rhstype))
6314 rhs = cp_convert (strip_top_quals (type), rhs);
6315 else
6317 /* If the right-hand side has unknown type, then it is an
6318 overloaded function. Call instantiate_type to get error
6319 messages. */
6320 if (rhstype == unknown_type_node)
6321 instantiate_type (type, rhs, itf_complain);
6322 else if (fndecl)
6323 error ("cannot convert `%T' to `%T' for argument `%P' to `%D'",
6324 rhstype, type, parmnum, fndecl);
6325 else
6326 error ("cannot convert `%T' to `%T' in %s", rhstype, type,
6327 errtype);
6328 return error_mark_node;
6331 return perform_implicit_conversion (strip_top_quals (type), rhs);
6334 /* Convert RHS to be of type TYPE.
6335 If EXP is non-zero, it is the target of the initialization.
6336 ERRTYPE is a string to use in error messages.
6338 Two major differences between the behavior of
6339 `convert_for_assignment' and `convert_for_initialization'
6340 are that references are bashed in the former, while
6341 copied in the latter, and aggregates are assigned in
6342 the former (operator=) while initialized in the
6343 latter (X(X&)).
6345 If using constructor make sure no conversion operator exists, if one does
6346 exist, an ambiguity exists.
6348 If flags doesn't include LOOKUP_COMPLAIN, don't complain about anything. */
6350 tree
6351 convert_for_initialization (exp, type, rhs, flags, errtype, fndecl, parmnum)
6352 tree exp, type, rhs;
6353 int flags;
6354 const char *errtype;
6355 tree fndecl;
6356 int parmnum;
6358 register enum tree_code codel = TREE_CODE (type);
6359 register tree rhstype;
6360 register enum tree_code coder;
6362 /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
6363 Strip such NOP_EXPRs, since RHS is used in non-lvalue context. */
6364 if (TREE_CODE (rhs) == NOP_EXPR
6365 && TREE_TYPE (rhs) == TREE_TYPE (TREE_OPERAND (rhs, 0))
6366 && codel != REFERENCE_TYPE)
6367 rhs = TREE_OPERAND (rhs, 0);
6369 if (rhs == error_mark_node
6370 || (TREE_CODE (rhs) == TREE_LIST && TREE_VALUE (rhs) == error_mark_node))
6371 return error_mark_node;
6373 if (TREE_CODE (rhs) == OFFSET_REF)
6375 rhs = resolve_offset_ref (rhs);
6376 if (rhs == error_mark_node)
6377 return error_mark_node;
6380 if (TREE_CODE (TREE_TYPE (rhs)) == REFERENCE_TYPE)
6381 rhs = convert_from_reference (rhs);
6383 if ((TREE_CODE (TREE_TYPE (rhs)) == ARRAY_TYPE
6384 && TREE_CODE (type) != ARRAY_TYPE
6385 && (TREE_CODE (type) != REFERENCE_TYPE
6386 || TREE_CODE (TREE_TYPE (type)) != ARRAY_TYPE))
6387 || (TREE_CODE (TREE_TYPE (rhs)) == FUNCTION_TYPE
6388 && (TREE_CODE (type) != REFERENCE_TYPE
6389 || TREE_CODE (TREE_TYPE (type)) != FUNCTION_TYPE))
6390 || TREE_CODE (TREE_TYPE (rhs)) == METHOD_TYPE)
6391 rhs = default_conversion (rhs);
6393 rhstype = TREE_TYPE (rhs);
6394 coder = TREE_CODE (rhstype);
6396 if (coder == ERROR_MARK)
6397 return error_mark_node;
6399 /* We accept references to incomplete types, so we can
6400 return here before checking if RHS is of complete type. */
6402 if (codel == REFERENCE_TYPE)
6404 /* This should eventually happen in convert_arguments. */
6405 int savew = 0, savee = 0;
6407 if (fndecl)
6408 savew = warningcount, savee = errorcount;
6409 rhs = initialize_reference (type, rhs);
6410 if (fndecl)
6412 if (warningcount > savew)
6413 cp_warning_at ("in passing argument %P of `%+D'", parmnum, fndecl);
6414 else if (errorcount > savee)
6415 cp_error_at ("in passing argument %P of `%+D'", parmnum, fndecl);
6417 return rhs;
6420 if (exp != 0)
6421 exp = require_complete_type (exp);
6422 if (exp == error_mark_node)
6423 return error_mark_node;
6425 if (TREE_CODE (rhstype) == REFERENCE_TYPE)
6426 rhstype = TREE_TYPE (rhstype);
6428 type = complete_type (type);
6430 if (IS_AGGR_TYPE (type))
6431 return ocp_convert (type, rhs, CONV_IMPLICIT|CONV_FORCE_TEMP, flags);
6433 return convert_for_assignment (type, rhs, errtype, fndecl, parmnum);
6436 /* Expand an ASM statement with operands, handling output operands
6437 that are not variables or INDIRECT_REFS by transforming such
6438 cases into cases that expand_asm_operands can handle.
6440 Arguments are same as for expand_asm_operands.
6442 We don't do default conversions on all inputs, because it can screw
6443 up operands that are expected to be in memory. */
6445 void
6446 c_expand_asm_operands (string, outputs, inputs, clobbers, vol, filename, line)
6447 tree string, outputs, inputs, clobbers;
6448 int vol;
6449 const char *filename;
6450 int line;
6452 int noutputs = list_length (outputs);
6453 register int i;
6454 /* o[I] is the place that output number I should be written. */
6455 register tree *o = (tree *) alloca (noutputs * sizeof (tree));
6456 register tree tail;
6458 /* Record the contents of OUTPUTS before it is modified. */
6459 for (i = 0, tail = outputs; tail; tail = TREE_CHAIN (tail), i++)
6460 o[i] = TREE_VALUE (tail);
6462 /* Generate the ASM_OPERANDS insn;
6463 store into the TREE_VALUEs of OUTPUTS some trees for
6464 where the values were actually stored. */
6465 expand_asm_operands (string, outputs, inputs, clobbers, vol, filename, line);
6467 /* Copy all the intermediate outputs into the specified outputs. */
6468 for (i = 0, tail = outputs; tail; tail = TREE_CHAIN (tail), i++)
6470 if (o[i] != TREE_VALUE (tail))
6472 expand_expr (build_modify_expr (o[i], NOP_EXPR, TREE_VALUE (tail)),
6473 const0_rtx, VOIDmode, EXPAND_NORMAL);
6474 free_temp_slots ();
6476 /* Restore the original value so that it's correct the next
6477 time we expand this function. */
6478 TREE_VALUE (tail) = o[i];
6480 /* Detect modification of read-only values.
6481 (Otherwise done by build_modify_expr.) */
6482 else
6484 tree type = TREE_TYPE (o[i]);
6485 if (CP_TYPE_CONST_P (type)
6486 || (IS_AGGR_TYPE_CODE (TREE_CODE (type))
6487 && C_TYPE_FIELDS_READONLY (type)))
6488 readonly_error (o[i], "modification by `asm'", 1);
6492 /* Those MODIFY_EXPRs could do autoincrements. */
6493 emit_queue ();
6496 /* If RETVAL is the address of, or a reference to, a local variable or
6497 temporary give an appropraite warning. */
6499 static void
6500 maybe_warn_about_returning_address_of_local (retval)
6501 tree retval;
6503 tree valtype = TREE_TYPE (DECL_RESULT (current_function_decl));
6504 tree whats_returned = retval;
6506 for (;;)
6508 if (TREE_CODE (whats_returned) == COMPOUND_EXPR)
6509 whats_returned = TREE_OPERAND (whats_returned, 1);
6510 else if (TREE_CODE (whats_returned) == CONVERT_EXPR
6511 || TREE_CODE (whats_returned) == NON_LVALUE_EXPR
6512 || TREE_CODE (whats_returned) == NOP_EXPR)
6513 whats_returned = TREE_OPERAND (whats_returned, 0);
6514 else
6515 break;
6518 if (TREE_CODE (whats_returned) != ADDR_EXPR)
6519 return;
6520 whats_returned = TREE_OPERAND (whats_returned, 0);
6522 if (TREE_CODE (valtype) == REFERENCE_TYPE)
6524 if (TREE_CODE (whats_returned) == AGGR_INIT_EXPR
6525 || TREE_CODE (whats_returned) == TARGET_EXPR)
6527 /* Get the target. */
6528 whats_returned = TREE_OPERAND (whats_returned, 0);
6529 warning ("returning reference to temporary");
6530 return;
6532 if (TREE_CODE (whats_returned) == VAR_DECL
6533 && DECL_NAME (whats_returned)
6534 && TEMP_NAME_P (DECL_NAME (whats_returned)))
6536 warning ("reference to non-lvalue returned");
6537 return;
6541 if (TREE_CODE (whats_returned) == VAR_DECL
6542 && DECL_NAME (whats_returned)
6543 && DECL_FUNCTION_SCOPE_P (whats_returned)
6544 && !(TREE_STATIC (whats_returned)
6545 || TREE_PUBLIC (whats_returned)))
6547 if (TREE_CODE (valtype) == REFERENCE_TYPE)
6548 cp_warning_at ("reference to local variable `%D' returned",
6549 whats_returned);
6550 else
6551 cp_warning_at ("address of local variable `%D' returned",
6552 whats_returned);
6553 return;
6557 /* Check that returning RETVAL from the current function is legal.
6558 Return an expression explicitly showing all conversions required to
6559 change RETVAL into the function return type, and to assign it to
6560 the DECL_RESULT for the function. */
6562 tree
6563 check_return_expr (retval)
6564 tree retval;
6566 tree result;
6567 /* The type actually returned by the function, after any
6568 promotions. */
6569 tree valtype;
6570 int fn_returns_value_p;
6572 /* A `volatile' function is one that isn't supposed to return, ever.
6573 (This is a G++ extension, used to get better code for functions
6574 that call the `volatile' function.) */
6575 if (TREE_THIS_VOLATILE (current_function_decl))
6576 warning ("function declared `noreturn' has a `return' statement");
6578 /* Check for various simple errors. */
6579 if (DECL_DESTRUCTOR_P (current_function_decl))
6581 if (retval)
6582 error ("returning a value from a destructor");
6583 return NULL_TREE;
6585 else if (DECL_CONSTRUCTOR_P (current_function_decl))
6587 if (in_function_try_handler)
6588 /* If a return statement appears in a handler of the
6589 function-try-block of a constructor, the program is ill-formed. */
6590 error ("cannot return from a handler of a function-try-block of a constructor");
6591 else if (retval)
6592 /* You can't return a value from a constructor. */
6593 error ("returning a value from a constructor");
6594 return NULL_TREE;
6597 /* When no explicit return-value is given in a function with a named
6598 return value, the named return value is used. */
6599 result = DECL_RESULT (current_function_decl);
6600 valtype = TREE_TYPE (result);
6601 my_friendly_assert (valtype != NULL_TREE, 19990924);
6602 fn_returns_value_p = !VOID_TYPE_P (valtype);
6603 if (!retval && DECL_NAME (result) && fn_returns_value_p)
6604 retval = result;
6606 /* Check for a return statement with no return value in a function
6607 that's supposed to return a value. */
6608 if (!retval && fn_returns_value_p)
6610 pedwarn ("return-statement with no value, in function declared with a non-void return type");
6611 /* Clear this, so finish_function won't say that we reach the
6612 end of a non-void function (which we don't, we gave a
6613 return!). */
6614 current_function_returns_null = 0;
6616 /* Check for a return statement with a value in a function that
6617 isn't supposed to return a value. */
6618 else if (retval && !fn_returns_value_p)
6620 if (VOID_TYPE_P (TREE_TYPE (retval)))
6621 /* You can return a `void' value from a function of `void'
6622 type. In that case, we have to evaluate the expression for
6623 its side-effects. */
6624 finish_expr_stmt (retval);
6625 else
6626 pedwarn ("return-statement with a value, in function declared with a void return type");
6628 current_function_returns_null = 1;
6630 /* There's really no value to return, after all. */
6631 return NULL_TREE;
6633 else if (!retval)
6634 /* Remember that this function can sometimes return without a
6635 value. */
6636 current_function_returns_null = 1;
6637 else
6638 /* Remember that this function did return a value. */
6639 current_function_returns_value = 1;
6641 /* Only operator new(...) throw(), can return NULL [expr.new/13]. */
6642 if ((DECL_OVERLOADED_OPERATOR_P (current_function_decl) == NEW_EXPR
6643 || DECL_OVERLOADED_OPERATOR_P (current_function_decl) == VEC_NEW_EXPR)
6644 && !TYPE_NOTHROW_P (TREE_TYPE (current_function_decl))
6645 && ! flag_check_new
6646 && null_ptr_cst_p (retval))
6647 warning ("`operator new' must not return NULL unless it is declared `throw()' (or -fcheck-new is in effect)");
6649 /* Effective C++ rule 15. See also start_function. */
6650 if (warn_ecpp
6651 && DECL_NAME (current_function_decl) == ansi_assopname(NOP_EXPR)
6652 && retval != current_class_ref)
6653 warning ("`operator=' should return a reference to `*this'");
6655 /* The fabled Named Return Value optimization, as per [class.copy]/15:
6657 [...] For a function with a class return type, if the expression
6658 in the return statement is the name of a local object, and the cv-
6659 unqualified type of the local object is the same as the function
6660 return type, an implementation is permitted to omit creating the tem-
6661 porary object to hold the function return value [...]
6663 So, if this is a value-returning function that always returns the same
6664 local variable, remember it.
6666 It might be nice to be more flexible, and choose the first suitable
6667 variable even if the function sometimes returns something else, but
6668 then we run the risk of clobbering the variable we chose if the other
6669 returned expression uses the chosen variable somehow. And people expect
6670 this restriction, anyway. (jason 2000-11-19)
6672 See finish_function, genrtl_start_function, and declare_return_variable
6673 for other pieces of this optimization. */
6675 if (fn_returns_value_p && flag_elide_constructors)
6677 if (retval != NULL_TREE
6678 && (current_function_return_value == NULL_TREE
6679 || current_function_return_value == retval)
6680 && TREE_CODE (retval) == VAR_DECL
6681 && DECL_CONTEXT (retval) == current_function_decl
6682 && ! TREE_STATIC (retval)
6683 && (DECL_ALIGN (retval)
6684 >= DECL_ALIGN (DECL_RESULT (current_function_decl)))
6685 && same_type_p ((TYPE_MAIN_VARIANT
6686 (TREE_TYPE (retval))),
6687 (TYPE_MAIN_VARIANT
6688 (TREE_TYPE (TREE_TYPE (current_function_decl))))))
6689 current_function_return_value = retval;
6690 else
6691 current_function_return_value = error_mark_node;
6694 /* We don't need to do any conversions when there's nothing being
6695 returned. */
6696 if (!retval || retval == error_mark_node)
6697 return retval;
6699 /* Do any required conversions. */
6700 if (retval == result || DECL_CONSTRUCTOR_P (current_function_decl))
6701 /* No conversions are required. */
6703 else
6705 /* The type the function is declared to return. */
6706 tree functype = TREE_TYPE (TREE_TYPE (current_function_decl));
6708 /* First convert the value to the function's return type, then
6709 to the type of return value's location to handle the
6710 case that functype is smaller than the valtype. */
6711 retval = convert_for_initialization
6712 (NULL_TREE, functype, retval, LOOKUP_NORMAL|LOOKUP_ONLYCONVERTING,
6713 "return", NULL_TREE, 0);
6714 retval = convert (valtype, retval);
6716 /* If the conversion failed, treat this just like `return;'. */
6717 if (retval == error_mark_node)
6718 return retval;
6719 /* We can't initialize a register from a AGGR_INIT_EXPR. */
6720 else if (! current_function_returns_struct
6721 && TREE_CODE (retval) == TARGET_EXPR
6722 && TREE_CODE (TREE_OPERAND (retval, 1)) == AGGR_INIT_EXPR)
6723 retval = build (COMPOUND_EXPR, TREE_TYPE (retval), retval,
6724 TREE_OPERAND (retval, 0));
6725 else
6726 maybe_warn_about_returning_address_of_local (retval);
6729 /* Actually copy the value returned into the appropriate location. */
6730 if (retval && retval != result)
6731 retval = build (INIT_EXPR, TREE_TYPE (result), result, retval);
6733 return retval;
6737 /* Returns non-zero if the pointer-type FROM can be converted to the
6738 pointer-type TO via a qualification conversion. If CONSTP is -1,
6739 then we return non-zero if the pointers are similar, and the
6740 cv-qualification signature of FROM is a proper subset of that of TO.
6742 If CONSTP is positive, then all outer pointers have been
6743 const-qualified. */
6745 static int
6746 comp_ptr_ttypes_real (to, from, constp)
6747 tree to, from;
6748 int constp;
6750 int to_more_cv_qualified = 0;
6752 for (; ; to = TREE_TYPE (to), from = TREE_TYPE (from))
6754 if (TREE_CODE (to) != TREE_CODE (from))
6755 return 0;
6757 if (TREE_CODE (from) == OFFSET_TYPE
6758 && same_type_p (TYPE_OFFSET_BASETYPE (from),
6759 TYPE_OFFSET_BASETYPE (to)))
6760 continue;
6762 /* Const and volatile mean something different for function types,
6763 so the usual checks are not appropriate. */
6764 if (TREE_CODE (to) != FUNCTION_TYPE && TREE_CODE (to) != METHOD_TYPE)
6766 if (!at_least_as_qualified_p (to, from))
6767 return 0;
6769 if (!at_least_as_qualified_p (from, to))
6771 if (constp == 0)
6772 return 0;
6773 else
6774 ++to_more_cv_qualified;
6777 if (constp > 0)
6778 constp &= TYPE_READONLY (to);
6781 if (TREE_CODE (to) != POINTER_TYPE)
6782 return
6783 same_type_ignoring_top_level_qualifiers_p (to, from)
6784 && (constp >= 0 || to_more_cv_qualified);
6788 /* When comparing, say, char ** to char const **, this function takes the
6789 'char *' and 'char const *'. Do not pass non-pointer types to this
6790 function. */
6793 comp_ptr_ttypes (to, from)
6794 tree to, from;
6796 return comp_ptr_ttypes_real (to, from, 1);
6799 /* Returns 1 if to and from are (possibly multi-level) pointers to the same
6800 type or inheritance-related types, regardless of cv-quals. */
6803 ptr_reasonably_similar (to, from)
6804 tree to, from;
6806 for (; ; to = TREE_TYPE (to), from = TREE_TYPE (from))
6808 /* Any target type is similar enough to void. */
6809 if (TREE_CODE (to) == VOID_TYPE
6810 || TREE_CODE (from) == VOID_TYPE)
6811 return 1;
6813 if (TREE_CODE (to) != TREE_CODE (from))
6814 return 0;
6816 if (TREE_CODE (from) == OFFSET_TYPE
6817 && comptypes (TYPE_OFFSET_BASETYPE (to),
6818 TYPE_OFFSET_BASETYPE (from),
6819 COMPARE_BASE | COMPARE_RELAXED))
6820 continue;
6822 if (TREE_CODE (to) == INTEGER_TYPE
6823 && TYPE_PRECISION (to) == TYPE_PRECISION (from))
6824 return 1;
6826 if (TREE_CODE (to) == FUNCTION_TYPE)
6827 return 1;
6829 if (TREE_CODE (to) != POINTER_TYPE)
6830 return comptypes
6831 (TYPE_MAIN_VARIANT (to), TYPE_MAIN_VARIANT (from),
6832 COMPARE_BASE | COMPARE_RELAXED);
6836 /* Like comp_ptr_ttypes, for const_cast. */
6838 static int
6839 comp_ptr_ttypes_const (to, from)
6840 tree to, from;
6842 for (; ; to = TREE_TYPE (to), from = TREE_TYPE (from))
6844 if (TREE_CODE (to) != TREE_CODE (from))
6845 return 0;
6847 if (TREE_CODE (from) == OFFSET_TYPE
6848 && same_type_p (TYPE_OFFSET_BASETYPE (from),
6849 TYPE_OFFSET_BASETYPE (to)))
6850 continue;
6852 if (TREE_CODE (to) != POINTER_TYPE)
6853 return same_type_ignoring_top_level_qualifiers_p (to, from);
6857 /* Like comp_ptr_ttypes, for reinterpret_cast. */
6859 static int
6860 comp_ptr_ttypes_reinterpret (to, from)
6861 tree to, from;
6863 int constp = 1;
6865 for (; ; to = TREE_TYPE (to), from = TREE_TYPE (from))
6867 if (TREE_CODE (from) == OFFSET_TYPE)
6868 from = TREE_TYPE (from);
6869 if (TREE_CODE (to) == OFFSET_TYPE)
6870 to = TREE_TYPE (to);
6872 /* Const and volatile mean something different for function types,
6873 so the usual checks are not appropriate. */
6874 if (TREE_CODE (from) != FUNCTION_TYPE && TREE_CODE (from) != METHOD_TYPE
6875 && TREE_CODE (to) != FUNCTION_TYPE && TREE_CODE (to) != METHOD_TYPE)
6877 if (!at_least_as_qualified_p (to, from))
6878 return 0;
6880 if (! constp
6881 && !at_least_as_qualified_p (from, to))
6882 return 0;
6883 constp &= TYPE_READONLY (to);
6886 if (TREE_CODE (from) != POINTER_TYPE
6887 || TREE_CODE (to) != POINTER_TYPE)
6888 return 1;
6892 /* Returns the type qualifiers for this type, including the qualifiers on the
6893 elements for an array type. */
6896 cp_type_quals (type)
6897 tree type;
6899 type = strip_array_types (type);
6900 return TYPE_QUALS (type);
6903 /* Returns non-zero if the TYPE contains a mutable member */
6906 cp_has_mutable_p (type)
6907 tree type;
6909 type = strip_array_types (type);
6911 return CLASS_TYPE_P (type) && CLASSTYPE_HAS_MUTABLE (type);
6914 /* Subroutine of casts_away_constness. Make T1 and T2 point at
6915 exemplar types such that casting T1 to T2 is casting away castness
6916 if and only if there is no implicit conversion from T1 to T2. */
6918 static void
6919 casts_away_constness_r (t1, t2)
6920 tree *t1;
6921 tree *t2;
6923 int quals1;
6924 int quals2;
6926 /* [expr.const.cast]
6928 For multi-level pointer to members and multi-level mixed pointers
6929 and pointers to members (conv.qual), the "member" aspect of a
6930 pointer to member level is ignored when determining if a const
6931 cv-qualifier has been cast away. */
6932 if (TYPE_PTRMEM_P (*t1))
6933 *t1 = build_pointer_type (TREE_TYPE (TREE_TYPE (*t1)));
6934 if (TYPE_PTRMEM_P (*t2))
6935 *t2 = build_pointer_type (TREE_TYPE (TREE_TYPE (*t2)));
6937 /* [expr.const.cast]
6939 For two pointer types:
6941 X1 is T1cv1,1 * ... cv1,N * where T1 is not a pointer type
6942 X2 is T2cv2,1 * ... cv2,M * where T2 is not a pointer type
6943 K is min(N,M)
6945 casting from X1 to X2 casts away constness if, for a non-pointer
6946 type T there does not exist an implicit conversion (clause
6947 _conv_) from:
6949 Tcv1,(N-K+1) * cv1,(N-K+2) * ... cv1,N *
6953 Tcv2,(M-K+1) * cv2,(M-K+2) * ... cv2,M *. */
6955 if (TREE_CODE (*t1) != POINTER_TYPE
6956 || TREE_CODE (*t2) != POINTER_TYPE)
6958 *t1 = cp_build_qualified_type (void_type_node,
6959 cp_type_quals (*t1));
6960 *t2 = cp_build_qualified_type (void_type_node,
6961 cp_type_quals (*t2));
6962 return;
6965 quals1 = cp_type_quals (*t1);
6966 quals2 = cp_type_quals (*t2);
6967 *t1 = TREE_TYPE (*t1);
6968 *t2 = TREE_TYPE (*t2);
6969 casts_away_constness_r (t1, t2);
6970 *t1 = build_pointer_type (*t1);
6971 *t2 = build_pointer_type (*t2);
6972 *t1 = cp_build_qualified_type (*t1, quals1);
6973 *t2 = cp_build_qualified_type (*t2, quals2);
6976 /* Returns non-zero if casting from TYPE1 to TYPE2 casts away
6977 constness. */
6979 static int
6980 casts_away_constness (t1, t2)
6981 tree t1;
6982 tree t2;
6984 if (TREE_CODE (t2) == REFERENCE_TYPE)
6986 /* [expr.const.cast]
6988 Casting from an lvalue of type T1 to an lvalue of type T2
6989 using a reference cast casts away constness if a cast from an
6990 rvalue of type "pointer to T1" to the type "pointer to T2"
6991 casts away constness. */
6992 t1 = (TREE_CODE (t1) == REFERENCE_TYPE
6993 ? TREE_TYPE (t1) : t1);
6994 return casts_away_constness (build_pointer_type (t1),
6995 build_pointer_type (TREE_TYPE (t2)));
6998 if (TYPE_PTRMEM_P (t1) && TYPE_PTRMEM_P (t2))
6999 /* [expr.const.cast]
7001 Casting from an rvalue of type "pointer to data member of X
7002 of type T1" to the type "pointer to data member of Y of type
7003 T2" casts away constness if a cast from an rvalue of type
7004 "pointer to T1" to the type "pointer to T2" casts away
7005 constness. */
7006 return casts_away_constness
7007 (build_pointer_type (TREE_TYPE (TREE_TYPE (t1))),
7008 build_pointer_type (TREE_TYPE (TREE_TYPE (t2))));
7010 /* Casting away constness is only something that makes sense for
7011 pointer or reference types. */
7012 if (TREE_CODE (t1) != POINTER_TYPE
7013 || TREE_CODE (t2) != POINTER_TYPE)
7014 return 0;
7016 /* Top-level qualifiers don't matter. */
7017 t1 = TYPE_MAIN_VARIANT (t1);
7018 t2 = TYPE_MAIN_VARIANT (t2);
7019 casts_away_constness_r (&t1, &t2);
7020 if (!can_convert (t2, t1))
7021 return 1;
7023 return 0;
7026 /* Returns TYPE with its cv qualifiers removed
7027 TYPE is T cv* .. *cv where T is not a pointer type,
7028 returns T * .. *. (If T is an array type, then the cv qualifiers
7029 above are those of the array members.) */
7031 static tree
7032 strip_all_pointer_quals (type)
7033 tree type;
7035 if (TREE_CODE (type) == POINTER_TYPE)
7036 return build_pointer_type (strip_all_pointer_quals (TREE_TYPE (type)));
7037 else if (TREE_CODE (type) == OFFSET_TYPE)
7038 return build_offset_type (TYPE_OFFSET_BASETYPE (type),
7039 strip_all_pointer_quals (TREE_TYPE (type)));
7040 else
7041 return TYPE_MAIN_VARIANT (type);