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