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