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