typeck.c (mark_addressable): Don't assume a FUNCTION_DECL has DECL_LANG_SPECIFIC.
[gcc.git] / gcc / cp / typeck.c
1 /* Build expressions with type checking for C++ compiler.
2 Copyright (C) 1987, 88, 89, 92, 93, 94, 95, 1996 Free Software Foundation, Inc.
3 Hacked by Michael Tiemann (tiemann@cygnus.com)
4
5 This file is part of GNU CC.
6
7 GNU CC is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2, or (at your option)
10 any later version.
11
12 GNU CC is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with GNU CC; see the file COPYING. If not, write to
19 the Free Software Foundation, 59 Temple Place - Suite 330,
20 Boston, MA 02111-1307, USA. */
21
22
23 /* This file is part of the C++ front end.
24 It contains routines to build C++ expressions given their operands,
25 including computing the types of the result, C and C++ specific error
26 checks, and some optimization.
27
28 There are also routines to build RETURN_STMT nodes and CASE_STMT nodes,
29 and to process initializations in declarations (since they work
30 like a strange sort of assignment). */
31
32 #include "config.h"
33 #include <stdio.h>
34 #include "tree.h"
35 #include "rtl.h"
36 #include "cp-tree.h"
37 #include "flags.h"
38 #include "output.h"
39 #include "expr.h"
40
41 #ifdef HAVE_STRING_H
42 #include <string.h>
43 #endif
44
45 extern void compiler_error ();
46
47 static tree convert_for_assignment PROTO((tree, tree, char*, tree,
48 int));
49 static tree pointer_int_sum PROTO((enum tree_code, tree, tree));
50 static tree rationalize_conditional_expr PROTO((enum tree_code, tree));
51 static int comp_target_parms PROTO((tree, tree, int));
52 static int comp_ptr_ttypes_real PROTO((tree, tree, int));
53 static int comp_ptr_ttypes_const PROTO((tree, tree));
54 static int comp_ptr_ttypes_reinterpret PROTO((tree, tree));
55 static int comp_array_types PROTO((int (*) (tree, tree, int), tree,
56 tree, int));
57 static tree build_ptrmemfunc1 PROTO((tree, tree, tree, tree, tree));
58 static tree common_base_type PROTO((tree, tree));
59 static tree convert_sequence PROTO((tree, tree));
60 static tree lookup_anon_field PROTO((tree, tree));
61 static tree pointer_diff PROTO((tree, tree));
62 static tree qualify_type PROTO((tree, tree));
63 static tree expand_target_expr PROTO((tree));
64 static tree get_delta_difference PROTO((tree, tree, int));
65
66 /* Return the target type of TYPE, which meas return T for:
67 T*, T&, T[], T (...), and otherwise, just T. */
68
69 tree
70 target_type (type)
71 tree type;
72 {
73 if (TREE_CODE (type) == REFERENCE_TYPE)
74 type = TREE_TYPE (type);
75 while (TREE_CODE (type) == POINTER_TYPE
76 || TREE_CODE (type) == ARRAY_TYPE
77 || TREE_CODE (type) == FUNCTION_TYPE
78 || TREE_CODE (type) == METHOD_TYPE
79 || TREE_CODE (type) == OFFSET_TYPE)
80 type = TREE_TYPE (type);
81 return type;
82 }
83
84 /* Do `exp = require_complete_type (exp);' to make sure exp
85 does not have an incomplete type. (That includes void types.) */
86
87 tree
88 require_complete_type (value)
89 tree value;
90 {
91 tree type;
92
93 if (processing_template_decl)
94 return value;
95
96 type = TREE_TYPE (value);
97
98 /* First, detect a valid value with a complete type. */
99 if (TYPE_SIZE (type) != 0
100 && type != void_type_node
101 && ! (TYPE_LANG_SPECIFIC (type)
102 && (IS_SIGNATURE_POINTER (type) || IS_SIGNATURE_REFERENCE (type))
103 && TYPE_SIZE (SIGNATURE_TYPE (type)) == 0))
104 return value;
105
106 /* If we see X::Y, we build an OFFSET_TYPE which has
107 not been laid out. Try to avoid an error by interpreting
108 it as this->X::Y, if reasonable. */
109 if (TREE_CODE (value) == OFFSET_REF
110 && current_class_ref != 0
111 && TREE_OPERAND (value, 0) == current_class_ref)
112 {
113 tree base, member = TREE_OPERAND (value, 1);
114 tree basetype = TYPE_OFFSET_BASETYPE (type);
115 my_friendly_assert (TREE_CODE (member) == FIELD_DECL, 305);
116 base = convert_pointer_to (basetype, current_class_ptr);
117 value = build (COMPONENT_REF, TREE_TYPE (member),
118 build_indirect_ref (base, NULL_PTR), member);
119 return require_complete_type (value);
120 }
121
122 if (IS_AGGR_TYPE (type) && CLASSTYPE_TEMPLATE_INSTANTIATION (type))
123 {
124 instantiate_class_template (TYPE_MAIN_VARIANT (type));
125 if (TYPE_SIZE (type) != 0)
126 return value;
127 }
128
129 incomplete_type_error (value, type);
130 return error_mark_node;
131 }
132
133 tree
134 complete_type (type)
135 tree type;
136 {
137 if (type == error_mark_node || TYPE_SIZE (type) != NULL_TREE)
138 ;
139 else if (TREE_CODE (type) == ARRAY_TYPE && TYPE_DOMAIN (type))
140 {
141 tree t = complete_type (TREE_TYPE (type));
142 if (TYPE_SIZE (t) != NULL_TREE && ! processing_template_decl)
143 layout_type (type);
144 TYPE_NEEDS_CONSTRUCTING (type)
145 = TYPE_NEEDS_CONSTRUCTING (TYPE_MAIN_VARIANT (t));
146 TYPE_NEEDS_DESTRUCTOR (type)
147 = TYPE_NEEDS_DESTRUCTOR (TYPE_MAIN_VARIANT (t));
148 }
149 else if (IS_AGGR_TYPE (type) && CLASSTYPE_TEMPLATE_INSTANTIATION (type))
150 instantiate_class_template (TYPE_MAIN_VARIANT (type));
151
152 return type;
153 }
154
155 /* Return truthvalue of whether type of EXP is instantiated. */
156
157 int
158 type_unknown_p (exp)
159 tree exp;
160 {
161 return (TREE_CODE (exp) == TREE_LIST
162 || TREE_TYPE (exp) == unknown_type_node
163 || (TREE_CODE (TREE_TYPE (exp)) == OFFSET_TYPE
164 && TREE_TYPE (TREE_TYPE (exp)) == unknown_type_node));
165 }
166
167 /* Return truthvalue of whether T is function (or pfn) type. */
168
169 int
170 fntype_p (t)
171 tree t;
172 {
173 return (TREE_CODE (t) == FUNCTION_TYPE || TREE_CODE (t) == METHOD_TYPE
174 || (TREE_CODE (t) == POINTER_TYPE
175 && (TREE_CODE (TREE_TYPE (t)) == FUNCTION_TYPE
176 || TREE_CODE (TREE_TYPE (t)) == METHOD_TYPE)));
177 }
178
179 /* Do `exp = require_instantiated_type (type, exp);' to make sure EXP
180 does not have an uninstantiated type.
181 TYPE is type to instantiate with, if uninstantiated. */
182
183 tree
184 require_instantiated_type (type, exp, errval)
185 tree type, exp, errval;
186 {
187 if (TREE_TYPE (exp) == NULL_TREE)
188 {
189 error ("argument list may not have an initializer list");
190 return errval;
191 }
192
193 if (TREE_TYPE (exp) == unknown_type_node
194 || (TREE_CODE (TREE_TYPE (exp)) == OFFSET_TYPE
195 && TREE_TYPE (TREE_TYPE (exp)) == unknown_type_node))
196 {
197 exp = instantiate_type (type, exp, 1);
198 if (TREE_TYPE (exp) == error_mark_node)
199 return errval;
200 }
201 return exp;
202 }
203
204 /* Return a variant of TYPE which has all the type qualifiers of LIKE
205 as well as those of TYPE. */
206
207 static tree
208 qualify_type (type, like)
209 tree type, like;
210 {
211 int constflag = TYPE_READONLY (type) || TYPE_READONLY (like);
212 int volflag = TYPE_VOLATILE (type) || TYPE_VOLATILE (like);
213 /* @@ Must do member pointers here. */
214 return cp_build_type_variant (type, constflag, volflag);
215 }
216 \f
217 /* Return the common type of two parameter lists.
218 We assume that comptypes has already been done and returned 1;
219 if that isn't so, this may crash.
220
221 As an optimization, free the space we allocate if the parameter
222 lists are already common. */
223
224 tree
225 commonparms (p1, p2)
226 tree p1, p2;
227 {
228 tree oldargs = p1, newargs, n;
229 int i, len;
230 int any_change = 0;
231 char *first_obj = (char *) oballoc (0);
232
233 len = list_length (p1);
234 newargs = tree_last (p1);
235
236 if (newargs == void_list_node)
237 i = 1;
238 else
239 {
240 i = 0;
241 newargs = 0;
242 }
243
244 for (; i < len; i++)
245 newargs = tree_cons (NULL_TREE, NULL_TREE, newargs);
246
247 n = newargs;
248
249 for (i = 0; p1;
250 p1 = TREE_CHAIN (p1), p2 = TREE_CHAIN (p2), n = TREE_CHAIN (n), i++)
251 {
252 if (TREE_PURPOSE (p1) && !TREE_PURPOSE (p2))
253 {
254 TREE_PURPOSE (n) = TREE_PURPOSE (p1);
255 any_change = 1;
256 }
257 else if (! TREE_PURPOSE (p1))
258 {
259 if (TREE_PURPOSE (p2))
260 {
261 TREE_PURPOSE (n) = TREE_PURPOSE (p2);
262 any_change = 1;
263 }
264 }
265 else
266 {
267 if (1 != simple_cst_equal (TREE_PURPOSE (p1), TREE_PURPOSE (p2)))
268 any_change = 1;
269 TREE_PURPOSE (n) = TREE_PURPOSE (p2);
270 }
271 if (TREE_VALUE (p1) != TREE_VALUE (p2))
272 {
273 any_change = 1;
274 TREE_VALUE (n) = common_type (TREE_VALUE (p1), TREE_VALUE (p2));
275 }
276 else
277 TREE_VALUE (n) = TREE_VALUE (p1);
278 }
279 if (! any_change)
280 {
281 obfree (first_obj);
282 return oldargs;
283 }
284
285 return newargs;
286 }
287
288 /* Return the common type of two types.
289 We assume that comptypes has already been done and returned 1;
290 if that isn't so, this may crash.
291
292 This is the type for the result of most arithmetic operations
293 if the operands have the given two types.
294
295 We do not deal with enumeral types here because they have already been
296 converted to integer types. */
297
298 tree
299 common_type (t1, t2)
300 tree t1, t2;
301 {
302 register enum tree_code code1;
303 register enum tree_code code2;
304 tree attributes;
305
306 /* Save time if the two types are the same. */
307
308 if (t1 == t2) return t1;
309
310 /* If one type is nonsense, use the other. */
311 if (t1 == error_mark_node)
312 return t2;
313 if (t2 == error_mark_node)
314 return t1;
315
316 /* Merge the attributes */
317
318 { register tree a1, a2;
319 a1 = TYPE_ATTRIBUTES (t1);
320 a2 = TYPE_ATTRIBUTES (t2);
321
322 /* Either one unset? Take the set one. */
323
324 if (!(attributes = a1))
325 attributes = a2;
326
327 /* One that completely contains the other? Take it. */
328
329 else if (a2 && !attribute_list_contained (a1, a2))
330 if (attribute_list_contained (a2, a1))
331 attributes = a2;
332 else
333 {
334 /* Pick the longest list, and hang on the other list. */
335 /* ??? For the moment we punt on the issue of attrs with args. */
336
337 if (list_length (a1) < list_length (a2))
338 attributes = a2, a2 = a1;
339
340 for (; a2; a2 = TREE_CHAIN (a2))
341 if (lookup_attribute (IDENTIFIER_POINTER (TREE_PURPOSE (a2)),
342 attributes) == NULL_TREE)
343 {
344 a1 = copy_node (a2);
345 TREE_CHAIN (a1) = attributes;
346 attributes = a1;
347 }
348 }
349 }
350
351 /* Treat an enum type as the unsigned integer type of the same width. */
352
353 if (TREE_CODE (t1) == ENUMERAL_TYPE)
354 t1 = type_for_size (TYPE_PRECISION (t1), 1);
355 if (TREE_CODE (t2) == ENUMERAL_TYPE)
356 t2 = type_for_size (TYPE_PRECISION (t2), 1);
357
358 if (TYPE_PTRMEMFUNC_P (t1))
359 t1 = TYPE_PTRMEMFUNC_FN_TYPE (t1);
360 if (TYPE_PTRMEMFUNC_P (t2))
361 t2 = TYPE_PTRMEMFUNC_FN_TYPE (t2);
362
363 code1 = TREE_CODE (t1);
364 code2 = TREE_CODE (t2);
365
366 /* If one type is complex, form the common type of the non-complex
367 components, then make that complex. Use T1 or T2 if it is the
368 required type. */
369 if (code1 == COMPLEX_TYPE || code2 == COMPLEX_TYPE)
370 {
371 tree subtype1 = code1 == COMPLEX_TYPE ? TREE_TYPE (t1) : t1;
372 tree subtype2 = code2 == COMPLEX_TYPE ? TREE_TYPE (t2) : t2;
373 tree subtype = common_type (subtype1, subtype2);
374
375 if (code1 == COMPLEX_TYPE && TREE_TYPE (t1) == subtype)
376 return build_type_attribute_variant (t1, attributes);
377 else if (code2 == COMPLEX_TYPE && TREE_TYPE (t2) == subtype)
378 return build_type_attribute_variant (t2, attributes);
379 else
380 return build_type_attribute_variant (build_complex_type (subtype),
381 attributes);
382 }
383
384 switch (code1)
385 {
386 case INTEGER_TYPE:
387 case REAL_TYPE:
388 /* If only one is real, use it as the result. */
389
390 if (code1 == REAL_TYPE && code2 != REAL_TYPE)
391 return build_type_attribute_variant (t1, attributes);
392
393 if (code2 == REAL_TYPE && code1 != REAL_TYPE)
394 return build_type_attribute_variant (t2, attributes);
395
396 /* Both real or both integers; use the one with greater precision. */
397
398 if (TYPE_PRECISION (t1) > TYPE_PRECISION (t2))
399 return build_type_attribute_variant (t1, attributes);
400 else if (TYPE_PRECISION (t2) > TYPE_PRECISION (t1))
401 return build_type_attribute_variant (t2, attributes);
402
403 /* Same precision. Prefer longs to ints even when same size. */
404
405 if (TYPE_MAIN_VARIANT (t1) == long_unsigned_type_node
406 || TYPE_MAIN_VARIANT (t2) == long_unsigned_type_node)
407 return build_type_attribute_variant (long_unsigned_type_node,
408 attributes);
409
410 if (TYPE_MAIN_VARIANT (t1) == long_integer_type_node
411 || TYPE_MAIN_VARIANT (t2) == long_integer_type_node)
412 {
413 /* But preserve unsignedness from the other type,
414 since long cannot hold all the values of an unsigned int. */
415 if (TREE_UNSIGNED (t1) || TREE_UNSIGNED (t2))
416 t1 = long_unsigned_type_node;
417 else
418 t1 = long_integer_type_node;
419 return build_type_attribute_variant (t1, attributes);
420 }
421
422 if (TYPE_MAIN_VARIANT (t1) == long_double_type_node
423 || TYPE_MAIN_VARIANT (t2) == long_double_type_node)
424 return build_type_attribute_variant (long_double_type_node,
425 attributes);
426
427 /* Otherwise prefer the unsigned one. */
428
429 if (TREE_UNSIGNED (t1))
430 return build_type_attribute_variant (t1, attributes);
431 else
432 return build_type_attribute_variant (t2, attributes);
433
434 case POINTER_TYPE:
435 case REFERENCE_TYPE:
436 /* For two pointers, do this recursively on the target type,
437 and combine the qualifiers of the two types' targets. */
438 /* This code was turned off; I don't know why.
439 But ANSI C++ specifies doing this with the qualifiers.
440 So I turned it on again. */
441 {
442 tree tt1 = TYPE_MAIN_VARIANT (TREE_TYPE (t1));
443 tree tt2 = TYPE_MAIN_VARIANT (TREE_TYPE (t2));
444 int constp
445 = TYPE_READONLY (TREE_TYPE (t1)) || TYPE_READONLY (TREE_TYPE (t2));
446 int volatilep
447 = TYPE_VOLATILE (TREE_TYPE (t1)) || TYPE_VOLATILE (TREE_TYPE (t2));
448 tree target;
449
450 if (tt1 == tt2)
451 target = tt1;
452 else if (tt1 == void_type_node || tt2 == void_type_node)
453 target = void_type_node;
454 else 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
651 can be mixed as valid: i.e. a pointer to a base class may be assigned
652 to a pointer to one of its derived 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 return attrval == 2 && val == 1 ? 2 : val;
849 }
850
851 /* Return 1 if TTL and TTR are pointers to types that are equivalent,
852 ignoring their qualifiers.
853
854 NPTRS is the number of pointers we can strip off and keep cool.
855 This is used to permit (for aggr A, aggr B) A, B* to convert to A*,
856 but to not permit B** to convert to A**. */
857
858 int
859 comp_target_types (ttl, ttr, nptrs)
860 tree ttl, ttr;
861 int nptrs;
862 {
863 ttl = TYPE_MAIN_VARIANT (ttl);
864 ttr = TYPE_MAIN_VARIANT (ttr);
865 if (ttl == ttr)
866 return 1;
867
868 if (TREE_CODE (ttr) != TREE_CODE (ttl))
869 return 0;
870
871 if (TREE_CODE (ttr) == POINTER_TYPE)
872 {
873 ttl = TREE_TYPE (ttl);
874 ttr = TREE_TYPE (ttr);
875
876 if (nptrs > 0)
877 {
878 if (TREE_CODE (ttl) == UNKNOWN_TYPE
879 || TREE_CODE (ttr) == UNKNOWN_TYPE)
880 return 1;
881 else if (TREE_CODE (ttl) == VOID_TYPE
882 && TREE_CODE (ttr) != FUNCTION_TYPE
883 && TREE_CODE (ttr) != METHOD_TYPE
884 && TREE_CODE (ttr) != OFFSET_TYPE)
885 return 1;
886 else if (TREE_CODE (ttr) == VOID_TYPE
887 && TREE_CODE (ttl) != FUNCTION_TYPE
888 && TREE_CODE (ttl) != METHOD_TYPE
889 && TREE_CODE (ttl) != OFFSET_TYPE)
890 return -1;
891 else if (TREE_CODE (ttl) == POINTER_TYPE
892 || TREE_CODE (ttl) == ARRAY_TYPE)
893 {
894 if (comp_ptr_ttypes (ttl, ttr))
895 return 1;
896 else if (comp_ptr_ttypes (ttr, ttl))
897 return -1;
898 return 0;
899 }
900 }
901
902 /* Const and volatile mean something different for function types,
903 so the usual checks are not appropriate. */
904 if (TREE_CODE (ttl) == FUNCTION_TYPE || TREE_CODE (ttl) == METHOD_TYPE)
905 return comp_target_types (ttl, ttr, nptrs - 1);
906
907 /* Make sure that the cv-quals change only in the same direction as
908 the target type. */
909 {
910 int t;
911 int c = TYPE_READONLY (ttl) - TYPE_READONLY (ttr);
912 int v = TYPE_VOLATILE (ttl) - TYPE_VOLATILE (ttr);
913
914 if ((c > 0 && v < 0) || (c < 0 && v > 0))
915 return 0;
916
917 if (TYPE_MAIN_VARIANT (ttl) == TYPE_MAIN_VARIANT (ttr))
918 return (c + v < 0) ? -1 : 1;
919
920 t = comp_target_types (ttl, ttr, nptrs - 1);
921 if ((t == 1 && c + v >= 0) || (t == -1 && c + v <= 0))
922 return t;
923
924 return 0;
925 }
926 }
927
928 if (TREE_CODE (ttr) == REFERENCE_TYPE)
929 return comp_target_types (TREE_TYPE (ttl), TREE_TYPE (ttr), nptrs);
930 if (TREE_CODE (ttr) == ARRAY_TYPE)
931 return comp_array_types (comp_target_types, ttl, ttr, 0);
932 else if (TREE_CODE (ttr) == FUNCTION_TYPE || TREE_CODE (ttr) == METHOD_TYPE)
933 if (comp_target_types (TREE_TYPE (ttl), TREE_TYPE (ttr), nptrs))
934 switch (comp_target_parms (TYPE_ARG_TYPES (ttl), TYPE_ARG_TYPES (ttr), 1))
935 {
936 case 0:
937 return 0;
938 case 1:
939 return 1;
940 case 2:
941 return -1;
942 default:
943 my_friendly_abort (112);
944 }
945 else
946 return 0;
947
948 /* for C++ */
949 else if (TREE_CODE (ttr) == OFFSET_TYPE)
950 {
951 /* Contravariance: we can assign a pointer to base member to a pointer
952 to derived member. Note difference from simple pointer case, where
953 we can pass a pointer to derived to a pointer to base. */
954 if (comptypes (TYPE_OFFSET_BASETYPE (ttr), TYPE_OFFSET_BASETYPE (ttl), 0))
955 return comp_target_types (TREE_TYPE (ttl), TREE_TYPE (ttr), nptrs);
956 else if (comptypes (TYPE_OFFSET_BASETYPE (ttl), TYPE_OFFSET_BASETYPE (ttr), 0)
957 && comp_target_types (TREE_TYPE (ttl), TREE_TYPE (ttr), nptrs))
958 return -1;
959 }
960 else if (IS_AGGR_TYPE (ttl))
961 {
962 if (nptrs < 0)
963 return 0;
964 if (comptypes (build_pointer_type (ttl), build_pointer_type (ttr), 0))
965 return 1;
966 if (comptypes (build_pointer_type (ttr), build_pointer_type (ttl), 0))
967 return -1;
968 return 0;
969 }
970
971 return 0;
972 }
973
974 /* If two types share a common base type, return that basetype.
975 If there is not a unique most-derived base type, this function
976 returns ERROR_MARK_NODE. */
977
978 static tree
979 common_base_type (tt1, tt2)
980 tree tt1, tt2;
981 {
982 tree best = NULL_TREE;
983 int i;
984
985 /* If one is a baseclass of another, that's good enough. */
986 if (UNIQUELY_DERIVED_FROM_P (tt1, tt2))
987 return tt1;
988 if (UNIQUELY_DERIVED_FROM_P (tt2, tt1))
989 return tt2;
990
991 /* Otherwise, try to find a unique baseclass of TT1
992 that is shared by TT2, and follow that down. */
993 for (i = CLASSTYPE_N_BASECLASSES (tt1)-1; i >= 0; i--)
994 {
995 tree basetype = TYPE_BINFO_BASETYPE (tt1, i);
996 tree trial = common_base_type (basetype, tt2);
997 if (trial)
998 {
999 if (trial == error_mark_node)
1000 return trial;
1001 if (best == NULL_TREE)
1002 best = trial;
1003 else if (best != trial)
1004 return error_mark_node;
1005 }
1006 }
1007
1008 /* Same for TT2. */
1009 for (i = CLASSTYPE_N_BASECLASSES (tt2)-1; i >= 0; i--)
1010 {
1011 tree basetype = TYPE_BINFO_BASETYPE (tt2, i);
1012 tree trial = common_base_type (tt1, basetype);
1013 if (trial)
1014 {
1015 if (trial == error_mark_node)
1016 return trial;
1017 if (best == NULL_TREE)
1018 best = trial;
1019 else if (best != trial)
1020 return error_mark_node;
1021 }
1022 }
1023 return best;
1024 }
1025 \f
1026 /* Subroutines of `comptypes'. */
1027
1028 /* Return 1 if two parameter type lists PARMS1 and PARMS2
1029 are equivalent in the sense that functions with those parameter types
1030 can have equivalent types.
1031 If either list is empty, we win.
1032 Otherwise, the two lists must be equivalent, element by element.
1033
1034 C++: See comment above about TYPE1, TYPE2, STRICT.
1035 If STRICT == 3, it means checking is strict, but do not compare
1036 default parameter values. */
1037
1038 int
1039 compparms (parms1, parms2, strict)
1040 tree parms1, parms2;
1041 int strict;
1042 {
1043 register tree t1 = parms1, t2 = parms2;
1044
1045 /* An unspecified parmlist matches any specified parmlist
1046 whose argument types don't need default promotions. */
1047
1048 if (strict <= 0 && t1 == 0)
1049 return self_promoting_args_p (t2);
1050 if (strict < 0 && t2 == 0)
1051 return self_promoting_args_p (t1);
1052
1053 while (1)
1054 {
1055 if (t1 == 0 && t2 == 0)
1056 return 1;
1057 /* If one parmlist is shorter than the other,
1058 they fail to match, unless STRICT is <= 0. */
1059 if (t1 == 0 || t2 == 0)
1060 {
1061 if (strict > 0)
1062 return 0;
1063 if (strict < 0)
1064 return 1;
1065 if (strict == 0)
1066 return t1 && TREE_PURPOSE (t1);
1067 }
1068 if (! comptypes (TREE_VALUE (t2), TREE_VALUE (t1), strict))
1069 {
1070 if (strict > 0)
1071 return 0;
1072 if (strict == 0)
1073 return t2 == void_list_node && TREE_PURPOSE (t1);
1074 return TREE_PURPOSE (t1) || TREE_PURPOSE (t2);
1075 }
1076
1077 t1 = TREE_CHAIN (t1);
1078 t2 = TREE_CHAIN (t2);
1079 }
1080 }
1081
1082 /* This really wants return whether or not parameter type lists
1083 would make their owning functions assignment compatible or not. */
1084
1085 static int
1086 comp_target_parms (parms1, parms2, strict)
1087 tree parms1, parms2;
1088 int strict;
1089 {
1090 register tree t1 = parms1, t2 = parms2;
1091 int warn_contravariance = 0;
1092
1093 /* An unspecified parmlist matches any specified parmlist
1094 whose argument types don't need default promotions.
1095 @@@ see 13.3.3 for a counterexample... */
1096
1097 if (t1 == 0 && t2 != 0)
1098 {
1099 cp_pedwarn ("ANSI C++ prohibits conversion from `(%#T)' to `(...)'",
1100 parms2);
1101 return self_promoting_args_p (t2);
1102 }
1103 if (t2 == 0)
1104 return self_promoting_args_p (t1);
1105
1106 for (; t1 || t2; t1 = TREE_CHAIN (t1), t2 = TREE_CHAIN (t2))
1107 {
1108 tree p1, p2;
1109
1110 /* If one parmlist is shorter than the other,
1111 they fail to match, unless STRICT is <= 0. */
1112 if (t1 == 0 || t2 == 0)
1113 {
1114 if (strict > 0)
1115 return 0;
1116 if (strict < 0)
1117 return 1 + warn_contravariance;
1118 return ((t1 && TREE_PURPOSE (t1)) + warn_contravariance);
1119 }
1120 p1 = TREE_VALUE (t1);
1121 p2 = TREE_VALUE (t2);
1122 if (p1 == p2)
1123 continue;
1124
1125 if ((TREE_CODE (p1) == POINTER_TYPE && TREE_CODE (p2) == POINTER_TYPE)
1126 || (TREE_CODE (p1) == REFERENCE_TYPE && TREE_CODE (p2) == REFERENCE_TYPE))
1127 {
1128 if (strict <= 0
1129 && (TYPE_MAIN_VARIANT (TREE_TYPE (p1))
1130 == TYPE_MAIN_VARIANT (TREE_TYPE (p2))))
1131 continue;
1132
1133 /* The following is wrong for contravariance,
1134 but many programs depend on it. */
1135 if (TREE_TYPE (p1) == void_type_node)
1136 continue;
1137 if (TREE_TYPE (p2) == void_type_node)
1138 {
1139 warn_contravariance = 1;
1140 continue;
1141 }
1142 if (IS_AGGR_TYPE (TREE_TYPE (p1)))
1143 {
1144 if (comptypes (p2, p1, 0) == 0)
1145 {
1146 if (comptypes (p1, p2, 0) != 0)
1147 warn_contravariance = 1;
1148 else
1149 return 0;
1150 }
1151 continue;
1152 }
1153 }
1154 /* Note backwards order due to contravariance. */
1155 if (comp_target_types (p2, p1, 1) == 0)
1156 {
1157 if (comp_target_types (p1, p2, 1))
1158 {
1159 warn_contravariance = 1;
1160 continue;
1161 }
1162 if (strict != 0)
1163 return 0;
1164 }
1165 /* Target types are compatible--just make sure that if
1166 we use parameter lists, that they are ok as well. */
1167 if (TREE_CODE (p1) == FUNCTION_TYPE || TREE_CODE (p1) == METHOD_TYPE)
1168 switch (comp_target_parms (TYPE_ARG_TYPES (p1),
1169 TYPE_ARG_TYPES (p2),
1170 strict))
1171 {
1172 case 0:
1173 return 0;
1174 case 1:
1175 break;
1176 case 2:
1177 warn_contravariance = 1;
1178 }
1179
1180 if (TREE_PURPOSE (t1) && TREE_PURPOSE (t2))
1181 {
1182 int cmp = simple_cst_equal (TREE_PURPOSE (t1), TREE_PURPOSE (t2));
1183 if (cmp < 0)
1184 my_friendly_abort (114);
1185 if (cmp == 0)
1186 return 0;
1187 }
1188 }
1189 return 1 + warn_contravariance;
1190 }
1191
1192 /* Return 1 if PARMS specifies a fixed number of parameters
1193 and none of their types is affected by default promotions. */
1194
1195 int
1196 self_promoting_args_p (parms)
1197 tree parms;
1198 {
1199 register tree t;
1200 for (t = parms; t; t = TREE_CHAIN (t))
1201 {
1202 register tree type = TREE_VALUE (t);
1203
1204 if (TREE_CHAIN (t) == 0 && type != void_type_node)
1205 return 0;
1206
1207 if (type == 0)
1208 return 0;
1209
1210 if (TYPE_MAIN_VARIANT (type) == float_type_node)
1211 return 0;
1212
1213 if (C_PROMOTING_INTEGER_TYPE_P (type))
1214 return 0;
1215 }
1216 return 1;
1217 }
1218 \f
1219 /* Return an unsigned type the same as TYPE in other respects.
1220
1221 C++: must make these work for type variants as well. */
1222
1223 tree
1224 unsigned_type (type)
1225 tree type;
1226 {
1227 tree type1 = TYPE_MAIN_VARIANT (type);
1228 if (type1 == signed_char_type_node || type1 == char_type_node)
1229 return unsigned_char_type_node;
1230 if (type1 == integer_type_node)
1231 return unsigned_type_node;
1232 if (type1 == short_integer_type_node)
1233 return short_unsigned_type_node;
1234 if (type1 == long_integer_type_node)
1235 return long_unsigned_type_node;
1236 if (type1 == long_long_integer_type_node)
1237 return long_long_unsigned_type_node;
1238 if (type1 == intDI_type_node)
1239 return unsigned_intDI_type_node;
1240 if (type1 == intSI_type_node)
1241 return unsigned_intSI_type_node;
1242 if (type1 == intHI_type_node)
1243 return unsigned_intHI_type_node;
1244 if (type1 == intQI_type_node)
1245 return unsigned_intQI_type_node;
1246
1247 return signed_or_unsigned_type (1, type);
1248 }
1249
1250 /* Return a signed type the same as TYPE in other respects. */
1251
1252 tree
1253 signed_type (type)
1254 tree type;
1255 {
1256 tree type1 = TYPE_MAIN_VARIANT (type);
1257 if (type1 == unsigned_char_type_node || type1 == char_type_node)
1258 return signed_char_type_node;
1259 if (type1 == unsigned_type_node)
1260 return integer_type_node;
1261 if (type1 == short_unsigned_type_node)
1262 return short_integer_type_node;
1263 if (type1 == long_unsigned_type_node)
1264 return long_integer_type_node;
1265 if (type1 == long_long_unsigned_type_node)
1266 return long_long_integer_type_node;
1267 if (type1 == unsigned_intDI_type_node)
1268 return intDI_type_node;
1269 if (type1 == unsigned_intSI_type_node)
1270 return intSI_type_node;
1271 if (type1 == unsigned_intHI_type_node)
1272 return intHI_type_node;
1273 if (type1 == unsigned_intQI_type_node)
1274 return intQI_type_node;
1275
1276 return signed_or_unsigned_type (0, type);
1277 }
1278
1279 /* Return a type the same as TYPE except unsigned or
1280 signed according to UNSIGNEDP. */
1281
1282 tree
1283 signed_or_unsigned_type (unsignedp, type)
1284 int unsignedp;
1285 tree type;
1286 {
1287 if (! INTEGRAL_TYPE_P (type)
1288 || TREE_UNSIGNED (type) == unsignedp)
1289 return type;
1290
1291 if (TYPE_PRECISION (type) == TYPE_PRECISION (signed_char_type_node))
1292 return unsignedp ? unsigned_char_type_node : signed_char_type_node;
1293 if (TYPE_PRECISION (type) == TYPE_PRECISION (integer_type_node))
1294 return unsignedp ? unsigned_type_node : integer_type_node;
1295 if (TYPE_PRECISION (type) == TYPE_PRECISION (short_integer_type_node))
1296 return unsignedp ? short_unsigned_type_node : short_integer_type_node;
1297 if (TYPE_PRECISION (type) == TYPE_PRECISION (long_integer_type_node))
1298 return unsignedp ? long_unsigned_type_node : long_integer_type_node;
1299 if (TYPE_PRECISION (type) == TYPE_PRECISION (long_long_integer_type_node))
1300 return (unsignedp ? long_long_unsigned_type_node
1301 : long_long_integer_type_node);
1302 return type;
1303 }
1304
1305 /* Compute the value of the `sizeof' operator. */
1306
1307 tree
1308 c_sizeof (type)
1309 tree type;
1310 {
1311 enum tree_code code = TREE_CODE (type);
1312 tree t;
1313
1314 if (processing_template_decl)
1315 return build_min (SIZEOF_EXPR, sizetype, type);
1316
1317 if (code == FUNCTION_TYPE)
1318 {
1319 if (pedantic || warn_pointer_arith)
1320 pedwarn ("ANSI C++ forbids taking the sizeof a function type");
1321 return size_int (1);
1322 }
1323 if (code == METHOD_TYPE)
1324 {
1325 if (pedantic || warn_pointer_arith)
1326 pedwarn ("ANSI C++ forbids taking the sizeof a method type");
1327 return size_int (1);
1328 }
1329 if (code == VOID_TYPE)
1330 {
1331 if (pedantic || warn_pointer_arith)
1332 pedwarn ("ANSI C++ forbids taking the sizeof a void type");
1333 return size_int (1);
1334 }
1335 if (code == ERROR_MARK)
1336 return size_int (1);
1337
1338 /* ARM $5.3.2: ``When applied to a reference, the result is the size of the
1339 referenced object.'' */
1340 if (code == REFERENCE_TYPE)
1341 type = TREE_TYPE (type);
1342
1343 /* We couldn't find anything in the ARM or the draft standard that says,
1344 one way or the other, if doing sizeof on something that doesn't have
1345 an object associated with it is correct or incorrect. For example, if
1346 you declare `struct S { char str[16]; };', and in your program do
1347 a `sizeof (S::str)', should we flag that as an error or should we give
1348 the size of it? Since it seems like a reasonable thing to do, we'll go
1349 with giving the value. */
1350 if (code == OFFSET_TYPE)
1351 type = TREE_TYPE (type);
1352
1353 /* @@ This also produces an error for a signature ref.
1354 In that case we should be able to do better. */
1355 if (IS_SIGNATURE (type))
1356 {
1357 error ("`sizeof' applied to a signature type");
1358 return size_int (0);
1359 }
1360
1361 if (TYPE_SIZE (complete_type (type)) == 0)
1362 {
1363 cp_error ("`sizeof' applied to incomplete type `%T'", type);
1364 return size_int (0);
1365 }
1366
1367 /* Convert in case a char is more than one unit. */
1368 t = size_binop (CEIL_DIV_EXPR, TYPE_SIZE (type),
1369 size_int (TYPE_PRECISION (char_type_node)));
1370 /* size_binop does not put the constant in range, so do it now. */
1371 if (TREE_CODE (t) == INTEGER_CST && force_fit_type (t, 0))
1372 TREE_CONSTANT_OVERFLOW (t) = TREE_OVERFLOW (t) = 1;
1373 return t;
1374 }
1375
1376 tree
1377 expr_sizeof (e)
1378 tree e;
1379 {
1380 if (processing_template_decl)
1381 return build_min (SIZEOF_EXPR, sizetype, e);
1382
1383 if (TREE_CODE (e) == COMPONENT_REF
1384 && DECL_BIT_FIELD (TREE_OPERAND (e, 1)))
1385 error ("sizeof applied to a bit-field");
1386 /* ANSI says arrays and functions are converted inside comma.
1387 But we can't really convert them in build_compound_expr
1388 because that would break commas in lvalues.
1389 So do the conversion here if operand was a comma. */
1390 if (TREE_CODE (e) == COMPOUND_EXPR
1391 && (TREE_CODE (TREE_TYPE (e)) == ARRAY_TYPE
1392 || TREE_CODE (TREE_TYPE (e)) == FUNCTION_TYPE))
1393 e = default_conversion (e);
1394 else if (TREE_CODE (e) == TREE_LIST)
1395 {
1396 tree t = TREE_VALUE (e);
1397 if (t != NULL_TREE
1398 && ((TREE_TYPE (t)
1399 && TREE_CODE (TREE_TYPE (t)) == FUNCTION_TYPE)
1400 || is_overloaded_fn (t)))
1401 pedwarn ("ANSI C++ forbids taking the sizeof a function type");
1402 }
1403 return c_sizeof (TREE_TYPE (e));
1404 }
1405
1406 tree
1407 c_sizeof_nowarn (type)
1408 tree type;
1409 {
1410 enum tree_code code = TREE_CODE (type);
1411 tree t;
1412
1413 if (code == FUNCTION_TYPE
1414 || code == METHOD_TYPE
1415 || code == VOID_TYPE
1416 || code == ERROR_MARK)
1417 return size_int (1);
1418 if (code == REFERENCE_TYPE)
1419 type = TREE_TYPE (type);
1420
1421 if (TYPE_SIZE (type) == 0)
1422 return size_int (0);
1423
1424 /* Convert in case a char is more than one unit. */
1425 t = size_binop (CEIL_DIV_EXPR, TYPE_SIZE (type),
1426 size_int (TYPE_PRECISION (char_type_node)));
1427 force_fit_type (t, 0);
1428 return t;
1429 }
1430
1431 /* Implement the __alignof keyword: Return the minimum required
1432 alignment of TYPE, measured in bytes. */
1433
1434 tree
1435 c_alignof (type)
1436 tree type;
1437 {
1438 enum tree_code code = TREE_CODE (type);
1439 tree t;
1440
1441 if (code == FUNCTION_TYPE || code == METHOD_TYPE)
1442 return size_int (FUNCTION_BOUNDARY / BITS_PER_UNIT);
1443
1444 if (code == VOID_TYPE || code == ERROR_MARK)
1445 return size_int (1);
1446
1447 /* C++: this is really correct! */
1448 if (code == REFERENCE_TYPE)
1449 type = TREE_TYPE (type);
1450
1451 /* @@ This also produces an error for a signature ref.
1452 In that case we should be able to do better. */
1453 if (IS_SIGNATURE (type))
1454 {
1455 error ("`__alignof' applied to a signature type");
1456 return size_int (1);
1457 }
1458
1459 t = size_int (TYPE_ALIGN (type) / BITS_PER_UNIT);
1460 force_fit_type (t, 0);
1461 return t;
1462 }
1463 \f
1464 /* Perform default promotions for C data used in expressions.
1465 Arrays and functions are converted to pointers;
1466 enumeral types or short or char, to int.
1467 In addition, manifest constants symbols are replaced by their values.
1468
1469 C++: this will automatically bash references to their target type. */
1470
1471 tree
1472 decay_conversion (exp)
1473 tree exp;
1474 {
1475 register tree type = TREE_TYPE (exp);
1476 register enum tree_code code = TREE_CODE (type);
1477
1478 if (code == OFFSET_TYPE)
1479 {
1480 if (TREE_CODE (exp) == OFFSET_REF)
1481 return decay_conversion (resolve_offset_ref (exp));
1482
1483 type = TREE_TYPE (type);
1484 code = TREE_CODE (type);
1485
1486 if (type == unknown_type_node)
1487 {
1488 cp_pedwarn ("assuming & on overloaded member function");
1489 return build_unary_op (ADDR_EXPR, exp, 0);
1490 }
1491 }
1492
1493 if (code == REFERENCE_TYPE)
1494 {
1495 exp = convert_from_reference (exp);
1496 type = TREE_TYPE (exp);
1497 code = TREE_CODE (type);
1498 }
1499
1500 /* Constants can be used directly unless they're not loadable. */
1501 if (TREE_CODE (exp) == CONST_DECL)
1502 exp = DECL_INITIAL (exp);
1503 /* Replace a nonvolatile const static variable with its value. */
1504 else if (TREE_READONLY_DECL_P (exp))
1505 {
1506 exp = decl_constant_value (exp);
1507 type = TREE_TYPE (exp);
1508 }
1509
1510 /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
1511 Leave such NOP_EXPRs, since RHS is being used in non-lvalue context. */
1512
1513 if (code == VOID_TYPE)
1514 {
1515 error ("void value not ignored as it ought to be");
1516 return error_mark_node;
1517 }
1518 if (code == FUNCTION_TYPE)
1519 {
1520 return build_unary_op (ADDR_EXPR, exp, 0);
1521 }
1522 if (code == METHOD_TYPE)
1523 {
1524 cp_pedwarn ("assuming & on `%E'", exp);
1525 return build_unary_op (ADDR_EXPR, exp, 0);
1526 }
1527 if (code == ARRAY_TYPE)
1528 {
1529 register tree adr;
1530 tree restype;
1531 tree ptrtype;
1532 int constp, volatilep;
1533
1534 if (TREE_CODE (exp) == INDIRECT_REF)
1535 {
1536 /* Stripping away the INDIRECT_REF is not the right
1537 thing to do for references... */
1538 tree inner = TREE_OPERAND (exp, 0);
1539 if (TREE_CODE (TREE_TYPE (inner)) == REFERENCE_TYPE)
1540 {
1541 inner = build1 (CONVERT_EXPR,
1542 build_pointer_type (TREE_TYPE (TREE_TYPE (inner))),
1543 inner);
1544 TREE_CONSTANT (inner) = TREE_CONSTANT (TREE_OPERAND (inner, 0));
1545 }
1546 return cp_convert (build_pointer_type (TREE_TYPE (type)), inner);
1547 }
1548
1549 if (TREE_CODE (exp) == COMPOUND_EXPR)
1550 {
1551 tree op1 = decay_conversion (TREE_OPERAND (exp, 1));
1552 return build (COMPOUND_EXPR, TREE_TYPE (op1),
1553 TREE_OPERAND (exp, 0), op1);
1554 }
1555
1556 if (!lvalue_p (exp)
1557 && ! (TREE_CODE (exp) == CONSTRUCTOR && TREE_STATIC (exp)))
1558 {
1559 error ("invalid use of non-lvalue array");
1560 return error_mark_node;
1561 }
1562
1563 constp = volatilep = 0;
1564 if (TREE_CODE_CLASS (TREE_CODE (exp)) == 'r'
1565 || TREE_CODE_CLASS (TREE_CODE (exp)) == 'd')
1566 {
1567 constp = TREE_READONLY (exp);
1568 volatilep = TREE_THIS_VOLATILE (exp);
1569 }
1570
1571 restype = TREE_TYPE (type);
1572 if (TYPE_READONLY (type) || TYPE_VOLATILE (type)
1573 || constp || volatilep)
1574 restype = cp_build_type_variant (restype,
1575 TYPE_READONLY (type) || constp,
1576 TYPE_VOLATILE (type) || volatilep);
1577 ptrtype = build_pointer_type (restype);
1578
1579 if (TREE_CODE (exp) == VAR_DECL)
1580 {
1581 /* ??? This is not really quite correct
1582 in that the type of the operand of ADDR_EXPR
1583 is not the target type of the type of the ADDR_EXPR itself.
1584 Question is, can this lossage be avoided? */
1585 adr = build1 (ADDR_EXPR, ptrtype, exp);
1586 if (mark_addressable (exp) == 0)
1587 return error_mark_node;
1588 TREE_CONSTANT (adr) = staticp (exp);
1589 TREE_SIDE_EFFECTS (adr) = 0; /* Default would be, same as EXP. */
1590 return adr;
1591 }
1592 /* This way is better for a COMPONENT_REF since it can
1593 simplify the offset for a component. */
1594 adr = build_unary_op (ADDR_EXPR, exp, 1);
1595 return cp_convert (ptrtype, adr);
1596 }
1597
1598 return exp;
1599 }
1600
1601 tree
1602 default_conversion (exp)
1603 tree exp;
1604 {
1605 tree type;
1606 enum tree_code code;
1607
1608 exp = decay_conversion (exp);
1609
1610 type = TREE_TYPE (exp);
1611 code = TREE_CODE (type);
1612
1613 if (INTEGRAL_CODE_P (code))
1614 {
1615 tree t = type_promotes_to (type);
1616 if (t != type)
1617 return cp_convert (t, exp);
1618 }
1619
1620 return exp;
1621 }
1622
1623 /* Take the address of an inline function without setting TREE_ADDRESSABLE
1624 or TREE_USED. */
1625
1626 tree
1627 inline_conversion (exp)
1628 tree exp;
1629 {
1630 if (TREE_CODE (exp) == FUNCTION_DECL)
1631 {
1632 tree type = build_type_variant
1633 (TREE_TYPE (exp), TREE_READONLY (exp), TREE_THIS_VOLATILE (exp));
1634 exp = build1 (ADDR_EXPR, build_pointer_type (type), exp);
1635 }
1636 return exp;
1637 }
1638 \f
1639 tree
1640 build_object_ref (datum, basetype, field)
1641 tree datum, basetype, field;
1642 {
1643 tree dtype;
1644 if (datum == error_mark_node)
1645 return error_mark_node;
1646
1647 dtype = TREE_TYPE (datum);
1648 if (TREE_CODE (dtype) == REFERENCE_TYPE)
1649 dtype = TREE_TYPE (dtype);
1650 if (! IS_AGGR_TYPE_CODE (TREE_CODE (dtype)))
1651 {
1652 cp_error ("request for member `%T::%D' in expression of non-aggregate type `%T'",
1653 basetype, field, dtype);
1654 return error_mark_node;
1655 }
1656 else if (IS_SIGNATURE (basetype))
1657 {
1658 warning ("signature name in scope resolution ignored");
1659 return build_component_ref (datum, field, NULL_TREE, 1);
1660 }
1661 else if (is_aggr_type (basetype, 1))
1662 {
1663 tree binfo = binfo_or_else (basetype, dtype);
1664 if (binfo)
1665 return build_x_component_ref (build_scoped_ref (datum, basetype),
1666 field, binfo, 1);
1667 }
1668 return error_mark_node;
1669 }
1670
1671 /* Like `build_component_ref, but uses an already found field, and converts
1672 from a reference. Must compute access for current_class_ref.
1673 Otherwise, ok. */
1674
1675 tree
1676 build_component_ref_1 (datum, field, protect)
1677 tree datum, field;
1678 int protect;
1679 {
1680 return convert_from_reference
1681 (build_component_ref (datum, field, NULL_TREE, protect));
1682 }
1683
1684 /* Given a COND_EXPR, MIN_EXPR, or MAX_EXPR in T, return it in a form that we
1685 can, for example, use as an lvalue. This code used to be in
1686 unary_complex_lvalue, but we needed it to deal with `a = (d == c) ? b : c'
1687 expressions, where we're dealing with aggregates. But now it's again only
1688 called from unary_complex_lvalue. The case (in particular) that led to
1689 this was with CODE == ADDR_EXPR, since it's not an lvalue when we'd
1690 get it there. */
1691
1692 static tree
1693 rationalize_conditional_expr (code, t)
1694 enum tree_code code;
1695 tree t;
1696 {
1697 /* For MIN_EXPR or MAX_EXPR, fold-const.c has arranged things so that
1698 the first operand is always the one to be used if both operands
1699 are equal, so we know what conditional expression this used to be. */
1700 if (TREE_CODE (t) == MIN_EXPR || TREE_CODE (t) == MAX_EXPR)
1701 {
1702 return
1703 build_conditional_expr (build_x_binary_op ((TREE_CODE (t) == MIN_EXPR
1704 ? LE_EXPR : GE_EXPR),
1705 TREE_OPERAND (t, 0),
1706 TREE_OPERAND (t, 1)),
1707 build_unary_op (code, TREE_OPERAND (t, 0), 0),
1708 build_unary_op (code, TREE_OPERAND (t, 1), 0));
1709 }
1710
1711 return
1712 build_conditional_expr (TREE_OPERAND (t, 0),
1713 build_unary_op (code, TREE_OPERAND (t, 1), 0),
1714 build_unary_op (code, TREE_OPERAND (t, 2), 0));
1715 }
1716
1717 /* Given the TYPE of an anonymous union field inside T, return the
1718 FIELD_DECL for the field. If not found return NULL_TREE. Because
1719 anonymous unions can nest, we must also search all anonymous unions
1720 that are directly reachable. */
1721
1722 static tree
1723 lookup_anon_field (t, type)
1724 tree t, type;
1725 {
1726 tree field;
1727
1728 for (field = TYPE_FIELDS (t); field; field = TREE_CHAIN (field))
1729 {
1730 if (TREE_STATIC (field))
1731 continue;
1732 if (TREE_CODE (field) != FIELD_DECL)
1733 continue;
1734
1735 /* If we find it directly, return the field. */
1736 if (DECL_NAME (field) == NULL_TREE
1737 && type == TREE_TYPE (field))
1738 {
1739 return field;
1740 }
1741
1742 /* Otherwise, it could be nested, search harder. */
1743 if (DECL_NAME (field) == NULL_TREE
1744 && TREE_CODE (TREE_TYPE (field)) == UNION_TYPE)
1745 {
1746 tree subfield = lookup_anon_field (TREE_TYPE (field), type);
1747 if (subfield)
1748 return subfield;
1749 }
1750 }
1751 return NULL_TREE;
1752 }
1753
1754 /* Build a COMPONENT_REF for a given DATUM, and it's member COMPONENT.
1755 COMPONENT can be an IDENTIFIER_NODE that is the name of the member
1756 that we are interested in, or it can be a FIELD_DECL. */
1757
1758 tree
1759 build_component_ref (datum, component, basetype_path, protect)
1760 tree datum, component, basetype_path;
1761 int protect;
1762 {
1763 register tree basetype = TREE_TYPE (datum);
1764 register enum tree_code code;
1765 register tree field = NULL;
1766 register tree ref;
1767
1768 if (processing_template_decl)
1769 return build_min_nt (COMPONENT_REF, datum, component);
1770
1771 /* If DATUM is a COMPOUND_EXPR or COND_EXPR, move our reference
1772 inside it. */
1773 switch (TREE_CODE (datum))
1774 {
1775 case COMPOUND_EXPR:
1776 {
1777 tree value = build_component_ref (TREE_OPERAND (datum, 1), component,
1778 basetype_path, protect);
1779 return build (COMPOUND_EXPR, TREE_TYPE (value),
1780 TREE_OPERAND (datum, 0), value);
1781 }
1782 case COND_EXPR:
1783 return build_conditional_expr
1784 (TREE_OPERAND (datum, 0),
1785 build_component_ref (TREE_OPERAND (datum, 1), component,
1786 basetype_path, protect),
1787 build_component_ref (TREE_OPERAND (datum, 2), component,
1788 basetype_path, protect));
1789 }
1790
1791 code = TREE_CODE (basetype);
1792
1793 if (code == REFERENCE_TYPE)
1794 {
1795 datum = convert_from_reference (datum);
1796 basetype = TREE_TYPE (datum);
1797 code = TREE_CODE (basetype);
1798 }
1799 if (TREE_CODE (datum) == OFFSET_REF)
1800 {
1801 datum = resolve_offset_ref (datum);
1802 basetype = TREE_TYPE (datum);
1803 code = TREE_CODE (basetype);
1804 }
1805
1806 /* First, see if there is a field or component with name COMPONENT. */
1807 if (TREE_CODE (component) == TREE_LIST)
1808 {
1809 my_friendly_assert (!(TREE_CHAIN (component) == NULL_TREE
1810 && DECL_CHAIN (TREE_VALUE (component)) == NULL_TREE), 309);
1811 return build (COMPONENT_REF, TREE_TYPE (component), datum, component);
1812 }
1813
1814 if (! IS_AGGR_TYPE_CODE (code))
1815 {
1816 if (code != ERROR_MARK)
1817 cp_error ("request for member `%D' in `%E', which is of non-aggregate type `%T'",
1818 component, datum, basetype);
1819 return error_mark_node;
1820 }
1821
1822 if (TYPE_SIZE (complete_type (basetype)) == 0)
1823 {
1824 incomplete_type_error (0, basetype);
1825 return error_mark_node;
1826 }
1827
1828 if (TREE_CODE (component) == BIT_NOT_EXPR)
1829 {
1830 if (TYPE_IDENTIFIER (basetype) != TREE_OPERAND (component, 0))
1831 {
1832 cp_error ("destructor specifier `%T::~%T' must have matching names",
1833 basetype, TREE_OPERAND (component, 0));
1834 return error_mark_node;
1835 }
1836 if (! TYPE_HAS_DESTRUCTOR (basetype))
1837 {
1838 cp_error ("type `%T' has no destructor", basetype);
1839 return error_mark_node;
1840 }
1841 return TREE_VEC_ELT (CLASSTYPE_METHOD_VEC (basetype), 1);
1842 }
1843
1844 /* Look up component name in the structure type definition. */
1845 if (CLASSTYPE_VFIELD (basetype)
1846 && DECL_NAME (CLASSTYPE_VFIELD (basetype)) == component)
1847 /* Special-case this because if we use normal lookups in an ambiguous
1848 hierarchy, the compiler will abort (because vptr lookups are
1849 not supposed to be ambiguous. */
1850 field = CLASSTYPE_VFIELD (basetype);
1851 else if (TREE_CODE (component) == FIELD_DECL
1852 || TREE_CODE (component) == TYPE_DECL)
1853 {
1854 field = component;
1855 }
1856 else
1857 {
1858 tree name = component;
1859 if (TREE_CODE (component) == VAR_DECL)
1860 name = DECL_NAME (component);
1861 if (basetype_path == NULL_TREE)
1862 basetype_path = TYPE_BINFO (basetype);
1863 field = lookup_field (basetype_path, name,
1864 protect && !VFIELD_NAME_P (name), 0);
1865 if (field == error_mark_node)
1866 return error_mark_node;
1867
1868 if (field == NULL_TREE)
1869 {
1870 /* Not found as a data field, look for it as a method. If found,
1871 then if this is the only possible one, return it, else
1872 report ambiguity error. */
1873 tree fndecls = lookup_fnfields (basetype_path, name, 1);
1874 if (fndecls == error_mark_node)
1875 return error_mark_node;
1876 if (fndecls)
1877 {
1878 if (TREE_CHAIN (fndecls) == NULL_TREE
1879 && DECL_CHAIN (TREE_VALUE (fndecls)) == NULL_TREE)
1880 {
1881 tree access, fndecl;
1882
1883 /* Unique, so use this one now. */
1884 basetype = TREE_PURPOSE (fndecls);
1885 fndecl = TREE_VALUE (fndecls);
1886 access = compute_access (TREE_PURPOSE (fndecls), fndecl);
1887 if (access == access_public_node)
1888 {
1889 if (DECL_VINDEX (fndecl)
1890 && ! resolves_to_fixed_type_p (datum, 0))
1891 {
1892 tree addr = build_unary_op (ADDR_EXPR, datum, 0);
1893 tree fntype = TREE_TYPE (fndecl);
1894
1895 addr = convert_pointer_to (DECL_CONTEXT (fndecl), addr);
1896 datum = build_indirect_ref (addr, NULL_PTR);
1897 my_friendly_assert (datum != error_mark_node, 310);
1898 fndecl = build_vfn_ref (&addr, datum, DECL_VINDEX (fndecl));
1899 TREE_TYPE (fndecl) = build_pointer_type (fntype);
1900 }
1901 else
1902 mark_used (fndecl);
1903 return build (OFFSET_REF, TREE_TYPE (fndecl), datum, fndecl);
1904 }
1905 if (access == access_protected_node)
1906 cp_error ("member function `%D' is protected", fndecl);
1907 else
1908 cp_error ("member function `%D' is private", fndecl);
1909 return error_mark_node;
1910 }
1911 else
1912 {
1913 /* Just act like build_offset_ref, since the object does
1914 not matter unless we're actually calling the function. */
1915 tree t;
1916
1917 t = build_tree_list (error_mark_node, fndecls);
1918 TREE_TYPE (t) = build_offset_type (basetype,
1919 unknown_type_node);
1920 return t;
1921 }
1922 }
1923
1924 cp_error ("`%#T' has no member named `%D'", basetype, name);
1925 return error_mark_node;
1926 }
1927 else if (TREE_TYPE (field) == error_mark_node)
1928 return error_mark_node;
1929
1930 if (TREE_CODE (field) != FIELD_DECL)
1931 {
1932 if (TREE_CODE (field) == TYPE_DECL)
1933 {
1934 cp_error ("invalid use of type decl `%#D' as expression", field);
1935 return error_mark_node;
1936 }
1937 else if (DECL_RTL (field) != 0)
1938 mark_used (field);
1939 else
1940 TREE_USED (field) = 1;
1941 return field;
1942 }
1943 }
1944
1945 /* See if we have to do any conversions so that we pick up the field from the
1946 right context. */
1947 if (DECL_FIELD_CONTEXT (field) != basetype)
1948 {
1949 tree context = DECL_FIELD_CONTEXT (field);
1950 tree base = context;
1951 while (base != basetype && TYPE_NAME (base)
1952 && ANON_AGGRNAME_P (TYPE_IDENTIFIER (base)))
1953 {
1954 base = TYPE_CONTEXT (base);
1955 }
1956
1957 /* Handle base classes here... */
1958 if (base != basetype && TYPE_USES_COMPLEX_INHERITANCE (basetype))
1959 {
1960 tree addr = build_unary_op (ADDR_EXPR, datum, 0);
1961 if (integer_zerop (addr))
1962 {
1963 error ("invalid reference to NULL ptr, use ptr-to-member instead");
1964 return error_mark_node;
1965 }
1966 if (VBASE_NAME_P (DECL_NAME (field)))
1967 {
1968 /* It doesn't matter which vbase pointer we grab, just
1969 find one of them. */
1970 tree binfo = get_binfo (base,
1971 TREE_TYPE (TREE_TYPE (addr)), 0);
1972 addr = convert_pointer_to_real (binfo, addr);
1973 }
1974 else
1975 addr = convert_pointer_to (base, addr);
1976 datum = build_indirect_ref (addr, NULL_PTR);
1977 my_friendly_assert (datum != error_mark_node, 311);
1978 }
1979 basetype = base;
1980
1981 /* Handle things from anon unions here... */
1982 if (TYPE_NAME (context) && ANON_AGGRNAME_P (TYPE_IDENTIFIER (context)))
1983 {
1984 tree subfield = lookup_anon_field (basetype, context);
1985 tree subdatum = build_component_ref (datum, subfield,
1986 basetype_path, protect);
1987 return build_component_ref (subdatum, field, basetype_path, protect);
1988 }
1989 }
1990
1991 ref = fold (build (COMPONENT_REF, TREE_TYPE (field),
1992 break_out_cleanups (datum), field));
1993
1994 if (TREE_READONLY (datum) || TREE_READONLY (field))
1995 TREE_READONLY (ref) = 1;
1996 if (TREE_THIS_VOLATILE (datum) || TREE_THIS_VOLATILE (field))
1997 TREE_THIS_VOLATILE (ref) = 1;
1998 if (DECL_MUTABLE_P (field))
1999 TREE_READONLY (ref) = 0;
2000
2001 return ref;
2002 }
2003
2004 /* Variant of build_component_ref for use in expressions, which should
2005 never have REFERENCE_TYPE. */
2006
2007 tree
2008 build_x_component_ref (datum, component, basetype_path, protect)
2009 tree datum, component, basetype_path;
2010 int protect;
2011 {
2012 tree t = build_component_ref (datum, component, basetype_path, protect);
2013
2014 if (! processing_template_decl)
2015 t = convert_from_reference (t);
2016
2017 return t;
2018 }
2019 \f
2020 /* Given an expression PTR for a pointer, return an expression
2021 for the value pointed to.
2022 ERRORSTRING is the name of the operator to appear in error messages.
2023
2024 This function may need to overload OPERATOR_FNNAME.
2025 Must also handle REFERENCE_TYPEs for C++. */
2026
2027 tree
2028 build_x_indirect_ref (ptr, errorstring)
2029 tree ptr;
2030 char *errorstring;
2031 {
2032 tree rval;
2033
2034 if (processing_template_decl)
2035 return build_min_nt (INDIRECT_REF, ptr);
2036
2037 rval = build_opfncall (INDIRECT_REF, LOOKUP_NORMAL, ptr, NULL_TREE, NULL_TREE);
2038 if (rval)
2039 return rval;
2040 return build_indirect_ref (ptr, errorstring);
2041 }
2042
2043 tree
2044 build_indirect_ref (ptr, errorstring)
2045 tree ptr;
2046 char *errorstring;
2047 {
2048 register tree pointer, type;
2049
2050 if (ptr == error_mark_node)
2051 return error_mark_node;
2052
2053 pointer = (TREE_CODE (TREE_TYPE (ptr)) == REFERENCE_TYPE
2054 ? ptr : default_conversion (ptr));
2055 type = TREE_TYPE (pointer);
2056
2057 if (ptr == current_class_ptr)
2058 return current_class_ref;
2059
2060 if (IS_AGGR_TYPE (type))
2061 {
2062 ptr = build_expr_type_conversion (WANT_POINTER, pointer, 1);
2063
2064 if (ptr)
2065 {
2066 pointer = ptr;
2067 type = TREE_TYPE (pointer);
2068 }
2069 }
2070
2071 if (TREE_CODE (type) == POINTER_TYPE || TREE_CODE (type) == REFERENCE_TYPE)
2072 {
2073 if (TREE_CODE (pointer) == ADDR_EXPR
2074 && !flag_volatile
2075 && (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_OPERAND (pointer, 0)))
2076 == TYPE_MAIN_VARIANT (TREE_TYPE (type)))
2077 && (TREE_READONLY (TREE_OPERAND (pointer, 0))
2078 == TYPE_READONLY (TREE_TYPE (type)))
2079 && (TREE_THIS_VOLATILE (TREE_OPERAND (pointer, 0))
2080 == TYPE_VOLATILE (TREE_TYPE (type))))
2081 return TREE_OPERAND (pointer, 0);
2082 else
2083 {
2084 tree t = TREE_TYPE (type);
2085 register tree ref = build1 (INDIRECT_REF,
2086 TYPE_MAIN_VARIANT (t), pointer);
2087
2088 /* We *must* set TREE_READONLY when dereferencing a pointer to const,
2089 so that we get the proper error message if the result is used
2090 to assign to. Also, &* is supposed to be a no-op. */
2091 TREE_READONLY (ref) = TYPE_READONLY (t);
2092 TREE_SIDE_EFFECTS (ref)
2093 = TYPE_VOLATILE (t) || TREE_SIDE_EFFECTS (pointer) || flag_volatile;
2094 TREE_THIS_VOLATILE (ref) = TYPE_VOLATILE (t);
2095 return ref;
2096 }
2097 }
2098 /* `pointer' won't be an error_mark_node if we were given a
2099 pointer to member, so it's cool to check for this here. */
2100 else if (TYPE_PTRMEMFUNC_P (type))
2101 error ("invalid use of `%s' on pointer to member function", errorstring);
2102 else if (TREE_CODE (type) == RECORD_TYPE
2103 && (IS_SIGNATURE_POINTER (type) || IS_SIGNATURE_REFERENCE (type)))
2104 error ("cannot dereference signature pointer/reference");
2105 else if (pointer != error_mark_node)
2106 {
2107 if (errorstring)
2108 error ("invalid type argument of `%s'", errorstring);
2109 else
2110 error ("invalid type argument");
2111 }
2112 return error_mark_node;
2113 }
2114
2115 /* This handles expressions of the form "a[i]", which denotes
2116 an array reference.
2117
2118 This is logically equivalent in C to *(a+i), but we may do it differently.
2119 If A is a variable or a member, we generate a primitive ARRAY_REF.
2120 This avoids forcing the array out of registers, and can work on
2121 arrays that are not lvalues (for example, members of structures returned
2122 by functions).
2123
2124 If INDEX is of some user-defined type, it must be converted to
2125 integer type. Otherwise, to make a compatible PLUS_EXPR, it
2126 will inherit the type of the array, which will be some pointer type. */
2127
2128 tree
2129 build_array_ref (array, idx)
2130 tree array, idx;
2131 {
2132 if (idx == 0)
2133 {
2134 error ("subscript missing in array reference");
2135 return error_mark_node;
2136 }
2137
2138 if (TREE_TYPE (array) == error_mark_node
2139 || TREE_TYPE (idx) == error_mark_node)
2140 return error_mark_node;
2141
2142 if (TREE_CODE (TREE_TYPE (array)) == ARRAY_TYPE
2143 && TREE_CODE (array) != INDIRECT_REF)
2144 {
2145 tree rval, type;
2146
2147 /* Subscripting with type char is likely to lose
2148 on a machine where chars are signed.
2149 So warn on any machine, but optionally.
2150 Don't warn for unsigned char since that type is safe.
2151 Don't warn for signed char because anyone who uses that
2152 must have done so deliberately. */
2153 if (warn_char_subscripts
2154 && TYPE_MAIN_VARIANT (TREE_TYPE (idx)) == char_type_node)
2155 warning ("array subscript has type `char'");
2156
2157 /* Apply default promotions *after* noticing character types. */
2158 idx = default_conversion (idx);
2159
2160 if (TREE_CODE (TREE_TYPE (idx)) != INTEGER_TYPE)
2161 {
2162 error ("array subscript is not an integer");
2163 return error_mark_node;
2164 }
2165
2166 /* An array that is indexed by a non-constant
2167 cannot be stored in a register; we must be able to do
2168 address arithmetic on its address.
2169 Likewise an array of elements of variable size. */
2170 if (TREE_CODE (idx) != INTEGER_CST
2171 || (TYPE_SIZE (TREE_TYPE (TREE_TYPE (array))) != 0
2172 && TREE_CODE (TYPE_SIZE (TREE_TYPE (TREE_TYPE (array)))) != INTEGER_CST))
2173 {
2174 if (mark_addressable (array) == 0)
2175 return error_mark_node;
2176 }
2177 /* An array that is indexed by a constant value which is not within
2178 the array bounds cannot be stored in a register either; because we
2179 would get a crash in store_bit_field/extract_bit_field when trying
2180 to access a non-existent part of the register. */
2181 if (TREE_CODE (idx) == INTEGER_CST
2182 && TYPE_VALUES (TREE_TYPE (array))
2183 && ! int_fits_type_p (idx, TYPE_VALUES (TREE_TYPE (array))))
2184 {
2185 if (mark_addressable (array) == 0)
2186 return error_mark_node;
2187 }
2188
2189 if (pedantic && !lvalue_p (array))
2190 pedwarn ("ANSI C++ forbids subscripting non-lvalue array");
2191
2192 /* Note in C++ it is valid to subscript a `register' array, since
2193 it is valid to take the address of something with that
2194 storage specification. */
2195 if (extra_warnings)
2196 {
2197 tree foo = array;
2198 while (TREE_CODE (foo) == COMPONENT_REF)
2199 foo = TREE_OPERAND (foo, 0);
2200 if (TREE_CODE (foo) == VAR_DECL && DECL_REGISTER (foo))
2201 warning ("subscripting array declared `register'");
2202 }
2203
2204 type = TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (array)));
2205 rval = build (ARRAY_REF, type, array, idx);
2206 /* Array ref is const/volatile if the array elements are
2207 or if the array is.. */
2208 TREE_READONLY (rval)
2209 |= (TYPE_READONLY (TREE_TYPE (TREE_TYPE (array)))
2210 | TREE_READONLY (array));
2211 TREE_SIDE_EFFECTS (rval)
2212 |= (TYPE_VOLATILE (TREE_TYPE (TREE_TYPE (array)))
2213 | TREE_SIDE_EFFECTS (array));
2214 TREE_THIS_VOLATILE (rval)
2215 |= (TYPE_VOLATILE (TREE_TYPE (TREE_TYPE (array)))
2216 /* This was added by rms on 16 Nov 91.
2217 It fixes vol struct foo *a; a->elts[1]
2218 in an inline function.
2219 Hope it doesn't break something else. */
2220 | TREE_THIS_VOLATILE (array));
2221 return require_complete_type (fold (rval));
2222 }
2223
2224 {
2225 tree ar = default_conversion (array);
2226 tree ind = default_conversion (idx);
2227
2228 /* Put the integer in IND to simplify error checking. */
2229 if (TREE_CODE (TREE_TYPE (ar)) == INTEGER_TYPE)
2230 {
2231 tree temp = ar;
2232 ar = ind;
2233 ind = temp;
2234 }
2235
2236 if (ar == error_mark_node)
2237 return ar;
2238
2239 if (TREE_CODE (TREE_TYPE (ar)) != POINTER_TYPE)
2240 {
2241 error ("subscripted value is neither array nor pointer");
2242 return error_mark_node;
2243 }
2244 if (TREE_CODE (TREE_TYPE (ind)) != INTEGER_TYPE)
2245 {
2246 error ("array subscript is not an integer");
2247 return error_mark_node;
2248 }
2249
2250 return build_indirect_ref (build_binary_op_nodefault (PLUS_EXPR, ar, ind, PLUS_EXPR),
2251 "array indexing");
2252 }
2253 }
2254 \f
2255 /* Build a function call to function FUNCTION with parameters PARAMS.
2256 PARAMS is a list--a chain of TREE_LIST nodes--in which the
2257 TREE_VALUE of each node is a parameter-expression. The PARAMS do
2258 not include any object pointer that may be required. FUNCTION's
2259 data type may be a function type or a pointer-to-function.
2260
2261 For C++: If FUNCTION's data type is a TREE_LIST, then the tree list
2262 is the list of possible methods that FUNCTION could conceivably
2263 be. If the list of methods comes from a class, then it will be
2264 a list of lists (where each element is associated with the class
2265 that produced it), otherwise it will be a simple list (for
2266 functions overloaded in global scope).
2267
2268 In the first case, TREE_VALUE (function) is the head of one of those
2269 lists, and TREE_PURPOSE is the name of the function.
2270
2271 In the second case, TREE_PURPOSE (function) is the function's
2272 name directly.
2273
2274 DECL is the class instance variable, usually CURRENT_CLASS_REF.
2275
2276 When calling a TEMPLATE_DECL, we don't require a complete return
2277 type. */
2278
2279 tree
2280 build_x_function_call (function, params, decl)
2281 tree function, params, decl;
2282 {
2283 tree type;
2284 tree template_id = NULL_TREE;
2285 int is_method;
2286
2287 if (function == error_mark_node)
2288 return error_mark_node;
2289
2290 if (processing_template_decl)
2291 return build_min_nt (CALL_EXPR, function, params, NULL_TREE);
2292
2293 /* Save explicit template arguments if found */
2294 if (TREE_CODE (function) == TEMPLATE_ID_EXPR)
2295 {
2296 template_id = function;
2297 function = TREE_OPERAND (function, 0);
2298 }
2299
2300 type = TREE_TYPE (function);
2301
2302 if (TREE_CODE (type) == OFFSET_TYPE
2303 && TREE_TYPE (type) == unknown_type_node
2304 && TREE_CODE (function) == TREE_LIST
2305 && TREE_CHAIN (function) == NULL_TREE)
2306 {
2307 /* Undo (Foo:bar)()... */
2308 type = TYPE_OFFSET_BASETYPE (type);
2309 function = TREE_VALUE (function);
2310 my_friendly_assert (TREE_CODE (function) == TREE_LIST, 999);
2311 my_friendly_assert (TREE_CHAIN (function) == NULL_TREE, 999);
2312 function = TREE_VALUE (function);
2313 my_friendly_assert (TREE_CODE (function) == FUNCTION_DECL, 999);
2314 function = DECL_NAME (function);
2315 return build_method_call (decl, function, params, TYPE_BINFO (type), LOOKUP_NORMAL);
2316 }
2317
2318 is_method = ((TREE_CODE (function) == TREE_LIST
2319 && current_class_type != NULL_TREE
2320 && IDENTIFIER_CLASS_VALUE (TREE_PURPOSE (function)) == function)
2321 || TREE_CODE (function) == IDENTIFIER_NODE
2322 || TREE_CODE (type) == METHOD_TYPE
2323 || TYPE_PTRMEMFUNC_P (type));
2324
2325 if (TREE_CODE (function) == FUNCTION_DECL
2326 && DECL_STATIC_FUNCTION_P (function))
2327 return build_member_call
2328 (DECL_CONTEXT (function), DECL_NAME (function), params);
2329
2330 /* Handle methods, friends, and overloaded functions, respectively. */
2331 if (is_method)
2332 {
2333 tree basetype = NULL_TREE;
2334
2335 if (TREE_CODE (function) == FUNCTION_DECL
2336 || DECL_FUNCTION_TEMPLATE_P (function))
2337 {
2338 basetype = DECL_CLASS_CONTEXT (function);
2339
2340 if (DECL_NAME (function))
2341 function = DECL_NAME (function);
2342 else
2343 function = TYPE_IDENTIFIER (DECL_CLASS_CONTEXT (function));
2344 }
2345 else if (TREE_CODE (function) == TREE_LIST)
2346 {
2347 my_friendly_assert (TREE_CODE (TREE_VALUE (function)) == FUNCTION_DECL, 312);
2348 basetype = DECL_CLASS_CONTEXT (TREE_VALUE (function));
2349 function = TREE_PURPOSE (function);
2350 }
2351 else if (TREE_CODE (function) != IDENTIFIER_NODE)
2352 {
2353 if (TREE_CODE (function) == OFFSET_REF)
2354 {
2355 if (TREE_OPERAND (function, 0))
2356 decl = TREE_OPERAND (function, 0);
2357 }
2358 /* Call via a pointer to member function. */
2359 if (decl == NULL_TREE)
2360 {
2361 error ("pointer to member function called, but not in class scope");
2362 return error_mark_node;
2363 }
2364 /* What other type of POINTER_TYPE could this be? */
2365 if (TREE_CODE (TREE_TYPE (function)) != POINTER_TYPE
2366 && ! TYPE_PTRMEMFUNC_P (TREE_TYPE (function))
2367 && TREE_CODE (function) != OFFSET_REF)
2368 function = build (OFFSET_REF, TREE_TYPE (type), NULL_TREE, function);
2369 goto do_x_function;
2370 }
2371
2372 /* this is an abbreviated method call.
2373 must go through here in case it is a virtual function.
2374 @@ Perhaps this could be optimized. */
2375
2376 if (basetype && (! current_class_type
2377 || ! DERIVED_FROM_P (basetype, current_class_type)))
2378 return build_member_call (basetype, function, params);
2379
2380 if (decl == NULL_TREE)
2381 {
2382 if (current_class_type == NULL_TREE)
2383 {
2384 error ("object missing in call to method `%s'",
2385 IDENTIFIER_POINTER (function));
2386 return error_mark_node;
2387 }
2388 /* Yow: call from a static member function. */
2389 decl = build1 (NOP_EXPR, build_pointer_type (current_class_type),
2390 error_mark_node);
2391 decl = build_indirect_ref (decl, NULL_PTR);
2392 }
2393
2394 /* Put back explicit template arguments, if any. */
2395 if (template_id)
2396 function = template_id;
2397 return build_method_call (decl, function, params,
2398 NULL_TREE, LOOKUP_NORMAL);
2399 }
2400 else if (TREE_CODE (function) == COMPONENT_REF
2401 && type == unknown_type_node)
2402 {
2403 /* Should we undo what was done in build_component_ref? */
2404 if (TREE_CODE (TREE_PURPOSE (TREE_OPERAND (function, 1))) == TREE_VEC)
2405 /* Get the name that build_component_ref hid. */
2406 function = DECL_NAME (TREE_VALUE (TREE_OPERAND (function, 1)));
2407 else
2408 function = TREE_PURPOSE (TREE_OPERAND (function, 1));
2409 return build_method_call (decl, function, params,
2410 NULL_TREE, LOOKUP_NORMAL);
2411 }
2412 else if (really_overloaded_fn (function))
2413 {
2414 if (TREE_VALUE (function) == NULL_TREE)
2415 {
2416 cp_error ("function `%D' declared overloaded, but no definitions appear with which to resolve it?!?",
2417 TREE_PURPOSE (function));
2418 return error_mark_node;
2419 }
2420 else
2421 {
2422 tree val = TREE_VALUE (function);
2423
2424 if (flag_ansi_overloading)
2425 {
2426 /* Put back explicit template arguments, if any. */
2427 if (template_id)
2428 function = template_id;
2429 return build_new_function_call (function, params);
2430 }
2431
2432 if (TREE_CODE (val) == TEMPLATE_DECL)
2433 return build_overload_call_real
2434 (function, params, LOOKUP_COMPLAIN, (struct candidate *)0, 0);
2435 else if (DECL_CHAIN (val) != NULL_TREE)
2436 return build_overload_call
2437 (function, params, LOOKUP_COMPLAIN);
2438 else
2439 my_friendly_abort (360);
2440 }
2441 }
2442
2443 do_x_function:
2444 if (TREE_CODE (function) == OFFSET_REF)
2445 {
2446 /* If the component is a data element (or a virtual function), we play
2447 games here to make things work. */
2448 tree decl_addr;
2449
2450 if (TREE_OPERAND (function, 0))
2451 decl = TREE_OPERAND (function, 0);
2452 else
2453 decl = current_class_ref;
2454
2455 decl_addr = build_unary_op (ADDR_EXPR, decl, 0);
2456 function = get_member_function_from_ptrfunc (&decl_addr,
2457 TREE_OPERAND (function, 1));
2458 params = expr_tree_cons (NULL_TREE, decl_addr, params);
2459 return build_function_call (function, params);
2460 }
2461
2462 type = TREE_TYPE (function);
2463 if (type != error_mark_node)
2464 {
2465 if (TREE_CODE (type) == REFERENCE_TYPE)
2466 type = TREE_TYPE (type);
2467
2468 if (IS_AGGR_TYPE (type))
2469 return build_opfncall (CALL_EXPR, LOOKUP_NORMAL, function, params, NULL_TREE);
2470 }
2471
2472 if (is_method)
2473 {
2474 tree fntype = TREE_TYPE (function);
2475 tree ctypeptr;
2476
2477 /* Explicitly named method? */
2478 if (TREE_CODE (function) == FUNCTION_DECL)
2479 ctypeptr = build_pointer_type (DECL_CLASS_CONTEXT (function));
2480 /* Expression with ptr-to-method type? It could either be a plain
2481 usage, or it might be a case where the ptr-to-method is being
2482 passed in as an argument. */
2483 else if (TYPE_PTRMEMFUNC_P (fntype))
2484 {
2485 tree rec = TYPE_METHOD_BASETYPE (TREE_TYPE (TYPE_PTRMEMFUNC_FN_TYPE (fntype)));
2486 ctypeptr = build_pointer_type (rec);
2487 }
2488 /* Unexpected node type? */
2489 else
2490 my_friendly_abort (116);
2491 if (decl == NULL_TREE)
2492 {
2493 if (current_function_decl
2494 && DECL_STATIC_FUNCTION_P (current_function_decl))
2495 error ("invalid call to member function needing `this' in static member function scope");
2496 else
2497 error ("pointer to member function called, but not in class scope");
2498 return error_mark_node;
2499 }
2500 if (TREE_CODE (TREE_TYPE (decl)) != POINTER_TYPE
2501 && ! TYPE_PTRMEMFUNC_P (TREE_TYPE (decl)))
2502 {
2503 decl = build_unary_op (ADDR_EXPR, decl, 0);
2504 decl = convert_pointer_to (TREE_TYPE (ctypeptr), decl);
2505 }
2506 else
2507 decl = build_c_cast (ctypeptr, decl);
2508 params = expr_tree_cons (NULL_TREE, decl, params);
2509 }
2510
2511 return build_function_call (function, params);
2512 }
2513
2514 /* Resolve a pointer to member function. INSTANCE is the object
2515 instance to use, if the member points to a virtual member. */
2516
2517 tree
2518 get_member_function_from_ptrfunc (instance_ptrptr, function)
2519 tree *instance_ptrptr;
2520 tree function;
2521 {
2522 if (TREE_CODE (function) == OFFSET_REF)
2523 {
2524 function = TREE_OPERAND (function, 1);
2525 }
2526
2527 if (TYPE_PTRMEMFUNC_P (TREE_TYPE (function)))
2528 {
2529 tree fntype, idx, e1, delta, delta2, e2, e3, aref, vtbl;
2530 tree instance;
2531
2532 tree instance_ptr = *instance_ptrptr;
2533
2534 if (TREE_SIDE_EFFECTS (instance_ptr))
2535 instance_ptr = save_expr (instance_ptr);
2536
2537 if (TREE_SIDE_EFFECTS (function))
2538 function = save_expr (function);
2539
2540 fntype = TYPE_PTRMEMFUNC_FN_TYPE (TREE_TYPE (function));
2541
2542 /* Promoting idx before saving it improves performance on RISC
2543 targets. Without promoting, the first compare used
2544 load-with-sign-extend, while the second used normal load then
2545 shift to sign-extend. An optimizer flaw, perhaps, but it's easier
2546 to make this change. */
2547 idx = save_expr (default_conversion
2548 (build_component_ref (function,
2549 index_identifier,
2550 NULL_TREE, 0)));
2551 e1 = build_binary_op (GT_EXPR, idx, integer_zero_node, 1);
2552 delta = cp_convert (ptrdiff_type_node,
2553 build_component_ref (function, delta_identifier, NULL_TREE, 0));
2554 delta2 = DELTA2_FROM_PTRMEMFUNC (function);
2555
2556 /* Convert down to the right base, before using the instance. */
2557 instance
2558 = convert_pointer_to_real (TYPE_METHOD_BASETYPE (TREE_TYPE (fntype)),
2559 instance_ptr);
2560 if (instance == error_mark_node && instance_ptr != error_mark_node)
2561 return instance;
2562
2563 vtbl = convert_pointer_to (ptr_type_node, instance);
2564 vtbl
2565 = build (PLUS_EXPR,
2566 build_pointer_type (build_pointer_type (vtable_entry_type)),
2567 vtbl, cp_convert (ptrdiff_type_node, delta2));
2568 vtbl = build_indirect_ref (vtbl, NULL_PTR);
2569 aref = build_array_ref (vtbl, build_binary_op (MINUS_EXPR,
2570 idx,
2571 integer_one_node, 1));
2572 if (! flag_vtable_thunks)
2573 {
2574 aref = save_expr (aref);
2575
2576 delta = build_binary_op (PLUS_EXPR,
2577 build_conditional_expr (e1, build_component_ref (aref, delta_identifier, NULL_TREE, 0), integer_zero_node),
2578 delta, 1);
2579 }
2580
2581 *instance_ptrptr = build (PLUS_EXPR, TREE_TYPE (instance_ptr),
2582 instance_ptr, delta);
2583 if (flag_vtable_thunks)
2584 e2 = aref;
2585 else
2586 e2 = build_component_ref (aref, pfn_identifier, NULL_TREE, 0);
2587
2588 e3 = PFN_FROM_PTRMEMFUNC (function);
2589 TREE_TYPE (e2) = TREE_TYPE (e3);
2590 e1 = build_conditional_expr (e1, e2, e3);
2591
2592 if (instance_ptr == error_mark_node
2593 && TREE_CODE (e1) != ADDR_EXPR
2594 && TREE_CODE (TREE_OPERAND (e1, 0)) != FUNCTION_DECL)
2595 cp_error ("object missing in `%E'", function);
2596
2597 function = e1;
2598
2599 /* Make sure this doesn't get evaluated first inside one of the
2600 branches of the COND_EXPR. */
2601 if (TREE_CODE (instance_ptr) == SAVE_EXPR)
2602 function = build (COMPOUND_EXPR, TREE_TYPE (function),
2603 instance_ptr, function);
2604 }
2605 return function;
2606 }
2607
2608 tree
2609 build_function_call_real (function, params, require_complete, flags)
2610 tree function, params;
2611 int require_complete, flags;
2612 {
2613 register tree fntype, fndecl;
2614 register tree value_type;
2615 register tree coerced_params;
2616 tree name = NULL_TREE, assembler_name = NULL_TREE;
2617 int is_method;
2618
2619 /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
2620 Strip such NOP_EXPRs, since FUNCTION is used in non-lvalue context. */
2621 if (TREE_CODE (function) == NOP_EXPR
2622 && TREE_TYPE (function) == TREE_TYPE (TREE_OPERAND (function, 0)))
2623 function = TREE_OPERAND (function, 0);
2624
2625 if (TREE_CODE (function) == FUNCTION_DECL)
2626 {
2627 name = DECL_NAME (function);
2628 assembler_name = DECL_ASSEMBLER_NAME (function);
2629
2630 GNU_xref_call (current_function_decl,
2631 IDENTIFIER_POINTER (name ? name
2632 : TYPE_IDENTIFIER (DECL_CLASS_CONTEXT (function))));
2633 mark_used (function);
2634 fndecl = function;
2635
2636 /* Convert anything with function type to a pointer-to-function. */
2637 if (pedantic
2638 && name
2639 && IDENTIFIER_LENGTH (name) == 4
2640 && ! strcmp (IDENTIFIER_POINTER (name), "main")
2641 && DECL_CONTEXT (function) == NULL_TREE)
2642 {
2643 pedwarn ("ANSI C++ forbids calling `main' from within program");
2644 }
2645
2646 if (pedantic && DECL_THIS_INLINE (function) && ! DECL_INITIAL (function)
2647 && ! DECL_ARTIFICIAL (function)
2648 && ! DECL_PENDING_INLINE_INFO (function))
2649 cp_pedwarn ("inline function `%#D' called before definition",
2650 function);
2651
2652 /* Differs from default_conversion by not setting TREE_ADDRESSABLE
2653 (because calling an inline function does not mean the function
2654 needs to be separately compiled). */
2655
2656 if (DECL_INLINE (function))
2657 function = inline_conversion (function);
2658 else
2659 function = build_addr_func (function);
2660 }
2661 else
2662 {
2663 fndecl = NULL_TREE;
2664
2665 function = build_addr_func (function);
2666 }
2667
2668 if (function == error_mark_node)
2669 return error_mark_node;
2670
2671 fntype = TREE_TYPE (function);
2672
2673 if (TYPE_PTRMEMFUNC_P (fntype))
2674 {
2675 cp_error ("must use .* or ->* to call pointer-to-member function in `%E (...)'",
2676 function);
2677 return error_mark_node;
2678 }
2679
2680 is_method = (TREE_CODE (fntype) == POINTER_TYPE
2681 && TREE_CODE (TREE_TYPE (fntype)) == METHOD_TYPE);
2682
2683 if (!((TREE_CODE (fntype) == POINTER_TYPE
2684 && TREE_CODE (TREE_TYPE (fntype)) == FUNCTION_TYPE)
2685 || is_method
2686 || TREE_CODE (function) == TEMPLATE_ID_EXPR))
2687 {
2688 cp_error ("`%E' cannot be used as a function", function);
2689 return error_mark_node;
2690 }
2691
2692 /* fntype now gets the type of function pointed to. */
2693 fntype = TREE_TYPE (fntype);
2694
2695 /* Convert the parameters to the types declared in the
2696 function prototype, or apply default promotions. */
2697
2698 if (flags & LOOKUP_COMPLAIN)
2699 coerced_params = convert_arguments (NULL_TREE, TYPE_ARG_TYPES (fntype),
2700 params, fndecl, LOOKUP_NORMAL);
2701 else
2702 coerced_params = convert_arguments (NULL_TREE, TYPE_ARG_TYPES (fntype),
2703 params, fndecl, 0);
2704
2705 if (coerced_params == error_mark_node)
2706 if (flags & LOOKUP_SPECULATIVELY)
2707 return NULL_TREE;
2708 else
2709 return error_mark_node;
2710
2711 /* Check for errors in format strings. */
2712
2713 if (warn_format && (name || assembler_name))
2714 check_function_format (name, assembler_name, coerced_params);
2715
2716 /* Recognize certain built-in functions so we can make tree-codes
2717 other than CALL_EXPR. We do this when it enables fold-const.c
2718 to do something useful. */
2719
2720 if (TREE_CODE (function) == ADDR_EXPR
2721 && TREE_CODE (TREE_OPERAND (function, 0)) == FUNCTION_DECL
2722 && DECL_BUILT_IN (TREE_OPERAND (function, 0)))
2723 switch (DECL_FUNCTION_CODE (TREE_OPERAND (function, 0)))
2724 {
2725 case BUILT_IN_ABS:
2726 case BUILT_IN_LABS:
2727 case BUILT_IN_FABS:
2728 if (coerced_params == 0)
2729 return integer_zero_node;
2730 return build_unary_op (ABS_EXPR, TREE_VALUE (coerced_params), 0);
2731 }
2732
2733 /* C++ */
2734 value_type = TREE_TYPE (fntype) ? TREE_TYPE (fntype) : void_type_node;
2735 {
2736 register tree result
2737 = build_call (function, value_type, coerced_params);
2738
2739 if (require_complete)
2740 {
2741 if (value_type == void_type_node)
2742 return result;
2743 result = require_complete_type (result);
2744 }
2745 if (IS_AGGR_TYPE (value_type))
2746 result = build_cplus_new (value_type, result);
2747 return convert_from_reference (result);
2748 }
2749 }
2750
2751 tree
2752 build_function_call (function, params)
2753 tree function, params;
2754 {
2755 return build_function_call_real (function, params, 1, LOOKUP_NORMAL);
2756 }
2757 \f
2758 /* Convert the actual parameter expressions in the list VALUES
2759 to the types in the list TYPELIST.
2760 If parmdecls is exhausted, or when an element has NULL as its type,
2761 perform the default conversions.
2762
2763 RETURN_LOC is the location of the return value, if known, NULL_TREE
2764 otherwise. This is useful in the case where we can avoid creating
2765 a temporary variable in the case where we can initialize the return
2766 value directly. If we are not eliding constructors, then we set this
2767 to NULL_TREE to avoid this avoidance.
2768
2769 NAME is an IDENTIFIER_NODE or 0. It is used only for error messages.
2770
2771 This is also where warnings about wrong number of args are generated.
2772
2773 Return a list of expressions for the parameters as converted.
2774
2775 Both VALUES and the returned value are chains of TREE_LIST nodes
2776 with the elements of the list in the TREE_VALUE slots of those nodes.
2777
2778 In C++, unspecified trailing parameters can be filled in with their
2779 default arguments, if such were specified. Do so here. */
2780
2781 tree
2782 convert_arguments (return_loc, typelist, values, fndecl, flags)
2783 tree return_loc, typelist, values, fndecl;
2784 int flags;
2785 {
2786 register tree typetail, valtail;
2787 register tree result = NULL_TREE;
2788 char *called_thing;
2789 int i = 0;
2790
2791 if (! flag_elide_constructors)
2792 return_loc = 0;
2793
2794 /* Argument passing is always copy-initialization. */
2795 flags |= LOOKUP_ONLYCONVERTING;
2796
2797 if (fndecl)
2798 {
2799 if (TREE_CODE (TREE_TYPE (fndecl)) == METHOD_TYPE)
2800 {
2801 if (DECL_NAME (fndecl) == NULL_TREE
2802 || IDENTIFIER_HAS_TYPE_VALUE (DECL_NAME (fndecl)))
2803 called_thing = "constructor";
2804 else
2805 called_thing = "member function";
2806 }
2807 else
2808 called_thing = "function";
2809 }
2810
2811 for (valtail = values, typetail = typelist;
2812 valtail;
2813 valtail = TREE_CHAIN (valtail), i++)
2814 {
2815 register tree type = typetail ? TREE_VALUE (typetail) : 0;
2816 register tree val = TREE_VALUE (valtail);
2817
2818 if (val == error_mark_node)
2819 return error_mark_node;
2820
2821 if (type == void_type_node)
2822 {
2823 if (fndecl)
2824 {
2825 cp_error_at ("too many arguments to %s `%+D'", called_thing,
2826 fndecl);
2827 error ("at this point in file");
2828 }
2829 else
2830 error ("too many arguments to function");
2831 /* In case anybody wants to know if this argument
2832 list is valid. */
2833 if (result)
2834 TREE_TYPE (tree_last (result)) = error_mark_node;
2835 break;
2836 }
2837
2838 /* The tree type of the parameter being passed may not yet be
2839 known. In this case, its type is TYPE_UNKNOWN, and will
2840 be instantiated by the type given by TYPE. If TYPE
2841 is also NULL, the tree type of VAL is ERROR_MARK_NODE. */
2842 if (type && type_unknown_p (val))
2843 val = require_instantiated_type (type, val, integer_zero_node);
2844 else if (type_unknown_p (val))
2845 {
2846 /* Strip the `&' from an overloaded FUNCTION_DECL. */
2847 if (TREE_CODE (val) == ADDR_EXPR)
2848 val = TREE_OPERAND (val, 0);
2849 if (really_overloaded_fn (val))
2850 cp_error ("insufficient type information to resolve address of overloaded function `%D'",
2851 DECL_NAME (get_first_fn (val)));
2852 else
2853 error ("insufficient type information in parameter list");
2854 val = integer_zero_node;
2855 }
2856 else if (TREE_CODE (val) == OFFSET_REF
2857 && TREE_CODE (TREE_TYPE (val)) == METHOD_TYPE)
2858 {
2859 /* This is unclean. Should be handled elsewhere. */
2860 val = build_unary_op (ADDR_EXPR, val, 0);
2861 }
2862 else if (TREE_CODE (val) == OFFSET_REF)
2863 val = resolve_offset_ref (val);
2864
2865 /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
2866 Strip such NOP_EXPRs, since VAL is used in non-lvalue context. */
2867 if (TREE_CODE (val) == NOP_EXPR
2868 && TREE_TYPE (val) == TREE_TYPE (TREE_OPERAND (val, 0))
2869 && (type == 0 || TREE_CODE (type) != REFERENCE_TYPE))
2870 val = TREE_OPERAND (val, 0);
2871
2872 if (type == 0 || TREE_CODE (type) != REFERENCE_TYPE)
2873 {
2874 if (TREE_CODE (TREE_TYPE (val)) == ARRAY_TYPE
2875 || TREE_CODE (TREE_TYPE (val)) == FUNCTION_TYPE
2876 || TREE_CODE (TREE_TYPE (val)) == METHOD_TYPE)
2877 val = default_conversion (val);
2878
2879 val = require_complete_type (val);
2880 }
2881
2882 if (val == error_mark_node)
2883 return error_mark_node;
2884
2885 if (type != 0)
2886 {
2887 /* Formal parm type is specified by a function prototype. */
2888 tree parmval;
2889
2890 if (TYPE_SIZE (complete_type (type)) == 0)
2891 {
2892 error ("parameter type of called function is incomplete");
2893 parmval = val;
2894 }
2895 else
2896 {
2897 parmval = convert_for_initialization
2898 (return_loc, type, val, flags,
2899 "argument passing", fndecl, i);
2900 #ifdef PROMOTE_PROTOTYPES
2901 if ((TREE_CODE (type) == INTEGER_TYPE
2902 || TREE_CODE (type) == ENUMERAL_TYPE)
2903 && (TYPE_PRECISION (type) < TYPE_PRECISION (integer_type_node)))
2904 parmval = default_conversion (parmval);
2905 #endif
2906 }
2907
2908 if (parmval == error_mark_node)
2909 return error_mark_node;
2910
2911 result = expr_tree_cons (NULL_TREE, parmval, result);
2912 }
2913 else
2914 {
2915 if (TREE_CODE (TREE_TYPE (val)) == REFERENCE_TYPE)
2916 val = convert_from_reference (val);
2917
2918 if (TREE_CODE (TREE_TYPE (val)) == REAL_TYPE
2919 && (TYPE_PRECISION (TREE_TYPE (val))
2920 < TYPE_PRECISION (double_type_node)))
2921 /* Convert `float' to `double'. */
2922 result = expr_tree_cons (NULL_TREE, cp_convert (double_type_node, val), result);
2923 else if (TYPE_LANG_SPECIFIC (TREE_TYPE (val))
2924 && ! TYPE_HAS_TRIVIAL_INIT_REF (TREE_TYPE (val)))
2925 {
2926 cp_warning ("cannot pass objects of type `%T' through `...'",
2927 TREE_TYPE (val));
2928 result = expr_tree_cons (NULL_TREE, val, result);
2929 }
2930 else
2931 /* Convert `short' and `char' to full-size `int'. */
2932 result = expr_tree_cons (NULL_TREE, default_conversion (val), result);
2933 }
2934
2935 if (typetail)
2936 typetail = TREE_CHAIN (typetail);
2937 }
2938
2939 if (typetail != 0 && typetail != void_list_node)
2940 {
2941 /* See if there are default arguments that can be used */
2942 if (TREE_PURPOSE (typetail))
2943 {
2944 for (; typetail != void_list_node; ++i)
2945 {
2946 tree type = TREE_VALUE (typetail);
2947 tree val = break_out_target_exprs (TREE_PURPOSE (typetail));
2948 tree parmval;
2949
2950 if (val == NULL_TREE)
2951 parmval = error_mark_node;
2952 else if (TREE_CODE (val) == CONSTRUCTOR)
2953 {
2954 parmval = digest_init (type, val, (tree *)0);
2955 parmval = convert_for_initialization (return_loc, type, parmval, flags,
2956 "default constructor", fndecl, i);
2957 }
2958 else
2959 {
2960 /* This could get clobbered by the following call. */
2961 if (TREE_HAS_CONSTRUCTOR (val))
2962 val = copy_node (val);
2963
2964 parmval = convert_for_initialization (return_loc, type, val, flags,
2965 "default argument", fndecl, i);
2966 #ifdef PROMOTE_PROTOTYPES
2967 if ((TREE_CODE (type) == INTEGER_TYPE
2968 || TREE_CODE (type) == ENUMERAL_TYPE)
2969 && (TYPE_PRECISION (type) < TYPE_PRECISION (integer_type_node)))
2970 parmval = default_conversion (parmval);
2971 #endif
2972 }
2973
2974 if (parmval == error_mark_node)
2975 return error_mark_node;
2976
2977 result = expr_tree_cons (0, parmval, result);
2978 typetail = TREE_CHAIN (typetail);
2979 /* ends with `...'. */
2980 if (typetail == NULL_TREE)
2981 break;
2982 }
2983 }
2984 else
2985 {
2986 if (fndecl)
2987 {
2988 char *buf = (char *)alloca (32 + strlen (called_thing));
2989 sprintf (buf, "too few arguments to %s `%%#D'", called_thing);
2990 cp_error_at (buf, fndecl);
2991 error ("at this point in file");
2992 }
2993 else
2994 error ("too few arguments to function");
2995 return error_mark_list;
2996 }
2997 }
2998
2999 return nreverse (result);
3000 }
3001 \f
3002 /* Build a binary-operation expression, after performing default
3003 conversions on the operands. CODE is the kind of expression to build. */
3004
3005 tree
3006 build_x_binary_op (code, arg1, arg2)
3007 enum tree_code code;
3008 tree arg1, arg2;
3009 {
3010 tree rval;
3011
3012 if (processing_template_decl)
3013 return build_min_nt (code, arg1, arg2);
3014
3015 if (flag_ansi_overloading)
3016 return build_new_op (code, LOOKUP_NORMAL, arg1, arg2, NULL_TREE);
3017
3018 rval = build_opfncall (code, LOOKUP_SPECULATIVELY,
3019 arg1, arg2, NULL_TREE);
3020 if (rval)
3021 return build_opfncall (code, LOOKUP_NORMAL, arg1, arg2, NULL_TREE);
3022 if (code == MEMBER_REF)
3023 return build_m_component_ref (build_indirect_ref (arg1, NULL_PTR),
3024 arg2);
3025 return build_binary_op (code, arg1, arg2, 1);
3026 }
3027
3028 tree
3029 build_binary_op (code, arg1, arg2, convert_p)
3030 enum tree_code code;
3031 tree arg1, arg2;
3032 int convert_p;
3033 {
3034 tree args[2];
3035
3036 args[0] = arg1;
3037 args[1] = arg2;
3038
3039 if (convert_p)
3040 {
3041 tree type0, type1;
3042 args[0] = decay_conversion (args[0]);
3043 args[1] = decay_conversion (args[1]);
3044
3045 if (args[0] == error_mark_node || args[1] == error_mark_node)
3046 return error_mark_node;
3047
3048 type0 = TREE_TYPE (args[0]);
3049 type1 = TREE_TYPE (args[1]);
3050
3051 if (type_unknown_p (args[0]))
3052 {
3053 args[0] = instantiate_type (type1, args[0], 1);
3054 args[0] = decay_conversion (args[0]);
3055 }
3056 else if (type_unknown_p (args[1]))
3057 {
3058 args[1] = require_instantiated_type (type0, args[1],
3059 error_mark_node);
3060 args[1] = decay_conversion (args[1]);
3061 }
3062
3063 if (IS_AGGR_TYPE (type0) || IS_AGGR_TYPE (type1))
3064 {
3065 /* Try to convert this to something reasonable. */
3066 if (! build_default_binary_type_conversion(code, &args[0], &args[1]))
3067 {
3068 cp_error ("no match for `%O(%#T, %#T)'", code,
3069 TREE_TYPE (arg1), TREE_TYPE (arg2));
3070 return error_mark_node;
3071 }
3072 }
3073 }
3074 return build_binary_op_nodefault (code, args[0], args[1], code);
3075 }
3076
3077 /* Build a binary-operation expression without default conversions.
3078 CODE is the kind of expression to build.
3079 This function differs from `build' in several ways:
3080 the data type of the result is computed and recorded in it,
3081 warnings are generated if arg data types are invalid,
3082 special handling for addition and subtraction of pointers is known,
3083 and some optimization is done (operations on narrow ints
3084 are done in the narrower type when that gives the same result).
3085 Constant folding is also done before the result is returned.
3086
3087 ERROR_CODE is the code that determines what to say in error messages.
3088 It is usually, but not always, the same as CODE.
3089
3090 Note that the operands will never have enumeral types
3091 because either they have just had the default conversions performed
3092 or they have both just been converted to some other type in which
3093 the arithmetic is to be done.
3094
3095 C++: must do special pointer arithmetic when implementing
3096 multiple inheritance, and deal with pointer to member functions. */
3097
3098 tree
3099 build_binary_op_nodefault (code, orig_op0, orig_op1, error_code)
3100 enum tree_code code;
3101 tree orig_op0, orig_op1;
3102 enum tree_code error_code;
3103 {
3104 tree op0, op1;
3105 register enum tree_code code0, code1;
3106 tree type0, type1;
3107
3108 /* Expression code to give to the expression when it is built.
3109 Normally this is CODE, which is what the caller asked for,
3110 but in some special cases we change it. */
3111 register enum tree_code resultcode = code;
3112
3113 /* Data type in which the computation is to be performed.
3114 In the simplest cases this is the common type of the arguments. */
3115 register tree result_type = NULL;
3116
3117 /* Nonzero means operands have already been type-converted
3118 in whatever way is necessary.
3119 Zero means they need to be converted to RESULT_TYPE. */
3120 int converted = 0;
3121
3122 /* Nonzero means create the expression with this type, rather than
3123 RESULT_TYPE. */
3124 tree build_type = 0;
3125
3126 /* Nonzero means after finally constructing the expression
3127 convert it to this type. */
3128 tree final_type = 0;
3129
3130 /* Nonzero if this is an operation like MIN or MAX which can
3131 safely be computed in short if both args are promoted shorts.
3132 Also implies COMMON.
3133 -1 indicates a bitwise operation; this makes a difference
3134 in the exact conditions for when it is safe to do the operation
3135 in a narrower mode. */
3136 int shorten = 0;
3137
3138 /* Nonzero if this is a comparison operation;
3139 if both args are promoted shorts, compare the original shorts.
3140 Also implies COMMON. */
3141 int short_compare = 0;
3142
3143 /* Nonzero if this is a right-shift operation, which can be computed on the
3144 original short and then promoted if the operand is a promoted short. */
3145 int short_shift = 0;
3146
3147 /* Nonzero means set RESULT_TYPE to the common type of the args. */
3148 int common = 0;
3149
3150 /* Apply default conversions. */
3151 if (code == TRUTH_AND_EXPR || code == TRUTH_ANDIF_EXPR
3152 || code == TRUTH_OR_EXPR || code == TRUTH_ORIF_EXPR
3153 || code == TRUTH_XOR_EXPR)
3154 {
3155 op0 = decay_conversion (orig_op0);
3156 op1 = decay_conversion (orig_op1);
3157 }
3158 else
3159 {
3160 op0 = default_conversion (orig_op0);
3161 op1 = default_conversion (orig_op1);
3162 }
3163
3164 type0 = TREE_TYPE (op0);
3165 type1 = TREE_TYPE (op1);
3166
3167 /* The expression codes of the data types of the arguments tell us
3168 whether the arguments are integers, floating, pointers, etc. */
3169 code0 = TREE_CODE (type0);
3170 code1 = TREE_CODE (type1);
3171
3172 /* Strip NON_LVALUE_EXPRs, etc., since we aren't using as an lvalue. */
3173 STRIP_TYPE_NOPS (op0);
3174 STRIP_TYPE_NOPS (op1);
3175
3176 /* If an error was already reported for one of the arguments,
3177 avoid reporting another error. */
3178
3179 if (code0 == ERROR_MARK || code1 == ERROR_MARK)
3180 return error_mark_node;
3181
3182 switch (code)
3183 {
3184 case PLUS_EXPR:
3185 /* Handle the pointer + int case. */
3186 if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
3187 return pointer_int_sum (PLUS_EXPR, op0, op1);
3188 else if (code1 == POINTER_TYPE && code0 == INTEGER_TYPE)
3189 return pointer_int_sum (PLUS_EXPR, op1, op0);
3190 else
3191 common = 1;
3192 break;
3193
3194 case MINUS_EXPR:
3195 /* Subtraction of two similar pointers.
3196 We must subtract them as integers, then divide by object size. */
3197 if (code0 == POINTER_TYPE && code1 == POINTER_TYPE
3198 && comp_target_types (type0, type1, 1))
3199 return pointer_diff (op0, op1);
3200 /* Handle pointer minus int. Just like pointer plus int. */
3201 else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
3202 return pointer_int_sum (MINUS_EXPR, op0, op1);
3203 else
3204 common = 1;
3205 break;
3206
3207 case MULT_EXPR:
3208 common = 1;
3209 break;
3210
3211 case TRUNC_DIV_EXPR:
3212 case CEIL_DIV_EXPR:
3213 case FLOOR_DIV_EXPR:
3214 case ROUND_DIV_EXPR:
3215 case EXACT_DIV_EXPR:
3216 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE
3217 || code0 == COMPLEX_TYPE)
3218 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
3219 || code1 == COMPLEX_TYPE))
3220 {
3221 if (TREE_CODE (op1) == INTEGER_CST && integer_zerop (op1))
3222 cp_warning ("division by zero in `%E / 0'", op0);
3223 else if (TREE_CODE (op1) == REAL_CST && real_zerop (op1))
3224 cp_warning ("division by zero in `%E / 0.'", op0);
3225
3226 if (!(code0 == INTEGER_TYPE && code1 == INTEGER_TYPE))
3227 resultcode = RDIV_EXPR;
3228 else
3229 /* When dividing two signed integers, we have to promote to int.
3230 unless we divide by a constant != -1. Note that default
3231 conversion will have been performed on the operands at this
3232 point, so we have to dig out the original type to find out if
3233 it was unsigned. */
3234 shorten = ((TREE_CODE (op0) == NOP_EXPR
3235 && TREE_UNSIGNED (TREE_TYPE (TREE_OPERAND (op0, 0))))
3236 || (TREE_CODE (op1) == INTEGER_CST
3237 && (TREE_INT_CST_LOW (op1) != -1
3238 || TREE_INT_CST_HIGH (op1) != -1)));
3239 common = 1;
3240 }
3241 break;
3242
3243 case BIT_AND_EXPR:
3244 case BIT_ANDTC_EXPR:
3245 case BIT_IOR_EXPR:
3246 case BIT_XOR_EXPR:
3247 if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
3248 shorten = -1;
3249 /* If one operand is a constant, and the other is a short type
3250 that has been converted to an int,
3251 really do the work in the short type and then convert the
3252 result to int. If we are lucky, the constant will be 0 or 1
3253 in the short type, making the entire operation go away. */
3254 if (TREE_CODE (op0) == INTEGER_CST
3255 && TREE_CODE (op1) == NOP_EXPR
3256 && TYPE_PRECISION (type1) > TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (op1, 0)))
3257 && TREE_UNSIGNED (TREE_TYPE (TREE_OPERAND (op1, 0))))
3258 {
3259 final_type = result_type;
3260 op1 = TREE_OPERAND (op1, 0);
3261 result_type = TREE_TYPE (op1);
3262 }
3263 if (TREE_CODE (op1) == INTEGER_CST
3264 && TREE_CODE (op0) == NOP_EXPR
3265 && TYPE_PRECISION (type0) > TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (op0, 0)))
3266 && TREE_UNSIGNED (TREE_TYPE (TREE_OPERAND (op0, 0))))
3267 {
3268 final_type = result_type;
3269 op0 = TREE_OPERAND (op0, 0);
3270 result_type = TREE_TYPE (op0);
3271 }
3272 break;
3273
3274 case TRUNC_MOD_EXPR:
3275 case FLOOR_MOD_EXPR:
3276 if (code1 == INTEGER_TYPE && integer_zerop (op1))
3277 cp_warning ("division by zero in `%E % 0'", op0);
3278 else if (code1 == REAL_TYPE && real_zerop (op1))
3279 cp_warning ("division by zero in `%E % 0.'", op0);
3280
3281 if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
3282 {
3283 /* Although it would be tempting to shorten always here, that loses
3284 on some targets, since the modulo instruction is undefined if the
3285 quotient can't be represented in the computation mode. We shorten
3286 only if unsigned or if dividing by something we know != -1. */
3287 shorten = ((TREE_CODE (op0) == NOP_EXPR
3288 && TREE_UNSIGNED (TREE_TYPE (TREE_OPERAND (op0, 0))))
3289 || (TREE_CODE (op1) == INTEGER_CST
3290 && (TREE_INT_CST_LOW (op1) != -1
3291 || TREE_INT_CST_HIGH (op1) != -1)));
3292 common = 1;
3293 }
3294 break;
3295
3296 case TRUTH_ANDIF_EXPR:
3297 case TRUTH_ORIF_EXPR:
3298 case TRUTH_AND_EXPR:
3299 case TRUTH_OR_EXPR:
3300 result_type = boolean_type_node;
3301 break;
3302
3303 /* Shift operations: result has same type as first operand;
3304 always convert second operand to int.
3305 Also set SHORT_SHIFT if shifting rightward. */
3306
3307 case RSHIFT_EXPR:
3308 if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
3309 {
3310 result_type = type0;
3311 if (TREE_CODE (op1) == INTEGER_CST)
3312 {
3313 if (tree_int_cst_lt (op1, integer_zero_node))
3314 warning ("right shift count is negative");
3315 else
3316 {
3317 if (TREE_INT_CST_LOW (op1) | TREE_INT_CST_HIGH (op1))
3318 short_shift = 1;
3319 if (TREE_INT_CST_HIGH (op1) != 0
3320 || ((unsigned HOST_WIDE_INT) TREE_INT_CST_LOW (op1)
3321 >= TYPE_PRECISION (type0)))
3322 warning ("right shift count >= width of type");
3323 }
3324 }
3325 /* Convert the shift-count to an integer, regardless of
3326 size of value being shifted. */
3327 if (TYPE_MAIN_VARIANT (TREE_TYPE (op1)) != integer_type_node)
3328 op1 = cp_convert (integer_type_node, op1);
3329 /* Avoid converting op1 to result_type later. */
3330 converted = 1;
3331 }
3332 break;
3333
3334 case LSHIFT_EXPR:
3335 if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
3336 {
3337 result_type = type0;
3338 if (TREE_CODE (op1) == INTEGER_CST)
3339 {
3340 if (tree_int_cst_lt (op1, integer_zero_node))
3341 warning ("left shift count is negative");
3342 else if (TREE_INT_CST_HIGH (op1) != 0
3343 || ((unsigned HOST_WIDE_INT) TREE_INT_CST_LOW (op1)
3344 >= TYPE_PRECISION (type0)))
3345 warning ("left shift count >= width of type");
3346 }
3347 /* Convert the shift-count to an integer, regardless of
3348 size of value being shifted. */
3349 if (TYPE_MAIN_VARIANT (TREE_TYPE (op1)) != integer_type_node)
3350 op1 = cp_convert (integer_type_node, op1);
3351 /* Avoid converting op1 to result_type later. */
3352 converted = 1;
3353 }
3354 break;
3355
3356 case RROTATE_EXPR:
3357 case LROTATE_EXPR:
3358 if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
3359 {
3360 result_type = type0;
3361 if (TREE_CODE (op1) == INTEGER_CST)
3362 {
3363 if (tree_int_cst_lt (op1, integer_zero_node))
3364 warning ("%s rotate count is negative",
3365 (code == LROTATE_EXPR) ? "left" : "right");
3366 else if (TREE_INT_CST_HIGH (op1) != 0
3367 || ((unsigned HOST_WIDE_INT) TREE_INT_CST_LOW (op1)
3368 >= TYPE_PRECISION (type0)))
3369 warning ("%s rotate count >= width of type",
3370 (code == LROTATE_EXPR) ? "left" : "right");
3371 }
3372 /* Convert the shift-count to an integer, regardless of
3373 size of value being shifted. */
3374 if (TYPE_MAIN_VARIANT (TREE_TYPE (op1)) != integer_type_node)
3375 op1 = cp_convert (integer_type_node, op1);
3376 }
3377 break;
3378
3379 case EQ_EXPR:
3380 case NE_EXPR:
3381 build_type = boolean_type_node;
3382 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE
3383 || code0 == COMPLEX_TYPE)
3384 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
3385 || code1 == COMPLEX_TYPE))
3386 short_compare = 1;
3387 else if (code0 == POINTER_TYPE && code1 == POINTER_TYPE)
3388 {
3389 register tree tt0 = TYPE_MAIN_VARIANT (TREE_TYPE (type0));
3390 register tree tt1 = TYPE_MAIN_VARIANT (TREE_TYPE (type1));
3391
3392 if (comp_target_types (type0, type1, 1))
3393 result_type = common_type (type0, type1);
3394 else if (tt0 == void_type_node)
3395 {
3396 if (pedantic && TREE_CODE (tt1) == FUNCTION_TYPE
3397 && tree_int_cst_lt (TYPE_SIZE (type0), TYPE_SIZE (type1)))
3398 pedwarn ("ANSI C++ forbids comparison of `void *' with function pointer");
3399 else if (TREE_CODE (tt1) == OFFSET_TYPE)
3400 pedwarn ("ANSI C++ forbids conversion of a pointer to member to `void *'");
3401 }
3402 else if (tt1 == void_type_node)
3403 {
3404 if (pedantic && TREE_CODE (tt0) == FUNCTION_TYPE
3405 && tree_int_cst_lt (TYPE_SIZE (type1), TYPE_SIZE (type0)))
3406 pedwarn ("ANSI C++ forbids comparison of `void *' with function pointer");
3407 }
3408 else
3409 cp_pedwarn ("comparison of distinct pointer types `%T' and `%T' lacks a cast",
3410 type0, type1);
3411
3412 if (result_type == NULL_TREE)
3413 result_type = ptr_type_node;
3414 }
3415 else if (code0 == POINTER_TYPE && TREE_CODE (op1) == INTEGER_CST
3416 && integer_zerop (op1))
3417 result_type = type0;
3418 else if (code1 == POINTER_TYPE && TREE_CODE (op0) == INTEGER_CST
3419 && integer_zerop (op0))
3420 result_type = type1;
3421 else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
3422 {
3423 result_type = type0;
3424 error ("ANSI C++ forbids comparison between pointer and integer");
3425 }
3426 else if (code0 == INTEGER_TYPE && code1 == POINTER_TYPE)
3427 {
3428 result_type = type1;
3429 error ("ANSI C++ forbids comparison between pointer and integer");
3430 }
3431 else if (TYPE_PTRMEMFUNC_P (type0) && TREE_CODE (op1) == INTEGER_CST
3432 && integer_zerop (op1))
3433 {
3434 op0 = build_component_ref (op0, index_identifier, NULL_TREE, 0);
3435 op1 = integer_zero_node;
3436 result_type = TREE_TYPE (op0);
3437 }
3438 else if (TYPE_PTRMEMFUNC_P (type1) && TREE_CODE (op0) == INTEGER_CST
3439 && integer_zerop (op0))
3440 {
3441 op0 = build_component_ref (op1, index_identifier, NULL_TREE, 0);
3442 op1 = integer_zero_node;
3443 result_type = TREE_TYPE (op0);
3444 }
3445 else if (TYPE_PTRMEMFUNC_P (type0) && TYPE_PTRMEMFUNC_P (type1)
3446 && (TYPE_PTRMEMFUNC_FN_TYPE (type0)
3447 == TYPE_PTRMEMFUNC_FN_TYPE (type1)))
3448 {
3449 /* The code we generate for the test is:
3450
3451 (op0.index == op1.index
3452 && ((op1.index != -1 && op0.delta2 == op1.delta2)
3453 || op0.pfn == op1.pfn)) */
3454
3455 tree index0 = build_component_ref (op0, index_identifier, NULL_TREE, 0);
3456 tree index1 = save_expr (build_component_ref (op1, index_identifier, NULL_TREE, 0));
3457 tree pfn0 = PFN_FROM_PTRMEMFUNC (op0);
3458 tree pfn1 = PFN_FROM_PTRMEMFUNC (op1);
3459 tree delta20 = DELTA2_FROM_PTRMEMFUNC (op0);
3460 tree delta21 = DELTA2_FROM_PTRMEMFUNC (op1);
3461 tree e1, e2, e3;
3462 tree integer_neg_one_node
3463 = build_binary_op (MINUS_EXPR, integer_zero_node, integer_one_node, 1);
3464 e1 = build_binary_op (EQ_EXPR, index0, index1, 1);
3465 e2 = build_binary_op (NE_EXPR, index1, integer_neg_one_node, 1);
3466 e2 = build_binary_op (TRUTH_ANDIF_EXPR, e2, build_binary_op (EQ_EXPR, delta20, delta21, 1), 1);
3467 e3 = build_binary_op (EQ_EXPR, pfn0, pfn1, 1);
3468 e2 = build_binary_op (TRUTH_ORIF_EXPR, e2, e3, 1);
3469 e2 = build_binary_op (TRUTH_ANDIF_EXPR, e1, e2, 1);
3470 if (code == EQ_EXPR)
3471 return e2;
3472 return build_binary_op (EQ_EXPR, e2, integer_zero_node, 1);
3473 }
3474 else if (TYPE_PTRMEMFUNC_P (type0)
3475 && TYPE_PTRMEMFUNC_FN_TYPE (type0) == type1)
3476 {
3477 tree index0 = build_component_ref (op0, index_identifier, NULL_TREE, 0);
3478 tree index1;
3479 tree pfn0 = PFN_FROM_PTRMEMFUNC (op0);
3480 tree delta20 = DELTA2_FROM_PTRMEMFUNC (op0);
3481 tree delta21 = integer_zero_node;
3482 tree e1, e2, e3;
3483 tree integer_neg_one_node
3484 = build_binary_op (MINUS_EXPR, integer_zero_node, integer_one_node, 1);
3485 if (TREE_CODE (TREE_OPERAND (op1, 0)) == FUNCTION_DECL
3486 && DECL_VINDEX (TREE_OPERAND (op1, 0)))
3487 {
3488 /* Map everything down one to make room for the null pointer to member. */
3489 index1 = size_binop (PLUS_EXPR,
3490 DECL_VINDEX (TREE_OPERAND (op1, 0)),
3491 integer_one_node);
3492 op1 = integer_zero_node;
3493 delta21 = CLASSTYPE_VFIELD (TYPE_METHOD_BASETYPE (TREE_TYPE (type1)));
3494 delta21 = DECL_FIELD_BITPOS (delta21);
3495 delta21 = size_binop (FLOOR_DIV_EXPR, delta21, size_int (BITS_PER_UNIT));
3496 }
3497 else
3498 index1 = integer_neg_one_node;
3499 {
3500 tree nop1 = build1 (NOP_EXPR, TYPE_PTRMEMFUNC_FN_TYPE (type0), op1);
3501 TREE_CONSTANT (nop1) = TREE_CONSTANT (op1);
3502 op1 = nop1;
3503 }
3504 e1 = build_binary_op (EQ_EXPR, index0, index1, 1);
3505 e2 = build_binary_op (NE_EXPR, index1, integer_neg_one_node, 1);
3506 e2 = build_binary_op (TRUTH_ANDIF_EXPR, e2, build_binary_op (EQ_EXPR, delta20, delta21, 1), 1);
3507 e3 = build_binary_op (EQ_EXPR, pfn0, op1, 1);
3508 e2 = build_binary_op (TRUTH_ORIF_EXPR, e2, e3, 1);
3509 e2 = build_binary_op (TRUTH_ANDIF_EXPR, e1, e2, 1);
3510 if (code == EQ_EXPR)
3511 return e2;
3512 return build_binary_op (EQ_EXPR, e2, integer_zero_node, 1);
3513 }
3514 else if (TYPE_PTRMEMFUNC_P (type1)
3515 && TYPE_PTRMEMFUNC_FN_TYPE (type1) == type0)
3516 {
3517 return build_binary_op (code, op1, op0, 1);
3518 }
3519 break;
3520
3521 case MAX_EXPR:
3522 case MIN_EXPR:
3523 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE)
3524 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE))
3525 shorten = 1;
3526 else if (code0 == POINTER_TYPE && code1 == POINTER_TYPE)
3527 {
3528 if (comp_target_types (type0, type1, 1))
3529 result_type = common_type (type0, type1);
3530 else
3531 {
3532 cp_pedwarn ("comparison of distinct pointer types `%T' and `%T' lacks a cast",
3533 type0, type1);
3534 result_type = ptr_type_node;
3535 }
3536 }
3537 break;
3538
3539 case LE_EXPR:
3540 case GE_EXPR:
3541 case LT_EXPR:
3542 case GT_EXPR:
3543 build_type = boolean_type_node;
3544 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE)
3545 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE))
3546 short_compare = 1;
3547 else if (code0 == POINTER_TYPE && code1 == POINTER_TYPE)
3548 {
3549 if (comp_target_types (type0, type1, 1))
3550 result_type = common_type (type0, type1);
3551 else
3552 {
3553 cp_pedwarn ("comparison of distinct pointer types `%T' and `%T' lacks a cast",
3554 type0, type1);
3555 result_type = ptr_type_node;
3556 }
3557 }
3558 else if (code0 == POINTER_TYPE && TREE_CODE (op1) == INTEGER_CST
3559 && integer_zerop (op1))
3560 result_type = type0;
3561 else if (code1 == POINTER_TYPE && TREE_CODE (op0) == INTEGER_CST
3562 && integer_zerop (op0))
3563 result_type = type1;
3564 else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
3565 {
3566 result_type = type0;
3567 pedwarn ("ANSI C++ forbids comparison between pointer and integer");
3568 }
3569 else if (code0 == INTEGER_TYPE && code1 == POINTER_TYPE)
3570 {
3571 result_type = type1;
3572 pedwarn ("ANSI C++ forbids comparison between pointer and integer");
3573 }
3574 break;
3575 }
3576
3577 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE || code0 == COMPLEX_TYPE)
3578 &&
3579 (code1 == INTEGER_TYPE || code1 == REAL_TYPE || code1 == COMPLEX_TYPE))
3580 {
3581 int none_complex = (code0 != COMPLEX_TYPE && code1 != COMPLEX_TYPE);
3582
3583 if (shorten || common || short_compare)
3584 result_type = common_type (type0, type1);
3585
3586 /* For certain operations (which identify themselves by shorten != 0)
3587 if both args were extended from the same smaller type,
3588 do the arithmetic in that type and then extend.
3589
3590 shorten !=0 and !=1 indicates a bitwise operation.
3591 For them, this optimization is safe only if
3592 both args are zero-extended or both are sign-extended.
3593 Otherwise, we might change the result.
3594 Eg, (short)-1 | (unsigned short)-1 is (int)-1
3595 but calculated in (unsigned short) it would be (unsigned short)-1. */
3596
3597 if (shorten && none_complex)
3598 {
3599 int unsigned0, unsigned1;
3600 tree arg0 = get_narrower (op0, &unsigned0);
3601 tree arg1 = get_narrower (op1, &unsigned1);
3602 /* UNS is 1 if the operation to be done is an unsigned one. */
3603 int uns = TREE_UNSIGNED (result_type);
3604 tree type;
3605
3606 final_type = result_type;
3607
3608 /* Handle the case that OP0 does not *contain* a conversion
3609 but it *requires* conversion to FINAL_TYPE. */
3610
3611 if (op0 == arg0 && TREE_TYPE (op0) != final_type)
3612 unsigned0 = TREE_UNSIGNED (TREE_TYPE (op0));
3613 if (op1 == arg1 && TREE_TYPE (op1) != final_type)
3614 unsigned1 = TREE_UNSIGNED (TREE_TYPE (op1));
3615
3616 /* Now UNSIGNED0 is 1 if ARG0 zero-extends to FINAL_TYPE. */
3617
3618 /* For bitwise operations, signedness of nominal type
3619 does not matter. Consider only how operands were extended. */
3620 if (shorten == -1)
3621 uns = unsigned0;
3622
3623 /* Note that in all three cases below we refrain from optimizing
3624 an unsigned operation on sign-extended args.
3625 That would not be valid. */
3626
3627 /* Both args variable: if both extended in same way
3628 from same width, do it in that width.
3629 Do it unsigned if args were zero-extended. */
3630 if ((TYPE_PRECISION (TREE_TYPE (arg0))
3631 < TYPE_PRECISION (result_type))
3632 && (TYPE_PRECISION (TREE_TYPE (arg1))
3633 == TYPE_PRECISION (TREE_TYPE (arg0)))
3634 && unsigned0 == unsigned1
3635 && (unsigned0 || !uns))
3636 result_type
3637 = signed_or_unsigned_type (unsigned0,
3638 common_type (TREE_TYPE (arg0), TREE_TYPE (arg1)));
3639 else if (TREE_CODE (arg0) == INTEGER_CST
3640 && (unsigned1 || !uns)
3641 && (TYPE_PRECISION (TREE_TYPE (arg1))
3642 < TYPE_PRECISION (result_type))
3643 && (type = signed_or_unsigned_type (unsigned1,
3644 TREE_TYPE (arg1)),
3645 int_fits_type_p (arg0, type)))
3646 result_type = type;
3647 else if (TREE_CODE (arg1) == INTEGER_CST
3648 && (unsigned0 || !uns)
3649 && (TYPE_PRECISION (TREE_TYPE (arg0))
3650 < TYPE_PRECISION (result_type))
3651 && (type = signed_or_unsigned_type (unsigned0,
3652 TREE_TYPE (arg0)),
3653 int_fits_type_p (arg1, type)))
3654 result_type = type;
3655 }
3656
3657 /* Shifts can be shortened if shifting right. */
3658
3659 if (short_shift)
3660 {
3661 int unsigned_arg;
3662 tree arg0 = get_narrower (op0, &unsigned_arg);
3663
3664 final_type = result_type;
3665
3666 if (arg0 == op0 && final_type == TREE_TYPE (op0))
3667 unsigned_arg = TREE_UNSIGNED (TREE_TYPE (op0));
3668
3669 if (TYPE_PRECISION (TREE_TYPE (arg0)) < TYPE_PRECISION (result_type)
3670 /* We can shorten only if the shift count is less than the
3671 number of bits in the smaller type size. */
3672 && TREE_INT_CST_HIGH (op1) == 0
3673 && TYPE_PRECISION (TREE_TYPE (arg0)) > TREE_INT_CST_LOW (op1)
3674 /* If arg is sign-extended and then unsigned-shifted,
3675 we can simulate this with a signed shift in arg's type
3676 only if the extended result is at least twice as wide
3677 as the arg. Otherwise, the shift could use up all the
3678 ones made by sign-extension and bring in zeros.
3679 We can't optimize that case at all, but in most machines
3680 it never happens because available widths are 2**N. */
3681 && (!TREE_UNSIGNED (final_type)
3682 || unsigned_arg
3683 || (((unsigned) 2 * TYPE_PRECISION (TREE_TYPE (arg0)))
3684 <= TYPE_PRECISION (result_type))))
3685 {
3686 /* Do an unsigned shift if the operand was zero-extended. */
3687 result_type
3688 = signed_or_unsigned_type (unsigned_arg,
3689 TREE_TYPE (arg0));
3690 /* Convert value-to-be-shifted to that type. */
3691 if (TREE_TYPE (op0) != result_type)
3692 op0 = cp_convert (result_type, op0);
3693 converted = 1;
3694 }
3695 }
3696
3697 /* Comparison operations are shortened too but differently.
3698 They identify themselves by setting short_compare = 1. */
3699
3700 if (short_compare)
3701 {
3702 /* Don't write &op0, etc., because that would prevent op0
3703 from being kept in a register.
3704 Instead, make copies of the our local variables and
3705 pass the copies by reference, then copy them back afterward. */
3706 tree xop0 = op0, xop1 = op1, xresult_type = result_type;
3707 enum tree_code xresultcode = resultcode;
3708 tree val
3709 = shorten_compare (&xop0, &xop1, &xresult_type, &xresultcode);
3710 if (val != 0)
3711 return cp_convert (boolean_type_node, val);
3712 op0 = xop0, op1 = xop1;
3713 converted = 1;
3714 resultcode = xresultcode;
3715 }
3716
3717 if (short_compare && warn_sign_compare)
3718 {
3719 int op0_signed = ! TREE_UNSIGNED (TREE_TYPE (orig_op0));
3720 int op1_signed = ! TREE_UNSIGNED (TREE_TYPE (orig_op1));
3721
3722 int unsignedp0, unsignedp1;
3723 tree primop0 = get_narrower (op0, &unsignedp0);
3724 tree primop1 = get_narrower (op1, &unsignedp1);
3725
3726 /* Check for comparison of different enum types. */
3727 if (flag_int_enum_equivalence == 0
3728 && TREE_CODE (TREE_TYPE (orig_op0)) == ENUMERAL_TYPE
3729 && TREE_CODE (TREE_TYPE (orig_op1)) == ENUMERAL_TYPE
3730 && TYPE_MAIN_VARIANT (TREE_TYPE (orig_op0))
3731 != TYPE_MAIN_VARIANT (TREE_TYPE (orig_op1)))
3732 {
3733 cp_warning ("comparison between `%#T' and `%#T'",
3734 TREE_TYPE (orig_op0), TREE_TYPE (orig_op1));
3735 }
3736
3737 /* Give warnings for comparisons between signed and unsigned
3738 quantities that may fail. */
3739 /* Do the checking based on the original operand trees, so that
3740 casts will be considered, but default promotions won't be. */
3741
3742 /* Do not warn if the comparison is being done in a signed type,
3743 since the signed type will only be chosen if it can represent
3744 all the values of the unsigned type. */
3745 if (! TREE_UNSIGNED (result_type))
3746 /* OK */;
3747 /* Do not warn if both operands are unsigned. */
3748 else if (op0_signed == op1_signed)
3749 /* OK */;
3750 /* Do not warn if the signed quantity is an unsuffixed
3751 integer literal (or some static constant expression
3752 involving such literals) and it is non-negative. */
3753 else if ((op0_signed && TREE_CODE (orig_op0) == INTEGER_CST
3754 && tree_int_cst_sgn (orig_op0) >= 0)
3755 || (op1_signed && TREE_CODE (orig_op1) == INTEGER_CST
3756 && tree_int_cst_sgn (orig_op1) >= 0))
3757 /* OK */;
3758 /* Do not warn if the comparison is an equality operation,
3759 the unsigned quantity is an integral constant and it does
3760 not use the most significant bit of result_type. */
3761 else if ((resultcode == EQ_EXPR || resultcode == NE_EXPR)
3762 && ((op0_signed && TREE_CODE (orig_op1) == INTEGER_CST
3763 && int_fits_type_p (orig_op1, signed_type (result_type))
3764 || (op1_signed && TREE_CODE (orig_op0) == INTEGER_CST
3765 && int_fits_type_p (orig_op0, signed_type (result_type))))))
3766 /* OK */;
3767 else
3768 warning ("comparison between signed and unsigned");
3769
3770 /* Warn if two unsigned values are being compared in a size
3771 larger than their original size, and one (and only one) is the
3772 result of a `~' operator. This comparison will always fail.
3773
3774 Also warn if one operand is a constant, and the constant does not
3775 have all bits set that are set in the ~ operand when it is
3776 extended. */
3777
3778 if ((TREE_CODE (primop0) == BIT_NOT_EXPR)
3779 ^ (TREE_CODE (primop1) == BIT_NOT_EXPR))
3780 {
3781 if (TREE_CODE (primop0) == BIT_NOT_EXPR)
3782 primop0 = get_narrower (TREE_OPERAND (op0, 0), &unsignedp0);
3783 if (TREE_CODE (primop1) == BIT_NOT_EXPR)
3784 primop1 = get_narrower (TREE_OPERAND (op1, 0), &unsignedp1);
3785
3786 if (TREE_CODE (primop0) == INTEGER_CST
3787 || TREE_CODE (primop1) == INTEGER_CST)
3788 {
3789 tree primop;
3790 HOST_WIDE_INT constant, mask;
3791 int unsignedp;
3792 unsigned bits;
3793
3794 if (TREE_CODE (primop0) == INTEGER_CST)
3795 {
3796 primop = primop1;
3797 unsignedp = unsignedp1;
3798 constant = TREE_INT_CST_LOW (primop0);
3799 }
3800 else
3801 {
3802 primop = primop0;
3803 unsignedp = unsignedp0;
3804 constant = TREE_INT_CST_LOW (primop1);
3805 }
3806
3807 bits = TYPE_PRECISION (TREE_TYPE (primop));
3808 if (bits < TYPE_PRECISION (result_type)
3809 && bits < HOST_BITS_PER_LONG && unsignedp)
3810 {
3811 mask = (~ (HOST_WIDE_INT) 0) << bits;
3812 if ((mask & constant) != mask)
3813 warning ("comparison of promoted ~unsigned with constant");
3814 }
3815 }
3816 else if (unsignedp0 && unsignedp1
3817 && (TYPE_PRECISION (TREE_TYPE (primop0))
3818 < TYPE_PRECISION (result_type))
3819 && (TYPE_PRECISION (TREE_TYPE (primop1))
3820 < TYPE_PRECISION (result_type)))
3821 warning ("comparison of promoted ~unsigned with unsigned");
3822 }
3823 }
3824 }
3825
3826 /* At this point, RESULT_TYPE must be nonzero to avoid an error message.
3827 If CONVERTED is zero, both args will be converted to type RESULT_TYPE.
3828 Then the expression will be built.
3829 It will be given type FINAL_TYPE if that is nonzero;
3830 otherwise, it will be given type RESULT_TYPE. */
3831
3832 if (!result_type)
3833 {
3834 cp_error ("invalid operands `%T' and `%T' to binary `%O'",
3835 TREE_TYPE (orig_op0), TREE_TYPE (orig_op1), error_code);
3836 return error_mark_node;
3837 }
3838
3839 if (! converted)
3840 {
3841 if (TREE_TYPE (op0) != result_type)
3842 op0 = cp_convert (result_type, op0);
3843 if (TREE_TYPE (op1) != result_type)
3844 op1 = cp_convert (result_type, op1);
3845 }
3846
3847 if (build_type == NULL_TREE)
3848 build_type = result_type;
3849
3850 {
3851 register tree result = build (resultcode, build_type, op0, op1);
3852 register tree folded;
3853
3854 folded = fold (result);
3855 if (folded == result)
3856 TREE_CONSTANT (folded) = TREE_CONSTANT (op0) & TREE_CONSTANT (op1);
3857 if (final_type != 0)
3858 return cp_convert (final_type, folded);
3859 return folded;
3860 }
3861 }
3862 \f
3863 /* Return a tree for the sum or difference (RESULTCODE says which)
3864 of pointer PTROP and integer INTOP. */
3865
3866 static tree
3867 pointer_int_sum (resultcode, ptrop, intop)
3868 enum tree_code resultcode;
3869 register tree ptrop, intop;
3870 {
3871 tree size_exp;
3872
3873 register tree result;
3874 register tree folded = fold (intop);
3875
3876 /* The result is a pointer of the same type that is being added. */
3877
3878 register tree result_type = TREE_TYPE (ptrop);
3879
3880 if (TREE_CODE (TREE_TYPE (result_type)) == VOID_TYPE)
3881 {
3882 if (pedantic || warn_pointer_arith)
3883 pedwarn ("ANSI C++ forbids using pointer of type `void *' in arithmetic");
3884 size_exp = integer_one_node;
3885 }
3886 else if (TREE_CODE (TREE_TYPE (result_type)) == FUNCTION_TYPE)
3887 {
3888 if (pedantic || warn_pointer_arith)
3889 pedwarn ("ANSI C++ forbids using pointer to a function in arithmetic");
3890 size_exp = integer_one_node;
3891 }
3892 else if (TREE_CODE (TREE_TYPE (result_type)) == METHOD_TYPE)
3893 {
3894 if (pedantic || warn_pointer_arith)
3895 pedwarn ("ANSI C++ forbids using pointer to a method in arithmetic");
3896 size_exp = integer_one_node;
3897 }
3898 else if (TREE_CODE (TREE_TYPE (result_type)) == OFFSET_TYPE)
3899 {
3900 if (pedantic || warn_pointer_arith)
3901 pedwarn ("ANSI C++ forbids using pointer to a member in arithmetic");
3902 size_exp = integer_one_node;
3903 }
3904 else
3905 size_exp = size_in_bytes (complete_type (TREE_TYPE (result_type)));
3906
3907 /* Needed to make OOPS V2R3 work. */
3908 intop = folded;
3909 if (TREE_CODE (intop) == INTEGER_CST
3910 && TREE_INT_CST_LOW (intop) == 0
3911 && TREE_INT_CST_HIGH (intop) == 0)
3912 return ptrop;
3913
3914 /* If what we are about to multiply by the size of the elements
3915 contains a constant term, apply distributive law
3916 and multiply that constant term separately.
3917 This helps produce common subexpressions. */
3918
3919 if ((TREE_CODE (intop) == PLUS_EXPR || TREE_CODE (intop) == MINUS_EXPR)
3920 && ! TREE_CONSTANT (intop)
3921 && TREE_CONSTANT (TREE_OPERAND (intop, 1))
3922 && TREE_CONSTANT (size_exp))
3923 {
3924 enum tree_code subcode = resultcode;
3925 if (TREE_CODE (intop) == MINUS_EXPR)
3926 subcode = (subcode == PLUS_EXPR ? MINUS_EXPR : PLUS_EXPR);
3927 ptrop = build_binary_op (subcode, ptrop, TREE_OPERAND (intop, 1), 1);
3928 intop = TREE_OPERAND (intop, 0);
3929 }
3930
3931 /* Convert the integer argument to a type the same size as sizetype
3932 so the multiply won't overflow spuriously. */
3933
3934 if (TYPE_PRECISION (TREE_TYPE (intop)) != TYPE_PRECISION (sizetype))
3935 intop = cp_convert (type_for_size (TYPE_PRECISION (sizetype), 0), intop);
3936
3937 /* Replace the integer argument with a suitable product by the object size.
3938 Do this multiplication as signed, then convert to the appropriate
3939 pointer type (actually unsigned integral). */
3940
3941 intop = cp_convert (result_type,
3942 build_binary_op (MULT_EXPR, intop,
3943 cp_convert (TREE_TYPE (intop), size_exp), 1));
3944
3945 /* Create the sum or difference. */
3946
3947 result = build (resultcode, result_type, ptrop, intop);
3948
3949 folded = fold (result);
3950 if (folded == result)
3951 TREE_CONSTANT (folded) = TREE_CONSTANT (ptrop) & TREE_CONSTANT (intop);
3952 return folded;
3953 }
3954
3955 /* Return a tree for the difference of pointers OP0 and OP1.
3956 The resulting tree has type int. */
3957
3958 static tree
3959 pointer_diff (op0, op1)
3960 register tree op0, op1;
3961 {
3962 register tree result, folded;
3963 tree restype = ptrdiff_type_node;
3964 tree target_type = TREE_TYPE (TREE_TYPE (op0));
3965
3966 if (pedantic || warn_pointer_arith)
3967 {
3968 if (TREE_CODE (target_type) == VOID_TYPE)
3969 pedwarn ("ANSI C++ forbids using pointer of type `void *' in subtraction");
3970 if (TREE_CODE (target_type) == FUNCTION_TYPE)
3971 pedwarn ("ANSI C++ forbids using pointer to a function in subtraction");
3972 if (TREE_CODE (target_type) == METHOD_TYPE)
3973 pedwarn ("ANSI C++ forbids using pointer to a method in subtraction");
3974 if (TREE_CODE (target_type) == OFFSET_TYPE)
3975 pedwarn ("ANSI C++ forbids using pointer to a member in subtraction");
3976 }
3977
3978 /* First do the subtraction as integers;
3979 then drop through to build the divide operator. */
3980
3981 op0 = build_binary_op (MINUS_EXPR,
3982 cp_convert (restype, op0), cp_convert (restype, op1), 1);
3983
3984 /* This generates an error if op1 is a pointer to an incomplete type. */
3985 if (TYPE_SIZE (TREE_TYPE (TREE_TYPE (op1))) == 0)
3986 error ("arithmetic on pointer to an incomplete type");
3987
3988 op1 = ((TREE_CODE (target_type) == VOID_TYPE
3989 || TREE_CODE (target_type) == FUNCTION_TYPE
3990 || TREE_CODE (target_type) == METHOD_TYPE
3991 || TREE_CODE (target_type) == OFFSET_TYPE)
3992 ? integer_one_node
3993 : size_in_bytes (target_type));
3994
3995 /* Do the division. */
3996
3997 result = build (EXACT_DIV_EXPR, restype, op0, cp_convert (restype, op1));
3998
3999 folded = fold (result);
4000 if (folded == result)
4001 TREE_CONSTANT (folded) = TREE_CONSTANT (op0) & TREE_CONSTANT (op1);
4002 return folded;
4003 }
4004 \f
4005 /* Handle the case of taking the address of a COMPONENT_REF.
4006 Called by `build_unary_op' and `build_up_reference'.
4007
4008 ARG is the COMPONENT_REF whose address we want.
4009 ARGTYPE is the pointer type that this address should have.
4010 MSG is an error message to print if this COMPONENT_REF is not
4011 addressable (such as a bitfield). */
4012
4013 tree
4014 build_component_addr (arg, argtype, msg)
4015 tree arg, argtype;
4016 char *msg;
4017 {
4018 tree field = TREE_OPERAND (arg, 1);
4019 tree basetype = decl_type_context (field);
4020 tree rval = build_unary_op (ADDR_EXPR, TREE_OPERAND (arg, 0), 0);
4021
4022 if (DECL_BIT_FIELD (field))
4023 {
4024 error (msg, IDENTIFIER_POINTER (DECL_NAME (field)));
4025 return error_mark_node;
4026 }
4027
4028 if (TREE_CODE (field) == FIELD_DECL
4029 && TYPE_USES_COMPLEX_INHERITANCE (basetype))
4030 {
4031 /* Can't convert directly to ARGTYPE, since that
4032 may have the same pointer type as one of our
4033 baseclasses. */
4034 rval = build1 (NOP_EXPR, argtype,
4035 convert_pointer_to (basetype, rval));
4036 TREE_CONSTANT (rval) = TREE_CONSTANT (TREE_OPERAND (rval, 0));
4037 }
4038 else
4039 /* This conversion is harmless. */
4040 rval = convert_force (argtype, rval, 0);
4041
4042 if (! integer_zerop (DECL_FIELD_BITPOS (field)))
4043 {
4044 tree offset = size_binop (EASY_DIV_EXPR, DECL_FIELD_BITPOS (field),
4045 size_int (BITS_PER_UNIT));
4046 int flag = TREE_CONSTANT (rval);
4047 rval = fold (build (PLUS_EXPR, argtype,
4048 rval, cp_convert (argtype, offset)));
4049 TREE_CONSTANT (rval) = flag;
4050 }
4051 return rval;
4052 }
4053
4054 /* Construct and perhaps optimize a tree representation
4055 for a unary operation. CODE, a tree_code, specifies the operation
4056 and XARG is the operand. */
4057
4058 tree
4059 build_x_unary_op (code, xarg)
4060 enum tree_code code;
4061 tree xarg;
4062 {
4063 if (processing_template_decl)
4064 return build_min_nt (code, xarg, NULL_TREE);
4065
4066 /* & rec, on incomplete RECORD_TYPEs is the simple opr &, not an
4067 error message. */
4068 if (code == ADDR_EXPR
4069 && TREE_CODE (xarg) != TEMPLATE_ID_EXPR
4070 && ((IS_AGGR_TYPE_CODE (TREE_CODE (TREE_TYPE (xarg)))
4071 && TYPE_SIZE (TREE_TYPE (xarg)) == NULL_TREE)
4072 || (TREE_CODE (xarg) == OFFSET_REF)))
4073 /* don't look for a function */;
4074 else
4075 {
4076 tree rval;
4077
4078 if (flag_ansi_overloading)
4079 {
4080 rval = build_new_op (code, LOOKUP_NORMAL, xarg,
4081 NULL_TREE, NULL_TREE);
4082 if (rval || code != ADDR_EXPR)
4083 return rval;
4084 }
4085 else
4086 {
4087 rval = build_opfncall (code, LOOKUP_SPECULATIVELY, xarg,
4088 NULL_TREE, NULL_TREE);
4089 if (rval)
4090 return build_opfncall (code, LOOKUP_NORMAL, xarg,
4091 NULL_TREE, NULL_TREE);
4092 }
4093 }
4094
4095 if (code == ADDR_EXPR)
4096 {
4097 if (TREE_CODE (xarg) == TARGET_EXPR)
4098 warning ("taking address of temporary");
4099 }
4100
4101 return build_unary_op (code, xarg, 0);
4102 }
4103
4104 /* Just like truthvalue_conversion, but we want a CLEANUP_POINT_EXPR. */
4105
4106 tree
4107 condition_conversion (expr)
4108 tree expr;
4109 {
4110 tree t;
4111 if (processing_template_decl)
4112 return expr;
4113 t = cp_convert (boolean_type_node, expr);
4114 t = fold (build1 (CLEANUP_POINT_EXPR, boolean_type_node, t));
4115 return t;
4116 }
4117
4118 /* C++: Must handle pointers to members.
4119
4120 Perhaps type instantiation should be extended to handle conversion
4121 from aggregates to types we don't yet know we want? (Or are those
4122 cases typically errors which should be reported?)
4123
4124 NOCONVERT nonzero suppresses the default promotions
4125 (such as from short to int). */
4126
4127 tree
4128 build_unary_op (code, xarg, noconvert)
4129 enum tree_code code;
4130 tree xarg;
4131 int noconvert;
4132 {
4133 /* No default_conversion here. It causes trouble for ADDR_EXPR. */
4134 register tree arg = xarg;
4135 register tree argtype = 0;
4136 char *errstring = NULL;
4137 tree val;
4138
4139 if (arg == error_mark_node)
4140 return error_mark_node;
4141
4142 switch (code)
4143 {
4144 case CONVERT_EXPR:
4145 /* This is used for unary plus, because a CONVERT_EXPR
4146 is enough to prevent anybody from looking inside for
4147 associativity, but won't generate any code. */
4148 if (!(arg = build_expr_type_conversion
4149 (WANT_ARITH | WANT_ENUM | WANT_POINTER, arg, 1)))
4150 errstring = "wrong type argument to unary plus";
4151 else
4152 {
4153 if (!noconvert)
4154 arg = default_conversion (arg);
4155 arg = build1 (NON_LVALUE_EXPR, TREE_TYPE (arg), arg);
4156 }
4157 break;
4158
4159 case NEGATE_EXPR:
4160 if (!(arg = build_expr_type_conversion (WANT_ARITH | WANT_ENUM, arg, 1)))
4161 errstring = "wrong type argument to unary minus";
4162 else if (!noconvert)
4163 arg = default_conversion (arg);
4164 break;
4165
4166 case BIT_NOT_EXPR:
4167 if (TREE_CODE (TREE_TYPE (arg)) == COMPLEX_TYPE)
4168 {
4169 code = CONJ_EXPR;
4170 if (!noconvert)
4171 arg = default_conversion (arg);
4172 }
4173 else if (!(arg = build_expr_type_conversion (WANT_INT | WANT_ENUM,
4174 arg, 1)))
4175 errstring = "wrong type argument to bit-complement";
4176 else if (!noconvert)
4177 arg = default_conversion (arg);
4178 break;
4179
4180 case ABS_EXPR:
4181 if (!(arg = build_expr_type_conversion (WANT_ARITH | WANT_ENUM, arg, 1)))
4182 errstring = "wrong type argument to abs";
4183 else if (!noconvert)
4184 arg = default_conversion (arg);
4185 break;
4186
4187 case CONJ_EXPR:
4188 /* Conjugating a real value is a no-op, but allow it anyway. */
4189 if (!(arg = build_expr_type_conversion (WANT_ARITH | WANT_ENUM, arg, 1)))
4190 errstring = "wrong type argument to conjugation";
4191 else if (!noconvert)
4192 arg = default_conversion (arg);
4193 break;
4194
4195 case TRUTH_NOT_EXPR:
4196 arg = cp_convert (boolean_type_node, arg);
4197 val = invert_truthvalue (arg);
4198 if (arg != error_mark_node)
4199 return val;
4200 errstring = "in argument to unary !";
4201 break;
4202
4203 case NOP_EXPR:
4204 break;
4205
4206 case REALPART_EXPR:
4207 if (TREE_CODE (arg) == COMPLEX_CST)
4208 return TREE_REALPART (arg);
4209 else if (TREE_CODE (TREE_TYPE (arg)) == COMPLEX_TYPE)
4210 return fold (build1 (REALPART_EXPR, TREE_TYPE (TREE_TYPE (arg)), arg));
4211 else
4212 return arg;
4213
4214 case IMAGPART_EXPR:
4215 if (TREE_CODE (arg) == COMPLEX_CST)
4216 return TREE_IMAGPART (arg);
4217 else if (TREE_CODE (TREE_TYPE (arg)) == COMPLEX_TYPE)
4218 return fold (build1 (IMAGPART_EXPR, TREE_TYPE (TREE_TYPE (arg)), arg));
4219 else
4220 return cp_convert (TREE_TYPE (arg), integer_zero_node);
4221
4222 case PREINCREMENT_EXPR:
4223 case POSTINCREMENT_EXPR:
4224 case PREDECREMENT_EXPR:
4225 case POSTDECREMENT_EXPR:
4226 /* Handle complex lvalues (when permitted)
4227 by reduction to simpler cases. */
4228
4229 val = unary_complex_lvalue (code, arg);
4230 if (val != 0)
4231 return val;
4232
4233 /* Increment or decrement the real part of the value,
4234 and don't change the imaginary part. */
4235 if (TREE_CODE (TREE_TYPE (arg)) == COMPLEX_TYPE)
4236 {
4237 tree real, imag;
4238
4239 arg = stabilize_reference (arg);
4240 real = build_unary_op (REALPART_EXPR, arg, 1);
4241 imag = build_unary_op (IMAGPART_EXPR, arg, 1);
4242 return build (COMPLEX_EXPR, TREE_TYPE (arg),
4243 build_unary_op (code, real, 1), imag);
4244 }
4245
4246 /* Report invalid types. */
4247
4248 if (!(arg = build_expr_type_conversion (WANT_ARITH | WANT_POINTER,
4249 arg, 1)))
4250 {
4251 if (code == PREINCREMENT_EXPR)
4252 errstring ="no pre-increment operator for type";
4253 else if (code == POSTINCREMENT_EXPR)
4254 errstring ="no post-increment operator for type";
4255 else if (code == PREDECREMENT_EXPR)
4256 errstring ="no pre-decrement operator for type";
4257 else
4258 errstring ="no post-decrement operator for type";
4259 break;
4260 }
4261
4262 /* Report something read-only. */
4263
4264 if (TYPE_READONLY (TREE_TYPE (arg))
4265 || TREE_READONLY (arg))
4266 readonly_error (arg, ((code == PREINCREMENT_EXPR
4267 || code == POSTINCREMENT_EXPR)
4268 ? "increment" : "decrement"),
4269 0);
4270
4271 {
4272 register tree inc;
4273 tree result_type = TREE_TYPE (arg);
4274
4275 arg = get_unwidened (arg, 0);
4276 argtype = TREE_TYPE (arg);
4277
4278 /* ARM $5.2.5 last annotation says this should be forbidden. */
4279 if (TREE_CODE (argtype) == ENUMERAL_TYPE)
4280 pedwarn ("ANSI C++ forbids %sing an enum",
4281 (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
4282 ? "increment" : "decrement");
4283
4284 /* Compute the increment. */
4285
4286 if (TREE_CODE (argtype) == POINTER_TYPE)
4287 {
4288 enum tree_code tmp = TREE_CODE (TREE_TYPE (argtype));
4289 if (TYPE_SIZE (complete_type (TREE_TYPE (argtype))) == 0)
4290 cp_error ("cannot %s a pointer to incomplete type `%T'",
4291 ((code == PREINCREMENT_EXPR
4292 || code == POSTINCREMENT_EXPR)
4293 ? "increment" : "decrement"), TREE_TYPE (argtype));
4294 else if (tmp == FUNCTION_TYPE || tmp == METHOD_TYPE
4295 || tmp == VOID_TYPE || tmp == OFFSET_TYPE)
4296 cp_pedwarn ("ANSI C++ forbids %sing a pointer of type `%T'",
4297 ((code == PREINCREMENT_EXPR
4298 || code == POSTINCREMENT_EXPR)
4299 ? "increment" : "decrement"), argtype);
4300 inc = c_sizeof_nowarn (TREE_TYPE (argtype));
4301 }
4302 else
4303 inc = integer_one_node;
4304
4305 inc = cp_convert (argtype, inc);
4306
4307 /* Handle incrementing a cast-expression. */
4308
4309 switch (TREE_CODE (arg))
4310 {
4311 case NOP_EXPR:
4312 case CONVERT_EXPR:
4313 case FLOAT_EXPR:
4314 case FIX_TRUNC_EXPR:
4315 case FIX_FLOOR_EXPR:
4316 case FIX_ROUND_EXPR:
4317 case FIX_CEIL_EXPR:
4318 {
4319 tree incremented, modify, value, compound;
4320 if (! lvalue_p (arg) && pedantic)
4321 pedwarn ("cast to non-reference type used as lvalue");
4322 arg = stabilize_reference (arg);
4323 if (code == PREINCREMENT_EXPR || code == PREDECREMENT_EXPR)
4324 value = arg;
4325 else
4326 value = save_expr (arg);
4327 incremented = build (((code == PREINCREMENT_EXPR
4328 || code == POSTINCREMENT_EXPR)
4329 ? PLUS_EXPR : MINUS_EXPR),
4330 argtype, value, inc);
4331 TREE_SIDE_EFFECTS (incremented) = 1;
4332
4333 modify = build_modify_expr (arg, NOP_EXPR, incremented);
4334 compound = build (COMPOUND_EXPR, TREE_TYPE (arg), modify, value);
4335
4336 /* Eliminate warning about unused result of + or -. */
4337 TREE_NO_UNUSED_WARNING (compound) = 1;
4338 return compound;
4339 }
4340 }
4341
4342 /* Complain about anything else that is not a true lvalue. */
4343 if (!lvalue_or_else (arg, ((code == PREINCREMENT_EXPR
4344 || code == POSTINCREMENT_EXPR)
4345 ? "increment" : "decrement")))
4346 return error_mark_node;
4347
4348 /* Forbid using -- on `bool'. */
4349 if (TREE_TYPE (arg) == boolean_type_node)
4350 {
4351 if (code == POSTDECREMENT_EXPR || code == PREDECREMENT_EXPR)
4352 {
4353 cp_error ("invalid use of `--' on bool variable `%D'", arg);
4354 return error_mark_node;
4355 }
4356 #if 0
4357 /* This will only work if someone can convince Kenner to accept
4358 my patch to expand_increment. (jason) */
4359 val = build (code, TREE_TYPE (arg), arg, inc);
4360 #else
4361 if (code == POSTINCREMENT_EXPR)
4362 {
4363 arg = stabilize_reference (arg);
4364 val = build (MODIFY_EXPR, TREE_TYPE (arg), arg,
4365 boolean_true_node);
4366 TREE_SIDE_EFFECTS (val) = 1;
4367 arg = save_expr (arg);
4368 val = build (COMPOUND_EXPR, TREE_TYPE (arg), val, arg);
4369 val = build (COMPOUND_EXPR, TREE_TYPE (arg), arg, val);
4370 }
4371 else
4372 val = build (MODIFY_EXPR, TREE_TYPE (arg), arg,
4373 boolean_true_node);
4374 #endif
4375 }
4376 else
4377 val = build (code, TREE_TYPE (arg), arg, inc);
4378
4379 TREE_SIDE_EFFECTS (val) = 1;
4380 return cp_convert (result_type, val);
4381 }
4382
4383 case ADDR_EXPR:
4384 /* Note that this operation never does default_conversion
4385 regardless of NOCONVERT. */
4386
4387 argtype = TREE_TYPE (arg);
4388 if (TREE_CODE (argtype) == REFERENCE_TYPE)
4389 {
4390 arg = build1
4391 (CONVERT_EXPR,
4392 build_pointer_type (TREE_TYPE (TREE_TYPE (arg))), arg);
4393 TREE_CONSTANT (arg) = TREE_CONSTANT (TREE_OPERAND (arg, 0));
4394 return arg;
4395 }
4396 else if (pedantic
4397 && TREE_CODE (arg) == FUNCTION_DECL
4398 && DECL_NAME (arg)
4399 && DECL_CONTEXT (arg) == NULL_TREE
4400 && IDENTIFIER_LENGTH (DECL_NAME (arg)) == 4
4401 && IDENTIFIER_POINTER (DECL_NAME (arg))[0] == 'm'
4402 && ! strcmp (IDENTIFIER_POINTER (DECL_NAME (arg)), "main"))
4403 /* ARM $3.4 */
4404 pedwarn ("taking address of function `main'");
4405
4406 /* Let &* cancel out to simplify resulting code. */
4407 if (TREE_CODE (arg) == INDIRECT_REF)
4408 {
4409 /* We don't need to have `current_class_ptr' wrapped in a
4410 NON_LVALUE_EXPR node. */
4411 if (arg == current_class_ref)
4412 return current_class_ptr;
4413
4414 arg = TREE_OPERAND (arg, 0);
4415 if (TREE_CODE (TREE_TYPE (arg)) == REFERENCE_TYPE)
4416 {
4417 arg = build1
4418 (CONVERT_EXPR,
4419 build_pointer_type (TREE_TYPE (TREE_TYPE (arg))), arg);
4420 TREE_CONSTANT (arg) = TREE_CONSTANT (TREE_OPERAND (arg, 0));
4421 }
4422 else if (lvalue_p (arg))
4423 /* Don't let this be an lvalue. */
4424 return non_lvalue (arg);
4425 return arg;
4426 }
4427
4428 /* For &x[y], return x+y */
4429 if (TREE_CODE (arg) == ARRAY_REF)
4430 {
4431 if (mark_addressable (TREE_OPERAND (arg, 0)) == 0)
4432 return error_mark_node;
4433 return build_binary_op (PLUS_EXPR, TREE_OPERAND (arg, 0),
4434 TREE_OPERAND (arg, 1), 1);
4435 }
4436
4437 /* Uninstantiated types are all functions. Taking the
4438 address of a function is a no-op, so just return the
4439 argument. */
4440
4441 if (TREE_CODE (arg) == IDENTIFIER_NODE
4442 && IDENTIFIER_OPNAME_P (arg))
4443 {
4444 my_friendly_abort (117);
4445 /* We don't know the type yet, so just work around the problem.
4446 We know that this will resolve to an lvalue. */
4447 return build1 (ADDR_EXPR, unknown_type_node, arg);
4448 }
4449
4450 if (TREE_CODE (arg) == TREE_LIST)
4451 {
4452 if (TREE_CODE (TREE_VALUE (arg)) == FUNCTION_DECL
4453 && DECL_CHAIN (TREE_VALUE (arg)) == NULL_TREE)
4454 /* Unique overloaded non-member function. */
4455 return build_unary_op (ADDR_EXPR, TREE_VALUE (arg), 0);
4456 if (TREE_CHAIN (arg) == NULL_TREE
4457 && TREE_CODE (TREE_VALUE (arg)) == TREE_LIST
4458 && DECL_CHAIN (TREE_VALUE (TREE_VALUE (arg))) == NULL_TREE)
4459 /* Unique overloaded member function. */
4460 return build_unary_op (ADDR_EXPR, TREE_VALUE (TREE_VALUE (arg)),
4461 0);
4462 return build1 (ADDR_EXPR, unknown_type_node, arg);
4463 }
4464 else if (TREE_CODE (arg) == TEMPLATE_ID_EXPR)
4465 {
4466 tree targs;
4467 tree fn;
4468
4469 /* We don't require a match here; it's possible that the
4470 context (like a cast to a particular type) will resolve
4471 the particular choice of template. */
4472 fn = determine_explicit_specialization (arg, NULL_TREE,
4473 &targs,
4474 0, 0);
4475
4476 if (fn)
4477 {
4478 fn = instantiate_template (fn, targs);
4479 mark_addressable (fn);
4480 return build_unary_op (ADDR_EXPR, fn, 0);
4481 }
4482
4483 return build1 (ADDR_EXPR, unknown_type_node, arg);
4484 }
4485
4486 /* Handle complex lvalues (when permitted)
4487 by reduction to simpler cases. */
4488 val = unary_complex_lvalue (code, arg);
4489 if (val != 0)
4490 return val;
4491
4492 switch (TREE_CODE (arg))
4493 {
4494 case NOP_EXPR:
4495 case CONVERT_EXPR:
4496 case FLOAT_EXPR:
4497 case FIX_TRUNC_EXPR:
4498 case FIX_FLOOR_EXPR:
4499 case FIX_ROUND_EXPR:
4500 case FIX_CEIL_EXPR:
4501 if (! lvalue_p (arg) && pedantic)
4502 pedwarn ("taking the address of a cast to non-reference type");
4503 }
4504
4505 /* Allow the address of a constructor if all the elements
4506 are constant. */
4507 if (TREE_CODE (arg) == CONSTRUCTOR && TREE_CONSTANT (arg))
4508 ;
4509 /* Anything not already handled and not a true memory reference
4510 is an error. */
4511 else if (TREE_CODE (argtype) != FUNCTION_TYPE
4512 && TREE_CODE (argtype) != METHOD_TYPE
4513 && !lvalue_or_else (arg, "unary `&'"))
4514 return error_mark_node;
4515
4516 /* Ordinary case; arg is a COMPONENT_REF or a decl. */
4517 /* If the lvalue is const or volatile,
4518 merge that into the type that the address will point to. */
4519 if (TREE_CODE_CLASS (TREE_CODE (arg)) == 'd'
4520 || TREE_CODE_CLASS (TREE_CODE (arg)) == 'r')
4521 {
4522 if (TREE_READONLY (arg) || TREE_THIS_VOLATILE (arg))
4523 argtype = cp_build_type_variant (argtype,
4524 TREE_READONLY (arg),
4525 TREE_THIS_VOLATILE (arg));
4526 }
4527
4528 argtype = build_pointer_type (argtype);
4529
4530 if (mark_addressable (arg) == 0)
4531 return error_mark_node;
4532
4533 {
4534 tree addr;
4535
4536 if (TREE_CODE (arg) == COMPONENT_REF)
4537 addr = build_component_addr (arg, argtype,
4538 "attempt to take address of bit-field structure member `%s'");
4539 else
4540 addr = build1 (code, argtype, arg);
4541
4542 /* Address of a static or external variable or
4543 function counts as a constant */
4544 if (staticp (arg))
4545 TREE_CONSTANT (addr) = 1;
4546
4547 if (TREE_CODE (argtype) == POINTER_TYPE
4548 && TREE_CODE (TREE_TYPE (argtype)) == METHOD_TYPE)
4549 {
4550 build_ptrmemfunc_type (argtype);
4551 addr = build_ptrmemfunc (argtype, addr, 0);
4552 }
4553
4554 return addr;
4555 }
4556 }
4557
4558 if (!errstring)
4559 {
4560 if (argtype == 0)
4561 argtype = TREE_TYPE (arg);
4562 return fold (build1 (code, argtype, arg));
4563 }
4564
4565 error (errstring);
4566 return error_mark_node;
4567 }
4568
4569 #if 0
4570 /* If CONVERSIONS is a conversion expression or a nested sequence of such,
4571 convert ARG with the same conversions in the same order
4572 and return the result. */
4573
4574 static tree
4575 convert_sequence (conversions, arg)
4576 tree conversions;
4577 tree arg;
4578 {
4579 switch (TREE_CODE (conversions))
4580 {
4581 case NOP_EXPR:
4582 case CONVERT_EXPR:
4583 case FLOAT_EXPR:
4584 case FIX_TRUNC_EXPR:
4585 case FIX_FLOOR_EXPR:
4586 case FIX_ROUND_EXPR:
4587 case FIX_CEIL_EXPR:
4588 return cp_convert (TREE_TYPE (conversions),
4589 convert_sequence (TREE_OPERAND (conversions, 0),
4590 arg));
4591
4592 default:
4593 return arg;
4594 }
4595 }
4596 #endif
4597
4598 /* Apply unary lvalue-demanding operator CODE to the expression ARG
4599 for certain kinds of expressions which are not really lvalues
4600 but which we can accept as lvalues.
4601
4602 If ARG is not a kind of expression we can handle, return zero. */
4603
4604 tree
4605 unary_complex_lvalue (code, arg)
4606 enum tree_code code;
4607 tree arg;
4608 {
4609 /* Handle (a, b) used as an "lvalue". */
4610 if (TREE_CODE (arg) == COMPOUND_EXPR)
4611 {
4612 tree real_result = build_unary_op (code, TREE_OPERAND (arg, 1), 0);
4613 return build (COMPOUND_EXPR, TREE_TYPE (real_result),
4614 TREE_OPERAND (arg, 0), real_result);
4615 }
4616
4617 /* Handle (a ? b : c) used as an "lvalue". */
4618 if (TREE_CODE (arg) == COND_EXPR
4619 || TREE_CODE (arg) == MIN_EXPR || TREE_CODE (arg) == MAX_EXPR)
4620 return rationalize_conditional_expr (code, arg);
4621
4622 if (TREE_CODE (arg) == MODIFY_EXPR
4623 || TREE_CODE (arg) == PREINCREMENT_EXPR
4624 || TREE_CODE (arg) == PREDECREMENT_EXPR)
4625 return unary_complex_lvalue
4626 (code, build (COMPOUND_EXPR, TREE_TYPE (TREE_OPERAND (arg, 0)),
4627 arg, TREE_OPERAND (arg, 0)));
4628
4629 if (code != ADDR_EXPR)
4630 return 0;
4631
4632 /* Handle (a = b) used as an "lvalue" for `&'. */
4633 if (TREE_CODE (arg) == MODIFY_EXPR
4634 || TREE_CODE (arg) == INIT_EXPR)
4635 {
4636 tree real_result = build_unary_op (code, TREE_OPERAND (arg, 0), 0);
4637 arg = build (COMPOUND_EXPR, TREE_TYPE (real_result), arg, real_result);
4638 TREE_NO_UNUSED_WARNING (arg) = 1;
4639 return arg;
4640 }
4641
4642 if (TREE_CODE (TREE_TYPE (arg)) == FUNCTION_TYPE
4643 || TREE_CODE (TREE_TYPE (arg)) == METHOD_TYPE
4644 || TREE_CODE (TREE_TYPE (arg)) == OFFSET_TYPE)
4645 {
4646 /* The representation of something of type OFFSET_TYPE
4647 is really the representation of a pointer to it.
4648 Here give the representation its true type. */
4649 tree t;
4650
4651 my_friendly_assert (TREE_CODE (arg) != SCOPE_REF, 313);
4652
4653 if (TREE_CODE (arg) != OFFSET_REF)
4654 return 0;
4655
4656 t = TREE_OPERAND (arg, 1);
4657
4658 if (TREE_CODE (t) == FUNCTION_DECL) /* Check all this code for right semantics. */
4659 return build_unary_op (ADDR_EXPR, t, 0);
4660 if (TREE_CODE (t) == VAR_DECL)
4661 return build_unary_op (ADDR_EXPR, t, 0);
4662 else
4663 {
4664 tree type;
4665 tree offset = integer_zero_node;
4666
4667 if (TREE_OPERAND (arg, 0)
4668 && (TREE_CODE (TREE_OPERAND (arg, 0)) != NOP_EXPR
4669 || TREE_OPERAND (TREE_OPERAND (arg, 0), 0) != error_mark_node))
4670 if (TREE_CODE (t) != FIELD_DECL)
4671 {
4672 /* Don't know if this should return address to just
4673 _DECL, or actual address resolved in this expression. */
4674 sorry ("address of bound pointer-to-member expression");
4675 return error_mark_node;
4676 }
4677
4678 type = TREE_TYPE (TREE_OPERAND (arg, 0));
4679
4680 if (TREE_CODE (TREE_TYPE (arg)) == OFFSET_TYPE)
4681 {
4682 /* Add in the offset to the intermediate subobject, if any. */
4683 offset = get_delta_difference (TYPE_OFFSET_BASETYPE (TREE_TYPE (arg)),
4684 type,
4685 0);
4686 type = TYPE_OFFSET_BASETYPE (TREE_TYPE (arg));
4687 }
4688
4689 /* Now in the offset to the final subobject. */
4690 offset = size_binop (PLUS_EXPR,
4691 offset,
4692 get_delta_difference (DECL_FIELD_CONTEXT (t),
4693 type,
4694 0));
4695
4696 /* Add in the offset to the field. */
4697 offset = size_binop (PLUS_EXPR, offset,
4698 size_binop (EASY_DIV_EXPR,
4699 DECL_FIELD_BITPOS (t),
4700 size_int (BITS_PER_UNIT)));
4701
4702 /* We offset all pointer to data members by 1 so that we can
4703 distinguish between a null pointer to data member and the first
4704 data member of a structure. */
4705 offset = size_binop (PLUS_EXPR, offset, size_int (1));
4706
4707 return cp_convert (build_pointer_type (TREE_TYPE (arg)), offset);
4708 }
4709 }
4710
4711
4712 /* We permit compiler to make function calls returning
4713 objects of aggregate type look like lvalues. */
4714 {
4715 tree targ = arg;
4716
4717 if (TREE_CODE (targ) == SAVE_EXPR)
4718 targ = TREE_OPERAND (targ, 0);
4719
4720 if (TREE_CODE (targ) == CALL_EXPR && IS_AGGR_TYPE (TREE_TYPE (targ)))
4721 {
4722 if (TREE_CODE (arg) == SAVE_EXPR)
4723 targ = arg;
4724 else
4725 targ = build_cplus_new (TREE_TYPE (arg), arg);
4726 return build1 (ADDR_EXPR, build_pointer_type (TREE_TYPE (arg)), targ);
4727 }
4728
4729 if (TREE_CODE (arg) == SAVE_EXPR && TREE_CODE (targ) == INDIRECT_REF)
4730 return build (SAVE_EXPR, build_pointer_type (TREE_TYPE (arg)),
4731 TREE_OPERAND (targ, 0), current_function_decl, NULL);
4732 }
4733
4734 /* Don't let anything else be handled specially. */
4735 return 0;
4736 }
4737 \f
4738 /* Mark EXP saying that we need to be able to take the
4739 address of it; it should not be allocated in a register.
4740 Value is 1 if successful.
4741
4742 C++: we do not allow `current_class_ptr' to be addressable. */
4743
4744 int
4745 mark_addressable (exp)
4746 tree exp;
4747 {
4748 register tree x = exp;
4749
4750 if (TREE_ADDRESSABLE (x) == 1)
4751 return 1;
4752
4753 while (1)
4754 switch (TREE_CODE (x))
4755 {
4756 case ADDR_EXPR:
4757 case COMPONENT_REF:
4758 case ARRAY_REF:
4759 case REALPART_EXPR:
4760 case IMAGPART_EXPR:
4761 x = TREE_OPERAND (x, 0);
4762 break;
4763
4764 case PARM_DECL:
4765 if (x == current_class_ptr)
4766 {
4767 if (! flag_this_is_variable)
4768 error ("address of `this' not available");
4769 TREE_ADDRESSABLE (x) = 1; /* so compiler doesn't die later */
4770 put_var_into_stack (x);
4771 return 1;
4772 }
4773 case VAR_DECL:
4774 if (TREE_STATIC (x) && TREE_READONLY (x)
4775 && DECL_RTL (x) != 0
4776 && ! DECL_IN_MEMORY_P (x))
4777 {
4778 /* We thought this would make a good constant variable,
4779 but we were wrong. */
4780 push_obstacks_nochange ();
4781 end_temporary_allocation ();
4782
4783 TREE_ASM_WRITTEN (x) = 0;
4784 DECL_RTL (x) = 0;
4785 rest_of_decl_compilation (x, 0, IDENTIFIER_LOCAL_VALUE (x) == 0, 0);
4786 TREE_ADDRESSABLE (x) = 1;
4787
4788 pop_obstacks ();
4789
4790 return 1;
4791 }
4792 /* Caller should not be trying to mark initialized
4793 constant fields addressable. */
4794 my_friendly_assert (DECL_LANG_SPECIFIC (x) == 0
4795 || DECL_IN_AGGR_P (x) == 0
4796 || TREE_STATIC (x)
4797 || DECL_EXTERNAL (x), 314);
4798
4799 case CONST_DECL:
4800 case RESULT_DECL:
4801 if (DECL_REGISTER (x) && !TREE_ADDRESSABLE (x)
4802 && !DECL_ARTIFICIAL (x) && extra_warnings)
4803 cp_warning ("address requested for `%D', which is declared `register'",
4804 x);
4805 put_var_into_stack (x);
4806 TREE_ADDRESSABLE (x) = 1;
4807 return 1;
4808
4809 case FUNCTION_DECL:
4810 if (DECL_LANG_SPECIFIC (x) != 0)
4811 {
4812 x = DECL_MAIN_VARIANT (x);
4813 /* We have to test both conditions here. The first may be
4814 non-zero in the case of processing a default function. The
4815 second may be non-zero in the case of a template function. */
4816 if (DECL_TEMPLATE_INFO (x) && !DECL_TEMPLATE_SPECIALIZATION (x))
4817 mark_used (x);
4818 }
4819 TREE_ADDRESSABLE (x) = 1;
4820 TREE_USED (x) = 1;
4821 TREE_ADDRESSABLE (DECL_ASSEMBLER_NAME (x)) = 1;
4822 return 1;
4823
4824 case CONSTRUCTOR:
4825 TREE_ADDRESSABLE (x) = 1;
4826 return 1;
4827
4828 case TARGET_EXPR:
4829 TREE_ADDRESSABLE (x) = 1;
4830 mark_addressable (TREE_OPERAND (x, 0));
4831 return 1;
4832
4833 default:
4834 return 1;
4835 }
4836 }
4837 \f
4838 /* Build and return a conditional expression IFEXP ? OP1 : OP2. */
4839
4840 tree
4841 build_x_conditional_expr (ifexp, op1, op2)
4842 tree ifexp, op1, op2;
4843 {
4844 tree rval = NULL_TREE;
4845
4846 if (processing_template_decl)
4847 return build_min_nt (COND_EXPR, ifexp, op1, op2);
4848
4849 if (flag_ansi_overloading)
4850 return build_new_op (COND_EXPR, LOOKUP_NORMAL, ifexp, op1, op2);
4851
4852 /* See comments in `build_x_binary_op'. */
4853 if (op1 != 0)
4854 rval = build_opfncall (COND_EXPR, LOOKUP_SPECULATIVELY, ifexp, op1, op2);
4855 if (rval)
4856 return build_opfncall (COND_EXPR, LOOKUP_NORMAL, ifexp, op1, op2);
4857
4858 return build_conditional_expr (ifexp, op1, op2);
4859 }
4860
4861 tree
4862 build_conditional_expr (ifexp, op1, op2)
4863 tree ifexp, op1, op2;
4864 {
4865 register tree type1;
4866 register tree type2;
4867 register enum tree_code code1;
4868 register enum tree_code code2;
4869 register tree result_type = NULL_TREE;
4870
4871 /* If second operand is omitted, it is the same as the first one;
4872 make sure it is calculated only once. */
4873 if (op1 == 0)
4874 {
4875 if (pedantic)
4876 pedwarn ("ANSI C++ forbids omitting the middle term of a ?: expression");
4877 ifexp = op1 = save_expr (ifexp);
4878 }
4879
4880 ifexp = cp_convert (boolean_type_node, ifexp);
4881
4882 if (TREE_CODE (ifexp) == ERROR_MARK)
4883 return error_mark_node;
4884
4885 op1 = require_instantiated_type (TREE_TYPE (op2), op1, error_mark_node);
4886 if (op1 == error_mark_node)
4887 return error_mark_node;
4888 op2 = require_instantiated_type (TREE_TYPE (op1), op2, error_mark_node);
4889 if (op2 == error_mark_node)
4890 return error_mark_node;
4891
4892 /* C++: REFERENCE_TYPES must be dereferenced. */
4893 type1 = TREE_TYPE (op1);
4894 code1 = TREE_CODE (type1);
4895 type2 = TREE_TYPE (op2);
4896 code2 = TREE_CODE (type2);
4897
4898 if (code1 == REFERENCE_TYPE)
4899 {
4900 op1 = convert_from_reference (op1);
4901 type1 = TREE_TYPE (op1);
4902 code1 = TREE_CODE (type1);
4903 }
4904 if (code2 == REFERENCE_TYPE)
4905 {
4906 op2 = convert_from_reference (op2);
4907 type2 = TREE_TYPE (op2);
4908 code2 = TREE_CODE (type2);
4909 }
4910
4911 /* Don't promote the operands separately if they promote
4912 the same way. Return the unpromoted type and let the combined
4913 value get promoted if necessary. */
4914
4915 if (TYPE_MAIN_VARIANT (type1) == TYPE_MAIN_VARIANT (type2)
4916 && code2 != ARRAY_TYPE
4917 && code2 != FUNCTION_TYPE
4918 && code2 != METHOD_TYPE)
4919 {
4920 tree result;
4921
4922 if (TREE_CONSTANT (ifexp)
4923 && (TREE_CODE (ifexp) == INTEGER_CST
4924 || TREE_CODE (ifexp) == ADDR_EXPR))
4925 return (integer_zerop (ifexp) ? op2 : op1);
4926
4927 if (TREE_CODE (op1) == CONST_DECL)
4928 op1 = DECL_INITIAL (op1);
4929 else if (TREE_READONLY_DECL_P (op1))
4930 op1 = decl_constant_value (op1);
4931 if (TREE_CODE (op2) == CONST_DECL)
4932 op2 = DECL_INITIAL (op2);
4933 else if (TREE_READONLY_DECL_P (op2))
4934 op2 = decl_constant_value (op2);
4935 if (type1 != type2)
4936 type1 = cp_build_type_variant
4937 (type1,
4938 TREE_READONLY (op1) || TREE_READONLY (op2),
4939 TREE_THIS_VOLATILE (op1) || TREE_THIS_VOLATILE (op2));
4940 /* ??? This is a kludge to deal with the fact that
4941 we don't sort out integers and enums properly, yet. */
4942 result = fold (build (COND_EXPR, type1, ifexp, op1, op2));
4943 if (TREE_TYPE (result) != type1)
4944 result = build1 (NOP_EXPR, type1, result);
4945 /* Expand both sides into the same slot,
4946 hopefully the target of the ?: expression. */
4947 if (TREE_CODE (op1) == TARGET_EXPR && TREE_CODE (op2) == TARGET_EXPR)
4948 {
4949 tree slot = build (VAR_DECL, TREE_TYPE (result));
4950 layout_decl (slot, 0);
4951 result = build (TARGET_EXPR, TREE_TYPE (result),
4952 slot, result, NULL_TREE, NULL_TREE);
4953 }
4954 return result;
4955 }
4956
4957 /* They don't match; promote them both and then try to reconcile them.
4958 But don't permit mismatching enum types. */
4959 if (code1 == ENUMERAL_TYPE)
4960 {
4961 if (code2 == ENUMERAL_TYPE)
4962 {
4963 cp_error ("enumeral mismatch in conditional expression: `%T' vs `%T'", type1, type2);
4964 return error_mark_node;
4965 }
4966 else if (extra_warnings && ! IS_AGGR_TYPE_CODE (code2)
4967 && type2 != type_promotes_to (type1))
4968 warning ("enumeral and non-enumeral type in conditional expression");
4969 }
4970 else if (extra_warnings
4971 && code2 == ENUMERAL_TYPE && ! IS_AGGR_TYPE_CODE (code1)
4972 && type1 != type_promotes_to (type2))
4973 warning ("enumeral and non-enumeral type in conditional expression");
4974
4975 if (code1 != VOID_TYPE)
4976 {
4977 op1 = default_conversion (op1);
4978 type1 = TREE_TYPE (op1);
4979 if (TYPE_PTRMEMFUNC_P (type1))
4980 type1 = TYPE_PTRMEMFUNC_FN_TYPE (type1);
4981 code1 = TREE_CODE (type1);
4982 }
4983 if (code2 != VOID_TYPE)
4984 {
4985 op2 = default_conversion (op2);
4986 type2 = TREE_TYPE (op2);
4987 if (TYPE_PTRMEMFUNC_P (type2))
4988 type2 = TYPE_PTRMEMFUNC_FN_TYPE (type2);
4989 code2 = TREE_CODE (type2);
4990 }
4991
4992 if (code1 == RECORD_TYPE && code2 == RECORD_TYPE
4993 && real_lvalue_p (op1) && real_lvalue_p (op2)
4994 && comptypes (type1, type2, -1))
4995 {
4996 type1 = build_reference_type (type1);
4997 type2 = build_reference_type (type2);
4998 result_type = common_type (type1, type2);
4999 op1 = convert_to_reference (result_type, op1, CONV_IMPLICIT,
5000 LOOKUP_NORMAL, NULL_TREE);
5001 op2 = convert_to_reference (result_type, op2, CONV_IMPLICIT,
5002 LOOKUP_NORMAL, NULL_TREE);
5003 }
5004 /* Quickly detect the usual case where op1 and op2 have the same type
5005 after promotion. */
5006 else if (TYPE_MAIN_VARIANT (type1) == TYPE_MAIN_VARIANT (type2))
5007 {
5008 if (type1 == type2)
5009 result_type = type1;
5010 else
5011 result_type = cp_build_type_variant
5012 (type1,
5013 TREE_READONLY (op1) || TREE_READONLY (op2),
5014 TREE_THIS_VOLATILE (op1) || TREE_THIS_VOLATILE (op2));
5015 }
5016 else if ((code1 == INTEGER_TYPE || code1 == REAL_TYPE)
5017 && (code2 == INTEGER_TYPE || code2 == REAL_TYPE))
5018 {
5019 result_type = common_type (type1, type2);
5020 }
5021 else if (code1 == VOID_TYPE || code2 == VOID_TYPE)
5022 {
5023 if (pedantic && (code1 != VOID_TYPE || code2 != VOID_TYPE))
5024 pedwarn ("ANSI C++ forbids conditional expr with only one void side");
5025 result_type = void_type_node;
5026 }
5027 else if (code1 == POINTER_TYPE && null_ptr_cst_p (op2))
5028 result_type = qualify_type (type1, type2);
5029 else if (code2 == POINTER_TYPE && null_ptr_cst_p (op1))
5030 result_type = qualify_type (type2, type1);
5031 else if (code1 == POINTER_TYPE && code2 == POINTER_TYPE)
5032 {
5033 if (comp_target_types (type1, type2, 1))
5034 result_type = common_type (type1, type2);
5035 else if (TYPE_MAIN_VARIANT (TREE_TYPE (type1)) == void_type_node)
5036 {
5037 if (pedantic && TREE_CODE (type2) == FUNCTION_TYPE)
5038 pedwarn ("ANSI C++ forbids conditional expr between `void *' and function pointer");
5039 result_type = qualify_type (type1, type2);
5040 }
5041 else if (TYPE_MAIN_VARIANT (TREE_TYPE (type2)) == void_type_node)
5042 {
5043 if (pedantic && TREE_CODE (type1) == FUNCTION_TYPE)
5044 pedwarn ("ANSI C++ forbids conditional expr between `void *' and function pointer");
5045 result_type = qualify_type (type2, type1);
5046 }
5047 /* C++ */
5048 else if (comptypes (type2, type1, 0))
5049 result_type = type2;
5050 else if (IS_AGGR_TYPE (TREE_TYPE (type1))
5051 && IS_AGGR_TYPE (TREE_TYPE (type2))
5052 && (result_type = common_base_type (TREE_TYPE (type1), TREE_TYPE (type2))))
5053 {
5054 if (result_type == error_mark_node)
5055 {
5056 cp_error ("common base type of types `%T' and `%T' is ambiguous",
5057 TREE_TYPE (type1), TREE_TYPE (type2));
5058 result_type = ptr_type_node;
5059 }
5060 else
5061 {
5062 if (pedantic
5063 && result_type != TREE_TYPE (type1)
5064 && result_type != TREE_TYPE (type2))
5065 cp_pedwarn ("`%T' and `%T' converted to `%T *' in conditional expression",
5066 type1, type2, result_type);
5067
5068 result_type = build_pointer_type (result_type);
5069 }
5070 }
5071 else
5072 {
5073 pedwarn ("pointer type mismatch in conditional expression");
5074 result_type = ptr_type_node;
5075 }
5076 }
5077 else if (code1 == POINTER_TYPE && code2 == INTEGER_TYPE)
5078 {
5079 pedwarn ("pointer/integer type mismatch in conditional expression");
5080 result_type = type1;
5081 }
5082 else if (code2 == POINTER_TYPE && code1 == INTEGER_TYPE)
5083 {
5084 pedwarn ("pointer/integer type mismatch in conditional expression");
5085 result_type = type2;
5086 }
5087
5088 if (!result_type)
5089 {
5090 /* The match does not look good. If either is
5091 an aggregate value, try converting to a scalar type. */
5092 if (code1 == RECORD_TYPE && code2 == RECORD_TYPE)
5093 {
5094 cp_error ("aggregate mismatch in conditional expression: `%T' vs `%T'", type1, type2);
5095 return error_mark_node;
5096 }
5097 /* Warning: this code assumes that conversion between cv-variants of
5098 a type is done using NOP_EXPRs. */
5099 if (code1 == RECORD_TYPE && TYPE_HAS_CONVERSION (type1))
5100 {
5101 /* There are other types besides pointers and records. */
5102 tree tmp;
5103 if (code2 == POINTER_TYPE)
5104 tmp = build_pointer_type
5105 (build_type_variant (TREE_TYPE (type2), 1, 1));
5106 else
5107 tmp = type2;
5108 tmp = build_type_conversion (CONVERT_EXPR, tmp, op1, 0);
5109 if (tmp == NULL_TREE)
5110 {
5111 cp_error ("incompatible types `%T' and `%T' in `?:'",
5112 type1, type2);
5113 return error_mark_node;
5114 }
5115 if (tmp == error_mark_node)
5116 error ("ambiguous pointer conversion");
5117 else
5118 STRIP_NOPS (tmp);
5119 result_type = common_type (type2, TREE_TYPE (tmp));
5120 op1 = tmp;
5121 }
5122 else if (code2 == RECORD_TYPE && TYPE_HAS_CONVERSION (type2))
5123 {
5124 tree tmp;
5125 if (code1 == POINTER_TYPE)
5126 tmp = build_pointer_type
5127 (build_type_variant (TREE_TYPE (type1), 1, 1));
5128 else
5129 tmp = type1;
5130
5131 tmp = build_type_conversion (CONVERT_EXPR, tmp, op2, 0);
5132 if (tmp == NULL_TREE)
5133 {
5134 cp_error ("incompatible types `%T' and `%T' in `?:'",
5135 type1, type2);
5136 return error_mark_node;
5137 }
5138 if (tmp == error_mark_node)
5139 error ("ambiguous pointer conversion");
5140 else
5141 STRIP_NOPS (tmp);
5142 result_type = common_type (type1, TREE_TYPE (tmp));
5143 op2 = tmp;
5144 }
5145 else if (flag_cond_mismatch)
5146 result_type = void_type_node;
5147 else
5148 {
5149 error ("type mismatch in conditional expression");
5150 return error_mark_node;
5151 }
5152 }
5153
5154 if (TREE_CODE (result_type) == POINTER_TYPE
5155 && TREE_CODE (TREE_TYPE (result_type)) == METHOD_TYPE)
5156 result_type = build_ptrmemfunc_type (result_type);
5157
5158 if (result_type != TREE_TYPE (op1))
5159 op1 = convert_for_initialization
5160 (NULL_TREE, result_type, op1, LOOKUP_NORMAL, "converting", NULL_TREE, 0);
5161 if (result_type != TREE_TYPE (op2))
5162 op2 = convert_for_initialization
5163 (NULL_TREE, result_type, op2, LOOKUP_NORMAL, "converting", NULL_TREE, 0);
5164
5165 if (TREE_CONSTANT (ifexp))
5166 return integer_zerop (ifexp) ? op2 : op1;
5167
5168 return convert_from_reference
5169 (fold (build (COND_EXPR, result_type, ifexp, op1, op2)));
5170 }
5171 \f
5172 /* Handle overloading of the ',' operator when needed. Otherwise,
5173 this function just builds an expression list. */
5174
5175 tree
5176 build_x_compound_expr (list)
5177 tree list;
5178 {
5179 tree rest = TREE_CHAIN (list);
5180 tree result;
5181
5182 if (processing_template_decl)
5183 return build_min_nt (COMPOUND_EXPR, list, NULL_TREE);
5184
5185 if (rest == NULL_TREE)
5186 return build_compound_expr (list);
5187
5188 result = build_opfncall (COMPOUND_EXPR, LOOKUP_NORMAL,
5189 TREE_VALUE (list), TREE_VALUE (rest), NULL_TREE);
5190 if (result)
5191 return build_x_compound_expr (expr_tree_cons (NULL_TREE, result, TREE_CHAIN (rest)));
5192
5193 if (! TREE_SIDE_EFFECTS (TREE_VALUE (list)))
5194 {
5195 /* the left-hand operand of a comma expression is like an expression
5196 statement: we should warn if it doesn't have any side-effects,
5197 unless it was explicitly cast to (void). */
5198 if ((extra_warnings || warn_unused)
5199 && !(TREE_CODE (TREE_VALUE(list)) == CONVERT_EXPR
5200 && TREE_TYPE (TREE_VALUE(list)) == void_type_node))
5201 warning("left-hand operand of comma expression has no effect");
5202 }
5203 #if 0 /* this requires a gcc backend patch to export warn_if_unused_value */
5204 else if (warn_unused)
5205 warn_if_unused_value (TREE_VALUE(list));
5206 #endif
5207
5208 return build_compound_expr (expr_tree_cons (NULL_TREE, TREE_VALUE (list),
5209 build_expr_list (NULL_TREE, build_x_compound_expr (rest))));
5210 }
5211
5212 /* Given a list of expressions, return a compound expression
5213 that performs them all and returns the value of the last of them. */
5214
5215 tree
5216 build_compound_expr (list)
5217 tree list;
5218 {
5219 register tree rest;
5220
5221 if (TREE_READONLY_DECL_P (TREE_VALUE (list)))
5222 TREE_VALUE (list) = decl_constant_value (TREE_VALUE (list));
5223
5224 if (TREE_CHAIN (list) == 0)
5225 {
5226 /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
5227 Strip such NOP_EXPRs, since LIST is used in non-lvalue context. */
5228 if (TREE_CODE (list) == NOP_EXPR
5229 && TREE_TYPE (list) == TREE_TYPE (TREE_OPERAND (list, 0)))
5230 list = TREE_OPERAND (list, 0);
5231
5232 /* Convert arrays to pointers. */
5233 if (TREE_CODE (TREE_TYPE (TREE_VALUE (list))) == ARRAY_TYPE)
5234 return default_conversion (TREE_VALUE (list));
5235 else
5236 return TREE_VALUE (list);
5237 }
5238
5239 rest = build_compound_expr (TREE_CHAIN (list));
5240
5241 /* When pedantic, a compound expression cannot be a constant expression. */
5242 if (! TREE_SIDE_EFFECTS (TREE_VALUE (list)) && ! pedantic)
5243 return rest;
5244
5245 return build (COMPOUND_EXPR, TREE_TYPE (rest),
5246 break_out_cleanups (TREE_VALUE (list)), rest);
5247 }
5248
5249 tree
5250 build_static_cast (type, expr)
5251 tree type, expr;
5252 {
5253 tree intype, binfo;
5254 int ok;
5255
5256 if (type == error_mark_node || expr == error_mark_node)
5257 return error_mark_node;
5258
5259 if (TREE_CODE (expr) == OFFSET_REF)
5260 expr = resolve_offset_ref (expr);
5261
5262 if (processing_template_decl)
5263 {
5264 tree t = build_min (STATIC_CAST_EXPR, type, expr);
5265 return t;
5266 }
5267
5268 /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
5269 Strip such NOP_EXPRs if VALUE is being used in non-lvalue context. */
5270 if (TREE_CODE (type) != REFERENCE_TYPE
5271 && TREE_CODE (expr) == NOP_EXPR
5272 && TREE_TYPE (expr) == TREE_TYPE (TREE_OPERAND (expr, 0)))
5273 expr = TREE_OPERAND (expr, 0);
5274
5275 if (TREE_CODE (type) == VOID_TYPE)
5276 return build1 (CONVERT_EXPR, type, expr);
5277
5278 if (type_unknown_p (expr))
5279 {
5280 expr = instantiate_type (type, expr, 1);
5281 if (expr == error_mark_node)
5282 return error_mark_node;
5283 }
5284
5285 if (TREE_CODE (type) == REFERENCE_TYPE)
5286 return (convert_from_reference
5287 (convert_to_reference (type, expr, CONV_STATIC|CONV_IMPLICIT,
5288 LOOKUP_COMPLAIN, NULL_TREE)));
5289
5290 if (IS_AGGR_TYPE (type))
5291 return build_cplus_new
5292 (type, (build_method_call
5293 (NULL_TREE, ctor_identifier, build_expr_list (NULL_TREE, expr),
5294 TYPE_BINFO (type), LOOKUP_NORMAL)));
5295
5296 expr = decay_conversion (expr);
5297 intype = TREE_TYPE (expr);
5298
5299 /* FIXME handle casting to array type. */
5300
5301 ok = 0;
5302 if (can_convert_arg (type, intype, expr))
5303 ok = 1;
5304 else if (TYPE_PTROB_P (type) && TYPE_PTROB_P (intype))
5305 {
5306 tree binfo;
5307 if (IS_AGGR_TYPE (TREE_TYPE (type)) && IS_AGGR_TYPE (TREE_TYPE (intype))
5308 && (TYPE_READONLY (TREE_TYPE (type))
5309 >= TYPE_READONLY (TREE_TYPE (intype)))
5310 && (TYPE_VOLATILE (TREE_TYPE (type))
5311 >= TYPE_VOLATILE (TREE_TYPE (intype)))
5312 && (binfo = get_binfo (TREE_TYPE (intype), TREE_TYPE (type), 0))
5313 && ! TREE_VIA_VIRTUAL (binfo))
5314 ok = 1;
5315 }
5316 else if (TYPE_PTRMEM_P (type) && TYPE_PTRMEM_P (intype))
5317 {
5318 if (comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (type))),
5319 TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (intype))), 1)
5320 && (TYPE_READONLY (TREE_TYPE (TREE_TYPE (type)))
5321 >= TYPE_READONLY (TREE_TYPE (TREE_TYPE (intype))))
5322 && (TYPE_VOLATILE (TREE_TYPE (TREE_TYPE (type)))
5323 >= TYPE_VOLATILE (TREE_TYPE (TREE_TYPE (intype))))
5324 && (binfo = get_binfo (TYPE_OFFSET_BASETYPE (intype),
5325 TYPE_OFFSET_BASETYPE (type), 0))
5326 && ! TREE_VIA_VIRTUAL (binfo))
5327 ok = 1;
5328 }
5329 else if (TREE_CODE (intype) != BOOLEAN_TYPE
5330 && TREE_CODE (type) != ARRAY_TYPE
5331 && TREE_CODE (type) != FUNCTION_TYPE
5332 && can_convert (intype, type))
5333 ok = 1;
5334
5335 if (ok)
5336 return build_c_cast (type, expr);
5337
5338 cp_error ("static_cast from `%T' to `%T'", intype, type);
5339 return error_mark_node;
5340 }
5341
5342 tree
5343 build_reinterpret_cast (type, expr)
5344 tree type, expr;
5345 {
5346 tree intype;
5347
5348 if (type == error_mark_node || expr == error_mark_node)
5349 return error_mark_node;
5350
5351 if (TREE_CODE (expr) == OFFSET_REF)
5352 expr = resolve_offset_ref (expr);
5353
5354 if (processing_template_decl)
5355 {
5356 tree t = build_min (REINTERPRET_CAST_EXPR, type, expr);
5357 return t;
5358 }
5359
5360 if (TREE_CODE (type) != REFERENCE_TYPE)
5361 {
5362 expr = decay_conversion (expr);
5363
5364 /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
5365 Strip such NOP_EXPRs if VALUE is being used in non-lvalue context. */
5366 if (TREE_CODE (expr) == NOP_EXPR
5367 && TREE_TYPE (expr) == TREE_TYPE (TREE_OPERAND (expr, 0)))
5368 expr = TREE_OPERAND (expr, 0);
5369 }
5370
5371 if (type_unknown_p (expr))
5372 {
5373 expr = instantiate_type (type, expr, 1);
5374 if (expr == error_mark_node)
5375 return error_mark_node;
5376 }
5377
5378 intype = TREE_TYPE (expr);
5379
5380 if (TREE_CODE (type) == REFERENCE_TYPE)
5381 {
5382 if (! real_lvalue_p (expr))
5383 {
5384 cp_error ("reinterpret_cast from `%T' rvalue to `%T'", intype, type);
5385 return error_mark_node;
5386 }
5387 expr = build_unary_op (ADDR_EXPR, expr, 0);
5388 if (expr != error_mark_node)
5389 expr = build_reinterpret_cast
5390 (build_pointer_type (TREE_TYPE (type)), expr);
5391 if (expr != error_mark_node)
5392 expr = build_indirect_ref (expr, 0);
5393 return expr;
5394 }
5395 else if (comptypes (TYPE_MAIN_VARIANT (intype), TYPE_MAIN_VARIANT (type), 1))
5396 return build_static_cast (type, expr);
5397
5398 if (TYPE_PTR_P (type) && (TREE_CODE (intype) == INTEGER_TYPE
5399 || TREE_CODE (intype) == ENUMERAL_TYPE))
5400 /* OK */;
5401 else if (TREE_CODE (type) == INTEGER_TYPE && TYPE_PTR_P (intype))
5402 {
5403 if (TYPE_PRECISION (type) < TYPE_PRECISION (intype))
5404 cp_pedwarn ("reinterpret_cast from `%T' to `%T' loses precision",
5405 intype, type);
5406 }
5407 else if ((TYPE_PTRFN_P (type) && TYPE_PTRFN_P (intype))
5408 || (TYPE_PTRMEMFUNC_P (type) && TYPE_PTRMEMFUNC_P (intype)))
5409 {
5410 if (TREE_READONLY_DECL_P (expr))
5411 expr = decl_constant_value (expr);
5412 return fold (build1 (NOP_EXPR, type, expr));
5413 }
5414 else if ((TYPE_PTRMEM_P (type) && TYPE_PTRMEM_P (intype))
5415 || (TYPE_PTROBV_P (type) && TYPE_PTROBV_P (intype)))
5416 {
5417 if (! comp_ptr_ttypes_reinterpret (TREE_TYPE (type), TREE_TYPE (intype)))
5418 cp_pedwarn ("reinterpret_cast from `%T' to `%T' casts away const (or volatile)",
5419 intype, type);
5420
5421 if (TREE_READONLY_DECL_P (expr))
5422 expr = decl_constant_value (expr);
5423 return fold (build1 (NOP_EXPR, type, expr));
5424 }
5425 else if ((TYPE_PTRFN_P (type) && TYPE_PTROBV_P (intype))
5426 || (TYPE_PTRFN_P (type) && TYPE_PTROBV_P (intype)))
5427 {
5428 pedwarn ("ANSI C++ forbids casting between pointers to functions and objects");
5429 if (TREE_READONLY_DECL_P (expr))
5430 expr = decl_constant_value (expr);
5431 return fold (build1 (NOP_EXPR, type, expr));
5432 }
5433 else
5434 {
5435 cp_error ("reinterpret_cast from `%T' to `%T'", intype, type);
5436 return error_mark_node;
5437 }
5438
5439 return cp_convert (type, expr);
5440 }
5441
5442 tree
5443 build_const_cast (type, expr)
5444 tree type, expr;
5445 {
5446 tree intype;
5447
5448 if (type == error_mark_node || expr == error_mark_node)
5449 return error_mark_node;
5450
5451 if (TREE_CODE (expr) == OFFSET_REF)
5452 expr = resolve_offset_ref (expr);
5453
5454 if (processing_template_decl)
5455 {
5456 tree t = build_min (CONST_CAST_EXPR, type, expr);
5457 return t;
5458 }
5459
5460 if (TREE_CODE (type) != REFERENCE_TYPE)
5461 {
5462 expr = decay_conversion (expr);
5463
5464 /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
5465 Strip such NOP_EXPRs if VALUE is being used in non-lvalue context. */
5466 if (TREE_CODE (expr) == NOP_EXPR
5467 && TREE_TYPE (expr) == TREE_TYPE (TREE_OPERAND (expr, 0)))
5468 expr = TREE_OPERAND (expr, 0);
5469 }
5470
5471 if (type_unknown_p (expr))
5472 {
5473 expr = instantiate_type (type, expr, 1);
5474 if (expr == error_mark_node)
5475 return error_mark_node;
5476 }
5477
5478 intype = TREE_TYPE (expr);
5479
5480 if (comptypes (TYPE_MAIN_VARIANT (intype), TYPE_MAIN_VARIANT (type), 1))
5481 return build_static_cast (type, expr);
5482 else if (TREE_CODE (type) == REFERENCE_TYPE)
5483 {
5484 if (! real_lvalue_p (expr))
5485 {
5486 cp_error ("const_cast from `%T' rvalue to `%T'", intype, type);
5487 return error_mark_node;
5488 }
5489
5490 if (comp_ptr_ttypes_const (TREE_TYPE (type), intype))
5491 return (convert_from_reference
5492 (convert_to_reference (type, expr, CONV_CONST|CONV_IMPLICIT,
5493 LOOKUP_COMPLAIN, NULL_TREE)));
5494 }
5495 else if (TREE_CODE (type) == POINTER_TYPE
5496 && TREE_CODE (intype) == POINTER_TYPE
5497 && comp_ptr_ttypes_const (TREE_TYPE (type), TREE_TYPE (intype)))
5498 return cp_convert (type, expr);
5499
5500 cp_error ("const_cast from `%T' to `%T'", intype, type);
5501 return error_mark_node;
5502 }
5503
5504 /* Build an expression representing a cast to type TYPE of expression EXPR.
5505
5506 ALLOW_NONCONVERTING is true if we should allow non-converting constructors
5507 when doing the cast. */
5508
5509 tree
5510 build_c_cast (type, expr)
5511 tree type, expr;
5512 {
5513 register tree value = expr;
5514
5515 if (type == error_mark_node || expr == error_mark_node)
5516 return error_mark_node;
5517
5518 /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
5519 Strip such NOP_EXPRs if VALUE is being used in non-lvalue context. */
5520 if (TREE_CODE (type) != REFERENCE_TYPE
5521 && TREE_CODE (value) == NOP_EXPR
5522 && TREE_TYPE (value) == TREE_TYPE (TREE_OPERAND (value, 0)))
5523 value = TREE_OPERAND (value, 0);
5524
5525 if (TREE_TYPE (expr)
5526 && TREE_CODE (TREE_TYPE (expr)) == OFFSET_TYPE
5527 && TREE_CODE (type) != OFFSET_TYPE)
5528 value = resolve_offset_ref (value);
5529
5530 if (TREE_CODE (type) == ARRAY_TYPE)
5531 {
5532 /* Allow casting from T1* to T2[] because Cfront allows it.
5533 NIHCL uses it. It is not valid ANSI C however, and hence, not
5534 valid ANSI C++. */
5535 if (TREE_CODE (TREE_TYPE (expr)) == POINTER_TYPE)
5536 {
5537 if (pedantic)
5538 pedwarn ("ANSI C++ forbids casting to an array type");
5539 type = build_pointer_type (TREE_TYPE (type));
5540 }
5541 else
5542 {
5543 error ("ANSI C++ forbids casting to an array type");
5544 return error_mark_node;
5545 }
5546 }
5547
5548 if (TREE_CODE (type) == FUNCTION_TYPE
5549 || TREE_CODE (type) == METHOD_TYPE)
5550 {
5551 cp_error ("casting to function type `%T'", type);
5552 return error_mark_node;
5553 }
5554
5555 if (IS_SIGNATURE (type))
5556 {
5557 error ("cast specifies signature type");
5558 return error_mark_node;
5559 }
5560
5561 if (processing_template_decl)
5562 {
5563 tree t = build_min (CAST_EXPR, type,
5564 min_tree_cons (NULL_TREE, value, NULL_TREE));
5565 return t;
5566 }
5567
5568 if (TREE_CODE (type) == VOID_TYPE)
5569 value = build1 (CONVERT_EXPR, type, value);
5570 else if (TREE_TYPE (value) == NULL_TREE
5571 || type_unknown_p (value))
5572 {
5573 value = instantiate_type (type, value, 1);
5574 /* Did we lose? */
5575 if (value == error_mark_node)
5576 return error_mark_node;
5577 }
5578 else
5579 {
5580 tree otype;
5581
5582 /* Convert functions and arrays to pointers and
5583 convert references to their expanded types,
5584 but don't convert any other types. */
5585 if (TREE_CODE (TREE_TYPE (value)) == FUNCTION_TYPE
5586 || (TREE_CODE (TREE_TYPE (value)) == METHOD_TYPE
5587 /* Don't do the default conversion if we want a
5588 pointer to a function. */
5589 && ! (TREE_CODE (type) == POINTER_TYPE
5590 && TREE_CODE (TREE_TYPE (type)) == FUNCTION_TYPE))
5591 || TREE_CODE (TREE_TYPE (value)) == ARRAY_TYPE
5592 || TREE_CODE (TREE_TYPE (value)) == REFERENCE_TYPE)
5593 value = default_conversion (value);
5594 otype = TREE_TYPE (value);
5595
5596 /* Optionally warn about potentially worrisome casts. */
5597
5598 if (warn_cast_qual
5599 && TREE_CODE (type) == POINTER_TYPE
5600 && TREE_CODE (otype) == POINTER_TYPE)
5601 {
5602 /* For C++ we make these regular warnings, rather than
5603 softening them into pedwarns. */
5604 if (TYPE_VOLATILE (TREE_TYPE (otype))
5605 && ! TYPE_VOLATILE (TREE_TYPE (type)))
5606 warning ("cast discards `volatile' from pointer target type");
5607 if (TYPE_READONLY (TREE_TYPE (otype))
5608 && ! TYPE_READONLY (TREE_TYPE (type)))
5609 warning ("cast discards `const' from pointer target type");
5610 }
5611
5612 /* Warn about possible alignment problems. */
5613 if (STRICT_ALIGNMENT && warn_cast_align
5614 && TREE_CODE (type) == POINTER_TYPE
5615 && TREE_CODE (otype) == POINTER_TYPE
5616 && TREE_CODE (TREE_TYPE (otype)) != VOID_TYPE
5617 && TREE_CODE (TREE_TYPE (otype)) != FUNCTION_TYPE
5618 && TYPE_ALIGN (TREE_TYPE (type)) > TYPE_ALIGN (TREE_TYPE (otype)))
5619 warning ("cast increases required alignment of target type");
5620
5621 #if 0
5622 /* We should see about re-enabling these, they seem useful to
5623 me. */
5624 if (TREE_CODE (type) == INTEGER_TYPE
5625 && TREE_CODE (otype) == POINTER_TYPE
5626 && TYPE_PRECISION (type) != TYPE_PRECISION (otype))
5627 warning ("cast from pointer to integer of different size");
5628
5629 if (TREE_CODE (type) == POINTER_TYPE
5630 && TREE_CODE (otype) == INTEGER_TYPE
5631 && TYPE_PRECISION (type) != TYPE_PRECISION (otype)
5632 /* Don't warn about converting 0 to pointer,
5633 provided the 0 was explicit--not cast or made by folding. */
5634 && !(TREE_CODE (value) == INTEGER_CST && integer_zerop (value)))
5635 warning ("cast to pointer from integer of different size");
5636 #endif
5637
5638 if (TREE_CODE (type) == REFERENCE_TYPE)
5639 value = (convert_from_reference
5640 (convert_to_reference (type, value, CONV_C_CAST,
5641 LOOKUP_COMPLAIN, NULL_TREE)));
5642 else
5643 {
5644 tree ovalue;
5645
5646 if (TREE_READONLY_DECL_P (value))
5647 value = decl_constant_value (value);
5648
5649 ovalue = value;
5650 value = convert_force (type, value, CONV_C_CAST);
5651
5652 /* Ignore any integer overflow caused by the cast. */
5653 if (TREE_CODE (value) == INTEGER_CST)
5654 {
5655 TREE_OVERFLOW (value) = TREE_OVERFLOW (ovalue);
5656 TREE_CONSTANT_OVERFLOW (value) = TREE_CONSTANT_OVERFLOW (ovalue);
5657 }
5658 }
5659 }
5660
5661 /* Always produce some operator for an explicit cast,
5662 so we can tell (for -pedantic) that the cast is no lvalue. */
5663 if (TREE_CODE (type) != REFERENCE_TYPE && value == expr
5664 && real_lvalue_p (value))
5665 value = non_lvalue (value);
5666
5667 return value;
5668 }
5669 \f
5670 static tree
5671 expand_target_expr (t)
5672 tree t;
5673 {
5674 extern int temp_slot_level;
5675 extern int target_temp_slot_level;
5676 int old_temp_level = target_temp_slot_level;
5677
5678 tree xval = make_node (RTL_EXPR);
5679 rtx rtxval;
5680
5681 /* Any TARGET_EXPR temps live only as long as the outer temp level.
5682 Since they are preserved in this new inner level, we know they
5683 will make it into the outer level. */
5684 push_temp_slots ();
5685 target_temp_slot_level = temp_slot_level;
5686
5687 do_pending_stack_adjust ();
5688 start_sequence_for_rtl_expr (xval);
5689 emit_note (0, -1);
5690 rtxval = expand_expr (t, NULL_RTX, VOIDmode, EXPAND_NORMAL);
5691 do_pending_stack_adjust ();
5692 TREE_SIDE_EFFECTS (xval) = 1;
5693 RTL_EXPR_SEQUENCE (xval) = get_insns ();
5694 end_sequence ();
5695 RTL_EXPR_RTL (xval) = rtxval;
5696 TREE_TYPE (xval) = TREE_TYPE (t);
5697
5698 pop_temp_slots ();
5699 target_temp_slot_level = old_temp_level;
5700
5701 return xval;
5702 }
5703
5704 /* Build an assignment expression of lvalue LHS from value RHS.
5705 MODIFYCODE is the code for a binary operator that we use
5706 to combine the old value of LHS with RHS to get the new value.
5707 Or else MODIFYCODE is NOP_EXPR meaning do a simple assignment.
5708
5709 C++: If MODIFYCODE is INIT_EXPR, then leave references unbashed. */
5710
5711 tree
5712 build_modify_expr (lhs, modifycode, rhs)
5713 tree lhs;
5714 enum tree_code modifycode;
5715 tree rhs;
5716 {
5717 register tree result;
5718 tree newrhs = rhs;
5719 tree lhstype = TREE_TYPE (lhs);
5720 tree olhstype = lhstype;
5721 tree olhs = lhs;
5722
5723 /* Avoid duplicate error messages from operands that had errors. */
5724 if (lhs == error_mark_node || rhs == error_mark_node)
5725 return error_mark_node;
5726
5727 /* Types that aren't fully specified cannot be used in assignments. */
5728 lhs = require_complete_type (lhs);
5729
5730 newrhs = rhs;
5731
5732 /* Handle assignment to signature pointers/refs. */
5733
5734 if (TYPE_LANG_SPECIFIC (lhstype)
5735 && (IS_SIGNATURE_POINTER (lhstype) || IS_SIGNATURE_REFERENCE (lhstype)))
5736 {
5737 return build_signature_pointer_constructor (lhs, rhs);
5738 }
5739
5740 /* Handle control structure constructs used as "lvalues". */
5741
5742 switch (TREE_CODE (lhs))
5743 {
5744 /* Handle --foo = 5; as these are valid constructs in C++ */
5745 case PREDECREMENT_EXPR:
5746 case PREINCREMENT_EXPR:
5747 if (TREE_SIDE_EFFECTS (TREE_OPERAND (lhs, 0)))
5748 lhs = build (TREE_CODE (lhs), TREE_TYPE (lhs),
5749 stabilize_reference (TREE_OPERAND (lhs, 0)),
5750 TREE_OPERAND (lhs, 1));
5751 return build (COMPOUND_EXPR, lhstype,
5752 lhs,
5753 build_modify_expr (TREE_OPERAND (lhs, 0),
5754 modifycode, rhs));
5755
5756 /* Handle (a, b) used as an "lvalue". */
5757 case COMPOUND_EXPR:
5758 newrhs = build_modify_expr (TREE_OPERAND (lhs, 1),
5759 modifycode, rhs);
5760 if (newrhs == error_mark_node)
5761 return error_mark_node;
5762 return build (COMPOUND_EXPR, lhstype,
5763 TREE_OPERAND (lhs, 0), newrhs);
5764
5765 case MODIFY_EXPR:
5766 newrhs = build_modify_expr (TREE_OPERAND (lhs, 0), modifycode, rhs);
5767 if (newrhs == error_mark_node)
5768 return error_mark_node;
5769 return build (COMPOUND_EXPR, lhstype, lhs, newrhs);
5770
5771 /* Handle (a ? b : c) used as an "lvalue". */
5772 case COND_EXPR:
5773 rhs = save_expr (rhs);
5774 {
5775 /* Produce (a ? (b = rhs) : (c = rhs))
5776 except that the RHS goes through a save-expr
5777 so the code to compute it is only emitted once. */
5778 tree cond
5779 = build_conditional_expr (TREE_OPERAND (lhs, 0),
5780 build_modify_expr (cp_convert (TREE_TYPE (lhs), TREE_OPERAND (lhs, 1)),
5781 modifycode, rhs),
5782 build_modify_expr (cp_convert (TREE_TYPE (lhs), TREE_OPERAND (lhs, 2)),
5783 modifycode, rhs));
5784 if (cond == error_mark_node)
5785 return cond;
5786 /* Make sure the code to compute the rhs comes out
5787 before the split. */
5788 return build (COMPOUND_EXPR, TREE_TYPE (lhs),
5789 /* Case to void to suppress warning
5790 from warn_if_unused_value. */
5791 cp_convert (void_type_node, rhs), cond);
5792 }
5793 }
5794
5795 if (TREE_CODE (lhs) == OFFSET_REF)
5796 {
5797 if (TREE_OPERAND (lhs, 0) == NULL_TREE)
5798 {
5799 /* Static class member? */
5800 tree member = TREE_OPERAND (lhs, 1);
5801 if (TREE_CODE (member) == VAR_DECL)
5802 lhs = member;
5803 else
5804 {
5805 compiler_error ("invalid static class member");
5806 return error_mark_node;
5807 }
5808 }
5809 else
5810 lhs = resolve_offset_ref (lhs);
5811
5812 olhstype = lhstype = TREE_TYPE (lhs);
5813 }
5814
5815 if (TREE_CODE (lhstype) == REFERENCE_TYPE
5816 && modifycode != INIT_EXPR)
5817 {
5818 lhs = convert_from_reference (lhs);
5819 olhstype = lhstype = TREE_TYPE (lhs);
5820 }
5821
5822 /* If a binary op has been requested, combine the old LHS value with the RHS
5823 producing the value we should actually store into the LHS. */
5824
5825 if (modifycode == INIT_EXPR)
5826 {
5827 if (! IS_AGGR_TYPE (lhstype))
5828 /* Do the default thing */;
5829 else if (! TYPE_HAS_CONSTRUCTOR (lhstype))
5830 {
5831 cp_error ("`%T' has no constructors", lhstype);
5832 return error_mark_node;
5833 }
5834 else if (TYPE_HAS_TRIVIAL_INIT_REF (lhstype)
5835 && TYPE_MAIN_VARIANT (lhstype) == TYPE_MAIN_VARIANT (TREE_TYPE (newrhs)))
5836 /* Do the default thing */;
5837 else
5838 {
5839 result = build_method_call (lhs, ctor_identifier,
5840 build_expr_list (NULL_TREE, rhs),
5841 TYPE_BINFO (lhstype), LOOKUP_NORMAL);
5842 if (result == NULL_TREE)
5843 return error_mark_node;
5844 return result;
5845 }
5846 }
5847 else if (modifycode == NOP_EXPR)
5848 {
5849 /* `operator=' is not an inheritable operator. */
5850 if (! IS_AGGR_TYPE (lhstype))
5851 /* Do the default thing */;
5852 else if (! TYPE_HAS_ASSIGNMENT (lhstype))
5853 {
5854 cp_error ("`%T' does not define operator=", lhstype);
5855 return error_mark_node;
5856 }
5857 else if (TYPE_HAS_TRIVIAL_ASSIGN_REF (lhstype)
5858 && TYPE_MAIN_VARIANT (lhstype) == TYPE_MAIN_VARIANT (TREE_TYPE (newrhs)))
5859 {
5860 build_opfncall (MODIFY_EXPR, LOOKUP_NORMAL,
5861 lhs, rhs, make_node (NOP_EXPR));
5862
5863 /* Do the default thing */;
5864 }
5865 else
5866 {
5867 result = build_opfncall (MODIFY_EXPR, LOOKUP_NORMAL,
5868 lhs, rhs, make_node (NOP_EXPR));
5869 if (result == NULL_TREE)
5870 return error_mark_node;
5871 return result;
5872 }
5873 lhstype = olhstype;
5874 }
5875 else if (PROMOTES_TO_AGGR_TYPE (lhstype, REFERENCE_TYPE))
5876 {
5877 /* This case must convert to some sort of lvalue that
5878 can participate in an op= operation. */
5879 tree lhs_tmp = lhs;
5880 tree rhs_tmp = rhs;
5881 if (build_default_binary_type_conversion (modifycode, &lhs_tmp, &rhs_tmp))
5882 {
5883 lhs = stabilize_reference (lhs_tmp);
5884 /* Forget it was ever anything else. */
5885 olhstype = lhstype = TREE_TYPE (lhs);
5886 newrhs = build_binary_op (modifycode, lhs, rhs_tmp, 1);
5887 }
5888 else
5889 {
5890 cp_error ("no match for `%Q(%#T, %#T)'", modifycode,
5891 TREE_TYPE (lhs), TREE_TYPE (rhs));
5892 return error_mark_node;
5893 }
5894 }
5895 else
5896 {
5897 lhs = stabilize_reference (lhs);
5898 newrhs = build_binary_op (modifycode, lhs, rhs, 1);
5899 if (newrhs == error_mark_node)
5900 {
5901 cp_error (" in evaluation of `%Q(%#T, %#T)'", modifycode,
5902 TREE_TYPE (lhs), TREE_TYPE (rhs));
5903 return error_mark_node;
5904 }
5905 }
5906
5907 /* Handle a cast used as an "lvalue".
5908 We have already performed any binary operator using the value as cast.
5909 Now convert the result to the cast type of the lhs,
5910 and then true type of the lhs and store it there;
5911 then convert result back to the cast type to be the value
5912 of the assignment. */
5913
5914 switch (TREE_CODE (lhs))
5915 {
5916 case NOP_EXPR:
5917 case CONVERT_EXPR:
5918 case FLOAT_EXPR:
5919 case FIX_TRUNC_EXPR:
5920 case FIX_FLOOR_EXPR:
5921 case FIX_ROUND_EXPR:
5922 case FIX_CEIL_EXPR:
5923 if (TREE_CODE (TREE_TYPE (newrhs)) == ARRAY_TYPE
5924 || TREE_CODE (TREE_TYPE (newrhs)) == FUNCTION_TYPE
5925 || TREE_CODE (TREE_TYPE (newrhs)) == METHOD_TYPE
5926 || TREE_CODE (TREE_TYPE (newrhs)) == OFFSET_TYPE)
5927 newrhs = default_conversion (newrhs);
5928 {
5929 tree inner_lhs = TREE_OPERAND (lhs, 0);
5930 tree result;
5931
5932 /* WP 5.4.1: The result is an lvalue if T is a reference type,
5933 otherwise the result is an rvalue. */
5934 if (! lvalue_p (lhs))
5935 pedwarn ("ANSI C++ forbids cast to non-reference type used as lvalue");
5936
5937 result = build_modify_expr (inner_lhs, NOP_EXPR,
5938 cp_convert (TREE_TYPE (inner_lhs),
5939 cp_convert (lhstype, newrhs)));
5940 if (result == error_mark_node)
5941 return result;
5942 return cp_convert (TREE_TYPE (lhs), result);
5943 }
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 conversiona");
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 NEW_EXPR. */
7312 else if (! current_function_returns_struct
7313 && TREE_CODE (retval) == TARGET_EXPR
7314 && TREE_CODE (TREE_OPERAND (retval, 1)) == NEW_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) == NEW_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 }