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