Update mainline egcs to gcc2 snapshot 971021.
[gcc.git] / gcc / cp / typeck.c
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)
4
5 This file is part of GNU CC.
6
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)
10 any later version.
11
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.
16
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. */
21
22
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.
27
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). */
31
32 #include "config.h"
33 #include <stdio.h>
34 #include "tree.h"
35 #include "rtl.h"
36 #include "cp-tree.h"
37 #include "flags.h"
38 #include "output.h"
39 #include "expr.h"
40
41 #ifdef HAVE_STRING_H
42 #include <string.h>
43 #endif
44
45 extern void compiler_error ();
46
47 static tree convert_for_assignment PROTO((tree, tree, char*, tree,
48 int));
49 static tree pointer_int_sum PROTO((enum tree_code, tree, tree));
50 static tree rationalize_conditional_expr PROTO((enum tree_code, tree));
51 static int comp_target_parms PROTO((tree, tree, int));
52 static int comp_ptr_ttypes_real PROTO((tree, tree, int));
53 static int comp_ptr_ttypes_const PROTO((tree, tree));
54 static int comp_ptr_ttypes_reinterpret PROTO((tree, tree));
55 static int comp_array_types PROTO((int (*) (tree, tree, int), tree,
56 tree, int));
57 static tree build_ptrmemfunc1 PROTO((tree, tree, tree, tree, tree));
58 static tree common_base_type PROTO((tree, tree));
59 static tree convert_sequence PROTO((tree, tree));
60 static tree lookup_anon_field PROTO((tree, tree));
61 static tree pointer_diff PROTO((tree, tree));
62 static tree qualify_type PROTO((tree, tree));
63 static tree expand_target_expr PROTO((tree));
64 static tree get_delta_difference PROTO((tree, tree, int));
65
66 /* Return the target type of TYPE, which meas return T for:
67 T*, T&, T[], T (...), and otherwise, just T. */
68
69 tree
70 target_type (type)
71 tree type;
72 {
73 if (TREE_CODE (type) == REFERENCE_TYPE)
74 type = TREE_TYPE (type);
75 while (TREE_CODE (type) == POINTER_TYPE
76 || TREE_CODE (type) == ARRAY_TYPE
77 || TREE_CODE (type) == FUNCTION_TYPE
78 || TREE_CODE (type) == METHOD_TYPE
79 || TREE_CODE (type) == OFFSET_TYPE)
80 type = TREE_TYPE (type);
81 return type;
82 }
83
84 /* Do `exp = require_complete_type (exp);' to make sure exp
85 does not have an incomplete type. (That includes void types.) */
86
87 tree
88 require_complete_type (value)
89 tree value;
90 {
91 tree type;
92
93 if (processing_template_decl)
94 return value;
95
96 type = TREE_TYPE (value);
97
98 /* First, detect a valid value with a complete type. */
99 if (TYPE_SIZE (type) != 0
100 && type != void_type_node
101 && ! (TYPE_LANG_SPECIFIC (type)
102 && (IS_SIGNATURE_POINTER (type) || IS_SIGNATURE_REFERENCE (type))
103 && TYPE_SIZE (SIGNATURE_TYPE (type)) == 0))
104 return value;
105
106 /* If we see X::Y, we build an OFFSET_TYPE which has
107 not been laid out. Try to avoid an error by interpreting
108 it as this->X::Y, if reasonable. */
109 if (TREE_CODE (value) == OFFSET_REF
110 && current_class_ref != 0
111 && TREE_OPERAND (value, 0) == current_class_ref)
112 {
113 tree base, member = TREE_OPERAND (value, 1);
114 tree basetype = TYPE_OFFSET_BASETYPE (type);
115 my_friendly_assert (TREE_CODE (member) == FIELD_DECL, 305);
116 base = convert_pointer_to (basetype, current_class_ptr);
117 value = build (COMPONENT_REF, TREE_TYPE (member),
118 build_indirect_ref (base, NULL_PTR), member);
119 return require_complete_type (value);
120 }
121
122 if (IS_AGGR_TYPE (type) && CLASSTYPE_TEMPLATE_INSTANTIATION (type))
123 {
124 instantiate_class_template (TYPE_MAIN_VARIANT (type));
125 if (TYPE_SIZE (type) != 0)
126 return value;
127 }
128
129 incomplete_type_error (value, type);
130 return error_mark_node;
131 }
132
133 tree
134 complete_type (type)
135 tree type;
136 {
137 if (type == error_mark_node || TYPE_SIZE (type) != NULL_TREE)
138 ;
139 else if (TREE_CODE (type) == ARRAY_TYPE && TYPE_DOMAIN (type))
140 {
141 tree t = complete_type (TREE_TYPE (type));
142 if (TYPE_SIZE (t) != NULL_TREE && ! processing_template_decl)
143 layout_type (type);
144 TYPE_NEEDS_CONSTRUCTING (type)
145 = TYPE_NEEDS_CONSTRUCTING (TYPE_MAIN_VARIANT (t));
146 TYPE_NEEDS_DESTRUCTOR (type)
147 = TYPE_NEEDS_DESTRUCTOR (TYPE_MAIN_VARIANT (t));
148 }
149 else if (IS_AGGR_TYPE (type) && CLASSTYPE_TEMPLATE_INSTANTIATION (type))
150 instantiate_class_template (TYPE_MAIN_VARIANT (type));
151
152 return type;
153 }
154
155 /* Return truthvalue of whether type of EXP is instantiated. */
156
157 int
158 type_unknown_p (exp)
159 tree exp;
160 {
161 return (TREE_CODE (exp) == TREE_LIST
162 || TREE_TYPE (exp) == unknown_type_node
163 || (TREE_CODE (TREE_TYPE (exp)) == OFFSET_TYPE
164 && TREE_TYPE (TREE_TYPE (exp)) == unknown_type_node));
165 }
166
167 /* Return truthvalue of whether T is function (or pfn) type. */
168
169 int
170 fntype_p (t)
171 tree t;
172 {
173 return (TREE_CODE (t) == FUNCTION_TYPE || TREE_CODE (t) == METHOD_TYPE
174 || (TREE_CODE (t) == POINTER_TYPE
175 && (TREE_CODE (TREE_TYPE (t)) == FUNCTION_TYPE
176 || TREE_CODE (TREE_TYPE (t)) == METHOD_TYPE)));
177 }
178
179 /* Do `exp = require_instantiated_type (type, exp);' to make sure EXP
180 does not have an uninstantiated type.
181 TYPE is type to instantiate with, if uninstantiated. */
182
183 tree
184 require_instantiated_type (type, exp, errval)
185 tree type, exp, errval;
186 {
187 if (TREE_TYPE (exp) == NULL_TREE)
188 {
189 error ("argument list may not have an initializer list");
190 return errval;
191 }
192
193 if (TREE_TYPE (exp) == unknown_type_node
194 || (TREE_CODE (TREE_TYPE (exp)) == OFFSET_TYPE
195 && TREE_TYPE (TREE_TYPE (exp)) == unknown_type_node))
196 {
197 exp = instantiate_type (type, exp, 1);
198 if (TREE_TYPE (exp) == error_mark_node)
199 return errval;
200 }
201 return exp;
202 }
203
204 /* Return a variant of TYPE which has all the type qualifiers of LIKE
205 as well as those of TYPE. */
206
207 static tree
208 qualify_type (type, like)
209 tree type, like;
210 {
211 int constflag = TYPE_READONLY (type) || TYPE_READONLY (like);
212 int volflag = TYPE_VOLATILE (type) || TYPE_VOLATILE (like);
213 /* @@ Must do member pointers here. */
214 return cp_build_type_variant (type, constflag, volflag);
215 }
216 \f
217 /* Return the common type of two parameter lists.
218 We assume that comptypes has already been done and returned 1;
219 if that isn't so, this may crash.
220
221 As an optimization, free the space we allocate if the parameter
222 lists are already common. */
223
224 tree
225 commonparms (p1, p2)
226 tree p1, p2;
227 {
228 tree oldargs = p1, newargs, n;
229 int i, len;
230 int any_change = 0;
231 char *first_obj = (char *) oballoc (0);
232
233 len = list_length (p1);
234 newargs = tree_last (p1);
235
236 if (newargs == void_list_node)
237 i = 1;
238 else
239 {
240 i = 0;
241 newargs = 0;
242 }
243
244 for (; i < len; i++)
245 newargs = tree_cons (NULL_TREE, NULL_TREE, newargs);
246
247 n = newargs;
248
249 for (i = 0; p1;
250 p1 = TREE_CHAIN (p1), p2 = TREE_CHAIN (p2), n = TREE_CHAIN (n), i++)
251 {
252 if (TREE_PURPOSE (p1) && !TREE_PURPOSE (p2))
253 {
254 TREE_PURPOSE (n) = TREE_PURPOSE (p1);
255 any_change = 1;
256 }
257 else if (! TREE_PURPOSE (p1))
258 {
259 if (TREE_PURPOSE (p2))
260 {
261 TREE_PURPOSE (n) = TREE_PURPOSE (p2);
262 any_change = 1;
263 }
264 }
265 else
266 {
267 if (1 != simple_cst_equal (TREE_PURPOSE (p1), TREE_PURPOSE (p2)))
268 any_change = 1;
269 TREE_PURPOSE (n) = TREE_PURPOSE (p2);
270 }
271 if (TREE_VALUE (p1) != TREE_VALUE (p2))
272 {
273 any_change = 1;
274 TREE_VALUE (n) = common_type (TREE_VALUE (p1), TREE_VALUE (p2));
275 }
276 else
277 TREE_VALUE (n) = TREE_VALUE (p1);
278 }
279 if (! any_change)
280 {
281 obfree (first_obj);
282 return oldargs;
283 }
284
285 return newargs;
286 }
287
288 /* Return the common type of two types.
289 We assume that comptypes has already been done and returned 1;
290 if that isn't so, this may crash.
291
292 This is the type for the result of most arithmetic operations
293 if the operands have the given two types.
294
295 We do not deal with enumeral types here because they have already been
296 converted to integer types. */
297
298 tree
299 common_type (t1, t2)
300 tree t1, t2;
301 {
302 register enum tree_code code1;
303 register enum tree_code code2;
304 tree attributes;
305
306 /* Save time if the two types are the same. */
307
308 if (t1 == t2) return t1;
309
310 /* If one type is nonsense, use the other. */
311 if (t1 == error_mark_node)
312 return t2;
313 if (t2 == error_mark_node)
314 return t1;
315
316 /* Merge the attributes */
317
318 { register tree a1, a2;
319 a1 = TYPE_ATTRIBUTES (t1);
320 a2 = TYPE_ATTRIBUTES (t2);
321
322 /* Either one unset? Take the set one. */
323
324 if (!(attributes = a1))
325 attributes = a2;
326
327 /* One that completely contains the other? Take it. */
328
329 else if (a2 && !attribute_list_contained (a1, a2))
330 if (attribute_list_contained (a2, a1))
331 attributes = a2;
332 else
333 {
334 /* Pick the longest list, and hang on the other list. */
335 /* ??? For the moment we punt on the issue of attrs with args. */
336
337 if (list_length (a1) < list_length (a2))
338 attributes = a2, a2 = a1;
339
340 for (; a2; a2 = TREE_CHAIN (a2))
341 if (lookup_attribute (IDENTIFIER_POINTER (TREE_PURPOSE (a2)),
342 attributes) == NULL_TREE)
343 {
344 a1 = copy_node (a2);
345 TREE_CHAIN (a1) = attributes;
346 attributes = a1;
347 }
348 }
349 }
350
351 /* Treat an enum type as the unsigned integer type of the same width. */
352
353 if (TREE_CODE (t1) == ENUMERAL_TYPE)
354 t1 = type_for_size (TYPE_PRECISION (t1), 1);
355 if (TREE_CODE (t2) == ENUMERAL_TYPE)
356 t2 = type_for_size (TYPE_PRECISION (t2), 1);
357
358 if (TYPE_PTRMEMFUNC_P (t1))
359 t1 = TYPE_PTRMEMFUNC_FN_TYPE (t1);
360 if (TYPE_PTRMEMFUNC_P (t2))
361 t2 = TYPE_PTRMEMFUNC_FN_TYPE (t2);
362
363 code1 = TREE_CODE (t1);
364 code2 = TREE_CODE (t2);
365
366 /* If one type is complex, form the common type of the non-complex
367 components, then make that complex. Use T1 or T2 if it is the
368 required type. */
369 if (code1 == COMPLEX_TYPE || code2 == COMPLEX_TYPE)
370 {
371 tree subtype1 = code1 == COMPLEX_TYPE ? TREE_TYPE (t1) : t1;
372 tree subtype2 = code2 == COMPLEX_TYPE ? TREE_TYPE (t2) : t2;
373 tree subtype = common_type (subtype1, subtype2);
374
375 if (code1 == COMPLEX_TYPE && TREE_TYPE (t1) == subtype)
376 return build_type_attribute_variant (t1, attributes);
377 else if (code2 == COMPLEX_TYPE && TREE_TYPE (t2) == subtype)
378 return build_type_attribute_variant (t2, attributes);
379 else
380 return build_type_attribute_variant (build_complex_type (subtype),
381 attributes);
382 }
383
384 switch (code1)
385 {
386 case INTEGER_TYPE:
387 case REAL_TYPE:
388 /* If only one is real, use it as the result. */
389
390 if (code1 == REAL_TYPE && code2 != REAL_TYPE)
391 return build_type_attribute_variant (t1, attributes);
392
393 if (code2 == REAL_TYPE && code1 != REAL_TYPE)
394 return build_type_attribute_variant (t2, attributes);
395
396 /* Both real or both integers; use the one with greater precision. */
397
398 if (TYPE_PRECISION (t1) > TYPE_PRECISION (t2))
399 return build_type_attribute_variant (t1, attributes);
400 else if (TYPE_PRECISION (t2) > TYPE_PRECISION (t1))
401 return build_type_attribute_variant (t2, attributes);
402
403 /* Same precision. Prefer longs to ints even when same size. */
404
405 if (TYPE_MAIN_VARIANT (t1) == long_unsigned_type_node
406 || TYPE_MAIN_VARIANT (t2) == long_unsigned_type_node)
407 return build_type_attribute_variant (long_unsigned_type_node,
408 attributes);
409
410 if (TYPE_MAIN_VARIANT (t1) == long_integer_type_node
411 || TYPE_MAIN_VARIANT (t2) == long_integer_type_node)
412 {
413 /* But preserve unsignedness from the other type,
414 since long cannot hold all the values of an unsigned int. */
415 if (TREE_UNSIGNED (t1) || TREE_UNSIGNED (t2))
416 t1 = long_unsigned_type_node;
417 else
418 t1 = long_integer_type_node;
419 return build_type_attribute_variant (t1, attributes);
420 }
421
422 if (TYPE_MAIN_VARIANT (t1) == long_double_type_node
423 || TYPE_MAIN_VARIANT (t2) == long_double_type_node)
424 return build_type_attribute_variant (long_double_type_node,
425 attributes);
426
427 /* Otherwise prefer the unsigned one. */
428
429 if (TREE_UNSIGNED (t1))
430 return build_type_attribute_variant (t1, attributes);
431 else
432 return build_type_attribute_variant (t2, attributes);
433
434 case POINTER_TYPE:
435 case REFERENCE_TYPE:
436 /* For two pointers, do this recursively on the target type,
437 and combine the qualifiers of the two types' targets. */
438 /* This code was turned off; I don't know why.
439 But ANSI C++ specifies doing this with the qualifiers.
440 So I turned it on again. */
441 {
442 tree tt1 = TYPE_MAIN_VARIANT (TREE_TYPE (t1));
443 tree tt2 = TYPE_MAIN_VARIANT (TREE_TYPE (t2));
444 int constp
445 = TYPE_READONLY (TREE_TYPE (t1)) || TYPE_READONLY (TREE_TYPE (t2));
446 int volatilep
447 = TYPE_VOLATILE (TREE_TYPE (t1)) || TYPE_VOLATILE (TREE_TYPE (t2));
448 tree target;
449
450 if (tt1 == tt2)
451 target = tt1;
452 else if (tt1 == void_type_node || tt2 == void_type_node)
453 target = void_type_node;
454 else if (tt1 == unknown_type_node)
455 target = tt2;
456 else if (tt2 == unknown_type_node)
457 target = tt1;
458 else
459 target = common_type (tt1, tt2);
460
461 target = cp_build_type_variant (target, constp, volatilep);
462 if (code1 == POINTER_TYPE)
463 t1 = build_pointer_type (target);
464 else
465 t1 = build_reference_type (target);
466 t1 = build_type_attribute_variant (t1, attributes);
467
468 if (TREE_CODE (target) == METHOD_TYPE)
469 t1 = build_ptrmemfunc_type (t1);
470
471 return t1;
472 }
473
474 case ARRAY_TYPE:
475 {
476 tree elt = common_type (TREE_TYPE (t1), TREE_TYPE (t2));
477 /* Save space: see if the result is identical to one of the args. */
478 if (elt == TREE_TYPE (t1) && TYPE_DOMAIN (t1))
479 return build_type_attribute_variant (t1, attributes);
480 if (elt == TREE_TYPE (t2) && TYPE_DOMAIN (t2))
481 return build_type_attribute_variant (t2, attributes);
482 /* Merge the element types, and have a size if either arg has one. */
483 t1 = build_cplus_array_type
484 (elt, TYPE_DOMAIN (TYPE_DOMAIN (t1) ? t1 : t2));
485 return build_type_attribute_variant (t1, attributes);
486 }
487
488 case FUNCTION_TYPE:
489 /* Function types: prefer the one that specified arg types.
490 If both do, merge the arg types. Also merge the return types. */
491 {
492 tree valtype = common_type (TREE_TYPE (t1), TREE_TYPE (t2));
493 tree p1 = TYPE_ARG_TYPES (t1);
494 tree p2 = TYPE_ARG_TYPES (t2);
495 tree rval, raises;
496
497 /* Save space: see if the result is identical to one of the args. */
498 if (valtype == TREE_TYPE (t1) && ! p2)
499 return build_type_attribute_variant (t1, attributes);
500 if (valtype == TREE_TYPE (t2) && ! p1)
501 return build_type_attribute_variant (t2, attributes);
502
503 /* Simple way if one arg fails to specify argument types. */
504 if (p1 == NULL_TREE || TREE_VALUE (p1) == void_type_node)
505 {
506 rval = build_function_type (valtype, p2);
507 if ((raises = TYPE_RAISES_EXCEPTIONS (t2)))
508 rval = build_exception_variant (rval, raises);
509 return build_type_attribute_variant (rval, attributes);
510 }
511 raises = TYPE_RAISES_EXCEPTIONS (t1);
512 if (p2 == NULL_TREE || TREE_VALUE (p2) == void_type_node)
513 {
514 rval = build_function_type (valtype, p1);
515 if (raises)
516 rval = build_exception_variant (rval, raises);
517 return build_type_attribute_variant (rval, attributes);
518 }
519
520 rval = build_function_type (valtype, commonparms (p1, p2));
521 rval = build_exception_variant (rval, raises);
522 return build_type_attribute_variant (rval, attributes);
523 }
524
525 case RECORD_TYPE:
526 case UNION_TYPE:
527 my_friendly_assert (TYPE_MAIN_VARIANT (t1) == t1
528 && TYPE_MAIN_VARIANT (t2) == t2, 306);
529
530 if (DERIVED_FROM_P (t1, t2) && binfo_or_else (t1, t2))
531 return build_type_attribute_variant (t1, attributes);
532 else if (binfo_or_else (t2, t1))
533 return build_type_attribute_variant (t2, attributes);
534 else
535 compiler_error ("common_type called with uncommon aggregate types");
536
537 case METHOD_TYPE:
538 if (TREE_CODE (TREE_TYPE (t1)) == TREE_CODE (TREE_TYPE (t2)))
539 {
540 /* Get this value the long way, since TYPE_METHOD_BASETYPE
541 is just the main variant of this. */
542 tree basetype;
543 tree raises, t3;
544
545 tree b1 = TYPE_OFFSET_BASETYPE (t1);
546 tree b2 = TYPE_OFFSET_BASETYPE (t2);
547
548 if (comptypes (b1, b2, 1)
549 || (DERIVED_FROM_P (b1, b2) && binfo_or_else (b1, b2)))
550 basetype = TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (t2)));
551 else
552 {
553 if (binfo_or_else (b2, b1) == NULL_TREE)
554 compiler_error ("common_type called with uncommon method types");
555 basetype = TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (t1)));
556 }
557
558 raises = TYPE_RAISES_EXCEPTIONS (t1);
559
560 /* If this was a member function type, get back to the
561 original type of type member function (i.e., without
562 the class instance variable up front. */
563 t1 = build_function_type (TREE_TYPE (t1), TREE_CHAIN (TYPE_ARG_TYPES (t1)));
564 t2 = build_function_type (TREE_TYPE (t2), TREE_CHAIN (TYPE_ARG_TYPES (t2)));
565 t3 = common_type (t1, t2);
566 t3 = build_cplus_method_type (basetype, TREE_TYPE (t3), TYPE_ARG_TYPES (t3));
567 t1 = build_exception_variant (t3, raises);
568 }
569 else
570 compiler_error ("common_type called with uncommon method types");
571
572 return build_type_attribute_variant (t1, attributes);
573
574 case OFFSET_TYPE:
575 if (TREE_TYPE (t1) == TREE_TYPE (t2))
576 {
577 tree b1 = TYPE_OFFSET_BASETYPE (t1);
578 tree b2 = TYPE_OFFSET_BASETYPE (t2);
579
580 if (comptypes (b1, b2, 1)
581 || (DERIVED_FROM_P (b1, b2) && binfo_or_else (b1, b2)))
582 return build_type_attribute_variant (t2, attributes);
583 else if (binfo_or_else (b2, b1))
584 return build_type_attribute_variant (t1, attributes);
585 }
586 compiler_error ("common_type called with uncommon member types");
587
588 default:
589 return build_type_attribute_variant (t1, attributes);
590 }
591 }
592 \f
593 /* Return 1 if TYPE1 and TYPE2 raise the same exceptions. */
594
595 int
596 compexcepttypes (t1, t2)
597 tree t1, t2;
598 {
599 return TYPE_RAISES_EXCEPTIONS (t1) == TYPE_RAISES_EXCEPTIONS (t2);
600 }
601
602 static int
603 comp_array_types (cmp, t1, t2, strict)
604 register int (*cmp) PROTO((tree, tree, int));
605 tree t1, t2;
606 int strict;
607 {
608 tree d1 = TYPE_DOMAIN (t1);
609 tree d2 = TYPE_DOMAIN (t2);
610
611 /* Target types must match incl. qualifiers. */
612 if (!(TREE_TYPE (t1) == TREE_TYPE (t2)
613 || (*cmp) (TREE_TYPE (t1), TREE_TYPE (t2), strict)))
614 return 0;
615
616 /* Sizes must match unless one is missing or variable. */
617 if (d1 == 0 || d2 == 0 || d1 == d2
618 || TREE_CODE (TYPE_MIN_VALUE (d1)) != INTEGER_CST
619 || TREE_CODE (TYPE_MIN_VALUE (d2)) != INTEGER_CST
620 || TREE_CODE (TYPE_MAX_VALUE (d1)) != INTEGER_CST
621 || TREE_CODE (TYPE_MAX_VALUE (d2)) != INTEGER_CST)
622 return 1;
623
624 return ((TREE_INT_CST_LOW (TYPE_MIN_VALUE (d1))
625 == TREE_INT_CST_LOW (TYPE_MIN_VALUE (d2)))
626 && (TREE_INT_CST_HIGH (TYPE_MIN_VALUE (d1))
627 == TREE_INT_CST_HIGH (TYPE_MIN_VALUE (d2)))
628 && (TREE_INT_CST_LOW (TYPE_MAX_VALUE (d1))
629 == TREE_INT_CST_LOW (TYPE_MAX_VALUE (d2)))
630 && (TREE_INT_CST_HIGH (TYPE_MAX_VALUE (d1))
631 == TREE_INT_CST_HIGH (TYPE_MAX_VALUE (d2))));
632 }
633
634 /* Return 1 if TYPE1 and TYPE2 are compatible types for assignment
635 or various other operations. This is what ANSI C++ speaks of as
636 "being the same".
637
638 For C++: argument STRICT says we should be strict about this
639 comparison:
640
641 2 : strict, except that if one type is a reference and
642 the other is not, compare the target type of the
643 reference to the type that's not a reference (ARM, p308).
644 This is used for checking for invalid overloading.
645 1 : strict (compared according to ANSI C)
646 This is used for checking whether two function decls match.
647 0 : <= (compared according to C++)
648 -1: <= or >= (relaxed)
649
650 Otherwise, pointers involving base classes and derived classes
651 can be mixed as valid: i.e. a pointer to a base class may be assigned
652 to a pointer to one of its derived classes, as per C++. A pointer to
653 a derived class may be passed as a parameter to a function expecting a
654 pointer to a base classes. These allowances do not commute. In this
655 case, TYPE1 is assumed to be the base class, and TYPE2 is assumed to
656 be the derived class. */
657
658 int
659 comptypes (type1, type2, strict)
660 tree type1, type2;
661 int strict;
662 {
663 register tree t1 = type1;
664 register tree t2 = type2;
665 int attrval, val;
666
667 /* Suppress errors caused by previously reported errors */
668
669 if (t1 == t2)
670 return 1;
671
672 /* This should never happen. */
673 my_friendly_assert (t1 != error_mark_node, 307);
674
675 if (t2 == error_mark_node)
676 return 0;
677
678 if (strict < 0)
679 {
680 /* Treat an enum type as the unsigned integer type of the same width. */
681
682 if (TREE_CODE (t1) == ENUMERAL_TYPE)
683 t1 = type_for_size (TYPE_PRECISION (t1), 1);
684 if (TREE_CODE (t2) == ENUMERAL_TYPE)
685 t2 = type_for_size (TYPE_PRECISION (t2), 1);
686
687 if (t1 == t2)
688 return 1;
689 }
690
691 if (TYPE_PTRMEMFUNC_P (t1))
692 t1 = TYPE_PTRMEMFUNC_FN_TYPE (t1);
693 if (TYPE_PTRMEMFUNC_P (t2))
694 t2 = TYPE_PTRMEMFUNC_FN_TYPE (t2);
695
696 /* Different classes of types can't be compatible. */
697
698 if (TREE_CODE (t1) != TREE_CODE (t2))
699 {
700 if (strict == 2
701 && ((TREE_CODE (t1) == REFERENCE_TYPE)
702 ^ (TREE_CODE (t2) == REFERENCE_TYPE)))
703 {
704 if (TREE_CODE (t1) == REFERENCE_TYPE)
705 return comptypes (TREE_TYPE (t1), t2, 1);
706 return comptypes (t1, TREE_TYPE (t2), 1);
707 }
708
709 return 0;
710 }
711 if (strict > 1)
712 strict = 1;
713
714 /* Qualifiers must match. */
715
716 if (TYPE_READONLY (t1) != TYPE_READONLY (t2))
717 return 0;
718 if (TYPE_VOLATILE (t1) != TYPE_VOLATILE (t2))
719 return 0;
720
721 /* Allow for two different type nodes which have essentially the same
722 definition. Note that we already checked for equality of the type
723 type qualifiers (just above). */
724
725 if (TYPE_MAIN_VARIANT (t1) == TYPE_MAIN_VARIANT (t2))
726 return 1;
727
728 /* ??? COMP_TYPE_ATTRIBUTES is currently useless for variables as each
729 attribute is its own main variant (`val' will remain 0). */
730 #ifndef COMP_TYPE_ATTRIBUTES
731 #define COMP_TYPE_ATTRIBUTES(t1,t2) 1
732 #endif
733
734 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
735 if (! (attrval = COMP_TYPE_ATTRIBUTES (t1, t2)))
736 return 0;
737
738 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
739 val = 0;
740
741 switch (TREE_CODE (t1))
742 {
743 case RECORD_TYPE:
744 case UNION_TYPE:
745 if (CLASSTYPE_TEMPLATE_INFO (t1) && CLASSTYPE_TEMPLATE_INFO (t2)
746 && CLASSTYPE_TI_TEMPLATE (t1) == CLASSTYPE_TI_TEMPLATE (t2))
747 {
748 int i = TREE_VEC_LENGTH (CLASSTYPE_TI_ARGS (t1));
749 tree *p1 = &TREE_VEC_ELT (CLASSTYPE_TI_ARGS (t1), 0);
750 tree *p2 = &TREE_VEC_ELT (CLASSTYPE_TI_ARGS (t2), 0);
751
752 while (i--)
753 {
754 if (TREE_CODE_CLASS (TREE_CODE (p1[i])) == 't')
755 {
756 if (! comptypes (p1[i], p2[i], 1))
757 return 0;
758 }
759 else
760 {
761 if (simple_cst_equal (p1[i], p2[i]) <= 0)
762 return 0;
763 }
764 }
765 return 1;
766 }
767 if (strict <= 0)
768 goto look_hard;
769 return 0;
770
771 case OFFSET_TYPE:
772 val = (comptypes (build_pointer_type (TYPE_OFFSET_BASETYPE (t1)),
773 build_pointer_type (TYPE_OFFSET_BASETYPE (t2)), strict)
774 && comptypes (TREE_TYPE (t1), TREE_TYPE (t2), strict));
775 break;
776
777 case METHOD_TYPE:
778 if (! compexcepttypes (t1, t2))
779 return 0;
780
781 /* This case is anti-symmetrical!
782 One can pass a base member (or member function)
783 to something expecting a derived member (or member function),
784 but not vice-versa! */
785
786 val = (comptypes (TREE_TYPE (t1), TREE_TYPE (t2), strict)
787 && compparms (TYPE_ARG_TYPES (t1),
788 TYPE_ARG_TYPES (t2), strict));
789 break;
790
791 case POINTER_TYPE:
792 case REFERENCE_TYPE:
793 t1 = TREE_TYPE (t1);
794 t2 = TREE_TYPE (t2);
795 if (t1 == t2)
796 {
797 val = 1;
798 break;
799 }
800 if (strict <= 0)
801 {
802 if (TREE_CODE (t1) == RECORD_TYPE && TREE_CODE (t2) == RECORD_TYPE)
803 {
804 int rval;
805 look_hard:
806 rval = t1 == t2 || DERIVED_FROM_P (t1, t2);
807
808 if (rval)
809 {
810 val = 1;
811 break;
812 }
813 if (strict < 0)
814 {
815 val = DERIVED_FROM_P (t2, t1);
816 break;
817 }
818 }
819 return 0;
820 }
821 else
822 val = comptypes (t1, t2, strict);
823 break;
824
825 case FUNCTION_TYPE:
826 if (! compexcepttypes (t1, t2))
827 return 0;
828
829 val = ((TREE_TYPE (t1) == TREE_TYPE (t2)
830 || comptypes (TREE_TYPE (t1), TREE_TYPE (t2), strict))
831 && compparms (TYPE_ARG_TYPES (t1), TYPE_ARG_TYPES (t2), strict));
832 break;
833
834 case ARRAY_TYPE:
835 /* Target types must match incl. qualifiers. */
836 val = comp_array_types (comptypes, t1, t2, strict);
837 break;
838
839 case TEMPLATE_TYPE_PARM:
840 return TEMPLATE_TYPE_IDX (t1) == TEMPLATE_TYPE_IDX (t2)
841 && TEMPLATE_TYPE_LEVEL (t1) == TEMPLATE_TYPE_LEVEL (t2);
842
843 case TYPENAME_TYPE:
844 if (TYPE_IDENTIFIER (t1) != TYPE_IDENTIFIER (t2))
845 return 0;
846 return comptypes (TYPE_CONTEXT (t1), TYPE_CONTEXT (t2), 1);
847 }
848 return attrval == 2 && val == 1 ? 2 : val;
849 }
850
851 /* Return 1 if TTL and TTR are pointers to types that are equivalent,
852 ignoring their qualifiers.
853
854 NPTRS is the number of pointers we can strip off and keep cool.
855 This is used to permit (for aggr A, aggr B) A, B* to convert to A*,
856 but to not permit B** to convert to A**. */
857
858 int
859 comp_target_types (ttl, ttr, nptrs)
860 tree ttl, ttr;
861 int nptrs;
862 {
863 ttl = TYPE_MAIN_VARIANT (ttl);
864 ttr = TYPE_MAIN_VARIANT (ttr);
865 if (ttl == ttr)
866 return 1;
867
868 if (TREE_CODE (ttr) != TREE_CODE (ttl))
869 return 0;
870
871 if (TREE_CODE (ttr) == POINTER_TYPE)
872 {
873 ttl = TREE_TYPE (ttl);
874 ttr = TREE_TYPE (ttr);
875
876 if (nptrs > 0)
877 {
878 if (TREE_CODE (ttl) == UNKNOWN_TYPE
879 || TREE_CODE (ttr) == UNKNOWN_TYPE)
880 return 1;
881 else if (TREE_CODE (ttl) == VOID_TYPE
882 && TREE_CODE (ttr) != FUNCTION_TYPE
883 && TREE_CODE (ttr) != METHOD_TYPE
884 && TREE_CODE (ttr) != OFFSET_TYPE)
885 return 1;
886 else if (TREE_CODE (ttr) == VOID_TYPE
887 && TREE_CODE (ttl) != FUNCTION_TYPE
888 && TREE_CODE (ttl) != METHOD_TYPE
889 && TREE_CODE (ttl) != OFFSET_TYPE)
890 return -1;
891 else if (TREE_CODE (ttl) == POINTER_TYPE
892 || TREE_CODE (ttl) == ARRAY_TYPE)
893 {
894 if (comp_ptr_ttypes (ttl, ttr))
895 return 1;
896 else if (comp_ptr_ttypes (ttr, ttl))
897 return -1;
898 return 0;
899 }
900 }
901
902 /* Const and volatile mean something different for function types,
903 so the usual checks are not appropriate. */
904 if (TREE_CODE (ttl) == FUNCTION_TYPE || TREE_CODE (ttl) == METHOD_TYPE)
905 return comp_target_types (ttl, ttr, nptrs - 1);
906
907 /* Make sure that the cv-quals change only in the same direction as
908 the target type. */
909 {
910 int t;
911 int c = TYPE_READONLY (ttl) - TYPE_READONLY (ttr);
912 int v = TYPE_VOLATILE (ttl) - TYPE_VOLATILE (ttr);
913
914 if ((c > 0 && v < 0) || (c < 0 && v > 0))
915 return 0;
916
917 if (TYPE_MAIN_VARIANT (ttl) == TYPE_MAIN_VARIANT (ttr))
918 return (c + v < 0) ? -1 : 1;
919
920 t = comp_target_types (ttl, ttr, nptrs - 1);
921 if ((t == 1 && c + v >= 0) || (t == -1 && c + v <= 0))
922 return t;
923
924 return 0;
925 }
926 }
927
928 if (TREE_CODE (ttr) == REFERENCE_TYPE)
929 return comp_target_types (TREE_TYPE (ttl), TREE_TYPE (ttr), nptrs);
930 if (TREE_CODE (ttr) == ARRAY_TYPE)
931 return comp_array_types (comp_target_types, ttl, ttr, 0);
932 else if (TREE_CODE (ttr) == FUNCTION_TYPE || TREE_CODE (ttr) == METHOD_TYPE)
933 if (comp_target_types (TREE_TYPE (ttl), TREE_TYPE (ttr), nptrs))
934 switch (comp_target_parms (TYPE_ARG_TYPES (ttl), TYPE_ARG_TYPES (ttr), 1))
935 {
936 case 0:
937 return 0;
938 case 1:
939 return 1;
940 case 2:
941 return -1;
942 default:
943 my_friendly_abort (112);
944 }
945 else
946 return 0;
947
948 /* for C++ */
949 else if (TREE_CODE (ttr) == OFFSET_TYPE)
950 {
951 /* Contravariance: we can assign a pointer to base member to a pointer
952 to derived member. Note difference from simple pointer case, where
953 we can pass a pointer to derived to a pointer to base. */
954 if (comptypes (TYPE_OFFSET_BASETYPE (ttr), TYPE_OFFSET_BASETYPE (ttl), 0))
955 return comp_target_types (TREE_TYPE (ttl), TREE_TYPE (ttr), nptrs);
956 else if (comptypes (TYPE_OFFSET_BASETYPE (ttl), TYPE_OFFSET_BASETYPE (ttr), 0)
957 && comp_target_types (TREE_TYPE (ttl), TREE_TYPE (ttr), nptrs))
958 return -1;
959 }
960 else if (IS_AGGR_TYPE (ttl))
961 {
962 if (nptrs < 0)
963 return 0;
964 if (comptypes (build_pointer_type (ttl), build_pointer_type (ttr), 0))
965 return 1;
966 if (comptypes (build_pointer_type (ttr), build_pointer_type (ttl), 0))
967 return -1;
968 return 0;
969 }
970
971 return 0;
972 }
973
974 /* If two types share a common base type, return that basetype.
975 If there is not a unique most-derived base type, this function
976 returns ERROR_MARK_NODE. */
977
978 static tree
979 common_base_type (tt1, tt2)
980 tree tt1, tt2;
981 {
982 tree best = NULL_TREE;
983 int i;
984
985 /* If one is a baseclass of another, that's good enough. */
986 if (UNIQUELY_DERIVED_FROM_P (tt1, tt2))
987 return tt1;
988 if (UNIQUELY_DERIVED_FROM_P (tt2, tt1))
989 return tt2;
990
991 /* Otherwise, try to find a unique baseclass of TT1
992 that is shared by TT2, and follow that down. */
993 for (i = CLASSTYPE_N_BASECLASSES (tt1)-1; i >= 0; i--)
994 {
995 tree basetype = TYPE_BINFO_BASETYPE (tt1, i);
996 tree trial = common_base_type (basetype, tt2);
997 if (trial)
998 {
999 if (trial == error_mark_node)
1000 return trial;
1001 if (best == NULL_TREE)
1002 best = trial;
1003 else if (best != trial)
1004 return error_mark_node;
1005 }
1006 }
1007
1008 /* Same for TT2. */
1009 for (i = CLASSTYPE_N_BASECLASSES (tt2)-1; i >= 0; i--)
1010 {
1011 tree basetype = TYPE_BINFO_BASETYPE (tt2, i);
1012 tree trial = common_base_type (tt1, basetype);
1013 if (trial)
1014 {
1015 if (trial == error_mark_node)
1016 return trial;
1017 if (best == NULL_TREE)
1018 best = trial;
1019 else if (best != trial)
1020 return error_mark_node;
1021 }
1022 }
1023 return best;
1024 }
1025 \f
1026 /* Subroutines of `comptypes'. */
1027
1028 /* Return 1 if two parameter type lists PARMS1 and PARMS2
1029 are equivalent in the sense that functions with those parameter types
1030 can have equivalent types.
1031 If either list is empty, we win.
1032 Otherwise, the two lists must be equivalent, element by element.
1033
1034 C++: See comment above about TYPE1, TYPE2, STRICT.
1035 If STRICT == 3, it means checking is strict, but do not compare
1036 default parameter values. */
1037
1038 int
1039 compparms (parms1, parms2, strict)
1040 tree parms1, parms2;
1041 int strict;
1042 {
1043 register tree t1 = parms1, t2 = parms2;
1044
1045 /* An unspecified parmlist matches any specified parmlist
1046 whose argument types don't need default promotions. */
1047
1048 if (strict <= 0 && t1 == 0)
1049 return self_promoting_args_p (t2);
1050 if (strict < 0 && t2 == 0)
1051 return self_promoting_args_p (t1);
1052
1053 while (1)
1054 {
1055 if (t1 == 0 && t2 == 0)
1056 return 1;
1057 /* If one parmlist is shorter than the other,
1058 they fail to match, unless STRICT is <= 0. */
1059 if (t1 == 0 || t2 == 0)
1060 {
1061 if (strict > 0)
1062 return 0;
1063 if (strict < 0)
1064 return 1;
1065 if (strict == 0)
1066 return t1 && TREE_PURPOSE (t1);
1067 }
1068 if (! comptypes (TREE_VALUE (t2), TREE_VALUE (t1), strict))
1069 {
1070 if (strict > 0)
1071 return 0;
1072 if (strict == 0)
1073 return t2 == void_list_node && TREE_PURPOSE (t1);
1074 return TREE_PURPOSE (t1) || TREE_PURPOSE (t2);
1075 }
1076
1077 t1 = TREE_CHAIN (t1);
1078 t2 = TREE_CHAIN (t2);
1079 }
1080 }
1081
1082 /* This really wants return whether or not parameter type lists
1083 would make their owning functions assignment compatible or not. */
1084
1085 static int
1086 comp_target_parms (parms1, parms2, strict)
1087 tree parms1, parms2;
1088 int strict;
1089 {
1090 register tree t1 = parms1, t2 = parms2;
1091 int warn_contravariance = 0;
1092
1093 /* An unspecified parmlist matches any specified parmlist
1094 whose argument types don't need default promotions.
1095 @@@ see 13.3.3 for a counterexample... */
1096
1097 if (t1 == 0 && t2 != 0)
1098 {
1099 cp_pedwarn ("ANSI C++ prohibits conversion from `(%#T)' to `(...)'",
1100 parms2);
1101 return self_promoting_args_p (t2);
1102 }
1103 if (t2 == 0)
1104 return self_promoting_args_p (t1);
1105
1106 for (; t1 || t2; t1 = TREE_CHAIN (t1), t2 = TREE_CHAIN (t2))
1107 {
1108 tree p1, p2;
1109
1110 /* If one parmlist is shorter than the other,
1111 they fail to match, unless STRICT is <= 0. */
1112 if (t1 == 0 || t2 == 0)
1113 {
1114 if (strict > 0)
1115 return 0;
1116 if (strict < 0)
1117 return 1 + warn_contravariance;
1118 return ((t1 && TREE_PURPOSE (t1)) + warn_contravariance);
1119 }
1120 p1 = TREE_VALUE (t1);
1121 p2 = TREE_VALUE (t2);
1122 if (p1 == p2)
1123 continue;
1124
1125 if ((TREE_CODE (p1) == POINTER_TYPE && TREE_CODE (p2) == POINTER_TYPE)
1126 || (TREE_CODE (p1) == REFERENCE_TYPE && TREE_CODE (p2) == REFERENCE_TYPE))
1127 {
1128 if (strict <= 0
1129 && (TYPE_MAIN_VARIANT (TREE_TYPE (p1))
1130 == TYPE_MAIN_VARIANT (TREE_TYPE (p2))))
1131 continue;
1132
1133 /* The following is wrong for contravariance,
1134 but many programs depend on it. */
1135 if (TREE_TYPE (p1) == void_type_node)
1136 continue;
1137 if (TREE_TYPE (p2) == void_type_node)
1138 {
1139 warn_contravariance = 1;
1140 continue;
1141 }
1142 if (IS_AGGR_TYPE (TREE_TYPE (p1)))
1143 {
1144 if (comptypes (p2, p1, 0) == 0)
1145 {
1146 if (comptypes (p1, p2, 0) != 0)
1147 warn_contravariance = 1;
1148 else
1149 return 0;
1150 }
1151 continue;
1152 }
1153 }
1154 /* Note backwards order due to contravariance. */
1155 if (comp_target_types (p2, p1, 1) == 0)
1156 {
1157 if (comp_target_types (p1, p2, 1))
1158 {
1159 warn_contravariance = 1;
1160 continue;
1161 }
1162 if (strict != 0)
1163 return 0;
1164 }
1165 /* Target types are compatible--just make sure that if
1166 we use parameter lists, that they are ok as well. */
1167 if (TREE_CODE (p1) == FUNCTION_TYPE || TREE_CODE (p1) == METHOD_TYPE)
1168 switch (comp_target_parms (TYPE_ARG_TYPES (p1),
1169 TYPE_ARG_TYPES (p2),
1170 strict))
1171 {
1172 case 0:
1173 return 0;
1174 case 1:
1175 break;
1176 case 2:
1177 warn_contravariance = 1;
1178 }
1179
1180 if (TREE_PURPOSE (t1) && TREE_PURPOSE (t2))
1181 {
1182 int cmp = simple_cst_equal (TREE_PURPOSE (t1), TREE_PURPOSE (t2));
1183 if (cmp < 0)
1184 my_friendly_abort (114);
1185 if (cmp == 0)
1186 return 0;
1187 }
1188 }
1189 return 1 + warn_contravariance;
1190 }
1191
1192 /* Return 1 if PARMS specifies a fixed number of parameters
1193 and none of their types is affected by default promotions. */
1194
1195 int
1196 self_promoting_args_p (parms)
1197 tree parms;
1198 {
1199 register tree t;
1200 for (t = parms; t; t = TREE_CHAIN (t))
1201 {
1202 register tree type = TREE_VALUE (t);
1203
1204 if (TREE_CHAIN (t) == 0 && type != void_type_node)
1205 return 0;
1206
1207 if (type == 0)
1208 return 0;
1209
1210 if (TYPE_MAIN_VARIANT (type) == float_type_node)
1211 return 0;
1212
1213 if (C_PROMOTING_INTEGER_TYPE_P (type))
1214 return 0;
1215 }
1216 return 1;
1217 }
1218 \f
1219 /* Return an unsigned type the same as TYPE in other respects.
1220
1221 C++: must make these work for type variants as well. */
1222
1223 tree
1224 unsigned_type (type)
1225 tree type;
1226 {
1227 tree type1 = TYPE_MAIN_VARIANT (type);
1228 if (type1 == signed_char_type_node || type1 == char_type_node)
1229 return unsigned_char_type_node;
1230 if (type1 == integer_type_node)
1231 return unsigned_type_node;
1232 if (type1 == short_integer_type_node)
1233 return short_unsigned_type_node;
1234 if (type1 == long_integer_type_node)
1235 return long_unsigned_type_node;
1236 if (type1 == long_long_integer_type_node)
1237 return long_long_unsigned_type_node;
1238 if (type1 == intDI_type_node)
1239 return unsigned_intDI_type_node;
1240 if (type1 == intSI_type_node)
1241 return unsigned_intSI_type_node;
1242 if (type1 == intHI_type_node)
1243 return unsigned_intHI_type_node;
1244 if (type1 == intQI_type_node)
1245 return unsigned_intQI_type_node;
1246
1247 return signed_or_unsigned_type (1, type);
1248 }
1249
1250 /* Return a signed type the same as TYPE in other respects. */
1251
1252 tree
1253 signed_type (type)
1254 tree type;
1255 {
1256 tree type1 = TYPE_MAIN_VARIANT (type);
1257 if (type1 == unsigned_char_type_node || type1 == char_type_node)
1258 return signed_char_type_node;
1259 if (type1 == unsigned_type_node)
1260 return integer_type_node;
1261 if (type1 == short_unsigned_type_node)
1262 return short_integer_type_node;
1263 if (type1 == long_unsigned_type_node)
1264 return long_integer_type_node;
1265 if (type1 == long_long_unsigned_type_node)
1266 return long_long_integer_type_node;
1267 if (type1 == unsigned_intDI_type_node)
1268 return intDI_type_node;
1269 if (type1 == unsigned_intSI_type_node)
1270 return intSI_type_node;
1271 if (type1 == unsigned_intHI_type_node)
1272 return intHI_type_node;
1273 if (type1 == unsigned_intQI_type_node)
1274 return intQI_type_node;
1275
1276 return signed_or_unsigned_type (0, type);
1277 }
1278
1279 /* Return a type the same as TYPE except unsigned or
1280 signed according to UNSIGNEDP. */
1281
1282 tree
1283 signed_or_unsigned_type (unsignedp, type)
1284 int unsignedp;
1285 tree type;
1286 {
1287 if (! INTEGRAL_TYPE_P (type)
1288 || TREE_UNSIGNED (type) == unsignedp)
1289 return type;
1290
1291 if (TYPE_PRECISION (type) == TYPE_PRECISION (signed_char_type_node))
1292 return unsignedp ? unsigned_char_type_node : signed_char_type_node;
1293 if (TYPE_PRECISION (type) == TYPE_PRECISION (integer_type_node))
1294 return unsignedp ? unsigned_type_node : integer_type_node;
1295 if (TYPE_PRECISION (type) == TYPE_PRECISION (short_integer_type_node))
1296 return unsignedp ? short_unsigned_type_node : short_integer_type_node;
1297 if (TYPE_PRECISION (type) == TYPE_PRECISION (long_integer_type_node))
1298 return unsignedp ? long_unsigned_type_node : long_integer_type_node;
1299 if (TYPE_PRECISION (type) == TYPE_PRECISION (long_long_integer_type_node))
1300 return (unsignedp ? long_long_unsigned_type_node
1301 : long_long_integer_type_node);
1302 return type;
1303 }
1304
1305 /* Compute the value of the `sizeof' operator. */
1306
1307 tree
1308 c_sizeof (type)
1309 tree type;
1310 {
1311 enum tree_code code = TREE_CODE (type);
1312 tree t;
1313
1314 if (processing_template_decl)
1315 return build_min (SIZEOF_EXPR, sizetype, type);
1316
1317 if (code == FUNCTION_TYPE)
1318 {
1319 if (pedantic || warn_pointer_arith)
1320 pedwarn ("ANSI C++ forbids taking the sizeof a function type");
1321 return size_int (1);
1322 }
1323 if (code == METHOD_TYPE)
1324 {
1325 if (pedantic || warn_pointer_arith)
1326 pedwarn ("ANSI C++ forbids taking the sizeof a method type");
1327 return size_int (1);
1328 }
1329 if (code == VOID_TYPE)
1330 {
1331 if (pedantic || warn_pointer_arith)
1332 pedwarn ("ANSI C++ forbids taking the sizeof a void type");
1333 return size_int (1);
1334 }
1335 if (code == ERROR_MARK)
1336 return size_int (1);
1337
1338 /* ARM $5.3.2: ``When applied to a reference, the result is the size of the
1339 referenced object.'' */
1340 if (code == REFERENCE_TYPE)
1341 type = TREE_TYPE (type);
1342
1343 /* We couldn't find anything in the ARM or the draft standard that says,
1344 one way or the other, if doing sizeof on something that doesn't have
1345 an object associated with it is correct or incorrect. For example, if
1346 you declare `struct S { char str[16]; };', and in your program do
1347 a `sizeof (S::str)', should we flag that as an error or should we give
1348 the size of it? Since it seems like a reasonable thing to do, we'll go
1349 with giving the value. */
1350 if (code == OFFSET_TYPE)
1351 type = TREE_TYPE (type);
1352
1353 /* @@ This also produces an error for a signature ref.
1354 In that case we should be able to do better. */
1355 if (IS_SIGNATURE (type))
1356 {
1357 error ("`sizeof' applied to a signature type");
1358 return size_int (0);
1359 }
1360
1361 if (TYPE_SIZE (complete_type (type)) == 0)
1362 {
1363 cp_error ("`sizeof' applied to incomplete type `%T'", type);
1364 return size_int (0);
1365 }
1366
1367 /* Convert in case a char is more than one unit. */
1368 t = size_binop (CEIL_DIV_EXPR, TYPE_SIZE (type),
1369 size_int (TYPE_PRECISION (char_type_node)));
1370 /* size_binop does not put the constant in range, so do it now. */
1371 if (TREE_CODE (t) == INTEGER_CST && force_fit_type (t, 0))
1372 TREE_CONSTANT_OVERFLOW (t) = TREE_OVERFLOW (t) = 1;
1373 return t;
1374 }
1375
1376 tree
1377 expr_sizeof (e)
1378 tree e;
1379 {
1380 if (processing_template_decl)
1381 return build_min (SIZEOF_EXPR, sizetype, e);
1382
1383 if (TREE_CODE (e) == COMPONENT_REF
1384 && DECL_BIT_FIELD (TREE_OPERAND (e, 1)))
1385 error ("sizeof applied to a bit-field");
1386 /* ANSI says arrays and functions are converted inside comma.
1387 But we can't really convert them in build_compound_expr
1388 because that would break commas in lvalues.
1389 So do the conversion here if operand was a comma. */
1390 if (TREE_CODE (e) == COMPOUND_EXPR
1391 && (TREE_CODE (TREE_TYPE (e)) == ARRAY_TYPE
1392 || TREE_CODE (TREE_TYPE (e)) == FUNCTION_TYPE))
1393 e = default_conversion (e);
1394 else if (TREE_CODE (e) == TREE_LIST)
1395 {
1396 tree t = TREE_VALUE (e);
1397 if (t != NULL_TREE
1398 && ((TREE_TYPE (t)
1399 && TREE_CODE (TREE_TYPE (t)) == FUNCTION_TYPE)
1400 || is_overloaded_fn (t)))
1401 pedwarn ("ANSI C++ forbids taking the sizeof a function type");
1402 }
1403 return c_sizeof (TREE_TYPE (e));
1404 }
1405
1406 tree
1407 c_sizeof_nowarn (type)
1408 tree type;
1409 {
1410 enum tree_code code = TREE_CODE (type);
1411 tree t;
1412
1413 if (code == FUNCTION_TYPE
1414 || code == METHOD_TYPE
1415 || code == VOID_TYPE
1416 || code == ERROR_MARK)
1417 return size_int (1);
1418 if (code == REFERENCE_TYPE)
1419 type = TREE_TYPE (type);
1420
1421 if (TYPE_SIZE (type) == 0)
1422 return size_int (0);
1423
1424 /* Convert in case a char is more than one unit. */
1425 t = size_binop (CEIL_DIV_EXPR, TYPE_SIZE (type),
1426 size_int (TYPE_PRECISION (char_type_node)));
1427 force_fit_type (t, 0);
1428 return t;
1429 }
1430
1431 /* Implement the __alignof keyword: Return the minimum required
1432 alignment of TYPE, measured in bytes. */
1433
1434 tree
1435 c_alignof (type)
1436 tree type;
1437 {
1438 enum tree_code code = TREE_CODE (type);
1439 tree t;
1440
1441 if (code == FUNCTION_TYPE || code == METHOD_TYPE)
1442 return size_int (FUNCTION_BOUNDARY / BITS_PER_UNIT);
1443
1444 if (code == VOID_TYPE || code == ERROR_MARK)
1445 return size_int (1);
1446
1447 /* C++: this is really correct! */
1448 if (code == REFERENCE_TYPE)
1449 type = TREE_TYPE (type);
1450
1451 /* @@ This also produces an error for a signature ref.
1452 In that case we should be able to do better. */
1453 if (IS_SIGNATURE (type))
1454 {
1455 error ("`__alignof' applied to a signature type");
1456 return size_int (1);
1457 }
1458
1459 t = size_int (TYPE_ALIGN (type) / BITS_PER_UNIT);
1460 force_fit_type (t, 0);
1461 return t;
1462 }
1463 \f
1464 /* Perform default promotions for C data used in expressions.
1465 Arrays and functions are converted to pointers;
1466 enumeral types or short or char, to int.
1467 In addition, manifest constants symbols are replaced by their values.
1468
1469 C++: this will automatically bash references to their target type. */
1470
1471 tree
1472 decay_conversion (exp)
1473 tree exp;
1474 {
1475 register tree type = TREE_TYPE (exp);
1476 register enum tree_code code = TREE_CODE (type);
1477
1478 if (code == OFFSET_TYPE)
1479 {
1480 if (TREE_CODE (exp) == OFFSET_REF)
1481 return decay_conversion (resolve_offset_ref (exp));
1482
1483 type = TREE_TYPE (type);
1484 code = TREE_CODE (type);
1485
1486 if (type == unknown_type_node)
1487 {
1488 cp_pedwarn ("assuming & on overloaded member function");
1489 return build_unary_op (ADDR_EXPR, exp, 0);
1490 }
1491 }
1492
1493 if (code == REFERENCE_TYPE)
1494 {
1495 exp = convert_from_reference (exp);
1496 type = TREE_TYPE (exp);
1497 code = TREE_CODE (type);
1498 }
1499
1500 /* Constants can be used directly unless they're not loadable. */
1501 if (TREE_CODE (exp) == CONST_DECL)
1502 exp = DECL_INITIAL (exp);
1503 /* Replace a nonvolatile const static variable with its value. */
1504 else if (TREE_READONLY_DECL_P (exp))
1505 {
1506 exp = decl_constant_value (exp);
1507 type = TREE_TYPE (exp);
1508 }
1509
1510 /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
1511 Leave such NOP_EXPRs, since RHS is being used in non-lvalue context. */
1512
1513 if (code == VOID_TYPE)
1514 {
1515 error ("void value not ignored as it ought to be");
1516 return error_mark_node;
1517 }
1518 if (code == FUNCTION_TYPE)
1519 {
1520 return build_unary_op (ADDR_EXPR, exp, 0);
1521 }
1522 if (code == METHOD_TYPE)
1523 {
1524 cp_pedwarn ("assuming & on `%E'", exp);
1525 return build_unary_op (ADDR_EXPR, exp, 0);
1526 }
1527 if (code == ARRAY_TYPE)
1528 {
1529 register tree adr;
1530 tree restype;
1531 tree ptrtype;
1532 int constp, volatilep;
1533
1534 if (TREE_CODE (exp) == INDIRECT_REF)
1535 {
1536 /* Stripping away the INDIRECT_REF is not the right
1537 thing to do for references... */
1538 tree inner = TREE_OPERAND (exp, 0);
1539 if (TREE_CODE (TREE_TYPE (inner)) == REFERENCE_TYPE)
1540 {
1541 inner = build1 (CONVERT_EXPR,
1542 build_pointer_type (TREE_TYPE (TREE_TYPE (inner))),
1543 inner);
1544 TREE_CONSTANT (inner) = TREE_CONSTANT (TREE_OPERAND (inner, 0));
1545 }
1546 return cp_convert (build_pointer_type (TREE_TYPE (type)), inner);
1547 }
1548
1549 if (TREE_CODE (exp) == COMPOUND_EXPR)
1550 {
1551 tree op1 = decay_conversion (TREE_OPERAND (exp, 1));
1552 return build (COMPOUND_EXPR, TREE_TYPE (op1),
1553 TREE_OPERAND (exp, 0), op1);
1554 }
1555
1556 if (!lvalue_p (exp)
1557 && ! (TREE_CODE (exp) == CONSTRUCTOR && TREE_STATIC (exp)))
1558 {
1559 error ("invalid use of non-lvalue array");
1560 return error_mark_node;
1561 }
1562
1563 constp = volatilep = 0;
1564 if (TREE_CODE_CLASS (TREE_CODE (exp)) == 'r'
1565 || TREE_CODE_CLASS (TREE_CODE (exp)) == 'd')
1566 {
1567 constp = TREE_READONLY (exp);
1568 volatilep = TREE_THIS_VOLATILE (exp);
1569 }
1570
1571 restype = TREE_TYPE (type);
1572 if (TYPE_READONLY (type) || TYPE_VOLATILE (type)
1573 || constp || volatilep)
1574 restype = cp_build_type_variant (restype,
1575 TYPE_READONLY (type) || constp,
1576 TYPE_VOLATILE (type) || volatilep);
1577 ptrtype = build_pointer_type (restype);
1578
1579 if (TREE_CODE (exp) == VAR_DECL)
1580 {
1581 /* ??? This is not really quite correct
1582 in that the type of the operand of ADDR_EXPR
1583 is not the target type of the type of the ADDR_EXPR itself.
1584 Question is, can this lossage be avoided? */
1585 adr = build1 (ADDR_EXPR, ptrtype, exp);
1586 if (mark_addressable (exp) == 0)
1587 return error_mark_node;
1588 TREE_CONSTANT (adr) = staticp (exp);
1589 TREE_SIDE_EFFECTS (adr) = 0; /* Default would be, same as EXP. */
1590 return adr;
1591 }
1592 /* This way is better for a COMPONENT_REF since it can
1593 simplify the offset for a component. */
1594 adr = build_unary_op (ADDR_EXPR, exp, 1);
1595 return cp_convert (ptrtype, adr);
1596 }
1597
1598 return exp;
1599 }
1600
1601 tree
1602 default_conversion (exp)
1603 tree exp;
1604 {
1605 tree type;
1606 enum tree_code code;
1607
1608 exp = decay_conversion (exp);
1609
1610 type = TREE_TYPE (exp);
1611 code = TREE_CODE (type);
1612
1613 if (INTEGRAL_CODE_P (code))
1614 {
1615 tree t = type_promotes_to (type);
1616 if (t != type)
1617 return cp_convert (t, exp);
1618 }
1619
1620 return exp;
1621 }
1622
1623 /* Take the address of an inline function without setting TREE_ADDRESSABLE
1624 or TREE_USED. */
1625
1626 tree
1627 inline_conversion (exp)
1628 tree exp;
1629 {
1630 if (TREE_CODE (exp) == FUNCTION_DECL)
1631 {
1632 tree type = build_type_variant
1633 (TREE_TYPE (exp), TREE_READONLY (exp), TREE_THIS_VOLATILE (exp));
1634 exp = build1 (ADDR_EXPR, build_pointer_type (type), exp);
1635 }
1636 return exp;
1637 }
1638 \f
1639 tree
1640 build_object_ref (datum, basetype, field)
1641 tree datum, basetype, field;
1642 {
1643 tree dtype;
1644 if (datum == error_mark_node)
1645 return error_mark_node;
1646
1647 dtype = TREE_TYPE (datum);
1648 if (TREE_CODE (dtype) == REFERENCE_TYPE)
1649 dtype = TREE_TYPE (dtype);
1650 if (! IS_AGGR_TYPE_CODE (TREE_CODE (dtype)))
1651 {
1652 cp_error ("request for member `%T::%D' in expression of non-aggregate type `%T'",
1653 basetype, field, dtype);
1654 return error_mark_node;
1655 }
1656 else if (IS_SIGNATURE (basetype))
1657 {
1658 warning ("signature name in scope resolution ignored");
1659 return build_component_ref (datum, field, NULL_TREE, 1);
1660 }
1661 else if (is_aggr_type (basetype, 1))
1662 {
1663 tree binfo = binfo_or_else (basetype, dtype);
1664 if (binfo)
1665 return build_x_component_ref (build_scoped_ref (datum, basetype),
1666 field, binfo, 1);
1667 }
1668 return error_mark_node;
1669 }
1670
1671 /* Like `build_component_ref, but uses an already found field, and converts
1672 from a reference. Must compute access for current_class_ref.
1673 Otherwise, ok. */
1674
1675 tree
1676 build_component_ref_1 (datum, field, protect)
1677 tree datum, field;
1678 int protect;
1679 {
1680 return convert_from_reference
1681 (build_component_ref (datum, field, NULL_TREE, protect));
1682 }
1683
1684 /* Given a COND_EXPR, MIN_EXPR, or MAX_EXPR in T, return it in a form that we
1685 can, for example, use as an lvalue. This code used to be in
1686 unary_complex_lvalue, but we needed it to deal with `a = (d == c) ? b : c'
1687 expressions, where we're dealing with aggregates. But now it's again only
1688 called from unary_complex_lvalue. The case (in particular) that led to
1689 this was with CODE == ADDR_EXPR, since it's not an lvalue when we'd
1690 get it there. */
1691
1692 static tree
1693 rationalize_conditional_expr (code, t)
1694 enum tree_code code;
1695 tree t;
1696 {
1697 /* For MIN_EXPR or MAX_EXPR, fold-const.c has arranged things so that
1698 the first operand is always the one to be used if both operands
1699 are equal, so we know what conditional expression this used to be. */
1700 if (TREE_CODE (t) == MIN_EXPR || TREE_CODE (t) == MAX_EXPR)
1701 {
1702 return
1703 build_conditional_expr (build_x_binary_op ((TREE_CODE (t) == MIN_EXPR
1704 ? LE_EXPR : GE_EXPR),
1705 TREE_OPERAND (t, 0),
1706 TREE_OPERAND (t, 1)),
1707 build_unary_op (code, TREE_OPERAND (t, 0), 0),
1708 build_unary_op (code, TREE_OPERAND (t, 1), 0));
1709 }
1710
1711 return
1712 build_conditional_expr (TREE_OPERAND (t, 0),
1713 build_unary_op (code, TREE_OPERAND (t, 1), 0),
1714 build_unary_op (code, TREE_OPERAND (t, 2), 0));
1715 }
1716
1717 /* Given the TYPE of an anonymous union field inside T, return the
1718 FIELD_DECL for the field. If not found return NULL_TREE. Because
1719 anonymous unions can nest, we must also search all anonymous unions
1720 that are directly reachable. */
1721
1722 static tree
1723 lookup_anon_field (t, type)
1724 tree t, type;
1725 {
1726 tree field;
1727
1728 for (field = TYPE_FIELDS (t); field; field = TREE_CHAIN (field))
1729 {
1730 if (TREE_STATIC (field))
1731 continue;
1732 if (TREE_CODE (field) != FIELD_DECL)
1733 continue;
1734
1735 /* If we find it directly, return the field. */
1736 if (DECL_NAME (field) == NULL_TREE
1737 && type == TREE_TYPE (field))
1738 {
1739 return field;
1740 }
1741
1742 /* Otherwise, it could be nested, search harder. */
1743 if (DECL_NAME (field) == NULL_TREE
1744 && TREE_CODE (TREE_TYPE (field)) == UNION_TYPE)
1745 {
1746 tree subfield = lookup_anon_field (TREE_TYPE (field), type);
1747 if (subfield)
1748 return subfield;
1749 }
1750 }
1751 return NULL_TREE;
1752 }
1753
1754 /* Build a COMPONENT_REF for a given DATUM, and it's member COMPONENT.
1755 COMPONENT can be an IDENTIFIER_NODE that is the name of the member
1756 that we are interested in, or it can be a FIELD_DECL. */
1757
1758 tree
1759 build_component_ref (datum, component, basetype_path, protect)
1760 tree datum, component, basetype_path;
1761 int protect;
1762 {
1763 register tree basetype = TREE_TYPE (datum);
1764 register enum tree_code code;
1765 register tree field = NULL;
1766 register tree ref;
1767
1768 if (processing_template_decl)
1769 return build_min_nt (COMPONENT_REF, datum, component);
1770
1771 /* If DATUM is a COMPOUND_EXPR or COND_EXPR, move our reference
1772 inside it. */
1773 switch (TREE_CODE (datum))
1774 {
1775 case COMPOUND_EXPR:
1776 {
1777 tree value = build_component_ref (TREE_OPERAND (datum, 1), component,
1778 basetype_path, protect);
1779 return build (COMPOUND_EXPR, TREE_TYPE (value),
1780 TREE_OPERAND (datum, 0), value);
1781 }
1782 case COND_EXPR:
1783 return build_conditional_expr
1784 (TREE_OPERAND (datum, 0),
1785 build_component_ref (TREE_OPERAND (datum, 1), component,
1786 basetype_path, protect),
1787 build_component_ref (TREE_OPERAND (datum, 2), component,
1788 basetype_path, protect));
1789 }
1790
1791 code = TREE_CODE (basetype);
1792
1793 if (code == REFERENCE_TYPE)
1794 {
1795 datum = convert_from_reference (datum);
1796 basetype = TREE_TYPE (datum);
1797 code = TREE_CODE (basetype);
1798 }
1799 if (TREE_CODE (datum) == OFFSET_REF)
1800 {
1801 datum = resolve_offset_ref (datum);
1802 basetype = TREE_TYPE (datum);
1803 code = TREE_CODE (basetype);
1804 }
1805
1806 /* First, see if there is a field or component with name COMPONENT. */
1807 if (TREE_CODE (component) == TREE_LIST)
1808 {
1809 my_friendly_assert (!(TREE_CHAIN (component) == NULL_TREE
1810 && DECL_CHAIN (TREE_VALUE (component)) == NULL_TREE), 309);
1811 return build (COMPONENT_REF, TREE_TYPE (component), datum, component);
1812 }
1813
1814 if (! IS_AGGR_TYPE_CODE (code))
1815 {
1816 if (code != ERROR_MARK)
1817 cp_error ("request for member `%D' in `%E', which is of non-aggregate type `%T'",
1818 component, datum, basetype);
1819 return error_mark_node;
1820 }
1821
1822 if (TYPE_SIZE (complete_type (basetype)) == 0)
1823 {
1824 incomplete_type_error (0, basetype);
1825 return error_mark_node;
1826 }
1827
1828 if (TREE_CODE (component) == BIT_NOT_EXPR)
1829 {
1830 if (TYPE_IDENTIFIER (basetype) != TREE_OPERAND (component, 0))
1831 {
1832 cp_error ("destructor specifier `%T::~%T' must have matching names",
1833 basetype, TREE_OPERAND (component, 0));
1834 return error_mark_node;
1835 }
1836 if (! TYPE_HAS_DESTRUCTOR (basetype))
1837 {
1838 cp_error ("type `%T' has no destructor", basetype);
1839 return error_mark_node;
1840 }
1841 return TREE_VEC_ELT (CLASSTYPE_METHOD_VEC (basetype), 1);
1842 }
1843
1844 /* Look up component name in the structure type definition. */
1845 if (CLASSTYPE_VFIELD (basetype)
1846 && DECL_NAME (CLASSTYPE_VFIELD (basetype)) == component)
1847 /* Special-case this because if we use normal lookups in an ambiguous
1848 hierarchy, the compiler will abort (because vptr lookups are
1849 not supposed to be ambiguous. */
1850 field = CLASSTYPE_VFIELD (basetype);
1851 else if (TREE_CODE (component) == FIELD_DECL
1852 || TREE_CODE (component) == TYPE_DECL)
1853 {
1854 field = component;
1855 }
1856 else
1857 {
1858 tree name = component;
1859 if (TREE_CODE (component) == VAR_DECL)
1860 name = DECL_NAME (component);
1861 if (basetype_path == NULL_TREE)
1862 basetype_path = TYPE_BINFO (basetype);
1863 field = lookup_field (basetype_path, name,
1864 protect && !VFIELD_NAME_P (name), 0);
1865 if (field == error_mark_node)
1866 return error_mark_node;
1867
1868 if (field == NULL_TREE)
1869 {
1870 /* Not found as a data field, look for it as a method. If found,
1871 then if this is the only possible one, return it, else
1872 report ambiguity error. */
1873 tree fndecls = lookup_fnfields (basetype_path, name, 1);
1874 if (fndecls == error_mark_node)
1875 return error_mark_node;
1876 if (fndecls)
1877 {
1878 if (TREE_CHAIN (fndecls) == NULL_TREE
1879 && DECL_CHAIN (TREE_VALUE (fndecls)) == NULL_TREE)
1880 {
1881 tree access, fndecl;
1882
1883 /* Unique, so use this one now. */
1884 basetype = TREE_PURPOSE (fndecls);
1885 fndecl = TREE_VALUE (fndecls);
1886 access = compute_access (TREE_PURPOSE (fndecls), fndecl);
1887 if (access == access_public_node)
1888 {
1889 if (DECL_VINDEX (fndecl)
1890 && ! resolves_to_fixed_type_p (datum, 0))
1891 {
1892 tree addr = build_unary_op (ADDR_EXPR, datum, 0);
1893 tree fntype = TREE_TYPE (fndecl);
1894
1895 addr = convert_pointer_to (DECL_CONTEXT (fndecl), addr);
1896 datum = build_indirect_ref (addr, NULL_PTR);
1897 my_friendly_assert (datum != error_mark_node, 310);
1898 fndecl = build_vfn_ref (&addr, datum, DECL_VINDEX (fndecl));
1899 TREE_TYPE (fndecl) = build_pointer_type (fntype);
1900 }
1901 else
1902 mark_used (fndecl);
1903 return build (OFFSET_REF, TREE_TYPE (fndecl), datum, fndecl);
1904 }
1905 if (access == access_protected_node)
1906 cp_error ("member function `%D' is protected", fndecl);
1907 else
1908 cp_error ("member function `%D' is private", fndecl);
1909 return error_mark_node;
1910 }
1911 else
1912 {
1913 /* Just act like build_offset_ref, since the object does
1914 not matter unless we're actually calling the function. */
1915 tree t;
1916
1917 t = build_tree_list (error_mark_node, fndecls);
1918 TREE_TYPE (t) = build_offset_type (basetype,
1919 unknown_type_node);
1920 return t;
1921 }
1922 }
1923
1924 cp_error ("`%#T' has no member named `%D'", basetype, name);
1925 return error_mark_node;
1926 }
1927 else if (TREE_TYPE (field) == error_mark_node)
1928 return error_mark_node;
1929
1930 if (TREE_CODE (field) != FIELD_DECL)
1931 {
1932 if (TREE_CODE (field) == TYPE_DECL)
1933 {
1934 cp_error ("invalid use of type decl `%#D' as expression", field);
1935 return error_mark_node;
1936 }
1937 else if (DECL_RTL (field) != 0)
1938 mark_used (field);
1939 else
1940 TREE_USED (field) = 1;
1941 return field;
1942 }
1943 }
1944
1945 /* See if we have to do any conversions so that we pick up the field from the
1946 right context. */
1947 if (DECL_FIELD_CONTEXT (field) != basetype)
1948 {
1949 tree context = DECL_FIELD_CONTEXT (field);
1950 tree base = context;
1951 while (base != basetype && TYPE_NAME (base)
1952 && ANON_AGGRNAME_P (TYPE_IDENTIFIER (base)))
1953 {
1954 base = TYPE_CONTEXT (base);
1955 }
1956
1957 /* Handle base classes here... */
1958 if (base != basetype && TYPE_USES_COMPLEX_INHERITANCE (basetype))
1959 {
1960 tree addr = build_unary_op (ADDR_EXPR, datum, 0);
1961 if (integer_zerop (addr))
1962 {
1963 error ("invalid reference to NULL ptr, use ptr-to-member instead");
1964 return error_mark_node;
1965 }
1966 if (VBASE_NAME_P (DECL_NAME (field)))
1967 {
1968 /* It doesn't matter which vbase pointer we grab, just
1969 find one of them. */
1970 tree binfo = get_binfo (base,
1971 TREE_TYPE (TREE_TYPE (addr)), 0);
1972 addr = convert_pointer_to_real (binfo, addr);
1973 }
1974 else
1975 addr = convert_pointer_to (base, addr);
1976 datum = build_indirect_ref (addr, NULL_PTR);
1977 my_friendly_assert (datum != error_mark_node, 311);
1978 }
1979 basetype = base;
1980
1981 /* Handle things from anon unions here... */
1982 if (TYPE_NAME (context) && ANON_AGGRNAME_P (TYPE_IDENTIFIER (context)))
1983 {
1984 tree subfield = lookup_anon_field (basetype, context);
1985 tree subdatum = build_component_ref (datum, subfield,
1986 basetype_path, protect);
1987 return build_component_ref (subdatum, field, basetype_path, protect);
1988 }
1989 }
1990
1991 ref = fold (build (COMPONENT_REF, TREE_TYPE (field),
1992 break_out_cleanups (datum), field));
1993
1994 if (TREE_READONLY (datum) || TREE_READONLY (field))
1995 TREE_READONLY (ref) = 1;
1996 if (TREE_THIS_VOLATILE (datum) || TREE_THIS_VOLATILE (field))
1997 TREE_THIS_VOLATILE (ref) = 1;
1998 if (DECL_MUTABLE_P (field))
1999 TREE_READONLY (ref) = 0;
2000
2001 return ref;
2002 }
2003
2004 /* Variant of build_component_ref for use in expressions, which should
2005 never have REFERENCE_TYPE. */
2006
2007 tree
2008 build_x_component_ref (datum, component, basetype_path, protect)
2009 tree datum, component, basetype_path;
2010 int protect;
2011 {
2012 tree t = build_component_ref (datum, component, basetype_path, protect);
2013
2014 if (! processing_template_decl)
2015 t = convert_from_reference (t);
2016
2017 return t;
2018 }
2019 \f
2020 /* Given an expression PTR for a pointer, return an expression
2021 for the value pointed to.
2022 ERRORSTRING is the name of the operator to appear in error messages.
2023
2024 This function may need to overload OPERATOR_FNNAME.
2025 Must also handle REFERENCE_TYPEs for C++. */
2026
2027 tree
2028 build_x_indirect_ref (ptr, errorstring)
2029 tree ptr;
2030 char *errorstring;
2031 {
2032 tree rval;
2033
2034 if (processing_template_decl)
2035 return build_min_nt (INDIRECT_REF, ptr);
2036
2037 rval = build_opfncall (INDIRECT_REF, LOOKUP_NORMAL, ptr, NULL_TREE, NULL_TREE);
2038 if (rval)
2039 return rval;
2040 return build_indirect_ref (ptr, errorstring);
2041 }
2042
2043 tree
2044 build_indirect_ref (ptr, errorstring)
2045 tree ptr;
2046 char *errorstring;
2047 {
2048 register tree pointer, type;
2049
2050 if (ptr == error_mark_node)
2051 return error_mark_node;
2052
2053 pointer = (TREE_CODE (TREE_TYPE (ptr)) == REFERENCE_TYPE
2054 ? ptr : default_conversion (ptr));
2055 type = TREE_TYPE (pointer);
2056
2057 if (ptr == current_class_ptr)
2058 return current_class_ref;
2059
2060 if (IS_AGGR_TYPE (type))
2061 {
2062 ptr = build_expr_type_conversion (WANT_POINTER, pointer, 1);
2063
2064 if (ptr)
2065 {
2066 pointer = ptr;
2067 type = TREE_TYPE (pointer);
2068 }
2069 }
2070
2071 if (TREE_CODE (type) == POINTER_TYPE || TREE_CODE (type) == REFERENCE_TYPE)
2072 {
2073 if (TREE_CODE (pointer) == ADDR_EXPR
2074 && !flag_volatile
2075 && (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_OPERAND (pointer, 0)))
2076 == TYPE_MAIN_VARIANT (TREE_TYPE (type)))
2077 && (TREE_READONLY (TREE_OPERAND (pointer, 0))
2078 == TYPE_READONLY (TREE_TYPE (type)))
2079 && (TREE_THIS_VOLATILE (TREE_OPERAND (pointer, 0))
2080 == TYPE_VOLATILE (TREE_TYPE (type))))
2081 return TREE_OPERAND (pointer, 0);
2082 else
2083 {
2084 tree t = TREE_TYPE (type);
2085 register tree ref = build1 (INDIRECT_REF,
2086 TYPE_MAIN_VARIANT (t), pointer);
2087
2088 /* We *must* set TREE_READONLY when dereferencing a pointer to const,
2089 so that we get the proper error message if the result is used
2090 to assign to. Also, &* is supposed to be a no-op. */
2091 TREE_READONLY (ref) = TYPE_READONLY (t);
2092 TREE_SIDE_EFFECTS (ref)
2093 = TYPE_VOLATILE (t) || TREE_SIDE_EFFECTS (pointer) || flag_volatile;
2094 TREE_THIS_VOLATILE (ref) = TYPE_VOLATILE (t);
2095 return ref;
2096 }
2097 }
2098 /* `pointer' won't be an error_mark_node if we were given a
2099 pointer to member, so it's cool to check for this here. */
2100 else if (TYPE_PTRMEMFUNC_P (type))
2101 error ("invalid use of `%s' on pointer to member function", errorstring);
2102 else if (TREE_CODE (type) == RECORD_TYPE
2103 && (IS_SIGNATURE_POINTER (type) || IS_SIGNATURE_REFERENCE (type)))
2104 error ("cannot dereference signature pointer/reference");
2105 else if (pointer != error_mark_node)
2106 {
2107 if (errorstring)
2108 error ("invalid type argument of `%s'", errorstring);
2109 else
2110 error ("invalid type argument");
2111 }
2112 return error_mark_node;
2113 }
2114
2115 /* This handles expressions of the form "a[i]", which denotes
2116 an array reference.
2117
2118 This is logically equivalent in C to *(a+i), but we may do it differently.
2119 If A is a variable or a member, we generate a primitive ARRAY_REF.
2120 This avoids forcing the array out of registers, and can work on
2121 arrays that are not lvalues (for example, members of structures returned
2122 by functions).
2123
2124 If INDEX is of some user-defined type, it must be converted to
2125 integer type. Otherwise, to make a compatible PLUS_EXPR, it
2126 will inherit the type of the array, which will be some pointer type. */
2127
2128 tree
2129 build_array_ref (array, idx)
2130 tree array, idx;
2131 {
2132 if (idx == 0)
2133 {
2134 error ("subscript missing in array reference");
2135 return error_mark_node;
2136 }
2137
2138 if (TREE_TYPE (array) == error_mark_node
2139 || TREE_TYPE (idx) == error_mark_node)
2140 return error_mark_node;
2141
2142 if (TREE_CODE (TREE_TYPE (array)) == ARRAY_TYPE
2143 && TREE_CODE (array) != INDIRECT_REF)
2144 {
2145 tree rval, type;
2146
2147 /* Subscripting with type char is likely to lose
2148 on a machine where chars are signed.
2149 So warn on any machine, but optionally.
2150 Don't warn for unsigned char since that type is safe.
2151 Don't warn for signed char because anyone who uses that
2152 must have done so deliberately. */
2153 if (warn_char_subscripts
2154 && TYPE_MAIN_VARIANT (TREE_TYPE (idx)) == char_type_node)
2155 warning ("array subscript has type `char'");
2156
2157 /* Apply default promotions *after* noticing character types. */
2158 idx = default_conversion (idx);
2159
2160 if (TREE_CODE (TREE_TYPE (idx)) != INTEGER_TYPE)
2161 {
2162 error ("array subscript is not an integer");
2163 return error_mark_node;
2164 }
2165
2166 /* An array that is indexed by a non-constant
2167 cannot be stored in a register; we must be able to do
2168 address arithmetic on its address.
2169 Likewise an array of elements of variable size. */
2170 if (TREE_CODE (idx) != INTEGER_CST
2171 || (TYPE_SIZE (TREE_TYPE (TREE_TYPE (array))) != 0
2172 && TREE_CODE (TYPE_SIZE (TREE_TYPE (TREE_TYPE (array)))) != INTEGER_CST))
2173 {
2174 if (mark_addressable (array) == 0)
2175 return error_mark_node;
2176 }
2177 /* An array that is indexed by a constant value which is not within
2178 the array bounds cannot be stored in a register either; because we
2179 would get a crash in store_bit_field/extract_bit_field when trying
2180 to access a non-existent part of the register. */
2181 if (TREE_CODE (idx) == INTEGER_CST
2182 && TYPE_VALUES (TREE_TYPE (array))
2183 && ! int_fits_type_p (idx, TYPE_VALUES (TREE_TYPE (array))))
2184 {
2185 if (mark_addressable (array) == 0)
2186 return error_mark_node;
2187 }
2188
2189 if (pedantic && !lvalue_p (array))
2190 pedwarn ("ANSI C++ forbids subscripting non-lvalue array");
2191
2192 /* Note in C++ it is valid to subscript a `register' array, since
2193 it is valid to take the address of something with that
2194 storage specification. */
2195 if (extra_warnings)
2196 {
2197 tree foo = array;
2198 while (TREE_CODE (foo) == COMPONENT_REF)
2199 foo = TREE_OPERAND (foo, 0);
2200 if (TREE_CODE (foo) == VAR_DECL && DECL_REGISTER (foo))
2201 warning ("subscripting array declared `register'");
2202 }
2203
2204 type = TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (array)));
2205 rval = build (ARRAY_REF, type, array, idx);
2206 /* Array ref is const/volatile if the array elements are
2207 or if the array is.. */
2208 TREE_READONLY (rval)
2209 |= (TYPE_READONLY (TREE_TYPE (TREE_TYPE (array)))
2210 | TREE_READONLY (array));
2211 TREE_SIDE_EFFECTS (rval)
2212 |= (TYPE_VOLATILE (TREE_TYPE (TREE_TYPE (array)))
2213 | TREE_SIDE_EFFECTS (array));
2214 TREE_THIS_VOLATILE (rval)
2215 |= (TYPE_VOLATILE (TREE_TYPE (TREE_TYPE (array)))
2216 /* This was added by rms on 16 Nov 91.
2217 It fixes vol struct foo *a; a->elts[1]
2218 in an inline function.
2219 Hope it doesn't break something else. */
2220 | TREE_THIS_VOLATILE (array));
2221 return require_complete_type (fold (rval));
2222 }
2223
2224 {
2225 tree ar = default_conversion (array);
2226 tree ind = default_conversion (idx);
2227
2228 /* Put the integer in IND to simplify error checking. */
2229 if (TREE_CODE (TREE_TYPE (ar)) == INTEGER_TYPE)
2230 {
2231 tree temp = ar;
2232 ar = ind;
2233 ind = temp;
2234 }
2235
2236 if (ar == error_mark_node)
2237 return ar;
2238
2239 if (TREE_CODE (TREE_TYPE (ar)) != POINTER_TYPE)
2240 {
2241 error ("subscripted value is neither array nor pointer");
2242 return error_mark_node;
2243 }
2244 if (TREE_CODE (TREE_TYPE (ind)) != INTEGER_TYPE)
2245 {
2246 error ("array subscript is not an integer");
2247 return error_mark_node;
2248 }
2249
2250 return build_indirect_ref (build_binary_op_nodefault (PLUS_EXPR, ar, ind, PLUS_EXPR),
2251 "array indexing");
2252 }
2253 }
2254 \f
2255 /* Build a function call to function FUNCTION with parameters PARAMS.
2256 PARAMS is a list--a chain of TREE_LIST nodes--in which the
2257 TREE_VALUE of each node is a parameter-expression. The PARAMS do
2258 not include any object pointer that may be required. FUNCTION's
2259 data type may be a function type or a pointer-to-function.
2260
2261 For C++: If FUNCTION's data type is a TREE_LIST, then the tree list
2262 is the list of possible methods that FUNCTION could conceivably
2263 be. If the list of methods comes from a class, then it will be
2264 a list of lists (where each element is associated with the class
2265 that produced it), otherwise it will be a simple list (for
2266 functions overloaded in global scope).
2267
2268 In the first case, TREE_VALUE (function) is the head of one of those
2269 lists, and TREE_PURPOSE is the name of the function.
2270
2271 In the second case, TREE_PURPOSE (function) is the function's
2272 name directly.
2273
2274 DECL is the class instance variable, usually CURRENT_CLASS_REF.
2275
2276 When calling a TEMPLATE_DECL, we don't require a complete return
2277 type. */
2278
2279 tree
2280 build_x_function_call (function, params, decl)
2281 tree function, params, decl;
2282 {
2283 tree type;
2284 int is_method;
2285
2286 if (function == error_mark_node)
2287 return error_mark_node;
2288
2289 if (processing_template_decl)
2290 return build_min_nt (CALL_EXPR, function, params, NULL_TREE);
2291
2292 type = TREE_TYPE (function);
2293
2294 if (TREE_CODE (type) == OFFSET_TYPE
2295 && TREE_TYPE (type) == unknown_type_node
2296 && TREE_CODE (function) == TREE_LIST
2297 && TREE_CHAIN (function) == NULL_TREE)
2298 {
2299 /* Undo (Foo:bar)()... */
2300 type = TYPE_OFFSET_BASETYPE (type);
2301 function = TREE_VALUE (function);
2302 my_friendly_assert (TREE_CODE (function) == TREE_LIST, 999);
2303 my_friendly_assert (TREE_CHAIN (function) == NULL_TREE, 999);
2304 function = TREE_VALUE (function);
2305 my_friendly_assert (TREE_CODE (function) == FUNCTION_DECL, 999);
2306 function = DECL_NAME (function);
2307 return build_method_call (decl, function, params, TYPE_BINFO (type), LOOKUP_NORMAL);
2308 }
2309
2310 is_method = ((TREE_CODE (function) == TREE_LIST
2311 && current_class_type != NULL_TREE
2312 && IDENTIFIER_CLASS_VALUE (TREE_PURPOSE (function)) == function)
2313 || TREE_CODE (function) == IDENTIFIER_NODE
2314 || TREE_CODE (type) == METHOD_TYPE
2315 || TYPE_PTRMEMFUNC_P (type));
2316
2317 if (TREE_CODE (function) == FUNCTION_DECL
2318 && DECL_STATIC_FUNCTION_P (function))
2319 return build_member_call
2320 (DECL_CONTEXT (function), DECL_NAME (function), params);
2321
2322 /* Handle methods, friends, and overloaded functions, respectively. */
2323 if (is_method)
2324 {
2325 tree basetype = NULL_TREE;
2326
2327 if (TREE_CODE (function) == FUNCTION_DECL
2328 || DECL_FUNCTION_TEMPLATE_P (function))
2329 {
2330 basetype = DECL_CLASS_CONTEXT (function);
2331
2332 if (DECL_NAME (function))
2333 function = DECL_NAME (function);
2334 else
2335 function = TYPE_IDENTIFIER (DECL_CLASS_CONTEXT (function));
2336 }
2337 else if (TREE_CODE (function) == TREE_LIST)
2338 {
2339 my_friendly_assert (TREE_CODE (TREE_VALUE (function)) == FUNCTION_DECL, 312);
2340 basetype = DECL_CLASS_CONTEXT (TREE_VALUE (function));
2341 function = TREE_PURPOSE (function);
2342 }
2343 else if (TREE_CODE (function) != IDENTIFIER_NODE)
2344 {
2345 if (TREE_CODE (function) == OFFSET_REF)
2346 {
2347 if (TREE_OPERAND (function, 0))
2348 decl = TREE_OPERAND (function, 0);
2349 }
2350 /* Call via a pointer to member function. */
2351 if (decl == NULL_TREE)
2352 {
2353 error ("pointer to member function called, but not in class scope");
2354 return error_mark_node;
2355 }
2356 /* What other type of POINTER_TYPE could this be? */
2357 if (TREE_CODE (TREE_TYPE (function)) != POINTER_TYPE
2358 && ! TYPE_PTRMEMFUNC_P (TREE_TYPE (function))
2359 && TREE_CODE (function) != OFFSET_REF)
2360 function = build (OFFSET_REF, TREE_TYPE (type), NULL_TREE, function);
2361 goto do_x_function;
2362 }
2363
2364 /* this is an abbreviated method call.
2365 must go through here in case it is a virtual function.
2366 @@ Perhaps this could be optimized. */
2367
2368 if (basetype && (! current_class_type
2369 || ! DERIVED_FROM_P (basetype, current_class_type)))
2370 return build_member_call (basetype, function, params);
2371
2372 if (decl == NULL_TREE)
2373 {
2374 if (current_class_type == NULL_TREE)
2375 {
2376 error ("object missing in call to method `%s'",
2377 IDENTIFIER_POINTER (function));
2378 return error_mark_node;
2379 }
2380 /* Yow: call from a static member function. */
2381 decl = build1 (NOP_EXPR, build_pointer_type (current_class_type),
2382 error_mark_node);
2383 decl = build_indirect_ref (decl, NULL_PTR);
2384 }
2385
2386 return build_method_call (decl, function, params,
2387 NULL_TREE, LOOKUP_NORMAL);
2388 }
2389 else if (TREE_CODE (function) == COMPONENT_REF
2390 && type == unknown_type_node)
2391 {
2392 /* Should we undo what was done in build_component_ref? */
2393 if (TREE_CODE (TREE_PURPOSE (TREE_OPERAND (function, 1))) == TREE_VEC)
2394 /* Get the name that build_component_ref hid. */
2395 function = DECL_NAME (TREE_VALUE (TREE_OPERAND (function, 1)));
2396 else
2397 function = TREE_PURPOSE (TREE_OPERAND (function, 1));
2398 return build_method_call (decl, function, params,
2399 NULL_TREE, LOOKUP_NORMAL);
2400 }
2401 else if (really_overloaded_fn (function))
2402 {
2403 if (TREE_VALUE (function) == NULL_TREE)
2404 {
2405 cp_error ("function `%D' declared overloaded, but no definitions appear with which to resolve it?!?",
2406 TREE_PURPOSE (function));
2407 return error_mark_node;
2408 }
2409 else
2410 {
2411 tree val = TREE_VALUE (function);
2412
2413 if (flag_ansi_overloading)
2414 return build_new_function_call (function, params, NULL_TREE);
2415
2416 if (TREE_CODE (val) == TEMPLATE_DECL)
2417 return build_overload_call_real
2418 (function, params, LOOKUP_COMPLAIN, (struct candidate *)0, 0);
2419 else if (DECL_CHAIN (val) != NULL_TREE)
2420 return build_overload_call
2421 (function, params, LOOKUP_COMPLAIN);
2422 else
2423 my_friendly_abort (360);
2424 }
2425 }
2426
2427 do_x_function:
2428 if (TREE_CODE (function) == OFFSET_REF)
2429 {
2430 /* If the component is a data element (or a virtual function), we play
2431 games here to make things work. */
2432 tree decl_addr;
2433
2434 if (TREE_OPERAND (function, 0))
2435 decl = TREE_OPERAND (function, 0);
2436 else
2437 decl = current_class_ref;
2438
2439 decl_addr = build_unary_op (ADDR_EXPR, decl, 0);
2440 function = get_member_function_from_ptrfunc (&decl_addr,
2441 TREE_OPERAND (function, 1));
2442 params = expr_tree_cons (NULL_TREE, decl_addr, params);
2443 return build_function_call (function, params);
2444 }
2445
2446 type = TREE_TYPE (function);
2447 if (type != error_mark_node)
2448 {
2449 if (TREE_CODE (type) == REFERENCE_TYPE)
2450 type = TREE_TYPE (type);
2451
2452 if (IS_AGGR_TYPE (type))
2453 return build_opfncall (CALL_EXPR, LOOKUP_NORMAL, function, params, NULL_TREE);
2454 }
2455
2456 if (is_method)
2457 {
2458 tree fntype = TREE_TYPE (function);
2459 tree ctypeptr;
2460
2461 /* Explicitly named method? */
2462 if (TREE_CODE (function) == FUNCTION_DECL)
2463 ctypeptr = build_pointer_type (DECL_CLASS_CONTEXT (function));
2464 /* Expression with ptr-to-method type? It could either be a plain
2465 usage, or it might be a case where the ptr-to-method is being
2466 passed in as an argument. */
2467 else if (TYPE_PTRMEMFUNC_P (fntype))
2468 {
2469 tree rec = TYPE_METHOD_BASETYPE (TREE_TYPE (TYPE_PTRMEMFUNC_FN_TYPE (fntype)));
2470 ctypeptr = build_pointer_type (rec);
2471 }
2472 /* Unexpected node type? */
2473 else
2474 my_friendly_abort (116);
2475 if (decl == NULL_TREE)
2476 {
2477 if (current_function_decl
2478 && DECL_STATIC_FUNCTION_P (current_function_decl))
2479 error ("invalid call to member function needing `this' in static member function scope");
2480 else
2481 error ("pointer to member function called, but not in class scope");
2482 return error_mark_node;
2483 }
2484 if (TREE_CODE (TREE_TYPE (decl)) != POINTER_TYPE
2485 && ! TYPE_PTRMEMFUNC_P (TREE_TYPE (decl)))
2486 {
2487 decl = build_unary_op (ADDR_EXPR, decl, 0);
2488 decl = convert_pointer_to (TREE_TYPE (ctypeptr), decl);
2489 }
2490 else
2491 decl = build_c_cast (ctypeptr, decl);
2492 params = expr_tree_cons (NULL_TREE, decl, params);
2493 }
2494
2495 return build_function_call (function, params);
2496 }
2497
2498 /* Resolve a pointer to member function. INSTANCE is the object
2499 instance to use, if the member points to a virtual member. */
2500
2501 tree
2502 get_member_function_from_ptrfunc (instance_ptrptr, function)
2503 tree *instance_ptrptr;
2504 tree function;
2505 {
2506 if (TREE_CODE (function) == OFFSET_REF)
2507 {
2508 function = TREE_OPERAND (function, 1);
2509 }
2510
2511 if (TYPE_PTRMEMFUNC_P (TREE_TYPE (function)))
2512 {
2513 tree fntype, idx, e1, delta, delta2, e2, e3, aref, vtbl;
2514 tree instance;
2515
2516 tree instance_ptr = *instance_ptrptr;
2517
2518 if (TREE_SIDE_EFFECTS (instance_ptr))
2519 instance_ptr = save_expr (instance_ptr);
2520
2521 if (TREE_SIDE_EFFECTS (function))
2522 function = save_expr (function);
2523
2524 fntype = TYPE_PTRMEMFUNC_FN_TYPE (TREE_TYPE (function));
2525
2526 /* Promoting idx before saving it improves performance on RISC
2527 targets. Without promoting, the first compare used
2528 load-with-sign-extend, while the second used normal load then
2529 shift to sign-extend. An optimizer flaw, perhaps, but it's easier
2530 to make this change. */
2531 idx = save_expr (default_conversion
2532 (build_component_ref (function,
2533 index_identifier,
2534 NULL_TREE, 0)));
2535 e1 = build_binary_op (GT_EXPR, idx, integer_zero_node, 1);
2536 delta = cp_convert (ptrdiff_type_node,
2537 build_component_ref (function, delta_identifier, NULL_TREE, 0));
2538 delta2 = DELTA2_FROM_PTRMEMFUNC (function);
2539
2540 /* Convert down to the right base, before using the instance. */
2541 instance
2542 = convert_pointer_to_real (TYPE_METHOD_BASETYPE (TREE_TYPE (fntype)),
2543 instance_ptr);
2544 if (instance == error_mark_node && instance_ptr != error_mark_node)
2545 return instance;
2546
2547 vtbl = convert_pointer_to (ptr_type_node, instance);
2548 vtbl
2549 = build (PLUS_EXPR,
2550 build_pointer_type (build_pointer_type (vtable_entry_type)),
2551 vtbl, cp_convert (ptrdiff_type_node, delta2));
2552 vtbl = build_indirect_ref (vtbl, NULL_PTR);
2553 aref = build_array_ref (vtbl, build_binary_op (MINUS_EXPR,
2554 idx,
2555 integer_one_node, 1));
2556 if (! flag_vtable_thunks)
2557 {
2558 aref = save_expr (aref);
2559
2560 delta = build_binary_op (PLUS_EXPR,
2561 build_conditional_expr (e1, build_component_ref (aref, delta_identifier, NULL_TREE, 0), integer_zero_node),
2562 delta, 1);
2563 }
2564
2565 *instance_ptrptr = build (PLUS_EXPR, TREE_TYPE (instance_ptr),
2566 instance_ptr, delta);
2567 if (flag_vtable_thunks)
2568 e2 = aref;
2569 else
2570 e2 = build_component_ref (aref, pfn_identifier, NULL_TREE, 0);
2571
2572 e3 = PFN_FROM_PTRMEMFUNC (function);
2573 TREE_TYPE (e2) = TREE_TYPE (e3);
2574 e1 = build_conditional_expr (e1, e2, e3);
2575
2576 if (instance_ptr == error_mark_node
2577 && TREE_CODE (e1) != ADDR_EXPR
2578 && TREE_CODE (TREE_OPERAND (e1, 0)) != FUNCTION_DECL)
2579 cp_error ("object missing in `%E'", function);
2580
2581 function = e1;
2582
2583 /* Make sure this doesn't get evaluated first inside one of the
2584 branches of the COND_EXPR. */
2585 if (TREE_CODE (instance_ptr) == SAVE_EXPR)
2586 function = build (COMPOUND_EXPR, TREE_TYPE (function),
2587 instance_ptr, function);
2588 }
2589 return function;
2590 }
2591
2592 tree
2593 build_function_call_real (function, params, require_complete, flags)
2594 tree function, params;
2595 int require_complete, flags;
2596 {
2597 register tree fntype, fndecl;
2598 register tree value_type;
2599 register tree coerced_params;
2600 tree name = NULL_TREE, assembler_name = NULL_TREE;
2601 int is_method;
2602
2603 /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
2604 Strip such NOP_EXPRs, since FUNCTION is used in non-lvalue context. */
2605 if (TREE_CODE (function) == NOP_EXPR
2606 && TREE_TYPE (function) == TREE_TYPE (TREE_OPERAND (function, 0)))
2607 function = TREE_OPERAND (function, 0);
2608
2609 if (TREE_CODE (function) == FUNCTION_DECL)
2610 {
2611 name = DECL_NAME (function);
2612 assembler_name = DECL_ASSEMBLER_NAME (function);
2613
2614 GNU_xref_call (current_function_decl,
2615 IDENTIFIER_POINTER (name ? name
2616 : TYPE_IDENTIFIER (DECL_CLASS_CONTEXT (function))));
2617 mark_used (function);
2618 fndecl = function;
2619
2620 /* Convert anything with function type to a pointer-to-function. */
2621 if (pedantic
2622 && name
2623 && IDENTIFIER_LENGTH (name) == 4
2624 && ! strcmp (IDENTIFIER_POINTER (name), "main")
2625 && DECL_CONTEXT (function) == NULL_TREE)
2626 {
2627 pedwarn ("ANSI C++ forbids calling `main' from within program");
2628 }
2629
2630 if (pedantic && DECL_THIS_INLINE (function) && ! DECL_INITIAL (function)
2631 && ! DECL_ARTIFICIAL (function)
2632 && ! DECL_PENDING_INLINE_INFO (function))
2633 cp_pedwarn ("inline function `%#D' called before definition",
2634 function);
2635
2636 /* Differs from default_conversion by not setting TREE_ADDRESSABLE
2637 (because calling an inline function does not mean the function
2638 needs to be separately compiled). */
2639
2640 if (DECL_INLINE (function))
2641 function = inline_conversion (function);
2642 else
2643 function = build_addr_func (function);
2644 }
2645 else
2646 {
2647 fndecl = NULL_TREE;
2648
2649 function = build_addr_func (function);
2650 }
2651
2652 if (function == error_mark_node)
2653 return error_mark_node;
2654
2655 fntype = TREE_TYPE (function);
2656
2657 if (TYPE_PTRMEMFUNC_P (fntype))
2658 {
2659 cp_error ("must use .* or ->* to call pointer-to-member function in `%E (...)'",
2660 function);
2661 return error_mark_node;
2662 }
2663
2664 is_method = (TREE_CODE (fntype) == POINTER_TYPE
2665 && TREE_CODE (TREE_TYPE (fntype)) == METHOD_TYPE);
2666
2667 if (!((TREE_CODE (fntype) == POINTER_TYPE
2668 && TREE_CODE (TREE_TYPE (fntype)) == FUNCTION_TYPE)
2669 || is_method
2670 || TREE_CODE (function) == TEMPLATE_ID_EXPR))
2671 {
2672 cp_error ("`%E' cannot be used as a function", function);
2673 return error_mark_node;
2674 }
2675
2676 /* fntype now gets the type of function pointed to. */
2677 fntype = TREE_TYPE (fntype);
2678
2679 /* Convert the parameters to the types declared in the
2680 function prototype, or apply default promotions. */
2681
2682 if (flags & LOOKUP_COMPLAIN)
2683 coerced_params = convert_arguments (NULL_TREE, TYPE_ARG_TYPES (fntype),
2684 params, fndecl, LOOKUP_NORMAL);
2685 else
2686 coerced_params = convert_arguments (NULL_TREE, TYPE_ARG_TYPES (fntype),
2687 params, fndecl, 0);
2688
2689 if (coerced_params == error_mark_node)
2690 if (flags & LOOKUP_SPECULATIVELY)
2691 return NULL_TREE;
2692 else
2693 return error_mark_node;
2694
2695 /* Check for errors in format strings. */
2696
2697 if (warn_format && (name || assembler_name))
2698 check_function_format (name, assembler_name, coerced_params);
2699
2700 /* Recognize certain built-in functions so we can make tree-codes
2701 other than CALL_EXPR. We do this when it enables fold-const.c
2702 to do something useful. */
2703
2704 if (TREE_CODE (function) == ADDR_EXPR
2705 && TREE_CODE (TREE_OPERAND (function, 0)) == FUNCTION_DECL
2706 && DECL_BUILT_IN (TREE_OPERAND (function, 0)))
2707 switch (DECL_FUNCTION_CODE (TREE_OPERAND (function, 0)))
2708 {
2709 case BUILT_IN_ABS:
2710 case BUILT_IN_LABS:
2711 case BUILT_IN_FABS:
2712 if (coerced_params == 0)
2713 return integer_zero_node;
2714 return build_unary_op (ABS_EXPR, TREE_VALUE (coerced_params), 0);
2715 }
2716
2717 /* C++ */
2718 value_type = TREE_TYPE (fntype) ? TREE_TYPE (fntype) : void_type_node;
2719 {
2720 register tree result
2721 = build_call (function, value_type, coerced_params);
2722
2723 if (require_complete)
2724 {
2725 if (value_type == void_type_node)
2726 return result;
2727 result = require_complete_type (result);
2728 }
2729 if (IS_AGGR_TYPE (value_type))
2730 result = build_cplus_new (value_type, result);
2731 return convert_from_reference (result);
2732 }
2733 }
2734
2735 tree
2736 build_function_call (function, params)
2737 tree function, params;
2738 {
2739 return build_function_call_real (function, params, 1, LOOKUP_NORMAL);
2740 }
2741 \f
2742 /* Convert the actual parameter expressions in the list VALUES
2743 to the types in the list TYPELIST.
2744 If parmdecls is exhausted, or when an element has NULL as its type,
2745 perform the default conversions.
2746
2747 RETURN_LOC is the location of the return value, if known, NULL_TREE
2748 otherwise. This is useful in the case where we can avoid creating
2749 a temporary variable in the case where we can initialize the return
2750 value directly. If we are not eliding constructors, then we set this
2751 to NULL_TREE to avoid this avoidance.
2752
2753 NAME is an IDENTIFIER_NODE or 0. It is used only for error messages.
2754
2755 This is also where warnings about wrong number of args are generated.
2756
2757 Return a list of expressions for the parameters as converted.
2758
2759 Both VALUES and the returned value are chains of TREE_LIST nodes
2760 with the elements of the list in the TREE_VALUE slots of those nodes.
2761
2762 In C++, unspecified trailing parameters can be filled in with their
2763 default arguments, if such were specified. Do so here. */
2764
2765 tree
2766 convert_arguments (return_loc, typelist, values, fndecl, flags)
2767 tree return_loc, typelist, values, fndecl;
2768 int flags;
2769 {
2770 register tree typetail, valtail;
2771 register tree result = NULL_TREE;
2772 char *called_thing;
2773 int i = 0;
2774
2775 if (! flag_elide_constructors)
2776 return_loc = 0;
2777
2778 /* Argument passing is always copy-initialization. */
2779 flags |= LOOKUP_ONLYCONVERTING;
2780
2781 if (fndecl)
2782 {
2783 if (TREE_CODE (TREE_TYPE (fndecl)) == METHOD_TYPE)
2784 {
2785 if (DECL_NAME (fndecl) == NULL_TREE
2786 || IDENTIFIER_HAS_TYPE_VALUE (DECL_NAME (fndecl)))
2787 called_thing = "constructor";
2788 else
2789 called_thing = "member function";
2790 }
2791 else
2792 called_thing = "function";
2793 }
2794
2795 for (valtail = values, typetail = typelist;
2796 valtail;
2797 valtail = TREE_CHAIN (valtail), i++)
2798 {
2799 register tree type = typetail ? TREE_VALUE (typetail) : 0;
2800 register tree val = TREE_VALUE (valtail);
2801
2802 if (val == error_mark_node)
2803 return error_mark_node;
2804
2805 if (type == void_type_node)
2806 {
2807 if (fndecl)
2808 {
2809 cp_error_at ("too many arguments to %s `%+D'", called_thing,
2810 fndecl);
2811 error ("at this point in file");
2812 }
2813 else
2814 error ("too many arguments to function");
2815 /* In case anybody wants to know if this argument
2816 list is valid. */
2817 if (result)
2818 TREE_TYPE (tree_last (result)) = error_mark_node;
2819 break;
2820 }
2821
2822 /* The tree type of the parameter being passed may not yet be
2823 known. In this case, its type is TYPE_UNKNOWN, and will
2824 be instantiated by the type given by TYPE. If TYPE
2825 is also NULL, the tree type of VAL is ERROR_MARK_NODE. */
2826 if (type && type_unknown_p (val))
2827 val = require_instantiated_type (type, val, integer_zero_node);
2828 else if (type_unknown_p (val))
2829 {
2830 /* Strip the `&' from an overloaded FUNCTION_DECL. */
2831 if (TREE_CODE (val) == ADDR_EXPR)
2832 val = TREE_OPERAND (val, 0);
2833 if (really_overloaded_fn (val))
2834 cp_error ("insufficient type information to resolve address of overloaded function `%D'",
2835 DECL_NAME (get_first_fn (val)));
2836 else
2837 error ("insufficient type information in parameter list");
2838 val = integer_zero_node;
2839 }
2840 else if (TREE_CODE (val) == OFFSET_REF
2841 && TREE_CODE (TREE_TYPE (val)) == METHOD_TYPE)
2842 {
2843 /* This is unclean. Should be handled elsewhere. */
2844 val = build_unary_op (ADDR_EXPR, val, 0);
2845 }
2846 else if (TREE_CODE (val) == OFFSET_REF)
2847 val = resolve_offset_ref (val);
2848
2849 /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
2850 Strip such NOP_EXPRs, since VAL is used in non-lvalue context. */
2851 if (TREE_CODE (val) == NOP_EXPR
2852 && TREE_TYPE (val) == TREE_TYPE (TREE_OPERAND (val, 0))
2853 && (type == 0 || TREE_CODE (type) != REFERENCE_TYPE))
2854 val = TREE_OPERAND (val, 0);
2855
2856 if (type == 0 || TREE_CODE (type) != REFERENCE_TYPE)
2857 {
2858 if (TREE_CODE (TREE_TYPE (val)) == ARRAY_TYPE
2859 || TREE_CODE (TREE_TYPE (val)) == FUNCTION_TYPE
2860 || TREE_CODE (TREE_TYPE (val)) == METHOD_TYPE)
2861 val = default_conversion (val);
2862
2863 val = require_complete_type (val);
2864 }
2865
2866 if (val == error_mark_node)
2867 return error_mark_node;
2868
2869 if (type != 0)
2870 {
2871 /* Formal parm type is specified by a function prototype. */
2872 tree parmval;
2873
2874 if (TYPE_SIZE (complete_type (type)) == 0)
2875 {
2876 error ("parameter type of called function is incomplete");
2877 parmval = val;
2878 }
2879 else
2880 {
2881 parmval = convert_for_initialization
2882 (return_loc, type, val, flags,
2883 "argument passing", fndecl, i);
2884 #ifdef PROMOTE_PROTOTYPES
2885 if ((TREE_CODE (type) == INTEGER_TYPE
2886 || TREE_CODE (type) == ENUMERAL_TYPE)
2887 && (TYPE_PRECISION (type) < TYPE_PRECISION (integer_type_node)))
2888 parmval = default_conversion (parmval);
2889 #endif
2890 }
2891
2892 if (parmval == error_mark_node)
2893 return error_mark_node;
2894
2895 result = expr_tree_cons (NULL_TREE, parmval, result);
2896 }
2897 else
2898 {
2899 if (TREE_CODE (TREE_TYPE (val)) == REFERENCE_TYPE)
2900 val = convert_from_reference (val);
2901
2902 if (TREE_CODE (TREE_TYPE (val)) == REAL_TYPE
2903 && (TYPE_PRECISION (TREE_TYPE (val))
2904 < TYPE_PRECISION (double_type_node)))
2905 /* Convert `float' to `double'. */
2906 result = expr_tree_cons (NULL_TREE, cp_convert (double_type_node, val), result);
2907 else if (TYPE_LANG_SPECIFIC (TREE_TYPE (val))
2908 && ! TYPE_HAS_TRIVIAL_INIT_REF (TREE_TYPE (val)))
2909 {
2910 cp_warning ("cannot pass objects of type `%T' through `...'",
2911 TREE_TYPE (val));
2912 result = expr_tree_cons (NULL_TREE, val, result);
2913 }
2914 else
2915 /* Convert `short' and `char' to full-size `int'. */
2916 result = expr_tree_cons (NULL_TREE, default_conversion (val), result);
2917 }
2918
2919 if (typetail)
2920 typetail = TREE_CHAIN (typetail);
2921 }
2922
2923 if (typetail != 0 && typetail != void_list_node)
2924 {
2925 /* See if there are default arguments that can be used */
2926 if (TREE_PURPOSE (typetail))
2927 {
2928 for (; typetail != void_list_node; ++i)
2929 {
2930 tree type = TREE_VALUE (typetail);
2931 tree val = break_out_target_exprs (TREE_PURPOSE (typetail));
2932 tree parmval;
2933
2934 if (val == NULL_TREE)
2935 parmval = error_mark_node;
2936 else if (TREE_CODE (val) == CONSTRUCTOR)
2937 {
2938 parmval = digest_init (type, val, (tree *)0);
2939 parmval = convert_for_initialization (return_loc, type, parmval, flags,
2940 "default constructor", fndecl, i);
2941 }
2942 else
2943 {
2944 /* This could get clobbered by the following call. */
2945 if (TREE_HAS_CONSTRUCTOR (val))
2946 val = copy_node (val);
2947
2948 parmval = convert_for_initialization (return_loc, type, val, flags,
2949 "default argument", fndecl, i);
2950 #ifdef PROMOTE_PROTOTYPES
2951 if ((TREE_CODE (type) == INTEGER_TYPE
2952 || TREE_CODE (type) == ENUMERAL_TYPE)
2953 && (TYPE_PRECISION (type) < TYPE_PRECISION (integer_type_node)))
2954 parmval = default_conversion (parmval);
2955 #endif
2956 }
2957
2958 if (parmval == error_mark_node)
2959 return error_mark_node;
2960
2961 result = expr_tree_cons (0, parmval, result);
2962 typetail = TREE_CHAIN (typetail);
2963 /* ends with `...'. */
2964 if (typetail == NULL_TREE)
2965 break;
2966 }
2967 }
2968 else
2969 {
2970 if (fndecl)
2971 {
2972 char *buf = (char *)alloca (32 + strlen (called_thing));
2973 sprintf (buf, "too few arguments to %s `%%#D'", called_thing);
2974 cp_error_at (buf, fndecl);
2975 error ("at this point in file");
2976 }
2977 else
2978 error ("too few arguments to function");
2979 return error_mark_list;
2980 }
2981 }
2982
2983 return nreverse (result);
2984 }
2985 \f
2986 /* Build a binary-operation expression, after performing default
2987 conversions on the operands. CODE is the kind of expression to build. */
2988
2989 tree
2990 build_x_binary_op (code, arg1, arg2)
2991 enum tree_code code;
2992 tree arg1, arg2;
2993 {
2994 tree rval;
2995
2996 if (processing_template_decl)
2997 return build_min_nt (code, arg1, arg2);
2998
2999 if (flag_ansi_overloading)
3000 return build_new_op (code, LOOKUP_NORMAL, arg1, arg2, NULL_TREE);
3001
3002 rval = build_opfncall (code, LOOKUP_SPECULATIVELY,
3003 arg1, arg2, NULL_TREE);
3004 if (rval)
3005 return build_opfncall (code, LOOKUP_NORMAL, arg1, arg2, NULL_TREE);
3006 if (code == MEMBER_REF)
3007 return build_m_component_ref (build_indirect_ref (arg1, NULL_PTR),
3008 arg2);
3009 return build_binary_op (code, arg1, arg2, 1);
3010 }
3011
3012 tree
3013 build_binary_op (code, arg1, arg2, convert_p)
3014 enum tree_code code;
3015 tree arg1, arg2;
3016 int convert_p;
3017 {
3018 tree args[2];
3019
3020 args[0] = arg1;
3021 args[1] = arg2;
3022
3023 if (convert_p)
3024 {
3025 tree type0, type1;
3026 args[0] = decay_conversion (args[0]);
3027 args[1] = decay_conversion (args[1]);
3028
3029 if (args[0] == error_mark_node || args[1] == error_mark_node)
3030 return error_mark_node;
3031
3032 type0 = TREE_TYPE (args[0]);
3033 type1 = TREE_TYPE (args[1]);
3034
3035 if (type_unknown_p (args[0]))
3036 {
3037 args[0] = instantiate_type (type1, args[0], 1);
3038 args[0] = decay_conversion (args[0]);
3039 }
3040 else if (type_unknown_p (args[1]))
3041 {
3042 args[1] = require_instantiated_type (type0, args[1],
3043 error_mark_node);
3044 args[1] = decay_conversion (args[1]);
3045 }
3046
3047 if (IS_AGGR_TYPE (type0) || IS_AGGR_TYPE (type1))
3048 {
3049 /* Try to convert this to something reasonable. */
3050 if (! build_default_binary_type_conversion(code, &args[0], &args[1]))
3051 {
3052 cp_error ("no match for `%O(%#T, %#T)'", code,
3053 TREE_TYPE (arg1), TREE_TYPE (arg2));
3054 return error_mark_node;
3055 }
3056 }
3057 }
3058 return build_binary_op_nodefault (code, args[0], args[1], code);
3059 }
3060
3061 /* Build a binary-operation expression without default conversions.
3062 CODE is the kind of expression to build.
3063 This function differs from `build' in several ways:
3064 the data type of the result is computed and recorded in it,
3065 warnings are generated if arg data types are invalid,
3066 special handling for addition and subtraction of pointers is known,
3067 and some optimization is done (operations on narrow ints
3068 are done in the narrower type when that gives the same result).
3069 Constant folding is also done before the result is returned.
3070
3071 ERROR_CODE is the code that determines what to say in error messages.
3072 It is usually, but not always, the same as CODE.
3073
3074 Note that the operands will never have enumeral types
3075 because either they have just had the default conversions performed
3076 or they have both just been converted to some other type in which
3077 the arithmetic is to be done.
3078
3079 C++: must do special pointer arithmetic when implementing
3080 multiple inheritance, and deal with pointer to member functions. */
3081
3082 tree
3083 build_binary_op_nodefault (code, orig_op0, orig_op1, error_code)
3084 enum tree_code code;
3085 tree orig_op0, orig_op1;
3086 enum tree_code error_code;
3087 {
3088 tree op0, op1;
3089 register enum tree_code code0, code1;
3090 tree type0, type1;
3091
3092 /* Expression code to give to the expression when it is built.
3093 Normally this is CODE, which is what the caller asked for,
3094 but in some special cases we change it. */
3095 register enum tree_code resultcode = code;
3096
3097 /* Data type in which the computation is to be performed.
3098 In the simplest cases this is the common type of the arguments. */
3099 register tree result_type = NULL;
3100
3101 /* Nonzero means operands have already been type-converted
3102 in whatever way is necessary.
3103 Zero means they need to be converted to RESULT_TYPE. */
3104 int converted = 0;
3105
3106 /* Nonzero means create the expression with this type, rather than
3107 RESULT_TYPE. */
3108 tree build_type = 0;
3109
3110 /* Nonzero means after finally constructing the expression
3111 convert it to this type. */
3112 tree final_type = 0;
3113
3114 /* Nonzero if this is an operation like MIN or MAX which can
3115 safely be computed in short if both args are promoted shorts.
3116 Also implies COMMON.
3117 -1 indicates a bitwise operation; this makes a difference
3118 in the exact conditions for when it is safe to do the operation
3119 in a narrower mode. */
3120 int shorten = 0;
3121
3122 /* Nonzero if this is a comparison operation;
3123 if both args are promoted shorts, compare the original shorts.
3124 Also implies COMMON. */
3125 int short_compare = 0;
3126
3127 /* Nonzero if this is a right-shift operation, which can be computed on the
3128 original short and then promoted if the operand is a promoted short. */
3129 int short_shift = 0;
3130
3131 /* Nonzero means set RESULT_TYPE to the common type of the args. */
3132 int common = 0;
3133
3134 /* Apply default conversions. */
3135 if (code == TRUTH_AND_EXPR || code == TRUTH_ANDIF_EXPR
3136 || code == TRUTH_OR_EXPR || code == TRUTH_ORIF_EXPR
3137 || code == TRUTH_XOR_EXPR)
3138 {
3139 op0 = decay_conversion (orig_op0);
3140 op1 = decay_conversion (orig_op1);
3141 }
3142 else
3143 {
3144 op0 = default_conversion (orig_op0);
3145 op1 = default_conversion (orig_op1);
3146 }
3147
3148 type0 = TREE_TYPE (op0);
3149 type1 = TREE_TYPE (op1);
3150
3151 /* The expression codes of the data types of the arguments tell us
3152 whether the arguments are integers, floating, pointers, etc. */
3153 code0 = TREE_CODE (type0);
3154 code1 = TREE_CODE (type1);
3155
3156 /* Strip NON_LVALUE_EXPRs, etc., since we aren't using as an lvalue. */
3157 STRIP_TYPE_NOPS (op0);
3158 STRIP_TYPE_NOPS (op1);
3159
3160 /* If an error was already reported for one of the arguments,
3161 avoid reporting another error. */
3162
3163 if (code0 == ERROR_MARK || code1 == ERROR_MARK)
3164 return error_mark_node;
3165
3166 switch (code)
3167 {
3168 case PLUS_EXPR:
3169 /* Handle the pointer + int case. */
3170 if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
3171 return pointer_int_sum (PLUS_EXPR, op0, op1);
3172 else if (code1 == POINTER_TYPE && code0 == INTEGER_TYPE)
3173 return pointer_int_sum (PLUS_EXPR, op1, op0);
3174 else
3175 common = 1;
3176 break;
3177
3178 case MINUS_EXPR:
3179 /* Subtraction of two similar pointers.
3180 We must subtract them as integers, then divide by object size. */
3181 if (code0 == POINTER_TYPE && code1 == POINTER_TYPE
3182 && comp_target_types (type0, type1, 1))
3183 return pointer_diff (op0, op1);
3184 /* Handle pointer minus int. Just like pointer plus int. */
3185 else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
3186 return pointer_int_sum (MINUS_EXPR, op0, op1);
3187 else
3188 common = 1;
3189 break;
3190
3191 case MULT_EXPR:
3192 common = 1;
3193 break;
3194
3195 case TRUNC_DIV_EXPR:
3196 case CEIL_DIV_EXPR:
3197 case FLOOR_DIV_EXPR:
3198 case ROUND_DIV_EXPR:
3199 case EXACT_DIV_EXPR:
3200 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE
3201 || code0 == COMPLEX_TYPE)
3202 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
3203 || code1 == COMPLEX_TYPE))
3204 {
3205 if (TREE_CODE (op1) == INTEGER_CST && integer_zerop (op1))
3206 cp_warning ("division by zero in `%E / 0'", op0);
3207 else if (TREE_CODE (op1) == REAL_CST && real_zerop (op1))
3208 cp_warning ("division by zero in `%E / 0.'", op0);
3209
3210 if (!(code0 == INTEGER_TYPE && code1 == INTEGER_TYPE))
3211 resultcode = RDIV_EXPR;
3212 else
3213 /* When dividing two signed integers, we have to promote to int.
3214 unless we divide by a constant != -1. Note that default
3215 conversion will have been performed on the operands at this
3216 point, so we have to dig out the original type to find out if
3217 it was unsigned. */
3218 shorten = ((TREE_CODE (op0) == NOP_EXPR
3219 && TREE_UNSIGNED (TREE_TYPE (TREE_OPERAND (op0, 0))))
3220 || (TREE_CODE (op1) == INTEGER_CST
3221 && (TREE_INT_CST_LOW (op1) != -1
3222 || TREE_INT_CST_HIGH (op1) != -1)));
3223 common = 1;
3224 }
3225 break;
3226
3227 case BIT_AND_EXPR:
3228 case BIT_ANDTC_EXPR:
3229 case BIT_IOR_EXPR:
3230 case BIT_XOR_EXPR:
3231 if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
3232 shorten = -1;
3233 /* If one operand is a constant, and the other is a short type
3234 that has been converted to an int,
3235 really do the work in the short type and then convert the
3236 result to int. If we are lucky, the constant will be 0 or 1
3237 in the short type, making the entire operation go away. */
3238 if (TREE_CODE (op0) == INTEGER_CST
3239 && TREE_CODE (op1) == NOP_EXPR
3240 && TYPE_PRECISION (type1) > TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (op1, 0)))
3241 && TREE_UNSIGNED (TREE_TYPE (TREE_OPERAND (op1, 0))))
3242 {
3243 final_type = result_type;
3244 op1 = TREE_OPERAND (op1, 0);
3245 result_type = TREE_TYPE (op1);
3246 }
3247 if (TREE_CODE (op1) == INTEGER_CST
3248 && TREE_CODE (op0) == NOP_EXPR
3249 && TYPE_PRECISION (type0) > TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (op0, 0)))
3250 && TREE_UNSIGNED (TREE_TYPE (TREE_OPERAND (op0, 0))))
3251 {
3252 final_type = result_type;
3253 op0 = TREE_OPERAND (op0, 0);
3254 result_type = TREE_TYPE (op0);
3255 }
3256 break;
3257
3258 case TRUNC_MOD_EXPR:
3259 case FLOOR_MOD_EXPR:
3260 if (code1 == INTEGER_TYPE && integer_zerop (op1))
3261 cp_warning ("division by zero in `%E % 0'", op0);
3262 else if (code1 == REAL_TYPE && real_zerop (op1))
3263 cp_warning ("division by zero in `%E % 0.'", op0);
3264
3265 if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
3266 {
3267 /* Although it would be tempting to shorten always here, that loses
3268 on some targets, since the modulo instruction is undefined if the
3269 quotient can't be represented in the computation mode. We shorten
3270 only if unsigned or if dividing by something we know != -1. */
3271 shorten = ((TREE_CODE (op0) == NOP_EXPR
3272 && TREE_UNSIGNED (TREE_TYPE (TREE_OPERAND (op0, 0))))
3273 || (TREE_CODE (op1) == INTEGER_CST
3274 && (TREE_INT_CST_LOW (op1) != -1
3275 || TREE_INT_CST_HIGH (op1) != -1)));
3276 common = 1;
3277 }
3278 break;
3279
3280 case TRUTH_ANDIF_EXPR:
3281 case TRUTH_ORIF_EXPR:
3282 case TRUTH_AND_EXPR:
3283 case TRUTH_OR_EXPR:
3284 result_type = boolean_type_node;
3285 break;
3286
3287 /* Shift operations: result has same type as first operand;
3288 always convert second operand to int.
3289 Also set SHORT_SHIFT if shifting rightward. */
3290
3291 case RSHIFT_EXPR:
3292 if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
3293 {
3294 result_type = type0;
3295 if (TREE_CODE (op1) == INTEGER_CST)
3296 {
3297 if (tree_int_cst_lt (op1, integer_zero_node))
3298 warning ("right shift count is negative");
3299 else
3300 {
3301 if (TREE_INT_CST_LOW (op1) | TREE_INT_CST_HIGH (op1))
3302 short_shift = 1;
3303 if (TREE_INT_CST_HIGH (op1) != 0
3304 || ((unsigned HOST_WIDE_INT) TREE_INT_CST_LOW (op1)
3305 >= TYPE_PRECISION (type0)))
3306 warning ("right shift count >= width of type");
3307 }
3308 }
3309 /* Convert the shift-count to an integer, regardless of
3310 size of value being shifted. */
3311 if (TYPE_MAIN_VARIANT (TREE_TYPE (op1)) != integer_type_node)
3312 op1 = cp_convert (integer_type_node, op1);
3313 /* Avoid converting op1 to result_type later. */
3314 converted = 1;
3315 }
3316 break;
3317
3318 case LSHIFT_EXPR:
3319 if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
3320 {
3321 result_type = type0;
3322 if (TREE_CODE (op1) == INTEGER_CST)
3323 {
3324 if (tree_int_cst_lt (op1, integer_zero_node))
3325 warning ("left shift count is negative");
3326 else if (TREE_INT_CST_HIGH (op1) != 0
3327 || ((unsigned HOST_WIDE_INT) TREE_INT_CST_LOW (op1)
3328 >= TYPE_PRECISION (type0)))
3329 warning ("left shift count >= width of type");
3330 }
3331 /* Convert the shift-count to an integer, regardless of
3332 size of value being shifted. */
3333 if (TYPE_MAIN_VARIANT (TREE_TYPE (op1)) != integer_type_node)
3334 op1 = cp_convert (integer_type_node, op1);
3335 /* Avoid converting op1 to result_type later. */
3336 converted = 1;
3337 }
3338 break;
3339
3340 case RROTATE_EXPR:
3341 case LROTATE_EXPR:
3342 if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
3343 {
3344 result_type = type0;
3345 if (TREE_CODE (op1) == INTEGER_CST)
3346 {
3347 if (tree_int_cst_lt (op1, integer_zero_node))
3348 warning ("%s rotate count is negative",
3349 (code == LROTATE_EXPR) ? "left" : "right");
3350 else if (TREE_INT_CST_HIGH (op1) != 0
3351 || ((unsigned HOST_WIDE_INT) TREE_INT_CST_LOW (op1)
3352 >= TYPE_PRECISION (type0)))
3353 warning ("%s rotate count >= width of type",
3354 (code == LROTATE_EXPR) ? "left" : "right");
3355 }
3356 /* Convert the shift-count to an integer, regardless of
3357 size of value being shifted. */
3358 if (TYPE_MAIN_VARIANT (TREE_TYPE (op1)) != integer_type_node)
3359 op1 = cp_convert (integer_type_node, op1);
3360 }
3361 break;
3362
3363 case EQ_EXPR:
3364 case NE_EXPR:
3365 build_type = boolean_type_node;
3366 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE
3367 || code0 == COMPLEX_TYPE)
3368 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
3369 || code1 == COMPLEX_TYPE))
3370 short_compare = 1;
3371 else if (code0 == POINTER_TYPE && code1 == POINTER_TYPE)
3372 {
3373 register tree tt0 = TYPE_MAIN_VARIANT (TREE_TYPE (type0));
3374 register tree tt1 = TYPE_MAIN_VARIANT (TREE_TYPE (type1));
3375
3376 if (comp_target_types (type0, type1, 1))
3377 result_type = common_type (type0, type1);
3378 else if (tt0 == void_type_node)
3379 {
3380 if (pedantic && TREE_CODE (tt1) == FUNCTION_TYPE
3381 && tree_int_cst_lt (TYPE_SIZE (type0), TYPE_SIZE (type1)))
3382 pedwarn ("ANSI C++ forbids comparison of `void *' with function pointer");
3383 else if (TREE_CODE (tt1) == OFFSET_TYPE)
3384 pedwarn ("ANSI C++ forbids conversion of a pointer to member to `void *'");
3385 }
3386 else if (tt1 == void_type_node)
3387 {
3388 if (pedantic && TREE_CODE (tt0) == FUNCTION_TYPE
3389 && tree_int_cst_lt (TYPE_SIZE (type1), TYPE_SIZE (type0)))
3390 pedwarn ("ANSI C++ forbids comparison of `void *' with function pointer");
3391 }
3392 else
3393 cp_pedwarn ("comparison of distinct pointer types `%T' and `%T' lacks a cast",
3394 type0, type1);
3395
3396 if (result_type == NULL_TREE)
3397 result_type = ptr_type_node;
3398 }
3399 else if (code0 == POINTER_TYPE && TREE_CODE (op1) == INTEGER_CST
3400 && integer_zerop (op1))
3401 result_type = type0;
3402 else if (code1 == POINTER_TYPE && TREE_CODE (op0) == INTEGER_CST
3403 && integer_zerop (op0))
3404 result_type = type1;
3405 else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
3406 {
3407 result_type = type0;
3408 error ("ANSI C++ forbids comparison between pointer and integer");
3409 }
3410 else if (code0 == INTEGER_TYPE && code1 == POINTER_TYPE)
3411 {
3412 result_type = type1;
3413 error ("ANSI C++ forbids comparison between pointer and integer");
3414 }
3415 else if (TYPE_PTRMEMFUNC_P (type0) && TREE_CODE (op1) == INTEGER_CST
3416 && integer_zerop (op1))
3417 {
3418 op0 = build_component_ref (op0, index_identifier, NULL_TREE, 0);
3419 op1 = integer_zero_node;
3420 result_type = TREE_TYPE (op0);
3421 }
3422 else if (TYPE_PTRMEMFUNC_P (type1) && TREE_CODE (op0) == INTEGER_CST
3423 && integer_zerop (op0))
3424 {
3425 op0 = build_component_ref (op1, index_identifier, NULL_TREE, 0);
3426 op1 = integer_zero_node;
3427 result_type = TREE_TYPE (op0);
3428 }
3429 else if (TYPE_PTRMEMFUNC_P (type0) && TYPE_PTRMEMFUNC_P (type1)
3430 && (TYPE_PTRMEMFUNC_FN_TYPE (type0)
3431 == TYPE_PTRMEMFUNC_FN_TYPE (type1)))
3432 {
3433 /* The code we generate for the test is:
3434
3435 (op0.index == op1.index
3436 && ((op1.index != -1 && op0.delta2 == op1.delta2)
3437 || op0.pfn == op1.pfn)) */
3438
3439 tree index0 = build_component_ref (op0, index_identifier, NULL_TREE, 0);
3440 tree index1 = save_expr (build_component_ref (op1, index_identifier, NULL_TREE, 0));
3441 tree pfn0 = PFN_FROM_PTRMEMFUNC (op0);
3442 tree pfn1 = PFN_FROM_PTRMEMFUNC (op1);
3443 tree delta20 = DELTA2_FROM_PTRMEMFUNC (op0);
3444 tree delta21 = DELTA2_FROM_PTRMEMFUNC (op1);
3445 tree e1, e2, e3;
3446 tree integer_neg_one_node
3447 = build_binary_op (MINUS_EXPR, integer_zero_node, integer_one_node, 1);
3448 e1 = build_binary_op (EQ_EXPR, index0, index1, 1);
3449 e2 = build_binary_op (NE_EXPR, index1, integer_neg_one_node, 1);
3450 e2 = build_binary_op (TRUTH_ANDIF_EXPR, e2, build_binary_op (EQ_EXPR, delta20, delta21, 1), 1);
3451 e3 = build_binary_op (EQ_EXPR, pfn0, pfn1, 1);
3452 e2 = build_binary_op (TRUTH_ORIF_EXPR, e2, e3, 1);
3453 e2 = build_binary_op (TRUTH_ANDIF_EXPR, e1, e2, 1);
3454 if (code == EQ_EXPR)
3455 return e2;
3456 return build_binary_op (EQ_EXPR, e2, integer_zero_node, 1);
3457 }
3458 else if (TYPE_PTRMEMFUNC_P (type0)
3459 && TYPE_PTRMEMFUNC_FN_TYPE (type0) == type1)
3460 {
3461 tree index0 = build_component_ref (op0, index_identifier, NULL_TREE, 0);
3462 tree index1;
3463 tree pfn0 = PFN_FROM_PTRMEMFUNC (op0);
3464 tree delta20 = DELTA2_FROM_PTRMEMFUNC (op0);
3465 tree delta21 = integer_zero_node;
3466 tree e1, e2, e3;
3467 tree integer_neg_one_node
3468 = build_binary_op (MINUS_EXPR, integer_zero_node, integer_one_node, 1);
3469 if (TREE_CODE (TREE_OPERAND (op1, 0)) == FUNCTION_DECL
3470 && DECL_VINDEX (TREE_OPERAND (op1, 0)))
3471 {
3472 /* Map everything down one to make room for the null pointer to member. */
3473 index1 = size_binop (PLUS_EXPR,
3474 DECL_VINDEX (TREE_OPERAND (op1, 0)),
3475 integer_one_node);
3476 op1 = integer_zero_node;
3477 delta21 = CLASSTYPE_VFIELD (TYPE_METHOD_BASETYPE (TREE_TYPE (type1)));
3478 delta21 = DECL_FIELD_BITPOS (delta21);
3479 delta21 = size_binop (FLOOR_DIV_EXPR, delta21, size_int (BITS_PER_UNIT));
3480 }
3481 else
3482 index1 = integer_neg_one_node;
3483 {
3484 tree nop1 = build1 (NOP_EXPR, TYPE_PTRMEMFUNC_FN_TYPE (type0), op1);
3485 TREE_CONSTANT (nop1) = TREE_CONSTANT (op1);
3486 op1 = nop1;
3487 }
3488 e1 = build_binary_op (EQ_EXPR, index0, index1, 1);
3489 e2 = build_binary_op (NE_EXPR, index1, integer_neg_one_node, 1);
3490 e2 = build_binary_op (TRUTH_ANDIF_EXPR, e2, build_binary_op (EQ_EXPR, delta20, delta21, 1), 1);
3491 e3 = build_binary_op (EQ_EXPR, pfn0, op1, 1);
3492 e2 = build_binary_op (TRUTH_ORIF_EXPR, e2, e3, 1);
3493 e2 = build_binary_op (TRUTH_ANDIF_EXPR, e1, e2, 1);
3494 if (code == EQ_EXPR)
3495 return e2;
3496 return build_binary_op (EQ_EXPR, e2, integer_zero_node, 1);
3497 }
3498 else if (TYPE_PTRMEMFUNC_P (type1)
3499 && TYPE_PTRMEMFUNC_FN_TYPE (type1) == type0)
3500 {
3501 return build_binary_op (code, op1, op0, 1);
3502 }
3503 break;
3504
3505 case MAX_EXPR:
3506 case MIN_EXPR:
3507 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE)
3508 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE))
3509 shorten = 1;
3510 else if (code0 == POINTER_TYPE && code1 == POINTER_TYPE)
3511 {
3512 if (comp_target_types (type0, type1, 1))
3513 result_type = common_type (type0, type1);
3514 else
3515 {
3516 cp_pedwarn ("comparison of distinct pointer types `%T' and `%T' lacks a cast",
3517 type0, type1);
3518 result_type = ptr_type_node;
3519 }
3520 }
3521 break;
3522
3523 case LE_EXPR:
3524 case GE_EXPR:
3525 case LT_EXPR:
3526 case GT_EXPR:
3527 build_type = boolean_type_node;
3528 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE)
3529 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE))
3530 short_compare = 1;
3531 else if (code0 == POINTER_TYPE && code1 == POINTER_TYPE)
3532 {
3533 if (comp_target_types (type0, type1, 1))
3534 result_type = common_type (type0, type1);
3535 else
3536 {
3537 cp_pedwarn ("comparison of distinct pointer types `%T' and `%T' lacks a cast",
3538 type0, type1);
3539 result_type = ptr_type_node;
3540 }
3541 }
3542 else if (code0 == POINTER_TYPE && TREE_CODE (op1) == INTEGER_CST
3543 && integer_zerop (op1))
3544 result_type = type0;
3545 else if (code1 == POINTER_TYPE && TREE_CODE (op0) == INTEGER_CST
3546 && integer_zerop (op0))
3547 result_type = type1;
3548 else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
3549 {
3550 result_type = type0;
3551 pedwarn ("ANSI C++ forbids comparison between pointer and integer");
3552 }
3553 else if (code0 == INTEGER_TYPE && code1 == POINTER_TYPE)
3554 {
3555 result_type = type1;
3556 pedwarn ("ANSI C++ forbids comparison between pointer and integer");
3557 }
3558 break;
3559 }
3560
3561 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE || code0 == COMPLEX_TYPE)
3562 &&
3563 (code1 == INTEGER_TYPE || code1 == REAL_TYPE || code1 == COMPLEX_TYPE))
3564 {
3565 int none_complex = (code0 != COMPLEX_TYPE && code1 != COMPLEX_TYPE);
3566
3567 if (shorten || common || short_compare)
3568 result_type = common_type (type0, type1);
3569
3570 /* For certain operations (which identify themselves by shorten != 0)
3571 if both args were extended from the same smaller type,
3572 do the arithmetic in that type and then extend.
3573
3574 shorten !=0 and !=1 indicates a bitwise operation.
3575 For them, this optimization is safe only if
3576 both args are zero-extended or both are sign-extended.
3577 Otherwise, we might change the result.
3578 Eg, (short)-1 | (unsigned short)-1 is (int)-1
3579 but calculated in (unsigned short) it would be (unsigned short)-1. */
3580
3581 if (shorten && none_complex)
3582 {
3583 int unsigned0, unsigned1;
3584 tree arg0 = get_narrower (op0, &unsigned0);
3585 tree arg1 = get_narrower (op1, &unsigned1);
3586 /* UNS is 1 if the operation to be done is an unsigned one. */
3587 int uns = TREE_UNSIGNED (result_type);
3588 tree type;
3589
3590 final_type = result_type;
3591
3592 /* Handle the case that OP0 does not *contain* a conversion
3593 but it *requires* conversion to FINAL_TYPE. */
3594
3595 if (op0 == arg0 && TREE_TYPE (op0) != final_type)
3596 unsigned0 = TREE_UNSIGNED (TREE_TYPE (op0));
3597 if (op1 == arg1 && TREE_TYPE (op1) != final_type)
3598 unsigned1 = TREE_UNSIGNED (TREE_TYPE (op1));
3599
3600 /* Now UNSIGNED0 is 1 if ARG0 zero-extends to FINAL_TYPE. */
3601
3602 /* For bitwise operations, signedness of nominal type
3603 does not matter. Consider only how operands were extended. */
3604 if (shorten == -1)
3605 uns = unsigned0;
3606
3607 /* Note that in all three cases below we refrain from optimizing
3608 an unsigned operation on sign-extended args.
3609 That would not be valid. */
3610
3611 /* Both args variable: if both extended in same way
3612 from same width, do it in that width.
3613 Do it unsigned if args were zero-extended. */
3614 if ((TYPE_PRECISION (TREE_TYPE (arg0))
3615 < TYPE_PRECISION (result_type))
3616 && (TYPE_PRECISION (TREE_TYPE (arg1))
3617 == TYPE_PRECISION (TREE_TYPE (arg0)))
3618 && unsigned0 == unsigned1
3619 && (unsigned0 || !uns))
3620 result_type
3621 = signed_or_unsigned_type (unsigned0,
3622 common_type (TREE_TYPE (arg0), TREE_TYPE (arg1)));
3623 else if (TREE_CODE (arg0) == INTEGER_CST
3624 && (unsigned1 || !uns)
3625 && (TYPE_PRECISION (TREE_TYPE (arg1))
3626 < TYPE_PRECISION (result_type))
3627 && (type = signed_or_unsigned_type (unsigned1,
3628 TREE_TYPE (arg1)),
3629 int_fits_type_p (arg0, type)))
3630 result_type = type;
3631 else if (TREE_CODE (arg1) == INTEGER_CST
3632 && (unsigned0 || !uns)
3633 && (TYPE_PRECISION (TREE_TYPE (arg0))
3634 < TYPE_PRECISION (result_type))
3635 && (type = signed_or_unsigned_type (unsigned0,
3636 TREE_TYPE (arg0)),
3637 int_fits_type_p (arg1, type)))
3638 result_type = type;
3639 }
3640
3641 /* Shifts can be shortened if shifting right. */
3642
3643 if (short_shift)
3644 {
3645 int unsigned_arg;
3646 tree arg0 = get_narrower (op0, &unsigned_arg);
3647
3648 final_type = result_type;
3649
3650 if (arg0 == op0 && final_type == TREE_TYPE (op0))
3651 unsigned_arg = TREE_UNSIGNED (TREE_TYPE (op0));
3652
3653 if (TYPE_PRECISION (TREE_TYPE (arg0)) < TYPE_PRECISION (result_type)
3654 /* We can shorten only if the shift count is less than the
3655 number of bits in the smaller type size. */
3656 && TREE_INT_CST_HIGH (op1) == 0
3657 && TYPE_PRECISION (TREE_TYPE (arg0)) > TREE_INT_CST_LOW (op1)
3658 /* If arg is sign-extended and then unsigned-shifted,
3659 we can simulate this with a signed shift in arg's type
3660 only if the extended result is at least twice as wide
3661 as the arg. Otherwise, the shift could use up all the
3662 ones made by sign-extension and bring in zeros.
3663 We can't optimize that case at all, but in most machines
3664 it never happens because available widths are 2**N. */
3665 && (!TREE_UNSIGNED (final_type)
3666 || unsigned_arg
3667 || (((unsigned) 2 * TYPE_PRECISION (TREE_TYPE (arg0)))
3668 <= TYPE_PRECISION (result_type))))
3669 {
3670 /* Do an unsigned shift if the operand was zero-extended. */
3671 result_type
3672 = signed_or_unsigned_type (unsigned_arg,
3673 TREE_TYPE (arg0));
3674 /* Convert value-to-be-shifted to that type. */
3675 if (TREE_TYPE (op0) != result_type)
3676 op0 = cp_convert (result_type, op0);
3677 converted = 1;
3678 }
3679 }
3680
3681 /* Comparison operations are shortened too but differently.
3682 They identify themselves by setting short_compare = 1. */
3683
3684 if (short_compare)
3685 {
3686 /* Don't write &op0, etc., because that would prevent op0
3687 from being kept in a register.
3688 Instead, make copies of the our local variables and
3689 pass the copies by reference, then copy them back afterward. */
3690 tree xop0 = op0, xop1 = op1, xresult_type = result_type;
3691 enum tree_code xresultcode = resultcode;
3692 tree val
3693 = shorten_compare (&xop0, &xop1, &xresult_type, &xresultcode);
3694 if (val != 0)
3695 return cp_convert (boolean_type_node, val);
3696 op0 = xop0, op1 = xop1;
3697 converted = 1;
3698 resultcode = xresultcode;
3699 }
3700
3701 if (short_compare && warn_sign_compare)
3702 {
3703 int op0_signed = ! TREE_UNSIGNED (TREE_TYPE (orig_op0));
3704 int op1_signed = ! TREE_UNSIGNED (TREE_TYPE (orig_op1));
3705
3706 int unsignedp0, unsignedp1;
3707 tree primop0 = get_narrower (op0, &unsignedp0);
3708 tree primop1 = get_narrower (op1, &unsignedp1);
3709
3710 /* Check for comparison of different enum types. */
3711 if (flag_int_enum_equivalence == 0
3712 && TREE_CODE (TREE_TYPE (orig_op0)) == ENUMERAL_TYPE
3713 && TREE_CODE (TREE_TYPE (orig_op1)) == ENUMERAL_TYPE
3714 && TYPE_MAIN_VARIANT (TREE_TYPE (orig_op0))
3715 != TYPE_MAIN_VARIANT (TREE_TYPE (orig_op1)))
3716 {
3717 cp_warning ("comparison between `%#T' and `%#T'",
3718 TREE_TYPE (orig_op0), TREE_TYPE (orig_op1));
3719 }
3720
3721 /* Give warnings for comparisons between signed and unsigned
3722 quantities that may fail. */
3723 /* Do the checking based on the original operand trees, so that
3724 casts will be considered, but default promotions won't be. */
3725
3726 /* Do not warn if the comparison is being done in a signed type,
3727 since the signed type will only be chosen if it can represent
3728 all the values of the unsigned type. */
3729 if (! TREE_UNSIGNED (result_type))
3730 /* OK */;
3731 /* Do not warn if both operands are unsigned. */
3732 else if (op0_signed == op1_signed)
3733 /* OK */;
3734 /* Do not warn if the signed quantity is an unsuffixed
3735 integer literal (or some static constant expression
3736 involving such literals) and it is non-negative. */
3737 else if ((op0_signed && TREE_CODE (orig_op0) == INTEGER_CST
3738 && tree_int_cst_sgn (orig_op0) >= 0)
3739 || (op1_signed && TREE_CODE (orig_op1) == INTEGER_CST
3740 && tree_int_cst_sgn (orig_op1) >= 0))
3741 /* OK */;
3742 /* Do not warn if the comparison is an equality operation,
3743 the unsigned quantity is an integral constant and it does
3744 not use the most significant bit of result_type. */
3745 else if ((resultcode == EQ_EXPR || resultcode == NE_EXPR)
3746 && ((op0_signed && TREE_CODE (orig_op1) == INTEGER_CST
3747 && int_fits_type_p (orig_op1, signed_type (result_type))
3748 || (op1_signed && TREE_CODE (orig_op0) == INTEGER_CST
3749 && int_fits_type_p (orig_op0, signed_type (result_type))))))
3750 /* OK */;
3751 else
3752 warning ("comparison between signed and unsigned");
3753
3754 /* Warn if two unsigned values are being compared in a size
3755 larger than their original size, and one (and only one) is the
3756 result of a `~' operator. This comparison will always fail.
3757
3758 Also warn if one operand is a constant, and the constant does not
3759 have all bits set that are set in the ~ operand when it is
3760 extended. */
3761
3762 if ((TREE_CODE (primop0) == BIT_NOT_EXPR)
3763 ^ (TREE_CODE (primop1) == BIT_NOT_EXPR))
3764 {
3765 if (TREE_CODE (primop0) == BIT_NOT_EXPR)
3766 primop0 = get_narrower (TREE_OPERAND (op0, 0), &unsignedp0);
3767 if (TREE_CODE (primop1) == BIT_NOT_EXPR)
3768 primop1 = get_narrower (TREE_OPERAND (op1, 0), &unsignedp1);
3769
3770 if (TREE_CODE (primop0) == INTEGER_CST
3771 || TREE_CODE (primop1) == INTEGER_CST)
3772 {
3773 tree primop;
3774 HOST_WIDE_INT constant, mask;
3775 int unsignedp;
3776 unsigned bits;
3777
3778 if (TREE_CODE (primop0) == INTEGER_CST)
3779 {
3780 primop = primop1;
3781 unsignedp = unsignedp1;
3782 constant = TREE_INT_CST_LOW (primop0);
3783 }
3784 else
3785 {
3786 primop = primop0;
3787 unsignedp = unsignedp0;
3788 constant = TREE_INT_CST_LOW (primop1);
3789 }
3790
3791 bits = TYPE_PRECISION (TREE_TYPE (primop));
3792 if (bits < TYPE_PRECISION (result_type)
3793 && bits < HOST_BITS_PER_LONG && unsignedp)
3794 {
3795 mask = (~ (HOST_WIDE_INT) 0) << bits;
3796 if ((mask & constant) != mask)
3797 warning ("comparison of promoted ~unsigned with constant");
3798 }
3799 }
3800 else if (unsignedp0 && unsignedp1
3801 && (TYPE_PRECISION (TREE_TYPE (primop0))
3802 < TYPE_PRECISION (result_type))
3803 && (TYPE_PRECISION (TREE_TYPE (primop1))
3804 < TYPE_PRECISION (result_type)))
3805 warning ("comparison of promoted ~unsigned with unsigned");
3806 }
3807 }
3808 }
3809
3810 /* At this point, RESULT_TYPE must be nonzero to avoid an error message.
3811 If CONVERTED is zero, both args will be converted to type RESULT_TYPE.
3812 Then the expression will be built.
3813 It will be given type FINAL_TYPE if that is nonzero;
3814 otherwise, it will be given type RESULT_TYPE. */
3815
3816 if (!result_type)
3817 {
3818 cp_error ("invalid operands `%T' and `%T' to binary `%O'",
3819 TREE_TYPE (orig_op0), TREE_TYPE (orig_op1), error_code);
3820 return error_mark_node;
3821 }
3822
3823 if (! converted)
3824 {
3825 if (TREE_TYPE (op0) != result_type)
3826 op0 = cp_convert (result_type, op0);
3827 if (TREE_TYPE (op1) != result_type)
3828 op1 = cp_convert (result_type, op1);
3829 }
3830
3831 if (build_type == NULL_TREE)
3832 build_type = result_type;
3833
3834 {
3835 register tree result = build (resultcode, build_type, op0, op1);
3836 register tree folded;
3837
3838 folded = fold (result);
3839 if (folded == result)
3840 TREE_CONSTANT (folded) = TREE_CONSTANT (op0) & TREE_CONSTANT (op1);
3841 if (final_type != 0)
3842 return cp_convert (final_type, folded);
3843 return folded;
3844 }
3845 }
3846 \f
3847 /* Return a tree for the sum or difference (RESULTCODE says which)
3848 of pointer PTROP and integer INTOP. */
3849
3850 static tree
3851 pointer_int_sum (resultcode, ptrop, intop)
3852 enum tree_code resultcode;
3853 register tree ptrop, intop;
3854 {
3855 tree size_exp;
3856
3857 register tree result;
3858 register tree folded = fold (intop);
3859
3860 /* The result is a pointer of the same type that is being added. */
3861
3862 register tree result_type = TREE_TYPE (ptrop);
3863
3864 if (TREE_CODE (TREE_TYPE (result_type)) == VOID_TYPE)
3865 {
3866 if (pedantic || warn_pointer_arith)
3867 pedwarn ("ANSI C++ forbids using pointer of type `void *' in arithmetic");
3868 size_exp = integer_one_node;
3869 }
3870 else if (TREE_CODE (TREE_TYPE (result_type)) == FUNCTION_TYPE)
3871 {
3872 if (pedantic || warn_pointer_arith)
3873 pedwarn ("ANSI C++ forbids using pointer to a function in arithmetic");
3874 size_exp = integer_one_node;
3875 }
3876 else if (TREE_CODE (TREE_TYPE (result_type)) == METHOD_TYPE)
3877 {
3878 if (pedantic || warn_pointer_arith)
3879 pedwarn ("ANSI C++ forbids using pointer to a method in arithmetic");
3880 size_exp = integer_one_node;
3881 }
3882 else if (TREE_CODE (TREE_TYPE (result_type)) == OFFSET_TYPE)
3883 {
3884 if (pedantic || warn_pointer_arith)
3885 pedwarn ("ANSI C++ forbids using pointer to a member in arithmetic");
3886 size_exp = integer_one_node;
3887 }
3888 else
3889 size_exp = size_in_bytes (complete_type (TREE_TYPE (result_type)));
3890
3891 /* Needed to make OOPS V2R3 work. */
3892 intop = folded;
3893 if (TREE_CODE (intop) == INTEGER_CST
3894 && TREE_INT_CST_LOW (intop) == 0
3895 && TREE_INT_CST_HIGH (intop) == 0)
3896 return ptrop;
3897
3898 /* If what we are about to multiply by the size of the elements
3899 contains a constant term, apply distributive law
3900 and multiply that constant term separately.
3901 This helps produce common subexpressions. */
3902
3903 if ((TREE_CODE (intop) == PLUS_EXPR || TREE_CODE (intop) == MINUS_EXPR)
3904 && ! TREE_CONSTANT (intop)
3905 && TREE_CONSTANT (TREE_OPERAND (intop, 1))
3906 && TREE_CONSTANT (size_exp))
3907 {
3908 enum tree_code subcode = resultcode;
3909 if (TREE_CODE (intop) == MINUS_EXPR)
3910 subcode = (subcode == PLUS_EXPR ? MINUS_EXPR : PLUS_EXPR);
3911 ptrop = build_binary_op (subcode, ptrop, TREE_OPERAND (intop, 1), 1);
3912 intop = TREE_OPERAND (intop, 0);
3913 }
3914
3915 /* Convert the integer argument to a type the same size as sizetype
3916 so the multiply won't overflow spuriously. */
3917
3918 if (TYPE_PRECISION (TREE_TYPE (intop)) != TYPE_PRECISION (sizetype))
3919 intop = cp_convert (type_for_size (TYPE_PRECISION (sizetype), 0), intop);
3920
3921 /* Replace the integer argument with a suitable product by the object size.
3922 Do this multiplication as signed, then convert to the appropriate
3923 pointer type (actually unsigned integral). */
3924
3925 intop = cp_convert (result_type,
3926 build_binary_op (MULT_EXPR, intop,
3927 cp_convert (TREE_TYPE (intop), size_exp), 1));
3928
3929 /* Create the sum or difference. */
3930
3931 result = build (resultcode, result_type, ptrop, intop);
3932
3933 folded = fold (result);
3934 if (folded == result)
3935 TREE_CONSTANT (folded) = TREE_CONSTANT (ptrop) & TREE_CONSTANT (intop);
3936 return folded;
3937 }
3938
3939 /* Return a tree for the difference of pointers OP0 and OP1.
3940 The resulting tree has type int. */
3941
3942 static tree
3943 pointer_diff (op0, op1)
3944 register tree op0, op1;
3945 {
3946 register tree result, folded;
3947 tree restype = ptrdiff_type_node;
3948 tree target_type = TREE_TYPE (TREE_TYPE (op0));
3949
3950 if (pedantic || warn_pointer_arith)
3951 {
3952 if (TREE_CODE (target_type) == VOID_TYPE)
3953 pedwarn ("ANSI C++ forbids using pointer of type `void *' in subtraction");
3954 if (TREE_CODE (target_type) == FUNCTION_TYPE)
3955 pedwarn ("ANSI C++ forbids using pointer to a function in subtraction");
3956 if (TREE_CODE (target_type) == METHOD_TYPE)
3957 pedwarn ("ANSI C++ forbids using pointer to a method in subtraction");
3958 if (TREE_CODE (target_type) == OFFSET_TYPE)
3959 pedwarn ("ANSI C++ forbids using pointer to a member in subtraction");
3960 }
3961
3962 /* First do the subtraction as integers;
3963 then drop through to build the divide operator. */
3964
3965 op0 = build_binary_op (MINUS_EXPR,
3966 cp_convert (restype, op0), cp_convert (restype, op1), 1);
3967
3968 /* This generates an error if op1 is a pointer to an incomplete type. */
3969 if (TYPE_SIZE (TREE_TYPE (TREE_TYPE (op1))) == 0)
3970 error ("arithmetic on pointer to an incomplete type");
3971
3972 op1 = ((TREE_CODE (target_type) == VOID_TYPE
3973 || TREE_CODE (target_type) == FUNCTION_TYPE
3974 || TREE_CODE (target_type) == METHOD_TYPE
3975 || TREE_CODE (target_type) == OFFSET_TYPE)
3976 ? integer_one_node
3977 : size_in_bytes (target_type));
3978
3979 /* Do the division. */
3980
3981 result = build (EXACT_DIV_EXPR, restype, op0, cp_convert (restype, op1));
3982
3983 folded = fold (result);
3984 if (folded == result)
3985 TREE_CONSTANT (folded) = TREE_CONSTANT (op0) & TREE_CONSTANT (op1);
3986 return folded;
3987 }
3988 \f
3989 /* Handle the case of taking the address of a COMPONENT_REF.
3990 Called by `build_unary_op' and `build_up_reference'.
3991
3992 ARG is the COMPONENT_REF whose address we want.
3993 ARGTYPE is the pointer type that this address should have.
3994 MSG is an error message to print if this COMPONENT_REF is not
3995 addressable (such as a bitfield). */
3996
3997 tree
3998 build_component_addr (arg, argtype, msg)
3999 tree arg, argtype;
4000 char *msg;
4001 {
4002 tree field = TREE_OPERAND (arg, 1);
4003 tree basetype = decl_type_context (field);
4004 tree rval = build_unary_op (ADDR_EXPR, TREE_OPERAND (arg, 0), 0);
4005
4006 if (DECL_BIT_FIELD (field))
4007 {
4008 error (msg, IDENTIFIER_POINTER (DECL_NAME (field)));
4009 return error_mark_node;
4010 }
4011
4012 if (TREE_CODE (field) == FIELD_DECL
4013 && TYPE_USES_COMPLEX_INHERITANCE (basetype))
4014 {
4015 /* Can't convert directly to ARGTYPE, since that
4016 may have the same pointer type as one of our
4017 baseclasses. */
4018 rval = build1 (NOP_EXPR, argtype,
4019 convert_pointer_to (basetype, rval));
4020 TREE_CONSTANT (rval) = TREE_CONSTANT (TREE_OPERAND (rval, 0));
4021 }
4022 else
4023 /* This conversion is harmless. */
4024 rval = convert_force (argtype, rval, 0);
4025
4026 if (! integer_zerop (DECL_FIELD_BITPOS (field)))
4027 {
4028 tree offset = size_binop (EASY_DIV_EXPR, DECL_FIELD_BITPOS (field),
4029 size_int (BITS_PER_UNIT));
4030 int flag = TREE_CONSTANT (rval);
4031 rval = fold (build (PLUS_EXPR, argtype,
4032 rval, cp_convert (argtype, offset)));
4033 TREE_CONSTANT (rval) = flag;
4034 }
4035 return rval;
4036 }
4037
4038 /* Construct and perhaps optimize a tree representation
4039 for a unary operation. CODE, a tree_code, specifies the operation
4040 and XARG is the operand. */
4041
4042 tree
4043 build_x_unary_op (code, xarg)
4044 enum tree_code code;
4045 tree xarg;
4046 {
4047 if (processing_template_decl)
4048 return build_min_nt (code, xarg, NULL_TREE);
4049
4050 /* & rec, on incomplete RECORD_TYPEs is the simple opr &, not an
4051 error message. */
4052 if (code == ADDR_EXPR
4053 && TREE_CODE (xarg) != TEMPLATE_ID_EXPR
4054 && ((IS_AGGR_TYPE_CODE (TREE_CODE (TREE_TYPE (xarg)))
4055 && TYPE_SIZE (TREE_TYPE (xarg)) == NULL_TREE)
4056 || (TREE_CODE (xarg) == OFFSET_REF)))
4057 /* don't look for a function */;
4058 else
4059 {
4060 tree rval;
4061
4062 if (flag_ansi_overloading)
4063 {
4064 rval = build_new_op (code, LOOKUP_NORMAL, xarg,
4065 NULL_TREE, NULL_TREE);
4066 if (rval || code != ADDR_EXPR)
4067 return rval;
4068 }
4069 else
4070 {
4071 rval = build_opfncall (code, LOOKUP_SPECULATIVELY, xarg,
4072 NULL_TREE, NULL_TREE);
4073 if (rval)
4074 return build_opfncall (code, LOOKUP_NORMAL, xarg,
4075 NULL_TREE, NULL_TREE);
4076 }
4077 }
4078
4079 if (code == ADDR_EXPR)
4080 {
4081 if (TREE_CODE (xarg) == TARGET_EXPR)
4082 warning ("taking address of temporary");
4083 }
4084
4085 return build_unary_op (code, xarg, 0);
4086 }
4087
4088 /* Just like truthvalue_conversion, but we want a CLEANUP_POINT_EXPR. */
4089
4090 tree
4091 condition_conversion (expr)
4092 tree expr;
4093 {
4094 tree t;
4095 if (processing_template_decl)
4096 return expr;
4097 t = cp_convert (boolean_type_node, expr);
4098 t = fold (build1 (CLEANUP_POINT_EXPR, boolean_type_node, t));
4099 return t;
4100 }
4101
4102 /* C++: Must handle pointers to members.
4103
4104 Perhaps type instantiation should be extended to handle conversion
4105 from aggregates to types we don't yet know we want? (Or are those
4106 cases typically errors which should be reported?)
4107
4108 NOCONVERT nonzero suppresses the default promotions
4109 (such as from short to int). */
4110
4111 tree
4112 build_unary_op (code, xarg, noconvert)
4113 enum tree_code code;
4114 tree xarg;
4115 int noconvert;
4116 {
4117 /* No default_conversion here. It causes trouble for ADDR_EXPR. */
4118 register tree arg = xarg;
4119 register tree argtype = 0;
4120 char *errstring = NULL;
4121 tree val;
4122
4123 if (arg == error_mark_node)
4124 return error_mark_node;
4125
4126 switch (code)
4127 {
4128 case CONVERT_EXPR:
4129 /* This is used for unary plus, because a CONVERT_EXPR
4130 is enough to prevent anybody from looking inside for
4131 associativity, but won't generate any code. */
4132 if (!(arg = build_expr_type_conversion
4133 (WANT_ARITH | WANT_ENUM | WANT_POINTER, arg, 1)))
4134 errstring = "wrong type argument to unary plus";
4135 else
4136 {
4137 if (!noconvert)
4138 arg = default_conversion (arg);
4139 arg = build1 (NON_LVALUE_EXPR, TREE_TYPE (arg), arg);
4140 }
4141 break;
4142
4143 case NEGATE_EXPR:
4144 if (!(arg = build_expr_type_conversion (WANT_ARITH | WANT_ENUM, arg, 1)))
4145 errstring = "wrong type argument to unary minus";
4146 else if (!noconvert)
4147 arg = default_conversion (arg);
4148 break;
4149
4150 case BIT_NOT_EXPR:
4151 if (TREE_CODE (TREE_TYPE (arg)) == COMPLEX_TYPE)
4152 {
4153 code = CONJ_EXPR;
4154 if (!noconvert)
4155 arg = default_conversion (arg);
4156 }
4157 else if (!(arg = build_expr_type_conversion (WANT_INT | WANT_ENUM,
4158 arg, 1)))
4159 errstring = "wrong type argument to bit-complement";
4160 else if (!noconvert)
4161 arg = default_conversion (arg);
4162 break;
4163
4164 case ABS_EXPR:
4165 if (!(arg = build_expr_type_conversion (WANT_ARITH | WANT_ENUM, arg, 1)))
4166 errstring = "wrong type argument to abs";
4167 else if (!noconvert)
4168 arg = default_conversion (arg);
4169 break;
4170
4171 case CONJ_EXPR:
4172 /* Conjugating a real value is a no-op, but allow it anyway. */
4173 if (!(arg = build_expr_type_conversion (WANT_ARITH | WANT_ENUM, arg, 1)))
4174 errstring = "wrong type argument to conjugation";
4175 else if (!noconvert)
4176 arg = default_conversion (arg);
4177 break;
4178
4179 case TRUTH_NOT_EXPR:
4180 arg = cp_convert (boolean_type_node, arg);
4181 val = invert_truthvalue (arg);
4182 if (arg != error_mark_node)
4183 return val;
4184 errstring = "in argument to unary !";
4185 break;
4186
4187 case NOP_EXPR:
4188 break;
4189
4190 case REALPART_EXPR:
4191 if (TREE_CODE (arg) == COMPLEX_CST)
4192 return TREE_REALPART (arg);
4193 else if (TREE_CODE (TREE_TYPE (arg)) == COMPLEX_TYPE)
4194 return fold (build1 (REALPART_EXPR, TREE_TYPE (TREE_TYPE (arg)), arg));
4195 else
4196 return arg;
4197
4198 case IMAGPART_EXPR:
4199 if (TREE_CODE (arg) == COMPLEX_CST)
4200 return TREE_IMAGPART (arg);
4201 else if (TREE_CODE (TREE_TYPE (arg)) == COMPLEX_TYPE)
4202 return fold (build1 (IMAGPART_EXPR, TREE_TYPE (TREE_TYPE (arg)), arg));
4203 else
4204 return cp_convert (TREE_TYPE (arg), integer_zero_node);
4205
4206 case PREINCREMENT_EXPR:
4207 case POSTINCREMENT_EXPR:
4208 case PREDECREMENT_EXPR:
4209 case POSTDECREMENT_EXPR:
4210 /* Handle complex lvalues (when permitted)
4211 by reduction to simpler cases. */
4212
4213 val = unary_complex_lvalue (code, arg);
4214 if (val != 0)
4215 return val;
4216
4217 /* Increment or decrement the real part of the value,
4218 and don't change the imaginary part. */
4219 if (TREE_CODE (TREE_TYPE (arg)) == COMPLEX_TYPE)
4220 {
4221 tree real, imag;
4222
4223 arg = stabilize_reference (arg);
4224 real = build_unary_op (REALPART_EXPR, arg, 1);
4225 imag = build_unary_op (IMAGPART_EXPR, arg, 1);
4226 return build (COMPLEX_EXPR, TREE_TYPE (arg),
4227 build_unary_op (code, real, 1), imag);
4228 }
4229
4230 /* Report invalid types. */
4231
4232 if (!(arg = build_expr_type_conversion (WANT_ARITH | WANT_POINTER,
4233 arg, 1)))
4234 {
4235 if (code == PREINCREMENT_EXPR)
4236 errstring ="no pre-increment operator for type";
4237 else if (code == POSTINCREMENT_EXPR)
4238 errstring ="no post-increment operator for type";
4239 else if (code == PREDECREMENT_EXPR)
4240 errstring ="no pre-decrement operator for type";
4241 else
4242 errstring ="no post-decrement operator for type";
4243 break;
4244 }
4245
4246 /* Report something read-only. */
4247
4248 if (TYPE_READONLY (TREE_TYPE (arg))
4249 || TREE_READONLY (arg))
4250 readonly_error (arg, ((code == PREINCREMENT_EXPR
4251 || code == POSTINCREMENT_EXPR)
4252 ? "increment" : "decrement"),
4253 0);
4254
4255 {
4256 register tree inc;
4257 tree result_type = TREE_TYPE (arg);
4258
4259 arg = get_unwidened (arg, 0);
4260 argtype = TREE_TYPE (arg);
4261
4262 /* ARM $5.2.5 last annotation says this should be forbidden. */
4263 if (TREE_CODE (argtype) == ENUMERAL_TYPE)
4264 pedwarn ("ANSI C++ forbids %sing an enum",
4265 (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
4266 ? "increment" : "decrement");
4267
4268 /* Compute the increment. */
4269
4270 if (TREE_CODE (argtype) == POINTER_TYPE)
4271 {
4272 enum tree_code tmp = TREE_CODE (TREE_TYPE (argtype));
4273 if (TYPE_SIZE (complete_type (TREE_TYPE (argtype))) == 0)
4274 cp_error ("cannot %s a pointer to incomplete type `%T'",
4275 ((code == PREINCREMENT_EXPR
4276 || code == POSTINCREMENT_EXPR)
4277 ? "increment" : "decrement"), TREE_TYPE (argtype));
4278 else if (tmp == FUNCTION_TYPE || tmp == METHOD_TYPE
4279 || tmp == VOID_TYPE || tmp == OFFSET_TYPE)
4280 cp_pedwarn ("ANSI C++ forbids %sing a pointer of type `%T'",
4281 ((code == PREINCREMENT_EXPR
4282 || code == POSTINCREMENT_EXPR)
4283 ? "increment" : "decrement"), argtype);
4284 inc = c_sizeof_nowarn (TREE_TYPE (argtype));
4285 }
4286 else
4287 inc = integer_one_node;
4288
4289 inc = cp_convert (argtype, inc);
4290
4291 /* Handle incrementing a cast-expression. */
4292
4293 switch (TREE_CODE (arg))
4294 {
4295 case NOP_EXPR:
4296 case CONVERT_EXPR:
4297 case FLOAT_EXPR:
4298 case FIX_TRUNC_EXPR:
4299 case FIX_FLOOR_EXPR:
4300 case FIX_ROUND_EXPR:
4301 case FIX_CEIL_EXPR:
4302 {
4303 tree incremented, modify, value, compound;
4304 if (! lvalue_p (arg) && pedantic)
4305 pedwarn ("cast to non-reference type used as lvalue");
4306 arg = stabilize_reference (arg);
4307 if (code == PREINCREMENT_EXPR || code == PREDECREMENT_EXPR)
4308 value = arg;
4309 else
4310 value = save_expr (arg);
4311 incremented = build (((code == PREINCREMENT_EXPR
4312 || code == POSTINCREMENT_EXPR)
4313 ? PLUS_EXPR : MINUS_EXPR),
4314 argtype, value, inc);
4315 TREE_SIDE_EFFECTS (incremented) = 1;
4316
4317 modify = build_modify_expr (arg, NOP_EXPR, incremented);
4318 compound = build (COMPOUND_EXPR, TREE_TYPE (arg), modify, value);
4319
4320 /* Eliminate warning about unused result of + or -. */
4321 TREE_NO_UNUSED_WARNING (compound) = 1;
4322 return compound;
4323 }
4324 }
4325
4326 /* Complain about anything else that is not a true lvalue. */
4327 if (!lvalue_or_else (arg, ((code == PREINCREMENT_EXPR
4328 || code == POSTINCREMENT_EXPR)
4329 ? "increment" : "decrement")))
4330 return error_mark_node;
4331
4332 /* Forbid using -- on `bool'. */
4333 if (TREE_TYPE (arg) == boolean_type_node)
4334 {
4335 if (code == POSTDECREMENT_EXPR || code == PREDECREMENT_EXPR)
4336 {
4337 cp_error ("invalid use of `--' on bool variable `%D'", arg);
4338 return error_mark_node;
4339 }
4340 #if 0
4341 /* This will only work if someone can convince Kenner to accept
4342 my patch to expand_increment. (jason) */
4343 val = build (code, TREE_TYPE (arg), arg, inc);
4344 #else
4345 if (code == POSTINCREMENT_EXPR)
4346 {
4347 arg = stabilize_reference (arg);
4348 val = build (MODIFY_EXPR, TREE_TYPE (arg), arg,
4349 boolean_true_node);
4350 TREE_SIDE_EFFECTS (val) = 1;
4351 arg = save_expr (arg);
4352 val = build (COMPOUND_EXPR, TREE_TYPE (arg), val, arg);
4353 val = build (COMPOUND_EXPR, TREE_TYPE (arg), arg, val);
4354 }
4355 else
4356 val = build (MODIFY_EXPR, TREE_TYPE (arg), arg,
4357 boolean_true_node);
4358 #endif
4359 }
4360 else
4361 val = build (code, TREE_TYPE (arg), arg, inc);
4362
4363 TREE_SIDE_EFFECTS (val) = 1;
4364 return cp_convert (result_type, val);
4365 }
4366
4367 case ADDR_EXPR:
4368 /* Note that this operation never does default_conversion
4369 regardless of NOCONVERT. */
4370
4371 argtype = TREE_TYPE (arg);
4372 if (TREE_CODE (argtype) == REFERENCE_TYPE)
4373 {
4374 arg = build1
4375 (CONVERT_EXPR,
4376 build_pointer_type (TREE_TYPE (TREE_TYPE (arg))), arg);
4377 TREE_CONSTANT (arg) = TREE_CONSTANT (TREE_OPERAND (arg, 0));
4378 return arg;
4379 }
4380 else if (pedantic
4381 && TREE_CODE (arg) == FUNCTION_DECL
4382 && DECL_NAME (arg)
4383 && DECL_CONTEXT (arg) == NULL_TREE
4384 && IDENTIFIER_LENGTH (DECL_NAME (arg)) == 4
4385 && IDENTIFIER_POINTER (DECL_NAME (arg))[0] == 'm'
4386 && ! strcmp (IDENTIFIER_POINTER (DECL_NAME (arg)), "main"))
4387 /* ARM $3.4 */
4388 pedwarn ("taking address of function `main'");
4389
4390 /* Let &* cancel out to simplify resulting code. */
4391 if (TREE_CODE (arg) == INDIRECT_REF)
4392 {
4393 /* We don't need to have `current_class_ptr' wrapped in a
4394 NON_LVALUE_EXPR node. */
4395 if (arg == current_class_ref)
4396 return current_class_ptr;
4397
4398 arg = TREE_OPERAND (arg, 0);
4399 if (TREE_CODE (TREE_TYPE (arg)) == REFERENCE_TYPE)
4400 {
4401 arg = build1
4402 (CONVERT_EXPR,
4403 build_pointer_type (TREE_TYPE (TREE_TYPE (arg))), arg);
4404 TREE_CONSTANT (arg) = TREE_CONSTANT (TREE_OPERAND (arg, 0));
4405 }
4406 else if (lvalue_p (arg))
4407 /* Don't let this be an lvalue. */
4408 return non_lvalue (arg);
4409 return arg;
4410 }
4411
4412 /* For &x[y], return x+y */
4413 if (TREE_CODE (arg) == ARRAY_REF)
4414 {
4415 if (mark_addressable (TREE_OPERAND (arg, 0)) == 0)
4416 return error_mark_node;
4417 return build_binary_op (PLUS_EXPR, TREE_OPERAND (arg, 0),
4418 TREE_OPERAND (arg, 1), 1);
4419 }
4420
4421 /* Uninstantiated types are all functions. Taking the
4422 address of a function is a no-op, so just return the
4423 argument. */
4424
4425 if (TREE_CODE (arg) == IDENTIFIER_NODE
4426 && IDENTIFIER_OPNAME_P (arg))
4427 {
4428 my_friendly_abort (117);
4429 /* We don't know the type yet, so just work around the problem.
4430 We know that this will resolve to an lvalue. */
4431 return build1 (ADDR_EXPR, unknown_type_node, arg);
4432 }
4433
4434 if (TREE_CODE (arg) == TREE_LIST)
4435 {
4436 if (TREE_CODE (TREE_VALUE (arg)) == FUNCTION_DECL
4437 && DECL_CHAIN (TREE_VALUE (arg)) == NULL_TREE)
4438 /* Unique overloaded non-member function. */
4439 return build_unary_op (ADDR_EXPR, TREE_VALUE (arg), 0);
4440 if (TREE_CHAIN (arg) == NULL_TREE
4441 && TREE_CODE (TREE_VALUE (arg)) == TREE_LIST
4442 && DECL_CHAIN (TREE_VALUE (TREE_VALUE (arg))) == NULL_TREE)
4443 /* Unique overloaded member function. */
4444 return build_unary_op (ADDR_EXPR, TREE_VALUE (TREE_VALUE (arg)),
4445 0);
4446 return build1 (ADDR_EXPR, unknown_type_node, arg);
4447 }
4448 else if (TREE_CODE (arg) == TEMPLATE_ID_EXPR)
4449 {
4450 tree targs;
4451 tree fn;
4452
4453 /* We don't require a match here; it's possible that the
4454 context (like a cast to a particular type) will resolve
4455 the particular choice of template. */
4456 fn = determine_explicit_specialization (arg, NULL_TREE,
4457 &targs,
4458 0, 0);
4459
4460 if (fn)
4461 {
4462 fn = instantiate_template (fn, targs);
4463 mark_addressable (fn);
4464 return build_unary_op (ADDR_EXPR, fn, 0);
4465 }
4466
4467 return build1 (ADDR_EXPR, unknown_type_node, arg);
4468 }
4469
4470 /* Handle complex lvalues (when permitted)
4471 by reduction to simpler cases. */
4472 val = unary_complex_lvalue (code, arg);
4473 if (val != 0)
4474 return val;
4475
4476 switch (TREE_CODE (arg))
4477 {
4478 case NOP_EXPR:
4479 case CONVERT_EXPR:
4480 case FLOAT_EXPR:
4481 case FIX_TRUNC_EXPR:
4482 case FIX_FLOOR_EXPR:
4483 case FIX_ROUND_EXPR:
4484 case FIX_CEIL_EXPR:
4485 if (! lvalue_p (arg) && pedantic)
4486 pedwarn ("taking the address of a cast to non-reference type");
4487 }
4488
4489 /* Allow the address of a constructor if all the elements
4490 are constant. */
4491 if (TREE_CODE (arg) == CONSTRUCTOR && TREE_CONSTANT (arg))
4492 ;
4493 /* Anything not already handled and not a true memory reference
4494 is an error. */
4495 else if (TREE_CODE (argtype) != FUNCTION_TYPE
4496 && TREE_CODE (argtype) != METHOD_TYPE
4497 && !lvalue_or_else (arg, "unary `&'"))
4498 return error_mark_node;
4499
4500 /* Ordinary case; arg is a COMPONENT_REF or a decl. */
4501 /* If the lvalue is const or volatile,
4502 merge that into the type that the address will point to. */
4503 if (TREE_CODE_CLASS (TREE_CODE (arg)) == 'd'
4504 || TREE_CODE_CLASS (TREE_CODE (arg)) == 'r')
4505 {
4506 if (TREE_READONLY (arg) || TREE_THIS_VOLATILE (arg))
4507 argtype = cp_build_type_variant (argtype,
4508 TREE_READONLY (arg),
4509 TREE_THIS_VOLATILE (arg));
4510 }
4511
4512 argtype = build_pointer_type (argtype);
4513
4514 if (mark_addressable (arg) == 0)
4515 return error_mark_node;
4516
4517 {
4518 tree addr;
4519
4520 if (TREE_CODE (arg) == COMPONENT_REF)
4521 addr = build_component_addr (arg, argtype,
4522 "attempt to take address of bit-field structure member `%s'");
4523 else
4524 addr = build1 (code, argtype, arg);
4525
4526 /* Address of a static or external variable or
4527 function counts as a constant */
4528 if (staticp (arg))
4529 TREE_CONSTANT (addr) = 1;
4530
4531 if (TREE_CODE (argtype) == POINTER_TYPE
4532 && TREE_CODE (TREE_TYPE (argtype)) == METHOD_TYPE)
4533 {
4534 build_ptrmemfunc_type (argtype);
4535 addr = build_ptrmemfunc (argtype, addr, 0);
4536 }
4537
4538 return addr;
4539 }
4540 }
4541
4542 if (!errstring)
4543 {
4544 if (argtype == 0)
4545 argtype = TREE_TYPE (arg);
4546 return fold (build1 (code, argtype, arg));
4547 }
4548
4549 error (errstring);
4550 return error_mark_node;
4551 }
4552
4553 #if 0
4554 /* If CONVERSIONS is a conversion expression or a nested sequence of such,
4555 convert ARG with the same conversions in the same order
4556 and return the result. */
4557
4558 static tree
4559 convert_sequence (conversions, arg)
4560 tree conversions;
4561 tree arg;
4562 {
4563 switch (TREE_CODE (conversions))
4564 {
4565 case NOP_EXPR:
4566 case CONVERT_EXPR:
4567 case FLOAT_EXPR:
4568 case FIX_TRUNC_EXPR:
4569 case FIX_FLOOR_EXPR:
4570 case FIX_ROUND_EXPR:
4571 case FIX_CEIL_EXPR:
4572 return cp_convert (TREE_TYPE (conversions),
4573 convert_sequence (TREE_OPERAND (conversions, 0),
4574 arg));
4575
4576 default:
4577 return arg;
4578 }
4579 }
4580 #endif
4581
4582 /* Apply unary lvalue-demanding operator CODE to the expression ARG
4583 for certain kinds of expressions which are not really lvalues
4584 but which we can accept as lvalues.
4585
4586 If ARG is not a kind of expression we can handle, return zero. */
4587
4588 tree
4589 unary_complex_lvalue (code, arg)
4590 enum tree_code code;
4591 tree arg;
4592 {
4593 /* Handle (a, b) used as an "lvalue". */
4594 if (TREE_CODE (arg) == COMPOUND_EXPR)
4595 {
4596 tree real_result = build_unary_op (code, TREE_OPERAND (arg, 1), 0);
4597 return build (COMPOUND_EXPR, TREE_TYPE (real_result),
4598 TREE_OPERAND (arg, 0), real_result);
4599 }
4600
4601 /* Handle (a ? b : c) used as an "lvalue". */
4602 if (TREE_CODE (arg) == COND_EXPR
4603 || TREE_CODE (arg) == MIN_EXPR || TREE_CODE (arg) == MAX_EXPR)
4604 return rationalize_conditional_expr (code, arg);
4605
4606 if (TREE_CODE (arg) == MODIFY_EXPR
4607 || TREE_CODE (arg) == PREINCREMENT_EXPR
4608 || TREE_CODE (arg) == PREDECREMENT_EXPR)
4609 return unary_complex_lvalue
4610 (code, build (COMPOUND_EXPR, TREE_TYPE (TREE_OPERAND (arg, 0)),
4611 arg, TREE_OPERAND (arg, 0)));
4612
4613 if (code != ADDR_EXPR)
4614 return 0;
4615
4616 /* Handle (a = b) used as an "lvalue" for `&'. */
4617 if (TREE_CODE (arg) == MODIFY_EXPR
4618 || TREE_CODE (arg) == INIT_EXPR)
4619 {
4620 tree real_result = build_unary_op (code, TREE_OPERAND (arg, 0), 0);
4621 arg = build (COMPOUND_EXPR, TREE_TYPE (real_result), arg, real_result);
4622 TREE_NO_UNUSED_WARNING (arg) = 1;
4623 return arg;
4624 }
4625
4626 if (TREE_CODE (TREE_TYPE (arg)) == FUNCTION_TYPE
4627 || TREE_CODE (TREE_TYPE (arg)) == METHOD_TYPE
4628 || TREE_CODE (TREE_TYPE (arg)) == OFFSET_TYPE)
4629 {
4630 /* The representation of something of type OFFSET_TYPE
4631 is really the representation of a pointer to it.
4632 Here give the representation its true type. */
4633 tree t;
4634
4635 my_friendly_assert (TREE_CODE (arg) != SCOPE_REF, 313);
4636
4637 if (TREE_CODE (arg) != OFFSET_REF)
4638 return 0;
4639
4640 t = TREE_OPERAND (arg, 1);
4641
4642 if (TREE_CODE (t) == FUNCTION_DECL) /* Check all this code for right semantics. */
4643 return build_unary_op (ADDR_EXPR, t, 0);
4644 if (TREE_CODE (t) == VAR_DECL)
4645 return build_unary_op (ADDR_EXPR, t, 0);
4646 else
4647 {
4648 tree type;
4649 tree offset = integer_zero_node;
4650
4651 if (TREE_OPERAND (arg, 0)
4652 && (TREE_CODE (TREE_OPERAND (arg, 0)) != NOP_EXPR
4653 || TREE_OPERAND (TREE_OPERAND (arg, 0), 0) != error_mark_node))
4654 if (TREE_CODE (t) != FIELD_DECL)
4655 {
4656 /* Don't know if this should return address to just
4657 _DECL, or actual address resolved in this expression. */
4658 sorry ("address of bound pointer-to-member expression");
4659 return error_mark_node;
4660 }
4661
4662 type = TREE_TYPE (TREE_OPERAND (arg, 0));
4663
4664 if (TREE_CODE (TREE_TYPE (arg)) == OFFSET_TYPE)
4665 {
4666 /* Add in the offset to the intermediate subobject, if any. */
4667 offset = get_delta_difference (TYPE_OFFSET_BASETYPE (TREE_TYPE (arg)),
4668 type,
4669 0);
4670 type = TYPE_OFFSET_BASETYPE (TREE_TYPE (arg));
4671 }
4672
4673 /* Now in the offset to the final subobject. */
4674 offset = size_binop (PLUS_EXPR,
4675 offset,
4676 get_delta_difference (DECL_FIELD_CONTEXT (t),
4677 type,
4678 0));
4679
4680 /* Add in the offset to the field. */
4681 offset = size_binop (PLUS_EXPR, offset,
4682 size_binop (EASY_DIV_EXPR,
4683 DECL_FIELD_BITPOS (t),
4684 size_int (BITS_PER_UNIT)));
4685
4686 /* We offset all pointer to data members by 1 so that we can
4687 distinguish between a null pointer to data member and the first
4688 data member of a structure. */
4689 offset = size_binop (PLUS_EXPR, offset, size_int (1));
4690
4691 return cp_convert (build_pointer_type (TREE_TYPE (arg)), offset);
4692 }
4693 }
4694
4695
4696 /* We permit compiler to make function calls returning
4697 objects of aggregate type look like lvalues. */
4698 {
4699 tree targ = arg;
4700
4701 if (TREE_CODE (targ) == SAVE_EXPR)
4702 targ = TREE_OPERAND (targ, 0);
4703
4704 if (TREE_CODE (targ) == CALL_EXPR && IS_AGGR_TYPE (TREE_TYPE (targ)))
4705 {
4706 if (TREE_CODE (arg) == SAVE_EXPR)
4707 targ = arg;
4708 else
4709 targ = build_cplus_new (TREE_TYPE (arg), arg);
4710 return build1 (ADDR_EXPR, build_pointer_type (TREE_TYPE (arg)), targ);
4711 }
4712
4713 if (TREE_CODE (arg) == SAVE_EXPR && TREE_CODE (targ) == INDIRECT_REF)
4714 return build (SAVE_EXPR, build_pointer_type (TREE_TYPE (arg)),
4715 TREE_OPERAND (targ, 0), current_function_decl, NULL);
4716 }
4717
4718 /* Don't let anything else be handled specially. */
4719 return 0;
4720 }
4721 \f
4722 /* Mark EXP saying that we need to be able to take the
4723 address of it; it should not be allocated in a register.
4724 Value is 1 if successful.
4725
4726 C++: we do not allow `current_class_ptr' to be addressable. */
4727
4728 int
4729 mark_addressable (exp)
4730 tree exp;
4731 {
4732 register tree x = exp;
4733
4734 if (TREE_ADDRESSABLE (x) == 1)
4735 return 1;
4736
4737 while (1)
4738 switch (TREE_CODE (x))
4739 {
4740 case ADDR_EXPR:
4741 case COMPONENT_REF:
4742 case ARRAY_REF:
4743 case REALPART_EXPR:
4744 case IMAGPART_EXPR:
4745 x = TREE_OPERAND (x, 0);
4746 break;
4747
4748 case PARM_DECL:
4749 if (x == current_class_ptr)
4750 {
4751 if (! flag_this_is_variable)
4752 error ("address of `this' not available");
4753 TREE_ADDRESSABLE (x) = 1; /* so compiler doesn't die later */
4754 put_var_into_stack (x);
4755 return 1;
4756 }
4757 case VAR_DECL:
4758 if (TREE_STATIC (x) && TREE_READONLY (x)
4759 && DECL_RTL (x) != 0
4760 && ! DECL_IN_MEMORY_P (x))
4761 {
4762 /* We thought this would make a good constant variable,
4763 but we were wrong. */
4764 push_obstacks_nochange ();
4765 end_temporary_allocation ();
4766
4767 TREE_ASM_WRITTEN (x) = 0;
4768 DECL_RTL (x) = 0;
4769 rest_of_decl_compilation (x, 0, IDENTIFIER_LOCAL_VALUE (x) == 0, 0);
4770 TREE_ADDRESSABLE (x) = 1;
4771
4772 pop_obstacks ();
4773
4774 return 1;
4775 }
4776 /* Caller should not be trying to mark initialized
4777 constant fields addressable. */
4778 my_friendly_assert (DECL_LANG_SPECIFIC (x) == 0
4779 || DECL_IN_AGGR_P (x) == 0
4780 || TREE_STATIC (x)
4781 || DECL_EXTERNAL (x), 314);
4782
4783 case CONST_DECL:
4784 case RESULT_DECL:
4785 if (DECL_REGISTER (x) && !TREE_ADDRESSABLE (x)
4786 && !DECL_ARTIFICIAL (x) && extra_warnings)
4787 cp_warning ("address requested for `%D', which is declared `register'",
4788 x);
4789 put_var_into_stack (x);
4790 TREE_ADDRESSABLE (x) = 1;
4791 return 1;
4792
4793 case FUNCTION_DECL:
4794 /* We have to test both conditions here. The first may
4795 be non-zero in the case of processing a default function.
4796 The second may be non-zero in the case of a template function. */
4797 x = DECL_MAIN_VARIANT (x);
4798 if (DECL_TEMPLATE_INFO (x) && !DECL_TEMPLATE_SPECIALIZATION (x))
4799 mark_used (x);
4800 TREE_ADDRESSABLE (x) = 1;
4801 TREE_USED (x) = 1;
4802 TREE_ADDRESSABLE (DECL_ASSEMBLER_NAME (x)) = 1;
4803 return 1;
4804
4805 case CONSTRUCTOR:
4806 TREE_ADDRESSABLE (x) = 1;
4807 return 1;
4808
4809 case TARGET_EXPR:
4810 TREE_ADDRESSABLE (x) = 1;
4811 mark_addressable (TREE_OPERAND (x, 0));
4812 return 1;
4813
4814 default:
4815 return 1;
4816 }
4817 }
4818 \f
4819 /* Build and return a conditional expression IFEXP ? OP1 : OP2. */
4820
4821 tree
4822 build_x_conditional_expr (ifexp, op1, op2)
4823 tree ifexp, op1, op2;
4824 {
4825 tree rval = NULL_TREE;
4826
4827 if (processing_template_decl)
4828 return build_min_nt (COND_EXPR, ifexp, op1, op2);
4829
4830 if (flag_ansi_overloading)
4831 return build_new_op (COND_EXPR, LOOKUP_NORMAL, ifexp, op1, op2);
4832
4833 /* See comments in `build_x_binary_op'. */
4834 if (op1 != 0)
4835 rval = build_opfncall (COND_EXPR, LOOKUP_SPECULATIVELY, ifexp, op1, op2);
4836 if (rval)
4837 return build_opfncall (COND_EXPR, LOOKUP_NORMAL, ifexp, op1, op2);
4838
4839 return build_conditional_expr (ifexp, op1, op2);
4840 }
4841
4842 tree
4843 build_conditional_expr (ifexp, op1, op2)
4844 tree ifexp, op1, op2;
4845 {
4846 register tree type1;
4847 register tree type2;
4848 register enum tree_code code1;
4849 register enum tree_code code2;
4850 register tree result_type = NULL_TREE;
4851
4852 /* If second operand is omitted, it is the same as the first one;
4853 make sure it is calculated only once. */
4854 if (op1 == 0)
4855 {
4856 if (pedantic)
4857 pedwarn ("ANSI C++ forbids omitting the middle term of a ?: expression");
4858 ifexp = op1 = save_expr (ifexp);
4859 }
4860
4861 ifexp = cp_convert (boolean_type_node, ifexp);
4862
4863 if (TREE_CODE (ifexp) == ERROR_MARK)
4864 return error_mark_node;
4865
4866 op1 = require_instantiated_type (TREE_TYPE (op2), op1, error_mark_node);
4867 if (op1 == error_mark_node)
4868 return error_mark_node;
4869 op2 = require_instantiated_type (TREE_TYPE (op1), op2, error_mark_node);
4870 if (op2 == error_mark_node)
4871 return error_mark_node;
4872
4873 /* C++: REFERENCE_TYPES must be dereferenced. */
4874 type1 = TREE_TYPE (op1);
4875 code1 = TREE_CODE (type1);
4876 type2 = TREE_TYPE (op2);
4877 code2 = TREE_CODE (type2);
4878
4879 if (code1 == REFERENCE_TYPE)
4880 {
4881 op1 = convert_from_reference (op1);
4882 type1 = TREE_TYPE (op1);
4883 code1 = TREE_CODE (type1);
4884 }
4885 if (code2 == REFERENCE_TYPE)
4886 {
4887 op2 = convert_from_reference (op2);
4888 type2 = TREE_TYPE (op2);
4889 code2 = TREE_CODE (type2);
4890 }
4891
4892 /* Don't promote the operands separately if they promote
4893 the same way. Return the unpromoted type and let the combined
4894 value get promoted if necessary. */
4895
4896 if (TYPE_MAIN_VARIANT (type1) == TYPE_MAIN_VARIANT (type2)
4897 && code2 != ARRAY_TYPE
4898 && code2 != FUNCTION_TYPE
4899 && code2 != METHOD_TYPE)
4900 {
4901 tree result;
4902
4903 if (TREE_CONSTANT (ifexp)
4904 && (TREE_CODE (ifexp) == INTEGER_CST
4905 || TREE_CODE (ifexp) == ADDR_EXPR))
4906 return (integer_zerop (ifexp) ? op2 : op1);
4907
4908 if (TREE_CODE (op1) == CONST_DECL)
4909 op1 = DECL_INITIAL (op1);
4910 else if (TREE_READONLY_DECL_P (op1))
4911 op1 = decl_constant_value (op1);
4912 if (TREE_CODE (op2) == CONST_DECL)
4913 op2 = DECL_INITIAL (op2);
4914 else if (TREE_READONLY_DECL_P (op2))
4915 op2 = decl_constant_value (op2);
4916 if (type1 != type2)
4917 type1 = cp_build_type_variant
4918 (type1,
4919 TREE_READONLY (op1) || TREE_READONLY (op2),
4920 TREE_THIS_VOLATILE (op1) || TREE_THIS_VOLATILE (op2));
4921 /* ??? This is a kludge to deal with the fact that
4922 we don't sort out integers and enums properly, yet. */
4923 result = fold (build (COND_EXPR, type1, ifexp, op1, op2));
4924 if (TREE_TYPE (result) != type1)
4925 result = build1 (NOP_EXPR, type1, result);
4926 /* Expand both sides into the same slot,
4927 hopefully the target of the ?: expression. */
4928 if (TREE_CODE (op1) == TARGET_EXPR && TREE_CODE (op2) == TARGET_EXPR)
4929 {
4930 tree slot = build (VAR_DECL, TREE_TYPE (result));
4931 layout_decl (slot, 0);
4932 result = build (TARGET_EXPR, TREE_TYPE (result),
4933 slot, result, NULL_TREE, NULL_TREE);
4934 }
4935 return result;
4936 }
4937
4938 /* They don't match; promote them both and then try to reconcile them.
4939 But don't permit mismatching enum types. */
4940 if (code1 == ENUMERAL_TYPE)
4941 {
4942 if (code2 == ENUMERAL_TYPE)
4943 {
4944 cp_error ("enumeral mismatch in conditional expression: `%T' vs `%T'", type1, type2);
4945 return error_mark_node;
4946 }
4947 else if (extra_warnings && ! IS_AGGR_TYPE_CODE (code2)
4948 && type2 != type_promotes_to (type1))
4949 warning ("enumeral and non-enumeral type in conditional expression");
4950 }
4951 else if (extra_warnings
4952 && code2 == ENUMERAL_TYPE && ! IS_AGGR_TYPE_CODE (code1)
4953 && type1 != type_promotes_to (type2))
4954 warning ("enumeral and non-enumeral type in conditional expression");
4955
4956 if (code1 != VOID_TYPE)
4957 {
4958 op1 = default_conversion (op1);
4959 type1 = TREE_TYPE (op1);
4960 if (TYPE_PTRMEMFUNC_P (type1))
4961 type1 = TYPE_PTRMEMFUNC_FN_TYPE (type1);
4962 code1 = TREE_CODE (type1);
4963 }
4964 if (code2 != VOID_TYPE)
4965 {
4966 op2 = default_conversion (op2);
4967 type2 = TREE_TYPE (op2);
4968 if (TYPE_PTRMEMFUNC_P (type2))
4969 type2 = TYPE_PTRMEMFUNC_FN_TYPE (type2);
4970 code2 = TREE_CODE (type2);
4971 }
4972
4973 if (code1 == RECORD_TYPE && code2 == RECORD_TYPE
4974 && real_lvalue_p (op1) && real_lvalue_p (op2)
4975 && comptypes (type1, type2, -1))
4976 {
4977 type1 = build_reference_type (type1);
4978 type2 = build_reference_type (type2);
4979 result_type = common_type (type1, type2);
4980 op1 = convert_to_reference (result_type, op1, CONV_IMPLICIT,
4981 LOOKUP_NORMAL, NULL_TREE);
4982 op2 = convert_to_reference (result_type, op2, CONV_IMPLICIT,
4983 LOOKUP_NORMAL, NULL_TREE);
4984 }
4985 /* Quickly detect the usual case where op1 and op2 have the same type
4986 after promotion. */
4987 else if (TYPE_MAIN_VARIANT (type1) == TYPE_MAIN_VARIANT (type2))
4988 {
4989 if (type1 == type2)
4990 result_type = type1;
4991 else
4992 result_type = cp_build_type_variant
4993 (type1,
4994 TREE_READONLY (op1) || TREE_READONLY (op2),
4995 TREE_THIS_VOLATILE (op1) || TREE_THIS_VOLATILE (op2));
4996 }
4997 else if ((code1 == INTEGER_TYPE || code1 == REAL_TYPE)
4998 && (code2 == INTEGER_TYPE || code2 == REAL_TYPE))
4999 {
5000 result_type = common_type (type1, type2);
5001 }
5002 else if (code1 == VOID_TYPE || code2 == VOID_TYPE)
5003 {
5004 if (pedantic && (code1 != VOID_TYPE || code2 != VOID_TYPE))
5005 pedwarn ("ANSI C++ forbids conditional expr with only one void side");
5006 result_type = void_type_node;
5007 }
5008 else if (code1 == POINTER_TYPE && null_ptr_cst_p (op2))
5009 result_type = qualify_type (type1, type2);
5010 else if (code2 == POINTER_TYPE && null_ptr_cst_p (op1))
5011 result_type = qualify_type (type2, type1);
5012 else if (code1 == POINTER_TYPE && code2 == POINTER_TYPE)
5013 {
5014 if (comp_target_types (type1, type2, 1))
5015 result_type = common_type (type1, type2);
5016 else if (TYPE_MAIN_VARIANT (TREE_TYPE (type1)) == void_type_node)
5017 {
5018 if (pedantic && TREE_CODE (type2) == FUNCTION_TYPE)
5019 pedwarn ("ANSI C++ forbids conditional expr between `void *' and function pointer");
5020 result_type = qualify_type (type1, type2);
5021 }
5022 else if (TYPE_MAIN_VARIANT (TREE_TYPE (type2)) == void_type_node)
5023 {
5024 if (pedantic && TREE_CODE (type1) == FUNCTION_TYPE)
5025 pedwarn ("ANSI C++ forbids conditional expr between `void *' and function pointer");
5026 result_type = qualify_type (type2, type1);
5027 }
5028 /* C++ */
5029 else if (comptypes (type2, type1, 0))
5030 result_type = type2;
5031 else if (IS_AGGR_TYPE (TREE_TYPE (type1))
5032 && IS_AGGR_TYPE (TREE_TYPE (type2))
5033 && (result_type = common_base_type (TREE_TYPE (type1), TREE_TYPE (type2))))
5034 {
5035 if (result_type == error_mark_node)
5036 {
5037 cp_error ("common base type of types `%T' and `%T' is ambiguous",
5038 TREE_TYPE (type1), TREE_TYPE (type2));
5039 result_type = ptr_type_node;
5040 }
5041 else
5042 {
5043 if (pedantic
5044 && result_type != TREE_TYPE (type1)
5045 && result_type != TREE_TYPE (type2))
5046 cp_pedwarn ("`%T' and `%T' converted to `%T *' in conditional expression",
5047 type1, type2, result_type);
5048
5049 result_type = build_pointer_type (result_type);
5050 }
5051 }
5052 else
5053 {
5054 pedwarn ("pointer type mismatch in conditional expression");
5055 result_type = ptr_type_node;
5056 }
5057 }
5058 else if (code1 == POINTER_TYPE && code2 == INTEGER_TYPE)
5059 {
5060 pedwarn ("pointer/integer type mismatch in conditional expression");
5061 result_type = type1;
5062 }
5063 else if (code2 == POINTER_TYPE && code1 == INTEGER_TYPE)
5064 {
5065 pedwarn ("pointer/integer type mismatch in conditional expression");
5066 result_type = type2;
5067 }
5068
5069 if (!result_type)
5070 {
5071 /* The match does not look good. If either is
5072 an aggregate value, try converting to a scalar type. */
5073 if (code1 == RECORD_TYPE && code2 == RECORD_TYPE)
5074 {
5075 cp_error ("aggregate mismatch in conditional expression: `%T' vs `%T'", type1, type2);
5076 return error_mark_node;
5077 }
5078 /* Warning: this code assumes that conversion between cv-variants of
5079 a type is done using NOP_EXPRs. */
5080 if (code1 == RECORD_TYPE && TYPE_HAS_CONVERSION (type1))
5081 {
5082 /* There are other types besides pointers and records. */
5083 tree tmp;
5084 if (code2 == POINTER_TYPE)
5085 tmp = build_pointer_type
5086 (build_type_variant (TREE_TYPE (type2), 1, 1));
5087 else
5088 tmp = type2;
5089 tmp = build_type_conversion (CONVERT_EXPR, tmp, op1, 0);
5090 if (tmp == NULL_TREE)
5091 {
5092 cp_error ("incompatible types `%T' and `%T' in `?:'",
5093 type1, type2);
5094 return error_mark_node;
5095 }
5096 if (tmp == error_mark_node)
5097 error ("ambiguous pointer conversion");
5098 else
5099 STRIP_NOPS (tmp);
5100 result_type = common_type (type2, TREE_TYPE (tmp));
5101 op1 = tmp;
5102 }
5103 else if (code2 == RECORD_TYPE && TYPE_HAS_CONVERSION (type2))
5104 {
5105 tree tmp;
5106 if (code1 == POINTER_TYPE)
5107 tmp = build_pointer_type
5108 (build_type_variant (TREE_TYPE (type1), 1, 1));
5109 else
5110 tmp = type1;
5111
5112 tmp = build_type_conversion (CONVERT_EXPR, tmp, op2, 0);
5113 if (tmp == NULL_TREE)
5114 {
5115 cp_error ("incompatible types `%T' and `%T' in `?:'",
5116 type1, type2);
5117 return error_mark_node;
5118 }
5119 if (tmp == error_mark_node)
5120 error ("ambiguous pointer conversion");
5121 else
5122 STRIP_NOPS (tmp);
5123 result_type = common_type (type1, TREE_TYPE (tmp));
5124 op2 = tmp;
5125 }
5126 else if (flag_cond_mismatch)
5127 result_type = void_type_node;
5128 else
5129 {
5130 error ("type mismatch in conditional expression");
5131 return error_mark_node;
5132 }
5133 }
5134
5135 if (TREE_CODE (result_type) == POINTER_TYPE
5136 && TREE_CODE (TREE_TYPE (result_type)) == METHOD_TYPE)
5137 result_type = build_ptrmemfunc_type (result_type);
5138
5139 if (result_type != TREE_TYPE (op1))
5140 op1 = convert_for_initialization
5141 (NULL_TREE, result_type, op1, LOOKUP_NORMAL, "converting", NULL_TREE, 0);
5142 if (result_type != TREE_TYPE (op2))
5143 op2 = convert_for_initialization
5144 (NULL_TREE, result_type, op2, LOOKUP_NORMAL, "converting", NULL_TREE, 0);
5145
5146 if (TREE_CONSTANT (ifexp))
5147 return integer_zerop (ifexp) ? op2 : op1;
5148
5149 return convert_from_reference
5150 (fold (build (COND_EXPR, result_type, ifexp, op1, op2)));
5151 }
5152 \f
5153 /* Handle overloading of the ',' operator when needed. Otherwise,
5154 this function just builds an expression list. */
5155
5156 tree
5157 build_x_compound_expr (list)
5158 tree list;
5159 {
5160 tree rest = TREE_CHAIN (list);
5161 tree result;
5162
5163 if (processing_template_decl)
5164 return build_min_nt (COMPOUND_EXPR, list, NULL_TREE);
5165
5166 if (rest == NULL_TREE)
5167 return build_compound_expr (list);
5168
5169 result = build_opfncall (COMPOUND_EXPR, LOOKUP_NORMAL,
5170 TREE_VALUE (list), TREE_VALUE (rest), NULL_TREE);
5171 if (result)
5172 return build_x_compound_expr (expr_tree_cons (NULL_TREE, result, TREE_CHAIN (rest)));
5173
5174 if (! TREE_SIDE_EFFECTS (TREE_VALUE (list)))
5175 {
5176 /* the left-hand operand of a comma expression is like an expression
5177 statement: we should warn if it doesn't have any side-effects,
5178 unless it was explicitly cast to (void). */
5179 if ((extra_warnings || warn_unused)
5180 && !(TREE_CODE (TREE_VALUE(list)) == CONVERT_EXPR
5181 && TREE_TYPE (TREE_VALUE(list)) == void_type_node))
5182 warning("left-hand operand of comma expression has no effect");
5183 }
5184 #if 0 /* this requires a gcc backend patch to export warn_if_unused_value */
5185 else if (warn_unused)
5186 warn_if_unused_value (TREE_VALUE(list));
5187 #endif
5188
5189 return build_compound_expr (expr_tree_cons (NULL_TREE, TREE_VALUE (list),
5190 build_expr_list (NULL_TREE, build_x_compound_expr (rest))));
5191 }
5192
5193 /* Given a list of expressions, return a compound expression
5194 that performs them all and returns the value of the last of them. */
5195
5196 tree
5197 build_compound_expr (list)
5198 tree list;
5199 {
5200 register tree rest;
5201
5202 if (TREE_READONLY_DECL_P (TREE_VALUE (list)))
5203 TREE_VALUE (list) = decl_constant_value (TREE_VALUE (list));
5204
5205 if (TREE_CHAIN (list) == 0)
5206 {
5207 /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
5208 Strip such NOP_EXPRs, since LIST is used in non-lvalue context. */
5209 if (TREE_CODE (list) == NOP_EXPR
5210 && TREE_TYPE (list) == TREE_TYPE (TREE_OPERAND (list, 0)))
5211 list = TREE_OPERAND (list, 0);
5212
5213 /* Convert arrays to pointers. */
5214 if (TREE_CODE (TREE_TYPE (TREE_VALUE (list))) == ARRAY_TYPE)
5215 return default_conversion (TREE_VALUE (list));
5216 else
5217 return TREE_VALUE (list);
5218 }
5219
5220 rest = build_compound_expr (TREE_CHAIN (list));
5221
5222 /* When pedantic, a compound expression cannot be a constant expression. */
5223 if (! TREE_SIDE_EFFECTS (TREE_VALUE (list)) && ! pedantic)
5224 return rest;
5225
5226 return build (COMPOUND_EXPR, TREE_TYPE (rest),
5227 break_out_cleanups (TREE_VALUE (list)), rest);
5228 }
5229
5230 tree
5231 build_static_cast (type, expr)
5232 tree type, expr;
5233 {
5234 tree intype, binfo;
5235 int ok;
5236
5237 if (type == error_mark_node || expr == error_mark_node)
5238 return error_mark_node;
5239
5240 if (TREE_CODE (expr) == OFFSET_REF)
5241 expr = resolve_offset_ref (expr);
5242
5243 if (processing_template_decl)
5244 {
5245 tree t = build_min (STATIC_CAST_EXPR, type, expr);
5246 return t;
5247 }
5248
5249 /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
5250 Strip such NOP_EXPRs if VALUE is being used in non-lvalue context. */
5251 if (TREE_CODE (type) != REFERENCE_TYPE
5252 && TREE_CODE (expr) == NOP_EXPR
5253 && TREE_TYPE (expr) == TREE_TYPE (TREE_OPERAND (expr, 0)))
5254 expr = TREE_OPERAND (expr, 0);
5255
5256 if (TREE_CODE (type) == VOID_TYPE)
5257 return build1 (CONVERT_EXPR, type, expr);
5258
5259 if (type_unknown_p (expr))
5260 {
5261 expr = instantiate_type (type, expr, 1);
5262 if (expr == error_mark_node)
5263 return error_mark_node;
5264 }
5265
5266 if (TREE_CODE (type) == REFERENCE_TYPE)
5267 return (convert_from_reference
5268 (convert_to_reference (type, expr, CONV_STATIC|CONV_IMPLICIT,
5269 LOOKUP_COMPLAIN, NULL_TREE)));
5270
5271 if (IS_AGGR_TYPE (type))
5272 return build_cplus_new
5273 (type, (build_method_call
5274 (NULL_TREE, ctor_identifier, build_expr_list (NULL_TREE, expr),
5275 TYPE_BINFO (type), LOOKUP_NORMAL)));
5276
5277 expr = decay_conversion (expr);
5278 intype = TREE_TYPE (expr);
5279
5280 /* FIXME handle casting to array type. */
5281
5282 ok = 0;
5283 if (can_convert_arg (type, intype, expr))
5284 ok = 1;
5285 else if (TYPE_PTROB_P (type) && TYPE_PTROB_P (intype))
5286 {
5287 tree binfo;
5288 if (IS_AGGR_TYPE (TREE_TYPE (type)) && IS_AGGR_TYPE (TREE_TYPE (intype))
5289 && (TYPE_READONLY (TREE_TYPE (type))
5290 >= TYPE_READONLY (TREE_TYPE (intype)))
5291 && (TYPE_VOLATILE (TREE_TYPE (type))
5292 >= TYPE_VOLATILE (TREE_TYPE (intype)))
5293 && (binfo = get_binfo (TREE_TYPE (intype), TREE_TYPE (type), 0))
5294 && ! TREE_VIA_VIRTUAL (binfo))
5295 ok = 1;
5296 }
5297 else if (TYPE_PTRMEM_P (type) && TYPE_PTRMEM_P (intype))
5298 {
5299 if (comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (type))),
5300 TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (intype))), 1)
5301 && (TYPE_READONLY (TREE_TYPE (TREE_TYPE (type)))
5302 >= TYPE_READONLY (TREE_TYPE (TREE_TYPE (intype))))
5303 && (TYPE_VOLATILE (TREE_TYPE (TREE_TYPE (type)))
5304 >= TYPE_VOLATILE (TREE_TYPE (TREE_TYPE (intype))))
5305 && (binfo = get_binfo (TYPE_OFFSET_BASETYPE (intype),
5306 TYPE_OFFSET_BASETYPE (type), 0))
5307 && ! TREE_VIA_VIRTUAL (binfo))
5308 ok = 1;
5309 }
5310 else if (TREE_CODE (intype) != BOOLEAN_TYPE
5311 && TREE_CODE (type) != ARRAY_TYPE
5312 && TREE_CODE (type) != FUNCTION_TYPE
5313 && can_convert (intype, type))
5314 ok = 1;
5315
5316 if (ok)
5317 return build_c_cast (type, expr);
5318
5319 cp_error ("static_cast from `%T' to `%T'", intype, type);
5320 return error_mark_node;
5321 }
5322
5323 tree
5324 build_reinterpret_cast (type, expr)
5325 tree type, expr;
5326 {
5327 tree intype;
5328
5329 if (type == error_mark_node || expr == error_mark_node)
5330 return error_mark_node;
5331
5332 if (TREE_CODE (expr) == OFFSET_REF)
5333 expr = resolve_offset_ref (expr);
5334
5335 if (processing_template_decl)
5336 {
5337 tree t = build_min (REINTERPRET_CAST_EXPR, type, expr);
5338 return t;
5339 }
5340
5341 if (TREE_CODE (type) != REFERENCE_TYPE)
5342 {
5343 expr = decay_conversion (expr);
5344
5345 /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
5346 Strip such NOP_EXPRs if VALUE is being used in non-lvalue context. */
5347 if (TREE_CODE (expr) == NOP_EXPR
5348 && TREE_TYPE (expr) == TREE_TYPE (TREE_OPERAND (expr, 0)))
5349 expr = TREE_OPERAND (expr, 0);
5350 }
5351
5352 if (type_unknown_p (expr))
5353 {
5354 expr = instantiate_type (type, expr, 1);
5355 if (expr == error_mark_node)
5356 return error_mark_node;
5357 }
5358
5359 intype = TREE_TYPE (expr);
5360
5361 if (TREE_CODE (type) == REFERENCE_TYPE)
5362 {
5363 if (! real_lvalue_p (expr))
5364 {
5365 cp_error ("reinterpret_cast from `%T' rvalue to `%T'", intype, type);
5366 return error_mark_node;
5367 }
5368 expr = build_unary_op (ADDR_EXPR, expr, 0);
5369 if (expr != error_mark_node)
5370 expr = build_reinterpret_cast
5371 (build_pointer_type (TREE_TYPE (type)), expr);
5372 if (expr != error_mark_node)
5373 expr = build_indirect_ref (expr, 0);
5374 return expr;
5375 }
5376 else if (comptypes (TYPE_MAIN_VARIANT (intype), TYPE_MAIN_VARIANT (type), 1))
5377 return build_static_cast (type, expr);
5378
5379 if (TYPE_PTR_P (type) && (TREE_CODE (intype) == INTEGER_TYPE
5380 || TREE_CODE (intype) == ENUMERAL_TYPE))
5381 /* OK */;
5382 else if (TREE_CODE (type) == INTEGER_TYPE && TYPE_PTR_P (intype))
5383 {
5384 if (TYPE_PRECISION (type) < TYPE_PRECISION (intype))
5385 cp_pedwarn ("reinterpret_cast from `%T' to `%T' loses precision",
5386 intype, type);
5387 }
5388 else if ((TYPE_PTRFN_P (type) && TYPE_PTRFN_P (intype))
5389 || (TYPE_PTRMEMFUNC_P (type) && TYPE_PTRMEMFUNC_P (intype)))
5390 {
5391 if (TREE_READONLY_DECL_P (expr))
5392 expr = decl_constant_value (expr);
5393 return fold (build1 (NOP_EXPR, type, expr));
5394 }
5395 else if ((TYPE_PTRMEM_P (type) && TYPE_PTRMEM_P (intype))
5396 || (TYPE_PTROBV_P (type) && TYPE_PTROBV_P (intype)))
5397 {
5398 if (! comp_ptr_ttypes_reinterpret (TREE_TYPE (type), TREE_TYPE (intype)))
5399 cp_pedwarn ("reinterpret_cast from `%T' to `%T' casts away const (or volatile)",
5400 intype, type);
5401
5402 if (TREE_READONLY_DECL_P (expr))
5403 expr = decl_constant_value (expr);
5404 return fold (build1 (NOP_EXPR, type, expr));
5405 }
5406 else if ((TYPE_PTRFN_P (type) && TYPE_PTROBV_P (intype))
5407 || (TYPE_PTRFN_P (type) && TYPE_PTROBV_P (intype)))
5408 {
5409 pedwarn ("ANSI C++ forbids casting between pointers to functions and objects");
5410 if (TREE_READONLY_DECL_P (expr))
5411 expr = decl_constant_value (expr);
5412 return fold (build1 (NOP_EXPR, type, expr));
5413 }
5414 else
5415 {
5416 cp_error ("reinterpret_cast from `%T' to `%T'", intype, type);
5417 return error_mark_node;
5418 }
5419
5420 return cp_convert (type, expr);
5421 }
5422
5423 tree
5424 build_const_cast (type, expr)
5425 tree type, expr;
5426 {
5427 tree intype;
5428
5429 if (type == error_mark_node || expr == error_mark_node)
5430 return error_mark_node;
5431
5432 if (TREE_CODE (expr) == OFFSET_REF)
5433 expr = resolve_offset_ref (expr);
5434
5435 if (processing_template_decl)
5436 {
5437 tree t = build_min (CONST_CAST_EXPR, type, expr);
5438 return t;
5439 }
5440
5441 if (TREE_CODE (type) != REFERENCE_TYPE)
5442 {
5443 expr = decay_conversion (expr);
5444
5445 /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
5446 Strip such NOP_EXPRs if VALUE is being used in non-lvalue context. */
5447 if (TREE_CODE (expr) == NOP_EXPR
5448 && TREE_TYPE (expr) == TREE_TYPE (TREE_OPERAND (expr, 0)))
5449 expr = TREE_OPERAND (expr, 0);
5450 }
5451
5452 if (type_unknown_p (expr))
5453 {
5454 expr = instantiate_type (type, expr, 1);
5455 if (expr == error_mark_node)
5456 return error_mark_node;
5457 }
5458
5459 intype = TREE_TYPE (expr);
5460
5461 if (comptypes (TYPE_MAIN_VARIANT (intype), TYPE_MAIN_VARIANT (type), 1))
5462 return build_static_cast (type, expr);
5463 else if (TREE_CODE (type) == REFERENCE_TYPE)
5464 {
5465 if (! real_lvalue_p (expr))
5466 {
5467 cp_error ("const_cast from `%T' rvalue to `%T'", intype, type);
5468 return error_mark_node;
5469 }
5470
5471 if (comp_ptr_ttypes_const (TREE_TYPE (type), intype))
5472 return (convert_from_reference
5473 (convert_to_reference (type, expr, CONV_CONST|CONV_IMPLICIT,
5474 LOOKUP_COMPLAIN, NULL_TREE)));
5475 }
5476 else if (TREE_CODE (type) == POINTER_TYPE
5477 && TREE_CODE (intype) == POINTER_TYPE
5478 && comp_ptr_ttypes_const (TREE_TYPE (type), TREE_TYPE (intype)))
5479 return cp_convert (type, expr);
5480
5481 cp_error ("const_cast from `%T' to `%T'", intype, type);
5482 return error_mark_node;
5483 }
5484
5485 /* Build an expression representing a cast to type TYPE of expression EXPR.
5486
5487 ALLOW_NONCONVERTING is true if we should allow non-converting constructors
5488 when doing the cast. */
5489
5490 tree
5491 build_c_cast (type, expr)
5492 tree type, expr;
5493 {
5494 register tree value = expr;
5495
5496 if (type == error_mark_node || expr == error_mark_node)
5497 return error_mark_node;
5498
5499 /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
5500 Strip such NOP_EXPRs if VALUE is being used in non-lvalue context. */
5501 if (TREE_CODE (type) != REFERENCE_TYPE
5502 && TREE_CODE (value) == NOP_EXPR
5503 && TREE_TYPE (value) == TREE_TYPE (TREE_OPERAND (value, 0)))
5504 value = TREE_OPERAND (value, 0);
5505
5506 if (TREE_TYPE (expr)
5507 && TREE_CODE (TREE_TYPE (expr)) == OFFSET_TYPE
5508 && TREE_CODE (type) != OFFSET_TYPE)
5509 value = resolve_offset_ref (value);
5510
5511 if (TREE_CODE (type) == ARRAY_TYPE)
5512 {
5513 /* Allow casting from T1* to T2[] because Cfront allows it.
5514 NIHCL uses it. It is not valid ANSI C however, and hence, not
5515 valid ANSI C++. */
5516 if (TREE_CODE (TREE_TYPE (expr)) == POINTER_TYPE)
5517 {
5518 if (pedantic)
5519 pedwarn ("ANSI C++ forbids casting to an array type");
5520 type = build_pointer_type (TREE_TYPE (type));
5521 }
5522 else
5523 {
5524 error ("ANSI C++ forbids casting to an array type");
5525 return error_mark_node;
5526 }
5527 }
5528
5529 if (TREE_CODE (type) == FUNCTION_TYPE
5530 || TREE_CODE (type) == METHOD_TYPE)
5531 {
5532 cp_error ("casting to function type `%T'", type);
5533 return error_mark_node;
5534 }
5535
5536 if (IS_SIGNATURE (type))
5537 {
5538 error ("cast specifies signature type");
5539 return error_mark_node;
5540 }
5541
5542 if (processing_template_decl)
5543 {
5544 tree t = build_min (CAST_EXPR, type,
5545 min_tree_cons (NULL_TREE, value, NULL_TREE));
5546 return t;
5547 }
5548
5549 if (TREE_CODE (type) == VOID_TYPE)
5550 value = build1 (CONVERT_EXPR, type, value);
5551 else if (TREE_TYPE (value) == NULL_TREE
5552 || type_unknown_p (value))
5553 {
5554 value = instantiate_type (type, value, 1);
5555 /* Did we lose? */
5556 if (value == error_mark_node)
5557 return error_mark_node;
5558 }
5559 else
5560 {
5561 tree otype;
5562
5563 /* Convert functions and arrays to pointers and
5564 convert references to their expanded types,
5565 but don't convert any other types. */
5566 if (TREE_CODE (TREE_TYPE (value)) == FUNCTION_TYPE
5567 || (TREE_CODE (TREE_TYPE (value)) == METHOD_TYPE
5568 /* Don't do the default conversion if we want a
5569 pointer to a function. */
5570 && ! (TREE_CODE (type) == POINTER_TYPE
5571 && TREE_CODE (TREE_TYPE (type)) == FUNCTION_TYPE))
5572 || TREE_CODE (TREE_TYPE (value)) == ARRAY_TYPE
5573 || TREE_CODE (TREE_TYPE (value)) == REFERENCE_TYPE)
5574 value = default_conversion (value);
5575 otype = TREE_TYPE (value);
5576
5577 /* Optionally warn about potentially worrisome casts. */
5578
5579 if (warn_cast_qual
5580 && TREE_CODE (type) == POINTER_TYPE
5581 && TREE_CODE (otype) == POINTER_TYPE)
5582 {
5583 /* For C++ we make these regular warnings, rather than
5584 softening them into pedwarns. */
5585 if (TYPE_VOLATILE (TREE_TYPE (otype))
5586 && ! TYPE_VOLATILE (TREE_TYPE (type)))
5587 warning ("cast discards `volatile' from pointer target type");
5588 if (TYPE_READONLY (TREE_TYPE (otype))
5589 && ! TYPE_READONLY (TREE_TYPE (type)))
5590 warning ("cast discards `const' from pointer target type");
5591 }
5592
5593 /* Warn about possible alignment problems. */
5594 if (STRICT_ALIGNMENT && warn_cast_align
5595 && TREE_CODE (type) == POINTER_TYPE
5596 && TREE_CODE (otype) == POINTER_TYPE
5597 && TREE_CODE (TREE_TYPE (otype)) != VOID_TYPE
5598 && TREE_CODE (TREE_TYPE (otype)) != FUNCTION_TYPE
5599 && TYPE_ALIGN (TREE_TYPE (type)) > TYPE_ALIGN (TREE_TYPE (otype)))
5600 warning ("cast increases required alignment of target type");
5601
5602 #if 0
5603 /* We should see about re-enabling these, they seem useful to
5604 me. */
5605 if (TREE_CODE (type) == INTEGER_TYPE
5606 && TREE_CODE (otype) == POINTER_TYPE
5607 && TYPE_PRECISION (type) != TYPE_PRECISION (otype))
5608 warning ("cast from pointer to integer of different size");
5609
5610 if (TREE_CODE (type) == POINTER_TYPE
5611 && TREE_CODE (otype) == INTEGER_TYPE
5612 && TYPE_PRECISION (type) != TYPE_PRECISION (otype)
5613 /* Don't warn about converting 0 to pointer,
5614 provided the 0 was explicit--not cast or made by folding. */
5615 && !(TREE_CODE (value) == INTEGER_CST && integer_zerop (value)))
5616 warning ("cast to pointer from integer of different size");
5617 #endif
5618
5619 if (TREE_CODE (type) == REFERENCE_TYPE)
5620 value = (convert_from_reference
5621 (convert_to_reference (type, value, CONV_C_CAST,
5622 LOOKUP_COMPLAIN, NULL_TREE)));
5623 else
5624 {
5625 tree ovalue;
5626
5627 if (TREE_READONLY_DECL_P (value))
5628 value = decl_constant_value (value);
5629
5630 ovalue = value;
5631 value = convert_force (type, value, CONV_C_CAST);
5632
5633 /* Ignore any integer overflow caused by the cast. */
5634 if (TREE_CODE (value) == INTEGER_CST)
5635 {
5636 TREE_OVERFLOW (value) = TREE_OVERFLOW (ovalue);
5637 TREE_CONSTANT_OVERFLOW (value) = TREE_CONSTANT_OVERFLOW (ovalue);
5638 }
5639 }
5640 }
5641
5642 /* Always produce some operator for an explicit cast,
5643 so we can tell (for -pedantic) that the cast is no lvalue. */
5644 if (TREE_CODE (type) != REFERENCE_TYPE && value == expr
5645 && real_lvalue_p (value))
5646 value = non_lvalue (value);
5647
5648 return value;
5649 }
5650 \f
5651 static tree
5652 expand_target_expr (t)
5653 tree t;
5654 {
5655 extern int temp_slot_level;
5656 extern int target_temp_slot_level;
5657 int old_temp_level = target_temp_slot_level;
5658
5659 tree xval = make_node (RTL_EXPR);
5660 rtx rtxval;
5661
5662 /* Any TARGET_EXPR temps live only as long as the outer temp level.
5663 Since they are preserved in this new inner level, we know they
5664 will make it into the outer level. */
5665 push_temp_slots ();
5666 target_temp_slot_level = temp_slot_level;
5667
5668 do_pending_stack_adjust ();
5669 start_sequence_for_rtl_expr (xval);
5670 emit_note (0, -1);
5671 rtxval = expand_expr (t, NULL_RTX, VOIDmode, EXPAND_NORMAL);
5672 do_pending_stack_adjust ();
5673 TREE_SIDE_EFFECTS (xval) = 1;
5674 RTL_EXPR_SEQUENCE (xval) = get_insns ();
5675 end_sequence ();
5676 RTL_EXPR_RTL (xval) = rtxval;
5677 TREE_TYPE (xval) = TREE_TYPE (t);
5678
5679 pop_temp_slots ();
5680 target_temp_slot_level = old_temp_level;
5681
5682 return xval;
5683 }
5684
5685 /* Build an assignment expression of lvalue LHS from value RHS.
5686 MODIFYCODE is the code for a binary operator that we use
5687 to combine the old value of LHS with RHS to get the new value.
5688 Or else MODIFYCODE is NOP_EXPR meaning do a simple assignment.
5689
5690 C++: If MODIFYCODE is INIT_EXPR, then leave references unbashed. */
5691
5692 tree
5693 build_modify_expr (lhs, modifycode, rhs)
5694 tree lhs;
5695 enum tree_code modifycode;
5696 tree rhs;
5697 {
5698 register tree result;
5699 tree newrhs = rhs;
5700 tree lhstype = TREE_TYPE (lhs);
5701 tree olhstype = lhstype;
5702 tree olhs = lhs;
5703
5704 /* Avoid duplicate error messages from operands that had errors. */
5705 if (lhs == error_mark_node || rhs == error_mark_node)
5706 return error_mark_node;
5707
5708 /* Types that aren't fully specified cannot be used in assignments. */
5709 lhs = require_complete_type (lhs);
5710
5711 newrhs = rhs;
5712
5713 /* Handle assignment to signature pointers/refs. */
5714
5715 if (TYPE_LANG_SPECIFIC (lhstype)
5716 && (IS_SIGNATURE_POINTER (lhstype) || IS_SIGNATURE_REFERENCE (lhstype)))
5717 {
5718 return build_signature_pointer_constructor (lhs, rhs);
5719 }
5720
5721 /* Handle control structure constructs used as "lvalues". */
5722
5723 switch (TREE_CODE (lhs))
5724 {
5725 /* Handle --foo = 5; as these are valid constructs in C++ */
5726 case PREDECREMENT_EXPR:
5727 case PREINCREMENT_EXPR:
5728 if (TREE_SIDE_EFFECTS (TREE_OPERAND (lhs, 0)))
5729 lhs = build (TREE_CODE (lhs), TREE_TYPE (lhs),
5730 stabilize_reference (TREE_OPERAND (lhs, 0)),
5731 TREE_OPERAND (lhs, 1));
5732 return build (COMPOUND_EXPR, lhstype,
5733 lhs,
5734 build_modify_expr (TREE_OPERAND (lhs, 0),
5735 modifycode, rhs));
5736
5737 /* Handle (a, b) used as an "lvalue". */
5738 case COMPOUND_EXPR:
5739 newrhs = build_modify_expr (TREE_OPERAND (lhs, 1),
5740 modifycode, rhs);
5741 if (newrhs == error_mark_node)
5742 return error_mark_node;
5743 return build (COMPOUND_EXPR, lhstype,
5744 TREE_OPERAND (lhs, 0), newrhs);
5745
5746 case MODIFY_EXPR:
5747 newrhs = build_modify_expr (TREE_OPERAND (lhs, 0), modifycode, rhs);
5748 if (newrhs == error_mark_node)
5749 return error_mark_node;
5750 return build (COMPOUND_EXPR, lhstype, lhs, newrhs);
5751
5752 /* Handle (a ? b : c) used as an "lvalue". */
5753 case COND_EXPR:
5754 rhs = save_expr (rhs);
5755 {
5756 /* Produce (a ? (b = rhs) : (c = rhs))
5757 except that the RHS goes through a save-expr
5758 so the code to compute it is only emitted once. */
5759 tree cond
5760 = build_conditional_expr (TREE_OPERAND (lhs, 0),
5761 build_modify_expr (cp_convert (TREE_TYPE (lhs), TREE_OPERAND (lhs, 1)),
5762 modifycode, rhs),
5763 build_modify_expr (cp_convert (TREE_TYPE (lhs), TREE_OPERAND (lhs, 2)),
5764 modifycode, rhs));
5765 if (cond == error_mark_node)
5766 return cond;
5767 /* Make sure the code to compute the rhs comes out
5768 before the split. */
5769 return build (COMPOUND_EXPR, TREE_TYPE (lhs),
5770 /* Case to void to suppress warning
5771 from warn_if_unused_value. */
5772 cp_convert (void_type_node, rhs), cond);
5773 }
5774 }
5775
5776 if (TREE_CODE (lhs) == OFFSET_REF)
5777 {
5778 if (TREE_OPERAND (lhs, 0) == NULL_TREE)
5779 {
5780 /* Static class member? */
5781 tree member = TREE_OPERAND (lhs, 1);
5782 if (TREE_CODE (member) == VAR_DECL)
5783 lhs = member;
5784 else
5785 {
5786 compiler_error ("invalid static class member");
5787 return error_mark_node;
5788 }
5789 }
5790 else
5791 lhs = resolve_offset_ref (lhs);
5792
5793 olhstype = lhstype = TREE_TYPE (lhs);
5794 }
5795
5796 if (TREE_CODE (lhstype) == REFERENCE_TYPE
5797 && modifycode != INIT_EXPR)
5798 {
5799 lhs = convert_from_reference (lhs);
5800 olhstype = lhstype = TREE_TYPE (lhs);
5801 }
5802
5803 /* If a binary op has been requested, combine the old LHS value with the RHS
5804 producing the value we should actually store into the LHS. */
5805
5806 if (modifycode == INIT_EXPR)
5807 {
5808 if (! IS_AGGR_TYPE (lhstype))
5809 /* Do the default thing */;
5810 else if (! TYPE_HAS_CONSTRUCTOR (lhstype))
5811 {
5812 cp_error ("`%T' has no constructors", lhstype);
5813 return error_mark_node;
5814 }
5815 else if (TYPE_HAS_TRIVIAL_INIT_REF (lhstype)
5816 && TYPE_MAIN_VARIANT (lhstype) == TYPE_MAIN_VARIANT (TREE_TYPE (newrhs)))
5817 /* Do the default thing */;
5818 else
5819 {
5820 result = build_method_call (lhs, ctor_identifier,
5821 build_expr_list (NULL_TREE, rhs),
5822 TYPE_BINFO (lhstype), LOOKUP_NORMAL);
5823 if (result == NULL_TREE)
5824 return error_mark_node;
5825 return result;
5826 }
5827 }
5828 else if (modifycode == NOP_EXPR)
5829 {
5830 /* `operator=' is not an inheritable operator. */
5831 if (! IS_AGGR_TYPE (lhstype))
5832 /* Do the default thing */;
5833 else if (! TYPE_HAS_ASSIGNMENT (lhstype))
5834 {
5835 cp_error ("`%T' does not define operator=", lhstype);
5836 return error_mark_node;
5837 }
5838 else if (TYPE_HAS_TRIVIAL_ASSIGN_REF (lhstype)
5839 && TYPE_MAIN_VARIANT (lhstype) == TYPE_MAIN_VARIANT (TREE_TYPE (newrhs)))
5840 {
5841 build_opfncall (MODIFY_EXPR, LOOKUP_NORMAL,
5842 lhs, rhs, make_node (NOP_EXPR));
5843
5844 /* Do the default thing */;
5845 }
5846 else
5847 {
5848 result = build_opfncall (MODIFY_EXPR, LOOKUP_NORMAL,
5849 lhs, rhs, make_node (NOP_EXPR));
5850 if (result == NULL_TREE)
5851 return error_mark_node;
5852 return result;
5853 }
5854 lhstype = olhstype;
5855 }
5856 else if (PROMOTES_TO_AGGR_TYPE (lhstype, REFERENCE_TYPE))
5857 {
5858 /* This case must convert to some sort of lvalue that
5859 can participate in an op= operation. */
5860 tree lhs_tmp = lhs;
5861 tree rhs_tmp = rhs;
5862 if (build_default_binary_type_conversion (modifycode, &lhs_tmp, &rhs_tmp))
5863 {
5864 lhs = stabilize_reference (lhs_tmp);
5865 /* Forget it was ever anything else. */
5866 olhstype = lhstype = TREE_TYPE (lhs);
5867 newrhs = build_binary_op (modifycode, lhs, rhs_tmp, 1);
5868 }
5869 else
5870 {
5871 cp_error ("no match for `%Q(%#T, %#T)'", modifycode,
5872 TREE_TYPE (lhs), TREE_TYPE (rhs));
5873 return error_mark_node;
5874 }
5875 }
5876 else
5877 {
5878 lhs = stabilize_reference (lhs);
5879 newrhs = build_binary_op (modifycode, lhs, rhs, 1);
5880 if (newrhs == error_mark_node)
5881 {
5882 cp_error (" in evaluation of `%Q(%#T, %#T)'", modifycode,
5883 TREE_TYPE (lhs), TREE_TYPE (rhs));
5884 return error_mark_node;
5885 }
5886 }
5887
5888 /* Handle a cast used as an "lvalue".
5889 We have already performed any binary operator using the value as cast.
5890 Now convert the result to the cast type of the lhs,
5891 and then true type of the lhs and store it there;
5892 then convert result back to the cast type to be the value
5893 of the assignment. */
5894
5895 switch (TREE_CODE (lhs))
5896 {
5897 case NOP_EXPR:
5898 case CONVERT_EXPR:
5899 case FLOAT_EXPR:
5900 case FIX_TRUNC_EXPR:
5901 case FIX_FLOOR_EXPR:
5902 case FIX_ROUND_EXPR:
5903 case FIX_CEIL_EXPR:
5904 if (TREE_CODE (TREE_TYPE (newrhs)) == ARRAY_TYPE
5905 || TREE_CODE (TREE_TYPE (newrhs)) == FUNCTION_TYPE
5906 || TREE_CODE (TREE_TYPE (newrhs)) == METHOD_TYPE
5907 || TREE_CODE (TREE_TYPE (newrhs)) == OFFSET_TYPE)
5908 newrhs = default_conversion (newrhs);
5909 {
5910 tree inner_lhs = TREE_OPERAND (lhs, 0);
5911 tree result;
5912
5913 /* WP 5.4.1: The result is an lvalue if T is a reference type,
5914 otherwise the result is an rvalue. */
5915 if (! lvalue_p (lhs))
5916 pedwarn ("ANSI C++ forbids cast to non-reference type used as lvalue");
5917
5918 result = build_modify_expr (inner_lhs, NOP_EXPR,
5919 cp_convert (TREE_TYPE (inner_lhs),
5920 cp_convert (lhstype, newrhs)));
5921 if (result == error_mark_node)
5922 return result;
5923 return cp_convert (TREE_TYPE (lhs), result);
5924 }
5925 }
5926
5927 /* Now we have handled acceptable kinds of LHS that are not truly lvalues.
5928 Reject anything strange now. */
5929
5930 if (!lvalue_or_else (lhs, "assignment"))
5931 return error_mark_node;
5932
5933 GNU_xref_assign (lhs);
5934
5935 /* Warn about storing in something that is `const'. */
5936 /* For C++, don't warn if this is initialization. */
5937 if (modifycode != INIT_EXPR
5938 /* For assignment to `const' signature pointer/reference fields,
5939 don't warn either, we already printed a better message before. */
5940 && ! (TREE_CODE (lhs) == COMPONENT_REF
5941 && (IS_SIGNATURE_POINTER (TREE_TYPE (TREE_OPERAND (lhs, 0)))
5942 || IS_SIGNATURE_REFERENCE (TREE_TYPE (TREE_OPERAND (lhs, 0)))))
5943 && (TREE_READONLY (lhs) || TYPE_READONLY (lhstype)
5944 || ((TREE_CODE (lhstype) == RECORD_TYPE
5945 || TREE_CODE (lhstype) == UNION_TYPE)
5946 && C_TYPE_FIELDS_READONLY (lhstype))
5947 || (TREE_CODE (lhstype) == REFERENCE_TYPE
5948 && TYPE_READONLY (TREE_TYPE (lhstype)))))
5949 readonly_error (lhs, "assignment", 0);
5950
5951 /* If storing into a structure or union member,
5952 it has probably been given type `int'.
5953 Compute the type that would go with
5954 the actual amount of storage the member occupies. */
5955
5956 if (TREE_CODE (lhs) == COMPONENT_REF
5957 && (TREE_CODE (lhstype) == INTEGER_TYPE
5958 || TREE_CODE (lhstype) == REAL_TYPE
5959 || TREE_CODE (lhstype) == ENUMERAL_TYPE))
5960 {
5961 lhstype = TREE_TYPE (get_unwidened (lhs, 0));
5962
5963 /* If storing in a field that is in actuality a short or narrower
5964 than one, we must store in the field in its actual type. */
5965
5966 if (lhstype != TREE_TYPE (lhs))
5967 {
5968 lhs = copy_node (lhs);
5969 TREE_TYPE (lhs) = lhstype;
5970 }
5971 }
5972
5973 /* check to see if there is an assignment to `this' */
5974 if (lhs == current_class_ptr)
5975 {
5976 if (flag_this_is_variable > 0
5977 && DECL_NAME (current_function_decl) != NULL_TREE
5978 && (DECL_NAME (current_function_decl)
5979 != constructor_name (current_class_type)))
5980 warning ("assignment to `this' not in constructor or destructor");
5981 current_function_just_assigned_this = 1;
5982 }
5983
5984 /* The TREE_TYPE of RHS may be TYPE_UNKNOWN. This can happen
5985 when the type of RHS is not yet known, i.e. its type
5986 is inherited from LHS. */
5987 rhs = require_instantiated_type (lhstype, newrhs, error_mark_node);
5988 if (rhs == error_mark_node)
5989 return error_mark_node;
5990 newrhs = rhs;
5991
5992 if (modifycode != INIT_EXPR)
5993 {
5994 /* Make modifycode now either a NOP_EXPR or an INIT_EXPR. */
5995 modifycode = NOP_EXPR;
5996 /* Reference-bashing */
5997 if (TREE_CODE (lhstype) == REFERENCE_TYPE)
5998 {
5999 tree tmp = convert_from_reference (lhs);
6000 lhstype = TREE_TYPE (tmp);
6001 if (TYPE_SIZE (lhstype) == 0)
6002 {
6003 incomplete_type_error (lhs, lhstype);
6004 return error_mark_node;
6005 }
6006 lhs = tmp;
6007 olhstype = lhstype;
6008 }
6009 if (TREE_CODE (TREE_TYPE (newrhs)) == REFERENCE_TYPE)
6010 {
6011 tree tmp = convert_from_reference (newrhs);
6012 if (TYPE_SIZE (TREE_TYPE (tmp)) == 0)
6013 {
6014 incomplete_type_error (newrhs, TREE_TYPE (tmp));
6015 return error_mark_node;
6016 }
6017 newrhs = tmp;
6018 }
6019 }
6020
6021 if (TREE_SIDE_EFFECTS (lhs))
6022 lhs = stabilize_reference (lhs);
6023 if (TREE_SIDE_EFFECTS (newrhs))
6024 newrhs = stabilize_reference (newrhs);
6025
6026 /* Convert new value to destination type. */
6027
6028 if (TREE_CODE (lhstype) == ARRAY_TYPE)
6029 {
6030 int from_array;
6031
6032 if (! comptypes (lhstype, TREE_TYPE (rhs), 0))
6033 {
6034 cp_error ("incompatible types in assignment of `%T' to `%T'",
6035 TREE_TYPE (rhs), lhstype);
6036 return error_mark_node;
6037 }
6038
6039 /* Allow array assignment in compiler-generated code. */
6040 if (pedantic && ! DECL_ARTIFICIAL (current_function_decl))
6041 pedwarn ("ANSI C++ forbids assignment of arrays");
6042
6043 /* Have to wrap this in RTL_EXPR for two cases:
6044 in base or member initialization and if we
6045 are a branch of a ?: operator. Since we
6046 can't easily know the latter, just do it always. */
6047
6048 result = make_node (RTL_EXPR);
6049
6050 TREE_TYPE (result) = void_type_node;
6051 do_pending_stack_adjust ();
6052 start_sequence_for_rtl_expr (result);
6053
6054 /* As a matter of principle, `start_sequence' should do this. */
6055 emit_note (0, -1);
6056
6057 from_array = TREE_CODE (TREE_TYPE (newrhs)) == ARRAY_TYPE
6058 ? 1 + (modifycode != INIT_EXPR): 0;
6059 expand_vec_init (lhs, lhs, array_type_nelts (lhstype), newrhs,
6060 from_array);
6061
6062 do_pending_stack_adjust ();
6063
6064 TREE_SIDE_EFFECTS (result) = 1;
6065 RTL_EXPR_SEQUENCE (result) = get_insns ();
6066 RTL_EXPR_RTL (result) = const0_rtx;
6067 end_sequence ();
6068 return result;
6069 }
6070
6071 if (modifycode == INIT_EXPR)
6072 {
6073 newrhs = convert_for_initialization (lhs, lhstype, newrhs, LOOKUP_NORMAL,
6074 "assignment", NULL_TREE, 0);
6075 if (lhs == DECL_RESULT (current_function_decl))
6076 {
6077 if (DECL_INITIAL (lhs))
6078 warning ("return value from function receives multiple initializations");
6079 DECL_INITIAL (lhs) = newrhs;
6080 }
6081 }
6082 else
6083 {
6084 /* Avoid warnings on enum bit fields. */
6085 if (TREE_CODE (olhstype) == ENUMERAL_TYPE
6086 && TREE_CODE (lhstype) == INTEGER_TYPE)
6087 {
6088 newrhs = convert_for_assignment (olhstype, newrhs, "assignment",
6089 NULL_TREE, 0);
6090 newrhs = convert_force (lhstype, newrhs, 0);
6091 }
6092 else
6093 newrhs = convert_for_assignment (lhstype, newrhs, "assignment",
6094 NULL_TREE, 0);
6095 if (TREE_CODE (newrhs) == CALL_EXPR
6096 && TYPE_NEEDS_CONSTRUCTING (lhstype))
6097 newrhs = build_cplus_new (lhstype, newrhs);
6098
6099 /* Can't initialize directly from a TARGET_EXPR, since that would
6100 cause the lhs to be constructed twice, and possibly result in
6101 accidental self-initialization. So we force the TARGET_EXPR to be
6102 expanded without a target. */
6103 if (TREE_CODE (newrhs) == TARGET_EXPR)
6104 newrhs = build (COMPOUND_EXPR, TREE_TYPE (newrhs), newrhs,
6105 TREE_OPERAND (newrhs, 0));
6106 }
6107
6108 if (newrhs == error_mark_node)
6109 return error_mark_node;
6110
6111 if (TREE_CODE (newrhs) == COND_EXPR)
6112 {
6113 tree lhs1;
6114 tree cond = TREE_OPERAND (newrhs, 0);
6115
6116 if (TREE_SIDE_EFFECTS (lhs))
6117 cond = build_compound_expr (tree_cons
6118 (NULL_TREE, lhs,
6119 build_expr_list (NULL_TREE, cond)));
6120
6121 /* Cannot have two identical lhs on this one tree (result) as preexpand
6122 calls will rip them out and fill in RTL for them, but when the
6123 rtl is generated, the calls will only be in the first side of the
6124 condition, not on both, or before the conditional jump! (mrs) */
6125 lhs1 = break_out_calls (lhs);
6126
6127 if (lhs == lhs1)
6128 /* If there's no change, the COND_EXPR behaves like any other rhs. */
6129 result = build (modifycode == NOP_EXPR ? MODIFY_EXPR : INIT_EXPR,
6130 lhstype, lhs, newrhs);
6131 else
6132 {
6133 tree result_type = TREE_TYPE (newrhs);
6134 /* We have to convert each arm to the proper type because the
6135 types may have been munged by constant folding. */
6136 result
6137 = build (COND_EXPR, result_type, cond,
6138 build_modify_expr (lhs, modifycode,
6139 cp_convert (result_type,
6140 TREE_OPERAND (newrhs, 1))),
6141 build_modify_expr (lhs1, modifycode,
6142 cp_convert (result_type,
6143 TREE_OPERAND (newrhs, 2))));
6144 }
6145 }
6146 else
6147 result = build (modifycode == NOP_EXPR ? MODIFY_EXPR : INIT_EXPR,
6148 lhstype, lhs, newrhs);
6149
6150 TREE_SIDE_EFFECTS (result) = 1;
6151
6152 /* If we got the LHS in a different type for storing in,
6153 convert the result back to the nominal type of LHS
6154 so that the value we return always has the same type
6155 as the LHS argument. */
6156
6157 if (olhstype == TREE_TYPE (result))
6158 return result;
6159 /* Avoid warnings converting integral types back into enums
6160 for enum bit fields. */
6161 if (TREE_CODE (TREE_TYPE (result)) == INTEGER_TYPE
6162 && TREE_CODE (olhstype) == ENUMERAL_TYPE)
6163 {
6164 result = build (COMPOUND_EXPR, olhstype, result, olhs);
6165 TREE_NO_UNUSED_WARNING (result) = 1;
6166 return result;
6167 }
6168 return convert_for_assignment (olhstype, result, "assignment",
6169 NULL_TREE, 0);
6170 }
6171
6172 tree
6173 build_x_modify_expr (lhs, modifycode, rhs)
6174 tree lhs;
6175 enum tree_code modifycode;
6176 tree rhs;
6177 {
6178 if (processing_template_decl)
6179 return build_min_nt (MODOP_EXPR, lhs,
6180 build_min_nt (modifycode, NULL_TREE, NULL_TREE), rhs);
6181
6182 if (modifycode != NOP_EXPR)
6183 {
6184 tree rval = build_opfncall (MODIFY_EXPR, LOOKUP_NORMAL, lhs, rhs,
6185 make_node (modifycode));
6186 if (rval)
6187 return rval;
6188 }
6189 return build_modify_expr (lhs, modifycode, rhs);
6190 }
6191
6192 /* Return 0 if EXP is not a valid lvalue in this language
6193 even though `lvalue_or_else' would accept it. */
6194
6195 int
6196 language_lvalue_valid (exp)
6197 tree exp;
6198 {
6199 return 1;
6200 }
6201 \f
6202 /* Get difference in deltas for different pointer to member function
6203 types. Return integer_zero_node, if FROM cannot be converted to a
6204 TO type. If FORCE is true, then allow reverse conversions as well. */
6205
6206 static tree
6207 get_delta_difference (from, to, force)
6208 tree from, to;
6209 int force;
6210 {
6211 tree delta = integer_zero_node;
6212 tree binfo;
6213
6214 if (to == from)
6215 return delta;
6216
6217 /* Should get_base_distance here, so we can check if any thing along the
6218 path is virtual, and we need to make sure we stay
6219 inside the real binfos when going through virtual bases.
6220 Maybe we should replace virtual bases with
6221 binfo_member (...CLASSTYPE_VBASECLASSES...)... (mrs) */
6222 binfo = get_binfo (from, to, 1);
6223 if (binfo == error_mark_node)
6224 {
6225 error (" in pointer to member function conversiona");
6226 return delta;
6227 }
6228 if (binfo == 0)
6229 {
6230 if (!force)
6231 {
6232 error_not_base_type (from, to);
6233 error (" in pointer to member conversion");
6234 return delta;
6235 }
6236 binfo = get_binfo (to, from, 1);
6237 if (binfo == error_mark_node)
6238 {
6239 if (!force)
6240 error (" in pointer to member conversion");
6241 return delta;
6242 }
6243 if (binfo == 0)
6244 {
6245 if (!force)
6246 cp_error ("cannot convert pointer to member of type %T to unrelated pointer to member of type %T", from, to);
6247 return delta;
6248 }
6249 if (TREE_VIA_VIRTUAL (binfo))
6250 {
6251 binfo = binfo_member (BINFO_TYPE (binfo),
6252 CLASSTYPE_VBASECLASSES (from));
6253 warning ("pointer to member conversion to virtual base class will only work if you are very careful");
6254 }
6255 delta = BINFO_OFFSET (binfo);
6256 delta = cp_convert (ptrdiff_type_node, delta);
6257
6258 return build_binary_op (MINUS_EXPR,
6259 integer_zero_node,
6260 delta, 1);
6261 }
6262 if (TREE_VIA_VIRTUAL (binfo))
6263 {
6264 warning ("pointer to member conversion from virtual base class will only work if you are very careful");
6265 }
6266 return BINFO_OFFSET (binfo);
6267 }
6268
6269 static tree
6270 build_ptrmemfunc1 (type, delta, idx, pfn, delta2)
6271 tree type, delta, idx, pfn, delta2;
6272 {
6273 tree u;
6274
6275 #if 0
6276 /* This is the old way we did it. We want to avoid calling
6277 digest_init, so that it can give an error if we use { } when
6278 initializing a pointer to member function. */
6279
6280 if (pfn)
6281 {
6282 u = build_nt (CONSTRUCTOR, NULL_TREE,
6283 expr_tree_cons (pfn_identifier, pfn, NULL_TREE));
6284 }
6285 else
6286 {
6287 u = build_nt (CONSTRUCTOR, NULL_TREE,
6288 expr_tree_cons (delta2_identifier, delta2, NULL_TREE));
6289 }
6290
6291 u = build_nt (CONSTRUCTOR, NULL_TREE,
6292 expr_tree_cons (NULL_TREE, delta,
6293 expr_tree_cons (NULL_TREE, idx,
6294 expr_tree_cons (NULL_TREE, u, NULL_TREE))));
6295
6296 return digest_init (type, u, (tree*)0);
6297 #else
6298 tree delta_field, idx_field, pfn_or_delta2_field, pfn_field, delta2_field;
6299 tree subtype;
6300 int allconstant, allsimple;
6301
6302 delta_field = TYPE_FIELDS (type);
6303 idx_field = TREE_CHAIN (delta_field);
6304 pfn_or_delta2_field = TREE_CHAIN (idx_field);
6305 subtype = TREE_TYPE (pfn_or_delta2_field);
6306 pfn_field = TYPE_FIELDS (subtype);
6307 delta2_field = TREE_CHAIN (pfn_field);
6308
6309 if (pfn)
6310 {
6311 allconstant = TREE_CONSTANT (pfn);
6312 allsimple = !! initializer_constant_valid_p (pfn, TREE_TYPE (pfn));
6313 u = expr_tree_cons (pfn_field, pfn, NULL_TREE);
6314 }
6315 else
6316 {
6317 delta2 = convert_and_check (delta_type_node, delta2);
6318 allconstant = TREE_CONSTANT (delta2);
6319 allsimple = !! initializer_constant_valid_p (delta2, TREE_TYPE (delta2));
6320 u = expr_tree_cons (delta2_field, delta2, NULL_TREE);
6321 }
6322
6323 delta = convert_and_check (delta_type_node, delta);
6324 idx = convert_and_check (delta_type_node, idx);
6325
6326 allconstant = allconstant && TREE_CONSTANT (delta) && TREE_CONSTANT (idx);
6327 allsimple = allsimple
6328 && initializer_constant_valid_p (delta, TREE_TYPE (delta))
6329 && initializer_constant_valid_p (idx, TREE_TYPE (idx));
6330
6331 u = build (CONSTRUCTOR, subtype, NULL_TREE, u);
6332 u = expr_tree_cons (delta_field, delta,
6333 expr_tree_cons (idx_field, idx,
6334 expr_tree_cons (pfn_or_delta2_field, u, NULL_TREE)));
6335 u = build (CONSTRUCTOR, type, NULL_TREE, u);
6336 TREE_CONSTANT (u) = allconstant;
6337 TREE_STATIC (u) = allconstant && allsimple;
6338 return u;
6339 #endif
6340 }
6341
6342 /* Build a constructor for a pointer to member function. It can be
6343 used to initialize global variables, local variable, or used
6344 as a value in expressions. TYPE is the POINTER to METHOD_TYPE we
6345 want to be.
6346
6347 If FORCE is non-zero, then force this conversion, even if
6348 we would rather not do it. Usually set when using an explicit
6349 cast.
6350
6351 Return error_mark_node, if something goes wrong. */
6352
6353 tree
6354 build_ptrmemfunc (type, pfn, force)
6355 tree type, pfn;
6356 int force;
6357 {
6358 tree idx = integer_zero_node;
6359 tree delta = integer_zero_node;
6360 tree delta2 = integer_zero_node;
6361 tree vfield_offset;
6362 tree npfn = NULL_TREE;
6363
6364 /* Handle multiple conversions of pointer to member functions. */
6365 if (TYPE_PTRMEMFUNC_P (TREE_TYPE (pfn)))
6366 {
6367 tree ndelta, ndelta2;
6368 tree e1, e2, e3, n;
6369
6370 /* Is is already the right type? */
6371 if (type == TYPE_PTRMEMFUNC_FN_TYPE (TREE_TYPE (pfn)))
6372 return pfn;
6373
6374 ndelta = cp_convert (ptrdiff_type_node, build_component_ref (pfn, delta_identifier, NULL_TREE, 0));
6375 ndelta2 = cp_convert (ptrdiff_type_node, DELTA2_FROM_PTRMEMFUNC (pfn));
6376 idx = build_component_ref (pfn, index_identifier, NULL_TREE, 0);
6377
6378 n = get_delta_difference (TYPE_METHOD_BASETYPE (TREE_TYPE (TYPE_PTRMEMFUNC_FN_TYPE (TREE_TYPE (pfn)))),
6379 TYPE_METHOD_BASETYPE (TREE_TYPE (type)),
6380 force);
6381
6382 delta = build_binary_op (PLUS_EXPR, ndelta, n, 1);
6383 delta2 = build_binary_op (PLUS_EXPR, ndelta2, n, 1);
6384 e1 = fold (build (GT_EXPR, boolean_type_node, idx, integer_zero_node));
6385
6386 e2 = build_ptrmemfunc1 (TYPE_GET_PTRMEMFUNC_TYPE (type), delta, idx,
6387 NULL_TREE, delta2);
6388
6389 pfn = PFN_FROM_PTRMEMFUNC (pfn);
6390 npfn = build1 (NOP_EXPR, type, pfn);
6391 TREE_CONSTANT (npfn) = TREE_CONSTANT (pfn);
6392
6393 e3 = build_ptrmemfunc1 (TYPE_GET_PTRMEMFUNC_TYPE (type), delta, idx, npfn,
6394 NULL_TREE);
6395 return build_conditional_expr (e1, e2, e3);
6396 }
6397
6398 /* Handle null pointer to member function conversions. */
6399 if (integer_zerop (pfn))
6400 {
6401 pfn = build_c_cast (type, integer_zero_node);
6402 return build_ptrmemfunc1 (TYPE_GET_PTRMEMFUNC_TYPE (type),
6403 integer_zero_node, integer_zero_node,
6404 pfn, NULL_TREE);
6405 }
6406
6407 if (TREE_CODE (pfn) == TREE_LIST
6408 || (TREE_CODE (pfn) == ADDR_EXPR
6409 && TREE_CODE (TREE_OPERAND (pfn, 0)) == TREE_LIST))
6410 return instantiate_type (type, pfn, 1);
6411
6412 /* Allow pointer to member conversions here. */
6413 delta = get_delta_difference (TYPE_METHOD_BASETYPE (TREE_TYPE (TREE_TYPE (pfn))),
6414 TYPE_METHOD_BASETYPE (TREE_TYPE (type)),
6415 force);
6416 delta2 = build_binary_op (PLUS_EXPR, delta2, delta, 1);
6417
6418 #if 0
6419 /* We need to check the argument types to see if they are compatible
6420 (any const or volatile violations. */
6421 something like this:
6422 comptype (TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (type))),
6423 TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (TREE_TYPE (pfn)))), ?);
6424 #endif
6425
6426 if (TREE_CODE (TREE_OPERAND (pfn, 0)) != FUNCTION_DECL)
6427 warning ("assuming pointer to member function is non-virtual");
6428
6429 if (TREE_CODE (TREE_OPERAND (pfn, 0)) == FUNCTION_DECL
6430 && DECL_VINDEX (TREE_OPERAND (pfn, 0)))
6431 {
6432 /* Find the offset to the vfield pointer in the object. */
6433 vfield_offset = get_binfo (DECL_CONTEXT (TREE_OPERAND (pfn, 0)),
6434 DECL_CLASS_CONTEXT (TREE_OPERAND (pfn, 0)),
6435 0);
6436 vfield_offset = get_vfield_offset (vfield_offset);
6437 delta2 = size_binop (PLUS_EXPR, vfield_offset, delta2);
6438
6439 /* Map everything down one to make room for the null pointer to member. */
6440 idx = size_binop (PLUS_EXPR,
6441 DECL_VINDEX (TREE_OPERAND (pfn, 0)),
6442 integer_one_node);
6443 }
6444 else
6445 {
6446 idx = size_binop (MINUS_EXPR, integer_zero_node, integer_one_node);
6447
6448 if (type == TREE_TYPE (pfn))
6449 {
6450 npfn = pfn;
6451 }
6452 else
6453 {
6454 npfn = build1 (NOP_EXPR, type, pfn);
6455 TREE_CONSTANT (npfn) = TREE_CONSTANT (pfn);
6456 }
6457 }
6458
6459 return build_ptrmemfunc1 (TYPE_GET_PTRMEMFUNC_TYPE (type), delta, idx, npfn, delta2);
6460 }
6461
6462 /* Convert value RHS to type TYPE as preparation for an assignment
6463 to an lvalue of type TYPE.
6464 The real work of conversion is done by `convert'.
6465 The purpose of this function is to generate error messages
6466 for assignments that are not allowed in C.
6467 ERRTYPE is a string to use in error messages:
6468 "assignment", "return", etc.
6469
6470 C++: attempts to allow `convert' to find conversions involving
6471 implicit type conversion between aggregate and scalar types
6472 as per 8.5.6 of C++ manual. Does not randomly dereference
6473 pointers to aggregates! */
6474
6475 static tree
6476 convert_for_assignment (type, rhs, errtype, fndecl, parmnum)
6477 tree type, rhs;
6478 char *errtype;
6479 tree fndecl;
6480 int parmnum;
6481 {
6482 register enum tree_code codel = TREE_CODE (type);
6483 register tree rhstype;
6484 register enum tree_code coder = TREE_CODE (TREE_TYPE (rhs));
6485
6486 if (coder == UNKNOWN_TYPE)
6487 rhs = instantiate_type (type, rhs, 1);
6488
6489 if (coder == ERROR_MARK)
6490 return error_mark_node;
6491
6492 if (codel == OFFSET_TYPE)
6493 {
6494 type = TREE_TYPE (type);
6495 codel = TREE_CODE (type);
6496 }
6497
6498 /* Strip NON_LVALUE_EXPRs since we aren't using as an lvalue. */
6499 if (TREE_CODE (rhs) == NON_LVALUE_EXPR)
6500 rhs = TREE_OPERAND (rhs, 0);
6501
6502 if (rhs == error_mark_node)
6503 return error_mark_node;
6504
6505 if (TREE_VALUE (rhs) == error_mark_node)
6506 return error_mark_node;
6507
6508 if (TREE_CODE (TREE_TYPE (rhs)) == OFFSET_TYPE)
6509 {
6510 rhs = resolve_offset_ref (rhs);
6511 if (rhs == error_mark_node)
6512 return error_mark_node;
6513 rhstype = TREE_TYPE (rhs);
6514 coder = TREE_CODE (rhstype);
6515 }
6516
6517 if (TREE_CODE (TREE_TYPE (rhs)) == ARRAY_TYPE
6518 || TREE_CODE (TREE_TYPE (rhs)) == FUNCTION_TYPE
6519 || TREE_CODE (TREE_TYPE (rhs)) == METHOD_TYPE)
6520 rhs = default_conversion (rhs);
6521 else if (TREE_CODE (TREE_TYPE (rhs)) == REFERENCE_TYPE)
6522 rhs = convert_from_reference (rhs);
6523
6524 rhstype = TREE_TYPE (rhs);
6525 coder = TREE_CODE (rhstype);
6526
6527 /* This should no longer change types on us. */
6528 if (TREE_CODE (rhs) == CONST_DECL)
6529 rhs = DECL_INITIAL (rhs);
6530 else if (TREE_READONLY_DECL_P (rhs))
6531 rhs = decl_constant_value (rhs);
6532
6533 if (type == rhstype)
6534 {
6535 overflow_warning (rhs);
6536 return rhs;
6537 }
6538
6539 if (coder == VOID_TYPE)
6540 {
6541 error ("void value not ignored as it ought to be");
6542 return error_mark_node;
6543 }
6544 /* Arithmetic types all interconvert. */
6545 if ((codel == INTEGER_TYPE || codel == REAL_TYPE || codel == BOOLEAN_TYPE
6546 || codel == COMPLEX_TYPE)
6547 && (coder == INTEGER_TYPE || coder == REAL_TYPE || coder == BOOLEAN_TYPE
6548 || coder == COMPLEX_TYPE))
6549 {
6550 /* But we should warn if assigning REAL_TYPE to INTEGER_TYPE. */
6551 if (coder == REAL_TYPE && codel == INTEGER_TYPE)
6552 {
6553 if (fndecl)
6554 cp_warning ("`%T' used for argument %P of `%D'",
6555 rhstype, parmnum, fndecl);
6556 else
6557 cp_warning ("%s to `%T' from `%T'", errtype, type, rhstype);
6558 }
6559 /* And we should warn if assigning a negative value to
6560 an unsigned variable. */
6561 else if (TREE_UNSIGNED (type) && codel != BOOLEAN_TYPE)
6562 {
6563 if (TREE_CODE (rhs) == INTEGER_CST
6564 && TREE_NEGATED_INT (rhs))
6565 {
6566 if (fndecl)
6567 cp_warning ("negative value `%E' passed as argument %P of `%D'",
6568 rhs, parmnum, fndecl);
6569 else
6570 cp_warning ("%s of negative value `%E' to `%T'",
6571 errtype, rhs, type);
6572 }
6573 overflow_warning (rhs);
6574 if (TREE_CONSTANT (rhs))
6575 rhs = fold (rhs);
6576 }
6577
6578 return convert_and_check (type, rhs);
6579 }
6580 /* Conversions involving enums. */
6581 else if ((codel == ENUMERAL_TYPE
6582 && (INTEGRAL_CODE_P (coder) || coder == REAL_TYPE))
6583 || (coder == ENUMERAL_TYPE
6584 && (INTEGRAL_CODE_P (codel) || codel == REAL_TYPE)))
6585 {
6586 return ocp_convert (type, rhs, CONV_IMPLICIT, LOOKUP_NORMAL);
6587 }
6588 /* Conversions among pointers */
6589 else if (codel == POINTER_TYPE
6590 && (coder == POINTER_TYPE
6591 || (coder == RECORD_TYPE
6592 && (IS_SIGNATURE_POINTER (rhstype)
6593 || IS_SIGNATURE_REFERENCE (rhstype)))))
6594 {
6595 register tree ttl = TREE_TYPE (type);
6596 register tree ttr;
6597 int ctt = 0;
6598
6599 if (coder == RECORD_TYPE)
6600 {
6601 rhs = build_optr_ref (rhs);
6602 rhstype = TREE_TYPE (rhs);
6603 }
6604 ttr = TREE_TYPE (rhstype);
6605
6606 /* If both pointers are of aggregate type, then we
6607 can give better error messages, and save some work
6608 as well. */
6609 if (TREE_CODE (ttl) == RECORD_TYPE && TREE_CODE (ttr) == RECORD_TYPE)
6610 {
6611 tree binfo;
6612
6613 if (TYPE_MAIN_VARIANT (ttl) == TYPE_MAIN_VARIANT (ttr)
6614 || type == class_star_type_node
6615 || rhstype == class_star_type_node)
6616 binfo = TYPE_BINFO (ttl);
6617 else
6618 binfo = get_binfo (ttl, ttr, 1);
6619
6620 if (binfo == error_mark_node)
6621 return error_mark_node;
6622 if (binfo == 0)
6623 return error_not_base_type (ttl, ttr);
6624
6625 if (! TYPE_READONLY (ttl) && TYPE_READONLY (ttr))
6626 {
6627 if (fndecl)
6628 cp_pedwarn ("passing `%T' as argument %P of `%D' discards const",
6629 rhstype, parmnum, fndecl);
6630 else
6631 cp_pedwarn ("%s to `%T' from `%T' discards const",
6632 errtype, type, rhstype);
6633 }
6634 if (! TYPE_VOLATILE (ttl) && TYPE_VOLATILE (ttr))
6635 {
6636 if (fndecl)
6637 cp_pedwarn ("passing `%T' as argument %P of `%D' discards volatile",
6638 rhstype, parmnum, fndecl);
6639 else
6640 cp_pedwarn ("%s to `%T' from `%T' discards volatile",
6641 errtype, type, rhstype);
6642 }
6643 }
6644
6645 /* Any non-function converts to a [const][volatile] void *
6646 and vice versa; otherwise, targets must be the same.
6647 Meanwhile, the lhs target must have all the qualifiers of the rhs. */
6648 else if (TYPE_MAIN_VARIANT (ttl) == void_type_node
6649 || TYPE_MAIN_VARIANT (ttr) == void_type_node
6650 || (ctt = comp_target_types (type, rhstype, 1))
6651 || (unsigned_type (TYPE_MAIN_VARIANT (ttl))
6652 == unsigned_type (TYPE_MAIN_VARIANT (ttr))))
6653 {
6654 /* ARM $4.8, commentary on p39. */
6655 if (TYPE_MAIN_VARIANT (ttl) == void_type_node
6656 && TREE_CODE (ttr) == OFFSET_TYPE)
6657 {
6658 cp_error ("no standard conversion from `%T' to `void *'", ttr);
6659 return error_mark_node;
6660 }
6661
6662 if (ctt < 0 && TYPE_MAIN_VARIANT (ttl) != TYPE_MAIN_VARIANT (ttr))
6663 cp_pedwarn ("converting `%T' to `%T' is a contravariance violation",
6664 rhstype, type);
6665
6666 if (TYPE_MAIN_VARIANT (ttl) != void_type_node
6667 && TYPE_MAIN_VARIANT (ttr) == void_type_node
6668 && ! null_ptr_cst_p (rhs))
6669 {
6670 if (coder == RECORD_TYPE)
6671 cp_pedwarn ("implicit conversion of signature pointer to type `%T'",
6672 type);
6673 else
6674 pedwarn ("ANSI C++ forbids implicit conversion from `void *' in %s",
6675 errtype);
6676 }
6677 /* Const and volatile mean something different for function types,
6678 so the usual warnings are not appropriate. */
6679 else if ((TREE_CODE (ttr) != FUNCTION_TYPE && TREE_CODE (ttr) != METHOD_TYPE)
6680 || (TREE_CODE (ttl) != FUNCTION_TYPE && TREE_CODE (ttl) != METHOD_TYPE))
6681 {
6682 if (TREE_CODE (ttl) == OFFSET_TYPE
6683 && binfo_member (TYPE_OFFSET_BASETYPE (ttr),
6684 CLASSTYPE_VBASECLASSES (TYPE_OFFSET_BASETYPE (ttl))))
6685 {
6686 sorry ("%s between pointer to members converting across virtual baseclasses", errtype);
6687 return error_mark_node;
6688 }
6689 else if (! TYPE_READONLY (ttl) && TYPE_READONLY (ttr))
6690 {
6691 if (fndecl)
6692 cp_pedwarn ("passing `%T' as argument %P of `%D' discards const",
6693 rhstype, parmnum, fndecl);
6694 else
6695 cp_pedwarn ("%s to `%T' from `%T' discards const",
6696 errtype, type, rhstype);
6697 }
6698 else if (! TYPE_VOLATILE (ttl) && TYPE_VOLATILE (ttr))
6699 {
6700 if (fndecl)
6701 cp_pedwarn ("passing `%T' as argument %P of `%D' discards volatile",
6702 rhstype, parmnum, fndecl);
6703 else
6704 cp_pedwarn ("%s to `%T' from `%T' discards volatile",
6705 errtype, type, rhstype);
6706 }
6707 else if (TREE_CODE (ttl) == TREE_CODE (ttr)
6708 && ! comp_target_types (type, rhstype, 1))
6709 {
6710 if (fndecl)
6711 cp_pedwarn ("passing `%T' as argument %P of `%D' changes signedness",
6712 rhstype, parmnum, fndecl);
6713 else
6714 cp_pedwarn ("%s to `%T' from `%T' changes signedness",
6715 errtype, type, rhstype);
6716 }
6717 }
6718 }
6719 else
6720 {
6721 int add_quals = 0, const_parity = 0, volatile_parity = 0;
6722 int left_const = 1;
6723 int unsigned_parity;
6724 int nptrs = 0;
6725
6726 /* This code is basically a duplicate of comp_ptr_ttypes_real. */
6727 for (; ; ttl = TREE_TYPE (ttl), ttr = TREE_TYPE (ttr))
6728 {
6729 nptrs -= 1;
6730 const_parity |= (TYPE_READONLY (ttl) < TYPE_READONLY (ttr));
6731 volatile_parity |= (TYPE_VOLATILE (ttl) < TYPE_VOLATILE (ttr));
6732
6733 if (! left_const
6734 && (TYPE_READONLY (ttl) > TYPE_READONLY (ttr)
6735 || TYPE_VOLATILE (ttl) > TYPE_VOLATILE (ttr)))
6736 add_quals = 1;
6737 left_const &= TYPE_READONLY (ttl);
6738
6739 if (TREE_CODE (ttl) != POINTER_TYPE
6740 || TREE_CODE (ttr) != POINTER_TYPE)
6741 break;
6742 }
6743 unsigned_parity = TREE_UNSIGNED (ttl) - TREE_UNSIGNED (ttr);
6744 if (unsigned_parity)
6745 {
6746 if (TREE_UNSIGNED (ttl))
6747 ttr = unsigned_type (ttr);
6748 else
6749 ttl = unsigned_type (ttl);
6750 }
6751
6752 if (comp_target_types (ttl, ttr, nptrs) > 0)
6753 {
6754 if (add_quals)
6755 {
6756 if (fndecl)
6757 cp_pedwarn ("passing `%T' as argument %P of `%D' adds cv-quals without intervening `const'",
6758 rhstype, parmnum, fndecl);
6759 else
6760 cp_pedwarn ("%s to `%T' from `%T' adds cv-quals without intervening `const'",
6761 errtype, type, rhstype);
6762 }
6763 if (const_parity)
6764 {
6765 if (fndecl)
6766 cp_pedwarn ("passing `%T' as argument %P of `%D' discards const",
6767 rhstype, parmnum, fndecl);
6768 else
6769 cp_pedwarn ("%s to `%T' from `%T' discards const",
6770 errtype, type, rhstype);
6771 }
6772 if (volatile_parity)
6773 {
6774 if (fndecl)
6775 cp_pedwarn ("passing `%T' as argument %P of `%D' discards volatile",
6776 rhstype, parmnum, fndecl);
6777 else
6778 cp_pedwarn ("%s to `%T' from `%T' discards volatile",
6779 errtype, type, rhstype);
6780 }
6781 if (unsigned_parity > 0)
6782 {
6783 if (fndecl)
6784 cp_pedwarn ("passing `%T' as argument %P of `%D' changes signed to unsigned",
6785 rhstype, parmnum, fndecl);
6786 else
6787 cp_pedwarn ("%s to `%T' from `%T' changes signed to unsigned",
6788 errtype, type, rhstype);
6789 }
6790 else if (unsigned_parity < 0)
6791 {
6792 if (fndecl)
6793 cp_pedwarn ("passing `%T' as argument %P of `%D' changes unsigned to signed",
6794 rhstype, parmnum, fndecl);
6795 else
6796 cp_pedwarn ("%s to `%T' from `%T' changes unsigned to signed",
6797 errtype, type, rhstype);
6798 }
6799
6800 /* C++ is not so friendly about converting function and
6801 member function pointers as C. Emit warnings here. */
6802 if (TREE_CODE (ttl) == FUNCTION_TYPE
6803 || TREE_CODE (ttl) == METHOD_TYPE)
6804 if (! comptypes (ttl, ttr, 0))
6805 {
6806 warning ("conflicting function types in %s:", errtype);
6807 cp_warning ("\t`%T' != `%T'", type, rhstype);
6808 }
6809 }
6810 else
6811 {
6812 if (fndecl)
6813 cp_error ("passing `%T' as argument %P of `%D'",
6814 rhstype, parmnum, fndecl);
6815 else
6816 cp_error ("%s to `%T' from `%T'", errtype, type, rhstype);
6817 return error_mark_node;
6818 }
6819 }
6820 return cp_convert (type, rhs);
6821 }
6822 else if (codel == POINTER_TYPE && coder == INTEGER_TYPE)
6823 {
6824 /* An explicit constant 0 can convert to a pointer,
6825 but not a 0 that results from casting or folding. */
6826 if (! (TREE_CODE (rhs) == INTEGER_CST && integer_zerop (rhs)))
6827 {
6828 if (fndecl)
6829 cp_pedwarn ("passing `%T' to argument %P of `%D' lacks a cast",
6830 rhstype, parmnum, fndecl);
6831 else
6832 cp_pedwarn ("%s to `%T' from `%T' lacks a cast",
6833 errtype, type, rhstype);
6834 }
6835 return cp_convert (type, rhs);
6836 }
6837 else if (codel == INTEGER_TYPE
6838 && (coder == POINTER_TYPE
6839 || (coder == RECORD_TYPE
6840 && (IS_SIGNATURE_POINTER (rhstype)
6841 || TYPE_PTRMEMFUNC_FLAG (rhstype)
6842 || IS_SIGNATURE_REFERENCE (rhstype)))))
6843 {
6844 if (fndecl)
6845 cp_pedwarn ("passing `%T' to argument %P of `%D' lacks a cast",
6846 rhstype, parmnum, fndecl);
6847 else
6848 cp_pedwarn ("%s to `%T' from `%T' lacks a cast",
6849 errtype, type, rhstype);
6850 return cp_convert (type, rhs);
6851 }
6852 else if (codel == BOOLEAN_TYPE
6853 && (coder == POINTER_TYPE
6854 || (coder == RECORD_TYPE
6855 && (IS_SIGNATURE_POINTER (rhstype)
6856 || TYPE_PTRMEMFUNC_FLAG (rhstype)
6857 || IS_SIGNATURE_REFERENCE (rhstype)))))
6858 return cp_convert (type, rhs);
6859
6860 /* C++ */
6861 else if (((coder == POINTER_TYPE
6862 && TREE_CODE (TREE_TYPE (rhstype)) == METHOD_TYPE)
6863 || integer_zerop (rhs)
6864 || TYPE_PTRMEMFUNC_P (rhstype))
6865 && TYPE_PTRMEMFUNC_P (type))
6866 {
6867 tree ttl = TYPE_PTRMEMFUNC_FN_TYPE (type);
6868 tree ttr = (TREE_CODE (rhstype) == POINTER_TYPE ? rhstype
6869 : TYPE_PTRMEMFUNC_FN_TYPE (type));
6870 int ctt = comp_target_types (ttl, ttr, 1);
6871
6872 if (ctt < 0)
6873 cp_pedwarn ("converting `%T' to `%T' is a contravariance violation",
6874 ttr, ttl);
6875 else if (ctt == 0)
6876 cp_error ("%s to `%T' from `%T'", errtype, ttl, ttr);
6877
6878 /* compatible pointer to member functions. */
6879 return build_ptrmemfunc (ttl, rhs, 0);
6880 }
6881 else if (codel == ERROR_MARK || coder == ERROR_MARK)
6882 return error_mark_node;
6883
6884 /* This should no longer happen. References are initialized via
6885 `convert_for_initialization'. They should otherwise be
6886 bashed before coming here. */
6887 else if (codel == REFERENCE_TYPE)
6888 my_friendly_abort (317);
6889 else if (TYPE_MAIN_VARIANT (type) == TYPE_MAIN_VARIANT (TREE_TYPE (rhs)))
6890 {
6891 tree nrhs = build1 (NOP_EXPR, type, rhs);
6892 TREE_CONSTANT (nrhs) = TREE_CONSTANT (rhs);
6893 return nrhs;
6894 }
6895 else if (TYPE_HAS_CONSTRUCTOR (type) || IS_AGGR_TYPE (TREE_TYPE (rhs)))
6896 return cp_convert (type, rhs);
6897 /* Handle anachronistic conversions from (::*)() to cv void* or (*)(). */
6898 else if (TREE_CODE (type) == POINTER_TYPE
6899 && (TREE_CODE (TREE_TYPE (type)) == FUNCTION_TYPE
6900 || TYPE_MAIN_VARIANT (TREE_TYPE (type)) == void_type_node)
6901 && TREE_TYPE (rhs)
6902 && TYPE_PTRMEMFUNC_P (TREE_TYPE (rhs)))
6903 return cp_convert (type, rhs);
6904
6905 cp_error ("%s to `%T' from `%T'", errtype, type, rhstype);
6906 return error_mark_node;
6907 }
6908
6909 /* Convert RHS to be of type TYPE. If EXP is non-zero,
6910 it is the target of the initialization.
6911 ERRTYPE is a string to use in error messages.
6912
6913 Two major differences between the behavior of
6914 `convert_for_assignment' and `convert_for_initialization'
6915 are that references are bashed in the former, while
6916 copied in the latter, and aggregates are assigned in
6917 the former (operator=) while initialized in the
6918 latter (X(X&)).
6919
6920 If using constructor make sure no conversion operator exists, if one does
6921 exist, an ambiguity exists.
6922
6923 If flags doesn't include LOOKUP_COMPLAIN, don't complain about anything. */
6924
6925 tree
6926 convert_for_initialization (exp, type, rhs, flags, errtype, fndecl, parmnum)
6927 tree exp, type, rhs;
6928 int flags;
6929 char *errtype;
6930 tree fndecl;
6931 int parmnum;
6932 {
6933 register enum tree_code codel = TREE_CODE (type);
6934 register tree rhstype;
6935 register enum tree_code coder;
6936
6937 /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
6938 Strip such NOP_EXPRs, since RHS is used in non-lvalue context. */
6939 if (TREE_CODE (rhs) == NOP_EXPR
6940 && TREE_TYPE (rhs) == TREE_TYPE (TREE_OPERAND (rhs, 0))
6941 && codel != REFERENCE_TYPE)
6942 rhs = TREE_OPERAND (rhs, 0);
6943
6944 if (rhs == error_mark_node
6945 || (TREE_CODE (rhs) == TREE_LIST && TREE_VALUE (rhs) == error_mark_node))
6946 return error_mark_node;
6947
6948 if (TREE_CODE (TREE_TYPE (rhs)) == OFFSET_TYPE)
6949 {
6950 rhs = resolve_offset_ref (rhs);
6951 if (rhs == error_mark_node)
6952 return error_mark_node;
6953 }
6954
6955 if (TREE_CODE (TREE_TYPE (rhs)) == REFERENCE_TYPE)
6956 rhs = convert_from_reference (rhs);
6957
6958 if ((TREE_CODE (TREE_TYPE (rhs)) == ARRAY_TYPE
6959 && TREE_CODE (type) != ARRAY_TYPE
6960 && (TREE_CODE (type) != REFERENCE_TYPE
6961 || TREE_CODE (TREE_TYPE (type)) != ARRAY_TYPE))
6962 || TREE_CODE (TREE_TYPE (rhs)) == FUNCTION_TYPE
6963 || TREE_CODE (TREE_TYPE (rhs)) == METHOD_TYPE)
6964 rhs = default_conversion (rhs);
6965
6966 rhstype = TREE_TYPE (rhs);
6967 coder = TREE_CODE (rhstype);
6968
6969 if (coder == UNKNOWN_TYPE)
6970 {
6971 rhs = instantiate_type (type, rhs, 1);
6972 rhstype = TREE_TYPE (rhs);
6973 coder = TREE_CODE (rhstype);
6974 }
6975
6976 if (coder == ERROR_MARK)
6977 return error_mark_node;
6978
6979 /* We accept references to incomplete types, so we can
6980 return here before checking if RHS is of complete type. */
6981
6982 if (codel == REFERENCE_TYPE)
6983 {
6984 /* This should eventually happen in convert_arguments. */
6985 extern int warningcount, errorcount;
6986 int savew, savee;
6987
6988 if (fndecl)
6989 savew = warningcount, savee = errorcount;
6990 rhs = convert_to_reference (type, rhs, CONV_IMPLICIT, flags,
6991 exp ? exp : error_mark_node);
6992 if (fndecl)
6993 {
6994 if (warningcount > savew)
6995 cp_warning_at ("in passing argument %P of `%+D'", parmnum, fndecl);
6996 else if (errorcount > savee)
6997 cp_error_at ("in passing argument %P of `%+D'", parmnum, fndecl);
6998 }
6999 return rhs;
7000 }
7001
7002 rhs = require_complete_type (rhs);
7003 if (rhs == error_mark_node)
7004 return error_mark_node;
7005
7006 if (exp != 0) exp = require_complete_type (exp);
7007 if (exp == error_mark_node)
7008 return error_mark_node;
7009
7010 if (TREE_CODE (rhstype) == REFERENCE_TYPE)
7011 rhstype = TREE_TYPE (rhstype);
7012
7013 type = complete_type (type);
7014
7015 if (TYPE_LANG_SPECIFIC (type)
7016 && (IS_SIGNATURE_POINTER (type) || IS_SIGNATURE_REFERENCE (type)))
7017 return build_signature_pointer_constructor (type, rhs);
7018
7019 if (IS_AGGR_TYPE (type)
7020 && (TYPE_NEEDS_CONSTRUCTING (type) || TREE_HAS_CONSTRUCTOR (rhs)))
7021 {
7022 if (flag_ansi_overloading)
7023 return ocp_convert (type, rhs, CONV_IMPLICIT|CONV_FORCE_TEMP, flags);
7024
7025 if (TYPE_MAIN_VARIANT (type) == TYPE_MAIN_VARIANT (rhstype))
7026 {
7027 /* This is sufficient to perform initialization. No need,
7028 apparently, to go through X(X&) to do first-cut
7029 initialization. Return through a TARGET_EXPR so that we get
7030 cleanups if it is used. */
7031 if (TREE_CODE (rhs) == CALL_EXPR)
7032 {
7033 rhs = build_cplus_new (type, rhs);
7034 return rhs;
7035 }
7036 /* Handle the case of default parameter initialization and
7037 initialization of static variables. */
7038 else if (TREE_CODE (rhs) == TARGET_EXPR)
7039 return rhs;
7040 else if (TREE_CODE (rhs) == INDIRECT_REF && TREE_HAS_CONSTRUCTOR (rhs))
7041 {
7042 my_friendly_assert (TREE_CODE (TREE_OPERAND (rhs, 0)) == CALL_EXPR, 318);
7043 if (exp)
7044 {
7045 my_friendly_assert (TREE_VALUE (TREE_OPERAND (TREE_OPERAND (rhs, 0), 1)) == NULL_TREE, 316);
7046 TREE_VALUE (TREE_OPERAND (TREE_OPERAND (rhs, 0), 1))
7047 = build_unary_op (ADDR_EXPR, exp, 0);
7048 }
7049 else
7050 rhs = build_cplus_new (type, TREE_OPERAND (rhs, 0));
7051 return rhs;
7052 }
7053 else if (TYPE_HAS_TRIVIAL_INIT_REF (type))
7054 return rhs;
7055 }
7056 if (TYPE_MAIN_VARIANT (type) == TYPE_MAIN_VARIANT (rhstype)
7057 || (IS_AGGR_TYPE (rhstype) && UNIQUELY_DERIVED_FROM_P (type, rhstype)))
7058 {
7059 if (TYPE_HAS_INIT_REF (type))
7060 {
7061 tree init = build_method_call (exp, ctor_identifier,
7062 build_expr_list (NULL_TREE, rhs),
7063 TYPE_BINFO (type), LOOKUP_NORMAL);
7064
7065 if (init == error_mark_node)
7066 return error_mark_node;
7067
7068 if (exp == 0)
7069 {
7070 exp = build_cplus_new (type, init);
7071 return exp;
7072 }
7073
7074 return build (COMPOUND_EXPR, type, init, exp);
7075 }
7076
7077 /* ??? The following warnings are turned off because
7078 this is another place where the default X(X&) constructor
7079 is implemented. */
7080 if (TYPE_HAS_ASSIGNMENT (type))
7081 cp_warning ("bitwise copy: `%T' defines operator=", type);
7082
7083 if (TREE_CODE (TREE_TYPE (rhs)) == REFERENCE_TYPE)
7084 rhs = convert_from_reference (rhs);
7085 if (type != rhstype)
7086 {
7087 tree nrhs = build1 (NOP_EXPR, type, rhs);
7088 TREE_CONSTANT (nrhs) = TREE_CONSTANT (rhs);
7089 rhs = nrhs;
7090 }
7091 return rhs;
7092 }
7093
7094 return ocp_convert (type, rhs, CONV_OLD_CONVERT,
7095 flags | LOOKUP_NO_CONVERSION);
7096 }
7097
7098 if (type == TREE_TYPE (rhs))
7099 {
7100 if (TREE_READONLY_DECL_P (rhs))
7101 rhs = decl_constant_value (rhs);
7102 return rhs;
7103 }
7104
7105 return convert_for_assignment (type, rhs, errtype, fndecl, parmnum);
7106 }
7107 \f
7108 /* Expand an ASM statement with operands, handling output operands
7109 that are not variables or INDIRECT_REFS by transforming such
7110 cases into cases that expand_asm_operands can handle.
7111
7112 Arguments are same as for expand_asm_operands.
7113
7114 We don't do default conversions on all inputs, because it can screw
7115 up operands that are expected to be in memory. */
7116
7117 void
7118 c_expand_asm_operands (string, outputs, inputs, clobbers, vol, filename, line)
7119 tree string, outputs, inputs, clobbers;
7120 int vol;
7121 char *filename;
7122 int line;
7123 {
7124 int noutputs = list_length (outputs);
7125 register int i;
7126 /* o[I] is the place that output number I should be written. */
7127 register tree *o = (tree *) alloca (noutputs * sizeof (tree));
7128 register tree tail;
7129
7130 /* Record the contents of OUTPUTS before it is modified. */
7131 for (i = 0, tail = outputs; tail; tail = TREE_CHAIN (tail), i++)
7132 o[i] = TREE_VALUE (tail);
7133
7134 /* Generate the ASM_OPERANDS insn;
7135 store into the TREE_VALUEs of OUTPUTS some trees for
7136 where the values were actually stored. */
7137 expand_asm_operands (string, outputs, inputs, clobbers, vol, filename, line);
7138
7139 /* Copy all the intermediate outputs into the specified outputs. */
7140 for (i = 0, tail = outputs; tail; tail = TREE_CHAIN (tail), i++)
7141 {
7142 if (o[i] != TREE_VALUE (tail))
7143 {
7144 expand_expr (build_modify_expr (o[i], NOP_EXPR, TREE_VALUE (tail)),
7145 const0_rtx, VOIDmode, EXPAND_NORMAL);
7146 free_temp_slots ();
7147 }
7148 /* Detect modification of read-only values.
7149 (Otherwise done by build_modify_expr.) */
7150 else
7151 {
7152 tree type = TREE_TYPE (o[i]);
7153 if (TYPE_READONLY (type)
7154 || ((TREE_CODE (type) == RECORD_TYPE
7155 || TREE_CODE (type) == UNION_TYPE)
7156 && C_TYPE_FIELDS_READONLY (type)))
7157 readonly_error (o[i], "modification by `asm'", 1);
7158 }
7159 }
7160
7161 /* Those MODIFY_EXPRs could do autoincrements. */
7162 emit_queue ();
7163 }
7164 \f
7165 /* Expand a C `return' statement.
7166 RETVAL is the expression for what to return,
7167 or a null pointer for `return;' with no value.
7168
7169 C++: upon seeing a `return', we must call destructors on all
7170 variables in scope which had constructors called on them.
7171 This means that if in a destructor, the base class destructors
7172 must be called before returning.
7173
7174 The RETURN statement in C++ has initialization semantics. */
7175
7176 void
7177 c_expand_return (retval)
7178 tree retval;
7179 {
7180 extern struct nesting *cond_stack, *loop_stack, *case_stack;
7181 extern tree dtor_label, ctor_label;
7182 tree result = DECL_RESULT (current_function_decl);
7183 tree valtype = TREE_TYPE (result);
7184
7185 if (TREE_THIS_VOLATILE (current_function_decl))
7186 warning ("function declared `noreturn' has a `return' statement");
7187
7188 if (retval == error_mark_node)
7189 {
7190 current_function_returns_null = 1;
7191 return;
7192 }
7193
7194 if (processing_template_decl)
7195 {
7196 add_tree (build_min_nt (RETURN_STMT, retval));
7197 return;
7198 }
7199
7200 if (retval == NULL_TREE)
7201 {
7202 /* A non-named return value does not count. */
7203
7204 /* Can't just return from a destructor. */
7205 if (dtor_label)
7206 {
7207 expand_goto (dtor_label);
7208 return;
7209 }
7210
7211 if (DECL_CONSTRUCTOR_P (current_function_decl))
7212 retval = current_class_ptr;
7213 else if (DECL_NAME (result) != NULL_TREE
7214 && TREE_CODE (valtype) != VOID_TYPE)
7215 retval = result;
7216 else
7217 {
7218 current_function_returns_null = 1;
7219
7220 if (valtype != NULL_TREE && TREE_CODE (valtype) != VOID_TYPE)
7221 {
7222 if (DECL_NAME (DECL_RESULT (current_function_decl)) == NULL_TREE)
7223 {
7224 pedwarn ("`return' with no value, in function returning non-void");
7225 /* Clear this, so finish_function won't say that we
7226 reach the end of a non-void function (which we don't,
7227 we gave a return!). */
7228 current_function_returns_null = 0;
7229 }
7230 }
7231
7232 expand_null_return ();
7233 return;
7234 }
7235 }
7236 else if (DECL_CONSTRUCTOR_P (current_function_decl)
7237 && retval != current_class_ptr)
7238 {
7239 if (flag_this_is_variable)
7240 error ("return from a constructor: use `this = ...' instead");
7241 else
7242 error ("return from a constructor");
7243 retval = current_class_ptr;
7244 }
7245
7246 /* Effective C++ rule 15. See also start_function. */
7247 if (warn_ecpp
7248 && DECL_NAME (current_function_decl) == ansi_opname[(int) MODIFY_EXPR]
7249 && retval != current_class_ref)
7250 cp_warning ("`operator=' should return a reference to `*this'");
7251
7252 if (valtype == NULL_TREE || TREE_CODE (valtype) == VOID_TYPE)
7253 {
7254 current_function_returns_null = 1;
7255 if ((pedantic && ! DECL_ARTIFICIAL (current_function_decl))
7256 || TREE_CODE (TREE_TYPE (retval)) != VOID_TYPE)
7257 pedwarn ("`return' with a value, in function returning void");
7258 expand_return (retval);
7259 return;
7260 }
7261
7262 /* Now deal with possible C++ hair:
7263 (1) Compute the return value.
7264 (2) If there are aggregate values with destructors which
7265 must be cleaned up, clean them (taking care
7266 not to clobber the return value).
7267 (3) If an X(X&) constructor is defined, the return
7268 value must be returned via that. */
7269
7270 if (retval == result
7271 || DECL_CONSTRUCTOR_P (current_function_decl))
7272 /* It's already done for us. */;
7273 else if (TREE_TYPE (retval) == void_type_node)
7274 {
7275 pedwarn ("return of void value in function returning non-void");
7276 expand_expr_stmt (retval);
7277 retval = 0;
7278 }
7279 else
7280 {
7281 retval = convert_for_initialization
7282 (NULL_TREE, valtype, retval, LOOKUP_NORMAL|LOOKUP_ONLYCONVERTING,
7283 "return", NULL_TREE, 0);
7284
7285 if (retval == error_mark_node)
7286 {
7287 /* Avoid warning about control reaching end of function. */
7288 expand_null_return ();
7289 return;
7290 }
7291
7292 /* We can't initialize a register from a NEW_EXPR. */
7293 else if (! current_function_returns_struct
7294 && TREE_CODE (retval) == TARGET_EXPR
7295 && TREE_CODE (TREE_OPERAND (retval, 1)) == NEW_EXPR)
7296 retval = build (COMPOUND_EXPR, TREE_TYPE (retval), retval,
7297 TREE_OPERAND (retval, 0));
7298
7299 /* Add some useful error checking for C++. */
7300 else if (TREE_CODE (valtype) == REFERENCE_TYPE)
7301 {
7302 tree whats_returned;
7303
7304 /* Sort through common things to see what it is
7305 we are returning. */
7306 whats_returned = retval;
7307 if (TREE_CODE (whats_returned) == COMPOUND_EXPR)
7308 {
7309 whats_returned = TREE_OPERAND (whats_returned, 1);
7310 if (TREE_CODE (whats_returned) == ADDR_EXPR)
7311 whats_returned = TREE_OPERAND (whats_returned, 0);
7312 }
7313 while (TREE_CODE (whats_returned) == CONVERT_EXPR
7314 || TREE_CODE (whats_returned) == NOP_EXPR)
7315 whats_returned = TREE_OPERAND (whats_returned, 0);
7316 if (TREE_CODE (whats_returned) == ADDR_EXPR)
7317 {
7318 whats_returned = TREE_OPERAND (whats_returned, 0);
7319 while (TREE_CODE (whats_returned) == NEW_EXPR
7320 || TREE_CODE (whats_returned) == TARGET_EXPR)
7321 {
7322 /* Get the target. */
7323 whats_returned = TREE_OPERAND (whats_returned, 0);
7324 warning ("returning reference to temporary");
7325 }
7326 }
7327
7328 if (TREE_CODE (whats_returned) == VAR_DECL && DECL_NAME (whats_returned))
7329 {
7330 if (TEMP_NAME_P (DECL_NAME (whats_returned)))
7331 warning ("reference to non-lvalue returned");
7332 else if (TREE_CODE (TREE_TYPE (whats_returned)) != REFERENCE_TYPE
7333 && ! TREE_STATIC (whats_returned)
7334 && IDENTIFIER_LOCAL_VALUE (DECL_NAME (whats_returned))
7335 && !TREE_PUBLIC (whats_returned))
7336 cp_warning_at ("reference to local variable `%D' returned", whats_returned);
7337 }
7338 }
7339 else if (TREE_CODE (retval) == ADDR_EXPR)
7340 {
7341 tree whats_returned = TREE_OPERAND (retval, 0);
7342
7343 if (TREE_CODE (whats_returned) == VAR_DECL
7344 && DECL_NAME (whats_returned)
7345 && IDENTIFIER_LOCAL_VALUE (DECL_NAME (whats_returned))
7346 && !TREE_STATIC (whats_returned)
7347 && !TREE_PUBLIC (whats_returned))
7348 cp_warning_at ("address of local variable `%D' returned", whats_returned);
7349 }
7350 }
7351
7352 if (retval != NULL_TREE
7353 && TREE_CODE_CLASS (TREE_CODE (retval)) == 'd'
7354 && cond_stack == 0 && loop_stack == 0 && case_stack == 0)
7355 current_function_return_value = retval;
7356
7357 if (ctor_label && TREE_CODE (ctor_label) != ERROR_MARK)
7358 {
7359 /* Here RETVAL is CURRENT_CLASS_PTR, so there's nothing to do. */
7360 expand_goto (ctor_label);
7361 }
7362
7363 if (retval && retval != result)
7364 {
7365 result = build (INIT_EXPR, TREE_TYPE (result), result, retval);
7366 TREE_SIDE_EFFECTS (result) = 1;
7367 }
7368
7369 expand_start_target_temps ();
7370
7371 expand_return (result);
7372
7373 expand_end_target_temps ();
7374
7375 current_function_returns_value = 1;
7376 }
7377 \f
7378 /* Start a C switch statement, testing expression EXP.
7379 Return EXP if it is valid, an error node otherwise. */
7380
7381 tree
7382 c_expand_start_case (exp)
7383 tree exp;
7384 {
7385 tree type;
7386 register enum tree_code code;
7387
7388 /* Convert from references, etc. */
7389 exp = default_conversion (exp);
7390 type = TREE_TYPE (exp);
7391 code = TREE_CODE (type);
7392
7393 if (IS_AGGR_TYPE_CODE (code))
7394 exp = build_type_conversion (CONVERT_EXPR, integer_type_node, exp, 1);
7395
7396 if (exp == NULL_TREE)
7397 {
7398 error ("switch quantity not an integer");
7399 exp = error_mark_node;
7400 }
7401 type = TREE_TYPE (exp);
7402 code = TREE_CODE (type);
7403
7404 if (code != INTEGER_TYPE && code != ENUMERAL_TYPE && code != ERROR_MARK)
7405 {
7406 error ("switch quantity not an integer");
7407 exp = error_mark_node;
7408 }
7409 else
7410 {
7411 tree idx;
7412
7413 exp = default_conversion (exp);
7414 type = TREE_TYPE (exp);
7415 idx = get_unwidened (exp, 0);
7416 /* We can't strip a conversion from a signed type to an unsigned,
7417 because if we did, int_fits_type_p would do the wrong thing
7418 when checking case values for being in range,
7419 and it's too hard to do the right thing. */
7420 if (TREE_UNSIGNED (TREE_TYPE (exp))
7421 == TREE_UNSIGNED (TREE_TYPE (idx)))
7422 exp = idx;
7423 }
7424
7425 expand_start_case
7426 (1, fold (build1 (CLEANUP_POINT_EXPR, TREE_TYPE (exp), exp)),
7427 type, "switch statement");
7428
7429 return exp;
7430 }
7431
7432 /* CONSTP remembers whether or not all the intervening pointers in the `to'
7433 type have been const. */
7434
7435 static int
7436 comp_ptr_ttypes_real (to, from, constp)
7437 tree to, from;
7438 int constp;
7439 {
7440 for (; ; to = TREE_TYPE (to), from = TREE_TYPE (from))
7441 {
7442 if (TREE_CODE (to) != TREE_CODE (from))
7443 return 0;
7444
7445 if (TREE_CODE (from) == OFFSET_TYPE
7446 && comptypes (TYPE_OFFSET_BASETYPE (from),
7447 TYPE_OFFSET_BASETYPE (to), 1))
7448 continue;
7449
7450 /* Const and volatile mean something different for function types,
7451 so the usual checks are not appropriate. */
7452 if (TREE_CODE (to) != FUNCTION_TYPE && TREE_CODE (to) != METHOD_TYPE)
7453 {
7454 if (TYPE_READONLY (from) > TYPE_READONLY (to)
7455 || TYPE_VOLATILE (from) > TYPE_VOLATILE (to))
7456 return 0;
7457
7458 if (! constp
7459 && (TYPE_READONLY (to) > TYPE_READONLY (from)
7460 || TYPE_VOLATILE (to) > TYPE_READONLY (from)))
7461 return 0;
7462 constp &= TYPE_READONLY (to);
7463 }
7464
7465 if (TREE_CODE (to) != POINTER_TYPE)
7466 return comptypes (TYPE_MAIN_VARIANT (to), TYPE_MAIN_VARIANT (from), 1);
7467 }
7468 }
7469
7470 /* When comparing, say, char ** to char const **, this function takes the
7471 'char *' and 'char const *'. Do not pass non-pointer types to this
7472 function. */
7473
7474 int
7475 comp_ptr_ttypes (to, from)
7476 tree to, from;
7477 {
7478 return comp_ptr_ttypes_real (to, from, 1);
7479 }
7480
7481 /* Returns 1 if to and from are (possibly multi-level) pointers to the same
7482 type or inheritance-related types, regardless of cv-quals. */
7483
7484 int
7485 ptr_reasonably_similar (to, from)
7486 tree to, from;
7487 {
7488 for (; ; to = TREE_TYPE (to), from = TREE_TYPE (from))
7489 {
7490 if (TREE_CODE (to) != TREE_CODE (from))
7491 return 0;
7492
7493 if (TREE_CODE (from) == OFFSET_TYPE
7494 && comptypes (TYPE_OFFSET_BASETYPE (to),
7495 TYPE_OFFSET_BASETYPE (from), -1))
7496 continue;
7497
7498 if (TREE_CODE (to) != POINTER_TYPE)
7499 return comptypes
7500 (TYPE_MAIN_VARIANT (to), TYPE_MAIN_VARIANT (from), -1);
7501 }
7502 }
7503
7504 /* Like comp_ptr_ttypes, for const_cast. */
7505
7506 static int
7507 comp_ptr_ttypes_const (to, from)
7508 tree to, from;
7509 {
7510 for (; ; to = TREE_TYPE (to), from = TREE_TYPE (from))
7511 {
7512 if (TREE_CODE (to) != TREE_CODE (from))
7513 return 0;
7514
7515 if (TREE_CODE (from) == OFFSET_TYPE
7516 && comptypes (TYPE_OFFSET_BASETYPE (from),
7517 TYPE_OFFSET_BASETYPE (to), 1))
7518 continue;
7519
7520 if (TREE_CODE (to) != POINTER_TYPE)
7521 return comptypes (TYPE_MAIN_VARIANT (to), TYPE_MAIN_VARIANT (from), 1);
7522 }
7523 }
7524
7525 /* Like comp_ptr_ttypes, for reinterpret_cast. */
7526
7527 static int
7528 comp_ptr_ttypes_reinterpret (to, from)
7529 tree to, from;
7530 {
7531 int constp = 1;
7532
7533 for (; ; to = TREE_TYPE (to), from = TREE_TYPE (from))
7534 {
7535 if (TREE_CODE (from) == OFFSET_TYPE)
7536 from = TREE_TYPE (from);
7537 if (TREE_CODE (to) == OFFSET_TYPE)
7538 to = TREE_TYPE (to);
7539
7540 if (TREE_CODE (to) != TREE_CODE (from))
7541 return 1;
7542
7543 /* Const and volatile mean something different for function types,
7544 so the usual checks are not appropriate. */
7545 if (TREE_CODE (to) != FUNCTION_TYPE && TREE_CODE (to) != METHOD_TYPE)
7546 {
7547 if (TYPE_READONLY (from) > TYPE_READONLY (to)
7548 || TYPE_VOLATILE (from) > TYPE_VOLATILE (to))
7549 return 0;
7550
7551 if (! constp
7552 && (TYPE_READONLY (to) > TYPE_READONLY (from)
7553 || TYPE_VOLATILE (to) > TYPE_READONLY (from)))
7554 return 0;
7555 constp &= TYPE_READONLY (to);
7556 }
7557
7558 if (TREE_CODE (to) != POINTER_TYPE)
7559 return 1;
7560 }
7561 }