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