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