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