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