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