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