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