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