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