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