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