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