1 /* Build expressions with type checking for C++ compiler.
2 Copyright (C) 1987, 88, 89, 92-96, 1997 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). */
45 extern void compiler_error ();
47 static tree convert_for_assignment
PROTO((tree
, tree
, char*, tree
,
49 static tree pointer_int_sum
PROTO((enum tree_code
, tree
, tree
));
50 static tree rationalize_conditional_expr
PROTO((enum tree_code
, tree
));
51 static int comp_target_parms
PROTO((tree
, tree
, int));
52 static int comp_ptr_ttypes_real
PROTO((tree
, tree
, int));
53 static int comp_ptr_ttypes_const
PROTO((tree
, tree
));
54 static int comp_ptr_ttypes_reinterpret
PROTO((tree
, tree
));
55 static int comp_array_types
PROTO((int (*) (tree
, tree
, int), tree
,
57 static tree build_ptrmemfunc1
PROTO((tree
, tree
, tree
, tree
, tree
));
58 static tree common_base_type
PROTO((tree
, tree
));
59 static tree convert_sequence
PROTO((tree
, tree
));
60 static tree lookup_anon_field
PROTO((tree
, tree
));
61 static tree pointer_diff
PROTO((tree
, tree
, tree
));
62 static tree qualify_type
PROTO((tree
, tree
));
63 static tree expand_target_expr
PROTO((tree
));
64 static tree get_delta_difference
PROTO((tree
, tree
, int));
66 /* Return the target type of TYPE, which meas return T for:
67 T*, T&, T[], T (...), and otherwise, just T. */
73 if (TREE_CODE (type
) == REFERENCE_TYPE
)
74 type
= TREE_TYPE (type
);
75 while (TREE_CODE (type
) == POINTER_TYPE
76 || TREE_CODE (type
) == ARRAY_TYPE
77 || TREE_CODE (type
) == FUNCTION_TYPE
78 || TREE_CODE (type
) == METHOD_TYPE
79 || TREE_CODE (type
) == OFFSET_TYPE
)
80 type
= TREE_TYPE (type
);
84 /* Do `exp = require_complete_type (exp);' to make sure exp
85 does not have an incomplete type. (That includes void types.) */
88 require_complete_type (value
)
93 if (processing_template_decl
)
96 type
= TREE_TYPE (value
);
98 /* First, detect a valid value with a complete type. */
99 if (TYPE_SIZE (type
) != 0
100 && type
!= void_type_node
101 && ! (TYPE_LANG_SPECIFIC (type
)
102 && (IS_SIGNATURE_POINTER (type
) || IS_SIGNATURE_REFERENCE (type
))
103 && TYPE_SIZE (SIGNATURE_TYPE (type
)) == 0))
106 /* If we see X::Y, we build an OFFSET_TYPE which has
107 not been laid out. Try to avoid an error by interpreting
108 it as this->X::Y, if reasonable. */
109 if (TREE_CODE (value
) == OFFSET_REF
110 && current_class_ref
!= 0
111 && TREE_OPERAND (value
, 0) == current_class_ref
)
113 tree base
, member
= TREE_OPERAND (value
, 1);
114 tree basetype
= TYPE_OFFSET_BASETYPE (type
);
115 my_friendly_assert (TREE_CODE (member
) == FIELD_DECL
, 305);
116 base
= convert_pointer_to (basetype
, current_class_ptr
);
117 value
= build (COMPONENT_REF
, TREE_TYPE (member
),
118 build_indirect_ref (base
, NULL_PTR
), member
);
119 return require_complete_type (value
);
122 if (IS_AGGR_TYPE (type
) && CLASSTYPE_TEMPLATE_INSTANTIATION (type
))
124 instantiate_class_template (TYPE_MAIN_VARIANT (type
));
125 if (TYPE_SIZE (type
) != 0)
129 incomplete_type_error (value
, type
);
130 return error_mark_node
;
137 if (type
== error_mark_node
|| TYPE_SIZE (type
) != NULL_TREE
)
139 else if (TREE_CODE (type
) == ARRAY_TYPE
&& TYPE_DOMAIN (type
))
141 tree t
= complete_type (TREE_TYPE (type
));
142 if (TYPE_SIZE (t
) != NULL_TREE
&& ! processing_template_decl
)
144 TYPE_NEEDS_CONSTRUCTING (type
)
145 = TYPE_NEEDS_CONSTRUCTING (TYPE_MAIN_VARIANT (t
));
146 TYPE_NEEDS_DESTRUCTOR (type
)
147 = TYPE_NEEDS_DESTRUCTOR (TYPE_MAIN_VARIANT (t
));
149 else if (IS_AGGR_TYPE (type
) && CLASSTYPE_TEMPLATE_INSTANTIATION (type
))
150 instantiate_class_template (TYPE_MAIN_VARIANT (type
));
155 /* Return truthvalue of whether type of EXP is instantiated. */
161 return (TREE_CODE (exp
) == TREE_LIST
162 || TREE_TYPE (exp
) == unknown_type_node
163 || (TREE_CODE (TREE_TYPE (exp
)) == OFFSET_TYPE
164 && TREE_TYPE (TREE_TYPE (exp
)) == unknown_type_node
));
167 /* Return truthvalue of whether T is function (or pfn) type. */
173 return (TREE_CODE (t
) == FUNCTION_TYPE
|| TREE_CODE (t
) == METHOD_TYPE
174 || (TREE_CODE (t
) == POINTER_TYPE
175 && (TREE_CODE (TREE_TYPE (t
)) == FUNCTION_TYPE
176 || TREE_CODE (TREE_TYPE (t
)) == METHOD_TYPE
)));
179 /* Do `exp = require_instantiated_type (type, exp);' to make sure EXP
180 does not have an uninstantiated type.
181 TYPE is type to instantiate with, if uninstantiated. */
184 require_instantiated_type (type
, exp
, errval
)
185 tree type
, exp
, errval
;
187 if (TREE_TYPE (exp
) == NULL_TREE
)
189 error ("argument list may not have an initializer list");
193 if (TREE_TYPE (exp
) == unknown_type_node
194 || (TREE_CODE (TREE_TYPE (exp
)) == OFFSET_TYPE
195 && TREE_TYPE (TREE_TYPE (exp
)) == unknown_type_node
))
197 exp
= instantiate_type (type
, exp
, 1);
198 if (TREE_TYPE (exp
) == error_mark_node
)
204 /* Return a variant of TYPE which has all the type qualifiers of LIKE
205 as well as those of TYPE. */
208 qualify_type (type
, like
)
211 int constflag
= TYPE_READONLY (type
) || TYPE_READONLY (like
);
212 int volflag
= TYPE_VOLATILE (type
) || TYPE_VOLATILE (like
);
213 /* @@ Must do member pointers here. */
214 return cp_build_type_variant (type
, constflag
, volflag
);
217 /* Return the common type of two parameter lists.
218 We assume that comptypes has already been done and returned 1;
219 if that isn't so, this may crash.
221 As an optimization, free the space we allocate if the parameter
222 lists are already common. */
228 tree oldargs
= p1
, newargs
, n
;
231 char *first_obj
= (char *) oballoc (0);
233 len
= list_length (p1
);
234 newargs
= tree_last (p1
);
236 if (newargs
== void_list_node
)
245 newargs
= tree_cons (NULL_TREE
, NULL_TREE
, newargs
);
250 p1
= TREE_CHAIN (p1
), p2
= TREE_CHAIN (p2
), n
= TREE_CHAIN (n
), i
++)
252 if (TREE_PURPOSE (p1
) && !TREE_PURPOSE (p2
))
254 TREE_PURPOSE (n
) = TREE_PURPOSE (p1
);
257 else if (! TREE_PURPOSE (p1
))
259 if (TREE_PURPOSE (p2
))
261 TREE_PURPOSE (n
) = TREE_PURPOSE (p2
);
267 if (1 != simple_cst_equal (TREE_PURPOSE (p1
), TREE_PURPOSE (p2
)))
269 TREE_PURPOSE (n
) = TREE_PURPOSE (p2
);
271 if (TREE_VALUE (p1
) != TREE_VALUE (p2
))
274 TREE_VALUE (n
) = common_type (TREE_VALUE (p1
), TREE_VALUE (p2
));
277 TREE_VALUE (n
) = TREE_VALUE (p1
);
288 /* Return the common type of two types.
289 We assume that comptypes has already been done and returned 1;
290 if that isn't so, this may crash.
292 This is the type for the result of most arithmetic operations
293 if the operands have the given two types.
295 We do not deal with enumeral types here because they have already been
296 converted to integer types. */
302 register enum tree_code code1
;
303 register enum tree_code code2
;
306 /* Save time if the two types are the same. */
308 if (t1
== t2
) return t1
;
310 /* If one type is nonsense, use the other. */
311 if (t1
== error_mark_node
)
313 if (t2
== error_mark_node
)
316 /* Merge the attributes */
318 { register tree a1
, a2
;
319 a1
= TYPE_ATTRIBUTES (t1
);
320 a2
= TYPE_ATTRIBUTES (t2
);
322 /* Either one unset? Take the set one. */
324 if (!(attributes
= a1
))
327 /* One that completely contains the other? Take it. */
329 else if (a2
&& !attribute_list_contained (a1
, a2
))
330 if (attribute_list_contained (a2
, a1
))
334 /* Pick the longest list, and hang on the other list. */
335 /* ??? For the moment we punt on the issue of attrs with args. */
337 if (list_length (a1
) < list_length (a2
))
338 attributes
= a2
, a2
= a1
;
340 for (; a2
; a2
= TREE_CHAIN (a2
))
341 if (lookup_attribute (IDENTIFIER_POINTER (TREE_PURPOSE (a2
)),
342 attributes
) == NULL_TREE
)
345 TREE_CHAIN (a1
) = attributes
;
351 /* Treat an enum type as the unsigned integer type of the same width. */
353 if (TREE_CODE (t1
) == ENUMERAL_TYPE
)
354 t1
= type_for_size (TYPE_PRECISION (t1
), 1);
355 if (TREE_CODE (t2
) == ENUMERAL_TYPE
)
356 t2
= type_for_size (TYPE_PRECISION (t2
), 1);
358 if (TYPE_PTRMEMFUNC_P (t1
))
359 t1
= TYPE_PTRMEMFUNC_FN_TYPE (t1
);
360 if (TYPE_PTRMEMFUNC_P (t2
))
361 t2
= TYPE_PTRMEMFUNC_FN_TYPE (t2
);
363 code1
= TREE_CODE (t1
);
364 code2
= TREE_CODE (t2
);
366 /* If one type is complex, form the common type of the non-complex
367 components, then make that complex. Use T1 or T2 if it is the
369 if (code1
== COMPLEX_TYPE
|| code2
== COMPLEX_TYPE
)
371 tree subtype1
= code1
== COMPLEX_TYPE
? TREE_TYPE (t1
) : t1
;
372 tree subtype2
= code2
== COMPLEX_TYPE
? TREE_TYPE (t2
) : t2
;
373 tree subtype
= common_type (subtype1
, subtype2
);
375 if (code1
== COMPLEX_TYPE
&& TREE_TYPE (t1
) == subtype
)
376 return build_type_attribute_variant (t1
, attributes
);
377 else if (code2
== COMPLEX_TYPE
&& TREE_TYPE (t2
) == subtype
)
378 return build_type_attribute_variant (t2
, attributes
);
380 return build_type_attribute_variant (build_complex_type (subtype
),
388 /* If only one is real, use it as the result. */
390 if (code1
== REAL_TYPE
&& code2
!= REAL_TYPE
)
391 return build_type_attribute_variant (t1
, attributes
);
393 if (code2
== REAL_TYPE
&& code1
!= REAL_TYPE
)
394 return build_type_attribute_variant (t2
, attributes
);
396 /* Both real or both integers; use the one with greater precision. */
398 if (TYPE_PRECISION (t1
) > TYPE_PRECISION (t2
))
399 return build_type_attribute_variant (t1
, attributes
);
400 else if (TYPE_PRECISION (t2
) > TYPE_PRECISION (t1
))
401 return build_type_attribute_variant (t2
, attributes
);
403 /* Same precision. Prefer longs to ints even when same size. */
405 if (TYPE_MAIN_VARIANT (t1
) == long_unsigned_type_node
406 || TYPE_MAIN_VARIANT (t2
) == long_unsigned_type_node
)
407 return build_type_attribute_variant (long_unsigned_type_node
,
410 if (TYPE_MAIN_VARIANT (t1
) == long_integer_type_node
411 || TYPE_MAIN_VARIANT (t2
) == long_integer_type_node
)
413 /* But preserve unsignedness from the other type,
414 since long cannot hold all the values of an unsigned int. */
415 if (TREE_UNSIGNED (t1
) || TREE_UNSIGNED (t2
))
416 t1
= long_unsigned_type_node
;
418 t1
= long_integer_type_node
;
419 return build_type_attribute_variant (t1
, attributes
);
422 if (TYPE_MAIN_VARIANT (t1
) == long_double_type_node
423 || TYPE_MAIN_VARIANT (t2
) == long_double_type_node
)
424 return build_type_attribute_variant (long_double_type_node
,
427 /* Otherwise prefer the unsigned one. */
429 if (TREE_UNSIGNED (t1
))
430 return build_type_attribute_variant (t1
, attributes
);
432 return build_type_attribute_variant (t2
, attributes
);
436 /* For two pointers, do this recursively on the target type,
437 and combine the qualifiers of the two types' targets. */
438 /* This code was turned off; I don't know why.
439 But ANSI C++ specifies doing this with the qualifiers.
440 So I turned it on again. */
442 tree tt1
= TYPE_MAIN_VARIANT (TREE_TYPE (t1
));
443 tree tt2
= TYPE_MAIN_VARIANT (TREE_TYPE (t2
));
445 = TYPE_READONLY (TREE_TYPE (t1
)) || TYPE_READONLY (TREE_TYPE (t2
));
447 = TYPE_VOLATILE (TREE_TYPE (t1
)) || TYPE_VOLATILE (TREE_TYPE (t2
));
452 else if (tt1
== void_type_node
|| tt2
== void_type_node
)
453 target
= void_type_node
;
454 else if (tt1
== unknown_type_node
)
456 else if (tt2
== unknown_type_node
)
459 target
= common_type (tt1
, tt2
);
461 target
= cp_build_type_variant (target
, constp
, volatilep
);
462 if (code1
== POINTER_TYPE
)
463 t1
= build_pointer_type (target
);
465 t1
= build_reference_type (target
);
466 t1
= build_type_attribute_variant (t1
, attributes
);
468 if (TREE_CODE (target
) == METHOD_TYPE
)
469 t1
= build_ptrmemfunc_type (t1
);
476 tree elt
= common_type (TREE_TYPE (t1
), TREE_TYPE (t2
));
477 /* Save space: see if the result is identical to one of the args. */
478 if (elt
== TREE_TYPE (t1
) && TYPE_DOMAIN (t1
))
479 return build_type_attribute_variant (t1
, attributes
);
480 if (elt
== TREE_TYPE (t2
) && TYPE_DOMAIN (t2
))
481 return build_type_attribute_variant (t2
, attributes
);
482 /* Merge the element types, and have a size if either arg has one. */
483 t1
= build_cplus_array_type
484 (elt
, TYPE_DOMAIN (TYPE_DOMAIN (t1
) ? t1
: t2
));
485 return build_type_attribute_variant (t1
, attributes
);
489 /* Function types: prefer the one that specified arg types.
490 If both do, merge the arg types. Also merge the return types. */
492 tree valtype
= common_type (TREE_TYPE (t1
), TREE_TYPE (t2
));
493 tree p1
= TYPE_ARG_TYPES (t1
);
494 tree p2
= TYPE_ARG_TYPES (t2
);
497 /* Save space: see if the result is identical to one of the args. */
498 if (valtype
== TREE_TYPE (t1
) && ! p2
)
499 return build_type_attribute_variant (t1
, attributes
);
500 if (valtype
== TREE_TYPE (t2
) && ! p1
)
501 return build_type_attribute_variant (t2
, attributes
);
503 /* Simple way if one arg fails to specify argument types. */
504 if (p1
== NULL_TREE
|| TREE_VALUE (p1
) == void_type_node
)
506 rval
= build_function_type (valtype
, p2
);
507 if ((raises
= TYPE_RAISES_EXCEPTIONS (t2
)))
508 rval
= build_exception_variant (rval
, raises
);
509 return build_type_attribute_variant (rval
, attributes
);
511 raises
= TYPE_RAISES_EXCEPTIONS (t1
);
512 if (p2
== NULL_TREE
|| TREE_VALUE (p2
) == void_type_node
)
514 rval
= build_function_type (valtype
, p1
);
516 rval
= build_exception_variant (rval
, raises
);
517 return build_type_attribute_variant (rval
, attributes
);
520 rval
= build_function_type (valtype
, commonparms (p1
, p2
));
521 rval
= build_exception_variant (rval
, raises
);
522 return build_type_attribute_variant (rval
, attributes
);
527 my_friendly_assert (TYPE_MAIN_VARIANT (t1
) == t1
528 && TYPE_MAIN_VARIANT (t2
) == t2
, 306);
530 if (DERIVED_FROM_P (t1
, t2
) && binfo_or_else (t1
, t2
))
531 return build_type_attribute_variant (t1
, attributes
);
532 else if (binfo_or_else (t2
, t1
))
533 return build_type_attribute_variant (t2
, attributes
);
535 compiler_error ("common_type called with uncommon aggregate types");
538 if (TREE_CODE (TREE_TYPE (t1
)) == TREE_CODE (TREE_TYPE (t2
)))
540 /* Get this value the long way, since TYPE_METHOD_BASETYPE
541 is just the main variant of this. */
545 tree b1
= TYPE_OFFSET_BASETYPE (t1
);
546 tree b2
= TYPE_OFFSET_BASETYPE (t2
);
548 if (comptypes (b1
, b2
, 1)
549 || (DERIVED_FROM_P (b1
, b2
) && binfo_or_else (b1
, b2
)))
550 basetype
= TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (t2
)));
553 if (binfo_or_else (b2
, b1
) == NULL_TREE
)
554 compiler_error ("common_type called with uncommon method types");
555 basetype
= TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (t1
)));
558 raises
= TYPE_RAISES_EXCEPTIONS (t1
);
560 /* If this was a member function type, get back to the
561 original type of type member function (i.e., without
562 the class instance variable up front. */
563 t1
= build_function_type (TREE_TYPE (t1
), TREE_CHAIN (TYPE_ARG_TYPES (t1
)));
564 t2
= build_function_type (TREE_TYPE (t2
), TREE_CHAIN (TYPE_ARG_TYPES (t2
)));
565 t3
= common_type (t1
, t2
);
566 t3
= build_cplus_method_type (basetype
, TREE_TYPE (t3
), TYPE_ARG_TYPES (t3
));
567 t1
= build_exception_variant (t3
, raises
);
570 compiler_error ("common_type called with uncommon method types");
572 return build_type_attribute_variant (t1
, attributes
);
575 if (TREE_TYPE (t1
) == TREE_TYPE (t2
))
577 tree b1
= TYPE_OFFSET_BASETYPE (t1
);
578 tree b2
= TYPE_OFFSET_BASETYPE (t2
);
580 if (comptypes (b1
, b2
, 1)
581 || (DERIVED_FROM_P (b1
, b2
) && binfo_or_else (b1
, b2
)))
582 return build_type_attribute_variant (t2
, attributes
);
583 else if (binfo_or_else (b2
, b1
))
584 return build_type_attribute_variant (t1
, attributes
);
586 compiler_error ("common_type called with uncommon member types");
589 return build_type_attribute_variant (t1
, attributes
);
593 /* Return 1 if TYPE1 and TYPE2 raise the same exceptions. */
596 compexcepttypes (t1
, t2
)
599 return TYPE_RAISES_EXCEPTIONS (t1
) == TYPE_RAISES_EXCEPTIONS (t2
);
603 comp_array_types (cmp
, t1
, t2
, strict
)
604 register int (*cmp
) PROTO((tree
, tree
, int));
608 tree d1
= TYPE_DOMAIN (t1
);
609 tree d2
= TYPE_DOMAIN (t2
);
611 /* Target types must match incl. qualifiers. */
612 if (!(TREE_TYPE (t1
) == TREE_TYPE (t2
)
613 || (*cmp
) (TREE_TYPE (t1
), TREE_TYPE (t2
), strict
)))
616 /* Sizes must match unless one is missing or variable. */
617 if (d1
== 0 || d2
== 0 || d1
== d2
618 || TREE_CODE (TYPE_MIN_VALUE (d1
)) != INTEGER_CST
619 || TREE_CODE (TYPE_MIN_VALUE (d2
)) != INTEGER_CST
620 || TREE_CODE (TYPE_MAX_VALUE (d1
)) != INTEGER_CST
621 || TREE_CODE (TYPE_MAX_VALUE (d2
)) != INTEGER_CST
)
624 return ((TREE_INT_CST_LOW (TYPE_MIN_VALUE (d1
))
625 == TREE_INT_CST_LOW (TYPE_MIN_VALUE (d2
)))
626 && (TREE_INT_CST_HIGH (TYPE_MIN_VALUE (d1
))
627 == TREE_INT_CST_HIGH (TYPE_MIN_VALUE (d2
)))
628 && (TREE_INT_CST_LOW (TYPE_MAX_VALUE (d1
))
629 == TREE_INT_CST_LOW (TYPE_MAX_VALUE (d2
)))
630 && (TREE_INT_CST_HIGH (TYPE_MAX_VALUE (d1
))
631 == TREE_INT_CST_HIGH (TYPE_MAX_VALUE (d2
))));
634 /* Return 1 if TYPE1 and TYPE2 are compatible types for assignment
635 or various other operations. This is what ANSI C++ speaks of as
638 For C++: argument STRICT says we should be strict about this
641 2 : strict, except that if one type is a reference and
642 the other is not, compare the target type of the
643 reference to the type that's not a reference (ARM, p308).
644 This is used for checking for invalid overloading.
645 1 : strict (compared according to ANSI C)
646 This is used for checking whether two function decls match.
647 0 : <= (compared according to C++)
648 -1: <= or >= (relaxed)
650 Otherwise, pointers involving base classes and derived classes can
651 be mixed as valid: i.e. a pointer to a derived class may be converted
652 to a pointer to one of its base classes, as per C++. A pointer to
653 a derived class may be passed as a parameter to a function expecting a
654 pointer to a base classes. These allowances do not commute. In this
655 case, TYPE1 is assumed to be the base class, and TYPE2 is assumed to
656 be the derived class. */
659 comptypes (type1
, type2
, strict
)
663 register tree t1
= type1
;
664 register tree t2
= type2
;
667 /* Suppress errors caused by previously reported errors */
672 /* This should never happen. */
673 my_friendly_assert (t1
!= error_mark_node
, 307);
675 if (t2
== error_mark_node
)
680 /* Treat an enum type as the unsigned integer type of the same width. */
682 if (TREE_CODE (t1
) == ENUMERAL_TYPE
)
683 t1
= type_for_size (TYPE_PRECISION (t1
), 1);
684 if (TREE_CODE (t2
) == ENUMERAL_TYPE
)
685 t2
= type_for_size (TYPE_PRECISION (t2
), 1);
691 if (TYPE_PTRMEMFUNC_P (t1
))
692 t1
= TYPE_PTRMEMFUNC_FN_TYPE (t1
);
693 if (TYPE_PTRMEMFUNC_P (t2
))
694 t2
= TYPE_PTRMEMFUNC_FN_TYPE (t2
);
696 /* Different classes of types can't be compatible. */
698 if (TREE_CODE (t1
) != TREE_CODE (t2
))
701 && ((TREE_CODE (t1
) == REFERENCE_TYPE
)
702 ^ (TREE_CODE (t2
) == REFERENCE_TYPE
)))
704 if (TREE_CODE (t1
) == REFERENCE_TYPE
)
705 return comptypes (TREE_TYPE (t1
), t2
, 1);
706 return comptypes (t1
, TREE_TYPE (t2
), 1);
714 /* Qualifiers must match. */
716 if (TYPE_READONLY (t1
) != TYPE_READONLY (t2
))
718 if (TYPE_VOLATILE (t1
) != TYPE_VOLATILE (t2
))
721 /* Allow for two different type nodes which have essentially the same
722 definition. Note that we already checked for equality of the type
723 type qualifiers (just above). */
725 if (TYPE_MAIN_VARIANT (t1
) == TYPE_MAIN_VARIANT (t2
))
728 /* ??? COMP_TYPE_ATTRIBUTES is currently useless for variables as each
729 attribute is its own main variant (`val' will remain 0). */
730 #ifndef COMP_TYPE_ATTRIBUTES
731 #define COMP_TYPE_ATTRIBUTES(t1,t2) 1
734 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
735 if (! (attrval
= COMP_TYPE_ATTRIBUTES (t1
, t2
)))
738 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
741 switch (TREE_CODE (t1
))
745 if (CLASSTYPE_TEMPLATE_INFO (t1
) && CLASSTYPE_TEMPLATE_INFO (t2
)
746 && CLASSTYPE_TI_TEMPLATE (t1
) == CLASSTYPE_TI_TEMPLATE (t2
))
748 int i
= TREE_VEC_LENGTH (CLASSTYPE_TI_ARGS (t1
));
749 tree
*p1
= &TREE_VEC_ELT (CLASSTYPE_TI_ARGS (t1
), 0);
750 tree
*p2
= &TREE_VEC_ELT (CLASSTYPE_TI_ARGS (t2
), 0);
754 if (TREE_CODE_CLASS (TREE_CODE (p1
[i
])) == 't')
756 if (! comptypes (p1
[i
], p2
[i
], 1))
761 if (simple_cst_equal (p1
[i
], p2
[i
]) <= 0)
772 val
= (comptypes (build_pointer_type (TYPE_OFFSET_BASETYPE (t1
)),
773 build_pointer_type (TYPE_OFFSET_BASETYPE (t2
)), strict
)
774 && comptypes (TREE_TYPE (t1
), TREE_TYPE (t2
), strict
));
778 if (! compexcepttypes (t1
, t2
))
781 /* This case is anti-symmetrical!
782 One can pass a base member (or member function)
783 to something expecting a derived member (or member function),
784 but not vice-versa! */
786 val
= (comptypes (TREE_TYPE (t1
), TREE_TYPE (t2
), strict
)
787 && compparms (TYPE_ARG_TYPES (t1
),
788 TYPE_ARG_TYPES (t2
), strict
));
802 if (TREE_CODE (t1
) == RECORD_TYPE
&& TREE_CODE (t2
) == RECORD_TYPE
)
806 rval
= t1
== t2
|| DERIVED_FROM_P (t1
, t2
);
815 val
= DERIVED_FROM_P (t2
, t1
);
822 val
= comptypes (t1
, t2
, strict
);
826 if (! compexcepttypes (t1
, t2
))
829 val
= ((TREE_TYPE (t1
) == TREE_TYPE (t2
)
830 || comptypes (TREE_TYPE (t1
), TREE_TYPE (t2
), strict
))
831 && compparms (TYPE_ARG_TYPES (t1
), TYPE_ARG_TYPES (t2
), strict
));
835 /* Target types must match incl. qualifiers. */
836 val
= comp_array_types (comptypes
, t1
, t2
, strict
);
839 case TEMPLATE_TYPE_PARM
:
840 return TEMPLATE_TYPE_IDX (t1
) == TEMPLATE_TYPE_IDX (t2
)
841 && TEMPLATE_TYPE_LEVEL (t1
) == TEMPLATE_TYPE_LEVEL (t2
);
844 if (TYPE_IDENTIFIER (t1
) != TYPE_IDENTIFIER (t2
))
846 return comptypes (TYPE_CONTEXT (t1
), TYPE_CONTEXT (t2
), 1);
851 return attrval
== 2 && val
== 1 ? 2 : val
;
854 /* Return 1 or -1 if TTL and TTR are pointers to types that are equivalent,
855 ignoring their qualifiers, 0 if not. Return 1 means that TTR can be
856 converted to TTL. Return -1 means that TTL can be converted to TTR but
859 NPTRS is the number of pointers we can strip off and keep cool.
860 This is used to permit (for aggr A, aggr B) A, B* to convert to A*,
861 but to not permit B** to convert to A**. */
864 comp_target_types (ttl
, ttr
, nptrs
)
868 ttl
= TYPE_MAIN_VARIANT (ttl
);
869 ttr
= TYPE_MAIN_VARIANT (ttr
);
873 if (TREE_CODE (ttr
) != TREE_CODE (ttl
))
876 if (TREE_CODE (ttr
) == POINTER_TYPE
)
878 ttl
= TREE_TYPE (ttl
);
879 ttr
= TREE_TYPE (ttr
);
883 if (TREE_CODE (ttl
) == UNKNOWN_TYPE
884 || TREE_CODE (ttr
) == UNKNOWN_TYPE
)
886 else if (TREE_CODE (ttl
) == VOID_TYPE
887 && TREE_CODE (ttr
) != FUNCTION_TYPE
888 && TREE_CODE (ttr
) != METHOD_TYPE
889 && TREE_CODE (ttr
) != OFFSET_TYPE
)
891 else if (TREE_CODE (ttr
) == VOID_TYPE
892 && TREE_CODE (ttl
) != FUNCTION_TYPE
893 && TREE_CODE (ttl
) != METHOD_TYPE
894 && TREE_CODE (ttl
) != OFFSET_TYPE
)
896 else if (TREE_CODE (ttl
) == POINTER_TYPE
897 || TREE_CODE (ttl
) == ARRAY_TYPE
)
899 if (comp_ptr_ttypes (ttl
, ttr
))
901 else if (comp_ptr_ttypes (ttr
, ttl
))
907 /* Const and volatile mean something different for function types,
908 so the usual checks are not appropriate. */
909 if (TREE_CODE (ttl
) == FUNCTION_TYPE
|| TREE_CODE (ttl
) == METHOD_TYPE
)
910 return comp_target_types (ttl
, ttr
, nptrs
- 1);
912 /* Make sure that the cv-quals change only in the same direction as
916 int c
= TYPE_READONLY (ttl
) - TYPE_READONLY (ttr
);
917 int v
= TYPE_VOLATILE (ttl
) - TYPE_VOLATILE (ttr
);
919 if ((c
> 0 && v
< 0) || (c
< 0 && v
> 0))
922 if (TYPE_MAIN_VARIANT (ttl
) == TYPE_MAIN_VARIANT (ttr
))
923 return (c
+ v
< 0) ? -1 : 1;
925 t
= comp_target_types (ttl
, ttr
, nptrs
- 1);
926 if ((t
== 1 && c
+ v
>= 0) || (t
== -1 && c
+ v
<= 0))
933 if (TREE_CODE (ttr
) == REFERENCE_TYPE
)
934 return comp_target_types (TREE_TYPE (ttl
), TREE_TYPE (ttr
), nptrs
);
935 if (TREE_CODE (ttr
) == ARRAY_TYPE
)
936 return comp_array_types (comp_target_types
, ttl
, ttr
, 0);
937 else if (TREE_CODE (ttr
) == FUNCTION_TYPE
|| TREE_CODE (ttr
) == METHOD_TYPE
)
938 if (comp_target_types (TREE_TYPE (ttl
), TREE_TYPE (ttr
), -1))
939 switch (comp_target_parms (TYPE_ARG_TYPES (ttl
), TYPE_ARG_TYPES (ttr
), 1))
948 my_friendly_abort (112);
954 else if (TREE_CODE (ttr
) == OFFSET_TYPE
)
956 /* Contravariance: we can assign a pointer to base member to a pointer
957 to derived member. Note difference from simple pointer case, where
958 we can pass a pointer to derived to a pointer to base. */
959 if (comptypes (TYPE_OFFSET_BASETYPE (ttr
), TYPE_OFFSET_BASETYPE (ttl
), 0))
960 return comp_target_types (TREE_TYPE (ttl
), TREE_TYPE (ttr
), nptrs
);
961 else if (comptypes (TYPE_OFFSET_BASETYPE (ttl
), TYPE_OFFSET_BASETYPE (ttr
), 0)
962 && comp_target_types (TREE_TYPE (ttl
), TREE_TYPE (ttr
), nptrs
))
965 else if (IS_AGGR_TYPE (ttl
))
969 if (comptypes (build_pointer_type (ttl
), build_pointer_type (ttr
), 0))
971 if (comptypes (build_pointer_type (ttr
), build_pointer_type (ttl
), 0))
979 /* If two types share a common base type, return that basetype.
980 If there is not a unique most-derived base type, this function
981 returns ERROR_MARK_NODE. */
984 common_base_type (tt1
, tt2
)
987 tree best
= NULL_TREE
;
990 /* If one is a baseclass of another, that's good enough. */
991 if (UNIQUELY_DERIVED_FROM_P (tt1
, tt2
))
993 if (UNIQUELY_DERIVED_FROM_P (tt2
, tt1
))
996 /* Otherwise, try to find a unique baseclass of TT1
997 that is shared by TT2, and follow that down. */
998 for (i
= CLASSTYPE_N_BASECLASSES (tt1
)-1; i
>= 0; i
--)
1000 tree basetype
= TYPE_BINFO_BASETYPE (tt1
, i
);
1001 tree trial
= common_base_type (basetype
, tt2
);
1004 if (trial
== error_mark_node
)
1006 if (best
== NULL_TREE
)
1008 else if (best
!= trial
)
1009 return error_mark_node
;
1014 for (i
= CLASSTYPE_N_BASECLASSES (tt2
)-1; i
>= 0; i
--)
1016 tree basetype
= TYPE_BINFO_BASETYPE (tt2
, i
);
1017 tree trial
= common_base_type (tt1
, basetype
);
1020 if (trial
== error_mark_node
)
1022 if (best
== NULL_TREE
)
1024 else if (best
!= trial
)
1025 return error_mark_node
;
1031 /* Subroutines of `comptypes'. */
1033 /* Return 1 if two parameter type lists PARMS1 and PARMS2
1034 are equivalent in the sense that functions with those parameter types
1035 can have equivalent types.
1036 If either list is empty, we win.
1037 Otherwise, the two lists must be equivalent, element by element.
1039 C++: See comment above about TYPE1, TYPE2, STRICT.
1040 If STRICT == 3, it means checking is strict, but do not compare
1041 default parameter values. */
1044 compparms (parms1
, parms2
, strict
)
1045 tree parms1
, parms2
;
1048 register tree t1
= parms1
, t2
= parms2
;
1050 /* An unspecified parmlist matches any specified parmlist
1051 whose argument types don't need default promotions. */
1053 if (strict
<= 0 && t1
== 0)
1054 return self_promoting_args_p (t2
);
1055 if (strict
< 0 && t2
== 0)
1056 return self_promoting_args_p (t1
);
1060 if (t1
== 0 && t2
== 0)
1062 /* If one parmlist is shorter than the other,
1063 they fail to match, unless STRICT is <= 0. */
1064 if (t1
== 0 || t2
== 0)
1071 return t1
&& TREE_PURPOSE (t1
);
1073 if (! comptypes (TREE_VALUE (t2
), TREE_VALUE (t1
), strict
))
1078 return t2
== void_list_node
&& TREE_PURPOSE (t1
);
1079 return TREE_PURPOSE (t1
) || TREE_PURPOSE (t2
);
1082 t1
= TREE_CHAIN (t1
);
1083 t2
= TREE_CHAIN (t2
);
1087 /* This really wants return whether or not parameter type lists
1088 would make their owning functions assignment compatible or not. */
1091 comp_target_parms (parms1
, parms2
, strict
)
1092 tree parms1
, parms2
;
1095 register tree t1
= parms1
, t2
= parms2
;
1096 int warn_contravariance
= 0;
1098 /* An unspecified parmlist matches any specified parmlist
1099 whose argument types don't need default promotions.
1100 @@@ see 13.3.3 for a counterexample... */
1102 if (t1
== 0 && t2
!= 0)
1104 cp_pedwarn ("ANSI C++ prohibits conversion from `(%#T)' to `(...)'",
1106 return self_promoting_args_p (t2
);
1109 return self_promoting_args_p (t1
);
1111 for (; t1
|| t2
; t1
= TREE_CHAIN (t1
), t2
= TREE_CHAIN (t2
))
1115 /* If one parmlist is shorter than the other,
1116 they fail to match, unless STRICT is <= 0. */
1117 if (t1
== 0 || t2
== 0)
1122 return 1 + warn_contravariance
;
1123 return ((t1
&& TREE_PURPOSE (t1
)) + warn_contravariance
);
1125 p1
= TREE_VALUE (t1
);
1126 p2
= TREE_VALUE (t2
);
1130 if ((TREE_CODE (p1
) == POINTER_TYPE
&& TREE_CODE (p2
) == POINTER_TYPE
)
1131 || (TREE_CODE (p1
) == REFERENCE_TYPE
&& TREE_CODE (p2
) == REFERENCE_TYPE
))
1134 && (TYPE_MAIN_VARIANT (TREE_TYPE (p1
))
1135 == TYPE_MAIN_VARIANT (TREE_TYPE (p2
))))
1138 /* The following is wrong for contravariance,
1139 but many programs depend on it. */
1140 if (TREE_TYPE (p1
) == void_type_node
)
1142 if (TREE_TYPE (p2
) == void_type_node
)
1144 warn_contravariance
= 1;
1147 if (IS_AGGR_TYPE (TREE_TYPE (p1
)))
1149 if (comptypes (p2
, p1
, 0) == 0)
1151 if (comptypes (p1
, p2
, 0) != 0)
1152 warn_contravariance
= 1;
1159 /* Note backwards order due to contravariance. */
1160 if (comp_target_types (p2
, p1
, 1) == 0)
1162 if (comp_target_types (p1
, p2
, 1))
1164 warn_contravariance
= 1;
1170 /* Target types are compatible--just make sure that if
1171 we use parameter lists, that they are ok as well. */
1172 if (TREE_CODE (p1
) == FUNCTION_TYPE
|| TREE_CODE (p1
) == METHOD_TYPE
)
1173 switch (comp_target_parms (TYPE_ARG_TYPES (p1
),
1174 TYPE_ARG_TYPES (p2
),
1182 warn_contravariance
= 1;
1185 if (TREE_PURPOSE (t1
) && TREE_PURPOSE (t2
))
1187 int cmp
= simple_cst_equal (TREE_PURPOSE (t1
), TREE_PURPOSE (t2
));
1189 my_friendly_abort (114);
1194 return 1 + warn_contravariance
;
1197 /* Return 1 if PARMS specifies a fixed number of parameters
1198 and none of their types is affected by default promotions. */
1201 self_promoting_args_p (parms
)
1205 for (t
= parms
; t
; t
= TREE_CHAIN (t
))
1207 register tree type
= TREE_VALUE (t
);
1209 if (TREE_CHAIN (t
) == 0 && type
!= void_type_node
)
1215 if (TYPE_MAIN_VARIANT (type
) == float_type_node
)
1218 if (C_PROMOTING_INTEGER_TYPE_P (type
))
1224 /* Return an unsigned type the same as TYPE in other respects.
1226 C++: must make these work for type variants as well. */
1229 unsigned_type (type
)
1232 tree type1
= TYPE_MAIN_VARIANT (type
);
1233 if (type1
== signed_char_type_node
|| type1
== char_type_node
)
1234 return unsigned_char_type_node
;
1235 if (type1
== integer_type_node
)
1236 return unsigned_type_node
;
1237 if (type1
== short_integer_type_node
)
1238 return short_unsigned_type_node
;
1239 if (type1
== long_integer_type_node
)
1240 return long_unsigned_type_node
;
1241 if (type1
== long_long_integer_type_node
)
1242 return long_long_unsigned_type_node
;
1243 if (type1
== intDI_type_node
)
1244 return unsigned_intDI_type_node
;
1245 if (type1
== intSI_type_node
)
1246 return unsigned_intSI_type_node
;
1247 if (type1
== intHI_type_node
)
1248 return unsigned_intHI_type_node
;
1249 if (type1
== intQI_type_node
)
1250 return unsigned_intQI_type_node
;
1252 return signed_or_unsigned_type (1, type
);
1255 /* Return a signed type the same as TYPE in other respects. */
1261 tree type1
= TYPE_MAIN_VARIANT (type
);
1262 if (type1
== unsigned_char_type_node
|| type1
== char_type_node
)
1263 return signed_char_type_node
;
1264 if (type1
== unsigned_type_node
)
1265 return integer_type_node
;
1266 if (type1
== short_unsigned_type_node
)
1267 return short_integer_type_node
;
1268 if (type1
== long_unsigned_type_node
)
1269 return long_integer_type_node
;
1270 if (type1
== long_long_unsigned_type_node
)
1271 return long_long_integer_type_node
;
1272 if (type1
== unsigned_intDI_type_node
)
1273 return intDI_type_node
;
1274 if (type1
== unsigned_intSI_type_node
)
1275 return intSI_type_node
;
1276 if (type1
== unsigned_intHI_type_node
)
1277 return intHI_type_node
;
1278 if (type1
== unsigned_intQI_type_node
)
1279 return intQI_type_node
;
1281 return signed_or_unsigned_type (0, type
);
1284 /* Return a type the same as TYPE except unsigned or
1285 signed according to UNSIGNEDP. */
1288 signed_or_unsigned_type (unsignedp
, type
)
1292 if (! INTEGRAL_TYPE_P (type
)
1293 || TREE_UNSIGNED (type
) == unsignedp
)
1296 if (TYPE_PRECISION (type
) == TYPE_PRECISION (signed_char_type_node
))
1297 return unsignedp
? unsigned_char_type_node
: signed_char_type_node
;
1298 if (TYPE_PRECISION (type
) == TYPE_PRECISION (integer_type_node
))
1299 return unsignedp
? unsigned_type_node
: integer_type_node
;
1300 if (TYPE_PRECISION (type
) == TYPE_PRECISION (short_integer_type_node
))
1301 return unsignedp
? short_unsigned_type_node
: short_integer_type_node
;
1302 if (TYPE_PRECISION (type
) == TYPE_PRECISION (long_integer_type_node
))
1303 return unsignedp
? long_unsigned_type_node
: long_integer_type_node
;
1304 if (TYPE_PRECISION (type
) == TYPE_PRECISION (long_long_integer_type_node
))
1305 return (unsignedp
? long_long_unsigned_type_node
1306 : long_long_integer_type_node
);
1310 /* Compute the value of the `sizeof' operator. */
1316 enum tree_code code
= TREE_CODE (type
);
1319 if (processing_template_decl
)
1320 return build_min (SIZEOF_EXPR
, sizetype
, type
);
1322 if (code
== FUNCTION_TYPE
)
1324 if (pedantic
|| warn_pointer_arith
)
1325 pedwarn ("ANSI C++ forbids taking the sizeof a function type");
1326 return size_int (1);
1328 if (code
== METHOD_TYPE
)
1330 if (pedantic
|| warn_pointer_arith
)
1331 pedwarn ("ANSI C++ forbids taking the sizeof a method type");
1332 return size_int (1);
1334 if (code
== VOID_TYPE
)
1336 if (pedantic
|| warn_pointer_arith
)
1337 pedwarn ("ANSI C++ forbids taking the sizeof a void type");
1338 return size_int (1);
1340 if (code
== ERROR_MARK
)
1341 return size_int (1);
1343 /* ARM $5.3.2: ``When applied to a reference, the result is the size of the
1344 referenced object.'' */
1345 if (code
== REFERENCE_TYPE
)
1346 type
= TREE_TYPE (type
);
1348 /* We couldn't find anything in the ARM or the draft standard that says,
1349 one way or the other, if doing sizeof on something that doesn't have
1350 an object associated with it is correct or incorrect. For example, if
1351 you declare `struct S { char str[16]; };', and in your program do
1352 a `sizeof (S::str)', should we flag that as an error or should we give
1353 the size of it? Since it seems like a reasonable thing to do, we'll go
1354 with giving the value. */
1355 if (code
== OFFSET_TYPE
)
1356 type
= TREE_TYPE (type
);
1358 /* @@ This also produces an error for a signature ref.
1359 In that case we should be able to do better. */
1360 if (IS_SIGNATURE (type
))
1362 error ("`sizeof' applied to a signature type");
1363 return size_int (0);
1366 if (TYPE_SIZE (complete_type (type
)) == 0)
1368 cp_error ("`sizeof' applied to incomplete type `%T'", type
);
1369 return size_int (0);
1372 /* Convert in case a char is more than one unit. */
1373 t
= size_binop (CEIL_DIV_EXPR
, TYPE_SIZE (type
),
1374 size_int (TYPE_PRECISION (char_type_node
)));
1375 /* size_binop does not put the constant in range, so do it now. */
1376 if (TREE_CODE (t
) == INTEGER_CST
&& force_fit_type (t
, 0))
1377 TREE_CONSTANT_OVERFLOW (t
) = TREE_OVERFLOW (t
) = 1;
1385 if (processing_template_decl
)
1386 return build_min (SIZEOF_EXPR
, sizetype
, e
);
1388 if (TREE_CODE (e
) == COMPONENT_REF
1389 && DECL_BIT_FIELD (TREE_OPERAND (e
, 1)))
1390 error ("sizeof applied to a bit-field");
1391 /* ANSI says arrays and functions are converted inside comma.
1392 But we can't really convert them in build_compound_expr
1393 because that would break commas in lvalues.
1394 So do the conversion here if operand was a comma. */
1395 if (TREE_CODE (e
) == COMPOUND_EXPR
1396 && (TREE_CODE (TREE_TYPE (e
)) == ARRAY_TYPE
1397 || TREE_CODE (TREE_TYPE (e
)) == FUNCTION_TYPE
))
1398 e
= default_conversion (e
);
1399 else if (TREE_CODE (e
) == TREE_LIST
)
1401 tree t
= TREE_VALUE (e
);
1404 && TREE_CODE (TREE_TYPE (t
)) == FUNCTION_TYPE
)
1405 || is_overloaded_fn (t
)))
1406 pedwarn ("ANSI C++ forbids taking the sizeof a function type");
1408 return c_sizeof (TREE_TYPE (e
));
1412 c_sizeof_nowarn (type
)
1415 enum tree_code code
= TREE_CODE (type
);
1418 if (code
== FUNCTION_TYPE
1419 || code
== METHOD_TYPE
1420 || code
== VOID_TYPE
1421 || code
== ERROR_MARK
)
1422 return size_int (1);
1423 if (code
== REFERENCE_TYPE
)
1424 type
= TREE_TYPE (type
);
1426 if (TYPE_SIZE (type
) == 0)
1427 return size_int (0);
1429 /* Convert in case a char is more than one unit. */
1430 t
= size_binop (CEIL_DIV_EXPR
, TYPE_SIZE (type
),
1431 size_int (TYPE_PRECISION (char_type_node
)));
1432 force_fit_type (t
, 0);
1436 /* Implement the __alignof keyword: Return the minimum required
1437 alignment of TYPE, measured in bytes. */
1443 enum tree_code code
= TREE_CODE (type
);
1446 if (code
== FUNCTION_TYPE
|| code
== METHOD_TYPE
)
1447 return size_int (FUNCTION_BOUNDARY
/ BITS_PER_UNIT
);
1449 if (code
== VOID_TYPE
|| code
== ERROR_MARK
)
1450 return size_int (1);
1452 /* C++: this is really correct! */
1453 if (code
== REFERENCE_TYPE
)
1454 type
= TREE_TYPE (type
);
1456 /* @@ This also produces an error for a signature ref.
1457 In that case we should be able to do better. */
1458 if (IS_SIGNATURE (type
))
1460 error ("`__alignof' applied to a signature type");
1461 return size_int (1);
1464 t
= size_int (TYPE_ALIGN (type
) / BITS_PER_UNIT
);
1465 force_fit_type (t
, 0);
1469 /* Perform default promotions for C data used in expressions.
1470 Arrays and functions are converted to pointers;
1471 enumeral types or short or char, to int.
1472 In addition, manifest constants symbols are replaced by their values.
1474 C++: this will automatically bash references to their target type. */
1477 decay_conversion (exp
)
1480 register tree type
= TREE_TYPE (exp
);
1481 register enum tree_code code
= TREE_CODE (type
);
1483 if (code
== OFFSET_TYPE
)
1485 if (TREE_CODE (exp
) == OFFSET_REF
)
1486 return decay_conversion (resolve_offset_ref (exp
));
1488 type
= TREE_TYPE (type
);
1489 code
= TREE_CODE (type
);
1491 if (type
== unknown_type_node
)
1493 cp_pedwarn ("assuming & on overloaded member function");
1494 return build_unary_op (ADDR_EXPR
, exp
, 0);
1498 if (code
== REFERENCE_TYPE
)
1500 exp
= convert_from_reference (exp
);
1501 type
= TREE_TYPE (exp
);
1502 code
= TREE_CODE (type
);
1505 /* Constants can be used directly unless they're not loadable. */
1506 if (TREE_CODE (exp
) == CONST_DECL
)
1507 exp
= DECL_INITIAL (exp
);
1508 /* Replace a nonvolatile const static variable with its value. */
1509 else if (TREE_READONLY_DECL_P (exp
))
1511 exp
= decl_constant_value (exp
);
1512 type
= TREE_TYPE (exp
);
1515 /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
1516 Leave such NOP_EXPRs, since RHS is being used in non-lvalue context. */
1518 if (code
== VOID_TYPE
)
1520 error ("void value not ignored as it ought to be");
1521 return error_mark_node
;
1523 if (code
== FUNCTION_TYPE
)
1525 return build_unary_op (ADDR_EXPR
, exp
, 0);
1527 if (code
== METHOD_TYPE
)
1529 cp_pedwarn ("assuming & on `%E'", exp
);
1530 return build_unary_op (ADDR_EXPR
, exp
, 0);
1532 if (code
== ARRAY_TYPE
)
1537 int constp
, volatilep
;
1539 if (TREE_CODE (exp
) == INDIRECT_REF
)
1541 /* Stripping away the INDIRECT_REF is not the right
1542 thing to do for references... */
1543 tree inner
= TREE_OPERAND (exp
, 0);
1544 if (TREE_CODE (TREE_TYPE (inner
)) == REFERENCE_TYPE
)
1546 inner
= build1 (CONVERT_EXPR
,
1547 build_pointer_type (TREE_TYPE (TREE_TYPE (inner
))),
1549 TREE_CONSTANT (inner
) = TREE_CONSTANT (TREE_OPERAND (inner
, 0));
1551 return cp_convert (build_pointer_type (TREE_TYPE (type
)), inner
);
1554 if (TREE_CODE (exp
) == COMPOUND_EXPR
)
1556 tree op1
= decay_conversion (TREE_OPERAND (exp
, 1));
1557 return build (COMPOUND_EXPR
, TREE_TYPE (op1
),
1558 TREE_OPERAND (exp
, 0), op1
);
1562 && ! (TREE_CODE (exp
) == CONSTRUCTOR
&& TREE_STATIC (exp
)))
1564 error ("invalid use of non-lvalue array");
1565 return error_mark_node
;
1568 constp
= volatilep
= 0;
1569 if (TREE_CODE_CLASS (TREE_CODE (exp
)) == 'r'
1570 || TREE_CODE_CLASS (TREE_CODE (exp
)) == 'd')
1572 constp
= TREE_READONLY (exp
);
1573 volatilep
= TREE_THIS_VOLATILE (exp
);
1576 restype
= TREE_TYPE (type
);
1577 if (TYPE_READONLY (type
) || TYPE_VOLATILE (type
)
1578 || constp
|| volatilep
)
1579 restype
= cp_build_type_variant (restype
,
1580 TYPE_READONLY (type
) || constp
,
1581 TYPE_VOLATILE (type
) || volatilep
);
1582 ptrtype
= build_pointer_type (restype
);
1584 if (TREE_CODE (exp
) == VAR_DECL
)
1586 /* ??? This is not really quite correct
1587 in that the type of the operand of ADDR_EXPR
1588 is not the target type of the type of the ADDR_EXPR itself.
1589 Question is, can this lossage be avoided? */
1590 adr
= build1 (ADDR_EXPR
, ptrtype
, exp
);
1591 if (mark_addressable (exp
) == 0)
1592 return error_mark_node
;
1593 TREE_CONSTANT (adr
) = staticp (exp
);
1594 TREE_SIDE_EFFECTS (adr
) = 0; /* Default would be, same as EXP. */
1597 /* This way is better for a COMPONENT_REF since it can
1598 simplify the offset for a component. */
1599 adr
= build_unary_op (ADDR_EXPR
, exp
, 1);
1600 return cp_convert (ptrtype
, adr
);
1607 default_conversion (exp
)
1611 enum tree_code code
;
1613 exp
= decay_conversion (exp
);
1615 type
= TREE_TYPE (exp
);
1616 code
= TREE_CODE (type
);
1618 if (INTEGRAL_CODE_P (code
))
1620 tree t
= type_promotes_to (type
);
1622 return cp_convert (t
, exp
);
1628 /* Take the address of an inline function without setting TREE_ADDRESSABLE
1632 inline_conversion (exp
)
1635 if (TREE_CODE (exp
) == FUNCTION_DECL
)
1637 tree type
= build_type_variant
1638 (TREE_TYPE (exp
), TREE_READONLY (exp
), TREE_THIS_VOLATILE (exp
));
1639 exp
= build1 (ADDR_EXPR
, build_pointer_type (type
), exp
);
1645 build_object_ref (datum
, basetype
, field
)
1646 tree datum
, basetype
, field
;
1649 if (datum
== error_mark_node
)
1650 return error_mark_node
;
1652 dtype
= TREE_TYPE (datum
);
1653 if (TREE_CODE (dtype
) == REFERENCE_TYPE
)
1654 dtype
= TREE_TYPE (dtype
);
1655 if (! IS_AGGR_TYPE_CODE (TREE_CODE (dtype
)))
1657 cp_error ("request for member `%T::%D' in expression of non-aggregate type `%T'",
1658 basetype
, field
, dtype
);
1659 return error_mark_node
;
1661 else if (IS_SIGNATURE (basetype
))
1663 warning ("signature name in scope resolution ignored");
1664 return build_component_ref (datum
, field
, NULL_TREE
, 1);
1666 else if (is_aggr_type (basetype
, 1))
1668 tree binfo
= binfo_or_else (basetype
, dtype
);
1670 return build_x_component_ref (build_scoped_ref (datum
, basetype
),
1673 return error_mark_node
;
1676 /* Like `build_component_ref, but uses an already found field, and converts
1677 from a reference. Must compute access for current_class_ref.
1681 build_component_ref_1 (datum
, field
, protect
)
1685 return convert_from_reference
1686 (build_component_ref (datum
, field
, NULL_TREE
, protect
));
1689 /* Given a COND_EXPR, MIN_EXPR, or MAX_EXPR in T, return it in a form that we
1690 can, for example, use as an lvalue. This code used to be in
1691 unary_complex_lvalue, but we needed it to deal with `a = (d == c) ? b : c'
1692 expressions, where we're dealing with aggregates. But now it's again only
1693 called from unary_complex_lvalue. The case (in particular) that led to
1694 this was with CODE == ADDR_EXPR, since it's not an lvalue when we'd
1698 rationalize_conditional_expr (code
, t
)
1699 enum tree_code code
;
1702 /* For MIN_EXPR or MAX_EXPR, fold-const.c has arranged things so that
1703 the first operand is always the one to be used if both operands
1704 are equal, so we know what conditional expression this used to be. */
1705 if (TREE_CODE (t
) == MIN_EXPR
|| TREE_CODE (t
) == MAX_EXPR
)
1708 build_conditional_expr (build_x_binary_op ((TREE_CODE (t
) == MIN_EXPR
1709 ? LE_EXPR
: GE_EXPR
),
1710 TREE_OPERAND (t
, 0),
1711 TREE_OPERAND (t
, 1)),
1712 build_unary_op (code
, TREE_OPERAND (t
, 0), 0),
1713 build_unary_op (code
, TREE_OPERAND (t
, 1), 0));
1717 build_conditional_expr (TREE_OPERAND (t
, 0),
1718 build_unary_op (code
, TREE_OPERAND (t
, 1), 0),
1719 build_unary_op (code
, TREE_OPERAND (t
, 2), 0));
1722 /* Given the TYPE of an anonymous union field inside T, return the
1723 FIELD_DECL for the field. If not found return NULL_TREE. Because
1724 anonymous unions can nest, we must also search all anonymous unions
1725 that are directly reachable. */
1728 lookup_anon_field (t
, type
)
1733 for (field
= TYPE_FIELDS (t
); field
; field
= TREE_CHAIN (field
))
1735 if (TREE_STATIC (field
))
1737 if (TREE_CODE (field
) != FIELD_DECL
)
1740 /* If we find it directly, return the field. */
1741 if (DECL_NAME (field
) == NULL_TREE
1742 && type
== TREE_TYPE (field
))
1747 /* Otherwise, it could be nested, search harder. */
1748 if (DECL_NAME (field
) == NULL_TREE
1749 && TREE_CODE (TREE_TYPE (field
)) == UNION_TYPE
)
1751 tree subfield
= lookup_anon_field (TREE_TYPE (field
), type
);
1759 /* Build a COMPONENT_REF for a given DATUM, and it's member COMPONENT.
1760 COMPONENT can be an IDENTIFIER_NODE that is the name of the member
1761 that we are interested in, or it can be a FIELD_DECL. */
1764 build_component_ref (datum
, component
, basetype_path
, protect
)
1765 tree datum
, component
, basetype_path
;
1768 register tree basetype
= TREE_TYPE (datum
);
1769 register enum tree_code code
;
1770 register tree field
= NULL
;
1773 if (processing_template_decl
)
1774 return build_min_nt (COMPONENT_REF
, datum
, component
);
1776 /* If DATUM is a COMPOUND_EXPR or COND_EXPR, move our reference
1778 switch (TREE_CODE (datum
))
1782 tree value
= build_component_ref (TREE_OPERAND (datum
, 1), component
,
1783 basetype_path
, protect
);
1784 return build (COMPOUND_EXPR
, TREE_TYPE (value
),
1785 TREE_OPERAND (datum
, 0), value
);
1788 return build_conditional_expr
1789 (TREE_OPERAND (datum
, 0),
1790 build_component_ref (TREE_OPERAND (datum
, 1), component
,
1791 basetype_path
, protect
),
1792 build_component_ref (TREE_OPERAND (datum
, 2), component
,
1793 basetype_path
, protect
));
1799 code
= TREE_CODE (basetype
);
1801 if (code
== REFERENCE_TYPE
)
1803 datum
= convert_from_reference (datum
);
1804 basetype
= TREE_TYPE (datum
);
1805 code
= TREE_CODE (basetype
);
1807 if (TREE_CODE (datum
) == OFFSET_REF
)
1809 datum
= resolve_offset_ref (datum
);
1810 basetype
= TREE_TYPE (datum
);
1811 code
= TREE_CODE (basetype
);
1814 /* First, see if there is a field or component with name COMPONENT. */
1815 if (TREE_CODE (component
) == TREE_LIST
)
1817 my_friendly_assert (!(TREE_CHAIN (component
) == NULL_TREE
1818 && DECL_CHAIN (TREE_VALUE (component
)) == NULL_TREE
), 309);
1819 return build (COMPONENT_REF
, TREE_TYPE (component
), datum
, component
);
1822 if (! IS_AGGR_TYPE_CODE (code
))
1824 if (code
!= ERROR_MARK
)
1825 cp_error ("request for member `%D' in `%E', which is of non-aggregate type `%T'",
1826 component
, datum
, basetype
);
1827 return error_mark_node
;
1830 if (TYPE_SIZE (complete_type (basetype
)) == 0)
1832 incomplete_type_error (0, basetype
);
1833 return error_mark_node
;
1836 if (TREE_CODE (component
) == BIT_NOT_EXPR
)
1838 if (TYPE_IDENTIFIER (basetype
) != TREE_OPERAND (component
, 0))
1840 cp_error ("destructor specifier `%T::~%T' must have matching names",
1841 basetype
, TREE_OPERAND (component
, 0));
1842 return error_mark_node
;
1844 if (! TYPE_HAS_DESTRUCTOR (basetype
))
1846 cp_error ("type `%T' has no destructor", basetype
);
1847 return error_mark_node
;
1849 return TREE_VEC_ELT (CLASSTYPE_METHOD_VEC (basetype
), 1);
1852 /* Look up component name in the structure type definition. */
1853 if (CLASSTYPE_VFIELD (basetype
)
1854 && DECL_NAME (CLASSTYPE_VFIELD (basetype
)) == component
)
1855 /* Special-case this because if we use normal lookups in an ambiguous
1856 hierarchy, the compiler will abort (because vptr lookups are
1857 not supposed to be ambiguous. */
1858 field
= CLASSTYPE_VFIELD (basetype
);
1859 else if (TREE_CODE (component
) == FIELD_DECL
1860 || TREE_CODE (component
) == TYPE_DECL
)
1866 tree name
= component
;
1867 if (TREE_CODE (component
) == VAR_DECL
)
1868 name
= DECL_NAME (component
);
1869 if (basetype_path
== NULL_TREE
)
1870 basetype_path
= TYPE_BINFO (basetype
);
1871 field
= lookup_field (basetype_path
, name
,
1872 protect
&& !VFIELD_NAME_P (name
), 0);
1873 if (field
== error_mark_node
)
1874 return error_mark_node
;
1876 if (field
== NULL_TREE
)
1878 /* Not found as a data field, look for it as a method. If found,
1879 then if this is the only possible one, return it, else
1880 report ambiguity error. */
1881 tree fndecls
= lookup_fnfields (basetype_path
, name
, 1);
1882 if (fndecls
== error_mark_node
)
1883 return error_mark_node
;
1886 if (TREE_CHAIN (fndecls
) == NULL_TREE
1887 && DECL_CHAIN (TREE_VALUE (fndecls
)) == NULL_TREE
)
1889 tree access
, fndecl
;
1891 /* Unique, so use this one now. */
1892 basetype
= TREE_PURPOSE (fndecls
);
1893 fndecl
= TREE_VALUE (fndecls
);
1894 access
= compute_access (TREE_PURPOSE (fndecls
), fndecl
);
1895 if (access
== access_public_node
)
1897 if (DECL_VINDEX (fndecl
)
1898 && ! resolves_to_fixed_type_p (datum
, 0))
1900 tree addr
= build_unary_op (ADDR_EXPR
, datum
, 0);
1901 tree fntype
= TREE_TYPE (fndecl
);
1903 addr
= convert_pointer_to (DECL_CONTEXT (fndecl
), addr
);
1904 datum
= build_indirect_ref (addr
, NULL_PTR
);
1905 my_friendly_assert (datum
!= error_mark_node
, 310);
1906 fndecl
= build_vfn_ref (&addr
, datum
, DECL_VINDEX (fndecl
));
1907 TREE_TYPE (fndecl
) = build_pointer_type (fntype
);
1911 return build (OFFSET_REF
, TREE_TYPE (fndecl
), datum
, fndecl
);
1913 if (access
== access_protected_node
)
1914 cp_error ("member function `%D' is protected", fndecl
);
1916 cp_error ("member function `%D' is private", fndecl
);
1917 return error_mark_node
;
1921 /* Just act like build_offset_ref, since the object does
1922 not matter unless we're actually calling the function. */
1925 t
= build_tree_list (error_mark_node
, fndecls
);
1926 TREE_TYPE (t
) = build_offset_type (basetype
,
1932 cp_error ("`%#T' has no member named `%D'", basetype
, name
);
1933 return error_mark_node
;
1935 else if (TREE_TYPE (field
) == error_mark_node
)
1936 return error_mark_node
;
1938 if (TREE_CODE (field
) != FIELD_DECL
)
1940 if (TREE_CODE (field
) == TYPE_DECL
)
1942 cp_error ("invalid use of type decl `%#D' as expression", field
);
1943 return error_mark_node
;
1945 else if (DECL_RTL (field
) != 0)
1948 TREE_USED (field
) = 1;
1953 /* See if we have to do any conversions so that we pick up the field from the
1955 if (DECL_FIELD_CONTEXT (field
) != basetype
)
1957 tree context
= DECL_FIELD_CONTEXT (field
);
1958 tree base
= context
;
1959 while (base
!= basetype
&& TYPE_NAME (base
)
1960 && ANON_AGGRNAME_P (TYPE_IDENTIFIER (base
)))
1962 base
= TYPE_CONTEXT (base
);
1965 /* Handle base classes here... */
1966 if (base
!= basetype
&& TYPE_USES_COMPLEX_INHERITANCE (basetype
))
1968 tree addr
= build_unary_op (ADDR_EXPR
, datum
, 0);
1969 if (integer_zerop (addr
))
1971 error ("invalid reference to NULL ptr, use ptr-to-member instead");
1972 return error_mark_node
;
1974 if (VBASE_NAME_P (DECL_NAME (field
)))
1976 /* It doesn't matter which vbase pointer we grab, just
1977 find one of them. */
1978 tree binfo
= get_binfo (base
,
1979 TREE_TYPE (TREE_TYPE (addr
)), 0);
1980 addr
= convert_pointer_to_real (binfo
, addr
);
1983 addr
= convert_pointer_to (base
, addr
);
1984 datum
= build_indirect_ref (addr
, NULL_PTR
);
1985 my_friendly_assert (datum
!= error_mark_node
, 311);
1989 /* Handle things from anon unions here... */
1990 if (TYPE_NAME (context
) && ANON_AGGRNAME_P (TYPE_IDENTIFIER (context
)))
1992 tree subfield
= lookup_anon_field (basetype
, context
);
1993 tree subdatum
= build_component_ref (datum
, subfield
,
1994 basetype_path
, protect
);
1995 return build_component_ref (subdatum
, field
, basetype_path
, protect
);
1999 ref
= fold (build (COMPONENT_REF
, TREE_TYPE (field
),
2000 break_out_cleanups (datum
), field
));
2002 if (TREE_READONLY (datum
) || TREE_READONLY (field
))
2003 TREE_READONLY (ref
) = 1;
2004 if (TREE_THIS_VOLATILE (datum
) || TREE_THIS_VOLATILE (field
))
2005 TREE_THIS_VOLATILE (ref
) = 1;
2006 if (DECL_MUTABLE_P (field
))
2007 TREE_READONLY (ref
) = 0;
2012 /* Variant of build_component_ref for use in expressions, which should
2013 never have REFERENCE_TYPE. */
2016 build_x_component_ref (datum
, component
, basetype_path
, protect
)
2017 tree datum
, component
, basetype_path
;
2020 tree t
= build_component_ref (datum
, component
, basetype_path
, protect
);
2022 if (! processing_template_decl
)
2023 t
= convert_from_reference (t
);
2028 /* Given an expression PTR for a pointer, return an expression
2029 for the value pointed to.
2030 ERRORSTRING is the name of the operator to appear in error messages.
2032 This function may need to overload OPERATOR_FNNAME.
2033 Must also handle REFERENCE_TYPEs for C++. */
2036 build_x_indirect_ref (ptr
, errorstring
)
2042 if (processing_template_decl
)
2043 return build_min_nt (INDIRECT_REF
, ptr
);
2045 rval
= build_opfncall (INDIRECT_REF
, LOOKUP_NORMAL
, ptr
, NULL_TREE
, NULL_TREE
);
2048 return build_indirect_ref (ptr
, errorstring
);
2052 build_indirect_ref (ptr
, errorstring
)
2056 register tree pointer
, type
;
2058 if (ptr
== error_mark_node
)
2059 return error_mark_node
;
2061 pointer
= (TREE_CODE (TREE_TYPE (ptr
)) == REFERENCE_TYPE
2062 ? ptr
: default_conversion (ptr
));
2063 type
= TREE_TYPE (pointer
);
2065 if (ptr
== current_class_ptr
)
2066 return current_class_ref
;
2068 if (IS_AGGR_TYPE (type
))
2070 ptr
= build_expr_type_conversion (WANT_POINTER
, pointer
, 1);
2075 type
= TREE_TYPE (pointer
);
2079 if (TREE_CODE (type
) == POINTER_TYPE
|| TREE_CODE (type
) == REFERENCE_TYPE
)
2081 if (TREE_CODE (pointer
) == ADDR_EXPR
2083 && (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_OPERAND (pointer
, 0)))
2084 == TYPE_MAIN_VARIANT (TREE_TYPE (type
)))
2085 && (TREE_READONLY (TREE_OPERAND (pointer
, 0))
2086 == TYPE_READONLY (TREE_TYPE (type
)))
2087 && (TREE_THIS_VOLATILE (TREE_OPERAND (pointer
, 0))
2088 == TYPE_VOLATILE (TREE_TYPE (type
))))
2089 return TREE_OPERAND (pointer
, 0);
2092 tree t
= TREE_TYPE (type
);
2093 register tree ref
= build1 (INDIRECT_REF
,
2094 TYPE_MAIN_VARIANT (t
), pointer
);
2096 /* We *must* set TREE_READONLY when dereferencing a pointer to const,
2097 so that we get the proper error message if the result is used
2098 to assign to. Also, &* is supposed to be a no-op. */
2099 TREE_READONLY (ref
) = TYPE_READONLY (t
);
2100 TREE_SIDE_EFFECTS (ref
)
2101 = TYPE_VOLATILE (t
) || TREE_SIDE_EFFECTS (pointer
) || flag_volatile
;
2102 TREE_THIS_VOLATILE (ref
) = TYPE_VOLATILE (t
);
2106 /* `pointer' won't be an error_mark_node if we were given a
2107 pointer to member, so it's cool to check for this here. */
2108 else if (TYPE_PTRMEMFUNC_P (type
))
2109 error ("invalid use of `%s' on pointer to member function", errorstring
);
2110 else if (TREE_CODE (type
) == RECORD_TYPE
2111 && (IS_SIGNATURE_POINTER (type
) || IS_SIGNATURE_REFERENCE (type
)))
2112 error ("cannot dereference signature pointer/reference");
2113 else if (pointer
!= error_mark_node
)
2116 error ("invalid type argument of `%s'", errorstring
);
2118 error ("invalid type argument");
2120 return error_mark_node
;
2123 /* This handles expressions of the form "a[i]", which denotes
2126 This is logically equivalent in C to *(a+i), but we may do it differently.
2127 If A is a variable or a member, we generate a primitive ARRAY_REF.
2128 This avoids forcing the array out of registers, and can work on
2129 arrays that are not lvalues (for example, members of structures returned
2132 If INDEX is of some user-defined type, it must be converted to
2133 integer type. Otherwise, to make a compatible PLUS_EXPR, it
2134 will inherit the type of the array, which will be some pointer type. */
2137 build_array_ref (array
, idx
)
2142 error ("subscript missing in array reference");
2143 return error_mark_node
;
2146 if (TREE_TYPE (array
) == error_mark_node
2147 || TREE_TYPE (idx
) == error_mark_node
)
2148 return error_mark_node
;
2150 if (TREE_CODE (TREE_TYPE (array
)) == ARRAY_TYPE
2151 && TREE_CODE (array
) != INDIRECT_REF
)
2155 /* Subscripting with type char is likely to lose
2156 on a machine where chars are signed.
2157 So warn on any machine, but optionally.
2158 Don't warn for unsigned char since that type is safe.
2159 Don't warn for signed char because anyone who uses that
2160 must have done so deliberately. */
2161 if (warn_char_subscripts
2162 && TYPE_MAIN_VARIANT (TREE_TYPE (idx
)) == char_type_node
)
2163 warning ("array subscript has type `char'");
2165 /* Apply default promotions *after* noticing character types. */
2166 idx
= default_conversion (idx
);
2168 if (TREE_CODE (TREE_TYPE (idx
)) != INTEGER_TYPE
)
2170 error ("array subscript is not an integer");
2171 return error_mark_node
;
2174 /* An array that is indexed by a non-constant
2175 cannot be stored in a register; we must be able to do
2176 address arithmetic on its address.
2177 Likewise an array of elements of variable size. */
2178 if (TREE_CODE (idx
) != INTEGER_CST
2179 || (TYPE_SIZE (TREE_TYPE (TREE_TYPE (array
))) != 0
2180 && TREE_CODE (TYPE_SIZE (TREE_TYPE (TREE_TYPE (array
)))) != INTEGER_CST
))
2182 if (mark_addressable (array
) == 0)
2183 return error_mark_node
;
2185 /* An array that is indexed by a constant value which is not within
2186 the array bounds cannot be stored in a register either; because we
2187 would get a crash in store_bit_field/extract_bit_field when trying
2188 to access a non-existent part of the register. */
2189 if (TREE_CODE (idx
) == INTEGER_CST
2190 && TYPE_VALUES (TREE_TYPE (array
))
2191 && ! int_fits_type_p (idx
, TYPE_VALUES (TREE_TYPE (array
))))
2193 if (mark_addressable (array
) == 0)
2194 return error_mark_node
;
2197 if (pedantic
&& !lvalue_p (array
))
2198 pedwarn ("ANSI C++ forbids subscripting non-lvalue array");
2200 /* Note in C++ it is valid to subscript a `register' array, since
2201 it is valid to take the address of something with that
2202 storage specification. */
2206 while (TREE_CODE (foo
) == COMPONENT_REF
)
2207 foo
= TREE_OPERAND (foo
, 0);
2208 if (TREE_CODE (foo
) == VAR_DECL
&& DECL_REGISTER (foo
))
2209 warning ("subscripting array declared `register'");
2212 type
= TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (array
)));
2213 rval
= build (ARRAY_REF
, type
, array
, idx
);
2214 /* Array ref is const/volatile if the array elements are
2215 or if the array is.. */
2216 TREE_READONLY (rval
)
2217 |= (TYPE_READONLY (TREE_TYPE (TREE_TYPE (array
)))
2218 | TREE_READONLY (array
));
2219 TREE_SIDE_EFFECTS (rval
)
2220 |= (TYPE_VOLATILE (TREE_TYPE (TREE_TYPE (array
)))
2221 | TREE_SIDE_EFFECTS (array
));
2222 TREE_THIS_VOLATILE (rval
)
2223 |= (TYPE_VOLATILE (TREE_TYPE (TREE_TYPE (array
)))
2224 /* This was added by rms on 16 Nov 91.
2225 It fixes vol struct foo *a; a->elts[1]
2226 in an inline function.
2227 Hope it doesn't break something else. */
2228 | TREE_THIS_VOLATILE (array
));
2229 return require_complete_type (fold (rval
));
2233 tree ar
= default_conversion (array
);
2234 tree ind
= default_conversion (idx
);
2236 /* Put the integer in IND to simplify error checking. */
2237 if (TREE_CODE (TREE_TYPE (ar
)) == INTEGER_TYPE
)
2244 if (ar
== error_mark_node
)
2247 if (TREE_CODE (TREE_TYPE (ar
)) != POINTER_TYPE
)
2249 error ("subscripted value is neither array nor pointer");
2250 return error_mark_node
;
2252 if (TREE_CODE (TREE_TYPE (ind
)) != INTEGER_TYPE
)
2254 error ("array subscript is not an integer");
2255 return error_mark_node
;
2258 return build_indirect_ref (build_binary_op_nodefault (PLUS_EXPR
, ar
, ind
, PLUS_EXPR
),
2263 /* Build a function call to function FUNCTION with parameters PARAMS.
2264 PARAMS is a list--a chain of TREE_LIST nodes--in which the
2265 TREE_VALUE of each node is a parameter-expression. The PARAMS do
2266 not include any object pointer that may be required. FUNCTION's
2267 data type may be a function type or a pointer-to-function.
2269 For C++: If FUNCTION's data type is a TREE_LIST, then the tree list
2270 is the list of possible methods that FUNCTION could conceivably
2271 be. If the list of methods comes from a class, then it will be
2272 a list of lists (where each element is associated with the class
2273 that produced it), otherwise it will be a simple list (for
2274 functions overloaded in global scope).
2276 In the first case, TREE_VALUE (function) is the head of one of those
2277 lists, and TREE_PURPOSE is the name of the function.
2279 In the second case, TREE_PURPOSE (function) is the function's
2282 DECL is the class instance variable, usually CURRENT_CLASS_REF.
2284 When calling a TEMPLATE_DECL, we don't require a complete return
2288 build_x_function_call (function
, params
, decl
)
2289 tree function
, params
, decl
;
2292 tree template_id
= NULL_TREE
;
2295 if (function
== error_mark_node
)
2296 return error_mark_node
;
2298 if (processing_template_decl
)
2299 return build_min_nt (CALL_EXPR
, function
, params
, NULL_TREE
);
2301 /* Save explicit template arguments if found */
2302 if (TREE_CODE (function
) == TEMPLATE_ID_EXPR
)
2304 template_id
= function
;
2305 function
= TREE_OPERAND (function
, 0);
2308 type
= TREE_TYPE (function
);
2310 if (TREE_CODE (type
) == OFFSET_TYPE
2311 && TREE_TYPE (type
) == unknown_type_node
2312 && TREE_CODE (function
) == TREE_LIST
2313 && TREE_CHAIN (function
) == NULL_TREE
)
2315 /* Undo (Foo:bar)()... */
2316 type
= TYPE_OFFSET_BASETYPE (type
);
2317 function
= TREE_VALUE (function
);
2318 my_friendly_assert (TREE_CODE (function
) == TREE_LIST
, 999);
2319 my_friendly_assert (TREE_CHAIN (function
) == NULL_TREE
, 999);
2320 function
= TREE_VALUE (function
);
2321 my_friendly_assert (TREE_CODE (function
) == FUNCTION_DECL
, 999);
2322 function
= DECL_NAME (function
);
2323 return build_method_call (decl
, function
, params
, TYPE_BINFO (type
), LOOKUP_NORMAL
);
2326 is_method
= ((TREE_CODE (function
) == TREE_LIST
2327 && current_class_type
!= NULL_TREE
2328 && IDENTIFIER_CLASS_VALUE (TREE_PURPOSE (function
)) == function
)
2329 || TREE_CODE (function
) == IDENTIFIER_NODE
2330 || TREE_CODE (type
) == METHOD_TYPE
2331 || TYPE_PTRMEMFUNC_P (type
));
2333 if (TREE_CODE (function
) == FUNCTION_DECL
2334 && DECL_STATIC_FUNCTION_P (function
))
2335 return build_member_call
2336 (DECL_CONTEXT (function
), DECL_NAME (function
), params
);
2338 /* Handle methods, friends, and overloaded functions, respectively. */
2341 tree basetype
= NULL_TREE
;
2343 if (TREE_CODE (function
) == FUNCTION_DECL
2344 || DECL_FUNCTION_TEMPLATE_P (function
))
2346 basetype
= DECL_CLASS_CONTEXT (function
);
2348 if (DECL_NAME (function
))
2349 function
= DECL_NAME (function
);
2351 function
= TYPE_IDENTIFIER (DECL_CLASS_CONTEXT (function
));
2353 else if (TREE_CODE (function
) == TREE_LIST
)
2355 my_friendly_assert (TREE_CODE (TREE_VALUE (function
)) == FUNCTION_DECL
, 312);
2356 basetype
= DECL_CLASS_CONTEXT (TREE_VALUE (function
));
2357 function
= TREE_PURPOSE (function
);
2359 else if (TREE_CODE (function
) != IDENTIFIER_NODE
)
2361 if (TREE_CODE (function
) == OFFSET_REF
)
2363 if (TREE_OPERAND (function
, 0))
2364 decl
= TREE_OPERAND (function
, 0);
2366 /* Call via a pointer to member function. */
2367 if (decl
== NULL_TREE
)
2369 error ("pointer to member function called, but not in class scope");
2370 return error_mark_node
;
2372 /* What other type of POINTER_TYPE could this be? */
2373 if (TREE_CODE (TREE_TYPE (function
)) != POINTER_TYPE
2374 && ! TYPE_PTRMEMFUNC_P (TREE_TYPE (function
))
2375 && TREE_CODE (function
) != OFFSET_REF
)
2376 function
= build (OFFSET_REF
, TREE_TYPE (type
), NULL_TREE
, function
);
2380 /* this is an abbreviated method call.
2381 must go through here in case it is a virtual function.
2382 @@ Perhaps this could be optimized. */
2384 if (basetype
&& (! current_class_type
2385 || ! DERIVED_FROM_P (basetype
, current_class_type
)))
2386 return build_member_call (basetype
, function
, params
);
2388 if (decl
== NULL_TREE
)
2390 if (current_class_type
== NULL_TREE
)
2392 error ("object missing in call to method `%s'",
2393 IDENTIFIER_POINTER (function
));
2394 return error_mark_node
;
2396 /* Yow: call from a static member function. */
2397 decl
= build1 (NOP_EXPR
, build_pointer_type (current_class_type
),
2399 decl
= build_indirect_ref (decl
, NULL_PTR
);
2402 /* Put back explicit template arguments, if any. */
2404 function
= template_id
;
2405 return build_method_call (decl
, function
, params
,
2406 NULL_TREE
, LOOKUP_NORMAL
);
2408 else if (TREE_CODE (function
) == COMPONENT_REF
2409 && type
== unknown_type_node
)
2411 /* Should we undo what was done in build_component_ref? */
2412 if (TREE_CODE (TREE_PURPOSE (TREE_OPERAND (function
, 1))) == TREE_VEC
)
2413 /* Get the name that build_component_ref hid. */
2414 function
= DECL_NAME (TREE_VALUE (TREE_OPERAND (function
, 1)));
2416 function
= TREE_PURPOSE (TREE_OPERAND (function
, 1));
2417 return build_method_call (decl
, function
, params
,
2418 NULL_TREE
, LOOKUP_NORMAL
);
2420 else if (really_overloaded_fn (function
))
2422 if (TREE_VALUE (function
) == NULL_TREE
)
2424 cp_error ("function `%D' declared overloaded, but no definitions appear with which to resolve it?!?",
2425 TREE_PURPOSE (function
));
2426 return error_mark_node
;
2430 tree val
= TREE_VALUE (function
);
2432 if (flag_ansi_overloading
)
2434 /* Put back explicit template arguments, if any. */
2436 function
= template_id
;
2437 return build_new_function_call (function
, params
);
2440 if (TREE_CODE (val
) == TEMPLATE_DECL
)
2441 return build_overload_call_real
2442 (function
, params
, LOOKUP_COMPLAIN
, (struct candidate
*)0, 0);
2443 else if (DECL_CHAIN (val
) != NULL_TREE
)
2444 return build_overload_call
2445 (function
, params
, LOOKUP_COMPLAIN
);
2447 my_friendly_abort (360);
2452 if (TREE_CODE (function
) == OFFSET_REF
)
2454 /* If the component is a data element (or a virtual function), we play
2455 games here to make things work. */
2458 if (TREE_OPERAND (function
, 0))
2459 decl
= TREE_OPERAND (function
, 0);
2461 decl
= current_class_ref
;
2463 decl_addr
= build_unary_op (ADDR_EXPR
, decl
, 0);
2464 function
= get_member_function_from_ptrfunc (&decl_addr
,
2465 TREE_OPERAND (function
, 1));
2466 params
= expr_tree_cons (NULL_TREE
, decl_addr
, params
);
2467 return build_function_call (function
, params
);
2470 type
= TREE_TYPE (function
);
2471 if (type
!= error_mark_node
)
2473 if (TREE_CODE (type
) == REFERENCE_TYPE
)
2474 type
= TREE_TYPE (type
);
2476 if (IS_AGGR_TYPE (type
))
2477 return build_opfncall (CALL_EXPR
, LOOKUP_NORMAL
, function
, params
, NULL_TREE
);
2482 tree fntype
= TREE_TYPE (function
);
2485 /* Explicitly named method? */
2486 if (TREE_CODE (function
) == FUNCTION_DECL
)
2487 ctypeptr
= build_pointer_type (DECL_CLASS_CONTEXT (function
));
2488 /* Expression with ptr-to-method type? It could either be a plain
2489 usage, or it might be a case where the ptr-to-method is being
2490 passed in as an argument. */
2491 else if (TYPE_PTRMEMFUNC_P (fntype
))
2493 tree rec
= TYPE_METHOD_BASETYPE (TREE_TYPE (TYPE_PTRMEMFUNC_FN_TYPE (fntype
)));
2494 ctypeptr
= build_pointer_type (rec
);
2496 /* Unexpected node type? */
2498 my_friendly_abort (116);
2499 if (decl
== NULL_TREE
)
2501 if (current_function_decl
2502 && DECL_STATIC_FUNCTION_P (current_function_decl
))
2503 error ("invalid call to member function needing `this' in static member function scope");
2505 error ("pointer to member function called, but not in class scope");
2506 return error_mark_node
;
2508 if (TREE_CODE (TREE_TYPE (decl
)) != POINTER_TYPE
2509 && ! TYPE_PTRMEMFUNC_P (TREE_TYPE (decl
)))
2511 decl
= build_unary_op (ADDR_EXPR
, decl
, 0);
2512 decl
= convert_pointer_to (TREE_TYPE (ctypeptr
), decl
);
2515 decl
= build_c_cast (ctypeptr
, decl
);
2516 params
= expr_tree_cons (NULL_TREE
, decl
, params
);
2519 return build_function_call (function
, params
);
2522 /* Resolve a pointer to member function. INSTANCE is the object
2523 instance to use, if the member points to a virtual member. */
2526 get_member_function_from_ptrfunc (instance_ptrptr
, function
)
2527 tree
*instance_ptrptr
;
2530 if (TREE_CODE (function
) == OFFSET_REF
)
2532 function
= TREE_OPERAND (function
, 1);
2535 if (TYPE_PTRMEMFUNC_P (TREE_TYPE (function
)))
2537 tree fntype
, idx
, e1
, delta
, delta2
, e2
, e3
, aref
, vtbl
;
2540 tree instance_ptr
= *instance_ptrptr
;
2542 if (TREE_SIDE_EFFECTS (instance_ptr
))
2543 instance_ptr
= save_expr (instance_ptr
);
2545 if (TREE_SIDE_EFFECTS (function
))
2546 function
= save_expr (function
);
2548 fntype
= TYPE_PTRMEMFUNC_FN_TYPE (TREE_TYPE (function
));
2550 /* Promoting idx before saving it improves performance on RISC
2551 targets. Without promoting, the first compare used
2552 load-with-sign-extend, while the second used normal load then
2553 shift to sign-extend. An optimizer flaw, perhaps, but it's easier
2554 to make this change. */
2555 idx
= save_expr (default_conversion
2556 (build_component_ref (function
,
2559 e1
= build_binary_op (GT_EXPR
, idx
, integer_zero_node
, 1);
2560 delta
= cp_convert (ptrdiff_type_node
,
2561 build_component_ref (function
, delta_identifier
, NULL_TREE
, 0));
2562 delta2
= DELTA2_FROM_PTRMEMFUNC (function
);
2564 /* Convert down to the right base, before using the instance. */
2566 = convert_pointer_to_real (TYPE_METHOD_BASETYPE (TREE_TYPE (fntype
)),
2568 if (instance
== error_mark_node
&& instance_ptr
!= error_mark_node
)
2571 vtbl
= convert_pointer_to (ptr_type_node
, instance
);
2574 build_pointer_type (build_pointer_type (vtable_entry_type
)),
2575 vtbl
, cp_convert (ptrdiff_type_node
, delta2
));
2576 vtbl
= build_indirect_ref (vtbl
, NULL_PTR
);
2577 aref
= build_array_ref (vtbl
, build_binary_op (MINUS_EXPR
,
2579 integer_one_node
, 1));
2580 if (! flag_vtable_thunks
)
2582 aref
= save_expr (aref
);
2584 delta
= build_binary_op (PLUS_EXPR
,
2585 build_conditional_expr (e1
, build_component_ref (aref
, delta_identifier
, NULL_TREE
, 0), integer_zero_node
),
2589 *instance_ptrptr
= build (PLUS_EXPR
, TREE_TYPE (instance_ptr
),
2590 instance_ptr
, delta
);
2591 if (flag_vtable_thunks
)
2594 e2
= build_component_ref (aref
, pfn_identifier
, NULL_TREE
, 0);
2596 e3
= PFN_FROM_PTRMEMFUNC (function
);
2597 TREE_TYPE (e2
) = TREE_TYPE (e3
);
2598 e1
= build_conditional_expr (e1
, e2
, e3
);
2600 if (instance_ptr
== error_mark_node
2601 && TREE_CODE (e1
) != ADDR_EXPR
2602 && TREE_CODE (TREE_OPERAND (e1
, 0)) != FUNCTION_DECL
)
2603 cp_error ("object missing in `%E'", function
);
2607 /* Make sure this doesn't get evaluated first inside one of the
2608 branches of the COND_EXPR. */
2609 if (TREE_CODE (instance_ptr
) == SAVE_EXPR
)
2610 function
= build (COMPOUND_EXPR
, TREE_TYPE (function
),
2611 instance_ptr
, function
);
2617 build_function_call_real (function
, params
, require_complete
, flags
)
2618 tree function
, params
;
2619 int require_complete
, flags
;
2621 register tree fntype
, fndecl
;
2622 register tree value_type
;
2623 register tree coerced_params
;
2624 tree name
= NULL_TREE
, assembler_name
= NULL_TREE
;
2627 /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
2628 Strip such NOP_EXPRs, since FUNCTION is used in non-lvalue context. */
2629 if (TREE_CODE (function
) == NOP_EXPR
2630 && TREE_TYPE (function
) == TREE_TYPE (TREE_OPERAND (function
, 0)))
2631 function
= TREE_OPERAND (function
, 0);
2633 if (TREE_CODE (function
) == FUNCTION_DECL
)
2635 name
= DECL_NAME (function
);
2636 assembler_name
= DECL_ASSEMBLER_NAME (function
);
2638 GNU_xref_call (current_function_decl
,
2639 IDENTIFIER_POINTER (name
? name
2640 : TYPE_IDENTIFIER (DECL_CLASS_CONTEXT (function
))));
2641 mark_used (function
);
2644 /* Convert anything with function type to a pointer-to-function. */
2647 && IDENTIFIER_LENGTH (name
) == 4
2648 && ! strcmp (IDENTIFIER_POINTER (name
), "main")
2649 && DECL_CONTEXT (function
) == NULL_TREE
)
2651 pedwarn ("ANSI C++ forbids calling `main' from within program");
2654 /* Differs from default_conversion by not setting TREE_ADDRESSABLE
2655 (because calling an inline function does not mean the function
2656 needs to be separately compiled). */
2658 if (DECL_INLINE (function
))
2659 function
= inline_conversion (function
);
2661 function
= build_addr_func (function
);
2667 function
= build_addr_func (function
);
2670 if (function
== error_mark_node
)
2671 return error_mark_node
;
2673 fntype
= TREE_TYPE (function
);
2675 if (TYPE_PTRMEMFUNC_P (fntype
))
2677 cp_error ("must use .* or ->* to call pointer-to-member function in `%E (...)'",
2679 return error_mark_node
;
2682 is_method
= (TREE_CODE (fntype
) == POINTER_TYPE
2683 && TREE_CODE (TREE_TYPE (fntype
)) == METHOD_TYPE
);
2685 if (!((TREE_CODE (fntype
) == POINTER_TYPE
2686 && TREE_CODE (TREE_TYPE (fntype
)) == FUNCTION_TYPE
)
2688 || TREE_CODE (function
) == TEMPLATE_ID_EXPR
))
2690 cp_error ("`%E' cannot be used as a function", function
);
2691 return error_mark_node
;
2694 /* fntype now gets the type of function pointed to. */
2695 fntype
= TREE_TYPE (fntype
);
2697 /* Convert the parameters to the types declared in the
2698 function prototype, or apply default promotions. */
2700 if (flags
& LOOKUP_COMPLAIN
)
2701 coerced_params
= convert_arguments (NULL_TREE
, TYPE_ARG_TYPES (fntype
),
2702 params
, fndecl
, LOOKUP_NORMAL
);
2704 coerced_params
= convert_arguments (NULL_TREE
, TYPE_ARG_TYPES (fntype
),
2707 if (coerced_params
== error_mark_node
)
2708 if (flags
& LOOKUP_SPECULATIVELY
)
2711 return error_mark_node
;
2713 /* Check for errors in format strings. */
2715 if (warn_format
&& (name
|| assembler_name
))
2716 check_function_format (name
, assembler_name
, coerced_params
);
2718 /* Recognize certain built-in functions so we can make tree-codes
2719 other than CALL_EXPR. We do this when it enables fold-const.c
2720 to do something useful. */
2722 if (TREE_CODE (function
) == ADDR_EXPR
2723 && TREE_CODE (TREE_OPERAND (function
, 0)) == FUNCTION_DECL
2724 && DECL_BUILT_IN (TREE_OPERAND (function
, 0)))
2725 switch (DECL_FUNCTION_CODE (TREE_OPERAND (function
, 0)))
2730 if (coerced_params
== 0)
2731 return integer_zero_node
;
2732 return build_unary_op (ABS_EXPR
, TREE_VALUE (coerced_params
), 0);
2739 value_type
= TREE_TYPE (fntype
) ? TREE_TYPE (fntype
) : void_type_node
;
2741 register tree result
2742 = build_call (function
, value_type
, coerced_params
);
2744 if (require_complete
)
2746 if (value_type
== void_type_node
)
2748 result
= require_complete_type (result
);
2750 if (IS_AGGR_TYPE (value_type
))
2751 result
= build_cplus_new (value_type
, result
);
2752 return convert_from_reference (result
);
2757 build_function_call (function
, params
)
2758 tree function
, params
;
2760 return build_function_call_real (function
, params
, 1, LOOKUP_NORMAL
);
2763 /* Convert the actual parameter expressions in the list VALUES
2764 to the types in the list TYPELIST.
2765 If parmdecls is exhausted, or when an element has NULL as its type,
2766 perform the default conversions.
2768 RETURN_LOC is the location of the return value, if known, NULL_TREE
2769 otherwise. This is useful in the case where we can avoid creating
2770 a temporary variable in the case where we can initialize the return
2771 value directly. If we are not eliding constructors, then we set this
2772 to NULL_TREE to avoid this avoidance.
2774 NAME is an IDENTIFIER_NODE or 0. It is used only for error messages.
2776 This is also where warnings about wrong number of args are generated.
2778 Return a list of expressions for the parameters as converted.
2780 Both VALUES and the returned value are chains of TREE_LIST nodes
2781 with the elements of the list in the TREE_VALUE slots of those nodes.
2783 In C++, unspecified trailing parameters can be filled in with their
2784 default arguments, if such were specified. Do so here. */
2787 convert_arguments (return_loc
, typelist
, values
, fndecl
, flags
)
2788 tree return_loc
, typelist
, values
, fndecl
;
2791 register tree typetail
, valtail
;
2792 register tree result
= NULL_TREE
;
2796 if (! flag_elide_constructors
)
2799 /* Argument passing is always copy-initialization. */
2800 flags
|= LOOKUP_ONLYCONVERTING
;
2804 if (TREE_CODE (TREE_TYPE (fndecl
)) == METHOD_TYPE
)
2806 if (DECL_NAME (fndecl
) == NULL_TREE
2807 || IDENTIFIER_HAS_TYPE_VALUE (DECL_NAME (fndecl
)))
2808 called_thing
= "constructor";
2810 called_thing
= "member function";
2813 called_thing
= "function";
2816 for (valtail
= values
, typetail
= typelist
;
2818 valtail
= TREE_CHAIN (valtail
), i
++)
2820 register tree type
= typetail
? TREE_VALUE (typetail
) : 0;
2821 register tree val
= TREE_VALUE (valtail
);
2823 if (val
== error_mark_node
)
2824 return error_mark_node
;
2826 if (type
== void_type_node
)
2830 cp_error_at ("too many arguments to %s `%+D'", called_thing
,
2832 error ("at this point in file");
2835 error ("too many arguments to function");
2836 /* In case anybody wants to know if this argument
2839 TREE_TYPE (tree_last (result
)) = error_mark_node
;
2843 /* The tree type of the parameter being passed may not yet be
2844 known. In this case, its type is TYPE_UNKNOWN, and will
2845 be instantiated by the type given by TYPE. If TYPE
2846 is also NULL, the tree type of VAL is ERROR_MARK_NODE. */
2847 if (type
&& type_unknown_p (val
))
2848 val
= require_instantiated_type (type
, val
, integer_zero_node
);
2849 else if (type_unknown_p (val
))
2851 /* Strip the `&' from an overloaded FUNCTION_DECL. */
2852 if (TREE_CODE (val
) == ADDR_EXPR
)
2853 val
= TREE_OPERAND (val
, 0);
2854 if (really_overloaded_fn (val
))
2855 cp_error ("insufficient type information to resolve address of overloaded function `%D'",
2856 DECL_NAME (get_first_fn (val
)));
2858 error ("insufficient type information in parameter list");
2859 val
= integer_zero_node
;
2861 else if (TREE_CODE (val
) == OFFSET_REF
2862 && TREE_CODE (TREE_TYPE (val
)) == METHOD_TYPE
)
2864 /* This is unclean. Should be handled elsewhere. */
2865 val
= build_unary_op (ADDR_EXPR
, val
, 0);
2867 else if (TREE_CODE (val
) == OFFSET_REF
)
2868 val
= resolve_offset_ref (val
);
2870 /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
2871 Strip such NOP_EXPRs, since VAL is used in non-lvalue context. */
2872 if (TREE_CODE (val
) == NOP_EXPR
2873 && TREE_TYPE (val
) == TREE_TYPE (TREE_OPERAND (val
, 0))
2874 && (type
== 0 || TREE_CODE (type
) != REFERENCE_TYPE
))
2875 val
= TREE_OPERAND (val
, 0);
2877 if (type
== 0 || TREE_CODE (type
) != REFERENCE_TYPE
)
2879 if (TREE_CODE (TREE_TYPE (val
)) == ARRAY_TYPE
2880 || TREE_CODE (TREE_TYPE (val
)) == FUNCTION_TYPE
2881 || TREE_CODE (TREE_TYPE (val
)) == METHOD_TYPE
)
2882 val
= default_conversion (val
);
2884 val
= require_complete_type (val
);
2887 if (val
== error_mark_node
)
2888 return error_mark_node
;
2892 /* Formal parm type is specified by a function prototype. */
2895 if (TYPE_SIZE (complete_type (type
)) == 0)
2897 error ("parameter type of called function is incomplete");
2902 parmval
= convert_for_initialization
2903 (return_loc
, type
, val
, flags
,
2904 "argument passing", fndecl
, i
);
2905 #ifdef PROMOTE_PROTOTYPES
2906 if ((TREE_CODE (type
) == INTEGER_TYPE
2907 || TREE_CODE (type
) == ENUMERAL_TYPE
)
2908 && (TYPE_PRECISION (type
) < TYPE_PRECISION (integer_type_node
)))
2909 parmval
= default_conversion (parmval
);
2913 if (parmval
== error_mark_node
)
2914 return error_mark_node
;
2916 result
= expr_tree_cons (NULL_TREE
, parmval
, result
);
2920 if (TREE_CODE (TREE_TYPE (val
)) == REFERENCE_TYPE
)
2921 val
= convert_from_reference (val
);
2923 if (TREE_CODE (TREE_TYPE (val
)) == REAL_TYPE
2924 && (TYPE_PRECISION (TREE_TYPE (val
))
2925 < TYPE_PRECISION (double_type_node
)))
2926 /* Convert `float' to `double'. */
2927 result
= expr_tree_cons (NULL_TREE
, cp_convert (double_type_node
, val
), result
);
2928 else if (TYPE_LANG_SPECIFIC (TREE_TYPE (val
))
2929 && ! TYPE_HAS_TRIVIAL_INIT_REF (TREE_TYPE (val
)))
2931 cp_warning ("cannot pass objects of type `%T' through `...'",
2933 result
= expr_tree_cons (NULL_TREE
, val
, result
);
2936 /* Convert `short' and `char' to full-size `int'. */
2937 result
= expr_tree_cons (NULL_TREE
, default_conversion (val
), result
);
2941 typetail
= TREE_CHAIN (typetail
);
2944 if (typetail
!= 0 && typetail
!= void_list_node
)
2946 /* See if there are default arguments that can be used */
2947 if (TREE_PURPOSE (typetail
))
2949 for (; typetail
!= void_list_node
; ++i
)
2951 tree type
= TREE_VALUE (typetail
);
2952 tree val
= break_out_target_exprs (TREE_PURPOSE (typetail
));
2955 if (val
== NULL_TREE
)
2956 parmval
= error_mark_node
;
2957 else if (TREE_CODE (val
) == CONSTRUCTOR
)
2959 parmval
= digest_init (type
, val
, (tree
*)0);
2960 parmval
= convert_for_initialization (return_loc
, type
, parmval
, flags
,
2961 "default constructor", fndecl
, i
);
2965 /* This could get clobbered by the following call. */
2966 if (TREE_HAS_CONSTRUCTOR (val
))
2967 val
= copy_node (val
);
2969 parmval
= convert_for_initialization (return_loc
, type
, val
, flags
,
2970 "default argument", fndecl
, i
);
2971 #ifdef PROMOTE_PROTOTYPES
2972 if ((TREE_CODE (type
) == INTEGER_TYPE
2973 || TREE_CODE (type
) == ENUMERAL_TYPE
)
2974 && (TYPE_PRECISION (type
) < TYPE_PRECISION (integer_type_node
)))
2975 parmval
= default_conversion (parmval
);
2979 if (parmval
== error_mark_node
)
2980 return error_mark_node
;
2982 result
= expr_tree_cons (0, parmval
, result
);
2983 typetail
= TREE_CHAIN (typetail
);
2984 /* ends with `...'. */
2985 if (typetail
== NULL_TREE
)
2993 char *buf
= (char *)alloca (32 + strlen (called_thing
));
2994 sprintf (buf
, "too few arguments to %s `%%#D'", called_thing
);
2995 cp_error_at (buf
, fndecl
);
2996 error ("at this point in file");
2999 error ("too few arguments to function");
3000 return error_mark_list
;
3004 return nreverse (result
);
3007 /* Build a binary-operation expression, after performing default
3008 conversions on the operands. CODE is the kind of expression to build. */
3011 build_x_binary_op (code
, arg1
, arg2
)
3012 enum tree_code code
;
3017 if (processing_template_decl
)
3018 return build_min_nt (code
, arg1
, arg2
);
3020 if (flag_ansi_overloading
)
3021 return build_new_op (code
, LOOKUP_NORMAL
, arg1
, arg2
, NULL_TREE
);
3023 rval
= build_opfncall (code
, LOOKUP_SPECULATIVELY
,
3024 arg1
, arg2
, NULL_TREE
);
3026 return build_opfncall (code
, LOOKUP_NORMAL
, arg1
, arg2
, NULL_TREE
);
3027 if (code
== MEMBER_REF
)
3028 return build_m_component_ref (build_indirect_ref (arg1
, NULL_PTR
),
3030 return build_binary_op (code
, arg1
, arg2
, 1);
3034 build_binary_op (code
, arg1
, arg2
, convert_p
)
3035 enum tree_code code
;
3047 args
[0] = decay_conversion (args
[0]);
3048 args
[1] = decay_conversion (args
[1]);
3050 if (args
[0] == error_mark_node
|| args
[1] == error_mark_node
)
3051 return error_mark_node
;
3053 type0
= TREE_TYPE (args
[0]);
3054 type1
= TREE_TYPE (args
[1]);
3056 if (type_unknown_p (args
[0]))
3058 args
[0] = instantiate_type (type1
, args
[0], 1);
3059 args
[0] = decay_conversion (args
[0]);
3061 else if (type_unknown_p (args
[1]))
3063 args
[1] = require_instantiated_type (type0
, args
[1],
3065 args
[1] = decay_conversion (args
[1]);
3068 if (IS_AGGR_TYPE (type0
) || IS_AGGR_TYPE (type1
))
3070 /* Try to convert this to something reasonable. */
3071 if (! build_default_binary_type_conversion(code
, &args
[0], &args
[1]))
3073 cp_error ("no match for `%O(%#T, %#T)'", code
,
3074 TREE_TYPE (arg1
), TREE_TYPE (arg2
));
3075 return error_mark_node
;
3079 return build_binary_op_nodefault (code
, args
[0], args
[1], code
);
3082 /* Build a binary-operation expression without default conversions.
3083 CODE is the kind of expression to build.
3084 This function differs from `build' in several ways:
3085 the data type of the result is computed and recorded in it,
3086 warnings are generated if arg data types are invalid,
3087 special handling for addition and subtraction of pointers is known,
3088 and some optimization is done (operations on narrow ints
3089 are done in the narrower type when that gives the same result).
3090 Constant folding is also done before the result is returned.
3092 ERROR_CODE is the code that determines what to say in error messages.
3093 It is usually, but not always, the same as CODE.
3095 Note that the operands will never have enumeral types
3096 because either they have just had the default conversions performed
3097 or they have both just been converted to some other type in which
3098 the arithmetic is to be done.
3100 C++: must do special pointer arithmetic when implementing
3101 multiple inheritance, and deal with pointer to member functions. */
3104 build_binary_op_nodefault (code
, orig_op0
, orig_op1
, error_code
)
3105 enum tree_code code
;
3106 tree orig_op0
, orig_op1
;
3107 enum tree_code error_code
;
3110 register enum tree_code code0
, code1
;
3113 /* Expression code to give to the expression when it is built.
3114 Normally this is CODE, which is what the caller asked for,
3115 but in some special cases we change it. */
3116 register enum tree_code resultcode
= code
;
3118 /* Data type in which the computation is to be performed.
3119 In the simplest cases this is the common type of the arguments. */
3120 register tree result_type
= NULL
;
3122 /* Nonzero means operands have already been type-converted
3123 in whatever way is necessary.
3124 Zero means they need to be converted to RESULT_TYPE. */
3127 /* Nonzero means create the expression with this type, rather than
3129 tree build_type
= 0;
3131 /* Nonzero means after finally constructing the expression
3132 convert it to this type. */
3133 tree final_type
= 0;
3135 /* Nonzero if this is an operation like MIN or MAX which can
3136 safely be computed in short if both args are promoted shorts.
3137 Also implies COMMON.
3138 -1 indicates a bitwise operation; this makes a difference
3139 in the exact conditions for when it is safe to do the operation
3140 in a narrower mode. */
3143 /* Nonzero if this is a comparison operation;
3144 if both args are promoted shorts, compare the original shorts.
3145 Also implies COMMON. */
3146 int short_compare
= 0;
3148 /* Nonzero if this is a right-shift operation, which can be computed on the
3149 original short and then promoted if the operand is a promoted short. */
3150 int short_shift
= 0;
3152 /* Nonzero means set RESULT_TYPE to the common type of the args. */
3155 /* Apply default conversions. */
3156 if (code
== TRUTH_AND_EXPR
|| code
== TRUTH_ANDIF_EXPR
3157 || code
== TRUTH_OR_EXPR
|| code
== TRUTH_ORIF_EXPR
3158 || code
== TRUTH_XOR_EXPR
)
3160 op0
= decay_conversion (orig_op0
);
3161 op1
= decay_conversion (orig_op1
);
3165 op0
= default_conversion (orig_op0
);
3166 op1
= default_conversion (orig_op1
);
3169 type0
= TREE_TYPE (op0
);
3170 type1
= TREE_TYPE (op1
);
3172 /* The expression codes of the data types of the arguments tell us
3173 whether the arguments are integers, floating, pointers, etc. */
3174 code0
= TREE_CODE (type0
);
3175 code1
= TREE_CODE (type1
);
3177 /* Strip NON_LVALUE_EXPRs, etc., since we aren't using as an lvalue. */
3178 STRIP_TYPE_NOPS (op0
);
3179 STRIP_TYPE_NOPS (op1
);
3181 /* If an error was already reported for one of the arguments,
3182 avoid reporting another error. */
3184 if (code0
== ERROR_MARK
|| code1
== ERROR_MARK
)
3185 return error_mark_node
;
3190 /* Handle the pointer + int case. */
3191 if (code0
== POINTER_TYPE
&& code1
== INTEGER_TYPE
)
3192 return pointer_int_sum (PLUS_EXPR
, op0
, op1
);
3193 else if (code1
== POINTER_TYPE
&& code0
== INTEGER_TYPE
)
3194 return pointer_int_sum (PLUS_EXPR
, op1
, op0
);
3200 /* Subtraction of two similar pointers.
3201 We must subtract them as integers, then divide by object size. */
3202 if (code0
== POINTER_TYPE
&& code1
== POINTER_TYPE
3203 && comp_target_types (type0
, type1
, 1))
3204 return pointer_diff (op0
, op1
, common_type (type0
, type1
));
3205 /* Handle pointer minus int. Just like pointer plus int. */
3206 else if (code0
== POINTER_TYPE
&& code1
== INTEGER_TYPE
)
3207 return pointer_int_sum (MINUS_EXPR
, op0
, op1
);
3216 case TRUNC_DIV_EXPR
:
3218 case FLOOR_DIV_EXPR
:
3219 case ROUND_DIV_EXPR
:
3220 case EXACT_DIV_EXPR
:
3221 if ((code0
== INTEGER_TYPE
|| code0
== REAL_TYPE
3222 || code0
== COMPLEX_TYPE
)
3223 && (code1
== INTEGER_TYPE
|| code1
== REAL_TYPE
3224 || code1
== COMPLEX_TYPE
))
3226 if (TREE_CODE (op1
) == INTEGER_CST
&& integer_zerop (op1
))
3227 cp_warning ("division by zero in `%E / 0'", op0
);
3228 else if (TREE_CODE (op1
) == REAL_CST
&& real_zerop (op1
))
3229 cp_warning ("division by zero in `%E / 0.'", op0
);
3231 if (!(code0
== INTEGER_TYPE
&& code1
== INTEGER_TYPE
))
3232 resultcode
= RDIV_EXPR
;
3234 /* When dividing two signed integers, we have to promote to int.
3235 unless we divide by a constant != -1. Note that default
3236 conversion will have been performed on the operands at this
3237 point, so we have to dig out the original type to find out if
3239 shorten
= ((TREE_CODE (op0
) == NOP_EXPR
3240 && TREE_UNSIGNED (TREE_TYPE (TREE_OPERAND (op0
, 0))))
3241 || (TREE_CODE (op1
) == INTEGER_CST
3242 && (TREE_INT_CST_LOW (op1
) != -1
3243 || TREE_INT_CST_HIGH (op1
) != -1)));
3249 case BIT_ANDTC_EXPR
:
3252 if (code0
== INTEGER_TYPE
&& code1
== INTEGER_TYPE
)
3254 /* If one operand is a constant, and the other is a short type
3255 that has been converted to an int,
3256 really do the work in the short type and then convert the
3257 result to int. If we are lucky, the constant will be 0 or 1
3258 in the short type, making the entire operation go away. */
3259 if (TREE_CODE (op0
) == INTEGER_CST
3260 && TREE_CODE (op1
) == NOP_EXPR
3261 && TYPE_PRECISION (type1
) > TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (op1
, 0)))
3262 && TREE_UNSIGNED (TREE_TYPE (TREE_OPERAND (op1
, 0))))
3264 final_type
= result_type
;
3265 op1
= TREE_OPERAND (op1
, 0);
3266 result_type
= TREE_TYPE (op1
);
3268 if (TREE_CODE (op1
) == INTEGER_CST
3269 && TREE_CODE (op0
) == NOP_EXPR
3270 && TYPE_PRECISION (type0
) > TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (op0
, 0)))
3271 && TREE_UNSIGNED (TREE_TYPE (TREE_OPERAND (op0
, 0))))
3273 final_type
= result_type
;
3274 op0
= TREE_OPERAND (op0
, 0);
3275 result_type
= TREE_TYPE (op0
);
3279 case TRUNC_MOD_EXPR
:
3280 case FLOOR_MOD_EXPR
:
3281 if (code1
== INTEGER_TYPE
&& integer_zerop (op1
))
3282 cp_warning ("division by zero in `%E % 0'", op0
);
3283 else if (code1
== REAL_TYPE
&& real_zerop (op1
))
3284 cp_warning ("division by zero in `%E % 0.'", op0
);
3286 if (code0
== INTEGER_TYPE
&& code1
== INTEGER_TYPE
)
3288 /* Although it would be tempting to shorten always here, that loses
3289 on some targets, since the modulo instruction is undefined if the
3290 quotient can't be represented in the computation mode. We shorten
3291 only if unsigned or if dividing by something we know != -1. */
3292 shorten
= ((TREE_CODE (op0
) == NOP_EXPR
3293 && TREE_UNSIGNED (TREE_TYPE (TREE_OPERAND (op0
, 0))))
3294 || (TREE_CODE (op1
) == INTEGER_CST
3295 && (TREE_INT_CST_LOW (op1
) != -1
3296 || TREE_INT_CST_HIGH (op1
) != -1)));
3301 case TRUTH_ANDIF_EXPR
:
3302 case TRUTH_ORIF_EXPR
:
3303 case TRUTH_AND_EXPR
:
3305 result_type
= boolean_type_node
;
3308 /* Shift operations: result has same type as first operand;
3309 always convert second operand to int.
3310 Also set SHORT_SHIFT if shifting rightward. */
3313 if (code0
== INTEGER_TYPE
&& code1
== INTEGER_TYPE
)
3315 result_type
= type0
;
3316 if (TREE_CODE (op1
) == INTEGER_CST
)
3318 if (tree_int_cst_lt (op1
, integer_zero_node
))
3319 warning ("right shift count is negative");
3322 if (TREE_INT_CST_LOW (op1
) | TREE_INT_CST_HIGH (op1
))
3324 if (TREE_INT_CST_HIGH (op1
) != 0
3325 || ((unsigned HOST_WIDE_INT
) TREE_INT_CST_LOW (op1
)
3326 >= TYPE_PRECISION (type0
)))
3327 warning ("right shift count >= width of type");
3330 /* Convert the shift-count to an integer, regardless of
3331 size of value being shifted. */
3332 if (TYPE_MAIN_VARIANT (TREE_TYPE (op1
)) != integer_type_node
)
3333 op1
= cp_convert (integer_type_node
, op1
);
3334 /* Avoid converting op1 to result_type later. */
3340 if (code0
== INTEGER_TYPE
&& code1
== INTEGER_TYPE
)
3342 result_type
= type0
;
3343 if (TREE_CODE (op1
) == INTEGER_CST
)
3345 if (tree_int_cst_lt (op1
, integer_zero_node
))
3346 warning ("left shift count is negative");
3347 else if (TREE_INT_CST_HIGH (op1
) != 0
3348 || ((unsigned HOST_WIDE_INT
) TREE_INT_CST_LOW (op1
)
3349 >= TYPE_PRECISION (type0
)))
3350 warning ("left shift count >= width of type");
3352 /* Convert the shift-count to an integer, regardless of
3353 size of value being shifted. */
3354 if (TYPE_MAIN_VARIANT (TREE_TYPE (op1
)) != integer_type_node
)
3355 op1
= cp_convert (integer_type_node
, op1
);
3356 /* Avoid converting op1 to result_type later. */
3363 if (code0
== INTEGER_TYPE
&& code1
== INTEGER_TYPE
)
3365 result_type
= type0
;
3366 if (TREE_CODE (op1
) == INTEGER_CST
)
3368 if (tree_int_cst_lt (op1
, integer_zero_node
))
3369 warning ("%s rotate count is negative",
3370 (code
== LROTATE_EXPR
) ? "left" : "right");
3371 else if (TREE_INT_CST_HIGH (op1
) != 0
3372 || ((unsigned HOST_WIDE_INT
) TREE_INT_CST_LOW (op1
)
3373 >= TYPE_PRECISION (type0
)))
3374 warning ("%s rotate count >= width of type",
3375 (code
== LROTATE_EXPR
) ? "left" : "right");
3377 /* Convert the shift-count to an integer, regardless of
3378 size of value being shifted. */
3379 if (TYPE_MAIN_VARIANT (TREE_TYPE (op1
)) != integer_type_node
)
3380 op1
= cp_convert (integer_type_node
, op1
);
3386 build_type
= boolean_type_node
;
3387 if ((code0
== INTEGER_TYPE
|| code0
== REAL_TYPE
3388 || code0
== COMPLEX_TYPE
)
3389 && (code1
== INTEGER_TYPE
|| code1
== REAL_TYPE
3390 || code1
== COMPLEX_TYPE
))
3392 else if (code0
== POINTER_TYPE
&& code1
== POINTER_TYPE
)
3394 register tree tt0
= TYPE_MAIN_VARIANT (TREE_TYPE (type0
));
3395 register tree tt1
= TYPE_MAIN_VARIANT (TREE_TYPE (type1
));
3397 if (comp_target_types (type0
, type1
, 1))
3398 result_type
= common_type (type0
, type1
);
3399 else if (tt0
== void_type_node
)
3401 if (pedantic
&& TREE_CODE (tt1
) == FUNCTION_TYPE
3402 && tree_int_cst_lt (TYPE_SIZE (type0
), TYPE_SIZE (type1
)))
3403 pedwarn ("ANSI C++ forbids comparison of `void *' with function pointer");
3404 else if (TREE_CODE (tt1
) == OFFSET_TYPE
)
3405 pedwarn ("ANSI C++ forbids conversion of a pointer to member to `void *'");
3407 else if (tt1
== void_type_node
)
3409 if (pedantic
&& TREE_CODE (tt0
) == FUNCTION_TYPE
3410 && tree_int_cst_lt (TYPE_SIZE (type1
), TYPE_SIZE (type0
)))
3411 pedwarn ("ANSI C++ forbids comparison of `void *' with function pointer");
3414 cp_pedwarn ("comparison of distinct pointer types `%T' and `%T' lacks a cast",
3417 if (result_type
== NULL_TREE
)
3418 result_type
= ptr_type_node
;
3420 else if (code0
== POINTER_TYPE
&& TREE_CODE (op1
) == INTEGER_CST
3421 && integer_zerop (op1
))
3422 result_type
= type0
;
3423 else if (code1
== POINTER_TYPE
&& TREE_CODE (op0
) == INTEGER_CST
3424 && integer_zerop (op0
))
3425 result_type
= type1
;
3426 else if (code0
== POINTER_TYPE
&& code1
== INTEGER_TYPE
)
3428 result_type
= type0
;
3429 error ("ANSI C++ forbids comparison between pointer and integer");
3431 else if (code0
== INTEGER_TYPE
&& code1
== POINTER_TYPE
)
3433 result_type
= type1
;
3434 error ("ANSI C++ forbids comparison between pointer and integer");
3436 else if (TYPE_PTRMEMFUNC_P (type0
) && TREE_CODE (op1
) == INTEGER_CST
3437 && integer_zerop (op1
))
3439 op0
= build_component_ref (op0
, index_identifier
, NULL_TREE
, 0);
3440 op1
= integer_zero_node
;
3441 result_type
= TREE_TYPE (op0
);
3443 else if (TYPE_PTRMEMFUNC_P (type1
) && TREE_CODE (op0
) == INTEGER_CST
3444 && integer_zerop (op0
))
3446 op0
= build_component_ref (op1
, index_identifier
, NULL_TREE
, 0);
3447 op1
= integer_zero_node
;
3448 result_type
= TREE_TYPE (op0
);
3450 else if (TYPE_PTRMEMFUNC_P (type0
) && TYPE_PTRMEMFUNC_P (type1
)
3451 && (TYPE_PTRMEMFUNC_FN_TYPE (type0
)
3452 == TYPE_PTRMEMFUNC_FN_TYPE (type1
)))
3454 /* The code we generate for the test is:
3456 (op0.index == op1.index
3457 && ((op1.index != -1 && op0.delta2 == op1.delta2)
3458 || op0.pfn == op1.pfn)) */
3460 tree index0
= build_component_ref (op0
, index_identifier
, NULL_TREE
, 0);
3461 tree index1
= save_expr (build_component_ref (op1
, index_identifier
, NULL_TREE
, 0));
3462 tree pfn0
= PFN_FROM_PTRMEMFUNC (op0
);
3463 tree pfn1
= PFN_FROM_PTRMEMFUNC (op1
);
3464 tree delta20
= DELTA2_FROM_PTRMEMFUNC (op0
);
3465 tree delta21
= DELTA2_FROM_PTRMEMFUNC (op1
);
3467 tree integer_neg_one_node
3468 = build_binary_op (MINUS_EXPR
, integer_zero_node
, integer_one_node
, 1);
3469 e1
= build_binary_op (EQ_EXPR
, index0
, index1
, 1);
3470 e2
= build_binary_op (NE_EXPR
, index1
, integer_neg_one_node
, 1);
3471 e2
= build_binary_op (TRUTH_ANDIF_EXPR
, e2
, build_binary_op (EQ_EXPR
, delta20
, delta21
, 1), 1);
3472 e3
= build_binary_op (EQ_EXPR
, pfn0
, pfn1
, 1);
3473 e2
= build_binary_op (TRUTH_ORIF_EXPR
, e2
, e3
, 1);
3474 e2
= build_binary_op (TRUTH_ANDIF_EXPR
, e1
, e2
, 1);
3475 if (code
== EQ_EXPR
)
3477 return build_binary_op (EQ_EXPR
, e2
, integer_zero_node
, 1);
3479 else if (TYPE_PTRMEMFUNC_P (type0
)
3480 && TYPE_PTRMEMFUNC_FN_TYPE (type0
) == type1
)
3482 tree index0
= build_component_ref (op0
, index_identifier
, NULL_TREE
, 0);
3484 tree pfn0
= PFN_FROM_PTRMEMFUNC (op0
);
3485 tree delta20
= DELTA2_FROM_PTRMEMFUNC (op0
);
3486 tree delta21
= integer_zero_node
;
3488 tree integer_neg_one_node
3489 = build_binary_op (MINUS_EXPR
, integer_zero_node
, integer_one_node
, 1);
3490 if (TREE_CODE (TREE_OPERAND (op1
, 0)) == FUNCTION_DECL
3491 && DECL_VINDEX (TREE_OPERAND (op1
, 0)))
3493 /* Map everything down one to make room for the null pointer to member. */
3494 index1
= size_binop (PLUS_EXPR
,
3495 DECL_VINDEX (TREE_OPERAND (op1
, 0)),
3497 op1
= integer_zero_node
;
3498 delta21
= CLASSTYPE_VFIELD (TYPE_METHOD_BASETYPE (TREE_TYPE (type1
)));
3499 delta21
= DECL_FIELD_BITPOS (delta21
);
3500 delta21
= size_binop (FLOOR_DIV_EXPR
, delta21
, size_int (BITS_PER_UNIT
));
3503 index1
= integer_neg_one_node
;
3505 tree nop1
= build1 (NOP_EXPR
, TYPE_PTRMEMFUNC_FN_TYPE (type0
), op1
);
3506 TREE_CONSTANT (nop1
) = TREE_CONSTANT (op1
);
3509 e1
= build_binary_op (EQ_EXPR
, index0
, index1
, 1);
3510 e2
= build_binary_op (NE_EXPR
, index1
, integer_neg_one_node
, 1);
3511 e2
= build_binary_op (TRUTH_ANDIF_EXPR
, e2
, build_binary_op (EQ_EXPR
, delta20
, delta21
, 1), 1);
3512 e3
= build_binary_op (EQ_EXPR
, pfn0
, op1
, 1);
3513 e2
= build_binary_op (TRUTH_ORIF_EXPR
, e2
, e3
, 1);
3514 e2
= build_binary_op (TRUTH_ANDIF_EXPR
, e1
, e2
, 1);
3515 if (code
== EQ_EXPR
)
3517 return build_binary_op (EQ_EXPR
, e2
, integer_zero_node
, 1);
3519 else if (TYPE_PTRMEMFUNC_P (type1
)
3520 && TYPE_PTRMEMFUNC_FN_TYPE (type1
) == type0
)
3522 return build_binary_op (code
, op1
, op0
, 1);
3528 if ((code0
== INTEGER_TYPE
|| code0
== REAL_TYPE
)
3529 && (code1
== INTEGER_TYPE
|| code1
== REAL_TYPE
))
3531 else if (code0
== POINTER_TYPE
&& code1
== POINTER_TYPE
)
3533 if (comp_target_types (type0
, type1
, 1))
3534 result_type
= common_type (type0
, type1
);
3537 cp_pedwarn ("comparison of distinct pointer types `%T' and `%T' lacks a cast",
3539 result_type
= ptr_type_node
;
3548 build_type
= boolean_type_node
;
3549 if ((code0
== INTEGER_TYPE
|| code0
== REAL_TYPE
)
3550 && (code1
== INTEGER_TYPE
|| code1
== REAL_TYPE
))
3552 else if (code0
== POINTER_TYPE
&& code1
== POINTER_TYPE
)
3554 if (comp_target_types (type0
, type1
, 1))
3555 result_type
= common_type (type0
, type1
);
3558 cp_pedwarn ("comparison of distinct pointer types `%T' and `%T' lacks a cast",
3560 result_type
= ptr_type_node
;
3563 else if (code0
== POINTER_TYPE
&& TREE_CODE (op1
) == INTEGER_CST
3564 && integer_zerop (op1
))
3565 result_type
= type0
;
3566 else if (code1
== POINTER_TYPE
&& TREE_CODE (op0
) == INTEGER_CST
3567 && integer_zerop (op0
))
3568 result_type
= type1
;
3569 else if (code0
== POINTER_TYPE
&& code1
== INTEGER_TYPE
)
3571 result_type
= type0
;
3572 pedwarn ("ANSI C++ forbids comparison between pointer and integer");
3574 else if (code0
== INTEGER_TYPE
&& code1
== POINTER_TYPE
)
3576 result_type
= type1
;
3577 pedwarn ("ANSI C++ forbids comparison between pointer and integer");
3585 if ((code0
== INTEGER_TYPE
|| code0
== REAL_TYPE
|| code0
== COMPLEX_TYPE
)
3587 (code1
== INTEGER_TYPE
|| code1
== REAL_TYPE
|| code1
== COMPLEX_TYPE
))
3589 int none_complex
= (code0
!= COMPLEX_TYPE
&& code1
!= COMPLEX_TYPE
);
3591 if (shorten
|| common
|| short_compare
)
3592 result_type
= common_type (type0
, type1
);
3594 /* For certain operations (which identify themselves by shorten != 0)
3595 if both args were extended from the same smaller type,
3596 do the arithmetic in that type and then extend.
3598 shorten !=0 and !=1 indicates a bitwise operation.
3599 For them, this optimization is safe only if
3600 both args are zero-extended or both are sign-extended.
3601 Otherwise, we might change the result.
3602 Eg, (short)-1 | (unsigned short)-1 is (int)-1
3603 but calculated in (unsigned short) it would be (unsigned short)-1. */
3605 if (shorten
&& none_complex
)
3607 int unsigned0
, unsigned1
;
3608 tree arg0
= get_narrower (op0
, &unsigned0
);
3609 tree arg1
= get_narrower (op1
, &unsigned1
);
3610 /* UNS is 1 if the operation to be done is an unsigned one. */
3611 int uns
= TREE_UNSIGNED (result_type
);
3614 final_type
= result_type
;
3616 /* Handle the case that OP0 does not *contain* a conversion
3617 but it *requires* conversion to FINAL_TYPE. */
3619 if (op0
== arg0
&& TREE_TYPE (op0
) != final_type
)
3620 unsigned0
= TREE_UNSIGNED (TREE_TYPE (op0
));
3621 if (op1
== arg1
&& TREE_TYPE (op1
) != final_type
)
3622 unsigned1
= TREE_UNSIGNED (TREE_TYPE (op1
));
3624 /* Now UNSIGNED0 is 1 if ARG0 zero-extends to FINAL_TYPE. */
3626 /* For bitwise operations, signedness of nominal type
3627 does not matter. Consider only how operands were extended. */
3631 /* Note that in all three cases below we refrain from optimizing
3632 an unsigned operation on sign-extended args.
3633 That would not be valid. */
3635 /* Both args variable: if both extended in same way
3636 from same width, do it in that width.
3637 Do it unsigned if args were zero-extended. */
3638 if ((TYPE_PRECISION (TREE_TYPE (arg0
))
3639 < TYPE_PRECISION (result_type
))
3640 && (TYPE_PRECISION (TREE_TYPE (arg1
))
3641 == TYPE_PRECISION (TREE_TYPE (arg0
)))
3642 && unsigned0
== unsigned1
3643 && (unsigned0
|| !uns
))
3645 = signed_or_unsigned_type (unsigned0
,
3646 common_type (TREE_TYPE (arg0
), TREE_TYPE (arg1
)));
3647 else if (TREE_CODE (arg0
) == INTEGER_CST
3648 && (unsigned1
|| !uns
)
3649 && (TYPE_PRECISION (TREE_TYPE (arg1
))
3650 < TYPE_PRECISION (result_type
))
3651 && (type
= signed_or_unsigned_type (unsigned1
,
3653 int_fits_type_p (arg0
, type
)))
3655 else if (TREE_CODE (arg1
) == INTEGER_CST
3656 && (unsigned0
|| !uns
)
3657 && (TYPE_PRECISION (TREE_TYPE (arg0
))
3658 < TYPE_PRECISION (result_type
))
3659 && (type
= signed_or_unsigned_type (unsigned0
,
3661 int_fits_type_p (arg1
, type
)))
3665 /* Shifts can be shortened if shifting right. */
3670 tree arg0
= get_narrower (op0
, &unsigned_arg
);
3672 final_type
= result_type
;
3674 if (arg0
== op0
&& final_type
== TREE_TYPE (op0
))
3675 unsigned_arg
= TREE_UNSIGNED (TREE_TYPE (op0
));
3677 if (TYPE_PRECISION (TREE_TYPE (arg0
)) < TYPE_PRECISION (result_type
)
3678 /* We can shorten only if the shift count is less than the
3679 number of bits in the smaller type size. */
3680 && TREE_INT_CST_HIGH (op1
) == 0
3681 && TYPE_PRECISION (TREE_TYPE (arg0
)) > TREE_INT_CST_LOW (op1
)
3682 /* If arg is sign-extended and then unsigned-shifted,
3683 we can simulate this with a signed shift in arg's type
3684 only if the extended result is at least twice as wide
3685 as the arg. Otherwise, the shift could use up all the
3686 ones made by sign-extension and bring in zeros.
3687 We can't optimize that case at all, but in most machines
3688 it never happens because available widths are 2**N. */
3689 && (!TREE_UNSIGNED (final_type
)
3691 || (((unsigned) 2 * TYPE_PRECISION (TREE_TYPE (arg0
)))
3692 <= TYPE_PRECISION (result_type
))))
3694 /* Do an unsigned shift if the operand was zero-extended. */
3696 = signed_or_unsigned_type (unsigned_arg
,
3698 /* Convert value-to-be-shifted to that type. */
3699 if (TREE_TYPE (op0
) != result_type
)
3700 op0
= cp_convert (result_type
, op0
);
3705 /* Comparison operations are shortened too but differently.
3706 They identify themselves by setting short_compare = 1. */
3710 /* Don't write &op0, etc., because that would prevent op0
3711 from being kept in a register.
3712 Instead, make copies of the our local variables and
3713 pass the copies by reference, then copy them back afterward. */
3714 tree xop0
= op0
, xop1
= op1
, xresult_type
= result_type
;
3715 enum tree_code xresultcode
= resultcode
;
3717 = shorten_compare (&xop0
, &xop1
, &xresult_type
, &xresultcode
);
3719 return cp_convert (boolean_type_node
, val
);
3720 op0
= xop0
, op1
= xop1
;
3722 resultcode
= xresultcode
;
3725 if (short_compare
&& warn_sign_compare
)
3727 int op0_signed
= ! TREE_UNSIGNED (TREE_TYPE (orig_op0
));
3728 int op1_signed
= ! TREE_UNSIGNED (TREE_TYPE (orig_op1
));
3730 int unsignedp0
, unsignedp1
;
3731 tree primop0
= get_narrower (op0
, &unsignedp0
);
3732 tree primop1
= get_narrower (op1
, &unsignedp1
);
3734 /* Check for comparison of different enum types. */
3735 if (flag_int_enum_equivalence
== 0
3736 && TREE_CODE (TREE_TYPE (orig_op0
)) == ENUMERAL_TYPE
3737 && TREE_CODE (TREE_TYPE (orig_op1
)) == ENUMERAL_TYPE
3738 && TYPE_MAIN_VARIANT (TREE_TYPE (orig_op0
))
3739 != TYPE_MAIN_VARIANT (TREE_TYPE (orig_op1
)))
3741 cp_warning ("comparison between `%#T' and `%#T'",
3742 TREE_TYPE (orig_op0
), TREE_TYPE (orig_op1
));
3745 /* Give warnings for comparisons between signed and unsigned
3746 quantities that may fail. */
3747 /* Do the checking based on the original operand trees, so that
3748 casts will be considered, but default promotions won't be. */
3750 /* Do not warn if the comparison is being done in a signed type,
3751 since the signed type will only be chosen if it can represent
3752 all the values of the unsigned type. */
3753 if (! TREE_UNSIGNED (result_type
))
3755 /* Do not warn if both operands are unsigned. */
3756 else if (op0_signed
== op1_signed
)
3758 /* Do not warn if the signed quantity is an unsuffixed
3759 integer literal (or some static constant expression
3760 involving such literals) and it is non-negative. */
3761 else if ((op0_signed
&& TREE_CODE (orig_op0
) == INTEGER_CST
3762 && tree_int_cst_sgn (orig_op0
) >= 0)
3763 || (op1_signed
&& TREE_CODE (orig_op1
) == INTEGER_CST
3764 && tree_int_cst_sgn (orig_op1
) >= 0))
3766 /* Do not warn if the comparison is an equality operation,
3767 the unsigned quantity is an integral constant and it does
3768 not use the most significant bit of result_type. */
3769 else if ((resultcode
== EQ_EXPR
|| resultcode
== NE_EXPR
)
3770 && ((op0_signed
&& TREE_CODE (orig_op1
) == INTEGER_CST
3771 && int_fits_type_p (orig_op1
, signed_type (result_type
))
3772 || (op1_signed
&& TREE_CODE (orig_op0
) == INTEGER_CST
3773 && int_fits_type_p (orig_op0
, signed_type (result_type
))))))
3776 warning ("comparison between signed and unsigned");
3778 /* Warn if two unsigned values are being compared in a size
3779 larger than their original size, and one (and only one) is the
3780 result of a `~' operator. This comparison will always fail.
3782 Also warn if one operand is a constant, and the constant does not
3783 have all bits set that are set in the ~ operand when it is
3786 if ((TREE_CODE (primop0
) == BIT_NOT_EXPR
)
3787 ^ (TREE_CODE (primop1
) == BIT_NOT_EXPR
))
3789 if (TREE_CODE (primop0
) == BIT_NOT_EXPR
)
3790 primop0
= get_narrower (TREE_OPERAND (op0
, 0), &unsignedp0
);
3791 if (TREE_CODE (primop1
) == BIT_NOT_EXPR
)
3792 primop1
= get_narrower (TREE_OPERAND (op1
, 0), &unsignedp1
);
3794 if (TREE_CODE (primop0
) == INTEGER_CST
3795 || TREE_CODE (primop1
) == INTEGER_CST
)
3798 HOST_WIDE_INT constant
, mask
;
3802 if (TREE_CODE (primop0
) == INTEGER_CST
)
3805 unsignedp
= unsignedp1
;
3806 constant
= TREE_INT_CST_LOW (primop0
);
3811 unsignedp
= unsignedp0
;
3812 constant
= TREE_INT_CST_LOW (primop1
);
3815 bits
= TYPE_PRECISION (TREE_TYPE (primop
));
3816 if (bits
< TYPE_PRECISION (result_type
)
3817 && bits
< HOST_BITS_PER_LONG
&& unsignedp
)
3819 mask
= (~ (HOST_WIDE_INT
) 0) << bits
;
3820 if ((mask
& constant
) != mask
)
3821 warning ("comparison of promoted ~unsigned with constant");
3824 else if (unsignedp0
&& unsignedp1
3825 && (TYPE_PRECISION (TREE_TYPE (primop0
))
3826 < TYPE_PRECISION (result_type
))
3827 && (TYPE_PRECISION (TREE_TYPE (primop1
))
3828 < TYPE_PRECISION (result_type
)))
3829 warning ("comparison of promoted ~unsigned with unsigned");
3834 /* At this point, RESULT_TYPE must be nonzero to avoid an error message.
3835 If CONVERTED is zero, both args will be converted to type RESULT_TYPE.
3836 Then the expression will be built.
3837 It will be given type FINAL_TYPE if that is nonzero;
3838 otherwise, it will be given type RESULT_TYPE. */
3842 cp_error ("invalid operands `%T' and `%T' to binary `%O'",
3843 TREE_TYPE (orig_op0
), TREE_TYPE (orig_op1
), error_code
);
3844 return error_mark_node
;
3849 if (TREE_TYPE (op0
) != result_type
)
3850 op0
= cp_convert (result_type
, op0
);
3851 if (TREE_TYPE (op1
) != result_type
)
3852 op1
= cp_convert (result_type
, op1
);
3855 if (build_type
== NULL_TREE
)
3856 build_type
= result_type
;
3859 register tree result
= build (resultcode
, build_type
, op0
, op1
);
3860 register tree folded
;
3862 folded
= fold (result
);
3863 if (folded
== result
)
3864 TREE_CONSTANT (folded
) = TREE_CONSTANT (op0
) & TREE_CONSTANT (op1
);
3865 if (final_type
!= 0)
3866 return cp_convert (final_type
, folded
);
3871 /* Return a tree for the sum or difference (RESULTCODE says which)
3872 of pointer PTROP and integer INTOP. */
3875 pointer_int_sum (resultcode
, ptrop
, intop
)
3876 enum tree_code resultcode
;
3877 register tree ptrop
, intop
;
3881 register tree result
;
3882 register tree folded
= fold (intop
);
3884 /* The result is a pointer of the same type that is being added. */
3886 register tree result_type
= TREE_TYPE (ptrop
);
3888 if (TREE_CODE (TREE_TYPE (result_type
)) == VOID_TYPE
)
3890 if (pedantic
|| warn_pointer_arith
)
3891 pedwarn ("ANSI C++ forbids using pointer of type `void *' in arithmetic");
3892 size_exp
= integer_one_node
;
3894 else if (TREE_CODE (TREE_TYPE (result_type
)) == FUNCTION_TYPE
)
3896 if (pedantic
|| warn_pointer_arith
)
3897 pedwarn ("ANSI C++ forbids using pointer to a function in arithmetic");
3898 size_exp
= integer_one_node
;
3900 else if (TREE_CODE (TREE_TYPE (result_type
)) == METHOD_TYPE
)
3902 if (pedantic
|| warn_pointer_arith
)
3903 pedwarn ("ANSI C++ forbids using pointer to a method in arithmetic");
3904 size_exp
= integer_one_node
;
3906 else if (TREE_CODE (TREE_TYPE (result_type
)) == OFFSET_TYPE
)
3908 if (pedantic
|| warn_pointer_arith
)
3909 pedwarn ("ANSI C++ forbids using pointer to a member in arithmetic");
3910 size_exp
= integer_one_node
;
3913 size_exp
= size_in_bytes (complete_type (TREE_TYPE (result_type
)));
3915 /* Needed to make OOPS V2R3 work. */
3917 if (TREE_CODE (intop
) == INTEGER_CST
3918 && TREE_INT_CST_LOW (intop
) == 0
3919 && TREE_INT_CST_HIGH (intop
) == 0)
3922 /* If what we are about to multiply by the size of the elements
3923 contains a constant term, apply distributive law
3924 and multiply that constant term separately.
3925 This helps produce common subexpressions. */
3927 if ((TREE_CODE (intop
) == PLUS_EXPR
|| TREE_CODE (intop
) == MINUS_EXPR
)
3928 && ! TREE_CONSTANT (intop
)
3929 && TREE_CONSTANT (TREE_OPERAND (intop
, 1))
3930 && TREE_CONSTANT (size_exp
))
3932 enum tree_code subcode
= resultcode
;
3933 if (TREE_CODE (intop
) == MINUS_EXPR
)
3934 subcode
= (subcode
== PLUS_EXPR
? MINUS_EXPR
: PLUS_EXPR
);
3935 ptrop
= build_binary_op (subcode
, ptrop
, TREE_OPERAND (intop
, 1), 1);
3936 intop
= TREE_OPERAND (intop
, 0);
3939 /* Convert the integer argument to a type the same size as sizetype
3940 so the multiply won't overflow spuriously. */
3942 if (TYPE_PRECISION (TREE_TYPE (intop
)) != TYPE_PRECISION (sizetype
))
3943 intop
= cp_convert (type_for_size (TYPE_PRECISION (sizetype
), 0), intop
);
3945 /* Replace the integer argument with a suitable product by the object size.
3946 Do this multiplication as signed, then convert to the appropriate
3947 pointer type (actually unsigned integral). */
3949 intop
= cp_convert (result_type
,
3950 build_binary_op (MULT_EXPR
, intop
,
3951 cp_convert (TREE_TYPE (intop
), size_exp
), 1));
3953 /* Create the sum or difference. */
3955 result
= build (resultcode
, result_type
, ptrop
, intop
);
3957 folded
= fold (result
);
3958 if (folded
== result
)
3959 TREE_CONSTANT (folded
) = TREE_CONSTANT (ptrop
) & TREE_CONSTANT (intop
);
3963 /* Return a tree for the difference of pointers OP0 and OP1.
3964 The resulting tree has type int. */
3967 pointer_diff (op0
, op1
, ptrtype
)
3968 register tree op0
, op1
;
3969 register tree ptrtype
;
3971 register tree result
, folded
;
3972 tree restype
= ptrdiff_type_node
;
3973 tree target_type
= TREE_TYPE (ptrtype
);
3975 if (pedantic
|| warn_pointer_arith
)
3977 if (TREE_CODE (target_type
) == VOID_TYPE
)
3978 pedwarn ("ANSI C++ forbids using pointer of type `void *' in subtraction");
3979 if (TREE_CODE (target_type
) == FUNCTION_TYPE
)
3980 pedwarn ("ANSI C++ forbids using pointer to a function in subtraction");
3981 if (TREE_CODE (target_type
) == METHOD_TYPE
)
3982 pedwarn ("ANSI C++ forbids using pointer to a method in subtraction");
3983 if (TREE_CODE (target_type
) == OFFSET_TYPE
)
3984 pedwarn ("ANSI C++ forbids using pointer to a member in subtraction");
3987 /* First do the subtraction as integers;
3988 then drop through to build the divide operator. */
3990 op0
= build_binary_op (MINUS_EXPR
,
3991 cp_convert (restype
, op0
), cp_convert (restype
, op1
), 1);
3993 /* This generates an error if op1 is a pointer to an incomplete type. */
3994 if (TYPE_SIZE (TREE_TYPE (TREE_TYPE (op1
))) == 0)
3995 error ("arithmetic on pointer to an incomplete type");
3997 op1
= ((TREE_CODE (target_type
) == VOID_TYPE
3998 || TREE_CODE (target_type
) == FUNCTION_TYPE
3999 || TREE_CODE (target_type
) == METHOD_TYPE
4000 || TREE_CODE (target_type
) == OFFSET_TYPE
)
4002 : size_in_bytes (target_type
));
4004 /* Do the division. */
4006 result
= build (EXACT_DIV_EXPR
, restype
, op0
, cp_convert (restype
, op1
));
4008 folded
= fold (result
);
4009 if (folded
== result
)
4010 TREE_CONSTANT (folded
) = TREE_CONSTANT (op0
) & TREE_CONSTANT (op1
);
4014 /* Handle the case of taking the address of a COMPONENT_REF.
4015 Called by `build_unary_op' and `build_up_reference'.
4017 ARG is the COMPONENT_REF whose address we want.
4018 ARGTYPE is the pointer type that this address should have.
4019 MSG is an error message to print if this COMPONENT_REF is not
4020 addressable (such as a bitfield). */
4023 build_component_addr (arg
, argtype
, msg
)
4027 tree field
= TREE_OPERAND (arg
, 1);
4028 tree basetype
= decl_type_context (field
);
4029 tree rval
= build_unary_op (ADDR_EXPR
, TREE_OPERAND (arg
, 0), 0);
4031 if (DECL_BIT_FIELD (field
))
4033 error (msg
, IDENTIFIER_POINTER (DECL_NAME (field
)));
4034 return error_mark_node
;
4037 if (TREE_CODE (field
) == FIELD_DECL
4038 && TYPE_USES_COMPLEX_INHERITANCE (basetype
))
4040 /* Can't convert directly to ARGTYPE, since that
4041 may have the same pointer type as one of our
4043 rval
= build1 (NOP_EXPR
, argtype
,
4044 convert_pointer_to (basetype
, rval
));
4045 TREE_CONSTANT (rval
) = TREE_CONSTANT (TREE_OPERAND (rval
, 0));
4048 /* This conversion is harmless. */
4049 rval
= convert_force (argtype
, rval
, 0);
4051 if (! integer_zerop (DECL_FIELD_BITPOS (field
)))
4053 tree offset
= size_binop (EASY_DIV_EXPR
, DECL_FIELD_BITPOS (field
),
4054 size_int (BITS_PER_UNIT
));
4055 int flag
= TREE_CONSTANT (rval
);
4056 rval
= fold (build (PLUS_EXPR
, argtype
,
4057 rval
, cp_convert (argtype
, offset
)));
4058 TREE_CONSTANT (rval
) = flag
;
4063 /* Construct and perhaps optimize a tree representation
4064 for a unary operation. CODE, a tree_code, specifies the operation
4065 and XARG is the operand. */
4068 build_x_unary_op (code
, xarg
)
4069 enum tree_code code
;
4072 if (processing_template_decl
)
4073 return build_min_nt (code
, xarg
, NULL_TREE
);
4075 /* & rec, on incomplete RECORD_TYPEs is the simple opr &, not an
4077 if (code
== ADDR_EXPR
4078 && TREE_CODE (xarg
) != TEMPLATE_ID_EXPR
4079 && ((IS_AGGR_TYPE_CODE (TREE_CODE (TREE_TYPE (xarg
)))
4080 && TYPE_SIZE (TREE_TYPE (xarg
)) == NULL_TREE
)
4081 || (TREE_CODE (xarg
) == OFFSET_REF
)))
4082 /* don't look for a function */;
4087 if (flag_ansi_overloading
)
4089 rval
= build_new_op (code
, LOOKUP_NORMAL
, xarg
,
4090 NULL_TREE
, NULL_TREE
);
4091 if (rval
|| code
!= ADDR_EXPR
)
4096 rval
= build_opfncall (code
, LOOKUP_SPECULATIVELY
, xarg
,
4097 NULL_TREE
, NULL_TREE
);
4099 return build_opfncall (code
, LOOKUP_NORMAL
, xarg
,
4100 NULL_TREE
, NULL_TREE
);
4104 if (code
== ADDR_EXPR
)
4106 if (TREE_CODE (xarg
) == TARGET_EXPR
)
4107 warning ("taking address of temporary");
4110 return build_unary_op (code
, xarg
, 0);
4113 /* Just like truthvalue_conversion, but we want a CLEANUP_POINT_EXPR. */
4116 condition_conversion (expr
)
4120 if (processing_template_decl
)
4122 t
= cp_convert (boolean_type_node
, expr
);
4123 t
= fold (build1 (CLEANUP_POINT_EXPR
, boolean_type_node
, t
));
4127 /* C++: Must handle pointers to members.
4129 Perhaps type instantiation should be extended to handle conversion
4130 from aggregates to types we don't yet know we want? (Or are those
4131 cases typically errors which should be reported?)
4133 NOCONVERT nonzero suppresses the default promotions
4134 (such as from short to int). */
4137 build_unary_op (code
, xarg
, noconvert
)
4138 enum tree_code code
;
4142 /* No default_conversion here. It causes trouble for ADDR_EXPR. */
4143 register tree arg
= xarg
;
4144 register tree argtype
= 0;
4145 char *errstring
= NULL
;
4148 if (arg
== error_mark_node
)
4149 return error_mark_node
;
4154 /* This is used for unary plus, because a CONVERT_EXPR
4155 is enough to prevent anybody from looking inside for
4156 associativity, but won't generate any code. */
4157 if (!(arg
= build_expr_type_conversion
4158 (WANT_ARITH
| WANT_ENUM
| WANT_POINTER
, arg
, 1)))
4159 errstring
= "wrong type argument to unary plus";
4163 arg
= default_conversion (arg
);
4164 arg
= build1 (NON_LVALUE_EXPR
, TREE_TYPE (arg
), arg
);
4169 if (!(arg
= build_expr_type_conversion (WANT_ARITH
| WANT_ENUM
, arg
, 1)))
4170 errstring
= "wrong type argument to unary minus";
4171 else if (!noconvert
)
4172 arg
= default_conversion (arg
);
4176 if (TREE_CODE (TREE_TYPE (arg
)) == COMPLEX_TYPE
)
4180 arg
= default_conversion (arg
);
4182 else if (!(arg
= build_expr_type_conversion (WANT_INT
| WANT_ENUM
,
4184 errstring
= "wrong type argument to bit-complement";
4185 else if (!noconvert
)
4186 arg
= default_conversion (arg
);
4190 if (!(arg
= build_expr_type_conversion (WANT_ARITH
| WANT_ENUM
, arg
, 1)))
4191 errstring
= "wrong type argument to abs";
4192 else if (!noconvert
)
4193 arg
= default_conversion (arg
);
4197 /* Conjugating a real value is a no-op, but allow it anyway. */
4198 if (!(arg
= build_expr_type_conversion (WANT_ARITH
| WANT_ENUM
, arg
, 1)))
4199 errstring
= "wrong type argument to conjugation";
4200 else if (!noconvert
)
4201 arg
= default_conversion (arg
);
4204 case TRUTH_NOT_EXPR
:
4205 arg
= cp_convert (boolean_type_node
, arg
);
4206 val
= invert_truthvalue (arg
);
4207 if (arg
!= error_mark_node
)
4209 errstring
= "in argument to unary !";
4216 if (TREE_CODE (arg
) == COMPLEX_CST
)
4217 return TREE_REALPART (arg
);
4218 else if (TREE_CODE (TREE_TYPE (arg
)) == COMPLEX_TYPE
)
4219 return fold (build1 (REALPART_EXPR
, TREE_TYPE (TREE_TYPE (arg
)), arg
));
4224 if (TREE_CODE (arg
) == COMPLEX_CST
)
4225 return TREE_IMAGPART (arg
);
4226 else if (TREE_CODE (TREE_TYPE (arg
)) == COMPLEX_TYPE
)
4227 return fold (build1 (IMAGPART_EXPR
, TREE_TYPE (TREE_TYPE (arg
)), arg
));
4229 return cp_convert (TREE_TYPE (arg
), integer_zero_node
);
4231 case PREINCREMENT_EXPR
:
4232 case POSTINCREMENT_EXPR
:
4233 case PREDECREMENT_EXPR
:
4234 case POSTDECREMENT_EXPR
:
4235 /* Handle complex lvalues (when permitted)
4236 by reduction to simpler cases. */
4238 val
= unary_complex_lvalue (code
, arg
);
4242 /* Increment or decrement the real part of the value,
4243 and don't change the imaginary part. */
4244 if (TREE_CODE (TREE_TYPE (arg
)) == COMPLEX_TYPE
)
4248 arg
= stabilize_reference (arg
);
4249 real
= build_unary_op (REALPART_EXPR
, arg
, 1);
4250 imag
= build_unary_op (IMAGPART_EXPR
, arg
, 1);
4251 return build (COMPLEX_EXPR
, TREE_TYPE (arg
),
4252 build_unary_op (code
, real
, 1), imag
);
4255 /* Report invalid types. */
4257 if (!(arg
= build_expr_type_conversion (WANT_ARITH
| WANT_POINTER
,
4260 if (code
== PREINCREMENT_EXPR
)
4261 errstring
="no pre-increment operator for type";
4262 else if (code
== POSTINCREMENT_EXPR
)
4263 errstring
="no post-increment operator for type";
4264 else if (code
== PREDECREMENT_EXPR
)
4265 errstring
="no pre-decrement operator for type";
4267 errstring
="no post-decrement operator for type";
4271 /* Report something read-only. */
4273 if (TYPE_READONLY (TREE_TYPE (arg
))
4274 || TREE_READONLY (arg
))
4275 readonly_error (arg
, ((code
== PREINCREMENT_EXPR
4276 || code
== POSTINCREMENT_EXPR
)
4277 ? "increment" : "decrement"),
4282 tree result_type
= TREE_TYPE (arg
);
4284 arg
= get_unwidened (arg
, 0);
4285 argtype
= TREE_TYPE (arg
);
4287 /* ARM $5.2.5 last annotation says this should be forbidden. */
4288 if (TREE_CODE (argtype
) == ENUMERAL_TYPE
)
4289 pedwarn ("ANSI C++ forbids %sing an enum",
4290 (code
== PREINCREMENT_EXPR
|| code
== POSTINCREMENT_EXPR
)
4291 ? "increment" : "decrement");
4293 /* Compute the increment. */
4295 if (TREE_CODE (argtype
) == POINTER_TYPE
)
4297 enum tree_code tmp
= TREE_CODE (TREE_TYPE (argtype
));
4298 if (TYPE_SIZE (complete_type (TREE_TYPE (argtype
))) == 0)
4299 cp_error ("cannot %s a pointer to incomplete type `%T'",
4300 ((code
== PREINCREMENT_EXPR
4301 || code
== POSTINCREMENT_EXPR
)
4302 ? "increment" : "decrement"), TREE_TYPE (argtype
));
4303 else if (tmp
== FUNCTION_TYPE
|| tmp
== METHOD_TYPE
4304 || tmp
== VOID_TYPE
|| tmp
== OFFSET_TYPE
)
4305 cp_pedwarn ("ANSI C++ forbids %sing a pointer of type `%T'",
4306 ((code
== PREINCREMENT_EXPR
4307 || code
== POSTINCREMENT_EXPR
)
4308 ? "increment" : "decrement"), argtype
);
4309 inc
= c_sizeof_nowarn (TREE_TYPE (argtype
));
4312 inc
= integer_one_node
;
4314 inc
= cp_convert (argtype
, inc
);
4316 /* Handle incrementing a cast-expression. */
4318 switch (TREE_CODE (arg
))
4323 case FIX_TRUNC_EXPR
:
4324 case FIX_FLOOR_EXPR
:
4325 case FIX_ROUND_EXPR
:
4328 tree incremented
, modify
, value
, compound
;
4329 if (! lvalue_p (arg
) && pedantic
)
4330 pedwarn ("cast to non-reference type used as lvalue");
4331 arg
= stabilize_reference (arg
);
4332 if (code
== PREINCREMENT_EXPR
|| code
== PREDECREMENT_EXPR
)
4335 value
= save_expr (arg
);
4336 incremented
= build (((code
== PREINCREMENT_EXPR
4337 || code
== POSTINCREMENT_EXPR
)
4338 ? PLUS_EXPR
: MINUS_EXPR
),
4339 argtype
, value
, inc
);
4340 TREE_SIDE_EFFECTS (incremented
) = 1;
4342 modify
= build_modify_expr (arg
, NOP_EXPR
, incremented
);
4343 compound
= build (COMPOUND_EXPR
, TREE_TYPE (arg
), modify
, value
);
4345 /* Eliminate warning about unused result of + or -. */
4346 TREE_NO_UNUSED_WARNING (compound
) = 1;
4354 /* Complain about anything else that is not a true lvalue. */
4355 if (!lvalue_or_else (arg
, ((code
== PREINCREMENT_EXPR
4356 || code
== POSTINCREMENT_EXPR
)
4357 ? "increment" : "decrement")))
4358 return error_mark_node
;
4360 /* Forbid using -- on `bool'. */
4361 if (TREE_TYPE (arg
) == boolean_type_node
)
4363 if (code
== POSTDECREMENT_EXPR
|| code
== PREDECREMENT_EXPR
)
4365 cp_error ("invalid use of `--' on bool variable `%D'", arg
);
4366 return error_mark_node
;
4369 /* This will only work if someone can convince Kenner to accept
4370 my patch to expand_increment. (jason) */
4371 val
= build (code
, TREE_TYPE (arg
), arg
, inc
);
4373 if (code
== POSTINCREMENT_EXPR
)
4375 arg
= stabilize_reference (arg
);
4376 val
= build (MODIFY_EXPR
, TREE_TYPE (arg
), arg
,
4378 TREE_SIDE_EFFECTS (val
) = 1;
4379 arg
= save_expr (arg
);
4380 val
= build (COMPOUND_EXPR
, TREE_TYPE (arg
), val
, arg
);
4381 val
= build (COMPOUND_EXPR
, TREE_TYPE (arg
), arg
, val
);
4384 val
= build (MODIFY_EXPR
, TREE_TYPE (arg
), arg
,
4389 val
= build (code
, TREE_TYPE (arg
), arg
, inc
);
4391 TREE_SIDE_EFFECTS (val
) = 1;
4392 return cp_convert (result_type
, val
);
4396 /* Note that this operation never does default_conversion
4397 regardless of NOCONVERT. */
4399 argtype
= TREE_TYPE (arg
);
4400 if (TREE_CODE (argtype
) == REFERENCE_TYPE
)
4404 build_pointer_type (TREE_TYPE (TREE_TYPE (arg
))), arg
);
4405 TREE_CONSTANT (arg
) = TREE_CONSTANT (TREE_OPERAND (arg
, 0));
4409 && TREE_CODE (arg
) == FUNCTION_DECL
4411 && DECL_CONTEXT (arg
) == NULL_TREE
4412 && IDENTIFIER_LENGTH (DECL_NAME (arg
)) == 4
4413 && IDENTIFIER_POINTER (DECL_NAME (arg
))[0] == 'm'
4414 && ! strcmp (IDENTIFIER_POINTER (DECL_NAME (arg
)), "main"))
4416 pedwarn ("taking address of function `main'");
4418 /* Let &* cancel out to simplify resulting code. */
4419 if (TREE_CODE (arg
) == INDIRECT_REF
)
4421 /* We don't need to have `current_class_ptr' wrapped in a
4422 NON_LVALUE_EXPR node. */
4423 if (arg
== current_class_ref
)
4424 return current_class_ptr
;
4426 arg
= TREE_OPERAND (arg
, 0);
4427 if (TREE_CODE (TREE_TYPE (arg
)) == REFERENCE_TYPE
)
4431 build_pointer_type (TREE_TYPE (TREE_TYPE (arg
))), arg
);
4432 TREE_CONSTANT (arg
) = TREE_CONSTANT (TREE_OPERAND (arg
, 0));
4434 else if (lvalue_p (arg
))
4435 /* Don't let this be an lvalue. */
4436 return non_lvalue (arg
);
4440 /* For &x[y], return x+y */
4441 if (TREE_CODE (arg
) == ARRAY_REF
)
4443 if (mark_addressable (TREE_OPERAND (arg
, 0)) == 0)
4444 return error_mark_node
;
4445 return build_binary_op (PLUS_EXPR
, TREE_OPERAND (arg
, 0),
4446 TREE_OPERAND (arg
, 1), 1);
4449 /* Uninstantiated types are all functions. Taking the
4450 address of a function is a no-op, so just return the
4453 if (TREE_CODE (arg
) == IDENTIFIER_NODE
4454 && IDENTIFIER_OPNAME_P (arg
))
4456 my_friendly_abort (117);
4457 /* We don't know the type yet, so just work around the problem.
4458 We know that this will resolve to an lvalue. */
4459 return build1 (ADDR_EXPR
, unknown_type_node
, arg
);
4462 if (TREE_CODE (arg
) == TREE_LIST
)
4464 if (TREE_CODE (TREE_VALUE (arg
)) == FUNCTION_DECL
4465 && DECL_CHAIN (TREE_VALUE (arg
)) == NULL_TREE
)
4466 /* Unique overloaded non-member function. */
4467 return build_unary_op (ADDR_EXPR
, TREE_VALUE (arg
), 0);
4468 if (TREE_CHAIN (arg
) == NULL_TREE
4469 && TREE_CODE (TREE_VALUE (arg
)) == TREE_LIST
4470 && DECL_CHAIN (TREE_VALUE (TREE_VALUE (arg
))) == NULL_TREE
)
4471 /* Unique overloaded member function. */
4472 return build_unary_op (ADDR_EXPR
, TREE_VALUE (TREE_VALUE (arg
)),
4474 return build1 (ADDR_EXPR
, unknown_type_node
, arg
);
4476 else if (TREE_CODE (arg
) == TEMPLATE_ID_EXPR
)
4481 /* We don't require a match here; it's possible that the
4482 context (like a cast to a particular type) will resolve
4483 the particular choice of template. */
4484 fn
= determine_explicit_specialization (arg
, NULL_TREE
,
4490 fn
= instantiate_template (fn
, targs
);
4491 mark_addressable (fn
);
4492 return build_unary_op (ADDR_EXPR
, fn
, 0);
4495 return build1 (ADDR_EXPR
, unknown_type_node
, arg
);
4498 /* Handle complex lvalues (when permitted)
4499 by reduction to simpler cases. */
4500 val
= unary_complex_lvalue (code
, arg
);
4504 switch (TREE_CODE (arg
))
4509 case FIX_TRUNC_EXPR
:
4510 case FIX_FLOOR_EXPR
:
4511 case FIX_ROUND_EXPR
:
4513 if (! lvalue_p (arg
) && pedantic
)
4514 pedwarn ("taking the address of a cast to non-reference type");
4521 /* Allow the address of a constructor if all the elements
4523 if (TREE_CODE (arg
) == CONSTRUCTOR
&& TREE_CONSTANT (arg
))
4525 /* Anything not already handled and not a true memory reference
4527 else if (TREE_CODE (argtype
) != FUNCTION_TYPE
4528 && TREE_CODE (argtype
) != METHOD_TYPE
4529 && !lvalue_or_else (arg
, "unary `&'"))
4530 return error_mark_node
;
4532 /* Ordinary case; arg is a COMPONENT_REF or a decl. */
4533 /* If the lvalue is const or volatile,
4534 merge that into the type that the address will point to. */
4535 if (TREE_CODE_CLASS (TREE_CODE (arg
)) == 'd'
4536 || TREE_CODE_CLASS (TREE_CODE (arg
)) == 'r')
4538 if (TREE_READONLY (arg
) || TREE_THIS_VOLATILE (arg
))
4539 argtype
= cp_build_type_variant (argtype
,
4540 TREE_READONLY (arg
),
4541 TREE_THIS_VOLATILE (arg
));
4544 argtype
= build_pointer_type (argtype
);
4546 if (mark_addressable (arg
) == 0)
4547 return error_mark_node
;
4552 if (TREE_CODE (arg
) == COMPONENT_REF
)
4553 addr
= build_component_addr (arg
, argtype
,
4554 "attempt to take address of bit-field structure member `%s'");
4556 addr
= build1 (code
, argtype
, arg
);
4558 /* Address of a static or external variable or
4559 function counts as a constant */
4561 TREE_CONSTANT (addr
) = 1;
4563 if (TREE_CODE (argtype
) == POINTER_TYPE
4564 && TREE_CODE (TREE_TYPE (argtype
)) == METHOD_TYPE
)
4566 build_ptrmemfunc_type (argtype
);
4567 addr
= build_ptrmemfunc (argtype
, addr
, 0);
4580 argtype
= TREE_TYPE (arg
);
4581 return fold (build1 (code
, argtype
, arg
));
4585 return error_mark_node
;
4589 /* If CONVERSIONS is a conversion expression or a nested sequence of such,
4590 convert ARG with the same conversions in the same order
4591 and return the result. */
4594 convert_sequence (conversions
, arg
)
4598 switch (TREE_CODE (conversions
))
4603 case FIX_TRUNC_EXPR
:
4604 case FIX_FLOOR_EXPR
:
4605 case FIX_ROUND_EXPR
:
4607 return cp_convert (TREE_TYPE (conversions
),
4608 convert_sequence (TREE_OPERAND (conversions
, 0),
4617 /* Apply unary lvalue-demanding operator CODE to the expression ARG
4618 for certain kinds of expressions which are not really lvalues
4619 but which we can accept as lvalues.
4621 If ARG is not a kind of expression we can handle, return zero. */
4624 unary_complex_lvalue (code
, arg
)
4625 enum tree_code code
;
4628 /* Handle (a, b) used as an "lvalue". */
4629 if (TREE_CODE (arg
) == COMPOUND_EXPR
)
4631 tree real_result
= build_unary_op (code
, TREE_OPERAND (arg
, 1), 0);
4632 return build (COMPOUND_EXPR
, TREE_TYPE (real_result
),
4633 TREE_OPERAND (arg
, 0), real_result
);
4636 /* Handle (a ? b : c) used as an "lvalue". */
4637 if (TREE_CODE (arg
) == COND_EXPR
4638 || TREE_CODE (arg
) == MIN_EXPR
|| TREE_CODE (arg
) == MAX_EXPR
)
4639 return rationalize_conditional_expr (code
, arg
);
4641 if (TREE_CODE (arg
) == MODIFY_EXPR
4642 || TREE_CODE (arg
) == PREINCREMENT_EXPR
4643 || TREE_CODE (arg
) == PREDECREMENT_EXPR
)
4644 return unary_complex_lvalue
4645 (code
, build (COMPOUND_EXPR
, TREE_TYPE (TREE_OPERAND (arg
, 0)),
4646 arg
, TREE_OPERAND (arg
, 0)));
4648 if (code
!= ADDR_EXPR
)
4651 /* Handle (a = b) used as an "lvalue" for `&'. */
4652 if (TREE_CODE (arg
) == MODIFY_EXPR
4653 || TREE_CODE (arg
) == INIT_EXPR
)
4655 tree real_result
= build_unary_op (code
, TREE_OPERAND (arg
, 0), 0);
4656 arg
= build (COMPOUND_EXPR
, TREE_TYPE (real_result
), arg
, real_result
);
4657 TREE_NO_UNUSED_WARNING (arg
) = 1;
4661 if (TREE_CODE (TREE_TYPE (arg
)) == FUNCTION_TYPE
4662 || TREE_CODE (TREE_TYPE (arg
)) == METHOD_TYPE
4663 || TREE_CODE (TREE_TYPE (arg
)) == OFFSET_TYPE
)
4665 /* The representation of something of type OFFSET_TYPE
4666 is really the representation of a pointer to it.
4667 Here give the representation its true type. */
4670 my_friendly_assert (TREE_CODE (arg
) != SCOPE_REF
, 313);
4672 if (TREE_CODE (arg
) != OFFSET_REF
)
4675 t
= TREE_OPERAND (arg
, 1);
4677 if (TREE_CODE (t
) == FUNCTION_DECL
) /* Check all this code for right semantics. */
4678 return build_unary_op (ADDR_EXPR
, t
, 0);
4679 if (TREE_CODE (t
) == VAR_DECL
)
4680 return build_unary_op (ADDR_EXPR
, t
, 0);
4684 tree offset
= integer_zero_node
;
4686 if (TREE_OPERAND (arg
, 0)
4687 && (TREE_CODE (TREE_OPERAND (arg
, 0)) != NOP_EXPR
4688 || TREE_OPERAND (TREE_OPERAND (arg
, 0), 0) != error_mark_node
))
4689 if (TREE_CODE (t
) != FIELD_DECL
)
4691 /* Don't know if this should return address to just
4692 _DECL, or actual address resolved in this expression. */
4693 sorry ("address of bound pointer-to-member expression");
4694 return error_mark_node
;
4697 type
= TYPE_OFFSET_BASETYPE (TREE_TYPE (arg
));
4699 /* Now in the offset to the final subobject. */
4700 offset
= size_binop (PLUS_EXPR
,
4702 get_delta_difference (DECL_FIELD_CONTEXT (t
),
4706 /* Add in the offset to the field. */
4707 offset
= size_binop (PLUS_EXPR
, offset
,
4708 size_binop (EASY_DIV_EXPR
,
4709 DECL_FIELD_BITPOS (t
),
4710 size_int (BITS_PER_UNIT
)));
4712 /* We offset all pointer to data members by 1 so that we can
4713 distinguish between a null pointer to data member and the first
4714 data member of a structure. */
4715 offset
= size_binop (PLUS_EXPR
, offset
, size_int (1));
4717 return cp_convert (build_pointer_type (TREE_TYPE (arg
)), offset
);
4722 /* We permit compiler to make function calls returning
4723 objects of aggregate type look like lvalues. */
4727 if (TREE_CODE (targ
) == SAVE_EXPR
)
4728 targ
= TREE_OPERAND (targ
, 0);
4730 if (TREE_CODE (targ
) == CALL_EXPR
&& IS_AGGR_TYPE (TREE_TYPE (targ
)))
4732 if (TREE_CODE (arg
) == SAVE_EXPR
)
4735 targ
= build_cplus_new (TREE_TYPE (arg
), arg
);
4736 return build1 (ADDR_EXPR
, build_pointer_type (TREE_TYPE (arg
)), targ
);
4739 if (TREE_CODE (arg
) == SAVE_EXPR
&& TREE_CODE (targ
) == INDIRECT_REF
)
4740 return build (SAVE_EXPR
, build_pointer_type (TREE_TYPE (arg
)),
4741 TREE_OPERAND (targ
, 0), current_function_decl
, NULL
);
4744 /* Don't let anything else be handled specially. */
4748 /* Mark EXP saying that we need to be able to take the
4749 address of it; it should not be allocated in a register.
4750 Value is 1 if successful.
4752 C++: we do not allow `current_class_ptr' to be addressable. */
4755 mark_addressable (exp
)
4758 register tree x
= exp
;
4760 if (TREE_ADDRESSABLE (x
) == 1)
4764 switch (TREE_CODE (x
))
4771 x
= TREE_OPERAND (x
, 0);
4775 if (x
== current_class_ptr
)
4777 if (! flag_this_is_variable
)
4778 error ("address of `this' not available");
4779 TREE_ADDRESSABLE (x
) = 1; /* so compiler doesn't die later */
4780 put_var_into_stack (x
);
4784 if (TREE_STATIC (x
) && TREE_READONLY (x
)
4785 && DECL_RTL (x
) != 0
4786 && ! DECL_IN_MEMORY_P (x
))
4788 /* We thought this would make a good constant variable,
4789 but we were wrong. */
4790 push_obstacks_nochange ();
4791 end_temporary_allocation ();
4793 TREE_ASM_WRITTEN (x
) = 0;
4795 rest_of_decl_compilation (x
, 0, IDENTIFIER_LOCAL_VALUE (x
) == 0, 0);
4796 TREE_ADDRESSABLE (x
) = 1;
4802 /* Caller should not be trying to mark initialized
4803 constant fields addressable. */
4804 my_friendly_assert (DECL_LANG_SPECIFIC (x
) == 0
4805 || DECL_IN_AGGR_P (x
) == 0
4807 || DECL_EXTERNAL (x
), 314);
4811 if (DECL_REGISTER (x
) && !TREE_ADDRESSABLE (x
)
4812 && !DECL_ARTIFICIAL (x
) && extra_warnings
)
4813 cp_warning ("address requested for `%D', which is declared `register'",
4815 put_var_into_stack (x
);
4816 TREE_ADDRESSABLE (x
) = 1;
4820 if (DECL_LANG_SPECIFIC (x
) != 0)
4822 x
= DECL_MAIN_VARIANT (x
);
4823 /* We have to test both conditions here. The first may be
4824 non-zero in the case of processing a default function. The
4825 second may be non-zero in the case of a template function. */
4826 if (DECL_TEMPLATE_INFO (x
) && !DECL_TEMPLATE_SPECIALIZATION (x
))
4829 TREE_ADDRESSABLE (x
) = 1;
4831 TREE_ADDRESSABLE (DECL_ASSEMBLER_NAME (x
)) = 1;
4835 TREE_ADDRESSABLE (x
) = 1;
4839 TREE_ADDRESSABLE (x
) = 1;
4840 mark_addressable (TREE_OPERAND (x
, 0));
4848 /* Build and return a conditional expression IFEXP ? OP1 : OP2. */
4851 build_x_conditional_expr (ifexp
, op1
, op2
)
4852 tree ifexp
, op1
, op2
;
4854 tree rval
= NULL_TREE
;
4856 if (processing_template_decl
)
4857 return build_min_nt (COND_EXPR
, ifexp
, op1
, op2
);
4859 if (flag_ansi_overloading
)
4860 return build_new_op (COND_EXPR
, LOOKUP_NORMAL
, ifexp
, op1
, op2
);
4862 /* See comments in `build_x_binary_op'. */
4864 rval
= build_opfncall (COND_EXPR
, LOOKUP_SPECULATIVELY
, ifexp
, op1
, op2
);
4866 return build_opfncall (COND_EXPR
, LOOKUP_NORMAL
, ifexp
, op1
, op2
);
4868 return build_conditional_expr (ifexp
, op1
, op2
);
4872 build_conditional_expr (ifexp
, op1
, op2
)
4873 tree ifexp
, op1
, op2
;
4875 register tree type1
;
4876 register tree type2
;
4877 register enum tree_code code1
;
4878 register enum tree_code code2
;
4879 register tree result_type
= NULL_TREE
;
4881 /* If second operand is omitted, it is the same as the first one;
4882 make sure it is calculated only once. */
4886 pedwarn ("ANSI C++ forbids omitting the middle term of a ?: expression");
4887 ifexp
= op1
= save_expr (ifexp
);
4890 ifexp
= cp_convert (boolean_type_node
, ifexp
);
4892 if (TREE_CODE (ifexp
) == ERROR_MARK
)
4893 return error_mark_node
;
4895 op1
= require_instantiated_type (TREE_TYPE (op2
), op1
, error_mark_node
);
4896 if (op1
== error_mark_node
)
4897 return error_mark_node
;
4898 op2
= require_instantiated_type (TREE_TYPE (op1
), op2
, error_mark_node
);
4899 if (op2
== error_mark_node
)
4900 return error_mark_node
;
4902 /* C++: REFERENCE_TYPES must be dereferenced. */
4903 type1
= TREE_TYPE (op1
);
4904 code1
= TREE_CODE (type1
);
4905 type2
= TREE_TYPE (op2
);
4906 code2
= TREE_CODE (type2
);
4908 if (code1
== REFERENCE_TYPE
)
4910 op1
= convert_from_reference (op1
);
4911 type1
= TREE_TYPE (op1
);
4912 code1
= TREE_CODE (type1
);
4914 if (code2
== REFERENCE_TYPE
)
4916 op2
= convert_from_reference (op2
);
4917 type2
= TREE_TYPE (op2
);
4918 code2
= TREE_CODE (type2
);
4921 /* Don't promote the operands separately if they promote
4922 the same way. Return the unpromoted type and let the combined
4923 value get promoted if necessary. */
4925 if (TYPE_MAIN_VARIANT (type1
) == TYPE_MAIN_VARIANT (type2
)
4926 && code2
!= ARRAY_TYPE
4927 && code2
!= FUNCTION_TYPE
4928 && code2
!= METHOD_TYPE
)
4932 if (TREE_CONSTANT (ifexp
)
4933 && (TREE_CODE (ifexp
) == INTEGER_CST
4934 || TREE_CODE (ifexp
) == ADDR_EXPR
))
4935 return (integer_zerop (ifexp
) ? op2
: op1
);
4937 if (TREE_CODE (op1
) == CONST_DECL
)
4938 op1
= DECL_INITIAL (op1
);
4939 else if (TREE_READONLY_DECL_P (op1
))
4940 op1
= decl_constant_value (op1
);
4941 if (TREE_CODE (op2
) == CONST_DECL
)
4942 op2
= DECL_INITIAL (op2
);
4943 else if (TREE_READONLY_DECL_P (op2
))
4944 op2
= decl_constant_value (op2
);
4946 type1
= cp_build_type_variant
4948 TREE_READONLY (op1
) || TREE_READONLY (op2
),
4949 TREE_THIS_VOLATILE (op1
) || TREE_THIS_VOLATILE (op2
));
4950 /* ??? This is a kludge to deal with the fact that
4951 we don't sort out integers and enums properly, yet. */
4952 result
= fold (build (COND_EXPR
, type1
, ifexp
, op1
, op2
));
4953 if (TREE_TYPE (result
) != type1
)
4954 result
= build1 (NOP_EXPR
, type1
, result
);
4955 /* Expand both sides into the same slot,
4956 hopefully the target of the ?: expression. */
4957 if (TREE_CODE (op1
) == TARGET_EXPR
&& TREE_CODE (op2
) == TARGET_EXPR
)
4959 tree slot
= build (VAR_DECL
, TREE_TYPE (result
));
4960 layout_decl (slot
, 0);
4961 result
= build (TARGET_EXPR
, TREE_TYPE (result
),
4962 slot
, result
, NULL_TREE
, NULL_TREE
);
4967 /* They don't match; promote them both and then try to reconcile them.
4968 But don't permit mismatching enum types. */
4969 if (code1
== ENUMERAL_TYPE
)
4971 if (code2
== ENUMERAL_TYPE
)
4973 cp_error ("enumeral mismatch in conditional expression: `%T' vs `%T'", type1
, type2
);
4974 return error_mark_node
;
4976 else if (extra_warnings
&& ! IS_AGGR_TYPE_CODE (code2
)
4977 && type2
!= type_promotes_to (type1
))
4978 warning ("enumeral and non-enumeral type in conditional expression");
4980 else if (extra_warnings
4981 && code2
== ENUMERAL_TYPE
&& ! IS_AGGR_TYPE_CODE (code1
)
4982 && type1
!= type_promotes_to (type2
))
4983 warning ("enumeral and non-enumeral type in conditional expression");
4985 if (code1
!= VOID_TYPE
)
4987 op1
= default_conversion (op1
);
4988 type1
= TREE_TYPE (op1
);
4989 if (TYPE_PTRMEMFUNC_P (type1
))
4990 type1
= TYPE_PTRMEMFUNC_FN_TYPE (type1
);
4991 code1
= TREE_CODE (type1
);
4993 if (code2
!= VOID_TYPE
)
4995 op2
= default_conversion (op2
);
4996 type2
= TREE_TYPE (op2
);
4997 if (TYPE_PTRMEMFUNC_P (type2
))
4998 type2
= TYPE_PTRMEMFUNC_FN_TYPE (type2
);
4999 code2
= TREE_CODE (type2
);
5002 if (code1
== RECORD_TYPE
&& code2
== RECORD_TYPE
5003 && real_lvalue_p (op1
) && real_lvalue_p (op2
)
5004 && comptypes (type1
, type2
, -1))
5006 type1
= build_reference_type (type1
);
5007 type2
= build_reference_type (type2
);
5008 result_type
= common_type (type1
, type2
);
5009 op1
= convert_to_reference (result_type
, op1
, CONV_IMPLICIT
,
5010 LOOKUP_NORMAL
, NULL_TREE
);
5011 op2
= convert_to_reference (result_type
, op2
, CONV_IMPLICIT
,
5012 LOOKUP_NORMAL
, NULL_TREE
);
5014 /* Quickly detect the usual case where op1 and op2 have the same type
5016 else if (TYPE_MAIN_VARIANT (type1
) == TYPE_MAIN_VARIANT (type2
))
5019 result_type
= type1
;
5021 result_type
= cp_build_type_variant
5023 TREE_READONLY (op1
) || TREE_READONLY (op2
),
5024 TREE_THIS_VOLATILE (op1
) || TREE_THIS_VOLATILE (op2
));
5026 else if ((code1
== INTEGER_TYPE
|| code1
== REAL_TYPE
)
5027 && (code2
== INTEGER_TYPE
|| code2
== REAL_TYPE
))
5029 result_type
= common_type (type1
, type2
);
5031 else if (code1
== VOID_TYPE
|| code2
== VOID_TYPE
)
5033 if (pedantic
&& (code1
!= VOID_TYPE
|| code2
!= VOID_TYPE
))
5034 pedwarn ("ANSI C++ forbids conditional expr with only one void side");
5035 result_type
= void_type_node
;
5037 else if (code1
== POINTER_TYPE
&& null_ptr_cst_p (op2
))
5038 result_type
= qualify_type (type1
, type2
);
5039 else if (code2
== POINTER_TYPE
&& null_ptr_cst_p (op1
))
5040 result_type
= qualify_type (type2
, type1
);
5041 else if (code1
== POINTER_TYPE
&& code2
== POINTER_TYPE
)
5043 if (comp_target_types (type1
, type2
, 1))
5044 result_type
= common_type (type1
, type2
);
5045 else if (TYPE_MAIN_VARIANT (TREE_TYPE (type1
)) == void_type_node
)
5047 if (pedantic
&& TREE_CODE (type2
) == FUNCTION_TYPE
)
5048 pedwarn ("ANSI C++ forbids conditional expr between `void *' and function pointer");
5049 result_type
= qualify_type (type1
, type2
);
5051 else if (TYPE_MAIN_VARIANT (TREE_TYPE (type2
)) == void_type_node
)
5053 if (pedantic
&& TREE_CODE (type1
) == FUNCTION_TYPE
)
5054 pedwarn ("ANSI C++ forbids conditional expr between `void *' and function pointer");
5055 result_type
= qualify_type (type2
, type1
);
5058 else if (comptypes (type2
, type1
, 0))
5059 result_type
= type2
;
5060 else if (IS_AGGR_TYPE (TREE_TYPE (type1
))
5061 && IS_AGGR_TYPE (TREE_TYPE (type2
))
5062 && (result_type
= common_base_type (TREE_TYPE (type1
), TREE_TYPE (type2
))))
5064 if (result_type
== error_mark_node
)
5066 cp_error ("common base type of types `%T' and `%T' is ambiguous",
5067 TREE_TYPE (type1
), TREE_TYPE (type2
));
5068 result_type
= ptr_type_node
;
5073 && result_type
!= TREE_TYPE (type1
)
5074 && result_type
!= TREE_TYPE (type2
))
5075 cp_pedwarn ("`%T' and `%T' converted to `%T *' in conditional expression",
5076 type1
, type2
, result_type
);
5078 result_type
= build_pointer_type (result_type
);
5083 pedwarn ("pointer type mismatch in conditional expression");
5084 result_type
= ptr_type_node
;
5087 else if (code1
== POINTER_TYPE
&& code2
== INTEGER_TYPE
)
5089 pedwarn ("pointer/integer type mismatch in conditional expression");
5090 result_type
= type1
;
5092 else if (code2
== POINTER_TYPE
&& code1
== INTEGER_TYPE
)
5094 pedwarn ("pointer/integer type mismatch in conditional expression");
5095 result_type
= type2
;
5100 /* The match does not look good. If either is
5101 an aggregate value, try converting to a scalar type. */
5102 if (code1
== RECORD_TYPE
&& code2
== RECORD_TYPE
)
5104 cp_error ("aggregate mismatch in conditional expression: `%T' vs `%T'", type1
, type2
);
5105 return error_mark_node
;
5107 /* Warning: this code assumes that conversion between cv-variants of
5108 a type is done using NOP_EXPRs. */
5109 if (code1
== RECORD_TYPE
&& TYPE_HAS_CONVERSION (type1
))
5111 /* There are other types besides pointers and records. */
5113 if (code2
== POINTER_TYPE
)
5114 tmp
= build_pointer_type
5115 (build_type_variant (TREE_TYPE (type2
), 1, 1));
5118 tmp
= build_type_conversion (CONVERT_EXPR
, tmp
, op1
, 0);
5119 if (tmp
== NULL_TREE
)
5121 cp_error ("incompatible types `%T' and `%T' in `?:'",
5123 return error_mark_node
;
5125 if (tmp
== error_mark_node
)
5126 error ("ambiguous pointer conversion");
5129 result_type
= common_type (type2
, TREE_TYPE (tmp
));
5132 else if (code2
== RECORD_TYPE
&& TYPE_HAS_CONVERSION (type2
))
5135 if (code1
== POINTER_TYPE
)
5136 tmp
= build_pointer_type
5137 (build_type_variant (TREE_TYPE (type1
), 1, 1));
5141 tmp
= build_type_conversion (CONVERT_EXPR
, tmp
, op2
, 0);
5142 if (tmp
== NULL_TREE
)
5144 cp_error ("incompatible types `%T' and `%T' in `?:'",
5146 return error_mark_node
;
5148 if (tmp
== error_mark_node
)
5149 error ("ambiguous pointer conversion");
5152 result_type
= common_type (type1
, TREE_TYPE (tmp
));
5155 else if (flag_cond_mismatch
)
5156 result_type
= void_type_node
;
5159 error ("type mismatch in conditional expression");
5160 return error_mark_node
;
5164 if (TREE_CODE (result_type
) == POINTER_TYPE
5165 && TREE_CODE (TREE_TYPE (result_type
)) == METHOD_TYPE
)
5166 result_type
= build_ptrmemfunc_type (result_type
);
5168 if (result_type
!= TREE_TYPE (op1
))
5169 op1
= convert_for_initialization
5170 (NULL_TREE
, result_type
, op1
, LOOKUP_NORMAL
, "converting", NULL_TREE
, 0);
5171 if (result_type
!= TREE_TYPE (op2
))
5172 op2
= convert_for_initialization
5173 (NULL_TREE
, result_type
, op2
, LOOKUP_NORMAL
, "converting", NULL_TREE
, 0);
5175 if (TREE_CONSTANT (ifexp
))
5176 return integer_zerop (ifexp
) ? op2
: op1
;
5178 return convert_from_reference
5179 (fold (build (COND_EXPR
, result_type
, ifexp
, op1
, op2
)));
5182 /* Handle overloading of the ',' operator when needed. Otherwise,
5183 this function just builds an expression list. */
5186 build_x_compound_expr (list
)
5189 tree rest
= TREE_CHAIN (list
);
5192 if (processing_template_decl
)
5193 return build_min_nt (COMPOUND_EXPR
, list
, NULL_TREE
);
5195 if (rest
== NULL_TREE
)
5196 return build_compound_expr (list
);
5198 result
= build_opfncall (COMPOUND_EXPR
, LOOKUP_NORMAL
,
5199 TREE_VALUE (list
), TREE_VALUE (rest
), NULL_TREE
);
5201 return build_x_compound_expr (expr_tree_cons (NULL_TREE
, result
, TREE_CHAIN (rest
)));
5203 if (! TREE_SIDE_EFFECTS (TREE_VALUE (list
)))
5205 /* the left-hand operand of a comma expression is like an expression
5206 statement: we should warn if it doesn't have any side-effects,
5207 unless it was explicitly cast to (void). */
5208 if ((extra_warnings
|| warn_unused
)
5209 && !(TREE_CODE (TREE_VALUE(list
)) == CONVERT_EXPR
5210 && TREE_TYPE (TREE_VALUE(list
)) == void_type_node
))
5211 warning("left-hand operand of comma expression has no effect");
5213 #if 0 /* this requires a gcc backend patch to export warn_if_unused_value */
5214 else if (warn_unused
)
5215 warn_if_unused_value (TREE_VALUE(list
));
5218 return build_compound_expr (expr_tree_cons (NULL_TREE
, TREE_VALUE (list
),
5219 build_expr_list (NULL_TREE
, build_x_compound_expr (rest
))));
5222 /* Given a list of expressions, return a compound expression
5223 that performs them all and returns the value of the last of them. */
5226 build_compound_expr (list
)
5231 if (TREE_READONLY_DECL_P (TREE_VALUE (list
)))
5232 TREE_VALUE (list
) = decl_constant_value (TREE_VALUE (list
));
5234 if (TREE_CHAIN (list
) == 0)
5236 /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
5237 Strip such NOP_EXPRs, since LIST is used in non-lvalue context. */
5238 if (TREE_CODE (list
) == NOP_EXPR
5239 && TREE_TYPE (list
) == TREE_TYPE (TREE_OPERAND (list
, 0)))
5240 list
= TREE_OPERAND (list
, 0);
5242 /* Convert arrays to pointers. */
5243 if (TREE_CODE (TREE_TYPE (TREE_VALUE (list
))) == ARRAY_TYPE
)
5244 return default_conversion (TREE_VALUE (list
));
5246 return TREE_VALUE (list
);
5249 rest
= build_compound_expr (TREE_CHAIN (list
));
5251 /* When pedantic, a compound expression cannot be a constant expression. */
5252 if (! TREE_SIDE_EFFECTS (TREE_VALUE (list
)) && ! pedantic
)
5255 return build (COMPOUND_EXPR
, TREE_TYPE (rest
),
5256 break_out_cleanups (TREE_VALUE (list
)), rest
);
5260 build_static_cast (type
, expr
)
5266 if (type
== error_mark_node
|| expr
== error_mark_node
)
5267 return error_mark_node
;
5269 if (TREE_CODE (expr
) == OFFSET_REF
)
5270 expr
= resolve_offset_ref (expr
);
5272 if (processing_template_decl
)
5274 tree t
= build_min (STATIC_CAST_EXPR
, copy_to_permanent (type
),
5279 /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
5280 Strip such NOP_EXPRs if VALUE is being used in non-lvalue context. */
5281 if (TREE_CODE (type
) != REFERENCE_TYPE
5282 && TREE_CODE (expr
) == NOP_EXPR
5283 && TREE_TYPE (expr
) == TREE_TYPE (TREE_OPERAND (expr
, 0)))
5284 expr
= TREE_OPERAND (expr
, 0);
5286 if (TREE_CODE (type
) == VOID_TYPE
)
5287 return build1 (CONVERT_EXPR
, type
, expr
);
5289 if (type_unknown_p (expr
))
5291 expr
= instantiate_type (type
, expr
, 1);
5292 if (expr
== error_mark_node
)
5293 return error_mark_node
;
5296 if (TREE_CODE (type
) == REFERENCE_TYPE
)
5297 return (convert_from_reference
5298 (convert_to_reference (type
, expr
, CONV_STATIC
|CONV_IMPLICIT
,
5299 LOOKUP_COMPLAIN
, NULL_TREE
)));
5301 if (IS_AGGR_TYPE (type
))
5302 return build_cplus_new
5303 (type
, (build_method_call
5304 (NULL_TREE
, ctor_identifier
, build_expr_list (NULL_TREE
, expr
),
5305 TYPE_BINFO (type
), LOOKUP_NORMAL
)));
5307 expr
= decay_conversion (expr
);
5308 intype
= TREE_TYPE (expr
);
5310 /* FIXME handle casting to array type. */
5313 if (can_convert_arg (type
, intype
, expr
))
5315 else if (TYPE_PTROB_P (type
) && TYPE_PTROB_P (intype
))
5318 if (IS_AGGR_TYPE (TREE_TYPE (type
)) && IS_AGGR_TYPE (TREE_TYPE (intype
))
5319 && (TYPE_READONLY (TREE_TYPE (type
))
5320 >= TYPE_READONLY (TREE_TYPE (intype
)))
5321 && (TYPE_VOLATILE (TREE_TYPE (type
))
5322 >= TYPE_VOLATILE (TREE_TYPE (intype
)))
5323 && (binfo
= get_binfo (TREE_TYPE (intype
), TREE_TYPE (type
), 0))
5324 && ! TREE_VIA_VIRTUAL (binfo
))
5327 else if (TYPE_PTRMEM_P (type
) && TYPE_PTRMEM_P (intype
))
5329 if (comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (type
))),
5330 TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (intype
))), 1)
5331 && (TYPE_READONLY (TREE_TYPE (TREE_TYPE (type
)))
5332 >= TYPE_READONLY (TREE_TYPE (TREE_TYPE (intype
))))
5333 && (TYPE_VOLATILE (TREE_TYPE (TREE_TYPE (type
)))
5334 >= TYPE_VOLATILE (TREE_TYPE (TREE_TYPE (intype
))))
5335 && (binfo
= get_binfo (TYPE_OFFSET_BASETYPE (intype
),
5336 TYPE_OFFSET_BASETYPE (type
), 0))
5337 && ! TREE_VIA_VIRTUAL (binfo
))
5340 else if (TREE_CODE (intype
) != BOOLEAN_TYPE
5341 && TREE_CODE (type
) != ARRAY_TYPE
5342 && TREE_CODE (type
) != FUNCTION_TYPE
5343 && can_convert (intype
, type
))
5347 return build_c_cast (type
, expr
);
5349 cp_error ("static_cast from `%T' to `%T'", intype
, type
);
5350 return error_mark_node
;
5354 build_reinterpret_cast (type
, expr
)
5359 if (type
== error_mark_node
|| expr
== error_mark_node
)
5360 return error_mark_node
;
5362 if (TREE_CODE (expr
) == OFFSET_REF
)
5363 expr
= resolve_offset_ref (expr
);
5365 if (processing_template_decl
)
5367 tree t
= build_min (REINTERPRET_CAST_EXPR
,
5368 copy_to_permanent (type
), expr
);
5372 if (TREE_CODE (type
) != REFERENCE_TYPE
)
5374 expr
= decay_conversion (expr
);
5376 /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
5377 Strip such NOP_EXPRs if VALUE is being used in non-lvalue context. */
5378 if (TREE_CODE (expr
) == NOP_EXPR
5379 && TREE_TYPE (expr
) == TREE_TYPE (TREE_OPERAND (expr
, 0)))
5380 expr
= TREE_OPERAND (expr
, 0);
5383 if (type_unknown_p (expr
))
5385 expr
= instantiate_type (type
, expr
, 1);
5386 if (expr
== error_mark_node
)
5387 return error_mark_node
;
5390 intype
= TREE_TYPE (expr
);
5392 if (TREE_CODE (type
) == REFERENCE_TYPE
)
5394 if (! real_lvalue_p (expr
))
5396 cp_error ("reinterpret_cast from `%T' rvalue to `%T'", intype
, type
);
5397 return error_mark_node
;
5399 expr
= build_unary_op (ADDR_EXPR
, expr
, 0);
5400 if (expr
!= error_mark_node
)
5401 expr
= build_reinterpret_cast
5402 (build_pointer_type (TREE_TYPE (type
)), expr
);
5403 if (expr
!= error_mark_node
)
5404 expr
= build_indirect_ref (expr
, 0);
5407 else if (comptypes (TYPE_MAIN_VARIANT (intype
), TYPE_MAIN_VARIANT (type
), 1))
5408 return build_static_cast (type
, expr
);
5410 if (TYPE_PTR_P (type
) && (TREE_CODE (intype
) == INTEGER_TYPE
5411 || TREE_CODE (intype
) == ENUMERAL_TYPE
))
5413 else if (TREE_CODE (type
) == INTEGER_TYPE
&& TYPE_PTR_P (intype
))
5415 if (TYPE_PRECISION (type
) < TYPE_PRECISION (intype
))
5416 cp_pedwarn ("reinterpret_cast from `%T' to `%T' loses precision",
5419 else if ((TYPE_PTRFN_P (type
) && TYPE_PTRFN_P (intype
))
5420 || (TYPE_PTRMEMFUNC_P (type
) && TYPE_PTRMEMFUNC_P (intype
)))
5422 if (TREE_READONLY_DECL_P (expr
))
5423 expr
= decl_constant_value (expr
);
5424 return fold (build1 (NOP_EXPR
, type
, expr
));
5426 else if ((TYPE_PTRMEM_P (type
) && TYPE_PTRMEM_P (intype
))
5427 || (TYPE_PTROBV_P (type
) && TYPE_PTROBV_P (intype
)))
5429 if (! comp_ptr_ttypes_reinterpret (TREE_TYPE (type
), TREE_TYPE (intype
)))
5430 cp_pedwarn ("reinterpret_cast from `%T' to `%T' casts away const (or volatile)",
5433 if (TREE_READONLY_DECL_P (expr
))
5434 expr
= decl_constant_value (expr
);
5435 return fold (build1 (NOP_EXPR
, type
, expr
));
5437 else if ((TYPE_PTRFN_P (type
) && TYPE_PTROBV_P (intype
))
5438 || (TYPE_PTRFN_P (type
) && TYPE_PTROBV_P (intype
)))
5440 pedwarn ("ANSI C++ forbids casting between pointers to functions and objects");
5441 if (TREE_READONLY_DECL_P (expr
))
5442 expr
= decl_constant_value (expr
);
5443 return fold (build1 (NOP_EXPR
, type
, expr
));
5447 cp_error ("reinterpret_cast from `%T' to `%T'", intype
, type
);
5448 return error_mark_node
;
5451 return cp_convert (type
, expr
);
5455 build_const_cast (type
, expr
)
5460 if (type
== error_mark_node
|| expr
== error_mark_node
)
5461 return error_mark_node
;
5463 if (TREE_CODE (expr
) == OFFSET_REF
)
5464 expr
= resolve_offset_ref (expr
);
5466 if (processing_template_decl
)
5468 tree t
= build_min (CONST_CAST_EXPR
, copy_to_permanent (type
),
5473 if (TREE_CODE (type
) != REFERENCE_TYPE
)
5475 expr
= decay_conversion (expr
);
5477 /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
5478 Strip such NOP_EXPRs if VALUE is being used in non-lvalue context. */
5479 if (TREE_CODE (expr
) == NOP_EXPR
5480 && TREE_TYPE (expr
) == TREE_TYPE (TREE_OPERAND (expr
, 0)))
5481 expr
= TREE_OPERAND (expr
, 0);
5484 if (type_unknown_p (expr
))
5486 expr
= instantiate_type (type
, expr
, 1);
5487 if (expr
== error_mark_node
)
5488 return error_mark_node
;
5491 intype
= TREE_TYPE (expr
);
5493 if (comptypes (TYPE_MAIN_VARIANT (intype
), TYPE_MAIN_VARIANT (type
), 1))
5494 return build_static_cast (type
, expr
);
5495 else if (TREE_CODE (type
) == REFERENCE_TYPE
)
5497 if (! real_lvalue_p (expr
))
5499 cp_error ("const_cast from `%T' rvalue to `%T'", intype
, type
);
5500 return error_mark_node
;
5503 if (comp_ptr_ttypes_const (TREE_TYPE (type
), intype
))
5505 expr
= build_unary_op (ADDR_EXPR
, expr
, 0);
5506 expr
= build1 (NOP_EXPR
, type
, expr
);
5507 return convert_from_reference (expr
);
5510 else if (TREE_CODE (type
) == POINTER_TYPE
5511 && TREE_CODE (intype
) == POINTER_TYPE
5512 && comp_ptr_ttypes_const (TREE_TYPE (type
), TREE_TYPE (intype
)))
5513 return cp_convert (type
, expr
);
5515 cp_error ("const_cast from `%T' to `%T'", intype
, type
);
5516 return error_mark_node
;
5519 /* Build an expression representing a cast to type TYPE of expression EXPR.
5521 ALLOW_NONCONVERTING is true if we should allow non-converting constructors
5522 when doing the cast. */
5525 build_c_cast (type
, expr
)
5528 register tree value
= expr
;
5530 if (type
== error_mark_node
|| expr
== error_mark_node
)
5531 return error_mark_node
;
5533 /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
5534 Strip such NOP_EXPRs if VALUE is being used in non-lvalue context. */
5535 if (TREE_CODE (type
) != REFERENCE_TYPE
5536 && TREE_CODE (value
) == NOP_EXPR
5537 && TREE_TYPE (value
) == TREE_TYPE (TREE_OPERAND (value
, 0)))
5538 value
= TREE_OPERAND (value
, 0);
5540 if (TREE_TYPE (expr
)
5541 && TREE_CODE (TREE_TYPE (expr
)) == OFFSET_TYPE
5542 && TREE_CODE (type
) != OFFSET_TYPE
)
5543 value
= resolve_offset_ref (value
);
5545 if (TREE_CODE (type
) == ARRAY_TYPE
)
5547 /* Allow casting from T1* to T2[] because Cfront allows it.
5548 NIHCL uses it. It is not valid ANSI C however, and hence, not
5550 if (TREE_CODE (TREE_TYPE (expr
)) == POINTER_TYPE
)
5553 pedwarn ("ANSI C++ forbids casting to an array type");
5554 type
= build_pointer_type (TREE_TYPE (type
));
5558 error ("ANSI C++ forbids casting to an array type");
5559 return error_mark_node
;
5563 if (TREE_CODE (type
) == FUNCTION_TYPE
5564 || TREE_CODE (type
) == METHOD_TYPE
)
5566 cp_error ("casting to function type `%T'", type
);
5567 return error_mark_node
;
5570 if (IS_SIGNATURE (type
))
5572 error ("cast specifies signature type");
5573 return error_mark_node
;
5576 if (processing_template_decl
)
5578 tree t
= build_min (CAST_EXPR
, type
,
5579 min_tree_cons (NULL_TREE
, value
, NULL_TREE
));
5583 if (TREE_CODE (type
) == VOID_TYPE
)
5584 value
= build1 (CONVERT_EXPR
, type
, value
);
5585 else if (TREE_TYPE (value
) == NULL_TREE
5586 || type_unknown_p (value
))
5588 value
= instantiate_type (type
, value
, 1);
5590 if (value
== error_mark_node
)
5591 return error_mark_node
;
5597 /* Convert functions and arrays to pointers and
5598 convert references to their expanded types,
5599 but don't convert any other types. */
5600 if (TREE_CODE (TREE_TYPE (value
)) == FUNCTION_TYPE
5601 || (TREE_CODE (TREE_TYPE (value
)) == METHOD_TYPE
5602 /* Don't do the default conversion if we want a
5603 pointer to a function. */
5604 && ! (TREE_CODE (type
) == POINTER_TYPE
5605 && TREE_CODE (TREE_TYPE (type
)) == FUNCTION_TYPE
))
5606 || TREE_CODE (TREE_TYPE (value
)) == ARRAY_TYPE
5607 || TREE_CODE (TREE_TYPE (value
)) == REFERENCE_TYPE
)
5608 value
= default_conversion (value
);
5609 otype
= TREE_TYPE (value
);
5611 /* Optionally warn about potentially worrisome casts. */
5614 && TREE_CODE (type
) == POINTER_TYPE
5615 && TREE_CODE (otype
) == POINTER_TYPE
)
5617 /* For C++ we make these regular warnings, rather than
5618 softening them into pedwarns. */
5619 if (TYPE_VOLATILE (TREE_TYPE (otype
))
5620 && ! TYPE_VOLATILE (TREE_TYPE (type
)))
5621 warning ("cast discards `volatile' from pointer target type");
5622 if (TYPE_READONLY (TREE_TYPE (otype
))
5623 && ! TYPE_READONLY (TREE_TYPE (type
)))
5624 warning ("cast discards `const' from pointer target type");
5627 /* Warn about possible alignment problems. */
5628 if (STRICT_ALIGNMENT
&& warn_cast_align
5629 && TREE_CODE (type
) == POINTER_TYPE
5630 && TREE_CODE (otype
) == POINTER_TYPE
5631 && TREE_CODE (TREE_TYPE (otype
)) != VOID_TYPE
5632 && TREE_CODE (TREE_TYPE (otype
)) != FUNCTION_TYPE
5633 && TYPE_ALIGN (TREE_TYPE (type
)) > TYPE_ALIGN (TREE_TYPE (otype
)))
5634 warning ("cast increases required alignment of target type");
5637 /* We should see about re-enabling these, they seem useful to
5639 if (TREE_CODE (type
) == INTEGER_TYPE
5640 && TREE_CODE (otype
) == POINTER_TYPE
5641 && TYPE_PRECISION (type
) != TYPE_PRECISION (otype
))
5642 warning ("cast from pointer to integer of different size");
5644 if (TREE_CODE (type
) == POINTER_TYPE
5645 && TREE_CODE (otype
) == INTEGER_TYPE
5646 && TYPE_PRECISION (type
) != TYPE_PRECISION (otype
)
5647 /* Don't warn about converting 0 to pointer,
5648 provided the 0 was explicit--not cast or made by folding. */
5649 && !(TREE_CODE (value
) == INTEGER_CST
&& integer_zerop (value
)))
5650 warning ("cast to pointer from integer of different size");
5653 if (TREE_CODE (type
) == REFERENCE_TYPE
)
5654 value
= (convert_from_reference
5655 (convert_to_reference (type
, value
, CONV_C_CAST
,
5656 LOOKUP_COMPLAIN
, NULL_TREE
)));
5661 if (TREE_READONLY_DECL_P (value
))
5662 value
= decl_constant_value (value
);
5665 value
= convert_force (type
, value
, CONV_C_CAST
);
5667 /* Ignore any integer overflow caused by the cast. */
5668 if (TREE_CODE (value
) == INTEGER_CST
)
5670 TREE_OVERFLOW (value
) = TREE_OVERFLOW (ovalue
);
5671 TREE_CONSTANT_OVERFLOW (value
) = TREE_CONSTANT_OVERFLOW (ovalue
);
5676 /* Always produce some operator for an explicit cast,
5677 so we can tell (for -pedantic) that the cast is no lvalue. */
5678 if (TREE_CODE (type
) != REFERENCE_TYPE
&& value
== expr
5679 && real_lvalue_p (value
))
5680 value
= non_lvalue (value
);
5686 expand_target_expr (t
)
5689 extern int temp_slot_level
;
5690 extern int target_temp_slot_level
;
5691 int old_temp_level
= target_temp_slot_level
;
5693 tree xval
= make_node (RTL_EXPR
);
5696 /* Any TARGET_EXPR temps live only as long as the outer temp level.
5697 Since they are preserved in this new inner level, we know they
5698 will make it into the outer level. */
5700 target_temp_slot_level
= temp_slot_level
;
5702 do_pending_stack_adjust ();
5703 start_sequence_for_rtl_expr (xval
);
5705 rtxval
= expand_expr (t
, NULL_RTX
, VOIDmode
, EXPAND_NORMAL
);
5706 do_pending_stack_adjust ();
5707 TREE_SIDE_EFFECTS (xval
) = 1;
5708 RTL_EXPR_SEQUENCE (xval
) = get_insns ();
5710 RTL_EXPR_RTL (xval
) = rtxval
;
5711 TREE_TYPE (xval
) = TREE_TYPE (t
);
5714 target_temp_slot_level
= old_temp_level
;
5719 /* Build an assignment expression of lvalue LHS from value RHS.
5720 MODIFYCODE is the code for a binary operator that we use
5721 to combine the old value of LHS with RHS to get the new value.
5722 Or else MODIFYCODE is NOP_EXPR meaning do a simple assignment.
5724 C++: If MODIFYCODE is INIT_EXPR, then leave references unbashed. */
5727 build_modify_expr (lhs
, modifycode
, rhs
)
5729 enum tree_code modifycode
;
5732 register tree result
;
5734 tree lhstype
= TREE_TYPE (lhs
);
5735 tree olhstype
= lhstype
;
5738 /* Avoid duplicate error messages from operands that had errors. */
5739 if (lhs
== error_mark_node
|| rhs
== error_mark_node
)
5740 return error_mark_node
;
5742 /* Types that aren't fully specified cannot be used in assignments. */
5743 lhs
= require_complete_type (lhs
);
5747 /* Handle assignment to signature pointers/refs. */
5749 if (TYPE_LANG_SPECIFIC (lhstype
)
5750 && (IS_SIGNATURE_POINTER (lhstype
) || IS_SIGNATURE_REFERENCE (lhstype
)))
5752 return build_signature_pointer_constructor (lhs
, rhs
);
5755 /* Handle control structure constructs used as "lvalues". */
5757 switch (TREE_CODE (lhs
))
5759 /* Handle --foo = 5; as these are valid constructs in C++ */
5760 case PREDECREMENT_EXPR
:
5761 case PREINCREMENT_EXPR
:
5762 if (TREE_SIDE_EFFECTS (TREE_OPERAND (lhs
, 0)))
5763 lhs
= build (TREE_CODE (lhs
), TREE_TYPE (lhs
),
5764 stabilize_reference (TREE_OPERAND (lhs
, 0)),
5765 TREE_OPERAND (lhs
, 1));
5766 return build (COMPOUND_EXPR
, lhstype
,
5768 build_modify_expr (TREE_OPERAND (lhs
, 0),
5771 /* Handle (a, b) used as an "lvalue". */
5773 newrhs
= build_modify_expr (TREE_OPERAND (lhs
, 1),
5775 if (newrhs
== error_mark_node
)
5776 return error_mark_node
;
5777 return build (COMPOUND_EXPR
, lhstype
,
5778 TREE_OPERAND (lhs
, 0), newrhs
);
5781 newrhs
= build_modify_expr (TREE_OPERAND (lhs
, 0), modifycode
, rhs
);
5782 if (newrhs
== error_mark_node
)
5783 return error_mark_node
;
5784 return build (COMPOUND_EXPR
, lhstype
, lhs
, newrhs
);
5786 /* Handle (a ? b : c) used as an "lvalue". */
5788 rhs
= save_expr (rhs
);
5790 /* Produce (a ? (b = rhs) : (c = rhs))
5791 except that the RHS goes through a save-expr
5792 so the code to compute it is only emitted once. */
5794 = build_conditional_expr (TREE_OPERAND (lhs
, 0),
5795 build_modify_expr (cp_convert (TREE_TYPE (lhs
), TREE_OPERAND (lhs
, 1)),
5797 build_modify_expr (cp_convert (TREE_TYPE (lhs
), TREE_OPERAND (lhs
, 2)),
5799 if (cond
== error_mark_node
)
5801 /* Make sure the code to compute the rhs comes out
5802 before the split. */
5803 return build (COMPOUND_EXPR
, TREE_TYPE (lhs
),
5804 /* Case to void to suppress warning
5805 from warn_if_unused_value. */
5806 cp_convert (void_type_node
, rhs
), cond
);
5813 if (TREE_CODE (lhs
) == OFFSET_REF
)
5815 if (TREE_OPERAND (lhs
, 0) == NULL_TREE
)
5817 /* Static class member? */
5818 tree member
= TREE_OPERAND (lhs
, 1);
5819 if (TREE_CODE (member
) == VAR_DECL
)
5823 compiler_error ("invalid static class member");
5824 return error_mark_node
;
5828 lhs
= resolve_offset_ref (lhs
);
5830 olhstype
= lhstype
= TREE_TYPE (lhs
);
5833 if (TREE_CODE (lhstype
) == REFERENCE_TYPE
5834 && modifycode
!= INIT_EXPR
)
5836 lhs
= convert_from_reference (lhs
);
5837 olhstype
= lhstype
= TREE_TYPE (lhs
);
5840 /* If a binary op has been requested, combine the old LHS value with the RHS
5841 producing the value we should actually store into the LHS. */
5843 if (modifycode
== INIT_EXPR
)
5845 if (! IS_AGGR_TYPE (lhstype
))
5846 /* Do the default thing */;
5849 result
= build_method_call (lhs
, ctor_identifier
,
5850 build_expr_list (NULL_TREE
, rhs
),
5851 TYPE_BINFO (lhstype
), LOOKUP_NORMAL
);
5852 if (result
== NULL_TREE
)
5853 return error_mark_node
;
5857 else if (modifycode
== NOP_EXPR
)
5859 /* `operator=' is not an inheritable operator. */
5860 if (! IS_AGGR_TYPE (lhstype
))
5861 /* Do the default thing */;
5864 result
= build_opfncall (MODIFY_EXPR
, LOOKUP_NORMAL
,
5865 lhs
, rhs
, make_node (NOP_EXPR
));
5866 if (result
== NULL_TREE
)
5867 return error_mark_node
;
5872 else if (PROMOTES_TO_AGGR_TYPE (lhstype
, REFERENCE_TYPE
))
5874 /* This case must convert to some sort of lvalue that
5875 can participate in an op= operation. */
5878 if (build_default_binary_type_conversion (modifycode
, &lhs_tmp
, &rhs_tmp
))
5880 lhs
= stabilize_reference (lhs_tmp
);
5881 /* Forget it was ever anything else. */
5882 olhstype
= lhstype
= TREE_TYPE (lhs
);
5883 newrhs
= build_binary_op (modifycode
, lhs
, rhs_tmp
, 1);
5887 cp_error ("no match for `%Q(%#T, %#T)'", modifycode
,
5888 TREE_TYPE (lhs
), TREE_TYPE (rhs
));
5889 return error_mark_node
;
5894 lhs
= stabilize_reference (lhs
);
5895 newrhs
= build_binary_op (modifycode
, lhs
, rhs
, 1);
5896 if (newrhs
== error_mark_node
)
5898 cp_error (" in evaluation of `%Q(%#T, %#T)'", modifycode
,
5899 TREE_TYPE (lhs
), TREE_TYPE (rhs
));
5900 return error_mark_node
;
5904 /* Handle a cast used as an "lvalue".
5905 We have already performed any binary operator using the value as cast.
5906 Now convert the result to the cast type of the lhs,
5907 and then true type of the lhs and store it there;
5908 then convert result back to the cast type to be the value
5909 of the assignment. */
5911 switch (TREE_CODE (lhs
))
5916 case FIX_TRUNC_EXPR
:
5917 case FIX_FLOOR_EXPR
:
5918 case FIX_ROUND_EXPR
:
5920 if (TREE_CODE (TREE_TYPE (newrhs
)) == ARRAY_TYPE
5921 || TREE_CODE (TREE_TYPE (newrhs
)) == FUNCTION_TYPE
5922 || TREE_CODE (TREE_TYPE (newrhs
)) == METHOD_TYPE
5923 || TREE_CODE (TREE_TYPE (newrhs
)) == OFFSET_TYPE
)
5924 newrhs
= default_conversion (newrhs
);
5926 tree inner_lhs
= TREE_OPERAND (lhs
, 0);
5929 /* WP 5.4.1: The result is an lvalue if T is a reference type,
5930 otherwise the result is an rvalue. */
5931 if (! lvalue_p (lhs
))
5932 pedwarn ("ANSI C++ forbids cast to non-reference type used as lvalue");
5934 result
= build_modify_expr (inner_lhs
, NOP_EXPR
,
5935 cp_convert (TREE_TYPE (inner_lhs
),
5936 cp_convert (lhstype
, newrhs
)));
5937 if (result
== error_mark_node
)
5939 return cp_convert (TREE_TYPE (lhs
), result
);
5946 /* Now we have handled acceptable kinds of LHS that are not truly lvalues.
5947 Reject anything strange now. */
5949 if (!lvalue_or_else (lhs
, "assignment"))
5950 return error_mark_node
;
5952 GNU_xref_assign (lhs
);
5954 /* Warn about storing in something that is `const'. */
5955 /* For C++, don't warn if this is initialization. */
5956 if (modifycode
!= INIT_EXPR
5957 /* For assignment to `const' signature pointer/reference fields,
5958 don't warn either, we already printed a better message before. */
5959 && ! (TREE_CODE (lhs
) == COMPONENT_REF
5960 && (IS_SIGNATURE_POINTER (TREE_TYPE (TREE_OPERAND (lhs
, 0)))
5961 || IS_SIGNATURE_REFERENCE (TREE_TYPE (TREE_OPERAND (lhs
, 0)))))
5962 && (TREE_READONLY (lhs
) || TYPE_READONLY (lhstype
)
5963 || ((TREE_CODE (lhstype
) == RECORD_TYPE
5964 || TREE_CODE (lhstype
) == UNION_TYPE
)
5965 && C_TYPE_FIELDS_READONLY (lhstype
))
5966 || (TREE_CODE (lhstype
) == REFERENCE_TYPE
5967 && TYPE_READONLY (TREE_TYPE (lhstype
)))))
5968 readonly_error (lhs
, "assignment", 0);
5970 /* If storing into a structure or union member,
5971 it has probably been given type `int'.
5972 Compute the type that would go with
5973 the actual amount of storage the member occupies. */
5975 if (TREE_CODE (lhs
) == COMPONENT_REF
5976 && (TREE_CODE (lhstype
) == INTEGER_TYPE
5977 || TREE_CODE (lhstype
) == REAL_TYPE
5978 || TREE_CODE (lhstype
) == ENUMERAL_TYPE
))
5980 lhstype
= TREE_TYPE (get_unwidened (lhs
, 0));
5982 /* If storing in a field that is in actuality a short or narrower
5983 than one, we must store in the field in its actual type. */
5985 if (lhstype
!= TREE_TYPE (lhs
))
5987 lhs
= copy_node (lhs
);
5988 TREE_TYPE (lhs
) = lhstype
;
5992 /* check to see if there is an assignment to `this' */
5993 if (lhs
== current_class_ptr
)
5995 if (flag_this_is_variable
> 0
5996 && DECL_NAME (current_function_decl
) != NULL_TREE
5997 && (DECL_NAME (current_function_decl
)
5998 != constructor_name (current_class_type
)))
5999 warning ("assignment to `this' not in constructor or destructor");
6000 current_function_just_assigned_this
= 1;
6003 /* The TREE_TYPE of RHS may be TYPE_UNKNOWN. This can happen
6004 when the type of RHS is not yet known, i.e. its type
6005 is inherited from LHS. */
6006 rhs
= require_instantiated_type (lhstype
, newrhs
, error_mark_node
);
6007 if (rhs
== error_mark_node
)
6008 return error_mark_node
;
6011 if (modifycode
!= INIT_EXPR
)
6013 /* Make modifycode now either a NOP_EXPR or an INIT_EXPR. */
6014 modifycode
= NOP_EXPR
;
6015 /* Reference-bashing */
6016 if (TREE_CODE (lhstype
) == REFERENCE_TYPE
)
6018 tree tmp
= convert_from_reference (lhs
);
6019 lhstype
= TREE_TYPE (tmp
);
6020 if (TYPE_SIZE (lhstype
) == 0)
6022 incomplete_type_error (lhs
, lhstype
);
6023 return error_mark_node
;
6028 if (TREE_CODE (TREE_TYPE (newrhs
)) == REFERENCE_TYPE
)
6030 tree tmp
= convert_from_reference (newrhs
);
6031 if (TYPE_SIZE (TREE_TYPE (tmp
)) == 0)
6033 incomplete_type_error (newrhs
, TREE_TYPE (tmp
));
6034 return error_mark_node
;
6040 if (TREE_SIDE_EFFECTS (lhs
))
6041 lhs
= stabilize_reference (lhs
);
6042 if (TREE_SIDE_EFFECTS (newrhs
))
6043 newrhs
= stabilize_reference (newrhs
);
6045 /* Convert new value to destination type. */
6047 if (TREE_CODE (lhstype
) == ARRAY_TYPE
)
6051 if (! comptypes (lhstype
, TREE_TYPE (rhs
), 0))
6053 cp_error ("incompatible types in assignment of `%T' to `%T'",
6054 TREE_TYPE (rhs
), lhstype
);
6055 return error_mark_node
;
6058 /* Allow array assignment in compiler-generated code. */
6059 if (pedantic
&& ! DECL_ARTIFICIAL (current_function_decl
))
6060 pedwarn ("ANSI C++ forbids assignment of arrays");
6062 /* Have to wrap this in RTL_EXPR for two cases:
6063 in base or member initialization and if we
6064 are a branch of a ?: operator. Since we
6065 can't easily know the latter, just do it always. */
6067 result
= make_node (RTL_EXPR
);
6069 TREE_TYPE (result
) = void_type_node
;
6070 do_pending_stack_adjust ();
6071 start_sequence_for_rtl_expr (result
);
6073 /* As a matter of principle, `start_sequence' should do this. */
6076 from_array
= TREE_CODE (TREE_TYPE (newrhs
)) == ARRAY_TYPE
6077 ? 1 + (modifycode
!= INIT_EXPR
): 0;
6078 expand_vec_init (lhs
, lhs
, array_type_nelts (lhstype
), newrhs
,
6081 do_pending_stack_adjust ();
6083 TREE_SIDE_EFFECTS (result
) = 1;
6084 RTL_EXPR_SEQUENCE (result
) = get_insns ();
6085 RTL_EXPR_RTL (result
) = const0_rtx
;
6090 if (modifycode
== INIT_EXPR
)
6092 newrhs
= convert_for_initialization (lhs
, lhstype
, newrhs
, LOOKUP_NORMAL
,
6093 "assignment", NULL_TREE
, 0);
6094 if (lhs
== DECL_RESULT (current_function_decl
))
6096 if (DECL_INITIAL (lhs
))
6097 warning ("return value from function receives multiple initializations");
6098 DECL_INITIAL (lhs
) = newrhs
;
6103 /* Avoid warnings on enum bit fields. */
6104 if (TREE_CODE (olhstype
) == ENUMERAL_TYPE
6105 && TREE_CODE (lhstype
) == INTEGER_TYPE
)
6107 newrhs
= convert_for_assignment (olhstype
, newrhs
, "assignment",
6109 newrhs
= convert_force (lhstype
, newrhs
, 0);
6112 newrhs
= convert_for_assignment (lhstype
, newrhs
, "assignment",
6114 if (TREE_CODE (newrhs
) == CALL_EXPR
6115 && TYPE_NEEDS_CONSTRUCTING (lhstype
))
6116 newrhs
= build_cplus_new (lhstype
, newrhs
);
6118 /* Can't initialize directly from a TARGET_EXPR, since that would
6119 cause the lhs to be constructed twice, and possibly result in
6120 accidental self-initialization. So we force the TARGET_EXPR to be
6121 expanded without a target. */
6122 if (TREE_CODE (newrhs
) == TARGET_EXPR
)
6123 newrhs
= build (COMPOUND_EXPR
, TREE_TYPE (newrhs
), newrhs
,
6124 TREE_OPERAND (newrhs
, 0));
6127 if (newrhs
== error_mark_node
)
6128 return error_mark_node
;
6130 if (TREE_CODE (newrhs
) == COND_EXPR
)
6133 tree cond
= TREE_OPERAND (newrhs
, 0);
6135 if (TREE_SIDE_EFFECTS (lhs
))
6136 cond
= build_compound_expr (tree_cons
6138 build_expr_list (NULL_TREE
, cond
)));
6140 /* Cannot have two identical lhs on this one tree (result) as preexpand
6141 calls will rip them out and fill in RTL for them, but when the
6142 rtl is generated, the calls will only be in the first side of the
6143 condition, not on both, or before the conditional jump! (mrs) */
6144 lhs1
= break_out_calls (lhs
);
6147 /* If there's no change, the COND_EXPR behaves like any other rhs. */
6148 result
= build (modifycode
== NOP_EXPR
? MODIFY_EXPR
: INIT_EXPR
,
6149 lhstype
, lhs
, newrhs
);
6152 tree result_type
= TREE_TYPE (newrhs
);
6153 /* We have to convert each arm to the proper type because the
6154 types may have been munged by constant folding. */
6156 = build (COND_EXPR
, result_type
, cond
,
6157 build_modify_expr (lhs
, modifycode
,
6158 cp_convert (result_type
,
6159 TREE_OPERAND (newrhs
, 1))),
6160 build_modify_expr (lhs1
, modifycode
,
6161 cp_convert (result_type
,
6162 TREE_OPERAND (newrhs
, 2))));
6166 result
= build (modifycode
== NOP_EXPR
? MODIFY_EXPR
: INIT_EXPR
,
6167 lhstype
, lhs
, newrhs
);
6169 TREE_SIDE_EFFECTS (result
) = 1;
6171 /* If we got the LHS in a different type for storing in,
6172 convert the result back to the nominal type of LHS
6173 so that the value we return always has the same type
6174 as the LHS argument. */
6176 if (olhstype
== TREE_TYPE (result
))
6178 /* Avoid warnings converting integral types back into enums
6179 for enum bit fields. */
6180 if (TREE_CODE (TREE_TYPE (result
)) == INTEGER_TYPE
6181 && TREE_CODE (olhstype
) == ENUMERAL_TYPE
)
6183 result
= build (COMPOUND_EXPR
, olhstype
, result
, olhs
);
6184 TREE_NO_UNUSED_WARNING (result
) = 1;
6187 return convert_for_assignment (olhstype
, result
, "assignment",
6192 build_x_modify_expr (lhs
, modifycode
, rhs
)
6194 enum tree_code modifycode
;
6197 if (processing_template_decl
)
6198 return build_min_nt (MODOP_EXPR
, lhs
,
6199 build_min_nt (modifycode
, NULL_TREE
, NULL_TREE
), rhs
);
6201 if (modifycode
!= NOP_EXPR
)
6203 tree rval
= build_opfncall (MODIFY_EXPR
, LOOKUP_NORMAL
, lhs
, rhs
,
6204 make_node (modifycode
));
6208 return build_modify_expr (lhs
, modifycode
, rhs
);
6211 /* Return 0 if EXP is not a valid lvalue in this language
6212 even though `lvalue_or_else' would accept it. */
6215 language_lvalue_valid (exp
)
6221 /* Get difference in deltas for different pointer to member function
6222 types. Return integer_zero_node, if FROM cannot be converted to a
6223 TO type. If FORCE is true, then allow reverse conversions as well. */
6226 get_delta_difference (from
, to
, force
)
6230 tree delta
= integer_zero_node
;
6236 /* Should get_base_distance here, so we can check if any thing along the
6237 path is virtual, and we need to make sure we stay
6238 inside the real binfos when going through virtual bases.
6239 Maybe we should replace virtual bases with
6240 binfo_member (...CLASSTYPE_VBASECLASSES...)... (mrs) */
6241 binfo
= get_binfo (from
, to
, 1);
6242 if (binfo
== error_mark_node
)
6244 error (" in pointer to member function conversion");
6251 error_not_base_type (from
, to
);
6252 error (" in pointer to member conversion");
6255 binfo
= get_binfo (to
, from
, 1);
6256 if (binfo
== error_mark_node
)
6259 error (" in pointer to member conversion");
6265 cp_error ("cannot convert pointer to member of type %T to unrelated pointer to member of type %T", from
, to
);
6268 if (TREE_VIA_VIRTUAL (binfo
))
6270 binfo
= binfo_member (BINFO_TYPE (binfo
),
6271 CLASSTYPE_VBASECLASSES (from
));
6272 warning ("pointer to member conversion to virtual base class will only work if you are very careful");
6274 delta
= BINFO_OFFSET (binfo
);
6275 delta
= cp_convert (ptrdiff_type_node
, delta
);
6277 return build_binary_op (MINUS_EXPR
,
6281 if (TREE_VIA_VIRTUAL (binfo
))
6283 warning ("pointer to member conversion from virtual base class will only work if you are very careful");
6285 return BINFO_OFFSET (binfo
);
6289 build_ptrmemfunc1 (type
, delta
, idx
, pfn
, delta2
)
6290 tree type
, delta
, idx
, pfn
, delta2
;
6295 /* This is the old way we did it. We want to avoid calling
6296 digest_init, so that it can give an error if we use { } when
6297 initializing a pointer to member function. */
6301 u
= build_nt (CONSTRUCTOR
, NULL_TREE
,
6302 expr_tree_cons (pfn_identifier
, pfn
, NULL_TREE
));
6306 u
= build_nt (CONSTRUCTOR
, NULL_TREE
,
6307 expr_tree_cons (delta2_identifier
, delta2
, NULL_TREE
));
6310 u
= build_nt (CONSTRUCTOR
, NULL_TREE
,
6311 expr_tree_cons (NULL_TREE
, delta
,
6312 expr_tree_cons (NULL_TREE
, idx
,
6313 expr_tree_cons (NULL_TREE
, u
, NULL_TREE
))));
6315 return digest_init (type
, u
, (tree
*)0);
6317 tree delta_field
, idx_field
, pfn_or_delta2_field
, pfn_field
, delta2_field
;
6319 int allconstant
, allsimple
;
6321 delta_field
= TYPE_FIELDS (type
);
6322 idx_field
= TREE_CHAIN (delta_field
);
6323 pfn_or_delta2_field
= TREE_CHAIN (idx_field
);
6324 subtype
= TREE_TYPE (pfn_or_delta2_field
);
6325 pfn_field
= TYPE_FIELDS (subtype
);
6326 delta2_field
= TREE_CHAIN (pfn_field
);
6330 allconstant
= TREE_CONSTANT (pfn
);
6331 allsimple
= !! initializer_constant_valid_p (pfn
, TREE_TYPE (pfn
));
6332 u
= expr_tree_cons (pfn_field
, pfn
, NULL_TREE
);
6336 delta2
= convert_and_check (delta_type_node
, delta2
);
6337 allconstant
= TREE_CONSTANT (delta2
);
6338 allsimple
= !! initializer_constant_valid_p (delta2
, TREE_TYPE (delta2
));
6339 u
= expr_tree_cons (delta2_field
, delta2
, NULL_TREE
);
6342 delta
= convert_and_check (delta_type_node
, delta
);
6343 idx
= convert_and_check (delta_type_node
, idx
);
6345 allconstant
= allconstant
&& TREE_CONSTANT (delta
) && TREE_CONSTANT (idx
);
6346 allsimple
= allsimple
6347 && initializer_constant_valid_p (delta
, TREE_TYPE (delta
))
6348 && initializer_constant_valid_p (idx
, TREE_TYPE (idx
));
6350 u
= build (CONSTRUCTOR
, subtype
, NULL_TREE
, u
);
6351 u
= expr_tree_cons (delta_field
, delta
,
6352 expr_tree_cons (idx_field
, idx
,
6353 expr_tree_cons (pfn_or_delta2_field
, u
, NULL_TREE
)));
6354 u
= build (CONSTRUCTOR
, type
, NULL_TREE
, u
);
6355 TREE_CONSTANT (u
) = allconstant
;
6356 TREE_STATIC (u
) = allconstant
&& allsimple
;
6361 /* Build a constructor for a pointer to member function. It can be
6362 used to initialize global variables, local variable, or used
6363 as a value in expressions. TYPE is the POINTER to METHOD_TYPE we
6366 If FORCE is non-zero, then force this conversion, even if
6367 we would rather not do it. Usually set when using an explicit
6370 Return error_mark_node, if something goes wrong. */
6373 build_ptrmemfunc (type
, pfn
, force
)
6377 tree idx
= integer_zero_node
;
6378 tree delta
= integer_zero_node
;
6379 tree delta2
= integer_zero_node
;
6381 tree npfn
= NULL_TREE
;
6383 /* Handle multiple conversions of pointer to member functions. */
6384 if (TYPE_PTRMEMFUNC_P (TREE_TYPE (pfn
)))
6386 tree ndelta
, ndelta2
;
6389 /* Is is already the right type? */
6390 if (type
== TYPE_PTRMEMFUNC_FN_TYPE (TREE_TYPE (pfn
)))
6393 ndelta
= cp_convert (ptrdiff_type_node
, build_component_ref (pfn
, delta_identifier
, NULL_TREE
, 0));
6394 ndelta2
= cp_convert (ptrdiff_type_node
, DELTA2_FROM_PTRMEMFUNC (pfn
));
6395 idx
= build_component_ref (pfn
, index_identifier
, NULL_TREE
, 0);
6397 n
= get_delta_difference (TYPE_METHOD_BASETYPE (TREE_TYPE (TYPE_PTRMEMFUNC_FN_TYPE (TREE_TYPE (pfn
)))),
6398 TYPE_METHOD_BASETYPE (TREE_TYPE (type
)),
6401 delta
= build_binary_op (PLUS_EXPR
, ndelta
, n
, 1);
6402 delta2
= build_binary_op (PLUS_EXPR
, ndelta2
, n
, 1);
6403 e1
= fold (build (GT_EXPR
, boolean_type_node
, idx
, integer_zero_node
));
6405 e2
= build_ptrmemfunc1 (TYPE_GET_PTRMEMFUNC_TYPE (type
), delta
, idx
,
6408 pfn
= PFN_FROM_PTRMEMFUNC (pfn
);
6409 npfn
= build1 (NOP_EXPR
, type
, pfn
);
6410 TREE_CONSTANT (npfn
) = TREE_CONSTANT (pfn
);
6412 e3
= build_ptrmemfunc1 (TYPE_GET_PTRMEMFUNC_TYPE (type
), delta
, idx
, npfn
,
6414 return build_conditional_expr (e1
, e2
, e3
);
6417 /* Handle null pointer to member function conversions. */
6418 if (integer_zerop (pfn
))
6420 pfn
= build_c_cast (type
, integer_zero_node
);
6421 return build_ptrmemfunc1 (TYPE_GET_PTRMEMFUNC_TYPE (type
),
6422 integer_zero_node
, integer_zero_node
,
6426 if (TREE_CODE (pfn
) == TREE_LIST
6427 || (TREE_CODE (pfn
) == ADDR_EXPR
6428 && TREE_CODE (TREE_OPERAND (pfn
, 0)) == TREE_LIST
))
6429 return instantiate_type (type
, pfn
, 1);
6431 /* Allow pointer to member conversions here. */
6432 delta
= get_delta_difference (TYPE_METHOD_BASETYPE (TREE_TYPE (TREE_TYPE (pfn
))),
6433 TYPE_METHOD_BASETYPE (TREE_TYPE (type
)),
6435 delta2
= build_binary_op (PLUS_EXPR
, delta2
, delta
, 1);
6438 /* We need to check the argument types to see if they are compatible
6439 (any const or volatile violations. */
6440 something like
this:
6441 comptype (TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (type
))),
6442 TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (TREE_TYPE (pfn
)))), ?);
6445 if (TREE_CODE (TREE_OPERAND (pfn
, 0)) != FUNCTION_DECL
)
6446 warning ("assuming pointer to member function is non-virtual");
6448 if (TREE_CODE (TREE_OPERAND (pfn
, 0)) == FUNCTION_DECL
6449 && DECL_VINDEX (TREE_OPERAND (pfn
, 0)))
6451 /* Find the offset to the vfield pointer in the object. */
6452 vfield_offset
= get_binfo (DECL_CONTEXT (TREE_OPERAND (pfn
, 0)),
6453 DECL_CLASS_CONTEXT (TREE_OPERAND (pfn
, 0)),
6455 vfield_offset
= get_vfield_offset (vfield_offset
);
6456 delta2
= size_binop (PLUS_EXPR
, vfield_offset
, delta2
);
6458 /* Map everything down one to make room for the null pointer to member. */
6459 idx
= size_binop (PLUS_EXPR
,
6460 DECL_VINDEX (TREE_OPERAND (pfn
, 0)),
6465 idx
= size_binop (MINUS_EXPR
, integer_zero_node
, integer_one_node
);
6467 if (type
== TREE_TYPE (pfn
))
6473 npfn
= build1 (NOP_EXPR
, type
, pfn
);
6474 TREE_CONSTANT (npfn
) = TREE_CONSTANT (pfn
);
6478 return build_ptrmemfunc1 (TYPE_GET_PTRMEMFUNC_TYPE (type
), delta
, idx
, npfn
, delta2
);
6481 /* Convert value RHS to type TYPE as preparation for an assignment
6482 to an lvalue of type TYPE.
6483 The real work of conversion is done by `convert'.
6484 The purpose of this function is to generate error messages
6485 for assignments that are not allowed in C.
6486 ERRTYPE is a string to use in error messages:
6487 "assignment", "return", etc.
6489 C++: attempts to allow `convert' to find conversions involving
6490 implicit type conversion between aggregate and scalar types
6491 as per 8.5.6 of C++ manual. Does not randomly dereference
6492 pointers to aggregates! */
6495 convert_for_assignment (type
, rhs
, errtype
, fndecl
, parmnum
)
6501 register enum tree_code codel
= TREE_CODE (type
);
6502 register tree rhstype
;
6503 register enum tree_code coder
= TREE_CODE (TREE_TYPE (rhs
));
6505 if (coder
== UNKNOWN_TYPE
)
6506 rhs
= instantiate_type (type
, rhs
, 1);
6508 if (coder
== ERROR_MARK
)
6509 return error_mark_node
;
6511 if (codel
== OFFSET_TYPE
)
6513 type
= TREE_TYPE (type
);
6514 codel
= TREE_CODE (type
);
6517 /* Strip NON_LVALUE_EXPRs since we aren't using as an lvalue. */
6518 if (TREE_CODE (rhs
) == NON_LVALUE_EXPR
)
6519 rhs
= TREE_OPERAND (rhs
, 0);
6521 if (rhs
== error_mark_node
)
6522 return error_mark_node
;
6524 if (TREE_VALUE (rhs
) == error_mark_node
)
6525 return error_mark_node
;
6527 if (TREE_CODE (TREE_TYPE (rhs
)) == OFFSET_TYPE
)
6529 rhs
= resolve_offset_ref (rhs
);
6530 if (rhs
== error_mark_node
)
6531 return error_mark_node
;
6532 rhstype
= TREE_TYPE (rhs
);
6533 coder
= TREE_CODE (rhstype
);
6536 if (TREE_CODE (TREE_TYPE (rhs
)) == ARRAY_TYPE
6537 || TREE_CODE (TREE_TYPE (rhs
)) == FUNCTION_TYPE
6538 || TREE_CODE (TREE_TYPE (rhs
)) == METHOD_TYPE
)
6539 rhs
= default_conversion (rhs
);
6540 else if (TREE_CODE (TREE_TYPE (rhs
)) == REFERENCE_TYPE
)
6541 rhs
= convert_from_reference (rhs
);
6543 rhstype
= TREE_TYPE (rhs
);
6544 coder
= TREE_CODE (rhstype
);
6546 /* This should no longer change types on us. */
6547 if (TREE_CODE (rhs
) == CONST_DECL
)
6548 rhs
= DECL_INITIAL (rhs
);
6549 else if (TREE_READONLY_DECL_P (rhs
))
6550 rhs
= decl_constant_value (rhs
);
6552 if (type
== rhstype
)
6554 overflow_warning (rhs
);
6558 if (coder
== VOID_TYPE
)
6560 error ("void value not ignored as it ought to be");
6561 return error_mark_node
;
6563 /* Arithmetic types all interconvert. */
6564 if ((codel
== INTEGER_TYPE
|| codel
== REAL_TYPE
|| codel
== BOOLEAN_TYPE
6565 || codel
== COMPLEX_TYPE
)
6566 && (coder
== INTEGER_TYPE
|| coder
== REAL_TYPE
|| coder
== BOOLEAN_TYPE
6567 || coder
== COMPLEX_TYPE
))
6569 /* But we should warn if assigning REAL_TYPE to INTEGER_TYPE. */
6570 if (coder
== REAL_TYPE
&& codel
== INTEGER_TYPE
)
6573 cp_warning ("`%T' used for argument %P of `%D'",
6574 rhstype
, parmnum
, fndecl
);
6576 cp_warning ("%s to `%T' from `%T'", errtype
, type
, rhstype
);
6578 /* And we should warn if assigning a negative value to
6579 an unsigned variable. */
6580 else if (TREE_UNSIGNED (type
) && codel
!= BOOLEAN_TYPE
)
6582 if (TREE_CODE (rhs
) == INTEGER_CST
6583 && TREE_NEGATED_INT (rhs
))
6586 cp_warning ("negative value `%E' passed as argument %P of `%D'",
6587 rhs
, parmnum
, fndecl
);
6589 cp_warning ("%s of negative value `%E' to `%T'",
6590 errtype
, rhs
, type
);
6592 overflow_warning (rhs
);
6593 if (TREE_CONSTANT (rhs
))
6597 return convert_and_check (type
, rhs
);
6599 /* Conversions involving enums. */
6600 else if ((codel
== ENUMERAL_TYPE
6601 && (INTEGRAL_CODE_P (coder
) || coder
== REAL_TYPE
))
6602 || (coder
== ENUMERAL_TYPE
6603 && (INTEGRAL_CODE_P (codel
) || codel
== REAL_TYPE
)))
6605 return ocp_convert (type
, rhs
, CONV_IMPLICIT
, LOOKUP_NORMAL
);
6607 /* Conversions among pointers */
6608 else if (codel
== POINTER_TYPE
6609 && (coder
== POINTER_TYPE
6610 || (coder
== RECORD_TYPE
6611 && (IS_SIGNATURE_POINTER (rhstype
)
6612 || IS_SIGNATURE_REFERENCE (rhstype
)))))
6614 register tree ttl
= TREE_TYPE (type
);
6618 if (coder
== RECORD_TYPE
)
6620 rhs
= build_optr_ref (rhs
);
6621 rhstype
= TREE_TYPE (rhs
);
6623 ttr
= TREE_TYPE (rhstype
);
6625 /* If both pointers are of aggregate type, then we
6626 can give better error messages, and save some work
6628 if (TREE_CODE (ttl
) == RECORD_TYPE
&& TREE_CODE (ttr
) == RECORD_TYPE
)
6632 if (TYPE_MAIN_VARIANT (ttl
) == TYPE_MAIN_VARIANT (ttr
)
6633 || type
== class_star_type_node
6634 || rhstype
== class_star_type_node
)
6635 binfo
= TYPE_BINFO (ttl
);
6637 binfo
= get_binfo (ttl
, ttr
, 1);
6639 if (binfo
== error_mark_node
)
6640 return error_mark_node
;
6642 return error_not_base_type (ttl
, ttr
);
6644 if (! TYPE_READONLY (ttl
) && TYPE_READONLY (ttr
))
6647 cp_pedwarn ("passing `%T' as argument %P of `%D' discards const",
6648 rhstype
, parmnum
, fndecl
);
6650 cp_pedwarn ("%s to `%T' from `%T' discards const",
6651 errtype
, type
, rhstype
);
6653 if (! TYPE_VOLATILE (ttl
) && TYPE_VOLATILE (ttr
))
6656 cp_pedwarn ("passing `%T' as argument %P of `%D' discards volatile",
6657 rhstype
, parmnum
, fndecl
);
6659 cp_pedwarn ("%s to `%T' from `%T' discards volatile",
6660 errtype
, type
, rhstype
);
6664 /* Any non-function converts to a [const][volatile] void *
6665 and vice versa; otherwise, targets must be the same.
6666 Meanwhile, the lhs target must have all the qualifiers of the rhs. */
6667 else if (TYPE_MAIN_VARIANT (ttl
) == void_type_node
6668 || TYPE_MAIN_VARIANT (ttr
) == void_type_node
6669 || (ctt
= comp_target_types (type
, rhstype
, 1))
6670 || (unsigned_type (TYPE_MAIN_VARIANT (ttl
))
6671 == unsigned_type (TYPE_MAIN_VARIANT (ttr
))))
6673 /* ARM $4.8, commentary on p39. */
6674 if (TYPE_MAIN_VARIANT (ttl
) == void_type_node
6675 && TREE_CODE (ttr
) == OFFSET_TYPE
)
6677 cp_error ("no standard conversion from `%T' to `void *'", ttr
);
6678 return error_mark_node
;
6681 if (ctt
< 0 && TYPE_MAIN_VARIANT (ttl
) != TYPE_MAIN_VARIANT (ttr
))
6682 cp_pedwarn ("converting `%T' to `%T' is a contravariance violation",
6685 if (TYPE_MAIN_VARIANT (ttl
) != void_type_node
6686 && TYPE_MAIN_VARIANT (ttr
) == void_type_node
6687 && ! null_ptr_cst_p (rhs
))
6689 if (coder
== RECORD_TYPE
)
6690 cp_pedwarn ("implicit conversion of signature pointer to type `%T'",
6693 pedwarn ("ANSI C++ forbids implicit conversion from `void *' in %s",
6696 /* Const and volatile mean something different for function types,
6697 so the usual warnings are not appropriate. */
6698 else if ((TREE_CODE (ttr
) != FUNCTION_TYPE
&& TREE_CODE (ttr
) != METHOD_TYPE
)
6699 || (TREE_CODE (ttl
) != FUNCTION_TYPE
&& TREE_CODE (ttl
) != METHOD_TYPE
))
6701 if (TREE_CODE (ttl
) == OFFSET_TYPE
6702 && binfo_member (TYPE_OFFSET_BASETYPE (ttr
),
6703 CLASSTYPE_VBASECLASSES (TYPE_OFFSET_BASETYPE (ttl
))))
6705 sorry ("%s between pointer to members converting across virtual baseclasses", errtype
);
6706 return error_mark_node
;
6708 else if (! TYPE_READONLY (ttl
) && TYPE_READONLY (ttr
))
6711 cp_pedwarn ("passing `%T' as argument %P of `%D' discards const",
6712 rhstype
, parmnum
, fndecl
);
6714 cp_pedwarn ("%s to `%T' from `%T' discards const",
6715 errtype
, type
, rhstype
);
6717 else if (! TYPE_VOLATILE (ttl
) && TYPE_VOLATILE (ttr
))
6720 cp_pedwarn ("passing `%T' as argument %P of `%D' discards volatile",
6721 rhstype
, parmnum
, fndecl
);
6723 cp_pedwarn ("%s to `%T' from `%T' discards volatile",
6724 errtype
, type
, rhstype
);
6726 else if (TREE_CODE (ttl
) == TREE_CODE (ttr
)
6727 && ! comp_target_types (type
, rhstype
, 1))
6730 cp_pedwarn ("passing `%T' as argument %P of `%D' changes signedness",
6731 rhstype
, parmnum
, fndecl
);
6733 cp_pedwarn ("%s to `%T' from `%T' changes signedness",
6734 errtype
, type
, rhstype
);
6740 int add_quals
= 0, const_parity
= 0, volatile_parity
= 0;
6742 int unsigned_parity
;
6745 /* This code is basically a duplicate of comp_ptr_ttypes_real. */
6746 for (; ; ttl
= TREE_TYPE (ttl
), ttr
= TREE_TYPE (ttr
))
6749 const_parity
|= (TYPE_READONLY (ttl
) < TYPE_READONLY (ttr
));
6750 volatile_parity
|= (TYPE_VOLATILE (ttl
) < TYPE_VOLATILE (ttr
));
6753 && (TYPE_READONLY (ttl
) > TYPE_READONLY (ttr
)
6754 || TYPE_VOLATILE (ttl
) > TYPE_VOLATILE (ttr
)))
6756 left_const
&= TYPE_READONLY (ttl
);
6758 if (TREE_CODE (ttl
) != POINTER_TYPE
6759 || TREE_CODE (ttr
) != POINTER_TYPE
)
6762 unsigned_parity
= TREE_UNSIGNED (ttl
) - TREE_UNSIGNED (ttr
);
6763 if (unsigned_parity
)
6765 if (TREE_UNSIGNED (ttl
))
6766 ttr
= unsigned_type (ttr
);
6768 ttl
= unsigned_type (ttl
);
6771 if (comp_target_types (ttl
, ttr
, nptrs
) > 0)
6776 cp_pedwarn ("passing `%T' as argument %P of `%D' adds cv-quals without intervening `const'",
6777 rhstype
, parmnum
, fndecl
);
6779 cp_pedwarn ("%s to `%T' from `%T' adds cv-quals without intervening `const'",
6780 errtype
, type
, rhstype
);
6785 cp_pedwarn ("passing `%T' as argument %P of `%D' discards const",
6786 rhstype
, parmnum
, fndecl
);
6788 cp_pedwarn ("%s to `%T' from `%T' discards const",
6789 errtype
, type
, rhstype
);
6791 if (volatile_parity
)
6794 cp_pedwarn ("passing `%T' as argument %P of `%D' discards volatile",
6795 rhstype
, parmnum
, fndecl
);
6797 cp_pedwarn ("%s to `%T' from `%T' discards volatile",
6798 errtype
, type
, rhstype
);
6800 if (unsigned_parity
> 0)
6803 cp_pedwarn ("passing `%T' as argument %P of `%D' changes signed to unsigned",
6804 rhstype
, parmnum
, fndecl
);
6806 cp_pedwarn ("%s to `%T' from `%T' changes signed to unsigned",
6807 errtype
, type
, rhstype
);
6809 else if (unsigned_parity
< 0)
6812 cp_pedwarn ("passing `%T' as argument %P of `%D' changes unsigned to signed",
6813 rhstype
, parmnum
, fndecl
);
6815 cp_pedwarn ("%s to `%T' from `%T' changes unsigned to signed",
6816 errtype
, type
, rhstype
);
6819 /* C++ is not so friendly about converting function and
6820 member function pointers as C. Emit warnings here. */
6821 if (TREE_CODE (ttl
) == FUNCTION_TYPE
6822 || TREE_CODE (ttl
) == METHOD_TYPE
)
6823 if (! comptypes (ttl
, ttr
, 0))
6825 warning ("conflicting function types in %s:", errtype
);
6826 cp_warning ("\t`%T' != `%T'", type
, rhstype
);
6832 cp_error ("passing `%T' as argument %P of `%D'",
6833 rhstype
, parmnum
, fndecl
);
6835 cp_error ("%s to `%T' from `%T'", errtype
, type
, rhstype
);
6836 return error_mark_node
;
6839 return cp_convert (type
, rhs
);
6841 else if (codel
== POINTER_TYPE
&& coder
== INTEGER_TYPE
)
6843 /* An explicit constant 0 can convert to a pointer,
6844 but not a 0 that results from casting or folding. */
6845 if (! (TREE_CODE (rhs
) == INTEGER_CST
&& integer_zerop (rhs
)))
6848 cp_pedwarn ("passing `%T' to argument %P of `%D' lacks a cast",
6849 rhstype
, parmnum
, fndecl
);
6851 cp_pedwarn ("%s to `%T' from `%T' lacks a cast",
6852 errtype
, type
, rhstype
);
6854 return cp_convert (type
, rhs
);
6856 else if (codel
== INTEGER_TYPE
6857 && (coder
== POINTER_TYPE
6858 || (coder
== RECORD_TYPE
6859 && (IS_SIGNATURE_POINTER (rhstype
)
6860 || TYPE_PTRMEMFUNC_FLAG (rhstype
)
6861 || IS_SIGNATURE_REFERENCE (rhstype
)))))
6864 cp_pedwarn ("passing `%T' to argument %P of `%D' lacks a cast",
6865 rhstype
, parmnum
, fndecl
);
6867 cp_pedwarn ("%s to `%T' from `%T' lacks a cast",
6868 errtype
, type
, rhstype
);
6869 return cp_convert (type
, rhs
);
6871 else if (codel
== BOOLEAN_TYPE
6872 && (coder
== POINTER_TYPE
6873 || (coder
== RECORD_TYPE
6874 && (IS_SIGNATURE_POINTER (rhstype
)
6875 || TYPE_PTRMEMFUNC_FLAG (rhstype
)
6876 || IS_SIGNATURE_REFERENCE (rhstype
)))))
6877 return cp_convert (type
, rhs
);
6880 else if (((coder
== POINTER_TYPE
6881 && TREE_CODE (TREE_TYPE (rhstype
)) == METHOD_TYPE
)
6882 || integer_zerop (rhs
)
6883 || TYPE_PTRMEMFUNC_P (rhstype
))
6884 && TYPE_PTRMEMFUNC_P (type
))
6886 tree ttl
= TYPE_PTRMEMFUNC_FN_TYPE (type
);
6887 tree ttr
= (TREE_CODE (rhstype
) == POINTER_TYPE
? rhstype
6888 : TYPE_PTRMEMFUNC_FN_TYPE (type
));
6889 int ctt
= comp_target_types (ttl
, ttr
, 1);
6892 cp_pedwarn ("converting `%T' to `%T' is a contravariance violation",
6895 cp_error ("%s to `%T' from `%T'", errtype
, ttl
, ttr
);
6897 /* compatible pointer to member functions. */
6898 return build_ptrmemfunc (ttl
, rhs
, 0);
6900 else if (codel
== ERROR_MARK
|| coder
== ERROR_MARK
)
6901 return error_mark_node
;
6903 /* This should no longer happen. References are initialized via
6904 `convert_for_initialization'. They should otherwise be
6905 bashed before coming here. */
6906 else if (codel
== REFERENCE_TYPE
)
6907 my_friendly_abort (317);
6908 else if (TYPE_MAIN_VARIANT (type
) == TYPE_MAIN_VARIANT (TREE_TYPE (rhs
)))
6910 tree nrhs
= build1 (NOP_EXPR
, type
, rhs
);
6911 TREE_CONSTANT (nrhs
) = TREE_CONSTANT (rhs
);
6914 else if (TYPE_HAS_CONSTRUCTOR (type
) || IS_AGGR_TYPE (TREE_TYPE (rhs
)))
6915 return cp_convert (type
, rhs
);
6916 /* Handle anachronistic conversions from (::*)() to cv void* or (*)(). */
6917 else if (TREE_CODE (type
) == POINTER_TYPE
6918 && (TREE_CODE (TREE_TYPE (type
)) == FUNCTION_TYPE
6919 || TYPE_MAIN_VARIANT (TREE_TYPE (type
)) == void_type_node
)
6921 && TYPE_PTRMEMFUNC_P (TREE_TYPE (rhs
)))
6922 return cp_convert (type
, rhs
);
6924 cp_error ("%s to `%T' from `%T'", errtype
, type
, rhstype
);
6925 return error_mark_node
;
6928 /* Convert RHS to be of type TYPE. If EXP is non-zero,
6929 it is the target of the initialization.
6930 ERRTYPE is a string to use in error messages.
6932 Two major differences between the behavior of
6933 `convert_for_assignment' and `convert_for_initialization'
6934 are that references are bashed in the former, while
6935 copied in the latter, and aggregates are assigned in
6936 the former (operator=) while initialized in the
6939 If using constructor make sure no conversion operator exists, if one does
6940 exist, an ambiguity exists.
6942 If flags doesn't include LOOKUP_COMPLAIN, don't complain about anything. */
6945 convert_for_initialization (exp
, type
, rhs
, flags
, errtype
, fndecl
, parmnum
)
6946 tree exp
, type
, rhs
;
6952 register enum tree_code codel
= TREE_CODE (type
);
6953 register tree rhstype
;
6954 register enum tree_code coder
;
6956 /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
6957 Strip such NOP_EXPRs, since RHS is used in non-lvalue context. */
6958 if (TREE_CODE (rhs
) == NOP_EXPR
6959 && TREE_TYPE (rhs
) == TREE_TYPE (TREE_OPERAND (rhs
, 0))
6960 && codel
!= REFERENCE_TYPE
)
6961 rhs
= TREE_OPERAND (rhs
, 0);
6963 if (rhs
== error_mark_node
6964 || (TREE_CODE (rhs
) == TREE_LIST
&& TREE_VALUE (rhs
) == error_mark_node
))
6965 return error_mark_node
;
6967 if (TREE_CODE (TREE_TYPE (rhs
)) == OFFSET_TYPE
)
6969 rhs
= resolve_offset_ref (rhs
);
6970 if (rhs
== error_mark_node
)
6971 return error_mark_node
;
6974 if (TREE_CODE (TREE_TYPE (rhs
)) == REFERENCE_TYPE
)
6975 rhs
= convert_from_reference (rhs
);
6977 if ((TREE_CODE (TREE_TYPE (rhs
)) == ARRAY_TYPE
6978 && TREE_CODE (type
) != ARRAY_TYPE
6979 && (TREE_CODE (type
) != REFERENCE_TYPE
6980 || TREE_CODE (TREE_TYPE (type
)) != ARRAY_TYPE
))
6981 || TREE_CODE (TREE_TYPE (rhs
)) == FUNCTION_TYPE
6982 || TREE_CODE (TREE_TYPE (rhs
)) == METHOD_TYPE
)
6983 rhs
= default_conversion (rhs
);
6985 rhstype
= TREE_TYPE (rhs
);
6986 coder
= TREE_CODE (rhstype
);
6988 if (coder
== UNKNOWN_TYPE
)
6990 rhs
= instantiate_type (type
, rhs
, 1);
6991 rhstype
= TREE_TYPE (rhs
);
6992 coder
= TREE_CODE (rhstype
);
6995 if (coder
== ERROR_MARK
)
6996 return error_mark_node
;
6998 /* We accept references to incomplete types, so we can
6999 return here before checking if RHS is of complete type. */
7001 if (codel
== REFERENCE_TYPE
)
7003 /* This should eventually happen in convert_arguments. */
7004 extern int warningcount
, errorcount
;
7008 savew
= warningcount
, savee
= errorcount
;
7009 rhs
= convert_to_reference (type
, rhs
, CONV_IMPLICIT
, flags
,
7010 exp
? exp
: error_mark_node
);
7013 if (warningcount
> savew
)
7014 cp_warning_at ("in passing argument %P of `%+D'", parmnum
, fndecl
);
7015 else if (errorcount
> savee
)
7016 cp_error_at ("in passing argument %P of `%+D'", parmnum
, fndecl
);
7021 rhs
= require_complete_type (rhs
);
7022 if (rhs
== error_mark_node
)
7023 return error_mark_node
;
7025 if (exp
!= 0) exp
= require_complete_type (exp
);
7026 if (exp
== error_mark_node
)
7027 return error_mark_node
;
7029 if (TREE_CODE (rhstype
) == REFERENCE_TYPE
)
7030 rhstype
= TREE_TYPE (rhstype
);
7032 type
= complete_type (type
);
7034 if (TYPE_LANG_SPECIFIC (type
)
7035 && (IS_SIGNATURE_POINTER (type
) || IS_SIGNATURE_REFERENCE (type
)))
7036 return build_signature_pointer_constructor (type
, rhs
);
7038 if (IS_AGGR_TYPE (type
)
7039 && (TYPE_NEEDS_CONSTRUCTING (type
) || TREE_HAS_CONSTRUCTOR (rhs
)))
7041 if (flag_ansi_overloading
)
7042 return ocp_convert (type
, rhs
, CONV_IMPLICIT
|CONV_FORCE_TEMP
, flags
);
7044 if (TYPE_MAIN_VARIANT (type
) == TYPE_MAIN_VARIANT (rhstype
))
7046 /* This is sufficient to perform initialization. No need,
7047 apparently, to go through X(X&) to do first-cut
7048 initialization. Return through a TARGET_EXPR so that we get
7049 cleanups if it is used. */
7050 if (TREE_CODE (rhs
) == CALL_EXPR
)
7052 rhs
= build_cplus_new (type
, rhs
);
7055 /* Handle the case of default parameter initialization and
7056 initialization of static variables. */
7057 else if (TREE_CODE (rhs
) == TARGET_EXPR
)
7059 else if (TREE_CODE (rhs
) == INDIRECT_REF
&& TREE_HAS_CONSTRUCTOR (rhs
))
7061 my_friendly_assert (TREE_CODE (TREE_OPERAND (rhs
, 0)) == CALL_EXPR
, 318);
7064 my_friendly_assert (TREE_VALUE (TREE_OPERAND (TREE_OPERAND (rhs
, 0), 1)) == NULL_TREE
, 316);
7065 TREE_VALUE (TREE_OPERAND (TREE_OPERAND (rhs
, 0), 1))
7066 = build_unary_op (ADDR_EXPR
, exp
, 0);
7069 rhs
= build_cplus_new (type
, TREE_OPERAND (rhs
, 0));
7072 else if (TYPE_HAS_TRIVIAL_INIT_REF (type
))
7075 if (TYPE_MAIN_VARIANT (type
) == TYPE_MAIN_VARIANT (rhstype
)
7076 || (IS_AGGR_TYPE (rhstype
) && UNIQUELY_DERIVED_FROM_P (type
, rhstype
)))
7078 if (TYPE_HAS_INIT_REF (type
))
7080 tree init
= build_method_call (exp
, ctor_identifier
,
7081 build_expr_list (NULL_TREE
, rhs
),
7082 TYPE_BINFO (type
), LOOKUP_NORMAL
);
7084 if (init
== error_mark_node
)
7085 return error_mark_node
;
7089 exp
= build_cplus_new (type
, init
);
7093 return build (COMPOUND_EXPR
, type
, init
, exp
);
7096 /* ??? The following warnings are turned off because
7097 this is another place where the default X(X&) constructor
7099 if (TYPE_HAS_ASSIGNMENT (type
))
7100 cp_warning ("bitwise copy: `%T' defines operator=", type
);
7102 if (TREE_CODE (TREE_TYPE (rhs
)) == REFERENCE_TYPE
)
7103 rhs
= convert_from_reference (rhs
);
7104 if (type
!= rhstype
)
7106 tree nrhs
= build1 (NOP_EXPR
, type
, rhs
);
7107 TREE_CONSTANT (nrhs
) = TREE_CONSTANT (rhs
);
7113 return ocp_convert (type
, rhs
, CONV_OLD_CONVERT
,
7114 flags
| LOOKUP_NO_CONVERSION
);
7117 if (type
== TREE_TYPE (rhs
))
7119 if (TREE_READONLY_DECL_P (rhs
))
7120 rhs
= decl_constant_value (rhs
);
7124 return convert_for_assignment (type
, rhs
, errtype
, fndecl
, parmnum
);
7127 /* Expand an ASM statement with operands, handling output operands
7128 that are not variables or INDIRECT_REFS by transforming such
7129 cases into cases that expand_asm_operands can handle.
7131 Arguments are same as for expand_asm_operands.
7133 We don't do default conversions on all inputs, because it can screw
7134 up operands that are expected to be in memory. */
7137 c_expand_asm_operands (string
, outputs
, inputs
, clobbers
, vol
, filename
, line
)
7138 tree string
, outputs
, inputs
, clobbers
;
7143 int noutputs
= list_length (outputs
);
7145 /* o[I] is the place that output number I should be written. */
7146 register tree
*o
= (tree
*) alloca (noutputs
* sizeof (tree
));
7149 /* Record the contents of OUTPUTS before it is modified. */
7150 for (i
= 0, tail
= outputs
; tail
; tail
= TREE_CHAIN (tail
), i
++)
7151 o
[i
] = TREE_VALUE (tail
);
7153 /* Generate the ASM_OPERANDS insn;
7154 store into the TREE_VALUEs of OUTPUTS some trees for
7155 where the values were actually stored. */
7156 expand_asm_operands (string
, outputs
, inputs
, clobbers
, vol
, filename
, line
);
7158 /* Copy all the intermediate outputs into the specified outputs. */
7159 for (i
= 0, tail
= outputs
; tail
; tail
= TREE_CHAIN (tail
), i
++)
7161 if (o
[i
] != TREE_VALUE (tail
))
7163 expand_expr (build_modify_expr (o
[i
], NOP_EXPR
, TREE_VALUE (tail
)),
7164 const0_rtx
, VOIDmode
, EXPAND_NORMAL
);
7167 /* Detect modification of read-only values.
7168 (Otherwise done by build_modify_expr.) */
7171 tree type
= TREE_TYPE (o
[i
]);
7172 if (TYPE_READONLY (type
)
7173 || ((TREE_CODE (type
) == RECORD_TYPE
7174 || TREE_CODE (type
) == UNION_TYPE
)
7175 && C_TYPE_FIELDS_READONLY (type
)))
7176 readonly_error (o
[i
], "modification by `asm'", 1);
7180 /* Those MODIFY_EXPRs could do autoincrements. */
7184 /* Expand a C `return' statement.
7185 RETVAL is the expression for what to return,
7186 or a null pointer for `return;' with no value.
7188 C++: upon seeing a `return', we must call destructors on all
7189 variables in scope which had constructors called on them.
7190 This means that if in a destructor, the base class destructors
7191 must be called before returning.
7193 The RETURN statement in C++ has initialization semantics. */
7196 c_expand_return (retval
)
7199 extern struct nesting
*cond_stack
, *loop_stack
, *case_stack
;
7200 extern tree dtor_label
, ctor_label
;
7201 tree result
= DECL_RESULT (current_function_decl
);
7202 tree valtype
= TREE_TYPE (result
);
7204 if (TREE_THIS_VOLATILE (current_function_decl
))
7205 warning ("function declared `noreturn' has a `return' statement");
7207 if (retval
== error_mark_node
)
7209 current_function_returns_null
= 1;
7213 if (processing_template_decl
)
7215 add_tree (build_min_nt (RETURN_STMT
, retval
));
7219 if (retval
== NULL_TREE
)
7221 /* A non-named return value does not count. */
7223 /* Can't just return from a destructor. */
7226 expand_goto (dtor_label
);
7230 if (DECL_CONSTRUCTOR_P (current_function_decl
))
7231 retval
= current_class_ptr
;
7232 else if (DECL_NAME (result
) != NULL_TREE
7233 && TREE_CODE (valtype
) != VOID_TYPE
)
7237 current_function_returns_null
= 1;
7239 if (valtype
!= NULL_TREE
&& TREE_CODE (valtype
) != VOID_TYPE
)
7241 if (DECL_NAME (DECL_RESULT (current_function_decl
)) == NULL_TREE
)
7243 pedwarn ("`return' with no value, in function returning non-void");
7244 /* Clear this, so finish_function won't say that we
7245 reach the end of a non-void function (which we don't,
7246 we gave a return!). */
7247 current_function_returns_null
= 0;
7251 expand_null_return ();
7255 else if (DECL_CONSTRUCTOR_P (current_function_decl
)
7256 && retval
!= current_class_ptr
)
7258 if (flag_this_is_variable
)
7259 error ("return from a constructor: use `this = ...' instead");
7261 error ("return from a constructor");
7262 retval
= current_class_ptr
;
7265 /* Effective C++ rule 15. See also start_function. */
7267 && DECL_NAME (current_function_decl
) == ansi_opname
[(int) MODIFY_EXPR
]
7268 && retval
!= current_class_ref
)
7269 cp_warning ("`operator=' should return a reference to `*this'");
7271 if (valtype
== NULL_TREE
|| TREE_CODE (valtype
) == VOID_TYPE
)
7273 current_function_returns_null
= 1;
7274 if ((pedantic
&& ! DECL_ARTIFICIAL (current_function_decl
))
7275 || TREE_CODE (TREE_TYPE (retval
)) != VOID_TYPE
)
7276 pedwarn ("`return' with a value, in function returning void");
7277 expand_return (retval
);
7281 /* Now deal with possible C++ hair:
7282 (1) Compute the return value.
7283 (2) If there are aggregate values with destructors which
7284 must be cleaned up, clean them (taking care
7285 not to clobber the return value).
7286 (3) If an X(X&) constructor is defined, the return
7287 value must be returned via that. */
7289 if (retval
== result
7290 || DECL_CONSTRUCTOR_P (current_function_decl
))
7291 /* It's already done for us. */;
7292 else if (TREE_TYPE (retval
) == void_type_node
)
7294 pedwarn ("return of void value in function returning non-void");
7295 expand_expr_stmt (retval
);
7300 retval
= convert_for_initialization
7301 (NULL_TREE
, valtype
, retval
, LOOKUP_NORMAL
|LOOKUP_ONLYCONVERTING
,
7302 "return", NULL_TREE
, 0);
7304 if (retval
== error_mark_node
)
7306 /* Avoid warning about control reaching end of function. */
7307 expand_null_return ();
7311 /* We can't initialize a register from a AGGR_INIT_EXPR. */
7312 else if (! current_function_returns_struct
7313 && TREE_CODE (retval
) == TARGET_EXPR
7314 && TREE_CODE (TREE_OPERAND (retval
, 1)) == AGGR_INIT_EXPR
)
7315 retval
= build (COMPOUND_EXPR
, TREE_TYPE (retval
), retval
,
7316 TREE_OPERAND (retval
, 0));
7318 /* Add some useful error checking for C++. */
7319 else if (TREE_CODE (valtype
) == REFERENCE_TYPE
)
7321 tree whats_returned
;
7323 /* Sort through common things to see what it is
7324 we are returning. */
7325 whats_returned
= retval
;
7326 if (TREE_CODE (whats_returned
) == COMPOUND_EXPR
)
7328 whats_returned
= TREE_OPERAND (whats_returned
, 1);
7329 if (TREE_CODE (whats_returned
) == ADDR_EXPR
)
7330 whats_returned
= TREE_OPERAND (whats_returned
, 0);
7332 while (TREE_CODE (whats_returned
) == CONVERT_EXPR
7333 || TREE_CODE (whats_returned
) == NOP_EXPR
)
7334 whats_returned
= TREE_OPERAND (whats_returned
, 0);
7335 if (TREE_CODE (whats_returned
) == ADDR_EXPR
)
7337 whats_returned
= TREE_OPERAND (whats_returned
, 0);
7338 while (TREE_CODE (whats_returned
) == AGGR_INIT_EXPR
7339 || TREE_CODE (whats_returned
) == TARGET_EXPR
)
7341 /* Get the target. */
7342 whats_returned
= TREE_OPERAND (whats_returned
, 0);
7343 warning ("returning reference to temporary");
7347 if (TREE_CODE (whats_returned
) == VAR_DECL
&& DECL_NAME (whats_returned
))
7349 if (TEMP_NAME_P (DECL_NAME (whats_returned
)))
7350 warning ("reference to non-lvalue returned");
7351 else if (TREE_CODE (TREE_TYPE (whats_returned
)) != REFERENCE_TYPE
7352 && ! TREE_STATIC (whats_returned
)
7353 && IDENTIFIER_LOCAL_VALUE (DECL_NAME (whats_returned
))
7354 && !TREE_PUBLIC (whats_returned
))
7355 cp_warning_at ("reference to local variable `%D' returned", whats_returned
);
7358 else if (TREE_CODE (retval
) == ADDR_EXPR
)
7360 tree whats_returned
= TREE_OPERAND (retval
, 0);
7362 if (TREE_CODE (whats_returned
) == VAR_DECL
7363 && DECL_NAME (whats_returned
)
7364 && IDENTIFIER_LOCAL_VALUE (DECL_NAME (whats_returned
))
7365 && !TREE_STATIC (whats_returned
)
7366 && !TREE_PUBLIC (whats_returned
))
7367 cp_warning_at ("address of local variable `%D' returned", whats_returned
);
7371 if (retval
!= NULL_TREE
7372 && TREE_CODE_CLASS (TREE_CODE (retval
)) == 'd'
7373 && cond_stack
== 0 && loop_stack
== 0 && case_stack
== 0)
7374 current_function_return_value
= retval
;
7376 if (ctor_label
&& TREE_CODE (ctor_label
) != ERROR_MARK
)
7378 /* Here RETVAL is CURRENT_CLASS_PTR, so there's nothing to do. */
7379 expand_goto (ctor_label
);
7382 if (retval
&& retval
!= result
)
7384 result
= build (INIT_EXPR
, TREE_TYPE (result
), result
, retval
);
7385 TREE_SIDE_EFFECTS (result
) = 1;
7388 expand_start_target_temps ();
7390 expand_return (result
);
7392 expand_end_target_temps ();
7394 current_function_returns_value
= 1;
7397 /* Start a C switch statement, testing expression EXP.
7398 Return EXP if it is valid, an error node otherwise. */
7401 c_expand_start_case (exp
)
7405 register enum tree_code code
;
7407 /* Convert from references, etc. */
7408 exp
= default_conversion (exp
);
7409 type
= TREE_TYPE (exp
);
7410 code
= TREE_CODE (type
);
7412 if (IS_AGGR_TYPE_CODE (code
))
7413 exp
= build_type_conversion (CONVERT_EXPR
, integer_type_node
, exp
, 1);
7415 if (exp
== NULL_TREE
)
7417 error ("switch quantity not an integer");
7418 exp
= error_mark_node
;
7420 type
= TREE_TYPE (exp
);
7421 code
= TREE_CODE (type
);
7423 if (code
!= INTEGER_TYPE
&& code
!= ENUMERAL_TYPE
&& code
!= ERROR_MARK
)
7425 error ("switch quantity not an integer");
7426 exp
= error_mark_node
;
7432 exp
= default_conversion (exp
);
7433 type
= TREE_TYPE (exp
);
7434 idx
= get_unwidened (exp
, 0);
7435 /* We can't strip a conversion from a signed type to an unsigned,
7436 because if we did, int_fits_type_p would do the wrong thing
7437 when checking case values for being in range,
7438 and it's too hard to do the right thing. */
7439 if (TREE_UNSIGNED (TREE_TYPE (exp
))
7440 == TREE_UNSIGNED (TREE_TYPE (idx
)))
7445 (1, fold (build1 (CLEANUP_POINT_EXPR
, TREE_TYPE (exp
), exp
)),
7446 type
, "switch statement");
7451 /* CONSTP remembers whether or not all the intervening pointers in the `to'
7452 type have been const. */
7455 comp_ptr_ttypes_real (to
, from
, constp
)
7459 for (; ; to
= TREE_TYPE (to
), from
= TREE_TYPE (from
))
7461 if (TREE_CODE (to
) != TREE_CODE (from
))
7464 if (TREE_CODE (from
) == OFFSET_TYPE
7465 && comptypes (TYPE_OFFSET_BASETYPE (from
),
7466 TYPE_OFFSET_BASETYPE (to
), 1))
7469 /* Const and volatile mean something different for function types,
7470 so the usual checks are not appropriate. */
7471 if (TREE_CODE (to
) != FUNCTION_TYPE
&& TREE_CODE (to
) != METHOD_TYPE
)
7473 if (TYPE_READONLY (from
) > TYPE_READONLY (to
)
7474 || TYPE_VOLATILE (from
) > TYPE_VOLATILE (to
))
7478 && (TYPE_READONLY (to
) > TYPE_READONLY (from
)
7479 || TYPE_VOLATILE (to
) > TYPE_READONLY (from
)))
7481 constp
&= TYPE_READONLY (to
);
7484 if (TREE_CODE (to
) != POINTER_TYPE
)
7485 return comptypes (TYPE_MAIN_VARIANT (to
), TYPE_MAIN_VARIANT (from
), 1);
7489 /* When comparing, say, char ** to char const **, this function takes the
7490 'char *' and 'char const *'. Do not pass non-pointer types to this
7494 comp_ptr_ttypes (to
, from
)
7497 return comp_ptr_ttypes_real (to
, from
, 1);
7500 /* Returns 1 if to and from are (possibly multi-level) pointers to the same
7501 type or inheritance-related types, regardless of cv-quals. */
7504 ptr_reasonably_similar (to
, from
)
7507 for (; ; to
= TREE_TYPE (to
), from
= TREE_TYPE (from
))
7509 if (TREE_CODE (to
) != TREE_CODE (from
))
7512 if (TREE_CODE (from
) == OFFSET_TYPE
7513 && comptypes (TYPE_OFFSET_BASETYPE (to
),
7514 TYPE_OFFSET_BASETYPE (from
), -1))
7517 if (TREE_CODE (to
) != POINTER_TYPE
)
7519 (TYPE_MAIN_VARIANT (to
), TYPE_MAIN_VARIANT (from
), -1);
7523 /* Like comp_ptr_ttypes, for const_cast. */
7526 comp_ptr_ttypes_const (to
, from
)
7529 for (; ; to
= TREE_TYPE (to
), from
= TREE_TYPE (from
))
7531 if (TREE_CODE (to
) != TREE_CODE (from
))
7534 if (TREE_CODE (from
) == OFFSET_TYPE
7535 && comptypes (TYPE_OFFSET_BASETYPE (from
),
7536 TYPE_OFFSET_BASETYPE (to
), 1))
7539 if (TREE_CODE (to
) != POINTER_TYPE
)
7540 return comptypes (TYPE_MAIN_VARIANT (to
), TYPE_MAIN_VARIANT (from
), 1);
7544 /* Like comp_ptr_ttypes, for reinterpret_cast. */
7547 comp_ptr_ttypes_reinterpret (to
, from
)
7552 for (; ; to
= TREE_TYPE (to
), from
= TREE_TYPE (from
))
7554 if (TREE_CODE (from
) == OFFSET_TYPE
)
7555 from
= TREE_TYPE (from
);
7556 if (TREE_CODE (to
) == OFFSET_TYPE
)
7557 to
= TREE_TYPE (to
);
7559 if (TREE_CODE (to
) != TREE_CODE (from
))
7562 /* Const and volatile mean something different for function types,
7563 so the usual checks are not appropriate. */
7564 if (TREE_CODE (to
) != FUNCTION_TYPE
&& TREE_CODE (to
) != METHOD_TYPE
)
7566 if (TYPE_READONLY (from
) > TYPE_READONLY (to
)
7567 || TYPE_VOLATILE (from
) > TYPE_VOLATILE (to
))
7571 && (TYPE_READONLY (to
) > TYPE_READONLY (from
)
7572 || TYPE_VOLATILE (to
) > TYPE_READONLY (from
)))
7574 constp
&= TYPE_READONLY (to
);
7577 if (TREE_CODE (to
) != POINTER_TYPE
)