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