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