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