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