1 /* Build expressions with type checking for C++ compiler.
2 Copyright (C) 1987, 88, 89, 92, 93, 94, 95, 1996 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 PROTO((tree));
44 static tree convert_for_assignment PROTO((tree, tree, char*, tree, int));
45 /* static */ tree convert_for_initialization PROTO((tree, tree, tree, int, char*, tree, int));
46 extern tree shorten_compare ();
47 extern void binary_op_error ();
48 static tree pointer_int_sum PROTO((enum tree_code, register tree, register tree));
49 static tree pointer_diff PROTO((register tree, register tree));
51 static tree convert_sequence ();
53 /* static */ tree unary_complex_lvalue PROTO((enum tree_code, tree));
54 static tree get_delta_difference PROTO((tree, tree, int));
56 extern rtx original_result_rtx;
57 extern int warn_synth;
59 /* Return the target type of TYPE, which meas return T for:
60 T*, T&, T[], T (...), and otherwise, just T. */
66 if (TREE_CODE (type) == REFERENCE_TYPE)
67 type = TREE_TYPE (type);
68 while (TREE_CODE (type) == POINTER_TYPE
69 || TREE_CODE (type) == ARRAY_TYPE
70 || TREE_CODE (type) == FUNCTION_TYPE
71 || TREE_CODE (type) == METHOD_TYPE
72 || TREE_CODE (type) == OFFSET_TYPE)
73 type = TREE_TYPE (type);
77 /* Do `exp = require_complete_type (exp);' to make sure exp
78 does not have an incomplete type. (That includes void types.) */
81 require_complete_type (value)
86 if (current_template_parms)
89 type = TREE_TYPE (value);
91 /* First, detect a valid value with a complete type. */
92 if (TYPE_SIZE (type) != 0
93 && type != void_type_node
94 && ! (TYPE_LANG_SPECIFIC (type)
95 && (IS_SIGNATURE_POINTER (type) || IS_SIGNATURE_REFERENCE (type))
96 && TYPE_SIZE (SIGNATURE_TYPE (type)) == 0))
99 /* If we see X::Y, we build an OFFSET_TYPE which has
100 not been laid out. Try to avoid an error by interpreting
101 it as this->X::Y, if reasonable. */
102 if (TREE_CODE (value) == OFFSET_REF
104 && TREE_OPERAND (value, 0) == C_C_D)
106 tree base, member = TREE_OPERAND (value, 1);
107 tree basetype = TYPE_OFFSET_BASETYPE (type);
108 my_friendly_assert (TREE_CODE (member) == FIELD_DECL, 305);
109 base = convert_pointer_to (basetype, current_class_decl);
110 value = build (COMPONENT_REF, TREE_TYPE (member),
111 build_indirect_ref (base, NULL_PTR), member);
112 return require_complete_type (value);
115 if (IS_AGGR_TYPE (type) && CLASSTYPE_TEMPLATE_INSTANTIATION (type))
117 instantiate_class_template (TYPE_MAIN_VARIANT (type));
118 if (TYPE_SIZE (type) != 0)
122 incomplete_type_error (value, type);
123 return error_mark_node;
130 if (TYPE_SIZE (type) != NULL_TREE)
132 else if (TREE_CODE (type) == ARRAY_TYPE)
134 tree t = complete_type (TREE_TYPE (type));
135 if (TYPE_SIZE (t) != NULL_TREE)
136 type = build_cplus_array_type (t, TYPE_DOMAIN (type));
138 else if (IS_AGGR_TYPE (type) && CLASSTYPE_TEMPLATE_INSTANTIATION (type))
139 instantiate_class_template (TYPE_MAIN_VARIANT (type));
144 /* Return truthvalue of whether type of EXP is instantiated. */
149 return (TREE_CODE (exp) == TREE_LIST
150 || TREE_TYPE (exp) == unknown_type_node
151 || (TREE_CODE (TREE_TYPE (exp)) == OFFSET_TYPE
152 && TREE_TYPE (TREE_TYPE (exp)) == unknown_type_node));
155 /* Return truthvalue of whether T is function (or pfn) type. */
160 return (TREE_CODE (t) == FUNCTION_TYPE || TREE_CODE (t) == METHOD_TYPE
161 || (TREE_CODE (t) == POINTER_TYPE
162 && (TREE_CODE (TREE_TYPE (t)) == FUNCTION_TYPE
163 || TREE_CODE (TREE_TYPE (t)) == METHOD_TYPE)));
166 /* Do `exp = require_instantiated_type (type, exp);' to make sure EXP
167 does not have an uninstantiated type.
168 TYPE is type to instantiate with, if uninstantiated. */
170 require_instantiated_type (type, exp, errval)
171 tree type, exp, errval;
173 if (TREE_TYPE (exp) == NULL_TREE)
175 error ("argument list may not have an initializer list");
179 if (TREE_TYPE (exp) == unknown_type_node
180 || (TREE_CODE (TREE_TYPE (exp)) == OFFSET_TYPE
181 && TREE_TYPE (TREE_TYPE (exp)) == unknown_type_node))
183 exp = instantiate_type (type, exp, 1);
184 if (TREE_TYPE (exp) == error_mark_node)
190 /* Return a variant of TYPE which has all the type qualifiers of LIKE
191 as well as those of TYPE. */
194 qualify_type (type, like)
197 int constflag = TYPE_READONLY (type) || TYPE_READONLY (like);
198 int volflag = TYPE_VOLATILE (type) || TYPE_VOLATILE (like);
199 /* @@ Must do member pointers here. */
200 return cp_build_type_variant (type, constflag, volflag);
203 /* Return the common type of two parameter lists.
204 We assume that comptypes has already been done and returned 1;
205 if that isn't so, this may crash.
207 As an optimization, free the space we allocate if the parameter
208 lists are already common. */
214 tree oldargs = p1, newargs, n;
217 char *first_obj = (char *) oballoc (0);
219 len = list_length (p1);
220 newargs = tree_last (p1);
222 if (newargs == void_list_node)
231 newargs = tree_cons (NULL_TREE, NULL_TREE, newargs);
236 p1 = TREE_CHAIN (p1), p2 = TREE_CHAIN (p2), n = TREE_CHAIN (n), i++)
238 if (TREE_PURPOSE (p1) && !TREE_PURPOSE (p2))
240 TREE_PURPOSE (n) = TREE_PURPOSE (p1);
243 else if (! TREE_PURPOSE (p1))
245 if (TREE_PURPOSE (p2))
247 TREE_PURPOSE (n) = TREE_PURPOSE (p2);
253 if (1 != simple_cst_equal (TREE_PURPOSE (p1), TREE_PURPOSE (p2)))
255 TREE_PURPOSE (n) = TREE_PURPOSE (p2);
257 if (TREE_VALUE (p1) != TREE_VALUE (p2))
260 TREE_VALUE (n) = common_type (TREE_VALUE (p1), TREE_VALUE (p2));
263 TREE_VALUE (n) = TREE_VALUE (p1);
274 /* Return the common type of two types.
275 We assume that comptypes has already been done and returned 1;
276 if that isn't so, this may crash.
278 This is the type for the result of most arithmetic operations
279 if the operands have the given two types.
281 We do not deal with enumeral types here because they have already been
282 converted to integer types. */
288 register enum tree_code code1;
289 register enum tree_code code2;
292 /* Save time if the two types are the same. */
294 if (t1 == t2) return t1;
296 /* If one type is nonsense, use the other. */
297 if (t1 == error_mark_node)
299 if (t2 == error_mark_node)
302 /* Merge the attributes */
304 { register tree a1, a2;
305 a1 = TYPE_ATTRIBUTES (t1);
306 a2 = TYPE_ATTRIBUTES (t2);
308 /* Either one unset? Take the set one. */
310 if (!(attributes = a1))
313 /* One that completely contains the other? Take it. */
315 else if (a2 && !attribute_list_contained (a1, a2))
316 if (attribute_list_contained (a2, a1))
320 /* Pick the longest list, and hang on the other list. */
321 /* ??? For the moment we punt on the issue of attrs with args. */
323 if (list_length (a1) < list_length (a2))
324 attributes = a2, a2 = a1;
326 for (; a2; a2 = TREE_CHAIN (a2))
327 if (lookup_attribute (IDENTIFIER_POINTER (TREE_PURPOSE (a2)),
328 attributes) == NULL_TREE)
331 TREE_CHAIN (a1) = attributes;
337 /* Treat an enum type as the unsigned integer type of the same width. */
339 if (TREE_CODE (t1) == ENUMERAL_TYPE)
340 t1 = type_for_size (TYPE_PRECISION (t1), 1);
341 if (TREE_CODE (t2) == ENUMERAL_TYPE)
342 t2 = type_for_size (TYPE_PRECISION (t2), 1);
344 if (TYPE_PTRMEMFUNC_P (t1))
345 t1 = TYPE_PTRMEMFUNC_FN_TYPE (t1);
346 if (TYPE_PTRMEMFUNC_P (t2))
347 t2 = TYPE_PTRMEMFUNC_FN_TYPE (t2);
349 code1 = TREE_CODE (t1);
350 code2 = TREE_CODE (t2);
356 /* If only one is real, use it as the result. */
358 if (code1 == REAL_TYPE && code2 != REAL_TYPE)
359 return build_type_attribute_variant (t1, attributes);
361 if (code2 == REAL_TYPE && code1 != REAL_TYPE)
362 return build_type_attribute_variant (t2, attributes);
364 /* Both real or both integers; use the one with greater precision. */
366 if (TYPE_PRECISION (t1) > TYPE_PRECISION (t2))
367 return build_type_attribute_variant (t1, attributes);
368 else if (TYPE_PRECISION (t2) > TYPE_PRECISION (t1))
369 return build_type_attribute_variant (t2, attributes);
371 /* Same precision. Prefer longs to ints even when same size. */
373 if (TYPE_MAIN_VARIANT (t1) == long_unsigned_type_node
374 || TYPE_MAIN_VARIANT (t2) == long_unsigned_type_node)
375 return build_type_attribute_variant (long_unsigned_type_node,
378 if (TYPE_MAIN_VARIANT (t1) == long_integer_type_node
379 || TYPE_MAIN_VARIANT (t2) == long_integer_type_node)
381 /* But preserve unsignedness from the other type,
382 since long cannot hold all the values of an unsigned int. */
383 if (TREE_UNSIGNED (t1) || TREE_UNSIGNED (t2))
384 t1 = long_unsigned_type_node;
386 t1 = long_integer_type_node;
387 return build_type_attribute_variant (t1, attributes);
390 if (TYPE_MAIN_VARIANT (t1) == long_double_type_node
391 || TYPE_MAIN_VARIANT (t2) == long_double_type_node)
392 return build_type_attribute_variant (long_double_type_node,
395 /* Otherwise prefer the unsigned one. */
397 if (TREE_UNSIGNED (t1))
398 return build_type_attribute_variant (t1, attributes);
400 return build_type_attribute_variant (t2, attributes);
404 /* For two pointers, do this recursively on the target type,
405 and combine the qualifiers of the two types' targets. */
406 /* This code was turned off; I don't know why.
407 But ANSI C++ specifies doing this with the qualifiers.
408 So I turned it on again. */
410 tree tt1 = TYPE_MAIN_VARIANT (TREE_TYPE (t1));
411 tree tt2 = TYPE_MAIN_VARIANT (TREE_TYPE (t2));
413 = TYPE_READONLY (TREE_TYPE (t1)) || TYPE_READONLY (TREE_TYPE (t2));
415 = TYPE_VOLATILE (TREE_TYPE (t1)) || TYPE_VOLATILE (TREE_TYPE (t2));
420 else if (tt1 == void_type_node || tt2 == void_type_node)
421 target = void_type_node;
423 target = common_type (tt1, tt2);
425 target = cp_build_type_variant (target, constp, volatilep);
426 if (code1 == POINTER_TYPE)
427 t1 = build_pointer_type (target);
429 t1 = build_reference_type (target);
430 t1 = build_type_attribute_variant (t1, attributes);
432 if (TREE_CODE (target) == METHOD_TYPE)
433 t1 = build_ptrmemfunc_type (t1);
440 tree elt = common_type (TREE_TYPE (t1), TREE_TYPE (t2));
441 /* Save space: see if the result is identical to one of the args. */
442 if (elt == TREE_TYPE (t1) && TYPE_DOMAIN (t1))
443 return build_type_attribute_variant (t1, attributes);
444 if (elt == TREE_TYPE (t2) && TYPE_DOMAIN (t2))
445 return build_type_attribute_variant (t2, attributes);
446 /* Merge the element types, and have a size if either arg has one. */
447 t1 = build_cplus_array_type (elt, TYPE_DOMAIN (TYPE_DOMAIN (t1) ? t1 : t2));
448 return build_type_attribute_variant (t1, attributes);
452 /* Function types: prefer the one that specified arg types.
453 If both do, merge the arg types. Also merge the return types. */
455 tree valtype = common_type (TREE_TYPE (t1), TREE_TYPE (t2));
456 tree p1 = TYPE_ARG_TYPES (t1);
457 tree p2 = TYPE_ARG_TYPES (t2);
460 /* Save space: see if the result is identical to one of the args. */
461 if (valtype == TREE_TYPE (t1) && ! p2)
462 return build_type_attribute_variant (t1, attributes);
463 if (valtype == TREE_TYPE (t2) && ! p1)
464 return build_type_attribute_variant (t2, attributes);
466 /* Simple way if one arg fails to specify argument types. */
467 if (p1 == NULL_TREE || TREE_VALUE (p1) == void_type_node)
469 rval = build_function_type (valtype, p2);
470 if ((raises = TYPE_RAISES_EXCEPTIONS (t2)))
471 rval = build_exception_variant (rval, raises);
472 return build_type_attribute_variant (rval, attributes);
474 raises = TYPE_RAISES_EXCEPTIONS (t1);
475 if (p2 == NULL_TREE || TREE_VALUE (p2) == void_type_node)
477 rval = build_function_type (valtype, p1);
479 rval = build_exception_variant (rval, raises);
480 return build_type_attribute_variant (rval, attributes);
483 rval = build_function_type (valtype, commonparms (p1, p2));
484 rval = build_exception_variant (rval, raises);
485 return build_type_attribute_variant (rval, attributes);
490 my_friendly_assert (TYPE_MAIN_VARIANT (t1) == t1
491 && TYPE_MAIN_VARIANT (t2) == t2, 306);
493 if (DERIVED_FROM_P (t1, t2) && binfo_or_else (t1, t2))
494 return build_type_attribute_variant (t1, attributes);
495 else if (binfo_or_else (t2, t1))
496 return build_type_attribute_variant (t2, attributes);
498 compiler_error ("common_type called with uncommon aggregate types");
501 if (TREE_CODE (TREE_TYPE (t1)) == TREE_CODE (TREE_TYPE (t2)))
503 /* Get this value the long way, since TYPE_METHOD_BASETYPE
504 is just the main variant of this. */
508 tree b1 = TYPE_OFFSET_BASETYPE (t1);
509 tree b2 = TYPE_OFFSET_BASETYPE (t2);
511 if (comptypes (b1, b2, 1)
512 || (DERIVED_FROM_P (b1, b2) && binfo_or_else (b1, b2)))
513 basetype = TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (t2)));
516 if (binfo_or_else (b2, b1) == NULL_TREE)
517 compiler_error ("common_type called with uncommon method types");
518 basetype = TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (t1)));
521 raises = TYPE_RAISES_EXCEPTIONS (t1);
523 /* If this was a member function type, get back to the
524 original type of type member function (i.e., without
525 the class instance variable up front. */
526 t1 = build_function_type (TREE_TYPE (t1), TREE_CHAIN (TYPE_ARG_TYPES (t1)));
527 t2 = build_function_type (TREE_TYPE (t2), TREE_CHAIN (TYPE_ARG_TYPES (t2)));
528 t3 = common_type (t1, t2);
529 t3 = build_cplus_method_type (basetype, TREE_TYPE (t3), TYPE_ARG_TYPES (t3));
530 t1 = build_exception_variant (t3, raises);
533 compiler_error ("common_type called with uncommon method types");
535 return build_type_attribute_variant (t1, attributes);
538 if (TREE_TYPE (t1) == TREE_TYPE (t2))
540 tree b1 = TYPE_OFFSET_BASETYPE (t1);
541 tree b2 = TYPE_OFFSET_BASETYPE (t2);
543 if (DERIVED_FROM_P (b1, b2) && binfo_or_else (b1, b2))
544 return build_type_attribute_variant (t2, attributes);
545 else if (binfo_or_else (b2, b1))
546 return build_type_attribute_variant (t1, attributes);
548 compiler_error ("common_type called with uncommon member types");
551 return build_type_attribute_variant (t1, attributes);
555 /* Return 1 if TYPE1 and TYPE2 raise the same exceptions. */
557 compexcepttypes (t1, t2)
560 return TYPE_RAISES_EXCEPTIONS (t1) == TYPE_RAISES_EXCEPTIONS (t2);
564 comp_array_types (cmp, t1, t2, strict)
565 register int (*cmp)();
569 tree d1 = TYPE_DOMAIN (t1);
570 tree d2 = TYPE_DOMAIN (t2);
572 /* Target types must match incl. qualifiers. */
573 if (!(TREE_TYPE (t1) == TREE_TYPE (t2)
574 || (*cmp) (TREE_TYPE (t1), TREE_TYPE (t2), strict)))
577 /* Sizes must match unless one is missing or variable. */
578 if (d1 == 0 || d2 == 0 || d1 == d2
579 || TREE_CODE (TYPE_MIN_VALUE (d1)) != INTEGER_CST
580 || TREE_CODE (TYPE_MIN_VALUE (d2)) != INTEGER_CST
581 || TREE_CODE (TYPE_MAX_VALUE (d1)) != INTEGER_CST
582 || TREE_CODE (TYPE_MAX_VALUE (d2)) != INTEGER_CST)
585 return ((TREE_INT_CST_LOW (TYPE_MIN_VALUE (d1))
586 == TREE_INT_CST_LOW (TYPE_MIN_VALUE (d2)))
587 && (TREE_INT_CST_HIGH (TYPE_MIN_VALUE (d1))
588 == TREE_INT_CST_HIGH (TYPE_MIN_VALUE (d2)))
589 && (TREE_INT_CST_LOW (TYPE_MAX_VALUE (d1))
590 == TREE_INT_CST_LOW (TYPE_MAX_VALUE (d2)))
591 && (TREE_INT_CST_HIGH (TYPE_MAX_VALUE (d1))
592 == TREE_INT_CST_HIGH (TYPE_MAX_VALUE (d2))));
595 /* Return 1 if TYPE1 and TYPE2 are compatible types for assignment
596 or various other operations. This is what ANSI C++ speaks of as
599 For C++: argument STRICT says we should be strict about this
602 2 : strict, except that if one type is a reference and
603 the other is not, compare the target type of the
604 reference to the type that's not a reference (ARM, p308).
605 This is used for checking for invalid overloading.
606 1 : strict (compared according to ANSI C)
607 This is used for checking whether two function decls match.
608 0 : <= (compared according to C++)
609 -1: <= or >= (relaxed)
611 Otherwise, pointers involving base classes and derived classes
612 can be mixed as valid: i.e. a pointer to a base class may be assigned
613 to a pointer to one of its derived classes, as per C++. A pointer to
614 a derived class may be passed as a parameter to a function expecting a
615 pointer to a base classes. These allowances do not commute. In this
616 case, TYPE1 is assumed to be the base class, and TYPE2 is assumed to
617 be the derived class. */
619 comptypes (type1, type2, strict)
623 register tree t1 = type1;
624 register tree t2 = type2;
627 /* Suppress errors caused by previously reported errors */
632 /* This should never happen. */
633 my_friendly_assert (t1 != error_mark_node, 307);
635 if (t2 == error_mark_node)
640 /* Treat an enum type as the unsigned integer type of the same width. */
642 if (TREE_CODE (t1) == ENUMERAL_TYPE)
643 t1 = type_for_size (TYPE_PRECISION (t1), 1);
644 if (TREE_CODE (t2) == ENUMERAL_TYPE)
645 t2 = type_for_size (TYPE_PRECISION (t2), 1);
651 if (TYPE_PTRMEMFUNC_P (t1))
652 t1 = TYPE_PTRMEMFUNC_FN_TYPE (t1);
653 if (TYPE_PTRMEMFUNC_P (t2))
654 t2 = TYPE_PTRMEMFUNC_FN_TYPE (t2);
656 /* Different classes of types can't be compatible. */
658 if (TREE_CODE (t1) != TREE_CODE (t2))
661 && ((TREE_CODE (t1) == REFERENCE_TYPE)
662 ^ (TREE_CODE (t2) == REFERENCE_TYPE)))
664 if (TREE_CODE (t1) == REFERENCE_TYPE)
665 return comptypes (TREE_TYPE (t1), t2, 1);
666 return comptypes (t1, TREE_TYPE (t2), 1);
674 /* Qualifiers must match. */
676 if (TYPE_READONLY (t1) != TYPE_READONLY (t2))
678 if (TYPE_VOLATILE (t1) != TYPE_VOLATILE (t2))
681 /* Allow for two different type nodes which have essentially the same
682 definition. Note that we already checked for equality of the type
683 type qualifiers (just above). */
685 if (TYPE_MAIN_VARIANT (t1) == TYPE_MAIN_VARIANT (t2))
688 /* ??? COMP_TYPE_ATTRIBUTES is currently useless for variables as each
689 attribute is its own main variant (`val' will remain 0). */
690 #ifndef COMP_TYPE_ATTRIBUTES
691 #define COMP_TYPE_ATTRIBUTES(t1,t2) 1
694 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
695 if (! (attrval = COMP_TYPE_ATTRIBUTES (t1, t2)))
698 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
701 switch (TREE_CODE (t1))
705 if (CLASSTYPE_TEMPLATE_INFO (t1) && CLASSTYPE_TEMPLATE_INFO (t2)
706 && CLASSTYPE_TI_TEMPLATE (t1) == CLASSTYPE_TI_TEMPLATE (t2))
708 int i = TREE_VEC_LENGTH (CLASSTYPE_TI_ARGS (t1));
709 tree *p1 = &TREE_VEC_ELT (CLASSTYPE_TI_ARGS (t1), 0);
710 tree *p2 = &TREE_VEC_ELT (CLASSTYPE_TI_ARGS (t2), 0);
714 if (TREE_CODE_CLASS (TREE_CODE (p1[i])) == 't')
716 if (! comptypes (p1[i], p2[i], 1))
721 if (simple_cst_equal (p1[i], p2[i]) <= 0)
732 val = (comptypes (build_pointer_type (TYPE_OFFSET_BASETYPE (t1)),
733 build_pointer_type (TYPE_OFFSET_BASETYPE (t2)), strict)
734 && comptypes (TREE_TYPE (t1), TREE_TYPE (t2), strict));
738 if (! compexcepttypes (t1, t2))
741 /* This case is anti-symmetrical!
742 One can pass a base member (or member function)
743 to something expecting a derived member (or member function),
744 but not vice-versa! */
746 val = (comptypes (TREE_TYPE (t1), TREE_TYPE (t2), strict)
747 && compparms (TYPE_ARG_TYPES (t1),
748 TYPE_ARG_TYPES (t2), strict));
762 if (TREE_CODE (t1) == RECORD_TYPE && TREE_CODE (t2) == RECORD_TYPE)
766 rval = t1 == t2 || UNIQUELY_DERIVED_FROM_P (t1, t2);
775 val = UNIQUELY_DERIVED_FROM_P (t2, t1);
782 val = comptypes (t1, t2, strict);
786 if (! compexcepttypes (t1, t2))
789 val = ((TREE_TYPE (t1) == TREE_TYPE (t2)
790 || comptypes (TREE_TYPE (t1), TREE_TYPE (t2), strict))
791 && compparms (TYPE_ARG_TYPES (t1), TYPE_ARG_TYPES (t2), strict));
795 /* Target types must match incl. qualifiers. */
796 val = comp_array_types (comptypes, t1, t2, strict);
799 case TEMPLATE_TYPE_PARM:
800 return TEMPLATE_TYPE_IDX (t1) == TEMPLATE_TYPE_IDX (t2);
803 if (TYPE_IDENTIFIER (t1) != TYPE_IDENTIFIER (t2))
805 return comptypes (TYPE_CONTEXT (t1), TYPE_CONTEXT (t2), 1);
807 return attrval == 2 && val == 1 ? 2 : val;
810 /* Return 1 if TTL and TTR are pointers to types that are equivalent,
811 ignoring their qualifiers.
813 NPTRS is the number of pointers we can strip off and keep cool.
814 This is used to permit (for aggr A, aggr B) A, B* to convert to A*,
815 but to not permit B** to convert to A**. */
818 comp_target_types (ttl, ttr, nptrs)
822 ttl = TYPE_MAIN_VARIANT (ttl);
823 ttr = TYPE_MAIN_VARIANT (ttr);
827 if (TREE_CODE (ttr) != TREE_CODE (ttl))
830 if (TREE_CODE (ttr) == POINTER_TYPE)
832 ttl = TREE_TYPE (ttl);
833 ttr = TREE_TYPE (ttr);
837 if (TREE_CODE (ttl) == VOID_TYPE
838 && TREE_CODE (ttr) != FUNCTION_TYPE
839 && TREE_CODE (ttr) != METHOD_TYPE
840 && TREE_CODE (ttr) != OFFSET_TYPE)
842 else if (TREE_CODE (ttr) == VOID_TYPE
843 && TREE_CODE (ttl) != FUNCTION_TYPE
844 && TREE_CODE (ttl) != METHOD_TYPE
845 && TREE_CODE (ttl) != OFFSET_TYPE)
847 else if (TREE_CODE (ttl) == POINTER_TYPE
848 || TREE_CODE (ttl) == ARRAY_TYPE)
850 if (comp_ptr_ttypes (ttl, ttr))
852 else if (comp_ptr_ttypes (ttr, ttl))
858 /* Const and volatile mean something different for function types,
859 so the usual checks are not appropriate. */
860 if (TREE_CODE (ttl) == FUNCTION_TYPE || TREE_CODE (ttl) == METHOD_TYPE)
861 return comp_target_types (ttl, ttr, nptrs - 1);
863 /* Make sure that the cv-quals change only in the same direction as
867 int c = TYPE_READONLY (ttl) - TYPE_READONLY (ttr);
868 int v = TYPE_VOLATILE (ttl) - TYPE_VOLATILE (ttr);
870 if ((c > 0 && v < 0) || (c < 0 && v > 0))
873 if (TYPE_MAIN_VARIANT (ttl) == TYPE_MAIN_VARIANT (ttr))
874 return (c + v < 0) ? -1 : 1;
876 t = comp_target_types (ttl, ttr, nptrs - 1);
877 if ((t == 1 && c + v >= 0) || (t == -1 && c + v <= 0))
884 if (TREE_CODE (ttr) == REFERENCE_TYPE)
885 return comp_target_types (TREE_TYPE (ttl), TREE_TYPE (ttr), nptrs);
886 if (TREE_CODE (ttr) == ARRAY_TYPE)
887 return comp_array_types (comp_target_types, ttl, ttr, 0);
888 else if (TREE_CODE (ttr) == FUNCTION_TYPE || TREE_CODE (ttr) == METHOD_TYPE)
889 if (comp_target_types (TREE_TYPE (ttl), TREE_TYPE (ttr), nptrs))
890 switch (comp_target_parms (TYPE_ARG_TYPES (ttl), TYPE_ARG_TYPES (ttr), 1))
899 my_friendly_abort (112);
905 else if (TREE_CODE (ttr) == OFFSET_TYPE)
907 /* Contravariance: we can assign a pointer to base member to a pointer
908 to derived member. Note difference from simple pointer case, where
909 we can pass a pointer to derived to a pointer to base. */
910 if (comptypes (TYPE_OFFSET_BASETYPE (ttr), TYPE_OFFSET_BASETYPE (ttl), 0))
911 return comp_target_types (TREE_TYPE (ttl), TREE_TYPE (ttr), nptrs);
912 else if (comptypes (TYPE_OFFSET_BASETYPE (ttl), TYPE_OFFSET_BASETYPE (ttr), 0)
913 && comp_target_types (TREE_TYPE (ttl), TREE_TYPE (ttr), nptrs))
916 else if (IS_AGGR_TYPE (ttl))
920 if (comptypes (build_pointer_type (ttl), build_pointer_type (ttr), 0))
922 if (comptypes (build_pointer_type (ttr), build_pointer_type (ttl), 0))
930 /* If two types share a common base type, return that basetype.
931 If there is not a unique most-derived base type, this function
932 returns ERROR_MARK_NODE. */
934 common_base_type (tt1, tt2)
937 tree best = NULL_TREE;
940 /* If one is a baseclass of another, that's good enough. */
941 if (UNIQUELY_DERIVED_FROM_P (tt1, tt2))
943 if (UNIQUELY_DERIVED_FROM_P (tt2, tt1))
946 /* Otherwise, try to find a unique baseclass of TT1
947 that is shared by TT2, and follow that down. */
948 for (i = CLASSTYPE_N_BASECLASSES (tt1)-1; i >= 0; i--)
950 tree basetype = TYPE_BINFO_BASETYPE (tt1, i);
951 tree trial = common_base_type (basetype, tt2);
954 if (trial == error_mark_node)
956 if (best == NULL_TREE)
958 else if (best != trial)
959 return error_mark_node;
964 for (i = CLASSTYPE_N_BASECLASSES (tt2)-1; i >= 0; i--)
966 tree basetype = TYPE_BINFO_BASETYPE (tt2, i);
967 tree trial = common_base_type (tt1, basetype);
970 if (trial == error_mark_node)
972 if (best == NULL_TREE)
974 else if (best != trial)
975 return error_mark_node;
981 /* Subroutines of `comptypes'. */
983 /* Return 1 if two parameter type lists PARMS1 and PARMS2
984 are equivalent in the sense that functions with those parameter types
985 can have equivalent types.
986 If either list is empty, we win.
987 Otherwise, the two lists must be equivalent, element by element.
989 C++: See comment above about TYPE1, TYPE2, STRICT.
990 If STRICT == 3, it means checking is strict, but do not compare
991 default parameter values. */
993 compparms (parms1, parms2, strict)
997 register tree t1 = parms1, t2 = parms2;
999 /* An unspecified parmlist matches any specified parmlist
1000 whose argument types don't need default promotions. */
1002 if (strict <= 0 && t1 == 0)
1003 return self_promoting_args_p (t2);
1004 if (strict < 0 && t2 == 0)
1005 return self_promoting_args_p (t1);
1009 if (t1 == 0 && t2 == 0)
1011 /* If one parmlist is shorter than the other,
1012 they fail to match, unless STRICT is <= 0. */
1013 if (t1 == 0 || t2 == 0)
1020 return t1 && TREE_PURPOSE (t1);
1022 if (! comptypes (TREE_VALUE (t2), TREE_VALUE (t1), strict))
1027 return t2 == void_list_node && TREE_PURPOSE (t1);
1028 return TREE_PURPOSE (t1) || TREE_PURPOSE (t2);
1031 t1 = TREE_CHAIN (t1);
1032 t2 = TREE_CHAIN (t2);
1036 /* This really wants return whether or not parameter type lists
1037 would make their owning functions assignment compatible or not. */
1039 comp_target_parms (parms1, parms2, strict)
1040 tree parms1, parms2;
1043 register tree t1 = parms1, t2 = parms2;
1044 int warn_contravariance = 0;
1046 /* An unspecified parmlist matches any specified parmlist
1047 whose argument types don't need default promotions.
1048 @@@ see 13.3.3 for a counterexample... */
1050 if (t1 == 0 && t2 != 0)
1052 cp_pedwarn ("ANSI C++ prohibits conversion from `(%#T)' to `(...)'",
1054 return self_promoting_args_p (t2);
1057 return self_promoting_args_p (t1);
1059 for (; t1 || t2; t1 = TREE_CHAIN (t1), t2 = TREE_CHAIN (t2))
1063 /* If one parmlist is shorter than the other,
1064 they fail to match, unless STRICT is <= 0. */
1065 if (t1 == 0 || t2 == 0)
1070 return 1 + warn_contravariance;
1071 return ((t1 && TREE_PURPOSE (t1)) + warn_contravariance);
1073 p1 = TREE_VALUE (t1);
1074 p2 = TREE_VALUE (t2);
1078 if ((TREE_CODE (p1) == POINTER_TYPE && TREE_CODE (p2) == POINTER_TYPE)
1079 || (TREE_CODE (p1) == REFERENCE_TYPE && TREE_CODE (p2) == REFERENCE_TYPE))
1082 && (TYPE_MAIN_VARIANT (TREE_TYPE (p1))
1083 == TYPE_MAIN_VARIANT (TREE_TYPE (p2))))
1086 /* The following is wrong for contravariance,
1087 but many programs depend on it. */
1088 if (TREE_TYPE (p1) == void_type_node)
1090 if (TREE_TYPE (p2) == void_type_node)
1092 warn_contravariance = 1;
1095 if (IS_AGGR_TYPE (TREE_TYPE (p1)))
1097 if (comptypes (p2, p1, 0) == 0)
1099 if (comptypes (p1, p2, 0) != 0)
1100 warn_contravariance = 1;
1107 /* Note backwards order due to contravariance. */
1108 if (comp_target_types (p2, p1, 1) == 0)
1110 if (comp_target_types (p1, p2, 1))
1112 warn_contravariance = 1;
1118 /* Target types are compatible--just make sure that if
1119 we use parameter lists, that they are ok as well. */
1120 if (TREE_CODE (p1) == FUNCTION_TYPE || TREE_CODE (p1) == METHOD_TYPE)
1121 switch (comp_target_parms (TYPE_ARG_TYPES (p1),
1122 TYPE_ARG_TYPES (p2),
1130 warn_contravariance = 1;
1133 if (TREE_PURPOSE (t1) && TREE_PURPOSE (t2))
1135 int cmp = simple_cst_equal (TREE_PURPOSE (t1), TREE_PURPOSE (t2));
1137 my_friendly_abort (114);
1142 return 1 + warn_contravariance;
1145 /* Return 1 if PARMS specifies a fixed number of parameters
1146 and none of their types is affected by default promotions. */
1149 self_promoting_args_p (parms)
1153 for (t = parms; t; t = TREE_CHAIN (t))
1155 register tree type = TREE_VALUE (t);
1157 if (TREE_CHAIN (t) == 0 && type != void_type_node)
1163 if (TYPE_MAIN_VARIANT (type) == float_type_node)
1166 if (C_PROMOTING_INTEGER_TYPE_P (type))
1172 /* Return an unsigned type the same as TYPE in other respects.
1174 C++: must make these work for type variants as well. */
1177 unsigned_type (type)
1180 tree type1 = TYPE_MAIN_VARIANT (type);
1181 if (type1 == signed_char_type_node || type1 == char_type_node)
1182 return unsigned_char_type_node;
1183 if (type1 == integer_type_node)
1184 return unsigned_type_node;
1185 if (type1 == short_integer_type_node)
1186 return short_unsigned_type_node;
1187 if (type1 == long_integer_type_node)
1188 return long_unsigned_type_node;
1189 if (type1 == long_long_integer_type_node)
1190 return long_long_unsigned_type_node;
1191 if (type1 == intDI_type_node)
1192 return unsigned_intDI_type_node;
1193 if (type1 == intSI_type_node)
1194 return unsigned_intSI_type_node;
1195 if (type1 == intHI_type_node)
1196 return unsigned_intHI_type_node;
1197 if (type1 == intQI_type_node)
1198 return unsigned_intQI_type_node;
1202 /* Return a signed type the same as TYPE in other respects. */
1208 tree type1 = TYPE_MAIN_VARIANT (type);
1209 if (type1 == unsigned_char_type_node || type1 == char_type_node)
1210 return signed_char_type_node;
1211 if (type1 == unsigned_type_node)
1212 return integer_type_node;
1213 if (type1 == short_unsigned_type_node)
1214 return short_integer_type_node;
1215 if (type1 == long_unsigned_type_node)
1216 return long_integer_type_node;
1217 if (type1 == long_long_unsigned_type_node)
1218 return long_long_integer_type_node;
1219 if (type1 == unsigned_intDI_type_node)
1220 return intDI_type_node;
1221 if (type1 == unsigned_intSI_type_node)
1222 return intSI_type_node;
1223 if (type1 == unsigned_intHI_type_node)
1224 return intHI_type_node;
1225 if (type1 == unsigned_intQI_type_node)
1226 return intQI_type_node;
1230 /* Return a type the same as TYPE except unsigned or
1231 signed according to UNSIGNEDP. */
1234 signed_or_unsigned_type (unsignedp, type)
1238 if (! INTEGRAL_TYPE_P (type))
1240 if (TYPE_PRECISION (type) == TYPE_PRECISION (signed_char_type_node))
1241 return unsignedp ? unsigned_char_type_node : signed_char_type_node;
1242 if (TYPE_PRECISION (type) == TYPE_PRECISION (integer_type_node))
1243 return unsignedp ? unsigned_type_node : integer_type_node;
1244 if (TYPE_PRECISION (type) == TYPE_PRECISION (short_integer_type_node))
1245 return unsignedp ? short_unsigned_type_node : short_integer_type_node;
1246 if (TYPE_PRECISION (type) == TYPE_PRECISION (long_integer_type_node))
1247 return unsignedp ? long_unsigned_type_node : long_integer_type_node;
1248 if (TYPE_PRECISION (type) == TYPE_PRECISION (long_long_integer_type_node))
1249 return (unsignedp ? long_long_unsigned_type_node
1250 : long_long_integer_type_node);
1254 /* Compute the value of the `sizeof' operator. */
1260 enum tree_code code = TREE_CODE (type);
1263 if (current_template_parms)
1264 return build_min (SIZEOF_EXPR, sizetype, type);
1266 if (code == FUNCTION_TYPE)
1268 if (pedantic || warn_pointer_arith)
1269 pedwarn ("ANSI C++ forbids taking the sizeof a function type");
1270 return size_int (1);
1272 if (code == METHOD_TYPE)
1274 if (pedantic || warn_pointer_arith)
1275 pedwarn ("ANSI C++ forbids taking the sizeof a method type");
1276 return size_int (1);
1278 if (code == VOID_TYPE)
1280 if (pedantic || warn_pointer_arith)
1281 pedwarn ("ANSI C++ forbids taking the sizeof a void type");
1282 return size_int (1);
1284 if (code == ERROR_MARK)
1285 return size_int (1);
1287 /* ARM $5.3.2: ``When applied to a reference, the result is the size of the
1288 referenced object.'' */
1289 if (code == REFERENCE_TYPE)
1290 type = TREE_TYPE (type);
1292 /* We couldn't find anything in the ARM or the draft standard that says,
1293 one way or the other, if doing sizeof on something that doesn't have
1294 an object associated with it is correct or incorrect. For example, if
1295 you declare `struct S { char str[16]; };', and in your program do
1296 a `sizeof (S::str)', should we flag that as an error or should we give
1297 the size of it? Since it seems like a reasonable thing to do, we'll go
1298 with giving the value. */
1299 if (code == OFFSET_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 ("`sizeof' applied to a signature type");
1307 return size_int (0);
1310 if (TYPE_SIZE (complete_type (type)) == 0)
1312 cp_error ("`sizeof' applied to incomplete type `%T'", type);
1313 return size_int (0);
1316 /* Convert in case a char is more than one unit. */
1317 t = size_binop (CEIL_DIV_EXPR, TYPE_SIZE (type),
1318 size_int (TYPE_PRECISION (char_type_node)));
1319 /* size_binop does not put the constant in range, so do it now. */
1320 if (TREE_CODE (t) == INTEGER_CST && force_fit_type (t, 0))
1321 TREE_CONSTANT_OVERFLOW (t) = TREE_OVERFLOW (t) = 1;
1329 if (current_template_parms)
1330 return build_min (SIZEOF_EXPR, sizetype, e);
1332 if (TREE_CODE (e) == COMPONENT_REF
1333 && DECL_BIT_FIELD (TREE_OPERAND (e, 1)))
1334 error ("sizeof applied to a bit-field");
1335 /* ANSI says arrays and functions are converted inside comma.
1336 But we can't really convert them in build_compound_expr
1337 because that would break commas in lvalues.
1338 So do the conversion here if operand was a comma. */
1339 if (TREE_CODE (e) == COMPOUND_EXPR
1340 && (TREE_CODE (TREE_TYPE (e)) == ARRAY_TYPE
1341 || TREE_CODE (TREE_TYPE (e)) == FUNCTION_TYPE))
1342 e = default_conversion (e);
1343 else if (TREE_CODE (e) == TREE_LIST)
1345 tree t = TREE_VALUE (e);
1348 && TREE_CODE (TREE_TYPE (t)) == FUNCTION_TYPE)
1349 || is_overloaded_fn (t)))
1350 pedwarn ("ANSI C++ forbids taking the sizeof a function type");
1352 return c_sizeof (TREE_TYPE (e));
1356 c_sizeof_nowarn (type)
1359 enum tree_code code = TREE_CODE (type);
1362 if (code == FUNCTION_TYPE
1363 || code == METHOD_TYPE
1364 || code == VOID_TYPE
1365 || code == ERROR_MARK)
1366 return size_int (1);
1367 if (code == REFERENCE_TYPE)
1368 type = TREE_TYPE (type);
1370 if (TYPE_SIZE (type) == 0)
1371 return size_int (0);
1373 /* Convert in case a char is more than one unit. */
1374 t = size_binop (CEIL_DIV_EXPR, TYPE_SIZE (type),
1375 size_int (TYPE_PRECISION (char_type_node)));
1376 force_fit_type (t, 0);
1380 /* Implement the __alignof keyword: Return the minimum required
1381 alignment of TYPE, measured in bytes. */
1387 enum tree_code code = TREE_CODE (type);
1390 if (code == FUNCTION_TYPE || code == METHOD_TYPE)
1391 return size_int (FUNCTION_BOUNDARY / BITS_PER_UNIT);
1393 if (code == VOID_TYPE || code == ERROR_MARK)
1394 return size_int (1);
1396 /* C++: this is really correct! */
1397 if (code == REFERENCE_TYPE)
1398 type = TREE_TYPE (type);
1400 /* @@ This also produces an error for a signature ref.
1401 In that case we should be able to do better. */
1402 if (IS_SIGNATURE (type))
1404 error ("`__alignof' applied to a signature type");
1405 return size_int (1);
1408 t = size_int (TYPE_ALIGN (type) / BITS_PER_UNIT);
1409 force_fit_type (t, 0);
1413 /* Perform default promotions for C data used in expressions.
1414 Arrays and functions are converted to pointers;
1415 enumeral types or short or char, to int.
1416 In addition, manifest constants symbols are replaced by their values.
1418 C++: this will automatically bash references to their target type. */
1421 decay_conversion (exp)
1424 register tree type = TREE_TYPE (exp);
1425 register enum tree_code code = TREE_CODE (type);
1427 if (code == OFFSET_TYPE /* || TREE_CODE (exp) == OFFSET_REF */ )
1429 if (TREE_CODE (exp) == OFFSET_REF)
1430 return decay_conversion (resolve_offset_ref (exp));
1432 type = TREE_TYPE (type);
1433 code = TREE_CODE (type);
1436 if (code == REFERENCE_TYPE)
1438 exp = convert_from_reference (exp);
1439 type = TREE_TYPE (exp);
1440 code = TREE_CODE (type);
1443 /* Constants can be used directly unless they're not loadable. */
1444 if (TREE_CODE (exp) == CONST_DECL)
1445 exp = DECL_INITIAL (exp);
1446 /* Replace a nonvolatile const static variable with its value. */
1447 else if (TREE_READONLY_DECL_P (exp))
1449 exp = decl_constant_value (exp);
1450 type = TREE_TYPE (exp);
1453 /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
1454 Leave such NOP_EXPRs, since RHS is being used in non-lvalue context. */
1456 if (code == VOID_TYPE)
1458 error ("void value not ignored as it ought to be");
1459 return error_mark_node;
1461 if (code == FUNCTION_TYPE)
1463 return build_unary_op (ADDR_EXPR, exp, 0);
1465 if (code == METHOD_TYPE)
1467 if (TREE_CODE (exp) == OFFSET_REF)
1469 /* FIXME: We should emit an error here about using a ptrmemfunc
1470 for something other than a function call. */
1471 my_friendly_assert (TREE_CODE (TREE_OPERAND (exp, 1)) == FUNCTION_DECL,
1473 return build_unary_op (ADDR_EXPR, TREE_OPERAND (exp, 1), 0);
1475 return build_unary_op (ADDR_EXPR, exp, 0);
1477 if (code == ARRAY_TYPE)
1482 int constp, volatilep;
1484 if (TREE_CODE (exp) == INDIRECT_REF)
1486 /* Stripping away the INDIRECT_REF is not the right
1487 thing to do for references... */
1488 tree inner = TREE_OPERAND (exp, 0);
1489 if (TREE_CODE (TREE_TYPE (inner)) == REFERENCE_TYPE)
1491 inner = build1 (CONVERT_EXPR,
1492 build_pointer_type (TREE_TYPE (TREE_TYPE (inner))),
1494 TREE_REFERENCE_EXPR (inner) = 1;
1496 return convert (build_pointer_type (TREE_TYPE (type)), inner);
1499 if (TREE_CODE (exp) == COMPOUND_EXPR)
1501 tree op1 = decay_conversion (TREE_OPERAND (exp, 1));
1502 return build (COMPOUND_EXPR, TREE_TYPE (op1),
1503 TREE_OPERAND (exp, 0), op1);
1507 && ! (TREE_CODE (exp) == CONSTRUCTOR && TREE_STATIC (exp)))
1509 error ("invalid use of non-lvalue array");
1510 return error_mark_node;
1513 constp = volatilep = 0;
1514 if (TREE_CODE_CLASS (TREE_CODE (exp)) == 'r'
1515 || TREE_CODE_CLASS (TREE_CODE (exp)) == 'd')
1517 constp = TREE_READONLY (exp);
1518 volatilep = TREE_THIS_VOLATILE (exp);
1521 restype = TREE_TYPE (type);
1522 if (TYPE_READONLY (type) || TYPE_VOLATILE (type)
1523 || constp || volatilep)
1524 restype = cp_build_type_variant (restype,
1525 TYPE_READONLY (type) || constp,
1526 TYPE_VOLATILE (type) || volatilep);
1527 ptrtype = build_pointer_type (restype);
1529 if (TREE_CODE (exp) == VAR_DECL)
1531 /* ??? This is not really quite correct
1532 in that the type of the operand of ADDR_EXPR
1533 is not the target type of the type of the ADDR_EXPR itself.
1534 Question is, can this lossage be avoided? */
1535 adr = build1 (ADDR_EXPR, ptrtype, exp);
1536 if (mark_addressable (exp) == 0)
1537 return error_mark_node;
1538 TREE_CONSTANT (adr) = staticp (exp);
1539 TREE_SIDE_EFFECTS (adr) = 0; /* Default would be, same as EXP. */
1542 /* This way is better for a COMPONENT_REF since it can
1543 simplify the offset for a component. */
1544 adr = build_unary_op (ADDR_EXPR, exp, 1);
1545 return convert (ptrtype, adr);
1552 default_conversion (exp)
1556 enum tree_code code;
1558 exp = decay_conversion (exp);
1560 type = TREE_TYPE (exp);
1561 code = TREE_CODE (type);
1563 if (INTEGRAL_CODE_P (code))
1565 tree t = type_promotes_to (type);
1567 return convert (t, exp);
1569 if (flag_traditional
1570 && TYPE_MAIN_VARIANT (type) == float_type_node)
1571 return convert (double_type_node, exp);
1577 build_object_ref (datum, basetype, field)
1578 tree datum, basetype, field;
1581 if (datum == error_mark_node)
1582 return error_mark_node;
1584 dtype = TREE_TYPE (datum);
1585 if (TREE_CODE (dtype) == REFERENCE_TYPE)
1586 dtype = TREE_TYPE (dtype);
1587 if (! IS_AGGR_TYPE_CODE (TREE_CODE (dtype)))
1589 cp_error ("request for member `%T::%D' in expression of non-aggregate type `%T'",
1590 basetype, field, dtype);
1591 return error_mark_node;
1593 else if (IS_SIGNATURE (basetype))
1595 warning ("signature name in scope resolution ignored");
1596 return build_component_ref (datum, field, NULL_TREE, 1);
1598 else if (is_aggr_type (basetype, 1))
1600 tree binfo = binfo_or_else (basetype, dtype);
1602 return build_component_ref (build_scoped_ref (datum, basetype),
1605 return error_mark_node;
1608 /* Like `build_component_ref, but uses an already found field.
1609 Must compute access for C_C_D. Otherwise, ok. */
1611 build_component_ref_1 (datum, field, protect)
1615 register tree basetype = TREE_TYPE (datum);
1616 register enum tree_code code = TREE_CODE (basetype);
1619 if (code == REFERENCE_TYPE)
1621 datum = convert_from_reference (datum);
1622 basetype = TREE_TYPE (datum);
1623 code = TREE_CODE (basetype);
1626 if (! IS_AGGR_TYPE_CODE (code))
1628 if (code != ERROR_MARK)
1629 cp_error ("request for member `%D' in `%E', which is of non-aggregate type `%T'",
1630 field, datum, basetype);
1631 return error_mark_node;
1634 if (TYPE_SIZE (basetype) == 0)
1636 incomplete_type_error (0, basetype);
1637 return error_mark_node;
1640 /* Look up component name in the structure type definition. */
1642 if (field == error_mark_node)
1643 my_friendly_abort (115);
1645 if (TREE_STATIC (field))
1650 tree access = compute_access (TYPE_BINFO (current_class_type), field);
1652 if (access == access_private_node)
1654 cp_error ("field `%D' is private", field);
1655 return error_mark_node;
1657 else if (access == access_protected_node)
1659 cp_error ("field `%D' is protected", field);
1660 return error_mark_node;
1664 ref = build (COMPONENT_REF, TREE_TYPE (field), datum, field);
1666 if (TREE_READONLY (datum) || TREE_READONLY (field))
1667 TREE_READONLY (ref) = 1;
1668 if (TREE_THIS_VOLATILE (datum) || TREE_THIS_VOLATILE (field))
1669 TREE_THIS_VOLATILE (ref) = 1;
1670 if (DECL_MUTABLE_P (field))
1671 TREE_READONLY (ref) = 0;
1676 /* Given a COND_EXPR in T, return it in a form that we can, for
1677 example, use as an lvalue. This code used to be in unary_complex_lvalue,
1678 but we needed it to deal with `a = (d == c) ? b : c' expressions, where
1679 we're dealing with aggregates. So, we now call this in unary_complex_lvalue,
1680 and in build_modify_expr. The case (in particular) that led to this was
1681 with CODE == ADDR_EXPR, since it's not an lvalue when we'd get it there. */
1683 rationalize_conditional_expr (code, t)
1684 enum tree_code code;
1688 build_conditional_expr (TREE_OPERAND (t, 0),
1689 build_unary_op (code, TREE_OPERAND (t, 1), 0),
1690 build_unary_op (code, TREE_OPERAND (t, 2), 0));
1693 /* Given the TYPE of an anonymous union field inside T, return the
1694 FIELD_DECL for the field. If not found return NULL_TREE. Because
1695 anonymous unions can nest, we must also search all anonymous unions
1696 that are directly reachable. */
1698 lookup_anon_field (t, type)
1703 for (field = TYPE_FIELDS (t); field; field = TREE_CHAIN (field))
1705 if (TREE_STATIC (field))
1707 if (TREE_CODE (field) != FIELD_DECL)
1710 /* If we find it directly, return the field. */
1711 if (DECL_NAME (field) == NULL_TREE
1712 && type == TREE_TYPE (field))
1717 /* Otherwise, it could be nested, search harder. */
1718 if (DECL_NAME (field) == NULL_TREE
1719 && TREE_CODE (TREE_TYPE (field)) == UNION_TYPE)
1721 tree subfield = lookup_anon_field (TREE_TYPE (field), type);
1729 /* Build a COMPONENT_REF for a given DATUM, and it's member COMPONENT.
1730 COMPONENT can be an IDENTIFIER_NODE that is the name of the member
1731 that we are interested in, or it can be a FIELD_DECL. */
1733 build_component_ref (datum, component, basetype_path, protect)
1734 tree datum, component, basetype_path;
1737 register tree basetype = TREE_TYPE (datum);
1738 register enum tree_code code;
1739 register tree field = NULL;
1742 if (current_template_parms)
1743 return build_min_nt (COMPONENT_REF, datum, component);
1745 /* If DATUM is a COMPOUND_EXPR or COND_EXPR, move our reference inside it. */
1746 switch (TREE_CODE (datum))
1750 tree value = build_component_ref (TREE_OPERAND (datum, 1), component,
1751 basetype_path, protect);
1752 return build (COMPOUND_EXPR, TREE_TYPE (value),
1753 TREE_OPERAND (datum, 0), value);
1756 return build_conditional_expr
1757 (TREE_OPERAND (datum, 0),
1758 build_component_ref (TREE_OPERAND (datum, 1), component,
1759 basetype_path, protect),
1760 build_component_ref (TREE_OPERAND (datum, 2), component,
1761 basetype_path, protect));
1764 code = TREE_CODE (basetype);
1766 if (code == REFERENCE_TYPE)
1768 datum = convert_from_reference (datum);
1769 basetype = TREE_TYPE (datum);
1770 code = TREE_CODE (basetype);
1772 if (TREE_CODE (datum) == OFFSET_REF)
1774 datum = resolve_offset_ref (datum);
1775 basetype = TREE_TYPE (datum);
1776 code = TREE_CODE (basetype);
1779 /* First, see if there is a field or component with name COMPONENT. */
1780 if (TREE_CODE (component) == TREE_LIST)
1782 my_friendly_assert (!(TREE_CHAIN (component) == NULL_TREE
1783 && DECL_CHAIN (TREE_VALUE (component)) == NULL_TREE), 309);
1784 return build (COMPONENT_REF, TREE_TYPE (component), datum, component);
1787 if (! IS_AGGR_TYPE_CODE (code))
1789 if (code != ERROR_MARK)
1790 cp_error ("request for member `%D' in `%E', which is of non-aggregate type `%T'",
1791 component, datum, basetype);
1792 return error_mark_node;
1795 if (TYPE_SIZE (complete_type (basetype)) == 0)
1797 incomplete_type_error (0, basetype);
1798 return error_mark_node;
1801 if (TREE_CODE (component) == BIT_NOT_EXPR)
1803 if (TYPE_IDENTIFIER (basetype) != TREE_OPERAND (component, 0))
1805 cp_error ("destructor specifier `%T::~%T' must have matching names",
1806 basetype, TREE_OPERAND (component, 0));
1807 return error_mark_node;
1809 if (! TYPE_HAS_DESTRUCTOR (basetype))
1811 cp_error ("type `%T' has no destructor", basetype);
1812 return error_mark_node;
1814 return TREE_VEC_ELT (CLASSTYPE_METHOD_VEC (basetype), 1);
1817 /* Look up component name in the structure type definition. */
1818 if (CLASSTYPE_VFIELD (basetype)
1819 && DECL_NAME (CLASSTYPE_VFIELD (basetype)) == component)
1820 /* Special-case this because if we use normal lookups in an ambiguous
1821 hierarchy, the compiler will abort (because vptr lookups are
1822 not supposed to be ambiguous. */
1823 field = CLASSTYPE_VFIELD (basetype);
1824 else if (TREE_CODE (component) == FIELD_DECL)
1830 if (basetype_path == NULL_TREE)
1831 basetype_path = TYPE_BINFO (basetype);
1832 field = lookup_field (basetype_path, component,
1833 protect && ! VFIELD_NAME_P (component), 0);
1834 if (field == error_mark_node)
1835 return error_mark_node;
1837 if (field == NULL_TREE)
1839 /* Not found as a data field, look for it as a method. If found,
1840 then if this is the only possible one, return it, else
1841 report ambiguity error. */
1842 tree fndecls = lookup_fnfields (basetype_path, component, 1);
1843 if (fndecls == error_mark_node)
1844 return error_mark_node;
1847 if (TREE_CHAIN (fndecls) == NULL_TREE
1848 && DECL_CHAIN (TREE_VALUE (fndecls)) == NULL_TREE)
1850 tree access, fndecl;
1852 /* Unique, so use this one now. */
1853 basetype = TREE_PURPOSE (fndecls);
1854 fndecl = TREE_VALUE (fndecls);
1855 access = compute_access (TREE_PURPOSE (fndecls), fndecl);
1856 if (access == access_public_node)
1858 if (DECL_VINDEX (fndecl)
1859 && ! resolves_to_fixed_type_p (datum, 0))
1861 tree addr = build_unary_op (ADDR_EXPR, datum, 0);
1862 addr = convert_pointer_to (DECL_CONTEXT (fndecl), addr);
1863 datum = build_indirect_ref (addr, NULL_PTR);
1864 my_friendly_assert (datum != error_mark_node, 310);
1865 fndecl = build_vfn_ref (&addr, datum, DECL_VINDEX (fndecl));
1870 if (access == access_protected_node)
1871 cp_error ("member function `%D' is protected", fndecl);
1873 cp_error ("member function `%D' is private", fndecl);
1874 return error_mark_node;
1878 /* Just act like build_offset_ref, since the object does
1879 not matter unless we're actually calling the function. */
1882 t = build_tree_list (error_mark_node, fndecls);
1883 TREE_TYPE (t) = build_offset_type (basetype,
1889 cp_error ("`%#T' has no member named `%D'", basetype, component);
1890 return error_mark_node;
1892 else if (TREE_TYPE (field) == error_mark_node)
1893 return error_mark_node;
1895 if (TREE_CODE (field) != FIELD_DECL)
1897 if (TREE_CODE (field) == TYPE_DECL)
1899 cp_error ("invalid use of type decl `%#D' as expression", field);
1900 return error_mark_node;
1902 else if (DECL_RTL (field) != 0)
1905 TREE_USED (field) = 1;
1910 /* See if we have to do any conversions so that we pick up the field from the
1912 if (DECL_FIELD_CONTEXT (field) != basetype)
1914 tree context = DECL_FIELD_CONTEXT (field);
1915 tree base = context;
1916 while (base != basetype && TYPE_NAME (base)
1917 && ANON_AGGRNAME_P (TYPE_IDENTIFIER (base)))
1919 base = TYPE_CONTEXT (base);
1922 /* Handle base classes here... */
1923 if (base != basetype && TYPE_USES_COMPLEX_INHERITANCE (basetype))
1925 tree addr = build_unary_op (ADDR_EXPR, datum, 0);
1926 if (integer_zerop (addr))
1928 error ("invalid reference to NULL ptr, use ptr-to-member instead");
1929 return error_mark_node;
1931 if (VBASE_NAME_P (DECL_NAME (field)))
1933 /* It doesn't matter which vbase pointer we grab, just
1934 find one of them. */
1935 tree binfo = get_binfo (base,
1936 TREE_TYPE (TREE_TYPE (addr)), 0);
1937 addr = convert_pointer_to_real (binfo, addr);
1940 addr = convert_pointer_to (base, addr);
1941 datum = build_indirect_ref (addr, NULL_PTR);
1942 my_friendly_assert (datum != error_mark_node, 311);
1946 /* Handle things from anon unions here... */
1947 if (TYPE_NAME (context) && ANON_AGGRNAME_P (TYPE_IDENTIFIER (context)))
1949 tree subfield = lookup_anon_field (basetype, context);
1950 tree subdatum = build_component_ref (datum, subfield,
1951 basetype_path, protect);
1952 return build_component_ref (subdatum, field, basetype_path, protect);
1956 ref = fold (build (COMPONENT_REF, TREE_TYPE (field),
1957 break_out_cleanups (datum), field));
1959 if (TREE_READONLY (datum) || TREE_READONLY (field))
1960 TREE_READONLY (ref) = 1;
1961 if (TREE_THIS_VOLATILE (datum) || TREE_THIS_VOLATILE (field))
1962 TREE_THIS_VOLATILE (ref) = 1;
1963 if (DECL_MUTABLE_P (field))
1964 TREE_READONLY (ref) = 0;
1969 /* Given an expression PTR for a pointer, return an expression
1970 for the value pointed to.
1971 ERRORSTRING is the name of the operator to appear in error messages.
1973 This function may need to overload OPERATOR_FNNAME.
1974 Must also handle REFERENCE_TYPEs for C++. */
1977 build_x_indirect_ref (ptr, errorstring)
1983 if (current_template_parms)
1984 return build_min_nt (INDIRECT_REF, ptr);
1986 rval = build_opfncall (INDIRECT_REF, LOOKUP_NORMAL, ptr, NULL_TREE, NULL_TREE);
1989 return build_indirect_ref (ptr, errorstring);
1993 build_indirect_ref (ptr, errorstring)
1997 register tree pointer = (TREE_CODE (TREE_TYPE (ptr)) == REFERENCE_TYPE ?
1998 ptr : default_conversion (ptr));
1999 register tree type = TREE_TYPE (pointer);
2001 if (ptr == current_class_decl)
2004 if (IS_AGGR_TYPE (type))
2006 ptr = build_expr_type_conversion (WANT_POINTER, pointer, 1);
2011 type = TREE_TYPE (pointer);
2015 if (TREE_CODE (type) == POINTER_TYPE || TREE_CODE (type) == REFERENCE_TYPE)
2017 if (TREE_CODE (pointer) == ADDR_EXPR
2018 && (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_OPERAND (pointer, 0)))
2019 == TYPE_MAIN_VARIANT (TREE_TYPE (type)))
2020 && (TREE_READONLY (TREE_OPERAND (pointer, 0))
2021 == TYPE_READONLY (TREE_TYPE (type)))
2022 && (TREE_THIS_VOLATILE (TREE_OPERAND (pointer, 0))
2023 == TYPE_VOLATILE (TREE_TYPE (type))))
2024 return TREE_OPERAND (pointer, 0);
2027 tree t = TREE_TYPE (type);
2028 register tree ref = build1 (INDIRECT_REF,
2029 TYPE_MAIN_VARIANT (t), pointer);
2031 TREE_READONLY (ref) = TYPE_READONLY (t);
2032 TREE_THIS_VOLATILE (ref) = TYPE_VOLATILE (t);
2033 TREE_SIDE_EFFECTS (ref)
2034 = TYPE_VOLATILE (t) || TREE_SIDE_EFFECTS (pointer);
2038 /* `pointer' won't be an error_mark_node if we were given a
2039 pointer to member, so it's cool to check for this here. */
2040 else if (TYPE_PTRMEMFUNC_P (type))
2041 error ("invalid use of `%s' on pointer to member function", errorstring);
2042 else if (TREE_CODE (type) == RECORD_TYPE
2043 && (IS_SIGNATURE_POINTER (type) || IS_SIGNATURE_REFERENCE (type)))
2044 error ("cannot dereference signature pointer/reference");
2045 else if (pointer != error_mark_node)
2048 error ("invalid type argument of `%s'", errorstring);
2050 error ("invalid type argument");
2052 return error_mark_node;
2055 /* This handles expressions of the form "a[i]", which denotes
2058 This is logically equivalent in C to *(a+i), but we may do it differently.
2059 If A is a variable or a member, we generate a primitive ARRAY_REF.
2060 This avoids forcing the array out of registers, and can work on
2061 arrays that are not lvalues (for example, members of structures returned
2064 If INDEX is of some user-defined type, it must be converted to
2065 integer type. Otherwise, to make a compatible PLUS_EXPR, it
2066 will inherit the type of the array, which will be some pointer type. */
2069 build_x_array_ref (array, index)
2072 tree rval = build_opfncall (ARRAY_REF, LOOKUP_NORMAL, array, index, NULL_TREE);
2075 return build_array_ref (array, index);
2079 build_array_ref (array, idx)
2084 error ("subscript missing in array reference");
2085 return error_mark_node;
2088 if (TREE_TYPE (array) == error_mark_node
2089 || TREE_TYPE (idx) == error_mark_node)
2090 return error_mark_node;
2092 if (TREE_CODE (TREE_TYPE (array)) == ARRAY_TYPE
2093 && TREE_CODE (array) != INDIRECT_REF)
2097 /* Subscripting with type char is likely to lose
2098 on a machine where chars are signed.
2099 So warn on any machine, but optionally.
2100 Don't warn for unsigned char since that type is safe.
2101 Don't warn for signed char because anyone who uses that
2102 must have done so deliberately. */
2103 if (warn_char_subscripts
2104 && TYPE_MAIN_VARIANT (TREE_TYPE (idx)) == char_type_node)
2105 warning ("array subscript has type `char'");
2107 /* Apply default promotions *after* noticing character types. */
2108 idx = default_conversion (idx);
2110 if (TREE_CODE (TREE_TYPE (idx)) != INTEGER_TYPE)
2112 error ("array subscript is not an integer");
2113 return error_mark_node;
2116 /* An array that is indexed by a non-constant
2117 cannot be stored in a register; we must be able to do
2118 address arithmetic on its address.
2119 Likewise an array of elements of variable size. */
2120 if (TREE_CODE (idx) != INTEGER_CST
2121 || (TYPE_SIZE (TREE_TYPE (TREE_TYPE (array))) != 0
2122 && TREE_CODE (TYPE_SIZE (TREE_TYPE (TREE_TYPE (array)))) != INTEGER_CST))
2124 if (mark_addressable (array) == 0)
2125 return error_mark_node;
2127 /* An array that is indexed by a constant value which is not within
2128 the array bounds cannot be stored in a register either; because we
2129 would get a crash in store_bit_field/extract_bit_field when trying
2130 to access a non-existent part of the register. */
2131 if (TREE_CODE (idx) == INTEGER_CST
2132 && TYPE_VALUES (TREE_TYPE (array))
2133 && ! int_fits_type_p (idx, TYPE_VALUES (TREE_TYPE (array))))
2135 if (mark_addressable (array) == 0)
2136 return error_mark_node;
2139 if (pedantic && !lvalue_p (array))
2140 pedwarn ("ANSI C++ forbids subscripting non-lvalue array");
2142 /* Note in C++ it is valid to subscript a `register' array, since
2143 it is valid to take the address of something with that
2144 storage specification. */
2148 while (TREE_CODE (foo) == COMPONENT_REF)
2149 foo = TREE_OPERAND (foo, 0);
2150 if (TREE_CODE (foo) == VAR_DECL && DECL_REGISTER (foo))
2151 warning ("subscripting array declared `register'");
2154 type = TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (array)));
2155 rval = build (ARRAY_REF, type, array, idx);
2156 /* Array ref is const/volatile if the array elements are
2157 or if the array is.. */
2158 TREE_READONLY (rval)
2159 |= (TYPE_READONLY (TREE_TYPE (TREE_TYPE (array)))
2160 | TREE_READONLY (array));
2161 TREE_SIDE_EFFECTS (rval)
2162 |= (TYPE_VOLATILE (TREE_TYPE (TREE_TYPE (array)))
2163 | TREE_SIDE_EFFECTS (array));
2164 TREE_THIS_VOLATILE (rval)
2165 |= (TYPE_VOLATILE (TREE_TYPE (TREE_TYPE (array)))
2166 /* This was added by rms on 16 Nov 91.
2167 It fixes vol struct foo *a; a->elts[1]
2168 in an inline function.
2169 Hope it doesn't break something else. */
2170 | TREE_THIS_VOLATILE (array));
2171 return require_complete_type (fold (rval));
2175 tree ar = default_conversion (array);
2176 tree ind = default_conversion (idx);
2178 /* Put the integer in IND to simplify error checking. */
2179 if (TREE_CODE (TREE_TYPE (ar)) == INTEGER_TYPE)
2186 if (ar == error_mark_node)
2189 if (TREE_CODE (TREE_TYPE (ar)) != POINTER_TYPE)
2191 error ("subscripted value is neither array nor pointer");
2192 return error_mark_node;
2194 if (TREE_CODE (TREE_TYPE (ind)) != INTEGER_TYPE)
2196 error ("array subscript is not an integer");
2197 return error_mark_node;
2200 return build_indirect_ref (build_binary_op_nodefault (PLUS_EXPR, ar, ind, PLUS_EXPR),
2205 /* Build a function call to function FUNCTION with parameters PARAMS.
2206 PARAMS is a list--a chain of TREE_LIST nodes--in which the
2207 TREE_VALUE of each node is a parameter-expression.
2208 FUNCTION's data type may be a function type or a pointer-to-function.
2210 For C++: If FUNCTION's data type is a TREE_LIST, then the tree list
2211 is the list of possible methods that FUNCTION could conceivably
2212 be. If the list of methods comes from a class, then it will be
2213 a list of lists (where each element is associated with the class
2214 that produced it), otherwise it will be a simple list (for
2215 functions overloaded in global scope).
2217 In the first case, TREE_VALUE (function) is the head of one of those
2218 lists, and TREE_PURPOSE is the name of the function.
2220 In the second case, TREE_PURPOSE (function) is the function's
2223 DECL is the class instance variable, usually CURRENT_CLASS_DECL. */
2226 * [eichin:19911015.1726EST] actually return a possibly incomplete
2230 build_x_function_call (function, params, decl)
2231 tree function, params, decl;
2236 if (function == error_mark_node)
2237 return error_mark_node;
2239 if (current_template_parms)
2240 return build_min_nt (CALL_EXPR, function, params, 0);
2242 type = TREE_TYPE (function);
2244 if (TREE_CODE (type) == OFFSET_TYPE
2245 && TREE_TYPE (type) == unknown_type_node
2246 && TREE_CODE (function) == TREE_LIST
2247 && TREE_CHAIN (function) == NULL_TREE)
2249 /* Undo (Foo:bar)()... */
2250 type = TYPE_OFFSET_BASETYPE (type);
2251 function = TREE_VALUE (function);
2252 my_friendly_assert (TREE_CODE (function) == TREE_LIST, 999);
2253 my_friendly_assert (TREE_CHAIN (function) == NULL_TREE, 999);
2254 function = TREE_VALUE (function);
2255 my_friendly_assert (TREE_CODE (function) == FUNCTION_DECL, 999);
2256 function = DECL_NAME (function);
2257 return build_method_call (decl, function, params, TYPE_BINFO (type), LOOKUP_NORMAL);
2260 is_method = ((TREE_CODE (function) == TREE_LIST
2261 && current_class_type != NULL_TREE
2262 && IDENTIFIER_CLASS_VALUE (TREE_PURPOSE (function)) == function)
2263 || TREE_CODE (function) == IDENTIFIER_NODE
2264 || TREE_CODE (type) == METHOD_TYPE
2265 || TYPE_PTRMEMFUNC_P (type));
2267 if (TREE_CODE (function) == FUNCTION_DECL
2268 && DECL_STATIC_FUNCTION_P (function))
2269 return build_member_call
2270 (DECL_CONTEXT (function), DECL_NAME (function), params);
2272 /* Handle methods, friends, and overloaded functions, respectively. */
2275 if (TREE_CODE (function) == FUNCTION_DECL)
2277 if (DECL_NAME (function))
2278 function = DECL_NAME (function);
2280 function = TYPE_IDENTIFIER (DECL_CLASS_CONTEXT (function));
2282 else if (TREE_CODE (function) == TREE_LIST)
2284 my_friendly_assert (TREE_CODE (TREE_VALUE (function)) == FUNCTION_DECL, 312);
2285 function = TREE_PURPOSE (function);
2287 else if (TREE_CODE (function) != IDENTIFIER_NODE)
2289 if (TREE_CODE (function) == OFFSET_REF)
2291 if (TREE_OPERAND (function, 0))
2292 decl = TREE_OPERAND (function, 0);
2294 /* Call via a pointer to member function. */
2295 if (decl == NULL_TREE)
2297 error ("pointer to member function called, but not in class scope");
2298 return error_mark_node;
2300 /* What other type of POINTER_TYPE could this be? */
2301 if (TREE_CODE (TREE_TYPE (function)) != POINTER_TYPE
2302 && ! TYPE_PTRMEMFUNC_P (TREE_TYPE (function))
2303 && TREE_CODE (function) != OFFSET_REF)
2304 function = build (OFFSET_REF, TREE_TYPE (type), NULL_TREE, function);
2308 /* this is an abbreviated method call.
2309 must go through here in case it is a virtual function.
2310 @@ Perhaps this could be optimized. */
2312 if (decl == NULL_TREE)
2314 if (current_class_type == NULL_TREE)
2316 error ("object missing in call to method `%s'",
2317 IDENTIFIER_POINTER (function));
2318 return error_mark_node;
2320 /* Yow: call from a static member function. */
2321 decl = build1 (NOP_EXPR, build_pointer_type (current_class_type),
2323 decl = build_indirect_ref (decl, NULL_PTR);
2326 return build_method_call (decl, function, params,
2327 NULL_TREE, LOOKUP_NORMAL);
2329 else if (TREE_CODE (function) == COMPONENT_REF
2330 && type == unknown_type_node)
2332 /* Should we undo what was done in build_component_ref? */
2333 if (TREE_CODE (TREE_PURPOSE (TREE_OPERAND (function, 1))) == TREE_VEC)
2334 /* Get the name that build_component_ref hid. */
2335 function = DECL_NAME (TREE_VALUE (TREE_OPERAND (function, 1)));
2337 function = TREE_PURPOSE (TREE_OPERAND (function, 1));
2338 return build_method_call (decl, function, params,
2339 NULL_TREE, LOOKUP_NORMAL);
2341 else if (TREE_CODE (function) == TREE_LIST)
2343 if (TREE_VALUE (function) == NULL_TREE)
2345 cp_error ("function `%D' declared overloaded, but no definitions appear with which to resolve it?!?",
2346 TREE_PURPOSE (function));
2347 return error_mark_node;
2351 tree val = TREE_VALUE (function);
2353 if (TREE_CODE (val) == TEMPLATE_DECL)
2354 return build_overload_call_maybe
2355 (function, params, LOOKUP_COMPLAIN, (struct candidate *)0);
2356 else if (DECL_CHAIN (val) != NULL_TREE)
2357 return build_overload_call
2358 (function, params, LOOKUP_COMPLAIN, (struct candidate *)0);
2360 my_friendly_abort (360);
2365 if (TREE_CODE (function) == OFFSET_REF)
2367 /* If the component is a data element (or a virtual function), we play
2368 games here to make things work. */
2371 if (TREE_OPERAND (function, 0))
2372 decl = TREE_OPERAND (function, 0);
2376 decl_addr = build_unary_op (ADDR_EXPR, decl, 0);
2377 function = get_member_function_from_ptrfunc (&decl_addr,
2378 TREE_OPERAND (function, 1));
2379 params = tree_cons (NULL_TREE, decl_addr, params);
2380 return build_function_call (function, params);
2383 type = TREE_TYPE (function);
2384 if (type != error_mark_node)
2386 if (TREE_CODE (type) == REFERENCE_TYPE)
2387 type = TREE_TYPE (type);
2389 if (TYPE_LANG_SPECIFIC (type)
2390 && TYPE_OVERLOADS_CALL_EXPR (complete_type (type)))
2391 return build_opfncall (CALL_EXPR, LOOKUP_NORMAL, function, params, NULL_TREE);
2396 tree fntype = TREE_TYPE (function);
2399 /* Explicitly named method? */
2400 if (TREE_CODE (function) == FUNCTION_DECL)
2401 ctypeptr = build_pointer_type (DECL_CLASS_CONTEXT (function));
2402 /* Expression with ptr-to-method type? It could either be a plain
2403 usage, or it might be a case where the ptr-to-method is being
2404 passed in as an argument. */
2405 else if (TYPE_PTRMEMFUNC_P (fntype))
2407 tree rec = TYPE_METHOD_BASETYPE (TREE_TYPE (TYPE_PTRMEMFUNC_FN_TYPE (fntype)));
2408 ctypeptr = build_pointer_type (rec);
2410 /* Unexpected node type? */
2412 my_friendly_abort (116);
2413 if (decl == NULL_TREE)
2415 if (current_function_decl
2416 && DECL_STATIC_FUNCTION_P (current_function_decl))
2417 error ("invalid call to member function needing `this' in static member function scope");
2419 error ("pointer to member function called, but not in class scope");
2420 return error_mark_node;
2422 if (TREE_CODE (TREE_TYPE (decl)) != POINTER_TYPE
2423 && ! TYPE_PTRMEMFUNC_P (TREE_TYPE (decl)))
2425 decl = build_unary_op (ADDR_EXPR, decl, 0);
2426 decl = convert_pointer_to (TREE_TYPE (ctypeptr), decl);
2429 decl = build_c_cast (ctypeptr, decl, 0);
2430 params = tree_cons (NULL_TREE, decl, params);
2433 return build_function_call (function, params);
2436 /* Resolve a pointer to member function. INSTANCE is the object
2437 instance to use, if the member points to a virtual member. */
2440 get_member_function_from_ptrfunc (instance_ptrptr, function)
2441 tree *instance_ptrptr;
2444 if (TREE_CODE (function) == OFFSET_REF)
2446 function = TREE_OPERAND (function, 1);
2449 if (TYPE_PTRMEMFUNC_P (TREE_TYPE (function)))
2451 tree fntype, index, e1, delta, delta2, e2, e3, aref, vtbl;
2454 tree instance_ptr = *instance_ptrptr;
2456 if (TREE_SIDE_EFFECTS (instance_ptr))
2457 instance_ptr = save_expr (instance_ptr);
2459 if (TREE_SIDE_EFFECTS (function))
2460 function = save_expr (function);
2462 fntype = TYPE_PTRMEMFUNC_FN_TYPE (TREE_TYPE (function));
2463 index = save_expr (build_component_ref (function,
2466 e1 = build (GT_EXPR, boolean_type_node, index,
2467 convert (delta_type_node, integer_zero_node));
2468 delta = convert (ptrdiff_type_node,
2469 build_component_ref (function, delta_identifier, 0, 0));
2470 delta2 = DELTA2_FROM_PTRMEMFUNC (function);
2472 /* convert down to the right base, before using the instance. */
2474 = convert_pointer_to_real (TYPE_METHOD_BASETYPE (TREE_TYPE (fntype)),
2476 if (instance == error_mark_node)
2479 vtbl = convert_pointer_to (ptr_type_node, instance);
2482 build_pointer_type (build_pointer_type (vtable_entry_type)),
2483 vtbl, convert (ptrdiff_type_node, delta2));
2484 vtbl = build_indirect_ref (vtbl, NULL_PTR);
2485 aref = build_array_ref (vtbl, build_binary_op (MINUS_EXPR,
2487 integer_one_node, 1));
2488 if (! flag_vtable_thunks)
2490 aref = save_expr (aref);
2492 /* Save the intermediate result in a SAVE_EXPR so we don't have to
2493 compute each component of the virtual function pointer twice. */
2494 if (TREE_CODE (aref) == INDIRECT_REF)
2495 TREE_OPERAND (aref, 0) = save_expr (TREE_OPERAND (aref, 0));
2497 delta = build_binary_op (PLUS_EXPR,
2498 build_conditional_expr (e1, build_component_ref (aref, delta_identifier, 0, 0), integer_zero_node),
2502 *instance_ptrptr = build (PLUS_EXPR, TREE_TYPE (instance_ptr),
2503 instance_ptr, delta);
2504 if (flag_vtable_thunks)
2507 e2 = build_component_ref (aref, pfn_identifier, 0, 0);
2509 e3 = PFN_FROM_PTRMEMFUNC (function);
2510 TREE_TYPE (e2) = TREE_TYPE (e3);
2511 function = build_conditional_expr (e1, e2, e3);
2513 /* Make sure this doesn't get evaluated first inside one of the
2514 branches of the COND_EXPR. */
2515 if (TREE_CODE (instance_ptr) == SAVE_EXPR)
2516 function = build (COMPOUND_EXPR, TREE_TYPE (function),
2517 instance_ptr, function);
2523 build_function_call_real (function, params, require_complete, flags)
2524 tree function, params;
2525 int require_complete, flags;
2527 register tree fntype, fndecl;
2528 register tree value_type;
2529 register tree coerced_params;
2530 tree name = NULL_TREE, assembler_name = NULL_TREE;
2533 /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
2534 Strip such NOP_EXPRs, since FUNCTION is used in non-lvalue context. */
2535 if (TREE_CODE (function) == NOP_EXPR
2536 && TREE_TYPE (function) == TREE_TYPE (TREE_OPERAND (function, 0)))
2537 function = TREE_OPERAND (function, 0);
2539 if (TREE_CODE (function) == FUNCTION_DECL)
2541 name = DECL_NAME (function);
2542 assembler_name = DECL_ASSEMBLER_NAME (function);
2544 GNU_xref_call (current_function_decl,
2545 IDENTIFIER_POINTER (name ? name
2546 : TYPE_IDENTIFIER (DECL_CLASS_CONTEXT (function))));
2547 mark_used (function);
2550 /* Convert anything with function type to a pointer-to-function. */
2553 && IDENTIFIER_LENGTH (name) == 4
2554 && ! strcmp (IDENTIFIER_POINTER (name), "main")
2555 && DECL_CONTEXT (function) == NULL_TREE)
2557 pedwarn ("ANSI C++ forbids calling `main' from within program");
2560 if (pedantic && DECL_THIS_INLINE (function) && ! DECL_INITIAL (function)
2561 && ! DECL_ARTIFICIAL (function)
2562 && ! DECL_PENDING_INLINE_INFO (function))
2563 cp_pedwarn ("inline function `%#D' called before definition",
2566 /* Differs from default_conversion by not setting TREE_ADDRESSABLE
2567 (because calling an inline function does not mean the function
2568 needs to be separately compiled). */
2570 if (DECL_INLINE (function))
2572 /* Is it a synthesized method that needs to be synthesized? */
2573 if (DECL_ARTIFICIAL (function) && ! DECL_INITIAL (function)
2574 /* Kludge: don't synthesize for default args. */
2575 && current_function_decl)
2576 synthesize_method (function);
2578 fntype = build_type_variant (TREE_TYPE (function),
2579 TREE_READONLY (function),
2580 TREE_THIS_VOLATILE (function));
2581 function = build1 (ADDR_EXPR, build_pointer_type (fntype), function);
2584 function = default_conversion (function);
2590 /* Convert anything with function type to a pointer-to-function. */
2591 if (function == error_mark_node)
2592 return error_mark_node;
2593 function = default_conversion (function);
2596 fntype = TREE_TYPE (function);
2598 if (TYPE_PTRMEMFUNC_P (fntype))
2600 tree instance_ptr = build_unary_op (ADDR_EXPR, C_C_D, 0);
2601 fntype = TYPE_PTRMEMFUNC_FN_TYPE (fntype);
2602 function = get_member_function_from_ptrfunc (&instance_ptr, function);
2605 is_method = (TREE_CODE (fntype) == POINTER_TYPE
2606 && TREE_CODE (TREE_TYPE (fntype)) == METHOD_TYPE);
2608 if (!((TREE_CODE (fntype) == POINTER_TYPE
2609 && TREE_CODE (TREE_TYPE (fntype)) == FUNCTION_TYPE)
2612 cp_error ("`%E' cannot be used as a function", function);
2613 return error_mark_node;
2616 /* fntype now gets the type of function pointed to. */
2617 fntype = TREE_TYPE (fntype);
2619 /* Convert the parameters to the types declared in the
2620 function prototype, or apply default promotions. */
2622 if (flags & LOOKUP_COMPLAIN)
2623 coerced_params = convert_arguments (NULL_TREE, TYPE_ARG_TYPES (fntype),
2624 params, fndecl, LOOKUP_NORMAL);
2626 coerced_params = convert_arguments (NULL_TREE, TYPE_ARG_TYPES (fntype),
2629 if (coerced_params == error_mark_node)
2630 if (flags & LOOKUP_SPECULATIVELY)
2633 return error_mark_node;
2635 /* Check for errors in format strings. */
2637 if (warn_format && (name || assembler_name))
2638 check_function_format (name, assembler_name, coerced_params);
2640 /* Recognize certain built-in functions so we can make tree-codes
2641 other than CALL_EXPR. We do this when it enables fold-const.c
2642 to do something useful. */
2644 if (TREE_CODE (function) == ADDR_EXPR
2645 && TREE_CODE (TREE_OPERAND (function, 0)) == FUNCTION_DECL
2646 && DECL_BUILT_IN (TREE_OPERAND (function, 0)))
2647 switch (DECL_FUNCTION_CODE (TREE_OPERAND (function, 0)))
2652 if (coerced_params == 0)
2653 return integer_zero_node;
2654 return build_unary_op (ABS_EXPR, TREE_VALUE (coerced_params), 0);
2658 value_type = TREE_TYPE (fntype) ? TREE_TYPE (fntype) : void_type_node;
2660 register tree result =
2661 build (CALL_EXPR, value_type,
2662 function, coerced_params, NULL_TREE);
2664 TREE_SIDE_EFFECTS (result) = 1;
2666 if (! require_complete)
2667 return convert_from_reference (result);
2668 if (value_type == void_type_node)
2670 result = require_complete_type (result);
2671 return convert_from_reference (result);
2676 build_function_call (function, params)
2677 tree function, params;
2679 return build_function_call_real (function, params, 1, LOOKUP_NORMAL);
2682 /* Convert the actual parameter expressions in the list VALUES
2683 to the types in the list TYPELIST.
2684 If parmdecls is exhausted, or when an element has NULL as its type,
2685 perform the default conversions.
2687 RETURN_LOC is the location of the return value, if known, NULL_TREE
2688 otherwise. This is useful in the case where we can avoid creating
2689 a temporary variable in the case where we can initialize the return
2690 value directly. If we are not eliding constructors, then we set this
2691 to NULL_TREE to avoid this avoidance.
2693 NAME is an IDENTIFIER_NODE or 0. It is used only for error messages.
2695 This is also where warnings about wrong number of args are generated.
2697 Return a list of expressions for the parameters as converted.
2699 Both VALUES and the returned value are chains of TREE_LIST nodes
2700 with the elements of the list in the TREE_VALUE slots of those nodes.
2702 In C++, unspecified trailing parameters can be filled in with their
2703 default arguments, if such were specified. Do so here. */
2706 convert_arguments (return_loc, typelist, values, fndecl, flags)
2707 tree return_loc, typelist, values, fndecl;
2710 register tree typetail, valtail;
2711 register tree result = NULL_TREE;
2715 if (! flag_elide_constructors)
2720 if (TREE_CODE (TREE_TYPE (fndecl)) == METHOD_TYPE)
2722 if (DECL_NAME (fndecl) == NULL_TREE
2723 || IDENTIFIER_HAS_TYPE_VALUE (DECL_NAME (fndecl)))
2724 called_thing = "constructor";
2726 called_thing = "member function";
2729 called_thing = "function";
2732 for (valtail = values, typetail = typelist;
2734 valtail = TREE_CHAIN (valtail), i++)
2736 register tree type = typetail ? TREE_VALUE (typetail) : 0;
2737 register tree val = TREE_VALUE (valtail);
2739 if (val == error_mark_node)
2740 return error_mark_node;
2742 if (type == void_type_node)
2746 cp_error_at ("too many arguments to %s `%+D'", called_thing,
2748 error ("at this point in file");
2751 error ("too many arguments to function");
2752 /* In case anybody wants to know if this argument
2755 TREE_TYPE (tree_last (result)) = error_mark_node;
2759 /* The tree type of the parameter being passed may not yet be
2760 known. In this case, its type is TYPE_UNKNOWN, and will
2761 be instantiated by the type given by TYPE. If TYPE
2762 is also NULL, the tree type of VAL is ERROR_MARK_NODE. */
2763 if (type && type_unknown_p (val))
2764 val = require_instantiated_type (type, val, integer_zero_node);
2765 else if (type_unknown_p (val))
2767 /* Strip the `&' from an overloaded FUNCTION_DECL. */
2768 if (TREE_CODE (val) == ADDR_EXPR)
2769 val = TREE_OPERAND (val, 0);
2770 if (TREE_CODE (val) == TREE_LIST
2771 && TREE_CHAIN (val) == NULL_TREE
2772 && TREE_TYPE (TREE_VALUE (val)) != NULL_TREE
2773 && (TREE_TYPE (val) == unknown_type_node
2774 || DECL_CHAIN (TREE_VALUE (val)) == NULL_TREE))
2775 /* Instantiates automatically. */
2776 val = TREE_VALUE (val);
2779 error ("insufficient type information in parameter list");
2780 val = integer_zero_node;
2783 else if (TREE_CODE (val) == OFFSET_REF
2784 && TREE_CODE (TREE_TYPE (val)) == METHOD_TYPE)
2786 /* This is unclean. Should be handled elsewhere. */
2787 val = build_unary_op (ADDR_EXPR, val, 0);
2789 else if (TREE_CODE (val) == OFFSET_REF)
2790 val = resolve_offset_ref (val);
2792 /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
2793 Strip such NOP_EXPRs, since VAL is used in non-lvalue context. */
2794 if (TREE_CODE (val) == NOP_EXPR
2795 && TREE_TYPE (val) == TREE_TYPE (TREE_OPERAND (val, 0))
2796 && (type == 0 || TREE_CODE (type) != REFERENCE_TYPE))
2797 val = TREE_OPERAND (val, 0);
2799 if (type == 0 || TREE_CODE (type) != REFERENCE_TYPE)
2801 if (TREE_CODE (TREE_TYPE (val)) == ARRAY_TYPE
2802 || TREE_CODE (TREE_TYPE (val)) == FUNCTION_TYPE
2803 || TREE_CODE (TREE_TYPE (val)) == METHOD_TYPE)
2804 val = default_conversion (val);
2806 val = require_complete_type (val);
2809 if (val == error_mark_node)
2810 return error_mark_node;
2814 /* Formal parm type is specified by a function prototype. */
2817 if (TYPE_SIZE (complete_type (type)) == 0)
2819 error ("parameter type of called function is incomplete");
2824 parmval = convert_for_initialization (return_loc, type, val,
2825 flags|INDIRECT_BIND,
2826 "argument passing", fndecl, i);
2827 #ifdef PROMOTE_PROTOTYPES
2828 if ((TREE_CODE (type) == INTEGER_TYPE
2829 || TREE_CODE (type) == ENUMERAL_TYPE)
2830 && (TYPE_PRECISION (type) < TYPE_PRECISION (integer_type_node)))
2831 parmval = default_conversion (parmval);
2835 if (parmval == error_mark_node)
2836 return error_mark_node;
2838 result = tree_cons (NULL_TREE, parmval, result);
2842 if (TREE_CODE (TREE_TYPE (val)) == REFERENCE_TYPE)
2843 val = convert_from_reference (val);
2845 if (TREE_CODE (TREE_TYPE (val)) == REAL_TYPE
2846 && (TYPE_PRECISION (TREE_TYPE (val))
2847 < TYPE_PRECISION (double_type_node)))
2848 /* Convert `float' to `double'. */
2849 result = tree_cons (NULL_TREE, convert (double_type_node, val), result);
2850 else if (TYPE_LANG_SPECIFIC (TREE_TYPE (val))
2851 && ! TYPE_HAS_TRIVIAL_INIT_REF (TREE_TYPE (val)))
2853 cp_warning ("cannot pass objects of type `%T' through `...'",
2855 result = tree_cons (NULL_TREE, val, result);
2858 /* Convert `short' and `char' to full-size `int'. */
2859 result = tree_cons (NULL_TREE, default_conversion (val), result);
2863 typetail = TREE_CHAIN (typetail);
2866 if (typetail != 0 && typetail != void_list_node)
2868 /* See if there are default arguments that can be used */
2869 if (TREE_PURPOSE (typetail))
2871 for (; typetail != void_list_node; ++i)
2873 tree type = TREE_VALUE (typetail);
2874 tree val = break_out_target_exprs (TREE_PURPOSE (typetail));
2877 if (val == NULL_TREE)
2878 parmval = error_mark_node;
2879 else if (TREE_CODE (val) == CONSTRUCTOR)
2881 parmval = digest_init (type, val, (tree *)0);
2882 parmval = convert_for_initialization (return_loc, type, parmval, flags,
2883 "default constructor", fndecl, i);
2887 /* This could get clobbered by the following call. */
2888 if (TREE_HAS_CONSTRUCTOR (val))
2889 val = copy_node (val);
2891 parmval = convert_for_initialization (return_loc, type, val, flags,
2892 "default argument", fndecl, i);
2893 #ifdef PROMOTE_PROTOTYPES
2894 if ((TREE_CODE (type) == INTEGER_TYPE
2895 || TREE_CODE (type) == ENUMERAL_TYPE)
2896 && (TYPE_PRECISION (type) < TYPE_PRECISION (integer_type_node)))
2897 parmval = default_conversion (parmval);
2901 if (parmval == error_mark_node)
2902 return error_mark_node;
2904 result = tree_cons (0, parmval, result);
2905 typetail = TREE_CHAIN (typetail);
2906 /* ends with `...'. */
2907 if (typetail == NULL_TREE)
2915 char *buf = (char *)alloca (32 + strlen (called_thing));
2916 sprintf (buf, "too few arguments to %s `%%#D'", called_thing);
2917 cp_error_at (buf, fndecl);
2918 error ("at this point in file");
2921 error ("too few arguments to function");
2922 return error_mark_list;
2926 return nreverse (result);
2929 /* Build a binary-operation expression, after performing default
2930 conversions on the operands. CODE is the kind of expression to build. */
2933 build_x_binary_op (code, arg1, arg2)
2934 enum tree_code code;
2939 if (current_template_parms)
2940 return build_min_nt (code, arg1, arg2);
2942 rval = build_opfncall (code, LOOKUP_SPECULATIVELY,
2943 arg1, arg2, NULL_TREE);
2945 return build_opfncall (code, LOOKUP_NORMAL, arg1, arg2, NULL_TREE);
2946 if (code == MEMBER_REF)
2947 return build_m_component_ref (build_indirect_ref (arg1, NULL_PTR),
2949 return build_binary_op (code, arg1, arg2, 1);
2953 build_binary_op (code, arg1, arg2, convert_p)
2954 enum tree_code code;
2966 args[0] = decay_conversion (args[0]);
2967 args[1] = decay_conversion (args[1]);
2969 if (args[0] == error_mark_node || args[1] == error_mark_node)
2970 return error_mark_node;
2972 type0 = TREE_TYPE (args[0]);
2973 type1 = TREE_TYPE (args[1]);
2975 if (type_unknown_p (args[0]))
2977 args[0] = instantiate_type (type1, args[0], 1);
2978 args[0] = decay_conversion (args[0]);
2980 else if (type_unknown_p (args[1]))
2982 args[1] = require_instantiated_type (type0, args[1],
2984 args[1] = decay_conversion (args[1]);
2987 if (IS_AGGR_TYPE (type0) || IS_AGGR_TYPE (type1))
2989 /* Try to convert this to something reasonable. */
2990 if (! build_default_binary_type_conversion(code, &args[0], &args[1]))
2992 cp_error ("no match for `%O(%#T, %#T)'", code,
2993 TREE_TYPE (arg1), TREE_TYPE (arg2));
2994 return error_mark_node;
2998 return build_binary_op_nodefault (code, args[0], args[1], code);
3001 /* Build a binary-operation expression without default conversions.
3002 CODE is the kind of expression to build.
3003 This function differs from `build' in several ways:
3004 the data type of the result is computed and recorded in it,
3005 warnings are generated if arg data types are invalid,
3006 special handling for addition and subtraction of pointers is known,
3007 and some optimization is done (operations on narrow ints
3008 are done in the narrower type when that gives the same result).
3009 Constant folding is also done before the result is returned.
3011 ERROR_CODE is the code that determines what to say in error messages.
3012 It is usually, but not always, the same as CODE.
3014 Note that the operands will never have enumeral types
3015 because either they have just had the default conversions performed
3016 or they have both just been converted to some other type in which
3017 the arithmetic is to be done.
3019 C++: must do special pointer arithmetic when implementing
3020 multiple inheritance, and deal with pointer to member functions. */
3023 build_binary_op_nodefault (code, orig_op0, orig_op1, error_code)
3024 enum tree_code code;
3025 tree orig_op0, orig_op1;
3026 enum tree_code error_code;
3029 register enum tree_code code0, code1;
3032 /* Expression code to give to the expression when it is built.
3033 Normally this is CODE, which is what the caller asked for,
3034 but in some special cases we change it. */
3035 register enum tree_code resultcode = code;
3037 /* Data type in which the computation is to be performed.
3038 In the simplest cases this is the common type of the arguments. */
3039 register tree result_type = NULL;
3041 /* Nonzero means operands have already been type-converted
3042 in whatever way is necessary.
3043 Zero means they need to be converted to RESULT_TYPE. */
3046 /* Nonzero means create the expression with this type, rather than
3048 tree build_type = 0;
3050 /* Nonzero means after finally constructing the expression
3051 convert it to this type. */
3052 tree final_type = 0;
3054 /* Nonzero if this is an operation like MIN or MAX which can
3055 safely be computed in short if both args are promoted shorts.
3056 Also implies COMMON.
3057 -1 indicates a bitwise operation; this makes a difference
3058 in the exact conditions for when it is safe to do the operation
3059 in a narrower mode. */
3062 /* Nonzero if this is a comparison operation;
3063 if both args are promoted shorts, compare the original shorts.
3064 Also implies COMMON. */
3065 int short_compare = 0;
3067 /* Nonzero if this is a right-shift operation, which can be computed on the
3068 original short and then promoted if the operand is a promoted short. */
3069 int short_shift = 0;
3071 /* Nonzero means set RESULT_TYPE to the common type of the args. */
3074 /* Apply default conversions. */
3075 if (code == TRUTH_AND_EXPR || code == TRUTH_ANDIF_EXPR
3076 || code == TRUTH_OR_EXPR || code == TRUTH_ORIF_EXPR
3077 || code == TRUTH_XOR_EXPR)
3079 op0 = decay_conversion (orig_op0);
3080 op1 = decay_conversion (orig_op1);
3084 op0 = default_conversion (orig_op0);
3085 op1 = default_conversion (orig_op1);
3088 type0 = TREE_TYPE (op0);
3089 type1 = TREE_TYPE (op1);
3091 /* The expression codes of the data types of the arguments tell us
3092 whether the arguments are integers, floating, pointers, etc. */
3093 code0 = TREE_CODE (type0);
3094 code1 = TREE_CODE (type1);
3096 /* Strip NON_LVALUE_EXPRs, etc., since we aren't using as an lvalue. */
3097 STRIP_TYPE_NOPS (op0);
3098 STRIP_TYPE_NOPS (op1);
3100 /* If an error was already reported for one of the arguments,
3101 avoid reporting another error. */
3103 if (code0 == ERROR_MARK || code1 == ERROR_MARK)
3104 return error_mark_node;
3109 /* Handle the pointer + int case. */
3110 if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
3111 return pointer_int_sum (PLUS_EXPR, op0, op1);
3112 else if (code1 == POINTER_TYPE && code0 == INTEGER_TYPE)
3113 return pointer_int_sum (PLUS_EXPR, op1, op0);
3119 /* Subtraction of two similar pointers.
3120 We must subtract them as integers, then divide by object size. */
3121 if (code0 == POINTER_TYPE && code1 == POINTER_TYPE
3122 && comp_target_types (type0, type1, 1))
3123 return pointer_diff (op0, op1);
3124 /* Handle pointer minus int. Just like pointer plus int. */
3125 else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
3126 return pointer_int_sum (MINUS_EXPR, op0, op1);
3135 case TRUNC_DIV_EXPR:
3137 case FLOOR_DIV_EXPR:
3138 case ROUND_DIV_EXPR:
3139 case EXACT_DIV_EXPR:
3140 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE)
3141 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE))
3143 if (TREE_CODE (op1) == INTEGER_CST && integer_zerop (op1))
3144 cp_warning ("division by zero in `%E / 0'", op0);
3145 else if (TREE_CODE (op1) == REAL_CST && real_zerop (op1))
3146 cp_warning ("division by zero in `%E / 0.'", op0);
3148 if (!(code0 == INTEGER_TYPE && code1 == INTEGER_TYPE))
3149 resultcode = RDIV_EXPR;
3151 /* When dividing two signed integers, we have to promote to int.
3152 unless we divide by a constant != -1. Note that default
3153 conversion will have been performed on the operands at this
3154 point, so we have to dig out the original type to find out if
3156 shorten = ((TREE_CODE (op0) == NOP_EXPR
3157 && TREE_UNSIGNED (TREE_TYPE (TREE_OPERAND (op0, 0))))
3158 || (TREE_CODE (op1) == INTEGER_CST
3159 && (TREE_INT_CST_LOW (op1) != -1
3160 || TREE_INT_CST_HIGH (op1) != -1)));
3166 case BIT_ANDTC_EXPR:
3169 if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
3171 /* If one operand is a constant, and the other is a short type
3172 that has been converted to an int,
3173 really do the work in the short type and then convert the
3174 result to int. If we are lucky, the constant will be 0 or 1
3175 in the short type, making the entire operation go away. */
3176 if (TREE_CODE (op0) == INTEGER_CST
3177 && TREE_CODE (op1) == NOP_EXPR
3178 && TYPE_PRECISION (type1) > TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (op1, 0)))
3179 && TREE_UNSIGNED (TREE_TYPE (TREE_OPERAND (op1, 0))))
3181 final_type = result_type;
3182 op1 = TREE_OPERAND (op1, 0);
3183 result_type = TREE_TYPE (op1);
3185 if (TREE_CODE (op1) == INTEGER_CST
3186 && TREE_CODE (op0) == NOP_EXPR
3187 && TYPE_PRECISION (type0) > TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (op0, 0)))
3188 && TREE_UNSIGNED (TREE_TYPE (TREE_OPERAND (op0, 0))))
3190 final_type = result_type;
3191 op0 = TREE_OPERAND (op0, 0);
3192 result_type = TREE_TYPE (op0);
3196 case TRUNC_MOD_EXPR:
3197 case FLOOR_MOD_EXPR:
3198 if (code1 == INTEGER_TYPE && integer_zerop (op1))
3199 cp_warning ("division by zero in `%E % 0'", op0);
3200 else if (code1 == REAL_TYPE && real_zerop (op1))
3201 cp_warning ("division by zero in `%E % 0.'", op0);
3203 if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
3205 /* Although it would be tempting to shorten always here, that loses
3206 on some targets, since the modulo instruction is undefined if the
3207 quotient can't be represented in the computation mode. We shorten
3208 only if unsigned or if dividing by something we know != -1. */
3209 shorten = ((TREE_CODE (op0) == NOP_EXPR
3210 && TREE_UNSIGNED (TREE_TYPE (TREE_OPERAND (op0, 0))))
3211 || (TREE_CODE (op1) == INTEGER_CST
3212 && (TREE_INT_CST_LOW (op1) != -1
3213 || TREE_INT_CST_HIGH (op1) != -1)));
3218 case TRUTH_ANDIF_EXPR:
3219 case TRUTH_ORIF_EXPR:
3220 case TRUTH_AND_EXPR:
3222 result_type = boolean_type_node;
3225 /* Shift operations: result has same type as first operand;
3226 always convert second operand to int.
3227 Also set SHORT_SHIFT if shifting rightward. */
3230 if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
3232 result_type = type0;
3233 if (TREE_CODE (op1) == INTEGER_CST)
3235 if (tree_int_cst_lt (op1, integer_zero_node))
3236 warning ("right shift count is negative");
3239 if (TREE_INT_CST_LOW (op1) | TREE_INT_CST_HIGH (op1))
3241 if (TREE_INT_CST_HIGH (op1) != 0
3242 || ((unsigned HOST_WIDE_INT) TREE_INT_CST_LOW (op1)
3243 >= TYPE_PRECISION (type0)))
3244 warning ("right shift count >= width of type");
3247 /* Convert the shift-count to an integer, regardless of
3248 size of value being shifted. */
3249 if (TYPE_MAIN_VARIANT (TREE_TYPE (op1)) != integer_type_node)
3250 op1 = convert (integer_type_node, op1);
3251 /* Avoid converting op1 to result_type later. */
3257 if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
3259 result_type = type0;
3260 if (TREE_CODE (op1) == INTEGER_CST)
3262 if (tree_int_cst_lt (op1, integer_zero_node))
3263 warning ("left shift count is negative");
3264 else if (TREE_INT_CST_HIGH (op1) != 0
3265 || ((unsigned HOST_WIDE_INT) TREE_INT_CST_LOW (op1)
3266 >= TYPE_PRECISION (type0)))
3267 warning ("left shift count >= width of type");
3269 /* Convert the shift-count to an integer, regardless of
3270 size of value being shifted. */
3271 if (TYPE_MAIN_VARIANT (TREE_TYPE (op1)) != integer_type_node)
3272 op1 = convert (integer_type_node, op1);
3273 /* Avoid converting op1 to result_type later. */
3280 if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
3282 result_type = type0;
3283 if (TREE_CODE (op1) == INTEGER_CST)
3285 if (tree_int_cst_lt (op1, integer_zero_node))
3286 warning ("%s rotate count is negative",
3287 (code == LROTATE_EXPR) ? "left" : "right");
3288 else if (TREE_INT_CST_HIGH (op1) != 0
3289 || ((unsigned HOST_WIDE_INT) TREE_INT_CST_LOW (op1)
3290 >= TYPE_PRECISION (type0)))
3291 warning ("%s rotate count >= width of type",
3292 (code == LROTATE_EXPR) ? "left" : "right");
3294 /* Convert the shift-count to an integer, regardless of
3295 size of value being shifted. */
3296 if (TYPE_MAIN_VARIANT (TREE_TYPE (op1)) != integer_type_node)
3297 op1 = convert (integer_type_node, op1);
3303 build_type = boolean_type_node;
3304 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE)
3305 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE))
3307 else if (code0 == POINTER_TYPE && code1 == POINTER_TYPE)
3309 register tree tt0 = TYPE_MAIN_VARIANT (TREE_TYPE (type0));
3310 register tree tt1 = TYPE_MAIN_VARIANT (TREE_TYPE (type1));
3312 if (comp_target_types (type0, type1, 1))
3313 result_type = common_type (type0, type1);
3314 else if (tt0 == void_type_node)
3316 if (pedantic && TREE_CODE (tt1) == FUNCTION_TYPE
3317 && tree_int_cst_lt (TYPE_SIZE (type0), TYPE_SIZE (type1)))
3318 pedwarn ("ANSI C++ forbids comparison of `void *' with function pointer");
3319 else if (TREE_CODE (tt1) == OFFSET_TYPE)
3320 pedwarn ("ANSI C++ forbids conversion of a pointer to member to `void *'");
3322 else if (tt1 == void_type_node)
3324 if (pedantic && TREE_CODE (tt0) == FUNCTION_TYPE
3325 && tree_int_cst_lt (TYPE_SIZE (type1), TYPE_SIZE (type0)))
3326 pedwarn ("ANSI C++ forbids comparison of `void *' with function pointer");
3329 cp_pedwarn ("comparison of distinct pointer types `%T' and `%T' lacks a cast",
3332 if (result_type == NULL_TREE)
3333 result_type = ptr_type_node;
3335 else if (code0 == POINTER_TYPE && TREE_CODE (op1) == INTEGER_CST
3336 && integer_zerop (op1))
3337 result_type = type0;
3338 else if (code1 == POINTER_TYPE && TREE_CODE (op0) == INTEGER_CST
3339 && integer_zerop (op0))
3340 result_type = type1;
3341 else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
3343 result_type = type0;
3344 error ("ANSI C++ forbids comparison between pointer and integer");
3346 else if (code0 == INTEGER_TYPE && code1 == POINTER_TYPE)
3348 result_type = type1;
3349 error ("ANSI C++ forbids comparison between pointer and integer");
3351 else if (TYPE_PTRMEMFUNC_P (type0) && TREE_CODE (op1) == INTEGER_CST
3352 && integer_zerop (op1))
3354 op0 = build_component_ref (op0, index_identifier, 0, 0);
3355 op1 = integer_zero_node;
3356 result_type = TREE_TYPE (op0);
3358 else if (TYPE_PTRMEMFUNC_P (type1) && TREE_CODE (op0) == INTEGER_CST
3359 && integer_zerop (op0))
3361 op0 = build_component_ref (op1, index_identifier, 0, 0);
3362 op1 = integer_zero_node;
3363 result_type = TREE_TYPE (op0);
3365 else if (TYPE_PTRMEMFUNC_P (type0) && TYPE_PTRMEMFUNC_P (type1)
3366 && (TYPE_PTRMEMFUNC_FN_TYPE (type0)
3367 == TYPE_PTRMEMFUNC_FN_TYPE (type1)))
3369 /* The code we generate for the test is:
3371 (op0.index == op1.index
3372 && ((op1.index != -1 && op0.delta2 == op1.delta2)
3373 || op0.pfn == op1.pfn)) */
3375 tree index0 = build_component_ref (op0, index_identifier, 0, 0);
3376 tree index1 = save_expr (build_component_ref (op1, index_identifier, 0, 0));
3377 tree pfn0 = PFN_FROM_PTRMEMFUNC (op0);
3378 tree pfn1 = PFN_FROM_PTRMEMFUNC (op1);
3379 tree delta20 = DELTA2_FROM_PTRMEMFUNC (op0);
3380 tree delta21 = DELTA2_FROM_PTRMEMFUNC (op1);
3382 tree integer_neg_one_node
3383 = build_binary_op (MINUS_EXPR, integer_zero_node, integer_one_node, 1);
3384 e1 = build_binary_op (EQ_EXPR, index0, index1, 1);
3385 e2 = build_binary_op (NE_EXPR, index1, integer_neg_one_node, 1);
3386 e2 = build_binary_op (TRUTH_ANDIF_EXPR, e2, build_binary_op (EQ_EXPR, delta20, delta21, 1), 1);
3387 e3 = build_binary_op (EQ_EXPR, pfn0, pfn1, 1);
3388 e2 = build_binary_op (TRUTH_ORIF_EXPR, e2, e3, 1);
3389 e2 = build_binary_op (TRUTH_ANDIF_EXPR, e1, e2, 1);
3390 if (code == EQ_EXPR)
3392 return build_binary_op (EQ_EXPR, e2, integer_zero_node, 1);
3394 else if (TYPE_PTRMEMFUNC_P (type0)
3395 && TYPE_PTRMEMFUNC_FN_TYPE (type0) == type1)
3397 tree index0 = build_component_ref (op0, index_identifier, 0, 0);
3399 tree pfn0 = PFN_FROM_PTRMEMFUNC (op0);
3400 tree delta20 = DELTA2_FROM_PTRMEMFUNC (op0);
3401 tree delta21 = integer_zero_node;
3403 tree integer_neg_one_node
3404 = build_binary_op (MINUS_EXPR, integer_zero_node, integer_one_node, 1);
3405 if (TREE_CODE (TREE_OPERAND (op1, 0)) == FUNCTION_DECL
3406 && DECL_VINDEX (TREE_OPERAND (op1, 0)))
3408 /* Map everything down one to make room for the null pointer to member. */
3409 index1 = size_binop (PLUS_EXPR,
3410 DECL_VINDEX (TREE_OPERAND (op1, 0)),
3412 op1 = integer_zero_node;
3413 delta21 = CLASSTYPE_VFIELD (TYPE_METHOD_BASETYPE (TREE_TYPE (type1)));
3414 delta21 = DECL_FIELD_BITPOS (delta21);
3415 delta21 = size_binop (FLOOR_DIV_EXPR, delta21, size_int (BITS_PER_UNIT));
3418 index1 = integer_neg_one_node;
3420 tree nop1 = build1 (NOP_EXPR, TYPE_PTRMEMFUNC_FN_TYPE (type0), op1);
3421 TREE_CONSTANT (nop1) = TREE_CONSTANT (op1);
3424 e1 = build_binary_op (EQ_EXPR, index0, index1, 1);
3425 e2 = build_binary_op (NE_EXPR, index1, integer_neg_one_node, 1);
3426 e2 = build_binary_op (TRUTH_ANDIF_EXPR, e2, build_binary_op (EQ_EXPR, delta20, delta21, 1), 1);
3427 e3 = build_binary_op (EQ_EXPR, pfn0, op1, 1);
3428 e2 = build_binary_op (TRUTH_ORIF_EXPR, e2, e3, 1);
3429 e2 = build_binary_op (TRUTH_ANDIF_EXPR, e1, e2, 1);
3430 if (code == EQ_EXPR)
3432 return build_binary_op (EQ_EXPR, e2, integer_zero_node, 1);
3434 else if (TYPE_PTRMEMFUNC_P (type1)
3435 && TYPE_PTRMEMFUNC_FN_TYPE (type1) == type0)
3437 return build_binary_op (code, op1, op0, 1);
3443 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE)
3444 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE))
3446 else if (code0 == POINTER_TYPE && code1 == POINTER_TYPE)
3448 if (comp_target_types (type0, type1, 1))
3449 result_type = common_type (type0, type1);
3452 cp_pedwarn ("comparison of distinct pointer types `%T' and `%T' lacks a cast",
3454 result_type = ptr_type_node;
3463 build_type = boolean_type_node;
3464 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE)
3465 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE))
3467 else if (code0 == POINTER_TYPE && code1 == POINTER_TYPE)
3469 if (comp_target_types (type0, type1, 1))
3470 result_type = common_type (type0, type1);
3473 cp_pedwarn ("comparison of distinct pointer types `%T' and `%T' lacks a cast",
3475 result_type = ptr_type_node;
3478 else if (code0 == POINTER_TYPE && TREE_CODE (op1) == INTEGER_CST
3479 && integer_zerop (op1))
3480 result_type = type0;
3481 else if (code1 == POINTER_TYPE && TREE_CODE (op0) == INTEGER_CST
3482 && integer_zerop (op0))
3483 result_type = type1;
3484 else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
3486 result_type = type0;
3488 pedwarn ("ANSI C++ forbids comparison between pointer and integer");
3489 else if (! flag_traditional)
3490 warning ("comparison between pointer and integer");
3492 else if (code0 == INTEGER_TYPE && code1 == POINTER_TYPE)
3494 result_type = type1;
3496 pedwarn ("ANSI C++ forbids comparison between pointer and integer");
3497 else if (! flag_traditional)
3498 warning ("comparison between pointer and integer");
3503 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE)
3504 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE))
3506 if (shorten || common || short_compare)
3507 result_type = common_type (type0, type1);
3509 /* For certain operations (which identify themselves by shorten != 0)
3510 if both args were extended from the same smaller type,
3511 do the arithmetic in that type and then extend.
3513 shorten !=0 and !=1 indicates a bitwise operation.
3514 For them, this optimization is safe only if
3515 both args are zero-extended or both are sign-extended.
3516 Otherwise, we might change the result.
3517 Eg, (short)-1 | (unsigned short)-1 is (int)-1
3518 but calculated in (unsigned short) it would be (unsigned short)-1. */
3522 int unsigned0, unsigned1;
3523 tree arg0 = get_narrower (op0, &unsigned0);
3524 tree arg1 = get_narrower (op1, &unsigned1);
3525 /* UNS is 1 if the operation to be done is an unsigned one. */
3526 int uns = TREE_UNSIGNED (result_type);
3529 final_type = result_type;
3531 /* Handle the case that OP0 does not *contain* a conversion
3532 but it *requires* conversion to FINAL_TYPE. */
3534 if (op0 == arg0 && TREE_TYPE (op0) != final_type)
3535 unsigned0 = TREE_UNSIGNED (TREE_TYPE (op0));
3536 if (op1 == arg1 && TREE_TYPE (op1) != final_type)
3537 unsigned1 = TREE_UNSIGNED (TREE_TYPE (op1));
3539 /* Now UNSIGNED0 is 1 if ARG0 zero-extends to FINAL_TYPE. */
3541 /* For bitwise operations, signedness of nominal type
3542 does not matter. Consider only how operands were extended. */
3546 /* Note that in all three cases below we refrain from optimizing
3547 an unsigned operation on sign-extended args.
3548 That would not be valid. */
3550 /* Both args variable: if both extended in same way
3551 from same width, do it in that width.
3552 Do it unsigned if args were zero-extended. */
3553 if ((TYPE_PRECISION (TREE_TYPE (arg0))
3554 < TYPE_PRECISION (result_type))
3555 && (TYPE_PRECISION (TREE_TYPE (arg1))
3556 == TYPE_PRECISION (TREE_TYPE (arg0)))
3557 && unsigned0 == unsigned1
3558 && (unsigned0 || !uns))
3560 = signed_or_unsigned_type (unsigned0,
3561 common_type (TREE_TYPE (arg0), TREE_TYPE (arg1)));
3562 else if (TREE_CODE (arg0) == INTEGER_CST
3563 && (unsigned1 || !uns)
3564 && (TYPE_PRECISION (TREE_TYPE (arg1))
3565 < TYPE_PRECISION (result_type))
3566 && (type = signed_or_unsigned_type (unsigned1,
3568 int_fits_type_p (arg0, type)))
3570 else if (TREE_CODE (arg1) == INTEGER_CST
3571 && (unsigned0 || !uns)
3572 && (TYPE_PRECISION (TREE_TYPE (arg0))
3573 < TYPE_PRECISION (result_type))
3574 && (type = signed_or_unsigned_type (unsigned0,
3576 int_fits_type_p (arg1, type)))
3580 /* Shifts can be shortened if shifting right. */
3585 tree arg0 = get_narrower (op0, &unsigned_arg);
3587 final_type = result_type;
3589 if (arg0 == op0 && final_type == TREE_TYPE (op0))
3590 unsigned_arg = TREE_UNSIGNED (TREE_TYPE (op0));
3592 if (TYPE_PRECISION (TREE_TYPE (arg0)) < TYPE_PRECISION (result_type)
3593 /* We can shorten only if the shift count is less than the
3594 number of bits in the smaller type size. */
3595 && TREE_INT_CST_HIGH (op1) == 0
3596 && TYPE_PRECISION (TREE_TYPE (arg0)) > TREE_INT_CST_LOW (op1)
3597 /* If arg is sign-extended and then unsigned-shifted,
3598 we can simulate this with a signed shift in arg's type
3599 only if the extended result is at least twice as wide
3600 as the arg. Otherwise, the shift could use up all the
3601 ones made by sign-extension and bring in zeros.
3602 We can't optimize that case at all, but in most machines
3603 it never happens because available widths are 2**N. */
3604 && (!TREE_UNSIGNED (final_type)
3606 || (((unsigned) 2 * TYPE_PRECISION (TREE_TYPE (arg0)))
3607 <= TYPE_PRECISION (result_type))))
3609 /* Do an unsigned shift if the operand was zero-extended. */
3611 = signed_or_unsigned_type (unsigned_arg,
3613 /* Convert value-to-be-shifted to that type. */
3614 if (TREE_TYPE (op0) != result_type)
3615 op0 = convert (result_type, op0);
3620 /* Comparison operations are shortened too but differently.
3621 They identify themselves by setting short_compare = 1. */
3625 /* Don't write &op0, etc., because that would prevent op0
3626 from being kept in a register.
3627 Instead, make copies of the our local variables and
3628 pass the copies by reference, then copy them back afterward. */
3629 tree xop0 = op0, xop1 = op1, xresult_type = result_type;
3630 enum tree_code xresultcode = resultcode;
3632 = shorten_compare (&xop0, &xop1, &xresult_type, &xresultcode);
3634 return convert (boolean_type_node, val);
3635 op0 = xop0, op1 = xop1;
3637 resultcode = xresultcode;
3640 if (short_compare && warn_sign_compare)
3642 int op0_signed = ! TREE_UNSIGNED (TREE_TYPE (orig_op0));
3643 int op1_signed = ! TREE_UNSIGNED (TREE_TYPE (orig_op1));
3645 int unsignedp0, unsignedp1;
3646 tree primop0 = get_narrower (op0, &unsignedp0);
3647 tree primop1 = get_narrower (op1, &unsignedp1);
3649 /* Check for comparison of different enum types. */
3650 if (flag_int_enum_equivalence == 0
3651 && TREE_CODE (TREE_TYPE (orig_op0)) == ENUMERAL_TYPE
3652 && TREE_CODE (TREE_TYPE (orig_op1)) == ENUMERAL_TYPE
3653 && TYPE_MAIN_VARIANT (TREE_TYPE (orig_op0))
3654 != TYPE_MAIN_VARIANT (TREE_TYPE (orig_op1)))
3656 cp_warning ("comparison between `%#T' and `%#T'",
3657 TREE_TYPE (orig_op0), TREE_TYPE (orig_op1));
3660 /* Give warnings for comparisons between signed and unsigned
3661 quantities that may fail. */
3662 /* Do the checking based on the original operand trees, so that
3663 casts will be considered, but default promotions won't be. */
3665 /* Do not warn if the comparison is being done in a signed type,
3666 since the signed type will only be chosen if it can represent
3667 all the values of the unsigned type. */
3668 if (! TREE_UNSIGNED (result_type))
3670 /* Do not warn if both operands are unsigned. */
3671 else if (op0_signed == op1_signed)
3673 /* Do not warn if the signed quantity is an unsuffixed
3674 integer literal (or some static constant expression
3675 involving such literals) and it is non-negative. */
3676 else if ((op0_signed && TREE_CODE (orig_op0) == INTEGER_CST
3677 && tree_int_cst_sgn (orig_op0) >= 0)
3678 || (op1_signed && TREE_CODE (orig_op1) == INTEGER_CST
3679 && tree_int_cst_sgn (orig_op1) >= 0))
3681 /* Do not warn if the comparison is an equality operation,
3682 the unsigned quantity is an integral constant and it does
3683 not use the most significant bit of result_type. */
3684 else if ((resultcode == EQ_EXPR || resultcode == NE_EXPR)
3685 && ((op0_signed && TREE_CODE (orig_op1) == INTEGER_CST
3686 && int_fits_type_p (orig_op1, signed_type (result_type))
3687 || (op1_signed && TREE_CODE (orig_op0) == INTEGER_CST
3688 && int_fits_type_p (orig_op0, signed_type (result_type))))))
3691 warning ("comparison between signed and unsigned");
3693 /* Warn if two unsigned values are being compared in a size
3694 larger than their original size, and one (and only one) is the
3695 result of a `~' operator. This comparison will always fail.
3697 Also warn if one operand is a constant, and the constant does not
3698 have all bits set that are set in the ~ operand when it is
3701 if ((TREE_CODE (primop0) == BIT_NOT_EXPR)
3702 ^ (TREE_CODE (primop1) == BIT_NOT_EXPR))
3704 if (TREE_CODE (primop0) == BIT_NOT_EXPR)
3705 primop0 = get_narrower (TREE_OPERAND (op0, 0), &unsignedp0);
3706 if (TREE_CODE (primop1) == BIT_NOT_EXPR)
3707 primop1 = get_narrower (TREE_OPERAND (op1, 0), &unsignedp1);
3709 if (TREE_CODE (primop0) == INTEGER_CST
3710 || TREE_CODE (primop1) == INTEGER_CST)
3713 HOST_WIDE_INT constant, mask;
3717 if (TREE_CODE (primop0) == INTEGER_CST)
3720 unsignedp = unsignedp1;
3721 constant = TREE_INT_CST_LOW (primop0);
3726 unsignedp = unsignedp0;
3727 constant = TREE_INT_CST_LOW (primop1);
3730 bits = TYPE_PRECISION (TREE_TYPE (primop));
3731 if (bits < TYPE_PRECISION (result_type)
3732 && bits < HOST_BITS_PER_LONG && unsignedp)
3734 mask = (~ (HOST_WIDE_INT) 0) << bits;
3735 if ((mask & constant) != mask)
3736 warning ("comparison of promoted ~unsigned with constant");
3739 else if (unsignedp0 && unsignedp1
3740 && (TYPE_PRECISION (TREE_TYPE (primop0))
3741 < TYPE_PRECISION (result_type))
3742 && (TYPE_PRECISION (TREE_TYPE (primop1))
3743 < TYPE_PRECISION (result_type)))
3744 warning ("comparison of promoted ~unsigned with unsigned");
3749 /* At this point, RESULT_TYPE must be nonzero to avoid an error message.
3750 If CONVERTED is zero, both args will be converted to type RESULT_TYPE.
3751 Then the expression will be built.
3752 It will be given type FINAL_TYPE if that is nonzero;
3753 otherwise, it will be given type RESULT_TYPE. */
3757 cp_error ("invalid operands `%T' and `%T' to binary `%O'",
3758 TREE_TYPE (orig_op0), TREE_TYPE (orig_op1), error_code);
3759 return error_mark_node;
3764 if (TREE_TYPE (op0) != result_type)
3765 op0 = convert (result_type, op0);
3766 if (TREE_TYPE (op1) != result_type)
3767 op1 = convert (result_type, op1);
3770 if (build_type == NULL_TREE)
3771 build_type = result_type;
3774 register tree result = build (resultcode, build_type, op0, op1);
3775 register tree folded;
3777 folded = fold (result);
3778 if (folded == result)
3779 TREE_CONSTANT (folded) = TREE_CONSTANT (op0) & TREE_CONSTANT (op1);
3780 if (final_type != 0)
3781 return convert (final_type, folded);
3786 /* Return a tree for the sum or difference (RESULTCODE says which)
3787 of pointer PTROP and integer INTOP. */
3790 pointer_int_sum (resultcode, ptrop, intop)
3791 enum tree_code resultcode;
3792 register tree ptrop, intop;
3796 register tree result;
3797 register tree folded = fold (intop);
3799 /* The result is a pointer of the same type that is being added. */
3801 register tree result_type = TREE_TYPE (ptrop);
3803 if (TREE_CODE (TREE_TYPE (result_type)) == VOID_TYPE)
3805 if (pedantic || warn_pointer_arith)
3806 pedwarn ("ANSI C++ forbids using pointer of type `void *' in arithmetic");
3807 size_exp = integer_one_node;
3809 else if (TREE_CODE (TREE_TYPE (result_type)) == FUNCTION_TYPE)
3811 if (pedantic || warn_pointer_arith)
3812 pedwarn ("ANSI C++ forbids using pointer to a function in arithmetic");
3813 size_exp = integer_one_node;
3815 else if (TREE_CODE (TREE_TYPE (result_type)) == METHOD_TYPE)
3817 if (pedantic || warn_pointer_arith)
3818 pedwarn ("ANSI C++ forbids using pointer to a method in arithmetic");
3819 size_exp = integer_one_node;
3821 else if (TREE_CODE (TREE_TYPE (result_type)) == OFFSET_TYPE)
3823 if (pedantic || warn_pointer_arith)
3824 pedwarn ("ANSI C++ forbids using pointer to a member in arithmetic");
3825 size_exp = integer_one_node;
3828 size_exp = size_in_bytes (complete_type (TREE_TYPE (result_type)));
3830 /* Needed to make OOPS V2R3 work. */
3832 if (TREE_CODE (intop) == INTEGER_CST
3833 && TREE_INT_CST_LOW (intop) == 0
3834 && TREE_INT_CST_HIGH (intop) == 0)
3837 /* If what we are about to multiply by the size of the elements
3838 contains a constant term, apply distributive law
3839 and multiply that constant term separately.
3840 This helps produce common subexpressions. */
3842 if ((TREE_CODE (intop) == PLUS_EXPR || TREE_CODE (intop) == MINUS_EXPR)
3843 && ! TREE_CONSTANT (intop)
3844 && TREE_CONSTANT (TREE_OPERAND (intop, 1))
3845 && TREE_CONSTANT (size_exp))
3847 enum tree_code subcode = resultcode;
3848 if (TREE_CODE (intop) == MINUS_EXPR)
3849 subcode = (subcode == PLUS_EXPR ? MINUS_EXPR : PLUS_EXPR);
3850 ptrop = build_binary_op (subcode, ptrop, TREE_OPERAND (intop, 1), 1);
3851 intop = TREE_OPERAND (intop, 0);
3854 /* Convert the integer argument to a type the same size as sizetype
3855 so the multiply won't overflow spuriously. */
3857 if (TYPE_PRECISION (TREE_TYPE (intop)) != TYPE_PRECISION (sizetype))
3858 intop = convert (type_for_size (TYPE_PRECISION (sizetype), 0), intop);
3860 /* Replace the integer argument with a suitable product by the object size.
3861 Do this multiplication as signed, then convert to the appropriate
3862 pointer type (actually unsigned integral). */
3864 intop = convert (result_type,
3865 build_binary_op (MULT_EXPR, intop,
3866 convert (TREE_TYPE (intop), size_exp), 1));
3868 /* Create the sum or difference. */
3870 result = build (resultcode, result_type, ptrop, intop);
3872 folded = fold (result);
3873 if (folded == result)
3874 TREE_CONSTANT (folded) = TREE_CONSTANT (ptrop) & TREE_CONSTANT (intop);
3878 /* Return a tree for the difference of pointers OP0 and OP1.
3879 The resulting tree has type int. */
3882 pointer_diff (op0, op1)
3883 register tree op0, op1;
3885 register tree result, folded;
3886 tree restype = ptrdiff_type_node;
3887 tree target_type = TREE_TYPE (TREE_TYPE (op0));
3889 if (pedantic || warn_pointer_arith)
3891 if (TREE_CODE (target_type) == VOID_TYPE)
3892 pedwarn ("ANSI C++ forbids using pointer of type `void *' in subtraction");
3893 if (TREE_CODE (target_type) == FUNCTION_TYPE)
3894 pedwarn ("ANSI C++ forbids using pointer to a function in subtraction");
3895 if (TREE_CODE (target_type) == METHOD_TYPE)
3896 pedwarn ("ANSI C++ forbids using pointer to a method in subtraction");
3897 if (TREE_CODE (target_type) == OFFSET_TYPE)
3898 pedwarn ("ANSI C++ forbids using pointer to a member in subtraction");
3901 /* First do the subtraction as integers;
3902 then drop through to build the divide operator. */
3904 op0 = build_binary_op (MINUS_EXPR,
3905 convert (restype, op0), convert (restype, op1), 1);
3907 /* This generates an error if op1 is a pointer to an incomplete type. */
3908 if (TYPE_SIZE (TREE_TYPE (TREE_TYPE (op1))) == 0)
3909 error ("arithmetic on pointer to an incomplete type");
3911 op1 = ((TREE_CODE (target_type) == VOID_TYPE
3912 || TREE_CODE (target_type) == FUNCTION_TYPE
3913 || TREE_CODE (target_type) == METHOD_TYPE
3914 || TREE_CODE (target_type) == OFFSET_TYPE)
3916 : size_in_bytes (target_type));
3918 /* Do the division. */
3920 result = build (EXACT_DIV_EXPR, restype, op0, convert (restype, op1));
3922 folded = fold (result);
3923 if (folded == result)
3924 TREE_CONSTANT (folded) = TREE_CONSTANT (op0) & TREE_CONSTANT (op1);
3928 /* Handle the case of taking the address of a COMPONENT_REF.
3929 Called by `build_unary_op' and `build_up_reference'.
3931 ARG is the COMPONENT_REF whose address we want.
3932 ARGTYPE is the pointer type that this address should have.
3933 MSG is an error message to print if this COMPONENT_REF is not
3934 addressable (such as a bitfield). */
3937 build_component_addr (arg, argtype, msg)
3941 tree field = TREE_OPERAND (arg, 1);
3942 tree basetype = decl_type_context (field);
3943 tree rval = build_unary_op (ADDR_EXPR, TREE_OPERAND (arg, 0), 0);
3945 if (DECL_BIT_FIELD (field))
3947 error (msg, IDENTIFIER_POINTER (DECL_NAME (field)));
3948 return error_mark_node;
3951 if (TREE_CODE (field) == FIELD_DECL
3952 && TYPE_USES_COMPLEX_INHERITANCE (basetype))
3954 /* Can't convert directly to ARGTYPE, since that
3955 may have the same pointer type as one of our
3957 rval = build1 (NOP_EXPR, argtype,
3958 convert_pointer_to (basetype, rval));
3959 TREE_CONSTANT (rval) = TREE_CONSTANT (TREE_OPERAND (rval, 0));
3962 /* This conversion is harmless. */
3963 rval = convert_force (argtype, rval, 0);
3965 if (! integer_zerop (DECL_FIELD_BITPOS (field)))
3967 tree offset = size_binop (EASY_DIV_EXPR, DECL_FIELD_BITPOS (field),
3968 size_int (BITS_PER_UNIT));
3969 int flag = TREE_CONSTANT (rval);
3970 rval = fold (build (PLUS_EXPR, argtype,
3971 rval, convert (argtype, offset)));
3972 TREE_CONSTANT (rval) = flag;
3977 /* Construct and perhaps optimize a tree representation
3978 for a unary operation. CODE, a tree_code, specifies the operation
3979 and XARG is the operand. */
3982 build_x_unary_op (code, xarg)
3983 enum tree_code code;
3986 if (current_template_parms)
3987 return build_min_nt (code, xarg, NULL_TREE);
3989 /* & rec, on incomplete RECORD_TYPEs is the simple opr &, not an
3991 if (code == ADDR_EXPR
3992 && ((IS_AGGR_TYPE_CODE (TREE_CODE (TREE_TYPE (xarg)))
3993 && TYPE_SIZE (TREE_TYPE (xarg)) == NULL_TREE)
3994 || (TREE_CODE (xarg) == OFFSET_REF)))
3995 /* don't look for a function */;
3998 tree rval = build_opfncall (code, LOOKUP_SPECULATIVELY, xarg,
3999 NULL_TREE, NULL_TREE);
4001 return build_opfncall (code, LOOKUP_NORMAL, xarg,
4002 NULL_TREE, NULL_TREE);
4005 if (code == ADDR_EXPR)
4007 if (TREE_CODE (xarg) == TARGET_EXPR)
4008 warning ("taking address of temporary");
4011 return build_unary_op (code, xarg, 0);
4014 /* Just like truthvalue_conversion, but we want a CLEANUP_POINT_EXPR. */
4017 condition_conversion (expr)
4021 if (current_template_parms)
4023 t = convert (boolean_type_node, expr);
4024 t = fold (build1 (CLEANUP_POINT_EXPR, boolean_type_node, t));
4028 /* C++: Must handle pointers to members.
4030 Perhaps type instantiation should be extended to handle conversion
4031 from aggregates to types we don't yet know we want? (Or are those
4032 cases typically errors which should be reported?)
4034 NOCONVERT nonzero suppresses the default promotions
4035 (such as from short to int). */
4037 build_unary_op (code, xarg, noconvert)
4038 enum tree_code code;
4042 /* No default_conversion here. It causes trouble for ADDR_EXPR. */
4043 register tree arg = xarg;
4044 register tree argtype = 0;
4045 char *errstring = NULL;
4048 if (arg == error_mark_node)
4049 return error_mark_node;
4054 /* This is used for unary plus, because a CONVERT_EXPR
4055 is enough to prevent anybody from looking inside for
4056 associativity, but won't generate any code. */
4057 if (!(arg = build_expr_type_conversion
4058 (WANT_ARITH | WANT_ENUM | WANT_POINTER, arg, 1)))
4059 errstring = "wrong type argument to unary plus";
4063 arg = default_conversion (arg);
4064 arg = build1 (NON_LVALUE_EXPR, TREE_TYPE (arg), arg);
4069 if (!(arg = build_expr_type_conversion (WANT_ARITH | WANT_ENUM, arg, 1)))
4070 errstring = "wrong type argument to unary minus";
4071 else if (!noconvert)
4072 arg = default_conversion (arg);
4076 if (!(arg = build_expr_type_conversion (WANT_INT | WANT_ENUM, arg, 1)))
4077 errstring = "wrong type argument to bit-complement";
4078 else if (!noconvert)
4079 arg = default_conversion (arg);
4083 if (!(arg = build_expr_type_conversion (WANT_ARITH | WANT_ENUM, arg, 1)))
4084 errstring = "wrong type argument to abs";
4085 else if (!noconvert)
4086 arg = default_conversion (arg);
4089 case TRUTH_NOT_EXPR:
4090 arg = convert (boolean_type_node, arg);
4091 val = invert_truthvalue (arg);
4092 if (arg != error_mark_node)
4094 errstring = "in argument to unary !";
4100 case PREINCREMENT_EXPR:
4101 case POSTINCREMENT_EXPR:
4102 case PREDECREMENT_EXPR:
4103 case POSTDECREMENT_EXPR:
4104 /* Handle complex lvalues (when permitted)
4105 by reduction to simpler cases. */
4107 val = unary_complex_lvalue (code, arg);
4111 /* Report invalid types. */
4113 if (!(arg = build_expr_type_conversion (WANT_ARITH | WANT_POINTER,
4116 if (code == PREINCREMENT_EXPR)
4117 errstring ="no pre-increment operator for type";
4118 else if (code == POSTINCREMENT_EXPR)
4119 errstring ="no post-increment operator for type";
4120 else if (code == PREDECREMENT_EXPR)
4121 errstring ="no pre-decrement operator for type";
4123 errstring ="no post-decrement operator for type";
4127 /* Report something read-only. */
4129 if (TYPE_READONLY (TREE_TYPE (arg))
4130 || TREE_READONLY (arg))
4131 readonly_error (arg, ((code == PREINCREMENT_EXPR
4132 || code == POSTINCREMENT_EXPR)
4133 ? "increment" : "decrement"),
4138 tree result_type = TREE_TYPE (arg);
4140 arg = get_unwidened (arg, 0);
4141 argtype = TREE_TYPE (arg);
4143 /* ARM $5.2.5 last annotation says this should be forbidden. */
4144 if (TREE_CODE (argtype) == ENUMERAL_TYPE)
4145 pedwarn ("ANSI C++ forbids %sing an enum",
4146 (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
4147 ? "increment" : "decrement");
4149 /* Compute the increment. */
4151 if (TREE_CODE (argtype) == POINTER_TYPE)
4153 enum tree_code tmp = TREE_CODE (TREE_TYPE (argtype));
4154 if (TYPE_SIZE (complete_type (TREE_TYPE (argtype))) == 0)
4155 cp_error ("cannot %s a pointer to incomplete type `%T'",
4156 ((code == PREINCREMENT_EXPR
4157 || code == POSTINCREMENT_EXPR)
4158 ? "increment" : "decrement"), TREE_TYPE (argtype));
4159 else if (tmp == FUNCTION_TYPE || tmp == METHOD_TYPE
4160 || tmp == VOID_TYPE || tmp == OFFSET_TYPE)
4161 cp_pedwarn ("ANSI C++ forbids %sing a pointer of type `%T'",
4162 ((code == PREINCREMENT_EXPR
4163 || code == POSTINCREMENT_EXPR)
4164 ? "increment" : "decrement"), argtype);
4165 inc = c_sizeof_nowarn (TREE_TYPE (argtype));
4168 inc = integer_one_node;
4170 inc = convert (argtype, inc);
4172 /* Handle incrementing a cast-expression. */
4174 switch (TREE_CODE (arg))
4179 case FIX_TRUNC_EXPR:
4180 case FIX_FLOOR_EXPR:
4181 case FIX_ROUND_EXPR:
4184 tree incremented, modify, value, compound;
4185 if (! lvalue_p (arg) && pedantic)
4186 pedwarn ("cast to non-reference type used as lvalue");
4187 arg = stabilize_reference (arg);
4188 if (code == PREINCREMENT_EXPR || code == PREDECREMENT_EXPR)
4191 value = save_expr (arg);
4192 incremented = build (((code == PREINCREMENT_EXPR
4193 || code == POSTINCREMENT_EXPR)
4194 ? PLUS_EXPR : MINUS_EXPR),
4195 argtype, value, inc);
4196 TREE_SIDE_EFFECTS (incremented) = 1;
4198 modify = build_modify_expr (arg, NOP_EXPR, incremented);
4199 compound = build (COMPOUND_EXPR, TREE_TYPE (arg), modify, value);
4201 /* Eliminate warning about unused result of + or -. */
4202 TREE_NO_UNUSED_WARNING (compound) = 1;
4207 /* Complain about anything else that is not a true lvalue. */
4208 if (!lvalue_or_else (arg, ((code == PREINCREMENT_EXPR
4209 || code == POSTINCREMENT_EXPR)
4210 ? "increment" : "decrement")))
4211 return error_mark_node;
4213 /* Forbid using -- on `bool'. */
4214 if (TREE_TYPE (arg) == boolean_type_node)
4216 if (code == POSTDECREMENT_EXPR || code == PREDECREMENT_EXPR)
4218 cp_error ("invalid use of `--' on bool variable `%D'", arg);
4219 return error_mark_node;
4222 /* This will only work if someone can convince Kenner to accept
4223 my patch to expand_increment. (jason) */
4224 val = build (code, TREE_TYPE (arg), arg, inc);
4226 if (code == POSTINCREMENT_EXPR)
4228 arg = stabilize_reference (arg);
4229 val = build (MODIFY_EXPR, TREE_TYPE (arg), arg,
4231 TREE_SIDE_EFFECTS (val) = 1;
4232 arg = save_expr (arg);
4233 val = build (COMPOUND_EXPR, TREE_TYPE (arg), val, arg);
4234 val = build (COMPOUND_EXPR, TREE_TYPE (arg), arg, val);
4237 val = build (MODIFY_EXPR, TREE_TYPE (arg), arg,
4242 val = build (code, TREE_TYPE (arg), arg, inc);
4244 TREE_SIDE_EFFECTS (val) = 1;
4245 return convert (result_type, val);
4249 /* Note that this operation never does default_conversion
4250 regardless of NOCONVERT. */
4252 argtype = TREE_TYPE (arg);
4253 if (TREE_CODE (argtype) == REFERENCE_TYPE)
4255 arg = build1 (CONVERT_EXPR, build_pointer_type (TREE_TYPE (TREE_TYPE (arg))), arg);
4256 TREE_REFERENCE_EXPR (arg) = 1;
4260 && TREE_CODE (arg) == FUNCTION_DECL
4262 && DECL_CONTEXT (arg) == NULL_TREE
4263 && IDENTIFIER_LENGTH (DECL_NAME (arg)) == 4
4264 && IDENTIFIER_POINTER (DECL_NAME (arg))[0] == 'm'
4265 && ! strcmp (IDENTIFIER_POINTER (DECL_NAME (arg)), "main"))
4267 pedwarn ("taking address of function `main'");
4269 /* Let &* cancel out to simplify resulting code. */
4270 if (TREE_CODE (arg) == INDIRECT_REF)
4272 /* We don't need to have `current_class_decl' wrapped in a
4273 NON_LVALUE_EXPR node. */
4275 return current_class_decl;
4277 /* Keep `default_conversion' from converting if
4278 ARG is of REFERENCE_TYPE. */
4279 arg = TREE_OPERAND (arg, 0);
4280 if (TREE_CODE (TREE_TYPE (arg)) == REFERENCE_TYPE)
4282 if (TREE_CODE (arg) == VAR_DECL && DECL_INITIAL (arg)
4283 && !TREE_SIDE_EFFECTS (DECL_INITIAL (arg)))
4284 arg = DECL_INITIAL (arg);
4285 arg = build1 (CONVERT_EXPR, build_pointer_type (TREE_TYPE (TREE_TYPE (arg))), arg);
4286 TREE_REFERENCE_EXPR (arg) = 1;
4287 TREE_CONSTANT (arg) = TREE_CONSTANT (TREE_OPERAND (arg, 0));
4289 else if (lvalue_p (arg))
4290 /* Don't let this be an lvalue. */
4291 return non_lvalue (arg);
4295 /* For &x[y], return x+y */
4296 if (TREE_CODE (arg) == ARRAY_REF)
4298 if (mark_addressable (TREE_OPERAND (arg, 0)) == 0)
4299 return error_mark_node;
4300 return build_binary_op (PLUS_EXPR, TREE_OPERAND (arg, 0),
4301 TREE_OPERAND (arg, 1), 1);
4304 /* Uninstantiated types are all functions. Taking the
4305 address of a function is a no-op, so just return the
4308 if (TREE_CODE (arg) == IDENTIFIER_NODE
4309 && IDENTIFIER_OPNAME_P (arg))
4311 my_friendly_abort (117);
4312 /* We don't know the type yet, so just work around the problem.
4313 We know that this will resolve to an lvalue. */
4314 return build1 (ADDR_EXPR, unknown_type_node, arg);
4317 if (TREE_CODE (arg) == TREE_LIST)
4319 if (TREE_CODE (TREE_VALUE (arg)) == FUNCTION_DECL
4320 && DECL_CHAIN (TREE_VALUE (arg)) == NULL_TREE)
4321 /* Unique overloaded non-member function. */
4322 return build_unary_op (ADDR_EXPR, TREE_VALUE (arg), 0);
4323 if (TREE_CHAIN (arg) == NULL_TREE
4324 && TREE_CODE (TREE_VALUE (arg)) == TREE_LIST
4325 && DECL_CHAIN (TREE_VALUE (TREE_VALUE (arg))) == NULL_TREE)
4326 /* Unique overloaded member function. */
4327 return build_unary_op (ADDR_EXPR, TREE_VALUE (TREE_VALUE (arg)),
4329 return build1 (ADDR_EXPR, unknown_type_node, arg);
4332 /* Handle complex lvalues (when permitted)
4333 by reduction to simpler cases. */
4334 val = unary_complex_lvalue (code, arg);
4338 switch (TREE_CODE (arg))
4343 case FIX_TRUNC_EXPR:
4344 case FIX_FLOOR_EXPR:
4345 case FIX_ROUND_EXPR:
4347 if (! lvalue_p (arg) && pedantic)
4348 pedwarn ("taking the address of a cast to non-reference type");
4351 /* Allow the address of a constructor if all the elements
4353 if (TREE_CODE (arg) == CONSTRUCTOR && TREE_CONSTANT (arg))
4355 /* Anything not already handled and not a true memory reference
4357 else if (TREE_CODE (argtype) != FUNCTION_TYPE
4358 && TREE_CODE (argtype) != METHOD_TYPE
4359 && !lvalue_or_else (arg, "unary `&'"))
4360 return error_mark_node;
4362 /* Ordinary case; arg is a COMPONENT_REF or a decl. */
4363 /* If the lvalue is const or volatile,
4364 merge that into the type that the address will point to. */
4365 if (TREE_CODE_CLASS (TREE_CODE (arg)) == 'd'
4366 || TREE_CODE_CLASS (TREE_CODE (arg)) == 'r')
4368 if (TREE_READONLY (arg) || TREE_THIS_VOLATILE (arg))
4369 argtype = cp_build_type_variant (argtype,
4370 TREE_READONLY (arg),
4371 TREE_THIS_VOLATILE (arg));
4374 argtype = build_pointer_type (argtype);
4376 if (mark_addressable (arg) == 0)
4377 return error_mark_node;
4382 if (TREE_CODE (arg) == COMPONENT_REF)
4383 addr = build_component_addr (arg, argtype,
4384 "attempt to take address of bit-field structure member `%s'");
4386 addr = build1 (code, argtype, arg);
4388 /* Address of a static or external variable or
4389 function counts as a constant */
4391 TREE_CONSTANT (addr) = 1;
4399 argtype = TREE_TYPE (arg);
4400 return fold (build1 (code, argtype, arg));
4404 return error_mark_node;
4408 /* If CONVERSIONS is a conversion expression or a nested sequence of such,
4409 convert ARG with the same conversions in the same order
4410 and return the result. */
4413 convert_sequence (conversions, arg)
4417 switch (TREE_CODE (conversions))
4422 case FIX_TRUNC_EXPR:
4423 case FIX_FLOOR_EXPR:
4424 case FIX_ROUND_EXPR:
4426 return convert (TREE_TYPE (conversions),
4427 convert_sequence (TREE_OPERAND (conversions, 0),
4436 /* Apply unary lvalue-demanding operator CODE to the expression ARG
4437 for certain kinds of expressions which are not really lvalues
4438 but which we can accept as lvalues.
4440 If ARG is not a kind of expression we can handle, return zero. */
4443 unary_complex_lvalue (code, arg)
4444 enum tree_code code;
4447 /* Handle (a, b) used as an "lvalue". */
4448 if (TREE_CODE (arg) == COMPOUND_EXPR)
4450 tree real_result = build_unary_op (code, TREE_OPERAND (arg, 1), 0);
4451 return build (COMPOUND_EXPR, TREE_TYPE (real_result),
4452 TREE_OPERAND (arg, 0), real_result);
4455 /* Handle (a ? b : c) used as an "lvalue". */
4456 if (TREE_CODE (arg) == COND_EXPR)
4457 return rationalize_conditional_expr (code, arg);
4459 if (TREE_CODE (arg) == MODIFY_EXPR
4460 || TREE_CODE (arg) == PREINCREMENT_EXPR
4461 || TREE_CODE (arg) == PREDECREMENT_EXPR)
4462 return unary_complex_lvalue
4463 (code, build (COMPOUND_EXPR, TREE_TYPE (TREE_OPERAND (arg, 0)),
4464 arg, TREE_OPERAND (arg, 0)));
4466 if (code != ADDR_EXPR)
4469 /* Handle (a = b) used as an "lvalue" for `&'. */
4470 if (TREE_CODE (arg) == MODIFY_EXPR
4471 || TREE_CODE (arg) == INIT_EXPR)
4473 tree real_result = build_unary_op (code, TREE_OPERAND (arg, 0), 0);
4474 return build (COMPOUND_EXPR, TREE_TYPE (real_result), arg, real_result);
4477 if (TREE_CODE (TREE_TYPE (arg)) == FUNCTION_TYPE
4478 || TREE_CODE (TREE_TYPE (arg)) == METHOD_TYPE
4479 || TREE_CODE (TREE_TYPE (arg)) == OFFSET_TYPE)
4481 /* The representation of something of type OFFSET_TYPE
4482 is really the representation of a pointer to it.
4483 Here give the representation its true type. */
4487 my_friendly_assert (TREE_CODE (arg) != SCOPE_REF, 313);
4489 if (TREE_CODE (arg) != OFFSET_REF)
4492 t = TREE_OPERAND (arg, 1);
4494 if (TREE_CODE (t) == FUNCTION_DECL) /* Check all this code for right semantics. */
4495 return build_unary_op (ADDR_EXPR, t, 0);
4496 if (TREE_CODE (t) == VAR_DECL)
4497 return build_unary_op (ADDR_EXPR, t, 0);
4500 if (TREE_OPERAND (arg, 0)
4501 && (TREE_CODE (TREE_OPERAND (arg, 0)) != NOP_EXPR
4502 || TREE_OPERAND (TREE_OPERAND (arg, 0), 0) != error_mark_node))
4503 if (TREE_CODE (t) != FIELD_DECL)
4505 /* Don't know if this should return address to just
4506 _DECL, or actual address resolved in this expression. */
4507 sorry ("address of bound pointer-to-member expression");
4508 return error_mark_node;
4511 /* Add in the offset to the right subobject. */
4512 offset = get_delta_difference (DECL_FIELD_CONTEXT (t),
4513 TREE_TYPE (TREE_OPERAND (arg, 0)),
4516 /* Add in the offset to the field. */
4517 offset = size_binop (PLUS_EXPR, offset,
4518 size_binop (EASY_DIV_EXPR,
4519 DECL_FIELD_BITPOS (t),
4520 size_int (BITS_PER_UNIT)));
4522 /* We offset all pointer to data memebers by 1 so that we can
4523 distinguish between a null pointer to data member and the first
4524 data member of a structure. */
4525 offset = size_binop (PLUS_EXPR, offset, size_int (1));
4527 return convert (build_pointer_type (TREE_TYPE (arg)), offset);
4533 /* This seems to be obsolete now (and posssibly wrong, compare with
4534 resolve_offset_ref). */
4535 if (TREE_CODE (arg) == OFFSET_REF)
4537 tree left = TREE_OPERAND (arg, 0), left_addr;
4538 tree right_addr = build_unary_op (ADDR_EXPR, TREE_OPERAND (arg, 1), 0);
4541 if (current_class_decl)
4542 left_addr = current_class_decl;
4545 error ("no `this' for pointer to member");
4546 return error_mark_node;
4549 left_addr = build_unary_op (ADDR_EXPR, left, 0);
4551 return build (PLUS_EXPR, build_pointer_type (TREE_TYPE (arg)),
4552 build1 (NOP_EXPR, integer_type_node, left_addr),
4553 build1 (NOP_EXPR, integer_type_node, right_addr));
4557 /* We permit compiler to make function calls returning
4558 objects of aggregate type look like lvalues. */
4562 if (TREE_CODE (targ) == SAVE_EXPR)
4563 targ = TREE_OPERAND (targ, 0);
4565 if (TREE_CODE (targ) == CALL_EXPR && IS_AGGR_TYPE (TREE_TYPE (targ)))
4567 if (TREE_CODE (arg) == SAVE_EXPR)
4570 targ = build_cplus_new (TREE_TYPE (arg), arg);
4571 return build1 (ADDR_EXPR, build_pointer_type (TREE_TYPE (arg)), targ);
4574 if (TREE_CODE (arg) == SAVE_EXPR && TREE_CODE (targ) == INDIRECT_REF)
4575 return build (SAVE_EXPR, build_pointer_type (TREE_TYPE (arg)),
4576 TREE_OPERAND (targ, 0), current_function_decl, NULL);
4579 /* Don't let anything else be handled specially. */
4583 /* Mark EXP saying that we need to be able to take the
4584 address of it; it should not be allocated in a register.
4585 Value is 1 if successful.
4587 C++: we do not allow `current_class_decl' to be addressable. */
4590 mark_addressable (exp)
4593 register tree x = exp;
4595 if (TREE_ADDRESSABLE (x) == 1)
4599 switch (TREE_CODE (x))
4604 x = TREE_OPERAND (x, 0);
4608 if (x == current_class_decl)
4610 error ("address of `this' not available");
4611 TREE_ADDRESSABLE (x) = 1; /* so compiler doesn't die later */
4612 put_var_into_stack (x);
4616 if (TREE_STATIC (x) && TREE_READONLY (x)
4617 && DECL_RTL (x) != 0
4618 && ! DECL_IN_MEMORY_P (x))
4620 /* We thought this would make a good constant variable,
4621 but we were wrong. */
4622 push_obstacks_nochange ();
4623 end_temporary_allocation ();
4625 TREE_ASM_WRITTEN (x) = 0;
4627 rest_of_decl_compilation (x, 0, IDENTIFIER_LOCAL_VALUE (x) == 0, 0);
4628 TREE_ADDRESSABLE (x) = 1;
4634 /* Caller should not be trying to mark initialized
4635 constant fields addressable. */
4636 my_friendly_assert (DECL_LANG_SPECIFIC (x) == 0
4637 || DECL_IN_AGGR_P (x) == 0
4639 || DECL_EXTERNAL (x), 314);
4643 /* For C++, we don't warn about taking the address of a register
4644 variable for CONST_DECLs; ARM p97 explicitly says it's okay. */
4645 put_var_into_stack (x);
4646 TREE_ADDRESSABLE (x) = 1;
4650 /* We have to test both conditions here. The first may
4651 be non-zero in the case of processing a default function.
4652 The second may be non-zero in the case of a template function. */
4653 x = DECL_MAIN_VARIANT (x);
4654 if ((DECL_THIS_INLINE (x) || DECL_PENDING_INLINE_INFO (x))
4655 && (DECL_CONTEXT (x) == NULL_TREE
4656 || TREE_CODE_CLASS (TREE_CODE (DECL_CONTEXT (x))) != 't'
4657 || ! CLASSTYPE_INTERFACE_ONLY (DECL_CONTEXT (x))))
4659 mark_inline_for_output (x);
4660 if (x == current_function_decl)
4661 DECL_EXTERNAL (x) = 0;
4663 TREE_ADDRESSABLE (x) = 1;
4665 TREE_ADDRESSABLE (DECL_ASSEMBLER_NAME (x)) = 1;
4673 /* Build and return a conditional expression IFEXP ? OP1 : OP2. */
4676 build_x_conditional_expr (ifexp, op1, op2)
4677 tree ifexp, op1, op2;
4679 tree rval = NULL_TREE;
4681 if (current_template_parms)
4682 return build_min_nt (COND_EXPR, ifexp, op1, op2);
4684 /* See comments in `build_x_binary_op'. */
4686 rval = build_opfncall (COND_EXPR, LOOKUP_SPECULATIVELY, ifexp, op1, op2);
4688 return build_opfncall (COND_EXPR, LOOKUP_NORMAL, ifexp, op1, op2);
4690 return build_conditional_expr (ifexp, op1, op2);
4694 build_conditional_expr (ifexp, op1, op2)
4695 tree ifexp, op1, op2;
4697 register tree type1;
4698 register tree type2;
4699 register enum tree_code code1;
4700 register enum tree_code code2;
4701 register tree result_type = NULL_TREE;
4702 tree orig_op1 = op1, orig_op2 = op2;
4704 /* If second operand is omitted, it is the same as the first one;
4705 make sure it is calculated only once. */
4709 pedwarn ("ANSI C++ forbids omitting the middle term of a ?: expression");
4710 ifexp = op1 = save_expr (ifexp);
4713 ifexp = convert (boolean_type_node, ifexp);
4715 if (TREE_CODE (ifexp) == ERROR_MARK)
4716 return error_mark_node;
4718 op1 = require_instantiated_type (TREE_TYPE (op2), op1, error_mark_node);
4719 if (op1 == error_mark_node)
4720 return error_mark_node;
4721 op2 = require_instantiated_type (TREE_TYPE (op1), op2, error_mark_node);
4722 if (op2 == error_mark_node)
4723 return error_mark_node;
4725 /* C++: REFERENCE_TYPES must be dereferenced. */
4726 type1 = TREE_TYPE (op1);
4727 code1 = TREE_CODE (type1);
4728 type2 = TREE_TYPE (op2);
4729 code2 = TREE_CODE (type2);
4731 if (code1 == REFERENCE_TYPE)
4733 op1 = convert_from_reference (op1);
4734 type1 = TREE_TYPE (op1);
4735 code1 = TREE_CODE (type1);
4737 if (code2 == REFERENCE_TYPE)
4739 op2 = convert_from_reference (op2);
4740 type2 = TREE_TYPE (op2);
4741 code2 = TREE_CODE (type2);
4744 /* Don't promote the operands separately if they promote
4745 the same way. Return the unpromoted type and let the combined
4746 value get promoted if necessary. */
4748 if (TYPE_MAIN_VARIANT (type1) == TYPE_MAIN_VARIANT (type2)
4749 && code2 != ARRAY_TYPE
4750 && code2 != FUNCTION_TYPE
4751 && code2 != METHOD_TYPE)
4755 if (TREE_CONSTANT (ifexp)
4756 && (TREE_CODE (ifexp) == INTEGER_CST
4757 || TREE_CODE (ifexp) == ADDR_EXPR))
4758 return (integer_zerop (ifexp) ? op2 : op1);
4760 if (TREE_CODE (op1) == CONST_DECL)
4761 op1 = DECL_INITIAL (op1);
4762 else if (TREE_READONLY_DECL_P (op1))
4763 op1 = decl_constant_value (op1);
4764 if (TREE_CODE (op2) == CONST_DECL)
4765 op2 = DECL_INITIAL (op2);
4766 else if (TREE_READONLY_DECL_P (op2))
4767 op2 = decl_constant_value (op2);
4769 type1 = cp_build_type_variant
4771 TREE_READONLY (op1) || TREE_READONLY (op2),
4772 TREE_THIS_VOLATILE (op1) || TREE_THIS_VOLATILE (op2));
4773 /* ??? This is a kludge to deal with the fact that
4774 we don't sort out integers and enums properly, yet. */
4775 result = fold (build (COND_EXPR, type1, ifexp, op1, op2));
4776 if (TREE_TYPE (result) != type1)
4777 result = build1 (NOP_EXPR, type1, result);
4781 /* They don't match; promote them both and then try to reconcile them.
4782 But don't permit mismatching enum types. */
4783 if (code1 == ENUMERAL_TYPE)
4785 if (code2 == ENUMERAL_TYPE)
4787 cp_error ("enumeral mismatch in conditional expression: `%T' vs `%T'", type1, type2);
4788 return error_mark_node;
4790 else if (extra_warnings && ! IS_AGGR_TYPE_CODE (code2)
4791 && type2 != type_promotes_to (type1))
4792 warning ("enumeral and non-enumeral type in conditional expression");
4794 else if (extra_warnings
4795 && code2 == ENUMERAL_TYPE && ! IS_AGGR_TYPE_CODE (code1)
4796 && type1 != type_promotes_to (type2))
4797 warning ("enumeral and non-enumeral type in conditional expression");
4799 if (code1 != VOID_TYPE)
4801 op1 = default_conversion (op1);
4802 type1 = TREE_TYPE (op1);
4803 if (TYPE_PTRMEMFUNC_P (type1))
4804 type1 = TYPE_PTRMEMFUNC_FN_TYPE (type1);
4805 code1 = TREE_CODE (type1);
4807 if (code2 != VOID_TYPE)
4809 op2 = default_conversion (op2);
4810 type2 = TREE_TYPE (op2);
4811 if (TYPE_PTRMEMFUNC_P (type2))
4812 type2 = TYPE_PTRMEMFUNC_FN_TYPE (type2);
4813 code2 = TREE_CODE (type2);
4816 if (code1 == RECORD_TYPE && code2 == RECORD_TYPE
4817 && real_lvalue_p (op1) && real_lvalue_p (op2)
4818 && comptypes (type1, type2, -1))
4820 type1 = build_reference_type (type1);
4821 type2 = build_reference_type (type2);
4822 result_type = common_type (type1, type2);
4823 op1 = convert_to_reference (result_type, op1, CONV_IMPLICIT,
4824 LOOKUP_NORMAL, NULL_TREE);
4825 op2 = convert_to_reference (result_type, op2, CONV_IMPLICIT,
4826 LOOKUP_NORMAL, NULL_TREE);
4828 /* Quickly detect the usual case where op1 and op2 have the same type
4830 else if (TYPE_MAIN_VARIANT (type1) == TYPE_MAIN_VARIANT (type2))
4833 result_type = type1;
4835 result_type = cp_build_type_variant
4837 TREE_READONLY (op1) || TREE_READONLY (op2),
4838 TREE_THIS_VOLATILE (op1) || TREE_THIS_VOLATILE (op2));
4840 else if ((code1 == INTEGER_TYPE || code1 == REAL_TYPE)
4841 && (code2 == INTEGER_TYPE || code2 == REAL_TYPE))
4843 result_type = common_type (type1, type2);
4845 else if (code1 == VOID_TYPE || code2 == VOID_TYPE)
4847 if (pedantic && (code1 != VOID_TYPE || code2 != VOID_TYPE))
4848 pedwarn ("ANSI C++ forbids conditional expr with only one void side");
4849 result_type = void_type_node;
4851 else if (code1 == POINTER_TYPE && code2 == POINTER_TYPE)
4853 if (comp_target_types (type1, type2, 1))
4854 result_type = common_type (type1, type2);
4855 else if (integer_zerop (op1) && TREE_TYPE (type1) == void_type_node
4856 && TREE_CODE (orig_op1) != NOP_EXPR)
4857 result_type = qualify_type (type2, type1);
4858 else if (integer_zerop (op2) && TREE_TYPE (type2) == void_type_node
4859 && TREE_CODE (orig_op2) != NOP_EXPR)
4860 result_type = qualify_type (type1, type2);
4861 else if (TYPE_MAIN_VARIANT (TREE_TYPE (type1)) == void_type_node)
4863 if (pedantic && TREE_CODE (type2) == FUNCTION_TYPE)
4864 pedwarn ("ANSI C++ forbids conditional expr between `void *' and function pointer");
4865 result_type = qualify_type (type1, type2);
4867 else if (TYPE_MAIN_VARIANT (TREE_TYPE (type2)) == void_type_node)
4869 if (pedantic && TREE_CODE (type1) == FUNCTION_TYPE)
4870 pedwarn ("ANSI C++ forbids conditional expr between `void *' and function pointer");
4871 result_type = qualify_type (type2, type1);
4874 else if (comptypes (type2, type1, 0))
4875 result_type = type2;
4876 else if (IS_AGGR_TYPE (TREE_TYPE (type1))
4877 && IS_AGGR_TYPE (TREE_TYPE (type2))
4878 && (result_type = common_base_type (TREE_TYPE (type1), TREE_TYPE (type2))))
4880 if (result_type == error_mark_node)
4882 cp_error ("common base type of types `%T' and `%T' is ambiguous",
4883 TREE_TYPE (type1), TREE_TYPE (type2));
4884 result_type = ptr_type_node;
4889 && result_type != TREE_TYPE (type1)
4890 && result_type != TREE_TYPE (type2))
4891 cp_pedwarn ("`%T' and `%T' converted to `%T *' in conditional expression",
4892 type1, type2, result_type);
4894 result_type = build_pointer_type (result_type);
4899 pedwarn ("pointer type mismatch in conditional expression");
4900 result_type = ptr_type_node;
4903 else if (code1 == POINTER_TYPE && code2 == INTEGER_TYPE)
4905 if (!integer_zerop (op2))
4906 pedwarn ("pointer/integer type mismatch in conditional expression");
4908 op2 = null_pointer_node;
4910 result_type = type1;
4912 else if (code2 == POINTER_TYPE && code1 == INTEGER_TYPE)
4914 if (!integer_zerop (op1))
4915 pedwarn ("pointer/integer type mismatch in conditional expression");
4917 op1 = null_pointer_node;
4919 result_type = type2;
4924 /* The match does not look good. If either is
4925 an aggregate value, try converting to a scalar type. */
4926 if (code1 == RECORD_TYPE && code2 == RECORD_TYPE)
4928 cp_error ("aggregate mismatch in conditional expression: `%T' vs `%T'", type1, type2);
4929 return error_mark_node;
4931 /* Warning: this code assumes that conversion between cv-variants of
4932 a type is done using NOP_EXPRs. */
4933 if (code1 == RECORD_TYPE && TYPE_HAS_CONVERSION (type1))
4935 tree tmp = build_pointer_type
4936 (build_type_variant (TREE_TYPE (type2), 1, 1));
4937 tmp = build_type_conversion (CONVERT_EXPR, tmp, op1, 0);
4938 if (tmp == NULL_TREE)
4940 cp_error ("incompatible types `%T' and `%T' in `?:'",
4942 return error_mark_node;
4944 if (tmp == error_mark_node)
4945 error ("ambiguous pointer conversion");
4948 result_type = common_type (type1, TREE_TYPE (tmp));
4951 else if (code2 == RECORD_TYPE && TYPE_HAS_CONVERSION (type2))
4953 tree tmp = build_pointer_type
4954 (build_type_variant (TREE_TYPE (type1), 1, 1));
4955 tmp = build_type_conversion (CONVERT_EXPR, tmp, op2, 0);
4956 if (tmp == NULL_TREE)
4958 cp_error ("incompatible types `%T' and `%T' in `?:'",
4960 return error_mark_node;
4962 if (tmp == error_mark_node)
4963 error ("ambiguous pointer conversion");
4966 result_type = common_type (type1, TREE_TYPE (tmp));
4969 else if (flag_cond_mismatch)
4970 result_type = void_type_node;
4973 error ("type mismatch in conditional expression");
4974 return error_mark_node;
4978 if (TREE_CODE (result_type) == POINTER_TYPE
4979 && TREE_CODE (TREE_TYPE (result_type)) == METHOD_TYPE)
4980 result_type = build_ptrmemfunc_type (result_type);
4982 if (result_type != TREE_TYPE (op1))
4983 op1 = convert_and_check (result_type, op1);
4984 if (result_type != TREE_TYPE (op2))
4985 op2 = convert_and_check (result_type, op2);
4987 if (TREE_CONSTANT (ifexp))
4988 return integer_zerop (ifexp) ? op2 : op1;
4990 return convert_from_reference
4991 (fold (build (COND_EXPR, result_type, ifexp, op1, op2)));
4994 /* Handle overloading of the ',' operator when needed. Otherwise,
4995 this function just builds an expression list. */
4997 build_x_compound_expr (list)
5000 tree rest = TREE_CHAIN (list);
5003 if (current_template_parms)
5004 return build_min_nt (COMPOUND_EXPR, list, NULL_TREE);
5006 if (rest == NULL_TREE)
5007 return build_compound_expr (list);
5009 result = build_opfncall (COMPOUND_EXPR, LOOKUP_NORMAL,
5010 TREE_VALUE (list), TREE_VALUE (rest), NULL_TREE);
5012 return build_x_compound_expr (tree_cons (NULL_TREE, result, TREE_CHAIN (rest)));
5014 if (! TREE_SIDE_EFFECTS (TREE_VALUE (list)))
5016 /* the left-hand operand of a comma expression is like an expression
5017 statement: we should warn if it doesn't have any side-effects,
5018 unless it was explicitly cast to (void). */
5019 if ((extra_warnings || warn_unused)
5020 && !(TREE_CODE (TREE_VALUE(list)) == CONVERT_EXPR
5021 && TREE_TYPE (TREE_VALUE(list)) == void_type_node))
5022 warning("left-hand operand of comma expression has no effect");
5024 #if 0 /* this requires a gcc backend patch to export warn_if_unused_value */
5025 else if (warn_unused)
5026 warn_if_unused_value (TREE_VALUE(list));
5029 return build_compound_expr (tree_cons (NULL_TREE, TREE_VALUE (list),
5030 build_tree_list (NULL_TREE, build_x_compound_expr (rest))));
5033 /* Given a list of expressions, return a compound expression
5034 that performs them all and returns the value of the last of them. */
5037 build_compound_expr (list)
5042 if (TREE_READONLY_DECL_P (TREE_VALUE (list)))
5043 TREE_VALUE (list) = decl_constant_value (TREE_VALUE (list));
5045 if (TREE_CHAIN (list) == 0)
5047 /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
5048 Strip such NOP_EXPRs, since LIST is used in non-lvalue context. */
5049 if (TREE_CODE (list) == NOP_EXPR
5050 && TREE_TYPE (list) == TREE_TYPE (TREE_OPERAND (list, 0)))
5051 list = TREE_OPERAND (list, 0);
5053 /* Convert arrays to pointers. */
5054 if (TREE_CODE (TREE_TYPE (TREE_VALUE (list))) == ARRAY_TYPE)
5055 return default_conversion (TREE_VALUE (list));
5057 return TREE_VALUE (list);
5060 rest = build_compound_expr (TREE_CHAIN (list));
5062 /* When pedantic, a compound expression cannot be a constant expression. */
5063 if (! TREE_SIDE_EFFECTS (TREE_VALUE (list)) && ! pedantic)
5066 return build (COMPOUND_EXPR, TREE_TYPE (rest),
5067 break_out_cleanups (TREE_VALUE (list)), rest);
5071 build_static_cast (type, expr)
5074 return build_c_cast (type, expr, 0);
5078 build_reinterpret_cast (type, expr)
5081 tree intype = TREE_TYPE (expr);
5083 if (TYPE_PTRMEMFUNC_P (type))
5084 type = TYPE_PTRMEMFUNC_FN_TYPE (type);
5086 if (current_template_parms)
5088 tree t = build_min (REINTERPRET_CAST_EXPR, type, expr);
5092 if (TYPE_PTRMEMFUNC_P (intype))
5093 intype = TYPE_PTRMEMFUNC_FN_TYPE (intype);
5095 if (! POINTER_TYPE_P (type) && ! TREE_CODE (type) == INTEGER_TYPE)
5097 cp_error ("reinterpret_cast cannot convert to type `%T'", type);
5098 return error_mark_node;
5100 if (! POINTER_TYPE_P (intype) && ! TREE_CODE (intype) == INTEGER_TYPE)
5102 cp_error ("reinterpret_cast cannot convert from type `%T'", type);
5103 return error_mark_node;
5105 if (TREE_CODE (type) == INTEGER_TYPE && TREE_CODE (intype) != POINTER_TYPE)
5107 cp_error ("reinterpret_cast cannot convert non-pointer type `%T' to `%T'",
5109 return error_mark_node;
5111 if (TREE_CODE (intype) == INTEGER_TYPE && TREE_CODE (type) != POINTER_TYPE)
5113 cp_error ("reinterpret_cast cannot convert `%T' to non-pointer type `%T'",
5115 return error_mark_node;
5118 if (TREE_CODE (type) == POINTER_TYPE && TREE_CODE (intype) == POINTER_TYPE)
5119 expr = convert (ptr_type_node, expr);
5121 return build_c_cast (type, expr, 0);
5125 build_const_cast (type, expr)
5128 tree intype = TREE_TYPE (expr);
5131 if (type == error_mark_node || expr == error_mark_node)
5132 return error_mark_node;
5134 if (TYPE_PTRMEMFUNC_P (type))
5135 type = TYPE_PTRMEMFUNC_FN_TYPE (type);
5136 if (TYPE_PTRMEMFUNC_P (intype))
5137 intype = TYPE_PTRMEMFUNC_FN_TYPE (intype);
5139 if (! POINTER_TYPE_P (type))
5141 cp_error ("const_cast cannot convert to non-pointer type `%T'", type);
5142 return error_mark_node;
5144 if (TREE_CODE (type) == REFERENCE_TYPE && ! real_lvalue_p (expr))
5146 cp_error ("const_cast cannot convert rvalue to type `%T'", type);
5147 return error_mark_node;
5149 if (TREE_CODE (type) == POINTER_TYPE && TREE_CODE (intype) != POINTER_TYPE)
5151 cp_error ("const_cast cannot convert non-pointer type `%T' to type `%T'",
5153 return error_mark_node;
5156 if (TREE_CODE (type) == REFERENCE_TYPE)
5158 t1 = TREE_TYPE (type);
5163 t1 = TREE_TYPE (type);
5164 t2 = TREE_TYPE (intype);
5166 for (; TREE_CODE (t1) == POINTER_TYPE && TREE_CODE (t2) == POINTER_TYPE;
5167 t1 = TREE_TYPE (t1), t2 = TREE_TYPE (t2))
5171 if (TREE_CODE (t1) == OFFSET_TYPE && TREE_CODE (t2) == OFFSET_TYPE)
5173 if (TYPE_OFFSET_BASETYPE (t1) != TYPE_OFFSET_BASETYPE (t2))
5175 cp_error ("const_cast cannot convert between pointers to members of different types `%T' and `%T'",
5176 TYPE_OFFSET_BASETYPE (t2), TYPE_OFFSET_BASETYPE (t1));
5177 return error_mark_node;
5179 t1 = TREE_TYPE (t1);
5180 t2 = TREE_TYPE (t2);
5183 if (TYPE_MAIN_VARIANT (t1) != TYPE_MAIN_VARIANT (t2))
5185 cp_error ("const_cast cannot convert unrelated type `%T' to `%T'",
5187 return error_mark_node;
5190 return build_c_cast (type, expr, 0);
5193 /* Build an expression representing a cast to type TYPE of expression EXPR.
5195 ALLOW_NONCONVERTING is true if we should allow non-converting constructors
5196 when doing the cast. */
5199 build_c_cast (type, expr, allow_nonconverting)
5202 int allow_nonconverting;
5204 register tree value = expr;
5206 if (type == error_mark_node || expr == error_mark_node)
5207 return error_mark_node;
5209 /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
5210 Strip such NOP_EXPRs if VALUE is being used in non-lvalue context. */
5211 if (TREE_CODE (type) != REFERENCE_TYPE
5212 && TREE_CODE (value) == NOP_EXPR
5213 && TREE_TYPE (value) == TREE_TYPE (TREE_OPERAND (value, 0)))
5214 value = TREE_OPERAND (value, 0);
5216 if (TREE_TYPE (expr)
5217 && TREE_CODE (TREE_TYPE (expr)) == OFFSET_TYPE
5218 && TREE_CODE (type) != OFFSET_TYPE)
5219 value = resolve_offset_ref (value);
5221 if (TREE_CODE (type) == ARRAY_TYPE)
5223 /* Allow casting from T1* to T2[] because Cfront allows it.
5224 NIHCL uses it. It is not valid ANSI C however, and hence, not
5226 if (TREE_CODE (TREE_TYPE (expr)) == POINTER_TYPE)
5229 pedwarn ("ANSI C++ forbids casting to an array type");
5230 type = build_pointer_type (TREE_TYPE (type));
5234 error ("ANSI C++ forbids casting to an array type");
5235 return error_mark_node;
5239 if (TREE_CODE (type) == FUNCTION_TYPE
5240 || TREE_CODE (type) == METHOD_TYPE)
5242 cp_error ("casting to function type `%T'", type);
5243 return error_mark_node;
5246 if (IS_SIGNATURE (type))
5248 error ("cast specifies signature type");
5249 return error_mark_node;
5252 if (current_template_parms)
5254 tree t = build_min (CAST_EXPR, type,
5255 min_tree_cons (NULL_TREE, value, NULL_TREE));
5259 if (TREE_CODE (type) == VOID_TYPE)
5260 value = build1 (CONVERT_EXPR, type, value);
5261 else if (TREE_TYPE (value) == NULL_TREE
5262 || type_unknown_p (value))
5264 value = instantiate_type (type, value, 1);
5266 if (value == error_mark_node)
5267 return error_mark_node;
5274 /* Convert functions and arrays to pointers and
5275 convert references to their expanded types,
5276 but don't convert any other types. */
5277 if (TREE_CODE (TREE_TYPE (value)) == FUNCTION_TYPE
5278 || TREE_CODE (TREE_TYPE (value)) == METHOD_TYPE
5279 || TREE_CODE (TREE_TYPE (value)) == ARRAY_TYPE
5280 || TREE_CODE (TREE_TYPE (value)) == REFERENCE_TYPE)
5281 value = default_conversion (value);
5282 otype = TREE_TYPE (value);
5284 /* Optionally warn about potentially worrisome casts. */
5287 && TREE_CODE (type) == POINTER_TYPE
5288 && TREE_CODE (otype) == POINTER_TYPE)
5290 /* For C++ we make these regular warnings, rather than
5291 softening them into pedwarns. */
5292 if (TYPE_VOLATILE (TREE_TYPE (otype))
5293 && ! TYPE_VOLATILE (TREE_TYPE (type)))
5294 warning ("cast discards `volatile' from pointer target type");
5295 if (TYPE_READONLY (TREE_TYPE (otype))
5296 && ! TYPE_READONLY (TREE_TYPE (type)))
5297 warning ("cast discards `const' from pointer target type");
5300 /* Warn about possible alignment problems. */
5301 if (STRICT_ALIGNMENT && warn_cast_align
5302 && TREE_CODE (type) == POINTER_TYPE
5303 && TREE_CODE (otype) == POINTER_TYPE
5304 && TREE_CODE (TREE_TYPE (otype)) != VOID_TYPE
5305 && TREE_CODE (TREE_TYPE (otype)) != FUNCTION_TYPE
5306 && TYPE_ALIGN (TREE_TYPE (type)) > TYPE_ALIGN (TREE_TYPE (otype)))
5307 warning ("cast increases required alignment of target type");
5310 /* We should see about re-enabling these, they seem useful to
5312 if (TREE_CODE (type) == INTEGER_TYPE
5313 && TREE_CODE (otype) == POINTER_TYPE
5314 && TYPE_PRECISION (type) != TYPE_PRECISION (otype))
5315 warning ("cast from pointer to integer of different size");
5317 if (TREE_CODE (type) == POINTER_TYPE
5318 && TREE_CODE (otype) == INTEGER_TYPE
5319 && TYPE_PRECISION (type) != TYPE_PRECISION (otype)
5320 /* Don't warn about converting 0 to pointer,
5321 provided the 0 was explicit--not cast or made by folding. */
5322 && !(TREE_CODE (value) == INTEGER_CST && integer_zerop (value)))
5323 warning ("cast to pointer from integer of different size");
5326 flag = allow_nonconverting ? CONV_NONCONVERTING : 0;
5328 if (TREE_CODE (type) == REFERENCE_TYPE)
5329 value = (convert_from_reference
5330 (convert_to_reference (type, value, CONV_OLD_CONVERT|flag,
5331 LOOKUP_COMPLAIN, NULL_TREE)));
5336 if (TREE_READONLY_DECL_P (value))
5337 value = decl_constant_value (value);
5340 value = convert_force (type, value, flag);
5342 /* Ignore any integer overflow caused by the cast. */
5343 if (TREE_CODE (value) == INTEGER_CST)
5345 TREE_OVERFLOW (value) = TREE_OVERFLOW (ovalue);
5346 TREE_CONSTANT_OVERFLOW (value) = TREE_CONSTANT_OVERFLOW (ovalue);
5351 /* Always produce some operator for an explicit cast,
5352 so we can tell (for -pedantic) that the cast is no lvalue.
5353 Also, pedantically, don't let (void *) (FOO *) 0 be a null
5354 pointer constant. */
5355 if (TREE_CODE (type) != REFERENCE_TYPE
5358 && TREE_CODE (value) == INTEGER_CST
5359 && TREE_CODE (expr) == INTEGER_CST
5360 && TREE_CODE (TREE_TYPE (expr)) != INTEGER_TYPE)))
5361 value = non_lvalue (value);
5367 expand_target_expr (t)
5370 extern int temp_slot_level;
5371 extern int target_temp_slot_level;
5372 int old_temp_level = target_temp_slot_level;
5374 tree xval = make_node (RTL_EXPR);
5377 /* Any TARGET_EXPR temps live only as long as the outer temp level.
5378 Since they are preserved in this new inner level, we know they
5379 will make it into the outer level. */
5381 target_temp_slot_level = temp_slot_level;
5383 do_pending_stack_adjust ();
5384 start_sequence_for_rtl_expr (xval);
5386 rtxval = expand_expr (t, NULL, VOIDmode, 0);
5387 do_pending_stack_adjust ();
5388 TREE_SIDE_EFFECTS (xval) = 1;
5389 RTL_EXPR_SEQUENCE (xval) = get_insns ();
5391 RTL_EXPR_RTL (xval) = rtxval;
5392 TREE_TYPE (xval) = TREE_TYPE (t);
5395 target_temp_slot_level = old_temp_level;
5400 /* Build an assignment expression of lvalue LHS from value RHS.
5401 MODIFYCODE is the code for a binary operator that we use
5402 to combine the old value of LHS with RHS to get the new value.
5403 Or else MODIFYCODE is NOP_EXPR meaning do a simple assignment.
5405 C++: If MODIFYCODE is INIT_EXPR, then leave references unbashed.
5408 build_modify_expr (lhs, modifycode, rhs)
5410 enum tree_code modifycode;
5413 register tree result;
5415 tree lhstype = TREE_TYPE (lhs);
5416 tree olhstype = lhstype;
5419 /* Avoid duplicate error messages from operands that had errors. */
5420 if (TREE_CODE (lhs) == ERROR_MARK || TREE_CODE (rhs) == ERROR_MARK)
5421 return error_mark_node;
5423 /* Types that aren't fully specified cannot be used in assignments. */
5424 lhs = require_complete_type (lhs);
5428 /* Handle assignment to signature pointers/refs. */
5430 if (TYPE_LANG_SPECIFIC (lhstype) &&
5431 (IS_SIGNATURE_POINTER (lhstype) || IS_SIGNATURE_REFERENCE (lhstype)))
5433 return build_signature_pointer_constructor (lhs, rhs);
5436 /* Handle control structure constructs used as "lvalues". */
5438 switch (TREE_CODE (lhs))
5440 /* Handle --foo = 5; as these are valid constructs in C++ */
5441 case PREDECREMENT_EXPR:
5442 case PREINCREMENT_EXPR:
5443 if (TREE_SIDE_EFFECTS (TREE_OPERAND (lhs, 0)))
5444 lhs = build (TREE_CODE (lhs), TREE_TYPE (lhs),
5445 stabilize_reference (TREE_OPERAND (lhs, 0)));
5446 return build (COMPOUND_EXPR, lhstype,
5448 build_modify_expr (TREE_OPERAND (lhs, 0),
5451 /* Handle (a, b) used as an "lvalue". */
5453 newrhs = build_modify_expr (TREE_OPERAND (lhs, 1),
5455 if (TREE_CODE (newrhs) == ERROR_MARK)
5456 return error_mark_node;
5457 return build (COMPOUND_EXPR, lhstype,
5458 TREE_OPERAND (lhs, 0), newrhs);
5461 newrhs = build_modify_expr (TREE_OPERAND (lhs, 0), modifycode, rhs);
5462 if (TREE_CODE (newrhs) == ERROR_MARK)
5463 return error_mark_node;
5464 return build (COMPOUND_EXPR, lhstype, lhs, newrhs);
5466 /* Handle (a ? b : c) used as an "lvalue". */
5468 rhs = save_expr (rhs);
5470 /* Produce (a ? (b = rhs) : (c = rhs))
5471 except that the RHS goes through a save-expr
5472 so the code to compute it is only emitted once. */
5474 = build_conditional_expr (TREE_OPERAND (lhs, 0),
5475 build_modify_expr (convert (TREE_TYPE (lhs), TREE_OPERAND (lhs, 1)),
5477 build_modify_expr (convert (TREE_TYPE (lhs), TREE_OPERAND (lhs, 2)),
5479 if (TREE_CODE (cond) == ERROR_MARK)
5481 /* Make sure the code to compute the rhs comes out
5482 before the split. */
5483 return build (COMPOUND_EXPR, TREE_TYPE (lhs),
5484 /* Case to void to suppress warning
5485 from warn_if_unused_value. */
5486 convert (void_type_node, rhs), cond);
5490 if (TREE_CODE (lhs) == OFFSET_REF)
5492 if (TREE_OPERAND (lhs, 0) == NULL_TREE)
5494 /* Static class member? */
5495 tree member = TREE_OPERAND (lhs, 1);
5496 if (TREE_CODE (member) == VAR_DECL)
5500 compiler_error ("invalid static class member");
5501 return error_mark_node;
5505 lhs = resolve_offset_ref (lhs);
5507 olhstype = lhstype = TREE_TYPE (lhs);
5510 if (TREE_CODE (lhstype) == REFERENCE_TYPE
5511 && modifycode != INIT_EXPR)
5513 lhs = convert_from_reference (lhs);
5514 olhstype = lhstype = TREE_TYPE (lhs);
5517 /* If a binary op has been requested, combine the old LHS value with the RHS
5518 producing the value we should actually store into the LHS. */
5520 if (modifycode == INIT_EXPR)
5522 if (! IS_AGGR_TYPE (lhstype))
5523 /* Do the default thing */;
5524 else if (! TYPE_HAS_CONSTRUCTOR (lhstype))
5526 cp_error ("`%T' has no constructors", lhstype);
5527 return error_mark_node;
5529 else if (TYPE_HAS_TRIVIAL_INIT_REF (lhstype)
5530 && TYPE_MAIN_VARIANT (lhstype) == TYPE_MAIN_VARIANT (TREE_TYPE (newrhs)))
5531 /* Do the default thing */;
5534 result = build_method_call (lhs, ctor_identifier,
5535 build_tree_list (NULL_TREE, rhs),
5536 TYPE_BINFO (lhstype), LOOKUP_NORMAL);
5537 if (result == NULL_TREE)
5538 return error_mark_node;
5542 else if (modifycode == NOP_EXPR)
5544 /* `operator=' is not an inheritable operator. */
5545 if (! IS_AGGR_TYPE (lhstype))
5546 /* Do the default thing */;
5547 else if (! TYPE_HAS_ASSIGNMENT (lhstype))
5549 cp_error ("`%T' does not define operator=", lhstype);
5550 return error_mark_node;
5552 else if (TYPE_HAS_TRIVIAL_ASSIGN_REF (lhstype)
5553 && TYPE_MAIN_VARIANT (lhstype) == TYPE_MAIN_VARIANT (TREE_TYPE (newrhs)))
5556 /* If we care about this, do overload resolution. */
5557 build_opfncall (MODIFY_EXPR, LOOKUP_NORMAL,
5558 lhs, rhs, make_node (NOP_EXPR));
5560 /* Do the default thing */;
5564 result = build_opfncall (MODIFY_EXPR, LOOKUP_NORMAL,
5565 lhs, rhs, make_node (NOP_EXPR));
5566 if (result == NULL_TREE)
5567 return error_mark_node;
5572 else if (PROMOTES_TO_AGGR_TYPE (lhstype, REFERENCE_TYPE))
5574 /* This case must convert to some sort of lvalue that
5575 can participate in an op= operation. */
5578 if (build_default_binary_type_conversion (modifycode, &lhs_tmp, &rhs_tmp))
5580 lhs = stabilize_reference (lhs_tmp);
5581 /* Forget it was ever anything else. */
5582 olhstype = lhstype = TREE_TYPE (lhs);
5583 newrhs = build_binary_op (modifycode, lhs, rhs_tmp, 1);
5587 cp_error ("no match for `%Q(%#T, %#T)'", modifycode,
5588 TREE_TYPE (lhs), TREE_TYPE (rhs));
5589 return error_mark_node;
5594 lhs = stabilize_reference (lhs);
5595 newrhs = build_binary_op (modifycode, lhs, rhs, 1);
5596 if (newrhs == error_mark_node)
5598 cp_error (" in evaluation of `%Q(%#T, %#T)'", modifycode,
5599 TREE_TYPE (lhs), TREE_TYPE (rhs));
5600 return error_mark_node;
5604 /* Handle a cast used as an "lvalue".
5605 We have already performed any binary operator using the value as cast.
5606 Now convert the result to the cast type of the lhs,
5607 and then true type of the lhs and store it there;
5608 then convert result back to the cast type to be the value
5609 of the assignment. */
5611 switch (TREE_CODE (lhs))
5616 case FIX_TRUNC_EXPR:
5617 case FIX_FLOOR_EXPR:
5618 case FIX_ROUND_EXPR:
5620 if (TREE_CODE (TREE_TYPE (newrhs)) == ARRAY_TYPE
5621 || TREE_CODE (TREE_TYPE (newrhs)) == FUNCTION_TYPE
5622 || TREE_CODE (TREE_TYPE (newrhs)) == METHOD_TYPE
5623 || TREE_CODE (TREE_TYPE (newrhs)) == OFFSET_TYPE)
5624 newrhs = default_conversion (newrhs);
5626 tree inner_lhs = TREE_OPERAND (lhs, 0);
5628 if (! lvalue_p (lhs) && pedantic)
5629 pedwarn ("cast to non-reference type used as lvalue");
5631 result = build_modify_expr (inner_lhs, NOP_EXPR,
5632 convert (TREE_TYPE (inner_lhs),
5633 convert (lhstype, newrhs)));
5634 if (TREE_CODE (result) == ERROR_MARK)
5636 return convert (TREE_TYPE (lhs), result);
5640 /* Now we have handled acceptable kinds of LHS that are not truly lvalues.
5641 Reject anything strange now. */
5643 if (!lvalue_or_else (lhs, "assignment"))
5644 return error_mark_node;
5646 GNU_xref_assign (lhs);
5648 /* Warn about storing in something that is `const'. */
5649 /* For C++, don't warn if this is initialization. */
5650 if (modifycode != INIT_EXPR
5651 /* For assignment to `const' signature pointer/reference fields,
5652 don't warn either, we already printed a better message before. */
5653 && ! (TREE_CODE (lhs) == COMPONENT_REF
5654 && (IS_SIGNATURE_POINTER (TREE_TYPE (TREE_OPERAND (lhs, 0)))
5655 || IS_SIGNATURE_REFERENCE (TREE_TYPE (TREE_OPERAND (lhs, 0)))))
5656 && (TREE_READONLY (lhs) || TYPE_READONLY (lhstype)
5657 || ((TREE_CODE (lhstype) == RECORD_TYPE
5658 || TREE_CODE (lhstype) == UNION_TYPE)
5659 && C_TYPE_FIELDS_READONLY (lhstype))
5660 || (TREE_CODE (lhstype) == REFERENCE_TYPE
5661 && TYPE_READONLY (TREE_TYPE (lhstype)))))
5662 readonly_error (lhs, "assignment", 0);
5664 /* If storing into a structure or union member,
5665 it has probably been given type `int'.
5666 Compute the type that would go with
5667 the actual amount of storage the member occupies. */
5669 if (TREE_CODE (lhs) == COMPONENT_REF
5670 && (TREE_CODE (lhstype) == INTEGER_TYPE
5671 || TREE_CODE (lhstype) == REAL_TYPE
5672 || TREE_CODE (lhstype) == ENUMERAL_TYPE))
5674 lhstype = TREE_TYPE (get_unwidened (lhs, 0));
5676 /* If storing in a field that is in actuality a short or narrower
5677 than one, we must store in the field in its actual type. */
5679 if (lhstype != TREE_TYPE (lhs))
5681 lhs = copy_node (lhs);
5682 TREE_TYPE (lhs) = lhstype;
5686 /* check to see if there is an assignment to `this' */
5687 if (lhs == current_class_decl)
5689 if (flag_this_is_variable > 0
5690 && DECL_NAME (current_function_decl) != NULL_TREE
5691 && (DECL_NAME (current_function_decl)
5692 != constructor_name (current_class_type)))
5693 warning ("assignment to `this' not in constructor or destructor");
5694 current_function_just_assigned_this = 1;
5697 /* The TREE_TYPE of RHS may be TYPE_UNKNOWN. This can happen
5698 when the type of RHS is not yet known, i.e. its type
5699 is inherited from LHS. */
5700 rhs = require_instantiated_type (lhstype, newrhs, error_mark_node);
5701 if (rhs == error_mark_node)
5702 return error_mark_node;
5705 if (modifycode != INIT_EXPR)
5707 /* Make modifycode now either a NOP_EXPR or an INIT_EXPR. */
5708 modifycode = NOP_EXPR;
5709 /* Reference-bashing */
5710 if (TREE_CODE (lhstype) == REFERENCE_TYPE)
5712 tree tmp = convert_from_reference (lhs);
5713 lhstype = TREE_TYPE (tmp);
5714 if (TYPE_SIZE (lhstype) == 0)
5716 incomplete_type_error (lhs, lhstype);
5717 return error_mark_node;
5722 if (TREE_CODE (TREE_TYPE (newrhs)) == REFERENCE_TYPE)
5724 tree tmp = convert_from_reference (newrhs);
5725 if (TYPE_SIZE (TREE_TYPE (tmp)) == 0)
5727 incomplete_type_error (newrhs, TREE_TYPE (tmp));
5728 return error_mark_node;
5734 if (TREE_SIDE_EFFECTS (lhs))
5735 lhs = stabilize_reference (lhs);
5736 if (TREE_SIDE_EFFECTS (newrhs))
5737 newrhs = stabilize_reference (newrhs);
5739 /* Convert new value to destination type. */
5741 if (TREE_CODE (lhstype) == ARRAY_TYPE)
5745 if (! comptypes (lhstype, TREE_TYPE (rhs), 0))
5747 cp_error ("incompatible types in assignment of `%T' to `%T'",
5748 TREE_TYPE (rhs), lhstype);
5749 return error_mark_node;
5752 /* Allow array assignment in compiler-generated code. */
5753 if (pedantic && ! DECL_ARTIFICIAL (current_function_decl))
5754 pedwarn ("ANSI C++ forbids assignment of arrays");
5756 /* Have to wrap this in RTL_EXPR for two cases:
5757 in base or member initialization and if we
5758 are a branch of a ?: operator. Since we
5759 can't easily know the latter, just do it always. */
5761 result = make_node (RTL_EXPR);
5763 TREE_TYPE (result) = void_type_node;
5764 do_pending_stack_adjust ();
5765 start_sequence_for_rtl_expr (result);
5767 /* As a matter of principle, `start_sequence' should do this. */
5770 from_array = TREE_CODE (TREE_TYPE (newrhs)) == ARRAY_TYPE
5771 ? 1 + (modifycode != INIT_EXPR): 0;
5772 expand_vec_init (lhs, lhs, array_type_nelts (lhstype), newrhs,
5775 do_pending_stack_adjust ();
5777 TREE_SIDE_EFFECTS (result) = 1;
5778 RTL_EXPR_SEQUENCE (result) = get_insns ();
5779 RTL_EXPR_RTL (result) = const0_rtx;
5784 if (modifycode == INIT_EXPR)
5786 newrhs = convert_for_initialization (lhs, lhstype, newrhs, LOOKUP_NORMAL,
5787 "assignment", NULL_TREE, 0);
5788 if (lhs == DECL_RESULT (current_function_decl))
5790 if (DECL_INITIAL (lhs))
5791 warning ("return value from function receives multiple initializations");
5792 DECL_INITIAL (lhs) = newrhs;
5797 /* Avoid warnings on enum bit fields. */
5798 if (TREE_CODE (olhstype) == ENUMERAL_TYPE
5799 && TREE_CODE (lhstype) == INTEGER_TYPE)
5801 newrhs = convert_for_assignment (olhstype, newrhs, "assignment",
5803 newrhs = convert_force (lhstype, newrhs, 0);
5806 newrhs = convert_for_assignment (lhstype, newrhs, "assignment",
5808 if (TREE_CODE (newrhs) == CALL_EXPR
5809 && TYPE_NEEDS_CONSTRUCTING (lhstype))
5810 newrhs = build_cplus_new (lhstype, newrhs);
5812 /* Can't initialize directly from a TARGET_EXPR, since that would
5813 cause the lhs to be constructed twice, and possibly result in
5814 accidental self-initialization. So we force the TARGET_EXPR to be
5815 expanded without a target. */
5816 if (TREE_CODE (newrhs) == TARGET_EXPR)
5817 newrhs = build (COMPOUND_EXPR, TREE_TYPE (newrhs), newrhs,
5818 TREE_VALUE (newrhs));
5821 if (TREE_CODE (newrhs) == ERROR_MARK)
5822 return error_mark_node;
5824 if (TREE_CODE (newrhs) == COND_EXPR)
5827 tree cond = TREE_OPERAND (newrhs, 0);
5829 if (TREE_SIDE_EFFECTS (lhs))
5830 cond = build_compound_expr (tree_cons
5832 build_tree_list (NULL_TREE, cond)));
5834 /* Cannot have two identical lhs on this one tree (result) as preexpand
5835 calls will rip them out and fill in RTL for them, but when the
5836 rtl is generated, the calls will only be in the first side of the
5837 condition, not on both, or before the conditional jump! (mrs) */
5838 lhs1 = break_out_calls (lhs);
5841 /* If there's no change, the COND_EXPR behaves like any other rhs. */
5842 result = build (modifycode == NOP_EXPR ? MODIFY_EXPR : INIT_EXPR,
5843 lhstype, lhs, newrhs);
5846 tree result_type = TREE_TYPE (newrhs);
5847 /* We have to convert each arm to the proper type because the
5848 types may have been munged by constant folding. */
5850 = build (COND_EXPR, result_type, cond,
5851 build_modify_expr (lhs, modifycode,
5852 convert (result_type,
5853 TREE_OPERAND (newrhs, 1))),
5854 build_modify_expr (lhs1, modifycode,
5855 convert (result_type,
5856 TREE_OPERAND (newrhs, 2))));
5860 result = build (modifycode == NOP_EXPR ? MODIFY_EXPR : INIT_EXPR,
5861 lhstype, lhs, newrhs);
5863 TREE_SIDE_EFFECTS (result) = 1;
5865 /* If we got the LHS in a different type for storing in,
5866 convert the result back to the nominal type of LHS
5867 so that the value we return always has the same type
5868 as the LHS argument. */
5870 if (olhstype == TREE_TYPE (result))
5872 /* Avoid warnings converting integral types back into enums
5873 for enum bit fields. */
5874 if (TREE_CODE (TREE_TYPE (result)) == INTEGER_TYPE
5875 && TREE_CODE (olhstype) == ENUMERAL_TYPE)
5877 result = build (COMPOUND_EXPR, olhstype, result, olhs);
5878 TREE_NO_UNUSED_WARNING (result) = 1;
5881 return convert_for_assignment (olhstype, result, "assignment",
5886 build_x_modify_expr (lhs, modifycode, rhs)
5888 enum tree_code modifycode;
5891 if (current_template_parms)
5892 return build_min_nt (MODOP_EXPR, lhs,
5893 build_min_nt (modifycode, 0, 0), rhs);
5895 if (modifycode != NOP_EXPR)
5897 tree rval = build_opfncall (MODIFY_EXPR, LOOKUP_NORMAL, lhs, rhs,
5898 make_node (modifycode));
5902 return build_modify_expr (lhs, modifycode, rhs);
5905 /* Return 0 if EXP is not a valid lvalue in this language
5906 even though `lvalue_or_else' would accept it. */
5909 language_lvalue_valid (exp)
5915 /* Get difference in deltas for different pointer to member function
5916 types. Return integer_zero_node, if FROM cannot be converted to a
5917 TO type. If FORCE is true, then allow reverse conversions as well. */
5919 get_delta_difference (from, to, force)
5923 tree delta = integer_zero_node;
5929 /* Should get_base_distance here, so we can check if any thing along the
5930 path is virtual, and we need to make sure we stay
5931 inside the real binfos when going through virtual bases.
5932 Maybe we should replace virtual bases with
5933 binfo_member (...CLASSTYPE_VBASECLASSES...)... (mrs) */
5934 binfo = get_binfo (from, to, 1);
5935 if (binfo == error_mark_node)
5937 error (" in pointer to member function conversion");
5944 error_not_base_type (from, to);
5945 error (" in pointer to member function conversion");
5948 binfo = get_binfo (to, from, 1);
5949 if (binfo == error_mark_node)
5951 error (" in pointer to member function conversion");
5956 cp_error ("cannot convert pointer to member of type %T to unrelated pointer to member of type %T", from, to);
5959 if (TREE_VIA_VIRTUAL (binfo))
5961 warning ("pointer to member conversion to virtual base class will only work if you are very careful");
5963 return build_binary_op (MINUS_EXPR,
5965 BINFO_OFFSET (binfo), 1);
5967 if (TREE_VIA_VIRTUAL (binfo))
5969 warning ("pointer to member conversion from virtual base class will only work if you are very careful");
5971 return BINFO_OFFSET (binfo);
5974 /* Build a constructor for a pointer to member function. It can be
5975 used to initialize global variables, local variable, or used
5976 as a value in expressions. TYPE is the POINTER to METHOD_TYPE we
5979 If FORCE is non-zero, then force this conversion, even if
5980 we would rather not do it. Usually set when using an explicit
5983 Return error_mark_node, if something goes wrong. */
5986 build_ptrmemfunc (type, pfn, force)
5990 tree index = integer_zero_node;
5991 tree delta = integer_zero_node;
5992 tree delta2 = integer_zero_node;
5997 /* Handle multiple conversions of pointer to member functions. */
5998 if (TYPE_PTRMEMFUNC_P (TREE_TYPE (pfn)))
6000 tree ndelta, ndelta2, nindex;
6001 /* Is is already the right type? */
6002 if (type == TYPE_PTRMEMFUNC_FN_TYPE (TREE_TYPE (pfn)))
6005 if (TREE_CODE (pfn) != CONSTRUCTOR)
6008 ndelta = convert (ptrdiff_type_node, build_component_ref (pfn, delta_identifier, 0, 0));
6009 ndelta2 = convert (ptrdiff_type_node, DELTA2_FROM_PTRMEMFUNC (pfn));
6010 index = build_component_ref (pfn, index_identifier, 0, 0);
6011 delta = get_delta_difference (TYPE_METHOD_BASETYPE (TREE_TYPE (TYPE_PTRMEMFUNC_FN_TYPE (TREE_TYPE (pfn)))),
6012 TYPE_METHOD_BASETYPE (TREE_TYPE (type)),
6014 delta = build_binary_op (PLUS_EXPR, delta, ndelta, 1);
6015 delta2 = build_binary_op (PLUS_EXPR, ndelta2, delta2, 1);
6016 e1 = fold (build (GT_EXPR, boolean_type_node, index, integer_zero_node));
6018 u = build_nt (CONSTRUCTOR, 0, tree_cons (delta2_identifier, delta2, NULL_TREE));
6019 u = build_nt (CONSTRUCTOR, 0, tree_cons (NULL_TREE, delta,
6020 tree_cons (NULL_TREE, index,
6021 tree_cons (NULL_TREE, u, NULL_TREE))));
6022 e2 = digest_init (TYPE_GET_PTRMEMFUNC_TYPE (type), u, (tree*)0);
6024 pfn = PFN_FROM_PTRMEMFUNC (pfn);
6025 npfn = build1 (NOP_EXPR, type, pfn);
6026 TREE_CONSTANT (npfn) = TREE_CONSTANT (pfn);
6028 u = build_nt (CONSTRUCTOR, 0, tree_cons (pfn_identifier, npfn, NULL_TREE));
6029 u = build_nt (CONSTRUCTOR, 0, tree_cons (NULL_TREE, delta,
6030 tree_cons (NULL_TREE, index,
6031 tree_cons (NULL_TREE, u, NULL_TREE))));
6032 e3 = digest_init (TYPE_GET_PTRMEMFUNC_TYPE (type), u, (tree*)0);
6033 return build_conditional_expr (e1, e2, e3);
6036 ndelta = TREE_VALUE (CONSTRUCTOR_ELTS (pfn));
6037 nindex = TREE_VALUE (TREE_CHAIN (CONSTRUCTOR_ELTS (pfn)));
6038 npfn = TREE_VALUE (TREE_CHAIN (TREE_CHAIN (CONSTRUCTOR_ELTS (pfn))));
6039 npfn = TREE_VALUE (CONSTRUCTOR_ELTS (npfn));
6040 if (integer_zerop (nindex))
6041 pfn = integer_zero_node;
6042 else if (integer_zerop (fold (size_binop (PLUS_EXPR, nindex, integer_one_node))))
6045 delta = get_delta_difference (TYPE_METHOD_BASETYPE (TREE_TYPE (TYPE_PTRMEMFUNC_FN_TYPE (TREE_TYPE (pfn)))),
6046 TYPE_METHOD_BASETYPE (TREE_TYPE (type)),
6048 delta = build_binary_op (PLUS_EXPR, delta, ndelta, 1);
6049 pfn = build1 (NOP_EXPR, type, npfn);
6050 TREE_CONSTANT (pfn) = TREE_CONSTANT (npfn);
6052 u = build_nt (CONSTRUCTOR, 0, tree_cons (pfn_identifier, pfn, NULL_TREE));
6053 u = build_nt (CONSTRUCTOR, 0, tree_cons (NULL_TREE, delta,
6054 tree_cons (NULL_TREE, nindex,
6055 tree_cons (NULL_TREE, u, NULL_TREE))));
6056 e3 = digest_init (TYPE_GET_PTRMEMFUNC_TYPE (type), u, (tree*)0);
6061 sorry ("value casting of variable nonnull pointer to member functions not supported");
6062 return error_mark_node;
6066 /* Handle null pointer to member function conversions. */
6067 if (integer_zerop (pfn))
6069 pfn = build_c_cast (type, integer_zero_node, 0);
6070 u = build_nt (CONSTRUCTOR, 0, tree_cons (pfn_identifier, pfn, NULL_TREE));
6071 u = build_nt (CONSTRUCTOR, 0, tree_cons (NULL_TREE, integer_zero_node,
6072 tree_cons (NULL_TREE, integer_zero_node,
6073 tree_cons (NULL_TREE, u, NULL_TREE))));
6074 return digest_init (TYPE_GET_PTRMEMFUNC_TYPE (type), u, (tree*)0);
6077 if (TREE_CODE (pfn) == TREE_LIST
6078 || (TREE_CODE (pfn) == ADDR_EXPR
6079 && TREE_CODE (TREE_OPERAND (pfn, 0)) == TREE_LIST))
6081 pfn = instantiate_type (type, pfn, 1);
6082 if (pfn == error_mark_node)
6083 return error_mark_node;
6084 if (TREE_CODE (pfn) != ADDR_EXPR)
6085 pfn = build_unary_op (ADDR_EXPR, pfn, 0);
6088 /* Allow pointer to member conversions here. */
6089 delta = get_delta_difference (TYPE_METHOD_BASETYPE (TREE_TYPE (TREE_TYPE (pfn))),
6090 TYPE_METHOD_BASETYPE (TREE_TYPE (type)),
6092 delta2 = build_binary_op (PLUS_EXPR, delta2, delta, 1);
6094 if (TREE_CODE (TREE_OPERAND (pfn, 0)) != FUNCTION_DECL)
6095 warning ("assuming pointer to member function is non-virtual");
6097 if (TREE_CODE (TREE_OPERAND (pfn, 0)) == FUNCTION_DECL
6098 && DECL_VINDEX (TREE_OPERAND (pfn, 0)))
6100 /* Find the offset to the vfield pointer in the object. */
6101 vfield_offset = get_binfo (DECL_CONTEXT (TREE_OPERAND (pfn, 0)),
6102 DECL_CLASS_CONTEXT (TREE_OPERAND (pfn, 0)),
6104 vfield_offset = get_vfield_offset (vfield_offset);
6105 delta2 = size_binop (PLUS_EXPR, vfield_offset, delta2);
6107 /* Map everything down one to make room for the null pointer to member. */
6108 index = size_binop (PLUS_EXPR,
6109 DECL_VINDEX (TREE_OPERAND (pfn, 0)),
6111 u = build_nt (CONSTRUCTOR, 0, tree_cons (delta2_identifier, delta2, NULL_TREE));
6115 index = size_binop (MINUS_EXPR, integer_zero_node, integer_one_node);
6117 npfn = build1 (NOP_EXPR, type, pfn);
6118 TREE_CONSTANT (npfn) = TREE_CONSTANT (pfn);
6120 u = build_nt (CONSTRUCTOR, 0, tree_cons (pfn_identifier, npfn, NULL_TREE));
6123 u = build_nt (CONSTRUCTOR, 0, tree_cons (NULL_TREE, delta,
6124 tree_cons (NULL_TREE, index,
6125 tree_cons (NULL_TREE, u, NULL_TREE))));
6126 return digest_init (TYPE_GET_PTRMEMFUNC_TYPE (type), u, (tree*)0);
6129 /* Convert value RHS to type TYPE as preparation for an assignment
6130 to an lvalue of type TYPE.
6131 The real work of conversion is done by `convert'.
6132 The purpose of this function is to generate error messages
6133 for assignments that are not allowed in C.
6134 ERRTYPE is a string to use in error messages:
6135 "assignment", "return", etc.
6137 C++: attempts to allow `convert' to find conversions involving
6138 implicit type conversion between aggregate and scalar types
6139 as per 8.5.6 of C++ manual. Does not randomly dereference
6140 pointers to aggregates! */
6143 convert_for_assignment (type, rhs, errtype, fndecl, parmnum)
6149 register enum tree_code codel = TREE_CODE (type);
6150 register tree rhstype;
6151 register enum tree_code coder = TREE_CODE (TREE_TYPE (rhs));
6153 if (coder == UNKNOWN_TYPE)
6154 rhs = instantiate_type (type, rhs, 1);
6156 if (coder == ERROR_MARK)
6157 return error_mark_node;
6159 if (codel == OFFSET_TYPE)
6161 type = TREE_TYPE (type);
6162 codel = TREE_CODE (type);
6165 /* Strip NON_LVALUE_EXPRs since we aren't using as an lvalue. */
6166 if (TREE_CODE (rhs) == NON_LVALUE_EXPR)
6167 rhs = TREE_OPERAND (rhs, 0);
6169 if (rhs == error_mark_node)
6170 return error_mark_node;
6172 if (TREE_VALUE (rhs) == error_mark_node)
6173 return error_mark_node;
6175 if (TREE_CODE (TREE_TYPE (rhs)) == OFFSET_TYPE)
6177 rhs = resolve_offset_ref (rhs);
6178 if (rhs == error_mark_node)
6179 return error_mark_node;
6180 rhstype = TREE_TYPE (rhs);
6181 coder = TREE_CODE (rhstype);
6184 if (TREE_CODE (TREE_TYPE (rhs)) == ARRAY_TYPE
6185 || TREE_CODE (TREE_TYPE (rhs)) == FUNCTION_TYPE
6186 || TREE_CODE (TREE_TYPE (rhs)) == METHOD_TYPE)
6187 rhs = default_conversion (rhs);
6188 else if (TREE_CODE (TREE_TYPE (rhs)) == REFERENCE_TYPE)
6189 rhs = convert_from_reference (rhs);
6191 rhstype = TREE_TYPE (rhs);
6192 coder = TREE_CODE (rhstype);
6194 /* This should no longer change types on us. */
6195 if (TREE_CODE (rhs) == CONST_DECL)
6196 rhs = DECL_INITIAL (rhs);
6197 else if (TREE_READONLY_DECL_P (rhs))
6198 rhs = decl_constant_value (rhs);
6200 if (type == rhstype)
6202 overflow_warning (rhs);
6206 if (coder == VOID_TYPE)
6208 error ("void value not ignored as it ought to be");
6209 return error_mark_node;
6211 /* Arithmetic types all interconvert. */
6212 if ((codel == INTEGER_TYPE || codel == REAL_TYPE || codel == BOOLEAN_TYPE)
6213 && (coder == INTEGER_TYPE || coder == REAL_TYPE || coder == BOOLEAN_TYPE))
6215 /* But we should warn if assigning REAL_TYPE to INTEGER_TYPE. */
6216 if (coder == REAL_TYPE && codel == INTEGER_TYPE)
6219 cp_warning ("`%T' used for argument %P of `%D'",
6220 rhstype, parmnum, fndecl);
6222 cp_warning ("%s to `%T' from `%T'", errtype, type, rhstype);
6224 /* And we should warn if assigning a negative value to
6225 an unsigned variable. */
6226 else if (TREE_UNSIGNED (type) && codel != BOOLEAN_TYPE)
6228 if (TREE_CODE (rhs) == INTEGER_CST
6229 && TREE_NEGATED_INT (rhs))
6232 cp_warning ("negative value `%E' passed as argument %P of `%D'",
6233 rhs, parmnum, fndecl);
6235 cp_warning ("%s of negative value `%E' to `%T'",
6236 errtype, rhs, type);
6238 overflow_warning (rhs);
6239 if (TREE_CONSTANT (rhs))
6243 return convert_and_check (type, rhs);
6245 /* Conversions involving enums. */
6246 else if ((codel == ENUMERAL_TYPE
6247 && (INTEGRAL_CODE_P (coder) || coder == REAL_TYPE))
6248 || (coder == ENUMERAL_TYPE
6249 && (INTEGRAL_CODE_P (codel) || codel == REAL_TYPE)))
6251 return cp_convert (type, rhs, CONV_IMPLICIT, LOOKUP_NORMAL);
6253 /* Conversions among pointers */
6254 else if (codel == POINTER_TYPE
6255 && (coder == POINTER_TYPE
6256 || (coder == RECORD_TYPE
6257 && (IS_SIGNATURE_POINTER (rhstype)
6258 || IS_SIGNATURE_REFERENCE (rhstype)))))
6260 register tree ttl = TREE_TYPE (type);
6264 if (coder == RECORD_TYPE)
6266 rhs = build_optr_ref (rhs);
6267 rhstype = TREE_TYPE (rhs);
6269 ttr = TREE_TYPE (rhstype);
6271 /* If both pointers are of aggregate type, then we
6272 can give better error messages, and save some work
6274 if (TREE_CODE (ttl) == RECORD_TYPE && TREE_CODE (ttr) == RECORD_TYPE)
6278 if (TYPE_MAIN_VARIANT (ttl) == TYPE_MAIN_VARIANT (ttr)
6279 || type == class_star_type_node
6280 || rhstype == class_star_type_node)
6281 binfo = TYPE_BINFO (ttl);
6283 binfo = get_binfo (ttl, ttr, 1);
6285 if (binfo == error_mark_node)
6286 return error_mark_node;
6288 return error_not_base_type (ttl, ttr);
6290 if (! TYPE_READONLY (ttl) && TYPE_READONLY (ttr))
6293 cp_pedwarn ("passing `%T' as argument %P of `%D' discards const",
6294 rhstype, parmnum, fndecl);
6296 cp_pedwarn ("%s to `%T' from `%T' discards const",
6297 errtype, type, rhstype);
6299 if (! TYPE_VOLATILE (ttl) && TYPE_VOLATILE (ttr))
6302 cp_pedwarn ("passing `%T' as argument %P of `%D' discards volatile",
6303 rhstype, parmnum, fndecl);
6305 cp_pedwarn ("%s to `%T' from `%T' discards volatile",
6306 errtype, type, rhstype);
6310 /* Any non-function converts to a [const][volatile] void *
6311 and vice versa; otherwise, targets must be the same.
6312 Meanwhile, the lhs target must have all the qualifiers of the rhs. */
6313 else if (TYPE_MAIN_VARIANT (ttl) == void_type_node
6314 || TYPE_MAIN_VARIANT (ttr) == void_type_node
6315 || (ctt = comp_target_types (type, rhstype, 1))
6316 || (unsigned_type (TYPE_MAIN_VARIANT (ttl))
6317 == unsigned_type (TYPE_MAIN_VARIANT (ttr))))
6319 /* ARM $4.8, commentary on p39. */
6320 if (TYPE_MAIN_VARIANT (ttl) == void_type_node
6321 && TREE_CODE (ttr) == OFFSET_TYPE)
6323 cp_error ("no standard conversion from `%T' to `void *'", ttr);
6324 return error_mark_node;
6328 cp_pedwarn ("converting `%T' to `%T' is a contravariance violation",
6331 if (TYPE_MAIN_VARIANT (ttl) != void_type_node
6332 && TYPE_MAIN_VARIANT (ttr) == void_type_node
6333 && rhs != null_pointer_node)
6335 if (coder == RECORD_TYPE)
6336 cp_pedwarn ("implicit conversion of signature pointer to type `%T'",
6339 pedwarn ("ANSI C++ forbids implicit conversion from `void *' in %s",
6342 /* Const and volatile mean something different for function types,
6343 so the usual warnings are not appropriate. */
6344 else if ((TREE_CODE (ttr) != FUNCTION_TYPE && TREE_CODE (ttr) != METHOD_TYPE)
6345 || (TREE_CODE (ttl) != FUNCTION_TYPE && TREE_CODE (ttl) != METHOD_TYPE))
6347 if (TREE_CODE (ttl) == OFFSET_TYPE
6348 && binfo_member (TYPE_OFFSET_BASETYPE (ttr),
6349 CLASSTYPE_VBASECLASSES (TYPE_OFFSET_BASETYPE (ttl))))
6351 sorry ("%s between pointer to members converting across virtual baseclasses", errtype);
6352 return error_mark_node;
6354 else if (! TYPE_READONLY (ttl) && TYPE_READONLY (ttr))
6357 cp_pedwarn ("passing `%T' as argument %P of `%D' discards const",
6358 rhstype, parmnum, fndecl);
6360 cp_pedwarn ("%s to `%T' from `%T' discards const",
6361 errtype, type, rhstype);
6363 else if (! TYPE_VOLATILE (ttl) && TYPE_VOLATILE (ttr))
6366 cp_pedwarn ("passing `%T' as argument %P of `%D' discards volatile",
6367 rhstype, parmnum, fndecl);
6369 cp_pedwarn ("%s to `%T' from `%T' discards volatile",
6370 errtype, type, rhstype);
6372 else if (TREE_CODE (ttl) == TREE_CODE (ttr)
6373 && ! comp_target_types (type, rhstype, 1))
6376 cp_pedwarn ("passing `%T' as argument %P of `%D' changes signedness",
6377 rhstype, parmnum, fndecl);
6379 cp_pedwarn ("%s to `%T' from `%T' changes signedness",
6380 errtype, type, rhstype);
6384 else if (TREE_CODE (ttr) == OFFSET_TYPE
6385 && TREE_CODE (ttl) != OFFSET_TYPE)
6387 /* Normally, pointers to different type codes (other
6388 than void) are not compatible, but we perform
6389 some type instantiation if that resolves the
6390 ambiguity of (X Y::*) and (X *). */
6392 if (current_class_decl)
6394 if (TREE_CODE (rhs) == INTEGER_CST)
6396 rhs = build (PLUS_EXPR, build_pointer_type (TREE_TYPE (ttr)),
6397 current_class_decl, rhs);
6398 return convert_for_assignment (type, rhs,
6399 errtype, fndecl, parmnum);
6402 if (TREE_CODE (ttl) == METHOD_TYPE)
6403 error ("%s between pointer-to-method and pointer-to-member types",
6406 error ("%s between pointer and pointer-to-member types", errtype);
6407 return error_mark_node;
6411 int add_quals = 0, const_parity = 0, volatile_parity = 0;
6413 int unsigned_parity;
6416 /* This code is basically a duplicate of comp_ptr_ttypes_real. */
6417 for (; ; ttl = TREE_TYPE (ttl), ttr = TREE_TYPE (ttr))
6420 const_parity |= (TYPE_READONLY (ttl) < TYPE_READONLY (ttr));
6421 volatile_parity |= (TYPE_VOLATILE (ttl) < TYPE_VOLATILE (ttr));
6424 && (TYPE_READONLY (ttl) > TYPE_READONLY (ttr)
6425 || TYPE_VOLATILE (ttl) > TYPE_VOLATILE (ttr)))
6427 left_const &= TYPE_READONLY (ttl);
6429 if (TREE_CODE (ttl) != POINTER_TYPE
6430 || TREE_CODE (ttr) != POINTER_TYPE)
6433 unsigned_parity = TREE_UNSIGNED (ttl) - TREE_UNSIGNED (ttr);
6434 if (unsigned_parity)
6436 if (TREE_UNSIGNED (ttl))
6437 ttr = unsigned_type (ttr);
6439 ttl = unsigned_type (ttl);
6442 if (comp_target_types (ttl, ttr, nptrs) > 0)
6447 cp_pedwarn ("passing `%T' as argument %P of `%D' adds cv-quals without intervening `const'",
6448 rhstype, parmnum, fndecl);
6450 cp_pedwarn ("%s to `%T' from `%T' adds cv-quals without intervening `const'",
6451 errtype, type, rhstype);
6456 cp_pedwarn ("passing `%T' as argument %P of `%D' discards const",
6457 rhstype, parmnum, fndecl);
6459 cp_pedwarn ("%s to `%T' from `%T' discards const",
6460 errtype, type, rhstype);
6462 if (volatile_parity)
6465 cp_pedwarn ("passing `%T' as argument %P of `%D' discards volatile",
6466 rhstype, parmnum, fndecl);
6468 cp_pedwarn ("%s to `%T' from `%T' discards volatile",
6469 errtype, type, rhstype);
6471 if (unsigned_parity > 0)
6474 cp_pedwarn ("passing `%T' as argument %P of `%D' changes signed to unsigned",
6475 rhstype, parmnum, fndecl);
6477 cp_pedwarn ("%s to `%T' from `%T' changes signed to unsigned",
6478 errtype, type, rhstype);
6480 else if (unsigned_parity < 0)
6483 cp_pedwarn ("passing `%T' as argument %P of `%D' changes unsigned to signed",
6484 rhstype, parmnum, fndecl);
6486 cp_pedwarn ("%s to `%T' from `%T' changes unsigned to signed",
6487 errtype, type, rhstype);
6490 /* C++ is not so friendly about converting function and
6491 member function pointers as C. Emit warnings here. */
6492 if (TREE_CODE (ttl) == FUNCTION_TYPE
6493 || TREE_CODE (ttl) == METHOD_TYPE)
6494 if (! comptypes (ttl, ttr, 0))
6496 warning ("conflicting function types in %s:", errtype);
6497 cp_warning ("\t`%T' != `%T'", type, rhstype);
6500 else if (TREE_CODE (TREE_TYPE (rhs)) == METHOD_TYPE)
6502 /* When does this happen? */
6503 my_friendly_abort (119);
6504 /* Conversion of a pointer-to-member type to void *. */
6505 rhs = build_unary_op (ADDR_EXPR, rhs, 0);
6506 TREE_TYPE (rhs) = type;
6509 else if (TREE_CODE (TREE_TYPE (rhs)) == OFFSET_TYPE)
6511 /* When does this happen? */
6512 my_friendly_abort (120);
6513 /* Conversion of a pointer-to-member type to void *. */
6514 rhs = build_unary_op (ADDR_EXPR, rhs, 0);
6515 TREE_TYPE (rhs) = type;
6521 cp_error ("passing `%T' as argument %P of `%D'",
6522 rhstype, parmnum, fndecl);
6524 cp_error ("%s to `%T' from `%T'", errtype, type, rhstype);
6525 return error_mark_node;
6528 return convert (type, rhs);
6530 else if (codel == POINTER_TYPE && coder == INTEGER_TYPE)
6532 /* An explicit constant 0 can convert to a pointer,
6533 but not a 0 that results from casting or folding. */
6534 if (! (TREE_CODE (rhs) == INTEGER_CST && integer_zerop (rhs)))
6537 cp_pedwarn ("passing `%T' to argument %P of `%D' lacks a cast",
6538 rhstype, parmnum, fndecl);
6540 cp_pedwarn ("%s to `%T' from `%T' lacks a cast",
6541 errtype, type, rhstype);
6542 return convert (type, rhs);
6544 return null_pointer_node;
6546 else if (codel == INTEGER_TYPE
6547 && (coder == POINTER_TYPE
6548 || (coder == RECORD_TYPE
6549 && (IS_SIGNATURE_POINTER (rhstype)
6550 || TYPE_PTRMEMFUNC_FLAG (rhstype)
6551 || IS_SIGNATURE_REFERENCE (rhstype)))))
6554 cp_pedwarn ("passing `%T' to argument %P of `%D' lacks a cast",
6555 rhstype, parmnum, fndecl);
6557 cp_pedwarn ("%s to `%T' from `%T' lacks a cast",
6558 errtype, type, rhstype);
6559 return convert (type, rhs);
6561 else if (codel == BOOLEAN_TYPE
6562 && (coder == POINTER_TYPE
6563 || (coder == RECORD_TYPE
6564 && (IS_SIGNATURE_POINTER (rhstype)
6565 || TYPE_PTRMEMFUNC_FLAG (rhstype)
6566 || IS_SIGNATURE_REFERENCE (rhstype)))))
6567 return convert (type, rhs);
6570 else if (((coder == POINTER_TYPE
6571 && TREE_CODE (TREE_TYPE (rhstype)) == METHOD_TYPE)
6572 || integer_zerop (rhs)
6573 || TYPE_PTRMEMFUNC_P (rhstype))
6574 && TYPE_PTRMEMFUNC_P (type))
6576 tree ttl = TYPE_PTRMEMFUNC_FN_TYPE (type);
6577 tree ttr = (TREE_CODE (rhstype) == POINTER_TYPE ? rhstype
6578 : TYPE_PTRMEMFUNC_FN_TYPE (type));
6579 int ctt = comp_target_types (ttl, ttr, 1);
6582 cp_pedwarn ("converting `%T' to `%T' is a contravariance violation",
6585 cp_error ("%s to `%T' from `%T'", errtype, ttl, ttr);
6587 /* compatible pointer to member functions. */
6588 return build_ptrmemfunc (ttl, rhs, 0);
6590 else if (codel == ERROR_MARK || coder == ERROR_MARK)
6591 return error_mark_node;
6593 /* This should no longer happen. References are initialized via
6594 `convert_for_initialization'. They should otherwise be
6595 bashed before coming here. */
6596 else if (codel == REFERENCE_TYPE)
6597 my_friendly_abort (317);
6598 else if (TYPE_MAIN_VARIANT (type) == TYPE_MAIN_VARIANT (TREE_TYPE (rhs)))
6600 tree nrhs = build1 (NOP_EXPR, type, rhs);
6601 TREE_CONSTANT (nrhs) = TREE_CONSTANT (rhs);
6604 else if (TYPE_HAS_CONSTRUCTOR (type) || IS_AGGR_TYPE (TREE_TYPE (rhs)))
6605 return convert (type, rhs);
6607 cp_error ("%s to `%T' from `%T'", errtype, type, rhstype);
6608 return error_mark_node;
6611 /* Convert RHS to be of type TYPE. If EXP is non-zero,
6612 it is the target of the initialization.
6613 ERRTYPE is a string to use in error messages.
6615 Two major differences between the behavior of
6616 `convert_for_assignment' and `convert_for_initialization'
6617 are that references are bashed in the former, while
6618 copied in the latter, and aggregates are assigned in
6619 the former (operator=) while initialized in the
6622 If using constructor make sure no conversion operator exists, if one does
6623 exist, an ambiguity exists.
6625 If flags doesn't include LOOKUP_COMPLAIN, don't complain about anything. */
6627 convert_for_initialization (exp, type, rhs, flags, errtype, fndecl, parmnum)
6628 tree exp, type, rhs;
6634 register enum tree_code codel = TREE_CODE (type);
6635 register tree rhstype;
6636 register enum tree_code coder;
6638 /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
6639 Strip such NOP_EXPRs, since RHS is used in non-lvalue context. */
6640 if (TREE_CODE (rhs) == NOP_EXPR
6641 && TREE_TYPE (rhs) == TREE_TYPE (TREE_OPERAND (rhs, 0))
6642 && codel != REFERENCE_TYPE)
6643 rhs = TREE_OPERAND (rhs, 0);
6645 if (rhs == error_mark_node
6646 || (TREE_CODE (rhs) == TREE_LIST && TREE_VALUE (rhs) == error_mark_node))
6647 return error_mark_node;
6649 if (TREE_CODE (TREE_TYPE (rhs)) == OFFSET_TYPE)
6651 rhs = resolve_offset_ref (rhs);
6652 if (rhs == error_mark_node)
6653 return error_mark_node;
6656 if (TREE_CODE (TREE_TYPE (rhs)) == REFERENCE_TYPE)
6657 rhs = convert_from_reference (rhs);
6659 if ((TREE_CODE (TREE_TYPE (rhs)) == ARRAY_TYPE
6660 && TREE_CODE (type) != ARRAY_TYPE
6661 && (TREE_CODE (type) != REFERENCE_TYPE
6662 || TREE_CODE (TREE_TYPE (type)) != ARRAY_TYPE))
6663 || TREE_CODE (TREE_TYPE (rhs)) == FUNCTION_TYPE
6664 || TREE_CODE (TREE_TYPE (rhs)) == METHOD_TYPE)
6665 rhs = default_conversion (rhs);
6667 rhstype = TREE_TYPE (rhs);
6668 coder = TREE_CODE (rhstype);
6670 if (coder == UNKNOWN_TYPE)
6672 rhs = instantiate_type (type, rhs, 1);
6673 rhstype = TREE_TYPE (rhs);
6674 coder = TREE_CODE (rhstype);
6677 if (coder == ERROR_MARK)
6678 return error_mark_node;
6680 /* We accept references to incomplete types, so we can
6681 return here before checking if RHS is of complete type. */
6683 if (codel == REFERENCE_TYPE)
6685 /* This should eventually happen in convert_arguments. */
6686 extern int warningcount, errorcount;
6690 savew = warningcount, savee = errorcount;
6691 rhs = convert_to_reference (type, rhs, CONV_IMPLICIT, flags,
6692 exp ? exp : error_mark_node);
6695 if (warningcount > savew)
6696 cp_warning_at ("in passing argument %P of `%+D'", parmnum, fndecl);
6697 else if (errorcount > savee)
6698 cp_error_at ("in passing argument %P of `%+D'", parmnum, fndecl);
6703 rhs = require_complete_type (rhs);
6704 if (rhs == error_mark_node)
6705 return error_mark_node;
6707 if (exp != 0) exp = require_complete_type (exp);
6708 if (exp == error_mark_node)
6709 return error_mark_node;
6711 if (TREE_CODE (rhstype) == REFERENCE_TYPE)
6712 rhstype = TREE_TYPE (rhstype);
6714 if (TYPE_LANG_SPECIFIC (type)
6715 && (IS_SIGNATURE_POINTER (type) || IS_SIGNATURE_REFERENCE (type)))
6716 return build_signature_pointer_constructor (type, rhs);
6718 if (IS_AGGR_TYPE (type)
6719 && (TYPE_NEEDS_CONSTRUCTING (type) || TREE_HAS_CONSTRUCTOR (rhs)))
6721 if (TYPE_MAIN_VARIANT (type) == TYPE_MAIN_VARIANT (rhstype))
6723 /* This is sufficient to perform initialization. No need,
6724 apparently, to go through X(X&) to do first-cut
6725 initialization. Return through a TARGET_EXPR so that we get
6726 cleanups if it is used. */
6727 if (TREE_CODE (rhs) == CALL_EXPR)
6729 rhs = build_cplus_new (type, rhs);
6732 /* Handle the case of default parameter initialization and
6733 initialization of static variables. */
6734 else if (TREE_CODE (rhs) == TARGET_EXPR)
6736 else if (TREE_CODE (rhs) == INDIRECT_REF && TREE_HAS_CONSTRUCTOR (rhs))
6738 my_friendly_assert (TREE_CODE (TREE_OPERAND (rhs, 0)) == CALL_EXPR, 318);
6741 my_friendly_assert (TREE_VALUE (TREE_OPERAND (TREE_OPERAND (rhs, 0), 1)) == NULL_TREE, 316);
6742 TREE_VALUE (TREE_OPERAND (TREE_OPERAND (rhs, 0), 1))
6743 = build_unary_op (ADDR_EXPR, exp, 0);
6746 rhs = build_cplus_new (type, TREE_OPERAND (rhs, 0));
6749 else if (TYPE_HAS_TRIVIAL_INIT_REF (type))
6752 if (TYPE_MAIN_VARIANT (type) == TYPE_MAIN_VARIANT (rhstype)
6753 || (IS_AGGR_TYPE (rhstype) && UNIQUELY_DERIVED_FROM_P (type, rhstype)))
6755 if (TYPE_HAS_INIT_REF (type))
6757 tree init = build_method_call (exp, ctor_identifier,
6758 build_tree_list (NULL_TREE, rhs),
6759 TYPE_BINFO (type), LOOKUP_NORMAL);
6761 if (init == error_mark_node)
6762 return error_mark_node;
6766 exp = build_cplus_new (type, init);
6770 return build (COMPOUND_EXPR, type, init, exp);
6773 /* ??? The following warnings are turned off because
6774 this is another place where the default X(X&) constructor
6776 if (TYPE_HAS_ASSIGNMENT (type))
6777 cp_warning ("bitwise copy: `%T' defines operator=", type);
6779 if (TREE_CODE (TREE_TYPE (rhs)) == REFERENCE_TYPE)
6780 rhs = convert_from_reference (rhs);
6781 if (type != rhstype)
6783 tree nrhs = build1 (NOP_EXPR, type, rhs);
6784 TREE_CONSTANT (nrhs) = TREE_CONSTANT (rhs);
6790 return cp_convert (type, rhs, CONV_OLD_CONVERT,
6791 flags | LOOKUP_NO_CONVERSION);
6794 if (type == TREE_TYPE (rhs))
6796 if (TREE_READONLY_DECL_P (rhs))
6797 rhs = decl_constant_value (rhs);
6801 return convert_for_assignment (type, rhs, errtype, fndecl, parmnum);
6804 /* Expand an ASM statement with operands, handling output operands
6805 that are not variables or INDIRECT_REFS by transforming such
6806 cases into cases that expand_asm_operands can handle.
6808 Arguments are same as for expand_asm_operands.
6810 We don't do default conversions on all inputs, because it can screw
6811 up operands that are expected to be in memory. */
6814 c_expand_asm_operands (string, outputs, inputs, clobbers, vol, filename, line)
6815 tree string, outputs, inputs, clobbers;
6820 int noutputs = list_length (outputs);
6822 /* o[I] is the place that output number I should be written. */
6823 register tree *o = (tree *) alloca (noutputs * sizeof (tree));
6826 /* Record the contents of OUTPUTS before it is modified. */
6827 for (i = 0, tail = outputs; tail; tail = TREE_CHAIN (tail), i++)
6828 o[i] = TREE_VALUE (tail);
6830 /* Generate the ASM_OPERANDS insn;
6831 store into the TREE_VALUEs of OUTPUTS some trees for
6832 where the values were actually stored. */
6833 expand_asm_operands (string, outputs, inputs, clobbers, vol, filename, line);
6835 /* Copy all the intermediate outputs into the specified outputs. */
6836 for (i = 0, tail = outputs; tail; tail = TREE_CHAIN (tail), i++)
6838 if (o[i] != TREE_VALUE (tail))
6840 expand_expr (build_modify_expr (o[i], NOP_EXPR, TREE_VALUE (tail)),
6841 const0_rtx, VOIDmode, 0);
6844 /* Detect modification of read-only values.
6845 (Otherwise done by build_modify_expr.) */
6848 tree type = TREE_TYPE (o[i]);
6849 if (TYPE_READONLY (type)
6850 || ((TREE_CODE (type) == RECORD_TYPE
6851 || TREE_CODE (type) == UNION_TYPE)
6852 && C_TYPE_FIELDS_READONLY (type)))
6853 readonly_error (o[i], "modification by `asm'", 1);
6857 /* Those MODIFY_EXPRs could do autoincrements. */
6861 /* Expand a C `return' statement.
6862 RETVAL is the expression for what to return,
6863 or a null pointer for `return;' with no value.
6865 C++: upon seeing a `return', we must call destructors on all
6866 variables in scope which had constructors called on them.
6867 This means that if in a destructor, the base class destructors
6868 must be called before returning.
6870 The RETURN statement in C++ has initialization semantics. */
6873 c_expand_return (retval)
6876 extern struct nesting *cond_stack, *loop_stack, *case_stack;
6877 extern tree dtor_label, ctor_label;
6878 tree result = DECL_RESULT (current_function_decl);
6879 tree valtype = TREE_TYPE (result);
6880 int returns_value = 1;
6882 if (TREE_THIS_VOLATILE (current_function_decl))
6883 warning ("function declared `noreturn' has a `return' statement");
6885 if (retval == error_mark_node)
6887 current_function_returns_null = 1;
6891 if (current_template_parms)
6893 add_tree (build_min_nt (RETURN_STMT, retval));
6897 if (retval == NULL_TREE)
6899 /* A non-named return value does not count. */
6901 /* Can't just return from a destructor. */
6904 expand_goto (dtor_label);
6908 if (DECL_CONSTRUCTOR_P (current_function_decl))
6909 retval = current_class_decl;
6910 else if (DECL_NAME (result) != NULL_TREE
6911 && TREE_CODE (valtype) != VOID_TYPE)
6915 current_function_returns_null = 1;
6917 if (valtype != NULL_TREE && TREE_CODE (valtype) != VOID_TYPE)
6919 if (DECL_NAME (DECL_RESULT (current_function_decl)) == NULL_TREE)
6921 pedwarn ("`return' with no value, in function returning non-void");
6922 /* Clear this, so finish_function won't say that we
6923 reach the end of a non-void function (which we don't,
6924 we gave a return!). */
6925 current_function_returns_null = 0;
6929 expand_null_return ();
6933 else if (DECL_CONSTRUCTOR_P (current_function_decl)
6934 && retval != current_class_decl)
6936 error ("return from a constructor: use `this = ...' instead");
6937 retval = current_class_decl;
6940 if (valtype == NULL_TREE || TREE_CODE (valtype) == VOID_TYPE)
6942 current_function_returns_null = 1;
6943 /* We do this here so we'll avoid a warning about how the function
6944 "may or may not return a value" in finish_function. */
6948 pedwarn ("`return' with a value, in function returning void");
6949 expand_return (retval);
6951 /* Add some useful error checking for C++. */
6952 else if (TREE_CODE (valtype) == REFERENCE_TYPE)
6954 tree whats_returned;
6955 tree tmp_result = result;
6957 /* Don't initialize directly into a non-BLKmode retval, since that
6958 could lose when being inlined by another caller. (GCC can't
6959 read the function return register in an inline function when
6960 the return value is being ignored). */
6961 if (result && TYPE_MODE (TREE_TYPE (tmp_result)) != BLKmode)
6964 /* convert to reference now, so we can give error if we
6965 return an reference to a non-lvalue. */
6966 retval = convert_for_initialization (tmp_result, valtype, retval,
6967 LOOKUP_NORMAL, "return",
6970 /* Sort through common things to see what it is
6971 we are returning. */
6972 whats_returned = retval;
6973 if (TREE_CODE (whats_returned) == COMPOUND_EXPR)
6975 whats_returned = TREE_OPERAND (whats_returned, 1);
6976 if (TREE_CODE (whats_returned) == ADDR_EXPR)
6977 whats_returned = TREE_OPERAND (whats_returned, 0);
6979 if (TREE_CODE (whats_returned) == ADDR_EXPR)
6981 whats_returned = TREE_OPERAND (whats_returned, 0);
6982 while (TREE_CODE (whats_returned) == NEW_EXPR
6983 || TREE_CODE (whats_returned) == TARGET_EXPR)
6985 /* Get the target. */
6986 whats_returned = TREE_OPERAND (whats_returned, 0);
6987 warning ("returning reference to temporary");
6991 if (TREE_CODE (whats_returned) == VAR_DECL && DECL_NAME (whats_returned))
6993 if (TEMP_NAME_P (DECL_NAME (whats_returned)))
6994 warning ("reference to non-lvalue returned");
6995 else if (! TREE_STATIC (whats_returned)
6996 && IDENTIFIER_LOCAL_VALUE (DECL_NAME (whats_returned))
6997 && !TREE_PUBLIC (whats_returned))
6998 cp_warning_at ("reference to local variable `%D' returned", whats_returned);
7001 else if (TREE_CODE (retval) == ADDR_EXPR)
7003 tree whats_returned = TREE_OPERAND (retval, 0);
7005 if (TREE_CODE (whats_returned) == VAR_DECL
7006 && DECL_NAME (whats_returned)
7007 && IDENTIFIER_LOCAL_VALUE (DECL_NAME (whats_returned))
7008 && !TREE_STATIC (whats_returned)
7009 && !TREE_PUBLIC (whats_returned))
7010 cp_warning_at ("address of local variable `%D' returned", whats_returned);
7012 else if (TREE_CODE (retval) == VAR_DECL)
7014 if (TREE_CODE (TREE_TYPE (retval)) == ARRAY_TYPE
7015 && DECL_NAME (retval)
7016 && IDENTIFIER_LOCAL_VALUE (DECL_NAME (retval))
7017 && !TREE_STATIC (retval)
7018 && !TREE_PUBLIC (retval))
7019 cp_warning_at ("address of local array `%D' returned", retval);
7022 /* Now deal with possible C++ hair:
7023 (1) Compute the return value.
7024 (2) If there are aggregate values with destructors which
7025 must be cleaned up, clean them (taking care
7026 not to clobber the return value).
7027 (3) If an X(X&) constructor is defined, the return
7028 value must be returned via that. */
7030 /* If we're returning in a register, we can't initialize the
7031 return value from a TARGET_EXPR. */
7032 if (TREE_CODE (retval) == TARGET_EXPR
7033 && TYPE_MAIN_VARIANT (TREE_TYPE (retval)) == TYPE_MAIN_VARIANT (valtype)
7034 && ! current_function_returns_struct)
7035 retval = expand_target_expr (retval);
7037 if (retval == result
7038 /* Watch out for constructors, which "return" aggregates
7039 via initialization, but which otherwise "return" a pointer. */
7040 || DECL_CONSTRUCTOR_P (current_function_decl))
7042 /* This is just an error--it's already been reported. */
7043 if (TYPE_SIZE (valtype) == NULL_TREE)
7046 if (TYPE_MODE (valtype) != BLKmode
7047 && any_pending_cleanups (1))
7048 retval = get_temp_regvar (valtype, retval);
7050 else if (IS_AGGR_TYPE (valtype) && current_function_returns_struct)
7052 expand_aggr_init (result, retval, 0, LOOKUP_ONLYCONVERTING);
7053 expand_cleanups_to (NULL_TREE);
7054 DECL_INITIAL (result) = NULL_TREE;
7059 if (TYPE_MODE (valtype) == VOIDmode)
7061 if (TYPE_MODE (TREE_TYPE (result)) != VOIDmode
7062 && warn_return_type)
7063 warning ("return of void value in function returning non-void");
7064 expand_expr_stmt (retval);
7068 else if (TYPE_MODE (valtype) != BLKmode
7069 && any_pending_cleanups (1))
7071 retval = get_temp_regvar (valtype, retval);
7072 expand_cleanups_to (NULL_TREE);
7077 retval = convert_for_initialization (result, valtype, retval,
7079 "return", NULL_TREE, 0);
7080 DECL_INITIAL (result) = NULL_TREE;
7082 if (retval == error_mark_node)
7088 if (retval != NULL_TREE
7089 && TREE_CODE_CLASS (TREE_CODE (retval)) == 'd'
7090 && cond_stack == 0 && loop_stack == 0 && case_stack == 0)
7091 current_function_return_value = retval;
7095 /* Everything's great--RETVAL is in RESULT. */
7096 if (original_result_rtx)
7098 store_expr (result, original_result_rtx, 0);
7099 expand_cleanups_to (NULL_TREE);
7100 use_variable (DECL_RTL (result));
7101 if (ctor_label && TREE_CODE (ctor_label) != ERROR_MARK)
7102 expand_goto (ctor_label);
7104 expand_null_return ();
7106 else if (retval && retval != result)
7108 /* Clear this out so the later call to decl_function_context
7109 won't end up bombing on us. */
7110 if (DECL_CONTEXT (result) == error_mark_node)
7111 DECL_CONTEXT (result) = NULL_TREE;
7112 /* Here is where we finally get RETVAL into RESULT.
7113 `expand_return' does the magic of protecting
7114 RESULT from cleanups. */
7115 retval = fold (build1 (CLEANUP_POINT_EXPR, TREE_TYPE (result),
7117 /* This part _must_ come second, because expand_return looks for
7118 the INIT_EXPR as the toplevel node only. :-( */
7119 retval = build (INIT_EXPR, TREE_TYPE (result), result, retval);
7120 TREE_SIDE_EFFECTS (retval) = 1;
7121 expand_return (retval);
7124 expand_return (result);
7128 /* We may still need to put RETVAL into RESULT. */
7129 result = DECL_RESULT (current_function_decl);
7130 if (original_result_rtx)
7132 /* Here we have a named return value that went
7133 into memory. We can compute RETVAL into that. */
7135 expand_assignment (result, retval, 0, 0);
7137 store_expr (result, original_result_rtx, 0);
7138 result = make_tree (TREE_TYPE (result), original_result_rtx);
7140 else if (ctor_label && TREE_CODE (ctor_label) != ERROR_MARK)
7142 /* Here RETVAL is CURRENT_CLASS_DECL, so there's nothing to do. */
7143 expand_goto (ctor_label);
7147 /* Here is where we finally get RETVAL into RESULT.
7148 `expand_return' does the magic of protecting
7149 RESULT from cleanups. */
7150 result = build (INIT_EXPR, TREE_TYPE (result), result, retval);
7151 TREE_SIDE_EFFECTS (result) = 1;
7152 expand_return (result);
7154 else if (TYPE_MODE (TREE_TYPE (result)) != VOIDmode)
7155 expand_return (result);
7158 current_function_returns_value = returns_value;
7160 /* One way to clear out cleanups that EXPR might
7161 generate. Note that this code will really be
7162 dead code, but that is ok--cleanups that were
7163 needed were handled by the magic of `return'. */
7164 expand_cleanups_to (NULL_TREE);
7167 /* Start a C switch statement, testing expression EXP.
7168 Return EXP if it is valid, an error node otherwise. */
7171 c_expand_start_case (exp)
7175 register enum tree_code code;
7177 /* Convert from references, etc. */
7178 exp = default_conversion (exp);
7179 type = TREE_TYPE (exp);
7180 code = TREE_CODE (type);
7182 if (IS_AGGR_TYPE_CODE (code))
7183 exp = build_type_conversion (CONVERT_EXPR, integer_type_node, exp, 1);
7185 if (exp == NULL_TREE)
7187 error ("switch quantity not an integer");
7188 exp = error_mark_node;
7190 type = TREE_TYPE (exp);
7191 code = TREE_CODE (type);
7193 if (code != INTEGER_TYPE && code != ENUMERAL_TYPE && code != ERROR_MARK)
7195 error ("switch quantity not an integer");
7196 exp = error_mark_node;
7202 exp = default_conversion (exp);
7203 type = TREE_TYPE (exp);
7204 index = get_unwidened (exp, 0);
7205 /* We can't strip a conversion from a signed type to an unsigned,
7206 because if we did, int_fits_type_p would do the wrong thing
7207 when checking case values for being in range,
7208 and it's too hard to do the right thing. */
7209 if (TREE_UNSIGNED (TREE_TYPE (exp))
7210 == TREE_UNSIGNED (TREE_TYPE (index)))
7215 (1, fold (build1 (CLEANUP_POINT_EXPR, TREE_TYPE (exp), exp)),
7216 type, "switch statement");
7221 /* CONSTP remembers whether or not all the intervening pointers in the `to'
7222 type have been const. */
7224 comp_ptr_ttypes_real (to, from, constp)
7228 for (; ; to = TREE_TYPE (to), from = TREE_TYPE (from))
7230 if (TREE_CODE (to) != TREE_CODE (from))
7233 /* Const and volatile mean something different for function types,
7234 so the usual checks are not appropriate. */
7235 if (TREE_CODE (to) != FUNCTION_TYPE && TREE_CODE (to) != METHOD_TYPE)
7237 if (TYPE_READONLY (from) > TYPE_READONLY (to)
7238 || TYPE_VOLATILE (from) > TYPE_VOLATILE (to))
7242 && (TYPE_READONLY (to) > TYPE_READONLY (from)
7243 || TYPE_VOLATILE (to) > TYPE_READONLY (from)))
7245 constp &= TYPE_READONLY (to);
7248 if (TREE_CODE (to) != POINTER_TYPE)
7249 return comptypes (TYPE_MAIN_VARIANT (to), TYPE_MAIN_VARIANT (from), 1);
7253 /* When comparing, say, char ** to char const **, this function takes the
7254 'char *' and 'char const *'. Do not pass non-pointer types to this
7257 comp_ptr_ttypes (to, from)
7260 return comp_ptr_ttypes_real (to, from, 1);