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