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