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