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 (processing_template_decl)
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
103 && current_class_ref != 0
104 && TREE_OPERAND (value, 0) == current_class_ref)
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_ptr);
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 == error_mark_node || TYPE_SIZE (type) != NULL_TREE)
132 else if (TREE_CODE (type) == ARRAY_TYPE && TYPE_DOMAIN (type))
134 tree t = complete_type (TREE_TYPE (type));
135 if (TYPE_SIZE (t) != NULL_TREE && ! processing_template_decl)
137 TYPE_NEEDS_CONSTRUCTING (type)
138 = TYPE_NEEDS_CONSTRUCTING (TYPE_MAIN_VARIANT (t));
139 TYPE_NEEDS_DESTRUCTOR (type)
140 = TYPE_NEEDS_DESTRUCTOR (TYPE_MAIN_VARIANT (t));
142 else if (IS_AGGR_TYPE (type) && CLASSTYPE_TEMPLATE_INSTANTIATION (type))
143 instantiate_class_template (TYPE_MAIN_VARIANT (type));
148 /* Return truthvalue of whether type of EXP is instantiated. */
154 return (TREE_CODE (exp) == TREE_LIST
155 || TREE_TYPE (exp) == unknown_type_node
156 || (TREE_CODE (TREE_TYPE (exp)) == OFFSET_TYPE
157 && TREE_TYPE (TREE_TYPE (exp)) == unknown_type_node));
160 /* Return truthvalue of whether T is function (or pfn) type. */
166 return (TREE_CODE (t) == FUNCTION_TYPE || TREE_CODE (t) == METHOD_TYPE
167 || (TREE_CODE (t) == POINTER_TYPE
168 && (TREE_CODE (TREE_TYPE (t)) == FUNCTION_TYPE
169 || TREE_CODE (TREE_TYPE (t)) == METHOD_TYPE)));
172 /* Do `exp = require_instantiated_type (type, exp);' to make sure EXP
173 does not have an uninstantiated type.
174 TYPE is type to instantiate with, if uninstantiated. */
177 require_instantiated_type (type, exp, errval)
178 tree type, exp, errval;
180 if (TREE_TYPE (exp) == NULL_TREE)
182 error ("argument list may not have an initializer list");
186 if (TREE_TYPE (exp) == unknown_type_node
187 || (TREE_CODE (TREE_TYPE (exp)) == OFFSET_TYPE
188 && TREE_TYPE (TREE_TYPE (exp)) == unknown_type_node))
190 exp = instantiate_type (type, exp, 1);
191 if (TREE_TYPE (exp) == error_mark_node)
197 /* Return a variant of TYPE which has all the type qualifiers of LIKE
198 as well as those of TYPE. */
201 qualify_type (type, like)
204 int constflag = TYPE_READONLY (type) || TYPE_READONLY (like);
205 int volflag = TYPE_VOLATILE (type) || TYPE_VOLATILE (like);
206 /* @@ Must do member pointers here. */
207 return cp_build_type_variant (type, constflag, volflag);
210 /* Return the common type of two parameter lists.
211 We assume that comptypes has already been done and returned 1;
212 if that isn't so, this may crash.
214 As an optimization, free the space we allocate if the parameter
215 lists are already common. */
221 tree oldargs = p1, newargs, n;
224 char *first_obj = (char *) oballoc (0);
226 len = list_length (p1);
227 newargs = tree_last (p1);
229 if (newargs == void_list_node)
238 newargs = tree_cons (NULL_TREE, NULL_TREE, newargs);
243 p1 = TREE_CHAIN (p1), p2 = TREE_CHAIN (p2), n = TREE_CHAIN (n), i++)
245 if (TREE_PURPOSE (p1) && !TREE_PURPOSE (p2))
247 TREE_PURPOSE (n) = TREE_PURPOSE (p1);
250 else if (! TREE_PURPOSE (p1))
252 if (TREE_PURPOSE (p2))
254 TREE_PURPOSE (n) = TREE_PURPOSE (p2);
260 if (1 != simple_cst_equal (TREE_PURPOSE (p1), TREE_PURPOSE (p2)))
262 TREE_PURPOSE (n) = TREE_PURPOSE (p2);
264 if (TREE_VALUE (p1) != TREE_VALUE (p2))
267 TREE_VALUE (n) = common_type (TREE_VALUE (p1), TREE_VALUE (p2));
270 TREE_VALUE (n) = TREE_VALUE (p1);
281 /* Return the common type of two types.
282 We assume that comptypes has already been done and returned 1;
283 if that isn't so, this may crash.
285 This is the type for the result of most arithmetic operations
286 if the operands have the given two types.
288 We do not deal with enumeral types here because they have already been
289 converted to integer types. */
295 register enum tree_code code1;
296 register enum tree_code code2;
299 /* Save time if the two types are the same. */
301 if (t1 == t2) return t1;
303 /* If one type is nonsense, use the other. */
304 if (t1 == error_mark_node)
306 if (t2 == error_mark_node)
309 /* Merge the attributes */
311 { register tree a1, a2;
312 a1 = TYPE_ATTRIBUTES (t1);
313 a2 = TYPE_ATTRIBUTES (t2);
315 /* Either one unset? Take the set one. */
317 if (!(attributes = a1))
320 /* One that completely contains the other? Take it. */
322 else if (a2 && !attribute_list_contained (a1, a2))
323 if (attribute_list_contained (a2, a1))
327 /* Pick the longest list, and hang on the other list. */
328 /* ??? For the moment we punt on the issue of attrs with args. */
330 if (list_length (a1) < list_length (a2))
331 attributes = a2, a2 = a1;
333 for (; a2; a2 = TREE_CHAIN (a2))
334 if (lookup_attribute (IDENTIFIER_POINTER (TREE_PURPOSE (a2)),
335 attributes) == NULL_TREE)
338 TREE_CHAIN (a1) = attributes;
344 /* Treat an enum type as the unsigned integer type of the same width. */
346 if (TREE_CODE (t1) == ENUMERAL_TYPE)
347 t1 = type_for_size (TYPE_PRECISION (t1), 1);
348 if (TREE_CODE (t2) == ENUMERAL_TYPE)
349 t2 = type_for_size (TYPE_PRECISION (t2), 1);
351 if (TYPE_PTRMEMFUNC_P (t1))
352 t1 = TYPE_PTRMEMFUNC_FN_TYPE (t1);
353 if (TYPE_PTRMEMFUNC_P (t2))
354 t2 = TYPE_PTRMEMFUNC_FN_TYPE (t2);
356 code1 = TREE_CODE (t1);
357 code2 = TREE_CODE (t2);
363 /* If only one is real, use it as the result. */
365 if (code1 == REAL_TYPE && code2 != REAL_TYPE)
366 return build_type_attribute_variant (t1, attributes);
368 if (code2 == REAL_TYPE && code1 != REAL_TYPE)
369 return build_type_attribute_variant (t2, attributes);
371 /* Both real or both integers; use the one with greater precision. */
373 if (TYPE_PRECISION (t1) > TYPE_PRECISION (t2))
374 return build_type_attribute_variant (t1, attributes);
375 else if (TYPE_PRECISION (t2) > TYPE_PRECISION (t1))
376 return build_type_attribute_variant (t2, attributes);
378 /* Same precision. Prefer longs to ints even when same size. */
380 if (TYPE_MAIN_VARIANT (t1) == long_unsigned_type_node
381 || TYPE_MAIN_VARIANT (t2) == long_unsigned_type_node)
382 return build_type_attribute_variant (long_unsigned_type_node,
385 if (TYPE_MAIN_VARIANT (t1) == long_integer_type_node
386 || TYPE_MAIN_VARIANT (t2) == long_integer_type_node)
388 /* But preserve unsignedness from the other type,
389 since long cannot hold all the values of an unsigned int. */
390 if (TREE_UNSIGNED (t1) || TREE_UNSIGNED (t2))
391 t1 = long_unsigned_type_node;
393 t1 = long_integer_type_node;
394 return build_type_attribute_variant (t1, attributes);
397 if (TYPE_MAIN_VARIANT (t1) == long_double_type_node
398 || TYPE_MAIN_VARIANT (t2) == long_double_type_node)
399 return build_type_attribute_variant (long_double_type_node,
402 /* Otherwise prefer the unsigned one. */
404 if (TREE_UNSIGNED (t1))
405 return build_type_attribute_variant (t1, attributes);
407 return build_type_attribute_variant (t2, attributes);
411 /* For two pointers, do this recursively on the target type,
412 and combine the qualifiers of the two types' targets. */
413 /* This code was turned off; I don't know why.
414 But ANSI C++ specifies doing this with the qualifiers.
415 So I turned it on again. */
417 tree tt1 = TYPE_MAIN_VARIANT (TREE_TYPE (t1));
418 tree tt2 = TYPE_MAIN_VARIANT (TREE_TYPE (t2));
420 = TYPE_READONLY (TREE_TYPE (t1)) || TYPE_READONLY (TREE_TYPE (t2));
422 = TYPE_VOLATILE (TREE_TYPE (t1)) || TYPE_VOLATILE (TREE_TYPE (t2));
427 else if (tt1 == void_type_node || tt2 == void_type_node)
428 target = void_type_node;
430 target = common_type (tt1, tt2);
432 target = cp_build_type_variant (target, constp, volatilep);
433 if (code1 == POINTER_TYPE)
434 t1 = build_pointer_type (target);
436 t1 = build_reference_type (target);
437 t1 = build_type_attribute_variant (t1, attributes);
439 if (TREE_CODE (target) == METHOD_TYPE)
440 t1 = build_ptrmemfunc_type (t1);
447 tree elt = common_type (TREE_TYPE (t1), TREE_TYPE (t2));
448 /* Save space: see if the result is identical to one of the args. */
449 if (elt == TREE_TYPE (t1) && TYPE_DOMAIN (t1))
450 return build_type_attribute_variant (t1, attributes);
451 if (elt == TREE_TYPE (t2) && TYPE_DOMAIN (t2))
452 return build_type_attribute_variant (t2, attributes);
453 /* Merge the element types, and have a size if either arg has one. */
454 t1 = build_cplus_array_type
455 (elt, TYPE_DOMAIN (TYPE_DOMAIN (t1) ? t1 : t2));
456 return build_type_attribute_variant (t1, attributes);
460 /* Function types: prefer the one that specified arg types.
461 If both do, merge the arg types. Also merge the return types. */
463 tree valtype = common_type (TREE_TYPE (t1), TREE_TYPE (t2));
464 tree p1 = TYPE_ARG_TYPES (t1);
465 tree p2 = TYPE_ARG_TYPES (t2);
468 /* Save space: see if the result is identical to one of the args. */
469 if (valtype == TREE_TYPE (t1) && ! p2)
470 return build_type_attribute_variant (t1, attributes);
471 if (valtype == TREE_TYPE (t2) && ! p1)
472 return build_type_attribute_variant (t2, attributes);
474 /* Simple way if one arg fails to specify argument types. */
475 if (p1 == NULL_TREE || TREE_VALUE (p1) == void_type_node)
477 rval = build_function_type (valtype, p2);
478 if ((raises = TYPE_RAISES_EXCEPTIONS (t2)))
479 rval = build_exception_variant (rval, raises);
480 return build_type_attribute_variant (rval, attributes);
482 raises = TYPE_RAISES_EXCEPTIONS (t1);
483 if (p2 == NULL_TREE || TREE_VALUE (p2) == void_type_node)
485 rval = build_function_type (valtype, p1);
487 rval = build_exception_variant (rval, raises);
488 return build_type_attribute_variant (rval, attributes);
491 rval = build_function_type (valtype, commonparms (p1, p2));
492 rval = build_exception_variant (rval, raises);
493 return build_type_attribute_variant (rval, attributes);
498 my_friendly_assert (TYPE_MAIN_VARIANT (t1) == t1
499 && TYPE_MAIN_VARIANT (t2) == t2, 306);
501 if (DERIVED_FROM_P (t1, t2) && binfo_or_else (t1, t2))
502 return build_type_attribute_variant (t1, attributes);
503 else if (binfo_or_else (t2, t1))
504 return build_type_attribute_variant (t2, attributes);
506 compiler_error ("common_type called with uncommon aggregate types");
509 if (TREE_CODE (TREE_TYPE (t1)) == TREE_CODE (TREE_TYPE (t2)))
511 /* Get this value the long way, since TYPE_METHOD_BASETYPE
512 is just the main variant of this. */
516 tree b1 = TYPE_OFFSET_BASETYPE (t1);
517 tree b2 = TYPE_OFFSET_BASETYPE (t2);
519 if (comptypes (b1, b2, 1)
520 || (DERIVED_FROM_P (b1, b2) && binfo_or_else (b1, b2)))
521 basetype = TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (t2)));
524 if (binfo_or_else (b2, b1) == NULL_TREE)
525 compiler_error ("common_type called with uncommon method types");
526 basetype = TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (t1)));
529 raises = TYPE_RAISES_EXCEPTIONS (t1);
531 /* If this was a member function type, get back to the
532 original type of type member function (i.e., without
533 the class instance variable up front. */
534 t1 = build_function_type (TREE_TYPE (t1), TREE_CHAIN (TYPE_ARG_TYPES (t1)));
535 t2 = build_function_type (TREE_TYPE (t2), TREE_CHAIN (TYPE_ARG_TYPES (t2)));
536 t3 = common_type (t1, t2);
537 t3 = build_cplus_method_type (basetype, TREE_TYPE (t3), TYPE_ARG_TYPES (t3));
538 t1 = build_exception_variant (t3, raises);
541 compiler_error ("common_type called with uncommon method types");
543 return build_type_attribute_variant (t1, attributes);
546 if (TREE_TYPE (t1) == TREE_TYPE (t2))
548 tree b1 = TYPE_OFFSET_BASETYPE (t1);
549 tree b2 = TYPE_OFFSET_BASETYPE (t2);
551 if (comptypes (b1, b2, 1)
552 || (DERIVED_FROM_P (b1, b2) && binfo_or_else (b1, b2)))
553 return build_type_attribute_variant (t2, attributes);
554 else if (binfo_or_else (b2, b1))
555 return build_type_attribute_variant (t1, attributes);
557 compiler_error ("common_type called with uncommon member types");
560 return build_type_attribute_variant (t1, attributes);
564 /* Return 1 if TYPE1 and TYPE2 raise the same exceptions. */
567 compexcepttypes (t1, t2)
570 return TYPE_RAISES_EXCEPTIONS (t1) == TYPE_RAISES_EXCEPTIONS (t2);
574 comp_array_types (cmp, t1, t2, strict)
575 register int (*cmp)();
579 tree d1 = TYPE_DOMAIN (t1);
580 tree d2 = TYPE_DOMAIN (t2);
582 /* Target types must match incl. qualifiers. */
583 if (!(TREE_TYPE (t1) == TREE_TYPE (t2)
584 || (*cmp) (TREE_TYPE (t1), TREE_TYPE (t2), strict)))
587 /* Sizes must match unless one is missing or variable. */
588 if (d1 == 0 || d2 == 0 || d1 == d2
589 || TREE_CODE (TYPE_MIN_VALUE (d1)) != INTEGER_CST
590 || TREE_CODE (TYPE_MIN_VALUE (d2)) != INTEGER_CST
591 || TREE_CODE (TYPE_MAX_VALUE (d1)) != INTEGER_CST
592 || TREE_CODE (TYPE_MAX_VALUE (d2)) != INTEGER_CST)
595 return ((TREE_INT_CST_LOW (TYPE_MIN_VALUE (d1))
596 == TREE_INT_CST_LOW (TYPE_MIN_VALUE (d2)))
597 && (TREE_INT_CST_HIGH (TYPE_MIN_VALUE (d1))
598 == TREE_INT_CST_HIGH (TYPE_MIN_VALUE (d2)))
599 && (TREE_INT_CST_LOW (TYPE_MAX_VALUE (d1))
600 == TREE_INT_CST_LOW (TYPE_MAX_VALUE (d2)))
601 && (TREE_INT_CST_HIGH (TYPE_MAX_VALUE (d1))
602 == TREE_INT_CST_HIGH (TYPE_MAX_VALUE (d2))));
605 /* Return 1 if TYPE1 and TYPE2 are compatible types for assignment
606 or various other operations. This is what ANSI C++ speaks of as
609 For C++: argument STRICT says we should be strict about this
612 2 : strict, except that if one type is a reference and
613 the other is not, compare the target type of the
614 reference to the type that's not a reference (ARM, p308).
615 This is used for checking for invalid overloading.
616 1 : strict (compared according to ANSI C)
617 This is used for checking whether two function decls match.
618 0 : <= (compared according to C++)
619 -1: <= or >= (relaxed)
621 Otherwise, pointers involving base classes and derived classes
622 can be mixed as valid: i.e. a pointer to a base class may be assigned
623 to a pointer to one of its derived classes, as per C++. A pointer to
624 a derived class may be passed as a parameter to a function expecting a
625 pointer to a base classes. These allowances do not commute. In this
626 case, TYPE1 is assumed to be the base class, and TYPE2 is assumed to
627 be the derived class. */
630 comptypes (type1, type2, strict)
634 register tree t1 = type1;
635 register tree t2 = type2;
638 /* Suppress errors caused by previously reported errors */
643 /* This should never happen. */
644 my_friendly_assert (t1 != error_mark_node, 307);
646 if (t2 == error_mark_node)
651 /* Treat an enum type as the unsigned integer type of the same width. */
653 if (TREE_CODE (t1) == ENUMERAL_TYPE)
654 t1 = type_for_size (TYPE_PRECISION (t1), 1);
655 if (TREE_CODE (t2) == ENUMERAL_TYPE)
656 t2 = type_for_size (TYPE_PRECISION (t2), 1);
662 if (TYPE_PTRMEMFUNC_P (t1))
663 t1 = TYPE_PTRMEMFUNC_FN_TYPE (t1);
664 if (TYPE_PTRMEMFUNC_P (t2))
665 t2 = TYPE_PTRMEMFUNC_FN_TYPE (t2);
667 /* Different classes of types can't be compatible. */
669 if (TREE_CODE (t1) != TREE_CODE (t2))
672 && ((TREE_CODE (t1) == REFERENCE_TYPE)
673 ^ (TREE_CODE (t2) == REFERENCE_TYPE)))
675 if (TREE_CODE (t1) == REFERENCE_TYPE)
676 return comptypes (TREE_TYPE (t1), t2, 1);
677 return comptypes (t1, TREE_TYPE (t2), 1);
685 /* Qualifiers must match. */
687 if (TYPE_READONLY (t1) != TYPE_READONLY (t2))
689 if (TYPE_VOLATILE (t1) != TYPE_VOLATILE (t2))
692 /* Allow for two different type nodes which have essentially the same
693 definition. Note that we already checked for equality of the type
694 type qualifiers (just above). */
696 if (TYPE_MAIN_VARIANT (t1) == TYPE_MAIN_VARIANT (t2))
699 /* ??? COMP_TYPE_ATTRIBUTES is currently useless for variables as each
700 attribute is its own main variant (`val' will remain 0). */
701 #ifndef COMP_TYPE_ATTRIBUTES
702 #define COMP_TYPE_ATTRIBUTES(t1,t2) 1
705 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
706 if (! (attrval = COMP_TYPE_ATTRIBUTES (t1, t2)))
709 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
712 switch (TREE_CODE (t1))
716 if (CLASSTYPE_TEMPLATE_INFO (t1) && CLASSTYPE_TEMPLATE_INFO (t2)
717 && CLASSTYPE_TI_TEMPLATE (t1) == CLASSTYPE_TI_TEMPLATE (t2))
719 int i = TREE_VEC_LENGTH (CLASSTYPE_TI_ARGS (t1));
720 tree *p1 = &TREE_VEC_ELT (CLASSTYPE_TI_ARGS (t1), 0);
721 tree *p2 = &TREE_VEC_ELT (CLASSTYPE_TI_ARGS (t2), 0);
725 if (TREE_CODE_CLASS (TREE_CODE (p1[i])) == 't')
727 if (! comptypes (p1[i], p2[i], 1))
732 if (simple_cst_equal (p1[i], p2[i]) <= 0)
743 val = (comptypes (build_pointer_type (TYPE_OFFSET_BASETYPE (t1)),
744 build_pointer_type (TYPE_OFFSET_BASETYPE (t2)), strict)
745 && comptypes (TREE_TYPE (t1), TREE_TYPE (t2), strict));
749 if (! compexcepttypes (t1, t2))
752 /* This case is anti-symmetrical!
753 One can pass a base member (or member function)
754 to something expecting a derived member (or member function),
755 but not vice-versa! */
757 val = (comptypes (TREE_TYPE (t1), TREE_TYPE (t2), strict)
758 && compparms (TYPE_ARG_TYPES (t1),
759 TYPE_ARG_TYPES (t2), strict));
773 if (TREE_CODE (t1) == RECORD_TYPE && TREE_CODE (t2) == RECORD_TYPE)
777 rval = t1 == t2 || DERIVED_FROM_P (t1, t2);
786 val = DERIVED_FROM_P (t2, t1);
793 val = comptypes (t1, t2, strict);
797 if (! compexcepttypes (t1, t2))
800 val = ((TREE_TYPE (t1) == TREE_TYPE (t2)
801 || comptypes (TREE_TYPE (t1), TREE_TYPE (t2), strict))
802 && compparms (TYPE_ARG_TYPES (t1), TYPE_ARG_TYPES (t2), strict));
806 /* Target types must match incl. qualifiers. */
807 val = comp_array_types (comptypes, t1, t2, strict);
810 case TEMPLATE_TYPE_PARM:
811 return TEMPLATE_TYPE_IDX (t1) == TEMPLATE_TYPE_IDX (t2);
814 if (TYPE_IDENTIFIER (t1) != TYPE_IDENTIFIER (t2))
816 return comptypes (TYPE_CONTEXT (t1), TYPE_CONTEXT (t2), 1);
818 return attrval == 2 && val == 1 ? 2 : val;
821 /* Return 1 if TTL and TTR are pointers to types that are equivalent,
822 ignoring their qualifiers.
824 NPTRS is the number of pointers we can strip off and keep cool.
825 This is used to permit (for aggr A, aggr B) A, B* to convert to A*,
826 but to not permit B** to convert to A**. */
829 comp_target_types (ttl, ttr, nptrs)
833 ttl = TYPE_MAIN_VARIANT (ttl);
834 ttr = TYPE_MAIN_VARIANT (ttr);
838 if (TREE_CODE (ttr) != TREE_CODE (ttl))
841 if (TREE_CODE (ttr) == POINTER_TYPE)
843 ttl = TREE_TYPE (ttl);
844 ttr = TREE_TYPE (ttr);
848 if (TREE_CODE (ttl) == VOID_TYPE
849 && TREE_CODE (ttr) != FUNCTION_TYPE
850 && TREE_CODE (ttr) != METHOD_TYPE
851 && TREE_CODE (ttr) != OFFSET_TYPE)
853 else if (TREE_CODE (ttr) == VOID_TYPE
854 && TREE_CODE (ttl) != FUNCTION_TYPE
855 && TREE_CODE (ttl) != METHOD_TYPE
856 && TREE_CODE (ttl) != OFFSET_TYPE)
858 else if (TREE_CODE (ttl) == POINTER_TYPE
859 || TREE_CODE (ttl) == ARRAY_TYPE)
861 if (comp_ptr_ttypes (ttl, ttr))
863 else if (comp_ptr_ttypes (ttr, ttl))
869 /* Const and volatile mean something different for function types,
870 so the usual checks are not appropriate. */
871 if (TREE_CODE (ttl) == FUNCTION_TYPE || TREE_CODE (ttl) == METHOD_TYPE)
872 return comp_target_types (ttl, ttr, nptrs - 1);
874 /* Make sure that the cv-quals change only in the same direction as
878 int c = TYPE_READONLY (ttl) - TYPE_READONLY (ttr);
879 int v = TYPE_VOLATILE (ttl) - TYPE_VOLATILE (ttr);
881 if ((c > 0 && v < 0) || (c < 0 && v > 0))
884 if (TYPE_MAIN_VARIANT (ttl) == TYPE_MAIN_VARIANT (ttr))
885 return (c + v < 0) ? -1 : 1;
887 t = comp_target_types (ttl, ttr, nptrs - 1);
888 if ((t == 1 && c + v >= 0) || (t == -1 && c + v <= 0))
895 if (TREE_CODE (ttr) == REFERENCE_TYPE)
896 return comp_target_types (TREE_TYPE (ttl), TREE_TYPE (ttr), nptrs);
897 if (TREE_CODE (ttr) == ARRAY_TYPE)
898 return comp_array_types (comp_target_types, ttl, ttr, 0);
899 else if (TREE_CODE (ttr) == FUNCTION_TYPE || TREE_CODE (ttr) == METHOD_TYPE)
900 if (comp_target_types (TREE_TYPE (ttl), TREE_TYPE (ttr), nptrs))
901 switch (comp_target_parms (TYPE_ARG_TYPES (ttl), TYPE_ARG_TYPES (ttr), 1))
910 my_friendly_abort (112);
916 else if (TREE_CODE (ttr) == OFFSET_TYPE)
918 /* Contravariance: we can assign a pointer to base member to a pointer
919 to derived member. Note difference from simple pointer case, where
920 we can pass a pointer to derived to a pointer to base. */
921 if (comptypes (TYPE_OFFSET_BASETYPE (ttr), TYPE_OFFSET_BASETYPE (ttl), 0))
922 return comp_target_types (TREE_TYPE (ttl), TREE_TYPE (ttr), nptrs);
923 else if (comptypes (TYPE_OFFSET_BASETYPE (ttl), TYPE_OFFSET_BASETYPE (ttr), 0)
924 && comp_target_types (TREE_TYPE (ttl), TREE_TYPE (ttr), nptrs))
927 else if (IS_AGGR_TYPE (ttl))
931 if (comptypes (build_pointer_type (ttl), build_pointer_type (ttr), 0))
933 if (comptypes (build_pointer_type (ttr), build_pointer_type (ttl), 0))
941 /* If two types share a common base type, return that basetype.
942 If there is not a unique most-derived base type, this function
943 returns ERROR_MARK_NODE. */
946 common_base_type (tt1, tt2)
949 tree best = NULL_TREE;
952 /* If one is a baseclass of another, that's good enough. */
953 if (UNIQUELY_DERIVED_FROM_P (tt1, tt2))
955 if (UNIQUELY_DERIVED_FROM_P (tt2, tt1))
958 /* Otherwise, try to find a unique baseclass of TT1
959 that is shared by TT2, and follow that down. */
960 for (i = CLASSTYPE_N_BASECLASSES (tt1)-1; i >= 0; i--)
962 tree basetype = TYPE_BINFO_BASETYPE (tt1, i);
963 tree trial = common_base_type (basetype, tt2);
966 if (trial == error_mark_node)
968 if (best == NULL_TREE)
970 else if (best != trial)
971 return error_mark_node;
976 for (i = CLASSTYPE_N_BASECLASSES (tt2)-1; i >= 0; i--)
978 tree basetype = TYPE_BINFO_BASETYPE (tt2, i);
979 tree trial = common_base_type (tt1, basetype);
982 if (trial == error_mark_node)
984 if (best == NULL_TREE)
986 else if (best != trial)
987 return error_mark_node;
993 /* Subroutines of `comptypes'. */
995 /* Return 1 if two parameter type lists PARMS1 and PARMS2
996 are equivalent in the sense that functions with those parameter types
997 can have equivalent types.
998 If either list is empty, we win.
999 Otherwise, the two lists must be equivalent, element by element.
1001 C++: See comment above about TYPE1, TYPE2, STRICT.
1002 If STRICT == 3, it means checking is strict, but do not compare
1003 default parameter values. */
1006 compparms (parms1, parms2, strict)
1007 tree parms1, parms2;
1010 register tree t1 = parms1, t2 = parms2;
1012 /* An unspecified parmlist matches any specified parmlist
1013 whose argument types don't need default promotions. */
1015 if (strict <= 0 && t1 == 0)
1016 return self_promoting_args_p (t2);
1017 if (strict < 0 && t2 == 0)
1018 return self_promoting_args_p (t1);
1022 if (t1 == 0 && t2 == 0)
1024 /* If one parmlist is shorter than the other,
1025 they fail to match, unless STRICT is <= 0. */
1026 if (t1 == 0 || t2 == 0)
1033 return t1 && TREE_PURPOSE (t1);
1035 if (! comptypes (TREE_VALUE (t2), TREE_VALUE (t1), strict))
1040 return t2 == void_list_node && TREE_PURPOSE (t1);
1041 return TREE_PURPOSE (t1) || TREE_PURPOSE (t2);
1044 t1 = TREE_CHAIN (t1);
1045 t2 = TREE_CHAIN (t2);
1049 /* This really wants return whether or not parameter type lists
1050 would make their owning functions assignment compatible or not. */
1053 comp_target_parms (parms1, parms2, strict)
1054 tree parms1, parms2;
1057 register tree t1 = parms1, t2 = parms2;
1058 int warn_contravariance = 0;
1060 /* An unspecified parmlist matches any specified parmlist
1061 whose argument types don't need default promotions.
1062 @@@ see 13.3.3 for a counterexample... */
1064 if (t1 == 0 && t2 != 0)
1066 cp_pedwarn ("ANSI C++ prohibits conversion from `(%#T)' to `(...)'",
1068 return self_promoting_args_p (t2);
1071 return self_promoting_args_p (t1);
1073 for (; t1 || t2; t1 = TREE_CHAIN (t1), t2 = TREE_CHAIN (t2))
1077 /* If one parmlist is shorter than the other,
1078 they fail to match, unless STRICT is <= 0. */
1079 if (t1 == 0 || t2 == 0)
1084 return 1 + warn_contravariance;
1085 return ((t1 && TREE_PURPOSE (t1)) + warn_contravariance);
1087 p1 = TREE_VALUE (t1);
1088 p2 = TREE_VALUE (t2);
1092 if ((TREE_CODE (p1) == POINTER_TYPE && TREE_CODE (p2) == POINTER_TYPE)
1093 || (TREE_CODE (p1) == REFERENCE_TYPE && TREE_CODE (p2) == REFERENCE_TYPE))
1096 && (TYPE_MAIN_VARIANT (TREE_TYPE (p1))
1097 == TYPE_MAIN_VARIANT (TREE_TYPE (p2))))
1100 /* The following is wrong for contravariance,
1101 but many programs depend on it. */
1102 if (TREE_TYPE (p1) == void_type_node)
1104 if (TREE_TYPE (p2) == void_type_node)
1106 warn_contravariance = 1;
1109 if (IS_AGGR_TYPE (TREE_TYPE (p1)))
1111 if (comptypes (p2, p1, 0) == 0)
1113 if (comptypes (p1, p2, 0) != 0)
1114 warn_contravariance = 1;
1121 /* Note backwards order due to contravariance. */
1122 if (comp_target_types (p2, p1, 1) == 0)
1124 if (comp_target_types (p1, p2, 1))
1126 warn_contravariance = 1;
1132 /* Target types are compatible--just make sure that if
1133 we use parameter lists, that they are ok as well. */
1134 if (TREE_CODE (p1) == FUNCTION_TYPE || TREE_CODE (p1) == METHOD_TYPE)
1135 switch (comp_target_parms (TYPE_ARG_TYPES (p1),
1136 TYPE_ARG_TYPES (p2),
1144 warn_contravariance = 1;
1147 if (TREE_PURPOSE (t1) && TREE_PURPOSE (t2))
1149 int cmp = simple_cst_equal (TREE_PURPOSE (t1), TREE_PURPOSE (t2));
1151 my_friendly_abort (114);
1156 return 1 + warn_contravariance;
1159 /* Return 1 if PARMS specifies a fixed number of parameters
1160 and none of their types is affected by default promotions. */
1163 self_promoting_args_p (parms)
1167 for (t = parms; t; t = TREE_CHAIN (t))
1169 register tree type = TREE_VALUE (t);
1171 if (TREE_CHAIN (t) == 0 && type != void_type_node)
1177 if (TYPE_MAIN_VARIANT (type) == float_type_node)
1180 if (C_PROMOTING_INTEGER_TYPE_P (type))
1186 /* Return an unsigned type the same as TYPE in other respects.
1188 C++: must make these work for type variants as well. */
1191 unsigned_type (type)
1194 tree type1 = TYPE_MAIN_VARIANT (type);
1195 if (type1 == signed_char_type_node || type1 == char_type_node)
1196 return unsigned_char_type_node;
1197 if (type1 == integer_type_node)
1198 return unsigned_type_node;
1199 if (type1 == short_integer_type_node)
1200 return short_unsigned_type_node;
1201 if (type1 == long_integer_type_node)
1202 return long_unsigned_type_node;
1203 if (type1 == long_long_integer_type_node)
1204 return long_long_unsigned_type_node;
1205 if (type1 == intDI_type_node)
1206 return unsigned_intDI_type_node;
1207 if (type1 == intSI_type_node)
1208 return unsigned_intSI_type_node;
1209 if (type1 == intHI_type_node)
1210 return unsigned_intHI_type_node;
1211 if (type1 == intQI_type_node)
1212 return unsigned_intQI_type_node;
1214 return signed_or_unsigned_type (1, type);
1217 /* Return a signed type the same as TYPE in other respects. */
1223 tree type1 = TYPE_MAIN_VARIANT (type);
1224 if (type1 == unsigned_char_type_node || type1 == char_type_node)
1225 return signed_char_type_node;
1226 if (type1 == unsigned_type_node)
1227 return integer_type_node;
1228 if (type1 == short_unsigned_type_node)
1229 return short_integer_type_node;
1230 if (type1 == long_unsigned_type_node)
1231 return long_integer_type_node;
1232 if (type1 == long_long_unsigned_type_node)
1233 return long_long_integer_type_node;
1234 if (type1 == unsigned_intDI_type_node)
1235 return intDI_type_node;
1236 if (type1 == unsigned_intSI_type_node)
1237 return intSI_type_node;
1238 if (type1 == unsigned_intHI_type_node)
1239 return intHI_type_node;
1240 if (type1 == unsigned_intQI_type_node)
1241 return intQI_type_node;
1243 return signed_or_unsigned_type (0, type);
1246 /* Return a type the same as TYPE except unsigned or
1247 signed according to UNSIGNEDP. */
1250 signed_or_unsigned_type (unsignedp, type)
1254 if (! INTEGRAL_TYPE_P (type)
1255 || TREE_UNSIGNED (type) == unsignedp)
1258 if (TYPE_PRECISION (type) == TYPE_PRECISION (signed_char_type_node))
1259 return unsignedp ? unsigned_char_type_node : signed_char_type_node;
1260 if (TYPE_PRECISION (type) == TYPE_PRECISION (integer_type_node))
1261 return unsignedp ? unsigned_type_node : integer_type_node;
1262 if (TYPE_PRECISION (type) == TYPE_PRECISION (short_integer_type_node))
1263 return unsignedp ? short_unsigned_type_node : short_integer_type_node;
1264 if (TYPE_PRECISION (type) == TYPE_PRECISION (long_integer_type_node))
1265 return unsignedp ? long_unsigned_type_node : long_integer_type_node;
1266 if (TYPE_PRECISION (type) == TYPE_PRECISION (long_long_integer_type_node))
1267 return (unsignedp ? long_long_unsigned_type_node
1268 : long_long_integer_type_node);
1272 /* Compute the value of the `sizeof' operator. */
1278 enum tree_code code = TREE_CODE (type);
1281 if (processing_template_decl)
1282 return build_min (SIZEOF_EXPR, sizetype, type);
1284 if (code == FUNCTION_TYPE)
1286 if (pedantic || warn_pointer_arith)
1287 pedwarn ("ANSI C++ forbids taking the sizeof a function type");
1288 return size_int (1);
1290 if (code == METHOD_TYPE)
1292 if (pedantic || warn_pointer_arith)
1293 pedwarn ("ANSI C++ forbids taking the sizeof a method type");
1294 return size_int (1);
1296 if (code == VOID_TYPE)
1298 if (pedantic || warn_pointer_arith)
1299 pedwarn ("ANSI C++ forbids taking the sizeof a void type");
1300 return size_int (1);
1302 if (code == ERROR_MARK)
1303 return size_int (1);
1305 /* ARM $5.3.2: ``When applied to a reference, the result is the size of the
1306 referenced object.'' */
1307 if (code == REFERENCE_TYPE)
1308 type = TREE_TYPE (type);
1310 /* We couldn't find anything in the ARM or the draft standard that says,
1311 one way or the other, if doing sizeof on something that doesn't have
1312 an object associated with it is correct or incorrect. For example, if
1313 you declare `struct S { char str[16]; };', and in your program do
1314 a `sizeof (S::str)', should we flag that as an error or should we give
1315 the size of it? Since it seems like a reasonable thing to do, we'll go
1316 with giving the value. */
1317 if (code == OFFSET_TYPE)
1318 type = TREE_TYPE (type);
1320 /* @@ This also produces an error for a signature ref.
1321 In that case we should be able to do better. */
1322 if (IS_SIGNATURE (type))
1324 error ("`sizeof' applied to a signature type");
1325 return size_int (0);
1328 if (TYPE_SIZE (complete_type (type)) == 0)
1330 cp_error ("`sizeof' applied to incomplete type `%T'", type);
1331 return size_int (0);
1334 /* Convert in case a char is more than one unit. */
1335 t = size_binop (CEIL_DIV_EXPR, TYPE_SIZE (type),
1336 size_int (TYPE_PRECISION (char_type_node)));
1337 /* size_binop does not put the constant in range, so do it now. */
1338 if (TREE_CODE (t) == INTEGER_CST && force_fit_type (t, 0))
1339 TREE_CONSTANT_OVERFLOW (t) = TREE_OVERFLOW (t) = 1;
1347 if (processing_template_decl)
1348 return build_min (SIZEOF_EXPR, sizetype, e);
1350 if (TREE_CODE (e) == COMPONENT_REF
1351 && DECL_BIT_FIELD (TREE_OPERAND (e, 1)))
1352 error ("sizeof applied to a bit-field");
1353 /* ANSI says arrays and functions are converted inside comma.
1354 But we can't really convert them in build_compound_expr
1355 because that would break commas in lvalues.
1356 So do the conversion here if operand was a comma. */
1357 if (TREE_CODE (e) == COMPOUND_EXPR
1358 && (TREE_CODE (TREE_TYPE (e)) == ARRAY_TYPE
1359 || TREE_CODE (TREE_TYPE (e)) == FUNCTION_TYPE))
1360 e = default_conversion (e);
1361 else if (TREE_CODE (e) == TREE_LIST)
1363 tree t = TREE_VALUE (e);
1366 && TREE_CODE (TREE_TYPE (t)) == FUNCTION_TYPE)
1367 || is_overloaded_fn (t)))
1368 pedwarn ("ANSI C++ forbids taking the sizeof a function type");
1370 return c_sizeof (TREE_TYPE (e));
1374 c_sizeof_nowarn (type)
1377 enum tree_code code = TREE_CODE (type);
1380 if (code == FUNCTION_TYPE
1381 || code == METHOD_TYPE
1382 || code == VOID_TYPE
1383 || code == ERROR_MARK)
1384 return size_int (1);
1385 if (code == REFERENCE_TYPE)
1386 type = TREE_TYPE (type);
1388 if (TYPE_SIZE (type) == 0)
1389 return size_int (0);
1391 /* Convert in case a char is more than one unit. */
1392 t = size_binop (CEIL_DIV_EXPR, TYPE_SIZE (type),
1393 size_int (TYPE_PRECISION (char_type_node)));
1394 force_fit_type (t, 0);
1398 /* Implement the __alignof keyword: Return the minimum required
1399 alignment of TYPE, measured in bytes. */
1405 enum tree_code code = TREE_CODE (type);
1408 if (code == FUNCTION_TYPE || code == METHOD_TYPE)
1409 return size_int (FUNCTION_BOUNDARY / BITS_PER_UNIT);
1411 if (code == VOID_TYPE || code == ERROR_MARK)
1412 return size_int (1);
1414 /* C++: this is really correct! */
1415 if (code == REFERENCE_TYPE)
1416 type = TREE_TYPE (type);
1418 /* @@ This also produces an error for a signature ref.
1419 In that case we should be able to do better. */
1420 if (IS_SIGNATURE (type))
1422 error ("`__alignof' applied to a signature type");
1423 return size_int (1);
1426 t = size_int (TYPE_ALIGN (type) / BITS_PER_UNIT);
1427 force_fit_type (t, 0);
1431 /* Perform default promotions for C data used in expressions.
1432 Arrays and functions are converted to pointers;
1433 enumeral types or short or char, to int.
1434 In addition, manifest constants symbols are replaced by their values.
1436 C++: this will automatically bash references to their target type. */
1439 decay_conversion (exp)
1442 register tree type = TREE_TYPE (exp);
1443 register enum tree_code code = TREE_CODE (type);
1445 if (code == OFFSET_TYPE)
1447 if (TREE_CODE (exp) == OFFSET_REF)
1448 return decay_conversion (resolve_offset_ref (exp));
1450 type = TREE_TYPE (type);
1451 code = TREE_CODE (type);
1453 if (type == unknown_type_node)
1455 cp_pedwarn ("assuming & on overloaded member function");
1456 return build_unary_op (ADDR_EXPR, exp, 0);
1460 if (code == REFERENCE_TYPE)
1462 exp = convert_from_reference (exp);
1463 type = TREE_TYPE (exp);
1464 code = TREE_CODE (type);
1467 /* Constants can be used directly unless they're not loadable. */
1468 if (TREE_CODE (exp) == CONST_DECL)
1469 exp = DECL_INITIAL (exp);
1470 /* Replace a nonvolatile const static variable with its value. */
1471 else if (TREE_READONLY_DECL_P (exp))
1473 exp = decl_constant_value (exp);
1474 type = TREE_TYPE (exp);
1477 /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
1478 Leave such NOP_EXPRs, since RHS is being used in non-lvalue context. */
1480 if (code == VOID_TYPE)
1482 error ("void value not ignored as it ought to be");
1483 return error_mark_node;
1485 if (code == FUNCTION_TYPE)
1487 return build_unary_op (ADDR_EXPR, exp, 0);
1489 if (code == METHOD_TYPE)
1491 cp_pedwarn ("assuming & on `%E'", exp);
1492 return build_unary_op (ADDR_EXPR, exp, 0);
1494 if (code == ARRAY_TYPE)
1499 int constp, volatilep;
1501 if (TREE_CODE (exp) == INDIRECT_REF)
1503 /* Stripping away the INDIRECT_REF is not the right
1504 thing to do for references... */
1505 tree inner = TREE_OPERAND (exp, 0);
1506 if (TREE_CODE (TREE_TYPE (inner)) == REFERENCE_TYPE)
1508 inner = build1 (CONVERT_EXPR,
1509 build_pointer_type (TREE_TYPE (TREE_TYPE (inner))),
1511 TREE_REFERENCE_EXPR (inner) = 1;
1513 return convert (build_pointer_type (TREE_TYPE (type)), inner);
1516 if (TREE_CODE (exp) == COMPOUND_EXPR)
1518 tree op1 = decay_conversion (TREE_OPERAND (exp, 1));
1519 return build (COMPOUND_EXPR, TREE_TYPE (op1),
1520 TREE_OPERAND (exp, 0), op1);
1524 && ! (TREE_CODE (exp) == CONSTRUCTOR && TREE_STATIC (exp)))
1526 error ("invalid use of non-lvalue array");
1527 return error_mark_node;
1530 constp = volatilep = 0;
1531 if (TREE_CODE_CLASS (TREE_CODE (exp)) == 'r'
1532 || TREE_CODE_CLASS (TREE_CODE (exp)) == 'd')
1534 constp = TREE_READONLY (exp);
1535 volatilep = TREE_THIS_VOLATILE (exp);
1538 restype = TREE_TYPE (type);
1539 if (TYPE_READONLY (type) || TYPE_VOLATILE (type)
1540 || constp || volatilep)
1541 restype = cp_build_type_variant (restype,
1542 TYPE_READONLY (type) || constp,
1543 TYPE_VOLATILE (type) || volatilep);
1544 ptrtype = build_pointer_type (restype);
1546 if (TREE_CODE (exp) == VAR_DECL)
1548 /* ??? This is not really quite correct
1549 in that the type of the operand of ADDR_EXPR
1550 is not the target type of the type of the ADDR_EXPR itself.
1551 Question is, can this lossage be avoided? */
1552 adr = build1 (ADDR_EXPR, ptrtype, exp);
1553 if (mark_addressable (exp) == 0)
1554 return error_mark_node;
1555 TREE_CONSTANT (adr) = staticp (exp);
1556 TREE_SIDE_EFFECTS (adr) = 0; /* Default would be, same as EXP. */
1559 /* This way is better for a COMPONENT_REF since it can
1560 simplify the offset for a component. */
1561 adr = build_unary_op (ADDR_EXPR, exp, 1);
1562 return convert (ptrtype, adr);
1569 default_conversion (exp)
1573 enum tree_code code;
1575 exp = decay_conversion (exp);
1577 type = TREE_TYPE (exp);
1578 code = TREE_CODE (type);
1580 if (INTEGRAL_CODE_P (code))
1582 tree t = type_promotes_to (type);
1584 return convert (t, exp);
1590 /* Take the address of an inline function without setting TREE_ADDRESSABLE
1594 inline_conversion (exp)
1597 if (TREE_CODE (exp) == FUNCTION_DECL)
1599 tree type = build_type_variant
1600 (TREE_TYPE (exp), TREE_READONLY (exp), TREE_THIS_VOLATILE (exp));
1601 exp = build1 (ADDR_EXPR, build_pointer_type (type), exp);
1607 build_object_ref (datum, basetype, field)
1608 tree datum, basetype, field;
1611 if (datum == error_mark_node)
1612 return error_mark_node;
1614 dtype = TREE_TYPE (datum);
1615 if (TREE_CODE (dtype) == REFERENCE_TYPE)
1616 dtype = TREE_TYPE (dtype);
1617 if (! IS_AGGR_TYPE_CODE (TREE_CODE (dtype)))
1619 cp_error ("request for member `%T::%D' in expression of non-aggregate type `%T'",
1620 basetype, field, dtype);
1621 return error_mark_node;
1623 else if (IS_SIGNATURE (basetype))
1625 warning ("signature name in scope resolution ignored");
1626 return build_component_ref (datum, field, NULL_TREE, 1);
1628 else if (is_aggr_type (basetype, 1))
1630 tree binfo = binfo_or_else (basetype, dtype);
1632 return build_x_component_ref (build_scoped_ref (datum, basetype),
1635 return error_mark_node;
1638 /* Like `build_component_ref, but uses an already found field.
1639 Must compute access for current_class_ref. Otherwise, ok. */
1642 build_component_ref_1 (datum, field, protect)
1646 return build_component_ref (datum, field, NULL_TREE, protect);
1649 /* Given a COND_EXPR in T, return it in a form that we can, for
1650 example, use as an lvalue. This code used to be in unary_complex_lvalue,
1651 but we needed it to deal with `a = (d == c) ? b : c' expressions, where
1652 we're dealing with aggregates. So, we now call this in unary_complex_lvalue,
1653 and in build_modify_expr. The case (in particular) that led to this was
1654 with CODE == ADDR_EXPR, since it's not an lvalue when we'd get it there. */
1657 rationalize_conditional_expr (code, t)
1658 enum tree_code code;
1662 build_conditional_expr (TREE_OPERAND (t, 0),
1663 build_unary_op (code, TREE_OPERAND (t, 1), 0),
1664 build_unary_op (code, TREE_OPERAND (t, 2), 0));
1667 /* Given the TYPE of an anonymous union field inside T, return the
1668 FIELD_DECL for the field. If not found return NULL_TREE. Because
1669 anonymous unions can nest, we must also search all anonymous unions
1670 that are directly reachable. */
1673 lookup_anon_field (t, type)
1678 for (field = TYPE_FIELDS (t); field; field = TREE_CHAIN (field))
1680 if (TREE_STATIC (field))
1682 if (TREE_CODE (field) != FIELD_DECL)
1685 /* If we find it directly, return the field. */
1686 if (DECL_NAME (field) == NULL_TREE
1687 && type == TREE_TYPE (field))
1692 /* Otherwise, it could be nested, search harder. */
1693 if (DECL_NAME (field) == NULL_TREE
1694 && TREE_CODE (TREE_TYPE (field)) == UNION_TYPE)
1696 tree subfield = lookup_anon_field (TREE_TYPE (field), type);
1704 /* Build a COMPONENT_REF for a given DATUM, and it's member COMPONENT.
1705 COMPONENT can be an IDENTIFIER_NODE that is the name of the member
1706 that we are interested in, or it can be a FIELD_DECL. */
1709 build_component_ref (datum, component, basetype_path, protect)
1710 tree datum, component, basetype_path;
1713 register tree basetype = TREE_TYPE (datum);
1714 register enum tree_code code;
1715 register tree field = NULL;
1718 if (processing_template_decl)
1719 return build_min_nt (COMPONENT_REF, datum, component);
1721 /* If DATUM is a COMPOUND_EXPR or COND_EXPR, move our reference
1723 switch (TREE_CODE (datum))
1727 tree value = build_component_ref (TREE_OPERAND (datum, 1), component,
1728 basetype_path, protect);
1729 return build (COMPOUND_EXPR, TREE_TYPE (value),
1730 TREE_OPERAND (datum, 0), value);
1733 return build_conditional_expr
1734 (TREE_OPERAND (datum, 0),
1735 build_component_ref (TREE_OPERAND (datum, 1), component,
1736 basetype_path, protect),
1737 build_component_ref (TREE_OPERAND (datum, 2), component,
1738 basetype_path, protect));
1741 code = TREE_CODE (basetype);
1743 if (code == REFERENCE_TYPE)
1745 datum = convert_from_reference (datum);
1746 basetype = TREE_TYPE (datum);
1747 code = TREE_CODE (basetype);
1749 if (TREE_CODE (datum) == OFFSET_REF)
1751 datum = resolve_offset_ref (datum);
1752 basetype = TREE_TYPE (datum);
1753 code = TREE_CODE (basetype);
1756 /* First, see if there is a field or component with name COMPONENT. */
1757 if (TREE_CODE (component) == TREE_LIST)
1759 my_friendly_assert (!(TREE_CHAIN (component) == NULL_TREE
1760 && DECL_CHAIN (TREE_VALUE (component)) == NULL_TREE), 309);
1761 return build (COMPONENT_REF, TREE_TYPE (component), datum, component);
1764 if (! IS_AGGR_TYPE_CODE (code))
1766 if (code != ERROR_MARK)
1767 cp_error ("request for member `%D' in `%E', which is of non-aggregate type `%T'",
1768 component, datum, basetype);
1769 return error_mark_node;
1772 if (TYPE_SIZE (complete_type (basetype)) == 0)
1774 incomplete_type_error (0, basetype);
1775 return error_mark_node;
1778 if (TREE_CODE (component) == BIT_NOT_EXPR)
1780 if (TYPE_IDENTIFIER (basetype) != TREE_OPERAND (component, 0))
1782 cp_error ("destructor specifier `%T::~%T' must have matching names",
1783 basetype, TREE_OPERAND (component, 0));
1784 return error_mark_node;
1786 if (! TYPE_HAS_DESTRUCTOR (basetype))
1788 cp_error ("type `%T' has no destructor", basetype);
1789 return error_mark_node;
1791 return TREE_VEC_ELT (CLASSTYPE_METHOD_VEC (basetype), 1);
1794 /* Look up component name in the structure type definition. */
1795 if (CLASSTYPE_VFIELD (basetype)
1796 && DECL_NAME (CLASSTYPE_VFIELD (basetype)) == component)
1797 /* Special-case this because if we use normal lookups in an ambiguous
1798 hierarchy, the compiler will abort (because vptr lookups are
1799 not supposed to be ambiguous. */
1800 field = CLASSTYPE_VFIELD (basetype);
1801 else if (TREE_CODE (component) == FIELD_DECL
1802 || TREE_CODE (component) == TYPE_DECL)
1808 tree name = component;
1809 if (TREE_CODE (component) == VAR_DECL)
1810 name = DECL_NAME (component);
1811 if (basetype_path == NULL_TREE)
1812 basetype_path = TYPE_BINFO (basetype);
1813 field = lookup_field (basetype_path, name,
1814 protect && !VFIELD_NAME_P (name), 0);
1815 if (field == error_mark_node)
1816 return error_mark_node;
1818 if (field == NULL_TREE)
1820 /* Not found as a data field, look for it as a method. If found,
1821 then if this is the only possible one, return it, else
1822 report ambiguity error. */
1823 tree fndecls = lookup_fnfields (basetype_path, name, 1);
1824 if (fndecls == error_mark_node)
1825 return error_mark_node;
1828 if (TREE_CHAIN (fndecls) == NULL_TREE
1829 && DECL_CHAIN (TREE_VALUE (fndecls)) == NULL_TREE)
1831 tree access, fndecl;
1833 /* Unique, so use this one now. */
1834 basetype = TREE_PURPOSE (fndecls);
1835 fndecl = TREE_VALUE (fndecls);
1836 access = compute_access (TREE_PURPOSE (fndecls), fndecl);
1837 if (access == access_public_node)
1839 if (DECL_VINDEX (fndecl)
1840 && ! resolves_to_fixed_type_p (datum, 0))
1842 tree addr = build_unary_op (ADDR_EXPR, datum, 0);
1843 tree fntype = TREE_TYPE (fndecl);
1845 addr = convert_pointer_to (DECL_CONTEXT (fndecl), addr);
1846 datum = build_indirect_ref (addr, NULL_PTR);
1847 my_friendly_assert (datum != error_mark_node, 310);
1848 fndecl = build_vfn_ref (&addr, datum, DECL_VINDEX (fndecl));
1849 TREE_TYPE (fndecl) = build_pointer_type (fntype);
1852 return build (OFFSET_REF, TREE_TYPE (fndecl), datum, fndecl);
1854 if (access == access_protected_node)
1855 cp_error ("member function `%D' is protected", fndecl);
1857 cp_error ("member function `%D' is private", fndecl);
1858 return error_mark_node;
1862 /* Just act like build_offset_ref, since the object does
1863 not matter unless we're actually calling the function. */
1866 t = build_tree_list (error_mark_node, fndecls);
1867 TREE_TYPE (t) = build_offset_type (basetype,
1873 cp_error ("`%#T' has no member named `%D'", basetype, name);
1874 return error_mark_node;
1876 else if (TREE_TYPE (field) == error_mark_node)
1877 return error_mark_node;
1879 if (TREE_CODE (field) != FIELD_DECL)
1881 if (TREE_CODE (field) == TYPE_DECL)
1883 cp_error ("invalid use of type decl `%#D' as expression", field);
1884 return error_mark_node;
1886 else if (DECL_RTL (field) != 0)
1889 TREE_USED (field) = 1;
1894 /* See if we have to do any conversions so that we pick up the field from the
1896 if (DECL_FIELD_CONTEXT (field) != basetype)
1898 tree context = DECL_FIELD_CONTEXT (field);
1899 tree base = context;
1900 while (base != basetype && TYPE_NAME (base)
1901 && ANON_AGGRNAME_P (TYPE_IDENTIFIER (base)))
1903 base = TYPE_CONTEXT (base);
1906 /* Handle base classes here... */
1907 if (base != basetype && TYPE_USES_COMPLEX_INHERITANCE (basetype))
1909 tree addr = build_unary_op (ADDR_EXPR, datum, 0);
1910 if (integer_zerop (addr))
1912 error ("invalid reference to NULL ptr, use ptr-to-member instead");
1913 return error_mark_node;
1915 if (VBASE_NAME_P (DECL_NAME (field)))
1917 /* It doesn't matter which vbase pointer we grab, just
1918 find one of them. */
1919 tree binfo = get_binfo (base,
1920 TREE_TYPE (TREE_TYPE (addr)), 0);
1921 addr = convert_pointer_to_real (binfo, addr);
1924 addr = convert_pointer_to (base, addr);
1925 datum = build_indirect_ref (addr, NULL_PTR);
1926 my_friendly_assert (datum != error_mark_node, 311);
1930 /* Handle things from anon unions here... */
1931 if (TYPE_NAME (context) && ANON_AGGRNAME_P (TYPE_IDENTIFIER (context)))
1933 tree subfield = lookup_anon_field (basetype, context);
1934 tree subdatum = build_component_ref (datum, subfield,
1935 basetype_path, protect);
1936 return build_component_ref (subdatum, field, basetype_path, protect);
1940 ref = fold (build (COMPONENT_REF, TREE_TYPE (field),
1941 break_out_cleanups (datum), field));
1943 if (TREE_READONLY (datum) || TREE_READONLY (field))
1944 TREE_READONLY (ref) = 1;
1945 if (TREE_THIS_VOLATILE (datum) || TREE_THIS_VOLATILE (field))
1946 TREE_THIS_VOLATILE (ref) = 1;
1947 if (DECL_MUTABLE_P (field))
1948 TREE_READONLY (ref) = 0;
1953 /* Variant of build_component_ref for use in expressions, which should
1954 never have REFERENCE_TYPE. */
1957 build_x_component_ref (datum, component, basetype_path, protect)
1958 tree datum, component, basetype_path;
1961 tree t = build_component_ref (datum, component, basetype_path, protect);
1963 if (! processing_template_decl)
1964 t = convert_from_reference (t);
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 (processing_template_decl)
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, type;
1999 if (ptr == error_mark_node)
2000 return error_mark_node;
2002 pointer = (TREE_CODE (TREE_TYPE (ptr)) == REFERENCE_TYPE
2003 ? ptr : default_conversion (ptr));
2004 type = TREE_TYPE (pointer);
2006 if (ptr == current_class_ptr)
2007 return current_class_ref;
2009 if (IS_AGGR_TYPE (type))
2011 ptr = build_expr_type_conversion (WANT_POINTER, pointer, 1);
2016 type = TREE_TYPE (pointer);
2020 if (TREE_CODE (type) == POINTER_TYPE || TREE_CODE (type) == REFERENCE_TYPE)
2022 if (TREE_CODE (pointer) == ADDR_EXPR
2023 && (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_OPERAND (pointer, 0)))
2024 == TYPE_MAIN_VARIANT (TREE_TYPE (type)))
2025 && (TREE_READONLY (TREE_OPERAND (pointer, 0))
2026 == TYPE_READONLY (TREE_TYPE (type)))
2027 && (TREE_THIS_VOLATILE (TREE_OPERAND (pointer, 0))
2028 == TYPE_VOLATILE (TREE_TYPE (type))))
2029 return TREE_OPERAND (pointer, 0);
2032 tree t = TREE_TYPE (type);
2033 register tree ref = build1 (INDIRECT_REF,
2034 TYPE_MAIN_VARIANT (t), pointer);
2036 TREE_READONLY (ref) = TYPE_READONLY (t);
2037 TREE_THIS_VOLATILE (ref) = TYPE_VOLATILE (t);
2038 TREE_SIDE_EFFECTS (ref)
2039 = TYPE_VOLATILE (t) || TREE_SIDE_EFFECTS (pointer);
2043 /* `pointer' won't be an error_mark_node if we were given a
2044 pointer to member, so it's cool to check for this here. */
2045 else if (TYPE_PTRMEMFUNC_P (type))
2046 error ("invalid use of `%s' on pointer to member function", errorstring);
2047 else if (TREE_CODE (type) == RECORD_TYPE
2048 && (IS_SIGNATURE_POINTER (type) || IS_SIGNATURE_REFERENCE (type)))
2049 error ("cannot dereference signature pointer/reference");
2050 else if (pointer != error_mark_node)
2053 error ("invalid type argument of `%s'", errorstring);
2055 error ("invalid type argument");
2057 return error_mark_node;
2060 /* This handles expressions of the form "a[i]", which denotes
2063 This is logically equivalent in C to *(a+i), but we may do it differently.
2064 If A is a variable or a member, we generate a primitive ARRAY_REF.
2065 This avoids forcing the array out of registers, and can work on
2066 arrays that are not lvalues (for example, members of structures returned
2069 If INDEX is of some user-defined type, it must be converted to
2070 integer type. Otherwise, to make a compatible PLUS_EXPR, it
2071 will inherit the type of the array, which will be some pointer type. */
2074 build_x_array_ref (array, idx)
2077 tree rval = build_opfncall (ARRAY_REF, LOOKUP_NORMAL, array, idx, NULL_TREE);
2080 return build_array_ref (array, idx);
2084 build_array_ref (array, idx)
2089 error ("subscript missing in array reference");
2090 return error_mark_node;
2093 if (TREE_TYPE (array) == error_mark_node
2094 || TREE_TYPE (idx) == error_mark_node)
2095 return error_mark_node;
2097 if (TREE_CODE (TREE_TYPE (array)) == ARRAY_TYPE
2098 && TREE_CODE (array) != INDIRECT_REF)
2102 /* Subscripting with type char is likely to lose
2103 on a machine where chars are signed.
2104 So warn on any machine, but optionally.
2105 Don't warn for unsigned char since that type is safe.
2106 Don't warn for signed char because anyone who uses that
2107 must have done so deliberately. */
2108 if (warn_char_subscripts
2109 && TYPE_MAIN_VARIANT (TREE_TYPE (idx)) == char_type_node)
2110 warning ("array subscript has type `char'");
2112 /* Apply default promotions *after* noticing character types. */
2113 idx = default_conversion (idx);
2115 if (TREE_CODE (TREE_TYPE (idx)) != INTEGER_TYPE)
2117 error ("array subscript is not an integer");
2118 return error_mark_node;
2121 /* An array that is indexed by a non-constant
2122 cannot be stored in a register; we must be able to do
2123 address arithmetic on its address.
2124 Likewise an array of elements of variable size. */
2125 if (TREE_CODE (idx) != INTEGER_CST
2126 || (TYPE_SIZE (TREE_TYPE (TREE_TYPE (array))) != 0
2127 && TREE_CODE (TYPE_SIZE (TREE_TYPE (TREE_TYPE (array)))) != INTEGER_CST))
2129 if (mark_addressable (array) == 0)
2130 return error_mark_node;
2132 /* An array that is indexed by a constant value which is not within
2133 the array bounds cannot be stored in a register either; because we
2134 would get a crash in store_bit_field/extract_bit_field when trying
2135 to access a non-existent part of the register. */
2136 if (TREE_CODE (idx) == INTEGER_CST
2137 && TYPE_VALUES (TREE_TYPE (array))
2138 && ! int_fits_type_p (idx, TYPE_VALUES (TREE_TYPE (array))))
2140 if (mark_addressable (array) == 0)
2141 return error_mark_node;
2144 if (pedantic && !lvalue_p (array))
2145 pedwarn ("ANSI C++ forbids subscripting non-lvalue array");
2147 /* Note in C++ it is valid to subscript a `register' array, since
2148 it is valid to take the address of something with that
2149 storage specification. */
2153 while (TREE_CODE (foo) == COMPONENT_REF)
2154 foo = TREE_OPERAND (foo, 0);
2155 if (TREE_CODE (foo) == VAR_DECL && DECL_REGISTER (foo))
2156 warning ("subscripting array declared `register'");
2159 type = TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (array)));
2160 rval = build (ARRAY_REF, type, array, idx);
2161 /* Array ref is const/volatile if the array elements are
2162 or if the array is.. */
2163 TREE_READONLY (rval)
2164 |= (TYPE_READONLY (TREE_TYPE (TREE_TYPE (array)))
2165 | TREE_READONLY (array));
2166 TREE_SIDE_EFFECTS (rval)
2167 |= (TYPE_VOLATILE (TREE_TYPE (TREE_TYPE (array)))
2168 | TREE_SIDE_EFFECTS (array));
2169 TREE_THIS_VOLATILE (rval)
2170 |= (TYPE_VOLATILE (TREE_TYPE (TREE_TYPE (array)))
2171 /* This was added by rms on 16 Nov 91.
2172 It fixes vol struct foo *a; a->elts[1]
2173 in an inline function.
2174 Hope it doesn't break something else. */
2175 | TREE_THIS_VOLATILE (array));
2176 return require_complete_type (fold (rval));
2180 tree ar = default_conversion (array);
2181 tree ind = default_conversion (idx);
2183 /* Put the integer in IND to simplify error checking. */
2184 if (TREE_CODE (TREE_TYPE (ar)) == INTEGER_TYPE)
2191 if (ar == error_mark_node)
2194 if (TREE_CODE (TREE_TYPE (ar)) != POINTER_TYPE)
2196 error ("subscripted value is neither array nor pointer");
2197 return error_mark_node;
2199 if (TREE_CODE (TREE_TYPE (ind)) != INTEGER_TYPE)
2201 error ("array subscript is not an integer");
2202 return error_mark_node;
2205 return build_indirect_ref (build_binary_op_nodefault (PLUS_EXPR, ar, ind, PLUS_EXPR),
2210 /* Build a function call to function FUNCTION with parameters PARAMS.
2211 PARAMS is a list--a chain of TREE_LIST nodes--in which the
2212 TREE_VALUE of each node is a parameter-expression. The PARAMS do
2213 not include any object pointer that may be required. FUNCTION's
2214 data type may be a function type or a pointer-to-function.
2216 For C++: If FUNCTION's data type is a TREE_LIST, then the tree list
2217 is the list of possible methods that FUNCTION could conceivably
2218 be. If the list of methods comes from a class, then it will be
2219 a list of lists (where each element is associated with the class
2220 that produced it), otherwise it will be a simple list (for
2221 functions overloaded in global scope).
2223 In the first case, TREE_VALUE (function) is the head of one of those
2224 lists, and TREE_PURPOSE is the name of the function.
2226 In the second case, TREE_PURPOSE (function) is the function's
2229 DECL is the class instance variable, usually CURRENT_CLASS_REF.
2231 When calling a TEMPLATE_DECL, we don't require a complete return
2235 build_x_function_call (function, params, decl)
2236 tree function, params, decl;
2241 if (function == error_mark_node)
2242 return error_mark_node;
2244 if (processing_template_decl)
2245 return build_min_nt (CALL_EXPR, function, params, NULL_TREE);
2247 type = TREE_TYPE (function);
2249 if (TREE_CODE (type) == OFFSET_TYPE
2250 && TREE_TYPE (type) == unknown_type_node
2251 && TREE_CODE (function) == TREE_LIST
2252 && TREE_CHAIN (function) == NULL_TREE)
2254 /* Undo (Foo:bar)()... */
2255 type = TYPE_OFFSET_BASETYPE (type);
2256 function = TREE_VALUE (function);
2257 my_friendly_assert (TREE_CODE (function) == TREE_LIST, 999);
2258 my_friendly_assert (TREE_CHAIN (function) == NULL_TREE, 999);
2259 function = TREE_VALUE (function);
2260 my_friendly_assert (TREE_CODE (function) == FUNCTION_DECL, 999);
2261 function = DECL_NAME (function);
2262 return build_method_call (decl, function, params, TYPE_BINFO (type), LOOKUP_NORMAL);
2265 is_method = ((TREE_CODE (function) == TREE_LIST
2266 && current_class_type != NULL_TREE
2267 && IDENTIFIER_CLASS_VALUE (TREE_PURPOSE (function)) == function)
2268 || TREE_CODE (function) == IDENTIFIER_NODE
2269 || TREE_CODE (type) == METHOD_TYPE
2270 || TYPE_PTRMEMFUNC_P (type));
2272 if (TREE_CODE (function) == FUNCTION_DECL
2273 && DECL_STATIC_FUNCTION_P (function))
2274 return build_member_call
2275 (DECL_CONTEXT (function), DECL_NAME (function), params);
2277 /* Handle methods, friends, and overloaded functions, respectively. */
2280 tree basetype = NULL_TREE;
2282 if (TREE_CODE (function) == FUNCTION_DECL)
2284 basetype = DECL_CLASS_CONTEXT (function);
2286 if (DECL_NAME (function))
2287 function = DECL_NAME (function);
2289 function = TYPE_IDENTIFIER (DECL_CLASS_CONTEXT (function));
2291 else if (TREE_CODE (function) == TREE_LIST)
2293 my_friendly_assert (TREE_CODE (TREE_VALUE (function)) == FUNCTION_DECL, 312);
2294 basetype = DECL_CLASS_CONTEXT (TREE_VALUE (function));
2295 function = TREE_PURPOSE (function);
2297 else if (TREE_CODE (function) != IDENTIFIER_NODE)
2299 if (TREE_CODE (function) == OFFSET_REF)
2301 if (TREE_OPERAND (function, 0))
2302 decl = TREE_OPERAND (function, 0);
2304 /* Call via a pointer to member function. */
2305 if (decl == NULL_TREE)
2307 error ("pointer to member function called, but not in class scope");
2308 return error_mark_node;
2310 /* What other type of POINTER_TYPE could this be? */
2311 if (TREE_CODE (TREE_TYPE (function)) != POINTER_TYPE
2312 && ! TYPE_PTRMEMFUNC_P (TREE_TYPE (function))
2313 && TREE_CODE (function) != OFFSET_REF)
2314 function = build (OFFSET_REF, TREE_TYPE (type), NULL_TREE, function);
2318 /* this is an abbreviated method call.
2319 must go through here in case it is a virtual function.
2320 @@ Perhaps this could be optimized. */
2322 if (basetype && (! current_class_type
2323 || ! DERIVED_FROM_P (basetype, current_class_type)))
2324 return build_member_call (basetype, function, params);
2326 if (decl == NULL_TREE)
2328 if (current_class_type == NULL_TREE)
2330 error ("object missing in call to method `%s'",
2331 IDENTIFIER_POINTER (function));
2332 return error_mark_node;
2334 /* Yow: call from a static member function. */
2335 decl = build1 (NOP_EXPR, build_pointer_type (current_class_type),
2337 decl = build_indirect_ref (decl, NULL_PTR);
2340 return build_method_call (decl, function, params,
2341 NULL_TREE, LOOKUP_NORMAL);
2343 else if (TREE_CODE (function) == COMPONENT_REF
2344 && type == unknown_type_node)
2346 /* Should we undo what was done in build_component_ref? */
2347 if (TREE_CODE (TREE_PURPOSE (TREE_OPERAND (function, 1))) == TREE_VEC)
2348 /* Get the name that build_component_ref hid. */
2349 function = DECL_NAME (TREE_VALUE (TREE_OPERAND (function, 1)));
2351 function = TREE_PURPOSE (TREE_OPERAND (function, 1));
2352 return build_method_call (decl, function, params,
2353 NULL_TREE, LOOKUP_NORMAL);
2355 else if (TREE_CODE (function) == TREE_LIST)
2357 if (TREE_VALUE (function) == NULL_TREE)
2359 cp_error ("function `%D' declared overloaded, but no definitions appear with which to resolve it?!?",
2360 TREE_PURPOSE (function));
2361 return error_mark_node;
2365 tree val = TREE_VALUE (function);
2367 if (flag_ansi_overloading)
2368 return build_new_function_call (function, params, NULL_TREE);
2370 if (TREE_CODE (val) == TEMPLATE_DECL)
2371 return build_overload_call_real
2372 (function, params, LOOKUP_COMPLAIN, (struct candidate *)0, 0);
2373 else if (DECL_CHAIN (val) != NULL_TREE)
2374 return build_overload_call
2375 (function, params, LOOKUP_COMPLAIN);
2377 my_friendly_abort (360);
2382 if (TREE_CODE (function) == OFFSET_REF)
2384 /* If the component is a data element (or a virtual function), we play
2385 games here to make things work. */
2388 if (TREE_OPERAND (function, 0))
2389 decl = TREE_OPERAND (function, 0);
2391 decl = current_class_ref;
2393 decl_addr = build_unary_op (ADDR_EXPR, decl, 0);
2394 function = get_member_function_from_ptrfunc (&decl_addr,
2395 TREE_OPERAND (function, 1));
2396 params = tree_cons (NULL_TREE, decl_addr, params);
2397 return build_function_call (function, params);
2400 type = TREE_TYPE (function);
2401 if (type != error_mark_node)
2403 if (TREE_CODE (type) == REFERENCE_TYPE)
2404 type = TREE_TYPE (type);
2406 if (IS_AGGR_TYPE (type))
2407 return build_opfncall (CALL_EXPR, LOOKUP_NORMAL, function, params, NULL_TREE);
2412 tree fntype = TREE_TYPE (function);
2415 /* Explicitly named method? */
2416 if (TREE_CODE (function) == FUNCTION_DECL)
2417 ctypeptr = build_pointer_type (DECL_CLASS_CONTEXT (function));
2418 /* Expression with ptr-to-method type? It could either be a plain
2419 usage, or it might be a case where the ptr-to-method is being
2420 passed in as an argument. */
2421 else if (TYPE_PTRMEMFUNC_P (fntype))
2423 tree rec = TYPE_METHOD_BASETYPE (TREE_TYPE (TYPE_PTRMEMFUNC_FN_TYPE (fntype)));
2424 ctypeptr = build_pointer_type (rec);
2426 /* Unexpected node type? */
2428 my_friendly_abort (116);
2429 if (decl == NULL_TREE)
2431 if (current_function_decl
2432 && DECL_STATIC_FUNCTION_P (current_function_decl))
2433 error ("invalid call to member function needing `this' in static member function scope");
2435 error ("pointer to member function called, but not in class scope");
2436 return error_mark_node;
2438 if (TREE_CODE (TREE_TYPE (decl)) != POINTER_TYPE
2439 && ! TYPE_PTRMEMFUNC_P (TREE_TYPE (decl)))
2441 decl = build_unary_op (ADDR_EXPR, decl, 0);
2442 decl = convert_pointer_to (TREE_TYPE (ctypeptr), decl);
2445 decl = build_c_cast (ctypeptr, decl);
2446 params = tree_cons (NULL_TREE, decl, params);
2449 return build_function_call (function, params);
2452 /* Resolve a pointer to member function. INSTANCE is the object
2453 instance to use, if the member points to a virtual member. */
2456 get_member_function_from_ptrfunc (instance_ptrptr, function)
2457 tree *instance_ptrptr;
2460 if (TREE_CODE (function) == OFFSET_REF)
2462 function = TREE_OPERAND (function, 1);
2465 if (TYPE_PTRMEMFUNC_P (TREE_TYPE (function)))
2467 tree fntype, idx, e1, delta, delta2, e2, e3, aref, vtbl;
2470 tree instance_ptr = *instance_ptrptr;
2472 if (TREE_SIDE_EFFECTS (instance_ptr))
2473 instance_ptr = save_expr (instance_ptr);
2475 if (TREE_SIDE_EFFECTS (function))
2476 function = save_expr (function);
2478 fntype = TYPE_PTRMEMFUNC_FN_TYPE (TREE_TYPE (function));
2479 idx = save_expr (build_component_ref (function,
2482 e1 = fold (build (GT_EXPR, boolean_type_node, idx,
2483 convert (delta_type_node, integer_zero_node)));
2484 delta = convert (ptrdiff_type_node,
2485 build_component_ref (function, delta_identifier, NULL_TREE, 0));
2486 delta2 = DELTA2_FROM_PTRMEMFUNC (function);
2488 /* convert down to the right base, before using the instance. */
2490 = convert_pointer_to_real (TYPE_METHOD_BASETYPE (TREE_TYPE (fntype)),
2492 if (instance == error_mark_node && instance_ptr != error_mark_node)
2495 vtbl = convert_pointer_to (ptr_type_node, instance);
2498 build_pointer_type (build_pointer_type (vtable_entry_type)),
2499 vtbl, convert (ptrdiff_type_node, delta2));
2500 vtbl = build_indirect_ref (vtbl, NULL_PTR);
2501 aref = build_array_ref (vtbl, build_binary_op (MINUS_EXPR,
2503 integer_one_node, 1));
2504 if (! flag_vtable_thunks)
2506 aref = save_expr (aref);
2508 delta = build_binary_op (PLUS_EXPR,
2509 build_conditional_expr (e1, build_component_ref (aref, delta_identifier, NULL_TREE, 0), integer_zero_node),
2513 *instance_ptrptr = build (PLUS_EXPR, TREE_TYPE (instance_ptr),
2514 instance_ptr, delta);
2515 if (flag_vtable_thunks)
2518 e2 = build_component_ref (aref, pfn_identifier, NULL_TREE, 0);
2520 e3 = PFN_FROM_PTRMEMFUNC (function);
2521 TREE_TYPE (e2) = TREE_TYPE (e3);
2522 e1 = build_conditional_expr (e1, e2, e3);
2524 if (instance_ptr == error_mark_node
2525 && TREE_CODE (e1) != ADDR_EXPR
2526 && TREE_CODE (TREE_OPERAND (e1, 0)) != FUNCTION_DECL)
2527 cp_error ("object missing in `%E'", function);
2531 /* Make sure this doesn't get evaluated first inside one of the
2532 branches of the COND_EXPR. */
2533 if (TREE_CODE (instance_ptr) == SAVE_EXPR)
2534 function = build (COMPOUND_EXPR, TREE_TYPE (function),
2535 instance_ptr, function);
2541 build_function_call_real (function, params, require_complete, flags)
2542 tree function, params;
2543 int require_complete, flags;
2545 register tree fntype, fndecl;
2546 register tree value_type;
2547 register tree coerced_params;
2548 tree name = NULL_TREE, assembler_name = NULL_TREE;
2551 /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
2552 Strip such NOP_EXPRs, since FUNCTION is used in non-lvalue context. */
2553 if (TREE_CODE (function) == NOP_EXPR
2554 && TREE_TYPE (function) == TREE_TYPE (TREE_OPERAND (function, 0)))
2555 function = TREE_OPERAND (function, 0);
2557 if (TREE_CODE (function) == FUNCTION_DECL)
2559 name = DECL_NAME (function);
2560 assembler_name = DECL_ASSEMBLER_NAME (function);
2562 GNU_xref_call (current_function_decl,
2563 IDENTIFIER_POINTER (name ? name
2564 : TYPE_IDENTIFIER (DECL_CLASS_CONTEXT (function))));
2565 mark_used (function);
2568 /* Convert anything with function type to a pointer-to-function. */
2571 && IDENTIFIER_LENGTH (name) == 4
2572 && ! strcmp (IDENTIFIER_POINTER (name), "main")
2573 && DECL_CONTEXT (function) == NULL_TREE)
2575 pedwarn ("ANSI C++ forbids calling `main' from within program");
2578 if (pedantic && DECL_THIS_INLINE (function) && ! DECL_INITIAL (function)
2579 && ! DECL_ARTIFICIAL (function)
2580 && ! DECL_PENDING_INLINE_INFO (function))
2581 cp_pedwarn ("inline function `%#D' called before definition",
2584 /* Differs from default_conversion by not setting TREE_ADDRESSABLE
2585 (because calling an inline function does not mean the function
2586 needs to be separately compiled). */
2588 if (DECL_INLINE (function))
2589 function = inline_conversion (function);
2591 function = build_addr_func (function);
2597 function = build_addr_func (function);
2600 if (function == error_mark_node)
2601 return error_mark_node;
2603 fntype = TREE_TYPE (function);
2605 if (TYPE_PTRMEMFUNC_P (fntype))
2607 cp_error ("must use .* or ->* to call pointer-to-member function in `%E (...)'",
2609 return error_mark_node;
2612 is_method = (TREE_CODE (fntype) == POINTER_TYPE
2613 && TREE_CODE (TREE_TYPE (fntype)) == METHOD_TYPE);
2615 if (!((TREE_CODE (fntype) == POINTER_TYPE
2616 && TREE_CODE (TREE_TYPE (fntype)) == FUNCTION_TYPE)
2619 cp_error ("`%E' cannot be used as a function", function);
2620 return error_mark_node;
2623 /* fntype now gets the type of function pointed to. */
2624 fntype = TREE_TYPE (fntype);
2626 /* Convert the parameters to the types declared in the
2627 function prototype, or apply default promotions. */
2629 if (flags & LOOKUP_COMPLAIN)
2630 coerced_params = convert_arguments (NULL_TREE, TYPE_ARG_TYPES (fntype),
2631 params, fndecl, LOOKUP_NORMAL);
2633 coerced_params = convert_arguments (NULL_TREE, TYPE_ARG_TYPES (fntype),
2636 if (coerced_params == error_mark_node)
2637 if (flags & LOOKUP_SPECULATIVELY)
2640 return error_mark_node;
2642 /* Check for errors in format strings. */
2644 if (warn_format && (name || assembler_name))
2645 check_function_format (name, assembler_name, coerced_params);
2647 /* Recognize certain built-in functions so we can make tree-codes
2648 other than CALL_EXPR. We do this when it enables fold-const.c
2649 to do something useful. */
2651 if (TREE_CODE (function) == ADDR_EXPR
2652 && TREE_CODE (TREE_OPERAND (function, 0)) == FUNCTION_DECL
2653 && DECL_BUILT_IN (TREE_OPERAND (function, 0)))
2654 switch (DECL_FUNCTION_CODE (TREE_OPERAND (function, 0)))
2659 if (coerced_params == 0)
2660 return integer_zero_node;
2661 return build_unary_op (ABS_EXPR, TREE_VALUE (coerced_params), 0);
2665 value_type = TREE_TYPE (fntype) ? TREE_TYPE (fntype) : void_type_node;
2667 register tree result
2668 = build_call (function, value_type, coerced_params);
2670 if (require_complete)
2672 if (value_type == void_type_node)
2674 result = require_complete_type (result);
2676 if (IS_AGGR_TYPE (value_type))
2677 result = build_cplus_new (value_type, result);
2678 return convert_from_reference (result);
2683 build_function_call (function, params)
2684 tree function, params;
2686 return build_function_call_real (function, params, 1, LOOKUP_NORMAL);
2689 /* Convert the actual parameter expressions in the list VALUES
2690 to the types in the list TYPELIST.
2691 If parmdecls is exhausted, or when an element has NULL as its type,
2692 perform the default conversions.
2694 RETURN_LOC is the location of the return value, if known, NULL_TREE
2695 otherwise. This is useful in the case where we can avoid creating
2696 a temporary variable in the case where we can initialize the return
2697 value directly. If we are not eliding constructors, then we set this
2698 to NULL_TREE to avoid this avoidance.
2700 NAME is an IDENTIFIER_NODE or 0. It is used only for error messages.
2702 This is also where warnings about wrong number of args are generated.
2704 Return a list of expressions for the parameters as converted.
2706 Both VALUES and the returned value are chains of TREE_LIST nodes
2707 with the elements of the list in the TREE_VALUE slots of those nodes.
2709 In C++, unspecified trailing parameters can be filled in with their
2710 default arguments, if such were specified. Do so here. */
2713 convert_arguments (return_loc, typelist, values, fndecl, flags)
2714 tree return_loc, typelist, values, fndecl;
2717 register tree typetail, valtail;
2718 register tree result = NULL_TREE;
2722 if (! flag_elide_constructors)
2725 /* Argument passing is always copy-initialization. */
2726 flags |= LOOKUP_ONLYCONVERTING;
2730 if (TREE_CODE (TREE_TYPE (fndecl)) == METHOD_TYPE)
2732 if (DECL_NAME (fndecl) == NULL_TREE
2733 || IDENTIFIER_HAS_TYPE_VALUE (DECL_NAME (fndecl)))
2734 called_thing = "constructor";
2736 called_thing = "member function";
2739 called_thing = "function";
2742 for (valtail = values, typetail = typelist;
2744 valtail = TREE_CHAIN (valtail), i++)
2746 register tree type = typetail ? TREE_VALUE (typetail) : 0;
2747 register tree val = TREE_VALUE (valtail);
2749 if (val == error_mark_node)
2750 return error_mark_node;
2752 if (type == void_type_node)
2756 cp_error_at ("too many arguments to %s `%+D'", called_thing,
2758 error ("at this point in file");
2761 error ("too many arguments to function");
2762 /* In case anybody wants to know if this argument
2765 TREE_TYPE (tree_last (result)) = error_mark_node;
2769 /* The tree type of the parameter being passed may not yet be
2770 known. In this case, its type is TYPE_UNKNOWN, and will
2771 be instantiated by the type given by TYPE. If TYPE
2772 is also NULL, the tree type of VAL is ERROR_MARK_NODE. */
2773 if (type && type_unknown_p (val))
2774 val = require_instantiated_type (type, val, integer_zero_node);
2775 else if (type_unknown_p (val))
2777 /* Strip the `&' from an overloaded FUNCTION_DECL. */
2778 if (TREE_CODE (val) == ADDR_EXPR)
2779 val = TREE_OPERAND (val, 0);
2780 if (TREE_CODE (val) == TREE_LIST
2781 && TREE_CHAIN (val) == NULL_TREE
2782 && TREE_TYPE (TREE_VALUE (val)) != NULL_TREE
2783 && (TREE_TYPE (val) == unknown_type_node
2784 || DECL_CHAIN (TREE_VALUE (val)) == NULL_TREE))
2785 /* Instantiates automatically. */
2786 val = TREE_VALUE (val);
2789 error ("insufficient type information in parameter list");
2790 val = integer_zero_node;
2793 else if (TREE_CODE (val) == OFFSET_REF
2794 && TREE_CODE (TREE_TYPE (val)) == METHOD_TYPE)
2796 /* This is unclean. Should be handled elsewhere. */
2797 val = build_unary_op (ADDR_EXPR, val, 0);
2799 else if (TREE_CODE (val) == OFFSET_REF)
2800 val = resolve_offset_ref (val);
2802 /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
2803 Strip such NOP_EXPRs, since VAL is used in non-lvalue context. */
2804 if (TREE_CODE (val) == NOP_EXPR
2805 && TREE_TYPE (val) == TREE_TYPE (TREE_OPERAND (val, 0))
2806 && (type == 0 || TREE_CODE (type) != REFERENCE_TYPE))
2807 val = TREE_OPERAND (val, 0);
2809 if (type == 0 || TREE_CODE (type) != REFERENCE_TYPE)
2811 if (TREE_CODE (TREE_TYPE (val)) == ARRAY_TYPE
2812 || TREE_CODE (TREE_TYPE (val)) == FUNCTION_TYPE
2813 || TREE_CODE (TREE_TYPE (val)) == METHOD_TYPE)
2814 val = default_conversion (val);
2816 val = require_complete_type (val);
2819 if (val == error_mark_node)
2820 return error_mark_node;
2824 /* Formal parm type is specified by a function prototype. */
2827 if (TYPE_SIZE (complete_type (type)) == 0)
2829 error ("parameter type of called function is incomplete");
2834 parmval = convert_for_initialization
2835 (return_loc, type, val, flags,
2836 "argument passing", fndecl, i);
2837 #ifdef PROMOTE_PROTOTYPES
2838 if ((TREE_CODE (type) == INTEGER_TYPE
2839 || TREE_CODE (type) == ENUMERAL_TYPE)
2840 && (TYPE_PRECISION (type) < TYPE_PRECISION (integer_type_node)))
2841 parmval = default_conversion (parmval);
2845 if (parmval == error_mark_node)
2846 return error_mark_node;
2848 result = tree_cons (NULL_TREE, parmval, result);
2852 if (TREE_CODE (TREE_TYPE (val)) == REFERENCE_TYPE)
2853 val = convert_from_reference (val);
2855 if (TREE_CODE (TREE_TYPE (val)) == REAL_TYPE
2856 && (TYPE_PRECISION (TREE_TYPE (val))
2857 < TYPE_PRECISION (double_type_node)))
2858 /* Convert `float' to `double'. */
2859 result = tree_cons (NULL_TREE, convert (double_type_node, val), result);
2860 else if (TYPE_LANG_SPECIFIC (TREE_TYPE (val))
2861 && ! TYPE_HAS_TRIVIAL_INIT_REF (TREE_TYPE (val)))
2863 cp_warning ("cannot pass objects of type `%T' through `...'",
2865 result = tree_cons (NULL_TREE, val, result);
2868 /* Convert `short' and `char' to full-size `int'. */
2869 result = tree_cons (NULL_TREE, default_conversion (val), result);
2873 typetail = TREE_CHAIN (typetail);
2876 if (typetail != 0 && typetail != void_list_node)
2878 /* See if there are default arguments that can be used */
2879 if (TREE_PURPOSE (typetail))
2881 for (; typetail != void_list_node; ++i)
2883 tree type = TREE_VALUE (typetail);
2884 tree val = break_out_target_exprs (TREE_PURPOSE (typetail));
2887 if (val == NULL_TREE)
2888 parmval = error_mark_node;
2889 else if (TREE_CODE (val) == CONSTRUCTOR)
2891 parmval = digest_init (type, val, (tree *)0);
2892 parmval = convert_for_initialization (return_loc, type, parmval, flags,
2893 "default constructor", fndecl, i);
2897 /* This could get clobbered by the following call. */
2898 if (TREE_HAS_CONSTRUCTOR (val))
2899 val = copy_node (val);
2901 parmval = convert_for_initialization (return_loc, type, val, flags,
2902 "default argument", fndecl, i);
2903 #ifdef PROMOTE_PROTOTYPES
2904 if ((TREE_CODE (type) == INTEGER_TYPE
2905 || TREE_CODE (type) == ENUMERAL_TYPE)
2906 && (TYPE_PRECISION (type) < TYPE_PRECISION (integer_type_node)))
2907 parmval = default_conversion (parmval);
2911 if (parmval == error_mark_node)
2912 return error_mark_node;
2914 result = tree_cons (0, parmval, result);
2915 typetail = TREE_CHAIN (typetail);
2916 /* ends with `...'. */
2917 if (typetail == NULL_TREE)
2925 char *buf = (char *)alloca (32 + strlen (called_thing));
2926 sprintf (buf, "too few arguments to %s `%%#D'", called_thing);
2927 cp_error_at (buf, fndecl);
2928 error ("at this point in file");
2931 error ("too few arguments to function");
2932 return error_mark_list;
2936 return nreverse (result);
2939 /* Build a binary-operation expression, after performing default
2940 conversions on the operands. CODE is the kind of expression to build. */
2943 build_x_binary_op (code, arg1, arg2)
2944 enum tree_code code;
2949 if (processing_template_decl)
2950 return build_min_nt (code, arg1, arg2);
2952 if (flag_ansi_overloading)
2953 return build_new_op (code, LOOKUP_NORMAL, arg1, arg2, NULL_TREE);
2955 rval = build_opfncall (code, LOOKUP_SPECULATIVELY,
2956 arg1, arg2, NULL_TREE);
2958 return build_opfncall (code, LOOKUP_NORMAL, arg1, arg2, NULL_TREE);
2959 if (code == MEMBER_REF)
2960 return build_m_component_ref (build_indirect_ref (arg1, NULL_PTR),
2962 return build_binary_op (code, arg1, arg2, 1);
2966 build_binary_op (code, arg1, arg2, convert_p)
2967 enum tree_code code;
2979 args[0] = decay_conversion (args[0]);
2980 args[1] = decay_conversion (args[1]);
2982 if (args[0] == error_mark_node || args[1] == error_mark_node)
2983 return error_mark_node;
2985 type0 = TREE_TYPE (args[0]);
2986 type1 = TREE_TYPE (args[1]);
2988 if (type_unknown_p (args[0]))
2990 args[0] = instantiate_type (type1, args[0], 1);
2991 args[0] = decay_conversion (args[0]);
2993 else if (type_unknown_p (args[1]))
2995 args[1] = require_instantiated_type (type0, args[1],
2997 args[1] = decay_conversion (args[1]);
3000 if (IS_AGGR_TYPE (type0) || IS_AGGR_TYPE (type1))
3002 /* Try to convert this to something reasonable. */
3003 if (! build_default_binary_type_conversion(code, &args[0], &args[1]))
3005 cp_error ("no match for `%O(%#T, %#T)'", code,
3006 TREE_TYPE (arg1), TREE_TYPE (arg2));
3007 return error_mark_node;
3011 return build_binary_op_nodefault (code, args[0], args[1], code);
3014 /* Build a binary-operation expression without default conversions.
3015 CODE is the kind of expression to build.
3016 This function differs from `build' in several ways:
3017 the data type of the result is computed and recorded in it,
3018 warnings are generated if arg data types are invalid,
3019 special handling for addition and subtraction of pointers is known,
3020 and some optimization is done (operations on narrow ints
3021 are done in the narrower type when that gives the same result).
3022 Constant folding is also done before the result is returned.
3024 ERROR_CODE is the code that determines what to say in error messages.
3025 It is usually, but not always, the same as CODE.
3027 Note that the operands will never have enumeral types
3028 because either they have just had the default conversions performed
3029 or they have both just been converted to some other type in which
3030 the arithmetic is to be done.
3032 C++: must do special pointer arithmetic when implementing
3033 multiple inheritance, and deal with pointer to member functions. */
3036 build_binary_op_nodefault (code, orig_op0, orig_op1, error_code)
3037 enum tree_code code;
3038 tree orig_op0, orig_op1;
3039 enum tree_code error_code;
3042 register enum tree_code code0, code1;
3045 /* Expression code to give to the expression when it is built.
3046 Normally this is CODE, which is what the caller asked for,
3047 but in some special cases we change it. */
3048 register enum tree_code resultcode = code;
3050 /* Data type in which the computation is to be performed.
3051 In the simplest cases this is the common type of the arguments. */
3052 register tree result_type = NULL;
3054 /* Nonzero means operands have already been type-converted
3055 in whatever way is necessary.
3056 Zero means they need to be converted to RESULT_TYPE. */
3059 /* Nonzero means create the expression with this type, rather than
3061 tree build_type = 0;
3063 /* Nonzero means after finally constructing the expression
3064 convert it to this type. */
3065 tree final_type = 0;
3067 /* Nonzero if this is an operation like MIN or MAX which can
3068 safely be computed in short if both args are promoted shorts.
3069 Also implies COMMON.
3070 -1 indicates a bitwise operation; this makes a difference
3071 in the exact conditions for when it is safe to do the operation
3072 in a narrower mode. */
3075 /* Nonzero if this is a comparison operation;
3076 if both args are promoted shorts, compare the original shorts.
3077 Also implies COMMON. */
3078 int short_compare = 0;
3080 /* Nonzero if this is a right-shift operation, which can be computed on the
3081 original short and then promoted if the operand is a promoted short. */
3082 int short_shift = 0;
3084 /* Nonzero means set RESULT_TYPE to the common type of the args. */
3087 /* Apply default conversions. */
3088 if (code == TRUTH_AND_EXPR || code == TRUTH_ANDIF_EXPR
3089 || code == TRUTH_OR_EXPR || code == TRUTH_ORIF_EXPR
3090 || code == TRUTH_XOR_EXPR)
3092 op0 = decay_conversion (orig_op0);
3093 op1 = decay_conversion (orig_op1);
3097 op0 = default_conversion (orig_op0);
3098 op1 = default_conversion (orig_op1);
3101 type0 = TREE_TYPE (op0);
3102 type1 = TREE_TYPE (op1);
3104 /* The expression codes of the data types of the arguments tell us
3105 whether the arguments are integers, floating, pointers, etc. */
3106 code0 = TREE_CODE (type0);
3107 code1 = TREE_CODE (type1);
3109 /* Strip NON_LVALUE_EXPRs, etc., since we aren't using as an lvalue. */
3110 STRIP_TYPE_NOPS (op0);
3111 STRIP_TYPE_NOPS (op1);
3113 /* If an error was already reported for one of the arguments,
3114 avoid reporting another error. */
3116 if (code0 == ERROR_MARK || code1 == ERROR_MARK)
3117 return error_mark_node;
3122 /* Handle the pointer + int case. */
3123 if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
3124 return pointer_int_sum (PLUS_EXPR, op0, op1);
3125 else if (code1 == POINTER_TYPE && code0 == INTEGER_TYPE)
3126 return pointer_int_sum (PLUS_EXPR, op1, op0);
3132 /* Subtraction of two similar pointers.
3133 We must subtract them as integers, then divide by object size. */
3134 if (code0 == POINTER_TYPE && code1 == POINTER_TYPE
3135 && comp_target_types (type0, type1, 1))
3136 return pointer_diff (op0, op1);
3137 /* Handle pointer minus int. Just like pointer plus int. */
3138 else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
3139 return pointer_int_sum (MINUS_EXPR, op0, op1);
3148 case TRUNC_DIV_EXPR:
3150 case FLOOR_DIV_EXPR:
3151 case ROUND_DIV_EXPR:
3152 case EXACT_DIV_EXPR:
3153 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE)
3154 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE))
3156 if (TREE_CODE (op1) == INTEGER_CST && integer_zerop (op1))
3157 cp_warning ("division by zero in `%E / 0'", op0);
3158 else if (TREE_CODE (op1) == REAL_CST && real_zerop (op1))
3159 cp_warning ("division by zero in `%E / 0.'", op0);
3161 if (!(code0 == INTEGER_TYPE && code1 == INTEGER_TYPE))
3162 resultcode = RDIV_EXPR;
3164 /* When dividing two signed integers, we have to promote to int.
3165 unless we divide by a constant != -1. Note that default
3166 conversion will have been performed on the operands at this
3167 point, so we have to dig out the original type to find out if
3169 shorten = ((TREE_CODE (op0) == NOP_EXPR
3170 && TREE_UNSIGNED (TREE_TYPE (TREE_OPERAND (op0, 0))))
3171 || (TREE_CODE (op1) == INTEGER_CST
3172 && (TREE_INT_CST_LOW (op1) != -1
3173 || TREE_INT_CST_HIGH (op1) != -1)));
3179 case BIT_ANDTC_EXPR:
3182 if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
3184 /* If one operand is a constant, and the other is a short type
3185 that has been converted to an int,
3186 really do the work in the short type and then convert the
3187 result to int. If we are lucky, the constant will be 0 or 1
3188 in the short type, making the entire operation go away. */
3189 if (TREE_CODE (op0) == INTEGER_CST
3190 && TREE_CODE (op1) == NOP_EXPR
3191 && TYPE_PRECISION (type1) > TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (op1, 0)))
3192 && TREE_UNSIGNED (TREE_TYPE (TREE_OPERAND (op1, 0))))
3194 final_type = result_type;
3195 op1 = TREE_OPERAND (op1, 0);
3196 result_type = TREE_TYPE (op1);
3198 if (TREE_CODE (op1) == INTEGER_CST
3199 && TREE_CODE (op0) == NOP_EXPR
3200 && TYPE_PRECISION (type0) > TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (op0, 0)))
3201 && TREE_UNSIGNED (TREE_TYPE (TREE_OPERAND (op0, 0))))
3203 final_type = result_type;
3204 op0 = TREE_OPERAND (op0, 0);
3205 result_type = TREE_TYPE (op0);
3209 case TRUNC_MOD_EXPR:
3210 case FLOOR_MOD_EXPR:
3211 if (code1 == INTEGER_TYPE && integer_zerop (op1))
3212 cp_warning ("division by zero in `%E % 0'", op0);
3213 else if (code1 == REAL_TYPE && real_zerop (op1))
3214 cp_warning ("division by zero in `%E % 0.'", op0);
3216 if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
3218 /* Although it would be tempting to shorten always here, that loses
3219 on some targets, since the modulo instruction is undefined if the
3220 quotient can't be represented in the computation mode. We shorten
3221 only if unsigned or if dividing by something we know != -1. */
3222 shorten = ((TREE_CODE (op0) == NOP_EXPR
3223 && TREE_UNSIGNED (TREE_TYPE (TREE_OPERAND (op0, 0))))
3224 || (TREE_CODE (op1) == INTEGER_CST
3225 && (TREE_INT_CST_LOW (op1) != -1
3226 || TREE_INT_CST_HIGH (op1) != -1)));
3231 case TRUTH_ANDIF_EXPR:
3232 case TRUTH_ORIF_EXPR:
3233 case TRUTH_AND_EXPR:
3235 result_type = boolean_type_node;
3238 /* Shift operations: result has same type as first operand;
3239 always convert second operand to int.
3240 Also set SHORT_SHIFT if shifting rightward. */
3243 if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
3245 result_type = type0;
3246 if (TREE_CODE (op1) == INTEGER_CST)
3248 if (tree_int_cst_lt (op1, integer_zero_node))
3249 warning ("right shift count is negative");
3252 if (TREE_INT_CST_LOW (op1) | TREE_INT_CST_HIGH (op1))
3254 if (TREE_INT_CST_HIGH (op1) != 0
3255 || ((unsigned HOST_WIDE_INT) TREE_INT_CST_LOW (op1)
3256 >= TYPE_PRECISION (type0)))
3257 warning ("right shift count >= width of type");
3260 /* Convert the shift-count to an integer, regardless of
3261 size of value being shifted. */
3262 if (TYPE_MAIN_VARIANT (TREE_TYPE (op1)) != integer_type_node)
3263 op1 = convert (integer_type_node, op1);
3264 /* Avoid converting op1 to result_type later. */
3270 if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
3272 result_type = type0;
3273 if (TREE_CODE (op1) == INTEGER_CST)
3275 if (tree_int_cst_lt (op1, integer_zero_node))
3276 warning ("left shift count is negative");
3277 else if (TREE_INT_CST_HIGH (op1) != 0
3278 || ((unsigned HOST_WIDE_INT) TREE_INT_CST_LOW (op1)
3279 >= TYPE_PRECISION (type0)))
3280 warning ("left shift count >= width of type");
3282 /* Convert the shift-count to an integer, regardless of
3283 size of value being shifted. */
3284 if (TYPE_MAIN_VARIANT (TREE_TYPE (op1)) != integer_type_node)
3285 op1 = convert (integer_type_node, op1);
3286 /* Avoid converting op1 to result_type later. */
3293 if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
3295 result_type = type0;
3296 if (TREE_CODE (op1) == INTEGER_CST)
3298 if (tree_int_cst_lt (op1, integer_zero_node))
3299 warning ("%s rotate count is negative",
3300 (code == LROTATE_EXPR) ? "left" : "right");
3301 else if (TREE_INT_CST_HIGH (op1) != 0
3302 || ((unsigned HOST_WIDE_INT) TREE_INT_CST_LOW (op1)
3303 >= TYPE_PRECISION (type0)))
3304 warning ("%s rotate count >= width of type",
3305 (code == LROTATE_EXPR) ? "left" : "right");
3307 /* Convert the shift-count to an integer, regardless of
3308 size of value being shifted. */
3309 if (TYPE_MAIN_VARIANT (TREE_TYPE (op1)) != integer_type_node)
3310 op1 = convert (integer_type_node, op1);
3316 build_type = boolean_type_node;
3317 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE)
3318 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE))
3320 else if (code0 == POINTER_TYPE && code1 == POINTER_TYPE)
3322 register tree tt0 = TYPE_MAIN_VARIANT (TREE_TYPE (type0));
3323 register tree tt1 = TYPE_MAIN_VARIANT (TREE_TYPE (type1));
3325 if (comp_target_types (type0, type1, 1))
3326 result_type = common_type (type0, type1);
3327 else if (tt0 == void_type_node)
3329 if (pedantic && TREE_CODE (tt1) == FUNCTION_TYPE
3330 && tree_int_cst_lt (TYPE_SIZE (type0), TYPE_SIZE (type1)))
3331 pedwarn ("ANSI C++ forbids comparison of `void *' with function pointer");
3332 else if (TREE_CODE (tt1) == OFFSET_TYPE)
3333 pedwarn ("ANSI C++ forbids conversion of a pointer to member to `void *'");
3335 else if (tt1 == void_type_node)
3337 if (pedantic && TREE_CODE (tt0) == FUNCTION_TYPE
3338 && tree_int_cst_lt (TYPE_SIZE (type1), TYPE_SIZE (type0)))
3339 pedwarn ("ANSI C++ forbids comparison of `void *' with function pointer");
3342 cp_pedwarn ("comparison of distinct pointer types `%T' and `%T' lacks a cast",
3345 if (result_type == NULL_TREE)
3346 result_type = ptr_type_node;
3348 else if (code0 == POINTER_TYPE && TREE_CODE (op1) == INTEGER_CST
3349 && integer_zerop (op1))
3350 result_type = type0;
3351 else if (code1 == POINTER_TYPE && TREE_CODE (op0) == INTEGER_CST
3352 && integer_zerop (op0))
3353 result_type = type1;
3354 else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
3356 result_type = type0;
3357 error ("ANSI C++ forbids comparison between pointer and integer");
3359 else if (code0 == INTEGER_TYPE && code1 == POINTER_TYPE)
3361 result_type = type1;
3362 error ("ANSI C++ forbids comparison between pointer and integer");
3364 else if (TYPE_PTRMEMFUNC_P (type0) && TREE_CODE (op1) == INTEGER_CST
3365 && integer_zerop (op1))
3367 op0 = build_component_ref (op0, index_identifier, NULL_TREE, 0);
3368 op1 = integer_zero_node;
3369 result_type = TREE_TYPE (op0);
3371 else if (TYPE_PTRMEMFUNC_P (type1) && TREE_CODE (op0) == INTEGER_CST
3372 && integer_zerop (op0))
3374 op0 = build_component_ref (op1, index_identifier, NULL_TREE, 0);
3375 op1 = integer_zero_node;
3376 result_type = TREE_TYPE (op0);
3378 else if (TYPE_PTRMEMFUNC_P (type0) && TYPE_PTRMEMFUNC_P (type1)
3379 && (TYPE_PTRMEMFUNC_FN_TYPE (type0)
3380 == TYPE_PTRMEMFUNC_FN_TYPE (type1)))
3382 /* The code we generate for the test is:
3384 (op0.index == op1.index
3385 && ((op1.index != -1 && op0.delta2 == op1.delta2)
3386 || op0.pfn == op1.pfn)) */
3388 tree index0 = build_component_ref (op0, index_identifier, NULL_TREE, 0);
3389 tree index1 = save_expr (build_component_ref (op1, index_identifier, NULL_TREE, 0));
3390 tree pfn0 = PFN_FROM_PTRMEMFUNC (op0);
3391 tree pfn1 = PFN_FROM_PTRMEMFUNC (op1);
3392 tree delta20 = DELTA2_FROM_PTRMEMFUNC (op0);
3393 tree delta21 = DELTA2_FROM_PTRMEMFUNC (op1);
3395 tree integer_neg_one_node
3396 = build_binary_op (MINUS_EXPR, integer_zero_node, integer_one_node, 1);
3397 e1 = build_binary_op (EQ_EXPR, index0, index1, 1);
3398 e2 = build_binary_op (NE_EXPR, index1, integer_neg_one_node, 1);
3399 e2 = build_binary_op (TRUTH_ANDIF_EXPR, e2, build_binary_op (EQ_EXPR, delta20, delta21, 1), 1);
3400 e3 = build_binary_op (EQ_EXPR, pfn0, pfn1, 1);
3401 e2 = build_binary_op (TRUTH_ORIF_EXPR, e2, e3, 1);
3402 e2 = build_binary_op (TRUTH_ANDIF_EXPR, e1, e2, 1);
3403 if (code == EQ_EXPR)
3405 return build_binary_op (EQ_EXPR, e2, integer_zero_node, 1);
3407 else if (TYPE_PTRMEMFUNC_P (type0)
3408 && TYPE_PTRMEMFUNC_FN_TYPE (type0) == type1)
3410 tree index0 = build_component_ref (op0, index_identifier, NULL_TREE, 0);
3412 tree pfn0 = PFN_FROM_PTRMEMFUNC (op0);
3413 tree delta20 = DELTA2_FROM_PTRMEMFUNC (op0);
3414 tree delta21 = integer_zero_node;
3416 tree integer_neg_one_node
3417 = build_binary_op (MINUS_EXPR, integer_zero_node, integer_one_node, 1);
3418 if (TREE_CODE (TREE_OPERAND (op1, 0)) == FUNCTION_DECL
3419 && DECL_VINDEX (TREE_OPERAND (op1, 0)))
3421 /* Map everything down one to make room for the null pointer to member. */
3422 index1 = size_binop (PLUS_EXPR,
3423 DECL_VINDEX (TREE_OPERAND (op1, 0)),
3425 op1 = integer_zero_node;
3426 delta21 = CLASSTYPE_VFIELD (TYPE_METHOD_BASETYPE (TREE_TYPE (type1)));
3427 delta21 = DECL_FIELD_BITPOS (delta21);
3428 delta21 = size_binop (FLOOR_DIV_EXPR, delta21, size_int (BITS_PER_UNIT));
3431 index1 = integer_neg_one_node;
3433 tree nop1 = build1 (NOP_EXPR, TYPE_PTRMEMFUNC_FN_TYPE (type0), op1);
3434 TREE_CONSTANT (nop1) = TREE_CONSTANT (op1);
3437 e1 = build_binary_op (EQ_EXPR, index0, index1, 1);
3438 e2 = build_binary_op (NE_EXPR, index1, integer_neg_one_node, 1);
3439 e2 = build_binary_op (TRUTH_ANDIF_EXPR, e2, build_binary_op (EQ_EXPR, delta20, delta21, 1), 1);
3440 e3 = build_binary_op (EQ_EXPR, pfn0, op1, 1);
3441 e2 = build_binary_op (TRUTH_ORIF_EXPR, e2, e3, 1);
3442 e2 = build_binary_op (TRUTH_ANDIF_EXPR, e1, e2, 1);
3443 if (code == EQ_EXPR)
3445 return build_binary_op (EQ_EXPR, e2, integer_zero_node, 1);
3447 else if (TYPE_PTRMEMFUNC_P (type1)
3448 && TYPE_PTRMEMFUNC_FN_TYPE (type1) == type0)
3450 return build_binary_op (code, op1, op0, 1);
3456 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE)
3457 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE))
3459 else if (code0 == POINTER_TYPE && code1 == POINTER_TYPE)
3461 if (comp_target_types (type0, type1, 1))
3462 result_type = common_type (type0, type1);
3465 cp_pedwarn ("comparison of distinct pointer types `%T' and `%T' lacks a cast",
3467 result_type = ptr_type_node;
3476 build_type = boolean_type_node;
3477 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE)
3478 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE))
3480 else if (code0 == POINTER_TYPE && code1 == POINTER_TYPE)
3482 if (comp_target_types (type0, type1, 1))
3483 result_type = common_type (type0, type1);
3486 cp_pedwarn ("comparison of distinct pointer types `%T' and `%T' lacks a cast",
3488 result_type = ptr_type_node;
3491 else if (code0 == POINTER_TYPE && TREE_CODE (op1) == INTEGER_CST
3492 && integer_zerop (op1))
3493 result_type = type0;
3494 else if (code1 == POINTER_TYPE && TREE_CODE (op0) == INTEGER_CST
3495 && integer_zerop (op0))
3496 result_type = type1;
3497 else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
3499 result_type = type0;
3500 pedwarn ("ANSI C++ forbids comparison between pointer and integer");
3502 else if (code0 == INTEGER_TYPE && code1 == POINTER_TYPE)
3504 result_type = type1;
3505 pedwarn ("ANSI C++ forbids comparison between pointer and integer");
3510 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE)
3511 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE))
3513 if (shorten || common || short_compare)
3514 result_type = common_type (type0, type1);
3516 /* For certain operations (which identify themselves by shorten != 0)
3517 if both args were extended from the same smaller type,
3518 do the arithmetic in that type and then extend.
3520 shorten !=0 and !=1 indicates a bitwise operation.
3521 For them, this optimization is safe only if
3522 both args are zero-extended or both are sign-extended.
3523 Otherwise, we might change the result.
3524 Eg, (short)-1 | (unsigned short)-1 is (int)-1
3525 but calculated in (unsigned short) it would be (unsigned short)-1. */
3529 int unsigned0, unsigned1;
3530 tree arg0 = get_narrower (op0, &unsigned0);
3531 tree arg1 = get_narrower (op1, &unsigned1);
3532 /* UNS is 1 if the operation to be done is an unsigned one. */
3533 int uns = TREE_UNSIGNED (result_type);
3536 final_type = result_type;
3538 /* Handle the case that OP0 does not *contain* a conversion
3539 but it *requires* conversion to FINAL_TYPE. */
3541 if (op0 == arg0 && TREE_TYPE (op0) != final_type)
3542 unsigned0 = TREE_UNSIGNED (TREE_TYPE (op0));
3543 if (op1 == arg1 && TREE_TYPE (op1) != final_type)
3544 unsigned1 = TREE_UNSIGNED (TREE_TYPE (op1));
3546 /* Now UNSIGNED0 is 1 if ARG0 zero-extends to FINAL_TYPE. */
3548 /* For bitwise operations, signedness of nominal type
3549 does not matter. Consider only how operands were extended. */
3553 /* Note that in all three cases below we refrain from optimizing
3554 an unsigned operation on sign-extended args.
3555 That would not be valid. */
3557 /* Both args variable: if both extended in same way
3558 from same width, do it in that width.
3559 Do it unsigned if args were zero-extended. */
3560 if ((TYPE_PRECISION (TREE_TYPE (arg0))
3561 < TYPE_PRECISION (result_type))
3562 && (TYPE_PRECISION (TREE_TYPE (arg1))
3563 == TYPE_PRECISION (TREE_TYPE (arg0)))
3564 && unsigned0 == unsigned1
3565 && (unsigned0 || !uns))
3567 = signed_or_unsigned_type (unsigned0,
3568 common_type (TREE_TYPE (arg0), TREE_TYPE (arg1)));
3569 else if (TREE_CODE (arg0) == INTEGER_CST
3570 && (unsigned1 || !uns)
3571 && (TYPE_PRECISION (TREE_TYPE (arg1))
3572 < TYPE_PRECISION (result_type))
3573 && (type = signed_or_unsigned_type (unsigned1,
3575 int_fits_type_p (arg0, type)))
3577 else if (TREE_CODE (arg1) == INTEGER_CST
3578 && (unsigned0 || !uns)
3579 && (TYPE_PRECISION (TREE_TYPE (arg0))
3580 < TYPE_PRECISION (result_type))
3581 && (type = signed_or_unsigned_type (unsigned0,
3583 int_fits_type_p (arg1, type)))
3587 /* Shifts can be shortened if shifting right. */
3592 tree arg0 = get_narrower (op0, &unsigned_arg);
3594 final_type = result_type;
3596 if (arg0 == op0 && final_type == TREE_TYPE (op0))
3597 unsigned_arg = TREE_UNSIGNED (TREE_TYPE (op0));
3599 if (TYPE_PRECISION (TREE_TYPE (arg0)) < TYPE_PRECISION (result_type)
3600 /* We can shorten only if the shift count is less than the
3601 number of bits in the smaller type size. */
3602 && TREE_INT_CST_HIGH (op1) == 0
3603 && TYPE_PRECISION (TREE_TYPE (arg0)) > TREE_INT_CST_LOW (op1)
3604 /* If arg is sign-extended and then unsigned-shifted,
3605 we can simulate this with a signed shift in arg's type
3606 only if the extended result is at least twice as wide
3607 as the arg. Otherwise, the shift could use up all the
3608 ones made by sign-extension and bring in zeros.
3609 We can't optimize that case at all, but in most machines
3610 it never happens because available widths are 2**N. */
3611 && (!TREE_UNSIGNED (final_type)
3613 || (((unsigned) 2 * TYPE_PRECISION (TREE_TYPE (arg0)))
3614 <= TYPE_PRECISION (result_type))))
3616 /* Do an unsigned shift if the operand was zero-extended. */
3618 = signed_or_unsigned_type (unsigned_arg,
3620 /* Convert value-to-be-shifted to that type. */
3621 if (TREE_TYPE (op0) != result_type)
3622 op0 = convert (result_type, op0);
3627 /* Comparison operations are shortened too but differently.
3628 They identify themselves by setting short_compare = 1. */
3632 /* Don't write &op0, etc., because that would prevent op0
3633 from being kept in a register.
3634 Instead, make copies of the our local variables and
3635 pass the copies by reference, then copy them back afterward. */
3636 tree xop0 = op0, xop1 = op1, xresult_type = result_type;
3637 enum tree_code xresultcode = resultcode;
3639 = shorten_compare (&xop0, &xop1, &xresult_type, &xresultcode);
3641 return convert (boolean_type_node, val);
3642 op0 = xop0, op1 = xop1;
3644 resultcode = xresultcode;
3647 if (short_compare && warn_sign_compare)
3649 int op0_signed = ! TREE_UNSIGNED (TREE_TYPE (orig_op0));
3650 int op1_signed = ! TREE_UNSIGNED (TREE_TYPE (orig_op1));
3652 int unsignedp0, unsignedp1;
3653 tree primop0 = get_narrower (op0, &unsignedp0);
3654 tree primop1 = get_narrower (op1, &unsignedp1);
3656 /* Check for comparison of different enum types. */
3657 if (flag_int_enum_equivalence == 0
3658 && TREE_CODE (TREE_TYPE (orig_op0)) == ENUMERAL_TYPE
3659 && TREE_CODE (TREE_TYPE (orig_op1)) == ENUMERAL_TYPE
3660 && TYPE_MAIN_VARIANT (TREE_TYPE (orig_op0))
3661 != TYPE_MAIN_VARIANT (TREE_TYPE (orig_op1)))
3663 cp_warning ("comparison between `%#T' and `%#T'",
3664 TREE_TYPE (orig_op0), TREE_TYPE (orig_op1));
3667 /* Give warnings for comparisons between signed and unsigned
3668 quantities that may fail. */
3669 /* Do the checking based on the original operand trees, so that
3670 casts will be considered, but default promotions won't be. */
3672 /* Do not warn if the comparison is being done in a signed type,
3673 since the signed type will only be chosen if it can represent
3674 all the values of the unsigned type. */
3675 if (! TREE_UNSIGNED (result_type))
3677 /* Do not warn if both operands are unsigned. */
3678 else if (op0_signed == op1_signed)
3680 /* Do not warn if the signed quantity is an unsuffixed
3681 integer literal (or some static constant expression
3682 involving such literals) and it is non-negative. */
3683 else if ((op0_signed && TREE_CODE (orig_op0) == INTEGER_CST
3684 && tree_int_cst_sgn (orig_op0) >= 0)
3685 || (op1_signed && TREE_CODE (orig_op1) == INTEGER_CST
3686 && tree_int_cst_sgn (orig_op1) >= 0))
3688 /* Do not warn if the comparison is an equality operation,
3689 the unsigned quantity is an integral constant and it does
3690 not use the most significant bit of result_type. */
3691 else if ((resultcode == EQ_EXPR || resultcode == NE_EXPR)
3692 && ((op0_signed && TREE_CODE (orig_op1) == INTEGER_CST
3693 && int_fits_type_p (orig_op1, signed_type (result_type))
3694 || (op1_signed && TREE_CODE (orig_op0) == INTEGER_CST
3695 && int_fits_type_p (orig_op0, signed_type (result_type))))))
3698 warning ("comparison between signed and unsigned");
3700 /* Warn if two unsigned values are being compared in a size
3701 larger than their original size, and one (and only one) is the
3702 result of a `~' operator. This comparison will always fail.
3704 Also warn if one operand is a constant, and the constant does not
3705 have all bits set that are set in the ~ operand when it is
3708 if ((TREE_CODE (primop0) == BIT_NOT_EXPR)
3709 ^ (TREE_CODE (primop1) == BIT_NOT_EXPR))
3711 if (TREE_CODE (primop0) == BIT_NOT_EXPR)
3712 primop0 = get_narrower (TREE_OPERAND (op0, 0), &unsignedp0);
3713 if (TREE_CODE (primop1) == BIT_NOT_EXPR)
3714 primop1 = get_narrower (TREE_OPERAND (op1, 0), &unsignedp1);
3716 if (TREE_CODE (primop0) == INTEGER_CST
3717 || TREE_CODE (primop1) == INTEGER_CST)
3720 HOST_WIDE_INT constant, mask;
3724 if (TREE_CODE (primop0) == INTEGER_CST)
3727 unsignedp = unsignedp1;
3728 constant = TREE_INT_CST_LOW (primop0);
3733 unsignedp = unsignedp0;
3734 constant = TREE_INT_CST_LOW (primop1);
3737 bits = TYPE_PRECISION (TREE_TYPE (primop));
3738 if (bits < TYPE_PRECISION (result_type)
3739 && bits < HOST_BITS_PER_LONG && unsignedp)
3741 mask = (~ (HOST_WIDE_INT) 0) << bits;
3742 if ((mask & constant) != mask)
3743 warning ("comparison of promoted ~unsigned with constant");
3746 else if (unsignedp0 && unsignedp1
3747 && (TYPE_PRECISION (TREE_TYPE (primop0))
3748 < TYPE_PRECISION (result_type))
3749 && (TYPE_PRECISION (TREE_TYPE (primop1))
3750 < TYPE_PRECISION (result_type)))
3751 warning ("comparison of promoted ~unsigned with unsigned");
3756 /* At this point, RESULT_TYPE must be nonzero to avoid an error message.
3757 If CONVERTED is zero, both args will be converted to type RESULT_TYPE.
3758 Then the expression will be built.
3759 It will be given type FINAL_TYPE if that is nonzero;
3760 otherwise, it will be given type RESULT_TYPE. */
3764 cp_error ("invalid operands `%T' and `%T' to binary `%O'",
3765 TREE_TYPE (orig_op0), TREE_TYPE (orig_op1), error_code);
3766 return error_mark_node;
3771 if (TREE_TYPE (op0) != result_type)
3772 op0 = convert (result_type, op0);
3773 if (TREE_TYPE (op1) != result_type)
3774 op1 = convert (result_type, op1);
3777 if (build_type == NULL_TREE)
3778 build_type = result_type;
3781 register tree result = build (resultcode, build_type, op0, op1);
3782 register tree folded;
3784 folded = fold (result);
3785 if (folded == result)
3786 TREE_CONSTANT (folded) = TREE_CONSTANT (op0) & TREE_CONSTANT (op1);
3787 if (final_type != 0)
3788 return convert (final_type, folded);
3793 /* Return a tree for the sum or difference (RESULTCODE says which)
3794 of pointer PTROP and integer INTOP. */
3797 pointer_int_sum (resultcode, ptrop, intop)
3798 enum tree_code resultcode;
3799 register tree ptrop, intop;
3803 register tree result;
3804 register tree folded = fold (intop);
3806 /* The result is a pointer of the same type that is being added. */
3808 register tree result_type = TREE_TYPE (ptrop);
3810 if (TREE_CODE (TREE_TYPE (result_type)) == VOID_TYPE)
3812 if (pedantic || warn_pointer_arith)
3813 pedwarn ("ANSI C++ forbids using pointer of type `void *' in arithmetic");
3814 size_exp = integer_one_node;
3816 else if (TREE_CODE (TREE_TYPE (result_type)) == FUNCTION_TYPE)
3818 if (pedantic || warn_pointer_arith)
3819 pedwarn ("ANSI C++ forbids using pointer to a function in arithmetic");
3820 size_exp = integer_one_node;
3822 else if (TREE_CODE (TREE_TYPE (result_type)) == METHOD_TYPE)
3824 if (pedantic || warn_pointer_arith)
3825 pedwarn ("ANSI C++ forbids using pointer to a method in arithmetic");
3826 size_exp = integer_one_node;
3828 else if (TREE_CODE (TREE_TYPE (result_type)) == OFFSET_TYPE)
3830 if (pedantic || warn_pointer_arith)
3831 pedwarn ("ANSI C++ forbids using pointer to a member in arithmetic");
3832 size_exp = integer_one_node;
3835 size_exp = size_in_bytes (complete_type (TREE_TYPE (result_type)));
3837 /* Needed to make OOPS V2R3 work. */
3839 if (TREE_CODE (intop) == INTEGER_CST
3840 && TREE_INT_CST_LOW (intop) == 0
3841 && TREE_INT_CST_HIGH (intop) == 0)
3844 /* If what we are about to multiply by the size of the elements
3845 contains a constant term, apply distributive law
3846 and multiply that constant term separately.
3847 This helps produce common subexpressions. */
3849 if ((TREE_CODE (intop) == PLUS_EXPR || TREE_CODE (intop) == MINUS_EXPR)
3850 && ! TREE_CONSTANT (intop)
3851 && TREE_CONSTANT (TREE_OPERAND (intop, 1))
3852 && TREE_CONSTANT (size_exp))
3854 enum tree_code subcode = resultcode;
3855 if (TREE_CODE (intop) == MINUS_EXPR)
3856 subcode = (subcode == PLUS_EXPR ? MINUS_EXPR : PLUS_EXPR);
3857 ptrop = build_binary_op (subcode, ptrop, TREE_OPERAND (intop, 1), 1);
3858 intop = TREE_OPERAND (intop, 0);
3861 /* Convert the integer argument to a type the same size as sizetype
3862 so the multiply won't overflow spuriously. */
3864 if (TYPE_PRECISION (TREE_TYPE (intop)) != TYPE_PRECISION (sizetype))
3865 intop = convert (type_for_size (TYPE_PRECISION (sizetype), 0), intop);
3867 /* Replace the integer argument with a suitable product by the object size.
3868 Do this multiplication as signed, then convert to the appropriate
3869 pointer type (actually unsigned integral). */
3871 intop = convert (result_type,
3872 build_binary_op (MULT_EXPR, intop,
3873 convert (TREE_TYPE (intop), size_exp), 1));
3875 /* Create the sum or difference. */
3877 result = build (resultcode, result_type, ptrop, intop);
3879 folded = fold (result);
3880 if (folded == result)
3881 TREE_CONSTANT (folded) = TREE_CONSTANT (ptrop) & TREE_CONSTANT (intop);
3885 /* Return a tree for the difference of pointers OP0 and OP1.
3886 The resulting tree has type int. */
3889 pointer_diff (op0, op1)
3890 register tree op0, op1;
3892 register tree result, folded;
3893 tree restype = ptrdiff_type_node;
3894 tree target_type = TREE_TYPE (TREE_TYPE (op0));
3896 if (pedantic || warn_pointer_arith)
3898 if (TREE_CODE (target_type) == VOID_TYPE)
3899 pedwarn ("ANSI C++ forbids using pointer of type `void *' in subtraction");
3900 if (TREE_CODE (target_type) == FUNCTION_TYPE)
3901 pedwarn ("ANSI C++ forbids using pointer to a function in subtraction");
3902 if (TREE_CODE (target_type) == METHOD_TYPE)
3903 pedwarn ("ANSI C++ forbids using pointer to a method in subtraction");
3904 if (TREE_CODE (target_type) == OFFSET_TYPE)
3905 pedwarn ("ANSI C++ forbids using pointer to a member in subtraction");
3908 /* First do the subtraction as integers;
3909 then drop through to build the divide operator. */
3911 op0 = build_binary_op (MINUS_EXPR,
3912 convert (restype, op0), convert (restype, op1), 1);
3914 /* This generates an error if op1 is a pointer to an incomplete type. */
3915 if (TYPE_SIZE (TREE_TYPE (TREE_TYPE (op1))) == 0)
3916 error ("arithmetic on pointer to an incomplete type");
3918 op1 = ((TREE_CODE (target_type) == VOID_TYPE
3919 || TREE_CODE (target_type) == FUNCTION_TYPE
3920 || TREE_CODE (target_type) == METHOD_TYPE
3921 || TREE_CODE (target_type) == OFFSET_TYPE)
3923 : size_in_bytes (target_type));
3925 /* Do the division. */
3927 result = build (EXACT_DIV_EXPR, restype, op0, convert (restype, op1));
3929 folded = fold (result);
3930 if (folded == result)
3931 TREE_CONSTANT (folded) = TREE_CONSTANT (op0) & TREE_CONSTANT (op1);
3935 /* Handle the case of taking the address of a COMPONENT_REF.
3936 Called by `build_unary_op' and `build_up_reference'.
3938 ARG is the COMPONENT_REF whose address we want.
3939 ARGTYPE is the pointer type that this address should have.
3940 MSG is an error message to print if this COMPONENT_REF is not
3941 addressable (such as a bitfield). */
3944 build_component_addr (arg, argtype, msg)
3948 tree field = TREE_OPERAND (arg, 1);
3949 tree basetype = decl_type_context (field);
3950 tree rval = build_unary_op (ADDR_EXPR, TREE_OPERAND (arg, 0), 0);
3952 if (DECL_BIT_FIELD (field))
3954 error (msg, IDENTIFIER_POINTER (DECL_NAME (field)));
3955 return error_mark_node;
3958 if (TREE_CODE (field) == FIELD_DECL
3959 && TYPE_USES_COMPLEX_INHERITANCE (basetype))
3961 /* Can't convert directly to ARGTYPE, since that
3962 may have the same pointer type as one of our
3964 rval = build1 (NOP_EXPR, argtype,
3965 convert_pointer_to (basetype, rval));
3966 TREE_CONSTANT (rval) = TREE_CONSTANT (TREE_OPERAND (rval, 0));
3969 /* This conversion is harmless. */
3970 rval = convert_force (argtype, rval, 0);
3972 if (! integer_zerop (DECL_FIELD_BITPOS (field)))
3974 tree offset = size_binop (EASY_DIV_EXPR, DECL_FIELD_BITPOS (field),
3975 size_int (BITS_PER_UNIT));
3976 int flag = TREE_CONSTANT (rval);
3977 rval = fold (build (PLUS_EXPR, argtype,
3978 rval, convert (argtype, offset)));
3979 TREE_CONSTANT (rval) = flag;
3984 /* Construct and perhaps optimize a tree representation
3985 for a unary operation. CODE, a tree_code, specifies the operation
3986 and XARG is the operand. */
3989 build_x_unary_op (code, xarg)
3990 enum tree_code code;
3993 if (processing_template_decl)
3994 return build_min_nt (code, xarg, NULL_TREE);
3996 /* & rec, on incomplete RECORD_TYPEs is the simple opr &, not an
3998 if (code == ADDR_EXPR
3999 && ((IS_AGGR_TYPE_CODE (TREE_CODE (TREE_TYPE (xarg)))
4000 && TYPE_SIZE (TREE_TYPE (xarg)) == NULL_TREE)
4001 || (TREE_CODE (xarg) == OFFSET_REF)))
4002 /* don't look for a function */;
4007 if (flag_ansi_overloading)
4009 rval = build_new_op (code, LOOKUP_NORMAL, xarg,
4010 NULL_TREE, NULL_TREE);
4011 if (rval || code != ADDR_EXPR)
4016 rval = build_opfncall (code, LOOKUP_SPECULATIVELY, xarg,
4017 NULL_TREE, NULL_TREE);
4019 return build_opfncall (code, LOOKUP_NORMAL, xarg,
4020 NULL_TREE, NULL_TREE);
4024 if (code == ADDR_EXPR)
4026 if (TREE_CODE (xarg) == TARGET_EXPR)
4027 warning ("taking address of temporary");
4030 return build_unary_op (code, xarg, 0);
4033 /* Just like truthvalue_conversion, but we want a CLEANUP_POINT_EXPR. */
4036 condition_conversion (expr)
4040 if (processing_template_decl)
4042 t = convert (boolean_type_node, expr);
4043 t = fold (build1 (CLEANUP_POINT_EXPR, boolean_type_node, t));
4047 /* C++: Must handle pointers to members.
4049 Perhaps type instantiation should be extended to handle conversion
4050 from aggregates to types we don't yet know we want? (Or are those
4051 cases typically errors which should be reported?)
4053 NOCONVERT nonzero suppresses the default promotions
4054 (such as from short to int). */
4057 build_unary_op (code, xarg, noconvert)
4058 enum tree_code code;
4062 /* No default_conversion here. It causes trouble for ADDR_EXPR. */
4063 register tree arg = xarg;
4064 register tree argtype = 0;
4065 char *errstring = NULL;
4068 if (arg == error_mark_node)
4069 return error_mark_node;
4074 /* This is used for unary plus, because a CONVERT_EXPR
4075 is enough to prevent anybody from looking inside for
4076 associativity, but won't generate any code. */
4077 if (!(arg = build_expr_type_conversion
4078 (WANT_ARITH | WANT_ENUM | WANT_POINTER, arg, 1)))
4079 errstring = "wrong type argument to unary plus";
4083 arg = default_conversion (arg);
4084 arg = build1 (NON_LVALUE_EXPR, TREE_TYPE (arg), arg);
4089 if (!(arg = build_expr_type_conversion (WANT_ARITH | WANT_ENUM, arg, 1)))
4090 errstring = "wrong type argument to unary minus";
4091 else if (!noconvert)
4092 arg = default_conversion (arg);
4096 if (!(arg = build_expr_type_conversion (WANT_INT | WANT_ENUM, arg, 1)))
4097 errstring = "wrong type argument to bit-complement";
4098 else if (!noconvert)
4099 arg = default_conversion (arg);
4103 if (!(arg = build_expr_type_conversion (WANT_ARITH | WANT_ENUM, arg, 1)))
4104 errstring = "wrong type argument to abs";
4105 else if (!noconvert)
4106 arg = default_conversion (arg);
4109 case TRUTH_NOT_EXPR:
4110 arg = convert (boolean_type_node, arg);
4111 val = invert_truthvalue (arg);
4112 if (arg != error_mark_node)
4114 errstring = "in argument to unary !";
4120 case PREINCREMENT_EXPR:
4121 case POSTINCREMENT_EXPR:
4122 case PREDECREMENT_EXPR:
4123 case POSTDECREMENT_EXPR:
4124 /* Handle complex lvalues (when permitted)
4125 by reduction to simpler cases. */
4127 val = unary_complex_lvalue (code, arg);
4131 /* Report invalid types. */
4133 if (!(arg = build_expr_type_conversion (WANT_ARITH | WANT_POINTER,
4136 if (code == PREINCREMENT_EXPR)
4137 errstring ="no pre-increment operator for type";
4138 else if (code == POSTINCREMENT_EXPR)
4139 errstring ="no post-increment operator for type";
4140 else if (code == PREDECREMENT_EXPR)
4141 errstring ="no pre-decrement operator for type";
4143 errstring ="no post-decrement operator for type";
4147 /* Report something read-only. */
4149 if (TYPE_READONLY (TREE_TYPE (arg))
4150 || TREE_READONLY (arg))
4151 readonly_error (arg, ((code == PREINCREMENT_EXPR
4152 || code == POSTINCREMENT_EXPR)
4153 ? "increment" : "decrement"),
4158 tree result_type = TREE_TYPE (arg);
4160 arg = get_unwidened (arg, 0);
4161 argtype = TREE_TYPE (arg);
4163 /* ARM $5.2.5 last annotation says this should be forbidden. */
4164 if (TREE_CODE (argtype) == ENUMERAL_TYPE)
4165 pedwarn ("ANSI C++ forbids %sing an enum",
4166 (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
4167 ? "increment" : "decrement");
4169 /* Compute the increment. */
4171 if (TREE_CODE (argtype) == POINTER_TYPE)
4173 enum tree_code tmp = TREE_CODE (TREE_TYPE (argtype));
4174 if (TYPE_SIZE (complete_type (TREE_TYPE (argtype))) == 0)
4175 cp_error ("cannot %s a pointer to incomplete type `%T'",
4176 ((code == PREINCREMENT_EXPR
4177 || code == POSTINCREMENT_EXPR)
4178 ? "increment" : "decrement"), TREE_TYPE (argtype));
4179 else if (tmp == FUNCTION_TYPE || tmp == METHOD_TYPE
4180 || tmp == VOID_TYPE || tmp == OFFSET_TYPE)
4181 cp_pedwarn ("ANSI C++ forbids %sing a pointer of type `%T'",
4182 ((code == PREINCREMENT_EXPR
4183 || code == POSTINCREMENT_EXPR)
4184 ? "increment" : "decrement"), argtype);
4185 inc = c_sizeof_nowarn (TREE_TYPE (argtype));
4188 inc = integer_one_node;
4190 inc = convert (argtype, inc);
4192 /* Handle incrementing a cast-expression. */
4194 switch (TREE_CODE (arg))
4199 case FIX_TRUNC_EXPR:
4200 case FIX_FLOOR_EXPR:
4201 case FIX_ROUND_EXPR:
4204 tree incremented, modify, value, compound;
4205 if (! lvalue_p (arg) && pedantic)
4206 pedwarn ("cast to non-reference type used as lvalue");
4207 arg = stabilize_reference (arg);
4208 if (code == PREINCREMENT_EXPR || code == PREDECREMENT_EXPR)
4211 value = save_expr (arg);
4212 incremented = build (((code == PREINCREMENT_EXPR
4213 || code == POSTINCREMENT_EXPR)
4214 ? PLUS_EXPR : MINUS_EXPR),
4215 argtype, value, inc);
4216 TREE_SIDE_EFFECTS (incremented) = 1;
4218 modify = build_modify_expr (arg, NOP_EXPR, incremented);
4219 compound = build (COMPOUND_EXPR, TREE_TYPE (arg), modify, value);
4221 /* Eliminate warning about unused result of + or -. */
4222 TREE_NO_UNUSED_WARNING (compound) = 1;
4227 /* Complain about anything else that is not a true lvalue. */
4228 if (!lvalue_or_else (arg, ((code == PREINCREMENT_EXPR
4229 || code == POSTINCREMENT_EXPR)
4230 ? "increment" : "decrement")))
4231 return error_mark_node;
4233 /* Forbid using -- on `bool'. */
4234 if (TREE_TYPE (arg) == boolean_type_node)
4236 if (code == POSTDECREMENT_EXPR || code == PREDECREMENT_EXPR)
4238 cp_error ("invalid use of `--' on bool variable `%D'", arg);
4239 return error_mark_node;
4242 /* This will only work if someone can convince Kenner to accept
4243 my patch to expand_increment. (jason) */
4244 val = build (code, TREE_TYPE (arg), arg, inc);
4246 if (code == POSTINCREMENT_EXPR)
4248 arg = stabilize_reference (arg);
4249 val = build (MODIFY_EXPR, TREE_TYPE (arg), arg,
4251 TREE_SIDE_EFFECTS (val) = 1;
4252 arg = save_expr (arg);
4253 val = build (COMPOUND_EXPR, TREE_TYPE (arg), val, arg);
4254 val = build (COMPOUND_EXPR, TREE_TYPE (arg), arg, val);
4257 val = build (MODIFY_EXPR, TREE_TYPE (arg), arg,
4262 val = build (code, TREE_TYPE (arg), arg, inc);
4264 TREE_SIDE_EFFECTS (val) = 1;
4265 return convert (result_type, val);
4269 /* Note that this operation never does default_conversion
4270 regardless of NOCONVERT. */
4272 argtype = TREE_TYPE (arg);
4273 if (TREE_CODE (argtype) == REFERENCE_TYPE)
4275 arg = build1 (CONVERT_EXPR, build_pointer_type (TREE_TYPE (TREE_TYPE (arg))), arg);
4276 TREE_REFERENCE_EXPR (arg) = 1;
4280 && TREE_CODE (arg) == FUNCTION_DECL
4282 && DECL_CONTEXT (arg) == NULL_TREE
4283 && IDENTIFIER_LENGTH (DECL_NAME (arg)) == 4
4284 && IDENTIFIER_POINTER (DECL_NAME (arg))[0] == 'm'
4285 && ! strcmp (IDENTIFIER_POINTER (DECL_NAME (arg)), "main"))
4287 pedwarn ("taking address of function `main'");
4289 /* Let &* cancel out to simplify resulting code. */
4290 if (TREE_CODE (arg) == INDIRECT_REF)
4292 /* We don't need to have `current_class_ptr' wrapped in a
4293 NON_LVALUE_EXPR node. */
4294 if (arg == current_class_ref)
4295 return current_class_ptr;
4297 /* Keep `default_conversion' from converting if
4298 ARG is of REFERENCE_TYPE. */
4299 arg = TREE_OPERAND (arg, 0);
4300 if (TREE_CODE (TREE_TYPE (arg)) == REFERENCE_TYPE)
4302 if (TREE_CODE (arg) == VAR_DECL && DECL_INITIAL (arg)
4303 && !TREE_SIDE_EFFECTS (DECL_INITIAL (arg)))
4304 arg = DECL_INITIAL (arg);
4305 arg = build1 (CONVERT_EXPR, build_pointer_type (TREE_TYPE (TREE_TYPE (arg))), arg);
4306 TREE_REFERENCE_EXPR (arg) = 1;
4307 TREE_CONSTANT (arg) = TREE_CONSTANT (TREE_OPERAND (arg, 0));
4309 else if (lvalue_p (arg))
4310 /* Don't let this be an lvalue. */
4311 return non_lvalue (arg);
4315 /* For &x[y], return x+y */
4316 if (TREE_CODE (arg) == ARRAY_REF)
4318 if (mark_addressable (TREE_OPERAND (arg, 0)) == 0)
4319 return error_mark_node;
4320 return build_binary_op (PLUS_EXPR, TREE_OPERAND (arg, 0),
4321 TREE_OPERAND (arg, 1), 1);
4324 /* Uninstantiated types are all functions. Taking the
4325 address of a function is a no-op, so just return the
4328 if (TREE_CODE (arg) == IDENTIFIER_NODE
4329 && IDENTIFIER_OPNAME_P (arg))
4331 my_friendly_abort (117);
4332 /* We don't know the type yet, so just work around the problem.
4333 We know that this will resolve to an lvalue. */
4334 return build1 (ADDR_EXPR, unknown_type_node, arg);
4337 if (TREE_CODE (arg) == TREE_LIST)
4339 if (TREE_CODE (TREE_VALUE (arg)) == FUNCTION_DECL
4340 && DECL_CHAIN (TREE_VALUE (arg)) == NULL_TREE)
4341 /* Unique overloaded non-member function. */
4342 return build_unary_op (ADDR_EXPR, TREE_VALUE (arg), 0);
4343 if (TREE_CHAIN (arg) == NULL_TREE
4344 && TREE_CODE (TREE_VALUE (arg)) == TREE_LIST
4345 && DECL_CHAIN (TREE_VALUE (TREE_VALUE (arg))) == NULL_TREE)
4346 /* Unique overloaded member function. */
4347 return build_unary_op (ADDR_EXPR, TREE_VALUE (TREE_VALUE (arg)),
4349 return build1 (ADDR_EXPR, unknown_type_node, arg);
4352 /* Handle complex lvalues (when permitted)
4353 by reduction to simpler cases. */
4354 val = unary_complex_lvalue (code, arg);
4358 switch (TREE_CODE (arg))
4363 case FIX_TRUNC_EXPR:
4364 case FIX_FLOOR_EXPR:
4365 case FIX_ROUND_EXPR:
4367 if (! lvalue_p (arg) && pedantic)
4368 pedwarn ("taking the address of a cast to non-reference type");
4371 /* Allow the address of a constructor if all the elements
4373 if (TREE_CODE (arg) == CONSTRUCTOR && TREE_CONSTANT (arg))
4375 /* Anything not already handled and not a true memory reference
4377 else if (TREE_CODE (argtype) != FUNCTION_TYPE
4378 && TREE_CODE (argtype) != METHOD_TYPE
4379 && !lvalue_or_else (arg, "unary `&'"))
4380 return error_mark_node;
4382 /* Ordinary case; arg is a COMPONENT_REF or a decl. */
4383 /* If the lvalue is const or volatile,
4384 merge that into the type that the address will point to. */
4385 if (TREE_CODE_CLASS (TREE_CODE (arg)) == 'd'
4386 || TREE_CODE_CLASS (TREE_CODE (arg)) == 'r')
4388 if (TREE_READONLY (arg) || TREE_THIS_VOLATILE (arg))
4389 argtype = cp_build_type_variant (argtype,
4390 TREE_READONLY (arg),
4391 TREE_THIS_VOLATILE (arg));
4394 argtype = build_pointer_type (argtype);
4396 if (mark_addressable (arg) == 0)
4397 return error_mark_node;
4402 if (TREE_CODE (arg) == COMPONENT_REF)
4403 addr = build_component_addr (arg, argtype,
4404 "attempt to take address of bit-field structure member `%s'");
4406 addr = build1 (code, argtype, arg);
4408 /* Address of a static or external variable or
4409 function counts as a constant */
4411 TREE_CONSTANT (addr) = 1;
4413 if (TREE_CODE (argtype) == POINTER_TYPE &&
4414 TREE_CODE (TREE_TYPE (argtype)) == METHOD_TYPE)
4416 build_ptrmemfunc_type (argtype);
4417 addr = build_ptrmemfunc (argtype, addr, 0);
4427 argtype = TREE_TYPE (arg);
4428 return fold (build1 (code, argtype, arg));
4432 return error_mark_node;
4436 /* If CONVERSIONS is a conversion expression or a nested sequence of such,
4437 convert ARG with the same conversions in the same order
4438 and return the result. */
4441 convert_sequence (conversions, arg)
4445 switch (TREE_CODE (conversions))
4450 case FIX_TRUNC_EXPR:
4451 case FIX_FLOOR_EXPR:
4452 case FIX_ROUND_EXPR:
4454 return convert (TREE_TYPE (conversions),
4455 convert_sequence (TREE_OPERAND (conversions, 0),
4464 /* Apply unary lvalue-demanding operator CODE to the expression ARG
4465 for certain kinds of expressions which are not really lvalues
4466 but which we can accept as lvalues.
4468 If ARG is not a kind of expression we can handle, return zero. */
4471 unary_complex_lvalue (code, arg)
4472 enum tree_code code;
4475 /* Handle (a, b) used as an "lvalue". */
4476 if (TREE_CODE (arg) == COMPOUND_EXPR)
4478 tree real_result = build_unary_op (code, TREE_OPERAND (arg, 1), 0);
4479 return build (COMPOUND_EXPR, TREE_TYPE (real_result),
4480 TREE_OPERAND (arg, 0), real_result);
4483 /* Handle (a ? b : c) used as an "lvalue". */
4484 if (TREE_CODE (arg) == COND_EXPR)
4485 return rationalize_conditional_expr (code, arg);
4487 if (TREE_CODE (arg) == MODIFY_EXPR
4488 || TREE_CODE (arg) == PREINCREMENT_EXPR
4489 || TREE_CODE (arg) == PREDECREMENT_EXPR)
4490 return unary_complex_lvalue
4491 (code, build (COMPOUND_EXPR, TREE_TYPE (TREE_OPERAND (arg, 0)),
4492 arg, TREE_OPERAND (arg, 0)));
4494 if (code != ADDR_EXPR)
4497 /* Handle (a = b) used as an "lvalue" for `&'. */
4498 if (TREE_CODE (arg) == MODIFY_EXPR
4499 || TREE_CODE (arg) == INIT_EXPR)
4501 tree real_result = build_unary_op (code, TREE_OPERAND (arg, 0), 0);
4502 arg = build (COMPOUND_EXPR, TREE_TYPE (real_result), arg, real_result);
4503 TREE_NO_UNUSED_WARNING (arg) = 1;
4507 if (TREE_CODE (TREE_TYPE (arg)) == FUNCTION_TYPE
4508 || TREE_CODE (TREE_TYPE (arg)) == METHOD_TYPE
4509 || TREE_CODE (TREE_TYPE (arg)) == OFFSET_TYPE)
4511 /* The representation of something of type OFFSET_TYPE
4512 is really the representation of a pointer to it.
4513 Here give the representation its true type. */
4516 my_friendly_assert (TREE_CODE (arg) != SCOPE_REF, 313);
4518 if (TREE_CODE (arg) != OFFSET_REF)
4521 t = TREE_OPERAND (arg, 1);
4523 if (TREE_CODE (t) == FUNCTION_DECL) /* Check all this code for right semantics. */
4524 return build_unary_op (ADDR_EXPR, t, 0);
4525 if (TREE_CODE (t) == VAR_DECL)
4526 return build_unary_op (ADDR_EXPR, t, 0);
4530 tree offset = integer_zero_node;
4532 if (TREE_OPERAND (arg, 0)
4533 && (TREE_CODE (TREE_OPERAND (arg, 0)) != NOP_EXPR
4534 || TREE_OPERAND (TREE_OPERAND (arg, 0), 0) != error_mark_node))
4535 if (TREE_CODE (t) != FIELD_DECL)
4537 /* Don't know if this should return address to just
4538 _DECL, or actual address resolved in this expression. */
4539 sorry ("address of bound pointer-to-member expression");
4540 return error_mark_node;
4543 type = TREE_TYPE (TREE_OPERAND (arg, 0));
4545 if (TREE_CODE (TREE_TYPE (arg)) == OFFSET_TYPE)
4547 /* Add in the offset to the intermediate subobject, if any. */
4548 offset = get_delta_difference (TYPE_OFFSET_BASETYPE (TREE_TYPE (arg)),
4551 type = TYPE_OFFSET_BASETYPE (TREE_TYPE (arg));
4554 /* Now in the offset to the final subobject. */
4555 offset = size_binop (PLUS_EXPR,
4557 get_delta_difference (DECL_FIELD_CONTEXT (t),
4561 /* Add in the offset to the field. */
4562 offset = size_binop (PLUS_EXPR, offset,
4563 size_binop (EASY_DIV_EXPR,
4564 DECL_FIELD_BITPOS (t),
4565 size_int (BITS_PER_UNIT)));
4567 /* We offset all pointer to data memebers by 1 so that we can
4568 distinguish between a null pointer to data member and the first
4569 data member of a structure. */
4570 offset = size_binop (PLUS_EXPR, offset, size_int (1));
4572 return convert (build_pointer_type (TREE_TYPE (arg)), offset);
4577 /* We permit compiler to make function calls returning
4578 objects of aggregate type look like lvalues. */
4582 if (TREE_CODE (targ) == SAVE_EXPR)
4583 targ = TREE_OPERAND (targ, 0);
4585 if (TREE_CODE (targ) == CALL_EXPR && IS_AGGR_TYPE (TREE_TYPE (targ)))
4587 if (TREE_CODE (arg) == SAVE_EXPR)
4590 targ = build_cplus_new (TREE_TYPE (arg), arg);
4591 return build1 (ADDR_EXPR, build_pointer_type (TREE_TYPE (arg)), targ);
4594 if (TREE_CODE (arg) == SAVE_EXPR && TREE_CODE (targ) == INDIRECT_REF)
4595 return build (SAVE_EXPR, build_pointer_type (TREE_TYPE (arg)),
4596 TREE_OPERAND (targ, 0), current_function_decl, NULL);
4599 /* Don't let anything else be handled specially. */
4603 /* Mark EXP saying that we need to be able to take the
4604 address of it; it should not be allocated in a register.
4605 Value is 1 if successful.
4607 C++: we do not allow `current_class_ptr' to be addressable. */
4610 mark_addressable (exp)
4613 register tree x = exp;
4615 if (TREE_ADDRESSABLE (x) == 1)
4619 switch (TREE_CODE (x))
4624 x = TREE_OPERAND (x, 0);
4628 if (x == current_class_ptr)
4630 if (! flag_this_is_variable)
4631 error ("address of `this' not available");
4632 TREE_ADDRESSABLE (x) = 1; /* so compiler doesn't die later */
4633 put_var_into_stack (x);
4637 if (TREE_STATIC (x) && TREE_READONLY (x)
4638 && DECL_RTL (x) != 0
4639 && ! DECL_IN_MEMORY_P (x))
4641 /* We thought this would make a good constant variable,
4642 but we were wrong. */
4643 push_obstacks_nochange ();
4644 end_temporary_allocation ();
4646 TREE_ASM_WRITTEN (x) = 0;
4648 rest_of_decl_compilation (x, 0, IDENTIFIER_LOCAL_VALUE (x) == 0, 0);
4649 TREE_ADDRESSABLE (x) = 1;
4655 /* Caller should not be trying to mark initialized
4656 constant fields addressable. */
4657 my_friendly_assert (DECL_LANG_SPECIFIC (x) == 0
4658 || DECL_IN_AGGR_P (x) == 0
4660 || DECL_EXTERNAL (x), 314);
4664 if (DECL_REGISTER (x) && !TREE_ADDRESSABLE (x)
4665 && !DECL_ARTIFICIAL (x) && extra_warnings)
4666 cp_warning ("address requested for `%D', which is declared `register'",
4668 put_var_into_stack (x);
4669 TREE_ADDRESSABLE (x) = 1;
4673 /* We have to test both conditions here. The first may
4674 be non-zero in the case of processing a default function.
4675 The second may be non-zero in the case of a template function. */
4676 x = DECL_MAIN_VARIANT (x);
4677 if ((DECL_THIS_INLINE (x) || DECL_PENDING_INLINE_INFO (x))
4678 && (DECL_CONTEXT (x) == NULL_TREE
4679 || TREE_CODE_CLASS (TREE_CODE (DECL_CONTEXT (x))) != 't'
4680 || ! CLASSTYPE_INTERFACE_ONLY (DECL_CONTEXT (x))))
4682 mark_inline_for_output (x);
4683 if (x == current_function_decl)
4684 DECL_EXTERNAL (x) = 0;
4686 TREE_ADDRESSABLE (x) = 1;
4688 TREE_ADDRESSABLE (DECL_ASSEMBLER_NAME (x)) = 1;
4692 TREE_ADDRESSABLE (x) = 1;
4696 TREE_ADDRESSABLE (x) = 1;
4697 mark_addressable (TREE_OPERAND (x, 0));
4705 /* Build and return a conditional expression IFEXP ? OP1 : OP2. */
4708 build_x_conditional_expr (ifexp, op1, op2)
4709 tree ifexp, op1, op2;
4711 tree rval = NULL_TREE;
4713 if (processing_template_decl)
4714 return build_min_nt (COND_EXPR, ifexp, op1, op2);
4716 if (flag_ansi_overloading)
4717 return build_new_op (COND_EXPR, LOOKUP_NORMAL, ifexp, op1, op2);
4719 /* See comments in `build_x_binary_op'. */
4721 rval = build_opfncall (COND_EXPR, LOOKUP_SPECULATIVELY, ifexp, op1, op2);
4723 return build_opfncall (COND_EXPR, LOOKUP_NORMAL, ifexp, op1, op2);
4725 return build_conditional_expr (ifexp, op1, op2);
4729 build_conditional_expr (ifexp, op1, op2)
4730 tree ifexp, op1, op2;
4732 register tree type1;
4733 register tree type2;
4734 register enum tree_code code1;
4735 register enum tree_code code2;
4736 register tree result_type = NULL_TREE;
4738 /* If second operand is omitted, it is the same as the first one;
4739 make sure it is calculated only once. */
4743 pedwarn ("ANSI C++ forbids omitting the middle term of a ?: expression");
4744 ifexp = op1 = save_expr (ifexp);
4747 ifexp = convert (boolean_type_node, ifexp);
4749 if (TREE_CODE (ifexp) == ERROR_MARK)
4750 return error_mark_node;
4752 op1 = require_instantiated_type (TREE_TYPE (op2), op1, error_mark_node);
4753 if (op1 == error_mark_node)
4754 return error_mark_node;
4755 op2 = require_instantiated_type (TREE_TYPE (op1), op2, error_mark_node);
4756 if (op2 == error_mark_node)
4757 return error_mark_node;
4759 /* C++: REFERENCE_TYPES must be dereferenced. */
4760 type1 = TREE_TYPE (op1);
4761 code1 = TREE_CODE (type1);
4762 type2 = TREE_TYPE (op2);
4763 code2 = TREE_CODE (type2);
4765 if (code1 == REFERENCE_TYPE)
4767 op1 = convert_from_reference (op1);
4768 type1 = TREE_TYPE (op1);
4769 code1 = TREE_CODE (type1);
4771 if (code2 == REFERENCE_TYPE)
4773 op2 = convert_from_reference (op2);
4774 type2 = TREE_TYPE (op2);
4775 code2 = TREE_CODE (type2);
4778 /* Don't promote the operands separately if they promote
4779 the same way. Return the unpromoted type and let the combined
4780 value get promoted if necessary. */
4782 if (TYPE_MAIN_VARIANT (type1) == TYPE_MAIN_VARIANT (type2)
4783 && code2 != ARRAY_TYPE
4784 && code2 != FUNCTION_TYPE
4785 && code2 != METHOD_TYPE)
4789 if (TREE_CONSTANT (ifexp)
4790 && (TREE_CODE (ifexp) == INTEGER_CST
4791 || TREE_CODE (ifexp) == ADDR_EXPR))
4792 return (integer_zerop (ifexp) ? op2 : op1);
4794 if (TREE_CODE (op1) == CONST_DECL)
4795 op1 = DECL_INITIAL (op1);
4796 else if (TREE_READONLY_DECL_P (op1))
4797 op1 = decl_constant_value (op1);
4798 if (TREE_CODE (op2) == CONST_DECL)
4799 op2 = DECL_INITIAL (op2);
4800 else if (TREE_READONLY_DECL_P (op2))
4801 op2 = decl_constant_value (op2);
4803 type1 = cp_build_type_variant
4805 TREE_READONLY (op1) || TREE_READONLY (op2),
4806 TREE_THIS_VOLATILE (op1) || TREE_THIS_VOLATILE (op2));
4807 /* ??? This is a kludge to deal with the fact that
4808 we don't sort out integers and enums properly, yet. */
4809 result = fold (build (COND_EXPR, type1, ifexp, op1, op2));
4810 if (TREE_TYPE (result) != type1)
4811 result = build1 (NOP_EXPR, type1, result);
4812 /* Expand both sides into the same slot,
4813 hopefully the target of the ?: expression. */
4814 if (TREE_CODE (op1) == TARGET_EXPR && TREE_CODE (op2) == TARGET_EXPR)
4816 tree slot = build (VAR_DECL, TREE_TYPE (result));
4817 layout_decl (slot, 0);
4818 result = build (TARGET_EXPR, TREE_TYPE (result),
4819 slot, result, NULL_TREE, NULL_TREE);
4824 /* They don't match; promote them both and then try to reconcile them.
4825 But don't permit mismatching enum types. */
4826 if (code1 == ENUMERAL_TYPE)
4828 if (code2 == ENUMERAL_TYPE)
4830 cp_error ("enumeral mismatch in conditional expression: `%T' vs `%T'", type1, type2);
4831 return error_mark_node;
4833 else if (extra_warnings && ! IS_AGGR_TYPE_CODE (code2)
4834 && type2 != type_promotes_to (type1))
4835 warning ("enumeral and non-enumeral type in conditional expression");
4837 else if (extra_warnings
4838 && code2 == ENUMERAL_TYPE && ! IS_AGGR_TYPE_CODE (code1)
4839 && type1 != type_promotes_to (type2))
4840 warning ("enumeral and non-enumeral type in conditional expression");
4842 if (code1 != VOID_TYPE)
4844 op1 = default_conversion (op1);
4845 type1 = TREE_TYPE (op1);
4846 if (TYPE_PTRMEMFUNC_P (type1))
4847 type1 = TYPE_PTRMEMFUNC_FN_TYPE (type1);
4848 code1 = TREE_CODE (type1);
4850 if (code2 != VOID_TYPE)
4852 op2 = default_conversion (op2);
4853 type2 = TREE_TYPE (op2);
4854 if (TYPE_PTRMEMFUNC_P (type2))
4855 type2 = TYPE_PTRMEMFUNC_FN_TYPE (type2);
4856 code2 = TREE_CODE (type2);
4859 if (code1 == RECORD_TYPE && code2 == RECORD_TYPE
4860 && real_lvalue_p (op1) && real_lvalue_p (op2)
4861 && comptypes (type1, type2, -1))
4863 type1 = build_reference_type (type1);
4864 type2 = build_reference_type (type2);
4865 result_type = common_type (type1, type2);
4866 op1 = convert_to_reference (result_type, op1, CONV_IMPLICIT,
4867 LOOKUP_NORMAL, NULL_TREE);
4868 op2 = convert_to_reference (result_type, op2, CONV_IMPLICIT,
4869 LOOKUP_NORMAL, NULL_TREE);
4871 /* Quickly detect the usual case where op1 and op2 have the same type
4873 else if (TYPE_MAIN_VARIANT (type1) == TYPE_MAIN_VARIANT (type2))
4876 result_type = type1;
4878 result_type = cp_build_type_variant
4880 TREE_READONLY (op1) || TREE_READONLY (op2),
4881 TREE_THIS_VOLATILE (op1) || TREE_THIS_VOLATILE (op2));
4883 else if ((code1 == INTEGER_TYPE || code1 == REAL_TYPE)
4884 && (code2 == INTEGER_TYPE || code2 == REAL_TYPE))
4886 result_type = common_type (type1, type2);
4888 else if (code1 == VOID_TYPE || code2 == VOID_TYPE)
4890 if (pedantic && (code1 != VOID_TYPE || code2 != VOID_TYPE))
4891 pedwarn ("ANSI C++ forbids conditional expr with only one void side");
4892 result_type = void_type_node;
4894 else if (code1 == POINTER_TYPE && null_ptr_cst_p (op2))
4895 result_type = qualify_type (type1, type2);
4896 else if (code2 == POINTER_TYPE && null_ptr_cst_p (op1))
4897 result_type = qualify_type (type2, type1);
4898 else if (code1 == POINTER_TYPE && code2 == POINTER_TYPE)
4900 if (comp_target_types (type1, type2, 1))
4901 result_type = common_type (type1, type2);
4902 else if (TYPE_MAIN_VARIANT (TREE_TYPE (type1)) == void_type_node)
4904 if (pedantic && TREE_CODE (type2) == FUNCTION_TYPE)
4905 pedwarn ("ANSI C++ forbids conditional expr between `void *' and function pointer");
4906 result_type = qualify_type (type1, type2);
4908 else if (TYPE_MAIN_VARIANT (TREE_TYPE (type2)) == void_type_node)
4910 if (pedantic && TREE_CODE (type1) == FUNCTION_TYPE)
4911 pedwarn ("ANSI C++ forbids conditional expr between `void *' and function pointer");
4912 result_type = qualify_type (type2, type1);
4915 else if (comptypes (type2, type1, 0))
4916 result_type = type2;
4917 else if (IS_AGGR_TYPE (TREE_TYPE (type1))
4918 && IS_AGGR_TYPE (TREE_TYPE (type2))
4919 && (result_type = common_base_type (TREE_TYPE (type1), TREE_TYPE (type2))))
4921 if (result_type == error_mark_node)
4923 cp_error ("common base type of types `%T' and `%T' is ambiguous",
4924 TREE_TYPE (type1), TREE_TYPE (type2));
4925 result_type = ptr_type_node;
4930 && result_type != TREE_TYPE (type1)
4931 && result_type != TREE_TYPE (type2))
4932 cp_pedwarn ("`%T' and `%T' converted to `%T *' in conditional expression",
4933 type1, type2, result_type);
4935 result_type = build_pointer_type (result_type);
4940 pedwarn ("pointer type mismatch in conditional expression");
4941 result_type = ptr_type_node;
4944 else if (code1 == POINTER_TYPE && code2 == INTEGER_TYPE)
4946 pedwarn ("pointer/integer type mismatch in conditional expression");
4947 result_type = type1;
4949 else if (code2 == POINTER_TYPE && code1 == INTEGER_TYPE)
4951 pedwarn ("pointer/integer type mismatch in conditional expression");
4952 result_type = type2;
4957 /* The match does not look good. If either is
4958 an aggregate value, try converting to a scalar type. */
4959 if (code1 == RECORD_TYPE && code2 == RECORD_TYPE)
4961 cp_error ("aggregate mismatch in conditional expression: `%T' vs `%T'", type1, type2);
4962 return error_mark_node;
4964 /* Warning: this code assumes that conversion between cv-variants of
4965 a type is done using NOP_EXPRs. */
4966 if (code1 == RECORD_TYPE && TYPE_HAS_CONVERSION (type1))
4968 /* There are other types besides pointers and records. */
4970 if (code2 == POINTER_TYPE)
4971 tmp = build_pointer_type
4972 (build_type_variant (TREE_TYPE (type2), 1, 1));
4975 tmp = build_type_conversion (CONVERT_EXPR, tmp, op1, 0);
4976 if (tmp == NULL_TREE)
4978 cp_error ("incompatible types `%T' and `%T' in `?:'",
4980 return error_mark_node;
4982 if (tmp == error_mark_node)
4983 error ("ambiguous pointer conversion");
4986 result_type = common_type (type2, TREE_TYPE (tmp));
4989 else if (code2 == RECORD_TYPE && TYPE_HAS_CONVERSION (type2))
4992 if (code1 == POINTER_TYPE)
4993 tmp = build_pointer_type
4994 (build_type_variant (TREE_TYPE (type1), 1, 1));
4998 tmp = build_type_conversion (CONVERT_EXPR, tmp, op2, 0);
4999 if (tmp == NULL_TREE)
5001 cp_error ("incompatible types `%T' and `%T' in `?:'",
5003 return error_mark_node;
5005 if (tmp == error_mark_node)
5006 error ("ambiguous pointer conversion");
5009 result_type = common_type (type1, TREE_TYPE (tmp));
5012 else if (flag_cond_mismatch)
5013 result_type = void_type_node;
5016 error ("type mismatch in conditional expression");
5017 return error_mark_node;
5021 if (TREE_CODE (result_type) == POINTER_TYPE
5022 && TREE_CODE (TREE_TYPE (result_type)) == METHOD_TYPE)
5023 result_type = build_ptrmemfunc_type (result_type);
5025 if (result_type != TREE_TYPE (op1))
5026 op1 = convert_and_check (result_type, op1);
5027 if (result_type != TREE_TYPE (op2))
5028 op2 = convert_and_check (result_type, op2);
5030 if (TREE_CONSTANT (ifexp))
5031 return integer_zerop (ifexp) ? op2 : op1;
5033 return convert_from_reference
5034 (fold (build (COND_EXPR, result_type, ifexp, op1, op2)));
5037 /* Handle overloading of the ',' operator when needed. Otherwise,
5038 this function just builds an expression list. */
5041 build_x_compound_expr (list)
5044 tree rest = TREE_CHAIN (list);
5047 if (processing_template_decl)
5048 return build_min_nt (COMPOUND_EXPR, list, NULL_TREE);
5050 if (rest == NULL_TREE)
5051 return build_compound_expr (list);
5053 result = build_opfncall (COMPOUND_EXPR, LOOKUP_NORMAL,
5054 TREE_VALUE (list), TREE_VALUE (rest), NULL_TREE);
5056 return build_x_compound_expr (tree_cons (NULL_TREE, result, TREE_CHAIN (rest)));
5058 if (! TREE_SIDE_EFFECTS (TREE_VALUE (list)))
5060 /* the left-hand operand of a comma expression is like an expression
5061 statement: we should warn if it doesn't have any side-effects,
5062 unless it was explicitly cast to (void). */
5063 if ((extra_warnings || warn_unused)
5064 && !(TREE_CODE (TREE_VALUE(list)) == CONVERT_EXPR
5065 && TREE_TYPE (TREE_VALUE(list)) == void_type_node))
5066 warning("left-hand operand of comma expression has no effect");
5068 #if 0 /* this requires a gcc backend patch to export warn_if_unused_value */
5069 else if (warn_unused)
5070 warn_if_unused_value (TREE_VALUE(list));
5073 return build_compound_expr (tree_cons (NULL_TREE, TREE_VALUE (list),
5074 build_tree_list (NULL_TREE, build_x_compound_expr (rest))));
5077 /* Given a list of expressions, return a compound expression
5078 that performs them all and returns the value of the last of them. */
5081 build_compound_expr (list)
5086 if (TREE_READONLY_DECL_P (TREE_VALUE (list)))
5087 TREE_VALUE (list) = decl_constant_value (TREE_VALUE (list));
5089 if (TREE_CHAIN (list) == 0)
5091 /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
5092 Strip such NOP_EXPRs, since LIST is used in non-lvalue context. */
5093 if (TREE_CODE (list) == NOP_EXPR
5094 && TREE_TYPE (list) == TREE_TYPE (TREE_OPERAND (list, 0)))
5095 list = TREE_OPERAND (list, 0);
5097 /* Convert arrays to pointers. */
5098 if (TREE_CODE (TREE_TYPE (TREE_VALUE (list))) == ARRAY_TYPE)
5099 return default_conversion (TREE_VALUE (list));
5101 return TREE_VALUE (list);
5104 rest = build_compound_expr (TREE_CHAIN (list));
5106 /* When pedantic, a compound expression cannot be a constant expression. */
5107 if (! TREE_SIDE_EFFECTS (TREE_VALUE (list)) && ! pedantic)
5110 return build (COMPOUND_EXPR, TREE_TYPE (rest),
5111 break_out_cleanups (TREE_VALUE (list)), rest);
5115 build_static_cast (type, expr)
5121 if (type == error_mark_node || expr == error_mark_node)
5122 return error_mark_node;
5124 if (TREE_CODE (expr) == OFFSET_REF)
5125 expr = resolve_offset_ref (expr);
5127 if (processing_template_decl)
5129 tree t = build_min (STATIC_CAST_EXPR, type, expr);
5133 /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
5134 Strip such NOP_EXPRs if VALUE is being used in non-lvalue context. */
5135 if (TREE_CODE (type) != REFERENCE_TYPE
5136 && TREE_CODE (expr) == NOP_EXPR
5137 && TREE_TYPE (expr) == TREE_TYPE (TREE_OPERAND (expr, 0)))
5138 expr = TREE_OPERAND (expr, 0);
5140 if (TREE_CODE (type) == VOID_TYPE)
5141 return build1 (CONVERT_EXPR, type, expr);
5143 if (type_unknown_p (expr))
5145 expr = instantiate_type (type, expr, 1);
5146 if (expr == error_mark_node)
5147 return error_mark_node;
5150 if (TREE_CODE (type) == REFERENCE_TYPE)
5151 return (convert_from_reference
5152 (convert_to_reference (type, expr, CONV_STATIC|CONV_IMPLICIT,
5153 LOOKUP_COMPLAIN, NULL_TREE)));
5155 if (IS_AGGR_TYPE (type))
5156 return build_cplus_new
5157 (type, (build_method_call
5158 (NULL_TREE, ctor_identifier, build_tree_list (NULL_TREE, expr),
5159 TYPE_BINFO (type), LOOKUP_NORMAL)));
5161 expr = decay_conversion (expr);
5162 intype = TREE_TYPE (expr);
5164 /* FIXME handle casting to array type. */
5167 if (can_convert_arg (type, intype, expr))
5169 else if (TYPE_PTROB_P (type) && TYPE_PTROB_P (intype))
5172 if (IS_AGGR_TYPE (TREE_TYPE (type)) && IS_AGGR_TYPE (TREE_TYPE (intype))
5173 && (TYPE_READONLY (TREE_TYPE (type))
5174 >= TYPE_READONLY (TREE_TYPE (intype)))
5175 && (TYPE_VOLATILE (TREE_TYPE (type))
5176 >= TYPE_VOLATILE (TREE_TYPE (intype)))
5177 && (binfo = get_binfo (TREE_TYPE (intype), TREE_TYPE (type), 0))
5178 && ! TREE_VIA_VIRTUAL (binfo))
5181 else if (TYPE_PTRMEM_P (type) && TYPE_PTRMEM_P (intype))
5183 if (comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (type))),
5184 TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (intype))), 1)
5185 && (TYPE_READONLY (TREE_TYPE (TREE_TYPE (type)))
5186 >= TYPE_READONLY (TREE_TYPE (TREE_TYPE (intype))))
5187 && (TYPE_VOLATILE (TREE_TYPE (TREE_TYPE (type)))
5188 >= TYPE_VOLATILE (TREE_TYPE (TREE_TYPE (intype))))
5189 && (binfo = get_binfo (TYPE_OFFSET_BASETYPE (intype),
5190 TYPE_OFFSET_BASETYPE (type), 0))
5191 && ! TREE_VIA_VIRTUAL (binfo))
5194 else if (TREE_CODE (intype) != BOOLEAN_TYPE
5195 && TREE_CODE (type) != ARRAY_TYPE
5196 && TREE_CODE (type) != FUNCTION_TYPE
5197 && can_convert (intype, type))
5201 return build_c_cast (type, expr);
5203 cp_error ("static_cast from `%T' to `%T'", intype, type);
5204 return error_mark_node;
5208 build_reinterpret_cast (type, expr)
5213 if (type == error_mark_node || expr == error_mark_node)
5214 return error_mark_node;
5216 if (TREE_CODE (expr) == OFFSET_REF)
5217 expr = resolve_offset_ref (expr);
5219 if (processing_template_decl)
5221 tree t = build_min (REINTERPRET_CAST_EXPR, type, expr);
5225 if (TREE_CODE (type) != REFERENCE_TYPE)
5227 expr = decay_conversion (expr);
5229 /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
5230 Strip such NOP_EXPRs if VALUE is being used in non-lvalue context. */
5231 if (TREE_CODE (expr) == NOP_EXPR
5232 && TREE_TYPE (expr) == TREE_TYPE (TREE_OPERAND (expr, 0)))
5233 expr = TREE_OPERAND (expr, 0);
5236 if (type_unknown_p (expr))
5238 expr = instantiate_type (type, expr, 1);
5239 if (expr == error_mark_node)
5240 return error_mark_node;
5243 intype = TREE_TYPE (expr);
5245 if (TREE_CODE (type) == REFERENCE_TYPE)
5247 if (! real_lvalue_p (expr))
5249 cp_error ("reinterpret_cast from `%T' rvalue to `%T'", intype, type);
5250 return error_mark_node;
5252 expr = build_unary_op (ADDR_EXPR, expr, 0);
5253 if (expr != error_mark_node)
5254 expr = build_reinterpret_cast
5255 (build_pointer_type (TREE_TYPE (type)), expr);
5256 if (expr != error_mark_node)
5257 expr = build_indirect_ref (expr, 0);
5260 else if (comptypes (TYPE_MAIN_VARIANT (intype), TYPE_MAIN_VARIANT (type), 1))
5261 return build_static_cast (type, expr);
5263 if (TYPE_PTR_P (type) && (TREE_CODE (intype) == INTEGER_TYPE
5264 || TREE_CODE (intype) == ENUMERAL_TYPE))
5266 else if (TREE_CODE (type) == INTEGER_TYPE && TYPE_PTR_P (intype))
5268 if (TYPE_PRECISION (type) < TYPE_PRECISION (intype))
5269 cp_pedwarn ("reinterpret_cast from `%T' to `%T' loses precision",
5272 else if ((TYPE_PTRFN_P (type) && TYPE_PTRFN_P (intype))
5273 || (TYPE_PTRMEMFUNC_P (type) && TYPE_PTRMEMFUNC_P (intype)))
5275 if (TREE_READONLY_DECL_P (expr))
5276 expr = decl_constant_value (expr);
5277 return fold (build1 (NOP_EXPR, type, expr));
5279 else if ((TYPE_PTRMEM_P (type) && TYPE_PTRMEM_P (intype))
5280 || (TYPE_PTROBV_P (type) && TYPE_PTROBV_P (intype)))
5282 if (! comp_ptr_ttypes_reinterpret (TREE_TYPE (type), TREE_TYPE (intype)))
5283 cp_pedwarn ("reinterpret_cast from `%T' to `%T' casts away const (or volatile)",
5286 if (TREE_READONLY_DECL_P (expr))
5287 expr = decl_constant_value (expr);
5288 return fold (build1 (NOP_EXPR, type, expr));
5292 cp_error ("reinterpret_cast from `%T' to `%T'", intype, type);
5293 return error_mark_node;
5296 return convert (type, expr);
5300 build_const_cast (type, expr)
5305 if (type == error_mark_node || expr == error_mark_node)
5306 return error_mark_node;
5308 if (TREE_CODE (expr) == OFFSET_REF)
5309 expr = resolve_offset_ref (expr);
5311 if (processing_template_decl)
5313 tree t = build_min (CONST_CAST_EXPR, type, expr);
5317 if (TREE_CODE (type) != REFERENCE_TYPE)
5319 expr = decay_conversion (expr);
5321 /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
5322 Strip such NOP_EXPRs if VALUE is being used in non-lvalue context. */
5323 if (TREE_CODE (expr) == NOP_EXPR
5324 && TREE_TYPE (expr) == TREE_TYPE (TREE_OPERAND (expr, 0)))
5325 expr = TREE_OPERAND (expr, 0);
5328 if (type_unknown_p (expr))
5330 expr = instantiate_type (type, expr, 1);
5331 if (expr == error_mark_node)
5332 return error_mark_node;
5335 intype = TREE_TYPE (expr);
5337 if (comptypes (TYPE_MAIN_VARIANT (intype), TYPE_MAIN_VARIANT (type), 1))
5338 return build_static_cast (type, expr);
5339 else if (TREE_CODE (type) == REFERENCE_TYPE)
5341 if (! real_lvalue_p (expr))
5343 cp_error ("const_cast from `%T' rvalue to `%T'", intype, type);
5344 return error_mark_node;
5347 if (comp_ptr_ttypes_const (TREE_TYPE (type), intype))
5348 return (convert_from_reference
5349 (convert_to_reference (type, expr, CONV_CONST|CONV_IMPLICIT,
5350 LOOKUP_COMPLAIN, NULL_TREE)));
5352 else if (TREE_CODE (type) == POINTER_TYPE
5353 && TREE_CODE (intype) == POINTER_TYPE
5354 && comp_ptr_ttypes_const (TREE_TYPE (type), TREE_TYPE (intype)))
5355 return convert (type, expr);
5357 cp_error ("const_cast from `%T' to `%T'", intype, type);
5358 return error_mark_node;
5361 /* Build an expression representing a cast to type TYPE of expression EXPR.
5363 ALLOW_NONCONVERTING is true if we should allow non-converting constructors
5364 when doing the cast. */
5367 build_c_cast (type, expr)
5371 register tree value = expr;
5373 if (type == error_mark_node || expr == error_mark_node)
5374 return error_mark_node;
5376 /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
5377 Strip such NOP_EXPRs if VALUE is being used in non-lvalue context. */
5378 if (TREE_CODE (type) != REFERENCE_TYPE
5379 && TREE_CODE (value) == NOP_EXPR
5380 && TREE_TYPE (value) == TREE_TYPE (TREE_OPERAND (value, 0)))
5381 value = TREE_OPERAND (value, 0);
5383 if (TREE_TYPE (expr)
5384 && TREE_CODE (TREE_TYPE (expr)) == OFFSET_TYPE
5385 && TREE_CODE (type) != OFFSET_TYPE)
5386 value = resolve_offset_ref (value);
5388 if (TREE_CODE (type) == ARRAY_TYPE)
5390 /* Allow casting from T1* to T2[] because Cfront allows it.
5391 NIHCL uses it. It is not valid ANSI C however, and hence, not
5393 if (TREE_CODE (TREE_TYPE (expr)) == POINTER_TYPE)
5396 pedwarn ("ANSI C++ forbids casting to an array type");
5397 type = build_pointer_type (TREE_TYPE (type));
5401 error ("ANSI C++ forbids casting to an array type");
5402 return error_mark_node;
5406 if (TREE_CODE (type) == FUNCTION_TYPE
5407 || TREE_CODE (type) == METHOD_TYPE)
5409 cp_error ("casting to function type `%T'", type);
5410 return error_mark_node;
5413 if (IS_SIGNATURE (type))
5415 error ("cast specifies signature type");
5416 return error_mark_node;
5419 if (processing_template_decl)
5421 tree t = build_min (CAST_EXPR, type,
5422 min_tree_cons (NULL_TREE, value, NULL_TREE));
5426 if (TREE_CODE (type) == VOID_TYPE)
5427 value = build1 (CONVERT_EXPR, type, value);
5428 else if (TREE_TYPE (value) == NULL_TREE
5429 || type_unknown_p (value))
5431 value = instantiate_type (type, value, 1);
5433 if (value == error_mark_node)
5434 return error_mark_node;
5441 /* Convert functions and arrays to pointers and
5442 convert references to their expanded types,
5443 but don't convert any other types. */
5444 if (TREE_CODE (TREE_TYPE (value)) == FUNCTION_TYPE
5445 || (TREE_CODE (TREE_TYPE (value)) == METHOD_TYPE
5446 /* Don't do the default conversion if we want a
5447 pointer to a function. */
5448 && TREE_CODE (type) != POINTER_TYPE
5449 && TREE_CODE (TREE_TYPE (type)) != FUNCTION_TYPE)
5450 || TREE_CODE (TREE_TYPE (value)) == ARRAY_TYPE
5451 || TREE_CODE (TREE_TYPE (value)) == REFERENCE_TYPE)
5452 value = default_conversion (value);
5453 otype = TREE_TYPE (value);
5455 /* Optionally warn about potentially worrisome casts. */
5458 && TREE_CODE (type) == POINTER_TYPE
5459 && TREE_CODE (otype) == POINTER_TYPE)
5461 /* For C++ we make these regular warnings, rather than
5462 softening them into pedwarns. */
5463 if (TYPE_VOLATILE (TREE_TYPE (otype))
5464 && ! TYPE_VOLATILE (TREE_TYPE (type)))
5465 warning ("cast discards `volatile' from pointer target type");
5466 if (TYPE_READONLY (TREE_TYPE (otype))
5467 && ! TYPE_READONLY (TREE_TYPE (type)))
5468 warning ("cast discards `const' from pointer target type");
5471 /* Warn about possible alignment problems. */
5472 if (STRICT_ALIGNMENT && warn_cast_align
5473 && TREE_CODE (type) == POINTER_TYPE
5474 && TREE_CODE (otype) == POINTER_TYPE
5475 && TREE_CODE (TREE_TYPE (otype)) != VOID_TYPE
5476 && TREE_CODE (TREE_TYPE (otype)) != FUNCTION_TYPE
5477 && TYPE_ALIGN (TREE_TYPE (type)) > TYPE_ALIGN (TREE_TYPE (otype)))
5478 warning ("cast increases required alignment of target type");
5481 /* We should see about re-enabling these, they seem useful to
5483 if (TREE_CODE (type) == INTEGER_TYPE
5484 && TREE_CODE (otype) == POINTER_TYPE
5485 && TYPE_PRECISION (type) != TYPE_PRECISION (otype))
5486 warning ("cast from pointer to integer of different size");
5488 if (TREE_CODE (type) == POINTER_TYPE
5489 && TREE_CODE (otype) == INTEGER_TYPE
5490 && TYPE_PRECISION (type) != TYPE_PRECISION (otype)
5491 /* Don't warn about converting 0 to pointer,
5492 provided the 0 was explicit--not cast or made by folding. */
5493 && !(TREE_CODE (value) == INTEGER_CST && integer_zerop (value)))
5494 warning ("cast to pointer from integer of different size");
5497 if (TREE_CODE (type) == REFERENCE_TYPE)
5498 value = (convert_from_reference
5499 (convert_to_reference (type, value, CONV_C_CAST,
5500 LOOKUP_COMPLAIN, NULL_TREE)));
5505 if (TREE_READONLY_DECL_P (value))
5506 value = decl_constant_value (value);
5509 value = convert_force (type, value, flag);
5511 /* Ignore any integer overflow caused by the cast. */
5512 if (TREE_CODE (value) == INTEGER_CST)
5514 TREE_OVERFLOW (value) = TREE_OVERFLOW (ovalue);
5515 TREE_CONSTANT_OVERFLOW (value) = TREE_CONSTANT_OVERFLOW (ovalue);
5520 /* Always produce some operator for an explicit cast,
5521 so we can tell (for -pedantic) that the cast is no lvalue. */
5522 if (TREE_CODE (type) != REFERENCE_TYPE && value == expr
5523 && real_lvalue_p (value))
5524 value = non_lvalue (value);
5530 expand_target_expr (t)
5533 extern int temp_slot_level;
5534 extern int target_temp_slot_level;
5535 int old_temp_level = target_temp_slot_level;
5537 tree xval = make_node (RTL_EXPR);
5540 /* Any TARGET_EXPR temps live only as long as the outer temp level.
5541 Since they are preserved in this new inner level, we know they
5542 will make it into the outer level. */
5544 target_temp_slot_level = temp_slot_level;
5546 do_pending_stack_adjust ();
5547 start_sequence_for_rtl_expr (xval);
5549 rtxval = expand_expr (t, NULL_RTX, VOIDmode, 0);
5550 do_pending_stack_adjust ();
5551 TREE_SIDE_EFFECTS (xval) = 1;
5552 RTL_EXPR_SEQUENCE (xval) = get_insns ();
5554 RTL_EXPR_RTL (xval) = rtxval;
5555 TREE_TYPE (xval) = TREE_TYPE (t);
5558 target_temp_slot_level = old_temp_level;
5563 /* Build an assignment expression of lvalue LHS from value RHS.
5564 MODIFYCODE is the code for a binary operator that we use
5565 to combine the old value of LHS with RHS to get the new value.
5566 Or else MODIFYCODE is NOP_EXPR meaning do a simple assignment.
5568 C++: If MODIFYCODE is INIT_EXPR, then leave references unbashed. */
5571 build_modify_expr (lhs, modifycode, rhs)
5573 enum tree_code modifycode;
5576 register tree result;
5578 tree lhstype = TREE_TYPE (lhs);
5579 tree olhstype = lhstype;
5582 /* Avoid duplicate error messages from operands that had errors. */
5583 if (TREE_CODE (lhs) == ERROR_MARK || TREE_CODE (rhs) == ERROR_MARK)
5584 return error_mark_node;
5586 /* Types that aren't fully specified cannot be used in assignments. */
5587 lhs = require_complete_type (lhs);
5591 /* Handle assignment to signature pointers/refs. */
5593 if (TYPE_LANG_SPECIFIC (lhstype) &&
5594 (IS_SIGNATURE_POINTER (lhstype) || IS_SIGNATURE_REFERENCE (lhstype)))
5596 return build_signature_pointer_constructor (lhs, rhs);
5599 /* Handle control structure constructs used as "lvalues". */
5601 switch (TREE_CODE (lhs))
5603 /* Handle --foo = 5; as these are valid constructs in C++ */
5604 case PREDECREMENT_EXPR:
5605 case PREINCREMENT_EXPR:
5606 if (TREE_SIDE_EFFECTS (TREE_OPERAND (lhs, 0)))
5607 lhs = build (TREE_CODE (lhs), TREE_TYPE (lhs),
5608 stabilize_reference (TREE_OPERAND (lhs, 0)),
5609 TREE_OPERAND (lhs, 1));
5610 return build (COMPOUND_EXPR, lhstype,
5612 build_modify_expr (TREE_OPERAND (lhs, 0),
5615 /* Handle (a, b) used as an "lvalue". */
5617 newrhs = build_modify_expr (TREE_OPERAND (lhs, 1),
5619 if (TREE_CODE (newrhs) == ERROR_MARK)
5620 return error_mark_node;
5621 return build (COMPOUND_EXPR, lhstype,
5622 TREE_OPERAND (lhs, 0), newrhs);
5625 newrhs = build_modify_expr (TREE_OPERAND (lhs, 0), modifycode, rhs);
5626 if (TREE_CODE (newrhs) == ERROR_MARK)
5627 return error_mark_node;
5628 return build (COMPOUND_EXPR, lhstype, lhs, newrhs);
5630 /* Handle (a ? b : c) used as an "lvalue". */
5632 rhs = save_expr (rhs);
5634 /* Produce (a ? (b = rhs) : (c = rhs))
5635 except that the RHS goes through a save-expr
5636 so the code to compute it is only emitted once. */
5638 = build_conditional_expr (TREE_OPERAND (lhs, 0),
5639 build_modify_expr (convert (TREE_TYPE (lhs), TREE_OPERAND (lhs, 1)),
5641 build_modify_expr (convert (TREE_TYPE (lhs), TREE_OPERAND (lhs, 2)),
5643 if (TREE_CODE (cond) == ERROR_MARK)
5645 /* Make sure the code to compute the rhs comes out
5646 before the split. */
5647 return build (COMPOUND_EXPR, TREE_TYPE (lhs),
5648 /* Case to void to suppress warning
5649 from warn_if_unused_value. */
5650 convert (void_type_node, rhs), cond);
5654 if (TREE_CODE (lhs) == OFFSET_REF)
5656 if (TREE_OPERAND (lhs, 0) == NULL_TREE)
5658 /* Static class member? */
5659 tree member = TREE_OPERAND (lhs, 1);
5660 if (TREE_CODE (member) == VAR_DECL)
5664 compiler_error ("invalid static class member");
5665 return error_mark_node;
5669 lhs = resolve_offset_ref (lhs);
5671 olhstype = lhstype = TREE_TYPE (lhs);
5674 if (TREE_CODE (lhstype) == REFERENCE_TYPE
5675 && modifycode != INIT_EXPR)
5677 lhs = convert_from_reference (lhs);
5678 olhstype = lhstype = TREE_TYPE (lhs);
5681 /* If a binary op has been requested, combine the old LHS value with the RHS
5682 producing the value we should actually store into the LHS. */
5684 if (modifycode == INIT_EXPR)
5686 if (! IS_AGGR_TYPE (lhstype))
5687 /* Do the default thing */;
5688 else if (! TYPE_HAS_CONSTRUCTOR (lhstype))
5690 cp_error ("`%T' has no constructors", lhstype);
5691 return error_mark_node;
5693 else if (TYPE_HAS_TRIVIAL_INIT_REF (lhstype)
5694 && TYPE_MAIN_VARIANT (lhstype) == TYPE_MAIN_VARIANT (TREE_TYPE (newrhs)))
5695 /* Do the default thing */;
5698 result = build_method_call (lhs, ctor_identifier,
5699 build_tree_list (NULL_TREE, rhs),
5700 TYPE_BINFO (lhstype), LOOKUP_NORMAL);
5701 if (result == NULL_TREE)
5702 return error_mark_node;
5706 else if (modifycode == NOP_EXPR)
5708 /* `operator=' is not an inheritable operator. */
5709 if (! IS_AGGR_TYPE (lhstype))
5710 /* Do the default thing */;
5711 else if (! TYPE_HAS_ASSIGNMENT (lhstype))
5713 cp_error ("`%T' does not define operator=", lhstype);
5714 return error_mark_node;
5716 else if (TYPE_HAS_TRIVIAL_ASSIGN_REF (lhstype)
5717 && TYPE_MAIN_VARIANT (lhstype) == TYPE_MAIN_VARIANT (TREE_TYPE (newrhs)))
5719 build_opfncall (MODIFY_EXPR, LOOKUP_NORMAL,
5720 lhs, rhs, make_node (NOP_EXPR));
5722 /* Do the default thing */;
5726 result = build_opfncall (MODIFY_EXPR, LOOKUP_NORMAL,
5727 lhs, rhs, make_node (NOP_EXPR));
5728 if (result == NULL_TREE)
5729 return error_mark_node;
5734 else if (PROMOTES_TO_AGGR_TYPE (lhstype, REFERENCE_TYPE))
5736 /* This case must convert to some sort of lvalue that
5737 can participate in an op= operation. */
5740 if (build_default_binary_type_conversion (modifycode, &lhs_tmp, &rhs_tmp))
5742 lhs = stabilize_reference (lhs_tmp);
5743 /* Forget it was ever anything else. */
5744 olhstype = lhstype = TREE_TYPE (lhs);
5745 newrhs = build_binary_op (modifycode, lhs, rhs_tmp, 1);
5749 cp_error ("no match for `%Q(%#T, %#T)'", modifycode,
5750 TREE_TYPE (lhs), TREE_TYPE (rhs));
5751 return error_mark_node;
5756 lhs = stabilize_reference (lhs);
5757 newrhs = build_binary_op (modifycode, lhs, rhs, 1);
5758 if (newrhs == error_mark_node)
5760 cp_error (" in evaluation of `%Q(%#T, %#T)'", modifycode,
5761 TREE_TYPE (lhs), TREE_TYPE (rhs));
5762 return error_mark_node;
5766 /* Handle a cast used as an "lvalue".
5767 We have already performed any binary operator using the value as cast.
5768 Now convert the result to the cast type of the lhs,
5769 and then true type of the lhs and store it there;
5770 then convert result back to the cast type to be the value
5771 of the assignment. */
5773 switch (TREE_CODE (lhs))
5778 case FIX_TRUNC_EXPR:
5779 case FIX_FLOOR_EXPR:
5780 case FIX_ROUND_EXPR:
5782 if (TREE_CODE (TREE_TYPE (newrhs)) == ARRAY_TYPE
5783 || TREE_CODE (TREE_TYPE (newrhs)) == FUNCTION_TYPE
5784 || TREE_CODE (TREE_TYPE (newrhs)) == METHOD_TYPE
5785 || TREE_CODE (TREE_TYPE (newrhs)) == OFFSET_TYPE)
5786 newrhs = default_conversion (newrhs);
5788 tree inner_lhs = TREE_OPERAND (lhs, 0);
5790 if (! lvalue_p (lhs) && pedantic)
5791 pedwarn ("cast to non-reference type used as lvalue");
5793 result = build_modify_expr (inner_lhs, NOP_EXPR,
5794 convert (TREE_TYPE (inner_lhs),
5795 convert (lhstype, newrhs)));
5796 if (TREE_CODE (result) == ERROR_MARK)
5798 return convert (TREE_TYPE (lhs), result);
5802 /* Now we have handled acceptable kinds of LHS that are not truly lvalues.
5803 Reject anything strange now. */
5805 if (!lvalue_or_else (lhs, "assignment"))
5806 return error_mark_node;
5808 GNU_xref_assign (lhs);
5810 /* Warn about storing in something that is `const'. */
5811 /* For C++, don't warn if this is initialization. */
5812 if (modifycode != INIT_EXPR
5813 /* For assignment to `const' signature pointer/reference fields,
5814 don't warn either, we already printed a better message before. */
5815 && ! (TREE_CODE (lhs) == COMPONENT_REF
5816 && (IS_SIGNATURE_POINTER (TREE_TYPE (TREE_OPERAND (lhs, 0)))
5817 || IS_SIGNATURE_REFERENCE (TREE_TYPE (TREE_OPERAND (lhs, 0)))))
5818 && (TREE_READONLY (lhs) || TYPE_READONLY (lhstype)
5819 || ((TREE_CODE (lhstype) == RECORD_TYPE
5820 || TREE_CODE (lhstype) == UNION_TYPE)
5821 && C_TYPE_FIELDS_READONLY (lhstype))
5822 || (TREE_CODE (lhstype) == REFERENCE_TYPE
5823 && TYPE_READONLY (TREE_TYPE (lhstype)))))
5824 readonly_error (lhs, "assignment", 0);
5826 /* If storing into a structure or union member,
5827 it has probably been given type `int'.
5828 Compute the type that would go with
5829 the actual amount of storage the member occupies. */
5831 if (TREE_CODE (lhs) == COMPONENT_REF
5832 && (TREE_CODE (lhstype) == INTEGER_TYPE
5833 || TREE_CODE (lhstype) == REAL_TYPE
5834 || TREE_CODE (lhstype) == ENUMERAL_TYPE))
5836 lhstype = TREE_TYPE (get_unwidened (lhs, 0));
5838 /* If storing in a field that is in actuality a short or narrower
5839 than one, we must store in the field in its actual type. */
5841 if (lhstype != TREE_TYPE (lhs))
5843 lhs = copy_node (lhs);
5844 TREE_TYPE (lhs) = lhstype;
5848 /* check to see if there is an assignment to `this' */
5849 if (lhs == current_class_ptr)
5851 if (flag_this_is_variable > 0
5852 && DECL_NAME (current_function_decl) != NULL_TREE
5853 && (DECL_NAME (current_function_decl)
5854 != constructor_name (current_class_type)))
5855 warning ("assignment to `this' not in constructor or destructor");
5856 current_function_just_assigned_this = 1;
5859 /* The TREE_TYPE of RHS may be TYPE_UNKNOWN. This can happen
5860 when the type of RHS is not yet known, i.e. its type
5861 is inherited from LHS. */
5862 rhs = require_instantiated_type (lhstype, newrhs, error_mark_node);
5863 if (rhs == error_mark_node)
5864 return error_mark_node;
5867 if (modifycode != INIT_EXPR)
5869 /* Make modifycode now either a NOP_EXPR or an INIT_EXPR. */
5870 modifycode = NOP_EXPR;
5871 /* Reference-bashing */
5872 if (TREE_CODE (lhstype) == REFERENCE_TYPE)
5874 tree tmp = convert_from_reference (lhs);
5875 lhstype = TREE_TYPE (tmp);
5876 if (TYPE_SIZE (lhstype) == 0)
5878 incomplete_type_error (lhs, lhstype);
5879 return error_mark_node;
5884 if (TREE_CODE (TREE_TYPE (newrhs)) == REFERENCE_TYPE)
5886 tree tmp = convert_from_reference (newrhs);
5887 if (TYPE_SIZE (TREE_TYPE (tmp)) == 0)
5889 incomplete_type_error (newrhs, TREE_TYPE (tmp));
5890 return error_mark_node;
5896 if (TREE_SIDE_EFFECTS (lhs))
5897 lhs = stabilize_reference (lhs);
5898 if (TREE_SIDE_EFFECTS (newrhs))
5899 newrhs = stabilize_reference (newrhs);
5901 /* Convert new value to destination type. */
5903 if (TREE_CODE (lhstype) == ARRAY_TYPE)
5907 if (! comptypes (lhstype, TREE_TYPE (rhs), 0))
5909 cp_error ("incompatible types in assignment of `%T' to `%T'",
5910 TREE_TYPE (rhs), lhstype);
5911 return error_mark_node;
5914 /* Allow array assignment in compiler-generated code. */
5915 if (pedantic && ! DECL_ARTIFICIAL (current_function_decl))
5916 pedwarn ("ANSI C++ forbids assignment of arrays");
5918 /* Have to wrap this in RTL_EXPR for two cases:
5919 in base or member initialization and if we
5920 are a branch of a ?: operator. Since we
5921 can't easily know the latter, just do it always. */
5923 result = make_node (RTL_EXPR);
5925 TREE_TYPE (result) = void_type_node;
5926 do_pending_stack_adjust ();
5927 start_sequence_for_rtl_expr (result);
5929 /* As a matter of principle, `start_sequence' should do this. */
5932 from_array = TREE_CODE (TREE_TYPE (newrhs)) == ARRAY_TYPE
5933 ? 1 + (modifycode != INIT_EXPR): 0;
5934 expand_vec_init (lhs, lhs, array_type_nelts (lhstype), newrhs,
5937 do_pending_stack_adjust ();
5939 TREE_SIDE_EFFECTS (result) = 1;
5940 RTL_EXPR_SEQUENCE (result) = get_insns ();
5941 RTL_EXPR_RTL (result) = const0_rtx;
5946 if (modifycode == INIT_EXPR)
5948 newrhs = convert_for_initialization (lhs, lhstype, newrhs, LOOKUP_NORMAL,
5949 "assignment", NULL_TREE, 0);
5950 if (lhs == DECL_RESULT (current_function_decl))
5952 if (DECL_INITIAL (lhs))
5953 warning ("return value from function receives multiple initializations");
5954 DECL_INITIAL (lhs) = newrhs;
5959 /* Avoid warnings on enum bit fields. */
5960 if (TREE_CODE (olhstype) == ENUMERAL_TYPE
5961 && TREE_CODE (lhstype) == INTEGER_TYPE)
5963 newrhs = convert_for_assignment (olhstype, newrhs, "assignment",
5965 newrhs = convert_force (lhstype, newrhs, 0);
5968 newrhs = convert_for_assignment (lhstype, newrhs, "assignment",
5970 if (TREE_CODE (newrhs) == CALL_EXPR
5971 && TYPE_NEEDS_CONSTRUCTING (lhstype))
5972 newrhs = build_cplus_new (lhstype, newrhs);
5974 /* Can't initialize directly from a TARGET_EXPR, since that would
5975 cause the lhs to be constructed twice, and possibly result in
5976 accidental self-initialization. So we force the TARGET_EXPR to be
5977 expanded without a target. */
5978 if (TREE_CODE (newrhs) == TARGET_EXPR)
5979 newrhs = build (COMPOUND_EXPR, TREE_TYPE (newrhs), newrhs,
5980 TREE_OPERAND (newrhs, 0));
5983 if (TREE_CODE (newrhs) == ERROR_MARK)
5984 return error_mark_node;
5986 if (TREE_CODE (newrhs) == COND_EXPR)
5989 tree cond = TREE_OPERAND (newrhs, 0);
5991 if (TREE_SIDE_EFFECTS (lhs))
5992 cond = build_compound_expr (tree_cons
5994 build_tree_list (NULL_TREE, cond)));
5996 /* Cannot have two identical lhs on this one tree (result) as preexpand
5997 calls will rip them out and fill in RTL for them, but when the
5998 rtl is generated, the calls will only be in the first side of the
5999 condition, not on both, or before the conditional jump! (mrs) */
6000 lhs1 = break_out_calls (lhs);
6003 /* If there's no change, the COND_EXPR behaves like any other rhs. */
6004 result = build (modifycode == NOP_EXPR ? MODIFY_EXPR : INIT_EXPR,
6005 lhstype, lhs, newrhs);
6008 tree result_type = TREE_TYPE (newrhs);
6009 /* We have to convert each arm to the proper type because the
6010 types may have been munged by constant folding. */
6012 = build (COND_EXPR, result_type, cond,
6013 build_modify_expr (lhs, modifycode,
6014 convert (result_type,
6015 TREE_OPERAND (newrhs, 1))),
6016 build_modify_expr (lhs1, modifycode,
6017 convert (result_type,
6018 TREE_OPERAND (newrhs, 2))));
6022 result = build (modifycode == NOP_EXPR ? MODIFY_EXPR : INIT_EXPR,
6023 lhstype, lhs, newrhs);
6025 TREE_SIDE_EFFECTS (result) = 1;
6027 /* If we got the LHS in a different type for storing in,
6028 convert the result back to the nominal type of LHS
6029 so that the value we return always has the same type
6030 as the LHS argument. */
6032 if (olhstype == TREE_TYPE (result))
6034 /* Avoid warnings converting integral types back into enums
6035 for enum bit fields. */
6036 if (TREE_CODE (TREE_TYPE (result)) == INTEGER_TYPE
6037 && TREE_CODE (olhstype) == ENUMERAL_TYPE)
6039 result = build (COMPOUND_EXPR, olhstype, result, olhs);
6040 TREE_NO_UNUSED_WARNING (result) = 1;
6043 return convert_for_assignment (olhstype, result, "assignment",
6048 build_x_modify_expr (lhs, modifycode, rhs)
6050 enum tree_code modifycode;
6053 if (processing_template_decl)
6054 return build_min_nt (MODOP_EXPR, lhs,
6055 build_min_nt (modifycode, NULL_TREE, NULL_TREE), rhs);
6057 if (modifycode != NOP_EXPR)
6059 tree rval = build_opfncall (MODIFY_EXPR, LOOKUP_NORMAL, lhs, rhs,
6060 make_node (modifycode));
6064 return build_modify_expr (lhs, modifycode, rhs);
6067 /* Return 0 if EXP is not a valid lvalue in this language
6068 even though `lvalue_or_else' would accept it. */
6071 language_lvalue_valid (exp)
6077 /* Get difference in deltas for different pointer to member function
6078 types. Return integer_zero_node, if FROM cannot be converted to a
6079 TO type. If FORCE is true, then allow reverse conversions as well. */
6082 get_delta_difference (from, to, force)
6086 tree delta = integer_zero_node;
6092 /* Should get_base_distance here, so we can check if any thing along the
6093 path is virtual, and we need to make sure we stay
6094 inside the real binfos when going through virtual bases.
6095 Maybe we should replace virtual bases with
6096 binfo_member (...CLASSTYPE_VBASECLASSES...)... (mrs) */
6097 binfo = get_binfo (from, to, 1);
6098 if (binfo == error_mark_node)
6100 error (" in pointer to member function conversiona");
6107 error_not_base_type (from, to);
6108 error (" in pointer to member conversion");
6111 binfo = get_binfo (to, from, 1);
6112 if (binfo == error_mark_node)
6115 error (" in pointer to member conversion");
6121 cp_error ("cannot convert pointer to member of type %T to unrelated pointer to member of type %T", from, to);
6124 if (TREE_VIA_VIRTUAL (binfo))
6126 binfo = binfo_member (BINFO_TYPE (binfo),
6127 CLASSTYPE_VBASECLASSES (from));
6128 warning ("pointer to member conversion to virtual base class will only work if you are very careful");
6130 delta = BINFO_OFFSET (binfo);
6131 delta = convert (ptrdiff_type_node, delta);
6133 return build_binary_op (MINUS_EXPR,
6137 if (TREE_VIA_VIRTUAL (binfo))
6139 warning ("pointer to member conversion from virtual base class will only work if you are very careful");
6141 return BINFO_OFFSET (binfo);
6145 build_ptrmemfunc1 (type, delta, idx, pfn, delta2)
6146 tree type, delta, idx, pfn, delta2;
6151 /* This is the old way we did it. We want to avoid calling
6152 digest_init, so that it can give an error if we use { } when
6153 initializing a pointer to member function. */
6157 u = build_nt (CONSTRUCTOR, NULL_TREE,
6158 tree_cons (pfn_identifier, pfn, NULL_TREE));
6162 u = build_nt (CONSTRUCTOR, NULL_TREE,
6163 tree_cons (delta2_identifier, delta2, NULL_TREE));
6166 u = build_nt (CONSTRUCTOR, NULL_TREE,
6167 tree_cons (NULL_TREE, delta,
6168 tree_cons (NULL_TREE, idx,
6169 tree_cons (NULL_TREE, u, NULL_TREE))));
6171 return digest_init (type, u, (tree*)0);
6173 tree delta_field, idx_field, pfn_or_delta2_field, pfn_field, delta2_field;
6175 int allconstant, allsimple;
6177 delta_field = TYPE_FIELDS (type);
6178 idx_field = TREE_CHAIN (delta_field);
6179 pfn_or_delta2_field = TREE_CHAIN (idx_field);
6180 subtype = TREE_TYPE (pfn_or_delta2_field);
6181 pfn_field = TYPE_FIELDS (subtype);
6182 delta2_field = TREE_CHAIN (pfn_field);
6186 allconstant = TREE_CONSTANT (pfn);
6187 allsimple = initializer_constant_valid_p (pfn, TREE_TYPE (pfn));
6188 u = tree_cons (pfn_field, pfn, NULL_TREE);
6192 delta2 = convert_and_check (delta_type_node, delta2);
6193 allconstant = TREE_CONSTANT (delta2);
6194 allsimple = initializer_constant_valid_p (delta2, TREE_TYPE (delta2));
6195 u = tree_cons (delta2_field, delta2, NULL_TREE);
6198 delta = convert_and_check (delta_type_node, delta);
6199 idx = convert_and_check (delta_type_node, idx);
6201 allconstant = allconstant && TREE_CONSTANT (delta) && TREE_CONSTANT (idx);
6202 allsimple = allsimple
6203 && initializer_constant_valid_p (delta, TREE_TYPE (delta))
6204 && initializer_constant_valid_p (idx, TREE_TYPE (idx));
6206 u = build (CONSTRUCTOR, subtype, NULL_TREE, u);
6207 u = tree_cons (delta_field, delta,
6208 tree_cons (idx_field, idx,
6209 tree_cons (pfn_or_delta2_field, u, NULL_TREE)));
6210 u = build (CONSTRUCTOR, type, NULL_TREE, u);
6211 TREE_CONSTANT (u) = allconstant;
6212 TREE_STATIC (u) = allconstant && allsimple;
6217 /* Build a constructor for a pointer to member function. It can be
6218 used to initialize global variables, local variable, or used
6219 as a value in expressions. TYPE is the POINTER to METHOD_TYPE we
6222 If FORCE is non-zero, then force this conversion, even if
6223 we would rather not do it. Usually set when using an explicit
6226 Return error_mark_node, if something goes wrong. */
6229 build_ptrmemfunc (type, pfn, force)
6233 tree idx = integer_zero_node;
6234 tree delta = integer_zero_node;
6235 tree delta2 = integer_zero_node;
6237 tree npfn = NULL_TREE;
6239 /* Handle multiple conversions of pointer to member functions. */
6240 if (TYPE_PTRMEMFUNC_P (TREE_TYPE (pfn)))
6242 tree ndelta, ndelta2;
6245 /* Is is already the right type? */
6246 if (type == TYPE_PTRMEMFUNC_FN_TYPE (TREE_TYPE (pfn)))
6249 ndelta = convert (ptrdiff_type_node, build_component_ref (pfn, delta_identifier, NULL_TREE, 0));
6250 ndelta2 = convert (ptrdiff_type_node, DELTA2_FROM_PTRMEMFUNC (pfn));
6251 idx = build_component_ref (pfn, index_identifier, NULL_TREE, 0);
6253 n = get_delta_difference (TYPE_METHOD_BASETYPE (TREE_TYPE (TYPE_PTRMEMFUNC_FN_TYPE (TREE_TYPE (pfn)))),
6254 TYPE_METHOD_BASETYPE (TREE_TYPE (type)),
6257 delta = build_binary_op (PLUS_EXPR, ndelta, n, 1);
6258 delta2 = build_binary_op (PLUS_EXPR, ndelta2, n, 1);
6259 e1 = fold (build (GT_EXPR, boolean_type_node, idx, integer_zero_node));
6261 e2 = build_ptrmemfunc1 (TYPE_GET_PTRMEMFUNC_TYPE (type), delta, idx,
6264 pfn = PFN_FROM_PTRMEMFUNC (pfn);
6265 npfn = build1 (NOP_EXPR, type, pfn);
6266 TREE_CONSTANT (npfn) = TREE_CONSTANT (pfn);
6268 e3 = build_ptrmemfunc1 (TYPE_GET_PTRMEMFUNC_TYPE (type), delta, idx, npfn,
6270 return build_conditional_expr (e1, e2, e3);
6273 /* Handle null pointer to member function conversions. */
6274 if (integer_zerop (pfn))
6276 pfn = build_c_cast (type, integer_zero_node);
6277 return build_ptrmemfunc1 (TYPE_GET_PTRMEMFUNC_TYPE (type),
6278 integer_zero_node, integer_zero_node,
6282 if (TREE_CODE (pfn) == TREE_LIST
6283 || (TREE_CODE (pfn) == ADDR_EXPR
6284 && TREE_CODE (TREE_OPERAND (pfn, 0)) == TREE_LIST))
6285 return instantiate_type (type, pfn, 1);
6287 /* Allow pointer to member conversions here. */
6288 delta = get_delta_difference (TYPE_METHOD_BASETYPE (TREE_TYPE (TREE_TYPE (pfn))),
6289 TYPE_METHOD_BASETYPE (TREE_TYPE (type)),
6291 delta2 = build_binary_op (PLUS_EXPR, delta2, delta, 1);
6294 /* We need to check the argument types to see if they are compatible
6295 (any const or volatile violations. */
6296 something like this:
6297 comptype (TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (type))),
6298 TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (TREE_TYPE (pfn)))), ?);
6301 if (TREE_CODE (TREE_OPERAND (pfn, 0)) != FUNCTION_DECL)
6302 warning ("assuming pointer to member function is non-virtual");
6304 if (TREE_CODE (TREE_OPERAND (pfn, 0)) == FUNCTION_DECL
6305 && DECL_VINDEX (TREE_OPERAND (pfn, 0)))
6307 /* Find the offset to the vfield pointer in the object. */
6308 vfield_offset = get_binfo (DECL_CONTEXT (TREE_OPERAND (pfn, 0)),
6309 DECL_CLASS_CONTEXT (TREE_OPERAND (pfn, 0)),
6311 vfield_offset = get_vfield_offset (vfield_offset);
6312 delta2 = size_binop (PLUS_EXPR, vfield_offset, delta2);
6314 /* Map everything down one to make room for the null pointer to member. */
6315 idx = size_binop (PLUS_EXPR,
6316 DECL_VINDEX (TREE_OPERAND (pfn, 0)),
6321 idx = size_binop (MINUS_EXPR, integer_zero_node, integer_one_node);
6323 if (type == TREE_TYPE (pfn))
6329 npfn = build1 (NOP_EXPR, type, pfn);
6330 TREE_CONSTANT (npfn) = TREE_CONSTANT (pfn);
6334 return build_ptrmemfunc1 (TYPE_GET_PTRMEMFUNC_TYPE (type), delta, idx, npfn, delta2);
6337 /* Convert value RHS to type TYPE as preparation for an assignment
6338 to an lvalue of type TYPE.
6339 The real work of conversion is done by `convert'.
6340 The purpose of this function is to generate error messages
6341 for assignments that are not allowed in C.
6342 ERRTYPE is a string to use in error messages:
6343 "assignment", "return", etc.
6345 C++: attempts to allow `convert' to find conversions involving
6346 implicit type conversion between aggregate and scalar types
6347 as per 8.5.6 of C++ manual. Does not randomly dereference
6348 pointers to aggregates! */
6351 convert_for_assignment (type, rhs, errtype, fndecl, parmnum)
6357 register enum tree_code codel = TREE_CODE (type);
6358 register tree rhstype;
6359 register enum tree_code coder = TREE_CODE (TREE_TYPE (rhs));
6361 if (coder == UNKNOWN_TYPE)
6362 rhs = instantiate_type (type, rhs, 1);
6364 if (coder == ERROR_MARK)
6365 return error_mark_node;
6367 if (codel == OFFSET_TYPE)
6369 type = TREE_TYPE (type);
6370 codel = TREE_CODE (type);
6373 /* Strip NON_LVALUE_EXPRs since we aren't using as an lvalue. */
6374 if (TREE_CODE (rhs) == NON_LVALUE_EXPR)
6375 rhs = TREE_OPERAND (rhs, 0);
6377 if (rhs == error_mark_node)
6378 return error_mark_node;
6380 if (TREE_VALUE (rhs) == error_mark_node)
6381 return error_mark_node;
6383 if (TREE_CODE (TREE_TYPE (rhs)) == OFFSET_TYPE)
6385 rhs = resolve_offset_ref (rhs);
6386 if (rhs == error_mark_node)
6387 return error_mark_node;
6388 rhstype = TREE_TYPE (rhs);
6389 coder = TREE_CODE (rhstype);
6392 if (TREE_CODE (TREE_TYPE (rhs)) == ARRAY_TYPE
6393 || TREE_CODE (TREE_TYPE (rhs)) == FUNCTION_TYPE
6394 || TREE_CODE (TREE_TYPE (rhs)) == METHOD_TYPE)
6395 rhs = default_conversion (rhs);
6396 else if (TREE_CODE (TREE_TYPE (rhs)) == REFERENCE_TYPE)
6397 rhs = convert_from_reference (rhs);
6399 rhstype = TREE_TYPE (rhs);
6400 coder = TREE_CODE (rhstype);
6402 /* This should no longer change types on us. */
6403 if (TREE_CODE (rhs) == CONST_DECL)
6404 rhs = DECL_INITIAL (rhs);
6405 else if (TREE_READONLY_DECL_P (rhs))
6406 rhs = decl_constant_value (rhs);
6408 if (type == rhstype)
6410 overflow_warning (rhs);
6414 if (coder == VOID_TYPE)
6416 error ("void value not ignored as it ought to be");
6417 return error_mark_node;
6419 /* Arithmetic types all interconvert. */
6420 if ((codel == INTEGER_TYPE || codel == REAL_TYPE || codel == BOOLEAN_TYPE)
6421 && (coder == INTEGER_TYPE || coder == REAL_TYPE || coder == BOOLEAN_TYPE))
6423 /* But we should warn if assigning REAL_TYPE to INTEGER_TYPE. */
6424 if (coder == REAL_TYPE && codel == INTEGER_TYPE)
6427 cp_warning ("`%T' used for argument %P of `%D'",
6428 rhstype, parmnum, fndecl);
6430 cp_warning ("%s to `%T' from `%T'", errtype, type, rhstype);
6432 /* And we should warn if assigning a negative value to
6433 an unsigned variable. */
6434 else if (TREE_UNSIGNED (type) && codel != BOOLEAN_TYPE)
6436 if (TREE_CODE (rhs) == INTEGER_CST
6437 && TREE_NEGATED_INT (rhs))
6440 cp_warning ("negative value `%E' passed as argument %P of `%D'",
6441 rhs, parmnum, fndecl);
6443 cp_warning ("%s of negative value `%E' to `%T'",
6444 errtype, rhs, type);
6446 overflow_warning (rhs);
6447 if (TREE_CONSTANT (rhs))
6451 return convert_and_check (type, rhs);
6453 /* Conversions involving enums. */
6454 else if ((codel == ENUMERAL_TYPE
6455 && (INTEGRAL_CODE_P (coder) || coder == REAL_TYPE))
6456 || (coder == ENUMERAL_TYPE
6457 && (INTEGRAL_CODE_P (codel) || codel == REAL_TYPE)))
6459 return cp_convert (type, rhs, CONV_IMPLICIT, LOOKUP_NORMAL);
6461 /* Conversions among pointers */
6462 else if (codel == POINTER_TYPE
6463 && (coder == POINTER_TYPE
6464 || (coder == RECORD_TYPE
6465 && (IS_SIGNATURE_POINTER (rhstype)
6466 || IS_SIGNATURE_REFERENCE (rhstype)))))
6468 register tree ttl = TREE_TYPE (type);
6472 if (coder == RECORD_TYPE)
6474 rhs = build_optr_ref (rhs);
6475 rhstype = TREE_TYPE (rhs);
6477 ttr = TREE_TYPE (rhstype);
6479 /* If both pointers are of aggregate type, then we
6480 can give better error messages, and save some work
6482 if (TREE_CODE (ttl) == RECORD_TYPE && TREE_CODE (ttr) == RECORD_TYPE)
6486 if (TYPE_MAIN_VARIANT (ttl) == TYPE_MAIN_VARIANT (ttr)
6487 || type == class_star_type_node
6488 || rhstype == class_star_type_node)
6489 binfo = TYPE_BINFO (ttl);
6491 binfo = get_binfo (ttl, ttr, 1);
6493 if (binfo == error_mark_node)
6494 return error_mark_node;
6496 return error_not_base_type (ttl, ttr);
6498 if (! TYPE_READONLY (ttl) && TYPE_READONLY (ttr))
6501 cp_pedwarn ("passing `%T' as argument %P of `%D' discards const",
6502 rhstype, parmnum, fndecl);
6504 cp_pedwarn ("%s to `%T' from `%T' discards const",
6505 errtype, type, rhstype);
6507 if (! TYPE_VOLATILE (ttl) && TYPE_VOLATILE (ttr))
6510 cp_pedwarn ("passing `%T' as argument %P of `%D' discards volatile",
6511 rhstype, parmnum, fndecl);
6513 cp_pedwarn ("%s to `%T' from `%T' discards volatile",
6514 errtype, type, rhstype);
6518 /* Any non-function converts to a [const][volatile] void *
6519 and vice versa; otherwise, targets must be the same.
6520 Meanwhile, the lhs target must have all the qualifiers of the rhs. */
6521 else if (TYPE_MAIN_VARIANT (ttl) == void_type_node
6522 || TYPE_MAIN_VARIANT (ttr) == void_type_node
6523 || (ctt = comp_target_types (type, rhstype, 1))
6524 || (unsigned_type (TYPE_MAIN_VARIANT (ttl))
6525 == unsigned_type (TYPE_MAIN_VARIANT (ttr))))
6527 /* ARM $4.8, commentary on p39. */
6528 if (TYPE_MAIN_VARIANT (ttl) == void_type_node
6529 && TREE_CODE (ttr) == OFFSET_TYPE)
6531 cp_error ("no standard conversion from `%T' to `void *'", ttr);
6532 return error_mark_node;
6535 if (ctt < 0 && TYPE_MAIN_VARIANT (ttl) != TYPE_MAIN_VARIANT (ttr))
6536 cp_pedwarn ("converting `%T' to `%T' is a contravariance violation",
6539 if (TYPE_MAIN_VARIANT (ttl) != void_type_node
6540 && TYPE_MAIN_VARIANT (ttr) == void_type_node
6541 && ! null_ptr_cst_p (rhs))
6543 if (coder == RECORD_TYPE)
6544 cp_pedwarn ("implicit conversion of signature pointer to type `%T'",
6547 pedwarn ("ANSI C++ forbids implicit conversion from `void *' in %s",
6550 /* Const and volatile mean something different for function types,
6551 so the usual warnings are not appropriate. */
6552 else if ((TREE_CODE (ttr) != FUNCTION_TYPE && TREE_CODE (ttr) != METHOD_TYPE)
6553 || (TREE_CODE (ttl) != FUNCTION_TYPE && TREE_CODE (ttl) != METHOD_TYPE))
6555 if (TREE_CODE (ttl) == OFFSET_TYPE
6556 && binfo_member (TYPE_OFFSET_BASETYPE (ttr),
6557 CLASSTYPE_VBASECLASSES (TYPE_OFFSET_BASETYPE (ttl))))
6559 sorry ("%s between pointer to members converting across virtual baseclasses", errtype);
6560 return error_mark_node;
6562 else if (! TYPE_READONLY (ttl) && TYPE_READONLY (ttr))
6565 cp_pedwarn ("passing `%T' as argument %P of `%D' discards const",
6566 rhstype, parmnum, fndecl);
6568 cp_pedwarn ("%s to `%T' from `%T' discards const",
6569 errtype, type, rhstype);
6571 else if (! TYPE_VOLATILE (ttl) && TYPE_VOLATILE (ttr))
6574 cp_pedwarn ("passing `%T' as argument %P of `%D' discards volatile",
6575 rhstype, parmnum, fndecl);
6577 cp_pedwarn ("%s to `%T' from `%T' discards volatile",
6578 errtype, type, rhstype);
6580 else if (TREE_CODE (ttl) == TREE_CODE (ttr)
6581 && ! comp_target_types (type, rhstype, 1))
6584 cp_pedwarn ("passing `%T' as argument %P of `%D' changes signedness",
6585 rhstype, parmnum, fndecl);
6587 cp_pedwarn ("%s to `%T' from `%T' changes signedness",
6588 errtype, type, rhstype);
6594 int add_quals = 0, const_parity = 0, volatile_parity = 0;
6596 int unsigned_parity;
6599 /* This code is basically a duplicate of comp_ptr_ttypes_real. */
6600 for (; ; ttl = TREE_TYPE (ttl), ttr = TREE_TYPE (ttr))
6603 const_parity |= (TYPE_READONLY (ttl) < TYPE_READONLY (ttr));
6604 volatile_parity |= (TYPE_VOLATILE (ttl) < TYPE_VOLATILE (ttr));
6607 && (TYPE_READONLY (ttl) > TYPE_READONLY (ttr)
6608 || TYPE_VOLATILE (ttl) > TYPE_VOLATILE (ttr)))
6610 left_const &= TYPE_READONLY (ttl);
6612 if (TREE_CODE (ttl) != POINTER_TYPE
6613 || TREE_CODE (ttr) != POINTER_TYPE)
6616 unsigned_parity = TREE_UNSIGNED (ttl) - TREE_UNSIGNED (ttr);
6617 if (unsigned_parity)
6619 if (TREE_UNSIGNED (ttl))
6620 ttr = unsigned_type (ttr);
6622 ttl = unsigned_type (ttl);
6625 if (comp_target_types (ttl, ttr, nptrs) > 0)
6630 cp_pedwarn ("passing `%T' as argument %P of `%D' adds cv-quals without intervening `const'",
6631 rhstype, parmnum, fndecl);
6633 cp_pedwarn ("%s to `%T' from `%T' adds cv-quals without intervening `const'",
6634 errtype, type, rhstype);
6639 cp_pedwarn ("passing `%T' as argument %P of `%D' discards const",
6640 rhstype, parmnum, fndecl);
6642 cp_pedwarn ("%s to `%T' from `%T' discards const",
6643 errtype, type, rhstype);
6645 if (volatile_parity)
6648 cp_pedwarn ("passing `%T' as argument %P of `%D' discards volatile",
6649 rhstype, parmnum, fndecl);
6651 cp_pedwarn ("%s to `%T' from `%T' discards volatile",
6652 errtype, type, rhstype);
6654 if (unsigned_parity > 0)
6657 cp_pedwarn ("passing `%T' as argument %P of `%D' changes signed to unsigned",
6658 rhstype, parmnum, fndecl);
6660 cp_pedwarn ("%s to `%T' from `%T' changes signed to unsigned",
6661 errtype, type, rhstype);
6663 else if (unsigned_parity < 0)
6666 cp_pedwarn ("passing `%T' as argument %P of `%D' changes unsigned to signed",
6667 rhstype, parmnum, fndecl);
6669 cp_pedwarn ("%s to `%T' from `%T' changes unsigned to signed",
6670 errtype, type, rhstype);
6673 /* C++ is not so friendly about converting function and
6674 member function pointers as C. Emit warnings here. */
6675 if (TREE_CODE (ttl) == FUNCTION_TYPE
6676 || TREE_CODE (ttl) == METHOD_TYPE)
6677 if (! comptypes (ttl, ttr, 0))
6679 warning ("conflicting function types in %s:", errtype);
6680 cp_warning ("\t`%T' != `%T'", type, rhstype);
6686 cp_error ("passing `%T' as argument %P of `%D'",
6687 rhstype, parmnum, fndecl);
6689 cp_error ("%s to `%T' from `%T'", errtype, type, rhstype);
6690 return error_mark_node;
6693 return convert (type, rhs);
6695 else if (codel == POINTER_TYPE && coder == INTEGER_TYPE)
6697 /* An explicit constant 0 can convert to a pointer,
6698 but not a 0 that results from casting or folding. */
6699 if (! (TREE_CODE (rhs) == INTEGER_CST && integer_zerop (rhs)))
6702 cp_pedwarn ("passing `%T' to argument %P of `%D' lacks a cast",
6703 rhstype, parmnum, fndecl);
6705 cp_pedwarn ("%s to `%T' from `%T' lacks a cast",
6706 errtype, type, rhstype);
6708 return convert (type, rhs);
6710 else if (codel == INTEGER_TYPE
6711 && (coder == POINTER_TYPE
6712 || (coder == RECORD_TYPE
6713 && (IS_SIGNATURE_POINTER (rhstype)
6714 || TYPE_PTRMEMFUNC_FLAG (rhstype)
6715 || IS_SIGNATURE_REFERENCE (rhstype)))))
6718 cp_pedwarn ("passing `%T' to argument %P of `%D' lacks a cast",
6719 rhstype, parmnum, fndecl);
6721 cp_pedwarn ("%s to `%T' from `%T' lacks a cast",
6722 errtype, type, rhstype);
6723 return convert (type, rhs);
6725 else if (codel == BOOLEAN_TYPE
6726 && (coder == POINTER_TYPE
6727 || (coder == RECORD_TYPE
6728 && (IS_SIGNATURE_POINTER (rhstype)
6729 || TYPE_PTRMEMFUNC_FLAG (rhstype)
6730 || IS_SIGNATURE_REFERENCE (rhstype)))))
6731 return convert (type, rhs);
6734 else if (((coder == POINTER_TYPE
6735 && TREE_CODE (TREE_TYPE (rhstype)) == METHOD_TYPE)
6736 || integer_zerop (rhs)
6737 || TYPE_PTRMEMFUNC_P (rhstype))
6738 && TYPE_PTRMEMFUNC_P (type))
6740 tree ttl = TYPE_PTRMEMFUNC_FN_TYPE (type);
6741 tree ttr = (TREE_CODE (rhstype) == POINTER_TYPE ? rhstype
6742 : TYPE_PTRMEMFUNC_FN_TYPE (type));
6743 int ctt = comp_target_types (ttl, ttr, 1);
6746 cp_pedwarn ("converting `%T' to `%T' is a contravariance violation",
6749 cp_error ("%s to `%T' from `%T'", errtype, ttl, ttr);
6751 /* compatible pointer to member functions. */
6752 return build_ptrmemfunc (ttl, rhs, 0);
6754 else if (codel == ERROR_MARK || coder == ERROR_MARK)
6755 return error_mark_node;
6757 /* This should no longer happen. References are initialized via
6758 `convert_for_initialization'. They should otherwise be
6759 bashed before coming here. */
6760 else if (codel == REFERENCE_TYPE)
6761 my_friendly_abort (317);
6762 else if (TYPE_MAIN_VARIANT (type) == TYPE_MAIN_VARIANT (TREE_TYPE (rhs)))
6764 tree nrhs = build1 (NOP_EXPR, type, rhs);
6765 TREE_CONSTANT (nrhs) = TREE_CONSTANT (rhs);
6768 else if (TYPE_HAS_CONSTRUCTOR (type) || IS_AGGR_TYPE (TREE_TYPE (rhs)))
6769 return convert (type, rhs);
6770 /* Handle anachronistic conversions from (::*)() to cv void* or (*)(). */
6771 else if (TREE_CODE (type) == POINTER_TYPE
6772 && (TREE_CODE (TREE_TYPE (type)) == FUNCTION_TYPE
6773 || TYPE_MAIN_VARIANT (TREE_TYPE (type)) == void_type_node)
6775 && TYPE_PTRMEMFUNC_P (TREE_TYPE (rhs)))
6776 return convert (type, rhs);
6778 cp_error ("%s to `%T' from `%T'", errtype, type, rhstype);
6779 return error_mark_node;
6782 /* Convert RHS to be of type TYPE. If EXP is non-zero,
6783 it is the target of the initialization.
6784 ERRTYPE is a string to use in error messages.
6786 Two major differences between the behavior of
6787 `convert_for_assignment' and `convert_for_initialization'
6788 are that references are bashed in the former, while
6789 copied in the latter, and aggregates are assigned in
6790 the former (operator=) while initialized in the
6793 If using constructor make sure no conversion operator exists, if one does
6794 exist, an ambiguity exists.
6796 If flags doesn't include LOOKUP_COMPLAIN, don't complain about anything. */
6799 convert_for_initialization (exp, type, rhs, flags, errtype, fndecl, parmnum)
6800 tree exp, type, rhs;
6806 register enum tree_code codel = TREE_CODE (type);
6807 register tree rhstype;
6808 register enum tree_code coder;
6810 /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
6811 Strip such NOP_EXPRs, since RHS is used in non-lvalue context. */
6812 if (TREE_CODE (rhs) == NOP_EXPR
6813 && TREE_TYPE (rhs) == TREE_TYPE (TREE_OPERAND (rhs, 0))
6814 && codel != REFERENCE_TYPE)
6815 rhs = TREE_OPERAND (rhs, 0);
6817 if (rhs == error_mark_node
6818 || (TREE_CODE (rhs) == TREE_LIST && TREE_VALUE (rhs) == error_mark_node))
6819 return error_mark_node;
6821 if (TREE_CODE (TREE_TYPE (rhs)) == OFFSET_TYPE)
6823 rhs = resolve_offset_ref (rhs);
6824 if (rhs == error_mark_node)
6825 return error_mark_node;
6828 if (TREE_CODE (TREE_TYPE (rhs)) == REFERENCE_TYPE)
6829 rhs = convert_from_reference (rhs);
6831 if ((TREE_CODE (TREE_TYPE (rhs)) == ARRAY_TYPE
6832 && TREE_CODE (type) != ARRAY_TYPE
6833 && (TREE_CODE (type) != REFERENCE_TYPE
6834 || TREE_CODE (TREE_TYPE (type)) != ARRAY_TYPE))
6835 || TREE_CODE (TREE_TYPE (rhs)) == FUNCTION_TYPE
6836 || TREE_CODE (TREE_TYPE (rhs)) == METHOD_TYPE)
6837 rhs = default_conversion (rhs);
6839 rhstype = TREE_TYPE (rhs);
6840 coder = TREE_CODE (rhstype);
6842 if (coder == UNKNOWN_TYPE)
6844 rhs = instantiate_type (type, rhs, 1);
6845 rhstype = TREE_TYPE (rhs);
6846 coder = TREE_CODE (rhstype);
6849 if (coder == ERROR_MARK)
6850 return error_mark_node;
6852 /* We accept references to incomplete types, so we can
6853 return here before checking if RHS is of complete type. */
6855 if (codel == REFERENCE_TYPE)
6857 /* This should eventually happen in convert_arguments. */
6858 extern int warningcount, errorcount;
6862 savew = warningcount, savee = errorcount;
6863 rhs = convert_to_reference (type, rhs, CONV_IMPLICIT, flags,
6864 exp ? exp : error_mark_node);
6867 if (warningcount > savew)
6868 cp_warning_at ("in passing argument %P of `%+D'", parmnum, fndecl);
6869 else if (errorcount > savee)
6870 cp_error_at ("in passing argument %P of `%+D'", parmnum, fndecl);
6875 rhs = require_complete_type (rhs);
6876 if (rhs == error_mark_node)
6877 return error_mark_node;
6879 if (exp != 0) exp = require_complete_type (exp);
6880 if (exp == error_mark_node)
6881 return error_mark_node;
6883 if (TREE_CODE (rhstype) == REFERENCE_TYPE)
6884 rhstype = TREE_TYPE (rhstype);
6886 type = complete_type (type);
6888 if (TYPE_LANG_SPECIFIC (type)
6889 && (IS_SIGNATURE_POINTER (type) || IS_SIGNATURE_REFERENCE (type)))
6890 return build_signature_pointer_constructor (type, rhs);
6892 if (IS_AGGR_TYPE (type)
6893 && (TYPE_NEEDS_CONSTRUCTING (type) || TREE_HAS_CONSTRUCTOR (rhs)))
6895 if (flag_ansi_overloading)
6896 return cp_convert (type, rhs, CONV_IMPLICIT|CONV_FORCE_TEMP, flags);
6898 if (TYPE_MAIN_VARIANT (type) == TYPE_MAIN_VARIANT (rhstype))
6900 /* This is sufficient to perform initialization. No need,
6901 apparently, to go through X(X&) to do first-cut
6902 initialization. Return through a TARGET_EXPR so that we get
6903 cleanups if it is used. */
6904 if (TREE_CODE (rhs) == CALL_EXPR)
6906 rhs = build_cplus_new (type, rhs);
6909 /* Handle the case of default parameter initialization and
6910 initialization of static variables. */
6911 else if (TREE_CODE (rhs) == TARGET_EXPR)
6913 else if (TREE_CODE (rhs) == INDIRECT_REF && TREE_HAS_CONSTRUCTOR (rhs))
6915 my_friendly_assert (TREE_CODE (TREE_OPERAND (rhs, 0)) == CALL_EXPR, 318);
6918 my_friendly_assert (TREE_VALUE (TREE_OPERAND (TREE_OPERAND (rhs, 0), 1)) == NULL_TREE, 316);
6919 TREE_VALUE (TREE_OPERAND (TREE_OPERAND (rhs, 0), 1))
6920 = build_unary_op (ADDR_EXPR, exp, 0);
6923 rhs = build_cplus_new (type, TREE_OPERAND (rhs, 0));
6926 else if (TYPE_HAS_TRIVIAL_INIT_REF (type))
6929 if (TYPE_MAIN_VARIANT (type) == TYPE_MAIN_VARIANT (rhstype)
6930 || (IS_AGGR_TYPE (rhstype) && UNIQUELY_DERIVED_FROM_P (type, rhstype)))
6932 if (TYPE_HAS_INIT_REF (type))
6934 tree init = build_method_call (exp, ctor_identifier,
6935 build_tree_list (NULL_TREE, rhs),
6936 TYPE_BINFO (type), LOOKUP_NORMAL);
6938 if (init == error_mark_node)
6939 return error_mark_node;
6943 exp = build_cplus_new (type, init);
6947 return build (COMPOUND_EXPR, type, init, exp);
6950 /* ??? The following warnings are turned off because
6951 this is another place where the default X(X&) constructor
6953 if (TYPE_HAS_ASSIGNMENT (type))
6954 cp_warning ("bitwise copy: `%T' defines operator=", type);
6956 if (TREE_CODE (TREE_TYPE (rhs)) == REFERENCE_TYPE)
6957 rhs = convert_from_reference (rhs);
6958 if (type != rhstype)
6960 tree nrhs = build1 (NOP_EXPR, type, rhs);
6961 TREE_CONSTANT (nrhs) = TREE_CONSTANT (rhs);
6967 return cp_convert (type, rhs, CONV_OLD_CONVERT,
6968 flags | LOOKUP_NO_CONVERSION);
6971 if (type == TREE_TYPE (rhs))
6973 if (TREE_READONLY_DECL_P (rhs))
6974 rhs = decl_constant_value (rhs);
6978 return convert_for_assignment (type, rhs, errtype, fndecl, parmnum);
6981 /* Expand an ASM statement with operands, handling output operands
6982 that are not variables or INDIRECT_REFS by transforming such
6983 cases into cases that expand_asm_operands can handle.
6985 Arguments are same as for expand_asm_operands.
6987 We don't do default conversions on all inputs, because it can screw
6988 up operands that are expected to be in memory. */
6991 c_expand_asm_operands (string, outputs, inputs, clobbers, vol, filename, line)
6992 tree string, outputs, inputs, clobbers;
6997 int noutputs = list_length (outputs);
6999 /* o[I] is the place that output number I should be written. */
7000 register tree *o = (tree *) alloca (noutputs * sizeof (tree));
7003 /* Record the contents of OUTPUTS before it is modified. */
7004 for (i = 0, tail = outputs; tail; tail = TREE_CHAIN (tail), i++)
7005 o[i] = TREE_VALUE (tail);
7007 /* Generate the ASM_OPERANDS insn;
7008 store into the TREE_VALUEs of OUTPUTS some trees for
7009 where the values were actually stored. */
7010 expand_asm_operands (string, outputs, inputs, clobbers, vol, filename, line);
7012 /* Copy all the intermediate outputs into the specified outputs. */
7013 for (i = 0, tail = outputs; tail; tail = TREE_CHAIN (tail), i++)
7015 if (o[i] != TREE_VALUE (tail))
7017 expand_expr (build_modify_expr (o[i], NOP_EXPR, TREE_VALUE (tail)),
7018 const0_rtx, VOIDmode, 0);
7021 /* Detect modification of read-only values.
7022 (Otherwise done by build_modify_expr.) */
7025 tree type = TREE_TYPE (o[i]);
7026 if (TYPE_READONLY (type)
7027 || ((TREE_CODE (type) == RECORD_TYPE
7028 || TREE_CODE (type) == UNION_TYPE)
7029 && C_TYPE_FIELDS_READONLY (type)))
7030 readonly_error (o[i], "modification by `asm'", 1);
7034 /* Those MODIFY_EXPRs could do autoincrements. */
7038 /* Expand a C `return' statement.
7039 RETVAL is the expression for what to return,
7040 or a null pointer for `return;' with no value.
7042 C++: upon seeing a `return', we must call destructors on all
7043 variables in scope which had constructors called on them.
7044 This means that if in a destructor, the base class destructors
7045 must be called before returning.
7047 The RETURN statement in C++ has initialization semantics. */
7050 c_expand_return (retval)
7053 extern struct nesting *cond_stack, *loop_stack, *case_stack;
7054 extern tree dtor_label, ctor_label;
7055 tree result = DECL_RESULT (current_function_decl);
7056 tree valtype = TREE_TYPE (result);
7058 if (TREE_THIS_VOLATILE (current_function_decl))
7059 warning ("function declared `noreturn' has a `return' statement");
7061 if (retval == error_mark_node)
7063 current_function_returns_null = 1;
7067 if (processing_template_decl)
7069 add_tree (build_min_nt (RETURN_STMT, retval));
7073 if (retval == NULL_TREE)
7075 /* A non-named return value does not count. */
7077 /* Can't just return from a destructor. */
7080 expand_goto (dtor_label);
7084 if (DECL_CONSTRUCTOR_P (current_function_decl))
7085 retval = current_class_ptr;
7086 else if (DECL_NAME (result) != NULL_TREE
7087 && TREE_CODE (valtype) != VOID_TYPE)
7091 current_function_returns_null = 1;
7093 if (valtype != NULL_TREE && TREE_CODE (valtype) != VOID_TYPE)
7095 if (DECL_NAME (DECL_RESULT (current_function_decl)) == NULL_TREE)
7097 pedwarn ("`return' with no value, in function returning non-void");
7098 /* Clear this, so finish_function won't say that we
7099 reach the end of a non-void function (which we don't,
7100 we gave a return!). */
7101 current_function_returns_null = 0;
7105 expand_null_return ();
7109 else if (DECL_CONSTRUCTOR_P (current_function_decl)
7110 && retval != current_class_ptr)
7112 if (flag_this_is_variable)
7113 error ("return from a constructor: use `this = ...' instead");
7115 error ("return from a constructor");
7116 retval = current_class_ptr;
7119 /* Effective C++ rule 15. See also start_function. */
7121 && DECL_NAME (current_function_decl) == ansi_opname[(int) MODIFY_EXPR]
7122 && retval != current_class_ref)
7123 cp_warning ("`operator=' should return a reference to `*this'");
7125 if (valtype == NULL_TREE || TREE_CODE (valtype) == VOID_TYPE)
7127 current_function_returns_null = 1;
7128 if (pedantic || TREE_CODE (TREE_TYPE (retval)) != VOID_TYPE)
7129 pedwarn ("`return' with a value, in function returning void");
7130 expand_return (retval);
7133 /* Add some useful error checking for C++. */
7134 else if (TREE_CODE (valtype) == REFERENCE_TYPE)
7136 tree whats_returned;
7137 tree tmp_result = result;
7139 /* Don't initialize directly into a non-BLKmode retval, since that
7140 could lose when being inlined by another caller. (GCC can't
7141 read the function return register in an inline function when
7142 the return value is being ignored). */
7143 if (result && TYPE_MODE (TREE_TYPE (tmp_result)) != BLKmode)
7146 /* convert to reference now, so we can give error if we
7147 return an reference to a non-lvalue. */
7148 retval = convert_for_initialization
7149 (tmp_result, valtype, retval, LOOKUP_NORMAL|LOOKUP_ONLYCONVERTING,
7150 "return", NULL_TREE, 0);
7152 /* Sort through common things to see what it is
7153 we are returning. */
7154 whats_returned = retval;
7155 if (TREE_CODE (whats_returned) == COMPOUND_EXPR)
7157 whats_returned = TREE_OPERAND (whats_returned, 1);
7158 if (TREE_CODE (whats_returned) == ADDR_EXPR)
7159 whats_returned = TREE_OPERAND (whats_returned, 0);
7161 if (TREE_CODE (whats_returned) == ADDR_EXPR)
7163 whats_returned = TREE_OPERAND (whats_returned, 0);
7164 while (TREE_CODE (whats_returned) == NEW_EXPR
7165 || TREE_CODE (whats_returned) == TARGET_EXPR)
7167 /* Get the target. */
7168 whats_returned = TREE_OPERAND (whats_returned, 0);
7169 warning ("returning reference to temporary");
7173 if (TREE_CODE (whats_returned) == VAR_DECL && DECL_NAME (whats_returned))
7175 if (TEMP_NAME_P (DECL_NAME (whats_returned)))
7176 warning ("reference to non-lvalue returned");
7177 else if (! TREE_STATIC (whats_returned)
7178 && IDENTIFIER_LOCAL_VALUE (DECL_NAME (whats_returned))
7179 && !TREE_PUBLIC (whats_returned))
7180 cp_warning_at ("reference to local variable `%D' returned", whats_returned);
7183 else if (TREE_CODE (retval) == ADDR_EXPR)
7185 tree whats_returned = TREE_OPERAND (retval, 0);
7187 if (TREE_CODE (whats_returned) == VAR_DECL
7188 && DECL_NAME (whats_returned)
7189 && IDENTIFIER_LOCAL_VALUE (DECL_NAME (whats_returned))
7190 && !TREE_STATIC (whats_returned)
7191 && !TREE_PUBLIC (whats_returned))
7192 cp_warning_at ("address of local variable `%D' returned", whats_returned);
7194 else if (TREE_CODE (retval) == VAR_DECL)
7196 if (TREE_CODE (TREE_TYPE (retval)) == ARRAY_TYPE
7197 && DECL_NAME (retval)
7198 && IDENTIFIER_LOCAL_VALUE (DECL_NAME (retval))
7199 && !TREE_STATIC (retval)
7200 && !TREE_PUBLIC (retval))
7201 cp_warning_at ("address of local array `%D' returned", retval);
7204 /* Now deal with possible C++ hair:
7205 (1) Compute the return value.
7206 (2) If there are aggregate values with destructors which
7207 must be cleaned up, clean them (taking care
7208 not to clobber the return value).
7209 (3) If an X(X&) constructor is defined, the return
7210 value must be returned via that. */
7212 if (retval == result
7213 || DECL_CONSTRUCTOR_P (current_function_decl))
7214 /* It's already done for us. */;
7215 else if (TREE_TYPE (retval) == void_type_node)
7217 pedwarn ("return of void value in function returning non-void");
7218 expand_expr_stmt (retval);
7223 /* We already did this above for refs, don't do it again. */
7224 if (TREE_CODE (valtype) != REFERENCE_TYPE)
7225 retval = convert_for_initialization
7226 (NULL_TREE, valtype, retval, LOOKUP_NORMAL|LOOKUP_ONLYCONVERTING,
7227 "return", NULL_TREE, 0);
7229 /* We can't initialize a register from a NEW_EXPR. */
7230 if (! current_function_returns_struct
7231 && TREE_CODE (retval) == TARGET_EXPR
7232 && TREE_CODE (TREE_OPERAND (retval, 1)) == NEW_EXPR)
7233 retval = build (COMPOUND_EXPR, TREE_TYPE (retval), retval,
7234 TREE_OPERAND (retval, 0));
7236 if (retval == error_mark_node)
7238 /* Avoid warning about control reaching end of function. */
7239 expand_null_return ();
7244 if (retval != NULL_TREE
7245 && TREE_CODE_CLASS (TREE_CODE (retval)) == 'd'
7246 && cond_stack == 0 && loop_stack == 0 && case_stack == 0)
7247 current_function_return_value = retval;
7249 if (ctor_label && TREE_CODE (ctor_label) != ERROR_MARK)
7251 /* Here RETVAL is CURRENT_CLASS_PTR, so there's nothing to do. */
7252 expand_goto (ctor_label);
7255 if (retval && retval != result)
7257 result = build (INIT_EXPR, TREE_TYPE (result), result, retval);
7258 TREE_SIDE_EFFECTS (result) = 1;
7260 expand_return (result);
7261 current_function_returns_value = 1;
7264 /* Start a C switch statement, testing expression EXP.
7265 Return EXP if it is valid, an error node otherwise. */
7268 c_expand_start_case (exp)
7272 register enum tree_code code;
7274 /* Convert from references, etc. */
7275 exp = default_conversion (exp);
7276 type = TREE_TYPE (exp);
7277 code = TREE_CODE (type);
7279 if (IS_AGGR_TYPE_CODE (code))
7280 exp = build_type_conversion (CONVERT_EXPR, integer_type_node, exp, 1);
7282 if (exp == NULL_TREE)
7284 error ("switch quantity not an integer");
7285 exp = error_mark_node;
7287 type = TREE_TYPE (exp);
7288 code = TREE_CODE (type);
7290 if (code != INTEGER_TYPE && code != ENUMERAL_TYPE && code != ERROR_MARK)
7292 error ("switch quantity not an integer");
7293 exp = error_mark_node;
7299 exp = default_conversion (exp);
7300 type = TREE_TYPE (exp);
7301 idx = get_unwidened (exp, 0);
7302 /* We can't strip a conversion from a signed type to an unsigned,
7303 because if we did, int_fits_type_p would do the wrong thing
7304 when checking case values for being in range,
7305 and it's too hard to do the right thing. */
7306 if (TREE_UNSIGNED (TREE_TYPE (exp))
7307 == TREE_UNSIGNED (TREE_TYPE (idx)))
7312 (1, fold (build1 (CLEANUP_POINT_EXPR, TREE_TYPE (exp), exp)),
7313 type, "switch statement");
7318 /* CONSTP remembers whether or not all the intervening pointers in the `to'
7319 type have been const. */
7322 comp_ptr_ttypes_real (to, from, constp)
7326 for (; ; to = TREE_TYPE (to), from = TREE_TYPE (from))
7328 if (TREE_CODE (to) != TREE_CODE (from))
7331 if (TREE_CODE (from) == OFFSET_TYPE
7332 && comptypes (TYPE_OFFSET_BASETYPE (from),
7333 TYPE_OFFSET_BASETYPE (to), 1))
7336 /* Const and volatile mean something different for function types,
7337 so the usual checks are not appropriate. */
7338 if (TREE_CODE (to) != FUNCTION_TYPE && TREE_CODE (to) != METHOD_TYPE)
7340 if (TYPE_READONLY (from) > TYPE_READONLY (to)
7341 || TYPE_VOLATILE (from) > TYPE_VOLATILE (to))
7345 && (TYPE_READONLY (to) > TYPE_READONLY (from)
7346 || TYPE_VOLATILE (to) > TYPE_READONLY (from)))
7348 constp &= TYPE_READONLY (to);
7351 if (TREE_CODE (to) != POINTER_TYPE)
7352 return comptypes (TYPE_MAIN_VARIANT (to), TYPE_MAIN_VARIANT (from), 1);
7356 /* When comparing, say, char ** to char const **, this function takes the
7357 'char *' and 'char const *'. Do not pass non-pointer types to this
7361 comp_ptr_ttypes (to, from)
7364 return comp_ptr_ttypes_real (to, from, 1);
7367 /* Returns 1 if to and from are (possibly multi-level) pointers to the same
7368 type or inheritance-related types, regardless of cv-quals. */
7371 ptr_reasonably_similar (to, from)
7374 for (; ; to = TREE_TYPE (to), from = TREE_TYPE (from))
7376 if (TREE_CODE (to) != TREE_CODE (from))
7379 if (TREE_CODE (from) == OFFSET_TYPE
7380 && comptypes (TYPE_OFFSET_BASETYPE (to),
7381 TYPE_OFFSET_BASETYPE (from), -1))
7384 if (TREE_CODE (to) != POINTER_TYPE)
7386 (TYPE_MAIN_VARIANT (to), TYPE_MAIN_VARIANT (from), -1);
7390 /* Like comp_ptr_ttypes, for const_cast. */
7393 comp_ptr_ttypes_const (to, from)
7396 for (; ; to = TREE_TYPE (to), from = TREE_TYPE (from))
7398 if (TREE_CODE (to) != TREE_CODE (from))
7401 if (TREE_CODE (from) == OFFSET_TYPE
7402 && comptypes (TYPE_OFFSET_BASETYPE (from),
7403 TYPE_OFFSET_BASETYPE (to), 1))
7406 if (TREE_CODE (to) != POINTER_TYPE)
7407 return comptypes (TYPE_MAIN_VARIANT (to), TYPE_MAIN_VARIANT (from), 1);
7411 /* Like comp_ptr_ttypes, for reinterpret_cast. */
7414 comp_ptr_ttypes_reinterpret (to, from)
7419 for (; ; to = TREE_TYPE (to), from = TREE_TYPE (from))
7421 if (TREE_CODE (from) == OFFSET_TYPE)
7422 from = TREE_TYPE (from);
7423 if (TREE_CODE (to) == OFFSET_TYPE)
7424 to = TREE_TYPE (to);
7426 if (TREE_CODE (to) != TREE_CODE (from))
7429 /* Const and volatile mean something different for function types,
7430 so the usual checks are not appropriate. */
7431 if (TREE_CODE (to) != FUNCTION_TYPE && TREE_CODE (to) != METHOD_TYPE)
7433 if (TYPE_READONLY (from) > TYPE_READONLY (to)
7434 || TYPE_VOLATILE (from) > TYPE_VOLATILE (to))
7438 && (TYPE_READONLY (to) > TYPE_READONLY (from)
7439 || TYPE_VOLATILE (to) > TYPE_READONLY (from)))
7441 constp &= TYPE_READONLY (to);
7444 if (TREE_CODE (to) != POINTER_TYPE)