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