1 /* Build expressions with type checking for C++ compiler.
2 Copyright (C) 1987, 88, 89, 92-98, 1999 Free Software Foundation, Inc.
3 Hacked by Michael Tiemann (tiemann@cygnus.com)
5 This file is part of GNU CC.
7 GNU CC is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2, or (at your option)
12 GNU CC is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with GNU CC; see the file COPYING. If not, write to
19 the Free Software Foundation, 59 Temple Place - Suite 330,
20 Boston, MA 02111-1307, USA. */
23 /* This file is part of the C++ front end.
24 It contains routines to build C++ expressions given their operands,
25 including computing the types of the result, C and C++ specific error
26 checks, and some optimization.
28 There are also routines to build RETURN_STMT nodes and CASE_STMT nodes,
29 and to process initializations in declarations (since they work
30 like a strange sort of assignment). */
42 extern void compiler_error ();
44 static tree convert_for_assignment
PROTO((tree
, tree
, char*, tree
,
46 static tree pointer_int_sum
PROTO((enum tree_code
, tree
, tree
));
47 static tree rationalize_conditional_expr
PROTO((enum tree_code
, tree
));
48 static int comp_target_parms
PROTO((tree
, tree
, int));
49 static int comp_ptr_ttypes_real
PROTO((tree
, tree
, int));
50 static int comp_ptr_ttypes_const
PROTO((tree
, tree
));
51 static int comp_ptr_ttypes_reinterpret
PROTO((tree
, tree
));
52 static int comp_array_types
PROTO((int (*) (tree
, tree
, int), tree
,
54 static tree build_ptrmemfunc1
PROTO((tree
, tree
, tree
, tree
, tree
));
55 static tree common_base_type
PROTO((tree
, tree
));
57 static tree convert_sequence
PROTO((tree
, tree
));
59 static tree lookup_anon_field
PROTO((tree
, tree
));
60 static tree pointer_diff
PROTO((tree
, tree
, tree
));
61 static tree qualify_type
PROTO((tree
, tree
));
62 static tree get_delta_difference
PROTO((tree
, tree
, int));
64 /* Return the target type of TYPE, which meas return T for:
65 T*, T&, T[], T (...), and otherwise, just T. */
71 if (TREE_CODE (type
) == REFERENCE_TYPE
)
72 type
= TREE_TYPE (type
);
73 while (TREE_CODE (type
) == POINTER_TYPE
74 || TREE_CODE (type
) == ARRAY_TYPE
75 || TREE_CODE (type
) == FUNCTION_TYPE
76 || TREE_CODE (type
) == METHOD_TYPE
77 || TREE_CODE (type
) == OFFSET_TYPE
)
78 type
= TREE_TYPE (type
);
82 /* Do `exp = require_complete_type (exp);' to make sure exp
83 does not have an incomplete type. (That includes void types.)
84 Returns the error_mark_node if the VALUE does not have
85 complete type when this function returns. */
88 require_complete_type (value
)
93 if (processing_template_decl
)
96 if (TREE_CODE (value
) == OVERLOAD
)
97 type
= unknown_type_node
;
99 type
= TREE_TYPE (value
);
101 /* First, detect a valid value with a complete type. */
102 if (TYPE_SIZE (type
) != 0
103 && type
!= void_type_node
104 && ! (TYPE_LANG_SPECIFIC (type
)
105 && (IS_SIGNATURE_POINTER (type
) || IS_SIGNATURE_REFERENCE (type
))
106 && TYPE_SIZE (SIGNATURE_TYPE (type
)) == 0))
109 /* If we see X::Y, we build an OFFSET_TYPE which has
110 not been laid out. Try to avoid an error by interpreting
111 it as this->X::Y, if reasonable. */
112 if (TREE_CODE (value
) == OFFSET_REF
113 && current_class_ref
!= 0
114 && TREE_OPERAND (value
, 0) == current_class_ref
)
116 tree base
, member
= TREE_OPERAND (value
, 1);
117 tree basetype
= TYPE_OFFSET_BASETYPE (type
);
118 my_friendly_assert (TREE_CODE (member
) == FIELD_DECL
, 305);
119 base
= convert_pointer_to (basetype
, current_class_ptr
);
120 value
= build (COMPONENT_REF
, TREE_TYPE (member
),
121 build_indirect_ref (base
, NULL_PTR
), member
);
122 return require_complete_type (value
);
125 if (complete_type_or_else (type
))
128 return error_mark_node
;
131 /* Try to complete TYPE, if it is incomplete. For example, if TYPE is
132 a template instantiation, do the instantiation. Returns TYPE,
133 whether or not it could be completed, unless something goes
134 horribly wrong, in which case the error_mark_node is returned. */
140 if (type
== NULL_TREE
)
141 /* Rather than crash, we return something sure to cause an error
143 return error_mark_node
;
145 if (type
== error_mark_node
|| TYPE_SIZE (type
) != NULL_TREE
)
147 else if (TREE_CODE (type
) == ARRAY_TYPE
&& TYPE_DOMAIN (type
))
149 tree t
= complete_type (TREE_TYPE (type
));
150 if (TYPE_SIZE (t
) != NULL_TREE
&& ! processing_template_decl
)
152 TYPE_NEEDS_CONSTRUCTING (type
)
153 = TYPE_NEEDS_CONSTRUCTING (TYPE_MAIN_VARIANT (t
));
154 TYPE_NEEDS_DESTRUCTOR (type
)
155 = TYPE_NEEDS_DESTRUCTOR (TYPE_MAIN_VARIANT (t
));
157 else if (CLASS_TYPE_P (type
) && CLASSTYPE_TEMPLATE_INSTANTIATION (type
))
158 instantiate_class_template (TYPE_MAIN_VARIANT (type
));
163 /* Like complete_type, but issue an error if the TYPE cannot be
164 completed. Returns NULL_TREE if the type cannot be made
168 complete_type_or_else (type
)
171 type
= complete_type (type
);
172 if (type
== error_mark_node
)
173 /* We already issued an error. */
175 else if (!TYPE_SIZE (type
))
177 incomplete_type_error (NULL_TREE
, type
);
184 /* Return truthvalue of whether type of EXP is instantiated. */
190 return (TREE_CODE (exp
) == OVERLOAD
191 || TREE_CODE (exp
) == TREE_LIST
192 || TREE_TYPE (exp
) == unknown_type_node
193 || (TREE_CODE (TREE_TYPE (exp
)) == OFFSET_TYPE
194 && TREE_TYPE (TREE_TYPE (exp
)) == unknown_type_node
));
197 /* Return truthvalue of whether T is function (or pfn) type. */
203 return (TREE_CODE (t
) == FUNCTION_TYPE
|| TREE_CODE (t
) == METHOD_TYPE
204 || (TREE_CODE (t
) == POINTER_TYPE
205 && (TREE_CODE (TREE_TYPE (t
)) == FUNCTION_TYPE
206 || TREE_CODE (TREE_TYPE (t
)) == METHOD_TYPE
)));
209 /* Return a variant of TYPE which has all the type qualifiers of LIKE
210 as well as those of TYPE. */
213 qualify_type (type
, like
)
216 /* @@ Must do member pointers here. */
217 return cp_build_qualified_type (type
, (CP_TYPE_QUALS (type
)
218 | CP_TYPE_QUALS (like
)));
221 /* Return the common type of two parameter lists.
222 We assume that comptypes has already been done and returned 1;
223 if that isn't so, this may crash.
225 As an optimization, free the space we allocate if the parameter
226 lists are already common. */
232 tree oldargs
= p1
, newargs
, n
;
235 char *first_obj
= (char *) oballoc (0);
237 len
= list_length (p1
);
238 newargs
= tree_last (p1
);
240 if (newargs
== void_list_node
)
249 newargs
= tree_cons (NULL_TREE
, NULL_TREE
, newargs
);
254 p1
= TREE_CHAIN (p1
), p2
= TREE_CHAIN (p2
), n
= TREE_CHAIN (n
), i
++)
256 if (TREE_PURPOSE (p1
) && !TREE_PURPOSE (p2
))
258 TREE_PURPOSE (n
) = TREE_PURPOSE (p1
);
261 else if (! TREE_PURPOSE (p1
))
263 if (TREE_PURPOSE (p2
))
265 TREE_PURPOSE (n
) = TREE_PURPOSE (p2
);
271 if (1 != simple_cst_equal (TREE_PURPOSE (p1
), TREE_PURPOSE (p2
)))
273 TREE_PURPOSE (n
) = TREE_PURPOSE (p2
);
275 if (TREE_VALUE (p1
) != TREE_VALUE (p2
))
278 TREE_VALUE (n
) = common_type (TREE_VALUE (p1
), TREE_VALUE (p2
));
281 TREE_VALUE (n
) = TREE_VALUE (p1
);
292 /* Given a type, perhaps copied for a typedef,
293 find the "original" version of it. */
298 while (TYPE_NAME (t
) != NULL_TREE
)
300 tree x
= TYPE_NAME (t
);
301 if (TREE_CODE (x
) != TYPE_DECL
)
303 x
= DECL_ORIGINAL_TYPE (x
);
311 /* Return the common type of two types.
312 We assume that comptypes has already been done and returned 1;
313 if that isn't so, this may crash.
315 This is the type for the result of most arithmetic operations
316 if the operands have the given two types.
318 We do not deal with enumeral types here because they have already been
319 converted to integer types. */
325 register enum tree_code code1
;
326 register enum tree_code code2
;
329 /* Save time if the two types are the same. */
332 t1
= original_type (t1
);
333 t2
= original_type (t2
);
337 /* If one type is nonsense, use the other. */
338 if (t1
== error_mark_node
)
340 if (t2
== error_mark_node
)
343 /* Merge the attributes. */
344 attributes
= merge_machine_type_attributes (t1
, t2
);
346 { register tree a1
, a2
;
347 a1
= TYPE_ATTRIBUTES (t1
);
348 a2
= TYPE_ATTRIBUTES (t2
);
350 /* Either one unset? Take the set one. */
352 if (!(attributes
= a1
))
355 /* One that completely contains the other? Take it. */
357 else if (a2
&& !attribute_list_contained (a1
, a2
))
359 if (attribute_list_contained (a2
, a1
))
363 /* Pick the longest list, and hang on the other list. */
364 /* ??? For the moment we punt on the issue of attrs with args. */
366 if (list_length (a1
) < list_length (a2
))
367 attributes
= a2
, a2
= a1
;
369 for (; a2
; a2
= TREE_CHAIN (a2
))
370 if (lookup_attribute (IDENTIFIER_POINTER (TREE_PURPOSE (a2
)),
371 attributes
) == NULL_TREE
)
374 TREE_CHAIN (a1
) = attributes
;
381 /* Treat an enum type as the unsigned integer type of the same width. */
383 if (TREE_CODE (t1
) == ENUMERAL_TYPE
)
384 t1
= type_for_size (TYPE_PRECISION (t1
), 1);
385 if (TREE_CODE (t2
) == ENUMERAL_TYPE
)
386 t2
= type_for_size (TYPE_PRECISION (t2
), 1);
388 if (TYPE_PTRMEMFUNC_P (t1
))
389 t1
= TYPE_PTRMEMFUNC_FN_TYPE (t1
);
390 if (TYPE_PTRMEMFUNC_P (t2
))
391 t2
= TYPE_PTRMEMFUNC_FN_TYPE (t2
);
393 code1
= TREE_CODE (t1
);
394 code2
= TREE_CODE (t2
);
396 /* If one type is complex, form the common type of the non-complex
397 components, then make that complex. Use T1 or T2 if it is the
399 if (code1
== COMPLEX_TYPE
|| code2
== COMPLEX_TYPE
)
401 tree subtype1
= code1
== COMPLEX_TYPE
? TREE_TYPE (t1
) : t1
;
402 tree subtype2
= code2
== COMPLEX_TYPE
? TREE_TYPE (t2
) : t2
;
403 tree subtype
= common_type (subtype1
, subtype2
);
405 if (code1
== COMPLEX_TYPE
&& TREE_TYPE (t1
) == subtype
)
406 return build_type_attribute_variant (t1
, attributes
);
407 else if (code2
== COMPLEX_TYPE
&& TREE_TYPE (t2
) == subtype
)
408 return build_type_attribute_variant (t2
, attributes
);
410 return build_type_attribute_variant (build_complex_type (subtype
),
418 /* If only one is real, use it as the result. */
420 if (code1
== REAL_TYPE
&& code2
!= REAL_TYPE
)
421 return build_type_attribute_variant (t1
, attributes
);
423 if (code2
== REAL_TYPE
&& code1
!= REAL_TYPE
)
424 return build_type_attribute_variant (t2
, attributes
);
426 /* Both real or both integers; use the one with greater precision. */
428 if (TYPE_PRECISION (t1
) > TYPE_PRECISION (t2
))
429 return build_type_attribute_variant (t1
, attributes
);
430 else if (TYPE_PRECISION (t2
) > TYPE_PRECISION (t1
))
431 return build_type_attribute_variant (t2
, attributes
);
433 /* Same precision. Prefer longs to ints even when same size. */
435 if (TYPE_MAIN_VARIANT (t1
) == long_unsigned_type_node
436 || TYPE_MAIN_VARIANT (t2
) == long_unsigned_type_node
)
437 return build_type_attribute_variant (long_unsigned_type_node
,
440 if (TYPE_MAIN_VARIANT (t1
) == long_integer_type_node
441 || TYPE_MAIN_VARIANT (t2
) == long_integer_type_node
)
443 /* But preserve unsignedness from the other type,
444 since long cannot hold all the values of an unsigned int. */
445 if (TREE_UNSIGNED (t1
) || TREE_UNSIGNED (t2
))
446 t1
= long_unsigned_type_node
;
448 t1
= long_integer_type_node
;
449 return build_type_attribute_variant (t1
, attributes
);
452 if (TYPE_MAIN_VARIANT (t1
) == long_double_type_node
453 || TYPE_MAIN_VARIANT (t2
) == long_double_type_node
)
454 return build_type_attribute_variant (long_double_type_node
,
457 /* Otherwise prefer the unsigned one. */
459 if (TREE_UNSIGNED (t1
))
460 return build_type_attribute_variant (t1
, attributes
);
462 return build_type_attribute_variant (t2
, attributes
);
466 /* For two pointers, do this recursively on the target type,
467 and combine the qualifiers of the two types' targets. */
468 /* This code was turned off; I don't know why.
469 But ANSI C++ specifies doing this with the qualifiers.
470 So I turned it on again. */
472 tree tt1
= TYPE_MAIN_VARIANT (TREE_TYPE (t1
));
473 tree tt2
= TYPE_MAIN_VARIANT (TREE_TYPE (t2
));
474 int type_quals
= (CP_TYPE_QUALS (TREE_TYPE (t1
))
475 | CP_TYPE_QUALS (TREE_TYPE (t2
)));
480 else if (tt1
== void_type_node
|| tt2
== void_type_node
)
481 target
= void_type_node
;
482 else if (tt1
== unknown_type_node
)
484 else if (tt2
== unknown_type_node
)
487 target
= common_type (tt1
, tt2
);
489 target
= cp_build_qualified_type (target
, type_quals
);
490 if (code1
== POINTER_TYPE
)
491 t1
= build_pointer_type (target
);
493 t1
= build_reference_type (target
);
494 t1
= build_type_attribute_variant (t1
, attributes
);
496 if (TREE_CODE (target
) == METHOD_TYPE
)
497 t1
= build_ptrmemfunc_type (t1
);
504 tree elt
= common_type (TREE_TYPE (t1
), TREE_TYPE (t2
));
505 /* Save space: see if the result is identical to one of the args. */
506 if (elt
== TREE_TYPE (t1
) && TYPE_DOMAIN (t1
))
507 return build_type_attribute_variant (t1
, attributes
);
508 if (elt
== TREE_TYPE (t2
) && TYPE_DOMAIN (t2
))
509 return build_type_attribute_variant (t2
, attributes
);
510 /* Merge the element types, and have a size if either arg has one. */
511 t1
= build_cplus_array_type
512 (elt
, TYPE_DOMAIN (TYPE_DOMAIN (t1
) ? t1
: t2
));
513 return build_type_attribute_variant (t1
, attributes
);
517 /* Function types: prefer the one that specified arg types.
518 If both do, merge the arg types. Also merge the return types. */
520 tree valtype
= common_type (TREE_TYPE (t1
), TREE_TYPE (t2
));
521 tree p1
= TYPE_ARG_TYPES (t1
);
522 tree p2
= TYPE_ARG_TYPES (t2
);
525 /* Save space: see if the result is identical to one of the args. */
526 if (valtype
== TREE_TYPE (t1
) && ! p2
)
527 return build_type_attribute_variant (t1
, attributes
);
528 if (valtype
== TREE_TYPE (t2
) && ! p1
)
529 return build_type_attribute_variant (t2
, attributes
);
531 /* Simple way if one arg fails to specify argument types. */
532 if (p1
== NULL_TREE
|| TREE_VALUE (p1
) == void_type_node
)
534 rval
= build_function_type (valtype
, p2
);
535 if ((raises
= TYPE_RAISES_EXCEPTIONS (t2
)))
536 rval
= build_exception_variant (rval
, raises
);
537 return build_type_attribute_variant (rval
, attributes
);
539 raises
= TYPE_RAISES_EXCEPTIONS (t1
);
540 if (p2
== NULL_TREE
|| TREE_VALUE (p2
) == void_type_node
)
542 rval
= build_function_type (valtype
, p1
);
544 rval
= build_exception_variant (rval
, raises
);
545 return build_type_attribute_variant (rval
, attributes
);
548 rval
= build_function_type (valtype
, commonparms (p1
, p2
));
549 rval
= build_exception_variant (rval
, raises
);
550 return build_type_attribute_variant (rval
, attributes
);
555 t1
= TYPE_MAIN_VARIANT (t1
);
556 t2
= TYPE_MAIN_VARIANT (t2
);
558 if (DERIVED_FROM_P (t1
, t2
) && binfo_or_else (t1
, t2
))
559 return build_type_attribute_variant (t1
, attributes
);
560 else if (binfo_or_else (t2
, t1
))
561 return build_type_attribute_variant (t2
, attributes
);
564 compiler_error ("common_type called with uncommon aggregate types");
565 return error_mark_node
;
569 if (TREE_CODE (TREE_TYPE (t1
)) == TREE_CODE (TREE_TYPE (t2
)))
571 /* Get this value the long way, since TYPE_METHOD_BASETYPE
572 is just the main variant of this. */
576 tree b1
= TYPE_OFFSET_BASETYPE (t1
);
577 tree b2
= TYPE_OFFSET_BASETYPE (t2
);
579 if (same_type_p (b1
, b2
)
580 || (DERIVED_FROM_P (b1
, b2
) && binfo_or_else (b1
, b2
)))
581 basetype
= TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (t2
)));
584 if (binfo_or_else (b2
, b1
) == NULL_TREE
)
585 compiler_error ("common_type called with uncommon method types");
586 basetype
= TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (t1
)));
589 raises
= TYPE_RAISES_EXCEPTIONS (t1
);
591 /* If this was a member function type, get back to the
592 original type of type member function (i.e., without
593 the class instance variable up front. */
594 t1
= build_function_type (TREE_TYPE (t1
),
595 TREE_CHAIN (TYPE_ARG_TYPES (t1
)));
596 t2
= build_function_type (TREE_TYPE (t2
),
597 TREE_CHAIN (TYPE_ARG_TYPES (t2
)));
598 t3
= common_type (t1
, t2
);
599 t3
= build_cplus_method_type (basetype
, TREE_TYPE (t3
),
600 TYPE_ARG_TYPES (t3
));
601 t1
= build_exception_variant (t3
, raises
);
604 compiler_error ("common_type called with uncommon method types");
606 return build_type_attribute_variant (t1
, attributes
);
609 if (TREE_TYPE (t1
) == TREE_TYPE (t2
))
611 tree b1
= TYPE_OFFSET_BASETYPE (t1
);
612 tree b2
= TYPE_OFFSET_BASETYPE (t2
);
614 if (same_type_p (b1
, b2
)
615 || (DERIVED_FROM_P (b1
, b2
) && binfo_or_else (b1
, b2
)))
616 return build_type_attribute_variant (t2
, attributes
);
617 else if (binfo_or_else (b2
, b1
))
618 return build_type_attribute_variant (t1
, attributes
);
620 compiler_error ("common_type called with uncommon member types");
623 return build_type_attribute_variant (t1
, attributes
);
627 /* Return 1 if TYPE1 and TYPE2 raise the same exceptions. */
630 compexcepttypes (t1
, t2
)
633 return TYPE_RAISES_EXCEPTIONS (t1
) == TYPE_RAISES_EXCEPTIONS (t2
);
636 /* Compare the array types T1 and T2, using CMP as the type comparison
637 function for the element types. STRICT is as for comptypes. */
640 comp_array_types (cmp
, t1
, t2
, strict
)
641 register int (*cmp
) PROTO((tree
, tree
, int));
651 /* The type of the array elements must be the same. */
652 if (!(TREE_TYPE (t1
) == TREE_TYPE (t2
)
653 || (*cmp
) (TREE_TYPE (t1
), TREE_TYPE (t2
),
654 strict
& ~COMPARE_REDECLARATION
)))
657 d1
= TYPE_DOMAIN (t1
);
658 d2
= TYPE_DOMAIN (t2
);
663 /* If one of the arrays is dimensionless, and the other has a
664 dimension, they are of different types. However, it is legal to
672 declarations for an array object can specify
673 array types that differ by the presence or absence of a major
674 array bound (_dcl.array_). */
676 return strict
& COMPARE_REDECLARATION
;
678 /* Check that the dimensions are the same. */
679 return (cp_tree_equal (TYPE_MIN_VALUE (d1
),
681 && cp_tree_equal (TYPE_MAX_VALUE (d1
),
682 TYPE_MAX_VALUE (d2
)));
685 /* Return 1 if TYPE1 and TYPE2 are compatible types for assignment
686 or various other operations. STRICT is a bitwise-or of the
690 comptypes (type1
, type2
, strict
)
694 register tree t1
= type1
;
695 register tree t2
= type2
;
697 int orig_strict
= strict
;
699 /* The special exemption for redeclaring array types without an
700 array bound only applies at the top level:
705 is not legal, for example. */
706 strict
&= ~COMPARE_REDECLARATION
;
708 /* Suppress errors caused by previously reported errors */
712 /* This should never happen. */
713 my_friendly_assert (t1
!= error_mark_node
, 307);
715 if (t2
== error_mark_node
)
718 if (strict
& COMPARE_RELAXED
)
720 /* Treat an enum type as the unsigned integer type of the same width. */
722 if (TREE_CODE (t1
) == ENUMERAL_TYPE
)
723 t1
= type_for_size (TYPE_PRECISION (t1
), 1);
724 if (TREE_CODE (t2
) == ENUMERAL_TYPE
)
725 t2
= type_for_size (TYPE_PRECISION (t2
), 1);
731 if (TYPE_PTRMEMFUNC_P (t1
))
732 t1
= TYPE_PTRMEMFUNC_FN_TYPE (t1
);
733 if (TYPE_PTRMEMFUNC_P (t2
))
734 t2
= TYPE_PTRMEMFUNC_FN_TYPE (t2
);
736 /* Different classes of types can't be compatible. */
737 if (TREE_CODE (t1
) != TREE_CODE (t2
))
740 /* Qualifiers must match. */
741 if (CP_TYPE_QUALS (t1
) != CP_TYPE_QUALS (t2
))
743 if (strict
== COMPARE_STRICT
744 && TYPE_FOR_JAVA (t1
) != TYPE_FOR_JAVA (t2
))
747 /* Allow for two different type nodes which have essentially the same
748 definition. Note that we already checked for equality of the type
749 qualifiers (just above). */
751 if (TYPE_MAIN_VARIANT (t1
) == TYPE_MAIN_VARIANT (t2
))
754 /* ??? COMP_TYPE_ATTRIBUTES is currently useless for variables as each
755 attribute is its own main variant (`val' will remain 0). */
756 #ifndef COMP_TYPE_ATTRIBUTES
757 #define COMP_TYPE_ATTRIBUTES(t1,t2) 1
760 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
761 if (! (attrval
= COMP_TYPE_ATTRIBUTES (t1
, t2
)))
764 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
767 switch (TREE_CODE (t1
))
769 case TEMPLATE_TEMPLATE_PARM
:
770 if (TEMPLATE_TYPE_IDX (t1
) != TEMPLATE_TYPE_IDX (t2
)
771 || TEMPLATE_TYPE_LEVEL (t1
) != TEMPLATE_TYPE_LEVEL (t2
))
773 if (! comp_template_parms (DECL_TEMPLATE_PARMS (TYPE_NAME (t1
)),
774 DECL_TEMPLATE_PARMS (TYPE_NAME (t2
))))
776 if (!TEMPLATE_TEMPLATE_PARM_TEMPLATE_INFO (t1
)
777 && ! TEMPLATE_TEMPLATE_PARM_TEMPLATE_INFO (t2
))
779 /* Don't check inheritance. */
780 strict
= COMPARE_STRICT
;
785 if (TYPE_TEMPLATE_INFO (t1
) && TYPE_TEMPLATE_INFO (t2
)
786 && (TYPE_TI_TEMPLATE (t1
) == TYPE_TI_TEMPLATE (t2
)
787 || TREE_CODE (t1
) == TEMPLATE_TEMPLATE_PARM
))
788 val
= comp_template_args (TYPE_TI_ARGS (t1
),
791 if ((strict
& COMPARE_BASE
) && DERIVED_FROM_P (t1
, t2
))
796 if ((strict
& COMPARE_RELAXED
) && DERIVED_FROM_P (t2
, t1
))
804 val
= (comptypes (build_pointer_type (TYPE_OFFSET_BASETYPE (t1
)),
805 build_pointer_type (TYPE_OFFSET_BASETYPE (t2
)), strict
)
806 && comptypes (TREE_TYPE (t1
), TREE_TYPE (t2
), strict
));
810 if (! compexcepttypes (t1
, t2
))
813 /* This case is anti-symmetrical!
814 One can pass a base member (or member function)
815 to something expecting a derived member (or member function),
816 but not vice-versa! */
818 val
= (comptypes (TREE_TYPE (t1
), TREE_TYPE (t2
), strict
)
819 && compparms (TYPE_ARG_TYPES (t1
), TYPE_ARG_TYPES (t2
)));
826 /* first, check whether the referred types match with the
827 required level of strictness */
828 val
= comptypes (t1
, t2
, strict
);
831 if (TREE_CODE (t1
) == RECORD_TYPE
832 && TREE_CODE (t2
) == RECORD_TYPE
)
837 if (! compexcepttypes (t1
, t2
))
840 val
= ((TREE_TYPE (t1
) == TREE_TYPE (t2
)
841 || comptypes (TREE_TYPE (t1
), TREE_TYPE (t2
), strict
))
842 && compparms (TYPE_ARG_TYPES (t1
), TYPE_ARG_TYPES (t2
)));
846 /* Target types must match incl. qualifiers. We use ORIG_STRICT
847 here since this is the one place where
848 COMPARE_REDECLARATION should be used. */
849 val
= comp_array_types (comptypes
, t1
, t2
, orig_strict
);
852 case TEMPLATE_TYPE_PARM
:
853 return TEMPLATE_TYPE_IDX (t1
) == TEMPLATE_TYPE_IDX (t2
)
854 && TEMPLATE_TYPE_LEVEL (t1
) == TEMPLATE_TYPE_LEVEL (t2
);
857 if (TYPE_IDENTIFIER (t1
) != TYPE_IDENTIFIER (t2
))
859 return same_type_p (TYPE_CONTEXT (t1
), TYPE_CONTEXT (t2
));
864 return attrval
== 2 && val
== 1 ? 2 : val
;
867 /* Subroutine of comp_target-types. Make sure that the cv-quals change
868 only in the same direction as the target type. */
871 comp_cv_target_types (ttl
, ttr
, nptrs
)
877 if (!at_least_as_qualified_p (ttl
, ttr
)
878 && !at_least_as_qualified_p (ttr
, ttl
))
879 /* The qualifications are incomparable. */
882 if (TYPE_MAIN_VARIANT (ttl
) == TYPE_MAIN_VARIANT (ttr
))
883 return more_qualified_p (ttr
, ttl
) ? -1 : 1;
885 t
= comp_target_types (ttl
, ttr
, nptrs
);
886 if ((t
== 1 && at_least_as_qualified_p (ttl
, ttr
))
887 || (t
== -1 && at_least_as_qualified_p (ttr
, ttl
)))
893 /* Return 1 or -1 if TTL and TTR are pointers to types that are equivalent,
894 ignoring their qualifiers, 0 if not. Return 1 means that TTR can be
895 converted to TTL. Return -1 means that TTL can be converted to TTR but
898 NPTRS is the number of pointers we can strip off and keep cool.
899 This is used to permit (for aggr A, aggr B) A, B* to convert to A*,
900 but to not permit B** to convert to A**.
902 This should go away. Callers should use can_convert or something
903 similar instead. (jason 17 Apr 1997) */
906 comp_target_types (ttl
, ttr
, nptrs
)
910 ttl
= TYPE_MAIN_VARIANT (ttl
);
911 ttr
= TYPE_MAIN_VARIANT (ttr
);
915 if (TREE_CODE (ttr
) != TREE_CODE (ttl
))
918 if ((TREE_CODE (ttr
) == POINTER_TYPE
919 || TREE_CODE (ttr
) == REFERENCE_TYPE
)
920 /* If we get a pointer with nptrs == 0, we don't allow any tweaking
921 of the type pointed to. This is necessary for reference init
922 semantics. We won't get here from a previous call with nptrs == 1;
923 for multi-level pointers we end up in comp_ptr_ttypes. */
926 int is_ptr
= TREE_CODE (ttr
) == POINTER_TYPE
;
928 ttl
= TREE_TYPE (ttl
);
929 ttr
= TREE_TYPE (ttr
);
933 if (TREE_CODE (ttl
) == UNKNOWN_TYPE
934 || TREE_CODE (ttr
) == UNKNOWN_TYPE
)
936 else if (TREE_CODE (ttl
) == VOID_TYPE
937 && TREE_CODE (ttr
) != FUNCTION_TYPE
938 && TREE_CODE (ttr
) != METHOD_TYPE
939 && TREE_CODE (ttr
) != OFFSET_TYPE
)
941 else if (TREE_CODE (ttr
) == VOID_TYPE
942 && TREE_CODE (ttl
) != FUNCTION_TYPE
943 && TREE_CODE (ttl
) != METHOD_TYPE
944 && TREE_CODE (ttl
) != OFFSET_TYPE
)
946 else if (TREE_CODE (ttl
) == POINTER_TYPE
947 || TREE_CODE (ttl
) == ARRAY_TYPE
)
949 if (comp_ptr_ttypes (ttl
, ttr
))
951 else if (comp_ptr_ttypes (ttr
, ttl
))
957 /* Const and volatile mean something different for function types,
958 so the usual checks are not appropriate. */
959 if (TREE_CODE (ttl
) == FUNCTION_TYPE
|| TREE_CODE (ttl
) == METHOD_TYPE
)
960 return comp_target_types (ttl
, ttr
, nptrs
- 1);
962 return comp_cv_target_types (ttl
, ttr
, nptrs
- 1);
965 if (TREE_CODE (ttr
) == ARRAY_TYPE
)
966 return comp_array_types (comp_target_types
, ttl
, ttr
, COMPARE_STRICT
);
967 else if (TREE_CODE (ttr
) == FUNCTION_TYPE
|| TREE_CODE (ttr
) == METHOD_TYPE
)
974 if (!same_type_p (TREE_TYPE (ttl
), TREE_TYPE (ttr
)))
979 switch (comp_target_types (TREE_TYPE (ttl
), TREE_TYPE (ttr
), -1))
988 argsl
= TYPE_ARG_TYPES (ttl
);
989 argsr
= TYPE_ARG_TYPES (ttr
);
991 /* Compare 'this' here, not in comp_target_parms. */
992 if (TREE_CODE (ttr
) == METHOD_TYPE
)
994 tree tl
= TYPE_METHOD_BASETYPE (ttl
);
995 tree tr
= TYPE_METHOD_BASETYPE (ttr
);
997 if (!same_or_base_type_p (tr
, tl
))
999 if (same_or_base_type_p (tl
, tr
))
1005 argsl
= TREE_CHAIN (argsl
);
1006 argsr
= TREE_CHAIN (argsr
);
1009 switch (comp_target_parms (argsl
, argsr
, 1))
1017 return saw_contra
? -1 : 1;
1020 else if (TREE_CODE (ttr
) == OFFSET_TYPE
)
1024 /* Contravariance: we can assign a pointer to base member to a pointer
1025 to derived member. Note difference from simple pointer case, where
1026 we can pass a pointer to derived to a pointer to base. */
1027 if (same_or_base_type_p (TYPE_OFFSET_BASETYPE (ttr
),
1028 TYPE_OFFSET_BASETYPE (ttl
)))
1030 else if (same_or_base_type_p (TYPE_OFFSET_BASETYPE (ttl
),
1031 TYPE_OFFSET_BASETYPE (ttr
)))
1041 ttl
= TREE_TYPE (ttl
);
1042 ttr
= TREE_TYPE (ttr
);
1044 if (TREE_CODE (ttl
) == POINTER_TYPE
1045 || TREE_CODE (ttl
) == ARRAY_TYPE
)
1047 if (comp_ptr_ttypes (ttl
, ttr
))
1053 if (comp_cv_target_types (ttl
, ttr
, nptrs
) == 1)
1058 else if (IS_AGGR_TYPE (ttl
))
1062 if (same_or_base_type_p (build_pointer_type (ttl
),
1063 build_pointer_type (ttr
)))
1065 if (same_or_base_type_p (build_pointer_type (ttr
),
1066 build_pointer_type (ttl
)))
1074 /* Returns 1 if TYPE1 is at least as qualified as TYPE2. */
1077 at_least_as_qualified_p (type1
, type2
)
1081 /* All qualifiers for TYPE2 must also appear in TYPE1. */
1082 return ((CP_TYPE_QUALS (type1
) & CP_TYPE_QUALS (type2
))
1083 == CP_TYPE_QUALS (type2
));
1086 /* Returns 1 if TYPE1 is more qualified than TYPE2. */
1089 more_qualified_p (type1
, type2
)
1093 return (CP_TYPE_QUALS (type1
) != CP_TYPE_QUALS (type2
)
1094 && at_least_as_qualified_p (type1
, type2
));
1097 /* Returns 1 if TYPE1 is more cv-qualified than TYPE2, -1 if TYPE2 is
1098 more cv-qualified that TYPE1, and 0 otherwise. */
1101 comp_cv_qualification (type1
, type2
)
1105 if (CP_TYPE_QUALS (type1
) == CP_TYPE_QUALS (type2
))
1108 if (at_least_as_qualified_p (type1
, type2
))
1111 else if (at_least_as_qualified_p (type2
, type1
))
1117 /* Returns 1 if the cv-qualification signature of TYPE1 is a proper
1118 subset of the cv-qualification signature of TYPE2, and the types
1119 are similar. Returns -1 if the other way 'round, and 0 otherwise. */
1122 comp_cv_qual_signature (type1
, type2
)
1126 if (comp_ptr_ttypes_real (type2
, type1
, -1))
1128 else if (comp_ptr_ttypes_real (type1
, type2
, -1))
1134 /* If two types share a common base type, return that basetype.
1135 If there is not a unique most-derived base type, this function
1136 returns ERROR_MARK_NODE. */
1139 common_base_type (tt1
, tt2
)
1142 tree best
= NULL_TREE
;
1145 /* If one is a baseclass of another, that's good enough. */
1146 if (UNIQUELY_DERIVED_FROM_P (tt1
, tt2
))
1148 if (UNIQUELY_DERIVED_FROM_P (tt2
, tt1
))
1151 /* Otherwise, try to find a unique baseclass of TT1
1152 that is shared by TT2, and follow that down. */
1153 for (i
= CLASSTYPE_N_BASECLASSES (tt1
)-1; i
>= 0; i
--)
1155 tree basetype
= TYPE_BINFO_BASETYPE (tt1
, i
);
1156 tree trial
= common_base_type (basetype
, tt2
);
1159 if (trial
== error_mark_node
)
1161 if (best
== NULL_TREE
)
1163 else if (best
!= trial
)
1164 return error_mark_node
;
1169 for (i
= CLASSTYPE_N_BASECLASSES (tt2
)-1; i
>= 0; i
--)
1171 tree basetype
= TYPE_BINFO_BASETYPE (tt2
, i
);
1172 tree trial
= common_base_type (tt1
, basetype
);
1175 if (trial
== error_mark_node
)
1177 if (best
== NULL_TREE
)
1179 else if (best
!= trial
)
1180 return error_mark_node
;
1186 /* Subroutines of `comptypes'. */
1188 /* Return 1 if two parameter type lists PARMS1 and PARMS2
1189 are equivalent in the sense that functions with those parameter types
1190 can have equivalent types.
1191 If either list is empty, we win.
1192 Otherwise, the two lists must be equivalent, element by element.
1194 C++: See comment above about TYPE1, TYPE2.
1196 STRICT is no longer used. */
1199 compparms (parms1
, parms2
)
1200 tree parms1
, parms2
;
1202 register tree t1
= parms1
, t2
= parms2
;
1204 /* An unspecified parmlist matches any specified parmlist
1205 whose argument types don't need default promotions. */
1209 if (t1
== 0 && t2
== 0)
1211 /* If one parmlist is shorter than the other,
1212 they fail to match. */
1213 if (t1
== 0 || t2
== 0)
1215 if (!same_type_p (TREE_VALUE (t2
), TREE_VALUE (t1
)))
1218 t1
= TREE_CHAIN (t1
);
1219 t2
= TREE_CHAIN (t2
);
1223 /* This really wants return whether or not parameter type lists
1224 would make their owning functions assignment compatible or not.
1226 The return value is like for comp_target_types.
1228 This should go away, possibly with the exception of the empty parmlist
1229 conversion; there are no conversions between function types in C++.
1230 (jason 17 Apr 1997) */
1233 comp_target_parms (parms1
, parms2
, strict
)
1234 tree parms1
, parms2
;
1237 register tree t1
= parms1
, t2
= parms2
;
1238 int warn_contravariance
= 0;
1240 /* In C, an unspecified parmlist matches any specified parmlist
1241 whose argument types don't need default promotions. This is not
1242 true for C++, but let's do it anyway for unfixed headers. */
1244 if (t1
== 0 && t2
!= 0)
1246 cp_pedwarn ("ANSI C++ prohibits conversion from `(%#T)' to `(...)'",
1248 return self_promoting_args_p (t2
);
1251 return self_promoting_args_p (t1
);
1253 for (; t1
|| t2
; t1
= TREE_CHAIN (t1
), t2
= TREE_CHAIN (t2
))
1257 /* If one parmlist is shorter than the other,
1258 they fail to match, unless STRICT is <= 0. */
1259 if (t1
== 0 || t2
== 0)
1264 return 1 + warn_contravariance
;
1265 return ((t1
&& TREE_PURPOSE (t1
)) + warn_contravariance
);
1267 p1
= TREE_VALUE (t1
);
1268 p2
= TREE_VALUE (t2
);
1269 if (same_type_p (p1
, p2
))
1275 if ((TREE_CODE (p1
) == POINTER_TYPE
&& TREE_CODE (p2
) == POINTER_TYPE
)
1276 || (TREE_CODE (p1
) == REFERENCE_TYPE
1277 && TREE_CODE (p2
) == REFERENCE_TYPE
))
1280 && (TYPE_MAIN_VARIANT (TREE_TYPE (p1
))
1281 == TYPE_MAIN_VARIANT (TREE_TYPE (p2
))))
1284 /* The following is wrong for contravariance,
1285 but many programs depend on it. */
1286 if (TREE_TYPE (p1
) == void_type_node
)
1288 if (TREE_TYPE (p2
) == void_type_node
)
1290 warn_contravariance
= 1;
1293 if (IS_AGGR_TYPE (TREE_TYPE (p1
))
1294 && !same_type_p (TYPE_MAIN_VARIANT (TREE_TYPE (p1
)),
1295 TYPE_MAIN_VARIANT (TREE_TYPE (p2
))))
1298 /* Note backwards order due to contravariance. */
1299 if (comp_target_types (p2
, p1
, 1) <= 0)
1301 if (comp_target_types (p1
, p2
, 1) > 0)
1303 warn_contravariance
= 1;
1310 return warn_contravariance
? -1 : 1;
1313 /* Return 1 if PARMS specifies a fixed number of parameters
1314 and none of their types is affected by default promotions. */
1317 self_promoting_args_p (parms
)
1321 for (t
= parms
; t
; t
= TREE_CHAIN (t
))
1323 register tree type
= TREE_VALUE (t
);
1325 if (TREE_CHAIN (t
) == 0 && type
!= void_type_node
)
1331 if (TYPE_MAIN_VARIANT (type
) == float_type_node
)
1334 if (C_PROMOTING_INTEGER_TYPE_P (type
))
1340 /* Return an unsigned type the same as TYPE in other respects.
1342 C++: must make these work for type variants as well. */
1345 unsigned_type (type
)
1348 tree type1
= TYPE_MAIN_VARIANT (type
);
1349 if (type1
== signed_char_type_node
|| type1
== char_type_node
)
1350 return unsigned_char_type_node
;
1351 if (type1
== integer_type_node
)
1352 return unsigned_type_node
;
1353 if (type1
== short_integer_type_node
)
1354 return short_unsigned_type_node
;
1355 if (type1
== long_integer_type_node
)
1356 return long_unsigned_type_node
;
1357 if (type1
== long_long_integer_type_node
)
1358 return long_long_unsigned_type_node
;
1359 #if HOST_BITS_PER_WIDE_INT >= 64
1360 if (type1
== intTI_type_node
)
1361 return unsigned_intTI_type_node
;
1363 if (type1
== intDI_type_node
)
1364 return unsigned_intDI_type_node
;
1365 if (type1
== intSI_type_node
)
1366 return unsigned_intSI_type_node
;
1367 if (type1
== intHI_type_node
)
1368 return unsigned_intHI_type_node
;
1369 if (type1
== intQI_type_node
)
1370 return unsigned_intQI_type_node
;
1372 return signed_or_unsigned_type (1, type
);
1375 /* Return a signed type the same as TYPE in other respects. */
1381 tree type1
= TYPE_MAIN_VARIANT (type
);
1382 if (type1
== unsigned_char_type_node
|| type1
== char_type_node
)
1383 return signed_char_type_node
;
1384 if (type1
== unsigned_type_node
)
1385 return integer_type_node
;
1386 if (type1
== short_unsigned_type_node
)
1387 return short_integer_type_node
;
1388 if (type1
== long_unsigned_type_node
)
1389 return long_integer_type_node
;
1390 if (type1
== long_long_unsigned_type_node
)
1391 return long_long_integer_type_node
;
1392 #if HOST_BITS_PER_WIDE_INT >= 64
1393 if (type1
== unsigned_intTI_type_node
)
1394 return intTI_type_node
;
1396 if (type1
== unsigned_intDI_type_node
)
1397 return intDI_type_node
;
1398 if (type1
== unsigned_intSI_type_node
)
1399 return intSI_type_node
;
1400 if (type1
== unsigned_intHI_type_node
)
1401 return intHI_type_node
;
1402 if (type1
== unsigned_intQI_type_node
)
1403 return intQI_type_node
;
1405 return signed_or_unsigned_type (0, type
);
1408 /* Return a type the same as TYPE except unsigned or
1409 signed according to UNSIGNEDP. */
1412 signed_or_unsigned_type (unsignedp
, type
)
1416 if (! INTEGRAL_TYPE_P (type
)
1417 || TREE_UNSIGNED (type
) == unsignedp
)
1420 if (TYPE_PRECISION (type
) == TYPE_PRECISION (signed_char_type_node
))
1421 return unsignedp
? unsigned_char_type_node
: signed_char_type_node
;
1422 if (TYPE_PRECISION (type
) == TYPE_PRECISION (integer_type_node
))
1423 return unsignedp
? unsigned_type_node
: integer_type_node
;
1424 if (TYPE_PRECISION (type
) == TYPE_PRECISION (short_integer_type_node
))
1425 return unsignedp
? short_unsigned_type_node
: short_integer_type_node
;
1426 if (TYPE_PRECISION (type
) == TYPE_PRECISION (long_integer_type_node
))
1427 return unsignedp
? long_unsigned_type_node
: long_integer_type_node
;
1428 if (TYPE_PRECISION (type
) == TYPE_PRECISION (long_long_integer_type_node
))
1429 return (unsignedp
? long_long_unsigned_type_node
1430 : long_long_integer_type_node
);
1434 /* Compute the value of the `sizeof' operator. */
1440 enum tree_code code
= TREE_CODE (type
);
1443 if (processing_template_decl
)
1444 return build_min (SIZEOF_EXPR
, sizetype
, type
);
1446 if (code
== FUNCTION_TYPE
)
1448 if (pedantic
|| warn_pointer_arith
)
1449 pedwarn ("ANSI C++ forbids taking the sizeof a function type");
1450 return size_int (1);
1452 if (code
== METHOD_TYPE
)
1454 if (pedantic
|| warn_pointer_arith
)
1455 pedwarn ("ANSI C++ forbids taking the sizeof a method type");
1456 return size_int (1);
1458 if (code
== VOID_TYPE
)
1460 if (pedantic
|| warn_pointer_arith
)
1461 pedwarn ("ANSI C++ forbids taking the sizeof a void type");
1462 return size_int (1);
1464 if (code
== ERROR_MARK
)
1465 return size_int (1);
1467 /* ARM $5.3.2: ``When applied to a reference, the result is the size of the
1468 referenced object.'' */
1469 if (code
== REFERENCE_TYPE
)
1470 type
= TREE_TYPE (type
);
1472 /* We couldn't find anything in the ARM or the draft standard that says,
1473 one way or the other, if doing sizeof on something that doesn't have
1474 an object associated with it is correct or incorrect. For example, if
1475 you declare `struct S { char str[16]; };', and in your program do
1476 a `sizeof (S::str)', should we flag that as an error or should we give
1477 the size of it? Since it seems like a reasonable thing to do, we'll go
1478 with giving the value. */
1479 if (code
== OFFSET_TYPE
)
1480 type
= TREE_TYPE (type
);
1482 /* @@ This also produces an error for a signature ref.
1483 In that case we should be able to do better. */
1484 if (IS_SIGNATURE (type
))
1486 error ("`sizeof' applied to a signature type");
1487 return size_int (0);
1490 if (TYPE_SIZE (complete_type (type
)) == 0)
1492 cp_error ("`sizeof' applied to incomplete type `%T'", type
);
1493 return size_int (0);
1496 /* Convert in case a char is more than one unit. */
1497 t
= size_binop (CEIL_DIV_EXPR
, TYPE_SIZE (type
),
1498 size_int (TYPE_PRECISION (char_type_node
)));
1499 t
= convert (sizetype
, t
);
1500 /* size_binop does not put the constant in range, so do it now. */
1501 if (TREE_CODE (t
) == INTEGER_CST
&& force_fit_type (t
, 0))
1502 TREE_CONSTANT_OVERFLOW (t
) = TREE_OVERFLOW (t
) = 1;
1510 if (processing_template_decl
)
1511 return build_min (SIZEOF_EXPR
, sizetype
, e
);
1513 if (TREE_CODE (e
) == COMPONENT_REF
1514 && DECL_C_BIT_FIELD (TREE_OPERAND (e
, 1)))
1515 error ("sizeof applied to a bit-field");
1516 /* ANSI says arrays and functions are converted inside comma.
1517 But we can't really convert them in build_compound_expr
1518 because that would break commas in lvalues.
1519 So do the conversion here if operand was a comma. */
1520 if (TREE_CODE (e
) == COMPOUND_EXPR
1521 && (TREE_CODE (TREE_TYPE (e
)) == ARRAY_TYPE
1522 || TREE_CODE (TREE_TYPE (e
)) == FUNCTION_TYPE
))
1523 e
= default_conversion (e
);
1524 else if (is_overloaded_fn (e
))
1526 pedwarn ("ANSI C++ forbids taking the sizeof a function type");
1527 return size_int (1);
1530 return c_sizeof (TREE_TYPE (e
));
1534 c_sizeof_nowarn (type
)
1537 enum tree_code code
= TREE_CODE (type
);
1540 if (code
== FUNCTION_TYPE
1541 || code
== METHOD_TYPE
1542 || code
== VOID_TYPE
1543 || code
== ERROR_MARK
)
1544 return size_int (1);
1545 if (code
== REFERENCE_TYPE
)
1546 type
= TREE_TYPE (type
);
1548 if (TYPE_SIZE (type
) == 0)
1549 return size_int (0);
1551 /* Convert in case a char is more than one unit. */
1552 t
= size_binop (CEIL_DIV_EXPR
, TYPE_SIZE (type
),
1553 size_int (TYPE_PRECISION (char_type_node
)));
1554 t
= convert (sizetype
, t
);
1555 force_fit_type (t
, 0);
1559 /* Implement the __alignof keyword: Return the minimum required
1560 alignment of TYPE, measured in bytes. */
1566 enum tree_code code
= TREE_CODE (type
);
1569 if (processing_template_decl
)
1570 return build_min (ALIGNOF_EXPR
, sizetype
, type
);
1572 if (code
== FUNCTION_TYPE
|| code
== METHOD_TYPE
)
1573 return size_int (FUNCTION_BOUNDARY
/ BITS_PER_UNIT
);
1575 if (code
== VOID_TYPE
|| code
== ERROR_MARK
)
1576 return size_int (1);
1578 /* C++: this is really correct! */
1579 if (code
== REFERENCE_TYPE
)
1580 type
= TREE_TYPE (type
);
1582 /* @@ This also produces an error for a signature ref.
1583 In that case we should be able to do better. */
1584 if (IS_SIGNATURE (type
))
1586 error ("`__alignof' applied to a signature type");
1587 return size_int (1);
1590 t
= size_int (TYPE_ALIGN (type
) / BITS_PER_UNIT
);
1591 force_fit_type (t
, 0);
1595 /* Perform default promotions for C data used in expressions.
1596 Arrays and functions are converted to pointers;
1597 enumeral types or short or char, to int.
1598 In addition, manifest constants symbols are replaced by their values.
1600 C++: this will automatically bash references to their target type. */
1603 decay_conversion (exp
)
1606 register tree type
= TREE_TYPE (exp
);
1607 register enum tree_code code
= TREE_CODE (type
);
1609 if (code
== OFFSET_TYPE
)
1611 if (TREE_CODE (exp
) == OFFSET_REF
)
1612 return decay_conversion (resolve_offset_ref (exp
));
1614 type
= TREE_TYPE (type
);
1615 code
= TREE_CODE (type
);
1617 if (type
== unknown_type_node
)
1619 cp_pedwarn ("assuming & on overloaded member function");
1620 return build_unary_op (ADDR_EXPR
, exp
, 0);
1624 if (code
== REFERENCE_TYPE
)
1626 exp
= convert_from_reference (exp
);
1627 type
= TREE_TYPE (exp
);
1628 code
= TREE_CODE (type
);
1631 /* Constants can be used directly unless they're not loadable. */
1632 if (TREE_CODE (exp
) == CONST_DECL
)
1633 exp
= DECL_INITIAL (exp
);
1634 /* Replace a nonvolatile const static variable with its value. */
1635 else if (TREE_READONLY_DECL_P (exp
))
1637 exp
= decl_constant_value (exp
);
1638 type
= TREE_TYPE (exp
);
1641 /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
1642 Leave such NOP_EXPRs, since RHS is being used in non-lvalue context. */
1644 if (code
== VOID_TYPE
)
1646 error ("void value not ignored as it ought to be");
1647 return error_mark_node
;
1649 if (code
== METHOD_TYPE
)
1651 cp_pedwarn ("assuming & on `%E'", exp
);
1652 return build_unary_op (ADDR_EXPR
, exp
, 0);
1654 if (code
== FUNCTION_TYPE
|| is_overloaded_fn (exp
))
1656 return build_unary_op (ADDR_EXPR
, exp
, 0);
1658 if (code
== ARRAY_TYPE
)
1663 if (TREE_CODE (exp
) == INDIRECT_REF
)
1665 /* Stripping away the INDIRECT_REF is not the right
1666 thing to do for references... */
1667 tree inner
= TREE_OPERAND (exp
, 0);
1668 if (TREE_CODE (TREE_TYPE (inner
)) == REFERENCE_TYPE
)
1670 inner
= build1 (CONVERT_EXPR
,
1671 build_pointer_type (TREE_TYPE
1672 (TREE_TYPE (inner
))),
1674 TREE_CONSTANT (inner
) = TREE_CONSTANT (TREE_OPERAND (inner
, 0));
1676 return cp_convert (build_pointer_type (TREE_TYPE (type
)), inner
);
1679 if (TREE_CODE (exp
) == COMPOUND_EXPR
)
1681 tree op1
= decay_conversion (TREE_OPERAND (exp
, 1));
1682 return build (COMPOUND_EXPR
, TREE_TYPE (op1
),
1683 TREE_OPERAND (exp
, 0), op1
);
1687 && ! (TREE_CODE (exp
) == CONSTRUCTOR
&& TREE_STATIC (exp
)))
1689 error ("invalid use of non-lvalue array");
1690 return error_mark_node
;
1693 ptrtype
= build_pointer_type (TREE_TYPE (type
));
1695 if (TREE_CODE (exp
) == VAR_DECL
)
1697 /* ??? This is not really quite correct
1698 in that the type of the operand of ADDR_EXPR
1699 is not the target type of the type of the ADDR_EXPR itself.
1700 Question is, can this lossage be avoided? */
1701 adr
= build1 (ADDR_EXPR
, ptrtype
, exp
);
1702 if (mark_addressable (exp
) == 0)
1703 return error_mark_node
;
1704 TREE_CONSTANT (adr
) = staticp (exp
);
1705 TREE_SIDE_EFFECTS (adr
) = 0; /* Default would be, same as EXP. */
1708 /* This way is better for a COMPONENT_REF since it can
1709 simplify the offset for a component. */
1710 adr
= build_unary_op (ADDR_EXPR
, exp
, 1);
1711 return cp_convert (ptrtype
, adr
);
1718 default_conversion (exp
)
1722 enum tree_code code
;
1724 exp
= decay_conversion (exp
);
1726 type
= TREE_TYPE (exp
);
1727 code
= TREE_CODE (type
);
1729 if (INTEGRAL_CODE_P (code
))
1731 tree t
= type_promotes_to (type
);
1733 return cp_convert (t
, exp
);
1739 /* Take the address of an inline function without setting TREE_ADDRESSABLE
1743 inline_conversion (exp
)
1746 if (TREE_CODE (exp
) == FUNCTION_DECL
)
1747 exp
= build1 (ADDR_EXPR
, build_pointer_type (TREE_TYPE (exp
)), exp
);
1752 /* Returns nonzero iff exp is a STRING_CST or the result of applying
1753 decay_conversion to one. */
1756 string_conv_p (totype
, exp
, warn
)
1762 if (! flag_const_strings
|| TREE_CODE (totype
) != POINTER_TYPE
)
1765 t
= TREE_TYPE (totype
);
1766 if (!same_type_p (t
, char_type_node
)
1767 && !same_type_p (t
, wchar_type_node
))
1770 if (TREE_CODE (exp
) == STRING_CST
)
1772 /* Make sure that we don't try to convert between char and wchar_t. */
1773 if (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (exp
))) != t
)
1778 /* Is this a string constant which has decayed to 'const char *'? */
1779 t
= build_pointer_type (build_qualified_type (t
, TYPE_QUAL_CONST
));
1780 if (!same_type_p (TREE_TYPE (exp
), t
))
1783 if (TREE_CODE (exp
) != ADDR_EXPR
1784 || TREE_CODE (TREE_OPERAND (exp
, 0)) != STRING_CST
)
1788 /* This warning is not very useful, as it complains about printf. */
1789 if (warn
&& warn_write_strings
)
1790 cp_warning ("deprecated conversion from string constant to `%T'", totype
);
1796 build_object_ref (datum
, basetype
, field
)
1797 tree datum
, basetype
, field
;
1800 if (datum
== error_mark_node
)
1801 return error_mark_node
;
1803 dtype
= TREE_TYPE (datum
);
1804 if (TREE_CODE (dtype
) == REFERENCE_TYPE
)
1805 dtype
= TREE_TYPE (dtype
);
1806 if (! IS_AGGR_TYPE_CODE (TREE_CODE (dtype
)))
1808 cp_error ("request for member `%T::%D' in expression of non-aggregate type `%T'",
1809 basetype
, field
, dtype
);
1810 return error_mark_node
;
1812 else if (IS_SIGNATURE (basetype
))
1814 warning ("signature name in scope resolution ignored");
1815 return build_component_ref (datum
, field
, NULL_TREE
, 1);
1817 else if (is_aggr_type (basetype
, 1))
1819 tree binfo
= binfo_or_else (basetype
, dtype
);
1821 return build_x_component_ref (build_scoped_ref (datum
, basetype
),
1824 return error_mark_node
;
1827 /* Like `build_component_ref, but uses an already found field, and converts
1828 from a reference. Must compute access for current_class_ref.
1832 build_component_ref_1 (datum
, field
, protect
)
1836 return convert_from_reference
1837 (build_component_ref (datum
, field
, NULL_TREE
, protect
));
1840 /* Given a COND_EXPR, MIN_EXPR, or MAX_EXPR in T, return it in a form that we
1841 can, for example, use as an lvalue. This code used to be in
1842 unary_complex_lvalue, but we needed it to deal with `a = (d == c) ? b : c'
1843 expressions, where we're dealing with aggregates. But now it's again only
1844 called from unary_complex_lvalue. The case (in particular) that led to
1845 this was with CODE == ADDR_EXPR, since it's not an lvalue when we'd
1849 rationalize_conditional_expr (code
, t
)
1850 enum tree_code code
;
1853 /* For MIN_EXPR or MAX_EXPR, fold-const.c has arranged things so that
1854 the first operand is always the one to be used if both operands
1855 are equal, so we know what conditional expression this used to be. */
1856 if (TREE_CODE (t
) == MIN_EXPR
|| TREE_CODE (t
) == MAX_EXPR
)
1859 build_conditional_expr (build_x_binary_op ((TREE_CODE (t
) == MIN_EXPR
1860 ? LE_EXPR
: GE_EXPR
),
1861 TREE_OPERAND (t
, 0),
1862 TREE_OPERAND (t
, 1)),
1863 build_unary_op (code
, TREE_OPERAND (t
, 0), 0),
1864 build_unary_op (code
, TREE_OPERAND (t
, 1), 0));
1868 build_conditional_expr (TREE_OPERAND (t
, 0),
1869 build_unary_op (code
, TREE_OPERAND (t
, 1), 0),
1870 build_unary_op (code
, TREE_OPERAND (t
, 2), 0));
1873 /* Given the TYPE of an anonymous union field inside T, return the
1874 FIELD_DECL for the field. If not found return NULL_TREE. Because
1875 anonymous unions can nest, we must also search all anonymous unions
1876 that are directly reachable. */
1879 lookup_anon_field (t
, type
)
1884 for (field
= TYPE_FIELDS (t
); field
; field
= TREE_CHAIN (field
))
1886 if (TREE_STATIC (field
))
1888 if (TREE_CODE (field
) != FIELD_DECL
)
1891 /* If we find it directly, return the field. */
1892 if (DECL_NAME (field
) == NULL_TREE
1893 && type
== TREE_TYPE (field
))
1898 /* Otherwise, it could be nested, search harder. */
1899 if (DECL_NAME (field
) == NULL_TREE
1900 && TREE_CODE (TREE_TYPE (field
)) == UNION_TYPE
)
1902 tree subfield
= lookup_anon_field (TREE_TYPE (field
), type
);
1910 /* Build a COMPONENT_REF for a given DATUM, and it's member COMPONENT.
1911 COMPONENT can be an IDENTIFIER_NODE that is the name of the member
1912 that we are interested in, or it can be a FIELD_DECL. */
1915 build_component_ref (datum
, component
, basetype_path
, protect
)
1916 tree datum
, component
, basetype_path
;
1919 register tree basetype
;
1920 register enum tree_code code
;
1921 register tree field
= NULL
;
1926 if (processing_template_decl
)
1927 return build_min_nt (COMPONENT_REF
, datum
, component
);
1929 if (datum
== error_mark_node
1930 || TREE_TYPE (datum
) == error_mark_node
)
1931 return error_mark_node
;
1933 /* BASETYPE holds the type of the class containing the COMPONENT. */
1934 basetype
= TYPE_MAIN_VARIANT (TREE_TYPE (datum
));
1936 /* If DATUM is a COMPOUND_EXPR or COND_EXPR, move our reference
1938 switch (TREE_CODE (datum
))
1942 tree value
= build_component_ref (TREE_OPERAND (datum
, 1), component
,
1943 basetype_path
, protect
);
1944 return build (COMPOUND_EXPR
, TREE_TYPE (value
),
1945 TREE_OPERAND (datum
, 0), value
);
1948 return build_conditional_expr
1949 (TREE_OPERAND (datum
, 0),
1950 build_component_ref (TREE_OPERAND (datum
, 1), component
,
1951 basetype_path
, protect
),
1952 build_component_ref (TREE_OPERAND (datum
, 2), component
,
1953 basetype_path
, protect
));
1956 cp_error ("invalid use of %D", datum
);
1957 datum
= error_mark_node
;
1964 code
= TREE_CODE (basetype
);
1966 if (code
== REFERENCE_TYPE
)
1968 datum
= convert_from_reference (datum
);
1969 basetype
= TYPE_MAIN_VARIANT (TREE_TYPE (datum
));
1970 code
= TREE_CODE (basetype
);
1972 if (TREE_CODE (datum
) == OFFSET_REF
)
1974 datum
= resolve_offset_ref (datum
);
1975 basetype
= TYPE_MAIN_VARIANT (TREE_TYPE (datum
));
1976 code
= TREE_CODE (basetype
);
1979 /* First, see if there is a field or component with name COMPONENT. */
1980 if (TREE_CODE (component
) == TREE_LIST
)
1982 /* I could not trigger this code. MvL */
1983 my_friendly_abort (980326);
1985 my_friendly_assert (!(TREE_CHAIN (component
) == NULL_TREE
1986 && DECL_CHAIN (TREE_VALUE (component
)) == NULL_TREE
), 309);
1988 return build (COMPONENT_REF
, TREE_TYPE (component
), datum
, component
);
1991 if (! IS_AGGR_TYPE_CODE (code
))
1993 if (code
!= ERROR_MARK
)
1994 cp_error ("request for member `%D' in `%E', which is of non-aggregate type `%T'",
1995 component
, datum
, basetype
);
1996 return error_mark_node
;
1999 if (!complete_type_or_else (basetype
))
2000 return error_mark_node
;
2002 if (TREE_CODE (component
) == BIT_NOT_EXPR
)
2004 if (TYPE_IDENTIFIER (basetype
) != TREE_OPERAND (component
, 0))
2006 cp_error ("destructor specifier `%T::~%T' must have matching names",
2007 basetype
, TREE_OPERAND (component
, 0));
2008 return error_mark_node
;
2010 if (! TYPE_HAS_DESTRUCTOR (basetype
))
2012 cp_error ("type `%T' has no destructor", basetype
);
2013 return error_mark_node
;
2015 return TREE_VEC_ELT (CLASSTYPE_METHOD_VEC (basetype
), 1);
2018 /* Look up component name in the structure type definition. */
2019 if (CLASSTYPE_VFIELD (basetype
)
2020 && DECL_NAME (CLASSTYPE_VFIELD (basetype
)) == component
)
2021 /* Special-case this because if we use normal lookups in an ambiguous
2022 hierarchy, the compiler will abort (because vptr lookups are
2023 not supposed to be ambiguous. */
2024 field
= CLASSTYPE_VFIELD (basetype
);
2025 else if (TREE_CODE (component
) == FIELD_DECL
)
2027 else if (TREE_CODE (component
) == TYPE_DECL
)
2029 cp_error ("invalid use of type decl `%#D' as expression", component
);
2030 return error_mark_node
;
2034 tree name
= component
;
2035 if (TREE_CODE (component
) == VAR_DECL
)
2036 name
= DECL_NAME (component
);
2037 if (basetype_path
== NULL_TREE
)
2038 basetype_path
= TYPE_BINFO (basetype
);
2039 field
= lookup_field (basetype_path
, name
,
2040 protect
&& !VFIELD_NAME_P (name
), 0);
2041 if (field
== error_mark_node
)
2042 return error_mark_node
;
2044 if (field
== NULL_TREE
)
2046 /* Not found as a data field, look for it as a method. If found,
2047 then if this is the only possible one, return it, else
2048 report ambiguity error. */
2049 tree fndecls
= lookup_fnfields (basetype_path
, name
, 1);
2050 if (fndecls
== error_mark_node
)
2051 return error_mark_node
;
2054 /* If the function is unique and static, we can resolve it
2055 now. Otherwise, we have to wait and see what context it is
2056 used in; a component_ref involving a non-static member
2057 function can only be used in a call (expr.ref). */
2058 if (TREE_CHAIN (fndecls
) == NULL_TREE
2059 && TREE_CODE (TREE_VALUE (fndecls
)) == FUNCTION_DECL
2060 && DECL_STATIC_FUNCTION_P (TREE_VALUE (fndecls
)))
2062 tree fndecl
= TREE_VALUE (fndecls
);
2063 enforce_access (TREE_PURPOSE (fndecls
), fndecl
);
2069 ref
= build (COMPONENT_REF
, unknown_type_node
,
2075 cp_error ("`%#T' has no member named `%D'", basetype
, name
);
2076 return error_mark_node
;
2078 else if (TREE_TYPE (field
) == error_mark_node
)
2079 return error_mark_node
;
2081 if (TREE_CODE (field
) != FIELD_DECL
)
2083 if (TREE_CODE (field
) == TYPE_DECL
)
2084 cp_pedwarn ("invalid use of type decl `%#D' as expression", field
);
2085 else if (DECL_RTL (field
) != 0)
2088 TREE_USED (field
) = 1;
2093 /* See if we have to do any conversions so that we pick up the field from the
2095 if (DECL_FIELD_CONTEXT (field
) != basetype
)
2097 tree context
= DECL_FIELD_CONTEXT (field
);
2098 tree base
= context
;
2099 while (!same_type_p (base
, basetype
) && TYPE_NAME (base
)
2100 && ANON_UNION_TYPE_P (base
))
2102 base
= TYPE_CONTEXT (base
);
2105 /* Handle base classes here... */
2106 if (base
!= basetype
&& TYPE_USES_COMPLEX_INHERITANCE (basetype
))
2108 tree addr
= build_unary_op (ADDR_EXPR
, datum
, 0);
2109 if (integer_zerop (addr
))
2111 error ("invalid reference to NULL ptr, use ptr-to-member instead");
2112 return error_mark_node
;
2114 if (VBASE_NAME_P (DECL_NAME (field
)))
2116 /* It doesn't matter which vbase pointer we grab, just
2117 find one of them. */
2118 tree binfo
= get_binfo (base
,
2119 TREE_TYPE (TREE_TYPE (addr
)), 0);
2120 addr
= convert_pointer_to_real (binfo
, addr
);
2123 addr
= convert_pointer_to (base
, addr
);
2124 datum
= build_indirect_ref (addr
, NULL_PTR
);
2125 my_friendly_assert (datum
!= error_mark_node
, 311);
2129 /* Handle things from anon unions here... */
2130 if (TYPE_NAME (context
) && ANON_UNION_TYPE_P (context
))
2132 tree subfield
= lookup_anon_field (basetype
, context
);
2133 tree subdatum
= build_component_ref (datum
, subfield
,
2134 basetype_path
, protect
);
2135 return build_component_ref (subdatum
, field
, basetype_path
, protect
);
2139 /* Compute the type of the field, as described in [expr.ref]. */
2140 type_quals
= TYPE_UNQUALIFIED
;
2141 field_type
= TREE_TYPE (field
);
2142 if (TREE_CODE (field_type
) == REFERENCE_TYPE
)
2143 /* The standard says that the type of the result should be the
2144 type referred to by the reference. But for now, at least, we
2145 do the conversion from reference type later. */
2149 type_quals
= (CP_TYPE_QUALS (field_type
)
2150 | CP_TYPE_QUALS (TREE_TYPE (datum
)));
2152 /* A field is const (volatile) if the enclosing object, or the
2153 field itself, is const (volatile). But, a mutable field is
2154 not const, even within a const object. */
2155 if (DECL_LANG_SPECIFIC (field
) && DECL_MUTABLE_P (field
))
2156 type_quals
&= ~TYPE_QUAL_CONST
;
2157 if (!IS_SIGNATURE (field_type
))
2158 field_type
= cp_build_qualified_type (field_type
, type_quals
);
2161 ref
= fold (build (COMPONENT_REF
, field_type
,
2162 break_out_cleanups (datum
), field
));
2164 /* Mark the expression const or volatile, as appropriate. Even
2165 though we've dealt with the type above, we still have to mark the
2166 expression itself. */
2167 if (type_quals
& TYPE_QUAL_CONST
)
2168 TREE_READONLY (ref
) = 1;
2169 else if (type_quals
& TYPE_QUAL_VOLATILE
)
2170 TREE_THIS_VOLATILE (ref
) = 1;
2175 /* Variant of build_component_ref for use in expressions, which should
2176 never have REFERENCE_TYPE. */
2179 build_x_component_ref (datum
, component
, basetype_path
, protect
)
2180 tree datum
, component
, basetype_path
;
2183 tree t
= build_component_ref (datum
, component
, basetype_path
, protect
);
2185 if (! processing_template_decl
)
2186 t
= convert_from_reference (t
);
2191 /* Given an expression PTR for a pointer, return an expression
2192 for the value pointed to.
2193 ERRORSTRING is the name of the operator to appear in error messages.
2195 This function may need to overload OPERATOR_FNNAME.
2196 Must also handle REFERENCE_TYPEs for C++. */
2199 build_x_indirect_ref (ptr
, errorstring
)
2205 if (processing_template_decl
)
2206 return build_min_nt (INDIRECT_REF
, ptr
);
2208 rval
= build_opfncall (INDIRECT_REF
, LOOKUP_NORMAL
, ptr
, NULL_TREE
,
2212 return build_indirect_ref (ptr
, errorstring
);
2216 build_indirect_ref (ptr
, errorstring
)
2220 register tree pointer
, type
;
2222 if (ptr
== error_mark_node
)
2223 return error_mark_node
;
2225 pointer
= (TREE_CODE (TREE_TYPE (ptr
)) == REFERENCE_TYPE
2226 ? ptr
: default_conversion (ptr
));
2227 type
= TREE_TYPE (pointer
);
2229 if (ptr
== current_class_ptr
)
2230 return current_class_ref
;
2232 if (TYPE_PTR_P (type
) || TREE_CODE (type
) == REFERENCE_TYPE
)
2236 If the type of the expression is "pointer to T," the type
2237 of the result is "T."
2239 We must use the canonical variant because certain parts of
2240 the back end, like fold, do pointer comparisons between
2242 tree t
= canonical_type_variant (TREE_TYPE (type
));
2244 if (TREE_CODE (pointer
) == ADDR_EXPR
2246 && same_type_p (t
, TREE_TYPE (TREE_OPERAND (pointer
, 0))))
2247 /* The POINTER was something like `&x'. We simplify `*&x' to
2249 return TREE_OPERAND (pointer
, 0);
2252 tree ref
= build1 (INDIRECT_REF
, t
, pointer
);
2254 /* We *must* set TREE_READONLY when dereferencing a pointer to const,
2255 so that we get the proper error message if the result is used
2256 to assign to. Also, &* is supposed to be a no-op. */
2257 TREE_READONLY (ref
) = CP_TYPE_CONST_P (t
);
2258 TREE_THIS_VOLATILE (ref
) = CP_TYPE_VOLATILE_P (t
);
2259 TREE_SIDE_EFFECTS (ref
)
2260 = (TREE_THIS_VOLATILE (ref
) || TREE_SIDE_EFFECTS (pointer
)
2265 /* `pointer' won't be an error_mark_node if we were given a
2266 pointer to member, so it's cool to check for this here. */
2267 else if (TYPE_PTRMEM_P (type
) || TYPE_PTRMEMFUNC_P (type
))
2268 error ("invalid use of `%s' on pointer to member", errorstring
);
2269 else if (TREE_CODE (type
) == RECORD_TYPE
2270 && (IS_SIGNATURE_POINTER (type
) || IS_SIGNATURE_REFERENCE (type
)))
2271 error ("cannot dereference signature pointer/reference");
2272 else if (pointer
!= error_mark_node
)
2275 error ("invalid type argument of `%s'", errorstring
);
2277 error ("invalid type argument");
2279 return error_mark_node
;
2282 /* This handles expressions of the form "a[i]", which denotes
2285 This is logically equivalent in C to *(a+i), but we may do it differently.
2286 If A is a variable or a member, we generate a primitive ARRAY_REF.
2287 This avoids forcing the array out of registers, and can work on
2288 arrays that are not lvalues (for example, members of structures returned
2291 If INDEX is of some user-defined type, it must be converted to
2292 integer type. Otherwise, to make a compatible PLUS_EXPR, it
2293 will inherit the type of the array, which will be some pointer type. */
2296 build_array_ref (array
, idx
)
2301 error ("subscript missing in array reference");
2302 return error_mark_node
;
2305 if (TREE_TYPE (array
) == error_mark_node
2306 || TREE_TYPE (idx
) == error_mark_node
)
2307 return error_mark_node
;
2309 if (TREE_CODE (TREE_TYPE (array
)) == ARRAY_TYPE
2310 && TREE_CODE (array
) != INDIRECT_REF
)
2314 /* Subscripting with type char is likely to lose
2315 on a machine where chars are signed.
2316 So warn on any machine, but optionally.
2317 Don't warn for unsigned char since that type is safe.
2318 Don't warn for signed char because anyone who uses that
2319 must have done so deliberately. */
2320 if (warn_char_subscripts
2321 && TYPE_MAIN_VARIANT (TREE_TYPE (idx
)) == char_type_node
)
2322 warning ("array subscript has type `char'");
2324 /* Apply default promotions *after* noticing character types. */
2325 idx
= default_conversion (idx
);
2327 if (TREE_CODE (TREE_TYPE (idx
)) != INTEGER_TYPE
)
2329 error ("array subscript is not an integer");
2330 return error_mark_node
;
2333 /* An array that is indexed by a non-constant
2334 cannot be stored in a register; we must be able to do
2335 address arithmetic on its address.
2336 Likewise an array of elements of variable size. */
2337 if (TREE_CODE (idx
) != INTEGER_CST
2338 || (TYPE_SIZE (TREE_TYPE (TREE_TYPE (array
))) != 0
2339 && (TREE_CODE (TYPE_SIZE (TREE_TYPE (TREE_TYPE (array
))))
2342 if (mark_addressable (array
) == 0)
2343 return error_mark_node
;
2345 /* An array that is indexed by a constant value which is not within
2346 the array bounds cannot be stored in a register either; because we
2347 would get a crash in store_bit_field/extract_bit_field when trying
2348 to access a non-existent part of the register. */
2349 if (TREE_CODE (idx
) == INTEGER_CST
2350 && TYPE_VALUES (TREE_TYPE (array
))
2351 && ! int_fits_type_p (idx
, TYPE_VALUES (TREE_TYPE (array
))))
2353 if (mark_addressable (array
) == 0)
2354 return error_mark_node
;
2357 if (pedantic
&& !lvalue_p (array
))
2358 pedwarn ("ANSI C++ forbids subscripting non-lvalue array");
2360 /* Note in C++ it is valid to subscript a `register' array, since
2361 it is valid to take the address of something with that
2362 storage specification. */
2366 while (TREE_CODE (foo
) == COMPONENT_REF
)
2367 foo
= TREE_OPERAND (foo
, 0);
2368 if (TREE_CODE (foo
) == VAR_DECL
&& DECL_REGISTER (foo
))
2369 warning ("subscripting array declared `register'");
2372 type
= TREE_TYPE (TREE_TYPE (array
));
2373 rval
= build (ARRAY_REF
, type
, array
, idx
);
2374 /* Array ref is const/volatile if the array elements are
2375 or if the array is.. */
2376 TREE_READONLY (rval
)
2377 |= (CP_TYPE_CONST_P (type
) | TREE_READONLY (array
));
2378 TREE_SIDE_EFFECTS (rval
)
2379 |= (CP_TYPE_VOLATILE_P (type
) | TREE_SIDE_EFFECTS (array
));
2380 TREE_THIS_VOLATILE (rval
)
2381 |= (CP_TYPE_VOLATILE_P (type
) | TREE_THIS_VOLATILE (array
));
2382 return require_complete_type (fold (rval
));
2386 tree ar
= default_conversion (array
);
2387 tree ind
= default_conversion (idx
);
2389 /* Put the integer in IND to simplify error checking. */
2390 if (TREE_CODE (TREE_TYPE (ar
)) == INTEGER_TYPE
)
2397 if (ar
== error_mark_node
)
2400 if (TREE_CODE (TREE_TYPE (ar
)) != POINTER_TYPE
)
2402 error ("subscripted value is neither array nor pointer");
2403 return error_mark_node
;
2405 if (TREE_CODE (TREE_TYPE (ind
)) != INTEGER_TYPE
)
2407 error ("array subscript is not an integer");
2408 return error_mark_node
;
2411 return build_indirect_ref (build_binary_op_nodefault (PLUS_EXPR
, ar
,
2417 /* Build a function call to function FUNCTION with parameters PARAMS.
2418 PARAMS is a list--a chain of TREE_LIST nodes--in which the
2419 TREE_VALUE of each node is a parameter-expression. The PARAMS do
2420 not include any object pointer that may be required. FUNCTION's
2421 data type may be a function type or a pointer-to-function.
2423 For C++: If FUNCTION's data type is a TREE_LIST, then the tree list
2424 is the list of possible methods that FUNCTION could conceivably
2425 be. If the list of methods comes from a class, then it will be
2426 a list of lists (where each element is associated with the class
2427 that produced it), otherwise it will be a simple list (for
2428 functions overloaded in global scope).
2430 In the first case, TREE_VALUE (function) is the head of one of those
2431 lists, and TREE_PURPOSE is the name of the function.
2433 In the second case, TREE_PURPOSE (function) is the function's
2436 DECL is the class instance variable, usually CURRENT_CLASS_REF.
2438 When calling a TEMPLATE_DECL, we don't require a complete return
2442 build_x_function_call (function
, params
, decl
)
2443 tree function
, params
, decl
;
2446 tree template_id
= NULL_TREE
;
2449 if (function
== error_mark_node
)
2450 return error_mark_node
;
2452 if (processing_template_decl
)
2453 return build_min_nt (CALL_EXPR
, function
, params
, NULL_TREE
);
2455 /* Save explicit template arguments if found */
2456 if (TREE_CODE (function
) == TEMPLATE_ID_EXPR
)
2458 template_id
= function
;
2459 function
= TREE_OPERAND (function
, 0);
2462 type
= TREE_TYPE (function
);
2464 if (TREE_CODE (type
) == OFFSET_TYPE
2465 && TREE_TYPE (type
) == unknown_type_node
2466 && TREE_CODE (function
) == TREE_LIST
2467 && TREE_CHAIN (function
) == NULL_TREE
)
2469 /* Undo (Foo:bar)()... */
2470 type
= TYPE_OFFSET_BASETYPE (type
);
2471 function
= TREE_VALUE (function
);
2472 my_friendly_assert (TREE_CODE (function
) == TREE_LIST
, 999);
2473 my_friendly_assert (TREE_CHAIN (function
) == NULL_TREE
, 999);
2474 function
= TREE_VALUE (function
);
2475 if (TREE_CODE (function
) == OVERLOAD
)
2476 function
= OVL_FUNCTION (function
);
2477 my_friendly_assert (TREE_CODE (function
) == FUNCTION_DECL
, 999);
2478 function
= DECL_NAME (function
);
2479 return build_method_call (decl
, function
, params
,
2480 TYPE_BINFO (type
), LOOKUP_NORMAL
);
2483 is_method
= ((TREE_CODE (function
) == TREE_LIST
2484 && current_class_type
!= NULL_TREE
2485 && (IDENTIFIER_CLASS_VALUE (TREE_PURPOSE (function
))
2487 || TREE_CODE (function
) == IDENTIFIER_NODE
2488 || TREE_CODE (type
) == METHOD_TYPE
2489 || TYPE_PTRMEMFUNC_P (type
));
2491 if ((TREE_CODE (function
) == FUNCTION_DECL
2492 && DECL_STATIC_FUNCTION_P (function
))
2493 || (TREE_CODE (function
) == TEMPLATE_DECL
2494 && DECL_STATIC_FUNCTION_P (DECL_RESULT (function
))))
2495 return build_member_call
2496 (DECL_CONTEXT (function
), DECL_NAME (function
), params
);
2498 /* A friend template. Make it look like a toplevel declaration. */
2499 if (! is_method
&& TREE_CODE (function
) == TEMPLATE_DECL
)
2500 function
= scratch_ovl_cons (function
, NULL_TREE
);
2502 /* Handle methods, friends, and overloaded functions, respectively. */
2505 tree basetype
= NULL_TREE
;
2507 if (TREE_CODE (function
) == FUNCTION_DECL
2508 || DECL_FUNCTION_TEMPLATE_P (function
))
2510 basetype
= DECL_CLASS_CONTEXT (function
);
2512 if (DECL_NAME (function
))
2513 function
= DECL_NAME (function
);
2515 function
= TYPE_IDENTIFIER (DECL_CLASS_CONTEXT (function
));
2517 else if (TREE_CODE (function
) == TREE_LIST
)
2519 my_friendly_assert (TREE_CODE (TREE_VALUE (function
))
2520 == FUNCTION_DECL
, 312);
2521 basetype
= DECL_CLASS_CONTEXT (TREE_VALUE (function
));
2522 function
= TREE_PURPOSE (function
);
2524 else if (TREE_CODE (function
) != IDENTIFIER_NODE
)
2526 if (TREE_CODE (function
) == OFFSET_REF
)
2528 if (TREE_OPERAND (function
, 0))
2529 decl
= TREE_OPERAND (function
, 0);
2531 /* Call via a pointer to member function. */
2532 if (decl
== NULL_TREE
)
2534 error ("pointer to member function called, but not in class scope");
2535 return error_mark_node
;
2537 /* What other type of POINTER_TYPE could this be? */
2538 if (TREE_CODE (TREE_TYPE (function
)) != POINTER_TYPE
2539 && ! TYPE_PTRMEMFUNC_P (TREE_TYPE (function
))
2540 && TREE_CODE (function
) != OFFSET_REF
)
2541 function
= build (OFFSET_REF
, TREE_TYPE (type
), NULL_TREE
,
2546 /* this is an abbreviated method call.
2547 must go through here in case it is a virtual function.
2548 @@ Perhaps this could be optimized. */
2550 if (basetype
&& (! current_class_type
2551 || ! DERIVED_FROM_P (basetype
, current_class_type
)))
2552 return build_member_call (basetype
, function
, params
);
2554 if (decl
== NULL_TREE
)
2556 if (current_class_type
== NULL_TREE
)
2558 error ("object missing in call to method `%s'",
2559 IDENTIFIER_POINTER (function
));
2560 return error_mark_node
;
2562 /* Yow: call from a static member function. */
2563 decl
= build_dummy_object (current_class_type
);
2566 /* Put back explicit template arguments, if any. */
2568 function
= template_id
;
2569 return build_method_call (decl
, function
, params
,
2570 NULL_TREE
, LOOKUP_NORMAL
);
2572 else if (TREE_CODE (function
) == COMPONENT_REF
2573 && type
== unknown_type_node
)
2575 /* Undo what we did in build_component_ref. */
2576 decl
= TREE_OPERAND (function
, 0);
2577 function
= TREE_OPERAND (function
, 1);
2578 function
= DECL_NAME (OVL_CURRENT (TREE_VALUE (function
)));
2579 return build_method_call (decl
, function
, params
,
2580 NULL_TREE
, LOOKUP_NORMAL
);
2582 else if (really_overloaded_fn (function
))
2584 if (OVL_FUNCTION (function
) == NULL_TREE
)
2586 cp_error ("function `%D' declared overloaded, but no definitions appear with which to resolve it?!?",
2587 TREE_PURPOSE (function
));
2588 return error_mark_node
;
2592 /* Put back explicit template arguments, if any. */
2594 function
= template_id
;
2595 return build_new_function_call (function
, params
);
2599 /* Remove a potential OVERLOAD around it */
2600 function
= OVL_CURRENT (function
);
2603 if (TREE_CODE (function
) == OFFSET_REF
)
2605 /* If the component is a data element (or a virtual function), we play
2606 games here to make things work. */
2609 if (TREE_OPERAND (function
, 0))
2610 decl
= TREE_OPERAND (function
, 0);
2612 decl
= current_class_ref
;
2614 decl_addr
= build_unary_op (ADDR_EXPR
, decl
, 0);
2616 /* Sigh. OFFSET_REFs are being used for too many things.
2617 They're being used both for -> and ->*, and we want to resolve
2618 the -> cases here, but leave the ->*. We could use
2619 resolve_offset_ref for those, too, but it would call
2620 get_member_function_from_ptrfunc and decl_addr wouldn't get
2621 updated properly. Nasty. */
2622 if (TREE_CODE (TREE_OPERAND (function
, 1)) == FIELD_DECL
)
2623 function
= resolve_offset_ref (function
);
2625 function
= TREE_OPERAND (function
, 1);
2627 function
= get_member_function_from_ptrfunc (&decl_addr
, function
);
2628 params
= expr_tree_cons (NULL_TREE
, decl_addr
, params
);
2629 return build_function_call (function
, params
);
2632 type
= TREE_TYPE (function
);
2633 if (type
!= error_mark_node
)
2635 if (TREE_CODE (type
) == REFERENCE_TYPE
)
2636 type
= TREE_TYPE (type
);
2638 if (IS_AGGR_TYPE (type
))
2639 return build_opfncall (CALL_EXPR
, LOOKUP_NORMAL
, function
, params
, NULL_TREE
);
2644 tree fntype
= TREE_TYPE (function
);
2645 tree ctypeptr
= NULL_TREE
;
2647 /* Explicitly named method? */
2648 if (TREE_CODE (function
) == FUNCTION_DECL
)
2649 ctypeptr
= build_pointer_type (DECL_CLASS_CONTEXT (function
));
2650 /* Expression with ptr-to-method type? It could either be a plain
2651 usage, or it might be a case where the ptr-to-method is being
2652 passed in as an argument. */
2653 else if (TYPE_PTRMEMFUNC_P (fntype
))
2655 tree rec
= TYPE_METHOD_BASETYPE (TREE_TYPE
2656 (TYPE_PTRMEMFUNC_FN_TYPE (fntype
)));
2657 ctypeptr
= build_pointer_type (rec
);
2659 /* Unexpected node type? */
2661 my_friendly_abort (116);
2662 if (decl
== NULL_TREE
)
2664 if (current_function_decl
2665 && DECL_STATIC_FUNCTION_P (current_function_decl
))
2666 error ("invalid call to member function needing `this' in static member function scope");
2668 error ("pointer to member function called, but not in class scope");
2669 return error_mark_node
;
2671 if (TREE_CODE (TREE_TYPE (decl
)) != POINTER_TYPE
2672 && ! TYPE_PTRMEMFUNC_P (TREE_TYPE (decl
)))
2674 decl
= build_unary_op (ADDR_EXPR
, decl
, 0);
2675 decl
= convert_pointer_to (TREE_TYPE (ctypeptr
), decl
);
2678 decl
= build_c_cast (ctypeptr
, decl
);
2679 params
= expr_tree_cons (NULL_TREE
, decl
, params
);
2682 return build_function_call (function
, params
);
2685 /* Resolve a pointer to member function. INSTANCE is the object
2686 instance to use, if the member points to a virtual member. */
2689 get_member_function_from_ptrfunc (instance_ptrptr
, function
)
2690 tree
*instance_ptrptr
;
2693 if (TREE_CODE (function
) == OFFSET_REF
)
2695 function
= TREE_OPERAND (function
, 1);
2698 if (TYPE_PTRMEMFUNC_P (TREE_TYPE (function
)))
2700 tree fntype
, idx
, e1
, delta
, delta2
, e2
, e3
, aref
, vtbl
;
2701 tree instance
, basetype
;
2703 tree instance_ptr
= *instance_ptrptr
;
2705 if (TREE_SIDE_EFFECTS (instance_ptr
))
2706 instance_ptr
= save_expr (instance_ptr
);
2708 if (TREE_SIDE_EFFECTS (function
))
2709 function
= save_expr (function
);
2711 fntype
= TYPE_PTRMEMFUNC_FN_TYPE (TREE_TYPE (function
));
2712 basetype
= TYPE_METHOD_BASETYPE (TREE_TYPE (fntype
));
2714 delta
= cp_convert (ptrdiff_type_node
,
2715 build_component_ref (function
, delta_identifier
,
2717 e3
= PFN_FROM_PTRMEMFUNC (function
);
2719 if (TYPE_SIZE (basetype
) != NULL_TREE
2720 && ! TYPE_VIRTUAL_P (basetype
))
2721 /* If basetype doesn't have virtual functions, don't emit code to
2722 handle that case. */
2726 /* Promoting idx before saving it improves performance on RISC
2727 targets. Without promoting, the first compare used
2728 load-with-sign-extend, while the second used normal load then
2729 shift to sign-extend. An optimizer flaw, perhaps, but it's
2730 easier to make this change. */
2731 idx
= save_expr (default_conversion
2732 (build_component_ref (function
,
2735 e1
= build_binary_op (GT_EXPR
, idx
, integer_zero_node
, 1);
2737 /* Convert down to the right base, before using the instance. */
2738 instance
= convert_pointer_to_real (basetype
, instance_ptr
);
2739 if (instance
== error_mark_node
&& instance_ptr
!= error_mark_node
)
2742 vtbl
= convert_pointer_to (ptr_type_node
, instance
);
2743 delta2
= DELTA2_FROM_PTRMEMFUNC (function
);
2746 build_pointer_type (build_pointer_type (vtable_entry_type
)),
2747 vtbl
, cp_convert (ptrdiff_type_node
, delta2
));
2748 vtbl
= build_indirect_ref (vtbl
, NULL_PTR
);
2749 aref
= build_array_ref (vtbl
, build_binary_op (MINUS_EXPR
,
2751 integer_one_node
, 1));
2752 if (! flag_vtable_thunks
)
2754 aref
= save_expr (aref
);
2756 delta
= build_binary_op
2758 build_conditional_expr (e1
,
2759 build_component_ref (aref
,
2766 if (flag_vtable_thunks
)
2769 e2
= build_component_ref (aref
, pfn_identifier
, NULL_TREE
, 0);
2770 TREE_TYPE (e2
) = TREE_TYPE (e3
);
2771 e1
= build_conditional_expr (e1
, e2
, e3
);
2773 /* Make sure this doesn't get evaluated first inside one of the
2774 branches of the COND_EXPR. */
2775 if (TREE_CODE (instance_ptr
) == SAVE_EXPR
)
2776 e1
= build (COMPOUND_EXPR
, TREE_TYPE (e1
),
2780 *instance_ptrptr
= build (PLUS_EXPR
, TREE_TYPE (instance_ptr
),
2781 instance_ptr
, delta
);
2783 if (instance_ptr
== error_mark_node
2784 && TREE_CODE (e1
) != ADDR_EXPR
2785 && TREE_CODE (TREE_OPERAND (e1
, 0)) != FUNCTION_DECL
)
2786 cp_error ("object missing in `%E'", function
);
2794 build_function_call_real (function
, params
, require_complete
, flags
)
2795 tree function
, params
;
2796 int require_complete
, flags
;
2798 register tree fntype
, fndecl
;
2799 register tree value_type
;
2800 register tree coerced_params
;
2801 tree name
= NULL_TREE
, assembler_name
= NULL_TREE
;
2804 /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
2805 Strip such NOP_EXPRs, since FUNCTION is used in non-lvalue context. */
2806 if (TREE_CODE (function
) == NOP_EXPR
2807 && TREE_TYPE (function
) == TREE_TYPE (TREE_OPERAND (function
, 0)))
2808 function
= TREE_OPERAND (function
, 0);
2810 if (TREE_CODE (function
) == FUNCTION_DECL
)
2812 name
= DECL_NAME (function
);
2813 assembler_name
= DECL_ASSEMBLER_NAME (function
);
2815 GNU_xref_call (current_function_decl
,
2816 IDENTIFIER_POINTER (name
? name
2817 : TYPE_IDENTIFIER (DECL_CLASS_CONTEXT
2819 mark_used (function
);
2822 /* Convert anything with function type to a pointer-to-function. */
2823 if (pedantic
&& DECL_MAIN_P (function
))
2824 pedwarn ("ANSI C++ forbids calling `main' from within program");
2826 /* Differs from default_conversion by not setting TREE_ADDRESSABLE
2827 (because calling an inline function does not mean the function
2828 needs to be separately compiled). */
2830 if (DECL_INLINE (function
))
2831 function
= inline_conversion (function
);
2833 function
= build_addr_func (function
);
2839 function
= build_addr_func (function
);
2842 if (function
== error_mark_node
)
2843 return error_mark_node
;
2845 fntype
= TREE_TYPE (function
);
2847 if (TYPE_PTRMEMFUNC_P (fntype
))
2849 cp_error ("must use .* or ->* to call pointer-to-member function in `%E (...)'",
2851 return error_mark_node
;
2854 is_method
= (TREE_CODE (fntype
) == POINTER_TYPE
2855 && TREE_CODE (TREE_TYPE (fntype
)) == METHOD_TYPE
);
2857 if (!((TREE_CODE (fntype
) == POINTER_TYPE
2858 && TREE_CODE (TREE_TYPE (fntype
)) == FUNCTION_TYPE
)
2860 || TREE_CODE (function
) == TEMPLATE_ID_EXPR
))
2862 cp_error ("`%E' cannot be used as a function", function
);
2863 return error_mark_node
;
2866 /* fntype now gets the type of function pointed to. */
2867 fntype
= TREE_TYPE (fntype
);
2869 /* Convert the parameters to the types declared in the
2870 function prototype, or apply default promotions. */
2872 if (flags
& LOOKUP_COMPLAIN
)
2873 coerced_params
= convert_arguments (TYPE_ARG_TYPES (fntype
),
2874 params
, fndecl
, LOOKUP_NORMAL
);
2876 coerced_params
= convert_arguments (TYPE_ARG_TYPES (fntype
),
2879 if (coerced_params
== error_mark_node
)
2881 if (flags
& LOOKUP_SPECULATIVELY
)
2884 return error_mark_node
;
2887 /* Check for errors in format strings. */
2889 if (warn_format
&& (name
|| assembler_name
))
2890 check_function_format (name
, assembler_name
, coerced_params
);
2892 /* Recognize certain built-in functions so we can make tree-codes
2893 other than CALL_EXPR. We do this when it enables fold-const.c
2894 to do something useful. */
2896 if (TREE_CODE (function
) == ADDR_EXPR
2897 && TREE_CODE (TREE_OPERAND (function
, 0)) == FUNCTION_DECL
2898 && DECL_BUILT_IN (TREE_OPERAND (function
, 0)))
2899 switch (DECL_FUNCTION_CODE (TREE_OPERAND (function
, 0)))
2904 if (coerced_params
== 0)
2905 return integer_zero_node
;
2906 return build_unary_op (ABS_EXPR
, TREE_VALUE (coerced_params
), 0);
2913 value_type
= TREE_TYPE (fntype
) ? TREE_TYPE (fntype
) : void_type_node
;
2915 register tree result
2916 = build_call (function
, value_type
, coerced_params
);
2918 if (require_complete
)
2920 if (value_type
== void_type_node
)
2922 result
= require_complete_type (result
);
2924 if (IS_AGGR_TYPE (value_type
))
2925 result
= build_cplus_new (value_type
, result
);
2926 return convert_from_reference (result
);
2931 build_function_call (function
, params
)
2932 tree function
, params
;
2934 return build_function_call_real (function
, params
, 1, LOOKUP_NORMAL
);
2937 /* Convert the actual parameter expressions in the list VALUES
2938 to the types in the list TYPELIST.
2939 If parmdecls is exhausted, or when an element has NULL as its type,
2940 perform the default conversions.
2942 NAME is an IDENTIFIER_NODE or 0. It is used only for error messages.
2944 This is also where warnings about wrong number of args are generated.
2946 Return a list of expressions for the parameters as converted.
2948 Both VALUES and the returned value are chains of TREE_LIST nodes
2949 with the elements of the list in the TREE_VALUE slots of those nodes.
2951 In C++, unspecified trailing parameters can be filled in with their
2952 default arguments, if such were specified. Do so here. */
2955 convert_arguments (typelist
, values
, fndecl
, flags
)
2956 tree typelist
, values
, fndecl
;
2959 register tree typetail
, valtail
;
2960 register tree result
= NULL_TREE
;
2961 char *called_thing
= 0;
2964 /* Argument passing is always copy-initialization. */
2965 flags
|= LOOKUP_ONLYCONVERTING
;
2969 if (TREE_CODE (TREE_TYPE (fndecl
)) == METHOD_TYPE
)
2971 if (DECL_NAME (fndecl
) == NULL_TREE
2972 || IDENTIFIER_HAS_TYPE_VALUE (DECL_NAME (fndecl
)))
2973 called_thing
= "constructor";
2975 called_thing
= "member function";
2978 called_thing
= "function";
2981 for (valtail
= values
, typetail
= typelist
;
2983 valtail
= TREE_CHAIN (valtail
), i
++)
2985 register tree type
= typetail
? TREE_VALUE (typetail
) : 0;
2986 register tree val
= TREE_VALUE (valtail
);
2988 if (val
== error_mark_node
)
2989 return error_mark_node
;
2991 if (type
== void_type_node
)
2995 cp_error_at ("too many arguments to %s `%+D'", called_thing
,
2997 error ("at this point in file");
3000 error ("too many arguments to function");
3001 /* In case anybody wants to know if this argument
3004 TREE_TYPE (tree_last (result
)) = error_mark_node
;
3008 if (TREE_CODE (val
) == OFFSET_REF
)
3009 val
= resolve_offset_ref (val
);
3011 /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
3012 Strip such NOP_EXPRs, since VAL is used in non-lvalue context. */
3013 if (TREE_CODE (val
) == NOP_EXPR
3014 && TREE_TYPE (val
) == TREE_TYPE (TREE_OPERAND (val
, 0))
3015 && (type
== 0 || TREE_CODE (type
) != REFERENCE_TYPE
))
3016 val
= TREE_OPERAND (val
, 0);
3018 if (type
== 0 || TREE_CODE (type
) != REFERENCE_TYPE
)
3020 if (TREE_CODE (TREE_TYPE (val
)) == ARRAY_TYPE
3021 || TREE_CODE (TREE_TYPE (val
)) == FUNCTION_TYPE
3022 || TREE_CODE (TREE_TYPE (val
)) == METHOD_TYPE
)
3023 val
= default_conversion (val
);
3025 val
= require_complete_type (val
);
3028 if (val
== error_mark_node
)
3029 return error_mark_node
;
3033 /* Formal parm type is specified by a function prototype. */
3036 if (TYPE_SIZE (complete_type (type
)) == 0)
3038 error ("parameter type of called function is incomplete");
3043 parmval
= convert_for_initialization
3044 (NULL_TREE
, type
, val
, flags
,
3045 "argument passing", fndecl
, i
);
3046 #ifdef PROMOTE_PROTOTYPES
3047 if ((TREE_CODE (type
) == INTEGER_TYPE
3048 || TREE_CODE (type
) == ENUMERAL_TYPE
)
3049 && (TYPE_PRECISION (type
)
3050 < TYPE_PRECISION (integer_type_node
)))
3051 parmval
= default_conversion (parmval
);
3055 if (parmval
== error_mark_node
)
3056 return error_mark_node
;
3058 result
= expr_tree_cons (NULL_TREE
, parmval
, result
);
3062 if (TREE_CODE (TREE_TYPE (val
)) == REFERENCE_TYPE
)
3063 val
= convert_from_reference (val
);
3065 result
= expr_tree_cons (NULL_TREE
,
3066 convert_arg_to_ellipsis (val
),
3071 typetail
= TREE_CHAIN (typetail
);
3074 if (typetail
!= 0 && typetail
!= void_list_node
)
3076 /* See if there are default arguments that can be used */
3077 if (TREE_PURPOSE (typetail
))
3079 for (; typetail
!= void_list_node
; ++i
)
3081 tree type
= TREE_VALUE (typetail
);
3082 tree val
= TREE_PURPOSE (typetail
);
3083 tree parmval
= convert_default_arg (type
, val
);
3085 if (parmval
== error_mark_node
)
3086 return error_mark_node
;
3088 result
= expr_tree_cons (0, parmval
, result
);
3089 typetail
= TREE_CHAIN (typetail
);
3090 /* ends with `...'. */
3091 if (typetail
== NULL_TREE
)
3099 char *buf
= (char *)alloca (32 + strlen (called_thing
));
3100 sprintf (buf
, "too few arguments to %s `%%#D'", called_thing
);
3101 cp_error_at (buf
, fndecl
);
3102 error ("at this point in file");
3105 error ("too few arguments to function");
3106 return error_mark_list
;
3110 return nreverse (result
);
3113 /* Build a binary-operation expression, after performing default
3114 conversions on the operands. CODE is the kind of expression to build. */
3117 build_x_binary_op (code
, arg1
, arg2
)
3118 enum tree_code code
;
3121 if (processing_template_decl
)
3122 return build_min_nt (code
, arg1
, arg2
);
3124 return build_new_op (code
, LOOKUP_NORMAL
, arg1
, arg2
, NULL_TREE
);
3128 build_binary_op (code
, arg1
, arg2
, convert_p
)
3129 enum tree_code code
;
3131 int convert_p ATTRIBUTE_UNUSED
;
3133 return build_binary_op_nodefault (code
, arg1
, arg2
, code
);
3136 /* Build a binary-operation expression without default conversions.
3137 CODE is the kind of expression to build.
3138 This function differs from `build' in several ways:
3139 the data type of the result is computed and recorded in it,
3140 warnings are generated if arg data types are invalid,
3141 special handling for addition and subtraction of pointers is known,
3142 and some optimization is done (operations on narrow ints
3143 are done in the narrower type when that gives the same result).
3144 Constant folding is also done before the result is returned.
3146 ERROR_CODE is the code that determines what to say in error messages.
3147 It is usually, but not always, the same as CODE.
3149 Note that the operands will never have enumeral types
3150 because either they have just had the default conversions performed
3151 or they have both just been converted to some other type in which
3152 the arithmetic is to be done.
3154 C++: must do special pointer arithmetic when implementing
3155 multiple inheritance, and deal with pointer to member functions. */
3158 build_binary_op_nodefault (code
, orig_op0
, orig_op1
, error_code
)
3159 enum tree_code code
;
3160 tree orig_op0
, orig_op1
;
3161 enum tree_code error_code
;
3164 register enum tree_code code0
, code1
;
3167 /* Expression code to give to the expression when it is built.
3168 Normally this is CODE, which is what the caller asked for,
3169 but in some special cases we change it. */
3170 register enum tree_code resultcode
= code
;
3172 /* Data type in which the computation is to be performed.
3173 In the simplest cases this is the common type of the arguments. */
3174 register tree result_type
= NULL
;
3176 /* Nonzero means operands have already been type-converted
3177 in whatever way is necessary.
3178 Zero means they need to be converted to RESULT_TYPE. */
3181 /* Nonzero means create the expression with this type, rather than
3183 tree build_type
= 0;
3185 /* Nonzero means after finally constructing the expression
3186 convert it to this type. */
3187 tree final_type
= 0;
3189 /* Nonzero if this is an operation like MIN or MAX which can
3190 safely be computed in short if both args are promoted shorts.
3191 Also implies COMMON.
3192 -1 indicates a bitwise operation; this makes a difference
3193 in the exact conditions for when it is safe to do the operation
3194 in a narrower mode. */
3197 /* Nonzero if this is a comparison operation;
3198 if both args are promoted shorts, compare the original shorts.
3199 Also implies COMMON. */
3200 int short_compare
= 0;
3202 /* Nonzero if this is a right-shift operation, which can be computed on the
3203 original short and then promoted if the operand is a promoted short. */
3204 int short_shift
= 0;
3206 /* Nonzero means set RESULT_TYPE to the common type of the args. */
3209 /* Apply default conversions. */
3210 if (code
== TRUTH_AND_EXPR
|| code
== TRUTH_ANDIF_EXPR
3211 || code
== TRUTH_OR_EXPR
|| code
== TRUTH_ORIF_EXPR
3212 || code
== TRUTH_XOR_EXPR
)
3214 op0
= decay_conversion (orig_op0
);
3215 op1
= decay_conversion (orig_op1
);
3219 op0
= default_conversion (orig_op0
);
3220 op1
= default_conversion (orig_op1
);
3223 type0
= TREE_TYPE (op0
);
3224 type1
= TREE_TYPE (op1
);
3226 /* The expression codes of the data types of the arguments tell us
3227 whether the arguments are integers, floating, pointers, etc. */
3228 code0
= TREE_CODE (type0
);
3229 code1
= TREE_CODE (type1
);
3231 /* Strip NON_LVALUE_EXPRs, etc., since we aren't using as an lvalue. */
3232 STRIP_TYPE_NOPS (op0
);
3233 STRIP_TYPE_NOPS (op1
);
3235 /* If an error was already reported for one of the arguments,
3236 avoid reporting another error. */
3238 if (code0
== ERROR_MARK
|| code1
== ERROR_MARK
)
3239 return error_mark_node
;
3244 /* Handle the pointer + int case. */
3245 if (code0
== POINTER_TYPE
&& code1
== INTEGER_TYPE
)
3246 return pointer_int_sum (PLUS_EXPR
, op0
, op1
);
3247 else if (code1
== POINTER_TYPE
&& code0
== INTEGER_TYPE
)
3248 return pointer_int_sum (PLUS_EXPR
, op1
, op0
);
3254 /* Subtraction of two similar pointers.
3255 We must subtract them as integers, then divide by object size. */
3256 if (code0
== POINTER_TYPE
&& code1
== POINTER_TYPE
3257 && comp_target_types (type0
, type1
, 1))
3258 return pointer_diff (op0
, op1
, common_type (type0
, type1
));
3259 /* Handle pointer minus int. Just like pointer plus int. */
3260 else if (code0
== POINTER_TYPE
&& code1
== INTEGER_TYPE
)
3261 return pointer_int_sum (MINUS_EXPR
, op0
, op1
);
3270 case TRUNC_DIV_EXPR
:
3272 case FLOOR_DIV_EXPR
:
3273 case ROUND_DIV_EXPR
:
3274 case EXACT_DIV_EXPR
:
3275 if ((code0
== INTEGER_TYPE
|| code0
== REAL_TYPE
3276 || code0
== COMPLEX_TYPE
)
3277 && (code1
== INTEGER_TYPE
|| code1
== REAL_TYPE
3278 || code1
== COMPLEX_TYPE
))
3280 if (TREE_CODE (op1
) == INTEGER_CST
&& integer_zerop (op1
))
3281 cp_warning ("division by zero in `%E / 0'", op0
);
3282 else if (TREE_CODE (op1
) == REAL_CST
&& real_zerop (op1
))
3283 cp_warning ("division by zero in `%E / 0.'", op0
);
3285 if (!(code0
== INTEGER_TYPE
&& code1
== INTEGER_TYPE
))
3286 resultcode
= RDIV_EXPR
;
3288 /* When dividing two signed integers, we have to promote to int.
3289 unless we divide by a constant != -1. Note that default
3290 conversion will have been performed on the operands at this
3291 point, so we have to dig out the original type to find out if
3293 shorten
= ((TREE_CODE (op0
) == NOP_EXPR
3294 && TREE_UNSIGNED (TREE_TYPE (TREE_OPERAND (op0
, 0))))
3295 || (TREE_CODE (op1
) == INTEGER_CST
3296 && (TREE_INT_CST_LOW (op1
) != -1
3297 || TREE_INT_CST_HIGH (op1
) != -1)));
3303 case BIT_ANDTC_EXPR
:
3306 if (code0
== INTEGER_TYPE
&& code1
== INTEGER_TYPE
)
3308 /* If one operand is a constant, and the other is a short type
3309 that has been converted to an int,
3310 really do the work in the short type and then convert the
3311 result to int. If we are lucky, the constant will be 0 or 1
3312 in the short type, making the entire operation go away. */
3313 if (TREE_CODE (op0
) == INTEGER_CST
3314 && TREE_CODE (op1
) == NOP_EXPR
3315 && (TYPE_PRECISION (type1
)
3316 > TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (op1
, 0))))
3317 && TREE_UNSIGNED (TREE_TYPE (TREE_OPERAND (op1
, 0))))
3319 final_type
= result_type
;
3320 op1
= TREE_OPERAND (op1
, 0);
3321 result_type
= TREE_TYPE (op1
);
3323 if (TREE_CODE (op1
) == INTEGER_CST
3324 && TREE_CODE (op0
) == NOP_EXPR
3325 && (TYPE_PRECISION (type0
)
3326 > TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (op0
, 0))))
3327 && TREE_UNSIGNED (TREE_TYPE (TREE_OPERAND (op0
, 0))))
3329 final_type
= result_type
;
3330 op0
= TREE_OPERAND (op0
, 0);
3331 result_type
= TREE_TYPE (op0
);
3335 case TRUNC_MOD_EXPR
:
3336 case FLOOR_MOD_EXPR
:
3337 if (code1
== INTEGER_TYPE
&& integer_zerop (op1
))
3338 cp_warning ("division by zero in `%E %% 0'", op0
);
3339 else if (code1
== REAL_TYPE
&& real_zerop (op1
))
3340 cp_warning ("division by zero in `%E %% 0.'", op0
);
3342 if (code0
== INTEGER_TYPE
&& code1
== INTEGER_TYPE
)
3344 /* Although it would be tempting to shorten always here, that loses
3345 on some targets, since the modulo instruction is undefined if the
3346 quotient can't be represented in the computation mode. We shorten
3347 only if unsigned or if dividing by something we know != -1. */
3348 shorten
= ((TREE_CODE (op0
) == NOP_EXPR
3349 && TREE_UNSIGNED (TREE_TYPE (TREE_OPERAND (op0
, 0))))
3350 || (TREE_CODE (op1
) == INTEGER_CST
3351 && (TREE_INT_CST_LOW (op1
) != -1
3352 || TREE_INT_CST_HIGH (op1
) != -1)));
3357 case TRUTH_ANDIF_EXPR
:
3358 case TRUTH_ORIF_EXPR
:
3359 case TRUTH_AND_EXPR
:
3361 result_type
= boolean_type_node
;
3364 /* Shift operations: result has same type as first operand;
3365 always convert second operand to int.
3366 Also set SHORT_SHIFT if shifting rightward. */
3369 if (code0
== INTEGER_TYPE
&& code1
== INTEGER_TYPE
)
3371 result_type
= type0
;
3372 if (TREE_CODE (op1
) == INTEGER_CST
)
3374 if (tree_int_cst_lt (op1
, integer_zero_node
))
3375 warning ("right shift count is negative");
3378 if (TREE_INT_CST_LOW (op1
) | TREE_INT_CST_HIGH (op1
))
3380 if (TREE_INT_CST_HIGH (op1
) != 0
3381 || ((unsigned HOST_WIDE_INT
) TREE_INT_CST_LOW (op1
)
3382 >= TYPE_PRECISION (type0
)))
3383 warning ("right shift count >= width of type");
3386 /* Convert the shift-count to an integer, regardless of
3387 size of value being shifted. */
3388 if (TYPE_MAIN_VARIANT (TREE_TYPE (op1
)) != integer_type_node
)
3389 op1
= cp_convert (integer_type_node
, op1
);
3390 /* Avoid converting op1 to result_type later. */
3396 if (code0
== INTEGER_TYPE
&& code1
== INTEGER_TYPE
)
3398 result_type
= type0
;
3399 if (TREE_CODE (op1
) == INTEGER_CST
)
3401 if (tree_int_cst_lt (op1
, integer_zero_node
))
3402 warning ("left shift count is negative");
3403 else if (TREE_INT_CST_HIGH (op1
) != 0
3404 || ((unsigned HOST_WIDE_INT
) TREE_INT_CST_LOW (op1
)
3405 >= TYPE_PRECISION (type0
)))
3406 warning ("left shift count >= width of type");
3408 /* Convert the shift-count to an integer, regardless of
3409 size of value being shifted. */
3410 if (TYPE_MAIN_VARIANT (TREE_TYPE (op1
)) != integer_type_node
)
3411 op1
= cp_convert (integer_type_node
, op1
);
3412 /* Avoid converting op1 to result_type later. */
3419 if (code0
== INTEGER_TYPE
&& code1
== INTEGER_TYPE
)
3421 result_type
= type0
;
3422 if (TREE_CODE (op1
) == INTEGER_CST
)
3424 if (tree_int_cst_lt (op1
, integer_zero_node
))
3425 warning ("%s rotate count is negative",
3426 (code
== LROTATE_EXPR
) ? "left" : "right");
3427 else if (TREE_INT_CST_HIGH (op1
) != 0
3428 || ((unsigned HOST_WIDE_INT
) TREE_INT_CST_LOW (op1
)
3429 >= TYPE_PRECISION (type0
)))
3430 warning ("%s rotate count >= width of type",
3431 (code
== LROTATE_EXPR
) ? "left" : "right");
3433 /* Convert the shift-count to an integer, regardless of
3434 size of value being shifted. */
3435 if (TYPE_MAIN_VARIANT (TREE_TYPE (op1
)) != integer_type_node
)
3436 op1
= cp_convert (integer_type_node
, op1
);
3442 build_type
= boolean_type_node
;
3443 if ((code0
== INTEGER_TYPE
|| code0
== REAL_TYPE
3444 || code0
== COMPLEX_TYPE
)
3445 && (code1
== INTEGER_TYPE
|| code1
== REAL_TYPE
3446 || code1
== COMPLEX_TYPE
))
3448 else if (code0
== POINTER_TYPE
&& code1
== POINTER_TYPE
)
3450 register tree tt0
= TYPE_MAIN_VARIANT (TREE_TYPE (type0
));
3451 register tree tt1
= TYPE_MAIN_VARIANT (TREE_TYPE (type1
));
3453 if (comp_target_types (type0
, type1
, 1))
3454 result_type
= common_type (type0
, type1
);
3455 else if (tt0
== void_type_node
)
3457 if (pedantic
&& TREE_CODE (tt1
) == FUNCTION_TYPE
3458 && tree_int_cst_lt (TYPE_SIZE (type0
), TYPE_SIZE (type1
)))
3459 pedwarn ("ANSI C++ forbids comparison of `void *' with function pointer");
3460 else if (TREE_CODE (tt1
) == OFFSET_TYPE
)
3461 pedwarn ("ANSI C++ forbids conversion of a pointer to member to `void *'");
3463 else if (tt1
== void_type_node
)
3465 if (pedantic
&& TREE_CODE (tt0
) == FUNCTION_TYPE
3466 && tree_int_cst_lt (TYPE_SIZE (type1
), TYPE_SIZE (type0
)))
3467 pedwarn ("ANSI C++ forbids comparison of `void *' with function pointer");
3470 cp_pedwarn ("comparison of distinct pointer types `%T' and `%T' lacks a cast",
3473 if (result_type
== NULL_TREE
)
3474 result_type
= ptr_type_node
;
3476 else if (code0
== POINTER_TYPE
&& TREE_CODE (op1
) == INTEGER_CST
3477 && integer_zerop (op1
))
3478 result_type
= type0
;
3479 else if (code1
== POINTER_TYPE
&& TREE_CODE (op0
) == INTEGER_CST
3480 && integer_zerop (op0
))
3481 result_type
= type1
;
3482 else if (code0
== POINTER_TYPE
&& code1
== INTEGER_TYPE
)
3484 result_type
= type0
;
3485 error ("ANSI C++ forbids comparison between pointer and integer");
3487 else if (code0
== INTEGER_TYPE
&& code1
== POINTER_TYPE
)
3489 result_type
= type1
;
3490 error ("ANSI C++ forbids comparison between pointer and integer");
3492 else if (TYPE_PTRMEMFUNC_P (type0
) && TREE_CODE (op1
) == INTEGER_CST
3493 && integer_zerop (op1
))
3495 op0
= build_component_ref (op0
, index_identifier
, NULL_TREE
, 0);
3496 op1
= integer_zero_node
;
3497 result_type
= TREE_TYPE (op0
);
3499 else if (TYPE_PTRMEMFUNC_P (type1
) && TREE_CODE (op0
) == INTEGER_CST
3500 && integer_zerop (op0
))
3502 op0
= build_component_ref (op1
, index_identifier
, NULL_TREE
, 0);
3503 op1
= integer_zero_node
;
3504 result_type
= TREE_TYPE (op0
);
3506 else if (TYPE_PTRMEMFUNC_P (type0
) && TYPE_PTRMEMFUNC_P (type1
)
3507 && (TYPE_PTRMEMFUNC_FN_TYPE (type0
)
3508 == TYPE_PTRMEMFUNC_FN_TYPE (type1
)))
3510 /* The code we generate for the test is:
3512 (op0.index == op1.index
3513 && ((op1.index != -1 && op0.delta2 == op1.delta2)
3514 || op0.pfn == op1.pfn)) */
3516 tree index0
= build_component_ref (op0
, index_identifier
,
3518 tree index1
= save_expr (build_component_ref (op1
, index_identifier
,
3520 tree pfn0
= PFN_FROM_PTRMEMFUNC (op0
);
3521 tree pfn1
= PFN_FROM_PTRMEMFUNC (op1
);
3522 tree delta20
= DELTA2_FROM_PTRMEMFUNC (op0
);
3523 tree delta21
= DELTA2_FROM_PTRMEMFUNC (op1
);
3525 tree integer_neg_one_node
3526 = build_binary_op (MINUS_EXPR
, integer_zero_node
,
3527 integer_one_node
, 1);
3528 e1
= build_binary_op (EQ_EXPR
, index0
, index1
, 1);
3529 e2
= build_binary_op (NE_EXPR
, index1
, integer_neg_one_node
, 1);
3530 e2
= build_binary_op (TRUTH_ANDIF_EXPR
, e2
,
3531 build_binary_op (EQ_EXPR
, delta20
, delta21
, 1),
3533 e3
= build_binary_op (EQ_EXPR
, pfn0
, pfn1
, 1);
3534 e2
= build_binary_op (TRUTH_ORIF_EXPR
, e2
, e3
, 1);
3535 e2
= build_binary_op (TRUTH_ANDIF_EXPR
, e1
, e2
, 1);
3536 if (code
== EQ_EXPR
)
3538 return build_binary_op (EQ_EXPR
, e2
, integer_zero_node
, 1);
3540 else if (TYPE_PTRMEMFUNC_P (type0
)
3541 && TYPE_PTRMEMFUNC_FN_TYPE (type0
) == type1
)
3543 tree index0
= build_component_ref (op0
, index_identifier
,
3546 tree pfn0
= PFN_FROM_PTRMEMFUNC (op0
);
3547 tree delta20
= DELTA2_FROM_PTRMEMFUNC (op0
);
3548 tree delta21
= integer_zero_node
;
3550 tree integer_neg_one_node
3551 = build_binary_op (MINUS_EXPR
, integer_zero_node
, integer_one_node
, 1);
3552 if (TREE_CODE (TREE_OPERAND (op1
, 0)) == FUNCTION_DECL
3553 && DECL_VINDEX (TREE_OPERAND (op1
, 0)))
3555 /* Map everything down one to make room for
3556 the null pointer to member. */
3557 index1
= size_binop (PLUS_EXPR
,
3558 DECL_VINDEX (TREE_OPERAND (op1
, 0)),
3560 op1
= integer_zero_node
;
3561 delta21
= CLASSTYPE_VFIELD (TYPE_METHOD_BASETYPE
3562 (TREE_TYPE (type1
)));
3563 delta21
= DECL_FIELD_BITPOS (delta21
);
3564 delta21
= size_binop (FLOOR_DIV_EXPR
, delta21
,
3565 size_int (BITS_PER_UNIT
));
3566 delta21
= convert (sizetype
, delta21
);
3569 index1
= integer_neg_one_node
;
3571 tree nop1
= build1 (NOP_EXPR
, TYPE_PTRMEMFUNC_FN_TYPE (type0
),
3573 TREE_CONSTANT (nop1
) = TREE_CONSTANT (op1
);
3576 e1
= build_binary_op (EQ_EXPR
, index0
, index1
, 1);
3577 e2
= build_binary_op (NE_EXPR
, index1
, integer_neg_one_node
, 1);
3578 e2
= build_binary_op (TRUTH_ANDIF_EXPR
, e2
,
3579 build_binary_op (EQ_EXPR
, delta20
, delta21
, 1),
3581 e3
= build_binary_op (EQ_EXPR
, pfn0
, op1
, 1);
3582 e2
= build_binary_op (TRUTH_ORIF_EXPR
, e2
, e3
, 1);
3583 e2
= build_binary_op (TRUTH_ANDIF_EXPR
, e1
, e2
, 1);
3584 if (code
== EQ_EXPR
)
3586 return build_binary_op (EQ_EXPR
, e2
, integer_zero_node
, 1);
3588 else if (TYPE_PTRMEMFUNC_P (type1
)
3589 && TYPE_PTRMEMFUNC_FN_TYPE (type1
) == type0
)
3591 return build_binary_op (code
, op1
, op0
, 1);
3597 if ((code0
== INTEGER_TYPE
|| code0
== REAL_TYPE
)
3598 && (code1
== INTEGER_TYPE
|| code1
== REAL_TYPE
))
3600 else if (code0
== POINTER_TYPE
&& code1
== POINTER_TYPE
)
3602 if (comp_target_types (type0
, type1
, 1))
3603 result_type
= common_type (type0
, type1
);
3606 cp_pedwarn ("comparison of distinct pointer types `%T' and `%T' lacks a cast",
3608 result_type
= ptr_type_node
;
3617 build_type
= boolean_type_node
;
3618 if ((code0
== INTEGER_TYPE
|| code0
== REAL_TYPE
)
3619 && (code1
== INTEGER_TYPE
|| code1
== REAL_TYPE
))
3621 else if (code0
== POINTER_TYPE
&& code1
== POINTER_TYPE
)
3623 if (comp_target_types (type0
, type1
, 1))
3624 result_type
= common_type (type0
, type1
);
3627 cp_pedwarn ("comparison of distinct pointer types `%T' and `%T' lacks a cast",
3629 result_type
= ptr_type_node
;
3632 else if (code0
== POINTER_TYPE
&& TREE_CODE (op1
) == INTEGER_CST
3633 && integer_zerop (op1
))
3634 result_type
= type0
;
3635 else if (code1
== POINTER_TYPE
&& TREE_CODE (op0
) == INTEGER_CST
3636 && integer_zerop (op0
))
3637 result_type
= type1
;
3638 else if (code0
== POINTER_TYPE
&& code1
== INTEGER_TYPE
)
3640 result_type
= type0
;
3641 pedwarn ("ANSI C++ forbids comparison between pointer and integer");
3643 else if (code0
== INTEGER_TYPE
&& code1
== POINTER_TYPE
)
3645 result_type
= type1
;
3646 pedwarn ("ANSI C++ forbids comparison between pointer and integer");
3654 if ((code0
== INTEGER_TYPE
|| code0
== REAL_TYPE
|| code0
== COMPLEX_TYPE
)
3656 (code1
== INTEGER_TYPE
|| code1
== REAL_TYPE
|| code1
== COMPLEX_TYPE
))
3658 int none_complex
= (code0
!= COMPLEX_TYPE
&& code1
!= COMPLEX_TYPE
);
3660 if (shorten
|| common
|| short_compare
)
3661 result_type
= common_type (type0
, type1
);
3663 /* For certain operations (which identify themselves by shorten != 0)
3664 if both args were extended from the same smaller type,
3665 do the arithmetic in that type and then extend.
3667 shorten !=0 and !=1 indicates a bitwise operation.
3668 For them, this optimization is safe only if
3669 both args are zero-extended or both are sign-extended.
3670 Otherwise, we might change the result.
3671 Eg, (short)-1 | (unsigned short)-1 is (int)-1
3672 but calculated in (unsigned short) it would be (unsigned short)-1. */
3674 if (shorten
&& none_complex
)
3676 int unsigned0
, unsigned1
;
3677 tree arg0
= get_narrower (op0
, &unsigned0
);
3678 tree arg1
= get_narrower (op1
, &unsigned1
);
3679 /* UNS is 1 if the operation to be done is an unsigned one. */
3680 int uns
= TREE_UNSIGNED (result_type
);
3683 final_type
= result_type
;
3685 /* Handle the case that OP0 does not *contain* a conversion
3686 but it *requires* conversion to FINAL_TYPE. */
3688 if (op0
== arg0
&& TREE_TYPE (op0
) != final_type
)
3689 unsigned0
= TREE_UNSIGNED (TREE_TYPE (op0
));
3690 if (op1
== arg1
&& TREE_TYPE (op1
) != final_type
)
3691 unsigned1
= TREE_UNSIGNED (TREE_TYPE (op1
));
3693 /* Now UNSIGNED0 is 1 if ARG0 zero-extends to FINAL_TYPE. */
3695 /* For bitwise operations, signedness of nominal type
3696 does not matter. Consider only how operands were extended. */
3700 /* Note that in all three cases below we refrain from optimizing
3701 an unsigned operation on sign-extended args.
3702 That would not be valid. */
3704 /* Both args variable: if both extended in same way
3705 from same width, do it in that width.
3706 Do it unsigned if args were zero-extended. */
3707 if ((TYPE_PRECISION (TREE_TYPE (arg0
))
3708 < TYPE_PRECISION (result_type
))
3709 && (TYPE_PRECISION (TREE_TYPE (arg1
))
3710 == TYPE_PRECISION (TREE_TYPE (arg0
)))
3711 && unsigned0
== unsigned1
3712 && (unsigned0
|| !uns
))
3714 = signed_or_unsigned_type (unsigned0
,
3715 common_type (TREE_TYPE (arg0
),
3717 else if (TREE_CODE (arg0
) == INTEGER_CST
3718 && (unsigned1
|| !uns
)
3719 && (TYPE_PRECISION (TREE_TYPE (arg1
))
3720 < TYPE_PRECISION (result_type
))
3721 && (type
= signed_or_unsigned_type (unsigned1
,
3723 int_fits_type_p (arg0
, type
)))
3725 else if (TREE_CODE (arg1
) == INTEGER_CST
3726 && (unsigned0
|| !uns
)
3727 && (TYPE_PRECISION (TREE_TYPE (arg0
))
3728 < TYPE_PRECISION (result_type
))
3729 && (type
= signed_or_unsigned_type (unsigned0
,
3731 int_fits_type_p (arg1
, type
)))
3735 /* Shifts can be shortened if shifting right. */
3740 tree arg0
= get_narrower (op0
, &unsigned_arg
);
3742 final_type
= result_type
;
3744 if (arg0
== op0
&& final_type
== TREE_TYPE (op0
))
3745 unsigned_arg
= TREE_UNSIGNED (TREE_TYPE (op0
));
3747 if (TYPE_PRECISION (TREE_TYPE (arg0
)) < TYPE_PRECISION (result_type
)
3748 /* We can shorten only if the shift count is less than the
3749 number of bits in the smaller type size. */
3750 && TREE_INT_CST_HIGH (op1
) == 0
3751 && TYPE_PRECISION (TREE_TYPE (arg0
)) > TREE_INT_CST_LOW (op1
)
3752 /* If arg is sign-extended and then unsigned-shifted,
3753 we can simulate this with a signed shift in arg's type
3754 only if the extended result is at least twice as wide
3755 as the arg. Otherwise, the shift could use up all the
3756 ones made by sign-extension and bring in zeros.
3757 We can't optimize that case at all, but in most machines
3758 it never happens because available widths are 2**N. */
3759 && (!TREE_UNSIGNED (final_type
)
3761 || (((unsigned) 2 * TYPE_PRECISION (TREE_TYPE (arg0
)))
3762 <= TYPE_PRECISION (result_type
))))
3764 /* Do an unsigned shift if the operand was zero-extended. */
3766 = signed_or_unsigned_type (unsigned_arg
,
3768 /* Convert value-to-be-shifted to that type. */
3769 if (TREE_TYPE (op0
) != result_type
)
3770 op0
= cp_convert (result_type
, op0
);
3775 /* Comparison operations are shortened too but differently.
3776 They identify themselves by setting short_compare = 1. */
3780 /* Don't write &op0, etc., because that would prevent op0
3781 from being kept in a register.
3782 Instead, make copies of the our local variables and
3783 pass the copies by reference, then copy them back afterward. */
3784 tree xop0
= op0
, xop1
= op1
, xresult_type
= result_type
;
3785 enum tree_code xresultcode
= resultcode
;
3787 = shorten_compare (&xop0
, &xop1
, &xresult_type
, &xresultcode
);
3789 return cp_convert (boolean_type_node
, val
);
3790 op0
= xop0
, op1
= xop1
;
3792 resultcode
= xresultcode
;
3795 if (short_compare
&& warn_sign_compare
)
3797 int op0_signed
= ! TREE_UNSIGNED (TREE_TYPE (orig_op0
));
3798 int op1_signed
= ! TREE_UNSIGNED (TREE_TYPE (orig_op1
));
3800 int unsignedp0
, unsignedp1
;
3801 tree primop0
= get_narrower (op0
, &unsignedp0
);
3802 tree primop1
= get_narrower (op1
, &unsignedp1
);
3804 /* Check for comparison of different enum types. */
3805 if (TREE_CODE (TREE_TYPE (orig_op0
)) == ENUMERAL_TYPE
3806 && TREE_CODE (TREE_TYPE (orig_op1
)) == ENUMERAL_TYPE
3807 && TYPE_MAIN_VARIANT (TREE_TYPE (orig_op0
))
3808 != TYPE_MAIN_VARIANT (TREE_TYPE (orig_op1
)))
3810 cp_warning ("comparison between `%#T' and `%#T'",
3811 TREE_TYPE (orig_op0
), TREE_TYPE (orig_op1
));
3814 /* Give warnings for comparisons between signed and unsigned
3815 quantities that may fail. */
3816 /* Do the checking based on the original operand trees, so that
3817 casts will be considered, but default promotions won't be. */
3819 /* Do not warn if the comparison is being done in a signed type,
3820 since the signed type will only be chosen if it can represent
3821 all the values of the unsigned type. */
3822 if (! TREE_UNSIGNED (result_type
))
3824 /* Do not warn if both operands are unsigned. */
3825 else if (op0_signed
== op1_signed
)
3827 /* Do not warn if the signed quantity is an unsuffixed
3828 integer literal (or some static constant expression
3829 involving such literals) and it is non-negative. */
3830 else if ((op0_signed
&& TREE_CODE (orig_op0
) == INTEGER_CST
3831 && tree_int_cst_sgn (orig_op0
) >= 0)
3832 || (op1_signed
&& TREE_CODE (orig_op1
) == INTEGER_CST
3833 && tree_int_cst_sgn (orig_op1
) >= 0))
3835 /* Do not warn if the comparison is an equality operation,
3836 the unsigned quantity is an integral constant and it does
3837 not use the most significant bit of result_type. */
3838 else if ((resultcode
== EQ_EXPR
|| resultcode
== NE_EXPR
)
3839 && ((op0_signed
&& TREE_CODE (orig_op1
) == INTEGER_CST
3840 && int_fits_type_p (orig_op1
,
3841 signed_type (result_type
)))
3842 || (op1_signed
&& TREE_CODE (orig_op0
) == INTEGER_CST
3843 && int_fits_type_p (orig_op0
,
3844 signed_type (result_type
)))))
3847 warning ("comparison between signed and unsigned");
3849 /* Warn if two unsigned values are being compared in a size
3850 larger than their original size, and one (and only one) is the
3851 result of a `~' operator. This comparison will always fail.
3853 Also warn if one operand is a constant, and the constant does not
3854 have all bits set that are set in the ~ operand when it is
3857 if ((TREE_CODE (primop0
) == BIT_NOT_EXPR
)
3858 ^ (TREE_CODE (primop1
) == BIT_NOT_EXPR
))
3860 if (TREE_CODE (primop0
) == BIT_NOT_EXPR
)
3861 primop0
= get_narrower (TREE_OPERAND (op0
, 0), &unsignedp0
);
3862 if (TREE_CODE (primop1
) == BIT_NOT_EXPR
)
3863 primop1
= get_narrower (TREE_OPERAND (op1
, 0), &unsignedp1
);
3865 if (TREE_CODE (primop0
) == INTEGER_CST
3866 || TREE_CODE (primop1
) == INTEGER_CST
)
3869 HOST_WIDE_INT constant
, mask
;
3873 if (TREE_CODE (primop0
) == INTEGER_CST
)
3876 unsignedp
= unsignedp1
;
3877 constant
= TREE_INT_CST_LOW (primop0
);
3882 unsignedp
= unsignedp0
;
3883 constant
= TREE_INT_CST_LOW (primop1
);
3886 bits
= TYPE_PRECISION (TREE_TYPE (primop
));
3887 if (bits
< TYPE_PRECISION (result_type
)
3888 && bits
< HOST_BITS_PER_LONG
&& unsignedp
)
3890 mask
= (~ (HOST_WIDE_INT
) 0) << bits
;
3891 if ((mask
& constant
) != mask
)
3892 warning ("comparison of promoted ~unsigned with constant");
3895 else if (unsignedp0
&& unsignedp1
3896 && (TYPE_PRECISION (TREE_TYPE (primop0
))
3897 < TYPE_PRECISION (result_type
))
3898 && (TYPE_PRECISION (TREE_TYPE (primop1
))
3899 < TYPE_PRECISION (result_type
)))
3900 warning ("comparison of promoted ~unsigned with unsigned");
3905 /* At this point, RESULT_TYPE must be nonzero to avoid an error message.
3906 If CONVERTED is zero, both args will be converted to type RESULT_TYPE.
3907 Then the expression will be built.
3908 It will be given type FINAL_TYPE if that is nonzero;
3909 otherwise, it will be given type RESULT_TYPE. */
3913 cp_error ("invalid operands `%T' and `%T' to binary `%O'",
3914 TREE_TYPE (orig_op0
), TREE_TYPE (orig_op1
), error_code
);
3915 return error_mark_node
;
3918 /* Issue warnings about peculiar, but legal, uses of NULL. */
3919 if (/* It's reasonable to use pointer values as operands of &&
3920 and ||, so NULL is no exception. */
3921 !(code
== TRUTH_ANDIF_EXPR
|| code
== TRUTH_ORIF_EXPR
)
3922 && (/* If OP0 is NULL and OP1 is not a pointer, or vice versa. */
3923 (orig_op0
== null_node
3924 && TREE_CODE (TREE_TYPE (op1
)) != POINTER_TYPE
)
3925 /* Or vice versa. */
3926 || (orig_op1
== null_node
3927 && TREE_CODE (TREE_TYPE (op0
)) != POINTER_TYPE
)
3928 /* Or, both are NULL and the operation was not a comparison. */
3929 || (orig_op0
== null_node
&& orig_op1
== null_node
3930 && code
!= EQ_EXPR
&& code
!= NE_EXPR
)))
3931 /* Some sort of arithmetic operation involving NULL was
3932 performed. Note that pointer-difference and pointer-addition
3933 have already been handled above, and so we don't end up here in
3935 cp_warning ("NULL used in arithmetic");
3939 if (TREE_TYPE (op0
) != result_type
)
3940 op0
= cp_convert (result_type
, op0
);
3941 if (TREE_TYPE (op1
) != result_type
)
3942 op1
= cp_convert (result_type
, op1
);
3944 if (op0
== error_mark_node
|| op1
== error_mark_node
)
3945 return error_mark_node
;
3948 if (build_type
== NULL_TREE
)
3949 build_type
= result_type
;
3952 register tree result
= build (resultcode
, build_type
, op0
, op1
);
3953 register tree folded
;
3955 folded
= fold (result
);
3956 if (folded
== result
)
3957 TREE_CONSTANT (folded
) = TREE_CONSTANT (op0
) & TREE_CONSTANT (op1
);
3958 if (final_type
!= 0)
3959 return cp_convert (final_type
, folded
);
3964 /* Return a tree for the sum or difference (RESULTCODE says which)
3965 of pointer PTROP and integer INTOP. */
3968 pointer_int_sum (resultcode
, ptrop
, intop
)
3969 enum tree_code resultcode
;
3970 register tree ptrop
, intop
;
3974 register tree result
;
3975 register tree folded
= fold (intop
);
3977 /* The result is a pointer of the same type that is being added. */
3979 register tree result_type
= TREE_TYPE (ptrop
);
3981 if (!complete_type_or_else (result_type
))
3982 return error_mark_node
;
3984 if (TREE_CODE (TREE_TYPE (result_type
)) == VOID_TYPE
)
3986 if (pedantic
|| warn_pointer_arith
)
3987 pedwarn ("ANSI C++ forbids using pointer of type `void *' in arithmetic");
3988 size_exp
= integer_one_node
;
3990 else if (TREE_CODE (TREE_TYPE (result_type
)) == FUNCTION_TYPE
)
3992 if (pedantic
|| warn_pointer_arith
)
3993 pedwarn ("ANSI C++ forbids using pointer to a function in arithmetic");
3994 size_exp
= integer_one_node
;
3996 else if (TREE_CODE (TREE_TYPE (result_type
)) == METHOD_TYPE
)
3998 if (pedantic
|| warn_pointer_arith
)
3999 pedwarn ("ANSI C++ forbids using pointer to a method in arithmetic");
4000 size_exp
= integer_one_node
;
4002 else if (TREE_CODE (TREE_TYPE (result_type
)) == OFFSET_TYPE
)
4004 if (pedantic
|| warn_pointer_arith
)
4005 pedwarn ("ANSI C++ forbids using pointer to a member in arithmetic");
4006 size_exp
= integer_one_node
;
4009 size_exp
= size_in_bytes (complete_type (TREE_TYPE (result_type
)));
4011 /* Needed to make OOPS V2R3 work. */
4013 if (TREE_CODE (intop
) == INTEGER_CST
4014 && TREE_INT_CST_LOW (intop
) == 0
4015 && TREE_INT_CST_HIGH (intop
) == 0)
4018 /* If what we are about to multiply by the size of the elements
4019 contains a constant term, apply distributive law
4020 and multiply that constant term separately.
4021 This helps produce common subexpressions. */
4023 if ((TREE_CODE (intop
) == PLUS_EXPR
|| TREE_CODE (intop
) == MINUS_EXPR
)
4024 && ! TREE_CONSTANT (intop
)
4025 && TREE_CONSTANT (TREE_OPERAND (intop
, 1))
4026 && TREE_CONSTANT (size_exp
))
4028 enum tree_code subcode
= resultcode
;
4029 if (TREE_CODE (intop
) == MINUS_EXPR
)
4030 subcode
= (subcode
== PLUS_EXPR
? MINUS_EXPR
: PLUS_EXPR
);
4031 ptrop
= build_binary_op (subcode
, ptrop
, TREE_OPERAND (intop
, 1), 1);
4032 intop
= TREE_OPERAND (intop
, 0);
4035 /* Convert the integer argument to a type the same size as sizetype
4036 so the multiply won't overflow spuriously. */
4038 if (TYPE_PRECISION (TREE_TYPE (intop
)) != TYPE_PRECISION (sizetype
))
4039 intop
= cp_convert (type_for_size (TYPE_PRECISION (sizetype
), 0), intop
);
4041 /* Replace the integer argument with a suitable product by the object size.
4042 Do this multiplication as signed, then convert to the appropriate
4043 pointer type (actually unsigned integral). */
4045 intop
= cp_convert (result_type
,
4046 build_binary_op (MULT_EXPR
, intop
,
4047 cp_convert (TREE_TYPE (intop
),
4051 /* Create the sum or difference. */
4053 result
= build (resultcode
, result_type
, ptrop
, intop
);
4055 folded
= fold (result
);
4056 if (folded
== result
)
4057 TREE_CONSTANT (folded
) = TREE_CONSTANT (ptrop
) & TREE_CONSTANT (intop
);
4061 /* Return a tree for the difference of pointers OP0 and OP1.
4062 The resulting tree has type int. */
4065 pointer_diff (op0
, op1
, ptrtype
)
4066 register tree op0
, op1
;
4067 register tree ptrtype
;
4069 register tree result
, folded
;
4070 tree restype
= ptrdiff_type_node
;
4071 tree target_type
= TREE_TYPE (ptrtype
);
4073 if (!complete_type_or_else (target_type
))
4074 return error_mark_node
;
4076 if (pedantic
|| warn_pointer_arith
)
4078 if (TREE_CODE (target_type
) == VOID_TYPE
)
4079 pedwarn ("ANSI C++ forbids using pointer of type `void *' in subtraction");
4080 if (TREE_CODE (target_type
) == FUNCTION_TYPE
)
4081 pedwarn ("ANSI C++ forbids using pointer to a function in subtraction");
4082 if (TREE_CODE (target_type
) == METHOD_TYPE
)
4083 pedwarn ("ANSI C++ forbids using pointer to a method in subtraction");
4084 if (TREE_CODE (target_type
) == OFFSET_TYPE
)
4085 pedwarn ("ANSI C++ forbids using pointer to a member in subtraction");
4088 /* First do the subtraction as integers;
4089 then drop through to build the divide operator. */
4091 op0
= build_binary_op (MINUS_EXPR
, cp_convert (restype
, op0
),
4092 cp_convert (restype
, op1
), 1);
4094 /* This generates an error if op1 is a pointer to an incomplete type. */
4095 if (TYPE_SIZE (TREE_TYPE (TREE_TYPE (op1
))) == 0)
4096 error ("arithmetic on pointer to an incomplete type");
4098 op1
= ((TREE_CODE (target_type
) == VOID_TYPE
4099 || TREE_CODE (target_type
) == FUNCTION_TYPE
4100 || TREE_CODE (target_type
) == METHOD_TYPE
4101 || TREE_CODE (target_type
) == OFFSET_TYPE
)
4103 : size_in_bytes (target_type
));
4105 /* Do the division. */
4107 result
= build (EXACT_DIV_EXPR
, restype
, op0
, cp_convert (restype
, op1
));
4109 folded
= fold (result
);
4110 if (folded
== result
)
4111 TREE_CONSTANT (folded
) = TREE_CONSTANT (op0
) & TREE_CONSTANT (op1
);
4115 /* Handle the case of taking the address of a COMPONENT_REF.
4116 Called by `build_unary_op' and `build_up_reference'.
4118 ARG is the COMPONENT_REF whose address we want.
4119 ARGTYPE is the pointer type that this address should have.
4120 MSG is an error message to print if this COMPONENT_REF is not
4121 addressable (such as a bitfield). */
4124 build_component_addr (arg
, argtype
, msg
)
4128 tree field
= TREE_OPERAND (arg
, 1);
4129 tree basetype
= decl_type_context (field
);
4130 tree rval
= build_unary_op (ADDR_EXPR
, TREE_OPERAND (arg
, 0), 0);
4132 my_friendly_assert (TREE_CODE (field
) == FIELD_DECL
, 981018);
4134 if (DECL_C_BIT_FIELD (field
))
4136 error (msg
, IDENTIFIER_POINTER (DECL_NAME (field
)));
4137 return error_mark_node
;
4140 if (TREE_CODE (field
) == FIELD_DECL
4141 && TYPE_USES_COMPLEX_INHERITANCE (basetype
))
4143 /* Can't convert directly to ARGTYPE, since that
4144 may have the same pointer type as one of our
4146 rval
= build1 (NOP_EXPR
, argtype
,
4147 convert_pointer_to (basetype
, rval
));
4148 TREE_CONSTANT (rval
) = TREE_CONSTANT (TREE_OPERAND (rval
, 0));
4151 /* This conversion is harmless. */
4152 rval
= convert_force (argtype
, rval
, 0);
4154 if (! integer_zerop (DECL_FIELD_BITPOS (field
)))
4156 tree offset
= size_binop (EASY_DIV_EXPR
, DECL_FIELD_BITPOS (field
),
4157 size_int (BITS_PER_UNIT
));
4158 int flag
= TREE_CONSTANT (rval
);
4159 offset
= convert (sizetype
, offset
);
4160 rval
= fold (build (PLUS_EXPR
, argtype
,
4161 rval
, cp_convert (argtype
, offset
)));
4162 TREE_CONSTANT (rval
) = flag
;
4167 /* Construct and perhaps optimize a tree representation
4168 for a unary operation. CODE, a tree_code, specifies the operation
4169 and XARG is the operand. */
4172 build_x_unary_op (code
, xarg
)
4173 enum tree_code code
;
4176 if (processing_template_decl
)
4177 return build_min_nt (code
, xarg
, NULL_TREE
);
4179 /* & rec, on incomplete RECORD_TYPEs is the simple opr &, not an
4181 if (code
== ADDR_EXPR
4182 && TREE_CODE (xarg
) != TEMPLATE_ID_EXPR
4183 && ((IS_AGGR_TYPE_CODE (TREE_CODE (TREE_TYPE (xarg
)))
4184 && TYPE_SIZE (TREE_TYPE (xarg
)) == NULL_TREE
)
4185 || (TREE_CODE (xarg
) == OFFSET_REF
)))
4186 /* don't look for a function */;
4191 rval
= build_new_op (code
, LOOKUP_NORMAL
, xarg
,
4192 NULL_TREE
, NULL_TREE
);
4193 if (rval
|| code
!= ADDR_EXPR
)
4197 if (code
== ADDR_EXPR
)
4199 if (TREE_CODE (xarg
) == TARGET_EXPR
)
4200 warning ("taking address of temporary");
4203 return build_unary_op (code
, xarg
, 0);
4206 /* Just like truthvalue_conversion, but we want a CLEANUP_POINT_EXPR. */
4209 condition_conversion (expr
)
4213 if (processing_template_decl
)
4215 t
= cp_convert (boolean_type_node
, expr
);
4216 t
= fold (build1 (CLEANUP_POINT_EXPR
, boolean_type_node
, t
));
4220 /* C++: Must handle pointers to members.
4222 Perhaps type instantiation should be extended to handle conversion
4223 from aggregates to types we don't yet know we want? (Or are those
4224 cases typically errors which should be reported?)
4226 NOCONVERT nonzero suppresses the default promotions
4227 (such as from short to int). */
4230 build_unary_op (code
, xarg
, noconvert
)
4231 enum tree_code code
;
4235 /* No default_conversion here. It causes trouble for ADDR_EXPR. */
4236 register tree arg
= xarg
;
4237 register tree argtype
= 0;
4238 char *errstring
= NULL
;
4241 if (arg
== error_mark_node
)
4242 return error_mark_node
;
4247 /* This is used for unary plus, because a CONVERT_EXPR
4248 is enough to prevent anybody from looking inside for
4249 associativity, but won't generate any code. */
4250 if (!(arg
= build_expr_type_conversion
4251 (WANT_ARITH
| WANT_ENUM
| WANT_POINTER
, arg
, 1)))
4252 errstring
= "wrong type argument to unary plus";
4256 arg
= default_conversion (arg
);
4257 arg
= build1 (NON_LVALUE_EXPR
, TREE_TYPE (arg
), arg
);
4258 TREE_CONSTANT (arg
) = TREE_CONSTANT (TREE_OPERAND (arg
, 0));
4263 if (!(arg
= build_expr_type_conversion (WANT_ARITH
| WANT_ENUM
, arg
, 1)))
4264 errstring
= "wrong type argument to unary minus";
4265 else if (!noconvert
)
4266 arg
= default_conversion (arg
);
4270 if (TREE_CODE (TREE_TYPE (arg
)) == COMPLEX_TYPE
)
4274 arg
= default_conversion (arg
);
4276 else if (!(arg
= build_expr_type_conversion (WANT_INT
| WANT_ENUM
,
4278 errstring
= "wrong type argument to bit-complement";
4279 else if (!noconvert
)
4280 arg
= default_conversion (arg
);
4284 if (!(arg
= build_expr_type_conversion (WANT_ARITH
| WANT_ENUM
, arg
, 1)))
4285 errstring
= "wrong type argument to abs";
4286 else if (!noconvert
)
4287 arg
= default_conversion (arg
);
4291 /* Conjugating a real value is a no-op, but allow it anyway. */
4292 if (!(arg
= build_expr_type_conversion (WANT_ARITH
| WANT_ENUM
, arg
, 1)))
4293 errstring
= "wrong type argument to conjugation";
4294 else if (!noconvert
)
4295 arg
= default_conversion (arg
);
4298 case TRUTH_NOT_EXPR
:
4299 arg
= cp_convert (boolean_type_node
, arg
);
4300 val
= invert_truthvalue (arg
);
4301 if (arg
!= error_mark_node
)
4303 errstring
= "in argument to unary !";
4310 if (TREE_CODE (arg
) == COMPLEX_CST
)
4311 return TREE_REALPART (arg
);
4312 else if (TREE_CODE (TREE_TYPE (arg
)) == COMPLEX_TYPE
)
4313 return fold (build1 (REALPART_EXPR
, TREE_TYPE (TREE_TYPE (arg
)), arg
));
4318 if (TREE_CODE (arg
) == COMPLEX_CST
)
4319 return TREE_IMAGPART (arg
);
4320 else if (TREE_CODE (TREE_TYPE (arg
)) == COMPLEX_TYPE
)
4321 return fold (build1 (IMAGPART_EXPR
, TREE_TYPE (TREE_TYPE (arg
)), arg
));
4323 return cp_convert (TREE_TYPE (arg
), integer_zero_node
);
4325 case PREINCREMENT_EXPR
:
4326 case POSTINCREMENT_EXPR
:
4327 case PREDECREMENT_EXPR
:
4328 case POSTDECREMENT_EXPR
:
4329 /* Handle complex lvalues (when permitted)
4330 by reduction to simpler cases. */
4332 val
= unary_complex_lvalue (code
, arg
);
4336 /* Increment or decrement the real part of the value,
4337 and don't change the imaginary part. */
4338 if (TREE_CODE (TREE_TYPE (arg
)) == COMPLEX_TYPE
)
4342 arg
= stabilize_reference (arg
);
4343 real
= build_unary_op (REALPART_EXPR
, arg
, 1);
4344 imag
= build_unary_op (IMAGPART_EXPR
, arg
, 1);
4345 return build (COMPLEX_EXPR
, TREE_TYPE (arg
),
4346 build_unary_op (code
, real
, 1), imag
);
4349 /* Report invalid types. */
4351 if (!(arg
= build_expr_type_conversion (WANT_ARITH
| WANT_POINTER
,
4354 if (code
== PREINCREMENT_EXPR
)
4355 errstring
="no pre-increment operator for type";
4356 else if (code
== POSTINCREMENT_EXPR
)
4357 errstring
="no post-increment operator for type";
4358 else if (code
== PREDECREMENT_EXPR
)
4359 errstring
="no pre-decrement operator for type";
4361 errstring
="no post-decrement operator for type";
4365 /* Report something read-only. */
4367 if (CP_TYPE_CONST_P (TREE_TYPE (arg
))
4368 || TREE_READONLY (arg
))
4369 readonly_error (arg
, ((code
== PREINCREMENT_EXPR
4370 || code
== POSTINCREMENT_EXPR
)
4371 ? "increment" : "decrement"),
4376 tree result_type
= TREE_TYPE (arg
);
4378 arg
= get_unwidened (arg
, 0);
4379 argtype
= TREE_TYPE (arg
);
4381 /* ARM $5.2.5 last annotation says this should be forbidden. */
4382 if (TREE_CODE (argtype
) == ENUMERAL_TYPE
)
4383 pedwarn ("ANSI C++ forbids %sing an enum",
4384 (code
== PREINCREMENT_EXPR
|| code
== POSTINCREMENT_EXPR
)
4385 ? "increment" : "decrement");
4387 /* Compute the increment. */
4389 if (TREE_CODE (argtype
) == POINTER_TYPE
)
4391 enum tree_code tmp
= TREE_CODE (TREE_TYPE (argtype
));
4392 if (TYPE_SIZE (complete_type (TREE_TYPE (argtype
))) == 0)
4393 cp_error ("cannot %s a pointer to incomplete type `%T'",
4394 ((code
== PREINCREMENT_EXPR
4395 || code
== POSTINCREMENT_EXPR
)
4396 ? "increment" : "decrement"), TREE_TYPE (argtype
));
4397 else if ((pedantic
|| warn_pointer_arith
)
4398 && (tmp
== FUNCTION_TYPE
|| tmp
== METHOD_TYPE
4399 || tmp
== VOID_TYPE
|| tmp
== OFFSET_TYPE
))
4400 cp_pedwarn ("ANSI C++ forbids %sing a pointer of type `%T'",
4401 ((code
== PREINCREMENT_EXPR
4402 || code
== POSTINCREMENT_EXPR
)
4403 ? "increment" : "decrement"), argtype
);
4404 inc
= c_sizeof_nowarn (TREE_TYPE (argtype
));
4407 inc
= integer_one_node
;
4409 inc
= cp_convert (argtype
, inc
);
4411 /* Handle incrementing a cast-expression. */
4413 switch (TREE_CODE (arg
))
4418 case FIX_TRUNC_EXPR
:
4419 case FIX_FLOOR_EXPR
:
4420 case FIX_ROUND_EXPR
:
4423 tree incremented
, modify
, value
, compound
;
4424 if (! lvalue_p (arg
) && pedantic
)
4425 pedwarn ("cast to non-reference type used as lvalue");
4426 arg
= stabilize_reference (arg
);
4427 if (code
== PREINCREMENT_EXPR
|| code
== PREDECREMENT_EXPR
)
4430 value
= save_expr (arg
);
4431 incremented
= build (((code
== PREINCREMENT_EXPR
4432 || code
== POSTINCREMENT_EXPR
)
4433 ? PLUS_EXPR
: MINUS_EXPR
),
4434 argtype
, value
, inc
);
4435 TREE_SIDE_EFFECTS (incremented
) = 1;
4437 modify
= build_modify_expr (arg
, NOP_EXPR
, incremented
);
4438 compound
= build (COMPOUND_EXPR
, TREE_TYPE (arg
), modify
, value
);
4440 /* Eliminate warning about unused result of + or -. */
4441 TREE_NO_UNUSED_WARNING (compound
) = 1;
4449 /* Complain about anything else that is not a true lvalue. */
4450 if (!lvalue_or_else (arg
, ((code
== PREINCREMENT_EXPR
4451 || code
== POSTINCREMENT_EXPR
)
4452 ? "increment" : "decrement")))
4453 return error_mark_node
;
4455 /* Forbid using -- on `bool'. */
4456 if (TREE_TYPE (arg
) == boolean_type_node
)
4458 if (code
== POSTDECREMENT_EXPR
|| code
== PREDECREMENT_EXPR
)
4460 cp_error ("invalid use of `--' on bool variable `%D'", arg
);
4461 return error_mark_node
;
4464 /* This will only work if someone can convince Kenner to accept
4465 my patch to expand_increment. (jason) */
4466 val
= build (code
, TREE_TYPE (arg
), arg
, inc
);
4468 if (code
== POSTINCREMENT_EXPR
)
4470 arg
= stabilize_reference (arg
);
4471 val
= build (MODIFY_EXPR
, TREE_TYPE (arg
), arg
,
4473 TREE_SIDE_EFFECTS (val
) = 1;
4474 arg
= save_expr (arg
);
4475 val
= build (COMPOUND_EXPR
, TREE_TYPE (arg
), val
, arg
);
4476 val
= build (COMPOUND_EXPR
, TREE_TYPE (arg
), arg
, val
);
4479 val
= build (MODIFY_EXPR
, TREE_TYPE (arg
), arg
,
4484 val
= build (code
, TREE_TYPE (arg
), arg
, inc
);
4486 TREE_SIDE_EFFECTS (val
) = 1;
4487 return cp_convert (result_type
, val
);
4491 /* Note that this operation never does default_conversion
4492 regardless of NOCONVERT. */
4494 argtype
= lvalue_type (arg
);
4495 if (TREE_CODE (argtype
) == REFERENCE_TYPE
)
4499 build_pointer_type (TREE_TYPE (argtype
)), arg
);
4500 TREE_CONSTANT (arg
) = TREE_CONSTANT (TREE_OPERAND (arg
, 0));
4503 else if (pedantic
&& DECL_MAIN_P (arg
))
4505 pedwarn ("taking address of function `main'");
4507 /* Let &* cancel out to simplify resulting code. */
4508 if (TREE_CODE (arg
) == INDIRECT_REF
)
4510 /* We don't need to have `current_class_ptr' wrapped in a
4511 NON_LVALUE_EXPR node. */
4512 if (arg
== current_class_ref
)
4513 return current_class_ptr
;
4515 arg
= TREE_OPERAND (arg
, 0);
4516 if (TREE_CODE (TREE_TYPE (arg
)) == REFERENCE_TYPE
)
4520 build_pointer_type (TREE_TYPE (TREE_TYPE (arg
))), arg
);
4521 TREE_CONSTANT (arg
) = TREE_CONSTANT (TREE_OPERAND (arg
, 0));
4523 else if (lvalue_p (arg
))
4524 /* Don't let this be an lvalue. */
4525 return non_lvalue (arg
);
4529 /* For &x[y], return x+y */
4530 if (TREE_CODE (arg
) == ARRAY_REF
)
4532 if (mark_addressable (TREE_OPERAND (arg
, 0)) == 0)
4533 return error_mark_node
;
4534 return build_binary_op (PLUS_EXPR
, TREE_OPERAND (arg
, 0),
4535 TREE_OPERAND (arg
, 1), 1);
4538 /* Uninstantiated types are all functions. Taking the
4539 address of a function is a no-op, so just return the
4542 if (TREE_CODE (arg
) == IDENTIFIER_NODE
4543 && IDENTIFIER_OPNAME_P (arg
))
4545 my_friendly_abort (117);
4546 /* We don't know the type yet, so just work around the problem.
4547 We know that this will resolve to an lvalue. */
4548 return build1 (ADDR_EXPR
, unknown_type_node
, arg
);
4551 if (TREE_CODE (arg
) == TEMPLATE_ID_EXPR
)
4556 /* We don't require a match here; it's possible that the
4557 context (like a cast to a particular type) will resolve
4558 the particular choice of template. */
4559 fn
= determine_specialization (arg
,
4567 fn
= instantiate_template (fn
, targs
);
4568 mark_addressable (fn
);
4569 return build_unary_op (ADDR_EXPR
, fn
, 0);
4572 return build1 (ADDR_EXPR
, unknown_type_node
, arg
);
4574 else if (type_unknown_p (arg
))
4575 return build1 (ADDR_EXPR
, unknown_type_node
, arg
);
4577 /* Handle complex lvalues (when permitted)
4578 by reduction to simpler cases. */
4579 val
= unary_complex_lvalue (code
, arg
);
4583 switch (TREE_CODE (arg
))
4588 case FIX_TRUNC_EXPR
:
4589 case FIX_FLOOR_EXPR
:
4590 case FIX_ROUND_EXPR
:
4592 if (! lvalue_p (arg
) && pedantic
)
4593 pedwarn ("taking the address of a cast to non-reference type");
4600 /* Allow the address of a constructor if all the elements
4602 if (TREE_CODE (arg
) == CONSTRUCTOR
&& TREE_HAS_CONSTRUCTOR (arg
)
4603 && TREE_CONSTANT (arg
))
4605 /* Anything not already handled and not a true memory reference
4607 else if (TREE_CODE (argtype
) != FUNCTION_TYPE
4608 && TREE_CODE (argtype
) != METHOD_TYPE
4609 && !lvalue_or_else (arg
, "unary `&'"))
4610 return error_mark_node
;
4612 if (argtype
!= error_mark_node
)
4613 argtype
= build_pointer_type (argtype
);
4615 if (mark_addressable (arg
) == 0)
4616 return error_mark_node
;
4621 if (TREE_CODE (arg
) == COMPONENT_REF
)
4622 addr
= build_component_addr
4624 "attempt to take address of bit-field structure member `%s'");
4626 addr
= build1 (code
, argtype
, arg
);
4628 /* Address of a static or external variable or
4629 function counts as a constant */
4631 TREE_CONSTANT (addr
) = 1;
4633 if (TREE_CODE (argtype
) == POINTER_TYPE
4634 && TREE_CODE (TREE_TYPE (argtype
)) == METHOD_TYPE
)
4636 build_ptrmemfunc_type (argtype
);
4637 addr
= build_ptrmemfunc (argtype
, addr
, 0);
4650 argtype
= TREE_TYPE (arg
);
4651 return fold (build1 (code
, argtype
, arg
));
4655 return error_mark_node
;
4659 /* If CONVERSIONS is a conversion expression or a nested sequence of such,
4660 convert ARG with the same conversions in the same order
4661 and return the result. */
4664 convert_sequence (conversions
, arg
)
4668 switch (TREE_CODE (conversions
))
4673 case FIX_TRUNC_EXPR
:
4674 case FIX_FLOOR_EXPR
:
4675 case FIX_ROUND_EXPR
:
4677 return cp_convert (TREE_TYPE (conversions
),
4678 convert_sequence (TREE_OPERAND (conversions
, 0),
4687 /* Apply unary lvalue-demanding operator CODE to the expression ARG
4688 for certain kinds of expressions which are not really lvalues
4689 but which we can accept as lvalues.
4691 If ARG is not a kind of expression we can handle, return zero. */
4694 unary_complex_lvalue (code
, arg
)
4695 enum tree_code code
;
4698 /* Handle (a, b) used as an "lvalue". */
4699 if (TREE_CODE (arg
) == COMPOUND_EXPR
)
4701 tree real_result
= build_unary_op (code
, TREE_OPERAND (arg
, 1), 0);
4702 return build (COMPOUND_EXPR
, TREE_TYPE (real_result
),
4703 TREE_OPERAND (arg
, 0), real_result
);
4706 /* Handle (a ? b : c) used as an "lvalue". */
4707 if (TREE_CODE (arg
) == COND_EXPR
4708 || TREE_CODE (arg
) == MIN_EXPR
|| TREE_CODE (arg
) == MAX_EXPR
)
4709 return rationalize_conditional_expr (code
, arg
);
4711 if (TREE_CODE (arg
) == MODIFY_EXPR
4712 || TREE_CODE (arg
) == PREINCREMENT_EXPR
4713 || TREE_CODE (arg
) == PREDECREMENT_EXPR
)
4714 return unary_complex_lvalue
4715 (code
, build (COMPOUND_EXPR
, TREE_TYPE (TREE_OPERAND (arg
, 0)),
4716 arg
, TREE_OPERAND (arg
, 0)));
4718 if (code
!= ADDR_EXPR
)
4721 /* Handle (a = b) used as an "lvalue" for `&'. */
4722 if (TREE_CODE (arg
) == MODIFY_EXPR
4723 || TREE_CODE (arg
) == INIT_EXPR
)
4725 tree real_result
= build_unary_op (code
, TREE_OPERAND (arg
, 0), 0);
4726 arg
= build (COMPOUND_EXPR
, TREE_TYPE (real_result
), arg
, real_result
);
4727 TREE_NO_UNUSED_WARNING (arg
) = 1;
4731 if (TREE_CODE (TREE_TYPE (arg
)) == FUNCTION_TYPE
4732 || TREE_CODE (TREE_TYPE (arg
)) == METHOD_TYPE
4733 || TREE_CODE (TREE_TYPE (arg
)) == OFFSET_TYPE
)
4735 /* The representation of something of type OFFSET_TYPE
4736 is really the representation of a pointer to it.
4737 Here give the representation its true type. */
4740 my_friendly_assert (TREE_CODE (arg
) != SCOPE_REF
, 313);
4742 if (TREE_CODE (arg
) != OFFSET_REF
)
4745 t
= TREE_OPERAND (arg
, 1);
4747 /* Check all this code for right semantics. */
4748 if (TREE_CODE (t
) == FUNCTION_DECL
)
4750 if (DECL_DESTRUCTOR_P (t
))
4751 cp_error ("taking address of destructor");
4752 return build_unary_op (ADDR_EXPR
, t
, 0);
4754 if (TREE_CODE (t
) == VAR_DECL
)
4755 return build_unary_op (ADDR_EXPR
, t
, 0);
4760 if (TREE_OPERAND (arg
, 0)
4761 && ! is_dummy_object (TREE_OPERAND (arg
, 0))
4762 && TREE_CODE (t
) != FIELD_DECL
)
4764 cp_error ("taking address of bound pointer-to-member expression");
4765 return error_mark_node
;
4768 type
= build_offset_type (DECL_FIELD_CONTEXT (t
), TREE_TYPE (t
));
4769 type
= build_pointer_type (type
);
4771 t
= make_node (PTRMEM_CST
);
4772 TREE_TYPE (t
) = type
;
4773 PTRMEM_CST_MEMBER (t
) = TREE_OPERAND (arg
, 1);
4779 /* We permit compiler to make function calls returning
4780 objects of aggregate type look like lvalues. */
4784 if (TREE_CODE (targ
) == SAVE_EXPR
)
4785 targ
= TREE_OPERAND (targ
, 0);
4787 if (TREE_CODE (targ
) == CALL_EXPR
&& IS_AGGR_TYPE (TREE_TYPE (targ
)))
4789 if (TREE_CODE (arg
) == SAVE_EXPR
)
4792 targ
= build_cplus_new (TREE_TYPE (arg
), arg
);
4793 return build1 (ADDR_EXPR
, build_pointer_type (TREE_TYPE (arg
)), targ
);
4796 if (TREE_CODE (arg
) == SAVE_EXPR
&& TREE_CODE (targ
) == INDIRECT_REF
)
4797 return build (SAVE_EXPR
, build_pointer_type (TREE_TYPE (arg
)),
4798 TREE_OPERAND (targ
, 0), current_function_decl
, NULL
);
4801 /* Don't let anything else be handled specially. */
4805 /* Mark EXP saying that we need to be able to take the
4806 address of it; it should not be allocated in a register.
4807 Value is 1 if successful.
4809 C++: we do not allow `current_class_ptr' to be addressable. */
4812 mark_addressable (exp
)
4815 register tree x
= exp
;
4817 if (TREE_ADDRESSABLE (x
) == 1)
4821 switch (TREE_CODE (x
))
4828 x
= TREE_OPERAND (x
, 0);
4832 if (x
== current_class_ptr
)
4834 if (! flag_this_is_variable
)
4835 error ("address of `this' not available");
4836 TREE_ADDRESSABLE (x
) = 1; /* so compiler doesn't die later */
4837 put_var_into_stack (x
);
4841 if (TREE_STATIC (x
) && TREE_READONLY (x
)
4842 && DECL_RTL (x
) != 0
4843 && ! DECL_IN_MEMORY_P (x
))
4845 /* We thought this would make a good constant variable,
4846 but we were wrong. */
4847 push_obstacks_nochange ();
4848 end_temporary_allocation ();
4850 TREE_ASM_WRITTEN (x
) = 0;
4852 rest_of_decl_compilation (x
, 0,
4853 !DECL_FUNCTION_SCOPE_P (x
),
4855 TREE_ADDRESSABLE (x
) = 1;
4861 /* Caller should not be trying to mark initialized
4862 constant fields addressable. */
4863 my_friendly_assert (DECL_LANG_SPECIFIC (x
) == 0
4864 || DECL_IN_AGGR_P (x
) == 0
4866 || DECL_EXTERNAL (x
), 314);
4870 if (DECL_REGISTER (x
) && !TREE_ADDRESSABLE (x
)
4871 && !DECL_ARTIFICIAL (x
) && extra_warnings
)
4872 cp_warning ("address requested for `%D', which is declared `register'",
4874 put_var_into_stack (x
);
4875 TREE_ADDRESSABLE (x
) = 1;
4879 if (DECL_LANG_SPECIFIC (x
) != 0)
4881 x
= DECL_MAIN_VARIANT (x
);
4882 /* We have to test both conditions here. The first may be
4883 non-zero in the case of processing a default function. The
4884 second may be non-zero in the case of a template function. */
4885 if (DECL_TEMPLATE_INFO (x
) && !DECL_TEMPLATE_SPECIALIZATION (x
))
4888 TREE_ADDRESSABLE (x
) = 1;
4890 TREE_ADDRESSABLE (DECL_ASSEMBLER_NAME (x
)) = 1;
4894 TREE_ADDRESSABLE (x
) = 1;
4898 TREE_ADDRESSABLE (x
) = 1;
4899 mark_addressable (TREE_OPERAND (x
, 0));
4907 /* Build and return a conditional expression IFEXP ? OP1 : OP2. */
4910 build_x_conditional_expr (ifexp
, op1
, op2
)
4911 tree ifexp
, op1
, op2
;
4913 if (processing_template_decl
)
4914 return build_min_nt (COND_EXPR
, ifexp
, op1
, op2
);
4916 return build_new_op (COND_EXPR
, LOOKUP_NORMAL
, ifexp
, op1
, op2
);
4920 build_conditional_expr (ifexp
, op1
, op2
)
4921 tree ifexp
, op1
, op2
;
4923 register tree type1
;
4924 register tree type2
;
4925 register enum tree_code code1
;
4926 register enum tree_code code2
;
4927 register tree result_type
= NULL_TREE
;
4929 /* If second operand is omitted, it is the same as the first one;
4930 make sure it is calculated only once. */
4934 pedwarn ("ANSI C++ forbids omitting the middle term of a ?: expression");
4935 ifexp
= op1
= save_expr (ifexp
);
4938 ifexp
= cp_convert (boolean_type_node
, ifexp
);
4940 if (TREE_CODE (ifexp
) == ERROR_MARK
)
4941 return error_mark_node
;
4943 /* C++: REFERENCE_TYPES must be dereferenced. */
4944 type1
= TREE_TYPE (op1
);
4945 code1
= TREE_CODE (type1
);
4946 type2
= TREE_TYPE (op2
);
4947 code2
= TREE_CODE (type2
);
4949 if (code1
== REFERENCE_TYPE
)
4951 op1
= convert_from_reference (op1
);
4952 type1
= TREE_TYPE (op1
);
4953 code1
= TREE_CODE (type1
);
4955 if (code2
== REFERENCE_TYPE
)
4957 op2
= convert_from_reference (op2
);
4958 type2
= TREE_TYPE (op2
);
4959 code2
= TREE_CODE (type2
);
4962 /* Don't promote the operands separately if they promote
4963 the same way. Return the unpromoted type and let the combined
4964 value get promoted if necessary. */
4966 if (TYPE_MAIN_VARIANT (type1
) == TYPE_MAIN_VARIANT (type2
)
4967 && code2
!= ARRAY_TYPE
4968 && code2
!= FUNCTION_TYPE
4969 && code2
!= METHOD_TYPE
)
4973 if (TREE_CONSTANT (ifexp
)
4974 && (TREE_CODE (ifexp
) == INTEGER_CST
4975 || TREE_CODE (ifexp
) == ADDR_EXPR
))
4976 return (integer_zerop (ifexp
) ? op2
: op1
);
4978 if (TREE_CODE (op1
) == CONST_DECL
)
4979 op1
= DECL_INITIAL (op1
);
4980 else if (TREE_READONLY_DECL_P (op1
))
4981 op1
= decl_constant_value (op1
);
4982 if (TREE_CODE (op2
) == CONST_DECL
)
4983 op2
= DECL_INITIAL (op2
);
4984 else if (TREE_READONLY_DECL_P (op2
))
4985 op2
= decl_constant_value (op2
);
4987 type1
= cp_build_qualified_type
4988 (type1
, (CP_TYPE_QUALS (TREE_TYPE (op1
))
4989 | CP_TYPE_QUALS (TREE_TYPE (op2
))));
4990 /* ??? This is a kludge to deal with the fact that
4991 we don't sort out integers and enums properly, yet. */
4992 result
= fold (build (COND_EXPR
, type1
, ifexp
, op1
, op2
));
4993 if (TREE_TYPE (result
) != type1
)
4994 result
= build1 (NOP_EXPR
, type1
, result
);
4995 /* Expand both sides into the same slot,
4996 hopefully the target of the ?: expression. */
4997 if (TREE_CODE (op1
) == TARGET_EXPR
&& TREE_CODE (op2
) == TARGET_EXPR
)
4999 tree slot
= build (VAR_DECL
, TREE_TYPE (result
));
5000 layout_decl (slot
, 0);
5001 result
= build (TARGET_EXPR
, TREE_TYPE (result
),
5002 slot
, result
, NULL_TREE
, NULL_TREE
);
5007 /* They don't match; promote them both and then try to reconcile them.
5008 But don't permit mismatching enum types. */
5009 if (code1
== ENUMERAL_TYPE
)
5011 if (code2
== ENUMERAL_TYPE
)
5013 cp_error ("enumeral mismatch in conditional expression: `%T' vs `%T'",
5015 return error_mark_node
;
5017 else if (extra_warnings
&& ! IS_AGGR_TYPE_CODE (code2
)
5018 && type2
!= type_promotes_to (type1
))
5019 warning ("enumeral and non-enumeral type in conditional expression");
5021 else if (extra_warnings
5022 && code2
== ENUMERAL_TYPE
&& ! IS_AGGR_TYPE_CODE (code1
)
5023 && type1
!= type_promotes_to (type2
))
5024 warning ("enumeral and non-enumeral type in conditional expression");
5026 if (code1
!= VOID_TYPE
)
5028 op1
= default_conversion (op1
);
5029 type1
= TREE_TYPE (op1
);
5030 if (TYPE_PTRMEMFUNC_P (type1
))
5031 type1
= TYPE_PTRMEMFUNC_FN_TYPE (type1
);
5032 code1
= TREE_CODE (type1
);
5034 if (code2
!= VOID_TYPE
)
5036 op2
= default_conversion (op2
);
5037 type2
= TREE_TYPE (op2
);
5038 if (TYPE_PTRMEMFUNC_P (type2
))
5039 type2
= TYPE_PTRMEMFUNC_FN_TYPE (type2
);
5040 code2
= TREE_CODE (type2
);
5043 if (code1
== RECORD_TYPE
&& code2
== RECORD_TYPE
5044 && real_lvalue_p (op1
) && real_lvalue_p (op2
)
5045 && comptypes (type1
, type2
, COMPARE_BASE
| COMPARE_RELAXED
))
5047 type1
= build_reference_type (type1
);
5048 type2
= build_reference_type (type2
);
5049 result_type
= common_type (type1
, type2
);
5050 op1
= convert_to_reference (result_type
, op1
, CONV_IMPLICIT
,
5051 LOOKUP_NORMAL
, NULL_TREE
);
5052 op2
= convert_to_reference (result_type
, op2
, CONV_IMPLICIT
,
5053 LOOKUP_NORMAL
, NULL_TREE
);
5055 /* Quickly detect the usual case where op1 and op2 have the same type
5057 else if (TYPE_MAIN_VARIANT (type1
) == TYPE_MAIN_VARIANT (type2
))
5060 result_type
= type1
;
5063 cp_build_qualified_type (type1
,
5064 CP_TYPE_QUALS (TREE_TYPE (op1
))
5065 | CP_TYPE_QUALS (TREE_TYPE (op2
)));
5067 else if ((code1
== INTEGER_TYPE
|| code1
== REAL_TYPE
)
5068 && (code2
== INTEGER_TYPE
|| code2
== REAL_TYPE
))
5070 result_type
= common_type (type1
, type2
);
5072 else if (code1
== VOID_TYPE
|| code2
== VOID_TYPE
)
5074 if (pedantic
&& (code1
!= VOID_TYPE
|| code2
!= VOID_TYPE
))
5075 pedwarn ("ANSI C++ forbids conditional expr with only one void side");
5076 result_type
= void_type_node
;
5078 else if (code1
== POINTER_TYPE
&& null_ptr_cst_p (op2
))
5079 result_type
= qualify_type (type1
, type2
);
5080 else if (code2
== POINTER_TYPE
&& null_ptr_cst_p (op1
))
5081 result_type
= qualify_type (type2
, type1
);
5082 else if (code1
== POINTER_TYPE
&& code2
== POINTER_TYPE
)
5084 if (comp_target_types (type1
, type2
, 1))
5085 result_type
= common_type (type1
, type2
);
5086 else if (TYPE_MAIN_VARIANT (TREE_TYPE (type1
)) == void_type_node
)
5088 if (pedantic
&& TREE_CODE (type2
) == FUNCTION_TYPE
)
5089 pedwarn ("ANSI C++ forbids conditional expr between `void *' and function pointer");
5090 result_type
= qualify_type (type1
, type2
);
5092 else if (TYPE_MAIN_VARIANT (TREE_TYPE (type2
)) == void_type_node
)
5094 if (pedantic
&& TREE_CODE (type1
) == FUNCTION_TYPE
)
5095 pedwarn ("ANSI C++ forbids conditional expr between `void *' and function pointer");
5096 result_type
= qualify_type (type2
, type1
);
5099 else if (same_or_base_type_p (type2
, type1
))
5100 result_type
= type2
;
5101 else if (IS_AGGR_TYPE (TREE_TYPE (type1
))
5102 && IS_AGGR_TYPE (TREE_TYPE (type2
))
5103 && (result_type
= common_base_type (TREE_TYPE (type1
),
5104 TREE_TYPE (type2
))))
5106 if (result_type
== error_mark_node
)
5108 cp_error ("common base type of types `%T' and `%T' is ambiguous",
5109 TREE_TYPE (type1
), TREE_TYPE (type2
));
5110 result_type
= ptr_type_node
;
5115 && result_type
!= TREE_TYPE (type1
)
5116 && result_type
!= TREE_TYPE (type2
))
5117 cp_pedwarn ("`%T' and `%T' converted to `%T *' in conditional expression",
5118 type1
, type2
, result_type
);
5120 result_type
= build_pointer_type (result_type
);
5125 pedwarn ("pointer type mismatch in conditional expression");
5126 result_type
= ptr_type_node
;
5129 else if (code1
== POINTER_TYPE
&& code2
== INTEGER_TYPE
)
5131 pedwarn ("pointer/integer type mismatch in conditional expression");
5132 result_type
= type1
;
5134 else if (code2
== POINTER_TYPE
&& code1
== INTEGER_TYPE
)
5136 pedwarn ("pointer/integer type mismatch in conditional expression");
5137 result_type
= type2
;
5139 if (type2
== unknown_type_node
)
5140 result_type
= type1
;
5141 else if (type1
== unknown_type_node
)
5142 result_type
= type2
;
5146 /* The match does not look good. If either is
5147 an aggregate value, try converting to a scalar type. */
5148 if (code1
== RECORD_TYPE
&& code2
== RECORD_TYPE
)
5150 cp_error ("aggregate mismatch in conditional expression: `%T' vs `%T'",
5152 return error_mark_node
;
5154 /* Warning: this code assumes that conversion between cv-variants of
5155 a type is done using NOP_EXPRs. */
5156 if (code1
== RECORD_TYPE
&& TYPE_HAS_CONVERSION (type1
))
5158 /* There are other types besides pointers and records. */
5160 if (code2
== POINTER_TYPE
)
5161 tmp
= build_pointer_type
5162 (cp_build_qualified_type (TREE_TYPE (type2
),
5164 | TYPE_QUAL_VOLATILE
5165 | TYPE_QUAL_RESTRICT
));
5168 tmp
= build_type_conversion (CONVERT_EXPR
, tmp
, op1
, 0);
5169 if (tmp
== NULL_TREE
)
5171 cp_error ("incompatible types `%T' and `%T' in `?:'",
5173 return error_mark_node
;
5175 if (tmp
== error_mark_node
)
5176 error ("ambiguous pointer conversion");
5179 result_type
= common_type (type2
, TREE_TYPE (tmp
));
5182 else if (code2
== RECORD_TYPE
&& TYPE_HAS_CONVERSION (type2
))
5185 if (code1
== POINTER_TYPE
)
5186 tmp
= build_pointer_type
5187 (cp_build_qualified_type (TREE_TYPE (type1
),
5189 | TYPE_QUAL_VOLATILE
5190 | TYPE_QUAL_RESTRICT
));
5194 tmp
= build_type_conversion (CONVERT_EXPR
, tmp
, op2
, 0);
5195 if (tmp
== NULL_TREE
)
5197 cp_error ("incompatible types `%T' and `%T' in `?:'",
5199 return error_mark_node
;
5201 if (tmp
== error_mark_node
)
5202 error ("ambiguous pointer conversion");
5205 result_type
= common_type (type1
, TREE_TYPE (tmp
));
5208 else if (flag_cond_mismatch
)
5209 result_type
= void_type_node
;
5212 error ("type mismatch in conditional expression");
5213 return error_mark_node
;
5217 if (TREE_CODE (result_type
) == POINTER_TYPE
5218 && TREE_CODE (TREE_TYPE (result_type
)) == METHOD_TYPE
)
5219 result_type
= build_ptrmemfunc_type (result_type
);
5221 if (result_type
!= TREE_TYPE (op1
))
5222 op1
= convert_for_initialization
5223 (NULL_TREE
, result_type
, op1
, LOOKUP_NORMAL
, "converting", NULL_TREE
, 0);
5224 if (result_type
!= TREE_TYPE (op2
))
5225 op2
= convert_for_initialization
5226 (NULL_TREE
, result_type
, op2
, LOOKUP_NORMAL
, "converting", NULL_TREE
, 0);
5228 if (TREE_CODE (ifexp
) == INTEGER_CST
)
5229 return integer_zerop (ifexp
) ? op2
: op1
;
5231 return convert_from_reference
5232 (fold (build (COND_EXPR
, result_type
, ifexp
, op1
, op2
)));
5235 /* Handle overloading of the ',' operator when needed. Otherwise,
5236 this function just builds an expression list. */
5239 build_x_compound_expr (list
)
5242 tree rest
= TREE_CHAIN (list
);
5245 if (processing_template_decl
)
5246 return build_min_nt (COMPOUND_EXPR
, list
, NULL_TREE
);
5248 if (rest
== NULL_TREE
)
5249 return build_compound_expr (list
);
5251 result
= build_opfncall (COMPOUND_EXPR
, LOOKUP_NORMAL
,
5252 TREE_VALUE (list
), TREE_VALUE (rest
), NULL_TREE
);
5254 return build_x_compound_expr (expr_tree_cons (NULL_TREE
, result
,
5255 TREE_CHAIN (rest
)));
5257 if (! TREE_SIDE_EFFECTS (TREE_VALUE (list
)))
5259 /* the left-hand operand of a comma expression is like an expression
5260 statement: we should warn if it doesn't have any side-effects,
5261 unless it was explicitly cast to (void). */
5262 if ((extra_warnings
|| warn_unused
)
5263 && !(TREE_CODE (TREE_VALUE(list
)) == CONVERT_EXPR
5264 && TREE_TYPE (TREE_VALUE(list
)) == void_type_node
))
5265 warning("left-hand operand of comma expression has no effect");
5267 #if 0 /* this requires a gcc backend patch to export warn_if_unused_value */
5268 else if (warn_unused
)
5269 warn_if_unused_value (TREE_VALUE(list
));
5272 return build_compound_expr
5273 (expr_tree_cons (NULL_TREE
, TREE_VALUE (list
),
5274 build_expr_list (NULL_TREE
,
5275 build_x_compound_expr (rest
))));
5278 /* Given a list of expressions, return a compound expression
5279 that performs them all and returns the value of the last of them. */
5282 build_compound_expr (list
)
5287 if (TREE_READONLY_DECL_P (TREE_VALUE (list
)))
5288 TREE_VALUE (list
) = decl_constant_value (TREE_VALUE (list
));
5290 if (TREE_CHAIN (list
) == 0)
5292 /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
5293 Strip such NOP_EXPRs, since LIST is used in non-lvalue context. */
5294 if (TREE_CODE (list
) == NOP_EXPR
5295 && TREE_TYPE (list
) == TREE_TYPE (TREE_OPERAND (list
, 0)))
5296 list
= TREE_OPERAND (list
, 0);
5298 /* Convert arrays to pointers. */
5299 if (TREE_CODE (TREE_TYPE (TREE_VALUE (list
))) == ARRAY_TYPE
)
5300 return default_conversion (TREE_VALUE (list
));
5302 return TREE_VALUE (list
);
5305 rest
= build_compound_expr (TREE_CHAIN (list
));
5307 /* When pedantic, a compound expression cannot be a constant expression. */
5308 if (! TREE_SIDE_EFFECTS (TREE_VALUE (list
)) && ! pedantic
)
5311 return build (COMPOUND_EXPR
, TREE_TYPE (rest
),
5312 break_out_cleanups (TREE_VALUE (list
)), rest
);
5316 build_static_cast (type
, expr
)
5322 if (type
== error_mark_node
|| expr
== error_mark_node
)
5323 return error_mark_node
;
5325 if (TREE_CODE (expr
) == OFFSET_REF
)
5326 expr
= resolve_offset_ref (expr
);
5328 if (processing_template_decl
)
5330 tree t
= build_min (STATIC_CAST_EXPR
, copy_to_permanent (type
),
5335 /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
5336 Strip such NOP_EXPRs if VALUE is being used in non-lvalue context. */
5337 if (TREE_CODE (type
) != REFERENCE_TYPE
5338 && TREE_CODE (expr
) == NOP_EXPR
5339 && TREE_TYPE (expr
) == TREE_TYPE (TREE_OPERAND (expr
, 0)))
5340 expr
= TREE_OPERAND (expr
, 0);
5342 if (TREE_CODE (type
) == VOID_TYPE
)
5343 return build1 (CONVERT_EXPR
, type
, expr
);
5345 if (TREE_CODE (type
) == REFERENCE_TYPE
)
5346 return (convert_from_reference
5347 (convert_to_reference (type
, expr
, CONV_STATIC
|CONV_IMPLICIT
,
5348 LOOKUP_COMPLAIN
, NULL_TREE
)));
5350 if (IS_AGGR_TYPE (type
))
5351 return build_cplus_new
5352 (type
, (build_method_call
5353 (NULL_TREE
, ctor_identifier
, build_expr_list (NULL_TREE
, expr
),
5354 TYPE_BINFO (type
), LOOKUP_NORMAL
)));
5356 expr
= decay_conversion (expr
);
5357 intype
= TREE_TYPE (expr
);
5359 /* FIXME handle casting to array type. */
5362 if (can_convert_arg (type
, intype
, expr
))
5364 else if (TYPE_PTROB_P (type
) && TYPE_PTROB_P (intype
))
5367 if (IS_AGGR_TYPE (TREE_TYPE (type
)) && IS_AGGR_TYPE (TREE_TYPE (intype
))
5368 && at_least_as_qualified_p (TREE_TYPE (type
),
5370 && (binfo
= get_binfo (TREE_TYPE (intype
), TREE_TYPE (type
), 0))
5371 && ! TREE_VIA_VIRTUAL (binfo
))
5374 else if (TYPE_PTRMEM_P (type
) && TYPE_PTRMEM_P (intype
))
5376 if (same_type_p (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (type
))),
5377 TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (intype
))))
5378 && at_least_as_qualified_p (TREE_TYPE (TREE_TYPE (type
)),
5379 TREE_TYPE (TREE_TYPE (intype
)))
5380 && (binfo
= get_binfo (TYPE_OFFSET_BASETYPE (TREE_TYPE (type
)),
5381 TYPE_OFFSET_BASETYPE (TREE_TYPE (intype
)), 0))
5382 && ! TREE_VIA_VIRTUAL (binfo
))
5385 else if (TREE_CODE (intype
) != BOOLEAN_TYPE
5386 && TREE_CODE (type
) != ARRAY_TYPE
5387 && TREE_CODE (type
) != FUNCTION_TYPE
5388 && can_convert (intype
, type
))
5392 return build_c_cast (type
, expr
);
5394 cp_error ("static_cast from `%T' to `%T'", intype
, type
);
5395 return error_mark_node
;
5399 build_reinterpret_cast (type
, expr
)
5404 if (type
== error_mark_node
|| expr
== error_mark_node
)
5405 return error_mark_node
;
5407 if (TREE_CODE (expr
) == OFFSET_REF
)
5408 expr
= resolve_offset_ref (expr
);
5410 if (processing_template_decl
)
5412 tree t
= build_min (REINTERPRET_CAST_EXPR
,
5413 copy_to_permanent (type
), expr
);
5417 if (TREE_CODE (type
) != REFERENCE_TYPE
)
5419 expr
= decay_conversion (expr
);
5421 /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
5422 Strip such NOP_EXPRs if VALUE is being used in non-lvalue context. */
5423 if (TREE_CODE (expr
) == NOP_EXPR
5424 && TREE_TYPE (expr
) == TREE_TYPE (TREE_OPERAND (expr
, 0)))
5425 expr
= TREE_OPERAND (expr
, 0);
5428 intype
= TREE_TYPE (expr
);
5430 if (TREE_CODE (type
) == REFERENCE_TYPE
)
5432 if (! real_lvalue_p (expr
))
5434 cp_error ("reinterpret_cast from `%T' rvalue to `%T'", intype
, type
);
5435 return error_mark_node
;
5437 expr
= build_unary_op (ADDR_EXPR
, expr
, 0);
5438 if (expr
!= error_mark_node
)
5439 expr
= build_reinterpret_cast
5440 (build_pointer_type (TREE_TYPE (type
)), expr
);
5441 if (expr
!= error_mark_node
)
5442 expr
= build_indirect_ref (expr
, 0);
5445 else if (same_type_p (TYPE_MAIN_VARIANT (intype
),
5446 TYPE_MAIN_VARIANT (type
)))
5447 return build_static_cast (type
, expr
);
5449 if (TYPE_PTR_P (type
) && (TREE_CODE (intype
) == INTEGER_TYPE
5450 || TREE_CODE (intype
) == ENUMERAL_TYPE
))
5452 else if (TREE_CODE (type
) == INTEGER_TYPE
&& TYPE_PTR_P (intype
))
5454 if (TYPE_PRECISION (type
) < TYPE_PRECISION (intype
))
5455 cp_pedwarn ("reinterpret_cast from `%T' to `%T' loses precision",
5458 else if ((TYPE_PTRFN_P (type
) && TYPE_PTRFN_P (intype
))
5459 || (TYPE_PTRMEMFUNC_P (type
) && TYPE_PTRMEMFUNC_P (intype
)))
5461 if (TREE_READONLY_DECL_P (expr
))
5462 expr
= decl_constant_value (expr
);
5463 return fold (build1 (NOP_EXPR
, type
, expr
));
5465 else if ((TYPE_PTRMEM_P (type
) && TYPE_PTRMEM_P (intype
))
5466 || (TYPE_PTROBV_P (type
) && TYPE_PTROBV_P (intype
)))
5468 if (! comp_ptr_ttypes_reinterpret (TREE_TYPE (type
), TREE_TYPE (intype
)))
5469 cp_pedwarn ("reinterpret_cast from `%T' to `%T' casts away const (or volatile)",
5472 if (TREE_READONLY_DECL_P (expr
))
5473 expr
= decl_constant_value (expr
);
5474 return fold (build1 (NOP_EXPR
, type
, expr
));
5476 else if ((TYPE_PTRFN_P (type
) && TYPE_PTROBV_P (intype
))
5477 || (TYPE_PTRFN_P (type
) && TYPE_PTROBV_P (intype
)))
5479 pedwarn ("ANSI C++ forbids casting between pointers to functions and objects");
5480 if (TREE_READONLY_DECL_P (expr
))
5481 expr
= decl_constant_value (expr
);
5482 return fold (build1 (NOP_EXPR
, type
, expr
));
5486 cp_error ("reinterpret_cast from `%T' to `%T'", intype
, type
);
5487 return error_mark_node
;
5490 return cp_convert (type
, expr
);
5494 build_const_cast (type
, expr
)
5499 if (type
== error_mark_node
|| expr
== error_mark_node
)
5500 return error_mark_node
;
5502 if (TREE_CODE (expr
) == OFFSET_REF
)
5503 expr
= resolve_offset_ref (expr
);
5505 if (processing_template_decl
)
5507 tree t
= build_min (CONST_CAST_EXPR
, copy_to_permanent (type
),
5512 if (TREE_CODE (type
) != REFERENCE_TYPE
)
5514 expr
= decay_conversion (expr
);
5516 /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
5517 Strip such NOP_EXPRs if VALUE is being used in non-lvalue context. */
5518 if (TREE_CODE (expr
) == NOP_EXPR
5519 && TREE_TYPE (expr
) == TREE_TYPE (TREE_OPERAND (expr
, 0)))
5520 expr
= TREE_OPERAND (expr
, 0);
5523 intype
= TREE_TYPE (expr
);
5525 if (same_type_p (TYPE_MAIN_VARIANT (intype
), TYPE_MAIN_VARIANT (type
)))
5526 return build_static_cast (type
, expr
);
5527 else if (TREE_CODE (type
) == REFERENCE_TYPE
)
5529 if (! real_lvalue_p (expr
))
5531 cp_error ("const_cast from `%T' rvalue to `%T'", intype
, type
);
5532 return error_mark_node
;
5535 if (comp_ptr_ttypes_const (TREE_TYPE (type
), intype
))
5537 expr
= build_unary_op (ADDR_EXPR
, expr
, 0);
5538 expr
= build1 (NOP_EXPR
, type
, expr
);
5539 return convert_from_reference (expr
);
5542 else if (TREE_CODE (type
) == POINTER_TYPE
5543 && TREE_CODE (intype
) == POINTER_TYPE
5544 && comp_ptr_ttypes_const (TREE_TYPE (type
), TREE_TYPE (intype
)))
5545 return cp_convert (type
, expr
);
5547 cp_error ("const_cast from `%T' to `%T'", intype
, type
);
5548 return error_mark_node
;
5551 /* Build an expression representing a cast to type TYPE of expression EXPR.
5553 ALLOW_NONCONVERTING is true if we should allow non-converting constructors
5554 when doing the cast. */
5557 build_c_cast (type
, expr
)
5560 register tree value
= expr
;
5563 if (type
== error_mark_node
|| expr
== error_mark_node
)
5564 return error_mark_node
;
5566 /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
5567 Strip such NOP_EXPRs if VALUE is being used in non-lvalue context. */
5568 if (TREE_CODE (type
) != REFERENCE_TYPE
5569 && TREE_CODE (value
) == NOP_EXPR
5570 && TREE_TYPE (value
) == TREE_TYPE (TREE_OPERAND (value
, 0)))
5571 value
= TREE_OPERAND (value
, 0);
5573 if (TREE_TYPE (expr
)
5574 && TREE_CODE (TREE_TYPE (expr
)) == OFFSET_TYPE
5575 && TREE_CODE (type
) != OFFSET_TYPE
)
5576 value
= resolve_offset_ref (value
);
5578 if (TREE_CODE (type
) == ARRAY_TYPE
)
5580 /* Allow casting from T1* to T2[] because Cfront allows it.
5581 NIHCL uses it. It is not valid ANSI C however, and hence, not
5583 if (TREE_CODE (TREE_TYPE (expr
)) == POINTER_TYPE
)
5586 pedwarn ("ANSI C++ forbids casting to an array type");
5587 type
= build_pointer_type (TREE_TYPE (type
));
5591 error ("ANSI C++ forbids casting to an array type");
5592 return error_mark_node
;
5596 if (TREE_CODE (type
) == FUNCTION_TYPE
5597 || TREE_CODE (type
) == METHOD_TYPE
)
5599 cp_error ("casting to function type `%T'", type
);
5600 return error_mark_node
;
5603 if (IS_SIGNATURE (type
))
5605 error ("cast specifies signature type");
5606 return error_mark_node
;
5609 if (processing_template_decl
)
5611 tree t
= build_min (CAST_EXPR
, type
,
5612 min_tree_cons (NULL_TREE
, value
, NULL_TREE
));
5616 /* Convert functions and arrays to pointers and
5617 convert references to their expanded types,
5618 but don't convert any other types. If, however, we are
5619 casting to a class type, there's no reason to do this: the
5620 cast will only succeed if there is a converting constructor,
5621 and the default conversions will be done at that point. In
5622 fact, doing the default conversion here is actually harmful
5626 struct S { S(const A&); };
5628 since we don't want the array-to-pointer conversion done. */
5629 if (!IS_AGGR_TYPE (type
))
5631 if (TREE_CODE (TREE_TYPE (value
)) == FUNCTION_TYPE
5632 || (TREE_CODE (TREE_TYPE (value
)) == METHOD_TYPE
5633 /* Don't do the default conversion on a ->* expression. */
5634 && ! (TREE_CODE (type
) == POINTER_TYPE
5635 && bound_pmf_p (value
)))
5636 || TREE_CODE (TREE_TYPE (value
)) == ARRAY_TYPE
5637 || TREE_CODE (TREE_TYPE (value
)) == REFERENCE_TYPE
)
5638 value
= default_conversion (value
);
5640 else if (TREE_CODE (TREE_TYPE (value
)) == REFERENCE_TYPE
)
5641 /* However, even for class types, we still need to strip away
5642 the reference type, since the call to convert_force below
5643 does not expect the input expression to be of reference
5645 value
= convert_from_reference (value
);
5647 otype
= TREE_TYPE (value
);
5649 /* Optionally warn about potentially worrisome casts. */
5652 && TREE_CODE (type
) == POINTER_TYPE
5653 && TREE_CODE (otype
) == POINTER_TYPE
5654 && !at_least_as_qualified_p (TREE_TYPE (type
),
5656 cp_warning ("cast discards qualifiers from pointer target type");
5658 /* Warn about possible alignment problems. */
5659 if (STRICT_ALIGNMENT
&& warn_cast_align
5660 && TREE_CODE (type
) == POINTER_TYPE
5661 && TREE_CODE (otype
) == POINTER_TYPE
5662 && TREE_CODE (TREE_TYPE (otype
)) != VOID_TYPE
5663 && TREE_CODE (TREE_TYPE (otype
)) != FUNCTION_TYPE
5664 && TYPE_ALIGN (TREE_TYPE (type
)) > TYPE_ALIGN (TREE_TYPE (otype
)))
5665 warning ("cast increases required alignment of target type");
5668 /* We should see about re-enabling these, they seem useful to
5670 if (TREE_CODE (type
) == INTEGER_TYPE
5671 && TREE_CODE (otype
) == POINTER_TYPE
5672 && TYPE_PRECISION (type
) != TYPE_PRECISION (otype
))
5673 warning ("cast from pointer to integer of different size");
5675 if (TREE_CODE (type
) == POINTER_TYPE
5676 && TREE_CODE (otype
) == INTEGER_TYPE
5677 && TYPE_PRECISION (type
) != TYPE_PRECISION (otype
)
5678 /* Don't warn about converting 0 to pointer,
5679 provided the 0 was explicit--not cast or made by folding. */
5680 && !(TREE_CODE (value
) == INTEGER_CST
&& integer_zerop (value
)))
5681 warning ("cast to pointer from integer of different size");
5684 if (TREE_CODE (type
) == REFERENCE_TYPE
)
5685 value
= (convert_from_reference
5686 (convert_to_reference (type
, value
, CONV_C_CAST
,
5687 LOOKUP_COMPLAIN
, NULL_TREE
)));
5692 if (TREE_READONLY_DECL_P (value
))
5693 value
= decl_constant_value (value
);
5696 value
= convert_force (type
, value
, CONV_C_CAST
);
5698 /* Ignore any integer overflow caused by the cast. */
5699 if (TREE_CODE (value
) == INTEGER_CST
)
5701 TREE_OVERFLOW (value
) = TREE_OVERFLOW (ovalue
);
5702 TREE_CONSTANT_OVERFLOW (value
) = TREE_CONSTANT_OVERFLOW (ovalue
);
5706 /* Always produce some operator for an explicit cast,
5707 so we can tell (for -pedantic) that the cast is no lvalue. */
5708 if (TREE_CODE (type
) != REFERENCE_TYPE
&& value
== expr
5709 && real_lvalue_p (value
))
5710 value
= non_lvalue (value
);
5715 /* Build an assignment expression of lvalue LHS from value RHS.
5716 MODIFYCODE is the code for a binary operator that we use
5717 to combine the old value of LHS with RHS to get the new value.
5718 Or else MODIFYCODE is NOP_EXPR meaning do a simple assignment.
5720 C++: If MODIFYCODE is INIT_EXPR, then leave references unbashed. */
5723 build_modify_expr (lhs
, modifycode
, rhs
)
5725 enum tree_code modifycode
;
5728 register tree result
;
5730 tree lhstype
= TREE_TYPE (lhs
);
5731 tree olhstype
= lhstype
;
5734 /* Avoid duplicate error messages from operands that had errors. */
5735 if (lhs
== error_mark_node
|| rhs
== error_mark_node
)
5736 return error_mark_node
;
5738 /* Types that aren't fully specified cannot be used in assignments. */
5739 lhs
= require_complete_type (lhs
);
5743 /* Handle assignment to signature pointers/refs. */
5745 if (TYPE_LANG_SPECIFIC (lhstype
)
5746 && (IS_SIGNATURE_POINTER (lhstype
) || IS_SIGNATURE_REFERENCE (lhstype
)))
5748 return build_signature_pointer_constructor (lhs
, rhs
);
5751 /* Handle control structure constructs used as "lvalues". */
5753 switch (TREE_CODE (lhs
))
5755 /* Handle --foo = 5; as these are valid constructs in C++ */
5756 case PREDECREMENT_EXPR
:
5757 case PREINCREMENT_EXPR
:
5758 if (TREE_SIDE_EFFECTS (TREE_OPERAND (lhs
, 0)))
5759 lhs
= build (TREE_CODE (lhs
), TREE_TYPE (lhs
),
5760 stabilize_reference (TREE_OPERAND (lhs
, 0)),
5761 TREE_OPERAND (lhs
, 1));
5762 return build (COMPOUND_EXPR
, lhstype
,
5764 build_modify_expr (TREE_OPERAND (lhs
, 0),
5767 /* Handle (a, b) used as an "lvalue". */
5769 newrhs
= build_modify_expr (TREE_OPERAND (lhs
, 1),
5771 if (newrhs
== error_mark_node
)
5772 return error_mark_node
;
5773 return build (COMPOUND_EXPR
, lhstype
,
5774 TREE_OPERAND (lhs
, 0), newrhs
);
5777 newrhs
= build_modify_expr (TREE_OPERAND (lhs
, 0), modifycode
, rhs
);
5778 if (newrhs
== error_mark_node
)
5779 return error_mark_node
;
5780 return build (COMPOUND_EXPR
, lhstype
, lhs
, newrhs
);
5782 /* Handle (a ? b : c) used as an "lvalue". */
5784 rhs
= save_expr (rhs
);
5786 /* Produce (a ? (b = rhs) : (c = rhs))
5787 except that the RHS goes through a save-expr
5788 so the code to compute it is only emitted once. */
5790 = build_conditional_expr (TREE_OPERAND (lhs
, 0),
5791 build_modify_expr (cp_convert (TREE_TYPE (lhs
), TREE_OPERAND (lhs
, 1)),
5793 build_modify_expr (cp_convert (TREE_TYPE (lhs
), TREE_OPERAND (lhs
, 2)),
5795 if (cond
== error_mark_node
)
5797 /* Make sure the code to compute the rhs comes out
5798 before the split. */
5799 return build (COMPOUND_EXPR
, TREE_TYPE (lhs
),
5800 /* Case to void to suppress warning
5801 from warn_if_unused_value. */
5802 cp_convert (void_type_node
, rhs
), cond
);
5809 if (TREE_CODE (lhs
) == OFFSET_REF
)
5811 if (TREE_OPERAND (lhs
, 0) == NULL_TREE
)
5813 /* Static class member? */
5814 tree member
= TREE_OPERAND (lhs
, 1);
5815 if (TREE_CODE (member
) == VAR_DECL
)
5819 compiler_error ("invalid static class member");
5820 return error_mark_node
;
5824 lhs
= resolve_offset_ref (lhs
);
5826 olhstype
= lhstype
= TREE_TYPE (lhs
);
5829 if (TREE_CODE (lhstype
) == REFERENCE_TYPE
5830 && modifycode
!= INIT_EXPR
)
5832 lhs
= convert_from_reference (lhs
);
5833 olhstype
= lhstype
= TREE_TYPE (lhs
);
5836 /* If a binary op has been requested, combine the old LHS value with the RHS
5837 producing the value we should actually store into the LHS. */
5839 if (modifycode
== INIT_EXPR
)
5841 if (! IS_AGGR_TYPE (lhstype
))
5842 /* Do the default thing */;
5845 result
= build_method_call (lhs
, ctor_identifier
,
5846 build_expr_list (NULL_TREE
, rhs
),
5847 TYPE_BINFO (lhstype
), LOOKUP_NORMAL
);
5848 if (result
== NULL_TREE
)
5849 return error_mark_node
;
5853 else if (modifycode
== NOP_EXPR
)
5855 /* `operator=' is not an inheritable operator. */
5856 if (! IS_AGGR_TYPE (lhstype
))
5857 /* Do the default thing */;
5860 result
= build_opfncall (MODIFY_EXPR
, LOOKUP_NORMAL
,
5861 lhs
, rhs
, make_node (NOP_EXPR
));
5862 if (result
== NULL_TREE
)
5863 return error_mark_node
;
5868 else if (PROMOTES_TO_AGGR_TYPE (lhstype
, REFERENCE_TYPE
))
5870 my_friendly_abort (978652);
5874 lhs
= stabilize_reference (lhs
);
5875 newrhs
= build_binary_op (modifycode
, lhs
, rhs
, 1);
5876 if (newrhs
== error_mark_node
)
5878 cp_error (" in evaluation of `%Q(%#T, %#T)'", modifycode
,
5879 TREE_TYPE (lhs
), TREE_TYPE (rhs
));
5880 return error_mark_node
;
5884 /* Handle a cast used as an "lvalue".
5885 We have already performed any binary operator using the value as cast.
5886 Now convert the result to the cast type of the lhs,
5887 and then true type of the lhs and store it there;
5888 then convert result back to the cast type to be the value
5889 of the assignment. */
5891 switch (TREE_CODE (lhs
))
5896 case FIX_TRUNC_EXPR
:
5897 case FIX_FLOOR_EXPR
:
5898 case FIX_ROUND_EXPR
:
5900 if (TREE_CODE (TREE_TYPE (newrhs
)) == ARRAY_TYPE
5901 || TREE_CODE (TREE_TYPE (newrhs
)) == FUNCTION_TYPE
5902 || TREE_CODE (TREE_TYPE (newrhs
)) == METHOD_TYPE
5903 || TREE_CODE (TREE_TYPE (newrhs
)) == OFFSET_TYPE
)
5904 newrhs
= default_conversion (newrhs
);
5906 tree inner_lhs
= TREE_OPERAND (lhs
, 0);
5909 /* WP 5.4.1: The result is an lvalue if T is a reference type,
5910 otherwise the result is an rvalue. */
5911 if (! lvalue_p (lhs
))
5912 pedwarn ("ANSI C++ forbids cast to non-reference type used as lvalue");
5914 result
= build_modify_expr (inner_lhs
, NOP_EXPR
,
5915 cp_convert (TREE_TYPE (inner_lhs
),
5916 cp_convert (lhstype
, newrhs
)));
5917 if (result
== error_mark_node
)
5919 return cp_convert (TREE_TYPE (lhs
), result
);
5926 /* Now we have handled acceptable kinds of LHS that are not truly lvalues.
5927 Reject anything strange now. */
5929 if (!lvalue_or_else (lhs
, "assignment"))
5930 return error_mark_node
;
5932 GNU_xref_assign (lhs
);
5934 /* Warn about storing in something that is `const'. */
5935 /* For C++, don't warn if this is initialization. */
5936 if (modifycode
!= INIT_EXPR
5937 /* For assignment to `const' signature pointer/reference fields,
5938 don't warn either, we already printed a better message before. */
5939 && ! (TREE_CODE (lhs
) == COMPONENT_REF
5940 && (IS_SIGNATURE_POINTER (TREE_TYPE (TREE_OPERAND (lhs
, 0)))
5941 || IS_SIGNATURE_REFERENCE (TREE_TYPE (TREE_OPERAND (lhs
, 0)))))
5942 && (TREE_READONLY (lhs
) || CP_TYPE_CONST_P (lhstype
)
5943 /* Functions are not modifiable, even though they are
5945 || TREE_CODE (TREE_TYPE (lhs
)) == FUNCTION_TYPE
5946 || ((TREE_CODE (lhstype
) == RECORD_TYPE
5947 || TREE_CODE (lhstype
) == UNION_TYPE
)
5948 && C_TYPE_FIELDS_READONLY (lhstype
))
5949 || (TREE_CODE (lhstype
) == REFERENCE_TYPE
5950 && CP_TYPE_CONST_P (TREE_TYPE (lhstype
)))))
5951 readonly_error (lhs
, "assignment", 0);
5953 /* If storing into a structure or union member,
5954 it has probably been given type `int'.
5955 Compute the type that would go with
5956 the actual amount of storage the member occupies. */
5958 if (TREE_CODE (lhs
) == COMPONENT_REF
5959 && (TREE_CODE (lhstype
) == INTEGER_TYPE
5960 || TREE_CODE (lhstype
) == REAL_TYPE
5961 || TREE_CODE (lhstype
) == ENUMERAL_TYPE
))
5963 lhstype
= TREE_TYPE (get_unwidened (lhs
, 0));
5965 /* If storing in a field that is in actuality a short or narrower
5966 than one, we must store in the field in its actual type. */
5968 if (lhstype
!= TREE_TYPE (lhs
))
5970 lhs
= copy_node (lhs
);
5971 TREE_TYPE (lhs
) = lhstype
;
5975 /* check to see if there is an assignment to `this' */
5976 if (lhs
== current_class_ptr
)
5978 if (flag_this_is_variable
> 0
5979 && DECL_NAME (current_function_decl
) != NULL_TREE
5980 && (DECL_NAME (current_function_decl
)
5981 != constructor_name (current_class_type
)))
5982 warning ("assignment to `this' not in constructor or destructor");
5983 current_function_just_assigned_this
= 1;
5986 if (modifycode
!= INIT_EXPR
)
5988 /* Make modifycode now either a NOP_EXPR or an INIT_EXPR. */
5989 modifycode
= NOP_EXPR
;
5990 /* Reference-bashing */
5991 if (TREE_CODE (lhstype
) == REFERENCE_TYPE
)
5993 tree tmp
= convert_from_reference (lhs
);
5994 lhstype
= TREE_TYPE (tmp
);
5995 if (TYPE_SIZE (lhstype
) == 0)
5997 incomplete_type_error (lhs
, lhstype
);
5998 return error_mark_node
;
6003 if (TREE_CODE (TREE_TYPE (newrhs
)) == REFERENCE_TYPE
)
6005 tree tmp
= convert_from_reference (newrhs
);
6006 if (TYPE_SIZE (TREE_TYPE (tmp
)) == 0)
6008 incomplete_type_error (newrhs
, TREE_TYPE (tmp
));
6009 return error_mark_node
;
6015 if (TREE_SIDE_EFFECTS (lhs
))
6016 lhs
= stabilize_reference (lhs
);
6017 if (TREE_SIDE_EFFECTS (newrhs
))
6018 newrhs
= stabilize_reference (newrhs
);
6020 /* Convert new value to destination type. */
6022 if (TREE_CODE (lhstype
) == ARRAY_TYPE
)
6026 if (!same_or_base_type_p (lhstype
, TREE_TYPE (rhs
)))
6028 cp_error ("incompatible types in assignment of `%T' to `%T'",
6029 TREE_TYPE (rhs
), lhstype
);
6030 return error_mark_node
;
6033 /* Allow array assignment in compiler-generated code. */
6034 if (pedantic
&& ! DECL_ARTIFICIAL (current_function_decl
))
6035 pedwarn ("ANSI C++ forbids assignment of arrays");
6037 /* Have to wrap this in RTL_EXPR for two cases:
6038 in base or member initialization and if we
6039 are a branch of a ?: operator. Since we
6040 can't easily know the latter, just do it always. */
6042 result
= make_node (RTL_EXPR
);
6044 TREE_TYPE (result
) = void_type_node
;
6045 do_pending_stack_adjust ();
6046 start_sequence_for_rtl_expr (result
);
6048 /* As a matter of principle, `start_sequence' should do this. */
6051 from_array
= TREE_CODE (TREE_TYPE (newrhs
)) == ARRAY_TYPE
6052 ? 1 + (modifycode
!= INIT_EXPR
): 0;
6053 expand_vec_init (lhs
, lhs
, array_type_nelts (lhstype
), newrhs
,
6056 do_pending_stack_adjust ();
6058 TREE_SIDE_EFFECTS (result
) = 1;
6059 RTL_EXPR_SEQUENCE (result
) = get_insns ();
6060 RTL_EXPR_RTL (result
) = const0_rtx
;
6065 if (modifycode
== INIT_EXPR
)
6067 newrhs
= convert_for_initialization (lhs
, lhstype
, newrhs
, LOOKUP_NORMAL
,
6068 "assignment", NULL_TREE
, 0);
6069 if (lhs
== DECL_RESULT (current_function_decl
))
6071 if (DECL_INITIAL (lhs
))
6072 warning ("return value from function receives multiple initializations");
6073 DECL_INITIAL (lhs
) = newrhs
;
6078 /* Avoid warnings on enum bit fields. */
6079 if (TREE_CODE (olhstype
) == ENUMERAL_TYPE
6080 && TREE_CODE (lhstype
) == INTEGER_TYPE
)
6082 newrhs
= convert_for_assignment (olhstype
, newrhs
, "assignment",
6084 newrhs
= convert_force (lhstype
, newrhs
, 0);
6087 newrhs
= convert_for_assignment (lhstype
, newrhs
, "assignment",
6089 if (TREE_CODE (newrhs
) == CALL_EXPR
6090 && TYPE_NEEDS_CONSTRUCTING (lhstype
))
6091 newrhs
= build_cplus_new (lhstype
, newrhs
);
6093 /* Can't initialize directly from a TARGET_EXPR, since that would
6094 cause the lhs to be constructed twice, and possibly result in
6095 accidental self-initialization. So we force the TARGET_EXPR to be
6096 expanded without a target. */
6097 if (TREE_CODE (newrhs
) == TARGET_EXPR
)
6098 newrhs
= build (COMPOUND_EXPR
, TREE_TYPE (newrhs
), newrhs
,
6099 TREE_OPERAND (newrhs
, 0));
6102 if (newrhs
== error_mark_node
)
6103 return error_mark_node
;
6105 if (TREE_CODE (newrhs
) == COND_EXPR
)
6108 tree cond
= TREE_OPERAND (newrhs
, 0);
6110 if (TREE_SIDE_EFFECTS (lhs
))
6111 cond
= build_compound_expr (tree_cons
6113 build_expr_list (NULL_TREE
, cond
)));
6115 /* Cannot have two identical lhs on this one tree (result) as preexpand
6116 calls will rip them out and fill in RTL for them, but when the
6117 rtl is generated, the calls will only be in the first side of the
6118 condition, not on both, or before the conditional jump! (mrs) */
6119 lhs1
= break_out_calls (lhs
);
6122 /* If there's no change, the COND_EXPR behaves like any other rhs. */
6123 result
= build (modifycode
== NOP_EXPR
? MODIFY_EXPR
: INIT_EXPR
,
6124 lhstype
, lhs
, newrhs
);
6127 tree result_type
= TREE_TYPE (newrhs
);
6128 /* We have to convert each arm to the proper type because the
6129 types may have been munged by constant folding. */
6131 = build (COND_EXPR
, result_type
, cond
,
6132 build_modify_expr (lhs
, modifycode
,
6133 cp_convert (result_type
,
6134 TREE_OPERAND (newrhs
, 1))),
6135 build_modify_expr (lhs1
, modifycode
,
6136 cp_convert (result_type
,
6137 TREE_OPERAND (newrhs
, 2))));
6141 result
= build (modifycode
== NOP_EXPR
? MODIFY_EXPR
: INIT_EXPR
,
6142 lhstype
, lhs
, newrhs
);
6144 TREE_SIDE_EFFECTS (result
) = 1;
6146 /* If we got the LHS in a different type for storing in,
6147 convert the result back to the nominal type of LHS
6148 so that the value we return always has the same type
6149 as the LHS argument. */
6151 if (olhstype
== TREE_TYPE (result
))
6153 /* Avoid warnings converting integral types back into enums
6154 for enum bit fields. */
6155 if (TREE_CODE (TREE_TYPE (result
)) == INTEGER_TYPE
6156 && TREE_CODE (olhstype
) == ENUMERAL_TYPE
)
6158 result
= build (COMPOUND_EXPR
, olhstype
, result
, olhs
);
6159 TREE_NO_UNUSED_WARNING (result
) = 1;
6162 return convert_for_assignment (olhstype
, result
, "assignment",
6167 build_x_modify_expr (lhs
, modifycode
, rhs
)
6169 enum tree_code modifycode
;
6172 if (processing_template_decl
)
6173 return build_min_nt (MODOP_EXPR
, lhs
,
6174 build_min_nt (modifycode
, NULL_TREE
, NULL_TREE
), rhs
);
6176 if (modifycode
!= NOP_EXPR
)
6178 tree rval
= build_opfncall (MODIFY_EXPR
, LOOKUP_NORMAL
, lhs
, rhs
,
6179 make_node (modifycode
));
6183 return build_modify_expr (lhs
, modifycode
, rhs
);
6187 /* Get difference in deltas for different pointer to member function
6188 types. Return integer_zero_node, if FROM cannot be converted to a
6189 TO type. If FORCE is true, then allow reverse conversions as well. */
6192 get_delta_difference (from
, to
, force
)
6196 tree delta
= integer_zero_node
;
6202 /* Should get_base_distance here, so we can check if any thing along the
6203 path is virtual, and we need to make sure we stay
6204 inside the real binfos when going through virtual bases.
6205 Maybe we should replace virtual bases with
6206 binfo_member (...CLASSTYPE_VBASECLASSES...)... (mrs) */
6207 binfo
= get_binfo (from
, to
, 1);
6208 if (binfo
== error_mark_node
)
6210 error (" in pointer to member function conversion");
6217 error_not_base_type (from
, to
);
6218 error (" in pointer to member conversion");
6221 binfo
= get_binfo (to
, from
, 1);
6222 if (binfo
== 0 || binfo
== error_mark_node
)
6224 if (TREE_VIA_VIRTUAL (binfo
))
6226 binfo
= binfo_member (BINFO_TYPE (binfo
),
6227 CLASSTYPE_VBASECLASSES (from
));
6228 cp_warning ("pointer to member cast to virtual base `%T'",
6229 BINFO_TYPE (binfo
));
6230 warning (" will only work if you are very careful");
6232 delta
= BINFO_OFFSET (binfo
);
6233 delta
= cp_convert (ptrdiff_type_node
, delta
);
6235 return build_binary_op (MINUS_EXPR
,
6240 if (TREE_VIA_VIRTUAL (binfo
))
6244 cp_warning ("pointer to member cast from virtual base `%T'",
6245 BINFO_TYPE (binfo
));
6246 warning (" will only work if you are very careful");
6249 cp_error ("pointer to member conversion from virtual base `%T'",
6250 BINFO_TYPE (binfo
));
6253 return BINFO_OFFSET (binfo
);
6257 build_ptrmemfunc1 (type
, delta
, idx
, pfn
, delta2
)
6258 tree type
, delta
, idx
, pfn
, delta2
;
6263 /* This is the old way we did it. We want to avoid calling
6264 digest_init, so that it can give an error if we use { } when
6265 initializing a pointer to member function. */
6269 u
= build_nt (CONSTRUCTOR
, NULL_TREE
,
6270 expr_tree_cons (pfn_identifier
, pfn
, NULL_TREE
));
6274 u
= build_nt (CONSTRUCTOR
, NULL_TREE
,
6275 expr_tree_cons (delta2_identifier
, delta2
, NULL_TREE
));
6278 u
= build_nt (CONSTRUCTOR
, NULL_TREE
,
6279 expr_tree_cons (NULL_TREE
, delta
,
6280 expr_tree_cons (NULL_TREE
, idx
,
6281 expr_tree_cons (NULL_TREE
, u
, NULL_TREE
))));
6283 return digest_init (type
, u
, (tree
*)0);
6285 tree delta_field
, idx_field
, pfn_or_delta2_field
, pfn_field
, delta2_field
;
6287 int allconstant
, allsimple
;
6289 delta_field
= TYPE_FIELDS (type
);
6290 idx_field
= TREE_CHAIN (delta_field
);
6291 pfn_or_delta2_field
= TREE_CHAIN (idx_field
);
6292 subtype
= TREE_TYPE (pfn_or_delta2_field
);
6293 pfn_field
= TYPE_FIELDS (subtype
);
6294 delta2_field
= TREE_CHAIN (pfn_field
);
6298 allconstant
= TREE_CONSTANT (pfn
);
6299 allsimple
= !! initializer_constant_valid_p (pfn
, TREE_TYPE (pfn
));
6300 u
= expr_tree_cons (pfn_field
, pfn
, NULL_TREE
);
6304 delta2
= convert_and_check (delta_type_node
, delta2
);
6305 allconstant
= TREE_CONSTANT (delta2
);
6306 allsimple
= !! initializer_constant_valid_p (delta2
, TREE_TYPE (delta2
));
6307 u
= expr_tree_cons (delta2_field
, delta2
, NULL_TREE
);
6310 delta
= convert_and_check (delta_type_node
, delta
);
6311 idx
= convert_and_check (delta_type_node
, idx
);
6313 allconstant
= allconstant
&& TREE_CONSTANT (delta
) && TREE_CONSTANT (idx
);
6314 allsimple
= allsimple
6315 && initializer_constant_valid_p (delta
, TREE_TYPE (delta
))
6316 && initializer_constant_valid_p (idx
, TREE_TYPE (idx
));
6318 u
= build (CONSTRUCTOR
, subtype
, NULL_TREE
, u
);
6319 u
= expr_tree_cons (delta_field
, delta
,
6320 expr_tree_cons (idx_field
, idx
,
6321 expr_tree_cons (pfn_or_delta2_field
, u
, NULL_TREE
)));
6322 u
= build (CONSTRUCTOR
, type
, NULL_TREE
, u
);
6323 TREE_CONSTANT (u
) = allconstant
;
6324 TREE_STATIC (u
) = allconstant
&& allsimple
;
6329 /* Build a constructor for a pointer to member function. It can be
6330 used to initialize global variables, local variable, or used
6331 as a value in expressions. TYPE is the POINTER to METHOD_TYPE we
6334 If FORCE is non-zero, then force this conversion, even if
6335 we would rather not do it. Usually set when using an explicit
6338 Return error_mark_node, if something goes wrong. */
6341 build_ptrmemfunc (type
, pfn
, force
)
6345 tree idx
= integer_zero_node
;
6346 tree delta
= integer_zero_node
;
6347 tree delta2
= integer_zero_node
;
6349 tree npfn
= NULL_TREE
;
6351 /* Handle multiple conversions of pointer to member functions. */
6352 if (TYPE_PTRMEMFUNC_P (TREE_TYPE (pfn
)))
6354 tree ndelta
, ndelta2
;
6358 /* Is is already the right type? */
6359 if (type
== TYPE_PTRMEMFUNC_FN_TYPE (TREE_TYPE (pfn
)))
6362 pfn_type
= TYPE_PTRMEMFUNC_FN_TYPE (TREE_TYPE (pfn
));
6364 && comp_target_types (type
, pfn_type
, 1) != 1)
6365 cp_error ("conversion to `%T' from `%T'", type
, pfn_type
);
6367 ndelta
= cp_convert (ptrdiff_type_node
, build_component_ref (pfn
, delta_identifier
, NULL_TREE
, 0));
6368 ndelta2
= cp_convert (ptrdiff_type_node
, DELTA2_FROM_PTRMEMFUNC (pfn
));
6369 idx
= build_component_ref (pfn
, index_identifier
, NULL_TREE
, 0);
6371 n
= get_delta_difference (TYPE_METHOD_BASETYPE (TREE_TYPE (pfn_type
)),
6372 TYPE_METHOD_BASETYPE (TREE_TYPE (type
)),
6375 delta
= build_binary_op (PLUS_EXPR
, ndelta
, n
, 1);
6376 delta2
= build_binary_op (PLUS_EXPR
, ndelta2
, n
, 1);
6377 e1
= fold (build (GT_EXPR
, boolean_type_node
, idx
, integer_zero_node
));
6379 e2
= build_ptrmemfunc1 (TYPE_GET_PTRMEMFUNC_TYPE (type
), delta
, idx
,
6382 pfn
= PFN_FROM_PTRMEMFUNC (pfn
);
6383 npfn
= build1 (NOP_EXPR
, type
, pfn
);
6384 TREE_CONSTANT (npfn
) = TREE_CONSTANT (pfn
);
6386 e3
= build_ptrmemfunc1 (TYPE_GET_PTRMEMFUNC_TYPE (type
), delta
, idx
, npfn
,
6388 return build_conditional_expr (e1
, e2
, e3
);
6391 /* Handle null pointer to member function conversions. */
6392 if (integer_zerop (pfn
))
6394 pfn
= build_c_cast (type
, integer_zero_node
);
6395 return build_ptrmemfunc1 (TYPE_GET_PTRMEMFUNC_TYPE (type
),
6396 integer_zero_node
, integer_zero_node
,
6400 if (type_unknown_p (pfn
))
6401 return instantiate_type (type
, pfn
, 1);
6404 && comp_target_types (type
, TREE_TYPE (pfn
), 0) != 1)
6405 cp_error ("conversion to `%T' from `%T'", type
, TREE_TYPE (pfn
));
6407 /* Allow pointer to member conversions here. */
6408 delta
= get_delta_difference (TYPE_METHOD_BASETYPE (TREE_TYPE (TREE_TYPE (pfn
))),
6409 TYPE_METHOD_BASETYPE (TREE_TYPE (type
)),
6411 delta2
= build_binary_op (PLUS_EXPR
, delta2
, delta
, 1);
6413 if (TREE_CODE (TREE_OPERAND (pfn
, 0)) != FUNCTION_DECL
)
6414 warning ("assuming pointer to member function is non-virtual");
6416 if (TREE_CODE (TREE_OPERAND (pfn
, 0)) == FUNCTION_DECL
6417 && DECL_VINDEX (TREE_OPERAND (pfn
, 0)))
6419 /* Find the offset to the vfield pointer in the object. */
6420 vfield_offset
= get_binfo (DECL_CONTEXT (TREE_OPERAND (pfn
, 0)),
6421 DECL_CLASS_CONTEXT (TREE_OPERAND (pfn
, 0)),
6423 vfield_offset
= get_vfield_offset (vfield_offset
);
6424 delta2
= size_binop (PLUS_EXPR
, vfield_offset
, delta2
);
6426 /* Map everything down one to make room for the null pointer to member. */
6427 idx
= size_binop (PLUS_EXPR
,
6428 DECL_VINDEX (TREE_OPERAND (pfn
, 0)),
6433 idx
= size_binop (MINUS_EXPR
, integer_zero_node
, integer_one_node
);
6435 if (type
== TREE_TYPE (pfn
))
6441 npfn
= build1 (NOP_EXPR
, type
, pfn
);
6442 TREE_CONSTANT (npfn
) = TREE_CONSTANT (pfn
);
6446 return build_ptrmemfunc1 (TYPE_GET_PTRMEMFUNC_TYPE (type
), delta
, idx
, npfn
, delta2
);
6449 /* Convert value RHS to type TYPE as preparation for an assignment
6450 to an lvalue of type TYPE.
6451 The real work of conversion is done by `convert'.
6452 The purpose of this function is to generate error messages
6453 for assignments that are not allowed in C.
6454 ERRTYPE is a string to use in error messages:
6455 "assignment", "return", etc.
6457 C++: attempts to allow `convert' to find conversions involving
6458 implicit type conversion between aggregate and scalar types
6459 as per 8.5.6 of C++ manual. Does not randomly dereference
6460 pointers to aggregates! */
6463 convert_for_assignment (type
, rhs
, errtype
, fndecl
, parmnum
)
6469 register enum tree_code codel
= TREE_CODE (type
);
6470 register tree rhstype
;
6471 register enum tree_code coder
= TREE_CODE (TREE_TYPE (rhs
));
6473 /* Issue warnings about peculiar, but legal, uses of NULL. */
6474 if (ARITHMETIC_TYPE_P (type
) && rhs
== null_node
)
6475 cp_warning ("converting NULL to non-pointer type");
6477 if (coder
== ERROR_MARK
)
6478 return error_mark_node
;
6480 if (codel
== OFFSET_TYPE
)
6482 type
= TREE_TYPE (type
);
6483 codel
= TREE_CODE (type
);
6486 /* Strip NON_LVALUE_EXPRs since we aren't using as an lvalue. */
6487 if (TREE_CODE (rhs
) == NON_LVALUE_EXPR
)
6488 rhs
= TREE_OPERAND (rhs
, 0);
6490 if (rhs
== error_mark_node
)
6491 return error_mark_node
;
6493 if (TREE_CODE (rhs
) == TREE_LIST
&& TREE_VALUE (rhs
) == error_mark_node
)
6494 return error_mark_node
;
6496 if (TREE_CODE (TREE_TYPE (rhs
)) == OFFSET_TYPE
)
6498 rhs
= resolve_offset_ref (rhs
);
6499 if (rhs
== error_mark_node
)
6500 return error_mark_node
;
6501 rhstype
= TREE_TYPE (rhs
);
6502 coder
= TREE_CODE (rhstype
);
6505 if (TREE_CODE (TREE_TYPE (rhs
)) == ARRAY_TYPE
6506 || is_overloaded_fn (rhs
))
6507 rhs
= default_conversion (rhs
);
6508 else if (TREE_CODE (TREE_TYPE (rhs
)) == REFERENCE_TYPE
)
6509 rhs
= convert_from_reference (rhs
);
6511 /* If rhs is some sort of overloaded function, ocp_convert will either
6512 do the right thing or complain; we don't need to check anything else.
6513 So just hand off. */
6514 if (type_unknown_p (rhs
))
6515 return ocp_convert (type
, rhs
, CONV_IMPLICIT
, LOOKUP_NORMAL
);
6517 rhstype
= TREE_TYPE (rhs
);
6518 coder
= TREE_CODE (rhstype
);
6520 /* This should no longer change types on us. */
6521 if (TREE_CODE (rhs
) == CONST_DECL
)
6522 rhs
= DECL_INITIAL (rhs
);
6523 else if (TREE_READONLY_DECL_P (rhs
))
6524 rhs
= decl_constant_value (rhs
);
6526 if (same_type_p (type
, rhstype
))
6528 overflow_warning (rhs
);
6532 if (coder
== VOID_TYPE
)
6534 error ("void value not ignored as it ought to be");
6535 return error_mark_node
;
6537 /* Arithmetic types all interconvert. */
6538 if ((codel
== INTEGER_TYPE
|| codel
== REAL_TYPE
|| codel
== BOOLEAN_TYPE
6539 || codel
== COMPLEX_TYPE
)
6540 && (coder
== INTEGER_TYPE
|| coder
== REAL_TYPE
|| coder
== BOOLEAN_TYPE
6541 || coder
== COMPLEX_TYPE
))
6543 /* But we should warn if assigning REAL_TYPE to INTEGER_TYPE. */
6544 if (coder
== REAL_TYPE
&& codel
== INTEGER_TYPE
)
6547 cp_warning ("`%T' used for argument %P of `%D'",
6548 rhstype
, parmnum
, fndecl
);
6550 cp_warning ("%s to `%T' from `%T'", errtype
, type
, rhstype
);
6552 /* And we should warn if assigning a negative value to
6553 an unsigned variable. */
6554 else if (TREE_UNSIGNED (type
) && codel
!= BOOLEAN_TYPE
)
6556 if (TREE_CODE (rhs
) == INTEGER_CST
6557 && TREE_NEGATED_INT (rhs
))
6560 cp_warning ("negative value `%E' passed as argument %P of `%D'",
6561 rhs
, parmnum
, fndecl
);
6563 cp_warning ("%s of negative value `%E' to `%T'",
6564 errtype
, rhs
, type
);
6566 overflow_warning (rhs
);
6567 if (TREE_CONSTANT (rhs
))
6571 return convert_and_check (type
, rhs
);
6573 /* Conversions involving enums. */
6574 else if ((codel
== ENUMERAL_TYPE
6575 && (INTEGRAL_CODE_P (coder
) || coder
== REAL_TYPE
))
6576 || (coder
== ENUMERAL_TYPE
6577 && (INTEGRAL_CODE_P (codel
) || codel
== REAL_TYPE
)))
6579 return ocp_convert (type
, rhs
, CONV_IMPLICIT
, LOOKUP_NORMAL
);
6581 /* Conversions among pointers */
6582 else if (codel
== POINTER_TYPE
6583 && (coder
== POINTER_TYPE
6584 || (coder
== RECORD_TYPE
6585 && (IS_SIGNATURE_POINTER (rhstype
)
6586 || IS_SIGNATURE_REFERENCE (rhstype
)))))
6588 register tree ttl
= TREE_TYPE (type
);
6592 if (coder
== RECORD_TYPE
)
6594 rhs
= build_optr_ref (rhs
);
6595 rhstype
= TREE_TYPE (rhs
);
6597 ttr
= TREE_TYPE (rhstype
);
6599 /* If both pointers are of aggregate type, then we
6600 can give better error messages, and save some work
6602 if (TREE_CODE (ttl
) == RECORD_TYPE
&& TREE_CODE (ttr
) == RECORD_TYPE
)
6606 if (TYPE_MAIN_VARIANT (ttl
) == TYPE_MAIN_VARIANT (ttr
)
6607 || type
== class_star_type_node
6608 || rhstype
== class_star_type_node
)
6609 binfo
= TYPE_BINFO (ttl
);
6611 binfo
= get_binfo (ttl
, ttr
, 1);
6613 if (binfo
== error_mark_node
)
6614 return error_mark_node
;
6616 return error_not_base_type (ttl
, ttr
);
6618 if (!at_least_as_qualified_p (ttl
, ttr
))
6621 cp_pedwarn ("passing `%T' as argument %P of `%D' discards qualifiers",
6622 rhstype
, parmnum
, fndecl
);
6624 cp_pedwarn ("%s to `%T' from `%T' discards qualifiers",
6625 errtype
, type
, rhstype
);
6629 /* Any non-function converts to a [const][volatile] void *
6630 and vice versa; otherwise, targets must be the same.
6631 Meanwhile, the lhs target must have all the qualifiers of the rhs. */
6632 else if (TYPE_MAIN_VARIANT (ttl
) == void_type_node
6633 || TYPE_MAIN_VARIANT (ttr
) == void_type_node
6634 || (ctt
= comp_target_types (type
, rhstype
, 1))
6635 || (unsigned_type (TYPE_MAIN_VARIANT (ttl
))
6636 == unsigned_type (TYPE_MAIN_VARIANT (ttr
))))
6638 /* ARM $4.8, commentary on p39. */
6639 if (TYPE_MAIN_VARIANT (ttl
) == void_type_node
6640 && TREE_CODE (ttr
) == OFFSET_TYPE
)
6642 cp_error ("no standard conversion from `%T' to `void *'", ttr
);
6643 return error_mark_node
;
6646 if (ctt
< 0 && TYPE_MAIN_VARIANT (ttl
) != TYPE_MAIN_VARIANT (ttr
))
6647 cp_pedwarn ("converting `%T' to `%T' is a contravariance violation",
6650 if (TYPE_MAIN_VARIANT (ttl
) != void_type_node
6651 && TYPE_MAIN_VARIANT (ttr
) == void_type_node
6652 && ! null_ptr_cst_p (rhs
))
6654 if (coder
== RECORD_TYPE
)
6655 cp_pedwarn ("implicit conversion of signature pointer to type `%T'",
6658 pedwarn ("ANSI C++ forbids implicit conversion from `void *' in %s",
6661 /* Const and volatile mean something different for function types,
6662 so the usual warnings are not appropriate. */
6663 else if ((TREE_CODE (ttr
) != FUNCTION_TYPE
&& TREE_CODE (ttr
) != METHOD_TYPE
)
6664 || (TREE_CODE (ttl
) != FUNCTION_TYPE
&& TREE_CODE (ttl
) != METHOD_TYPE
))
6666 if (TREE_CODE (ttl
) == OFFSET_TYPE
6667 && binfo_member (TYPE_OFFSET_BASETYPE (ttr
),
6668 CLASSTYPE_VBASECLASSES (TYPE_OFFSET_BASETYPE (ttl
))))
6670 error ("%s between pointer to members converting across virtual baseclasses", errtype
);
6671 return error_mark_node
;
6673 else if (!at_least_as_qualified_p (ttl
, ttr
))
6675 if (string_conv_p (type
, rhs
, 1))
6676 /* converting from string constant to char *, OK. */;
6678 cp_pedwarn ("passing `%T' as argument %P of `%D' discards qualifiers",
6679 rhstype
, parmnum
, fndecl
);
6681 cp_pedwarn ("%s to `%T' from `%T' discards qualifiers",
6682 errtype
, type
, rhstype
);
6684 else if (TREE_CODE (ttl
) == TREE_CODE (ttr
)
6685 && ! comp_target_types (type
, rhstype
, 1))
6688 cp_pedwarn ("passing `%T' as argument %P of `%D' changes signedness",
6689 rhstype
, parmnum
, fndecl
);
6691 cp_pedwarn ("%s to `%T' from `%T' changes signedness",
6692 errtype
, type
, rhstype
);
6699 int drops_quals
= 0;
6701 int unsigned_parity
;
6704 /* This code is basically a duplicate of comp_ptr_ttypes_real. */
6705 for (; ; ttl
= TREE_TYPE (ttl
), ttr
= TREE_TYPE (ttr
))
6708 drops_quals
|= !at_least_as_qualified_p (ttl
, ttr
);
6711 && !at_least_as_qualified_p (ttr
, ttl
))
6713 left_const
&= TYPE_READONLY (ttl
);
6715 if (TREE_CODE (ttl
) != POINTER_TYPE
6716 || TREE_CODE (ttr
) != POINTER_TYPE
)
6719 unsigned_parity
= TREE_UNSIGNED (ttl
) - TREE_UNSIGNED (ttr
);
6720 if (unsigned_parity
)
6722 if (TREE_UNSIGNED (ttl
))
6723 ttr
= unsigned_type (ttr
);
6725 ttl
= unsigned_type (ttl
);
6728 if (comp_target_types (ttl
, ttr
, nptrs
) > 0)
6733 cp_pedwarn ("passing `%T' as argument %P of `%D' adds cv-quals without intervening `const'",
6734 rhstype
, parmnum
, fndecl
);
6736 cp_pedwarn ("%s to `%T' from `%T' adds cv-quals without intervening `const'",
6737 errtype
, type
, rhstype
);
6742 cp_pedwarn ("passing `%T' as argument %P of `%D' discards qualifiers",
6743 rhstype
, parmnum
, fndecl
);
6745 cp_pedwarn ("%s to `%T' from `%T' discards qualifiers",
6746 errtype
, type
, rhstype
);
6748 if (unsigned_parity
> 0)
6751 cp_pedwarn ("passing `%T' as argument %P of `%D' changes signed to unsigned",
6752 rhstype
, parmnum
, fndecl
);
6754 cp_pedwarn ("%s to `%T' from `%T' changes signed to unsigned",
6755 errtype
, type
, rhstype
);
6757 else if (unsigned_parity
< 0)
6760 cp_pedwarn ("passing `%T' as argument %P of `%D' changes unsigned to signed",
6761 rhstype
, parmnum
, fndecl
);
6763 cp_pedwarn ("%s to `%T' from `%T' changes unsigned to signed",
6764 errtype
, type
, rhstype
);
6767 /* C++ is not so friendly about converting function and
6768 member function pointers as C. Emit warnings here. */
6769 if (TREE_CODE (ttl
) == FUNCTION_TYPE
6770 || TREE_CODE (ttl
) == METHOD_TYPE
)
6771 if (!same_or_base_type_p (ttl
, ttr
))
6773 warning ("conflicting function types in %s:", errtype
);
6774 cp_warning ("\t`%T' != `%T'", type
, rhstype
);
6780 cp_error ("passing `%T' as argument %P of `%D'",
6781 rhstype
, parmnum
, fndecl
);
6783 cp_error ("%s to `%T' from `%T'", errtype
, type
, rhstype
);
6784 return error_mark_node
;
6787 return cp_convert (type
, rhs
);
6789 else if (codel
== POINTER_TYPE
&& coder
== INTEGER_TYPE
)
6791 /* An explicit constant 0 can convert to a pointer,
6792 but not a 0 that results from casting or folding. */
6793 if (! (TREE_CODE (rhs
) == INTEGER_CST
&& integer_zerop (rhs
)))
6796 cp_pedwarn ("passing `%T' to argument %P of `%D' lacks a cast",
6797 rhstype
, parmnum
, fndecl
);
6799 cp_pedwarn ("%s to `%T' from `%T' lacks a cast",
6800 errtype
, type
, rhstype
);
6802 return cp_convert (type
, rhs
);
6804 else if (codel
== INTEGER_TYPE
6805 && (coder
== POINTER_TYPE
6806 || (coder
== RECORD_TYPE
6807 && (IS_SIGNATURE_POINTER (rhstype
)
6808 || TYPE_PTRMEMFUNC_FLAG (rhstype
)
6809 || IS_SIGNATURE_REFERENCE (rhstype
)))))
6812 cp_pedwarn ("passing `%T' to argument %P of `%D' lacks a cast",
6813 rhstype
, parmnum
, fndecl
);
6815 cp_pedwarn ("%s to `%T' from `%T' lacks a cast",
6816 errtype
, type
, rhstype
);
6817 return cp_convert (type
, rhs
);
6819 else if (codel
== BOOLEAN_TYPE
6820 && (coder
== POINTER_TYPE
6821 || (coder
== RECORD_TYPE
6822 && (IS_SIGNATURE_POINTER (rhstype
)
6823 || TYPE_PTRMEMFUNC_FLAG (rhstype
)
6824 || IS_SIGNATURE_REFERENCE (rhstype
)))))
6825 return cp_convert (type
, rhs
);
6828 else if (((coder
== POINTER_TYPE
6829 && TREE_CODE (TREE_TYPE (rhstype
)) == METHOD_TYPE
)
6830 || integer_zerop (rhs
)
6831 || TYPE_PTRMEMFUNC_P (rhstype
))
6832 && TYPE_PTRMEMFUNC_P (type
))
6834 tree ttl
= TYPE_PTRMEMFUNC_FN_TYPE (type
);
6835 tree ttr
= (TYPE_PTRMEMFUNC_P (rhstype
)
6836 ? TYPE_PTRMEMFUNC_FN_TYPE (rhstype
)
6838 int ctt
= (TREE_CODE (rhstype
) == INTEGER_TYPE
? 1
6839 : comp_target_types (ttl
, ttr
, 1));
6842 cp_pedwarn ("converting `%T' to `%T' is a contravariance violation",
6845 cp_error ("%s to `%T' from `%T'", errtype
, ttl
, ttr
);
6847 /* compatible pointer to member functions. */
6848 return build_ptrmemfunc (ttl
, rhs
, 0);
6850 else if (codel
== ERROR_MARK
|| coder
== ERROR_MARK
)
6851 return error_mark_node
;
6853 /* This should no longer happen. References are initialized via
6854 `convert_for_initialization'. They should otherwise be
6855 bashed before coming here. */
6856 else if (codel
== REFERENCE_TYPE
)
6857 my_friendly_abort (317);
6858 else if (TYPE_MAIN_VARIANT (type
) == TYPE_MAIN_VARIANT (TREE_TYPE (rhs
)))
6860 tree nrhs
= build1 (NOP_EXPR
, type
, rhs
);
6861 TREE_CONSTANT (nrhs
) = TREE_CONSTANT (rhs
);
6864 else if (TYPE_HAS_CONSTRUCTOR (type
) || IS_AGGR_TYPE (TREE_TYPE (rhs
)))
6865 return cp_convert (type
, rhs
);
6866 /* Handle anachronistic conversions from (::*)() to cv void* or (*)(). */
6867 else if (TREE_CODE (type
) == POINTER_TYPE
6868 && (TREE_CODE (TREE_TYPE (type
)) == FUNCTION_TYPE
6869 || TYPE_MAIN_VARIANT (TREE_TYPE (type
)) == void_type_node
)
6871 && TYPE_PTRMEMFUNC_P (TREE_TYPE (rhs
)))
6872 return cp_convert (type
, rhs
);
6874 cp_error ("%s to `%T' from `%T'", errtype
, type
, rhstype
);
6875 return error_mark_node
;
6878 /* Convert RHS to be of type TYPE.
6879 If EXP is non-zero, it is the target of the initialization.
6880 ERRTYPE is a string to use in error messages.
6882 Two major differences between the behavior of
6883 `convert_for_assignment' and `convert_for_initialization'
6884 are that references are bashed in the former, while
6885 copied in the latter, and aggregates are assigned in
6886 the former (operator=) while initialized in the
6889 If using constructor make sure no conversion operator exists, if one does
6890 exist, an ambiguity exists.
6892 If flags doesn't include LOOKUP_COMPLAIN, don't complain about anything. */
6895 convert_for_initialization (exp
, type
, rhs
, flags
, errtype
, fndecl
, parmnum
)
6896 tree exp
, type
, rhs
;
6902 register enum tree_code codel
= TREE_CODE (type
);
6903 register tree rhstype
;
6904 register enum tree_code coder
;
6906 /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
6907 Strip such NOP_EXPRs, since RHS is used in non-lvalue context. */
6908 if (TREE_CODE (rhs
) == NOP_EXPR
6909 && TREE_TYPE (rhs
) == TREE_TYPE (TREE_OPERAND (rhs
, 0))
6910 && codel
!= REFERENCE_TYPE
)
6911 rhs
= TREE_OPERAND (rhs
, 0);
6913 if (rhs
== error_mark_node
6914 || (TREE_CODE (rhs
) == TREE_LIST
&& TREE_VALUE (rhs
) == error_mark_node
))
6915 return error_mark_node
;
6917 if (TREE_CODE (TREE_TYPE (rhs
)) == OFFSET_TYPE
)
6919 rhs
= resolve_offset_ref (rhs
);
6920 if (rhs
== error_mark_node
)
6921 return error_mark_node
;
6924 if (TREE_CODE (TREE_TYPE (rhs
)) == REFERENCE_TYPE
)
6925 rhs
= convert_from_reference (rhs
);
6927 if ((TREE_CODE (TREE_TYPE (rhs
)) == ARRAY_TYPE
6928 && TREE_CODE (type
) != ARRAY_TYPE
6929 && (TREE_CODE (type
) != REFERENCE_TYPE
6930 || TREE_CODE (TREE_TYPE (type
)) != ARRAY_TYPE
))
6931 || (TREE_CODE (TREE_TYPE (rhs
)) == FUNCTION_TYPE
6932 && (TREE_CODE (type
) != REFERENCE_TYPE
6933 || TREE_CODE (TREE_TYPE (type
)) != FUNCTION_TYPE
))
6934 || TREE_CODE (TREE_TYPE (rhs
)) == METHOD_TYPE
)
6935 rhs
= default_conversion (rhs
);
6937 rhstype
= TREE_TYPE (rhs
);
6938 coder
= TREE_CODE (rhstype
);
6940 if (coder
== ERROR_MARK
)
6941 return error_mark_node
;
6943 /* We accept references to incomplete types, so we can
6944 return here before checking if RHS is of complete type. */
6946 if (codel
== REFERENCE_TYPE
)
6948 /* This should eventually happen in convert_arguments. */
6949 extern int warningcount
, errorcount
;
6950 int savew
= 0, savee
= 0;
6953 savew
= warningcount
, savee
= errorcount
;
6954 rhs
= convert_to_reference (type
, rhs
, CONV_IMPLICIT
, flags
,
6955 exp
? exp
: error_mark_node
);
6958 if (warningcount
> savew
)
6959 cp_warning_at ("in passing argument %P of `%+D'", parmnum
, fndecl
);
6960 else if (errorcount
> savee
)
6961 cp_error_at ("in passing argument %P of `%+D'", parmnum
, fndecl
);
6966 rhs
= require_complete_type (rhs
);
6967 if (rhs
== error_mark_node
)
6968 return error_mark_node
;
6970 if (exp
!= 0) exp
= require_complete_type (exp
);
6971 if (exp
== error_mark_node
)
6972 return error_mark_node
;
6974 if (TREE_CODE (rhstype
) == REFERENCE_TYPE
)
6975 rhstype
= TREE_TYPE (rhstype
);
6977 type
= complete_type (type
);
6979 if (TYPE_LANG_SPECIFIC (type
)
6980 && (IS_SIGNATURE_POINTER (type
) || IS_SIGNATURE_REFERENCE (type
)))
6981 return build_signature_pointer_constructor (type
, rhs
);
6983 if (IS_AGGR_TYPE (type
))
6984 return ocp_convert (type
, rhs
, CONV_IMPLICIT
|CONV_FORCE_TEMP
, flags
);
6986 if (type
== TREE_TYPE (rhs
))
6988 /* Issue warnings about peculiar, but legal, uses of NULL. We
6989 do this *before* the call to decl_constant_value so as to
6990 avoid duplicate warnings on code like `const int I = NULL;
6992 if (ARITHMETIC_TYPE_P (type
) && rhs
== null_node
)
6993 cp_warning ("converting NULL to non-pointer type");
6995 if (TREE_READONLY_DECL_P (rhs
))
6996 rhs
= decl_constant_value (rhs
);
7001 return convert_for_assignment (type
, rhs
, errtype
, fndecl
, parmnum
);
7004 /* Expand an ASM statement with operands, handling output operands
7005 that are not variables or INDIRECT_REFS by transforming such
7006 cases into cases that expand_asm_operands can handle.
7008 Arguments are same as for expand_asm_operands.
7010 We don't do default conversions on all inputs, because it can screw
7011 up operands that are expected to be in memory. */
7014 c_expand_asm_operands (string
, outputs
, inputs
, clobbers
, vol
, filename
, line
)
7015 tree string
, outputs
, inputs
, clobbers
;
7020 int noutputs
= list_length (outputs
);
7022 /* o[I] is the place that output number I should be written. */
7023 register tree
*o
= (tree
*) alloca (noutputs
* sizeof (tree
));
7026 /* Record the contents of OUTPUTS before it is modified. */
7027 for (i
= 0, tail
= outputs
; tail
; tail
= TREE_CHAIN (tail
), i
++)
7028 o
[i
] = TREE_VALUE (tail
);
7030 /* Generate the ASM_OPERANDS insn;
7031 store into the TREE_VALUEs of OUTPUTS some trees for
7032 where the values were actually stored. */
7033 expand_asm_operands (string
, outputs
, inputs
, clobbers
, vol
, filename
, line
);
7035 /* Copy all the intermediate outputs into the specified outputs. */
7036 for (i
= 0, tail
= outputs
; tail
; tail
= TREE_CHAIN (tail
), i
++)
7038 if (o
[i
] != TREE_VALUE (tail
))
7040 expand_expr (build_modify_expr (o
[i
], NOP_EXPR
, TREE_VALUE (tail
)),
7041 const0_rtx
, VOIDmode
, EXPAND_NORMAL
);
7044 /* Detect modification of read-only values.
7045 (Otherwise done by build_modify_expr.) */
7048 tree type
= TREE_TYPE (o
[i
]);
7049 if (CP_TYPE_CONST_P (type
)
7050 || ((TREE_CODE (type
) == RECORD_TYPE
7051 || TREE_CODE (type
) == UNION_TYPE
)
7052 && C_TYPE_FIELDS_READONLY (type
)))
7053 readonly_error (o
[i
], "modification by `asm'", 1);
7057 /* Those MODIFY_EXPRs could do autoincrements. */
7061 /* Expand a C `return' statement.
7062 RETVAL is the expression for what to return,
7063 or a null pointer for `return;' with no value.
7065 C++: upon seeing a `return', we must call destructors on all
7066 variables in scope which had constructors called on them.
7067 This means that if in a destructor, the base class destructors
7068 must be called before returning.
7070 The RETURN statement in C++ has initialization semantics. */
7073 c_expand_return (retval
)
7076 extern struct nesting
*cond_stack
, *loop_stack
, *case_stack
;
7077 extern tree dtor_label
, ctor_label
;
7078 tree result
= DECL_RESULT (current_function_decl
);
7079 tree valtype
= TREE_TYPE (result
);
7081 if (TREE_THIS_VOLATILE (current_function_decl
))
7082 warning ("function declared `noreturn' has a `return' statement");
7084 if (retval
== error_mark_node
)
7086 current_function_returns_null
= 1;
7090 if (processing_template_decl
)
7092 add_tree (build_min_nt (RETURN_STMT
, retval
));
7099 error ("returning a value from a destructor");
7101 /* Can't just return from a destructor. */
7102 expand_goto (dtor_label
);
7106 if (retval
== NULL_TREE
)
7108 /* A non-named return value does not count. */
7110 if (DECL_CONSTRUCTOR_P (current_function_decl
))
7111 retval
= current_class_ptr
;
7112 else if (DECL_NAME (result
) != NULL_TREE
7113 && TREE_CODE (valtype
) != VOID_TYPE
)
7117 current_function_returns_null
= 1;
7119 if (valtype
!= NULL_TREE
&& TREE_CODE (valtype
) != VOID_TYPE
)
7121 if (DECL_NAME (DECL_RESULT (current_function_decl
)) == NULL_TREE
)
7123 pedwarn ("`return' with no value, in function returning non-void");
7124 /* Clear this, so finish_function won't say that we
7125 reach the end of a non-void function (which we don't,
7126 we gave a return!). */
7127 current_function_returns_null
= 0;
7131 expand_null_return ();
7135 else if (DECL_CONSTRUCTOR_P (current_function_decl
))
7137 if (flag_this_is_variable
)
7138 error ("return from a constructor: use `this = ...' instead");
7140 error ("returning a value from a constructor");
7141 retval
= current_class_ptr
;
7144 /* Effective C++ rule 15. See also start_function. */
7146 && DECL_NAME (current_function_decl
) == ansi_opname
[(int) MODIFY_EXPR
]
7147 && retval
!= current_class_ref
)
7148 cp_warning ("`operator=' should return a reference to `*this'");
7150 if (valtype
== NULL_TREE
|| TREE_CODE (valtype
) == VOID_TYPE
)
7152 current_function_returns_null
= 1;
7153 if (TREE_CODE (TREE_TYPE (retval
)) != VOID_TYPE
)
7154 pedwarn ("`return' with a value, in function returning void");
7155 expand_return (retval
);
7159 /* Now deal with possible C++ hair:
7160 (1) Compute the return value.
7161 (2) If there are aggregate values with destructors which
7162 must be cleaned up, clean them (taking care
7163 not to clobber the return value).
7164 (3) If an X(X&) constructor is defined, the return
7165 value must be returned via that. */
7167 if (retval
== result
7168 || DECL_CONSTRUCTOR_P (current_function_decl
))
7169 /* It's already done for us. */;
7170 else if (TREE_TYPE (retval
) == void_type_node
)
7172 pedwarn ("return of void value in function returning non-void");
7173 expand_expr_stmt (retval
);
7178 tree functype
= TREE_TYPE (TREE_TYPE (current_function_decl
));
7180 /* First convert the value to the function's return type, then
7181 to the type of return value's location to handle the
7182 case that functype is thiner than the valtype. */
7184 retval
= convert_for_initialization
7185 (NULL_TREE
, functype
, retval
, LOOKUP_NORMAL
|LOOKUP_ONLYCONVERTING
,
7186 "return", NULL_TREE
, 0);
7188 retval
= convert (valtype
, retval
);
7190 if (retval
== error_mark_node
)
7192 /* Avoid warning about control reaching end of function. */
7193 expand_null_return ();
7197 /* We can't initialize a register from a AGGR_INIT_EXPR. */
7198 else if (! current_function_returns_struct
7199 && TREE_CODE (retval
) == TARGET_EXPR
7200 && TREE_CODE (TREE_OPERAND (retval
, 1)) == AGGR_INIT_EXPR
)
7201 retval
= build (COMPOUND_EXPR
, TREE_TYPE (retval
), retval
,
7202 TREE_OPERAND (retval
, 0));
7204 /* Add some useful error checking for C++. */
7205 else if (TREE_CODE (valtype
) == REFERENCE_TYPE
)
7207 tree whats_returned
;
7209 /* Sort through common things to see what it is
7210 we are returning. */
7211 whats_returned
= retval
;
7212 if (TREE_CODE (whats_returned
) == COMPOUND_EXPR
)
7214 whats_returned
= TREE_OPERAND (whats_returned
, 1);
7215 if (TREE_CODE (whats_returned
) == ADDR_EXPR
)
7216 whats_returned
= TREE_OPERAND (whats_returned
, 0);
7218 while (TREE_CODE (whats_returned
) == CONVERT_EXPR
7219 || TREE_CODE (whats_returned
) == NOP_EXPR
)
7220 whats_returned
= TREE_OPERAND (whats_returned
, 0);
7221 if (TREE_CODE (whats_returned
) == ADDR_EXPR
)
7223 whats_returned
= TREE_OPERAND (whats_returned
, 0);
7224 while (TREE_CODE (whats_returned
) == AGGR_INIT_EXPR
7225 || TREE_CODE (whats_returned
) == TARGET_EXPR
)
7227 /* Get the target. */
7228 whats_returned
= TREE_OPERAND (whats_returned
, 0);
7229 warning ("returning reference to temporary");
7233 if (TREE_CODE (whats_returned
) == VAR_DECL
&& DECL_NAME (whats_returned
))
7235 if (TEMP_NAME_P (DECL_NAME (whats_returned
)))
7236 warning ("reference to non-lvalue returned");
7237 else if (TREE_CODE (TREE_TYPE (whats_returned
)) != REFERENCE_TYPE
7238 && DECL_FUNCTION_SCOPE_P (whats_returned
)
7239 && !(TREE_STATIC (whats_returned
)
7240 || TREE_PUBLIC (whats_returned
)))
7241 cp_warning_at ("reference to local variable `%D' returned", whats_returned
);
7244 else if (TREE_CODE (retval
) == ADDR_EXPR
)
7246 tree whats_returned
= TREE_OPERAND (retval
, 0);
7248 if (TREE_CODE (whats_returned
) == VAR_DECL
7249 && DECL_NAME (whats_returned
)
7250 && DECL_FUNCTION_SCOPE_P (whats_returned
)
7251 && !(TREE_STATIC (whats_returned
)
7252 || TREE_PUBLIC (whats_returned
)))
7253 cp_warning_at ("address of local variable `%D' returned", whats_returned
);
7257 if (retval
!= NULL_TREE
7258 && TREE_CODE_CLASS (TREE_CODE (retval
)) == 'd'
7259 && cond_stack
== 0 && loop_stack
== 0 && case_stack
== 0)
7260 current_function_return_value
= retval
;
7262 if (ctor_label
&& TREE_CODE (ctor_label
) != ERROR_MARK
)
7264 /* Here RETVAL is CURRENT_CLASS_PTR, so there's nothing to do. */
7265 expand_goto (ctor_label
);
7268 if (retval
&& retval
!= result
)
7270 result
= build (INIT_EXPR
, TREE_TYPE (result
), result
, retval
);
7271 TREE_SIDE_EFFECTS (result
) = 1;
7274 expand_start_target_temps ();
7276 expand_return (result
);
7278 expand_end_target_temps ();
7280 current_function_returns_value
= 1;
7283 /* Start a C switch statement, testing expression EXP.
7284 Return EXP if it is valid, an error node otherwise. */
7287 c_expand_start_case (exp
)
7292 exp
= build_expr_type_conversion (WANT_INT
| WANT_ENUM
, exp
, 1);
7293 if (exp
== NULL_TREE
)
7295 error ("switch quantity not an integer");
7296 exp
= error_mark_node
;
7298 if (exp
== error_mark_node
)
7299 return error_mark_node
;
7301 exp
= default_conversion (exp
);
7302 type
= TREE_TYPE (exp
);
7303 idx
= get_unwidened (exp
, 0);
7304 /* We can't strip a conversion from a signed type to an unsigned,
7305 because if we did, int_fits_type_p would do the wrong thing
7306 when checking case values for being in range,
7307 and it's too hard to do the right thing. */
7308 if (TREE_UNSIGNED (TREE_TYPE (exp
)) == TREE_UNSIGNED (TREE_TYPE (idx
)))
7312 (1, fold (build1 (CLEANUP_POINT_EXPR
, TREE_TYPE (exp
), exp
)),
7313 type
, "switch statement");
7318 /* Returns non-zero if the pointer-type FROM can be converted to the
7319 pointer-type TO via a qualification conversion. If CONSTP is -1,
7320 then we return non-zero if the pointers are similar, and the
7321 cv-qualification signature of FROM is a proper subset of that of TO.
7323 If CONSTP is positive, then all outer pointers have been
7327 comp_ptr_ttypes_real (to
, from
, constp
)
7331 int to_more_cv_qualified
= 0;
7333 for (; ; to
= TREE_TYPE (to
), from
= TREE_TYPE (from
))
7335 if (TREE_CODE (to
) != TREE_CODE (from
))
7338 if (TREE_CODE (from
) == OFFSET_TYPE
7339 && same_type_p (TYPE_OFFSET_BASETYPE (from
),
7340 TYPE_OFFSET_BASETYPE (to
)))
7343 /* Const and volatile mean something different for function types,
7344 so the usual checks are not appropriate. */
7345 if (TREE_CODE (to
) != FUNCTION_TYPE
&& TREE_CODE (to
) != METHOD_TYPE
)
7347 if (!at_least_as_qualified_p (to
, from
))
7350 if (!at_least_as_qualified_p (from
, to
))
7355 ++to_more_cv_qualified
;
7359 constp
&= TYPE_READONLY (to
);
7362 if (TREE_CODE (to
) != POINTER_TYPE
)
7364 same_type_p (TYPE_MAIN_VARIANT (to
), TYPE_MAIN_VARIANT (from
))
7365 && (constp
>= 0 || to_more_cv_qualified
);
7369 /* When comparing, say, char ** to char const **, this function takes the
7370 'char *' and 'char const *'. Do not pass non-pointer types to this
7374 comp_ptr_ttypes (to
, from
)
7377 return comp_ptr_ttypes_real (to
, from
, 1);
7380 /* Returns 1 if to and from are (possibly multi-level) pointers to the same
7381 type or inheritance-related types, regardless of cv-quals. */
7384 ptr_reasonably_similar (to
, from
)
7387 for (; ; to
= TREE_TYPE (to
), from
= TREE_TYPE (from
))
7389 if (TREE_CODE (to
) != TREE_CODE (from
))
7392 if (TREE_CODE (from
) == OFFSET_TYPE
7393 && comptypes (TYPE_OFFSET_BASETYPE (to
),
7394 TYPE_OFFSET_BASETYPE (from
),
7395 COMPARE_BASE
| COMPARE_RELAXED
))
7398 if (TREE_CODE (to
) != POINTER_TYPE
)
7400 (TYPE_MAIN_VARIANT (to
), TYPE_MAIN_VARIANT (from
),
7401 COMPARE_BASE
| COMPARE_RELAXED
);
7405 /* Like comp_ptr_ttypes, for const_cast. */
7408 comp_ptr_ttypes_const (to
, from
)
7411 for (; ; to
= TREE_TYPE (to
), from
= TREE_TYPE (from
))
7413 if (TREE_CODE (to
) != TREE_CODE (from
))
7416 if (TREE_CODE (from
) == OFFSET_TYPE
7417 && same_type_p (TYPE_OFFSET_BASETYPE (from
),
7418 TYPE_OFFSET_BASETYPE (to
)))
7421 if (TREE_CODE (to
) != POINTER_TYPE
)
7422 return same_type_p (TYPE_MAIN_VARIANT (to
),
7423 TYPE_MAIN_VARIANT (from
));
7427 /* Like comp_ptr_ttypes, for reinterpret_cast. */
7430 comp_ptr_ttypes_reinterpret (to
, from
)
7435 for (; ; to
= TREE_TYPE (to
), from
= TREE_TYPE (from
))
7437 if (TREE_CODE (from
) == OFFSET_TYPE
)
7438 from
= TREE_TYPE (from
);
7439 if (TREE_CODE (to
) == OFFSET_TYPE
)
7440 to
= TREE_TYPE (to
);
7442 if (TREE_CODE (to
) != TREE_CODE (from
))
7445 /* Const and volatile mean something different for function types,
7446 so the usual checks are not appropriate. */
7447 if (TREE_CODE (to
) != FUNCTION_TYPE
&& TREE_CODE (to
) != METHOD_TYPE
)
7449 if (!at_least_as_qualified_p (to
, from
))
7453 && !at_least_as_qualified_p (from
, to
))
7455 constp
&= TYPE_READONLY (to
);
7458 if (TREE_CODE (to
) != POINTER_TYPE
)
7463 /* Returns the type-qualifier set corresponding to TYPE. */
7466 cp_type_quals (type
)
7469 while (TREE_CODE (type
) == ARRAY_TYPE
)
7470 type
= TREE_TYPE (type
);
7472 return TYPE_QUALS (type
);
7475 /* Returns non-zero if the TYPE contains a mutable member */
7478 cp_has_mutable_p (type
)
7481 while (TREE_CODE (type
) == ARRAY_TYPE
)
7482 type
= TREE_TYPE (type
);
7484 return CLASS_TYPE_P (type
) && CLASSTYPE_HAS_MUTABLE (type
);