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