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