* cp-tree.h (make_typename_type): Change prototype.
[official-gcc.git] / gcc / cp / typeck.c
bloba923b8117bf4b007347cf07b443cf8467109f192
1 /* Build expressions with type checking for C++ compiler.
2 Copyright (C) 1987, 88, 89, 92-98, 1999 Free Software Foundation, Inc.
3 Hacked by Michael Tiemann (tiemann@cygnus.com)
5 This file is part of GNU CC.
7 GNU CC is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2, or (at your option)
10 any later version.
12 GNU CC is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with GNU CC; see the file COPYING. If not, write to
19 the Free Software Foundation, 59 Temple Place - Suite 330,
20 Boston, MA 02111-1307, USA. */
23 /* This file is part of the C++ front end.
24 It contains routines to build C++ expressions given their operands,
25 including computing the types of the result, C and C++ specific error
26 checks, and some optimization.
28 There are also routines to build RETURN_STMT nodes and CASE_STMT nodes,
29 and to process initializations in declarations (since they work
30 like a strange sort of assignment). */
32 #include "config.h"
33 #include "system.h"
34 #include "tree.h"
35 #include "rtl.h"
36 #include "cp-tree.h"
37 #include "tm_p.h"
38 #include "flags.h"
39 #include "output.h"
40 #include "expr.h"
41 #include "toplev.h"
42 #include "defaults.h"
44 static tree convert_for_assignment PROTO((tree, tree, const char *, tree,
45 int));
46 static tree pointer_int_sum PROTO((enum tree_code, tree, tree));
47 static tree rationalize_conditional_expr PROTO((enum tree_code, tree));
48 static int comp_target_parms PROTO((tree, tree, int));
49 static int comp_ptr_ttypes_real PROTO((tree, tree, int));
50 static int comp_ptr_ttypes_const PROTO((tree, tree));
51 static int comp_ptr_ttypes_reinterpret PROTO((tree, tree));
52 static int comp_except_types PROTO((tree, tree, int));
53 static int comp_array_types PROTO((int (*) (tree, tree, int), tree,
54 tree, int));
55 static tree common_base_type PROTO((tree, tree));
56 #if 0
57 static tree convert_sequence PROTO((tree, tree));
58 #endif
59 static tree lookup_anon_field PROTO((tree, tree));
60 static tree pointer_diff PROTO((tree, tree, tree));
61 static tree build_component_addr PROTO((tree, tree));
62 static tree qualify_type PROTO((tree, tree));
63 static tree get_delta_difference PROTO((tree, tree, int));
64 static int comp_cv_target_types PROTO((tree, tree, int));
65 static void casts_away_constness_r PROTO((tree *, tree *));
66 static int casts_away_constness PROTO ((tree, tree));
67 static void maybe_warn_about_returning_address_of_local PROTO ((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 (TYPE_SIZE (type) != 0
108 && TYPE_SIZE (type) != size_zero_node)
109 return value;
111 /* If we see X::Y, we build an OFFSET_TYPE which has
112 not been laid out. Try to avoid an error by interpreting
113 it as this->X::Y, if reasonable. */
114 if (TREE_CODE (value) == OFFSET_REF
115 && current_class_ref != 0
116 && TREE_OPERAND (value, 0) == current_class_ref)
118 tree base, member = TREE_OPERAND (value, 1);
119 tree basetype = TYPE_OFFSET_BASETYPE (type);
120 my_friendly_assert (TREE_CODE (member) == FIELD_DECL, 305);
121 base = convert_pointer_to (basetype, current_class_ptr);
122 value = build (COMPONENT_REF, TREE_TYPE (member),
123 build_indirect_ref (base, NULL_PTR), member);
124 return require_complete_type (value);
127 if (complete_type_or_else (type, value))
128 return value;
129 else
130 return error_mark_node;
133 /* Try to complete TYPE, if it is incomplete. For example, if TYPE is
134 a template instantiation, do the instantiation. Returns TYPE,
135 whether or not it could be completed, unless something goes
136 horribly wrong, in which case the error_mark_node is returned. */
138 tree
139 complete_type (type)
140 tree type;
142 if (type == NULL_TREE)
143 /* Rather than crash, we return something sure to cause an error
144 at some point. */
145 return error_mark_node;
147 if (type == error_mark_node || TYPE_SIZE (type) != NULL_TREE)
149 else if (TREE_CODE (type) == ARRAY_TYPE && TYPE_DOMAIN (type))
151 tree t = complete_type (TREE_TYPE (type));
152 if (TYPE_SIZE (t) != NULL_TREE && ! processing_template_decl)
153 layout_type (type);
154 TYPE_NEEDS_CONSTRUCTING (type)
155 = TYPE_NEEDS_CONSTRUCTING (TYPE_MAIN_VARIANT (t));
156 TYPE_NEEDS_DESTRUCTOR (type)
157 = TYPE_NEEDS_DESTRUCTOR (TYPE_MAIN_VARIANT (t));
159 else if (CLASS_TYPE_P (type) && CLASSTYPE_TEMPLATE_INSTANTIATION (type))
160 instantiate_class_template (TYPE_MAIN_VARIANT (type));
162 return type;
165 /* Like complete_type, but issue an error if the TYPE cannot be
166 completed. VALUE is used for informative diagnostics.
167 Returns NULL_TREE if the type cannot be made complete. */
169 tree
170 complete_type_or_else (type, value)
171 tree type;
172 tree value;
174 type = complete_type (type);
175 if (type == error_mark_node)
176 /* We already issued an error. */
177 return NULL_TREE;
178 else if (!TYPE_SIZE (type) || TYPE_SIZE (type) == size_zero_node)
180 incomplete_type_error (value, type);
181 return NULL_TREE;
183 else
184 return type;
187 /* Return truthvalue of whether type of EXP is instantiated. */
190 type_unknown_p (exp)
191 tree exp;
193 return (TREE_CODE (exp) == OVERLOAD
194 || TREE_CODE (exp) == TREE_LIST
195 || TREE_TYPE (exp) == unknown_type_node
196 || (TREE_CODE (TREE_TYPE (exp)) == OFFSET_TYPE
197 && TREE_TYPE (TREE_TYPE (exp)) == unknown_type_node));
200 /* Return truthvalue of whether T is function (or pfn) type. */
203 fntype_p (t)
204 tree t;
206 return (TREE_CODE (t) == FUNCTION_TYPE || TREE_CODE (t) == METHOD_TYPE
207 || (TREE_CODE (t) == POINTER_TYPE
208 && (TREE_CODE (TREE_TYPE (t)) == FUNCTION_TYPE
209 || TREE_CODE (TREE_TYPE (t)) == METHOD_TYPE)));
212 /* Return a variant of TYPE which has all the type qualifiers of LIKE
213 as well as those of TYPE. */
215 static tree
216 qualify_type (type, like)
217 tree type, like;
219 /* @@ Must do member pointers here. */
220 return cp_build_qualified_type (type, (CP_TYPE_QUALS (type)
221 | CP_TYPE_QUALS (like)));
224 /* Return the common type of two parameter lists.
225 We assume that comptypes has already been done and returned 1;
226 if that isn't so, this may crash.
228 As an optimization, free the space we allocate if the parameter
229 lists are already common. */
231 tree
232 commonparms (p1, p2)
233 tree p1, p2;
235 tree oldargs = p1, newargs, n;
236 int i, len;
237 int any_change = 0;
238 char *first_obj = (char *) oballoc (0);
240 len = list_length (p1);
241 newargs = tree_last (p1);
243 if (newargs == void_list_node)
244 i = 1;
245 else
247 i = 0;
248 newargs = 0;
251 for (; i < len; i++)
252 newargs = tree_cons (NULL_TREE, NULL_TREE, newargs);
254 n = newargs;
256 for (i = 0; p1;
257 p1 = TREE_CHAIN (p1), p2 = TREE_CHAIN (p2), n = TREE_CHAIN (n), i++)
259 if (TREE_PURPOSE (p1) && !TREE_PURPOSE (p2))
261 TREE_PURPOSE (n) = TREE_PURPOSE (p1);
262 any_change = 1;
264 else if (! TREE_PURPOSE (p1))
266 if (TREE_PURPOSE (p2))
268 TREE_PURPOSE (n) = TREE_PURPOSE (p2);
269 any_change = 1;
272 else
274 if (1 != simple_cst_equal (TREE_PURPOSE (p1), TREE_PURPOSE (p2)))
275 any_change = 1;
276 TREE_PURPOSE (n) = TREE_PURPOSE (p2);
278 if (TREE_VALUE (p1) != TREE_VALUE (p2))
280 any_change = 1;
281 TREE_VALUE (n) = common_type (TREE_VALUE (p1), TREE_VALUE (p2));
283 else
284 TREE_VALUE (n) = TREE_VALUE (p1);
286 if (! any_change)
288 obfree (first_obj);
289 return oldargs;
292 return newargs;
295 /* Given a type, perhaps copied for a typedef,
296 find the "original" version of it. */
297 tree
298 original_type (t)
299 tree t;
301 while (TYPE_NAME (t) != NULL_TREE)
303 tree x = TYPE_NAME (t);
304 if (TREE_CODE (x) != TYPE_DECL)
305 break;
306 x = DECL_ORIGINAL_TYPE (x);
307 if (x == NULL_TREE)
308 break;
309 t = x;
311 return t;
314 /* T1 and T2 are arithmetic or enumeration types. Return the type
315 that will result from the "usual arithmetic converions" on T1 and
316 T2 as described in [expr]. */
318 tree
319 type_after_usual_arithmetic_conversions (t1, t2)
320 tree t1;
321 tree t2;
323 enum tree_code code1 = TREE_CODE (t1);
324 enum tree_code code2 = TREE_CODE (t2);
325 tree attributes;
327 /* FIXME: Attributes. */
328 my_friendly_assert (ARITHMETIC_TYPE_P (t1)
329 || TREE_CODE (t1) == ENUMERAL_TYPE,
330 19990725);
331 my_friendly_assert (ARITHMETIC_TYPE_P (t2)
332 || TREE_CODE (t2) == ENUMERAL_TYPE,
333 19990725);
335 /* In what follows, we slightly generalize the rules given in [expr]
336 so as to deal with `long long'. First, merge the attributes. */
337 attributes = merge_machine_type_attributes (t1, t2);
339 /* If only one is real, use it as the result. */
340 if (code1 == REAL_TYPE && code2 != REAL_TYPE)
341 return build_type_attribute_variant (t1, attributes);
342 if (code2 == REAL_TYPE && code1 != REAL_TYPE)
343 return build_type_attribute_variant (t2, attributes);
345 /* Perform the integral promotions. */
346 if (code1 != REAL_TYPE)
348 t1 = type_promotes_to (t1);
349 t2 = type_promotes_to (t2);
352 /* Both real or both integers; use the one with greater precision. */
353 if (TYPE_PRECISION (t1) > TYPE_PRECISION (t2))
354 return build_type_attribute_variant (t1, attributes);
355 else if (TYPE_PRECISION (t2) > TYPE_PRECISION (t1))
356 return build_type_attribute_variant (t2, attributes);
358 if (code1 != REAL_TYPE)
360 /* If one is unsigned long long, then convert the other to unsigned
361 long long. */
362 if (same_type_p (TYPE_MAIN_VARIANT (t1), long_long_unsigned_type_node)
363 || same_type_p (TYPE_MAIN_VARIANT (t2), long_long_unsigned_type_node))
364 return build_type_attribute_variant (long_long_unsigned_type_node,
365 attributes);
366 /* If one is a long long, and the other is an unsigned long, and
367 long long can represent all the values of an unsigned long, then
368 convert to a long long. Otherwise, convert to an unsigned long
369 long. Otherwise, if either operand is long long, convert the
370 other to long long.
372 Since we're here, we know the TYPE_PRECISION is the same;
373 therefore converting to long long cannot represent all the values
374 of an unsigned long, so we choose unsigned long long in that
375 case. */
376 if (same_type_p (TYPE_MAIN_VARIANT (t1), long_long_integer_type_node)
377 || same_type_p (TYPE_MAIN_VARIANT (t2), long_long_integer_type_node))
379 tree t = ((TREE_UNSIGNED (t1) || TREE_UNSIGNED (t2))
380 ? long_long_unsigned_type_node
381 : long_long_integer_type_node);
382 return build_type_attribute_variant (t, attributes);
385 /* Go through the same procedure, but for longs. */
386 if (same_type_p (TYPE_MAIN_VARIANT (t1), long_unsigned_type_node)
387 || same_type_p (TYPE_MAIN_VARIANT (t2), long_unsigned_type_node))
388 return build_type_attribute_variant (long_unsigned_type_node,
389 attributes);
390 if (same_type_p (TYPE_MAIN_VARIANT (t1), long_integer_type_node)
391 || same_type_p (TYPE_MAIN_VARIANT (t2), long_integer_type_node))
393 tree t = ((TREE_UNSIGNED (t1) || TREE_UNSIGNED (t2))
394 ? long_unsigned_type_node : long_integer_type_node);
395 return build_type_attribute_variant (t, attributes);
397 /* Otherwise prefer the unsigned one. */
398 if (TREE_UNSIGNED (t1))
399 return build_type_attribute_variant (t1, attributes);
400 else
401 return build_type_attribute_variant (t2, attributes);
403 else
405 if (same_type_p (TYPE_MAIN_VARIANT (t1), long_double_type_node)
406 || same_type_p (TYPE_MAIN_VARIANT (t2), long_double_type_node))
407 return build_type_attribute_variant (long_double_type_node,
408 attributes);
409 if (same_type_p (TYPE_MAIN_VARIANT (t1), double_type_node)
410 || same_type_p (TYPE_MAIN_VARIANT (t2), double_type_node))
411 return build_type_attribute_variant (double_type_node,
412 attributes);
413 else
414 return build_type_attribute_variant (float_type_node,
415 attributes);
419 /* Return the composite pointer type (see [expr.rel]) for T1 and T2.
420 ARG1 and ARG2 are the values with those types. The LOCATION is a
421 string describing the current location, in case an error occurs. */
423 tree
424 composite_pointer_type (t1, t2, arg1, arg2, location)
425 tree t1;
426 tree t2;
427 tree arg1;
428 tree arg2;
429 const char* location;
431 tree result_type;
433 /* [expr.rel]
435 If one operand is a null pointer constant, the composite pointer
436 type is the type of the other operand. */
437 if (null_ptr_cst_p (arg1))
438 return t2;
439 if (null_ptr_cst_p (arg2))
440 return t1;
442 /* Deal with pointer-to-member functions in the same way as we deal
443 with pointers to functions. */
444 if (TYPE_PTRMEMFUNC_P (t1))
445 t1 = TYPE_PTRMEMFUNC_FN_TYPE (t1);
446 if (TYPE_PTRMEMFUNC_P (t2))
447 t2 = TYPE_PTRMEMFUNC_FN_TYPE (t2);
449 if (comp_target_types (t1, t2, 1))
450 result_type = common_type (t1, t2);
451 else if (TYPE_MAIN_VARIANT (TREE_TYPE (t1)) == void_type_node)
453 if (pedantic && TREE_CODE (t2) == FUNCTION_TYPE)
454 pedwarn ("ANSI C++ forbids %s between `void *' and function pointer",
455 location);
456 result_type = qualify_type (t1, t2);
458 else if (TYPE_MAIN_VARIANT (TREE_TYPE (t2)) == void_type_node)
460 if (pedantic && TREE_CODE (t1) == FUNCTION_TYPE)
461 pedwarn ("ANSI C++ forbids %s between `void *' and function pointer",
462 location);
463 result_type = qualify_type (t2, t1);
465 /* C++ */
466 else if (same_or_base_type_p (t2, t1))
467 result_type = t2;
468 else if (IS_AGGR_TYPE (TREE_TYPE (t1))
469 && IS_AGGR_TYPE (TREE_TYPE (t2))
470 && (result_type = common_base_type (TREE_TYPE (t1),
471 TREE_TYPE (t2))))
473 if (result_type == error_mark_node)
475 cp_error ("common base type of types `%T' and `%T' is ambiguous",
476 TREE_TYPE (t1), TREE_TYPE (t2));
477 result_type = ptr_type_node;
479 else
481 if (pedantic
482 && result_type != TREE_TYPE (t1)
483 && result_type != TREE_TYPE (t2))
484 cp_pedwarn ("`%T' and `%T' converted to `%T *' in %s",
485 t1, t2, result_type, location);
487 result_type = build_pointer_type (result_type);
490 else
492 cp_pedwarn ("pointer type mismatch in %s", location);
493 result_type = ptr_type_node;
496 return result_type;
499 /* Return the common type of two types.
500 We assume that comptypes has already been done and returned 1;
501 if that isn't so, this may crash.
503 This is the type for the result of most arithmetic operations
504 if the operands have the given two types.
506 We do not deal with enumeral types here because they have already been
507 converted to integer types. */
509 tree
510 common_type (t1, t2)
511 tree t1, t2;
513 register enum tree_code code1;
514 register enum tree_code code2;
515 tree attributes;
517 /* Save time if the two types are the same. */
518 if (t1 == t2)
519 return t1;
520 t1 = original_type (t1);
521 t2 = original_type (t2);
522 if (t1 == t2)
523 return t1;
525 /* If one type is nonsense, use the other. */
526 if (t1 == error_mark_node)
527 return t2;
528 if (t2 == error_mark_node)
529 return t1;
531 if ((ARITHMETIC_TYPE_P (t1) || TREE_CODE (t1) == ENUMERAL_TYPE)
532 && (ARITHMETIC_TYPE_P (t2) || TREE_CODE (t2) == ENUMERAL_TYPE))
533 return type_after_usual_arithmetic_conversions (t1, t2);
535 /* Merge the attributes. */
536 attributes = merge_machine_type_attributes (t1, t2);
538 /* Treat an enum type as the unsigned integer type of the same width. */
540 if (TREE_CODE (t1) == ENUMERAL_TYPE)
541 t1 = type_for_size (TYPE_PRECISION (t1), 1);
542 if (TREE_CODE (t2) == ENUMERAL_TYPE)
543 t2 = type_for_size (TYPE_PRECISION (t2), 1);
545 if (TYPE_PTRMEMFUNC_P (t1))
546 t1 = TYPE_PTRMEMFUNC_FN_TYPE (t1);
547 if (TYPE_PTRMEMFUNC_P (t2))
548 t2 = TYPE_PTRMEMFUNC_FN_TYPE (t2);
550 code1 = TREE_CODE (t1);
551 code2 = TREE_CODE (t2);
553 /* If one type is complex, form the common type of the non-complex
554 components, then make that complex. Use T1 or T2 if it is the
555 required type. */
556 if (code1 == COMPLEX_TYPE || code2 == COMPLEX_TYPE)
558 tree subtype1 = code1 == COMPLEX_TYPE ? TREE_TYPE (t1) : t1;
559 tree subtype2 = code2 == COMPLEX_TYPE ? TREE_TYPE (t2) : t2;
560 tree subtype = common_type (subtype1, subtype2);
562 if (code1 == COMPLEX_TYPE && TREE_TYPE (t1) == subtype)
563 return build_type_attribute_variant (t1, attributes);
564 else if (code2 == COMPLEX_TYPE && TREE_TYPE (t2) == subtype)
565 return build_type_attribute_variant (t2, attributes);
566 else
567 return build_type_attribute_variant (build_complex_type (subtype),
568 attributes);
571 switch (code1)
573 case INTEGER_TYPE:
574 case REAL_TYPE:
575 /* We should have called type_after_usual_arithmetic_conversions
576 above. */
577 my_friendly_abort (19990725);
578 break;
580 case POINTER_TYPE:
581 case REFERENCE_TYPE:
582 /* For two pointers, do this recursively on the target type,
583 and combine the qualifiers of the two types' targets. */
584 /* This code was turned off; I don't know why.
585 But ANSI C++ specifies doing this with the qualifiers.
586 So I turned it on again. */
588 tree tt1 = TREE_TYPE (t1);
589 tree tt2 = TREE_TYPE (t2);
590 tree b1, b2;
591 int type_quals;
592 tree target;
594 if (TREE_CODE (tt1) == OFFSET_TYPE)
596 b1 = TYPE_OFFSET_BASETYPE (tt1);
597 b2 = TYPE_OFFSET_BASETYPE (tt2);
598 tt1 = TREE_TYPE (tt1);
599 tt2 = TREE_TYPE (tt2);
601 else
602 b1 = b2 = NULL_TREE;
604 type_quals = (CP_TYPE_QUALS (tt1) | CP_TYPE_QUALS (tt2));
605 tt1 = TYPE_MAIN_VARIANT (tt1);
606 tt2 = TYPE_MAIN_VARIANT (tt2);
608 if (tt1 == tt2)
609 target = tt1;
610 else if (tt1 == void_type_node || tt2 == void_type_node)
611 target = void_type_node;
612 else if (tt1 == unknown_type_node)
613 target = tt2;
614 else if (tt2 == unknown_type_node)
615 target = tt1;
616 else
617 target = common_type (tt1, tt2);
619 target = cp_build_qualified_type (target, type_quals);
621 if (b1)
623 if (same_type_p (b1, b2)
624 || (DERIVED_FROM_P (b1, b2) && binfo_or_else (b1, b2)))
625 target = build_offset_type (b2, target);
626 else if (binfo_or_else (b2, b1))
627 target = build_offset_type (b1, target);
630 if (code1 == POINTER_TYPE)
631 t1 = build_pointer_type (target);
632 else
633 t1 = build_reference_type (target);
634 t1 = build_type_attribute_variant (t1, attributes);
636 if (TREE_CODE (target) == METHOD_TYPE)
637 t1 = build_ptrmemfunc_type (t1);
639 return t1;
642 case ARRAY_TYPE:
644 tree elt = common_type (TREE_TYPE (t1), TREE_TYPE (t2));
645 /* Save space: see if the result is identical to one of the args. */
646 if (elt == TREE_TYPE (t1) && TYPE_DOMAIN (t1))
647 return build_type_attribute_variant (t1, attributes);
648 if (elt == TREE_TYPE (t2) && TYPE_DOMAIN (t2))
649 return build_type_attribute_variant (t2, attributes);
650 /* Merge the element types, and have a size if either arg has one. */
651 t1 = build_cplus_array_type
652 (elt, TYPE_DOMAIN (TYPE_DOMAIN (t1) ? t1 : t2));
653 return build_type_attribute_variant (t1, attributes);
656 case FUNCTION_TYPE:
657 /* Function types: prefer the one that specified arg types.
658 If both do, merge the arg types. Also merge the return types. */
660 tree valtype = common_type (TREE_TYPE (t1), TREE_TYPE (t2));
661 tree p1 = TYPE_ARG_TYPES (t1);
662 tree p2 = TYPE_ARG_TYPES (t2);
663 tree rval, raises;
665 /* Save space: see if the result is identical to one of the args. */
666 if (valtype == TREE_TYPE (t1) && ! p2)
667 return build_type_attribute_variant (t1, attributes);
668 if (valtype == TREE_TYPE (t2) && ! p1)
669 return build_type_attribute_variant (t2, attributes);
671 /* Simple way if one arg fails to specify argument types. */
672 if (p1 == NULL_TREE || TREE_VALUE (p1) == void_type_node)
674 rval = build_function_type (valtype, p2);
675 if ((raises = TYPE_RAISES_EXCEPTIONS (t2)))
676 rval = build_exception_variant (rval, raises);
677 return build_type_attribute_variant (rval, attributes);
679 raises = TYPE_RAISES_EXCEPTIONS (t1);
680 if (p2 == NULL_TREE || TREE_VALUE (p2) == void_type_node)
682 rval = build_function_type (valtype, p1);
683 if (raises)
684 rval = build_exception_variant (rval, raises);
685 return build_type_attribute_variant (rval, attributes);
688 rval = build_function_type (valtype, commonparms (p1, p2));
689 rval = build_exception_variant (rval, raises);
690 return build_type_attribute_variant (rval, attributes);
693 case RECORD_TYPE:
694 case UNION_TYPE:
695 t1 = TYPE_MAIN_VARIANT (t1);
696 t2 = TYPE_MAIN_VARIANT (t2);
698 if (DERIVED_FROM_P (t1, t2) && binfo_or_else (t1, t2))
699 return build_type_attribute_variant (t1, attributes);
700 else if (binfo_or_else (t2, t1))
701 return build_type_attribute_variant (t2, attributes);
702 else
704 compiler_error ("common_type called with uncommon aggregate types");
705 return error_mark_node;
708 case METHOD_TYPE:
709 if (TREE_CODE (TREE_TYPE (t1)) == TREE_CODE (TREE_TYPE (t2)))
711 /* Get this value the long way, since TYPE_METHOD_BASETYPE
712 is just the main variant of this. */
713 tree basetype;
714 tree raises, t3;
716 tree b1 = TYPE_OFFSET_BASETYPE (t1);
717 tree b2 = TYPE_OFFSET_BASETYPE (t2);
719 if (same_type_p (b1, b2)
720 || (DERIVED_FROM_P (b1, b2) && binfo_or_else (b1, b2)))
721 basetype = TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (t2)));
722 else
724 if (binfo_or_else (b2, b1) == NULL_TREE)
725 compiler_error ("common_type called with uncommon method types");
726 basetype = TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (t1)));
729 raises = TYPE_RAISES_EXCEPTIONS (t1);
731 /* If this was a member function type, get back to the
732 original type of type member function (i.e., without
733 the class instance variable up front. */
734 t1 = build_function_type (TREE_TYPE (t1),
735 TREE_CHAIN (TYPE_ARG_TYPES (t1)));
736 t2 = build_function_type (TREE_TYPE (t2),
737 TREE_CHAIN (TYPE_ARG_TYPES (t2)));
738 t3 = common_type (t1, t2);
739 t3 = build_cplus_method_type (basetype, TREE_TYPE (t3),
740 TYPE_ARG_TYPES (t3));
741 t1 = build_exception_variant (t3, raises);
743 else
744 compiler_error ("common_type called with uncommon method types");
746 return build_type_attribute_variant (t1, attributes);
748 case OFFSET_TYPE:
749 /* Pointers to members should now be handled by the POINTER_TYPE
750 case above. */
751 my_friendly_abort (990325);
753 default:
754 return build_type_attribute_variant (t1, attributes);
758 /* Compare two exception specifier types for exactness or subsetness, if
759 allowed. Returns 0 for mismatch, 1 for same, 2 if B is allowed by A.
761 [except.spec] "If a class X ... objects of class X or any class publicly
762 and unambigously derrived from X. Similarly, if a pointer type Y * ...
763 exceptions of type Y * or that are pointers to any type publicly and
764 unambigously derrived from Y. Otherwise a function only allows exceptions
765 that have the same type ..."
766 This does not mention cv qualifiers and is different to what throw
767 [except.throw] and catch [except.catch] will do. They will ignore the
768 top level cv qualifiers, and allow qualifiers in the pointer to class
769 example.
771 We implement the letter of the standard. */
773 static int
774 comp_except_types (a, b, exact)
775 tree a, b;
776 int exact;
778 if (same_type_p (a, b))
779 return 1;
780 else if (!exact)
782 if (CP_TYPE_QUALS (a) || CP_TYPE_QUALS (b))
783 return 0;
785 if (TREE_CODE (a) == POINTER_TYPE
786 && TREE_CODE (b) == POINTER_TYPE)
788 a = TREE_TYPE (a);
789 b = TREE_TYPE (b);
790 if (CP_TYPE_QUALS (a) || CP_TYPE_QUALS (b))
791 return 0;
794 if (TREE_CODE (a) != RECORD_TYPE
795 || TREE_CODE (b) != RECORD_TYPE)
796 return 0;
798 if (ACCESSIBLY_UNIQUELY_DERIVED_P (a, b))
799 return 2;
801 return 0;
804 /* Return 1 if TYPE1 and TYPE2 are equivalent exception specifiers.
805 If EXACT is 0, T2 can be a subset of T1 (according to 15.4/7),
806 otherwise it must be exact. Exception lists are unordered, but
807 we've already filtered out duplicates. Most lists will be in order,
808 we should try to make use of that. */
811 comp_except_specs (t1, t2, exact)
812 tree t1, t2;
813 int exact;
815 tree probe;
816 tree base;
817 int length = 0;
819 if (t1 == t2)
820 return 1;
822 if (t1 == NULL_TREE) /* T1 is ... */
823 return t2 == NULL_TREE || !exact;
824 if (!TREE_VALUE (t1)) /* t1 is EMPTY */
825 return t2 != NULL_TREE && !TREE_VALUE (t2);
826 if (t2 == NULL_TREE) /* T2 is ... */
827 return 0;
828 if (TREE_VALUE(t1) && !TREE_VALUE (t2)) /* T2 is EMPTY, T1 is not */
829 return !exact;
831 /* Neither set is ... or EMPTY, make sure each part of T2 is in T1.
832 Count how many we find, to determine exactness. For exact matching and
833 ordered T1, T2, this is an O(n) operation, otherwise its worst case is
834 O(nm). */
835 for (base = t1; t2 != NULL_TREE; t2 = TREE_CHAIN (t2))
837 for (probe = base; probe != NULL_TREE; probe = TREE_CHAIN (probe))
839 tree a = TREE_VALUE (probe);
840 tree b = TREE_VALUE (t2);
842 if (comp_except_types (a, b, exact))
844 if (probe == base && exact)
845 base = TREE_CHAIN (probe);
846 length++;
847 break;
850 if (probe == NULL_TREE)
851 return 0;
853 return !exact || base == NULL_TREE || length == list_length (t1);
856 /* Compare the array types T1 and T2, using CMP as the type comparison
857 function for the element types. STRICT is as for comptypes. */
859 static int
860 comp_array_types (cmp, t1, t2, strict)
861 register int (*cmp) PROTO((tree, tree, int));
862 tree t1, t2;
863 int strict;
865 tree d1;
866 tree d2;
868 if (t1 == t2)
869 return 1;
871 /* The type of the array elements must be the same. */
872 if (!(TREE_TYPE (t1) == TREE_TYPE (t2)
873 || (*cmp) (TREE_TYPE (t1), TREE_TYPE (t2),
874 strict & ~COMPARE_REDECLARATION)))
875 return 0;
877 d1 = TYPE_DOMAIN (t1);
878 d2 = TYPE_DOMAIN (t2);
880 if (d1 == d2)
881 return 1;
883 /* If one of the arrays is dimensionless, and the other has a
884 dimension, they are of different types. However, it is legal to
885 write:
887 extern int a[];
888 int a[3];
890 by [basic.link]:
892 declarations for an array object can specify
893 array types that differ by the presence or absence of a major
894 array bound (_dcl.array_). */
895 if (!d1 || !d2)
896 return strict & COMPARE_REDECLARATION;
898 /* Check that the dimensions are the same. */
899 return (cp_tree_equal (TYPE_MIN_VALUE (d1),
900 TYPE_MIN_VALUE (d2))
901 && cp_tree_equal (TYPE_MAX_VALUE (d1),
902 TYPE_MAX_VALUE (d2)));
905 /* Return 1 if T1 and T2 are compatible types for assignment or
906 various other operations. STRICT is a bitwise-or of the COMPARE_*
907 flags. */
910 comptypes (t1, t2, strict)
911 tree t1;
912 tree t2;
913 int strict;
915 int attrval, val;
916 int orig_strict = strict;
918 /* The special exemption for redeclaring array types without an
919 array bound only applies at the top level:
921 extern int (*i)[];
922 int (*i)[8];
924 is not legal, for example. */
925 strict &= ~COMPARE_REDECLARATION;
927 /* Suppress errors caused by previously reported errors */
928 if (t1 == t2)
929 return 1;
931 /* This should never happen. */
932 my_friendly_assert (t1 != error_mark_node, 307);
934 if (t2 == error_mark_node)
935 return 0;
937 if (strict & COMPARE_RELAXED)
939 /* Treat an enum type as the unsigned integer type of the same width. */
941 if (TREE_CODE (t1) == ENUMERAL_TYPE)
942 t1 = type_for_size (TYPE_PRECISION (t1), 1);
943 if (TREE_CODE (t2) == ENUMERAL_TYPE)
944 t2 = type_for_size (TYPE_PRECISION (t2), 1);
946 if (t1 == t2)
947 return 1;
950 if (TYPE_PTRMEMFUNC_P (t1))
951 t1 = TYPE_PTRMEMFUNC_FN_TYPE (t1);
952 if (TYPE_PTRMEMFUNC_P (t2))
953 t2 = TYPE_PTRMEMFUNC_FN_TYPE (t2);
955 /* Different classes of types can't be compatible. */
956 if (TREE_CODE (t1) != TREE_CODE (t2))
957 return 0;
959 /* Qualifiers must match. */
960 if (CP_TYPE_QUALS (t1) != CP_TYPE_QUALS (t2))
961 return 0;
962 if (strict == COMPARE_STRICT
963 && TYPE_FOR_JAVA (t1) != TYPE_FOR_JAVA (t2))
964 return 0;
966 /* Allow for two different type nodes which have essentially the same
967 definition. Note that we already checked for equality of the type
968 qualifiers (just above). */
970 if (TYPE_MAIN_VARIANT (t1) == TYPE_MAIN_VARIANT (t2))
971 return 1;
973 /* ??? COMP_TYPE_ATTRIBUTES is currently useless for variables as each
974 attribute is its own main variant (`val' will remain 0). */
975 #ifndef COMP_TYPE_ATTRIBUTES
976 #define COMP_TYPE_ATTRIBUTES(t1,t2) 1
977 #endif
979 if (strict & COMPARE_NO_ATTRIBUTES)
980 attrval = 1;
981 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
982 else if (! (attrval = COMP_TYPE_ATTRIBUTES (t1, t2)))
983 return 0;
985 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
986 val = 0;
988 switch (TREE_CODE (t1))
990 case TEMPLATE_TEMPLATE_PARM:
991 if (TEMPLATE_TYPE_IDX (t1) != TEMPLATE_TYPE_IDX (t2)
992 || TEMPLATE_TYPE_LEVEL (t1) != TEMPLATE_TYPE_LEVEL (t2))
993 return 0;
994 if (! comp_template_parms (DECL_TEMPLATE_PARMS (TYPE_NAME (t1)),
995 DECL_TEMPLATE_PARMS (TYPE_NAME (t2))))
996 return 0;
997 if (!TEMPLATE_TEMPLATE_PARM_TEMPLATE_INFO (t1)
998 && ! TEMPLATE_TEMPLATE_PARM_TEMPLATE_INFO (t2))
999 return 1;
1000 /* Don't check inheritance. */
1001 strict = COMPARE_STRICT;
1002 /* fall through */
1004 case RECORD_TYPE:
1005 case UNION_TYPE:
1006 if (TYPE_TEMPLATE_INFO (t1) && TYPE_TEMPLATE_INFO (t2)
1007 && (TYPE_TI_TEMPLATE (t1) == TYPE_TI_TEMPLATE (t2)
1008 || TREE_CODE (t1) == TEMPLATE_TEMPLATE_PARM))
1009 val = comp_template_args (TYPE_TI_ARGS (t1),
1010 TYPE_TI_ARGS (t2));
1011 look_hard:
1012 if ((strict & COMPARE_BASE) && DERIVED_FROM_P (t1, t2))
1013 val = 1;
1014 else if ((strict & COMPARE_RELAXED) && DERIVED_FROM_P (t2, t1))
1015 val = 1;
1016 break;
1018 case OFFSET_TYPE:
1019 val = (comptypes (build_pointer_type (TYPE_OFFSET_BASETYPE (t1)),
1020 build_pointer_type (TYPE_OFFSET_BASETYPE (t2)), strict)
1021 && comptypes (TREE_TYPE (t1), TREE_TYPE (t2), strict));
1022 break;
1024 case METHOD_TYPE:
1025 if (! comp_except_specs (TYPE_RAISES_EXCEPTIONS (t1),
1026 TYPE_RAISES_EXCEPTIONS (t2), 1))
1027 return 0;
1029 /* This case is anti-symmetrical!
1030 One can pass a base member (or member function)
1031 to something expecting a derived member (or member function),
1032 but not vice-versa! */
1034 val = (comptypes (TREE_TYPE (t1), TREE_TYPE (t2), strict)
1035 && compparms (TYPE_ARG_TYPES (t1), TYPE_ARG_TYPES (t2)));
1036 break;
1038 case POINTER_TYPE:
1039 case REFERENCE_TYPE:
1040 t1 = TREE_TYPE (t1);
1041 t2 = TREE_TYPE (t2);
1042 /* first, check whether the referred types match with the
1043 required level of strictness */
1044 val = comptypes (t1, t2, strict);
1045 if (val)
1046 break;
1047 if (TREE_CODE (t1) == RECORD_TYPE
1048 && TREE_CODE (t2) == RECORD_TYPE)
1049 goto look_hard;
1050 break;
1052 case FUNCTION_TYPE:
1053 if (! comp_except_specs (TYPE_RAISES_EXCEPTIONS (t1),
1054 TYPE_RAISES_EXCEPTIONS (t2), 1))
1055 return 0;
1057 val = ((TREE_TYPE (t1) == TREE_TYPE (t2)
1058 || comptypes (TREE_TYPE (t1), TREE_TYPE (t2), strict))
1059 && compparms (TYPE_ARG_TYPES (t1), TYPE_ARG_TYPES (t2)));
1060 break;
1062 case ARRAY_TYPE:
1063 /* Target types must match incl. qualifiers. We use ORIG_STRICT
1064 here since this is the one place where
1065 COMPARE_REDECLARATION should be used. */
1066 val = comp_array_types (comptypes, t1, t2, orig_strict);
1067 break;
1069 case TEMPLATE_TYPE_PARM:
1070 return TEMPLATE_TYPE_IDX (t1) == TEMPLATE_TYPE_IDX (t2)
1071 && TEMPLATE_TYPE_LEVEL (t1) == TEMPLATE_TYPE_LEVEL (t2);
1073 case TYPENAME_TYPE:
1074 if (TYPE_IDENTIFIER (t1) != TYPE_IDENTIFIER (t2))
1075 return 0;
1076 return same_type_p (TYPE_CONTEXT (t1), TYPE_CONTEXT (t2));
1078 case COMPLEX_TYPE:
1079 return same_type_p (TREE_TYPE (t1), TREE_TYPE (t2));
1081 default:
1082 break;
1084 return attrval == 2 && val == 1 ? 2 : val;
1087 /* Subroutine of comp_target-types. Make sure that the cv-quals change
1088 only in the same direction as the target type. */
1090 static int
1091 comp_cv_target_types (ttl, ttr, nptrs)
1092 tree ttl, ttr;
1093 int nptrs;
1095 int t;
1097 if (!at_least_as_qualified_p (ttl, ttr)
1098 && !at_least_as_qualified_p (ttr, ttl))
1099 /* The qualifications are incomparable. */
1100 return 0;
1102 if (TYPE_MAIN_VARIANT (ttl) == TYPE_MAIN_VARIANT (ttr))
1103 return more_qualified_p (ttr, ttl) ? -1 : 1;
1105 t = comp_target_types (ttl, ttr, nptrs);
1106 if ((t == 1 && at_least_as_qualified_p (ttl, ttr))
1107 || (t == -1 && at_least_as_qualified_p (ttr, ttl)))
1108 return t;
1110 return 0;
1113 /* Return 1 or -1 if TTL and TTR are pointers to types that are equivalent,
1114 ignoring their qualifiers, 0 if not. Return 1 means that TTR can be
1115 converted to TTL. Return -1 means that TTL can be converted to TTR but
1116 not vice versa.
1118 NPTRS is the number of pointers we can strip off and keep cool.
1119 This is used to permit (for aggr A, aggr B) A, B* to convert to A*,
1120 but to not permit B** to convert to A**.
1122 This should go away. Callers should use can_convert or something
1123 similar instead. (jason 17 Apr 1997) */
1126 comp_target_types (ttl, ttr, nptrs)
1127 tree ttl, ttr;
1128 int nptrs;
1130 ttl = TYPE_MAIN_VARIANT (ttl);
1131 ttr = TYPE_MAIN_VARIANT (ttr);
1132 if (same_type_p (ttl, ttr))
1133 return 1;
1135 if (TREE_CODE (ttr) != TREE_CODE (ttl))
1136 return 0;
1138 if ((TREE_CODE (ttr) == POINTER_TYPE
1139 || TREE_CODE (ttr) == REFERENCE_TYPE)
1140 /* If we get a pointer with nptrs == 0, we don't allow any tweaking
1141 of the type pointed to. This is necessary for reference init
1142 semantics. We won't get here from a previous call with nptrs == 1;
1143 for multi-level pointers we end up in comp_ptr_ttypes. */
1144 && nptrs > 0)
1146 int is_ptr = TREE_CODE (ttr) == POINTER_TYPE;
1148 ttl = TREE_TYPE (ttl);
1149 ttr = TREE_TYPE (ttr);
1151 if (is_ptr)
1153 if (TREE_CODE (ttl) == UNKNOWN_TYPE
1154 || TREE_CODE (ttr) == UNKNOWN_TYPE)
1155 return 1;
1156 else if (TREE_CODE (ttl) == VOID_TYPE
1157 && TREE_CODE (ttr) != FUNCTION_TYPE
1158 && TREE_CODE (ttr) != METHOD_TYPE
1159 && TREE_CODE (ttr) != OFFSET_TYPE)
1160 return 1;
1161 else if (TREE_CODE (ttr) == VOID_TYPE
1162 && TREE_CODE (ttl) != FUNCTION_TYPE
1163 && TREE_CODE (ttl) != METHOD_TYPE
1164 && TREE_CODE (ttl) != OFFSET_TYPE)
1165 return -1;
1166 else if (TREE_CODE (ttl) == POINTER_TYPE
1167 || TREE_CODE (ttl) == ARRAY_TYPE)
1169 if (comp_ptr_ttypes (ttl, ttr))
1170 return 1;
1171 else if (comp_ptr_ttypes (ttr, ttl))
1172 return -1;
1173 return 0;
1177 /* Const and volatile mean something different for function types,
1178 so the usual checks are not appropriate. */
1179 if (TREE_CODE (ttl) == FUNCTION_TYPE || TREE_CODE (ttl) == METHOD_TYPE)
1180 return comp_target_types (ttl, ttr, nptrs - 1);
1182 return comp_cv_target_types (ttl, ttr, nptrs - 1);
1185 if (TREE_CODE (ttr) == ARRAY_TYPE)
1186 return comp_array_types (comp_target_types, ttl, ttr, COMPARE_STRICT);
1187 else if (TREE_CODE (ttr) == FUNCTION_TYPE || TREE_CODE (ttr) == METHOD_TYPE)
1189 tree argsl, argsr;
1190 int saw_contra = 0;
1192 if (pedantic)
1194 if (!same_type_p (TREE_TYPE (ttl), TREE_TYPE (ttr)))
1195 return 0;
1197 else
1199 switch (comp_target_types (TREE_TYPE (ttl), TREE_TYPE (ttr), -1))
1201 case 0:
1202 return 0;
1203 case -1:
1204 saw_contra = 1;
1208 argsl = TYPE_ARG_TYPES (ttl);
1209 argsr = TYPE_ARG_TYPES (ttr);
1211 /* Compare 'this' here, not in comp_target_parms. */
1212 if (TREE_CODE (ttr) == METHOD_TYPE)
1214 tree tl = TYPE_METHOD_BASETYPE (ttl);
1215 tree tr = TYPE_METHOD_BASETYPE (ttr);
1217 if (!same_or_base_type_p (tr, tl))
1219 if (same_or_base_type_p (tl, tr))
1220 saw_contra = 1;
1221 else
1222 return 0;
1225 argsl = TREE_CHAIN (argsl);
1226 argsr = TREE_CHAIN (argsr);
1229 switch (comp_target_parms (argsl, argsr, 1))
1231 case 0:
1232 return 0;
1233 case -1:
1234 saw_contra = 1;
1237 return saw_contra ? -1 : 1;
1239 /* for C++ */
1240 else if (TREE_CODE (ttr) == OFFSET_TYPE)
1242 int base;
1244 /* Contravariance: we can assign a pointer to base member to a pointer
1245 to derived member. Note difference from simple pointer case, where
1246 we can pass a pointer to derived to a pointer to base. */
1247 if (same_or_base_type_p (TYPE_OFFSET_BASETYPE (ttr),
1248 TYPE_OFFSET_BASETYPE (ttl)))
1249 base = 1;
1250 else if (same_or_base_type_p (TYPE_OFFSET_BASETYPE (ttl),
1251 TYPE_OFFSET_BASETYPE (ttr)))
1253 tree tmp = ttl;
1254 ttl = ttr;
1255 ttr = tmp;
1256 base = -1;
1258 else
1259 return 0;
1261 ttl = TREE_TYPE (ttl);
1262 ttr = TREE_TYPE (ttr);
1264 if (TREE_CODE (ttl) == POINTER_TYPE
1265 || TREE_CODE (ttl) == ARRAY_TYPE)
1267 if (comp_ptr_ttypes (ttl, ttr))
1268 return base;
1269 return 0;
1271 else
1273 if (comp_cv_target_types (ttl, ttr, nptrs) == 1)
1274 return base;
1275 return 0;
1278 else if (IS_AGGR_TYPE (ttl))
1280 if (nptrs < 0)
1281 return 0;
1282 if (same_or_base_type_p (build_pointer_type (ttl),
1283 build_pointer_type (ttr)))
1284 return 1;
1285 if (same_or_base_type_p (build_pointer_type (ttr),
1286 build_pointer_type (ttl)))
1287 return -1;
1288 return 0;
1291 return 0;
1294 /* Returns 1 if TYPE1 is at least as qualified as TYPE2. */
1297 at_least_as_qualified_p (type1, type2)
1298 tree type1;
1299 tree type2;
1301 /* All qualifiers for TYPE2 must also appear in TYPE1. */
1302 return ((CP_TYPE_QUALS (type1) & CP_TYPE_QUALS (type2))
1303 == CP_TYPE_QUALS (type2));
1306 /* Returns 1 if TYPE1 is more qualified than TYPE2. */
1309 more_qualified_p (type1, type2)
1310 tree type1;
1311 tree type2;
1313 return (CP_TYPE_QUALS (type1) != CP_TYPE_QUALS (type2)
1314 && at_least_as_qualified_p (type1, type2));
1317 /* Returns 1 if TYPE1 is more cv-qualified than TYPE2, -1 if TYPE2 is
1318 more cv-qualified that TYPE1, and 0 otherwise. */
1321 comp_cv_qualification (type1, type2)
1322 tree type1;
1323 tree type2;
1325 if (CP_TYPE_QUALS (type1) == CP_TYPE_QUALS (type2))
1326 return 0;
1328 if (at_least_as_qualified_p (type1, type2))
1329 return 1;
1331 else if (at_least_as_qualified_p (type2, type1))
1332 return -1;
1334 return 0;
1337 /* Returns 1 if the cv-qualification signature of TYPE1 is a proper
1338 subset of the cv-qualification signature of TYPE2, and the types
1339 are similar. Returns -1 if the other way 'round, and 0 otherwise. */
1342 comp_cv_qual_signature (type1, type2)
1343 tree type1;
1344 tree type2;
1346 if (comp_ptr_ttypes_real (type2, type1, -1))
1347 return 1;
1348 else if (comp_ptr_ttypes_real (type1, type2, -1))
1349 return -1;
1350 else
1351 return 0;
1354 /* If two types share a common base type, return that basetype.
1355 If there is not a unique most-derived base type, this function
1356 returns ERROR_MARK_NODE. */
1358 static tree
1359 common_base_type (tt1, tt2)
1360 tree tt1, tt2;
1362 tree best = NULL_TREE;
1363 int i;
1365 /* If one is a baseclass of another, that's good enough. */
1366 if (UNIQUELY_DERIVED_FROM_P (tt1, tt2))
1367 return tt1;
1368 if (UNIQUELY_DERIVED_FROM_P (tt2, tt1))
1369 return tt2;
1371 /* Otherwise, try to find a unique baseclass of TT1
1372 that is shared by TT2, and follow that down. */
1373 for (i = CLASSTYPE_N_BASECLASSES (tt1)-1; i >= 0; i--)
1375 tree basetype = TYPE_BINFO_BASETYPE (tt1, i);
1376 tree trial = common_base_type (basetype, tt2);
1377 if (trial)
1379 if (trial == error_mark_node)
1380 return trial;
1381 if (best == NULL_TREE)
1382 best = trial;
1383 else if (best != trial)
1384 return error_mark_node;
1388 /* Same for TT2. */
1389 for (i = CLASSTYPE_N_BASECLASSES (tt2)-1; i >= 0; i--)
1391 tree basetype = TYPE_BINFO_BASETYPE (tt2, i);
1392 tree trial = common_base_type (tt1, basetype);
1393 if (trial)
1395 if (trial == error_mark_node)
1396 return trial;
1397 if (best == NULL_TREE)
1398 best = trial;
1399 else if (best != trial)
1400 return error_mark_node;
1403 return best;
1406 /* Subroutines of `comptypes'. */
1408 /* Return 1 if two parameter type lists PARMS1 and PARMS2 are
1409 equivalent in the sense that functions with those parameter types
1410 can have equivalent types. The two lists must be equivalent,
1411 element by element.
1413 C++: See comment above about TYPE1, TYPE2. */
1416 compparms (parms1, parms2)
1417 tree parms1, parms2;
1419 register tree t1 = parms1, t2 = parms2;
1421 /* An unspecified parmlist matches any specified parmlist
1422 whose argument types don't need default promotions. */
1424 while (1)
1426 if (t1 == 0 && t2 == 0)
1427 return 1;
1428 /* If one parmlist is shorter than the other,
1429 they fail to match. */
1430 if (t1 == 0 || t2 == 0)
1431 return 0;
1432 if (!same_type_p (TREE_VALUE (t2), TREE_VALUE (t1)))
1433 return 0;
1435 t1 = TREE_CHAIN (t1);
1436 t2 = TREE_CHAIN (t2);
1440 /* This really wants return whether or not parameter type lists
1441 would make their owning functions assignment compatible or not.
1443 The return value is like for comp_target_types.
1445 This should go away, possibly with the exception of the empty parmlist
1446 conversion; there are no conversions between function types in C++.
1447 (jason 17 Apr 1997) */
1449 static int
1450 comp_target_parms (parms1, parms2, strict)
1451 tree parms1, parms2;
1452 int strict;
1454 register tree t1 = parms1, t2 = parms2;
1455 int warn_contravariance = 0;
1457 /* In C, an unspecified parmlist matches any specified parmlist
1458 whose argument types don't need default promotions. This is not
1459 true for C++, but let's do it anyway for unfixed headers. */
1461 if (t1 == 0 && t2 != 0)
1463 if (! flag_strict_prototype && t2 == void_list_node)
1464 /* t1 might be the arglist of a function pointer in extern "C"
1465 declared to take (), which we fudged to (...). Don't make the
1466 user pay for our mistake. */;
1467 else
1468 cp_pedwarn ("ANSI C++ prohibits conversion from `%#T' to `(...)'",
1469 parms2);
1470 return self_promoting_args_p (t2);
1472 if (t2 == 0)
1473 return self_promoting_args_p (t1);
1475 for (; t1 || t2; t1 = TREE_CHAIN (t1), t2 = TREE_CHAIN (t2))
1477 tree p1, p2;
1479 /* If one parmlist is shorter than the other,
1480 they fail to match, unless STRICT is <= 0. */
1481 if (t1 == 0 || t2 == 0)
1483 if (strict > 0)
1484 return 0;
1485 if (strict < 0)
1486 return 1 + warn_contravariance;
1487 return ((t1 && TREE_PURPOSE (t1)) + warn_contravariance);
1489 p1 = TREE_VALUE (t1);
1490 p2 = TREE_VALUE (t2);
1491 if (same_type_p (p1, p2))
1492 continue;
1494 if (pedantic)
1495 return 0;
1497 if ((TREE_CODE (p1) == POINTER_TYPE && TREE_CODE (p2) == POINTER_TYPE)
1498 || (TREE_CODE (p1) == REFERENCE_TYPE
1499 && TREE_CODE (p2) == REFERENCE_TYPE))
1501 if (strict <= 0
1502 && (TYPE_MAIN_VARIANT (TREE_TYPE (p1))
1503 == TYPE_MAIN_VARIANT (TREE_TYPE (p2))))
1504 continue;
1506 /* The following is wrong for contravariance,
1507 but many programs depend on it. */
1508 if (TREE_TYPE (p1) == void_type_node)
1509 continue;
1510 if (TREE_TYPE (p2) == void_type_node)
1512 warn_contravariance = 1;
1513 continue;
1515 if (IS_AGGR_TYPE (TREE_TYPE (p1))
1516 && !same_type_p (TYPE_MAIN_VARIANT (TREE_TYPE (p1)),
1517 TYPE_MAIN_VARIANT (TREE_TYPE (p2))))
1518 return 0;
1520 /* Note backwards order due to contravariance. */
1521 if (comp_target_types (p2, p1, 1) <= 0)
1523 if (comp_target_types (p1, p2, 1) > 0)
1525 warn_contravariance = 1;
1526 continue;
1528 if (strict != 0)
1529 return 0;
1532 return warn_contravariance ? -1 : 1;
1535 /* Compute the value of the `sizeof' operator. */
1537 tree
1538 c_sizeof (type)
1539 tree type;
1541 enum tree_code code = TREE_CODE (type);
1542 tree t;
1544 if (processing_template_decl)
1545 return build_min (SIZEOF_EXPR, sizetype, type);
1547 if (code == FUNCTION_TYPE)
1549 if (pedantic || warn_pointer_arith)
1550 pedwarn ("ANSI C++ forbids taking the sizeof a function type");
1551 return size_int (1);
1553 if (code == METHOD_TYPE)
1555 if (pedantic || warn_pointer_arith)
1556 pedwarn ("ANSI C++ forbids taking the sizeof a method type");
1557 return size_int (1);
1559 if (code == VOID_TYPE)
1561 if (pedantic || warn_pointer_arith)
1562 pedwarn ("ANSI C++ forbids taking the sizeof a void type");
1563 return size_int (1);
1565 if (code == ERROR_MARK)
1566 return size_int (1);
1568 /* ARM $5.3.2: ``When applied to a reference, the result is the size of the
1569 referenced object.'' */
1570 if (code == REFERENCE_TYPE)
1571 type = TREE_TYPE (type);
1573 if (code == OFFSET_TYPE)
1575 cp_error ("`sizeof' applied to non-static member");
1576 return size_int (0);
1579 if (TYPE_SIZE (complete_type (type)) == 0)
1581 cp_error ("`sizeof' applied to incomplete type `%T'", type);
1582 return size_int (0);
1585 /* Convert in case a char is more than one unit. */
1586 t = size_binop (CEIL_DIV_EXPR, TYPE_SIZE (type),
1587 size_int (TYPE_PRECISION (char_type_node)));
1588 t = convert (sizetype, t);
1589 /* size_binop does not put the constant in range, so do it now. */
1590 if (TREE_CODE (t) == INTEGER_CST && force_fit_type (t, 0))
1591 TREE_CONSTANT_OVERFLOW (t) = TREE_OVERFLOW (t) = 1;
1592 return t;
1595 tree
1596 expr_sizeof (e)
1597 tree e;
1599 if (processing_template_decl)
1600 return build_min (SIZEOF_EXPR, sizetype, e);
1602 if (TREE_CODE (e) == COMPONENT_REF
1603 && DECL_C_BIT_FIELD (TREE_OPERAND (e, 1)))
1604 error ("sizeof applied to a bit-field");
1605 if (is_overloaded_fn (e))
1607 pedwarn ("ANSI C++ forbids taking the sizeof a function type");
1608 return size_int (1);
1610 else if (type_unknown_p (e))
1612 incomplete_type_error (e, TREE_TYPE (e));
1613 return size_int (1);
1615 /* It's illegal to say `sizeof (X::i)' for `i' a non-static data
1616 member unless you're in a non-static member of X. So hand off to
1617 resolve_offset_ref. [expr.prim] */
1618 else if (TREE_CODE (e) == OFFSET_REF)
1619 e = resolve_offset_ref (e);
1621 if (e == error_mark_node)
1622 return e;
1624 return c_sizeof (TREE_TYPE (e));
1627 tree
1628 c_sizeof_nowarn (type)
1629 tree type;
1631 enum tree_code code = TREE_CODE (type);
1632 tree t;
1634 if (code == FUNCTION_TYPE
1635 || code == METHOD_TYPE
1636 || code == VOID_TYPE
1637 || code == ERROR_MARK)
1638 return size_int (1);
1639 if (code == REFERENCE_TYPE)
1640 type = TREE_TYPE (type);
1642 if (TYPE_SIZE (type) == 0)
1643 return size_int (0);
1645 /* Convert in case a char is more than one unit. */
1646 t = size_binop (CEIL_DIV_EXPR, TYPE_SIZE (type),
1647 size_int (TYPE_PRECISION (char_type_node)));
1648 t = convert (sizetype, t);
1649 force_fit_type (t, 0);
1650 return t;
1653 /* Implement the __alignof keyword: Return the minimum required
1654 alignment of TYPE, measured in bytes. */
1656 tree
1657 c_alignof (type)
1658 tree type;
1660 enum tree_code code = TREE_CODE (type);
1661 tree t;
1663 if (processing_template_decl)
1664 return build_min (ALIGNOF_EXPR, sizetype, type);
1666 if (code == FUNCTION_TYPE || code == METHOD_TYPE)
1667 return size_int (FUNCTION_BOUNDARY / BITS_PER_UNIT);
1669 if (code == VOID_TYPE || code == ERROR_MARK)
1670 return size_int (1);
1672 /* C++: this is really correct! */
1673 if (code == REFERENCE_TYPE)
1674 type = TREE_TYPE (type);
1676 t = size_int (TYPE_ALIGN (type) / BITS_PER_UNIT);
1677 force_fit_type (t, 0);
1678 return t;
1681 /* Perform the array-to-pointer and function-to-pointer conversions
1682 for EXP.
1684 In addition, references are converted to rvalues and manifest
1685 constants are replaced by their values. */
1687 tree
1688 decay_conversion (exp)
1689 tree exp;
1691 register tree type;
1692 register enum tree_code code;
1694 if (TREE_CODE (exp) == OFFSET_REF)
1695 exp = resolve_offset_ref (exp);
1697 type = TREE_TYPE (exp);
1698 code = TREE_CODE (type);
1700 if (code == REFERENCE_TYPE)
1702 exp = convert_from_reference (exp);
1703 type = TREE_TYPE (exp);
1704 code = TREE_CODE (type);
1707 /* Constants can be used directly unless they're not loadable. */
1708 if (TREE_CODE (exp) == CONST_DECL)
1709 exp = DECL_INITIAL (exp);
1710 /* Replace a nonvolatile const static variable with its value. We
1711 don't do this for arrays, though; we want the address of the
1712 first element of the array, not the address of the first element
1713 of its initializing constant. We *do* replace variables that the
1714 user isn't really supposed to know about; this is a hack to deal
1715 with __PRETTY_FUNCTION__ and the like. */
1716 else if (TREE_READONLY_DECL_P (exp)
1717 && (code != ARRAY_TYPE
1718 || (TREE_CODE (exp) == VAR_DECL && DECL_IGNORED_P (exp))))
1720 exp = decl_constant_value (exp);
1721 type = TREE_TYPE (exp);
1724 /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
1725 Leave such NOP_EXPRs, since RHS is being used in non-lvalue context. */
1727 if (code == VOID_TYPE)
1729 error ("void value not ignored as it ought to be");
1730 return error_mark_node;
1732 if (code == METHOD_TYPE)
1733 my_friendly_abort (990506);
1734 if (code == FUNCTION_TYPE || is_overloaded_fn (exp))
1735 return build_unary_op (ADDR_EXPR, exp, 0);
1736 if (code == ARRAY_TYPE)
1738 register tree adr;
1739 tree ptrtype;
1741 if (TREE_CODE (exp) == INDIRECT_REF)
1743 /* Stripping away the INDIRECT_REF is not the right
1744 thing to do for references... */
1745 tree inner = TREE_OPERAND (exp, 0);
1746 if (TREE_CODE (TREE_TYPE (inner)) == REFERENCE_TYPE)
1748 inner = build1 (CONVERT_EXPR,
1749 build_pointer_type (TREE_TYPE
1750 (TREE_TYPE (inner))),
1751 inner);
1752 TREE_CONSTANT (inner) = TREE_CONSTANT (TREE_OPERAND (inner, 0));
1754 return cp_convert (build_pointer_type (TREE_TYPE (type)), inner);
1757 if (TREE_CODE (exp) == COMPOUND_EXPR)
1759 tree op1 = decay_conversion (TREE_OPERAND (exp, 1));
1760 return build (COMPOUND_EXPR, TREE_TYPE (op1),
1761 TREE_OPERAND (exp, 0), op1);
1764 if (!lvalue_p (exp)
1765 && ! (TREE_CODE (exp) == CONSTRUCTOR && TREE_STATIC (exp)))
1767 error ("invalid use of non-lvalue array");
1768 return error_mark_node;
1771 ptrtype = build_pointer_type (TREE_TYPE (type));
1773 if (TREE_CODE (exp) == VAR_DECL)
1775 /* ??? This is not really quite correct
1776 in that the type of the operand of ADDR_EXPR
1777 is not the target type of the type of the ADDR_EXPR itself.
1778 Question is, can this lossage be avoided? */
1779 adr = build1 (ADDR_EXPR, ptrtype, exp);
1780 if (mark_addressable (exp) == 0)
1781 return error_mark_node;
1782 TREE_CONSTANT (adr) = staticp (exp);
1783 TREE_SIDE_EFFECTS (adr) = 0; /* Default would be, same as EXP. */
1784 return adr;
1786 /* This way is better for a COMPONENT_REF since it can
1787 simplify the offset for a component. */
1788 adr = build_unary_op (ADDR_EXPR, exp, 1);
1789 return cp_convert (ptrtype, adr);
1792 /* [basic.lval]: Class rvalues can have cv-qualified types; non-class
1793 rvalues always have cv-unqualified types. */
1794 if (! CLASS_TYPE_P (type))
1795 exp = cp_convert (TYPE_MAIN_VARIANT (type), exp);
1797 return exp;
1800 tree
1801 default_conversion (exp)
1802 tree exp;
1804 tree type;
1805 enum tree_code code;
1807 exp = decay_conversion (exp);
1809 type = TREE_TYPE (exp);
1810 code = TREE_CODE (type);
1812 if (INTEGRAL_CODE_P (code))
1814 tree t = type_promotes_to (type);
1815 if (t != type)
1816 return cp_convert (t, exp);
1819 return exp;
1822 /* Take the address of an inline function without setting TREE_ADDRESSABLE
1823 or TREE_USED. */
1825 tree
1826 inline_conversion (exp)
1827 tree exp;
1829 if (TREE_CODE (exp) == FUNCTION_DECL)
1830 exp = build1 (ADDR_EXPR, build_pointer_type (TREE_TYPE (exp)), exp);
1832 return exp;
1835 /* Returns nonzero iff exp is a STRING_CST or the result of applying
1836 decay_conversion to one. */
1839 string_conv_p (totype, exp, warn)
1840 tree totype, exp;
1841 int warn;
1843 tree t;
1845 if (! flag_const_strings || TREE_CODE (totype) != POINTER_TYPE)
1846 return 0;
1848 t = TREE_TYPE (totype);
1849 if (!same_type_p (t, char_type_node)
1850 && !same_type_p (t, wchar_type_node))
1851 return 0;
1853 if (TREE_CODE (exp) == STRING_CST)
1855 /* Make sure that we don't try to convert between char and wchar_t. */
1856 if (!same_type_p (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (exp))), t))
1857 return 0;
1859 else
1861 /* Is this a string constant which has decayed to 'const char *'? */
1862 t = build_pointer_type (build_qualified_type (t, TYPE_QUAL_CONST));
1863 if (!same_type_p (TREE_TYPE (exp), t))
1864 return 0;
1865 STRIP_NOPS (exp);
1866 if (TREE_CODE (exp) != ADDR_EXPR
1867 || TREE_CODE (TREE_OPERAND (exp, 0)) != STRING_CST)
1868 return 0;
1871 /* This warning is not very useful, as it complains about printf. */
1872 if (warn && warn_write_strings)
1873 cp_warning ("deprecated conversion from string constant to `%T'", totype);
1875 return 1;
1878 tree
1879 build_object_ref (datum, basetype, field)
1880 tree datum, basetype, field;
1882 tree dtype;
1883 if (datum == error_mark_node)
1884 return error_mark_node;
1886 dtype = TREE_TYPE (datum);
1887 if (TREE_CODE (dtype) == REFERENCE_TYPE)
1888 dtype = TREE_TYPE (dtype);
1889 if (! IS_AGGR_TYPE_CODE (TREE_CODE (dtype)))
1891 cp_error ("request for member `%T::%D' in expression of non-aggregate type `%T'",
1892 basetype, field, dtype);
1893 return error_mark_node;
1895 else if (is_aggr_type (basetype, 1))
1897 tree binfo = binfo_or_else (basetype, dtype);
1898 if (binfo)
1899 return build_x_component_ref (build_scoped_ref (datum, basetype),
1900 field, binfo, 1);
1902 return error_mark_node;
1905 /* Like `build_component_ref, but uses an already found field, and converts
1906 from a reference. Must compute access for current_class_ref.
1907 Otherwise, ok. */
1909 tree
1910 build_component_ref_1 (datum, field, protect)
1911 tree datum, field;
1912 int protect;
1914 return convert_from_reference
1915 (build_component_ref (datum, field, NULL_TREE, protect));
1918 /* Given a COND_EXPR, MIN_EXPR, or MAX_EXPR in T, return it in a form that we
1919 can, for example, use as an lvalue. This code used to be in
1920 unary_complex_lvalue, but we needed it to deal with `a = (d == c) ? b : c'
1921 expressions, where we're dealing with aggregates. But now it's again only
1922 called from unary_complex_lvalue. The case (in particular) that led to
1923 this was with CODE == ADDR_EXPR, since it's not an lvalue when we'd
1924 get it there. */
1926 static tree
1927 rationalize_conditional_expr (code, t)
1928 enum tree_code code;
1929 tree t;
1931 /* For MIN_EXPR or MAX_EXPR, fold-const.c has arranged things so that
1932 the first operand is always the one to be used if both operands
1933 are equal, so we know what conditional expression this used to be. */
1934 if (TREE_CODE (t) == MIN_EXPR || TREE_CODE (t) == MAX_EXPR)
1936 return
1937 build_conditional_expr (build_x_binary_op ((TREE_CODE (t) == MIN_EXPR
1938 ? LE_EXPR : GE_EXPR),
1939 TREE_OPERAND (t, 0),
1940 TREE_OPERAND (t, 1)),
1941 build_unary_op (code, TREE_OPERAND (t, 0), 0),
1942 build_unary_op (code, TREE_OPERAND (t, 1), 0));
1945 return
1946 build_conditional_expr (TREE_OPERAND (t, 0),
1947 build_unary_op (code, TREE_OPERAND (t, 1), 0),
1948 build_unary_op (code, TREE_OPERAND (t, 2), 0));
1951 /* Given the TYPE of an anonymous union field inside T, return the
1952 FIELD_DECL for the field. If not found return NULL_TREE. Because
1953 anonymous unions can nest, we must also search all anonymous unions
1954 that are directly reachable. */
1956 static tree
1957 lookup_anon_field (t, type)
1958 tree t, type;
1960 tree field;
1962 for (field = TYPE_FIELDS (t); field; field = TREE_CHAIN (field))
1964 if (TREE_STATIC (field))
1965 continue;
1966 if (TREE_CODE (field) != FIELD_DECL)
1967 continue;
1969 /* If we find it directly, return the field. */
1970 if (DECL_NAME (field) == NULL_TREE
1971 && type == TREE_TYPE (field))
1973 return field;
1976 /* Otherwise, it could be nested, search harder. */
1977 if (DECL_NAME (field) == NULL_TREE
1978 && ANON_AGGR_TYPE_P (TREE_TYPE (field)))
1980 tree subfield = lookup_anon_field (TREE_TYPE (field), type);
1981 if (subfield)
1982 return subfield;
1985 return NULL_TREE;
1988 /* Build a COMPONENT_REF for a given DATUM, and it's member COMPONENT.
1989 COMPONENT can be an IDENTIFIER_NODE that is the name of the member
1990 that we are interested in, or it can be a FIELD_DECL. */
1992 tree
1993 build_component_ref (datum, component, basetype_path, protect)
1994 tree datum, component, basetype_path;
1995 int protect;
1997 register tree basetype;
1998 register enum tree_code code;
1999 register tree field = NULL;
2000 register tree ref;
2001 tree field_type;
2002 int type_quals;
2004 if (processing_template_decl)
2005 return build_min_nt (COMPONENT_REF, datum, component);
2007 if (datum == error_mark_node
2008 || TREE_TYPE (datum) == error_mark_node)
2009 return error_mark_node;
2011 /* BASETYPE holds the type of the class containing the COMPONENT. */
2012 basetype = TYPE_MAIN_VARIANT (TREE_TYPE (datum));
2014 /* If DATUM is a COMPOUND_EXPR or COND_EXPR, move our reference
2015 inside it. */
2016 switch (TREE_CODE (datum))
2018 case COMPOUND_EXPR:
2020 tree value = build_component_ref (TREE_OPERAND (datum, 1), component,
2021 basetype_path, protect);
2022 return build (COMPOUND_EXPR, TREE_TYPE (value),
2023 TREE_OPERAND (datum, 0), value);
2025 case COND_EXPR:
2026 return build_conditional_expr
2027 (TREE_OPERAND (datum, 0),
2028 build_component_ref (TREE_OPERAND (datum, 1), component,
2029 basetype_path, protect),
2030 build_component_ref (TREE_OPERAND (datum, 2), component,
2031 basetype_path, protect));
2033 case TEMPLATE_DECL:
2034 cp_error ("invalid use of %D", datum);
2035 datum = error_mark_node;
2036 break;
2038 default:
2039 break;
2042 code = TREE_CODE (basetype);
2044 if (code == REFERENCE_TYPE)
2046 datum = convert_from_reference (datum);
2047 basetype = TYPE_MAIN_VARIANT (TREE_TYPE (datum));
2048 code = TREE_CODE (basetype);
2050 if (TREE_CODE (datum) == OFFSET_REF)
2052 datum = resolve_offset_ref (datum);
2053 basetype = TYPE_MAIN_VARIANT (TREE_TYPE (datum));
2054 code = TREE_CODE (basetype);
2057 /* First, see if there is a field or component with name COMPONENT. */
2058 if (TREE_CODE (component) == TREE_LIST)
2060 /* I could not trigger this code. MvL */
2061 my_friendly_abort (980326);
2062 #ifdef DEAD
2063 my_friendly_assert (!(TREE_CHAIN (component) == NULL_TREE
2064 && DECL_CHAIN (TREE_VALUE (component)) == NULL_TREE), 309);
2065 #endif
2066 return build (COMPONENT_REF, TREE_TYPE (component), datum, component);
2069 if (! IS_AGGR_TYPE_CODE (code))
2071 if (code != ERROR_MARK)
2072 cp_error ("request for member `%D' in `%E', which is of non-aggregate type `%T'",
2073 component, datum, basetype);
2074 return error_mark_node;
2077 if (!complete_type_or_else (basetype, datum))
2078 return error_mark_node;
2080 if (TREE_CODE (component) == BIT_NOT_EXPR)
2082 if (TYPE_IDENTIFIER (basetype) != TREE_OPERAND (component, 0))
2084 cp_error ("destructor specifier `%T::~%T' must have matching names",
2085 basetype, TREE_OPERAND (component, 0));
2086 return error_mark_node;
2088 if (! TYPE_HAS_DESTRUCTOR (basetype))
2090 cp_error ("type `%T' has no destructor", basetype);
2091 return error_mark_node;
2093 return TREE_VEC_ELT (CLASSTYPE_METHOD_VEC (basetype), 1);
2096 /* Look up component name in the structure type definition. */
2097 if (TYPE_VFIELD (basetype)
2098 && DECL_NAME (TYPE_VFIELD (basetype)) == component)
2099 /* Special-case this because if we use normal lookups in an ambiguous
2100 hierarchy, the compiler will abort (because vptr lookups are
2101 not supposed to be ambiguous. */
2102 field = TYPE_VFIELD (basetype);
2103 else if (TREE_CODE (component) == FIELD_DECL)
2104 field = component;
2105 else if (TREE_CODE (component) == TYPE_DECL)
2107 cp_error ("invalid use of type decl `%#D' as expression", component);
2108 return error_mark_node;
2110 else if (TREE_CODE (component) == TEMPLATE_DECL)
2112 cp_error ("invalid use of template `%#D' as expression", component);
2113 return error_mark_node;
2115 else
2117 tree name = component;
2118 if (TREE_CODE (component) == VAR_DECL)
2119 name = DECL_NAME (component);
2120 if (TREE_CODE (component) == NAMESPACE_DECL)
2121 /* Source is in error, but produce a sensible diagnostic. */
2122 name = DECL_NAME (component);
2123 if (basetype_path == NULL_TREE)
2124 basetype_path = TYPE_BINFO (basetype);
2125 field = lookup_field (basetype_path, name,
2126 protect && !VFIELD_NAME_P (name), 0);
2127 if (field == error_mark_node)
2128 return error_mark_node;
2130 if (field == NULL_TREE)
2132 /* Not found as a data field, look for it as a method. If found,
2133 then if this is the only possible one, return it, else
2134 report ambiguity error. */
2135 tree fndecls = lookup_fnfields (basetype_path, name, 1);
2136 if (fndecls == error_mark_node)
2137 return error_mark_node;
2138 if (fndecls)
2140 /* If the function is unique and static, we can resolve it
2141 now. Otherwise, we have to wait and see what context it is
2142 used in; a component_ref involving a non-static member
2143 function can only be used in a call (expr.ref). */
2145 if (TREE_CHAIN (fndecls) == NULL_TREE
2146 && TREE_CODE (TREE_VALUE (fndecls)) == FUNCTION_DECL)
2148 if (DECL_STATIC_FUNCTION_P (TREE_VALUE (fndecls)))
2150 tree fndecl = TREE_VALUE (fndecls);
2151 enforce_access (TREE_PURPOSE (fndecls), fndecl);
2152 mark_used (fndecl);
2153 return fndecl;
2155 else
2157 /* A unique non-static member function. Other parts
2158 of the compiler expect something with
2159 unknown_type_node to be really overloaded, so
2160 let's oblige. */
2161 TREE_VALUE (fndecls)
2162 = scratch_ovl_cons (TREE_VALUE (fndecls), NULL_TREE);
2166 ref = build (COMPONENT_REF, unknown_type_node,
2167 datum, TREE_VALUE (fndecls));
2168 return ref;
2171 cp_error ("`%#T' has no member named `%D'", basetype, name);
2172 return error_mark_node;
2174 else if (TREE_TYPE (field) == error_mark_node)
2175 return error_mark_node;
2177 if (TREE_CODE (field) != FIELD_DECL)
2179 if (TREE_CODE (field) == TYPE_DECL)
2180 cp_pedwarn ("invalid use of type decl `%#D' as expression", field);
2181 else if (DECL_RTL (field) != 0)
2182 mark_used (field);
2183 else
2184 TREE_USED (field) = 1;
2185 return field;
2189 /* See if we have to do any conversions so that we pick up the field from the
2190 right context. */
2191 if (DECL_FIELD_CONTEXT (field) != basetype)
2193 tree context = DECL_FIELD_CONTEXT (field);
2194 tree base = context;
2195 while (!same_type_p (base, basetype) && TYPE_NAME (base)
2196 && ANON_AGGR_TYPE_P (base))
2198 base = TYPE_CONTEXT (base);
2201 /* Handle base classes here... */
2202 if (base != basetype && TYPE_USES_COMPLEX_INHERITANCE (basetype))
2204 tree addr = build_unary_op (ADDR_EXPR, datum, 0);
2205 if (integer_zerop (addr))
2207 error ("invalid reference to NULL ptr, use ptr-to-member instead");
2208 return error_mark_node;
2210 if (VBASE_NAME_P (DECL_NAME (field)))
2212 /* It doesn't matter which vbase pointer we grab, just
2213 find one of them. */
2214 tree binfo = get_binfo (base,
2215 TREE_TYPE (TREE_TYPE (addr)), 0);
2216 addr = convert_pointer_to_real (binfo, addr);
2218 else
2219 addr = convert_pointer_to (base, addr);
2220 datum = build_indirect_ref (addr, NULL_PTR);
2221 my_friendly_assert (datum != error_mark_node, 311);
2223 basetype = base;
2225 /* Handle things from anon unions here... */
2226 if (TYPE_NAME (context) && ANON_AGGR_TYPE_P (context))
2228 tree subfield = lookup_anon_field (basetype, context);
2229 tree subdatum = build_component_ref (datum, subfield,
2230 basetype_path, protect);
2231 return build_component_ref (subdatum, field, basetype_path, protect);
2235 /* Compute the type of the field, as described in [expr.ref]. */
2236 type_quals = TYPE_UNQUALIFIED;
2237 field_type = TREE_TYPE (field);
2238 if (TREE_CODE (field_type) == REFERENCE_TYPE)
2239 /* The standard says that the type of the result should be the
2240 type referred to by the reference. But for now, at least, we
2241 do the conversion from reference type later. */
2243 else
2245 type_quals = (CP_TYPE_QUALS (field_type)
2246 | CP_TYPE_QUALS (TREE_TYPE (datum)));
2248 /* A field is const (volatile) if the enclosing object, or the
2249 field itself, is const (volatile). But, a mutable field is
2250 not const, even within a const object. */
2251 if (DECL_LANG_SPECIFIC (field) && DECL_MUTABLE_P (field))
2252 type_quals &= ~TYPE_QUAL_CONST;
2253 field_type = cp_build_qualified_type (field_type, type_quals);
2256 ref = fold (build (COMPONENT_REF, field_type,
2257 break_out_cleanups (datum), field));
2259 /* Mark the expression const or volatile, as appropriate. Even
2260 though we've dealt with the type above, we still have to mark the
2261 expression itself. */
2262 if (type_quals & TYPE_QUAL_CONST)
2263 TREE_READONLY (ref) = 1;
2264 else if (type_quals & TYPE_QUAL_VOLATILE)
2265 TREE_THIS_VOLATILE (ref) = 1;
2267 return ref;
2270 /* Variant of build_component_ref for use in expressions, which should
2271 never have REFERENCE_TYPE. */
2273 tree
2274 build_x_component_ref (datum, component, basetype_path, protect)
2275 tree datum, component, basetype_path;
2276 int protect;
2278 tree t = build_component_ref (datum, component, basetype_path, protect);
2280 if (! processing_template_decl)
2281 t = convert_from_reference (t);
2283 return t;
2286 /* Given an expression PTR for a pointer, return an expression
2287 for the value pointed to.
2288 ERRORSTRING is the name of the operator to appear in error messages.
2290 This function may need to overload OPERATOR_FNNAME.
2291 Must also handle REFERENCE_TYPEs for C++. */
2293 tree
2294 build_x_indirect_ref (ptr, errorstring)
2295 tree ptr;
2296 const char *errorstring;
2298 tree rval;
2300 if (processing_template_decl)
2301 return build_min_nt (INDIRECT_REF, ptr);
2303 rval = build_opfncall (INDIRECT_REF, LOOKUP_NORMAL, ptr, NULL_TREE,
2304 NULL_TREE);
2305 if (rval)
2306 return rval;
2307 return build_indirect_ref (ptr, errorstring);
2310 tree
2311 build_indirect_ref (ptr, errorstring)
2312 tree ptr;
2313 const char *errorstring;
2315 register tree pointer, type;
2317 if (ptr == error_mark_node)
2318 return error_mark_node;
2320 if (ptr == current_class_ptr)
2321 return current_class_ref;
2323 pointer = (TREE_CODE (TREE_TYPE (ptr)) == REFERENCE_TYPE
2324 ? ptr : default_conversion (ptr));
2325 type = TREE_TYPE (pointer);
2327 if (TYPE_PTR_P (type) || TREE_CODE (type) == REFERENCE_TYPE)
2329 /* [expr.unary.op]
2331 If the type of the expression is "pointer to T," the type
2332 of the result is "T."
2334 We must use the canonical variant because certain parts of
2335 the back end, like fold, do pointer comparisons between
2336 types. */
2337 tree t = canonical_type_variant (TREE_TYPE (type));
2339 if (same_type_p (TYPE_MAIN_VARIANT (t), void_type_node))
2341 /* A pointer to incomplete type (other than cv void) can be
2342 dereferenced [expr.unary.op]/1 */
2343 cp_error ("`%T' is not a pointer-to-object type", type);
2344 return error_mark_node;
2346 else if (TREE_CODE (pointer) == ADDR_EXPR
2347 && !flag_volatile
2348 && same_type_p (t, TREE_TYPE (TREE_OPERAND (pointer, 0))))
2349 /* The POINTER was something like `&x'. We simplify `*&x' to
2350 `x'. */
2351 return TREE_OPERAND (pointer, 0);
2352 else
2354 tree ref = build1 (INDIRECT_REF, t, pointer);
2356 /* We *must* set TREE_READONLY when dereferencing a pointer to const,
2357 so that we get the proper error message if the result is used
2358 to assign to. Also, &* is supposed to be a no-op. */
2359 TREE_READONLY (ref) = CP_TYPE_CONST_P (t);
2360 TREE_THIS_VOLATILE (ref) = CP_TYPE_VOLATILE_P (t);
2361 TREE_SIDE_EFFECTS (ref)
2362 = (TREE_THIS_VOLATILE (ref) || TREE_SIDE_EFFECTS (pointer)
2363 || flag_volatile);
2364 return ref;
2367 /* `pointer' won't be an error_mark_node if we were given a
2368 pointer to member, so it's cool to check for this here. */
2369 else if (TYPE_PTRMEM_P (type) || TYPE_PTRMEMFUNC_P (type))
2370 error ("invalid use of `%s' on pointer to member", errorstring);
2371 else if (pointer != error_mark_node)
2373 if (errorstring)
2374 error ("invalid type argument of `%s'", errorstring);
2375 else
2376 error ("invalid type argument");
2378 return error_mark_node;
2381 /* This handles expressions of the form "a[i]", which denotes
2382 an array reference.
2384 This is logically equivalent in C to *(a+i), but we may do it differently.
2385 If A is a variable or a member, we generate a primitive ARRAY_REF.
2386 This avoids forcing the array out of registers, and can work on
2387 arrays that are not lvalues (for example, members of structures returned
2388 by functions).
2390 If INDEX is of some user-defined type, it must be converted to
2391 integer type. Otherwise, to make a compatible PLUS_EXPR, it
2392 will inherit the type of the array, which will be some pointer type. */
2394 tree
2395 build_array_ref (array, idx)
2396 tree array, idx;
2398 if (idx == 0)
2400 error ("subscript missing in array reference");
2401 return error_mark_node;
2404 if (TREE_TYPE (array) == error_mark_node
2405 || TREE_TYPE (idx) == error_mark_node)
2406 return error_mark_node;
2408 if (TREE_CODE (TREE_TYPE (array)) == ARRAY_TYPE
2409 && TREE_CODE (array) != INDIRECT_REF)
2411 tree rval, type;
2413 /* Subscripting with type char is likely to lose
2414 on a machine where chars are signed.
2415 So warn on any machine, but optionally.
2416 Don't warn for unsigned char since that type is safe.
2417 Don't warn for signed char because anyone who uses that
2418 must have done so deliberately. */
2419 if (warn_char_subscripts
2420 && TYPE_MAIN_VARIANT (TREE_TYPE (idx)) == char_type_node)
2421 warning ("array subscript has type `char'");
2423 /* Apply default promotions *after* noticing character types. */
2424 idx = default_conversion (idx);
2426 if (TREE_CODE (TREE_TYPE (idx)) != INTEGER_TYPE)
2428 error ("array subscript is not an integer");
2429 return error_mark_node;
2432 /* An array that is indexed by a non-constant
2433 cannot be stored in a register; we must be able to do
2434 address arithmetic on its address.
2435 Likewise an array of elements of variable size. */
2436 if (TREE_CODE (idx) != INTEGER_CST
2437 || (TYPE_SIZE (TREE_TYPE (TREE_TYPE (array))) != 0
2438 && (TREE_CODE (TYPE_SIZE (TREE_TYPE (TREE_TYPE (array))))
2439 != INTEGER_CST)))
2441 if (mark_addressable (array) == 0)
2442 return error_mark_node;
2444 /* An array that is indexed by a constant value which is not within
2445 the array bounds cannot be stored in a register either; because we
2446 would get a crash in store_bit_field/extract_bit_field when trying
2447 to access a non-existent part of the register. */
2448 if (TREE_CODE (idx) == INTEGER_CST
2449 && TYPE_VALUES (TREE_TYPE (array))
2450 && ! int_fits_type_p (idx, TYPE_VALUES (TREE_TYPE (array))))
2452 if (mark_addressable (array) == 0)
2453 return error_mark_node;
2456 if (pedantic && !lvalue_p (array))
2457 pedwarn ("ANSI C++ forbids subscripting non-lvalue array");
2459 /* Note in C++ it is valid to subscript a `register' array, since
2460 it is valid to take the address of something with that
2461 storage specification. */
2462 if (extra_warnings)
2464 tree foo = array;
2465 while (TREE_CODE (foo) == COMPONENT_REF)
2466 foo = TREE_OPERAND (foo, 0);
2467 if (TREE_CODE (foo) == VAR_DECL && DECL_REGISTER (foo))
2468 warning ("subscripting array declared `register'");
2471 type = TREE_TYPE (TREE_TYPE (array));
2472 rval = build (ARRAY_REF, type, array, idx);
2473 /* Array ref is const/volatile if the array elements are
2474 or if the array is.. */
2475 TREE_READONLY (rval)
2476 |= (CP_TYPE_CONST_P (type) | TREE_READONLY (array));
2477 TREE_SIDE_EFFECTS (rval)
2478 |= (CP_TYPE_VOLATILE_P (type) | TREE_SIDE_EFFECTS (array));
2479 TREE_THIS_VOLATILE (rval)
2480 |= (CP_TYPE_VOLATILE_P (type) | TREE_THIS_VOLATILE (array));
2481 return require_complete_type (fold (rval));
2485 tree ar = default_conversion (array);
2486 tree ind = default_conversion (idx);
2488 /* Put the integer in IND to simplify error checking. */
2489 if (TREE_CODE (TREE_TYPE (ar)) == INTEGER_TYPE)
2491 tree temp = ar;
2492 ar = ind;
2493 ind = temp;
2496 if (ar == error_mark_node)
2497 return ar;
2499 if (TREE_CODE (TREE_TYPE (ar)) != POINTER_TYPE)
2501 error ("subscripted value is neither array nor pointer");
2502 return error_mark_node;
2504 if (TREE_CODE (TREE_TYPE (ind)) != INTEGER_TYPE)
2506 error ("array subscript is not an integer");
2507 return error_mark_node;
2510 return build_indirect_ref (build_binary_op_nodefault (PLUS_EXPR, ar,
2511 ind, PLUS_EXPR),
2512 "array indexing");
2516 /* Build a function call to function FUNCTION with parameters PARAMS.
2517 PARAMS is a list--a chain of TREE_LIST nodes--in which the
2518 TREE_VALUE of each node is a parameter-expression. The PARAMS do
2519 not include any object pointer that may be required. FUNCTION's
2520 data type may be a function type or a pointer-to-function.
2522 For C++: If FUNCTION's data type is a TREE_LIST, then the tree list
2523 is the list of possible methods that FUNCTION could conceivably
2524 be. If the list of methods comes from a class, then it will be
2525 a list of lists (where each element is associated with the class
2526 that produced it), otherwise it will be a simple list (for
2527 functions overloaded in global scope).
2529 In the first case, TREE_VALUE (function) is the head of one of those
2530 lists, and TREE_PURPOSE is the name of the function.
2532 In the second case, TREE_PURPOSE (function) is the function's
2533 name directly.
2535 DECL is the class instance variable, usually CURRENT_CLASS_REF.
2537 When calling a TEMPLATE_DECL, we don't require a complete return
2538 type. */
2540 tree
2541 build_x_function_call (function, params, decl)
2542 tree function, params, decl;
2544 tree type;
2545 tree template_id = NULL_TREE;
2546 int is_method;
2548 if (function == error_mark_node)
2549 return error_mark_node;
2551 if (processing_template_decl)
2552 return build_min_nt (CALL_EXPR, function, params, NULL_TREE);
2554 /* Save explicit template arguments if found */
2555 if (TREE_CODE (function) == TEMPLATE_ID_EXPR)
2557 template_id = function;
2558 function = TREE_OPERAND (function, 0);
2561 type = TREE_TYPE (function);
2563 if (TREE_CODE (type) == OFFSET_TYPE
2564 && TREE_TYPE (type) == unknown_type_node
2565 && TREE_CODE (function) == TREE_LIST
2566 && TREE_CHAIN (function) == NULL_TREE)
2568 /* Undo (Foo:bar)()... */
2569 type = TYPE_OFFSET_BASETYPE (type);
2570 function = TREE_VALUE (function);
2571 my_friendly_assert (TREE_CODE (function) == TREE_LIST, 999);
2572 my_friendly_assert (TREE_CHAIN (function) == NULL_TREE, 999);
2573 function = TREE_VALUE (function);
2574 if (TREE_CODE (function) == OVERLOAD)
2575 function = OVL_FUNCTION (function);
2576 my_friendly_assert (TREE_CODE (function) == FUNCTION_DECL, 999);
2577 function = DECL_NAME (function);
2578 return build_method_call (decl, function, params,
2579 TYPE_BINFO (type), LOOKUP_NORMAL);
2582 if ((TREE_CODE (function) == FUNCTION_DECL
2583 && DECL_STATIC_FUNCTION_P (function))
2584 || (TREE_CODE (function) == TEMPLATE_DECL
2585 && DECL_STATIC_FUNCTION_P (DECL_RESULT (function))))
2586 return build_member_call(DECL_CONTEXT (function),
2587 template_id
2588 ? template_id : DECL_NAME (function),
2589 params);
2591 is_method = ((TREE_CODE (function) == TREE_LIST
2592 && current_class_type != NULL_TREE
2593 && (IDENTIFIER_CLASS_VALUE (TREE_PURPOSE (function))
2594 == function))
2595 || (TREE_CODE (function) == OVERLOAD
2596 && DECL_FUNCTION_MEMBER_P (OVL_CURRENT (function)))
2597 || TREE_CODE (function) == IDENTIFIER_NODE
2598 || TREE_CODE (type) == METHOD_TYPE
2599 || TYPE_PTRMEMFUNC_P (type));
2601 /* A friend template. Make it look like a toplevel declaration. */
2602 if (! is_method && TREE_CODE (function) == TEMPLATE_DECL)
2603 function = scratch_ovl_cons (function, NULL_TREE);
2605 /* Handle methods, friends, and overloaded functions, respectively. */
2606 if (is_method)
2608 tree basetype = NULL_TREE;
2610 if (TREE_CODE (function) == OVERLOAD)
2611 function = OVL_CURRENT (function);
2613 if (TREE_CODE (function) == FUNCTION_DECL
2614 || DECL_FUNCTION_TEMPLATE_P (function))
2616 basetype = DECL_CLASS_CONTEXT (function);
2618 if (DECL_NAME (function))
2619 function = DECL_NAME (function);
2620 else
2621 function = TYPE_IDENTIFIER (DECL_CLASS_CONTEXT (function));
2623 else if (TREE_CODE (function) == TREE_LIST)
2625 my_friendly_assert (TREE_CODE (TREE_VALUE (function))
2626 == FUNCTION_DECL, 312);
2627 basetype = DECL_CLASS_CONTEXT (TREE_VALUE (function));
2628 function = TREE_PURPOSE (function);
2630 else if (TREE_CODE (function) != IDENTIFIER_NODE)
2632 if (TREE_CODE (function) == OFFSET_REF)
2634 if (TREE_OPERAND (function, 0))
2635 decl = TREE_OPERAND (function, 0);
2637 /* Call via a pointer to member function. */
2638 if (decl == NULL_TREE)
2640 error ("pointer to member function called, but not in class scope");
2641 return error_mark_node;
2643 /* What other type of POINTER_TYPE could this be? */
2644 if (TREE_CODE (TREE_TYPE (function)) != POINTER_TYPE
2645 && ! TYPE_PTRMEMFUNC_P (TREE_TYPE (function))
2646 && TREE_CODE (function) != OFFSET_REF)
2647 function = build (OFFSET_REF, TREE_TYPE (type), NULL_TREE,
2648 function);
2649 goto do_x_function;
2652 /* this is an abbreviated method call.
2653 must go through here in case it is a virtual function.
2654 @@ Perhaps this could be optimized. */
2656 if (basetype && (! current_class_type
2657 || ! DERIVED_FROM_P (basetype, current_class_type)))
2658 return build_member_call (basetype, function, params);
2660 if (decl == NULL_TREE)
2662 if (current_class_type == NULL_TREE)
2664 cp_error ("object missing in call to method `%D'", function);
2665 return error_mark_node;
2667 /* Yow: call from a static member function. */
2668 decl = build_dummy_object (current_class_type);
2671 /* Put back explicit template arguments, if any. */
2672 if (template_id)
2673 function = template_id;
2674 return build_method_call (decl, function, params,
2675 NULL_TREE, LOOKUP_NORMAL);
2677 else if (TREE_CODE (function) == COMPONENT_REF
2678 && type == unknown_type_node)
2680 /* Undo what we did in build_component_ref. */
2681 decl = TREE_OPERAND (function, 0);
2682 function = TREE_OPERAND (function, 1);
2683 function = DECL_NAME (OVL_CURRENT (function));
2685 if (template_id)
2687 TREE_OPERAND (template_id, 0) = function;
2688 function = template_id;
2691 return build_method_call (decl, function, params,
2692 NULL_TREE, LOOKUP_NORMAL);
2694 else if (really_overloaded_fn (function))
2696 if (OVL_FUNCTION (function) == NULL_TREE)
2698 cp_error ("function `%D' declared overloaded, but no definitions appear with which to resolve it?!?",
2699 TREE_PURPOSE (function));
2700 return error_mark_node;
2702 else
2704 /* Put back explicit template arguments, if any. */
2705 if (template_id)
2706 function = template_id;
2707 return build_new_function_call (function, params);
2710 else
2711 /* Remove a potential OVERLOAD around it */
2712 function = OVL_CURRENT (function);
2714 do_x_function:
2715 if (TREE_CODE (function) == OFFSET_REF)
2717 /* If the component is a data element (or a virtual function), we play
2718 games here to make things work. */
2719 tree decl_addr;
2721 if (TREE_OPERAND (function, 0))
2722 decl = TREE_OPERAND (function, 0);
2723 else
2724 decl = current_class_ref;
2726 decl_addr = build_unary_op (ADDR_EXPR, decl, 0);
2728 /* Sigh. OFFSET_REFs are being used for too many things.
2729 They're being used both for -> and ->*, and we want to resolve
2730 the -> cases here, but leave the ->*. We could use
2731 resolve_offset_ref for those, too, but it would call
2732 get_member_function_from_ptrfunc and decl_addr wouldn't get
2733 updated properly. Nasty. */
2734 if (TREE_CODE (TREE_OPERAND (function, 1)) == FIELD_DECL)
2735 function = resolve_offset_ref (function);
2736 else
2737 function = TREE_OPERAND (function, 1);
2739 function = get_member_function_from_ptrfunc (&decl_addr, function);
2740 params = tree_cons (NULL_TREE, decl_addr, params);
2741 return build_function_call (function, params);
2744 type = TREE_TYPE (function);
2745 if (type != error_mark_node)
2747 if (TREE_CODE (type) == REFERENCE_TYPE)
2748 type = TREE_TYPE (type);
2750 if (IS_AGGR_TYPE (type))
2751 return build_opfncall (CALL_EXPR, LOOKUP_NORMAL, function, params, NULL_TREE);
2754 if (is_method)
2756 tree fntype = TREE_TYPE (function);
2757 tree ctypeptr = NULL_TREE;
2759 /* Explicitly named method? */
2760 if (TREE_CODE (function) == FUNCTION_DECL)
2761 ctypeptr = build_pointer_type (DECL_CLASS_CONTEXT (function));
2762 /* Expression with ptr-to-method type? It could either be a plain
2763 usage, or it might be a case where the ptr-to-method is being
2764 passed in as an argument. */
2765 else if (TYPE_PTRMEMFUNC_P (fntype))
2767 tree rec = TYPE_METHOD_BASETYPE (TREE_TYPE
2768 (TYPE_PTRMEMFUNC_FN_TYPE (fntype)));
2769 ctypeptr = build_pointer_type (rec);
2771 /* Unexpected node type? */
2772 else
2773 my_friendly_abort (116);
2774 if (decl == NULL_TREE)
2776 if (current_function_decl
2777 && DECL_STATIC_FUNCTION_P (current_function_decl))
2778 error ("invalid call to member function needing `this' in static member function scope");
2779 else
2780 error ("pointer to member function called, but not in class scope");
2781 return error_mark_node;
2783 if (TREE_CODE (TREE_TYPE (decl)) != POINTER_TYPE
2784 && ! TYPE_PTRMEMFUNC_P (TREE_TYPE (decl)))
2786 decl = build_unary_op (ADDR_EXPR, decl, 0);
2787 decl = convert_pointer_to (TREE_TYPE (ctypeptr), decl);
2789 else
2790 decl = build_c_cast (ctypeptr, decl);
2791 params = tree_cons (NULL_TREE, decl, params);
2794 return build_function_call (function, params);
2797 /* Resolve a pointer to member function. INSTANCE is the object
2798 instance to use, if the member points to a virtual member. */
2800 tree
2801 get_member_function_from_ptrfunc (instance_ptrptr, function)
2802 tree *instance_ptrptr;
2803 tree function;
2805 if (TREE_CODE (function) == OFFSET_REF)
2807 function = TREE_OPERAND (function, 1);
2810 if (TYPE_PTRMEMFUNC_P (TREE_TYPE (function)))
2812 tree fntype, idx, e1, delta, delta2, e2, e3, aref, vtbl;
2813 tree instance, basetype;
2815 tree instance_ptr = *instance_ptrptr;
2817 if (instance_ptr == error_mark_node
2818 && TREE_CODE (function) == PTRMEM_CST)
2820 /* Extracting the function address from a pmf is only
2821 allowed with -Wno-pmf-conversions. It only works for
2822 pmf constants. */
2823 e1 = build_addr_func (PTRMEM_CST_MEMBER (function));
2824 e1 = convert (TYPE_PTRMEMFUNC_FN_TYPE (TREE_TYPE (function)), e1);
2825 return e1;
2828 if (TREE_SIDE_EFFECTS (instance_ptr))
2829 instance_ptr = save_expr (instance_ptr);
2831 if (TREE_SIDE_EFFECTS (function))
2832 function = save_expr (function);
2834 fntype = TYPE_PTRMEMFUNC_FN_TYPE (TREE_TYPE (function));
2835 basetype = TYPE_METHOD_BASETYPE (TREE_TYPE (fntype));
2837 delta = cp_convert (ptrdiff_type_node,
2838 build_component_ref (function, delta_identifier,
2839 NULL_TREE, 0));
2840 e3 = PFN_FROM_PTRMEMFUNC (function);
2842 /* This used to avoid checking for virtual functions if basetype
2843 has no virtual functions, according to an earlier ANSI draft.
2844 With the final ISO C++ rules, such an optimization is
2845 incorrect: A pointer to a derived member can be static_cast
2846 to pointer-to-base-member, as long as the dynamic object
2847 later has the right member. */
2849 /* Promoting idx before saving it improves performance on RISC
2850 targets. Without promoting, the first compare used
2851 load-with-sign-extend, while the second used normal load then
2852 shift to sign-extend. An optimizer flaw, perhaps, but it's
2853 easier to make this change. */
2854 idx = save_expr (default_conversion
2855 (build_component_ref (function,
2856 index_identifier,
2857 NULL_TREE, 0)));
2858 e1 = build_binary_op (GE_EXPR, idx, integer_zero_node);
2860 /* Convert down to the right base, before using the instance. */
2861 instance = convert_pointer_to_real (basetype, instance_ptr);
2862 if (instance == error_mark_node && instance_ptr != error_mark_node)
2863 return instance;
2865 vtbl = convert_pointer_to (ptr_type_node, instance);
2866 delta2 = DELTA2_FROM_PTRMEMFUNC (function);
2867 vtbl = build
2868 (PLUS_EXPR,
2869 build_pointer_type (build_pointer_type (vtable_entry_type)),
2870 vtbl, cp_convert (ptrdiff_type_node, delta2));
2871 vtbl = build_indirect_ref (vtbl, NULL_PTR);
2872 aref = build_array_ref (vtbl, build_binary_op (MINUS_EXPR,
2873 idx,
2874 integer_one_node));
2875 if (! flag_vtable_thunks)
2877 aref = save_expr (aref);
2879 delta = build_binary_op
2880 (PLUS_EXPR,
2881 build_conditional_expr (e1,
2882 build_component_ref (aref,
2883 delta_identifier,
2884 NULL_TREE, 0),
2885 integer_zero_node),
2886 delta);
2889 if (flag_vtable_thunks)
2890 e2 = aref;
2891 else
2892 e2 = build_component_ref (aref, pfn_identifier, NULL_TREE, 0);
2893 TREE_TYPE (e2) = TREE_TYPE (e3);
2894 e1 = build_conditional_expr (e1, e2, e3);
2896 /* Make sure this doesn't get evaluated first inside one of the
2897 branches of the COND_EXPR. */
2898 if (TREE_CODE (instance_ptr) == SAVE_EXPR)
2899 e1 = build (COMPOUND_EXPR, TREE_TYPE (e1),
2900 instance_ptr, e1);
2902 *instance_ptrptr = build (PLUS_EXPR, TREE_TYPE (instance_ptr),
2903 instance_ptr, delta);
2905 if (instance_ptr == error_mark_node
2906 && TREE_CODE (e1) != ADDR_EXPR
2907 && TREE_CODE (TREE_OPERAND (e1, 0)) != FUNCTION_DECL)
2908 cp_error ("object missing in `%E'", function);
2910 function = e1;
2912 return function;
2915 tree
2916 build_function_call_real (function, params, require_complete, flags)
2917 tree function, params;
2918 int require_complete, flags;
2920 register tree fntype, fndecl;
2921 register tree value_type;
2922 register tree coerced_params;
2923 tree name = NULL_TREE, assembler_name = NULL_TREE;
2924 int is_method;
2926 /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
2927 Strip such NOP_EXPRs, since FUNCTION is used in non-lvalue context. */
2928 if (TREE_CODE (function) == NOP_EXPR
2929 && TREE_TYPE (function) == TREE_TYPE (TREE_OPERAND (function, 0)))
2930 function = TREE_OPERAND (function, 0);
2932 if (TREE_CODE (function) == FUNCTION_DECL)
2934 name = DECL_NAME (function);
2935 assembler_name = DECL_ASSEMBLER_NAME (function);
2937 GNU_xref_call (current_function_decl,
2938 IDENTIFIER_POINTER (name ? name
2939 : TYPE_IDENTIFIER (DECL_CLASS_CONTEXT
2940 (function))));
2941 mark_used (function);
2942 fndecl = function;
2944 /* Convert anything with function type to a pointer-to-function. */
2945 if (pedantic && DECL_MAIN_P (function))
2946 pedwarn ("ANSI C++ forbids calling `main' from within program");
2948 /* Differs from default_conversion by not setting TREE_ADDRESSABLE
2949 (because calling an inline function does not mean the function
2950 needs to be separately compiled). */
2952 if (DECL_INLINE (function))
2953 function = inline_conversion (function);
2954 else
2955 function = build_addr_func (function);
2957 else
2959 fndecl = NULL_TREE;
2961 function = build_addr_func (function);
2964 if (function == error_mark_node)
2965 return error_mark_node;
2967 fntype = TREE_TYPE (function);
2969 if (TYPE_PTRMEMFUNC_P (fntype))
2971 cp_error ("must use .* or ->* to call pointer-to-member function in `%E (...)'",
2972 function);
2973 return error_mark_node;
2976 is_method = (TREE_CODE (fntype) == POINTER_TYPE
2977 && TREE_CODE (TREE_TYPE (fntype)) == METHOD_TYPE);
2979 if (!((TREE_CODE (fntype) == POINTER_TYPE
2980 && TREE_CODE (TREE_TYPE (fntype)) == FUNCTION_TYPE)
2981 || is_method
2982 || TREE_CODE (function) == TEMPLATE_ID_EXPR))
2984 cp_error ("`%E' cannot be used as a function", function);
2985 return error_mark_node;
2988 /* fntype now gets the type of function pointed to. */
2989 fntype = TREE_TYPE (fntype);
2991 /* Convert the parameters to the types declared in the
2992 function prototype, or apply default promotions. */
2994 if (flags & LOOKUP_COMPLAIN)
2995 coerced_params = convert_arguments (TYPE_ARG_TYPES (fntype),
2996 params, fndecl, LOOKUP_NORMAL);
2997 else
2998 coerced_params = convert_arguments (TYPE_ARG_TYPES (fntype),
2999 params, fndecl, 0);
3001 if (coerced_params == error_mark_node)
3003 if (flags & LOOKUP_SPECULATIVELY)
3004 return NULL_TREE;
3005 else
3006 return error_mark_node;
3009 /* Check for errors in format strings. */
3011 if (warn_format && (name || assembler_name))
3012 check_function_format (name, assembler_name, coerced_params);
3014 /* Recognize certain built-in functions so we can make tree-codes
3015 other than CALL_EXPR. We do this when it enables fold-const.c
3016 to do something useful. */
3018 if (TREE_CODE (function) == ADDR_EXPR
3019 && TREE_CODE (TREE_OPERAND (function, 0)) == FUNCTION_DECL
3020 && DECL_BUILT_IN (TREE_OPERAND (function, 0))
3021 && DECL_BUILT_IN_CLASS (TREE_OPERAND (function, 0)) == BUILT_IN_NORMAL)
3022 switch (DECL_FUNCTION_CODE (TREE_OPERAND (function, 0)))
3024 case BUILT_IN_ABS:
3025 case BUILT_IN_LABS:
3026 case BUILT_IN_FABS:
3027 if (coerced_params == 0)
3028 return integer_zero_node;
3029 return build_unary_op (ABS_EXPR, TREE_VALUE (coerced_params), 0);
3031 default:
3032 break;
3035 /* C++ */
3036 value_type = TREE_TYPE (fntype) ? TREE_TYPE (fntype) : void_type_node;
3038 register tree result
3039 = build_call (function, value_type, coerced_params);
3041 if (require_complete)
3043 if (TREE_CODE (value_type) == VOID_TYPE)
3044 return result;
3045 result = require_complete_type (result);
3047 if (IS_AGGR_TYPE (value_type))
3048 result = build_cplus_new (value_type, result);
3049 return convert_from_reference (result);
3053 tree
3054 build_function_call (function, params)
3055 tree function, params;
3057 return build_function_call_real (function, params, 1, LOOKUP_NORMAL);
3060 /* Convert the actual parameter expressions in the list VALUES
3061 to the types in the list TYPELIST.
3062 If parmdecls is exhausted, or when an element has NULL as its type,
3063 perform the default conversions.
3065 NAME is an IDENTIFIER_NODE or 0. It is used only for error messages.
3067 This is also where warnings about wrong number of args are generated.
3069 Return a list of expressions for the parameters as converted.
3071 Both VALUES and the returned value are chains of TREE_LIST nodes
3072 with the elements of the list in the TREE_VALUE slots of those nodes.
3074 In C++, unspecified trailing parameters can be filled in with their
3075 default arguments, if such were specified. Do so here. */
3077 tree
3078 convert_arguments (typelist, values, fndecl, flags)
3079 tree typelist, values, fndecl;
3080 int flags;
3082 register tree typetail, valtail;
3083 register tree result = NULL_TREE;
3084 const char *called_thing = 0;
3085 int i = 0;
3087 /* Argument passing is always copy-initialization. */
3088 flags |= LOOKUP_ONLYCONVERTING;
3090 if (fndecl)
3092 if (TREE_CODE (TREE_TYPE (fndecl)) == METHOD_TYPE)
3094 if (DECL_NAME (fndecl) == NULL_TREE
3095 || IDENTIFIER_HAS_TYPE_VALUE (DECL_NAME (fndecl)))
3096 called_thing = "constructor";
3097 else
3098 called_thing = "member function";
3100 else
3101 called_thing = "function";
3104 for (valtail = values, typetail = typelist;
3105 valtail;
3106 valtail = TREE_CHAIN (valtail), i++)
3108 register tree type = typetail ? TREE_VALUE (typetail) : 0;
3109 register tree val = TREE_VALUE (valtail);
3111 if (val == error_mark_node)
3112 return error_mark_node;
3114 if (type == void_type_node)
3116 if (fndecl)
3118 cp_error_at ("too many arguments to %s `%+#D'", called_thing,
3119 fndecl);
3120 error ("at this point in file");
3122 else
3123 error ("too many arguments to function");
3124 /* In case anybody wants to know if this argument
3125 list is valid. */
3126 if (result)
3127 TREE_TYPE (tree_last (result)) = error_mark_node;
3128 break;
3131 if (TREE_CODE (val) == OFFSET_REF)
3132 val = resolve_offset_ref (val);
3134 /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
3135 Strip such NOP_EXPRs, since VAL is used in non-lvalue context. */
3136 if (TREE_CODE (val) == NOP_EXPR
3137 && TREE_TYPE (val) == TREE_TYPE (TREE_OPERAND (val, 0))
3138 && (type == 0 || TREE_CODE (type) != REFERENCE_TYPE))
3139 val = TREE_OPERAND (val, 0);
3141 if (type == 0 || TREE_CODE (type) != REFERENCE_TYPE)
3143 if (TREE_CODE (TREE_TYPE (val)) == ARRAY_TYPE
3144 || TREE_CODE (TREE_TYPE (val)) == FUNCTION_TYPE
3145 || TREE_CODE (TREE_TYPE (val)) == METHOD_TYPE)
3146 val = default_conversion (val);
3149 if (val == error_mark_node)
3150 return error_mark_node;
3152 if (type != 0)
3154 /* Formal parm type is specified by a function prototype. */
3155 tree parmval;
3157 if (TYPE_SIZE (complete_type (type)) == 0)
3159 error ("parameter type of called function is incomplete");
3160 parmval = val;
3162 else
3164 parmval = convert_for_initialization
3165 (NULL_TREE, type, val, flags,
3166 "argument passing", fndecl, i);
3167 if (PROMOTE_PROTOTYPES
3168 && (TREE_CODE (type) == INTEGER_TYPE
3169 || TREE_CODE (type) == ENUMERAL_TYPE)
3170 && (TYPE_PRECISION (type)
3171 < TYPE_PRECISION (integer_type_node)))
3172 parmval = default_conversion (parmval);
3175 if (parmval == error_mark_node)
3176 return error_mark_node;
3178 result = tree_cons (NULL_TREE, parmval, result);
3180 else
3182 if (TREE_CODE (TREE_TYPE (val)) == REFERENCE_TYPE)
3183 val = convert_from_reference (val);
3185 result = tree_cons (NULL_TREE,
3186 convert_arg_to_ellipsis (val),
3187 result);
3190 if (typetail)
3191 typetail = TREE_CHAIN (typetail);
3194 if (typetail != 0 && typetail != void_list_node)
3196 /* See if there are default arguments that can be used */
3197 if (TREE_PURPOSE (typetail))
3199 for (; typetail != void_list_node; ++i)
3201 tree parmval
3202 = convert_default_arg (TREE_VALUE (typetail),
3203 TREE_PURPOSE (typetail),
3204 fndecl);
3206 if (parmval == error_mark_node)
3207 return error_mark_node;
3209 result = tree_cons (0, parmval, result);
3210 typetail = TREE_CHAIN (typetail);
3211 /* ends with `...'. */
3212 if (typetail == NULL_TREE)
3213 break;
3216 else
3218 if (fndecl)
3220 cp_error_at ("too few arguments to %s `%+#D'",
3221 called_thing, fndecl);
3222 error ("at this point in file");
3224 else
3225 error ("too few arguments to function");
3226 return error_mark_list;
3230 return nreverse (result);
3233 /* Build a binary-operation expression, after performing default
3234 conversions on the operands. CODE is the kind of expression to build. */
3236 tree
3237 build_x_binary_op (code, arg1, arg2)
3238 enum tree_code code;
3239 tree arg1, arg2;
3241 if (processing_template_decl)
3242 return build_min_nt (code, arg1, arg2);
3244 return build_new_op (code, LOOKUP_NORMAL, arg1, arg2, NULL_TREE);
3247 tree
3248 build_binary_op (code, arg1, arg2)
3249 enum tree_code code;
3250 tree arg1, arg2;
3252 return build_binary_op_nodefault (code, arg1, arg2, code);
3255 /* Build a binary-operation expression without default conversions.
3256 CODE is the kind of expression to build.
3257 This function differs from `build' in several ways:
3258 the data type of the result is computed and recorded in it,
3259 warnings are generated if arg data types are invalid,
3260 special handling for addition and subtraction of pointers is known,
3261 and some optimization is done (operations on narrow ints
3262 are done in the narrower type when that gives the same result).
3263 Constant folding is also done before the result is returned.
3265 ERROR_CODE is the code that determines what to say in error messages.
3266 It is usually, but not always, the same as CODE.
3268 Note that the operands will never have enumeral types
3269 because either they have just had the default conversions performed
3270 or they have both just been converted to some other type in which
3271 the arithmetic is to be done.
3273 C++: must do special pointer arithmetic when implementing
3274 multiple inheritance, and deal with pointer to member functions. */
3276 tree
3277 build_binary_op_nodefault (code, orig_op0, orig_op1, error_code)
3278 enum tree_code code;
3279 tree orig_op0, orig_op1;
3280 enum tree_code error_code;
3282 tree op0, op1;
3283 register enum tree_code code0, code1;
3284 tree type0, type1;
3286 /* Expression code to give to the expression when it is built.
3287 Normally this is CODE, which is what the caller asked for,
3288 but in some special cases we change it. */
3289 register enum tree_code resultcode = code;
3291 /* Data type in which the computation is to be performed.
3292 In the simplest cases this is the common type of the arguments. */
3293 register tree result_type = NULL;
3295 /* Nonzero means operands have already been type-converted
3296 in whatever way is necessary.
3297 Zero means they need to be converted to RESULT_TYPE. */
3298 int converted = 0;
3300 /* Nonzero means create the expression with this type, rather than
3301 RESULT_TYPE. */
3302 tree build_type = 0;
3304 /* Nonzero means after finally constructing the expression
3305 convert it to this type. */
3306 tree final_type = 0;
3308 /* Nonzero if this is an operation like MIN or MAX which can
3309 safely be computed in short if both args are promoted shorts.
3310 Also implies COMMON.
3311 -1 indicates a bitwise operation; this makes a difference
3312 in the exact conditions for when it is safe to do the operation
3313 in a narrower mode. */
3314 int shorten = 0;
3316 /* Nonzero if this is a comparison operation;
3317 if both args are promoted shorts, compare the original shorts.
3318 Also implies COMMON. */
3319 int short_compare = 0;
3321 /* Nonzero if this is a right-shift operation, which can be computed on the
3322 original short and then promoted if the operand is a promoted short. */
3323 int short_shift = 0;
3325 /* Nonzero means set RESULT_TYPE to the common type of the args. */
3326 int common = 0;
3328 /* Apply default conversions. */
3329 if (code == TRUTH_AND_EXPR || code == TRUTH_ANDIF_EXPR
3330 || code == TRUTH_OR_EXPR || code == TRUTH_ORIF_EXPR
3331 || code == TRUTH_XOR_EXPR)
3333 op0 = decay_conversion (orig_op0);
3334 op1 = decay_conversion (orig_op1);
3336 else
3338 op0 = default_conversion (orig_op0);
3339 op1 = default_conversion (orig_op1);
3342 /* Strip NON_LVALUE_EXPRs, etc., since we aren't using as an lvalue. */
3343 STRIP_TYPE_NOPS (op0);
3344 STRIP_TYPE_NOPS (op1);
3346 /* DTRT if one side is an overloaded function, but complain about it. */
3347 if (type_unknown_p (op0))
3349 tree t = instantiate_type (TREE_TYPE (op1), op0, 0);
3350 if (t != error_mark_node)
3352 cp_pedwarn ("assuming cast to `%T' from overloaded function",
3353 TREE_TYPE (t));
3354 op0 = t;
3357 if (type_unknown_p (op1))
3359 tree t = instantiate_type (TREE_TYPE (op0), op1, 0);
3360 if (t != error_mark_node)
3362 cp_pedwarn ("assuming cast to `%T' from overloaded function",
3363 TREE_TYPE (t));
3364 op1 = t;
3368 type0 = TREE_TYPE (op0);
3369 type1 = TREE_TYPE (op1);
3371 /* The expression codes of the data types of the arguments tell us
3372 whether the arguments are integers, floating, pointers, etc. */
3373 code0 = TREE_CODE (type0);
3374 code1 = TREE_CODE (type1);
3376 /* If an error was already reported for one of the arguments,
3377 avoid reporting another error. */
3379 if (code0 == ERROR_MARK || code1 == ERROR_MARK)
3380 return error_mark_node;
3382 switch (code)
3384 case PLUS_EXPR:
3385 /* Handle the pointer + int case. */
3386 if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
3387 return pointer_int_sum (PLUS_EXPR, op0, op1);
3388 else if (code1 == POINTER_TYPE && code0 == INTEGER_TYPE)
3389 return pointer_int_sum (PLUS_EXPR, op1, op0);
3390 else
3391 common = 1;
3392 break;
3394 case MINUS_EXPR:
3395 /* Subtraction of two similar pointers.
3396 We must subtract them as integers, then divide by object size. */
3397 if (code0 == POINTER_TYPE && code1 == POINTER_TYPE
3398 && comp_target_types (type0, type1, 1))
3399 return pointer_diff (op0, op1, common_type (type0, type1));
3400 /* Handle pointer minus int. Just like pointer plus int. */
3401 else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
3402 return pointer_int_sum (MINUS_EXPR, op0, op1);
3403 else
3404 common = 1;
3405 break;
3407 case MULT_EXPR:
3408 common = 1;
3409 break;
3411 case TRUNC_DIV_EXPR:
3412 case CEIL_DIV_EXPR:
3413 case FLOOR_DIV_EXPR:
3414 case ROUND_DIV_EXPR:
3415 case EXACT_DIV_EXPR:
3416 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE
3417 || code0 == COMPLEX_TYPE)
3418 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
3419 || code1 == COMPLEX_TYPE))
3421 if (TREE_CODE (op1) == INTEGER_CST && integer_zerop (op1))
3422 cp_warning ("division by zero in `%E / 0'", op0);
3423 else if (TREE_CODE (op1) == REAL_CST && real_zerop (op1))
3424 cp_warning ("division by zero in `%E / 0.'", op0);
3426 if (!(code0 == INTEGER_TYPE && code1 == INTEGER_TYPE))
3427 resultcode = RDIV_EXPR;
3428 else
3429 /* When dividing two signed integers, we have to promote to int.
3430 unless we divide by a constant != -1. Note that default
3431 conversion will have been performed on the operands at this
3432 point, so we have to dig out the original type to find out if
3433 it was unsigned. */
3434 shorten = ((TREE_CODE (op0) == NOP_EXPR
3435 && TREE_UNSIGNED (TREE_TYPE (TREE_OPERAND (op0, 0))))
3436 || (TREE_CODE (op1) == INTEGER_CST
3437 && (TREE_INT_CST_LOW (op1) != -1
3438 || TREE_INT_CST_HIGH (op1) != -1)));
3439 common = 1;
3441 break;
3443 case BIT_AND_EXPR:
3444 case BIT_ANDTC_EXPR:
3445 case BIT_IOR_EXPR:
3446 case BIT_XOR_EXPR:
3447 if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
3448 shorten = -1;
3449 /* If one operand is a constant, and the other is a short type
3450 that has been converted to an int,
3451 really do the work in the short type and then convert the
3452 result to int. If we are lucky, the constant will be 0 or 1
3453 in the short type, making the entire operation go away. */
3454 if (TREE_CODE (op0) == INTEGER_CST
3455 && TREE_CODE (op1) == NOP_EXPR
3456 && (TYPE_PRECISION (type1)
3457 > TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (op1, 0))))
3458 && TREE_UNSIGNED (TREE_TYPE (TREE_OPERAND (op1, 0))))
3460 final_type = result_type;
3461 op1 = TREE_OPERAND (op1, 0);
3462 result_type = TREE_TYPE (op1);
3464 if (TREE_CODE (op1) == INTEGER_CST
3465 && TREE_CODE (op0) == NOP_EXPR
3466 && (TYPE_PRECISION (type0)
3467 > TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (op0, 0))))
3468 && TREE_UNSIGNED (TREE_TYPE (TREE_OPERAND (op0, 0))))
3470 final_type = result_type;
3471 op0 = TREE_OPERAND (op0, 0);
3472 result_type = TREE_TYPE (op0);
3474 break;
3476 case TRUNC_MOD_EXPR:
3477 case FLOOR_MOD_EXPR:
3478 if (code1 == INTEGER_TYPE && integer_zerop (op1))
3479 cp_warning ("division by zero in `%E %% 0'", op0);
3480 else if (code1 == REAL_TYPE && real_zerop (op1))
3481 cp_warning ("division by zero in `%E %% 0.'", op0);
3483 if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
3485 /* Although it would be tempting to shorten always here, that loses
3486 on some targets, since the modulo instruction is undefined if the
3487 quotient can't be represented in the computation mode. We shorten
3488 only if unsigned or if dividing by something we know != -1. */
3489 shorten = ((TREE_CODE (op0) == NOP_EXPR
3490 && TREE_UNSIGNED (TREE_TYPE (TREE_OPERAND (op0, 0))))
3491 || (TREE_CODE (op1) == INTEGER_CST
3492 && (TREE_INT_CST_LOW (op1) != -1
3493 || TREE_INT_CST_HIGH (op1) != -1)));
3494 common = 1;
3496 break;
3498 case TRUTH_ANDIF_EXPR:
3499 case TRUTH_ORIF_EXPR:
3500 case TRUTH_AND_EXPR:
3501 case TRUTH_OR_EXPR:
3502 result_type = boolean_type_node;
3503 break;
3505 /* Shift operations: result has same type as first operand;
3506 always convert second operand to int.
3507 Also set SHORT_SHIFT if shifting rightward. */
3509 case RSHIFT_EXPR:
3510 if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
3512 result_type = type0;
3513 if (TREE_CODE (op1) == INTEGER_CST)
3515 if (tree_int_cst_lt (op1, integer_zero_node))
3516 warning ("right shift count is negative");
3517 else
3519 if (TREE_INT_CST_LOW (op1) | TREE_INT_CST_HIGH (op1))
3520 short_shift = 1;
3521 if (TREE_INT_CST_HIGH (op1) != 0
3522 || ((unsigned HOST_WIDE_INT) TREE_INT_CST_LOW (op1)
3523 >= TYPE_PRECISION (type0)))
3524 warning ("right shift count >= width of type");
3527 /* Convert the shift-count to an integer, regardless of
3528 size of value being shifted. */
3529 if (TYPE_MAIN_VARIANT (TREE_TYPE (op1)) != integer_type_node)
3530 op1 = cp_convert (integer_type_node, op1);
3531 /* Avoid converting op1 to result_type later. */
3532 converted = 1;
3534 break;
3536 case LSHIFT_EXPR:
3537 if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
3539 result_type = type0;
3540 if (TREE_CODE (op1) == INTEGER_CST)
3542 if (tree_int_cst_lt (op1, integer_zero_node))
3543 warning ("left shift count is negative");
3544 else if (TREE_INT_CST_HIGH (op1) != 0
3545 || ((unsigned HOST_WIDE_INT) TREE_INT_CST_LOW (op1)
3546 >= TYPE_PRECISION (type0)))
3547 warning ("left shift count >= width of type");
3549 /* Convert the shift-count to an integer, regardless of
3550 size of value being shifted. */
3551 if (TYPE_MAIN_VARIANT (TREE_TYPE (op1)) != integer_type_node)
3552 op1 = cp_convert (integer_type_node, op1);
3553 /* Avoid converting op1 to result_type later. */
3554 converted = 1;
3556 break;
3558 case RROTATE_EXPR:
3559 case LROTATE_EXPR:
3560 if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
3562 result_type = type0;
3563 if (TREE_CODE (op1) == INTEGER_CST)
3565 if (tree_int_cst_lt (op1, integer_zero_node))
3566 warning ("%s rotate count is negative",
3567 (code == LROTATE_EXPR) ? "left" : "right");
3568 else if (TREE_INT_CST_HIGH (op1) != 0
3569 || ((unsigned HOST_WIDE_INT) TREE_INT_CST_LOW (op1)
3570 >= TYPE_PRECISION (type0)))
3571 warning ("%s rotate count >= width of type",
3572 (code == LROTATE_EXPR) ? "left" : "right");
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);
3579 break;
3581 case EQ_EXPR:
3582 case NE_EXPR:
3583 if (warn_float_equal && (code0 == REAL_TYPE || code1 == REAL_TYPE))
3584 warning ("comparing floating point with == or != is unsafe");
3586 build_type = boolean_type_node;
3587 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE
3588 || code0 == COMPLEX_TYPE)
3589 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
3590 || code1 == COMPLEX_TYPE))
3591 short_compare = 1;
3592 else if (code0 == POINTER_TYPE && code1 == POINTER_TYPE)
3594 register tree tt0 = TYPE_MAIN_VARIANT (TREE_TYPE (type0));
3595 register tree tt1 = TYPE_MAIN_VARIANT (TREE_TYPE (type1));
3597 if (comp_target_types (type0, type1, 1))
3598 result_type = common_type (type0, type1);
3599 else if (tt0 == void_type_node)
3601 if (pedantic && TREE_CODE (tt1) == FUNCTION_TYPE
3602 && tree_int_cst_lt (TYPE_SIZE (type0), TYPE_SIZE (type1)))
3603 pedwarn ("ANSI C++ forbids comparison of `void *' with function pointer");
3604 else if (TREE_CODE (tt1) == OFFSET_TYPE)
3605 pedwarn ("ANSI C++ forbids conversion of a pointer to member to `void *'");
3607 else if (tt1 == void_type_node)
3609 if (pedantic && TREE_CODE (tt0) == FUNCTION_TYPE
3610 && tree_int_cst_lt (TYPE_SIZE (type1), TYPE_SIZE (type0)))
3611 pedwarn ("ANSI C++ forbids comparison of `void *' with function pointer");
3613 else
3614 cp_pedwarn ("comparison of distinct pointer types `%T' and `%T' lacks a cast",
3615 type0, type1);
3617 if (result_type == NULL_TREE)
3618 result_type = ptr_type_node;
3620 else if (code0 == POINTER_TYPE && null_ptr_cst_p (op1))
3621 result_type = type0;
3622 else if (code1 == POINTER_TYPE && null_ptr_cst_p (op0))
3623 result_type = type1;
3624 else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
3626 result_type = type0;
3627 error ("ANSI C++ forbids comparison between pointer and integer");
3629 else if (code0 == INTEGER_TYPE && code1 == POINTER_TYPE)
3631 result_type = type1;
3632 error ("ANSI C++ forbids comparison between pointer and integer");
3634 else if (TYPE_PTRMEMFUNC_P (type0) && null_ptr_cst_p (op1))
3636 op0 = build_component_ref (op0, index_identifier, NULL_TREE, 0);
3637 op1 = integer_zero_node;
3638 result_type = TREE_TYPE (op0);
3640 else if (TYPE_PTRMEMFUNC_P (type1) && null_ptr_cst_p (op0))
3642 op0 = build_component_ref (op1, index_identifier, NULL_TREE, 0);
3643 op1 = integer_zero_node;
3644 result_type = TREE_TYPE (op0);
3646 else if (TYPE_PTRMEMFUNC_P (type0) && TYPE_PTRMEMFUNC_P (type1)
3647 && same_type_p (type0, type1))
3649 /* The code we generate for the test is:
3651 (op0.index == op1.index
3652 && ((op1.index != -1 && op0.delta2 == op1.delta2)
3653 || op0.pfn == op1.pfn)) */
3655 tree index0 = build_component_ref (op0, index_identifier,
3656 NULL_TREE, 0);
3657 tree index1 = save_expr (build_component_ref (op1, index_identifier,
3658 NULL_TREE, 0));
3659 tree pfn0 = PFN_FROM_PTRMEMFUNC (op0);
3660 tree pfn1 = PFN_FROM_PTRMEMFUNC (op1);
3661 tree delta20 = DELTA2_FROM_PTRMEMFUNC (op0);
3662 tree delta21 = DELTA2_FROM_PTRMEMFUNC (op1);
3663 tree e1, e2, e3;
3664 tree integer_neg_one_node
3665 = build_binary_op (MINUS_EXPR, integer_zero_node,
3666 integer_one_node);
3667 e1 = build_binary_op (EQ_EXPR, index0, index1);
3668 e2 = build_binary_op (NE_EXPR, index1, integer_neg_one_node);
3669 e2 = build_binary_op (TRUTH_ANDIF_EXPR, e2,
3670 build_binary_op (EQ_EXPR, delta20, delta21));
3671 /* We can't use build_binary_op for this cmp because it would get
3672 confused by the ptr to method types and think we want pmfs. */
3673 e3 = build (EQ_EXPR, boolean_type_node, pfn0, pfn1);
3674 e2 = build_binary_op (TRUTH_ORIF_EXPR, e2, e3);
3675 e2 = build_binary_op (TRUTH_ANDIF_EXPR, e1, e2);
3676 if (code == EQ_EXPR)
3677 return e2;
3678 return build_binary_op (EQ_EXPR, e2, integer_zero_node);
3680 else if (TYPE_PTRMEMFUNC_P (type0)
3681 && same_type_p (TYPE_PTRMEMFUNC_FN_TYPE (type0), type1))
3683 tree index0 = build_component_ref (op0, index_identifier,
3684 NULL_TREE, 0);
3685 tree index1;
3686 tree pfn0 = PFN_FROM_PTRMEMFUNC (op0);
3687 tree delta20 = DELTA2_FROM_PTRMEMFUNC (op0);
3688 tree delta21 = integer_zero_node;
3689 tree e1, e2, e3;
3690 tree integer_neg_one_node
3691 = build_binary_op (MINUS_EXPR, integer_zero_node, integer_one_node);
3692 if (TREE_CODE (TREE_OPERAND (op1, 0)) == FUNCTION_DECL
3693 && DECL_VINDEX (TREE_OPERAND (op1, 0)))
3695 /* Map everything down one to make room for
3696 the null pointer to member. */
3697 index1 = size_binop (PLUS_EXPR,
3698 DECL_VINDEX (TREE_OPERAND (op1, 0)),
3699 integer_one_node);
3700 op1 = integer_zero_node;
3701 delta21 = TYPE_VFIELD (TYPE_METHOD_BASETYPE
3702 (TREE_TYPE (type1)));
3703 delta21 = DECL_FIELD_BITPOS (delta21);
3704 delta21 = size_binop (FLOOR_DIV_EXPR, delta21,
3705 size_int (BITS_PER_UNIT));
3706 delta21 = convert (sizetype, delta21);
3708 else
3709 index1 = integer_neg_one_node;
3711 tree nop1 = build1 (NOP_EXPR, TYPE_PTRMEMFUNC_FN_TYPE (type0),
3712 op1);
3713 TREE_CONSTANT (nop1) = TREE_CONSTANT (op1);
3714 op1 = nop1;
3716 e1 = build_binary_op (EQ_EXPR, index0, index1);
3717 e2 = build_binary_op (NE_EXPR, index1, integer_neg_one_node);
3718 e2 = build_binary_op (TRUTH_ANDIF_EXPR, e2,
3719 build_binary_op (EQ_EXPR, delta20, delta21));
3720 /* We can't use build_binary_op for this cmp because it would get
3721 confused by the ptr to method types and think we want pmfs. */
3722 e3 = build (EQ_EXPR, boolean_type_node, pfn0, op1);
3723 e2 = build_binary_op (TRUTH_ORIF_EXPR, e2, e3);
3724 e2 = build_binary_op (TRUTH_ANDIF_EXPR, e1, e2);
3725 if (code == EQ_EXPR)
3726 return e2;
3727 return build_binary_op (EQ_EXPR, e2, integer_zero_node);
3729 else if (TYPE_PTRMEMFUNC_P (type1)
3730 && same_type_p (TYPE_PTRMEMFUNC_FN_TYPE (type1), type0))
3731 return build_binary_op (code, op1, op0);
3732 break;
3734 case MAX_EXPR:
3735 case MIN_EXPR:
3736 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE)
3737 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE))
3738 shorten = 1;
3739 else if (code0 == POINTER_TYPE && code1 == POINTER_TYPE)
3741 if (comp_target_types (type0, type1, 1))
3742 result_type = common_type (type0, type1);
3743 else
3745 cp_pedwarn ("comparison of distinct pointer types `%T' and `%T' lacks a cast",
3746 type0, type1);
3747 result_type = ptr_type_node;
3750 break;
3752 case LE_EXPR:
3753 case GE_EXPR:
3754 case LT_EXPR:
3755 case GT_EXPR:
3756 build_type = boolean_type_node;
3757 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE)
3758 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE))
3759 short_compare = 1;
3760 else if (code0 == POINTER_TYPE && code1 == POINTER_TYPE)
3762 if (comp_target_types (type0, type1, 1))
3763 result_type = common_type (type0, type1);
3764 else
3766 cp_pedwarn ("comparison of distinct pointer types `%T' and `%T' lacks a cast",
3767 type0, type1);
3768 result_type = ptr_type_node;
3771 else if (code0 == POINTER_TYPE && TREE_CODE (op1) == INTEGER_CST
3772 && integer_zerop (op1))
3773 result_type = type0;
3774 else if (code1 == POINTER_TYPE && TREE_CODE (op0) == INTEGER_CST
3775 && integer_zerop (op0))
3776 result_type = type1;
3777 else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
3779 result_type = type0;
3780 pedwarn ("ANSI C++ forbids comparison between pointer and integer");
3782 else if (code0 == INTEGER_TYPE && code1 == POINTER_TYPE)
3784 result_type = type1;
3785 pedwarn ("ANSI C++ forbids comparison between pointer and integer");
3787 break;
3789 default:
3790 break;
3793 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE || code0 == COMPLEX_TYPE)
3795 (code1 == INTEGER_TYPE || code1 == REAL_TYPE || code1 == COMPLEX_TYPE))
3797 int none_complex = (code0 != COMPLEX_TYPE && code1 != COMPLEX_TYPE);
3799 if (shorten || common || short_compare)
3800 result_type = common_type (type0, type1);
3802 /* For certain operations (which identify themselves by shorten != 0)
3803 if both args were extended from the same smaller type,
3804 do the arithmetic in that type and then extend.
3806 shorten !=0 and !=1 indicates a bitwise operation.
3807 For them, this optimization is safe only if
3808 both args are zero-extended or both are sign-extended.
3809 Otherwise, we might change the result.
3810 Eg, (short)-1 | (unsigned short)-1 is (int)-1
3811 but calculated in (unsigned short) it would be (unsigned short)-1. */
3813 if (shorten && none_complex)
3815 int unsigned0, unsigned1;
3816 tree arg0 = get_narrower (op0, &unsigned0);
3817 tree arg1 = get_narrower (op1, &unsigned1);
3818 /* UNS is 1 if the operation to be done is an unsigned one. */
3819 int uns = TREE_UNSIGNED (result_type);
3820 tree type;
3822 final_type = result_type;
3824 /* Handle the case that OP0 does not *contain* a conversion
3825 but it *requires* conversion to FINAL_TYPE. */
3827 if (op0 == arg0 && TREE_TYPE (op0) != final_type)
3828 unsigned0 = TREE_UNSIGNED (TREE_TYPE (op0));
3829 if (op1 == arg1 && TREE_TYPE (op1) != final_type)
3830 unsigned1 = TREE_UNSIGNED (TREE_TYPE (op1));
3832 /* Now UNSIGNED0 is 1 if ARG0 zero-extends to FINAL_TYPE. */
3834 /* For bitwise operations, signedness of nominal type
3835 does not matter. Consider only how operands were extended. */
3836 if (shorten == -1)
3837 uns = unsigned0;
3839 /* Note that in all three cases below we refrain from optimizing
3840 an unsigned operation on sign-extended args.
3841 That would not be valid. */
3843 /* Both args variable: if both extended in same way
3844 from same width, do it in that width.
3845 Do it unsigned if args were zero-extended. */
3846 if ((TYPE_PRECISION (TREE_TYPE (arg0))
3847 < TYPE_PRECISION (result_type))
3848 && (TYPE_PRECISION (TREE_TYPE (arg1))
3849 == TYPE_PRECISION (TREE_TYPE (arg0)))
3850 && unsigned0 == unsigned1
3851 && (unsigned0 || !uns))
3852 result_type
3853 = signed_or_unsigned_type (unsigned0,
3854 common_type (TREE_TYPE (arg0),
3855 TREE_TYPE (arg1)));
3856 else if (TREE_CODE (arg0) == INTEGER_CST
3857 && (unsigned1 || !uns)
3858 && (TYPE_PRECISION (TREE_TYPE (arg1))
3859 < TYPE_PRECISION (result_type))
3860 && (type = signed_or_unsigned_type (unsigned1,
3861 TREE_TYPE (arg1)),
3862 int_fits_type_p (arg0, type)))
3863 result_type = type;
3864 else if (TREE_CODE (arg1) == INTEGER_CST
3865 && (unsigned0 || !uns)
3866 && (TYPE_PRECISION (TREE_TYPE (arg0))
3867 < TYPE_PRECISION (result_type))
3868 && (type = signed_or_unsigned_type (unsigned0,
3869 TREE_TYPE (arg0)),
3870 int_fits_type_p (arg1, type)))
3871 result_type = type;
3874 /* Shifts can be shortened if shifting right. */
3876 if (short_shift)
3878 int unsigned_arg;
3879 tree arg0 = get_narrower (op0, &unsigned_arg);
3881 final_type = result_type;
3883 if (arg0 == op0 && final_type == TREE_TYPE (op0))
3884 unsigned_arg = TREE_UNSIGNED (TREE_TYPE (op0));
3886 if (TYPE_PRECISION (TREE_TYPE (arg0)) < TYPE_PRECISION (result_type)
3887 /* We can shorten only if the shift count is less than the
3888 number of bits in the smaller type size. */
3889 && TREE_INT_CST_HIGH (op1) == 0
3890 && TYPE_PRECISION (TREE_TYPE (arg0)) > TREE_INT_CST_LOW (op1)
3891 /* If arg is sign-extended and then unsigned-shifted,
3892 we can simulate this with a signed shift in arg's type
3893 only if the extended result is at least twice as wide
3894 as the arg. Otherwise, the shift could use up all the
3895 ones made by sign-extension and bring in zeros.
3896 We can't optimize that case at all, but in most machines
3897 it never happens because available widths are 2**N. */
3898 && (!TREE_UNSIGNED (final_type)
3899 || unsigned_arg
3900 || (((unsigned) 2 * TYPE_PRECISION (TREE_TYPE (arg0)))
3901 <= TYPE_PRECISION (result_type))))
3903 /* Do an unsigned shift if the operand was zero-extended. */
3904 result_type
3905 = signed_or_unsigned_type (unsigned_arg,
3906 TREE_TYPE (arg0));
3907 /* Convert value-to-be-shifted to that type. */
3908 if (TREE_TYPE (op0) != result_type)
3909 op0 = cp_convert (result_type, op0);
3910 converted = 1;
3914 /* Comparison operations are shortened too but differently.
3915 They identify themselves by setting short_compare = 1. */
3917 if (short_compare)
3919 /* Don't write &op0, etc., because that would prevent op0
3920 from being kept in a register.
3921 Instead, make copies of the our local variables and
3922 pass the copies by reference, then copy them back afterward. */
3923 tree xop0 = op0, xop1 = op1, xresult_type = result_type;
3924 enum tree_code xresultcode = resultcode;
3925 tree val
3926 = shorten_compare (&xop0, &xop1, &xresult_type, &xresultcode);
3927 if (val != 0)
3928 return cp_convert (boolean_type_node, val);
3929 op0 = xop0, op1 = xop1;
3930 converted = 1;
3931 resultcode = xresultcode;
3934 if (short_compare && warn_sign_compare)
3936 int op0_signed = ! TREE_UNSIGNED (TREE_TYPE (orig_op0));
3937 int op1_signed = ! TREE_UNSIGNED (TREE_TYPE (orig_op1));
3939 int unsignedp0, unsignedp1;
3940 tree primop0 = get_narrower (op0, &unsignedp0);
3941 tree primop1 = get_narrower (op1, &unsignedp1);
3943 /* Check for comparison of different enum types. */
3944 if (TREE_CODE (TREE_TYPE (orig_op0)) == ENUMERAL_TYPE
3945 && TREE_CODE (TREE_TYPE (orig_op1)) == ENUMERAL_TYPE
3946 && TYPE_MAIN_VARIANT (TREE_TYPE (orig_op0))
3947 != TYPE_MAIN_VARIANT (TREE_TYPE (orig_op1)))
3949 cp_warning ("comparison between `%#T' and `%#T'",
3950 TREE_TYPE (orig_op0), TREE_TYPE (orig_op1));
3953 /* Give warnings for comparisons between signed and unsigned
3954 quantities that may fail. */
3955 /* Do the checking based on the original operand trees, so that
3956 casts will be considered, but default promotions won't be. */
3958 /* Do not warn if the comparison is being done in a signed type,
3959 since the signed type will only be chosen if it can represent
3960 all the values of the unsigned type. */
3961 if (! TREE_UNSIGNED (result_type))
3962 /* OK */;
3963 /* Do not warn if both operands are unsigned. */
3964 else if (op0_signed == op1_signed)
3965 /* OK */;
3966 /* Do not warn if the signed quantity is an unsuffixed
3967 integer literal (or some static constant expression
3968 involving such literals) and it is non-negative. */
3969 else if ((op0_signed && TREE_CODE (orig_op0) == INTEGER_CST
3970 && tree_int_cst_sgn (orig_op0) >= 0)
3971 || (op1_signed && TREE_CODE (orig_op1) == INTEGER_CST
3972 && tree_int_cst_sgn (orig_op1) >= 0))
3973 /* OK */;
3974 /* Do not warn if the comparison is an equality operation,
3975 the unsigned quantity is an integral constant and it does
3976 not use the most significant bit of result_type. */
3977 else if ((resultcode == EQ_EXPR || resultcode == NE_EXPR)
3978 && ((op0_signed && TREE_CODE (orig_op1) == INTEGER_CST
3979 && int_fits_type_p (orig_op1,
3980 signed_type (result_type)))
3981 || (op1_signed && TREE_CODE (orig_op0) == INTEGER_CST
3982 && int_fits_type_p (orig_op0,
3983 signed_type (result_type)))))
3984 /* OK */;
3985 else
3986 warning ("comparison between signed and unsigned");
3988 /* Warn if two unsigned values are being compared in a size
3989 larger than their original size, and one (and only one) is the
3990 result of a `~' operator. This comparison will always fail.
3992 Also warn if one operand is a constant, and the constant does not
3993 have all bits set that are set in the ~ operand when it is
3994 extended. */
3996 if ((TREE_CODE (primop0) == BIT_NOT_EXPR)
3997 ^ (TREE_CODE (primop1) == BIT_NOT_EXPR))
3999 if (TREE_CODE (primop0) == BIT_NOT_EXPR)
4000 primop0 = get_narrower (TREE_OPERAND (op0, 0), &unsignedp0);
4001 if (TREE_CODE (primop1) == BIT_NOT_EXPR)
4002 primop1 = get_narrower (TREE_OPERAND (op1, 0), &unsignedp1);
4004 if (TREE_CODE (primop0) == INTEGER_CST
4005 || TREE_CODE (primop1) == INTEGER_CST)
4007 tree primop;
4008 HOST_WIDE_INT constant, mask;
4009 int unsignedp;
4010 unsigned bits;
4012 if (TREE_CODE (primop0) == INTEGER_CST)
4014 primop = primop1;
4015 unsignedp = unsignedp1;
4016 constant = TREE_INT_CST_LOW (primop0);
4018 else
4020 primop = primop0;
4021 unsignedp = unsignedp0;
4022 constant = TREE_INT_CST_LOW (primop1);
4025 bits = TYPE_PRECISION (TREE_TYPE (primop));
4026 if (bits < TYPE_PRECISION (result_type)
4027 && bits < HOST_BITS_PER_LONG && unsignedp)
4029 mask = (~ (HOST_WIDE_INT) 0) << bits;
4030 if ((mask & constant) != mask)
4031 warning ("comparison of promoted ~unsigned with constant");
4034 else if (unsignedp0 && unsignedp1
4035 && (TYPE_PRECISION (TREE_TYPE (primop0))
4036 < TYPE_PRECISION (result_type))
4037 && (TYPE_PRECISION (TREE_TYPE (primop1))
4038 < TYPE_PRECISION (result_type)))
4039 warning ("comparison of promoted ~unsigned with unsigned");
4044 /* At this point, RESULT_TYPE must be nonzero to avoid an error message.
4045 If CONVERTED is zero, both args will be converted to type RESULT_TYPE.
4046 Then the expression will be built.
4047 It will be given type FINAL_TYPE if that is nonzero;
4048 otherwise, it will be given type RESULT_TYPE. */
4050 if (!result_type)
4052 cp_error ("invalid operands `%T' and `%T' to binary `%O'",
4053 TREE_TYPE (orig_op0), TREE_TYPE (orig_op1), error_code);
4054 return error_mark_node;
4057 /* Issue warnings about peculiar, but legal, uses of NULL. */
4058 if (/* It's reasonable to use pointer values as operands of &&
4059 and ||, so NULL is no exception. */
4060 !(code == TRUTH_ANDIF_EXPR || code == TRUTH_ORIF_EXPR)
4061 && (/* If OP0 is NULL and OP1 is not a pointer, or vice versa. */
4062 (orig_op0 == null_node
4063 && TREE_CODE (TREE_TYPE (op1)) != POINTER_TYPE)
4064 /* Or vice versa. */
4065 || (orig_op1 == null_node
4066 && TREE_CODE (TREE_TYPE (op0)) != POINTER_TYPE)
4067 /* Or, both are NULL and the operation was not a comparison. */
4068 || (orig_op0 == null_node && orig_op1 == null_node
4069 && code != EQ_EXPR && code != NE_EXPR)))
4070 /* Some sort of arithmetic operation involving NULL was
4071 performed. Note that pointer-difference and pointer-addition
4072 have already been handled above, and so we don't end up here in
4073 that case. */
4074 cp_warning ("NULL used in arithmetic");
4076 if (! converted)
4078 if (TREE_TYPE (op0) != result_type)
4079 op0 = cp_convert (result_type, op0);
4080 if (TREE_TYPE (op1) != result_type)
4081 op1 = cp_convert (result_type, op1);
4083 if (op0 == error_mark_node || op1 == error_mark_node)
4084 return error_mark_node;
4087 if (build_type == NULL_TREE)
4088 build_type = result_type;
4091 register tree result = build (resultcode, build_type, op0, op1);
4092 register tree folded;
4094 folded = fold (result);
4095 if (folded == result)
4096 TREE_CONSTANT (folded) = TREE_CONSTANT (op0) & TREE_CONSTANT (op1);
4097 if (final_type != 0)
4098 return cp_convert (final_type, folded);
4099 return folded;
4103 /* Return a tree for the sum or difference (RESULTCODE says which)
4104 of pointer PTROP and integer INTOP. */
4106 static tree
4107 pointer_int_sum (resultcode, ptrop, intop)
4108 enum tree_code resultcode;
4109 register tree ptrop, intop;
4111 tree size_exp;
4113 register tree result;
4114 register tree folded = fold (intop);
4116 /* The result is a pointer of the same type that is being added. */
4118 register tree result_type = TREE_TYPE (ptrop);
4120 if (!complete_type_or_else (result_type, ptrop))
4121 return error_mark_node;
4123 if (TREE_CODE (TREE_TYPE (result_type)) == VOID_TYPE)
4125 if (pedantic || warn_pointer_arith)
4126 pedwarn ("ANSI C++ forbids using pointer of type `void *' in arithmetic");
4127 size_exp = integer_one_node;
4129 else if (TREE_CODE (TREE_TYPE (result_type)) == FUNCTION_TYPE)
4131 if (pedantic || warn_pointer_arith)
4132 pedwarn ("ANSI C++ forbids using pointer to a function in arithmetic");
4133 size_exp = integer_one_node;
4135 else if (TREE_CODE (TREE_TYPE (result_type)) == METHOD_TYPE)
4137 if (pedantic || warn_pointer_arith)
4138 pedwarn ("ANSI C++ forbids using pointer to a method in arithmetic");
4139 size_exp = integer_one_node;
4141 else if (TREE_CODE (TREE_TYPE (result_type)) == OFFSET_TYPE)
4143 if (pedantic || warn_pointer_arith)
4144 pedwarn ("ANSI C++ forbids using pointer to a member in arithmetic");
4145 size_exp = integer_one_node;
4147 else
4148 size_exp = size_in_bytes (complete_type (TREE_TYPE (result_type)));
4150 /* Needed to make OOPS V2R3 work. */
4151 intop = folded;
4152 if (TREE_CODE (intop) == INTEGER_CST
4153 && TREE_INT_CST_LOW (intop) == 0
4154 && TREE_INT_CST_HIGH (intop) == 0)
4155 return ptrop;
4157 /* If what we are about to multiply by the size of the elements
4158 contains a constant term, apply distributive law
4159 and multiply that constant term separately.
4160 This helps produce common subexpressions. */
4162 if ((TREE_CODE (intop) == PLUS_EXPR || TREE_CODE (intop) == MINUS_EXPR)
4163 && ! TREE_CONSTANT (intop)
4164 && TREE_CONSTANT (TREE_OPERAND (intop, 1))
4165 && TREE_CONSTANT (size_exp))
4167 enum tree_code subcode = resultcode;
4168 if (TREE_CODE (intop) == MINUS_EXPR)
4169 subcode = (subcode == PLUS_EXPR ? MINUS_EXPR : PLUS_EXPR);
4170 ptrop = build_binary_op (subcode, ptrop, TREE_OPERAND (intop, 1));
4171 intop = TREE_OPERAND (intop, 0);
4174 /* Convert the integer argument to a type the same size as sizetype
4175 so the multiply won't overflow spuriously. */
4177 if (TYPE_PRECISION (TREE_TYPE (intop)) != TYPE_PRECISION (sizetype))
4178 intop = cp_convert (type_for_size (TYPE_PRECISION (sizetype), 0), intop);
4180 /* Replace the integer argument with a suitable product by the object size.
4181 Do this multiplication as signed, then convert to the appropriate
4182 pointer type (actually unsigned integral). */
4184 intop = cp_convert (result_type,
4185 build_binary_op (MULT_EXPR, intop,
4186 cp_convert (TREE_TYPE (intop),
4187 size_exp)));
4189 /* Create the sum or difference. */
4191 result = build (resultcode, result_type, ptrop, intop);
4193 folded = fold (result);
4194 if (folded == result)
4195 TREE_CONSTANT (folded) = TREE_CONSTANT (ptrop) & TREE_CONSTANT (intop);
4196 return folded;
4199 /* Return a tree for the difference of pointers OP0 and OP1.
4200 The resulting tree has type int. */
4202 static tree
4203 pointer_diff (op0, op1, ptrtype)
4204 register tree op0, op1;
4205 register tree ptrtype;
4207 register tree result, folded;
4208 tree restype = ptrdiff_type_node;
4209 tree target_type = TREE_TYPE (ptrtype);
4211 if (!complete_type_or_else (target_type, NULL_TREE))
4212 return error_mark_node;
4214 if (pedantic || warn_pointer_arith)
4216 if (TREE_CODE (target_type) == VOID_TYPE)
4217 pedwarn ("ANSI C++ forbids using pointer of type `void *' in subtraction");
4218 if (TREE_CODE (target_type) == FUNCTION_TYPE)
4219 pedwarn ("ANSI C++ forbids using pointer to a function in subtraction");
4220 if (TREE_CODE (target_type) == METHOD_TYPE)
4221 pedwarn ("ANSI C++ forbids using pointer to a method in subtraction");
4222 if (TREE_CODE (target_type) == OFFSET_TYPE)
4223 pedwarn ("ANSI C++ forbids using pointer to a member in subtraction");
4226 /* First do the subtraction as integers;
4227 then drop through to build the divide operator. */
4229 op0 = build_binary_op (MINUS_EXPR, cp_convert (restype, op0),
4230 cp_convert (restype, op1));
4232 /* This generates an error if op1 is a pointer to an incomplete type. */
4233 if (TYPE_SIZE (TREE_TYPE (TREE_TYPE (op1))) == 0)
4234 error ("arithmetic on pointer to an incomplete type");
4236 op1 = ((TREE_CODE (target_type) == VOID_TYPE
4237 || TREE_CODE (target_type) == FUNCTION_TYPE
4238 || TREE_CODE (target_type) == METHOD_TYPE
4239 || TREE_CODE (target_type) == OFFSET_TYPE)
4240 ? integer_one_node
4241 : size_in_bytes (target_type));
4243 /* Do the division. */
4245 result = build (EXACT_DIV_EXPR, restype, op0, cp_convert (restype, op1));
4247 folded = fold (result);
4248 if (folded == result)
4249 TREE_CONSTANT (folded) = TREE_CONSTANT (op0) & TREE_CONSTANT (op1);
4250 return folded;
4253 /* Handle the case of taking the address of a COMPONENT_REF.
4254 Called by `build_unary_op'.
4256 ARG is the COMPONENT_REF whose address we want.
4257 ARGTYPE is the pointer type that this address should have. */
4259 static tree
4260 build_component_addr (arg, argtype)
4261 tree arg, argtype;
4263 tree field = TREE_OPERAND (arg, 1);
4264 tree basetype = decl_type_context (field);
4265 tree rval = build_unary_op (ADDR_EXPR, TREE_OPERAND (arg, 0), 0);
4267 my_friendly_assert (TREE_CODE (field) == FIELD_DECL, 981018);
4269 if (DECL_C_BIT_FIELD (field))
4271 cp_error ("attempt to take address of bit-field structure member `%D'",
4272 field);
4273 return error_mark_node;
4276 if (TREE_CODE (field) == FIELD_DECL
4277 && TYPE_USES_COMPLEX_INHERITANCE (basetype))
4279 /* Can't convert directly to ARGTYPE, since that
4280 may have the same pointer type as one of our
4281 baseclasses. */
4282 rval = build1 (NOP_EXPR, argtype,
4283 convert_pointer_to (basetype, rval));
4284 TREE_CONSTANT (rval) = TREE_CONSTANT (TREE_OPERAND (rval, 0));
4286 else
4287 /* This conversion is harmless. */
4288 rval = convert_force (argtype, rval, 0);
4290 if (! integer_zerop (DECL_FIELD_BITPOS (field)))
4292 tree offset = size_binop (EASY_DIV_EXPR, DECL_FIELD_BITPOS (field),
4293 size_int (BITS_PER_UNIT));
4294 int flag = TREE_CONSTANT (rval);
4295 offset = convert (sizetype, offset);
4296 rval = fold (build (PLUS_EXPR, argtype,
4297 rval, cp_convert (argtype, offset)));
4298 TREE_CONSTANT (rval) = flag;
4300 return rval;
4303 /* Construct and perhaps optimize a tree representation
4304 for a unary operation. CODE, a tree_code, specifies the operation
4305 and XARG is the operand. */
4307 tree
4308 build_x_unary_op (code, xarg)
4309 enum tree_code code;
4310 tree xarg;
4312 if (processing_template_decl)
4313 return build_min_nt (code, xarg, NULL_TREE);
4315 /* & rec, on incomplete RECORD_TYPEs is the simple opr &, not an
4316 error message. */
4317 if (code == ADDR_EXPR
4318 && TREE_CODE (xarg) != TEMPLATE_ID_EXPR
4319 && ((IS_AGGR_TYPE_CODE (TREE_CODE (TREE_TYPE (xarg)))
4320 && TYPE_SIZE (TREE_TYPE (xarg)) == NULL_TREE)
4321 || (TREE_CODE (xarg) == OFFSET_REF)))
4322 /* don't look for a function */;
4323 else
4325 tree rval;
4327 rval = build_new_op (code, LOOKUP_NORMAL, xarg,
4328 NULL_TREE, NULL_TREE);
4329 if (rval || code != ADDR_EXPR)
4330 return rval;
4333 if (code == ADDR_EXPR)
4335 if (TREE_CODE (xarg) == TARGET_EXPR)
4336 warning ("taking address of temporary");
4339 return build_unary_op (code, xarg, 0);
4342 /* Just like truthvalue_conversion, but we want a CLEANUP_POINT_EXPR. */
4344 tree
4345 condition_conversion (expr)
4346 tree expr;
4348 tree t;
4349 if (processing_template_decl)
4350 return expr;
4351 t = perform_implicit_conversion (boolean_type_node, expr);
4352 t = fold (build1 (CLEANUP_POINT_EXPR, boolean_type_node, t));
4353 return t;
4356 /* C++: Must handle pointers to members.
4358 Perhaps type instantiation should be extended to handle conversion
4359 from aggregates to types we don't yet know we want? (Or are those
4360 cases typically errors which should be reported?)
4362 NOCONVERT nonzero suppresses the default promotions
4363 (such as from short to int). */
4365 tree
4366 build_unary_op (code, xarg, noconvert)
4367 enum tree_code code;
4368 tree xarg;
4369 int noconvert;
4371 /* No default_conversion here. It causes trouble for ADDR_EXPR. */
4372 register tree arg = xarg;
4373 register tree argtype = 0;
4374 const char *errstring = NULL;
4375 tree val;
4377 if (arg == error_mark_node)
4378 return error_mark_node;
4380 switch (code)
4382 case CONVERT_EXPR:
4383 /* This is used for unary plus, because a CONVERT_EXPR
4384 is enough to prevent anybody from looking inside for
4385 associativity, but won't generate any code. */
4386 if (!(arg = build_expr_type_conversion
4387 (WANT_ARITH | WANT_ENUM | WANT_POINTER, arg, 1)))
4388 errstring = "wrong type argument to unary plus";
4389 else
4391 if (!noconvert)
4392 arg = default_conversion (arg);
4393 arg = build1 (NON_LVALUE_EXPR, TREE_TYPE (arg), arg);
4394 TREE_CONSTANT (arg) = TREE_CONSTANT (TREE_OPERAND (arg, 0));
4396 break;
4398 case NEGATE_EXPR:
4399 if (!(arg = build_expr_type_conversion (WANT_ARITH | WANT_ENUM, arg, 1)))
4400 errstring = "wrong type argument to unary minus";
4401 else if (!noconvert)
4402 arg = default_conversion (arg);
4403 break;
4405 case BIT_NOT_EXPR:
4406 if (TREE_CODE (TREE_TYPE (arg)) == COMPLEX_TYPE)
4408 code = CONJ_EXPR;
4409 if (!noconvert)
4410 arg = default_conversion (arg);
4412 else if (!(arg = build_expr_type_conversion (WANT_INT | WANT_ENUM,
4413 arg, 1)))
4414 errstring = "wrong type argument to bit-complement";
4415 else if (!noconvert)
4416 arg = default_conversion (arg);
4417 break;
4419 case ABS_EXPR:
4420 if (!(arg = build_expr_type_conversion (WANT_ARITH | WANT_ENUM, arg, 1)))
4421 errstring = "wrong type argument to abs";
4422 else if (!noconvert)
4423 arg = default_conversion (arg);
4424 break;
4426 case CONJ_EXPR:
4427 /* Conjugating a real value is a no-op, but allow it anyway. */
4428 if (!(arg = build_expr_type_conversion (WANT_ARITH | WANT_ENUM, arg, 1)))
4429 errstring = "wrong type argument to conjugation";
4430 else if (!noconvert)
4431 arg = default_conversion (arg);
4432 break;
4434 case TRUTH_NOT_EXPR:
4435 arg = cp_convert (boolean_type_node, arg);
4436 val = invert_truthvalue (arg);
4437 if (arg != error_mark_node)
4438 return val;
4439 errstring = "in argument to unary !";
4440 break;
4442 case NOP_EXPR:
4443 break;
4445 case REALPART_EXPR:
4446 if (TREE_CODE (arg) == COMPLEX_CST)
4447 return TREE_REALPART (arg);
4448 else if (TREE_CODE (TREE_TYPE (arg)) == COMPLEX_TYPE)
4449 return fold (build1 (REALPART_EXPR, TREE_TYPE (TREE_TYPE (arg)), arg));
4450 else
4451 return arg;
4453 case IMAGPART_EXPR:
4454 if (TREE_CODE (arg) == COMPLEX_CST)
4455 return TREE_IMAGPART (arg);
4456 else if (TREE_CODE (TREE_TYPE (arg)) == COMPLEX_TYPE)
4457 return fold (build1 (IMAGPART_EXPR, TREE_TYPE (TREE_TYPE (arg)), arg));
4458 else
4459 return cp_convert (TREE_TYPE (arg), integer_zero_node);
4461 case PREINCREMENT_EXPR:
4462 case POSTINCREMENT_EXPR:
4463 case PREDECREMENT_EXPR:
4464 case POSTDECREMENT_EXPR:
4465 /* Handle complex lvalues (when permitted)
4466 by reduction to simpler cases. */
4468 val = unary_complex_lvalue (code, arg);
4469 if (val != 0)
4470 return val;
4472 /* Increment or decrement the real part of the value,
4473 and don't change the imaginary part. */
4474 if (TREE_CODE (TREE_TYPE (arg)) == COMPLEX_TYPE)
4476 tree real, imag;
4478 arg = stabilize_reference (arg);
4479 real = build_unary_op (REALPART_EXPR, arg, 1);
4480 imag = build_unary_op (IMAGPART_EXPR, arg, 1);
4481 return build (COMPLEX_EXPR, TREE_TYPE (arg),
4482 build_unary_op (code, real, 1), imag);
4485 /* Report invalid types. */
4487 if (!(arg = build_expr_type_conversion (WANT_ARITH | WANT_POINTER,
4488 arg, 1)))
4490 if (code == PREINCREMENT_EXPR)
4491 errstring ="no pre-increment operator for type";
4492 else if (code == POSTINCREMENT_EXPR)
4493 errstring ="no post-increment operator for type";
4494 else if (code == PREDECREMENT_EXPR)
4495 errstring ="no pre-decrement operator for type";
4496 else
4497 errstring ="no post-decrement operator for type";
4498 break;
4501 /* Report something read-only. */
4503 if (CP_TYPE_CONST_P (TREE_TYPE (arg))
4504 || TREE_READONLY (arg))
4505 readonly_error (arg, ((code == PREINCREMENT_EXPR
4506 || code == POSTINCREMENT_EXPR)
4507 ? "increment" : "decrement"),
4511 register tree inc;
4512 tree result_type = TREE_TYPE (arg);
4514 arg = get_unwidened (arg, 0);
4515 argtype = TREE_TYPE (arg);
4517 /* ARM $5.2.5 last annotation says this should be forbidden. */
4518 if (TREE_CODE (argtype) == ENUMERAL_TYPE)
4519 pedwarn ("ANSI C++ forbids %sing an enum",
4520 (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
4521 ? "increment" : "decrement");
4523 /* Compute the increment. */
4525 if (TREE_CODE (argtype) == POINTER_TYPE)
4527 enum tree_code tmp = TREE_CODE (TREE_TYPE (argtype));
4528 if (TYPE_SIZE (complete_type (TREE_TYPE (argtype))) == 0)
4529 cp_error ("cannot %s a pointer to incomplete type `%T'",
4530 ((code == PREINCREMENT_EXPR
4531 || code == POSTINCREMENT_EXPR)
4532 ? "increment" : "decrement"), TREE_TYPE (argtype));
4533 else if ((pedantic || warn_pointer_arith)
4534 && (tmp == FUNCTION_TYPE || tmp == METHOD_TYPE
4535 || tmp == VOID_TYPE || tmp == OFFSET_TYPE))
4536 cp_pedwarn ("ANSI C++ forbids %sing a pointer of type `%T'",
4537 ((code == PREINCREMENT_EXPR
4538 || code == POSTINCREMENT_EXPR)
4539 ? "increment" : "decrement"), argtype);
4540 inc = c_sizeof_nowarn (TREE_TYPE (argtype));
4542 else
4543 inc = integer_one_node;
4545 inc = cp_convert (argtype, inc);
4547 /* Handle incrementing a cast-expression. */
4549 switch (TREE_CODE (arg))
4551 case NOP_EXPR:
4552 case CONVERT_EXPR:
4553 case FLOAT_EXPR:
4554 case FIX_TRUNC_EXPR:
4555 case FIX_FLOOR_EXPR:
4556 case FIX_ROUND_EXPR:
4557 case FIX_CEIL_EXPR:
4559 tree incremented, modify, value, compound;
4560 if (! lvalue_p (arg) && pedantic)
4561 pedwarn ("cast to non-reference type used as lvalue");
4562 arg = stabilize_reference (arg);
4563 if (code == PREINCREMENT_EXPR || code == PREDECREMENT_EXPR)
4564 value = arg;
4565 else
4566 value = save_expr (arg);
4567 incremented = build (((code == PREINCREMENT_EXPR
4568 || code == POSTINCREMENT_EXPR)
4569 ? PLUS_EXPR : MINUS_EXPR),
4570 argtype, value, inc);
4572 modify = build_modify_expr (arg, NOP_EXPR, incremented);
4573 compound = build (COMPOUND_EXPR, TREE_TYPE (arg), modify, value);
4575 /* Eliminate warning about unused result of + or -. */
4576 TREE_NO_UNUSED_WARNING (compound) = 1;
4577 return compound;
4580 default:
4581 break;
4584 /* Complain about anything else that is not a true lvalue. */
4585 if (!lvalue_or_else (arg, ((code == PREINCREMENT_EXPR
4586 || code == POSTINCREMENT_EXPR)
4587 ? "increment" : "decrement")))
4588 return error_mark_node;
4590 /* Forbid using -- on `bool'. */
4591 if (TREE_TYPE (arg) == boolean_type_node)
4593 if (code == POSTDECREMENT_EXPR || code == PREDECREMENT_EXPR)
4595 cp_error ("invalid use of `--' on bool variable `%D'", arg);
4596 return error_mark_node;
4598 #if 0
4599 /* This will only work if someone can convince Kenner to accept
4600 my patch to expand_increment. (jason) */
4601 val = build (code, TREE_TYPE (arg), arg, inc);
4602 #else
4603 if (code == POSTINCREMENT_EXPR)
4605 arg = stabilize_reference (arg);
4606 val = build (MODIFY_EXPR, TREE_TYPE (arg), arg,
4607 boolean_true_node);
4608 arg = save_expr (arg);
4609 val = build (COMPOUND_EXPR, TREE_TYPE (arg), val, arg);
4610 val = build (COMPOUND_EXPR, TREE_TYPE (arg), arg, val);
4612 else
4613 val = build (MODIFY_EXPR, TREE_TYPE (arg), arg,
4614 boolean_true_node);
4615 #endif
4617 else
4618 val = build (code, TREE_TYPE (arg), arg, inc);
4620 TREE_SIDE_EFFECTS (val) = 1;
4621 return cp_convert (result_type, val);
4624 case ADDR_EXPR:
4625 /* Note that this operation never does default_conversion
4626 regardless of NOCONVERT. */
4628 argtype = lvalue_type (arg);
4629 if (TREE_CODE (argtype) == REFERENCE_TYPE)
4631 arg = build1
4632 (CONVERT_EXPR,
4633 build_pointer_type (TREE_TYPE (argtype)), arg);
4634 TREE_CONSTANT (arg) = TREE_CONSTANT (TREE_OPERAND (arg, 0));
4635 return arg;
4637 else if (pedantic && DECL_MAIN_P (arg))
4638 /* ARM $3.4 */
4639 pedwarn ("taking address of function `main'");
4641 /* Let &* cancel out to simplify resulting code. */
4642 if (TREE_CODE (arg) == INDIRECT_REF)
4644 /* We don't need to have `current_class_ptr' wrapped in a
4645 NON_LVALUE_EXPR node. */
4646 if (arg == current_class_ref)
4647 return current_class_ptr;
4649 arg = TREE_OPERAND (arg, 0);
4650 if (TREE_CODE (TREE_TYPE (arg)) == REFERENCE_TYPE)
4652 arg = build1
4653 (CONVERT_EXPR,
4654 build_pointer_type (TREE_TYPE (TREE_TYPE (arg))), arg);
4655 TREE_CONSTANT (arg) = TREE_CONSTANT (TREE_OPERAND (arg, 0));
4657 else if (lvalue_p (arg))
4658 /* Don't let this be an lvalue. */
4659 return non_lvalue (arg);
4660 return arg;
4663 /* For &x[y], return x+y */
4664 if (TREE_CODE (arg) == ARRAY_REF)
4666 if (mark_addressable (TREE_OPERAND (arg, 0)) == 0)
4667 return error_mark_node;
4668 return build_binary_op (PLUS_EXPR, TREE_OPERAND (arg, 0),
4669 TREE_OPERAND (arg, 1));
4672 /* Uninstantiated types are all functions. Taking the
4673 address of a function is a no-op, so just return the
4674 argument. */
4676 if (TREE_CODE (arg) == IDENTIFIER_NODE
4677 && IDENTIFIER_OPNAME_P (arg))
4679 my_friendly_abort (117);
4680 /* We don't know the type yet, so just work around the problem.
4681 We know that this will resolve to an lvalue. */
4682 return build1 (ADDR_EXPR, unknown_type_node, arg);
4685 if (TREE_CODE (arg) == COMPONENT_REF && type_unknown_p (arg)
4686 && OVL_NEXT (TREE_OPERAND (arg, 1)) == NULL_TREE)
4688 /* They're trying to take the address of a unique non-static
4689 member function. This is ill-formed, but let's try to DTRT.
4690 Note: We only handle unique functions here because we don't
4691 want to complain if there's a static overload; non-unique
4692 cases will be handled by instantiate_type. But we need to
4693 handle this case here to allow casts on the resulting PMF. */
4695 tree base = TREE_TYPE (TREE_OPERAND (arg, 0));
4696 tree name = DECL_NAME (OVL_CURRENT (TREE_OPERAND (arg, 1)));
4698 if (! flag_ms_extensions)
4700 if (current_class_type
4701 && TREE_OPERAND (arg, 0) == current_class_ref)
4702 /* An expression like &memfn. */
4703 pedwarn ("taking the address of a non-static member function");
4704 else
4705 pedwarn ("taking the address of a bound member function");
4707 cp_pedwarn
4708 (" to form a pointer to member function, say `&%T::%D'",
4709 base, name);
4712 arg = build_offset_ref (base, name);
4715 if (type_unknown_p (arg))
4716 return build1 (ADDR_EXPR, unknown_type_node, arg);
4718 /* Handle complex lvalues (when permitted)
4719 by reduction to simpler cases. */
4720 val = unary_complex_lvalue (code, arg);
4721 if (val != 0)
4722 return val;
4724 switch (TREE_CODE (arg))
4726 case NOP_EXPR:
4727 case CONVERT_EXPR:
4728 case FLOAT_EXPR:
4729 case FIX_TRUNC_EXPR:
4730 case FIX_FLOOR_EXPR:
4731 case FIX_ROUND_EXPR:
4732 case FIX_CEIL_EXPR:
4733 if (! lvalue_p (arg) && pedantic)
4734 pedwarn ("taking the address of a cast to non-reference type");
4735 break;
4737 default:
4738 break;
4741 /* Allow the address of a constructor if all the elements
4742 are constant. */
4743 if (TREE_CODE (arg) == CONSTRUCTOR && TREE_HAS_CONSTRUCTOR (arg)
4744 && TREE_CONSTANT (arg))
4746 /* Anything not already handled and not a true memory reference
4747 is an error. */
4748 else if (TREE_CODE (argtype) != FUNCTION_TYPE
4749 && TREE_CODE (argtype) != METHOD_TYPE
4750 && !lvalue_or_else (arg, "unary `&'"))
4751 return error_mark_node;
4753 if (argtype != error_mark_node)
4754 argtype = build_pointer_type (argtype);
4756 if (mark_addressable (arg) == 0)
4757 return error_mark_node;
4760 tree addr;
4762 if (TREE_CODE (arg) == COMPONENT_REF)
4763 addr = build_component_addr (arg, argtype);
4764 else
4765 addr = build1 (ADDR_EXPR, argtype, arg);
4767 /* Address of a static or external variable or
4768 function counts as a constant */
4769 if (staticp (arg))
4770 TREE_CONSTANT (addr) = 1;
4772 if (TREE_CODE (argtype) == POINTER_TYPE
4773 && TREE_CODE (TREE_TYPE (argtype)) == METHOD_TYPE)
4775 build_ptrmemfunc_type (argtype);
4776 addr = build_ptrmemfunc (argtype, addr, 0);
4779 return addr;
4782 default:
4783 break;
4786 if (!errstring)
4788 if (argtype == 0)
4789 argtype = TREE_TYPE (arg);
4790 return fold (build1 (code, argtype, arg));
4793 error (errstring);
4794 return error_mark_node;
4797 #if 0
4798 /* If CONVERSIONS is a conversion expression or a nested sequence of such,
4799 convert ARG with the same conversions in the same order
4800 and return the result. */
4802 static tree
4803 convert_sequence (conversions, arg)
4804 tree conversions;
4805 tree arg;
4807 switch (TREE_CODE (conversions))
4809 case NOP_EXPR:
4810 case CONVERT_EXPR:
4811 case FLOAT_EXPR:
4812 case FIX_TRUNC_EXPR:
4813 case FIX_FLOOR_EXPR:
4814 case FIX_ROUND_EXPR:
4815 case FIX_CEIL_EXPR:
4816 return cp_convert (TREE_TYPE (conversions),
4817 convert_sequence (TREE_OPERAND (conversions, 0),
4818 arg));
4820 default:
4821 return arg;
4824 #endif
4826 /* Apply unary lvalue-demanding operator CODE to the expression ARG
4827 for certain kinds of expressions which are not really lvalues
4828 but which we can accept as lvalues.
4830 If ARG is not a kind of expression we can handle, return zero. */
4832 tree
4833 unary_complex_lvalue (code, arg)
4834 enum tree_code code;
4835 tree arg;
4837 /* Handle (a, b) used as an "lvalue". */
4838 if (TREE_CODE (arg) == COMPOUND_EXPR)
4840 tree real_result = build_unary_op (code, TREE_OPERAND (arg, 1), 0);
4841 return build (COMPOUND_EXPR, TREE_TYPE (real_result),
4842 TREE_OPERAND (arg, 0), real_result);
4845 /* Handle (a ? b : c) used as an "lvalue". */
4846 if (TREE_CODE (arg) == COND_EXPR
4847 || TREE_CODE (arg) == MIN_EXPR || TREE_CODE (arg) == MAX_EXPR)
4848 return rationalize_conditional_expr (code, arg);
4850 if (TREE_CODE (arg) == MODIFY_EXPR
4851 || TREE_CODE (arg) == PREINCREMENT_EXPR
4852 || TREE_CODE (arg) == PREDECREMENT_EXPR)
4853 return unary_complex_lvalue
4854 (code, build (COMPOUND_EXPR, TREE_TYPE (TREE_OPERAND (arg, 0)),
4855 arg, TREE_OPERAND (arg, 0)));
4857 if (code != ADDR_EXPR)
4858 return 0;
4860 /* Handle (a = b) used as an "lvalue" for `&'. */
4861 if (TREE_CODE (arg) == MODIFY_EXPR
4862 || TREE_CODE (arg) == INIT_EXPR)
4864 tree real_result = build_unary_op (code, TREE_OPERAND (arg, 0), 0);
4865 arg = build (COMPOUND_EXPR, TREE_TYPE (real_result), arg, real_result);
4866 TREE_NO_UNUSED_WARNING (arg) = 1;
4867 return arg;
4870 if (TREE_CODE (TREE_TYPE (arg)) == FUNCTION_TYPE
4871 || TREE_CODE (TREE_TYPE (arg)) == METHOD_TYPE
4872 || TREE_CODE (TREE_TYPE (arg)) == OFFSET_TYPE)
4874 /* The representation of something of type OFFSET_TYPE
4875 is really the representation of a pointer to it.
4876 Here give the representation its true type. */
4877 tree t;
4879 my_friendly_assert (TREE_CODE (arg) != SCOPE_REF, 313);
4881 if (TREE_CODE (arg) != OFFSET_REF)
4882 return 0;
4884 t = TREE_OPERAND (arg, 1);
4886 /* Check all this code for right semantics. */
4887 if (TREE_CODE (t) == FUNCTION_DECL)
4889 if (DECL_DESTRUCTOR_P (t))
4890 cp_error ("taking address of destructor");
4891 return build_unary_op (ADDR_EXPR, t, 0);
4893 if (TREE_CODE (t) == VAR_DECL)
4894 return build_unary_op (ADDR_EXPR, t, 0);
4895 else
4897 tree type;
4899 if (TREE_OPERAND (arg, 0)
4900 && ! is_dummy_object (TREE_OPERAND (arg, 0))
4901 && TREE_CODE (t) != FIELD_DECL)
4903 cp_error ("taking address of bound pointer-to-member expression");
4904 return error_mark_node;
4907 type = build_offset_type (DECL_FIELD_CONTEXT (t), TREE_TYPE (t));
4908 type = build_pointer_type (type);
4910 t = make_ptrmem_cst (type, TREE_OPERAND (arg, 1));
4911 return t;
4916 /* We permit compiler to make function calls returning
4917 objects of aggregate type look like lvalues. */
4919 tree targ = arg;
4921 if (TREE_CODE (targ) == SAVE_EXPR)
4922 targ = TREE_OPERAND (targ, 0);
4924 if (TREE_CODE (targ) == CALL_EXPR && IS_AGGR_TYPE (TREE_TYPE (targ)))
4926 if (TREE_CODE (arg) == SAVE_EXPR)
4927 targ = arg;
4928 else
4929 targ = build_cplus_new (TREE_TYPE (arg), arg);
4930 return build1 (ADDR_EXPR, build_pointer_type (TREE_TYPE (arg)), targ);
4933 if (TREE_CODE (arg) == SAVE_EXPR && TREE_CODE (targ) == INDIRECT_REF)
4934 return build (SAVE_EXPR, build_pointer_type (TREE_TYPE (arg)),
4935 TREE_OPERAND (targ, 0), current_function_decl, NULL);
4938 /* Don't let anything else be handled specially. */
4939 return 0;
4942 /* Mark EXP saying that we need to be able to take the
4943 address of it; it should not be allocated in a register.
4944 Value is 1 if successful.
4946 C++: we do not allow `current_class_ptr' to be addressable. */
4949 mark_addressable (exp)
4950 tree exp;
4952 register tree x = exp;
4954 if (TREE_ADDRESSABLE (x) == 1)
4955 return 1;
4957 while (1)
4958 switch (TREE_CODE (x))
4960 case ADDR_EXPR:
4961 case COMPONENT_REF:
4962 case ARRAY_REF:
4963 case REALPART_EXPR:
4964 case IMAGPART_EXPR:
4965 x = TREE_OPERAND (x, 0);
4966 break;
4968 case PARM_DECL:
4969 if (x == current_class_ptr)
4971 if (! flag_this_is_variable)
4972 error ("address of `this' not available");
4973 TREE_ADDRESSABLE (x) = 1; /* so compiler doesn't die later */
4974 put_var_into_stack (x);
4975 return 1;
4977 case VAR_DECL:
4978 if (TREE_STATIC (x) && TREE_READONLY (x)
4979 && DECL_RTL (x) != 0
4980 && ! DECL_IN_MEMORY_P (x))
4982 /* We thought this would make a good constant variable,
4983 but we were wrong. */
4984 push_permanent_obstack ();
4986 TREE_ASM_WRITTEN (x) = 0;
4987 DECL_RTL (x) = 0;
4988 rest_of_decl_compilation (x, 0,
4989 !DECL_FUNCTION_SCOPE_P (x),
4991 TREE_ADDRESSABLE (x) = 1;
4993 pop_obstacks ();
4995 return 1;
4997 /* Caller should not be trying to mark initialized
4998 constant fields addressable. */
4999 my_friendly_assert (DECL_LANG_SPECIFIC (x) == 0
5000 || DECL_IN_AGGR_P (x) == 0
5001 || TREE_STATIC (x)
5002 || DECL_EXTERNAL (x), 314);
5004 case CONST_DECL:
5005 case RESULT_DECL:
5006 if (DECL_REGISTER (x) && !TREE_ADDRESSABLE (x)
5007 && !DECL_ARTIFICIAL (x) && extra_warnings)
5008 cp_warning ("address requested for `%D', which is declared `register'",
5010 TREE_ADDRESSABLE (x) = 1;
5011 TREE_USED (x) = 1;
5012 if (current_function && expanding_p)
5013 put_var_into_stack (x);
5014 return 1;
5016 case FUNCTION_DECL:
5017 if (DECL_LANG_SPECIFIC (x) != 0)
5019 x = DECL_MAIN_VARIANT (x);
5020 /* We have to test both conditions here. The first may be
5021 non-zero in the case of processing a default function. The
5022 second may be non-zero in the case of a template function. */
5023 if (DECL_TEMPLATE_INFO (x) && !DECL_TEMPLATE_SPECIALIZATION (x))
5024 mark_used (x);
5026 TREE_ADDRESSABLE (x) = 1;
5027 TREE_USED (x) = 1;
5028 TREE_ADDRESSABLE (DECL_ASSEMBLER_NAME (x)) = 1;
5029 return 1;
5031 case CONSTRUCTOR:
5032 TREE_ADDRESSABLE (x) = 1;
5033 return 1;
5035 case TARGET_EXPR:
5036 TREE_ADDRESSABLE (x) = 1;
5037 mark_addressable (TREE_OPERAND (x, 0));
5038 return 1;
5040 default:
5041 return 1;
5045 /* Build and return a conditional expression IFEXP ? OP1 : OP2. */
5047 tree
5048 build_x_conditional_expr (ifexp, op1, op2)
5049 tree ifexp, op1, op2;
5051 if (processing_template_decl)
5052 return build_min_nt (COND_EXPR, ifexp, op1, op2);
5054 return build_conditional_expr (ifexp, op1, op2);
5057 /* Handle overloading of the ',' operator when needed. Otherwise,
5058 this function just builds an expression list. */
5060 tree
5061 build_x_compound_expr (list)
5062 tree list;
5064 tree rest = TREE_CHAIN (list);
5065 tree result;
5067 if (processing_template_decl)
5068 return build_min_nt (COMPOUND_EXPR, list, NULL_TREE);
5070 if (rest == NULL_TREE)
5071 return build_compound_expr (list);
5073 result = build_opfncall (COMPOUND_EXPR, LOOKUP_NORMAL,
5074 TREE_VALUE (list), TREE_VALUE (rest), NULL_TREE);
5075 if (result)
5076 return build_x_compound_expr (tree_cons (NULL_TREE, result,
5077 TREE_CHAIN (rest)));
5079 if (! TREE_SIDE_EFFECTS (TREE_VALUE (list)))
5081 /* FIXME: This test should be in the implicit cast to void of the LHS. */
5082 /* the left-hand operand of a comma expression is like an expression
5083 statement: we should warn if it doesn't have any side-effects,
5084 unless it was explicitly cast to (void). */
5085 if ((extra_warnings || warn_unused)
5086 && !(TREE_CODE (TREE_VALUE(list)) == CONVERT_EXPR
5087 && TREE_TYPE (TREE_VALUE(list)) == void_type_node))
5088 warning("left-hand operand of comma expression has no effect");
5090 #if 0 /* this requires a gcc backend patch to export warn_if_unused_value */
5091 else if (warn_unused)
5092 warn_if_unused_value (TREE_VALUE(list));
5093 #endif
5095 return build_compound_expr
5096 (tree_cons (NULL_TREE, TREE_VALUE (list),
5097 build_expr_list (NULL_TREE,
5098 build_x_compound_expr (rest))));
5101 /* Given a list of expressions, return a compound expression
5102 that performs them all and returns the value of the last of them. */
5104 tree
5105 build_compound_expr (list)
5106 tree list;
5108 register tree rest;
5109 tree first;
5111 if (TREE_READONLY_DECL_P (TREE_VALUE (list)))
5112 TREE_VALUE (list) = decl_constant_value (TREE_VALUE (list));
5114 if (TREE_CHAIN (list) == 0)
5116 /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
5117 Strip such NOP_EXPRs, since LIST is used in non-lvalue context. */
5118 if (TREE_CODE (list) == NOP_EXPR
5119 && TREE_TYPE (list) == TREE_TYPE (TREE_OPERAND (list, 0)))
5120 list = TREE_OPERAND (list, 0);
5122 return TREE_VALUE (list);
5125 first = TREE_VALUE (list);
5126 first = convert_to_void (first, "lhs of comma");
5127 if (first == error_mark_node)
5128 return error_mark_node;
5130 rest = build_compound_expr (TREE_CHAIN (list));
5131 if (rest == error_mark_node)
5132 return error_mark_node;
5134 /* When pedantic, a compound expression cannot be a constant expression. */
5135 if (! TREE_SIDE_EFFECTS (first) && ! pedantic)
5136 return rest;
5138 return build (COMPOUND_EXPR, TREE_TYPE (rest),
5139 break_out_cleanups (first), rest);
5142 tree
5143 build_static_cast (type, expr)
5144 tree type, expr;
5146 tree intype, binfo;
5147 int ok;
5149 if (type == error_mark_node || expr == error_mark_node)
5150 return error_mark_node;
5152 if (TREE_CODE (expr) == OFFSET_REF)
5153 expr = resolve_offset_ref (expr);
5155 if (processing_template_decl)
5157 tree t = build_min (STATIC_CAST_EXPR, type, expr);
5158 return t;
5161 /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
5162 Strip such NOP_EXPRs if VALUE is being used in non-lvalue context. */
5163 if (TREE_CODE (type) != REFERENCE_TYPE
5164 && TREE_CODE (expr) == NOP_EXPR
5165 && TREE_TYPE (expr) == TREE_TYPE (TREE_OPERAND (expr, 0)))
5166 expr = TREE_OPERAND (expr, 0);
5168 if (TREE_CODE (type) == VOID_TYPE)
5170 expr = convert_to_void (expr, /*implicit=*/NULL);
5171 return expr;
5174 if (TREE_CODE (type) == REFERENCE_TYPE)
5175 return (convert_from_reference
5176 (convert_to_reference (type, expr, CONV_STATIC|CONV_IMPLICIT,
5177 LOOKUP_COMPLAIN, NULL_TREE)));
5179 if (IS_AGGR_TYPE (type))
5180 return build_cplus_new
5181 (type, (build_method_call
5182 (NULL_TREE, ctor_identifier, build_expr_list (NULL_TREE, expr),
5183 TYPE_BINFO (type), LOOKUP_NORMAL)));
5185 expr = decay_conversion (expr);
5186 intype = TREE_TYPE (expr);
5188 /* FIXME handle casting to array type. */
5190 ok = 0;
5191 if (can_convert_arg (type, intype, expr))
5192 ok = 1;
5193 else if (TYPE_PTROB_P (type) && TYPE_PTROB_P (intype))
5195 tree binfo;
5196 if (IS_AGGR_TYPE (TREE_TYPE (type)) && IS_AGGR_TYPE (TREE_TYPE (intype))
5197 && at_least_as_qualified_p (TREE_TYPE (type),
5198 TREE_TYPE (intype))
5199 && (binfo = get_binfo (TREE_TYPE (intype), TREE_TYPE (type), 0))
5200 && ! TREE_VIA_VIRTUAL (binfo))
5201 ok = 1;
5203 else if (TYPE_PTRMEM_P (type) && TYPE_PTRMEM_P (intype))
5205 if (same_type_p (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (type))),
5206 TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (intype))))
5207 && at_least_as_qualified_p (TREE_TYPE (TREE_TYPE (type)),
5208 TREE_TYPE (TREE_TYPE (intype)))
5209 && (binfo = get_binfo (TYPE_OFFSET_BASETYPE (TREE_TYPE (type)),
5210 TYPE_OFFSET_BASETYPE (TREE_TYPE (intype)), 0))
5211 && ! TREE_VIA_VIRTUAL (binfo))
5212 ok = 1;
5214 else if (TREE_CODE (intype) != BOOLEAN_TYPE
5215 && TREE_CODE (type) != ARRAY_TYPE
5216 && TREE_CODE (type) != FUNCTION_TYPE
5217 && can_convert (intype, type))
5218 ok = 1;
5220 /* [expr.static.cast]
5222 The static_cast operator shall not be used to cast away
5223 constnes. */
5224 if (ok && casts_away_constness (intype, type))
5226 cp_error ("static_cast from `%T' to `%T' casts away constness",
5227 intype, type);
5228 return error_mark_node;
5231 if (ok)
5232 return build_c_cast (type, expr);
5234 cp_error ("static_cast from `%T' to `%T'", intype, type);
5235 return error_mark_node;
5238 tree
5239 build_reinterpret_cast (type, expr)
5240 tree type, expr;
5242 tree intype;
5244 if (type == error_mark_node || expr == error_mark_node)
5245 return error_mark_node;
5247 if (TREE_CODE (expr) == OFFSET_REF)
5248 expr = resolve_offset_ref (expr);
5250 if (processing_template_decl)
5252 tree t = build_min (REINTERPRET_CAST_EXPR, type, expr);
5253 return t;
5256 if (TREE_CODE (type) != REFERENCE_TYPE)
5258 expr = decay_conversion (expr);
5260 /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
5261 Strip such NOP_EXPRs if VALUE is being used in non-lvalue context. */
5262 if (TREE_CODE (expr) == NOP_EXPR
5263 && TREE_TYPE (expr) == TREE_TYPE (TREE_OPERAND (expr, 0)))
5264 expr = TREE_OPERAND (expr, 0);
5267 intype = TREE_TYPE (expr);
5269 if (TREE_CODE (type) == REFERENCE_TYPE)
5271 if (! real_lvalue_p (expr))
5273 cp_error ("reinterpret_cast from `%T' rvalue to `%T'", intype, type);
5274 return error_mark_node;
5276 expr = build_unary_op (ADDR_EXPR, expr, 0);
5277 if (expr != error_mark_node)
5278 expr = build_reinterpret_cast
5279 (build_pointer_type (TREE_TYPE (type)), expr);
5280 if (expr != error_mark_node)
5281 expr = build_indirect_ref (expr, 0);
5282 return expr;
5284 else if (same_type_p (TYPE_MAIN_VARIANT (intype),
5285 TYPE_MAIN_VARIANT (type)))
5286 return build_static_cast (type, expr);
5288 if (TYPE_PTR_P (type) && (TREE_CODE (intype) == INTEGER_TYPE
5289 || TREE_CODE (intype) == ENUMERAL_TYPE))
5290 /* OK */;
5291 else if (TREE_CODE (type) == INTEGER_TYPE && TYPE_PTR_P (intype))
5293 if (TYPE_PRECISION (type) < TYPE_PRECISION (intype))
5294 cp_pedwarn ("reinterpret_cast from `%T' to `%T' loses precision",
5295 intype, type);
5297 else if ((TYPE_PTRFN_P (type) && TYPE_PTRFN_P (intype))
5298 || (TYPE_PTRMEMFUNC_P (type) && TYPE_PTRMEMFUNC_P (intype)))
5300 if (TREE_READONLY_DECL_P (expr))
5301 expr = decl_constant_value (expr);
5302 return fold (build1 (NOP_EXPR, type, expr));
5304 else if ((TYPE_PTRMEM_P (type) && TYPE_PTRMEM_P (intype))
5305 || (TYPE_PTROBV_P (type) && TYPE_PTROBV_P (intype)))
5307 if (! comp_ptr_ttypes_reinterpret (TREE_TYPE (type), TREE_TYPE (intype)))
5308 cp_pedwarn ("reinterpret_cast from `%T' to `%T' casts away const (or volatile)",
5309 intype, type);
5311 if (TREE_READONLY_DECL_P (expr))
5312 expr = decl_constant_value (expr);
5313 return fold (build1 (NOP_EXPR, type, expr));
5315 else if ((TYPE_PTRFN_P (type) && TYPE_PTROBV_P (intype))
5316 || (TYPE_PTRFN_P (intype) && TYPE_PTROBV_P (type)))
5318 pedwarn ("ANSI C++ forbids casting between pointers to functions and objects");
5319 if (TREE_READONLY_DECL_P (expr))
5320 expr = decl_constant_value (expr);
5321 return fold (build1 (NOP_EXPR, type, expr));
5323 else
5325 cp_error ("reinterpret_cast from `%T' to `%T'", intype, type);
5326 return error_mark_node;
5329 return cp_convert (type, expr);
5332 tree
5333 build_const_cast (type, expr)
5334 tree type, expr;
5336 tree intype;
5338 if (type == error_mark_node || expr == error_mark_node)
5339 return error_mark_node;
5341 if (TREE_CODE (expr) == OFFSET_REF)
5342 expr = resolve_offset_ref (expr);
5344 if (processing_template_decl)
5346 tree t = build_min (CONST_CAST_EXPR, type, expr);
5347 return t;
5350 if (!POINTER_TYPE_P (type))
5352 cp_error ("`%T' is not a pointer, reference, or pointer-to-data-member type",
5353 type);
5354 cp_error ("as required by const_cast");
5356 else if (TREE_CODE (TREE_TYPE (type)) == FUNCTION_TYPE)
5358 cp_error ("`%T' is a pointer or reference to a function type",
5359 type);
5360 cp_error ("which is forbidden by const_cast");
5361 return error_mark_node;
5364 if (TREE_CODE (type) != REFERENCE_TYPE)
5366 expr = decay_conversion (expr);
5368 /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
5369 Strip such NOP_EXPRs if VALUE is being used in non-lvalue context. */
5370 if (TREE_CODE (expr) == NOP_EXPR
5371 && TREE_TYPE (expr) == TREE_TYPE (TREE_OPERAND (expr, 0)))
5372 expr = TREE_OPERAND (expr, 0);
5375 intype = TREE_TYPE (expr);
5377 if (same_type_p (TYPE_MAIN_VARIANT (intype), TYPE_MAIN_VARIANT (type)))
5378 return build_static_cast (type, expr);
5379 else if (TREE_CODE (type) == REFERENCE_TYPE)
5381 if (! real_lvalue_p (expr))
5383 cp_error ("const_cast from `%T' rvalue to `%T'", intype, type);
5384 return error_mark_node;
5387 if (comp_ptr_ttypes_const (TREE_TYPE (type), intype))
5389 expr = build_unary_op (ADDR_EXPR, expr, 0);
5390 expr = build1 (NOP_EXPR, type, expr);
5391 return convert_from_reference (expr);
5394 else if (TREE_CODE (type) == POINTER_TYPE
5395 && TREE_CODE (intype) == POINTER_TYPE
5396 && comp_ptr_ttypes_const (TREE_TYPE (type), TREE_TYPE (intype)))
5397 return cp_convert (type, expr);
5399 cp_error ("const_cast from `%T' to `%T'", intype, type);
5400 return error_mark_node;
5403 /* Build an expression representing a cast to type TYPE of expression EXPR.
5405 ALLOW_NONCONVERTING is true if we should allow non-converting constructors
5406 when doing the cast. */
5408 tree
5409 build_c_cast (type, expr)
5410 tree type, expr;
5412 register tree value = expr;
5413 tree otype;
5415 if (type == error_mark_node || expr == error_mark_node)
5416 return error_mark_node;
5418 /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
5419 Strip such NOP_EXPRs if VALUE is being used in non-lvalue context. */
5420 if (TREE_CODE (type) != REFERENCE_TYPE
5421 && TREE_CODE (value) == NOP_EXPR
5422 && TREE_TYPE (value) == TREE_TYPE (TREE_OPERAND (value, 0)))
5423 value = TREE_OPERAND (value, 0);
5425 if (TREE_CODE (value) == OFFSET_REF)
5426 value = resolve_offset_ref (value);
5428 if (TREE_CODE (type) == ARRAY_TYPE)
5430 /* Allow casting from T1* to T2[] because Cfront allows it.
5431 NIHCL uses it. It is not valid ANSI C however, and hence, not
5432 valid ANSI C++. */
5433 if (TREE_CODE (TREE_TYPE (expr)) == POINTER_TYPE)
5435 if (pedantic)
5436 pedwarn ("ANSI C++ forbids casting to an array type");
5437 type = build_pointer_type (TREE_TYPE (type));
5439 else
5441 error ("ANSI C++ forbids casting to an array type");
5442 return error_mark_node;
5446 if (TREE_CODE (type) == FUNCTION_TYPE
5447 || TREE_CODE (type) == METHOD_TYPE)
5449 cp_error ("casting to function type `%T'", type);
5450 return error_mark_node;
5453 if (processing_template_decl)
5455 tree t = build_min (CAST_EXPR, type,
5456 min_tree_cons (NULL_TREE, value, NULL_TREE));
5457 return t;
5460 if (TREE_CODE (type) == VOID_TYPE)
5462 /* Conversion to void does not cause any of the normal function to
5463 * pointer, array to pointer and lvalue to rvalue decays. */
5465 value = convert_to_void (value, /*implicit=*/NULL);
5466 return value;
5468 /* Convert functions and arrays to pointers and
5469 convert references to their expanded types,
5470 but don't convert any other types. If, however, we are
5471 casting to a class type, there's no reason to do this: the
5472 cast will only succeed if there is a converting constructor,
5473 and the default conversions will be done at that point. In
5474 fact, doing the default conversion here is actually harmful
5475 in cases like this:
5477 typedef int A[2];
5478 struct S { S(const A&); };
5480 since we don't want the array-to-pointer conversion done. */
5481 if (!IS_AGGR_TYPE (type))
5483 if (TREE_CODE (TREE_TYPE (value)) == FUNCTION_TYPE
5484 || (TREE_CODE (TREE_TYPE (value)) == METHOD_TYPE
5485 /* Don't do the default conversion on a ->* expression. */
5486 && ! (TREE_CODE (type) == POINTER_TYPE
5487 && bound_pmf_p (value)))
5488 || TREE_CODE (TREE_TYPE (value)) == ARRAY_TYPE
5489 || TREE_CODE (TREE_TYPE (value)) == REFERENCE_TYPE)
5490 value = default_conversion (value);
5492 else if (TREE_CODE (TREE_TYPE (value)) == REFERENCE_TYPE)
5493 /* However, even for class types, we still need to strip away
5494 the reference type, since the call to convert_force below
5495 does not expect the input expression to be of reference
5496 type. */
5497 value = convert_from_reference (value);
5499 otype = TREE_TYPE (value);
5501 /* Optionally warn about potentially worrisome casts. */
5503 if (warn_cast_qual
5504 && TREE_CODE (type) == POINTER_TYPE
5505 && TREE_CODE (otype) == POINTER_TYPE
5506 && !at_least_as_qualified_p (TREE_TYPE (type),
5507 TREE_TYPE (otype)))
5508 cp_warning ("cast discards qualifiers from pointer target type");
5510 /* Warn about possible alignment problems. */
5511 if (STRICT_ALIGNMENT && warn_cast_align
5512 && TREE_CODE (type) == POINTER_TYPE
5513 && TREE_CODE (otype) == POINTER_TYPE
5514 && TREE_CODE (TREE_TYPE (otype)) != VOID_TYPE
5515 && TREE_CODE (TREE_TYPE (otype)) != FUNCTION_TYPE
5516 && TYPE_ALIGN (TREE_TYPE (type)) > TYPE_ALIGN (TREE_TYPE (otype)))
5517 warning ("cast increases required alignment of target type");
5519 #if 0
5520 /* We should see about re-enabling these, they seem useful to
5521 me. */
5522 if (TREE_CODE (type) == INTEGER_TYPE
5523 && TREE_CODE (otype) == POINTER_TYPE
5524 && TYPE_PRECISION (type) != TYPE_PRECISION (otype))
5525 warning ("cast from pointer to integer of different size");
5527 if (TREE_CODE (type) == POINTER_TYPE
5528 && TREE_CODE (otype) == INTEGER_TYPE
5529 && TYPE_PRECISION (type) != TYPE_PRECISION (otype)
5530 /* Don't warn about converting 0 to pointer,
5531 provided the 0 was explicit--not cast or made by folding. */
5532 && !(TREE_CODE (value) == INTEGER_CST && integer_zerop (value)))
5533 warning ("cast to pointer from integer of different size");
5534 #endif
5536 if (TREE_CODE (type) == REFERENCE_TYPE)
5537 value = (convert_from_reference
5538 (convert_to_reference (type, value, CONV_C_CAST,
5539 LOOKUP_COMPLAIN, NULL_TREE)));
5540 else
5542 tree ovalue;
5544 if (TREE_READONLY_DECL_P (value))
5545 value = decl_constant_value (value);
5547 ovalue = value;
5548 value = convert_force (type, value, CONV_C_CAST);
5550 /* Ignore any integer overflow caused by the cast. */
5551 if (TREE_CODE (value) == INTEGER_CST)
5553 TREE_OVERFLOW (value) = TREE_OVERFLOW (ovalue);
5554 TREE_CONSTANT_OVERFLOW (value) = TREE_CONSTANT_OVERFLOW (ovalue);
5558 /* Always produce some operator for an explicit cast,
5559 so we can tell (for -pedantic) that the cast is no lvalue. */
5560 if (TREE_CODE (type) != REFERENCE_TYPE && value == expr
5561 && real_lvalue_p (value))
5562 value = non_lvalue (value);
5564 return value;
5567 /* Build an assignment expression of lvalue LHS from value RHS.
5568 MODIFYCODE is the code for a binary operator that we use
5569 to combine the old value of LHS with RHS to get the new value.
5570 Or else MODIFYCODE is NOP_EXPR meaning do a simple assignment.
5572 C++: If MODIFYCODE is INIT_EXPR, then leave references unbashed. */
5574 tree
5575 build_modify_expr (lhs, modifycode, rhs)
5576 tree lhs;
5577 enum tree_code modifycode;
5578 tree rhs;
5580 register tree result;
5581 tree newrhs = rhs;
5582 tree lhstype = TREE_TYPE (lhs);
5583 tree olhstype = lhstype;
5584 tree olhs = lhs;
5586 /* Avoid duplicate error messages from operands that had errors. */
5587 if (lhs == error_mark_node || rhs == error_mark_node)
5588 return error_mark_node;
5590 /* Types that aren't fully specified cannot be used in assignments. */
5591 lhs = require_complete_type (lhs);
5593 newrhs = rhs;
5595 /* Handle control structure constructs used as "lvalues". */
5597 switch (TREE_CODE (lhs))
5599 /* Handle --foo = 5; as these are valid constructs in C++ */
5600 case PREDECREMENT_EXPR:
5601 case PREINCREMENT_EXPR:
5602 if (TREE_SIDE_EFFECTS (TREE_OPERAND (lhs, 0)))
5603 lhs = build (TREE_CODE (lhs), TREE_TYPE (lhs),
5604 stabilize_reference (TREE_OPERAND (lhs, 0)),
5605 TREE_OPERAND (lhs, 1));
5606 return build (COMPOUND_EXPR, lhstype,
5607 lhs,
5608 build_modify_expr (TREE_OPERAND (lhs, 0),
5609 modifycode, rhs));
5611 /* Handle (a, b) used as an "lvalue". */
5612 case COMPOUND_EXPR:
5613 newrhs = build_modify_expr (TREE_OPERAND (lhs, 1),
5614 modifycode, rhs);
5615 if (newrhs == error_mark_node)
5616 return error_mark_node;
5617 return build (COMPOUND_EXPR, lhstype,
5618 TREE_OPERAND (lhs, 0), newrhs);
5620 case MODIFY_EXPR:
5621 newrhs = build_modify_expr (TREE_OPERAND (lhs, 0), modifycode, rhs);
5622 if (newrhs == error_mark_node)
5623 return error_mark_node;
5624 return build (COMPOUND_EXPR, lhstype, lhs, newrhs);
5626 /* Handle (a ? b : c) used as an "lvalue". */
5627 case COND_EXPR:
5628 rhs = save_expr (rhs);
5630 /* Produce (a ? (b = rhs) : (c = rhs))
5631 except that the RHS goes through a save-expr
5632 so the code to compute it is only emitted once. */
5633 tree cond;
5635 /* Check this here to avoid odd errors when trying to convert
5636 a throw to the type of the COND_EXPR. */
5637 if (!lvalue_or_else (lhs, "assignment"))
5638 return error_mark_node;
5640 cond = build_conditional_expr
5641 (TREE_OPERAND (lhs, 0),
5642 build_modify_expr (cp_convert (TREE_TYPE (lhs),
5643 TREE_OPERAND (lhs, 1)),
5644 modifycode, rhs),
5645 build_modify_expr (cp_convert (TREE_TYPE (lhs),
5646 TREE_OPERAND (lhs, 2)),
5647 modifycode, rhs));
5649 if (cond == error_mark_node)
5650 return cond;
5651 /* Make sure the code to compute the rhs comes out
5652 before the split. */
5653 return build (COMPOUND_EXPR, TREE_TYPE (lhs),
5654 /* Case to void to suppress warning
5655 from warn_if_unused_value. */
5656 cp_convert (void_type_node, rhs), cond);
5659 default:
5660 break;
5663 if (TREE_CODE (lhs) == OFFSET_REF)
5665 if (TREE_OPERAND (lhs, 0) == NULL_TREE)
5667 /* Static class member? */
5668 tree member = TREE_OPERAND (lhs, 1);
5669 if (TREE_CODE (member) == VAR_DECL)
5670 lhs = member;
5671 else
5673 compiler_error ("invalid static class member");
5674 return error_mark_node;
5677 else
5678 lhs = resolve_offset_ref (lhs);
5680 olhstype = lhstype = TREE_TYPE (lhs);
5683 if (lhs == error_mark_node)
5684 return lhs;
5686 if (TREE_CODE (lhstype) == REFERENCE_TYPE
5687 && modifycode != INIT_EXPR)
5689 lhs = convert_from_reference (lhs);
5690 olhstype = lhstype = TREE_TYPE (lhs);
5693 /* If a binary op has been requested, combine the old LHS value with the RHS
5694 producing the value we should actually store into the LHS. */
5696 if (modifycode == INIT_EXPR)
5698 if (! IS_AGGR_TYPE (lhstype))
5699 /* Do the default thing */;
5700 else
5702 result = build_method_call (lhs, ctor_identifier,
5703 build_expr_list (NULL_TREE, rhs),
5704 TYPE_BINFO (lhstype), LOOKUP_NORMAL);
5705 if (result == NULL_TREE)
5706 return error_mark_node;
5707 return result;
5710 else if (modifycode == NOP_EXPR)
5712 /* `operator=' is not an inheritable operator. */
5713 if (! IS_AGGR_TYPE (lhstype))
5714 /* Do the default thing */;
5715 else
5717 result = build_opfncall (MODIFY_EXPR, LOOKUP_NORMAL,
5718 lhs, rhs, make_node (NOP_EXPR));
5719 if (result == NULL_TREE)
5720 return error_mark_node;
5721 return result;
5723 lhstype = olhstype;
5725 else if (PROMOTES_TO_AGGR_TYPE (lhstype, REFERENCE_TYPE))
5727 my_friendly_abort (978652);
5729 else
5731 lhs = stabilize_reference (lhs);
5732 newrhs = build_binary_op (modifycode, lhs, rhs);
5733 if (newrhs == error_mark_node)
5735 cp_error (" in evaluation of `%Q(%#T, %#T)'", modifycode,
5736 TREE_TYPE (lhs), TREE_TYPE (rhs));
5737 return error_mark_node;
5741 /* Handle a cast used as an "lvalue".
5742 We have already performed any binary operator using the value as cast.
5743 Now convert the result to the cast type of the lhs,
5744 and then true type of the lhs and store it there;
5745 then convert result back to the cast type to be the value
5746 of the assignment. */
5748 switch (TREE_CODE (lhs))
5750 case NOP_EXPR:
5751 case CONVERT_EXPR:
5752 case FLOAT_EXPR:
5753 case FIX_TRUNC_EXPR:
5754 case FIX_FLOOR_EXPR:
5755 case FIX_ROUND_EXPR:
5756 case FIX_CEIL_EXPR:
5757 if (TREE_CODE (TREE_TYPE (newrhs)) == ARRAY_TYPE
5758 || TREE_CODE (TREE_TYPE (newrhs)) == FUNCTION_TYPE
5759 || TREE_CODE (TREE_TYPE (newrhs)) == METHOD_TYPE
5760 || TREE_CODE (TREE_TYPE (newrhs)) == OFFSET_TYPE)
5761 newrhs = default_conversion (newrhs);
5763 tree inner_lhs = TREE_OPERAND (lhs, 0);
5764 tree result;
5766 /* WP 5.4.1: The result is an lvalue if T is a reference type,
5767 otherwise the result is an rvalue. */
5768 if (! lvalue_p (lhs))
5769 pedwarn ("ANSI C++ forbids cast to non-reference type used as lvalue");
5771 result = build_modify_expr (inner_lhs, NOP_EXPR,
5772 cp_convert (TREE_TYPE (inner_lhs),
5773 cp_convert (lhstype, newrhs)));
5774 if (result == error_mark_node)
5775 return result;
5776 return cp_convert (TREE_TYPE (lhs), result);
5779 default:
5780 break;
5783 /* Now we have handled acceptable kinds of LHS that are not truly lvalues.
5784 Reject anything strange now. */
5786 if (!lvalue_or_else (lhs, "assignment"))
5787 return error_mark_node;
5789 GNU_xref_assign (lhs);
5791 /* Warn about storing in something that is `const'. */
5792 /* For C++, don't warn if this is initialization. */
5793 if (modifycode != INIT_EXPR
5794 && (TREE_READONLY (lhs) || CP_TYPE_CONST_P (lhstype)
5795 /* Functions are not modifiable, even though they are
5796 lvalues. */
5797 || TREE_CODE (TREE_TYPE (lhs)) == FUNCTION_TYPE
5798 || (IS_AGGR_TYPE_CODE (TREE_CODE (lhstype))
5799 && C_TYPE_FIELDS_READONLY (lhstype))
5800 || (TREE_CODE (lhstype) == REFERENCE_TYPE
5801 && CP_TYPE_CONST_P (TREE_TYPE (lhstype)))))
5802 readonly_error (lhs, "assignment", 0);
5804 /* If storing into a structure or union member,
5805 it has probably been given type `int'.
5806 Compute the type that would go with
5807 the actual amount of storage the member occupies. */
5809 if (TREE_CODE (lhs) == COMPONENT_REF
5810 && (TREE_CODE (lhstype) == INTEGER_TYPE
5811 || TREE_CODE (lhstype) == REAL_TYPE
5812 || TREE_CODE (lhstype) == ENUMERAL_TYPE))
5814 lhstype = TREE_TYPE (get_unwidened (lhs, 0));
5816 /* If storing in a field that is in actuality a short or narrower
5817 than one, we must store in the field in its actual type. */
5819 if (lhstype != TREE_TYPE (lhs))
5821 lhs = copy_node (lhs);
5822 TREE_TYPE (lhs) = lhstype;
5826 if (modifycode != INIT_EXPR)
5828 /* Make modifycode now either a NOP_EXPR or an INIT_EXPR. */
5829 modifycode = NOP_EXPR;
5830 /* Reference-bashing */
5831 if (TREE_CODE (lhstype) == REFERENCE_TYPE)
5833 tree tmp = convert_from_reference (lhs);
5834 lhstype = TREE_TYPE (tmp);
5835 if (TYPE_SIZE (lhstype) == 0)
5837 incomplete_type_error (lhs, lhstype);
5838 return error_mark_node;
5840 lhs = tmp;
5841 olhstype = lhstype;
5843 if (TREE_CODE (TREE_TYPE (newrhs)) == REFERENCE_TYPE)
5845 tree tmp = convert_from_reference (newrhs);
5846 if (TYPE_SIZE (TREE_TYPE (tmp)) == 0)
5848 incomplete_type_error (newrhs, TREE_TYPE (tmp));
5849 return error_mark_node;
5851 newrhs = tmp;
5855 if (TREE_SIDE_EFFECTS (lhs))
5856 lhs = stabilize_reference (lhs);
5857 if (TREE_SIDE_EFFECTS (newrhs))
5858 newrhs = stabilize_reference (newrhs);
5860 /* Convert new value to destination type. */
5862 if (TREE_CODE (lhstype) == ARRAY_TYPE)
5864 int from_array;
5866 if (!same_or_base_type_p (lhstype, TREE_TYPE (rhs)))
5868 cp_error ("incompatible types in assignment of `%T' to `%T'",
5869 TREE_TYPE (rhs), lhstype);
5870 return error_mark_node;
5873 /* Allow array assignment in compiler-generated code. */
5874 if (pedantic && ! DECL_ARTIFICIAL (current_function_decl))
5875 pedwarn ("ANSI C++ forbids assignment of arrays");
5877 from_array = TREE_CODE (TREE_TYPE (newrhs)) == ARRAY_TYPE
5878 ? 1 + (modifycode != INIT_EXPR): 0;
5879 return (build_vec_init
5880 (lhs, lhs, array_type_nelts (lhstype), newrhs,
5881 from_array));
5884 if (modifycode == INIT_EXPR)
5886 newrhs = convert_for_initialization (lhs, lhstype, newrhs, LOOKUP_NORMAL,
5887 "assignment", NULL_TREE, 0);
5888 if (current_function_decl &&
5889 lhs == DECL_RESULT (current_function_decl))
5891 if (DECL_INITIAL (lhs))
5892 warning ("return value from function receives multiple initializations");
5893 DECL_INITIAL (lhs) = newrhs;
5896 else
5898 /* Avoid warnings on enum bit fields. */
5899 if (TREE_CODE (olhstype) == ENUMERAL_TYPE
5900 && TREE_CODE (lhstype) == INTEGER_TYPE)
5902 newrhs = convert_for_assignment (olhstype, newrhs, "assignment",
5903 NULL_TREE, 0);
5904 newrhs = convert_force (lhstype, newrhs, 0);
5906 else
5907 newrhs = convert_for_assignment (lhstype, newrhs, "assignment",
5908 NULL_TREE, 0);
5909 if (TREE_CODE (newrhs) == CALL_EXPR
5910 && TYPE_NEEDS_CONSTRUCTING (lhstype))
5911 newrhs = build_cplus_new (lhstype, newrhs);
5913 /* Can't initialize directly from a TARGET_EXPR, since that would
5914 cause the lhs to be constructed twice, and possibly result in
5915 accidental self-initialization. So we force the TARGET_EXPR to be
5916 expanded without a target. */
5917 if (TREE_CODE (newrhs) == TARGET_EXPR)
5918 newrhs = build (COMPOUND_EXPR, TREE_TYPE (newrhs), newrhs,
5919 TREE_OPERAND (newrhs, 0));
5922 if (newrhs == error_mark_node)
5923 return error_mark_node;
5925 if (TREE_CODE (newrhs) == COND_EXPR)
5927 tree lhs1;
5928 tree cond = TREE_OPERAND (newrhs, 0);
5930 if (TREE_SIDE_EFFECTS (lhs))
5931 cond = build_compound_expr (tree_cons
5932 (NULL_TREE, lhs,
5933 build_expr_list (NULL_TREE, cond)));
5935 /* Cannot have two identical lhs on this one tree (result) as preexpand
5936 calls will rip them out and fill in RTL for them, but when the
5937 rtl is generated, the calls will only be in the first side of the
5938 condition, not on both, or before the conditional jump! (mrs) */
5939 lhs1 = break_out_calls (lhs);
5941 if (lhs == lhs1)
5942 /* If there's no change, the COND_EXPR behaves like any other rhs. */
5943 result = build (modifycode == NOP_EXPR ? MODIFY_EXPR : INIT_EXPR,
5944 lhstype, lhs, newrhs);
5945 else
5947 tree result_type = TREE_TYPE (newrhs);
5948 /* We have to convert each arm to the proper type because the
5949 types may have been munged by constant folding. */
5950 result
5951 = build (COND_EXPR, result_type, cond,
5952 build_modify_expr (lhs, modifycode,
5953 cp_convert (result_type,
5954 TREE_OPERAND (newrhs, 1))),
5955 build_modify_expr (lhs1, modifycode,
5956 cp_convert (result_type,
5957 TREE_OPERAND (newrhs, 2))));
5960 else
5961 result = build (modifycode == NOP_EXPR ? MODIFY_EXPR : INIT_EXPR,
5962 lhstype, lhs, newrhs);
5964 TREE_SIDE_EFFECTS (result) = 1;
5966 /* If we got the LHS in a different type for storing in,
5967 convert the result back to the nominal type of LHS
5968 so that the value we return always has the same type
5969 as the LHS argument. */
5971 if (olhstype == TREE_TYPE (result))
5972 return result;
5973 /* Avoid warnings converting integral types back into enums
5974 for enum bit fields. */
5975 if (TREE_CODE (TREE_TYPE (result)) == INTEGER_TYPE
5976 && TREE_CODE (olhstype) == ENUMERAL_TYPE)
5978 result = build (COMPOUND_EXPR, olhstype, result, olhs);
5979 TREE_NO_UNUSED_WARNING (result) = 1;
5980 return result;
5982 return convert_for_assignment (olhstype, result, "assignment",
5983 NULL_TREE, 0);
5986 tree
5987 build_x_modify_expr (lhs, modifycode, rhs)
5988 tree lhs;
5989 enum tree_code modifycode;
5990 tree rhs;
5992 if (processing_template_decl)
5993 return build_min_nt (MODOP_EXPR, lhs,
5994 build_min_nt (modifycode, NULL_TREE, NULL_TREE), rhs);
5996 if (modifycode != NOP_EXPR)
5998 tree rval = build_opfncall (MODIFY_EXPR, LOOKUP_NORMAL, lhs, rhs,
5999 make_node (modifycode));
6000 if (rval)
6001 return rval;
6003 return build_modify_expr (lhs, modifycode, rhs);
6007 /* Get difference in deltas for different pointer to member function
6008 types. Return integer_zero_node, if FROM cannot be converted to a
6009 TO type. If FORCE is true, then allow reverse conversions as well.
6011 Note that the naming of FROM and TO is kind of backwards; the return
6012 value is what we add to a TO in order to get a FROM. They are named
6013 this way because we call this function to find out how to convert from
6014 a pointer to member of FROM to a pointer to member of TO. */
6016 static tree
6017 get_delta_difference (from, to, force)
6018 tree from, to;
6019 int force;
6021 tree delta = integer_zero_node;
6022 tree binfo;
6024 if (to == from)
6025 return delta;
6027 /* Should get_base_distance here, so we can check if any thing along the
6028 path is virtual, and we need to make sure we stay
6029 inside the real binfos when going through virtual bases.
6030 Maybe we should replace virtual bases with
6031 binfo_member (...CLASSTYPE_VBASECLASSES...)... (mrs) */
6032 binfo = get_binfo (from, to, 1);
6033 if (binfo == error_mark_node)
6035 error (" in pointer to member function conversion");
6036 return delta;
6038 if (binfo == 0)
6040 if (!force)
6042 error_not_base_type (from, to);
6043 error (" in pointer to member conversion");
6044 return delta;
6046 binfo = get_binfo (to, from, 1);
6047 if (binfo == 0 || binfo == error_mark_node)
6048 return delta;
6049 if (binfo_from_vbase (binfo))
6051 binfo = binfo_member (BINFO_TYPE (binfo),
6052 CLASSTYPE_VBASECLASSES (from));
6053 cp_warning ("pointer to member cast to virtual base `%T'",
6054 BINFO_TYPE (binfo));
6055 warning (" will only work if you are very careful");
6057 delta = BINFO_OFFSET (binfo);
6058 delta = cp_convert (ptrdiff_type_node, delta);
6060 return build_binary_op (MINUS_EXPR,
6061 integer_zero_node,
6062 delta);
6065 if (binfo_from_vbase (binfo))
6067 if (force)
6069 cp_warning ("pointer to member cast from virtual base `%T'",
6070 BINFO_TYPE (binfo));
6071 warning (" will only work if you are very careful");
6073 else
6074 cp_error ("pointer to member conversion from virtual base `%T'",
6075 BINFO_TYPE (binfo));
6078 return BINFO_OFFSET (binfo);
6081 tree
6082 build_ptrmemfunc1 (type, delta, idx, pfn, delta2)
6083 tree type, delta, idx, pfn, delta2;
6085 tree u;
6087 #if 0
6088 /* This is the old way we did it. We want to avoid calling
6089 digest_init, so that it can give an error if we use { } when
6090 initializing a pointer to member function. */
6092 if (pfn)
6094 u = build_nt (CONSTRUCTOR, NULL_TREE,
6095 tree_cons (pfn_identifier, pfn, NULL_TREE));
6097 else
6099 u = build_nt (CONSTRUCTOR, NULL_TREE,
6100 tree_cons (delta2_identifier, delta2, NULL_TREE));
6103 u = build_nt (CONSTRUCTOR, NULL_TREE,
6104 tree_cons (NULL_TREE, delta,
6105 tree_cons (NULL_TREE, idx,
6106 tree_cons (NULL_TREE, u, NULL_TREE))));
6108 return digest_init (type, u, (tree*)0);
6109 #else
6110 tree delta_field, idx_field, pfn_or_delta2_field, pfn_field, delta2_field;
6111 tree subtype;
6112 int allconstant, allsimple;
6114 delta_field = TYPE_FIELDS (type);
6115 idx_field = TREE_CHAIN (delta_field);
6116 pfn_or_delta2_field = TREE_CHAIN (idx_field);
6117 subtype = TREE_TYPE (pfn_or_delta2_field);
6118 pfn_field = TYPE_FIELDS (subtype);
6119 delta2_field = TREE_CHAIN (pfn_field);
6121 if (pfn)
6123 allconstant = TREE_CONSTANT (pfn);
6124 allsimple = !! initializer_constant_valid_p (pfn, TREE_TYPE (pfn));
6125 u = tree_cons (pfn_field, pfn, NULL_TREE);
6127 else
6129 delta2 = convert_and_check (delta_type_node, delta2);
6130 allconstant = TREE_CONSTANT (delta2);
6131 allsimple = !! initializer_constant_valid_p (delta2, TREE_TYPE (delta2));
6132 u = tree_cons (delta2_field, delta2, NULL_TREE);
6135 delta = convert_and_check (delta_type_node, delta);
6136 idx = convert_and_check (delta_type_node, idx);
6138 allconstant = allconstant && TREE_CONSTANT (delta) && TREE_CONSTANT (idx);
6139 allsimple = allsimple
6140 && initializer_constant_valid_p (delta, TREE_TYPE (delta))
6141 && initializer_constant_valid_p (idx, TREE_TYPE (idx));
6143 u = build (CONSTRUCTOR, subtype, NULL_TREE, u);
6144 u = tree_cons (delta_field, delta,
6145 tree_cons (idx_field, idx,
6146 tree_cons (pfn_or_delta2_field, u, NULL_TREE)));
6147 u = build (CONSTRUCTOR, type, NULL_TREE, u);
6148 TREE_CONSTANT (u) = allconstant;
6149 TREE_STATIC (u) = allconstant && allsimple;
6150 return u;
6151 #endif
6154 /* Build a constructor for a pointer to member function. It can be
6155 used to initialize global variables, local variable, or used
6156 as a value in expressions. TYPE is the POINTER to METHOD_TYPE we
6157 want to be.
6159 If FORCE is non-zero, then force this conversion, even if
6160 we would rather not do it. Usually set when using an explicit
6161 cast.
6163 Return error_mark_node, if something goes wrong. */
6165 tree
6166 build_ptrmemfunc (type, pfn, force)
6167 tree type, pfn;
6168 int force;
6170 tree fn;
6171 tree pfn_type = TREE_TYPE (pfn);
6172 tree to_type = build_ptrmemfunc_type (type);
6174 /* Handle multiple conversions of pointer to member functions. */
6175 if (TYPE_PTRMEMFUNC_P (TREE_TYPE (pfn)))
6177 tree idx = integer_zero_node;
6178 tree delta = integer_zero_node;
6179 tree delta2 = integer_zero_node;
6180 tree npfn = NULL_TREE;
6181 tree ndelta, ndelta2;
6182 tree e1, e2, e3, n;
6184 if (!force
6185 && !can_convert_arg (to_type, TREE_TYPE (pfn), pfn))
6186 cp_error ("conversion to `%T' from `%T'",
6187 to_type, pfn_type);
6189 if (TREE_CODE (pfn) == PTRMEM_CST)
6191 /* We could just build the resulting CONSTRUCTOR now, but we
6192 don't, relying on the general machinery below, together
6193 with constant-folding, to do the right thing. We don't
6194 want to return a PTRMEM_CST here, since a
6195 pointer-to-member constant is no longer a valid template
6196 argument once it is cast to any type, including its
6197 original type. */
6198 expand_ptrmemfunc_cst (pfn, &ndelta, &idx, &npfn, &ndelta2);
6199 if (npfn)
6200 /* This constant points to a non-virtual function.
6201 NDELTA2 will be NULL, but it's value doesn't really
6202 matter since we won't use it anyhow. */
6203 ndelta2 = integer_zero_node;
6205 else if (same_type_p (to_type, pfn_type))
6206 /* We don't have to do any conversion. Note that we do this
6207 after checking for a PTRMEM_CST so that a PTRMEM_CST, cast
6208 to its own type, will not be considered a legal non-type
6209 template argument. */
6210 return pfn;
6211 else
6213 ndelta = cp_convert (ptrdiff_type_node,
6214 build_component_ref (pfn,
6215 delta_identifier,
6216 NULL_TREE, 0));
6217 ndelta2 = cp_convert (ptrdiff_type_node,
6218 DELTA2_FROM_PTRMEMFUNC (pfn));
6219 idx = build_component_ref (pfn, index_identifier, NULL_TREE, 0);
6222 n = get_delta_difference (TYPE_PTRMEMFUNC_OBJECT_TYPE (pfn_type),
6223 TYPE_PTRMEMFUNC_OBJECT_TYPE (to_type),
6224 force);
6225 delta = build_binary_op (PLUS_EXPR, ndelta, n);
6226 delta2 = build_binary_op (PLUS_EXPR, ndelta2, n);
6227 e1 = fold (build (GT_EXPR, boolean_type_node, idx, integer_zero_node));
6229 /* If it's a virtual function, this is what we want. */
6230 e2 = build_ptrmemfunc1 (to_type, delta, idx,
6231 NULL_TREE, delta2);
6233 pfn = PFN_FROM_PTRMEMFUNC (pfn);
6234 npfn = build1 (NOP_EXPR, type, pfn);
6235 TREE_CONSTANT (npfn) = TREE_CONSTANT (pfn);
6237 /* But if it's a non-virtual function, or NULL, we use this
6238 instead. */
6239 e3 = build_ptrmemfunc1 (to_type, delta,
6240 idx, npfn, NULL_TREE);
6241 return build_conditional_expr (e1, e2, e3);
6244 /* Handle null pointer to member function conversions. */
6245 if (integer_zerop (pfn))
6247 pfn = build_c_cast (type, integer_zero_node);
6248 return build_ptrmemfunc1 (to_type,
6249 integer_zero_node, integer_zero_node,
6250 pfn, NULL_TREE);
6253 if (type_unknown_p (pfn))
6254 return instantiate_type (type, pfn, 1);
6256 fn = TREE_OPERAND (pfn, 0);
6257 my_friendly_assert (TREE_CODE (fn) == FUNCTION_DECL, 0);
6258 return make_ptrmem_cst (to_type, fn);
6261 /* Return the DELTA, IDX, PFN, and DELTA2 values for the PTRMEM_CST
6262 given by CST. */
6264 void
6265 expand_ptrmemfunc_cst (cst, delta, idx, pfn, delta2)
6266 tree cst;
6267 tree *delta;
6268 tree *idx;
6269 tree *pfn;
6270 tree *delta2;
6272 tree type = TREE_TYPE (cst);
6273 tree fn = PTRMEM_CST_MEMBER (cst);
6274 tree ptr_class, fn_class;
6276 my_friendly_assert (TREE_CODE (fn) == FUNCTION_DECL, 0);
6278 /* The class that the function belongs to. */
6279 fn_class = DECL_CLASS_CONTEXT (fn);
6281 /* The class that we're creating a pointer to member of. */
6282 ptr_class = TYPE_PTRMEMFUNC_OBJECT_TYPE (type);
6284 /* First, calculate the adjustment to the function's class. */
6285 *delta = get_delta_difference (fn_class, ptr_class, /*force=*/0);
6287 if (!DECL_VIRTUAL_P (fn))
6289 *idx = size_binop (MINUS_EXPR, integer_zero_node, integer_one_node);
6290 *pfn = convert (TYPE_PTRMEMFUNC_FN_TYPE (type), build_addr_func (fn));
6291 *delta2 = NULL_TREE;
6293 else
6295 /* If we're dealing with a virtual function, we have to adjust 'this'
6296 again, to point to the base which provides the vtable entry for
6297 fn; the call will do the opposite adjustment. */
6298 tree orig_class = DECL_VIRTUAL_CONTEXT (fn);
6299 tree binfo = binfo_or_else (orig_class, fn_class);
6300 *delta = size_binop (PLUS_EXPR, *delta, BINFO_OFFSET (binfo));
6302 /* Map everything down one to make room for the null PMF. */
6303 *idx = size_binop (PLUS_EXPR, DECL_VINDEX (fn), integer_one_node);
6304 *pfn = NULL_TREE;
6306 /* Offset from an object of PTR_CLASS to the vptr for ORIG_CLASS. */
6307 *delta2 = size_binop (PLUS_EXPR, *delta,
6308 get_vfield_offset (TYPE_BINFO (orig_class)));
6312 /* Return an expression for DELTA2 from the pointer-to-member function
6313 given by T. */
6315 tree
6316 delta2_from_ptrmemfunc (t)
6317 tree t;
6319 if (TREE_CODE (t) == PTRMEM_CST)
6321 tree delta;
6322 tree idx;
6323 tree pfn;
6324 tree delta2;
6326 expand_ptrmemfunc_cst (t, &delta, &idx, &pfn, &delta2);
6327 if (delta2)
6328 return delta2;
6331 return (build_component_ref
6332 (build_component_ref (t,
6333 pfn_or_delta2_identifier, NULL_TREE,
6334 0),
6335 delta2_identifier, NULL_TREE, 0));
6338 /* Return an expression for PFN from the pointer-to-member function
6339 given by T. */
6341 tree
6342 pfn_from_ptrmemfunc (t)
6343 tree t;
6345 if (TREE_CODE (t) == PTRMEM_CST)
6347 tree delta;
6348 tree idx;
6349 tree pfn;
6350 tree delta2;
6352 expand_ptrmemfunc_cst (t, &delta, &idx, &pfn, &delta2);
6353 if (pfn)
6354 return pfn;
6357 return (build_component_ref
6358 (build_component_ref (t,
6359 pfn_or_delta2_identifier, NULL_TREE,
6360 0),
6361 pfn_identifier, NULL_TREE, 0));
6364 /* Convert value RHS to type TYPE as preparation for an assignment to
6365 an lvalue of type TYPE. ERRTYPE is a string to use in error
6366 messages: "assignment", "return", etc. If FNDECL is non-NULL, we
6367 are doing the conversion in order to pass the PARMNUMth argument of
6368 FNDECL. */
6370 static tree
6371 convert_for_assignment (type, rhs, errtype, fndecl, parmnum)
6372 tree type, rhs;
6373 const char *errtype;
6374 tree fndecl;
6375 int parmnum;
6377 register enum tree_code codel = TREE_CODE (type);
6378 register tree rhstype;
6379 register enum tree_code coder;
6381 if (codel == OFFSET_TYPE)
6382 my_friendly_abort (990505);
6384 if (TREE_CODE (rhs) == OFFSET_REF)
6385 rhs = resolve_offset_ref (rhs);
6387 /* Strip NON_LVALUE_EXPRs since we aren't using as an lvalue. */
6388 if (TREE_CODE (rhs) == NON_LVALUE_EXPR)
6389 rhs = TREE_OPERAND (rhs, 0);
6391 rhstype = TREE_TYPE (rhs);
6392 coder = TREE_CODE (rhstype);
6394 if (rhs == error_mark_node || rhstype == error_mark_node)
6395 return error_mark_node;
6396 if (TREE_CODE (rhs) == TREE_LIST && TREE_VALUE (rhs) == error_mark_node)
6397 return error_mark_node;
6399 /* Issue warnings about peculiar, but legal, uses of NULL. */
6400 if (ARITHMETIC_TYPE_P (type) && rhs == null_node)
6401 cp_warning ("converting NULL to non-pointer type");
6403 /* The RHS of an assignment cannot have void type. */
6404 if (coder == VOID_TYPE)
6406 error ("void value not ignored as it ought to be");
6407 return error_mark_node;
6410 /* Simplify the RHS if possible. */
6411 if (TREE_CODE (rhs) == CONST_DECL)
6412 rhs = DECL_INITIAL (rhs);
6413 else if (TREE_READONLY_DECL_P (rhs))
6414 rhs = decl_constant_value (rhs);
6416 /* Warn about assigning a floating-point type to an integer type. */
6417 if (coder == REAL_TYPE && codel == INTEGER_TYPE)
6419 if (fndecl)
6420 cp_warning ("`%T' used for argument %P of `%D'",
6421 rhstype, parmnum, fndecl);
6422 else
6423 cp_warning ("%s to `%T' from `%T'", errtype, type, rhstype);
6425 /* And warn about assigning a negative value to an unsigned
6426 variable. */
6427 else if (TREE_UNSIGNED (type) && codel != BOOLEAN_TYPE)
6429 if (TREE_CODE (rhs) == INTEGER_CST
6430 && TREE_NEGATED_INT (rhs))
6432 if (fndecl)
6433 cp_warning ("negative value `%E' passed as argument %P of `%D'",
6434 rhs, parmnum, fndecl);
6435 else
6436 cp_warning ("%s of negative value `%E' to `%T'",
6437 errtype, rhs, type);
6439 overflow_warning (rhs);
6440 if (TREE_CONSTANT (rhs))
6441 rhs = fold (rhs);
6444 /* [expr.ass]
6446 The expression is implicitly converted (clause _conv_) to the
6447 cv-unqualified type of the left operand. */
6448 if (!can_convert_arg (type, rhstype, rhs))
6450 /* When -Wno-pmf-conversions is use, we just silently allow
6451 conversions from pointers-to-members to plain pointers. If
6452 the conversion doesn't work, cp_convert will complain. */
6453 if (!warn_pmf2ptr
6454 && TYPE_PTR_P (type)
6455 && TYPE_PTRMEMFUNC_P (rhstype))
6456 rhs = cp_convert (strip_top_quals (type), rhs);
6457 else
6459 /* If the right-hand side has unknown type, then it is an
6460 overloaded function. Call instantiate_type to get error
6461 messages. */
6462 if (rhstype == unknown_type_node)
6463 instantiate_type (type, rhs, 1);
6464 else if (fndecl)
6465 cp_error ("cannot convert `%T' to `%T' for argument `%P' to `%D'",
6466 rhstype, type, parmnum, fndecl);
6467 else
6468 cp_error ("cannot convert `%T' to `%T' in %s", rhstype, type,
6469 errtype);
6470 return error_mark_node;
6473 return perform_implicit_conversion (strip_top_quals (type), rhs);
6476 /* Convert RHS to be of type TYPE.
6477 If EXP is non-zero, it is the target of the initialization.
6478 ERRTYPE is a string to use in error messages.
6480 Two major differences between the behavior of
6481 `convert_for_assignment' and `convert_for_initialization'
6482 are that references are bashed in the former, while
6483 copied in the latter, and aggregates are assigned in
6484 the former (operator=) while initialized in the
6485 latter (X(X&)).
6487 If using constructor make sure no conversion operator exists, if one does
6488 exist, an ambiguity exists.
6490 If flags doesn't include LOOKUP_COMPLAIN, don't complain about anything. */
6492 tree
6493 convert_for_initialization (exp, type, rhs, flags, errtype, fndecl, parmnum)
6494 tree exp, type, rhs;
6495 int flags;
6496 const char *errtype;
6497 tree fndecl;
6498 int parmnum;
6500 register enum tree_code codel = TREE_CODE (type);
6501 register tree rhstype;
6502 register enum tree_code coder;
6504 /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
6505 Strip such NOP_EXPRs, since RHS is used in non-lvalue context. */
6506 if (TREE_CODE (rhs) == NOP_EXPR
6507 && TREE_TYPE (rhs) == TREE_TYPE (TREE_OPERAND (rhs, 0))
6508 && codel != REFERENCE_TYPE)
6509 rhs = TREE_OPERAND (rhs, 0);
6511 if (rhs == error_mark_node
6512 || (TREE_CODE (rhs) == TREE_LIST && TREE_VALUE (rhs) == error_mark_node))
6513 return error_mark_node;
6515 if (TREE_CODE (rhs) == OFFSET_REF)
6517 rhs = resolve_offset_ref (rhs);
6518 if (rhs == error_mark_node)
6519 return error_mark_node;
6522 if (TREE_CODE (TREE_TYPE (rhs)) == REFERENCE_TYPE)
6523 rhs = convert_from_reference (rhs);
6525 if ((TREE_CODE (TREE_TYPE (rhs)) == ARRAY_TYPE
6526 && TREE_CODE (type) != ARRAY_TYPE
6527 && (TREE_CODE (type) != REFERENCE_TYPE
6528 || TREE_CODE (TREE_TYPE (type)) != ARRAY_TYPE))
6529 || (TREE_CODE (TREE_TYPE (rhs)) == FUNCTION_TYPE
6530 && (TREE_CODE (type) != REFERENCE_TYPE
6531 || TREE_CODE (TREE_TYPE (type)) != FUNCTION_TYPE))
6532 || TREE_CODE (TREE_TYPE (rhs)) == METHOD_TYPE)
6533 rhs = default_conversion (rhs);
6535 rhstype = TREE_TYPE (rhs);
6536 coder = TREE_CODE (rhstype);
6538 if (coder == ERROR_MARK)
6539 return error_mark_node;
6541 /* We accept references to incomplete types, so we can
6542 return here before checking if RHS is of complete type. */
6544 if (codel == REFERENCE_TYPE)
6546 /* This should eventually happen in convert_arguments. */
6547 extern int warningcount, errorcount;
6548 int savew = 0, savee = 0;
6550 if (fndecl)
6551 savew = warningcount, savee = errorcount;
6552 rhs = initialize_reference (type, rhs);
6553 if (fndecl)
6555 if (warningcount > savew)
6556 cp_warning_at ("in passing argument %P of `%+D'", parmnum, fndecl);
6557 else if (errorcount > savee)
6558 cp_error_at ("in passing argument %P of `%+D'", parmnum, fndecl);
6560 return rhs;
6563 if (exp != 0)
6564 exp = require_complete_type (exp);
6565 if (exp == error_mark_node)
6566 return error_mark_node;
6568 if (TREE_CODE (rhstype) == REFERENCE_TYPE)
6569 rhstype = TREE_TYPE (rhstype);
6571 type = complete_type (type);
6573 if (IS_AGGR_TYPE (type))
6574 return ocp_convert (type, rhs, CONV_IMPLICIT|CONV_FORCE_TEMP, flags);
6576 if (type == TREE_TYPE (rhs))
6578 /* Issue warnings about peculiar, but legal, uses of NULL. We
6579 do this *before* the call to decl_constant_value so as to
6580 avoid duplicate warnings on code like `const int I = NULL;
6581 f(I);'. */
6582 if (ARITHMETIC_TYPE_P (type) && rhs == null_node)
6583 cp_warning ("converting NULL to non-pointer type");
6585 if (TREE_READONLY_DECL_P (rhs))
6586 rhs = decl_constant_value (rhs);
6588 return rhs;
6591 return convert_for_assignment (type, rhs, errtype, fndecl, parmnum);
6594 /* Expand an ASM statement with operands, handling output operands
6595 that are not variables or INDIRECT_REFS by transforming such
6596 cases into cases that expand_asm_operands can handle.
6598 Arguments are same as for expand_asm_operands.
6600 We don't do default conversions on all inputs, because it can screw
6601 up operands that are expected to be in memory. */
6603 void
6604 c_expand_asm_operands (string, outputs, inputs, clobbers, vol, filename, line)
6605 tree string, outputs, inputs, clobbers;
6606 int vol;
6607 char *filename;
6608 int line;
6610 int noutputs = list_length (outputs);
6611 register int i;
6612 /* o[I] is the place that output number I should be written. */
6613 register tree *o = (tree *) alloca (noutputs * sizeof (tree));
6614 register tree tail;
6616 /* Record the contents of OUTPUTS before it is modified. */
6617 for (i = 0, tail = outputs; tail; tail = TREE_CHAIN (tail), i++)
6618 o[i] = TREE_VALUE (tail);
6620 /* Generate the ASM_OPERANDS insn;
6621 store into the TREE_VALUEs of OUTPUTS some trees for
6622 where the values were actually stored. */
6623 expand_asm_operands (string, outputs, inputs, clobbers, vol, filename, line);
6625 /* Copy all the intermediate outputs into the specified outputs. */
6626 for (i = 0, tail = outputs; tail; tail = TREE_CHAIN (tail), i++)
6628 if (o[i] != TREE_VALUE (tail))
6630 expand_expr (build_modify_expr (o[i], NOP_EXPR, TREE_VALUE (tail)),
6631 const0_rtx, VOIDmode, EXPAND_NORMAL);
6632 free_temp_slots ();
6634 /* Detect modification of read-only values.
6635 (Otherwise done by build_modify_expr.) */
6636 else
6638 tree type = TREE_TYPE (o[i]);
6639 if (CP_TYPE_CONST_P (type)
6640 || (IS_AGGR_TYPE_CODE (TREE_CODE (type))
6641 && C_TYPE_FIELDS_READONLY (type)))
6642 readonly_error (o[i], "modification by `asm'", 1);
6646 /* Those MODIFY_EXPRs could do autoincrements. */
6647 emit_queue ();
6650 /* If RETVAL is the address of, or a reference to, a local variable or
6651 temporary give an appropraite warning. */
6653 static void
6654 maybe_warn_about_returning_address_of_local (retval)
6655 tree retval;
6657 tree valtype = TREE_TYPE (DECL_RESULT (current_function_decl));
6659 if (TREE_CODE (valtype) == REFERENCE_TYPE)
6661 tree whats_returned;
6663 /* Sort through common things to see what it is
6664 we are returning. */
6665 whats_returned = retval;
6666 if (TREE_CODE (whats_returned) == COMPOUND_EXPR)
6668 whats_returned = TREE_OPERAND (whats_returned, 1);
6669 if (TREE_CODE (whats_returned) == ADDR_EXPR)
6670 whats_returned = TREE_OPERAND (whats_returned, 0);
6672 while (TREE_CODE (whats_returned) == CONVERT_EXPR
6673 || TREE_CODE (whats_returned) == NOP_EXPR)
6674 whats_returned = TREE_OPERAND (whats_returned, 0);
6675 if (TREE_CODE (whats_returned) == ADDR_EXPR)
6677 whats_returned = TREE_OPERAND (whats_returned, 0);
6678 while (TREE_CODE (whats_returned) == AGGR_INIT_EXPR
6679 || TREE_CODE (whats_returned) == TARGET_EXPR)
6681 /* Get the target. */
6682 whats_returned = TREE_OPERAND (whats_returned, 0);
6683 warning ("returning reference to temporary");
6687 if (TREE_CODE (whats_returned) == VAR_DECL
6688 && DECL_NAME (whats_returned))
6690 if (TEMP_NAME_P (DECL_NAME (whats_returned)))
6691 warning ("reference to non-lvalue returned");
6692 else if (TREE_CODE (TREE_TYPE (whats_returned)) != REFERENCE_TYPE
6693 && DECL_FUNCTION_SCOPE_P (whats_returned)
6694 && !(TREE_STATIC (whats_returned)
6695 || TREE_PUBLIC (whats_returned)))
6696 cp_warning_at ("reference to local variable `%D' returned",
6697 whats_returned);
6700 else if (TREE_CODE (retval) == ADDR_EXPR)
6702 tree whats_returned = TREE_OPERAND (retval, 0);
6704 if (TREE_CODE (whats_returned) == VAR_DECL
6705 && DECL_NAME (whats_returned)
6706 && DECL_FUNCTION_SCOPE_P (whats_returned)
6707 && !(TREE_STATIC (whats_returned)
6708 || TREE_PUBLIC (whats_returned)))
6709 cp_warning_at ("address of local variable `%D' returned",
6710 whats_returned);
6714 /* Check that returning RETVAL from the current function is legal.
6715 Return an expression explicitly showing all conversions required to
6716 change RETVAL into the function return type, and to assign it to
6717 the DECL_RESULT for the function. */
6719 tree
6720 check_return_expr (retval)
6721 tree retval;
6723 tree result;
6724 /* The type actually returned by the function, after any
6725 promotions. */
6726 tree valtype;
6727 int fn_returns_value_p;
6729 /* A `volatile' function is one that isn't supposed to return, ever.
6730 (This is a G++ extension, used to get better code for functions
6731 that call the `volatile' function.) */
6732 if (TREE_THIS_VOLATILE (current_function_decl))
6733 warning ("function declared `noreturn' has a `return' statement");
6735 /* Check for various simple errors. */
6736 if (retval == error_mark_node)
6738 /* If an error occurred, there's nothing to do. */
6739 current_function_returns_null = 1;
6740 return error_mark_node;
6742 else if (dtor_label)
6744 if (retval)
6745 error ("returning a value from a destructor");
6746 return NULL_TREE;
6748 else if (in_function_try_handler
6749 && DECL_CONSTRUCTOR_P (current_function_decl))
6751 /* If a return statement appears in a handler of the
6752 function-try-block of a constructor, the program is ill-formed. */
6753 error ("cannot return from a handler of a function-try-block of a constructor");
6754 return error_mark_node;
6756 else if (retval && DECL_CONSTRUCTOR_P (current_function_decl))
6757 /* You can't return a value from a constructor. */
6758 error ("returning a value from a constructor");
6760 /* Constructors actually always return `this', even though in C++
6761 you can't return a value from a constructor. */
6762 if (DECL_CONSTRUCTOR_P (current_function_decl))
6763 retval = current_class_ptr;
6765 /* When no explicit return-value is given in a function with a named
6766 return value, the named return value is used. */
6767 result = DECL_RESULT (current_function_decl);
6768 valtype = TREE_TYPE (result);
6769 my_friendly_assert (valtype != NULL_TREE, 19990924);
6770 fn_returns_value_p = !same_type_p (valtype, void_type_node);
6771 if (!retval && DECL_NAME (result) && fn_returns_value_p)
6772 retval = result;
6774 /* Check for a return statement with no return value in a function
6775 that's supposed to return a value. */
6776 if (!retval && fn_returns_value_p)
6778 pedwarn ("`return' with no value, in function returning non-void");
6779 /* Clear this, so finish_function won't say that we reach the
6780 end of a non-void function (which we don't, we gave a
6781 return!). */
6782 current_function_returns_null = 0;
6784 /* Check for a return statement with a value in a function that
6785 isn't supposed to return a value. */
6786 else if (retval && !fn_returns_value_p)
6788 if (same_type_p (TREE_TYPE (retval), void_type_node))
6789 /* You can return a `void' value from a function of `void'
6790 type. In that case, we have to evaluate the expression for
6791 its side-effects. */
6792 finish_expr_stmt (retval);
6793 else
6794 pedwarn ("`return' with a value, in function returning void");
6796 current_function_returns_null = 1;
6798 /* There's really no value to return, after all. */
6799 return NULL_TREE;
6801 else if (!retval)
6802 /* Remember that this function can sometimes return without a
6803 value. */
6804 current_function_returns_null = 1;
6806 /* Only operator new(...) throw(), can return NULL [expr.new/13]. */
6807 if ((DECL_NAME (current_function_decl) == ansi_opname[(int) NEW_EXPR]
6808 || DECL_NAME (current_function_decl) == ansi_opname[(int) VEC_NEW_EXPR])
6809 && !TYPE_NOTHROW_P (TREE_TYPE (current_function_decl))
6810 && null_ptr_cst_p (retval))
6811 cp_warning ("operator new should throw an exception, not return NULL");
6813 /* Effective C++ rule 15. See also start_function. */
6814 if (warn_ecpp
6815 && DECL_NAME (current_function_decl) == ansi_opname[(int) MODIFY_EXPR]
6816 && retval != current_class_ref)
6817 cp_warning ("`operator=' should return a reference to `*this'");
6819 /* We don't need to do any conversions when there's nothing being
6820 returned. */
6821 if (!retval)
6822 return NULL_TREE;
6824 /* Do any required conversions. */
6825 if (retval == result || DECL_CONSTRUCTOR_P (current_function_decl))
6826 /* No conversions are required. */
6828 else
6830 /* The type the function is declared to return. */
6831 tree functype = TREE_TYPE (TREE_TYPE (current_function_decl));
6833 /* First convert the value to the function's return type, then
6834 to the type of return value's location to handle the
6835 case that functype is thiner than the valtype. */
6836 retval = convert_for_initialization
6837 (NULL_TREE, functype, retval, LOOKUP_NORMAL|LOOKUP_ONLYCONVERTING,
6838 "return", NULL_TREE, 0);
6839 retval = convert (valtype, retval);
6841 /* If the conversion failed, treat this just like `return;'. */
6842 if (retval == error_mark_node)
6843 return NULL_TREE;
6844 /* We can't initialize a register from a AGGR_INIT_EXPR. */
6845 else if (! current_function_returns_struct
6846 && TREE_CODE (retval) == TARGET_EXPR
6847 && TREE_CODE (TREE_OPERAND (retval, 1)) == AGGR_INIT_EXPR)
6848 retval = build (COMPOUND_EXPR, TREE_TYPE (retval), retval,
6849 TREE_OPERAND (retval, 0));
6850 else
6851 maybe_warn_about_returning_address_of_local (retval);
6854 /* Actually copy the value returned into the appropriate location. */
6855 if (retval && retval != result)
6856 retval = build (INIT_EXPR, TREE_TYPE (result), result, retval);
6858 /* All done. Remember that this function did return a value. */
6859 current_function_returns_value = 1;
6860 return retval;
6863 /* Expand a C `return' statement.
6864 RETVAL is the expression for what to return,
6865 or a null pointer for `return;' with no value.
6867 C++: upon seeing a `return', we must call destructors on all
6868 variables in scope which had constructors called on them.
6869 This means that if in a destructor, the base class destructors
6870 must be called before returning.
6872 The RETURN statement in C++ has initialization semantics. */
6874 void
6875 c_expand_return (retval)
6876 tree retval;
6878 if (!retval)
6879 expand_null_return ();
6880 else
6882 expand_start_target_temps ();
6883 expand_return (retval);
6884 expand_end_target_temps ();
6888 /* Start a C switch statement, testing expression EXP.
6889 Return EXP if it is valid, an error node otherwise. */
6891 tree
6892 c_expand_start_case (exp)
6893 tree exp;
6895 tree type, idx;
6897 exp = build_expr_type_conversion (WANT_INT | WANT_ENUM, exp, 1);
6898 if (exp == NULL_TREE)
6900 error ("switch quantity not an integer");
6901 exp = error_mark_node;
6903 if (exp == error_mark_node)
6904 return error_mark_node;
6906 exp = default_conversion (exp);
6907 type = TREE_TYPE (exp);
6908 idx = get_unwidened (exp, 0);
6909 /* We can't strip a conversion from a signed type to an unsigned,
6910 because if we did, int_fits_type_p would do the wrong thing
6911 when checking case values for being in range,
6912 and it's too hard to do the right thing. */
6913 if (TREE_UNSIGNED (TREE_TYPE (exp)) == TREE_UNSIGNED (TREE_TYPE (idx)))
6914 exp = idx;
6916 expand_start_case
6917 (1, fold (build1 (CLEANUP_POINT_EXPR, TREE_TYPE (exp), exp)),
6918 type, "switch statement");
6920 return exp;
6923 /* Returns non-zero if the pointer-type FROM can be converted to the
6924 pointer-type TO via a qualification conversion. If CONSTP is -1,
6925 then we return non-zero if the pointers are similar, and the
6926 cv-qualification signature of FROM is a proper subset of that of TO.
6928 If CONSTP is positive, then all outer pointers have been
6929 const-qualified. */
6931 static int
6932 comp_ptr_ttypes_real (to, from, constp)
6933 tree to, from;
6934 int constp;
6936 int to_more_cv_qualified = 0;
6938 for (; ; to = TREE_TYPE (to), from = TREE_TYPE (from))
6940 if (TREE_CODE (to) != TREE_CODE (from))
6941 return 0;
6943 if (TREE_CODE (from) == OFFSET_TYPE
6944 && same_type_p (TYPE_OFFSET_BASETYPE (from),
6945 TYPE_OFFSET_BASETYPE (to)))
6946 continue;
6948 /* Const and volatile mean something different for function types,
6949 so the usual checks are not appropriate. */
6950 if (TREE_CODE (to) != FUNCTION_TYPE && TREE_CODE (to) != METHOD_TYPE)
6952 if (!at_least_as_qualified_p (to, from))
6953 return 0;
6955 if (!at_least_as_qualified_p (from, to))
6957 if (constp == 0)
6958 return 0;
6959 else
6960 ++to_more_cv_qualified;
6963 if (constp > 0)
6964 constp &= TYPE_READONLY (to);
6967 if (TREE_CODE (to) != POINTER_TYPE)
6968 return
6969 same_type_p (TYPE_MAIN_VARIANT (to), TYPE_MAIN_VARIANT (from))
6970 && (constp >= 0 || to_more_cv_qualified);
6974 /* When comparing, say, char ** to char const **, this function takes the
6975 'char *' and 'char const *'. Do not pass non-pointer types to this
6976 function. */
6979 comp_ptr_ttypes (to, from)
6980 tree to, from;
6982 return comp_ptr_ttypes_real (to, from, 1);
6985 /* Returns 1 if to and from are (possibly multi-level) pointers to the same
6986 type or inheritance-related types, regardless of cv-quals. */
6989 ptr_reasonably_similar (to, from)
6990 tree to, from;
6992 for (; ; to = TREE_TYPE (to), from = TREE_TYPE (from))
6994 if (TREE_CODE (to) != TREE_CODE (from))
6995 return 0;
6997 if (TREE_CODE (from) == OFFSET_TYPE
6998 && comptypes (TYPE_OFFSET_BASETYPE (to),
6999 TYPE_OFFSET_BASETYPE (from),
7000 COMPARE_BASE | COMPARE_RELAXED))
7001 continue;
7003 if (TREE_CODE (to) != POINTER_TYPE)
7004 return comptypes
7005 (TYPE_MAIN_VARIANT (to), TYPE_MAIN_VARIANT (from),
7006 COMPARE_BASE | COMPARE_RELAXED);
7010 /* Like comp_ptr_ttypes, for const_cast. */
7012 static int
7013 comp_ptr_ttypes_const (to, from)
7014 tree to, from;
7016 for (; ; to = TREE_TYPE (to), from = TREE_TYPE (from))
7018 if (TREE_CODE (to) != TREE_CODE (from))
7019 return 0;
7021 if (TREE_CODE (from) == OFFSET_TYPE
7022 && same_type_p (TYPE_OFFSET_BASETYPE (from),
7023 TYPE_OFFSET_BASETYPE (to)))
7024 continue;
7026 if (TREE_CODE (to) != POINTER_TYPE)
7027 return same_type_p (TYPE_MAIN_VARIANT (to),
7028 TYPE_MAIN_VARIANT (from));
7032 /* Like comp_ptr_ttypes, for reinterpret_cast. */
7034 static int
7035 comp_ptr_ttypes_reinterpret (to, from)
7036 tree to, from;
7038 int constp = 1;
7040 for (; ; to = TREE_TYPE (to), from = TREE_TYPE (from))
7042 if (TREE_CODE (from) == OFFSET_TYPE)
7043 from = TREE_TYPE (from);
7044 if (TREE_CODE (to) == OFFSET_TYPE)
7045 to = TREE_TYPE (to);
7047 /* Const and volatile mean something different for function types,
7048 so the usual checks are not appropriate. */
7049 if (TREE_CODE (from) != FUNCTION_TYPE && TREE_CODE (from) != METHOD_TYPE
7050 && TREE_CODE (to) != FUNCTION_TYPE && TREE_CODE (to) != METHOD_TYPE)
7052 if (!at_least_as_qualified_p (to, from))
7053 return 0;
7055 if (! constp
7056 && !at_least_as_qualified_p (from, to))
7057 return 0;
7058 constp &= TYPE_READONLY (to);
7061 if (TREE_CODE (from) != POINTER_TYPE
7062 || TREE_CODE (to) != POINTER_TYPE)
7063 return 1;
7067 /* Recursively examines the array elements of TYPE, until a non-array
7068 element type is found. */
7070 tree
7071 strip_array_types (type)
7072 tree type;
7074 while (TREE_CODE (type) == ARRAY_TYPE)
7075 type = TREE_TYPE (type);
7077 return type;
7080 /* Returns the type-qualifier set corresponding to TYPE. */
7083 cp_type_quals (type)
7084 tree type;
7086 return TYPE_QUALS (strip_array_types (type));
7089 /* Returns non-zero if the TYPE contains a mutable member */
7092 cp_has_mutable_p (type)
7093 tree type;
7095 while (TREE_CODE (type) == ARRAY_TYPE)
7096 type = TREE_TYPE (type);
7098 return CLASS_TYPE_P (type) && CLASSTYPE_HAS_MUTABLE (type);
7101 /* Subroutine of casts_away_constness. Make T1 and T2 point at
7102 exemplar types such that casting T1 to T2 is casting away castness
7103 if and only if there is no implicit conversion from T1 to T2. */
7105 static void
7106 casts_away_constness_r (t1, t2)
7107 tree *t1;
7108 tree *t2;
7110 int quals1;
7111 int quals2;
7113 /* [expr.const.cast]
7115 For multi-level pointer to members and multi-level mixed pointers
7116 and pointers to members (conv.qual), the "member" aspect of a
7117 pointer to member level is ignored when determining if a const
7118 cv-qualifier has been cast away. */
7119 if (TYPE_PTRMEM_P (*t1))
7120 *t1 = build_pointer_type (TREE_TYPE (TREE_TYPE (*t1)));
7121 if (TYPE_PTRMEM_P (*t2))
7122 *t2 = build_pointer_type (TREE_TYPE (TREE_TYPE (*t2)));
7124 /* [expr.const.cast]
7126 For two pointer types:
7128 X1 is T1cv1,1 * ... cv1,N * where T1 is not a pointer type
7129 X2 is T2cv2,1 * ... cv2,M * where T2 is not a pointer type
7130 K is min(N,M)
7132 casting from X1 to X2 casts away constness if, for a non-pointer
7133 type T there does not exist an implicit conversion (clause
7134 _conv_) from:
7136 Tcv1,(N-K+1) * cv1,(N-K+2) * ... cv1,N *
7140 Tcv2,(M-K+1) * cv2,(M-K+2) * ... cv2,M *. */
7142 if (TREE_CODE (*t1) != POINTER_TYPE
7143 || TREE_CODE (*t2) != POINTER_TYPE)
7145 *t1 = cp_build_qualified_type (void_type_node,
7146 CP_TYPE_QUALS (*t1));
7147 *t2 = cp_build_qualified_type (void_type_node,
7148 CP_TYPE_QUALS (*t2));
7149 return;
7152 quals1 = CP_TYPE_QUALS (*t1);
7153 quals2 = CP_TYPE_QUALS (*t2);
7154 *t1 = TREE_TYPE (*t1);
7155 *t2 = TREE_TYPE (*t2);
7156 casts_away_constness_r (t1, t2);
7157 *t1 = build_pointer_type (*t1);
7158 *t2 = build_pointer_type (*t2);
7159 *t1 = cp_build_qualified_type (*t1, quals1);
7160 *t2 = cp_build_qualified_type (*t2, quals2);
7163 /* Returns non-zero if casting from TYPE1 to TYPE2 casts away
7164 constness. */
7166 static int
7167 casts_away_constness (t1, t2)
7168 tree t1;
7169 tree t2;
7171 if (TREE_CODE (t2) == REFERENCE_TYPE)
7173 /* [expr.const.cast]
7175 Casting from an lvalue of type T1 to an lvalue of type T2
7176 using a reference cast casts away constness if a cast from an
7177 rvalue of type "pointer to T1" to the type "pointer to T2"
7178 casts away constness. */
7179 t1 = (TREE_CODE (t1) == REFERENCE_TYPE
7180 ? TREE_TYPE (t1) : t1);
7181 return casts_away_constness (build_pointer_type (t1),
7182 build_pointer_type (TREE_TYPE (t2)));
7185 if (TYPE_PTRMEM_P (t1) && TYPE_PTRMEM_P (t2))
7186 /* [expr.const.cast]
7188 Casting from an rvalue of type "pointer to data member of X
7189 of type T1" to the type "pointer to data member of Y of type
7190 T2" casts away constness if a cast from an rvalue of type
7191 "poitner to T1" to the type "pointer to T2" casts away
7192 constness. */
7193 return casts_away_constness
7194 (build_pointer_type (TREE_TYPE (TREE_TYPE (t1))),
7195 build_pointer_type (TREE_TYPE (TREE_TYPE (t2))));
7197 /* Casting away constness is only something that makes sense for
7198 pointer or reference types. */
7199 if (TREE_CODE (t1) != POINTER_TYPE
7200 || TREE_CODE (t2) != POINTER_TYPE)
7201 return 0;
7203 /* Top-level qualifiers don't matter. */
7204 t1 = TYPE_MAIN_VARIANT (t1);
7205 t2 = TYPE_MAIN_VARIANT (t2);
7206 casts_away_constness_r (&t1, &t2);
7207 if (!can_convert (t2, t1))
7208 return 1;
7210 return 0;