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