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