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