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