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