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