1 /* Build expressions with type checking for C++ compiler.
2 Copyright (C) 1987, 88, 89, 92, 93, 94, 95, 1996 Free Software Foundation, Inc.
3 Hacked by Michael Tiemann (tiemann@cygnus.com)
5 This file is part of GNU CC.
7 GNU CC is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2, or (at your option)
12 GNU CC is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with GNU CC; see the file COPYING. If not, write to
19 the Free Software Foundation, 59 Temple Place - Suite 330,
20 Boston, MA 02111-1307, USA. */
23 /* This file is part of the C++ front end.
24 It contains routines to build C++ expressions given their operands,
25 including computing the types of the result, C and C++ specific error
26 checks, and some optimization.
28 There are also routines to build RETURN_STMT nodes and CASE_STMT nodes,
29 and to process initializations in declarations (since they work
30 like a strange sort of assignment). */
44 extern void compiler_error ();
46 static tree convert_for_assignment
PROTO((tree
, tree
, char*, tree
,
48 static tree pointer_int_sum
PROTO((enum tree_code
, tree
, tree
));
49 static tree rationalize_conditional_expr
PROTO((enum tree_code
, tree
));
50 static int comp_target_parms
PROTO((tree
, tree
, int));
51 static int comp_ptr_ttypes_real
PROTO((tree
, tree
, int));
52 static int comp_ptr_ttypes_const
PROTO((tree
, tree
));
53 static int comp_ptr_ttypes_reinterpret
PROTO((tree
, tree
));
54 static int comp_array_types
PROTO((int (*) (tree
, tree
, int), tree
,
56 static tree build_ptrmemfunc1
PROTO((tree
, tree
, tree
, tree
, tree
));
57 static tree common_base_type
PROTO((tree
, tree
));
58 static tree convert_sequence
PROTO((tree
, tree
));
59 static tree lookup_anon_field
PROTO((tree
, tree
));
60 static tree pointer_diff
PROTO((tree
, tree
));
61 static tree qualify_type
PROTO((tree
, tree
));
62 static tree expand_target_expr
PROTO((tree
));
63 static tree get_delta_difference
PROTO((tree
, tree
, int));
65 /* Return the target type of TYPE, which meas return T for:
66 T*, T&, T[], T (...), and otherwise, just T. */
72 if (TREE_CODE (type
) == REFERENCE_TYPE
)
73 type
= TREE_TYPE (type
);
74 while (TREE_CODE (type
) == POINTER_TYPE
75 || TREE_CODE (type
) == ARRAY_TYPE
76 || TREE_CODE (type
) == FUNCTION_TYPE
77 || TREE_CODE (type
) == METHOD_TYPE
78 || TREE_CODE (type
) == OFFSET_TYPE
)
79 type
= TREE_TYPE (type
);
83 /* Do `exp = require_complete_type (exp);' to make sure exp
84 does not have an incomplete type. (That includes void types.) */
87 require_complete_type (value
)
92 if (processing_template_decl
)
95 type
= TREE_TYPE (value
);
97 /* First, detect a valid value with a complete type. */
98 if (TYPE_SIZE (type
) != 0
99 && type
!= void_type_node
100 && ! (TYPE_LANG_SPECIFIC (type
)
101 && (IS_SIGNATURE_POINTER (type
) || IS_SIGNATURE_REFERENCE (type
))
102 && TYPE_SIZE (SIGNATURE_TYPE (type
)) == 0))
105 /* If we see X::Y, we build an OFFSET_TYPE which has
106 not been laid out. Try to avoid an error by interpreting
107 it as this->X::Y, if reasonable. */
108 if (TREE_CODE (value
) == OFFSET_REF
109 && current_class_ref
!= 0
110 && TREE_OPERAND (value
, 0) == current_class_ref
)
112 tree base
, member
= TREE_OPERAND (value
, 1);
113 tree basetype
= TYPE_OFFSET_BASETYPE (type
);
114 my_friendly_assert (TREE_CODE (member
) == FIELD_DECL
, 305);
115 base
= convert_pointer_to (basetype
, current_class_ptr
);
116 value
= build (COMPONENT_REF
, TREE_TYPE (member
),
117 build_indirect_ref (base
, NULL_PTR
), member
);
118 return require_complete_type (value
);
121 if (IS_AGGR_TYPE (type
) && CLASSTYPE_TEMPLATE_INSTANTIATION (type
))
123 instantiate_class_template (TYPE_MAIN_VARIANT (type
));
124 if (TYPE_SIZE (type
) != 0)
128 incomplete_type_error (value
, type
);
129 return error_mark_node
;
136 if (type
== error_mark_node
|| TYPE_SIZE (type
) != NULL_TREE
)
138 else if (TREE_CODE (type
) == ARRAY_TYPE
&& TYPE_DOMAIN (type
))
140 tree t
= complete_type (TREE_TYPE (type
));
141 if (TYPE_SIZE (t
) != NULL_TREE
&& ! processing_template_decl
)
143 TYPE_NEEDS_CONSTRUCTING (type
)
144 = TYPE_NEEDS_CONSTRUCTING (TYPE_MAIN_VARIANT (t
));
145 TYPE_NEEDS_DESTRUCTOR (type
)
146 = TYPE_NEEDS_DESTRUCTOR (TYPE_MAIN_VARIANT (t
));
148 else if (IS_AGGR_TYPE (type
) && CLASSTYPE_TEMPLATE_INSTANTIATION (type
))
149 instantiate_class_template (TYPE_MAIN_VARIANT (type
));
154 /* Return truthvalue of whether type of EXP is instantiated. */
160 return (TREE_CODE (exp
) == TREE_LIST
161 || TREE_TYPE (exp
) == unknown_type_node
162 || (TREE_CODE (TREE_TYPE (exp
)) == OFFSET_TYPE
163 && TREE_TYPE (TREE_TYPE (exp
)) == unknown_type_node
));
166 /* Return truthvalue of whether T is function (or pfn) type. */
172 return (TREE_CODE (t
) == FUNCTION_TYPE
|| TREE_CODE (t
) == METHOD_TYPE
173 || (TREE_CODE (t
) == POINTER_TYPE
174 && (TREE_CODE (TREE_TYPE (t
)) == FUNCTION_TYPE
175 || TREE_CODE (TREE_TYPE (t
)) == METHOD_TYPE
)));
178 /* Do `exp = require_instantiated_type (type, exp);' to make sure EXP
179 does not have an uninstantiated type.
180 TYPE is type to instantiate with, if uninstantiated. */
183 require_instantiated_type (type
, exp
, errval
)
184 tree type
, exp
, errval
;
186 if (TREE_TYPE (exp
) == NULL_TREE
)
188 error ("argument list may not have an initializer list");
192 if (TREE_TYPE (exp
) == unknown_type_node
193 || (TREE_CODE (TREE_TYPE (exp
)) == OFFSET_TYPE
194 && TREE_TYPE (TREE_TYPE (exp
)) == unknown_type_node
))
196 exp
= instantiate_type (type
, exp
, 1);
197 if (TREE_TYPE (exp
) == error_mark_node
)
203 /* Return a variant of TYPE which has all the type qualifiers of LIKE
204 as well as those of TYPE. */
207 qualify_type (type
, like
)
210 int constflag
= TYPE_READONLY (type
) || TYPE_READONLY (like
);
211 int volflag
= TYPE_VOLATILE (type
) || TYPE_VOLATILE (like
);
212 /* @@ Must do member pointers here. */
213 return cp_build_type_variant (type
, constflag
, volflag
);
216 /* Return the common type of two parameter lists.
217 We assume that comptypes has already been done and returned 1;
218 if that isn't so, this may crash.
220 As an optimization, free the space we allocate if the parameter
221 lists are already common. */
227 tree oldargs
= p1
, newargs
, n
;
230 char *first_obj
= (char *) oballoc (0);
232 len
= list_length (p1
);
233 newargs
= tree_last (p1
);
235 if (newargs
== void_list_node
)
244 newargs
= tree_cons (NULL_TREE
, NULL_TREE
, newargs
);
249 p1
= TREE_CHAIN (p1
), p2
= TREE_CHAIN (p2
), n
= TREE_CHAIN (n
), i
++)
251 if (TREE_PURPOSE (p1
) && !TREE_PURPOSE (p2
))
253 TREE_PURPOSE (n
) = TREE_PURPOSE (p1
);
256 else if (! TREE_PURPOSE (p1
))
258 if (TREE_PURPOSE (p2
))
260 TREE_PURPOSE (n
) = TREE_PURPOSE (p2
);
266 if (1 != simple_cst_equal (TREE_PURPOSE (p1
), TREE_PURPOSE (p2
)))
268 TREE_PURPOSE (n
) = TREE_PURPOSE (p2
);
270 if (TREE_VALUE (p1
) != TREE_VALUE (p2
))
273 TREE_VALUE (n
) = common_type (TREE_VALUE (p1
), TREE_VALUE (p2
));
276 TREE_VALUE (n
) = TREE_VALUE (p1
);
287 /* Return the common type of two types.
288 We assume that comptypes has already been done and returned 1;
289 if that isn't so, this may crash.
291 This is the type for the result of most arithmetic operations
292 if the operands have the given two types.
294 We do not deal with enumeral types here because they have already been
295 converted to integer types. */
301 register enum tree_code code1
;
302 register enum tree_code code2
;
305 /* Save time if the two types are the same. */
307 if (t1
== t2
) return t1
;
309 /* If one type is nonsense, use the other. */
310 if (t1
== error_mark_node
)
312 if (t2
== error_mark_node
)
315 /* Merge the attributes */
317 { register tree a1
, a2
;
318 a1
= TYPE_ATTRIBUTES (t1
);
319 a2
= TYPE_ATTRIBUTES (t2
);
321 /* Either one unset? Take the set one. */
323 if (!(attributes
= a1
))
326 /* One that completely contains the other? Take it. */
328 else if (a2
&& !attribute_list_contained (a1
, a2
))
329 if (attribute_list_contained (a2
, a1
))
333 /* Pick the longest list, and hang on the other list. */
334 /* ??? For the moment we punt on the issue of attrs with args. */
336 if (list_length (a1
) < list_length (a2
))
337 attributes
= a2
, a2
= a1
;
339 for (; a2
; a2
= TREE_CHAIN (a2
))
340 if (lookup_attribute (IDENTIFIER_POINTER (TREE_PURPOSE (a2
)),
341 attributes
) == NULL_TREE
)
344 TREE_CHAIN (a1
) = attributes
;
350 /* Treat an enum type as the unsigned integer type of the same width. */
352 if (TREE_CODE (t1
) == ENUMERAL_TYPE
)
353 t1
= type_for_size (TYPE_PRECISION (t1
), 1);
354 if (TREE_CODE (t2
) == ENUMERAL_TYPE
)
355 t2
= type_for_size (TYPE_PRECISION (t2
), 1);
357 if (TYPE_PTRMEMFUNC_P (t1
))
358 t1
= TYPE_PTRMEMFUNC_FN_TYPE (t1
);
359 if (TYPE_PTRMEMFUNC_P (t2
))
360 t2
= TYPE_PTRMEMFUNC_FN_TYPE (t2
);
362 code1
= TREE_CODE (t1
);
363 code2
= TREE_CODE (t2
);
365 /* If one type is complex, form the common type of the non-complex
366 components, then make that complex. Use T1 or T2 if it is the
368 if (code1
== COMPLEX_TYPE
|| code2
== COMPLEX_TYPE
)
370 tree subtype1
= code1
== COMPLEX_TYPE
? TREE_TYPE (t1
) : t1
;
371 tree subtype2
= code2
== COMPLEX_TYPE
? TREE_TYPE (t2
) : t2
;
372 tree subtype
= common_type (subtype1
, subtype2
);
374 if (code1
== COMPLEX_TYPE
&& TREE_TYPE (t1
) == subtype
)
375 return build_type_attribute_variant (t1
, attributes
);
376 else if (code2
== COMPLEX_TYPE
&& TREE_TYPE (t2
) == subtype
)
377 return build_type_attribute_variant (t2
, attributes
);
379 return build_type_attribute_variant (build_complex_type (subtype
),
387 /* If only one is real, use it as the result. */
389 if (code1
== REAL_TYPE
&& code2
!= REAL_TYPE
)
390 return build_type_attribute_variant (t1
, attributes
);
392 if (code2
== REAL_TYPE
&& code1
!= REAL_TYPE
)
393 return build_type_attribute_variant (t2
, attributes
);
395 /* Both real or both integers; use the one with greater precision. */
397 if (TYPE_PRECISION (t1
) > TYPE_PRECISION (t2
))
398 return build_type_attribute_variant (t1
, attributes
);
399 else if (TYPE_PRECISION (t2
) > TYPE_PRECISION (t1
))
400 return build_type_attribute_variant (t2
, attributes
);
402 /* Same precision. Prefer longs to ints even when same size. */
404 if (TYPE_MAIN_VARIANT (t1
) == long_unsigned_type_node
405 || TYPE_MAIN_VARIANT (t2
) == long_unsigned_type_node
)
406 return build_type_attribute_variant (long_unsigned_type_node
,
409 if (TYPE_MAIN_VARIANT (t1
) == long_integer_type_node
410 || TYPE_MAIN_VARIANT (t2
) == long_integer_type_node
)
412 /* But preserve unsignedness from the other type,
413 since long cannot hold all the values of an unsigned int. */
414 if (TREE_UNSIGNED (t1
) || TREE_UNSIGNED (t2
))
415 t1
= long_unsigned_type_node
;
417 t1
= long_integer_type_node
;
418 return build_type_attribute_variant (t1
, attributes
);
421 if (TYPE_MAIN_VARIANT (t1
) == long_double_type_node
422 || TYPE_MAIN_VARIANT (t2
) == long_double_type_node
)
423 return build_type_attribute_variant (long_double_type_node
,
426 /* Otherwise prefer the unsigned one. */
428 if (TREE_UNSIGNED (t1
))
429 return build_type_attribute_variant (t1
, attributes
);
431 return build_type_attribute_variant (t2
, attributes
);
435 /* For two pointers, do this recursively on the target type,
436 and combine the qualifiers of the two types' targets. */
437 /* This code was turned off; I don't know why.
438 But ANSI C++ specifies doing this with the qualifiers.
439 So I turned it on again. */
441 tree tt1
= TYPE_MAIN_VARIANT (TREE_TYPE (t1
));
442 tree tt2
= TYPE_MAIN_VARIANT (TREE_TYPE (t2
));
444 = TYPE_READONLY (TREE_TYPE (t1
)) || TYPE_READONLY (TREE_TYPE (t2
));
446 = TYPE_VOLATILE (TREE_TYPE (t1
)) || TYPE_VOLATILE (TREE_TYPE (t2
));
451 else if (tt1
== void_type_node
|| tt2
== void_type_node
)
452 target
= void_type_node
;
454 target
= common_type (tt1
, tt2
);
456 target
= cp_build_type_variant (target
, constp
, volatilep
);
457 if (code1
== POINTER_TYPE
)
458 t1
= build_pointer_type (target
);
460 t1
= build_reference_type (target
);
461 t1
= build_type_attribute_variant (t1
, attributes
);
463 if (TREE_CODE (target
) == METHOD_TYPE
)
464 t1
= build_ptrmemfunc_type (t1
);
471 tree elt
= common_type (TREE_TYPE (t1
), TREE_TYPE (t2
));
472 /* Save space: see if the result is identical to one of the args. */
473 if (elt
== TREE_TYPE (t1
) && TYPE_DOMAIN (t1
))
474 return build_type_attribute_variant (t1
, attributes
);
475 if (elt
== TREE_TYPE (t2
) && TYPE_DOMAIN (t2
))
476 return build_type_attribute_variant (t2
, attributes
);
477 /* Merge the element types, and have a size if either arg has one. */
478 t1
= build_cplus_array_type
479 (elt
, TYPE_DOMAIN (TYPE_DOMAIN (t1
) ? t1
: t2
));
480 return build_type_attribute_variant (t1
, attributes
);
484 /* Function types: prefer the one that specified arg types.
485 If both do, merge the arg types. Also merge the return types. */
487 tree valtype
= common_type (TREE_TYPE (t1
), TREE_TYPE (t2
));
488 tree p1
= TYPE_ARG_TYPES (t1
);
489 tree p2
= TYPE_ARG_TYPES (t2
);
492 /* Save space: see if the result is identical to one of the args. */
493 if (valtype
== TREE_TYPE (t1
) && ! p2
)
494 return build_type_attribute_variant (t1
, attributes
);
495 if (valtype
== TREE_TYPE (t2
) && ! p1
)
496 return build_type_attribute_variant (t2
, attributes
);
498 /* Simple way if one arg fails to specify argument types. */
499 if (p1
== NULL_TREE
|| TREE_VALUE (p1
) == void_type_node
)
501 rval
= build_function_type (valtype
, p2
);
502 if ((raises
= TYPE_RAISES_EXCEPTIONS (t2
)))
503 rval
= build_exception_variant (rval
, raises
);
504 return build_type_attribute_variant (rval
, attributes
);
506 raises
= TYPE_RAISES_EXCEPTIONS (t1
);
507 if (p2
== NULL_TREE
|| TREE_VALUE (p2
) == void_type_node
)
509 rval
= build_function_type (valtype
, p1
);
511 rval
= build_exception_variant (rval
, raises
);
512 return build_type_attribute_variant (rval
, attributes
);
515 rval
= build_function_type (valtype
, commonparms (p1
, p2
));
516 rval
= build_exception_variant (rval
, raises
);
517 return build_type_attribute_variant (rval
, attributes
);
522 my_friendly_assert (TYPE_MAIN_VARIANT (t1
) == t1
523 && TYPE_MAIN_VARIANT (t2
) == t2
, 306);
525 if (DERIVED_FROM_P (t1
, t2
) && binfo_or_else (t1
, t2
))
526 return build_type_attribute_variant (t1
, attributes
);
527 else if (binfo_or_else (t2
, t1
))
528 return build_type_attribute_variant (t2
, attributes
);
530 compiler_error ("common_type called with uncommon aggregate types");
533 if (TREE_CODE (TREE_TYPE (t1
)) == TREE_CODE (TREE_TYPE (t2
)))
535 /* Get this value the long way, since TYPE_METHOD_BASETYPE
536 is just the main variant of this. */
540 tree b1
= TYPE_OFFSET_BASETYPE (t1
);
541 tree b2
= TYPE_OFFSET_BASETYPE (t2
);
543 if (comptypes (b1
, b2
, 1)
544 || (DERIVED_FROM_P (b1
, b2
) && binfo_or_else (b1
, b2
)))
545 basetype
= TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (t2
)));
548 if (binfo_or_else (b2
, b1
) == NULL_TREE
)
549 compiler_error ("common_type called with uncommon method types");
550 basetype
= TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (t1
)));
553 raises
= TYPE_RAISES_EXCEPTIONS (t1
);
555 /* If this was a member function type, get back to the
556 original type of type member function (i.e., without
557 the class instance variable up front. */
558 t1
= build_function_type (TREE_TYPE (t1
), TREE_CHAIN (TYPE_ARG_TYPES (t1
)));
559 t2
= build_function_type (TREE_TYPE (t2
), TREE_CHAIN (TYPE_ARG_TYPES (t2
)));
560 t3
= common_type (t1
, t2
);
561 t3
= build_cplus_method_type (basetype
, TREE_TYPE (t3
), TYPE_ARG_TYPES (t3
));
562 t1
= build_exception_variant (t3
, raises
);
565 compiler_error ("common_type called with uncommon method types");
567 return build_type_attribute_variant (t1
, attributes
);
570 if (TREE_TYPE (t1
) == TREE_TYPE (t2
))
572 tree b1
= TYPE_OFFSET_BASETYPE (t1
);
573 tree b2
= TYPE_OFFSET_BASETYPE (t2
);
575 if (comptypes (b1
, b2
, 1)
576 || (DERIVED_FROM_P (b1
, b2
) && binfo_or_else (b1
, b2
)))
577 return build_type_attribute_variant (t2
, attributes
);
578 else if (binfo_or_else (b2
, b1
))
579 return build_type_attribute_variant (t1
, attributes
);
581 compiler_error ("common_type called with uncommon member types");
584 return build_type_attribute_variant (t1
, attributes
);
588 /* Return 1 if TYPE1 and TYPE2 raise the same exceptions. */
591 compexcepttypes (t1
, t2
)
594 return TYPE_RAISES_EXCEPTIONS (t1
) == TYPE_RAISES_EXCEPTIONS (t2
);
598 comp_array_types (cmp
, t1
, t2
, strict
)
599 register int (*cmp
) PROTO((tree
, tree
, int));
603 tree d1
= TYPE_DOMAIN (t1
);
604 tree d2
= TYPE_DOMAIN (t2
);
606 /* Target types must match incl. qualifiers. */
607 if (!(TREE_TYPE (t1
) == TREE_TYPE (t2
)
608 || (*cmp
) (TREE_TYPE (t1
), TREE_TYPE (t2
), strict
)))
611 /* Sizes must match unless one is missing or variable. */
612 if (d1
== 0 || d2
== 0 || d1
== d2
613 || TREE_CODE (TYPE_MIN_VALUE (d1
)) != INTEGER_CST
614 || TREE_CODE (TYPE_MIN_VALUE (d2
)) != INTEGER_CST
615 || TREE_CODE (TYPE_MAX_VALUE (d1
)) != INTEGER_CST
616 || TREE_CODE (TYPE_MAX_VALUE (d2
)) != INTEGER_CST
)
619 return ((TREE_INT_CST_LOW (TYPE_MIN_VALUE (d1
))
620 == TREE_INT_CST_LOW (TYPE_MIN_VALUE (d2
)))
621 && (TREE_INT_CST_HIGH (TYPE_MIN_VALUE (d1
))
622 == TREE_INT_CST_HIGH (TYPE_MIN_VALUE (d2
)))
623 && (TREE_INT_CST_LOW (TYPE_MAX_VALUE (d1
))
624 == TREE_INT_CST_LOW (TYPE_MAX_VALUE (d2
)))
625 && (TREE_INT_CST_HIGH (TYPE_MAX_VALUE (d1
))
626 == TREE_INT_CST_HIGH (TYPE_MAX_VALUE (d2
))));
629 /* Return 1 if TYPE1 and TYPE2 are compatible types for assignment
630 or various other operations. This is what ANSI C++ speaks of as
633 For C++: argument STRICT says we should be strict about this
636 2 : strict, except that if one type is a reference and
637 the other is not, compare the target type of the
638 reference to the type that's not a reference (ARM, p308).
639 This is used for checking for invalid overloading.
640 1 : strict (compared according to ANSI C)
641 This is used for checking whether two function decls match.
642 0 : <= (compared according to C++)
643 -1: <= or >= (relaxed)
645 Otherwise, pointers involving base classes and derived classes
646 can be mixed as valid: i.e. a pointer to a base class may be assigned
647 to a pointer to one of its derived classes, as per C++. A pointer to
648 a derived class may be passed as a parameter to a function expecting a
649 pointer to a base classes. These allowances do not commute. In this
650 case, TYPE1 is assumed to be the base class, and TYPE2 is assumed to
651 be the derived class. */
654 comptypes (type1
, type2
, strict
)
658 register tree t1
= type1
;
659 register tree t2
= type2
;
662 /* Suppress errors caused by previously reported errors */
667 /* This should never happen. */
668 my_friendly_assert (t1
!= error_mark_node
, 307);
670 if (t2
== error_mark_node
)
675 /* Treat an enum type as the unsigned integer type of the same width. */
677 if (TREE_CODE (t1
) == ENUMERAL_TYPE
)
678 t1
= type_for_size (TYPE_PRECISION (t1
), 1);
679 if (TREE_CODE (t2
) == ENUMERAL_TYPE
)
680 t2
= type_for_size (TYPE_PRECISION (t2
), 1);
686 if (TYPE_PTRMEMFUNC_P (t1
))
687 t1
= TYPE_PTRMEMFUNC_FN_TYPE (t1
);
688 if (TYPE_PTRMEMFUNC_P (t2
))
689 t2
= TYPE_PTRMEMFUNC_FN_TYPE (t2
);
691 /* Different classes of types can't be compatible. */
693 if (TREE_CODE (t1
) != TREE_CODE (t2
))
696 && ((TREE_CODE (t1
) == REFERENCE_TYPE
)
697 ^ (TREE_CODE (t2
) == REFERENCE_TYPE
)))
699 if (TREE_CODE (t1
) == REFERENCE_TYPE
)
700 return comptypes (TREE_TYPE (t1
), t2
, 1);
701 return comptypes (t1
, TREE_TYPE (t2
), 1);
709 /* Qualifiers must match. */
711 if (TYPE_READONLY (t1
) != TYPE_READONLY (t2
))
713 if (TYPE_VOLATILE (t1
) != TYPE_VOLATILE (t2
))
716 /* Allow for two different type nodes which have essentially the same
717 definition. Note that we already checked for equality of the type
718 type qualifiers (just above). */
720 if (TYPE_MAIN_VARIANT (t1
) == TYPE_MAIN_VARIANT (t2
))
723 /* ??? COMP_TYPE_ATTRIBUTES is currently useless for variables as each
724 attribute is its own main variant (`val' will remain 0). */
725 #ifndef COMP_TYPE_ATTRIBUTES
726 #define COMP_TYPE_ATTRIBUTES(t1,t2) 1
729 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
730 if (! (attrval
= COMP_TYPE_ATTRIBUTES (t1
, t2
)))
733 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
736 switch (TREE_CODE (t1
))
740 if (CLASSTYPE_TEMPLATE_INFO (t1
) && CLASSTYPE_TEMPLATE_INFO (t2
)
741 && CLASSTYPE_TI_TEMPLATE (t1
) == CLASSTYPE_TI_TEMPLATE (t2
))
743 int i
= TREE_VEC_LENGTH (CLASSTYPE_TI_ARGS (t1
));
744 tree
*p1
= &TREE_VEC_ELT (CLASSTYPE_TI_ARGS (t1
), 0);
745 tree
*p2
= &TREE_VEC_ELT (CLASSTYPE_TI_ARGS (t2
), 0);
749 if (TREE_CODE_CLASS (TREE_CODE (p1
[i
])) == 't')
751 if (! comptypes (p1
[i
], p2
[i
], 1))
756 if (simple_cst_equal (p1
[i
], p2
[i
]) <= 0)
767 val
= (comptypes (build_pointer_type (TYPE_OFFSET_BASETYPE (t1
)),
768 build_pointer_type (TYPE_OFFSET_BASETYPE (t2
)), strict
)
769 && comptypes (TREE_TYPE (t1
), TREE_TYPE (t2
), strict
));
773 if (! compexcepttypes (t1
, t2
))
776 /* This case is anti-symmetrical!
777 One can pass a base member (or member function)
778 to something expecting a derived member (or member function),
779 but not vice-versa! */
781 val
= (comptypes (TREE_TYPE (t1
), TREE_TYPE (t2
), strict
)
782 && compparms (TYPE_ARG_TYPES (t1
),
783 TYPE_ARG_TYPES (t2
), strict
));
797 if (TREE_CODE (t1
) == RECORD_TYPE
&& TREE_CODE (t2
) == RECORD_TYPE
)
801 rval
= t1
== t2
|| DERIVED_FROM_P (t1
, t2
);
810 val
= DERIVED_FROM_P (t2
, t1
);
817 val
= comptypes (t1
, t2
, strict
);
821 if (! compexcepttypes (t1
, t2
))
824 val
= ((TREE_TYPE (t1
) == TREE_TYPE (t2
)
825 || comptypes (TREE_TYPE (t1
), TREE_TYPE (t2
), strict
))
826 && compparms (TYPE_ARG_TYPES (t1
), TYPE_ARG_TYPES (t2
), strict
));
830 /* Target types must match incl. qualifiers. */
831 val
= comp_array_types (comptypes
, t1
, t2
, strict
);
834 case TEMPLATE_TYPE_PARM
:
835 return TEMPLATE_TYPE_IDX (t1
) == TEMPLATE_TYPE_IDX (t2
)
836 && TEMPLATE_TYPE_LEVEL (t1
) == TEMPLATE_TYPE_LEVEL (t2
);
839 if (TYPE_IDENTIFIER (t1
) != TYPE_IDENTIFIER (t2
))
841 return comptypes (TYPE_CONTEXT (t1
), TYPE_CONTEXT (t2
), 1);
843 return attrval
== 2 && val
== 1 ? 2 : val
;
846 /* Return 1 if TTL and TTR are pointers to types that are equivalent,
847 ignoring their qualifiers.
849 NPTRS is the number of pointers we can strip off and keep cool.
850 This is used to permit (for aggr A, aggr B) A, B* to convert to A*,
851 but to not permit B** to convert to A**. */
854 comp_target_types (ttl
, ttr
, nptrs
)
858 ttl
= TYPE_MAIN_VARIANT (ttl
);
859 ttr
= TYPE_MAIN_VARIANT (ttr
);
863 if (TREE_CODE (ttr
) != TREE_CODE (ttl
))
866 if (TREE_CODE (ttr
) == POINTER_TYPE
)
868 ttl
= TREE_TYPE (ttl
);
869 ttr
= TREE_TYPE (ttr
);
873 if (TREE_CODE (ttl
) == VOID_TYPE
874 && TREE_CODE (ttr
) != FUNCTION_TYPE
875 && TREE_CODE (ttr
) != METHOD_TYPE
876 && TREE_CODE (ttr
) != OFFSET_TYPE
)
878 else if (TREE_CODE (ttr
) == VOID_TYPE
879 && TREE_CODE (ttl
) != FUNCTION_TYPE
880 && TREE_CODE (ttl
) != METHOD_TYPE
881 && TREE_CODE (ttl
) != OFFSET_TYPE
)
883 else if (TREE_CODE (ttl
) == POINTER_TYPE
884 || TREE_CODE (ttl
) == ARRAY_TYPE
)
886 if (comp_ptr_ttypes (ttl
, ttr
))
888 else if (comp_ptr_ttypes (ttr
, ttl
))
894 /* Const and volatile mean something different for function types,
895 so the usual checks are not appropriate. */
896 if (TREE_CODE (ttl
) == FUNCTION_TYPE
|| TREE_CODE (ttl
) == METHOD_TYPE
)
897 return comp_target_types (ttl
, ttr
, nptrs
- 1);
899 /* Make sure that the cv-quals change only in the same direction as
903 int c
= TYPE_READONLY (ttl
) - TYPE_READONLY (ttr
);
904 int v
= TYPE_VOLATILE (ttl
) - TYPE_VOLATILE (ttr
);
906 if ((c
> 0 && v
< 0) || (c
< 0 && v
> 0))
909 if (TYPE_MAIN_VARIANT (ttl
) == TYPE_MAIN_VARIANT (ttr
))
910 return (c
+ v
< 0) ? -1 : 1;
912 t
= comp_target_types (ttl
, ttr
, nptrs
- 1);
913 if ((t
== 1 && c
+ v
>= 0) || (t
== -1 && c
+ v
<= 0))
920 if (TREE_CODE (ttr
) == REFERENCE_TYPE
)
921 return comp_target_types (TREE_TYPE (ttl
), TREE_TYPE (ttr
), nptrs
);
922 if (TREE_CODE (ttr
) == ARRAY_TYPE
)
923 return comp_array_types (comp_target_types
, ttl
, ttr
, 0);
924 else if (TREE_CODE (ttr
) == FUNCTION_TYPE
|| TREE_CODE (ttr
) == METHOD_TYPE
)
925 if (comp_target_types (TREE_TYPE (ttl
), TREE_TYPE (ttr
), nptrs
))
926 switch (comp_target_parms (TYPE_ARG_TYPES (ttl
), TYPE_ARG_TYPES (ttr
), 1))
935 my_friendly_abort (112);
941 else if (TREE_CODE (ttr
) == OFFSET_TYPE
)
943 /* Contravariance: we can assign a pointer to base member to a pointer
944 to derived member. Note difference from simple pointer case, where
945 we can pass a pointer to derived to a pointer to base. */
946 if (comptypes (TYPE_OFFSET_BASETYPE (ttr
), TYPE_OFFSET_BASETYPE (ttl
), 0))
947 return comp_target_types (TREE_TYPE (ttl
), TREE_TYPE (ttr
), nptrs
);
948 else if (comptypes (TYPE_OFFSET_BASETYPE (ttl
), TYPE_OFFSET_BASETYPE (ttr
), 0)
949 && comp_target_types (TREE_TYPE (ttl
), TREE_TYPE (ttr
), nptrs
))
952 else if (IS_AGGR_TYPE (ttl
))
956 if (comptypes (build_pointer_type (ttl
), build_pointer_type (ttr
), 0))
958 if (comptypes (build_pointer_type (ttr
), build_pointer_type (ttl
), 0))
966 /* If two types share a common base type, return that basetype.
967 If there is not a unique most-derived base type, this function
968 returns ERROR_MARK_NODE. */
971 common_base_type (tt1
, tt2
)
974 tree best
= NULL_TREE
;
977 /* If one is a baseclass of another, that's good enough. */
978 if (UNIQUELY_DERIVED_FROM_P (tt1
, tt2
))
980 if (UNIQUELY_DERIVED_FROM_P (tt2
, tt1
))
983 /* Otherwise, try to find a unique baseclass of TT1
984 that is shared by TT2, and follow that down. */
985 for (i
= CLASSTYPE_N_BASECLASSES (tt1
)-1; i
>= 0; i
--)
987 tree basetype
= TYPE_BINFO_BASETYPE (tt1
, i
);
988 tree trial
= common_base_type (basetype
, tt2
);
991 if (trial
== error_mark_node
)
993 if (best
== NULL_TREE
)
995 else if (best
!= trial
)
996 return error_mark_node
;
1001 for (i
= CLASSTYPE_N_BASECLASSES (tt2
)-1; i
>= 0; i
--)
1003 tree basetype
= TYPE_BINFO_BASETYPE (tt2
, i
);
1004 tree trial
= common_base_type (tt1
, basetype
);
1007 if (trial
== error_mark_node
)
1009 if (best
== NULL_TREE
)
1011 else if (best
!= trial
)
1012 return error_mark_node
;
1018 /* Subroutines of `comptypes'. */
1020 /* Return 1 if two parameter type lists PARMS1 and PARMS2
1021 are equivalent in the sense that functions with those parameter types
1022 can have equivalent types.
1023 If either list is empty, we win.
1024 Otherwise, the two lists must be equivalent, element by element.
1026 C++: See comment above about TYPE1, TYPE2, STRICT.
1027 If STRICT == 3, it means checking is strict, but do not compare
1028 default parameter values. */
1031 compparms (parms1
, parms2
, strict
)
1032 tree parms1
, parms2
;
1035 register tree t1
= parms1
, t2
= parms2
;
1037 /* An unspecified parmlist matches any specified parmlist
1038 whose argument types don't need default promotions. */
1040 if (strict
<= 0 && t1
== 0)
1041 return self_promoting_args_p (t2
);
1042 if (strict
< 0 && t2
== 0)
1043 return self_promoting_args_p (t1
);
1047 if (t1
== 0 && t2
== 0)
1049 /* If one parmlist is shorter than the other,
1050 they fail to match, unless STRICT is <= 0. */
1051 if (t1
== 0 || t2
== 0)
1058 return t1
&& TREE_PURPOSE (t1
);
1060 if (! comptypes (TREE_VALUE (t2
), TREE_VALUE (t1
), strict
))
1065 return t2
== void_list_node
&& TREE_PURPOSE (t1
);
1066 return TREE_PURPOSE (t1
) || TREE_PURPOSE (t2
);
1069 t1
= TREE_CHAIN (t1
);
1070 t2
= TREE_CHAIN (t2
);
1074 /* This really wants return whether or not parameter type lists
1075 would make their owning functions assignment compatible or not. */
1078 comp_target_parms (parms1
, parms2
, strict
)
1079 tree parms1
, parms2
;
1082 register tree t1
= parms1
, t2
= parms2
;
1083 int warn_contravariance
= 0;
1085 /* An unspecified parmlist matches any specified parmlist
1086 whose argument types don't need default promotions.
1087 @@@ see 13.3.3 for a counterexample... */
1089 if (t1
== 0 && t2
!= 0)
1091 cp_pedwarn ("ANSI C++ prohibits conversion from `(%#T)' to `(...)'",
1093 return self_promoting_args_p (t2
);
1096 return self_promoting_args_p (t1
);
1098 for (; t1
|| t2
; t1
= TREE_CHAIN (t1
), t2
= TREE_CHAIN (t2
))
1102 /* If one parmlist is shorter than the other,
1103 they fail to match, unless STRICT is <= 0. */
1104 if (t1
== 0 || t2
== 0)
1109 return 1 + warn_contravariance
;
1110 return ((t1
&& TREE_PURPOSE (t1
)) + warn_contravariance
);
1112 p1
= TREE_VALUE (t1
);
1113 p2
= TREE_VALUE (t2
);
1117 if ((TREE_CODE (p1
) == POINTER_TYPE
&& TREE_CODE (p2
) == POINTER_TYPE
)
1118 || (TREE_CODE (p1
) == REFERENCE_TYPE
&& TREE_CODE (p2
) == REFERENCE_TYPE
))
1121 && (TYPE_MAIN_VARIANT (TREE_TYPE (p1
))
1122 == TYPE_MAIN_VARIANT (TREE_TYPE (p2
))))
1125 /* The following is wrong for contravariance,
1126 but many programs depend on it. */
1127 if (TREE_TYPE (p1
) == void_type_node
)
1129 if (TREE_TYPE (p2
) == void_type_node
)
1131 warn_contravariance
= 1;
1134 if (IS_AGGR_TYPE (TREE_TYPE (p1
)))
1136 if (comptypes (p2
, p1
, 0) == 0)
1138 if (comptypes (p1
, p2
, 0) != 0)
1139 warn_contravariance
= 1;
1146 /* Note backwards order due to contravariance. */
1147 if (comp_target_types (p2
, p1
, 1) == 0)
1149 if (comp_target_types (p1
, p2
, 1))
1151 warn_contravariance
= 1;
1157 /* Target types are compatible--just make sure that if
1158 we use parameter lists, that they are ok as well. */
1159 if (TREE_CODE (p1
) == FUNCTION_TYPE
|| TREE_CODE (p1
) == METHOD_TYPE
)
1160 switch (comp_target_parms (TYPE_ARG_TYPES (p1
),
1161 TYPE_ARG_TYPES (p2
),
1169 warn_contravariance
= 1;
1172 if (TREE_PURPOSE (t1
) && TREE_PURPOSE (t2
))
1174 int cmp
= simple_cst_equal (TREE_PURPOSE (t1
), TREE_PURPOSE (t2
));
1176 my_friendly_abort (114);
1181 return 1 + warn_contravariance
;
1184 /* Return 1 if PARMS specifies a fixed number of parameters
1185 and none of their types is affected by default promotions. */
1188 self_promoting_args_p (parms
)
1192 for (t
= parms
; t
; t
= TREE_CHAIN (t
))
1194 register tree type
= TREE_VALUE (t
);
1196 if (TREE_CHAIN (t
) == 0 && type
!= void_type_node
)
1202 if (TYPE_MAIN_VARIANT (type
) == float_type_node
)
1205 if (C_PROMOTING_INTEGER_TYPE_P (type
))
1211 /* Return an unsigned type the same as TYPE in other respects.
1213 C++: must make these work for type variants as well. */
1216 unsigned_type (type
)
1219 tree type1
= TYPE_MAIN_VARIANT (type
);
1220 if (type1
== signed_char_type_node
|| type1
== char_type_node
)
1221 return unsigned_char_type_node
;
1222 if (type1
== integer_type_node
)
1223 return unsigned_type_node
;
1224 if (type1
== short_integer_type_node
)
1225 return short_unsigned_type_node
;
1226 if (type1
== long_integer_type_node
)
1227 return long_unsigned_type_node
;
1228 if (type1
== long_long_integer_type_node
)
1229 return long_long_unsigned_type_node
;
1230 if (type1
== intDI_type_node
)
1231 return unsigned_intDI_type_node
;
1232 if (type1
== intSI_type_node
)
1233 return unsigned_intSI_type_node
;
1234 if (type1
== intHI_type_node
)
1235 return unsigned_intHI_type_node
;
1236 if (type1
== intQI_type_node
)
1237 return unsigned_intQI_type_node
;
1239 return signed_or_unsigned_type (1, type
);
1242 /* Return a signed type the same as TYPE in other respects. */
1248 tree type1
= TYPE_MAIN_VARIANT (type
);
1249 if (type1
== unsigned_char_type_node
|| type1
== char_type_node
)
1250 return signed_char_type_node
;
1251 if (type1
== unsigned_type_node
)
1252 return integer_type_node
;
1253 if (type1
== short_unsigned_type_node
)
1254 return short_integer_type_node
;
1255 if (type1
== long_unsigned_type_node
)
1256 return long_integer_type_node
;
1257 if (type1
== long_long_unsigned_type_node
)
1258 return long_long_integer_type_node
;
1259 if (type1
== unsigned_intDI_type_node
)
1260 return intDI_type_node
;
1261 if (type1
== unsigned_intSI_type_node
)
1262 return intSI_type_node
;
1263 if (type1
== unsigned_intHI_type_node
)
1264 return intHI_type_node
;
1265 if (type1
== unsigned_intQI_type_node
)
1266 return intQI_type_node
;
1268 return signed_or_unsigned_type (0, type
);
1271 /* Return a type the same as TYPE except unsigned or
1272 signed according to UNSIGNEDP. */
1275 signed_or_unsigned_type (unsignedp
, type
)
1279 if (! INTEGRAL_TYPE_P (type
)
1280 || TREE_UNSIGNED (type
) == unsignedp
)
1283 if (TYPE_PRECISION (type
) == TYPE_PRECISION (signed_char_type_node
))
1284 return unsignedp
? unsigned_char_type_node
: signed_char_type_node
;
1285 if (TYPE_PRECISION (type
) == TYPE_PRECISION (integer_type_node
))
1286 return unsignedp
? unsigned_type_node
: integer_type_node
;
1287 if (TYPE_PRECISION (type
) == TYPE_PRECISION (short_integer_type_node
))
1288 return unsignedp
? short_unsigned_type_node
: short_integer_type_node
;
1289 if (TYPE_PRECISION (type
) == TYPE_PRECISION (long_integer_type_node
))
1290 return unsignedp
? long_unsigned_type_node
: long_integer_type_node
;
1291 if (TYPE_PRECISION (type
) == TYPE_PRECISION (long_long_integer_type_node
))
1292 return (unsignedp
? long_long_unsigned_type_node
1293 : long_long_integer_type_node
);
1297 /* Compute the value of the `sizeof' operator. */
1303 enum tree_code code
= TREE_CODE (type
);
1306 if (processing_template_decl
)
1307 return build_min (SIZEOF_EXPR
, sizetype
, type
);
1309 if (code
== FUNCTION_TYPE
)
1311 if (pedantic
|| warn_pointer_arith
)
1312 pedwarn ("ANSI C++ forbids taking the sizeof a function type");
1313 return size_int (1);
1315 if (code
== METHOD_TYPE
)
1317 if (pedantic
|| warn_pointer_arith
)
1318 pedwarn ("ANSI C++ forbids taking the sizeof a method type");
1319 return size_int (1);
1321 if (code
== VOID_TYPE
)
1323 if (pedantic
|| warn_pointer_arith
)
1324 pedwarn ("ANSI C++ forbids taking the sizeof a void type");
1325 return size_int (1);
1327 if (code
== ERROR_MARK
)
1328 return size_int (1);
1330 /* ARM $5.3.2: ``When applied to a reference, the result is the size of the
1331 referenced object.'' */
1332 if (code
== REFERENCE_TYPE
)
1333 type
= TREE_TYPE (type
);
1335 /* We couldn't find anything in the ARM or the draft standard that says,
1336 one way or the other, if doing sizeof on something that doesn't have
1337 an object associated with it is correct or incorrect. For example, if
1338 you declare `struct S { char str[16]; };', and in your program do
1339 a `sizeof (S::str)', should we flag that as an error or should we give
1340 the size of it? Since it seems like a reasonable thing to do, we'll go
1341 with giving the value. */
1342 if (code
== OFFSET_TYPE
)
1343 type
= TREE_TYPE (type
);
1345 /* @@ This also produces an error for a signature ref.
1346 In that case we should be able to do better. */
1347 if (IS_SIGNATURE (type
))
1349 error ("`sizeof' applied to a signature type");
1350 return size_int (0);
1353 if (TYPE_SIZE (complete_type (type
)) == 0)
1355 cp_error ("`sizeof' applied to incomplete type `%T'", type
);
1356 return size_int (0);
1359 /* Convert in case a char is more than one unit. */
1360 t
= size_binop (CEIL_DIV_EXPR
, TYPE_SIZE (type
),
1361 size_int (TYPE_PRECISION (char_type_node
)));
1362 /* size_binop does not put the constant in range, so do it now. */
1363 if (TREE_CODE (t
) == INTEGER_CST
&& force_fit_type (t
, 0))
1364 TREE_CONSTANT_OVERFLOW (t
) = TREE_OVERFLOW (t
) = 1;
1372 if (processing_template_decl
)
1373 return build_min (SIZEOF_EXPR
, sizetype
, e
);
1375 if (TREE_CODE (e
) == COMPONENT_REF
1376 && DECL_BIT_FIELD (TREE_OPERAND (e
, 1)))
1377 error ("sizeof applied to a bit-field");
1378 /* ANSI says arrays and functions are converted inside comma.
1379 But we can't really convert them in build_compound_expr
1380 because that would break commas in lvalues.
1381 So do the conversion here if operand was a comma. */
1382 if (TREE_CODE (e
) == COMPOUND_EXPR
1383 && (TREE_CODE (TREE_TYPE (e
)) == ARRAY_TYPE
1384 || TREE_CODE (TREE_TYPE (e
)) == FUNCTION_TYPE
))
1385 e
= default_conversion (e
);
1386 else if (TREE_CODE (e
) == TREE_LIST
)
1388 tree t
= TREE_VALUE (e
);
1391 && TREE_CODE (TREE_TYPE (t
)) == FUNCTION_TYPE
)
1392 || is_overloaded_fn (t
)))
1393 pedwarn ("ANSI C++ forbids taking the sizeof a function type");
1395 return c_sizeof (TREE_TYPE (e
));
1399 c_sizeof_nowarn (type
)
1402 enum tree_code code
= TREE_CODE (type
);
1405 if (code
== FUNCTION_TYPE
1406 || code
== METHOD_TYPE
1407 || code
== VOID_TYPE
1408 || code
== ERROR_MARK
)
1409 return size_int (1);
1410 if (code
== REFERENCE_TYPE
)
1411 type
= TREE_TYPE (type
);
1413 if (TYPE_SIZE (type
) == 0)
1414 return size_int (0);
1416 /* Convert in case a char is more than one unit. */
1417 t
= size_binop (CEIL_DIV_EXPR
, TYPE_SIZE (type
),
1418 size_int (TYPE_PRECISION (char_type_node
)));
1419 force_fit_type (t
, 0);
1423 /* Implement the __alignof keyword: Return the minimum required
1424 alignment of TYPE, measured in bytes. */
1430 enum tree_code code
= TREE_CODE (type
);
1433 if (code
== FUNCTION_TYPE
|| code
== METHOD_TYPE
)
1434 return size_int (FUNCTION_BOUNDARY
/ BITS_PER_UNIT
);
1436 if (code
== VOID_TYPE
|| code
== ERROR_MARK
)
1437 return size_int (1);
1439 /* C++: this is really correct! */
1440 if (code
== REFERENCE_TYPE
)
1441 type
= TREE_TYPE (type
);
1443 /* @@ This also produces an error for a signature ref.
1444 In that case we should be able to do better. */
1445 if (IS_SIGNATURE (type
))
1447 error ("`__alignof' applied to a signature type");
1448 return size_int (1);
1451 t
= size_int (TYPE_ALIGN (type
) / BITS_PER_UNIT
);
1452 force_fit_type (t
, 0);
1456 /* Perform default promotions for C data used in expressions.
1457 Arrays and functions are converted to pointers;
1458 enumeral types or short or char, to int.
1459 In addition, manifest constants symbols are replaced by their values.
1461 C++: this will automatically bash references to their target type. */
1464 decay_conversion (exp
)
1467 register tree type
= TREE_TYPE (exp
);
1468 register enum tree_code code
= TREE_CODE (type
);
1470 if (code
== OFFSET_TYPE
)
1472 if (TREE_CODE (exp
) == OFFSET_REF
)
1473 return decay_conversion (resolve_offset_ref (exp
));
1475 type
= TREE_TYPE (type
);
1476 code
= TREE_CODE (type
);
1478 if (type
== unknown_type_node
)
1480 cp_pedwarn ("assuming & on overloaded member function");
1481 return build_unary_op (ADDR_EXPR
, exp
, 0);
1485 if (code
== REFERENCE_TYPE
)
1487 exp
= convert_from_reference (exp
);
1488 type
= TREE_TYPE (exp
);
1489 code
= TREE_CODE (type
);
1492 /* Constants can be used directly unless they're not loadable. */
1493 if (TREE_CODE (exp
) == CONST_DECL
)
1494 exp
= DECL_INITIAL (exp
);
1495 /* Replace a nonvolatile const static variable with its value. */
1496 else if (TREE_READONLY_DECL_P (exp
))
1498 exp
= decl_constant_value (exp
);
1499 type
= TREE_TYPE (exp
);
1502 /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
1503 Leave such NOP_EXPRs, since RHS is being used in non-lvalue context. */
1505 if (code
== VOID_TYPE
)
1507 error ("void value not ignored as it ought to be");
1508 return error_mark_node
;
1510 if (code
== FUNCTION_TYPE
)
1512 return build_unary_op (ADDR_EXPR
, exp
, 0);
1514 if (code
== METHOD_TYPE
)
1516 cp_pedwarn ("assuming & on `%E'", exp
);
1517 return build_unary_op (ADDR_EXPR
, exp
, 0);
1519 if (code
== ARRAY_TYPE
)
1524 int constp
, volatilep
;
1526 if (TREE_CODE (exp
) == INDIRECT_REF
)
1528 /* Stripping away the INDIRECT_REF is not the right
1529 thing to do for references... */
1530 tree inner
= TREE_OPERAND (exp
, 0);
1531 if (TREE_CODE (TREE_TYPE (inner
)) == REFERENCE_TYPE
)
1533 inner
= build1 (CONVERT_EXPR
,
1534 build_pointer_type (TREE_TYPE (TREE_TYPE (inner
))),
1536 TREE_CONSTANT (inner
) = TREE_CONSTANT (TREE_OPERAND (inner
, 0));
1538 return cp_convert (build_pointer_type (TREE_TYPE (type
)), inner
);
1541 if (TREE_CODE (exp
) == COMPOUND_EXPR
)
1543 tree op1
= decay_conversion (TREE_OPERAND (exp
, 1));
1544 return build (COMPOUND_EXPR
, TREE_TYPE (op1
),
1545 TREE_OPERAND (exp
, 0), op1
);
1549 && ! (TREE_CODE (exp
) == CONSTRUCTOR
&& TREE_STATIC (exp
)))
1551 error ("invalid use of non-lvalue array");
1552 return error_mark_node
;
1555 constp
= volatilep
= 0;
1556 if (TREE_CODE_CLASS (TREE_CODE (exp
)) == 'r'
1557 || TREE_CODE_CLASS (TREE_CODE (exp
)) == 'd')
1559 constp
= TREE_READONLY (exp
);
1560 volatilep
= TREE_THIS_VOLATILE (exp
);
1563 restype
= TREE_TYPE (type
);
1564 if (TYPE_READONLY (type
) || TYPE_VOLATILE (type
)
1565 || constp
|| volatilep
)
1566 restype
= cp_build_type_variant (restype
,
1567 TYPE_READONLY (type
) || constp
,
1568 TYPE_VOLATILE (type
) || volatilep
);
1569 ptrtype
= build_pointer_type (restype
);
1571 if (TREE_CODE (exp
) == VAR_DECL
)
1573 /* ??? This is not really quite correct
1574 in that the type of the operand of ADDR_EXPR
1575 is not the target type of the type of the ADDR_EXPR itself.
1576 Question is, can this lossage be avoided? */
1577 adr
= build1 (ADDR_EXPR
, ptrtype
, exp
);
1578 if (mark_addressable (exp
) == 0)
1579 return error_mark_node
;
1580 TREE_CONSTANT (adr
) = staticp (exp
);
1581 TREE_SIDE_EFFECTS (adr
) = 0; /* Default would be, same as EXP. */
1584 /* This way is better for a COMPONENT_REF since it can
1585 simplify the offset for a component. */
1586 adr
= build_unary_op (ADDR_EXPR
, exp
, 1);
1587 return cp_convert (ptrtype
, adr
);
1594 default_conversion (exp
)
1598 enum tree_code code
;
1600 exp
= decay_conversion (exp
);
1602 type
= TREE_TYPE (exp
);
1603 code
= TREE_CODE (type
);
1605 if (INTEGRAL_CODE_P (code
))
1607 tree t
= type_promotes_to (type
);
1609 return cp_convert (t
, exp
);
1615 /* Take the address of an inline function without setting TREE_ADDRESSABLE
1619 inline_conversion (exp
)
1622 if (TREE_CODE (exp
) == FUNCTION_DECL
)
1624 tree type
= build_type_variant
1625 (TREE_TYPE (exp
), TREE_READONLY (exp
), TREE_THIS_VOLATILE (exp
));
1626 exp
= build1 (ADDR_EXPR
, build_pointer_type (type
), exp
);
1632 build_object_ref (datum
, basetype
, field
)
1633 tree datum
, basetype
, field
;
1636 if (datum
== error_mark_node
)
1637 return error_mark_node
;
1639 dtype
= TREE_TYPE (datum
);
1640 if (TREE_CODE (dtype
) == REFERENCE_TYPE
)
1641 dtype
= TREE_TYPE (dtype
);
1642 if (! IS_AGGR_TYPE_CODE (TREE_CODE (dtype
)))
1644 cp_error ("request for member `%T::%D' in expression of non-aggregate type `%T'",
1645 basetype
, field
, dtype
);
1646 return error_mark_node
;
1648 else if (IS_SIGNATURE (basetype
))
1650 warning ("signature name in scope resolution ignored");
1651 return build_component_ref (datum
, field
, NULL_TREE
, 1);
1653 else if (is_aggr_type (basetype
, 1))
1655 tree binfo
= binfo_or_else (basetype
, dtype
);
1657 return build_x_component_ref (build_scoped_ref (datum
, basetype
),
1660 return error_mark_node
;
1663 /* Like `build_component_ref, but uses an already found field, and converts
1664 from a reference. Must compute access for current_class_ref.
1668 build_component_ref_1 (datum
, field
, protect
)
1672 return convert_from_reference
1673 (build_component_ref (datum
, field
, NULL_TREE
, protect
));
1676 /* Given a COND_EXPR in T, return it in a form that we can, for
1677 example, use as an lvalue. This code used to be in unary_complex_lvalue,
1678 but we needed it to deal with `a = (d == c) ? b : c' expressions, where
1679 we're dealing with aggregates. So, we now call this in unary_complex_lvalue,
1680 and in build_modify_expr. The case (in particular) that led to this was
1681 with CODE == ADDR_EXPR, since it's not an lvalue when we'd get it there. */
1684 rationalize_conditional_expr (code
, t
)
1685 enum tree_code code
;
1689 build_conditional_expr (TREE_OPERAND (t
, 0),
1690 build_unary_op (code
, TREE_OPERAND (t
, 1), 0),
1691 build_unary_op (code
, TREE_OPERAND (t
, 2), 0));
1694 /* Given the TYPE of an anonymous union field inside T, return the
1695 FIELD_DECL for the field. If not found return NULL_TREE. Because
1696 anonymous unions can nest, we must also search all anonymous unions
1697 that are directly reachable. */
1700 lookup_anon_field (t
, type
)
1705 for (field
= TYPE_FIELDS (t
); field
; field
= TREE_CHAIN (field
))
1707 if (TREE_STATIC (field
))
1709 if (TREE_CODE (field
) != FIELD_DECL
)
1712 /* If we find it directly, return the field. */
1713 if (DECL_NAME (field
) == NULL_TREE
1714 && type
== TREE_TYPE (field
))
1719 /* Otherwise, it could be nested, search harder. */
1720 if (DECL_NAME (field
) == NULL_TREE
1721 && TREE_CODE (TREE_TYPE (field
)) == UNION_TYPE
)
1723 tree subfield
= lookup_anon_field (TREE_TYPE (field
), type
);
1731 /* Build a COMPONENT_REF for a given DATUM, and it's member COMPONENT.
1732 COMPONENT can be an IDENTIFIER_NODE that is the name of the member
1733 that we are interested in, or it can be a FIELD_DECL. */
1736 build_component_ref (datum
, component
, basetype_path
, protect
)
1737 tree datum
, component
, basetype_path
;
1740 register tree basetype
= TREE_TYPE (datum
);
1741 register enum tree_code code
;
1742 register tree field
= NULL
;
1745 if (processing_template_decl
)
1746 return build_min_nt (COMPONENT_REF
, datum
, component
);
1748 /* If DATUM is a COMPOUND_EXPR or COND_EXPR, move our reference
1750 switch (TREE_CODE (datum
))
1754 tree value
= build_component_ref (TREE_OPERAND (datum
, 1), component
,
1755 basetype_path
, protect
);
1756 return build (COMPOUND_EXPR
, TREE_TYPE (value
),
1757 TREE_OPERAND (datum
, 0), value
);
1760 return build_conditional_expr
1761 (TREE_OPERAND (datum
, 0),
1762 build_component_ref (TREE_OPERAND (datum
, 1), component
,
1763 basetype_path
, protect
),
1764 build_component_ref (TREE_OPERAND (datum
, 2), component
,
1765 basetype_path
, protect
));
1768 code
= TREE_CODE (basetype
);
1770 if (code
== REFERENCE_TYPE
)
1772 datum
= convert_from_reference (datum
);
1773 basetype
= TREE_TYPE (datum
);
1774 code
= TREE_CODE (basetype
);
1776 if (TREE_CODE (datum
) == OFFSET_REF
)
1778 datum
= resolve_offset_ref (datum
);
1779 basetype
= TREE_TYPE (datum
);
1780 code
= TREE_CODE (basetype
);
1783 /* First, see if there is a field or component with name COMPONENT. */
1784 if (TREE_CODE (component
) == TREE_LIST
)
1786 my_friendly_assert (!(TREE_CHAIN (component
) == NULL_TREE
1787 && DECL_CHAIN (TREE_VALUE (component
)) == NULL_TREE
), 309);
1788 return build (COMPONENT_REF
, TREE_TYPE (component
), datum
, component
);
1791 if (! IS_AGGR_TYPE_CODE (code
))
1793 if (code
!= ERROR_MARK
)
1794 cp_error ("request for member `%D' in `%E', which is of non-aggregate type `%T'",
1795 component
, datum
, basetype
);
1796 return error_mark_node
;
1799 if (TYPE_SIZE (complete_type (basetype
)) == 0)
1801 incomplete_type_error (0, basetype
);
1802 return error_mark_node
;
1805 if (TREE_CODE (component
) == BIT_NOT_EXPR
)
1807 if (TYPE_IDENTIFIER (basetype
) != TREE_OPERAND (component
, 0))
1809 cp_error ("destructor specifier `%T::~%T' must have matching names",
1810 basetype
, TREE_OPERAND (component
, 0));
1811 return error_mark_node
;
1813 if (! TYPE_HAS_DESTRUCTOR (basetype
))
1815 cp_error ("type `%T' has no destructor", basetype
);
1816 return error_mark_node
;
1818 return TREE_VEC_ELT (CLASSTYPE_METHOD_VEC (basetype
), 1);
1821 /* Look up component name in the structure type definition. */
1822 if (CLASSTYPE_VFIELD (basetype
)
1823 && DECL_NAME (CLASSTYPE_VFIELD (basetype
)) == component
)
1824 /* Special-case this because if we use normal lookups in an ambiguous
1825 hierarchy, the compiler will abort (because vptr lookups are
1826 not supposed to be ambiguous. */
1827 field
= CLASSTYPE_VFIELD (basetype
);
1828 else if (TREE_CODE (component
) == FIELD_DECL
1829 || TREE_CODE (component
) == TYPE_DECL
)
1835 tree name
= component
;
1836 if (TREE_CODE (component
) == VAR_DECL
)
1837 name
= DECL_NAME (component
);
1838 if (basetype_path
== NULL_TREE
)
1839 basetype_path
= TYPE_BINFO (basetype
);
1840 field
= lookup_field (basetype_path
, name
,
1841 protect
&& !VFIELD_NAME_P (name
), 0);
1842 if (field
== error_mark_node
)
1843 return error_mark_node
;
1845 if (field
== NULL_TREE
)
1847 /* Not found as a data field, look for it as a method. If found,
1848 then if this is the only possible one, return it, else
1849 report ambiguity error. */
1850 tree fndecls
= lookup_fnfields (basetype_path
, name
, 1);
1851 if (fndecls
== error_mark_node
)
1852 return error_mark_node
;
1855 if (TREE_CHAIN (fndecls
) == NULL_TREE
1856 && DECL_CHAIN (TREE_VALUE (fndecls
)) == NULL_TREE
)
1858 tree access
, fndecl
;
1860 /* Unique, so use this one now. */
1861 basetype
= TREE_PURPOSE (fndecls
);
1862 fndecl
= TREE_VALUE (fndecls
);
1863 access
= compute_access (TREE_PURPOSE (fndecls
), fndecl
);
1864 if (access
== access_public_node
)
1866 if (DECL_VINDEX (fndecl
)
1867 && ! resolves_to_fixed_type_p (datum
, 0))
1869 tree addr
= build_unary_op (ADDR_EXPR
, datum
, 0);
1870 tree fntype
= TREE_TYPE (fndecl
);
1872 addr
= convert_pointer_to (DECL_CONTEXT (fndecl
), addr
);
1873 datum
= build_indirect_ref (addr
, NULL_PTR
);
1874 my_friendly_assert (datum
!= error_mark_node
, 310);
1875 fndecl
= build_vfn_ref (&addr
, datum
, DECL_VINDEX (fndecl
));
1876 TREE_TYPE (fndecl
) = build_pointer_type (fntype
);
1880 return build (OFFSET_REF
, TREE_TYPE (fndecl
), datum
, fndecl
);
1882 if (access
== access_protected_node
)
1883 cp_error ("member function `%D' is protected", fndecl
);
1885 cp_error ("member function `%D' is private", fndecl
);
1886 return error_mark_node
;
1890 /* Just act like build_offset_ref, since the object does
1891 not matter unless we're actually calling the function. */
1894 t
= build_tree_list (error_mark_node
, fndecls
);
1895 TREE_TYPE (t
) = build_offset_type (basetype
,
1901 cp_error ("`%#T' has no member named `%D'", basetype
, name
);
1902 return error_mark_node
;
1904 else if (TREE_TYPE (field
) == error_mark_node
)
1905 return error_mark_node
;
1907 if (TREE_CODE (field
) != FIELD_DECL
)
1909 if (TREE_CODE (field
) == TYPE_DECL
)
1911 cp_error ("invalid use of type decl `%#D' as expression", field
);
1912 return error_mark_node
;
1914 else if (DECL_RTL (field
) != 0)
1917 TREE_USED (field
) = 1;
1922 /* See if we have to do any conversions so that we pick up the field from the
1924 if (DECL_FIELD_CONTEXT (field
) != basetype
)
1926 tree context
= DECL_FIELD_CONTEXT (field
);
1927 tree base
= context
;
1928 while (base
!= basetype
&& TYPE_NAME (base
)
1929 && ANON_AGGRNAME_P (TYPE_IDENTIFIER (base
)))
1931 base
= TYPE_CONTEXT (base
);
1934 /* Handle base classes here... */
1935 if (base
!= basetype
&& TYPE_USES_COMPLEX_INHERITANCE (basetype
))
1937 tree addr
= build_unary_op (ADDR_EXPR
, datum
, 0);
1938 if (integer_zerop (addr
))
1940 error ("invalid reference to NULL ptr, use ptr-to-member instead");
1941 return error_mark_node
;
1943 if (VBASE_NAME_P (DECL_NAME (field
)))
1945 /* It doesn't matter which vbase pointer we grab, just
1946 find one of them. */
1947 tree binfo
= get_binfo (base
,
1948 TREE_TYPE (TREE_TYPE (addr
)), 0);
1949 addr
= convert_pointer_to_real (binfo
, addr
);
1952 addr
= convert_pointer_to (base
, addr
);
1953 datum
= build_indirect_ref (addr
, NULL_PTR
);
1954 my_friendly_assert (datum
!= error_mark_node
, 311);
1958 /* Handle things from anon unions here... */
1959 if (TYPE_NAME (context
) && ANON_AGGRNAME_P (TYPE_IDENTIFIER (context
)))
1961 tree subfield
= lookup_anon_field (basetype
, context
);
1962 tree subdatum
= build_component_ref (datum
, subfield
,
1963 basetype_path
, protect
);
1964 return build_component_ref (subdatum
, field
, basetype_path
, protect
);
1968 ref
= fold (build (COMPONENT_REF
, TREE_TYPE (field
),
1969 break_out_cleanups (datum
), field
));
1971 if (TREE_READONLY (datum
) || TREE_READONLY (field
))
1972 TREE_READONLY (ref
) = 1;
1973 if (TREE_THIS_VOLATILE (datum
) || TREE_THIS_VOLATILE (field
))
1974 TREE_THIS_VOLATILE (ref
) = 1;
1975 if (DECL_MUTABLE_P (field
))
1976 TREE_READONLY (ref
) = 0;
1981 /* Variant of build_component_ref for use in expressions, which should
1982 never have REFERENCE_TYPE. */
1985 build_x_component_ref (datum
, component
, basetype_path
, protect
)
1986 tree datum
, component
, basetype_path
;
1989 tree t
= build_component_ref (datum
, component
, basetype_path
, protect
);
1991 if (! processing_template_decl
)
1992 t
= convert_from_reference (t
);
1997 /* Given an expression PTR for a pointer, return an expression
1998 for the value pointed to.
1999 ERRORSTRING is the name of the operator to appear in error messages.
2001 This function may need to overload OPERATOR_FNNAME.
2002 Must also handle REFERENCE_TYPEs for C++. */
2005 build_x_indirect_ref (ptr
, errorstring
)
2011 if (processing_template_decl
)
2012 return build_min_nt (INDIRECT_REF
, ptr
);
2014 rval
= build_opfncall (INDIRECT_REF
, LOOKUP_NORMAL
, ptr
, NULL_TREE
, NULL_TREE
);
2017 return build_indirect_ref (ptr
, errorstring
);
2021 build_indirect_ref (ptr
, errorstring
)
2025 register tree pointer
, type
;
2027 if (ptr
== error_mark_node
)
2028 return error_mark_node
;
2030 pointer
= (TREE_CODE (TREE_TYPE (ptr
)) == REFERENCE_TYPE
2031 ? ptr
: default_conversion (ptr
));
2032 type
= TREE_TYPE (pointer
);
2034 if (ptr
== current_class_ptr
)
2035 return current_class_ref
;
2037 if (IS_AGGR_TYPE (type
))
2039 ptr
= build_expr_type_conversion (WANT_POINTER
, pointer
, 1);
2044 type
= TREE_TYPE (pointer
);
2048 if (TREE_CODE (type
) == POINTER_TYPE
|| TREE_CODE (type
) == REFERENCE_TYPE
)
2050 if (TREE_CODE (pointer
) == ADDR_EXPR
2052 && (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_OPERAND (pointer
, 0)))
2053 == TYPE_MAIN_VARIANT (TREE_TYPE (type
)))
2054 && (TREE_READONLY (TREE_OPERAND (pointer
, 0))
2055 == TYPE_READONLY (TREE_TYPE (type
)))
2056 && (TREE_THIS_VOLATILE (TREE_OPERAND (pointer
, 0))
2057 == TYPE_VOLATILE (TREE_TYPE (type
))))
2058 return TREE_OPERAND (pointer
, 0);
2061 tree t
= TREE_TYPE (type
);
2062 register tree ref
= build1 (INDIRECT_REF
,
2063 TYPE_MAIN_VARIANT (t
), pointer
);
2065 /* We *must* set TREE_READONLY when dereferencing a pointer to const,
2066 so that we get the proper error message if the result is used
2067 to assign to. Also, &* is supposed to be a no-op. */
2068 TREE_READONLY (ref
) = TYPE_READONLY (t
);
2069 TREE_SIDE_EFFECTS (ref
)
2070 = TYPE_VOLATILE (t
) || TREE_SIDE_EFFECTS (pointer
) || flag_volatile
;
2071 TREE_THIS_VOLATILE (ref
) = TYPE_VOLATILE (t
);
2075 /* `pointer' won't be an error_mark_node if we were given a
2076 pointer to member, so it's cool to check for this here. */
2077 else if (TYPE_PTRMEMFUNC_P (type
))
2078 error ("invalid use of `%s' on pointer to member function", errorstring
);
2079 else if (TREE_CODE (type
) == RECORD_TYPE
2080 && (IS_SIGNATURE_POINTER (type
) || IS_SIGNATURE_REFERENCE (type
)))
2081 error ("cannot dereference signature pointer/reference");
2082 else if (pointer
!= error_mark_node
)
2085 error ("invalid type argument of `%s'", errorstring
);
2087 error ("invalid type argument");
2089 return error_mark_node
;
2092 /* This handles expressions of the form "a[i]", which denotes
2095 This is logically equivalent in C to *(a+i), but we may do it differently.
2096 If A is a variable or a member, we generate a primitive ARRAY_REF.
2097 This avoids forcing the array out of registers, and can work on
2098 arrays that are not lvalues (for example, members of structures returned
2101 If INDEX is of some user-defined type, it must be converted to
2102 integer type. Otherwise, to make a compatible PLUS_EXPR, it
2103 will inherit the type of the array, which will be some pointer type. */
2106 build_array_ref (array
, idx
)
2111 error ("subscript missing in array reference");
2112 return error_mark_node
;
2115 if (TREE_TYPE (array
) == error_mark_node
2116 || TREE_TYPE (idx
) == error_mark_node
)
2117 return error_mark_node
;
2119 if (TREE_CODE (TREE_TYPE (array
)) == ARRAY_TYPE
2120 && TREE_CODE (array
) != INDIRECT_REF
)
2124 /* Subscripting with type char is likely to lose
2125 on a machine where chars are signed.
2126 So warn on any machine, but optionally.
2127 Don't warn for unsigned char since that type is safe.
2128 Don't warn for signed char because anyone who uses that
2129 must have done so deliberately. */
2130 if (warn_char_subscripts
2131 && TYPE_MAIN_VARIANT (TREE_TYPE (idx
)) == char_type_node
)
2132 warning ("array subscript has type `char'");
2134 /* Apply default promotions *after* noticing character types. */
2135 idx
= default_conversion (idx
);
2137 if (TREE_CODE (TREE_TYPE (idx
)) != INTEGER_TYPE
)
2139 error ("array subscript is not an integer");
2140 return error_mark_node
;
2143 /* An array that is indexed by a non-constant
2144 cannot be stored in a register; we must be able to do
2145 address arithmetic on its address.
2146 Likewise an array of elements of variable size. */
2147 if (TREE_CODE (idx
) != INTEGER_CST
2148 || (TYPE_SIZE (TREE_TYPE (TREE_TYPE (array
))) != 0
2149 && TREE_CODE (TYPE_SIZE (TREE_TYPE (TREE_TYPE (array
)))) != INTEGER_CST
))
2151 if (mark_addressable (array
) == 0)
2152 return error_mark_node
;
2154 /* An array that is indexed by a constant value which is not within
2155 the array bounds cannot be stored in a register either; because we
2156 would get a crash in store_bit_field/extract_bit_field when trying
2157 to access a non-existent part of the register. */
2158 if (TREE_CODE (idx
) == INTEGER_CST
2159 && TYPE_VALUES (TREE_TYPE (array
))
2160 && ! int_fits_type_p (idx
, TYPE_VALUES (TREE_TYPE (array
))))
2162 if (mark_addressable (array
) == 0)
2163 return error_mark_node
;
2166 if (pedantic
&& !lvalue_p (array
))
2167 pedwarn ("ANSI C++ forbids subscripting non-lvalue array");
2169 /* Note in C++ it is valid to subscript a `register' array, since
2170 it is valid to take the address of something with that
2171 storage specification. */
2175 while (TREE_CODE (foo
) == COMPONENT_REF
)
2176 foo
= TREE_OPERAND (foo
, 0);
2177 if (TREE_CODE (foo
) == VAR_DECL
&& DECL_REGISTER (foo
))
2178 warning ("subscripting array declared `register'");
2181 type
= TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (array
)));
2182 rval
= build (ARRAY_REF
, type
, array
, idx
);
2183 /* Array ref is const/volatile if the array elements are
2184 or if the array is.. */
2185 TREE_READONLY (rval
)
2186 |= (TYPE_READONLY (TREE_TYPE (TREE_TYPE (array
)))
2187 | TREE_READONLY (array
));
2188 TREE_SIDE_EFFECTS (rval
)
2189 |= (TYPE_VOLATILE (TREE_TYPE (TREE_TYPE (array
)))
2190 | TREE_SIDE_EFFECTS (array
));
2191 TREE_THIS_VOLATILE (rval
)
2192 |= (TYPE_VOLATILE (TREE_TYPE (TREE_TYPE (array
)))
2193 /* This was added by rms on 16 Nov 91.
2194 It fixes vol struct foo *a; a->elts[1]
2195 in an inline function.
2196 Hope it doesn't break something else. */
2197 | TREE_THIS_VOLATILE (array
));
2198 return require_complete_type (fold (rval
));
2202 tree ar
= default_conversion (array
);
2203 tree ind
= default_conversion (idx
);
2205 /* Put the integer in IND to simplify error checking. */
2206 if (TREE_CODE (TREE_TYPE (ar
)) == INTEGER_TYPE
)
2213 if (ar
== error_mark_node
)
2216 if (TREE_CODE (TREE_TYPE (ar
)) != POINTER_TYPE
)
2218 error ("subscripted value is neither array nor pointer");
2219 return error_mark_node
;
2221 if (TREE_CODE (TREE_TYPE (ind
)) != INTEGER_TYPE
)
2223 error ("array subscript is not an integer");
2224 return error_mark_node
;
2227 return build_indirect_ref (build_binary_op_nodefault (PLUS_EXPR
, ar
, ind
, PLUS_EXPR
),
2232 /* Build a function call to function FUNCTION with parameters PARAMS.
2233 PARAMS is a list--a chain of TREE_LIST nodes--in which the
2234 TREE_VALUE of each node is a parameter-expression. The PARAMS do
2235 not include any object pointer that may be required. FUNCTION's
2236 data type may be a function type or a pointer-to-function.
2238 For C++: If FUNCTION's data type is a TREE_LIST, then the tree list
2239 is the list of possible methods that FUNCTION could conceivably
2240 be. If the list of methods comes from a class, then it will be
2241 a list of lists (where each element is associated with the class
2242 that produced it), otherwise it will be a simple list (for
2243 functions overloaded in global scope).
2245 In the first case, TREE_VALUE (function) is the head of one of those
2246 lists, and TREE_PURPOSE is the name of the function.
2248 In the second case, TREE_PURPOSE (function) is the function's
2251 DECL is the class instance variable, usually CURRENT_CLASS_REF.
2253 When calling a TEMPLATE_DECL, we don't require a complete return
2257 build_x_function_call (function
, params
, decl
)
2258 tree function
, params
, decl
;
2263 if (function
== error_mark_node
)
2264 return error_mark_node
;
2266 if (processing_template_decl
)
2267 return build_min_nt (CALL_EXPR
, function
, params
, NULL_TREE
);
2269 type
= TREE_TYPE (function
);
2271 if (TREE_CODE (type
) == OFFSET_TYPE
2272 && TREE_TYPE (type
) == unknown_type_node
2273 && TREE_CODE (function
) == TREE_LIST
2274 && TREE_CHAIN (function
) == NULL_TREE
)
2276 /* Undo (Foo:bar)()... */
2277 type
= TYPE_OFFSET_BASETYPE (type
);
2278 function
= TREE_VALUE (function
);
2279 my_friendly_assert (TREE_CODE (function
) == TREE_LIST
, 999);
2280 my_friendly_assert (TREE_CHAIN (function
) == NULL_TREE
, 999);
2281 function
= TREE_VALUE (function
);
2282 my_friendly_assert (TREE_CODE (function
) == FUNCTION_DECL
, 999);
2283 function
= DECL_NAME (function
);
2284 return build_method_call (decl
, function
, params
, TYPE_BINFO (type
), LOOKUP_NORMAL
);
2287 is_method
= ((TREE_CODE (function
) == TREE_LIST
2288 && current_class_type
!= NULL_TREE
2289 && IDENTIFIER_CLASS_VALUE (TREE_PURPOSE (function
)) == function
)
2290 || TREE_CODE (function
) == IDENTIFIER_NODE
2291 || TREE_CODE (type
) == METHOD_TYPE
2292 || TYPE_PTRMEMFUNC_P (type
));
2294 if (TREE_CODE (function
) == FUNCTION_DECL
2295 && DECL_STATIC_FUNCTION_P (function
))
2296 return build_member_call
2297 (DECL_CONTEXT (function
), DECL_NAME (function
), params
);
2299 /* Handle methods, friends, and overloaded functions, respectively. */
2302 tree basetype
= NULL_TREE
;
2304 if (TREE_CODE (function
) == FUNCTION_DECL
2305 || DECL_FUNCTION_TEMPLATE_P (function
))
2307 basetype
= DECL_CLASS_CONTEXT (function
);
2309 if (DECL_NAME (function
))
2310 function
= DECL_NAME (function
);
2312 function
= TYPE_IDENTIFIER (DECL_CLASS_CONTEXT (function
));
2314 else if (TREE_CODE (function
) == TREE_LIST
)
2316 my_friendly_assert (TREE_CODE (TREE_VALUE (function
)) == FUNCTION_DECL
, 312);
2317 basetype
= DECL_CLASS_CONTEXT (TREE_VALUE (function
));
2318 function
= TREE_PURPOSE (function
);
2320 else if (TREE_CODE (function
) != IDENTIFIER_NODE
)
2322 if (TREE_CODE (function
) == OFFSET_REF
)
2324 if (TREE_OPERAND (function
, 0))
2325 decl
= TREE_OPERAND (function
, 0);
2327 /* Call via a pointer to member function. */
2328 if (decl
== NULL_TREE
)
2330 error ("pointer to member function called, but not in class scope");
2331 return error_mark_node
;
2333 /* What other type of POINTER_TYPE could this be? */
2334 if (TREE_CODE (TREE_TYPE (function
)) != POINTER_TYPE
2335 && ! TYPE_PTRMEMFUNC_P (TREE_TYPE (function
))
2336 && TREE_CODE (function
) != OFFSET_REF
)
2337 function
= build (OFFSET_REF
, TREE_TYPE (type
), NULL_TREE
, function
);
2341 /* this is an abbreviated method call.
2342 must go through here in case it is a virtual function.
2343 @@ Perhaps this could be optimized. */
2345 if (basetype
&& (! current_class_type
2346 || ! DERIVED_FROM_P (basetype
, current_class_type
)))
2347 return build_member_call (basetype
, function
, params
);
2349 if (decl
== NULL_TREE
)
2351 if (current_class_type
== NULL_TREE
)
2353 error ("object missing in call to method `%s'",
2354 IDENTIFIER_POINTER (function
));
2355 return error_mark_node
;
2357 /* Yow: call from a static member function. */
2358 decl
= build1 (NOP_EXPR
, build_pointer_type (current_class_type
),
2360 decl
= build_indirect_ref (decl
, NULL_PTR
);
2363 return build_method_call (decl
, function
, params
,
2364 NULL_TREE
, LOOKUP_NORMAL
);
2366 else if (TREE_CODE (function
) == COMPONENT_REF
2367 && type
== unknown_type_node
)
2369 /* Should we undo what was done in build_component_ref? */
2370 if (TREE_CODE (TREE_PURPOSE (TREE_OPERAND (function
, 1))) == TREE_VEC
)
2371 /* Get the name that build_component_ref hid. */
2372 function
= DECL_NAME (TREE_VALUE (TREE_OPERAND (function
, 1)));
2374 function
= TREE_PURPOSE (TREE_OPERAND (function
, 1));
2375 return build_method_call (decl
, function
, params
,
2376 NULL_TREE
, LOOKUP_NORMAL
);
2378 else if (really_overloaded_fn (function
))
2380 if (TREE_VALUE (function
) == NULL_TREE
)
2382 cp_error ("function `%D' declared overloaded, but no definitions appear with which to resolve it?!?",
2383 TREE_PURPOSE (function
));
2384 return error_mark_node
;
2388 tree val
= TREE_VALUE (function
);
2390 if (flag_ansi_overloading
)
2391 return build_new_function_call (function
, params
, NULL_TREE
);
2393 if (TREE_CODE (val
) == TEMPLATE_DECL
)
2394 return build_overload_call_real
2395 (function
, params
, LOOKUP_COMPLAIN
, (struct candidate
*)0, 0);
2396 else if (DECL_CHAIN (val
) != NULL_TREE
)
2397 return build_overload_call
2398 (function
, params
, LOOKUP_COMPLAIN
);
2400 my_friendly_abort (360);
2405 if (TREE_CODE (function
) == OFFSET_REF
)
2407 /* If the component is a data element (or a virtual function), we play
2408 games here to make things work. */
2411 if (TREE_OPERAND (function
, 0))
2412 decl
= TREE_OPERAND (function
, 0);
2414 decl
= current_class_ref
;
2416 decl_addr
= build_unary_op (ADDR_EXPR
, decl
, 0);
2417 function
= get_member_function_from_ptrfunc (&decl_addr
,
2418 TREE_OPERAND (function
, 1));
2419 params
= tree_cons (NULL_TREE
, decl_addr
, params
);
2420 return build_function_call (function
, params
);
2423 type
= TREE_TYPE (function
);
2424 if (type
!= error_mark_node
)
2426 if (TREE_CODE (type
) == REFERENCE_TYPE
)
2427 type
= TREE_TYPE (type
);
2429 if (IS_AGGR_TYPE (type
))
2430 return build_opfncall (CALL_EXPR
, LOOKUP_NORMAL
, function
, params
, NULL_TREE
);
2435 tree fntype
= TREE_TYPE (function
);
2438 /* Explicitly named method? */
2439 if (TREE_CODE (function
) == FUNCTION_DECL
)
2440 ctypeptr
= build_pointer_type (DECL_CLASS_CONTEXT (function
));
2441 /* Expression with ptr-to-method type? It could either be a plain
2442 usage, or it might be a case where the ptr-to-method is being
2443 passed in as an argument. */
2444 else if (TYPE_PTRMEMFUNC_P (fntype
))
2446 tree rec
= TYPE_METHOD_BASETYPE (TREE_TYPE (TYPE_PTRMEMFUNC_FN_TYPE (fntype
)));
2447 ctypeptr
= build_pointer_type (rec
);
2449 /* Unexpected node type? */
2451 my_friendly_abort (116);
2452 if (decl
== NULL_TREE
)
2454 if (current_function_decl
2455 && DECL_STATIC_FUNCTION_P (current_function_decl
))
2456 error ("invalid call to member function needing `this' in static member function scope");
2458 error ("pointer to member function called, but not in class scope");
2459 return error_mark_node
;
2461 if (TREE_CODE (TREE_TYPE (decl
)) != POINTER_TYPE
2462 && ! TYPE_PTRMEMFUNC_P (TREE_TYPE (decl
)))
2464 decl
= build_unary_op (ADDR_EXPR
, decl
, 0);
2465 decl
= convert_pointer_to (TREE_TYPE (ctypeptr
), decl
);
2468 decl
= build_c_cast (ctypeptr
, decl
);
2469 params
= tree_cons (NULL_TREE
, decl
, params
);
2472 return build_function_call (function
, params
);
2475 /* Resolve a pointer to member function. INSTANCE is the object
2476 instance to use, if the member points to a virtual member. */
2479 get_member_function_from_ptrfunc (instance_ptrptr
, function
)
2480 tree
*instance_ptrptr
;
2483 if (TREE_CODE (function
) == OFFSET_REF
)
2485 function
= TREE_OPERAND (function
, 1);
2488 if (TYPE_PTRMEMFUNC_P (TREE_TYPE (function
)))
2490 tree fntype
, idx
, e1
, delta
, delta2
, e2
, e3
, aref
, vtbl
;
2493 tree instance_ptr
= *instance_ptrptr
;
2495 if (TREE_SIDE_EFFECTS (instance_ptr
))
2496 instance_ptr
= save_expr (instance_ptr
);
2498 if (TREE_SIDE_EFFECTS (function
))
2499 function
= save_expr (function
);
2501 fntype
= TYPE_PTRMEMFUNC_FN_TYPE (TREE_TYPE (function
));
2503 /* Promoting idx before saving it improves performance on RISC
2504 targets. Without promoting, the first compare used
2505 load-with-sign-extend, while the second used normal load then
2506 shift to sign-extend. An optimizer flaw, perhaps, but it's easier
2507 to make this change. */
2508 idx
= save_expr (default_conversion
2509 (build_component_ref (function
,
2512 e1
= build_binary_op (GT_EXPR
, idx
, integer_zero_node
, 1);
2513 delta
= cp_convert (ptrdiff_type_node
,
2514 build_component_ref (function
, delta_identifier
, NULL_TREE
, 0));
2515 delta2
= DELTA2_FROM_PTRMEMFUNC (function
);
2517 /* Convert down to the right base, before using the instance. */
2519 = convert_pointer_to_real (TYPE_METHOD_BASETYPE (TREE_TYPE (fntype
)),
2521 if (instance
== error_mark_node
&& instance_ptr
!= error_mark_node
)
2524 vtbl
= convert_pointer_to (ptr_type_node
, instance
);
2527 build_pointer_type (build_pointer_type (vtable_entry_type
)),
2528 vtbl
, cp_convert (ptrdiff_type_node
, delta2
));
2529 vtbl
= build_indirect_ref (vtbl
, NULL_PTR
);
2530 aref
= build_array_ref (vtbl
, build_binary_op (MINUS_EXPR
,
2532 integer_one_node
, 1));
2533 if (! flag_vtable_thunks
)
2535 aref
= save_expr (aref
);
2537 delta
= build_binary_op (PLUS_EXPR
,
2538 build_conditional_expr (e1
, build_component_ref (aref
, delta_identifier
, NULL_TREE
, 0), integer_zero_node
),
2542 *instance_ptrptr
= build (PLUS_EXPR
, TREE_TYPE (instance_ptr
),
2543 instance_ptr
, delta
);
2544 if (flag_vtable_thunks
)
2547 e2
= build_component_ref (aref
, pfn_identifier
, NULL_TREE
, 0);
2549 e3
= PFN_FROM_PTRMEMFUNC (function
);
2550 TREE_TYPE (e2
) = TREE_TYPE (e3
);
2551 e1
= build_conditional_expr (e1
, e2
, e3
);
2553 if (instance_ptr
== error_mark_node
2554 && TREE_CODE (e1
) != ADDR_EXPR
2555 && TREE_CODE (TREE_OPERAND (e1
, 0)) != FUNCTION_DECL
)
2556 cp_error ("object missing in `%E'", function
);
2560 /* Make sure this doesn't get evaluated first inside one of the
2561 branches of the COND_EXPR. */
2562 if (TREE_CODE (instance_ptr
) == SAVE_EXPR
)
2563 function
= build (COMPOUND_EXPR
, TREE_TYPE (function
),
2564 instance_ptr
, function
);
2570 build_function_call_real (function
, params
, require_complete
, flags
)
2571 tree function
, params
;
2572 int require_complete
, flags
;
2574 register tree fntype
, fndecl
;
2575 register tree value_type
;
2576 register tree coerced_params
;
2577 tree name
= NULL_TREE
, assembler_name
= NULL_TREE
;
2580 /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
2581 Strip such NOP_EXPRs, since FUNCTION is used in non-lvalue context. */
2582 if (TREE_CODE (function
) == NOP_EXPR
2583 && TREE_TYPE (function
) == TREE_TYPE (TREE_OPERAND (function
, 0)))
2584 function
= TREE_OPERAND (function
, 0);
2586 if (TREE_CODE (function
) == FUNCTION_DECL
)
2588 name
= DECL_NAME (function
);
2589 assembler_name
= DECL_ASSEMBLER_NAME (function
);
2591 GNU_xref_call (current_function_decl
,
2592 IDENTIFIER_POINTER (name
? name
2593 : TYPE_IDENTIFIER (DECL_CLASS_CONTEXT (function
))));
2594 mark_used (function
);
2597 /* Convert anything with function type to a pointer-to-function. */
2600 && IDENTIFIER_LENGTH (name
) == 4
2601 && ! strcmp (IDENTIFIER_POINTER (name
), "main")
2602 && DECL_CONTEXT (function
) == NULL_TREE
)
2604 pedwarn ("ANSI C++ forbids calling `main' from within program");
2607 if (pedantic
&& DECL_THIS_INLINE (function
) && ! DECL_INITIAL (function
)
2608 && ! DECL_ARTIFICIAL (function
)
2609 && ! DECL_PENDING_INLINE_INFO (function
))
2610 cp_pedwarn ("inline function `%#D' called before definition",
2613 /* Differs from default_conversion by not setting TREE_ADDRESSABLE
2614 (because calling an inline function does not mean the function
2615 needs to be separately compiled). */
2617 if (DECL_INLINE (function
))
2618 function
= inline_conversion (function
);
2620 function
= build_addr_func (function
);
2626 function
= build_addr_func (function
);
2629 if (function
== error_mark_node
)
2630 return error_mark_node
;
2632 fntype
= TREE_TYPE (function
);
2634 if (TYPE_PTRMEMFUNC_P (fntype
))
2636 cp_error ("must use .* or ->* to call pointer-to-member function in `%E (...)'",
2638 return error_mark_node
;
2641 is_method
= (TREE_CODE (fntype
) == POINTER_TYPE
2642 && TREE_CODE (TREE_TYPE (fntype
)) == METHOD_TYPE
);
2644 if (!((TREE_CODE (fntype
) == POINTER_TYPE
2645 && TREE_CODE (TREE_TYPE (fntype
)) == FUNCTION_TYPE
)
2647 || TREE_CODE (function
) == TEMPLATE_ID_EXPR
))
2649 cp_error ("`%E' cannot be used as a function", function
);
2650 return error_mark_node
;
2653 /* fntype now gets the type of function pointed to. */
2654 fntype
= TREE_TYPE (fntype
);
2656 /* Convert the parameters to the types declared in the
2657 function prototype, or apply default promotions. */
2659 if (flags
& LOOKUP_COMPLAIN
)
2660 coerced_params
= convert_arguments (NULL_TREE
, TYPE_ARG_TYPES (fntype
),
2661 params
, fndecl
, LOOKUP_NORMAL
);
2663 coerced_params
= convert_arguments (NULL_TREE
, TYPE_ARG_TYPES (fntype
),
2666 if (coerced_params
== error_mark_node
)
2667 if (flags
& LOOKUP_SPECULATIVELY
)
2670 return error_mark_node
;
2672 /* Check for errors in format strings. */
2674 if (warn_format
&& (name
|| assembler_name
))
2675 check_function_format (name
, assembler_name
, coerced_params
);
2677 /* Recognize certain built-in functions so we can make tree-codes
2678 other than CALL_EXPR. We do this when it enables fold-const.c
2679 to do something useful. */
2681 if (TREE_CODE (function
) == ADDR_EXPR
2682 && TREE_CODE (TREE_OPERAND (function
, 0)) == FUNCTION_DECL
2683 && DECL_BUILT_IN (TREE_OPERAND (function
, 0)))
2684 switch (DECL_FUNCTION_CODE (TREE_OPERAND (function
, 0)))
2689 if (coerced_params
== 0)
2690 return integer_zero_node
;
2691 return build_unary_op (ABS_EXPR
, TREE_VALUE (coerced_params
), 0);
2695 value_type
= TREE_TYPE (fntype
) ? TREE_TYPE (fntype
) : void_type_node
;
2697 register tree result
2698 = build_call (function
, value_type
, coerced_params
);
2700 if (require_complete
)
2702 if (value_type
== void_type_node
)
2704 result
= require_complete_type (result
);
2706 if (IS_AGGR_TYPE (value_type
))
2707 result
= build_cplus_new (value_type
, result
);
2708 return convert_from_reference (result
);
2713 build_function_call (function
, params
)
2714 tree function
, params
;
2716 return build_function_call_real (function
, params
, 1, LOOKUP_NORMAL
);
2719 /* Convert the actual parameter expressions in the list VALUES
2720 to the types in the list TYPELIST.
2721 If parmdecls is exhausted, or when an element has NULL as its type,
2722 perform the default conversions.
2724 RETURN_LOC is the location of the return value, if known, NULL_TREE
2725 otherwise. This is useful in the case where we can avoid creating
2726 a temporary variable in the case where we can initialize the return
2727 value directly. If we are not eliding constructors, then we set this
2728 to NULL_TREE to avoid this avoidance.
2730 NAME is an IDENTIFIER_NODE or 0. It is used only for error messages.
2732 This is also where warnings about wrong number of args are generated.
2734 Return a list of expressions for the parameters as converted.
2736 Both VALUES and the returned value are chains of TREE_LIST nodes
2737 with the elements of the list in the TREE_VALUE slots of those nodes.
2739 In C++, unspecified trailing parameters can be filled in with their
2740 default arguments, if such were specified. Do so here. */
2743 convert_arguments (return_loc
, typelist
, values
, fndecl
, flags
)
2744 tree return_loc
, typelist
, values
, fndecl
;
2747 register tree typetail
, valtail
;
2748 register tree result
= NULL_TREE
;
2752 if (! flag_elide_constructors
)
2755 /* Argument passing is always copy-initialization. */
2756 flags
|= LOOKUP_ONLYCONVERTING
;
2760 if (TREE_CODE (TREE_TYPE (fndecl
)) == METHOD_TYPE
)
2762 if (DECL_NAME (fndecl
) == NULL_TREE
2763 || IDENTIFIER_HAS_TYPE_VALUE (DECL_NAME (fndecl
)))
2764 called_thing
= "constructor";
2766 called_thing
= "member function";
2769 called_thing
= "function";
2772 for (valtail
= values
, typetail
= typelist
;
2774 valtail
= TREE_CHAIN (valtail
), i
++)
2776 register tree type
= typetail
? TREE_VALUE (typetail
) : 0;
2777 register tree val
= TREE_VALUE (valtail
);
2779 if (val
== error_mark_node
)
2780 return error_mark_node
;
2782 if (type
== void_type_node
)
2786 cp_error_at ("too many arguments to %s `%+D'", called_thing
,
2788 error ("at this point in file");
2791 error ("too many arguments to function");
2792 /* In case anybody wants to know if this argument
2795 TREE_TYPE (tree_last (result
)) = error_mark_node
;
2799 /* The tree type of the parameter being passed may not yet be
2800 known. In this case, its type is TYPE_UNKNOWN, and will
2801 be instantiated by the type given by TYPE. If TYPE
2802 is also NULL, the tree type of VAL is ERROR_MARK_NODE. */
2803 if (type
&& type_unknown_p (val
))
2804 val
= require_instantiated_type (type
, val
, integer_zero_node
);
2805 else if (type_unknown_p (val
))
2807 /* Strip the `&' from an overloaded FUNCTION_DECL. */
2808 if (TREE_CODE (val
) == ADDR_EXPR
)
2809 val
= TREE_OPERAND (val
, 0);
2810 if (really_overloaded_fn (val
))
2811 cp_error ("insufficient type information to resolve address of overloaded function `%D'",
2812 DECL_NAME (get_first_fn (val
)));
2814 error ("insufficient type information in parameter list");
2815 val
= integer_zero_node
;
2817 else if (TREE_CODE (val
) == OFFSET_REF
2818 && TREE_CODE (TREE_TYPE (val
)) == METHOD_TYPE
)
2820 /* This is unclean. Should be handled elsewhere. */
2821 val
= build_unary_op (ADDR_EXPR
, val
, 0);
2823 else if (TREE_CODE (val
) == OFFSET_REF
)
2824 val
= resolve_offset_ref (val
);
2826 /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
2827 Strip such NOP_EXPRs, since VAL is used in non-lvalue context. */
2828 if (TREE_CODE (val
) == NOP_EXPR
2829 && TREE_TYPE (val
) == TREE_TYPE (TREE_OPERAND (val
, 0))
2830 && (type
== 0 || TREE_CODE (type
) != REFERENCE_TYPE
))
2831 val
= TREE_OPERAND (val
, 0);
2833 if (type
== 0 || TREE_CODE (type
) != REFERENCE_TYPE
)
2835 if (TREE_CODE (TREE_TYPE (val
)) == ARRAY_TYPE
2836 || TREE_CODE (TREE_TYPE (val
)) == FUNCTION_TYPE
2837 || TREE_CODE (TREE_TYPE (val
)) == METHOD_TYPE
)
2838 val
= default_conversion (val
);
2840 val
= require_complete_type (val
);
2843 if (val
== error_mark_node
)
2844 return error_mark_node
;
2848 /* Formal parm type is specified by a function prototype. */
2851 if (TYPE_SIZE (complete_type (type
)) == 0)
2853 error ("parameter type of called function is incomplete");
2858 parmval
= convert_for_initialization
2859 (return_loc
, type
, val
, flags
,
2860 "argument passing", fndecl
, i
);
2861 #ifdef PROMOTE_PROTOTYPES
2862 if ((TREE_CODE (type
) == INTEGER_TYPE
2863 || TREE_CODE (type
) == ENUMERAL_TYPE
)
2864 && (TYPE_PRECISION (type
) < TYPE_PRECISION (integer_type_node
)))
2865 parmval
= default_conversion (parmval
);
2869 if (parmval
== error_mark_node
)
2870 return error_mark_node
;
2872 result
= tree_cons (NULL_TREE
, parmval
, result
);
2876 if (TREE_CODE (TREE_TYPE (val
)) == REFERENCE_TYPE
)
2877 val
= convert_from_reference (val
);
2879 if (TREE_CODE (TREE_TYPE (val
)) == REAL_TYPE
2880 && (TYPE_PRECISION (TREE_TYPE (val
))
2881 < TYPE_PRECISION (double_type_node
)))
2882 /* Convert `float' to `double'. */
2883 result
= tree_cons (NULL_TREE
, cp_convert (double_type_node
, val
), result
);
2884 else if (TYPE_LANG_SPECIFIC (TREE_TYPE (val
))
2885 && ! TYPE_HAS_TRIVIAL_INIT_REF (TREE_TYPE (val
)))
2887 cp_warning ("cannot pass objects of type `%T' through `...'",
2889 result
= tree_cons (NULL_TREE
, val
, result
);
2892 /* Convert `short' and `char' to full-size `int'. */
2893 result
= tree_cons (NULL_TREE
, default_conversion (val
), result
);
2897 typetail
= TREE_CHAIN (typetail
);
2900 if (typetail
!= 0 && typetail
!= void_list_node
)
2902 /* See if there are default arguments that can be used */
2903 if (TREE_PURPOSE (typetail
))
2905 for (; typetail
!= void_list_node
; ++i
)
2907 tree type
= TREE_VALUE (typetail
);
2908 tree val
= break_out_target_exprs (TREE_PURPOSE (typetail
));
2911 if (val
== NULL_TREE
)
2912 parmval
= error_mark_node
;
2913 else if (TREE_CODE (val
) == CONSTRUCTOR
)
2915 parmval
= digest_init (type
, val
, (tree
*)0);
2916 parmval
= convert_for_initialization (return_loc
, type
, parmval
, flags
,
2917 "default constructor", fndecl
, i
);
2921 /* This could get clobbered by the following call. */
2922 if (TREE_HAS_CONSTRUCTOR (val
))
2923 val
= copy_node (val
);
2925 parmval
= convert_for_initialization (return_loc
, type
, val
, flags
,
2926 "default argument", fndecl
, i
);
2927 #ifdef PROMOTE_PROTOTYPES
2928 if ((TREE_CODE (type
) == INTEGER_TYPE
2929 || TREE_CODE (type
) == ENUMERAL_TYPE
)
2930 && (TYPE_PRECISION (type
) < TYPE_PRECISION (integer_type_node
)))
2931 parmval
= default_conversion (parmval
);
2935 if (parmval
== error_mark_node
)
2936 return error_mark_node
;
2938 result
= tree_cons (0, parmval
, result
);
2939 typetail
= TREE_CHAIN (typetail
);
2940 /* ends with `...'. */
2941 if (typetail
== NULL_TREE
)
2949 char *buf
= (char *)alloca (32 + strlen (called_thing
));
2950 sprintf (buf
, "too few arguments to %s `%%#D'", called_thing
);
2951 cp_error_at (buf
, fndecl
);
2952 error ("at this point in file");
2955 error ("too few arguments to function");
2956 return error_mark_list
;
2960 return nreverse (result
);
2963 /* Build a binary-operation expression, after performing default
2964 conversions on the operands. CODE is the kind of expression to build. */
2967 build_x_binary_op (code
, arg1
, arg2
)
2968 enum tree_code code
;
2973 if (processing_template_decl
)
2974 return build_min_nt (code
, arg1
, arg2
);
2976 if (flag_ansi_overloading
)
2977 return build_new_op (code
, LOOKUP_NORMAL
, arg1
, arg2
, NULL_TREE
);
2979 rval
= build_opfncall (code
, LOOKUP_SPECULATIVELY
,
2980 arg1
, arg2
, NULL_TREE
);
2982 return build_opfncall (code
, LOOKUP_NORMAL
, arg1
, arg2
, NULL_TREE
);
2983 if (code
== MEMBER_REF
)
2984 return build_m_component_ref (build_indirect_ref (arg1
, NULL_PTR
),
2986 return build_binary_op (code
, arg1
, arg2
, 1);
2990 build_binary_op (code
, arg1
, arg2
, convert_p
)
2991 enum tree_code code
;
3003 args
[0] = decay_conversion (args
[0]);
3004 args
[1] = decay_conversion (args
[1]);
3006 if (args
[0] == error_mark_node
|| args
[1] == error_mark_node
)
3007 return error_mark_node
;
3009 type0
= TREE_TYPE (args
[0]);
3010 type1
= TREE_TYPE (args
[1]);
3012 if (type_unknown_p (args
[0]))
3014 args
[0] = instantiate_type (type1
, args
[0], 1);
3015 args
[0] = decay_conversion (args
[0]);
3017 else if (type_unknown_p (args
[1]))
3019 args
[1] = require_instantiated_type (type0
, args
[1],
3021 args
[1] = decay_conversion (args
[1]);
3024 if (IS_AGGR_TYPE (type0
) || IS_AGGR_TYPE (type1
))
3026 /* Try to convert this to something reasonable. */
3027 if (! build_default_binary_type_conversion(code
, &args
[0], &args
[1]))
3029 cp_error ("no match for `%O(%#T, %#T)'", code
,
3030 TREE_TYPE (arg1
), TREE_TYPE (arg2
));
3031 return error_mark_node
;
3035 return build_binary_op_nodefault (code
, args
[0], args
[1], code
);
3038 /* Build a binary-operation expression without default conversions.
3039 CODE is the kind of expression to build.
3040 This function differs from `build' in several ways:
3041 the data type of the result is computed and recorded in it,
3042 warnings are generated if arg data types are invalid,
3043 special handling for addition and subtraction of pointers is known,
3044 and some optimization is done (operations on narrow ints
3045 are done in the narrower type when that gives the same result).
3046 Constant folding is also done before the result is returned.
3048 ERROR_CODE is the code that determines what to say in error messages.
3049 It is usually, but not always, the same as CODE.
3051 Note that the operands will never have enumeral types
3052 because either they have just had the default conversions performed
3053 or they have both just been converted to some other type in which
3054 the arithmetic is to be done.
3056 C++: must do special pointer arithmetic when implementing
3057 multiple inheritance, and deal with pointer to member functions. */
3060 build_binary_op_nodefault (code
, orig_op0
, orig_op1
, error_code
)
3061 enum tree_code code
;
3062 tree orig_op0
, orig_op1
;
3063 enum tree_code error_code
;
3066 register enum tree_code code0
, code1
;
3069 /* Expression code to give to the expression when it is built.
3070 Normally this is CODE, which is what the caller asked for,
3071 but in some special cases we change it. */
3072 register enum tree_code resultcode
= code
;
3074 /* Data type in which the computation is to be performed.
3075 In the simplest cases this is the common type of the arguments. */
3076 register tree result_type
= NULL
;
3078 /* Nonzero means operands have already been type-converted
3079 in whatever way is necessary.
3080 Zero means they need to be converted to RESULT_TYPE. */
3083 /* Nonzero means create the expression with this type, rather than
3085 tree build_type
= 0;
3087 /* Nonzero means after finally constructing the expression
3088 convert it to this type. */
3089 tree final_type
= 0;
3091 /* Nonzero if this is an operation like MIN or MAX which can
3092 safely be computed in short if both args are promoted shorts.
3093 Also implies COMMON.
3094 -1 indicates a bitwise operation; this makes a difference
3095 in the exact conditions for when it is safe to do the operation
3096 in a narrower mode. */
3099 /* Nonzero if this is a comparison operation;
3100 if both args are promoted shorts, compare the original shorts.
3101 Also implies COMMON. */
3102 int short_compare
= 0;
3104 /* Nonzero if this is a right-shift operation, which can be computed on the
3105 original short and then promoted if the operand is a promoted short. */
3106 int short_shift
= 0;
3108 /* Nonzero means set RESULT_TYPE to the common type of the args. */
3111 /* Apply default conversions. */
3112 if (code
== TRUTH_AND_EXPR
|| code
== TRUTH_ANDIF_EXPR
3113 || code
== TRUTH_OR_EXPR
|| code
== TRUTH_ORIF_EXPR
3114 || code
== TRUTH_XOR_EXPR
)
3116 op0
= decay_conversion (orig_op0
);
3117 op1
= decay_conversion (orig_op1
);
3121 op0
= default_conversion (orig_op0
);
3122 op1
= default_conversion (orig_op1
);
3125 type0
= TREE_TYPE (op0
);
3126 type1
= TREE_TYPE (op1
);
3128 /* The expression codes of the data types of the arguments tell us
3129 whether the arguments are integers, floating, pointers, etc. */
3130 code0
= TREE_CODE (type0
);
3131 code1
= TREE_CODE (type1
);
3133 /* Strip NON_LVALUE_EXPRs, etc., since we aren't using as an lvalue. */
3134 STRIP_TYPE_NOPS (op0
);
3135 STRIP_TYPE_NOPS (op1
);
3137 /* If an error was already reported for one of the arguments,
3138 avoid reporting another error. */
3140 if (code0
== ERROR_MARK
|| code1
== ERROR_MARK
)
3141 return error_mark_node
;
3146 /* Handle the pointer + int case. */
3147 if (code0
== POINTER_TYPE
&& code1
== INTEGER_TYPE
)
3148 return pointer_int_sum (PLUS_EXPR
, op0
, op1
);
3149 else if (code1
== POINTER_TYPE
&& code0
== INTEGER_TYPE
)
3150 return pointer_int_sum (PLUS_EXPR
, op1
, op0
);
3156 /* Subtraction of two similar pointers.
3157 We must subtract them as integers, then divide by object size. */
3158 if (code0
== POINTER_TYPE
&& code1
== POINTER_TYPE
3159 && comp_target_types (type0
, type1
, 1))
3160 return pointer_diff (op0
, op1
);
3161 /* Handle pointer minus int. Just like pointer plus int. */
3162 else if (code0
== POINTER_TYPE
&& code1
== INTEGER_TYPE
)
3163 return pointer_int_sum (MINUS_EXPR
, op0
, op1
);
3172 case TRUNC_DIV_EXPR
:
3174 case FLOOR_DIV_EXPR
:
3175 case ROUND_DIV_EXPR
:
3176 case EXACT_DIV_EXPR
:
3177 if ((code0
== INTEGER_TYPE
|| code0
== REAL_TYPE
3178 || code0
== COMPLEX_TYPE
)
3179 && (code1
== INTEGER_TYPE
|| code1
== REAL_TYPE
3180 || code1
== COMPLEX_TYPE
))
3182 if (TREE_CODE (op1
) == INTEGER_CST
&& integer_zerop (op1
))
3183 cp_warning ("division by zero in `%E / 0'", op0
);
3184 else if (TREE_CODE (op1
) == REAL_CST
&& real_zerop (op1
))
3185 cp_warning ("division by zero in `%E / 0.'", op0
);
3187 if (!(code0
== INTEGER_TYPE
&& code1
== INTEGER_TYPE
))
3188 resultcode
= RDIV_EXPR
;
3190 /* When dividing two signed integers, we have to promote to int.
3191 unless we divide by a constant != -1. Note that default
3192 conversion will have been performed on the operands at this
3193 point, so we have to dig out the original type to find out if
3195 shorten
= ((TREE_CODE (op0
) == NOP_EXPR
3196 && TREE_UNSIGNED (TREE_TYPE (TREE_OPERAND (op0
, 0))))
3197 || (TREE_CODE (op1
) == INTEGER_CST
3198 && (TREE_INT_CST_LOW (op1
) != -1
3199 || TREE_INT_CST_HIGH (op1
) != -1)));
3205 case BIT_ANDTC_EXPR
:
3208 if (code0
== INTEGER_TYPE
&& code1
== INTEGER_TYPE
)
3210 /* If one operand is a constant, and the other is a short type
3211 that has been converted to an int,
3212 really do the work in the short type and then convert the
3213 result to int. If we are lucky, the constant will be 0 or 1
3214 in the short type, making the entire operation go away. */
3215 if (TREE_CODE (op0
) == INTEGER_CST
3216 && TREE_CODE (op1
) == NOP_EXPR
3217 && TYPE_PRECISION (type1
) > TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (op1
, 0)))
3218 && TREE_UNSIGNED (TREE_TYPE (TREE_OPERAND (op1
, 0))))
3220 final_type
= result_type
;
3221 op1
= TREE_OPERAND (op1
, 0);
3222 result_type
= TREE_TYPE (op1
);
3224 if (TREE_CODE (op1
) == INTEGER_CST
3225 && TREE_CODE (op0
) == NOP_EXPR
3226 && TYPE_PRECISION (type0
) > TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (op0
, 0)))
3227 && TREE_UNSIGNED (TREE_TYPE (TREE_OPERAND (op0
, 0))))
3229 final_type
= result_type
;
3230 op0
= TREE_OPERAND (op0
, 0);
3231 result_type
= TREE_TYPE (op0
);
3235 case TRUNC_MOD_EXPR
:
3236 case FLOOR_MOD_EXPR
:
3237 if (code1
== INTEGER_TYPE
&& integer_zerop (op1
))
3238 cp_warning ("division by zero in `%E % 0'", op0
);
3239 else if (code1
== REAL_TYPE
&& real_zerop (op1
))
3240 cp_warning ("division by zero in `%E % 0.'", op0
);
3242 if (code0
== INTEGER_TYPE
&& code1
== INTEGER_TYPE
)
3244 /* Although it would be tempting to shorten always here, that loses
3245 on some targets, since the modulo instruction is undefined if the
3246 quotient can't be represented in the computation mode. We shorten
3247 only if unsigned or if dividing by something we know != -1. */
3248 shorten
= ((TREE_CODE (op0
) == NOP_EXPR
3249 && TREE_UNSIGNED (TREE_TYPE (TREE_OPERAND (op0
, 0))))
3250 || (TREE_CODE (op1
) == INTEGER_CST
3251 && (TREE_INT_CST_LOW (op1
) != -1
3252 || TREE_INT_CST_HIGH (op1
) != -1)));
3257 case TRUTH_ANDIF_EXPR
:
3258 case TRUTH_ORIF_EXPR
:
3259 case TRUTH_AND_EXPR
:
3261 result_type
= boolean_type_node
;
3264 /* Shift operations: result has same type as first operand;
3265 always convert second operand to int.
3266 Also set SHORT_SHIFT if shifting rightward. */
3269 if (code0
== INTEGER_TYPE
&& code1
== INTEGER_TYPE
)
3271 result_type
= type0
;
3272 if (TREE_CODE (op1
) == INTEGER_CST
)
3274 if (tree_int_cst_lt (op1
, integer_zero_node
))
3275 warning ("right shift count is negative");
3278 if (TREE_INT_CST_LOW (op1
) | TREE_INT_CST_HIGH (op1
))
3280 if (TREE_INT_CST_HIGH (op1
) != 0
3281 || ((unsigned HOST_WIDE_INT
) TREE_INT_CST_LOW (op1
)
3282 >= TYPE_PRECISION (type0
)))
3283 warning ("right shift count >= width of type");
3286 /* Convert the shift-count to an integer, regardless of
3287 size of value being shifted. */
3288 if (TYPE_MAIN_VARIANT (TREE_TYPE (op1
)) != integer_type_node
)
3289 op1
= cp_convert (integer_type_node
, op1
);
3290 /* Avoid converting op1 to result_type later. */
3296 if (code0
== INTEGER_TYPE
&& code1
== INTEGER_TYPE
)
3298 result_type
= type0
;
3299 if (TREE_CODE (op1
) == INTEGER_CST
)
3301 if (tree_int_cst_lt (op1
, integer_zero_node
))
3302 warning ("left shift count is negative");
3303 else if (TREE_INT_CST_HIGH (op1
) != 0
3304 || ((unsigned HOST_WIDE_INT
) TREE_INT_CST_LOW (op1
)
3305 >= TYPE_PRECISION (type0
)))
3306 warning ("left shift count >= width of type");
3308 /* Convert the shift-count to an integer, regardless of
3309 size of value being shifted. */
3310 if (TYPE_MAIN_VARIANT (TREE_TYPE (op1
)) != integer_type_node
)
3311 op1
= cp_convert (integer_type_node
, op1
);
3312 /* Avoid converting op1 to result_type later. */
3319 if (code0
== INTEGER_TYPE
&& code1
== INTEGER_TYPE
)
3321 result_type
= type0
;
3322 if (TREE_CODE (op1
) == INTEGER_CST
)
3324 if (tree_int_cst_lt (op1
, integer_zero_node
))
3325 warning ("%s rotate count is negative",
3326 (code
== LROTATE_EXPR
) ? "left" : "right");
3327 else if (TREE_INT_CST_HIGH (op1
) != 0
3328 || ((unsigned HOST_WIDE_INT
) TREE_INT_CST_LOW (op1
)
3329 >= TYPE_PRECISION (type0
)))
3330 warning ("%s rotate count >= width of type",
3331 (code
== LROTATE_EXPR
) ? "left" : "right");
3333 /* Convert the shift-count to an integer, regardless of
3334 size of value being shifted. */
3335 if (TYPE_MAIN_VARIANT (TREE_TYPE (op1
)) != integer_type_node
)
3336 op1
= cp_convert (integer_type_node
, op1
);
3342 build_type
= boolean_type_node
;
3343 if ((code0
== INTEGER_TYPE
|| code0
== REAL_TYPE
3344 || code0
== COMPLEX_TYPE
)
3345 && (code1
== INTEGER_TYPE
|| code1
== REAL_TYPE
3346 || code1
== COMPLEX_TYPE
))
3348 else if (code0
== POINTER_TYPE
&& code1
== POINTER_TYPE
)
3350 register tree tt0
= TYPE_MAIN_VARIANT (TREE_TYPE (type0
));
3351 register tree tt1
= TYPE_MAIN_VARIANT (TREE_TYPE (type1
));
3353 if (comp_target_types (type0
, type1
, 1))
3354 result_type
= common_type (type0
, type1
);
3355 else if (tt0
== void_type_node
)
3357 if (pedantic
&& TREE_CODE (tt1
) == FUNCTION_TYPE
3358 && tree_int_cst_lt (TYPE_SIZE (type0
), TYPE_SIZE (type1
)))
3359 pedwarn ("ANSI C++ forbids comparison of `void *' with function pointer");
3360 else if (TREE_CODE (tt1
) == OFFSET_TYPE
)
3361 pedwarn ("ANSI C++ forbids conversion of a pointer to member to `void *'");
3363 else if (tt1
== void_type_node
)
3365 if (pedantic
&& TREE_CODE (tt0
) == FUNCTION_TYPE
3366 && tree_int_cst_lt (TYPE_SIZE (type1
), TYPE_SIZE (type0
)))
3367 pedwarn ("ANSI C++ forbids comparison of `void *' with function pointer");
3370 cp_pedwarn ("comparison of distinct pointer types `%T' and `%T' lacks a cast",
3373 if (result_type
== NULL_TREE
)
3374 result_type
= ptr_type_node
;
3376 else if (code0
== POINTER_TYPE
&& TREE_CODE (op1
) == INTEGER_CST
3377 && integer_zerop (op1
))
3378 result_type
= type0
;
3379 else if (code1
== POINTER_TYPE
&& TREE_CODE (op0
) == INTEGER_CST
3380 && integer_zerop (op0
))
3381 result_type
= type1
;
3382 else if (code0
== POINTER_TYPE
&& code1
== INTEGER_TYPE
)
3384 result_type
= type0
;
3385 error ("ANSI C++ forbids comparison between pointer and integer");
3387 else if (code0
== INTEGER_TYPE
&& code1
== POINTER_TYPE
)
3389 result_type
= type1
;
3390 error ("ANSI C++ forbids comparison between pointer and integer");
3392 else if (TYPE_PTRMEMFUNC_P (type0
) && TREE_CODE (op1
) == INTEGER_CST
3393 && integer_zerop (op1
))
3395 op0
= build_component_ref (op0
, index_identifier
, NULL_TREE
, 0);
3396 op1
= integer_zero_node
;
3397 result_type
= TREE_TYPE (op0
);
3399 else if (TYPE_PTRMEMFUNC_P (type1
) && TREE_CODE (op0
) == INTEGER_CST
3400 && integer_zerop (op0
))
3402 op0
= build_component_ref (op1
, index_identifier
, NULL_TREE
, 0);
3403 op1
= integer_zero_node
;
3404 result_type
= TREE_TYPE (op0
);
3406 else if (TYPE_PTRMEMFUNC_P (type0
) && TYPE_PTRMEMFUNC_P (type1
)
3407 && (TYPE_PTRMEMFUNC_FN_TYPE (type0
)
3408 == TYPE_PTRMEMFUNC_FN_TYPE (type1
)))
3410 /* The code we generate for the test is:
3412 (op0.index == op1.index
3413 && ((op1.index != -1 && op0.delta2 == op1.delta2)
3414 || op0.pfn == op1.pfn)) */
3416 tree index0
= build_component_ref (op0
, index_identifier
, NULL_TREE
, 0);
3417 tree index1
= save_expr (build_component_ref (op1
, index_identifier
, NULL_TREE
, 0));
3418 tree pfn0
= PFN_FROM_PTRMEMFUNC (op0
);
3419 tree pfn1
= PFN_FROM_PTRMEMFUNC (op1
);
3420 tree delta20
= DELTA2_FROM_PTRMEMFUNC (op0
);
3421 tree delta21
= DELTA2_FROM_PTRMEMFUNC (op1
);
3423 tree integer_neg_one_node
3424 = build_binary_op (MINUS_EXPR
, integer_zero_node
, integer_one_node
, 1);
3425 e1
= build_binary_op (EQ_EXPR
, index0
, index1
, 1);
3426 e2
= build_binary_op (NE_EXPR
, index1
, integer_neg_one_node
, 1);
3427 e2
= build_binary_op (TRUTH_ANDIF_EXPR
, e2
, build_binary_op (EQ_EXPR
, delta20
, delta21
, 1), 1);
3428 e3
= build_binary_op (EQ_EXPR
, pfn0
, pfn1
, 1);
3429 e2
= build_binary_op (TRUTH_ORIF_EXPR
, e2
, e3
, 1);
3430 e2
= build_binary_op (TRUTH_ANDIF_EXPR
, e1
, e2
, 1);
3431 if (code
== EQ_EXPR
)
3433 return build_binary_op (EQ_EXPR
, e2
, integer_zero_node
, 1);
3435 else if (TYPE_PTRMEMFUNC_P (type0
)
3436 && TYPE_PTRMEMFUNC_FN_TYPE (type0
) == type1
)
3438 tree index0
= build_component_ref (op0
, index_identifier
, NULL_TREE
, 0);
3440 tree pfn0
= PFN_FROM_PTRMEMFUNC (op0
);
3441 tree delta20
= DELTA2_FROM_PTRMEMFUNC (op0
);
3442 tree delta21
= integer_zero_node
;
3444 tree integer_neg_one_node
3445 = build_binary_op (MINUS_EXPR
, integer_zero_node
, integer_one_node
, 1);
3446 if (TREE_CODE (TREE_OPERAND (op1
, 0)) == FUNCTION_DECL
3447 && DECL_VINDEX (TREE_OPERAND (op1
, 0)))
3449 /* Map everything down one to make room for the null pointer to member. */
3450 index1
= size_binop (PLUS_EXPR
,
3451 DECL_VINDEX (TREE_OPERAND (op1
, 0)),
3453 op1
= integer_zero_node
;
3454 delta21
= CLASSTYPE_VFIELD (TYPE_METHOD_BASETYPE (TREE_TYPE (type1
)));
3455 delta21
= DECL_FIELD_BITPOS (delta21
);
3456 delta21
= size_binop (FLOOR_DIV_EXPR
, delta21
, size_int (BITS_PER_UNIT
));
3459 index1
= integer_neg_one_node
;
3461 tree nop1
= build1 (NOP_EXPR
, TYPE_PTRMEMFUNC_FN_TYPE (type0
), op1
);
3462 TREE_CONSTANT (nop1
) = TREE_CONSTANT (op1
);
3465 e1
= build_binary_op (EQ_EXPR
, index0
, index1
, 1);
3466 e2
= build_binary_op (NE_EXPR
, index1
, integer_neg_one_node
, 1);
3467 e2
= build_binary_op (TRUTH_ANDIF_EXPR
, e2
, build_binary_op (EQ_EXPR
, delta20
, delta21
, 1), 1);
3468 e3
= build_binary_op (EQ_EXPR
, pfn0
, op1
, 1);
3469 e2
= build_binary_op (TRUTH_ORIF_EXPR
, e2
, e3
, 1);
3470 e2
= build_binary_op (TRUTH_ANDIF_EXPR
, e1
, e2
, 1);
3471 if (code
== EQ_EXPR
)
3473 return build_binary_op (EQ_EXPR
, e2
, integer_zero_node
, 1);
3475 else if (TYPE_PTRMEMFUNC_P (type1
)
3476 && TYPE_PTRMEMFUNC_FN_TYPE (type1
) == type0
)
3478 return build_binary_op (code
, op1
, op0
, 1);
3484 if ((code0
== INTEGER_TYPE
|| code0
== REAL_TYPE
)
3485 && (code1
== INTEGER_TYPE
|| code1
== REAL_TYPE
))
3487 else if (code0
== POINTER_TYPE
&& code1
== POINTER_TYPE
)
3489 if (comp_target_types (type0
, type1
, 1))
3490 result_type
= common_type (type0
, type1
);
3493 cp_pedwarn ("comparison of distinct pointer types `%T' and `%T' lacks a cast",
3495 result_type
= ptr_type_node
;
3504 build_type
= boolean_type_node
;
3505 if ((code0
== INTEGER_TYPE
|| code0
== REAL_TYPE
)
3506 && (code1
== INTEGER_TYPE
|| code1
== REAL_TYPE
))
3508 else if (code0
== POINTER_TYPE
&& code1
== POINTER_TYPE
)
3510 if (comp_target_types (type0
, type1
, 1))
3511 result_type
= common_type (type0
, type1
);
3514 cp_pedwarn ("comparison of distinct pointer types `%T' and `%T' lacks a cast",
3516 result_type
= ptr_type_node
;
3519 else if (code0
== POINTER_TYPE
&& TREE_CODE (op1
) == INTEGER_CST
3520 && integer_zerop (op1
))
3521 result_type
= type0
;
3522 else if (code1
== POINTER_TYPE
&& TREE_CODE (op0
) == INTEGER_CST
3523 && integer_zerop (op0
))
3524 result_type
= type1
;
3525 else if (code0
== POINTER_TYPE
&& code1
== INTEGER_TYPE
)
3527 result_type
= type0
;
3528 pedwarn ("ANSI C++ forbids comparison between pointer and integer");
3530 else if (code0
== INTEGER_TYPE
&& code1
== POINTER_TYPE
)
3532 result_type
= type1
;
3533 pedwarn ("ANSI C++ forbids comparison between pointer and integer");
3538 if ((code0
== INTEGER_TYPE
|| code0
== REAL_TYPE
|| code0
== COMPLEX_TYPE
)
3540 (code1
== INTEGER_TYPE
|| code1
== REAL_TYPE
|| code1
== COMPLEX_TYPE
))
3542 int none_complex
= (code0
!= COMPLEX_TYPE
&& code1
!= COMPLEX_TYPE
);
3544 if (shorten
|| common
|| short_compare
)
3545 result_type
= common_type (type0
, type1
);
3547 /* For certain operations (which identify themselves by shorten != 0)
3548 if both args were extended from the same smaller type,
3549 do the arithmetic in that type and then extend.
3551 shorten !=0 and !=1 indicates a bitwise operation.
3552 For them, this optimization is safe only if
3553 both args are zero-extended or both are sign-extended.
3554 Otherwise, we might change the result.
3555 Eg, (short)-1 | (unsigned short)-1 is (int)-1
3556 but calculated in (unsigned short) it would be (unsigned short)-1. */
3558 if (shorten
&& none_complex
)
3560 int unsigned0
, unsigned1
;
3561 tree arg0
= get_narrower (op0
, &unsigned0
);
3562 tree arg1
= get_narrower (op1
, &unsigned1
);
3563 /* UNS is 1 if the operation to be done is an unsigned one. */
3564 int uns
= TREE_UNSIGNED (result_type
);
3567 final_type
= result_type
;
3569 /* Handle the case that OP0 does not *contain* a conversion
3570 but it *requires* conversion to FINAL_TYPE. */
3572 if (op0
== arg0
&& TREE_TYPE (op0
) != final_type
)
3573 unsigned0
= TREE_UNSIGNED (TREE_TYPE (op0
));
3574 if (op1
== arg1
&& TREE_TYPE (op1
) != final_type
)
3575 unsigned1
= TREE_UNSIGNED (TREE_TYPE (op1
));
3577 /* Now UNSIGNED0 is 1 if ARG0 zero-extends to FINAL_TYPE. */
3579 /* For bitwise operations, signedness of nominal type
3580 does not matter. Consider only how operands were extended. */
3584 /* Note that in all three cases below we refrain from optimizing
3585 an unsigned operation on sign-extended args.
3586 That would not be valid. */
3588 /* Both args variable: if both extended in same way
3589 from same width, do it in that width.
3590 Do it unsigned if args were zero-extended. */
3591 if ((TYPE_PRECISION (TREE_TYPE (arg0
))
3592 < TYPE_PRECISION (result_type
))
3593 && (TYPE_PRECISION (TREE_TYPE (arg1
))
3594 == TYPE_PRECISION (TREE_TYPE (arg0
)))
3595 && unsigned0
== unsigned1
3596 && (unsigned0
|| !uns
))
3598 = signed_or_unsigned_type (unsigned0
,
3599 common_type (TREE_TYPE (arg0
), TREE_TYPE (arg1
)));
3600 else if (TREE_CODE (arg0
) == INTEGER_CST
3601 && (unsigned1
|| !uns
)
3602 && (TYPE_PRECISION (TREE_TYPE (arg1
))
3603 < TYPE_PRECISION (result_type
))
3604 && (type
= signed_or_unsigned_type (unsigned1
,
3606 int_fits_type_p (arg0
, type
)))
3608 else if (TREE_CODE (arg1
) == INTEGER_CST
3609 && (unsigned0
|| !uns
)
3610 && (TYPE_PRECISION (TREE_TYPE (arg0
))
3611 < TYPE_PRECISION (result_type
))
3612 && (type
= signed_or_unsigned_type (unsigned0
,
3614 int_fits_type_p (arg1
, type
)))
3618 /* Shifts can be shortened if shifting right. */
3623 tree arg0
= get_narrower (op0
, &unsigned_arg
);
3625 final_type
= result_type
;
3627 if (arg0
== op0
&& final_type
== TREE_TYPE (op0
))
3628 unsigned_arg
= TREE_UNSIGNED (TREE_TYPE (op0
));
3630 if (TYPE_PRECISION (TREE_TYPE (arg0
)) < TYPE_PRECISION (result_type
)
3631 /* We can shorten only if the shift count is less than the
3632 number of bits in the smaller type size. */
3633 && TREE_INT_CST_HIGH (op1
) == 0
3634 && TYPE_PRECISION (TREE_TYPE (arg0
)) > TREE_INT_CST_LOW (op1
)
3635 /* If arg is sign-extended and then unsigned-shifted,
3636 we can simulate this with a signed shift in arg's type
3637 only if the extended result is at least twice as wide
3638 as the arg. Otherwise, the shift could use up all the
3639 ones made by sign-extension and bring in zeros.
3640 We can't optimize that case at all, but in most machines
3641 it never happens because available widths are 2**N. */
3642 && (!TREE_UNSIGNED (final_type
)
3644 || (((unsigned) 2 * TYPE_PRECISION (TREE_TYPE (arg0
)))
3645 <= TYPE_PRECISION (result_type
))))
3647 /* Do an unsigned shift if the operand was zero-extended. */
3649 = signed_or_unsigned_type (unsigned_arg
,
3651 /* Convert value-to-be-shifted to that type. */
3652 if (TREE_TYPE (op0
) != result_type
)
3653 op0
= cp_convert (result_type
, op0
);
3658 /* Comparison operations are shortened too but differently.
3659 They identify themselves by setting short_compare = 1. */
3663 /* Don't write &op0, etc., because that would prevent op0
3664 from being kept in a register.
3665 Instead, make copies of the our local variables and
3666 pass the copies by reference, then copy them back afterward. */
3667 tree xop0
= op0
, xop1
= op1
, xresult_type
= result_type
;
3668 enum tree_code xresultcode
= resultcode
;
3670 = shorten_compare (&xop0
, &xop1
, &xresult_type
, &xresultcode
);
3672 return cp_convert (boolean_type_node
, val
);
3673 op0
= xop0
, op1
= xop1
;
3675 resultcode
= xresultcode
;
3678 if (short_compare
&& warn_sign_compare
)
3680 int op0_signed
= ! TREE_UNSIGNED (TREE_TYPE (orig_op0
));
3681 int op1_signed
= ! TREE_UNSIGNED (TREE_TYPE (orig_op1
));
3683 int unsignedp0
, unsignedp1
;
3684 tree primop0
= get_narrower (op0
, &unsignedp0
);
3685 tree primop1
= get_narrower (op1
, &unsignedp1
);
3687 /* Check for comparison of different enum types. */
3688 if (flag_int_enum_equivalence
== 0
3689 && TREE_CODE (TREE_TYPE (orig_op0
)) == ENUMERAL_TYPE
3690 && TREE_CODE (TREE_TYPE (orig_op1
)) == ENUMERAL_TYPE
3691 && TYPE_MAIN_VARIANT (TREE_TYPE (orig_op0
))
3692 != TYPE_MAIN_VARIANT (TREE_TYPE (orig_op1
)))
3694 cp_warning ("comparison between `%#T' and `%#T'",
3695 TREE_TYPE (orig_op0
), TREE_TYPE (orig_op1
));
3698 /* Give warnings for comparisons between signed and unsigned
3699 quantities that may fail. */
3700 /* Do the checking based on the original operand trees, so that
3701 casts will be considered, but default promotions won't be. */
3703 /* Do not warn if the comparison is being done in a signed type,
3704 since the signed type will only be chosen if it can represent
3705 all the values of the unsigned type. */
3706 if (! TREE_UNSIGNED (result_type
))
3708 /* Do not warn if both operands are unsigned. */
3709 else if (op0_signed
== op1_signed
)
3711 /* Do not warn if the signed quantity is an unsuffixed
3712 integer literal (or some static constant expression
3713 involving such literals) and it is non-negative. */
3714 else if ((op0_signed
&& TREE_CODE (orig_op0
) == INTEGER_CST
3715 && tree_int_cst_sgn (orig_op0
) >= 0)
3716 || (op1_signed
&& TREE_CODE (orig_op1
) == INTEGER_CST
3717 && tree_int_cst_sgn (orig_op1
) >= 0))
3719 /* Do not warn if the comparison is an equality operation,
3720 the unsigned quantity is an integral constant and it does
3721 not use the most significant bit of result_type. */
3722 else if ((resultcode
== EQ_EXPR
|| resultcode
== NE_EXPR
)
3723 && ((op0_signed
&& TREE_CODE (orig_op1
) == INTEGER_CST
3724 && int_fits_type_p (orig_op1
, signed_type (result_type
))
3725 || (op1_signed
&& TREE_CODE (orig_op0
) == INTEGER_CST
3726 && int_fits_type_p (orig_op0
, signed_type (result_type
))))))
3729 warning ("comparison between signed and unsigned");
3731 /* Warn if two unsigned values are being compared in a size
3732 larger than their original size, and one (and only one) is the
3733 result of a `~' operator. This comparison will always fail.
3735 Also warn if one operand is a constant, and the constant does not
3736 have all bits set that are set in the ~ operand when it is
3739 if ((TREE_CODE (primop0
) == BIT_NOT_EXPR
)
3740 ^ (TREE_CODE (primop1
) == BIT_NOT_EXPR
))
3742 if (TREE_CODE (primop0
) == BIT_NOT_EXPR
)
3743 primop0
= get_narrower (TREE_OPERAND (op0
, 0), &unsignedp0
);
3744 if (TREE_CODE (primop1
) == BIT_NOT_EXPR
)
3745 primop1
= get_narrower (TREE_OPERAND (op1
, 0), &unsignedp1
);
3747 if (TREE_CODE (primop0
) == INTEGER_CST
3748 || TREE_CODE (primop1
) == INTEGER_CST
)
3751 HOST_WIDE_INT constant
, mask
;
3755 if (TREE_CODE (primop0
) == INTEGER_CST
)
3758 unsignedp
= unsignedp1
;
3759 constant
= TREE_INT_CST_LOW (primop0
);
3764 unsignedp
= unsignedp0
;
3765 constant
= TREE_INT_CST_LOW (primop1
);
3768 bits
= TYPE_PRECISION (TREE_TYPE (primop
));
3769 if (bits
< TYPE_PRECISION (result_type
)
3770 && bits
< HOST_BITS_PER_LONG
&& unsignedp
)
3772 mask
= (~ (HOST_WIDE_INT
) 0) << bits
;
3773 if ((mask
& constant
) != mask
)
3774 warning ("comparison of promoted ~unsigned with constant");
3777 else if (unsignedp0
&& unsignedp1
3778 && (TYPE_PRECISION (TREE_TYPE (primop0
))
3779 < TYPE_PRECISION (result_type
))
3780 && (TYPE_PRECISION (TREE_TYPE (primop1
))
3781 < TYPE_PRECISION (result_type
)))
3782 warning ("comparison of promoted ~unsigned with unsigned");
3787 /* At this point, RESULT_TYPE must be nonzero to avoid an error message.
3788 If CONVERTED is zero, both args will be converted to type RESULT_TYPE.
3789 Then the expression will be built.
3790 It will be given type FINAL_TYPE if that is nonzero;
3791 otherwise, it will be given type RESULT_TYPE. */
3795 cp_error ("invalid operands `%T' and `%T' to binary `%O'",
3796 TREE_TYPE (orig_op0
), TREE_TYPE (orig_op1
), error_code
);
3797 return error_mark_node
;
3802 if (TREE_TYPE (op0
) != result_type
)
3803 op0
= cp_convert (result_type
, op0
);
3804 if (TREE_TYPE (op1
) != result_type
)
3805 op1
= cp_convert (result_type
, op1
);
3808 if (build_type
== NULL_TREE
)
3809 build_type
= result_type
;
3812 register tree result
= build (resultcode
, build_type
, op0
, op1
);
3813 register tree folded
;
3815 folded
= fold (result
);
3816 if (folded
== result
)
3817 TREE_CONSTANT (folded
) = TREE_CONSTANT (op0
) & TREE_CONSTANT (op1
);
3818 if (final_type
!= 0)
3819 return cp_convert (final_type
, folded
);
3824 /* Return a tree for the sum or difference (RESULTCODE says which)
3825 of pointer PTROP and integer INTOP. */
3828 pointer_int_sum (resultcode
, ptrop
, intop
)
3829 enum tree_code resultcode
;
3830 register tree ptrop
, intop
;
3834 register tree result
;
3835 register tree folded
= fold (intop
);
3837 /* The result is a pointer of the same type that is being added. */
3839 register tree result_type
= TREE_TYPE (ptrop
);
3841 if (TREE_CODE (TREE_TYPE (result_type
)) == VOID_TYPE
)
3843 if (pedantic
|| warn_pointer_arith
)
3844 pedwarn ("ANSI C++ forbids using pointer of type `void *' in arithmetic");
3845 size_exp
= integer_one_node
;
3847 else if (TREE_CODE (TREE_TYPE (result_type
)) == FUNCTION_TYPE
)
3849 if (pedantic
|| warn_pointer_arith
)
3850 pedwarn ("ANSI C++ forbids using pointer to a function in arithmetic");
3851 size_exp
= integer_one_node
;
3853 else if (TREE_CODE (TREE_TYPE (result_type
)) == METHOD_TYPE
)
3855 if (pedantic
|| warn_pointer_arith
)
3856 pedwarn ("ANSI C++ forbids using pointer to a method in arithmetic");
3857 size_exp
= integer_one_node
;
3859 else if (TREE_CODE (TREE_TYPE (result_type
)) == OFFSET_TYPE
)
3861 if (pedantic
|| warn_pointer_arith
)
3862 pedwarn ("ANSI C++ forbids using pointer to a member in arithmetic");
3863 size_exp
= integer_one_node
;
3866 size_exp
= size_in_bytes (complete_type (TREE_TYPE (result_type
)));
3868 /* Needed to make OOPS V2R3 work. */
3870 if (TREE_CODE (intop
) == INTEGER_CST
3871 && TREE_INT_CST_LOW (intop
) == 0
3872 && TREE_INT_CST_HIGH (intop
) == 0)
3875 /* If what we are about to multiply by the size of the elements
3876 contains a constant term, apply distributive law
3877 and multiply that constant term separately.
3878 This helps produce common subexpressions. */
3880 if ((TREE_CODE (intop
) == PLUS_EXPR
|| TREE_CODE (intop
) == MINUS_EXPR
)
3881 && ! TREE_CONSTANT (intop
)
3882 && TREE_CONSTANT (TREE_OPERAND (intop
, 1))
3883 && TREE_CONSTANT (size_exp
))
3885 enum tree_code subcode
= resultcode
;
3886 if (TREE_CODE (intop
) == MINUS_EXPR
)
3887 subcode
= (subcode
== PLUS_EXPR
? MINUS_EXPR
: PLUS_EXPR
);
3888 ptrop
= build_binary_op (subcode
, ptrop
, TREE_OPERAND (intop
, 1), 1);
3889 intop
= TREE_OPERAND (intop
, 0);
3892 /* Convert the integer argument to a type the same size as sizetype
3893 so the multiply won't overflow spuriously. */
3895 if (TYPE_PRECISION (TREE_TYPE (intop
)) != TYPE_PRECISION (sizetype
))
3896 intop
= cp_convert (type_for_size (TYPE_PRECISION (sizetype
), 0), intop
);
3898 /* Replace the integer argument with a suitable product by the object size.
3899 Do this multiplication as signed, then convert to the appropriate
3900 pointer type (actually unsigned integral). */
3902 intop
= cp_convert (result_type
,
3903 build_binary_op (MULT_EXPR
, intop
,
3904 cp_convert (TREE_TYPE (intop
), size_exp
), 1));
3906 /* Create the sum or difference. */
3908 result
= build (resultcode
, result_type
, ptrop
, intop
);
3910 folded
= fold (result
);
3911 if (folded
== result
)
3912 TREE_CONSTANT (folded
) = TREE_CONSTANT (ptrop
) & TREE_CONSTANT (intop
);
3916 /* Return a tree for the difference of pointers OP0 and OP1.
3917 The resulting tree has type int. */
3920 pointer_diff (op0
, op1
)
3921 register tree op0
, op1
;
3923 register tree result
, folded
;
3924 tree restype
= ptrdiff_type_node
;
3925 tree target_type
= TREE_TYPE (TREE_TYPE (op0
));
3927 if (pedantic
|| warn_pointer_arith
)
3929 if (TREE_CODE (target_type
) == VOID_TYPE
)
3930 pedwarn ("ANSI C++ forbids using pointer of type `void *' in subtraction");
3931 if (TREE_CODE (target_type
) == FUNCTION_TYPE
)
3932 pedwarn ("ANSI C++ forbids using pointer to a function in subtraction");
3933 if (TREE_CODE (target_type
) == METHOD_TYPE
)
3934 pedwarn ("ANSI C++ forbids using pointer to a method in subtraction");
3935 if (TREE_CODE (target_type
) == OFFSET_TYPE
)
3936 pedwarn ("ANSI C++ forbids using pointer to a member in subtraction");
3939 /* First do the subtraction as integers;
3940 then drop through to build the divide operator. */
3942 op0
= build_binary_op (MINUS_EXPR
,
3943 cp_convert (restype
, op0
), cp_convert (restype
, op1
), 1);
3945 /* This generates an error if op1 is a pointer to an incomplete type. */
3946 if (TYPE_SIZE (TREE_TYPE (TREE_TYPE (op1
))) == 0)
3947 error ("arithmetic on pointer to an incomplete type");
3949 op1
= ((TREE_CODE (target_type
) == VOID_TYPE
3950 || TREE_CODE (target_type
) == FUNCTION_TYPE
3951 || TREE_CODE (target_type
) == METHOD_TYPE
3952 || TREE_CODE (target_type
) == OFFSET_TYPE
)
3954 : size_in_bytes (target_type
));
3956 /* Do the division. */
3958 result
= build (EXACT_DIV_EXPR
, restype
, op0
, cp_convert (restype
, op1
));
3960 folded
= fold (result
);
3961 if (folded
== result
)
3962 TREE_CONSTANT (folded
) = TREE_CONSTANT (op0
) & TREE_CONSTANT (op1
);
3966 /* Handle the case of taking the address of a COMPONENT_REF.
3967 Called by `build_unary_op' and `build_up_reference'.
3969 ARG is the COMPONENT_REF whose address we want.
3970 ARGTYPE is the pointer type that this address should have.
3971 MSG is an error message to print if this COMPONENT_REF is not
3972 addressable (such as a bitfield). */
3975 build_component_addr (arg
, argtype
, msg
)
3979 tree field
= TREE_OPERAND (arg
, 1);
3980 tree basetype
= decl_type_context (field
);
3981 tree rval
= build_unary_op (ADDR_EXPR
, TREE_OPERAND (arg
, 0), 0);
3983 if (DECL_BIT_FIELD (field
))
3985 error (msg
, IDENTIFIER_POINTER (DECL_NAME (field
)));
3986 return error_mark_node
;
3989 if (TREE_CODE (field
) == FIELD_DECL
3990 && TYPE_USES_COMPLEX_INHERITANCE (basetype
))
3992 /* Can't convert directly to ARGTYPE, since that
3993 may have the same pointer type as one of our
3995 rval
= build1 (NOP_EXPR
, argtype
,
3996 convert_pointer_to (basetype
, rval
));
3997 TREE_CONSTANT (rval
) = TREE_CONSTANT (TREE_OPERAND (rval
, 0));
4000 /* This conversion is harmless. */
4001 rval
= convert_force (argtype
, rval
, 0);
4003 if (! integer_zerop (DECL_FIELD_BITPOS (field
)))
4005 tree offset
= size_binop (EASY_DIV_EXPR
, DECL_FIELD_BITPOS (field
),
4006 size_int (BITS_PER_UNIT
));
4007 int flag
= TREE_CONSTANT (rval
);
4008 rval
= fold (build (PLUS_EXPR
, argtype
,
4009 rval
, cp_convert (argtype
, offset
)));
4010 TREE_CONSTANT (rval
) = flag
;
4015 /* Construct and perhaps optimize a tree representation
4016 for a unary operation. CODE, a tree_code, specifies the operation
4017 and XARG is the operand. */
4020 build_x_unary_op (code
, xarg
)
4021 enum tree_code code
;
4024 if (processing_template_decl
)
4025 return build_min_nt (code
, xarg
, NULL_TREE
);
4027 /* & rec, on incomplete RECORD_TYPEs is the simple opr &, not an
4029 if (code
== ADDR_EXPR
4030 && TREE_CODE (xarg
) != TEMPLATE_ID_EXPR
4031 && ((IS_AGGR_TYPE_CODE (TREE_CODE (TREE_TYPE (xarg
)))
4032 && TYPE_SIZE (TREE_TYPE (xarg
)) == NULL_TREE
)
4033 || (TREE_CODE (xarg
) == OFFSET_REF
)))
4034 /* don't look for a function */;
4039 if (flag_ansi_overloading
)
4041 rval
= build_new_op (code
, LOOKUP_NORMAL
, xarg
,
4042 NULL_TREE
, NULL_TREE
);
4043 if (rval
|| code
!= ADDR_EXPR
)
4048 rval
= build_opfncall (code
, LOOKUP_SPECULATIVELY
, xarg
,
4049 NULL_TREE
, NULL_TREE
);
4051 return build_opfncall (code
, LOOKUP_NORMAL
, xarg
,
4052 NULL_TREE
, NULL_TREE
);
4056 if (code
== ADDR_EXPR
)
4058 if (TREE_CODE (xarg
) == TARGET_EXPR
)
4059 warning ("taking address of temporary");
4062 return build_unary_op (code
, xarg
, 0);
4065 /* Just like truthvalue_conversion, but we want a CLEANUP_POINT_EXPR. */
4068 condition_conversion (expr
)
4072 if (processing_template_decl
)
4074 t
= cp_convert (boolean_type_node
, expr
);
4075 t
= fold (build1 (CLEANUP_POINT_EXPR
, boolean_type_node
, t
));
4079 /* C++: Must handle pointers to members.
4081 Perhaps type instantiation should be extended to handle conversion
4082 from aggregates to types we don't yet know we want? (Or are those
4083 cases typically errors which should be reported?)
4085 NOCONVERT nonzero suppresses the default promotions
4086 (such as from short to int). */
4089 build_unary_op (code
, xarg
, noconvert
)
4090 enum tree_code code
;
4094 /* No default_conversion here. It causes trouble for ADDR_EXPR. */
4095 register tree arg
= xarg
;
4096 register tree argtype
= 0;
4097 char *errstring
= NULL
;
4100 if (arg
== error_mark_node
)
4101 return error_mark_node
;
4106 /* This is used for unary plus, because a CONVERT_EXPR
4107 is enough to prevent anybody from looking inside for
4108 associativity, but won't generate any code. */
4109 if (!(arg
= build_expr_type_conversion
4110 (WANT_ARITH
| WANT_ENUM
| WANT_POINTER
, arg
, 1)))
4111 errstring
= "wrong type argument to unary plus";
4115 arg
= default_conversion (arg
);
4116 arg
= build1 (NON_LVALUE_EXPR
, TREE_TYPE (arg
), arg
);
4121 if (!(arg
= build_expr_type_conversion (WANT_ARITH
| WANT_ENUM
, arg
, 1)))
4122 errstring
= "wrong type argument to unary minus";
4123 else if (!noconvert
)
4124 arg
= default_conversion (arg
);
4128 if (TREE_CODE (TREE_TYPE (arg
)) == COMPLEX_TYPE
)
4132 arg
= default_conversion (arg
);
4134 else if (!(arg
= build_expr_type_conversion (WANT_INT
| WANT_ENUM
,
4136 errstring
= "wrong type argument to bit-complement";
4137 else if (!noconvert
)
4138 arg
= default_conversion (arg
);
4142 if (!(arg
= build_expr_type_conversion (WANT_ARITH
| WANT_ENUM
, arg
, 1)))
4143 errstring
= "wrong type argument to abs";
4144 else if (!noconvert
)
4145 arg
= default_conversion (arg
);
4149 /* Conjugating a real value is a no-op, but allow it anyway. */
4150 if (!(arg
= build_expr_type_conversion (WANT_ARITH
| WANT_ENUM
, arg
, 1)))
4151 errstring
= "wrong type argument to conjugation";
4152 else if (!noconvert
)
4153 arg
= default_conversion (arg
);
4156 case TRUTH_NOT_EXPR
:
4157 arg
= cp_convert (boolean_type_node
, arg
);
4158 val
= invert_truthvalue (arg
);
4159 if (arg
!= error_mark_node
)
4161 errstring
= "in argument to unary !";
4168 if (TREE_CODE (arg
) == COMPLEX_CST
)
4169 return TREE_REALPART (arg
);
4170 else if (TREE_CODE (TREE_TYPE (arg
)) == COMPLEX_TYPE
)
4171 return fold (build1 (REALPART_EXPR
, TREE_TYPE (TREE_TYPE (arg
)), arg
));
4176 if (TREE_CODE (arg
) == COMPLEX_CST
)
4177 return TREE_IMAGPART (arg
);
4178 else if (TREE_CODE (TREE_TYPE (arg
)) == COMPLEX_TYPE
)
4179 return fold (build1 (IMAGPART_EXPR
, TREE_TYPE (TREE_TYPE (arg
)), arg
));
4181 return cp_convert (TREE_TYPE (arg
), integer_zero_node
);
4183 case PREINCREMENT_EXPR
:
4184 case POSTINCREMENT_EXPR
:
4185 case PREDECREMENT_EXPR
:
4186 case POSTDECREMENT_EXPR
:
4187 /* Handle complex lvalues (when permitted)
4188 by reduction to simpler cases. */
4190 val
= unary_complex_lvalue (code
, arg
);
4194 /* Increment or decrement the real part of the value,
4195 and don't change the imaginary part. */
4196 if (TREE_CODE (TREE_TYPE (arg
)) == COMPLEX_TYPE
)
4200 arg
= stabilize_reference (arg
);
4201 real
= build_unary_op (REALPART_EXPR
, arg
, 1);
4202 imag
= build_unary_op (IMAGPART_EXPR
, arg
, 1);
4203 return build (COMPLEX_EXPR
, TREE_TYPE (arg
),
4204 build_unary_op (code
, real
, 1), imag
);
4207 /* Report invalid types. */
4209 if (!(arg
= build_expr_type_conversion (WANT_ARITH
| WANT_POINTER
,
4212 if (code
== PREINCREMENT_EXPR
)
4213 errstring
="no pre-increment operator for type";
4214 else if (code
== POSTINCREMENT_EXPR
)
4215 errstring
="no post-increment operator for type";
4216 else if (code
== PREDECREMENT_EXPR
)
4217 errstring
="no pre-decrement operator for type";
4219 errstring
="no post-decrement operator for type";
4223 /* Report something read-only. */
4225 if (TYPE_READONLY (TREE_TYPE (arg
))
4226 || TREE_READONLY (arg
))
4227 readonly_error (arg
, ((code
== PREINCREMENT_EXPR
4228 || code
== POSTINCREMENT_EXPR
)
4229 ? "increment" : "decrement"),
4234 tree result_type
= TREE_TYPE (arg
);
4236 arg
= get_unwidened (arg
, 0);
4237 argtype
= TREE_TYPE (arg
);
4239 /* ARM $5.2.5 last annotation says this should be forbidden. */
4240 if (TREE_CODE (argtype
) == ENUMERAL_TYPE
)
4241 pedwarn ("ANSI C++ forbids %sing an enum",
4242 (code
== PREINCREMENT_EXPR
|| code
== POSTINCREMENT_EXPR
)
4243 ? "increment" : "decrement");
4245 /* Compute the increment. */
4247 if (TREE_CODE (argtype
) == POINTER_TYPE
)
4249 enum tree_code tmp
= TREE_CODE (TREE_TYPE (argtype
));
4250 if (TYPE_SIZE (complete_type (TREE_TYPE (argtype
))) == 0)
4251 cp_error ("cannot %s a pointer to incomplete type `%T'",
4252 ((code
== PREINCREMENT_EXPR
4253 || code
== POSTINCREMENT_EXPR
)
4254 ? "increment" : "decrement"), TREE_TYPE (argtype
));
4255 else if (tmp
== FUNCTION_TYPE
|| tmp
== METHOD_TYPE
4256 || tmp
== VOID_TYPE
|| tmp
== OFFSET_TYPE
)
4257 cp_pedwarn ("ANSI C++ forbids %sing a pointer of type `%T'",
4258 ((code
== PREINCREMENT_EXPR
4259 || code
== POSTINCREMENT_EXPR
)
4260 ? "increment" : "decrement"), argtype
);
4261 inc
= c_sizeof_nowarn (TREE_TYPE (argtype
));
4264 inc
= integer_one_node
;
4266 inc
= cp_convert (argtype
, inc
);
4268 /* Handle incrementing a cast-expression. */
4270 switch (TREE_CODE (arg
))
4275 case FIX_TRUNC_EXPR
:
4276 case FIX_FLOOR_EXPR
:
4277 case FIX_ROUND_EXPR
:
4280 tree incremented
, modify
, value
, compound
;
4281 if (! lvalue_p (arg
) && pedantic
)
4282 pedwarn ("cast to non-reference type used as lvalue");
4283 arg
= stabilize_reference (arg
);
4284 if (code
== PREINCREMENT_EXPR
|| code
== PREDECREMENT_EXPR
)
4287 value
= save_expr (arg
);
4288 incremented
= build (((code
== PREINCREMENT_EXPR
4289 || code
== POSTINCREMENT_EXPR
)
4290 ? PLUS_EXPR
: MINUS_EXPR
),
4291 argtype
, value
, inc
);
4292 TREE_SIDE_EFFECTS (incremented
) = 1;
4294 modify
= build_modify_expr (arg
, NOP_EXPR
, incremented
);
4295 compound
= build (COMPOUND_EXPR
, TREE_TYPE (arg
), modify
, value
);
4297 /* Eliminate warning about unused result of + or -. */
4298 TREE_NO_UNUSED_WARNING (compound
) = 1;
4303 /* Complain about anything else that is not a true lvalue. */
4304 if (!lvalue_or_else (arg
, ((code
== PREINCREMENT_EXPR
4305 || code
== POSTINCREMENT_EXPR
)
4306 ? "increment" : "decrement")))
4307 return error_mark_node
;
4309 /* Forbid using -- on `bool'. */
4310 if (TREE_TYPE (arg
) == boolean_type_node
)
4312 if (code
== POSTDECREMENT_EXPR
|| code
== PREDECREMENT_EXPR
)
4314 cp_error ("invalid use of `--' on bool variable `%D'", arg
);
4315 return error_mark_node
;
4318 /* This will only work if someone can convince Kenner to accept
4319 my patch to expand_increment. (jason) */
4320 val
= build (code
, TREE_TYPE (arg
), arg
, inc
);
4322 if (code
== POSTINCREMENT_EXPR
)
4324 arg
= stabilize_reference (arg
);
4325 val
= build (MODIFY_EXPR
, TREE_TYPE (arg
), arg
,
4327 TREE_SIDE_EFFECTS (val
) = 1;
4328 arg
= save_expr (arg
);
4329 val
= build (COMPOUND_EXPR
, TREE_TYPE (arg
), val
, arg
);
4330 val
= build (COMPOUND_EXPR
, TREE_TYPE (arg
), arg
, val
);
4333 val
= build (MODIFY_EXPR
, TREE_TYPE (arg
), arg
,
4338 val
= build (code
, TREE_TYPE (arg
), arg
, inc
);
4340 TREE_SIDE_EFFECTS (val
) = 1;
4341 return cp_convert (result_type
, val
);
4345 /* Note that this operation never does default_conversion
4346 regardless of NOCONVERT. */
4348 argtype
= TREE_TYPE (arg
);
4349 if (TREE_CODE (argtype
) == REFERENCE_TYPE
)
4353 build_pointer_type (TREE_TYPE (TREE_TYPE (arg
))), arg
);
4354 TREE_CONSTANT (arg
) = TREE_CONSTANT (TREE_OPERAND (arg
, 0));
4358 && TREE_CODE (arg
) == FUNCTION_DECL
4360 && DECL_CONTEXT (arg
) == NULL_TREE
4361 && IDENTIFIER_LENGTH (DECL_NAME (arg
)) == 4
4362 && IDENTIFIER_POINTER (DECL_NAME (arg
))[0] == 'm'
4363 && ! strcmp (IDENTIFIER_POINTER (DECL_NAME (arg
)), "main"))
4365 pedwarn ("taking address of function `main'");
4367 /* Let &* cancel out to simplify resulting code. */
4368 if (TREE_CODE (arg
) == INDIRECT_REF
)
4370 /* We don't need to have `current_class_ptr' wrapped in a
4371 NON_LVALUE_EXPR node. */
4372 if (arg
== current_class_ref
)
4373 return current_class_ptr
;
4375 arg
= TREE_OPERAND (arg
, 0);
4376 if (TREE_CODE (TREE_TYPE (arg
)) == REFERENCE_TYPE
)
4380 build_pointer_type (TREE_TYPE (TREE_TYPE (arg
))), arg
);
4381 TREE_CONSTANT (arg
) = TREE_CONSTANT (TREE_OPERAND (arg
, 0));
4383 else if (lvalue_p (arg
))
4384 /* Don't let this be an lvalue. */
4385 return non_lvalue (arg
);
4389 /* For &x[y], return x+y */
4390 if (TREE_CODE (arg
) == ARRAY_REF
)
4392 if (mark_addressable (TREE_OPERAND (arg
, 0)) == 0)
4393 return error_mark_node
;
4394 return build_binary_op (PLUS_EXPR
, TREE_OPERAND (arg
, 0),
4395 TREE_OPERAND (arg
, 1), 1);
4398 /* Uninstantiated types are all functions. Taking the
4399 address of a function is a no-op, so just return the
4402 if (TREE_CODE (arg
) == IDENTIFIER_NODE
4403 && IDENTIFIER_OPNAME_P (arg
))
4405 my_friendly_abort (117);
4406 /* We don't know the type yet, so just work around the problem.
4407 We know that this will resolve to an lvalue. */
4408 return build1 (ADDR_EXPR
, unknown_type_node
, arg
);
4411 if (TREE_CODE (arg
) == TREE_LIST
)
4413 if (TREE_CODE (TREE_VALUE (arg
)) == FUNCTION_DECL
4414 && DECL_CHAIN (TREE_VALUE (arg
)) == NULL_TREE
)
4415 /* Unique overloaded non-member function. */
4416 return build_unary_op (ADDR_EXPR
, TREE_VALUE (arg
), 0);
4417 if (TREE_CHAIN (arg
) == NULL_TREE
4418 && TREE_CODE (TREE_VALUE (arg
)) == TREE_LIST
4419 && DECL_CHAIN (TREE_VALUE (TREE_VALUE (arg
))) == NULL_TREE
)
4420 /* Unique overloaded member function. */
4421 return build_unary_op (ADDR_EXPR
, TREE_VALUE (TREE_VALUE (arg
)),
4423 return build1 (ADDR_EXPR
, unknown_type_node
, arg
);
4425 else if (TREE_CODE (arg
) == TEMPLATE_ID_EXPR
)
4430 /* We don't require a match here; it's possible that the
4431 context (like a cast to a particular type) will resolve
4432 the particular choice of template. */
4433 fn
= determine_explicit_specialization (arg
, NULL_TREE
,
4439 fn
= instantiate_template (fn
, targs
);
4440 mark_addressable (fn
);
4441 return build_unary_op (ADDR_EXPR
, fn
, 0);
4444 return build1 (ADDR_EXPR
, unknown_type_node
, arg
);
4447 /* Handle complex lvalues (when permitted)
4448 by reduction to simpler cases. */
4449 val
= unary_complex_lvalue (code
, arg
);
4453 switch (TREE_CODE (arg
))
4458 case FIX_TRUNC_EXPR
:
4459 case FIX_FLOOR_EXPR
:
4460 case FIX_ROUND_EXPR
:
4462 if (! lvalue_p (arg
) && pedantic
)
4463 pedwarn ("taking the address of a cast to non-reference type");
4466 /* Allow the address of a constructor if all the elements
4468 if (TREE_CODE (arg
) == CONSTRUCTOR
&& TREE_CONSTANT (arg
))
4470 /* Anything not already handled and not a true memory reference
4472 else if (TREE_CODE (argtype
) != FUNCTION_TYPE
4473 && TREE_CODE (argtype
) != METHOD_TYPE
4474 && !lvalue_or_else (arg
, "unary `&'"))
4475 return error_mark_node
;
4477 /* Ordinary case; arg is a COMPONENT_REF or a decl. */
4478 /* If the lvalue is const or volatile,
4479 merge that into the type that the address will point to. */
4480 if (TREE_CODE_CLASS (TREE_CODE (arg
)) == 'd'
4481 || TREE_CODE_CLASS (TREE_CODE (arg
)) == 'r')
4483 if (TREE_READONLY (arg
) || TREE_THIS_VOLATILE (arg
))
4484 argtype
= cp_build_type_variant (argtype
,
4485 TREE_READONLY (arg
),
4486 TREE_THIS_VOLATILE (arg
));
4489 argtype
= build_pointer_type (argtype
);
4491 if (mark_addressable (arg
) == 0)
4492 return error_mark_node
;
4497 if (TREE_CODE (arg
) == COMPONENT_REF
)
4498 addr
= build_component_addr (arg
, argtype
,
4499 "attempt to take address of bit-field structure member `%s'");
4501 addr
= build1 (code
, argtype
, arg
);
4503 /* Address of a static or external variable or
4504 function counts as a constant */
4506 TREE_CONSTANT (addr
) = 1;
4508 if (TREE_CODE (argtype
) == POINTER_TYPE
4509 && TREE_CODE (TREE_TYPE (argtype
)) == METHOD_TYPE
)
4511 build_ptrmemfunc_type (argtype
);
4512 addr
= build_ptrmemfunc (argtype
, addr
, 0);
4522 argtype
= TREE_TYPE (arg
);
4523 return fold (build1 (code
, argtype
, arg
));
4527 return error_mark_node
;
4531 /* If CONVERSIONS is a conversion expression or a nested sequence of such,
4532 convert ARG with the same conversions in the same order
4533 and return the result. */
4536 convert_sequence (conversions
, arg
)
4540 switch (TREE_CODE (conversions
))
4545 case FIX_TRUNC_EXPR
:
4546 case FIX_FLOOR_EXPR
:
4547 case FIX_ROUND_EXPR
:
4549 return cp_convert (TREE_TYPE (conversions
),
4550 convert_sequence (TREE_OPERAND (conversions
, 0),
4559 /* Apply unary lvalue-demanding operator CODE to the expression ARG
4560 for certain kinds of expressions which are not really lvalues
4561 but which we can accept as lvalues.
4563 If ARG is not a kind of expression we can handle, return zero. */
4566 unary_complex_lvalue (code
, arg
)
4567 enum tree_code code
;
4570 /* Handle (a, b) used as an "lvalue". */
4571 if (TREE_CODE (arg
) == COMPOUND_EXPR
)
4573 tree real_result
= build_unary_op (code
, TREE_OPERAND (arg
, 1), 0);
4574 return build (COMPOUND_EXPR
, TREE_TYPE (real_result
),
4575 TREE_OPERAND (arg
, 0), real_result
);
4578 /* Handle (a ? b : c) used as an "lvalue". */
4579 if (TREE_CODE (arg
) == COND_EXPR
)
4580 return rationalize_conditional_expr (code
, arg
);
4582 if (TREE_CODE (arg
) == MODIFY_EXPR
4583 || TREE_CODE (arg
) == PREINCREMENT_EXPR
4584 || TREE_CODE (arg
) == PREDECREMENT_EXPR
)
4585 return unary_complex_lvalue
4586 (code
, build (COMPOUND_EXPR
, TREE_TYPE (TREE_OPERAND (arg
, 0)),
4587 arg
, TREE_OPERAND (arg
, 0)));
4589 if (code
!= ADDR_EXPR
)
4592 /* Handle (a = b) used as an "lvalue" for `&'. */
4593 if (TREE_CODE (arg
) == MODIFY_EXPR
4594 || TREE_CODE (arg
) == INIT_EXPR
)
4596 tree real_result
= build_unary_op (code
, TREE_OPERAND (arg
, 0), 0);
4597 arg
= build (COMPOUND_EXPR
, TREE_TYPE (real_result
), arg
, real_result
);
4598 TREE_NO_UNUSED_WARNING (arg
) = 1;
4602 if (TREE_CODE (TREE_TYPE (arg
)) == FUNCTION_TYPE
4603 || TREE_CODE (TREE_TYPE (arg
)) == METHOD_TYPE
4604 || TREE_CODE (TREE_TYPE (arg
)) == OFFSET_TYPE
)
4606 /* The representation of something of type OFFSET_TYPE
4607 is really the representation of a pointer to it.
4608 Here give the representation its true type. */
4611 my_friendly_assert (TREE_CODE (arg
) != SCOPE_REF
, 313);
4613 if (TREE_CODE (arg
) != OFFSET_REF
)
4616 t
= TREE_OPERAND (arg
, 1);
4618 if (TREE_CODE (t
) == FUNCTION_DECL
) /* Check all this code for right semantics. */
4619 return build_unary_op (ADDR_EXPR
, t
, 0);
4620 if (TREE_CODE (t
) == VAR_DECL
)
4621 return build_unary_op (ADDR_EXPR
, t
, 0);
4625 tree offset
= integer_zero_node
;
4627 if (TREE_OPERAND (arg
, 0)
4628 && (TREE_CODE (TREE_OPERAND (arg
, 0)) != NOP_EXPR
4629 || TREE_OPERAND (TREE_OPERAND (arg
, 0), 0) != error_mark_node
))
4630 if (TREE_CODE (t
) != FIELD_DECL
)
4632 /* Don't know if this should return address to just
4633 _DECL, or actual address resolved in this expression. */
4634 sorry ("address of bound pointer-to-member expression");
4635 return error_mark_node
;
4638 type
= TREE_TYPE (TREE_OPERAND (arg
, 0));
4640 if (TREE_CODE (TREE_TYPE (arg
)) == OFFSET_TYPE
)
4642 /* Add in the offset to the intermediate subobject, if any. */
4643 offset
= get_delta_difference (TYPE_OFFSET_BASETYPE (TREE_TYPE (arg
)),
4646 type
= TYPE_OFFSET_BASETYPE (TREE_TYPE (arg
));
4649 /* Now in the offset to the final subobject. */
4650 offset
= size_binop (PLUS_EXPR
,
4652 get_delta_difference (DECL_FIELD_CONTEXT (t
),
4656 /* Add in the offset to the field. */
4657 offset
= size_binop (PLUS_EXPR
, offset
,
4658 size_binop (EASY_DIV_EXPR
,
4659 DECL_FIELD_BITPOS (t
),
4660 size_int (BITS_PER_UNIT
)));
4662 /* We offset all pointer to data memebers by 1 so that we can
4663 distinguish between a null pointer to data member and the first
4664 data member of a structure. */
4665 offset
= size_binop (PLUS_EXPR
, offset
, size_int (1));
4667 return cp_convert (build_pointer_type (TREE_TYPE (arg
)), offset
);
4672 /* We permit compiler to make function calls returning
4673 objects of aggregate type look like lvalues. */
4677 if (TREE_CODE (targ
) == SAVE_EXPR
)
4678 targ
= TREE_OPERAND (targ
, 0);
4680 if (TREE_CODE (targ
) == CALL_EXPR
&& IS_AGGR_TYPE (TREE_TYPE (targ
)))
4682 if (TREE_CODE (arg
) == SAVE_EXPR
)
4685 targ
= build_cplus_new (TREE_TYPE (arg
), arg
);
4686 return build1 (ADDR_EXPR
, build_pointer_type (TREE_TYPE (arg
)), targ
);
4689 if (TREE_CODE (arg
) == SAVE_EXPR
&& TREE_CODE (targ
) == INDIRECT_REF
)
4690 return build (SAVE_EXPR
, build_pointer_type (TREE_TYPE (arg
)),
4691 TREE_OPERAND (targ
, 0), current_function_decl
, NULL
);
4694 /* Don't let anything else be handled specially. */
4698 /* Mark EXP saying that we need to be able to take the
4699 address of it; it should not be allocated in a register.
4700 Value is 1 if successful.
4702 C++: we do not allow `current_class_ptr' to be addressable. */
4705 mark_addressable (exp
)
4708 register tree x
= exp
;
4710 if (TREE_ADDRESSABLE (x
) == 1)
4714 switch (TREE_CODE (x
))
4721 x
= TREE_OPERAND (x
, 0);
4725 if (x
== current_class_ptr
)
4727 if (! flag_this_is_variable
)
4728 error ("address of `this' not available");
4729 TREE_ADDRESSABLE (x
) = 1; /* so compiler doesn't die later */
4730 put_var_into_stack (x
);
4734 if (TREE_STATIC (x
) && TREE_READONLY (x
)
4735 && DECL_RTL (x
) != 0
4736 && ! DECL_IN_MEMORY_P (x
))
4738 /* We thought this would make a good constant variable,
4739 but we were wrong. */
4740 push_obstacks_nochange ();
4741 end_temporary_allocation ();
4743 TREE_ASM_WRITTEN (x
) = 0;
4745 rest_of_decl_compilation (x
, 0, IDENTIFIER_LOCAL_VALUE (x
) == 0, 0);
4746 TREE_ADDRESSABLE (x
) = 1;
4752 /* Caller should not be trying to mark initialized
4753 constant fields addressable. */
4754 my_friendly_assert (DECL_LANG_SPECIFIC (x
) == 0
4755 || DECL_IN_AGGR_P (x
) == 0
4757 || DECL_EXTERNAL (x
), 314);
4761 if (DECL_REGISTER (x
) && !TREE_ADDRESSABLE (x
)
4762 && !DECL_ARTIFICIAL (x
) && extra_warnings
)
4763 cp_warning ("address requested for `%D', which is declared `register'",
4765 put_var_into_stack (x
);
4766 TREE_ADDRESSABLE (x
) = 1;
4770 /* We have to test both conditions here. The first may
4771 be non-zero in the case of processing a default function.
4772 The second may be non-zero in the case of a template function. */
4773 x
= DECL_MAIN_VARIANT (x
);
4774 if ((DECL_THIS_INLINE (x
) || DECL_PENDING_INLINE_INFO (x
))
4775 && (DECL_CONTEXT (x
) == NULL_TREE
4776 || TREE_CODE_CLASS (TREE_CODE (DECL_CONTEXT (x
))) != 't'
4777 || ! CLASSTYPE_INTERFACE_ONLY (DECL_CONTEXT (x
))))
4779 mark_inline_for_output (x
);
4780 if (x
== current_function_decl
)
4781 DECL_EXTERNAL (x
) = 0;
4783 if (DECL_TEMPLATE_INFO (x
) && !DECL_TEMPLATE_SPECIALIZATION (x
))
4785 TREE_ADDRESSABLE (x
) = 1;
4787 TREE_ADDRESSABLE (DECL_ASSEMBLER_NAME (x
)) = 1;
4791 TREE_ADDRESSABLE (x
) = 1;
4795 TREE_ADDRESSABLE (x
) = 1;
4796 mark_addressable (TREE_OPERAND (x
, 0));
4804 /* Build and return a conditional expression IFEXP ? OP1 : OP2. */
4807 build_x_conditional_expr (ifexp
, op1
, op2
)
4808 tree ifexp
, op1
, op2
;
4810 tree rval
= NULL_TREE
;
4812 if (processing_template_decl
)
4813 return build_min_nt (COND_EXPR
, ifexp
, op1
, op2
);
4815 if (flag_ansi_overloading
)
4816 return build_new_op (COND_EXPR
, LOOKUP_NORMAL
, ifexp
, op1
, op2
);
4818 /* See comments in `build_x_binary_op'. */
4820 rval
= build_opfncall (COND_EXPR
, LOOKUP_SPECULATIVELY
, ifexp
, op1
, op2
);
4822 return build_opfncall (COND_EXPR
, LOOKUP_NORMAL
, ifexp
, op1
, op2
);
4824 return build_conditional_expr (ifexp
, op1
, op2
);
4828 build_conditional_expr (ifexp
, op1
, op2
)
4829 tree ifexp
, op1
, op2
;
4831 register tree type1
;
4832 register tree type2
;
4833 register enum tree_code code1
;
4834 register enum tree_code code2
;
4835 register tree result_type
= NULL_TREE
;
4837 /* If second operand is omitted, it is the same as the first one;
4838 make sure it is calculated only once. */
4842 pedwarn ("ANSI C++ forbids omitting the middle term of a ?: expression");
4843 ifexp
= op1
= save_expr (ifexp
);
4846 ifexp
= cp_convert (boolean_type_node
, ifexp
);
4848 if (TREE_CODE (ifexp
) == ERROR_MARK
)
4849 return error_mark_node
;
4851 op1
= require_instantiated_type (TREE_TYPE (op2
), op1
, error_mark_node
);
4852 if (op1
== error_mark_node
)
4853 return error_mark_node
;
4854 op2
= require_instantiated_type (TREE_TYPE (op1
), op2
, error_mark_node
);
4855 if (op2
== error_mark_node
)
4856 return error_mark_node
;
4858 /* C++: REFERENCE_TYPES must be dereferenced. */
4859 type1
= TREE_TYPE (op1
);
4860 code1
= TREE_CODE (type1
);
4861 type2
= TREE_TYPE (op2
);
4862 code2
= TREE_CODE (type2
);
4864 if (code1
== REFERENCE_TYPE
)
4866 op1
= convert_from_reference (op1
);
4867 type1
= TREE_TYPE (op1
);
4868 code1
= TREE_CODE (type1
);
4870 if (code2
== REFERENCE_TYPE
)
4872 op2
= convert_from_reference (op2
);
4873 type2
= TREE_TYPE (op2
);
4874 code2
= TREE_CODE (type2
);
4877 /* Don't promote the operands separately if they promote
4878 the same way. Return the unpromoted type and let the combined
4879 value get promoted if necessary. */
4881 if (TYPE_MAIN_VARIANT (type1
) == TYPE_MAIN_VARIANT (type2
)
4882 && code2
!= ARRAY_TYPE
4883 && code2
!= FUNCTION_TYPE
4884 && code2
!= METHOD_TYPE
)
4888 if (TREE_CONSTANT (ifexp
)
4889 && (TREE_CODE (ifexp
) == INTEGER_CST
4890 || TREE_CODE (ifexp
) == ADDR_EXPR
))
4891 return (integer_zerop (ifexp
) ? op2
: op1
);
4893 if (TREE_CODE (op1
) == CONST_DECL
)
4894 op1
= DECL_INITIAL (op1
);
4895 else if (TREE_READONLY_DECL_P (op1
))
4896 op1
= decl_constant_value (op1
);
4897 if (TREE_CODE (op2
) == CONST_DECL
)
4898 op2
= DECL_INITIAL (op2
);
4899 else if (TREE_READONLY_DECL_P (op2
))
4900 op2
= decl_constant_value (op2
);
4902 type1
= cp_build_type_variant
4904 TREE_READONLY (op1
) || TREE_READONLY (op2
),
4905 TREE_THIS_VOLATILE (op1
) || TREE_THIS_VOLATILE (op2
));
4906 /* ??? This is a kludge to deal with the fact that
4907 we don't sort out integers and enums properly, yet. */
4908 result
= fold (build (COND_EXPR
, type1
, ifexp
, op1
, op2
));
4909 if (TREE_TYPE (result
) != type1
)
4910 result
= build1 (NOP_EXPR
, type1
, result
);
4911 /* Expand both sides into the same slot,
4912 hopefully the target of the ?: expression. */
4913 if (TREE_CODE (op1
) == TARGET_EXPR
&& TREE_CODE (op2
) == TARGET_EXPR
)
4915 tree slot
= build (VAR_DECL
, TREE_TYPE (result
));
4916 layout_decl (slot
, 0);
4917 result
= build (TARGET_EXPR
, TREE_TYPE (result
),
4918 slot
, result
, NULL_TREE
, NULL_TREE
);
4923 /* They don't match; promote them both and then try to reconcile them.
4924 But don't permit mismatching enum types. */
4925 if (code1
== ENUMERAL_TYPE
)
4927 if (code2
== ENUMERAL_TYPE
)
4929 cp_error ("enumeral mismatch in conditional expression: `%T' vs `%T'", type1
, type2
);
4930 return error_mark_node
;
4932 else if (extra_warnings
&& ! IS_AGGR_TYPE_CODE (code2
)
4933 && type2
!= type_promotes_to (type1
))
4934 warning ("enumeral and non-enumeral type in conditional expression");
4936 else if (extra_warnings
4937 && code2
== ENUMERAL_TYPE
&& ! IS_AGGR_TYPE_CODE (code1
)
4938 && type1
!= type_promotes_to (type2
))
4939 warning ("enumeral and non-enumeral type in conditional expression");
4941 if (code1
!= VOID_TYPE
)
4943 op1
= default_conversion (op1
);
4944 type1
= TREE_TYPE (op1
);
4945 if (TYPE_PTRMEMFUNC_P (type1
))
4946 type1
= TYPE_PTRMEMFUNC_FN_TYPE (type1
);
4947 code1
= TREE_CODE (type1
);
4949 if (code2
!= VOID_TYPE
)
4951 op2
= default_conversion (op2
);
4952 type2
= TREE_TYPE (op2
);
4953 if (TYPE_PTRMEMFUNC_P (type2
))
4954 type2
= TYPE_PTRMEMFUNC_FN_TYPE (type2
);
4955 code2
= TREE_CODE (type2
);
4958 if (code1
== RECORD_TYPE
&& code2
== RECORD_TYPE
4959 && real_lvalue_p (op1
) && real_lvalue_p (op2
)
4960 && comptypes (type1
, type2
, -1))
4962 type1
= build_reference_type (type1
);
4963 type2
= build_reference_type (type2
);
4964 result_type
= common_type (type1
, type2
);
4965 op1
= convert_to_reference (result_type
, op1
, CONV_IMPLICIT
,
4966 LOOKUP_NORMAL
, NULL_TREE
);
4967 op2
= convert_to_reference (result_type
, op2
, CONV_IMPLICIT
,
4968 LOOKUP_NORMAL
, NULL_TREE
);
4970 /* Quickly detect the usual case where op1 and op2 have the same type
4972 else if (TYPE_MAIN_VARIANT (type1
) == TYPE_MAIN_VARIANT (type2
))
4975 result_type
= type1
;
4977 result_type
= cp_build_type_variant
4979 TREE_READONLY (op1
) || TREE_READONLY (op2
),
4980 TREE_THIS_VOLATILE (op1
) || TREE_THIS_VOLATILE (op2
));
4982 else if ((code1
== INTEGER_TYPE
|| code1
== REAL_TYPE
)
4983 && (code2
== INTEGER_TYPE
|| code2
== REAL_TYPE
))
4985 result_type
= common_type (type1
, type2
);
4987 else if (code1
== VOID_TYPE
|| code2
== VOID_TYPE
)
4989 if (pedantic
&& (code1
!= VOID_TYPE
|| code2
!= VOID_TYPE
))
4990 pedwarn ("ANSI C++ forbids conditional expr with only one void side");
4991 result_type
= void_type_node
;
4993 else if (code1
== POINTER_TYPE
&& null_ptr_cst_p (op2
))
4994 result_type
= qualify_type (type1
, type2
);
4995 else if (code2
== POINTER_TYPE
&& null_ptr_cst_p (op1
))
4996 result_type
= qualify_type (type2
, type1
);
4997 else if (code1
== POINTER_TYPE
&& code2
== POINTER_TYPE
)
4999 if (comp_target_types (type1
, type2
, 1))
5000 result_type
= common_type (type1
, type2
);
5001 else if (TYPE_MAIN_VARIANT (TREE_TYPE (type1
)) == void_type_node
)
5003 if (pedantic
&& TREE_CODE (type2
) == FUNCTION_TYPE
)
5004 pedwarn ("ANSI C++ forbids conditional expr between `void *' and function pointer");
5005 result_type
= qualify_type (type1
, type2
);
5007 else if (TYPE_MAIN_VARIANT (TREE_TYPE (type2
)) == void_type_node
)
5009 if (pedantic
&& TREE_CODE (type1
) == FUNCTION_TYPE
)
5010 pedwarn ("ANSI C++ forbids conditional expr between `void *' and function pointer");
5011 result_type
= qualify_type (type2
, type1
);
5014 else if (comptypes (type2
, type1
, 0))
5015 result_type
= type2
;
5016 else if (IS_AGGR_TYPE (TREE_TYPE (type1
))
5017 && IS_AGGR_TYPE (TREE_TYPE (type2
))
5018 && (result_type
= common_base_type (TREE_TYPE (type1
), TREE_TYPE (type2
))))
5020 if (result_type
== error_mark_node
)
5022 cp_error ("common base type of types `%T' and `%T' is ambiguous",
5023 TREE_TYPE (type1
), TREE_TYPE (type2
));
5024 result_type
= ptr_type_node
;
5029 && result_type
!= TREE_TYPE (type1
)
5030 && result_type
!= TREE_TYPE (type2
))
5031 cp_pedwarn ("`%T' and `%T' converted to `%T *' in conditional expression",
5032 type1
, type2
, result_type
);
5034 result_type
= build_pointer_type (result_type
);
5039 pedwarn ("pointer type mismatch in conditional expression");
5040 result_type
= ptr_type_node
;
5043 else if (code1
== POINTER_TYPE
&& code2
== INTEGER_TYPE
)
5045 pedwarn ("pointer/integer type mismatch in conditional expression");
5046 result_type
= type1
;
5048 else if (code2
== POINTER_TYPE
&& code1
== INTEGER_TYPE
)
5050 pedwarn ("pointer/integer type mismatch in conditional expression");
5051 result_type
= type2
;
5056 /* The match does not look good. If either is
5057 an aggregate value, try converting to a scalar type. */
5058 if (code1
== RECORD_TYPE
&& code2
== RECORD_TYPE
)
5060 cp_error ("aggregate mismatch in conditional expression: `%T' vs `%T'", type1
, type2
);
5061 return error_mark_node
;
5063 /* Warning: this code assumes that conversion between cv-variants of
5064 a type is done using NOP_EXPRs. */
5065 if (code1
== RECORD_TYPE
&& TYPE_HAS_CONVERSION (type1
))
5067 /* There are other types besides pointers and records. */
5069 if (code2
== POINTER_TYPE
)
5070 tmp
= build_pointer_type
5071 (build_type_variant (TREE_TYPE (type2
), 1, 1));
5074 tmp
= build_type_conversion (CONVERT_EXPR
, tmp
, op1
, 0);
5075 if (tmp
== NULL_TREE
)
5077 cp_error ("incompatible types `%T' and `%T' in `?:'",
5079 return error_mark_node
;
5081 if (tmp
== error_mark_node
)
5082 error ("ambiguous pointer conversion");
5085 result_type
= common_type (type2
, TREE_TYPE (tmp
));
5088 else if (code2
== RECORD_TYPE
&& TYPE_HAS_CONVERSION (type2
))
5091 if (code1
== POINTER_TYPE
)
5092 tmp
= build_pointer_type
5093 (build_type_variant (TREE_TYPE (type1
), 1, 1));
5097 tmp
= build_type_conversion (CONVERT_EXPR
, tmp
, op2
, 0);
5098 if (tmp
== NULL_TREE
)
5100 cp_error ("incompatible types `%T' and `%T' in `?:'",
5102 return error_mark_node
;
5104 if (tmp
== error_mark_node
)
5105 error ("ambiguous pointer conversion");
5108 result_type
= common_type (type1
, TREE_TYPE (tmp
));
5111 else if (flag_cond_mismatch
)
5112 result_type
= void_type_node
;
5115 error ("type mismatch in conditional expression");
5116 return error_mark_node
;
5120 if (TREE_CODE (result_type
) == POINTER_TYPE
5121 && TREE_CODE (TREE_TYPE (result_type
)) == METHOD_TYPE
)
5122 result_type
= build_ptrmemfunc_type (result_type
);
5124 if (result_type
!= TREE_TYPE (op1
))
5125 op1
= convert_for_initialization
5126 (NULL_TREE
, result_type
, op1
, LOOKUP_NORMAL
, "converting", NULL_TREE
, 0);
5127 if (result_type
!= TREE_TYPE (op2
))
5128 op2
= convert_for_initialization
5129 (NULL_TREE
, result_type
, op2
, LOOKUP_NORMAL
, "converting", NULL_TREE
, 0);
5131 if (TREE_CONSTANT (ifexp
))
5132 return integer_zerop (ifexp
) ? op2
: op1
;
5134 return convert_from_reference
5135 (fold (build (COND_EXPR
, result_type
, ifexp
, op1
, op2
)));
5138 /* Handle overloading of the ',' operator when needed. Otherwise,
5139 this function just builds an expression list. */
5142 build_x_compound_expr (list
)
5145 tree rest
= TREE_CHAIN (list
);
5148 if (processing_template_decl
)
5149 return build_min_nt (COMPOUND_EXPR
, list
, NULL_TREE
);
5151 if (rest
== NULL_TREE
)
5152 return build_compound_expr (list
);
5154 result
= build_opfncall (COMPOUND_EXPR
, LOOKUP_NORMAL
,
5155 TREE_VALUE (list
), TREE_VALUE (rest
), NULL_TREE
);
5157 return build_x_compound_expr (tree_cons (NULL_TREE
, result
, TREE_CHAIN (rest
)));
5159 if (! TREE_SIDE_EFFECTS (TREE_VALUE (list
)))
5161 /* the left-hand operand of a comma expression is like an expression
5162 statement: we should warn if it doesn't have any side-effects,
5163 unless it was explicitly cast to (void). */
5164 if ((extra_warnings
|| warn_unused
)
5165 && !(TREE_CODE (TREE_VALUE(list
)) == CONVERT_EXPR
5166 && TREE_TYPE (TREE_VALUE(list
)) == void_type_node
))
5167 warning("left-hand operand of comma expression has no effect");
5169 #if 0 /* this requires a gcc backend patch to export warn_if_unused_value */
5170 else if (warn_unused
)
5171 warn_if_unused_value (TREE_VALUE(list
));
5174 return build_compound_expr (tree_cons (NULL_TREE
, TREE_VALUE (list
),
5175 build_tree_list (NULL_TREE
, build_x_compound_expr (rest
))));
5178 /* Given a list of expressions, return a compound expression
5179 that performs them all and returns the value of the last of them. */
5182 build_compound_expr (list
)
5187 if (TREE_READONLY_DECL_P (TREE_VALUE (list
)))
5188 TREE_VALUE (list
) = decl_constant_value (TREE_VALUE (list
));
5190 if (TREE_CHAIN (list
) == 0)
5192 /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
5193 Strip such NOP_EXPRs, since LIST is used in non-lvalue context. */
5194 if (TREE_CODE (list
) == NOP_EXPR
5195 && TREE_TYPE (list
) == TREE_TYPE (TREE_OPERAND (list
, 0)))
5196 list
= TREE_OPERAND (list
, 0);
5198 /* Convert arrays to pointers. */
5199 if (TREE_CODE (TREE_TYPE (TREE_VALUE (list
))) == ARRAY_TYPE
)
5200 return default_conversion (TREE_VALUE (list
));
5202 return TREE_VALUE (list
);
5205 rest
= build_compound_expr (TREE_CHAIN (list
));
5207 /* When pedantic, a compound expression cannot be a constant expression. */
5208 if (! TREE_SIDE_EFFECTS (TREE_VALUE (list
)) && ! pedantic
)
5211 return build (COMPOUND_EXPR
, TREE_TYPE (rest
),
5212 break_out_cleanups (TREE_VALUE (list
)), rest
);
5216 build_static_cast (type
, expr
)
5222 if (type
== error_mark_node
|| expr
== error_mark_node
)
5223 return error_mark_node
;
5225 if (TREE_CODE (expr
) == OFFSET_REF
)
5226 expr
= resolve_offset_ref (expr
);
5228 if (processing_template_decl
)
5230 tree t
= build_min (STATIC_CAST_EXPR
, type
, expr
);
5234 /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
5235 Strip such NOP_EXPRs if VALUE is being used in non-lvalue context. */
5236 if (TREE_CODE (type
) != REFERENCE_TYPE
5237 && TREE_CODE (expr
) == NOP_EXPR
5238 && TREE_TYPE (expr
) == TREE_TYPE (TREE_OPERAND (expr
, 0)))
5239 expr
= TREE_OPERAND (expr
, 0);
5241 if (TREE_CODE (type
) == VOID_TYPE
)
5242 return build1 (CONVERT_EXPR
, type
, expr
);
5244 if (type_unknown_p (expr
))
5246 expr
= instantiate_type (type
, expr
, 1);
5247 if (expr
== error_mark_node
)
5248 return error_mark_node
;
5251 if (TREE_CODE (type
) == REFERENCE_TYPE
)
5252 return (convert_from_reference
5253 (convert_to_reference (type
, expr
, CONV_STATIC
|CONV_IMPLICIT
,
5254 LOOKUP_COMPLAIN
, NULL_TREE
)));
5256 if (IS_AGGR_TYPE (type
))
5257 return build_cplus_new
5258 (type
, (build_method_call
5259 (NULL_TREE
, ctor_identifier
, build_tree_list (NULL_TREE
, expr
),
5260 TYPE_BINFO (type
), LOOKUP_NORMAL
)));
5262 expr
= decay_conversion (expr
);
5263 intype
= TREE_TYPE (expr
);
5265 /* FIXME handle casting to array type. */
5268 if (can_convert_arg (type
, intype
, expr
))
5270 else if (TYPE_PTROB_P (type
) && TYPE_PTROB_P (intype
))
5273 if (IS_AGGR_TYPE (TREE_TYPE (type
)) && IS_AGGR_TYPE (TREE_TYPE (intype
))
5274 && (TYPE_READONLY (TREE_TYPE (type
))
5275 >= TYPE_READONLY (TREE_TYPE (intype
)))
5276 && (TYPE_VOLATILE (TREE_TYPE (type
))
5277 >= TYPE_VOLATILE (TREE_TYPE (intype
)))
5278 && (binfo
= get_binfo (TREE_TYPE (intype
), TREE_TYPE (type
), 0))
5279 && ! TREE_VIA_VIRTUAL (binfo
))
5282 else if (TYPE_PTRMEM_P (type
) && TYPE_PTRMEM_P (intype
))
5284 if (comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (type
))),
5285 TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (intype
))), 1)
5286 && (TYPE_READONLY (TREE_TYPE (TREE_TYPE (type
)))
5287 >= TYPE_READONLY (TREE_TYPE (TREE_TYPE (intype
))))
5288 && (TYPE_VOLATILE (TREE_TYPE (TREE_TYPE (type
)))
5289 >= TYPE_VOLATILE (TREE_TYPE (TREE_TYPE (intype
))))
5290 && (binfo
= get_binfo (TYPE_OFFSET_BASETYPE (intype
),
5291 TYPE_OFFSET_BASETYPE (type
), 0))
5292 && ! TREE_VIA_VIRTUAL (binfo
))
5295 else if (TREE_CODE (intype
) != BOOLEAN_TYPE
5296 && TREE_CODE (type
) != ARRAY_TYPE
5297 && TREE_CODE (type
) != FUNCTION_TYPE
5298 && can_convert (intype
, type
))
5302 return build_c_cast (type
, expr
);
5304 cp_error ("static_cast from `%T' to `%T'", intype
, type
);
5305 return error_mark_node
;
5309 build_reinterpret_cast (type
, expr
)
5314 if (type
== error_mark_node
|| expr
== error_mark_node
)
5315 return error_mark_node
;
5317 if (TREE_CODE (expr
) == OFFSET_REF
)
5318 expr
= resolve_offset_ref (expr
);
5320 if (processing_template_decl
)
5322 tree t
= build_min (REINTERPRET_CAST_EXPR
, type
, expr
);
5326 if (TREE_CODE (type
) != REFERENCE_TYPE
)
5328 expr
= decay_conversion (expr
);
5330 /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
5331 Strip such NOP_EXPRs if VALUE is being used in non-lvalue context. */
5332 if (TREE_CODE (expr
) == NOP_EXPR
5333 && TREE_TYPE (expr
) == TREE_TYPE (TREE_OPERAND (expr
, 0)))
5334 expr
= TREE_OPERAND (expr
, 0);
5337 if (type_unknown_p (expr
))
5339 expr
= instantiate_type (type
, expr
, 1);
5340 if (expr
== error_mark_node
)
5341 return error_mark_node
;
5344 intype
= TREE_TYPE (expr
);
5346 if (TREE_CODE (type
) == REFERENCE_TYPE
)
5348 if (! real_lvalue_p (expr
))
5350 cp_error ("reinterpret_cast from `%T' rvalue to `%T'", intype
, type
);
5351 return error_mark_node
;
5353 expr
= build_unary_op (ADDR_EXPR
, expr
, 0);
5354 if (expr
!= error_mark_node
)
5355 expr
= build_reinterpret_cast
5356 (build_pointer_type (TREE_TYPE (type
)), expr
);
5357 if (expr
!= error_mark_node
)
5358 expr
= build_indirect_ref (expr
, 0);
5361 else if (comptypes (TYPE_MAIN_VARIANT (intype
), TYPE_MAIN_VARIANT (type
), 1))
5362 return build_static_cast (type
, expr
);
5364 if (TYPE_PTR_P (type
) && (TREE_CODE (intype
) == INTEGER_TYPE
5365 || TREE_CODE (intype
) == ENUMERAL_TYPE
))
5367 else if (TREE_CODE (type
) == INTEGER_TYPE
&& TYPE_PTR_P (intype
))
5369 if (TYPE_PRECISION (type
) < TYPE_PRECISION (intype
))
5370 cp_pedwarn ("reinterpret_cast from `%T' to `%T' loses precision",
5373 else if ((TYPE_PTRFN_P (type
) && TYPE_PTRFN_P (intype
))
5374 || (TYPE_PTRMEMFUNC_P (type
) && TYPE_PTRMEMFUNC_P (intype
)))
5376 if (TREE_READONLY_DECL_P (expr
))
5377 expr
= decl_constant_value (expr
);
5378 return fold (build1 (NOP_EXPR
, type
, expr
));
5380 else if ((TYPE_PTRMEM_P (type
) && TYPE_PTRMEM_P (intype
))
5381 || (TYPE_PTROBV_P (type
) && TYPE_PTROBV_P (intype
)))
5383 if (! comp_ptr_ttypes_reinterpret (TREE_TYPE (type
), TREE_TYPE (intype
)))
5384 cp_pedwarn ("reinterpret_cast from `%T' to `%T' casts away const (or volatile)",
5387 if (TREE_READONLY_DECL_P (expr
))
5388 expr
= decl_constant_value (expr
);
5389 return fold (build1 (NOP_EXPR
, type
, expr
));
5393 cp_error ("reinterpret_cast from `%T' to `%T'", intype
, type
);
5394 return error_mark_node
;
5397 return cp_convert (type
, expr
);
5401 build_const_cast (type
, expr
)
5406 if (type
== error_mark_node
|| expr
== error_mark_node
)
5407 return error_mark_node
;
5409 if (TREE_CODE (expr
) == OFFSET_REF
)
5410 expr
= resolve_offset_ref (expr
);
5412 if (processing_template_decl
)
5414 tree t
= build_min (CONST_CAST_EXPR
, type
, expr
);
5418 if (TREE_CODE (type
) != REFERENCE_TYPE
)
5420 expr
= decay_conversion (expr
);
5422 /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
5423 Strip such NOP_EXPRs if VALUE is being used in non-lvalue context. */
5424 if (TREE_CODE (expr
) == NOP_EXPR
5425 && TREE_TYPE (expr
) == TREE_TYPE (TREE_OPERAND (expr
, 0)))
5426 expr
= TREE_OPERAND (expr
, 0);
5429 if (type_unknown_p (expr
))
5431 expr
= instantiate_type (type
, expr
, 1);
5432 if (expr
== error_mark_node
)
5433 return error_mark_node
;
5436 intype
= TREE_TYPE (expr
);
5438 if (comptypes (TYPE_MAIN_VARIANT (intype
), TYPE_MAIN_VARIANT (type
), 1))
5439 return build_static_cast (type
, expr
);
5440 else if (TREE_CODE (type
) == REFERENCE_TYPE
)
5442 if (! real_lvalue_p (expr
))
5444 cp_error ("const_cast from `%T' rvalue to `%T'", intype
, type
);
5445 return error_mark_node
;
5448 if (comp_ptr_ttypes_const (TREE_TYPE (type
), intype
))
5449 return (convert_from_reference
5450 (convert_to_reference (type
, expr
, CONV_CONST
|CONV_IMPLICIT
,
5451 LOOKUP_COMPLAIN
, NULL_TREE
)));
5453 else if (TREE_CODE (type
) == POINTER_TYPE
5454 && TREE_CODE (intype
) == POINTER_TYPE
5455 && comp_ptr_ttypes_const (TREE_TYPE (type
), TREE_TYPE (intype
)))
5456 return cp_convert (type
, expr
);
5458 cp_error ("const_cast from `%T' to `%T'", intype
, type
);
5459 return error_mark_node
;
5462 /* Build an expression representing a cast to type TYPE of expression EXPR.
5464 ALLOW_NONCONVERTING is true if we should allow non-converting constructors
5465 when doing the cast. */
5468 build_c_cast (type
, expr
)
5471 register tree value
= expr
;
5473 if (type
== error_mark_node
|| expr
== error_mark_node
)
5474 return error_mark_node
;
5476 /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
5477 Strip such NOP_EXPRs if VALUE is being used in non-lvalue context. */
5478 if (TREE_CODE (type
) != REFERENCE_TYPE
5479 && TREE_CODE (value
) == NOP_EXPR
5480 && TREE_TYPE (value
) == TREE_TYPE (TREE_OPERAND (value
, 0)))
5481 value
= TREE_OPERAND (value
, 0);
5483 if (TREE_TYPE (expr
)
5484 && TREE_CODE (TREE_TYPE (expr
)) == OFFSET_TYPE
5485 && TREE_CODE (type
) != OFFSET_TYPE
)
5486 value
= resolve_offset_ref (value
);
5488 if (TREE_CODE (type
) == ARRAY_TYPE
)
5490 /* Allow casting from T1* to T2[] because Cfront allows it.
5491 NIHCL uses it. It is not valid ANSI C however, and hence, not
5493 if (TREE_CODE (TREE_TYPE (expr
)) == POINTER_TYPE
)
5496 pedwarn ("ANSI C++ forbids casting to an array type");
5497 type
= build_pointer_type (TREE_TYPE (type
));
5501 error ("ANSI C++ forbids casting to an array type");
5502 return error_mark_node
;
5506 if (TREE_CODE (type
) == FUNCTION_TYPE
5507 || TREE_CODE (type
) == METHOD_TYPE
)
5509 cp_error ("casting to function type `%T'", type
);
5510 return error_mark_node
;
5513 if (IS_SIGNATURE (type
))
5515 error ("cast specifies signature type");
5516 return error_mark_node
;
5519 if (processing_template_decl
)
5521 tree t
= build_min (CAST_EXPR
, type
,
5522 min_tree_cons (NULL_TREE
, value
, NULL_TREE
));
5526 if (TREE_CODE (type
) == VOID_TYPE
)
5527 value
= build1 (CONVERT_EXPR
, type
, value
);
5528 else if (TREE_TYPE (value
) == NULL_TREE
5529 || type_unknown_p (value
))
5531 value
= instantiate_type (type
, value
, 1);
5533 if (value
== error_mark_node
)
5534 return error_mark_node
;
5540 /* Convert functions and arrays to pointers and
5541 convert references to their expanded types,
5542 but don't convert any other types. */
5543 if (TREE_CODE (TREE_TYPE (value
)) == FUNCTION_TYPE
5544 || (TREE_CODE (TREE_TYPE (value
)) == METHOD_TYPE
5545 /* Don't do the default conversion if we want a
5546 pointer to a function. */
5547 && ! (TREE_CODE (type
) == POINTER_TYPE
5548 && TREE_CODE (TREE_TYPE (type
)) == FUNCTION_TYPE
))
5549 || TREE_CODE (TREE_TYPE (value
)) == ARRAY_TYPE
5550 || TREE_CODE (TREE_TYPE (value
)) == REFERENCE_TYPE
)
5551 value
= default_conversion (value
);
5552 otype
= TREE_TYPE (value
);
5554 /* Optionally warn about potentially worrisome casts. */
5557 && TREE_CODE (type
) == POINTER_TYPE
5558 && TREE_CODE (otype
) == POINTER_TYPE
)
5560 /* For C++ we make these regular warnings, rather than
5561 softening them into pedwarns. */
5562 if (TYPE_VOLATILE (TREE_TYPE (otype
))
5563 && ! TYPE_VOLATILE (TREE_TYPE (type
)))
5564 warning ("cast discards `volatile' from pointer target type");
5565 if (TYPE_READONLY (TREE_TYPE (otype
))
5566 && ! TYPE_READONLY (TREE_TYPE (type
)))
5567 warning ("cast discards `const' from pointer target type");
5570 /* Warn about possible alignment problems. */
5571 if (STRICT_ALIGNMENT
&& warn_cast_align
5572 && TREE_CODE (type
) == POINTER_TYPE
5573 && TREE_CODE (otype
) == POINTER_TYPE
5574 && TREE_CODE (TREE_TYPE (otype
)) != VOID_TYPE
5575 && TREE_CODE (TREE_TYPE (otype
)) != FUNCTION_TYPE
5576 && TYPE_ALIGN (TREE_TYPE (type
)) > TYPE_ALIGN (TREE_TYPE (otype
)))
5577 warning ("cast increases required alignment of target type");
5580 /* We should see about re-enabling these, they seem useful to
5582 if (TREE_CODE (type
) == INTEGER_TYPE
5583 && TREE_CODE (otype
) == POINTER_TYPE
5584 && TYPE_PRECISION (type
) != TYPE_PRECISION (otype
))
5585 warning ("cast from pointer to integer of different size");
5587 if (TREE_CODE (type
) == POINTER_TYPE
5588 && TREE_CODE (otype
) == INTEGER_TYPE
5589 && TYPE_PRECISION (type
) != TYPE_PRECISION (otype
)
5590 /* Don't warn about converting 0 to pointer,
5591 provided the 0 was explicit--not cast or made by folding. */
5592 && !(TREE_CODE (value
) == INTEGER_CST
&& integer_zerop (value
)))
5593 warning ("cast to pointer from integer of different size");
5596 if (TREE_CODE (type
) == REFERENCE_TYPE
)
5597 value
= (convert_from_reference
5598 (convert_to_reference (type
, value
, CONV_C_CAST
,
5599 LOOKUP_COMPLAIN
, NULL_TREE
)));
5604 if (TREE_READONLY_DECL_P (value
))
5605 value
= decl_constant_value (value
);
5608 value
= convert_force (type
, value
, CONV_C_CAST
);
5610 /* Ignore any integer overflow caused by the cast. */
5611 if (TREE_CODE (value
) == INTEGER_CST
)
5613 TREE_OVERFLOW (value
) = TREE_OVERFLOW (ovalue
);
5614 TREE_CONSTANT_OVERFLOW (value
) = TREE_CONSTANT_OVERFLOW (ovalue
);
5619 /* Always produce some operator for an explicit cast,
5620 so we can tell (for -pedantic) that the cast is no lvalue. */
5621 if (TREE_CODE (type
) != REFERENCE_TYPE
&& value
== expr
5622 && real_lvalue_p (value
))
5623 value
= non_lvalue (value
);
5629 expand_target_expr (t
)
5632 extern int temp_slot_level
;
5633 extern int target_temp_slot_level
;
5634 int old_temp_level
= target_temp_slot_level
;
5636 tree xval
= make_node (RTL_EXPR
);
5639 /* Any TARGET_EXPR temps live only as long as the outer temp level.
5640 Since they are preserved in this new inner level, we know they
5641 will make it into the outer level. */
5643 target_temp_slot_level
= temp_slot_level
;
5645 do_pending_stack_adjust ();
5646 start_sequence_for_rtl_expr (xval
);
5648 rtxval
= expand_expr (t
, NULL_RTX
, VOIDmode
, 0);
5649 do_pending_stack_adjust ();
5650 TREE_SIDE_EFFECTS (xval
) = 1;
5651 RTL_EXPR_SEQUENCE (xval
) = get_insns ();
5653 RTL_EXPR_RTL (xval
) = rtxval
;
5654 TREE_TYPE (xval
) = TREE_TYPE (t
);
5657 target_temp_slot_level
= old_temp_level
;
5662 /* Build an assignment expression of lvalue LHS from value RHS.
5663 MODIFYCODE is the code for a binary operator that we use
5664 to combine the old value of LHS with RHS to get the new value.
5665 Or else MODIFYCODE is NOP_EXPR meaning do a simple assignment.
5667 C++: If MODIFYCODE is INIT_EXPR, then leave references unbashed. */
5670 build_modify_expr (lhs
, modifycode
, rhs
)
5672 enum tree_code modifycode
;
5675 register tree result
;
5677 tree lhstype
= TREE_TYPE (lhs
);
5678 tree olhstype
= lhstype
;
5681 /* Avoid duplicate error messages from operands that had errors. */
5682 if (lhs
== error_mark_node
|| rhs
== error_mark_node
)
5683 return error_mark_node
;
5685 /* Types that aren't fully specified cannot be used in assignments. */
5686 lhs
= require_complete_type (lhs
);
5690 /* Handle assignment to signature pointers/refs. */
5692 if (TYPE_LANG_SPECIFIC (lhstype
)
5693 && (IS_SIGNATURE_POINTER (lhstype
) || IS_SIGNATURE_REFERENCE (lhstype
)))
5695 return build_signature_pointer_constructor (lhs
, rhs
);
5698 /* Handle control structure constructs used as "lvalues". */
5700 switch (TREE_CODE (lhs
))
5702 /* Handle --foo = 5; as these are valid constructs in C++ */
5703 case PREDECREMENT_EXPR
:
5704 case PREINCREMENT_EXPR
:
5705 if (TREE_SIDE_EFFECTS (TREE_OPERAND (lhs
, 0)))
5706 lhs
= build (TREE_CODE (lhs
), TREE_TYPE (lhs
),
5707 stabilize_reference (TREE_OPERAND (lhs
, 0)),
5708 TREE_OPERAND (lhs
, 1));
5709 return build (COMPOUND_EXPR
, lhstype
,
5711 build_modify_expr (TREE_OPERAND (lhs
, 0),
5714 /* Handle (a, b) used as an "lvalue". */
5716 newrhs
= build_modify_expr (TREE_OPERAND (lhs
, 1),
5718 if (newrhs
== error_mark_node
)
5719 return error_mark_node
;
5720 return build (COMPOUND_EXPR
, lhstype
,
5721 TREE_OPERAND (lhs
, 0), newrhs
);
5724 newrhs
= build_modify_expr (TREE_OPERAND (lhs
, 0), modifycode
, rhs
);
5725 if (newrhs
== error_mark_node
)
5726 return error_mark_node
;
5727 return build (COMPOUND_EXPR
, lhstype
, lhs
, newrhs
);
5729 /* Handle (a ? b : c) used as an "lvalue". */
5731 rhs
= save_expr (rhs
);
5733 /* Produce (a ? (b = rhs) : (c = rhs))
5734 except that the RHS goes through a save-expr
5735 so the code to compute it is only emitted once. */
5737 = build_conditional_expr (TREE_OPERAND (lhs
, 0),
5738 build_modify_expr (cp_convert (TREE_TYPE (lhs
), TREE_OPERAND (lhs
, 1)),
5740 build_modify_expr (cp_convert (TREE_TYPE (lhs
), TREE_OPERAND (lhs
, 2)),
5742 if (cond
== error_mark_node
)
5744 /* Make sure the code to compute the rhs comes out
5745 before the split. */
5746 return build (COMPOUND_EXPR
, TREE_TYPE (lhs
),
5747 /* Case to void to suppress warning
5748 from warn_if_unused_value. */
5749 cp_convert (void_type_node
, rhs
), cond
);
5753 if (TREE_CODE (lhs
) == OFFSET_REF
)
5755 if (TREE_OPERAND (lhs
, 0) == NULL_TREE
)
5757 /* Static class member? */
5758 tree member
= TREE_OPERAND (lhs
, 1);
5759 if (TREE_CODE (member
) == VAR_DECL
)
5763 compiler_error ("invalid static class member");
5764 return error_mark_node
;
5768 lhs
= resolve_offset_ref (lhs
);
5770 olhstype
= lhstype
= TREE_TYPE (lhs
);
5773 if (TREE_CODE (lhstype
) == REFERENCE_TYPE
5774 && modifycode
!= INIT_EXPR
)
5776 lhs
= convert_from_reference (lhs
);
5777 olhstype
= lhstype
= TREE_TYPE (lhs
);
5780 /* If a binary op has been requested, combine the old LHS value with the RHS
5781 producing the value we should actually store into the LHS. */
5783 if (modifycode
== INIT_EXPR
)
5785 if (! IS_AGGR_TYPE (lhstype
))
5786 /* Do the default thing */;
5787 else if (! TYPE_HAS_CONSTRUCTOR (lhstype
))
5789 cp_error ("`%T' has no constructors", lhstype
);
5790 return error_mark_node
;
5792 else if (TYPE_HAS_TRIVIAL_INIT_REF (lhstype
)
5793 && TYPE_MAIN_VARIANT (lhstype
) == TYPE_MAIN_VARIANT (TREE_TYPE (newrhs
)))
5794 /* Do the default thing */;
5797 result
= build_method_call (lhs
, ctor_identifier
,
5798 build_tree_list (NULL_TREE
, rhs
),
5799 TYPE_BINFO (lhstype
), LOOKUP_NORMAL
);
5800 if (result
== NULL_TREE
)
5801 return error_mark_node
;
5805 else if (modifycode
== NOP_EXPR
)
5807 /* `operator=' is not an inheritable operator. */
5808 if (! IS_AGGR_TYPE (lhstype
))
5809 /* Do the default thing */;
5810 else if (! TYPE_HAS_ASSIGNMENT (lhstype
))
5812 cp_error ("`%T' does not define operator=", lhstype
);
5813 return error_mark_node
;
5815 else if (TYPE_HAS_TRIVIAL_ASSIGN_REF (lhstype
)
5816 && TYPE_MAIN_VARIANT (lhstype
) == TYPE_MAIN_VARIANT (TREE_TYPE (newrhs
)))
5818 build_opfncall (MODIFY_EXPR
, LOOKUP_NORMAL
,
5819 lhs
, rhs
, make_node (NOP_EXPR
));
5821 /* Do the default thing */;
5825 result
= build_opfncall (MODIFY_EXPR
, LOOKUP_NORMAL
,
5826 lhs
, rhs
, make_node (NOP_EXPR
));
5827 if (result
== NULL_TREE
)
5828 return error_mark_node
;
5833 else if (PROMOTES_TO_AGGR_TYPE (lhstype
, REFERENCE_TYPE
))
5835 /* This case must convert to some sort of lvalue that
5836 can participate in an op= operation. */
5839 if (build_default_binary_type_conversion (modifycode
, &lhs_tmp
, &rhs_tmp
))
5841 lhs
= stabilize_reference (lhs_tmp
);
5842 /* Forget it was ever anything else. */
5843 olhstype
= lhstype
= TREE_TYPE (lhs
);
5844 newrhs
= build_binary_op (modifycode
, lhs
, rhs_tmp
, 1);
5848 cp_error ("no match for `%Q(%#T, %#T)'", modifycode
,
5849 TREE_TYPE (lhs
), TREE_TYPE (rhs
));
5850 return error_mark_node
;
5855 lhs
= stabilize_reference (lhs
);
5856 newrhs
= build_binary_op (modifycode
, lhs
, rhs
, 1);
5857 if (newrhs
== error_mark_node
)
5859 cp_error (" in evaluation of `%Q(%#T, %#T)'", modifycode
,
5860 TREE_TYPE (lhs
), TREE_TYPE (rhs
));
5861 return error_mark_node
;
5865 /* Handle a cast used as an "lvalue".
5866 We have already performed any binary operator using the value as cast.
5867 Now convert the result to the cast type of the lhs,
5868 and then true type of the lhs and store it there;
5869 then convert result back to the cast type to be the value
5870 of the assignment. */
5872 switch (TREE_CODE (lhs
))
5877 case FIX_TRUNC_EXPR
:
5878 case FIX_FLOOR_EXPR
:
5879 case FIX_ROUND_EXPR
:
5881 if (TREE_CODE (TREE_TYPE (newrhs
)) == ARRAY_TYPE
5882 || TREE_CODE (TREE_TYPE (newrhs
)) == FUNCTION_TYPE
5883 || TREE_CODE (TREE_TYPE (newrhs
)) == METHOD_TYPE
5884 || TREE_CODE (TREE_TYPE (newrhs
)) == OFFSET_TYPE
)
5885 newrhs
= default_conversion (newrhs
);
5887 tree inner_lhs
= TREE_OPERAND (lhs
, 0);
5890 /* WP 5.4.1: The result is an lvalue if T is a reference type,
5891 otherwise the result is an rvalue. */
5892 if (! lvalue_p (lhs
))
5893 pedwarn ("ANSI C++ forbids cast to non-reference type used as lvalue");
5895 result
= build_modify_expr (inner_lhs
, NOP_EXPR
,
5896 cp_convert (TREE_TYPE (inner_lhs
),
5897 cp_convert (lhstype
, newrhs
)));
5898 if (result
== error_mark_node
)
5900 return cp_convert (TREE_TYPE (lhs
), result
);
5904 /* Now we have handled acceptable kinds of LHS that are not truly lvalues.
5905 Reject anything strange now. */
5907 if (!lvalue_or_else (lhs
, "assignment"))
5908 return error_mark_node
;
5910 GNU_xref_assign (lhs
);
5912 /* Warn about storing in something that is `const'. */
5913 /* For C++, don't warn if this is initialization. */
5914 if (modifycode
!= INIT_EXPR
5915 /* For assignment to `const' signature pointer/reference fields,
5916 don't warn either, we already printed a better message before. */
5917 && ! (TREE_CODE (lhs
) == COMPONENT_REF
5918 && (IS_SIGNATURE_POINTER (TREE_TYPE (TREE_OPERAND (lhs
, 0)))
5919 || IS_SIGNATURE_REFERENCE (TREE_TYPE (TREE_OPERAND (lhs
, 0)))))
5920 && (TREE_READONLY (lhs
) || TYPE_READONLY (lhstype
)
5921 || ((TREE_CODE (lhstype
) == RECORD_TYPE
5922 || TREE_CODE (lhstype
) == UNION_TYPE
)
5923 && C_TYPE_FIELDS_READONLY (lhstype
))
5924 || (TREE_CODE (lhstype
) == REFERENCE_TYPE
5925 && TYPE_READONLY (TREE_TYPE (lhstype
)))))
5926 readonly_error (lhs
, "assignment", 0);
5928 /* If storing into a structure or union member,
5929 it has probably been given type `int'.
5930 Compute the type that would go with
5931 the actual amount of storage the member occupies. */
5933 if (TREE_CODE (lhs
) == COMPONENT_REF
5934 && (TREE_CODE (lhstype
) == INTEGER_TYPE
5935 || TREE_CODE (lhstype
) == REAL_TYPE
5936 || TREE_CODE (lhstype
) == ENUMERAL_TYPE
))
5938 lhstype
= TREE_TYPE (get_unwidened (lhs
, 0));
5940 /* If storing in a field that is in actuality a short or narrower
5941 than one, we must store in the field in its actual type. */
5943 if (lhstype
!= TREE_TYPE (lhs
))
5945 lhs
= copy_node (lhs
);
5946 TREE_TYPE (lhs
) = lhstype
;
5950 /* check to see if there is an assignment to `this' */
5951 if (lhs
== current_class_ptr
)
5953 if (flag_this_is_variable
> 0
5954 && DECL_NAME (current_function_decl
) != NULL_TREE
5955 && (DECL_NAME (current_function_decl
)
5956 != constructor_name (current_class_type
)))
5957 warning ("assignment to `this' not in constructor or destructor");
5958 current_function_just_assigned_this
= 1;
5961 /* The TREE_TYPE of RHS may be TYPE_UNKNOWN. This can happen
5962 when the type of RHS is not yet known, i.e. its type
5963 is inherited from LHS. */
5964 rhs
= require_instantiated_type (lhstype
, newrhs
, error_mark_node
);
5965 if (rhs
== error_mark_node
)
5966 return error_mark_node
;
5969 if (modifycode
!= INIT_EXPR
)
5971 /* Make modifycode now either a NOP_EXPR or an INIT_EXPR. */
5972 modifycode
= NOP_EXPR
;
5973 /* Reference-bashing */
5974 if (TREE_CODE (lhstype
) == REFERENCE_TYPE
)
5976 tree tmp
= convert_from_reference (lhs
);
5977 lhstype
= TREE_TYPE (tmp
);
5978 if (TYPE_SIZE (lhstype
) == 0)
5980 incomplete_type_error (lhs
, lhstype
);
5981 return error_mark_node
;
5986 if (TREE_CODE (TREE_TYPE (newrhs
)) == REFERENCE_TYPE
)
5988 tree tmp
= convert_from_reference (newrhs
);
5989 if (TYPE_SIZE (TREE_TYPE (tmp
)) == 0)
5991 incomplete_type_error (newrhs
, TREE_TYPE (tmp
));
5992 return error_mark_node
;
5998 if (TREE_SIDE_EFFECTS (lhs
))
5999 lhs
= stabilize_reference (lhs
);
6000 if (TREE_SIDE_EFFECTS (newrhs
))
6001 newrhs
= stabilize_reference (newrhs
);
6003 /* Convert new value to destination type. */
6005 if (TREE_CODE (lhstype
) == ARRAY_TYPE
)
6009 if (! comptypes (lhstype
, TREE_TYPE (rhs
), 0))
6011 cp_error ("incompatible types in assignment of `%T' to `%T'",
6012 TREE_TYPE (rhs
), lhstype
);
6013 return error_mark_node
;
6016 /* Allow array assignment in compiler-generated code. */
6017 if (pedantic
&& ! DECL_ARTIFICIAL (current_function_decl
))
6018 pedwarn ("ANSI C++ forbids assignment of arrays");
6020 /* Have to wrap this in RTL_EXPR for two cases:
6021 in base or member initialization and if we
6022 are a branch of a ?: operator. Since we
6023 can't easily know the latter, just do it always. */
6025 result
= make_node (RTL_EXPR
);
6027 TREE_TYPE (result
) = void_type_node
;
6028 do_pending_stack_adjust ();
6029 start_sequence_for_rtl_expr (result
);
6031 /* As a matter of principle, `start_sequence' should do this. */
6034 from_array
= TREE_CODE (TREE_TYPE (newrhs
)) == ARRAY_TYPE
6035 ? 1 + (modifycode
!= INIT_EXPR
): 0;
6036 expand_vec_init (lhs
, lhs
, array_type_nelts (lhstype
), newrhs
,
6039 do_pending_stack_adjust ();
6041 TREE_SIDE_EFFECTS (result
) = 1;
6042 RTL_EXPR_SEQUENCE (result
) = get_insns ();
6043 RTL_EXPR_RTL (result
) = const0_rtx
;
6048 if (modifycode
== INIT_EXPR
)
6050 newrhs
= convert_for_initialization (lhs
, lhstype
, newrhs
, LOOKUP_NORMAL
,
6051 "assignment", NULL_TREE
, 0);
6052 if (lhs
== DECL_RESULT (current_function_decl
))
6054 if (DECL_INITIAL (lhs
))
6055 warning ("return value from function receives multiple initializations");
6056 DECL_INITIAL (lhs
) = newrhs
;
6061 /* Avoid warnings on enum bit fields. */
6062 if (TREE_CODE (olhstype
) == ENUMERAL_TYPE
6063 && TREE_CODE (lhstype
) == INTEGER_TYPE
)
6065 newrhs
= convert_for_assignment (olhstype
, newrhs
, "assignment",
6067 newrhs
= convert_force (lhstype
, newrhs
, 0);
6070 newrhs
= convert_for_assignment (lhstype
, newrhs
, "assignment",
6072 if (TREE_CODE (newrhs
) == CALL_EXPR
6073 && TYPE_NEEDS_CONSTRUCTING (lhstype
))
6074 newrhs
= build_cplus_new (lhstype
, newrhs
);
6076 /* Can't initialize directly from a TARGET_EXPR, since that would
6077 cause the lhs to be constructed twice, and possibly result in
6078 accidental self-initialization. So we force the TARGET_EXPR to be
6079 expanded without a target. */
6080 if (TREE_CODE (newrhs
) == TARGET_EXPR
)
6081 newrhs
= build (COMPOUND_EXPR
, TREE_TYPE (newrhs
), newrhs
,
6082 TREE_OPERAND (newrhs
, 0));
6085 if (newrhs
== error_mark_node
)
6086 return error_mark_node
;
6088 if (TREE_CODE (newrhs
) == COND_EXPR
)
6091 tree cond
= TREE_OPERAND (newrhs
, 0);
6093 if (TREE_SIDE_EFFECTS (lhs
))
6094 cond
= build_compound_expr (tree_cons
6096 build_tree_list (NULL_TREE
, cond
)));
6098 /* Cannot have two identical lhs on this one tree (result) as preexpand
6099 calls will rip them out and fill in RTL for them, but when the
6100 rtl is generated, the calls will only be in the first side of the
6101 condition, not on both, or before the conditional jump! (mrs) */
6102 lhs1
= break_out_calls (lhs
);
6105 /* If there's no change, the COND_EXPR behaves like any other rhs. */
6106 result
= build (modifycode
== NOP_EXPR
? MODIFY_EXPR
: INIT_EXPR
,
6107 lhstype
, lhs
, newrhs
);
6110 tree result_type
= TREE_TYPE (newrhs
);
6111 /* We have to convert each arm to the proper type because the
6112 types may have been munged by constant folding. */
6114 = build (COND_EXPR
, result_type
, cond
,
6115 build_modify_expr (lhs
, modifycode
,
6116 cp_convert (result_type
,
6117 TREE_OPERAND (newrhs
, 1))),
6118 build_modify_expr (lhs1
, modifycode
,
6119 cp_convert (result_type
,
6120 TREE_OPERAND (newrhs
, 2))));
6124 result
= build (modifycode
== NOP_EXPR
? MODIFY_EXPR
: INIT_EXPR
,
6125 lhstype
, lhs
, newrhs
);
6127 TREE_SIDE_EFFECTS (result
) = 1;
6129 /* If we got the LHS in a different type for storing in,
6130 convert the result back to the nominal type of LHS
6131 so that the value we return always has the same type
6132 as the LHS argument. */
6134 if (olhstype
== TREE_TYPE (result
))
6136 /* Avoid warnings converting integral types back into enums
6137 for enum bit fields. */
6138 if (TREE_CODE (TREE_TYPE (result
)) == INTEGER_TYPE
6139 && TREE_CODE (olhstype
) == ENUMERAL_TYPE
)
6141 result
= build (COMPOUND_EXPR
, olhstype
, result
, olhs
);
6142 TREE_NO_UNUSED_WARNING (result
) = 1;
6145 return convert_for_assignment (olhstype
, result
, "assignment",
6150 build_x_modify_expr (lhs
, modifycode
, rhs
)
6152 enum tree_code modifycode
;
6155 if (processing_template_decl
)
6156 return build_min_nt (MODOP_EXPR
, lhs
,
6157 build_min_nt (modifycode
, NULL_TREE
, NULL_TREE
), rhs
);
6159 if (modifycode
!= NOP_EXPR
)
6161 tree rval
= build_opfncall (MODIFY_EXPR
, LOOKUP_NORMAL
, lhs
, rhs
,
6162 make_node (modifycode
));
6166 return build_modify_expr (lhs
, modifycode
, rhs
);
6169 /* Return 0 if EXP is not a valid lvalue in this language
6170 even though `lvalue_or_else' would accept it. */
6173 language_lvalue_valid (exp
)
6179 /* Get difference in deltas for different pointer to member function
6180 types. Return integer_zero_node, if FROM cannot be converted to a
6181 TO type. If FORCE is true, then allow reverse conversions as well. */
6184 get_delta_difference (from
, to
, force
)
6188 tree delta
= integer_zero_node
;
6194 /* Should get_base_distance here, so we can check if any thing along the
6195 path is virtual, and we need to make sure we stay
6196 inside the real binfos when going through virtual bases.
6197 Maybe we should replace virtual bases with
6198 binfo_member (...CLASSTYPE_VBASECLASSES...)... (mrs) */
6199 binfo
= get_binfo (from
, to
, 1);
6200 if (binfo
== error_mark_node
)
6202 error (" in pointer to member function conversiona");
6209 error_not_base_type (from
, to
);
6210 error (" in pointer to member conversion");
6213 binfo
= get_binfo (to
, from
, 1);
6214 if (binfo
== error_mark_node
)
6217 error (" in pointer to member conversion");
6223 cp_error ("cannot convert pointer to member of type %T to unrelated pointer to member of type %T", from
, to
);
6226 if (TREE_VIA_VIRTUAL (binfo
))
6228 binfo
= binfo_member (BINFO_TYPE (binfo
),
6229 CLASSTYPE_VBASECLASSES (from
));
6230 warning ("pointer to member conversion to virtual base class will only work if you are very careful");
6232 delta
= BINFO_OFFSET (binfo
);
6233 delta
= cp_convert (ptrdiff_type_node
, delta
);
6235 return build_binary_op (MINUS_EXPR
,
6239 if (TREE_VIA_VIRTUAL (binfo
))
6241 warning ("pointer to member conversion from virtual base class will only work if you are very careful");
6243 return BINFO_OFFSET (binfo
);
6247 build_ptrmemfunc1 (type
, delta
, idx
, pfn
, delta2
)
6248 tree type
, delta
, idx
, pfn
, delta2
;
6253 /* This is the old way we did it. We want to avoid calling
6254 digest_init, so that it can give an error if we use { } when
6255 initializing a pointer to member function. */
6259 u
= build_nt (CONSTRUCTOR
, NULL_TREE
,
6260 tree_cons (pfn_identifier
, pfn
, NULL_TREE
));
6264 u
= build_nt (CONSTRUCTOR
, NULL_TREE
,
6265 tree_cons (delta2_identifier
, delta2
, NULL_TREE
));
6268 u
= build_nt (CONSTRUCTOR
, NULL_TREE
,
6269 tree_cons (NULL_TREE
, delta
,
6270 tree_cons (NULL_TREE
, idx
,
6271 tree_cons (NULL_TREE
, u
, NULL_TREE
))));
6273 return digest_init (type
, u
, (tree
*)0);
6275 tree delta_field
, idx_field
, pfn_or_delta2_field
, pfn_field
, delta2_field
;
6277 int allconstant
, allsimple
;
6279 delta_field
= TYPE_FIELDS (type
);
6280 idx_field
= TREE_CHAIN (delta_field
);
6281 pfn_or_delta2_field
= TREE_CHAIN (idx_field
);
6282 subtype
= TREE_TYPE (pfn_or_delta2_field
);
6283 pfn_field
= TYPE_FIELDS (subtype
);
6284 delta2_field
= TREE_CHAIN (pfn_field
);
6288 allconstant
= TREE_CONSTANT (pfn
);
6289 allsimple
= !! initializer_constant_valid_p (pfn
, TREE_TYPE (pfn
));
6290 u
= tree_cons (pfn_field
, pfn
, NULL_TREE
);
6294 delta2
= convert_and_check (delta_type_node
, delta2
);
6295 allconstant
= TREE_CONSTANT (delta2
);
6296 allsimple
= !! initializer_constant_valid_p (delta2
, TREE_TYPE (delta2
));
6297 u
= tree_cons (delta2_field
, delta2
, NULL_TREE
);
6300 delta
= convert_and_check (delta_type_node
, delta
);
6301 idx
= convert_and_check (delta_type_node
, idx
);
6303 allconstant
= allconstant
&& TREE_CONSTANT (delta
) && TREE_CONSTANT (idx
);
6304 allsimple
= allsimple
6305 && initializer_constant_valid_p (delta
, TREE_TYPE (delta
))
6306 && initializer_constant_valid_p (idx
, TREE_TYPE (idx
));
6308 u
= build (CONSTRUCTOR
, subtype
, NULL_TREE
, u
);
6309 u
= tree_cons (delta_field
, delta
,
6310 tree_cons (idx_field
, idx
,
6311 tree_cons (pfn_or_delta2_field
, u
, NULL_TREE
)));
6312 u
= build (CONSTRUCTOR
, type
, NULL_TREE
, u
);
6313 TREE_CONSTANT (u
) = allconstant
;
6314 TREE_STATIC (u
) = allconstant
&& allsimple
;
6319 /* Build a constructor for a pointer to member function. It can be
6320 used to initialize global variables, local variable, or used
6321 as a value in expressions. TYPE is the POINTER to METHOD_TYPE we
6324 If FORCE is non-zero, then force this conversion, even if
6325 we would rather not do it. Usually set when using an explicit
6328 Return error_mark_node, if something goes wrong. */
6331 build_ptrmemfunc (type
, pfn
, force
)
6335 tree idx
= integer_zero_node
;
6336 tree delta
= integer_zero_node
;
6337 tree delta2
= integer_zero_node
;
6339 tree npfn
= NULL_TREE
;
6341 /* Handle multiple conversions of pointer to member functions. */
6342 if (TYPE_PTRMEMFUNC_P (TREE_TYPE (pfn
)))
6344 tree ndelta
, ndelta2
;
6347 /* Is is already the right type? */
6348 if (type
== TYPE_PTRMEMFUNC_FN_TYPE (TREE_TYPE (pfn
)))
6351 ndelta
= cp_convert (ptrdiff_type_node
, build_component_ref (pfn
, delta_identifier
, NULL_TREE
, 0));
6352 ndelta2
= cp_convert (ptrdiff_type_node
, DELTA2_FROM_PTRMEMFUNC (pfn
));
6353 idx
= build_component_ref (pfn
, index_identifier
, NULL_TREE
, 0);
6355 n
= get_delta_difference (TYPE_METHOD_BASETYPE (TREE_TYPE (TYPE_PTRMEMFUNC_FN_TYPE (TREE_TYPE (pfn
)))),
6356 TYPE_METHOD_BASETYPE (TREE_TYPE (type
)),
6359 delta
= build_binary_op (PLUS_EXPR
, ndelta
, n
, 1);
6360 delta2
= build_binary_op (PLUS_EXPR
, ndelta2
, n
, 1);
6361 e1
= fold (build (GT_EXPR
, boolean_type_node
, idx
, integer_zero_node
));
6363 e2
= build_ptrmemfunc1 (TYPE_GET_PTRMEMFUNC_TYPE (type
), delta
, idx
,
6366 pfn
= PFN_FROM_PTRMEMFUNC (pfn
);
6367 npfn
= build1 (NOP_EXPR
, type
, pfn
);
6368 TREE_CONSTANT (npfn
) = TREE_CONSTANT (pfn
);
6370 e3
= build_ptrmemfunc1 (TYPE_GET_PTRMEMFUNC_TYPE (type
), delta
, idx
, npfn
,
6372 return build_conditional_expr (e1
, e2
, e3
);
6375 /* Handle null pointer to member function conversions. */
6376 if (integer_zerop (pfn
))
6378 pfn
= build_c_cast (type
, integer_zero_node
);
6379 return build_ptrmemfunc1 (TYPE_GET_PTRMEMFUNC_TYPE (type
),
6380 integer_zero_node
, integer_zero_node
,
6384 if (TREE_CODE (pfn
) == TREE_LIST
6385 || (TREE_CODE (pfn
) == ADDR_EXPR
6386 && TREE_CODE (TREE_OPERAND (pfn
, 0)) == TREE_LIST
))
6387 return instantiate_type (type
, pfn
, 1);
6389 /* Allow pointer to member conversions here. */
6390 delta
= get_delta_difference (TYPE_METHOD_BASETYPE (TREE_TYPE (TREE_TYPE (pfn
))),
6391 TYPE_METHOD_BASETYPE (TREE_TYPE (type
)),
6393 delta2
= build_binary_op (PLUS_EXPR
, delta2
, delta
, 1);
6396 /* We need to check the argument types to see if they are compatible
6397 (any const or volatile violations. */
6398 something like
this:
6399 comptype (TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (type
))),
6400 TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (TREE_TYPE (pfn
)))), ?);
6403 if (TREE_CODE (TREE_OPERAND (pfn
, 0)) != FUNCTION_DECL
)
6404 warning ("assuming pointer to member function is non-virtual");
6406 if (TREE_CODE (TREE_OPERAND (pfn
, 0)) == FUNCTION_DECL
6407 && DECL_VINDEX (TREE_OPERAND (pfn
, 0)))
6409 /* Find the offset to the vfield pointer in the object. */
6410 vfield_offset
= get_binfo (DECL_CONTEXT (TREE_OPERAND (pfn
, 0)),
6411 DECL_CLASS_CONTEXT (TREE_OPERAND (pfn
, 0)),
6413 vfield_offset
= get_vfield_offset (vfield_offset
);
6414 delta2
= size_binop (PLUS_EXPR
, vfield_offset
, delta2
);
6416 /* Map everything down one to make room for the null pointer to member. */
6417 idx
= size_binop (PLUS_EXPR
,
6418 DECL_VINDEX (TREE_OPERAND (pfn
, 0)),
6423 idx
= size_binop (MINUS_EXPR
, integer_zero_node
, integer_one_node
);
6425 if (type
== TREE_TYPE (pfn
))
6431 npfn
= build1 (NOP_EXPR
, type
, pfn
);
6432 TREE_CONSTANT (npfn
) = TREE_CONSTANT (pfn
);
6436 return build_ptrmemfunc1 (TYPE_GET_PTRMEMFUNC_TYPE (type
), delta
, idx
, npfn
, delta2
);
6439 /* Convert value RHS to type TYPE as preparation for an assignment
6440 to an lvalue of type TYPE.
6441 The real work of conversion is done by `convert'.
6442 The purpose of this function is to generate error messages
6443 for assignments that are not allowed in C.
6444 ERRTYPE is a string to use in error messages:
6445 "assignment", "return", etc.
6447 C++: attempts to allow `convert' to find conversions involving
6448 implicit type conversion between aggregate and scalar types
6449 as per 8.5.6 of C++ manual. Does not randomly dereference
6450 pointers to aggregates! */
6453 convert_for_assignment (type
, rhs
, errtype
, fndecl
, parmnum
)
6459 register enum tree_code codel
= TREE_CODE (type
);
6460 register tree rhstype
;
6461 register enum tree_code coder
= TREE_CODE (TREE_TYPE (rhs
));
6463 if (coder
== UNKNOWN_TYPE
)
6464 rhs
= instantiate_type (type
, rhs
, 1);
6466 if (coder
== ERROR_MARK
)
6467 return error_mark_node
;
6469 if (codel
== OFFSET_TYPE
)
6471 type
= TREE_TYPE (type
);
6472 codel
= TREE_CODE (type
);
6475 /* Strip NON_LVALUE_EXPRs since we aren't using as an lvalue. */
6476 if (TREE_CODE (rhs
) == NON_LVALUE_EXPR
)
6477 rhs
= TREE_OPERAND (rhs
, 0);
6479 if (rhs
== error_mark_node
)
6480 return error_mark_node
;
6482 if (TREE_VALUE (rhs
) == error_mark_node
)
6483 return error_mark_node
;
6485 if (TREE_CODE (TREE_TYPE (rhs
)) == OFFSET_TYPE
)
6487 rhs
= resolve_offset_ref (rhs
);
6488 if (rhs
== error_mark_node
)
6489 return error_mark_node
;
6490 rhstype
= TREE_TYPE (rhs
);
6491 coder
= TREE_CODE (rhstype
);
6494 if (TREE_CODE (TREE_TYPE (rhs
)) == ARRAY_TYPE
6495 || TREE_CODE (TREE_TYPE (rhs
)) == FUNCTION_TYPE
6496 || TREE_CODE (TREE_TYPE (rhs
)) == METHOD_TYPE
)
6497 rhs
= default_conversion (rhs
);
6498 else if (TREE_CODE (TREE_TYPE (rhs
)) == REFERENCE_TYPE
)
6499 rhs
= convert_from_reference (rhs
);
6501 rhstype
= TREE_TYPE (rhs
);
6502 coder
= TREE_CODE (rhstype
);
6504 /* This should no longer change types on us. */
6505 if (TREE_CODE (rhs
) == CONST_DECL
)
6506 rhs
= DECL_INITIAL (rhs
);
6507 else if (TREE_READONLY_DECL_P (rhs
))
6508 rhs
= decl_constant_value (rhs
);
6510 if (type
== rhstype
)
6512 overflow_warning (rhs
);
6516 if (coder
== VOID_TYPE
)
6518 error ("void value not ignored as it ought to be");
6519 return error_mark_node
;
6521 /* Arithmetic types all interconvert. */
6522 if ((codel
== INTEGER_TYPE
|| codel
== REAL_TYPE
|| codel
== BOOLEAN_TYPE
6523 || codel
== COMPLEX_TYPE
)
6524 && (coder
== INTEGER_TYPE
|| coder
== REAL_TYPE
|| coder
== BOOLEAN_TYPE
6525 || coder
== COMPLEX_TYPE
))
6527 /* But we should warn if assigning REAL_TYPE to INTEGER_TYPE. */
6528 if (coder
== REAL_TYPE
&& codel
== INTEGER_TYPE
)
6531 cp_warning ("`%T' used for argument %P of `%D'",
6532 rhstype
, parmnum
, fndecl
);
6534 cp_warning ("%s to `%T' from `%T'", errtype
, type
, rhstype
);
6536 /* And we should warn if assigning a negative value to
6537 an unsigned variable. */
6538 else if (TREE_UNSIGNED (type
) && codel
!= BOOLEAN_TYPE
)
6540 if (TREE_CODE (rhs
) == INTEGER_CST
6541 && TREE_NEGATED_INT (rhs
))
6544 cp_warning ("negative value `%E' passed as argument %P of `%D'",
6545 rhs
, parmnum
, fndecl
);
6547 cp_warning ("%s of negative value `%E' to `%T'",
6548 errtype
, rhs
, type
);
6550 overflow_warning (rhs
);
6551 if (TREE_CONSTANT (rhs
))
6555 return convert_and_check (type
, rhs
);
6557 /* Conversions involving enums. */
6558 else if ((codel
== ENUMERAL_TYPE
6559 && (INTEGRAL_CODE_P (coder
) || coder
== REAL_TYPE
))
6560 || (coder
== ENUMERAL_TYPE
6561 && (INTEGRAL_CODE_P (codel
) || codel
== REAL_TYPE
)))
6563 return ocp_convert (type
, rhs
, CONV_IMPLICIT
, LOOKUP_NORMAL
);
6565 /* Conversions among pointers */
6566 else if (codel
== POINTER_TYPE
6567 && (coder
== POINTER_TYPE
6568 || (coder
== RECORD_TYPE
6569 && (IS_SIGNATURE_POINTER (rhstype
)
6570 || IS_SIGNATURE_REFERENCE (rhstype
)))))
6572 register tree ttl
= TREE_TYPE (type
);
6576 if (coder
== RECORD_TYPE
)
6578 rhs
= build_optr_ref (rhs
);
6579 rhstype
= TREE_TYPE (rhs
);
6581 ttr
= TREE_TYPE (rhstype
);
6583 /* If both pointers are of aggregate type, then we
6584 can give better error messages, and save some work
6586 if (TREE_CODE (ttl
) == RECORD_TYPE
&& TREE_CODE (ttr
) == RECORD_TYPE
)
6590 if (TYPE_MAIN_VARIANT (ttl
) == TYPE_MAIN_VARIANT (ttr
)
6591 || type
== class_star_type_node
6592 || rhstype
== class_star_type_node
)
6593 binfo
= TYPE_BINFO (ttl
);
6595 binfo
= get_binfo (ttl
, ttr
, 1);
6597 if (binfo
== error_mark_node
)
6598 return error_mark_node
;
6600 return error_not_base_type (ttl
, ttr
);
6602 if (! TYPE_READONLY (ttl
) && TYPE_READONLY (ttr
))
6605 cp_pedwarn ("passing `%T' as argument %P of `%D' discards const",
6606 rhstype
, parmnum
, fndecl
);
6608 cp_pedwarn ("%s to `%T' from `%T' discards const",
6609 errtype
, type
, rhstype
);
6611 if (! TYPE_VOLATILE (ttl
) && TYPE_VOLATILE (ttr
))
6614 cp_pedwarn ("passing `%T' as argument %P of `%D' discards volatile",
6615 rhstype
, parmnum
, fndecl
);
6617 cp_pedwarn ("%s to `%T' from `%T' discards volatile",
6618 errtype
, type
, rhstype
);
6622 /* Any non-function converts to a [const][volatile] void *
6623 and vice versa; otherwise, targets must be the same.
6624 Meanwhile, the lhs target must have all the qualifiers of the rhs. */
6625 else if (TYPE_MAIN_VARIANT (ttl
) == void_type_node
6626 || TYPE_MAIN_VARIANT (ttr
) == void_type_node
6627 || (ctt
= comp_target_types (type
, rhstype
, 1))
6628 || (unsigned_type (TYPE_MAIN_VARIANT (ttl
))
6629 == unsigned_type (TYPE_MAIN_VARIANT (ttr
))))
6631 /* ARM $4.8, commentary on p39. */
6632 if (TYPE_MAIN_VARIANT (ttl
) == void_type_node
6633 && TREE_CODE (ttr
) == OFFSET_TYPE
)
6635 cp_error ("no standard conversion from `%T' to `void *'", ttr
);
6636 return error_mark_node
;
6639 if (ctt
< 0 && TYPE_MAIN_VARIANT (ttl
) != TYPE_MAIN_VARIANT (ttr
))
6640 cp_pedwarn ("converting `%T' to `%T' is a contravariance violation",
6643 if (TYPE_MAIN_VARIANT (ttl
) != void_type_node
6644 && TYPE_MAIN_VARIANT (ttr
) == void_type_node
6645 && ! null_ptr_cst_p (rhs
))
6647 if (coder
== RECORD_TYPE
)
6648 cp_pedwarn ("implicit conversion of signature pointer to type `%T'",
6651 pedwarn ("ANSI C++ forbids implicit conversion from `void *' in %s",
6654 /* Const and volatile mean something different for function types,
6655 so the usual warnings are not appropriate. */
6656 else if ((TREE_CODE (ttr
) != FUNCTION_TYPE
&& TREE_CODE (ttr
) != METHOD_TYPE
)
6657 || (TREE_CODE (ttl
) != FUNCTION_TYPE
&& TREE_CODE (ttl
) != METHOD_TYPE
))
6659 if (TREE_CODE (ttl
) == OFFSET_TYPE
6660 && binfo_member (TYPE_OFFSET_BASETYPE (ttr
),
6661 CLASSTYPE_VBASECLASSES (TYPE_OFFSET_BASETYPE (ttl
))))
6663 sorry ("%s between pointer to members converting across virtual baseclasses", errtype
);
6664 return error_mark_node
;
6666 else if (! TYPE_READONLY (ttl
) && TYPE_READONLY (ttr
))
6669 cp_pedwarn ("passing `%T' as argument %P of `%D' discards const",
6670 rhstype
, parmnum
, fndecl
);
6672 cp_pedwarn ("%s to `%T' from `%T' discards const",
6673 errtype
, type
, rhstype
);
6675 else if (! TYPE_VOLATILE (ttl
) && TYPE_VOLATILE (ttr
))
6678 cp_pedwarn ("passing `%T' as argument %P of `%D' discards volatile",
6679 rhstype
, parmnum
, fndecl
);
6681 cp_pedwarn ("%s to `%T' from `%T' discards volatile",
6682 errtype
, type
, rhstype
);
6684 else if (TREE_CODE (ttl
) == TREE_CODE (ttr
)
6685 && ! comp_target_types (type
, rhstype
, 1))
6688 cp_pedwarn ("passing `%T' as argument %P of `%D' changes signedness",
6689 rhstype
, parmnum
, fndecl
);
6691 cp_pedwarn ("%s to `%T' from `%T' changes signedness",
6692 errtype
, type
, rhstype
);
6698 int add_quals
= 0, const_parity
= 0, volatile_parity
= 0;
6700 int unsigned_parity
;
6703 /* This code is basically a duplicate of comp_ptr_ttypes_real. */
6704 for (; ; ttl
= TREE_TYPE (ttl
), ttr
= TREE_TYPE (ttr
))
6707 const_parity
|= (TYPE_READONLY (ttl
) < TYPE_READONLY (ttr
));
6708 volatile_parity
|= (TYPE_VOLATILE (ttl
) < TYPE_VOLATILE (ttr
));
6711 && (TYPE_READONLY (ttl
) > TYPE_READONLY (ttr
)
6712 || TYPE_VOLATILE (ttl
) > TYPE_VOLATILE (ttr
)))
6714 left_const
&= TYPE_READONLY (ttl
);
6716 if (TREE_CODE (ttl
) != POINTER_TYPE
6717 || TREE_CODE (ttr
) != POINTER_TYPE
)
6720 unsigned_parity
= TREE_UNSIGNED (ttl
) - TREE_UNSIGNED (ttr
);
6721 if (unsigned_parity
)
6723 if (TREE_UNSIGNED (ttl
))
6724 ttr
= unsigned_type (ttr
);
6726 ttl
= unsigned_type (ttl
);
6729 if (comp_target_types (ttl
, ttr
, nptrs
) > 0)
6734 cp_pedwarn ("passing `%T' as argument %P of `%D' adds cv-quals without intervening `const'",
6735 rhstype
, parmnum
, fndecl
);
6737 cp_pedwarn ("%s to `%T' from `%T' adds cv-quals without intervening `const'",
6738 errtype
, type
, rhstype
);
6743 cp_pedwarn ("passing `%T' as argument %P of `%D' discards const",
6744 rhstype
, parmnum
, fndecl
);
6746 cp_pedwarn ("%s to `%T' from `%T' discards const",
6747 errtype
, type
, rhstype
);
6749 if (volatile_parity
)
6752 cp_pedwarn ("passing `%T' as argument %P of `%D' discards volatile",
6753 rhstype
, parmnum
, fndecl
);
6755 cp_pedwarn ("%s to `%T' from `%T' discards volatile",
6756 errtype
, type
, rhstype
);
6758 if (unsigned_parity
> 0)
6761 cp_pedwarn ("passing `%T' as argument %P of `%D' changes signed to unsigned",
6762 rhstype
, parmnum
, fndecl
);
6764 cp_pedwarn ("%s to `%T' from `%T' changes signed to unsigned",
6765 errtype
, type
, rhstype
);
6767 else if (unsigned_parity
< 0)
6770 cp_pedwarn ("passing `%T' as argument %P of `%D' changes unsigned to signed",
6771 rhstype
, parmnum
, fndecl
);
6773 cp_pedwarn ("%s to `%T' from `%T' changes unsigned to signed",
6774 errtype
, type
, rhstype
);
6777 /* C++ is not so friendly about converting function and
6778 member function pointers as C. Emit warnings here. */
6779 if (TREE_CODE (ttl
) == FUNCTION_TYPE
6780 || TREE_CODE (ttl
) == METHOD_TYPE
)
6781 if (! comptypes (ttl
, ttr
, 0))
6783 warning ("conflicting function types in %s:", errtype
);
6784 cp_warning ("\t`%T' != `%T'", type
, rhstype
);
6790 cp_error ("passing `%T' as argument %P of `%D'",
6791 rhstype
, parmnum
, fndecl
);
6793 cp_error ("%s to `%T' from `%T'", errtype
, type
, rhstype
);
6794 return error_mark_node
;
6797 return cp_convert (type
, rhs
);
6799 else if (codel
== POINTER_TYPE
&& coder
== INTEGER_TYPE
)
6801 /* An explicit constant 0 can convert to a pointer,
6802 but not a 0 that results from casting or folding. */
6803 if (! (TREE_CODE (rhs
) == INTEGER_CST
&& integer_zerop (rhs
)))
6806 cp_pedwarn ("passing `%T' to argument %P of `%D' lacks a cast",
6807 rhstype
, parmnum
, fndecl
);
6809 cp_pedwarn ("%s to `%T' from `%T' lacks a cast",
6810 errtype
, type
, rhstype
);
6812 return cp_convert (type
, rhs
);
6814 else if (codel
== INTEGER_TYPE
6815 && (coder
== POINTER_TYPE
6816 || (coder
== RECORD_TYPE
6817 && (IS_SIGNATURE_POINTER (rhstype
)
6818 || TYPE_PTRMEMFUNC_FLAG (rhstype
)
6819 || IS_SIGNATURE_REFERENCE (rhstype
)))))
6822 cp_pedwarn ("passing `%T' to argument %P of `%D' lacks a cast",
6823 rhstype
, parmnum
, fndecl
);
6825 cp_pedwarn ("%s to `%T' from `%T' lacks a cast",
6826 errtype
, type
, rhstype
);
6827 return cp_convert (type
, rhs
);
6829 else if (codel
== BOOLEAN_TYPE
6830 && (coder
== POINTER_TYPE
6831 || (coder
== RECORD_TYPE
6832 && (IS_SIGNATURE_POINTER (rhstype
)
6833 || TYPE_PTRMEMFUNC_FLAG (rhstype
)
6834 || IS_SIGNATURE_REFERENCE (rhstype
)))))
6835 return cp_convert (type
, rhs
);
6838 else if (((coder
== POINTER_TYPE
6839 && TREE_CODE (TREE_TYPE (rhstype
)) == METHOD_TYPE
)
6840 || integer_zerop (rhs
)
6841 || TYPE_PTRMEMFUNC_P (rhstype
))
6842 && TYPE_PTRMEMFUNC_P (type
))
6844 tree ttl
= TYPE_PTRMEMFUNC_FN_TYPE (type
);
6845 tree ttr
= (TREE_CODE (rhstype
) == POINTER_TYPE
? rhstype
6846 : TYPE_PTRMEMFUNC_FN_TYPE (type
));
6847 int ctt
= comp_target_types (ttl
, ttr
, 1);
6850 cp_pedwarn ("converting `%T' to `%T' is a contravariance violation",
6853 cp_error ("%s to `%T' from `%T'", errtype
, ttl
, ttr
);
6855 /* compatible pointer to member functions. */
6856 return build_ptrmemfunc (ttl
, rhs
, 0);
6858 else if (codel
== ERROR_MARK
|| coder
== ERROR_MARK
)
6859 return error_mark_node
;
6861 /* This should no longer happen. References are initialized via
6862 `convert_for_initialization'. They should otherwise be
6863 bashed before coming here. */
6864 else if (codel
== REFERENCE_TYPE
)
6865 my_friendly_abort (317);
6866 else if (TYPE_MAIN_VARIANT (type
) == TYPE_MAIN_VARIANT (TREE_TYPE (rhs
)))
6868 tree nrhs
= build1 (NOP_EXPR
, type
, rhs
);
6869 TREE_CONSTANT (nrhs
) = TREE_CONSTANT (rhs
);
6872 else if (TYPE_HAS_CONSTRUCTOR (type
) || IS_AGGR_TYPE (TREE_TYPE (rhs
)))
6873 return cp_convert (type
, rhs
);
6874 /* Handle anachronistic conversions from (::*)() to cv void* or (*)(). */
6875 else if (TREE_CODE (type
) == POINTER_TYPE
6876 && (TREE_CODE (TREE_TYPE (type
)) == FUNCTION_TYPE
6877 || TYPE_MAIN_VARIANT (TREE_TYPE (type
)) == void_type_node
)
6879 && TYPE_PTRMEMFUNC_P (TREE_TYPE (rhs
)))
6880 return cp_convert (type
, rhs
);
6882 cp_error ("%s to `%T' from `%T'", errtype
, type
, rhstype
);
6883 return error_mark_node
;
6886 /* Convert RHS to be of type TYPE. If EXP is non-zero,
6887 it is the target of the initialization.
6888 ERRTYPE is a string to use in error messages.
6890 Two major differences between the behavior of
6891 `convert_for_assignment' and `convert_for_initialization'
6892 are that references are bashed in the former, while
6893 copied in the latter, and aggregates are assigned in
6894 the former (operator=) while initialized in the
6897 If using constructor make sure no conversion operator exists, if one does
6898 exist, an ambiguity exists.
6900 If flags doesn't include LOOKUP_COMPLAIN, don't complain about anything. */
6903 convert_for_initialization (exp
, type
, rhs
, flags
, errtype
, fndecl
, parmnum
)
6904 tree exp
, type
, rhs
;
6910 register enum tree_code codel
= TREE_CODE (type
);
6911 register tree rhstype
;
6912 register enum tree_code coder
;
6914 /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
6915 Strip such NOP_EXPRs, since RHS is used in non-lvalue context. */
6916 if (TREE_CODE (rhs
) == NOP_EXPR
6917 && TREE_TYPE (rhs
) == TREE_TYPE (TREE_OPERAND (rhs
, 0))
6918 && codel
!= REFERENCE_TYPE
)
6919 rhs
= TREE_OPERAND (rhs
, 0);
6921 if (rhs
== error_mark_node
6922 || (TREE_CODE (rhs
) == TREE_LIST
&& TREE_VALUE (rhs
) == error_mark_node
))
6923 return error_mark_node
;
6925 if (TREE_CODE (TREE_TYPE (rhs
)) == OFFSET_TYPE
)
6927 rhs
= resolve_offset_ref (rhs
);
6928 if (rhs
== error_mark_node
)
6929 return error_mark_node
;
6932 if (TREE_CODE (TREE_TYPE (rhs
)) == REFERENCE_TYPE
)
6933 rhs
= convert_from_reference (rhs
);
6935 if ((TREE_CODE (TREE_TYPE (rhs
)) == ARRAY_TYPE
6936 && TREE_CODE (type
) != ARRAY_TYPE
6937 && (TREE_CODE (type
) != REFERENCE_TYPE
6938 || TREE_CODE (TREE_TYPE (type
)) != ARRAY_TYPE
))
6939 || TREE_CODE (TREE_TYPE (rhs
)) == FUNCTION_TYPE
6940 || TREE_CODE (TREE_TYPE (rhs
)) == METHOD_TYPE
)
6941 rhs
= default_conversion (rhs
);
6943 rhstype
= TREE_TYPE (rhs
);
6944 coder
= TREE_CODE (rhstype
);
6946 if (coder
== UNKNOWN_TYPE
)
6948 rhs
= instantiate_type (type
, rhs
, 1);
6949 rhstype
= TREE_TYPE (rhs
);
6950 coder
= TREE_CODE (rhstype
);
6953 if (coder
== ERROR_MARK
)
6954 return error_mark_node
;
6956 /* We accept references to incomplete types, so we can
6957 return here before checking if RHS is of complete type. */
6959 if (codel
== REFERENCE_TYPE
)
6961 /* This should eventually happen in convert_arguments. */
6962 extern int warningcount
, errorcount
;
6966 savew
= warningcount
, savee
= errorcount
;
6967 rhs
= convert_to_reference (type
, rhs
, CONV_IMPLICIT
, flags
,
6968 exp
? exp
: error_mark_node
);
6971 if (warningcount
> savew
)
6972 cp_warning_at ("in passing argument %P of `%+D'", parmnum
, fndecl
);
6973 else if (errorcount
> savee
)
6974 cp_error_at ("in passing argument %P of `%+D'", parmnum
, fndecl
);
6979 rhs
= require_complete_type (rhs
);
6980 if (rhs
== error_mark_node
)
6981 return error_mark_node
;
6983 if (exp
!= 0) exp
= require_complete_type (exp
);
6984 if (exp
== error_mark_node
)
6985 return error_mark_node
;
6987 if (TREE_CODE (rhstype
) == REFERENCE_TYPE
)
6988 rhstype
= TREE_TYPE (rhstype
);
6990 type
= complete_type (type
);
6992 if (TYPE_LANG_SPECIFIC (type
)
6993 && (IS_SIGNATURE_POINTER (type
) || IS_SIGNATURE_REFERENCE (type
)))
6994 return build_signature_pointer_constructor (type
, rhs
);
6996 if (IS_AGGR_TYPE (type
)
6997 && (TYPE_NEEDS_CONSTRUCTING (type
) || TREE_HAS_CONSTRUCTOR (rhs
)))
6999 if (flag_ansi_overloading
)
7000 return ocp_convert (type
, rhs
, CONV_IMPLICIT
|CONV_FORCE_TEMP
, flags
);
7002 if (TYPE_MAIN_VARIANT (type
) == TYPE_MAIN_VARIANT (rhstype
))
7004 /* This is sufficient to perform initialization. No need,
7005 apparently, to go through X(X&) to do first-cut
7006 initialization. Return through a TARGET_EXPR so that we get
7007 cleanups if it is used. */
7008 if (TREE_CODE (rhs
) == CALL_EXPR
)
7010 rhs
= build_cplus_new (type
, rhs
);
7013 /* Handle the case of default parameter initialization and
7014 initialization of static variables. */
7015 else if (TREE_CODE (rhs
) == TARGET_EXPR
)
7017 else if (TREE_CODE (rhs
) == INDIRECT_REF
&& TREE_HAS_CONSTRUCTOR (rhs
))
7019 my_friendly_assert (TREE_CODE (TREE_OPERAND (rhs
, 0)) == CALL_EXPR
, 318);
7022 my_friendly_assert (TREE_VALUE (TREE_OPERAND (TREE_OPERAND (rhs
, 0), 1)) == NULL_TREE
, 316);
7023 TREE_VALUE (TREE_OPERAND (TREE_OPERAND (rhs
, 0), 1))
7024 = build_unary_op (ADDR_EXPR
, exp
, 0);
7027 rhs
= build_cplus_new (type
, TREE_OPERAND (rhs
, 0));
7030 else if (TYPE_HAS_TRIVIAL_INIT_REF (type
))
7033 if (TYPE_MAIN_VARIANT (type
) == TYPE_MAIN_VARIANT (rhstype
)
7034 || (IS_AGGR_TYPE (rhstype
) && UNIQUELY_DERIVED_FROM_P (type
, rhstype
)))
7036 if (TYPE_HAS_INIT_REF (type
))
7038 tree init
= build_method_call (exp
, ctor_identifier
,
7039 build_tree_list (NULL_TREE
, rhs
),
7040 TYPE_BINFO (type
), LOOKUP_NORMAL
);
7042 if (init
== error_mark_node
)
7043 return error_mark_node
;
7047 exp
= build_cplus_new (type
, init
);
7051 return build (COMPOUND_EXPR
, type
, init
, exp
);
7054 /* ??? The following warnings are turned off because
7055 this is another place where the default X(X&) constructor
7057 if (TYPE_HAS_ASSIGNMENT (type
))
7058 cp_warning ("bitwise copy: `%T' defines operator=", type
);
7060 if (TREE_CODE (TREE_TYPE (rhs
)) == REFERENCE_TYPE
)
7061 rhs
= convert_from_reference (rhs
);
7062 if (type
!= rhstype
)
7064 tree nrhs
= build1 (NOP_EXPR
, type
, rhs
);
7065 TREE_CONSTANT (nrhs
) = TREE_CONSTANT (rhs
);
7071 return ocp_convert (type
, rhs
, CONV_OLD_CONVERT
,
7072 flags
| LOOKUP_NO_CONVERSION
);
7075 if (type
== TREE_TYPE (rhs
))
7077 if (TREE_READONLY_DECL_P (rhs
))
7078 rhs
= decl_constant_value (rhs
);
7082 return convert_for_assignment (type
, rhs
, errtype
, fndecl
, parmnum
);
7085 /* Expand an ASM statement with operands, handling output operands
7086 that are not variables or INDIRECT_REFS by transforming such
7087 cases into cases that expand_asm_operands can handle.
7089 Arguments are same as for expand_asm_operands.
7091 We don't do default conversions on all inputs, because it can screw
7092 up operands that are expected to be in memory. */
7095 c_expand_asm_operands (string
, outputs
, inputs
, clobbers
, vol
, filename
, line
)
7096 tree string
, outputs
, inputs
, clobbers
;
7101 int noutputs
= list_length (outputs
);
7103 /* o[I] is the place that output number I should be written. */
7104 register tree
*o
= (tree
*) alloca (noutputs
* sizeof (tree
));
7107 /* Record the contents of OUTPUTS before it is modified. */
7108 for (i
= 0, tail
= outputs
; tail
; tail
= TREE_CHAIN (tail
), i
++)
7109 o
[i
] = TREE_VALUE (tail
);
7111 /* Generate the ASM_OPERANDS insn;
7112 store into the TREE_VALUEs of OUTPUTS some trees for
7113 where the values were actually stored. */
7114 expand_asm_operands (string
, outputs
, inputs
, clobbers
, vol
, filename
, line
);
7116 /* Copy all the intermediate outputs into the specified outputs. */
7117 for (i
= 0, tail
= outputs
; tail
; tail
= TREE_CHAIN (tail
), i
++)
7119 if (o
[i
] != TREE_VALUE (tail
))
7121 expand_expr (build_modify_expr (o
[i
], NOP_EXPR
, TREE_VALUE (tail
)),
7122 const0_rtx
, VOIDmode
, 0);
7125 /* Detect modification of read-only values.
7126 (Otherwise done by build_modify_expr.) */
7129 tree type
= TREE_TYPE (o
[i
]);
7130 if (TYPE_READONLY (type
)
7131 || ((TREE_CODE (type
) == RECORD_TYPE
7132 || TREE_CODE (type
) == UNION_TYPE
)
7133 && C_TYPE_FIELDS_READONLY (type
)))
7134 readonly_error (o
[i
], "modification by `asm'", 1);
7138 /* Those MODIFY_EXPRs could do autoincrements. */
7142 /* Expand a C `return' statement.
7143 RETVAL is the expression for what to return,
7144 or a null pointer for `return;' with no value.
7146 C++: upon seeing a `return', we must call destructors on all
7147 variables in scope which had constructors called on them.
7148 This means that if in a destructor, the base class destructors
7149 must be called before returning.
7151 The RETURN statement in C++ has initialization semantics. */
7154 c_expand_return (retval
)
7157 extern struct nesting
*cond_stack
, *loop_stack
, *case_stack
;
7158 extern tree dtor_label
, ctor_label
;
7159 tree result
= DECL_RESULT (current_function_decl
);
7160 tree valtype
= TREE_TYPE (result
);
7162 if (TREE_THIS_VOLATILE (current_function_decl
))
7163 warning ("function declared `noreturn' has a `return' statement");
7165 if (retval
== error_mark_node
)
7167 current_function_returns_null
= 1;
7171 if (processing_template_decl
)
7173 add_tree (build_min_nt (RETURN_STMT
, retval
));
7177 if (retval
== NULL_TREE
)
7179 /* A non-named return value does not count. */
7181 /* Can't just return from a destructor. */
7184 expand_goto (dtor_label
);
7188 if (DECL_CONSTRUCTOR_P (current_function_decl
))
7189 retval
= current_class_ptr
;
7190 else if (DECL_NAME (result
) != NULL_TREE
7191 && TREE_CODE (valtype
) != VOID_TYPE
)
7195 current_function_returns_null
= 1;
7197 if (valtype
!= NULL_TREE
&& TREE_CODE (valtype
) != VOID_TYPE
)
7199 if (DECL_NAME (DECL_RESULT (current_function_decl
)) == NULL_TREE
)
7201 pedwarn ("`return' with no value, in function returning non-void");
7202 /* Clear this, so finish_function won't say that we
7203 reach the end of a non-void function (which we don't,
7204 we gave a return!). */
7205 current_function_returns_null
= 0;
7209 expand_null_return ();
7213 else if (DECL_CONSTRUCTOR_P (current_function_decl
)
7214 && retval
!= current_class_ptr
)
7216 if (flag_this_is_variable
)
7217 error ("return from a constructor: use `this = ...' instead");
7219 error ("return from a constructor");
7220 retval
= current_class_ptr
;
7223 /* Effective C++ rule 15. See also start_function. */
7225 && DECL_NAME (current_function_decl
) == ansi_opname
[(int) MODIFY_EXPR
]
7226 && retval
!= current_class_ref
)
7227 cp_warning ("`operator=' should return a reference to `*this'");
7229 if (valtype
== NULL_TREE
|| TREE_CODE (valtype
) == VOID_TYPE
)
7231 current_function_returns_null
= 1;
7232 if ((pedantic
&& ! DECL_ARTIFICIAL (current_function_decl
))
7233 || TREE_CODE (TREE_TYPE (retval
)) != VOID_TYPE
)
7234 pedwarn ("`return' with a value, in function returning void");
7235 expand_return (retval
);
7239 /* Now deal with possible C++ hair:
7240 (1) Compute the return value.
7241 (2) If there are aggregate values with destructors which
7242 must be cleaned up, clean them (taking care
7243 not to clobber the return value).
7244 (3) If an X(X&) constructor is defined, the return
7245 value must be returned via that. */
7247 if (retval
== result
7248 || DECL_CONSTRUCTOR_P (current_function_decl
))
7249 /* It's already done for us. */;
7250 else if (TREE_TYPE (retval
) == void_type_node
)
7252 pedwarn ("return of void value in function returning non-void");
7253 expand_expr_stmt (retval
);
7258 retval
= convert_for_initialization
7259 (NULL_TREE
, valtype
, retval
, LOOKUP_NORMAL
|LOOKUP_ONLYCONVERTING
,
7260 "return", NULL_TREE
, 0);
7262 if (retval
== error_mark_node
)
7264 /* Avoid warning about control reaching end of function. */
7265 expand_null_return ();
7269 /* We can't initialize a register from a NEW_EXPR. */
7270 else if (! current_function_returns_struct
7271 && TREE_CODE (retval
) == TARGET_EXPR
7272 && TREE_CODE (TREE_OPERAND (retval
, 1)) == NEW_EXPR
)
7273 retval
= build (COMPOUND_EXPR
, TREE_TYPE (retval
), retval
,
7274 TREE_OPERAND (retval
, 0));
7276 /* Add some useful error checking for C++. */
7277 else if (TREE_CODE (valtype
) == REFERENCE_TYPE
)
7279 tree whats_returned
;
7281 /* Sort through common things to see what it is
7282 we are returning. */
7283 whats_returned
= retval
;
7284 if (TREE_CODE (whats_returned
) == COMPOUND_EXPR
)
7286 whats_returned
= TREE_OPERAND (whats_returned
, 1);
7287 if (TREE_CODE (whats_returned
) == ADDR_EXPR
)
7288 whats_returned
= TREE_OPERAND (whats_returned
, 0);
7290 while (TREE_CODE (whats_returned
) == CONVERT_EXPR
7291 || TREE_CODE (whats_returned
) == NOP_EXPR
)
7292 whats_returned
= TREE_OPERAND (whats_returned
, 0);
7293 if (TREE_CODE (whats_returned
) == ADDR_EXPR
)
7295 whats_returned
= TREE_OPERAND (whats_returned
, 0);
7296 while (TREE_CODE (whats_returned
) == NEW_EXPR
7297 || TREE_CODE (whats_returned
) == TARGET_EXPR
)
7299 /* Get the target. */
7300 whats_returned
= TREE_OPERAND (whats_returned
, 0);
7301 warning ("returning reference to temporary");
7305 if (TREE_CODE (whats_returned
) == VAR_DECL
&& DECL_NAME (whats_returned
))
7307 if (TEMP_NAME_P (DECL_NAME (whats_returned
)))
7308 warning ("reference to non-lvalue returned");
7309 else if (! TREE_STATIC (whats_returned
)
7310 && IDENTIFIER_LOCAL_VALUE (DECL_NAME (whats_returned
))
7311 && !TREE_PUBLIC (whats_returned
))
7312 cp_warning_at ("reference to local variable `%D' returned", whats_returned
);
7315 else if (TREE_CODE (retval
) == ADDR_EXPR
)
7317 tree whats_returned
= TREE_OPERAND (retval
, 0);
7319 if (TREE_CODE (whats_returned
) == VAR_DECL
7320 && DECL_NAME (whats_returned
)
7321 && IDENTIFIER_LOCAL_VALUE (DECL_NAME (whats_returned
))
7322 && !TREE_STATIC (whats_returned
)
7323 && !TREE_PUBLIC (whats_returned
))
7324 cp_warning_at ("address of local variable `%D' returned", whats_returned
);
7328 if (retval
!= NULL_TREE
7329 && TREE_CODE_CLASS (TREE_CODE (retval
)) == 'd'
7330 && cond_stack
== 0 && loop_stack
== 0 && case_stack
== 0)
7331 current_function_return_value
= retval
;
7333 if (ctor_label
&& TREE_CODE (ctor_label
) != ERROR_MARK
)
7335 /* Here RETVAL is CURRENT_CLASS_PTR, so there's nothing to do. */
7336 expand_goto (ctor_label
);
7339 if (retval
&& retval
!= result
)
7341 result
= build (INIT_EXPR
, TREE_TYPE (result
), result
, retval
);
7342 TREE_SIDE_EFFECTS (result
) = 1;
7345 expand_start_target_temps ();
7347 expand_return (result
);
7349 expand_end_target_temps ();
7351 current_function_returns_value
= 1;
7354 /* Start a C switch statement, testing expression EXP.
7355 Return EXP if it is valid, an error node otherwise. */
7358 c_expand_start_case (exp
)
7362 register enum tree_code code
;
7364 /* Convert from references, etc. */
7365 exp
= default_conversion (exp
);
7366 type
= TREE_TYPE (exp
);
7367 code
= TREE_CODE (type
);
7369 if (IS_AGGR_TYPE_CODE (code
))
7370 exp
= build_type_conversion (CONVERT_EXPR
, integer_type_node
, exp
, 1);
7372 if (exp
== NULL_TREE
)
7374 error ("switch quantity not an integer");
7375 exp
= error_mark_node
;
7377 type
= TREE_TYPE (exp
);
7378 code
= TREE_CODE (type
);
7380 if (code
!= INTEGER_TYPE
&& code
!= ENUMERAL_TYPE
&& code
!= ERROR_MARK
)
7382 error ("switch quantity not an integer");
7383 exp
= error_mark_node
;
7389 exp
= default_conversion (exp
);
7390 type
= TREE_TYPE (exp
);
7391 idx
= get_unwidened (exp
, 0);
7392 /* We can't strip a conversion from a signed type to an unsigned,
7393 because if we did, int_fits_type_p would do the wrong thing
7394 when checking case values for being in range,
7395 and it's too hard to do the right thing. */
7396 if (TREE_UNSIGNED (TREE_TYPE (exp
))
7397 == TREE_UNSIGNED (TREE_TYPE (idx
)))
7402 (1, fold (build1 (CLEANUP_POINT_EXPR
, TREE_TYPE (exp
), exp
)),
7403 type
, "switch statement");
7408 /* CONSTP remembers whether or not all the intervening pointers in the `to'
7409 type have been const. */
7412 comp_ptr_ttypes_real (to
, from
, constp
)
7416 for (; ; to
= TREE_TYPE (to
), from
= TREE_TYPE (from
))
7418 if (TREE_CODE (to
) != TREE_CODE (from
))
7421 if (TREE_CODE (from
) == OFFSET_TYPE
7422 && comptypes (TYPE_OFFSET_BASETYPE (from
),
7423 TYPE_OFFSET_BASETYPE (to
), 1))
7426 /* Const and volatile mean something different for function types,
7427 so the usual checks are not appropriate. */
7428 if (TREE_CODE (to
) != FUNCTION_TYPE
&& TREE_CODE (to
) != METHOD_TYPE
)
7430 if (TYPE_READONLY (from
) > TYPE_READONLY (to
)
7431 || TYPE_VOLATILE (from
) > TYPE_VOLATILE (to
))
7435 && (TYPE_READONLY (to
) > TYPE_READONLY (from
)
7436 || TYPE_VOLATILE (to
) > TYPE_READONLY (from
)))
7438 constp
&= TYPE_READONLY (to
);
7441 if (TREE_CODE (to
) != POINTER_TYPE
)
7442 return comptypes (TYPE_MAIN_VARIANT (to
), TYPE_MAIN_VARIANT (from
), 1);
7446 /* When comparing, say, char ** to char const **, this function takes the
7447 'char *' and 'char const *'. Do not pass non-pointer types to this
7451 comp_ptr_ttypes (to
, from
)
7454 return comp_ptr_ttypes_real (to
, from
, 1);
7457 /* Returns 1 if to and from are (possibly multi-level) pointers to the same
7458 type or inheritance-related types, regardless of cv-quals. */
7461 ptr_reasonably_similar (to
, from
)
7464 for (; ; to
= TREE_TYPE (to
), from
= TREE_TYPE (from
))
7466 if (TREE_CODE (to
) != TREE_CODE (from
))
7469 if (TREE_CODE (from
) == OFFSET_TYPE
7470 && comptypes (TYPE_OFFSET_BASETYPE (to
),
7471 TYPE_OFFSET_BASETYPE (from
), -1))
7474 if (TREE_CODE (to
) != POINTER_TYPE
)
7476 (TYPE_MAIN_VARIANT (to
), TYPE_MAIN_VARIANT (from
), -1);
7480 /* Like comp_ptr_ttypes, for const_cast. */
7483 comp_ptr_ttypes_const (to
, from
)
7486 for (; ; to
= TREE_TYPE (to
), from
= TREE_TYPE (from
))
7488 if (TREE_CODE (to
) != TREE_CODE (from
))
7491 if (TREE_CODE (from
) == OFFSET_TYPE
7492 && comptypes (TYPE_OFFSET_BASETYPE (from
),
7493 TYPE_OFFSET_BASETYPE (to
), 1))
7496 if (TREE_CODE (to
) != POINTER_TYPE
)
7497 return comptypes (TYPE_MAIN_VARIANT (to
), TYPE_MAIN_VARIANT (from
), 1);
7501 /* Like comp_ptr_ttypes, for reinterpret_cast. */
7504 comp_ptr_ttypes_reinterpret (to
, from
)
7509 for (; ; to
= TREE_TYPE (to
), from
= TREE_TYPE (from
))
7511 if (TREE_CODE (from
) == OFFSET_TYPE
)
7512 from
= TREE_TYPE (from
);
7513 if (TREE_CODE (to
) == OFFSET_TYPE
)
7514 to
= TREE_TYPE (to
);
7516 if (TREE_CODE (to
) != TREE_CODE (from
))
7519 /* Const and volatile mean something different for function types,
7520 so the usual checks are not appropriate. */
7521 if (TREE_CODE (to
) != FUNCTION_TYPE
&& TREE_CODE (to
) != METHOD_TYPE
)
7523 if (TYPE_READONLY (from
) > TYPE_READONLY (to
)
7524 || TYPE_VOLATILE (from
) > TYPE_VOLATILE (to
))
7528 && (TYPE_READONLY (to
) > TYPE_READONLY (from
)
7529 || TYPE_VOLATILE (to
) > TYPE_READONLY (from
)))
7531 constp
&= TYPE_READONLY (to
);
7534 if (TREE_CODE (to
) != POINTER_TYPE
)