1 /* Build expressions with type checking for C++ compiler.
2 Copyright (C) 1987, 88, 89, 92, 93, 94, 1995 Free Software Foundation, Inc.
3 Hacked by Michael Tiemann (tiemann@cygnus.com)
5 This file is part of GNU CC.
7 GNU CC is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2, or (at your option)
12 GNU CC is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with GNU CC; see the file COPYING. If not, write to
19 the Free Software Foundation, 59 Temple Place - Suite 330,
20 Boston, MA 02111-1307, USA. */
23 /* This file is part of the C++ front end.
24 It contains routines to build C++ expressions given their operands,
25 including computing the types of the result, C and C++ specific error
26 checks, and some optimization.
28 There are also routines to build RETURN_STMT nodes and CASE_STMT nodes,
29 and to process initializations in declarations (since they work
30 like a strange sort of assignment). */
33 extern void warning ();
43 int mark_addressable ();
44 static tree convert_for_assignment ();
45 /* static */ tree convert_for_initialization ();
46 extern tree shorten_compare ();
47 extern void binary_op_error ();
48 static tree pointer_int_sum ();
49 static tree pointer_diff ();
50 static tree convert_sequence ();
51 /* static */ tree unary_complex_lvalue ();
52 static tree get_delta_difference PROTO((tree, tree, int));
54 extern rtx original_result_rtx;
55 extern int warn_synth;
57 /* Return the target type of TYPE, which meas return T for:
58 T*, T&, T[], T (...), and otherwise, just T. */
64 if (TREE_CODE (type) == REFERENCE_TYPE)
65 type = TREE_TYPE (type);
66 while (TREE_CODE (type) == POINTER_TYPE
67 || TREE_CODE (type) == ARRAY_TYPE
68 || TREE_CODE (type) == FUNCTION_TYPE
69 || TREE_CODE (type) == METHOD_TYPE
70 || TREE_CODE (type) == OFFSET_TYPE)
71 type = TREE_TYPE (type);
75 /* Do `exp = require_complete_type (exp);' to make sure exp
76 does not have an incomplete type. (That includes void types.) */
79 require_complete_type (value)
82 tree type = TREE_TYPE (value);
84 /* First, detect a valid value with a complete type. */
85 if (TYPE_SIZE (type) != 0
86 && type != void_type_node
87 && ! (TYPE_LANG_SPECIFIC (type)
88 && (IS_SIGNATURE_POINTER (type) || IS_SIGNATURE_REFERENCE (type))
89 && TYPE_SIZE (SIGNATURE_TYPE (type)) == 0))
92 /* If we see X::Y, we build an OFFSET_TYPE which has
93 not been laid out. Try to avoid an error by interpreting
94 it as this->X::Y, if reasonable. */
95 if (TREE_CODE (value) == OFFSET_REF
97 && TREE_OPERAND (value, 0) == C_C_D)
99 tree base, member = TREE_OPERAND (value, 1);
100 tree basetype = TYPE_OFFSET_BASETYPE (type);
101 my_friendly_assert (TREE_CODE (member) == FIELD_DECL, 305);
102 base = convert_pointer_to (basetype, current_class_decl);
103 value = build (COMPONENT_REF, TREE_TYPE (member),
104 build_indirect_ref (base, NULL_PTR), member);
105 return require_complete_type (value);
108 incomplete_type_error (value, type);
109 return error_mark_node;
112 /* Return truthvalue of whether type of EXP is instantiated. */
117 return (TREE_CODE (exp) == TREE_LIST
118 || TREE_TYPE (exp) == unknown_type_node
119 || (TREE_CODE (TREE_TYPE (exp)) == OFFSET_TYPE
120 && TREE_TYPE (TREE_TYPE (exp)) == unknown_type_node));
123 /* Return truthvalue of whether T is function (or pfn) type. */
128 return (TREE_CODE (t) == FUNCTION_TYPE || TREE_CODE (t) == METHOD_TYPE
129 || (TREE_CODE (t) == POINTER_TYPE
130 && (TREE_CODE (TREE_TYPE (t)) == FUNCTION_TYPE
131 || TREE_CODE (TREE_TYPE (t)) == METHOD_TYPE)));
134 /* Do `exp = require_instantiated_type (type, exp);' to make sure EXP
135 does not have an uninstantiated type.
136 TYPE is type to instantiate with, if uninstantiated. */
138 require_instantiated_type (type, exp, errval)
139 tree type, exp, errval;
141 if (TREE_TYPE (exp) == NULL_TREE)
143 error ("argument list may not have an initializer list");
147 if (TREE_TYPE (exp) == unknown_type_node
148 || (TREE_CODE (TREE_TYPE (exp)) == OFFSET_TYPE
149 && TREE_TYPE (TREE_TYPE (exp)) == unknown_type_node))
151 exp = instantiate_type (type, exp, 1);
152 if (TREE_TYPE (exp) == error_mark_node)
158 /* Return a variant of TYPE which has all the type qualifiers of LIKE
159 as well as those of TYPE. */
162 qualify_type (type, like)
165 int constflag = TYPE_READONLY (type) || TYPE_READONLY (like);
166 int volflag = TYPE_VOLATILE (type) || TYPE_VOLATILE (like);
167 /* @@ Must do member pointers here. */
168 return cp_build_type_variant (type, constflag, volflag);
171 /* Return the common type of two parameter lists.
172 We assume that comptypes has already been done and returned 1;
173 if that isn't so, this may crash.
175 As an optimization, free the space we allocate if the parameter
176 lists are already common. */
182 tree oldargs = p1, newargs, n;
185 char *first_obj = (char *) oballoc (0);
187 len = list_length (p1);
188 newargs = tree_last (p1);
190 if (newargs == void_list_node)
199 newargs = tree_cons (NULL_TREE, NULL_TREE, newargs);
204 p1 = TREE_CHAIN (p1), p2 = TREE_CHAIN (p2), n = TREE_CHAIN (n), i++)
206 if (TREE_PURPOSE (p1) && !TREE_PURPOSE (p2))
208 TREE_PURPOSE (n) = TREE_PURPOSE (p1);
211 else if (! TREE_PURPOSE (p1))
213 if (TREE_PURPOSE (p2))
215 TREE_PURPOSE (n) = TREE_PURPOSE (p2);
221 if (1 != simple_cst_equal (TREE_PURPOSE (p1), TREE_PURPOSE (p2)))
223 TREE_PURPOSE (n) = TREE_PURPOSE (p2);
225 if (TREE_VALUE (p1) != TREE_VALUE (p2))
228 TREE_VALUE (n) = common_type (TREE_VALUE (p1), TREE_VALUE (p2));
231 TREE_VALUE (n) = TREE_VALUE (p1);
242 /* Return the common type of two types.
243 We assume that comptypes has already been done and returned 1;
244 if that isn't so, this may crash.
246 This is the type for the result of most arithmetic operations
247 if the operands have the given two types.
249 We do not deal with enumeral types here because they have already been
250 converted to integer types. */
256 register enum tree_code code1;
257 register enum tree_code code2;
260 /* Save time if the two types are the same. */
262 if (t1 == t2) return t1;
264 /* If one type is nonsense, use the other. */
265 if (t1 == error_mark_node)
267 if (t2 == error_mark_node)
270 /* Merge the attributes */
272 { register tree a1, a2;
273 a1 = TYPE_ATTRIBUTES (t1);
274 a2 = TYPE_ATTRIBUTES (t2);
276 /* Either one unset? Take the set one. */
278 if (!(attributes = a1))
281 /* One that completely contains the other? Take it. */
283 else if (a2 && !attribute_list_contained (a1, a2))
284 if (attribute_list_contained (a2, a1))
288 /* Pick the longest list, and hang on the other list. */
289 /* ??? For the moment we punt on the issue of attrs with args. */
291 if (list_length (a1) < list_length (a2))
292 attributes = a2, a2 = a1;
294 for (; a2; a2 = TREE_CHAIN (a2))
295 if (lookup_attribute (IDENTIFIER_POINTER (TREE_PURPOSE (a2)),
296 attributes) == NULL_TREE)
299 TREE_CHAIN (a1) = attributes;
305 /* Treat an enum type as the unsigned integer type of the same width. */
307 if (TREE_CODE (t1) == ENUMERAL_TYPE)
308 t1 = type_for_size (TYPE_PRECISION (t1), 1);
309 if (TREE_CODE (t2) == ENUMERAL_TYPE)
310 t2 = type_for_size (TYPE_PRECISION (t2), 1);
312 code1 = TREE_CODE (t1);
313 code2 = TREE_CODE (t2);
319 /* If only one is real, use it as the result. */
321 if (code1 == REAL_TYPE && code2 != REAL_TYPE)
322 return build_type_attribute_variant (t1, attributes);
324 if (code2 == REAL_TYPE && code1 != REAL_TYPE)
325 return build_type_attribute_variant (t2, attributes);
327 /* Both real or both integers; use the one with greater precision. */
329 if (TYPE_PRECISION (t1) > TYPE_PRECISION (t2))
330 return build_type_attribute_variant (t1, attributes);
331 else if (TYPE_PRECISION (t2) > TYPE_PRECISION (t1))
332 return build_type_attribute_variant (t2, attributes);
334 /* Same precision. Prefer longs to ints even when same size. */
336 if (TYPE_MAIN_VARIANT (t1) == long_unsigned_type_node
337 || TYPE_MAIN_VARIANT (t2) == long_unsigned_type_node)
338 return build_type_attribute_variant (long_unsigned_type_node,
341 if (TYPE_MAIN_VARIANT (t1) == long_integer_type_node
342 || TYPE_MAIN_VARIANT (t2) == long_integer_type_node)
344 /* But preserve unsignedness from the other type,
345 since long cannot hold all the values of an unsigned int. */
346 if (TREE_UNSIGNED (t1) || TREE_UNSIGNED (t2))
347 t1 = long_unsigned_type_node;
349 t1 = long_integer_type_node;
350 return build_type_attribute_variant (t1, attributes);
353 if (TYPE_MAIN_VARIANT (t1) == long_double_type_node
354 || TYPE_MAIN_VARIANT (t2) == long_double_type_node)
355 return build_type_attribute_variant (long_double_type_node,
358 /* Otherwise prefer the unsigned one. */
360 if (TREE_UNSIGNED (t1))
361 return build_type_attribute_variant (t1, attributes);
363 return build_type_attribute_variant (t2, attributes);
367 /* For two pointers, do this recursively on the target type,
368 and combine the qualifiers of the two types' targets. */
369 /* This code was turned off; I don't know why.
370 But ANSI C++ specifies doing this with the qualifiers.
371 So I turned it on again. */
373 tree tt1 = TYPE_MAIN_VARIANT (TREE_TYPE (t1));
374 tree tt2 = TYPE_MAIN_VARIANT (TREE_TYPE (t2));
376 = TYPE_READONLY (TREE_TYPE (t1)) || TYPE_READONLY (TREE_TYPE (t2));
378 = TYPE_VOLATILE (TREE_TYPE (t1)) || TYPE_VOLATILE (TREE_TYPE (t2));
383 else if (tt1 == void_type_node || tt2 == void_type_node)
384 target = void_type_node;
386 target = common_type (tt1, tt2);
388 target = cp_build_type_variant (target, constp, volatilep);
389 if (code1 == POINTER_TYPE)
390 t1 = build_pointer_type (target);
392 t1 = build_reference_type (target);
393 t1 = build_type_attribute_variant (t1, attributes);
395 if (TREE_CODE (target) == METHOD_TYPE)
396 t1 = build_ptrmemfunc_type (t1);
403 tree elt = common_type (TREE_TYPE (t1), TREE_TYPE (t2));
404 /* Save space: see if the result is identical to one of the args. */
405 if (elt == TREE_TYPE (t1) && TYPE_DOMAIN (t1))
406 return build_type_attribute_variant (t1, attributes);
407 if (elt == TREE_TYPE (t2) && TYPE_DOMAIN (t2))
408 return build_type_attribute_variant (t2, attributes);
409 /* Merge the element types, and have a size if either arg has one. */
410 t1 = build_cplus_array_type (elt, TYPE_DOMAIN (TYPE_DOMAIN (t1) ? t1 : t2));
411 return build_type_attribute_variant (t1, attributes);
415 /* Function types: prefer the one that specified arg types.
416 If both do, merge the arg types. Also merge the return types. */
418 tree valtype = common_type (TREE_TYPE (t1), TREE_TYPE (t2));
419 tree p1 = TYPE_ARG_TYPES (t1);
420 tree p2 = TYPE_ARG_TYPES (t2);
423 /* Save space: see if the result is identical to one of the args. */
424 if (valtype == TREE_TYPE (t1) && ! p2)
425 return build_type_attribute_variant (t1, attributes);
426 if (valtype == TREE_TYPE (t2) && ! p1)
427 return build_type_attribute_variant (t2, attributes);
429 /* Simple way if one arg fails to specify argument types. */
430 if (p1 == NULL_TREE || TREE_VALUE (p1) == void_type_node)
432 rval = build_function_type (valtype, p2);
433 if ((raises = TYPE_RAISES_EXCEPTIONS (t2)))
434 rval = build_exception_variant (rval, raises);
435 return build_type_attribute_variant (rval, attributes);
437 raises = TYPE_RAISES_EXCEPTIONS (t1);
438 if (p2 == NULL_TREE || TREE_VALUE (p2) == void_type_node)
440 rval = build_function_type (valtype, p1);
442 rval = build_exception_variant (rval, raises);
443 return build_type_attribute_variant (rval, attributes);
446 rval = build_function_type (valtype, commonparms (p1, p2));
447 rval = build_exception_variant (rval, raises);
448 return build_type_attribute_variant (rval, attributes);
453 my_friendly_assert (TYPE_MAIN_VARIANT (t1) == t1
454 && TYPE_MAIN_VARIANT (t2) == t2, 306);
456 if (DERIVED_FROM_P (t1, t2) && binfo_or_else (t1, t2))
457 return build_type_attribute_variant (t1, attributes);
458 else if (binfo_or_else (t2, t1))
459 return build_type_attribute_variant (t2, attributes);
461 compiler_error ("common_type called with uncommon aggregate types");
464 if (TREE_CODE (TREE_TYPE (t1)) == TREE_CODE (TREE_TYPE (t2)))
466 /* Get this value the long way, since TYPE_METHOD_BASETYPE
467 is just the main variant of this. */
471 tree b1 = TYPE_OFFSET_BASETYPE (t1);
472 tree b2 = TYPE_OFFSET_BASETYPE (t2);
474 if (DERIVED_FROM_P (b1, b2) && binfo_or_else (b1, b2))
475 basetype = TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (t2)));
478 if (binfo_or_else (b2, b1) == NULL_TREE)
479 compiler_error ("common_type called with uncommon method types");
480 basetype = TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (t1)));
483 raises = TYPE_RAISES_EXCEPTIONS (t1);
485 /* If this was a member function type, get back to the
486 original type of type member function (i.e., without
487 the class instance variable up front. */
488 t1 = build_function_type (TREE_TYPE (t1), TREE_CHAIN (TYPE_ARG_TYPES (t1)));
489 t2 = build_function_type (TREE_TYPE (t2), TREE_CHAIN (TYPE_ARG_TYPES (t2)));
490 t3 = common_type (t1, t2);
491 t3 = build_cplus_method_type (basetype, TREE_TYPE (t3), TYPE_ARG_TYPES (t3));
492 t1 = build_exception_variant (t3, raises);
495 compiler_error ("common_type called with uncommon method types");
497 return build_type_attribute_variant (t1, attributes);
500 if (TREE_TYPE (t1) == TREE_TYPE (t2))
502 tree b1 = TYPE_OFFSET_BASETYPE (t1);
503 tree b2 = TYPE_OFFSET_BASETYPE (t2);
505 if (DERIVED_FROM_P (b1, b2) && binfo_or_else (b1, b2))
506 return build_type_attribute_variant (t2, attributes);
507 else if (binfo_or_else (b2, b1))
508 return build_type_attribute_variant (t1, attributes);
510 compiler_error ("common_type called with uncommon member types");
513 return build_type_attribute_variant (t1, attributes);
517 /* Return 1 if TYPE1 and TYPE2 raise the same exceptions. */
519 compexcepttypes (t1, t2, strict)
523 return TYPE_RAISES_EXCEPTIONS (t1) == TYPE_RAISES_EXCEPTIONS (t2);
527 comp_array_types (cmp, t1, t2, strict)
528 register int (*cmp)();
532 tree d1 = TYPE_DOMAIN (t1);
533 tree d2 = TYPE_DOMAIN (t2);
535 /* Target types must match incl. qualifiers. */
536 if (!(TREE_TYPE (t1) == TREE_TYPE (t2)
537 || (*cmp) (TREE_TYPE (t1), TREE_TYPE (t2), strict)))
540 /* Sizes must match unless one is missing or variable. */
541 if (d1 == 0 || d2 == 0 || d1 == d2
542 || TREE_CODE (TYPE_MIN_VALUE (d1)) != INTEGER_CST
543 || TREE_CODE (TYPE_MIN_VALUE (d2)) != INTEGER_CST
544 || TREE_CODE (TYPE_MAX_VALUE (d1)) != INTEGER_CST
545 || TREE_CODE (TYPE_MAX_VALUE (d2)) != INTEGER_CST)
548 return ((TREE_INT_CST_LOW (TYPE_MIN_VALUE (d1))
549 == TREE_INT_CST_LOW (TYPE_MIN_VALUE (d2)))
550 && (TREE_INT_CST_HIGH (TYPE_MIN_VALUE (d1))
551 == TREE_INT_CST_HIGH (TYPE_MIN_VALUE (d2)))
552 && (TREE_INT_CST_LOW (TYPE_MAX_VALUE (d1))
553 == TREE_INT_CST_LOW (TYPE_MAX_VALUE (d2)))
554 && (TREE_INT_CST_HIGH (TYPE_MAX_VALUE (d1))
555 == TREE_INT_CST_HIGH (TYPE_MAX_VALUE (d2))));
558 /* Return 1 if TYPE1 and TYPE2 are compatible types for assignment
559 or various other operations. This is what ANSI C++ speaks of as
562 For C++: argument STRICT says we should be strict about this
565 2 : strict, except that if one type is a reference and
566 the other is not, compare the target type of the
567 reference to the type that's not a reference (ARM, p308).
568 This is used for checking for invalid overloading.
569 1 : strict (compared according to ANSI C)
570 This is used for checking whether two function decls match.
571 0 : <= (compared according to C++)
572 -1: <= or >= (relaxed)
574 Otherwise, pointers involving base classes and derived classes
575 can be mixed as valid: i.e. a pointer to a base class may be assigned
576 to a pointer to one of its derived classes, as per C++. A pointer to
577 a derived class may be passed as a parameter to a function expecting a
578 pointer to a base classes. These allowances do not commute. In this
579 case, TYPE1 is assumed to be the base class, and TYPE2 is assumed to
580 be the derived class. */
582 comptypes (type1, type2, strict)
586 register tree t1 = type1;
587 register tree t2 = type2;
590 /* Suppress errors caused by previously reported errors */
595 /* This should never happen. */
596 my_friendly_assert (t1 != error_mark_node, 307);
598 if (t2 == error_mark_node)
603 /* Treat an enum type as the unsigned integer type of the same width. */
605 if (TREE_CODE (t1) == ENUMERAL_TYPE)
606 t1 = type_for_size (TYPE_PRECISION (t1), 1);
607 if (TREE_CODE (t2) == ENUMERAL_TYPE)
608 t2 = type_for_size (TYPE_PRECISION (t2), 1);
614 /* Different classes of types can't be compatible. */
616 if (TREE_CODE (t1) != TREE_CODE (t2))
619 && ((TREE_CODE (t1) == REFERENCE_TYPE)
620 ^ (TREE_CODE (t2) == REFERENCE_TYPE)))
622 if (TREE_CODE (t1) == REFERENCE_TYPE)
623 return comptypes (TREE_TYPE (t1), t2, 1);
624 return comptypes (t1, TREE_TYPE (t2), 1);
632 /* Qualifiers must match. */
634 if (TYPE_READONLY (t1) != TYPE_READONLY (t2))
636 if (TYPE_VOLATILE (t1) != TYPE_VOLATILE (t2))
639 /* Allow for two different type nodes which have essentially the same
640 definition. Note that we already checked for equality of the type
641 type qualifiers (just above). */
643 if (TYPE_MAIN_VARIANT (t1) == TYPE_MAIN_VARIANT (t2))
646 #ifdef COMP_TYPE_ATTRIBUTES
647 if (! (attrval = COMP_TYPE_ATTRIBUTES (t1, t2)))
650 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
654 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
657 switch (TREE_CODE (t1))
666 val = (comptypes (build_pointer_type (TYPE_OFFSET_BASETYPE (t1)),
667 build_pointer_type (TYPE_OFFSET_BASETYPE (t2)), strict)
668 && comptypes (TREE_TYPE (t1), TREE_TYPE (t2), strict));
672 if (! compexcepttypes (t1, t2, strict))
675 /* This case is anti-symmetrical!
676 One can pass a base member (or member function)
677 to something expecting a derived member (or member function),
678 but not vice-versa! */
680 val = (comptypes (TREE_TYPE (t1), TREE_TYPE (t2), strict)
681 && compparms (TYPE_ARG_TYPES (t1),
682 TYPE_ARG_TYPES (t2), strict));
696 if (TREE_CODE (t1) == RECORD_TYPE && TREE_CODE (t2) == RECORD_TYPE)
700 rval = t1 == t2 || UNIQUELY_DERIVED_FROM_P (t1, t2);
709 val = UNIQUELY_DERIVED_FROM_P (t2, t1);
716 val = comptypes (t1, t2, strict);
720 if (! compexcepttypes (t1, t2, strict))
723 val = ((TREE_TYPE (t1) == TREE_TYPE (t2)
724 || comptypes (TREE_TYPE (t1), TREE_TYPE (t2), strict))
725 && compparms (TYPE_ARG_TYPES (t1), TYPE_ARG_TYPES (t2), strict));
729 /* Target types must match incl. qualifiers. */
730 val = comp_array_types (comptypes, t1, t2, strict);
733 case TEMPLATE_TYPE_PARM:
734 return TEMPLATE_TYPE_IDX (t1) == TEMPLATE_TYPE_IDX (t2);
736 case UNINSTANTIATED_P_TYPE:
737 if (UPT_TEMPLATE (t1) != UPT_TEMPLATE (t2))
740 int i = TREE_VEC_LENGTH (UPT_PARMS (t1));
741 tree *p1 = &TREE_VEC_ELT (UPT_PARMS (t1), 0);
742 tree *p2 = &TREE_VEC_ELT (UPT_PARMS (t2), 0);
746 if (TREE_CODE_CLASS (TREE_CODE (p1[i])) == 't')
748 if (! comptypes (p1[i], p2[i], 1))
753 if (simple_cst_equal (p1[i], p2[i]) <= 0)
760 return attrval == 2 && val == 1 ? 2 : val;
763 /* Return 1 if TTL and TTR are pointers to types that are equivalent,
764 ignoring their qualifiers.
766 NPTRS is the number of pointers we can strip off and keep cool.
767 This is used to permit (for aggr A, aggr B) A, B* to convert to A*,
768 but to not permit B** to convert to A**. */
771 comp_target_types (ttl, ttr, nptrs)
775 ttl = TYPE_MAIN_VARIANT (ttl);
776 ttr = TYPE_MAIN_VARIANT (ttr);
780 if (TREE_CODE (ttr) != TREE_CODE (ttl))
783 if (TREE_CODE (ttr) == POINTER_TYPE)
785 ttl = TREE_TYPE (ttl);
786 ttr = TREE_TYPE (ttr);
790 if (TREE_CODE (ttl) == VOID_TYPE
791 && TREE_CODE (ttr) != FUNCTION_TYPE
792 && TREE_CODE (ttr) != METHOD_TYPE
793 && TREE_CODE (ttr) != OFFSET_TYPE)
795 else if (TREE_CODE (ttr) == VOID_TYPE
796 && TREE_CODE (ttl) != FUNCTION_TYPE
797 && TREE_CODE (ttl) != METHOD_TYPE
798 && TREE_CODE (ttl) != OFFSET_TYPE)
800 else if (TREE_CODE (ttl) == POINTER_TYPE
801 || TREE_CODE (ttl) == ARRAY_TYPE)
803 if (comp_ptr_ttypes (ttl, ttr))
805 else if (comp_ptr_ttypes (ttr, ttl))
811 /* Const and volatile mean something different for function types,
812 so the usual checks are not appropriate. */
813 if (TREE_CODE (ttl) == FUNCTION_TYPE || TREE_CODE (ttl) == METHOD_TYPE)
814 return comp_target_types (ttl, ttr, nptrs - 1);
816 /* Make sure that the cv-quals change only in the same direction as
820 int c = TYPE_READONLY (ttl) - TYPE_READONLY (ttr);
821 int v = TYPE_VOLATILE (ttl) - TYPE_VOLATILE (ttr);
823 if ((c > 0 && v < 0) || (c < 0 && v > 0))
826 if (TYPE_MAIN_VARIANT (ttl) == TYPE_MAIN_VARIANT (ttr))
827 return (c + v < 0) ? -1 : 1;
829 t = comp_target_types (ttl, ttr, nptrs - 1);
830 if ((t == 1 && c + v >= 0) || (t == -1 && c + v <= 0))
837 if (TREE_CODE (ttr) == REFERENCE_TYPE)
838 return comp_target_types (TREE_TYPE (ttl), TREE_TYPE (ttr), nptrs);
839 if (TREE_CODE (ttr) == ARRAY_TYPE)
840 return comp_array_types (comp_target_types, ttl, ttr, 0);
841 else if (TREE_CODE (ttr) == FUNCTION_TYPE || TREE_CODE (ttr) == METHOD_TYPE)
842 if (comp_target_types (TREE_TYPE (ttl), TREE_TYPE (ttr), nptrs))
843 switch (comp_target_parms (TYPE_ARG_TYPES (ttl), TYPE_ARG_TYPES (ttr), 1))
852 my_friendly_abort (112);
858 else if (TREE_CODE (ttr) == OFFSET_TYPE)
860 /* Contravariance: we can assign a pointer to base member to a pointer
861 to derived member. Note difference from simple pointer case, where
862 we can pass a pointer to derived to a pointer to base. */
863 if (comptypes (TYPE_OFFSET_BASETYPE (ttr), TYPE_OFFSET_BASETYPE (ttl), 0))
864 return comp_target_types (TREE_TYPE (ttl), TREE_TYPE (ttr), nptrs);
865 else if (comptypes (TYPE_OFFSET_BASETYPE (ttl), TYPE_OFFSET_BASETYPE (ttr), 0)
866 && comp_target_types (TREE_TYPE (ttl), TREE_TYPE (ttr), nptrs))
869 else if (IS_AGGR_TYPE (ttl))
873 if (comptypes (build_pointer_type (ttl), build_pointer_type (ttr), 0))
875 if (comptypes (build_pointer_type (ttr), build_pointer_type (ttl), 0))
883 /* If two types share a common base type, return that basetype.
884 If there is not a unique most-derived base type, this function
885 returns ERROR_MARK_NODE. */
887 common_base_type (tt1, tt2)
890 tree best = NULL_TREE, tmp;
893 /* If one is a baseclass of another, that's good enough. */
894 if (UNIQUELY_DERIVED_FROM_P (tt1, tt2))
896 if (UNIQUELY_DERIVED_FROM_P (tt2, tt1))
899 /* Otherwise, try to find a unique baseclass of TT1
900 that is shared by TT2, and follow that down. */
901 for (i = CLASSTYPE_N_BASECLASSES (tt1)-1; i >= 0; i--)
903 tree basetype = TYPE_BINFO_BASETYPE (tt1, i);
904 tree trial = common_base_type (basetype, tt2);
907 if (trial == error_mark_node)
909 if (best == NULL_TREE)
911 else if (best != trial)
912 return error_mark_node;
917 for (i = CLASSTYPE_N_BASECLASSES (tt2)-1; i >= 0; i--)
919 tree basetype = TYPE_BINFO_BASETYPE (tt2, i);
920 tree trial = common_base_type (tt1, basetype);
923 if (trial == error_mark_node)
925 if (best == NULL_TREE)
927 else if (best != trial)
928 return error_mark_node;
934 /* Subroutines of `comptypes'. */
936 /* Return 1 if two parameter type lists PARMS1 and PARMS2
937 are equivalent in the sense that functions with those parameter types
938 can have equivalent types.
939 If either list is empty, we win.
940 Otherwise, the two lists must be equivalent, element by element.
942 C++: See comment above about TYPE1, TYPE2, STRICT.
943 If STRICT == 3, it means checking is strict, but do not compare
944 default parameter values. */
946 compparms (parms1, parms2, strict)
950 register tree t1 = parms1, t2 = parms2;
952 /* An unspecified parmlist matches any specified parmlist
953 whose argument types don't need default promotions. */
955 if (strict <= 0 && t1 == 0)
956 return self_promoting_args_p (t2);
957 if (strict < 0 && t2 == 0)
958 return self_promoting_args_p (t1);
962 if (t1 == 0 && t2 == 0)
964 /* If one parmlist is shorter than the other,
965 they fail to match, unless STRICT is <= 0. */
966 if (t1 == 0 || t2 == 0)
973 return t1 && TREE_PURPOSE (t1);
975 if (! comptypes (TREE_VALUE (t2), TREE_VALUE (t1), strict))
980 return t2 == void_list_node && TREE_PURPOSE (t1);
981 return TREE_PURPOSE (t1) || TREE_PURPOSE (t2);
984 t1 = TREE_CHAIN (t1);
985 t2 = TREE_CHAIN (t2);
989 /* This really wants return whether or not parameter type lists
990 would make their owning functions assignment compatible or not. */
992 comp_target_parms (parms1, parms2, strict)
996 register tree t1 = parms1, t2 = parms2;
997 int warn_contravariance = 0;
999 /* An unspecified parmlist matches any specified parmlist
1000 whose argument types don't need default promotions.
1001 @@@ see 13.3.3 for a counterexample... */
1003 if (t1 == 0 && t2 != 0)
1005 cp_pedwarn ("ANSI C++ prohibits conversion from `(%#T)' to `(...)'",
1007 return self_promoting_args_p (t2);
1010 return self_promoting_args_p (t1);
1012 for (; t1 || t2; t1 = TREE_CHAIN (t1), t2 = TREE_CHAIN (t2))
1016 /* If one parmlist is shorter than the other,
1017 they fail to match, unless STRICT is <= 0. */
1018 if (t1 == 0 || t2 == 0)
1023 return 1 + warn_contravariance;
1024 return ((t1 && TREE_PURPOSE (t1)) + warn_contravariance);
1026 p1 = TREE_VALUE (t1);
1027 p2 = TREE_VALUE (t2);
1031 if ((TREE_CODE (p1) == POINTER_TYPE && TREE_CODE (p2) == POINTER_TYPE)
1032 || (TREE_CODE (p1) == REFERENCE_TYPE && TREE_CODE (p2) == REFERENCE_TYPE))
1035 && (TYPE_MAIN_VARIANT (TREE_TYPE (p1))
1036 == TYPE_MAIN_VARIANT (TREE_TYPE (p2))))
1039 /* The following is wrong for contravariance,
1040 but many programs depend on it. */
1041 if (TREE_TYPE (p1) == void_type_node)
1043 if (TREE_TYPE (p2) == void_type_node)
1045 warn_contravariance = 1;
1048 if (IS_AGGR_TYPE (TREE_TYPE (p1)))
1050 if (comptypes (p2, p1, 0) == 0)
1052 if (comptypes (p1, p2, 0) != 0)
1053 warn_contravariance = 1;
1060 /* Note backwards order due to contravariance. */
1061 if (comp_target_types (p2, p1, 1) == 0)
1063 if (comp_target_types (p1, p2, 1))
1065 warn_contravariance = 1;
1071 /* Target types are compatible--just make sure that if
1072 we use parameter lists, that they are ok as well. */
1073 if (TREE_CODE (p1) == FUNCTION_TYPE || TREE_CODE (p1) == METHOD_TYPE)
1074 switch (comp_target_parms (TYPE_ARG_TYPES (p1),
1075 TYPE_ARG_TYPES (p2),
1083 warn_contravariance = 1;
1086 if (TREE_PURPOSE (t1) && TREE_PURPOSE (t2))
1088 int cmp = simple_cst_equal (TREE_PURPOSE (t1), TREE_PURPOSE (t2));
1090 my_friendly_abort (114);
1095 return 1 + warn_contravariance;
1098 /* Return 1 if PARMS specifies a fixed number of parameters
1099 and none of their types is affected by default promotions. */
1102 self_promoting_args_p (parms)
1106 for (t = parms; t; t = TREE_CHAIN (t))
1108 register tree type = TREE_VALUE (t);
1110 if (TREE_CHAIN (t) == 0 && type != void_type_node)
1113 if (TYPE_MAIN_VARIANT (type) == float_type_node)
1119 if (C_PROMOTING_INTEGER_TYPE_P (type))
1125 /* Return an unsigned type the same as TYPE in other respects.
1127 C++: must make these work for type variants as well. */
1130 unsigned_type (type)
1133 tree type1 = TYPE_MAIN_VARIANT (type);
1134 if (type1 == signed_char_type_node || type1 == char_type_node)
1135 return unsigned_char_type_node;
1136 if (type1 == integer_type_node)
1137 return unsigned_type_node;
1138 if (type1 == short_integer_type_node)
1139 return short_unsigned_type_node;
1140 if (type1 == long_integer_type_node)
1141 return long_unsigned_type_node;
1142 if (type1 == long_long_integer_type_node)
1143 return long_long_unsigned_type_node;
1147 /* Return a signed type the same as TYPE in other respects. */
1153 tree type1 = TYPE_MAIN_VARIANT (type);
1154 if (type1 == unsigned_char_type_node || type1 == char_type_node)
1155 return signed_char_type_node;
1156 if (type1 == unsigned_type_node)
1157 return integer_type_node;
1158 if (type1 == short_unsigned_type_node)
1159 return short_integer_type_node;
1160 if (type1 == long_unsigned_type_node)
1161 return long_integer_type_node;
1162 if (type1 == long_long_unsigned_type_node)
1163 return long_long_integer_type_node;
1167 /* Return a type the same as TYPE except unsigned or
1168 signed according to UNSIGNEDP. */
1171 signed_or_unsigned_type (unsignedp, type)
1175 if (! INTEGRAL_TYPE_P (type))
1177 if (TYPE_PRECISION (type) == TYPE_PRECISION (signed_char_type_node))
1178 return unsignedp ? unsigned_char_type_node : signed_char_type_node;
1179 if (TYPE_PRECISION (type) == TYPE_PRECISION (integer_type_node))
1180 return unsignedp ? unsigned_type_node : integer_type_node;
1181 if (TYPE_PRECISION (type) == TYPE_PRECISION (short_integer_type_node))
1182 return unsignedp ? short_unsigned_type_node : short_integer_type_node;
1183 if (TYPE_PRECISION (type) == TYPE_PRECISION (long_integer_type_node))
1184 return unsignedp ? long_unsigned_type_node : long_integer_type_node;
1185 if (TYPE_PRECISION (type) == TYPE_PRECISION (long_long_integer_type_node))
1186 return (unsignedp ? long_long_unsigned_type_node
1187 : long_long_integer_type_node);
1195 enum tree_code code = TREE_CODE (type);
1198 if (code == FUNCTION_TYPE)
1200 if (pedantic || warn_pointer_arith)
1201 pedwarn ("ANSI C++ forbids taking the sizeof a function type");
1202 return size_int (1);
1204 if (code == METHOD_TYPE)
1206 if (pedantic || warn_pointer_arith)
1207 pedwarn ("ANSI C++ forbids taking the sizeof a method type");
1208 return size_int (1);
1210 if (code == VOID_TYPE)
1212 if (pedantic || warn_pointer_arith)
1213 pedwarn ("ANSI C++ forbids taking the sizeof a void type");
1214 return size_int (1);
1216 if (code == ERROR_MARK)
1217 return size_int (1);
1219 /* ARM $5.3.2: ``When applied to a reference, the result is the size of the
1220 referenced object.'' */
1221 if (code == REFERENCE_TYPE)
1222 type = TREE_TYPE (type);
1224 /* We couldn't find anything in the ARM or the draft standard that says,
1225 one way or the other, if doing sizeof on something that doesn't have
1226 an object associated with it is correct or incorrect. For example, if
1227 you declare `struct S { char str[16]; };', and in your program do
1228 a `sizeof (S::str)', should we flag that as an error or should we give
1229 the size of it? Since it seems like a reasonable thing to do, we'll go
1230 with giving the value. */
1231 if (code == OFFSET_TYPE)
1232 type = TREE_TYPE (type);
1234 /* @@ This also produces an error for a signature ref.
1235 In that case we should be able to do better. */
1236 if (IS_SIGNATURE (type))
1238 error ("`sizeof' applied to a signature type");
1239 return size_int (0);
1242 if (TYPE_SIZE (type) == 0)
1244 error ("`sizeof' applied to an incomplete type");
1245 return size_int (0);
1248 /* Convert in case a char is more than one unit. */
1249 t = size_binop (CEIL_DIV_EXPR, TYPE_SIZE (type),
1250 size_int (TYPE_PRECISION (char_type_node)));
1251 /* size_binop does not put the constant in range, so do it now. */
1252 if (TREE_CODE (t) == INTEGER_CST && force_fit_type (t, 0))
1253 TREE_CONSTANT_OVERFLOW (t) = TREE_OVERFLOW (t) = 1;
1258 c_sizeof_nowarn (type)
1261 enum tree_code code = TREE_CODE (type);
1264 if (code == FUNCTION_TYPE
1265 || code == METHOD_TYPE
1266 || code == VOID_TYPE
1267 || code == ERROR_MARK)
1268 return size_int (1);
1269 if (code == REFERENCE_TYPE)
1270 type = TREE_TYPE (type);
1272 if (TYPE_SIZE (type) == 0)
1273 return size_int (0);
1275 /* Convert in case a char is more than one unit. */
1276 t = size_binop (CEIL_DIV_EXPR, TYPE_SIZE (type),
1277 size_int (TYPE_PRECISION (char_type_node)));
1278 force_fit_type (t, 0);
1282 /* Implement the __alignof keyword: Return the minimum required
1283 alignment of TYPE, measured in bytes. */
1289 enum tree_code code = TREE_CODE (type);
1292 if (code == FUNCTION_TYPE || code == METHOD_TYPE)
1293 return size_int (FUNCTION_BOUNDARY / BITS_PER_UNIT);
1295 if (code == VOID_TYPE || code == ERROR_MARK)
1296 return size_int (1);
1298 /* C++: this is really correct! */
1299 if (code == REFERENCE_TYPE)
1300 type = TREE_TYPE (type);
1302 /* @@ This also produces an error for a signature ref.
1303 In that case we should be able to do better. */
1304 if (IS_SIGNATURE (type))
1306 error ("`__alignof' applied to a signature type");
1307 return size_int (1);
1310 t = size_int (TYPE_ALIGN (type) / BITS_PER_UNIT);
1311 force_fit_type (t, 0);
1315 /* Perform default promotions for C data used in expressions.
1316 Arrays and functions are converted to pointers;
1317 enumeral types or short or char, to int.
1318 In addition, manifest constants symbols are replaced by their values.
1320 C++: this will automatically bash references to their target type. */
1323 decay_conversion (exp)
1326 register tree type = TREE_TYPE (exp);
1327 register enum tree_code code = TREE_CODE (type);
1329 if (code == OFFSET_TYPE /* || TREE_CODE (exp) == OFFSET_REF */ )
1331 if (TREE_CODE (exp) == OFFSET_REF)
1332 return decay_conversion (resolve_offset_ref (exp));
1334 type = TREE_TYPE (type);
1335 code = TREE_CODE (type);
1338 if (code == REFERENCE_TYPE)
1340 exp = convert_from_reference (exp);
1341 type = TREE_TYPE (exp);
1342 code = TREE_CODE (type);
1345 /* Constants can be used directly unless they're not loadable. */
1346 if (TREE_CODE (exp) == CONST_DECL)
1347 exp = DECL_INITIAL (exp);
1348 /* Replace a nonvolatile const static variable with its value. */
1349 else if (TREE_READONLY_DECL_P (exp))
1351 exp = decl_constant_value (exp);
1352 type = TREE_TYPE (exp);
1355 /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
1356 Leave such NOP_EXPRs, since RHS is being used in non-lvalue context. */
1358 if (code == VOID_TYPE)
1360 error ("void value not ignored as it ought to be");
1361 return error_mark_node;
1363 if (code == FUNCTION_TYPE)
1365 return build_unary_op (ADDR_EXPR, exp, 0);
1367 if (code == METHOD_TYPE)
1369 if (TREE_CODE (exp) == OFFSET_REF)
1371 /* FIXME: We should emit an error here about using a ptrmemfunc
1372 for something other than a function call. */
1373 my_friendly_assert (TREE_CODE (TREE_OPERAND (exp, 1)) == FUNCTION_DECL,
1375 return build_unary_op (ADDR_EXPR, TREE_OPERAND (exp, 1), 0);
1377 return build_unary_op (ADDR_EXPR, exp, 0);
1379 if (code == ARRAY_TYPE)
1384 int constp, volatilep;
1386 if (TREE_CODE (exp) == INDIRECT_REF)
1388 /* Stripping away the INDIRECT_REF is not the right
1389 thing to do for references... */
1390 tree inner = TREE_OPERAND (exp, 0);
1391 if (TREE_CODE (TREE_TYPE (inner)) == REFERENCE_TYPE)
1393 inner = build1 (CONVERT_EXPR,
1394 build_pointer_type (TREE_TYPE (TREE_TYPE (inner))),
1396 TREE_REFERENCE_EXPR (inner) = 1;
1398 return convert (build_pointer_type (TREE_TYPE (type)), inner);
1401 if (TREE_CODE (exp) == COMPOUND_EXPR)
1403 tree op1 = decay_conversion (TREE_OPERAND (exp, 1));
1404 return build (COMPOUND_EXPR, TREE_TYPE (op1),
1405 TREE_OPERAND (exp, 0), op1);
1409 && ! (TREE_CODE (exp) == CONSTRUCTOR && TREE_STATIC (exp)))
1411 error ("invalid use of non-lvalue array");
1412 return error_mark_node;
1415 constp = volatilep = 0;
1416 if (TREE_CODE_CLASS (TREE_CODE (exp)) == 'r'
1417 || TREE_CODE_CLASS (TREE_CODE (exp)) == 'd')
1419 constp = TREE_READONLY (exp);
1420 volatilep = TREE_THIS_VOLATILE (exp);
1423 restype = TREE_TYPE (type);
1424 if (TYPE_READONLY (type) || TYPE_VOLATILE (type)
1425 || constp || volatilep)
1426 restype = cp_build_type_variant (restype,
1427 TYPE_READONLY (type) || constp,
1428 TYPE_VOLATILE (type) || volatilep);
1429 ptrtype = build_pointer_type (restype);
1431 if (TREE_CODE (exp) == VAR_DECL)
1433 /* ??? This is not really quite correct
1434 in that the type of the operand of ADDR_EXPR
1435 is not the target type of the type of the ADDR_EXPR itself.
1436 Question is, can this lossage be avoided? */
1437 adr = build1 (ADDR_EXPR, ptrtype, exp);
1438 if (mark_addressable (exp) == 0)
1439 return error_mark_node;
1440 TREE_CONSTANT (adr) = staticp (exp);
1441 TREE_SIDE_EFFECTS (adr) = 0; /* Default would be, same as EXP. */
1444 /* This way is better for a COMPONENT_REF since it can
1445 simplify the offset for a component. */
1446 adr = build_unary_op (ADDR_EXPR, exp, 1);
1447 return convert (ptrtype, adr);
1454 default_conversion (exp)
1458 enum tree_code code;
1460 exp = decay_conversion (exp);
1462 type = TREE_TYPE (exp);
1463 code = TREE_CODE (type);
1465 if (INTEGRAL_CODE_P (code))
1467 tree t = type_promotes_to (type);
1469 return convert (t, exp);
1471 if (flag_traditional
1472 && TYPE_MAIN_VARIANT (type) == float_type_node)
1473 return convert (double_type_node, exp);
1479 build_object_ref (datum, basetype, field)
1480 tree datum, basetype, field;
1483 if (datum == error_mark_node)
1484 return error_mark_node;
1486 dtype = TREE_TYPE (datum);
1487 if (TREE_CODE (dtype) == REFERENCE_TYPE)
1488 dtype = TREE_TYPE (dtype);
1489 if (! IS_AGGR_TYPE_CODE (TREE_CODE (dtype)))
1491 cp_error ("request for member `%T::%D' in expression of non-aggregate type `%T'",
1492 basetype, field, dtype);
1493 return error_mark_node;
1495 else if (IS_SIGNATURE (basetype))
1497 warning ("signature name in scope resolution ignored");
1498 return build_component_ref (datum, field, NULL_TREE, 1);
1500 else if (is_aggr_type (basetype, 1))
1502 tree binfo = binfo_or_else (basetype, dtype);
1504 return build_component_ref (build_scoped_ref (datum, basetype),
1507 return error_mark_node;
1510 /* Like `build_component_ref, but uses an already found field.
1511 Must compute access for C_C_D. Otherwise, ok. */
1513 build_component_ref_1 (datum, field, protect)
1517 register tree basetype = TREE_TYPE (datum);
1518 register enum tree_code code = TREE_CODE (basetype);
1521 if (code == REFERENCE_TYPE)
1523 datum = convert_from_reference (datum);
1524 basetype = TREE_TYPE (datum);
1525 code = TREE_CODE (basetype);
1528 if (! IS_AGGR_TYPE_CODE (code))
1530 if (code != ERROR_MARK)
1531 cp_error ("request for member `%D' in `%E', which is of non-aggregate type `%T'",
1532 field, datum, basetype);
1533 return error_mark_node;
1536 if (TYPE_SIZE (basetype) == 0)
1538 incomplete_type_error (0, basetype);
1539 return error_mark_node;
1542 /* Look up component name in the structure type definition. */
1544 if (field == error_mark_node)
1545 my_friendly_abort (115);
1547 if (TREE_STATIC (field))
1552 tree access = compute_access (TYPE_BINFO (current_class_type), field);
1554 if (access == access_private_node)
1556 cp_error ("field `%D' is private", field);
1557 return error_mark_node;
1559 else if (access == access_protected_node)
1561 cp_error ("field `%D' is protected", field);
1562 return error_mark_node;
1566 ref = build (COMPONENT_REF, TREE_TYPE (field), datum, field);
1568 if (TREE_READONLY (datum) || TREE_READONLY (field))
1569 TREE_READONLY (ref) = 1;
1570 if (TREE_THIS_VOLATILE (datum) || TREE_THIS_VOLATILE (field))
1571 TREE_THIS_VOLATILE (ref) = 1;
1572 if (DECL_MUTABLE_P (field))
1573 TREE_READONLY (ref) = 0;
1578 /* Given a COND_EXPR in T, return it in a form that we can, for
1579 example, use as an lvalue. This code used to be in unary_complex_lvalue,
1580 but we needed it to deal with `a = (d == c) ? b : c' expressions, where
1581 we're dealing with aggregates. So, we now call this in unary_complex_lvalue,
1582 and in build_modify_expr. The case (in particular) that led to this was
1583 with CODE == ADDR_EXPR, since it's not an lvalue when we'd get it there. */
1585 rationalize_conditional_expr (code, t)
1586 enum tree_code code;
1590 build_conditional_expr (TREE_OPERAND (t, 0),
1591 build_unary_op (code, TREE_OPERAND (t, 1), 0),
1592 build_unary_op (code, TREE_OPERAND (t, 2), 0));
1595 /* Given the TYPE of an anonymous union field inside T, return the
1596 FIELD_DECL for the field. If not found return NULL_TREE. Because
1597 anonymous unions can nest, we must also search all anonymous unions
1598 that are directly reachable. */
1600 lookup_anon_field (t, type)
1605 for (field = TYPE_FIELDS (t); field; field = TREE_CHAIN (field))
1607 if (TREE_STATIC (field))
1609 if (TREE_CODE (field) != FIELD_DECL)
1612 /* If we find it directly, return the field. */
1613 if (DECL_NAME (field) == NULL_TREE
1614 && type == TREE_TYPE (field))
1619 /* Otherwise, it could be nested, search harder. */
1620 if (DECL_NAME (field) == NULL_TREE
1621 && TREE_CODE (TREE_TYPE (field)) == UNION_TYPE)
1623 tree subfield = lookup_anon_field (TREE_TYPE (field), type);
1631 /* Build a COMPONENT_REF for a given DATUM, and it's member COMPONENT.
1632 COMPONENT can be an IDENTIFIER_NODE that is the name of the member
1633 that we are interested in, or it can be a FIELD_DECL. */
1635 build_component_ref (datum, component, basetype_path, protect)
1636 tree datum, component, basetype_path;
1639 register tree basetype = TREE_TYPE (datum);
1640 register enum tree_code code = TREE_CODE (basetype);
1641 register tree field = NULL;
1644 /* If DATUM is a COMPOUND_EXPR or COND_EXPR, move our reference inside it. */
1645 switch (TREE_CODE (datum))
1649 tree value = build_component_ref (TREE_OPERAND (datum, 1), component,
1650 basetype_path, protect);
1651 return build (COMPOUND_EXPR, TREE_TYPE (value),
1652 TREE_OPERAND (datum, 0), value);
1655 return build_conditional_expr
1656 (TREE_OPERAND (datum, 0),
1657 build_component_ref (TREE_OPERAND (datum, 1), component,
1658 basetype_path, protect),
1659 build_component_ref (TREE_OPERAND (datum, 2), component,
1660 basetype_path, protect));
1663 if (code == REFERENCE_TYPE)
1665 datum = convert_from_reference (datum);
1666 basetype = TREE_TYPE (datum);
1667 code = TREE_CODE (basetype);
1670 /* First, see if there is a field or component with name COMPONENT. */
1671 if (TREE_CODE (component) == TREE_LIST)
1673 my_friendly_assert (!(TREE_CHAIN (component) == NULL_TREE
1674 && DECL_CHAIN (TREE_VALUE (component)) == NULL_TREE), 309);
1675 return build (COMPONENT_REF, TREE_TYPE (component), datum, component);
1678 if (! IS_AGGR_TYPE_CODE (code))
1680 if (code != ERROR_MARK)
1681 cp_error ("request for member `%D' in `%E', which is of non-aggregate type `%T'",
1682 component, datum, basetype);
1683 return error_mark_node;
1686 if (TYPE_SIZE (basetype) == 0)
1688 incomplete_type_error (0, basetype);
1689 return error_mark_node;
1692 if (TREE_CODE (component) == BIT_NOT_EXPR)
1694 if (TYPE_IDENTIFIER (basetype) != TREE_OPERAND (component, 0))
1696 cp_error ("destructor specifier `%T::~%T' must have matching names",
1697 basetype, TREE_OPERAND (component, 0));
1698 return error_mark_node;
1700 if (! TYPE_HAS_DESTRUCTOR (basetype))
1702 cp_error ("type `%T' has no destructor", basetype);
1703 return error_mark_node;
1705 return TREE_VEC_ELT (CLASSTYPE_METHOD_VEC (basetype), 0);
1708 /* Look up component name in the structure type definition. */
1709 if (CLASSTYPE_VFIELD (basetype)
1710 && DECL_NAME (CLASSTYPE_VFIELD (basetype)) == component)
1711 /* Special-case this because if we use normal lookups in an ambiguous
1712 hierarchy, the compiler will abort (because vptr lookups are
1713 not supposed to be ambiguous. */
1714 field = CLASSTYPE_VFIELD (basetype);
1715 else if (TREE_CODE (component) == FIELD_DECL)
1721 if (basetype_path == NULL_TREE)
1722 basetype_path = TYPE_BINFO (basetype);
1723 field = lookup_field (basetype_path, component,
1724 protect && ! VFIELD_NAME_P (component), 0);
1725 if (field == error_mark_node)
1726 return error_mark_node;
1728 if (field == NULL_TREE)
1730 /* Not found as a data field, look for it as a method. If found,
1731 then if this is the only possible one, return it, else
1732 report ambiguity error. */
1733 tree fndecls = lookup_fnfields (basetype_path, component, 1);
1734 if (fndecls == error_mark_node)
1735 return error_mark_node;
1738 if (TREE_CHAIN (fndecls) == NULL_TREE
1739 && DECL_CHAIN (TREE_VALUE (fndecls)) == NULL_TREE)
1741 tree access, fndecl;
1743 /* Unique, so use this one now. */
1744 basetype = TREE_PURPOSE (fndecls);
1745 fndecl = TREE_VALUE (fndecls);
1746 access = compute_access (TREE_PURPOSE (fndecls), fndecl);
1747 if (access == access_public_node)
1749 if (DECL_VINDEX (fndecl)
1750 && ! resolves_to_fixed_type_p (datum, 0))
1752 tree addr = build_unary_op (ADDR_EXPR, datum, 0);
1753 addr = convert_pointer_to (DECL_CONTEXT (fndecl), addr);
1754 datum = build_indirect_ref (addr, NULL_PTR);
1755 my_friendly_assert (datum != error_mark_node, 310);
1756 fndecl = build_vfn_ref (&addr, datum, DECL_VINDEX (fndecl));
1761 if (access == access_protected_node)
1762 cp_error ("member function `%D' is protected", fndecl);
1764 cp_error ("member function `%D' is private", fndecl);
1765 return error_mark_node;
1769 /* Just act like build_offset_ref, since the object does
1770 not matter unless we're actually calling the function. */
1773 t = build_tree_list (error_mark_node, fndecls);
1774 TREE_TYPE (t) = build_offset_type (basetype,
1780 cp_error ("`%#T' has no member named `%D'", basetype, component);
1781 return error_mark_node;
1783 else if (TREE_TYPE (field) == error_mark_node)
1784 return error_mark_node;
1786 if (TREE_CODE (field) != FIELD_DECL)
1788 if (TREE_CODE (field) == TYPE_DECL)
1790 cp_error ("invalid use of type decl `%#D' as expression", field);
1791 return error_mark_node;
1793 else if (DECL_RTL (field) != 0)
1796 TREE_USED (field) = 1;
1801 if (DECL_FIELD_CONTEXT (field) != basetype)
1803 tree context = DECL_FIELD_CONTEXT (field);
1804 if (ANON_AGGRNAME_P (TYPE_IDENTIFIER (context)))
1806 tree subfield = lookup_anon_field (basetype, context);
1807 tree subdatum = build_component_ref (datum, subfield,
1808 basetype_path, protect);
1809 return build_component_ref (subdatum, field, basetype_path, protect);
1813 if (DECL_FIELD_CONTEXT (field) != basetype
1814 && TYPE_USES_COMPLEX_INHERITANCE (basetype))
1816 tree addr = build_unary_op (ADDR_EXPR, datum, 0);
1817 if (integer_zerop (addr))
1819 error ("invalid reference to NULL ptr, use ptr-to-member instead");
1820 return error_mark_node;
1822 if (VBASE_NAME_P (DECL_NAME (field)))
1824 /* It doesn't matter which vbase pointer we grab, just
1825 find one of them. */
1826 tree binfo = get_binfo (DECL_FIELD_CONTEXT (field),
1827 TREE_TYPE (TREE_TYPE (addr)), 0);
1828 addr = convert_pointer_to_real (binfo, addr);
1831 addr = convert_pointer_to (DECL_FIELD_CONTEXT (field), addr);
1832 datum = build_indirect_ref (addr, NULL_PTR);
1833 my_friendly_assert (datum != error_mark_node, 311);
1835 ref = fold (build (COMPONENT_REF, TREE_TYPE (field),
1836 break_out_cleanups (datum), field));
1838 if (TREE_READONLY (datum) || TREE_READONLY (field))
1839 TREE_READONLY (ref) = 1;
1840 if (TREE_THIS_VOLATILE (datum) || TREE_THIS_VOLATILE (field))
1841 TREE_THIS_VOLATILE (ref) = 1;
1842 if (DECL_MUTABLE_P (field))
1843 TREE_READONLY (ref) = 0;
1848 /* Given an expression PTR for a pointer, return an expression
1849 for the value pointed to.
1850 ERRORSTRING is the name of the operator to appear in error messages.
1852 This function may need to overload OPERATOR_FNNAME.
1853 Must also handle REFERENCE_TYPEs for C++. */
1856 build_x_indirect_ref (ptr, errorstring)
1860 tree rval = build_opfncall (INDIRECT_REF, LOOKUP_NORMAL, ptr, NULL_TREE, NULL_TREE);
1863 return build_indirect_ref (ptr, errorstring);
1867 build_indirect_ref (ptr, errorstring)
1871 register tree pointer = (TREE_CODE (TREE_TYPE (ptr)) == REFERENCE_TYPE ?
1872 ptr : default_conversion (ptr));
1873 register tree type = TREE_TYPE (pointer);
1875 if (ptr == current_class_decl)
1878 if (IS_AGGR_TYPE (type))
1880 ptr = build_expr_type_conversion (WANT_POINTER, pointer, 1);
1885 type = TREE_TYPE (pointer);
1889 if (TREE_CODE (type) == POINTER_TYPE || TREE_CODE (type) == REFERENCE_TYPE)
1891 if (TREE_CODE (pointer) == ADDR_EXPR
1892 && (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_OPERAND (pointer, 0)))
1893 == TYPE_MAIN_VARIANT (TREE_TYPE (type)))
1894 && (TREE_READONLY (TREE_OPERAND (pointer, 0))
1895 == TYPE_READONLY (TREE_TYPE (type)))
1896 && (TREE_THIS_VOLATILE (TREE_OPERAND (pointer, 0))
1897 == TYPE_VOLATILE (TREE_TYPE (type))))
1898 return TREE_OPERAND (pointer, 0);
1901 tree t = TREE_TYPE (type);
1902 register tree ref = build1 (INDIRECT_REF,
1903 TYPE_MAIN_VARIANT (t), pointer);
1905 TREE_READONLY (ref) = TYPE_READONLY (t);
1906 TREE_THIS_VOLATILE (ref) = TYPE_VOLATILE (t);
1907 TREE_SIDE_EFFECTS (ref)
1908 = TYPE_VOLATILE (t) || TREE_SIDE_EFFECTS (pointer);
1912 /* `pointer' won't be an error_mark_node if we were given a
1913 pointer to member, so it's cool to check for this here. */
1914 else if (TYPE_PTRMEMFUNC_P (type))
1915 error ("invalid use of `%s' on pointer to member function", errorstring);
1916 else if (TREE_CODE (type) == RECORD_TYPE
1917 && (IS_SIGNATURE_POINTER (type) || IS_SIGNATURE_REFERENCE (type)))
1918 error ("cannot dereference signature pointer/reference");
1919 else if (pointer != error_mark_node)
1922 error ("invalid type argument of `%s'", errorstring);
1924 error ("invalid type argument");
1926 return error_mark_node;
1929 /* This handles expressions of the form "a[i]", which denotes
1932 This is logically equivalent in C to *(a+i), but we may do it differently.
1933 If A is a variable or a member, we generate a primitive ARRAY_REF.
1934 This avoids forcing the array out of registers, and can work on
1935 arrays that are not lvalues (for example, members of structures returned
1938 If INDEX is of some user-defined type, it must be converted to
1939 integer type. Otherwise, to make a compatible PLUS_EXPR, it
1940 will inherit the type of the array, which will be some pointer type. */
1943 build_x_array_ref (array, index)
1946 tree rval = build_opfncall (ARRAY_REF, LOOKUP_NORMAL, array, index, NULL_TREE);
1949 return build_array_ref (array, index);
1953 build_array_ref (array, idx)
1960 error ("subscript missing in array reference");
1961 return error_mark_node;
1964 if (TREE_TYPE (array) == error_mark_node
1965 || TREE_TYPE (idx) == error_mark_node)
1966 return error_mark_node;
1968 itype = TREE_TYPE (idx);
1970 if (TREE_CODE (TREE_TYPE (array)) == ARRAY_TYPE
1971 && TREE_CODE (array) != INDIRECT_REF)
1975 /* Subscripting with type char is likely to lose
1976 on a machine where chars are signed.
1977 So warn on any machine, but optionally.
1978 Don't warn for unsigned char since that type is safe.
1979 Don't warn for signed char because anyone who uses that
1980 must have done so deliberately. */
1981 if (warn_char_subscripts
1982 && TYPE_MAIN_VARIANT (TREE_TYPE (idx)) == char_type_node)
1983 warning ("array subscript has type `char'");
1985 /* Apply default promotions *after* noticing character types. */
1986 idx = default_conversion (idx);
1988 if (TREE_CODE (TREE_TYPE (idx)) != INTEGER_TYPE)
1990 error ("array subscript is not an integer");
1991 return error_mark_node;
1994 /* An array that is indexed by a non-constant
1995 cannot be stored in a register; we must be able to do
1996 address arithmetic on its address.
1997 Likewise an array of elements of variable size. */
1998 if (TREE_CODE (idx) != INTEGER_CST
1999 || (TYPE_SIZE (TREE_TYPE (TREE_TYPE (array))) != 0
2000 && TREE_CODE (TYPE_SIZE (TREE_TYPE (TREE_TYPE (array)))) != INTEGER_CST))
2002 if (mark_addressable (array) == 0)
2003 return error_mark_node;
2005 /* An array that is indexed by a constant value which is not within
2006 the array bounds cannot be stored in a register either; because we
2007 would get a crash in store_bit_field/extract_bit_field when trying
2008 to access a non-existent part of the register. */
2009 if (TREE_CODE (idx) == INTEGER_CST
2010 && TYPE_VALUES (TREE_TYPE (array))
2011 && ! int_fits_type_p (idx, TYPE_VALUES (TREE_TYPE (array))))
2013 if (mark_addressable (array) == 0)
2014 return error_mark_node;
2017 if (pedantic && !lvalue_p (array))
2018 pedwarn ("ANSI C++ forbids subscripting non-lvalue array");
2020 /* Note in C++ it is valid to subscript a `register' array, since
2021 it is valid to take the address of something with that
2022 storage specification. */
2026 while (TREE_CODE (foo) == COMPONENT_REF)
2027 foo = TREE_OPERAND (foo, 0);
2028 if (TREE_CODE (foo) == VAR_DECL && DECL_REGISTER (foo))
2029 warning ("subscripting array declared `register'");
2032 type = TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (array)));
2033 rval = build (ARRAY_REF, type, array, idx);
2034 /* Array ref is const/volatile if the array elements are
2035 or if the array is.. */
2036 TREE_READONLY (rval)
2037 |= (TYPE_READONLY (TREE_TYPE (TREE_TYPE (array)))
2038 | TREE_READONLY (array));
2039 TREE_SIDE_EFFECTS (rval)
2040 |= (TYPE_VOLATILE (TREE_TYPE (TREE_TYPE (array)))
2041 | TREE_SIDE_EFFECTS (array));
2042 TREE_THIS_VOLATILE (rval)
2043 |= (TYPE_VOLATILE (TREE_TYPE (TREE_TYPE (array)))
2044 /* This was added by rms on 16 Nov 91.
2045 It fixes vol struct foo *a; a->elts[1]
2046 in an inline function.
2047 Hope it doesn't break something else. */
2048 | TREE_THIS_VOLATILE (array));
2049 return require_complete_type (fold (rval));
2053 tree ar = default_conversion (array);
2054 tree ind = default_conversion (idx);
2056 /* Put the integer in IND to simplify error checking. */
2057 if (TREE_CODE (TREE_TYPE (ar)) == INTEGER_TYPE)
2064 if (ar == error_mark_node)
2067 if (TREE_CODE (TREE_TYPE (ar)) != POINTER_TYPE)
2069 error ("subscripted value is neither array nor pointer");
2070 return error_mark_node;
2072 if (TREE_CODE (TREE_TYPE (ind)) != INTEGER_TYPE)
2074 error ("array subscript is not an integer");
2075 return error_mark_node;
2078 return build_indirect_ref (build_binary_op_nodefault (PLUS_EXPR, ar, ind, PLUS_EXPR),
2083 /* Build a function call to function FUNCTION with parameters PARAMS.
2084 PARAMS is a list--a chain of TREE_LIST nodes--in which the
2085 TREE_VALUE of each node is a parameter-expression.
2086 FUNCTION's data type may be a function type or a pointer-to-function.
2088 For C++: If FUNCTION's data type is a TREE_LIST, then the tree list
2089 is the list of possible methods that FUNCTION could conceivably
2090 be. If the list of methods comes from a class, then it will be
2091 a list of lists (where each element is associated with the class
2092 that produced it), otherwise it will be a simple list (for
2093 functions overloaded in global scope).
2095 In the first case, TREE_VALUE (function) is the head of one of those
2096 lists, and TREE_PURPOSE is the name of the function.
2098 In the second case, TREE_PURPOSE (function) is the function's
2101 DECL is the class instance variable, usually CURRENT_CLASS_DECL. */
2104 * [eichin:19911015.1726EST] actually return a possibly incomplete
2108 build_x_function_call (function, params, decl)
2109 tree function, params, decl;
2114 if (function == error_mark_node)
2115 return error_mark_node;
2117 type = TREE_TYPE (function);
2118 is_method = ((TREE_CODE (function) == TREE_LIST
2119 && current_class_type != NULL_TREE
2120 && IDENTIFIER_CLASS_VALUE (TREE_PURPOSE (function)) == function)
2121 || TREE_CODE (function) == IDENTIFIER_NODE
2122 || TREE_CODE (type) == METHOD_TYPE
2123 || TYPE_PTRMEMFUNC_P (type));
2125 /* Handle methods, friends, and overloaded functions, respectively. */
2128 if (TREE_CODE (function) == FUNCTION_DECL)
2130 if (DECL_NAME (function))
2131 function = DECL_NAME (function);
2133 function = TYPE_IDENTIFIER (DECL_CLASS_CONTEXT (function));
2135 else if (TREE_CODE (function) == TREE_LIST)
2137 my_friendly_assert (TREE_CODE (TREE_VALUE (function)) == FUNCTION_DECL, 312);
2138 function = TREE_PURPOSE (function);
2140 else if (TREE_CODE (function) != IDENTIFIER_NODE)
2142 if (TREE_CODE (function) == OFFSET_REF)
2144 if (TREE_OPERAND (function, 0))
2145 decl = TREE_OPERAND (function, 0);
2147 /* Call via a pointer to member function. */
2148 if (decl == NULL_TREE)
2150 error ("pointer to member function called, but not in class scope");
2151 return error_mark_node;
2153 /* What other type of POINTER_TYPE could this be? */
2154 if (TREE_CODE (TREE_TYPE (function)) != POINTER_TYPE
2155 && ! TYPE_PTRMEMFUNC_P (TREE_TYPE (function))
2156 && TREE_CODE (function) != OFFSET_REF)
2157 function = build (OFFSET_REF, TREE_TYPE (type), NULL_TREE, function);
2161 /* this is an abbreviated method call.
2162 must go through here in case it is a virtual function.
2163 @@ Perhaps this could be optimized. */
2165 if (decl == NULL_TREE)
2167 if (current_class_type == NULL_TREE)
2169 error ("object missing in call to method `%s'",
2170 IDENTIFIER_POINTER (function));
2171 return error_mark_node;
2173 /* Yow: call from a static member function. */
2174 decl = build1 (NOP_EXPR, build_pointer_type (current_class_type),
2176 decl = build_indirect_ref (decl, NULL_PTR);
2179 return build_method_call (decl, function, params,
2180 NULL_TREE, LOOKUP_NORMAL);
2182 else if (TREE_CODE (function) == COMPONENT_REF
2183 && type == unknown_type_node)
2185 /* Should we undo what was done in build_component_ref? */
2186 if (TREE_CODE (TREE_PURPOSE (TREE_OPERAND (function, 1))) == TREE_VEC)
2187 /* Get the name that build_component_ref hid. */
2188 function = DECL_NAME (TREE_VALUE (TREE_OPERAND (function, 1)));
2190 function = TREE_PURPOSE (TREE_OPERAND (function, 1));
2191 return build_method_call (decl, function, params,
2192 NULL_TREE, LOOKUP_NORMAL);
2194 else if (TREE_CODE (function) == TREE_LIST)
2196 if (TREE_VALUE (function) == NULL_TREE)
2198 cp_error ("function `%D' declared overloaded, but no definitions appear with which to resolve it?!?",
2199 TREE_PURPOSE (function));
2200 return error_mark_node;
2204 tree val = TREE_VALUE (function);
2206 if (TREE_CODE (val) == TEMPLATE_DECL)
2207 return build_overload_call_maybe
2208 (function, params, LOOKUP_COMPLAIN, (struct candidate *)0);
2209 else if (DECL_CHAIN (val) != NULL_TREE)
2210 return build_overload_call
2211 (function, params, LOOKUP_COMPLAIN, (struct candidate *)0);
2213 my_friendly_abort (360);
2218 if (TREE_CODE (function) == OFFSET_REF)
2220 /* If the component is a data element (or a virtual function), we play
2221 games here to make things work. */
2224 if (TREE_OPERAND (function, 0))
2225 decl = TREE_OPERAND (function, 0);
2229 decl_addr = build_unary_op (ADDR_EXPR, decl, 0);
2230 function = get_member_function_from_ptrfunc (&decl_addr,
2231 TREE_OPERAND (function, 1));
2232 params = tree_cons (NULL_TREE, decl_addr, params);
2233 return build_function_call (function, params);
2236 type = TREE_TYPE (function);
2237 if (type != error_mark_node)
2239 if (TREE_CODE (type) == REFERENCE_TYPE)
2240 type = TREE_TYPE (type);
2242 if (TYPE_LANG_SPECIFIC (type) && TYPE_OVERLOADS_CALL_EXPR (type))
2243 return build_opfncall (CALL_EXPR, LOOKUP_NORMAL, function, params, NULL_TREE);
2248 tree fntype = TREE_TYPE (function);
2251 /* Explicitly named method? */
2252 if (TREE_CODE (function) == FUNCTION_DECL)
2253 ctypeptr = build_pointer_type (DECL_CLASS_CONTEXT (function));
2254 /* Expression with ptr-to-method type? It could either be a plain
2255 usage, or it might be a case where the ptr-to-method is being
2256 passed in as an argument. */
2257 else if (TYPE_PTRMEMFUNC_P (fntype))
2259 tree rec = TYPE_METHOD_BASETYPE (TREE_TYPE (TYPE_PTRMEMFUNC_FN_TYPE (fntype)));
2260 ctypeptr = build_pointer_type (rec);
2262 /* Unexpected node type? */
2264 my_friendly_abort (116);
2265 if (decl == NULL_TREE)
2267 if (current_function_decl
2268 && DECL_STATIC_FUNCTION_P (current_function_decl))
2269 error ("invalid call to member function needing `this' in static member function scope");
2271 error ("pointer to member function called, but not in class scope");
2272 return error_mark_node;
2274 if (TREE_CODE (TREE_TYPE (decl)) != POINTER_TYPE
2275 && ! TYPE_PTRMEMFUNC_P (TREE_TYPE (decl)))
2277 decl = build_unary_op (ADDR_EXPR, decl, 0);
2278 decl = convert_pointer_to (TREE_TYPE (ctypeptr), decl);
2281 decl = build_c_cast (ctypeptr, decl, 0);
2282 params = tree_cons (NULL_TREE, decl, params);
2285 return build_function_call (function, params);
2288 /* Resolve a pointer to member function. INSTANCE is the object
2289 instance to use, if the member points to a virtual member. */
2292 get_member_function_from_ptrfunc (instance_ptrptr, function)
2293 tree *instance_ptrptr;
2296 if (TREE_CODE (function) == OFFSET_REF)
2298 function = TREE_OPERAND (function, 1);
2301 if (TYPE_PTRMEMFUNC_P (TREE_TYPE (function)))
2303 tree fntype, index, e1, delta, delta2, e2, e3, aref, vtbl;
2306 tree instance_ptr = *instance_ptrptr;
2308 if (TREE_SIDE_EFFECTS (instance_ptr))
2309 instance_ptr = save_expr (instance_ptr);
2311 if (TREE_SIDE_EFFECTS (function))
2312 function = save_expr (function);
2314 fntype = TYPE_PTRMEMFUNC_FN_TYPE (TREE_TYPE (function));
2315 index = save_expr (build_component_ref (function,
2318 e1 = build (GT_EXPR, boolean_type_node, index,
2319 convert (delta_type_node, integer_zero_node));
2320 delta = convert (ptrdiff_type_node,
2321 build_component_ref (function, delta_identifier, 0, 0));
2322 delta2 = DELTA2_FROM_PTRMEMFUNC (function);
2324 /* convert down to the right base, before using the instance. */
2326 = convert_pointer_to_real (TYPE_METHOD_BASETYPE (TREE_TYPE (fntype)),
2328 if (instance == error_mark_node)
2331 vtbl = convert_pointer_to (ptr_type_node, instance);
2334 build_pointer_type (build_pointer_type (vtable_entry_type)),
2335 vtbl, convert (ptrdiff_type_node, delta2));
2336 vtbl = build_indirect_ref (vtbl, NULL_PTR);
2337 aref = build_array_ref (vtbl, build_binary_op (MINUS_EXPR,
2339 integer_one_node, 1));
2340 if (! flag_vtable_thunks)
2342 aref = save_expr (aref);
2344 /* Save the intermediate result in a SAVE_EXPR so we don't have to
2345 compute each component of the virtual function pointer twice. */
2346 if (TREE_CODE (aref) == INDIRECT_REF)
2347 TREE_OPERAND (aref, 0) = save_expr (TREE_OPERAND (aref, 0));
2349 delta = build_binary_op (PLUS_EXPR,
2350 build_conditional_expr (e1, build_component_ref (aref, delta_identifier, 0, 0), integer_zero_node),
2354 *instance_ptrptr = build (PLUS_EXPR, TREE_TYPE (instance_ptr),
2355 instance_ptr, delta);
2356 if (flag_vtable_thunks)
2359 e2 = build_component_ref (aref, pfn_identifier, 0, 0);
2361 e3 = PFN_FROM_PTRMEMFUNC (function);
2362 TREE_TYPE (e2) = TREE_TYPE (e3);
2363 function = build_conditional_expr (e1, e2, e3);
2365 /* Make sure this doesn't get evaluated first inside one of the
2366 branches of the COND_EXPR. */
2367 if (TREE_CODE (instance_ptr) == SAVE_EXPR)
2368 function = build (COMPOUND_EXPR, TREE_TYPE (function),
2369 instance_ptr, function);
2375 build_function_call_real (function, params, require_complete, flags)
2376 tree function, params;
2377 int require_complete, flags;
2379 register tree fntype, fndecl;
2380 register tree value_type;
2381 register tree coerced_params;
2382 tree name = NULL_TREE, assembler_name = NULL_TREE;
2385 /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
2386 Strip such NOP_EXPRs, since FUNCTION is used in non-lvalue context. */
2387 if (TREE_CODE (function) == NOP_EXPR
2388 && TREE_TYPE (function) == TREE_TYPE (TREE_OPERAND (function, 0)))
2389 function = TREE_OPERAND (function, 0);
2391 if (TREE_CODE (function) == FUNCTION_DECL)
2393 name = DECL_NAME (function);
2394 assembler_name = DECL_ASSEMBLER_NAME (function);
2396 GNU_xref_call (current_function_decl,
2397 IDENTIFIER_POINTER (name ? name
2398 : TYPE_IDENTIFIER (DECL_CLASS_CONTEXT (function))));
2399 mark_used (function);
2402 /* Convert anything with function type to a pointer-to-function. */
2405 && IDENTIFIER_LENGTH (name) == 4
2406 && ! strcmp (IDENTIFIER_POINTER (name), "main")
2407 && DECL_CONTEXT (function) == NULL_TREE)
2409 pedwarn ("ANSI C++ forbids calling `main' from within program");
2412 if (pedantic && DECL_THIS_INLINE (function) && ! DECL_INITIAL (function)
2413 && ! DECL_ARTIFICIAL (function)
2414 && ! DECL_PENDING_INLINE_INFO (function))
2415 cp_pedwarn ("inline function `%#D' called before definition",
2418 /* Differs from default_conversion by not setting TREE_ADDRESSABLE
2419 (because calling an inline function does not mean the function
2420 needs to be separately compiled). */
2422 if (DECL_INLINE (function))
2424 /* Is it a synthesized method that needs to be synthesized? */
2425 if (DECL_ARTIFICIAL (function) && ! DECL_INITIAL (function)
2426 /* Kludge: don't synthesize for default args. */
2427 && current_function_decl)
2428 synthesize_method (function);
2430 fntype = build_type_variant (TREE_TYPE (function),
2431 TREE_READONLY (function),
2432 TREE_THIS_VOLATILE (function));
2433 function = build1 (ADDR_EXPR, build_pointer_type (fntype), function);
2436 function = default_conversion (function);
2442 /* Convert anything with function type to a pointer-to-function. */
2443 if (function == error_mark_node)
2444 return error_mark_node;
2445 function = default_conversion (function);
2448 fntype = TREE_TYPE (function);
2450 if (TYPE_PTRMEMFUNC_P (fntype))
2452 tree instance_ptr = build_unary_op (ADDR_EXPR, C_C_D, 0);
2453 fntype = TYPE_PTRMEMFUNC_FN_TYPE (fntype);
2454 function = get_member_function_from_ptrfunc (&instance_ptr, function);
2457 is_method = (TREE_CODE (fntype) == POINTER_TYPE
2458 && TREE_CODE (TREE_TYPE (fntype)) == METHOD_TYPE);
2460 if (!((TREE_CODE (fntype) == POINTER_TYPE
2461 && TREE_CODE (TREE_TYPE (fntype)) == FUNCTION_TYPE)
2464 cp_error ("`%E' cannot be used as a function", function);
2465 return error_mark_node;
2468 /* fntype now gets the type of function pointed to. */
2469 fntype = TREE_TYPE (fntype);
2471 /* Convert the parameters to the types declared in the
2472 function prototype, or apply default promotions. */
2474 if (flags & LOOKUP_COMPLAIN)
2475 coerced_params = convert_arguments (NULL_TREE, TYPE_ARG_TYPES (fntype),
2476 params, fndecl, LOOKUP_NORMAL);
2478 coerced_params = convert_arguments (NULL_TREE, TYPE_ARG_TYPES (fntype),
2481 if (coerced_params == error_mark_node)
2482 if (flags & LOOKUP_SPECULATIVELY)
2485 return error_mark_node;
2487 /* Check for errors in format strings. */
2489 if (warn_format && (name || assembler_name))
2490 check_function_format (name, assembler_name, coerced_params);
2492 /* Recognize certain built-in functions so we can make tree-codes
2493 other than CALL_EXPR. We do this when it enables fold-const.c
2494 to do something useful. */
2496 if (TREE_CODE (function) == ADDR_EXPR
2497 && TREE_CODE (TREE_OPERAND (function, 0)) == FUNCTION_DECL
2498 && DECL_BUILT_IN (TREE_OPERAND (function, 0)))
2499 switch (DECL_FUNCTION_CODE (TREE_OPERAND (function, 0)))
2504 if (coerced_params == 0)
2505 return integer_zero_node;
2506 return build_unary_op (ABS_EXPR, TREE_VALUE (coerced_params), 0);
2510 value_type = TREE_TYPE (fntype) ? TREE_TYPE (fntype) : void_type_node;
2512 register tree result =
2513 build (CALL_EXPR, value_type,
2514 function, coerced_params, NULL_TREE);
2516 TREE_SIDE_EFFECTS (result) = 1;
2518 if (! require_complete)
2519 return convert_from_reference (result);
2520 if (value_type == void_type_node)
2522 result = require_complete_type (result);
2523 return convert_from_reference (result);
2528 build_function_call (function, params)
2529 tree function, params;
2531 return build_function_call_real (function, params, 1, LOOKUP_NORMAL);
2534 /* Convert the actual parameter expressions in the list VALUES
2535 to the types in the list TYPELIST.
2536 If parmdecls is exhausted, or when an element has NULL as its type,
2537 perform the default conversions.
2539 RETURN_LOC is the location of the return value, if known, NULL_TREE
2540 otherwise. This is useful in the case where we can avoid creating
2541 a temporary variable in the case where we can initialize the return
2542 value directly. If we are not eliding constructors, then we set this
2543 to NULL_TREE to avoid this avoidance.
2545 NAME is an IDENTIFIER_NODE or 0. It is used only for error messages.
2547 This is also where warnings about wrong number of args are generated.
2549 Return a list of expressions for the parameters as converted.
2551 Both VALUES and the returned value are chains of TREE_LIST nodes
2552 with the elements of the list in the TREE_VALUE slots of those nodes.
2554 In C++, unspecified trailing parameters can be filled in with their
2555 default arguments, if such were specified. Do so here. */
2558 convert_arguments (return_loc, typelist, values, fndecl, flags)
2559 tree return_loc, typelist, values, fndecl;
2562 register tree typetail, valtail;
2563 register tree result = NULL_TREE;
2567 if (! flag_elide_constructors)
2572 if (TREE_CODE (TREE_TYPE (fndecl)) == METHOD_TYPE)
2574 if (DECL_NAME (fndecl) == NULL_TREE
2575 || IDENTIFIER_HAS_TYPE_VALUE (DECL_NAME (fndecl)))
2576 called_thing = "constructor";
2578 called_thing = "member function";
2581 called_thing = "function";
2584 for (valtail = values, typetail = typelist;
2586 valtail = TREE_CHAIN (valtail), i++)
2588 register tree type = typetail ? TREE_VALUE (typetail) : 0;
2589 register tree val = TREE_VALUE (valtail);
2591 if (val == error_mark_node)
2592 return error_mark_node;
2594 if (type == void_type_node)
2598 char *buf = (char *)alloca (40 + strlen (called_thing));
2599 sprintf (buf, "too many arguments to %s `%%s'", called_thing);
2600 error_with_decl (fndecl, buf);
2601 error ("at this point in file");
2604 error ("too many arguments to function");
2605 /* In case anybody wants to know if this argument
2608 TREE_TYPE (tree_last (result)) = error_mark_node;
2612 /* The tree type of the parameter being passed may not yet be
2613 known. In this case, its type is TYPE_UNKNOWN, and will
2614 be instantiated by the type given by TYPE. If TYPE
2615 is also NULL, the tree type of VAL is ERROR_MARK_NODE. */
2616 if (type && type_unknown_p (val))
2617 val = require_instantiated_type (type, val, integer_zero_node);
2618 else if (type_unknown_p (val))
2620 /* Strip the `&' from an overloaded FUNCTION_DECL. */
2621 if (TREE_CODE (val) == ADDR_EXPR)
2622 val = TREE_OPERAND (val, 0);
2623 if (TREE_CODE (val) == TREE_LIST
2624 && TREE_CHAIN (val) == NULL_TREE
2625 && TREE_TYPE (TREE_VALUE (val)) != NULL_TREE
2626 && (TREE_TYPE (val) == unknown_type_node
2627 || DECL_CHAIN (TREE_VALUE (val)) == NULL_TREE))
2628 /* Instantiates automatically. */
2629 val = TREE_VALUE (val);
2632 error ("insufficient type information in parameter list");
2633 val = integer_zero_node;
2636 else if (TREE_CODE (val) == OFFSET_REF
2637 && TREE_CODE (TREE_TYPE (val)) == METHOD_TYPE)
2639 /* This is unclean. Should be handled elsewhere. */
2640 val = build_unary_op (ADDR_EXPR, val, 0);
2642 else if (TREE_CODE (val) == OFFSET_REF)
2643 val = resolve_offset_ref (val);
2645 /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
2646 Strip such NOP_EXPRs, since VAL is used in non-lvalue context. */
2647 if (TREE_CODE (val) == NOP_EXPR
2648 && TREE_TYPE (val) == TREE_TYPE (TREE_OPERAND (val, 0))
2649 && (type == 0 || TREE_CODE (type) != REFERENCE_TYPE))
2650 val = TREE_OPERAND (val, 0);
2652 if (type == 0 || TREE_CODE (type) != REFERENCE_TYPE)
2654 if (TREE_CODE (TREE_TYPE (val)) == ARRAY_TYPE
2655 || TREE_CODE (TREE_TYPE (val)) == FUNCTION_TYPE
2656 || TREE_CODE (TREE_TYPE (val)) == METHOD_TYPE)
2657 val = default_conversion (val);
2659 val = require_complete_type (val);
2662 if (val == error_mark_node)
2663 return error_mark_node;
2667 /* Formal parm type is specified by a function prototype. */
2670 if (TYPE_SIZE (type) == 0)
2672 error ("parameter type of called function is incomplete");
2677 parmval = convert_for_initialization (return_loc, type, val,
2678 flags|INDIRECT_BIND,
2679 "argument passing", fndecl, i);
2680 #ifdef PROMOTE_PROTOTYPES
2681 if ((TREE_CODE (type) == INTEGER_TYPE
2682 || TREE_CODE (type) == ENUMERAL_TYPE)
2683 && (TYPE_PRECISION (type) < TYPE_PRECISION (integer_type_node)))
2684 parmval = default_conversion (parmval);
2688 if (parmval == error_mark_node)
2689 return error_mark_node;
2691 result = tree_cons (NULL_TREE, parmval, result);
2695 if (TREE_CODE (TREE_TYPE (val)) == REFERENCE_TYPE)
2696 val = convert_from_reference (val);
2698 if (TREE_CODE (TREE_TYPE (val)) == REAL_TYPE
2699 && (TYPE_PRECISION (TREE_TYPE (val))
2700 < TYPE_PRECISION (double_type_node)))
2701 /* Convert `float' to `double'. */
2702 result = tree_cons (NULL_TREE, convert (double_type_node, val), result);
2703 else if (TYPE_LANG_SPECIFIC (TREE_TYPE (val))
2704 && ! TYPE_HAS_TRIVIAL_INIT_REF (TREE_TYPE (val)))
2706 cp_warning ("cannot pass objects of type `%T' through `...'",
2708 result = tree_cons (NULL_TREE, val, result);
2711 /* Convert `short' and `char' to full-size `int'. */
2712 result = tree_cons (NULL_TREE, default_conversion (val), result);
2716 typetail = TREE_CHAIN (typetail);
2719 if (typetail != 0 && typetail != void_list_node)
2721 /* See if there are default arguments that can be used */
2722 if (TREE_PURPOSE (typetail))
2724 for (; typetail != void_list_node; ++i)
2726 tree type = TREE_VALUE (typetail);
2727 tree val = break_out_target_exprs (TREE_PURPOSE (typetail));
2730 if (val == NULL_TREE)
2731 parmval = error_mark_node;
2732 else if (TREE_CODE (val) == CONSTRUCTOR)
2734 parmval = digest_init (type, val, (tree *)0);
2735 parmval = convert_for_initialization (return_loc, type, parmval, flags,
2736 "default constructor", fndecl, i);
2740 /* This could get clobbered by the following call. */
2741 if (TREE_HAS_CONSTRUCTOR (val))
2742 val = copy_node (val);
2744 parmval = convert_for_initialization (return_loc, type, val, flags,
2745 "default argument", fndecl, i);
2746 #ifdef PROMOTE_PROTOTYPES
2747 if ((TREE_CODE (type) == INTEGER_TYPE
2748 || TREE_CODE (type) == ENUMERAL_TYPE)
2749 && (TYPE_PRECISION (type) < TYPE_PRECISION (integer_type_node)))
2750 parmval = default_conversion (parmval);
2754 if (parmval == error_mark_node)
2755 return error_mark_node;
2757 result = tree_cons (0, parmval, result);
2758 typetail = TREE_CHAIN (typetail);
2759 /* ends with `...'. */
2760 if (typetail == NULL_TREE)
2768 char *buf = (char *)alloca (32 + strlen (called_thing));
2769 sprintf (buf, "too few arguments to %s `%%#D'", called_thing);
2770 cp_error_at (buf, fndecl);
2771 error ("at this point in file");
2774 error ("too few arguments to function");
2775 return error_mark_list;
2779 return nreverse (result);
2782 /* Build a binary-operation expression, after performing default
2783 conversions on the operands. CODE is the kind of expression to build. */
2786 build_x_binary_op (code, arg1, arg2)
2787 enum tree_code code;
2790 tree rval = build_opfncall (code, LOOKUP_SPECULATIVELY,
2791 arg1, arg2, NULL_TREE);
2793 return build_opfncall (code, LOOKUP_NORMAL, arg1, arg2, NULL_TREE);
2794 if (code == MEMBER_REF)
2795 return build_m_component_ref (build_indirect_ref (arg1, NULL_PTR),
2797 return build_binary_op (code, arg1, arg2, 1);
2801 build_binary_op (code, arg1, arg2, convert_p)
2802 enum tree_code code;
2815 args[0] = decay_conversion (args[0]);
2816 args[1] = decay_conversion (args[1]);
2818 if (args[0] == error_mark_node || args[1] == error_mark_node)
2819 return error_mark_node;
2821 type0 = TREE_TYPE (args[0]);
2822 type1 = TREE_TYPE (args[1]);
2824 if (type_unknown_p (args[0]))
2826 args[0] = instantiate_type (type1, args[0], 1);
2827 args[0] = decay_conversion (args[0]);
2829 else if (type_unknown_p (args[1]))
2831 args[1] = require_instantiated_type (type0, args[1],
2833 args[1] = decay_conversion (args[1]);
2836 if (IS_AGGR_TYPE (type0) || IS_AGGR_TYPE (type1))
2838 /* Try to convert this to something reasonable. */
2839 if (! build_default_binary_type_conversion(code, &args[0], &args[1]))
2841 cp_error ("no match for `%O(%#T, %#T)'", code,
2842 TREE_TYPE (arg1), TREE_TYPE (arg2));
2843 return error_mark_node;
2847 return build_binary_op_nodefault (code, args[0], args[1], code);
2850 /* Build a binary-operation expression without default conversions.
2851 CODE is the kind of expression to build.
2852 This function differs from `build' in several ways:
2853 the data type of the result is computed and recorded in it,
2854 warnings are generated if arg data types are invalid,
2855 special handling for addition and subtraction of pointers is known,
2856 and some optimization is done (operations on narrow ints
2857 are done in the narrower type when that gives the same result).
2858 Constant folding is also done before the result is returned.
2860 ERROR_CODE is the code that determines what to say in error messages.
2861 It is usually, but not always, the same as CODE.
2863 Note that the operands will never have enumeral types
2864 because either they have just had the default conversions performed
2865 or they have both just been converted to some other type in which
2866 the arithmetic is to be done.
2868 C++: must do special pointer arithmetic when implementing
2869 multiple inheritance, and deal with pointer to member functions. */
2872 build_binary_op_nodefault (code, orig_op0, orig_op1, error_code)
2873 enum tree_code code;
2874 tree orig_op0, orig_op1;
2875 enum tree_code error_code;
2878 register enum tree_code code0, code1;
2881 /* Expression code to give to the expression when it is built.
2882 Normally this is CODE, which is what the caller asked for,
2883 but in some special cases we change it. */
2884 register enum tree_code resultcode = code;
2886 /* Data type in which the computation is to be performed.
2887 In the simplest cases this is the common type of the arguments. */
2888 register tree result_type = NULL;
2890 /* Nonzero means operands have already been type-converted
2891 in whatever way is necessary.
2892 Zero means they need to be converted to RESULT_TYPE. */
2895 /* Nonzero means create the expression with this type, rather than
2897 tree build_type = 0;
2899 /* Nonzero means after finally constructing the expression
2900 convert it to this type. */
2901 tree final_type = 0;
2903 /* Nonzero if this is an operation like MIN or MAX which can
2904 safely be computed in short if both args are promoted shorts.
2905 Also implies COMMON.
2906 -1 indicates a bitwise operation; this makes a difference
2907 in the exact conditions for when it is safe to do the operation
2908 in a narrower mode. */
2911 /* Nonzero if this is a comparison operation;
2912 if both args are promoted shorts, compare the original shorts.
2913 Also implies COMMON. */
2914 int short_compare = 0;
2916 /* Nonzero if this is a right-shift operation, which can be computed on the
2917 original short and then promoted if the operand is a promoted short. */
2918 int short_shift = 0;
2920 /* Nonzero means set RESULT_TYPE to the common type of the args. */
2923 /* Apply default conversions. */
2924 if (code == TRUTH_AND_EXPR || code == TRUTH_ANDIF_EXPR
2925 || code == TRUTH_OR_EXPR || code == TRUTH_ORIF_EXPR
2926 || code == TRUTH_XOR_EXPR)
2928 op0 = decay_conversion (orig_op0);
2929 op1 = decay_conversion (orig_op1);
2933 op0 = default_conversion (orig_op0);
2934 op1 = default_conversion (orig_op1);
2937 type0 = TREE_TYPE (op0);
2938 type1 = TREE_TYPE (op1);
2940 /* The expression codes of the data types of the arguments tell us
2941 whether the arguments are integers, floating, pointers, etc. */
2942 code0 = TREE_CODE (type0);
2943 code1 = TREE_CODE (type1);
2945 /* Strip NON_LVALUE_EXPRs, etc., since we aren't using as an lvalue. */
2946 STRIP_TYPE_NOPS (op0);
2947 STRIP_TYPE_NOPS (op1);
2949 /* If an error was already reported for one of the arguments,
2950 avoid reporting another error. */
2952 if (code0 == ERROR_MARK || code1 == ERROR_MARK)
2953 return error_mark_node;
2958 /* Handle the pointer + int case. */
2959 if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
2960 return pointer_int_sum (PLUS_EXPR, op0, op1);
2961 else if (code1 == POINTER_TYPE && code0 == INTEGER_TYPE)
2962 return pointer_int_sum (PLUS_EXPR, op1, op0);
2968 /* Subtraction of two similar pointers.
2969 We must subtract them as integers, then divide by object size. */
2970 if (code0 == POINTER_TYPE && code1 == POINTER_TYPE
2971 && comp_target_types (type0, type1, 1))
2972 return pointer_diff (op0, op1);
2973 /* Handle pointer minus int. Just like pointer plus int. */
2974 else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
2975 return pointer_int_sum (MINUS_EXPR, op0, op1);
2984 case TRUNC_DIV_EXPR:
2986 case FLOOR_DIV_EXPR:
2987 case ROUND_DIV_EXPR:
2988 case EXACT_DIV_EXPR:
2989 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE)
2990 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE))
2992 if (TREE_CODE (op1) == INTEGER_CST && integer_zerop (op1))
2993 cp_warning ("division by zero in `%E / 0'", op0);
2994 else if (TREE_CODE (op1) == REAL_CST && real_zerop (op1))
2995 cp_warning ("division by zero in `%E / 0.'", op0);
2997 if (!(code0 == INTEGER_TYPE && code1 == INTEGER_TYPE))
2998 resultcode = RDIV_EXPR;
3000 /* When dividing two signed integers, we have to promote to int.
3001 unless we divide by a constant != -1. Note that default
3002 conversion will have been performed on the operands at this
3003 point, so we have to dig out the original type to find out if
3005 shorten = ((TREE_CODE (op0) == NOP_EXPR
3006 && TREE_UNSIGNED (TREE_TYPE (TREE_OPERAND (op0, 0))))
3007 || (TREE_CODE (op1) == INTEGER_CST
3008 && (TREE_INT_CST_LOW (op1) != -1
3009 || TREE_INT_CST_HIGH (op1) != -1)));
3015 case BIT_ANDTC_EXPR:
3018 if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
3020 /* If one operand is a constant, and the other is a short type
3021 that has been converted to an int,
3022 really do the work in the short type and then convert the
3023 result to int. If we are lucky, the constant will be 0 or 1
3024 in the short type, making the entire operation go away. */
3025 if (TREE_CODE (op0) == INTEGER_CST
3026 && TREE_CODE (op1) == NOP_EXPR
3027 && TYPE_PRECISION (type1) > TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (op1, 0)))
3028 && TREE_UNSIGNED (TREE_TYPE (TREE_OPERAND (op1, 0))))
3030 final_type = result_type;
3031 op1 = TREE_OPERAND (op1, 0);
3032 result_type = TREE_TYPE (op1);
3034 if (TREE_CODE (op1) == INTEGER_CST
3035 && TREE_CODE (op0) == NOP_EXPR
3036 && TYPE_PRECISION (type0) > TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (op0, 0)))
3037 && TREE_UNSIGNED (TREE_TYPE (TREE_OPERAND (op0, 0))))
3039 final_type = result_type;
3040 op0 = TREE_OPERAND (op0, 0);
3041 result_type = TREE_TYPE (op0);
3045 case TRUNC_MOD_EXPR:
3046 case FLOOR_MOD_EXPR:
3047 if (code1 == INTEGER_TYPE && integer_zerop (op1))
3048 cp_warning ("division by zero in `%E % 0'", op0);
3049 else if (code1 == REAL_TYPE && real_zerop (op1))
3050 cp_warning ("division by zero in `%E % 0.'", op0);
3052 if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
3054 /* Although it would be tempting to shorten always here, that loses
3055 on some targets, since the modulo instruction is undefined if the
3056 quotient can't be represented in the computation mode. We shorten
3057 only if unsigned or if dividing by something we know != -1. */
3058 shorten = ((TREE_CODE (op0) == NOP_EXPR
3059 && TREE_UNSIGNED (TREE_TYPE (TREE_OPERAND (op0, 0))))
3060 || (TREE_CODE (op1) == INTEGER_CST
3061 && (TREE_INT_CST_LOW (op1) != -1
3062 || TREE_INT_CST_HIGH (op1) != -1)));
3067 case TRUTH_ANDIF_EXPR:
3068 case TRUTH_ORIF_EXPR:
3069 case TRUTH_AND_EXPR:
3071 result_type = boolean_type_node;
3074 /* Shift operations: result has same type as first operand;
3075 always convert second operand to int.
3076 Also set SHORT_SHIFT if shifting rightward. */
3079 if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
3081 result_type = type0;
3082 if (TREE_CODE (op1) == INTEGER_CST)
3084 if (tree_int_cst_lt (op1, integer_zero_node))
3085 warning ("right shift count is negative");
3088 if (TREE_INT_CST_LOW (op1) | TREE_INT_CST_HIGH (op1))
3090 if (TREE_INT_CST_HIGH (op1) != 0
3091 || ((unsigned HOST_WIDE_INT) TREE_INT_CST_LOW (op1)
3092 >= TYPE_PRECISION (type0)))
3093 warning ("right shift count >= width of type");
3096 /* Convert the shift-count to an integer, regardless of
3097 size of value being shifted. */
3098 if (TYPE_MAIN_VARIANT (TREE_TYPE (op1)) != integer_type_node)
3099 op1 = convert (integer_type_node, op1);
3100 /* Avoid converting op1 to result_type later. */
3106 if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
3108 result_type = type0;
3109 if (TREE_CODE (op1) == INTEGER_CST)
3111 if (tree_int_cst_lt (op1, integer_zero_node))
3112 warning ("left shift count is negative");
3113 else if (TREE_INT_CST_HIGH (op1) != 0
3114 || ((unsigned HOST_WIDE_INT) TREE_INT_CST_LOW (op1)
3115 >= TYPE_PRECISION (type0)))
3116 warning ("left shift count >= width of type");
3118 /* Convert the shift-count to an integer, regardless of
3119 size of value being shifted. */
3120 if (TYPE_MAIN_VARIANT (TREE_TYPE (op1)) != integer_type_node)
3121 op1 = convert (integer_type_node, op1);
3122 /* Avoid converting op1 to result_type later. */
3129 if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
3131 result_type = type0;
3132 if (TREE_CODE (op1) == INTEGER_CST)
3134 if (tree_int_cst_lt (op1, integer_zero_node))
3135 warning ("%s rotate count is negative",
3136 (code == LROTATE_EXPR) ? "left" : "right");
3137 else if (TREE_INT_CST_HIGH (op1) != 0
3138 || ((unsigned HOST_WIDE_INT) TREE_INT_CST_LOW (op1)
3139 >= TYPE_PRECISION (type0)))
3140 warning ("%s rotate count >= width of type",
3141 (code == LROTATE_EXPR) ? "left" : "right");
3143 /* Convert the shift-count to an integer, regardless of
3144 size of value being shifted. */
3145 if (TYPE_MAIN_VARIANT (TREE_TYPE (op1)) != integer_type_node)
3146 op1 = convert (integer_type_node, op1);
3152 build_type = boolean_type_node;
3153 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE)
3154 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE))
3156 else if (code0 == POINTER_TYPE && code1 == POINTER_TYPE)
3158 register tree tt0 = TYPE_MAIN_VARIANT (TREE_TYPE (type0));
3159 register tree tt1 = TYPE_MAIN_VARIANT (TREE_TYPE (type1));
3161 if (comp_target_types (type0, type1, 1))
3162 result_type = common_type (type0, type1);
3163 else if (tt0 == void_type_node)
3165 if (pedantic && TREE_CODE (tt1) == FUNCTION_TYPE
3166 && tree_int_cst_lt (TYPE_SIZE (type0), TYPE_SIZE (type1)))
3167 pedwarn ("ANSI C++ forbids comparison of `void *' with function pointer");
3168 else if (TREE_CODE (tt1) == OFFSET_TYPE)
3169 pedwarn ("ANSI C++ forbids conversion of a pointer to member to `void *'");
3171 else if (tt1 == void_type_node)
3173 if (pedantic && TREE_CODE (tt0) == FUNCTION_TYPE
3174 && tree_int_cst_lt (TYPE_SIZE (type1), TYPE_SIZE (type0)))
3175 pedwarn ("ANSI C++ forbids comparison of `void *' with function pointer");
3178 cp_pedwarn ("comparison of distinct pointer types `%T' and `%T' lacks a cast",
3181 if (result_type == NULL_TREE)
3182 result_type = ptr_type_node;
3184 else if (code0 == POINTER_TYPE && TREE_CODE (op1) == INTEGER_CST
3185 && integer_zerop (op1))
3186 result_type = type0;
3187 else if (code1 == POINTER_TYPE && TREE_CODE (op0) == INTEGER_CST
3188 && integer_zerop (op0))
3189 result_type = type1;
3190 else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
3192 result_type = type0;
3193 error ("ANSI C++ forbids comparison between pointer and integer");
3195 else if (code0 == INTEGER_TYPE && code1 == POINTER_TYPE)
3197 result_type = type1;
3198 error ("ANSI C++ forbids comparison between pointer and integer");
3200 else if (TYPE_PTRMEMFUNC_P (type0) && TREE_CODE (op1) == INTEGER_CST
3201 && integer_zerop (op1))
3203 op0 = build_component_ref (op0, index_identifier, 0, 0);
3204 op1 = integer_zero_node;
3205 result_type = TREE_TYPE (op0);
3207 else if (TYPE_PTRMEMFUNC_P (type1) && TREE_CODE (op0) == INTEGER_CST
3208 && integer_zerop (op0))
3210 op0 = build_component_ref (op1, index_identifier, 0, 0);
3211 op1 = integer_zero_node;
3212 result_type = TREE_TYPE (op0);
3214 else if (TYPE_PTRMEMFUNC_P (type0) && TYPE_PTRMEMFUNC_P (type1)
3215 && (TYPE_PTRMEMFUNC_FN_TYPE (type0)
3216 == TYPE_PTRMEMFUNC_FN_TYPE (type1)))
3218 /* The code we generate for the test is:
3220 (op0.index == op1.index
3221 && ((op1.index != -1 && op0.delta2 == op1.delta2)
3222 || op0.pfn == op1.pfn)) */
3224 tree index0 = build_component_ref (op0, index_identifier, 0, 0);
3225 tree index1 = save_expr (build_component_ref (op1, index_identifier, 0, 0));
3226 tree pfn0 = PFN_FROM_PTRMEMFUNC (op0);
3227 tree pfn1 = PFN_FROM_PTRMEMFUNC (op1);
3228 tree delta20 = DELTA2_FROM_PTRMEMFUNC (op0);
3229 tree delta21 = DELTA2_FROM_PTRMEMFUNC (op1);
3231 tree integer_neg_one_node
3232 = build_binary_op (MINUS_EXPR, integer_zero_node, integer_one_node, 1);
3233 e1 = build_binary_op (EQ_EXPR, index0, index1, 1);
3234 e2 = build_binary_op (NE_EXPR, index1, integer_neg_one_node, 1);
3235 e2 = build_binary_op (TRUTH_ANDIF_EXPR, e2, build_binary_op (EQ_EXPR, delta20, delta21, 1), 1);
3236 e3 = build_binary_op (EQ_EXPR, pfn0, pfn1, 1);
3237 e2 = build_binary_op (TRUTH_ORIF_EXPR, e2, e3, 1);
3238 e2 = build_binary_op (TRUTH_ANDIF_EXPR, e1, e2, 1);
3239 if (code == EQ_EXPR)
3241 return build_binary_op (EQ_EXPR, e2, integer_zero_node, 1);
3243 else if (TYPE_PTRMEMFUNC_P (type0)
3244 && TYPE_PTRMEMFUNC_FN_TYPE (type0) == type1)
3246 tree index0 = build_component_ref (op0, index_identifier, 0, 0);
3248 tree pfn0 = PFN_FROM_PTRMEMFUNC (op0);
3249 tree delta20 = DELTA2_FROM_PTRMEMFUNC (op0);
3250 tree delta21 = integer_zero_node;
3252 tree integer_neg_one_node
3253 = build_binary_op (MINUS_EXPR, integer_zero_node, integer_one_node, 1);
3254 if (TREE_CODE (TREE_OPERAND (op1, 0)) == FUNCTION_DECL
3255 && DECL_VINDEX (TREE_OPERAND (op1, 0)))
3257 /* Map everything down one to make room for the null pointer to member. */
3258 index1 = size_binop (PLUS_EXPR,
3259 DECL_VINDEX (TREE_OPERAND (op1, 0)),
3261 op1 = integer_zero_node;
3262 delta21 = CLASSTYPE_VFIELD (TYPE_METHOD_BASETYPE (TREE_TYPE (type1)));
3263 delta21 = DECL_FIELD_BITPOS (delta21);
3264 delta21 = size_binop (FLOOR_DIV_EXPR, delta21, size_int (BITS_PER_UNIT));
3267 index1 = integer_neg_one_node;
3269 tree nop1 = build1 (NOP_EXPR, TYPE_PTRMEMFUNC_FN_TYPE (type0), op1);
3270 TREE_CONSTANT (nop1) = TREE_CONSTANT (op1);
3273 e1 = build_binary_op (EQ_EXPR, index0, index1, 1);
3274 e2 = build_binary_op (NE_EXPR, index1, integer_neg_one_node, 1);
3275 e2 = build_binary_op (TRUTH_ANDIF_EXPR, e2, build_binary_op (EQ_EXPR, delta20, delta21, 1), 1);
3276 e3 = build_binary_op (EQ_EXPR, pfn0, op1, 1);
3277 e2 = build_binary_op (TRUTH_ORIF_EXPR, e2, e3, 1);
3278 e2 = build_binary_op (TRUTH_ANDIF_EXPR, e1, e2, 1);
3279 if (code == EQ_EXPR)
3281 return build_binary_op (EQ_EXPR, e2, integer_zero_node, 1);
3283 else if (TYPE_PTRMEMFUNC_P (type1)
3284 && TYPE_PTRMEMFUNC_FN_TYPE (type1) == type0)
3286 return build_binary_op (code, op1, op0, 1);
3292 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE)
3293 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE))
3295 else if (code0 == POINTER_TYPE && code1 == POINTER_TYPE)
3297 if (comp_target_types (type0, type1, 1))
3298 result_type = common_type (type0, type1);
3301 cp_pedwarn ("comparison of distinct pointer types `%T' and `%T' lacks a cast",
3303 result_type = ptr_type_node;
3312 build_type = boolean_type_node;
3313 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE)
3314 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE))
3316 else if (code0 == POINTER_TYPE && code1 == POINTER_TYPE)
3318 if (comp_target_types (type0, type1, 1))
3319 result_type = common_type (type0, type1);
3322 cp_pedwarn ("comparison of distinct pointer types `%T' and `%T' lacks a cast",
3324 result_type = ptr_type_node;
3327 else if (code0 == POINTER_TYPE && TREE_CODE (op1) == INTEGER_CST
3328 && integer_zerop (op1))
3329 result_type = type0;
3330 else if (code1 == POINTER_TYPE && TREE_CODE (op0) == INTEGER_CST
3331 && integer_zerop (op0))
3332 result_type = type1;
3333 else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
3335 result_type = type0;
3337 pedwarn ("ANSI C++ forbids comparison between pointer and integer");
3338 else if (! flag_traditional)
3339 warning ("comparison between pointer and integer");
3341 else if (code0 == INTEGER_TYPE && code1 == POINTER_TYPE)
3343 result_type = type1;
3345 pedwarn ("ANSI C++ forbids comparison between pointer and integer");
3346 else if (! flag_traditional)
3347 warning ("comparison between pointer and integer");
3352 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE)
3353 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE))
3355 if (shorten || common || short_compare)
3356 result_type = common_type (type0, type1);
3358 /* For certain operations (which identify themselves by shorten != 0)
3359 if both args were extended from the same smaller type,
3360 do the arithmetic in that type and then extend.
3362 shorten !=0 and !=1 indicates a bitwise operation.
3363 For them, this optimization is safe only if
3364 both args are zero-extended or both are sign-extended.
3365 Otherwise, we might change the result.
3366 Eg, (short)-1 | (unsigned short)-1 is (int)-1
3367 but calculated in (unsigned short) it would be (unsigned short)-1. */
3371 int unsigned0, unsigned1;
3372 tree arg0 = get_narrower (op0, &unsigned0);
3373 tree arg1 = get_narrower (op1, &unsigned1);
3374 /* UNS is 1 if the operation to be done is an unsigned one. */
3375 int uns = TREE_UNSIGNED (result_type);
3378 final_type = result_type;
3380 /* Handle the case that OP0 does not *contain* a conversion
3381 but it *requires* conversion to FINAL_TYPE. */
3383 if (op0 == arg0 && TREE_TYPE (op0) != final_type)
3384 unsigned0 = TREE_UNSIGNED (TREE_TYPE (op0));
3385 if (op1 == arg1 && TREE_TYPE (op1) != final_type)
3386 unsigned1 = TREE_UNSIGNED (TREE_TYPE (op1));
3388 /* Now UNSIGNED0 is 1 if ARG0 zero-extends to FINAL_TYPE. */
3390 /* For bitwise operations, signedness of nominal type
3391 does not matter. Consider only how operands were extended. */
3395 /* Note that in all three cases below we refrain from optimizing
3396 an unsigned operation on sign-extended args.
3397 That would not be valid. */
3399 /* Both args variable: if both extended in same way
3400 from same width, do it in that width.
3401 Do it unsigned if args were zero-extended. */
3402 if ((TYPE_PRECISION (TREE_TYPE (arg0))
3403 < TYPE_PRECISION (result_type))
3404 && (TYPE_PRECISION (TREE_TYPE (arg1))
3405 == TYPE_PRECISION (TREE_TYPE (arg0)))
3406 && unsigned0 == unsigned1
3407 && (unsigned0 || !uns))
3409 = signed_or_unsigned_type (unsigned0,
3410 common_type (TREE_TYPE (arg0), TREE_TYPE (arg1)));
3411 else if (TREE_CODE (arg0) == INTEGER_CST
3412 && (unsigned1 || !uns)
3413 && (TYPE_PRECISION (TREE_TYPE (arg1))
3414 < TYPE_PRECISION (result_type))
3415 && (type = signed_or_unsigned_type (unsigned1,
3417 int_fits_type_p (arg0, type)))
3419 else if (TREE_CODE (arg1) == INTEGER_CST
3420 && (unsigned0 || !uns)
3421 && (TYPE_PRECISION (TREE_TYPE (arg0))
3422 < TYPE_PRECISION (result_type))
3423 && (type = signed_or_unsigned_type (unsigned0,
3425 int_fits_type_p (arg1, type)))
3429 /* Shifts can be shortened if shifting right. */
3434 tree arg0 = get_narrower (op0, &unsigned_arg);
3436 final_type = result_type;
3438 if (arg0 == op0 && final_type == TREE_TYPE (op0))
3439 unsigned_arg = TREE_UNSIGNED (TREE_TYPE (op0));
3441 if (TYPE_PRECISION (TREE_TYPE (arg0)) < TYPE_PRECISION (result_type)
3442 /* We can shorten only if the shift count is less than the
3443 number of bits in the smaller type size. */
3444 && TREE_INT_CST_HIGH (op1) == 0
3445 && TYPE_PRECISION (TREE_TYPE (arg0)) > TREE_INT_CST_LOW (op1)
3446 /* If arg is sign-extended and then unsigned-shifted,
3447 we can simulate this with a signed shift in arg's type
3448 only if the extended result is at least twice as wide
3449 as the arg. Otherwise, the shift could use up all the
3450 ones made by sign-extension and bring in zeros.
3451 We can't optimize that case at all, but in most machines
3452 it never happens because available widths are 2**N. */
3453 && (!TREE_UNSIGNED (final_type)
3455 || ((unsigned) 2 * TYPE_PRECISION (TREE_TYPE (arg0))
3456 <= TYPE_PRECISION (result_type))))
3458 /* Do an unsigned shift if the operand was zero-extended. */
3460 = signed_or_unsigned_type (unsigned_arg,
3462 /* Convert value-to-be-shifted to that type. */
3463 if (TREE_TYPE (op0) != result_type)
3464 op0 = convert (result_type, op0);
3469 /* Comparison operations are shortened too but differently.
3470 They identify themselves by setting short_compare = 1. */
3474 /* Don't write &op0, etc., because that would prevent op0
3475 from being kept in a register.
3476 Instead, make copies of the our local variables and
3477 pass the copies by reference, then copy them back afterward. */
3478 tree xop0 = op0, xop1 = op1, xresult_type = result_type;
3479 enum tree_code xresultcode = resultcode;
3481 = shorten_compare (&xop0, &xop1, &xresult_type, &xresultcode);
3483 return convert (boolean_type_node, val);
3484 op0 = xop0, op1 = xop1;
3486 resultcode = xresultcode;
3489 if (short_compare && extra_warnings)
3491 int op0_signed = ! TREE_UNSIGNED (TREE_TYPE (orig_op0));
3492 int op1_signed = ! TREE_UNSIGNED (TREE_TYPE (orig_op1));
3494 int unsignedp0, unsignedp1;
3495 tree primop0 = get_narrower (op0, &unsignedp0);
3496 tree primop1 = get_narrower (op1, &unsignedp1);
3498 /* Check for comparison of different enum types. */
3499 if (flag_int_enum_equivalence == 0
3500 && TREE_CODE (TREE_TYPE (orig_op0)) == ENUMERAL_TYPE
3501 && TREE_CODE (TREE_TYPE (orig_op1)) == ENUMERAL_TYPE
3502 && TYPE_MAIN_VARIANT (TREE_TYPE (orig_op0))
3503 != TYPE_MAIN_VARIANT (TREE_TYPE (orig_op1)))
3505 cp_warning ("comparison between `%#T' and `%#T'",
3506 TREE_TYPE (orig_op0), TREE_TYPE (orig_op1));
3509 /* Give warnings for comparisons between signed and unsigned
3510 quantities that may fail. */
3511 /* Do the checking based on the original operand trees, so that
3512 casts will be considered, but default promotions won't be. */
3514 /* Do not warn if the comparison is being done in a signed type,
3515 since the signed type will only be chosen if it can represent
3516 all the values of the unsigned type. */
3517 if (! TREE_UNSIGNED (result_type))
3519 /* Do not warn if both operands are unsigned. */
3520 else if (op0_signed == op1_signed)
3522 /* Do not warn if the signed quantity is an unsuffixed
3523 integer literal (or some static constant expression
3524 involving such literals) and it is non-negative. */
3525 else if ((op0_signed && TREE_CODE (orig_op0) == INTEGER_CST
3526 && tree_int_cst_sgn (orig_op0) >= 0)
3527 || (op1_signed && TREE_CODE (orig_op1) == INTEGER_CST
3528 && tree_int_cst_sgn (orig_op1) >= 0))
3530 /* Do not warn if the comparison is an equality operation,
3531 the unsigned quantity is an integral constant and it does
3532 not use the most significant bit of result_type. */
3533 else if ((resultcode == EQ_EXPR || resultcode == NE_EXPR)
3534 && ((op0_signed && TREE_CODE (orig_op1) == INTEGER_CST
3535 && int_fits_type_p (orig_op1, signed_type (result_type))
3536 || (op1_signed && TREE_CODE (orig_op0) == INTEGER_CST
3537 && int_fits_type_p (orig_op0, signed_type (result_type))))))
3540 warning ("comparison between signed and unsigned");
3542 /* Warn if two unsigned values are being compared in a size
3543 larger than their original size, and one (and only one) is the
3544 result of a `~' operator. This comparison will always fail.
3546 Also warn if one operand is a constant, and the constant does not
3547 have all bits set that are set in the ~ operand when it is
3550 if (TREE_CODE (primop0) == BIT_NOT_EXPR
3551 ^ TREE_CODE (primop1) == BIT_NOT_EXPR)
3553 if (TREE_CODE (primop0) == BIT_NOT_EXPR)
3554 primop0 = get_narrower (TREE_OPERAND (op0, 0), &unsignedp0);
3555 if (TREE_CODE (primop1) == BIT_NOT_EXPR)
3556 primop1 = get_narrower (TREE_OPERAND (op1, 0), &unsignedp1);
3558 if (TREE_CODE (primop0) == INTEGER_CST
3559 || TREE_CODE (primop1) == INTEGER_CST)
3562 HOST_WIDE_INT constant, mask;
3566 if (TREE_CODE (primop0) == INTEGER_CST)
3569 unsignedp = unsignedp1;
3570 constant = TREE_INT_CST_LOW (primop0);
3575 unsignedp = unsignedp0;
3576 constant = TREE_INT_CST_LOW (primop1);
3579 bits = TYPE_PRECISION (TREE_TYPE (primop));
3580 if (bits < TYPE_PRECISION (result_type)
3581 && bits < HOST_BITS_PER_LONG && unsignedp)
3583 mask = (~ (HOST_WIDE_INT) 0) << bits;
3584 if ((mask & constant) != mask)
3585 warning ("comparison of promoted ~unsigned with constant");
3588 else if (unsignedp0 && unsignedp1
3589 && (TYPE_PRECISION (TREE_TYPE (primop0))
3590 < TYPE_PRECISION (result_type))
3591 && (TYPE_PRECISION (TREE_TYPE (primop1))
3592 < TYPE_PRECISION (result_type)))
3593 warning ("comparison of promoted ~unsigned with unsigned");
3598 /* At this point, RESULT_TYPE must be nonzero to avoid an error message.
3599 If CONVERTED is zero, both args will be converted to type RESULT_TYPE.
3600 Then the expression will be built.
3601 It will be given type FINAL_TYPE if that is nonzero;
3602 otherwise, it will be given type RESULT_TYPE. */
3606 cp_error ("invalid operands `%T' and `%T' to binary `%O'",
3607 TREE_TYPE (orig_op0), TREE_TYPE (orig_op1), error_code);
3608 return error_mark_node;
3613 if (TREE_TYPE (op0) != result_type)
3614 op0 = convert (result_type, op0);
3615 if (TREE_TYPE (op1) != result_type)
3616 op1 = convert (result_type, op1);
3619 if (build_type == NULL_TREE)
3620 build_type = result_type;
3623 register tree result = build (resultcode, build_type, op0, op1);
3624 register tree folded;
3626 folded = fold (result);
3627 if (folded == result)
3628 TREE_CONSTANT (folded) = TREE_CONSTANT (op0) & TREE_CONSTANT (op1);
3629 if (final_type != 0)
3630 return convert (final_type, folded);
3635 /* Return a tree for the sum or difference (RESULTCODE says which)
3636 of pointer PTROP and integer INTOP. */
3639 pointer_int_sum (resultcode, ptrop, intop)
3640 enum tree_code resultcode;
3641 register tree ptrop, intop;
3645 register tree result;
3646 register tree folded = fold (intop);
3648 /* The result is a pointer of the same type that is being added. */
3650 register tree result_type = TREE_TYPE (ptrop);
3652 if (TREE_CODE (TREE_TYPE (result_type)) == VOID_TYPE)
3654 if (pedantic || warn_pointer_arith)
3655 pedwarn ("ANSI C++ forbids using pointer of type `void *' in arithmetic");
3656 size_exp = integer_one_node;
3658 else if (TREE_CODE (TREE_TYPE (result_type)) == FUNCTION_TYPE)
3660 if (pedantic || warn_pointer_arith)
3661 pedwarn ("ANSI C++ forbids using pointer to a function in arithmetic");
3662 size_exp = integer_one_node;
3664 else if (TREE_CODE (TREE_TYPE (result_type)) == METHOD_TYPE)
3666 if (pedantic || warn_pointer_arith)
3667 pedwarn ("ANSI C++ forbids using pointer to a method in arithmetic");
3668 size_exp = integer_one_node;
3670 else if (TREE_CODE (TREE_TYPE (result_type)) == OFFSET_TYPE)
3672 if (pedantic || warn_pointer_arith)
3673 pedwarn ("ANSI C++ forbids using pointer to a member in arithmetic");
3674 size_exp = integer_one_node;
3677 size_exp = size_in_bytes (TREE_TYPE (result_type));
3679 /* Needed to make OOPS V2R3 work. */
3681 if (TREE_CODE (intop) == INTEGER_CST
3682 && TREE_INT_CST_LOW (intop) == 0
3683 && TREE_INT_CST_HIGH (intop) == 0)
3686 /* If what we are about to multiply by the size of the elements
3687 contains a constant term, apply distributive law
3688 and multiply that constant term separately.
3689 This helps produce common subexpressions. */
3691 if ((TREE_CODE (intop) == PLUS_EXPR || TREE_CODE (intop) == MINUS_EXPR)
3692 && ! TREE_CONSTANT (intop)
3693 && TREE_CONSTANT (TREE_OPERAND (intop, 1))
3694 && TREE_CONSTANT (size_exp))
3696 enum tree_code subcode = resultcode;
3697 if (TREE_CODE (intop) == MINUS_EXPR)
3698 subcode = (subcode == PLUS_EXPR ? MINUS_EXPR : PLUS_EXPR);
3699 ptrop = build_binary_op (subcode, ptrop, TREE_OPERAND (intop, 1), 1);
3700 intop = TREE_OPERAND (intop, 0);
3703 /* Convert the integer argument to a type the same size as sizetype
3704 so the multiply won't overflow spuriously. */
3706 if (TYPE_PRECISION (TREE_TYPE (intop)) != TYPE_PRECISION (sizetype))
3707 intop = convert (type_for_size (TYPE_PRECISION (sizetype), 0), intop);
3709 /* Replace the integer argument with a suitable product by the object size.
3710 Do this multiplication as signed, then convert to the appropriate
3711 pointer type (actually unsigned integral). */
3713 intop = convert (result_type,
3714 build_binary_op (MULT_EXPR, intop,
3715 convert (TREE_TYPE (intop), size_exp), 1));
3717 /* Create the sum or difference. */
3719 result = build (resultcode, result_type, ptrop, intop);
3721 folded = fold (result);
3722 if (folded == result)
3723 TREE_CONSTANT (folded) = TREE_CONSTANT (ptrop) & TREE_CONSTANT (intop);
3727 /* Return a tree for the difference of pointers OP0 and OP1.
3728 The resulting tree has type int. */
3731 pointer_diff (op0, op1)
3732 register tree op0, op1;
3734 register tree result, folded;
3735 tree restype = ptrdiff_type_node;
3736 tree target_type = TREE_TYPE (TREE_TYPE (op0));
3738 if (pedantic || warn_pointer_arith)
3740 if (TREE_CODE (target_type) == VOID_TYPE)
3741 pedwarn ("ANSI C++ forbids using pointer of type `void *' in subtraction");
3742 if (TREE_CODE (target_type) == FUNCTION_TYPE)
3743 pedwarn ("ANSI C++ forbids using pointer to a function in subtraction");
3744 if (TREE_CODE (target_type) == METHOD_TYPE)
3745 pedwarn ("ANSI C++ forbids using pointer to a method in subtraction");
3746 if (TREE_CODE (target_type) == OFFSET_TYPE)
3747 pedwarn ("ANSI C++ forbids using pointer to a member in subtraction");
3750 /* First do the subtraction as integers;
3751 then drop through to build the divide operator. */
3753 op0 = build_binary_op (MINUS_EXPR,
3754 convert (restype, op0), convert (restype, op1), 1);
3756 /* This generates an error if op1 is a pointer to an incomplete type. */
3757 if (TYPE_SIZE (TREE_TYPE (TREE_TYPE (op1))) == 0)
3758 error ("arithmetic on pointer to an incomplete type");
3760 op1 = ((TREE_CODE (target_type) == VOID_TYPE
3761 || TREE_CODE (target_type) == FUNCTION_TYPE
3762 || TREE_CODE (target_type) == METHOD_TYPE
3763 || TREE_CODE (target_type) == OFFSET_TYPE)
3765 : size_in_bytes (target_type));
3767 /* Do the division. */
3769 result = build (EXACT_DIV_EXPR, restype, op0, convert (restype, op1));
3771 folded = fold (result);
3772 if (folded == result)
3773 TREE_CONSTANT (folded) = TREE_CONSTANT (op0) & TREE_CONSTANT (op1);
3777 /* Handle the case of taking the address of a COMPONENT_REF.
3778 Called by `build_unary_op' and `build_up_reference'.
3780 ARG is the COMPONENT_REF whose address we want.
3781 ARGTYPE is the pointer type that this address should have.
3782 MSG is an error message to print if this COMPONENT_REF is not
3783 addressable (such as a bitfield). */
3786 build_component_addr (arg, argtype, msg)
3790 tree field = TREE_OPERAND (arg, 1);
3791 tree basetype = decl_type_context (field);
3792 tree rval = build_unary_op (ADDR_EXPR, TREE_OPERAND (arg, 0), 0);
3794 if (DECL_BIT_FIELD (field))
3796 error (msg, IDENTIFIER_POINTER (DECL_NAME (field)));
3797 return error_mark_node;
3800 if (TREE_CODE (field) == FIELD_DECL
3801 && TYPE_USES_COMPLEX_INHERITANCE (basetype))
3803 /* Can't convert directly to ARGTYPE, since that
3804 may have the same pointer type as one of our
3806 rval = build1 (NOP_EXPR, argtype,
3807 convert_pointer_to (basetype, rval));
3808 TREE_CONSTANT (rval) = TREE_CONSTANT (TREE_OPERAND (rval, 0));
3811 /* This conversion is harmless. */
3812 rval = convert_force (argtype, rval, 0);
3814 if (! integer_zerop (DECL_FIELD_BITPOS (field)))
3816 tree offset = size_binop (EASY_DIV_EXPR, DECL_FIELD_BITPOS (field),
3817 size_int (BITS_PER_UNIT));
3818 int flag = TREE_CONSTANT (rval);
3819 rval = fold (build (PLUS_EXPR, argtype,
3820 rval, convert (argtype, offset)));
3821 TREE_CONSTANT (rval) = flag;
3826 /* Construct and perhaps optimize a tree representation
3827 for a unary operation. CODE, a tree_code, specifies the operation
3828 and XARG is the operand. */
3831 build_x_unary_op (code, xarg)
3832 enum tree_code code;
3835 /* & rec, on incomplete RECORD_TYPEs is the simple opr &, not an
3837 if (code == ADDR_EXPR
3838 && ((IS_AGGR_TYPE_CODE (TREE_CODE (TREE_TYPE (xarg)))
3839 && TYPE_SIZE (TREE_TYPE (xarg)) == NULL_TREE)
3840 || (TREE_CODE (xarg) == OFFSET_REF)))
3841 /* don't look for a function */;
3844 tree rval = build_opfncall (code, LOOKUP_SPECULATIVELY, xarg,
3845 NULL_TREE, NULL_TREE);
3847 return build_opfncall (code, LOOKUP_NORMAL, xarg,
3848 NULL_TREE, NULL_TREE);
3850 return build_unary_op (code, xarg, 0);
3853 /* Just like truthvalue_conversion, but we want a CLEANUP_POINT_EXPR. */
3856 condition_conversion (expr)
3859 tree t = convert (boolean_type_node, expr);
3860 t = fold (build1 (CLEANUP_POINT_EXPR, boolean_type_node, t));
3864 /* C++: Must handle pointers to members.
3866 Perhaps type instantiation should be extended to handle conversion
3867 from aggregates to types we don't yet know we want? (Or are those
3868 cases typically errors which should be reported?)
3870 NOCONVERT nonzero suppresses the default promotions
3871 (such as from short to int). */
3873 build_unary_op (code, xarg, noconvert)
3874 enum tree_code code;
3878 /* No default_conversion here. It causes trouble for ADDR_EXPR. */
3879 register tree arg = xarg;
3880 register tree argtype = 0;
3881 char *errstring = NULL;
3884 if (arg == error_mark_node)
3885 return error_mark_node;
3890 /* This is used for unary plus, because a CONVERT_EXPR
3891 is enough to prevent anybody from looking inside for
3892 associativity, but won't generate any code. */
3893 if (!(arg = build_expr_type_conversion
3894 (WANT_ARITH | WANT_ENUM | WANT_POINTER, arg, 1)))
3895 errstring = "wrong type argument to unary plus";
3899 arg = default_conversion (arg);
3900 arg = build1 (NON_LVALUE_EXPR, TREE_TYPE (arg), arg);
3905 if (!(arg = build_expr_type_conversion (WANT_ARITH | WANT_ENUM, arg, 1)))
3906 errstring = "wrong type argument to unary minus";
3907 else if (!noconvert)
3908 arg = default_conversion (arg);
3912 if (!(arg = build_expr_type_conversion (WANT_INT | WANT_ENUM, arg, 1)))
3913 errstring = "wrong type argument to bit-complement";
3914 else if (!noconvert)
3915 arg = default_conversion (arg);
3919 if (!(arg = build_expr_type_conversion (WANT_ARITH | WANT_ENUM, arg, 1)))
3920 errstring = "wrong type argument to abs";
3921 else if (!noconvert)
3922 arg = default_conversion (arg);
3925 case TRUTH_NOT_EXPR:
3926 arg = convert (boolean_type_node, arg);
3927 val = invert_truthvalue (arg);
3928 if (arg != error_mark_node)
3930 errstring = "in argument to unary !";
3936 case PREINCREMENT_EXPR:
3937 case POSTINCREMENT_EXPR:
3938 case PREDECREMENT_EXPR:
3939 case POSTDECREMENT_EXPR:
3940 /* Handle complex lvalues (when permitted)
3941 by reduction to simpler cases. */
3943 val = unary_complex_lvalue (code, arg);
3947 /* Report invalid types. */
3949 if (!(arg = build_expr_type_conversion (WANT_ARITH | WANT_POINTER,
3952 if (code == PREINCREMENT_EXPR)
3953 errstring ="no pre-increment operator for type";
3954 else if (code == POSTINCREMENT_EXPR)
3955 errstring ="no post-increment operator for type";
3956 else if (code == PREDECREMENT_EXPR)
3957 errstring ="no pre-decrement operator for type";
3959 errstring ="no post-decrement operator for type";
3963 /* Report something read-only. */
3965 if (TYPE_READONLY (TREE_TYPE (arg))
3966 || TREE_READONLY (arg))
3967 readonly_error (arg, ((code == PREINCREMENT_EXPR
3968 || code == POSTINCREMENT_EXPR)
3969 ? "increment" : "decrement"),
3974 tree result_type = TREE_TYPE (arg);
3976 arg = get_unwidened (arg, 0);
3977 argtype = TREE_TYPE (arg);
3979 /* ARM $5.2.5 last annotation says this should be forbidden. */
3980 if (TREE_CODE (argtype) == ENUMERAL_TYPE)
3981 pedwarn ("ANSI C++ forbids %sing an enum",
3982 (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
3983 ? "increment" : "decrement");
3985 /* Compute the increment. */
3987 if (TREE_CODE (argtype) == POINTER_TYPE)
3989 enum tree_code tmp = TREE_CODE (TREE_TYPE (argtype));
3990 if (TYPE_SIZE (TREE_TYPE (argtype)) == 0)
3991 cp_error ("cannot %s a pointer to incomplete type `%T'",
3992 ((code == PREINCREMENT_EXPR
3993 || code == POSTINCREMENT_EXPR)
3994 ? "increment" : "decrement"), TREE_TYPE (argtype));
3995 else if (tmp == FUNCTION_TYPE || tmp == METHOD_TYPE
3996 || tmp == VOID_TYPE || tmp == OFFSET_TYPE)
3997 cp_pedwarn ("ANSI C++ forbids %sing a pointer of type `%T'",
3998 ((code == PREINCREMENT_EXPR
3999 || code == POSTINCREMENT_EXPR)
4000 ? "increment" : "decrement"), argtype);
4001 inc = c_sizeof_nowarn (TREE_TYPE (argtype));
4004 inc = integer_one_node;
4006 inc = convert (argtype, inc);
4008 /* Handle incrementing a cast-expression. */
4010 switch (TREE_CODE (arg))
4015 case FIX_TRUNC_EXPR:
4016 case FIX_FLOOR_EXPR:
4017 case FIX_ROUND_EXPR:
4020 tree incremented, modify, value, compound;
4021 if (! lvalue_p (arg) && pedantic)
4022 pedwarn ("cast to non-reference type used as lvalue");
4023 arg = stabilize_reference (arg);
4024 if (code == PREINCREMENT_EXPR || code == PREDECREMENT_EXPR)
4027 value = save_expr (arg);
4028 incremented = build (((code == PREINCREMENT_EXPR
4029 || code == POSTINCREMENT_EXPR)
4030 ? PLUS_EXPR : MINUS_EXPR),
4031 argtype, value, inc);
4032 TREE_SIDE_EFFECTS (incremented) = 1;
4034 modify = build_modify_expr (arg, NOP_EXPR, incremented);
4035 compound = build (COMPOUND_EXPR, TREE_TYPE (arg), modify, value);
4037 /* Eliminate warning about unused result of + or -. */
4038 TREE_NO_UNUSED_WARNING (compound) = 1;
4043 /* Complain about anything else that is not a true lvalue. */
4044 if (!lvalue_or_else (arg, ((code == PREINCREMENT_EXPR
4045 || code == POSTINCREMENT_EXPR)
4046 ? "increment" : "decrement")))
4047 return error_mark_node;
4049 /* Forbid using -- on `bool'. */
4050 if (TREE_TYPE (arg) == boolean_type_node)
4052 if (code == POSTDECREMENT_EXPR || code == PREDECREMENT_EXPR)
4054 cp_error ("invalid use of `--' on bool variable `%D'", arg);
4055 return error_mark_node;
4058 /* This will only work if someone can convince Kenner to accept
4059 my patch to expand_increment. (jason) */
4060 val = build (code, TREE_TYPE (arg), arg, inc);
4062 if (code == POSTINCREMENT_EXPR)
4064 arg = stabilize_reference (arg);
4065 val = build (MODIFY_EXPR, TREE_TYPE (arg), arg,
4067 TREE_SIDE_EFFECTS (val) = 1;
4068 arg = save_expr (arg);
4069 val = build (COMPOUND_EXPR, TREE_TYPE (arg), val, arg);
4070 val = build (COMPOUND_EXPR, TREE_TYPE (arg), arg, val);
4073 val = build (MODIFY_EXPR, TREE_TYPE (arg), arg,
4078 val = build (code, TREE_TYPE (arg), arg, inc);
4080 TREE_SIDE_EFFECTS (val) = 1;
4081 return convert (result_type, val);
4085 /* Note that this operation never does default_conversion
4086 regardless of NOCONVERT. */
4088 argtype = TREE_TYPE (arg);
4089 if (TREE_CODE (argtype) == REFERENCE_TYPE)
4091 arg = build1 (CONVERT_EXPR, build_pointer_type (TREE_TYPE (TREE_TYPE (arg))), arg);
4092 TREE_REFERENCE_EXPR (arg) = 1;
4096 && TREE_CODE (arg) == FUNCTION_DECL
4098 && DECL_CONTEXT (arg) == NULL_TREE
4099 && IDENTIFIER_LENGTH (DECL_NAME (arg)) == 4
4100 && IDENTIFIER_POINTER (DECL_NAME (arg))[0] == 'm'
4101 && ! strcmp (IDENTIFIER_POINTER (DECL_NAME (arg)), "main"))
4103 pedwarn ("taking address of function `main'");
4105 /* Let &* cancel out to simplify resulting code. */
4106 if (TREE_CODE (arg) == INDIRECT_REF)
4108 /* We don't need to have `current_class_decl' wrapped in a
4109 NON_LVALUE_EXPR node. */
4111 return current_class_decl;
4113 /* Keep `default_conversion' from converting if
4114 ARG is of REFERENCE_TYPE. */
4115 arg = TREE_OPERAND (arg, 0);
4116 if (TREE_CODE (TREE_TYPE (arg)) == REFERENCE_TYPE)
4118 if (TREE_CODE (arg) == VAR_DECL && DECL_INITIAL (arg)
4119 && !TREE_SIDE_EFFECTS (DECL_INITIAL (arg)))
4120 arg = DECL_INITIAL (arg);
4121 arg = build1 (CONVERT_EXPR, build_pointer_type (TREE_TYPE (TREE_TYPE (arg))), arg);
4122 TREE_REFERENCE_EXPR (arg) = 1;
4123 TREE_CONSTANT (arg) = TREE_CONSTANT (TREE_OPERAND (arg, 0));
4125 else if (lvalue_p (arg))
4126 /* Don't let this be an lvalue. */
4127 return non_lvalue (arg);
4131 /* For &x[y], return x+y */
4132 if (TREE_CODE (arg) == ARRAY_REF)
4134 if (mark_addressable (TREE_OPERAND (arg, 0)) == 0)
4135 return error_mark_node;
4136 return build_binary_op (PLUS_EXPR, TREE_OPERAND (arg, 0),
4137 TREE_OPERAND (arg, 1), 1);
4140 /* Uninstantiated types are all functions. Taking the
4141 address of a function is a no-op, so just return the
4144 if (TREE_CODE (arg) == IDENTIFIER_NODE
4145 && IDENTIFIER_OPNAME_P (arg))
4147 my_friendly_abort (117);
4148 /* We don't know the type yet, so just work around the problem.
4149 We know that this will resolve to an lvalue. */
4150 return build1 (ADDR_EXPR, unknown_type_node, arg);
4153 if (TREE_CODE (arg) == TREE_LIST)
4155 if (TREE_CODE (TREE_VALUE (arg)) == FUNCTION_DECL
4156 && DECL_CHAIN (TREE_VALUE (arg)) == NULL_TREE)
4157 /* Unique overloaded non-member function. */
4158 return build_unary_op (ADDR_EXPR, TREE_VALUE (arg), 0);
4159 if (TREE_CHAIN (arg) == NULL_TREE
4160 && TREE_CODE (TREE_VALUE (arg)) == TREE_LIST
4161 && DECL_CHAIN (TREE_VALUE (TREE_VALUE (arg))) == NULL_TREE)
4162 /* Unique overloaded member function. */
4163 return build_unary_op (ADDR_EXPR, TREE_VALUE (TREE_VALUE (arg)),
4165 return build1 (ADDR_EXPR, unknown_type_node, arg);
4168 /* Handle complex lvalues (when permitted)
4169 by reduction to simpler cases. */
4170 val = unary_complex_lvalue (code, arg);
4174 switch (TREE_CODE (arg))
4179 case FIX_TRUNC_EXPR:
4180 case FIX_FLOOR_EXPR:
4181 case FIX_ROUND_EXPR:
4183 if (! lvalue_p (arg) && pedantic)
4184 pedwarn ("taking the address of a cast to non-reference type");
4187 /* Allow the address of a constructor if all the elements
4189 if (TREE_CODE (arg) == CONSTRUCTOR && TREE_CONSTANT (arg))
4191 /* Anything not already handled and not a true memory reference
4193 else if (TREE_CODE (argtype) != FUNCTION_TYPE
4194 && TREE_CODE (argtype) != METHOD_TYPE
4195 && !lvalue_or_else (arg, "unary `&'"))
4196 return error_mark_node;
4198 /* Ordinary case; arg is a COMPONENT_REF or a decl. */
4199 /* If the lvalue is const or volatile,
4200 merge that into the type that the address will point to. */
4201 if (TREE_CODE_CLASS (TREE_CODE (arg)) == 'd'
4202 || TREE_CODE_CLASS (TREE_CODE (arg)) == 'r')
4204 if (TREE_READONLY (arg) || TREE_THIS_VOLATILE (arg))
4205 argtype = cp_build_type_variant (argtype,
4206 TREE_READONLY (arg),
4207 TREE_THIS_VOLATILE (arg));
4210 argtype = build_pointer_type (argtype);
4212 if (mark_addressable (arg) == 0)
4213 return error_mark_node;
4218 if (TREE_CODE (arg) == COMPONENT_REF)
4219 addr = build_component_addr (arg, argtype,
4220 "attempt to take address of bit-field structure member `%s'");
4222 addr = build1 (code, argtype, arg);
4224 /* Address of a static or external variable or
4225 function counts as a constant */
4227 TREE_CONSTANT (addr) = 1;
4235 argtype = TREE_TYPE (arg);
4236 return fold (build1 (code, argtype, arg));
4240 return error_mark_node;
4243 /* If CONVERSIONS is a conversion expression or a nested sequence of such,
4244 convert ARG with the same conversions in the same order
4245 and return the result. */
4248 convert_sequence (conversions, arg)
4252 switch (TREE_CODE (conversions))
4257 case FIX_TRUNC_EXPR:
4258 case FIX_FLOOR_EXPR:
4259 case FIX_ROUND_EXPR:
4261 return convert (TREE_TYPE (conversions),
4262 convert_sequence (TREE_OPERAND (conversions, 0),
4270 /* Apply unary lvalue-demanding operator CODE to the expression ARG
4271 for certain kinds of expressions which are not really lvalues
4272 but which we can accept as lvalues.
4274 If ARG is not a kind of expression we can handle, return zero. */
4277 unary_complex_lvalue (code, arg)
4278 enum tree_code code;
4281 /* Handle (a, b) used as an "lvalue". */
4282 if (TREE_CODE (arg) == COMPOUND_EXPR)
4284 tree real_result = build_unary_op (code, TREE_OPERAND (arg, 1), 0);
4285 return build (COMPOUND_EXPR, TREE_TYPE (real_result),
4286 TREE_OPERAND (arg, 0), real_result);
4289 /* Handle (a ? b : c) used as an "lvalue". */
4290 if (TREE_CODE (arg) == COND_EXPR)
4291 return rationalize_conditional_expr (code, arg);
4293 if (TREE_CODE (arg) == MODIFY_EXPR
4294 || TREE_CODE (arg) == PREINCREMENT_EXPR
4295 || TREE_CODE (arg) == PREDECREMENT_EXPR)
4296 return unary_complex_lvalue
4297 (code, build (COMPOUND_EXPR, TREE_TYPE (TREE_OPERAND (arg, 0)),
4298 arg, TREE_OPERAND (arg, 0)));
4300 if (code != ADDR_EXPR)
4303 /* Handle (a = b) used as an "lvalue" for `&'. */
4304 if (TREE_CODE (arg) == MODIFY_EXPR
4305 || TREE_CODE (arg) == INIT_EXPR)
4307 tree real_result = build_unary_op (code, TREE_OPERAND (arg, 0), 0);
4308 return build (COMPOUND_EXPR, TREE_TYPE (real_result), arg, real_result);
4311 if (TREE_CODE (arg) == WITH_CLEANUP_EXPR)
4313 tree real_result = build_unary_op (code, TREE_OPERAND (arg, 0), 0);
4314 real_result = build (WITH_CLEANUP_EXPR, TREE_TYPE (real_result),
4315 real_result, 0, TREE_OPERAND (arg, 2));
4319 if (TREE_CODE (TREE_TYPE (arg)) == FUNCTION_TYPE
4320 || TREE_CODE (TREE_TYPE (arg)) == METHOD_TYPE
4321 || TREE_CODE (TREE_TYPE (arg)) == OFFSET_TYPE)
4323 /* The representation of something of type OFFSET_TYPE
4324 is really the representation of a pointer to it.
4325 Here give the representation its true type. */
4329 my_friendly_assert (TREE_CODE (arg) != SCOPE_REF, 313);
4331 if (TREE_CODE (arg) != OFFSET_REF)
4334 t = TREE_OPERAND (arg, 1);
4336 if (TREE_CODE (t) == FUNCTION_DECL) /* Check all this code for right semantics. */
4337 return build_unary_op (ADDR_EXPR, t, 0);
4338 if (TREE_CODE (t) == VAR_DECL)
4339 return build_unary_op (ADDR_EXPR, t, 0);
4342 if (TREE_OPERAND (arg, 0)
4343 && (TREE_CODE (TREE_OPERAND (arg, 0)) != NOP_EXPR
4344 || TREE_OPERAND (TREE_OPERAND (arg, 0), 0) != error_mark_node))
4345 if (TREE_CODE (t) != FIELD_DECL)
4347 /* Don't know if this should return address to just
4348 _DECL, or actual address resolved in this expression. */
4349 sorry ("address of bound pointer-to-member expression");
4350 return error_mark_node;
4353 offset = get_delta_difference (DECL_FIELD_CONTEXT (t),
4354 TREE_TYPE (TREE_OPERAND (arg, 0)),
4356 offset = size_binop (PLUS_EXPR, offset,
4357 size_binop (EASY_DIV_EXPR,
4358 DECL_FIELD_BITPOS (t),
4359 size_int (BITS_PER_UNIT)));
4360 return convert (build_pointer_type (TREE_TYPE (arg)), offset);
4364 if (TREE_CODE (arg) == OFFSET_REF)
4366 tree left = TREE_OPERAND (arg, 0), left_addr;
4367 tree right_addr = build_unary_op (ADDR_EXPR, TREE_OPERAND (arg, 1), 0);
4370 if (current_class_decl)
4371 left_addr = current_class_decl;
4374 error ("no `this' for pointer to member");
4375 return error_mark_node;
4378 left_addr = build_unary_op (ADDR_EXPR, left, 0);
4380 return build (PLUS_EXPR, build_pointer_type (TREE_TYPE (arg)),
4381 build1 (NOP_EXPR, integer_type_node, left_addr),
4382 build1 (NOP_EXPR, integer_type_node, right_addr));
4385 /* We permit compiler to make function calls returning
4386 objects of aggregate type look like lvalues. */
4390 if (TREE_CODE (targ) == SAVE_EXPR)
4391 targ = TREE_OPERAND (targ, 0);
4393 if (TREE_CODE (targ) == CALL_EXPR && IS_AGGR_TYPE (TREE_TYPE (targ)))
4395 if (TREE_CODE (arg) == SAVE_EXPR)
4398 targ = build_cplus_new (TREE_TYPE (arg), arg, 1);
4399 return build1 (ADDR_EXPR, build_pointer_type (TREE_TYPE (arg)), targ);
4402 if (TREE_CODE (arg) == SAVE_EXPR && TREE_CODE (targ) == INDIRECT_REF)
4403 return build (SAVE_EXPR, build_pointer_type (TREE_TYPE (arg)),
4404 TREE_OPERAND (targ, 0), current_function_decl, NULL);
4406 /* We shouldn't wrap WITH_CLEANUP_EXPRs inside of SAVE_EXPRs, but in case
4407 we do, here's how to handle it. */
4408 if (TREE_CODE (arg) == SAVE_EXPR && TREE_CODE (targ) == WITH_CLEANUP_EXPR)
4411 /* Not really a bug, but something to turn on when testing. */
4412 compiler_error ("WITH_CLEANUP_EXPR wrapped in SAVE_EXPR");
4414 return unary_complex_lvalue (ADDR_EXPR, targ);
4418 /* Don't let anything else be handled specially. */
4422 /* Mark EXP saying that we need to be able to take the
4423 address of it; it should not be allocated in a register.
4424 Value is 1 if successful.
4426 C++: we do not allow `current_class_decl' to be addressable. */
4429 mark_addressable (exp)
4432 register tree x = exp;
4434 if (TREE_ADDRESSABLE (x) == 1)
4438 switch (TREE_CODE (x))
4443 x = TREE_OPERAND (x, 0);
4447 if (x == current_class_decl)
4449 error ("address of `this' not available");
4450 TREE_ADDRESSABLE (x) = 1; /* so compiler doesn't die later */
4451 put_var_into_stack (x);
4456 && TREE_READONLY (x)
4457 && DECL_RTL (x) != 0
4458 && ! decl_in_memory_p (x))
4460 /* We thought this would make a good constant variable,
4461 but we were wrong. */
4462 push_obstacks_nochange ();
4463 end_temporary_allocation ();
4465 TREE_ASM_WRITTEN (x) = 0;
4467 rest_of_decl_compilation (x, 0, IDENTIFIER_LOCAL_VALUE (x) == 0, 0);
4468 TREE_ADDRESSABLE (x) = 1;
4474 /* Caller should not be trying to mark initialized
4475 constant fields addressable. */
4476 my_friendly_assert (DECL_LANG_SPECIFIC (x) == 0
4477 || DECL_IN_AGGR_P (x) == 0
4479 || DECL_EXTERNAL (x), 314);
4483 /* For C++, we don't warn about taking the address of a register
4484 variable for CONST_DECLs; ARM p97 explicitly says it's okay. */
4485 put_var_into_stack (x);
4486 TREE_ADDRESSABLE (x) = 1;
4490 /* We have to test both conditions here. The first may
4491 be non-zero in the case of processing a default function.
4492 The second may be non-zero in the case of a template function. */
4493 x = DECL_MAIN_VARIANT (x);
4494 if ((DECL_THIS_INLINE (x) || DECL_PENDING_INLINE_INFO (x))
4495 && (DECL_CONTEXT (x) == NULL_TREE
4496 || TREE_CODE_CLASS (TREE_CODE (DECL_CONTEXT (x))) != 't'
4497 || ! CLASSTYPE_INTERFACE_ONLY (DECL_CONTEXT (x))))
4499 mark_inline_for_output (x);
4500 if (x == current_function_decl)
4501 DECL_EXTERNAL (x) = 0;
4503 TREE_ADDRESSABLE (x) = 1;
4505 TREE_ADDRESSABLE (DECL_ASSEMBLER_NAME (x)) = 1;
4513 /* Build and return a conditional expression IFEXP ? OP1 : OP2. */
4516 build_x_conditional_expr (ifexp, op1, op2)
4517 tree ifexp, op1, op2;
4519 tree rval = NULL_TREE;
4521 /* See comments in `build_x_binary_op'. */
4523 rval = build_opfncall (COND_EXPR, LOOKUP_SPECULATIVELY, ifexp, op1, op2);
4525 return build_opfncall (COND_EXPR, LOOKUP_NORMAL, ifexp, op1, op2);
4527 return build_conditional_expr (ifexp, op1, op2);
4531 build_conditional_expr (ifexp, op1, op2)
4532 tree ifexp, op1, op2;
4534 register tree type1;
4535 register tree type2;
4536 register enum tree_code code1;
4537 register enum tree_code code2;
4538 register tree result_type = NULL_TREE;
4539 tree orig_op1 = op1, orig_op2 = op2;
4541 /* If second operand is omitted, it is the same as the first one;
4542 make sure it is calculated only once. */
4546 pedwarn ("ANSI C++ forbids omitting the middle term of a ?: expression");
4547 ifexp = op1 = save_expr (ifexp);
4550 ifexp = convert (boolean_type_node, ifexp);
4552 if (TREE_CODE (ifexp) == ERROR_MARK)
4553 return error_mark_node;
4555 op1 = require_instantiated_type (TREE_TYPE (op2), op1, error_mark_node);
4556 if (op1 == error_mark_node)
4557 return error_mark_node;
4558 op2 = require_instantiated_type (TREE_TYPE (op1), op2, error_mark_node);
4559 if (op2 == error_mark_node)
4560 return error_mark_node;
4562 /* C++: REFERENCE_TYPES must be dereferenced. */
4563 type1 = TREE_TYPE (op1);
4564 code1 = TREE_CODE (type1);
4565 type2 = TREE_TYPE (op2);
4566 code2 = TREE_CODE (type2);
4568 if (code1 == REFERENCE_TYPE)
4570 op1 = convert_from_reference (op1);
4571 type1 = TREE_TYPE (op1);
4572 code1 = TREE_CODE (type1);
4574 if (code2 == REFERENCE_TYPE)
4576 op2 = convert_from_reference (op2);
4577 type2 = TREE_TYPE (op2);
4578 code2 = TREE_CODE (type2);
4581 /* Don't promote the operands separately if they promote
4582 the same way. Return the unpromoted type and let the combined
4583 value get promoted if necessary. */
4585 if (TYPE_MAIN_VARIANT (type1) == TYPE_MAIN_VARIANT (type2)
4586 && code2 != ARRAY_TYPE
4587 && code2 != FUNCTION_TYPE
4588 && code2 != METHOD_TYPE)
4592 if (TREE_CONSTANT (ifexp)
4593 && (TREE_CODE (ifexp) == INTEGER_CST
4594 || TREE_CODE (ifexp) == ADDR_EXPR))
4595 return (integer_zerop (ifexp) ? op2 : op1);
4597 if (TREE_CODE (op1) == CONST_DECL)
4598 op1 = DECL_INITIAL (op1);
4599 else if (TREE_READONLY_DECL_P (op1))
4600 op1 = decl_constant_value (op1);
4601 if (TREE_CODE (op2) == CONST_DECL)
4602 op2 = DECL_INITIAL (op2);
4603 else if (TREE_READONLY_DECL_P (op2))
4604 op2 = decl_constant_value (op2);
4606 type1 = cp_build_type_variant
4608 TREE_READONLY (op1) || TREE_READONLY (op2),
4609 TREE_THIS_VOLATILE (op1) || TREE_THIS_VOLATILE (op2));
4610 /* ??? This is a kludge to deal with the fact that
4611 we don't sort out integers and enums properly, yet. */
4612 result = fold (build (COND_EXPR, type1, ifexp, op1, op2));
4613 if (TREE_TYPE (result) != type1)
4614 result = build1 (NOP_EXPR, type1, result);
4618 /* They don't match; promote them both and then try to reconcile them.
4619 But don't permit mismatching enum types. */
4620 if (code1 == ENUMERAL_TYPE)
4622 if (code2 == ENUMERAL_TYPE)
4624 cp_error ("enumeral mismatch in conditional expression: `%T' vs `%T'", type1, type2);
4625 return error_mark_node;
4627 else if (extra_warnings && ! IS_AGGR_TYPE_CODE (code2)
4628 && type2 != type_promotes_to (type1))
4629 warning ("enumeral and non-enumeral type in conditional expression");
4631 else if (extra_warnings
4632 && code2 == ENUMERAL_TYPE && ! IS_AGGR_TYPE_CODE (code1)
4633 && type1 != type_promotes_to (type2))
4634 warning ("enumeral and non-enumeral type in conditional expression");
4636 if (code1 != VOID_TYPE)
4638 op1 = default_conversion (op1);
4639 type1 = TREE_TYPE (op1);
4640 if (TYPE_PTRMEMFUNC_P (type1))
4641 type1 = TYPE_PTRMEMFUNC_FN_TYPE (type1);
4642 code1 = TREE_CODE (type1);
4644 if (code2 != VOID_TYPE)
4646 op2 = default_conversion (op2);
4647 type2 = TREE_TYPE (op2);
4648 if (TYPE_PTRMEMFUNC_P (type2))
4649 type2 = TYPE_PTRMEMFUNC_FN_TYPE (type2);
4650 code2 = TREE_CODE (type2);
4653 if (code1 == RECORD_TYPE && code2 == RECORD_TYPE
4654 && real_lvalue_p (op1) && real_lvalue_p (op2)
4655 && comptypes (type1, type2, -1))
4657 type1 = build_reference_type (type1);
4658 type2 = build_reference_type (type2);
4659 result_type = common_type (type1, type2);
4660 op1 = convert_to_reference (result_type, op1, CONV_IMPLICIT,
4661 LOOKUP_NORMAL, NULL_TREE);
4662 op2 = convert_to_reference (result_type, op2, CONV_IMPLICIT,
4663 LOOKUP_NORMAL, NULL_TREE);
4665 /* Quickly detect the usual case where op1 and op2 have the same type
4667 else if (TYPE_MAIN_VARIANT (type1) == TYPE_MAIN_VARIANT (type2))
4670 result_type = type1;
4672 result_type = cp_build_type_variant
4674 TREE_READONLY (op1) || TREE_READONLY (op2),
4675 TREE_THIS_VOLATILE (op1) || TREE_THIS_VOLATILE (op2));
4677 else if ((code1 == INTEGER_TYPE || code1 == REAL_TYPE)
4678 && (code2 == INTEGER_TYPE || code2 == REAL_TYPE))
4680 result_type = common_type (type1, type2);
4682 else if (code1 == VOID_TYPE || code2 == VOID_TYPE)
4684 if (pedantic && (code1 != VOID_TYPE || code2 != VOID_TYPE))
4685 pedwarn ("ANSI C++ forbids conditional expr with only one void side");
4686 result_type = void_type_node;
4688 else if (code1 == POINTER_TYPE && code2 == POINTER_TYPE)
4690 if (comp_target_types (type1, type2, 1))
4691 result_type = common_type (type1, type2);
4692 else if (integer_zerop (op1) && TREE_TYPE (type1) == void_type_node
4693 && TREE_CODE (orig_op1) != NOP_EXPR)
4694 result_type = qualify_type (type2, type1);
4695 else if (integer_zerop (op2) && TREE_TYPE (type2) == void_type_node
4696 && TREE_CODE (orig_op2) != NOP_EXPR)
4697 result_type = qualify_type (type1, type2);
4698 else if (TYPE_MAIN_VARIANT (TREE_TYPE (type1)) == void_type_node)
4700 if (pedantic && TREE_CODE (type2) == FUNCTION_TYPE)
4701 pedwarn ("ANSI C++ forbids conditional expr between `void *' and function pointer");
4702 result_type = qualify_type (type1, type2);
4704 else if (TYPE_MAIN_VARIANT (TREE_TYPE (type2)) == void_type_node)
4706 if (pedantic && TREE_CODE (type1) == FUNCTION_TYPE)
4707 pedwarn ("ANSI C++ forbids conditional expr between `void *' and function pointer");
4708 result_type = qualify_type (type2, type1);
4711 else if (comptypes (type2, type1, 0))
4712 result_type = type2;
4713 else if (IS_AGGR_TYPE (TREE_TYPE (type1))
4714 && IS_AGGR_TYPE (TREE_TYPE (type2))
4715 && (result_type = common_base_type (TREE_TYPE (type1), TREE_TYPE (type2))))
4717 if (result_type == error_mark_node)
4719 cp_error ("common base type of types `%T' and `%T' is ambiguous",
4720 TREE_TYPE (type1), TREE_TYPE (type2));
4721 result_type = ptr_type_node;
4726 && result_type != TREE_TYPE (type1)
4727 && result_type != TREE_TYPE (type2))
4728 cp_pedwarn ("`%T' and `%T' converted to `%T *' in conditional expression",
4729 type1, type2, result_type);
4731 result_type = build_pointer_type (result_type);
4736 pedwarn ("pointer type mismatch in conditional expression");
4737 result_type = ptr_type_node;
4740 else if (code1 == POINTER_TYPE && code2 == INTEGER_TYPE)
4742 if (!integer_zerop (op2))
4743 pedwarn ("pointer/integer type mismatch in conditional expression");
4745 op2 = null_pointer_node;
4747 result_type = type1;
4749 else if (code2 == POINTER_TYPE && code1 == INTEGER_TYPE)
4751 if (!integer_zerop (op1))
4752 pedwarn ("pointer/integer type mismatch in conditional expression");
4754 op1 = null_pointer_node;
4756 result_type = type2;
4761 /* The match does not look good. If either is
4762 an aggregate value, try converting to a scalar type. */
4763 if (code1 == RECORD_TYPE && code2 == RECORD_TYPE)
4765 cp_error ("aggregate mismatch in conditional expression: `%T' vs `%T'", type1, type2);
4766 return error_mark_node;
4768 if (code1 == RECORD_TYPE && TYPE_HAS_CONVERSION (type1))
4770 tree tmp = build_type_conversion (CONVERT_EXPR, type2, op1, 0);
4771 if (tmp == NULL_TREE)
4773 cp_error ("aggregate type `%T' could not convert on lhs of `:'", type1);
4774 return error_mark_node;
4776 if (tmp == error_mark_node)
4777 error ("ambiguous pointer conversion");
4778 result_type = type2;
4781 else if (code2 == RECORD_TYPE && TYPE_HAS_CONVERSION (type2))
4783 tree tmp = build_type_conversion (CONVERT_EXPR, type1, op2, 0);
4784 if (tmp == NULL_TREE)
4786 cp_error ("aggregate type `%T' could not convert on rhs of `:'", type2);
4787 return error_mark_node;
4789 if (tmp == error_mark_node)
4790 error ("ambiguous pointer conversion");
4791 result_type = type1;
4794 else if (flag_cond_mismatch)
4795 result_type = void_type_node;
4798 error ("type mismatch in conditional expression");
4799 return error_mark_node;
4803 if (TREE_CODE (result_type) == POINTER_TYPE
4804 && TREE_CODE (TREE_TYPE (result_type)) == METHOD_TYPE)
4805 result_type = build_ptrmemfunc_type (result_type);
4807 if (result_type != TREE_TYPE (op1))
4808 op1 = convert_and_check (result_type, op1);
4809 if (result_type != TREE_TYPE (op2))
4810 op2 = convert_and_check (result_type, op2);
4812 if (TREE_CONSTANT (ifexp))
4813 return integer_zerop (ifexp) ? op2 : op1;
4815 return convert_from_reference
4816 (fold (build (COND_EXPR, result_type, ifexp, op1, op2)));
4819 /* Handle overloading of the ',' operator when needed. Otherwise,
4820 this function just builds an expression list. */
4822 build_x_compound_expr (list)
4825 tree rest = TREE_CHAIN (list);
4828 if (rest == NULL_TREE)
4829 return build_compound_expr (list);
4831 result = build_opfncall (COMPOUND_EXPR, LOOKUP_NORMAL,
4832 TREE_VALUE (list), TREE_VALUE (rest), NULL_TREE);
4834 return build_x_compound_expr (tree_cons (NULL_TREE, result, TREE_CHAIN (rest)));
4836 if (! TREE_SIDE_EFFECTS (TREE_VALUE (list)))
4838 /* the left-hand operand of a comma expression is like an expression
4839 statement: we should warn if it doesn't have any side-effects,
4840 unless it was explicitly cast to (void). */
4841 if ((extra_warnings || warn_unused)
4842 && !(TREE_CODE (TREE_VALUE(list)) == CONVERT_EXPR
4843 && TREE_TYPE (TREE_VALUE(list)) == void_type_node))
4844 warning("left-hand operand of comma expression has no effect");
4846 #if 0 /* this requires a gcc backend patch to export warn_if_unused_value */
4847 else if (warn_unused)
4848 warn_if_unused_value (TREE_VALUE(list));
4851 return build_compound_expr (tree_cons (NULL_TREE, TREE_VALUE (list),
4852 build_tree_list (NULL_TREE, build_x_compound_expr (rest))));
4855 /* Given a list of expressions, return a compound expression
4856 that performs them all and returns the value of the last of them. */
4859 build_compound_expr (list)
4864 if (TREE_READONLY_DECL_P (TREE_VALUE (list)))
4865 TREE_VALUE (list) = decl_constant_value (TREE_VALUE (list));
4867 if (TREE_CHAIN (list) == 0)
4869 /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
4870 Strip such NOP_EXPRs, since LIST is used in non-lvalue context. */
4871 if (TREE_CODE (list) == NOP_EXPR
4872 && TREE_TYPE (list) == TREE_TYPE (TREE_OPERAND (list, 0)))
4873 list = TREE_OPERAND (list, 0);
4875 /* Convert arrays to pointers. */
4876 if (TREE_CODE (TREE_TYPE (TREE_VALUE (list))) == ARRAY_TYPE)
4877 return default_conversion (TREE_VALUE (list));
4879 return TREE_VALUE (list);
4882 rest = build_compound_expr (TREE_CHAIN (list));
4884 /* When pedantic, a compound expression cannot be a constant expression. */
4885 if (! TREE_SIDE_EFFECTS (TREE_VALUE (list)) && ! pedantic)
4888 return build (COMPOUND_EXPR, TREE_TYPE (rest),
4889 break_out_cleanups (TREE_VALUE (list)), rest);
4892 tree build_static_cast (type, expr)
4895 return build_c_cast (type, expr, 0);
4898 tree build_reinterpret_cast (type, expr)
4901 tree intype = TREE_TYPE (expr);
4903 if (TYPE_PTRMEMFUNC_P (type))
4904 type = TYPE_PTRMEMFUNC_FN_TYPE (type);
4905 if (TYPE_PTRMEMFUNC_P (intype))
4906 intype = TYPE_PTRMEMFUNC_FN_TYPE (intype);
4908 if (! POINTER_TYPE_P (type) && ! TREE_CODE (type) == INTEGER_TYPE)
4910 cp_error ("reinterpret_cast cannot convert to type `%T'", type);
4911 return error_mark_node;
4913 if (! POINTER_TYPE_P (intype) && ! TREE_CODE (intype) == INTEGER_TYPE)
4915 cp_error ("reinterpret_cast cannot convert from type `%T'", type);
4916 return error_mark_node;
4918 if (TREE_CODE (type) == INTEGER_TYPE && TREE_CODE (intype) != POINTER_TYPE)
4920 cp_error ("reinterpret_cast cannot convert non-pointer type `%T' to `%T'",
4922 return error_mark_node;
4924 if (TREE_CODE (intype) == INTEGER_TYPE && TREE_CODE (type) != POINTER_TYPE)
4926 cp_error ("reinterpret_cast cannot convert `%T' to non-pointer type `%T'",
4928 return error_mark_node;
4931 if (TREE_CODE (type) == POINTER_TYPE && TREE_CODE (intype) == POINTER_TYPE)
4932 expr = convert (ptr_type_node, expr);
4934 return build_c_cast (type, expr, 0);
4937 tree build_const_cast (type, expr)
4940 tree intype = TREE_TYPE (expr);
4943 if (type == error_mark_node || expr == error_mark_node)
4944 return error_mark_node;
4946 if (TYPE_PTRMEMFUNC_P (type))
4947 type = TYPE_PTRMEMFUNC_FN_TYPE (type);
4948 if (TYPE_PTRMEMFUNC_P (intype))
4949 intype = TYPE_PTRMEMFUNC_FN_TYPE (intype);
4951 if (! POINTER_TYPE_P (type))
4953 cp_error ("const_cast cannot convert to non-pointer type `%T'", type);
4954 return error_mark_node;
4956 if (TREE_CODE (type) == REFERENCE_TYPE && ! real_lvalue_p (expr))
4958 cp_error ("const_cast cannot convert rvalue to type `%T'", type);
4959 return error_mark_node;
4961 if (TREE_CODE (type) == POINTER_TYPE && TREE_CODE (intype) != POINTER_TYPE)
4963 cp_error ("const_cast cannot convert non-pointer type `%T' to type `%T'",
4965 return error_mark_node;
4968 if (TREE_CODE (type) == REFERENCE_TYPE)
4970 t1 = TREE_TYPE (type);
4975 t1 = TREE_TYPE (type);
4976 t2 = TREE_TYPE (intype);
4978 for (; TREE_CODE (t1) == POINTER_TYPE && TREE_CODE (t2) == POINTER_TYPE;
4979 t1 = TREE_TYPE (t1), t2 = TREE_TYPE (t2))
4983 if (TREE_CODE (t1) == OFFSET_TYPE && TREE_CODE (t2) == OFFSET_TYPE)
4985 if (TYPE_OFFSET_BASETYPE (t1) != TYPE_OFFSET_BASETYPE (t2))
4987 cp_error ("const_cast cannot convert between pointers to members of different types `%T' and `%T'",
4988 TYPE_OFFSET_BASETYPE (t2), TYPE_OFFSET_BASETYPE (t1));
4989 return error_mark_node;
4991 t1 = TREE_TYPE (t1);
4992 t2 = TREE_TYPE (t2);
4995 if (TYPE_MAIN_VARIANT (t1) != TYPE_MAIN_VARIANT (t2))
4997 cp_error ("const_cast cannot convert unrelated type `%T' to `%T'",
4999 return error_mark_node;
5002 return build_c_cast (type, expr, 0);
5005 /* Build an expression representing a cast to type TYPE of expression EXPR.
5007 ALLOW_NONCONVERTING is true if we should allow non-converting constructors
5008 when doing the cast. */
5011 build_c_cast (type, expr, allow_nonconverting)
5014 int allow_nonconverting;
5016 register tree value = expr;
5018 if (type == error_mark_node || expr == error_mark_node)
5019 return error_mark_node;
5021 /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
5022 Strip such NOP_EXPRs if VALUE is being used in non-lvalue context. */
5023 if (TREE_CODE (type) != REFERENCE_TYPE
5024 && TREE_CODE (value) == NOP_EXPR
5025 && TREE_TYPE (value) == TREE_TYPE (TREE_OPERAND (value, 0)))
5026 value = TREE_OPERAND (value, 0);
5028 if (TREE_TYPE (expr)
5029 && TREE_CODE (TREE_TYPE (expr)) == OFFSET_TYPE
5030 && TREE_CODE (type) != OFFSET_TYPE)
5031 value = resolve_offset_ref (value);
5033 if (TREE_CODE (type) == ARRAY_TYPE)
5035 /* Allow casting from T1* to T2[] because Cfront allows it.
5036 NIHCL uses it. It is not valid ANSI C however, and hence, not
5038 if (TREE_CODE (TREE_TYPE (expr)) == POINTER_TYPE)
5041 pedwarn ("ANSI C++ forbids casting to an array type");
5042 type = build_pointer_type (TREE_TYPE (type));
5046 error ("ANSI C++ forbids casting to an array type");
5047 return error_mark_node;
5051 if (TREE_CODE (type) == FUNCTION_TYPE
5052 || TREE_CODE (type) == METHOD_TYPE)
5054 cp_error ("casting to function type `%T'", type);
5055 return error_mark_node;
5058 if (IS_SIGNATURE (type))
5060 error ("cast specifies signature type");
5061 return error_mark_node;
5064 /* If there's only one function in the overloaded space,
5066 if (TREE_CODE (value) == TREE_LIST
5067 && TREE_CHAIN (value) == NULL_TREE)
5068 value = TREE_VALUE (value);
5070 if (TREE_CODE (type) == VOID_TYPE)
5071 value = build1 (CONVERT_EXPR, type, value);
5072 else if (TREE_TYPE (value) == NULL_TREE
5073 || type_unknown_p (value))
5075 value = instantiate_type (type, value, 1);
5077 if (value == error_mark_node)
5078 return error_mark_node;
5085 /* Convert functions and arrays to pointers and
5086 convert references to their expanded types,
5087 but don't convert any other types. */
5088 if (TREE_CODE (TREE_TYPE (value)) == FUNCTION_TYPE
5089 || TREE_CODE (TREE_TYPE (value)) == METHOD_TYPE
5090 || TREE_CODE (TREE_TYPE (value)) == ARRAY_TYPE
5091 || TREE_CODE (TREE_TYPE (value)) == REFERENCE_TYPE)
5092 value = default_conversion (value);
5093 otype = TREE_TYPE (value);
5095 /* Optionally warn about potentially worrisome casts. */
5098 && TREE_CODE (type) == POINTER_TYPE
5099 && TREE_CODE (otype) == POINTER_TYPE)
5101 /* For C++ we make these regular warnings, rather than
5102 softening them into pedwarns. */
5103 if (TYPE_VOLATILE (TREE_TYPE (otype))
5104 && ! TYPE_VOLATILE (TREE_TYPE (type)))
5105 warning ("cast discards `volatile' from pointer target type");
5106 if (TYPE_READONLY (TREE_TYPE (otype))
5107 && ! TYPE_READONLY (TREE_TYPE (type)))
5108 warning ("cast discards `const' from pointer target type");
5111 /* Warn about possible alignment problems. */
5112 if (STRICT_ALIGNMENT && warn_cast_align
5113 && TREE_CODE (type) == POINTER_TYPE
5114 && TREE_CODE (otype) == POINTER_TYPE
5115 && TREE_CODE (TREE_TYPE (otype)) != VOID_TYPE
5116 && TREE_CODE (TREE_TYPE (otype)) != FUNCTION_TYPE
5117 && TYPE_ALIGN (TREE_TYPE (type)) > TYPE_ALIGN (TREE_TYPE (otype)))
5118 warning ("cast increases required alignment of target type");
5121 /* We should see about re-enabling these, they seem useful to
5123 if (TREE_CODE (type) == INTEGER_TYPE
5124 && TREE_CODE (otype) == POINTER_TYPE
5125 && TYPE_PRECISION (type) != TYPE_PRECISION (otype))
5126 warning ("cast from pointer to integer of different size");
5128 if (TREE_CODE (type) == POINTER_TYPE
5129 && TREE_CODE (otype) == INTEGER_TYPE
5130 && TYPE_PRECISION (type) != TYPE_PRECISION (otype)
5131 /* Don't warn about converting 0 to pointer,
5132 provided the 0 was explicit--not cast or made by folding. */
5133 && !(TREE_CODE (value) == INTEGER_CST && integer_zerop (value)))
5134 warning ("cast to pointer from integer of different size");
5137 flag = allow_nonconverting ? CONV_NONCONVERTING : 0;
5139 if (TREE_CODE (type) == REFERENCE_TYPE)
5140 value = (convert_from_reference
5141 (convert_to_reference (type, value, CONV_OLD_CONVERT|flag,
5142 LOOKUP_COMPLAIN, NULL_TREE)));
5147 if (TREE_READONLY_DECL_P (value))
5148 value = decl_constant_value (value);
5151 value = convert_force (type, value, flag);
5153 /* Ignore any integer overflow caused by the cast. */
5154 if (TREE_CODE (value) == INTEGER_CST)
5156 TREE_OVERFLOW (value) = TREE_OVERFLOW (ovalue);
5157 TREE_CONSTANT_OVERFLOW (value) = TREE_CONSTANT_OVERFLOW (ovalue);
5162 /* Always produce some operator for an explicit cast,
5163 so we can tell (for -pedantic) that the cast is no lvalue.
5164 Also, pedantically, don't let (void *) (FOO *) 0 be a null
5165 pointer constant. */
5166 if (TREE_CODE (type) != REFERENCE_TYPE
5169 && TREE_CODE (value) == INTEGER_CST
5170 && TREE_CODE (expr) == INTEGER_CST
5171 && TREE_CODE (TREE_TYPE (expr)) != INTEGER_TYPE)))
5172 value = non_lvalue (value);
5178 expand_target_expr (t)
5181 extern int temp_slot_level;
5182 extern int target_temp_slot_level;
5183 int old_temp_level = target_temp_slot_level;
5185 tree xval = make_node (RTL_EXPR);
5188 /* Any TARGET_EXPR temps live only as long as the outer temp level.
5189 Since they are preserved in this new inner level, we know they
5190 will make it into the outer level. */
5192 target_temp_slot_level = temp_slot_level;
5194 do_pending_stack_adjust ();
5195 start_sequence_for_rtl_expr (xval);
5197 rtxval = expand_expr (t, NULL, VOIDmode, 0);
5198 do_pending_stack_adjust ();
5199 TREE_SIDE_EFFECTS (xval) = 1;
5200 RTL_EXPR_SEQUENCE (xval) = get_insns ();
5202 RTL_EXPR_RTL (xval) = rtxval;
5203 TREE_TYPE (xval) = TREE_TYPE (t);
5206 target_temp_slot_level = old_temp_level;
5211 /* Build an assignment expression of lvalue LHS from value RHS.
5212 MODIFYCODE is the code for a binary operator that we use
5213 to combine the old value of LHS with RHS to get the new value.
5214 Or else MODIFYCODE is NOP_EXPR meaning do a simple assignment.
5216 C++: If MODIFYCODE is INIT_EXPR, then leave references unbashed.
5219 build_modify_expr (lhs, modifycode, rhs)
5221 enum tree_code modifycode;
5224 register tree result;
5226 tree lhstype = TREE_TYPE (lhs);
5227 tree olhstype = lhstype;
5230 /* Avoid duplicate error messages from operands that had errors. */
5231 if (TREE_CODE (lhs) == ERROR_MARK || TREE_CODE (rhs) == ERROR_MARK)
5232 return error_mark_node;
5234 /* Types that aren't fully specified cannot be used in assignments. */
5235 lhs = require_complete_type (lhs);
5239 /* Handle assignment to signature pointers/refs. */
5241 if (TYPE_LANG_SPECIFIC (lhstype) &&
5242 (IS_SIGNATURE_POINTER (lhstype) || IS_SIGNATURE_REFERENCE (lhstype)))
5244 return build_signature_pointer_constructor (lhs, rhs);
5247 /* Handle control structure constructs used as "lvalues". */
5249 switch (TREE_CODE (lhs))
5251 /* Handle --foo = 5; as these are valid constructs in C++ */
5252 case PREDECREMENT_EXPR:
5253 case PREINCREMENT_EXPR:
5254 if (TREE_SIDE_EFFECTS (TREE_OPERAND (lhs, 0)))
5255 lhs = build (TREE_CODE (lhs), TREE_TYPE (lhs),
5256 stabilize_reference (TREE_OPERAND (lhs, 0)));
5257 return build (COMPOUND_EXPR, lhstype,
5259 build_modify_expr (TREE_OPERAND (lhs, 0),
5262 /* Handle (a, b) used as an "lvalue". */
5264 newrhs = build_modify_expr (TREE_OPERAND (lhs, 1),
5266 if (TREE_CODE (newrhs) == ERROR_MARK)
5267 return error_mark_node;
5268 return build (COMPOUND_EXPR, lhstype,
5269 TREE_OPERAND (lhs, 0), newrhs);
5272 newrhs = build_modify_expr (TREE_OPERAND (lhs, 0), modifycode, rhs);
5273 if (TREE_CODE (newrhs) == ERROR_MARK)
5274 return error_mark_node;
5275 return build (COMPOUND_EXPR, lhstype, lhs, newrhs);
5277 /* Handle (a ? b : c) used as an "lvalue". */
5279 rhs = save_expr (rhs);
5281 /* Produce (a ? (b = rhs) : (c = rhs))
5282 except that the RHS goes through a save-expr
5283 so the code to compute it is only emitted once. */
5285 = build_conditional_expr (TREE_OPERAND (lhs, 0),
5286 build_modify_expr (convert (TREE_TYPE (lhs), TREE_OPERAND (lhs, 1)),
5288 build_modify_expr (convert (TREE_TYPE (lhs), TREE_OPERAND (lhs, 2)),
5290 if (TREE_CODE (cond) == ERROR_MARK)
5292 /* Make sure the code to compute the rhs comes out
5293 before the split. */
5294 return build (COMPOUND_EXPR, TREE_TYPE (lhs),
5295 /* Case to void to suppress warning
5296 from warn_if_unused_value. */
5297 convert (void_type_node, rhs), cond);
5301 if (TREE_CODE (lhs) == OFFSET_REF)
5303 if (TREE_OPERAND (lhs, 0) == NULL_TREE)
5305 /* Static class member? */
5306 tree member = TREE_OPERAND (lhs, 1);
5307 if (TREE_CODE (member) == VAR_DECL)
5311 compiler_error ("invalid static class member");
5312 return error_mark_node;
5316 lhs = resolve_offset_ref (lhs);
5318 olhstype = lhstype = TREE_TYPE (lhs);
5321 if (TREE_CODE (lhstype) == REFERENCE_TYPE
5322 && modifycode != INIT_EXPR)
5324 lhs = convert_from_reference (lhs);
5325 olhstype = lhstype = TREE_TYPE (lhs);
5328 /* If a binary op has been requested, combine the old LHS value with the RHS
5329 producing the value we should actually store into the LHS. */
5331 if (modifycode == INIT_EXPR)
5333 if (! IS_AGGR_TYPE (lhstype))
5334 /* Do the default thing */;
5335 else if (! TYPE_HAS_CONSTRUCTOR (lhstype))
5337 cp_error ("`%T' has no constructors", lhstype);
5338 return error_mark_node;
5340 else if (TYPE_HAS_TRIVIAL_INIT_REF (lhstype)
5341 && TYPE_MAIN_VARIANT (lhstype) == TYPE_MAIN_VARIANT (TREE_TYPE (newrhs)))
5342 /* Do the default thing */;
5345 result = build_method_call (lhs, constructor_name_full (lhstype),
5346 build_tree_list (NULL_TREE, rhs),
5347 NULL_TREE, LOOKUP_NORMAL);
5348 if (result == NULL_TREE)
5349 return error_mark_node;
5353 else if (modifycode == NOP_EXPR)
5355 /* `operator=' is not an inheritable operator. */
5356 if (! IS_AGGR_TYPE (lhstype))
5357 /* Do the default thing */;
5358 else if (! TYPE_HAS_ASSIGNMENT (lhstype))
5360 cp_error ("`%T' does not define operator=", lhstype);
5361 return error_mark_node;
5363 else if (TYPE_HAS_TRIVIAL_ASSIGN_REF (lhstype)
5364 && TYPE_MAIN_VARIANT (lhstype) == TYPE_MAIN_VARIANT (TREE_TYPE (newrhs)))
5367 /* If we care about this, do overload resolution. */
5368 build_opfncall (MODIFY_EXPR, LOOKUP_NORMAL,
5369 lhs, rhs, make_node (NOP_EXPR));
5371 /* Do the default thing */;
5375 result = build_opfncall (MODIFY_EXPR, LOOKUP_NORMAL,
5376 lhs, rhs, make_node (NOP_EXPR));
5377 if (result == NULL_TREE)
5378 return error_mark_node;
5383 else if (PROMOTES_TO_AGGR_TYPE (lhstype, REFERENCE_TYPE))
5385 /* This case must convert to some sort of lvalue that
5386 can participate in an op= operation. */
5389 if (build_default_binary_type_conversion (modifycode, &lhs_tmp, &rhs_tmp))
5391 lhs = stabilize_reference (lhs_tmp);
5392 /* Forget it was ever anything else. */
5393 olhstype = lhstype = TREE_TYPE (lhs);
5394 newrhs = build_binary_op (modifycode, lhs, rhs_tmp, 1);
5398 cp_error ("no match for `%O(%#T, %#T)'", modifycode,
5399 TREE_TYPE (lhs), TREE_TYPE (rhs));
5400 return error_mark_node;
5405 lhs = stabilize_reference (lhs);
5406 newrhs = build_binary_op (modifycode, lhs, rhs, 1);
5409 /* Handle a cast used as an "lvalue".
5410 We have already performed any binary operator using the value as cast.
5411 Now convert the result to the cast type of the lhs,
5412 and then true type of the lhs and store it there;
5413 then convert result back to the cast type to be the value
5414 of the assignment. */
5416 switch (TREE_CODE (lhs))
5421 case FIX_TRUNC_EXPR:
5422 case FIX_FLOOR_EXPR:
5423 case FIX_ROUND_EXPR:
5425 if (TREE_CODE (TREE_TYPE (newrhs)) == ARRAY_TYPE
5426 || TREE_CODE (TREE_TYPE (newrhs)) == FUNCTION_TYPE
5427 || TREE_CODE (TREE_TYPE (newrhs)) == METHOD_TYPE
5428 || TREE_CODE (TREE_TYPE (newrhs)) == OFFSET_TYPE)
5429 newrhs = default_conversion (newrhs);
5431 tree inner_lhs = TREE_OPERAND (lhs, 0);
5433 if (! lvalue_p (lhs) && pedantic)
5434 pedwarn ("cast to non-reference type used as lvalue");
5436 result = build_modify_expr (inner_lhs, NOP_EXPR,
5437 convert (TREE_TYPE (inner_lhs),
5438 convert (lhstype, newrhs)));
5439 if (TREE_CODE (result) == ERROR_MARK)
5441 return convert (TREE_TYPE (lhs), result);
5445 /* Now we have handled acceptable kinds of LHS that are not truly lvalues.
5446 Reject anything strange now. */
5448 if (!lvalue_or_else (lhs, "assignment"))
5449 return error_mark_node;
5451 GNU_xref_assign (lhs);
5453 /* Warn about storing in something that is `const'. */
5454 /* For C++, don't warn if this is initialization. */
5455 if (modifycode != INIT_EXPR
5456 /* For assignment to `const' signature pointer/reference fields,
5457 don't warn either, we already printed a better message before. */
5458 && ! (TREE_CODE (lhs) == COMPONENT_REF
5459 && (IS_SIGNATURE_POINTER (TREE_TYPE (TREE_OPERAND (lhs, 0)))
5460 || IS_SIGNATURE_REFERENCE (TREE_TYPE (TREE_OPERAND (lhs, 0)))))
5461 && (TREE_READONLY (lhs) || TYPE_READONLY (lhstype)
5462 || ((TREE_CODE (lhstype) == RECORD_TYPE
5463 || TREE_CODE (lhstype) == UNION_TYPE)
5464 && C_TYPE_FIELDS_READONLY (lhstype))
5465 || (TREE_CODE (lhstype) == REFERENCE_TYPE
5466 && TYPE_READONLY (TREE_TYPE (lhstype)))))
5467 readonly_error (lhs, "assignment", 0);
5469 /* If storing into a structure or union member,
5470 it has probably been given type `int'.
5471 Compute the type that would go with
5472 the actual amount of storage the member occupies. */
5474 if (TREE_CODE (lhs) == COMPONENT_REF
5475 && (TREE_CODE (lhstype) == INTEGER_TYPE
5476 || TREE_CODE (lhstype) == REAL_TYPE
5477 || TREE_CODE (lhstype) == ENUMERAL_TYPE))
5479 lhstype = TREE_TYPE (get_unwidened (lhs, 0));
5481 /* If storing in a field that is in actuality a short or narrower
5482 than one, we must store in the field in its actual type. */
5484 if (lhstype != TREE_TYPE (lhs))
5486 lhs = copy_node (lhs);
5487 TREE_TYPE (lhs) = lhstype;
5491 /* check to see if there is an assignment to `this' */
5492 if (lhs == current_class_decl)
5494 if (flag_this_is_variable > 0
5495 && DECL_NAME (current_function_decl) != NULL_TREE
5496 && (DECL_NAME (current_function_decl)
5497 != constructor_name (current_class_type)))
5498 warning ("assignment to `this' not in constructor or destructor");
5499 current_function_just_assigned_this = 1;
5502 /* The TREE_TYPE of RHS may be TYPE_UNKNOWN. This can happen
5503 when the type of RHS is not yet known, i.e. its type
5504 is inherited from LHS. */
5505 rhs = require_instantiated_type (lhstype, newrhs, error_mark_node);
5506 if (rhs == error_mark_node)
5507 return error_mark_node;
5510 if (modifycode != INIT_EXPR)
5512 /* Make modifycode now either a NOP_EXPR or an INIT_EXPR. */
5513 modifycode = NOP_EXPR;
5514 /* Reference-bashing */
5515 if (TREE_CODE (lhstype) == REFERENCE_TYPE)
5517 tree tmp = convert_from_reference (lhs);
5518 lhstype = TREE_TYPE (tmp);
5519 if (TYPE_SIZE (lhstype) == 0)
5521 incomplete_type_error (lhs, lhstype);
5522 return error_mark_node;
5527 if (TREE_CODE (TREE_TYPE (newrhs)) == REFERENCE_TYPE)
5529 tree tmp = convert_from_reference (newrhs);
5530 if (TYPE_SIZE (TREE_TYPE (tmp)) == 0)
5532 incomplete_type_error (newrhs, TREE_TYPE (tmp));
5533 return error_mark_node;
5539 if (TREE_SIDE_EFFECTS (lhs))
5540 lhs = stabilize_reference (lhs);
5541 if (TREE_SIDE_EFFECTS (newrhs))
5542 newrhs = stabilize_reference (newrhs);
5544 /* Convert new value to destination type. */
5546 if (TREE_CODE (lhstype) == ARRAY_TYPE)
5550 if (! comptypes (lhstype, TREE_TYPE (rhs), 0))
5552 cp_error ("incompatible types in assignment of `%T' to `%T'",
5553 TREE_TYPE (rhs), lhstype);
5554 return error_mark_node;
5557 /* Allow array assignment in compiler-generated code. */
5558 if (pedantic && ! DECL_ARTIFICIAL (current_function_decl))
5559 pedwarn ("ANSI C++ forbids assignment of arrays");
5561 /* Have to wrap this in RTL_EXPR for two cases:
5562 in base or member initialization and if we
5563 are a branch of a ?: operator. Since we
5564 can't easily know the latter, just do it always. */
5566 result = make_node (RTL_EXPR);
5568 TREE_TYPE (result) = void_type_node;
5569 do_pending_stack_adjust ();
5570 start_sequence_for_rtl_expr (result);
5572 /* As a matter of principle, `start_sequence' should do this. */
5575 from_array = TREE_CODE (TREE_TYPE (newrhs)) == ARRAY_TYPE
5576 ? 1 + (modifycode != INIT_EXPR): 0;
5577 expand_vec_init (lhs, lhs, array_type_nelts (lhstype), newrhs,
5580 do_pending_stack_adjust ();
5582 TREE_SIDE_EFFECTS (result) = 1;
5583 RTL_EXPR_SEQUENCE (result) = get_insns ();
5584 RTL_EXPR_RTL (result) = const0_rtx;
5589 if (modifycode == INIT_EXPR)
5591 newrhs = convert_for_initialization (lhs, lhstype, newrhs, LOOKUP_NORMAL,
5592 "assignment", NULL_TREE, 0);
5593 if (lhs == DECL_RESULT (current_function_decl))
5595 if (DECL_INITIAL (lhs))
5596 warning ("return value from function receives multiple initializations");
5597 DECL_INITIAL (lhs) = newrhs;
5602 /* Avoid warnings on enum bit fields. */
5603 if (TREE_CODE (olhstype) == ENUMERAL_TYPE
5604 && TREE_CODE (lhstype) == INTEGER_TYPE)
5606 newrhs = convert_for_assignment (olhstype, newrhs, "assignment",
5608 newrhs = convert_force (lhstype, newrhs, 0);
5611 newrhs = convert_for_assignment (lhstype, newrhs, "assignment",
5613 if (TREE_CODE (newrhs) == CALL_EXPR
5614 && TYPE_NEEDS_CONSTRUCTING (lhstype))
5615 newrhs = build_cplus_new (lhstype, newrhs, 0);
5617 /* Can't initialize directly from a TARGET_EXPR, since that would
5618 cause the lhs to be constructed twice, and possibly result in
5619 accidental self-initialization. So we force the TARGET_EXPR to be
5620 expanded without a target. */
5621 if (TREE_CODE (newrhs) == TARGET_EXPR)
5622 newrhs = build (COMPOUND_EXPR, TREE_TYPE (newrhs), newrhs,
5623 TREE_VALUE (newrhs));
5626 if (TREE_CODE (newrhs) == ERROR_MARK)
5627 return error_mark_node;
5629 if (TREE_CODE (newrhs) == COND_EXPR)
5632 tree cond = TREE_OPERAND (newrhs, 0);
5634 if (TREE_SIDE_EFFECTS (lhs))
5635 cond = build_compound_expr (tree_cons
5637 build_tree_list (NULL_TREE, cond)));
5639 /* Cannot have two identical lhs on this one tree (result) as preexpand
5640 calls will rip them out and fill in RTL for them, but when the
5641 rtl is generated, the calls will only be in the first side of the
5642 condition, not on both, or before the conditional jump! (mrs) */
5643 lhs1 = break_out_calls (lhs);
5646 /* If there's no change, the COND_EXPR behaves like any other rhs. */
5647 result = build (modifycode == NOP_EXPR ? MODIFY_EXPR : INIT_EXPR,
5648 lhstype, lhs, newrhs);
5651 tree result_type = TREE_TYPE (newrhs);
5652 /* We have to convert each arm to the proper type because the
5653 types may have been munged by constant folding. */
5655 = build (COND_EXPR, result_type, cond,
5656 build_modify_expr (lhs, modifycode,
5657 convert (result_type,
5658 TREE_OPERAND (newrhs, 1))),
5659 build_modify_expr (lhs1, modifycode,
5660 convert (result_type,
5661 TREE_OPERAND (newrhs, 2))));
5664 else if (modifycode != INIT_EXPR && TREE_CODE (newrhs) == WITH_CLEANUP_EXPR)
5666 tree cleanup = TREE_OPERAND (newrhs, 2);
5669 /* Finish up by running cleanups and having the "value" of the lhs. */
5670 tree exprlist = tree_cons (NULL_TREE, cleanup,
5671 build_tree_list (NULL_TREE, lhs));
5672 newrhs = TREE_OPERAND (newrhs, 0);
5673 if (TREE_CODE (newrhs) == TARGET_EXPR)
5674 slot = TREE_OPERAND (newrhs, 0);
5675 else if (TREE_CODE (newrhs) == ADDR_EXPR)
5677 /* Bad but valid. */
5679 warning ("address taken of temporary object");
5682 my_friendly_abort (118);
5684 /* Copy the value computed in SLOT into LHS. */
5685 exprlist = tree_cons (NULL_TREE,
5686 build_modify_expr (lhs, modifycode, slot),
5688 /* Evaluate the expression that needs CLEANUP. This will
5689 compute the value into SLOT. */
5690 exprlist = tree_cons (NULL_TREE, newrhs, exprlist);
5691 result = convert (lhstype, build_compound_expr (exprlist));
5694 result = build (modifycode == NOP_EXPR ? MODIFY_EXPR : INIT_EXPR,
5695 lhstype, lhs, newrhs);
5696 TREE_SIDE_EFFECTS (result) = 1;
5698 /* If we got the LHS in a different type for storing in,
5699 convert the result back to the nominal type of LHS
5700 so that the value we return always has the same type
5701 as the LHS argument. */
5703 if (olhstype == TREE_TYPE (result))
5705 /* Avoid warnings converting integral types back into enums
5706 for enum bit fields. */
5707 if (TREE_CODE (TREE_TYPE (result)) == INTEGER_TYPE
5708 && TREE_CODE (olhstype) == ENUMERAL_TYPE)
5710 result = build (COMPOUND_EXPR, olhstype, result, olhs);
5711 TREE_NO_UNUSED_WARNING (result) = 1;
5714 return convert_for_assignment (olhstype, result, "assignment",
5719 /* Return 0 if EXP is not a valid lvalue in this language
5720 even though `lvalue_or_else' would accept it. */
5723 language_lvalue_valid (exp)
5729 /* Get difference in deltas for different pointer to member function
5730 types. Return integer_zero_node, if FROM cannot be converted to a
5731 TO type. If FORCE is true, then allow reverse conversions as well. */
5733 get_delta_difference (from, to, force)
5737 tree delta = integer_zero_node;
5743 /* Should get_base_distance here, so we can check if any thing along the
5744 path is virtual, and we need to make sure we stay
5745 inside the real binfos when going through virtual bases.
5746 Maybe we should replace virtual bases with
5747 binfo_member (...CLASSTYPE_VBASECLASSES...)... (mrs) */
5748 binfo = get_binfo (from, to, 1);
5749 if (binfo == error_mark_node)
5751 error (" in pointer to member function conversion");
5758 error_not_base_type (from, to);
5759 error (" in pointer to member function conversion");
5762 binfo = get_binfo (to, from, 1);
5763 if (binfo == error_mark_node)
5765 error (" in pointer to member function conversion");
5770 error ("cannot convert pointer to member of type %T to unrelated pointer to member of type %T", from, to);
5773 if (TREE_VIA_VIRTUAL (binfo))
5775 warning ("pointer to member conversion to virtual base class will only work if you are very careful");
5777 return build_binary_op (MINUS_EXPR,
5779 BINFO_OFFSET (binfo), 1);
5781 if (TREE_VIA_VIRTUAL (binfo))
5783 warning ("pointer to member conversion from virtual base class will only work if you are very careful");
5785 return BINFO_OFFSET (binfo);
5788 /* Build a constructor for a pointer to member function. It can be
5789 used to initialize global variables, local variable, or used
5790 as a value in expressions. TYPE is the POINTER to METHOD_TYPE we
5793 If FORCE is non-zero, then force this conversion, even if
5794 we would rather not do it. Usually set when using an explicit
5797 Return error_mark_node, if something goes wrong. */
5800 build_ptrmemfunc (type, pfn, force)
5804 tree index = integer_zero_node;
5805 tree delta = integer_zero_node;
5806 tree delta2 = integer_zero_node;
5811 /* Handle multiple conversions of pointer to member functions. */
5812 if (TYPE_PTRMEMFUNC_P (TREE_TYPE (pfn)))
5814 tree ndelta, ndelta2, nindex;
5815 /* Is is already the right type? */
5816 if (type == TYPE_PTRMEMFUNC_FN_TYPE (TREE_TYPE (pfn)))
5819 if (TREE_CODE (pfn) != CONSTRUCTOR)
5822 ndelta = convert (ptrdiff_type_node, build_component_ref (pfn, delta_identifier, 0, 0));
5823 ndelta2 = convert (ptrdiff_type_node, DELTA2_FROM_PTRMEMFUNC (pfn));
5824 index = build_component_ref (pfn, index_identifier, 0, 0);
5825 delta = get_delta_difference (TYPE_METHOD_BASETYPE (TREE_TYPE (TYPE_PTRMEMFUNC_FN_TYPE (TREE_TYPE (pfn)))),
5826 TYPE_METHOD_BASETYPE (TREE_TYPE (type)),
5828 delta = build_binary_op (PLUS_EXPR, delta, ndelta, 1);
5829 delta2 = build_binary_op (PLUS_EXPR, ndelta2, delta2, 1);
5830 e1 = fold (build (GT_EXPR, boolean_type_node, index, integer_zero_node));
5832 u = build_nt (CONSTRUCTOR, 0, tree_cons (delta2_identifier, delta2, NULL_TREE));
5833 u = build_nt (CONSTRUCTOR, 0, tree_cons (NULL_TREE, delta,
5834 tree_cons (NULL_TREE, index,
5835 tree_cons (NULL_TREE, u, NULL_TREE))));
5836 e2 = digest_init (TYPE_GET_PTRMEMFUNC_TYPE (type), u, (tree*)0);
5838 pfn = PFN_FROM_PTRMEMFUNC (pfn);
5839 npfn = build1 (NOP_EXPR, type, pfn);
5840 TREE_CONSTANT (npfn) = TREE_CONSTANT (pfn);
5842 u = build_nt (CONSTRUCTOR, 0, tree_cons (pfn_identifier, npfn, NULL_TREE));
5843 u = build_nt (CONSTRUCTOR, 0, tree_cons (NULL_TREE, delta,
5844 tree_cons (NULL_TREE, index,
5845 tree_cons (NULL_TREE, u, NULL_TREE))));
5846 e3 = digest_init (TYPE_GET_PTRMEMFUNC_TYPE (type), u, (tree*)0);
5847 return build_conditional_expr (e1, e2, e3);
5850 ndelta = TREE_VALUE (CONSTRUCTOR_ELTS (pfn));
5851 nindex = TREE_VALUE (TREE_CHAIN (CONSTRUCTOR_ELTS (pfn)));
5852 npfn = TREE_VALUE (TREE_CHAIN (TREE_CHAIN (CONSTRUCTOR_ELTS (pfn))));
5853 npfn = TREE_VALUE (CONSTRUCTOR_ELTS (npfn));
5854 if (integer_zerop (nindex))
5855 pfn = integer_zero_node;
5856 else if (integer_zerop (fold (size_binop (PLUS_EXPR, nindex, integer_one_node))))
5859 delta = get_delta_difference (TYPE_METHOD_BASETYPE (TREE_TYPE (TYPE_PTRMEMFUNC_FN_TYPE (TREE_TYPE (pfn)))),
5860 TYPE_METHOD_BASETYPE (TREE_TYPE (type)),
5862 delta = build_binary_op (PLUS_EXPR, delta, ndelta, 1);
5863 pfn = build1 (NOP_EXPR, type, npfn);
5864 TREE_CONSTANT (pfn) = TREE_CONSTANT (npfn);
5866 u = build_nt (CONSTRUCTOR, 0, tree_cons (pfn_identifier, pfn, NULL_TREE));
5867 u = build_nt (CONSTRUCTOR, 0, tree_cons (NULL_TREE, delta,
5868 tree_cons (NULL_TREE, nindex,
5869 tree_cons (NULL_TREE, u, NULL_TREE))));
5870 e3 = digest_init (TYPE_GET_PTRMEMFUNC_TYPE (type), u, (tree*)0);
5875 sorry ("value casting of variable nonnull pointer to member functions not supported");
5876 return error_mark_node;
5880 /* Handle null pointer to member function conversions. */
5881 if (integer_zerop (pfn))
5883 pfn = build_c_cast (type, integer_zero_node, 0);
5884 u = build_nt (CONSTRUCTOR, 0, tree_cons (pfn_identifier, pfn, NULL_TREE));
5885 u = build_nt (CONSTRUCTOR, 0, tree_cons (NULL_TREE, integer_zero_node,
5886 tree_cons (NULL_TREE, integer_zero_node,
5887 tree_cons (NULL_TREE, u, NULL_TREE))));
5888 return digest_init (TYPE_GET_PTRMEMFUNC_TYPE (type), u, (tree*)0);
5891 if (TREE_CODE (pfn) == TREE_LIST
5892 || (TREE_CODE (pfn) == ADDR_EXPR
5893 && TREE_CODE (TREE_OPERAND (pfn, 0)) == TREE_LIST))
5895 pfn = instantiate_type (type, pfn, 1);
5896 if (pfn == error_mark_node)
5897 return error_mark_node;
5898 if (TREE_CODE (pfn) != ADDR_EXPR)
5899 pfn = build_unary_op (ADDR_EXPR, pfn, 0);
5902 /* Allow pointer to member conversions here. */
5903 delta = get_delta_difference (TYPE_METHOD_BASETYPE (TREE_TYPE (TREE_TYPE (pfn))),
5904 TYPE_METHOD_BASETYPE (TREE_TYPE (type)),
5906 delta2 = build_binary_op (PLUS_EXPR, delta2, delta, 1);
5908 if (TREE_CODE (TREE_OPERAND (pfn, 0)) != FUNCTION_DECL)
5909 warning ("assuming pointer to member function is non-virtual");
5911 if (TREE_CODE (TREE_OPERAND (pfn, 0)) == FUNCTION_DECL
5912 && DECL_VINDEX (TREE_OPERAND (pfn, 0)))
5914 /* Find the offset to the vfield pointer in the object. */
5915 vfield_offset = get_binfo (DECL_CONTEXT (TREE_OPERAND (pfn, 0)),
5916 DECL_CLASS_CONTEXT (TREE_OPERAND (pfn, 0)),
5918 vfield_offset = get_vfield_offset (vfield_offset);
5919 delta2 = size_binop (PLUS_EXPR, vfield_offset, delta2);
5921 /* Map everything down one to make room for the null pointer to member. */
5922 index = size_binop (PLUS_EXPR,
5923 DECL_VINDEX (TREE_OPERAND (pfn, 0)),
5925 u = build_nt (CONSTRUCTOR, 0, tree_cons (delta2_identifier, delta2, NULL_TREE));
5929 index = size_binop (MINUS_EXPR, integer_zero_node, integer_one_node);
5931 npfn = build1 (NOP_EXPR, type, pfn);
5932 TREE_CONSTANT (npfn) = TREE_CONSTANT (pfn);
5934 u = build_nt (CONSTRUCTOR, 0, tree_cons (pfn_identifier, npfn, NULL_TREE));
5937 u = build_nt (CONSTRUCTOR, 0, tree_cons (NULL_TREE, delta,
5938 tree_cons (NULL_TREE, index,
5939 tree_cons (NULL_TREE, u, NULL_TREE))));
5940 return digest_init (TYPE_GET_PTRMEMFUNC_TYPE (type), u, (tree*)0);
5943 /* Convert value RHS to type TYPE as preparation for an assignment
5944 to an lvalue of type TYPE.
5945 The real work of conversion is done by `convert'.
5946 The purpose of this function is to generate error messages
5947 for assignments that are not allowed in C.
5948 ERRTYPE is a string to use in error messages:
5949 "assignment", "return", etc.
5951 C++: attempts to allow `convert' to find conversions involving
5952 implicit type conversion between aggregate and scalar types
5953 as per 8.5.6 of C++ manual. Does not randomly dereference
5954 pointers to aggregates! */
5957 convert_for_assignment (type, rhs, errtype, fndecl, parmnum)
5963 register enum tree_code codel = TREE_CODE (type);
5964 register tree rhstype;
5965 register enum tree_code coder = TREE_CODE (TREE_TYPE (rhs));
5967 if (coder == UNKNOWN_TYPE)
5968 rhs = instantiate_type (type, rhs, 1);
5970 if (coder == ERROR_MARK)
5971 return error_mark_node;
5973 if (codel == OFFSET_TYPE)
5975 type = TREE_TYPE (type);
5976 codel = TREE_CODE (type);
5979 /* Strip NON_LVALUE_EXPRs since we aren't using as an lvalue. */
5980 if (TREE_CODE (rhs) == NON_LVALUE_EXPR)
5981 rhs = TREE_OPERAND (rhs, 0);
5983 if (rhs == error_mark_node)
5984 return error_mark_node;
5986 if (TREE_VALUE (rhs) == error_mark_node)
5987 return error_mark_node;
5989 if (TREE_CODE (TREE_TYPE (rhs)) == OFFSET_TYPE)
5991 rhs = resolve_offset_ref (rhs);
5992 if (rhs == error_mark_node)
5993 return error_mark_node;
5994 rhstype = TREE_TYPE (rhs);
5995 coder = TREE_CODE (rhstype);
5998 if (TREE_CODE (TREE_TYPE (rhs)) == ARRAY_TYPE
5999 || TREE_CODE (TREE_TYPE (rhs)) == FUNCTION_TYPE
6000 || TREE_CODE (TREE_TYPE (rhs)) == METHOD_TYPE)
6001 rhs = default_conversion (rhs);
6002 else if (TREE_CODE (TREE_TYPE (rhs)) == REFERENCE_TYPE)
6003 rhs = convert_from_reference (rhs);
6005 rhstype = TREE_TYPE (rhs);
6006 coder = TREE_CODE (rhstype);
6008 /* This should no longer change types on us. */
6009 if (TREE_CODE (rhs) == CONST_DECL)
6010 rhs = DECL_INITIAL (rhs);
6011 else if (TREE_READONLY_DECL_P (rhs))
6012 rhs = decl_constant_value (rhs);
6014 if (type == rhstype)
6016 overflow_warning (rhs);
6020 if (coder == VOID_TYPE)
6022 error ("void value not ignored as it ought to be");
6023 return error_mark_node;
6025 /* Arithmetic types all interconvert. */
6026 if ((codel == INTEGER_TYPE || codel == REAL_TYPE || codel == BOOLEAN_TYPE)
6027 && (coder == INTEGER_TYPE || coder == REAL_TYPE || coder == BOOLEAN_TYPE))
6029 /* But we should warn if assigning REAL_TYPE to INTEGER_TYPE. */
6030 if (coder == REAL_TYPE && codel == INTEGER_TYPE)
6033 cp_warning ("`%T' used for argument %P of `%D'",
6034 rhstype, parmnum, fndecl);
6036 cp_warning ("%s to `%T' from `%T'", errtype, type, rhstype);
6038 /* And we should warn if assigning a negative value to
6039 an unsigned variable. */
6040 else if (TREE_UNSIGNED (type) && codel != BOOLEAN_TYPE)
6042 if (TREE_CODE (rhs) == INTEGER_CST
6043 && TREE_NEGATED_INT (rhs))
6046 cp_warning ("negative value `%E' passed as argument %P of `%D'",
6047 rhs, parmnum, fndecl);
6049 cp_warning ("%s of negative value `%E' to `%T'",
6050 errtype, rhs, type);
6052 overflow_warning (rhs);
6053 if (TREE_CONSTANT (rhs))
6057 return convert_and_check (type, rhs);
6059 /* Conversions involving enums. */
6060 else if ((codel == ENUMERAL_TYPE
6061 && (INTEGRAL_CODE_P (coder) || coder == REAL_TYPE))
6062 || (coder == ENUMERAL_TYPE
6063 && (INTEGRAL_CODE_P (codel) || codel == REAL_TYPE)))
6065 return cp_convert (type, rhs, CONV_IMPLICIT, LOOKUP_NORMAL);
6067 /* Conversions among pointers */
6068 else if (codel == POINTER_TYPE
6069 && (coder == POINTER_TYPE
6070 || (coder == RECORD_TYPE
6071 && (IS_SIGNATURE_POINTER (rhstype)
6072 || IS_SIGNATURE_REFERENCE (rhstype)))))
6074 register tree ttl = TREE_TYPE (type);
6078 if (coder == RECORD_TYPE)
6080 rhs = build_optr_ref (rhs);
6081 rhstype = TREE_TYPE (rhs);
6083 ttr = TREE_TYPE (rhstype);
6085 /* If both pointers are of aggregate type, then we
6086 can give better error messages, and save some work
6088 if (TREE_CODE (ttl) == RECORD_TYPE && TREE_CODE (ttr) == RECORD_TYPE)
6092 if (TYPE_MAIN_VARIANT (ttl) == TYPE_MAIN_VARIANT (ttr)
6093 || type == class_star_type_node
6094 || rhstype == class_star_type_node)
6095 binfo = TYPE_BINFO (ttl);
6097 binfo = get_binfo (ttl, ttr, 1);
6099 if (binfo == error_mark_node)
6100 return error_mark_node;
6102 return error_not_base_type (ttl, ttr);
6104 if (! TYPE_READONLY (ttl) && TYPE_READONLY (ttr))
6107 cp_pedwarn ("passing `%T' as argument %P of `%D' discards const",
6108 rhstype, parmnum, fndecl);
6110 cp_pedwarn ("%s to `%T' from `%T' discards const",
6111 errtype, type, rhstype);
6113 if (! TYPE_VOLATILE (ttl) && TYPE_VOLATILE (ttr))
6116 cp_pedwarn ("passing `%T' as argument %P of `%D' discards volatile",
6117 rhstype, parmnum, fndecl);
6119 cp_pedwarn ("%s to `%T' from `%T' discards volatile",
6120 errtype, type, rhstype);
6124 /* Any non-function converts to a [const][volatile] void *
6125 and vice versa; otherwise, targets must be the same.
6126 Meanwhile, the lhs target must have all the qualifiers of the rhs. */
6127 else if (TYPE_MAIN_VARIANT (ttl) == void_type_node
6128 || TYPE_MAIN_VARIANT (ttr) == void_type_node
6129 || (ctt = comp_target_types (type, rhstype, 1))
6130 || (unsigned_type (TYPE_MAIN_VARIANT (ttl))
6131 == unsigned_type (TYPE_MAIN_VARIANT (ttr))))
6133 /* ARM $4.8, commentary on p39. */
6134 if (TYPE_MAIN_VARIANT (ttl) == void_type_node
6135 && TREE_CODE (ttr) == OFFSET_TYPE)
6137 cp_error ("no standard conversion from `%T' to `void *'", ttr);
6138 return error_mark_node;
6142 cp_pedwarn ("converting `%T' to `%T' is a contravariance violation",
6145 if (TYPE_MAIN_VARIANT (ttl) != void_type_node
6146 && TYPE_MAIN_VARIANT (ttr) == void_type_node
6147 && rhs != null_pointer_node)
6149 if (coder == RECORD_TYPE)
6150 cp_pedwarn ("implicit conversion of signature pointer to type `%T'",
6153 pedwarn ("ANSI C++ forbids implicit conversion from `void *' in %s",
6156 /* Const and volatile mean something different for function types,
6157 so the usual warnings are not appropriate. */
6158 else if ((TREE_CODE (ttr) != FUNCTION_TYPE && TREE_CODE (ttr) != METHOD_TYPE)
6159 || (TREE_CODE (ttl) != FUNCTION_TYPE && TREE_CODE (ttl) != METHOD_TYPE))
6161 if (TREE_CODE (ttl) == OFFSET_TYPE
6162 && binfo_member (TYPE_OFFSET_BASETYPE (ttr),
6163 CLASSTYPE_VBASECLASSES (TYPE_OFFSET_BASETYPE (ttl))))
6165 sorry ("%s between pointer to members converting across virtual baseclasses", errtype);
6166 return error_mark_node;
6168 else if (! TYPE_READONLY (ttl) && TYPE_READONLY (ttr))
6171 cp_pedwarn ("passing `%T' as argument %P of `%D' discards const",
6172 rhstype, parmnum, fndecl);
6174 cp_pedwarn ("%s to `%T' from `%T' discards const",
6175 errtype, type, rhstype);
6177 else if (! TYPE_VOLATILE (ttl) && TYPE_VOLATILE (ttr))
6180 cp_pedwarn ("passing `%T' as argument %P of `%D' discards volatile",
6181 rhstype, parmnum, fndecl);
6183 cp_pedwarn ("%s to `%T' from `%T' discards volatile",
6184 errtype, type, rhstype);
6186 else if (TREE_CODE (ttl) == TREE_CODE (ttr)
6187 && ! comp_target_types (type, rhstype, 1))
6190 cp_pedwarn ("passing `%T' as argument %P of `%D' changes signedness",
6191 rhstype, parmnum, fndecl);
6193 cp_pedwarn ("%s to `%T' from `%T' changes signedness",
6194 errtype, type, rhstype);
6198 else if (TREE_CODE (ttr) == OFFSET_TYPE
6199 && TREE_CODE (ttl) != OFFSET_TYPE)
6201 /* Normally, pointers to different type codes (other
6202 than void) are not compatible, but we perform
6203 some type instantiation if that resolves the
6204 ambiguity of (X Y::*) and (X *). */
6206 if (current_class_decl)
6208 if (TREE_CODE (rhs) == INTEGER_CST)
6210 rhs = build (PLUS_EXPR, build_pointer_type (TREE_TYPE (ttr)),
6211 current_class_decl, rhs);
6212 return convert_for_assignment (type, rhs,
6213 errtype, fndecl, parmnum);
6216 if (TREE_CODE (ttl) == METHOD_TYPE)
6217 error ("%s between pointer-to-method and pointer-to-member types",
6220 error ("%s between pointer and pointer-to-member types", errtype);
6221 return error_mark_node;
6225 int add_quals = 0, const_parity = 0, volatile_parity = 0;
6227 int unsigned_parity;
6230 /* This code is basically a duplicate of comp_ptr_ttypes_real. */
6231 for (; ; ttl = TREE_TYPE (ttl), ttr = TREE_TYPE (ttr))
6234 const_parity |= TYPE_READONLY (ttl) < TYPE_READONLY (ttr);
6235 volatile_parity |= TYPE_VOLATILE (ttl) < TYPE_VOLATILE (ttr);
6238 && (TYPE_READONLY (ttl) > TYPE_READONLY (ttr)
6239 || TYPE_VOLATILE (ttl) > TYPE_VOLATILE (ttr)))
6241 left_const &= TYPE_READONLY (ttl);
6243 if (TREE_CODE (ttl) != POINTER_TYPE
6244 || TREE_CODE (ttr) != POINTER_TYPE)
6247 unsigned_parity = TREE_UNSIGNED (ttl) - TREE_UNSIGNED (ttr);
6248 if (unsigned_parity)
6250 if (TREE_UNSIGNED (ttl))
6251 ttr = unsigned_type (ttr);
6253 ttl = unsigned_type (ttl);
6256 if (comp_target_types (ttl, ttr, nptrs) > 0)
6261 cp_pedwarn ("passing `%T' as argument %P of `%D' adds cv-quals without intervening `const'",
6262 rhstype, parmnum, fndecl);
6264 cp_pedwarn ("%s to `%T' from `%T' adds cv-quals without intervening `const'",
6265 errtype, type, rhstype);
6270 cp_pedwarn ("passing `%T' as argument %P of `%D' discards const",
6271 rhstype, parmnum, fndecl);
6273 cp_pedwarn ("%s to `%T' from `%T' discards const",
6274 errtype, type, rhstype);
6276 if (volatile_parity)
6279 cp_pedwarn ("passing `%T' as argument %P of `%D' discards volatile",
6280 rhstype, parmnum, fndecl);
6282 cp_pedwarn ("%s to `%T' from `%T' discards volatile",
6283 errtype, type, rhstype);
6285 if (unsigned_parity > 0)
6288 cp_pedwarn ("passing `%T' as argument %P of `%D' changes signed to unsigned",
6289 rhstype, parmnum, fndecl);
6291 cp_pedwarn ("%s to `%T' from `%T' changes signed to unsigned",
6292 errtype, type, rhstype);
6294 else if (unsigned_parity < 0)
6297 cp_pedwarn ("passing `%T' as argument %P of `%D' changes unsigned to signed",
6298 rhstype, parmnum, fndecl);
6300 cp_pedwarn ("%s to `%T' from `%T' changes unsigned to signed",
6301 errtype, type, rhstype);
6304 /* C++ is not so friendly about converting function and
6305 member function pointers as C. Emit warnings here. */
6306 if (TREE_CODE (ttl) == FUNCTION_TYPE
6307 || TREE_CODE (ttl) == METHOD_TYPE)
6308 if (! comptypes (ttl, ttr, 0))
6310 warning ("conflicting function types in %s:", errtype);
6311 cp_warning ("\t`%T' != `%T'", type, rhstype);
6314 else if (TREE_CODE (TREE_TYPE (rhs)) == METHOD_TYPE)
6316 /* When does this happen? */
6317 my_friendly_abort (119);
6318 /* Conversion of a pointer-to-member type to void *. */
6319 rhs = build_unary_op (ADDR_EXPR, rhs, 0);
6320 TREE_TYPE (rhs) = type;
6323 else if (TREE_CODE (TREE_TYPE (rhs)) == OFFSET_TYPE)
6325 /* When does this happen? */
6326 my_friendly_abort (120);
6327 /* Conversion of a pointer-to-member type to void *. */
6328 rhs = build_unary_op (ADDR_EXPR, rhs, 0);
6329 TREE_TYPE (rhs) = type;
6335 cp_error ("passing `%T' as argument %P of `%D'",
6336 rhstype, parmnum, fndecl);
6338 cp_error ("%s to `%T' from `%T'", errtype, type, rhstype);
6339 return error_mark_node;
6342 return convert (type, rhs);
6344 else if (codel == POINTER_TYPE && coder == INTEGER_TYPE)
6346 /* An explicit constant 0 can convert to a pointer,
6347 but not a 0 that results from casting or folding. */
6348 if (! (TREE_CODE (rhs) == INTEGER_CST && integer_zerop (rhs)))
6351 cp_pedwarn ("passing `%T' to argument %P of `%D' lacks a cast",
6352 rhstype, parmnum, fndecl);
6354 cp_pedwarn ("%s to `%T' from `%T' lacks a cast",
6355 errtype, type, rhstype);
6356 return convert (type, rhs);
6358 return null_pointer_node;
6360 else if (codel == INTEGER_TYPE
6361 && (coder == POINTER_TYPE
6362 || (coder == RECORD_TYPE
6363 && (IS_SIGNATURE_POINTER (rhstype)
6364 || TYPE_PTRMEMFUNC_FLAG (rhstype)
6365 || IS_SIGNATURE_REFERENCE (rhstype)))))
6368 cp_pedwarn ("passing `%T' to argument %P of `%D' lacks a cast",
6369 rhstype, parmnum, fndecl);
6371 cp_pedwarn ("%s to `%T' from `%T' lacks a cast",
6372 errtype, type, rhstype);
6373 return convert (type, rhs);
6375 else if (codel == BOOLEAN_TYPE
6376 && (coder == POINTER_TYPE
6377 || (coder == RECORD_TYPE
6378 && (IS_SIGNATURE_POINTER (rhstype)
6379 || TYPE_PTRMEMFUNC_FLAG (rhstype)
6380 || IS_SIGNATURE_REFERENCE (rhstype)))))
6381 return convert (type, rhs);
6384 else if (((coder == POINTER_TYPE
6385 && TREE_CODE (TREE_TYPE (rhstype)) == METHOD_TYPE)
6386 || integer_zerop (rhs)
6387 || TYPE_PTRMEMFUNC_P (rhstype))
6388 && TYPE_PTRMEMFUNC_P (type))
6390 tree ttl = TYPE_PTRMEMFUNC_FN_TYPE (type);
6391 tree ttr = (TREE_CODE (rhstype) == POINTER_TYPE ? rhstype
6392 : TYPE_PTRMEMFUNC_FN_TYPE (type));
6393 int ctt = comp_target_types (ttl, ttr, 1);
6396 cp_pedwarn ("converting `%T' to `%T' is a contravariance violation",
6399 cp_error ("%s to `%T' from `%T'", errtype, ttl, ttr);
6401 /* compatible pointer to member functions. */
6402 return build_ptrmemfunc (ttl, rhs, 0);
6404 else if (codel == ERROR_MARK || coder == ERROR_MARK)
6405 return error_mark_node;
6407 /* This should no longer happen. References are initialized via
6408 `convert_for_initialization'. They should otherwise be
6409 bashed before coming here. */
6410 else if (codel == REFERENCE_TYPE)
6411 my_friendly_abort (317);
6412 else if (TYPE_MAIN_VARIANT (type) == TYPE_MAIN_VARIANT (TREE_TYPE (rhs)))
6414 tree nrhs = build1 (NOP_EXPR, type, rhs);
6415 TREE_CONSTANT (nrhs) = TREE_CONSTANT (rhs);
6418 else if (TYPE_HAS_CONSTRUCTOR (type) || IS_AGGR_TYPE (TREE_TYPE (rhs)))
6419 return convert (type, rhs);
6421 cp_error ("%s to `%T' from `%T'", errtype, type, rhstype);
6422 return error_mark_node;
6425 /* Convert RHS to be of type TYPE. If EXP is non-zero,
6426 it is the target of the initialization.
6427 ERRTYPE is a string to use in error messages.
6429 Two major differences between the behavior of
6430 `convert_for_assignment' and `convert_for_initialization'
6431 are that references are bashed in the former, while
6432 copied in the latter, and aggregates are assigned in
6433 the former (operator=) while initialized in the
6436 If using constructor make sure no conversion operator exists, if one does
6437 exist, an ambiguity exists.
6439 If flags doesn't include LOOKUP_COMPLAIN, don't complain about anything. */
6441 convert_for_initialization (exp, type, rhs, flags, errtype, fndecl, parmnum)
6442 tree exp, type, rhs;
6448 register enum tree_code codel = TREE_CODE (type);
6449 register tree rhstype;
6450 register enum tree_code coder;
6452 /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
6453 Strip such NOP_EXPRs, since RHS is used in non-lvalue context. */
6454 if (TREE_CODE (rhs) == NOP_EXPR
6455 && TREE_TYPE (rhs) == TREE_TYPE (TREE_OPERAND (rhs, 0))
6456 && codel != REFERENCE_TYPE)
6457 rhs = TREE_OPERAND (rhs, 0);
6459 if (rhs == error_mark_node
6460 || (TREE_CODE (rhs) == TREE_LIST && TREE_VALUE (rhs) == error_mark_node))
6461 return error_mark_node;
6463 if (TREE_CODE (TREE_TYPE (rhs)) == OFFSET_TYPE)
6465 rhs = resolve_offset_ref (rhs);
6466 if (rhs == error_mark_node)
6467 return error_mark_node;
6470 if (TREE_CODE (TREE_TYPE (rhs)) == REFERENCE_TYPE)
6471 rhs = convert_from_reference (rhs);
6473 if ((TREE_CODE (TREE_TYPE (rhs)) == ARRAY_TYPE
6474 && TREE_CODE (type) != ARRAY_TYPE
6475 && (TREE_CODE (type) != REFERENCE_TYPE
6476 || TREE_CODE (TREE_TYPE (type)) != ARRAY_TYPE))
6477 || TREE_CODE (TREE_TYPE (rhs)) == FUNCTION_TYPE
6478 || TREE_CODE (TREE_TYPE (rhs)) == METHOD_TYPE)
6479 rhs = default_conversion (rhs);
6481 rhstype = TREE_TYPE (rhs);
6482 coder = TREE_CODE (rhstype);
6484 if (coder == UNKNOWN_TYPE)
6486 rhs = instantiate_type (type, rhs, 1);
6487 rhstype = TREE_TYPE (rhs);
6488 coder = TREE_CODE (rhstype);
6491 if (coder == ERROR_MARK)
6492 return error_mark_node;
6494 /* We accept references to incomplete types, so we can
6495 return here before checking if RHS is of complete type. */
6497 if (codel == REFERENCE_TYPE)
6499 /* This should eventually happen in convert_arguments. */
6500 extern int warningcount, errorcount;
6504 savew = warningcount, savee = errorcount;
6505 rhs = convert_to_reference (type, rhs, CONV_IMPLICIT, flags,
6506 exp ? exp : error_mark_node);
6509 if (warningcount > savew)
6510 cp_warning_at ("in passing argument %P of `%+D'", parmnum, fndecl);
6511 else if (errorcount > savee)
6512 cp_error_at ("in passing argument %P of `%+D'", parmnum, fndecl);
6517 rhs = require_complete_type (rhs);
6518 if (rhs == error_mark_node)
6519 return error_mark_node;
6521 if (exp != 0) exp = require_complete_type (exp);
6522 if (exp == error_mark_node)
6523 return error_mark_node;
6525 if (TREE_CODE (rhstype) == REFERENCE_TYPE)
6526 rhstype = TREE_TYPE (rhstype);
6528 if (TYPE_LANG_SPECIFIC (type)
6529 && (IS_SIGNATURE_POINTER (type) || IS_SIGNATURE_REFERENCE (type)))
6530 return build_signature_pointer_constructor (type, rhs);
6532 if (IS_AGGR_TYPE (type)
6533 && (TYPE_NEEDS_CONSTRUCTING (type) || TREE_HAS_CONSTRUCTOR (rhs)))
6535 if (TYPE_MAIN_VARIANT (type) == TYPE_MAIN_VARIANT (rhstype))
6537 /* This is sufficient to perform initialization. No need,
6538 apparently, to go through X(X&) to do first-cut
6539 initialization. Return through a TARGET_EXPR so that we get
6540 cleanups if it is used. */
6541 if (TREE_CODE (rhs) == CALL_EXPR)
6543 rhs = build_cplus_new (type, rhs, 0);
6546 /* Handle the case of default parameter initialization and
6547 initialization of static variables. */
6548 else if (TREE_CODE (rhs) == TARGET_EXPR)
6550 else if (TREE_CODE (rhs) == INDIRECT_REF && TREE_HAS_CONSTRUCTOR (rhs))
6552 my_friendly_assert (TREE_CODE (TREE_OPERAND (rhs, 0)) == CALL_EXPR, 318);
6555 my_friendly_assert (TREE_VALUE (TREE_OPERAND (TREE_OPERAND (rhs, 0), 1)) == NULL_TREE, 316);
6556 TREE_VALUE (TREE_OPERAND (TREE_OPERAND (rhs, 0), 1))
6557 = build_unary_op (ADDR_EXPR, exp, 0);
6560 rhs = build_cplus_new (type, TREE_OPERAND (rhs, 0), 0);
6563 else if (TYPE_HAS_TRIVIAL_INIT_REF (type))
6566 if (TYPE_MAIN_VARIANT (type) == TYPE_MAIN_VARIANT (rhstype)
6567 || (IS_AGGR_TYPE (rhstype) && UNIQUELY_DERIVED_FROM_P (type, rhstype)))
6569 if (TYPE_HAS_INIT_REF (type))
6571 tree init = build_method_call (exp, constructor_name_full (type),
6572 build_tree_list (NULL_TREE, rhs),
6573 TYPE_BINFO (type), LOOKUP_NORMAL);
6575 if (init == error_mark_node)
6576 return error_mark_node;
6580 exp = build_cplus_new (type, init, 0);
6584 return build (COMPOUND_EXPR, type, init, exp);
6587 /* ??? The following warnings are turned off because
6588 this is another place where the default X(X&) constructor
6590 if (TYPE_HAS_ASSIGNMENT (type))
6591 cp_warning ("bitwise copy: `%T' defines operator=", type);
6593 if (TREE_CODE (TREE_TYPE (rhs)) == REFERENCE_TYPE)
6594 rhs = convert_from_reference (rhs);
6595 if (type != rhstype)
6597 tree nrhs = build1 (NOP_EXPR, type, rhs);
6598 TREE_CONSTANT (nrhs) = TREE_CONSTANT (rhs);
6604 return cp_convert (type, rhs, CONV_OLD_CONVERT, flags);
6607 if (type == TREE_TYPE (rhs))
6609 if (TREE_READONLY_DECL_P (rhs))
6610 rhs = decl_constant_value (rhs);
6614 return convert_for_assignment (type, rhs, errtype, fndecl, parmnum);
6617 /* Expand an ASM statement with operands, handling output operands
6618 that are not variables or INDIRECT_REFS by transforming such
6619 cases into cases that expand_asm_operands can handle.
6621 Arguments are same as for expand_asm_operands.
6623 We don't do default conversions on all inputs, because it can screw
6624 up operands that are expected to be in memory. */
6627 c_expand_asm_operands (string, outputs, inputs, clobbers, vol, filename, line)
6628 tree string, outputs, inputs, clobbers;
6633 int noutputs = list_length (outputs);
6635 /* o[I] is the place that output number I should be written. */
6636 register tree *o = (tree *) alloca (noutputs * sizeof (tree));
6639 /* Record the contents of OUTPUTS before it is modified. */
6640 for (i = 0, tail = outputs; tail; tail = TREE_CHAIN (tail), i++)
6641 o[i] = TREE_VALUE (tail);
6643 /* Generate the ASM_OPERANDS insn;
6644 store into the TREE_VALUEs of OUTPUTS some trees for
6645 where the values were actually stored. */
6646 expand_asm_operands (string, outputs, inputs, clobbers, vol, filename, line);
6648 /* Copy all the intermediate outputs into the specified outputs. */
6649 for (i = 0, tail = outputs; tail; tail = TREE_CHAIN (tail), i++)
6651 if (o[i] != TREE_VALUE (tail))
6653 expand_expr (build_modify_expr (o[i], NOP_EXPR, TREE_VALUE (tail)),
6654 const0_rtx, VOIDmode, 0);
6657 /* Detect modification of read-only values.
6658 (Otherwise done by build_modify_expr.) */
6661 tree type = TREE_TYPE (o[i]);
6662 if (TYPE_READONLY (type)
6663 || ((TREE_CODE (type) == RECORD_TYPE
6664 || TREE_CODE (type) == UNION_TYPE)
6665 && C_TYPE_FIELDS_READONLY (type)))
6666 readonly_error (o[i], "modification by `asm'", 1);
6670 /* Those MODIFY_EXPRs could do autoincrements. */
6674 /* Expand a C `return' statement.
6675 RETVAL is the expression for what to return,
6676 or a null pointer for `return;' with no value.
6678 C++: upon seeing a `return', we must call destructors on all
6679 variables in scope which had constructors called on them.
6680 This means that if in a destructor, the base class destructors
6681 must be called before returning.
6683 The RETURN statement in C++ has initialization semantics. */
6686 c_expand_return (retval)
6689 extern struct nesting *cond_stack, *loop_stack, *case_stack;
6690 extern tree dtor_label, ctor_label;
6691 tree result = DECL_RESULT (current_function_decl);
6692 tree valtype = TREE_TYPE (result);
6693 register int use_temp = 0;
6694 int returns_value = 1;
6696 if (TREE_THIS_VOLATILE (current_function_decl))
6697 warning ("function declared `noreturn' has a `return' statement");
6699 if (retval == error_mark_node)
6701 current_function_returns_null = 1;
6705 if (retval == NULL_TREE)
6707 /* A non-named return value does not count. */
6709 /* Can't just return from a destructor. */
6712 expand_goto (dtor_label);
6716 if (DECL_CONSTRUCTOR_P (current_function_decl))
6717 retval = current_class_decl;
6718 else if (DECL_NAME (result) != NULL_TREE
6719 && TREE_CODE (valtype) != VOID_TYPE)
6723 current_function_returns_null = 1;
6725 if (valtype != NULL_TREE && TREE_CODE (valtype) != VOID_TYPE)
6727 if (DECL_NAME (DECL_RESULT (current_function_decl)) == NULL_TREE)
6729 pedwarn ("`return' with no value, in function returning non-void");
6730 /* Clear this, so finish_function won't say that we
6731 reach the end of a non-void function (which we don't,
6732 we gave a return!). */
6733 current_function_returns_null = 0;
6737 expand_null_return ();
6741 else if (DECL_CONSTRUCTOR_P (current_function_decl)
6742 && retval != current_class_decl)
6744 error ("return from a constructor: use `this = ...' instead");
6745 retval = current_class_decl;
6748 if (valtype == NULL_TREE || TREE_CODE (valtype) == VOID_TYPE)
6750 current_function_returns_null = 1;
6751 /* We do this here so we'll avoid a warning about how the function
6752 "may or may not return a value" in finish_function. */
6756 pedwarn ("`return' with a value, in function returning void");
6757 expand_return (retval);
6759 /* Add some useful error checking for C++. */
6760 else if (TREE_CODE (valtype) == REFERENCE_TYPE)
6762 tree whats_returned;
6763 tree tmp_result = result;
6765 /* Don't initialize directly into a non-BLKmode retval, since that
6766 could lose when being inlined by another caller. (GCC can't
6767 read the function return register in an inline function when
6768 the return value is being ignored). */
6769 if (result && TYPE_MODE (TREE_TYPE (tmp_result)) != BLKmode)
6772 /* convert to reference now, so we can give error if we
6773 return an reference to a non-lvalue. */
6774 retval = convert_for_initialization (tmp_result, valtype, retval,
6775 LOOKUP_NORMAL, "return",
6778 /* Sort through common things to see what it is
6779 we are returning. */
6780 whats_returned = retval;
6781 if (TREE_CODE (whats_returned) == COMPOUND_EXPR)
6783 whats_returned = TREE_OPERAND (whats_returned, 1);
6784 if (TREE_CODE (whats_returned) == ADDR_EXPR)
6785 whats_returned = TREE_OPERAND (whats_returned, 0);
6787 if (TREE_CODE (whats_returned) == ADDR_EXPR)
6789 whats_returned = TREE_OPERAND (whats_returned, 0);
6790 while (TREE_CODE (whats_returned) == NEW_EXPR
6791 || TREE_CODE (whats_returned) == TARGET_EXPR
6792 || TREE_CODE (whats_returned) == WITH_CLEANUP_EXPR)
6794 /* Get the target. */
6795 whats_returned = TREE_OPERAND (whats_returned, 0);
6796 warning ("returning reference to temporary");
6800 if (TREE_CODE (whats_returned) == VAR_DECL && DECL_NAME (whats_returned))
6802 if (TEMP_NAME_P (DECL_NAME (whats_returned)))
6803 warning ("reference to non-lvalue returned");
6804 else if (! TREE_STATIC (whats_returned)
6805 && IDENTIFIER_LOCAL_VALUE (DECL_NAME (whats_returned)))
6806 cp_warning_at ("reference to local variable `%D' returned", whats_returned);
6809 else if (TREE_CODE (retval) == ADDR_EXPR)
6811 tree whats_returned = TREE_OPERAND (retval, 0);
6813 if (TREE_CODE (whats_returned) == VAR_DECL
6814 && DECL_NAME (whats_returned)
6815 && IDENTIFIER_LOCAL_VALUE (DECL_NAME (whats_returned))
6816 && !TREE_STATIC (whats_returned))
6817 cp_warning_at ("address of local variable `%D' returned", whats_returned);
6820 /* Now deal with possible C++ hair:
6821 (1) Compute the return value.
6822 (2) If there are aggregate values with destructors which
6823 must be cleaned up, clean them (taking care
6824 not to clobber the return value).
6825 (3) If an X(X&) constructor is defined, the return
6826 value must be returned via that. */
6828 /* If we're returning in a register, we can't initialize the
6829 return value from a TARGET_EXPR. */
6830 if (TREE_CODE (retval) == TARGET_EXPR
6831 && TYPE_MAIN_VARIANT (TREE_TYPE (retval)) == TYPE_MAIN_VARIANT (valtype)
6832 && ! current_function_returns_struct)
6833 retval = expand_target_expr (retval);
6835 if (retval == result
6836 /* Watch out for constructors, which "return" aggregates
6837 via initialization, but which otherwise "return" a pointer. */
6838 || DECL_CONSTRUCTOR_P (current_function_decl))
6840 /* This is just an error--it's already been reported. */
6841 if (TYPE_SIZE (valtype) == NULL_TREE)
6844 if (TYPE_MODE (valtype) != BLKmode
6845 && any_pending_cleanups (1))
6847 retval = get_temp_regvar (valtype, retval);
6848 use_temp = obey_regdecls;
6851 else if (IS_AGGR_TYPE (valtype) && current_function_returns_struct)
6853 expand_aggr_init (result, retval, 0, LOOKUP_ONLYCONVERTING);
6854 expand_cleanups_to (NULL_TREE);
6855 DECL_INITIAL (result) = NULL_TREE;
6860 if (TYPE_MODE (valtype) == VOIDmode)
6862 if (TYPE_MODE (TREE_TYPE (result)) != VOIDmode
6863 && warn_return_type)
6864 warning ("return of void value in function returning non-void");
6865 expand_expr_stmt (retval);
6869 else if (TYPE_MODE (valtype) != BLKmode
6870 && any_pending_cleanups (1))
6872 retval = get_temp_regvar (valtype, retval);
6873 expand_cleanups_to (NULL_TREE);
6874 use_temp = obey_regdecls;
6879 retval = convert_for_initialization (result, valtype, retval,
6881 "return", NULL_TREE, 0);
6882 DECL_INITIAL (result) = NULL_TREE;
6884 if (retval == error_mark_node)
6890 if (retval != NULL_TREE
6891 && TREE_CODE_CLASS (TREE_CODE (retval)) == 'd'
6892 && cond_stack == 0 && loop_stack == 0 && case_stack == 0)
6893 current_function_return_value = retval;
6897 /* Everything's great--RETVAL is in RESULT. */
6898 if (original_result_rtx)
6900 store_expr (result, original_result_rtx, 0);
6901 expand_cleanups_to (NULL_TREE);
6902 use_variable (DECL_RTL (result));
6903 if (ctor_label && TREE_CODE (ctor_label) != ERROR_MARK)
6904 expand_goto (ctor_label);
6906 expand_null_return ();
6908 else if (retval && retval != result)
6910 /* Clear this out so the later call to decl_function_context
6911 won't end up bombing on us. */
6912 if (DECL_CONTEXT (result) == error_mark_node)
6913 DECL_CONTEXT (result) = NULL_TREE;
6914 /* Here is where we finally get RETVAL into RESULT.
6915 `expand_return' does the magic of protecting
6916 RESULT from cleanups. */
6917 retval = fold (build1 (CLEANUP_POINT_EXPR, TREE_TYPE (result),
6919 /* This part _must_ come second, because expand_return looks for
6920 the INIT_EXPR as the toplevel node only. :-( */
6921 retval = build (INIT_EXPR, TREE_TYPE (result), result, retval);
6922 TREE_SIDE_EFFECTS (retval) = 1;
6923 expand_return (retval);
6926 expand_return (result);
6930 /* We may still need to put RETVAL into RESULT. */
6931 result = DECL_RESULT (current_function_decl);
6932 if (original_result_rtx)
6934 /* Here we have a named return value that went
6935 into memory. We can compute RETVAL into that. */
6937 expand_assignment (result, retval, 0, 0);
6939 store_expr (result, original_result_rtx, 0);
6940 result = make_tree (TREE_TYPE (result), original_result_rtx);
6942 else if (ctor_label && TREE_CODE (ctor_label) != ERROR_MARK)
6944 /* Here RETVAL is CURRENT_CLASS_DECL, so there's nothing to do. */
6945 expand_goto (ctor_label);
6949 /* Here is where we finally get RETVAL into RESULT.
6950 `expand_return' does the magic of protecting
6951 RESULT from cleanups. */
6952 result = build (INIT_EXPR, TREE_TYPE (result), result, retval);
6953 TREE_SIDE_EFFECTS (result) = 1;
6954 expand_return (result);
6956 else if (TYPE_MODE (TREE_TYPE (result)) != VOIDmode)
6957 expand_return (result);
6960 current_function_returns_value = returns_value;
6962 /* One way to clear out cleanups that EXPR might
6963 generate. Note that this code will really be
6964 dead code, but that is ok--cleanups that were
6965 needed were handled by the magic of `return'. */
6966 expand_cleanups_to (NULL_TREE);
6969 /* Start a C switch statement, testing expression EXP.
6970 Return EXP if it is valid, an error node otherwise. */
6973 c_expand_start_case (exp)
6977 register enum tree_code code;
6979 /* Convert from references, etc. */
6980 exp = default_conversion (exp);
6981 type = TREE_TYPE (exp);
6982 code = TREE_CODE (type);
6984 if (IS_AGGR_TYPE_CODE (code))
6985 exp = build_type_conversion (CONVERT_EXPR, integer_type_node, exp, 1);
6987 if (exp == NULL_TREE)
6989 error ("switch quantity not an integer");
6990 exp = error_mark_node;
6992 type = TREE_TYPE (exp);
6993 code = TREE_CODE (type);
6995 if (code != INTEGER_TYPE && code != ENUMERAL_TYPE && code != ERROR_MARK)
6997 error ("switch quantity not an integer");
6998 exp = error_mark_node;
7004 exp = default_conversion (exp);
7005 type = TREE_TYPE (exp);
7006 index = get_unwidened (exp, 0);
7007 /* We can't strip a conversion from a signed type to an unsigned,
7008 because if we did, int_fits_type_p would do the wrong thing
7009 when checking case values for being in range,
7010 and it's too hard to do the right thing. */
7011 if (TREE_UNSIGNED (TREE_TYPE (exp))
7012 == TREE_UNSIGNED (TREE_TYPE (index)))
7017 (1, fold (build1 (CLEANUP_POINT_EXPR, TREE_TYPE (exp), exp)),
7018 type, "switch statement");
7023 /* CONSTP remembers whether or not all the intervening pointers in the `to'
7024 type have been const. */
7026 comp_ptr_ttypes_real (to, from, constp)
7030 for (; ; to = TREE_TYPE (to), from = TREE_TYPE (from))
7032 if (TREE_CODE (to) != TREE_CODE (from))
7035 /* Const and volatile mean something different for function types,
7036 so the usual checks are not appropriate. */
7037 if (TREE_CODE (to) != FUNCTION_TYPE && TREE_CODE (to) != METHOD_TYPE)
7039 if (TYPE_READONLY (from) > TYPE_READONLY (to)
7040 || TYPE_VOLATILE (from) > TYPE_VOLATILE (to))
7044 && (TYPE_READONLY (to) > TYPE_READONLY (from)
7045 || TYPE_VOLATILE (to) > TYPE_READONLY (from)))
7047 constp &= TYPE_READONLY (to);
7050 if (TREE_CODE (to) != POINTER_TYPE)
7051 return comptypes (TYPE_MAIN_VARIANT (to), TYPE_MAIN_VARIANT (from), 1);
7055 /* When comparing, say, char ** to char const **, this function takes the
7056 'char *' and 'char const *'. Do not pass non-pointer types to this
7059 comp_ptr_ttypes (to, from)
7062 return comp_ptr_ttypes_real (to, from, 1);