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