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