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