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