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