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