1 /* Build expressions with type checking for C++ compiler.
2 Copyright (C) 1987, 88, 89, 92, 93, 94, 95, 1996 Free Software Foundation, Inc.
3 Hacked by Michael Tiemann (tiemann@cygnus.com)
5 This file is part of GNU CC.
7 GNU CC is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2, or (at your option)
12 GNU CC is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with GNU CC; see the file COPYING. If not, write to
19 the Free Software Foundation, 59 Temple Place - Suite 330,
20 Boston, MA 02111-1307, USA. */
23 /* This file is part of the C++ front end.
24 It contains routines to build C++ expressions given their operands,
25 including computing the types of the result, C and C++ specific error
26 checks, and some optimization.
28 There are also routines to build RETURN_STMT nodes and CASE_STMT nodes,
29 and to process initializations in declarations (since they work
30 like a strange sort of assignment). */
33 extern void warning ();
43 int mark_addressable PROTO((tree));
44 static tree convert_for_assignment PROTO((tree, tree, char*, tree, int));
45 /* static */ tree convert_for_initialization PROTO((tree, tree, tree, int, char*, tree, int));
46 extern tree shorten_compare ();
47 extern void binary_op_error ();
48 static tree pointer_int_sum PROTO((enum tree_code, register tree, register tree));
49 static tree pointer_diff PROTO((register tree, register tree));
51 static tree convert_sequence ();
53 /* static */ tree unary_complex_lvalue PROTO((enum tree_code, tree));
54 static tree get_delta_difference PROTO((tree, tree, int));
56 extern rtx original_result_rtx;
57 extern int warn_synth;
59 /* Return the target type of TYPE, which meas return T for:
60 T*, T&, T[], T (...), and otherwise, just T. */
66 if (TREE_CODE (type) == REFERENCE_TYPE)
67 type = TREE_TYPE (type);
68 while (TREE_CODE (type) == POINTER_TYPE
69 || TREE_CODE (type) == ARRAY_TYPE
70 || TREE_CODE (type) == FUNCTION_TYPE
71 || TREE_CODE (type) == METHOD_TYPE
72 || TREE_CODE (type) == OFFSET_TYPE)
73 type = TREE_TYPE (type);
77 /* Do `exp = require_complete_type (exp);' to make sure exp
78 does not have an incomplete type. (That includes void types.) */
81 require_complete_type (value)
86 if (current_template_parms)
89 type = TREE_TYPE (value);
91 /* First, detect a valid value with a complete type. */
92 if (TYPE_SIZE (type) != 0
93 && type != void_type_node
94 && ! (TYPE_LANG_SPECIFIC (type)
95 && (IS_SIGNATURE_POINTER (type) || IS_SIGNATURE_REFERENCE (type))
96 && TYPE_SIZE (SIGNATURE_TYPE (type)) == 0))
99 /* If we see X::Y, we build an OFFSET_TYPE which has
100 not been laid out. Try to avoid an error by interpreting
101 it as this->X::Y, if reasonable. */
102 if (TREE_CODE (value) == OFFSET_REF
104 && TREE_OPERAND (value, 0) == C_C_D)
106 tree base, member = TREE_OPERAND (value, 1);
107 tree basetype = TYPE_OFFSET_BASETYPE (type);
108 my_friendly_assert (TREE_CODE (member) == FIELD_DECL, 305);
109 base = convert_pointer_to (basetype, current_class_decl);
110 value = build (COMPONENT_REF, TREE_TYPE (member),
111 build_indirect_ref (base, NULL_PTR), member);
112 return require_complete_type (value);
115 if (IS_AGGR_TYPE (type) && CLASSTYPE_TEMPLATE_INSTANTIATION (type))
117 instantiate_class_template (TYPE_MAIN_VARIANT (type));
118 if (TYPE_SIZE (type) != 0)
122 incomplete_type_error (value, type);
123 return error_mark_node;
130 if (TYPE_SIZE (type) != NULL_TREE)
132 else if (TREE_CODE (type) == ARRAY_TYPE)
134 tree t = complete_type (TREE_TYPE (type));
135 if (TYPE_SIZE (t) != NULL_TREE)
136 type = build_cplus_array_type (t, TYPE_DOMAIN (type));
138 else if (IS_AGGR_TYPE (type) && CLASSTYPE_TEMPLATE_INSTANTIATION (type))
139 instantiate_class_template (TYPE_MAIN_VARIANT (type));
144 /* Return truthvalue of whether type of EXP is instantiated. */
149 return (TREE_CODE (exp) == TREE_LIST
150 || TREE_TYPE (exp) == unknown_type_node
151 || (TREE_CODE (TREE_TYPE (exp)) == OFFSET_TYPE
152 && TREE_TYPE (TREE_TYPE (exp)) == unknown_type_node));
155 /* Return truthvalue of whether T is function (or pfn) type. */
160 return (TREE_CODE (t) == FUNCTION_TYPE || TREE_CODE (t) == METHOD_TYPE
161 || (TREE_CODE (t) == POINTER_TYPE
162 && (TREE_CODE (TREE_TYPE (t)) == FUNCTION_TYPE
163 || TREE_CODE (TREE_TYPE (t)) == METHOD_TYPE)));
166 /* Do `exp = require_instantiated_type (type, exp);' to make sure EXP
167 does not have an uninstantiated type.
168 TYPE is type to instantiate with, if uninstantiated. */
170 require_instantiated_type (type, exp, errval)
171 tree type, exp, errval;
173 if (TREE_TYPE (exp) == NULL_TREE)
175 error ("argument list may not have an initializer list");
179 if (TREE_TYPE (exp) == unknown_type_node
180 || (TREE_CODE (TREE_TYPE (exp)) == OFFSET_TYPE
181 && TREE_TYPE (TREE_TYPE (exp)) == unknown_type_node))
183 exp = instantiate_type (type, exp, 1);
184 if (TREE_TYPE (exp) == error_mark_node)
190 /* Return a variant of TYPE which has all the type qualifiers of LIKE
191 as well as those of TYPE. */
194 qualify_type (type, like)
197 int constflag = TYPE_READONLY (type) || TYPE_READONLY (like);
198 int volflag = TYPE_VOLATILE (type) || TYPE_VOLATILE (like);
199 /* @@ Must do member pointers here. */
200 return cp_build_type_variant (type, constflag, volflag);
203 /* Return the common type of two parameter lists.
204 We assume that comptypes has already been done and returned 1;
205 if that isn't so, this may crash.
207 As an optimization, free the space we allocate if the parameter
208 lists are already common. */
214 tree oldargs = p1, newargs, n;
217 char *first_obj = (char *) oballoc (0);
219 len = list_length (p1);
220 newargs = tree_last (p1);
222 if (newargs == void_list_node)
231 newargs = tree_cons (NULL_TREE, NULL_TREE, newargs);
236 p1 = TREE_CHAIN (p1), p2 = TREE_CHAIN (p2), n = TREE_CHAIN (n), i++)
238 if (TREE_PURPOSE (p1) && !TREE_PURPOSE (p2))
240 TREE_PURPOSE (n) = TREE_PURPOSE (p1);
243 else if (! TREE_PURPOSE (p1))
245 if (TREE_PURPOSE (p2))
247 TREE_PURPOSE (n) = TREE_PURPOSE (p2);
253 if (1 != simple_cst_equal (TREE_PURPOSE (p1), TREE_PURPOSE (p2)))
255 TREE_PURPOSE (n) = TREE_PURPOSE (p2);
257 if (TREE_VALUE (p1) != TREE_VALUE (p2))
260 TREE_VALUE (n) = common_type (TREE_VALUE (p1), TREE_VALUE (p2));
263 TREE_VALUE (n) = TREE_VALUE (p1);
274 /* Return the common type of two types.
275 We assume that comptypes has already been done and returned 1;
276 if that isn't so, this may crash.
278 This is the type for the result of most arithmetic operations
279 if the operands have the given two types.
281 We do not deal with enumeral types here because they have already been
282 converted to integer types. */
288 register enum tree_code code1;
289 register enum tree_code code2;
292 /* Save time if the two types are the same. */
294 if (t1 == t2) return t1;
296 /* If one type is nonsense, use the other. */
297 if (t1 == error_mark_node)
299 if (t2 == error_mark_node)
302 /* Merge the attributes */
304 { register tree a1, a2;
305 a1 = TYPE_ATTRIBUTES (t1);
306 a2 = TYPE_ATTRIBUTES (t2);
308 /* Either one unset? Take the set one. */
310 if (!(attributes = a1))
313 /* One that completely contains the other? Take it. */
315 else if (a2 && !attribute_list_contained (a1, a2))
316 if (attribute_list_contained (a2, a1))
320 /* Pick the longest list, and hang on the other list. */
321 /* ??? For the moment we punt on the issue of attrs with args. */
323 if (list_length (a1) < list_length (a2))
324 attributes = a2, a2 = a1;
326 for (; a2; a2 = TREE_CHAIN (a2))
327 if (lookup_attribute (IDENTIFIER_POINTER (TREE_PURPOSE (a2)),
328 attributes) == NULL_TREE)
331 TREE_CHAIN (a1) = attributes;
337 /* Treat an enum type as the unsigned integer type of the same width. */
339 if (TREE_CODE (t1) == ENUMERAL_TYPE)
340 t1 = type_for_size (TYPE_PRECISION (t1), 1);
341 if (TREE_CODE (t2) == ENUMERAL_TYPE)
342 t2 = type_for_size (TYPE_PRECISION (t2), 1);
344 if (TYPE_PTRMEMFUNC_P (t1))
345 t1 = TYPE_PTRMEMFUNC_FN_TYPE (t1);
346 if (TYPE_PTRMEMFUNC_P (t2))
347 t2 = TYPE_PTRMEMFUNC_FN_TYPE (t2);
349 code1 = TREE_CODE (t1);
350 code2 = TREE_CODE (t2);
356 /* If only one is real, use it as the result. */
358 if (code1 == REAL_TYPE && code2 != REAL_TYPE)
359 return build_type_attribute_variant (t1, attributes);
361 if (code2 == REAL_TYPE && code1 != REAL_TYPE)
362 return build_type_attribute_variant (t2, attributes);
364 /* Both real or both integers; use the one with greater precision. */
366 if (TYPE_PRECISION (t1) > TYPE_PRECISION (t2))
367 return build_type_attribute_variant (t1, attributes);
368 else if (TYPE_PRECISION (t2) > TYPE_PRECISION (t1))
369 return build_type_attribute_variant (t2, attributes);
371 /* Same precision. Prefer longs to ints even when same size. */
373 if (TYPE_MAIN_VARIANT (t1) == long_unsigned_type_node
374 || TYPE_MAIN_VARIANT (t2) == long_unsigned_type_node)
375 return build_type_attribute_variant (long_unsigned_type_node,
378 if (TYPE_MAIN_VARIANT (t1) == long_integer_type_node
379 || TYPE_MAIN_VARIANT (t2) == long_integer_type_node)
381 /* But preserve unsignedness from the other type,
382 since long cannot hold all the values of an unsigned int. */
383 if (TREE_UNSIGNED (t1) || TREE_UNSIGNED (t2))
384 t1 = long_unsigned_type_node;
386 t1 = long_integer_type_node;
387 return build_type_attribute_variant (t1, attributes);
390 if (TYPE_MAIN_VARIANT (t1) == long_double_type_node
391 || TYPE_MAIN_VARIANT (t2) == long_double_type_node)
392 return build_type_attribute_variant (long_double_type_node,
395 /* Otherwise prefer the unsigned one. */
397 if (TREE_UNSIGNED (t1))
398 return build_type_attribute_variant (t1, attributes);
400 return build_type_attribute_variant (t2, attributes);
404 /* For two pointers, do this recursively on the target type,
405 and combine the qualifiers of the two types' targets. */
406 /* This code was turned off; I don't know why.
407 But ANSI C++ specifies doing this with the qualifiers.
408 So I turned it on again. */
410 tree tt1 = TYPE_MAIN_VARIANT (TREE_TYPE (t1));
411 tree tt2 = TYPE_MAIN_VARIANT (TREE_TYPE (t2));
413 = TYPE_READONLY (TREE_TYPE (t1)) || TYPE_READONLY (TREE_TYPE (t2));
415 = TYPE_VOLATILE (TREE_TYPE (t1)) || TYPE_VOLATILE (TREE_TYPE (t2));
420 else if (tt1 == void_type_node || tt2 == void_type_node)
421 target = void_type_node;
423 target = common_type (tt1, tt2);
425 target = cp_build_type_variant (target, constp, volatilep);
426 if (code1 == POINTER_TYPE)
427 t1 = build_pointer_type (target);
429 t1 = build_reference_type (target);
430 t1 = build_type_attribute_variant (t1, attributes);
432 if (TREE_CODE (target) == METHOD_TYPE)
433 t1 = build_ptrmemfunc_type (t1);
441 = TYPE_READONLY (t1) || TYPE_READONLY (t2);
443 = TYPE_VOLATILE (t1) || TYPE_VOLATILE (t2);
444 tree elt = common_type (TREE_TYPE (t1), TREE_TYPE (t2));
445 /* Save space: see if the result is identical to one of the args. */
446 if (elt == TREE_TYPE (t1) && TYPE_DOMAIN (t1))
447 return build_type_attribute_variant (t1, attributes);
448 if (elt == TREE_TYPE (t2) && TYPE_DOMAIN (t2))
449 return build_type_attribute_variant (t2, attributes);
450 /* Merge the element types, and have a size if either arg has one. */
451 t1 = build_cplus_array_type (TYPE_MAIN_VARIANT (elt), TYPE_DOMAIN (TYPE_DOMAIN (t1) ? t1 : t2));
452 if (constp || volatilep)
453 t1 = cp_build_type_variant (t1, constp, volatilep);
454 return build_type_attribute_variant (t1, attributes);
458 /* Function types: prefer the one that specified arg types.
459 If both do, merge the arg types. Also merge the return types. */
461 tree valtype = common_type (TREE_TYPE (t1), TREE_TYPE (t2));
462 tree p1 = TYPE_ARG_TYPES (t1);
463 tree p2 = TYPE_ARG_TYPES (t2);
466 /* Save space: see if the result is identical to one of the args. */
467 if (valtype == TREE_TYPE (t1) && ! p2)
468 return build_type_attribute_variant (t1, attributes);
469 if (valtype == TREE_TYPE (t2) && ! p1)
470 return build_type_attribute_variant (t2, attributes);
472 /* Simple way if one arg fails to specify argument types. */
473 if (p1 == NULL_TREE || TREE_VALUE (p1) == void_type_node)
475 rval = build_function_type (valtype, p2);
476 if ((raises = TYPE_RAISES_EXCEPTIONS (t2)))
477 rval = build_exception_variant (rval, raises);
478 return build_type_attribute_variant (rval, attributes);
480 raises = TYPE_RAISES_EXCEPTIONS (t1);
481 if (p2 == NULL_TREE || TREE_VALUE (p2) == void_type_node)
483 rval = build_function_type (valtype, p1);
485 rval = build_exception_variant (rval, raises);
486 return build_type_attribute_variant (rval, attributes);
489 rval = build_function_type (valtype, commonparms (p1, p2));
490 rval = build_exception_variant (rval, raises);
491 return build_type_attribute_variant (rval, attributes);
496 my_friendly_assert (TYPE_MAIN_VARIANT (t1) == t1
497 && TYPE_MAIN_VARIANT (t2) == t2, 306);
499 if (DERIVED_FROM_P (t1, t2) && binfo_or_else (t1, t2))
500 return build_type_attribute_variant (t1, attributes);
501 else if (binfo_or_else (t2, t1))
502 return build_type_attribute_variant (t2, attributes);
504 compiler_error ("common_type called with uncommon aggregate types");
507 if (TREE_CODE (TREE_TYPE (t1)) == TREE_CODE (TREE_TYPE (t2)))
509 /* Get this value the long way, since TYPE_METHOD_BASETYPE
510 is just the main variant of this. */
514 tree b1 = TYPE_OFFSET_BASETYPE (t1);
515 tree b2 = TYPE_OFFSET_BASETYPE (t2);
517 if (comptypes (b1, b2, 1)
518 || (DERIVED_FROM_P (b1, b2) && binfo_or_else (b1, b2)))
519 basetype = TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (t2)));
522 if (binfo_or_else (b2, b1) == NULL_TREE)
523 compiler_error ("common_type called with uncommon method types");
524 basetype = TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (t1)));
527 raises = TYPE_RAISES_EXCEPTIONS (t1);
529 /* If this was a member function type, get back to the
530 original type of type member function (i.e., without
531 the class instance variable up front. */
532 t1 = build_function_type (TREE_TYPE (t1), TREE_CHAIN (TYPE_ARG_TYPES (t1)));
533 t2 = build_function_type (TREE_TYPE (t2), TREE_CHAIN (TYPE_ARG_TYPES (t2)));
534 t3 = common_type (t1, t2);
535 t3 = build_cplus_method_type (basetype, TREE_TYPE (t3), TYPE_ARG_TYPES (t3));
536 t1 = build_exception_variant (t3, raises);
539 compiler_error ("common_type called with uncommon method types");
541 return build_type_attribute_variant (t1, attributes);
544 if (TREE_TYPE (t1) == TREE_TYPE (t2))
546 tree b1 = TYPE_OFFSET_BASETYPE (t1);
547 tree b2 = TYPE_OFFSET_BASETYPE (t2);
549 if (DERIVED_FROM_P (b1, b2) && binfo_or_else (b1, b2))
550 return build_type_attribute_variant (t2, attributes);
551 else if (binfo_or_else (b2, b1))
552 return build_type_attribute_variant (t1, attributes);
554 compiler_error ("common_type called with uncommon member types");
557 return build_type_attribute_variant (t1, attributes);
561 /* Return 1 if TYPE1 and TYPE2 raise the same exceptions. */
563 compexcepttypes (t1, t2)
566 return TYPE_RAISES_EXCEPTIONS (t1) == TYPE_RAISES_EXCEPTIONS (t2);
570 comp_array_types (cmp, t1, t2, strict)
571 register int (*cmp)();
575 tree d1 = TYPE_DOMAIN (t1);
576 tree d2 = TYPE_DOMAIN (t2);
578 /* Target types must match incl. qualifiers. */
579 if (!(TREE_TYPE (t1) == TREE_TYPE (t2)
580 || (*cmp) (TREE_TYPE (t1), TREE_TYPE (t2), strict)))
583 /* Sizes must match unless one is missing or variable. */
584 if (d1 == 0 || d2 == 0 || d1 == d2
585 || TREE_CODE (TYPE_MIN_VALUE (d1)) != INTEGER_CST
586 || TREE_CODE (TYPE_MIN_VALUE (d2)) != INTEGER_CST
587 || TREE_CODE (TYPE_MAX_VALUE (d1)) != INTEGER_CST
588 || TREE_CODE (TYPE_MAX_VALUE (d2)) != INTEGER_CST)
591 return ((TREE_INT_CST_LOW (TYPE_MIN_VALUE (d1))
592 == TREE_INT_CST_LOW (TYPE_MIN_VALUE (d2)))
593 && (TREE_INT_CST_HIGH (TYPE_MIN_VALUE (d1))
594 == TREE_INT_CST_HIGH (TYPE_MIN_VALUE (d2)))
595 && (TREE_INT_CST_LOW (TYPE_MAX_VALUE (d1))
596 == TREE_INT_CST_LOW (TYPE_MAX_VALUE (d2)))
597 && (TREE_INT_CST_HIGH (TYPE_MAX_VALUE (d1))
598 == TREE_INT_CST_HIGH (TYPE_MAX_VALUE (d2))));
601 /* Return 1 if TYPE1 and TYPE2 are compatible types for assignment
602 or various other operations. This is what ANSI C++ speaks of as
605 For C++: argument STRICT says we should be strict about this
608 2 : strict, except that if one type is a reference and
609 the other is not, compare the target type of the
610 reference to the type that's not a reference (ARM, p308).
611 This is used for checking for invalid overloading.
612 1 : strict (compared according to ANSI C)
613 This is used for checking whether two function decls match.
614 0 : <= (compared according to C++)
615 -1: <= or >= (relaxed)
617 Otherwise, pointers involving base classes and derived classes
618 can be mixed as valid: i.e. a pointer to a base class may be assigned
619 to a pointer to one of its derived classes, as per C++. A pointer to
620 a derived class may be passed as a parameter to a function expecting a
621 pointer to a base classes. These allowances do not commute. In this
622 case, TYPE1 is assumed to be the base class, and TYPE2 is assumed to
623 be the derived class. */
625 comptypes (type1, type2, strict)
629 register tree t1 = type1;
630 register tree t2 = type2;
633 /* Suppress errors caused by previously reported errors */
638 /* This should never happen. */
639 my_friendly_assert (t1 != error_mark_node, 307);
641 if (t2 == error_mark_node)
646 /* Treat an enum type as the unsigned integer type of the same width. */
648 if (TREE_CODE (t1) == ENUMERAL_TYPE)
649 t1 = type_for_size (TYPE_PRECISION (t1), 1);
650 if (TREE_CODE (t2) == ENUMERAL_TYPE)
651 t2 = type_for_size (TYPE_PRECISION (t2), 1);
657 if (TYPE_PTRMEMFUNC_P (t1))
658 t1 = TYPE_PTRMEMFUNC_FN_TYPE (t1);
659 if (TYPE_PTRMEMFUNC_P (t2))
660 t2 = TYPE_PTRMEMFUNC_FN_TYPE (t2);
662 /* Different classes of types can't be compatible. */
664 if (TREE_CODE (t1) != TREE_CODE (t2))
667 && ((TREE_CODE (t1) == REFERENCE_TYPE)
668 ^ (TREE_CODE (t2) == REFERENCE_TYPE)))
670 if (TREE_CODE (t1) == REFERENCE_TYPE)
671 return comptypes (TREE_TYPE (t1), t2, 1);
672 return comptypes (t1, TREE_TYPE (t2), 1);
680 /* Qualifiers must match. */
682 if (TYPE_READONLY (t1) != TYPE_READONLY (t2))
684 if (TYPE_VOLATILE (t1) != TYPE_VOLATILE (t2))
687 /* Allow for two different type nodes which have essentially the same
688 definition. Note that we already checked for equality of the type
689 type qualifiers (just above). */
691 if (TYPE_MAIN_VARIANT (t1) == TYPE_MAIN_VARIANT (t2))
694 /* ??? COMP_TYPE_ATTRIBUTES is currently useless for variables as each
695 attribute is its own main variant (`val' will remain 0). */
696 #ifndef COMP_TYPE_ATTRIBUTES
697 #define COMP_TYPE_ATTRIBUTES(t1,t2) 1
700 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
701 if (! (attrval = COMP_TYPE_ATTRIBUTES (t1, t2)))
704 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
707 switch (TREE_CODE (t1))
711 if (CLASSTYPE_TEMPLATE_INFO (t1) && CLASSTYPE_TEMPLATE_INFO (t2)
712 && CLASSTYPE_TI_TEMPLATE (t1) == CLASSTYPE_TI_TEMPLATE (t2))
714 int i = TREE_VEC_LENGTH (CLASSTYPE_TI_ARGS (t1));
715 tree *p1 = &TREE_VEC_ELT (CLASSTYPE_TI_ARGS (t1), 0);
716 tree *p2 = &TREE_VEC_ELT (CLASSTYPE_TI_ARGS (t2), 0);
720 if (TREE_CODE_CLASS (TREE_CODE (p1[i])) == 't')
722 if (! comptypes (p1[i], p2[i], 1))
727 if (simple_cst_equal (p1[i], p2[i]) <= 0)
738 val = (comptypes (build_pointer_type (TYPE_OFFSET_BASETYPE (t1)),
739 build_pointer_type (TYPE_OFFSET_BASETYPE (t2)), strict)
740 && comptypes (TREE_TYPE (t1), TREE_TYPE (t2), strict));
744 if (! compexcepttypes (t1, t2))
747 /* This case is anti-symmetrical!
748 One can pass a base member (or member function)
749 to something expecting a derived member (or member function),
750 but not vice-versa! */
752 val = (comptypes (TREE_TYPE (t1), TREE_TYPE (t2), strict)
753 && compparms (TYPE_ARG_TYPES (t1),
754 TYPE_ARG_TYPES (t2), strict));
768 if (TREE_CODE (t1) == RECORD_TYPE && TREE_CODE (t2) == RECORD_TYPE)
772 rval = t1 == t2 || UNIQUELY_DERIVED_FROM_P (t1, t2);
781 val = UNIQUELY_DERIVED_FROM_P (t2, t1);
788 val = comptypes (t1, t2, strict);
792 if (! compexcepttypes (t1, t2))
795 val = ((TREE_TYPE (t1) == TREE_TYPE (t2)
796 || comptypes (TREE_TYPE (t1), TREE_TYPE (t2), strict))
797 && compparms (TYPE_ARG_TYPES (t1), TYPE_ARG_TYPES (t2), strict));
801 /* Target types must match incl. qualifiers. */
802 val = comp_array_types (comptypes, t1, t2, strict);
805 case TEMPLATE_TYPE_PARM:
806 return TEMPLATE_TYPE_IDX (t1) == TEMPLATE_TYPE_IDX (t2);
809 if (TYPE_IDENTIFIER (t1) != TYPE_IDENTIFIER (t2))
811 return comptypes (TYPE_CONTEXT (t1), TYPE_CONTEXT (t2), 1);
813 return attrval == 2 && val == 1 ? 2 : val;
816 /* Return 1 if TTL and TTR are pointers to types that are equivalent,
817 ignoring their qualifiers.
819 NPTRS is the number of pointers we can strip off and keep cool.
820 This is used to permit (for aggr A, aggr B) A, B* to convert to A*,
821 but to not permit B** to convert to A**. */
824 comp_target_types (ttl, ttr, nptrs)
828 ttl = TYPE_MAIN_VARIANT (ttl);
829 ttr = TYPE_MAIN_VARIANT (ttr);
833 if (TREE_CODE (ttr) != TREE_CODE (ttl))
836 if (TREE_CODE (ttr) == POINTER_TYPE)
838 ttl = TREE_TYPE (ttl);
839 ttr = TREE_TYPE (ttr);
843 if (TREE_CODE (ttl) == VOID_TYPE
844 && TREE_CODE (ttr) != FUNCTION_TYPE
845 && TREE_CODE (ttr) != METHOD_TYPE
846 && TREE_CODE (ttr) != OFFSET_TYPE)
848 else if (TREE_CODE (ttr) == VOID_TYPE
849 && TREE_CODE (ttl) != FUNCTION_TYPE
850 && TREE_CODE (ttl) != METHOD_TYPE
851 && TREE_CODE (ttl) != OFFSET_TYPE)
853 else if (TREE_CODE (ttl) == POINTER_TYPE
854 || TREE_CODE (ttl) == ARRAY_TYPE)
856 if (comp_ptr_ttypes (ttl, ttr))
858 else if (comp_ptr_ttypes (ttr, ttl))
864 /* Const and volatile mean something different for function types,
865 so the usual checks are not appropriate. */
866 if (TREE_CODE (ttl) == FUNCTION_TYPE || TREE_CODE (ttl) == METHOD_TYPE)
867 return comp_target_types (ttl, ttr, nptrs - 1);
869 /* Make sure that the cv-quals change only in the same direction as
873 int c = TYPE_READONLY (ttl) - TYPE_READONLY (ttr);
874 int v = TYPE_VOLATILE (ttl) - TYPE_VOLATILE (ttr);
876 if ((c > 0 && v < 0) || (c < 0 && v > 0))
879 if (TYPE_MAIN_VARIANT (ttl) == TYPE_MAIN_VARIANT (ttr))
880 return (c + v < 0) ? -1 : 1;
882 t = comp_target_types (ttl, ttr, nptrs - 1);
883 if ((t == 1 && c + v >= 0) || (t == -1 && c + v <= 0))
890 if (TREE_CODE (ttr) == REFERENCE_TYPE)
891 return comp_target_types (TREE_TYPE (ttl), TREE_TYPE (ttr), nptrs);
892 if (TREE_CODE (ttr) == ARRAY_TYPE)
893 return comp_array_types (comp_target_types, ttl, ttr, 0);
894 else if (TREE_CODE (ttr) == FUNCTION_TYPE || TREE_CODE (ttr) == METHOD_TYPE)
895 if (comp_target_types (TREE_TYPE (ttl), TREE_TYPE (ttr), nptrs))
896 switch (comp_target_parms (TYPE_ARG_TYPES (ttl), TYPE_ARG_TYPES (ttr), 1))
905 my_friendly_abort (112);
911 else if (TREE_CODE (ttr) == OFFSET_TYPE)
913 /* Contravariance: we can assign a pointer to base member to a pointer
914 to derived member. Note difference from simple pointer case, where
915 we can pass a pointer to derived to a pointer to base. */
916 if (comptypes (TYPE_OFFSET_BASETYPE (ttr), TYPE_OFFSET_BASETYPE (ttl), 0))
917 return comp_target_types (TREE_TYPE (ttl), TREE_TYPE (ttr), nptrs);
918 else if (comptypes (TYPE_OFFSET_BASETYPE (ttl), TYPE_OFFSET_BASETYPE (ttr), 0)
919 && comp_target_types (TREE_TYPE (ttl), TREE_TYPE (ttr), nptrs))
922 else if (IS_AGGR_TYPE (ttl))
926 if (comptypes (build_pointer_type (ttl), build_pointer_type (ttr), 0))
928 if (comptypes (build_pointer_type (ttr), build_pointer_type (ttl), 0))
936 /* If two types share a common base type, return that basetype.
937 If there is not a unique most-derived base type, this function
938 returns ERROR_MARK_NODE. */
940 common_base_type (tt1, tt2)
943 tree best = NULL_TREE;
946 /* If one is a baseclass of another, that's good enough. */
947 if (UNIQUELY_DERIVED_FROM_P (tt1, tt2))
949 if (UNIQUELY_DERIVED_FROM_P (tt2, tt1))
952 /* Otherwise, try to find a unique baseclass of TT1
953 that is shared by TT2, and follow that down. */
954 for (i = CLASSTYPE_N_BASECLASSES (tt1)-1; i >= 0; i--)
956 tree basetype = TYPE_BINFO_BASETYPE (tt1, i);
957 tree trial = common_base_type (basetype, tt2);
960 if (trial == error_mark_node)
962 if (best == NULL_TREE)
964 else if (best != trial)
965 return error_mark_node;
970 for (i = CLASSTYPE_N_BASECLASSES (tt2)-1; i >= 0; i--)
972 tree basetype = TYPE_BINFO_BASETYPE (tt2, i);
973 tree trial = common_base_type (tt1, basetype);
976 if (trial == error_mark_node)
978 if (best == NULL_TREE)
980 else if (best != trial)
981 return error_mark_node;
987 /* Subroutines of `comptypes'. */
989 /* Return 1 if two parameter type lists PARMS1 and PARMS2
990 are equivalent in the sense that functions with those parameter types
991 can have equivalent types.
992 If either list is empty, we win.
993 Otherwise, the two lists must be equivalent, element by element.
995 C++: See comment above about TYPE1, TYPE2, STRICT.
996 If STRICT == 3, it means checking is strict, but do not compare
997 default parameter values. */
999 compparms (parms1, parms2, strict)
1000 tree parms1, parms2;
1003 register tree t1 = parms1, t2 = parms2;
1005 /* An unspecified parmlist matches any specified parmlist
1006 whose argument types don't need default promotions. */
1008 if (strict <= 0 && t1 == 0)
1009 return self_promoting_args_p (t2);
1010 if (strict < 0 && t2 == 0)
1011 return self_promoting_args_p (t1);
1015 if (t1 == 0 && t2 == 0)
1017 /* If one parmlist is shorter than the other,
1018 they fail to match, unless STRICT is <= 0. */
1019 if (t1 == 0 || t2 == 0)
1026 return t1 && TREE_PURPOSE (t1);
1028 if (! comptypes (TREE_VALUE (t2), TREE_VALUE (t1), strict))
1033 return t2 == void_list_node && TREE_PURPOSE (t1);
1034 return TREE_PURPOSE (t1) || TREE_PURPOSE (t2);
1037 t1 = TREE_CHAIN (t1);
1038 t2 = TREE_CHAIN (t2);
1042 /* This really wants return whether or not parameter type lists
1043 would make their owning functions assignment compatible or not. */
1045 comp_target_parms (parms1, parms2, strict)
1046 tree parms1, parms2;
1049 register tree t1 = parms1, t2 = parms2;
1050 int warn_contravariance = 0;
1052 /* An unspecified parmlist matches any specified parmlist
1053 whose argument types don't need default promotions.
1054 @@@ see 13.3.3 for a counterexample... */
1056 if (t1 == 0 && t2 != 0)
1058 cp_pedwarn ("ANSI C++ prohibits conversion from `(%#T)' to `(...)'",
1060 return self_promoting_args_p (t2);
1063 return self_promoting_args_p (t1);
1065 for (; t1 || t2; t1 = TREE_CHAIN (t1), t2 = TREE_CHAIN (t2))
1069 /* If one parmlist is shorter than the other,
1070 they fail to match, unless STRICT is <= 0. */
1071 if (t1 == 0 || t2 == 0)
1076 return 1 + warn_contravariance;
1077 return ((t1 && TREE_PURPOSE (t1)) + warn_contravariance);
1079 p1 = TREE_VALUE (t1);
1080 p2 = TREE_VALUE (t2);
1084 if ((TREE_CODE (p1) == POINTER_TYPE && TREE_CODE (p2) == POINTER_TYPE)
1085 || (TREE_CODE (p1) == REFERENCE_TYPE && TREE_CODE (p2) == REFERENCE_TYPE))
1088 && (TYPE_MAIN_VARIANT (TREE_TYPE (p1))
1089 == TYPE_MAIN_VARIANT (TREE_TYPE (p2))))
1092 /* The following is wrong for contravariance,
1093 but many programs depend on it. */
1094 if (TREE_TYPE (p1) == void_type_node)
1096 if (TREE_TYPE (p2) == void_type_node)
1098 warn_contravariance = 1;
1101 if (IS_AGGR_TYPE (TREE_TYPE (p1)))
1103 if (comptypes (p2, p1, 0) == 0)
1105 if (comptypes (p1, p2, 0) != 0)
1106 warn_contravariance = 1;
1113 /* Note backwards order due to contravariance. */
1114 if (comp_target_types (p2, p1, 1) == 0)
1116 if (comp_target_types (p1, p2, 1))
1118 warn_contravariance = 1;
1124 /* Target types are compatible--just make sure that if
1125 we use parameter lists, that they are ok as well. */
1126 if (TREE_CODE (p1) == FUNCTION_TYPE || TREE_CODE (p1) == METHOD_TYPE)
1127 switch (comp_target_parms (TYPE_ARG_TYPES (p1),
1128 TYPE_ARG_TYPES (p2),
1136 warn_contravariance = 1;
1139 if (TREE_PURPOSE (t1) && TREE_PURPOSE (t2))
1141 int cmp = simple_cst_equal (TREE_PURPOSE (t1), TREE_PURPOSE (t2));
1143 my_friendly_abort (114);
1148 return 1 + warn_contravariance;
1151 /* Return 1 if PARMS specifies a fixed number of parameters
1152 and none of their types is affected by default promotions. */
1155 self_promoting_args_p (parms)
1159 for (t = parms; t; t = TREE_CHAIN (t))
1161 register tree type = TREE_VALUE (t);
1163 if (TREE_CHAIN (t) == 0 && type != void_type_node)
1169 if (TYPE_MAIN_VARIANT (type) == float_type_node)
1172 if (C_PROMOTING_INTEGER_TYPE_P (type))
1178 /* Return an unsigned type the same as TYPE in other respects.
1180 C++: must make these work for type variants as well. */
1183 unsigned_type (type)
1186 tree type1 = TYPE_MAIN_VARIANT (type);
1187 if (type1 == signed_char_type_node || type1 == char_type_node)
1188 return unsigned_char_type_node;
1189 if (type1 == integer_type_node)
1190 return unsigned_type_node;
1191 if (type1 == short_integer_type_node)
1192 return short_unsigned_type_node;
1193 if (type1 == long_integer_type_node)
1194 return long_unsigned_type_node;
1195 if (type1 == long_long_integer_type_node)
1196 return long_long_unsigned_type_node;
1197 if (type1 == intDI_type_node)
1198 return unsigned_intDI_type_node;
1199 if (type1 == intSI_type_node)
1200 return unsigned_intSI_type_node;
1201 if (type1 == intHI_type_node)
1202 return unsigned_intHI_type_node;
1203 if (type1 == intQI_type_node)
1204 return unsigned_intQI_type_node;
1208 /* Return a signed type the same as TYPE in other respects. */
1214 tree type1 = TYPE_MAIN_VARIANT (type);
1215 if (type1 == unsigned_char_type_node || type1 == char_type_node)
1216 return signed_char_type_node;
1217 if (type1 == unsigned_type_node)
1218 return integer_type_node;
1219 if (type1 == short_unsigned_type_node)
1220 return short_integer_type_node;
1221 if (type1 == long_unsigned_type_node)
1222 return long_integer_type_node;
1223 if (type1 == long_long_unsigned_type_node)
1224 return long_long_integer_type_node;
1225 if (type1 == unsigned_intDI_type_node)
1226 return intDI_type_node;
1227 if (type1 == unsigned_intSI_type_node)
1228 return intSI_type_node;
1229 if (type1 == unsigned_intHI_type_node)
1230 return intHI_type_node;
1231 if (type1 == unsigned_intQI_type_node)
1232 return intQI_type_node;
1236 /* Return a type the same as TYPE except unsigned or
1237 signed according to UNSIGNEDP. */
1240 signed_or_unsigned_type (unsignedp, type)
1244 if (! INTEGRAL_TYPE_P (type))
1246 if (TYPE_PRECISION (type) == TYPE_PRECISION (signed_char_type_node))
1247 return unsignedp ? unsigned_char_type_node : signed_char_type_node;
1248 if (TYPE_PRECISION (type) == TYPE_PRECISION (integer_type_node))
1249 return unsignedp ? unsigned_type_node : integer_type_node;
1250 if (TYPE_PRECISION (type) == TYPE_PRECISION (short_integer_type_node))
1251 return unsignedp ? short_unsigned_type_node : short_integer_type_node;
1252 if (TYPE_PRECISION (type) == TYPE_PRECISION (long_integer_type_node))
1253 return unsignedp ? long_unsigned_type_node : long_integer_type_node;
1254 if (TYPE_PRECISION (type) == TYPE_PRECISION (long_long_integer_type_node))
1255 return (unsignedp ? long_long_unsigned_type_node
1256 : long_long_integer_type_node);
1260 /* Compute the value of the `sizeof' operator. */
1266 enum tree_code code = TREE_CODE (type);
1269 if (current_template_parms)
1270 return build_min (SIZEOF_EXPR, sizetype, type);
1272 if (code == FUNCTION_TYPE)
1274 if (pedantic || warn_pointer_arith)
1275 pedwarn ("ANSI C++ forbids taking the sizeof a function type");
1276 return size_int (1);
1278 if (code == METHOD_TYPE)
1280 if (pedantic || warn_pointer_arith)
1281 pedwarn ("ANSI C++ forbids taking the sizeof a method type");
1282 return size_int (1);
1284 if (code == VOID_TYPE)
1286 if (pedantic || warn_pointer_arith)
1287 pedwarn ("ANSI C++ forbids taking the sizeof a void type");
1288 return size_int (1);
1290 if (code == ERROR_MARK)
1291 return size_int (1);
1293 /* ARM $5.3.2: ``When applied to a reference, the result is the size of the
1294 referenced object.'' */
1295 if (code == REFERENCE_TYPE)
1296 type = TREE_TYPE (type);
1298 /* We couldn't find anything in the ARM or the draft standard that says,
1299 one way or the other, if doing sizeof on something that doesn't have
1300 an object associated with it is correct or incorrect. For example, if
1301 you declare `struct S { char str[16]; };', and in your program do
1302 a `sizeof (S::str)', should we flag that as an error or should we give
1303 the size of it? Since it seems like a reasonable thing to do, we'll go
1304 with giving the value. */
1305 if (code == OFFSET_TYPE)
1306 type = TREE_TYPE (type);
1308 /* @@ This also produces an error for a signature ref.
1309 In that case we should be able to do better. */
1310 if (IS_SIGNATURE (type))
1312 error ("`sizeof' applied to a signature type");
1313 return size_int (0);
1316 if (TYPE_SIZE (complete_type (type)) == 0)
1318 cp_error ("`sizeof' applied to incomplete type `%T'", type);
1319 return size_int (0);
1322 /* Convert in case a char is more than one unit. */
1323 t = size_binop (CEIL_DIV_EXPR, TYPE_SIZE (type),
1324 size_int (TYPE_PRECISION (char_type_node)));
1325 /* size_binop does not put the constant in range, so do it now. */
1326 if (TREE_CODE (t) == INTEGER_CST && force_fit_type (t, 0))
1327 TREE_CONSTANT_OVERFLOW (t) = TREE_OVERFLOW (t) = 1;
1335 if (current_template_parms)
1336 return build_min (SIZEOF_EXPR, sizetype, e);
1338 if (TREE_CODE (e) == COMPONENT_REF
1339 && DECL_BIT_FIELD (TREE_OPERAND (e, 1)))
1340 error ("sizeof applied to a bit-field");
1341 /* ANSI says arrays and functions are converted inside comma.
1342 But we can't really convert them in build_compound_expr
1343 because that would break commas in lvalues.
1344 So do the conversion here if operand was a comma. */
1345 if (TREE_CODE (e) == COMPOUND_EXPR
1346 && (TREE_CODE (TREE_TYPE (e)) == ARRAY_TYPE
1347 || TREE_CODE (TREE_TYPE (e)) == FUNCTION_TYPE))
1348 e = default_conversion (e);
1349 else if (TREE_CODE (e) == TREE_LIST)
1351 tree t = TREE_VALUE (e);
1354 && TREE_CODE (TREE_TYPE (t)) == FUNCTION_TYPE)
1355 || is_overloaded_fn (t)))
1356 pedwarn ("ANSI C++ forbids taking the sizeof a function type");
1358 return c_sizeof (TREE_TYPE (e));
1362 c_sizeof_nowarn (type)
1365 enum tree_code code = TREE_CODE (type);
1368 if (code == FUNCTION_TYPE
1369 || code == METHOD_TYPE
1370 || code == VOID_TYPE
1371 || code == ERROR_MARK)
1372 return size_int (1);
1373 if (code == REFERENCE_TYPE)
1374 type = TREE_TYPE (type);
1376 if (TYPE_SIZE (type) == 0)
1377 return size_int (0);
1379 /* Convert in case a char is more than one unit. */
1380 t = size_binop (CEIL_DIV_EXPR, TYPE_SIZE (type),
1381 size_int (TYPE_PRECISION (char_type_node)));
1382 force_fit_type (t, 0);
1386 /* Implement the __alignof keyword: Return the minimum required
1387 alignment of TYPE, measured in bytes. */
1393 enum tree_code code = TREE_CODE (type);
1396 if (code == FUNCTION_TYPE || code == METHOD_TYPE)
1397 return size_int (FUNCTION_BOUNDARY / BITS_PER_UNIT);
1399 if (code == VOID_TYPE || code == ERROR_MARK)
1400 return size_int (1);
1402 /* C++: this is really correct! */
1403 if (code == REFERENCE_TYPE)
1404 type = TREE_TYPE (type);
1406 /* @@ This also produces an error for a signature ref.
1407 In that case we should be able to do better. */
1408 if (IS_SIGNATURE (type))
1410 error ("`__alignof' applied to a signature type");
1411 return size_int (1);
1414 t = size_int (TYPE_ALIGN (type) / BITS_PER_UNIT);
1415 force_fit_type (t, 0);
1419 /* Perform default promotions for C data used in expressions.
1420 Arrays and functions are converted to pointers;
1421 enumeral types or short or char, to int.
1422 In addition, manifest constants symbols are replaced by their values.
1424 C++: this will automatically bash references to their target type. */
1427 decay_conversion (exp)
1430 register tree type = TREE_TYPE (exp);
1431 register enum tree_code code = TREE_CODE (type);
1433 if (code == OFFSET_TYPE /* || TREE_CODE (exp) == OFFSET_REF */ )
1435 if (TREE_CODE (exp) == OFFSET_REF)
1436 return decay_conversion (resolve_offset_ref (exp));
1438 type = TREE_TYPE (type);
1439 code = TREE_CODE (type);
1442 if (code == REFERENCE_TYPE)
1444 exp = convert_from_reference (exp);
1445 type = TREE_TYPE (exp);
1446 code = TREE_CODE (type);
1449 /* Constants can be used directly unless they're not loadable. */
1450 if (TREE_CODE (exp) == CONST_DECL)
1451 exp = DECL_INITIAL (exp);
1452 /* Replace a nonvolatile const static variable with its value. */
1453 else if (TREE_READONLY_DECL_P (exp))
1455 exp = decl_constant_value (exp);
1456 type = TREE_TYPE (exp);
1459 /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
1460 Leave such NOP_EXPRs, since RHS is being used in non-lvalue context. */
1462 if (code == VOID_TYPE)
1464 error ("void value not ignored as it ought to be");
1465 return error_mark_node;
1467 if (code == FUNCTION_TYPE)
1469 return build_unary_op (ADDR_EXPR, exp, 0);
1471 if (code == METHOD_TYPE)
1473 if (TREE_CODE (exp) == OFFSET_REF)
1475 /* FIXME: We should emit an error here about using a ptrmemfunc
1476 for something other than a function call. */
1477 my_friendly_assert (TREE_CODE (TREE_OPERAND (exp, 1)) == FUNCTION_DECL,
1479 return build_unary_op (ADDR_EXPR, TREE_OPERAND (exp, 1), 0);
1481 return build_unary_op (ADDR_EXPR, exp, 0);
1483 if (code == ARRAY_TYPE)
1488 int constp, volatilep;
1490 if (TREE_CODE (exp) == INDIRECT_REF)
1492 /* Stripping away the INDIRECT_REF is not the right
1493 thing to do for references... */
1494 tree inner = TREE_OPERAND (exp, 0);
1495 if (TREE_CODE (TREE_TYPE (inner)) == REFERENCE_TYPE)
1497 inner = build1 (CONVERT_EXPR,
1498 build_pointer_type (TREE_TYPE (TREE_TYPE (inner))),
1500 TREE_REFERENCE_EXPR (inner) = 1;
1502 return convert (build_pointer_type (TREE_TYPE (type)), inner);
1505 if (TREE_CODE (exp) == COMPOUND_EXPR)
1507 tree op1 = decay_conversion (TREE_OPERAND (exp, 1));
1508 return build (COMPOUND_EXPR, TREE_TYPE (op1),
1509 TREE_OPERAND (exp, 0), op1);
1513 && ! (TREE_CODE (exp) == CONSTRUCTOR && TREE_STATIC (exp)))
1515 error ("invalid use of non-lvalue array");
1516 return error_mark_node;
1519 constp = volatilep = 0;
1520 if (TREE_CODE_CLASS (TREE_CODE (exp)) == 'r'
1521 || TREE_CODE_CLASS (TREE_CODE (exp)) == 'd')
1523 constp = TREE_READONLY (exp);
1524 volatilep = TREE_THIS_VOLATILE (exp);
1527 restype = TREE_TYPE (type);
1528 if (TYPE_READONLY (type) || TYPE_VOLATILE (type)
1529 || constp || volatilep)
1530 restype = cp_build_type_variant (restype,
1531 TYPE_READONLY (type) || constp,
1532 TYPE_VOLATILE (type) || volatilep);
1533 ptrtype = build_pointer_type (restype);
1535 if (TREE_CODE (exp) == VAR_DECL)
1537 /* ??? This is not really quite correct
1538 in that the type of the operand of ADDR_EXPR
1539 is not the target type of the type of the ADDR_EXPR itself.
1540 Question is, can this lossage be avoided? */
1541 adr = build1 (ADDR_EXPR, ptrtype, exp);
1542 if (mark_addressable (exp) == 0)
1543 return error_mark_node;
1544 TREE_CONSTANT (adr) = staticp (exp);
1545 TREE_SIDE_EFFECTS (adr) = 0; /* Default would be, same as EXP. */
1548 /* This way is better for a COMPONENT_REF since it can
1549 simplify the offset for a component. */
1550 adr = build_unary_op (ADDR_EXPR, exp, 1);
1551 return convert (ptrtype, adr);
1558 default_conversion (exp)
1562 enum tree_code code;
1564 exp = decay_conversion (exp);
1566 type = TREE_TYPE (exp);
1567 code = TREE_CODE (type);
1569 if (INTEGRAL_CODE_P (code))
1571 tree t = type_promotes_to (type);
1573 return convert (t, exp);
1575 if (flag_traditional
1576 && TYPE_MAIN_VARIANT (type) == float_type_node)
1577 return convert (double_type_node, exp);
1582 /* Take the address of an inline function without setting TREE_ADDRESSABLE
1586 inline_conversion (exp)
1589 if (TREE_CODE (exp) == FUNCTION_DECL)
1591 tree type = build_type_variant
1592 (TREE_TYPE (exp), TREE_READONLY (exp), TREE_THIS_VOLATILE (exp));
1593 exp = build1 (ADDR_EXPR, build_pointer_type (type), exp);
1599 build_object_ref (datum, basetype, field)
1600 tree datum, basetype, field;
1603 if (datum == error_mark_node)
1604 return error_mark_node;
1606 dtype = TREE_TYPE (datum);
1607 if (TREE_CODE (dtype) == REFERENCE_TYPE)
1608 dtype = TREE_TYPE (dtype);
1609 if (! IS_AGGR_TYPE_CODE (TREE_CODE (dtype)))
1611 cp_error ("request for member `%T::%D' in expression of non-aggregate type `%T'",
1612 basetype, field, dtype);
1613 return error_mark_node;
1615 else if (IS_SIGNATURE (basetype))
1617 warning ("signature name in scope resolution ignored");
1618 return build_component_ref (datum, field, NULL_TREE, 1);
1620 else if (is_aggr_type (basetype, 1))
1622 tree binfo = binfo_or_else (basetype, dtype);
1624 return build_component_ref (build_scoped_ref (datum, basetype),
1627 return error_mark_node;
1630 /* Like `build_component_ref, but uses an already found field.
1631 Must compute access for C_C_D. Otherwise, ok. */
1633 build_component_ref_1 (datum, field, protect)
1637 register tree basetype = TREE_TYPE (datum);
1638 register enum tree_code code = TREE_CODE (basetype);
1641 if (code == REFERENCE_TYPE)
1643 datum = convert_from_reference (datum);
1644 basetype = TREE_TYPE (datum);
1645 code = TREE_CODE (basetype);
1648 if (! IS_AGGR_TYPE_CODE (code))
1650 if (code != ERROR_MARK)
1651 cp_error ("request for member `%D' in `%E', which is of non-aggregate type `%T'",
1652 field, datum, basetype);
1653 return error_mark_node;
1656 if (TYPE_SIZE (basetype) == 0)
1658 incomplete_type_error (0, basetype);
1659 return error_mark_node;
1662 /* Look up component name in the structure type definition. */
1664 if (field == error_mark_node)
1665 my_friendly_abort (115);
1667 if (TREE_STATIC (field))
1672 tree access = compute_access (TYPE_BINFO (current_class_type), field);
1674 if (access == access_private_node)
1676 cp_error ("field `%D' is private", field);
1677 return error_mark_node;
1679 else if (access == access_protected_node)
1681 cp_error ("field `%D' is protected", field);
1682 return error_mark_node;
1686 ref = build (COMPONENT_REF, TREE_TYPE (field), datum, field);
1688 if (TREE_READONLY (datum) || TREE_READONLY (field))
1689 TREE_READONLY (ref) = 1;
1690 if (TREE_THIS_VOLATILE (datum) || TREE_THIS_VOLATILE (field))
1691 TREE_THIS_VOLATILE (ref) = 1;
1692 if (DECL_MUTABLE_P (field))
1693 TREE_READONLY (ref) = 0;
1698 /* Given a COND_EXPR in T, return it in a form that we can, for
1699 example, use as an lvalue. This code used to be in unary_complex_lvalue,
1700 but we needed it to deal with `a = (d == c) ? b : c' expressions, where
1701 we're dealing with aggregates. So, we now call this in unary_complex_lvalue,
1702 and in build_modify_expr. The case (in particular) that led to this was
1703 with CODE == ADDR_EXPR, since it's not an lvalue when we'd get it there. */
1705 rationalize_conditional_expr (code, t)
1706 enum tree_code code;
1710 build_conditional_expr (TREE_OPERAND (t, 0),
1711 build_unary_op (code, TREE_OPERAND (t, 1), 0),
1712 build_unary_op (code, TREE_OPERAND (t, 2), 0));
1715 /* Given the TYPE of an anonymous union field inside T, return the
1716 FIELD_DECL for the field. If not found return NULL_TREE. Because
1717 anonymous unions can nest, we must also search all anonymous unions
1718 that are directly reachable. */
1720 lookup_anon_field (t, type)
1725 for (field = TYPE_FIELDS (t); field; field = TREE_CHAIN (field))
1727 if (TREE_STATIC (field))
1729 if (TREE_CODE (field) != FIELD_DECL)
1732 /* If we find it directly, return the field. */
1733 if (DECL_NAME (field) == NULL_TREE
1734 && type == TREE_TYPE (field))
1739 /* Otherwise, it could be nested, search harder. */
1740 if (DECL_NAME (field) == NULL_TREE
1741 && TREE_CODE (TREE_TYPE (field)) == UNION_TYPE)
1743 tree subfield = lookup_anon_field (TREE_TYPE (field), type);
1751 /* Build a COMPONENT_REF for a given DATUM, and it's member COMPONENT.
1752 COMPONENT can be an IDENTIFIER_NODE that is the name of the member
1753 that we are interested in, or it can be a FIELD_DECL. */
1755 build_component_ref (datum, component, basetype_path, protect)
1756 tree datum, component, basetype_path;
1759 register tree basetype = TREE_TYPE (datum);
1760 register enum tree_code code;
1761 register tree field = NULL;
1764 if (current_template_parms)
1765 return build_min_nt (COMPONENT_REF, datum, component);
1767 /* If DATUM is a COMPOUND_EXPR or COND_EXPR, move our reference inside it. */
1768 switch (TREE_CODE (datum))
1772 tree value = build_component_ref (TREE_OPERAND (datum, 1), component,
1773 basetype_path, protect);
1774 return build (COMPOUND_EXPR, TREE_TYPE (value),
1775 TREE_OPERAND (datum, 0), value);
1778 return build_conditional_expr
1779 (TREE_OPERAND (datum, 0),
1780 build_component_ref (TREE_OPERAND (datum, 1), component,
1781 basetype_path, protect),
1782 build_component_ref (TREE_OPERAND (datum, 2), component,
1783 basetype_path, protect));
1786 code = TREE_CODE (basetype);
1788 if (code == REFERENCE_TYPE)
1790 datum = convert_from_reference (datum);
1791 basetype = TREE_TYPE (datum);
1792 code = TREE_CODE (basetype);
1794 if (TREE_CODE (datum) == OFFSET_REF)
1796 datum = resolve_offset_ref (datum);
1797 basetype = TREE_TYPE (datum);
1798 code = TREE_CODE (basetype);
1801 /* First, see if there is a field or component with name COMPONENT. */
1802 if (TREE_CODE (component) == TREE_LIST)
1804 my_friendly_assert (!(TREE_CHAIN (component) == NULL_TREE
1805 && DECL_CHAIN (TREE_VALUE (component)) == NULL_TREE), 309);
1806 return build (COMPONENT_REF, TREE_TYPE (component), datum, component);
1809 if (! IS_AGGR_TYPE_CODE (code))
1811 if (code != ERROR_MARK)
1812 cp_error ("request for member `%D' in `%E', which is of non-aggregate type `%T'",
1813 component, datum, basetype);
1814 return error_mark_node;
1817 if (TYPE_SIZE (complete_type (basetype)) == 0)
1819 incomplete_type_error (0, basetype);
1820 return error_mark_node;
1823 if (TREE_CODE (component) == BIT_NOT_EXPR)
1825 if (TYPE_IDENTIFIER (basetype) != TREE_OPERAND (component, 0))
1827 cp_error ("destructor specifier `%T::~%T' must have matching names",
1828 basetype, TREE_OPERAND (component, 0));
1829 return error_mark_node;
1831 if (! TYPE_HAS_DESTRUCTOR (basetype))
1833 cp_error ("type `%T' has no destructor", basetype);
1834 return error_mark_node;
1836 return TREE_VEC_ELT (CLASSTYPE_METHOD_VEC (basetype), 1);
1839 /* Look up component name in the structure type definition. */
1840 if (CLASSTYPE_VFIELD (basetype)
1841 && DECL_NAME (CLASSTYPE_VFIELD (basetype)) == component)
1842 /* Special-case this because if we use normal lookups in an ambiguous
1843 hierarchy, the compiler will abort (because vptr lookups are
1844 not supposed to be ambiguous. */
1845 field = CLASSTYPE_VFIELD (basetype);
1846 else if (TREE_CODE (component) == FIELD_DECL)
1852 if (basetype_path == NULL_TREE)
1853 basetype_path = TYPE_BINFO (basetype);
1854 field = lookup_field (basetype_path, component,
1855 protect && ! VFIELD_NAME_P (component), 0);
1856 if (field == error_mark_node)
1857 return error_mark_node;
1859 if (field == NULL_TREE)
1861 /* Not found as a data field, look for it as a method. If found,
1862 then if this is the only possible one, return it, else
1863 report ambiguity error. */
1864 tree fndecls = lookup_fnfields (basetype_path, component, 1);
1865 if (fndecls == error_mark_node)
1866 return error_mark_node;
1869 if (TREE_CHAIN (fndecls) == NULL_TREE
1870 && DECL_CHAIN (TREE_VALUE (fndecls)) == NULL_TREE)
1872 tree access, fndecl;
1874 /* Unique, so use this one now. */
1875 basetype = TREE_PURPOSE (fndecls);
1876 fndecl = TREE_VALUE (fndecls);
1877 access = compute_access (TREE_PURPOSE (fndecls), fndecl);
1878 if (access == access_public_node)
1880 if (DECL_VINDEX (fndecl)
1881 && ! resolves_to_fixed_type_p (datum, 0))
1883 tree addr = build_unary_op (ADDR_EXPR, datum, 0);
1884 addr = convert_pointer_to (DECL_CONTEXT (fndecl), addr);
1885 datum = build_indirect_ref (addr, NULL_PTR);
1886 my_friendly_assert (datum != error_mark_node, 310);
1887 fndecl = build_vfn_ref (&addr, datum, DECL_VINDEX (fndecl));
1892 if (access == access_protected_node)
1893 cp_error ("member function `%D' is protected", fndecl);
1895 cp_error ("member function `%D' is private", fndecl);
1896 return error_mark_node;
1900 /* Just act like build_offset_ref, since the object does
1901 not matter unless we're actually calling the function. */
1904 t = build_tree_list (error_mark_node, fndecls);
1905 TREE_TYPE (t) = build_offset_type (basetype,
1911 cp_error ("`%#T' has no member named `%D'", basetype, component);
1912 return error_mark_node;
1914 else if (TREE_TYPE (field) == error_mark_node)
1915 return error_mark_node;
1917 if (TREE_CODE (field) != FIELD_DECL)
1919 if (TREE_CODE (field) == TYPE_DECL)
1921 cp_error ("invalid use of type decl `%#D' as expression", field);
1922 return error_mark_node;
1924 else if (DECL_RTL (field) != 0)
1927 TREE_USED (field) = 1;
1932 /* See if we have to do any conversions so that we pick up the field from the
1934 if (DECL_FIELD_CONTEXT (field) != basetype)
1936 tree context = DECL_FIELD_CONTEXT (field);
1937 tree base = context;
1938 while (base != basetype && TYPE_NAME (base)
1939 && ANON_AGGRNAME_P (TYPE_IDENTIFIER (base)))
1941 base = TYPE_CONTEXT (base);
1944 /* Handle base classes here... */
1945 if (base != basetype && TYPE_USES_COMPLEX_INHERITANCE (basetype))
1947 tree addr = build_unary_op (ADDR_EXPR, datum, 0);
1948 if (integer_zerop (addr))
1950 error ("invalid reference to NULL ptr, use ptr-to-member instead");
1951 return error_mark_node;
1953 if (VBASE_NAME_P (DECL_NAME (field)))
1955 /* It doesn't matter which vbase pointer we grab, just
1956 find one of them. */
1957 tree binfo = get_binfo (base,
1958 TREE_TYPE (TREE_TYPE (addr)), 0);
1959 addr = convert_pointer_to_real (binfo, addr);
1962 addr = convert_pointer_to (base, addr);
1963 datum = build_indirect_ref (addr, NULL_PTR);
1964 my_friendly_assert (datum != error_mark_node, 311);
1968 /* Handle things from anon unions here... */
1969 if (TYPE_NAME (context) && ANON_AGGRNAME_P (TYPE_IDENTIFIER (context)))
1971 tree subfield = lookup_anon_field (basetype, context);
1972 tree subdatum = build_component_ref (datum, subfield,
1973 basetype_path, protect);
1974 return build_component_ref (subdatum, field, basetype_path, protect);
1978 ref = fold (build (COMPONENT_REF, TREE_TYPE (field),
1979 break_out_cleanups (datum), field));
1981 if (TREE_READONLY (datum) || TREE_READONLY (field))
1982 TREE_READONLY (ref) = 1;
1983 if (TREE_THIS_VOLATILE (datum) || TREE_THIS_VOLATILE (field))
1984 TREE_THIS_VOLATILE (ref) = 1;
1985 if (DECL_MUTABLE_P (field))
1986 TREE_READONLY (ref) = 0;
1991 /* Given an expression PTR for a pointer, return an expression
1992 for the value pointed to.
1993 ERRORSTRING is the name of the operator to appear in error messages.
1995 This function may need to overload OPERATOR_FNNAME.
1996 Must also handle REFERENCE_TYPEs for C++. */
1999 build_x_indirect_ref (ptr, errorstring)
2005 if (current_template_parms)
2006 return build_min_nt (INDIRECT_REF, ptr);
2008 rval = build_opfncall (INDIRECT_REF, LOOKUP_NORMAL, ptr, NULL_TREE, NULL_TREE);
2011 return build_indirect_ref (ptr, errorstring);
2015 build_indirect_ref (ptr, errorstring)
2019 register tree pointer = (TREE_CODE (TREE_TYPE (ptr)) == REFERENCE_TYPE ?
2020 ptr : default_conversion (ptr));
2021 register tree type = TREE_TYPE (pointer);
2023 if (ptr == current_class_decl)
2026 if (IS_AGGR_TYPE (type))
2028 ptr = build_expr_type_conversion (WANT_POINTER, pointer, 1);
2033 type = TREE_TYPE (pointer);
2037 if (TREE_CODE (type) == POINTER_TYPE || TREE_CODE (type) == REFERENCE_TYPE)
2039 if (TREE_CODE (pointer) == ADDR_EXPR
2040 && (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_OPERAND (pointer, 0)))
2041 == TYPE_MAIN_VARIANT (TREE_TYPE (type)))
2042 && (TREE_READONLY (TREE_OPERAND (pointer, 0))
2043 == TYPE_READONLY (TREE_TYPE (type)))
2044 && (TREE_THIS_VOLATILE (TREE_OPERAND (pointer, 0))
2045 == TYPE_VOLATILE (TREE_TYPE (type))))
2046 return TREE_OPERAND (pointer, 0);
2049 tree t = TREE_TYPE (type);
2050 register tree ref = build1 (INDIRECT_REF,
2051 TYPE_MAIN_VARIANT (t), pointer);
2053 TREE_READONLY (ref) = TYPE_READONLY (t);
2054 TREE_THIS_VOLATILE (ref) = TYPE_VOLATILE (t);
2055 TREE_SIDE_EFFECTS (ref)
2056 = TYPE_VOLATILE (t) || TREE_SIDE_EFFECTS (pointer);
2060 /* `pointer' won't be an error_mark_node if we were given a
2061 pointer to member, so it's cool to check for this here. */
2062 else if (TYPE_PTRMEMFUNC_P (type))
2063 error ("invalid use of `%s' on pointer to member function", errorstring);
2064 else if (TREE_CODE (type) == RECORD_TYPE
2065 && (IS_SIGNATURE_POINTER (type) || IS_SIGNATURE_REFERENCE (type)))
2066 error ("cannot dereference signature pointer/reference");
2067 else if (pointer != error_mark_node)
2070 error ("invalid type argument of `%s'", errorstring);
2072 error ("invalid type argument");
2074 return error_mark_node;
2077 /* This handles expressions of the form "a[i]", which denotes
2080 This is logically equivalent in C to *(a+i), but we may do it differently.
2081 If A is a variable or a member, we generate a primitive ARRAY_REF.
2082 This avoids forcing the array out of registers, and can work on
2083 arrays that are not lvalues (for example, members of structures returned
2086 If INDEX is of some user-defined type, it must be converted to
2087 integer type. Otherwise, to make a compatible PLUS_EXPR, it
2088 will inherit the type of the array, which will be some pointer type. */
2091 build_x_array_ref (array, index)
2094 tree rval = build_opfncall (ARRAY_REF, LOOKUP_NORMAL, array, index, NULL_TREE);
2097 return build_array_ref (array, index);
2101 build_array_ref (array, idx)
2106 error ("subscript missing in array reference");
2107 return error_mark_node;
2110 if (TREE_TYPE (array) == error_mark_node
2111 || TREE_TYPE (idx) == error_mark_node)
2112 return error_mark_node;
2114 if (TREE_CODE (TREE_TYPE (array)) == ARRAY_TYPE
2115 && TREE_CODE (array) != INDIRECT_REF)
2119 /* Subscripting with type char is likely to lose
2120 on a machine where chars are signed.
2121 So warn on any machine, but optionally.
2122 Don't warn for unsigned char since that type is safe.
2123 Don't warn for signed char because anyone who uses that
2124 must have done so deliberately. */
2125 if (warn_char_subscripts
2126 && TYPE_MAIN_VARIANT (TREE_TYPE (idx)) == char_type_node)
2127 warning ("array subscript has type `char'");
2129 /* Apply default promotions *after* noticing character types. */
2130 idx = default_conversion (idx);
2132 if (TREE_CODE (TREE_TYPE (idx)) != INTEGER_TYPE)
2134 error ("array subscript is not an integer");
2135 return error_mark_node;
2138 /* An array that is indexed by a non-constant
2139 cannot be stored in a register; we must be able to do
2140 address arithmetic on its address.
2141 Likewise an array of elements of variable size. */
2142 if (TREE_CODE (idx) != INTEGER_CST
2143 || (TYPE_SIZE (TREE_TYPE (TREE_TYPE (array))) != 0
2144 && TREE_CODE (TYPE_SIZE (TREE_TYPE (TREE_TYPE (array)))) != INTEGER_CST))
2146 if (mark_addressable (array) == 0)
2147 return error_mark_node;
2149 /* An array that is indexed by a constant value which is not within
2150 the array bounds cannot be stored in a register either; because we
2151 would get a crash in store_bit_field/extract_bit_field when trying
2152 to access a non-existent part of the register. */
2153 if (TREE_CODE (idx) == INTEGER_CST
2154 && TYPE_VALUES (TREE_TYPE (array))
2155 && ! int_fits_type_p (idx, TYPE_VALUES (TREE_TYPE (array))))
2157 if (mark_addressable (array) == 0)
2158 return error_mark_node;
2161 if (pedantic && !lvalue_p (array))
2162 pedwarn ("ANSI C++ forbids subscripting non-lvalue array");
2164 /* Note in C++ it is valid to subscript a `register' array, since
2165 it is valid to take the address of something with that
2166 storage specification. */
2170 while (TREE_CODE (foo) == COMPONENT_REF)
2171 foo = TREE_OPERAND (foo, 0);
2172 if (TREE_CODE (foo) == VAR_DECL && DECL_REGISTER (foo))
2173 warning ("subscripting array declared `register'");
2176 type = TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (array)));
2177 rval = build (ARRAY_REF, type, array, idx);
2178 /* Array ref is const/volatile if the array elements are
2179 or if the array is.. */
2180 TREE_READONLY (rval)
2181 |= (TYPE_READONLY (TREE_TYPE (TREE_TYPE (array)))
2182 | TREE_READONLY (array));
2183 TREE_SIDE_EFFECTS (rval)
2184 |= (TYPE_VOLATILE (TREE_TYPE (TREE_TYPE (array)))
2185 | TREE_SIDE_EFFECTS (array));
2186 TREE_THIS_VOLATILE (rval)
2187 |= (TYPE_VOLATILE (TREE_TYPE (TREE_TYPE (array)))
2188 /* This was added by rms on 16 Nov 91.
2189 It fixes vol struct foo *a; a->elts[1]
2190 in an inline function.
2191 Hope it doesn't break something else. */
2192 | TREE_THIS_VOLATILE (array));
2193 return require_complete_type (fold (rval));
2197 tree ar = default_conversion (array);
2198 tree ind = default_conversion (idx);
2200 /* Put the integer in IND to simplify error checking. */
2201 if (TREE_CODE (TREE_TYPE (ar)) == INTEGER_TYPE)
2208 if (ar == error_mark_node)
2211 if (TREE_CODE (TREE_TYPE (ar)) != POINTER_TYPE)
2213 error ("subscripted value is neither array nor pointer");
2214 return error_mark_node;
2216 if (TREE_CODE (TREE_TYPE (ind)) != INTEGER_TYPE)
2218 error ("array subscript is not an integer");
2219 return error_mark_node;
2222 return build_indirect_ref (build_binary_op_nodefault (PLUS_EXPR, ar, ind, PLUS_EXPR),
2227 /* Build a function call to function FUNCTION with parameters PARAMS.
2228 PARAMS is a list--a chain of TREE_LIST nodes--in which the
2229 TREE_VALUE of each node is a parameter-expression.
2230 FUNCTION's data type may be a function type or a pointer-to-function.
2232 For C++: If FUNCTION's data type is a TREE_LIST, then the tree list
2233 is the list of possible methods that FUNCTION could conceivably
2234 be. If the list of methods comes from a class, then it will be
2235 a list of lists (where each element is associated with the class
2236 that produced it), otherwise it will be a simple list (for
2237 functions overloaded in global scope).
2239 In the first case, TREE_VALUE (function) is the head of one of those
2240 lists, and TREE_PURPOSE is the name of the function.
2242 In the second case, TREE_PURPOSE (function) is the function's
2245 DECL is the class instance variable, usually CURRENT_CLASS_DECL. */
2248 * [eichin:19911015.1726EST] actually return a possibly incomplete
2252 build_x_function_call (function, params, decl)
2253 tree function, params, decl;
2258 if (function == error_mark_node)
2259 return error_mark_node;
2261 if (current_template_parms)
2262 return build_min_nt (CALL_EXPR, function, params, 0);
2264 type = TREE_TYPE (function);
2266 if (TREE_CODE (type) == OFFSET_TYPE
2267 && TREE_TYPE (type) == unknown_type_node
2268 && TREE_CODE (function) == TREE_LIST
2269 && TREE_CHAIN (function) == NULL_TREE)
2271 /* Undo (Foo:bar)()... */
2272 type = TYPE_OFFSET_BASETYPE (type);
2273 function = TREE_VALUE (function);
2274 my_friendly_assert (TREE_CODE (function) == TREE_LIST, 999);
2275 my_friendly_assert (TREE_CHAIN (function) == NULL_TREE, 999);
2276 function = TREE_VALUE (function);
2277 my_friendly_assert (TREE_CODE (function) == FUNCTION_DECL, 999);
2278 function = DECL_NAME (function);
2279 return build_method_call (decl, function, params, TYPE_BINFO (type), LOOKUP_NORMAL);
2282 is_method = ((TREE_CODE (function) == TREE_LIST
2283 && current_class_type != NULL_TREE
2284 && IDENTIFIER_CLASS_VALUE (TREE_PURPOSE (function)) == function)
2285 || TREE_CODE (function) == IDENTIFIER_NODE
2286 || TREE_CODE (type) == METHOD_TYPE
2287 || TYPE_PTRMEMFUNC_P (type));
2289 if (TREE_CODE (function) == FUNCTION_DECL
2290 && DECL_STATIC_FUNCTION_P (function))
2291 return build_member_call
2292 (DECL_CONTEXT (function), DECL_NAME (function), params);
2294 /* Handle methods, friends, and overloaded functions, respectively. */
2297 if (TREE_CODE (function) == FUNCTION_DECL)
2299 if (DECL_NAME (function))
2300 function = DECL_NAME (function);
2302 function = TYPE_IDENTIFIER (DECL_CLASS_CONTEXT (function));
2304 else if (TREE_CODE (function) == TREE_LIST)
2306 my_friendly_assert (TREE_CODE (TREE_VALUE (function)) == FUNCTION_DECL, 312);
2307 function = TREE_PURPOSE (function);
2309 else if (TREE_CODE (function) != IDENTIFIER_NODE)
2311 if (TREE_CODE (function) == OFFSET_REF)
2313 if (TREE_OPERAND (function, 0))
2314 decl = TREE_OPERAND (function, 0);
2316 /* Call via a pointer to member function. */
2317 if (decl == NULL_TREE)
2319 error ("pointer to member function called, but not in class scope");
2320 return error_mark_node;
2322 /* What other type of POINTER_TYPE could this be? */
2323 if (TREE_CODE (TREE_TYPE (function)) != POINTER_TYPE
2324 && ! TYPE_PTRMEMFUNC_P (TREE_TYPE (function))
2325 && TREE_CODE (function) != OFFSET_REF)
2326 function = build (OFFSET_REF, TREE_TYPE (type), NULL_TREE, function);
2330 /* this is an abbreviated method call.
2331 must go through here in case it is a virtual function.
2332 @@ Perhaps this could be optimized. */
2334 if (decl == NULL_TREE)
2336 if (current_class_type == NULL_TREE)
2338 error ("object missing in call to method `%s'",
2339 IDENTIFIER_POINTER (function));
2340 return error_mark_node;
2342 /* Yow: call from a static member function. */
2343 decl = build1 (NOP_EXPR, build_pointer_type (current_class_type),
2345 decl = build_indirect_ref (decl, NULL_PTR);
2348 return build_method_call (decl, function, params,
2349 NULL_TREE, LOOKUP_NORMAL);
2351 else if (TREE_CODE (function) == COMPONENT_REF
2352 && type == unknown_type_node)
2354 /* Should we undo what was done in build_component_ref? */
2355 if (TREE_CODE (TREE_PURPOSE (TREE_OPERAND (function, 1))) == TREE_VEC)
2356 /* Get the name that build_component_ref hid. */
2357 function = DECL_NAME (TREE_VALUE (TREE_OPERAND (function, 1)));
2359 function = TREE_PURPOSE (TREE_OPERAND (function, 1));
2360 return build_method_call (decl, function, params,
2361 NULL_TREE, LOOKUP_NORMAL);
2363 else if (TREE_CODE (function) == TREE_LIST)
2365 if (TREE_VALUE (function) == NULL_TREE)
2367 cp_error ("function `%D' declared overloaded, but no definitions appear with which to resolve it?!?",
2368 TREE_PURPOSE (function));
2369 return error_mark_node;
2373 tree val = TREE_VALUE (function);
2375 if (TREE_CODE (val) == TEMPLATE_DECL)
2376 return build_overload_call_maybe
2377 (function, params, LOOKUP_COMPLAIN, (struct candidate *)0);
2378 else if (DECL_CHAIN (val) != NULL_TREE)
2379 return build_overload_call
2380 (function, params, LOOKUP_COMPLAIN, (struct candidate *)0);
2382 my_friendly_abort (360);
2387 if (TREE_CODE (function) == OFFSET_REF)
2389 /* If the component is a data element (or a virtual function), we play
2390 games here to make things work. */
2393 if (TREE_OPERAND (function, 0))
2394 decl = TREE_OPERAND (function, 0);
2398 decl_addr = build_unary_op (ADDR_EXPR, decl, 0);
2399 function = get_member_function_from_ptrfunc (&decl_addr,
2400 TREE_OPERAND (function, 1));
2401 params = tree_cons (NULL_TREE, decl_addr, params);
2402 return build_function_call (function, params);
2405 type = TREE_TYPE (function);
2406 if (type != error_mark_node)
2408 if (TREE_CODE (type) == REFERENCE_TYPE)
2409 type = TREE_TYPE (type);
2411 if (TYPE_LANG_SPECIFIC (type)
2412 && TYPE_OVERLOADS_CALL_EXPR (complete_type (type)))
2413 return build_opfncall (CALL_EXPR, LOOKUP_NORMAL, function, params, NULL_TREE);
2418 tree fntype = TREE_TYPE (function);
2421 /* Explicitly named method? */
2422 if (TREE_CODE (function) == FUNCTION_DECL)
2423 ctypeptr = build_pointer_type (DECL_CLASS_CONTEXT (function));
2424 /* Expression with ptr-to-method type? It could either be a plain
2425 usage, or it might be a case where the ptr-to-method is being
2426 passed in as an argument. */
2427 else if (TYPE_PTRMEMFUNC_P (fntype))
2429 tree rec = TYPE_METHOD_BASETYPE (TREE_TYPE (TYPE_PTRMEMFUNC_FN_TYPE (fntype)));
2430 ctypeptr = build_pointer_type (rec);
2432 /* Unexpected node type? */
2434 my_friendly_abort (116);
2435 if (decl == NULL_TREE)
2437 if (current_function_decl
2438 && DECL_STATIC_FUNCTION_P (current_function_decl))
2439 error ("invalid call to member function needing `this' in static member function scope");
2441 error ("pointer to member function called, but not in class scope");
2442 return error_mark_node;
2444 if (TREE_CODE (TREE_TYPE (decl)) != POINTER_TYPE
2445 && ! TYPE_PTRMEMFUNC_P (TREE_TYPE (decl)))
2447 decl = build_unary_op (ADDR_EXPR, decl, 0);
2448 decl = convert_pointer_to (TREE_TYPE (ctypeptr), decl);
2451 decl = build_c_cast (ctypeptr, decl, 0);
2452 params = tree_cons (NULL_TREE, decl, params);
2455 return build_function_call (function, params);
2458 /* Resolve a pointer to member function. INSTANCE is the object
2459 instance to use, if the member points to a virtual member. */
2462 get_member_function_from_ptrfunc (instance_ptrptr, function)
2463 tree *instance_ptrptr;
2466 if (TREE_CODE (function) == OFFSET_REF)
2468 function = TREE_OPERAND (function, 1);
2471 if (TYPE_PTRMEMFUNC_P (TREE_TYPE (function)))
2473 tree fntype, index, e1, delta, delta2, e2, e3, aref, vtbl;
2476 tree instance_ptr = *instance_ptrptr;
2478 if (TREE_SIDE_EFFECTS (instance_ptr))
2479 instance_ptr = save_expr (instance_ptr);
2481 if (TREE_SIDE_EFFECTS (function))
2482 function = save_expr (function);
2484 fntype = TYPE_PTRMEMFUNC_FN_TYPE (TREE_TYPE (function));
2485 index = save_expr (build_component_ref (function,
2488 e1 = build (GT_EXPR, boolean_type_node, index,
2489 convert (delta_type_node, integer_zero_node));
2490 delta = convert (ptrdiff_type_node,
2491 build_component_ref (function, delta_identifier, 0, 0));
2492 delta2 = DELTA2_FROM_PTRMEMFUNC (function);
2494 /* convert down to the right base, before using the instance. */
2496 = convert_pointer_to_real (TYPE_METHOD_BASETYPE (TREE_TYPE (fntype)),
2498 if (instance == error_mark_node)
2501 vtbl = convert_pointer_to (ptr_type_node, instance);
2504 build_pointer_type (build_pointer_type (vtable_entry_type)),
2505 vtbl, convert (ptrdiff_type_node, delta2));
2506 vtbl = build_indirect_ref (vtbl, NULL_PTR);
2507 aref = build_array_ref (vtbl, build_binary_op (MINUS_EXPR,
2509 integer_one_node, 1));
2510 if (! flag_vtable_thunks)
2512 aref = save_expr (aref);
2514 delta = build_binary_op (PLUS_EXPR,
2515 build_conditional_expr (e1, build_component_ref (aref, delta_identifier, 0, 0), integer_zero_node),
2519 *instance_ptrptr = build (PLUS_EXPR, TREE_TYPE (instance_ptr),
2520 instance_ptr, delta);
2521 if (flag_vtable_thunks)
2524 e2 = build_component_ref (aref, pfn_identifier, 0, 0);
2526 e3 = PFN_FROM_PTRMEMFUNC (function);
2527 TREE_TYPE (e2) = TREE_TYPE (e3);
2528 function = build_conditional_expr (e1, e2, e3);
2530 /* Make sure this doesn't get evaluated first inside one of the
2531 branches of the COND_EXPR. */
2532 if (TREE_CODE (instance_ptr) == SAVE_EXPR)
2533 function = build (COMPOUND_EXPR, TREE_TYPE (function),
2534 instance_ptr, function);
2540 build_function_call_real (function, params, require_complete, flags)
2541 tree function, params;
2542 int require_complete, flags;
2544 register tree fntype, fndecl;
2545 register tree value_type;
2546 register tree coerced_params;
2547 tree name = NULL_TREE, assembler_name = NULL_TREE;
2550 /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
2551 Strip such NOP_EXPRs, since FUNCTION is used in non-lvalue context. */
2552 if (TREE_CODE (function) == NOP_EXPR
2553 && TREE_TYPE (function) == TREE_TYPE (TREE_OPERAND (function, 0)))
2554 function = TREE_OPERAND (function, 0);
2556 if (TREE_CODE (function) == FUNCTION_DECL)
2558 name = DECL_NAME (function);
2559 assembler_name = DECL_ASSEMBLER_NAME (function);
2561 GNU_xref_call (current_function_decl,
2562 IDENTIFIER_POINTER (name ? name
2563 : TYPE_IDENTIFIER (DECL_CLASS_CONTEXT (function))));
2564 mark_used (function);
2567 /* Convert anything with function type to a pointer-to-function. */
2570 && IDENTIFIER_LENGTH (name) == 4
2571 && ! strcmp (IDENTIFIER_POINTER (name), "main")
2572 && DECL_CONTEXT (function) == NULL_TREE)
2574 pedwarn ("ANSI C++ forbids calling `main' from within program");
2577 if (pedantic && DECL_THIS_INLINE (function) && ! DECL_INITIAL (function)
2578 && ! DECL_ARTIFICIAL (function)
2579 && ! DECL_PENDING_INLINE_INFO (function))
2580 cp_pedwarn ("inline function `%#D' called before definition",
2583 /* Differs from default_conversion by not setting TREE_ADDRESSABLE
2584 (because calling an inline function does not mean the function
2585 needs to be separately compiled). */
2587 if (DECL_INLINE (function))
2589 /* Is it a synthesized method that needs to be synthesized? */
2590 if (DECL_ARTIFICIAL (function) && ! DECL_INITIAL (function)
2591 /* Kludge: don't synthesize for default args. */
2592 && current_function_decl)
2593 synthesize_method (function);
2595 function = inline_conversion (function);
2598 function = default_conversion (function);
2604 /* Convert anything with function type to a pointer-to-function. */
2605 if (function == error_mark_node)
2606 return error_mark_node;
2607 function = default_conversion (function);
2610 fntype = TREE_TYPE (function);
2612 if (TYPE_PTRMEMFUNC_P (fntype))
2614 tree instance_ptr = build_unary_op (ADDR_EXPR, C_C_D, 0);
2615 fntype = TYPE_PTRMEMFUNC_FN_TYPE (fntype);
2616 function = get_member_function_from_ptrfunc (&instance_ptr, function);
2619 is_method = (TREE_CODE (fntype) == POINTER_TYPE
2620 && TREE_CODE (TREE_TYPE (fntype)) == METHOD_TYPE);
2622 if (!((TREE_CODE (fntype) == POINTER_TYPE
2623 && TREE_CODE (TREE_TYPE (fntype)) == FUNCTION_TYPE)
2626 cp_error ("`%E' cannot be used as a function", function);
2627 return error_mark_node;
2630 /* fntype now gets the type of function pointed to. */
2631 fntype = TREE_TYPE (fntype);
2633 /* Convert the parameters to the types declared in the
2634 function prototype, or apply default promotions. */
2636 if (flags & LOOKUP_COMPLAIN)
2637 coerced_params = convert_arguments (NULL_TREE, TYPE_ARG_TYPES (fntype),
2638 params, fndecl, LOOKUP_NORMAL);
2640 coerced_params = convert_arguments (NULL_TREE, TYPE_ARG_TYPES (fntype),
2643 if (coerced_params == error_mark_node)
2644 if (flags & LOOKUP_SPECULATIVELY)
2647 return error_mark_node;
2649 /* Check for errors in format strings. */
2651 if (warn_format && (name || assembler_name))
2652 check_function_format (name, assembler_name, coerced_params);
2654 /* Recognize certain built-in functions so we can make tree-codes
2655 other than CALL_EXPR. We do this when it enables fold-const.c
2656 to do something useful. */
2658 if (TREE_CODE (function) == ADDR_EXPR
2659 && TREE_CODE (TREE_OPERAND (function, 0)) == FUNCTION_DECL
2660 && DECL_BUILT_IN (TREE_OPERAND (function, 0)))
2661 switch (DECL_FUNCTION_CODE (TREE_OPERAND (function, 0)))
2666 if (coerced_params == 0)
2667 return integer_zero_node;
2668 return build_unary_op (ABS_EXPR, TREE_VALUE (coerced_params), 0);
2672 value_type = TREE_TYPE (fntype) ? TREE_TYPE (fntype) : void_type_node;
2674 register tree result =
2675 build (CALL_EXPR, value_type,
2676 function, coerced_params, NULL_TREE);
2678 TREE_SIDE_EFFECTS (result) = 1;
2680 if (! require_complete)
2681 return convert_from_reference (result);
2682 if (value_type == void_type_node)
2684 result = require_complete_type (result);
2685 return convert_from_reference (result);
2690 build_function_call (function, params)
2691 tree function, params;
2693 return build_function_call_real (function, params, 1, LOOKUP_NORMAL);
2696 /* Convert the actual parameter expressions in the list VALUES
2697 to the types in the list TYPELIST.
2698 If parmdecls is exhausted, or when an element has NULL as its type,
2699 perform the default conversions.
2701 RETURN_LOC is the location of the return value, if known, NULL_TREE
2702 otherwise. This is useful in the case where we can avoid creating
2703 a temporary variable in the case where we can initialize the return
2704 value directly. If we are not eliding constructors, then we set this
2705 to NULL_TREE to avoid this avoidance.
2707 NAME is an IDENTIFIER_NODE or 0. It is used only for error messages.
2709 This is also where warnings about wrong number of args are generated.
2711 Return a list of expressions for the parameters as converted.
2713 Both VALUES and the returned value are chains of TREE_LIST nodes
2714 with the elements of the list in the TREE_VALUE slots of those nodes.
2716 In C++, unspecified trailing parameters can be filled in with their
2717 default arguments, if such were specified. Do so here. */
2720 convert_arguments (return_loc, typelist, values, fndecl, flags)
2721 tree return_loc, typelist, values, fndecl;
2724 register tree typetail, valtail;
2725 register tree result = NULL_TREE;
2729 if (! flag_elide_constructors)
2734 if (TREE_CODE (TREE_TYPE (fndecl)) == METHOD_TYPE)
2736 if (DECL_NAME (fndecl) == NULL_TREE
2737 || IDENTIFIER_HAS_TYPE_VALUE (DECL_NAME (fndecl)))
2738 called_thing = "constructor";
2740 called_thing = "member function";
2743 called_thing = "function";
2746 for (valtail = values, typetail = typelist;
2748 valtail = TREE_CHAIN (valtail), i++)
2750 register tree type = typetail ? TREE_VALUE (typetail) : 0;
2751 register tree val = TREE_VALUE (valtail);
2753 if (val == error_mark_node)
2754 return error_mark_node;
2756 if (type == void_type_node)
2760 cp_error_at ("too many arguments to %s `%+D'", called_thing,
2762 error ("at this point in file");
2765 error ("too many arguments to function");
2766 /* In case anybody wants to know if this argument
2769 TREE_TYPE (tree_last (result)) = error_mark_node;
2773 /* The tree type of the parameter being passed may not yet be
2774 known. In this case, its type is TYPE_UNKNOWN, and will
2775 be instantiated by the type given by TYPE. If TYPE
2776 is also NULL, the tree type of VAL is ERROR_MARK_NODE. */
2777 if (type && type_unknown_p (val))
2778 val = require_instantiated_type (type, val, integer_zero_node);
2779 else if (type_unknown_p (val))
2781 /* Strip the `&' from an overloaded FUNCTION_DECL. */
2782 if (TREE_CODE (val) == ADDR_EXPR)
2783 val = TREE_OPERAND (val, 0);
2784 if (TREE_CODE (val) == TREE_LIST
2785 && TREE_CHAIN (val) == NULL_TREE
2786 && TREE_TYPE (TREE_VALUE (val)) != NULL_TREE
2787 && (TREE_TYPE (val) == unknown_type_node
2788 || DECL_CHAIN (TREE_VALUE (val)) == NULL_TREE))
2789 /* Instantiates automatically. */
2790 val = TREE_VALUE (val);
2793 error ("insufficient type information in parameter list");
2794 val = integer_zero_node;
2797 else if (TREE_CODE (val) == OFFSET_REF
2798 && TREE_CODE (TREE_TYPE (val)) == METHOD_TYPE)
2800 /* This is unclean. Should be handled elsewhere. */
2801 val = build_unary_op (ADDR_EXPR, val, 0);
2803 else if (TREE_CODE (val) == OFFSET_REF)
2804 val = resolve_offset_ref (val);
2806 /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
2807 Strip such NOP_EXPRs, since VAL is used in non-lvalue context. */
2808 if (TREE_CODE (val) == NOP_EXPR
2809 && TREE_TYPE (val) == TREE_TYPE (TREE_OPERAND (val, 0))
2810 && (type == 0 || TREE_CODE (type) != REFERENCE_TYPE))
2811 val = TREE_OPERAND (val, 0);
2813 if (type == 0 || TREE_CODE (type) != REFERENCE_TYPE)
2815 if (TREE_CODE (TREE_TYPE (val)) == ARRAY_TYPE
2816 || TREE_CODE (TREE_TYPE (val)) == FUNCTION_TYPE
2817 || TREE_CODE (TREE_TYPE (val)) == METHOD_TYPE)
2818 val = default_conversion (val);
2820 val = require_complete_type (val);
2823 if (val == error_mark_node)
2824 return error_mark_node;
2828 /* Formal parm type is specified by a function prototype. */
2831 if (TYPE_SIZE (complete_type (type)) == 0)
2833 error ("parameter type of called function is incomplete");
2838 parmval = convert_for_initialization (return_loc, type, val,
2839 flags|INDIRECT_BIND,
2840 "argument passing", fndecl, i);
2841 #ifdef PROMOTE_PROTOTYPES
2842 if ((TREE_CODE (type) == INTEGER_TYPE
2843 || TREE_CODE (type) == ENUMERAL_TYPE)
2844 && (TYPE_PRECISION (type) < TYPE_PRECISION (integer_type_node)))
2845 parmval = default_conversion (parmval);
2849 if (parmval == error_mark_node)
2850 return error_mark_node;
2852 result = tree_cons (NULL_TREE, parmval, result);
2856 if (TREE_CODE (TREE_TYPE (val)) == REFERENCE_TYPE)
2857 val = convert_from_reference (val);
2859 if (TREE_CODE (TREE_TYPE (val)) == REAL_TYPE
2860 && (TYPE_PRECISION (TREE_TYPE (val))
2861 < TYPE_PRECISION (double_type_node)))
2862 /* Convert `float' to `double'. */
2863 result = tree_cons (NULL_TREE, convert (double_type_node, val), result);
2864 else if (TYPE_LANG_SPECIFIC (TREE_TYPE (val))
2865 && ! TYPE_HAS_TRIVIAL_INIT_REF (TREE_TYPE (val)))
2867 cp_warning ("cannot pass objects of type `%T' through `...'",
2869 result = tree_cons (NULL_TREE, val, result);
2872 /* Convert `short' and `char' to full-size `int'. */
2873 result = tree_cons (NULL_TREE, default_conversion (val), result);
2877 typetail = TREE_CHAIN (typetail);
2880 if (typetail != 0 && typetail != void_list_node)
2882 /* See if there are default arguments that can be used */
2883 if (TREE_PURPOSE (typetail))
2885 for (; typetail != void_list_node; ++i)
2887 tree type = TREE_VALUE (typetail);
2888 tree val = break_out_target_exprs (TREE_PURPOSE (typetail));
2891 if (val == NULL_TREE)
2892 parmval = error_mark_node;
2893 else if (TREE_CODE (val) == CONSTRUCTOR)
2895 parmval = digest_init (type, val, (tree *)0);
2896 parmval = convert_for_initialization (return_loc, type, parmval, flags,
2897 "default constructor", fndecl, i);
2901 /* This could get clobbered by the following call. */
2902 if (TREE_HAS_CONSTRUCTOR (val))
2903 val = copy_node (val);
2905 parmval = convert_for_initialization (return_loc, type, val, flags,
2906 "default argument", fndecl, i);
2907 #ifdef PROMOTE_PROTOTYPES
2908 if ((TREE_CODE (type) == INTEGER_TYPE
2909 || TREE_CODE (type) == ENUMERAL_TYPE)
2910 && (TYPE_PRECISION (type) < TYPE_PRECISION (integer_type_node)))
2911 parmval = default_conversion (parmval);
2915 if (parmval == error_mark_node)
2916 return error_mark_node;
2918 result = tree_cons (0, parmval, result);
2919 typetail = TREE_CHAIN (typetail);
2920 /* ends with `...'. */
2921 if (typetail == NULL_TREE)
2929 char *buf = (char *)alloca (32 + strlen (called_thing));
2930 sprintf (buf, "too few arguments to %s `%%#D'", called_thing);
2931 cp_error_at (buf, fndecl);
2932 error ("at this point in file");
2935 error ("too few arguments to function");
2936 return error_mark_list;
2940 return nreverse (result);
2943 /* Build a binary-operation expression, after performing default
2944 conversions on the operands. CODE is the kind of expression to build. */
2947 build_x_binary_op (code, arg1, arg2)
2948 enum tree_code code;
2953 if (current_template_parms)
2954 return build_min_nt (code, arg1, arg2);
2956 rval = build_opfncall (code, LOOKUP_SPECULATIVELY,
2957 arg1, arg2, NULL_TREE);
2959 return build_opfncall (code, LOOKUP_NORMAL, arg1, arg2, NULL_TREE);
2960 if (code == MEMBER_REF)
2961 return build_m_component_ref (build_indirect_ref (arg1, NULL_PTR),
2963 return build_binary_op (code, arg1, arg2, 1);
2967 build_binary_op (code, arg1, arg2, convert_p)
2968 enum tree_code code;
2980 args[0] = decay_conversion (args[0]);
2981 args[1] = decay_conversion (args[1]);
2983 if (args[0] == error_mark_node || args[1] == error_mark_node)
2984 return error_mark_node;
2986 type0 = TREE_TYPE (args[0]);
2987 type1 = TREE_TYPE (args[1]);
2989 if (type_unknown_p (args[0]))
2991 args[0] = instantiate_type (type1, args[0], 1);
2992 args[0] = decay_conversion (args[0]);
2994 else if (type_unknown_p (args[1]))
2996 args[1] = require_instantiated_type (type0, args[1],
2998 args[1] = decay_conversion (args[1]);
3001 if (IS_AGGR_TYPE (type0) || IS_AGGR_TYPE (type1))
3003 /* Try to convert this to something reasonable. */
3004 if (! build_default_binary_type_conversion(code, &args[0], &args[1]))
3006 cp_error ("no match for `%O(%#T, %#T)'", code,
3007 TREE_TYPE (arg1), TREE_TYPE (arg2));
3008 return error_mark_node;
3012 return build_binary_op_nodefault (code, args[0], args[1], code);
3015 /* Build a binary-operation expression without default conversions.
3016 CODE is the kind of expression to build.
3017 This function differs from `build' in several ways:
3018 the data type of the result is computed and recorded in it,
3019 warnings are generated if arg data types are invalid,
3020 special handling for addition and subtraction of pointers is known,
3021 and some optimization is done (operations on narrow ints
3022 are done in the narrower type when that gives the same result).
3023 Constant folding is also done before the result is returned.
3025 ERROR_CODE is the code that determines what to say in error messages.
3026 It is usually, but not always, the same as CODE.
3028 Note that the operands will never have enumeral types
3029 because either they have just had the default conversions performed
3030 or they have both just been converted to some other type in which
3031 the arithmetic is to be done.
3033 C++: must do special pointer arithmetic when implementing
3034 multiple inheritance, and deal with pointer to member functions. */
3037 build_binary_op_nodefault (code, orig_op0, orig_op1, error_code)
3038 enum tree_code code;
3039 tree orig_op0, orig_op1;
3040 enum tree_code error_code;
3043 register enum tree_code code0, code1;
3046 /* Expression code to give to the expression when it is built.
3047 Normally this is CODE, which is what the caller asked for,
3048 but in some special cases we change it. */
3049 register enum tree_code resultcode = code;
3051 /* Data type in which the computation is to be performed.
3052 In the simplest cases this is the common type of the arguments. */
3053 register tree result_type = NULL;
3055 /* Nonzero means operands have already been type-converted
3056 in whatever way is necessary.
3057 Zero means they need to be converted to RESULT_TYPE. */
3060 /* Nonzero means create the expression with this type, rather than
3062 tree build_type = 0;
3064 /* Nonzero means after finally constructing the expression
3065 convert it to this type. */
3066 tree final_type = 0;
3068 /* Nonzero if this is an operation like MIN or MAX which can
3069 safely be computed in short if both args are promoted shorts.
3070 Also implies COMMON.
3071 -1 indicates a bitwise operation; this makes a difference
3072 in the exact conditions for when it is safe to do the operation
3073 in a narrower mode. */
3076 /* Nonzero if this is a comparison operation;
3077 if both args are promoted shorts, compare the original shorts.
3078 Also implies COMMON. */
3079 int short_compare = 0;
3081 /* Nonzero if this is a right-shift operation, which can be computed on the
3082 original short and then promoted if the operand is a promoted short. */
3083 int short_shift = 0;
3085 /* Nonzero means set RESULT_TYPE to the common type of the args. */
3088 /* Apply default conversions. */
3089 if (code == TRUTH_AND_EXPR || code == TRUTH_ANDIF_EXPR
3090 || code == TRUTH_OR_EXPR || code == TRUTH_ORIF_EXPR
3091 || code == TRUTH_XOR_EXPR)
3093 op0 = decay_conversion (orig_op0);
3094 op1 = decay_conversion (orig_op1);
3098 op0 = default_conversion (orig_op0);
3099 op1 = default_conversion (orig_op1);
3102 type0 = TREE_TYPE (op0);
3103 type1 = TREE_TYPE (op1);
3105 /* The expression codes of the data types of the arguments tell us
3106 whether the arguments are integers, floating, pointers, etc. */
3107 code0 = TREE_CODE (type0);
3108 code1 = TREE_CODE (type1);
3110 /* Strip NON_LVALUE_EXPRs, etc., since we aren't using as an lvalue. */
3111 STRIP_TYPE_NOPS (op0);
3112 STRIP_TYPE_NOPS (op1);
3114 /* If an error was already reported for one of the arguments,
3115 avoid reporting another error. */
3117 if (code0 == ERROR_MARK || code1 == ERROR_MARK)
3118 return error_mark_node;
3123 /* Handle the pointer + int case. */
3124 if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
3125 return pointer_int_sum (PLUS_EXPR, op0, op1);
3126 else if (code1 == POINTER_TYPE && code0 == INTEGER_TYPE)
3127 return pointer_int_sum (PLUS_EXPR, op1, op0);
3133 /* Subtraction of two similar pointers.
3134 We must subtract them as integers, then divide by object size. */
3135 if (code0 == POINTER_TYPE && code1 == POINTER_TYPE
3136 && comp_target_types (type0, type1, 1))
3137 return pointer_diff (op0, op1);
3138 /* Handle pointer minus int. Just like pointer plus int. */
3139 else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
3140 return pointer_int_sum (MINUS_EXPR, op0, op1);
3149 case TRUNC_DIV_EXPR:
3151 case FLOOR_DIV_EXPR:
3152 case ROUND_DIV_EXPR:
3153 case EXACT_DIV_EXPR:
3154 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE)
3155 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE))
3157 if (TREE_CODE (op1) == INTEGER_CST && integer_zerop (op1))
3158 cp_warning ("division by zero in `%E / 0'", op0);
3159 else if (TREE_CODE (op1) == REAL_CST && real_zerop (op1))
3160 cp_warning ("division by zero in `%E / 0.'", op0);
3162 if (!(code0 == INTEGER_TYPE && code1 == INTEGER_TYPE))
3163 resultcode = RDIV_EXPR;
3165 /* When dividing two signed integers, we have to promote to int.
3166 unless we divide by a constant != -1. Note that default
3167 conversion will have been performed on the operands at this
3168 point, so we have to dig out the original type to find out if
3170 shorten = ((TREE_CODE (op0) == NOP_EXPR
3171 && TREE_UNSIGNED (TREE_TYPE (TREE_OPERAND (op0, 0))))
3172 || (TREE_CODE (op1) == INTEGER_CST
3173 && (TREE_INT_CST_LOW (op1) != -1
3174 || TREE_INT_CST_HIGH (op1) != -1)));
3180 case BIT_ANDTC_EXPR:
3183 if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
3185 /* If one operand is a constant, and the other is a short type
3186 that has been converted to an int,
3187 really do the work in the short type and then convert the
3188 result to int. If we are lucky, the constant will be 0 or 1
3189 in the short type, making the entire operation go away. */
3190 if (TREE_CODE (op0) == INTEGER_CST
3191 && TREE_CODE (op1) == NOP_EXPR
3192 && TYPE_PRECISION (type1) > TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (op1, 0)))
3193 && TREE_UNSIGNED (TREE_TYPE (TREE_OPERAND (op1, 0))))
3195 final_type = result_type;
3196 op1 = TREE_OPERAND (op1, 0);
3197 result_type = TREE_TYPE (op1);
3199 if (TREE_CODE (op1) == INTEGER_CST
3200 && TREE_CODE (op0) == NOP_EXPR
3201 && TYPE_PRECISION (type0) > TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (op0, 0)))
3202 && TREE_UNSIGNED (TREE_TYPE (TREE_OPERAND (op0, 0))))
3204 final_type = result_type;
3205 op0 = TREE_OPERAND (op0, 0);
3206 result_type = TREE_TYPE (op0);
3210 case TRUNC_MOD_EXPR:
3211 case FLOOR_MOD_EXPR:
3212 if (code1 == INTEGER_TYPE && integer_zerop (op1))
3213 cp_warning ("division by zero in `%E % 0'", op0);
3214 else if (code1 == REAL_TYPE && real_zerop (op1))
3215 cp_warning ("division by zero in `%E % 0.'", op0);
3217 if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
3219 /* Although it would be tempting to shorten always here, that loses
3220 on some targets, since the modulo instruction is undefined if the
3221 quotient can't be represented in the computation mode. We shorten
3222 only if unsigned or if dividing by something we know != -1. */
3223 shorten = ((TREE_CODE (op0) == NOP_EXPR
3224 && TREE_UNSIGNED (TREE_TYPE (TREE_OPERAND (op0, 0))))
3225 || (TREE_CODE (op1) == INTEGER_CST
3226 && (TREE_INT_CST_LOW (op1) != -1
3227 || TREE_INT_CST_HIGH (op1) != -1)));
3232 case TRUTH_ANDIF_EXPR:
3233 case TRUTH_ORIF_EXPR:
3234 case TRUTH_AND_EXPR:
3236 result_type = boolean_type_node;
3239 /* Shift operations: result has same type as first operand;
3240 always convert second operand to int.
3241 Also set SHORT_SHIFT if shifting rightward. */
3244 if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
3246 result_type = type0;
3247 if (TREE_CODE (op1) == INTEGER_CST)
3249 if (tree_int_cst_lt (op1, integer_zero_node))
3250 warning ("right shift count is negative");
3253 if (TREE_INT_CST_LOW (op1) | TREE_INT_CST_HIGH (op1))
3255 if (TREE_INT_CST_HIGH (op1) != 0
3256 || ((unsigned HOST_WIDE_INT) TREE_INT_CST_LOW (op1)
3257 >= TYPE_PRECISION (type0)))
3258 warning ("right shift count >= width of type");
3261 /* Convert the shift-count to an integer, regardless of
3262 size of value being shifted. */
3263 if (TYPE_MAIN_VARIANT (TREE_TYPE (op1)) != integer_type_node)
3264 op1 = convert (integer_type_node, op1);
3265 /* Avoid converting op1 to result_type later. */
3271 if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
3273 result_type = type0;
3274 if (TREE_CODE (op1) == INTEGER_CST)
3276 if (tree_int_cst_lt (op1, integer_zero_node))
3277 warning ("left shift count is negative");
3278 else if (TREE_INT_CST_HIGH (op1) != 0
3279 || ((unsigned HOST_WIDE_INT) TREE_INT_CST_LOW (op1)
3280 >= TYPE_PRECISION (type0)))
3281 warning ("left shift count >= width of type");
3283 /* Convert the shift-count to an integer, regardless of
3284 size of value being shifted. */
3285 if (TYPE_MAIN_VARIANT (TREE_TYPE (op1)) != integer_type_node)
3286 op1 = convert (integer_type_node, op1);
3287 /* Avoid converting op1 to result_type later. */
3294 if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
3296 result_type = type0;
3297 if (TREE_CODE (op1) == INTEGER_CST)
3299 if (tree_int_cst_lt (op1, integer_zero_node))
3300 warning ("%s rotate count is negative",
3301 (code == LROTATE_EXPR) ? "left" : "right");
3302 else if (TREE_INT_CST_HIGH (op1) != 0
3303 || ((unsigned HOST_WIDE_INT) TREE_INT_CST_LOW (op1)
3304 >= TYPE_PRECISION (type0)))
3305 warning ("%s rotate count >= width of type",
3306 (code == LROTATE_EXPR) ? "left" : "right");
3308 /* Convert the shift-count to an integer, regardless of
3309 size of value being shifted. */
3310 if (TYPE_MAIN_VARIANT (TREE_TYPE (op1)) != integer_type_node)
3311 op1 = convert (integer_type_node, op1);
3317 build_type = boolean_type_node;
3318 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE)
3319 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE))
3321 else if (code0 == POINTER_TYPE && code1 == POINTER_TYPE)
3323 register tree tt0 = TYPE_MAIN_VARIANT (TREE_TYPE (type0));
3324 register tree tt1 = TYPE_MAIN_VARIANT (TREE_TYPE (type1));
3326 if (comp_target_types (type0, type1, 1))
3327 result_type = common_type (type0, type1);
3328 else if (tt0 == void_type_node)
3330 if (pedantic && TREE_CODE (tt1) == FUNCTION_TYPE
3331 && tree_int_cst_lt (TYPE_SIZE (type0), TYPE_SIZE (type1)))
3332 pedwarn ("ANSI C++ forbids comparison of `void *' with function pointer");
3333 else if (TREE_CODE (tt1) == OFFSET_TYPE)
3334 pedwarn ("ANSI C++ forbids conversion of a pointer to member to `void *'");
3336 else if (tt1 == void_type_node)
3338 if (pedantic && TREE_CODE (tt0) == FUNCTION_TYPE
3339 && tree_int_cst_lt (TYPE_SIZE (type1), TYPE_SIZE (type0)))
3340 pedwarn ("ANSI C++ forbids comparison of `void *' with function pointer");
3343 cp_pedwarn ("comparison of distinct pointer types `%T' and `%T' lacks a cast",
3346 if (result_type == NULL_TREE)
3347 result_type = ptr_type_node;
3349 else if (code0 == POINTER_TYPE && TREE_CODE (op1) == INTEGER_CST
3350 && integer_zerop (op1))
3351 result_type = type0;
3352 else if (code1 == POINTER_TYPE && TREE_CODE (op0) == INTEGER_CST
3353 && integer_zerop (op0))
3354 result_type = type1;
3355 else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
3357 result_type = type0;
3358 error ("ANSI C++ forbids comparison between pointer and integer");
3360 else if (code0 == INTEGER_TYPE && code1 == POINTER_TYPE)
3362 result_type = type1;
3363 error ("ANSI C++ forbids comparison between pointer and integer");
3365 else if (TYPE_PTRMEMFUNC_P (type0) && TREE_CODE (op1) == INTEGER_CST
3366 && integer_zerop (op1))
3368 op0 = build_component_ref (op0, index_identifier, 0, 0);
3369 op1 = integer_zero_node;
3370 result_type = TREE_TYPE (op0);
3372 else if (TYPE_PTRMEMFUNC_P (type1) && TREE_CODE (op0) == INTEGER_CST
3373 && integer_zerop (op0))
3375 op0 = build_component_ref (op1, index_identifier, 0, 0);
3376 op1 = integer_zero_node;
3377 result_type = TREE_TYPE (op0);
3379 else if (TYPE_PTRMEMFUNC_P (type0) && TYPE_PTRMEMFUNC_P (type1)
3380 && (TYPE_PTRMEMFUNC_FN_TYPE (type0)
3381 == TYPE_PTRMEMFUNC_FN_TYPE (type1)))
3383 /* The code we generate for the test is:
3385 (op0.index == op1.index
3386 && ((op1.index != -1 && op0.delta2 == op1.delta2)
3387 || op0.pfn == op1.pfn)) */
3389 tree index0 = build_component_ref (op0, index_identifier, 0, 0);
3390 tree index1 = save_expr (build_component_ref (op1, index_identifier, 0, 0));
3391 tree pfn0 = PFN_FROM_PTRMEMFUNC (op0);
3392 tree pfn1 = PFN_FROM_PTRMEMFUNC (op1);
3393 tree delta20 = DELTA2_FROM_PTRMEMFUNC (op0);
3394 tree delta21 = DELTA2_FROM_PTRMEMFUNC (op1);
3396 tree integer_neg_one_node
3397 = build_binary_op (MINUS_EXPR, integer_zero_node, integer_one_node, 1);
3398 e1 = build_binary_op (EQ_EXPR, index0, index1, 1);
3399 e2 = build_binary_op (NE_EXPR, index1, integer_neg_one_node, 1);
3400 e2 = build_binary_op (TRUTH_ANDIF_EXPR, e2, build_binary_op (EQ_EXPR, delta20, delta21, 1), 1);
3401 e3 = build_binary_op (EQ_EXPR, pfn0, pfn1, 1);
3402 e2 = build_binary_op (TRUTH_ORIF_EXPR, e2, e3, 1);
3403 e2 = build_binary_op (TRUTH_ANDIF_EXPR, e1, e2, 1);
3404 if (code == EQ_EXPR)
3406 return build_binary_op (EQ_EXPR, e2, integer_zero_node, 1);
3408 else if (TYPE_PTRMEMFUNC_P (type0)
3409 && TYPE_PTRMEMFUNC_FN_TYPE (type0) == type1)
3411 tree index0 = build_component_ref (op0, index_identifier, 0, 0);
3413 tree pfn0 = PFN_FROM_PTRMEMFUNC (op0);
3414 tree delta20 = DELTA2_FROM_PTRMEMFUNC (op0);
3415 tree delta21 = integer_zero_node;
3417 tree integer_neg_one_node
3418 = build_binary_op (MINUS_EXPR, integer_zero_node, integer_one_node, 1);
3419 if (TREE_CODE (TREE_OPERAND (op1, 0)) == FUNCTION_DECL
3420 && DECL_VINDEX (TREE_OPERAND (op1, 0)))
3422 /* Map everything down one to make room for the null pointer to member. */
3423 index1 = size_binop (PLUS_EXPR,
3424 DECL_VINDEX (TREE_OPERAND (op1, 0)),
3426 op1 = integer_zero_node;
3427 delta21 = CLASSTYPE_VFIELD (TYPE_METHOD_BASETYPE (TREE_TYPE (type1)));
3428 delta21 = DECL_FIELD_BITPOS (delta21);
3429 delta21 = size_binop (FLOOR_DIV_EXPR, delta21, size_int (BITS_PER_UNIT));
3432 index1 = integer_neg_one_node;
3434 tree nop1 = build1 (NOP_EXPR, TYPE_PTRMEMFUNC_FN_TYPE (type0), op1);
3435 TREE_CONSTANT (nop1) = TREE_CONSTANT (op1);
3438 e1 = build_binary_op (EQ_EXPR, index0, index1, 1);
3439 e2 = build_binary_op (NE_EXPR, index1, integer_neg_one_node, 1);
3440 e2 = build_binary_op (TRUTH_ANDIF_EXPR, e2, build_binary_op (EQ_EXPR, delta20, delta21, 1), 1);
3441 e3 = build_binary_op (EQ_EXPR, pfn0, op1, 1);
3442 e2 = build_binary_op (TRUTH_ORIF_EXPR, e2, e3, 1);
3443 e2 = build_binary_op (TRUTH_ANDIF_EXPR, e1, e2, 1);
3444 if (code == EQ_EXPR)
3446 return build_binary_op (EQ_EXPR, e2, integer_zero_node, 1);
3448 else if (TYPE_PTRMEMFUNC_P (type1)
3449 && TYPE_PTRMEMFUNC_FN_TYPE (type1) == type0)
3451 return build_binary_op (code, op1, op0, 1);
3457 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE)
3458 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE))
3460 else if (code0 == POINTER_TYPE && code1 == POINTER_TYPE)
3462 if (comp_target_types (type0, type1, 1))
3463 result_type = common_type (type0, type1);
3466 cp_pedwarn ("comparison of distinct pointer types `%T' and `%T' lacks a cast",
3468 result_type = ptr_type_node;
3477 build_type = boolean_type_node;
3478 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE)
3479 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE))
3481 else if (code0 == POINTER_TYPE && code1 == POINTER_TYPE)
3483 if (comp_target_types (type0, type1, 1))
3484 result_type = common_type (type0, type1);
3487 cp_pedwarn ("comparison of distinct pointer types `%T' and `%T' lacks a cast",
3489 result_type = ptr_type_node;
3492 else if (code0 == POINTER_TYPE && TREE_CODE (op1) == INTEGER_CST
3493 && integer_zerop (op1))
3494 result_type = type0;
3495 else if (code1 == POINTER_TYPE && TREE_CODE (op0) == INTEGER_CST
3496 && integer_zerop (op0))
3497 result_type = type1;
3498 else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
3500 result_type = type0;
3502 pedwarn ("ANSI C++ forbids comparison between pointer and integer");
3503 else if (! flag_traditional)
3504 warning ("comparison between pointer and integer");
3506 else if (code0 == INTEGER_TYPE && code1 == POINTER_TYPE)
3508 result_type = type1;
3510 pedwarn ("ANSI C++ forbids comparison between pointer and integer");
3511 else if (! flag_traditional)
3512 warning ("comparison between pointer and integer");
3517 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE)
3518 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE))
3520 if (shorten || common || short_compare)
3521 result_type = common_type (type0, type1);
3523 /* For certain operations (which identify themselves by shorten != 0)
3524 if both args were extended from the same smaller type,
3525 do the arithmetic in that type and then extend.
3527 shorten !=0 and !=1 indicates a bitwise operation.
3528 For them, this optimization is safe only if
3529 both args are zero-extended or both are sign-extended.
3530 Otherwise, we might change the result.
3531 Eg, (short)-1 | (unsigned short)-1 is (int)-1
3532 but calculated in (unsigned short) it would be (unsigned short)-1. */
3536 int unsigned0, unsigned1;
3537 tree arg0 = get_narrower (op0, &unsigned0);
3538 tree arg1 = get_narrower (op1, &unsigned1);
3539 /* UNS is 1 if the operation to be done is an unsigned one. */
3540 int uns = TREE_UNSIGNED (result_type);
3543 final_type = result_type;
3545 /* Handle the case that OP0 does not *contain* a conversion
3546 but it *requires* conversion to FINAL_TYPE. */
3548 if (op0 == arg0 && TREE_TYPE (op0) != final_type)
3549 unsigned0 = TREE_UNSIGNED (TREE_TYPE (op0));
3550 if (op1 == arg1 && TREE_TYPE (op1) != final_type)
3551 unsigned1 = TREE_UNSIGNED (TREE_TYPE (op1));
3553 /* Now UNSIGNED0 is 1 if ARG0 zero-extends to FINAL_TYPE. */
3555 /* For bitwise operations, signedness of nominal type
3556 does not matter. Consider only how operands were extended. */
3560 /* Note that in all three cases below we refrain from optimizing
3561 an unsigned operation on sign-extended args.
3562 That would not be valid. */
3564 /* Both args variable: if both extended in same way
3565 from same width, do it in that width.
3566 Do it unsigned if args were zero-extended. */
3567 if ((TYPE_PRECISION (TREE_TYPE (arg0))
3568 < TYPE_PRECISION (result_type))
3569 && (TYPE_PRECISION (TREE_TYPE (arg1))
3570 == TYPE_PRECISION (TREE_TYPE (arg0)))
3571 && unsigned0 == unsigned1
3572 && (unsigned0 || !uns))
3574 = signed_or_unsigned_type (unsigned0,
3575 common_type (TREE_TYPE (arg0), TREE_TYPE (arg1)));
3576 else if (TREE_CODE (arg0) == INTEGER_CST
3577 && (unsigned1 || !uns)
3578 && (TYPE_PRECISION (TREE_TYPE (arg1))
3579 < TYPE_PRECISION (result_type))
3580 && (type = signed_or_unsigned_type (unsigned1,
3582 int_fits_type_p (arg0, type)))
3584 else if (TREE_CODE (arg1) == INTEGER_CST
3585 && (unsigned0 || !uns)
3586 && (TYPE_PRECISION (TREE_TYPE (arg0))
3587 < TYPE_PRECISION (result_type))
3588 && (type = signed_or_unsigned_type (unsigned0,
3590 int_fits_type_p (arg1, type)))
3594 /* Shifts can be shortened if shifting right. */
3599 tree arg0 = get_narrower (op0, &unsigned_arg);
3601 final_type = result_type;
3603 if (arg0 == op0 && final_type == TREE_TYPE (op0))
3604 unsigned_arg = TREE_UNSIGNED (TREE_TYPE (op0));
3606 if (TYPE_PRECISION (TREE_TYPE (arg0)) < TYPE_PRECISION (result_type)
3607 /* We can shorten only if the shift count is less than the
3608 number of bits in the smaller type size. */
3609 && TREE_INT_CST_HIGH (op1) == 0
3610 && TYPE_PRECISION (TREE_TYPE (arg0)) > TREE_INT_CST_LOW (op1)
3611 /* If arg is sign-extended and then unsigned-shifted,
3612 we can simulate this with a signed shift in arg's type
3613 only if the extended result is at least twice as wide
3614 as the arg. Otherwise, the shift could use up all the
3615 ones made by sign-extension and bring in zeros.
3616 We can't optimize that case at all, but in most machines
3617 it never happens because available widths are 2**N. */
3618 && (!TREE_UNSIGNED (final_type)
3620 || (((unsigned) 2 * TYPE_PRECISION (TREE_TYPE (arg0)))
3621 <= TYPE_PRECISION (result_type))))
3623 /* Do an unsigned shift if the operand was zero-extended. */
3625 = signed_or_unsigned_type (unsigned_arg,
3627 /* Convert value-to-be-shifted to that type. */
3628 if (TREE_TYPE (op0) != result_type)
3629 op0 = convert (result_type, op0);
3634 /* Comparison operations are shortened too but differently.
3635 They identify themselves by setting short_compare = 1. */
3639 /* Don't write &op0, etc., because that would prevent op0
3640 from being kept in a register.
3641 Instead, make copies of the our local variables and
3642 pass the copies by reference, then copy them back afterward. */
3643 tree xop0 = op0, xop1 = op1, xresult_type = result_type;
3644 enum tree_code xresultcode = resultcode;
3646 = shorten_compare (&xop0, &xop1, &xresult_type, &xresultcode);
3648 return convert (boolean_type_node, val);
3649 op0 = xop0, op1 = xop1;
3651 resultcode = xresultcode;
3654 if (short_compare && warn_sign_compare)
3656 int op0_signed = ! TREE_UNSIGNED (TREE_TYPE (orig_op0));
3657 int op1_signed = ! TREE_UNSIGNED (TREE_TYPE (orig_op1));
3659 int unsignedp0, unsignedp1;
3660 tree primop0 = get_narrower (op0, &unsignedp0);
3661 tree primop1 = get_narrower (op1, &unsignedp1);
3663 /* Check for comparison of different enum types. */
3664 if (flag_int_enum_equivalence == 0
3665 && TREE_CODE (TREE_TYPE (orig_op0)) == ENUMERAL_TYPE
3666 && TREE_CODE (TREE_TYPE (orig_op1)) == ENUMERAL_TYPE
3667 && TYPE_MAIN_VARIANT (TREE_TYPE (orig_op0))
3668 != TYPE_MAIN_VARIANT (TREE_TYPE (orig_op1)))
3670 cp_warning ("comparison between `%#T' and `%#T'",
3671 TREE_TYPE (orig_op0), TREE_TYPE (orig_op1));
3674 /* Give warnings for comparisons between signed and unsigned
3675 quantities that may fail. */
3676 /* Do the checking based on the original operand trees, so that
3677 casts will be considered, but default promotions won't be. */
3679 /* Do not warn if the comparison is being done in a signed type,
3680 since the signed type will only be chosen if it can represent
3681 all the values of the unsigned type. */
3682 if (! TREE_UNSIGNED (result_type))
3684 /* Do not warn if both operands are unsigned. */
3685 else if (op0_signed == op1_signed)
3687 /* Do not warn if the signed quantity is an unsuffixed
3688 integer literal (or some static constant expression
3689 involving such literals) and it is non-negative. */
3690 else if ((op0_signed && TREE_CODE (orig_op0) == INTEGER_CST
3691 && tree_int_cst_sgn (orig_op0) >= 0)
3692 || (op1_signed && TREE_CODE (orig_op1) == INTEGER_CST
3693 && tree_int_cst_sgn (orig_op1) >= 0))
3695 /* Do not warn if the comparison is an equality operation,
3696 the unsigned quantity is an integral constant and it does
3697 not use the most significant bit of result_type. */
3698 else if ((resultcode == EQ_EXPR || resultcode == NE_EXPR)
3699 && ((op0_signed && TREE_CODE (orig_op1) == INTEGER_CST
3700 && int_fits_type_p (orig_op1, signed_type (result_type))
3701 || (op1_signed && TREE_CODE (orig_op0) == INTEGER_CST
3702 && int_fits_type_p (orig_op0, signed_type (result_type))))))
3705 warning ("comparison between signed and unsigned");
3707 /* Warn if two unsigned values are being compared in a size
3708 larger than their original size, and one (and only one) is the
3709 result of a `~' operator. This comparison will always fail.
3711 Also warn if one operand is a constant, and the constant does not
3712 have all bits set that are set in the ~ operand when it is
3715 if ((TREE_CODE (primop0) == BIT_NOT_EXPR)
3716 ^ (TREE_CODE (primop1) == BIT_NOT_EXPR))
3718 if (TREE_CODE (primop0) == BIT_NOT_EXPR)
3719 primop0 = get_narrower (TREE_OPERAND (op0, 0), &unsignedp0);
3720 if (TREE_CODE (primop1) == BIT_NOT_EXPR)
3721 primop1 = get_narrower (TREE_OPERAND (op1, 0), &unsignedp1);
3723 if (TREE_CODE (primop0) == INTEGER_CST
3724 || TREE_CODE (primop1) == INTEGER_CST)
3727 HOST_WIDE_INT constant, mask;
3731 if (TREE_CODE (primop0) == INTEGER_CST)
3734 unsignedp = unsignedp1;
3735 constant = TREE_INT_CST_LOW (primop0);
3740 unsignedp = unsignedp0;
3741 constant = TREE_INT_CST_LOW (primop1);
3744 bits = TYPE_PRECISION (TREE_TYPE (primop));
3745 if (bits < TYPE_PRECISION (result_type)
3746 && bits < HOST_BITS_PER_LONG && unsignedp)
3748 mask = (~ (HOST_WIDE_INT) 0) << bits;
3749 if ((mask & constant) != mask)
3750 warning ("comparison of promoted ~unsigned with constant");
3753 else if (unsignedp0 && unsignedp1
3754 && (TYPE_PRECISION (TREE_TYPE (primop0))
3755 < TYPE_PRECISION (result_type))
3756 && (TYPE_PRECISION (TREE_TYPE (primop1))
3757 < TYPE_PRECISION (result_type)))
3758 warning ("comparison of promoted ~unsigned with unsigned");
3763 /* At this point, RESULT_TYPE must be nonzero to avoid an error message.
3764 If CONVERTED is zero, both args will be converted to type RESULT_TYPE.
3765 Then the expression will be built.
3766 It will be given type FINAL_TYPE if that is nonzero;
3767 otherwise, it will be given type RESULT_TYPE. */
3771 cp_error ("invalid operands `%T' and `%T' to binary `%O'",
3772 TREE_TYPE (orig_op0), TREE_TYPE (orig_op1), error_code);
3773 return error_mark_node;
3778 if (TREE_TYPE (op0) != result_type)
3779 op0 = convert (result_type, op0);
3780 if (TREE_TYPE (op1) != result_type)
3781 op1 = convert (result_type, op1);
3784 if (build_type == NULL_TREE)
3785 build_type = result_type;
3788 register tree result = build (resultcode, build_type, op0, op1);
3789 register tree folded;
3791 folded = fold (result);
3792 if (folded == result)
3793 TREE_CONSTANT (folded) = TREE_CONSTANT (op0) & TREE_CONSTANT (op1);
3794 if (final_type != 0)
3795 return convert (final_type, folded);
3800 /* Return a tree for the sum or difference (RESULTCODE says which)
3801 of pointer PTROP and integer INTOP. */
3804 pointer_int_sum (resultcode, ptrop, intop)
3805 enum tree_code resultcode;
3806 register tree ptrop, intop;
3810 register tree result;
3811 register tree folded = fold (intop);
3813 /* The result is a pointer of the same type that is being added. */
3815 register tree result_type = TREE_TYPE (ptrop);
3817 if (TREE_CODE (TREE_TYPE (result_type)) == VOID_TYPE)
3819 if (pedantic || warn_pointer_arith)
3820 pedwarn ("ANSI C++ forbids using pointer of type `void *' in arithmetic");
3821 size_exp = integer_one_node;
3823 else if (TREE_CODE (TREE_TYPE (result_type)) == FUNCTION_TYPE)
3825 if (pedantic || warn_pointer_arith)
3826 pedwarn ("ANSI C++ forbids using pointer to a function in arithmetic");
3827 size_exp = integer_one_node;
3829 else if (TREE_CODE (TREE_TYPE (result_type)) == METHOD_TYPE)
3831 if (pedantic || warn_pointer_arith)
3832 pedwarn ("ANSI C++ forbids using pointer to a method in arithmetic");
3833 size_exp = integer_one_node;
3835 else if (TREE_CODE (TREE_TYPE (result_type)) == OFFSET_TYPE)
3837 if (pedantic || warn_pointer_arith)
3838 pedwarn ("ANSI C++ forbids using pointer to a member in arithmetic");
3839 size_exp = integer_one_node;
3842 size_exp = size_in_bytes (complete_type (TREE_TYPE (result_type)));
3844 /* Needed to make OOPS V2R3 work. */
3846 if (TREE_CODE (intop) == INTEGER_CST
3847 && TREE_INT_CST_LOW (intop) == 0
3848 && TREE_INT_CST_HIGH (intop) == 0)
3851 /* If what we are about to multiply by the size of the elements
3852 contains a constant term, apply distributive law
3853 and multiply that constant term separately.
3854 This helps produce common subexpressions. */
3856 if ((TREE_CODE (intop) == PLUS_EXPR || TREE_CODE (intop) == MINUS_EXPR)
3857 && ! TREE_CONSTANT (intop)
3858 && TREE_CONSTANT (TREE_OPERAND (intop, 1))
3859 && TREE_CONSTANT (size_exp))
3861 enum tree_code subcode = resultcode;
3862 if (TREE_CODE (intop) == MINUS_EXPR)
3863 subcode = (subcode == PLUS_EXPR ? MINUS_EXPR : PLUS_EXPR);
3864 ptrop = build_binary_op (subcode, ptrop, TREE_OPERAND (intop, 1), 1);
3865 intop = TREE_OPERAND (intop, 0);
3868 /* Convert the integer argument to a type the same size as sizetype
3869 so the multiply won't overflow spuriously. */
3871 if (TYPE_PRECISION (TREE_TYPE (intop)) != TYPE_PRECISION (sizetype))
3872 intop = convert (type_for_size (TYPE_PRECISION (sizetype), 0), intop);
3874 /* Replace the integer argument with a suitable product by the object size.
3875 Do this multiplication as signed, then convert to the appropriate
3876 pointer type (actually unsigned integral). */
3878 intop = convert (result_type,
3879 build_binary_op (MULT_EXPR, intop,
3880 convert (TREE_TYPE (intop), size_exp), 1));
3882 /* Create the sum or difference. */
3884 result = build (resultcode, result_type, ptrop, intop);
3886 folded = fold (result);
3887 if (folded == result)
3888 TREE_CONSTANT (folded) = TREE_CONSTANT (ptrop) & TREE_CONSTANT (intop);
3892 /* Return a tree for the difference of pointers OP0 and OP1.
3893 The resulting tree has type int. */
3896 pointer_diff (op0, op1)
3897 register tree op0, op1;
3899 register tree result, folded;
3900 tree restype = ptrdiff_type_node;
3901 tree target_type = TREE_TYPE (TREE_TYPE (op0));
3903 if (pedantic || warn_pointer_arith)
3905 if (TREE_CODE (target_type) == VOID_TYPE)
3906 pedwarn ("ANSI C++ forbids using pointer of type `void *' in subtraction");
3907 if (TREE_CODE (target_type) == FUNCTION_TYPE)
3908 pedwarn ("ANSI C++ forbids using pointer to a function in subtraction");
3909 if (TREE_CODE (target_type) == METHOD_TYPE)
3910 pedwarn ("ANSI C++ forbids using pointer to a method in subtraction");
3911 if (TREE_CODE (target_type) == OFFSET_TYPE)
3912 pedwarn ("ANSI C++ forbids using pointer to a member in subtraction");
3915 /* First do the subtraction as integers;
3916 then drop through to build the divide operator. */
3918 op0 = build_binary_op (MINUS_EXPR,
3919 convert (restype, op0), convert (restype, op1), 1);
3921 /* This generates an error if op1 is a pointer to an incomplete type. */
3922 if (TYPE_SIZE (TREE_TYPE (TREE_TYPE (op1))) == 0)
3923 error ("arithmetic on pointer to an incomplete type");
3925 op1 = ((TREE_CODE (target_type) == VOID_TYPE
3926 || TREE_CODE (target_type) == FUNCTION_TYPE
3927 || TREE_CODE (target_type) == METHOD_TYPE
3928 || TREE_CODE (target_type) == OFFSET_TYPE)
3930 : size_in_bytes (target_type));
3932 /* Do the division. */
3934 result = build (EXACT_DIV_EXPR, restype, op0, convert (restype, op1));
3936 folded = fold (result);
3937 if (folded == result)
3938 TREE_CONSTANT (folded) = TREE_CONSTANT (op0) & TREE_CONSTANT (op1);
3942 /* Handle the case of taking the address of a COMPONENT_REF.
3943 Called by `build_unary_op' and `build_up_reference'.
3945 ARG is the COMPONENT_REF whose address we want.
3946 ARGTYPE is the pointer type that this address should have.
3947 MSG is an error message to print if this COMPONENT_REF is not
3948 addressable (such as a bitfield). */
3951 build_component_addr (arg, argtype, msg)
3955 tree field = TREE_OPERAND (arg, 1);
3956 tree basetype = decl_type_context (field);
3957 tree rval = build_unary_op (ADDR_EXPR, TREE_OPERAND (arg, 0), 0);
3959 if (DECL_BIT_FIELD (field))
3961 error (msg, IDENTIFIER_POINTER (DECL_NAME (field)));
3962 return error_mark_node;
3965 if (TREE_CODE (field) == FIELD_DECL
3966 && TYPE_USES_COMPLEX_INHERITANCE (basetype))
3968 /* Can't convert directly to ARGTYPE, since that
3969 may have the same pointer type as one of our
3971 rval = build1 (NOP_EXPR, argtype,
3972 convert_pointer_to (basetype, rval));
3973 TREE_CONSTANT (rval) = TREE_CONSTANT (TREE_OPERAND (rval, 0));
3976 /* This conversion is harmless. */
3977 rval = convert_force (argtype, rval, 0);
3979 if (! integer_zerop (DECL_FIELD_BITPOS (field)))
3981 tree offset = size_binop (EASY_DIV_EXPR, DECL_FIELD_BITPOS (field),
3982 size_int (BITS_PER_UNIT));
3983 int flag = TREE_CONSTANT (rval);
3984 rval = fold (build (PLUS_EXPR, argtype,
3985 rval, convert (argtype, offset)));
3986 TREE_CONSTANT (rval) = flag;
3991 /* Construct and perhaps optimize a tree representation
3992 for a unary operation. CODE, a tree_code, specifies the operation
3993 and XARG is the operand. */
3996 build_x_unary_op (code, xarg)
3997 enum tree_code code;
4000 if (current_template_parms)
4001 return build_min_nt (code, xarg, NULL_TREE);
4003 /* & rec, on incomplete RECORD_TYPEs is the simple opr &, not an
4005 if (code == ADDR_EXPR
4006 && ((IS_AGGR_TYPE_CODE (TREE_CODE (TREE_TYPE (xarg)))
4007 && TYPE_SIZE (TREE_TYPE (xarg)) == NULL_TREE)
4008 || (TREE_CODE (xarg) == OFFSET_REF)))
4009 /* don't look for a function */;
4012 tree rval = build_opfncall (code, LOOKUP_SPECULATIVELY, xarg,
4013 NULL_TREE, NULL_TREE);
4015 return build_opfncall (code, LOOKUP_NORMAL, xarg,
4016 NULL_TREE, NULL_TREE);
4019 if (code == ADDR_EXPR)
4021 if (TREE_CODE (xarg) == TARGET_EXPR)
4022 warning ("taking address of temporary");
4025 return build_unary_op (code, xarg, 0);
4028 /* Just like truthvalue_conversion, but we want a CLEANUP_POINT_EXPR. */
4031 condition_conversion (expr)
4035 if (current_template_parms)
4037 t = convert (boolean_type_node, expr);
4038 t = fold (build1 (CLEANUP_POINT_EXPR, boolean_type_node, t));
4042 /* C++: Must handle pointers to members.
4044 Perhaps type instantiation should be extended to handle conversion
4045 from aggregates to types we don't yet know we want? (Or are those
4046 cases typically errors which should be reported?)
4048 NOCONVERT nonzero suppresses the default promotions
4049 (such as from short to int). */
4051 build_unary_op (code, xarg, noconvert)
4052 enum tree_code code;
4056 /* No default_conversion here. It causes trouble for ADDR_EXPR. */
4057 register tree arg = xarg;
4058 register tree argtype = 0;
4059 char *errstring = NULL;
4062 if (arg == error_mark_node)
4063 return error_mark_node;
4068 /* This is used for unary plus, because a CONVERT_EXPR
4069 is enough to prevent anybody from looking inside for
4070 associativity, but won't generate any code. */
4071 if (!(arg = build_expr_type_conversion
4072 (WANT_ARITH | WANT_ENUM | WANT_POINTER, arg, 1)))
4073 errstring = "wrong type argument to unary plus";
4077 arg = default_conversion (arg);
4078 arg = build1 (NON_LVALUE_EXPR, TREE_TYPE (arg), arg);
4083 if (!(arg = build_expr_type_conversion (WANT_ARITH | WANT_ENUM, arg, 1)))
4084 errstring = "wrong type argument to unary minus";
4085 else if (!noconvert)
4086 arg = default_conversion (arg);
4090 if (!(arg = build_expr_type_conversion (WANT_INT | WANT_ENUM, arg, 1)))
4091 errstring = "wrong type argument to bit-complement";
4092 else if (!noconvert)
4093 arg = default_conversion (arg);
4097 if (!(arg = build_expr_type_conversion (WANT_ARITH | WANT_ENUM, arg, 1)))
4098 errstring = "wrong type argument to abs";
4099 else if (!noconvert)
4100 arg = default_conversion (arg);
4103 case TRUTH_NOT_EXPR:
4104 arg = convert (boolean_type_node, arg);
4105 val = invert_truthvalue (arg);
4106 if (arg != error_mark_node)
4108 errstring = "in argument to unary !";
4114 case PREINCREMENT_EXPR:
4115 case POSTINCREMENT_EXPR:
4116 case PREDECREMENT_EXPR:
4117 case POSTDECREMENT_EXPR:
4118 /* Handle complex lvalues (when permitted)
4119 by reduction to simpler cases. */
4121 val = unary_complex_lvalue (code, arg);
4125 /* Report invalid types. */
4127 if (!(arg = build_expr_type_conversion (WANT_ARITH | WANT_POINTER,
4130 if (code == PREINCREMENT_EXPR)
4131 errstring ="no pre-increment operator for type";
4132 else if (code == POSTINCREMENT_EXPR)
4133 errstring ="no post-increment operator for type";
4134 else if (code == PREDECREMENT_EXPR)
4135 errstring ="no pre-decrement operator for type";
4137 errstring ="no post-decrement operator for type";
4141 /* Report something read-only. */
4143 if (TYPE_READONLY (TREE_TYPE (arg))
4144 || TREE_READONLY (arg))
4145 readonly_error (arg, ((code == PREINCREMENT_EXPR
4146 || code == POSTINCREMENT_EXPR)
4147 ? "increment" : "decrement"),
4152 tree result_type = TREE_TYPE (arg);
4154 arg = get_unwidened (arg, 0);
4155 argtype = TREE_TYPE (arg);
4157 /* ARM $5.2.5 last annotation says this should be forbidden. */
4158 if (TREE_CODE (argtype) == ENUMERAL_TYPE)
4159 pedwarn ("ANSI C++ forbids %sing an enum",
4160 (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
4161 ? "increment" : "decrement");
4163 /* Compute the increment. */
4165 if (TREE_CODE (argtype) == POINTER_TYPE)
4167 enum tree_code tmp = TREE_CODE (TREE_TYPE (argtype));
4168 if (TYPE_SIZE (complete_type (TREE_TYPE (argtype))) == 0)
4169 cp_error ("cannot %s a pointer to incomplete type `%T'",
4170 ((code == PREINCREMENT_EXPR
4171 || code == POSTINCREMENT_EXPR)
4172 ? "increment" : "decrement"), TREE_TYPE (argtype));
4173 else if (tmp == FUNCTION_TYPE || tmp == METHOD_TYPE
4174 || tmp == VOID_TYPE || tmp == OFFSET_TYPE)
4175 cp_pedwarn ("ANSI C++ forbids %sing a pointer of type `%T'",
4176 ((code == PREINCREMENT_EXPR
4177 || code == POSTINCREMENT_EXPR)
4178 ? "increment" : "decrement"), argtype);
4179 inc = c_sizeof_nowarn (TREE_TYPE (argtype));
4182 inc = integer_one_node;
4184 inc = convert (argtype, inc);
4186 /* Handle incrementing a cast-expression. */
4188 switch (TREE_CODE (arg))
4193 case FIX_TRUNC_EXPR:
4194 case FIX_FLOOR_EXPR:
4195 case FIX_ROUND_EXPR:
4198 tree incremented, modify, value, compound;
4199 if (! lvalue_p (arg) && pedantic)
4200 pedwarn ("cast to non-reference type used as lvalue");
4201 arg = stabilize_reference (arg);
4202 if (code == PREINCREMENT_EXPR || code == PREDECREMENT_EXPR)
4205 value = save_expr (arg);
4206 incremented = build (((code == PREINCREMENT_EXPR
4207 || code == POSTINCREMENT_EXPR)
4208 ? PLUS_EXPR : MINUS_EXPR),
4209 argtype, value, inc);
4210 TREE_SIDE_EFFECTS (incremented) = 1;
4212 modify = build_modify_expr (arg, NOP_EXPR, incremented);
4213 compound = build (COMPOUND_EXPR, TREE_TYPE (arg), modify, value);
4215 /* Eliminate warning about unused result of + or -. */
4216 TREE_NO_UNUSED_WARNING (compound) = 1;
4221 /* Complain about anything else that is not a true lvalue. */
4222 if (!lvalue_or_else (arg, ((code == PREINCREMENT_EXPR
4223 || code == POSTINCREMENT_EXPR)
4224 ? "increment" : "decrement")))
4225 return error_mark_node;
4227 /* Forbid using -- on `bool'. */
4228 if (TREE_TYPE (arg) == boolean_type_node)
4230 if (code == POSTDECREMENT_EXPR || code == PREDECREMENT_EXPR)
4232 cp_error ("invalid use of `--' on bool variable `%D'", arg);
4233 return error_mark_node;
4236 /* This will only work if someone can convince Kenner to accept
4237 my patch to expand_increment. (jason) */
4238 val = build (code, TREE_TYPE (arg), arg, inc);
4240 if (code == POSTINCREMENT_EXPR)
4242 arg = stabilize_reference (arg);
4243 val = build (MODIFY_EXPR, TREE_TYPE (arg), arg,
4245 TREE_SIDE_EFFECTS (val) = 1;
4246 arg = save_expr (arg);
4247 val = build (COMPOUND_EXPR, TREE_TYPE (arg), val, arg);
4248 val = build (COMPOUND_EXPR, TREE_TYPE (arg), arg, val);
4251 val = build (MODIFY_EXPR, TREE_TYPE (arg), arg,
4256 val = build (code, TREE_TYPE (arg), arg, inc);
4258 TREE_SIDE_EFFECTS (val) = 1;
4259 return convert (result_type, val);
4263 /* Note that this operation never does default_conversion
4264 regardless of NOCONVERT. */
4266 argtype = TREE_TYPE (arg);
4267 if (TREE_CODE (argtype) == REFERENCE_TYPE)
4269 arg = build1 (CONVERT_EXPR, build_pointer_type (TREE_TYPE (TREE_TYPE (arg))), arg);
4270 TREE_REFERENCE_EXPR (arg) = 1;
4274 && TREE_CODE (arg) == FUNCTION_DECL
4276 && DECL_CONTEXT (arg) == NULL_TREE
4277 && IDENTIFIER_LENGTH (DECL_NAME (arg)) == 4
4278 && IDENTIFIER_POINTER (DECL_NAME (arg))[0] == 'm'
4279 && ! strcmp (IDENTIFIER_POINTER (DECL_NAME (arg)), "main"))
4281 pedwarn ("taking address of function `main'");
4283 /* Let &* cancel out to simplify resulting code. */
4284 if (TREE_CODE (arg) == INDIRECT_REF)
4286 /* We don't need to have `current_class_decl' wrapped in a
4287 NON_LVALUE_EXPR node. */
4289 return current_class_decl;
4291 /* Keep `default_conversion' from converting if
4292 ARG is of REFERENCE_TYPE. */
4293 arg = TREE_OPERAND (arg, 0);
4294 if (TREE_CODE (TREE_TYPE (arg)) == REFERENCE_TYPE)
4296 if (TREE_CODE (arg) == VAR_DECL && DECL_INITIAL (arg)
4297 && !TREE_SIDE_EFFECTS (DECL_INITIAL (arg)))
4298 arg = DECL_INITIAL (arg);
4299 arg = build1 (CONVERT_EXPR, build_pointer_type (TREE_TYPE (TREE_TYPE (arg))), arg);
4300 TREE_REFERENCE_EXPR (arg) = 1;
4301 TREE_CONSTANT (arg) = TREE_CONSTANT (TREE_OPERAND (arg, 0));
4303 else if (lvalue_p (arg))
4304 /* Don't let this be an lvalue. */
4305 return non_lvalue (arg);
4309 /* For &x[y], return x+y */
4310 if (TREE_CODE (arg) == ARRAY_REF)
4312 if (mark_addressable (TREE_OPERAND (arg, 0)) == 0)
4313 return error_mark_node;
4314 return build_binary_op (PLUS_EXPR, TREE_OPERAND (arg, 0),
4315 TREE_OPERAND (arg, 1), 1);
4318 /* Uninstantiated types are all functions. Taking the
4319 address of a function is a no-op, so just return the
4322 if (TREE_CODE (arg) == IDENTIFIER_NODE
4323 && IDENTIFIER_OPNAME_P (arg))
4325 my_friendly_abort (117);
4326 /* We don't know the type yet, so just work around the problem.
4327 We know that this will resolve to an lvalue. */
4328 return build1 (ADDR_EXPR, unknown_type_node, arg);
4331 if (TREE_CODE (arg) == TREE_LIST)
4333 if (TREE_CODE (TREE_VALUE (arg)) == FUNCTION_DECL
4334 && DECL_CHAIN (TREE_VALUE (arg)) == NULL_TREE)
4335 /* Unique overloaded non-member function. */
4336 return build_unary_op (ADDR_EXPR, TREE_VALUE (arg), 0);
4337 if (TREE_CHAIN (arg) == NULL_TREE
4338 && TREE_CODE (TREE_VALUE (arg)) == TREE_LIST
4339 && DECL_CHAIN (TREE_VALUE (TREE_VALUE (arg))) == NULL_TREE)
4340 /* Unique overloaded member function. */
4341 return build_unary_op (ADDR_EXPR, TREE_VALUE (TREE_VALUE (arg)),
4343 return build1 (ADDR_EXPR, unknown_type_node, arg);
4346 /* Handle complex lvalues (when permitted)
4347 by reduction to simpler cases. */
4348 val = unary_complex_lvalue (code, arg);
4352 switch (TREE_CODE (arg))
4357 case FIX_TRUNC_EXPR:
4358 case FIX_FLOOR_EXPR:
4359 case FIX_ROUND_EXPR:
4361 if (! lvalue_p (arg) && pedantic)
4362 pedwarn ("taking the address of a cast to non-reference type");
4365 /* Allow the address of a constructor if all the elements
4367 if (TREE_CODE (arg) == CONSTRUCTOR && TREE_CONSTANT (arg))
4369 /* Anything not already handled and not a true memory reference
4371 else if (TREE_CODE (argtype) != FUNCTION_TYPE
4372 && TREE_CODE (argtype) != METHOD_TYPE
4373 && !lvalue_or_else (arg, "unary `&'"))
4374 return error_mark_node;
4376 /* Ordinary case; arg is a COMPONENT_REF or a decl. */
4377 /* If the lvalue is const or volatile,
4378 merge that into the type that the address will point to. */
4379 if (TREE_CODE_CLASS (TREE_CODE (arg)) == 'd'
4380 || TREE_CODE_CLASS (TREE_CODE (arg)) == 'r')
4382 if (TREE_READONLY (arg) || TREE_THIS_VOLATILE (arg))
4383 argtype = cp_build_type_variant (argtype,
4384 TREE_READONLY (arg),
4385 TREE_THIS_VOLATILE (arg));
4388 argtype = build_pointer_type (argtype);
4390 if (mark_addressable (arg) == 0)
4391 return error_mark_node;
4396 if (TREE_CODE (arg) == COMPONENT_REF)
4397 addr = build_component_addr (arg, argtype,
4398 "attempt to take address of bit-field structure member `%s'");
4400 addr = build1 (code, argtype, arg);
4402 /* Address of a static or external variable or
4403 function counts as a constant */
4405 TREE_CONSTANT (addr) = 1;
4413 argtype = TREE_TYPE (arg);
4414 return fold (build1 (code, argtype, arg));
4418 return error_mark_node;
4422 /* If CONVERSIONS is a conversion expression or a nested sequence of such,
4423 convert ARG with the same conversions in the same order
4424 and return the result. */
4427 convert_sequence (conversions, arg)
4431 switch (TREE_CODE (conversions))
4436 case FIX_TRUNC_EXPR:
4437 case FIX_FLOOR_EXPR:
4438 case FIX_ROUND_EXPR:
4440 return convert (TREE_TYPE (conversions),
4441 convert_sequence (TREE_OPERAND (conversions, 0),
4450 /* Apply unary lvalue-demanding operator CODE to the expression ARG
4451 for certain kinds of expressions which are not really lvalues
4452 but which we can accept as lvalues.
4454 If ARG is not a kind of expression we can handle, return zero. */
4457 unary_complex_lvalue (code, arg)
4458 enum tree_code code;
4461 /* Handle (a, b) used as an "lvalue". */
4462 if (TREE_CODE (arg) == COMPOUND_EXPR)
4464 tree real_result = build_unary_op (code, TREE_OPERAND (arg, 1), 0);
4465 return build (COMPOUND_EXPR, TREE_TYPE (real_result),
4466 TREE_OPERAND (arg, 0), real_result);
4469 /* Handle (a ? b : c) used as an "lvalue". */
4470 if (TREE_CODE (arg) == COND_EXPR)
4471 return rationalize_conditional_expr (code, arg);
4473 if (TREE_CODE (arg) == MODIFY_EXPR
4474 || TREE_CODE (arg) == PREINCREMENT_EXPR
4475 || TREE_CODE (arg) == PREDECREMENT_EXPR)
4476 return unary_complex_lvalue
4477 (code, build (COMPOUND_EXPR, TREE_TYPE (TREE_OPERAND (arg, 0)),
4478 arg, TREE_OPERAND (arg, 0)));
4480 if (code != ADDR_EXPR)
4483 /* Handle (a = b) used as an "lvalue" for `&'. */
4484 if (TREE_CODE (arg) == MODIFY_EXPR
4485 || TREE_CODE (arg) == INIT_EXPR)
4487 tree real_result = build_unary_op (code, TREE_OPERAND (arg, 0), 0);
4488 return build (COMPOUND_EXPR, TREE_TYPE (real_result), arg, real_result);
4491 if (TREE_CODE (TREE_TYPE (arg)) == FUNCTION_TYPE
4492 || TREE_CODE (TREE_TYPE (arg)) == METHOD_TYPE
4493 || TREE_CODE (TREE_TYPE (arg)) == OFFSET_TYPE)
4495 /* The representation of something of type OFFSET_TYPE
4496 is really the representation of a pointer to it.
4497 Here give the representation its true type. */
4501 my_friendly_assert (TREE_CODE (arg) != SCOPE_REF, 313);
4503 if (TREE_CODE (arg) != OFFSET_REF)
4506 t = TREE_OPERAND (arg, 1);
4508 if (TREE_CODE (t) == FUNCTION_DECL) /* Check all this code for right semantics. */
4509 return build_unary_op (ADDR_EXPR, t, 0);
4510 if (TREE_CODE (t) == VAR_DECL)
4511 return build_unary_op (ADDR_EXPR, t, 0);
4514 if (TREE_OPERAND (arg, 0)
4515 && (TREE_CODE (TREE_OPERAND (arg, 0)) != NOP_EXPR
4516 || TREE_OPERAND (TREE_OPERAND (arg, 0), 0) != error_mark_node))
4517 if (TREE_CODE (t) != FIELD_DECL)
4519 /* Don't know if this should return address to just
4520 _DECL, or actual address resolved in this expression. */
4521 sorry ("address of bound pointer-to-member expression");
4522 return error_mark_node;
4525 /* Add in the offset to the right subobject. */
4526 offset = get_delta_difference (DECL_FIELD_CONTEXT (t),
4527 TREE_TYPE (TREE_OPERAND (arg, 0)),
4530 /* Add in the offset to the field. */
4531 offset = size_binop (PLUS_EXPR, offset,
4532 size_binop (EASY_DIV_EXPR,
4533 DECL_FIELD_BITPOS (t),
4534 size_int (BITS_PER_UNIT)));
4536 /* We offset all pointer to data memebers by 1 so that we can
4537 distinguish between a null pointer to data member and the first
4538 data member of a structure. */
4539 offset = size_binop (PLUS_EXPR, offset, size_int (1));
4541 return convert (build_pointer_type (TREE_TYPE (arg)), offset);
4547 /* This seems to be obsolete now (and posssibly wrong, compare with
4548 resolve_offset_ref). */
4549 if (TREE_CODE (arg) == OFFSET_REF)
4551 tree left = TREE_OPERAND (arg, 0), left_addr;
4552 tree right_addr = build_unary_op (ADDR_EXPR, TREE_OPERAND (arg, 1), 0);
4555 if (current_class_decl)
4556 left_addr = current_class_decl;
4559 error ("no `this' for pointer to member");
4560 return error_mark_node;
4563 left_addr = build_unary_op (ADDR_EXPR, left, 0);
4565 return build (PLUS_EXPR, build_pointer_type (TREE_TYPE (arg)),
4566 build1 (NOP_EXPR, integer_type_node, left_addr),
4567 build1 (NOP_EXPR, integer_type_node, right_addr));
4571 /* We permit compiler to make function calls returning
4572 objects of aggregate type look like lvalues. */
4576 if (TREE_CODE (targ) == SAVE_EXPR)
4577 targ = TREE_OPERAND (targ, 0);
4579 if (TREE_CODE (targ) == CALL_EXPR && IS_AGGR_TYPE (TREE_TYPE (targ)))
4581 if (TREE_CODE (arg) == SAVE_EXPR)
4584 targ = build_cplus_new (TREE_TYPE (arg), arg);
4585 return build1 (ADDR_EXPR, build_pointer_type (TREE_TYPE (arg)), targ);
4588 if (TREE_CODE (arg) == SAVE_EXPR && TREE_CODE (targ) == INDIRECT_REF)
4589 return build (SAVE_EXPR, build_pointer_type (TREE_TYPE (arg)),
4590 TREE_OPERAND (targ, 0), current_function_decl, NULL);
4593 /* Don't let anything else be handled specially. */
4597 /* Mark EXP saying that we need to be able to take the
4598 address of it; it should not be allocated in a register.
4599 Value is 1 if successful.
4601 C++: we do not allow `current_class_decl' to be addressable. */
4604 mark_addressable (exp)
4607 register tree x = exp;
4609 if (TREE_ADDRESSABLE (x) == 1)
4613 switch (TREE_CODE (x))
4618 x = TREE_OPERAND (x, 0);
4622 if (x == current_class_decl)
4624 error ("address of `this' not available");
4625 TREE_ADDRESSABLE (x) = 1; /* so compiler doesn't die later */
4626 put_var_into_stack (x);
4630 if (TREE_STATIC (x) && TREE_READONLY (x)
4631 && DECL_RTL (x) != 0
4632 && ! DECL_IN_MEMORY_P (x))
4634 /* We thought this would make a good constant variable,
4635 but we were wrong. */
4636 push_obstacks_nochange ();
4637 end_temporary_allocation ();
4639 TREE_ASM_WRITTEN (x) = 0;
4641 rest_of_decl_compilation (x, 0, IDENTIFIER_LOCAL_VALUE (x) == 0, 0);
4642 TREE_ADDRESSABLE (x) = 1;
4648 /* Caller should not be trying to mark initialized
4649 constant fields addressable. */
4650 my_friendly_assert (DECL_LANG_SPECIFIC (x) == 0
4651 || DECL_IN_AGGR_P (x) == 0
4653 || DECL_EXTERNAL (x), 314);
4657 /* For C++, we don't warn about taking the address of a register
4658 variable for CONST_DECLs; ARM p97 explicitly says it's okay. */
4659 put_var_into_stack (x);
4660 TREE_ADDRESSABLE (x) = 1;
4664 /* We have to test both conditions here. The first may
4665 be non-zero in the case of processing a default function.
4666 The second may be non-zero in the case of a template function. */
4667 x = DECL_MAIN_VARIANT (x);
4668 if ((DECL_THIS_INLINE (x) || DECL_PENDING_INLINE_INFO (x))
4669 && (DECL_CONTEXT (x) == NULL_TREE
4670 || TREE_CODE_CLASS (TREE_CODE (DECL_CONTEXT (x))) != 't'
4671 || ! CLASSTYPE_INTERFACE_ONLY (DECL_CONTEXT (x))))
4673 mark_inline_for_output (x);
4674 if (x == current_function_decl)
4675 DECL_EXTERNAL (x) = 0;
4677 TREE_ADDRESSABLE (x) = 1;
4679 TREE_ADDRESSABLE (DECL_ASSEMBLER_NAME (x)) = 1;
4687 /* Build and return a conditional expression IFEXP ? OP1 : OP2. */
4690 build_x_conditional_expr (ifexp, op1, op2)
4691 tree ifexp, op1, op2;
4693 tree rval = NULL_TREE;
4695 if (current_template_parms)
4696 return build_min_nt (COND_EXPR, ifexp, op1, op2);
4698 /* See comments in `build_x_binary_op'. */
4700 rval = build_opfncall (COND_EXPR, LOOKUP_SPECULATIVELY, ifexp, op1, op2);
4702 return build_opfncall (COND_EXPR, LOOKUP_NORMAL, ifexp, op1, op2);
4704 return build_conditional_expr (ifexp, op1, op2);
4708 build_conditional_expr (ifexp, op1, op2)
4709 tree ifexp, op1, op2;
4711 register tree type1;
4712 register tree type2;
4713 register enum tree_code code1;
4714 register enum tree_code code2;
4715 register tree result_type = NULL_TREE;
4716 tree orig_op1 = op1, orig_op2 = op2;
4718 /* If second operand is omitted, it is the same as the first one;
4719 make sure it is calculated only once. */
4723 pedwarn ("ANSI C++ forbids omitting the middle term of a ?: expression");
4724 ifexp = op1 = save_expr (ifexp);
4727 ifexp = convert (boolean_type_node, ifexp);
4729 if (TREE_CODE (ifexp) == ERROR_MARK)
4730 return error_mark_node;
4732 op1 = require_instantiated_type (TREE_TYPE (op2), op1, error_mark_node);
4733 if (op1 == error_mark_node)
4734 return error_mark_node;
4735 op2 = require_instantiated_type (TREE_TYPE (op1), op2, error_mark_node);
4736 if (op2 == error_mark_node)
4737 return error_mark_node;
4739 /* C++: REFERENCE_TYPES must be dereferenced. */
4740 type1 = TREE_TYPE (op1);
4741 code1 = TREE_CODE (type1);
4742 type2 = TREE_TYPE (op2);
4743 code2 = TREE_CODE (type2);
4745 if (code1 == REFERENCE_TYPE)
4747 op1 = convert_from_reference (op1);
4748 type1 = TREE_TYPE (op1);
4749 code1 = TREE_CODE (type1);
4751 if (code2 == REFERENCE_TYPE)
4753 op2 = convert_from_reference (op2);
4754 type2 = TREE_TYPE (op2);
4755 code2 = TREE_CODE (type2);
4758 /* Don't promote the operands separately if they promote
4759 the same way. Return the unpromoted type and let the combined
4760 value get promoted if necessary. */
4762 if (TYPE_MAIN_VARIANT (type1) == TYPE_MAIN_VARIANT (type2)
4763 && code2 != ARRAY_TYPE
4764 && code2 != FUNCTION_TYPE
4765 && code2 != METHOD_TYPE)
4769 if (TREE_CONSTANT (ifexp)
4770 && (TREE_CODE (ifexp) == INTEGER_CST
4771 || TREE_CODE (ifexp) == ADDR_EXPR))
4772 return (integer_zerop (ifexp) ? op2 : op1);
4774 if (TREE_CODE (op1) == CONST_DECL)
4775 op1 = DECL_INITIAL (op1);
4776 else if (TREE_READONLY_DECL_P (op1))
4777 op1 = decl_constant_value (op1);
4778 if (TREE_CODE (op2) == CONST_DECL)
4779 op2 = DECL_INITIAL (op2);
4780 else if (TREE_READONLY_DECL_P (op2))
4781 op2 = decl_constant_value (op2);
4783 type1 = cp_build_type_variant
4785 TREE_READONLY (op1) || TREE_READONLY (op2),
4786 TREE_THIS_VOLATILE (op1) || TREE_THIS_VOLATILE (op2));
4787 /* ??? This is a kludge to deal with the fact that
4788 we don't sort out integers and enums properly, yet. */
4789 result = fold (build (COND_EXPR, type1, ifexp, op1, op2));
4790 if (TREE_TYPE (result) != type1)
4791 result = build1 (NOP_EXPR, type1, result);
4795 /* They don't match; promote them both and then try to reconcile them.
4796 But don't permit mismatching enum types. */
4797 if (code1 == ENUMERAL_TYPE)
4799 if (code2 == ENUMERAL_TYPE)
4801 cp_error ("enumeral mismatch in conditional expression: `%T' vs `%T'", type1, type2);
4802 return error_mark_node;
4804 else if (extra_warnings && ! IS_AGGR_TYPE_CODE (code2)
4805 && type2 != type_promotes_to (type1))
4806 warning ("enumeral and non-enumeral type in conditional expression");
4808 else if (extra_warnings
4809 && code2 == ENUMERAL_TYPE && ! IS_AGGR_TYPE_CODE (code1)
4810 && type1 != type_promotes_to (type2))
4811 warning ("enumeral and non-enumeral type in conditional expression");
4813 if (code1 != VOID_TYPE)
4815 op1 = default_conversion (op1);
4816 type1 = TREE_TYPE (op1);
4817 if (TYPE_PTRMEMFUNC_P (type1))
4818 type1 = TYPE_PTRMEMFUNC_FN_TYPE (type1);
4819 code1 = TREE_CODE (type1);
4821 if (code2 != VOID_TYPE)
4823 op2 = default_conversion (op2);
4824 type2 = TREE_TYPE (op2);
4825 if (TYPE_PTRMEMFUNC_P (type2))
4826 type2 = TYPE_PTRMEMFUNC_FN_TYPE (type2);
4827 code2 = TREE_CODE (type2);
4830 if (code1 == RECORD_TYPE && code2 == RECORD_TYPE
4831 && real_lvalue_p (op1) && real_lvalue_p (op2)
4832 && comptypes (type1, type2, -1))
4834 type1 = build_reference_type (type1);
4835 type2 = build_reference_type (type2);
4836 result_type = common_type (type1, type2);
4837 op1 = convert_to_reference (result_type, op1, CONV_IMPLICIT,
4838 LOOKUP_NORMAL, NULL_TREE);
4839 op2 = convert_to_reference (result_type, op2, CONV_IMPLICIT,
4840 LOOKUP_NORMAL, NULL_TREE);
4842 /* Quickly detect the usual case where op1 and op2 have the same type
4844 else if (TYPE_MAIN_VARIANT (type1) == TYPE_MAIN_VARIANT (type2))
4847 result_type = type1;
4849 result_type = cp_build_type_variant
4851 TREE_READONLY (op1) || TREE_READONLY (op2),
4852 TREE_THIS_VOLATILE (op1) || TREE_THIS_VOLATILE (op2));
4854 else if ((code1 == INTEGER_TYPE || code1 == REAL_TYPE)
4855 && (code2 == INTEGER_TYPE || code2 == REAL_TYPE))
4857 result_type = common_type (type1, type2);
4859 else if (code1 == VOID_TYPE || code2 == VOID_TYPE)
4861 if (pedantic && (code1 != VOID_TYPE || code2 != VOID_TYPE))
4862 pedwarn ("ANSI C++ forbids conditional expr with only one void side");
4863 result_type = void_type_node;
4865 else if (code1 == POINTER_TYPE && code2 == POINTER_TYPE)
4867 if (comp_target_types (type1, type2, 1))
4868 result_type = common_type (type1, type2);
4869 else if (integer_zerop (op1) && TREE_TYPE (type1) == void_type_node
4870 && TREE_CODE (orig_op1) != NOP_EXPR)
4871 result_type = qualify_type (type2, type1);
4872 else if (integer_zerop (op2) && TREE_TYPE (type2) == void_type_node
4873 && TREE_CODE (orig_op2) != NOP_EXPR)
4874 result_type = qualify_type (type1, type2);
4875 else if (TYPE_MAIN_VARIANT (TREE_TYPE (type1)) == void_type_node)
4877 if (pedantic && TREE_CODE (type2) == FUNCTION_TYPE)
4878 pedwarn ("ANSI C++ forbids conditional expr between `void *' and function pointer");
4879 result_type = qualify_type (type1, type2);
4881 else if (TYPE_MAIN_VARIANT (TREE_TYPE (type2)) == void_type_node)
4883 if (pedantic && TREE_CODE (type1) == FUNCTION_TYPE)
4884 pedwarn ("ANSI C++ forbids conditional expr between `void *' and function pointer");
4885 result_type = qualify_type (type2, type1);
4888 else if (comptypes (type2, type1, 0))
4889 result_type = type2;
4890 else if (IS_AGGR_TYPE (TREE_TYPE (type1))
4891 && IS_AGGR_TYPE (TREE_TYPE (type2))
4892 && (result_type = common_base_type (TREE_TYPE (type1), TREE_TYPE (type2))))
4894 if (result_type == error_mark_node)
4896 cp_error ("common base type of types `%T' and `%T' is ambiguous",
4897 TREE_TYPE (type1), TREE_TYPE (type2));
4898 result_type = ptr_type_node;
4903 && result_type != TREE_TYPE (type1)
4904 && result_type != TREE_TYPE (type2))
4905 cp_pedwarn ("`%T' and `%T' converted to `%T *' in conditional expression",
4906 type1, type2, result_type);
4908 result_type = build_pointer_type (result_type);
4913 pedwarn ("pointer type mismatch in conditional expression");
4914 result_type = ptr_type_node;
4917 else if (code1 == POINTER_TYPE && code2 == INTEGER_TYPE)
4919 if (!integer_zerop (op2))
4920 pedwarn ("pointer/integer type mismatch in conditional expression");
4922 op2 = null_pointer_node;
4924 result_type = type1;
4926 else if (code2 == POINTER_TYPE && code1 == INTEGER_TYPE)
4928 if (!integer_zerop (op1))
4929 pedwarn ("pointer/integer type mismatch in conditional expression");
4931 op1 = null_pointer_node;
4933 result_type = type2;
4938 /* The match does not look good. If either is
4939 an aggregate value, try converting to a scalar type. */
4940 if (code1 == RECORD_TYPE && code2 == RECORD_TYPE)
4942 cp_error ("aggregate mismatch in conditional expression: `%T' vs `%T'", type1, type2);
4943 return error_mark_node;
4945 /* Warning: this code assumes that conversion between cv-variants of
4946 a type is done using NOP_EXPRs. */
4947 if (code1 == RECORD_TYPE && TYPE_HAS_CONVERSION (type1))
4949 tree tmp = build_pointer_type
4950 (build_type_variant (TREE_TYPE (type2), 1, 1));
4951 tmp = build_type_conversion (CONVERT_EXPR, tmp, op1, 0);
4952 if (tmp == NULL_TREE)
4954 cp_error ("incompatible types `%T' and `%T' in `?:'",
4956 return error_mark_node;
4958 if (tmp == error_mark_node)
4959 error ("ambiguous pointer conversion");
4962 result_type = common_type (type1, TREE_TYPE (tmp));
4965 else if (code2 == RECORD_TYPE && TYPE_HAS_CONVERSION (type2))
4967 tree tmp = build_pointer_type
4968 (build_type_variant (TREE_TYPE (type1), 1, 1));
4969 tmp = build_type_conversion (CONVERT_EXPR, tmp, op2, 0);
4970 if (tmp == NULL_TREE)
4972 cp_error ("incompatible types `%T' and `%T' in `?:'",
4974 return error_mark_node;
4976 if (tmp == error_mark_node)
4977 error ("ambiguous pointer conversion");
4980 result_type = common_type (type1, TREE_TYPE (tmp));
4983 else if (flag_cond_mismatch)
4984 result_type = void_type_node;
4987 error ("type mismatch in conditional expression");
4988 return error_mark_node;
4992 if (TREE_CODE (result_type) == POINTER_TYPE
4993 && TREE_CODE (TREE_TYPE (result_type)) == METHOD_TYPE)
4994 result_type = build_ptrmemfunc_type (result_type);
4996 if (result_type != TREE_TYPE (op1))
4997 op1 = convert_and_check (result_type, op1);
4998 if (result_type != TREE_TYPE (op2))
4999 op2 = convert_and_check (result_type, op2);
5001 if (TREE_CONSTANT (ifexp))
5002 return integer_zerop (ifexp) ? op2 : op1;
5004 return convert_from_reference
5005 (fold (build (COND_EXPR, result_type, ifexp, op1, op2)));
5008 /* Handle overloading of the ',' operator when needed. Otherwise,
5009 this function just builds an expression list. */
5011 build_x_compound_expr (list)
5014 tree rest = TREE_CHAIN (list);
5017 if (current_template_parms)
5018 return build_min_nt (COMPOUND_EXPR, list, NULL_TREE);
5020 if (rest == NULL_TREE)
5021 return build_compound_expr (list);
5023 result = build_opfncall (COMPOUND_EXPR, LOOKUP_NORMAL,
5024 TREE_VALUE (list), TREE_VALUE (rest), NULL_TREE);
5026 return build_x_compound_expr (tree_cons (NULL_TREE, result, TREE_CHAIN (rest)));
5028 if (! TREE_SIDE_EFFECTS (TREE_VALUE (list)))
5030 /* the left-hand operand of a comma expression is like an expression
5031 statement: we should warn if it doesn't have any side-effects,
5032 unless it was explicitly cast to (void). */
5033 if ((extra_warnings || warn_unused)
5034 && !(TREE_CODE (TREE_VALUE(list)) == CONVERT_EXPR
5035 && TREE_TYPE (TREE_VALUE(list)) == void_type_node))
5036 warning("left-hand operand of comma expression has no effect");
5038 #if 0 /* this requires a gcc backend patch to export warn_if_unused_value */
5039 else if (warn_unused)
5040 warn_if_unused_value (TREE_VALUE(list));
5043 return build_compound_expr (tree_cons (NULL_TREE, TREE_VALUE (list),
5044 build_tree_list (NULL_TREE, build_x_compound_expr (rest))));
5047 /* Given a list of expressions, return a compound expression
5048 that performs them all and returns the value of the last of them. */
5051 build_compound_expr (list)
5056 if (TREE_READONLY_DECL_P (TREE_VALUE (list)))
5057 TREE_VALUE (list) = decl_constant_value (TREE_VALUE (list));
5059 if (TREE_CHAIN (list) == 0)
5061 /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
5062 Strip such NOP_EXPRs, since LIST is used in non-lvalue context. */
5063 if (TREE_CODE (list) == NOP_EXPR
5064 && TREE_TYPE (list) == TREE_TYPE (TREE_OPERAND (list, 0)))
5065 list = TREE_OPERAND (list, 0);
5067 /* Convert arrays to pointers. */
5068 if (TREE_CODE (TREE_TYPE (TREE_VALUE (list))) == ARRAY_TYPE)
5069 return default_conversion (TREE_VALUE (list));
5071 return TREE_VALUE (list);
5074 rest = build_compound_expr (TREE_CHAIN (list));
5076 /* When pedantic, a compound expression cannot be a constant expression. */
5077 if (! TREE_SIDE_EFFECTS (TREE_VALUE (list)) && ! pedantic)
5080 return build (COMPOUND_EXPR, TREE_TYPE (rest),
5081 break_out_cleanups (TREE_VALUE (list)), rest);
5085 build_static_cast (type, expr)
5088 return build_c_cast (type, expr, 0);
5092 build_reinterpret_cast (type, expr)
5095 tree intype = TREE_TYPE (expr);
5097 if (current_template_parms)
5099 tree t = build_min (REINTERPRET_CAST_EXPR, type, expr);
5103 if (TYPE_PTRMEMFUNC_P (type))
5104 if (TYPE_PTRMEMFUNC_P (intype))
5105 return build1 (NOP_EXPR, type, expr);
5107 if (TYPE_PTRMEMFUNC_P (type))
5108 type = TYPE_PTRMEMFUNC_FN_TYPE (type);
5110 if (TYPE_PTRMEMFUNC_P (intype))
5111 intype = TYPE_PTRMEMFUNC_FN_TYPE (intype);
5113 if (! POINTER_TYPE_P (type) && ! TREE_CODE (type) == INTEGER_TYPE)
5115 cp_error ("reinterpret_cast cannot convert to type `%T'", type);
5116 return error_mark_node;
5118 if (! POINTER_TYPE_P (intype) && ! TREE_CODE (intype) == INTEGER_TYPE)
5120 cp_error ("reinterpret_cast cannot convert from type `%T'", type);
5121 return error_mark_node;
5123 if (TREE_CODE (type) == INTEGER_TYPE && TREE_CODE (intype) != POINTER_TYPE)
5125 cp_error ("reinterpret_cast cannot convert non-pointer type `%T' to `%T'",
5127 return error_mark_node;
5129 if (TREE_CODE (intype) == INTEGER_TYPE && TREE_CODE (type) != POINTER_TYPE)
5131 cp_error ("reinterpret_cast cannot convert `%T' to non-pointer type `%T'",
5133 return error_mark_node;
5136 if (TREE_CODE (type) == POINTER_TYPE && TREE_CODE (intype) == POINTER_TYPE)
5137 expr = convert (ptr_type_node, expr);
5139 return build_c_cast (type, expr, 0);
5143 build_const_cast (type, expr)
5146 tree intype = TREE_TYPE (expr);
5149 if (type == error_mark_node || expr == error_mark_node)
5150 return error_mark_node;
5152 if (TYPE_PTRMEMFUNC_P (type))
5153 type = TYPE_PTRMEMFUNC_FN_TYPE (type);
5154 if (TYPE_PTRMEMFUNC_P (intype))
5155 intype = TYPE_PTRMEMFUNC_FN_TYPE (intype);
5157 if (! POINTER_TYPE_P (type))
5159 cp_error ("const_cast cannot convert to non-pointer type `%T'", type);
5160 return error_mark_node;
5162 if (TREE_CODE (type) == REFERENCE_TYPE && ! real_lvalue_p (expr))
5164 cp_error ("const_cast cannot convert rvalue to type `%T'", type);
5165 return error_mark_node;
5167 if (TREE_CODE (type) == POINTER_TYPE && TREE_CODE (intype) != POINTER_TYPE)
5169 cp_error ("const_cast cannot convert non-pointer type `%T' to type `%T'",
5171 return error_mark_node;
5174 if (TREE_CODE (type) == REFERENCE_TYPE)
5176 t1 = TREE_TYPE (type);
5181 t1 = TREE_TYPE (type);
5182 t2 = TREE_TYPE (intype);
5184 for (; TREE_CODE (t1) == POINTER_TYPE && TREE_CODE (t2) == POINTER_TYPE;
5185 t1 = TREE_TYPE (t1), t2 = TREE_TYPE (t2))
5189 if (TREE_CODE (t1) == OFFSET_TYPE && TREE_CODE (t2) == OFFSET_TYPE)
5191 if (TYPE_OFFSET_BASETYPE (t1) != TYPE_OFFSET_BASETYPE (t2))
5193 cp_error ("const_cast cannot convert between pointers to members of different types `%T' and `%T'",
5194 TYPE_OFFSET_BASETYPE (t2), TYPE_OFFSET_BASETYPE (t1));
5195 return error_mark_node;
5197 t1 = TREE_TYPE (t1);
5198 t2 = TREE_TYPE (t2);
5201 if (TYPE_MAIN_VARIANT (t1) != TYPE_MAIN_VARIANT (t2))
5203 cp_error ("const_cast cannot convert unrelated type `%T' to `%T'",
5205 return error_mark_node;
5208 return build_c_cast (type, expr, 0);
5211 /* Build an expression representing a cast to type TYPE of expression EXPR.
5213 ALLOW_NONCONVERTING is true if we should allow non-converting constructors
5214 when doing the cast. */
5217 build_c_cast (type, expr, allow_nonconverting)
5220 int allow_nonconverting;
5222 register tree value = expr;
5224 if (type == error_mark_node || expr == error_mark_node)
5225 return error_mark_node;
5227 /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
5228 Strip such NOP_EXPRs if VALUE is being used in non-lvalue context. */
5229 if (TREE_CODE (type) != REFERENCE_TYPE
5230 && TREE_CODE (value) == NOP_EXPR
5231 && TREE_TYPE (value) == TREE_TYPE (TREE_OPERAND (value, 0)))
5232 value = TREE_OPERAND (value, 0);
5234 if (TREE_TYPE (expr)
5235 && TREE_CODE (TREE_TYPE (expr)) == OFFSET_TYPE
5236 && TREE_CODE (type) != OFFSET_TYPE)
5237 value = resolve_offset_ref (value);
5239 if (TREE_CODE (type) == ARRAY_TYPE)
5241 /* Allow casting from T1* to T2[] because Cfront allows it.
5242 NIHCL uses it. It is not valid ANSI C however, and hence, not
5244 if (TREE_CODE (TREE_TYPE (expr)) == POINTER_TYPE)
5247 pedwarn ("ANSI C++ forbids casting to an array type");
5248 type = build_pointer_type (TREE_TYPE (type));
5252 error ("ANSI C++ forbids casting to an array type");
5253 return error_mark_node;
5257 if (TREE_CODE (type) == FUNCTION_TYPE
5258 || TREE_CODE (type) == METHOD_TYPE)
5260 cp_error ("casting to function type `%T'", type);
5261 return error_mark_node;
5264 if (IS_SIGNATURE (type))
5266 error ("cast specifies signature type");
5267 return error_mark_node;
5270 if (current_template_parms)
5272 tree t = build_min (CAST_EXPR, type,
5273 min_tree_cons (NULL_TREE, value, NULL_TREE));
5277 if (TREE_CODE (type) == VOID_TYPE)
5278 value = build1 (CONVERT_EXPR, type, value);
5279 else if (TREE_TYPE (value) == NULL_TREE
5280 || type_unknown_p (value))
5282 value = instantiate_type (type, value, 1);
5284 if (value == error_mark_node)
5285 return error_mark_node;
5292 /* Convert functions and arrays to pointers and
5293 convert references to their expanded types,
5294 but don't convert any other types. */
5295 if (TREE_CODE (TREE_TYPE (value)) == FUNCTION_TYPE
5296 || TREE_CODE (TREE_TYPE (value)) == METHOD_TYPE
5297 || TREE_CODE (TREE_TYPE (value)) == ARRAY_TYPE
5298 || TREE_CODE (TREE_TYPE (value)) == REFERENCE_TYPE)
5299 value = default_conversion (value);
5300 otype = TREE_TYPE (value);
5302 /* Optionally warn about potentially worrisome casts. */
5305 && TREE_CODE (type) == POINTER_TYPE
5306 && TREE_CODE (otype) == POINTER_TYPE)
5308 /* For C++ we make these regular warnings, rather than
5309 softening them into pedwarns. */
5310 if (TYPE_VOLATILE (TREE_TYPE (otype))
5311 && ! TYPE_VOLATILE (TREE_TYPE (type)))
5312 warning ("cast discards `volatile' from pointer target type");
5313 if (TYPE_READONLY (TREE_TYPE (otype))
5314 && ! TYPE_READONLY (TREE_TYPE (type)))
5315 warning ("cast discards `const' from pointer target type");
5318 /* Warn about possible alignment problems. */
5319 if (STRICT_ALIGNMENT && warn_cast_align
5320 && TREE_CODE (type) == POINTER_TYPE
5321 && TREE_CODE (otype) == POINTER_TYPE
5322 && TREE_CODE (TREE_TYPE (otype)) != VOID_TYPE
5323 && TREE_CODE (TREE_TYPE (otype)) != FUNCTION_TYPE
5324 && TYPE_ALIGN (TREE_TYPE (type)) > TYPE_ALIGN (TREE_TYPE (otype)))
5325 warning ("cast increases required alignment of target type");
5328 /* We should see about re-enabling these, they seem useful to
5330 if (TREE_CODE (type) == INTEGER_TYPE
5331 && TREE_CODE (otype) == POINTER_TYPE
5332 && TYPE_PRECISION (type) != TYPE_PRECISION (otype))
5333 warning ("cast from pointer to integer of different size");
5335 if (TREE_CODE (type) == POINTER_TYPE
5336 && TREE_CODE (otype) == INTEGER_TYPE
5337 && TYPE_PRECISION (type) != TYPE_PRECISION (otype)
5338 /* Don't warn about converting 0 to pointer,
5339 provided the 0 was explicit--not cast or made by folding. */
5340 && !(TREE_CODE (value) == INTEGER_CST && integer_zerop (value)))
5341 warning ("cast to pointer from integer of different size");
5344 flag = allow_nonconverting ? CONV_NONCONVERTING : 0;
5346 if (TREE_CODE (type) == REFERENCE_TYPE)
5347 value = (convert_from_reference
5348 (convert_to_reference (type, value, CONV_OLD_CONVERT|flag,
5349 LOOKUP_COMPLAIN, NULL_TREE)));
5354 if (TREE_READONLY_DECL_P (value))
5355 value = decl_constant_value (value);
5358 value = convert_force (type, value, flag);
5360 /* Ignore any integer overflow caused by the cast. */
5361 if (TREE_CODE (value) == INTEGER_CST)
5363 TREE_OVERFLOW (value) = TREE_OVERFLOW (ovalue);
5364 TREE_CONSTANT_OVERFLOW (value) = TREE_CONSTANT_OVERFLOW (ovalue);
5369 /* Always produce some operator for an explicit cast,
5370 so we can tell (for -pedantic) that the cast is no lvalue.
5371 Also, pedantically, don't let (void *) (FOO *) 0 be a null
5372 pointer constant. */
5373 if (TREE_CODE (type) != REFERENCE_TYPE
5376 && TREE_CODE (value) == INTEGER_CST
5377 && TREE_CODE (expr) == INTEGER_CST
5378 && TREE_CODE (TREE_TYPE (expr)) != INTEGER_TYPE)))
5379 value = non_lvalue (value);
5385 expand_target_expr (t)
5388 extern int temp_slot_level;
5389 extern int target_temp_slot_level;
5390 int old_temp_level = target_temp_slot_level;
5392 tree xval = make_node (RTL_EXPR);
5395 /* Any TARGET_EXPR temps live only as long as the outer temp level.
5396 Since they are preserved in this new inner level, we know they
5397 will make it into the outer level. */
5399 target_temp_slot_level = temp_slot_level;
5401 do_pending_stack_adjust ();
5402 start_sequence_for_rtl_expr (xval);
5404 rtxval = expand_expr (t, NULL, VOIDmode, 0);
5405 do_pending_stack_adjust ();
5406 TREE_SIDE_EFFECTS (xval) = 1;
5407 RTL_EXPR_SEQUENCE (xval) = get_insns ();
5409 RTL_EXPR_RTL (xval) = rtxval;
5410 TREE_TYPE (xval) = TREE_TYPE (t);
5413 target_temp_slot_level = old_temp_level;
5418 /* Build an assignment expression of lvalue LHS from value RHS.
5419 MODIFYCODE is the code for a binary operator that we use
5420 to combine the old value of LHS with RHS to get the new value.
5421 Or else MODIFYCODE is NOP_EXPR meaning do a simple assignment.
5423 C++: If MODIFYCODE is INIT_EXPR, then leave references unbashed.
5426 build_modify_expr (lhs, modifycode, rhs)
5428 enum tree_code modifycode;
5431 register tree result;
5433 tree lhstype = TREE_TYPE (lhs);
5434 tree olhstype = lhstype;
5437 /* Avoid duplicate error messages from operands that had errors. */
5438 if (TREE_CODE (lhs) == ERROR_MARK || TREE_CODE (rhs) == ERROR_MARK)
5439 return error_mark_node;
5441 /* Types that aren't fully specified cannot be used in assignments. */
5442 lhs = require_complete_type (lhs);
5446 /* Handle assignment to signature pointers/refs. */
5448 if (TYPE_LANG_SPECIFIC (lhstype) &&
5449 (IS_SIGNATURE_POINTER (lhstype) || IS_SIGNATURE_REFERENCE (lhstype)))
5451 return build_signature_pointer_constructor (lhs, rhs);
5454 /* Handle control structure constructs used as "lvalues". */
5456 switch (TREE_CODE (lhs))
5458 /* Handle --foo = 5; as these are valid constructs in C++ */
5459 case PREDECREMENT_EXPR:
5460 case PREINCREMENT_EXPR:
5461 if (TREE_SIDE_EFFECTS (TREE_OPERAND (lhs, 0)))
5462 lhs = build (TREE_CODE (lhs), TREE_TYPE (lhs),
5463 stabilize_reference (TREE_OPERAND (lhs, 0)),
5464 TREE_OPERAND (lhs, 1));
5465 return build (COMPOUND_EXPR, lhstype,
5467 build_modify_expr (TREE_OPERAND (lhs, 0),
5470 /* Handle (a, b) used as an "lvalue". */
5472 newrhs = build_modify_expr (TREE_OPERAND (lhs, 1),
5474 if (TREE_CODE (newrhs) == ERROR_MARK)
5475 return error_mark_node;
5476 return build (COMPOUND_EXPR, lhstype,
5477 TREE_OPERAND (lhs, 0), newrhs);
5480 newrhs = build_modify_expr (TREE_OPERAND (lhs, 0), modifycode, rhs);
5481 if (TREE_CODE (newrhs) == ERROR_MARK)
5482 return error_mark_node;
5483 return build (COMPOUND_EXPR, lhstype, lhs, newrhs);
5485 /* Handle (a ? b : c) used as an "lvalue". */
5487 rhs = save_expr (rhs);
5489 /* Produce (a ? (b = rhs) : (c = rhs))
5490 except that the RHS goes through a save-expr
5491 so the code to compute it is only emitted once. */
5493 = build_conditional_expr (TREE_OPERAND (lhs, 0),
5494 build_modify_expr (convert (TREE_TYPE (lhs), TREE_OPERAND (lhs, 1)),
5496 build_modify_expr (convert (TREE_TYPE (lhs), TREE_OPERAND (lhs, 2)),
5498 if (TREE_CODE (cond) == ERROR_MARK)
5500 /* Make sure the code to compute the rhs comes out
5501 before the split. */
5502 return build (COMPOUND_EXPR, TREE_TYPE (lhs),
5503 /* Case to void to suppress warning
5504 from warn_if_unused_value. */
5505 convert (void_type_node, rhs), cond);
5509 if (TREE_CODE (lhs) == OFFSET_REF)
5511 if (TREE_OPERAND (lhs, 0) == NULL_TREE)
5513 /* Static class member? */
5514 tree member = TREE_OPERAND (lhs, 1);
5515 if (TREE_CODE (member) == VAR_DECL)
5519 compiler_error ("invalid static class member");
5520 return error_mark_node;
5524 lhs = resolve_offset_ref (lhs);
5526 olhstype = lhstype = TREE_TYPE (lhs);
5529 if (TREE_CODE (lhstype) == REFERENCE_TYPE
5530 && modifycode != INIT_EXPR)
5532 lhs = convert_from_reference (lhs);
5533 olhstype = lhstype = TREE_TYPE (lhs);
5536 /* If a binary op has been requested, combine the old LHS value with the RHS
5537 producing the value we should actually store into the LHS. */
5539 if (modifycode == INIT_EXPR)
5541 if (! IS_AGGR_TYPE (lhstype))
5542 /* Do the default thing */;
5543 else if (! TYPE_HAS_CONSTRUCTOR (lhstype))
5545 cp_error ("`%T' has no constructors", lhstype);
5546 return error_mark_node;
5548 else if (TYPE_HAS_TRIVIAL_INIT_REF (lhstype)
5549 && TYPE_MAIN_VARIANT (lhstype) == TYPE_MAIN_VARIANT (TREE_TYPE (newrhs)))
5550 /* Do the default thing */;
5553 result = build_method_call (lhs, ctor_identifier,
5554 build_tree_list (NULL_TREE, rhs),
5555 TYPE_BINFO (lhstype), LOOKUP_NORMAL);
5556 if (result == NULL_TREE)
5557 return error_mark_node;
5561 else if (modifycode == NOP_EXPR)
5563 /* `operator=' is not an inheritable operator. */
5564 if (! IS_AGGR_TYPE (lhstype))
5565 /* Do the default thing */;
5566 else if (! TYPE_HAS_ASSIGNMENT (lhstype))
5568 cp_error ("`%T' does not define operator=", lhstype);
5569 return error_mark_node;
5571 else if (TYPE_HAS_TRIVIAL_ASSIGN_REF (lhstype)
5572 && TYPE_MAIN_VARIANT (lhstype) == TYPE_MAIN_VARIANT (TREE_TYPE (newrhs)))
5575 /* If we care about this, do overload resolution. */
5576 build_opfncall (MODIFY_EXPR, LOOKUP_NORMAL,
5577 lhs, rhs, make_node (NOP_EXPR));
5579 /* Do the default thing */;
5583 result = build_opfncall (MODIFY_EXPR, LOOKUP_NORMAL,
5584 lhs, rhs, make_node (NOP_EXPR));
5585 if (result == NULL_TREE)
5586 return error_mark_node;
5591 else if (PROMOTES_TO_AGGR_TYPE (lhstype, REFERENCE_TYPE))
5593 /* This case must convert to some sort of lvalue that
5594 can participate in an op= operation. */
5597 if (build_default_binary_type_conversion (modifycode, &lhs_tmp, &rhs_tmp))
5599 lhs = stabilize_reference (lhs_tmp);
5600 /* Forget it was ever anything else. */
5601 olhstype = lhstype = TREE_TYPE (lhs);
5602 newrhs = build_binary_op (modifycode, lhs, rhs_tmp, 1);
5606 cp_error ("no match for `%Q(%#T, %#T)'", modifycode,
5607 TREE_TYPE (lhs), TREE_TYPE (rhs));
5608 return error_mark_node;
5613 lhs = stabilize_reference (lhs);
5614 newrhs = build_binary_op (modifycode, lhs, rhs, 1);
5615 if (newrhs == error_mark_node)
5617 cp_error (" in evaluation of `%Q(%#T, %#T)'", modifycode,
5618 TREE_TYPE (lhs), TREE_TYPE (rhs));
5619 return error_mark_node;
5623 /* Handle a cast used as an "lvalue".
5624 We have already performed any binary operator using the value as cast.
5625 Now convert the result to the cast type of the lhs,
5626 and then true type of the lhs and store it there;
5627 then convert result back to the cast type to be the value
5628 of the assignment. */
5630 switch (TREE_CODE (lhs))
5635 case FIX_TRUNC_EXPR:
5636 case FIX_FLOOR_EXPR:
5637 case FIX_ROUND_EXPR:
5639 if (TREE_CODE (TREE_TYPE (newrhs)) == ARRAY_TYPE
5640 || TREE_CODE (TREE_TYPE (newrhs)) == FUNCTION_TYPE
5641 || TREE_CODE (TREE_TYPE (newrhs)) == METHOD_TYPE
5642 || TREE_CODE (TREE_TYPE (newrhs)) == OFFSET_TYPE)
5643 newrhs = default_conversion (newrhs);
5645 tree inner_lhs = TREE_OPERAND (lhs, 0);
5647 if (! lvalue_p (lhs) && pedantic)
5648 pedwarn ("cast to non-reference type used as lvalue");
5650 result = build_modify_expr (inner_lhs, NOP_EXPR,
5651 convert (TREE_TYPE (inner_lhs),
5652 convert (lhstype, newrhs)));
5653 if (TREE_CODE (result) == ERROR_MARK)
5655 return convert (TREE_TYPE (lhs), result);
5659 /* Now we have handled acceptable kinds of LHS that are not truly lvalues.
5660 Reject anything strange now. */
5662 if (!lvalue_or_else (lhs, "assignment"))
5663 return error_mark_node;
5665 GNU_xref_assign (lhs);
5667 /* Warn about storing in something that is `const'. */
5668 /* For C++, don't warn if this is initialization. */
5669 if (modifycode != INIT_EXPR
5670 /* For assignment to `const' signature pointer/reference fields,
5671 don't warn either, we already printed a better message before. */
5672 && ! (TREE_CODE (lhs) == COMPONENT_REF
5673 && (IS_SIGNATURE_POINTER (TREE_TYPE (TREE_OPERAND (lhs, 0)))
5674 || IS_SIGNATURE_REFERENCE (TREE_TYPE (TREE_OPERAND (lhs, 0)))))
5675 && (TREE_READONLY (lhs) || TYPE_READONLY (lhstype)
5676 || ((TREE_CODE (lhstype) == RECORD_TYPE
5677 || TREE_CODE (lhstype) == UNION_TYPE)
5678 && C_TYPE_FIELDS_READONLY (lhstype))
5679 || (TREE_CODE (lhstype) == REFERENCE_TYPE
5680 && TYPE_READONLY (TREE_TYPE (lhstype)))))
5681 readonly_error (lhs, "assignment", 0);
5683 /* If storing into a structure or union member,
5684 it has probably been given type `int'.
5685 Compute the type that would go with
5686 the actual amount of storage the member occupies. */
5688 if (TREE_CODE (lhs) == COMPONENT_REF
5689 && (TREE_CODE (lhstype) == INTEGER_TYPE
5690 || TREE_CODE (lhstype) == REAL_TYPE
5691 || TREE_CODE (lhstype) == ENUMERAL_TYPE))
5693 lhstype = TREE_TYPE (get_unwidened (lhs, 0));
5695 /* If storing in a field that is in actuality a short or narrower
5696 than one, we must store in the field in its actual type. */
5698 if (lhstype != TREE_TYPE (lhs))
5700 lhs = copy_node (lhs);
5701 TREE_TYPE (lhs) = lhstype;
5705 /* check to see if there is an assignment to `this' */
5706 if (lhs == current_class_decl)
5708 if (flag_this_is_variable > 0
5709 && DECL_NAME (current_function_decl) != NULL_TREE
5710 && (DECL_NAME (current_function_decl)
5711 != constructor_name (current_class_type)))
5712 warning ("assignment to `this' not in constructor or destructor");
5713 current_function_just_assigned_this = 1;
5716 /* The TREE_TYPE of RHS may be TYPE_UNKNOWN. This can happen
5717 when the type of RHS is not yet known, i.e. its type
5718 is inherited from LHS. */
5719 rhs = require_instantiated_type (lhstype, newrhs, error_mark_node);
5720 if (rhs == error_mark_node)
5721 return error_mark_node;
5724 if (modifycode != INIT_EXPR)
5726 /* Make modifycode now either a NOP_EXPR or an INIT_EXPR. */
5727 modifycode = NOP_EXPR;
5728 /* Reference-bashing */
5729 if (TREE_CODE (lhstype) == REFERENCE_TYPE)
5731 tree tmp = convert_from_reference (lhs);
5732 lhstype = TREE_TYPE (tmp);
5733 if (TYPE_SIZE (lhstype) == 0)
5735 incomplete_type_error (lhs, lhstype);
5736 return error_mark_node;
5741 if (TREE_CODE (TREE_TYPE (newrhs)) == REFERENCE_TYPE)
5743 tree tmp = convert_from_reference (newrhs);
5744 if (TYPE_SIZE (TREE_TYPE (tmp)) == 0)
5746 incomplete_type_error (newrhs, TREE_TYPE (tmp));
5747 return error_mark_node;
5753 if (TREE_SIDE_EFFECTS (lhs))
5754 lhs = stabilize_reference (lhs);
5755 if (TREE_SIDE_EFFECTS (newrhs))
5756 newrhs = stabilize_reference (newrhs);
5758 /* Convert new value to destination type. */
5760 if (TREE_CODE (lhstype) == ARRAY_TYPE)
5764 if (! comptypes (lhstype, TREE_TYPE (rhs), 0))
5766 cp_error ("incompatible types in assignment of `%T' to `%T'",
5767 TREE_TYPE (rhs), lhstype);
5768 return error_mark_node;
5771 /* Allow array assignment in compiler-generated code. */
5772 if (pedantic && ! DECL_ARTIFICIAL (current_function_decl))
5773 pedwarn ("ANSI C++ forbids assignment of arrays");
5775 /* Have to wrap this in RTL_EXPR for two cases:
5776 in base or member initialization and if we
5777 are a branch of a ?: operator. Since we
5778 can't easily know the latter, just do it always. */
5780 result = make_node (RTL_EXPR);
5782 TREE_TYPE (result) = void_type_node;
5783 do_pending_stack_adjust ();
5784 start_sequence_for_rtl_expr (result);
5786 /* As a matter of principle, `start_sequence' should do this. */
5789 from_array = TREE_CODE (TREE_TYPE (newrhs)) == ARRAY_TYPE
5790 ? 1 + (modifycode != INIT_EXPR): 0;
5791 expand_vec_init (lhs, lhs, array_type_nelts (lhstype), newrhs,
5794 do_pending_stack_adjust ();
5796 TREE_SIDE_EFFECTS (result) = 1;
5797 RTL_EXPR_SEQUENCE (result) = get_insns ();
5798 RTL_EXPR_RTL (result) = const0_rtx;
5803 if (modifycode == INIT_EXPR)
5805 newrhs = convert_for_initialization (lhs, lhstype, newrhs, LOOKUP_NORMAL,
5806 "assignment", NULL_TREE, 0);
5807 if (lhs == DECL_RESULT (current_function_decl))
5809 if (DECL_INITIAL (lhs))
5810 warning ("return value from function receives multiple initializations");
5811 DECL_INITIAL (lhs) = newrhs;
5816 /* Avoid warnings on enum bit fields. */
5817 if (TREE_CODE (olhstype) == ENUMERAL_TYPE
5818 && TREE_CODE (lhstype) == INTEGER_TYPE)
5820 newrhs = convert_for_assignment (olhstype, newrhs, "assignment",
5822 newrhs = convert_force (lhstype, newrhs, 0);
5825 newrhs = convert_for_assignment (lhstype, newrhs, "assignment",
5827 if (TREE_CODE (newrhs) == CALL_EXPR
5828 && TYPE_NEEDS_CONSTRUCTING (lhstype))
5829 newrhs = build_cplus_new (lhstype, newrhs);
5831 /* Can't initialize directly from a TARGET_EXPR, since that would
5832 cause the lhs to be constructed twice, and possibly result in
5833 accidental self-initialization. So we force the TARGET_EXPR to be
5834 expanded without a target. */
5835 if (TREE_CODE (newrhs) == TARGET_EXPR)
5836 newrhs = build (COMPOUND_EXPR, TREE_TYPE (newrhs), newrhs,
5837 TREE_VALUE (newrhs));
5840 if (TREE_CODE (newrhs) == ERROR_MARK)
5841 return error_mark_node;
5843 if (TREE_CODE (newrhs) == COND_EXPR)
5846 tree cond = TREE_OPERAND (newrhs, 0);
5848 if (TREE_SIDE_EFFECTS (lhs))
5849 cond = build_compound_expr (tree_cons
5851 build_tree_list (NULL_TREE, cond)));
5853 /* Cannot have two identical lhs on this one tree (result) as preexpand
5854 calls will rip them out and fill in RTL for them, but when the
5855 rtl is generated, the calls will only be in the first side of the
5856 condition, not on both, or before the conditional jump! (mrs) */
5857 lhs1 = break_out_calls (lhs);
5860 /* If there's no change, the COND_EXPR behaves like any other rhs. */
5861 result = build (modifycode == NOP_EXPR ? MODIFY_EXPR : INIT_EXPR,
5862 lhstype, lhs, newrhs);
5865 tree result_type = TREE_TYPE (newrhs);
5866 /* We have to convert each arm to the proper type because the
5867 types may have been munged by constant folding. */
5869 = build (COND_EXPR, result_type, cond,
5870 build_modify_expr (lhs, modifycode,
5871 convert (result_type,
5872 TREE_OPERAND (newrhs, 1))),
5873 build_modify_expr (lhs1, modifycode,
5874 convert (result_type,
5875 TREE_OPERAND (newrhs, 2))));
5879 result = build (modifycode == NOP_EXPR ? MODIFY_EXPR : INIT_EXPR,
5880 lhstype, lhs, newrhs);
5882 TREE_SIDE_EFFECTS (result) = 1;
5884 /* If we got the LHS in a different type for storing in,
5885 convert the result back to the nominal type of LHS
5886 so that the value we return always has the same type
5887 as the LHS argument. */
5889 if (olhstype == TREE_TYPE (result))
5891 /* Avoid warnings converting integral types back into enums
5892 for enum bit fields. */
5893 if (TREE_CODE (TREE_TYPE (result)) == INTEGER_TYPE
5894 && TREE_CODE (olhstype) == ENUMERAL_TYPE)
5896 result = build (COMPOUND_EXPR, olhstype, result, olhs);
5897 TREE_NO_UNUSED_WARNING (result) = 1;
5900 return convert_for_assignment (olhstype, result, "assignment",
5905 build_x_modify_expr (lhs, modifycode, rhs)
5907 enum tree_code modifycode;
5910 if (current_template_parms)
5911 return build_min_nt (MODOP_EXPR, lhs,
5912 build_min_nt (modifycode, 0, 0), rhs);
5914 if (modifycode != NOP_EXPR)
5916 tree rval = build_opfncall (MODIFY_EXPR, LOOKUP_NORMAL, lhs, rhs,
5917 make_node (modifycode));
5921 return build_modify_expr (lhs, modifycode, rhs);
5924 /* Return 0 if EXP is not a valid lvalue in this language
5925 even though `lvalue_or_else' would accept it. */
5928 language_lvalue_valid (exp)
5934 /* Get difference in deltas for different pointer to member function
5935 types. Return integer_zero_node, if FROM cannot be converted to a
5936 TO type. If FORCE is true, then allow reverse conversions as well. */
5938 get_delta_difference (from, to, force)
5942 tree delta = integer_zero_node;
5948 /* Should get_base_distance here, so we can check if any thing along the
5949 path is virtual, and we need to make sure we stay
5950 inside the real binfos when going through virtual bases.
5951 Maybe we should replace virtual bases with
5952 binfo_member (...CLASSTYPE_VBASECLASSES...)... (mrs) */
5953 binfo = get_binfo (from, to, 1);
5954 if (binfo == error_mark_node)
5956 error (" in pointer to member function conversion");
5963 error_not_base_type (from, to);
5964 error (" in pointer to member function conversion");
5967 binfo = get_binfo (to, from, 1);
5968 if (binfo == error_mark_node)
5970 error (" in pointer to member function conversion");
5975 cp_error ("cannot convert pointer to member of type %T to unrelated pointer to member of type %T", from, to);
5978 if (TREE_VIA_VIRTUAL (binfo))
5980 warning ("pointer to member conversion to virtual base class will only work if you are very careful");
5982 return build_binary_op (MINUS_EXPR,
5984 BINFO_OFFSET (binfo), 1);
5986 if (TREE_VIA_VIRTUAL (binfo))
5988 warning ("pointer to member conversion from virtual base class will only work if you are very careful");
5990 return BINFO_OFFSET (binfo);
5993 /* Build a constructor for a pointer to member function. It can be
5994 used to initialize global variables, local variable, or used
5995 as a value in expressions. TYPE is the POINTER to METHOD_TYPE we
5998 If FORCE is non-zero, then force this conversion, even if
5999 we would rather not do it. Usually set when using an explicit
6002 Return error_mark_node, if something goes wrong. */
6005 build_ptrmemfunc (type, pfn, force)
6009 tree index = integer_zero_node;
6010 tree delta = integer_zero_node;
6011 tree delta2 = integer_zero_node;
6016 /* Handle multiple conversions of pointer to member functions. */
6017 if (TYPE_PTRMEMFUNC_P (TREE_TYPE (pfn)))
6019 tree ndelta, ndelta2, nindex;
6020 /* Is is already the right type? */
6021 if (type == TYPE_PTRMEMFUNC_FN_TYPE (TREE_TYPE (pfn)))
6024 if (TREE_CODE (pfn) != CONSTRUCTOR)
6027 ndelta = convert (ptrdiff_type_node, build_component_ref (pfn, delta_identifier, 0, 0));
6028 ndelta2 = convert (ptrdiff_type_node, DELTA2_FROM_PTRMEMFUNC (pfn));
6029 index = build_component_ref (pfn, index_identifier, 0, 0);
6030 delta = get_delta_difference (TYPE_METHOD_BASETYPE (TREE_TYPE (TYPE_PTRMEMFUNC_FN_TYPE (TREE_TYPE (pfn)))),
6031 TYPE_METHOD_BASETYPE (TREE_TYPE (type)),
6033 delta = build_binary_op (PLUS_EXPR, delta, ndelta, 1);
6034 delta2 = build_binary_op (PLUS_EXPR, ndelta2, delta2, 1);
6035 e1 = fold (build (GT_EXPR, boolean_type_node, index, integer_zero_node));
6037 u = build_nt (CONSTRUCTOR, 0, tree_cons (delta2_identifier, delta2, NULL_TREE));
6038 u = build_nt (CONSTRUCTOR, 0, tree_cons (NULL_TREE, delta,
6039 tree_cons (NULL_TREE, index,
6040 tree_cons (NULL_TREE, u, NULL_TREE))));
6041 e2 = digest_init (TYPE_GET_PTRMEMFUNC_TYPE (type), u, (tree*)0);
6043 pfn = PFN_FROM_PTRMEMFUNC (pfn);
6044 npfn = build1 (NOP_EXPR, type, pfn);
6045 TREE_CONSTANT (npfn) = TREE_CONSTANT (pfn);
6047 u = build_nt (CONSTRUCTOR, 0, tree_cons (pfn_identifier, npfn, NULL_TREE));
6048 u = build_nt (CONSTRUCTOR, 0, tree_cons (NULL_TREE, delta,
6049 tree_cons (NULL_TREE, index,
6050 tree_cons (NULL_TREE, u, NULL_TREE))));
6051 e3 = digest_init (TYPE_GET_PTRMEMFUNC_TYPE (type), u, (tree*)0);
6052 return build_conditional_expr (e1, e2, e3);
6055 ndelta = TREE_VALUE (CONSTRUCTOR_ELTS (pfn));
6056 nindex = TREE_VALUE (TREE_CHAIN (CONSTRUCTOR_ELTS (pfn)));
6057 npfn = TREE_VALUE (TREE_CHAIN (TREE_CHAIN (CONSTRUCTOR_ELTS (pfn))));
6058 npfn = TREE_VALUE (CONSTRUCTOR_ELTS (npfn));
6059 if (integer_zerop (nindex))
6060 pfn = integer_zero_node;
6061 else if (integer_zerop (fold (size_binop (PLUS_EXPR, nindex, integer_one_node))))
6064 delta = get_delta_difference (TYPE_METHOD_BASETYPE (TREE_TYPE (TYPE_PTRMEMFUNC_FN_TYPE (TREE_TYPE (pfn)))),
6065 TYPE_METHOD_BASETYPE (TREE_TYPE (type)),
6067 delta = build_binary_op (PLUS_EXPR, delta, ndelta, 1);
6068 pfn = build1 (NOP_EXPR, type, npfn);
6069 TREE_CONSTANT (pfn) = TREE_CONSTANT (npfn);
6071 u = build_nt (CONSTRUCTOR, 0, tree_cons (pfn_identifier, pfn, NULL_TREE));
6072 u = build_nt (CONSTRUCTOR, 0, tree_cons (NULL_TREE, delta,
6073 tree_cons (NULL_TREE, nindex,
6074 tree_cons (NULL_TREE, u, NULL_TREE))));
6075 e3 = digest_init (TYPE_GET_PTRMEMFUNC_TYPE (type), u, (tree*)0);
6080 sorry ("value casting of variable nonnull pointer to member functions not supported");
6081 return error_mark_node;
6085 /* Handle null pointer to member function conversions. */
6086 if (integer_zerop (pfn))
6088 pfn = build_c_cast (type, integer_zero_node, 0);
6089 u = build_nt (CONSTRUCTOR, 0, tree_cons (pfn_identifier, pfn, NULL_TREE));
6090 u = build_nt (CONSTRUCTOR, 0, tree_cons (NULL_TREE, integer_zero_node,
6091 tree_cons (NULL_TREE, integer_zero_node,
6092 tree_cons (NULL_TREE, u, NULL_TREE))));
6093 return digest_init (TYPE_GET_PTRMEMFUNC_TYPE (type), u, (tree*)0);
6096 if (TREE_CODE (pfn) == TREE_LIST
6097 || (TREE_CODE (pfn) == ADDR_EXPR
6098 && TREE_CODE (TREE_OPERAND (pfn, 0)) == TREE_LIST))
6100 pfn = instantiate_type (type, pfn, 1);
6101 if (pfn == error_mark_node)
6102 return error_mark_node;
6103 if (TREE_CODE (pfn) != ADDR_EXPR)
6104 pfn = build_unary_op (ADDR_EXPR, pfn, 0);
6107 /* Allow pointer to member conversions here. */
6108 delta = get_delta_difference (TYPE_METHOD_BASETYPE (TREE_TYPE (TREE_TYPE (pfn))),
6109 TYPE_METHOD_BASETYPE (TREE_TYPE (type)),
6111 delta2 = build_binary_op (PLUS_EXPR, delta2, delta, 1);
6113 if (TREE_CODE (TREE_OPERAND (pfn, 0)) != FUNCTION_DECL)
6114 warning ("assuming pointer to member function is non-virtual");
6116 if (TREE_CODE (TREE_OPERAND (pfn, 0)) == FUNCTION_DECL
6117 && DECL_VINDEX (TREE_OPERAND (pfn, 0)))
6119 /* Find the offset to the vfield pointer in the object. */
6120 vfield_offset = get_binfo (DECL_CONTEXT (TREE_OPERAND (pfn, 0)),
6121 DECL_CLASS_CONTEXT (TREE_OPERAND (pfn, 0)),
6123 vfield_offset = get_vfield_offset (vfield_offset);
6124 delta2 = size_binop (PLUS_EXPR, vfield_offset, delta2);
6126 /* Map everything down one to make room for the null pointer to member. */
6127 index = size_binop (PLUS_EXPR,
6128 DECL_VINDEX (TREE_OPERAND (pfn, 0)),
6130 u = build_nt (CONSTRUCTOR, 0, tree_cons (delta2_identifier, delta2, NULL_TREE));
6134 index = size_binop (MINUS_EXPR, integer_zero_node, integer_one_node);
6136 npfn = build1 (NOP_EXPR, type, pfn);
6137 TREE_CONSTANT (npfn) = TREE_CONSTANT (pfn);
6139 u = build_nt (CONSTRUCTOR, 0, tree_cons (pfn_identifier, npfn, NULL_TREE));
6142 u = build_nt (CONSTRUCTOR, 0, tree_cons (NULL_TREE, delta,
6143 tree_cons (NULL_TREE, index,
6144 tree_cons (NULL_TREE, u, NULL_TREE))));
6145 return digest_init (TYPE_GET_PTRMEMFUNC_TYPE (type), u, (tree*)0);
6148 /* Convert value RHS to type TYPE as preparation for an assignment
6149 to an lvalue of type TYPE.
6150 The real work of conversion is done by `convert'.
6151 The purpose of this function is to generate error messages
6152 for assignments that are not allowed in C.
6153 ERRTYPE is a string to use in error messages:
6154 "assignment", "return", etc.
6156 C++: attempts to allow `convert' to find conversions involving
6157 implicit type conversion between aggregate and scalar types
6158 as per 8.5.6 of C++ manual. Does not randomly dereference
6159 pointers to aggregates! */
6162 convert_for_assignment (type, rhs, errtype, fndecl, parmnum)
6168 register enum tree_code codel = TREE_CODE (type);
6169 register tree rhstype;
6170 register enum tree_code coder = TREE_CODE (TREE_TYPE (rhs));
6172 if (coder == UNKNOWN_TYPE)
6173 rhs = instantiate_type (type, rhs, 1);
6175 if (coder == ERROR_MARK)
6176 return error_mark_node;
6178 if (codel == OFFSET_TYPE)
6180 type = TREE_TYPE (type);
6181 codel = TREE_CODE (type);
6184 /* Strip NON_LVALUE_EXPRs since we aren't using as an lvalue. */
6185 if (TREE_CODE (rhs) == NON_LVALUE_EXPR)
6186 rhs = TREE_OPERAND (rhs, 0);
6188 if (rhs == error_mark_node)
6189 return error_mark_node;
6191 if (TREE_VALUE (rhs) == error_mark_node)
6192 return error_mark_node;
6194 if (TREE_CODE (TREE_TYPE (rhs)) == OFFSET_TYPE)
6196 rhs = resolve_offset_ref (rhs);
6197 if (rhs == error_mark_node)
6198 return error_mark_node;
6199 rhstype = TREE_TYPE (rhs);
6200 coder = TREE_CODE (rhstype);
6203 if (TREE_CODE (TREE_TYPE (rhs)) == ARRAY_TYPE
6204 || TREE_CODE (TREE_TYPE (rhs)) == FUNCTION_TYPE
6205 || TREE_CODE (TREE_TYPE (rhs)) == METHOD_TYPE)
6206 rhs = default_conversion (rhs);
6207 else if (TREE_CODE (TREE_TYPE (rhs)) == REFERENCE_TYPE)
6208 rhs = convert_from_reference (rhs);
6210 rhstype = TREE_TYPE (rhs);
6211 coder = TREE_CODE (rhstype);
6213 /* This should no longer change types on us. */
6214 if (TREE_CODE (rhs) == CONST_DECL)
6215 rhs = DECL_INITIAL (rhs);
6216 else if (TREE_READONLY_DECL_P (rhs))
6217 rhs = decl_constant_value (rhs);
6219 if (type == rhstype)
6221 overflow_warning (rhs);
6225 if (coder == VOID_TYPE)
6227 error ("void value not ignored as it ought to be");
6228 return error_mark_node;
6230 /* Arithmetic types all interconvert. */
6231 if ((codel == INTEGER_TYPE || codel == REAL_TYPE || codel == BOOLEAN_TYPE)
6232 && (coder == INTEGER_TYPE || coder == REAL_TYPE || coder == BOOLEAN_TYPE))
6234 /* But we should warn if assigning REAL_TYPE to INTEGER_TYPE. */
6235 if (coder == REAL_TYPE && codel == INTEGER_TYPE)
6238 cp_warning ("`%T' used for argument %P of `%D'",
6239 rhstype, parmnum, fndecl);
6241 cp_warning ("%s to `%T' from `%T'", errtype, type, rhstype);
6243 /* And we should warn if assigning a negative value to
6244 an unsigned variable. */
6245 else if (TREE_UNSIGNED (type) && codel != BOOLEAN_TYPE)
6247 if (TREE_CODE (rhs) == INTEGER_CST
6248 && TREE_NEGATED_INT (rhs))
6251 cp_warning ("negative value `%E' passed as argument %P of `%D'",
6252 rhs, parmnum, fndecl);
6254 cp_warning ("%s of negative value `%E' to `%T'",
6255 errtype, rhs, type);
6257 overflow_warning (rhs);
6258 if (TREE_CONSTANT (rhs))
6262 return convert_and_check (type, rhs);
6264 /* Conversions involving enums. */
6265 else if ((codel == ENUMERAL_TYPE
6266 && (INTEGRAL_CODE_P (coder) || coder == REAL_TYPE))
6267 || (coder == ENUMERAL_TYPE
6268 && (INTEGRAL_CODE_P (codel) || codel == REAL_TYPE)))
6270 return cp_convert (type, rhs, CONV_IMPLICIT, LOOKUP_NORMAL);
6272 /* Conversions among pointers */
6273 else if (codel == POINTER_TYPE
6274 && (coder == POINTER_TYPE
6275 || (coder == RECORD_TYPE
6276 && (IS_SIGNATURE_POINTER (rhstype)
6277 || IS_SIGNATURE_REFERENCE (rhstype)))))
6279 register tree ttl = TREE_TYPE (type);
6283 if (coder == RECORD_TYPE)
6285 rhs = build_optr_ref (rhs);
6286 rhstype = TREE_TYPE (rhs);
6288 ttr = TREE_TYPE (rhstype);
6290 /* If both pointers are of aggregate type, then we
6291 can give better error messages, and save some work
6293 if (TREE_CODE (ttl) == RECORD_TYPE && TREE_CODE (ttr) == RECORD_TYPE)
6297 if (TYPE_MAIN_VARIANT (ttl) == TYPE_MAIN_VARIANT (ttr)
6298 || type == class_star_type_node
6299 || rhstype == class_star_type_node)
6300 binfo = TYPE_BINFO (ttl);
6302 binfo = get_binfo (ttl, ttr, 1);
6304 if (binfo == error_mark_node)
6305 return error_mark_node;
6307 return error_not_base_type (ttl, ttr);
6309 if (! TYPE_READONLY (ttl) && TYPE_READONLY (ttr))
6312 cp_pedwarn ("passing `%T' as argument %P of `%D' discards const",
6313 rhstype, parmnum, fndecl);
6315 cp_pedwarn ("%s to `%T' from `%T' discards const",
6316 errtype, type, rhstype);
6318 if (! TYPE_VOLATILE (ttl) && TYPE_VOLATILE (ttr))
6321 cp_pedwarn ("passing `%T' as argument %P of `%D' discards volatile",
6322 rhstype, parmnum, fndecl);
6324 cp_pedwarn ("%s to `%T' from `%T' discards volatile",
6325 errtype, type, rhstype);
6329 /* Any non-function converts to a [const][volatile] void *
6330 and vice versa; otherwise, targets must be the same.
6331 Meanwhile, the lhs target must have all the qualifiers of the rhs. */
6332 else if (TYPE_MAIN_VARIANT (ttl) == void_type_node
6333 || TYPE_MAIN_VARIANT (ttr) == void_type_node
6334 || (ctt = comp_target_types (type, rhstype, 1))
6335 || (unsigned_type (TYPE_MAIN_VARIANT (ttl))
6336 == unsigned_type (TYPE_MAIN_VARIANT (ttr))))
6338 /* ARM $4.8, commentary on p39. */
6339 if (TYPE_MAIN_VARIANT (ttl) == void_type_node
6340 && TREE_CODE (ttr) == OFFSET_TYPE)
6342 cp_error ("no standard conversion from `%T' to `void *'", ttr);
6343 return error_mark_node;
6347 cp_pedwarn ("converting `%T' to `%T' is a contravariance violation",
6350 if (TYPE_MAIN_VARIANT (ttl) != void_type_node
6351 && TYPE_MAIN_VARIANT (ttr) == void_type_node
6352 && rhs != null_pointer_node)
6354 if (coder == RECORD_TYPE)
6355 cp_pedwarn ("implicit conversion of signature pointer to type `%T'",
6358 pedwarn ("ANSI C++ forbids implicit conversion from `void *' in %s",
6361 /* Const and volatile mean something different for function types,
6362 so the usual warnings are not appropriate. */
6363 else if ((TREE_CODE (ttr) != FUNCTION_TYPE && TREE_CODE (ttr) != METHOD_TYPE)
6364 || (TREE_CODE (ttl) != FUNCTION_TYPE && TREE_CODE (ttl) != METHOD_TYPE))
6366 if (TREE_CODE (ttl) == OFFSET_TYPE
6367 && binfo_member (TYPE_OFFSET_BASETYPE (ttr),
6368 CLASSTYPE_VBASECLASSES (TYPE_OFFSET_BASETYPE (ttl))))
6370 sorry ("%s between pointer to members converting across virtual baseclasses", errtype);
6371 return error_mark_node;
6373 else if (! TYPE_READONLY (ttl) && TYPE_READONLY (ttr))
6376 cp_pedwarn ("passing `%T' as argument %P of `%D' discards const",
6377 rhstype, parmnum, fndecl);
6379 cp_pedwarn ("%s to `%T' from `%T' discards const",
6380 errtype, type, rhstype);
6382 else if (! TYPE_VOLATILE (ttl) && TYPE_VOLATILE (ttr))
6385 cp_pedwarn ("passing `%T' as argument %P of `%D' discards volatile",
6386 rhstype, parmnum, fndecl);
6388 cp_pedwarn ("%s to `%T' from `%T' discards volatile",
6389 errtype, type, rhstype);
6391 else if (TREE_CODE (ttl) == TREE_CODE (ttr)
6392 && ! comp_target_types (type, rhstype, 1))
6395 cp_pedwarn ("passing `%T' as argument %P of `%D' changes signedness",
6396 rhstype, parmnum, fndecl);
6398 cp_pedwarn ("%s to `%T' from `%T' changes signedness",
6399 errtype, type, rhstype);
6403 else if (TREE_CODE (ttr) == OFFSET_TYPE
6404 && TREE_CODE (ttl) != OFFSET_TYPE)
6406 /* Normally, pointers to different type codes (other
6407 than void) are not compatible, but we perform
6408 some type instantiation if that resolves the
6409 ambiguity of (X Y::*) and (X *). */
6411 if (current_class_decl)
6413 if (TREE_CODE (rhs) == INTEGER_CST)
6415 rhs = build (PLUS_EXPR, build_pointer_type (TREE_TYPE (ttr)),
6416 current_class_decl, rhs);
6417 return convert_for_assignment (type, rhs,
6418 errtype, fndecl, parmnum);
6421 if (TREE_CODE (ttl) == METHOD_TYPE)
6422 error ("%s between pointer-to-method and pointer-to-member types",
6425 error ("%s between pointer and pointer-to-member types", errtype);
6426 return error_mark_node;
6430 int add_quals = 0, const_parity = 0, volatile_parity = 0;
6432 int unsigned_parity;
6435 /* This code is basically a duplicate of comp_ptr_ttypes_real. */
6436 for (; ; ttl = TREE_TYPE (ttl), ttr = TREE_TYPE (ttr))
6439 const_parity |= (TYPE_READONLY (ttl) < TYPE_READONLY (ttr));
6440 volatile_parity |= (TYPE_VOLATILE (ttl) < TYPE_VOLATILE (ttr));
6443 && (TYPE_READONLY (ttl) > TYPE_READONLY (ttr)
6444 || TYPE_VOLATILE (ttl) > TYPE_VOLATILE (ttr)))
6446 left_const &= TYPE_READONLY (ttl);
6448 if (TREE_CODE (ttl) != POINTER_TYPE
6449 || TREE_CODE (ttr) != POINTER_TYPE)
6452 unsigned_parity = TREE_UNSIGNED (ttl) - TREE_UNSIGNED (ttr);
6453 if (unsigned_parity)
6455 if (TREE_UNSIGNED (ttl))
6456 ttr = unsigned_type (ttr);
6458 ttl = unsigned_type (ttl);
6461 if (comp_target_types (ttl, ttr, nptrs) > 0)
6466 cp_pedwarn ("passing `%T' as argument %P of `%D' adds cv-quals without intervening `const'",
6467 rhstype, parmnum, fndecl);
6469 cp_pedwarn ("%s to `%T' from `%T' adds cv-quals without intervening `const'",
6470 errtype, type, rhstype);
6475 cp_pedwarn ("passing `%T' as argument %P of `%D' discards const",
6476 rhstype, parmnum, fndecl);
6478 cp_pedwarn ("%s to `%T' from `%T' discards const",
6479 errtype, type, rhstype);
6481 if (volatile_parity)
6484 cp_pedwarn ("passing `%T' as argument %P of `%D' discards volatile",
6485 rhstype, parmnum, fndecl);
6487 cp_pedwarn ("%s to `%T' from `%T' discards volatile",
6488 errtype, type, rhstype);
6490 if (unsigned_parity > 0)
6493 cp_pedwarn ("passing `%T' as argument %P of `%D' changes signed to unsigned",
6494 rhstype, parmnum, fndecl);
6496 cp_pedwarn ("%s to `%T' from `%T' changes signed to unsigned",
6497 errtype, type, rhstype);
6499 else if (unsigned_parity < 0)
6502 cp_pedwarn ("passing `%T' as argument %P of `%D' changes unsigned to signed",
6503 rhstype, parmnum, fndecl);
6505 cp_pedwarn ("%s to `%T' from `%T' changes unsigned to signed",
6506 errtype, type, rhstype);
6509 /* C++ is not so friendly about converting function and
6510 member function pointers as C. Emit warnings here. */
6511 if (TREE_CODE (ttl) == FUNCTION_TYPE
6512 || TREE_CODE (ttl) == METHOD_TYPE)
6513 if (! comptypes (ttl, ttr, 0))
6515 warning ("conflicting function types in %s:", errtype);
6516 cp_warning ("\t`%T' != `%T'", type, rhstype);
6519 else if (TREE_CODE (TREE_TYPE (rhs)) == METHOD_TYPE)
6521 /* When does this happen? */
6522 my_friendly_abort (119);
6523 /* Conversion of a pointer-to-member type to void *. */
6524 rhs = build_unary_op (ADDR_EXPR, rhs, 0);
6525 TREE_TYPE (rhs) = type;
6528 else if (TREE_CODE (TREE_TYPE (rhs)) == OFFSET_TYPE)
6530 /* When does this happen? */
6531 my_friendly_abort (120);
6532 /* Conversion of a pointer-to-member type to void *. */
6533 rhs = build_unary_op (ADDR_EXPR, rhs, 0);
6534 TREE_TYPE (rhs) = type;
6540 cp_error ("passing `%T' as argument %P of `%D'",
6541 rhstype, parmnum, fndecl);
6543 cp_error ("%s to `%T' from `%T'", errtype, type, rhstype);
6544 return error_mark_node;
6547 return convert (type, rhs);
6549 else if (codel == POINTER_TYPE && coder == INTEGER_TYPE)
6551 /* An explicit constant 0 can convert to a pointer,
6552 but not a 0 that results from casting or folding. */
6553 if (! (TREE_CODE (rhs) == INTEGER_CST && integer_zerop (rhs)))
6556 cp_pedwarn ("passing `%T' to argument %P of `%D' lacks a cast",
6557 rhstype, parmnum, fndecl);
6559 cp_pedwarn ("%s to `%T' from `%T' lacks a cast",
6560 errtype, type, rhstype);
6561 return convert (type, rhs);
6563 return null_pointer_node;
6565 else if (codel == INTEGER_TYPE
6566 && (coder == POINTER_TYPE
6567 || (coder == RECORD_TYPE
6568 && (IS_SIGNATURE_POINTER (rhstype)
6569 || TYPE_PTRMEMFUNC_FLAG (rhstype)
6570 || IS_SIGNATURE_REFERENCE (rhstype)))))
6573 cp_pedwarn ("passing `%T' to argument %P of `%D' lacks a cast",
6574 rhstype, parmnum, fndecl);
6576 cp_pedwarn ("%s to `%T' from `%T' lacks a cast",
6577 errtype, type, rhstype);
6578 return convert (type, rhs);
6580 else if (codel == BOOLEAN_TYPE
6581 && (coder == POINTER_TYPE
6582 || (coder == RECORD_TYPE
6583 && (IS_SIGNATURE_POINTER (rhstype)
6584 || TYPE_PTRMEMFUNC_FLAG (rhstype)
6585 || IS_SIGNATURE_REFERENCE (rhstype)))))
6586 return convert (type, rhs);
6589 else if (((coder == POINTER_TYPE
6590 && TREE_CODE (TREE_TYPE (rhstype)) == METHOD_TYPE)
6591 || integer_zerop (rhs)
6592 || TYPE_PTRMEMFUNC_P (rhstype))
6593 && TYPE_PTRMEMFUNC_P (type))
6595 tree ttl = TYPE_PTRMEMFUNC_FN_TYPE (type);
6596 tree ttr = (TREE_CODE (rhstype) == POINTER_TYPE ? rhstype
6597 : TYPE_PTRMEMFUNC_FN_TYPE (type));
6598 int ctt = comp_target_types (ttl, ttr, 1);
6601 cp_pedwarn ("converting `%T' to `%T' is a contravariance violation",
6604 cp_error ("%s to `%T' from `%T'", errtype, ttl, ttr);
6606 /* compatible pointer to member functions. */
6607 return build_ptrmemfunc (ttl, rhs, 0);
6609 else if (codel == ERROR_MARK || coder == ERROR_MARK)
6610 return error_mark_node;
6612 /* This should no longer happen. References are initialized via
6613 `convert_for_initialization'. They should otherwise be
6614 bashed before coming here. */
6615 else if (codel == REFERENCE_TYPE)
6616 my_friendly_abort (317);
6617 else if (TYPE_MAIN_VARIANT (type) == TYPE_MAIN_VARIANT (TREE_TYPE (rhs)))
6619 tree nrhs = build1 (NOP_EXPR, type, rhs);
6620 TREE_CONSTANT (nrhs) = TREE_CONSTANT (rhs);
6623 else if (TYPE_HAS_CONSTRUCTOR (type) || IS_AGGR_TYPE (TREE_TYPE (rhs)))
6624 return convert (type, rhs);
6626 cp_error ("%s to `%T' from `%T'", errtype, type, rhstype);
6627 return error_mark_node;
6630 /* Convert RHS to be of type TYPE. If EXP is non-zero,
6631 it is the target of the initialization.
6632 ERRTYPE is a string to use in error messages.
6634 Two major differences between the behavior of
6635 `convert_for_assignment' and `convert_for_initialization'
6636 are that references are bashed in the former, while
6637 copied in the latter, and aggregates are assigned in
6638 the former (operator=) while initialized in the
6641 If using constructor make sure no conversion operator exists, if one does
6642 exist, an ambiguity exists.
6644 If flags doesn't include LOOKUP_COMPLAIN, don't complain about anything. */
6646 convert_for_initialization (exp, type, rhs, flags, errtype, fndecl, parmnum)
6647 tree exp, type, rhs;
6653 register enum tree_code codel = TREE_CODE (type);
6654 register tree rhstype;
6655 register enum tree_code coder;
6657 /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
6658 Strip such NOP_EXPRs, since RHS is used in non-lvalue context. */
6659 if (TREE_CODE (rhs) == NOP_EXPR
6660 && TREE_TYPE (rhs) == TREE_TYPE (TREE_OPERAND (rhs, 0))
6661 && codel != REFERENCE_TYPE)
6662 rhs = TREE_OPERAND (rhs, 0);
6664 if (rhs == error_mark_node
6665 || (TREE_CODE (rhs) == TREE_LIST && TREE_VALUE (rhs) == error_mark_node))
6666 return error_mark_node;
6668 if (TREE_CODE (TREE_TYPE (rhs)) == OFFSET_TYPE)
6670 rhs = resolve_offset_ref (rhs);
6671 if (rhs == error_mark_node)
6672 return error_mark_node;
6675 if (TREE_CODE (TREE_TYPE (rhs)) == REFERENCE_TYPE)
6676 rhs = convert_from_reference (rhs);
6678 if ((TREE_CODE (TREE_TYPE (rhs)) == ARRAY_TYPE
6679 && TREE_CODE (type) != ARRAY_TYPE
6680 && (TREE_CODE (type) != REFERENCE_TYPE
6681 || TREE_CODE (TREE_TYPE (type)) != ARRAY_TYPE))
6682 || TREE_CODE (TREE_TYPE (rhs)) == FUNCTION_TYPE
6683 || TREE_CODE (TREE_TYPE (rhs)) == METHOD_TYPE)
6684 rhs = default_conversion (rhs);
6686 rhstype = TREE_TYPE (rhs);
6687 coder = TREE_CODE (rhstype);
6689 if (coder == UNKNOWN_TYPE)
6691 rhs = instantiate_type (type, rhs, 1);
6692 rhstype = TREE_TYPE (rhs);
6693 coder = TREE_CODE (rhstype);
6696 if (coder == ERROR_MARK)
6697 return error_mark_node;
6699 /* We accept references to incomplete types, so we can
6700 return here before checking if RHS is of complete type. */
6702 if (codel == REFERENCE_TYPE)
6704 /* This should eventually happen in convert_arguments. */
6705 extern int warningcount, errorcount;
6709 savew = warningcount, savee = errorcount;
6710 rhs = convert_to_reference (type, rhs, CONV_IMPLICIT, flags,
6711 exp ? exp : error_mark_node);
6714 if (warningcount > savew)
6715 cp_warning_at ("in passing argument %P of `%+D'", parmnum, fndecl);
6716 else if (errorcount > savee)
6717 cp_error_at ("in passing argument %P of `%+D'", parmnum, fndecl);
6722 rhs = require_complete_type (rhs);
6723 if (rhs == error_mark_node)
6724 return error_mark_node;
6726 if (exp != 0) exp = require_complete_type (exp);
6727 if (exp == error_mark_node)
6728 return error_mark_node;
6730 if (TREE_CODE (rhstype) == REFERENCE_TYPE)
6731 rhstype = TREE_TYPE (rhstype);
6733 if (TYPE_LANG_SPECIFIC (type)
6734 && (IS_SIGNATURE_POINTER (type) || IS_SIGNATURE_REFERENCE (type)))
6735 return build_signature_pointer_constructor (type, rhs);
6737 if (IS_AGGR_TYPE (type)
6738 && (TYPE_NEEDS_CONSTRUCTING (type) || TREE_HAS_CONSTRUCTOR (rhs)))
6740 if (TYPE_MAIN_VARIANT (type) == TYPE_MAIN_VARIANT (rhstype))
6742 /* This is sufficient to perform initialization. No need,
6743 apparently, to go through X(X&) to do first-cut
6744 initialization. Return through a TARGET_EXPR so that we get
6745 cleanups if it is used. */
6746 if (TREE_CODE (rhs) == CALL_EXPR)
6748 rhs = build_cplus_new (type, rhs);
6751 /* Handle the case of default parameter initialization and
6752 initialization of static variables. */
6753 else if (TREE_CODE (rhs) == TARGET_EXPR)
6755 else if (TREE_CODE (rhs) == INDIRECT_REF && TREE_HAS_CONSTRUCTOR (rhs))
6757 my_friendly_assert (TREE_CODE (TREE_OPERAND (rhs, 0)) == CALL_EXPR, 318);
6760 my_friendly_assert (TREE_VALUE (TREE_OPERAND (TREE_OPERAND (rhs, 0), 1)) == NULL_TREE, 316);
6761 TREE_VALUE (TREE_OPERAND (TREE_OPERAND (rhs, 0), 1))
6762 = build_unary_op (ADDR_EXPR, exp, 0);
6765 rhs = build_cplus_new (type, TREE_OPERAND (rhs, 0));
6768 else if (TYPE_HAS_TRIVIAL_INIT_REF (type))
6771 if (TYPE_MAIN_VARIANT (type) == TYPE_MAIN_VARIANT (rhstype)
6772 || (IS_AGGR_TYPE (rhstype) && UNIQUELY_DERIVED_FROM_P (type, rhstype)))
6774 if (TYPE_HAS_INIT_REF (type))
6776 tree init = build_method_call (exp, ctor_identifier,
6777 build_tree_list (NULL_TREE, rhs),
6778 TYPE_BINFO (type), LOOKUP_NORMAL);
6780 if (init == error_mark_node)
6781 return error_mark_node;
6785 exp = build_cplus_new (type, init);
6789 return build (COMPOUND_EXPR, type, init, exp);
6792 /* ??? The following warnings are turned off because
6793 this is another place where the default X(X&) constructor
6795 if (TYPE_HAS_ASSIGNMENT (type))
6796 cp_warning ("bitwise copy: `%T' defines operator=", type);
6798 if (TREE_CODE (TREE_TYPE (rhs)) == REFERENCE_TYPE)
6799 rhs = convert_from_reference (rhs);
6800 if (type != rhstype)
6802 tree nrhs = build1 (NOP_EXPR, type, rhs);
6803 TREE_CONSTANT (nrhs) = TREE_CONSTANT (rhs);
6809 return cp_convert (type, rhs, CONV_OLD_CONVERT,
6810 flags | LOOKUP_NO_CONVERSION);
6813 if (type == TREE_TYPE (rhs))
6815 if (TREE_READONLY_DECL_P (rhs))
6816 rhs = decl_constant_value (rhs);
6820 return convert_for_assignment (type, rhs, errtype, fndecl, parmnum);
6823 /* Expand an ASM statement with operands, handling output operands
6824 that are not variables or INDIRECT_REFS by transforming such
6825 cases into cases that expand_asm_operands can handle.
6827 Arguments are same as for expand_asm_operands.
6829 We don't do default conversions on all inputs, because it can screw
6830 up operands that are expected to be in memory. */
6833 c_expand_asm_operands (string, outputs, inputs, clobbers, vol, filename, line)
6834 tree string, outputs, inputs, clobbers;
6839 int noutputs = list_length (outputs);
6841 /* o[I] is the place that output number I should be written. */
6842 register tree *o = (tree *) alloca (noutputs * sizeof (tree));
6845 /* Record the contents of OUTPUTS before it is modified. */
6846 for (i = 0, tail = outputs; tail; tail = TREE_CHAIN (tail), i++)
6847 o[i] = TREE_VALUE (tail);
6849 /* Generate the ASM_OPERANDS insn;
6850 store into the TREE_VALUEs of OUTPUTS some trees for
6851 where the values were actually stored. */
6852 expand_asm_operands (string, outputs, inputs, clobbers, vol, filename, line);
6854 /* Copy all the intermediate outputs into the specified outputs. */
6855 for (i = 0, tail = outputs; tail; tail = TREE_CHAIN (tail), i++)
6857 if (o[i] != TREE_VALUE (tail))
6859 expand_expr (build_modify_expr (o[i], NOP_EXPR, TREE_VALUE (tail)),
6860 const0_rtx, VOIDmode, 0);
6863 /* Detect modification of read-only values.
6864 (Otherwise done by build_modify_expr.) */
6867 tree type = TREE_TYPE (o[i]);
6868 if (TYPE_READONLY (type)
6869 || ((TREE_CODE (type) == RECORD_TYPE
6870 || TREE_CODE (type) == UNION_TYPE)
6871 && C_TYPE_FIELDS_READONLY (type)))
6872 readonly_error (o[i], "modification by `asm'", 1);
6876 /* Those MODIFY_EXPRs could do autoincrements. */
6880 /* Expand a C `return' statement.
6881 RETVAL is the expression for what to return,
6882 or a null pointer for `return;' with no value.
6884 C++: upon seeing a `return', we must call destructors on all
6885 variables in scope which had constructors called on them.
6886 This means that if in a destructor, the base class destructors
6887 must be called before returning.
6889 The RETURN statement in C++ has initialization semantics. */
6892 c_expand_return (retval)
6895 extern struct nesting *cond_stack, *loop_stack, *case_stack;
6896 extern tree dtor_label, ctor_label;
6897 tree result = DECL_RESULT (current_function_decl);
6898 tree valtype = TREE_TYPE (result);
6899 int returns_value = 1;
6901 if (TREE_THIS_VOLATILE (current_function_decl))
6902 warning ("function declared `noreturn' has a `return' statement");
6904 if (retval == error_mark_node)
6906 current_function_returns_null = 1;
6910 if (current_template_parms)
6912 add_tree (build_min_nt (RETURN_STMT, retval));
6916 if (retval == NULL_TREE)
6918 /* A non-named return value does not count. */
6920 /* Can't just return from a destructor. */
6923 expand_goto (dtor_label);
6927 if (DECL_CONSTRUCTOR_P (current_function_decl))
6928 retval = current_class_decl;
6929 else if (DECL_NAME (result) != NULL_TREE
6930 && TREE_CODE (valtype) != VOID_TYPE)
6934 current_function_returns_null = 1;
6936 if (valtype != NULL_TREE && TREE_CODE (valtype) != VOID_TYPE)
6938 if (DECL_NAME (DECL_RESULT (current_function_decl)) == NULL_TREE)
6940 pedwarn ("`return' with no value, in function returning non-void");
6941 /* Clear this, so finish_function won't say that we
6942 reach the end of a non-void function (which we don't,
6943 we gave a return!). */
6944 current_function_returns_null = 0;
6948 expand_null_return ();
6952 else if (DECL_CONSTRUCTOR_P (current_function_decl)
6953 && retval != current_class_decl)
6955 error ("return from a constructor: use `this = ...' instead");
6956 retval = current_class_decl;
6959 if (valtype == NULL_TREE || TREE_CODE (valtype) == VOID_TYPE)
6961 current_function_returns_null = 1;
6962 /* We do this here so we'll avoid a warning about how the function
6963 "may or may not return a value" in finish_function. */
6967 pedwarn ("`return' with a value, in function returning void");
6968 expand_return (retval);
6970 /* Add some useful error checking for C++. */
6971 else if (TREE_CODE (valtype) == REFERENCE_TYPE)
6973 tree whats_returned;
6974 tree tmp_result = result;
6976 /* Don't initialize directly into a non-BLKmode retval, since that
6977 could lose when being inlined by another caller. (GCC can't
6978 read the function return register in an inline function when
6979 the return value is being ignored). */
6980 if (result && TYPE_MODE (TREE_TYPE (tmp_result)) != BLKmode)
6983 /* convert to reference now, so we can give error if we
6984 return an reference to a non-lvalue. */
6985 retval = convert_for_initialization (tmp_result, valtype, retval,
6986 LOOKUP_NORMAL, "return",
6989 /* Sort through common things to see what it is
6990 we are returning. */
6991 whats_returned = retval;
6992 if (TREE_CODE (whats_returned) == COMPOUND_EXPR)
6994 whats_returned = TREE_OPERAND (whats_returned, 1);
6995 if (TREE_CODE (whats_returned) == ADDR_EXPR)
6996 whats_returned = TREE_OPERAND (whats_returned, 0);
6998 if (TREE_CODE (whats_returned) == ADDR_EXPR)
7000 whats_returned = TREE_OPERAND (whats_returned, 0);
7001 while (TREE_CODE (whats_returned) == NEW_EXPR
7002 || TREE_CODE (whats_returned) == TARGET_EXPR)
7004 /* Get the target. */
7005 whats_returned = TREE_OPERAND (whats_returned, 0);
7006 warning ("returning reference to temporary");
7010 if (TREE_CODE (whats_returned) == VAR_DECL && DECL_NAME (whats_returned))
7012 if (TEMP_NAME_P (DECL_NAME (whats_returned)))
7013 warning ("reference to non-lvalue returned");
7014 else if (! TREE_STATIC (whats_returned)
7015 && IDENTIFIER_LOCAL_VALUE (DECL_NAME (whats_returned))
7016 && !TREE_PUBLIC (whats_returned))
7017 cp_warning_at ("reference to local variable `%D' returned", whats_returned);
7020 else if (TREE_CODE (retval) == ADDR_EXPR)
7022 tree whats_returned = TREE_OPERAND (retval, 0);
7024 if (TREE_CODE (whats_returned) == VAR_DECL
7025 && DECL_NAME (whats_returned)
7026 && IDENTIFIER_LOCAL_VALUE (DECL_NAME (whats_returned))
7027 && !TREE_STATIC (whats_returned)
7028 && !TREE_PUBLIC (whats_returned))
7029 cp_warning_at ("address of local variable `%D' returned", whats_returned);
7031 else if (TREE_CODE (retval) == VAR_DECL)
7033 if (TREE_CODE (TREE_TYPE (retval)) == ARRAY_TYPE
7034 && DECL_NAME (retval)
7035 && IDENTIFIER_LOCAL_VALUE (DECL_NAME (retval))
7036 && !TREE_STATIC (retval)
7037 && !TREE_PUBLIC (retval))
7038 cp_warning_at ("address of local array `%D' returned", retval);
7041 /* Now deal with possible C++ hair:
7042 (1) Compute the return value.
7043 (2) If there are aggregate values with destructors which
7044 must be cleaned up, clean them (taking care
7045 not to clobber the return value).
7046 (3) If an X(X&) constructor is defined, the return
7047 value must be returned via that. */
7049 /* If we're returning in a register, we can't initialize the
7050 return value from a TARGET_EXPR. */
7051 if (TREE_CODE (retval) == TARGET_EXPR
7052 && TYPE_MAIN_VARIANT (TREE_TYPE (retval)) == TYPE_MAIN_VARIANT (valtype)
7053 && ! current_function_returns_struct)
7054 retval = expand_target_expr (retval);
7056 if (retval == result
7057 /* Watch out for constructors, which "return" aggregates
7058 via initialization, but which otherwise "return" a pointer. */
7059 || DECL_CONSTRUCTOR_P (current_function_decl))
7061 /* This is just an error--it's already been reported. */
7062 if (TYPE_SIZE (valtype) == NULL_TREE)
7065 if (TYPE_MODE (valtype) != BLKmode
7066 && any_pending_cleanups (1))
7067 retval = get_temp_regvar (valtype, retval);
7069 else if (IS_AGGR_TYPE (valtype) && current_function_returns_struct)
7071 expand_aggr_init (result, retval, 0, LOOKUP_ONLYCONVERTING);
7072 expand_cleanups_to (NULL_TREE);
7073 DECL_INITIAL (result) = NULL_TREE;
7078 if (TYPE_MODE (valtype) == VOIDmode)
7080 if (TYPE_MODE (TREE_TYPE (result)) != VOIDmode
7081 && warn_return_type)
7082 warning ("return of void value in function returning non-void");
7083 expand_expr_stmt (retval);
7087 else if (TYPE_MODE (valtype) != BLKmode
7088 && any_pending_cleanups (1))
7090 retval = get_temp_regvar (valtype, retval);
7091 expand_cleanups_to (NULL_TREE);
7096 retval = convert_for_initialization (result, valtype, retval,
7098 "return", NULL_TREE, 0);
7099 DECL_INITIAL (result) = NULL_TREE;
7101 if (retval == error_mark_node)
7107 if (retval != NULL_TREE
7108 && TREE_CODE_CLASS (TREE_CODE (retval)) == 'd'
7109 && cond_stack == 0 && loop_stack == 0 && case_stack == 0)
7110 current_function_return_value = retval;
7114 /* Everything's great--RETVAL is in RESULT. */
7115 if (original_result_rtx)
7117 store_expr (result, original_result_rtx, 0);
7118 expand_cleanups_to (NULL_TREE);
7119 use_variable (DECL_RTL (result));
7120 if (ctor_label && TREE_CODE (ctor_label) != ERROR_MARK)
7121 expand_goto (ctor_label);
7123 expand_null_return ();
7125 else if (retval && retval != result)
7127 /* Clear this out so the later call to decl_function_context
7128 won't end up bombing on us. */
7129 if (DECL_CONTEXT (result) == error_mark_node)
7130 DECL_CONTEXT (result) = NULL_TREE;
7131 /* Here is where we finally get RETVAL into RESULT.
7132 `expand_return' does the magic of protecting
7133 RESULT from cleanups. */
7134 retval = fold (build1 (CLEANUP_POINT_EXPR, TREE_TYPE (result),
7136 /* This part _must_ come second, because expand_return looks for
7137 the INIT_EXPR as the toplevel node only. :-( */
7138 retval = build (INIT_EXPR, TREE_TYPE (result), result, retval);
7139 TREE_SIDE_EFFECTS (retval) = 1;
7140 expand_return (retval);
7143 expand_return (result);
7147 /* We may still need to put RETVAL into RESULT. */
7148 result = DECL_RESULT (current_function_decl);
7149 if (original_result_rtx)
7151 /* Here we have a named return value that went
7152 into memory. We can compute RETVAL into that. */
7154 expand_assignment (result, retval, 0, 0);
7156 store_expr (result, original_result_rtx, 0);
7157 result = make_tree (TREE_TYPE (result), original_result_rtx);
7159 else if (ctor_label && TREE_CODE (ctor_label) != ERROR_MARK)
7161 /* Here RETVAL is CURRENT_CLASS_DECL, so there's nothing to do. */
7162 expand_goto (ctor_label);
7166 /* Here is where we finally get RETVAL into RESULT.
7167 `expand_return' does the magic of protecting
7168 RESULT from cleanups. */
7169 result = build (INIT_EXPR, TREE_TYPE (result), result, retval);
7170 TREE_SIDE_EFFECTS (result) = 1;
7171 expand_return (result);
7173 else if (TYPE_MODE (TREE_TYPE (result)) != VOIDmode)
7174 expand_return (result);
7177 current_function_returns_value = returns_value;
7179 /* One way to clear out cleanups that EXPR might
7180 generate. Note that this code will really be
7181 dead code, but that is ok--cleanups that were
7182 needed were handled by the magic of `return'. */
7183 expand_cleanups_to (NULL_TREE);
7186 /* Start a C switch statement, testing expression EXP.
7187 Return EXP if it is valid, an error node otherwise. */
7190 c_expand_start_case (exp)
7194 register enum tree_code code;
7196 /* Convert from references, etc. */
7197 exp = default_conversion (exp);
7198 type = TREE_TYPE (exp);
7199 code = TREE_CODE (type);
7201 if (IS_AGGR_TYPE_CODE (code))
7202 exp = build_type_conversion (CONVERT_EXPR, integer_type_node, exp, 1);
7204 if (exp == NULL_TREE)
7206 error ("switch quantity not an integer");
7207 exp = error_mark_node;
7209 type = TREE_TYPE (exp);
7210 code = TREE_CODE (type);
7212 if (code != INTEGER_TYPE && code != ENUMERAL_TYPE && code != ERROR_MARK)
7214 error ("switch quantity not an integer");
7215 exp = error_mark_node;
7221 exp = default_conversion (exp);
7222 type = TREE_TYPE (exp);
7223 index = get_unwidened (exp, 0);
7224 /* We can't strip a conversion from a signed type to an unsigned,
7225 because if we did, int_fits_type_p would do the wrong thing
7226 when checking case values for being in range,
7227 and it's too hard to do the right thing. */
7228 if (TREE_UNSIGNED (TREE_TYPE (exp))
7229 == TREE_UNSIGNED (TREE_TYPE (index)))
7234 (1, fold (build1 (CLEANUP_POINT_EXPR, TREE_TYPE (exp), exp)),
7235 type, "switch statement");
7240 /* CONSTP remembers whether or not all the intervening pointers in the `to'
7241 type have been const. */
7243 comp_ptr_ttypes_real (to, from, constp)
7247 for (; ; to = TREE_TYPE (to), from = TREE_TYPE (from))
7249 if (TREE_CODE (to) != TREE_CODE (from))
7252 /* Const and volatile mean something different for function types,
7253 so the usual checks are not appropriate. */
7254 if (TREE_CODE (to) != FUNCTION_TYPE && TREE_CODE (to) != METHOD_TYPE)
7256 if (TYPE_READONLY (from) > TYPE_READONLY (to)
7257 || TYPE_VOLATILE (from) > TYPE_VOLATILE (to))
7261 && (TYPE_READONLY (to) > TYPE_READONLY (from)
7262 || TYPE_VOLATILE (to) > TYPE_READONLY (from)))
7264 constp &= TYPE_READONLY (to);
7267 if (TREE_CODE (to) != POINTER_TYPE)
7268 return comptypes (TYPE_MAIN_VARIANT (to), TYPE_MAIN_VARIANT (from), 1);
7272 /* When comparing, say, char ** to char const **, this function takes the
7273 'char *' and 'char const *'. Do not pass non-pointer types to this
7276 comp_ptr_ttypes (to, from)
7279 return comp_ptr_ttypes_real (to, from, 1);