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