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