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