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