* configure.in: Arrange to include defaults.h in [ht]config.h/tm.h.
[official-gcc.git] / gcc / cp / typeck.c
blobef2a9217a81d5dda95c1c0c939e66dfa12d448ab
1 /* Build expressions with type checking for C++ compiler.
2 Copyright (C) 1987, 1988, 1989, 1992, 1993, 1994, 1995, 1996, 1997, 1998,
3 1999, 2000, 2001 Free Software Foundation, Inc.
4 Hacked by Michael Tiemann (tiemann@cygnus.com)
6 This file is part of GNU CC.
8 GNU CC is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2, or (at your option)
11 any later version.
13 GNU CC is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with GNU CC; see the file COPYING. If not, write to
20 the Free Software Foundation, 59 Temple Place - Suite 330,
21 Boston, MA 02111-1307, USA. */
24 /* This file is part of the C++ front end.
25 It contains routines to build C++ expressions given their operands,
26 including computing the types of the result, C and C++ specific error
27 checks, and some optimization.
29 There are also routines to build RETURN_STMT nodes and CASE_STMT nodes,
30 and to process initializations in declarations (since they work
31 like a strange sort of assignment). */
33 #include "config.h"
34 #include "system.h"
35 #include "tree.h"
36 #include "rtl.h"
37 #include "expr.h"
38 #include "cp-tree.h"
39 #include "tm_p.h"
40 #include "flags.h"
41 #include "output.h"
42 #include "toplev.h"
44 static tree convert_for_assignment PARAMS ((tree, tree, const char *, tree,
45 int));
46 static tree pointer_int_sum PARAMS ((enum tree_code, tree, tree));
47 static tree rationalize_conditional_expr PARAMS ((enum tree_code, tree));
48 static int comp_target_parms PARAMS ((tree, tree));
49 static int comp_ptr_ttypes_real PARAMS ((tree, tree, int));
50 static int comp_ptr_ttypes_const PARAMS ((tree, tree));
51 static int comp_ptr_ttypes_reinterpret PARAMS ((tree, tree));
52 static int comp_except_types PARAMS ((tree, tree, int));
53 static int comp_array_types PARAMS ((int (*) (tree, tree, int), tree,
54 tree, int));
55 static tree common_base_type PARAMS ((tree, tree));
56 static tree lookup_anon_field PARAMS ((tree, tree));
57 static tree pointer_diff PARAMS ((tree, tree, tree));
58 static tree build_component_addr PARAMS ((tree, tree));
59 static tree qualify_type_recursive PARAMS ((tree, tree));
60 static tree get_delta_difference PARAMS ((tree, tree, int));
61 static int comp_cv_target_types PARAMS ((tree, tree, int));
62 static void casts_away_constness_r PARAMS ((tree *, tree *));
63 static int casts_away_constness PARAMS ((tree, tree));
64 static void maybe_warn_about_returning_address_of_local PARAMS ((tree));
65 static tree strip_all_pointer_quals PARAMS ((tree));
67 /* Return the target type of TYPE, which means return T for:
68 T*, T&, T[], T (...), and otherwise, just T. */
70 tree
71 target_type (type)
72 tree type;
74 if (TREE_CODE (type) == REFERENCE_TYPE)
75 type = TREE_TYPE (type);
76 while (TREE_CODE (type) == POINTER_TYPE
77 || TREE_CODE (type) == ARRAY_TYPE
78 || TREE_CODE (type) == FUNCTION_TYPE
79 || TREE_CODE (type) == METHOD_TYPE
80 || TREE_CODE (type) == OFFSET_TYPE)
81 type = TREE_TYPE (type);
82 return type;
85 /* Do `exp = require_complete_type (exp);' to make sure exp
86 does not have an incomplete type. (That includes void types.)
87 Returns the error_mark_node if the VALUE does not have
88 complete type when this function returns. */
90 tree
91 require_complete_type (value)
92 tree value;
94 tree type;
96 if (processing_template_decl || value == error_mark_node)
97 return value;
99 if (TREE_CODE (value) == OVERLOAD)
100 type = unknown_type_node;
101 else
102 type = TREE_TYPE (value);
104 /* First, detect a valid value with a complete type. */
105 if (COMPLETE_TYPE_P (type))
106 return value;
108 /* If we see X::Y, we build an OFFSET_TYPE which has
109 not been laid out. Try to avoid an error by interpreting
110 it as this->X::Y, if reasonable. */
111 if (TREE_CODE (value) == OFFSET_REF
112 && current_class_ref != 0
113 && TREE_OPERAND (value, 0) == current_class_ref)
115 tree base, member = TREE_OPERAND (value, 1);
116 tree basetype = TYPE_OFFSET_BASETYPE (type);
117 my_friendly_assert (TREE_CODE (member) == FIELD_DECL, 305);
118 base = convert_pointer_to (basetype, current_class_ptr);
119 value = build (COMPONENT_REF, TREE_TYPE (member),
120 build_indirect_ref (base, NULL_PTR), member);
121 return require_complete_type (value);
124 if (complete_type_or_else (type, value))
125 return value;
126 else
127 return error_mark_node;
130 /* Try to complete TYPE, if it is incomplete. For example, if TYPE is
131 a template instantiation, do the instantiation. Returns TYPE,
132 whether or not it could be completed, unless something goes
133 horribly wrong, in which case the error_mark_node is returned. */
135 tree
136 complete_type (type)
137 tree type;
139 if (type == NULL_TREE)
140 /* Rather than crash, we return something sure to cause an error
141 at some point. */
142 return error_mark_node;
144 if (type == error_mark_node || COMPLETE_TYPE_P (type))
146 else if (TREE_CODE (type) == ARRAY_TYPE && TYPE_DOMAIN (type))
148 tree t = complete_type (TREE_TYPE (type));
149 if (COMPLETE_TYPE_P (t) && ! processing_template_decl)
150 layout_type (type);
151 TYPE_NEEDS_CONSTRUCTING (type)
152 = TYPE_NEEDS_CONSTRUCTING (TYPE_MAIN_VARIANT (t));
153 TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type)
154 = TYPE_HAS_NONTRIVIAL_DESTRUCTOR (TYPE_MAIN_VARIANT (t));
156 else if (CLASS_TYPE_P (type) && CLASSTYPE_TEMPLATE_INSTANTIATION (type))
157 instantiate_class_template (TYPE_MAIN_VARIANT (type));
159 return type;
162 /* Like complete_type, but issue an error if the TYPE cannot be
163 completed. VALUE is used for informative diagnostics.
164 Returns NULL_TREE if the type cannot be made complete. */
166 tree
167 complete_type_or_else (type, value)
168 tree type;
169 tree value;
171 type = complete_type (type);
172 if (type == error_mark_node)
173 /* We already issued an error. */
174 return NULL_TREE;
175 else if (!COMPLETE_TYPE_P (type))
177 incomplete_type_error (value, type);
178 return NULL_TREE;
180 else
181 return type;
184 /* Return truthvalue of whether type of EXP is instantiated. */
187 type_unknown_p (exp)
188 tree exp;
190 return (TREE_CODE (exp) == OVERLOAD
191 || TREE_CODE (exp) == TREE_LIST
192 || TREE_TYPE (exp) == unknown_type_node
193 || (TREE_CODE (TREE_TYPE (exp)) == OFFSET_TYPE
194 && TREE_TYPE (TREE_TYPE (exp)) == unknown_type_node));
197 /* Return a pointer or pointer to member type similar to T1, with a
198 cv-qualification signature that is the union of the cv-qualification
199 signatures of T1 and T2: [expr.rel], [expr.eq]. */
201 static tree
202 qualify_type_recursive (t1, t2)
203 tree t1, t2;
205 if ((TYPE_PTR_P (t1) && TYPE_PTR_P (t2))
206 || (TYPE_PTRMEM_P (t1) && TYPE_PTRMEM_P (t2)))
208 tree tt1 = TREE_TYPE (t1);
209 tree tt2 = TREE_TYPE (t2);
210 tree b1;
211 int type_quals;
212 tree target;
213 tree attributes = merge_machine_type_attributes (t1, t2);
215 if (TREE_CODE (tt1) == OFFSET_TYPE)
217 b1 = TYPE_OFFSET_BASETYPE (tt1);
218 tt1 = TREE_TYPE (tt1);
219 tt2 = TREE_TYPE (tt2);
221 else
222 b1 = NULL_TREE;
224 type_quals = (CP_TYPE_QUALS (tt1) | CP_TYPE_QUALS (tt2));
225 target = qualify_type_recursive (tt1, tt2);
226 target = cp_build_qualified_type (target, type_quals);
227 if (b1)
228 target = build_offset_type (b1, target);
229 t1 = build_pointer_type (target);
230 t1 = build_type_attribute_variant (t1, attributes);
232 return t1;
235 /* Return the common type of two parameter lists.
236 We assume that comptypes has already been done and returned 1;
237 if that isn't so, this may crash.
239 As an optimization, free the space we allocate if the parameter
240 lists are already common. */
242 tree
243 commonparms (p1, p2)
244 tree p1, p2;
246 tree oldargs = p1, newargs, n;
247 int i, len;
248 int any_change = 0;
250 len = list_length (p1);
251 newargs = tree_last (p1);
253 if (newargs == void_list_node)
254 i = 1;
255 else
257 i = 0;
258 newargs = 0;
261 for (; i < len; i++)
262 newargs = tree_cons (NULL_TREE, NULL_TREE, newargs);
264 n = newargs;
266 for (i = 0; p1;
267 p1 = TREE_CHAIN (p1), p2 = TREE_CHAIN (p2), n = TREE_CHAIN (n), i++)
269 if (TREE_PURPOSE (p1) && !TREE_PURPOSE (p2))
271 TREE_PURPOSE (n) = TREE_PURPOSE (p1);
272 any_change = 1;
274 else if (! TREE_PURPOSE (p1))
276 if (TREE_PURPOSE (p2))
278 TREE_PURPOSE (n) = TREE_PURPOSE (p2);
279 any_change = 1;
282 else
284 if (1 != simple_cst_equal (TREE_PURPOSE (p1), TREE_PURPOSE (p2)))
285 any_change = 1;
286 TREE_PURPOSE (n) = TREE_PURPOSE (p2);
288 if (TREE_VALUE (p1) != TREE_VALUE (p2))
290 any_change = 1;
291 TREE_VALUE (n) = common_type (TREE_VALUE (p1), TREE_VALUE (p2));
293 else
294 TREE_VALUE (n) = TREE_VALUE (p1);
296 if (! any_change)
297 return oldargs;
299 return newargs;
302 /* Given a type, perhaps copied for a typedef,
303 find the "original" version of it. */
304 tree
305 original_type (t)
306 tree t;
308 while (TYPE_NAME (t) != NULL_TREE)
310 tree x = TYPE_NAME (t);
311 if (TREE_CODE (x) != TYPE_DECL)
312 break;
313 x = DECL_ORIGINAL_TYPE (x);
314 if (x == NULL_TREE)
315 break;
316 t = x;
318 return t;
321 /* T1 and T2 are arithmetic or enumeration types. Return the type
322 that will result from the "usual arithmetic converions" on T1 and
323 T2 as described in [expr]. */
325 tree
326 type_after_usual_arithmetic_conversions (t1, t2)
327 tree t1;
328 tree t2;
330 enum tree_code code1 = TREE_CODE (t1);
331 enum tree_code code2 = TREE_CODE (t2);
332 tree attributes;
334 /* FIXME: Attributes. */
335 my_friendly_assert (ARITHMETIC_TYPE_P (t1)
336 || TREE_CODE (t1) == ENUMERAL_TYPE,
337 19990725);
338 my_friendly_assert (ARITHMETIC_TYPE_P (t2)
339 || TREE_CODE (t2) == ENUMERAL_TYPE,
340 19990725);
342 /* In what follows, we slightly generalize the rules given in [expr]
343 so as to deal with `long long'. First, merge the attributes. */
344 attributes = merge_machine_type_attributes (t1, t2);
346 /* If only one is real, use it as the result. */
347 if (code1 == REAL_TYPE && code2 != REAL_TYPE)
348 return build_type_attribute_variant (t1, attributes);
349 if (code2 == REAL_TYPE && code1 != REAL_TYPE)
350 return build_type_attribute_variant (t2, attributes);
352 /* Perform the integral promotions. */
353 if (code1 != REAL_TYPE)
355 t1 = type_promotes_to (t1);
356 t2 = type_promotes_to (t2);
359 /* Both real or both integers; use the one with greater precision. */
360 if (TYPE_PRECISION (t1) > TYPE_PRECISION (t2))
361 return build_type_attribute_variant (t1, attributes);
362 else if (TYPE_PRECISION (t2) > TYPE_PRECISION (t1))
363 return build_type_attribute_variant (t2, attributes);
365 if (code1 != REAL_TYPE)
367 /* If one is a sizetype, use it so size_binop doesn't blow up. */
368 if (TYPE_IS_SIZETYPE (t1) > TYPE_IS_SIZETYPE (t2))
369 return build_type_attribute_variant (t1, attributes);
370 if (TYPE_IS_SIZETYPE (t2) > TYPE_IS_SIZETYPE (t1))
371 return build_type_attribute_variant (t2, attributes);
373 /* If one is unsigned long long, then convert the other to unsigned
374 long long. */
375 if (same_type_p (TYPE_MAIN_VARIANT (t1), long_long_unsigned_type_node)
376 || same_type_p (TYPE_MAIN_VARIANT (t2), long_long_unsigned_type_node))
377 return build_type_attribute_variant (long_long_unsigned_type_node,
378 attributes);
379 /* If one is a long long, and the other is an unsigned long, and
380 long long can represent all the values of an unsigned long, then
381 convert to a long long. Otherwise, convert to an unsigned long
382 long. Otherwise, if either operand is long long, convert the
383 other to long long.
385 Since we're here, we know the TYPE_PRECISION is the same;
386 therefore converting to long long cannot represent all the values
387 of an unsigned long, so we choose unsigned long long in that
388 case. */
389 if (same_type_p (TYPE_MAIN_VARIANT (t1), long_long_integer_type_node)
390 || same_type_p (TYPE_MAIN_VARIANT (t2), long_long_integer_type_node))
392 tree t = ((TREE_UNSIGNED (t1) || TREE_UNSIGNED (t2))
393 ? long_long_unsigned_type_node
394 : long_long_integer_type_node);
395 return build_type_attribute_variant (t, attributes);
398 /* Go through the same procedure, but for longs. */
399 if (same_type_p (TYPE_MAIN_VARIANT (t1), long_unsigned_type_node)
400 || same_type_p (TYPE_MAIN_VARIANT (t2), long_unsigned_type_node))
401 return build_type_attribute_variant (long_unsigned_type_node,
402 attributes);
403 if (same_type_p (TYPE_MAIN_VARIANT (t1), long_integer_type_node)
404 || same_type_p (TYPE_MAIN_VARIANT (t2), long_integer_type_node))
406 tree t = ((TREE_UNSIGNED (t1) || TREE_UNSIGNED (t2))
407 ? long_unsigned_type_node : long_integer_type_node);
408 return build_type_attribute_variant (t, attributes);
410 /* Otherwise prefer the unsigned one. */
411 if (TREE_UNSIGNED (t1))
412 return build_type_attribute_variant (t1, attributes);
413 else
414 return build_type_attribute_variant (t2, attributes);
416 else
418 if (same_type_p (TYPE_MAIN_VARIANT (t1), long_double_type_node)
419 || same_type_p (TYPE_MAIN_VARIANT (t2), long_double_type_node))
420 return build_type_attribute_variant (long_double_type_node,
421 attributes);
422 if (same_type_p (TYPE_MAIN_VARIANT (t1), double_type_node)
423 || same_type_p (TYPE_MAIN_VARIANT (t2), double_type_node))
424 return build_type_attribute_variant (double_type_node,
425 attributes);
426 else
427 return build_type_attribute_variant (float_type_node,
428 attributes);
432 /* Return the composite pointer type (see [expr.rel]) for T1 and T2.
433 ARG1 and ARG2 are the values with those types. The LOCATION is a
434 string describing the current location, in case an error occurs. */
436 tree
437 composite_pointer_type (t1, t2, arg1, arg2, location)
438 tree t1;
439 tree t2;
440 tree arg1;
441 tree arg2;
442 const char* location;
444 tree result_type;
446 /* [expr.rel]
448 If one operand is a null pointer constant, the composite pointer
449 type is the type of the other operand. */
450 if (null_ptr_cst_p (arg1))
451 return t2;
452 if (null_ptr_cst_p (arg2))
453 return t1;
455 /* Deal with pointer-to-member functions in the same way as we deal
456 with pointers to functions. */
457 if (TYPE_PTRMEMFUNC_P (t1))
458 t1 = TYPE_PTRMEMFUNC_FN_TYPE (t1);
459 if (TYPE_PTRMEMFUNC_P (t2))
460 t2 = TYPE_PTRMEMFUNC_FN_TYPE (t2);
462 /* We have:
464 [expr.rel]
466 If one of the operands has type "pointer to cv1 void*", then
467 the other has type "pointer to cv2T", and the composite pointer
468 type is "pointer to cv12 void", where cv12 is the union of cv1
469 and cv2.
471 If either type is a pointer to void, make sure it is T1. */
472 if (VOID_TYPE_P (TREE_TYPE (t2)))
474 tree t;
475 t = t1;
476 t1 = t2;
477 t2 = t;
479 /* Now, if T1 is a pointer to void, merge the qualifiers. */
480 if (VOID_TYPE_P (TREE_TYPE (t1)))
482 if (pedantic && TYPE_PTRFN_P (t2))
483 pedwarn ("ISO C++ forbids %s between pointer of type `void *' and pointer-to-function", location);
484 t1 = TREE_TYPE (t1);
485 t2 = TREE_TYPE (t2);
486 result_type = cp_build_qualified_type (void_type_node,
487 (CP_TYPE_QUALS (t1)
488 | CP_TYPE_QUALS (t2)));
489 result_type = build_pointer_type (result_type);
491 else
493 tree full1 = qualify_type_recursive (t1, t2);
494 tree full2 = qualify_type_recursive (t2, t1);
496 int val = comp_target_types (full1, full2, 1);
498 if (val > 0)
499 result_type = full1;
500 else if (val < 0)
501 result_type = full2;
502 else
504 cp_pedwarn ("%s between distinct pointer types `%T' and `%T' lacks a cast",
505 location, t1, t2);
506 result_type = ptr_type_node;
510 return result_type;
513 /* Return the common type of two types.
514 We assume that comptypes has already been done and returned 1;
515 if that isn't so, this may crash.
517 This is the type for the result of most arithmetic operations
518 if the operands have the given two types.
520 We do not deal with enumeral types here because they have already been
521 converted to integer types. */
523 tree
524 common_type (t1, t2)
525 tree t1, t2;
527 register enum tree_code code1;
528 register enum tree_code code2;
529 tree attributes;
531 /* Save time if the two types are the same. */
532 if (t1 == t2)
533 return t1;
534 t1 = original_type (t1);
535 t2 = original_type (t2);
536 if (t1 == t2)
537 return t1;
539 /* If one type is nonsense, use the other. */
540 if (t1 == error_mark_node)
541 return t2;
542 if (t2 == error_mark_node)
543 return t1;
545 if ((ARITHMETIC_TYPE_P (t1) || TREE_CODE (t1) == ENUMERAL_TYPE)
546 && (ARITHMETIC_TYPE_P (t2) || TREE_CODE (t2) == ENUMERAL_TYPE))
547 return type_after_usual_arithmetic_conversions (t1, t2);
549 /* Merge the attributes. */
550 attributes = merge_machine_type_attributes (t1, t2);
552 /* Treat an enum type as the unsigned integer type of the same width. */
554 if (TREE_CODE (t1) == ENUMERAL_TYPE)
555 t1 = type_for_size (TYPE_PRECISION (t1), 1);
556 if (TREE_CODE (t2) == ENUMERAL_TYPE)
557 t2 = type_for_size (TYPE_PRECISION (t2), 1);
559 if (TYPE_PTRMEMFUNC_P (t1))
560 t1 = TYPE_PTRMEMFUNC_FN_TYPE (t1);
561 if (TYPE_PTRMEMFUNC_P (t2))
562 t2 = TYPE_PTRMEMFUNC_FN_TYPE (t2);
564 code1 = TREE_CODE (t1);
565 code2 = TREE_CODE (t2);
567 /* If one type is complex, form the common type of the non-complex
568 components, then make that complex. Use T1 or T2 if it is the
569 required type. */
570 if (code1 == COMPLEX_TYPE || code2 == COMPLEX_TYPE)
572 tree subtype1 = code1 == COMPLEX_TYPE ? TREE_TYPE (t1) : t1;
573 tree subtype2 = code2 == COMPLEX_TYPE ? TREE_TYPE (t2) : t2;
574 tree subtype = common_type (subtype1, subtype2);
576 if (code1 == COMPLEX_TYPE && TREE_TYPE (t1) == subtype)
577 return build_type_attribute_variant (t1, attributes);
578 else if (code2 == COMPLEX_TYPE && TREE_TYPE (t2) == subtype)
579 return build_type_attribute_variant (t2, attributes);
580 else
581 return build_type_attribute_variant (build_complex_type (subtype),
582 attributes);
585 switch (code1)
587 case INTEGER_TYPE:
588 case REAL_TYPE:
589 /* We should have called type_after_usual_arithmetic_conversions
590 above. */
591 my_friendly_abort (19990725);
592 break;
594 case POINTER_TYPE:
595 case REFERENCE_TYPE:
596 /* For two pointers, do this recursively on the target type,
597 and combine the qualifiers of the two types' targets. */
598 /* This code was turned off; I don't know why.
599 But ISO C++ specifies doing this with the qualifiers.
600 So I turned it on again. */
602 tree tt1 = TREE_TYPE (t1);
603 tree tt2 = TREE_TYPE (t2);
604 tree b1, b2;
605 int type_quals;
606 tree target;
608 if (TREE_CODE (tt1) == OFFSET_TYPE)
610 b1 = TYPE_OFFSET_BASETYPE (tt1);
611 b2 = TYPE_OFFSET_BASETYPE (tt2);
612 tt1 = TREE_TYPE (tt1);
613 tt2 = TREE_TYPE (tt2);
615 else
616 b1 = b2 = NULL_TREE;
618 type_quals = (CP_TYPE_QUALS (tt1) | CP_TYPE_QUALS (tt2));
619 tt1 = TYPE_MAIN_VARIANT (tt1);
620 tt2 = TYPE_MAIN_VARIANT (tt2);
622 if (tt1 == tt2)
623 target = tt1;
624 else if (VOID_TYPE_P (tt1) || VOID_TYPE_P (tt2))
625 target = void_type_node;
626 else if (tt1 == unknown_type_node)
627 target = tt2;
628 else if (tt2 == unknown_type_node)
629 target = tt1;
630 else
631 target = common_type (tt1, tt2);
633 target = cp_build_qualified_type (target, type_quals);
635 if (b1)
637 if (same_type_p (b1, b2)
638 || (DERIVED_FROM_P (b1, b2) && binfo_or_else (b1, b2)))
639 target = build_offset_type (b2, target);
640 else if (binfo_or_else (b2, b1))
641 target = build_offset_type (b1, target);
644 if (code1 == POINTER_TYPE)
645 t1 = build_pointer_type (target);
646 else
647 t1 = build_reference_type (target);
648 t1 = build_type_attribute_variant (t1, attributes);
650 if (TREE_CODE (target) == METHOD_TYPE)
651 t1 = build_ptrmemfunc_type (t1);
653 return t1;
656 case ARRAY_TYPE:
658 tree elt = common_type (TREE_TYPE (t1), TREE_TYPE (t2));
659 /* Save space: see if the result is identical to one of the args. */
660 if (elt == TREE_TYPE (t1) && TYPE_DOMAIN (t1))
661 return build_type_attribute_variant (t1, attributes);
662 if (elt == TREE_TYPE (t2) && TYPE_DOMAIN (t2))
663 return build_type_attribute_variant (t2, attributes);
664 /* Merge the element types, and have a size if either arg has one. */
665 t1 = build_cplus_array_type
666 (elt, TYPE_DOMAIN (TYPE_DOMAIN (t1) ? t1 : t2));
667 return build_type_attribute_variant (t1, attributes);
670 case FUNCTION_TYPE:
671 /* Function types: prefer the one that specified arg types.
672 If both do, merge the arg types. Also merge the return types. */
674 tree valtype = common_type (TREE_TYPE (t1), TREE_TYPE (t2));
675 tree p1 = TYPE_ARG_TYPES (t1);
676 tree p2 = TYPE_ARG_TYPES (t2);
677 tree rval, raises;
679 /* Save space: see if the result is identical to one of the args. */
680 if (valtype == TREE_TYPE (t1) && ! p2)
681 return build_type_attribute_variant (t1, attributes);
682 if (valtype == TREE_TYPE (t2) && ! p1)
683 return build_type_attribute_variant (t2, attributes);
685 /* Simple way if one arg fails to specify argument types. */
686 if (p1 == NULL_TREE || TREE_VALUE (p1) == void_type_node)
688 rval = build_function_type (valtype, p2);
689 if ((raises = TYPE_RAISES_EXCEPTIONS (t2)))
690 rval = build_exception_variant (rval, raises);
691 return build_type_attribute_variant (rval, attributes);
693 raises = TYPE_RAISES_EXCEPTIONS (t1);
694 if (p2 == NULL_TREE || TREE_VALUE (p2) == void_type_node)
696 rval = build_function_type (valtype, p1);
697 if (raises)
698 rval = build_exception_variant (rval, raises);
699 return build_type_attribute_variant (rval, attributes);
702 rval = build_function_type (valtype, commonparms (p1, p2));
703 rval = build_exception_variant (rval, raises);
704 return build_type_attribute_variant (rval, attributes);
707 case RECORD_TYPE:
708 case UNION_TYPE:
709 t1 = TYPE_MAIN_VARIANT (t1);
710 t2 = TYPE_MAIN_VARIANT (t2);
712 if (DERIVED_FROM_P (t1, t2) && binfo_or_else (t1, t2))
713 return build_type_attribute_variant (t1, attributes);
714 else if (binfo_or_else (t2, t1))
715 return build_type_attribute_variant (t2, attributes);
716 else
718 compiler_error ("common_type called with uncommon aggregate types");
719 return error_mark_node;
722 case METHOD_TYPE:
723 if (TREE_CODE (TREE_TYPE (t1)) == TREE_CODE (TREE_TYPE (t2)))
725 /* Get this value the long way, since TYPE_METHOD_BASETYPE
726 is just the main variant of this. */
727 tree basetype;
728 tree raises, t3;
730 tree b1 = TYPE_OFFSET_BASETYPE (t1);
731 tree b2 = TYPE_OFFSET_BASETYPE (t2);
733 if (same_type_p (b1, b2)
734 || (DERIVED_FROM_P (b1, b2) && binfo_or_else (b1, b2)))
735 basetype = TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (t2)));
736 else
738 if (binfo_or_else (b2, b1) == NULL_TREE)
739 compiler_error ("common_type called with uncommon method types");
740 basetype = TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (t1)));
743 raises = TYPE_RAISES_EXCEPTIONS (t1);
745 /* If this was a member function type, get back to the
746 original type of type member function (i.e., without
747 the class instance variable up front. */
748 t1 = build_function_type (TREE_TYPE (t1),
749 TREE_CHAIN (TYPE_ARG_TYPES (t1)));
750 t2 = build_function_type (TREE_TYPE (t2),
751 TREE_CHAIN (TYPE_ARG_TYPES (t2)));
752 t3 = common_type (t1, t2);
753 t3 = build_cplus_method_type (basetype, TREE_TYPE (t3),
754 TYPE_ARG_TYPES (t3));
755 t1 = build_exception_variant (t3, raises);
757 else
758 compiler_error ("common_type called with uncommon method types");
760 return build_type_attribute_variant (t1, attributes);
762 case OFFSET_TYPE:
763 /* Pointers to members should now be handled by the POINTER_TYPE
764 case above. */
765 my_friendly_abort (990325);
767 default:
768 return build_type_attribute_variant (t1, attributes);
772 /* Compare two exception specifier types for exactness or subsetness, if
773 allowed. Returns 0 for mismatch, 1 for same, 2 if B is allowed by A.
775 [except.spec] "If a class X ... objects of class X or any class publicly
776 and unambigously derrived from X. Similarly, if a pointer type Y * ...
777 exceptions of type Y * or that are pointers to any type publicly and
778 unambigously derrived from Y. Otherwise a function only allows exceptions
779 that have the same type ..."
780 This does not mention cv qualifiers and is different to what throw
781 [except.throw] and catch [except.catch] will do. They will ignore the
782 top level cv qualifiers, and allow qualifiers in the pointer to class
783 example.
785 We implement the letter of the standard. */
787 static int
788 comp_except_types (a, b, exact)
789 tree a, b;
790 int exact;
792 if (same_type_p (a, b))
793 return 1;
794 else if (!exact)
796 if (CP_TYPE_QUALS (a) || CP_TYPE_QUALS (b))
797 return 0;
799 if (TREE_CODE (a) == POINTER_TYPE
800 && TREE_CODE (b) == POINTER_TYPE)
802 a = TREE_TYPE (a);
803 b = TREE_TYPE (b);
804 if (CP_TYPE_QUALS (a) || CP_TYPE_QUALS (b))
805 return 0;
808 if (TREE_CODE (a) != RECORD_TYPE
809 || TREE_CODE (b) != RECORD_TYPE)
810 return 0;
812 if (ACCESSIBLY_UNIQUELY_DERIVED_P (a, b))
813 return 2;
815 return 0;
818 /* Return 1 if TYPE1 and TYPE2 are equivalent exception specifiers.
819 If EXACT is 0, T2 can be a subset of T1 (according to 15.4/7),
820 otherwise it must be exact. Exception lists are unordered, but
821 we've already filtered out duplicates. Most lists will be in order,
822 we should try to make use of that. */
825 comp_except_specs (t1, t2, exact)
826 tree t1, t2;
827 int exact;
829 tree probe;
830 tree base;
831 int length = 0;
833 if (t1 == t2)
834 return 1;
836 if (t1 == NULL_TREE) /* T1 is ... */
837 return t2 == NULL_TREE || !exact;
838 if (!TREE_VALUE (t1)) /* t1 is EMPTY */
839 return t2 != NULL_TREE && !TREE_VALUE (t2);
840 if (t2 == NULL_TREE) /* T2 is ... */
841 return 0;
842 if (TREE_VALUE(t1) && !TREE_VALUE (t2)) /* T2 is EMPTY, T1 is not */
843 return !exact;
845 /* Neither set is ... or EMPTY, make sure each part of T2 is in T1.
846 Count how many we find, to determine exactness. For exact matching and
847 ordered T1, T2, this is an O(n) operation, otherwise its worst case is
848 O(nm). */
849 for (base = t1; t2 != NULL_TREE; t2 = TREE_CHAIN (t2))
851 for (probe = base; probe != NULL_TREE; probe = TREE_CHAIN (probe))
853 tree a = TREE_VALUE (probe);
854 tree b = TREE_VALUE (t2);
856 if (comp_except_types (a, b, exact))
858 if (probe == base && exact)
859 base = TREE_CHAIN (probe);
860 length++;
861 break;
864 if (probe == NULL_TREE)
865 return 0;
867 return !exact || base == NULL_TREE || length == list_length (t1);
870 /* Compare the array types T1 and T2, using CMP as the type comparison
871 function for the element types. STRICT is as for comptypes. */
873 static int
874 comp_array_types (cmp, t1, t2, strict)
875 register int (*cmp) PARAMS ((tree, tree, int));
876 tree t1, t2;
877 int strict;
879 tree d1;
880 tree d2;
882 if (t1 == t2)
883 return 1;
885 /* The type of the array elements must be the same. */
886 if (!(TREE_TYPE (t1) == TREE_TYPE (t2)
887 || (*cmp) (TREE_TYPE (t1), TREE_TYPE (t2),
888 strict & ~COMPARE_REDECLARATION)))
889 return 0;
891 d1 = TYPE_DOMAIN (t1);
892 d2 = TYPE_DOMAIN (t2);
894 if (d1 == d2)
895 return 1;
897 /* If one of the arrays is dimensionless, and the other has a
898 dimension, they are of different types. However, it is legal to
899 write:
901 extern int a[];
902 int a[3];
904 by [basic.link]:
906 declarations for an array object can specify
907 array types that differ by the presence or absence of a major
908 array bound (_dcl.array_). */
909 if (!d1 || !d2)
910 return strict & COMPARE_REDECLARATION;
912 /* Check that the dimensions are the same. */
913 return (cp_tree_equal (TYPE_MIN_VALUE (d1),
914 TYPE_MIN_VALUE (d2))
915 && cp_tree_equal (TYPE_MAX_VALUE (d1),
916 TYPE_MAX_VALUE (d2)));
919 /* Return 1 if T1 and T2 are compatible types for assignment or
920 various other operations. STRICT is a bitwise-or of the COMPARE_*
921 flags. */
924 comptypes (t1, t2, strict)
925 tree t1;
926 tree t2;
927 int strict;
929 int attrval, val;
930 int orig_strict = strict;
932 /* The special exemption for redeclaring array types without an
933 array bound only applies at the top level:
935 extern int (*i)[];
936 int (*i)[8];
938 is not legal, for example. */
939 strict &= ~COMPARE_REDECLARATION;
941 /* Suppress errors caused by previously reported errors */
942 if (t1 == t2)
943 return 1;
945 /* This should never happen. */
946 my_friendly_assert (t1 != error_mark_node, 307);
948 if (t2 == error_mark_node)
949 return 0;
951 /* If either type is the internal version of sizetype, return the
952 language version. */
953 if (TREE_CODE (t1) == INTEGER_TYPE && TYPE_IS_SIZETYPE (t1)
954 && TYPE_DOMAIN (t1) != 0)
955 t1 = TYPE_DOMAIN (t1);
957 if (TREE_CODE (t2) == INTEGER_TYPE && TYPE_IS_SIZETYPE (t2)
958 && TYPE_DOMAIN (t2) != 0)
959 t2 = TYPE_DOMAIN (t2);
961 if (strict & COMPARE_RELAXED)
963 /* Treat an enum type as the unsigned integer type of the same width. */
965 if (TREE_CODE (t1) == ENUMERAL_TYPE)
966 t1 = type_for_size (TYPE_PRECISION (t1), 1);
967 if (TREE_CODE (t2) == ENUMERAL_TYPE)
968 t2 = type_for_size (TYPE_PRECISION (t2), 1);
970 if (t1 == t2)
971 return 1;
974 if (TYPE_PTRMEMFUNC_P (t1))
975 t1 = TYPE_PTRMEMFUNC_FN_TYPE (t1);
976 if (TYPE_PTRMEMFUNC_P (t2))
977 t2 = TYPE_PTRMEMFUNC_FN_TYPE (t2);
979 /* Different classes of types can't be compatible. */
980 if (TREE_CODE (t1) != TREE_CODE (t2))
981 return 0;
983 /* Qualifiers must match. */
984 if (CP_TYPE_QUALS (t1) != CP_TYPE_QUALS (t2))
985 return 0;
986 if (strict == COMPARE_STRICT
987 && TYPE_FOR_JAVA (t1) != TYPE_FOR_JAVA (t2))
988 return 0;
990 /* Allow for two different type nodes which have essentially the same
991 definition. Note that we already checked for equality of the type
992 qualifiers (just above). */
994 if (TYPE_MAIN_VARIANT (t1) == TYPE_MAIN_VARIANT (t2))
995 return 1;
997 /* ??? COMP_TYPE_ATTRIBUTES is currently useless for variables as each
998 attribute is its own main variant (`val' will remain 0). */
999 #ifndef COMP_TYPE_ATTRIBUTES
1000 #define COMP_TYPE_ATTRIBUTES(t1,t2) 1
1001 #endif
1003 if (strict & COMPARE_NO_ATTRIBUTES)
1004 attrval = 1;
1005 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
1006 else if (! (attrval = COMP_TYPE_ATTRIBUTES (t1, t2)))
1007 return 0;
1009 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
1010 val = 0;
1012 switch (TREE_CODE (t1))
1014 case TEMPLATE_TEMPLATE_PARM:
1015 case BOUND_TEMPLATE_TEMPLATE_PARM:
1016 if (TEMPLATE_TYPE_IDX (t1) != TEMPLATE_TYPE_IDX (t2)
1017 || TEMPLATE_TYPE_LEVEL (t1) != TEMPLATE_TYPE_LEVEL (t2))
1018 return 0;
1019 if (! comp_template_parms
1020 (DECL_TEMPLATE_PARMS (TEMPLATE_TEMPLATE_PARM_TEMPLATE_DECL (t1)),
1021 DECL_TEMPLATE_PARMS (TEMPLATE_TEMPLATE_PARM_TEMPLATE_DECL (t2))))
1022 return 0;
1023 if (TREE_CODE (t1) == TEMPLATE_TEMPLATE_PARM)
1024 return 1;
1025 /* Don't check inheritance. */
1026 strict = COMPARE_STRICT;
1027 /* fall through */
1029 case RECORD_TYPE:
1030 case UNION_TYPE:
1031 if (TYPE_TEMPLATE_INFO (t1) && TYPE_TEMPLATE_INFO (t2)
1032 && (TYPE_TI_TEMPLATE (t1) == TYPE_TI_TEMPLATE (t2)
1033 || TREE_CODE (t1) == BOUND_TEMPLATE_TEMPLATE_PARM))
1034 val = comp_template_args (TYPE_TI_ARGS (t1),
1035 TYPE_TI_ARGS (t2));
1036 look_hard:
1037 if ((strict & COMPARE_BASE) && DERIVED_FROM_P (t1, t2))
1038 val = 1;
1039 else if ((strict & COMPARE_RELAXED) && DERIVED_FROM_P (t2, t1))
1040 val = 1;
1041 break;
1043 case OFFSET_TYPE:
1044 val = (comptypes (build_pointer_type (TYPE_OFFSET_BASETYPE (t1)),
1045 build_pointer_type (TYPE_OFFSET_BASETYPE (t2)), strict)
1046 && comptypes (TREE_TYPE (t1), TREE_TYPE (t2), strict));
1047 break;
1049 case METHOD_TYPE:
1050 if (! comp_except_specs (TYPE_RAISES_EXCEPTIONS (t1),
1051 TYPE_RAISES_EXCEPTIONS (t2), 1))
1052 return 0;
1054 /* This case is anti-symmetrical!
1055 One can pass a base member (or member function)
1056 to something expecting a derived member (or member function),
1057 but not vice-versa! */
1059 val = (comptypes (TREE_TYPE (t1), TREE_TYPE (t2), strict)
1060 && compparms (TYPE_ARG_TYPES (t1), TYPE_ARG_TYPES (t2)));
1061 break;
1063 case POINTER_TYPE:
1064 case REFERENCE_TYPE:
1065 t1 = TREE_TYPE (t1);
1066 t2 = TREE_TYPE (t2);
1067 /* first, check whether the referred types match with the
1068 required level of strictness */
1069 val = comptypes (t1, t2, strict);
1070 if (val)
1071 break;
1072 if (TREE_CODE (t1) == RECORD_TYPE
1073 && TREE_CODE (t2) == RECORD_TYPE)
1074 goto look_hard;
1075 break;
1077 case FUNCTION_TYPE:
1078 if (! comp_except_specs (TYPE_RAISES_EXCEPTIONS (t1),
1079 TYPE_RAISES_EXCEPTIONS (t2), 1))
1080 return 0;
1082 val = ((TREE_TYPE (t1) == TREE_TYPE (t2)
1083 || comptypes (TREE_TYPE (t1), TREE_TYPE (t2), strict))
1084 && compparms (TYPE_ARG_TYPES (t1), TYPE_ARG_TYPES (t2)));
1085 break;
1087 case ARRAY_TYPE:
1088 /* Target types must match incl. qualifiers. We use ORIG_STRICT
1089 here since this is the one place where
1090 COMPARE_REDECLARATION should be used. */
1091 val = comp_array_types (comptypes, t1, t2, orig_strict);
1092 break;
1094 case TEMPLATE_TYPE_PARM:
1095 return TEMPLATE_TYPE_IDX (t1) == TEMPLATE_TYPE_IDX (t2)
1096 && TEMPLATE_TYPE_LEVEL (t1) == TEMPLATE_TYPE_LEVEL (t2);
1098 case TYPENAME_TYPE:
1099 if (cp_tree_equal (TYPENAME_TYPE_FULLNAME (t1),
1100 TYPENAME_TYPE_FULLNAME (t2)) < 1)
1101 return 0;
1102 return same_type_p (TYPE_CONTEXT (t1), TYPE_CONTEXT (t2));
1104 case COMPLEX_TYPE:
1105 return same_type_p (TREE_TYPE (t1), TREE_TYPE (t2));
1107 default:
1108 break;
1110 return attrval == 2 && val == 1 ? 2 : val;
1113 /* Subroutine of comp_target-types. Make sure that the cv-quals change
1114 only in the same direction as the target type. */
1116 static int
1117 comp_cv_target_types (ttl, ttr, nptrs)
1118 tree ttl, ttr;
1119 int nptrs;
1121 int t;
1123 if (!at_least_as_qualified_p (ttl, ttr)
1124 && !at_least_as_qualified_p (ttr, ttl))
1125 /* The qualifications are incomparable. */
1126 return 0;
1128 if (TYPE_MAIN_VARIANT (ttl) == TYPE_MAIN_VARIANT (ttr))
1129 return more_qualified_p (ttr, ttl) ? -1 : 1;
1131 t = comp_target_types (ttl, ttr, nptrs);
1132 if ((t == 1 && at_least_as_qualified_p (ttl, ttr))
1133 || (t == -1 && at_least_as_qualified_p (ttr, ttl)))
1134 return t;
1136 return 0;
1139 /* Return 1 or -1 if TTL and TTR are pointers to types that are equivalent,
1140 ignoring their qualifiers, 0 if not. Return 1 means that TTR can be
1141 converted to TTL. Return -1 means that TTL can be converted to TTR but
1142 not vice versa.
1144 NPTRS is the number of pointers we can strip off and keep cool.
1145 This is used to permit (for aggr A, aggr B) A, B* to convert to A*,
1146 but to not permit B** to convert to A**.
1148 This should go away. Callers should use can_convert or something
1149 similar instead. (jason 17 Apr 1997) */
1152 comp_target_types (ttl, ttr, nptrs)
1153 tree ttl, ttr;
1154 int nptrs;
1156 ttl = TYPE_MAIN_VARIANT (ttl);
1157 ttr = TYPE_MAIN_VARIANT (ttr);
1158 if (same_type_p (ttl, ttr))
1159 return 1;
1161 if (TREE_CODE (ttr) != TREE_CODE (ttl))
1162 return 0;
1164 if ((TREE_CODE (ttr) == POINTER_TYPE
1165 || TREE_CODE (ttr) == REFERENCE_TYPE)
1166 /* If we get a pointer with nptrs == 0, we don't allow any tweaking
1167 of the type pointed to. This is necessary for reference init
1168 semantics. We won't get here from a previous call with nptrs == 1;
1169 for multi-level pointers we end up in comp_ptr_ttypes. */
1170 && nptrs > 0)
1172 int is_ptr = TREE_CODE (ttr) == POINTER_TYPE;
1174 ttl = TREE_TYPE (ttl);
1175 ttr = TREE_TYPE (ttr);
1177 if (is_ptr)
1179 if (TREE_CODE (ttl) == UNKNOWN_TYPE
1180 || TREE_CODE (ttr) == UNKNOWN_TYPE)
1181 return 1;
1182 else if (TREE_CODE (ttl) == VOID_TYPE
1183 && TREE_CODE (ttr) != FUNCTION_TYPE
1184 && TREE_CODE (ttr) != METHOD_TYPE
1185 && TREE_CODE (ttr) != OFFSET_TYPE)
1186 return 1;
1187 else if (TREE_CODE (ttr) == VOID_TYPE
1188 && TREE_CODE (ttl) != FUNCTION_TYPE
1189 && TREE_CODE (ttl) != METHOD_TYPE
1190 && TREE_CODE (ttl) != OFFSET_TYPE)
1191 return -1;
1192 else if (TREE_CODE (ttl) == POINTER_TYPE
1193 || TREE_CODE (ttl) == ARRAY_TYPE)
1195 if (comp_ptr_ttypes (ttl, ttr))
1196 return 1;
1197 else if (comp_ptr_ttypes (ttr, ttl))
1198 return -1;
1199 return 0;
1203 /* Const and volatile mean something different for function types,
1204 so the usual checks are not appropriate. */
1205 if (TREE_CODE (ttl) == FUNCTION_TYPE || TREE_CODE (ttl) == METHOD_TYPE)
1206 return comp_target_types (ttl, ttr, nptrs - 1);
1208 return comp_cv_target_types (ttl, ttr, nptrs - 1);
1211 if (TREE_CODE (ttr) == ARRAY_TYPE)
1212 return comp_array_types (comp_target_types, ttl, ttr, COMPARE_STRICT);
1213 else if (TREE_CODE (ttr) == FUNCTION_TYPE || TREE_CODE (ttr) == METHOD_TYPE)
1215 tree argsl, argsr;
1216 int saw_contra = 0;
1218 if (pedantic)
1220 if (!same_type_p (TREE_TYPE (ttl), TREE_TYPE (ttr)))
1221 return 0;
1223 else
1225 switch (comp_target_types (TREE_TYPE (ttl), TREE_TYPE (ttr), -1))
1227 case 0:
1228 return 0;
1229 case -1:
1230 saw_contra = 1;
1234 argsl = TYPE_ARG_TYPES (ttl);
1235 argsr = TYPE_ARG_TYPES (ttr);
1237 /* Compare 'this' here, not in comp_target_parms. */
1238 if (TREE_CODE (ttr) == METHOD_TYPE)
1240 tree tl = TYPE_METHOD_BASETYPE (ttl);
1241 tree tr = TYPE_METHOD_BASETYPE (ttr);
1243 if (!same_or_base_type_p (tr, tl))
1245 if (same_or_base_type_p (tl, tr))
1246 saw_contra = 1;
1247 else
1248 return 0;
1251 argsl = TREE_CHAIN (argsl);
1252 argsr = TREE_CHAIN (argsr);
1255 switch (comp_target_parms (argsl, argsr))
1257 case 0:
1258 return 0;
1259 case -1:
1260 saw_contra = 1;
1263 return saw_contra ? -1 : 1;
1265 /* for C++ */
1266 else if (TREE_CODE (ttr) == OFFSET_TYPE)
1268 int base;
1270 /* Contravariance: we can assign a pointer to base member to a pointer
1271 to derived member. Note difference from simple pointer case, where
1272 we can pass a pointer to derived to a pointer to base. */
1273 if (same_or_base_type_p (TYPE_OFFSET_BASETYPE (ttr),
1274 TYPE_OFFSET_BASETYPE (ttl)))
1275 base = 1;
1276 else if (same_or_base_type_p (TYPE_OFFSET_BASETYPE (ttl),
1277 TYPE_OFFSET_BASETYPE (ttr)))
1279 tree tmp = ttl;
1280 ttl = ttr;
1281 ttr = tmp;
1282 base = -1;
1284 else
1285 return 0;
1287 ttl = TREE_TYPE (ttl);
1288 ttr = TREE_TYPE (ttr);
1290 if (TREE_CODE (ttl) == POINTER_TYPE
1291 || TREE_CODE (ttl) == ARRAY_TYPE)
1293 if (comp_ptr_ttypes (ttl, ttr))
1294 return base;
1295 return 0;
1297 else
1299 if (comp_cv_target_types (ttl, ttr, nptrs) == 1)
1300 return base;
1301 return 0;
1304 else if (IS_AGGR_TYPE (ttl))
1306 if (nptrs < 0)
1307 return 0;
1308 if (same_or_base_type_p (build_pointer_type (ttl),
1309 build_pointer_type (ttr)))
1310 return 1;
1311 if (same_or_base_type_p (build_pointer_type (ttr),
1312 build_pointer_type (ttl)))
1313 return -1;
1314 return 0;
1317 return 0;
1320 /* Returns 1 if TYPE1 is at least as qualified as TYPE2. */
1323 at_least_as_qualified_p (type1, type2)
1324 tree type1;
1325 tree type2;
1327 /* All qualifiers for TYPE2 must also appear in TYPE1. */
1328 return ((CP_TYPE_QUALS (type1) & CP_TYPE_QUALS (type2))
1329 == CP_TYPE_QUALS (type2));
1332 /* Returns 1 if TYPE1 is more qualified than TYPE2. */
1335 more_qualified_p (type1, type2)
1336 tree type1;
1337 tree type2;
1339 return (CP_TYPE_QUALS (type1) != CP_TYPE_QUALS (type2)
1340 && at_least_as_qualified_p (type1, type2));
1343 /* Returns 1 if TYPE1 is more cv-qualified than TYPE2, -1 if TYPE2 is
1344 more cv-qualified that TYPE1, and 0 otherwise. */
1347 comp_cv_qualification (type1, type2)
1348 tree type1;
1349 tree type2;
1351 if (CP_TYPE_QUALS (type1) == CP_TYPE_QUALS (type2))
1352 return 0;
1354 if (at_least_as_qualified_p (type1, type2))
1355 return 1;
1357 else if (at_least_as_qualified_p (type2, type1))
1358 return -1;
1360 return 0;
1363 /* Returns 1 if the cv-qualification signature of TYPE1 is a proper
1364 subset of the cv-qualification signature of TYPE2, and the types
1365 are similar. Returns -1 if the other way 'round, and 0 otherwise. */
1368 comp_cv_qual_signature (type1, type2)
1369 tree type1;
1370 tree type2;
1372 if (comp_ptr_ttypes_real (type2, type1, -1))
1373 return 1;
1374 else if (comp_ptr_ttypes_real (type1, type2, -1))
1375 return -1;
1376 else
1377 return 0;
1380 /* If two types share a common base type, return that basetype.
1381 If there is not a unique most-derived base type, this function
1382 returns ERROR_MARK_NODE. */
1384 static tree
1385 common_base_type (tt1, tt2)
1386 tree tt1, tt2;
1388 tree best = NULL_TREE;
1389 int i;
1391 /* If one is a baseclass of another, that's good enough. */
1392 if (UNIQUELY_DERIVED_FROM_P (tt1, tt2))
1393 return tt1;
1394 if (UNIQUELY_DERIVED_FROM_P (tt2, tt1))
1395 return tt2;
1397 /* Otherwise, try to find a unique baseclass of TT1
1398 that is shared by TT2, and follow that down. */
1399 for (i = CLASSTYPE_N_BASECLASSES (tt1)-1; i >= 0; i--)
1401 tree basetype = TYPE_BINFO_BASETYPE (tt1, i);
1402 tree trial = common_base_type (basetype, tt2);
1403 if (trial)
1405 if (trial == error_mark_node)
1406 return trial;
1407 if (best == NULL_TREE)
1408 best = trial;
1409 else if (best != trial)
1410 return error_mark_node;
1414 /* Same for TT2. */
1415 for (i = CLASSTYPE_N_BASECLASSES (tt2)-1; i >= 0; i--)
1417 tree basetype = TYPE_BINFO_BASETYPE (tt2, i);
1418 tree trial = common_base_type (tt1, basetype);
1419 if (trial)
1421 if (trial == error_mark_node)
1422 return trial;
1423 if (best == NULL_TREE)
1424 best = trial;
1425 else if (best != trial)
1426 return error_mark_node;
1429 return best;
1432 /* Subroutines of `comptypes'. */
1434 /* Return 1 if two parameter type lists PARMS1 and PARMS2 are
1435 equivalent in the sense that functions with those parameter types
1436 can have equivalent types. The two lists must be equivalent,
1437 element by element.
1439 C++: See comment above about TYPE1, TYPE2. */
1442 compparms (parms1, parms2)
1443 tree parms1, parms2;
1445 register tree t1 = parms1, t2 = parms2;
1447 /* An unspecified parmlist matches any specified parmlist
1448 whose argument types don't need default promotions. */
1450 while (1)
1452 if (t1 == 0 && t2 == 0)
1453 return 1;
1454 /* If one parmlist is shorter than the other,
1455 they fail to match. */
1456 if (t1 == 0 || t2 == 0)
1457 return 0;
1458 if (!same_type_p (TREE_VALUE (t2), TREE_VALUE (t1)))
1459 return 0;
1461 t1 = TREE_CHAIN (t1);
1462 t2 = TREE_CHAIN (t2);
1466 /* This really wants return whether or not parameter type lists
1467 would make their owning functions assignment compatible or not.
1469 The return value is like for comp_target_types.
1471 This should go away, possibly with the exception of the empty parmlist
1472 conversion; there are no conversions between function types in C++.
1473 (jason 17 Apr 1997) */
1475 static int
1476 comp_target_parms (parms1, parms2)
1477 tree parms1, parms2;
1479 register tree t1 = parms1, t2 = parms2;
1480 int warn_contravariance = 0;
1482 /* In C, an unspecified parmlist matches any specified parmlist
1483 whose argument types don't need default promotions. This is not
1484 true for C++, but let's do it anyway for unfixed headers. */
1486 if (t1 == 0 && t2 != 0)
1488 cp_pedwarn ("ISO C++ prohibits conversion from `%#T' to `(...)'",
1489 parms2);
1490 return self_promoting_args_p (t2);
1492 if (t2 == 0)
1493 return self_promoting_args_p (t1);
1495 for (; t1 || t2; t1 = TREE_CHAIN (t1), t2 = TREE_CHAIN (t2))
1497 tree p1, p2;
1499 /* If one parmlist is shorter than the other,
1500 they fail to match, unless STRICT is <= 0. */
1501 if (t1 == 0 || t2 == 0)
1502 return 0;
1503 p1 = TREE_VALUE (t1);
1504 p2 = TREE_VALUE (t2);
1505 if (same_type_p (p1, p2))
1506 continue;
1508 if (pedantic)
1509 return 0;
1511 if ((TREE_CODE (p1) == POINTER_TYPE && TREE_CODE (p2) == POINTER_TYPE)
1512 || (TREE_CODE (p1) == REFERENCE_TYPE
1513 && TREE_CODE (p2) == REFERENCE_TYPE))
1515 /* The following is wrong for contravariance,
1516 but many programs depend on it. */
1517 if (TREE_TYPE (p1) == void_type_node)
1518 continue;
1519 if (TREE_TYPE (p2) == void_type_node)
1521 warn_contravariance = 1;
1522 continue;
1524 if (IS_AGGR_TYPE (TREE_TYPE (p1))
1525 && !same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (p1),
1526 TREE_TYPE (p2)))
1527 return 0;
1529 /* Note backwards order due to contravariance. */
1530 if (comp_target_types (p2, p1, 1) <= 0)
1532 if (comp_target_types (p1, p2, 1) > 0)
1534 warn_contravariance = 1;
1535 continue;
1537 return 0;
1540 return warn_contravariance ? -1 : 1;
1543 /* Compute the value of the `sizeof' operator. */
1545 tree
1546 c_sizeof (type)
1547 tree type;
1549 enum tree_code code = TREE_CODE (type);
1550 tree size;
1552 if (processing_template_decl)
1553 return build_min (SIZEOF_EXPR, sizetype, type);
1555 if (code == FUNCTION_TYPE)
1557 if (pedantic || warn_pointer_arith)
1558 pedwarn ("ISO C++ forbids applying `sizeof' to a function type");
1559 size = size_one_node;
1561 else if (code == METHOD_TYPE)
1563 if (pedantic || warn_pointer_arith)
1564 pedwarn ("ISO C++ forbids applying `sizeof' to a member function");
1565 size = size_one_node;
1567 else if (code == VOID_TYPE)
1569 if (pedantic || warn_pointer_arith)
1570 pedwarn ("ISO C++ forbids applying `sizeof' to type `void' which is an incomplete type");
1571 size = size_one_node;
1573 else if (code == ERROR_MARK)
1574 size = size_one_node;
1575 else
1577 /* ARM $5.3.2: ``When applied to a reference, the result is the
1578 size of the referenced object.'' */
1579 if (code == REFERENCE_TYPE)
1580 type = TREE_TYPE (type);
1582 if (code == OFFSET_TYPE)
1584 cp_error ("`sizeof' applied to non-static member");
1585 size = size_zero_node;
1587 else if (!COMPLETE_TYPE_P (complete_type (type)))
1589 cp_error ("`sizeof' applied to incomplete type `%T'", type);
1590 size = size_zero_node;
1592 else
1593 /* Convert in case a char is more than one unit. */
1594 size = size_binop (CEIL_DIV_EXPR, TYPE_SIZE_UNIT (type),
1595 size_int (TYPE_PRECISION (char_type_node)
1596 / BITS_PER_UNIT));
1599 /* SIZE will have an integer type with TYPE_IS_SIZETYPE set.
1600 TYPE_IS_SIZETYPE means that certain things (like overflow) will
1601 never happen. However, this node should really have type
1602 `size_t', which is just a typedef for an ordinary integer type. */
1603 size = fold (build1 (NOP_EXPR, c_size_type_node, size));
1604 my_friendly_assert (!TYPE_IS_SIZETYPE (TREE_TYPE (size)),
1605 20001021);
1606 return size;
1610 tree
1611 expr_sizeof (e)
1612 tree e;
1614 if (processing_template_decl)
1615 return build_min (SIZEOF_EXPR, sizetype, e);
1617 if (TREE_CODE (e) == COMPONENT_REF
1618 && DECL_C_BIT_FIELD (TREE_OPERAND (e, 1)))
1619 error ("sizeof applied to a bit-field");
1620 if (is_overloaded_fn (e))
1622 pedwarn ("ISO C++ forbids applying `sizeof' to an expression of function type");
1623 return c_sizeof (char_type_node);
1625 else if (type_unknown_p (e))
1627 incomplete_type_error (e, TREE_TYPE (e));
1628 return c_sizeof (char_type_node);
1630 /* It's illegal to say `sizeof (X::i)' for `i' a non-static data
1631 member unless you're in a non-static member of X. So hand off to
1632 resolve_offset_ref. [expr.prim] */
1633 else if (TREE_CODE (e) == OFFSET_REF)
1634 e = resolve_offset_ref (e);
1636 if (e == error_mark_node)
1637 return e;
1639 return c_sizeof (TREE_TYPE (e));
1642 tree
1643 c_sizeof_nowarn (type)
1644 tree type;
1646 enum tree_code code = TREE_CODE (type);
1647 tree size;
1649 if (code == FUNCTION_TYPE
1650 || code == METHOD_TYPE
1651 || code == VOID_TYPE
1652 || code == ERROR_MARK)
1653 size = size_one_node;
1654 else
1656 if (code == REFERENCE_TYPE)
1657 type = TREE_TYPE (type);
1659 if (!COMPLETE_TYPE_P (type))
1660 size = size_zero_node;
1661 else
1662 /* Convert in case a char is more than one unit. */
1663 size = size_binop (CEIL_DIV_EXPR, TYPE_SIZE_UNIT (type),
1664 size_int (TYPE_PRECISION (char_type_node)
1665 / BITS_PER_UNIT));
1668 /* SIZE will have an integer type with TYPE_IS_SIZETYPE set.
1669 TYPE_IS_SIZETYPE means that certain things (like overflow) will
1670 never happen. However, this node should really have type
1671 `size_t', which is just a typedef for an ordinary integer type. */
1672 size = fold (build1 (NOP_EXPR, c_size_type_node, size));
1673 my_friendly_assert (!TYPE_IS_SIZETYPE (TREE_TYPE (size)),
1674 20001021);
1675 return size;
1678 /* Implement the __alignof keyword: Return the minimum required
1679 alignment of TYPE, measured in bytes. */
1681 tree
1682 c_alignof (type)
1683 tree type;
1685 enum tree_code code = TREE_CODE (type);
1686 tree t;
1688 if (processing_template_decl)
1689 return build_min (ALIGNOF_EXPR, sizetype, type);
1691 if (code == FUNCTION_TYPE || code == METHOD_TYPE)
1692 t = size_int (FUNCTION_BOUNDARY / BITS_PER_UNIT);
1693 else if (code == VOID_TYPE || code == ERROR_MARK)
1694 t = size_one_node;
1695 else
1697 /* Similar to sizeof, __alignof applies to the referant. */
1698 if (code == REFERENCE_TYPE)
1699 type = TREE_TYPE (type);
1701 t = size_int (TYPE_ALIGN (type) / BITS_PER_UNIT);
1704 return fold (build1 (NOP_EXPR, c_size_type_node, t));
1707 /* Perform the array-to-pointer and function-to-pointer conversions
1708 for EXP.
1710 In addition, references are converted to lvalues and manifest
1711 constants are replaced by their values. */
1713 tree
1714 decay_conversion (exp)
1715 tree exp;
1717 register tree type;
1718 register enum tree_code code;
1720 if (TREE_CODE (exp) == OFFSET_REF)
1721 exp = resolve_offset_ref (exp);
1723 type = TREE_TYPE (exp);
1724 code = TREE_CODE (type);
1726 if (code == REFERENCE_TYPE)
1728 exp = convert_from_reference (exp);
1729 type = TREE_TYPE (exp);
1730 code = TREE_CODE (type);
1733 if (type == error_mark_node)
1734 return error_mark_node;
1736 /* Constants can be used directly unless they're not loadable. */
1737 if (TREE_CODE (exp) == CONST_DECL)
1738 exp = DECL_INITIAL (exp);
1739 /* Replace a nonvolatile const static variable with its value. We
1740 don't do this for arrays, though; we want the address of the
1741 first element of the array, not the address of the first element
1742 of its initializing constant. */
1743 else if (code != ARRAY_TYPE)
1745 exp = decl_constant_value (exp);
1746 type = TREE_TYPE (exp);
1749 /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
1750 Leave such NOP_EXPRs, since RHS is being used in non-lvalue context. */
1752 if (code == VOID_TYPE)
1754 error ("void value not ignored as it ought to be");
1755 return error_mark_node;
1757 if (code == METHOD_TYPE)
1758 my_friendly_abort (990506);
1759 if (code == FUNCTION_TYPE || is_overloaded_fn (exp))
1760 return build_unary_op (ADDR_EXPR, exp, 0);
1761 if (code == ARRAY_TYPE)
1763 register tree adr;
1764 tree ptrtype;
1766 if (TREE_CODE (exp) == INDIRECT_REF)
1768 /* Stripping away the INDIRECT_REF is not the right
1769 thing to do for references... */
1770 tree inner = TREE_OPERAND (exp, 0);
1771 if (TREE_CODE (TREE_TYPE (inner)) == REFERENCE_TYPE)
1773 inner = build1 (CONVERT_EXPR,
1774 build_pointer_type (TREE_TYPE
1775 (TREE_TYPE (inner))),
1776 inner);
1777 TREE_CONSTANT (inner) = TREE_CONSTANT (TREE_OPERAND (inner, 0));
1779 return cp_convert (build_pointer_type (TREE_TYPE (type)), inner);
1782 if (TREE_CODE (exp) == COMPOUND_EXPR)
1784 tree op1 = decay_conversion (TREE_OPERAND (exp, 1));
1785 return build (COMPOUND_EXPR, TREE_TYPE (op1),
1786 TREE_OPERAND (exp, 0), op1);
1789 if (!lvalue_p (exp)
1790 && ! (TREE_CODE (exp) == CONSTRUCTOR && TREE_STATIC (exp)))
1792 error ("invalid use of non-lvalue array");
1793 return error_mark_node;
1796 ptrtype = build_pointer_type (TREE_TYPE (type));
1798 if (TREE_CODE (exp) == VAR_DECL)
1800 /* ??? This is not really quite correct
1801 in that the type of the operand of ADDR_EXPR
1802 is not the target type of the type of the ADDR_EXPR itself.
1803 Question is, can this lossage be avoided? */
1804 adr = build1 (ADDR_EXPR, ptrtype, exp);
1805 if (mark_addressable (exp) == 0)
1806 return error_mark_node;
1807 TREE_CONSTANT (adr) = staticp (exp);
1808 TREE_SIDE_EFFECTS (adr) = 0; /* Default would be, same as EXP. */
1809 return adr;
1811 /* This way is better for a COMPONENT_REF since it can
1812 simplify the offset for a component. */
1813 adr = build_unary_op (ADDR_EXPR, exp, 1);
1814 return cp_convert (ptrtype, adr);
1817 /* [basic.lval]: Class rvalues can have cv-qualified types; non-class
1818 rvalues always have cv-unqualified types. */
1819 if (! CLASS_TYPE_P (type))
1820 exp = cp_convert (TYPE_MAIN_VARIANT (type), exp);
1822 return exp;
1825 tree
1826 default_conversion (exp)
1827 tree exp;
1829 tree type;
1830 enum tree_code code;
1832 exp = decay_conversion (exp);
1834 type = TREE_TYPE (exp);
1835 code = TREE_CODE (type);
1837 if (INTEGRAL_CODE_P (code))
1839 tree t = type_promotes_to (type);
1840 if (t != type)
1841 return cp_convert (t, exp);
1844 return exp;
1847 /* Take the address of an inline function without setting TREE_ADDRESSABLE
1848 or TREE_USED. */
1850 tree
1851 inline_conversion (exp)
1852 tree exp;
1854 if (TREE_CODE (exp) == FUNCTION_DECL)
1855 exp = build1 (ADDR_EXPR, build_pointer_type (TREE_TYPE (exp)), exp);
1857 return exp;
1860 /* Returns nonzero iff exp is a STRING_CST or the result of applying
1861 decay_conversion to one. */
1864 string_conv_p (totype, exp, warn)
1865 tree totype, exp;
1866 int warn;
1868 tree t;
1870 if (! flag_const_strings || TREE_CODE (totype) != POINTER_TYPE)
1871 return 0;
1873 t = TREE_TYPE (totype);
1874 if (!same_type_p (t, char_type_node)
1875 && !same_type_p (t, wchar_type_node))
1876 return 0;
1878 if (TREE_CODE (exp) == STRING_CST)
1880 /* Make sure that we don't try to convert between char and wchar_t. */
1881 if (!same_type_p (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (exp))), t))
1882 return 0;
1884 else
1886 /* Is this a string constant which has decayed to 'const char *'? */
1887 t = build_pointer_type (build_qualified_type (t, TYPE_QUAL_CONST));
1888 if (!same_type_p (TREE_TYPE (exp), t))
1889 return 0;
1890 STRIP_NOPS (exp);
1891 if (TREE_CODE (exp) != ADDR_EXPR
1892 || TREE_CODE (TREE_OPERAND (exp, 0)) != STRING_CST)
1893 return 0;
1896 /* This warning is not very useful, as it complains about printf. */
1897 if (warn && warn_write_strings)
1898 cp_warning ("deprecated conversion from string constant to `%T'", totype);
1900 return 1;
1903 tree
1904 build_object_ref (datum, basetype, field)
1905 tree datum, basetype, field;
1907 tree dtype;
1908 if (datum == error_mark_node)
1909 return error_mark_node;
1911 dtype = TREE_TYPE (datum);
1912 if (TREE_CODE (dtype) == REFERENCE_TYPE)
1913 dtype = TREE_TYPE (dtype);
1914 if (! IS_AGGR_TYPE_CODE (TREE_CODE (dtype)))
1916 cp_error ("request for member `%T::%D' in expression of non-aggregate type `%T'",
1917 basetype, field, dtype);
1918 return error_mark_node;
1920 else if (is_aggr_type (basetype, 1))
1922 tree binfo = binfo_or_else (basetype, dtype);
1923 if (binfo)
1924 return build_x_component_ref (build_scoped_ref (datum, basetype),
1925 field, binfo, 1);
1927 return error_mark_node;
1930 /* Like `build_component_ref, but uses an already found field, and converts
1931 from a reference. Must compute access for current_class_ref.
1932 Otherwise, ok. */
1934 tree
1935 build_component_ref_1 (datum, field, protect)
1936 tree datum, field;
1937 int protect;
1939 return convert_from_reference
1940 (build_component_ref (datum, field, NULL_TREE, protect));
1943 /* Given a COND_EXPR, MIN_EXPR, or MAX_EXPR in T, return it in a form that we
1944 can, for example, use as an lvalue. This code used to be in
1945 unary_complex_lvalue, but we needed it to deal with `a = (d == c) ? b : c'
1946 expressions, where we're dealing with aggregates. But now it's again only
1947 called from unary_complex_lvalue. The case (in particular) that led to
1948 this was with CODE == ADDR_EXPR, since it's not an lvalue when we'd
1949 get it there. */
1951 static tree
1952 rationalize_conditional_expr (code, t)
1953 enum tree_code code;
1954 tree t;
1956 /* For MIN_EXPR or MAX_EXPR, fold-const.c has arranged things so that
1957 the first operand is always the one to be used if both operands
1958 are equal, so we know what conditional expression this used to be. */
1959 if (TREE_CODE (t) == MIN_EXPR || TREE_CODE (t) == MAX_EXPR)
1961 return
1962 build_conditional_expr (build_x_binary_op ((TREE_CODE (t) == MIN_EXPR
1963 ? LE_EXPR : GE_EXPR),
1964 TREE_OPERAND (t, 0),
1965 TREE_OPERAND (t, 1)),
1966 build_unary_op (code, TREE_OPERAND (t, 0), 0),
1967 build_unary_op (code, TREE_OPERAND (t, 1), 0));
1970 return
1971 build_conditional_expr (TREE_OPERAND (t, 0),
1972 build_unary_op (code, TREE_OPERAND (t, 1), 0),
1973 build_unary_op (code, TREE_OPERAND (t, 2), 0));
1976 /* Given the TYPE of an anonymous union field inside T, return the
1977 FIELD_DECL for the field. If not found return NULL_TREE. Because
1978 anonymous unions can nest, we must also search all anonymous unions
1979 that are directly reachable. */
1981 static tree
1982 lookup_anon_field (t, type)
1983 tree t, type;
1985 tree field;
1987 for (field = TYPE_FIELDS (t); field; field = TREE_CHAIN (field))
1989 if (TREE_STATIC (field))
1990 continue;
1991 if (TREE_CODE (field) != FIELD_DECL)
1992 continue;
1994 /* If we find it directly, return the field. */
1995 if (DECL_NAME (field) == NULL_TREE
1996 && type == TYPE_MAIN_VARIANT (TREE_TYPE (field)))
1998 return field;
2001 /* Otherwise, it could be nested, search harder. */
2002 if (DECL_NAME (field) == NULL_TREE
2003 && ANON_AGGR_TYPE_P (TREE_TYPE (field)))
2005 tree subfield = lookup_anon_field (TREE_TYPE (field), type);
2006 if (subfield)
2007 return subfield;
2010 return NULL_TREE;
2013 /* Build a COMPONENT_REF for a given DATUM, and it's member COMPONENT.
2014 COMPONENT can be an IDENTIFIER_NODE that is the name of the member
2015 that we are interested in, or it can be a FIELD_DECL. */
2017 tree
2018 build_component_ref (datum, component, basetype_path, protect)
2019 tree datum, component, basetype_path;
2020 int protect;
2022 register tree basetype;
2023 register enum tree_code code;
2024 register tree field = NULL;
2025 register tree ref;
2026 tree field_type;
2027 int type_quals;
2029 if (processing_template_decl)
2030 return build_min_nt (COMPONENT_REF, datum, component);
2032 if (datum == error_mark_node
2033 || TREE_TYPE (datum) == error_mark_node)
2034 return error_mark_node;
2036 /* BASETYPE holds the type of the class containing the COMPONENT. */
2037 basetype = TYPE_MAIN_VARIANT (TREE_TYPE (datum));
2039 /* If DATUM is a COMPOUND_EXPR or COND_EXPR, move our reference
2040 inside it. */
2041 switch (TREE_CODE (datum))
2043 case COMPOUND_EXPR:
2045 tree value = build_component_ref (TREE_OPERAND (datum, 1), component,
2046 basetype_path, protect);
2047 return build (COMPOUND_EXPR, TREE_TYPE (value),
2048 TREE_OPERAND (datum, 0), value);
2050 case COND_EXPR:
2051 return build_conditional_expr
2052 (TREE_OPERAND (datum, 0),
2053 build_component_ref (TREE_OPERAND (datum, 1), component,
2054 basetype_path, protect),
2055 build_component_ref (TREE_OPERAND (datum, 2), component,
2056 basetype_path, protect));
2058 case TEMPLATE_DECL:
2059 cp_error ("invalid use of %D", datum);
2060 datum = error_mark_node;
2061 break;
2063 default:
2064 break;
2067 code = TREE_CODE (basetype);
2069 if (code == REFERENCE_TYPE)
2071 datum = convert_from_reference (datum);
2072 basetype = TYPE_MAIN_VARIANT (TREE_TYPE (datum));
2073 code = TREE_CODE (basetype);
2075 if (TREE_CODE (datum) == OFFSET_REF)
2077 datum = resolve_offset_ref (datum);
2078 basetype = TYPE_MAIN_VARIANT (TREE_TYPE (datum));
2079 code = TREE_CODE (basetype);
2082 /* First, see if there is a field or component with name COMPONENT. */
2083 if (TREE_CODE (component) == TREE_LIST)
2085 /* I could not trigger this code. MvL */
2086 my_friendly_abort (980326);
2087 #ifdef DEAD
2088 my_friendly_assert (!(TREE_CHAIN (component) == NULL_TREE
2089 && DECL_CHAIN (TREE_VALUE (component)) == NULL_TREE), 309);
2090 #endif
2091 return build (COMPONENT_REF, TREE_TYPE (component), datum, component);
2094 if (! IS_AGGR_TYPE_CODE (code))
2096 if (code != ERROR_MARK)
2097 cp_error ("request for member `%D' in `%E', which is of non-aggregate type `%T'",
2098 component, datum, basetype);
2099 return error_mark_node;
2102 if (!complete_type_or_else (basetype, datum))
2103 return error_mark_node;
2105 if (TREE_CODE (component) == BIT_NOT_EXPR)
2107 if (TYPE_IDENTIFIER (basetype) != TREE_OPERAND (component, 0))
2109 cp_error ("destructor specifier `%T::~%T' must have matching names",
2110 basetype, TREE_OPERAND (component, 0));
2111 return error_mark_node;
2113 if (! TYPE_HAS_DESTRUCTOR (basetype))
2115 cp_error ("type `%T' has no destructor", basetype);
2116 return error_mark_node;
2118 return TREE_VEC_ELT (CLASSTYPE_METHOD_VEC (basetype), 1);
2121 /* Look up component name in the structure type definition. */
2122 if (TYPE_VFIELD (basetype)
2123 && DECL_NAME (TYPE_VFIELD (basetype)) == component)
2124 /* Special-case this because if we use normal lookups in an ambiguous
2125 hierarchy, the compiler will abort (because vptr lookups are
2126 not supposed to be ambiguous. */
2127 field = TYPE_VFIELD (basetype);
2128 else if (TREE_CODE (component) == FIELD_DECL)
2129 field = component;
2130 else if (TREE_CODE (component) == TYPE_DECL)
2132 cp_error ("invalid use of type decl `%#D' as expression", component);
2133 return error_mark_node;
2135 else if (TREE_CODE (component) == TEMPLATE_DECL)
2137 cp_error ("invalid use of template `%#D' as expression", component);
2138 return error_mark_node;
2140 else
2142 tree name = component;
2143 if (TREE_CODE (component) == VAR_DECL)
2144 name = DECL_NAME (component);
2145 if (TREE_CODE (component) == NAMESPACE_DECL)
2146 /* Source is in error, but produce a sensible diagnostic. */
2147 name = DECL_NAME (component);
2148 if (basetype_path == NULL_TREE)
2149 basetype_path = TYPE_BINFO (basetype);
2150 field = lookup_field (basetype_path, name,
2151 protect && !VFIELD_NAME_P (name), 0);
2152 if (field == error_mark_node)
2153 return error_mark_node;
2155 if (field == NULL_TREE)
2157 /* Not found as a data field, look for it as a method. If found,
2158 then if this is the only possible one, return it, else
2159 report ambiguity error. */
2160 tree fndecls = lookup_fnfields (basetype_path, name, 1);
2161 if (fndecls == error_mark_node)
2162 return error_mark_node;
2163 if (fndecls)
2165 /* If the function is unique and static, we can resolve it
2166 now. Otherwise, we have to wait and see what context it is
2167 used in; a component_ref involving a non-static member
2168 function can only be used in a call (expr.ref). */
2170 if (TREE_CHAIN (fndecls) == NULL_TREE
2171 && TREE_CODE (TREE_VALUE (fndecls)) == FUNCTION_DECL)
2173 if (DECL_STATIC_FUNCTION_P (TREE_VALUE (fndecls)))
2175 tree fndecl = TREE_VALUE (fndecls);
2176 enforce_access (basetype_path, fndecl);
2177 mark_used (fndecl);
2178 return fndecl;
2180 else
2182 /* A unique non-static member function. Other parts
2183 of the compiler expect something with
2184 unknown_type_node to be really overloaded, so
2185 let's oblige. */
2186 TREE_VALUE (fndecls)
2187 = ovl_cons (TREE_VALUE (fndecls), NULL_TREE);
2191 ref = build (COMPONENT_REF, unknown_type_node,
2192 datum, TREE_VALUE (fndecls));
2193 return ref;
2196 cp_error ("`%#T' has no member named `%D'", basetype, name);
2197 return error_mark_node;
2199 else if (TREE_TYPE (field) == error_mark_node)
2200 return error_mark_node;
2202 if (TREE_CODE (field) != FIELD_DECL)
2204 if (TREE_CODE (field) == TYPE_DECL)
2205 cp_pedwarn ("invalid use of type decl `%#D' as expression", field);
2206 else if (DECL_RTL (field) != 0)
2207 mark_used (field);
2208 else
2209 TREE_USED (field) = 1;
2211 /* Do evaluate the object when accessing a static member. */
2212 if (TREE_SIDE_EFFECTS (datum))
2213 field = build (COMPOUND_EXPR, TREE_TYPE (field), datum, field);
2215 return field;
2219 /* See if we have to do any conversions so that we pick up the field from the
2220 right context. */
2221 if (DECL_FIELD_CONTEXT (field) != basetype)
2223 tree context = DECL_FIELD_CONTEXT (field);
2224 tree base = context;
2225 while (!same_type_p (base, basetype) && TYPE_NAME (base)
2226 && ANON_AGGR_TYPE_P (base))
2227 base = TYPE_CONTEXT (base);
2229 /* Handle base classes here... */
2230 if (base != basetype && TYPE_BASE_CONVS_MAY_REQUIRE_CODE_P (basetype))
2232 tree addr = build_unary_op (ADDR_EXPR, datum, 0);
2233 if (integer_zerop (addr))
2235 error ("invalid reference to NULL ptr, use ptr-to-member instead");
2236 return error_mark_node;
2238 if (VBASE_NAME_P (DECL_NAME (field)))
2240 /* It doesn't matter which vbase pointer we grab, just
2241 find one of them. */
2242 tree binfo = get_binfo (base,
2243 TREE_TYPE (TREE_TYPE (addr)), 0);
2244 addr = convert_pointer_to_real (binfo, addr);
2246 else
2247 addr = convert_pointer_to (base, addr);
2248 datum = build_indirect_ref (addr, NULL_PTR);
2249 if (datum == error_mark_node)
2250 return error_mark_node;
2252 basetype = base;
2254 /* Handle things from anon unions here... */
2255 if (TYPE_NAME (context) && ANON_AGGR_TYPE_P (context))
2257 tree subfield = lookup_anon_field (basetype, context);
2258 tree subdatum = build_component_ref (datum, subfield,
2259 basetype_path, protect);
2260 return build_component_ref (subdatum, field, basetype_path, protect);
2264 /* Compute the type of the field, as described in [expr.ref]. */
2265 type_quals = TYPE_UNQUALIFIED;
2266 field_type = TREE_TYPE (field);
2267 if (TREE_CODE (field_type) == REFERENCE_TYPE)
2268 /* The standard says that the type of the result should be the
2269 type referred to by the reference. But for now, at least, we
2270 do the conversion from reference type later. */
2272 else
2274 type_quals = (CP_TYPE_QUALS (field_type)
2275 | CP_TYPE_QUALS (TREE_TYPE (datum)));
2277 /* A field is const (volatile) if the enclosing object, or the
2278 field itself, is const (volatile). But, a mutable field is
2279 not const, even within a const object. */
2280 if (DECL_MUTABLE_P (field))
2281 type_quals &= ~TYPE_QUAL_CONST;
2282 field_type = cp_build_qualified_type (field_type, type_quals);
2285 ref = fold (build (COMPONENT_REF, field_type, datum, field));
2287 /* Mark the expression const or volatile, as appropriate. Even
2288 though we've dealt with the type above, we still have to mark the
2289 expression itself. */
2290 if (type_quals & TYPE_QUAL_CONST)
2291 TREE_READONLY (ref) = 1;
2292 else if (type_quals & TYPE_QUAL_VOLATILE)
2293 TREE_THIS_VOLATILE (ref) = 1;
2295 return ref;
2298 /* Variant of build_component_ref for use in expressions, which should
2299 never have REFERENCE_TYPE. */
2301 tree
2302 build_x_component_ref (datum, component, basetype_path, protect)
2303 tree datum, component, basetype_path;
2304 int protect;
2306 tree t = build_component_ref (datum, component, basetype_path, protect);
2308 if (! processing_template_decl)
2309 t = convert_from_reference (t);
2311 return t;
2314 /* Given an expression PTR for a pointer, return an expression
2315 for the value pointed to.
2316 ERRORSTRING is the name of the operator to appear in error messages.
2318 This function may need to overload OPERATOR_FNNAME.
2319 Must also handle REFERENCE_TYPEs for C++. */
2321 tree
2322 build_x_indirect_ref (ptr, errorstring)
2323 tree ptr;
2324 const char *errorstring;
2326 tree rval;
2328 if (processing_template_decl)
2329 return build_min_nt (INDIRECT_REF, ptr);
2331 rval = build_opfncall (INDIRECT_REF, LOOKUP_NORMAL, ptr, NULL_TREE,
2332 NULL_TREE);
2333 if (rval)
2334 return rval;
2335 return build_indirect_ref (ptr, errorstring);
2338 tree
2339 build_indirect_ref (ptr, errorstring)
2340 tree ptr;
2341 const char *errorstring;
2343 register tree pointer, type;
2345 if (ptr == error_mark_node)
2346 return error_mark_node;
2348 if (ptr == current_class_ptr)
2349 return current_class_ref;
2351 pointer = (TREE_CODE (TREE_TYPE (ptr)) == REFERENCE_TYPE
2352 ? ptr : default_conversion (ptr));
2353 type = TREE_TYPE (pointer);
2355 if (TYPE_PTR_P (type) || TREE_CODE (type) == REFERENCE_TYPE)
2357 /* [expr.unary.op]
2359 If the type of the expression is "pointer to T," the type
2360 of the result is "T."
2362 We must use the canonical variant because certain parts of
2363 the back end, like fold, do pointer comparisons between
2364 types. */
2365 tree t = canonical_type_variant (TREE_TYPE (type));
2367 if (VOID_TYPE_P (t))
2369 /* A pointer to incomplete type (other than cv void) can be
2370 dereferenced [expr.unary.op]/1 */
2371 cp_error ("`%T' is not a pointer-to-object type", type);
2372 return error_mark_node;
2374 else if (TREE_CODE (pointer) == ADDR_EXPR
2375 && !flag_volatile
2376 && same_type_p (t, TREE_TYPE (TREE_OPERAND (pointer, 0))))
2377 /* The POINTER was something like `&x'. We simplify `*&x' to
2378 `x'. */
2379 return TREE_OPERAND (pointer, 0);
2380 else
2382 tree ref = build1 (INDIRECT_REF, t, pointer);
2384 /* We *must* set TREE_READONLY when dereferencing a pointer to const,
2385 so that we get the proper error message if the result is used
2386 to assign to. Also, &* is supposed to be a no-op. */
2387 TREE_READONLY (ref) = CP_TYPE_CONST_P (t);
2388 TREE_THIS_VOLATILE (ref) = CP_TYPE_VOLATILE_P (t);
2389 TREE_SIDE_EFFECTS (ref)
2390 = (TREE_THIS_VOLATILE (ref) || TREE_SIDE_EFFECTS (pointer)
2391 || flag_volatile);
2392 return ref;
2395 /* `pointer' won't be an error_mark_node if we were given a
2396 pointer to member, so it's cool to check for this here. */
2397 else if (TYPE_PTRMEM_P (type) || TYPE_PTRMEMFUNC_P (type))
2398 error ("invalid use of `%s' on pointer to member", errorstring);
2399 else if (pointer != error_mark_node)
2401 if (errorstring)
2402 error ("invalid type argument of `%s'", errorstring);
2403 else
2404 error ("invalid type argument");
2406 return error_mark_node;
2409 /* This handles expressions of the form "a[i]", which denotes
2410 an array reference.
2412 This is logically equivalent in C to *(a+i), but we may do it differently.
2413 If A is a variable or a member, we generate a primitive ARRAY_REF.
2414 This avoids forcing the array out of registers, and can work on
2415 arrays that are not lvalues (for example, members of structures returned
2416 by functions).
2418 If INDEX is of some user-defined type, it must be converted to
2419 integer type. Otherwise, to make a compatible PLUS_EXPR, it
2420 will inherit the type of the array, which will be some pointer type. */
2422 tree
2423 build_array_ref (array, idx)
2424 tree array, idx;
2426 if (idx == 0)
2428 error ("subscript missing in array reference");
2429 return error_mark_node;
2432 if (TREE_TYPE (array) == error_mark_node
2433 || TREE_TYPE (idx) == error_mark_node)
2434 return error_mark_node;
2436 if (TREE_CODE (TREE_TYPE (array)) == ARRAY_TYPE
2437 && TREE_CODE (array) != INDIRECT_REF)
2439 tree rval, type;
2441 /* Subscripting with type char is likely to lose
2442 on a machine where chars are signed.
2443 So warn on any machine, but optionally.
2444 Don't warn for unsigned char since that type is safe.
2445 Don't warn for signed char because anyone who uses that
2446 must have done so deliberately. */
2447 if (warn_char_subscripts
2448 && TYPE_MAIN_VARIANT (TREE_TYPE (idx)) == char_type_node)
2449 warning ("array subscript has type `char'");
2451 /* Apply default promotions *after* noticing character types. */
2452 idx = default_conversion (idx);
2454 if (TREE_CODE (TREE_TYPE (idx)) != INTEGER_TYPE)
2456 error ("array subscript is not an integer");
2457 return error_mark_node;
2460 /* An array that is indexed by a non-constant
2461 cannot be stored in a register; we must be able to do
2462 address arithmetic on its address.
2463 Likewise an array of elements of variable size. */
2464 if (TREE_CODE (idx) != INTEGER_CST
2465 || (COMPLETE_TYPE_P (TREE_TYPE (TREE_TYPE (array)))
2466 && (TREE_CODE (TYPE_SIZE (TREE_TYPE (TREE_TYPE (array))))
2467 != INTEGER_CST)))
2469 if (mark_addressable (array) == 0)
2470 return error_mark_node;
2472 /* An array that is indexed by a constant value which is not within
2473 the array bounds cannot be stored in a register either; because we
2474 would get a crash in store_bit_field/extract_bit_field when trying
2475 to access a non-existent part of the register. */
2476 if (TREE_CODE (idx) == INTEGER_CST
2477 && TYPE_VALUES (TREE_TYPE (array))
2478 && ! int_fits_type_p (idx, TYPE_VALUES (TREE_TYPE (array))))
2480 if (mark_addressable (array) == 0)
2481 return error_mark_node;
2484 if (pedantic && !lvalue_p (array))
2485 pedwarn ("ISO C++ forbids subscripting non-lvalue array");
2487 /* Note in C++ it is valid to subscript a `register' array, since
2488 it is valid to take the address of something with that
2489 storage specification. */
2490 if (extra_warnings)
2492 tree foo = array;
2493 while (TREE_CODE (foo) == COMPONENT_REF)
2494 foo = TREE_OPERAND (foo, 0);
2495 if (TREE_CODE (foo) == VAR_DECL && DECL_REGISTER (foo))
2496 warning ("subscripting array declared `register'");
2499 type = TREE_TYPE (TREE_TYPE (array));
2500 rval = build (ARRAY_REF, type, array, idx);
2501 /* Array ref is const/volatile if the array elements are
2502 or if the array is.. */
2503 TREE_READONLY (rval)
2504 |= (CP_TYPE_CONST_P (type) | TREE_READONLY (array));
2505 TREE_SIDE_EFFECTS (rval)
2506 |= (CP_TYPE_VOLATILE_P (type) | TREE_SIDE_EFFECTS (array));
2507 TREE_THIS_VOLATILE (rval)
2508 |= (CP_TYPE_VOLATILE_P (type) | TREE_THIS_VOLATILE (array));
2509 return require_complete_type (fold (rval));
2513 tree ar = default_conversion (array);
2514 tree ind = default_conversion (idx);
2516 /* Put the integer in IND to simplify error checking. */
2517 if (TREE_CODE (TREE_TYPE (ar)) == INTEGER_TYPE)
2519 tree temp = ar;
2520 ar = ind;
2521 ind = temp;
2524 if (ar == error_mark_node)
2525 return ar;
2527 if (TREE_CODE (TREE_TYPE (ar)) != POINTER_TYPE)
2529 error ("subscripted value is neither array nor pointer");
2530 return error_mark_node;
2532 if (TREE_CODE (TREE_TYPE (ind)) != INTEGER_TYPE)
2534 error ("array subscript is not an integer");
2535 return error_mark_node;
2538 return build_indirect_ref (cp_build_binary_op (PLUS_EXPR, ar, ind),
2539 "array indexing");
2543 /* Build a function call to function FUNCTION with parameters PARAMS.
2544 PARAMS is a list--a chain of TREE_LIST nodes--in which the
2545 TREE_VALUE of each node is a parameter-expression. The PARAMS do
2546 not include any object pointer that may be required. FUNCTION's
2547 data type may be a function type or a pointer-to-function.
2549 For C++: If FUNCTION's data type is a TREE_LIST, then the tree list
2550 is the list of possible methods that FUNCTION could conceivably
2551 be. If the list of methods comes from a class, then it will be
2552 a list of lists (where each element is associated with the class
2553 that produced it), otherwise it will be a simple list (for
2554 functions overloaded in global scope).
2556 In the first case, TREE_VALUE (function) is the head of one of those
2557 lists, and TREE_PURPOSE is the name of the function.
2559 In the second case, TREE_PURPOSE (function) is the function's
2560 name directly.
2562 DECL is the class instance variable, usually CURRENT_CLASS_REF.
2564 When calling a TEMPLATE_DECL, we don't require a complete return
2565 type. */
2567 tree
2568 build_x_function_call (function, params, decl)
2569 tree function, params, decl;
2571 tree type;
2572 tree template_id = NULL_TREE;
2573 int is_method;
2575 if (function == error_mark_node)
2576 return error_mark_node;
2578 if (processing_template_decl)
2579 return build_min_nt (CALL_EXPR, function, params, NULL_TREE);
2581 /* Save explicit template arguments if found */
2582 if (TREE_CODE (function) == TEMPLATE_ID_EXPR)
2584 template_id = function;
2585 function = TREE_OPERAND (function, 0);
2588 type = TREE_TYPE (function);
2590 if (TREE_CODE (type) == OFFSET_TYPE
2591 && TREE_TYPE (type) == unknown_type_node
2592 && TREE_CODE (function) == TREE_LIST
2593 && TREE_CHAIN (function) == NULL_TREE)
2595 /* Undo (Foo:bar)()... */
2596 type = TYPE_OFFSET_BASETYPE (type);
2597 function = TREE_VALUE (function);
2598 my_friendly_assert (TREE_CODE (function) == TREE_LIST, 999);
2599 my_friendly_assert (TREE_CHAIN (function) == NULL_TREE, 999);
2600 function = TREE_VALUE (function);
2601 if (TREE_CODE (function) == OVERLOAD)
2602 function = OVL_FUNCTION (function);
2603 my_friendly_assert (TREE_CODE (function) == FUNCTION_DECL, 999);
2604 function = DECL_NAME (function);
2605 return build_method_call (decl, function, params,
2606 TYPE_BINFO (type), LOOKUP_NORMAL);
2609 if (TREE_CODE (function) == OFFSET_REF
2610 && TREE_CODE (type) != METHOD_TYPE)
2611 function = resolve_offset_ref (function);
2613 if ((TREE_CODE (function) == FUNCTION_DECL
2614 && DECL_STATIC_FUNCTION_P (function))
2615 || (DECL_FUNCTION_TEMPLATE_P (function)
2616 && DECL_STATIC_FUNCTION_P (DECL_TEMPLATE_RESULT (function))))
2617 return build_member_call (DECL_CONTEXT (function),
2618 template_id
2619 ? template_id : DECL_NAME (function),
2620 params);
2622 is_method = ((TREE_CODE (function) == TREE_LIST
2623 && current_class_type != NULL_TREE
2624 && (IDENTIFIER_CLASS_VALUE (TREE_PURPOSE (function))
2625 == function))
2626 || (TREE_CODE (function) == OVERLOAD
2627 && DECL_FUNCTION_MEMBER_P (OVL_CURRENT (function)))
2628 || TREE_CODE (function) == IDENTIFIER_NODE
2629 || TREE_CODE (type) == METHOD_TYPE
2630 || TYPE_PTRMEMFUNC_P (type));
2632 /* A friend template. Make it look like a toplevel declaration. */
2633 if (! is_method && TREE_CODE (function) == TEMPLATE_DECL)
2634 function = ovl_cons (function, NULL_TREE);
2636 /* Handle methods, friends, and overloaded functions, respectively. */
2637 if (is_method)
2639 tree basetype = NULL_TREE;
2641 if (TREE_CODE (function) == OVERLOAD)
2642 function = OVL_CURRENT (function);
2644 if (TREE_CODE (function) == FUNCTION_DECL
2645 || DECL_FUNCTION_TEMPLATE_P (function))
2647 basetype = DECL_CONTEXT (function);
2649 if (DECL_NAME (function))
2650 function = DECL_NAME (function);
2651 else
2652 function = TYPE_IDENTIFIER (DECL_CONTEXT (function));
2654 else if (TREE_CODE (function) == TREE_LIST)
2656 my_friendly_assert (TREE_CODE (TREE_VALUE (function))
2657 == FUNCTION_DECL, 312);
2658 basetype = DECL_CONTEXT (TREE_VALUE (function));
2659 function = TREE_PURPOSE (function);
2661 else if (TREE_CODE (function) != IDENTIFIER_NODE)
2663 if (TREE_CODE (function) == OFFSET_REF)
2665 if (TREE_OPERAND (function, 0))
2666 decl = TREE_OPERAND (function, 0);
2668 /* Call via a pointer to member function. */
2669 if (decl == NULL_TREE)
2671 error ("pointer to member function called, but not in class scope");
2672 return error_mark_node;
2674 /* What other type of POINTER_TYPE could this be? */
2675 if (TREE_CODE (TREE_TYPE (function)) != POINTER_TYPE
2676 && ! TYPE_PTRMEMFUNC_P (TREE_TYPE (function))
2677 && TREE_CODE (function) != OFFSET_REF)
2678 function = build (OFFSET_REF, TREE_TYPE (type), NULL_TREE,
2679 function);
2680 goto do_x_function;
2683 /* this is an abbreviated method call.
2684 must go through here in case it is a virtual function.
2685 @@ Perhaps this could be optimized. */
2687 if (basetype && (! current_class_type
2688 || ! DERIVED_FROM_P (basetype, current_class_type)))
2689 return build_member_call (basetype, function, params);
2691 if (decl == NULL_TREE)
2693 if (current_class_type == NULL_TREE)
2695 cp_error ("object missing in call to method `%D'", function);
2696 return error_mark_node;
2698 /* Yow: call from a static member function. */
2699 decl = build_dummy_object (current_class_type);
2702 /* Put back explicit template arguments, if any. */
2703 if (template_id)
2704 function = template_id;
2705 return build_method_call (decl, function, params,
2706 NULL_TREE, LOOKUP_NORMAL);
2708 else if (TREE_CODE (function) == COMPONENT_REF
2709 && type == unknown_type_node)
2711 /* Undo what we did in build_component_ref. */
2712 decl = TREE_OPERAND (function, 0);
2713 function = TREE_OPERAND (function, 1);
2714 function = DECL_NAME (OVL_CURRENT (function));
2716 if (template_id)
2718 TREE_OPERAND (template_id, 0) = function;
2719 function = template_id;
2722 return build_method_call (decl, function, params,
2723 NULL_TREE, LOOKUP_NORMAL);
2725 else if (really_overloaded_fn (function))
2727 if (OVL_FUNCTION (function) == NULL_TREE)
2729 cp_error ("function `%D' declared overloaded, but no definitions appear with which to resolve it?!?",
2730 TREE_PURPOSE (function));
2731 return error_mark_node;
2733 else
2735 /* Put back explicit template arguments, if any. */
2736 if (template_id)
2737 function = template_id;
2738 return build_new_function_call (function, params);
2741 else
2742 /* Remove a potential OVERLOAD around it */
2743 function = OVL_CURRENT (function);
2745 do_x_function:
2746 if (TREE_CODE (function) == OFFSET_REF)
2748 /* If the component is a data element (or a virtual function), we play
2749 games here to make things work. */
2750 tree decl_addr;
2752 if (TREE_OPERAND (function, 0))
2753 decl = TREE_OPERAND (function, 0);
2754 else
2755 decl = current_class_ref;
2757 decl_addr = build_unary_op (ADDR_EXPR, decl, 0);
2759 /* Sigh. OFFSET_REFs are being used for too many things.
2760 They're being used both for -> and ->*, and we want to resolve
2761 the -> cases here, but leave the ->*. We could use
2762 resolve_offset_ref for those, too, but it would call
2763 get_member_function_from_ptrfunc and decl_addr wouldn't get
2764 updated properly. Nasty. */
2765 if (TREE_CODE (TREE_OPERAND (function, 1)) == FIELD_DECL)
2766 function = resolve_offset_ref (function);
2767 else
2768 function = TREE_OPERAND (function, 1);
2770 function = get_member_function_from_ptrfunc (&decl_addr, function);
2771 params = tree_cons (NULL_TREE, decl_addr, params);
2772 return build_function_call (function, params);
2775 type = TREE_TYPE (function);
2776 if (type != error_mark_node)
2778 if (TREE_CODE (type) == REFERENCE_TYPE)
2779 type = TREE_TYPE (type);
2781 if (IS_AGGR_TYPE (type))
2782 return build_opfncall (CALL_EXPR, LOOKUP_NORMAL, function, params, NULL_TREE);
2785 if (is_method)
2787 tree fntype = TREE_TYPE (function);
2788 tree ctypeptr = NULL_TREE;
2790 /* Explicitly named method? */
2791 if (TREE_CODE (function) == FUNCTION_DECL)
2792 ctypeptr = build_pointer_type (DECL_CLASS_CONTEXT (function));
2793 /* Expression with ptr-to-method type? It could either be a plain
2794 usage, or it might be a case where the ptr-to-method is being
2795 passed in as an argument. */
2796 else if (TYPE_PTRMEMFUNC_P (fntype))
2798 tree rec = TYPE_METHOD_BASETYPE (TREE_TYPE
2799 (TYPE_PTRMEMFUNC_FN_TYPE (fntype)));
2800 ctypeptr = build_pointer_type (rec);
2802 /* Unexpected node type? */
2803 else
2804 my_friendly_abort (116);
2805 if (decl == NULL_TREE)
2807 if (current_function_decl
2808 && DECL_STATIC_FUNCTION_P (current_function_decl))
2809 error ("invalid call to member function needing `this' in static member function scope");
2810 else
2811 error ("pointer to member function called, but not in class scope");
2812 return error_mark_node;
2814 if (TREE_CODE (TREE_TYPE (decl)) != POINTER_TYPE
2815 && ! TYPE_PTRMEMFUNC_P (TREE_TYPE (decl)))
2817 decl = build_unary_op (ADDR_EXPR, decl, 0);
2818 decl = convert_pointer_to (TREE_TYPE (ctypeptr), decl);
2820 else
2821 decl = build_c_cast (ctypeptr, decl);
2822 params = tree_cons (NULL_TREE, decl, params);
2825 return build_function_call (function, params);
2828 /* Resolve a pointer to member function. INSTANCE is the object
2829 instance to use, if the member points to a virtual member. */
2831 tree
2832 get_member_function_from_ptrfunc (instance_ptrptr, function)
2833 tree *instance_ptrptr;
2834 tree function;
2836 if (TREE_CODE (function) == OFFSET_REF)
2838 function = TREE_OPERAND (function, 1);
2841 if (TYPE_PTRMEMFUNC_P (TREE_TYPE (function)))
2843 tree fntype, idx, e1, delta, delta2, e2, e3, aref, vtbl;
2844 tree instance, basetype;
2846 tree instance_ptr = *instance_ptrptr;
2848 if (instance_ptr == error_mark_node
2849 && TREE_CODE (function) == PTRMEM_CST)
2851 /* Extracting the function address from a pmf is only
2852 allowed with -Wno-pmf-conversions. It only works for
2853 pmf constants. */
2854 e1 = build_addr_func (PTRMEM_CST_MEMBER (function));
2855 e1 = convert (TYPE_PTRMEMFUNC_FN_TYPE (TREE_TYPE (function)), e1);
2856 return e1;
2859 if (TREE_SIDE_EFFECTS (instance_ptr))
2860 instance_ptr = save_expr (instance_ptr);
2862 if (TREE_SIDE_EFFECTS (function))
2863 function = save_expr (function);
2865 fntype = TYPE_PTRMEMFUNC_FN_TYPE (TREE_TYPE (function));
2866 basetype = TYPE_METHOD_BASETYPE (TREE_TYPE (fntype));
2868 /* Convert down to the right base, before using the instance. */
2869 instance = convert_pointer_to_real (basetype, instance_ptr);
2870 if (instance == error_mark_node && instance_ptr != error_mark_node)
2871 return instance;
2873 e3 = PFN_FROM_PTRMEMFUNC (function);
2875 /* This used to avoid checking for virtual functions if basetype
2876 has no virtual functions, according to an earlier ANSI draft.
2877 With the final ISO C++ rules, such an optimization is
2878 incorrect: A pointer to a derived member can be static_cast
2879 to pointer-to-base-member, as long as the dynamic object
2880 later has the right member. */
2882 /* Promoting idx before saving it improves performance on RISC
2883 targets. Without promoting, the first compare used
2884 load-with-sign-extend, while the second used normal load then
2885 shift to sign-extend. An optimizer flaw, perhaps, but it's
2886 easier to make this change. */
2887 if (flag_new_abi)
2889 idx = cp_build_binary_op (TRUNC_DIV_EXPR,
2890 build1 (NOP_EXPR, vtable_index_type, e3),
2891 TYPE_SIZE_UNIT (vtable_entry_type));
2892 e1 = cp_build_binary_op (BIT_AND_EXPR,
2893 build1 (NOP_EXPR, vtable_index_type, e3),
2894 integer_one_node);
2896 else
2898 idx = save_expr (default_conversion
2899 (build_component_ref (function,
2900 index_identifier,
2901 NULL_TREE, 0)));
2902 e1 = cp_build_binary_op (GE_EXPR, idx, integer_zero_node);
2903 idx = cp_build_binary_op (MINUS_EXPR, idx, integer_one_node);
2906 vtbl = convert_pointer_to (ptr_type_node, instance);
2907 delta = cp_convert (ptrdiff_type_node,
2908 build_component_ref (function, delta_identifier,
2909 NULL_TREE, 0));
2910 if (flag_new_abi)
2911 /* DELTA2 is the amount by which to adjust the `this' pointer
2912 to find the vtbl. */
2913 delta2 = delta;
2914 else
2915 delta2 = DELTA2_FROM_PTRMEMFUNC (function);
2916 vtbl = build
2917 (PLUS_EXPR,
2918 build_pointer_type (build_pointer_type (vtable_entry_type)),
2919 vtbl, cp_convert (ptrdiff_type_node, delta2));
2920 vtbl = build_indirect_ref (vtbl, NULL_PTR);
2921 aref = build_array_ref (vtbl, idx);
2923 if (! flag_vtable_thunks)
2925 aref = save_expr (aref);
2927 delta = cp_build_binary_op
2928 (PLUS_EXPR,
2929 build_conditional_expr (e1,
2930 build_component_ref (aref,
2931 delta_identifier,
2932 NULL_TREE, 0),
2933 integer_zero_node),
2934 delta);
2937 if (flag_vtable_thunks)
2938 e2 = aref;
2939 else
2940 e2 = build_component_ref (aref, pfn_identifier, NULL_TREE, 0);
2941 TREE_TYPE (e2) = TREE_TYPE (e3);
2942 e1 = build_conditional_expr (e1, e2, e3);
2944 /* Make sure this doesn't get evaluated first inside one of the
2945 branches of the COND_EXPR. */
2946 if (TREE_CODE (instance_ptr) == SAVE_EXPR)
2947 e1 = build (COMPOUND_EXPR, TREE_TYPE (e1),
2948 instance_ptr, e1);
2950 *instance_ptrptr = build (PLUS_EXPR, TREE_TYPE (instance_ptr),
2951 instance_ptr, delta);
2953 if (instance_ptr == error_mark_node
2954 && TREE_CODE (e1) != ADDR_EXPR
2955 && TREE_CODE (TREE_OPERAND (e1, 0)) != FUNCTION_DECL)
2956 cp_error ("object missing in `%E'", function);
2958 function = e1;
2960 return function;
2963 tree
2964 build_function_call_real (function, params, require_complete, flags)
2965 tree function, params;
2966 int require_complete, flags;
2968 register tree fntype, fndecl;
2969 register tree value_type;
2970 register tree coerced_params;
2971 tree result;
2972 tree name = NULL_TREE, assembler_name = NULL_TREE;
2973 int is_method;
2975 /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
2976 Strip such NOP_EXPRs, since FUNCTION is used in non-lvalue context. */
2977 if (TREE_CODE (function) == NOP_EXPR
2978 && TREE_TYPE (function) == TREE_TYPE (TREE_OPERAND (function, 0)))
2979 function = TREE_OPERAND (function, 0);
2981 if (TREE_CODE (function) == FUNCTION_DECL)
2983 name = DECL_NAME (function);
2984 assembler_name = DECL_ASSEMBLER_NAME (function);
2986 GNU_xref_call (current_function_decl,
2987 IDENTIFIER_POINTER (name ? name
2988 : TYPE_IDENTIFIER (DECL_CLASS_CONTEXT
2989 (function))));
2990 mark_used (function);
2991 fndecl = function;
2993 /* Convert anything with function type to a pointer-to-function. */
2994 if (pedantic && DECL_MAIN_P (function))
2995 pedwarn ("ISO C++ forbids calling `::main' from within program");
2997 /* Differs from default_conversion by not setting TREE_ADDRESSABLE
2998 (because calling an inline function does not mean the function
2999 needs to be separately compiled). */
3001 if (DECL_INLINE (function))
3002 function = inline_conversion (function);
3003 else
3004 function = build_addr_func (function);
3006 else
3008 fndecl = NULL_TREE;
3010 function = build_addr_func (function);
3013 if (function == error_mark_node)
3014 return error_mark_node;
3016 fntype = TREE_TYPE (function);
3018 if (TYPE_PTRMEMFUNC_P (fntype))
3020 cp_error ("must use .* or ->* to call pointer-to-member function in `%E (...)'",
3021 function);
3022 return error_mark_node;
3025 is_method = (TREE_CODE (fntype) == POINTER_TYPE
3026 && TREE_CODE (TREE_TYPE (fntype)) == METHOD_TYPE);
3028 if (!((TREE_CODE (fntype) == POINTER_TYPE
3029 && TREE_CODE (TREE_TYPE (fntype)) == FUNCTION_TYPE)
3030 || is_method
3031 || TREE_CODE (function) == TEMPLATE_ID_EXPR))
3033 cp_error ("`%E' cannot be used as a function", function);
3034 return error_mark_node;
3037 /* fntype now gets the type of function pointed to. */
3038 fntype = TREE_TYPE (fntype);
3040 /* Convert the parameters to the types declared in the
3041 function prototype, or apply default promotions. */
3043 if (flags & LOOKUP_COMPLAIN)
3044 coerced_params = convert_arguments (TYPE_ARG_TYPES (fntype),
3045 params, fndecl, LOOKUP_NORMAL);
3046 else
3047 coerced_params = convert_arguments (TYPE_ARG_TYPES (fntype),
3048 params, fndecl, 0);
3050 if (coerced_params == error_mark_node)
3052 if (flags & LOOKUP_SPECULATIVELY)
3053 return NULL_TREE;
3054 else
3055 return error_mark_node;
3058 /* Check for errors in format strings. */
3060 if (warn_format && (name || assembler_name))
3061 check_function_format (NULL, name, assembler_name, coerced_params);
3063 /* Recognize certain built-in functions so we can make tree-codes
3064 other than CALL_EXPR. We do this when it enables fold-const.c
3065 to do something useful. */
3067 if (TREE_CODE (function) == ADDR_EXPR
3068 && TREE_CODE (TREE_OPERAND (function, 0)) == FUNCTION_DECL
3069 && DECL_BUILT_IN (TREE_OPERAND (function, 0)))
3071 result = expand_tree_builtin (TREE_OPERAND (function, 0),
3072 params, coerced_params);
3073 if (result)
3074 return result;
3077 /* Some built-in function calls will be evaluated at
3078 compile-time in fold (). */
3079 result = fold (build_call (function, coerced_params));
3080 value_type = TREE_TYPE (result);
3082 if (require_complete)
3084 if (TREE_CODE (value_type) == VOID_TYPE)
3085 return result;
3086 result = require_complete_type (result);
3088 if (IS_AGGR_TYPE (value_type))
3089 result = build_cplus_new (value_type, result);
3090 return convert_from_reference (result);
3093 tree
3094 build_function_call (function, params)
3095 tree function, params;
3097 return build_function_call_real (function, params, 1, LOOKUP_NORMAL);
3100 /* Convert the actual parameter expressions in the list VALUES
3101 to the types in the list TYPELIST.
3102 If parmdecls is exhausted, or when an element has NULL as its type,
3103 perform the default conversions.
3105 NAME is an IDENTIFIER_NODE or 0. It is used only for error messages.
3107 This is also where warnings about wrong number of args are generated.
3109 Return a list of expressions for the parameters as converted.
3111 Both VALUES and the returned value are chains of TREE_LIST nodes
3112 with the elements of the list in the TREE_VALUE slots of those nodes.
3114 In C++, unspecified trailing parameters can be filled in with their
3115 default arguments, if such were specified. Do so here. */
3117 tree
3118 convert_arguments (typelist, values, fndecl, flags)
3119 tree typelist, values, fndecl;
3120 int flags;
3122 register tree typetail, valtail;
3123 register tree result = NULL_TREE;
3124 const char *called_thing = 0;
3125 int i = 0;
3127 /* Argument passing is always copy-initialization. */
3128 flags |= LOOKUP_ONLYCONVERTING;
3130 if (fndecl)
3132 if (TREE_CODE (TREE_TYPE (fndecl)) == METHOD_TYPE)
3134 if (DECL_NAME (fndecl) == NULL_TREE
3135 || IDENTIFIER_HAS_TYPE_VALUE (DECL_NAME (fndecl)))
3136 called_thing = "constructor";
3137 else
3138 called_thing = "member function";
3140 else
3141 called_thing = "function";
3144 for (valtail = values, typetail = typelist;
3145 valtail;
3146 valtail = TREE_CHAIN (valtail), i++)
3148 register tree type = typetail ? TREE_VALUE (typetail) : 0;
3149 register tree val = TREE_VALUE (valtail);
3151 if (val == error_mark_node)
3152 return error_mark_node;
3154 if (type == void_type_node)
3156 if (fndecl)
3158 cp_error_at ("too many arguments to %s `%+#D'", called_thing,
3159 fndecl);
3160 error ("at this point in file");
3162 else
3163 error ("too many arguments to function");
3164 /* In case anybody wants to know if this argument
3165 list is valid. */
3166 if (result)
3167 TREE_TYPE (tree_last (result)) = error_mark_node;
3168 break;
3171 if (TREE_CODE (val) == OFFSET_REF)
3172 val = resolve_offset_ref (val);
3174 /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
3175 Strip such NOP_EXPRs, since VAL is used in non-lvalue context. */
3176 if (TREE_CODE (val) == NOP_EXPR
3177 && TREE_TYPE (val) == TREE_TYPE (TREE_OPERAND (val, 0))
3178 && (type == 0 || TREE_CODE (type) != REFERENCE_TYPE))
3179 val = TREE_OPERAND (val, 0);
3181 if (type == 0 || TREE_CODE (type) != REFERENCE_TYPE)
3183 if (TREE_CODE (TREE_TYPE (val)) == ARRAY_TYPE
3184 || TREE_CODE (TREE_TYPE (val)) == FUNCTION_TYPE
3185 || TREE_CODE (TREE_TYPE (val)) == METHOD_TYPE)
3186 val = default_conversion (val);
3189 if (val == error_mark_node)
3190 return error_mark_node;
3192 if (type != 0)
3194 /* Formal parm type is specified by a function prototype. */
3195 tree parmval;
3197 if (!COMPLETE_TYPE_P (complete_type (type)))
3199 error ("parameter type of called function is incomplete");
3200 parmval = val;
3202 else
3204 parmval = convert_for_initialization
3205 (NULL_TREE, type, val, flags,
3206 "argument passing", fndecl, i);
3207 if (PROMOTE_PROTOTYPES
3208 && (TREE_CODE (type) == INTEGER_TYPE
3209 || TREE_CODE (type) == ENUMERAL_TYPE)
3210 && (TYPE_PRECISION (type)
3211 < TYPE_PRECISION (integer_type_node)))
3212 parmval = default_conversion (parmval);
3215 if (parmval == error_mark_node)
3216 return error_mark_node;
3218 result = tree_cons (NULL_TREE, parmval, result);
3220 else
3222 if (TREE_CODE (TREE_TYPE (val)) == REFERENCE_TYPE)
3223 val = convert_from_reference (val);
3225 result = tree_cons (NULL_TREE,
3226 convert_arg_to_ellipsis (val),
3227 result);
3230 if (typetail)
3231 typetail = TREE_CHAIN (typetail);
3234 if (typetail != 0 && typetail != void_list_node)
3236 /* See if there are default arguments that can be used */
3237 if (TREE_PURPOSE (typetail))
3239 for (; typetail != void_list_node; ++i)
3241 tree parmval
3242 = convert_default_arg (TREE_VALUE (typetail),
3243 TREE_PURPOSE (typetail),
3244 fndecl, i);
3246 if (parmval == error_mark_node)
3247 return error_mark_node;
3249 result = tree_cons (0, parmval, result);
3250 typetail = TREE_CHAIN (typetail);
3251 /* ends with `...'. */
3252 if (typetail == NULL_TREE)
3253 break;
3256 else
3258 if (fndecl)
3260 cp_error_at ("too few arguments to %s `%+#D'",
3261 called_thing, fndecl);
3262 error ("at this point in file");
3264 else
3265 error ("too few arguments to function");
3266 return error_mark_list;
3270 return nreverse (result);
3273 /* Build a binary-operation expression, after performing default
3274 conversions on the operands. CODE is the kind of expression to build. */
3276 tree
3277 build_x_binary_op (code, arg1, arg2)
3278 enum tree_code code;
3279 tree arg1, arg2;
3281 if (processing_template_decl)
3282 return build_min_nt (code, arg1, arg2);
3284 return build_new_op (code, LOOKUP_NORMAL, arg1, arg2, NULL_TREE);
3287 /* Build a binary-operation expression without default conversions.
3288 CODE is the kind of expression to build.
3289 This function differs from `build' in several ways:
3290 the data type of the result is computed and recorded in it,
3291 warnings are generated if arg data types are invalid,
3292 special handling for addition and subtraction of pointers is known,
3293 and some optimization is done (operations on narrow ints
3294 are done in the narrower type when that gives the same result).
3295 Constant folding is also done before the result is returned.
3297 Note that the operands will never have enumeral types
3298 because either they have just had the default conversions performed
3299 or they have both just been converted to some other type in which
3300 the arithmetic is to be done.
3302 C++: must do special pointer arithmetic when implementing
3303 multiple inheritance, and deal with pointer to member functions. */
3305 tree
3306 build_binary_op (code, orig_op0, orig_op1, convert_p)
3307 enum tree_code code;
3308 tree orig_op0, orig_op1;
3309 int convert_p ATTRIBUTE_UNUSED;
3311 tree op0, op1;
3312 register enum tree_code code0, code1;
3313 tree type0, type1;
3315 /* Expression code to give to the expression when it is built.
3316 Normally this is CODE, which is what the caller asked for,
3317 but in some special cases we change it. */
3318 register enum tree_code resultcode = code;
3320 /* Data type in which the computation is to be performed.
3321 In the simplest cases this is the common type of the arguments. */
3322 register tree result_type = NULL;
3324 /* Nonzero means operands have already been type-converted
3325 in whatever way is necessary.
3326 Zero means they need to be converted to RESULT_TYPE. */
3327 int converted = 0;
3329 /* Nonzero means create the expression with this type, rather than
3330 RESULT_TYPE. */
3331 tree build_type = 0;
3333 /* Nonzero means after finally constructing the expression
3334 convert it to this type. */
3335 tree final_type = 0;
3337 /* Nonzero if this is an operation like MIN or MAX which can
3338 safely be computed in short if both args are promoted shorts.
3339 Also implies COMMON.
3340 -1 indicates a bitwise operation; this makes a difference
3341 in the exact conditions for when it is safe to do the operation
3342 in a narrower mode. */
3343 int shorten = 0;
3345 /* Nonzero if this is a comparison operation;
3346 if both args are promoted shorts, compare the original shorts.
3347 Also implies COMMON. */
3348 int short_compare = 0;
3350 /* Nonzero if this is a right-shift operation, which can be computed on the
3351 original short and then promoted if the operand is a promoted short. */
3352 int short_shift = 0;
3354 /* Nonzero means set RESULT_TYPE to the common type of the args. */
3355 int common = 0;
3357 /* Apply default conversions. */
3358 if (code == TRUTH_AND_EXPR || code == TRUTH_ANDIF_EXPR
3359 || code == TRUTH_OR_EXPR || code == TRUTH_ORIF_EXPR
3360 || code == TRUTH_XOR_EXPR)
3362 op0 = decay_conversion (orig_op0);
3363 op1 = decay_conversion (orig_op1);
3365 else
3367 op0 = default_conversion (orig_op0);
3368 op1 = default_conversion (orig_op1);
3371 /* Strip NON_LVALUE_EXPRs, etc., since we aren't using as an lvalue. */
3372 STRIP_TYPE_NOPS (op0);
3373 STRIP_TYPE_NOPS (op1);
3375 /* DTRT if one side is an overloaded function, but complain about it. */
3376 if (type_unknown_p (op0))
3378 tree t = instantiate_type (TREE_TYPE (op1), op0, itf_none);
3379 if (t != error_mark_node)
3381 cp_pedwarn ("assuming cast to type `%T' from overloaded function",
3382 TREE_TYPE (t));
3383 op0 = t;
3386 if (type_unknown_p (op1))
3388 tree t = instantiate_type (TREE_TYPE (op0), op1, itf_none);
3389 if (t != error_mark_node)
3391 cp_pedwarn ("assuming cast to type `%T' from overloaded function",
3392 TREE_TYPE (t));
3393 op1 = t;
3397 type0 = TREE_TYPE (op0);
3398 type1 = TREE_TYPE (op1);
3400 /* The expression codes of the data types of the arguments tell us
3401 whether the arguments are integers, floating, pointers, etc. */
3402 code0 = TREE_CODE (type0);
3403 code1 = TREE_CODE (type1);
3405 /* If an error was already reported for one of the arguments,
3406 avoid reporting another error. */
3408 if (code0 == ERROR_MARK || code1 == ERROR_MARK)
3409 return error_mark_node;
3411 switch (code)
3413 case PLUS_EXPR:
3414 /* Handle the pointer + int case. */
3415 if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
3416 return pointer_int_sum (PLUS_EXPR, op0, op1);
3417 else if (code1 == POINTER_TYPE && code0 == INTEGER_TYPE)
3418 return pointer_int_sum (PLUS_EXPR, op1, op0);
3419 else
3420 common = 1;
3421 break;
3423 case MINUS_EXPR:
3424 /* Subtraction of two similar pointers.
3425 We must subtract them as integers, then divide by object size. */
3426 if (code0 == POINTER_TYPE && code1 == POINTER_TYPE
3427 && comp_target_types (type0, type1, 1))
3428 return pointer_diff (op0, op1, common_type (type0, type1));
3429 /* Handle pointer minus int. Just like pointer plus int. */
3430 else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
3431 return pointer_int_sum (MINUS_EXPR, op0, op1);
3432 else
3433 common = 1;
3434 break;
3436 case MULT_EXPR:
3437 common = 1;
3438 break;
3440 case TRUNC_DIV_EXPR:
3441 case CEIL_DIV_EXPR:
3442 case FLOOR_DIV_EXPR:
3443 case ROUND_DIV_EXPR:
3444 case EXACT_DIV_EXPR:
3445 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE
3446 || code0 == COMPLEX_TYPE)
3447 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
3448 || code1 == COMPLEX_TYPE))
3450 if (TREE_CODE (op1) == INTEGER_CST && integer_zerop (op1))
3451 cp_warning ("division by zero in `%E / 0'", op0);
3452 else if (TREE_CODE (op1) == REAL_CST && real_zerop (op1))
3453 cp_warning ("division by zero in `%E / 0.'", op0);
3455 if (!(code0 == INTEGER_TYPE && code1 == INTEGER_TYPE))
3456 resultcode = RDIV_EXPR;
3457 else
3458 /* When dividing two signed integers, we have to promote to int.
3459 unless we divide by a constant != -1. Note that default
3460 conversion will have been performed on the operands at this
3461 point, so we have to dig out the original type to find out if
3462 it was unsigned. */
3463 shorten = ((TREE_CODE (op0) == NOP_EXPR
3464 && TREE_UNSIGNED (TREE_TYPE (TREE_OPERAND (op0, 0))))
3465 || (TREE_CODE (op1) == INTEGER_CST
3466 && ! integer_all_onesp (op1)));
3468 common = 1;
3470 break;
3472 case BIT_AND_EXPR:
3473 case BIT_ANDTC_EXPR:
3474 case BIT_IOR_EXPR:
3475 case BIT_XOR_EXPR:
3476 if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
3477 shorten = -1;
3478 /* If one operand is a constant, and the other is a short type
3479 that has been converted to an int,
3480 really do the work in the short type and then convert the
3481 result to int. If we are lucky, the constant will be 0 or 1
3482 in the short type, making the entire operation go away. */
3483 if (TREE_CODE (op0) == INTEGER_CST
3484 && TREE_CODE (op1) == NOP_EXPR
3485 && (TYPE_PRECISION (type1)
3486 > TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (op1, 0))))
3487 && TREE_UNSIGNED (TREE_TYPE (TREE_OPERAND (op1, 0))))
3489 final_type = result_type;
3490 op1 = TREE_OPERAND (op1, 0);
3491 result_type = TREE_TYPE (op1);
3493 if (TREE_CODE (op1) == INTEGER_CST
3494 && TREE_CODE (op0) == NOP_EXPR
3495 && (TYPE_PRECISION (type0)
3496 > TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (op0, 0))))
3497 && TREE_UNSIGNED (TREE_TYPE (TREE_OPERAND (op0, 0))))
3499 final_type = result_type;
3500 op0 = TREE_OPERAND (op0, 0);
3501 result_type = TREE_TYPE (op0);
3503 break;
3505 case TRUNC_MOD_EXPR:
3506 case FLOOR_MOD_EXPR:
3507 if (code1 == INTEGER_TYPE && integer_zerop (op1))
3508 cp_warning ("division by zero in `%E %% 0'", op0);
3509 else if (code1 == REAL_TYPE && real_zerop (op1))
3510 cp_warning ("division by zero in `%E %% 0.'", op0);
3512 if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
3514 /* Although it would be tempting to shorten always here, that loses
3515 on some targets, since the modulo instruction is undefined if the
3516 quotient can't be represented in the computation mode. We shorten
3517 only if unsigned or if dividing by something we know != -1. */
3518 shorten = ((TREE_CODE (op0) == NOP_EXPR
3519 && TREE_UNSIGNED (TREE_TYPE (TREE_OPERAND (op0, 0))))
3520 || (TREE_CODE (op1) == INTEGER_CST
3521 && ! integer_all_onesp (op1)));
3522 common = 1;
3524 break;
3526 case TRUTH_ANDIF_EXPR:
3527 case TRUTH_ORIF_EXPR:
3528 case TRUTH_AND_EXPR:
3529 case TRUTH_OR_EXPR:
3530 result_type = boolean_type_node;
3531 break;
3533 /* Shift operations: result has same type as first operand;
3534 always convert second operand to int.
3535 Also set SHORT_SHIFT if shifting rightward. */
3537 case RSHIFT_EXPR:
3538 if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
3540 result_type = type0;
3541 if (TREE_CODE (op1) == INTEGER_CST)
3543 if (tree_int_cst_lt (op1, integer_zero_node))
3544 warning ("right shift count is negative");
3545 else
3547 if (! integer_zerop (op1))
3548 short_shift = 1;
3549 if (compare_tree_int (op1, TYPE_PRECISION (type0)) >= 0)
3550 warning ("right shift count >= width of type");
3553 /* Convert the shift-count to an integer, regardless of
3554 size of value being shifted. */
3555 if (TYPE_MAIN_VARIANT (TREE_TYPE (op1)) != integer_type_node)
3556 op1 = cp_convert (integer_type_node, op1);
3557 /* Avoid converting op1 to result_type later. */
3558 converted = 1;
3560 break;
3562 case LSHIFT_EXPR:
3563 if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
3565 result_type = type0;
3566 if (TREE_CODE (op1) == INTEGER_CST)
3568 if (tree_int_cst_lt (op1, integer_zero_node))
3569 warning ("left shift count is negative");
3570 else if (compare_tree_int (op1, TYPE_PRECISION (type0)) >= 0)
3571 warning ("left shift count >= width of type");
3573 /* Convert the shift-count to an integer, regardless of
3574 size of value being shifted. */
3575 if (TYPE_MAIN_VARIANT (TREE_TYPE (op1)) != integer_type_node)
3576 op1 = cp_convert (integer_type_node, op1);
3577 /* Avoid converting op1 to result_type later. */
3578 converted = 1;
3580 break;
3582 case RROTATE_EXPR:
3583 case LROTATE_EXPR:
3584 if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
3586 result_type = type0;
3587 if (TREE_CODE (op1) == INTEGER_CST)
3589 if (tree_int_cst_lt (op1, integer_zero_node))
3590 warning ("%s rotate count is negative",
3591 (code == LROTATE_EXPR) ? "left" : "right");
3592 else if (compare_tree_int (op1, TYPE_PRECISION (type0)) >= 0)
3593 warning ("%s rotate count >= width of type",
3594 (code == LROTATE_EXPR) ? "left" : "right");
3596 /* Convert the shift-count to an integer, regardless of
3597 size of value being shifted. */
3598 if (TYPE_MAIN_VARIANT (TREE_TYPE (op1)) != integer_type_node)
3599 op1 = cp_convert (integer_type_node, op1);
3601 break;
3603 case EQ_EXPR:
3604 case NE_EXPR:
3605 if (warn_float_equal && (code0 == REAL_TYPE || code1 == REAL_TYPE))
3606 warning ("comparing floating point with == or != is unsafe");
3608 build_type = boolean_type_node;
3609 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE
3610 || code0 == COMPLEX_TYPE)
3611 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
3612 || code1 == COMPLEX_TYPE))
3613 short_compare = 1;
3614 else if (code0 == POINTER_TYPE && code1 == POINTER_TYPE)
3615 result_type = composite_pointer_type (type0, type1, op0, op1,
3616 "comparison");
3617 else if (code0 == POINTER_TYPE && null_ptr_cst_p (op1))
3618 result_type = type0;
3619 else if (code1 == POINTER_TYPE && null_ptr_cst_p (op0))
3620 result_type = type1;
3621 else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
3623 result_type = type0;
3624 error ("ISO C++ forbids comparison between pointer and integer");
3626 else if (code0 == INTEGER_TYPE && code1 == POINTER_TYPE)
3628 result_type = type1;
3629 error ("ISO C++ forbids comparison between pointer and integer");
3631 else if (TYPE_PTRMEMFUNC_P (type0) && null_ptr_cst_p (op1))
3633 if (flag_new_abi)
3635 op0 = build_component_ref (op0, pfn_identifier, NULL_TREE, 0);
3636 op1 = cp_convert (TREE_TYPE (op0), integer_zero_node);
3638 else
3640 op0 = build_component_ref (op0, index_identifier, NULL_TREE, 0);
3641 op1 = integer_zero_node;
3643 result_type = TREE_TYPE (op0);
3645 else if (TYPE_PTRMEMFUNC_P (type1) && null_ptr_cst_p (op0))
3646 return cp_build_binary_op (code, op1, op0);
3647 else if (TYPE_PTRMEMFUNC_P (type0) && TYPE_PTRMEMFUNC_P (type1)
3648 && same_type_p (type0, type1))
3650 /* E will be the final comparison. */
3651 tree e;
3652 /* E1 and E2 are for scratch. */
3653 tree e1;
3654 tree e2;
3656 if (TREE_SIDE_EFFECTS (op0))
3657 op0 = save_expr (op0);
3658 if (TREE_SIDE_EFFECTS (op1))
3659 op1 = save_expr (op1);
3661 if (flag_new_abi)
3663 /* We generate:
3665 (op0.pfn == op1.pfn
3666 && (!op0.pfn || op0.delta == op1.delta))
3668 The reason for the `!op0.pfn' bit is that a NULL
3669 pointer-to-member is any member with a zero PFN; the
3670 DELTA field is unspecified. */
3671 tree pfn0;
3672 tree pfn1;
3673 tree delta0;
3674 tree delta1;
3676 pfn0 = pfn_from_ptrmemfunc (op0);
3677 pfn1 = pfn_from_ptrmemfunc (op1);
3678 delta0 = build_component_ref (op0, delta_identifier,
3679 NULL_TREE, 0);
3680 delta1 = build_component_ref (op1, delta_identifier,
3681 NULL_TREE, 0);
3682 e1 = cp_build_binary_op (EQ_EXPR, delta0, delta1);
3683 e2 = cp_build_binary_op (EQ_EXPR,
3684 pfn0,
3685 cp_convert (TREE_TYPE (pfn0),
3686 integer_zero_node));
3687 e1 = cp_build_binary_op (TRUTH_ORIF_EXPR, e1, e2);
3688 e2 = build (EQ_EXPR, boolean_type_node, pfn0, pfn1);
3689 e = cp_build_binary_op (TRUTH_ANDIF_EXPR, e2, e1);
3691 else
3693 /* The code we generate for the test is:
3695 (op0.index == op1.index
3696 && op0.delta == op1.delta
3697 && (op1.index == -1 ? op0.pfn == op1.pfn
3698 : op0.delta2 == op1.delta2)) */
3700 tree index0 = build_component_ref (op0, index_identifier,
3701 NULL_TREE, 0);
3702 tree index1
3703 = save_expr (build_component_ref (op1, index_identifier,
3704 NULL_TREE, 0));
3705 tree delta0 = build_component_ref (op0, delta_identifier,
3706 NULL_TREE, 0);
3707 tree delta1 = build_component_ref (op1, delta_identifier,
3708 NULL_TREE, 0);
3709 tree pfn0 = PFN_FROM_PTRMEMFUNC (op0);
3710 tree pfn1 = PFN_FROM_PTRMEMFUNC (op1);
3711 tree delta20 = DELTA2_FROM_PTRMEMFUNC (op0);
3712 tree delta21 = DELTA2_FROM_PTRMEMFUNC (op1);
3713 tree e3;
3714 tree integer_neg_one_node
3715 = cp_build_binary_op (MINUS_EXPR, integer_zero_node,
3716 integer_one_node);
3717 e1 = cp_build_binary_op (EQ_EXPR, index1, integer_neg_one_node);
3718 /* We can't use build_binary_op for this cmp because it
3719 would get confused by the ptr to method types and
3720 think we want pmfs. */
3721 e2 = build (EQ_EXPR, boolean_type_node, pfn0, pfn1);
3722 e3 = cp_build_binary_op (EQ_EXPR, delta20, delta21);
3723 e = build_conditional_expr (e1, e2, e3);
3724 e1 = cp_build_binary_op (EQ_EXPR, index0, index1);
3725 e = cp_build_binary_op (TRUTH_ANDIF_EXPR, e1, e);
3726 e1 = cp_build_binary_op (EQ_EXPR, delta0, delta1);
3727 e = cp_build_binary_op (TRUTH_ANDIF_EXPR, e1, e);
3729 if (code == EQ_EXPR)
3730 return e;
3731 return cp_build_binary_op (EQ_EXPR, e, integer_zero_node);
3733 else if ((TYPE_PTRMEMFUNC_P (type0)
3734 && same_type_p (TYPE_PTRMEMFUNC_FN_TYPE (type0), type1))
3735 || (TYPE_PTRMEMFUNC_P (type1)
3736 && same_type_p (TYPE_PTRMEMFUNC_FN_TYPE (type1), type0)))
3737 my_friendly_abort (20000221);
3738 break;
3740 case MAX_EXPR:
3741 case MIN_EXPR:
3742 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE)
3743 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE))
3744 shorten = 1;
3745 else if (code0 == POINTER_TYPE && code1 == POINTER_TYPE)
3746 result_type = composite_pointer_type (type0, type1, op0, op1,
3747 "comparison");
3748 break;
3750 case LE_EXPR:
3751 case GE_EXPR:
3752 case LT_EXPR:
3753 case GT_EXPR:
3754 build_type = boolean_type_node;
3755 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE)
3756 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE))
3757 short_compare = 1;
3758 else if (code0 == POINTER_TYPE && code1 == POINTER_TYPE)
3759 result_type = composite_pointer_type (type0, type1, op0, op1,
3760 "comparison");
3761 else if (code0 == POINTER_TYPE && TREE_CODE (op1) == INTEGER_CST
3762 && integer_zerop (op1))
3763 result_type = type0;
3764 else if (code1 == POINTER_TYPE && TREE_CODE (op0) == INTEGER_CST
3765 && integer_zerop (op0))
3766 result_type = type1;
3767 else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
3769 result_type = type0;
3770 pedwarn ("ISO C++ forbids comparison between pointer and integer");
3772 else if (code0 == INTEGER_TYPE && code1 == POINTER_TYPE)
3774 result_type = type1;
3775 pedwarn ("ISO C++ forbids comparison between pointer and integer");
3777 break;
3779 case UNORDERED_EXPR:
3780 case ORDERED_EXPR:
3781 case UNLT_EXPR:
3782 case UNLE_EXPR:
3783 case UNGT_EXPR:
3784 case UNGE_EXPR:
3785 case UNEQ_EXPR:
3786 build_type = integer_type_node;
3787 if (code0 != REAL_TYPE || code1 != REAL_TYPE)
3789 error ("unordered comparison on non-floating point argument");
3790 return error_mark_node;
3792 common = 1;
3793 break;
3795 default:
3796 break;
3799 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE || code0 == COMPLEX_TYPE)
3801 (code1 == INTEGER_TYPE || code1 == REAL_TYPE || code1 == COMPLEX_TYPE))
3803 int none_complex = (code0 != COMPLEX_TYPE && code1 != COMPLEX_TYPE);
3805 if (shorten || common || short_compare)
3806 result_type = common_type (type0, type1);
3808 /* For certain operations (which identify themselves by shorten != 0)
3809 if both args were extended from the same smaller type,
3810 do the arithmetic in that type and then extend.
3812 shorten !=0 and !=1 indicates a bitwise operation.
3813 For them, this optimization is safe only if
3814 both args are zero-extended or both are sign-extended.
3815 Otherwise, we might change the result.
3816 Eg, (short)-1 | (unsigned short)-1 is (int)-1
3817 but calculated in (unsigned short) it would be (unsigned short)-1. */
3819 if (shorten && none_complex)
3821 int unsigned0, unsigned1;
3822 tree arg0 = get_narrower (op0, &unsigned0);
3823 tree arg1 = get_narrower (op1, &unsigned1);
3824 /* UNS is 1 if the operation to be done is an unsigned one. */
3825 int uns = TREE_UNSIGNED (result_type);
3826 tree type;
3828 final_type = result_type;
3830 /* Handle the case that OP0 does not *contain* a conversion
3831 but it *requires* conversion to FINAL_TYPE. */
3833 if (op0 == arg0 && TREE_TYPE (op0) != final_type)
3834 unsigned0 = TREE_UNSIGNED (TREE_TYPE (op0));
3835 if (op1 == arg1 && TREE_TYPE (op1) != final_type)
3836 unsigned1 = TREE_UNSIGNED (TREE_TYPE (op1));
3838 /* Now UNSIGNED0 is 1 if ARG0 zero-extends to FINAL_TYPE. */
3840 /* For bitwise operations, signedness of nominal type
3841 does not matter. Consider only how operands were extended. */
3842 if (shorten == -1)
3843 uns = unsigned0;
3845 /* Note that in all three cases below we refrain from optimizing
3846 an unsigned operation on sign-extended args.
3847 That would not be valid. */
3849 /* Both args variable: if both extended in same way
3850 from same width, do it in that width.
3851 Do it unsigned if args were zero-extended. */
3852 if ((TYPE_PRECISION (TREE_TYPE (arg0))
3853 < TYPE_PRECISION (result_type))
3854 && (TYPE_PRECISION (TREE_TYPE (arg1))
3855 == TYPE_PRECISION (TREE_TYPE (arg0)))
3856 && unsigned0 == unsigned1
3857 && (unsigned0 || !uns))
3858 result_type
3859 = signed_or_unsigned_type (unsigned0,
3860 common_type (TREE_TYPE (arg0),
3861 TREE_TYPE (arg1)));
3862 else if (TREE_CODE (arg0) == INTEGER_CST
3863 && (unsigned1 || !uns)
3864 && (TYPE_PRECISION (TREE_TYPE (arg1))
3865 < TYPE_PRECISION (result_type))
3866 && (type = signed_or_unsigned_type (unsigned1,
3867 TREE_TYPE (arg1)),
3868 int_fits_type_p (arg0, type)))
3869 result_type = type;
3870 else if (TREE_CODE (arg1) == INTEGER_CST
3871 && (unsigned0 || !uns)
3872 && (TYPE_PRECISION (TREE_TYPE (arg0))
3873 < TYPE_PRECISION (result_type))
3874 && (type = signed_or_unsigned_type (unsigned0,
3875 TREE_TYPE (arg0)),
3876 int_fits_type_p (arg1, type)))
3877 result_type = type;
3880 /* Shifts can be shortened if shifting right. */
3882 if (short_shift)
3884 int unsigned_arg;
3885 tree arg0 = get_narrower (op0, &unsigned_arg);
3887 final_type = result_type;
3889 if (arg0 == op0 && final_type == TREE_TYPE (op0))
3890 unsigned_arg = TREE_UNSIGNED (TREE_TYPE (op0));
3892 if (TYPE_PRECISION (TREE_TYPE (arg0)) < TYPE_PRECISION (result_type)
3893 /* We can shorten only if the shift count is less than the
3894 number of bits in the smaller type size. */
3895 && compare_tree_int (op1, TYPE_PRECISION (TREE_TYPE (arg0))) < 0
3896 /* If arg is sign-extended and then unsigned-shifted,
3897 we can simulate this with a signed shift in arg's type
3898 only if the extended result is at least twice as wide
3899 as the arg. Otherwise, the shift could use up all the
3900 ones made by sign-extension and bring in zeros.
3901 We can't optimize that case at all, but in most machines
3902 it never happens because available widths are 2**N. */
3903 && (!TREE_UNSIGNED (final_type)
3904 || unsigned_arg
3905 || (((unsigned) 2 * TYPE_PRECISION (TREE_TYPE (arg0)))
3906 <= TYPE_PRECISION (result_type))))
3908 /* Do an unsigned shift if the operand was zero-extended. */
3909 result_type
3910 = signed_or_unsigned_type (unsigned_arg,
3911 TREE_TYPE (arg0));
3912 /* Convert value-to-be-shifted to that type. */
3913 if (TREE_TYPE (op0) != result_type)
3914 op0 = cp_convert (result_type, op0);
3915 converted = 1;
3919 /* Comparison operations are shortened too but differently.
3920 They identify themselves by setting short_compare = 1. */
3922 if (short_compare)
3924 /* Don't write &op0, etc., because that would prevent op0
3925 from being kept in a register.
3926 Instead, make copies of the our local variables and
3927 pass the copies by reference, then copy them back afterward. */
3928 tree xop0 = op0, xop1 = op1, xresult_type = result_type;
3929 enum tree_code xresultcode = resultcode;
3930 tree val
3931 = shorten_compare (&xop0, &xop1, &xresult_type, &xresultcode);
3932 if (val != 0)
3933 return cp_convert (boolean_type_node, val);
3934 op0 = xop0, op1 = xop1;
3935 converted = 1;
3936 resultcode = xresultcode;
3939 if ((short_compare || code == MIN_EXPR || code == MAX_EXPR)
3940 && warn_sign_compare)
3942 int op0_signed = ! TREE_UNSIGNED (TREE_TYPE (orig_op0));
3943 int op1_signed = ! TREE_UNSIGNED (TREE_TYPE (orig_op1));
3945 int unsignedp0, unsignedp1;
3946 tree primop0 = get_narrower (op0, &unsignedp0);
3947 tree primop1 = get_narrower (op1, &unsignedp1);
3949 /* Check for comparison of different enum types. */
3950 if (TREE_CODE (TREE_TYPE (orig_op0)) == ENUMERAL_TYPE
3951 && TREE_CODE (TREE_TYPE (orig_op1)) == ENUMERAL_TYPE
3952 && TYPE_MAIN_VARIANT (TREE_TYPE (orig_op0))
3953 != TYPE_MAIN_VARIANT (TREE_TYPE (orig_op1)))
3955 cp_warning ("comparison between types `%#T' and `%#T'",
3956 TREE_TYPE (orig_op0), TREE_TYPE (orig_op1));
3959 /* Give warnings for comparisons between signed and unsigned
3960 quantities that may fail. */
3961 /* Do the checking based on the original operand trees, so that
3962 casts will be considered, but default promotions won't be. */
3964 /* Do not warn if the comparison is being done in a signed type,
3965 since the signed type will only be chosen if it can represent
3966 all the values of the unsigned type. */
3967 if (! TREE_UNSIGNED (result_type))
3968 /* OK */;
3969 /* Do not warn if both operands are unsigned. */
3970 else if (op0_signed == op1_signed)
3971 /* OK */;
3972 /* Do not warn if the signed quantity is an unsuffixed
3973 integer literal (or some static constant expression
3974 involving such literals or a conditional expression
3975 involving such literals) and it is non-negative. */
3976 else if ((op0_signed && tree_expr_nonnegative_p (orig_op0))
3977 || (op1_signed && tree_expr_nonnegative_p (orig_op1)))
3978 /* OK */;
3979 /* Do not warn if the comparison is an equality operation,
3980 the unsigned quantity is an integral constant and it does
3981 not use the most significant bit of result_type. */
3982 else if ((resultcode == EQ_EXPR || resultcode == NE_EXPR)
3983 && ((op0_signed && TREE_CODE (orig_op1) == INTEGER_CST
3984 && int_fits_type_p (orig_op1,
3985 signed_type (result_type)))
3986 || (op1_signed && TREE_CODE (orig_op0) == INTEGER_CST
3987 && int_fits_type_p (orig_op0,
3988 signed_type (result_type)))))
3989 /* OK */;
3990 else
3991 warning ("comparison between signed and unsigned integer expressions");
3993 /* Warn if two unsigned values are being compared in a size
3994 larger than their original size, and one (and only one) is the
3995 result of a `~' operator. This comparison will always fail.
3997 Also warn if one operand is a constant, and the constant does not
3998 have all bits set that are set in the ~ operand when it is
3999 extended. */
4001 if ((TREE_CODE (primop0) == BIT_NOT_EXPR)
4002 ^ (TREE_CODE (primop1) == BIT_NOT_EXPR))
4004 if (TREE_CODE (primop0) == BIT_NOT_EXPR)
4005 primop0 = get_narrower (TREE_OPERAND (op0, 0), &unsignedp0);
4006 if (TREE_CODE (primop1) == BIT_NOT_EXPR)
4007 primop1 = get_narrower (TREE_OPERAND (op1, 0), &unsignedp1);
4009 if (host_integerp (primop0, 0) || host_integerp (primop1, 0))
4011 tree primop;
4012 HOST_WIDE_INT constant, mask;
4013 int unsignedp;
4014 unsigned int bits;
4016 if (host_integerp (primop0, 0))
4018 primop = primop1;
4019 unsignedp = unsignedp1;
4020 constant = tree_low_cst (primop0, 0);
4022 else
4024 primop = primop0;
4025 unsignedp = unsignedp0;
4026 constant = tree_low_cst (primop1, 0);
4029 bits = TYPE_PRECISION (TREE_TYPE (primop));
4030 if (bits < TYPE_PRECISION (result_type)
4031 && bits < HOST_BITS_PER_LONG && unsignedp)
4033 mask = (~ (HOST_WIDE_INT) 0) << bits;
4034 if ((mask & constant) != mask)
4035 warning ("comparison of promoted ~unsigned with constant");
4038 else if (unsignedp0 && unsignedp1
4039 && (TYPE_PRECISION (TREE_TYPE (primop0))
4040 < TYPE_PRECISION (result_type))
4041 && (TYPE_PRECISION (TREE_TYPE (primop1))
4042 < TYPE_PRECISION (result_type)))
4043 warning ("comparison of promoted ~unsigned with unsigned");
4048 /* At this point, RESULT_TYPE must be nonzero to avoid an error message.
4049 If CONVERTED is zero, both args will be converted to type RESULT_TYPE.
4050 Then the expression will be built.
4051 It will be given type FINAL_TYPE if that is nonzero;
4052 otherwise, it will be given type RESULT_TYPE. */
4054 if (!result_type)
4056 cp_error ("invalid operands of types `%T' and `%T' to binary `%O'",
4057 TREE_TYPE (orig_op0), TREE_TYPE (orig_op1), code);
4058 return error_mark_node;
4061 /* Issue warnings about peculiar, but legal, uses of NULL. */
4062 if (/* It's reasonable to use pointer values as operands of &&
4063 and ||, so NULL is no exception. */
4064 !(code == TRUTH_ANDIF_EXPR || code == TRUTH_ORIF_EXPR)
4065 && (/* If OP0 is NULL and OP1 is not a pointer, or vice versa. */
4066 (orig_op0 == null_node
4067 && TREE_CODE (TREE_TYPE (op1)) != POINTER_TYPE)
4068 /* Or vice versa. */
4069 || (orig_op1 == null_node
4070 && TREE_CODE (TREE_TYPE (op0)) != POINTER_TYPE)
4071 /* Or, both are NULL and the operation was not a comparison. */
4072 || (orig_op0 == null_node && orig_op1 == null_node
4073 && code != EQ_EXPR && code != NE_EXPR)))
4074 /* Some sort of arithmetic operation involving NULL was
4075 performed. Note that pointer-difference and pointer-addition
4076 have already been handled above, and so we don't end up here in
4077 that case. */
4078 cp_warning ("NULL used in arithmetic");
4080 if (! converted)
4082 if (TREE_TYPE (op0) != result_type)
4083 op0 = cp_convert (result_type, op0);
4084 if (TREE_TYPE (op1) != result_type)
4085 op1 = cp_convert (result_type, op1);
4087 if (op0 == error_mark_node || op1 == error_mark_node)
4088 return error_mark_node;
4091 if (build_type == NULL_TREE)
4092 build_type = result_type;
4095 register tree result = build (resultcode, build_type, op0, op1);
4096 register tree folded;
4098 folded = fold (result);
4099 if (folded == result)
4100 TREE_CONSTANT (folded) = TREE_CONSTANT (op0) & TREE_CONSTANT (op1);
4101 if (final_type != 0)
4102 return cp_convert (final_type, folded);
4103 return folded;
4107 /* Return a tree for the sum or difference (RESULTCODE says which)
4108 of pointer PTROP and integer INTOP. */
4110 static tree
4111 pointer_int_sum (resultcode, ptrop, intop)
4112 enum tree_code resultcode;
4113 register tree ptrop, intop;
4115 tree size_exp;
4117 register tree result;
4118 register tree folded = fold (intop);
4120 /* The result is a pointer of the same type that is being added. */
4122 register tree result_type = TREE_TYPE (ptrop);
4124 if (!complete_type_or_else (result_type, ptrop))
4125 return error_mark_node;
4127 if (TREE_CODE (TREE_TYPE (result_type)) == VOID_TYPE)
4129 if (pedantic || warn_pointer_arith)
4130 pedwarn ("ISO C++ forbids using pointer of type `void *' in pointer arithmetic");
4131 size_exp = integer_one_node;
4133 else if (TREE_CODE (TREE_TYPE (result_type)) == FUNCTION_TYPE)
4135 if (pedantic || warn_pointer_arith)
4136 pedwarn ("ISO C++ forbids using a pointer-to-function in pointer arithmetic");
4137 size_exp = integer_one_node;
4139 else if (TREE_CODE (TREE_TYPE (result_type)) == METHOD_TYPE)
4141 if (pedantic || warn_pointer_arith)
4142 pedwarn ("ISO C++ forbids using a pointer to member function in pointer arithmetic");
4143 size_exp = integer_one_node;
4145 else if (TREE_CODE (TREE_TYPE (result_type)) == OFFSET_TYPE)
4147 if (pedantic || warn_pointer_arith)
4148 pedwarn ("ISO C++ forbids using pointer to a member in pointer arithmetic");
4149 size_exp = integer_one_node;
4151 else
4152 size_exp = size_in_bytes (complete_type (TREE_TYPE (result_type)));
4154 /* Needed to make OOPS V2R3 work. */
4155 intop = folded;
4156 if (integer_zerop (intop))
4157 return ptrop;
4159 /* If what we are about to multiply by the size of the elements
4160 contains a constant term, apply distributive law
4161 and multiply that constant term separately.
4162 This helps produce common subexpressions. */
4164 if ((TREE_CODE (intop) == PLUS_EXPR || TREE_CODE (intop) == MINUS_EXPR)
4165 && ! TREE_CONSTANT (intop)
4166 && TREE_CONSTANT (TREE_OPERAND (intop, 1))
4167 && TREE_CONSTANT (size_exp))
4169 enum tree_code subcode = resultcode;
4170 if (TREE_CODE (intop) == MINUS_EXPR)
4171 subcode = (subcode == PLUS_EXPR ? MINUS_EXPR : PLUS_EXPR);
4172 ptrop = cp_build_binary_op (subcode, ptrop, TREE_OPERAND (intop, 1));
4173 intop = TREE_OPERAND (intop, 0);
4176 /* Convert the integer argument to a type the same size as sizetype
4177 so the multiply won't overflow spuriously. */
4179 if (TYPE_PRECISION (TREE_TYPE (intop)) != TYPE_PRECISION (sizetype))
4180 intop = cp_convert (type_for_size (TYPE_PRECISION (sizetype), 0), intop);
4182 /* Replace the integer argument with a suitable product by the object size.
4183 Do this multiplication as signed, then convert to the appropriate
4184 pointer type (actually unsigned integral). */
4186 intop = cp_convert (result_type,
4187 cp_build_binary_op (MULT_EXPR, intop,
4188 cp_convert (TREE_TYPE (intop),
4189 size_exp)));
4191 /* Create the sum or difference. */
4193 result = build (resultcode, result_type, ptrop, intop);
4195 folded = fold (result);
4196 if (folded == result)
4197 TREE_CONSTANT (folded) = TREE_CONSTANT (ptrop) & TREE_CONSTANT (intop);
4198 return folded;
4201 /* Return a tree for the difference of pointers OP0 and OP1.
4202 The resulting tree has type int. */
4204 static tree
4205 pointer_diff (op0, op1, ptrtype)
4206 register tree op0, op1;
4207 register tree ptrtype;
4209 register tree result, folded;
4210 tree restype = ptrdiff_type_node;
4211 tree target_type = TREE_TYPE (ptrtype);
4213 if (!complete_type_or_else (target_type, NULL_TREE))
4214 return error_mark_node;
4216 if (pedantic || warn_pointer_arith)
4218 if (TREE_CODE (target_type) == VOID_TYPE)
4219 pedwarn ("ISO C++ forbids using pointer of type `void *' in subtraction");
4220 if (TREE_CODE (target_type) == FUNCTION_TYPE)
4221 pedwarn ("ISO C++ forbids using pointer to a function in subtraction");
4222 if (TREE_CODE (target_type) == METHOD_TYPE)
4223 pedwarn ("ISO C++ forbids using pointer to a method in subtraction");
4224 if (TREE_CODE (target_type) == OFFSET_TYPE)
4225 pedwarn ("ISO C++ forbids using pointer to a member in subtraction");
4228 /* First do the subtraction as integers;
4229 then drop through to build the divide operator. */
4231 op0 = cp_build_binary_op (MINUS_EXPR,
4232 cp_convert (restype, op0),
4233 cp_convert (restype, op1));
4235 /* This generates an error if op1 is a pointer to an incomplete type. */
4236 if (!COMPLETE_TYPE_P (TREE_TYPE (TREE_TYPE (op1))))
4237 error ("invalid use of a pointer to an incomplete type in pointer arithmetic");
4239 op1 = ((TREE_CODE (target_type) == VOID_TYPE
4240 || TREE_CODE (target_type) == FUNCTION_TYPE
4241 || TREE_CODE (target_type) == METHOD_TYPE
4242 || TREE_CODE (target_type) == OFFSET_TYPE)
4243 ? integer_one_node
4244 : size_in_bytes (target_type));
4246 /* Do the division. */
4248 result = build (EXACT_DIV_EXPR, restype, op0, cp_convert (restype, op1));
4250 folded = fold (result);
4251 if (folded == result)
4252 TREE_CONSTANT (folded) = TREE_CONSTANT (op0) & TREE_CONSTANT (op1);
4253 return folded;
4256 /* Handle the case of taking the address of a COMPONENT_REF.
4257 Called by `build_unary_op'.
4259 ARG is the COMPONENT_REF whose address we want.
4260 ARGTYPE is the pointer type that this address should have. */
4262 static tree
4263 build_component_addr (arg, argtype)
4264 tree arg, argtype;
4266 tree field = TREE_OPERAND (arg, 1);
4267 tree basetype = decl_type_context (field);
4268 tree rval = build_unary_op (ADDR_EXPR, TREE_OPERAND (arg, 0), 0);
4270 my_friendly_assert (TREE_CODE (field) == FIELD_DECL, 981018);
4272 if (DECL_C_BIT_FIELD (field))
4274 cp_error ("attempt to take address of bit-field structure member `%D'",
4275 field);
4276 return error_mark_node;
4279 if (TREE_CODE (field) == FIELD_DECL
4280 && TYPE_BASE_CONVS_MAY_REQUIRE_CODE_P (basetype))
4282 /* Can't convert directly to ARGTYPE, since that
4283 may have the same pointer type as one of our
4284 baseclasses. */
4285 rval = build1 (NOP_EXPR, argtype,
4286 convert_pointer_to (basetype, rval));
4287 TREE_CONSTANT (rval) = TREE_CONSTANT (TREE_OPERAND (rval, 0));
4289 else
4290 /* This conversion is harmless. */
4291 rval = convert_force (argtype, rval, 0);
4293 return fold (build (PLUS_EXPR, argtype, rval,
4294 cp_convert (argtype, byte_position (field))));
4297 /* Construct and perhaps optimize a tree representation
4298 for a unary operation. CODE, a tree_code, specifies the operation
4299 and XARG is the operand. */
4301 tree
4302 build_x_unary_op (code, xarg)
4303 enum tree_code code;
4304 tree xarg;
4306 tree exp;
4307 int ptrmem = 0;
4309 if (processing_template_decl)
4310 return build_min_nt (code, xarg, NULL_TREE);
4312 /* & rec, on incomplete RECORD_TYPEs is the simple opr &, not an
4313 error message. */
4314 if (code == ADDR_EXPR
4315 && TREE_CODE (xarg) != TEMPLATE_ID_EXPR
4316 && ((IS_AGGR_TYPE_CODE (TREE_CODE (TREE_TYPE (xarg)))
4317 && !COMPLETE_TYPE_P (TREE_TYPE (xarg)))
4318 || (TREE_CODE (xarg) == OFFSET_REF)))
4319 /* don't look for a function */;
4320 else
4322 tree rval;
4324 rval = build_new_op (code, LOOKUP_NORMAL, xarg,
4325 NULL_TREE, NULL_TREE);
4326 if (rval || code != ADDR_EXPR)
4327 return rval;
4329 if (code == ADDR_EXPR)
4331 if (TREE_CODE (xarg) == OFFSET_REF)
4333 ptrmem = PTRMEM_OK_P (xarg);
4335 if (!ptrmem && !flag_ms_extensions
4336 && TREE_CODE (TREE_TYPE (TREE_OPERAND (xarg, 1))) == METHOD_TYPE)
4337 /* A single non-static member, make sure we don't allow a
4338 pointer-to-member. */
4339 xarg = ovl_cons (TREE_OPERAND (xarg, 1), NULL_TREE);
4341 else if (TREE_CODE (xarg) == TARGET_EXPR)
4342 warning ("taking address of temporary");
4344 exp = build_unary_op (code, xarg, 0);
4345 if (TREE_CODE (exp) == ADDR_EXPR)
4346 PTRMEM_OK_P (exp) = ptrmem;
4348 return exp;
4351 /* Like truthvalue_conversion, but handle pointer-to-member constants, where
4352 a null value is represented by an INTEGER_CST of -1. */
4354 tree
4355 cp_truthvalue_conversion (expr)
4356 tree expr;
4358 tree type = TREE_TYPE (expr);
4359 if (TYPE_PTRMEM_P (type))
4360 return build_binary_op (NE_EXPR, expr, integer_zero_node, 1);
4361 else
4362 return truthvalue_conversion (expr);
4365 /* Just like cp_truthvalue_conversion, but we want a CLEANUP_POINT_EXPR. */
4367 tree
4368 condition_conversion (expr)
4369 tree expr;
4371 tree t;
4372 if (processing_template_decl)
4373 return expr;
4374 t = perform_implicit_conversion (boolean_type_node, expr);
4375 t = fold (build1 (CLEANUP_POINT_EXPR, boolean_type_node, t));
4376 return t;
4379 /* C++: Must handle pointers to members.
4381 Perhaps type instantiation should be extended to handle conversion
4382 from aggregates to types we don't yet know we want? (Or are those
4383 cases typically errors which should be reported?)
4385 NOCONVERT nonzero suppresses the default promotions
4386 (such as from short to int). */
4388 tree
4389 build_unary_op (code, xarg, noconvert)
4390 enum tree_code code;
4391 tree xarg;
4392 int noconvert;
4394 /* No default_conversion here. It causes trouble for ADDR_EXPR. */
4395 register tree arg = xarg;
4396 register tree argtype = 0;
4397 const char *errstring = NULL;
4398 tree val;
4400 if (arg == error_mark_node)
4401 return error_mark_node;
4403 switch (code)
4405 case CONVERT_EXPR:
4406 /* This is used for unary plus, because a CONVERT_EXPR
4407 is enough to prevent anybody from looking inside for
4408 associativity, but won't generate any code. */
4409 if (!(arg = build_expr_type_conversion
4410 (WANT_ARITH | WANT_ENUM | WANT_POINTER, arg, 1)))
4411 errstring = "wrong type argument to unary plus";
4412 else
4414 if (!noconvert)
4415 arg = default_conversion (arg);
4416 arg = build1 (NON_LVALUE_EXPR, TREE_TYPE (arg), arg);
4417 TREE_CONSTANT (arg) = TREE_CONSTANT (TREE_OPERAND (arg, 0));
4419 break;
4421 case NEGATE_EXPR:
4422 if (!(arg = build_expr_type_conversion (WANT_ARITH | WANT_ENUM, arg, 1)))
4423 errstring = "wrong type argument to unary minus";
4424 else if (!noconvert)
4425 arg = default_conversion (arg);
4426 break;
4428 case BIT_NOT_EXPR:
4429 if (TREE_CODE (TREE_TYPE (arg)) == COMPLEX_TYPE)
4431 code = CONJ_EXPR;
4432 if (!noconvert)
4433 arg = default_conversion (arg);
4435 else if (!(arg = build_expr_type_conversion (WANT_INT | WANT_ENUM,
4436 arg, 1)))
4437 errstring = "wrong type argument to bit-complement";
4438 else if (!noconvert)
4439 arg = default_conversion (arg);
4440 break;
4442 case ABS_EXPR:
4443 if (!(arg = build_expr_type_conversion (WANT_ARITH | WANT_ENUM, arg, 1)))
4444 errstring = "wrong type argument to abs";
4445 else if (!noconvert)
4446 arg = default_conversion (arg);
4447 break;
4449 case CONJ_EXPR:
4450 /* Conjugating a real value is a no-op, but allow it anyway. */
4451 if (!(arg = build_expr_type_conversion (WANT_ARITH | WANT_ENUM, arg, 1)))
4452 errstring = "wrong type argument to conjugation";
4453 else if (!noconvert)
4454 arg = default_conversion (arg);
4455 break;
4457 case TRUTH_NOT_EXPR:
4458 arg = cp_convert (boolean_type_node, arg);
4459 val = invert_truthvalue (arg);
4460 if (arg != error_mark_node)
4461 return val;
4462 errstring = "in argument to unary !";
4463 break;
4465 case NOP_EXPR:
4466 break;
4468 case REALPART_EXPR:
4469 if (TREE_CODE (arg) == COMPLEX_CST)
4470 return TREE_REALPART (arg);
4471 else if (TREE_CODE (TREE_TYPE (arg)) == COMPLEX_TYPE)
4472 return fold (build1 (REALPART_EXPR, TREE_TYPE (TREE_TYPE (arg)), arg));
4473 else
4474 return arg;
4476 case IMAGPART_EXPR:
4477 if (TREE_CODE (arg) == COMPLEX_CST)
4478 return TREE_IMAGPART (arg);
4479 else if (TREE_CODE (TREE_TYPE (arg)) == COMPLEX_TYPE)
4480 return fold (build1 (IMAGPART_EXPR, TREE_TYPE (TREE_TYPE (arg)), arg));
4481 else
4482 return cp_convert (TREE_TYPE (arg), integer_zero_node);
4484 case PREINCREMENT_EXPR:
4485 case POSTINCREMENT_EXPR:
4486 case PREDECREMENT_EXPR:
4487 case POSTDECREMENT_EXPR:
4488 /* Handle complex lvalues (when permitted)
4489 by reduction to simpler cases. */
4491 val = unary_complex_lvalue (code, arg);
4492 if (val != 0)
4493 return val;
4495 /* Increment or decrement the real part of the value,
4496 and don't change the imaginary part. */
4497 if (TREE_CODE (TREE_TYPE (arg)) == COMPLEX_TYPE)
4499 tree real, imag;
4501 arg = stabilize_reference (arg);
4502 real = build_unary_op (REALPART_EXPR, arg, 1);
4503 imag = build_unary_op (IMAGPART_EXPR, arg, 1);
4504 return build (COMPLEX_EXPR, TREE_TYPE (arg),
4505 build_unary_op (code, real, 1), imag);
4508 /* Report invalid types. */
4510 if (!(arg = build_expr_type_conversion (WANT_ARITH | WANT_POINTER,
4511 arg, 1)))
4513 if (code == PREINCREMENT_EXPR)
4514 errstring ="no pre-increment operator for type";
4515 else if (code == POSTINCREMENT_EXPR)
4516 errstring ="no post-increment operator for type";
4517 else if (code == PREDECREMENT_EXPR)
4518 errstring ="no pre-decrement operator for type";
4519 else
4520 errstring ="no post-decrement operator for type";
4521 break;
4524 /* Report something read-only. */
4526 if (CP_TYPE_CONST_P (TREE_TYPE (arg))
4527 || TREE_READONLY (arg))
4528 readonly_error (arg, ((code == PREINCREMENT_EXPR
4529 || code == POSTINCREMENT_EXPR)
4530 ? "increment" : "decrement"),
4534 register tree inc;
4535 tree result_type = TREE_TYPE (arg);
4537 arg = get_unwidened (arg, 0);
4538 argtype = TREE_TYPE (arg);
4540 /* ARM $5.2.5 last annotation says this should be forbidden. */
4541 if (TREE_CODE (argtype) == ENUMERAL_TYPE)
4542 pedwarn ("ISO C++ forbids %sing an enum",
4543 (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
4544 ? "increment" : "decrement");
4546 /* Compute the increment. */
4548 if (TREE_CODE (argtype) == POINTER_TYPE)
4550 enum tree_code tmp = TREE_CODE (TREE_TYPE (argtype));
4551 tree type = complete_type (TREE_TYPE (argtype));
4553 if (!COMPLETE_OR_VOID_TYPE_P (type))
4554 cp_error ("cannot %s a pointer to incomplete type `%T'",
4555 ((code == PREINCREMENT_EXPR
4556 || code == POSTINCREMENT_EXPR)
4557 ? "increment" : "decrement"), TREE_TYPE (argtype));
4558 else if ((pedantic || warn_pointer_arith)
4559 && (tmp == FUNCTION_TYPE || tmp == METHOD_TYPE
4560 || tmp == VOID_TYPE || tmp == OFFSET_TYPE))
4561 cp_pedwarn ("ISO C++ forbids %sing a pointer of type `%T'",
4562 ((code == PREINCREMENT_EXPR
4563 || code == POSTINCREMENT_EXPR)
4564 ? "increment" : "decrement"), argtype);
4565 inc = c_sizeof_nowarn (TREE_TYPE (argtype));
4567 else
4568 inc = integer_one_node;
4570 inc = cp_convert (argtype, inc);
4572 /* Handle incrementing a cast-expression. */
4574 switch (TREE_CODE (arg))
4576 case NOP_EXPR:
4577 case CONVERT_EXPR:
4578 case FLOAT_EXPR:
4579 case FIX_TRUNC_EXPR:
4580 case FIX_FLOOR_EXPR:
4581 case FIX_ROUND_EXPR:
4582 case FIX_CEIL_EXPR:
4584 tree incremented, modify, value, compound;
4585 if (! lvalue_p (arg) && pedantic)
4586 pedwarn ("cast to non-reference type used as lvalue");
4587 arg = stabilize_reference (arg);
4588 if (code == PREINCREMENT_EXPR || code == PREDECREMENT_EXPR)
4589 value = arg;
4590 else
4591 value = save_expr (arg);
4592 incremented = build (((code == PREINCREMENT_EXPR
4593 || code == POSTINCREMENT_EXPR)
4594 ? PLUS_EXPR : MINUS_EXPR),
4595 argtype, value, inc);
4597 modify = build_modify_expr (arg, NOP_EXPR, incremented);
4598 compound = build (COMPOUND_EXPR, TREE_TYPE (arg), modify, value);
4600 /* Eliminate warning about unused result of + or -. */
4601 TREE_NO_UNUSED_WARNING (compound) = 1;
4602 return compound;
4605 default:
4606 break;
4609 /* Complain about anything else that is not a true lvalue. */
4610 if (!lvalue_or_else (arg, ((code == PREINCREMENT_EXPR
4611 || code == POSTINCREMENT_EXPR)
4612 ? "increment" : "decrement")))
4613 return error_mark_node;
4615 /* Forbid using -- on `bool'. */
4616 if (TREE_TYPE (arg) == boolean_type_node)
4618 if (code == POSTDECREMENT_EXPR || code == PREDECREMENT_EXPR)
4620 cp_error ("invalid use of `--' on bool variable `%D'", arg);
4621 return error_mark_node;
4623 #if 0
4624 /* This will only work if someone can convince Kenner to accept
4625 my patch to expand_increment. (jason) */
4626 val = build (code, TREE_TYPE (arg), arg, inc);
4627 #else
4628 val = boolean_increment (code, arg);
4629 #endif
4631 else
4632 val = build (code, TREE_TYPE (arg), arg, inc);
4634 TREE_SIDE_EFFECTS (val) = 1;
4635 return cp_convert (result_type, val);
4638 case ADDR_EXPR:
4639 /* Note that this operation never does default_conversion
4640 regardless of NOCONVERT. */
4642 argtype = lvalue_type (arg);
4643 if (TREE_CODE (argtype) == REFERENCE_TYPE)
4645 arg = build1
4646 (CONVERT_EXPR,
4647 build_pointer_type (TREE_TYPE (argtype)), arg);
4648 TREE_CONSTANT (arg) = TREE_CONSTANT (TREE_OPERAND (arg, 0));
4649 return arg;
4651 else if (pedantic && DECL_MAIN_P (arg))
4652 /* ARM $3.4 */
4653 pedwarn ("ISO C++ forbids taking address of function `::main'");
4655 /* Let &* cancel out to simplify resulting code. */
4656 if (TREE_CODE (arg) == INDIRECT_REF)
4658 /* We don't need to have `current_class_ptr' wrapped in a
4659 NON_LVALUE_EXPR node. */
4660 if (arg == current_class_ref)
4661 return current_class_ptr;
4663 arg = TREE_OPERAND (arg, 0);
4664 if (TREE_CODE (TREE_TYPE (arg)) == REFERENCE_TYPE)
4666 arg = build1
4667 (CONVERT_EXPR,
4668 build_pointer_type (TREE_TYPE (TREE_TYPE (arg))), arg);
4669 TREE_CONSTANT (arg) = TREE_CONSTANT (TREE_OPERAND (arg, 0));
4671 else if (lvalue_p (arg))
4672 /* Don't let this be an lvalue. */
4673 return non_lvalue (arg);
4674 return arg;
4677 /* For &x[y], return x+y */
4678 if (TREE_CODE (arg) == ARRAY_REF)
4680 if (mark_addressable (TREE_OPERAND (arg, 0)) == 0)
4681 return error_mark_node;
4682 return cp_build_binary_op (PLUS_EXPR, TREE_OPERAND (arg, 0),
4683 TREE_OPERAND (arg, 1));
4686 /* Uninstantiated types are all functions. Taking the
4687 address of a function is a no-op, so just return the
4688 argument. */
4690 if (TREE_CODE (arg) == IDENTIFIER_NODE
4691 && IDENTIFIER_OPNAME_P (arg))
4693 my_friendly_abort (117);
4694 /* We don't know the type yet, so just work around the problem.
4695 We know that this will resolve to an lvalue. */
4696 return build1 (ADDR_EXPR, unknown_type_node, arg);
4699 if (TREE_CODE (arg) == COMPONENT_REF && type_unknown_p (arg)
4700 && OVL_NEXT (TREE_OPERAND (arg, 1)) == NULL_TREE)
4702 /* They're trying to take the address of a unique non-static
4703 member function. This is ill-formed (except in MS-land),
4704 but let's try to DTRT.
4705 Note: We only handle unique functions here because we don't
4706 want to complain if there's a static overload; non-unique
4707 cases will be handled by instantiate_type. But we need to
4708 handle this case here to allow casts on the resulting PMF.
4709 We could defer this in non-MS mode, but it's easier to give
4710 a useful error here. */
4712 tree base = TREE_TYPE (TREE_OPERAND (arg, 0));
4713 tree name = DECL_NAME (OVL_CURRENT (TREE_OPERAND (arg, 1)));
4715 if (! flag_ms_extensions)
4717 if (current_class_type
4718 && TREE_OPERAND (arg, 0) == current_class_ref)
4719 /* An expression like &memfn. */
4720 cp_pedwarn ("ISO C++ forbids taking the address of a non-static member function to form a pointer to member function. Say `&%T::%D'", base, name);
4721 else
4722 cp_pedwarn ("ISO C++ forbids taking the address of a bound member function to form a pointer to member function. Say `&%T::%D'", base, name);
4724 arg = build_offset_ref (base, name);
4727 if (type_unknown_p (arg))
4728 return build1 (ADDR_EXPR, unknown_type_node, arg);
4730 /* Handle complex lvalues (when permitted)
4731 by reduction to simpler cases. */
4732 val = unary_complex_lvalue (code, arg);
4733 if (val != 0)
4734 return val;
4736 switch (TREE_CODE (arg))
4738 case NOP_EXPR:
4739 case CONVERT_EXPR:
4740 case FLOAT_EXPR:
4741 case FIX_TRUNC_EXPR:
4742 case FIX_FLOOR_EXPR:
4743 case FIX_ROUND_EXPR:
4744 case FIX_CEIL_EXPR:
4745 if (! lvalue_p (arg) && pedantic)
4746 pedwarn ("ISO C++ forbids taking the address of a cast to a non-lvalue expression");
4747 break;
4749 default:
4750 break;
4753 /* Allow the address of a constructor if all the elements
4754 are constant. */
4755 if (TREE_CODE (arg) == CONSTRUCTOR && TREE_HAS_CONSTRUCTOR (arg)
4756 && TREE_CONSTANT (arg))
4758 /* Anything not already handled and not a true memory reference
4759 is an error. */
4760 else if (TREE_CODE (argtype) != FUNCTION_TYPE
4761 && TREE_CODE (argtype) != METHOD_TYPE
4762 && !lvalue_or_else (arg, "unary `&'"))
4763 return error_mark_node;
4765 if (argtype != error_mark_node)
4766 argtype = build_pointer_type (argtype);
4768 if (mark_addressable (arg) == 0)
4769 return error_mark_node;
4772 tree addr;
4774 if (TREE_CODE (arg) == COMPONENT_REF)
4775 addr = build_component_addr (arg, argtype);
4776 else
4777 addr = build1 (ADDR_EXPR, argtype, arg);
4779 /* Address of a static or external variable or
4780 function counts as a constant */
4781 if (staticp (arg))
4782 TREE_CONSTANT (addr) = 1;
4784 if (TREE_CODE (argtype) == POINTER_TYPE
4785 && TREE_CODE (TREE_TYPE (argtype)) == METHOD_TYPE)
4787 build_ptrmemfunc_type (argtype);
4788 addr = build_ptrmemfunc (argtype, addr, 0);
4791 return addr;
4794 default:
4795 break;
4798 if (!errstring)
4800 if (argtype == 0)
4801 argtype = TREE_TYPE (arg);
4802 return fold (build1 (code, argtype, arg));
4805 error ("%s", errstring);
4806 return error_mark_node;
4809 /* Apply unary lvalue-demanding operator CODE to the expression ARG
4810 for certain kinds of expressions which are not really lvalues
4811 but which we can accept as lvalues.
4813 If ARG is not a kind of expression we can handle, return zero. */
4815 tree
4816 unary_complex_lvalue (code, arg)
4817 enum tree_code code;
4818 tree arg;
4820 /* Handle (a, b) used as an "lvalue". */
4821 if (TREE_CODE (arg) == COMPOUND_EXPR)
4823 tree real_result = build_unary_op (code, TREE_OPERAND (arg, 1), 0);
4824 return build (COMPOUND_EXPR, TREE_TYPE (real_result),
4825 TREE_OPERAND (arg, 0), real_result);
4828 /* Handle (a ? b : c) used as an "lvalue". */
4829 if (TREE_CODE (arg) == COND_EXPR
4830 || TREE_CODE (arg) == MIN_EXPR || TREE_CODE (arg) == MAX_EXPR)
4831 return rationalize_conditional_expr (code, arg);
4833 if (TREE_CODE (arg) == MODIFY_EXPR
4834 || TREE_CODE (arg) == PREINCREMENT_EXPR
4835 || TREE_CODE (arg) == PREDECREMENT_EXPR)
4836 return unary_complex_lvalue
4837 (code, build (COMPOUND_EXPR, TREE_TYPE (TREE_OPERAND (arg, 0)),
4838 arg, TREE_OPERAND (arg, 0)));
4840 if (code != ADDR_EXPR)
4841 return 0;
4843 /* Handle (a = b) used as an "lvalue" for `&'. */
4844 if (TREE_CODE (arg) == MODIFY_EXPR
4845 || TREE_CODE (arg) == INIT_EXPR)
4847 tree real_result = build_unary_op (code, TREE_OPERAND (arg, 0), 0);
4848 arg = build (COMPOUND_EXPR, TREE_TYPE (real_result), arg, real_result);
4849 TREE_NO_UNUSED_WARNING (arg) = 1;
4850 return arg;
4853 if (TREE_CODE (TREE_TYPE (arg)) == FUNCTION_TYPE
4854 || TREE_CODE (TREE_TYPE (arg)) == METHOD_TYPE
4855 || TREE_CODE (TREE_TYPE (arg)) == OFFSET_TYPE)
4857 /* The representation of something of type OFFSET_TYPE
4858 is really the representation of a pointer to it.
4859 Here give the representation its true type. */
4860 tree t;
4862 my_friendly_assert (TREE_CODE (arg) != SCOPE_REF, 313);
4864 if (TREE_CODE (arg) != OFFSET_REF)
4865 return 0;
4867 t = TREE_OPERAND (arg, 1);
4869 /* Check all this code for right semantics. */
4870 if (TREE_CODE (t) == FUNCTION_DECL)
4872 if (DECL_DESTRUCTOR_P (t))
4873 cp_error ("taking address of destructor");
4874 return build_unary_op (ADDR_EXPR, t, 0);
4876 if (TREE_CODE (t) == VAR_DECL)
4877 return build_unary_op (ADDR_EXPR, t, 0);
4878 else
4880 tree type;
4882 if (TREE_OPERAND (arg, 0)
4883 && ! is_dummy_object (TREE_OPERAND (arg, 0))
4884 && TREE_CODE (t) != FIELD_DECL)
4886 cp_error ("taking address of bound pointer-to-member expression");
4887 return error_mark_node;
4890 type = build_offset_type (DECL_FIELD_CONTEXT (t), TREE_TYPE (t));
4891 type = build_pointer_type (type);
4893 t = make_ptrmem_cst (type, TREE_OPERAND (arg, 1));
4894 return t;
4899 /* We permit compiler to make function calls returning
4900 objects of aggregate type look like lvalues. */
4902 tree targ = arg;
4904 if (TREE_CODE (targ) == SAVE_EXPR)
4905 targ = TREE_OPERAND (targ, 0);
4907 if (TREE_CODE (targ) == CALL_EXPR && IS_AGGR_TYPE (TREE_TYPE (targ)))
4909 if (TREE_CODE (arg) == SAVE_EXPR)
4910 targ = arg;
4911 else
4912 targ = build_cplus_new (TREE_TYPE (arg), arg);
4913 return build1 (ADDR_EXPR, build_pointer_type (TREE_TYPE (arg)), targ);
4916 if (TREE_CODE (arg) == SAVE_EXPR && TREE_CODE (targ) == INDIRECT_REF)
4917 return build (SAVE_EXPR, build_pointer_type (TREE_TYPE (arg)),
4918 TREE_OPERAND (targ, 0), current_function_decl, NULL);
4921 /* Don't let anything else be handled specially. */
4922 return 0;
4925 /* Mark EXP saying that we need to be able to take the
4926 address of it; it should not be allocated in a register.
4927 Value is 1 if successful.
4929 C++: we do not allow `current_class_ptr' to be addressable. */
4932 mark_addressable (exp)
4933 tree exp;
4935 register tree x = exp;
4937 if (TREE_ADDRESSABLE (x) == 1)
4938 return 1;
4940 while (1)
4941 switch (TREE_CODE (x))
4943 case ADDR_EXPR:
4944 case COMPONENT_REF:
4945 case ARRAY_REF:
4946 case REALPART_EXPR:
4947 case IMAGPART_EXPR:
4948 x = TREE_OPERAND (x, 0);
4949 break;
4951 case PARM_DECL:
4952 if (x == current_class_ptr)
4954 if (! flag_this_is_variable)
4955 error ("cannot take the address of `this', which is an ravlue expression");
4956 TREE_ADDRESSABLE (x) = 1; /* so compiler doesn't die later */
4957 return 1;
4959 case VAR_DECL:
4960 /* Caller should not be trying to mark initialized
4961 constant fields addressable. */
4962 my_friendly_assert (DECL_LANG_SPECIFIC (x) == 0
4963 || DECL_IN_AGGR_P (x) == 0
4964 || TREE_STATIC (x)
4965 || DECL_EXTERNAL (x), 314);
4967 case CONST_DECL:
4968 case RESULT_DECL:
4969 if (DECL_REGISTER (x) && !TREE_ADDRESSABLE (x)
4970 && !DECL_ARTIFICIAL (x) && extra_warnings)
4971 cp_warning ("address requested for `%D', which is declared `register'",
4973 TREE_ADDRESSABLE (x) = 1;
4974 return 1;
4976 case FUNCTION_DECL:
4977 TREE_ADDRESSABLE (x) = 1;
4978 TREE_ADDRESSABLE (DECL_ASSEMBLER_NAME (x)) = 1;
4979 return 1;
4981 case CONSTRUCTOR:
4982 TREE_ADDRESSABLE (x) = 1;
4983 return 1;
4985 case TARGET_EXPR:
4986 TREE_ADDRESSABLE (x) = 1;
4987 mark_addressable (TREE_OPERAND (x, 0));
4988 return 1;
4990 default:
4991 return 1;
4995 /* Build and return a conditional expression IFEXP ? OP1 : OP2. */
4997 tree
4998 build_x_conditional_expr (ifexp, op1, op2)
4999 tree ifexp, op1, op2;
5001 if (processing_template_decl)
5002 return build_min_nt (COND_EXPR, ifexp, op1, op2);
5004 return build_conditional_expr (ifexp, op1, op2);
5007 /* Handle overloading of the ',' operator when needed. Otherwise,
5008 this function just builds an expression list. */
5010 tree
5011 build_x_compound_expr (list)
5012 tree list;
5014 tree rest = TREE_CHAIN (list);
5015 tree result;
5017 if (processing_template_decl)
5018 return build_min_nt (COMPOUND_EXPR, list, NULL_TREE);
5020 if (rest == NULL_TREE)
5021 return build_compound_expr (list);
5023 result = build_opfncall (COMPOUND_EXPR, LOOKUP_NORMAL,
5024 TREE_VALUE (list), TREE_VALUE (rest), NULL_TREE);
5025 if (result)
5026 return build_x_compound_expr (tree_cons (NULL_TREE, result,
5027 TREE_CHAIN (rest)));
5029 if (! TREE_SIDE_EFFECTS (TREE_VALUE (list)))
5031 /* FIXME: This test should be in the implicit cast to void of the LHS. */
5032 /* the left-hand operand of a comma expression is like an expression
5033 statement: we should warn if it doesn't have any side-effects,
5034 unless it was explicitly cast to (void). */
5035 if ((extra_warnings || warn_unused_value)
5036 && !(TREE_CODE (TREE_VALUE(list)) == CONVERT_EXPR
5037 && VOID_TYPE_P (TREE_TYPE (TREE_VALUE(list)))))
5038 warning("left-hand operand of comma expression has no effect");
5040 #if 0 /* this requires a gcc backend patch to export warn_if_unused_value */
5041 else if (warn_unused_value)
5042 warn_if_unused_value (TREE_VALUE(list));
5043 #endif
5045 return build_compound_expr
5046 (tree_cons (NULL_TREE, TREE_VALUE (list),
5047 build_tree_list (NULL_TREE,
5048 build_x_compound_expr (rest))));
5051 /* Given a list of expressions, return a compound expression
5052 that performs them all and returns the value of the last of them. */
5054 tree
5055 build_compound_expr (list)
5056 tree list;
5058 register tree rest;
5059 tree first;
5061 TREE_VALUE (list) = decl_constant_value (TREE_VALUE (list));
5063 if (TREE_CHAIN (list) == 0)
5065 /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
5066 Strip such NOP_EXPRs, since LIST is used in non-lvalue context. */
5067 if (TREE_CODE (list) == NOP_EXPR
5068 && TREE_TYPE (list) == TREE_TYPE (TREE_OPERAND (list, 0)))
5069 list = TREE_OPERAND (list, 0);
5071 return TREE_VALUE (list);
5074 first = TREE_VALUE (list);
5075 first = convert_to_void (first, "left-hand operand of comma");
5076 if (first == error_mark_node)
5077 return error_mark_node;
5079 rest = build_compound_expr (TREE_CHAIN (list));
5080 if (rest == error_mark_node)
5081 return error_mark_node;
5083 /* When pedantic, a compound expression cannot be a constant expression. */
5084 if (! TREE_SIDE_EFFECTS (first) && ! pedantic)
5085 return rest;
5087 return build (COMPOUND_EXPR, TREE_TYPE (rest), first, rest);
5090 tree
5091 build_static_cast (type, expr)
5092 tree type, expr;
5094 tree intype;
5095 int ok;
5097 if (type == error_mark_node || expr == error_mark_node)
5098 return error_mark_node;
5100 if (TREE_CODE (expr) == OFFSET_REF)
5101 expr = resolve_offset_ref (expr);
5103 if (processing_template_decl)
5105 tree t = build_min (STATIC_CAST_EXPR, type, expr);
5106 return t;
5109 /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
5110 Strip such NOP_EXPRs if VALUE is being used in non-lvalue context. */
5111 if (TREE_CODE (type) != REFERENCE_TYPE
5112 && TREE_CODE (expr) == NOP_EXPR
5113 && TREE_TYPE (expr) == TREE_TYPE (TREE_OPERAND (expr, 0)))
5114 expr = TREE_OPERAND (expr, 0);
5116 if (TREE_CODE (type) == VOID_TYPE)
5118 expr = convert_to_void (expr, /*implicit=*/NULL);
5119 return expr;
5122 if (TREE_CODE (type) == REFERENCE_TYPE)
5123 return (convert_from_reference
5124 (convert_to_reference (type, expr, CONV_STATIC|CONV_IMPLICIT,
5125 LOOKUP_COMPLAIN, NULL_TREE)));
5127 if (IS_AGGR_TYPE (type))
5128 return build_cplus_new (type, (build_method_call
5129 (NULL_TREE, complete_ctor_identifier,
5130 build_tree_list (NULL_TREE, expr),
5131 TYPE_BINFO (type), LOOKUP_NORMAL)));
5133 expr = decay_conversion (expr);
5134 intype = TREE_TYPE (expr);
5136 /* FIXME handle casting to array type. */
5138 ok = 0;
5139 if (IS_AGGR_TYPE (intype)
5140 ? can_convert_arg (type, intype, expr)
5141 : can_convert_arg (strip_all_pointer_quals (type),
5142 strip_all_pointer_quals (intype), expr))
5143 /* This is a standard conversion. */
5144 ok = 1;
5145 else if (TYPE_PTROB_P (type) && TYPE_PTROB_P (intype))
5147 /* They're pointers to objects. They must be aggregates that
5148 are related non-virtually. */
5150 tree binfo;
5152 if (IS_AGGR_TYPE (TREE_TYPE (type)) && IS_AGGR_TYPE (TREE_TYPE (intype))
5153 && (binfo = get_binfo (TREE_TYPE (intype), TREE_TYPE (type), 0))
5154 && !binfo_from_vbase (binfo))
5155 ok = 1;
5157 else if (TREE_CODE (intype) != BOOLEAN_TYPE
5158 && TREE_CODE (type) != ARRAY_TYPE
5159 && TREE_CODE (type) != FUNCTION_TYPE
5160 && can_convert (intype, strip_all_pointer_quals (type)))
5161 ok = 1;
5163 /* [expr.static.cast]
5165 The static_cast operator shall not be used to cast away
5166 constness. */
5167 if (ok && casts_away_constness (intype, type))
5169 cp_error ("static_cast from type `%T' to type `%T' casts away constness",
5170 intype, type);
5171 return error_mark_node;
5174 if (ok)
5175 return build_c_cast (type, expr);
5177 cp_error ("invalid static_cast from type `%T' to type `%T'", intype, type);
5178 return error_mark_node;
5181 tree
5182 build_reinterpret_cast (type, expr)
5183 tree type, expr;
5185 tree intype;
5187 if (type == error_mark_node || expr == error_mark_node)
5188 return error_mark_node;
5190 if (TREE_CODE (expr) == OFFSET_REF)
5191 expr = resolve_offset_ref (expr);
5193 if (processing_template_decl)
5195 tree t = build_min (REINTERPRET_CAST_EXPR, type, expr);
5196 return t;
5199 if (TREE_CODE (type) != REFERENCE_TYPE)
5201 expr = decay_conversion (expr);
5203 /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
5204 Strip such NOP_EXPRs if VALUE is being used in non-lvalue context. */
5205 if (TREE_CODE (expr) == NOP_EXPR
5206 && TREE_TYPE (expr) == TREE_TYPE (TREE_OPERAND (expr, 0)))
5207 expr = TREE_OPERAND (expr, 0);
5210 intype = TREE_TYPE (expr);
5212 if (TREE_CODE (type) == REFERENCE_TYPE)
5214 if (! real_lvalue_p (expr))
5216 cp_error ("invalid reinterpret_cast of an rvalue expression of type `%T' to type `%T'", intype, type);
5217 return error_mark_node;
5219 expr = build_unary_op (ADDR_EXPR, expr, 0);
5220 if (expr != error_mark_node)
5221 expr = build_reinterpret_cast
5222 (build_pointer_type (TREE_TYPE (type)), expr);
5223 if (expr != error_mark_node)
5224 expr = build_indirect_ref (expr, 0);
5225 return expr;
5227 else if (same_type_ignoring_top_level_qualifiers_p (intype, type))
5228 return build_static_cast (type, expr);
5230 if (TYPE_PTR_P (type) && (TREE_CODE (intype) == INTEGER_TYPE
5231 || TREE_CODE (intype) == ENUMERAL_TYPE))
5232 /* OK */;
5233 else if (TREE_CODE (type) == INTEGER_TYPE && TYPE_PTR_P (intype))
5235 if (TYPE_PRECISION (type) < TYPE_PRECISION (intype))
5236 cp_pedwarn ("reinterpret_cast from `%T' to `%T' loses precision",
5237 intype, type);
5239 else if ((TYPE_PTRFN_P (type) && TYPE_PTRFN_P (intype))
5240 || (TYPE_PTRMEMFUNC_P (type) && TYPE_PTRMEMFUNC_P (intype)))
5242 expr = decl_constant_value (expr);
5243 return fold (build1 (NOP_EXPR, type, expr));
5245 else if ((TYPE_PTRMEM_P (type) && TYPE_PTRMEM_P (intype))
5246 || (TYPE_PTROBV_P (type) && TYPE_PTROBV_P (intype)))
5248 if (! comp_ptr_ttypes_reinterpret (TREE_TYPE (type), TREE_TYPE (intype)))
5249 cp_pedwarn ("reinterpret_cast from `%T' to `%T' casts away const (or volatile)",
5250 intype, type);
5252 expr = decl_constant_value (expr);
5253 return fold (build1 (NOP_EXPR, type, expr));
5255 else if ((TYPE_PTRFN_P (type) && TYPE_PTROBV_P (intype))
5256 || (TYPE_PTRFN_P (intype) && TYPE_PTROBV_P (type)))
5258 pedwarn ("ISO C++ forbids casting between pointer-to-function and pointer-to-object");
5259 expr = decl_constant_value (expr);
5260 return fold (build1 (NOP_EXPR, type, expr));
5262 else
5264 cp_error ("invalid reinterpret_cast from type `%T' to type `%T'",
5265 intype, type);
5266 return error_mark_node;
5269 return cp_convert (type, expr);
5272 tree
5273 build_const_cast (type, expr)
5274 tree type, expr;
5276 tree intype;
5278 if (type == error_mark_node || expr == error_mark_node)
5279 return error_mark_node;
5281 if (TREE_CODE (expr) == OFFSET_REF)
5282 expr = resolve_offset_ref (expr);
5284 if (processing_template_decl)
5286 tree t = build_min (CONST_CAST_EXPR, type, expr);
5287 return t;
5290 if (!POINTER_TYPE_P (type))
5291 cp_error ("invalid use of const_cast with type `%T', which is not a pointer, reference, nor a pointer-to-data-member type", type);
5292 else if (TREE_CODE (TREE_TYPE (type)) == FUNCTION_TYPE)
5294 cp_error ("invalid use of const_cast with type `%T', which is a pointer or reference to a function type", type);
5295 return error_mark_node;
5298 if (TREE_CODE (type) != REFERENCE_TYPE)
5300 expr = decay_conversion (expr);
5302 /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
5303 Strip such NOP_EXPRs if VALUE is being used in non-lvalue context. */
5304 if (TREE_CODE (expr) == NOP_EXPR
5305 && TREE_TYPE (expr) == TREE_TYPE (TREE_OPERAND (expr, 0)))
5306 expr = TREE_OPERAND (expr, 0);
5309 intype = TREE_TYPE (expr);
5311 if (same_type_ignoring_top_level_qualifiers_p (intype, type))
5312 return build_static_cast (type, expr);
5313 else if (TREE_CODE (type) == REFERENCE_TYPE)
5315 if (! real_lvalue_p (expr))
5317 cp_error ("invalid const_cast of an rvalue of type `%T' to type `%T'", intype, type);
5318 return error_mark_node;
5321 if (comp_ptr_ttypes_const (TREE_TYPE (type), intype))
5323 expr = build_unary_op (ADDR_EXPR, expr, 0);
5324 expr = build1 (NOP_EXPR, type, expr);
5325 return convert_from_reference (expr);
5328 else if (TREE_CODE (type) == POINTER_TYPE
5329 && TREE_CODE (intype) == POINTER_TYPE
5330 && comp_ptr_ttypes_const (TREE_TYPE (type), TREE_TYPE (intype)))
5331 return cp_convert (type, expr);
5333 cp_error ("invalid const_cast from type `%T' to type `%T'", intype, type);
5334 return error_mark_node;
5337 /* Build an expression representing a cast to type TYPE of expression EXPR.
5339 ALLOW_NONCONVERTING is true if we should allow non-converting constructors
5340 when doing the cast. */
5342 tree
5343 build_c_cast (type, expr)
5344 tree type, expr;
5346 register tree value = expr;
5347 tree otype;
5349 if (type == error_mark_node || expr == error_mark_node)
5350 return error_mark_node;
5352 if (processing_template_decl)
5354 tree t = build_min (CAST_EXPR, type,
5355 tree_cons (NULL_TREE, value, NULL_TREE));
5356 return t;
5359 /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
5360 Strip such NOP_EXPRs if VALUE is being used in non-lvalue context. */
5361 if (TREE_CODE (type) != REFERENCE_TYPE
5362 && TREE_CODE (value) == NOP_EXPR
5363 && TREE_TYPE (value) == TREE_TYPE (TREE_OPERAND (value, 0)))
5364 value = TREE_OPERAND (value, 0);
5366 if (TREE_CODE (value) == OFFSET_REF)
5367 value = resolve_offset_ref (value);
5369 if (TREE_CODE (type) == ARRAY_TYPE)
5371 /* Allow casting from T1* to T2[] because Cfront allows it.
5372 NIHCL uses it. It is not valid ISO C++ however. */
5373 if (TREE_CODE (TREE_TYPE (expr)) == POINTER_TYPE)
5375 cp_pedwarn ("ISO C++ forbids casting to an array type `%T'", type);
5376 type = build_pointer_type (TREE_TYPE (type));
5378 else
5380 cp_error ("ISO C++ forbids casting to an array type `%T'", type);
5381 return error_mark_node;
5385 if (TREE_CODE (type) == FUNCTION_TYPE
5386 || TREE_CODE (type) == METHOD_TYPE)
5388 cp_error ("invalid cast to function type `%T'", type);
5389 return error_mark_node;
5392 if (TREE_CODE (type) == VOID_TYPE)
5394 /* Conversion to void does not cause any of the normal function to
5395 * pointer, array to pointer and lvalue to rvalue decays. */
5397 value = convert_to_void (value, /*implicit=*/NULL);
5398 return value;
5400 /* Convert functions and arrays to pointers and
5401 convert references to their expanded types,
5402 but don't convert any other types. If, however, we are
5403 casting to a class type, there's no reason to do this: the
5404 cast will only succeed if there is a converting constructor,
5405 and the default conversions will be done at that point. In
5406 fact, doing the default conversion here is actually harmful
5407 in cases like this:
5409 typedef int A[2];
5410 struct S { S(const A&); };
5412 since we don't want the array-to-pointer conversion done. */
5413 if (!IS_AGGR_TYPE (type))
5415 if (TREE_CODE (TREE_TYPE (value)) == FUNCTION_TYPE
5416 || (TREE_CODE (TREE_TYPE (value)) == METHOD_TYPE
5417 /* Don't do the default conversion on a ->* expression. */
5418 && ! (TREE_CODE (type) == POINTER_TYPE
5419 && bound_pmf_p (value)))
5420 || TREE_CODE (TREE_TYPE (value)) == ARRAY_TYPE
5421 || TREE_CODE (TREE_TYPE (value)) == REFERENCE_TYPE)
5422 value = default_conversion (value);
5424 else if (TREE_CODE (TREE_TYPE (value)) == REFERENCE_TYPE)
5425 /* However, even for class types, we still need to strip away
5426 the reference type, since the call to convert_force below
5427 does not expect the input expression to be of reference
5428 type. */
5429 value = convert_from_reference (value);
5431 otype = TREE_TYPE (value);
5433 /* Optionally warn about potentially worrisome casts. */
5435 if (warn_cast_qual
5436 && TREE_CODE (type) == POINTER_TYPE
5437 && TREE_CODE (otype) == POINTER_TYPE
5438 && !at_least_as_qualified_p (TREE_TYPE (type),
5439 TREE_TYPE (otype)))
5440 cp_warning ("cast from `%T' to `%T' discards qualifiers from pointer target type",
5441 otype, type);
5443 if (TREE_CODE (type) == INTEGER_TYPE
5444 && TREE_CODE (otype) == POINTER_TYPE
5445 && TYPE_PRECISION (type) != TYPE_PRECISION (otype))
5446 warning ("cast from pointer to integer of different size");
5448 if (TREE_CODE (type) == POINTER_TYPE
5449 && TREE_CODE (otype) == INTEGER_TYPE
5450 && TYPE_PRECISION (type) != TYPE_PRECISION (otype)
5451 /* Don't warn about converting any constant. */
5452 && !TREE_CONSTANT (value))
5453 warning ("cast to pointer from integer of different size");
5455 if (TREE_CODE (type) == REFERENCE_TYPE)
5456 value = (convert_from_reference
5457 (convert_to_reference (type, value, CONV_C_CAST,
5458 LOOKUP_COMPLAIN, NULL_TREE)));
5459 else
5461 tree ovalue;
5463 value = decl_constant_value (value);
5465 ovalue = value;
5466 value = convert_force (type, value, CONV_C_CAST);
5468 /* Ignore any integer overflow caused by the cast. */
5469 if (TREE_CODE (value) == INTEGER_CST)
5471 TREE_OVERFLOW (value) = TREE_OVERFLOW (ovalue);
5472 TREE_CONSTANT_OVERFLOW (value) = TREE_CONSTANT_OVERFLOW (ovalue);
5476 /* Warn about possible alignment problems. Do this here when we will have
5477 instantiated any necessary template types. */
5478 if (STRICT_ALIGNMENT && warn_cast_align
5479 && TREE_CODE (type) == POINTER_TYPE
5480 && TREE_CODE (otype) == POINTER_TYPE
5481 && TREE_CODE (TREE_TYPE (otype)) != VOID_TYPE
5482 && TREE_CODE (TREE_TYPE (otype)) != FUNCTION_TYPE
5483 && COMPLETE_TYPE_P (TREE_TYPE (otype))
5484 && COMPLETE_TYPE_P (TREE_TYPE (type))
5485 && TYPE_ALIGN (TREE_TYPE (type)) > TYPE_ALIGN (TREE_TYPE (otype)))
5486 cp_warning ("cast from `%T' to `%T' increases required alignment of target type",
5487 otype, type);
5489 /* Always produce some operator for an explicit cast,
5490 so we can tell (for -pedantic) that the cast is no lvalue. */
5491 if (TREE_CODE (type) != REFERENCE_TYPE && value == expr
5492 && real_lvalue_p (value))
5493 value = non_lvalue (value);
5495 return value;
5498 /* Build an assignment expression of lvalue LHS from value RHS.
5499 MODIFYCODE is the code for a binary operator that we use
5500 to combine the old value of LHS with RHS to get the new value.
5501 Or else MODIFYCODE is NOP_EXPR meaning do a simple assignment.
5503 C++: If MODIFYCODE is INIT_EXPR, then leave references unbashed. */
5505 tree
5506 build_modify_expr (lhs, modifycode, rhs)
5507 tree lhs;
5508 enum tree_code modifycode;
5509 tree rhs;
5511 register tree result;
5512 tree newrhs = rhs;
5513 tree lhstype = TREE_TYPE (lhs);
5514 tree olhstype = lhstype;
5515 tree olhs = lhs;
5517 /* Avoid duplicate error messages from operands that had errors. */
5518 if (lhs == error_mark_node || rhs == error_mark_node)
5519 return error_mark_node;
5521 /* Types that aren't fully specified cannot be used in assignments. */
5522 lhs = require_complete_type (lhs);
5524 newrhs = rhs;
5526 /* Handle control structure constructs used as "lvalues". */
5528 switch (TREE_CODE (lhs))
5530 /* Handle --foo = 5; as these are valid constructs in C++ */
5531 case PREDECREMENT_EXPR:
5532 case PREINCREMENT_EXPR:
5533 if (TREE_SIDE_EFFECTS (TREE_OPERAND (lhs, 0)))
5534 lhs = build (TREE_CODE (lhs), TREE_TYPE (lhs),
5535 stabilize_reference (TREE_OPERAND (lhs, 0)),
5536 TREE_OPERAND (lhs, 1));
5537 return build (COMPOUND_EXPR, lhstype,
5538 lhs,
5539 build_modify_expr (TREE_OPERAND (lhs, 0),
5540 modifycode, rhs));
5542 /* Handle (a, b) used as an "lvalue". */
5543 case COMPOUND_EXPR:
5544 newrhs = build_modify_expr (TREE_OPERAND (lhs, 1),
5545 modifycode, rhs);
5546 if (newrhs == error_mark_node)
5547 return error_mark_node;
5548 return build (COMPOUND_EXPR, lhstype,
5549 TREE_OPERAND (lhs, 0), newrhs);
5551 case MODIFY_EXPR:
5552 newrhs = build_modify_expr (TREE_OPERAND (lhs, 0), modifycode, rhs);
5553 if (newrhs == error_mark_node)
5554 return error_mark_node;
5555 return build (COMPOUND_EXPR, lhstype, lhs, newrhs);
5557 /* Handle (a ? b : c) used as an "lvalue". */
5558 case COND_EXPR:
5559 rhs = save_expr (rhs);
5561 /* Produce (a ? (b = rhs) : (c = rhs))
5562 except that the RHS goes through a save-expr
5563 so the code to compute it is only emitted once. */
5564 tree cond;
5566 /* Check this here to avoid odd errors when trying to convert
5567 a throw to the type of the COND_EXPR. */
5568 if (!lvalue_or_else (lhs, "assignment"))
5569 return error_mark_node;
5571 cond = build_conditional_expr
5572 (TREE_OPERAND (lhs, 0),
5573 build_modify_expr (cp_convert (TREE_TYPE (lhs),
5574 TREE_OPERAND (lhs, 1)),
5575 modifycode, rhs),
5576 build_modify_expr (cp_convert (TREE_TYPE (lhs),
5577 TREE_OPERAND (lhs, 2)),
5578 modifycode, rhs));
5580 if (cond == error_mark_node)
5581 return cond;
5582 /* Make sure the code to compute the rhs comes out
5583 before the split. */
5584 return build (COMPOUND_EXPR, TREE_TYPE (lhs),
5585 /* Case to void to suppress warning
5586 from warn_if_unused_value. */
5587 cp_convert (void_type_node, rhs), cond);
5590 default:
5591 break;
5594 if (TREE_CODE (lhs) == OFFSET_REF)
5596 if (TREE_OPERAND (lhs, 0) == NULL_TREE)
5598 /* Static class member? */
5599 tree member = TREE_OPERAND (lhs, 1);
5600 if (TREE_CODE (member) == VAR_DECL)
5601 lhs = member;
5602 else
5604 compiler_error ("invalid static class member");
5605 return error_mark_node;
5608 else
5609 lhs = resolve_offset_ref (lhs);
5611 olhstype = lhstype = TREE_TYPE (lhs);
5614 if (lhs == error_mark_node)
5615 return lhs;
5617 if (TREE_CODE (lhstype) == REFERENCE_TYPE
5618 && modifycode != INIT_EXPR)
5620 lhs = convert_from_reference (lhs);
5621 olhstype = lhstype = TREE_TYPE (lhs);
5624 /* If a binary op has been requested, combine the old LHS value with the RHS
5625 producing the value we should actually store into the LHS. */
5627 if (modifycode == INIT_EXPR)
5629 if (TREE_CODE (rhs) == CONSTRUCTOR)
5631 if (! same_type_p (TREE_TYPE (rhs), lhstype))
5632 abort ();
5633 result = build (INIT_EXPR, lhstype, lhs, rhs);
5634 TREE_SIDE_EFFECTS (result) = 1;
5635 return result;
5637 else if (! IS_AGGR_TYPE (lhstype))
5638 /* Do the default thing */;
5639 else
5641 result = build_method_call (lhs, complete_ctor_identifier,
5642 build_tree_list (NULL_TREE, rhs),
5643 TYPE_BINFO (lhstype), LOOKUP_NORMAL);
5644 if (result == NULL_TREE)
5645 return error_mark_node;
5646 return result;
5649 else if (modifycode == NOP_EXPR)
5651 /* `operator=' is not an inheritable operator. */
5652 if (! IS_AGGR_TYPE (lhstype))
5653 /* Do the default thing */;
5654 else
5656 result = build_opfncall (MODIFY_EXPR, LOOKUP_NORMAL,
5657 lhs, rhs, make_node (NOP_EXPR));
5658 if (result == NULL_TREE)
5659 return error_mark_node;
5660 return result;
5662 lhstype = olhstype;
5664 else if (PROMOTES_TO_AGGR_TYPE (lhstype, REFERENCE_TYPE))
5666 my_friendly_abort (978652);
5668 else
5670 lhs = stabilize_reference (lhs);
5671 newrhs = cp_build_binary_op (modifycode, lhs, rhs);
5672 if (newrhs == error_mark_node)
5674 cp_error (" in evaluation of `%Q(%#T, %#T)'", modifycode,
5675 TREE_TYPE (lhs), TREE_TYPE (rhs));
5676 return error_mark_node;
5680 /* Handle a cast used as an "lvalue".
5681 We have already performed any binary operator using the value as cast.
5682 Now convert the result to the cast type of the lhs,
5683 and then true type of the lhs and store it there;
5684 then convert result back to the cast type to be the value
5685 of the assignment. */
5687 switch (TREE_CODE (lhs))
5689 case NOP_EXPR:
5690 case CONVERT_EXPR:
5691 case FLOAT_EXPR:
5692 case FIX_TRUNC_EXPR:
5693 case FIX_FLOOR_EXPR:
5694 case FIX_ROUND_EXPR:
5695 case FIX_CEIL_EXPR:
5696 if (TREE_CODE (TREE_TYPE (newrhs)) == ARRAY_TYPE
5697 || TREE_CODE (TREE_TYPE (newrhs)) == FUNCTION_TYPE
5698 || TREE_CODE (TREE_TYPE (newrhs)) == METHOD_TYPE
5699 || TREE_CODE (TREE_TYPE (newrhs)) == OFFSET_TYPE)
5700 newrhs = default_conversion (newrhs);
5702 tree inner_lhs = TREE_OPERAND (lhs, 0);
5703 tree result;
5705 /* ISO C++ 5.4/1: The result is an lvalue if T is a reference
5706 type, otherwise the result is an rvalue. */
5707 if (! lvalue_p (lhs))
5708 pedwarn ("ISO C++ forbids cast to non-reference type used as lvalue");
5710 result = build_modify_expr (inner_lhs, NOP_EXPR,
5711 cp_convert (TREE_TYPE (inner_lhs),
5712 cp_convert (lhstype, newrhs)));
5713 if (result == error_mark_node)
5714 return result;
5715 return cp_convert (TREE_TYPE (lhs), result);
5718 default:
5719 break;
5722 /* Now we have handled acceptable kinds of LHS that are not truly lvalues.
5723 Reject anything strange now. */
5725 if (!lvalue_or_else (lhs, "assignment"))
5726 return error_mark_node;
5728 GNU_xref_assign (lhs);
5730 /* Warn about storing in something that is `const'. */
5731 /* For C++, don't warn if this is initialization. */
5732 if (modifycode != INIT_EXPR
5733 && (TREE_READONLY (lhs) || CP_TYPE_CONST_P (lhstype)
5734 /* Functions are not modifiable, even though they are
5735 lvalues. */
5736 || TREE_CODE (TREE_TYPE (lhs)) == FUNCTION_TYPE
5737 || (IS_AGGR_TYPE_CODE (TREE_CODE (lhstype))
5738 && C_TYPE_FIELDS_READONLY (lhstype))
5739 || (TREE_CODE (lhstype) == REFERENCE_TYPE
5740 && CP_TYPE_CONST_P (TREE_TYPE (lhstype)))))
5741 readonly_error (lhs, "assignment", 0);
5743 /* If storing into a structure or union member,
5744 it has probably been given type `int'.
5745 Compute the type that would go with
5746 the actual amount of storage the member occupies. */
5748 if (TREE_CODE (lhs) == COMPONENT_REF
5749 && (TREE_CODE (lhstype) == INTEGER_TYPE
5750 || TREE_CODE (lhstype) == REAL_TYPE
5751 || TREE_CODE (lhstype) == ENUMERAL_TYPE))
5753 lhstype = TREE_TYPE (get_unwidened (lhs, 0));
5755 /* If storing in a field that is in actuality a short or narrower
5756 than one, we must store in the field in its actual type. */
5758 if (lhstype != TREE_TYPE (lhs))
5760 lhs = copy_node (lhs);
5761 TREE_TYPE (lhs) = lhstype;
5765 if (modifycode != INIT_EXPR)
5767 /* Make modifycode now either a NOP_EXPR or an INIT_EXPR. */
5768 modifycode = NOP_EXPR;
5769 /* Reference-bashing */
5770 if (TREE_CODE (lhstype) == REFERENCE_TYPE)
5772 tree tmp = convert_from_reference (lhs);
5773 lhstype = TREE_TYPE (tmp);
5774 if (!COMPLETE_TYPE_P (lhstype))
5776 incomplete_type_error (lhs, lhstype);
5777 return error_mark_node;
5779 lhs = tmp;
5780 olhstype = lhstype;
5782 if (TREE_CODE (TREE_TYPE (newrhs)) == REFERENCE_TYPE)
5784 tree tmp = convert_from_reference (newrhs);
5785 if (!COMPLETE_TYPE_P (TREE_TYPE (tmp)))
5787 incomplete_type_error (newrhs, TREE_TYPE (tmp));
5788 return error_mark_node;
5790 newrhs = tmp;
5794 if (TREE_SIDE_EFFECTS (lhs))
5795 lhs = stabilize_reference (lhs);
5796 if (TREE_SIDE_EFFECTS (newrhs))
5797 newrhs = stabilize_reference (newrhs);
5799 /* Convert new value to destination type. */
5801 if (TREE_CODE (lhstype) == ARRAY_TYPE)
5803 int from_array;
5805 if (!same_or_base_type_p (lhstype, TREE_TYPE (rhs)))
5807 cp_error ("incompatible types in assignment of `%T' to `%T'",
5808 TREE_TYPE (rhs), lhstype);
5809 return error_mark_node;
5812 /* Allow array assignment in compiler-generated code. */
5813 if (pedantic && ! DECL_ARTIFICIAL (current_function_decl))
5814 pedwarn ("ISO C++ forbids assignment of arrays");
5816 from_array = TREE_CODE (TREE_TYPE (newrhs)) == ARRAY_TYPE
5817 ? 1 + (modifycode != INIT_EXPR): 0;
5818 return build_vec_init (lhs, newrhs, from_array);
5821 if (modifycode == INIT_EXPR)
5823 newrhs = convert_for_initialization (lhs, lhstype, newrhs, LOOKUP_NORMAL,
5824 "initialization", NULL_TREE, 0);
5825 if (current_function_decl &&
5826 lhs == DECL_RESULT (current_function_decl))
5828 if (DECL_INITIAL (lhs))
5829 warning ("return value from function receives multiple initializations");
5830 DECL_INITIAL (lhs) = newrhs;
5833 else
5835 /* Avoid warnings on enum bit fields. */
5836 if (TREE_CODE (olhstype) == ENUMERAL_TYPE
5837 && TREE_CODE (lhstype) == INTEGER_TYPE)
5839 newrhs = convert_for_assignment (olhstype, newrhs, "assignment",
5840 NULL_TREE, 0);
5841 newrhs = convert_force (lhstype, newrhs, 0);
5843 else
5844 newrhs = convert_for_assignment (lhstype, newrhs, "assignment",
5845 NULL_TREE, 0);
5846 if (TREE_CODE (newrhs) == CALL_EXPR
5847 && TYPE_NEEDS_CONSTRUCTING (lhstype))
5848 newrhs = build_cplus_new (lhstype, newrhs);
5850 /* Can't initialize directly from a TARGET_EXPR, since that would
5851 cause the lhs to be constructed twice, and possibly result in
5852 accidental self-initialization. So we force the TARGET_EXPR to be
5853 expanded without a target. */
5854 if (TREE_CODE (newrhs) == TARGET_EXPR)
5855 newrhs = build (COMPOUND_EXPR, TREE_TYPE (newrhs), newrhs,
5856 TREE_OPERAND (newrhs, 0));
5859 if (newrhs == error_mark_node)
5860 return error_mark_node;
5862 if (TREE_CODE (newrhs) == COND_EXPR)
5864 tree lhs1;
5865 tree cond = TREE_OPERAND (newrhs, 0);
5867 if (TREE_SIDE_EFFECTS (lhs))
5868 cond = build_compound_expr (tree_cons
5869 (NULL_TREE, lhs,
5870 build_tree_list (NULL_TREE, cond)));
5872 /* Cannot have two identical lhs on this one tree (result) as preexpand
5873 calls will rip them out and fill in RTL for them, but when the
5874 rtl is generated, the calls will only be in the first side of the
5875 condition, not on both, or before the conditional jump! (mrs) */
5876 lhs1 = break_out_calls (lhs);
5878 if (lhs == lhs1)
5879 /* If there's no change, the COND_EXPR behaves like any other rhs. */
5880 result = build (modifycode == NOP_EXPR ? MODIFY_EXPR : INIT_EXPR,
5881 lhstype, lhs, newrhs);
5882 else
5884 tree result_type = TREE_TYPE (newrhs);
5885 /* We have to convert each arm to the proper type because the
5886 types may have been munged by constant folding. */
5887 result
5888 = build (COND_EXPR, result_type, cond,
5889 build_modify_expr (lhs, modifycode,
5890 cp_convert (result_type,
5891 TREE_OPERAND (newrhs, 1))),
5892 build_modify_expr (lhs1, modifycode,
5893 cp_convert (result_type,
5894 TREE_OPERAND (newrhs, 2))));
5897 else
5898 result = build (modifycode == NOP_EXPR ? MODIFY_EXPR : INIT_EXPR,
5899 lhstype, lhs, newrhs);
5901 TREE_SIDE_EFFECTS (result) = 1;
5903 /* If we got the LHS in a different type for storing in,
5904 convert the result back to the nominal type of LHS
5905 so that the value we return always has the same type
5906 as the LHS argument. */
5908 if (olhstype == TREE_TYPE (result))
5909 return result;
5910 /* Avoid warnings converting integral types back into enums
5911 for enum bit fields. */
5912 if (TREE_CODE (TREE_TYPE (result)) == INTEGER_TYPE
5913 && TREE_CODE (olhstype) == ENUMERAL_TYPE)
5915 result = build (COMPOUND_EXPR, olhstype, result, olhs);
5916 TREE_NO_UNUSED_WARNING (result) = 1;
5917 return result;
5919 return convert_for_assignment (olhstype, result, "assignment",
5920 NULL_TREE, 0);
5923 tree
5924 build_x_modify_expr (lhs, modifycode, rhs)
5925 tree lhs;
5926 enum tree_code modifycode;
5927 tree rhs;
5929 if (processing_template_decl)
5930 return build_min_nt (MODOP_EXPR, lhs,
5931 build_min_nt (modifycode, NULL_TREE, NULL_TREE), rhs);
5933 if (modifycode != NOP_EXPR)
5935 tree rval = build_opfncall (MODIFY_EXPR, LOOKUP_NORMAL, lhs, rhs,
5936 make_node (modifycode));
5937 if (rval)
5938 return rval;
5940 return build_modify_expr (lhs, modifycode, rhs);
5944 /* Get difference in deltas for different pointer to member function
5945 types. Return integer_zero_node, if FROM cannot be converted to a
5946 TO type. If FORCE is true, then allow reverse conversions as well.
5948 Note that the naming of FROM and TO is kind of backwards; the return
5949 value is what we add to a TO in order to get a FROM. They are named
5950 this way because we call this function to find out how to convert from
5951 a pointer to member of FROM to a pointer to member of TO. */
5953 static tree
5954 get_delta_difference (from, to, force)
5955 tree from, to;
5956 int force;
5958 tree delta = integer_zero_node;
5959 tree binfo;
5960 tree virt_binfo;
5962 if (to == from)
5963 return delta;
5965 /* Should get_base_distance here, so we can check if any thing along
5966 the path is virtual, and we need to make sure we stay inside the
5967 real binfos when going through virtual bases. Maybe we should
5968 replace virtual bases with BINFO_FOR_VBASE ... (mrs) */
5969 binfo = get_binfo (from, to, 1);
5970 if (binfo == error_mark_node)
5972 error (" in pointer to member function conversion");
5973 return delta;
5975 if (binfo == 0)
5977 if (!force)
5979 error_not_base_type (from, to);
5980 error (" in pointer to member conversion");
5981 return delta;
5983 binfo = get_binfo (to, from, 1);
5984 if (binfo == 0 || binfo == error_mark_node)
5985 return delta;
5986 virt_binfo = binfo_from_vbase (binfo);
5988 if (virt_binfo)
5990 /* This is a reinterpret cast, we choose to do nothing. */
5991 cp_warning ("pointer to member cast via virtual base `%T' of `%T'",
5992 BINFO_TYPE (virt_binfo),
5993 BINFO_TYPE (BINFO_INHERITANCE_CHAIN (virt_binfo)));
5994 return delta;
5996 delta = BINFO_OFFSET (binfo);
5997 delta = cp_convert (ptrdiff_type_node, delta);
5998 delta = cp_build_binary_op (MINUS_EXPR,
5999 integer_zero_node,
6000 delta);
6002 return delta;
6005 virt_binfo = binfo_from_vbase (binfo);
6006 if (virt_binfo)
6008 /* This is a reinterpret cast, we choose to do nothing. */
6009 if (force)
6010 cp_warning ("pointer to member cast via virtual base `%T' of `%T'",
6011 BINFO_TYPE (virt_binfo),
6012 BINFO_TYPE (BINFO_INHERITANCE_CHAIN (virt_binfo)));
6013 else
6014 cp_error ("pointer to member conversion via virtual base `%T' of `%T'",
6015 BINFO_TYPE (virt_binfo),
6016 BINFO_TYPE (BINFO_INHERITANCE_CHAIN (virt_binfo)));
6017 return delta;
6019 delta = BINFO_OFFSET (binfo);
6021 return cp_convert (ptrdiff_type_node, delta);
6024 /* Return a constructor for the pointer-to-member-function TYPE using
6025 the other components as specified. */
6027 tree
6028 build_ptrmemfunc1 (type, delta, idx, pfn, delta2)
6029 tree type, delta, idx, pfn, delta2;
6031 tree u = NULL_TREE;
6032 tree delta_field;
6033 tree idx_field;
6034 tree pfn_or_delta2_field;
6035 tree pfn_field;
6036 tree delta2_field;
6037 tree subtype;
6038 int allconstant, allsimple;
6040 /* Pull the FIELD_DECLs out of the type. */
6041 if (flag_new_abi)
6043 pfn_field = TYPE_FIELDS (type);
6044 delta_field = TREE_CHAIN (pfn_field);
6045 idx_field = NULL_TREE;
6046 pfn_or_delta2_field = NULL_TREE;
6047 delta2_field = NULL_TREE;
6048 subtype = NULL_TREE;
6050 else
6052 delta_field = TYPE_FIELDS (type);
6053 idx_field = TREE_CHAIN (delta_field);
6054 pfn_or_delta2_field = TREE_CHAIN (idx_field);
6055 subtype = TREE_TYPE (pfn_or_delta2_field);
6056 pfn_field = TYPE_FIELDS (subtype);
6057 delta2_field = TREE_CHAIN (pfn_field);
6060 /* Make sure DELTA has the type we want. */
6061 delta = convert_and_check (delta_type_node, delta);
6063 /* Keep track of whether the initializer is a) constant, and b) can
6064 be done statically. */
6065 allconstant = TREE_CONSTANT (delta);
6066 allsimple = (initializer_constant_valid_p (delta, TREE_TYPE (delta))
6067 != NULL_TREE);
6069 if (pfn)
6071 /* A non-virtual function. */
6072 if (!flag_new_abi)
6073 u = build_tree_list (pfn_field, pfn);
6075 allconstant &= TREE_CONSTANT (pfn);
6076 allsimple &= (initializer_constant_valid_p (pfn, TREE_TYPE (pfn))
6077 != NULL_TREE);
6079 else
6081 /* A virtual function. */
6082 if (flag_new_abi)
6084 allconstant &= TREE_CONSTANT (pfn);
6085 allsimple &= (initializer_constant_valid_p (pfn, TREE_TYPE (pfn))
6086 != NULL_TREE);
6088 else
6090 idx = convert_and_check (delta_type_node, idx);
6091 u = build_tree_list (delta2_field, delta2);
6093 allconstant &= TREE_CONSTANT (idx) && TREE_CONSTANT (delta2);
6094 allsimple &= ((initializer_constant_valid_p (idx, TREE_TYPE (idx))
6095 != NULL_TREE)
6096 && (initializer_constant_valid_p (delta2,
6097 TREE_TYPE (delta2))
6098 != NULL_TREE));
6102 /* Finish creating the initializer. */
6103 if (flag_new_abi)
6104 u = tree_cons (pfn_field, pfn,
6105 build_tree_list (delta_field, delta));
6106 else
6108 u = build (CONSTRUCTOR, subtype, NULL_TREE, u);
6109 u = tree_cons (delta_field, delta,
6110 tree_cons (idx_field,
6111 idx,
6112 build_tree_list (pfn_or_delta2_field,
6113 u)));
6115 u = build (CONSTRUCTOR, type, NULL_TREE, u);
6116 TREE_CONSTANT (u) = allconstant;
6117 TREE_STATIC (u) = allconstant && allsimple;
6118 return u;
6121 /* Build a constructor for a pointer to member function. It can be
6122 used to initialize global variables, local variable, or used
6123 as a value in expressions. TYPE is the POINTER to METHOD_TYPE we
6124 want to be.
6126 If FORCE is non-zero, then force this conversion, even if
6127 we would rather not do it. Usually set when using an explicit
6128 cast.
6130 Return error_mark_node, if something goes wrong. */
6132 tree
6133 build_ptrmemfunc (type, pfn, force)
6134 tree type, pfn;
6135 int force;
6137 tree fn;
6138 tree pfn_type = TREE_TYPE (pfn);
6139 tree to_type = build_ptrmemfunc_type (type);
6141 /* Handle multiple conversions of pointer to member functions. */
6142 if (TYPE_PTRMEMFUNC_P (TREE_TYPE (pfn)))
6144 tree idx = integer_zero_node;
6145 tree delta = integer_zero_node;
6146 tree delta2 = integer_zero_node;
6147 tree npfn = NULL_TREE;
6148 tree ndelta, ndelta2;
6149 tree e1, e2, e3, n;
6151 if (!force
6152 && !can_convert_arg (to_type, TREE_TYPE (pfn), pfn))
6153 cp_error ("invalid conversion to type `%T' from type `%T'",
6154 to_type, pfn_type);
6156 n = get_delta_difference (TYPE_PTRMEMFUNC_OBJECT_TYPE (pfn_type),
6157 TYPE_PTRMEMFUNC_OBJECT_TYPE (to_type),
6158 force);
6160 /* We don't have to do any conversion to convert a
6161 pointer-to-member to its own type. But, we don't want to
6162 just return a PTRMEM_CST if there's an explicit cast; that
6163 cast should make the expression an invalid template argument. */
6164 if (TREE_CODE (pfn) != PTRMEM_CST)
6166 if (same_type_p (to_type, pfn_type))
6167 return pfn;
6168 else if (integer_zerop (n))
6169 return build_reinterpret_cast (to_type, pfn);
6172 if (TREE_SIDE_EFFECTS (pfn))
6173 pfn = save_expr (pfn);
6175 if (flag_new_abi)
6177 /* Under the new ABI, the conversion is easy. Just adjust
6178 the DELTA field. */
6179 npfn = build_component_ref (pfn, pfn_identifier, NULL_TREE, 0);
6180 delta = build_component_ref (pfn, delta_identifier, NULL_TREE, 0);
6181 delta = cp_convert (ptrdiff_type_node, delta);
6182 delta = cp_build_binary_op (PLUS_EXPR, delta, n);
6183 return build_ptrmemfunc1 (to_type, delta, NULL_TREE, npfn,
6184 NULL_TREE);
6187 if (TREE_CODE (pfn) == PTRMEM_CST)
6189 /* We could just build the resulting CONSTRUCTOR now, but we
6190 don't, relying on the general machinery below, together
6191 with constant-folding, to do the right thing. */
6192 expand_ptrmemfunc_cst (pfn, &ndelta, &idx, &npfn, &ndelta2);
6193 if (npfn)
6194 /* This constant points to a non-virtual function.
6195 NDELTA2 will be NULL, but it's value doesn't really
6196 matter since we won't use it anyhow. */
6197 ndelta2 = integer_zero_node;
6199 else
6201 ndelta = cp_convert (ptrdiff_type_node,
6202 build_component_ref (pfn,
6203 delta_identifier,
6204 NULL_TREE, 0));
6205 ndelta2 = cp_convert (ptrdiff_type_node,
6206 DELTA2_FROM_PTRMEMFUNC (pfn));
6207 idx = build_component_ref (pfn, index_identifier, NULL_TREE, 0);
6210 delta = cp_build_binary_op (PLUS_EXPR, ndelta, n);
6211 delta2 = cp_build_binary_op (PLUS_EXPR, ndelta2, n);
6212 e1 = fold (build (GT_EXPR, boolean_type_node, idx, integer_zero_node));
6214 /* If it's a virtual function, this is what we want. */
6215 e2 = build_ptrmemfunc1 (to_type, delta, idx, NULL_TREE, delta2);
6217 pfn = PFN_FROM_PTRMEMFUNC (pfn);
6218 npfn = build1 (NOP_EXPR, type, pfn);
6219 TREE_CONSTANT (npfn) = TREE_CONSTANT (pfn);
6221 /* But if it's a non-virtual function, or NULL, we use this
6222 instead. */
6223 e3 = build_ptrmemfunc1 (to_type, delta, idx, npfn, NULL_TREE);
6224 return build_conditional_expr (e1, e2, e3);
6227 /* Handle null pointer to member function conversions. */
6228 if (integer_zerop (pfn))
6230 pfn = build_c_cast (type, integer_zero_node);
6231 return build_ptrmemfunc1 (to_type,
6232 integer_zero_node, integer_zero_node,
6233 pfn, NULL_TREE);
6236 if (type_unknown_p (pfn))
6237 return instantiate_type (type, pfn, itf_complain);
6239 fn = TREE_OPERAND (pfn, 0);
6240 my_friendly_assert (TREE_CODE (fn) == FUNCTION_DECL, 0);
6241 return make_ptrmem_cst (to_type, fn);
6244 /* Return the DELTA, IDX, PFN, and DELTA2 values for the PTRMEM_CST
6245 given by CST.
6247 ??? There is no consistency as to the types returned for the above
6248 values. Some code acts as if its a sizetype and some as if its
6249 integer_type_node. */
6251 void
6252 expand_ptrmemfunc_cst (cst, delta, idx, pfn, delta2)
6253 tree cst;
6254 tree *delta;
6255 tree *idx;
6256 tree *pfn;
6257 tree *delta2;
6259 tree type = TREE_TYPE (cst);
6260 tree fn = PTRMEM_CST_MEMBER (cst);
6261 tree ptr_class, fn_class;
6263 my_friendly_assert (TREE_CODE (fn) == FUNCTION_DECL, 0);
6265 /* The class that the function belongs to. */
6266 fn_class = DECL_CONTEXT (fn);
6268 /* The class that we're creating a pointer to member of. */
6269 ptr_class = TYPE_PTRMEMFUNC_OBJECT_TYPE (type);
6271 /* First, calculate the adjustment to the function's class. */
6272 *delta = get_delta_difference (fn_class, ptr_class, /*force=*/0);
6274 if (!DECL_VIRTUAL_P (fn))
6276 if (!flag_new_abi)
6277 *idx = build_int_2 (-1, -1);
6278 else
6279 *idx = NULL_TREE;
6280 *pfn = convert (TYPE_PTRMEMFUNC_FN_TYPE (type), build_addr_func (fn));
6281 *delta2 = NULL_TREE;
6283 else
6285 /* If we're dealing with a virtual function, we have to adjust 'this'
6286 again, to point to the base which provides the vtable entry for
6287 fn; the call will do the opposite adjustment. */
6288 tree orig_class = DECL_VIRTUAL_CONTEXT (fn);
6289 tree binfo = binfo_or_else (orig_class, fn_class);
6290 *delta = fold (build (PLUS_EXPR, TREE_TYPE (*delta),
6291 *delta, BINFO_OFFSET (binfo)));
6293 if (!flag_new_abi)
6295 /* Map everything down one to make room for the null PMF. */
6296 *idx = fold (build (PLUS_EXPR, integer_type_node,
6297 DECL_VINDEX (fn), integer_one_node));
6298 *pfn = NULL_TREE;
6300 else
6302 /* Under the new ABI, we set PFN to the vtable offset, plus
6303 one, at which the function can be found. */
6304 *idx = NULL_TREE;
6305 *pfn = fold (build (MULT_EXPR, integer_type_node,
6306 DECL_VINDEX (fn),
6307 TYPE_SIZE_UNIT (vtable_entry_type)));
6308 *pfn = fold (build (PLUS_EXPR, integer_type_node, *pfn,
6309 integer_one_node));
6310 *pfn = fold (build1 (NOP_EXPR, TYPE_PTRMEMFUNC_FN_TYPE (type),
6311 *pfn));
6314 /* Offset from an object of PTR_CLASS to the vptr for ORIG_CLASS. */
6315 *delta2 = fold (build (PLUS_EXPR, integer_type_node, *delta,
6316 get_vfield_offset (TYPE_BINFO (orig_class))));
6320 /* Return an expression for DELTA2 from the pointer-to-member function
6321 given by T. */
6323 tree
6324 delta2_from_ptrmemfunc (t)
6325 tree t;
6327 my_friendly_assert (!flag_new_abi, 20000221);
6329 if (TREE_CODE (t) == PTRMEM_CST)
6331 tree delta;
6332 tree idx;
6333 tree pfn;
6334 tree delta2;
6336 expand_ptrmemfunc_cst (t, &delta, &idx, &pfn, &delta2);
6337 if (delta2)
6338 return delta2;
6341 return (build_component_ref
6342 (build_component_ref (t,
6343 pfn_or_delta2_identifier, NULL_TREE,
6344 0),
6345 delta2_identifier, NULL_TREE, 0));
6348 /* Return an expression for PFN from the pointer-to-member function
6349 given by T. */
6351 tree
6352 pfn_from_ptrmemfunc (t)
6353 tree t;
6355 if (TREE_CODE (t) == PTRMEM_CST)
6357 tree delta;
6358 tree idx;
6359 tree pfn;
6360 tree delta2;
6362 expand_ptrmemfunc_cst (t, &delta, &idx, &pfn, &delta2);
6363 if (pfn)
6364 return pfn;
6367 if (flag_new_abi)
6368 return build_component_ref (t, pfn_identifier, NULL_TREE, 0);
6369 else
6370 return (build_component_ref
6371 (build_component_ref (t,
6372 pfn_or_delta2_identifier, NULL_TREE,
6373 0),
6374 pfn_identifier, NULL_TREE, 0));
6377 /* Expression EXPR is about to be implicitly converted to TYPE. Warn
6378 if this is a potentially dangerous thing to do. Returns a possibly
6379 marked EXPR. */
6381 tree
6382 dubious_conversion_warnings (type, expr, errtype, fndecl, parmnum)
6383 tree type;
6384 tree expr;
6385 const char *errtype;
6386 tree fndecl;
6387 int parmnum;
6389 if (TREE_CODE (type) == REFERENCE_TYPE)
6390 type = TREE_TYPE (type);
6392 /* Issue warnings about peculiar, but legal, uses of NULL. */
6393 if (ARITHMETIC_TYPE_P (type) && expr == null_node)
6395 if (fndecl)
6396 cp_warning ("passing NULL used for non-pointer %s %P of `%D'",
6397 errtype, parmnum, fndecl);
6398 else
6399 cp_warning ("%s to non-pointer type `%T' from NULL", errtype, type);
6402 /* Warn about assigning a floating-point type to an integer type. */
6403 if (TREE_CODE (TREE_TYPE (expr)) == REAL_TYPE
6404 && TREE_CODE (type) == INTEGER_TYPE)
6406 if (fndecl)
6407 cp_warning ("passing `%T' for %s %P of `%D'",
6408 TREE_TYPE (expr), errtype, parmnum, fndecl);
6409 else
6410 cp_warning ("%s to `%T' from `%T'", errtype, type, TREE_TYPE (expr));
6412 /* And warn about assigning a negative value to an unsigned
6413 variable. */
6414 else if (TREE_UNSIGNED (type) && TREE_CODE (type) != BOOLEAN_TYPE)
6416 if (TREE_CODE (expr) == INTEGER_CST
6417 && TREE_NEGATED_INT (expr))
6419 if (fndecl)
6420 cp_warning ("passing negative value `%E' for %s %P of `%D'",
6421 expr, errtype, parmnum, fndecl);
6422 else
6423 cp_warning ("%s of negative value `%E' to `%T'",
6424 errtype, expr, type);
6427 overflow_warning (expr);
6429 if (TREE_CONSTANT (expr))
6430 expr = fold (expr);
6432 return expr;
6435 /* Convert value RHS to type TYPE as preparation for an assignment to
6436 an lvalue of type TYPE. ERRTYPE is a string to use in error
6437 messages: "assignment", "return", etc. If FNDECL is non-NULL, we
6438 are doing the conversion in order to pass the PARMNUMth argument of
6439 FNDECL. */
6441 static tree
6442 convert_for_assignment (type, rhs, errtype, fndecl, parmnum)
6443 tree type, rhs;
6444 const char *errtype;
6445 tree fndecl;
6446 int parmnum;
6448 register enum tree_code codel = TREE_CODE (type);
6449 register tree rhstype;
6450 register enum tree_code coder;
6452 if (codel == OFFSET_TYPE)
6453 my_friendly_abort (990505);
6455 if (TREE_CODE (rhs) == OFFSET_REF)
6456 rhs = resolve_offset_ref (rhs);
6458 /* Strip NON_LVALUE_EXPRs since we aren't using as an lvalue. */
6459 if (TREE_CODE (rhs) == NON_LVALUE_EXPR)
6460 rhs = TREE_OPERAND (rhs, 0);
6462 rhstype = TREE_TYPE (rhs);
6463 coder = TREE_CODE (rhstype);
6465 if (rhs == error_mark_node || rhstype == error_mark_node)
6466 return error_mark_node;
6467 if (TREE_CODE (rhs) == TREE_LIST && TREE_VALUE (rhs) == error_mark_node)
6468 return error_mark_node;
6470 rhs = dubious_conversion_warnings (type, rhs, errtype, fndecl, parmnum);
6472 /* The RHS of an assignment cannot have void type. */
6473 if (coder == VOID_TYPE)
6475 error ("void value not ignored as it ought to be");
6476 return error_mark_node;
6479 /* Simplify the RHS if possible. */
6480 if (TREE_CODE (rhs) == CONST_DECL)
6481 rhs = DECL_INITIAL (rhs);
6482 else if (coder != ARRAY_TYPE)
6483 rhs = decl_constant_value (rhs);
6485 /* [expr.ass]
6487 The expression is implicitly converted (clause _conv_) to the
6488 cv-unqualified type of the left operand. */
6489 if (!can_convert_arg (type, rhstype, rhs))
6491 /* When -Wno-pmf-conversions is use, we just silently allow
6492 conversions from pointers-to-members to plain pointers. If
6493 the conversion doesn't work, cp_convert will complain. */
6494 if (!warn_pmf2ptr
6495 && TYPE_PTR_P (type)
6496 && TYPE_PTRMEMFUNC_P (rhstype))
6497 rhs = cp_convert (strip_top_quals (type), rhs);
6498 else
6500 /* If the right-hand side has unknown type, then it is an
6501 overloaded function. Call instantiate_type to get error
6502 messages. */
6503 if (rhstype == unknown_type_node)
6504 instantiate_type (type, rhs, itf_complain);
6505 else if (fndecl)
6506 cp_error ("cannot convert `%T' to `%T' for argument `%P' to `%D'",
6507 rhstype, type, parmnum, fndecl);
6508 else
6509 cp_error ("cannot convert `%T' to `%T' in %s", rhstype, type,
6510 errtype);
6511 return error_mark_node;
6514 return perform_implicit_conversion (strip_top_quals (type), rhs);
6517 /* Convert RHS to be of type TYPE.
6518 If EXP is non-zero, it is the target of the initialization.
6519 ERRTYPE is a string to use in error messages.
6521 Two major differences between the behavior of
6522 `convert_for_assignment' and `convert_for_initialization'
6523 are that references are bashed in the former, while
6524 copied in the latter, and aggregates are assigned in
6525 the former (operator=) while initialized in the
6526 latter (X(X&)).
6528 If using constructor make sure no conversion operator exists, if one does
6529 exist, an ambiguity exists.
6531 If flags doesn't include LOOKUP_COMPLAIN, don't complain about anything. */
6533 tree
6534 convert_for_initialization (exp, type, rhs, flags, errtype, fndecl, parmnum)
6535 tree exp, type, rhs;
6536 int flags;
6537 const char *errtype;
6538 tree fndecl;
6539 int parmnum;
6541 register enum tree_code codel = TREE_CODE (type);
6542 register tree rhstype;
6543 register enum tree_code coder;
6545 /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
6546 Strip such NOP_EXPRs, since RHS is used in non-lvalue context. */
6547 if (TREE_CODE (rhs) == NOP_EXPR
6548 && TREE_TYPE (rhs) == TREE_TYPE (TREE_OPERAND (rhs, 0))
6549 && codel != REFERENCE_TYPE)
6550 rhs = TREE_OPERAND (rhs, 0);
6552 if (rhs == error_mark_node
6553 || (TREE_CODE (rhs) == TREE_LIST && TREE_VALUE (rhs) == error_mark_node))
6554 return error_mark_node;
6556 if (TREE_CODE (rhs) == OFFSET_REF)
6558 rhs = resolve_offset_ref (rhs);
6559 if (rhs == error_mark_node)
6560 return error_mark_node;
6563 if (TREE_CODE (TREE_TYPE (rhs)) == REFERENCE_TYPE)
6564 rhs = convert_from_reference (rhs);
6566 if ((TREE_CODE (TREE_TYPE (rhs)) == ARRAY_TYPE
6567 && TREE_CODE (type) != ARRAY_TYPE
6568 && (TREE_CODE (type) != REFERENCE_TYPE
6569 || TREE_CODE (TREE_TYPE (type)) != ARRAY_TYPE))
6570 || (TREE_CODE (TREE_TYPE (rhs)) == FUNCTION_TYPE
6571 && (TREE_CODE (type) != REFERENCE_TYPE
6572 || TREE_CODE (TREE_TYPE (type)) != FUNCTION_TYPE))
6573 || TREE_CODE (TREE_TYPE (rhs)) == METHOD_TYPE)
6574 rhs = default_conversion (rhs);
6576 rhstype = TREE_TYPE (rhs);
6577 coder = TREE_CODE (rhstype);
6579 if (coder == ERROR_MARK)
6580 return error_mark_node;
6582 /* We accept references to incomplete types, so we can
6583 return here before checking if RHS is of complete type. */
6585 if (codel == REFERENCE_TYPE)
6587 /* This should eventually happen in convert_arguments. */
6588 extern int warningcount, errorcount;
6589 int savew = 0, savee = 0;
6591 if (fndecl)
6592 savew = warningcount, savee = errorcount;
6593 rhs = initialize_reference (type, rhs);
6594 if (fndecl)
6596 if (warningcount > savew)
6597 cp_warning_at ("in passing argument %P of `%+D'", parmnum, fndecl);
6598 else if (errorcount > savee)
6599 cp_error_at ("in passing argument %P of `%+D'", parmnum, fndecl);
6601 return rhs;
6604 if (exp != 0)
6605 exp = require_complete_type (exp);
6606 if (exp == error_mark_node)
6607 return error_mark_node;
6609 if (TREE_CODE (rhstype) == REFERENCE_TYPE)
6610 rhstype = TREE_TYPE (rhstype);
6612 type = complete_type (type);
6614 if (IS_AGGR_TYPE (type))
6615 return ocp_convert (type, rhs, CONV_IMPLICIT|CONV_FORCE_TEMP, flags);
6617 return convert_for_assignment (type, rhs, errtype, fndecl, parmnum);
6620 /* Expand an ASM statement with operands, handling output operands
6621 that are not variables or INDIRECT_REFS by transforming such
6622 cases into cases that expand_asm_operands can handle.
6624 Arguments are same as for expand_asm_operands.
6626 We don't do default conversions on all inputs, because it can screw
6627 up operands that are expected to be in memory. */
6629 void
6630 c_expand_asm_operands (string, outputs, inputs, clobbers, vol, filename, line)
6631 tree string, outputs, inputs, clobbers;
6632 int vol;
6633 const char *filename;
6634 int line;
6636 int noutputs = list_length (outputs);
6637 register int i;
6638 /* o[I] is the place that output number I should be written. */
6639 register tree *o = (tree *) alloca (noutputs * sizeof (tree));
6640 register tree tail;
6642 /* Record the contents of OUTPUTS before it is modified. */
6643 for (i = 0, tail = outputs; tail; tail = TREE_CHAIN (tail), i++)
6644 o[i] = TREE_VALUE (tail);
6646 /* Generate the ASM_OPERANDS insn;
6647 store into the TREE_VALUEs of OUTPUTS some trees for
6648 where the values were actually stored. */
6649 expand_asm_operands (string, outputs, inputs, clobbers, vol, filename, line);
6651 /* Copy all the intermediate outputs into the specified outputs. */
6652 for (i = 0, tail = outputs; tail; tail = TREE_CHAIN (tail), i++)
6654 if (o[i] != TREE_VALUE (tail))
6656 expand_expr (build_modify_expr (o[i], NOP_EXPR, TREE_VALUE (tail)),
6657 const0_rtx, VOIDmode, EXPAND_NORMAL);
6658 free_temp_slots ();
6660 /* Restore the original value so that it's correct the next
6661 time we expand this function. */
6662 TREE_VALUE (tail) = o[i];
6664 /* Detect modification of read-only values.
6665 (Otherwise done by build_modify_expr.) */
6666 else
6668 tree type = TREE_TYPE (o[i]);
6669 if (CP_TYPE_CONST_P (type)
6670 || (IS_AGGR_TYPE_CODE (TREE_CODE (type))
6671 && C_TYPE_FIELDS_READONLY (type)))
6672 readonly_error (o[i], "modification by `asm'", 1);
6676 /* Those MODIFY_EXPRs could do autoincrements. */
6677 emit_queue ();
6680 /* If RETVAL is the address of, or a reference to, a local variable or
6681 temporary give an appropraite warning. */
6683 static void
6684 maybe_warn_about_returning_address_of_local (retval)
6685 tree retval;
6687 tree valtype = TREE_TYPE (DECL_RESULT (current_function_decl));
6688 tree whats_returned = retval;
6690 for (;;)
6692 if (TREE_CODE (whats_returned) == COMPOUND_EXPR)
6693 whats_returned = TREE_OPERAND (whats_returned, 1);
6694 else if (TREE_CODE (whats_returned) == CONVERT_EXPR
6695 || TREE_CODE (whats_returned) == NON_LVALUE_EXPR
6696 || TREE_CODE (whats_returned) == NOP_EXPR)
6697 whats_returned = TREE_OPERAND (whats_returned, 0);
6698 else
6699 break;
6702 if (TREE_CODE (whats_returned) != ADDR_EXPR)
6703 return;
6704 whats_returned = TREE_OPERAND (whats_returned, 0);
6706 if (TREE_CODE (valtype) == REFERENCE_TYPE)
6708 if (TREE_CODE (whats_returned) == AGGR_INIT_EXPR
6709 || TREE_CODE (whats_returned) == TARGET_EXPR)
6711 /* Get the target. */
6712 whats_returned = TREE_OPERAND (whats_returned, 0);
6713 warning ("returning reference to temporary");
6714 return;
6716 if (TREE_CODE (whats_returned) == VAR_DECL
6717 && DECL_NAME (whats_returned)
6718 && TEMP_NAME_P (DECL_NAME (whats_returned)))
6720 warning ("reference to non-lvalue returned");
6721 return;
6725 if (TREE_CODE (whats_returned) == VAR_DECL
6726 && DECL_NAME (whats_returned)
6727 && DECL_FUNCTION_SCOPE_P (whats_returned)
6728 && !(TREE_STATIC (whats_returned)
6729 || TREE_PUBLIC (whats_returned)))
6731 if (TREE_CODE (valtype) == REFERENCE_TYPE)
6732 cp_warning_at ("reference to local variable `%D' returned",
6733 whats_returned);
6734 else
6735 cp_warning_at ("address of local variable `%D' returned",
6736 whats_returned);
6737 return;
6741 /* Check that returning RETVAL from the current function is legal.
6742 Return an expression explicitly showing all conversions required to
6743 change RETVAL into the function return type, and to assign it to
6744 the DECL_RESULT for the function. */
6746 tree
6747 check_return_expr (retval)
6748 tree retval;
6750 tree result;
6751 /* The type actually returned by the function, after any
6752 promotions. */
6753 tree valtype;
6754 int fn_returns_value_p;
6756 /* A `volatile' function is one that isn't supposed to return, ever.
6757 (This is a G++ extension, used to get better code for functions
6758 that call the `volatile' function.) */
6759 if (TREE_THIS_VOLATILE (current_function_decl))
6760 warning ("function declared `noreturn' has a `return' statement");
6762 /* Check for various simple errors. */
6763 if (dtor_label)
6765 if (retval)
6766 error ("returning a value from a destructor");
6767 return NULL_TREE;
6769 else if (DECL_CONSTRUCTOR_P (current_function_decl))
6771 if (in_function_try_handler)
6772 /* If a return statement appears in a handler of the
6773 function-try-block of a constructor, the program is ill-formed. */
6774 error ("cannot return from a handler of a function-try-block of a constructor");
6775 else if (retval)
6776 /* You can't return a value from a constructor. */
6777 error ("returning a value from a constructor");
6778 return NULL_TREE;
6781 /* Under the old ABI, constructors actually always return `this',
6782 even though in C++ you can't return a value from a constructor. */
6783 if (!flag_new_abi && DECL_CONSTRUCTOR_P (current_function_decl))
6784 retval = current_class_ptr;
6786 /* When no explicit return-value is given in a function with a named
6787 return value, the named return value is used. */
6788 result = DECL_RESULT (current_function_decl);
6789 valtype = TREE_TYPE (result);
6790 my_friendly_assert (valtype != NULL_TREE, 19990924);
6791 fn_returns_value_p = !VOID_TYPE_P (valtype);
6792 if (!retval && DECL_NAME (result) && fn_returns_value_p)
6793 retval = result;
6795 /* Check for a return statement with no return value in a function
6796 that's supposed to return a value. */
6797 if (!retval && fn_returns_value_p)
6799 pedwarn ("return-statement with no value, in function declared with a non-void return type");
6800 /* Clear this, so finish_function won't say that we reach the
6801 end of a non-void function (which we don't, we gave a
6802 return!). */
6803 current_function_returns_null = 0;
6805 /* Check for a return statement with a value in a function that
6806 isn't supposed to return a value. */
6807 else if (retval && !fn_returns_value_p)
6809 if (VOID_TYPE_P (TREE_TYPE (retval)))
6810 /* You can return a `void' value from a function of `void'
6811 type. In that case, we have to evaluate the expression for
6812 its side-effects. */
6813 finish_expr_stmt (retval);
6814 else
6815 pedwarn ("return-statement with a value, in function declared with a void return type");
6817 current_function_returns_null = 1;
6819 /* There's really no value to return, after all. */
6820 return NULL_TREE;
6822 else if (!retval)
6823 /* Remember that this function can sometimes return without a
6824 value. */
6825 current_function_returns_null = 1;
6826 else
6827 /* Remember that this function did return a value. */
6828 current_function_returns_value = 1;
6830 /* Only operator new(...) throw(), can return NULL [expr.new/13]. */
6831 if ((DECL_OVERLOADED_OPERATOR_P (current_function_decl) == NEW_EXPR
6832 || DECL_OVERLOADED_OPERATOR_P (current_function_decl) == VEC_NEW_EXPR)
6833 && !TYPE_NOTHROW_P (TREE_TYPE (current_function_decl))
6834 && ! flag_check_new
6835 && null_ptr_cst_p (retval))
6836 cp_warning ("`operator new' must not return NULL unless it is declared `throw()' (or -fcheck-new is in effect)");
6838 /* Effective C++ rule 15. See also start_function. */
6839 if (warn_ecpp
6840 && DECL_NAME (current_function_decl) == ansi_assopname(NOP_EXPR)
6841 && retval != current_class_ref)
6842 cp_warning ("`operator=' should return a reference to `*this'");
6844 /* We don't need to do any conversions when there's nothing being
6845 returned. */
6846 if (!retval || retval == error_mark_node)
6847 return retval;
6849 /* Do any required conversions. */
6850 if (retval == result || DECL_CONSTRUCTOR_P (current_function_decl))
6851 /* No conversions are required. */
6853 else
6855 /* The type the function is declared to return. */
6856 tree functype = TREE_TYPE (TREE_TYPE (current_function_decl));
6858 /* First convert the value to the function's return type, then
6859 to the type of return value's location to handle the
6860 case that functype is smaller than the valtype. */
6861 retval = convert_for_initialization
6862 (NULL_TREE, functype, retval, LOOKUP_NORMAL|LOOKUP_ONLYCONVERTING,
6863 "return", NULL_TREE, 0);
6864 retval = convert (valtype, retval);
6866 /* If the conversion failed, treat this just like `return;'. */
6867 if (retval == error_mark_node)
6868 return retval;
6869 /* We can't initialize a register from a AGGR_INIT_EXPR. */
6870 else if (! current_function_returns_struct
6871 && TREE_CODE (retval) == TARGET_EXPR
6872 && TREE_CODE (TREE_OPERAND (retval, 1)) == AGGR_INIT_EXPR)
6873 retval = build (COMPOUND_EXPR, TREE_TYPE (retval), retval,
6874 TREE_OPERAND (retval, 0));
6875 else
6876 maybe_warn_about_returning_address_of_local (retval);
6879 /* Actually copy the value returned into the appropriate location. */
6880 if (retval && retval != result)
6881 retval = build (INIT_EXPR, TREE_TYPE (result), result, retval);
6883 return retval;
6887 /* Returns non-zero if the pointer-type FROM can be converted to the
6888 pointer-type TO via a qualification conversion. If CONSTP is -1,
6889 then we return non-zero if the pointers are similar, and the
6890 cv-qualification signature of FROM is a proper subset of that of TO.
6892 If CONSTP is positive, then all outer pointers have been
6893 const-qualified. */
6895 static int
6896 comp_ptr_ttypes_real (to, from, constp)
6897 tree to, from;
6898 int constp;
6900 int to_more_cv_qualified = 0;
6902 for (; ; to = TREE_TYPE (to), from = TREE_TYPE (from))
6904 if (TREE_CODE (to) != TREE_CODE (from))
6905 return 0;
6907 if (TREE_CODE (from) == OFFSET_TYPE
6908 && same_type_p (TYPE_OFFSET_BASETYPE (from),
6909 TYPE_OFFSET_BASETYPE (to)))
6910 continue;
6912 /* Const and volatile mean something different for function types,
6913 so the usual checks are not appropriate. */
6914 if (TREE_CODE (to) != FUNCTION_TYPE && TREE_CODE (to) != METHOD_TYPE)
6916 if (!at_least_as_qualified_p (to, from))
6917 return 0;
6919 if (!at_least_as_qualified_p (from, to))
6921 if (constp == 0)
6922 return 0;
6923 else
6924 ++to_more_cv_qualified;
6927 if (constp > 0)
6928 constp &= TYPE_READONLY (to);
6931 if (TREE_CODE (to) != POINTER_TYPE)
6932 return
6933 same_type_ignoring_top_level_qualifiers_p (to, from)
6934 && (constp >= 0 || to_more_cv_qualified);
6938 /* When comparing, say, char ** to char const **, this function takes the
6939 'char *' and 'char const *'. Do not pass non-pointer types to this
6940 function. */
6943 comp_ptr_ttypes (to, from)
6944 tree to, from;
6946 return comp_ptr_ttypes_real (to, from, 1);
6949 /* Returns 1 if to and from are (possibly multi-level) pointers to the same
6950 type or inheritance-related types, regardless of cv-quals. */
6953 ptr_reasonably_similar (to, from)
6954 tree to, from;
6956 for (; ; to = TREE_TYPE (to), from = TREE_TYPE (from))
6958 if (TREE_CODE (to) != TREE_CODE (from))
6959 return 0;
6961 if (TREE_CODE (from) == OFFSET_TYPE
6962 && comptypes (TYPE_OFFSET_BASETYPE (to),
6963 TYPE_OFFSET_BASETYPE (from),
6964 COMPARE_BASE | COMPARE_RELAXED))
6965 continue;
6967 if (TREE_CODE (to) != POINTER_TYPE)
6968 return comptypes
6969 (TYPE_MAIN_VARIANT (to), TYPE_MAIN_VARIANT (from),
6970 COMPARE_BASE | COMPARE_RELAXED);
6974 /* Like comp_ptr_ttypes, for const_cast. */
6976 static int
6977 comp_ptr_ttypes_const (to, from)
6978 tree to, from;
6980 for (; ; to = TREE_TYPE (to), from = TREE_TYPE (from))
6982 if (TREE_CODE (to) != TREE_CODE (from))
6983 return 0;
6985 if (TREE_CODE (from) == OFFSET_TYPE
6986 && same_type_p (TYPE_OFFSET_BASETYPE (from),
6987 TYPE_OFFSET_BASETYPE (to)))
6988 continue;
6990 if (TREE_CODE (to) != POINTER_TYPE)
6991 return same_type_ignoring_top_level_qualifiers_p (to, from);
6995 /* Like comp_ptr_ttypes, for reinterpret_cast. */
6997 static int
6998 comp_ptr_ttypes_reinterpret (to, from)
6999 tree to, from;
7001 int constp = 1;
7003 for (; ; to = TREE_TYPE (to), from = TREE_TYPE (from))
7005 if (TREE_CODE (from) == OFFSET_TYPE)
7006 from = TREE_TYPE (from);
7007 if (TREE_CODE (to) == OFFSET_TYPE)
7008 to = TREE_TYPE (to);
7010 /* Const and volatile mean something different for function types,
7011 so the usual checks are not appropriate. */
7012 if (TREE_CODE (from) != FUNCTION_TYPE && TREE_CODE (from) != METHOD_TYPE
7013 && TREE_CODE (to) != FUNCTION_TYPE && TREE_CODE (to) != METHOD_TYPE)
7015 if (!at_least_as_qualified_p (to, from))
7016 return 0;
7018 if (! constp
7019 && !at_least_as_qualified_p (from, to))
7020 return 0;
7021 constp &= TYPE_READONLY (to);
7024 if (TREE_CODE (from) != POINTER_TYPE
7025 || TREE_CODE (to) != POINTER_TYPE)
7026 return 1;
7030 /* Returns the type-qualifier set corresponding to TYPE. */
7033 cp_type_quals (type)
7034 tree type;
7036 type = strip_array_types (type);
7037 return TYPE_QUALS (type);
7040 /* Returns non-zero if the TYPE contains a mutable member */
7043 cp_has_mutable_p (type)
7044 tree type;
7046 type = strip_array_types (type);
7048 return CLASS_TYPE_P (type) && CLASSTYPE_HAS_MUTABLE (type);
7051 /* Subroutine of casts_away_constness. Make T1 and T2 point at
7052 exemplar types such that casting T1 to T2 is casting away castness
7053 if and only if there is no implicit conversion from T1 to T2. */
7055 static void
7056 casts_away_constness_r (t1, t2)
7057 tree *t1;
7058 tree *t2;
7060 int quals1;
7061 int quals2;
7063 /* [expr.const.cast]
7065 For multi-level pointer to members and multi-level mixed pointers
7066 and pointers to members (conv.qual), the "member" aspect of a
7067 pointer to member level is ignored when determining if a const
7068 cv-qualifier has been cast away. */
7069 if (TYPE_PTRMEM_P (*t1))
7070 *t1 = build_pointer_type (TREE_TYPE (TREE_TYPE (*t1)));
7071 if (TYPE_PTRMEM_P (*t2))
7072 *t2 = build_pointer_type (TREE_TYPE (TREE_TYPE (*t2)));
7074 /* [expr.const.cast]
7076 For two pointer types:
7078 X1 is T1cv1,1 * ... cv1,N * where T1 is not a pointer type
7079 X2 is T2cv2,1 * ... cv2,M * where T2 is not a pointer type
7080 K is min(N,M)
7082 casting from X1 to X2 casts away constness if, for a non-pointer
7083 type T there does not exist an implicit conversion (clause
7084 _conv_) from:
7086 Tcv1,(N-K+1) * cv1,(N-K+2) * ... cv1,N *
7090 Tcv2,(M-K+1) * cv2,(M-K+2) * ... cv2,M *. */
7092 if (TREE_CODE (*t1) != POINTER_TYPE
7093 || TREE_CODE (*t2) != POINTER_TYPE)
7095 *t1 = cp_build_qualified_type (void_type_node,
7096 CP_TYPE_QUALS (*t1));
7097 *t2 = cp_build_qualified_type (void_type_node,
7098 CP_TYPE_QUALS (*t2));
7099 return;
7102 quals1 = CP_TYPE_QUALS (*t1);
7103 quals2 = CP_TYPE_QUALS (*t2);
7104 *t1 = TREE_TYPE (*t1);
7105 *t2 = TREE_TYPE (*t2);
7106 casts_away_constness_r (t1, t2);
7107 *t1 = build_pointer_type (*t1);
7108 *t2 = build_pointer_type (*t2);
7109 *t1 = cp_build_qualified_type (*t1, quals1);
7110 *t2 = cp_build_qualified_type (*t2, quals2);
7113 /* Returns non-zero if casting from TYPE1 to TYPE2 casts away
7114 constness. */
7116 static int
7117 casts_away_constness (t1, t2)
7118 tree t1;
7119 tree t2;
7121 if (TREE_CODE (t2) == REFERENCE_TYPE)
7123 /* [expr.const.cast]
7125 Casting from an lvalue of type T1 to an lvalue of type T2
7126 using a reference cast casts away constness if a cast from an
7127 rvalue of type "pointer to T1" to the type "pointer to T2"
7128 casts away constness. */
7129 t1 = (TREE_CODE (t1) == REFERENCE_TYPE
7130 ? TREE_TYPE (t1) : t1);
7131 return casts_away_constness (build_pointer_type (t1),
7132 build_pointer_type (TREE_TYPE (t2)));
7135 if (TYPE_PTRMEM_P (t1) && TYPE_PTRMEM_P (t2))
7136 /* [expr.const.cast]
7138 Casting from an rvalue of type "pointer to data member of X
7139 of type T1" to the type "pointer to data member of Y of type
7140 T2" casts away constness if a cast from an rvalue of type
7141 "poitner to T1" to the type "pointer to T2" casts away
7142 constness. */
7143 return casts_away_constness
7144 (build_pointer_type (TREE_TYPE (TREE_TYPE (t1))),
7145 build_pointer_type (TREE_TYPE (TREE_TYPE (t2))));
7147 /* Casting away constness is only something that makes sense for
7148 pointer or reference types. */
7149 if (TREE_CODE (t1) != POINTER_TYPE
7150 || TREE_CODE (t2) != POINTER_TYPE)
7151 return 0;
7153 /* Top-level qualifiers don't matter. */
7154 t1 = TYPE_MAIN_VARIANT (t1);
7155 t2 = TYPE_MAIN_VARIANT (t2);
7156 casts_away_constness_r (&t1, &t2);
7157 if (!can_convert (t2, t1))
7158 return 1;
7160 return 0;
7163 /* Returns TYPE with its cv qualifiers removed
7164 TYPE is T cv* .. *cv where T is not a pointer type,
7165 returns T * .. *. (If T is an array type, then the cv qualifiers
7166 above are those of the array members.) */
7168 static tree
7169 strip_all_pointer_quals (type)
7170 tree type;
7172 if (TREE_CODE (type) == POINTER_TYPE)
7173 return build_pointer_type (strip_all_pointer_quals (TREE_TYPE (type)));
7174 else if (TREE_CODE (type) == OFFSET_TYPE)
7175 return build_offset_type (TYPE_OFFSET_BASETYPE (type),
7176 strip_all_pointer_quals (TREE_TYPE (type)));
7177 else
7178 return TYPE_MAIN_VARIANT (type);