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