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