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