1 /* Build expressions with type checking for C++ compiler.
2 Copyright (C) 1987, 88, 89, 92-97, 1998 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). */
42 extern void compiler_error ();
44 static tree convert_for_assignment PROTO((tree, tree, char*, tree,
46 static tree pointer_int_sum PROTO((enum tree_code, tree, tree));
47 static tree rationalize_conditional_expr PROTO((enum tree_code, tree));
48 static int comp_target_parms PROTO((tree, tree, int));
49 static int comp_ptr_ttypes_real PROTO((tree, tree, int));
50 static int comp_ptr_ttypes_const PROTO((tree, tree));
51 static int comp_ptr_ttypes_reinterpret PROTO((tree, tree));
52 static int comp_array_types PROTO((int (*) (tree, tree, int), tree,
54 static tree build_ptrmemfunc1 PROTO((tree, tree, tree, tree, tree));
55 static tree common_base_type PROTO((tree, tree));
57 static tree convert_sequence PROTO((tree, tree));
59 static tree lookup_anon_field PROTO((tree, tree));
60 static tree pointer_diff PROTO((tree, tree, tree));
61 static tree qualify_type PROTO((tree, tree));
62 static tree get_delta_difference PROTO((tree, tree, int));
64 /* Return the target type of TYPE, which meas return T for:
65 T*, T&, T[], T (...), and otherwise, just T. */
71 if (TREE_CODE (type) == REFERENCE_TYPE)
72 type = TREE_TYPE (type);
73 while (TREE_CODE (type) == POINTER_TYPE
74 || TREE_CODE (type) == ARRAY_TYPE
75 || TREE_CODE (type) == FUNCTION_TYPE
76 || TREE_CODE (type) == METHOD_TYPE
77 || TREE_CODE (type) == OFFSET_TYPE)
78 type = TREE_TYPE (type);
82 /* Do `exp = require_complete_type (exp);' to make sure exp
83 does not have an incomplete type. (That includes void types.) */
86 require_complete_type (value)
91 if (processing_template_decl)
94 if (TREE_CODE (value) == OVERLOAD)
95 type = unknown_type_node;
97 type = TREE_TYPE (value);
99 /* First, detect a valid value with a complete type. */
100 if (TYPE_SIZE (type) != 0
101 && type != void_type_node
102 && ! (TYPE_LANG_SPECIFIC (type)
103 && (IS_SIGNATURE_POINTER (type) || IS_SIGNATURE_REFERENCE (type))
104 && TYPE_SIZE (SIGNATURE_TYPE (type)) == 0))
107 /* If we see X::Y, we build an OFFSET_TYPE which has
108 not been laid out. Try to avoid an error by interpreting
109 it as this->X::Y, if reasonable. */
110 if (TREE_CODE (value) == OFFSET_REF
111 && current_class_ref != 0
112 && TREE_OPERAND (value, 0) == current_class_ref)
114 tree base, member = TREE_OPERAND (value, 1);
115 tree basetype = TYPE_OFFSET_BASETYPE (type);
116 my_friendly_assert (TREE_CODE (member) == FIELD_DECL, 305);
117 base = convert_pointer_to (basetype, current_class_ptr);
118 value = build (COMPONENT_REF, TREE_TYPE (member),
119 build_indirect_ref (base, NULL_PTR), member);
120 return require_complete_type (value);
123 if (IS_AGGR_TYPE (type) && CLASSTYPE_TEMPLATE_INSTANTIATION (type))
125 instantiate_class_template (TYPE_MAIN_VARIANT (type));
126 if (TYPE_SIZE (type) != 0)
130 incomplete_type_error (value, type);
131 return error_mark_node;
138 if (type == NULL_TREE)
139 /* Rather than crash, we return something sure to cause an error
141 return error_mark_node;
143 if (type == error_mark_node || TYPE_SIZE (type) != NULL_TREE)
145 else if (TREE_CODE (type) == ARRAY_TYPE && TYPE_DOMAIN (type))
147 tree t = complete_type (TREE_TYPE (type));
148 if (TYPE_SIZE (t) != NULL_TREE && ! processing_template_decl)
150 TYPE_NEEDS_CONSTRUCTING (type)
151 = TYPE_NEEDS_CONSTRUCTING (TYPE_MAIN_VARIANT (t));
152 TYPE_NEEDS_DESTRUCTOR (type)
153 = TYPE_NEEDS_DESTRUCTOR (TYPE_MAIN_VARIANT (t));
155 else if (IS_AGGR_TYPE (type) && CLASSTYPE_TEMPLATE_INSTANTIATION (type))
156 instantiate_class_template (TYPE_MAIN_VARIANT (type));
161 /* Return truthvalue of whether type of EXP is instantiated. */
167 return (TREE_CODE (exp) == OVERLOAD
168 || TREE_CODE (exp) == TREE_LIST
169 || TREE_TYPE (exp) == unknown_type_node
170 || (TREE_CODE (TREE_TYPE (exp)) == OFFSET_TYPE
171 && TREE_TYPE (TREE_TYPE (exp)) == unknown_type_node));
174 /* Return truthvalue of whether T is function (or pfn) type. */
180 return (TREE_CODE (t) == FUNCTION_TYPE || TREE_CODE (t) == METHOD_TYPE
181 || (TREE_CODE (t) == POINTER_TYPE
182 && (TREE_CODE (TREE_TYPE (t)) == FUNCTION_TYPE
183 || TREE_CODE (TREE_TYPE (t)) == METHOD_TYPE)));
186 /* Do `exp = require_instantiated_type (type, exp);' to make sure EXP
187 does not have an uninstantiated type.
188 TYPE is type to instantiate with, if uninstantiated. */
191 require_instantiated_type (type, exp, errval)
192 tree type, exp, errval;
194 if (TREE_TYPE (exp) == NULL_TREE)
196 error ("argument list may not have an initializer list");
200 if (TREE_CODE (exp) == OVERLOAD
201 || TREE_TYPE (exp) == unknown_type_node
202 || (TREE_CODE (TREE_TYPE (exp)) == OFFSET_TYPE
203 && TREE_TYPE (TREE_TYPE (exp)) == unknown_type_node))
205 exp = instantiate_type (type, exp, 1);
206 if (TREE_TYPE (exp) == error_mark_node)
212 /* Return a variant of TYPE which has all the type qualifiers of LIKE
213 as well as those of TYPE. */
216 qualify_type (type, like)
219 int constflag = TYPE_READONLY (type) || TYPE_READONLY (like);
220 int volflag = TYPE_VOLATILE (type) || TYPE_VOLATILE (like);
221 /* @@ Must do member pointers here. */
222 return cp_build_type_variant (type, constflag, volflag);
225 /* Return the common type of two parameter lists.
226 We assume that comptypes has already been done and returned 1;
227 if that isn't so, this may crash.
229 As an optimization, free the space we allocate if the parameter
230 lists are already common. */
236 tree oldargs = p1, newargs, n;
239 char *first_obj = (char *) oballoc (0);
241 len = list_length (p1);
242 newargs = tree_last (p1);
244 if (newargs == void_list_node)
253 newargs = tree_cons (NULL_TREE, NULL_TREE, newargs);
258 p1 = TREE_CHAIN (p1), p2 = TREE_CHAIN (p2), n = TREE_CHAIN (n), i++)
260 if (TREE_PURPOSE (p1) && !TREE_PURPOSE (p2))
262 TREE_PURPOSE (n) = TREE_PURPOSE (p1);
265 else if (! TREE_PURPOSE (p1))
267 if (TREE_PURPOSE (p2))
269 TREE_PURPOSE (n) = TREE_PURPOSE (p2);
275 if (1 != simple_cst_equal (TREE_PURPOSE (p1), TREE_PURPOSE (p2)))
277 TREE_PURPOSE (n) = TREE_PURPOSE (p2);
279 if (TREE_VALUE (p1) != TREE_VALUE (p2))
282 TREE_VALUE (n) = common_type (TREE_VALUE (p1), TREE_VALUE (p2));
285 TREE_VALUE (n) = TREE_VALUE (p1);
296 /* Given a type, perhaps copied for a typedef,
297 find the "original" version of it. */
302 while (TYPE_NAME (t) != NULL_TREE)
304 tree x = TYPE_NAME (t);
305 if (TREE_CODE (x) != TYPE_DECL)
307 x = DECL_ORIGINAL_TYPE (x);
315 /* Return the common type of two types.
316 We assume that comptypes has already been done and returned 1;
317 if that isn't so, this may crash.
319 This is the type for the result of most arithmetic operations
320 if the operands have the given two types.
322 We do not deal with enumeral types here because they have already been
323 converted to integer types. */
329 register enum tree_code code1;
330 register enum tree_code code2;
333 /* Save time if the two types are the same. */
336 t1 = original_type (t1);
337 t2 = original_type (t2);
341 /* If one type is nonsense, use the other. */
342 if (t1 == error_mark_node)
344 if (t2 == error_mark_node)
347 /* Merge the attributes. */
348 attributes = merge_machine_type_attributes (t1, t2);
350 { register tree a1, a2;
351 a1 = TYPE_ATTRIBUTES (t1);
352 a2 = TYPE_ATTRIBUTES (t2);
354 /* Either one unset? Take the set one. */
356 if (!(attributes = a1))
359 /* One that completely contains the other? Take it. */
361 else if (a2 && !attribute_list_contained (a1, a2))
363 if (attribute_list_contained (a2, a1))
367 /* Pick the longest list, and hang on the other list. */
368 /* ??? For the moment we punt on the issue of attrs with args. */
370 if (list_length (a1) < list_length (a2))
371 attributes = a2, a2 = a1;
373 for (; a2; a2 = TREE_CHAIN (a2))
374 if (lookup_attribute (IDENTIFIER_POINTER (TREE_PURPOSE (a2)),
375 attributes) == NULL_TREE)
378 TREE_CHAIN (a1) = attributes;
385 /* Treat an enum type as the unsigned integer type of the same width. */
387 if (TREE_CODE (t1) == ENUMERAL_TYPE)
388 t1 = type_for_size (TYPE_PRECISION (t1), 1);
389 if (TREE_CODE (t2) == ENUMERAL_TYPE)
390 t2 = type_for_size (TYPE_PRECISION (t2), 1);
392 if (TYPE_PTRMEMFUNC_P (t1))
393 t1 = TYPE_PTRMEMFUNC_FN_TYPE (t1);
394 if (TYPE_PTRMEMFUNC_P (t2))
395 t2 = TYPE_PTRMEMFUNC_FN_TYPE (t2);
397 code1 = TREE_CODE (t1);
398 code2 = TREE_CODE (t2);
400 /* If one type is complex, form the common type of the non-complex
401 components, then make that complex. Use T1 or T2 if it is the
403 if (code1 == COMPLEX_TYPE || code2 == COMPLEX_TYPE)
405 tree subtype1 = code1 == COMPLEX_TYPE ? TREE_TYPE (t1) : t1;
406 tree subtype2 = code2 == COMPLEX_TYPE ? TREE_TYPE (t2) : t2;
407 tree subtype = common_type (subtype1, subtype2);
409 if (code1 == COMPLEX_TYPE && TREE_TYPE (t1) == subtype)
410 return build_type_attribute_variant (t1, attributes);
411 else if (code2 == COMPLEX_TYPE && TREE_TYPE (t2) == subtype)
412 return build_type_attribute_variant (t2, attributes);
414 return build_type_attribute_variant (build_complex_type (subtype),
422 /* If only one is real, use it as the result. */
424 if (code1 == REAL_TYPE && code2 != REAL_TYPE)
425 return build_type_attribute_variant (t1, attributes);
427 if (code2 == REAL_TYPE && code1 != REAL_TYPE)
428 return build_type_attribute_variant (t2, attributes);
430 /* Both real or both integers; use the one with greater precision. */
432 if (TYPE_PRECISION (t1) > TYPE_PRECISION (t2))
433 return build_type_attribute_variant (t1, attributes);
434 else if (TYPE_PRECISION (t2) > TYPE_PRECISION (t1))
435 return build_type_attribute_variant (t2, attributes);
437 /* Same precision. Prefer longs to ints even when same size. */
439 if (TYPE_MAIN_VARIANT (t1) == long_unsigned_type_node
440 || TYPE_MAIN_VARIANT (t2) == long_unsigned_type_node)
441 return build_type_attribute_variant (long_unsigned_type_node,
444 if (TYPE_MAIN_VARIANT (t1) == long_integer_type_node
445 || TYPE_MAIN_VARIANT (t2) == long_integer_type_node)
447 /* But preserve unsignedness from the other type,
448 since long cannot hold all the values of an unsigned int. */
449 if (TREE_UNSIGNED (t1) || TREE_UNSIGNED (t2))
450 t1 = long_unsigned_type_node;
452 t1 = long_integer_type_node;
453 return build_type_attribute_variant (t1, attributes);
456 if (TYPE_MAIN_VARIANT (t1) == long_double_type_node
457 || TYPE_MAIN_VARIANT (t2) == long_double_type_node)
458 return build_type_attribute_variant (long_double_type_node,
461 /* Otherwise prefer the unsigned one. */
463 if (TREE_UNSIGNED (t1))
464 return build_type_attribute_variant (t1, attributes);
466 return build_type_attribute_variant (t2, attributes);
470 /* For two pointers, do this recursively on the target type,
471 and combine the qualifiers of the two types' targets. */
472 /* This code was turned off; I don't know why.
473 But ANSI C++ specifies doing this with the qualifiers.
474 So I turned it on again. */
476 tree tt1 = TYPE_MAIN_VARIANT (TREE_TYPE (t1));
477 tree tt2 = TYPE_MAIN_VARIANT (TREE_TYPE (t2));
479 = TYPE_READONLY (TREE_TYPE (t1)) || TYPE_READONLY (TREE_TYPE (t2));
481 = TYPE_VOLATILE (TREE_TYPE (t1)) || TYPE_VOLATILE (TREE_TYPE (t2));
486 else if (tt1 == void_type_node || tt2 == void_type_node)
487 target = void_type_node;
488 else if (tt1 == unknown_type_node)
490 else if (tt2 == unknown_type_node)
493 target = common_type (tt1, tt2);
495 target = cp_build_type_variant (target, constp, volatilep);
496 if (code1 == POINTER_TYPE)
497 t1 = build_pointer_type (target);
499 t1 = build_reference_type (target);
500 t1 = build_type_attribute_variant (t1, attributes);
502 if (TREE_CODE (target) == METHOD_TYPE)
503 t1 = build_ptrmemfunc_type (t1);
510 tree elt = common_type (TREE_TYPE (t1), TREE_TYPE (t2));
511 /* Save space: see if the result is identical to one of the args. */
512 if (elt == TREE_TYPE (t1) && TYPE_DOMAIN (t1))
513 return build_type_attribute_variant (t1, attributes);
514 if (elt == TREE_TYPE (t2) && TYPE_DOMAIN (t2))
515 return build_type_attribute_variant (t2, attributes);
516 /* Merge the element types, and have a size if either arg has one. */
517 t1 = build_cplus_array_type
518 (elt, TYPE_DOMAIN (TYPE_DOMAIN (t1) ? t1 : t2));
519 return build_type_attribute_variant (t1, attributes);
523 /* Function types: prefer the one that specified arg types.
524 If both do, merge the arg types. Also merge the return types. */
526 tree valtype = common_type (TREE_TYPE (t1), TREE_TYPE (t2));
527 tree p1 = TYPE_ARG_TYPES (t1);
528 tree p2 = TYPE_ARG_TYPES (t2);
531 /* Save space: see if the result is identical to one of the args. */
532 if (valtype == TREE_TYPE (t1) && ! p2)
533 return build_type_attribute_variant (t1, attributes);
534 if (valtype == TREE_TYPE (t2) && ! p1)
535 return build_type_attribute_variant (t2, attributes);
537 /* Simple way if one arg fails to specify argument types. */
538 if (p1 == NULL_TREE || TREE_VALUE (p1) == void_type_node)
540 rval = build_function_type (valtype, p2);
541 if ((raises = TYPE_RAISES_EXCEPTIONS (t2)))
542 rval = build_exception_variant (rval, raises);
543 return build_type_attribute_variant (rval, attributes);
545 raises = TYPE_RAISES_EXCEPTIONS (t1);
546 if (p2 == NULL_TREE || TREE_VALUE (p2) == void_type_node)
548 rval = build_function_type (valtype, p1);
550 rval = build_exception_variant (rval, raises);
551 return build_type_attribute_variant (rval, attributes);
554 rval = build_function_type (valtype, commonparms (p1, p2));
555 rval = build_exception_variant (rval, raises);
556 return build_type_attribute_variant (rval, attributes);
561 t1 = TYPE_MAIN_VARIANT (t1);
562 t2 = TYPE_MAIN_VARIANT (t2);
564 if (DERIVED_FROM_P (t1, t2) && binfo_or_else (t1, t2))
565 return build_type_attribute_variant (t1, attributes);
566 else if (binfo_or_else (t2, t1))
567 return build_type_attribute_variant (t2, attributes);
569 compiler_error ("common_type called with uncommon aggregate types");
572 if (TREE_CODE (TREE_TYPE (t1)) == TREE_CODE (TREE_TYPE (t2)))
574 /* Get this value the long way, since TYPE_METHOD_BASETYPE
575 is just the main variant of this. */
579 tree b1 = TYPE_OFFSET_BASETYPE (t1);
580 tree b2 = TYPE_OFFSET_BASETYPE (t2);
582 if (comptypes (b1, b2, 1)
583 || (DERIVED_FROM_P (b1, b2) && binfo_or_else (b1, b2)))
584 basetype = TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (t2)));
587 if (binfo_or_else (b2, b1) == NULL_TREE)
588 compiler_error ("common_type called with uncommon method types");
589 basetype = TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (t1)));
592 raises = TYPE_RAISES_EXCEPTIONS (t1);
594 /* If this was a member function type, get back to the
595 original type of type member function (i.e., without
596 the class instance variable up front. */
597 t1 = build_function_type (TREE_TYPE (t1),
598 TREE_CHAIN (TYPE_ARG_TYPES (t1)));
599 t2 = build_function_type (TREE_TYPE (t2),
600 TREE_CHAIN (TYPE_ARG_TYPES (t2)));
601 t3 = common_type (t1, t2);
602 t3 = build_cplus_method_type (basetype, TREE_TYPE (t3),
603 TYPE_ARG_TYPES (t3));
604 t1 = build_exception_variant (t3, raises);
607 compiler_error ("common_type called with uncommon method types");
609 return build_type_attribute_variant (t1, attributes);
612 if (TREE_TYPE (t1) == TREE_TYPE (t2))
614 tree b1 = TYPE_OFFSET_BASETYPE (t1);
615 tree b2 = TYPE_OFFSET_BASETYPE (t2);
617 if (comptypes (b1, b2, 1)
618 || (DERIVED_FROM_P (b1, b2) && binfo_or_else (b1, b2)))
619 return build_type_attribute_variant (t2, attributes);
620 else if (binfo_or_else (b2, b1))
621 return build_type_attribute_variant (t1, attributes);
623 compiler_error ("common_type called with uncommon member types");
626 return build_type_attribute_variant (t1, attributes);
630 /* Return 1 if TYPE1 and TYPE2 raise the same exceptions. */
633 compexcepttypes (t1, t2)
636 return TYPE_RAISES_EXCEPTIONS (t1) == TYPE_RAISES_EXCEPTIONS (t2);
640 comp_array_types (cmp, t1, t2, strict)
641 register int (*cmp) PROTO((tree, tree, int));
645 tree d1 = TYPE_DOMAIN (t1);
646 tree d2 = TYPE_DOMAIN (t2);
648 /* Target types must match incl. qualifiers. */
649 if (!(TREE_TYPE (t1) == TREE_TYPE (t2)
650 || (*cmp) (TREE_TYPE (t1), TREE_TYPE (t2), strict)))
653 /* Sizes must match unless one is missing or variable. */
654 if (d1 == 0 || d2 == 0 || d1 == d2
655 || TREE_CODE (TYPE_MIN_VALUE (d1)) != INTEGER_CST
656 || TREE_CODE (TYPE_MIN_VALUE (d2)) != INTEGER_CST
657 || TREE_CODE (TYPE_MAX_VALUE (d1)) != INTEGER_CST
658 || TREE_CODE (TYPE_MAX_VALUE (d2)) != INTEGER_CST)
661 return ((TREE_INT_CST_LOW (TYPE_MIN_VALUE (d1))
662 == TREE_INT_CST_LOW (TYPE_MIN_VALUE (d2)))
663 && (TREE_INT_CST_HIGH (TYPE_MIN_VALUE (d1))
664 == TREE_INT_CST_HIGH (TYPE_MIN_VALUE (d2)))
665 && (TREE_INT_CST_LOW (TYPE_MAX_VALUE (d1))
666 == TREE_INT_CST_LOW (TYPE_MAX_VALUE (d2)))
667 && (TREE_INT_CST_HIGH (TYPE_MAX_VALUE (d1))
668 == TREE_INT_CST_HIGH (TYPE_MAX_VALUE (d2))));
671 /* Return 1 if TYPE1 and TYPE2 are compatible types for assignment
672 or various other operations. This is what ANSI C++ speaks of as
675 For C++: argument STRICT says we should be strict about this
678 2 : strict, except that if one type is a reference and
679 the other is not, compare the target type of the
680 reference to the type that's not a reference (ARM, p308).
681 This is used for checking for invalid overloading.
682 1 : strict (compared according to ANSI C)
683 This is used for checking whether two function decls match.
684 0 : <= (compared according to C++)
685 -1: <= or >= (relaxed)
687 Otherwise, pointers involving base classes and derived classes can
688 be mixed as valid: i.e. a pointer to a derived class may be converted
689 to a pointer to one of its base classes, as per C++. A pointer to
690 a derived class may be passed as a parameter to a function expecting a
691 pointer to a base classes. These allowances do not commute. In this
692 case, TYPE1 is assumed to be the base class, and TYPE2 is assumed to
693 be the derived class. */
696 comptypes (type1, type2, strict)
700 register tree t1 = type1;
701 register tree t2 = type2;
704 /* Suppress errors caused by previously reported errors */
709 /* This should never happen. */
710 my_friendly_assert (t1 != error_mark_node, 307);
712 if (t2 == error_mark_node)
717 /* Treat an enum type as the unsigned integer type of the same width. */
719 if (TREE_CODE (t1) == ENUMERAL_TYPE)
720 t1 = type_for_size (TYPE_PRECISION (t1), 1);
721 if (TREE_CODE (t2) == ENUMERAL_TYPE)
722 t2 = type_for_size (TYPE_PRECISION (t2), 1);
728 if (TYPE_PTRMEMFUNC_P (t1))
729 t1 = TYPE_PTRMEMFUNC_FN_TYPE (t1);
730 if (TYPE_PTRMEMFUNC_P (t2))
731 t2 = TYPE_PTRMEMFUNC_FN_TYPE (t2);
733 /* Different classes of types can't be compatible. */
735 if (TREE_CODE (t1) != TREE_CODE (t2))
738 && ((TREE_CODE (t1) == REFERENCE_TYPE)
739 ^ (TREE_CODE (t2) == REFERENCE_TYPE)))
741 if (TREE_CODE (t1) == REFERENCE_TYPE)
742 return comptypes (TREE_TYPE (t1), t2, 1);
743 return comptypes (t1, TREE_TYPE (t2), 1);
751 /* Qualifiers must match. */
753 if (TYPE_READONLY (t1) != TYPE_READONLY (t2))
755 if (TYPE_VOLATILE (t1) != TYPE_VOLATILE (t2))
757 if (strict > 0 && TYPE_FOR_JAVA (t1) != TYPE_FOR_JAVA (t2))
760 /* Allow for two different type nodes which have essentially the same
761 definition. Note that we already checked for equality of the type
762 qualifiers (just above). */
764 if (TYPE_MAIN_VARIANT (t1) == TYPE_MAIN_VARIANT (t2))
767 /* ??? COMP_TYPE_ATTRIBUTES is currently useless for variables as each
768 attribute is its own main variant (`val' will remain 0). */
769 #ifndef COMP_TYPE_ATTRIBUTES
770 #define COMP_TYPE_ATTRIBUTES(t1,t2) 1
773 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
774 if (! (attrval = COMP_TYPE_ATTRIBUTES (t1, t2)))
777 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
780 switch (TREE_CODE (t1))
782 case TEMPLATE_TEMPLATE_PARM:
783 if (TEMPLATE_TYPE_IDX (t1) != TEMPLATE_TYPE_IDX (t2)
784 || TEMPLATE_TYPE_LEVEL (t1) != TEMPLATE_TYPE_LEVEL (t2))
786 if (! CLASSTYPE_TEMPLATE_INFO (t1) && ! CLASSTYPE_TEMPLATE_INFO (t2))
788 /* Don't check inheritance. */
794 if (CLASSTYPE_TEMPLATE_INFO (t1) && CLASSTYPE_TEMPLATE_INFO (t2)
795 && (CLASSTYPE_TI_TEMPLATE (t1) == CLASSTYPE_TI_TEMPLATE (t2)
796 || TREE_CODE (t1) == TEMPLATE_TEMPLATE_PARM))
797 return comp_template_args (CLASSTYPE_TI_ARGS (t1),
798 CLASSTYPE_TI_ARGS (t2));
804 val = (comptypes (build_pointer_type (TYPE_OFFSET_BASETYPE (t1)),
805 build_pointer_type (TYPE_OFFSET_BASETYPE (t2)), strict)
806 && comptypes (TREE_TYPE (t1), TREE_TYPE (t2), strict));
810 if (! compexcepttypes (t1, t2))
813 /* This case is anti-symmetrical!
814 One can pass a base member (or member function)
815 to something expecting a derived member (or member function),
816 but not vice-versa! */
818 val = (comptypes (TREE_TYPE (t1), TREE_TYPE (t2), strict)
819 && compparms (TYPE_ARG_TYPES (t1),
820 TYPE_ARG_TYPES (t2), strict));
834 if (TREE_CODE (t1) == RECORD_TYPE && TREE_CODE (t2) == RECORD_TYPE)
838 rval = t1 == t2 || DERIVED_FROM_P (t1, t2);
847 val = DERIVED_FROM_P (t2, t1);
854 val = comptypes (t1, t2, strict);
858 if (! compexcepttypes (t1, t2))
861 val = ((TREE_TYPE (t1) == TREE_TYPE (t2)
862 || comptypes (TREE_TYPE (t1), TREE_TYPE (t2), strict))
863 && compparms (TYPE_ARG_TYPES (t1), TYPE_ARG_TYPES (t2), strict));
867 /* Target types must match incl. qualifiers. */
868 val = comp_array_types (comptypes, t1, t2, strict);
871 case TEMPLATE_TYPE_PARM:
872 return TEMPLATE_TYPE_IDX (t1) == TEMPLATE_TYPE_IDX (t2)
873 && TEMPLATE_TYPE_LEVEL (t1) == TEMPLATE_TYPE_LEVEL (t2);
876 if (TYPE_IDENTIFIER (t1) != TYPE_IDENTIFIER (t2))
878 return comptypes (TYPE_CONTEXT (t1), TYPE_CONTEXT (t2), 1);
883 return attrval == 2 && val == 1 ? 2 : val;
886 /* Return 1 or -1 if TTL and TTR are pointers to types that are equivalent,
887 ignoring their qualifiers, 0 if not. Return 1 means that TTR can be
888 converted to TTL. Return -1 means that TTL can be converted to TTR but
891 NPTRS is the number of pointers we can strip off and keep cool.
892 This is used to permit (for aggr A, aggr B) A, B* to convert to A*,
893 but to not permit B** to convert to A**.
895 This should go away. Callers should use can_convert or something
896 similar instead. (jason 17 Apr 1997) */
899 comp_target_types (ttl, ttr, nptrs)
903 ttl = TYPE_MAIN_VARIANT (ttl);
904 ttr = TYPE_MAIN_VARIANT (ttr);
908 if (TREE_CODE (ttr) != TREE_CODE (ttl))
911 if (TREE_CODE (ttr) == POINTER_TYPE
912 || (TREE_CODE (ttr) == REFERENCE_TYPE))
914 int is_ptr = TREE_CODE (ttr) == POINTER_TYPE;
916 ttl = TREE_TYPE (ttl);
917 ttr = TREE_TYPE (ttr);
919 if (nptrs > 0 && is_ptr)
921 if (TREE_CODE (ttl) == UNKNOWN_TYPE
922 || TREE_CODE (ttr) == UNKNOWN_TYPE)
924 else if (TREE_CODE (ttl) == VOID_TYPE
925 && TREE_CODE (ttr) != FUNCTION_TYPE
926 && TREE_CODE (ttr) != METHOD_TYPE
927 && TREE_CODE (ttr) != OFFSET_TYPE)
929 else if (TREE_CODE (ttr) == VOID_TYPE
930 && TREE_CODE (ttl) != FUNCTION_TYPE
931 && TREE_CODE (ttl) != METHOD_TYPE
932 && TREE_CODE (ttl) != OFFSET_TYPE)
934 else if (TREE_CODE (ttl) == POINTER_TYPE
935 || TREE_CODE (ttl) == ARRAY_TYPE)
937 if (comp_ptr_ttypes (ttl, ttr))
939 else if (comp_ptr_ttypes (ttr, ttl))
945 /* Const and volatile mean something different for function types,
946 so the usual checks are not appropriate. */
947 if (TREE_CODE (ttl) == FUNCTION_TYPE || TREE_CODE (ttl) == METHOD_TYPE)
948 return comp_target_types (ttl, ttr, nptrs - 1);
950 /* Make sure that the cv-quals change only in the same direction as
954 int c = TYPE_READONLY (ttl) - TYPE_READONLY (ttr);
955 int v = TYPE_VOLATILE (ttl) - TYPE_VOLATILE (ttr);
957 if ((c > 0 && v < 0) || (c < 0 && v > 0))
960 if (TYPE_MAIN_VARIANT (ttl) == TYPE_MAIN_VARIANT (ttr))
961 return (c + v < 0) ? -1 : 1;
963 t = comp_target_types (ttl, ttr, nptrs - 1);
964 if ((t == 1 && c + v >= 0) || (t == -1 && c + v <= 0))
971 if (TREE_CODE (ttr) == ARRAY_TYPE)
972 return comp_array_types (comp_target_types, ttl, ttr, 0);
973 else if (TREE_CODE (ttr) == FUNCTION_TYPE || TREE_CODE (ttr) == METHOD_TYPE)
980 if (comptypes (TREE_TYPE (ttl), TREE_TYPE (ttr), 1) == 0)
985 switch (comp_target_types (TREE_TYPE (ttl), TREE_TYPE (ttr), -1))
994 argsl = TYPE_ARG_TYPES (ttl);
995 argsr = TYPE_ARG_TYPES (ttr);
997 /* Compare 'this' here, not in comp_target_parms. */
998 if (TREE_CODE (ttr) == METHOD_TYPE)
1000 tree tl = TYPE_METHOD_BASETYPE (ttl);
1001 tree tr = TYPE_METHOD_BASETYPE (ttr);
1003 if (comptypes (tr, tl, 0) == 0)
1005 if (comptypes (tl, tr, 0))
1011 argsl = TREE_CHAIN (argsl);
1012 argsr = TREE_CHAIN (argsr);
1015 switch (comp_target_parms (argsl, argsr, 1))
1023 return saw_contra ? -1 : 1;
1026 else if (TREE_CODE (ttr) == OFFSET_TYPE)
1028 /* Contravariance: we can assign a pointer to base member to a pointer
1029 to derived member. Note difference from simple pointer case, where
1030 we can pass a pointer to derived to a pointer to base. */
1031 if (comptypes (TYPE_OFFSET_BASETYPE (ttr),
1032 TYPE_OFFSET_BASETYPE (ttl), 0))
1033 return comp_target_types (TREE_TYPE (ttl), TREE_TYPE (ttr), nptrs);
1034 else if (comptypes (TYPE_OFFSET_BASETYPE (ttl),
1035 TYPE_OFFSET_BASETYPE (ttr), 0)
1036 && comp_target_types (TREE_TYPE (ttl), TREE_TYPE (ttr), nptrs))
1039 else if (IS_AGGR_TYPE (ttl))
1043 if (comptypes (build_pointer_type (ttl), build_pointer_type (ttr), 0))
1045 if (comptypes (build_pointer_type (ttr), build_pointer_type (ttl), 0))
1053 /* Returns 1 if TYPE1 is more cv-qualified than TYPE2, -1 if TYPE2 is
1054 more cv-qualified that TYPE1, and 0 otherwise. */
1057 comp_cv_qualification (type1, type2)
1061 if (TYPE_READONLY (type1) == TYPE_READONLY (type2)
1062 && TYPE_VOLATILE (type1) == TYPE_VOLATILE (type2))
1065 if (TYPE_READONLY (type1) >= TYPE_READONLY (type2)
1066 && TYPE_VOLATILE (type1) >= TYPE_VOLATILE (type2))
1069 if (TYPE_READONLY (type2) >= TYPE_READONLY (type1)
1070 && TYPE_VOLATILE (type2) >= TYPE_VOLATILE (type1))
1076 /* Returns 1 if the cv-qualification signature of TYPE1 is a proper
1077 subset of the cv-qualification signature of TYPE2, and the types
1078 are similar. Returns -1 if the other way 'round, and 0 otherwise. */
1081 comp_cv_qual_signature (type1, type2)
1085 if (comp_ptr_ttypes_real (type2, type1, -1))
1087 else if (comp_ptr_ttypes_real (type1, type2, -1))
1093 /* If two types share a common base type, return that basetype.
1094 If there is not a unique most-derived base type, this function
1095 returns ERROR_MARK_NODE. */
1098 common_base_type (tt1, tt2)
1101 tree best = NULL_TREE;
1104 /* If one is a baseclass of another, that's good enough. */
1105 if (UNIQUELY_DERIVED_FROM_P (tt1, tt2))
1107 if (UNIQUELY_DERIVED_FROM_P (tt2, tt1))
1110 /* Otherwise, try to find a unique baseclass of TT1
1111 that is shared by TT2, and follow that down. */
1112 for (i = CLASSTYPE_N_BASECLASSES (tt1)-1; i >= 0; i--)
1114 tree basetype = TYPE_BINFO_BASETYPE (tt1, i);
1115 tree trial = common_base_type (basetype, tt2);
1118 if (trial == error_mark_node)
1120 if (best == NULL_TREE)
1122 else if (best != trial)
1123 return error_mark_node;
1128 for (i = CLASSTYPE_N_BASECLASSES (tt2)-1; i >= 0; i--)
1130 tree basetype = TYPE_BINFO_BASETYPE (tt2, i);
1131 tree trial = common_base_type (tt1, basetype);
1134 if (trial == error_mark_node)
1136 if (best == NULL_TREE)
1138 else if (best != trial)
1139 return error_mark_node;
1145 /* Subroutines of `comptypes'. */
1147 /* Return 1 if two parameter type lists PARMS1 and PARMS2
1148 are equivalent in the sense that functions with those parameter types
1149 can have equivalent types.
1150 If either list is empty, we win.
1151 Otherwise, the two lists must be equivalent, element by element.
1153 C++: See comment above about TYPE1, TYPE2.
1155 STRICT is no longer used. */
1158 compparms (parms1, parms2, strict)
1159 tree parms1, parms2;
1162 register tree t1 = parms1, t2 = parms2;
1164 /* An unspecified parmlist matches any specified parmlist
1165 whose argument types don't need default promotions. */
1169 if (t1 == 0 && t2 == 0)
1171 /* If one parmlist is shorter than the other,
1172 they fail to match. */
1173 if (t1 == 0 || t2 == 0)
1175 if (! comptypes (TREE_VALUE (t2), TREE_VALUE (t1), 1))
1178 t1 = TREE_CHAIN (t1);
1179 t2 = TREE_CHAIN (t2);
1183 /* This really wants return whether or not parameter type lists
1184 would make their owning functions assignment compatible or not.
1186 The return value is like for comp_target_types.
1188 This should go away, possibly with the exception of the empty parmlist
1189 conversion; there are no conversions between function types in C++.
1190 (jason 17 Apr 1997) */
1193 comp_target_parms (parms1, parms2, strict)
1194 tree parms1, parms2;
1197 register tree t1 = parms1, t2 = parms2;
1198 int warn_contravariance = 0;
1200 /* In C, an unspecified parmlist matches any specified parmlist
1201 whose argument types don't need default promotions. This is not
1202 true for C++, but let's do it anyway for unfixed headers. */
1204 if (t1 == 0 && t2 != 0)
1206 cp_pedwarn ("ANSI C++ prohibits conversion from `(%#T)' to `(...)'",
1208 return self_promoting_args_p (t2);
1211 return self_promoting_args_p (t1);
1213 for (; t1 || t2; t1 = TREE_CHAIN (t1), t2 = TREE_CHAIN (t2))
1217 /* If one parmlist is shorter than the other,
1218 they fail to match, unless STRICT is <= 0. */
1219 if (t1 == 0 || t2 == 0)
1224 return 1 + warn_contravariance;
1225 return ((t1 && TREE_PURPOSE (t1)) + warn_contravariance);
1227 p1 = TREE_VALUE (t1);
1228 p2 = TREE_VALUE (t2);
1229 if (comptypes (p1, p2, 1))
1235 if ((TREE_CODE (p1) == POINTER_TYPE && TREE_CODE (p2) == POINTER_TYPE)
1236 || (TREE_CODE (p1) == REFERENCE_TYPE
1237 && TREE_CODE (p2) == REFERENCE_TYPE))
1240 && (TYPE_MAIN_VARIANT (TREE_TYPE (p1))
1241 == TYPE_MAIN_VARIANT (TREE_TYPE (p2))))
1244 /* The following is wrong for contravariance,
1245 but many programs depend on it. */
1246 if (TREE_TYPE (p1) == void_type_node)
1248 if (TREE_TYPE (p2) == void_type_node)
1250 warn_contravariance = 1;
1253 if (IS_AGGR_TYPE (TREE_TYPE (p1)))
1255 if (comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (p1)),
1256 TYPE_MAIN_VARIANT (TREE_TYPE (p2)), 1) == 0)
1260 /* Note backwards order due to contravariance. */
1261 if (comp_target_types (p2, p1, 1) <= 0)
1263 if (comp_target_types (p1, p2, 1) > 0)
1265 warn_contravariance = 1;
1272 return warn_contravariance ? -1 : 1;
1275 /* Return 1 if PARMS specifies a fixed number of parameters
1276 and none of their types is affected by default promotions. */
1279 self_promoting_args_p (parms)
1283 for (t = parms; t; t = TREE_CHAIN (t))
1285 register tree type = TREE_VALUE (t);
1287 if (TREE_CHAIN (t) == 0 && type != void_type_node)
1293 if (TYPE_MAIN_VARIANT (type) == float_type_node)
1296 if (C_PROMOTING_INTEGER_TYPE_P (type))
1302 /* Return an unsigned type the same as TYPE in other respects.
1304 C++: must make these work for type variants as well. */
1307 unsigned_type (type)
1310 tree type1 = TYPE_MAIN_VARIANT (type);
1311 if (type1 == signed_char_type_node || type1 == char_type_node)
1312 return unsigned_char_type_node;
1313 if (type1 == integer_type_node)
1314 return unsigned_type_node;
1315 if (type1 == short_integer_type_node)
1316 return short_unsigned_type_node;
1317 if (type1 == long_integer_type_node)
1318 return long_unsigned_type_node;
1319 if (type1 == long_long_integer_type_node)
1320 return long_long_unsigned_type_node;
1321 if (type1 == intTI_type_node)
1322 return unsigned_intTI_type_node;
1323 if (type1 == intDI_type_node)
1324 return unsigned_intDI_type_node;
1325 if (type1 == intSI_type_node)
1326 return unsigned_intSI_type_node;
1327 if (type1 == intHI_type_node)
1328 return unsigned_intHI_type_node;
1329 if (type1 == intQI_type_node)
1330 return unsigned_intQI_type_node;
1332 return signed_or_unsigned_type (1, type);
1335 /* Return a signed type the same as TYPE in other respects. */
1341 tree type1 = TYPE_MAIN_VARIANT (type);
1342 if (type1 == unsigned_char_type_node || type1 == char_type_node)
1343 return signed_char_type_node;
1344 if (type1 == unsigned_type_node)
1345 return integer_type_node;
1346 if (type1 == short_unsigned_type_node)
1347 return short_integer_type_node;
1348 if (type1 == long_unsigned_type_node)
1349 return long_integer_type_node;
1350 if (type1 == long_long_unsigned_type_node)
1351 return long_long_integer_type_node;
1352 if (type1 == unsigned_intTI_type_node)
1353 return intTI_type_node;
1354 if (type1 == unsigned_intDI_type_node)
1355 return intDI_type_node;
1356 if (type1 == unsigned_intSI_type_node)
1357 return intSI_type_node;
1358 if (type1 == unsigned_intHI_type_node)
1359 return intHI_type_node;
1360 if (type1 == unsigned_intQI_type_node)
1361 return intQI_type_node;
1363 return signed_or_unsigned_type (0, type);
1366 /* Return a type the same as TYPE except unsigned or
1367 signed according to UNSIGNEDP. */
1370 signed_or_unsigned_type (unsignedp, type)
1374 if (! INTEGRAL_TYPE_P (type)
1375 || TREE_UNSIGNED (type) == unsignedp)
1378 if (TYPE_PRECISION (type) == TYPE_PRECISION (signed_char_type_node))
1379 return unsignedp ? unsigned_char_type_node : signed_char_type_node;
1380 if (TYPE_PRECISION (type) == TYPE_PRECISION (integer_type_node))
1381 return unsignedp ? unsigned_type_node : integer_type_node;
1382 if (TYPE_PRECISION (type) == TYPE_PRECISION (short_integer_type_node))
1383 return unsignedp ? short_unsigned_type_node : short_integer_type_node;
1384 if (TYPE_PRECISION (type) == TYPE_PRECISION (long_integer_type_node))
1385 return unsignedp ? long_unsigned_type_node : long_integer_type_node;
1386 if (TYPE_PRECISION (type) == TYPE_PRECISION (long_long_integer_type_node))
1387 return (unsignedp ? long_long_unsigned_type_node
1388 : long_long_integer_type_node);
1392 /* Compute the value of the `sizeof' operator. */
1398 enum tree_code code = TREE_CODE (type);
1401 if (processing_template_decl)
1402 return build_min (SIZEOF_EXPR, sizetype, type);
1404 if (code == FUNCTION_TYPE)
1406 if (pedantic || warn_pointer_arith)
1407 pedwarn ("ANSI C++ forbids taking the sizeof a function type");
1408 return size_int (1);
1410 if (code == METHOD_TYPE)
1412 if (pedantic || warn_pointer_arith)
1413 pedwarn ("ANSI C++ forbids taking the sizeof a method type");
1414 return size_int (1);
1416 if (code == VOID_TYPE)
1418 if (pedantic || warn_pointer_arith)
1419 pedwarn ("ANSI C++ forbids taking the sizeof a void type");
1420 return size_int (1);
1422 if (code == ERROR_MARK)
1423 return size_int (1);
1425 /* ARM $5.3.2: ``When applied to a reference, the result is the size of the
1426 referenced object.'' */
1427 if (code == REFERENCE_TYPE)
1428 type = TREE_TYPE (type);
1430 /* We couldn't find anything in the ARM or the draft standard that says,
1431 one way or the other, if doing sizeof on something that doesn't have
1432 an object associated with it is correct or incorrect. For example, if
1433 you declare `struct S { char str[16]; };', and in your program do
1434 a `sizeof (S::str)', should we flag that as an error or should we give
1435 the size of it? Since it seems like a reasonable thing to do, we'll go
1436 with giving the value. */
1437 if (code == OFFSET_TYPE)
1438 type = TREE_TYPE (type);
1440 /* @@ This also produces an error for a signature ref.
1441 In that case we should be able to do better. */
1442 if (IS_SIGNATURE (type))
1444 error ("`sizeof' applied to a signature type");
1445 return size_int (0);
1448 if (TYPE_SIZE (complete_type (type)) == 0)
1450 cp_error ("`sizeof' applied to incomplete type `%T'", type);
1451 return size_int (0);
1454 /* Convert in case a char is more than one unit. */
1455 t = size_binop (CEIL_DIV_EXPR, TYPE_SIZE (type),
1456 size_int (TYPE_PRECISION (char_type_node)));
1457 t = convert (sizetype, t);
1458 /* size_binop does not put the constant in range, so do it now. */
1459 if (TREE_CODE (t) == INTEGER_CST && force_fit_type (t, 0))
1460 TREE_CONSTANT_OVERFLOW (t) = TREE_OVERFLOW (t) = 1;
1468 if (processing_template_decl)
1469 return build_min (SIZEOF_EXPR, sizetype, e);
1471 if (TREE_CODE (e) == COMPONENT_REF
1472 && DECL_BIT_FIELD (TREE_OPERAND (e, 1)))
1473 error ("sizeof applied to a bit-field");
1474 /* ANSI says arrays and functions are converted inside comma.
1475 But we can't really convert them in build_compound_expr
1476 because that would break commas in lvalues.
1477 So do the conversion here if operand was a comma. */
1478 if (TREE_CODE (e) == COMPOUND_EXPR
1479 && (TREE_CODE (TREE_TYPE (e)) == ARRAY_TYPE
1480 || TREE_CODE (TREE_TYPE (e)) == FUNCTION_TYPE))
1481 e = default_conversion (e);
1482 else if (TREE_CODE (e) == TREE_LIST)
1484 tree t = TREE_VALUE (e);
1487 && TREE_CODE (TREE_TYPE (t)) == FUNCTION_TYPE)
1488 || is_overloaded_fn (t)))
1489 pedwarn ("ANSI C++ forbids taking the sizeof a function type");
1491 return c_sizeof (TREE_TYPE (e));
1495 c_sizeof_nowarn (type)
1498 enum tree_code code = TREE_CODE (type);
1501 if (code == FUNCTION_TYPE
1502 || code == METHOD_TYPE
1503 || code == VOID_TYPE
1504 || code == ERROR_MARK)
1505 return size_int (1);
1506 if (code == REFERENCE_TYPE)
1507 type = TREE_TYPE (type);
1509 if (TYPE_SIZE (type) == 0)
1510 return size_int (0);
1512 /* Convert in case a char is more than one unit. */
1513 t = size_binop (CEIL_DIV_EXPR, TYPE_SIZE (type),
1514 size_int (TYPE_PRECISION (char_type_node)));
1515 t = convert (sizetype, t);
1516 force_fit_type (t, 0);
1520 /* Implement the __alignof keyword: Return the minimum required
1521 alignment of TYPE, measured in bytes. */
1527 enum tree_code code = TREE_CODE (type);
1530 if (processing_template_decl)
1531 return build_min (ALIGNOF_EXPR, sizetype, type);
1533 if (code == FUNCTION_TYPE || code == METHOD_TYPE)
1534 return size_int (FUNCTION_BOUNDARY / BITS_PER_UNIT);
1536 if (code == VOID_TYPE || code == ERROR_MARK)
1537 return size_int (1);
1539 /* C++: this is really correct! */
1540 if (code == REFERENCE_TYPE)
1541 type = TREE_TYPE (type);
1543 /* @@ This also produces an error for a signature ref.
1544 In that case we should be able to do better. */
1545 if (IS_SIGNATURE (type))
1547 error ("`__alignof' applied to a signature type");
1548 return size_int (1);
1551 t = size_int (TYPE_ALIGN (type) / BITS_PER_UNIT);
1552 force_fit_type (t, 0);
1556 /* Perform default promotions for C data used in expressions.
1557 Arrays and functions are converted to pointers;
1558 enumeral types or short or char, to int.
1559 In addition, manifest constants symbols are replaced by their values.
1561 C++: this will automatically bash references to their target type. */
1564 decay_conversion (exp)
1567 register tree type = TREE_TYPE (exp);
1568 register enum tree_code code = TREE_CODE (type);
1570 if (code == OFFSET_TYPE)
1572 if (TREE_CODE (exp) == OFFSET_REF)
1573 return decay_conversion (resolve_offset_ref (exp));
1575 type = TREE_TYPE (type);
1576 code = TREE_CODE (type);
1578 if (type == unknown_type_node)
1580 cp_pedwarn ("assuming & on overloaded member function");
1581 return build_unary_op (ADDR_EXPR, exp, 0);
1585 if (code == REFERENCE_TYPE)
1587 exp = convert_from_reference (exp);
1588 type = TREE_TYPE (exp);
1589 code = TREE_CODE (type);
1592 /* Constants can be used directly unless they're not loadable. */
1593 if (TREE_CODE (exp) == CONST_DECL)
1594 exp = DECL_INITIAL (exp);
1595 /* Replace a nonvolatile const static variable with its value. */
1596 else if (TREE_READONLY_DECL_P (exp))
1598 exp = decl_constant_value (exp);
1599 type = TREE_TYPE (exp);
1602 if (TREE_CODE (exp) == NAMESPACE_DECL)
1604 cp_error ("namespace `%D' used as expression", exp);
1605 return error_mark_node;
1608 /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
1609 Leave such NOP_EXPRs, since RHS is being used in non-lvalue context. */
1611 if (code == VOID_TYPE)
1613 error ("void value not ignored as it ought to be");
1614 return error_mark_node;
1616 if (code == FUNCTION_TYPE)
1618 return build_unary_op (ADDR_EXPR, exp, 0);
1620 if (code == METHOD_TYPE)
1622 cp_pedwarn ("assuming & on `%E'", exp);
1623 return build_unary_op (ADDR_EXPR, exp, 0);
1625 if (code == ARRAY_TYPE)
1630 int constp, volatilep;
1632 if (TREE_CODE (exp) == INDIRECT_REF)
1634 /* Stripping away the INDIRECT_REF is not the right
1635 thing to do for references... */
1636 tree inner = TREE_OPERAND (exp, 0);
1637 if (TREE_CODE (TREE_TYPE (inner)) == REFERENCE_TYPE)
1639 inner = build1 (CONVERT_EXPR,
1640 build_pointer_type (TREE_TYPE
1641 (TREE_TYPE (inner))),
1643 TREE_CONSTANT (inner) = TREE_CONSTANT (TREE_OPERAND (inner, 0));
1645 return cp_convert (build_pointer_type (TREE_TYPE (type)), inner);
1648 if (TREE_CODE (exp) == COMPOUND_EXPR)
1650 tree op1 = decay_conversion (TREE_OPERAND (exp, 1));
1651 return build (COMPOUND_EXPR, TREE_TYPE (op1),
1652 TREE_OPERAND (exp, 0), op1);
1656 && ! (TREE_CODE (exp) == CONSTRUCTOR && TREE_STATIC (exp)))
1658 error ("invalid use of non-lvalue array");
1659 return error_mark_node;
1662 constp = volatilep = 0;
1663 if (TREE_CODE_CLASS (TREE_CODE (exp)) == 'r'
1664 || TREE_CODE_CLASS (TREE_CODE (exp)) == 'd')
1666 constp = TREE_READONLY (exp);
1667 volatilep = TREE_THIS_VOLATILE (exp);
1670 restype = TREE_TYPE (type);
1671 if (TYPE_READONLY (type) || TYPE_VOLATILE (type)
1672 || constp || volatilep)
1673 restype = cp_build_type_variant (restype,
1674 TYPE_READONLY (type) || constp,
1675 TYPE_VOLATILE (type) || volatilep);
1676 ptrtype = build_pointer_type (restype);
1678 if (TREE_CODE (exp) == VAR_DECL)
1680 /* ??? This is not really quite correct
1681 in that the type of the operand of ADDR_EXPR
1682 is not the target type of the type of the ADDR_EXPR itself.
1683 Question is, can this lossage be avoided? */
1684 adr = build1 (ADDR_EXPR, ptrtype, exp);
1685 if (mark_addressable (exp) == 0)
1686 return error_mark_node;
1687 TREE_CONSTANT (adr) = staticp (exp);
1688 TREE_SIDE_EFFECTS (adr) = 0; /* Default would be, same as EXP. */
1691 /* This way is better for a COMPONENT_REF since it can
1692 simplify the offset for a component. */
1693 adr = build_unary_op (ADDR_EXPR, exp, 1);
1694 return cp_convert (ptrtype, adr);
1701 default_conversion (exp)
1705 enum tree_code code;
1707 exp = decay_conversion (exp);
1709 type = TREE_TYPE (exp);
1710 code = TREE_CODE (type);
1712 if (INTEGRAL_CODE_P (code))
1714 tree t = type_promotes_to (type);
1716 return cp_convert (t, exp);
1722 /* Take the address of an inline function without setting TREE_ADDRESSABLE
1726 inline_conversion (exp)
1729 if (TREE_CODE (exp) == FUNCTION_DECL)
1731 tree type = build_type_variant
1732 (TREE_TYPE (exp), TREE_READONLY (exp), TREE_THIS_VOLATILE (exp));
1733 exp = build1 (ADDR_EXPR, build_pointer_type (type), exp);
1739 build_object_ref (datum, basetype, field)
1740 tree datum, basetype, field;
1743 if (datum == error_mark_node)
1744 return error_mark_node;
1746 dtype = TREE_TYPE (datum);
1747 if (TREE_CODE (dtype) == REFERENCE_TYPE)
1748 dtype = TREE_TYPE (dtype);
1749 if (! IS_AGGR_TYPE_CODE (TREE_CODE (dtype)))
1751 cp_error ("request for member `%T::%D' in expression of non-aggregate type `%T'",
1752 basetype, field, dtype);
1753 return error_mark_node;
1755 else if (IS_SIGNATURE (basetype))
1757 warning ("signature name in scope resolution ignored");
1758 return build_component_ref (datum, field, NULL_TREE, 1);
1760 else if (is_aggr_type (basetype, 1))
1762 tree binfo = binfo_or_else (basetype, dtype);
1764 return build_x_component_ref (build_scoped_ref (datum, basetype),
1767 return error_mark_node;
1770 /* Like `build_component_ref, but uses an already found field, and converts
1771 from a reference. Must compute access for current_class_ref.
1775 build_component_ref_1 (datum, field, protect)
1779 return convert_from_reference
1780 (build_component_ref (datum, field, NULL_TREE, protect));
1783 /* Given a COND_EXPR, MIN_EXPR, or MAX_EXPR in T, return it in a form that we
1784 can, for example, use as an lvalue. This code used to be in
1785 unary_complex_lvalue, but we needed it to deal with `a = (d == c) ? b : c'
1786 expressions, where we're dealing with aggregates. But now it's again only
1787 called from unary_complex_lvalue. The case (in particular) that led to
1788 this was with CODE == ADDR_EXPR, since it's not an lvalue when we'd
1792 rationalize_conditional_expr (code, t)
1793 enum tree_code code;
1796 /* For MIN_EXPR or MAX_EXPR, fold-const.c has arranged things so that
1797 the first operand is always the one to be used if both operands
1798 are equal, so we know what conditional expression this used to be. */
1799 if (TREE_CODE (t) == MIN_EXPR || TREE_CODE (t) == MAX_EXPR)
1802 build_conditional_expr (build_x_binary_op ((TREE_CODE (t) == MIN_EXPR
1803 ? LE_EXPR : GE_EXPR),
1804 TREE_OPERAND (t, 0),
1805 TREE_OPERAND (t, 1)),
1806 build_unary_op (code, TREE_OPERAND (t, 0), 0),
1807 build_unary_op (code, TREE_OPERAND (t, 1), 0));
1811 build_conditional_expr (TREE_OPERAND (t, 0),
1812 build_unary_op (code, TREE_OPERAND (t, 1), 0),
1813 build_unary_op (code, TREE_OPERAND (t, 2), 0));
1816 /* Given the TYPE of an anonymous union field inside T, return the
1817 FIELD_DECL for the field. If not found return NULL_TREE. Because
1818 anonymous unions can nest, we must also search all anonymous unions
1819 that are directly reachable. */
1822 lookup_anon_field (t, type)
1827 for (field = TYPE_FIELDS (t); field; field = TREE_CHAIN (field))
1829 if (TREE_STATIC (field))
1831 if (TREE_CODE (field) != FIELD_DECL)
1834 /* If we find it directly, return the field. */
1835 if (DECL_NAME (field) == NULL_TREE
1836 && type == TREE_TYPE (field))
1841 /* Otherwise, it could be nested, search harder. */
1842 if (DECL_NAME (field) == NULL_TREE
1843 && TREE_CODE (TREE_TYPE (field)) == UNION_TYPE)
1845 tree subfield = lookup_anon_field (TREE_TYPE (field), type);
1853 /* Build a COMPONENT_REF for a given DATUM, and it's member COMPONENT.
1854 COMPONENT can be an IDENTIFIER_NODE that is the name of the member
1855 that we are interested in, or it can be a FIELD_DECL. */
1858 build_component_ref (datum, component, basetype_path, protect)
1859 tree datum, component, basetype_path;
1862 register tree basetype = TREE_TYPE (datum);
1863 register enum tree_code code;
1864 register tree field = NULL;
1867 if (processing_template_decl)
1868 return build_min_nt (COMPONENT_REF, datum, component);
1870 /* If DATUM is a COMPOUND_EXPR or COND_EXPR, move our reference
1872 switch (TREE_CODE (datum))
1876 tree value = build_component_ref (TREE_OPERAND (datum, 1), component,
1877 basetype_path, protect);
1878 return build (COMPOUND_EXPR, TREE_TYPE (value),
1879 TREE_OPERAND (datum, 0), value);
1882 return build_conditional_expr
1883 (TREE_OPERAND (datum, 0),
1884 build_component_ref (TREE_OPERAND (datum, 1), component,
1885 basetype_path, protect),
1886 build_component_ref (TREE_OPERAND (datum, 2), component,
1887 basetype_path, protect));
1890 cp_error ("invalid use of %D", datum);
1891 datum = error_mark_node;
1898 code = TREE_CODE (basetype);
1900 if (code == REFERENCE_TYPE)
1902 datum = convert_from_reference (datum);
1903 basetype = TREE_TYPE (datum);
1904 code = TREE_CODE (basetype);
1906 if (TREE_CODE (datum) == OFFSET_REF)
1908 datum = resolve_offset_ref (datum);
1909 basetype = TREE_TYPE (datum);
1910 code = TREE_CODE (basetype);
1913 /* First, see if there is a field or component with name COMPONENT. */
1914 if (TREE_CODE (component) == TREE_LIST)
1916 /* I could not trigger this code. MvL */
1917 my_friendly_abort (980326);
1919 my_friendly_assert (!(TREE_CHAIN (component) == NULL_TREE
1920 && DECL_CHAIN (TREE_VALUE (component)) == NULL_TREE), 309);
1922 return build (COMPONENT_REF, TREE_TYPE (component), datum, component);
1925 if (! IS_AGGR_TYPE_CODE (code))
1927 if (code != ERROR_MARK)
1928 cp_error ("request for member `%D' in `%E', which is of non-aggregate type `%T'",
1929 component, datum, basetype);
1930 return error_mark_node;
1933 if (TYPE_SIZE (complete_type (basetype)) == 0)
1935 incomplete_type_error (0, basetype);
1936 return error_mark_node;
1939 if (TREE_CODE (component) == BIT_NOT_EXPR)
1941 if (TYPE_IDENTIFIER (basetype) != TREE_OPERAND (component, 0))
1943 cp_error ("destructor specifier `%T::~%T' must have matching names",
1944 basetype, TREE_OPERAND (component, 0));
1945 return error_mark_node;
1947 if (! TYPE_HAS_DESTRUCTOR (basetype))
1949 cp_error ("type `%T' has no destructor", basetype);
1950 return error_mark_node;
1952 return TREE_VEC_ELT (CLASSTYPE_METHOD_VEC (basetype), 1);
1955 /* Look up component name in the structure type definition. */
1956 if (CLASSTYPE_VFIELD (basetype)
1957 && DECL_NAME (CLASSTYPE_VFIELD (basetype)) == component)
1958 /* Special-case this because if we use normal lookups in an ambiguous
1959 hierarchy, the compiler will abort (because vptr lookups are
1960 not supposed to be ambiguous. */
1961 field = CLASSTYPE_VFIELD (basetype);
1962 else if (TREE_CODE (component) == FIELD_DECL)
1964 else if (TREE_CODE (component) == TYPE_DECL)
1966 cp_pedwarn ("invalid use of type decl `%#D' as expression", component);
1971 tree name = component;
1972 if (TREE_CODE (component) == VAR_DECL)
1973 name = DECL_NAME (component);
1974 if (basetype_path == NULL_TREE)
1975 basetype_path = TYPE_BINFO (basetype);
1976 field = lookup_field (basetype_path, name,
1977 protect && !VFIELD_NAME_P (name), 0);
1978 if (field == error_mark_node)
1979 return error_mark_node;
1981 if (field == NULL_TREE)
1983 /* Not found as a data field, look for it as a method. If found,
1984 then if this is the only possible one, return it, else
1985 report ambiguity error. */
1986 tree fndecls = lookup_fnfields (basetype_path, name, 1);
1987 if (fndecls == error_mark_node)
1988 return error_mark_node;
1991 if (TREE_CHAIN (fndecls) == NULL_TREE
1992 && TREE_CODE (TREE_VALUE (fndecls)) != OVERLOAD)
1994 tree access, fndecl;
1996 /* Unique, so use this one now. */
1997 basetype = TREE_PURPOSE (fndecls);
1998 fndecl = TREE_VALUE (fndecls);
1999 access = compute_access (TREE_PURPOSE (fndecls), fndecl);
2000 if (access == access_public_node)
2002 if (DECL_VINDEX (fndecl)
2003 && ! resolves_to_fixed_type_p (datum, 0))
2005 tree addr = build_unary_op (ADDR_EXPR, datum, 0);
2006 tree fntype = TREE_TYPE (fndecl);
2008 addr = convert_pointer_to (DECL_CONTEXT (fndecl),
2010 datum = build_indirect_ref (addr, NULL_PTR);
2011 my_friendly_assert (datum != error_mark_node, 310);
2012 fndecl = build_vfn_ref (&addr, datum,
2013 DECL_VINDEX (fndecl));
2014 /* The type of fndecl is a function type,
2015 not a pointer-to-function type, since
2016 build_vfn_ref returns not the correct
2017 vtable slot, but the indirection of the
2018 correct vtable slot. */
2019 TREE_TYPE (fndecl) = fntype;
2023 return build (OFFSET_REF, TREE_TYPE (fndecl),
2026 if (access == access_protected_node)
2027 cp_error ("member function `%D' is protected", fndecl);
2029 cp_error ("member function `%D' is private", fndecl);
2030 return error_mark_node;
2034 /* Just act like build_offset_ref, since the object does
2035 not matter unless we're actually calling the function. */
2038 t = build_tree_list (error_mark_node, fndecls);
2039 TREE_TYPE (t) = build_offset_type (basetype,
2045 cp_error ("`%#T' has no member named `%D'", basetype, name);
2046 return error_mark_node;
2048 else if (TREE_TYPE (field) == error_mark_node)
2049 return error_mark_node;
2051 if (TREE_CODE (field) != FIELD_DECL)
2053 if (TREE_CODE (field) == TYPE_DECL)
2054 cp_pedwarn ("invalid use of type decl `%#D' as expression", field);
2055 else if (DECL_RTL (field) != 0)
2058 TREE_USED (field) = 1;
2063 /* See if we have to do any conversions so that we pick up the field from the
2065 if (DECL_FIELD_CONTEXT (field) != basetype)
2067 tree context = DECL_FIELD_CONTEXT (field);
2068 tree base = context;
2069 while (!comptypes (base, basetype,1) && TYPE_NAME (base)
2070 && ANON_UNION_TYPE_P (base))
2072 base = TYPE_CONTEXT (base);
2075 /* Handle base classes here... */
2076 if (base != basetype && TYPE_USES_COMPLEX_INHERITANCE (basetype))
2078 tree addr = build_unary_op (ADDR_EXPR, datum, 0);
2079 if (integer_zerop (addr))
2081 error ("invalid reference to NULL ptr, use ptr-to-member instead");
2082 return error_mark_node;
2084 if (VBASE_NAME_P (DECL_NAME (field)))
2086 /* It doesn't matter which vbase pointer we grab, just
2087 find one of them. */
2088 tree binfo = get_binfo (base,
2089 TREE_TYPE (TREE_TYPE (addr)), 0);
2090 addr = convert_pointer_to_real (binfo, addr);
2093 addr = convert_pointer_to (base, addr);
2094 datum = build_indirect_ref (addr, NULL_PTR);
2095 my_friendly_assert (datum != error_mark_node, 311);
2099 /* Handle things from anon unions here... */
2100 if (TYPE_NAME (context) && ANON_UNION_TYPE_P (context))
2102 tree subfield = lookup_anon_field (basetype, context);
2103 tree subdatum = build_component_ref (datum, subfield,
2104 basetype_path, protect);
2105 return build_component_ref (subdatum, field, basetype_path, protect);
2109 ref = fold (build (COMPONENT_REF, TREE_TYPE (field),
2110 break_out_cleanups (datum), field));
2112 if (TREE_READONLY (datum) || TREE_READONLY (field))
2113 TREE_READONLY (ref) = 1;
2114 if (TREE_THIS_VOLATILE (datum) || TREE_THIS_VOLATILE (field))
2115 TREE_THIS_VOLATILE (ref) = 1;
2116 if (DECL_LANG_SPECIFIC (field) && DECL_MUTABLE_P (field))
2117 TREE_READONLY (ref) = 0;
2122 /* Variant of build_component_ref for use in expressions, which should
2123 never have REFERENCE_TYPE. */
2126 build_x_component_ref (datum, component, basetype_path, protect)
2127 tree datum, component, basetype_path;
2130 tree t = build_component_ref (datum, component, basetype_path, protect);
2132 if (! processing_template_decl)
2133 t = convert_from_reference (t);
2138 /* Given an expression PTR for a pointer, return an expression
2139 for the value pointed to.
2140 ERRORSTRING is the name of the operator to appear in error messages.
2142 This function may need to overload OPERATOR_FNNAME.
2143 Must also handle REFERENCE_TYPEs for C++. */
2146 build_x_indirect_ref (ptr, errorstring)
2152 if (processing_template_decl)
2153 return build_min_nt (INDIRECT_REF, ptr);
2155 rval = build_opfncall (INDIRECT_REF, LOOKUP_NORMAL, ptr, NULL_TREE,
2159 return build_indirect_ref (ptr, errorstring);
2163 build_indirect_ref (ptr, errorstring)
2167 register tree pointer, type;
2169 if (ptr == error_mark_node)
2170 return error_mark_node;
2172 pointer = (TREE_CODE (TREE_TYPE (ptr)) == REFERENCE_TYPE
2173 ? ptr : default_conversion (ptr));
2174 type = TREE_TYPE (pointer);
2176 if (ptr == current_class_ptr)
2177 return current_class_ref;
2179 if (TREE_CODE (type) == POINTER_TYPE || TREE_CODE (type) == REFERENCE_TYPE)
2181 if (TREE_CODE (pointer) == ADDR_EXPR
2183 && (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_OPERAND (pointer, 0)))
2184 == TYPE_MAIN_VARIANT (TREE_TYPE (type)))
2185 && (TREE_READONLY (TREE_OPERAND (pointer, 0))
2186 == TYPE_READONLY (TREE_TYPE (type)))
2187 && (TREE_THIS_VOLATILE (TREE_OPERAND (pointer, 0))
2188 == TYPE_VOLATILE (TREE_TYPE (type))))
2189 return TREE_OPERAND (pointer, 0);
2192 tree t = TREE_TYPE (type);
2193 register tree ref = build1 (INDIRECT_REF,
2194 TYPE_MAIN_VARIANT (t), pointer);
2196 /* We *must* set TREE_READONLY when dereferencing a pointer to const,
2197 so that we get the proper error message if the result is used
2198 to assign to. Also, &* is supposed to be a no-op. */
2199 TREE_READONLY (ref) = TYPE_READONLY (t);
2200 TREE_SIDE_EFFECTS (ref)
2201 = (TYPE_VOLATILE (t) || TREE_SIDE_EFFECTS (pointer)
2203 TREE_THIS_VOLATILE (ref) = TYPE_VOLATILE (t);
2207 /* `pointer' won't be an error_mark_node if we were given a
2208 pointer to member, so it's cool to check for this here. */
2209 else if (TYPE_PTRMEMFUNC_P (type))
2210 error ("invalid use of `%s' on pointer to member function", errorstring);
2211 else if (TREE_CODE (type) == RECORD_TYPE
2212 && (IS_SIGNATURE_POINTER (type) || IS_SIGNATURE_REFERENCE (type)))
2213 error ("cannot dereference signature pointer/reference");
2214 else if (pointer != error_mark_node)
2217 error ("invalid type argument of `%s'", errorstring);
2219 error ("invalid type argument");
2221 return error_mark_node;
2224 /* This handles expressions of the form "a[i]", which denotes
2227 This is logically equivalent in C to *(a+i), but we may do it differently.
2228 If A is a variable or a member, we generate a primitive ARRAY_REF.
2229 This avoids forcing the array out of registers, and can work on
2230 arrays that are not lvalues (for example, members of structures returned
2233 If INDEX is of some user-defined type, it must be converted to
2234 integer type. Otherwise, to make a compatible PLUS_EXPR, it
2235 will inherit the type of the array, which will be some pointer type. */
2238 build_array_ref (array, idx)
2243 error ("subscript missing in array reference");
2244 return error_mark_node;
2247 if (TREE_TYPE (array) == error_mark_node
2248 || TREE_TYPE (idx) == error_mark_node)
2249 return error_mark_node;
2251 if (TREE_CODE (TREE_TYPE (array)) == ARRAY_TYPE
2252 && TREE_CODE (array) != INDIRECT_REF)
2256 /* Subscripting with type char is likely to lose
2257 on a machine where chars are signed.
2258 So warn on any machine, but optionally.
2259 Don't warn for unsigned char since that type is safe.
2260 Don't warn for signed char because anyone who uses that
2261 must have done so deliberately. */
2262 if (warn_char_subscripts
2263 && TYPE_MAIN_VARIANT (TREE_TYPE (idx)) == char_type_node)
2264 warning ("array subscript has type `char'");
2266 /* Apply default promotions *after* noticing character types. */
2267 idx = default_conversion (idx);
2269 if (TREE_CODE (TREE_TYPE (idx)) != INTEGER_TYPE)
2271 error ("array subscript is not an integer");
2272 return error_mark_node;
2275 /* An array that is indexed by a non-constant
2276 cannot be stored in a register; we must be able to do
2277 address arithmetic on its address.
2278 Likewise an array of elements of variable size. */
2279 if (TREE_CODE (idx) != INTEGER_CST
2280 || (TYPE_SIZE (TREE_TYPE (TREE_TYPE (array))) != 0
2281 && (TREE_CODE (TYPE_SIZE (TREE_TYPE (TREE_TYPE (array))))
2284 if (mark_addressable (array) == 0)
2285 return error_mark_node;
2287 /* An array that is indexed by a constant value which is not within
2288 the array bounds cannot be stored in a register either; because we
2289 would get a crash in store_bit_field/extract_bit_field when trying
2290 to access a non-existent part of the register. */
2291 if (TREE_CODE (idx) == INTEGER_CST
2292 && TYPE_VALUES (TREE_TYPE (array))
2293 && ! int_fits_type_p (idx, TYPE_VALUES (TREE_TYPE (array))))
2295 if (mark_addressable (array) == 0)
2296 return error_mark_node;
2299 if (pedantic && !lvalue_p (array))
2300 pedwarn ("ANSI C++ forbids subscripting non-lvalue array");
2302 /* Note in C++ it is valid to subscript a `register' array, since
2303 it is valid to take the address of something with that
2304 storage specification. */
2308 while (TREE_CODE (foo) == COMPONENT_REF)
2309 foo = TREE_OPERAND (foo, 0);
2310 if (TREE_CODE (foo) == VAR_DECL && DECL_REGISTER (foo))
2311 warning ("subscripting array declared `register'");
2314 type = TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (array)));
2315 rval = build (ARRAY_REF, type, array, idx);
2316 /* Array ref is const/volatile if the array elements are
2317 or if the array is.. */
2318 TREE_READONLY (rval)
2319 |= (TYPE_READONLY (TREE_TYPE (TREE_TYPE (array)))
2320 | TREE_READONLY (array));
2321 TREE_SIDE_EFFECTS (rval)
2322 |= (TYPE_VOLATILE (TREE_TYPE (TREE_TYPE (array)))
2323 | TREE_SIDE_EFFECTS (array));
2324 TREE_THIS_VOLATILE (rval)
2325 |= (TYPE_VOLATILE (TREE_TYPE (TREE_TYPE (array)))
2326 /* This was added by rms on 16 Nov 91.
2327 It fixes vol struct foo *a; a->elts[1]
2328 in an inline function.
2329 Hope it doesn't break something else. */
2330 | TREE_THIS_VOLATILE (array));
2331 return require_complete_type (fold (rval));
2335 tree ar = default_conversion (array);
2336 tree ind = default_conversion (idx);
2338 /* Put the integer in IND to simplify error checking. */
2339 if (TREE_CODE (TREE_TYPE (ar)) == INTEGER_TYPE)
2346 if (ar == error_mark_node)
2349 if (TREE_CODE (TREE_TYPE (ar)) != POINTER_TYPE)
2351 error ("subscripted value is neither array nor pointer");
2352 return error_mark_node;
2354 if (TREE_CODE (TREE_TYPE (ind)) != INTEGER_TYPE)
2356 error ("array subscript is not an integer");
2357 return error_mark_node;
2360 return build_indirect_ref (build_binary_op_nodefault (PLUS_EXPR, ar,
2366 /* Build a function call to function FUNCTION with parameters PARAMS.
2367 PARAMS is a list--a chain of TREE_LIST nodes--in which the
2368 TREE_VALUE of each node is a parameter-expression. The PARAMS do
2369 not include any object pointer that may be required. FUNCTION's
2370 data type may be a function type or a pointer-to-function.
2372 For C++: If FUNCTION's data type is a TREE_LIST, then the tree list
2373 is the list of possible methods that FUNCTION could conceivably
2374 be. If the list of methods comes from a class, then it will be
2375 a list of lists (where each element is associated with the class
2376 that produced it), otherwise it will be a simple list (for
2377 functions overloaded in global scope).
2379 In the first case, TREE_VALUE (function) is the head of one of those
2380 lists, and TREE_PURPOSE is the name of the function.
2382 In the second case, TREE_PURPOSE (function) is the function's
2385 DECL is the class instance variable, usually CURRENT_CLASS_REF.
2387 When calling a TEMPLATE_DECL, we don't require a complete return
2391 build_x_function_call (function, params, decl)
2392 tree function, params, decl;
2395 tree template_id = NULL_TREE;
2398 if (function == error_mark_node)
2399 return error_mark_node;
2401 if (processing_template_decl)
2402 return build_min_nt (CALL_EXPR, function, params, NULL_TREE);
2404 /* Save explicit template arguments if found */
2405 if (TREE_CODE (function) == TEMPLATE_ID_EXPR)
2407 template_id = function;
2408 function = TREE_OPERAND (function, 0);
2411 type = TREE_TYPE (function);
2413 if (TREE_CODE (type) == OFFSET_TYPE
2414 && TREE_TYPE (type) == unknown_type_node
2415 && TREE_CODE (function) == TREE_LIST
2416 && TREE_CHAIN (function) == NULL_TREE)
2418 /* Undo (Foo:bar)()... */
2419 type = TYPE_OFFSET_BASETYPE (type);
2420 function = TREE_VALUE (function);
2421 my_friendly_assert (TREE_CODE (function) == TREE_LIST, 999);
2422 my_friendly_assert (TREE_CHAIN (function) == NULL_TREE, 999);
2423 function = TREE_VALUE (function);
2424 my_friendly_assert (TREE_CODE (function) == FUNCTION_DECL, 999);
2425 function = DECL_NAME (function);
2426 return build_method_call (decl, function, params,
2427 TYPE_BINFO (type), LOOKUP_NORMAL);
2430 is_method = ((TREE_CODE (function) == TREE_LIST
2431 && current_class_type != NULL_TREE
2432 && (IDENTIFIER_CLASS_VALUE (TREE_PURPOSE (function))
2434 || TREE_CODE (function) == IDENTIFIER_NODE
2435 || TREE_CODE (type) == METHOD_TYPE
2436 || TYPE_PTRMEMFUNC_P (type));
2438 if ((TREE_CODE (function) == FUNCTION_DECL
2439 && DECL_STATIC_FUNCTION_P (function))
2440 || (TREE_CODE (function) == TEMPLATE_DECL
2441 && DECL_STATIC_FUNCTION_P (DECL_RESULT (function))))
2442 return build_member_call
2443 (DECL_CONTEXT (function), DECL_NAME (function), params);
2445 /* A friend template. Make it look like a toplevel declaration. */
2446 if (! is_method && TREE_CODE (function) == TEMPLATE_DECL)
2447 function = scratch_ovl_cons (function, NULL_TREE);
2449 /* Handle methods, friends, and overloaded functions, respectively. */
2452 tree basetype = NULL_TREE;
2454 if (TREE_CODE (function) == FUNCTION_DECL
2455 || DECL_FUNCTION_TEMPLATE_P (function))
2457 basetype = DECL_CLASS_CONTEXT (function);
2459 if (DECL_NAME (function))
2460 function = DECL_NAME (function);
2462 function = TYPE_IDENTIFIER (DECL_CLASS_CONTEXT (function));
2464 else if (TREE_CODE (function) == TREE_LIST)
2466 my_friendly_assert (TREE_CODE (TREE_VALUE (function))
2467 == FUNCTION_DECL, 312);
2468 basetype = DECL_CLASS_CONTEXT (TREE_VALUE (function));
2469 function = TREE_PURPOSE (function);
2471 else if (TREE_CODE (function) != IDENTIFIER_NODE)
2473 if (TREE_CODE (function) == OFFSET_REF)
2475 if (TREE_OPERAND (function, 0))
2476 decl = TREE_OPERAND (function, 0);
2478 /* Call via a pointer to member function. */
2479 if (decl == NULL_TREE)
2481 error ("pointer to member function called, but not in class scope");
2482 return error_mark_node;
2484 /* What other type of POINTER_TYPE could this be? */
2485 if (TREE_CODE (TREE_TYPE (function)) != POINTER_TYPE
2486 && ! TYPE_PTRMEMFUNC_P (TREE_TYPE (function))
2487 && TREE_CODE (function) != OFFSET_REF)
2488 function = build (OFFSET_REF, TREE_TYPE (type), NULL_TREE,
2493 /* this is an abbreviated method call.
2494 must go through here in case it is a virtual function.
2495 @@ Perhaps this could be optimized. */
2497 if (basetype && (! current_class_type
2498 || ! DERIVED_FROM_P (basetype, current_class_type)))
2499 return build_member_call (basetype, function, params);
2501 if (decl == NULL_TREE)
2503 if (current_class_type == NULL_TREE)
2505 error ("object missing in call to method `%s'",
2506 IDENTIFIER_POINTER (function));
2507 return error_mark_node;
2509 /* Yow: call from a static member function. */
2510 decl = build1 (NOP_EXPR, build_pointer_type (current_class_type),
2512 decl = build_indirect_ref (decl, NULL_PTR);
2515 /* Put back explicit template arguments, if any. */
2517 function = template_id;
2518 return build_method_call (decl, function, params,
2519 NULL_TREE, LOOKUP_NORMAL);
2521 else if (TREE_CODE (function) == COMPONENT_REF
2522 && type == unknown_type_node)
2524 /* Should we undo what was done in build_component_ref? */
2525 if (TREE_CODE (TREE_PURPOSE (TREE_OPERAND (function, 1))) == TREE_VEC)
2526 /* Get the name that build_component_ref hid. */
2527 function = DECL_NAME (TREE_VALUE (TREE_OPERAND (function, 1)));
2529 function = TREE_PURPOSE (TREE_OPERAND (function, 1));
2530 return build_method_call (decl, function, params,
2531 NULL_TREE, LOOKUP_NORMAL);
2533 else if (really_overloaded_fn (function))
2535 if (OVL_FUNCTION (function) == NULL_TREE)
2537 cp_error ("function `%D' declared overloaded, but no definitions appear with which to resolve it?!?",
2538 TREE_PURPOSE (function));
2539 return error_mark_node;
2543 /* Put back explicit template arguments, if any. */
2545 function = template_id;
2546 return build_new_function_call (function, params);
2550 /* Remove a potential OVERLOAD around it */
2551 function = OVL_CURRENT (function);
2554 if (TREE_CODE (function) == OFFSET_REF)
2556 /* If the component is a data element (or a virtual function), we play
2557 games here to make things work. */
2560 if (TREE_OPERAND (function, 0))
2561 decl = TREE_OPERAND (function, 0);
2563 decl = current_class_ref;
2565 decl_addr = build_unary_op (ADDR_EXPR, decl, 0);
2567 /* Sigh. OFFSET_REFs are being used for too many things.
2568 They're being used both for -> and ->*, and we want to resolve
2569 the -> cases here, but leave the ->*. We could use
2570 resolve_offset_ref for those, too, but it would call
2571 get_member_function_from_ptrfunc and decl_addr wouldn't get
2572 updated properly. Nasty. */
2573 if (TREE_CODE (TREE_OPERAND (function, 1)) == FIELD_DECL)
2574 function = resolve_offset_ref (function);
2576 function = TREE_OPERAND (function, 1);
2578 function = get_member_function_from_ptrfunc (&decl_addr, function);
2579 params = expr_tree_cons (NULL_TREE, decl_addr, params);
2580 return build_function_call (function, params);
2583 type = TREE_TYPE (function);
2584 if (type != error_mark_node)
2586 if (TREE_CODE (type) == REFERENCE_TYPE)
2587 type = TREE_TYPE (type);
2589 if (IS_AGGR_TYPE (type))
2590 return build_opfncall (CALL_EXPR, LOOKUP_NORMAL, function, params, NULL_TREE);
2595 tree fntype = TREE_TYPE (function);
2596 tree ctypeptr = NULL_TREE;
2598 /* Explicitly named method? */
2599 if (TREE_CODE (function) == FUNCTION_DECL)
2600 ctypeptr = build_pointer_type (DECL_CLASS_CONTEXT (function));
2601 /* Expression with ptr-to-method type? It could either be a plain
2602 usage, or it might be a case where the ptr-to-method is being
2603 passed in as an argument. */
2604 else if (TYPE_PTRMEMFUNC_P (fntype))
2606 tree rec = TYPE_METHOD_BASETYPE (TREE_TYPE
2607 (TYPE_PTRMEMFUNC_FN_TYPE (fntype)));
2608 ctypeptr = build_pointer_type (rec);
2610 /* Unexpected node type? */
2612 my_friendly_abort (116);
2613 if (decl == NULL_TREE)
2615 if (current_function_decl
2616 && DECL_STATIC_FUNCTION_P (current_function_decl))
2617 error ("invalid call to member function needing `this' in static member function scope");
2619 error ("pointer to member function called, but not in class scope");
2620 return error_mark_node;
2622 if (TREE_CODE (TREE_TYPE (decl)) != POINTER_TYPE
2623 && ! TYPE_PTRMEMFUNC_P (TREE_TYPE (decl)))
2625 decl = build_unary_op (ADDR_EXPR, decl, 0);
2626 decl = convert_pointer_to (TREE_TYPE (ctypeptr), decl);
2629 decl = build_c_cast (ctypeptr, decl);
2630 params = expr_tree_cons (NULL_TREE, decl, params);
2633 return build_function_call (function, params);
2636 /* Resolve a pointer to member function. INSTANCE is the object
2637 instance to use, if the member points to a virtual member. */
2640 get_member_function_from_ptrfunc (instance_ptrptr, function)
2641 tree *instance_ptrptr;
2644 if (TREE_CODE (function) == OFFSET_REF)
2646 function = TREE_OPERAND (function, 1);
2649 if (TYPE_PTRMEMFUNC_P (TREE_TYPE (function)))
2651 tree fntype, idx, e1, delta, delta2, e2, e3, aref, vtbl;
2654 tree instance_ptr = *instance_ptrptr;
2656 if (TREE_SIDE_EFFECTS (instance_ptr))
2657 instance_ptr = save_expr (instance_ptr);
2659 if (TREE_SIDE_EFFECTS (function))
2660 function = save_expr (function);
2662 fntype = TYPE_PTRMEMFUNC_FN_TYPE (TREE_TYPE (function));
2664 /* Promoting idx before saving it improves performance on RISC
2665 targets. Without promoting, the first compare used
2666 load-with-sign-extend, while the second used normal load then
2667 shift to sign-extend. An optimizer flaw, perhaps, but it's easier
2668 to make this change. */
2669 idx = save_expr (default_conversion
2670 (build_component_ref (function,
2673 e1 = build_binary_op (GT_EXPR, idx, integer_zero_node, 1);
2674 delta = cp_convert (ptrdiff_type_node,
2675 build_component_ref (function, delta_identifier,
2677 delta2 = DELTA2_FROM_PTRMEMFUNC (function);
2679 /* Convert down to the right base, before using the instance. */
2681 = convert_pointer_to_real (TYPE_METHOD_BASETYPE (TREE_TYPE (fntype)),
2683 if (instance == error_mark_node && instance_ptr != error_mark_node)
2686 vtbl = convert_pointer_to (ptr_type_node, instance);
2689 build_pointer_type (build_pointer_type (vtable_entry_type)),
2690 vtbl, cp_convert (ptrdiff_type_node, delta2));
2691 vtbl = build_indirect_ref (vtbl, NULL_PTR);
2692 aref = build_array_ref (vtbl, build_binary_op (MINUS_EXPR,
2694 integer_one_node, 1));
2695 if (! flag_vtable_thunks)
2697 aref = save_expr (aref);
2699 delta = build_binary_op
2701 build_conditional_expr (e1, build_component_ref (aref,
2708 *instance_ptrptr = build (PLUS_EXPR, TREE_TYPE (instance_ptr),
2709 instance_ptr, delta);
2710 if (flag_vtable_thunks)
2713 e2 = build_component_ref (aref, pfn_identifier, NULL_TREE, 0);
2715 e3 = PFN_FROM_PTRMEMFUNC (function);
2716 TREE_TYPE (e2) = TREE_TYPE (e3);
2717 e1 = build_conditional_expr (e1, e2, e3);
2719 if (instance_ptr == error_mark_node
2720 && TREE_CODE (e1) != ADDR_EXPR
2721 && TREE_CODE (TREE_OPERAND (e1, 0)) != FUNCTION_DECL)
2722 cp_error ("object missing in `%E'", function);
2726 /* Make sure this doesn't get evaluated first inside one of the
2727 branches of the COND_EXPR. */
2728 if (TREE_CODE (instance_ptr) == SAVE_EXPR)
2729 function = build (COMPOUND_EXPR, TREE_TYPE (function),
2730 instance_ptr, function);
2736 build_function_call_real (function, params, require_complete, flags)
2737 tree function, params;
2738 int require_complete, flags;
2740 register tree fntype, fndecl;
2741 register tree value_type;
2742 register tree coerced_params;
2743 tree name = NULL_TREE, assembler_name = NULL_TREE;
2746 /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
2747 Strip such NOP_EXPRs, since FUNCTION is used in non-lvalue context. */
2748 if (TREE_CODE (function) == NOP_EXPR
2749 && TREE_TYPE (function) == TREE_TYPE (TREE_OPERAND (function, 0)))
2750 function = TREE_OPERAND (function, 0);
2752 if (TREE_CODE (function) == FUNCTION_DECL)
2754 name = DECL_NAME (function);
2755 assembler_name = DECL_ASSEMBLER_NAME (function);
2757 GNU_xref_call (current_function_decl,
2758 IDENTIFIER_POINTER (name ? name
2759 : TYPE_IDENTIFIER (DECL_CLASS_CONTEXT
2761 mark_used (function);
2764 /* Convert anything with function type to a pointer-to-function. */
2765 if (pedantic && DECL_MAIN_P (function))
2766 pedwarn ("ANSI C++ forbids calling `main' from within program");
2768 /* Differs from default_conversion by not setting TREE_ADDRESSABLE
2769 (because calling an inline function does not mean the function
2770 needs to be separately compiled). */
2772 if (DECL_INLINE (function))
2773 function = inline_conversion (function);
2775 function = build_addr_func (function);
2781 function = build_addr_func (function);
2784 if (function == error_mark_node)
2785 return error_mark_node;
2787 fntype = TREE_TYPE (function);
2789 if (TYPE_PTRMEMFUNC_P (fntype))
2791 cp_error ("must use .* or ->* to call pointer-to-member function in `%E (...)'",
2793 return error_mark_node;
2796 is_method = (TREE_CODE (fntype) == POINTER_TYPE
2797 && TREE_CODE (TREE_TYPE (fntype)) == METHOD_TYPE);
2799 if (!((TREE_CODE (fntype) == POINTER_TYPE
2800 && TREE_CODE (TREE_TYPE (fntype)) == FUNCTION_TYPE)
2802 || TREE_CODE (function) == TEMPLATE_ID_EXPR))
2804 cp_error ("`%E' cannot be used as a function", function);
2805 return error_mark_node;
2808 /* fntype now gets the type of function pointed to. */
2809 fntype = TREE_TYPE (fntype);
2811 /* Convert the parameters to the types declared in the
2812 function prototype, or apply default promotions. */
2814 if (flags & LOOKUP_COMPLAIN)
2815 coerced_params = convert_arguments (NULL_TREE, TYPE_ARG_TYPES (fntype),
2816 params, fndecl, LOOKUP_NORMAL);
2818 coerced_params = convert_arguments (NULL_TREE, TYPE_ARG_TYPES (fntype),
2821 if (coerced_params == error_mark_node)
2823 if (flags & LOOKUP_SPECULATIVELY)
2826 return error_mark_node;
2829 /* Check for errors in format strings. */
2831 if (warn_format && (name || assembler_name))
2832 check_function_format (name, assembler_name, coerced_params);
2834 /* Recognize certain built-in functions so we can make tree-codes
2835 other than CALL_EXPR. We do this when it enables fold-const.c
2836 to do something useful. */
2838 if (TREE_CODE (function) == ADDR_EXPR
2839 && TREE_CODE (TREE_OPERAND (function, 0)) == FUNCTION_DECL
2840 && DECL_BUILT_IN (TREE_OPERAND (function, 0)))
2841 switch (DECL_FUNCTION_CODE (TREE_OPERAND (function, 0)))
2846 if (coerced_params == 0)
2847 return integer_zero_node;
2848 return build_unary_op (ABS_EXPR, TREE_VALUE (coerced_params), 0);
2855 value_type = TREE_TYPE (fntype) ? TREE_TYPE (fntype) : void_type_node;
2857 register tree result
2858 = build_call (function, value_type, coerced_params);
2860 if (require_complete)
2862 if (value_type == void_type_node)
2864 result = require_complete_type (result);
2866 if (IS_AGGR_TYPE (value_type))
2867 result = build_cplus_new (value_type, result);
2868 return convert_from_reference (result);
2873 build_function_call (function, params)
2874 tree function, params;
2876 return build_function_call_real (function, params, 1, LOOKUP_NORMAL);
2879 /* Convert the actual parameter expressions in the list VALUES
2880 to the types in the list TYPELIST.
2881 If parmdecls is exhausted, or when an element has NULL as its type,
2882 perform the default conversions.
2884 RETURN_LOC is the location of the return value, if known, NULL_TREE
2885 otherwise. This is useful in the case where we can avoid creating
2886 a temporary variable in the case where we can initialize the return
2887 value directly. If we are not eliding constructors, then we set this
2888 to NULL_TREE to avoid this avoidance.
2890 NAME is an IDENTIFIER_NODE or 0. It is used only for error messages.
2892 This is also where warnings about wrong number of args are generated.
2894 Return a list of expressions for the parameters as converted.
2896 Both VALUES and the returned value are chains of TREE_LIST nodes
2897 with the elements of the list in the TREE_VALUE slots of those nodes.
2899 In C++, unspecified trailing parameters can be filled in with their
2900 default arguments, if such were specified. Do so here. */
2903 convert_arguments (return_loc, typelist, values, fndecl, flags)
2904 tree return_loc, typelist, values, fndecl;
2907 register tree typetail, valtail;
2908 register tree result = NULL_TREE;
2909 char *called_thing = 0;
2912 if (! flag_elide_constructors)
2915 /* Argument passing is always copy-initialization. */
2916 flags |= LOOKUP_ONLYCONVERTING;
2920 if (TREE_CODE (TREE_TYPE (fndecl)) == METHOD_TYPE)
2922 if (DECL_NAME (fndecl) == NULL_TREE
2923 || IDENTIFIER_HAS_TYPE_VALUE (DECL_NAME (fndecl)))
2924 called_thing = "constructor";
2926 called_thing = "member function";
2929 called_thing = "function";
2932 for (valtail = values, typetail = typelist;
2934 valtail = TREE_CHAIN (valtail), i++)
2936 register tree type = typetail ? TREE_VALUE (typetail) : 0;
2937 register tree val = TREE_VALUE (valtail);
2939 if (val == error_mark_node)
2940 return error_mark_node;
2942 if (type == void_type_node)
2946 cp_error_at ("too many arguments to %s `%+D'", called_thing,
2948 error ("at this point in file");
2951 error ("too many arguments to function");
2952 /* In case anybody wants to know if this argument
2955 TREE_TYPE (tree_last (result)) = error_mark_node;
2959 /* The tree type of the parameter being passed may not yet be
2960 known. In this case, its type is TYPE_UNKNOWN, and will
2961 be instantiated by the type given by TYPE. If TYPE
2962 is also NULL, the tree type of VAL is ERROR_MARK_NODE. */
2963 if (type && type_unknown_p (val))
2964 val = require_instantiated_type (type, val, integer_zero_node);
2965 else if (type_unknown_p (val))
2967 /* Strip the `&' from an overloaded FUNCTION_DECL. */
2968 if (TREE_CODE (val) == ADDR_EXPR)
2969 val = TREE_OPERAND (val, 0);
2970 if (really_overloaded_fn (val))
2971 cp_error ("insufficient type information to resolve address of overloaded function `%D'",
2972 DECL_NAME (get_first_fn (val)));
2974 error ("insufficient type information in parameter list");
2975 val = integer_zero_node;
2977 else if (TREE_CODE (val) == OFFSET_REF
2978 && TREE_CODE (TREE_TYPE (val)) == METHOD_TYPE)
2980 /* This is unclean. Should be handled elsewhere. */
2981 val = build_unary_op (ADDR_EXPR, val, 0);
2983 else if (TREE_CODE (val) == OFFSET_REF)
2984 val = resolve_offset_ref (val);
2986 /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
2987 Strip such NOP_EXPRs, since VAL is used in non-lvalue context. */
2988 if (TREE_CODE (val) == NOP_EXPR
2989 && TREE_TYPE (val) == TREE_TYPE (TREE_OPERAND (val, 0))
2990 && (type == 0 || TREE_CODE (type) != REFERENCE_TYPE))
2991 val = TREE_OPERAND (val, 0);
2993 if (type == 0 || TREE_CODE (type) != REFERENCE_TYPE)
2995 if (TREE_CODE (TREE_TYPE (val)) == ARRAY_TYPE
2996 || TREE_CODE (TREE_TYPE (val)) == FUNCTION_TYPE
2997 || TREE_CODE (TREE_TYPE (val)) == METHOD_TYPE)
2998 val = default_conversion (val);
3000 val = require_complete_type (val);
3003 if (val == error_mark_node)
3004 return error_mark_node;
3008 /* Formal parm type is specified by a function prototype. */
3011 if (TYPE_SIZE (complete_type (type)) == 0)
3013 error ("parameter type of called function is incomplete");
3018 parmval = convert_for_initialization
3019 (return_loc, type, val, flags,
3020 "argument passing", fndecl, i);
3021 #ifdef PROMOTE_PROTOTYPES
3022 if ((TREE_CODE (type) == INTEGER_TYPE
3023 || TREE_CODE (type) == ENUMERAL_TYPE)
3024 && (TYPE_PRECISION (type)
3025 < TYPE_PRECISION (integer_type_node)))
3026 parmval = default_conversion (parmval);
3030 if (parmval == error_mark_node)
3031 return error_mark_node;
3033 result = expr_tree_cons (NULL_TREE, parmval, result);
3037 if (TREE_CODE (TREE_TYPE (val)) == REFERENCE_TYPE)
3038 val = convert_from_reference (val);
3040 result = expr_tree_cons (NULL_TREE,
3041 convert_arg_to_ellipsis (val),
3046 typetail = TREE_CHAIN (typetail);
3049 if (typetail != 0 && typetail != void_list_node)
3051 /* See if there are default arguments that can be used */
3052 if (TREE_PURPOSE (typetail))
3054 for (; typetail != void_list_node; ++i)
3056 tree type = TREE_VALUE (typetail);
3057 tree val = TREE_PURPOSE (typetail);
3058 tree parmval = convert_default_arg (type, val);
3060 if (parmval == error_mark_node)
3061 return error_mark_node;
3063 result = expr_tree_cons (0, parmval, result);
3064 typetail = TREE_CHAIN (typetail);
3065 /* ends with `...'. */
3066 if (typetail == NULL_TREE)
3074 char *buf = (char *)alloca (32 + strlen (called_thing));
3075 sprintf (buf, "too few arguments to %s `%%#D'", called_thing);
3076 cp_error_at (buf, fndecl);
3077 error ("at this point in file");
3080 error ("too few arguments to function");
3081 return error_mark_list;
3085 return nreverse (result);
3088 /* Build a binary-operation expression, after performing default
3089 conversions on the operands. CODE is the kind of expression to build. */
3092 build_x_binary_op (code, arg1, arg2)
3093 enum tree_code code;
3096 if (processing_template_decl)
3097 return build_min_nt (code, arg1, arg2);
3099 return build_new_op (code, LOOKUP_NORMAL, arg1, arg2, NULL_TREE);
3103 build_binary_op (code, arg1, arg2, convert_p)
3104 enum tree_code code;
3116 args[0] = decay_conversion (args[0]);
3117 args[1] = decay_conversion (args[1]);
3119 if (args[0] == error_mark_node || args[1] == error_mark_node)
3120 return error_mark_node;
3122 type0 = TREE_TYPE (args[0]);
3123 type1 = TREE_TYPE (args[1]);
3125 if (type_unknown_p (args[0]))
3127 args[0] = instantiate_type (type1, args[0], 1);
3128 args[0] = decay_conversion (args[0]);
3130 else if (type_unknown_p (args[1]))
3132 args[1] = require_instantiated_type (type0, args[1],
3134 args[1] = decay_conversion (args[1]);
3137 if (IS_AGGR_TYPE (type0) || IS_AGGR_TYPE (type1))
3138 my_friendly_abort (754867);
3140 return build_binary_op_nodefault (code, args[0], args[1], code);
3143 /* Build a binary-operation expression without default conversions.
3144 CODE is the kind of expression to build.
3145 This function differs from `build' in several ways:
3146 the data type of the result is computed and recorded in it,
3147 warnings are generated if arg data types are invalid,
3148 special handling for addition and subtraction of pointers is known,
3149 and some optimization is done (operations on narrow ints
3150 are done in the narrower type when that gives the same result).
3151 Constant folding is also done before the result is returned.
3153 ERROR_CODE is the code that determines what to say in error messages.
3154 It is usually, but not always, the same as CODE.
3156 Note that the operands will never have enumeral types
3157 because either they have just had the default conversions performed
3158 or they have both just been converted to some other type in which
3159 the arithmetic is to be done.
3161 C++: must do special pointer arithmetic when implementing
3162 multiple inheritance, and deal with pointer to member functions. */
3165 build_binary_op_nodefault (code, orig_op0, orig_op1, error_code)
3166 enum tree_code code;
3167 tree orig_op0, orig_op1;
3168 enum tree_code error_code;
3171 register enum tree_code code0, code1;
3174 /* Expression code to give to the expression when it is built.
3175 Normally this is CODE, which is what the caller asked for,
3176 but in some special cases we change it. */
3177 register enum tree_code resultcode = code;
3179 /* Data type in which the computation is to be performed.
3180 In the simplest cases this is the common type of the arguments. */
3181 register tree result_type = NULL;
3183 /* Nonzero means operands have already been type-converted
3184 in whatever way is necessary.
3185 Zero means they need to be converted to RESULT_TYPE. */
3188 /* Nonzero means create the expression with this type, rather than
3190 tree build_type = 0;
3192 /* Nonzero means after finally constructing the expression
3193 convert it to this type. */
3194 tree final_type = 0;
3196 /* Nonzero if this is an operation like MIN or MAX which can
3197 safely be computed in short if both args are promoted shorts.
3198 Also implies COMMON.
3199 -1 indicates a bitwise operation; this makes a difference
3200 in the exact conditions for when it is safe to do the operation
3201 in a narrower mode. */
3204 /* Nonzero if this is a comparison operation;
3205 if both args are promoted shorts, compare the original shorts.
3206 Also implies COMMON. */
3207 int short_compare = 0;
3209 /* Nonzero if this is a right-shift operation, which can be computed on the
3210 original short and then promoted if the operand is a promoted short. */
3211 int short_shift = 0;
3213 /* Nonzero means set RESULT_TYPE to the common type of the args. */
3216 /* Apply default conversions. */
3217 if (code == TRUTH_AND_EXPR || code == TRUTH_ANDIF_EXPR
3218 || code == TRUTH_OR_EXPR || code == TRUTH_ORIF_EXPR
3219 || code == TRUTH_XOR_EXPR)
3221 op0 = decay_conversion (orig_op0);
3222 op1 = decay_conversion (orig_op1);
3226 op0 = default_conversion (orig_op0);
3227 op1 = default_conversion (orig_op1);
3230 type0 = TREE_TYPE (op0);
3231 type1 = TREE_TYPE (op1);
3233 /* The expression codes of the data types of the arguments tell us
3234 whether the arguments are integers, floating, pointers, etc. */
3235 code0 = TREE_CODE (type0);
3236 code1 = TREE_CODE (type1);
3238 /* Strip NON_LVALUE_EXPRs, etc., since we aren't using as an lvalue. */
3239 STRIP_TYPE_NOPS (op0);
3240 STRIP_TYPE_NOPS (op1);
3242 /* If an error was already reported for one of the arguments,
3243 avoid reporting another error. */
3245 if (code0 == ERROR_MARK || code1 == ERROR_MARK)
3246 return error_mark_node;
3251 /* Handle the pointer + int case. */
3252 if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
3253 return pointer_int_sum (PLUS_EXPR, op0, op1);
3254 else if (code1 == POINTER_TYPE && code0 == INTEGER_TYPE)
3255 return pointer_int_sum (PLUS_EXPR, op1, op0);
3261 /* Subtraction of two similar pointers.
3262 We must subtract them as integers, then divide by object size. */
3263 if (code0 == POINTER_TYPE && code1 == POINTER_TYPE
3264 && comp_target_types (type0, type1, 1))
3265 return pointer_diff (op0, op1, common_type (type0, type1));
3266 /* Handle pointer minus int. Just like pointer plus int. */
3267 else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
3268 return pointer_int_sum (MINUS_EXPR, op0, op1);
3277 case TRUNC_DIV_EXPR:
3279 case FLOOR_DIV_EXPR:
3280 case ROUND_DIV_EXPR:
3281 case EXACT_DIV_EXPR:
3282 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE
3283 || code0 == COMPLEX_TYPE)
3284 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
3285 || code1 == COMPLEX_TYPE))
3287 if (TREE_CODE (op1) == INTEGER_CST && integer_zerop (op1))
3288 cp_warning ("division by zero in `%E / 0'", op0);
3289 else if (TREE_CODE (op1) == REAL_CST && real_zerop (op1))
3290 cp_warning ("division by zero in `%E / 0.'", op0);
3292 if (!(code0 == INTEGER_TYPE && code1 == INTEGER_TYPE))
3293 resultcode = RDIV_EXPR;
3295 /* When dividing two signed integers, we have to promote to int.
3296 unless we divide by a constant != -1. Note that default
3297 conversion will have been performed on the operands at this
3298 point, so we have to dig out the original type to find out if
3300 shorten = ((TREE_CODE (op0) == NOP_EXPR
3301 && TREE_UNSIGNED (TREE_TYPE (TREE_OPERAND (op0, 0))))
3302 || (TREE_CODE (op1) == INTEGER_CST
3303 && (TREE_INT_CST_LOW (op1) != -1
3304 || TREE_INT_CST_HIGH (op1) != -1)));
3310 case BIT_ANDTC_EXPR:
3313 if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
3315 /* If one operand is a constant, and the other is a short type
3316 that has been converted to an int,
3317 really do the work in the short type and then convert the
3318 result to int. If we are lucky, the constant will be 0 or 1
3319 in the short type, making the entire operation go away. */
3320 if (TREE_CODE (op0) == INTEGER_CST
3321 && TREE_CODE (op1) == NOP_EXPR
3322 && (TYPE_PRECISION (type1)
3323 > TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (op1, 0))))
3324 && TREE_UNSIGNED (TREE_TYPE (TREE_OPERAND (op1, 0))))
3326 final_type = result_type;
3327 op1 = TREE_OPERAND (op1, 0);
3328 result_type = TREE_TYPE (op1);
3330 if (TREE_CODE (op1) == INTEGER_CST
3331 && TREE_CODE (op0) == NOP_EXPR
3332 && (TYPE_PRECISION (type0)
3333 > TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (op0, 0))))
3334 && TREE_UNSIGNED (TREE_TYPE (TREE_OPERAND (op0, 0))))
3336 final_type = result_type;
3337 op0 = TREE_OPERAND (op0, 0);
3338 result_type = TREE_TYPE (op0);
3342 case TRUNC_MOD_EXPR:
3343 case FLOOR_MOD_EXPR:
3344 if (code1 == INTEGER_TYPE && integer_zerop (op1))
3345 cp_warning ("division by zero in `%E % 0'", op0);
3346 else if (code1 == REAL_TYPE && real_zerop (op1))
3347 cp_warning ("division by zero in `%E % 0.'", op0);
3349 if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
3351 /* Although it would be tempting to shorten always here, that loses
3352 on some targets, since the modulo instruction is undefined if the
3353 quotient can't be represented in the computation mode. We shorten
3354 only if unsigned or if dividing by something we know != -1. */
3355 shorten = ((TREE_CODE (op0) == NOP_EXPR
3356 && TREE_UNSIGNED (TREE_TYPE (TREE_OPERAND (op0, 0))))
3357 || (TREE_CODE (op1) == INTEGER_CST
3358 && (TREE_INT_CST_LOW (op1) != -1
3359 || TREE_INT_CST_HIGH (op1) != -1)));
3364 case TRUTH_ANDIF_EXPR:
3365 case TRUTH_ORIF_EXPR:
3366 case TRUTH_AND_EXPR:
3368 result_type = boolean_type_node;
3371 /* Shift operations: result has same type as first operand;
3372 always convert second operand to int.
3373 Also set SHORT_SHIFT if shifting rightward. */
3376 if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
3378 result_type = type0;
3379 if (TREE_CODE (op1) == INTEGER_CST)
3381 if (tree_int_cst_lt (op1, integer_zero_node))
3382 warning ("right shift count is negative");
3385 if (TREE_INT_CST_LOW (op1) | TREE_INT_CST_HIGH (op1))
3387 if (TREE_INT_CST_HIGH (op1) != 0
3388 || ((unsigned HOST_WIDE_INT) TREE_INT_CST_LOW (op1)
3389 >= TYPE_PRECISION (type0)))
3390 warning ("right shift count >= width of type");
3393 /* Convert the shift-count to an integer, regardless of
3394 size of value being shifted. */
3395 if (TYPE_MAIN_VARIANT (TREE_TYPE (op1)) != integer_type_node)
3396 op1 = cp_convert (integer_type_node, op1);
3397 /* Avoid converting op1 to result_type later. */
3403 if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
3405 result_type = type0;
3406 if (TREE_CODE (op1) == INTEGER_CST)
3408 if (tree_int_cst_lt (op1, integer_zero_node))
3409 warning ("left shift count is negative");
3410 else if (TREE_INT_CST_HIGH (op1) != 0
3411 || ((unsigned HOST_WIDE_INT) TREE_INT_CST_LOW (op1)
3412 >= TYPE_PRECISION (type0)))
3413 warning ("left shift count >= width of type");
3415 /* Convert the shift-count to an integer, regardless of
3416 size of value being shifted. */
3417 if (TYPE_MAIN_VARIANT (TREE_TYPE (op1)) != integer_type_node)
3418 op1 = cp_convert (integer_type_node, op1);
3419 /* Avoid converting op1 to result_type later. */
3426 if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
3428 result_type = type0;
3429 if (TREE_CODE (op1) == INTEGER_CST)
3431 if (tree_int_cst_lt (op1, integer_zero_node))
3432 warning ("%s rotate count is negative",
3433 (code == LROTATE_EXPR) ? "left" : "right");
3434 else if (TREE_INT_CST_HIGH (op1) != 0
3435 || ((unsigned HOST_WIDE_INT) TREE_INT_CST_LOW (op1)
3436 >= TYPE_PRECISION (type0)))
3437 warning ("%s rotate count >= width of type",
3438 (code == LROTATE_EXPR) ? "left" : "right");
3440 /* Convert the shift-count to an integer, regardless of
3441 size of value being shifted. */
3442 if (TYPE_MAIN_VARIANT (TREE_TYPE (op1)) != integer_type_node)
3443 op1 = cp_convert (integer_type_node, op1);
3449 build_type = boolean_type_node;
3450 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE
3451 || code0 == COMPLEX_TYPE)
3452 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
3453 || code1 == COMPLEX_TYPE))
3455 else if (code0 == POINTER_TYPE && code1 == POINTER_TYPE)
3457 register tree tt0 = TYPE_MAIN_VARIANT (TREE_TYPE (type0));
3458 register tree tt1 = TYPE_MAIN_VARIANT (TREE_TYPE (type1));
3460 if (comp_target_types (type0, type1, 1))
3461 result_type = common_type (type0, type1);
3462 else if (tt0 == void_type_node)
3464 if (pedantic && TREE_CODE (tt1) == FUNCTION_TYPE
3465 && tree_int_cst_lt (TYPE_SIZE (type0), TYPE_SIZE (type1)))
3466 pedwarn ("ANSI C++ forbids comparison of `void *' with function pointer");
3467 else if (TREE_CODE (tt1) == OFFSET_TYPE)
3468 pedwarn ("ANSI C++ forbids conversion of a pointer to member to `void *'");
3470 else if (tt1 == void_type_node)
3472 if (pedantic && TREE_CODE (tt0) == FUNCTION_TYPE
3473 && tree_int_cst_lt (TYPE_SIZE (type1), TYPE_SIZE (type0)))
3474 pedwarn ("ANSI C++ forbids comparison of `void *' with function pointer");
3477 cp_pedwarn ("comparison of distinct pointer types `%T' and `%T' lacks a cast",
3480 if (result_type == NULL_TREE)
3481 result_type = ptr_type_node;
3483 else if (code0 == POINTER_TYPE && TREE_CODE (op1) == INTEGER_CST
3484 && integer_zerop (op1))
3485 result_type = type0;
3486 else if (code1 == POINTER_TYPE && TREE_CODE (op0) == INTEGER_CST
3487 && integer_zerop (op0))
3488 result_type = type1;
3489 else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
3491 result_type = type0;
3492 error ("ANSI C++ forbids comparison between pointer and integer");
3494 else if (code0 == INTEGER_TYPE && code1 == POINTER_TYPE)
3496 result_type = type1;
3497 error ("ANSI C++ forbids comparison between pointer and integer");
3499 else if (TYPE_PTRMEMFUNC_P (type0) && TREE_CODE (op1) == INTEGER_CST
3500 && integer_zerop (op1))
3502 op0 = build_component_ref (op0, index_identifier, NULL_TREE, 0);
3503 op1 = integer_zero_node;
3504 result_type = TREE_TYPE (op0);
3506 else if (TYPE_PTRMEMFUNC_P (type1) && TREE_CODE (op0) == INTEGER_CST
3507 && integer_zerop (op0))
3509 op0 = build_component_ref (op1, index_identifier, NULL_TREE, 0);
3510 op1 = integer_zero_node;
3511 result_type = TREE_TYPE (op0);
3513 else if (TYPE_PTRMEMFUNC_P (type0) && TYPE_PTRMEMFUNC_P (type1)
3514 && (TYPE_PTRMEMFUNC_FN_TYPE (type0)
3515 == TYPE_PTRMEMFUNC_FN_TYPE (type1)))
3517 /* The code we generate for the test is:
3519 (op0.index == op1.index
3520 && ((op1.index != -1 && op0.delta2 == op1.delta2)
3521 || op0.pfn == op1.pfn)) */
3523 tree index0 = build_component_ref (op0, index_identifier,
3525 tree index1 = save_expr (build_component_ref (op1, index_identifier,
3527 tree pfn0 = PFN_FROM_PTRMEMFUNC (op0);
3528 tree pfn1 = PFN_FROM_PTRMEMFUNC (op1);
3529 tree delta20 = DELTA2_FROM_PTRMEMFUNC (op0);
3530 tree delta21 = DELTA2_FROM_PTRMEMFUNC (op1);
3532 tree integer_neg_one_node
3533 = build_binary_op (MINUS_EXPR, integer_zero_node,
3534 integer_one_node, 1);
3535 e1 = build_binary_op (EQ_EXPR, index0, index1, 1);
3536 e2 = build_binary_op (NE_EXPR, index1, integer_neg_one_node, 1);
3537 e2 = build_binary_op (TRUTH_ANDIF_EXPR, e2,
3538 build_binary_op (EQ_EXPR, delta20, delta21, 1),
3540 e3 = build_binary_op (EQ_EXPR, pfn0, pfn1, 1);
3541 e2 = build_binary_op (TRUTH_ORIF_EXPR, e2, e3, 1);
3542 e2 = build_binary_op (TRUTH_ANDIF_EXPR, e1, e2, 1);
3543 if (code == EQ_EXPR)
3545 return build_binary_op (EQ_EXPR, e2, integer_zero_node, 1);
3547 else if (TYPE_PTRMEMFUNC_P (type0)
3548 && TYPE_PTRMEMFUNC_FN_TYPE (type0) == type1)
3550 tree index0 = build_component_ref (op0, index_identifier,
3553 tree pfn0 = PFN_FROM_PTRMEMFUNC (op0);
3554 tree delta20 = DELTA2_FROM_PTRMEMFUNC (op0);
3555 tree delta21 = integer_zero_node;
3557 tree integer_neg_one_node
3558 = build_binary_op (MINUS_EXPR, integer_zero_node, integer_one_node, 1);
3559 if (TREE_CODE (TREE_OPERAND (op1, 0)) == FUNCTION_DECL
3560 && DECL_VINDEX (TREE_OPERAND (op1, 0)))
3562 /* Map everything down one to make room for
3563 the null pointer to member. */
3564 index1 = size_binop (PLUS_EXPR,
3565 DECL_VINDEX (TREE_OPERAND (op1, 0)),
3567 op1 = integer_zero_node;
3568 delta21 = CLASSTYPE_VFIELD (TYPE_METHOD_BASETYPE
3569 (TREE_TYPE (type1)));
3570 delta21 = DECL_FIELD_BITPOS (delta21);
3571 delta21 = size_binop (FLOOR_DIV_EXPR, delta21,
3572 size_int (BITS_PER_UNIT));
3573 delta21 = convert (sizetype, delta21);
3576 index1 = integer_neg_one_node;
3578 tree nop1 = build1 (NOP_EXPR, TYPE_PTRMEMFUNC_FN_TYPE (type0),
3580 TREE_CONSTANT (nop1) = TREE_CONSTANT (op1);
3583 e1 = build_binary_op (EQ_EXPR, index0, index1, 1);
3584 e2 = build_binary_op (NE_EXPR, index1, integer_neg_one_node, 1);
3585 e2 = build_binary_op (TRUTH_ANDIF_EXPR, e2,
3586 build_binary_op (EQ_EXPR, delta20, delta21, 1),
3588 e3 = build_binary_op (EQ_EXPR, pfn0, op1, 1);
3589 e2 = build_binary_op (TRUTH_ORIF_EXPR, e2, e3, 1);
3590 e2 = build_binary_op (TRUTH_ANDIF_EXPR, e1, e2, 1);
3591 if (code == EQ_EXPR)
3593 return build_binary_op (EQ_EXPR, e2, integer_zero_node, 1);
3595 else if (TYPE_PTRMEMFUNC_P (type1)
3596 && TYPE_PTRMEMFUNC_FN_TYPE (type1) == type0)
3598 return build_binary_op (code, op1, op0, 1);
3604 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE)
3605 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE))
3607 else if (code0 == POINTER_TYPE && code1 == POINTER_TYPE)
3609 if (comp_target_types (type0, type1, 1))
3610 result_type = common_type (type0, type1);
3613 cp_pedwarn ("comparison of distinct pointer types `%T' and `%T' lacks a cast",
3615 result_type = ptr_type_node;
3624 build_type = boolean_type_node;
3625 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE)
3626 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE))
3628 else if (code0 == POINTER_TYPE && code1 == POINTER_TYPE)
3630 if (comp_target_types (type0, type1, 1))
3631 result_type = common_type (type0, type1);
3634 cp_pedwarn ("comparison of distinct pointer types `%T' and `%T' lacks a cast",
3636 result_type = ptr_type_node;
3639 else if (code0 == POINTER_TYPE && TREE_CODE (op1) == INTEGER_CST
3640 && integer_zerop (op1))
3641 result_type = type0;
3642 else if (code1 == POINTER_TYPE && TREE_CODE (op0) == INTEGER_CST
3643 && integer_zerop (op0))
3644 result_type = type1;
3645 else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
3647 result_type = type0;
3648 pedwarn ("ANSI C++ forbids comparison between pointer and integer");
3650 else if (code0 == INTEGER_TYPE && code1 == POINTER_TYPE)
3652 result_type = type1;
3653 pedwarn ("ANSI C++ forbids comparison between pointer and integer");
3661 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE || code0 == COMPLEX_TYPE)
3663 (code1 == INTEGER_TYPE || code1 == REAL_TYPE || code1 == COMPLEX_TYPE))
3665 int none_complex = (code0 != COMPLEX_TYPE && code1 != COMPLEX_TYPE);
3667 if (shorten || common || short_compare)
3668 result_type = common_type (type0, type1);
3670 /* For certain operations (which identify themselves by shorten != 0)
3671 if both args were extended from the same smaller type,
3672 do the arithmetic in that type and then extend.
3674 shorten !=0 and !=1 indicates a bitwise operation.
3675 For them, this optimization is safe only if
3676 both args are zero-extended or both are sign-extended.
3677 Otherwise, we might change the result.
3678 Eg, (short)-1 | (unsigned short)-1 is (int)-1
3679 but calculated in (unsigned short) it would be (unsigned short)-1. */
3681 if (shorten && none_complex)
3683 int unsigned0, unsigned1;
3684 tree arg0 = get_narrower (op0, &unsigned0);
3685 tree arg1 = get_narrower (op1, &unsigned1);
3686 /* UNS is 1 if the operation to be done is an unsigned one. */
3687 int uns = TREE_UNSIGNED (result_type);
3690 final_type = result_type;
3692 /* Handle the case that OP0 does not *contain* a conversion
3693 but it *requires* conversion to FINAL_TYPE. */
3695 if (op0 == arg0 && TREE_TYPE (op0) != final_type)
3696 unsigned0 = TREE_UNSIGNED (TREE_TYPE (op0));
3697 if (op1 == arg1 && TREE_TYPE (op1) != final_type)
3698 unsigned1 = TREE_UNSIGNED (TREE_TYPE (op1));
3700 /* Now UNSIGNED0 is 1 if ARG0 zero-extends to FINAL_TYPE. */
3702 /* For bitwise operations, signedness of nominal type
3703 does not matter. Consider only how operands were extended. */
3707 /* Note that in all three cases below we refrain from optimizing
3708 an unsigned operation on sign-extended args.
3709 That would not be valid. */
3711 /* Both args variable: if both extended in same way
3712 from same width, do it in that width.
3713 Do it unsigned if args were zero-extended. */
3714 if ((TYPE_PRECISION (TREE_TYPE (arg0))
3715 < TYPE_PRECISION (result_type))
3716 && (TYPE_PRECISION (TREE_TYPE (arg1))
3717 == TYPE_PRECISION (TREE_TYPE (arg0)))
3718 && unsigned0 == unsigned1
3719 && (unsigned0 || !uns))
3721 = signed_or_unsigned_type (unsigned0,
3722 common_type (TREE_TYPE (arg0),
3724 else if (TREE_CODE (arg0) == INTEGER_CST
3725 && (unsigned1 || !uns)
3726 && (TYPE_PRECISION (TREE_TYPE (arg1))
3727 < TYPE_PRECISION (result_type))
3728 && (type = signed_or_unsigned_type (unsigned1,
3730 int_fits_type_p (arg0, type)))
3732 else if (TREE_CODE (arg1) == INTEGER_CST
3733 && (unsigned0 || !uns)
3734 && (TYPE_PRECISION (TREE_TYPE (arg0))
3735 < TYPE_PRECISION (result_type))
3736 && (type = signed_or_unsigned_type (unsigned0,
3738 int_fits_type_p (arg1, type)))
3742 /* Shifts can be shortened if shifting right. */
3747 tree arg0 = get_narrower (op0, &unsigned_arg);
3749 final_type = result_type;
3751 if (arg0 == op0 && final_type == TREE_TYPE (op0))
3752 unsigned_arg = TREE_UNSIGNED (TREE_TYPE (op0));
3754 if (TYPE_PRECISION (TREE_TYPE (arg0)) < TYPE_PRECISION (result_type)
3755 /* We can shorten only if the shift count is less than the
3756 number of bits in the smaller type size. */
3757 && TREE_INT_CST_HIGH (op1) == 0
3758 && TYPE_PRECISION (TREE_TYPE (arg0)) > TREE_INT_CST_LOW (op1)
3759 /* If arg is sign-extended and then unsigned-shifted,
3760 we can simulate this with a signed shift in arg's type
3761 only if the extended result is at least twice as wide
3762 as the arg. Otherwise, the shift could use up all the
3763 ones made by sign-extension and bring in zeros.
3764 We can't optimize that case at all, but in most machines
3765 it never happens because available widths are 2**N. */
3766 && (!TREE_UNSIGNED (final_type)
3768 || (((unsigned) 2 * TYPE_PRECISION (TREE_TYPE (arg0)))
3769 <= TYPE_PRECISION (result_type))))
3771 /* Do an unsigned shift if the operand was zero-extended. */
3773 = signed_or_unsigned_type (unsigned_arg,
3775 /* Convert value-to-be-shifted to that type. */
3776 if (TREE_TYPE (op0) != result_type)
3777 op0 = cp_convert (result_type, op0);
3782 /* Comparison operations are shortened too but differently.
3783 They identify themselves by setting short_compare = 1. */
3787 /* Don't write &op0, etc., because that would prevent op0
3788 from being kept in a register.
3789 Instead, make copies of the our local variables and
3790 pass the copies by reference, then copy them back afterward. */
3791 tree xop0 = op0, xop1 = op1, xresult_type = result_type;
3792 enum tree_code xresultcode = resultcode;
3794 = shorten_compare (&xop0, &xop1, &xresult_type, &xresultcode);
3796 return cp_convert (boolean_type_node, val);
3797 op0 = xop0, op1 = xop1;
3799 resultcode = xresultcode;
3802 if (short_compare && warn_sign_compare)
3804 int op0_signed = ! TREE_UNSIGNED (TREE_TYPE (orig_op0));
3805 int op1_signed = ! TREE_UNSIGNED (TREE_TYPE (orig_op1));
3807 int unsignedp0, unsignedp1;
3808 tree primop0 = get_narrower (op0, &unsignedp0);
3809 tree primop1 = get_narrower (op1, &unsignedp1);
3811 /* Check for comparison of different enum types. */
3812 if (flag_int_enum_equivalence == 0
3813 && TREE_CODE (TREE_TYPE (orig_op0)) == ENUMERAL_TYPE
3814 && TREE_CODE (TREE_TYPE (orig_op1)) == ENUMERAL_TYPE
3815 && TYPE_MAIN_VARIANT (TREE_TYPE (orig_op0))
3816 != TYPE_MAIN_VARIANT (TREE_TYPE (orig_op1)))
3818 cp_warning ("comparison between `%#T' and `%#T'",
3819 TREE_TYPE (orig_op0), TREE_TYPE (orig_op1));
3822 /* Give warnings for comparisons between signed and unsigned
3823 quantities that may fail. */
3824 /* Do the checking based on the original operand trees, so that
3825 casts will be considered, but default promotions won't be. */
3827 /* Do not warn if the comparison is being done in a signed type,
3828 since the signed type will only be chosen if it can represent
3829 all the values of the unsigned type. */
3830 if (! TREE_UNSIGNED (result_type))
3832 /* Do not warn if both operands are unsigned. */
3833 else if (op0_signed == op1_signed)
3835 /* Do not warn if the signed quantity is an unsuffixed
3836 integer literal (or some static constant expression
3837 involving such literals) and it is non-negative. */
3838 else if ((op0_signed && TREE_CODE (orig_op0) == INTEGER_CST
3839 && tree_int_cst_sgn (orig_op0) >= 0)
3840 || (op1_signed && TREE_CODE (orig_op1) == INTEGER_CST
3841 && tree_int_cst_sgn (orig_op1) >= 0))
3843 /* Do not warn if the comparison is an equality operation,
3844 the unsigned quantity is an integral constant and it does
3845 not use the most significant bit of result_type. */
3846 else if ((resultcode == EQ_EXPR || resultcode == NE_EXPR)
3847 && ((op0_signed && TREE_CODE (orig_op1) == INTEGER_CST
3848 && int_fits_type_p (orig_op1,
3849 signed_type (result_type)))
3850 || (op1_signed && TREE_CODE (orig_op0) == INTEGER_CST
3851 && int_fits_type_p (orig_op0,
3852 signed_type (result_type)))))
3855 warning ("comparison between signed and unsigned");
3857 /* Warn if two unsigned values are being compared in a size
3858 larger than their original size, and one (and only one) is the
3859 result of a `~' operator. This comparison will always fail.
3861 Also warn if one operand is a constant, and the constant does not
3862 have all bits set that are set in the ~ operand when it is
3865 if ((TREE_CODE (primop0) == BIT_NOT_EXPR)
3866 ^ (TREE_CODE (primop1) == BIT_NOT_EXPR))
3868 if (TREE_CODE (primop0) == BIT_NOT_EXPR)
3869 primop0 = get_narrower (TREE_OPERAND (op0, 0), &unsignedp0);
3870 if (TREE_CODE (primop1) == BIT_NOT_EXPR)
3871 primop1 = get_narrower (TREE_OPERAND (op1, 0), &unsignedp1);
3873 if (TREE_CODE (primop0) == INTEGER_CST
3874 || TREE_CODE (primop1) == INTEGER_CST)
3877 HOST_WIDE_INT constant, mask;
3881 if (TREE_CODE (primop0) == INTEGER_CST)
3884 unsignedp = unsignedp1;
3885 constant = TREE_INT_CST_LOW (primop0);
3890 unsignedp = unsignedp0;
3891 constant = TREE_INT_CST_LOW (primop1);
3894 bits = TYPE_PRECISION (TREE_TYPE (primop));
3895 if (bits < TYPE_PRECISION (result_type)
3896 && bits < HOST_BITS_PER_LONG && unsignedp)
3898 mask = (~ (HOST_WIDE_INT) 0) << bits;
3899 if ((mask & constant) != mask)
3900 warning ("comparison of promoted ~unsigned with constant");
3903 else if (unsignedp0 && unsignedp1
3904 && (TYPE_PRECISION (TREE_TYPE (primop0))
3905 < TYPE_PRECISION (result_type))
3906 && (TYPE_PRECISION (TREE_TYPE (primop1))
3907 < TYPE_PRECISION (result_type)))
3908 warning ("comparison of promoted ~unsigned with unsigned");
3913 /* At this point, RESULT_TYPE must be nonzero to avoid an error message.
3914 If CONVERTED is zero, both args will be converted to type RESULT_TYPE.
3915 Then the expression will be built.
3916 It will be given type FINAL_TYPE if that is nonzero;
3917 otherwise, it will be given type RESULT_TYPE. */
3921 cp_error ("invalid operands `%T' and `%T' to binary `%O'",
3922 TREE_TYPE (orig_op0), TREE_TYPE (orig_op1), error_code);
3923 return error_mark_node;
3928 if (TREE_TYPE (op0) != result_type)
3929 op0 = cp_convert (result_type, op0);
3930 if (TREE_TYPE (op1) != result_type)
3931 op1 = cp_convert (result_type, op1);
3934 if (build_type == NULL_TREE)
3935 build_type = result_type;
3938 register tree result = build (resultcode, build_type, op0, op1);
3939 register tree folded;
3941 folded = fold (result);
3942 if (folded == result)
3943 TREE_CONSTANT (folded) = TREE_CONSTANT (op0) & TREE_CONSTANT (op1);
3944 if (final_type != 0)
3945 return cp_convert (final_type, folded);
3950 /* Return a tree for the sum or difference (RESULTCODE says which)
3951 of pointer PTROP and integer INTOP. */
3954 pointer_int_sum (resultcode, ptrop, intop)
3955 enum tree_code resultcode;
3956 register tree ptrop, intop;
3960 register tree result;
3961 register tree folded = fold (intop);
3963 /* The result is a pointer of the same type that is being added. */
3965 register tree result_type = TREE_TYPE (ptrop);
3967 if (TREE_CODE (TREE_TYPE (result_type)) == VOID_TYPE)
3969 if (pedantic || warn_pointer_arith)
3970 pedwarn ("ANSI C++ forbids using pointer of type `void *' in arithmetic");
3971 size_exp = integer_one_node;
3973 else if (TREE_CODE (TREE_TYPE (result_type)) == FUNCTION_TYPE)
3975 if (pedantic || warn_pointer_arith)
3976 pedwarn ("ANSI C++ forbids using pointer to a function in arithmetic");
3977 size_exp = integer_one_node;
3979 else if (TREE_CODE (TREE_TYPE (result_type)) == METHOD_TYPE)
3981 if (pedantic || warn_pointer_arith)
3982 pedwarn ("ANSI C++ forbids using pointer to a method in arithmetic");
3983 size_exp = integer_one_node;
3985 else if (TREE_CODE (TREE_TYPE (result_type)) == OFFSET_TYPE)
3987 if (pedantic || warn_pointer_arith)
3988 pedwarn ("ANSI C++ forbids using pointer to a member in arithmetic");
3989 size_exp = integer_one_node;
3992 size_exp = size_in_bytes (complete_type (TREE_TYPE (result_type)));
3994 /* Needed to make OOPS V2R3 work. */
3996 if (TREE_CODE (intop) == INTEGER_CST
3997 && TREE_INT_CST_LOW (intop) == 0
3998 && TREE_INT_CST_HIGH (intop) == 0)
4001 /* If what we are about to multiply by the size of the elements
4002 contains a constant term, apply distributive law
4003 and multiply that constant term separately.
4004 This helps produce common subexpressions. */
4006 if ((TREE_CODE (intop) == PLUS_EXPR || TREE_CODE (intop) == MINUS_EXPR)
4007 && ! TREE_CONSTANT (intop)
4008 && TREE_CONSTANT (TREE_OPERAND (intop, 1))
4009 && TREE_CONSTANT (size_exp))
4011 enum tree_code subcode = resultcode;
4012 if (TREE_CODE (intop) == MINUS_EXPR)
4013 subcode = (subcode == PLUS_EXPR ? MINUS_EXPR : PLUS_EXPR);
4014 ptrop = build_binary_op (subcode, ptrop, TREE_OPERAND (intop, 1), 1);
4015 intop = TREE_OPERAND (intop, 0);
4018 /* Convert the integer argument to a type the same size as sizetype
4019 so the multiply won't overflow spuriously. */
4021 if (TYPE_PRECISION (TREE_TYPE (intop)) != TYPE_PRECISION (sizetype))
4022 intop = cp_convert (type_for_size (TYPE_PRECISION (sizetype), 0), intop);
4024 /* Replace the integer argument with a suitable product by the object size.
4025 Do this multiplication as signed, then convert to the appropriate
4026 pointer type (actually unsigned integral). */
4028 intop = cp_convert (result_type,
4029 build_binary_op (MULT_EXPR, intop,
4030 cp_convert (TREE_TYPE (intop),
4034 /* Create the sum or difference. */
4036 result = build (resultcode, result_type, ptrop, intop);
4038 folded = fold (result);
4039 if (folded == result)
4040 TREE_CONSTANT (folded) = TREE_CONSTANT (ptrop) & TREE_CONSTANT (intop);
4044 /* Return a tree for the difference of pointers OP0 and OP1.
4045 The resulting tree has type int. */
4048 pointer_diff (op0, op1, ptrtype)
4049 register tree op0, op1;
4050 register tree ptrtype;
4052 register tree result, folded;
4053 tree restype = ptrdiff_type_node;
4054 tree target_type = TREE_TYPE (ptrtype);
4056 if (pedantic || warn_pointer_arith)
4058 if (TREE_CODE (target_type) == VOID_TYPE)
4059 pedwarn ("ANSI C++ forbids using pointer of type `void *' in subtraction");
4060 if (TREE_CODE (target_type) == FUNCTION_TYPE)
4061 pedwarn ("ANSI C++ forbids using pointer to a function in subtraction");
4062 if (TREE_CODE (target_type) == METHOD_TYPE)
4063 pedwarn ("ANSI C++ forbids using pointer to a method in subtraction");
4064 if (TREE_CODE (target_type) == OFFSET_TYPE)
4065 pedwarn ("ANSI C++ forbids using pointer to a member in subtraction");
4068 /* First do the subtraction as integers;
4069 then drop through to build the divide operator. */
4071 op0 = build_binary_op (MINUS_EXPR, cp_convert (restype, op0),
4072 cp_convert (restype, op1), 1);
4074 /* This generates an error if op1 is a pointer to an incomplete type. */
4075 if (TYPE_SIZE (TREE_TYPE (TREE_TYPE (op1))) == 0)
4076 error ("arithmetic on pointer to an incomplete type");
4078 op1 = ((TREE_CODE (target_type) == VOID_TYPE
4079 || TREE_CODE (target_type) == FUNCTION_TYPE
4080 || TREE_CODE (target_type) == METHOD_TYPE
4081 || TREE_CODE (target_type) == OFFSET_TYPE)
4083 : size_in_bytes (target_type));
4085 /* Do the division. */
4087 result = build (EXACT_DIV_EXPR, restype, op0, cp_convert (restype, op1));
4089 folded = fold (result);
4090 if (folded == result)
4091 TREE_CONSTANT (folded) = TREE_CONSTANT (op0) & TREE_CONSTANT (op1);
4095 /* Handle the case of taking the address of a COMPONENT_REF.
4096 Called by `build_unary_op' and `build_up_reference'.
4098 ARG is the COMPONENT_REF whose address we want.
4099 ARGTYPE is the pointer type that this address should have.
4100 MSG is an error message to print if this COMPONENT_REF is not
4101 addressable (such as a bitfield). */
4104 build_component_addr (arg, argtype, msg)
4108 tree field = TREE_OPERAND (arg, 1);
4109 tree basetype = decl_type_context (field);
4110 tree rval = build_unary_op (ADDR_EXPR, TREE_OPERAND (arg, 0), 0);
4112 if (DECL_BIT_FIELD (field))
4114 error (msg, IDENTIFIER_POINTER (DECL_NAME (field)));
4115 return error_mark_node;
4118 if (TREE_CODE (field) == FIELD_DECL
4119 && TYPE_USES_COMPLEX_INHERITANCE (basetype))
4121 /* Can't convert directly to ARGTYPE, since that
4122 may have the same pointer type as one of our
4124 rval = build1 (NOP_EXPR, argtype,
4125 convert_pointer_to (basetype, rval));
4126 TREE_CONSTANT (rval) = TREE_CONSTANT (TREE_OPERAND (rval, 0));
4129 /* This conversion is harmless. */
4130 rval = convert_force (argtype, rval, 0);
4132 if (! integer_zerop (DECL_FIELD_BITPOS (field)))
4134 tree offset = size_binop (EASY_DIV_EXPR, DECL_FIELD_BITPOS (field),
4135 size_int (BITS_PER_UNIT));
4136 int flag = TREE_CONSTANT (rval);
4137 offset = convert (sizetype, offset);
4138 rval = fold (build (PLUS_EXPR, argtype,
4139 rval, cp_convert (argtype, offset)));
4140 TREE_CONSTANT (rval) = flag;
4145 /* Construct and perhaps optimize a tree representation
4146 for a unary operation. CODE, a tree_code, specifies the operation
4147 and XARG is the operand. */
4150 build_x_unary_op (code, xarg)
4151 enum tree_code code;
4154 if (processing_template_decl)
4155 return build_min_nt (code, xarg, NULL_TREE);
4157 /* & rec, on incomplete RECORD_TYPEs is the simple opr &, not an
4159 if (code == ADDR_EXPR
4160 && TREE_CODE (xarg) != TEMPLATE_ID_EXPR
4161 && ((IS_AGGR_TYPE_CODE (TREE_CODE (TREE_TYPE (xarg)))
4162 && TYPE_SIZE (TREE_TYPE (xarg)) == NULL_TREE)
4163 || (TREE_CODE (xarg) == OFFSET_REF)))
4164 /* don't look for a function */;
4169 rval = build_new_op (code, LOOKUP_NORMAL, xarg,
4170 NULL_TREE, NULL_TREE);
4171 if (rval || code != ADDR_EXPR)
4175 if (code == ADDR_EXPR)
4177 if (TREE_CODE (xarg) == TARGET_EXPR)
4178 warning ("taking address of temporary");
4181 return build_unary_op (code, xarg, 0);
4184 /* Just like truthvalue_conversion, but we want a CLEANUP_POINT_EXPR. */
4187 condition_conversion (expr)
4191 if (processing_template_decl)
4193 t = cp_convert (boolean_type_node, expr);
4194 t = fold (build1 (CLEANUP_POINT_EXPR, boolean_type_node, t));
4198 /* C++: Must handle pointers to members.
4200 Perhaps type instantiation should be extended to handle conversion
4201 from aggregates to types we don't yet know we want? (Or are those
4202 cases typically errors which should be reported?)
4204 NOCONVERT nonzero suppresses the default promotions
4205 (such as from short to int). */
4208 build_unary_op (code, xarg, noconvert)
4209 enum tree_code code;
4213 /* No default_conversion here. It causes trouble for ADDR_EXPR. */
4214 register tree arg = xarg;
4215 register tree argtype = 0;
4216 char *errstring = NULL;
4219 if (arg == error_mark_node)
4220 return error_mark_node;
4225 /* This is used for unary plus, because a CONVERT_EXPR
4226 is enough to prevent anybody from looking inside for
4227 associativity, but won't generate any code. */
4228 if (!(arg = build_expr_type_conversion
4229 (WANT_ARITH | WANT_ENUM | WANT_POINTER, arg, 1)))
4230 errstring = "wrong type argument to unary plus";
4234 arg = default_conversion (arg);
4235 arg = build1 (NON_LVALUE_EXPR, TREE_TYPE (arg), arg);
4236 TREE_CONSTANT (arg) = TREE_CONSTANT (TREE_OPERAND (arg, 0));
4241 if (!(arg = build_expr_type_conversion (WANT_ARITH | WANT_ENUM, arg, 1)))
4242 errstring = "wrong type argument to unary minus";
4243 else if (!noconvert)
4244 arg = default_conversion (arg);
4248 if (TREE_CODE (TREE_TYPE (arg)) == COMPLEX_TYPE)
4252 arg = default_conversion (arg);
4254 else if (!(arg = build_expr_type_conversion (WANT_INT | WANT_ENUM,
4256 errstring = "wrong type argument to bit-complement";
4257 else if (!noconvert)
4258 arg = default_conversion (arg);
4262 if (!(arg = build_expr_type_conversion (WANT_ARITH | WANT_ENUM, arg, 1)))
4263 errstring = "wrong type argument to abs";
4264 else if (!noconvert)
4265 arg = default_conversion (arg);
4269 /* Conjugating a real value is a no-op, but allow it anyway. */
4270 if (!(arg = build_expr_type_conversion (WANT_ARITH | WANT_ENUM, arg, 1)))
4271 errstring = "wrong type argument to conjugation";
4272 else if (!noconvert)
4273 arg = default_conversion (arg);
4276 case TRUTH_NOT_EXPR:
4277 arg = cp_convert (boolean_type_node, arg);
4278 val = invert_truthvalue (arg);
4279 if (arg != error_mark_node)
4281 errstring = "in argument to unary !";
4288 if (TREE_CODE (arg) == COMPLEX_CST)
4289 return TREE_REALPART (arg);
4290 else if (TREE_CODE (TREE_TYPE (arg)) == COMPLEX_TYPE)
4291 return fold (build1 (REALPART_EXPR, TREE_TYPE (TREE_TYPE (arg)), arg));
4296 if (TREE_CODE (arg) == COMPLEX_CST)
4297 return TREE_IMAGPART (arg);
4298 else if (TREE_CODE (TREE_TYPE (arg)) == COMPLEX_TYPE)
4299 return fold (build1 (IMAGPART_EXPR, TREE_TYPE (TREE_TYPE (arg)), arg));
4301 return cp_convert (TREE_TYPE (arg), integer_zero_node);
4303 case PREINCREMENT_EXPR:
4304 case POSTINCREMENT_EXPR:
4305 case PREDECREMENT_EXPR:
4306 case POSTDECREMENT_EXPR:
4307 /* Handle complex lvalues (when permitted)
4308 by reduction to simpler cases. */
4310 val = unary_complex_lvalue (code, arg);
4314 /* Increment or decrement the real part of the value,
4315 and don't change the imaginary part. */
4316 if (TREE_CODE (TREE_TYPE (arg)) == COMPLEX_TYPE)
4320 arg = stabilize_reference (arg);
4321 real = build_unary_op (REALPART_EXPR, arg, 1);
4322 imag = build_unary_op (IMAGPART_EXPR, arg, 1);
4323 return build (COMPLEX_EXPR, TREE_TYPE (arg),
4324 build_unary_op (code, real, 1), imag);
4327 /* Report invalid types. */
4329 if (!(arg = build_expr_type_conversion (WANT_ARITH | WANT_POINTER,
4332 if (code == PREINCREMENT_EXPR)
4333 errstring ="no pre-increment operator for type";
4334 else if (code == POSTINCREMENT_EXPR)
4335 errstring ="no post-increment operator for type";
4336 else if (code == PREDECREMENT_EXPR)
4337 errstring ="no pre-decrement operator for type";
4339 errstring ="no post-decrement operator for type";
4343 /* Report something read-only. */
4345 if (TYPE_READONLY (TREE_TYPE (arg))
4346 || TREE_READONLY (arg))
4347 readonly_error (arg, ((code == PREINCREMENT_EXPR
4348 || code == POSTINCREMENT_EXPR)
4349 ? "increment" : "decrement"),
4354 tree result_type = TREE_TYPE (arg);
4356 arg = get_unwidened (arg, 0);
4357 argtype = TREE_TYPE (arg);
4359 /* ARM $5.2.5 last annotation says this should be forbidden. */
4360 if (TREE_CODE (argtype) == ENUMERAL_TYPE)
4361 pedwarn ("ANSI C++ forbids %sing an enum",
4362 (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
4363 ? "increment" : "decrement");
4365 /* Compute the increment. */
4367 if (TREE_CODE (argtype) == POINTER_TYPE)
4369 enum tree_code tmp = TREE_CODE (TREE_TYPE (argtype));
4370 if (TYPE_SIZE (complete_type (TREE_TYPE (argtype))) == 0)
4371 cp_error ("cannot %s a pointer to incomplete type `%T'",
4372 ((code == PREINCREMENT_EXPR
4373 || code == POSTINCREMENT_EXPR)
4374 ? "increment" : "decrement"), TREE_TYPE (argtype));
4375 else if ((pedantic || warn_pointer_arith)
4376 && (tmp == FUNCTION_TYPE || tmp == METHOD_TYPE
4377 || tmp == VOID_TYPE || tmp == OFFSET_TYPE))
4378 cp_pedwarn ("ANSI C++ forbids %sing a pointer of type `%T'",
4379 ((code == PREINCREMENT_EXPR
4380 || code == POSTINCREMENT_EXPR)
4381 ? "increment" : "decrement"), argtype);
4382 inc = c_sizeof_nowarn (TREE_TYPE (argtype));
4385 inc = integer_one_node;
4387 inc = cp_convert (argtype, inc);
4389 /* Handle incrementing a cast-expression. */
4391 switch (TREE_CODE (arg))
4396 case FIX_TRUNC_EXPR:
4397 case FIX_FLOOR_EXPR:
4398 case FIX_ROUND_EXPR:
4401 tree incremented, modify, value, compound;
4402 if (! lvalue_p (arg) && pedantic)
4403 pedwarn ("cast to non-reference type used as lvalue");
4404 arg = stabilize_reference (arg);
4405 if (code == PREINCREMENT_EXPR || code == PREDECREMENT_EXPR)
4408 value = save_expr (arg);
4409 incremented = build (((code == PREINCREMENT_EXPR
4410 || code == POSTINCREMENT_EXPR)
4411 ? PLUS_EXPR : MINUS_EXPR),
4412 argtype, value, inc);
4413 TREE_SIDE_EFFECTS (incremented) = 1;
4415 modify = build_modify_expr (arg, NOP_EXPR, incremented);
4416 compound = build (COMPOUND_EXPR, TREE_TYPE (arg), modify, value);
4418 /* Eliminate warning about unused result of + or -. */
4419 TREE_NO_UNUSED_WARNING (compound) = 1;
4427 /* Complain about anything else that is not a true lvalue. */
4428 if (!lvalue_or_else (arg, ((code == PREINCREMENT_EXPR
4429 || code == POSTINCREMENT_EXPR)
4430 ? "increment" : "decrement")))
4431 return error_mark_node;
4433 /* Forbid using -- on `bool'. */
4434 if (TREE_TYPE (arg) == boolean_type_node)
4436 if (code == POSTDECREMENT_EXPR || code == PREDECREMENT_EXPR)
4438 cp_error ("invalid use of `--' on bool variable `%D'", arg);
4439 return error_mark_node;
4442 /* This will only work if someone can convince Kenner to accept
4443 my patch to expand_increment. (jason) */
4444 val = build (code, TREE_TYPE (arg), arg, inc);
4446 if (code == POSTINCREMENT_EXPR)
4448 arg = stabilize_reference (arg);
4449 val = build (MODIFY_EXPR, TREE_TYPE (arg), arg,
4451 TREE_SIDE_EFFECTS (val) = 1;
4452 arg = save_expr (arg);
4453 val = build (COMPOUND_EXPR, TREE_TYPE (arg), val, arg);
4454 val = build (COMPOUND_EXPR, TREE_TYPE (arg), arg, val);
4457 val = build (MODIFY_EXPR, TREE_TYPE (arg), arg,
4462 val = build (code, TREE_TYPE (arg), arg, inc);
4464 TREE_SIDE_EFFECTS (val) = 1;
4465 return cp_convert (result_type, val);
4469 /* Note that this operation never does default_conversion
4470 regardless of NOCONVERT. */
4472 argtype = TREE_TYPE (arg);
4473 if (TREE_CODE (argtype) == REFERENCE_TYPE)
4477 build_pointer_type (TREE_TYPE (TREE_TYPE (arg))), arg);
4478 TREE_CONSTANT (arg) = TREE_CONSTANT (TREE_OPERAND (arg, 0));
4481 else if (pedantic && DECL_MAIN_P (arg))
4483 pedwarn ("taking address of function `main'");
4485 /* Let &* cancel out to simplify resulting code. */
4486 if (TREE_CODE (arg) == INDIRECT_REF)
4488 /* We don't need to have `current_class_ptr' wrapped in a
4489 NON_LVALUE_EXPR node. */
4490 if (arg == current_class_ref)
4491 return current_class_ptr;
4493 arg = TREE_OPERAND (arg, 0);
4494 if (TREE_CODE (TREE_TYPE (arg)) == REFERENCE_TYPE)
4498 build_pointer_type (TREE_TYPE (TREE_TYPE (arg))), arg);
4499 TREE_CONSTANT (arg) = TREE_CONSTANT (TREE_OPERAND (arg, 0));
4501 else if (lvalue_p (arg))
4502 /* Don't let this be an lvalue. */
4503 return non_lvalue (arg);
4507 /* For &x[y], return x+y */
4508 if (TREE_CODE (arg) == ARRAY_REF)
4510 if (mark_addressable (TREE_OPERAND (arg, 0)) == 0)
4511 return error_mark_node;
4512 return build_binary_op (PLUS_EXPR, TREE_OPERAND (arg, 0),
4513 TREE_OPERAND (arg, 1), 1);
4516 /* Uninstantiated types are all functions. Taking the
4517 address of a function is a no-op, so just return the
4520 if (TREE_CODE (arg) == IDENTIFIER_NODE
4521 && IDENTIFIER_OPNAME_P (arg))
4523 my_friendly_abort (117);
4524 /* We don't know the type yet, so just work around the problem.
4525 We know that this will resolve to an lvalue. */
4526 return build1 (ADDR_EXPR, unknown_type_node, arg);
4529 if (TREE_CODE (arg) == OVERLOAD
4530 || (TREE_CODE (arg) == OFFSET_REF
4531 && TREE_CODE (TREE_OPERAND (arg, 1)) == TEMPLATE_ID_EXPR))
4532 return build1 (ADDR_EXPR, unknown_type_node, arg);
4533 else if (TREE_CODE (arg) == TREE_LIST)
4535 if (TREE_CODE (TREE_VALUE (arg)) == FUNCTION_DECL)
4536 /* Unique overloaded non-member function. */
4537 return build_unary_op (ADDR_EXPR, TREE_VALUE (arg), 0);
4538 if (TREE_CHAIN (arg) == NULL_TREE
4539 && TREE_CODE (TREE_VALUE (arg)) == TREE_LIST
4540 && TREE_CODE (TREE_VALUE (TREE_VALUE (arg))) != OVERLOAD)
4541 /* Unique overloaded member function. */
4542 return build_unary_op (ADDR_EXPR, TREE_VALUE (TREE_VALUE (arg)),
4544 return build1 (ADDR_EXPR, unknown_type_node, arg);
4546 else if (TREE_CODE (arg) == TEMPLATE_ID_EXPR)
4551 /* We don't require a match here; it's possible that the
4552 context (like a cast to a particular type) will resolve
4553 the particular choice of template. */
4554 fn = determine_specialization (arg,
4562 fn = instantiate_template (fn, targs);
4563 mark_addressable (fn);
4564 return build_unary_op (ADDR_EXPR, fn, 0);
4567 return build1 (ADDR_EXPR, unknown_type_node, arg);
4570 /* Handle complex lvalues (when permitted)
4571 by reduction to simpler cases. */
4572 val = unary_complex_lvalue (code, arg);
4576 switch (TREE_CODE (arg))
4581 case FIX_TRUNC_EXPR:
4582 case FIX_FLOOR_EXPR:
4583 case FIX_ROUND_EXPR:
4585 if (! lvalue_p (arg) && pedantic)
4586 pedwarn ("taking the address of a cast to non-reference type");
4593 /* Allow the address of a constructor if all the elements
4595 if (TREE_CODE (arg) == CONSTRUCTOR && TREE_HAS_CONSTRUCTOR (arg)
4596 && TREE_CONSTANT (arg))
4598 /* Anything not already handled and not a true memory reference
4600 else if (TREE_CODE (argtype) != FUNCTION_TYPE
4601 && TREE_CODE (argtype) != METHOD_TYPE
4602 && !lvalue_or_else (arg, "unary `&'"))
4603 return error_mark_node;
4605 /* Ordinary case; arg is a COMPONENT_REF or a decl. */
4606 /* If the lvalue is const or volatile,
4607 merge that into the type that the address will point to. */
4608 if (TREE_CODE_CLASS (TREE_CODE (arg)) == 'd'
4609 || TREE_CODE_CLASS (TREE_CODE (arg)) == 'r')
4611 if (TREE_READONLY (arg) || TREE_THIS_VOLATILE (arg))
4612 argtype = cp_build_type_variant (argtype,
4613 TREE_READONLY (arg),
4614 TREE_THIS_VOLATILE (arg));
4617 argtype = build_pointer_type (argtype);
4619 if (mark_addressable (arg) == 0)
4620 return error_mark_node;
4625 if (TREE_CODE (arg) == COMPONENT_REF)
4626 addr = build_component_addr
4628 "attempt to take address of bit-field structure member `%s'");
4630 addr = build1 (code, argtype, arg);
4632 /* Address of a static or external variable or
4633 function counts as a constant */
4635 TREE_CONSTANT (addr) = 1;
4637 if (TREE_CODE (argtype) == POINTER_TYPE
4638 && TREE_CODE (TREE_TYPE (argtype)) == METHOD_TYPE)
4640 build_ptrmemfunc_type (argtype);
4641 addr = build_ptrmemfunc (argtype, addr, 0);
4654 argtype = TREE_TYPE (arg);
4655 return fold (build1 (code, argtype, arg));
4659 return error_mark_node;
4663 /* If CONVERSIONS is a conversion expression or a nested sequence of such,
4664 convert ARG with the same conversions in the same order
4665 and return the result. */
4668 convert_sequence (conversions, arg)
4672 switch (TREE_CODE (conversions))
4677 case FIX_TRUNC_EXPR:
4678 case FIX_FLOOR_EXPR:
4679 case FIX_ROUND_EXPR:
4681 return cp_convert (TREE_TYPE (conversions),
4682 convert_sequence (TREE_OPERAND (conversions, 0),
4691 /* Apply unary lvalue-demanding operator CODE to the expression ARG
4692 for certain kinds of expressions which are not really lvalues
4693 but which we can accept as lvalues.
4695 If ARG is not a kind of expression we can handle, return zero. */
4698 unary_complex_lvalue (code, arg)
4699 enum tree_code code;
4702 /* Handle (a, b) used as an "lvalue". */
4703 if (TREE_CODE (arg) == COMPOUND_EXPR)
4705 tree real_result = build_unary_op (code, TREE_OPERAND (arg, 1), 0);
4706 return build (COMPOUND_EXPR, TREE_TYPE (real_result),
4707 TREE_OPERAND (arg, 0), real_result);
4710 /* Handle (a ? b : c) used as an "lvalue". */
4711 if (TREE_CODE (arg) == COND_EXPR
4712 || TREE_CODE (arg) == MIN_EXPR || TREE_CODE (arg) == MAX_EXPR)
4713 return rationalize_conditional_expr (code, arg);
4715 if (TREE_CODE (arg) == MODIFY_EXPR
4716 || TREE_CODE (arg) == PREINCREMENT_EXPR
4717 || TREE_CODE (arg) == PREDECREMENT_EXPR)
4718 return unary_complex_lvalue
4719 (code, build (COMPOUND_EXPR, TREE_TYPE (TREE_OPERAND (arg, 0)),
4720 arg, TREE_OPERAND (arg, 0)));
4722 if (code != ADDR_EXPR)
4725 /* Handle (a = b) used as an "lvalue" for `&'. */
4726 if (TREE_CODE (arg) == MODIFY_EXPR
4727 || TREE_CODE (arg) == INIT_EXPR)
4729 tree real_result = build_unary_op (code, TREE_OPERAND (arg, 0), 0);
4730 arg = build (COMPOUND_EXPR, TREE_TYPE (real_result), arg, real_result);
4731 TREE_NO_UNUSED_WARNING (arg) = 1;
4735 if (TREE_CODE (TREE_TYPE (arg)) == FUNCTION_TYPE
4736 || TREE_CODE (TREE_TYPE (arg)) == METHOD_TYPE
4737 || TREE_CODE (TREE_TYPE (arg)) == OFFSET_TYPE)
4739 /* The representation of something of type OFFSET_TYPE
4740 is really the representation of a pointer to it.
4741 Here give the representation its true type. */
4744 my_friendly_assert (TREE_CODE (arg) != SCOPE_REF, 313);
4746 if (TREE_CODE (arg) != OFFSET_REF)
4749 t = TREE_OPERAND (arg, 1);
4751 /* Check all this code for right semantics. */
4752 if (TREE_CODE (t) == FUNCTION_DECL)
4753 return build_unary_op (ADDR_EXPR, t, 0);
4754 if (TREE_CODE (t) == VAR_DECL)
4755 return build_unary_op (ADDR_EXPR, t, 0);
4761 if (TREE_OPERAND (arg, 0)
4762 && (TREE_CODE (TREE_OPERAND (arg, 0)) != NOP_EXPR
4763 || (TREE_OPERAND (TREE_OPERAND (arg, 0), 0)
4764 != error_mark_node)))
4765 if (TREE_CODE (t) != FIELD_DECL)
4767 /* Don't know if this should return address to just
4768 _DECL, or actual address resolved in this expression. */
4769 sorry ("address of bound pointer-to-member expression");
4770 return error_mark_node;
4773 /* Add in the offset to the field. */
4774 offset = convert (sizetype,
4775 size_binop (EASY_DIV_EXPR,
4776 DECL_FIELD_BITPOS (t),
4777 size_int (BITS_PER_UNIT)));
4779 /* We offset all pointer to data members by 1 so that we can
4780 distinguish between a null pointer to data member and the first
4781 data member of a structure. */
4782 offset = size_binop (PLUS_EXPR, offset, size_int (1));
4784 type = build_offset_type (DECL_FIELD_CONTEXT (t), TREE_TYPE (t));
4785 type = build_pointer_type (type);
4787 return cp_convert (type, offset);
4792 /* We permit compiler to make function calls returning
4793 objects of aggregate type look like lvalues. */
4797 if (TREE_CODE (targ) == SAVE_EXPR)
4798 targ = TREE_OPERAND (targ, 0);
4800 if (TREE_CODE (targ) == CALL_EXPR && IS_AGGR_TYPE (TREE_TYPE (targ)))
4802 if (TREE_CODE (arg) == SAVE_EXPR)
4805 targ = build_cplus_new (TREE_TYPE (arg), arg);
4806 return build1 (ADDR_EXPR, build_pointer_type (TREE_TYPE (arg)), targ);
4809 if (TREE_CODE (arg) == SAVE_EXPR && TREE_CODE (targ) == INDIRECT_REF)
4810 return build (SAVE_EXPR, build_pointer_type (TREE_TYPE (arg)),
4811 TREE_OPERAND (targ, 0), current_function_decl, NULL);
4814 /* Don't let anything else be handled specially. */
4818 /* Mark EXP saying that we need to be able to take the
4819 address of it; it should not be allocated in a register.
4820 Value is 1 if successful.
4822 C++: we do not allow `current_class_ptr' to be addressable. */
4825 mark_addressable (exp)
4828 register tree x = exp;
4830 if (TREE_ADDRESSABLE (x) == 1)
4834 switch (TREE_CODE (x))
4841 x = TREE_OPERAND (x, 0);
4845 if (x == current_class_ptr)
4847 if (! flag_this_is_variable)
4848 error ("address of `this' not available");
4849 TREE_ADDRESSABLE (x) = 1; /* so compiler doesn't die later */
4850 put_var_into_stack (x);
4854 if (TREE_STATIC (x) && TREE_READONLY (x)
4855 && DECL_RTL (x) != 0
4856 && ! DECL_IN_MEMORY_P (x))
4858 /* We thought this would make a good constant variable,
4859 but we were wrong. */
4860 push_obstacks_nochange ();
4861 end_temporary_allocation ();
4863 TREE_ASM_WRITTEN (x) = 0;
4865 rest_of_decl_compilation (x, 0, IDENTIFIER_LOCAL_VALUE (x) == 0,
4867 TREE_ADDRESSABLE (x) = 1;
4873 /* Caller should not be trying to mark initialized
4874 constant fields addressable. */
4875 my_friendly_assert (DECL_LANG_SPECIFIC (x) == 0
4876 || DECL_IN_AGGR_P (x) == 0
4878 || DECL_EXTERNAL (x), 314);
4882 if (DECL_REGISTER (x) && !TREE_ADDRESSABLE (x)
4883 && !DECL_ARTIFICIAL (x) && extra_warnings)
4884 cp_warning ("address requested for `%D', which is declared `register'",
4886 put_var_into_stack (x);
4887 TREE_ADDRESSABLE (x) = 1;
4891 if (DECL_LANG_SPECIFIC (x) != 0)
4893 x = DECL_MAIN_VARIANT (x);
4894 /* We have to test both conditions here. The first may be
4895 non-zero in the case of processing a default function. The
4896 second may be non-zero in the case of a template function. */
4897 if (DECL_TEMPLATE_INFO (x) && !DECL_TEMPLATE_SPECIALIZATION (x))
4900 TREE_ADDRESSABLE (x) = 1;
4902 TREE_ADDRESSABLE (DECL_ASSEMBLER_NAME (x)) = 1;
4906 TREE_ADDRESSABLE (x) = 1;
4910 TREE_ADDRESSABLE (x) = 1;
4911 mark_addressable (TREE_OPERAND (x, 0));
4919 /* Build and return a conditional expression IFEXP ? OP1 : OP2. */
4922 build_x_conditional_expr (ifexp, op1, op2)
4923 tree ifexp, op1, op2;
4925 if (processing_template_decl)
4926 return build_min_nt (COND_EXPR, ifexp, op1, op2);
4928 return build_new_op (COND_EXPR, LOOKUP_NORMAL, ifexp, op1, op2);
4932 build_conditional_expr (ifexp, op1, op2)
4933 tree ifexp, op1, op2;
4935 register tree type1;
4936 register tree type2;
4937 register enum tree_code code1;
4938 register enum tree_code code2;
4939 register tree result_type = NULL_TREE;
4941 /* If second operand is omitted, it is the same as the first one;
4942 make sure it is calculated only once. */
4946 pedwarn ("ANSI C++ forbids omitting the middle term of a ?: expression");
4947 ifexp = op1 = save_expr (ifexp);
4950 ifexp = cp_convert (boolean_type_node, ifexp);
4952 if (TREE_CODE (ifexp) == ERROR_MARK)
4953 return error_mark_node;
4955 op1 = require_instantiated_type (TREE_TYPE (op2), op1, error_mark_node);
4956 if (op1 == error_mark_node)
4957 return error_mark_node;
4958 op2 = require_instantiated_type (TREE_TYPE (op1), op2, error_mark_node);
4959 if (op2 == error_mark_node)
4960 return error_mark_node;
4962 /* C++: REFERENCE_TYPES must be dereferenced. */
4963 type1 = TREE_TYPE (op1);
4964 code1 = TREE_CODE (type1);
4965 type2 = TREE_TYPE (op2);
4966 code2 = TREE_CODE (type2);
4968 if (code1 == REFERENCE_TYPE)
4970 op1 = convert_from_reference (op1);
4971 type1 = TREE_TYPE (op1);
4972 code1 = TREE_CODE (type1);
4974 if (code2 == REFERENCE_TYPE)
4976 op2 = convert_from_reference (op2);
4977 type2 = TREE_TYPE (op2);
4978 code2 = TREE_CODE (type2);
4981 /* Don't promote the operands separately if they promote
4982 the same way. Return the unpromoted type and let the combined
4983 value get promoted if necessary. */
4985 if (TYPE_MAIN_VARIANT (type1) == TYPE_MAIN_VARIANT (type2)
4986 && code2 != ARRAY_TYPE
4987 && code2 != FUNCTION_TYPE
4988 && code2 != METHOD_TYPE)
4992 if (TREE_CONSTANT (ifexp)
4993 && (TREE_CODE (ifexp) == INTEGER_CST
4994 || TREE_CODE (ifexp) == ADDR_EXPR))
4995 return (integer_zerop (ifexp) ? op2 : op1);
4997 if (TREE_CODE (op1) == CONST_DECL)
4998 op1 = DECL_INITIAL (op1);
4999 else if (TREE_READONLY_DECL_P (op1))
5000 op1 = decl_constant_value (op1);
5001 if (TREE_CODE (op2) == CONST_DECL)
5002 op2 = DECL_INITIAL (op2);
5003 else if (TREE_READONLY_DECL_P (op2))
5004 op2 = decl_constant_value (op2);
5006 type1 = cp_build_type_variant
5008 TREE_READONLY (op1) || TREE_READONLY (op2),
5009 TREE_THIS_VOLATILE (op1) || TREE_THIS_VOLATILE (op2));
5010 /* ??? This is a kludge to deal with the fact that
5011 we don't sort out integers and enums properly, yet. */
5012 result = fold (build (COND_EXPR, type1, ifexp, op1, op2));
5013 if (TREE_TYPE (result) != type1)
5014 result = build1 (NOP_EXPR, type1, result);
5015 /* Expand both sides into the same slot,
5016 hopefully the target of the ?: expression. */
5017 if (TREE_CODE (op1) == TARGET_EXPR && TREE_CODE (op2) == TARGET_EXPR)
5019 tree slot = build (VAR_DECL, TREE_TYPE (result));
5020 layout_decl (slot, 0);
5021 result = build (TARGET_EXPR, TREE_TYPE (result),
5022 slot, result, NULL_TREE, NULL_TREE);
5027 /* They don't match; promote them both and then try to reconcile them.
5028 But don't permit mismatching enum types. */
5029 if (code1 == ENUMERAL_TYPE)
5031 if (code2 == ENUMERAL_TYPE)
5033 cp_error ("enumeral mismatch in conditional expression: `%T' vs `%T'",
5035 return error_mark_node;
5037 else if (extra_warnings && ! IS_AGGR_TYPE_CODE (code2)
5038 && type2 != type_promotes_to (type1))
5039 warning ("enumeral and non-enumeral type in conditional expression");
5041 else if (extra_warnings
5042 && code2 == ENUMERAL_TYPE && ! IS_AGGR_TYPE_CODE (code1)
5043 && type1 != type_promotes_to (type2))
5044 warning ("enumeral and non-enumeral type in conditional expression");
5046 if (code1 != VOID_TYPE)
5048 op1 = default_conversion (op1);
5049 type1 = TREE_TYPE (op1);
5050 if (TYPE_PTRMEMFUNC_P (type1))
5051 type1 = TYPE_PTRMEMFUNC_FN_TYPE (type1);
5052 code1 = TREE_CODE (type1);
5054 if (code2 != VOID_TYPE)
5056 op2 = default_conversion (op2);
5057 type2 = TREE_TYPE (op2);
5058 if (TYPE_PTRMEMFUNC_P (type2))
5059 type2 = TYPE_PTRMEMFUNC_FN_TYPE (type2);
5060 code2 = TREE_CODE (type2);
5063 if (code1 == RECORD_TYPE && code2 == RECORD_TYPE
5064 && real_lvalue_p (op1) && real_lvalue_p (op2)
5065 && comptypes (type1, type2, -1))
5067 type1 = build_reference_type (type1);
5068 type2 = build_reference_type (type2);
5069 result_type = common_type (type1, type2);
5070 op1 = convert_to_reference (result_type, op1, CONV_IMPLICIT,
5071 LOOKUP_NORMAL, NULL_TREE);
5072 op2 = convert_to_reference (result_type, op2, CONV_IMPLICIT,
5073 LOOKUP_NORMAL, NULL_TREE);
5075 /* Quickly detect the usual case where op1 and op2 have the same type
5077 else if (TYPE_MAIN_VARIANT (type1) == TYPE_MAIN_VARIANT (type2))
5080 result_type = type1;
5082 result_type = cp_build_type_variant
5084 TREE_READONLY (op1) || TREE_READONLY (op2),
5085 TREE_THIS_VOLATILE (op1) || TREE_THIS_VOLATILE (op2));
5087 else if ((code1 == INTEGER_TYPE || code1 == REAL_TYPE)
5088 && (code2 == INTEGER_TYPE || code2 == REAL_TYPE))
5090 result_type = common_type (type1, type2);
5092 else if (code1 == VOID_TYPE || code2 == VOID_TYPE)
5094 if (pedantic && (code1 != VOID_TYPE || code2 != VOID_TYPE))
5095 pedwarn ("ANSI C++ forbids conditional expr with only one void side");
5096 result_type = void_type_node;
5098 else if (code1 == POINTER_TYPE && null_ptr_cst_p (op2))
5099 result_type = qualify_type (type1, type2);
5100 else if (code2 == POINTER_TYPE && null_ptr_cst_p (op1))
5101 result_type = qualify_type (type2, type1);
5102 else if (code1 == POINTER_TYPE && code2 == POINTER_TYPE)
5104 if (comp_target_types (type1, type2, 1))
5105 result_type = common_type (type1, type2);
5106 else if (TYPE_MAIN_VARIANT (TREE_TYPE (type1)) == void_type_node)
5108 if (pedantic && TREE_CODE (type2) == FUNCTION_TYPE)
5109 pedwarn ("ANSI C++ forbids conditional expr between `void *' and function pointer");
5110 result_type = qualify_type (type1, type2);
5112 else if (TYPE_MAIN_VARIANT (TREE_TYPE (type2)) == void_type_node)
5114 if (pedantic && TREE_CODE (type1) == FUNCTION_TYPE)
5115 pedwarn ("ANSI C++ forbids conditional expr between `void *' and function pointer");
5116 result_type = qualify_type (type2, type1);
5119 else if (comptypes (type2, type1, 0))
5120 result_type = type2;
5121 else if (IS_AGGR_TYPE (TREE_TYPE (type1))
5122 && IS_AGGR_TYPE (TREE_TYPE (type2))
5123 && (result_type = common_base_type (TREE_TYPE (type1),
5124 TREE_TYPE (type2))))
5126 if (result_type == error_mark_node)
5128 cp_error ("common base type of types `%T' and `%T' is ambiguous",
5129 TREE_TYPE (type1), TREE_TYPE (type2));
5130 result_type = ptr_type_node;
5135 && result_type != TREE_TYPE (type1)
5136 && result_type != TREE_TYPE (type2))
5137 cp_pedwarn ("`%T' and `%T' converted to `%T *' in conditional expression",
5138 type1, type2, result_type);
5140 result_type = build_pointer_type (result_type);
5145 pedwarn ("pointer type mismatch in conditional expression");
5146 result_type = ptr_type_node;
5149 else if (code1 == POINTER_TYPE && code2 == INTEGER_TYPE)
5151 pedwarn ("pointer/integer type mismatch in conditional expression");
5152 result_type = type1;
5154 else if (code2 == POINTER_TYPE && code1 == INTEGER_TYPE)
5156 pedwarn ("pointer/integer type mismatch in conditional expression");
5157 result_type = type2;
5162 /* The match does not look good. If either is
5163 an aggregate value, try converting to a scalar type. */
5164 if (code1 == RECORD_TYPE && code2 == RECORD_TYPE)
5166 cp_error ("aggregate mismatch in conditional expression: `%T' vs `%T'",
5168 return error_mark_node;
5170 /* Warning: this code assumes that conversion between cv-variants of
5171 a type is done using NOP_EXPRs. */
5172 if (code1 == RECORD_TYPE && TYPE_HAS_CONVERSION (type1))
5174 /* There are other types besides pointers and records. */
5176 if (code2 == POINTER_TYPE)
5177 tmp = build_pointer_type
5178 (build_type_variant (TREE_TYPE (type2), 1, 1));
5181 tmp = build_type_conversion (CONVERT_EXPR, tmp, op1, 0);
5182 if (tmp == NULL_TREE)
5184 cp_error ("incompatible types `%T' and `%T' in `?:'",
5186 return error_mark_node;
5188 if (tmp == error_mark_node)
5189 error ("ambiguous pointer conversion");
5192 result_type = common_type (type2, TREE_TYPE (tmp));
5195 else if (code2 == RECORD_TYPE && TYPE_HAS_CONVERSION (type2))
5198 if (code1 == POINTER_TYPE)
5199 tmp = build_pointer_type
5200 (build_type_variant (TREE_TYPE (type1), 1, 1));
5204 tmp = build_type_conversion (CONVERT_EXPR, tmp, op2, 0);
5205 if (tmp == NULL_TREE)
5207 cp_error ("incompatible types `%T' and `%T' in `?:'",
5209 return error_mark_node;
5211 if (tmp == error_mark_node)
5212 error ("ambiguous pointer conversion");
5215 result_type = common_type (type1, TREE_TYPE (tmp));
5218 else if (flag_cond_mismatch)
5219 result_type = void_type_node;
5222 error ("type mismatch in conditional expression");
5223 return error_mark_node;
5227 if (TREE_CODE (result_type) == POINTER_TYPE
5228 && TREE_CODE (TREE_TYPE (result_type)) == METHOD_TYPE)
5229 result_type = build_ptrmemfunc_type (result_type);
5231 if (result_type != TREE_TYPE (op1))
5232 op1 = convert_for_initialization
5233 (NULL_TREE, result_type, op1, LOOKUP_NORMAL, "converting", NULL_TREE, 0);
5234 if (result_type != TREE_TYPE (op2))
5235 op2 = convert_for_initialization
5236 (NULL_TREE, result_type, op2, LOOKUP_NORMAL, "converting", NULL_TREE, 0);
5238 if (TREE_CONSTANT (ifexp))
5239 return integer_zerop (ifexp) ? op2 : op1;
5241 return convert_from_reference
5242 (fold (build (COND_EXPR, result_type, ifexp, op1, op2)));
5245 /* Handle overloading of the ',' operator when needed. Otherwise,
5246 this function just builds an expression list. */
5249 build_x_compound_expr (list)
5252 tree rest = TREE_CHAIN (list);
5255 if (processing_template_decl)
5256 return build_min_nt (COMPOUND_EXPR, list, NULL_TREE);
5258 if (rest == NULL_TREE)
5259 return build_compound_expr (list);
5261 result = build_opfncall (COMPOUND_EXPR, LOOKUP_NORMAL,
5262 TREE_VALUE (list), TREE_VALUE (rest), NULL_TREE);
5264 return build_x_compound_expr (expr_tree_cons (NULL_TREE, result,
5265 TREE_CHAIN (rest)));
5267 if (! TREE_SIDE_EFFECTS (TREE_VALUE (list)))
5269 /* the left-hand operand of a comma expression is like an expression
5270 statement: we should warn if it doesn't have any side-effects,
5271 unless it was explicitly cast to (void). */
5272 if ((extra_warnings || warn_unused)
5273 && !(TREE_CODE (TREE_VALUE(list)) == CONVERT_EXPR
5274 && TREE_TYPE (TREE_VALUE(list)) == void_type_node))
5275 warning("left-hand operand of comma expression has no effect");
5277 #if 0 /* this requires a gcc backend patch to export warn_if_unused_value */
5278 else if (warn_unused)
5279 warn_if_unused_value (TREE_VALUE(list));
5282 return build_compound_expr
5283 (expr_tree_cons (NULL_TREE, TREE_VALUE (list),
5284 build_expr_list (NULL_TREE,
5285 build_x_compound_expr (rest))));
5288 /* Given a list of expressions, return a compound expression
5289 that performs them all and returns the value of the last of them. */
5292 build_compound_expr (list)
5297 if (TREE_READONLY_DECL_P (TREE_VALUE (list)))
5298 TREE_VALUE (list) = decl_constant_value (TREE_VALUE (list));
5300 if (TREE_CHAIN (list) == 0)
5302 /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
5303 Strip such NOP_EXPRs, since LIST is used in non-lvalue context. */
5304 if (TREE_CODE (list) == NOP_EXPR
5305 && TREE_TYPE (list) == TREE_TYPE (TREE_OPERAND (list, 0)))
5306 list = TREE_OPERAND (list, 0);
5308 /* Convert arrays to pointers. */
5309 if (TREE_CODE (TREE_TYPE (TREE_VALUE (list))) == ARRAY_TYPE)
5310 return default_conversion (TREE_VALUE (list));
5312 return TREE_VALUE (list);
5315 rest = build_compound_expr (TREE_CHAIN (list));
5317 /* When pedantic, a compound expression cannot be a constant expression. */
5318 if (! TREE_SIDE_EFFECTS (TREE_VALUE (list)) && ! pedantic)
5321 return build (COMPOUND_EXPR, TREE_TYPE (rest),
5322 break_out_cleanups (TREE_VALUE (list)), rest);
5326 build_static_cast (type, expr)
5332 if (type == error_mark_node || expr == error_mark_node)
5333 return error_mark_node;
5335 if (TREE_CODE (expr) == OFFSET_REF)
5336 expr = resolve_offset_ref (expr);
5338 if (processing_template_decl)
5340 tree t = build_min (STATIC_CAST_EXPR, copy_to_permanent (type),
5345 /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
5346 Strip such NOP_EXPRs if VALUE is being used in non-lvalue context. */
5347 if (TREE_CODE (type) != REFERENCE_TYPE
5348 && TREE_CODE (expr) == NOP_EXPR
5349 && TREE_TYPE (expr) == TREE_TYPE (TREE_OPERAND (expr, 0)))
5350 expr = TREE_OPERAND (expr, 0);
5352 if (TREE_CODE (type) == VOID_TYPE)
5353 return build1 (CONVERT_EXPR, type, expr);
5355 if (type_unknown_p (expr))
5357 expr = instantiate_type (type, expr, 1);
5358 if (expr == error_mark_node)
5359 return error_mark_node;
5362 if (TREE_CODE (type) == REFERENCE_TYPE)
5363 return (convert_from_reference
5364 (convert_to_reference (type, expr, CONV_STATIC|CONV_IMPLICIT,
5365 LOOKUP_COMPLAIN, NULL_TREE)));
5367 if (IS_AGGR_TYPE (type))
5368 return build_cplus_new
5369 (type, (build_method_call
5370 (NULL_TREE, ctor_identifier, build_expr_list (NULL_TREE, expr),
5371 TYPE_BINFO (type), LOOKUP_NORMAL)));
5373 expr = decay_conversion (expr);
5374 intype = TREE_TYPE (expr);
5376 /* FIXME handle casting to array type. */
5379 if (can_convert_arg (type, intype, expr))
5381 else if (TYPE_PTROB_P (type) && TYPE_PTROB_P (intype))
5384 if (IS_AGGR_TYPE (TREE_TYPE (type)) && IS_AGGR_TYPE (TREE_TYPE (intype))
5385 && (TYPE_READONLY (TREE_TYPE (type))
5386 >= TYPE_READONLY (TREE_TYPE (intype)))
5387 && (TYPE_VOLATILE (TREE_TYPE (type))
5388 >= TYPE_VOLATILE (TREE_TYPE (intype)))
5389 && (binfo = get_binfo (TREE_TYPE (intype), TREE_TYPE (type), 0))
5390 && ! TREE_VIA_VIRTUAL (binfo))
5393 else if (TYPE_PTRMEM_P (type) && TYPE_PTRMEM_P (intype))
5395 if (comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (type))),
5396 TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (intype))), 1)
5397 && (TYPE_READONLY (TREE_TYPE (TREE_TYPE (type)))
5398 >= TYPE_READONLY (TREE_TYPE (TREE_TYPE (intype))))
5399 && (TYPE_VOLATILE (TREE_TYPE (TREE_TYPE (type)))
5400 >= TYPE_VOLATILE (TREE_TYPE (TREE_TYPE (intype))))
5401 && (binfo = get_binfo (TYPE_OFFSET_BASETYPE (intype),
5402 TYPE_OFFSET_BASETYPE (type), 0))
5403 && ! TREE_VIA_VIRTUAL (binfo))
5406 else if (TREE_CODE (intype) != BOOLEAN_TYPE
5407 && TREE_CODE (type) != ARRAY_TYPE
5408 && TREE_CODE (type) != FUNCTION_TYPE
5409 && can_convert (intype, type))
5413 return build_c_cast (type, expr);
5415 cp_error ("static_cast from `%T' to `%T'", intype, type);
5416 return error_mark_node;
5420 build_reinterpret_cast (type, expr)
5425 if (type == error_mark_node || expr == error_mark_node)
5426 return error_mark_node;
5428 if (TREE_CODE (expr) == OFFSET_REF)
5429 expr = resolve_offset_ref (expr);
5431 if (processing_template_decl)
5433 tree t = build_min (REINTERPRET_CAST_EXPR,
5434 copy_to_permanent (type), expr);
5438 if (TREE_CODE (type) != REFERENCE_TYPE)
5440 expr = decay_conversion (expr);
5442 /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
5443 Strip such NOP_EXPRs if VALUE is being used in non-lvalue context. */
5444 if (TREE_CODE (expr) == NOP_EXPR
5445 && TREE_TYPE (expr) == TREE_TYPE (TREE_OPERAND (expr, 0)))
5446 expr = TREE_OPERAND (expr, 0);
5449 if (type_unknown_p (expr))
5451 expr = instantiate_type (type, expr, 1);
5452 if (expr == error_mark_node)
5453 return error_mark_node;
5456 intype = TREE_TYPE (expr);
5458 if (TREE_CODE (type) == REFERENCE_TYPE)
5460 if (! real_lvalue_p (expr))
5462 cp_error ("reinterpret_cast from `%T' rvalue to `%T'", intype, type);
5463 return error_mark_node;
5465 expr = build_unary_op (ADDR_EXPR, expr, 0);
5466 if (expr != error_mark_node)
5467 expr = build_reinterpret_cast
5468 (build_pointer_type (TREE_TYPE (type)), expr);
5469 if (expr != error_mark_node)
5470 expr = build_indirect_ref (expr, 0);
5473 else if (comptypes (TYPE_MAIN_VARIANT (intype), TYPE_MAIN_VARIANT (type), 1))
5474 return build_static_cast (type, expr);
5476 if (TYPE_PTR_P (type) && (TREE_CODE (intype) == INTEGER_TYPE
5477 || TREE_CODE (intype) == ENUMERAL_TYPE))
5479 else if (TREE_CODE (type) == INTEGER_TYPE && TYPE_PTR_P (intype))
5481 if (TYPE_PRECISION (type) < TYPE_PRECISION (intype))
5482 cp_pedwarn ("reinterpret_cast from `%T' to `%T' loses precision",
5485 else if ((TYPE_PTRFN_P (type) && TYPE_PTRFN_P (intype))
5486 || (TYPE_PTRMEMFUNC_P (type) && TYPE_PTRMEMFUNC_P (intype)))
5488 if (TREE_READONLY_DECL_P (expr))
5489 expr = decl_constant_value (expr);
5490 return fold (build1 (NOP_EXPR, type, expr));
5492 else if ((TYPE_PTRMEM_P (type) && TYPE_PTRMEM_P (intype))
5493 || (TYPE_PTROBV_P (type) && TYPE_PTROBV_P (intype)))
5495 if (! comp_ptr_ttypes_reinterpret (TREE_TYPE (type), TREE_TYPE (intype)))
5496 cp_pedwarn ("reinterpret_cast from `%T' to `%T' casts away const (or volatile)",
5499 if (TREE_READONLY_DECL_P (expr))
5500 expr = decl_constant_value (expr);
5501 return fold (build1 (NOP_EXPR, type, expr));
5503 else if ((TYPE_PTRFN_P (type) && TYPE_PTROBV_P (intype))
5504 || (TYPE_PTRFN_P (type) && TYPE_PTROBV_P (intype)))
5506 pedwarn ("ANSI C++ forbids casting between pointers to functions and objects");
5507 if (TREE_READONLY_DECL_P (expr))
5508 expr = decl_constant_value (expr);
5509 return fold (build1 (NOP_EXPR, type, expr));
5513 cp_error ("reinterpret_cast from `%T' to `%T'", intype, type);
5514 return error_mark_node;
5517 return cp_convert (type, expr);
5521 build_const_cast (type, expr)
5526 if (type == error_mark_node || expr == error_mark_node)
5527 return error_mark_node;
5529 if (TREE_CODE (expr) == OFFSET_REF)
5530 expr = resolve_offset_ref (expr);
5532 if (processing_template_decl)
5534 tree t = build_min (CONST_CAST_EXPR, copy_to_permanent (type),
5539 if (TREE_CODE (type) != REFERENCE_TYPE)
5541 expr = decay_conversion (expr);
5543 /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
5544 Strip such NOP_EXPRs if VALUE is being used in non-lvalue context. */
5545 if (TREE_CODE (expr) == NOP_EXPR
5546 && TREE_TYPE (expr) == TREE_TYPE (TREE_OPERAND (expr, 0)))
5547 expr = TREE_OPERAND (expr, 0);
5550 if (type_unknown_p (expr))
5552 expr = instantiate_type (type, expr, 1);
5553 if (expr == error_mark_node)
5554 return error_mark_node;
5557 intype = TREE_TYPE (expr);
5559 if (comptypes (TYPE_MAIN_VARIANT (intype), TYPE_MAIN_VARIANT (type), 1))
5560 return build_static_cast (type, expr);
5561 else if (TREE_CODE (type) == REFERENCE_TYPE)
5563 if (! real_lvalue_p (expr))
5565 cp_error ("const_cast from `%T' rvalue to `%T'", intype, type);
5566 return error_mark_node;
5569 if (comp_ptr_ttypes_const (TREE_TYPE (type), intype))
5571 expr = build_unary_op (ADDR_EXPR, expr, 0);
5572 expr = build1 (NOP_EXPR, type, expr);
5573 return convert_from_reference (expr);
5576 else if (TREE_CODE (type) == POINTER_TYPE
5577 && TREE_CODE (intype) == POINTER_TYPE
5578 && comp_ptr_ttypes_const (TREE_TYPE (type), TREE_TYPE (intype)))
5579 return cp_convert (type, expr);
5581 cp_error ("const_cast from `%T' to `%T'", intype, type);
5582 return error_mark_node;
5585 /* Build an expression representing a cast to type TYPE of expression EXPR.
5587 ALLOW_NONCONVERTING is true if we should allow non-converting constructors
5588 when doing the cast. */
5591 build_c_cast (type, expr)
5594 register tree value = expr;
5596 if (type == error_mark_node || expr == error_mark_node)
5597 return error_mark_node;
5599 /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
5600 Strip such NOP_EXPRs if VALUE is being used in non-lvalue context. */
5601 if (TREE_CODE (type) != REFERENCE_TYPE
5602 && TREE_CODE (value) == NOP_EXPR
5603 && TREE_TYPE (value) == TREE_TYPE (TREE_OPERAND (value, 0)))
5604 value = TREE_OPERAND (value, 0);
5606 if (TREE_TYPE (expr)
5607 && TREE_CODE (TREE_TYPE (expr)) == OFFSET_TYPE
5608 && TREE_CODE (type) != OFFSET_TYPE)
5609 value = resolve_offset_ref (value);
5611 if (TREE_CODE (type) == ARRAY_TYPE)
5613 /* Allow casting from T1* to T2[] because Cfront allows it.
5614 NIHCL uses it. It is not valid ANSI C however, and hence, not
5616 if (TREE_CODE (TREE_TYPE (expr)) == POINTER_TYPE)
5619 pedwarn ("ANSI C++ forbids casting to an array type");
5620 type = build_pointer_type (TREE_TYPE (type));
5624 error ("ANSI C++ forbids casting to an array type");
5625 return error_mark_node;
5629 if (TREE_CODE (type) == FUNCTION_TYPE
5630 || TREE_CODE (type) == METHOD_TYPE)
5632 cp_error ("casting to function type `%T'", type);
5633 return error_mark_node;
5636 if (IS_SIGNATURE (type))
5638 error ("cast specifies signature type");
5639 return error_mark_node;
5642 if (processing_template_decl)
5644 tree t = build_min (CAST_EXPR, type,
5645 min_tree_cons (NULL_TREE, value, NULL_TREE));
5649 if (TREE_CODE (type) == VOID_TYPE)
5650 value = build1 (CONVERT_EXPR, type, value);
5651 else if (TREE_TYPE (value) == NULL_TREE
5652 || type_unknown_p (value))
5654 value = instantiate_type (type, value, 1);
5656 if (value == error_mark_node)
5657 return error_mark_node;
5663 /* Convert functions and arrays to pointers and
5664 convert references to their expanded types,
5665 but don't convert any other types. If, however, we are
5666 casting to a class type, there's no reason to do this: the
5667 cast will only succeed if there is a converting constructor,
5668 and the default conversions will be done at that point. In
5669 fact, doing the default conversion here is actually harmful
5673 struct S { S(const A&); };
5675 since we don't want the array-to-pointer conversion done. */
5676 if (!IS_AGGR_TYPE (type))
5678 if (TREE_CODE (TREE_TYPE (value)) == FUNCTION_TYPE
5679 || (TREE_CODE (TREE_TYPE (value)) == METHOD_TYPE
5680 /* Don't do the default conversion if we want a
5681 pointer to a function. */
5682 && ! (TREE_CODE (type) == POINTER_TYPE
5683 && TREE_CODE (TREE_TYPE (type)) == FUNCTION_TYPE))
5684 || TREE_CODE (TREE_TYPE (value)) == ARRAY_TYPE
5685 || TREE_CODE (TREE_TYPE (value)) == REFERENCE_TYPE)
5686 value = default_conversion (value);
5688 else if (TREE_CODE (TREE_TYPE (value)) == REFERENCE_TYPE)
5689 /* However, even for class types, we still need to strip away
5690 the reference type, since the call to convert_force below
5691 does not expect the input expression to be of reference
5693 value = convert_from_reference (value);
5695 otype = TREE_TYPE (value);
5697 /* Optionally warn about potentially worrisome casts. */
5700 && TREE_CODE (type) == POINTER_TYPE
5701 && TREE_CODE (otype) == POINTER_TYPE)
5703 /* For C++ we make these regular warnings, rather than
5704 softening them into pedwarns. */
5705 if (TYPE_VOLATILE (TREE_TYPE (otype))
5706 && ! TYPE_VOLATILE (TREE_TYPE (type)))
5707 warning ("cast discards `volatile' from pointer target type");
5708 if (TYPE_READONLY (TREE_TYPE (otype))
5709 && ! TYPE_READONLY (TREE_TYPE (type)))
5710 warning ("cast discards `const' from pointer target type");
5713 /* Warn about possible alignment problems. */
5714 if (STRICT_ALIGNMENT && warn_cast_align
5715 && TREE_CODE (type) == POINTER_TYPE
5716 && TREE_CODE (otype) == POINTER_TYPE
5717 && TREE_CODE (TREE_TYPE (otype)) != VOID_TYPE
5718 && TREE_CODE (TREE_TYPE (otype)) != FUNCTION_TYPE
5719 && TYPE_ALIGN (TREE_TYPE (type)) > TYPE_ALIGN (TREE_TYPE (otype)))
5720 warning ("cast increases required alignment of target type");
5723 /* We should see about re-enabling these, they seem useful to
5725 if (TREE_CODE (type) == INTEGER_TYPE
5726 && TREE_CODE (otype) == POINTER_TYPE
5727 && TYPE_PRECISION (type) != TYPE_PRECISION (otype))
5728 warning ("cast from pointer to integer of different size");
5730 if (TREE_CODE (type) == POINTER_TYPE
5731 && TREE_CODE (otype) == INTEGER_TYPE
5732 && TYPE_PRECISION (type) != TYPE_PRECISION (otype)
5733 /* Don't warn about converting 0 to pointer,
5734 provided the 0 was explicit--not cast or made by folding. */
5735 && !(TREE_CODE (value) == INTEGER_CST && integer_zerop (value)))
5736 warning ("cast to pointer from integer of different size");
5739 if (TREE_CODE (type) == REFERENCE_TYPE)
5740 value = (convert_from_reference
5741 (convert_to_reference (type, value, CONV_C_CAST,
5742 LOOKUP_COMPLAIN, NULL_TREE)));
5747 if (TREE_READONLY_DECL_P (value))
5748 value = decl_constant_value (value);
5751 value = convert_force (type, value, CONV_C_CAST);
5753 /* Ignore any integer overflow caused by the cast. */
5754 if (TREE_CODE (value) == INTEGER_CST)
5756 TREE_OVERFLOW (value) = TREE_OVERFLOW (ovalue);
5757 TREE_CONSTANT_OVERFLOW (value) = TREE_CONSTANT_OVERFLOW (ovalue);
5762 /* Always produce some operator for an explicit cast,
5763 so we can tell (for -pedantic) that the cast is no lvalue. */
5764 if (TREE_CODE (type) != REFERENCE_TYPE && value == expr
5765 && real_lvalue_p (value))
5766 value = non_lvalue (value);
5771 /* Build an assignment expression of lvalue LHS from value RHS.
5772 MODIFYCODE is the code for a binary operator that we use
5773 to combine the old value of LHS with RHS to get the new value.
5774 Or else MODIFYCODE is NOP_EXPR meaning do a simple assignment.
5776 C++: If MODIFYCODE is INIT_EXPR, then leave references unbashed. */
5779 build_modify_expr (lhs, modifycode, rhs)
5781 enum tree_code modifycode;
5784 register tree result;
5786 tree lhstype = TREE_TYPE (lhs);
5787 tree olhstype = lhstype;
5790 /* Avoid duplicate error messages from operands that had errors. */
5791 if (lhs == error_mark_node || rhs == error_mark_node)
5792 return error_mark_node;
5794 /* Types that aren't fully specified cannot be used in assignments. */
5795 lhs = require_complete_type (lhs);
5799 /* Handle assignment to signature pointers/refs. */
5801 if (TYPE_LANG_SPECIFIC (lhstype)
5802 && (IS_SIGNATURE_POINTER (lhstype) || IS_SIGNATURE_REFERENCE (lhstype)))
5804 return build_signature_pointer_constructor (lhs, rhs);
5807 /* Handle control structure constructs used as "lvalues". */
5809 switch (TREE_CODE (lhs))
5811 /* Handle --foo = 5; as these are valid constructs in C++ */
5812 case PREDECREMENT_EXPR:
5813 case PREINCREMENT_EXPR:
5814 if (TREE_SIDE_EFFECTS (TREE_OPERAND (lhs, 0)))
5815 lhs = build (TREE_CODE (lhs), TREE_TYPE (lhs),
5816 stabilize_reference (TREE_OPERAND (lhs, 0)),
5817 TREE_OPERAND (lhs, 1));
5818 return build (COMPOUND_EXPR, lhstype,
5820 build_modify_expr (TREE_OPERAND (lhs, 0),
5823 /* Handle (a, b) used as an "lvalue". */
5825 newrhs = build_modify_expr (TREE_OPERAND (lhs, 1),
5827 if (newrhs == error_mark_node)
5828 return error_mark_node;
5829 return build (COMPOUND_EXPR, lhstype,
5830 TREE_OPERAND (lhs, 0), newrhs);
5833 newrhs = build_modify_expr (TREE_OPERAND (lhs, 0), modifycode, rhs);
5834 if (newrhs == error_mark_node)
5835 return error_mark_node;
5836 return build (COMPOUND_EXPR, lhstype, lhs, newrhs);
5838 /* Handle (a ? b : c) used as an "lvalue". */
5840 rhs = save_expr (rhs);
5842 /* Produce (a ? (b = rhs) : (c = rhs))
5843 except that the RHS goes through a save-expr
5844 so the code to compute it is only emitted once. */
5846 = build_conditional_expr (TREE_OPERAND (lhs, 0),
5847 build_modify_expr (cp_convert (TREE_TYPE (lhs), TREE_OPERAND (lhs, 1)),
5849 build_modify_expr (cp_convert (TREE_TYPE (lhs), TREE_OPERAND (lhs, 2)),
5851 if (cond == error_mark_node)
5853 /* Make sure the code to compute the rhs comes out
5854 before the split. */
5855 return build (COMPOUND_EXPR, TREE_TYPE (lhs),
5856 /* Case to void to suppress warning
5857 from warn_if_unused_value. */
5858 cp_convert (void_type_node, rhs), cond);
5865 if (TREE_CODE (lhs) == OFFSET_REF)
5867 if (TREE_OPERAND (lhs, 0) == NULL_TREE)
5869 /* Static class member? */
5870 tree member = TREE_OPERAND (lhs, 1);
5871 if (TREE_CODE (member) == VAR_DECL)
5875 compiler_error ("invalid static class member");
5876 return error_mark_node;
5880 lhs = resolve_offset_ref (lhs);
5882 olhstype = lhstype = TREE_TYPE (lhs);
5885 if (TREE_CODE (lhstype) == REFERENCE_TYPE
5886 && modifycode != INIT_EXPR)
5888 lhs = convert_from_reference (lhs);
5889 olhstype = lhstype = TREE_TYPE (lhs);
5892 /* If a binary op has been requested, combine the old LHS value with the RHS
5893 producing the value we should actually store into the LHS. */
5895 if (modifycode == INIT_EXPR)
5897 if (! IS_AGGR_TYPE (lhstype))
5898 /* Do the default thing */;
5901 result = build_method_call (lhs, ctor_identifier,
5902 build_expr_list (NULL_TREE, rhs),
5903 TYPE_BINFO (lhstype), LOOKUP_NORMAL);
5904 if (result == NULL_TREE)
5905 return error_mark_node;
5909 else if (modifycode == NOP_EXPR)
5911 /* `operator=' is not an inheritable operator. */
5912 if (! IS_AGGR_TYPE (lhstype))
5913 /* Do the default thing */;
5916 result = build_opfncall (MODIFY_EXPR, LOOKUP_NORMAL,
5917 lhs, rhs, make_node (NOP_EXPR));
5918 if (result == NULL_TREE)
5919 return error_mark_node;
5924 else if (PROMOTES_TO_AGGR_TYPE (lhstype, REFERENCE_TYPE))
5926 my_friendly_abort (978652);
5930 lhs = stabilize_reference (lhs);
5931 newrhs = build_binary_op (modifycode, lhs, rhs, 1);
5932 if (newrhs == error_mark_node)
5934 cp_error (" in evaluation of `%Q(%#T, %#T)'", modifycode,
5935 TREE_TYPE (lhs), TREE_TYPE (rhs));
5936 return error_mark_node;
5940 /* Handle a cast used as an "lvalue".
5941 We have already performed any binary operator using the value as cast.
5942 Now convert the result to the cast type of the lhs,
5943 and then true type of the lhs and store it there;
5944 then convert result back to the cast type to be the value
5945 of the assignment. */
5947 switch (TREE_CODE (lhs))
5952 case FIX_TRUNC_EXPR:
5953 case FIX_FLOOR_EXPR:
5954 case FIX_ROUND_EXPR:
5956 if (TREE_CODE (TREE_TYPE (newrhs)) == ARRAY_TYPE
5957 || TREE_CODE (TREE_TYPE (newrhs)) == FUNCTION_TYPE
5958 || TREE_CODE (TREE_TYPE (newrhs)) == METHOD_TYPE
5959 || TREE_CODE (TREE_TYPE (newrhs)) == OFFSET_TYPE)
5960 newrhs = default_conversion (newrhs);
5962 tree inner_lhs = TREE_OPERAND (lhs, 0);
5965 /* WP 5.4.1: The result is an lvalue if T is a reference type,
5966 otherwise the result is an rvalue. */
5967 if (! lvalue_p (lhs))
5968 pedwarn ("ANSI C++ forbids cast to non-reference type used as lvalue");
5970 result = build_modify_expr (inner_lhs, NOP_EXPR,
5971 cp_convert (TREE_TYPE (inner_lhs),
5972 cp_convert (lhstype, newrhs)));
5973 if (result == error_mark_node)
5975 return cp_convert (TREE_TYPE (lhs), result);
5982 /* Now we have handled acceptable kinds of LHS that are not truly lvalues.
5983 Reject anything strange now. */
5985 if (!lvalue_or_else (lhs, "assignment"))
5986 return error_mark_node;
5988 GNU_xref_assign (lhs);
5990 /* Warn about storing in something that is `const'. */
5991 /* For C++, don't warn if this is initialization. */
5992 if (modifycode != INIT_EXPR
5993 /* For assignment to `const' signature pointer/reference fields,
5994 don't warn either, we already printed a better message before. */
5995 && ! (TREE_CODE (lhs) == COMPONENT_REF
5996 && (IS_SIGNATURE_POINTER (TREE_TYPE (TREE_OPERAND (lhs, 0)))
5997 || IS_SIGNATURE_REFERENCE (TREE_TYPE (TREE_OPERAND (lhs, 0)))))
5998 && (TREE_READONLY (lhs) || TYPE_READONLY (lhstype)
5999 || ((TREE_CODE (lhstype) == RECORD_TYPE
6000 || TREE_CODE (lhstype) == UNION_TYPE)
6001 && C_TYPE_FIELDS_READONLY (lhstype))
6002 || (TREE_CODE (lhstype) == REFERENCE_TYPE
6003 && TYPE_READONLY (TREE_TYPE (lhstype)))))
6004 readonly_error (lhs, "assignment", 0);
6006 /* If storing into a structure or union member,
6007 it has probably been given type `int'.
6008 Compute the type that would go with
6009 the actual amount of storage the member occupies. */
6011 if (TREE_CODE (lhs) == COMPONENT_REF
6012 && (TREE_CODE (lhstype) == INTEGER_TYPE
6013 || TREE_CODE (lhstype) == REAL_TYPE
6014 || TREE_CODE (lhstype) == ENUMERAL_TYPE))
6016 lhstype = TREE_TYPE (get_unwidened (lhs, 0));
6018 /* If storing in a field that is in actuality a short or narrower
6019 than one, we must store in the field in its actual type. */
6021 if (lhstype != TREE_TYPE (lhs))
6023 lhs = copy_node (lhs);
6024 TREE_TYPE (lhs) = lhstype;
6028 /* check to see if there is an assignment to `this' */
6029 if (lhs == current_class_ptr)
6031 if (flag_this_is_variable > 0
6032 && DECL_NAME (current_function_decl) != NULL_TREE
6033 && (DECL_NAME (current_function_decl)
6034 != constructor_name (current_class_type)))
6035 warning ("assignment to `this' not in constructor or destructor");
6036 current_function_just_assigned_this = 1;
6039 /* The TREE_TYPE of RHS may be TYPE_UNKNOWN. This can happen
6040 when the type of RHS is not yet known, i.e. its type
6041 is inherited from LHS. */
6042 rhs = require_instantiated_type (lhstype, newrhs, error_mark_node);
6043 if (rhs == error_mark_node)
6044 return error_mark_node;
6047 if (modifycode != INIT_EXPR)
6049 /* Make modifycode now either a NOP_EXPR or an INIT_EXPR. */
6050 modifycode = NOP_EXPR;
6051 /* Reference-bashing */
6052 if (TREE_CODE (lhstype) == REFERENCE_TYPE)
6054 tree tmp = convert_from_reference (lhs);
6055 lhstype = TREE_TYPE (tmp);
6056 if (TYPE_SIZE (lhstype) == 0)
6058 incomplete_type_error (lhs, lhstype);
6059 return error_mark_node;
6064 if (TREE_CODE (TREE_TYPE (newrhs)) == REFERENCE_TYPE)
6066 tree tmp = convert_from_reference (newrhs);
6067 if (TYPE_SIZE (TREE_TYPE (tmp)) == 0)
6069 incomplete_type_error (newrhs, TREE_TYPE (tmp));
6070 return error_mark_node;
6076 if (TREE_SIDE_EFFECTS (lhs))
6077 lhs = stabilize_reference (lhs);
6078 if (TREE_SIDE_EFFECTS (newrhs))
6079 newrhs = stabilize_reference (newrhs);
6081 /* Convert new value to destination type. */
6083 if (TREE_CODE (lhstype) == ARRAY_TYPE)
6087 if (! comptypes (lhstype, TREE_TYPE (rhs), 0))
6089 cp_error ("incompatible types in assignment of `%T' to `%T'",
6090 TREE_TYPE (rhs), lhstype);
6091 return error_mark_node;
6094 /* Allow array assignment in compiler-generated code. */
6095 if (pedantic && ! DECL_ARTIFICIAL (current_function_decl))
6096 pedwarn ("ANSI C++ forbids assignment of arrays");
6098 /* Have to wrap this in RTL_EXPR for two cases:
6099 in base or member initialization and if we
6100 are a branch of a ?: operator. Since we
6101 can't easily know the latter, just do it always. */
6103 result = make_node (RTL_EXPR);
6105 TREE_TYPE (result) = void_type_node;
6106 do_pending_stack_adjust ();
6107 start_sequence_for_rtl_expr (result);
6109 /* As a matter of principle, `start_sequence' should do this. */
6112 from_array = TREE_CODE (TREE_TYPE (newrhs)) == ARRAY_TYPE
6113 ? 1 + (modifycode != INIT_EXPR): 0;
6114 expand_vec_init (lhs, lhs, array_type_nelts (lhstype), newrhs,
6117 do_pending_stack_adjust ();
6119 TREE_SIDE_EFFECTS (result) = 1;
6120 RTL_EXPR_SEQUENCE (result) = get_insns ();
6121 RTL_EXPR_RTL (result) = const0_rtx;
6126 if (modifycode == INIT_EXPR)
6128 newrhs = convert_for_initialization (lhs, lhstype, newrhs, LOOKUP_NORMAL,
6129 "assignment", NULL_TREE, 0);
6130 if (lhs == DECL_RESULT (current_function_decl))
6132 if (DECL_INITIAL (lhs))
6133 warning ("return value from function receives multiple initializations");
6134 DECL_INITIAL (lhs) = newrhs;
6139 /* Avoid warnings on enum bit fields. */
6140 if (TREE_CODE (olhstype) == ENUMERAL_TYPE
6141 && TREE_CODE (lhstype) == INTEGER_TYPE)
6143 newrhs = convert_for_assignment (olhstype, newrhs, "assignment",
6145 newrhs = convert_force (lhstype, newrhs, 0);
6148 newrhs = convert_for_assignment (lhstype, newrhs, "assignment",
6150 if (TREE_CODE (newrhs) == CALL_EXPR
6151 && TYPE_NEEDS_CONSTRUCTING (lhstype))
6152 newrhs = build_cplus_new (lhstype, newrhs);
6154 /* Can't initialize directly from a TARGET_EXPR, since that would
6155 cause the lhs to be constructed twice, and possibly result in
6156 accidental self-initialization. So we force the TARGET_EXPR to be
6157 expanded without a target. */
6158 if (TREE_CODE (newrhs) == TARGET_EXPR)
6159 newrhs = build (COMPOUND_EXPR, TREE_TYPE (newrhs), newrhs,
6160 TREE_OPERAND (newrhs, 0));
6163 if (newrhs == error_mark_node)
6164 return error_mark_node;
6166 if (TREE_CODE (newrhs) == COND_EXPR)
6169 tree cond = TREE_OPERAND (newrhs, 0);
6171 if (TREE_SIDE_EFFECTS (lhs))
6172 cond = build_compound_expr (tree_cons
6174 build_expr_list (NULL_TREE, cond)));
6176 /* Cannot have two identical lhs on this one tree (result) as preexpand
6177 calls will rip them out and fill in RTL for them, but when the
6178 rtl is generated, the calls will only be in the first side of the
6179 condition, not on both, or before the conditional jump! (mrs) */
6180 lhs1 = break_out_calls (lhs);
6183 /* If there's no change, the COND_EXPR behaves like any other rhs. */
6184 result = build (modifycode == NOP_EXPR ? MODIFY_EXPR : INIT_EXPR,
6185 lhstype, lhs, newrhs);
6188 tree result_type = TREE_TYPE (newrhs);
6189 /* We have to convert each arm to the proper type because the
6190 types may have been munged by constant folding. */
6192 = build (COND_EXPR, result_type, cond,
6193 build_modify_expr (lhs, modifycode,
6194 cp_convert (result_type,
6195 TREE_OPERAND (newrhs, 1))),
6196 build_modify_expr (lhs1, modifycode,
6197 cp_convert (result_type,
6198 TREE_OPERAND (newrhs, 2))));
6202 result = build (modifycode == NOP_EXPR ? MODIFY_EXPR : INIT_EXPR,
6203 lhstype, lhs, newrhs);
6205 TREE_SIDE_EFFECTS (result) = 1;
6207 /* If we got the LHS in a different type for storing in,
6208 convert the result back to the nominal type of LHS
6209 so that the value we return always has the same type
6210 as the LHS argument. */
6212 if (olhstype == TREE_TYPE (result))
6214 /* Avoid warnings converting integral types back into enums
6215 for enum bit fields. */
6216 if (TREE_CODE (TREE_TYPE (result)) == INTEGER_TYPE
6217 && TREE_CODE (olhstype) == ENUMERAL_TYPE)
6219 result = build (COMPOUND_EXPR, olhstype, result, olhs);
6220 TREE_NO_UNUSED_WARNING (result) = 1;
6223 return convert_for_assignment (olhstype, result, "assignment",
6228 build_x_modify_expr (lhs, modifycode, rhs)
6230 enum tree_code modifycode;
6233 if (processing_template_decl)
6234 return build_min_nt (MODOP_EXPR, lhs,
6235 build_min_nt (modifycode, NULL_TREE, NULL_TREE), rhs);
6237 if (modifycode != NOP_EXPR)
6239 tree rval = build_opfncall (MODIFY_EXPR, LOOKUP_NORMAL, lhs, rhs,
6240 make_node (modifycode));
6244 return build_modify_expr (lhs, modifycode, rhs);
6247 /* Return 0 if EXP is not a valid lvalue in this language
6248 even though `lvalue_or_else' would accept it. */
6251 language_lvalue_valid (exp)
6252 tree exp ATTRIBUTE_UNUSED;
6257 /* Get difference in deltas for different pointer to member function
6258 types. Return integer_zero_node, if FROM cannot be converted to a
6259 TO type. If FORCE is true, then allow reverse conversions as well. */
6262 get_delta_difference (from, to, force)
6266 tree delta = integer_zero_node;
6272 /* Should get_base_distance here, so we can check if any thing along the
6273 path is virtual, and we need to make sure we stay
6274 inside the real binfos when going through virtual bases.
6275 Maybe we should replace virtual bases with
6276 binfo_member (...CLASSTYPE_VBASECLASSES...)... (mrs) */
6277 binfo = get_binfo (from, to, 1);
6278 if (binfo == error_mark_node)
6280 error (" in pointer to member function conversion");
6287 error_not_base_type (from, to);
6288 error (" in pointer to member conversion");
6291 binfo = get_binfo (to, from, 1);
6292 if (binfo == 0 || binfo == error_mark_node)
6294 if (TREE_VIA_VIRTUAL (binfo))
6296 binfo = binfo_member (BINFO_TYPE (binfo),
6297 CLASSTYPE_VBASECLASSES (from));
6298 cp_warning ("pointer to member cast to virtual base `%T'",
6299 BINFO_TYPE (binfo));
6300 warning (" will only work if you are very careful");
6302 delta = BINFO_OFFSET (binfo);
6303 delta = cp_convert (ptrdiff_type_node, delta);
6305 return build_binary_op (MINUS_EXPR,
6310 if (TREE_VIA_VIRTUAL (binfo))
6314 cp_warning ("pointer to member cast from virtual base `%T'",
6315 BINFO_TYPE (binfo));
6316 warning (" will only work if you are very careful");
6319 cp_error ("pointer to member conversion from virtual base `%T'",
6320 BINFO_TYPE (binfo));
6323 return BINFO_OFFSET (binfo);
6327 build_ptrmemfunc1 (type, delta, idx, pfn, delta2)
6328 tree type, delta, idx, pfn, delta2;
6333 /* This is the old way we did it. We want to avoid calling
6334 digest_init, so that it can give an error if we use { } when
6335 initializing a pointer to member function. */
6339 u = build_nt (CONSTRUCTOR, NULL_TREE,
6340 expr_tree_cons (pfn_identifier, pfn, NULL_TREE));
6344 u = build_nt (CONSTRUCTOR, NULL_TREE,
6345 expr_tree_cons (delta2_identifier, delta2, NULL_TREE));
6348 u = build_nt (CONSTRUCTOR, NULL_TREE,
6349 expr_tree_cons (NULL_TREE, delta,
6350 expr_tree_cons (NULL_TREE, idx,
6351 expr_tree_cons (NULL_TREE, u, NULL_TREE))));
6353 return digest_init (type, u, (tree*)0);
6355 tree delta_field, idx_field, pfn_or_delta2_field, pfn_field, delta2_field;
6357 int allconstant, allsimple;
6359 delta_field = TYPE_FIELDS (type);
6360 idx_field = TREE_CHAIN (delta_field);
6361 pfn_or_delta2_field = TREE_CHAIN (idx_field);
6362 subtype = TREE_TYPE (pfn_or_delta2_field);
6363 pfn_field = TYPE_FIELDS (subtype);
6364 delta2_field = TREE_CHAIN (pfn_field);
6368 allconstant = TREE_CONSTANT (pfn);
6369 allsimple = !! initializer_constant_valid_p (pfn, TREE_TYPE (pfn));
6370 u = expr_tree_cons (pfn_field, pfn, NULL_TREE);
6374 delta2 = convert_and_check (delta_type_node, delta2);
6375 allconstant = TREE_CONSTANT (delta2);
6376 allsimple = !! initializer_constant_valid_p (delta2, TREE_TYPE (delta2));
6377 u = expr_tree_cons (delta2_field, delta2, NULL_TREE);
6380 delta = convert_and_check (delta_type_node, delta);
6381 idx = convert_and_check (delta_type_node, idx);
6383 allconstant = allconstant && TREE_CONSTANT (delta) && TREE_CONSTANT (idx);
6384 allsimple = allsimple
6385 && initializer_constant_valid_p (delta, TREE_TYPE (delta))
6386 && initializer_constant_valid_p (idx, TREE_TYPE (idx));
6388 u = build (CONSTRUCTOR, subtype, NULL_TREE, u);
6389 u = expr_tree_cons (delta_field, delta,
6390 expr_tree_cons (idx_field, idx,
6391 expr_tree_cons (pfn_or_delta2_field, u, NULL_TREE)));
6392 u = build (CONSTRUCTOR, type, NULL_TREE, u);
6393 TREE_CONSTANT (u) = allconstant;
6394 TREE_STATIC (u) = allconstant && allsimple;
6399 /* Build a constructor for a pointer to member function. It can be
6400 used to initialize global variables, local variable, or used
6401 as a value in expressions. TYPE is the POINTER to METHOD_TYPE we
6404 If FORCE is non-zero, then force this conversion, even if
6405 we would rather not do it. Usually set when using an explicit
6408 Return error_mark_node, if something goes wrong. */
6411 build_ptrmemfunc (type, pfn, force)
6415 tree idx = integer_zero_node;
6416 tree delta = integer_zero_node;
6417 tree delta2 = integer_zero_node;
6419 tree npfn = NULL_TREE;
6421 /* Handle multiple conversions of pointer to member functions. */
6422 if (TYPE_PTRMEMFUNC_P (TREE_TYPE (pfn)))
6424 tree ndelta, ndelta2;
6428 /* Is is already the right type? */
6429 if (type == TYPE_PTRMEMFUNC_FN_TYPE (TREE_TYPE (pfn)))
6432 pfn_type = TYPE_PTRMEMFUNC_FN_TYPE (TREE_TYPE (pfn));
6434 && comp_target_types (type, pfn_type, 0) != 1)
6435 cp_error ("conversion to `%T' from `%T'", type, pfn_type);
6437 ndelta = cp_convert (ptrdiff_type_node, build_component_ref (pfn, delta_identifier, NULL_TREE, 0));
6438 ndelta2 = cp_convert (ptrdiff_type_node, DELTA2_FROM_PTRMEMFUNC (pfn));
6439 idx = build_component_ref (pfn, index_identifier, NULL_TREE, 0);
6441 n = get_delta_difference (TYPE_METHOD_BASETYPE (TREE_TYPE (pfn_type)),
6442 TYPE_METHOD_BASETYPE (TREE_TYPE (type)),
6445 delta = build_binary_op (PLUS_EXPR, ndelta, n, 1);
6446 delta2 = build_binary_op (PLUS_EXPR, ndelta2, n, 1);
6447 e1 = fold (build (GT_EXPR, boolean_type_node, idx, integer_zero_node));
6449 e2 = build_ptrmemfunc1 (TYPE_GET_PTRMEMFUNC_TYPE (type), delta, idx,
6452 pfn = PFN_FROM_PTRMEMFUNC (pfn);
6453 npfn = build1 (NOP_EXPR, type, pfn);
6454 TREE_CONSTANT (npfn) = TREE_CONSTANT (pfn);
6456 e3 = build_ptrmemfunc1 (TYPE_GET_PTRMEMFUNC_TYPE (type), delta, idx, npfn,
6458 return build_conditional_expr (e1, e2, e3);
6461 /* Handle null pointer to member function conversions. */
6462 if (integer_zerop (pfn))
6464 pfn = build_c_cast (type, integer_zero_node);
6465 return build_ptrmemfunc1 (TYPE_GET_PTRMEMFUNC_TYPE (type),
6466 integer_zero_node, integer_zero_node,
6470 if (TREE_CODE (pfn) == TREE_LIST
6471 || (TREE_CODE (pfn) == ADDR_EXPR
6472 && TREE_CODE (TREE_OPERAND (pfn, 0)) == TREE_LIST))
6473 return instantiate_type (type, pfn, 1);
6476 && comp_target_types (type, TREE_TYPE (pfn), 0) != 1)
6477 cp_error ("conversion to `%T' from `%T'", type, TREE_TYPE (pfn));
6479 /* Allow pointer to member conversions here. */
6480 delta = get_delta_difference (TYPE_METHOD_BASETYPE (TREE_TYPE (TREE_TYPE (pfn))),
6481 TYPE_METHOD_BASETYPE (TREE_TYPE (type)),
6483 delta2 = build_binary_op (PLUS_EXPR, delta2, delta, 1);
6485 if (TREE_CODE (TREE_OPERAND (pfn, 0)) != FUNCTION_DECL)
6486 warning ("assuming pointer to member function is non-virtual");
6488 if (TREE_CODE (TREE_OPERAND (pfn, 0)) == FUNCTION_DECL
6489 && DECL_VINDEX (TREE_OPERAND (pfn, 0)))
6491 /* Find the offset to the vfield pointer in the object. */
6492 vfield_offset = get_binfo (DECL_CONTEXT (TREE_OPERAND (pfn, 0)),
6493 DECL_CLASS_CONTEXT (TREE_OPERAND (pfn, 0)),
6495 vfield_offset = get_vfield_offset (vfield_offset);
6496 delta2 = size_binop (PLUS_EXPR, vfield_offset, delta2);
6498 /* Map everything down one to make room for the null pointer to member. */
6499 idx = size_binop (PLUS_EXPR,
6500 DECL_VINDEX (TREE_OPERAND (pfn, 0)),
6505 idx = size_binop (MINUS_EXPR, integer_zero_node, integer_one_node);
6507 if (type == TREE_TYPE (pfn))
6513 npfn = build1 (NOP_EXPR, type, pfn);
6514 TREE_CONSTANT (npfn) = TREE_CONSTANT (pfn);
6518 return build_ptrmemfunc1 (TYPE_GET_PTRMEMFUNC_TYPE (type), delta, idx, npfn, delta2);
6521 /* Convert value RHS to type TYPE as preparation for an assignment
6522 to an lvalue of type TYPE.
6523 The real work of conversion is done by `convert'.
6524 The purpose of this function is to generate error messages
6525 for assignments that are not allowed in C.
6526 ERRTYPE is a string to use in error messages:
6527 "assignment", "return", etc.
6529 C++: attempts to allow `convert' to find conversions involving
6530 implicit type conversion between aggregate and scalar types
6531 as per 8.5.6 of C++ manual. Does not randomly dereference
6532 pointers to aggregates! */
6535 convert_for_assignment (type, rhs, errtype, fndecl, parmnum)
6541 register enum tree_code codel = TREE_CODE (type);
6542 register tree rhstype;
6543 register enum tree_code coder = TREE_CODE (TREE_TYPE (rhs));
6545 if (coder == UNKNOWN_TYPE)
6546 rhs = instantiate_type (type, rhs, 1);
6548 if (coder == ERROR_MARK)
6549 return error_mark_node;
6551 if (codel == OFFSET_TYPE)
6553 type = TREE_TYPE (type);
6554 codel = TREE_CODE (type);
6557 /* Strip NON_LVALUE_EXPRs since we aren't using as an lvalue. */
6558 if (TREE_CODE (rhs) == NON_LVALUE_EXPR)
6559 rhs = TREE_OPERAND (rhs, 0);
6561 if (rhs == error_mark_node)
6562 return error_mark_node;
6564 if (TREE_CODE (rhs) == TREE_LIST && TREE_VALUE (rhs) == error_mark_node)
6565 return error_mark_node;
6567 if (TREE_CODE (TREE_TYPE (rhs)) == OFFSET_TYPE)
6569 rhs = resolve_offset_ref (rhs);
6570 if (rhs == error_mark_node)
6571 return error_mark_node;
6572 rhstype = TREE_TYPE (rhs);
6573 coder = TREE_CODE (rhstype);
6576 if (TREE_CODE (TREE_TYPE (rhs)) == ARRAY_TYPE
6577 || TREE_CODE (TREE_TYPE (rhs)) == FUNCTION_TYPE
6578 || TREE_CODE (TREE_TYPE (rhs)) == METHOD_TYPE)
6579 rhs = default_conversion (rhs);
6580 else if (TREE_CODE (TREE_TYPE (rhs)) == REFERENCE_TYPE)
6581 rhs = convert_from_reference (rhs);
6583 rhstype = TREE_TYPE (rhs);
6584 coder = TREE_CODE (rhstype);
6586 /* This should no longer change types on us. */
6587 if (TREE_CODE (rhs) == CONST_DECL)
6588 rhs = DECL_INITIAL (rhs);
6589 else if (TREE_READONLY_DECL_P (rhs))
6590 rhs = decl_constant_value (rhs);
6592 if (type == rhstype)
6594 overflow_warning (rhs);
6598 if (coder == VOID_TYPE)
6600 error ("void value not ignored as it ought to be");
6601 return error_mark_node;
6603 /* Arithmetic types all interconvert. */
6604 if ((codel == INTEGER_TYPE || codel == REAL_TYPE || codel == BOOLEAN_TYPE
6605 || codel == COMPLEX_TYPE)
6606 && (coder == INTEGER_TYPE || coder == REAL_TYPE || coder == BOOLEAN_TYPE
6607 || coder == COMPLEX_TYPE))
6609 /* But we should warn if assigning REAL_TYPE to INTEGER_TYPE. */
6610 if (coder == REAL_TYPE && codel == INTEGER_TYPE)
6613 cp_warning ("`%T' used for argument %P of `%D'",
6614 rhstype, parmnum, fndecl);
6616 cp_warning ("%s to `%T' from `%T'", errtype, type, rhstype);
6618 /* And we should warn if assigning a negative value to
6619 an unsigned variable. */
6620 else if (TREE_UNSIGNED (type) && codel != BOOLEAN_TYPE)
6622 if (TREE_CODE (rhs) == INTEGER_CST
6623 && TREE_NEGATED_INT (rhs))
6626 cp_warning ("negative value `%E' passed as argument %P of `%D'",
6627 rhs, parmnum, fndecl);
6629 cp_warning ("%s of negative value `%E' to `%T'",
6630 errtype, rhs, type);
6632 overflow_warning (rhs);
6633 if (TREE_CONSTANT (rhs))
6637 return convert_and_check (type, rhs);
6639 /* Conversions involving enums. */
6640 else if ((codel == ENUMERAL_TYPE
6641 && (INTEGRAL_CODE_P (coder) || coder == REAL_TYPE))
6642 || (coder == ENUMERAL_TYPE
6643 && (INTEGRAL_CODE_P (codel) || codel == REAL_TYPE)))
6645 return ocp_convert (type, rhs, CONV_IMPLICIT, LOOKUP_NORMAL);
6647 /* Conversions among pointers */
6648 else if (codel == POINTER_TYPE
6649 && (coder == POINTER_TYPE
6650 || (coder == RECORD_TYPE
6651 && (IS_SIGNATURE_POINTER (rhstype)
6652 || IS_SIGNATURE_REFERENCE (rhstype)))))
6654 register tree ttl = TREE_TYPE (type);
6658 if (coder == RECORD_TYPE)
6660 rhs = build_optr_ref (rhs);
6661 rhstype = TREE_TYPE (rhs);
6663 ttr = TREE_TYPE (rhstype);
6665 /* If both pointers are of aggregate type, then we
6666 can give better error messages, and save some work
6668 if (TREE_CODE (ttl) == RECORD_TYPE && TREE_CODE (ttr) == RECORD_TYPE)
6672 if (TYPE_MAIN_VARIANT (ttl) == TYPE_MAIN_VARIANT (ttr)
6673 || type == class_star_type_node
6674 || rhstype == class_star_type_node)
6675 binfo = TYPE_BINFO (ttl);
6677 binfo = get_binfo (ttl, ttr, 1);
6679 if (binfo == error_mark_node)
6680 return error_mark_node;
6682 return error_not_base_type (ttl, ttr);
6684 if (! TYPE_READONLY (ttl) && TYPE_READONLY (ttr))
6687 cp_pedwarn ("passing `%T' as argument %P of `%D' discards const",
6688 rhstype, parmnum, fndecl);
6690 cp_pedwarn ("%s to `%T' from `%T' discards const",
6691 errtype, type, rhstype);
6693 if (! TYPE_VOLATILE (ttl) && TYPE_VOLATILE (ttr))
6696 cp_pedwarn ("passing `%T' as argument %P of `%D' discards volatile",
6697 rhstype, parmnum, fndecl);
6699 cp_pedwarn ("%s to `%T' from `%T' discards volatile",
6700 errtype, type, rhstype);
6704 /* Any non-function converts to a [const][volatile] void *
6705 and vice versa; otherwise, targets must be the same.
6706 Meanwhile, the lhs target must have all the qualifiers of the rhs. */
6707 else if (TYPE_MAIN_VARIANT (ttl) == void_type_node
6708 || TYPE_MAIN_VARIANT (ttr) == void_type_node
6709 || (ctt = comp_target_types (type, rhstype, 1))
6710 || (unsigned_type (TYPE_MAIN_VARIANT (ttl))
6711 == unsigned_type (TYPE_MAIN_VARIANT (ttr))))
6713 /* ARM $4.8, commentary on p39. */
6714 if (TYPE_MAIN_VARIANT (ttl) == void_type_node
6715 && TREE_CODE (ttr) == OFFSET_TYPE)
6717 cp_error ("no standard conversion from `%T' to `void *'", ttr);
6718 return error_mark_node;
6721 if (ctt < 0 && TYPE_MAIN_VARIANT (ttl) != TYPE_MAIN_VARIANT (ttr))
6722 cp_pedwarn ("converting `%T' to `%T' is a contravariance violation",
6725 if (TYPE_MAIN_VARIANT (ttl) != void_type_node
6726 && TYPE_MAIN_VARIANT (ttr) == void_type_node
6727 && ! null_ptr_cst_p (rhs))
6729 if (coder == RECORD_TYPE)
6730 cp_pedwarn ("implicit conversion of signature pointer to type `%T'",
6733 pedwarn ("ANSI C++ forbids implicit conversion from `void *' in %s",
6736 /* Const and volatile mean something different for function types,
6737 so the usual warnings are not appropriate. */
6738 else if ((TREE_CODE (ttr) != FUNCTION_TYPE && TREE_CODE (ttr) != METHOD_TYPE)
6739 || (TREE_CODE (ttl) != FUNCTION_TYPE && TREE_CODE (ttl) != METHOD_TYPE))
6741 if (TREE_CODE (ttl) == OFFSET_TYPE
6742 && binfo_member (TYPE_OFFSET_BASETYPE (ttr),
6743 CLASSTYPE_VBASECLASSES (TYPE_OFFSET_BASETYPE (ttl))))
6745 sorry ("%s between pointer to members converting across virtual baseclasses", errtype);
6746 return error_mark_node;
6748 else if (! TYPE_READONLY (ttl) && TYPE_READONLY (ttr))
6751 cp_pedwarn ("passing `%T' as argument %P of `%D' discards const",
6752 rhstype, parmnum, fndecl);
6754 cp_pedwarn ("%s to `%T' from `%T' discards const",
6755 errtype, type, rhstype);
6757 else if (! TYPE_VOLATILE (ttl) && TYPE_VOLATILE (ttr))
6760 cp_pedwarn ("passing `%T' as argument %P of `%D' discards volatile",
6761 rhstype, parmnum, fndecl);
6763 cp_pedwarn ("%s to `%T' from `%T' discards volatile",
6764 errtype, type, rhstype);
6766 else if (TREE_CODE (ttl) == TREE_CODE (ttr)
6767 && ! comp_target_types (type, rhstype, 1))
6770 cp_pedwarn ("passing `%T' as argument %P of `%D' changes signedness",
6771 rhstype, parmnum, fndecl);
6773 cp_pedwarn ("%s to `%T' from `%T' changes signedness",
6774 errtype, type, rhstype);
6780 int add_quals = 0, const_parity = 0, volatile_parity = 0;
6782 int unsigned_parity;
6785 /* This code is basically a duplicate of comp_ptr_ttypes_real. */
6786 for (; ; ttl = TREE_TYPE (ttl), ttr = TREE_TYPE (ttr))
6789 const_parity |= (TYPE_READONLY (ttl) < TYPE_READONLY (ttr));
6790 volatile_parity |= (TYPE_VOLATILE (ttl) < TYPE_VOLATILE (ttr));
6793 && (TYPE_READONLY (ttl) > TYPE_READONLY (ttr)
6794 || TYPE_VOLATILE (ttl) > TYPE_VOLATILE (ttr)))
6796 left_const &= TYPE_READONLY (ttl);
6798 if (TREE_CODE (ttl) != POINTER_TYPE
6799 || TREE_CODE (ttr) != POINTER_TYPE)
6802 unsigned_parity = TREE_UNSIGNED (ttl) - TREE_UNSIGNED (ttr);
6803 if (unsigned_parity)
6805 if (TREE_UNSIGNED (ttl))
6806 ttr = unsigned_type (ttr);
6808 ttl = unsigned_type (ttl);
6811 if (comp_target_types (ttl, ttr, nptrs) > 0)
6816 cp_pedwarn ("passing `%T' as argument %P of `%D' adds cv-quals without intervening `const'",
6817 rhstype, parmnum, fndecl);
6819 cp_pedwarn ("%s to `%T' from `%T' adds cv-quals without intervening `const'",
6820 errtype, type, rhstype);
6825 cp_pedwarn ("passing `%T' as argument %P of `%D' discards const",
6826 rhstype, parmnum, fndecl);
6828 cp_pedwarn ("%s to `%T' from `%T' discards const",
6829 errtype, type, rhstype);
6831 if (volatile_parity)
6834 cp_pedwarn ("passing `%T' as argument %P of `%D' discards volatile",
6835 rhstype, parmnum, fndecl);
6837 cp_pedwarn ("%s to `%T' from `%T' discards volatile",
6838 errtype, type, rhstype);
6840 if (unsigned_parity > 0)
6843 cp_pedwarn ("passing `%T' as argument %P of `%D' changes signed to unsigned",
6844 rhstype, parmnum, fndecl);
6846 cp_pedwarn ("%s to `%T' from `%T' changes signed to unsigned",
6847 errtype, type, rhstype);
6849 else if (unsigned_parity < 0)
6852 cp_pedwarn ("passing `%T' as argument %P of `%D' changes unsigned to signed",
6853 rhstype, parmnum, fndecl);
6855 cp_pedwarn ("%s to `%T' from `%T' changes unsigned to signed",
6856 errtype, type, rhstype);
6859 /* C++ is not so friendly about converting function and
6860 member function pointers as C. Emit warnings here. */
6861 if (TREE_CODE (ttl) == FUNCTION_TYPE
6862 || TREE_CODE (ttl) == METHOD_TYPE)
6863 if (! comptypes (ttl, ttr, 0))
6865 warning ("conflicting function types in %s:", errtype);
6866 cp_warning ("\t`%T' != `%T'", type, rhstype);
6872 cp_error ("passing `%T' as argument %P of `%D'",
6873 rhstype, parmnum, fndecl);
6875 cp_error ("%s to `%T' from `%T'", errtype, type, rhstype);
6876 return error_mark_node;
6879 return cp_convert (type, rhs);
6881 else if (codel == POINTER_TYPE && coder == INTEGER_TYPE)
6883 /* An explicit constant 0 can convert to a pointer,
6884 but not a 0 that results from casting or folding. */
6885 if (! (TREE_CODE (rhs) == INTEGER_CST && integer_zerop (rhs)))
6888 cp_pedwarn ("passing `%T' to argument %P of `%D' lacks a cast",
6889 rhstype, parmnum, fndecl);
6891 cp_pedwarn ("%s to `%T' from `%T' lacks a cast",
6892 errtype, type, rhstype);
6894 return cp_convert (type, rhs);
6896 else if (codel == INTEGER_TYPE
6897 && (coder == POINTER_TYPE
6898 || (coder == RECORD_TYPE
6899 && (IS_SIGNATURE_POINTER (rhstype)
6900 || TYPE_PTRMEMFUNC_FLAG (rhstype)
6901 || IS_SIGNATURE_REFERENCE (rhstype)))))
6904 cp_pedwarn ("passing `%T' to argument %P of `%D' lacks a cast",
6905 rhstype, parmnum, fndecl);
6907 cp_pedwarn ("%s to `%T' from `%T' lacks a cast",
6908 errtype, type, rhstype);
6909 return cp_convert (type, rhs);
6911 else if (codel == BOOLEAN_TYPE
6912 && (coder == POINTER_TYPE
6913 || (coder == RECORD_TYPE
6914 && (IS_SIGNATURE_POINTER (rhstype)
6915 || TYPE_PTRMEMFUNC_FLAG (rhstype)
6916 || IS_SIGNATURE_REFERENCE (rhstype)))))
6917 return cp_convert (type, rhs);
6920 else if (((coder == POINTER_TYPE
6921 && TREE_CODE (TREE_TYPE (rhstype)) == METHOD_TYPE)
6922 || integer_zerop (rhs)
6923 || TYPE_PTRMEMFUNC_P (rhstype))
6924 && TYPE_PTRMEMFUNC_P (type))
6926 tree ttl = TYPE_PTRMEMFUNC_FN_TYPE (type);
6927 tree ttr = (TYPE_PTRMEMFUNC_P (rhstype)
6928 ? TYPE_PTRMEMFUNC_FN_TYPE (rhstype)
6930 int ctt = (TREE_CODE (rhstype) == INTEGER_TYPE ? 1
6931 : comp_target_types (ttl, ttr, 1));
6934 cp_pedwarn ("converting `%T' to `%T' is a contravariance violation",
6937 cp_error ("%s to `%T' from `%T'", errtype, ttl, ttr);
6939 /* compatible pointer to member functions. */
6940 return build_ptrmemfunc (ttl, rhs, 0);
6942 else if (codel == ERROR_MARK || coder == ERROR_MARK)
6943 return error_mark_node;
6945 /* This should no longer happen. References are initialized via
6946 `convert_for_initialization'. They should otherwise be
6947 bashed before coming here. */
6948 else if (codel == REFERENCE_TYPE)
6949 my_friendly_abort (317);
6950 else if (TYPE_MAIN_VARIANT (type) == TYPE_MAIN_VARIANT (TREE_TYPE (rhs)))
6952 tree nrhs = build1 (NOP_EXPR, type, rhs);
6953 TREE_CONSTANT (nrhs) = TREE_CONSTANT (rhs);
6956 else if (TYPE_HAS_CONSTRUCTOR (type) || IS_AGGR_TYPE (TREE_TYPE (rhs)))
6957 return cp_convert (type, rhs);
6958 /* Handle anachronistic conversions from (::*)() to cv void* or (*)(). */
6959 else if (TREE_CODE (type) == POINTER_TYPE
6960 && (TREE_CODE (TREE_TYPE (type)) == FUNCTION_TYPE
6961 || TYPE_MAIN_VARIANT (TREE_TYPE (type)) == void_type_node)
6963 && TYPE_PTRMEMFUNC_P (TREE_TYPE (rhs)))
6964 return cp_convert (type, rhs);
6966 cp_error ("%s to `%T' from `%T'", errtype, type, rhstype);
6967 return error_mark_node;
6970 /* Convert RHS to be of type TYPE. If EXP is non-zero,
6971 it is the target of the initialization.
6972 ERRTYPE is a string to use in error messages.
6974 Two major differences between the behavior of
6975 `convert_for_assignment' and `convert_for_initialization'
6976 are that references are bashed in the former, while
6977 copied in the latter, and aggregates are assigned in
6978 the former (operator=) while initialized in the
6981 If using constructor make sure no conversion operator exists, if one does
6982 exist, an ambiguity exists.
6984 If flags doesn't include LOOKUP_COMPLAIN, don't complain about anything. */
6987 convert_for_initialization (exp, type, rhs, flags, errtype, fndecl, parmnum)
6988 tree exp, type, rhs;
6994 register enum tree_code codel = TREE_CODE (type);
6995 register tree rhstype;
6996 register enum tree_code coder;
6998 /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
6999 Strip such NOP_EXPRs, since RHS is used in non-lvalue context. */
7000 if (TREE_CODE (rhs) == NOP_EXPR
7001 && TREE_TYPE (rhs) == TREE_TYPE (TREE_OPERAND (rhs, 0))
7002 && codel != REFERENCE_TYPE)
7003 rhs = TREE_OPERAND (rhs, 0);
7005 if (rhs == error_mark_node
7006 || (TREE_CODE (rhs) == TREE_LIST && TREE_VALUE (rhs) == error_mark_node))
7007 return error_mark_node;
7009 if (TREE_CODE (TREE_TYPE (rhs)) == OFFSET_TYPE)
7011 rhs = resolve_offset_ref (rhs);
7012 if (rhs == error_mark_node)
7013 return error_mark_node;
7016 if (TREE_CODE (TREE_TYPE (rhs)) == REFERENCE_TYPE)
7017 rhs = convert_from_reference (rhs);
7019 if ((TREE_CODE (TREE_TYPE (rhs)) == ARRAY_TYPE
7020 && TREE_CODE (type) != ARRAY_TYPE
7021 && (TREE_CODE (type) != REFERENCE_TYPE
7022 || TREE_CODE (TREE_TYPE (type)) != ARRAY_TYPE))
7023 || TREE_CODE (TREE_TYPE (rhs)) == FUNCTION_TYPE
7024 || TREE_CODE (TREE_TYPE (rhs)) == METHOD_TYPE)
7025 rhs = default_conversion (rhs);
7027 rhstype = TREE_TYPE (rhs);
7028 coder = TREE_CODE (rhstype);
7030 if (coder == UNKNOWN_TYPE)
7032 rhs = instantiate_type (type, rhs, 1);
7033 rhstype = TREE_TYPE (rhs);
7034 coder = TREE_CODE (rhstype);
7037 if (coder == ERROR_MARK)
7038 return error_mark_node;
7040 /* We accept references to incomplete types, so we can
7041 return here before checking if RHS is of complete type. */
7043 if (codel == REFERENCE_TYPE)
7045 /* This should eventually happen in convert_arguments. */
7046 extern int warningcount, errorcount;
7047 int savew = 0, savee = 0;
7050 savew = warningcount, savee = errorcount;
7051 rhs = convert_to_reference (type, rhs, CONV_IMPLICIT, flags,
7052 exp ? exp : error_mark_node);
7055 if (warningcount > savew)
7056 cp_warning_at ("in passing argument %P of `%+D'", parmnum, fndecl);
7057 else if (errorcount > savee)
7058 cp_error_at ("in passing argument %P of `%+D'", parmnum, fndecl);
7063 rhs = require_complete_type (rhs);
7064 if (rhs == error_mark_node)
7065 return error_mark_node;
7067 if (exp != 0) exp = require_complete_type (exp);
7068 if (exp == error_mark_node)
7069 return error_mark_node;
7071 if (TREE_CODE (rhstype) == REFERENCE_TYPE)
7072 rhstype = TREE_TYPE (rhstype);
7074 type = complete_type (type);
7076 if (TYPE_LANG_SPECIFIC (type)
7077 && (IS_SIGNATURE_POINTER (type) || IS_SIGNATURE_REFERENCE (type)))
7078 return build_signature_pointer_constructor (type, rhs);
7080 if (IS_AGGR_TYPE (type))
7081 return ocp_convert (type, rhs, CONV_IMPLICIT|CONV_FORCE_TEMP, flags);
7083 if (type == TREE_TYPE (rhs))
7085 if (TREE_READONLY_DECL_P (rhs))
7086 rhs = decl_constant_value (rhs);
7090 return convert_for_assignment (type, rhs, errtype, fndecl, parmnum);
7093 /* Expand an ASM statement with operands, handling output operands
7094 that are not variables or INDIRECT_REFS by transforming such
7095 cases into cases that expand_asm_operands can handle.
7097 Arguments are same as for expand_asm_operands.
7099 We don't do default conversions on all inputs, because it can screw
7100 up operands that are expected to be in memory. */
7103 c_expand_asm_operands (string, outputs, inputs, clobbers, vol, filename, line)
7104 tree string, outputs, inputs, clobbers;
7109 int noutputs = list_length (outputs);
7111 /* o[I] is the place that output number I should be written. */
7112 register tree *o = (tree *) alloca (noutputs * sizeof (tree));
7115 /* Record the contents of OUTPUTS before it is modified. */
7116 for (i = 0, tail = outputs; tail; tail = TREE_CHAIN (tail), i++)
7117 o[i] = TREE_VALUE (tail);
7119 /* Generate the ASM_OPERANDS insn;
7120 store into the TREE_VALUEs of OUTPUTS some trees for
7121 where the values were actually stored. */
7122 expand_asm_operands (string, outputs, inputs, clobbers, vol, filename, line);
7124 /* Copy all the intermediate outputs into the specified outputs. */
7125 for (i = 0, tail = outputs; tail; tail = TREE_CHAIN (tail), i++)
7127 if (o[i] != TREE_VALUE (tail))
7129 expand_expr (build_modify_expr (o[i], NOP_EXPR, TREE_VALUE (tail)),
7130 const0_rtx, VOIDmode, EXPAND_NORMAL);
7133 /* Detect modification of read-only values.
7134 (Otherwise done by build_modify_expr.) */
7137 tree type = TREE_TYPE (o[i]);
7138 if (TYPE_READONLY (type)
7139 || ((TREE_CODE (type) == RECORD_TYPE
7140 || TREE_CODE (type) == UNION_TYPE)
7141 && C_TYPE_FIELDS_READONLY (type)))
7142 readonly_error (o[i], "modification by `asm'", 1);
7146 /* Those MODIFY_EXPRs could do autoincrements. */
7150 /* Expand a C `return' statement.
7151 RETVAL is the expression for what to return,
7152 or a null pointer for `return;' with no value.
7154 C++: upon seeing a `return', we must call destructors on all
7155 variables in scope which had constructors called on them.
7156 This means that if in a destructor, the base class destructors
7157 must be called before returning.
7159 The RETURN statement in C++ has initialization semantics. */
7162 c_expand_return (retval)
7165 extern struct nesting *cond_stack, *loop_stack, *case_stack;
7166 extern tree dtor_label, ctor_label;
7167 tree result = DECL_RESULT (current_function_decl);
7168 tree valtype = TREE_TYPE (result);
7170 if (TREE_THIS_VOLATILE (current_function_decl))
7171 warning ("function declared `noreturn' has a `return' statement");
7173 if (retval == error_mark_node)
7175 current_function_returns_null = 1;
7179 if (processing_template_decl)
7181 add_tree (build_min_nt (RETURN_STMT, retval));
7185 if (retval == NULL_TREE)
7187 /* A non-named return value does not count. */
7189 /* Can't just return from a destructor. */
7192 expand_goto (dtor_label);
7196 if (DECL_CONSTRUCTOR_P (current_function_decl))
7197 retval = current_class_ptr;
7198 else if (DECL_NAME (result) != NULL_TREE
7199 && TREE_CODE (valtype) != VOID_TYPE)
7203 current_function_returns_null = 1;
7205 if (valtype != NULL_TREE && TREE_CODE (valtype) != VOID_TYPE)
7207 if (DECL_NAME (DECL_RESULT (current_function_decl)) == NULL_TREE)
7209 pedwarn ("`return' with no value, in function returning non-void");
7210 /* Clear this, so finish_function won't say that we
7211 reach the end of a non-void function (which we don't,
7212 we gave a return!). */
7213 current_function_returns_null = 0;
7217 expand_null_return ();
7221 else if (DECL_CONSTRUCTOR_P (current_function_decl)
7222 && retval != current_class_ptr)
7224 if (flag_this_is_variable)
7225 error ("return from a constructor: use `this = ...' instead");
7227 error ("return from a constructor");
7228 retval = current_class_ptr;
7231 /* Effective C++ rule 15. See also start_function. */
7233 && DECL_NAME (current_function_decl) == ansi_opname[(int) MODIFY_EXPR]
7234 && retval != current_class_ref)
7235 cp_warning ("`operator=' should return a reference to `*this'");
7237 if (valtype == NULL_TREE || TREE_CODE (valtype) == VOID_TYPE)
7239 current_function_returns_null = 1;
7240 if (TREE_CODE (TREE_TYPE (retval)) != VOID_TYPE)
7241 pedwarn ("`return' with a value, in function returning void");
7242 expand_return (retval);
7246 /* Now deal with possible C++ hair:
7247 (1) Compute the return value.
7248 (2) If there are aggregate values with destructors which
7249 must be cleaned up, clean them (taking care
7250 not to clobber the return value).
7251 (3) If an X(X&) constructor is defined, the return
7252 value must be returned via that. */
7254 if (retval == result
7255 || DECL_CONSTRUCTOR_P (current_function_decl))
7256 /* It's already done for us. */;
7257 else if (TREE_TYPE (retval) == void_type_node)
7259 pedwarn ("return of void value in function returning non-void");
7260 expand_expr_stmt (retval);
7265 retval = convert_for_initialization
7266 (NULL_TREE, valtype, retval, LOOKUP_NORMAL|LOOKUP_ONLYCONVERTING,
7267 "return", NULL_TREE, 0);
7269 if (retval == error_mark_node)
7271 /* Avoid warning about control reaching end of function. */
7272 expand_null_return ();
7276 /* We can't initialize a register from a AGGR_INIT_EXPR. */
7277 else if (! current_function_returns_struct
7278 && TREE_CODE (retval) == TARGET_EXPR
7279 && TREE_CODE (TREE_OPERAND (retval, 1)) == AGGR_INIT_EXPR)
7280 retval = build (COMPOUND_EXPR, TREE_TYPE (retval), retval,
7281 TREE_OPERAND (retval, 0));
7283 /* Add some useful error checking for C++. */
7284 else if (TREE_CODE (valtype) == REFERENCE_TYPE)
7286 tree whats_returned;
7288 /* Sort through common things to see what it is
7289 we are returning. */
7290 whats_returned = retval;
7291 if (TREE_CODE (whats_returned) == COMPOUND_EXPR)
7293 whats_returned = TREE_OPERAND (whats_returned, 1);
7294 if (TREE_CODE (whats_returned) == ADDR_EXPR)
7295 whats_returned = TREE_OPERAND (whats_returned, 0);
7297 while (TREE_CODE (whats_returned) == CONVERT_EXPR
7298 || TREE_CODE (whats_returned) == NOP_EXPR)
7299 whats_returned = TREE_OPERAND (whats_returned, 0);
7300 if (TREE_CODE (whats_returned) == ADDR_EXPR)
7302 whats_returned = TREE_OPERAND (whats_returned, 0);
7303 while (TREE_CODE (whats_returned) == AGGR_INIT_EXPR
7304 || TREE_CODE (whats_returned) == TARGET_EXPR)
7306 /* Get the target. */
7307 whats_returned = TREE_OPERAND (whats_returned, 0);
7308 warning ("returning reference to temporary");
7312 if (TREE_CODE (whats_returned) == VAR_DECL && DECL_NAME (whats_returned))
7314 if (TEMP_NAME_P (DECL_NAME (whats_returned)))
7315 warning ("reference to non-lvalue returned");
7316 else if (TREE_CODE (TREE_TYPE (whats_returned)) != REFERENCE_TYPE
7317 && ! TREE_STATIC (whats_returned)
7318 && IDENTIFIER_LOCAL_VALUE (DECL_NAME (whats_returned))
7319 && !TREE_PUBLIC (whats_returned))
7320 cp_warning_at ("reference to local variable `%D' returned", whats_returned);
7323 else if (TREE_CODE (retval) == ADDR_EXPR)
7325 tree whats_returned = TREE_OPERAND (retval, 0);
7327 if (TREE_CODE (whats_returned) == VAR_DECL
7328 && DECL_NAME (whats_returned)
7329 && IDENTIFIER_LOCAL_VALUE (DECL_NAME (whats_returned))
7330 && !TREE_STATIC (whats_returned)
7331 && !TREE_PUBLIC (whats_returned))
7332 cp_warning_at ("address of local variable `%D' returned", whats_returned);
7336 if (retval != NULL_TREE
7337 && TREE_CODE_CLASS (TREE_CODE (retval)) == 'd'
7338 && cond_stack == 0 && loop_stack == 0 && case_stack == 0)
7339 current_function_return_value = retval;
7341 if (ctor_label && TREE_CODE (ctor_label) != ERROR_MARK)
7343 /* Here RETVAL is CURRENT_CLASS_PTR, so there's nothing to do. */
7344 expand_goto (ctor_label);
7347 if (retval && retval != result)
7349 result = build (INIT_EXPR, TREE_TYPE (result), result, retval);
7350 TREE_SIDE_EFFECTS (result) = 1;
7353 expand_start_target_temps ();
7355 expand_return (result);
7357 expand_end_target_temps ();
7359 current_function_returns_value = 1;
7362 /* Start a C switch statement, testing expression EXP.
7363 Return EXP if it is valid, an error node otherwise. */
7366 c_expand_start_case (exp)
7370 register enum tree_code code;
7372 /* Convert from references, etc. */
7373 exp = default_conversion (exp);
7374 type = TREE_TYPE (exp);
7375 code = TREE_CODE (type);
7377 if (IS_AGGR_TYPE_CODE (code))
7378 exp = build_type_conversion (CONVERT_EXPR, integer_type_node, exp, 1);
7380 if (exp == NULL_TREE)
7382 error ("switch quantity not an integer");
7383 exp = error_mark_node;
7385 type = TREE_TYPE (exp);
7386 code = TREE_CODE (type);
7388 if (code != INTEGER_TYPE && code != ENUMERAL_TYPE && code != ERROR_MARK)
7390 error ("switch quantity not an integer");
7391 exp = error_mark_node;
7397 exp = default_conversion (exp);
7398 type = TREE_TYPE (exp);
7399 idx = get_unwidened (exp, 0);
7400 /* We can't strip a conversion from a signed type to an unsigned,
7401 because if we did, int_fits_type_p would do the wrong thing
7402 when checking case values for being in range,
7403 and it's too hard to do the right thing. */
7404 if (TREE_UNSIGNED (TREE_TYPE (exp))
7405 == TREE_UNSIGNED (TREE_TYPE (idx)))
7410 (1, fold (build1 (CLEANUP_POINT_EXPR, TREE_TYPE (exp), exp)),
7411 type, "switch statement");
7416 /* Returns non-zero if the pointer-type FROM can be converted to the
7417 pointer-type TO via a qualification conversion. If CONSTP is -1,
7418 then we return non-zero if the pointers are similar, and the
7419 cv-qualification signature of FROM is a proper subset of that of TO.
7421 If CONSTP is positive, then all outer pointers have been
7425 comp_ptr_ttypes_real (to, from, constp)
7429 int to_more_cv_qualified = 0;
7431 for (; ; to = TREE_TYPE (to), from = TREE_TYPE (from))
7433 if (TREE_CODE (to) != TREE_CODE (from))
7436 if (TREE_CODE (from) == OFFSET_TYPE
7437 && comptypes (TYPE_OFFSET_BASETYPE (from),
7438 TYPE_OFFSET_BASETYPE (to), 1))
7441 /* Const and volatile mean something different for function types,
7442 so the usual checks are not appropriate. */
7443 if (TREE_CODE (to) != FUNCTION_TYPE && TREE_CODE (to) != METHOD_TYPE)
7445 if (TYPE_READONLY (from) > TYPE_READONLY (to)
7446 || TYPE_VOLATILE (from) > TYPE_VOLATILE (to))
7449 if (TYPE_READONLY (to) > TYPE_READONLY (from)
7450 || TYPE_VOLATILE (to) > TYPE_VOLATILE (from))
7455 ++to_more_cv_qualified;
7459 constp &= TYPE_READONLY (to);
7462 if (TREE_CODE (to) != POINTER_TYPE)
7464 comptypes (TYPE_MAIN_VARIANT (to), TYPE_MAIN_VARIANT (from), 1)
7465 && (constp >= 0 || to_more_cv_qualified);
7469 /* When comparing, say, char ** to char const **, this function takes the
7470 'char *' and 'char const *'. Do not pass non-pointer types to this
7474 comp_ptr_ttypes (to, from)
7477 return comp_ptr_ttypes_real (to, from, 1);
7480 /* Returns 1 if to and from are (possibly multi-level) pointers to the same
7481 type or inheritance-related types, regardless of cv-quals. */
7484 ptr_reasonably_similar (to, from)
7487 for (; ; to = TREE_TYPE (to), from = TREE_TYPE (from))
7489 if (TREE_CODE (to) != TREE_CODE (from))
7492 if (TREE_CODE (from) == OFFSET_TYPE
7493 && comptypes (TYPE_OFFSET_BASETYPE (to),
7494 TYPE_OFFSET_BASETYPE (from), -1))
7497 if (TREE_CODE (to) != POINTER_TYPE)
7499 (TYPE_MAIN_VARIANT (to), TYPE_MAIN_VARIANT (from), -1);
7503 /* Like comp_ptr_ttypes, for const_cast. */
7506 comp_ptr_ttypes_const (to, from)
7509 for (; ; to = TREE_TYPE (to), from = TREE_TYPE (from))
7511 if (TREE_CODE (to) != TREE_CODE (from))
7514 if (TREE_CODE (from) == OFFSET_TYPE
7515 && comptypes (TYPE_OFFSET_BASETYPE (from),
7516 TYPE_OFFSET_BASETYPE (to), 1))
7519 if (TREE_CODE (to) != POINTER_TYPE)
7520 return comptypes (TYPE_MAIN_VARIANT (to), TYPE_MAIN_VARIANT (from), 1);
7524 /* Like comp_ptr_ttypes, for reinterpret_cast. */
7527 comp_ptr_ttypes_reinterpret (to, from)
7532 for (; ; to = TREE_TYPE (to), from = TREE_TYPE (from))
7534 if (TREE_CODE (from) == OFFSET_TYPE)
7535 from = TREE_TYPE (from);
7536 if (TREE_CODE (to) == OFFSET_TYPE)
7537 to = TREE_TYPE (to);
7539 if (TREE_CODE (to) != TREE_CODE (from))
7542 /* Const and volatile mean something different for function types,
7543 so the usual checks are not appropriate. */
7544 if (TREE_CODE (to) != FUNCTION_TYPE && TREE_CODE (to) != METHOD_TYPE)
7546 if (TYPE_READONLY (from) > TYPE_READONLY (to)
7547 || TYPE_VOLATILE (from) > TYPE_VOLATILE (to))
7551 && (TYPE_READONLY (to) > TYPE_READONLY (from)
7552 || TYPE_VOLATILE (to) > TYPE_READONLY (from)))
7554 constp &= TYPE_READONLY (to);
7557 if (TREE_CODE (to) != POINTER_TYPE)