PR c++/3637
[official-gcc.git] / gcc / cp / typeck.c
blobe57dd1cc18d77cb0368e374763554c2010ead673
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)
6 This file is part of GNU CC.
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.
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.
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. */
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.
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). */
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"
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));
69 /* Return the target type of TYPE, which means return T for:
70 T*, T&, T[], T (...), and otherwise, just T. */
72 tree
73 target_type (type)
74 tree type;
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;
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. */
92 tree
93 require_complete_type (value)
94 tree value;
96 tree type;
98 if (processing_template_decl || value == error_mark_node)
99 return value;
101 if (TREE_CODE (value) == OVERLOAD)
102 type = unknown_type_node;
103 else
104 type = TREE_TYPE (value);
106 /* First, detect a valid value with a complete type. */
107 if (COMPLETE_TYPE_P (type))
108 return value;
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)
117 tree base, member = TREE_OPERAND (value, 1);
118 tree basetype = TYPE_OFFSET_BASETYPE (type);
119 my_friendly_assert (TREE_CODE (member) == FIELD_DECL, 305);
120 base = convert_pointer_to (basetype, current_class_ptr);
121 value = build (COMPONENT_REF, TREE_TYPE (member),
122 build_indirect_ref (base, NULL), member);
123 return require_complete_type (value);
126 if (complete_type_or_else (type, value))
127 return value;
128 else
129 return error_mark_node;
132 /* Try to complete TYPE, if it is incomplete. For example, if TYPE is
133 a template instantiation, do the instantiation. Returns TYPE,
134 whether or not it could be completed, unless something goes
135 horribly wrong, in which case the error_mark_node is returned. */
137 tree
138 complete_type (type)
139 tree type;
141 if (type == NULL_TREE)
142 /* Rather than crash, we return something sure to cause an error
143 at some point. */
144 return error_mark_node;
146 if (type == error_mark_node || COMPLETE_TYPE_P (type))
148 else if (TREE_CODE (type) == ARRAY_TYPE && TYPE_DOMAIN (type))
150 tree t = complete_type (TREE_TYPE (type));
151 if (COMPLETE_TYPE_P (t) && ! processing_template_decl)
152 layout_type (type);
153 TYPE_NEEDS_CONSTRUCTING (type)
154 = TYPE_NEEDS_CONSTRUCTING (TYPE_MAIN_VARIANT (t));
155 TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type)
156 = TYPE_HAS_NONTRIVIAL_DESTRUCTOR (TYPE_MAIN_VARIANT (t));
158 else if (CLASS_TYPE_P (type) && CLASSTYPE_TEMPLATE_INSTANTIATION (type))
159 instantiate_class_template (TYPE_MAIN_VARIANT (type));
161 return type;
164 /* Like complete_type, but issue an error if the TYPE cannot be
165 completed. VALUE is used for informative diagnostics.
166 Returns NULL_TREE if the type cannot be made complete. */
168 tree
169 complete_type_or_else (type, value)
170 tree type;
171 tree value;
173 type = complete_type (type);
174 if (type == error_mark_node)
175 /* We already issued an error. */
176 return NULL_TREE;
177 else if (!COMPLETE_TYPE_P (type))
179 incomplete_type_error (value, type);
180 return NULL_TREE;
182 else
183 return type;
186 /* Return truthvalue of whether type of EXP is instantiated. */
189 type_unknown_p (exp)
190 tree exp;
192 return (TREE_CODE (exp) == OVERLOAD
193 || TREE_CODE (exp) == TREE_LIST
194 || TREE_TYPE (exp) == unknown_type_node
195 || (TREE_CODE (TREE_TYPE (exp)) == OFFSET_TYPE
196 && TREE_TYPE (TREE_TYPE (exp)) == unknown_type_node));
199 /* Return a pointer or pointer to member type similar to T1, with a
200 cv-qualification signature that is the union of the cv-qualification
201 signatures of T1 and T2: [expr.rel], [expr.eq]. */
203 static tree
204 qualify_type_recursive (t1, t2)
205 tree t1, t2;
207 if ((TYPE_PTR_P (t1) && TYPE_PTR_P (t2))
208 || (TYPE_PTRMEM_P (t1) && TYPE_PTRMEM_P (t2)))
210 tree tt1 = TREE_TYPE (t1);
211 tree tt2 = TREE_TYPE (t2);
212 tree b1;
213 int type_quals;
214 tree tgt;
215 tree attributes = (*targetm.merge_type_attributes) (t1, t2);
217 if (TREE_CODE (tt1) == OFFSET_TYPE)
219 b1 = TYPE_OFFSET_BASETYPE (tt1);
220 tt1 = TREE_TYPE (tt1);
221 tt2 = TREE_TYPE (tt2);
223 else
224 b1 = NULL_TREE;
226 type_quals = (CP_TYPE_QUALS (tt1) | CP_TYPE_QUALS (tt2));
227 tgt = qualify_type_recursive (tt1, tt2);
228 tgt = cp_build_qualified_type (tgt, type_quals);
229 if (b1)
230 tgt = build_offset_type (b1, tgt);
231 t1 = build_pointer_type (tgt);
232 t1 = build_type_attribute_variant (t1, attributes);
234 return t1;
237 /* Return the common type of two parameter lists.
238 We assume that comptypes has already been done and returned 1;
239 if that isn't so, this may crash.
241 As an optimization, free the space we allocate if the parameter
242 lists are already common. */
244 tree
245 commonparms (p1, p2)
246 tree p1, p2;
248 tree oldargs = p1, newargs, n;
249 int i, len;
250 int any_change = 0;
252 len = list_length (p1);
253 newargs = tree_last (p1);
255 if (newargs == void_list_node)
256 i = 1;
257 else
259 i = 0;
260 newargs = 0;
263 for (; i < len; i++)
264 newargs = tree_cons (NULL_TREE, NULL_TREE, newargs);
266 n = newargs;
268 for (i = 0; p1;
269 p1 = TREE_CHAIN (p1), p2 = TREE_CHAIN (p2), n = TREE_CHAIN (n), i++)
271 if (TREE_PURPOSE (p1) && !TREE_PURPOSE (p2))
273 TREE_PURPOSE (n) = TREE_PURPOSE (p1);
274 any_change = 1;
276 else if (! TREE_PURPOSE (p1))
278 if (TREE_PURPOSE (p2))
280 TREE_PURPOSE (n) = TREE_PURPOSE (p2);
281 any_change = 1;
284 else
286 if (1 != simple_cst_equal (TREE_PURPOSE (p1), TREE_PURPOSE (p2)))
287 any_change = 1;
288 TREE_PURPOSE (n) = TREE_PURPOSE (p2);
290 if (TREE_VALUE (p1) != TREE_VALUE (p2))
292 any_change = 1;
293 TREE_VALUE (n) = common_type (TREE_VALUE (p1), TREE_VALUE (p2));
295 else
296 TREE_VALUE (n) = TREE_VALUE (p1);
298 if (! any_change)
299 return oldargs;
301 return newargs;
304 /* Given a type, perhaps copied for a typedef,
305 find the "original" version of it. */
306 tree
307 original_type (t)
308 tree t;
310 while (TYPE_NAME (t) != NULL_TREE)
312 tree x = TYPE_NAME (t);
313 if (TREE_CODE (x) != TYPE_DECL)
314 break;
315 x = DECL_ORIGINAL_TYPE (x);
316 if (x == NULL_TREE)
317 break;
318 t = x;
320 return t;
323 /* T1 and T2 are arithmetic or enumeration types. Return the type
324 that will result from the "usual arithmetic converions" on T1 and
325 T2 as described in [expr]. */
327 tree
328 type_after_usual_arithmetic_conversions (t1, t2)
329 tree t1;
330 tree t2;
332 enum tree_code code1 = TREE_CODE (t1);
333 enum tree_code code2 = TREE_CODE (t2);
334 tree attributes;
336 /* FIXME: Attributes. */
337 my_friendly_assert (ARITHMETIC_TYPE_P (t1)
338 || TREE_CODE (t1) == ENUMERAL_TYPE,
339 19990725);
340 my_friendly_assert (ARITHMETIC_TYPE_P (t2)
341 || TREE_CODE (t2) == ENUMERAL_TYPE,
342 19990725);
344 /* In what follows, we slightly generalize the rules given in [expr]
345 so as to deal with `long long'. First, merge the attributes. */
346 attributes = (*targetm.merge_type_attributes) (t1, t2);
348 /* If only one is real, use it as the result. */
349 if (code1 == REAL_TYPE && code2 != REAL_TYPE)
350 return build_type_attribute_variant (t1, attributes);
351 if (code2 == REAL_TYPE && code1 != REAL_TYPE)
352 return build_type_attribute_variant (t2, attributes);
354 /* Perform the integral promotions. */
355 if (code1 != REAL_TYPE)
357 t1 = type_promotes_to (t1);
358 t2 = type_promotes_to (t2);
361 /* Both real or both integers; use the one with greater precision. */
362 if (TYPE_PRECISION (t1) > TYPE_PRECISION (t2))
363 return build_type_attribute_variant (t1, attributes);
364 else if (TYPE_PRECISION (t2) > TYPE_PRECISION (t1))
365 return build_type_attribute_variant (t2, attributes);
367 if (code1 != REAL_TYPE)
369 /* If one is a sizetype, use it so size_binop doesn't blow up. */
370 if (TYPE_IS_SIZETYPE (t1) > TYPE_IS_SIZETYPE (t2))
371 return build_type_attribute_variant (t1, attributes);
372 if (TYPE_IS_SIZETYPE (t2) > TYPE_IS_SIZETYPE (t1))
373 return build_type_attribute_variant (t2, attributes);
375 /* If one is unsigned long long, then convert the other to unsigned
376 long long. */
377 if (same_type_p (TYPE_MAIN_VARIANT (t1), long_long_unsigned_type_node)
378 || same_type_p (TYPE_MAIN_VARIANT (t2), long_long_unsigned_type_node))
379 return build_type_attribute_variant (long_long_unsigned_type_node,
380 attributes);
381 /* If one is a long long, and the other is an unsigned long, and
382 long long can represent all the values of an unsigned long, then
383 convert to a long long. Otherwise, convert to an unsigned long
384 long. Otherwise, if either operand is long long, convert the
385 other to long long.
387 Since we're here, we know the TYPE_PRECISION is the same;
388 therefore converting to long long cannot represent all the values
389 of an unsigned long, so we choose unsigned long long in that
390 case. */
391 if (same_type_p (TYPE_MAIN_VARIANT (t1), long_long_integer_type_node)
392 || same_type_p (TYPE_MAIN_VARIANT (t2), long_long_integer_type_node))
394 tree t = ((TREE_UNSIGNED (t1) || TREE_UNSIGNED (t2))
395 ? long_long_unsigned_type_node
396 : long_long_integer_type_node);
397 return build_type_attribute_variant (t, attributes);
400 /* Go through the same procedure, but for longs. */
401 if (same_type_p (TYPE_MAIN_VARIANT (t1), long_unsigned_type_node)
402 || same_type_p (TYPE_MAIN_VARIANT (t2), long_unsigned_type_node))
403 return build_type_attribute_variant (long_unsigned_type_node,
404 attributes);
405 if (same_type_p (TYPE_MAIN_VARIANT (t1), long_integer_type_node)
406 || same_type_p (TYPE_MAIN_VARIANT (t2), long_integer_type_node))
408 tree t = ((TREE_UNSIGNED (t1) || TREE_UNSIGNED (t2))
409 ? long_unsigned_type_node : long_integer_type_node);
410 return build_type_attribute_variant (t, attributes);
412 /* Otherwise prefer the unsigned one. */
413 if (TREE_UNSIGNED (t1))
414 return build_type_attribute_variant (t1, attributes);
415 else
416 return build_type_attribute_variant (t2, attributes);
418 else
420 if (same_type_p (TYPE_MAIN_VARIANT (t1), long_double_type_node)
421 || same_type_p (TYPE_MAIN_VARIANT (t2), long_double_type_node))
422 return build_type_attribute_variant (long_double_type_node,
423 attributes);
424 if (same_type_p (TYPE_MAIN_VARIANT (t1), double_type_node)
425 || same_type_p (TYPE_MAIN_VARIANT (t2), double_type_node))
426 return build_type_attribute_variant (double_type_node,
427 attributes);
428 else
429 return build_type_attribute_variant (float_type_node,
430 attributes);
434 /* Return the composite pointer type (see [expr.rel]) for T1 and T2.
435 ARG1 and ARG2 are the values with those types. The LOCATION is a
436 string describing the current location, in case an error occurs. */
438 tree
439 composite_pointer_type (t1, t2, arg1, arg2, location)
440 tree t1;
441 tree t2;
442 tree arg1;
443 tree arg2;
444 const char* location;
446 tree result_type;
448 /* [expr.rel]
450 If one operand is a null pointer constant, the composite pointer
451 type is the type of the other operand. */
452 if (null_ptr_cst_p (arg1))
453 return t2;
454 if (null_ptr_cst_p (arg2))
455 return t1;
457 /* Deal with pointer-to-member functions in the same way as we deal
458 with pointers to functions. */
459 if (TYPE_PTRMEMFUNC_P (t1))
460 t1 = TYPE_PTRMEMFUNC_FN_TYPE (t1);
461 if (TYPE_PTRMEMFUNC_P (t2))
462 t2 = TYPE_PTRMEMFUNC_FN_TYPE (t2);
464 /* We have:
466 [expr.rel]
468 If one of the operands has type "pointer to cv1 void*", then
469 the other has type "pointer to cv2T", and the composite pointer
470 type is "pointer to cv12 void", where cv12 is the union of cv1
471 and cv2.
473 If either type is a pointer to void, make sure it is T1. */
474 if (VOID_TYPE_P (TREE_TYPE (t2)))
476 tree t;
477 t = t1;
478 t1 = t2;
479 t2 = t;
481 /* Now, if T1 is a pointer to void, merge the qualifiers. */
482 if (VOID_TYPE_P (TREE_TYPE (t1)))
484 if (pedantic && TYPE_PTRFN_P (t2))
485 pedwarn ("ISO C++ forbids %s between pointer of type `void *' and pointer-to-function", location);
486 t1 = TREE_TYPE (t1);
487 t2 = TREE_TYPE (t2);
488 result_type = cp_build_qualified_type (void_type_node,
489 (CP_TYPE_QUALS (t1)
490 | CP_TYPE_QUALS (t2)));
491 result_type = build_pointer_type (result_type);
493 else
495 tree full1 = qualify_type_recursive (t1, t2);
496 tree full2 = qualify_type_recursive (t2, t1);
498 int val = comp_target_types (full1, full2, 1);
500 if (val > 0)
501 result_type = full1;
502 else if (val < 0)
503 result_type = full2;
504 else
506 cp_pedwarn ("%s between distinct pointer types `%T' and `%T' lacks a cast",
507 location, t1, t2);
508 result_type = ptr_type_node;
512 return result_type;
515 /* Return the common type of two types.
516 We assume that comptypes has already been done and returned 1;
517 if that isn't so, this may crash.
519 This is the type for the result of most arithmetic operations
520 if the operands have the given two types.
522 We do not deal with enumeral types here because they have already been
523 converted to integer types. */
525 tree
526 common_type (t1, t2)
527 tree t1, t2;
529 register enum tree_code code1;
530 register enum tree_code code2;
531 tree attributes;
533 /* Save time if the two types are the same. */
534 if (t1 == t2)
535 return t1;
536 t1 = original_type (t1);
537 t2 = original_type (t2);
538 if (t1 == t2)
539 return t1;
541 /* If one type is nonsense, use the other. */
542 if (t1 == error_mark_node)
543 return t2;
544 if (t2 == error_mark_node)
545 return t1;
547 if ((ARITHMETIC_TYPE_P (t1) || TREE_CODE (t1) == ENUMERAL_TYPE)
548 && (ARITHMETIC_TYPE_P (t2) || TREE_CODE (t2) == ENUMERAL_TYPE))
549 return type_after_usual_arithmetic_conversions (t1, t2);
551 /* Merge the attributes. */
552 attributes = (*targetm.merge_type_attributes) (t1, t2);
554 /* Treat an enum type as the unsigned integer type of the same width. */
556 if (TREE_CODE (t1) == ENUMERAL_TYPE)
557 t1 = type_for_size (TYPE_PRECISION (t1), 1);
558 if (TREE_CODE (t2) == ENUMERAL_TYPE)
559 t2 = type_for_size (TYPE_PRECISION (t2), 1);
561 if (TYPE_PTRMEMFUNC_P (t1))
562 t1 = TYPE_PTRMEMFUNC_FN_TYPE (t1);
563 if (TYPE_PTRMEMFUNC_P (t2))
564 t2 = TYPE_PTRMEMFUNC_FN_TYPE (t2);
566 code1 = TREE_CODE (t1);
567 code2 = TREE_CODE (t2);
569 /* If one type is complex, form the common type of the non-complex
570 components, then make that complex. Use T1 or T2 if it is the
571 required type. */
572 if (code1 == COMPLEX_TYPE || code2 == COMPLEX_TYPE)
574 tree subtype1 = code1 == COMPLEX_TYPE ? TREE_TYPE (t1) : t1;
575 tree subtype2 = code2 == COMPLEX_TYPE ? TREE_TYPE (t2) : t2;
576 tree subtype = common_type (subtype1, subtype2);
578 if (code1 == COMPLEX_TYPE && TREE_TYPE (t1) == subtype)
579 return build_type_attribute_variant (t1, attributes);
580 else if (code2 == COMPLEX_TYPE && TREE_TYPE (t2) == subtype)
581 return build_type_attribute_variant (t2, attributes);
582 else
583 return build_type_attribute_variant (build_complex_type (subtype),
584 attributes);
587 switch (code1)
589 case INTEGER_TYPE:
590 case REAL_TYPE:
591 /* We should have called type_after_usual_arithmetic_conversions
592 above. */
593 my_friendly_abort (19990725);
594 break;
596 case POINTER_TYPE:
597 case REFERENCE_TYPE:
598 /* For two pointers, do this recursively on the target type,
599 and combine the qualifiers of the two types' targets. */
600 /* This code was turned off; I don't know why.
601 But ISO C++ specifies doing this with the qualifiers.
602 So I turned it on again. */
604 tree tt1 = TREE_TYPE (t1);
605 tree tt2 = TREE_TYPE (t2);
606 tree b1, b2;
607 int type_quals;
608 tree target;
610 if (TREE_CODE (tt1) == OFFSET_TYPE)
612 b1 = TYPE_OFFSET_BASETYPE (tt1);
613 b2 = TYPE_OFFSET_BASETYPE (tt2);
614 tt1 = TREE_TYPE (tt1);
615 tt2 = TREE_TYPE (tt2);
617 else
618 b1 = b2 = NULL_TREE;
620 type_quals = (CP_TYPE_QUALS (tt1) | CP_TYPE_QUALS (tt2));
621 tt1 = TYPE_MAIN_VARIANT (tt1);
622 tt2 = TYPE_MAIN_VARIANT (tt2);
624 if (tt1 == tt2)
625 target = tt1;
626 else if (VOID_TYPE_P (tt1) || VOID_TYPE_P (tt2))
627 target = void_type_node;
628 else if (tt1 == unknown_type_node)
629 target = tt2;
630 else if (tt2 == unknown_type_node)
631 target = tt1;
632 else
633 target = common_type (tt1, tt2);
635 target = cp_build_qualified_type (target, type_quals);
637 if (b1)
639 if (same_type_p (b1, b2)
640 || (DERIVED_FROM_P (b1, b2) && binfo_or_else (b1, b2)))
641 target = build_offset_type (b2, target);
642 else if (binfo_or_else (b2, b1))
643 target = build_offset_type (b1, target);
646 if (code1 == POINTER_TYPE)
647 t1 = build_pointer_type (target);
648 else
649 t1 = build_reference_type (target);
650 t1 = build_type_attribute_variant (t1, attributes);
652 if (TREE_CODE (target) == METHOD_TYPE)
653 t1 = build_ptrmemfunc_type (t1);
655 return t1;
658 case ARRAY_TYPE:
660 tree elt = common_type (TREE_TYPE (t1), TREE_TYPE (t2));
661 /* Save space: see if the result is identical to one of the args. */
662 if (elt == TREE_TYPE (t1) && TYPE_DOMAIN (t1))
663 return build_type_attribute_variant (t1, attributes);
664 if (elt == TREE_TYPE (t2) && TYPE_DOMAIN (t2))
665 return build_type_attribute_variant (t2, attributes);
666 /* Merge the element types, and have a size if either arg has one. */
667 t1 = build_cplus_array_type
668 (elt, TYPE_DOMAIN (TYPE_DOMAIN (t1) ? t1 : t2));
669 return build_type_attribute_variant (t1, attributes);
672 case FUNCTION_TYPE:
673 /* Function types: prefer the one that specified arg types.
674 If both do, merge the arg types. Also merge the return types. */
676 tree valtype = common_type (TREE_TYPE (t1), TREE_TYPE (t2));
677 tree p1 = TYPE_ARG_TYPES (t1);
678 tree p2 = TYPE_ARG_TYPES (t2);
679 tree rval, raises;
681 /* Save space: see if the result is identical to one of the args. */
682 if (valtype == TREE_TYPE (t1) && ! p2)
683 return build_type_attribute_variant (t1, attributes);
684 if (valtype == TREE_TYPE (t2) && ! p1)
685 return build_type_attribute_variant (t2, attributes);
687 /* Simple way if one arg fails to specify argument types. */
688 if (p1 == NULL_TREE || TREE_VALUE (p1) == void_type_node)
690 rval = build_function_type (valtype, p2);
691 if ((raises = TYPE_RAISES_EXCEPTIONS (t2)))
692 rval = build_exception_variant (rval, raises);
693 return build_type_attribute_variant (rval, attributes);
695 raises = TYPE_RAISES_EXCEPTIONS (t1);
696 if (p2 == NULL_TREE || TREE_VALUE (p2) == void_type_node)
698 rval = build_function_type (valtype, p1);
699 if (raises)
700 rval = build_exception_variant (rval, raises);
701 return build_type_attribute_variant (rval, attributes);
704 rval = build_function_type (valtype, commonparms (p1, p2));
705 rval = build_exception_variant (rval, raises);
706 return build_type_attribute_variant (rval, attributes);
709 case RECORD_TYPE:
710 case UNION_TYPE:
711 t1 = TYPE_MAIN_VARIANT (t1);
712 t2 = TYPE_MAIN_VARIANT (t2);
714 if (DERIVED_FROM_P (t1, t2) && binfo_or_else (t1, t2))
715 return build_type_attribute_variant (t1, attributes);
716 else if (binfo_or_else (t2, t1))
717 return build_type_attribute_variant (t2, attributes);
718 else
720 compiler_error ("common_type called with uncommon aggregate types");
721 return error_mark_node;
724 case METHOD_TYPE:
725 if (TREE_CODE (TREE_TYPE (t1)) == TREE_CODE (TREE_TYPE (t2)))
727 /* Get this value the long way, since TYPE_METHOD_BASETYPE
728 is just the main variant of this. */
729 tree basetype;
730 tree raises, t3;
732 tree b1 = TYPE_OFFSET_BASETYPE (t1);
733 tree b2 = TYPE_OFFSET_BASETYPE (t2);
735 if (same_type_p (b1, b2)
736 || (DERIVED_FROM_P (b1, b2) && binfo_or_else (b1, b2)))
737 basetype = TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (t2)));
738 else
740 if (binfo_or_else (b2, b1) == NULL_TREE)
741 compiler_error ("common_type called with uncommon method types");
742 basetype = TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (t1)));
745 raises = TYPE_RAISES_EXCEPTIONS (t1);
747 /* If this was a member function type, get back to the
748 original type of type member function (i.e., without
749 the class instance variable up front. */
750 t1 = build_function_type (TREE_TYPE (t1),
751 TREE_CHAIN (TYPE_ARG_TYPES (t1)));
752 t2 = build_function_type (TREE_TYPE (t2),
753 TREE_CHAIN (TYPE_ARG_TYPES (t2)));
754 t3 = common_type (t1, t2);
755 t3 = build_cplus_method_type (basetype, TREE_TYPE (t3),
756 TYPE_ARG_TYPES (t3));
757 t1 = build_exception_variant (t3, raises);
759 else
760 compiler_error ("common_type called with uncommon method types");
762 return build_type_attribute_variant (t1, attributes);
764 case OFFSET_TYPE:
765 /* Pointers to members should now be handled by the POINTER_TYPE
766 case above. */
767 my_friendly_abort (990325);
769 default:
770 return build_type_attribute_variant (t1, attributes);
774 /* Compare two exception specifier types for exactness or subsetness, if
775 allowed. Returns 0 for mismatch, 1 for same, 2 if B is allowed by A.
777 [except.spec] "If a class X ... objects of class X or any class publicly
778 and unambigously derrived from X. Similarly, if a pointer type Y * ...
779 exceptions of type Y * or that are pointers to any type publicly and
780 unambigously derrived from Y. Otherwise a function only allows exceptions
781 that have the same type ..."
782 This does not mention cv qualifiers and is different to what throw
783 [except.throw] and catch [except.catch] will do. They will ignore the
784 top level cv qualifiers, and allow qualifiers in the pointer to class
785 example.
787 We implement the letter of the standard. */
789 static int
790 comp_except_types (a, b, exact)
791 tree a, b;
792 int exact;
794 if (same_type_p (a, b))
795 return 1;
796 else if (!exact)
798 if (CP_TYPE_QUALS (a) || CP_TYPE_QUALS (b))
799 return 0;
801 if (TREE_CODE (a) == POINTER_TYPE
802 && TREE_CODE (b) == POINTER_TYPE)
804 a = TREE_TYPE (a);
805 b = TREE_TYPE (b);
806 if (CP_TYPE_QUALS (a) || CP_TYPE_QUALS (b))
807 return 0;
810 if (TREE_CODE (a) != RECORD_TYPE
811 || TREE_CODE (b) != RECORD_TYPE)
812 return 0;
814 if (ACCESSIBLY_UNIQUELY_DERIVED_P (a, b))
815 return 2;
817 return 0;
820 /* Return 1 if TYPE1 and TYPE2 are equivalent exception specifiers.
821 If EXACT is 0, T2 can be a subset of T1 (according to 15.4/7),
822 otherwise it must be exact. Exception lists are unordered, but
823 we've already filtered out duplicates. Most lists will be in order,
824 we should try to make use of that. */
827 comp_except_specs (t1, t2, exact)
828 tree t1, t2;
829 int exact;
831 tree probe;
832 tree base;
833 int length = 0;
835 if (t1 == t2)
836 return 1;
838 if (t1 == NULL_TREE) /* T1 is ... */
839 return t2 == NULL_TREE || !exact;
840 if (!TREE_VALUE (t1)) /* t1 is EMPTY */
841 return t2 != NULL_TREE && !TREE_VALUE (t2);
842 if (t2 == NULL_TREE) /* T2 is ... */
843 return 0;
844 if (TREE_VALUE(t1) && !TREE_VALUE (t2)) /* T2 is EMPTY, T1 is not */
845 return !exact;
847 /* Neither set is ... or EMPTY, make sure each part of T2 is in T1.
848 Count how many we find, to determine exactness. For exact matching and
849 ordered T1, T2, this is an O(n) operation, otherwise its worst case is
850 O(nm). */
851 for (base = t1; t2 != NULL_TREE; t2 = TREE_CHAIN (t2))
853 for (probe = base; probe != NULL_TREE; probe = TREE_CHAIN (probe))
855 tree a = TREE_VALUE (probe);
856 tree b = TREE_VALUE (t2);
858 if (comp_except_types (a, b, exact))
860 if (probe == base && exact)
861 base = TREE_CHAIN (probe);
862 length++;
863 break;
866 if (probe == NULL_TREE)
867 return 0;
869 return !exact || base == NULL_TREE || length == list_length (t1);
872 /* Compare the array types T1 and T2, using CMP as the type comparison
873 function for the element types. STRICT is as for comptypes. */
875 static int
876 comp_array_types (cmp, t1, t2, strict)
877 register int (*cmp) PARAMS ((tree, tree, int));
878 tree t1, t2;
879 int strict;
881 tree d1;
882 tree d2;
884 if (t1 == t2)
885 return 1;
887 /* The type of the array elements must be the same. */
888 if (!(TREE_TYPE (t1) == TREE_TYPE (t2)
889 || (*cmp) (TREE_TYPE (t1), TREE_TYPE (t2),
890 strict & ~COMPARE_REDECLARATION)))
891 return 0;
893 d1 = TYPE_DOMAIN (t1);
894 d2 = TYPE_DOMAIN (t2);
896 if (d1 == d2)
897 return 1;
899 /* If one of the arrays is dimensionless, and the other has a
900 dimension, they are of different types. However, it is legal to
901 write:
903 extern int a[];
904 int a[3];
906 by [basic.link]:
908 declarations for an array object can specify
909 array types that differ by the presence or absence of a major
910 array bound (_dcl.array_). */
911 if (!d1 || !d2)
912 return strict & COMPARE_REDECLARATION;
914 /* Check that the dimensions are the same. */
915 return (cp_tree_equal (TYPE_MIN_VALUE (d1),
916 TYPE_MIN_VALUE (d2))
917 && cp_tree_equal (TYPE_MAX_VALUE (d1),
918 TYPE_MAX_VALUE (d2)));
921 /* Return 1 if T1 and T2 are compatible types for assignment or
922 various other operations. STRICT is a bitwise-or of the COMPARE_*
923 flags. */
926 comptypes (t1, t2, strict)
927 tree t1;
928 tree t2;
929 int strict;
931 int attrval, val;
932 int orig_strict = strict;
934 /* The special exemption for redeclaring array types without an
935 array bound only applies at the top level:
937 extern int (*i)[];
938 int (*i)[8];
940 is not legal, for example. */
941 strict &= ~COMPARE_REDECLARATION;
943 /* Suppress errors caused by previously reported errors */
944 if (t1 == t2)
945 return 1;
947 /* This should never happen. */
948 my_friendly_assert (t1 != error_mark_node, 307);
950 if (t2 == error_mark_node)
951 return 0;
953 /* If either type is the internal version of sizetype, return the
954 language version. */
955 if (TREE_CODE (t1) == INTEGER_TYPE && TYPE_IS_SIZETYPE (t1)
956 && TYPE_DOMAIN (t1) != 0)
957 t1 = TYPE_DOMAIN (t1);
959 if (TREE_CODE (t2) == INTEGER_TYPE && TYPE_IS_SIZETYPE (t2)
960 && TYPE_DOMAIN (t2) != 0)
961 t2 = TYPE_DOMAIN (t2);
963 if (strict & COMPARE_RELAXED)
965 /* Treat an enum type as the unsigned integer type of the same width. */
967 if (TREE_CODE (t1) == ENUMERAL_TYPE)
968 t1 = type_for_size (TYPE_PRECISION (t1), 1);
969 if (TREE_CODE (t2) == ENUMERAL_TYPE)
970 t2 = type_for_size (TYPE_PRECISION (t2), 1);
972 if (t1 == t2)
973 return 1;
976 if (TYPE_PTRMEMFUNC_P (t1))
977 t1 = TYPE_PTRMEMFUNC_FN_TYPE (t1);
978 if (TYPE_PTRMEMFUNC_P (t2))
979 t2 = TYPE_PTRMEMFUNC_FN_TYPE (t2);
981 /* Different classes of types can't be compatible. */
982 if (TREE_CODE (t1) != TREE_CODE (t2))
983 return 0;
985 /* Qualifiers must match. */
986 if (CP_TYPE_QUALS (t1) != CP_TYPE_QUALS (t2))
987 return 0;
988 if (strict == COMPARE_STRICT
989 && TYPE_FOR_JAVA (t1) != TYPE_FOR_JAVA (t2))
990 return 0;
992 /* Allow for two different type nodes which have essentially the same
993 definition. Note that we already checked for equality of the type
994 qualifiers (just above). */
996 if (TYPE_MAIN_VARIANT (t1) == TYPE_MAIN_VARIANT (t2))
997 return 1;
999 if (strict & COMPARE_NO_ATTRIBUTES)
1000 attrval = 1;
1001 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
1002 else if (! (attrval = (*targetm.comp_type_attributes) (t1, t2)))
1003 return 0;
1005 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
1006 val = 0;
1008 switch (TREE_CODE (t1))
1010 case TEMPLATE_TEMPLATE_PARM:
1011 case BOUND_TEMPLATE_TEMPLATE_PARM:
1012 if (TEMPLATE_TYPE_IDX (t1) != TEMPLATE_TYPE_IDX (t2)
1013 || TEMPLATE_TYPE_LEVEL (t1) != TEMPLATE_TYPE_LEVEL (t2))
1014 return 0;
1015 if (! comp_template_parms
1016 (DECL_TEMPLATE_PARMS (TEMPLATE_TEMPLATE_PARM_TEMPLATE_DECL (t1)),
1017 DECL_TEMPLATE_PARMS (TEMPLATE_TEMPLATE_PARM_TEMPLATE_DECL (t2))))
1018 return 0;
1019 if (TREE_CODE (t1) == TEMPLATE_TEMPLATE_PARM)
1020 return 1;
1021 /* Don't check inheritance. */
1022 strict = COMPARE_STRICT;
1023 /* fall through */
1025 case RECORD_TYPE:
1026 case UNION_TYPE:
1027 if (TYPE_TEMPLATE_INFO (t1) && TYPE_TEMPLATE_INFO (t2)
1028 && (TYPE_TI_TEMPLATE (t1) == TYPE_TI_TEMPLATE (t2)
1029 || TREE_CODE (t1) == BOUND_TEMPLATE_TEMPLATE_PARM))
1030 val = comp_template_args (TYPE_TI_ARGS (t1),
1031 TYPE_TI_ARGS (t2));
1032 look_hard:
1033 if ((strict & COMPARE_BASE) && DERIVED_FROM_P (t1, t2))
1034 val = 1;
1035 else if ((strict & COMPARE_RELAXED) && DERIVED_FROM_P (t2, t1))
1036 val = 1;
1037 break;
1039 case OFFSET_TYPE:
1040 val = (comptypes (build_pointer_type (TYPE_OFFSET_BASETYPE (t1)),
1041 build_pointer_type (TYPE_OFFSET_BASETYPE (t2)), strict)
1042 && comptypes (TREE_TYPE (t1), TREE_TYPE (t2), strict));
1043 break;
1045 case METHOD_TYPE:
1046 if (! comp_except_specs (TYPE_RAISES_EXCEPTIONS (t1),
1047 TYPE_RAISES_EXCEPTIONS (t2), 1))
1048 return 0;
1050 /* This case is anti-symmetrical!
1051 One can pass a base member (or member function)
1052 to something expecting a derived member (or member function),
1053 but not vice-versa! */
1055 val = (comptypes (TREE_TYPE (t1), TREE_TYPE (t2), strict)
1056 && compparms (TYPE_ARG_TYPES (t1), TYPE_ARG_TYPES (t2)));
1057 break;
1059 case POINTER_TYPE:
1060 case REFERENCE_TYPE:
1061 t1 = TREE_TYPE (t1);
1062 t2 = TREE_TYPE (t2);
1063 /* first, check whether the referred types match with the
1064 required level of strictness */
1065 val = comptypes (t1, t2, strict);
1066 if (val)
1067 break;
1068 if (TREE_CODE (t1) == RECORD_TYPE
1069 && TREE_CODE (t2) == RECORD_TYPE)
1070 goto look_hard;
1071 break;
1073 case FUNCTION_TYPE:
1074 if (! comp_except_specs (TYPE_RAISES_EXCEPTIONS (t1),
1075 TYPE_RAISES_EXCEPTIONS (t2), 1))
1076 return 0;
1078 val = ((TREE_TYPE (t1) == TREE_TYPE (t2)
1079 || comptypes (TREE_TYPE (t1), TREE_TYPE (t2), strict))
1080 && compparms (TYPE_ARG_TYPES (t1), TYPE_ARG_TYPES (t2)));
1081 break;
1083 case ARRAY_TYPE:
1084 /* Target types must match incl. qualifiers. We use ORIG_STRICT
1085 here since this is the one place where
1086 COMPARE_REDECLARATION should be used. */
1087 val = comp_array_types (comptypes, t1, t2, orig_strict);
1088 break;
1090 case TEMPLATE_TYPE_PARM:
1091 return TEMPLATE_TYPE_IDX (t1) == TEMPLATE_TYPE_IDX (t2)
1092 && TEMPLATE_TYPE_LEVEL (t1) == TEMPLATE_TYPE_LEVEL (t2);
1094 case TYPENAME_TYPE:
1095 if (cp_tree_equal (TYPENAME_TYPE_FULLNAME (t1),
1096 TYPENAME_TYPE_FULLNAME (t2)) < 1)
1097 return 0;
1098 return same_type_p (TYPE_CONTEXT (t1), TYPE_CONTEXT (t2));
1100 case UNBOUND_CLASS_TEMPLATE:
1101 if (cp_tree_equal (TYPE_IDENTIFIER (t1),
1102 TYPE_IDENTIFIER (t2)) < 1)
1103 return 0;
1104 return same_type_p (TYPE_CONTEXT (t1), TYPE_CONTEXT (t2));
1106 case COMPLEX_TYPE:
1107 return same_type_p (TREE_TYPE (t1), TREE_TYPE (t2));
1109 default:
1110 break;
1112 return attrval == 2 && val == 1 ? 2 : val;
1115 /* Subroutine of comp_target-types. Make sure that the cv-quals change
1116 only in the same direction as the target type. */
1118 static int
1119 comp_cv_target_types (ttl, ttr, nptrs)
1120 tree ttl, ttr;
1121 int nptrs;
1123 int t;
1125 if (!at_least_as_qualified_p (ttl, ttr)
1126 && !at_least_as_qualified_p (ttr, ttl))
1127 /* The qualifications are incomparable. */
1128 return 0;
1130 if (TYPE_MAIN_VARIANT (ttl) == TYPE_MAIN_VARIANT (ttr))
1131 return more_qualified_p (ttr, ttl) ? -1 : 1;
1133 t = comp_target_types (ttl, ttr, nptrs);
1134 if ((t == 1 && at_least_as_qualified_p (ttl, ttr))
1135 || (t == -1 && at_least_as_qualified_p (ttr, ttl)))
1136 return t;
1138 return 0;
1141 /* Return 1 or -1 if TTL and TTR are pointers to types that are equivalent,
1142 ignoring their qualifiers, 0 if not. Return 1 means that TTR can be
1143 converted to TTL. Return -1 means that TTL can be converted to TTR but
1144 not vice versa.
1146 NPTRS is the number of pointers we can strip off and keep cool.
1147 This is used to permit (for aggr A, aggr B) A, B* to convert to A*,
1148 but to not permit B** to convert to A**.
1150 This should go away. Callers should use can_convert or something
1151 similar instead. (jason 17 Apr 1997) */
1154 comp_target_types (ttl, ttr, nptrs)
1155 tree ttl, ttr;
1156 int nptrs;
1158 ttl = TYPE_MAIN_VARIANT (ttl);
1159 ttr = TYPE_MAIN_VARIANT (ttr);
1160 if (same_type_p (ttl, ttr))
1161 return 1;
1163 if (TREE_CODE (ttr) != TREE_CODE (ttl))
1164 return 0;
1166 if ((TREE_CODE (ttr) == POINTER_TYPE
1167 || TREE_CODE (ttr) == REFERENCE_TYPE)
1168 /* If we get a pointer with nptrs == 0, we don't allow any tweaking
1169 of the type pointed to. This is necessary for reference init
1170 semantics. We won't get here from a previous call with nptrs == 1;
1171 for multi-level pointers we end up in comp_ptr_ttypes. */
1172 && nptrs > 0)
1174 int is_ptr = TREE_CODE (ttr) == POINTER_TYPE;
1176 ttl = TREE_TYPE (ttl);
1177 ttr = TREE_TYPE (ttr);
1179 if (is_ptr)
1181 if (TREE_CODE (ttl) == UNKNOWN_TYPE
1182 || TREE_CODE (ttr) == UNKNOWN_TYPE)
1183 return 1;
1184 else if (TREE_CODE (ttl) == VOID_TYPE
1185 && TREE_CODE (ttr) != FUNCTION_TYPE
1186 && TREE_CODE (ttr) != METHOD_TYPE
1187 && TREE_CODE (ttr) != OFFSET_TYPE)
1188 return 1;
1189 else if (TREE_CODE (ttr) == VOID_TYPE
1190 && TREE_CODE (ttl) != FUNCTION_TYPE
1191 && TREE_CODE (ttl) != METHOD_TYPE
1192 && TREE_CODE (ttl) != OFFSET_TYPE)
1193 return -1;
1194 else if (TREE_CODE (ttl) == POINTER_TYPE
1195 || TREE_CODE (ttl) == ARRAY_TYPE)
1197 if (comp_ptr_ttypes (ttl, ttr))
1198 return 1;
1199 else if (comp_ptr_ttypes (ttr, ttl))
1200 return -1;
1201 return 0;
1205 /* Const and volatile mean something different for function types,
1206 so the usual checks are not appropriate. */
1207 if (TREE_CODE (ttl) == FUNCTION_TYPE || TREE_CODE (ttl) == METHOD_TYPE)
1208 return comp_target_types (ttl, ttr, nptrs - 1);
1210 return comp_cv_target_types (ttl, ttr, nptrs - 1);
1213 if (TREE_CODE (ttr) == ARRAY_TYPE)
1214 return comp_array_types (comp_target_types, ttl, ttr, COMPARE_STRICT);
1215 else if (TREE_CODE (ttr) == FUNCTION_TYPE || TREE_CODE (ttr) == METHOD_TYPE)
1217 tree argsl, argsr;
1218 int saw_contra = 0;
1220 if (pedantic)
1222 if (!same_type_p (TREE_TYPE (ttl), TREE_TYPE (ttr)))
1223 return 0;
1225 else
1227 switch (comp_target_types (TREE_TYPE (ttl), TREE_TYPE (ttr), -1))
1229 case 0:
1230 return 0;
1231 case -1:
1232 saw_contra = 1;
1236 argsl = TYPE_ARG_TYPES (ttl);
1237 argsr = TYPE_ARG_TYPES (ttr);
1239 /* Compare 'this' here, not in comp_target_parms. */
1240 if (TREE_CODE (ttr) == METHOD_TYPE)
1242 tree tl = TYPE_METHOD_BASETYPE (ttl);
1243 tree tr = TYPE_METHOD_BASETYPE (ttr);
1245 if (!same_or_base_type_p (tr, tl))
1247 if (same_or_base_type_p (tl, tr))
1248 saw_contra = 1;
1249 else
1250 return 0;
1253 argsl = TREE_CHAIN (argsl);
1254 argsr = TREE_CHAIN (argsr);
1257 switch (comp_target_parms (argsl, argsr))
1259 case 0:
1260 return 0;
1261 case -1:
1262 saw_contra = 1;
1265 return saw_contra ? -1 : 1;
1267 /* for C++ */
1268 else if (TREE_CODE (ttr) == OFFSET_TYPE)
1270 int base;
1272 /* Contravariance: we can assign a pointer to base member to a pointer
1273 to derived member. Note difference from simple pointer case, where
1274 we can pass a pointer to derived to a pointer to base. */
1275 if (same_or_base_type_p (TYPE_OFFSET_BASETYPE (ttr),
1276 TYPE_OFFSET_BASETYPE (ttl)))
1277 base = 1;
1278 else if (same_or_base_type_p (TYPE_OFFSET_BASETYPE (ttl),
1279 TYPE_OFFSET_BASETYPE (ttr)))
1281 tree tmp = ttl;
1282 ttl = ttr;
1283 ttr = tmp;
1284 base = -1;
1286 else
1287 return 0;
1289 ttl = TREE_TYPE (ttl);
1290 ttr = TREE_TYPE (ttr);
1292 if (TREE_CODE (ttl) == POINTER_TYPE
1293 || TREE_CODE (ttl) == ARRAY_TYPE)
1295 if (comp_ptr_ttypes (ttl, ttr))
1296 return base;
1297 return 0;
1299 else
1301 if (comp_cv_target_types (ttl, ttr, nptrs) == 1)
1302 return base;
1303 return 0;
1306 else if (IS_AGGR_TYPE (ttl))
1308 if (nptrs < 0)
1309 return 0;
1310 if (same_or_base_type_p (build_pointer_type (ttl),
1311 build_pointer_type (ttr)))
1312 return 1;
1313 if (same_or_base_type_p (build_pointer_type (ttr),
1314 build_pointer_type (ttl)))
1315 return -1;
1316 return 0;
1319 return 0;
1322 /* Returns 1 if TYPE1 is at least as qualified as TYPE2. */
1325 at_least_as_qualified_p (type1, type2)
1326 tree type1;
1327 tree type2;
1329 /* All qualifiers for TYPE2 must also appear in TYPE1. */
1330 return ((CP_TYPE_QUALS (type1) & CP_TYPE_QUALS (type2))
1331 == CP_TYPE_QUALS (type2));
1334 /* Returns 1 if TYPE1 is more qualified than TYPE2. */
1337 more_qualified_p (type1, type2)
1338 tree type1;
1339 tree type2;
1341 return (CP_TYPE_QUALS (type1) != CP_TYPE_QUALS (type2)
1342 && at_least_as_qualified_p (type1, type2));
1345 /* Returns 1 if TYPE1 is more cv-qualified than TYPE2, -1 if TYPE2 is
1346 more cv-qualified that TYPE1, and 0 otherwise. */
1349 comp_cv_qualification (type1, type2)
1350 tree type1;
1351 tree type2;
1353 if (CP_TYPE_QUALS (type1) == CP_TYPE_QUALS (type2))
1354 return 0;
1356 if (at_least_as_qualified_p (type1, type2))
1357 return 1;
1359 else if (at_least_as_qualified_p (type2, type1))
1360 return -1;
1362 return 0;
1365 /* Returns 1 if the cv-qualification signature of TYPE1 is a proper
1366 subset of the cv-qualification signature of TYPE2, and the types
1367 are similar. Returns -1 if the other way 'round, and 0 otherwise. */
1370 comp_cv_qual_signature (type1, type2)
1371 tree type1;
1372 tree type2;
1374 if (comp_ptr_ttypes_real (type2, type1, -1))
1375 return 1;
1376 else if (comp_ptr_ttypes_real (type1, type2, -1))
1377 return -1;
1378 else
1379 return 0;
1382 /* If two types share a common base type, return that basetype.
1383 If there is not a unique most-derived base type, this function
1384 returns ERROR_MARK_NODE. */
1386 static tree
1387 common_base_type (tt1, tt2)
1388 tree tt1, tt2;
1390 tree best = NULL_TREE;
1391 int i;
1393 /* If one is a baseclass of another, that's good enough. */
1394 if (UNIQUELY_DERIVED_FROM_P (tt1, tt2))
1395 return tt1;
1396 if (UNIQUELY_DERIVED_FROM_P (tt2, tt1))
1397 return tt2;
1399 /* Otherwise, try to find a unique baseclass of TT1
1400 that is shared by TT2, and follow that down. */
1401 for (i = CLASSTYPE_N_BASECLASSES (tt1)-1; i >= 0; i--)
1403 tree basetype = TYPE_BINFO_BASETYPE (tt1, i);
1404 tree trial = common_base_type (basetype, tt2);
1405 if (trial)
1407 if (trial == error_mark_node)
1408 return trial;
1409 if (best == NULL_TREE)
1410 best = trial;
1411 else if (best != trial)
1412 return error_mark_node;
1416 /* Same for TT2. */
1417 for (i = CLASSTYPE_N_BASECLASSES (tt2)-1; i >= 0; i--)
1419 tree basetype = TYPE_BINFO_BASETYPE (tt2, i);
1420 tree trial = common_base_type (tt1, basetype);
1421 if (trial)
1423 if (trial == error_mark_node)
1424 return trial;
1425 if (best == NULL_TREE)
1426 best = trial;
1427 else if (best != trial)
1428 return error_mark_node;
1431 return best;
1434 /* Subroutines of `comptypes'. */
1436 /* Return 1 if two parameter type lists PARMS1 and PARMS2 are
1437 equivalent in the sense that functions with those parameter types
1438 can have equivalent types. The two lists must be equivalent,
1439 element by element.
1441 C++: See comment above about TYPE1, TYPE2. */
1444 compparms (parms1, parms2)
1445 tree parms1, parms2;
1447 register tree t1 = parms1, t2 = parms2;
1449 /* An unspecified parmlist matches any specified parmlist
1450 whose argument types don't need default promotions. */
1452 while (1)
1454 if (t1 == 0 && t2 == 0)
1455 return 1;
1456 /* If one parmlist is shorter than the other,
1457 they fail to match. */
1458 if (t1 == 0 || t2 == 0)
1459 return 0;
1460 if (!same_type_p (TREE_VALUE (t2), TREE_VALUE (t1)))
1461 return 0;
1463 t1 = TREE_CHAIN (t1);
1464 t2 = TREE_CHAIN (t2);
1468 /* This really wants return whether or not parameter type lists
1469 would make their owning functions assignment compatible or not.
1471 The return value is like for comp_target_types.
1473 This should go away, possibly with the exception of the empty parmlist
1474 conversion; there are no conversions between function types in C++.
1475 (jason 17 Apr 1997) */
1477 static int
1478 comp_target_parms (parms1, parms2)
1479 tree parms1, parms2;
1481 register tree t1 = parms1, t2 = parms2;
1482 int warn_contravariance = 0;
1484 /* In C, an unspecified parmlist matches any specified parmlist
1485 whose argument types don't need default promotions. This is not
1486 true for C++, but let's do it anyway for unfixed headers. */
1488 if (t1 == 0 && t2 != 0)
1490 cp_pedwarn ("ISO C++ prohibits conversion from `%#T' to `(...)'",
1491 parms2);
1492 return self_promoting_args_p (t2);
1494 if (t2 == 0)
1495 return self_promoting_args_p (t1);
1497 for (; t1 || t2; t1 = TREE_CHAIN (t1), t2 = TREE_CHAIN (t2))
1499 tree p1, p2;
1501 /* If one parmlist is shorter than the other,
1502 they fail to match, unless STRICT is <= 0. */
1503 if (t1 == 0 || t2 == 0)
1504 return 0;
1505 p1 = TREE_VALUE (t1);
1506 p2 = TREE_VALUE (t2);
1507 if (same_type_p (p1, p2))
1508 continue;
1510 if (pedantic)
1511 return 0;
1513 if ((TREE_CODE (p1) == POINTER_TYPE && TREE_CODE (p2) == POINTER_TYPE)
1514 || (TREE_CODE (p1) == REFERENCE_TYPE
1515 && TREE_CODE (p2) == REFERENCE_TYPE))
1517 /* The following is wrong for contravariance,
1518 but many programs depend on it. */
1519 if (TREE_TYPE (p1) == void_type_node)
1520 continue;
1521 if (TREE_TYPE (p2) == void_type_node)
1523 warn_contravariance = 1;
1524 continue;
1526 if (IS_AGGR_TYPE (TREE_TYPE (p1))
1527 && !same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (p1),
1528 TREE_TYPE (p2)))
1529 return 0;
1531 /* Note backwards order due to contravariance. */
1532 if (comp_target_types (p2, p1, 1) <= 0)
1534 if (comp_target_types (p1, p2, 1) > 0)
1536 warn_contravariance = 1;
1537 continue;
1539 return 0;
1542 return warn_contravariance ? -1 : 1;
1545 /* Compute the value of the `sizeof' operator. */
1547 tree
1548 c_sizeof (type)
1549 tree type;
1551 enum tree_code code = TREE_CODE (type);
1552 tree size;
1554 if (processing_template_decl)
1555 return build_min_nt (SIZEOF_EXPR, type);
1557 if (code == FUNCTION_TYPE)
1559 if (pedantic || warn_pointer_arith)
1560 pedwarn ("ISO C++ forbids applying `sizeof' to a function type");
1561 size = size_one_node;
1563 else if (code == METHOD_TYPE)
1565 if (pedantic || warn_pointer_arith)
1566 pedwarn ("ISO C++ forbids applying `sizeof' to a member function");
1567 size = size_one_node;
1569 else if (code == VOID_TYPE)
1571 if (pedantic || warn_pointer_arith)
1572 pedwarn ("ISO C++ forbids applying `sizeof' to type `void' which is an incomplete type");
1573 size = size_one_node;
1575 else if (code == ERROR_MARK)
1576 size = size_one_node;
1577 else
1579 /* ARM $5.3.2: ``When applied to a reference, the result is the
1580 size of the referenced object.'' */
1581 if (code == REFERENCE_TYPE)
1582 type = TREE_TYPE (type);
1584 if (code == OFFSET_TYPE)
1586 cp_error ("`sizeof' applied to non-static member");
1587 size = size_zero_node;
1589 else if (!COMPLETE_TYPE_P (complete_type (type)))
1591 cp_error ("`sizeof' applied to incomplete type `%T'", type);
1592 size = size_zero_node;
1594 else
1595 /* Convert in case a char is more than one unit. */
1596 size = size_binop (CEIL_DIV_EXPR, TYPE_SIZE_UNIT (type),
1597 size_int (TYPE_PRECISION (char_type_node)
1598 / BITS_PER_UNIT));
1601 /* SIZE will have an integer type with TYPE_IS_SIZETYPE set.
1602 TYPE_IS_SIZETYPE means that certain things (like overflow) will
1603 never happen. However, this node should really have type
1604 `size_t', which is just a typedef for an ordinary integer type. */
1605 size = fold (build1 (NOP_EXPR, c_size_type_node, size));
1606 my_friendly_assert (!TYPE_IS_SIZETYPE (TREE_TYPE (size)),
1607 20001021);
1608 return size;
1612 tree
1613 expr_sizeof (e)
1614 tree e;
1616 if (processing_template_decl)
1617 return build_min_nt (SIZEOF_EXPR, e);
1619 if (TREE_CODE (e) == COMPONENT_REF
1620 && DECL_C_BIT_FIELD (TREE_OPERAND (e, 1)))
1621 error ("sizeof applied to a bit-field");
1622 if (is_overloaded_fn (e))
1624 pedwarn ("ISO C++ forbids applying `sizeof' to an expression of function type");
1625 return c_sizeof (char_type_node);
1627 else if (type_unknown_p (e))
1629 incomplete_type_error (e, TREE_TYPE (e));
1630 return c_sizeof (char_type_node);
1632 /* It's illegal to say `sizeof (X::i)' for `i' a non-static data
1633 member unless you're in a non-static member of X. So hand off to
1634 resolve_offset_ref. [expr.prim] */
1635 else if (TREE_CODE (e) == OFFSET_REF)
1636 e = resolve_offset_ref (e);
1638 if (e == error_mark_node)
1639 return e;
1641 return c_sizeof (TREE_TYPE (e));
1644 tree
1645 c_sizeof_nowarn (type)
1646 tree type;
1648 enum tree_code code = TREE_CODE (type);
1649 tree size;
1651 if (code == FUNCTION_TYPE
1652 || code == METHOD_TYPE
1653 || code == VOID_TYPE
1654 || code == ERROR_MARK)
1655 size = size_one_node;
1656 else
1658 if (code == REFERENCE_TYPE)
1659 type = TREE_TYPE (type);
1661 if (!COMPLETE_TYPE_P (type))
1662 size = size_zero_node;
1663 else
1664 /* Convert in case a char is more than one unit. */
1665 size = size_binop (CEIL_DIV_EXPR, TYPE_SIZE_UNIT (type),
1666 size_int (TYPE_PRECISION (char_type_node)
1667 / BITS_PER_UNIT));
1670 /* SIZE will have an integer type with TYPE_IS_SIZETYPE set.
1671 TYPE_IS_SIZETYPE means that certain things (like overflow) will
1672 never happen. However, this node should really have type
1673 `size_t', which is just a typedef for an ordinary integer type. */
1674 size = fold (build1 (NOP_EXPR, c_size_type_node, size));
1675 my_friendly_assert (!TYPE_IS_SIZETYPE (TREE_TYPE (size)),
1676 20001021);
1677 return size;
1680 /* Perform the array-to-pointer and function-to-pointer conversions
1681 for EXP.
1683 In addition, references are converted to lvalues and manifest
1684 constants are replaced by their values. */
1686 tree
1687 decay_conversion (exp)
1688 tree exp;
1690 register tree type;
1691 register enum tree_code code;
1693 if (TREE_CODE (exp) == OFFSET_REF)
1694 exp = resolve_offset_ref (exp);
1696 type = TREE_TYPE (exp);
1697 code = TREE_CODE (type);
1699 if (code == REFERENCE_TYPE)
1701 exp = convert_from_reference (exp);
1702 type = TREE_TYPE (exp);
1703 code = TREE_CODE (type);
1706 if (type == error_mark_node)
1707 return error_mark_node;
1709 if (type_unknown_p (exp))
1711 incomplete_type_error (exp, TREE_TYPE (exp));
1712 return error_mark_node;
1715 /* Constants can be used directly unless they're not loadable. */
1716 if (TREE_CODE (exp) == CONST_DECL)
1717 exp = DECL_INITIAL (exp);
1718 /* Replace a nonvolatile const static variable with its value. We
1719 don't do this for arrays, though; we want the address of the
1720 first element of the array, not the address of the first element
1721 of its initializing constant. */
1722 else if (code != ARRAY_TYPE)
1724 exp = decl_constant_value (exp);
1725 type = TREE_TYPE (exp);
1728 /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
1729 Leave such NOP_EXPRs, since RHS is being used in non-lvalue context. */
1731 if (code == VOID_TYPE)
1733 error ("void value not ignored as it ought to be");
1734 return error_mark_node;
1736 if (code == METHOD_TYPE)
1737 my_friendly_abort (990506);
1738 if (code == FUNCTION_TYPE || is_overloaded_fn (exp))
1739 return build_unary_op (ADDR_EXPR, exp, 0);
1740 if (code == ARRAY_TYPE)
1742 register tree adr;
1743 tree ptrtype;
1745 if (TREE_CODE (exp) == INDIRECT_REF)
1747 /* Stripping away the INDIRECT_REF is not the right
1748 thing to do for references... */
1749 tree inner = TREE_OPERAND (exp, 0);
1750 if (TREE_CODE (TREE_TYPE (inner)) == REFERENCE_TYPE)
1752 inner = build1 (CONVERT_EXPR,
1753 build_pointer_type (TREE_TYPE
1754 (TREE_TYPE (inner))),
1755 inner);
1756 TREE_CONSTANT (inner) = TREE_CONSTANT (TREE_OPERAND (inner, 0));
1758 return cp_convert (build_pointer_type (TREE_TYPE (type)), inner);
1761 if (TREE_CODE (exp) == COMPOUND_EXPR)
1763 tree op1 = decay_conversion (TREE_OPERAND (exp, 1));
1764 return build (COMPOUND_EXPR, TREE_TYPE (op1),
1765 TREE_OPERAND (exp, 0), op1);
1768 if (!lvalue_p (exp)
1769 && ! (TREE_CODE (exp) == CONSTRUCTOR && TREE_STATIC (exp)))
1771 error ("invalid use of non-lvalue array");
1772 return error_mark_node;
1775 ptrtype = build_pointer_type (TREE_TYPE (type));
1777 if (TREE_CODE (exp) == VAR_DECL)
1779 /* ??? This is not really quite correct
1780 in that the type of the operand of ADDR_EXPR
1781 is not the target type of the type of the ADDR_EXPR itself.
1782 Question is, can this lossage be avoided? */
1783 adr = build1 (ADDR_EXPR, ptrtype, exp);
1784 if (mark_addressable (exp) == 0)
1785 return error_mark_node;
1786 TREE_CONSTANT (adr) = staticp (exp);
1787 TREE_SIDE_EFFECTS (adr) = 0; /* Default would be, same as EXP. */
1788 return adr;
1790 /* This way is better for a COMPONENT_REF since it can
1791 simplify the offset for a component. */
1792 adr = build_unary_op (ADDR_EXPR, exp, 1);
1793 return cp_convert (ptrtype, adr);
1796 /* [basic.lval]: Class rvalues can have cv-qualified types; non-class
1797 rvalues always have cv-unqualified types. */
1798 if (! CLASS_TYPE_P (type))
1799 exp = cp_convert (TYPE_MAIN_VARIANT (type), exp);
1801 return exp;
1804 tree
1805 default_conversion (exp)
1806 tree exp;
1808 tree type;
1809 enum tree_code code;
1811 exp = decay_conversion (exp);
1813 type = TREE_TYPE (exp);
1814 code = TREE_CODE (type);
1816 if (INTEGRAL_CODE_P (code))
1818 tree t = type_promotes_to (type);
1819 if (t != type)
1820 return cp_convert (t, exp);
1823 return exp;
1826 /* Take the address of an inline function without setting TREE_ADDRESSABLE
1827 or TREE_USED. */
1829 tree
1830 inline_conversion (exp)
1831 tree exp;
1833 if (TREE_CODE (exp) == FUNCTION_DECL)
1834 exp = build1 (ADDR_EXPR, build_pointer_type (TREE_TYPE (exp)), exp);
1836 return exp;
1839 /* Returns nonzero iff exp is a STRING_CST or the result of applying
1840 decay_conversion to one. */
1843 string_conv_p (totype, exp, warn)
1844 tree totype, exp;
1845 int warn;
1847 tree t;
1849 if (! flag_const_strings || TREE_CODE (totype) != POINTER_TYPE)
1850 return 0;
1852 t = TREE_TYPE (totype);
1853 if (!same_type_p (t, char_type_node)
1854 && !same_type_p (t, wchar_type_node))
1855 return 0;
1857 if (TREE_CODE (exp) == STRING_CST)
1859 /* Make sure that we don't try to convert between char and wchar_t. */
1860 if (!same_type_p (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (exp))), t))
1861 return 0;
1863 else
1865 /* Is this a string constant which has decayed to 'const char *'? */
1866 t = build_pointer_type (build_qualified_type (t, TYPE_QUAL_CONST));
1867 if (!same_type_p (TREE_TYPE (exp), t))
1868 return 0;
1869 STRIP_NOPS (exp);
1870 if (TREE_CODE (exp) != ADDR_EXPR
1871 || TREE_CODE (TREE_OPERAND (exp, 0)) != STRING_CST)
1872 return 0;
1875 /* This warning is not very useful, as it complains about printf. */
1876 if (warn && warn_write_strings)
1877 cp_warning ("deprecated conversion from string constant to `%T'", totype);
1879 return 1;
1882 tree
1883 build_object_ref (datum, basetype, field)
1884 tree datum, basetype, field;
1886 tree dtype;
1887 if (datum == error_mark_node)
1888 return error_mark_node;
1890 dtype = TREE_TYPE (datum);
1891 if (TREE_CODE (dtype) == REFERENCE_TYPE)
1892 dtype = TREE_TYPE (dtype);
1893 if (! IS_AGGR_TYPE_CODE (TREE_CODE (dtype)))
1895 cp_error ("request for member `%T::%D' in expression of non-aggregate type `%T'",
1896 basetype, field, dtype);
1897 return error_mark_node;
1899 else if (is_aggr_type (basetype, 1))
1901 tree binfo = binfo_or_else (basetype, dtype);
1902 if (binfo)
1903 return build_x_component_ref (build_scoped_ref (datum, basetype),
1904 field, binfo, 1);
1906 return error_mark_node;
1909 /* Like `build_component_ref, but uses an already found field, and converts
1910 from a reference. Must compute access for current_class_ref.
1911 Otherwise, ok. */
1913 tree
1914 build_component_ref_1 (datum, field, protect)
1915 tree datum, field;
1916 int protect;
1918 return convert_from_reference
1919 (build_component_ref (datum, field, NULL_TREE, protect));
1922 /* Given a COND_EXPR, MIN_EXPR, or MAX_EXPR in T, return it in a form that we
1923 can, for example, use as an lvalue. This code used to be in
1924 unary_complex_lvalue, but we needed it to deal with `a = (d == c) ? b : c'
1925 expressions, where we're dealing with aggregates. But now it's again only
1926 called from unary_complex_lvalue. The case (in particular) that led to
1927 this was with CODE == ADDR_EXPR, since it's not an lvalue when we'd
1928 get it there. */
1930 static tree
1931 rationalize_conditional_expr (code, t)
1932 enum tree_code code;
1933 tree t;
1935 /* For MIN_EXPR or MAX_EXPR, fold-const.c has arranged things so that
1936 the first operand is always the one to be used if both operands
1937 are equal, so we know what conditional expression this used to be. */
1938 if (TREE_CODE (t) == MIN_EXPR || TREE_CODE (t) == MAX_EXPR)
1940 return
1941 build_conditional_expr (build_x_binary_op ((TREE_CODE (t) == MIN_EXPR
1942 ? LE_EXPR : GE_EXPR),
1943 TREE_OPERAND (t, 0),
1944 TREE_OPERAND (t, 1)),
1945 build_unary_op (code, TREE_OPERAND (t, 0), 0),
1946 build_unary_op (code, TREE_OPERAND (t, 1), 0));
1949 return
1950 build_conditional_expr (TREE_OPERAND (t, 0),
1951 build_unary_op (code, TREE_OPERAND (t, 1), 0),
1952 build_unary_op (code, TREE_OPERAND (t, 2), 0));
1955 /* Given the TYPE of an anonymous union field inside T, return the
1956 FIELD_DECL for the field. If not found return NULL_TREE. Because
1957 anonymous unions can nest, we must also search all anonymous unions
1958 that are directly reachable. */
1960 static tree
1961 lookup_anon_field (t, type)
1962 tree t, type;
1964 tree field;
1966 for (field = TYPE_FIELDS (t); field; field = TREE_CHAIN (field))
1968 if (TREE_STATIC (field))
1969 continue;
1970 if (TREE_CODE (field) != FIELD_DECL)
1971 continue;
1973 /* If we find it directly, return the field. */
1974 if (DECL_NAME (field) == NULL_TREE
1975 && type == TYPE_MAIN_VARIANT (TREE_TYPE (field)))
1977 return field;
1980 /* Otherwise, it could be nested, search harder. */
1981 if (DECL_NAME (field) == NULL_TREE
1982 && ANON_AGGR_TYPE_P (TREE_TYPE (field)))
1984 tree subfield = lookup_anon_field (TREE_TYPE (field), type);
1985 if (subfield)
1986 return subfield;
1989 return NULL_TREE;
1992 /* Build a COMPONENT_REF for a given DATUM, and it's member COMPONENT.
1993 COMPONENT can be an IDENTIFIER_NODE that is the name of the member
1994 that we are interested in, or it can be a FIELD_DECL. */
1996 tree
1997 build_component_ref (datum, component, basetype_path, protect)
1998 tree datum, component, basetype_path;
1999 int protect;
2001 register tree basetype;
2002 register enum tree_code code;
2003 register tree field = NULL;
2004 register tree ref;
2005 tree field_type;
2006 int type_quals;
2008 if (processing_template_decl)
2009 return build_min_nt (COMPONENT_REF, datum, component);
2011 if (datum == error_mark_node
2012 || TREE_TYPE (datum) == error_mark_node)
2013 return error_mark_node;
2015 /* BASETYPE holds the type of the class containing the COMPONENT. */
2016 basetype = TYPE_MAIN_VARIANT (TREE_TYPE (datum));
2018 /* If DATUM is a COMPOUND_EXPR or COND_EXPR, move our reference
2019 inside it. */
2020 switch (TREE_CODE (datum))
2022 case COMPOUND_EXPR:
2024 tree value = build_component_ref (TREE_OPERAND (datum, 1), component,
2025 basetype_path, protect);
2026 return build (COMPOUND_EXPR, TREE_TYPE (value),
2027 TREE_OPERAND (datum, 0), value);
2029 case COND_EXPR:
2030 return build_conditional_expr
2031 (TREE_OPERAND (datum, 0),
2032 build_component_ref (TREE_OPERAND (datum, 1), component,
2033 basetype_path, protect),
2034 build_component_ref (TREE_OPERAND (datum, 2), component,
2035 basetype_path, protect));
2037 case TEMPLATE_DECL:
2038 cp_error ("invalid use of %D", datum);
2039 datum = error_mark_node;
2040 break;
2042 default:
2043 break;
2046 code = TREE_CODE (basetype);
2048 if (code == REFERENCE_TYPE)
2050 datum = convert_from_reference (datum);
2051 basetype = TYPE_MAIN_VARIANT (TREE_TYPE (datum));
2052 code = TREE_CODE (basetype);
2054 if (TREE_CODE (datum) == OFFSET_REF)
2056 datum = resolve_offset_ref (datum);
2057 basetype = TYPE_MAIN_VARIANT (TREE_TYPE (datum));
2058 code = TREE_CODE (basetype);
2061 /* First, see if there is a field or component with name COMPONENT. */
2062 if (TREE_CODE (component) == TREE_LIST)
2064 /* I could not trigger this code. MvL */
2065 my_friendly_abort (980326);
2066 #ifdef DEAD
2067 my_friendly_assert (!(TREE_CHAIN (component) == NULL_TREE
2068 && DECL_CHAIN (TREE_VALUE (component)) == NULL_TREE), 309);
2069 #endif
2070 return build (COMPONENT_REF, TREE_TYPE (component), datum, component);
2073 if (! IS_AGGR_TYPE_CODE (code))
2075 if (code != ERROR_MARK)
2076 cp_error ("request for member `%D' in `%E', which is of non-aggregate type `%T'",
2077 component, datum, basetype);
2078 return error_mark_node;
2081 if (!complete_type_or_else (basetype, datum))
2082 return error_mark_node;
2084 if (TREE_CODE (component) == BIT_NOT_EXPR)
2086 if (TYPE_IDENTIFIER (basetype) != TREE_OPERAND (component, 0))
2088 cp_error ("destructor specifier `%T::~%T' must have matching names",
2089 basetype, TREE_OPERAND (component, 0));
2090 return error_mark_node;
2092 if (! TYPE_HAS_DESTRUCTOR (basetype))
2094 cp_error ("type `%T' has no destructor", basetype);
2095 return error_mark_node;
2097 return TREE_VEC_ELT (CLASSTYPE_METHOD_VEC (basetype), 1);
2100 /* Look up component name in the structure type definition. */
2101 if (TYPE_VFIELD (basetype)
2102 && DECL_NAME (TYPE_VFIELD (basetype)) == component)
2103 /* Special-case this because if we use normal lookups in an ambiguous
2104 hierarchy, the compiler will abort (because vptr lookups are
2105 not supposed to be ambiguous. */
2106 field = TYPE_VFIELD (basetype);
2107 else if (TREE_CODE (component) == FIELD_DECL)
2108 field = component;
2109 else if (TREE_CODE (component) == TYPE_DECL)
2111 cp_error ("invalid use of type decl `%#D' as expression", component);
2112 return error_mark_node;
2114 else if (TREE_CODE (component) == TEMPLATE_DECL)
2116 cp_error ("invalid use of template `%#D' as expression", component);
2117 return error_mark_node;
2119 else
2121 tree name = component;
2122 if (TREE_CODE (component) == VAR_DECL)
2123 name = DECL_NAME (component);
2124 if (TREE_CODE (component) == NAMESPACE_DECL)
2125 /* Source is in error, but produce a sensible diagnostic. */
2126 name = DECL_NAME (component);
2127 if (basetype_path == NULL_TREE)
2128 basetype_path = TYPE_BINFO (basetype);
2129 field = lookup_field (basetype_path, name,
2130 protect && !VFIELD_NAME_P (name), 0);
2131 if (field == error_mark_node)
2132 return error_mark_node;
2134 if (field == NULL_TREE)
2136 /* Not found as a data field, look for it as a method. If found,
2137 then if this is the only possible one, return it, else
2138 report ambiguity error. */
2139 tree fndecls = lookup_fnfields (basetype_path, name, 1);
2140 if (fndecls == error_mark_node)
2141 return error_mark_node;
2142 if (fndecls)
2144 /* If the function is unique and static, we can resolve it
2145 now. Otherwise, we have to wait and see what context it is
2146 used in; a component_ref involving a non-static member
2147 function can only be used in a call (expr.ref). */
2149 if (TREE_CHAIN (fndecls) == NULL_TREE
2150 && TREE_CODE (TREE_VALUE (fndecls)) == FUNCTION_DECL)
2152 if (DECL_STATIC_FUNCTION_P (TREE_VALUE (fndecls)))
2154 tree fndecl = TREE_VALUE (fndecls);
2155 enforce_access (basetype_path, fndecl);
2156 mark_used (fndecl);
2157 return fndecl;
2159 else
2161 /* A unique non-static member function. Other parts
2162 of the compiler expect something with
2163 unknown_type_node to be really overloaded, so
2164 let's oblige. */
2165 TREE_VALUE (fndecls)
2166 = ovl_cons (TREE_VALUE (fndecls), NULL_TREE);
2170 ref = build (COMPONENT_REF, unknown_type_node,
2171 datum, TREE_VALUE (fndecls));
2172 return ref;
2175 cp_error ("`%#T' has no member named `%D'", basetype, name);
2176 return error_mark_node;
2178 else if (TREE_TYPE (field) == error_mark_node)
2179 return error_mark_node;
2181 if (TREE_CODE (field) != FIELD_DECL)
2183 if (TREE_CODE (field) == TYPE_DECL)
2184 cp_pedwarn ("invalid use of type decl `%#D' as expression", field);
2185 else if (DECL_RTL (field) != 0)
2186 mark_used (field);
2187 else
2188 TREE_USED (field) = 1;
2190 /* Do evaluate the object when accessing a static member. */
2191 if (TREE_SIDE_EFFECTS (datum))
2192 field = build (COMPOUND_EXPR, TREE_TYPE (field), datum, field);
2194 return field;
2198 /* See if we have to do any conversions so that we pick up the field from the
2199 right context. */
2200 if (DECL_FIELD_CONTEXT (field) != basetype)
2202 tree context = DECL_FIELD_CONTEXT (field);
2203 tree base = context;
2204 while (!same_type_p (base, basetype) && TYPE_NAME (base)
2205 && ANON_AGGR_TYPE_P (base))
2206 base = TYPE_CONTEXT (base);
2208 /* Handle base classes here... */
2209 if (base != basetype && TYPE_BASE_CONVS_MAY_REQUIRE_CODE_P (basetype))
2211 tree addr = build_unary_op (ADDR_EXPR, datum, 0);
2212 if (integer_zerop (addr))
2214 error ("invalid reference to NULL ptr, use ptr-to-member instead");
2215 return error_mark_node;
2217 addr = convert_pointer_to (base, addr);
2218 datum = build_indirect_ref (addr, NULL);
2219 if (datum == error_mark_node)
2220 return error_mark_node;
2222 basetype = base;
2224 /* Handle things from anon unions here... */
2225 if (TYPE_NAME (context) && ANON_AGGR_TYPE_P (context))
2227 tree subfield = lookup_anon_field (basetype, context);
2228 tree subdatum = build_component_ref (datum, subfield,
2229 basetype_path, protect);
2230 return build_component_ref (subdatum, field, basetype_path, protect);
2234 /* Compute the type of the field, as described in [expr.ref]. */
2235 type_quals = TYPE_UNQUALIFIED;
2236 field_type = TREE_TYPE (field);
2237 if (TREE_CODE (field_type) == REFERENCE_TYPE)
2238 /* The standard says that the type of the result should be the
2239 type referred to by the reference. But for now, at least, we
2240 do the conversion from reference type later. */
2242 else
2244 type_quals = (CP_TYPE_QUALS (field_type)
2245 | CP_TYPE_QUALS (TREE_TYPE (datum)));
2247 /* A field is const (volatile) if the enclosing object, or the
2248 field itself, is const (volatile). But, a mutable field is
2249 not const, even within a const object. */
2250 if (DECL_MUTABLE_P (field))
2251 type_quals &= ~TYPE_QUAL_CONST;
2252 field_type = cp_build_qualified_type (field_type, type_quals);
2255 ref = fold (build (COMPONENT_REF, field_type, datum, field));
2257 /* Mark the expression const or volatile, as appropriate. Even
2258 though we've dealt with the type above, we still have to mark the
2259 expression itself. */
2260 if (type_quals & TYPE_QUAL_CONST)
2261 TREE_READONLY (ref) = 1;
2262 else if (type_quals & TYPE_QUAL_VOLATILE)
2263 TREE_THIS_VOLATILE (ref) = 1;
2265 return ref;
2268 /* Variant of build_component_ref for use in expressions, which should
2269 never have REFERENCE_TYPE. */
2271 tree
2272 build_x_component_ref (datum, component, basetype_path, protect)
2273 tree datum, component, basetype_path;
2274 int protect;
2276 tree t = build_component_ref (datum, component, basetype_path, protect);
2278 if (! processing_template_decl)
2279 t = convert_from_reference (t);
2281 return t;
2284 /* Given an expression PTR for a pointer, return an expression
2285 for the value pointed to.
2286 ERRORSTRING is the name of the operator to appear in error messages.
2288 This function may need to overload OPERATOR_FNNAME.
2289 Must also handle REFERENCE_TYPEs for C++. */
2291 tree
2292 build_x_indirect_ref (ptr, errorstring)
2293 tree ptr;
2294 const char *errorstring;
2296 tree rval;
2298 if (processing_template_decl)
2299 return build_min_nt (INDIRECT_REF, ptr);
2301 rval = build_opfncall (INDIRECT_REF, LOOKUP_NORMAL, ptr, NULL_TREE,
2302 NULL_TREE);
2303 if (rval)
2304 return rval;
2305 return build_indirect_ref (ptr, errorstring);
2308 tree
2309 build_indirect_ref (ptr, errorstring)
2310 tree ptr;
2311 const char *errorstring;
2313 register tree pointer, type;
2315 if (ptr == error_mark_node)
2316 return error_mark_node;
2318 if (ptr == current_class_ptr)
2319 return current_class_ref;
2321 pointer = (TREE_CODE (TREE_TYPE (ptr)) == REFERENCE_TYPE
2322 ? ptr : default_conversion (ptr));
2323 type = TREE_TYPE (pointer);
2325 if (TYPE_PTR_P (type) || TREE_CODE (type) == REFERENCE_TYPE)
2327 /* [expr.unary.op]
2329 If the type of the expression is "pointer to T," the type
2330 of the result is "T."
2332 We must use the canonical variant because certain parts of
2333 the back end, like fold, do pointer comparisons between
2334 types. */
2335 tree t = canonical_type_variant (TREE_TYPE (type));
2337 if (VOID_TYPE_P (t))
2339 /* A pointer to incomplete type (other than cv void) can be
2340 dereferenced [expr.unary.op]/1 */
2341 cp_error ("`%T' is not a pointer-to-object type", type);
2342 return error_mark_node;
2344 else if (TREE_CODE (pointer) == ADDR_EXPR
2345 && !flag_volatile
2346 && same_type_p (t, TREE_TYPE (TREE_OPERAND (pointer, 0))))
2347 /* The POINTER was something like `&x'. We simplify `*&x' to
2348 `x'. */
2349 return TREE_OPERAND (pointer, 0);
2350 else
2352 tree ref = build1 (INDIRECT_REF, t, pointer);
2354 /* We *must* set TREE_READONLY when dereferencing a pointer to const,
2355 so that we get the proper error message if the result is used
2356 to assign to. Also, &* is supposed to be a no-op. */
2357 TREE_READONLY (ref) = CP_TYPE_CONST_P (t);
2358 TREE_THIS_VOLATILE (ref) = CP_TYPE_VOLATILE_P (t);
2359 TREE_SIDE_EFFECTS (ref)
2360 = (TREE_THIS_VOLATILE (ref) || TREE_SIDE_EFFECTS (pointer)
2361 || flag_volatile);
2362 return ref;
2365 /* `pointer' won't be an error_mark_node if we were given a
2366 pointer to member, so it's cool to check for this here. */
2367 else if (TYPE_PTRMEM_P (type) || TYPE_PTRMEMFUNC_P (type))
2368 error ("invalid use of `%s' on pointer to member", errorstring);
2369 else if (pointer != error_mark_node)
2371 if (errorstring)
2372 error ("invalid type argument of `%s'", errorstring);
2373 else
2374 error ("invalid type argument");
2376 return error_mark_node;
2379 /* This handles expressions of the form "a[i]", which denotes
2380 an array reference.
2382 This is logically equivalent in C to *(a+i), but we may do it differently.
2383 If A is a variable or a member, we generate a primitive ARRAY_REF.
2384 This avoids forcing the array out of registers, and can work on
2385 arrays that are not lvalues (for example, members of structures returned
2386 by functions).
2388 If INDEX is of some user-defined type, it must be converted to
2389 integer type. Otherwise, to make a compatible PLUS_EXPR, it
2390 will inherit the type of the array, which will be some pointer type. */
2392 tree
2393 build_array_ref (array, idx)
2394 tree array, idx;
2396 if (idx == 0)
2398 error ("subscript missing in array reference");
2399 return error_mark_node;
2402 if (TREE_TYPE (array) == error_mark_node
2403 || TREE_TYPE (idx) == error_mark_node)
2404 return error_mark_node;
2406 /* If ARRAY is a COMPOUND_EXPR or COND_EXPR, move our reference
2407 inside it. */
2408 switch (TREE_CODE (array))
2410 case COMPOUND_EXPR:
2412 tree value = build_array_ref (TREE_OPERAND (array, 1), idx);
2413 return build (COMPOUND_EXPR, TREE_TYPE (value),
2414 TREE_OPERAND (array, 0), value);
2417 case COND_EXPR:
2418 return build_conditional_expr
2419 (TREE_OPERAND (array, 0),
2420 build_array_ref (TREE_OPERAND (array, 1), idx),
2421 build_array_ref (TREE_OPERAND (array, 2), idx));
2423 default:
2424 break;
2427 if (TREE_CODE (TREE_TYPE (array)) == ARRAY_TYPE
2428 && TREE_CODE (array) != INDIRECT_REF)
2430 tree rval, type;
2432 /* Subscripting with type char is likely to lose
2433 on a machine where chars are signed.
2434 So warn on any machine, but optionally.
2435 Don't warn for unsigned char since that type is safe.
2436 Don't warn for signed char because anyone who uses that
2437 must have done so deliberately. */
2438 if (warn_char_subscripts
2439 && TYPE_MAIN_VARIANT (TREE_TYPE (idx)) == char_type_node)
2440 warning ("array subscript has type `char'");
2442 /* Apply default promotions *after* noticing character types. */
2443 idx = default_conversion (idx);
2445 if (TREE_CODE (TREE_TYPE (idx)) != INTEGER_TYPE)
2447 error ("array subscript is not an integer");
2448 return error_mark_node;
2451 /* An array that is indexed by a non-constant
2452 cannot be stored in a register; we must be able to do
2453 address arithmetic on its address.
2454 Likewise an array of elements of variable size. */
2455 if (TREE_CODE (idx) != INTEGER_CST
2456 || (COMPLETE_TYPE_P (TREE_TYPE (TREE_TYPE (array)))
2457 && (TREE_CODE (TYPE_SIZE (TREE_TYPE (TREE_TYPE (array))))
2458 != INTEGER_CST)))
2460 if (mark_addressable (array) == 0)
2461 return error_mark_node;
2464 /* An array that is indexed by a constant value which is not within
2465 the array bounds cannot be stored in a register either; because we
2466 would get a crash in store_bit_field/extract_bit_field when trying
2467 to access a non-existent part of the register. */
2468 if (TREE_CODE (idx) == INTEGER_CST
2469 && TYPE_VALUES (TREE_TYPE (array))
2470 && ! int_fits_type_p (idx, TYPE_VALUES (TREE_TYPE (array))))
2472 if (mark_addressable (array) == 0)
2473 return error_mark_node;
2476 if (pedantic && !lvalue_p (array))
2477 pedwarn ("ISO C++ forbids subscripting non-lvalue array");
2479 /* Note in C++ it is valid to subscript a `register' array, since
2480 it is valid to take the address of something with that
2481 storage specification. */
2482 if (extra_warnings)
2484 tree foo = array;
2485 while (TREE_CODE (foo) == COMPONENT_REF)
2486 foo = TREE_OPERAND (foo, 0);
2487 if (TREE_CODE (foo) == VAR_DECL && DECL_REGISTER (foo))
2488 warning ("subscripting array declared `register'");
2491 type = TREE_TYPE (TREE_TYPE (array));
2492 rval = build (ARRAY_REF, type, array, idx);
2493 /* Array ref is const/volatile if the array elements are
2494 or if the array is.. */
2495 TREE_READONLY (rval)
2496 |= (CP_TYPE_CONST_P (type) | TREE_READONLY (array));
2497 TREE_SIDE_EFFECTS (rval)
2498 |= (CP_TYPE_VOLATILE_P (type) | TREE_SIDE_EFFECTS (array));
2499 TREE_THIS_VOLATILE (rval)
2500 |= (CP_TYPE_VOLATILE_P (type) | TREE_THIS_VOLATILE (array));
2501 return require_complete_type (fold (rval));
2505 tree ar = default_conversion (array);
2506 tree ind = default_conversion (idx);
2508 /* Put the integer in IND to simplify error checking. */
2509 if (TREE_CODE (TREE_TYPE (ar)) == INTEGER_TYPE)
2511 tree temp = ar;
2512 ar = ind;
2513 ind = temp;
2516 if (ar == error_mark_node)
2517 return ar;
2519 if (TREE_CODE (TREE_TYPE (ar)) != POINTER_TYPE)
2521 error ("subscripted value is neither array nor pointer");
2522 return error_mark_node;
2524 if (TREE_CODE (TREE_TYPE (ind)) != INTEGER_TYPE)
2526 error ("array subscript is not an integer");
2527 return error_mark_node;
2530 return build_indirect_ref (cp_build_binary_op (PLUS_EXPR, ar, ind),
2531 "array indexing");
2535 /* Build a function call to function FUNCTION with parameters PARAMS.
2536 PARAMS is a list--a chain of TREE_LIST nodes--in which the
2537 TREE_VALUE of each node is a parameter-expression. The PARAMS do
2538 not include any object pointer that may be required. FUNCTION's
2539 data type may be a function type or a pointer-to-function.
2541 For C++: If FUNCTION's data type is a TREE_LIST, then the tree list
2542 is the list of possible methods that FUNCTION could conceivably
2543 be. If the list of methods comes from a class, then it will be
2544 a list of lists (where each element is associated with the class
2545 that produced it), otherwise it will be a simple list (for
2546 functions overloaded in global scope).
2548 In the first case, TREE_VALUE (function) is the head of one of those
2549 lists, and TREE_PURPOSE is the name of the function.
2551 In the second case, TREE_PURPOSE (function) is the function's
2552 name directly.
2554 DECL is the class instance variable, usually CURRENT_CLASS_REF.
2556 When calling a TEMPLATE_DECL, we don't require a complete return
2557 type. */
2559 tree
2560 build_x_function_call (function, params, decl)
2561 tree function, params, decl;
2563 tree type;
2564 tree template_id = NULL_TREE;
2565 int is_method;
2567 if (function == error_mark_node)
2568 return error_mark_node;
2570 if (processing_template_decl)
2571 return build_min_nt (CALL_EXPR, function, params, NULL_TREE);
2573 /* Save explicit template arguments if found */
2574 if (TREE_CODE (function) == TEMPLATE_ID_EXPR)
2576 template_id = function;
2577 function = TREE_OPERAND (function, 0);
2580 type = TREE_TYPE (function);
2582 if (TREE_CODE (type) == OFFSET_TYPE
2583 && TREE_TYPE (type) == unknown_type_node
2584 && TREE_CODE (function) == TREE_LIST
2585 && TREE_CHAIN (function) == NULL_TREE)
2587 /* Undo (Foo:bar)()... */
2588 type = TYPE_OFFSET_BASETYPE (type);
2589 function = TREE_VALUE (function);
2590 my_friendly_assert (TREE_CODE (function) == TREE_LIST, 999);
2591 my_friendly_assert (TREE_CHAIN (function) == NULL_TREE, 999);
2592 function = TREE_VALUE (function);
2593 if (TREE_CODE (function) == OVERLOAD)
2594 function = OVL_FUNCTION (function);
2595 my_friendly_assert (TREE_CODE (function) == FUNCTION_DECL, 999);
2596 function = DECL_NAME (function);
2597 return build_method_call (decl, function, params,
2598 TYPE_BINFO (type), LOOKUP_NORMAL);
2601 if (TREE_CODE (function) == OFFSET_REF
2602 && TREE_CODE (type) != METHOD_TYPE)
2603 function = resolve_offset_ref (function);
2605 if ((TREE_CODE (function) == FUNCTION_DECL
2606 && DECL_STATIC_FUNCTION_P (function))
2607 || (DECL_FUNCTION_TEMPLATE_P (function)
2608 && DECL_STATIC_FUNCTION_P (DECL_TEMPLATE_RESULT (function))))
2609 return build_member_call (DECL_CONTEXT (function),
2610 template_id
2611 ? template_id : DECL_NAME (function),
2612 params);
2614 is_method = ((TREE_CODE (function) == TREE_LIST
2615 && current_class_type != NULL_TREE
2616 && (IDENTIFIER_CLASS_VALUE (TREE_PURPOSE (function))
2617 == function))
2618 || (TREE_CODE (function) == OVERLOAD
2619 && DECL_FUNCTION_MEMBER_P (OVL_CURRENT (function)))
2620 || TREE_CODE (function) == IDENTIFIER_NODE
2621 || TREE_CODE (type) == METHOD_TYPE
2622 || TYPE_PTRMEMFUNC_P (type));
2624 /* A friend template. Make it look like a toplevel declaration. */
2625 if (! is_method && TREE_CODE (function) == TEMPLATE_DECL)
2626 function = ovl_cons (function, NULL_TREE);
2628 /* Handle methods, friends, and overloaded functions, respectively. */
2629 if (is_method)
2631 tree basetype = NULL_TREE;
2633 if (TREE_CODE (function) == OVERLOAD)
2634 function = OVL_CURRENT (function);
2636 if (TREE_CODE (function) == FUNCTION_DECL
2637 || DECL_FUNCTION_TEMPLATE_P (function))
2639 basetype = DECL_CONTEXT (function);
2641 if (DECL_NAME (function))
2642 function = DECL_NAME (function);
2643 else
2644 function = TYPE_IDENTIFIER (DECL_CONTEXT (function));
2646 else if (TREE_CODE (function) == TREE_LIST)
2648 my_friendly_assert (TREE_CODE (TREE_VALUE (function))
2649 == FUNCTION_DECL, 312);
2650 basetype = DECL_CONTEXT (TREE_VALUE (function));
2651 function = TREE_PURPOSE (function);
2653 else if (TREE_CODE (function) != IDENTIFIER_NODE)
2655 if (TREE_CODE (function) == OFFSET_REF)
2657 if (TREE_OPERAND (function, 0))
2658 decl = TREE_OPERAND (function, 0);
2660 /* Call via a pointer to member function. */
2661 if (decl == NULL_TREE)
2663 error ("pointer to member function called, but not in class scope");
2664 return error_mark_node;
2666 /* What other type of POINTER_TYPE could this be? */
2667 if (TREE_CODE (TREE_TYPE (function)) != POINTER_TYPE
2668 && ! TYPE_PTRMEMFUNC_P (TREE_TYPE (function))
2669 && TREE_CODE (function) != OFFSET_REF)
2670 function = build (OFFSET_REF, TREE_TYPE (type), NULL_TREE,
2671 function);
2672 goto do_x_function;
2675 /* this is an abbreviated method call.
2676 must go through here in case it is a virtual function.
2677 @@ Perhaps this could be optimized. */
2679 if (basetype && (! current_class_type
2680 || ! DERIVED_FROM_P (basetype, current_class_type)))
2681 return build_member_call (basetype, function, params);
2683 if (decl == NULL_TREE)
2685 if (current_class_type == NULL_TREE)
2687 cp_error ("object missing in call to method `%D'", function);
2688 return error_mark_node;
2690 /* Yow: call from a static member function. */
2691 decl = build_dummy_object (current_class_type);
2694 /* Put back explicit template arguments, if any. */
2695 if (template_id)
2696 function = template_id;
2697 return build_method_call (decl, function, params,
2698 NULL_TREE, LOOKUP_NORMAL);
2700 else if (TREE_CODE (function) == COMPONENT_REF
2701 && type == unknown_type_node)
2703 /* Undo what we did in build_component_ref. */
2704 decl = TREE_OPERAND (function, 0);
2705 function = TREE_OPERAND (function, 1);
2706 function = DECL_NAME (OVL_CURRENT (function));
2708 if (template_id)
2710 TREE_OPERAND (template_id, 0) = function;
2711 function = template_id;
2714 return build_method_call (decl, function, params,
2715 NULL_TREE, LOOKUP_NORMAL);
2717 else if (really_overloaded_fn (function))
2719 if (OVL_FUNCTION (function) == NULL_TREE)
2721 cp_error ("function `%D' declared overloaded, but no definitions appear with which to resolve it?!?",
2722 TREE_PURPOSE (function));
2723 return error_mark_node;
2725 else
2727 /* Put back explicit template arguments, if any. */
2728 if (template_id)
2729 function = template_id;
2730 return build_new_function_call (function, params);
2733 else
2734 /* Remove a potential OVERLOAD around it */
2735 function = OVL_CURRENT (function);
2737 do_x_function:
2738 if (TREE_CODE (function) == OFFSET_REF)
2740 /* If the component is a data element (or a virtual function), we play
2741 games here to make things work. */
2742 tree decl_addr;
2744 if (TREE_OPERAND (function, 0))
2745 decl = TREE_OPERAND (function, 0);
2746 else
2747 decl = current_class_ref;
2749 decl_addr = build_unary_op (ADDR_EXPR, decl, 0);
2751 /* Sigh. OFFSET_REFs are being used for too many things.
2752 They're being used both for -> and ->*, and we want to resolve
2753 the -> cases here, but leave the ->*. We could use
2754 resolve_offset_ref for those, too, but it would call
2755 get_member_function_from_ptrfunc and decl_addr wouldn't get
2756 updated properly. Nasty. */
2757 if (TREE_CODE (TREE_OPERAND (function, 1)) == FIELD_DECL)
2758 function = resolve_offset_ref (function);
2759 else
2760 function = TREE_OPERAND (function, 1);
2762 function = get_member_function_from_ptrfunc (&decl_addr, function);
2763 params = tree_cons (NULL_TREE, decl_addr, params);
2764 return build_function_call (function, params);
2767 type = TREE_TYPE (function);
2768 if (type != error_mark_node)
2770 if (TREE_CODE (type) == REFERENCE_TYPE)
2771 type = TREE_TYPE (type);
2773 if (IS_AGGR_TYPE (type))
2774 return build_opfncall (CALL_EXPR, LOOKUP_NORMAL, function, params, NULL_TREE);
2777 if (is_method)
2779 tree fntype = TREE_TYPE (function);
2780 tree ctypeptr = NULL_TREE;
2782 /* Explicitly named method? */
2783 if (TREE_CODE (function) == FUNCTION_DECL)
2784 ctypeptr = build_pointer_type (DECL_CLASS_CONTEXT (function));
2785 /* Expression with ptr-to-method type? It could either be a plain
2786 usage, or it might be a case where the ptr-to-method is being
2787 passed in as an argument. */
2788 else if (TYPE_PTRMEMFUNC_P (fntype))
2790 tree rec = TYPE_METHOD_BASETYPE (TREE_TYPE
2791 (TYPE_PTRMEMFUNC_FN_TYPE (fntype)));
2792 ctypeptr = build_pointer_type (rec);
2794 /* Unexpected node type? */
2795 else
2796 my_friendly_abort (116);
2797 if (decl == NULL_TREE)
2799 if (current_function_decl
2800 && DECL_STATIC_FUNCTION_P (current_function_decl))
2801 error ("invalid call to member function needing `this' in static member function scope");
2802 else
2803 error ("pointer to member function called, but not in class scope");
2804 return error_mark_node;
2806 if (TREE_CODE (TREE_TYPE (decl)) != POINTER_TYPE
2807 && ! TYPE_PTRMEMFUNC_P (TREE_TYPE (decl)))
2809 decl = build_unary_op (ADDR_EXPR, decl, 0);
2810 decl = convert_pointer_to (TREE_TYPE (ctypeptr), decl);
2812 else
2813 decl = build_c_cast (ctypeptr, decl);
2814 params = tree_cons (NULL_TREE, decl, params);
2817 return build_function_call (function, params);
2820 /* Resolve a pointer to member function. INSTANCE is the object
2821 instance to use, if the member points to a virtual member. */
2823 tree
2824 get_member_function_from_ptrfunc (instance_ptrptr, function)
2825 tree *instance_ptrptr;
2826 tree function;
2828 if (TREE_CODE (function) == OFFSET_REF)
2830 function = TREE_OPERAND (function, 1);
2833 if (TYPE_PTRMEMFUNC_P (TREE_TYPE (function)))
2835 tree fntype, idx, e1, delta, delta2, e2, e3, vtbl;
2836 tree instance, basetype;
2838 tree instance_ptr = *instance_ptrptr;
2840 if (instance_ptr == error_mark_node
2841 && TREE_CODE (function) == PTRMEM_CST)
2843 /* Extracting the function address from a pmf is only
2844 allowed with -Wno-pmf-conversions. It only works for
2845 pmf constants. */
2846 e1 = build_addr_func (PTRMEM_CST_MEMBER (function));
2847 e1 = convert (TYPE_PTRMEMFUNC_FN_TYPE (TREE_TYPE (function)), e1);
2848 return e1;
2851 if (TREE_SIDE_EFFECTS (instance_ptr))
2852 instance_ptr = save_expr (instance_ptr);
2854 if (TREE_SIDE_EFFECTS (function))
2855 function = save_expr (function);
2857 fntype = TYPE_PTRMEMFUNC_FN_TYPE (TREE_TYPE (function));
2858 basetype = TYPE_METHOD_BASETYPE (TREE_TYPE (fntype));
2860 /* Convert down to the right base, before using the instance. */
2861 instance = convert_pointer_to_real (basetype, instance_ptr);
2862 if (instance == error_mark_node && instance_ptr != error_mark_node)
2863 return instance;
2865 e3 = PFN_FROM_PTRMEMFUNC (function);
2867 vtbl = convert_pointer_to (ptr_type_node, instance);
2868 delta = cp_convert (ptrdiff_type_node,
2869 build_component_ref (function, delta_identifier,
2870 NULL_TREE, 0));
2872 /* This used to avoid checking for virtual functions if basetype
2873 has no virtual functions, according to an earlier ANSI draft.
2874 With the final ISO C++ rules, such an optimization is
2875 incorrect: A pointer to a derived member can be static_cast
2876 to pointer-to-base-member, as long as the dynamic object
2877 later has the right member. */
2879 /* Promoting idx before saving it improves performance on RISC
2880 targets. Without promoting, the first compare used
2881 load-with-sign-extend, while the second used normal load then
2882 shift to sign-extend. An optimizer flaw, perhaps, but it's
2883 easier to make this change. */
2884 switch (TARGET_PTRMEMFUNC_VBIT_LOCATION)
2886 case ptrmemfunc_vbit_in_pfn:
2887 idx = cp_build_binary_op (TRUNC_DIV_EXPR,
2888 build1 (NOP_EXPR, vtable_index_type, e3),
2889 TYPE_SIZE_UNIT (vtable_entry_type));
2890 e1 = cp_build_binary_op (BIT_AND_EXPR,
2891 build1 (NOP_EXPR, vtable_index_type, e3),
2892 integer_one_node);
2893 break;
2895 case ptrmemfunc_vbit_in_delta:
2896 idx = build1 (NOP_EXPR, vtable_index_type, e3);
2897 e1 = cp_build_binary_op (BIT_AND_EXPR,
2898 delta, integer_one_node);
2899 delta = cp_build_binary_op (RSHIFT_EXPR,
2900 build1 (NOP_EXPR, vtable_index_type,
2901 delta),
2902 integer_one_node);
2903 break;
2905 default:
2906 abort ();
2909 /* DELTA2 is the amount by which to adjust the `this' pointer
2910 to find the vtbl. */
2911 delta2 = delta;
2912 vtbl = build
2913 (PLUS_EXPR,
2914 build_pointer_type (build_pointer_type (vtable_entry_type)),
2915 vtbl, cp_convert (ptrdiff_type_node, delta2));
2916 vtbl = build_indirect_ref (vtbl, NULL);
2917 e2 = build_array_ref (vtbl, idx);
2919 /* When using function descriptors, the address of the
2920 vtable entry is treated as a function pointer. */
2921 if (TARGET_VTABLE_USES_DESCRIPTORS)
2922 e2 = build1 (NOP_EXPR, TREE_TYPE (e2),
2923 build_unary_op (ADDR_EXPR, e2, /*noconvert=*/1));
2925 TREE_TYPE (e2) = TREE_TYPE (e3);
2926 e1 = build_conditional_expr (e1, e2, e3);
2928 /* Make sure this doesn't get evaluated first inside one of the
2929 branches of the COND_EXPR. */
2930 if (TREE_CODE (instance_ptr) == SAVE_EXPR)
2931 e1 = build (COMPOUND_EXPR, TREE_TYPE (e1),
2932 instance_ptr, e1);
2934 *instance_ptrptr = build (PLUS_EXPR, TREE_TYPE (instance_ptr),
2935 instance_ptr, delta);
2937 if (instance_ptr == error_mark_node
2938 && TREE_CODE (e1) != ADDR_EXPR
2939 && TREE_CODE (TREE_OPERAND (e1, 0)) != FUNCTION_DECL)
2940 cp_error ("object missing in `%E'", function);
2942 function = e1;
2944 return function;
2947 tree
2948 build_function_call_real (function, params, require_complete, flags)
2949 tree function, params;
2950 int require_complete, flags;
2952 register tree fntype, fndecl;
2953 register tree value_type;
2954 register tree coerced_params;
2955 tree result;
2956 tree name = NULL_TREE, assembler_name = NULL_TREE;
2957 int is_method;
2959 /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
2960 Strip such NOP_EXPRs, since FUNCTION is used in non-lvalue context. */
2961 if (TREE_CODE (function) == NOP_EXPR
2962 && TREE_TYPE (function) == TREE_TYPE (TREE_OPERAND (function, 0)))
2963 function = TREE_OPERAND (function, 0);
2965 if (TREE_CODE (function) == FUNCTION_DECL)
2967 name = DECL_NAME (function);
2968 assembler_name = DECL_ASSEMBLER_NAME (function);
2970 GNU_xref_call (current_function_decl,
2971 IDENTIFIER_POINTER (name ? name
2972 : TYPE_IDENTIFIER (DECL_CLASS_CONTEXT
2973 (function))));
2974 mark_used (function);
2975 fndecl = function;
2977 /* Convert anything with function type to a pointer-to-function. */
2978 if (pedantic && DECL_MAIN_P (function))
2979 pedwarn ("ISO C++ forbids calling `::main' from within program");
2981 /* Differs from default_conversion by not setting TREE_ADDRESSABLE
2982 (because calling an inline function does not mean the function
2983 needs to be separately compiled). */
2985 if (DECL_INLINE (function))
2986 function = inline_conversion (function);
2987 else
2988 function = build_addr_func (function);
2990 else
2992 fndecl = NULL_TREE;
2994 function = build_addr_func (function);
2997 if (function == error_mark_node)
2998 return error_mark_node;
3000 fntype = TREE_TYPE (function);
3002 if (TYPE_PTRMEMFUNC_P (fntype))
3004 cp_error ("must use .* or ->* to call pointer-to-member function in `%E (...)'",
3005 function);
3006 return error_mark_node;
3009 is_method = (TREE_CODE (fntype) == POINTER_TYPE
3010 && TREE_CODE (TREE_TYPE (fntype)) == METHOD_TYPE);
3012 if (!((TREE_CODE (fntype) == POINTER_TYPE
3013 && TREE_CODE (TREE_TYPE (fntype)) == FUNCTION_TYPE)
3014 || is_method
3015 || TREE_CODE (function) == TEMPLATE_ID_EXPR))
3017 cp_error ("`%E' cannot be used as a function", function);
3018 return error_mark_node;
3021 /* fntype now gets the type of function pointed to. */
3022 fntype = TREE_TYPE (fntype);
3024 /* Convert the parameters to the types declared in the
3025 function prototype, or apply default promotions. */
3027 if (flags & LOOKUP_COMPLAIN)
3028 coerced_params = convert_arguments (TYPE_ARG_TYPES (fntype),
3029 params, fndecl, LOOKUP_NORMAL);
3030 else
3031 coerced_params = convert_arguments (TYPE_ARG_TYPES (fntype),
3032 params, fndecl, 0);
3034 if (coerced_params == error_mark_node)
3036 if (flags & LOOKUP_SPECULATIVELY)
3037 return NULL_TREE;
3038 else
3039 return error_mark_node;
3042 /* Check for errors in format strings. */
3044 if (warn_format)
3045 check_function_format (NULL, TYPE_ATTRIBUTES (fntype), coerced_params);
3047 /* Recognize certain built-in functions so we can make tree-codes
3048 other than CALL_EXPR. We do this when it enables fold-const.c
3049 to do something useful. */
3051 if (TREE_CODE (function) == ADDR_EXPR
3052 && TREE_CODE (TREE_OPERAND (function, 0)) == FUNCTION_DECL
3053 && DECL_BUILT_IN (TREE_OPERAND (function, 0)))
3055 result = expand_tree_builtin (TREE_OPERAND (function, 0),
3056 params, coerced_params);
3057 if (result)
3058 return result;
3061 /* Some built-in function calls will be evaluated at
3062 compile-time in fold (). */
3063 result = fold (build_call (function, coerced_params));
3064 value_type = TREE_TYPE (result);
3066 if (require_complete)
3068 if (TREE_CODE (value_type) == VOID_TYPE)
3069 return result;
3070 result = require_complete_type (result);
3072 if (IS_AGGR_TYPE (value_type))
3073 result = build_cplus_new (value_type, result);
3074 return convert_from_reference (result);
3077 tree
3078 build_function_call (function, params)
3079 tree function, params;
3081 return build_function_call_real (function, params, 1, LOOKUP_NORMAL);
3084 /* Convert the actual parameter expressions in the list VALUES
3085 to the types in the list TYPELIST.
3086 If parmdecls is exhausted, or when an element has NULL as its type,
3087 perform the default conversions.
3089 NAME is an IDENTIFIER_NODE or 0. It is used only for error messages.
3091 This is also where warnings about wrong number of args are generated.
3093 Return a list of expressions for the parameters as converted.
3095 Both VALUES and the returned value are chains of TREE_LIST nodes
3096 with the elements of the list in the TREE_VALUE slots of those nodes.
3098 In C++, unspecified trailing parameters can be filled in with their
3099 default arguments, if such were specified. Do so here. */
3101 tree
3102 convert_arguments (typelist, values, fndecl, flags)
3103 tree typelist, values, fndecl;
3104 int flags;
3106 register tree typetail, valtail;
3107 register tree result = NULL_TREE;
3108 const char *called_thing = 0;
3109 int i = 0;
3111 /* Argument passing is always copy-initialization. */
3112 flags |= LOOKUP_ONLYCONVERTING;
3114 if (fndecl)
3116 if (TREE_CODE (TREE_TYPE (fndecl)) == METHOD_TYPE)
3118 if (DECL_NAME (fndecl) == NULL_TREE
3119 || IDENTIFIER_HAS_TYPE_VALUE (DECL_NAME (fndecl)))
3120 called_thing = "constructor";
3121 else
3122 called_thing = "member function";
3124 else
3125 called_thing = "function";
3128 for (valtail = values, typetail = typelist;
3129 valtail;
3130 valtail = TREE_CHAIN (valtail), i++)
3132 register tree type = typetail ? TREE_VALUE (typetail) : 0;
3133 register tree val = TREE_VALUE (valtail);
3135 if (val == error_mark_node)
3136 return error_mark_node;
3138 if (type == void_type_node)
3140 if (fndecl)
3142 cp_error_at ("too many arguments to %s `%+#D'", called_thing,
3143 fndecl);
3144 error ("at this point in file");
3146 else
3147 error ("too many arguments to function");
3148 /* In case anybody wants to know if this argument
3149 list is valid. */
3150 if (result)
3151 TREE_TYPE (tree_last (result)) = error_mark_node;
3152 break;
3155 if (TREE_CODE (val) == OFFSET_REF)
3156 val = resolve_offset_ref (val);
3158 /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
3159 Strip such NOP_EXPRs, since VAL is used in non-lvalue context. */
3160 if (TREE_CODE (val) == NOP_EXPR
3161 && TREE_TYPE (val) == TREE_TYPE (TREE_OPERAND (val, 0))
3162 && (type == 0 || TREE_CODE (type) != REFERENCE_TYPE))
3163 val = TREE_OPERAND (val, 0);
3165 if (type == 0 || TREE_CODE (type) != REFERENCE_TYPE)
3167 if (TREE_CODE (TREE_TYPE (val)) == ARRAY_TYPE
3168 || TREE_CODE (TREE_TYPE (val)) == FUNCTION_TYPE
3169 || TREE_CODE (TREE_TYPE (val)) == METHOD_TYPE)
3170 val = default_conversion (val);
3173 if (val == error_mark_node)
3174 return error_mark_node;
3176 if (type != 0)
3178 /* Formal parm type is specified by a function prototype. */
3179 tree parmval;
3181 if (!COMPLETE_TYPE_P (complete_type (type)))
3183 error ("parameter type of called function is incomplete");
3184 parmval = val;
3186 else
3188 parmval = convert_for_initialization
3189 (NULL_TREE, type, val, flags,
3190 "argument passing", fndecl, i);
3191 if (PROMOTE_PROTOTYPES
3192 && INTEGRAL_TYPE_P (type)
3193 && (TYPE_PRECISION (type)
3194 < TYPE_PRECISION (integer_type_node)))
3195 parmval = default_conversion (parmval);
3198 if (parmval == error_mark_node)
3199 return error_mark_node;
3201 result = tree_cons (NULL_TREE, parmval, result);
3203 else
3205 if (TREE_CODE (TREE_TYPE (val)) == REFERENCE_TYPE)
3206 val = convert_from_reference (val);
3208 if (fndecl && DECL_BUILT_IN (fndecl)
3209 && DECL_FUNCTION_CODE (fndecl) == BUILT_IN_CONSTANT_P)
3210 /* Don't do ellipsis conversion for __built_in_constant_p
3211 as this will result in spurious warnings for non-POD
3212 types. */
3213 val = require_complete_type (val);
3214 else
3215 val = convert_arg_to_ellipsis (val);
3217 result = tree_cons (NULL_TREE, val, result);
3220 if (typetail)
3221 typetail = TREE_CHAIN (typetail);
3224 if (typetail != 0 && typetail != void_list_node)
3226 /* See if there are default arguments that can be used */
3227 if (TREE_PURPOSE (typetail))
3229 for (; typetail != void_list_node; ++i)
3231 tree parmval
3232 = convert_default_arg (TREE_VALUE (typetail),
3233 TREE_PURPOSE (typetail),
3234 fndecl, i);
3236 if (parmval == error_mark_node)
3237 return error_mark_node;
3239 result = tree_cons (0, parmval, result);
3240 typetail = TREE_CHAIN (typetail);
3241 /* ends with `...'. */
3242 if (typetail == NULL_TREE)
3243 break;
3246 else
3248 if (fndecl)
3250 cp_error_at ("too few arguments to %s `%+#D'",
3251 called_thing, fndecl);
3252 error ("at this point in file");
3254 else
3255 error ("too few arguments to function");
3256 return error_mark_list;
3260 return nreverse (result);
3263 /* Build a binary-operation expression, after performing default
3264 conversions on the operands. CODE is the kind of expression to build. */
3266 tree
3267 build_x_binary_op (code, arg1, arg2)
3268 enum tree_code code;
3269 tree arg1, arg2;
3271 if (processing_template_decl)
3272 return build_min_nt (code, arg1, arg2);
3274 return build_new_op (code, LOOKUP_NORMAL, arg1, arg2, NULL_TREE);
3277 /* Build a binary-operation expression without default conversions.
3278 CODE is the kind of expression to build.
3279 This function differs from `build' in several ways:
3280 the data type of the result is computed and recorded in it,
3281 warnings are generated if arg data types are invalid,
3282 special handling for addition and subtraction of pointers is known,
3283 and some optimization is done (operations on narrow ints
3284 are done in the narrower type when that gives the same result).
3285 Constant folding is also done before the result is returned.
3287 Note that the operands will never have enumeral types
3288 because either they have just had the default conversions performed
3289 or they have both just been converted to some other type in which
3290 the arithmetic is to be done.
3292 C++: must do special pointer arithmetic when implementing
3293 multiple inheritance, and deal with pointer to member functions. */
3295 tree
3296 build_binary_op (code, orig_op0, orig_op1, convert_p)
3297 enum tree_code code;
3298 tree orig_op0, orig_op1;
3299 int convert_p ATTRIBUTE_UNUSED;
3301 tree op0, op1;
3302 register enum tree_code code0, code1;
3303 tree type0, type1;
3305 /* Expression code to give to the expression when it is built.
3306 Normally this is CODE, which is what the caller asked for,
3307 but in some special cases we change it. */
3308 register enum tree_code resultcode = code;
3310 /* Data type in which the computation is to be performed.
3311 In the simplest cases this is the common type of the arguments. */
3312 register tree result_type = NULL;
3314 /* Nonzero means operands have already been type-converted
3315 in whatever way is necessary.
3316 Zero means they need to be converted to RESULT_TYPE. */
3317 int converted = 0;
3319 /* Nonzero means create the expression with this type, rather than
3320 RESULT_TYPE. */
3321 tree build_type = 0;
3323 /* Nonzero means after finally constructing the expression
3324 convert it to this type. */
3325 tree final_type = 0;
3327 /* Nonzero if this is an operation like MIN or MAX which can
3328 safely be computed in short if both args are promoted shorts.
3329 Also implies COMMON.
3330 -1 indicates a bitwise operation; this makes a difference
3331 in the exact conditions for when it is safe to do the operation
3332 in a narrower mode. */
3333 int shorten = 0;
3335 /* Nonzero if this is a comparison operation;
3336 if both args are promoted shorts, compare the original shorts.
3337 Also implies COMMON. */
3338 int short_compare = 0;
3340 /* Nonzero if this is a right-shift operation, which can be computed on the
3341 original short and then promoted if the operand is a promoted short. */
3342 int short_shift = 0;
3344 /* Nonzero means set RESULT_TYPE to the common type of the args. */
3345 int common = 0;
3347 /* Apply default conversions. */
3348 op0 = orig_op0;
3349 op1 = orig_op1;
3351 if (code == TRUTH_AND_EXPR || code == TRUTH_ANDIF_EXPR
3352 || code == TRUTH_OR_EXPR || code == TRUTH_ORIF_EXPR
3353 || code == TRUTH_XOR_EXPR)
3355 if (!really_overloaded_fn (op0))
3356 op0 = decay_conversion (op0);
3357 if (!really_overloaded_fn (op1))
3358 op1 = decay_conversion (op1);
3360 else
3362 if (!really_overloaded_fn (op0))
3363 op0 = default_conversion (op0);
3364 if (!really_overloaded_fn (op1))
3365 op1 = default_conversion (op1);
3368 /* Strip NON_LVALUE_EXPRs, etc., since we aren't using as an lvalue. */
3369 STRIP_TYPE_NOPS (op0);
3370 STRIP_TYPE_NOPS (op1);
3372 /* DTRT if one side is an overloaded function, but complain about it. */
3373 if (type_unknown_p (op0))
3375 tree t = instantiate_type (TREE_TYPE (op1), op0, itf_none);
3376 if (t != error_mark_node)
3378 cp_pedwarn ("assuming cast to type `%T' from overloaded function",
3379 TREE_TYPE (t));
3380 op0 = t;
3383 if (type_unknown_p (op1))
3385 tree t = instantiate_type (TREE_TYPE (op0), op1, itf_none);
3386 if (t != error_mark_node)
3388 cp_pedwarn ("assuming cast to type `%T' from overloaded function",
3389 TREE_TYPE (t));
3390 op1 = t;
3394 type0 = TREE_TYPE (op0);
3395 type1 = TREE_TYPE (op1);
3397 /* The expression codes of the data types of the arguments tell us
3398 whether the arguments are integers, floating, pointers, etc. */
3399 code0 = TREE_CODE (type0);
3400 code1 = TREE_CODE (type1);
3402 /* If an error was already reported for one of the arguments,
3403 avoid reporting another error. */
3405 if (code0 == ERROR_MARK || code1 == ERROR_MARK)
3406 return error_mark_node;
3408 switch (code)
3410 case PLUS_EXPR:
3411 /* Handle the pointer + int case. */
3412 if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
3413 return pointer_int_sum (PLUS_EXPR, op0, op1);
3414 else if (code1 == POINTER_TYPE && code0 == INTEGER_TYPE)
3415 return pointer_int_sum (PLUS_EXPR, op1, op0);
3416 else
3417 common = 1;
3418 break;
3420 case MINUS_EXPR:
3421 /* Subtraction of two similar pointers.
3422 We must subtract them as integers, then divide by object size. */
3423 if (code0 == POINTER_TYPE && code1 == POINTER_TYPE
3424 && comp_target_types (type0, type1, 1))
3425 return pointer_diff (op0, op1, common_type (type0, type1));
3426 /* Handle pointer minus int. Just like pointer plus int. */
3427 else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
3428 return pointer_int_sum (MINUS_EXPR, op0, op1);
3429 else
3430 common = 1;
3431 break;
3433 case MULT_EXPR:
3434 common = 1;
3435 break;
3437 case TRUNC_DIV_EXPR:
3438 case CEIL_DIV_EXPR:
3439 case FLOOR_DIV_EXPR:
3440 case ROUND_DIV_EXPR:
3441 case EXACT_DIV_EXPR:
3442 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE
3443 || code0 == COMPLEX_TYPE)
3444 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
3445 || code1 == COMPLEX_TYPE))
3447 if (TREE_CODE (op1) == INTEGER_CST && integer_zerop (op1))
3448 cp_warning ("division by zero in `%E / 0'", op0);
3449 else if (TREE_CODE (op1) == REAL_CST && real_zerop (op1))
3450 cp_warning ("division by zero in `%E / 0.'", op0);
3452 if (!(code0 == INTEGER_TYPE && code1 == INTEGER_TYPE))
3453 resultcode = RDIV_EXPR;
3454 else
3455 /* When dividing two signed integers, we have to promote to int.
3456 unless we divide by a constant != -1. Note that default
3457 conversion will have been performed on the operands at this
3458 point, so we have to dig out the original type to find out if
3459 it was unsigned. */
3460 shorten = ((TREE_CODE (op0) == NOP_EXPR
3461 && TREE_UNSIGNED (TREE_TYPE (TREE_OPERAND (op0, 0))))
3462 || (TREE_CODE (op1) == INTEGER_CST
3463 && ! integer_all_onesp (op1)));
3465 common = 1;
3467 break;
3469 case BIT_AND_EXPR:
3470 case BIT_ANDTC_EXPR:
3471 case BIT_IOR_EXPR:
3472 case BIT_XOR_EXPR:
3473 if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
3474 shorten = -1;
3475 /* If one operand is a constant, and the other is a short type
3476 that has been converted to an int,
3477 really do the work in the short type and then convert the
3478 result to int. If we are lucky, the constant will be 0 or 1
3479 in the short type, making the entire operation go away. */
3480 if (TREE_CODE (op0) == INTEGER_CST
3481 && TREE_CODE (op1) == NOP_EXPR
3482 && (TYPE_PRECISION (type1)
3483 > TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (op1, 0))))
3484 && TREE_UNSIGNED (TREE_TYPE (TREE_OPERAND (op1, 0))))
3486 final_type = result_type;
3487 op1 = TREE_OPERAND (op1, 0);
3488 result_type = TREE_TYPE (op1);
3490 if (TREE_CODE (op1) == INTEGER_CST
3491 && TREE_CODE (op0) == NOP_EXPR
3492 && (TYPE_PRECISION (type0)
3493 > TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (op0, 0))))
3494 && TREE_UNSIGNED (TREE_TYPE (TREE_OPERAND (op0, 0))))
3496 final_type = result_type;
3497 op0 = TREE_OPERAND (op0, 0);
3498 result_type = TREE_TYPE (op0);
3500 break;
3502 case TRUNC_MOD_EXPR:
3503 case FLOOR_MOD_EXPR:
3504 if (code1 == INTEGER_TYPE && integer_zerop (op1))
3505 cp_warning ("division by zero in `%E %% 0'", op0);
3506 else if (code1 == REAL_TYPE && real_zerop (op1))
3507 cp_warning ("division by zero in `%E %% 0.'", op0);
3509 if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
3511 /* Although it would be tempting to shorten always here, that loses
3512 on some targets, since the modulo instruction is undefined if the
3513 quotient can't be represented in the computation mode. We shorten
3514 only if unsigned or if dividing by something we know != -1. */
3515 shorten = ((TREE_CODE (op0) == NOP_EXPR
3516 && TREE_UNSIGNED (TREE_TYPE (TREE_OPERAND (op0, 0))))
3517 || (TREE_CODE (op1) == INTEGER_CST
3518 && ! integer_all_onesp (op1)));
3519 common = 1;
3521 break;
3523 case TRUTH_ANDIF_EXPR:
3524 case TRUTH_ORIF_EXPR:
3525 case TRUTH_AND_EXPR:
3526 case TRUTH_OR_EXPR:
3527 result_type = boolean_type_node;
3528 break;
3530 /* Shift operations: result has same type as first operand;
3531 always convert second operand to int.
3532 Also set SHORT_SHIFT if shifting rightward. */
3534 case RSHIFT_EXPR:
3535 if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
3537 result_type = type0;
3538 if (TREE_CODE (op1) == INTEGER_CST)
3540 if (tree_int_cst_lt (op1, integer_zero_node))
3541 warning ("right shift count is negative");
3542 else
3544 if (! integer_zerop (op1))
3545 short_shift = 1;
3546 if (compare_tree_int (op1, TYPE_PRECISION (type0)) >= 0)
3547 warning ("right shift count >= width of type");
3550 /* Convert the shift-count to an integer, regardless of
3551 size of value being shifted. */
3552 if (TYPE_MAIN_VARIANT (TREE_TYPE (op1)) != integer_type_node)
3553 op1 = cp_convert (integer_type_node, op1);
3554 /* Avoid converting op1 to result_type later. */
3555 converted = 1;
3557 break;
3559 case LSHIFT_EXPR:
3560 if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
3562 result_type = type0;
3563 if (TREE_CODE (op1) == INTEGER_CST)
3565 if (tree_int_cst_lt (op1, integer_zero_node))
3566 warning ("left shift count is negative");
3567 else if (compare_tree_int (op1, TYPE_PRECISION (type0)) >= 0)
3568 warning ("left shift count >= width of type");
3570 /* Convert the shift-count to an integer, regardless of
3571 size of value being shifted. */
3572 if (TYPE_MAIN_VARIANT (TREE_TYPE (op1)) != integer_type_node)
3573 op1 = cp_convert (integer_type_node, op1);
3574 /* Avoid converting op1 to result_type later. */
3575 converted = 1;
3577 break;
3579 case RROTATE_EXPR:
3580 case LROTATE_EXPR:
3581 if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
3583 result_type = type0;
3584 if (TREE_CODE (op1) == INTEGER_CST)
3586 if (tree_int_cst_lt (op1, integer_zero_node))
3587 warning ("%s rotate count is negative",
3588 (code == LROTATE_EXPR) ? "left" : "right");
3589 else if (compare_tree_int (op1, TYPE_PRECISION (type0)) >= 0)
3590 warning ("%s rotate count >= width of type",
3591 (code == LROTATE_EXPR) ? "left" : "right");
3593 /* Convert the shift-count to an integer, regardless of
3594 size of value being shifted. */
3595 if (TYPE_MAIN_VARIANT (TREE_TYPE (op1)) != integer_type_node)
3596 op1 = cp_convert (integer_type_node, op1);
3598 break;
3600 case EQ_EXPR:
3601 case NE_EXPR:
3602 if (warn_float_equal && (code0 == REAL_TYPE || code1 == REAL_TYPE))
3603 warning ("comparing floating point with == or != is unsafe");
3605 build_type = boolean_type_node;
3606 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE
3607 || code0 == COMPLEX_TYPE)
3608 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
3609 || code1 == COMPLEX_TYPE))
3610 short_compare = 1;
3611 else if (code0 == POINTER_TYPE && code1 == POINTER_TYPE)
3612 result_type = composite_pointer_type (type0, type1, op0, op1,
3613 "comparison");
3614 else if (code0 == POINTER_TYPE && null_ptr_cst_p (op1))
3615 result_type = type0;
3616 else if (code1 == POINTER_TYPE && null_ptr_cst_p (op0))
3617 result_type = type1;
3618 else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
3620 result_type = type0;
3621 error ("ISO C++ forbids comparison between pointer and integer");
3623 else if (code0 == INTEGER_TYPE && code1 == POINTER_TYPE)
3625 result_type = type1;
3626 error ("ISO C++ forbids comparison between pointer and integer");
3628 else if (TYPE_PTRMEMFUNC_P (type0) && null_ptr_cst_p (op1))
3630 op0 = build_component_ref (op0, pfn_identifier, NULL_TREE, 0);
3631 op1 = cp_convert (TREE_TYPE (op0), integer_zero_node);
3632 result_type = TREE_TYPE (op0);
3634 else if (TYPE_PTRMEMFUNC_P (type1) && null_ptr_cst_p (op0))
3635 return cp_build_binary_op (code, op1, op0);
3636 else if (TYPE_PTRMEMFUNC_P (type0) && TYPE_PTRMEMFUNC_P (type1)
3637 && same_type_p (type0, type1))
3639 /* E will be the final comparison. */
3640 tree e;
3641 /* E1 and E2 are for scratch. */
3642 tree e1;
3643 tree e2;
3644 tree pfn0;
3645 tree pfn1;
3646 tree delta0;
3647 tree delta1;
3649 if (TREE_SIDE_EFFECTS (op0))
3650 op0 = save_expr (op0);
3651 if (TREE_SIDE_EFFECTS (op1))
3652 op1 = save_expr (op1);
3654 /* We generate:
3656 (op0.pfn == op1.pfn
3657 && (!op0.pfn || op0.delta == op1.delta))
3659 The reason for the `!op0.pfn' bit is that a NULL
3660 pointer-to-member is any member with a zero PFN; the
3661 DELTA field is unspecified. */
3662 pfn0 = pfn_from_ptrmemfunc (op0);
3663 pfn1 = pfn_from_ptrmemfunc (op1);
3664 delta0 = build_component_ref (op0, delta_identifier,
3665 NULL_TREE, 0);
3666 delta1 = build_component_ref (op1, delta_identifier,
3667 NULL_TREE, 0);
3668 e1 = cp_build_binary_op (EQ_EXPR, delta0, delta1);
3669 e2 = cp_build_binary_op (EQ_EXPR,
3670 pfn0,
3671 cp_convert (TREE_TYPE (pfn0),
3672 integer_zero_node));
3673 e1 = cp_build_binary_op (TRUTH_ORIF_EXPR, e1, e2);
3674 e2 = build (EQ_EXPR, boolean_type_node, pfn0, pfn1);
3675 e = cp_build_binary_op (TRUTH_ANDIF_EXPR, e2, e1);
3676 if (code == EQ_EXPR)
3677 return e;
3678 return cp_build_binary_op (EQ_EXPR, e, integer_zero_node);
3680 else if ((TYPE_PTRMEMFUNC_P (type0)
3681 && same_type_p (TYPE_PTRMEMFUNC_FN_TYPE (type0), type1))
3682 || (TYPE_PTRMEMFUNC_P (type1)
3683 && same_type_p (TYPE_PTRMEMFUNC_FN_TYPE (type1), type0)))
3684 my_friendly_abort (20000221);
3685 break;
3687 case MAX_EXPR:
3688 case MIN_EXPR:
3689 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE)
3690 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE))
3691 shorten = 1;
3692 else if (code0 == POINTER_TYPE && code1 == POINTER_TYPE)
3693 result_type = composite_pointer_type (type0, type1, op0, op1,
3694 "comparison");
3695 break;
3697 case LE_EXPR:
3698 case GE_EXPR:
3699 case LT_EXPR:
3700 case GT_EXPR:
3701 build_type = boolean_type_node;
3702 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE)
3703 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE))
3704 short_compare = 1;
3705 else if (code0 == POINTER_TYPE && code1 == POINTER_TYPE)
3706 result_type = composite_pointer_type (type0, type1, op0, op1,
3707 "comparison");
3708 else if (code0 == POINTER_TYPE && TREE_CODE (op1) == INTEGER_CST
3709 && integer_zerop (op1))
3710 result_type = type0;
3711 else if (code1 == POINTER_TYPE && TREE_CODE (op0) == INTEGER_CST
3712 && integer_zerop (op0))
3713 result_type = type1;
3714 else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
3716 result_type = type0;
3717 pedwarn ("ISO C++ forbids comparison between pointer and integer");
3719 else if (code0 == INTEGER_TYPE && code1 == POINTER_TYPE)
3721 result_type = type1;
3722 pedwarn ("ISO C++ forbids comparison between pointer and integer");
3724 break;
3726 case UNORDERED_EXPR:
3727 case ORDERED_EXPR:
3728 case UNLT_EXPR:
3729 case UNLE_EXPR:
3730 case UNGT_EXPR:
3731 case UNGE_EXPR:
3732 case UNEQ_EXPR:
3733 build_type = integer_type_node;
3734 if (code0 != REAL_TYPE || code1 != REAL_TYPE)
3736 error ("unordered comparison on non-floating point argument");
3737 return error_mark_node;
3739 common = 1;
3740 break;
3742 default:
3743 break;
3746 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE || code0 == COMPLEX_TYPE)
3748 (code1 == INTEGER_TYPE || code1 == REAL_TYPE || code1 == COMPLEX_TYPE))
3750 int none_complex = (code0 != COMPLEX_TYPE && code1 != COMPLEX_TYPE);
3752 if (shorten || common || short_compare)
3753 result_type = common_type (type0, type1);
3755 /* For certain operations (which identify themselves by shorten != 0)
3756 if both args were extended from the same smaller type,
3757 do the arithmetic in that type and then extend.
3759 shorten !=0 and !=1 indicates a bitwise operation.
3760 For them, this optimization is safe only if
3761 both args are zero-extended or both are sign-extended.
3762 Otherwise, we might change the result.
3763 Eg, (short)-1 | (unsigned short)-1 is (int)-1
3764 but calculated in (unsigned short) it would be (unsigned short)-1. */
3766 if (shorten && none_complex)
3768 int unsigned0, unsigned1;
3769 tree arg0 = get_narrower (op0, &unsigned0);
3770 tree arg1 = get_narrower (op1, &unsigned1);
3771 /* UNS is 1 if the operation to be done is an unsigned one. */
3772 int uns = TREE_UNSIGNED (result_type);
3773 tree type;
3775 final_type = result_type;
3777 /* Handle the case that OP0 does not *contain* a conversion
3778 but it *requires* conversion to FINAL_TYPE. */
3780 if (op0 == arg0 && TREE_TYPE (op0) != final_type)
3781 unsigned0 = TREE_UNSIGNED (TREE_TYPE (op0));
3782 if (op1 == arg1 && TREE_TYPE (op1) != final_type)
3783 unsigned1 = TREE_UNSIGNED (TREE_TYPE (op1));
3785 /* Now UNSIGNED0 is 1 if ARG0 zero-extends to FINAL_TYPE. */
3787 /* For bitwise operations, signedness of nominal type
3788 does not matter. Consider only how operands were extended. */
3789 if (shorten == -1)
3790 uns = unsigned0;
3792 /* Note that in all three cases below we refrain from optimizing
3793 an unsigned operation on sign-extended args.
3794 That would not be valid. */
3796 /* Both args variable: if both extended in same way
3797 from same width, do it in that width.
3798 Do it unsigned if args were zero-extended. */
3799 if ((TYPE_PRECISION (TREE_TYPE (arg0))
3800 < TYPE_PRECISION (result_type))
3801 && (TYPE_PRECISION (TREE_TYPE (arg1))
3802 == TYPE_PRECISION (TREE_TYPE (arg0)))
3803 && unsigned0 == unsigned1
3804 && (unsigned0 || !uns))
3805 result_type
3806 = signed_or_unsigned_type (unsigned0,
3807 common_type (TREE_TYPE (arg0),
3808 TREE_TYPE (arg1)));
3809 else if (TREE_CODE (arg0) == INTEGER_CST
3810 && (unsigned1 || !uns)
3811 && (TYPE_PRECISION (TREE_TYPE (arg1))
3812 < TYPE_PRECISION (result_type))
3813 && (type = signed_or_unsigned_type (unsigned1,
3814 TREE_TYPE (arg1)),
3815 int_fits_type_p (arg0, type)))
3816 result_type = type;
3817 else if (TREE_CODE (arg1) == INTEGER_CST
3818 && (unsigned0 || !uns)
3819 && (TYPE_PRECISION (TREE_TYPE (arg0))
3820 < TYPE_PRECISION (result_type))
3821 && (type = signed_or_unsigned_type (unsigned0,
3822 TREE_TYPE (arg0)),
3823 int_fits_type_p (arg1, type)))
3824 result_type = type;
3827 /* Shifts can be shortened if shifting right. */
3829 if (short_shift)
3831 int unsigned_arg;
3832 tree arg0 = get_narrower (op0, &unsigned_arg);
3834 final_type = result_type;
3836 if (arg0 == op0 && final_type == TREE_TYPE (op0))
3837 unsigned_arg = TREE_UNSIGNED (TREE_TYPE (op0));
3839 if (TYPE_PRECISION (TREE_TYPE (arg0)) < TYPE_PRECISION (result_type)
3840 /* We can shorten only if the shift count is less than the
3841 number of bits in the smaller type size. */
3842 && compare_tree_int (op1, TYPE_PRECISION (TREE_TYPE (arg0))) < 0
3843 /* If arg is sign-extended and then unsigned-shifted,
3844 we can simulate this with a signed shift in arg's type
3845 only if the extended result is at least twice as wide
3846 as the arg. Otherwise, the shift could use up all the
3847 ones made by sign-extension and bring in zeros.
3848 We can't optimize that case at all, but in most machines
3849 it never happens because available widths are 2**N. */
3850 && (!TREE_UNSIGNED (final_type)
3851 || unsigned_arg
3852 || (((unsigned) 2 * TYPE_PRECISION (TREE_TYPE (arg0)))
3853 <= TYPE_PRECISION (result_type))))
3855 /* Do an unsigned shift if the operand was zero-extended. */
3856 result_type
3857 = signed_or_unsigned_type (unsigned_arg,
3858 TREE_TYPE (arg0));
3859 /* Convert value-to-be-shifted to that type. */
3860 if (TREE_TYPE (op0) != result_type)
3861 op0 = cp_convert (result_type, op0);
3862 converted = 1;
3866 /* Comparison operations are shortened too but differently.
3867 They identify themselves by setting short_compare = 1. */
3869 if (short_compare)
3871 /* Don't write &op0, etc., because that would prevent op0
3872 from being kept in a register.
3873 Instead, make copies of the our local variables and
3874 pass the copies by reference, then copy them back afterward. */
3875 tree xop0 = op0, xop1 = op1, xresult_type = result_type;
3876 enum tree_code xresultcode = resultcode;
3877 tree val
3878 = shorten_compare (&xop0, &xop1, &xresult_type, &xresultcode);
3879 if (val != 0)
3880 return cp_convert (boolean_type_node, val);
3881 op0 = xop0, op1 = xop1;
3882 converted = 1;
3883 resultcode = xresultcode;
3886 if ((short_compare || code == MIN_EXPR || code == MAX_EXPR)
3887 && warn_sign_compare)
3889 int op0_signed = ! TREE_UNSIGNED (TREE_TYPE (orig_op0));
3890 int op1_signed = ! TREE_UNSIGNED (TREE_TYPE (orig_op1));
3892 int unsignedp0, unsignedp1;
3893 tree primop0 = get_narrower (op0, &unsignedp0);
3894 tree primop1 = get_narrower (op1, &unsignedp1);
3896 /* Check for comparison of different enum types. */
3897 if (TREE_CODE (TREE_TYPE (orig_op0)) == ENUMERAL_TYPE
3898 && TREE_CODE (TREE_TYPE (orig_op1)) == ENUMERAL_TYPE
3899 && TYPE_MAIN_VARIANT (TREE_TYPE (orig_op0))
3900 != TYPE_MAIN_VARIANT (TREE_TYPE (orig_op1)))
3902 cp_warning ("comparison between types `%#T' and `%#T'",
3903 TREE_TYPE (orig_op0), TREE_TYPE (orig_op1));
3906 /* Give warnings for comparisons between signed and unsigned
3907 quantities that may fail. */
3908 /* Do the checking based on the original operand trees, so that
3909 casts will be considered, but default promotions won't be. */
3911 /* Do not warn if the comparison is being done in a signed type,
3912 since the signed type will only be chosen if it can represent
3913 all the values of the unsigned type. */
3914 if (! TREE_UNSIGNED (result_type))
3915 /* OK */;
3916 /* Do not warn if both operands are unsigned. */
3917 else if (op0_signed == op1_signed)
3918 /* OK */;
3919 /* Do not warn if the signed quantity is an unsuffixed
3920 integer literal (or some static constant expression
3921 involving such literals or a conditional expression
3922 involving such literals) and it is non-negative. */
3923 else if ((op0_signed && tree_expr_nonnegative_p (orig_op0))
3924 || (op1_signed && tree_expr_nonnegative_p (orig_op1)))
3925 /* OK */;
3926 /* Do not warn if the comparison is an equality operation,
3927 the unsigned quantity is an integral constant and it does
3928 not use the most significant bit of result_type. */
3929 else if ((resultcode == EQ_EXPR || resultcode == NE_EXPR)
3930 && ((op0_signed && TREE_CODE (orig_op1) == INTEGER_CST
3931 && int_fits_type_p (orig_op1,
3932 signed_type (result_type)))
3933 || (op1_signed && TREE_CODE (orig_op0) == INTEGER_CST
3934 && int_fits_type_p (orig_op0,
3935 signed_type (result_type)))))
3936 /* OK */;
3937 else
3938 warning ("comparison between signed and unsigned integer expressions");
3940 /* Warn if two unsigned values are being compared in a size
3941 larger than their original size, and one (and only one) is the
3942 result of a `~' operator. This comparison will always fail.
3944 Also warn if one operand is a constant, and the constant does not
3945 have all bits set that are set in the ~ operand when it is
3946 extended. */
3948 if ((TREE_CODE (primop0) == BIT_NOT_EXPR)
3949 ^ (TREE_CODE (primop1) == BIT_NOT_EXPR))
3951 if (TREE_CODE (primop0) == BIT_NOT_EXPR)
3952 primop0 = get_narrower (TREE_OPERAND (op0, 0), &unsignedp0);
3953 if (TREE_CODE (primop1) == BIT_NOT_EXPR)
3954 primop1 = get_narrower (TREE_OPERAND (op1, 0), &unsignedp1);
3956 if (host_integerp (primop0, 0) || host_integerp (primop1, 0))
3958 tree primop;
3959 HOST_WIDE_INT constant, mask;
3960 int unsignedp;
3961 unsigned int bits;
3963 if (host_integerp (primop0, 0))
3965 primop = primop1;
3966 unsignedp = unsignedp1;
3967 constant = tree_low_cst (primop0, 0);
3969 else
3971 primop = primop0;
3972 unsignedp = unsignedp0;
3973 constant = tree_low_cst (primop1, 0);
3976 bits = TYPE_PRECISION (TREE_TYPE (primop));
3977 if (bits < TYPE_PRECISION (result_type)
3978 && bits < HOST_BITS_PER_LONG && unsignedp)
3980 mask = (~ (HOST_WIDE_INT) 0) << bits;
3981 if ((mask & constant) != mask)
3982 warning ("comparison of promoted ~unsigned with constant");
3985 else if (unsignedp0 && unsignedp1
3986 && (TYPE_PRECISION (TREE_TYPE (primop0))
3987 < TYPE_PRECISION (result_type))
3988 && (TYPE_PRECISION (TREE_TYPE (primop1))
3989 < TYPE_PRECISION (result_type)))
3990 warning ("comparison of promoted ~unsigned with unsigned");
3995 /* At this point, RESULT_TYPE must be nonzero to avoid an error message.
3996 If CONVERTED is zero, both args will be converted to type RESULT_TYPE.
3997 Then the expression will be built.
3998 It will be given type FINAL_TYPE if that is nonzero;
3999 otherwise, it will be given type RESULT_TYPE. */
4001 if (!result_type)
4003 cp_error ("invalid operands of types `%T' and `%T' to binary `%O'",
4004 TREE_TYPE (orig_op0), TREE_TYPE (orig_op1), code);
4005 return error_mark_node;
4008 /* Issue warnings about peculiar, but legal, uses of NULL. */
4009 if (/* It's reasonable to use pointer values as operands of &&
4010 and ||, so NULL is no exception. */
4011 !(code == TRUTH_ANDIF_EXPR || code == TRUTH_ORIF_EXPR)
4012 && (/* If OP0 is NULL and OP1 is not a pointer, or vice versa. */
4013 (orig_op0 == null_node
4014 && TREE_CODE (TREE_TYPE (op1)) != POINTER_TYPE)
4015 /* Or vice versa. */
4016 || (orig_op1 == null_node
4017 && TREE_CODE (TREE_TYPE (op0)) != POINTER_TYPE)
4018 /* Or, both are NULL and the operation was not a comparison. */
4019 || (orig_op0 == null_node && orig_op1 == null_node
4020 && code != EQ_EXPR && code != NE_EXPR)))
4021 /* Some sort of arithmetic operation involving NULL was
4022 performed. Note that pointer-difference and pointer-addition
4023 have already been handled above, and so we don't end up here in
4024 that case. */
4025 cp_warning ("NULL used in arithmetic");
4027 if (! converted)
4029 if (TREE_TYPE (op0) != result_type)
4030 op0 = cp_convert (result_type, op0);
4031 if (TREE_TYPE (op1) != result_type)
4032 op1 = cp_convert (result_type, op1);
4034 if (op0 == error_mark_node || op1 == error_mark_node)
4035 return error_mark_node;
4038 if (build_type == NULL_TREE)
4039 build_type = result_type;
4042 register tree result = build (resultcode, build_type, op0, op1);
4043 register tree folded;
4045 folded = fold (result);
4046 if (folded == result)
4047 TREE_CONSTANT (folded) = TREE_CONSTANT (op0) & TREE_CONSTANT (op1);
4048 if (final_type != 0)
4049 return cp_convert (final_type, folded);
4050 return folded;
4054 /* Return a tree for the sum or difference (RESULTCODE says which)
4055 of pointer PTROP and integer INTOP. */
4057 static tree
4058 pointer_int_sum (resultcode, ptrop, intop)
4059 enum tree_code resultcode;
4060 register tree ptrop, intop;
4062 tree size_exp;
4064 register tree result;
4065 register tree folded = fold (intop);
4067 /* The result is a pointer of the same type that is being added. */
4069 register tree result_type = TREE_TYPE (ptrop);
4071 if (!complete_type_or_else (result_type, ptrop))
4072 return error_mark_node;
4074 if (TREE_CODE (TREE_TYPE (result_type)) == VOID_TYPE)
4076 if (pedantic || warn_pointer_arith)
4077 pedwarn ("ISO C++ forbids using pointer of type `void *' in pointer arithmetic");
4078 size_exp = integer_one_node;
4080 else if (TREE_CODE (TREE_TYPE (result_type)) == FUNCTION_TYPE)
4082 if (pedantic || warn_pointer_arith)
4083 pedwarn ("ISO C++ forbids using a pointer-to-function in pointer arithmetic");
4084 size_exp = integer_one_node;
4086 else if (TREE_CODE (TREE_TYPE (result_type)) == METHOD_TYPE)
4088 if (pedantic || warn_pointer_arith)
4089 pedwarn ("ISO C++ forbids using a pointer to member function in pointer arithmetic");
4090 size_exp = integer_one_node;
4092 else if (TREE_CODE (TREE_TYPE (result_type)) == OFFSET_TYPE)
4094 if (pedantic || warn_pointer_arith)
4095 pedwarn ("ISO C++ forbids using pointer to a member in pointer arithmetic");
4096 size_exp = integer_one_node;
4098 else
4099 size_exp = size_in_bytes (complete_type (TREE_TYPE (result_type)));
4101 /* Needed to make OOPS V2R3 work. */
4102 intop = folded;
4103 if (integer_zerop (intop))
4104 return ptrop;
4106 /* If what we are about to multiply by the size of the elements
4107 contains a constant term, apply distributive law
4108 and multiply that constant term separately.
4109 This helps produce common subexpressions. */
4111 if ((TREE_CODE (intop) == PLUS_EXPR || TREE_CODE (intop) == MINUS_EXPR)
4112 && ! TREE_CONSTANT (intop)
4113 && TREE_CONSTANT (TREE_OPERAND (intop, 1))
4114 && TREE_CONSTANT (size_exp))
4116 enum tree_code subcode = resultcode;
4117 if (TREE_CODE (intop) == MINUS_EXPR)
4118 subcode = (subcode == PLUS_EXPR ? MINUS_EXPR : PLUS_EXPR);
4119 ptrop = cp_build_binary_op (subcode, ptrop, TREE_OPERAND (intop, 1));
4120 intop = TREE_OPERAND (intop, 0);
4123 /* Convert the integer argument to a type the same size as sizetype
4124 so the multiply won't overflow spuriously. */
4126 if (TYPE_PRECISION (TREE_TYPE (intop)) != TYPE_PRECISION (sizetype))
4127 intop = cp_convert (type_for_size (TYPE_PRECISION (sizetype), 0), intop);
4129 /* Replace the integer argument with a suitable product by the object size.
4130 Do this multiplication as signed, then convert to the appropriate
4131 pointer type (actually unsigned integral). */
4133 intop = cp_convert (result_type,
4134 cp_build_binary_op (MULT_EXPR, intop,
4135 cp_convert (TREE_TYPE (intop),
4136 size_exp)));
4138 /* Create the sum or difference. */
4140 result = build (resultcode, result_type, ptrop, intop);
4142 folded = fold (result);
4143 if (folded == result)
4144 TREE_CONSTANT (folded) = TREE_CONSTANT (ptrop) & TREE_CONSTANT (intop);
4145 return folded;
4148 /* Return a tree for the difference of pointers OP0 and OP1.
4149 The resulting tree has type int. */
4151 static tree
4152 pointer_diff (op0, op1, ptrtype)
4153 register tree op0, op1;
4154 register tree ptrtype;
4156 register tree result, folded;
4157 tree restype = ptrdiff_type_node;
4158 tree target_type = TREE_TYPE (ptrtype);
4160 if (!complete_type_or_else (target_type, NULL_TREE))
4161 return error_mark_node;
4163 if (pedantic || warn_pointer_arith)
4165 if (TREE_CODE (target_type) == VOID_TYPE)
4166 pedwarn ("ISO C++ forbids using pointer of type `void *' in subtraction");
4167 if (TREE_CODE (target_type) == FUNCTION_TYPE)
4168 pedwarn ("ISO C++ forbids using pointer to a function in subtraction");
4169 if (TREE_CODE (target_type) == METHOD_TYPE)
4170 pedwarn ("ISO C++ forbids using pointer to a method in subtraction");
4171 if (TREE_CODE (target_type) == OFFSET_TYPE)
4172 pedwarn ("ISO C++ forbids using pointer to a member in subtraction");
4175 /* First do the subtraction as integers;
4176 then drop through to build the divide operator. */
4178 op0 = cp_build_binary_op (MINUS_EXPR,
4179 cp_convert (restype, op0),
4180 cp_convert (restype, op1));
4182 /* This generates an error if op1 is a pointer to an incomplete type. */
4183 if (!COMPLETE_TYPE_P (TREE_TYPE (TREE_TYPE (op1))))
4184 error ("invalid use of a pointer to an incomplete type in pointer arithmetic");
4186 op1 = ((TREE_CODE (target_type) == VOID_TYPE
4187 || TREE_CODE (target_type) == FUNCTION_TYPE
4188 || TREE_CODE (target_type) == METHOD_TYPE
4189 || TREE_CODE (target_type) == OFFSET_TYPE)
4190 ? integer_one_node
4191 : size_in_bytes (target_type));
4193 /* Do the division. */
4195 result = build (EXACT_DIV_EXPR, restype, op0, cp_convert (restype, op1));
4197 folded = fold (result);
4198 if (folded == result)
4199 TREE_CONSTANT (folded) = TREE_CONSTANT (op0) & TREE_CONSTANT (op1);
4200 return folded;
4203 /* Handle the case of taking the address of a COMPONENT_REF.
4204 Called by `build_unary_op'.
4206 ARG is the COMPONENT_REF whose address we want.
4207 ARGTYPE is the pointer type that this address should have. */
4209 static tree
4210 build_component_addr (arg, argtype)
4211 tree arg, argtype;
4213 tree field = TREE_OPERAND (arg, 1);
4214 tree basetype = decl_type_context (field);
4215 tree rval = build_unary_op (ADDR_EXPR, TREE_OPERAND (arg, 0), 0);
4217 my_friendly_assert (TREE_CODE (field) == FIELD_DECL, 981018);
4219 if (DECL_C_BIT_FIELD (field))
4221 cp_error ("attempt to take address of bit-field structure member `%D'",
4222 field);
4223 return error_mark_node;
4226 if (TREE_CODE (field) == FIELD_DECL
4227 && TYPE_BASE_CONVS_MAY_REQUIRE_CODE_P (basetype))
4229 /* Can't convert directly to ARGTYPE, since that
4230 may have the same pointer type as one of our
4231 baseclasses. */
4232 rval = build1 (NOP_EXPR, argtype,
4233 convert_pointer_to (basetype, rval));
4234 TREE_CONSTANT (rval) = TREE_CONSTANT (TREE_OPERAND (rval, 0));
4236 else
4237 /* This conversion is harmless. */
4238 rval = convert_force (argtype, rval, 0);
4240 return fold (build (PLUS_EXPR, argtype, rval,
4241 cp_convert (argtype, byte_position (field))));
4244 /* Construct and perhaps optimize a tree representation
4245 for a unary operation. CODE, a tree_code, specifies the operation
4246 and XARG is the operand. */
4248 tree
4249 build_x_unary_op (code, xarg)
4250 enum tree_code code;
4251 tree xarg;
4253 tree exp;
4254 int ptrmem = 0;
4256 if (processing_template_decl)
4257 return build_min_nt (code, xarg, NULL_TREE);
4259 /* & rec, on incomplete RECORD_TYPEs is the simple opr &, not an
4260 error message. */
4261 if (code == ADDR_EXPR
4262 && TREE_CODE (xarg) != TEMPLATE_ID_EXPR
4263 && ((IS_AGGR_TYPE_CODE (TREE_CODE (TREE_TYPE (xarg)))
4264 && !COMPLETE_TYPE_P (TREE_TYPE (xarg)))
4265 || (TREE_CODE (xarg) == OFFSET_REF)))
4266 /* don't look for a function */;
4267 else
4269 tree rval;
4271 rval = build_new_op (code, LOOKUP_NORMAL, xarg,
4272 NULL_TREE, NULL_TREE);
4273 if (rval || code != ADDR_EXPR)
4274 return rval;
4276 if (code == ADDR_EXPR)
4278 if (TREE_CODE (xarg) == OFFSET_REF)
4280 ptrmem = PTRMEM_OK_P (xarg);
4282 if (!ptrmem && !flag_ms_extensions
4283 && TREE_CODE (TREE_TYPE (TREE_OPERAND (xarg, 1))) == METHOD_TYPE)
4284 /* A single non-static member, make sure we don't allow a
4285 pointer-to-member. */
4286 xarg = ovl_cons (TREE_OPERAND (xarg, 1), NULL_TREE);
4288 else if (TREE_CODE (xarg) == TARGET_EXPR)
4289 warning ("taking address of temporary");
4291 exp = build_unary_op (code, xarg, 0);
4292 if (TREE_CODE (exp) == ADDR_EXPR)
4293 PTRMEM_OK_P (exp) = ptrmem;
4295 return exp;
4298 /* Like truthvalue_conversion, but handle pointer-to-member constants, where
4299 a null value is represented by an INTEGER_CST of -1. */
4301 tree
4302 cp_truthvalue_conversion (expr)
4303 tree expr;
4305 tree type = TREE_TYPE (expr);
4306 if (TYPE_PTRMEM_P (type))
4307 return build_binary_op (NE_EXPR, expr, integer_zero_node, 1);
4308 else
4309 return truthvalue_conversion (expr);
4312 /* Just like cp_truthvalue_conversion, but we want a CLEANUP_POINT_EXPR. */
4314 tree
4315 condition_conversion (expr)
4316 tree expr;
4318 tree t;
4319 if (processing_template_decl)
4320 return expr;
4321 if (TREE_CODE (expr) == OFFSET_REF)
4322 expr = resolve_offset_ref (expr);
4323 t = perform_implicit_conversion (boolean_type_node, expr);
4324 t = fold (build1 (CLEANUP_POINT_EXPR, boolean_type_node, t));
4325 return t;
4328 /* C++: Must handle pointers to members.
4330 Perhaps type instantiation should be extended to handle conversion
4331 from aggregates to types we don't yet know we want? (Or are those
4332 cases typically errors which should be reported?)
4334 NOCONVERT nonzero suppresses the default promotions
4335 (such as from short to int). */
4337 tree
4338 build_unary_op (code, xarg, noconvert)
4339 enum tree_code code;
4340 tree xarg;
4341 int noconvert;
4343 /* No default_conversion here. It causes trouble for ADDR_EXPR. */
4344 register tree arg = xarg;
4345 register tree argtype = 0;
4346 const char *errstring = NULL;
4347 tree val;
4349 if (arg == error_mark_node)
4350 return error_mark_node;
4352 switch (code)
4354 case CONVERT_EXPR:
4355 /* This is used for unary plus, because a CONVERT_EXPR
4356 is enough to prevent anybody from looking inside for
4357 associativity, but won't generate any code. */
4358 if (!(arg = build_expr_type_conversion
4359 (WANT_ARITH | WANT_ENUM | WANT_POINTER, arg, 1)))
4360 errstring = "wrong type argument to unary plus";
4361 else
4363 if (!noconvert)
4364 arg = default_conversion (arg);
4365 arg = build1 (NON_LVALUE_EXPR, TREE_TYPE (arg), arg);
4366 TREE_CONSTANT (arg) = TREE_CONSTANT (TREE_OPERAND (arg, 0));
4368 break;
4370 case NEGATE_EXPR:
4371 if (!(arg = build_expr_type_conversion (WANT_ARITH | WANT_ENUM, arg, 1)))
4372 errstring = "wrong type argument to unary minus";
4373 else if (!noconvert)
4374 arg = default_conversion (arg);
4375 break;
4377 case BIT_NOT_EXPR:
4378 if (TREE_CODE (TREE_TYPE (arg)) == COMPLEX_TYPE)
4380 code = CONJ_EXPR;
4381 if (!noconvert)
4382 arg = default_conversion (arg);
4384 else if (!(arg = build_expr_type_conversion (WANT_INT | WANT_ENUM,
4385 arg, 1)))
4386 errstring = "wrong type argument to bit-complement";
4387 else if (!noconvert)
4388 arg = default_conversion (arg);
4389 break;
4391 case ABS_EXPR:
4392 if (!(arg = build_expr_type_conversion (WANT_ARITH | WANT_ENUM, arg, 1)))
4393 errstring = "wrong type argument to abs";
4394 else if (!noconvert)
4395 arg = default_conversion (arg);
4396 break;
4398 case CONJ_EXPR:
4399 /* Conjugating a real value is a no-op, but allow it anyway. */
4400 if (!(arg = build_expr_type_conversion (WANT_ARITH | WANT_ENUM, arg, 1)))
4401 errstring = "wrong type argument to conjugation";
4402 else if (!noconvert)
4403 arg = default_conversion (arg);
4404 break;
4406 case TRUTH_NOT_EXPR:
4407 arg = cp_convert (boolean_type_node, arg);
4408 val = invert_truthvalue (arg);
4409 if (arg != error_mark_node)
4410 return val;
4411 errstring = "in argument to unary !";
4412 break;
4414 case NOP_EXPR:
4415 break;
4417 case REALPART_EXPR:
4418 if (TREE_CODE (arg) == COMPLEX_CST)
4419 return TREE_REALPART (arg);
4420 else if (TREE_CODE (TREE_TYPE (arg)) == COMPLEX_TYPE)
4421 return fold (build1 (REALPART_EXPR, TREE_TYPE (TREE_TYPE (arg)), arg));
4422 else
4423 return arg;
4425 case IMAGPART_EXPR:
4426 if (TREE_CODE (arg) == COMPLEX_CST)
4427 return TREE_IMAGPART (arg);
4428 else if (TREE_CODE (TREE_TYPE (arg)) == COMPLEX_TYPE)
4429 return fold (build1 (IMAGPART_EXPR, TREE_TYPE (TREE_TYPE (arg)), arg));
4430 else
4431 return cp_convert (TREE_TYPE (arg), integer_zero_node);
4433 case PREINCREMENT_EXPR:
4434 case POSTINCREMENT_EXPR:
4435 case PREDECREMENT_EXPR:
4436 case POSTDECREMENT_EXPR:
4437 /* Handle complex lvalues (when permitted)
4438 by reduction to simpler cases. */
4440 val = unary_complex_lvalue (code, arg);
4441 if (val != 0)
4442 return val;
4444 /* Increment or decrement the real part of the value,
4445 and don't change the imaginary part. */
4446 if (TREE_CODE (TREE_TYPE (arg)) == COMPLEX_TYPE)
4448 tree real, imag;
4450 arg = stabilize_reference (arg);
4451 real = build_unary_op (REALPART_EXPR, arg, 1);
4452 imag = build_unary_op (IMAGPART_EXPR, arg, 1);
4453 return build (COMPLEX_EXPR, TREE_TYPE (arg),
4454 build_unary_op (code, real, 1), imag);
4457 /* Report invalid types. */
4459 if (!(arg = build_expr_type_conversion (WANT_ARITH | WANT_POINTER,
4460 arg, 1)))
4462 if (code == PREINCREMENT_EXPR)
4463 errstring ="no pre-increment operator for type";
4464 else if (code == POSTINCREMENT_EXPR)
4465 errstring ="no post-increment operator for type";
4466 else if (code == PREDECREMENT_EXPR)
4467 errstring ="no pre-decrement operator for type";
4468 else
4469 errstring ="no post-decrement operator for type";
4470 break;
4473 /* Report something read-only. */
4475 if (CP_TYPE_CONST_P (TREE_TYPE (arg))
4476 || TREE_READONLY (arg))
4477 readonly_error (arg, ((code == PREINCREMENT_EXPR
4478 || code == POSTINCREMENT_EXPR)
4479 ? "increment" : "decrement"),
4483 register tree inc;
4484 tree result_type = TREE_TYPE (arg);
4486 arg = get_unwidened (arg, 0);
4487 argtype = TREE_TYPE (arg);
4489 /* ARM $5.2.5 last annotation says this should be forbidden. */
4490 if (TREE_CODE (argtype) == ENUMERAL_TYPE)
4491 pedwarn ("ISO C++ forbids %sing an enum",
4492 (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
4493 ? "increment" : "decrement");
4495 /* Compute the increment. */
4497 if (TREE_CODE (argtype) == POINTER_TYPE)
4499 enum tree_code tmp = TREE_CODE (TREE_TYPE (argtype));
4500 tree type = complete_type (TREE_TYPE (argtype));
4502 if (!COMPLETE_OR_VOID_TYPE_P (type))
4503 cp_error ("cannot %s a pointer to incomplete type `%T'",
4504 ((code == PREINCREMENT_EXPR
4505 || code == POSTINCREMENT_EXPR)
4506 ? "increment" : "decrement"), TREE_TYPE (argtype));
4507 else if ((pedantic || warn_pointer_arith)
4508 && (tmp == FUNCTION_TYPE || tmp == METHOD_TYPE
4509 || tmp == VOID_TYPE || tmp == OFFSET_TYPE))
4510 cp_pedwarn ("ISO C++ forbids %sing a pointer of type `%T'",
4511 ((code == PREINCREMENT_EXPR
4512 || code == POSTINCREMENT_EXPR)
4513 ? "increment" : "decrement"), argtype);
4514 inc = c_sizeof_nowarn (TREE_TYPE (argtype));
4516 else
4517 inc = integer_one_node;
4519 inc = cp_convert (argtype, inc);
4521 /* Handle incrementing a cast-expression. */
4523 switch (TREE_CODE (arg))
4525 case NOP_EXPR:
4526 case CONVERT_EXPR:
4527 case FLOAT_EXPR:
4528 case FIX_TRUNC_EXPR:
4529 case FIX_FLOOR_EXPR:
4530 case FIX_ROUND_EXPR:
4531 case FIX_CEIL_EXPR:
4533 tree incremented, modify, value, compound;
4534 if (! lvalue_p (arg) && pedantic)
4535 pedwarn ("cast to non-reference type used as lvalue");
4536 arg = stabilize_reference (arg);
4537 if (code == PREINCREMENT_EXPR || code == PREDECREMENT_EXPR)
4538 value = arg;
4539 else
4540 value = save_expr (arg);
4541 incremented = build (((code == PREINCREMENT_EXPR
4542 || code == POSTINCREMENT_EXPR)
4543 ? PLUS_EXPR : MINUS_EXPR),
4544 argtype, value, inc);
4546 modify = build_modify_expr (arg, NOP_EXPR, incremented);
4547 compound = build (COMPOUND_EXPR, TREE_TYPE (arg), modify, value);
4549 /* Eliminate warning about unused result of + or -. */
4550 TREE_NO_UNUSED_WARNING (compound) = 1;
4551 return compound;
4554 default:
4555 break;
4558 /* Complain about anything else that is not a true lvalue. */
4559 if (!lvalue_or_else (arg, ((code == PREINCREMENT_EXPR
4560 || code == POSTINCREMENT_EXPR)
4561 ? "increment" : "decrement")))
4562 return error_mark_node;
4564 /* Forbid using -- on `bool'. */
4565 if (TREE_TYPE (arg) == boolean_type_node)
4567 if (code == POSTDECREMENT_EXPR || code == PREDECREMENT_EXPR)
4569 cp_error ("invalid use of `--' on bool variable `%D'", arg);
4570 return error_mark_node;
4572 #if 0
4573 /* This will only work if someone can convince Kenner to accept
4574 my patch to expand_increment. (jason) */
4575 val = build (code, TREE_TYPE (arg), arg, inc);
4576 #else
4577 val = boolean_increment (code, arg);
4578 #endif
4580 else
4581 val = build (code, TREE_TYPE (arg), arg, inc);
4583 TREE_SIDE_EFFECTS (val) = 1;
4584 return cp_convert (result_type, val);
4587 case ADDR_EXPR:
4588 /* Note that this operation never does default_conversion
4589 regardless of NOCONVERT. */
4591 argtype = lvalue_type (arg);
4592 if (TREE_CODE (argtype) == REFERENCE_TYPE)
4594 arg = build1
4595 (CONVERT_EXPR,
4596 build_pointer_type (TREE_TYPE (argtype)), arg);
4597 TREE_CONSTANT (arg) = TREE_CONSTANT (TREE_OPERAND (arg, 0));
4598 return arg;
4600 else if (pedantic && DECL_MAIN_P (arg))
4601 /* ARM $3.4 */
4602 pedwarn ("ISO C++ forbids taking address of function `::main'");
4604 /* Let &* cancel out to simplify resulting code. */
4605 if (TREE_CODE (arg) == INDIRECT_REF)
4607 /* We don't need to have `current_class_ptr' wrapped in a
4608 NON_LVALUE_EXPR node. */
4609 if (arg == current_class_ref)
4610 return current_class_ptr;
4612 arg = TREE_OPERAND (arg, 0);
4613 if (TREE_CODE (TREE_TYPE (arg)) == REFERENCE_TYPE)
4615 arg = build1
4616 (CONVERT_EXPR,
4617 build_pointer_type (TREE_TYPE (TREE_TYPE (arg))), arg);
4618 TREE_CONSTANT (arg) = TREE_CONSTANT (TREE_OPERAND (arg, 0));
4620 else if (lvalue_p (arg))
4621 /* Don't let this be an lvalue. */
4622 return non_lvalue (arg);
4623 return arg;
4626 /* For &x[y], return x+y */
4627 if (TREE_CODE (arg) == ARRAY_REF)
4629 if (mark_addressable (TREE_OPERAND (arg, 0)) == 0)
4630 return error_mark_node;
4631 return cp_build_binary_op (PLUS_EXPR, TREE_OPERAND (arg, 0),
4632 TREE_OPERAND (arg, 1));
4635 /* Uninstantiated types are all functions. Taking the
4636 address of a function is a no-op, so just return the
4637 argument. */
4639 if (TREE_CODE (arg) == IDENTIFIER_NODE
4640 && IDENTIFIER_OPNAME_P (arg))
4642 my_friendly_abort (117);
4643 /* We don't know the type yet, so just work around the problem.
4644 We know that this will resolve to an lvalue. */
4645 return build1 (ADDR_EXPR, unknown_type_node, arg);
4648 if (TREE_CODE (arg) == COMPONENT_REF && type_unknown_p (arg)
4649 && OVL_NEXT (TREE_OPERAND (arg, 1)) == NULL_TREE)
4651 /* They're trying to take the address of a unique non-static
4652 member function. This is ill-formed (except in MS-land),
4653 but let's try to DTRT.
4654 Note: We only handle unique functions here because we don't
4655 want to complain if there's a static overload; non-unique
4656 cases will be handled by instantiate_type. But we need to
4657 handle this case here to allow casts on the resulting PMF.
4658 We could defer this in non-MS mode, but it's easier to give
4659 a useful error here. */
4661 tree base = TREE_TYPE (TREE_OPERAND (arg, 0));
4662 tree name = DECL_NAME (OVL_CURRENT (TREE_OPERAND (arg, 1)));
4664 if (! flag_ms_extensions)
4666 if (current_class_type
4667 && TREE_OPERAND (arg, 0) == current_class_ref)
4668 /* An expression like &memfn. */
4669 cp_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);
4670 else
4671 cp_pedwarn ("ISO C++ forbids taking the address of a bound member function to form a pointer to member function. Say `&%T::%D'", base, name);
4673 arg = build_offset_ref (base, name);
4676 if (type_unknown_p (arg))
4677 return build1 (ADDR_EXPR, unknown_type_node, arg);
4679 /* Handle complex lvalues (when permitted)
4680 by reduction to simpler cases. */
4681 val = unary_complex_lvalue (code, arg);
4682 if (val != 0)
4683 return val;
4685 switch (TREE_CODE (arg))
4687 case NOP_EXPR:
4688 case CONVERT_EXPR:
4689 case FLOAT_EXPR:
4690 case FIX_TRUNC_EXPR:
4691 case FIX_FLOOR_EXPR:
4692 case FIX_ROUND_EXPR:
4693 case FIX_CEIL_EXPR:
4694 if (! lvalue_p (arg) && pedantic)
4695 pedwarn ("ISO C++ forbids taking the address of a cast to a non-lvalue expression");
4696 break;
4698 default:
4699 break;
4702 /* Allow the address of a constructor if all the elements
4703 are constant. */
4704 if (TREE_CODE (arg) == CONSTRUCTOR && TREE_HAS_CONSTRUCTOR (arg)
4705 && TREE_CONSTANT (arg))
4707 /* Anything not already handled and not a true memory reference
4708 is an error. */
4709 else if (TREE_CODE (argtype) != FUNCTION_TYPE
4710 && TREE_CODE (argtype) != METHOD_TYPE
4711 && !lvalue_or_else (arg, "unary `&'"))
4712 return error_mark_node;
4714 if (argtype != error_mark_node)
4715 argtype = build_pointer_type (argtype);
4717 if (mark_addressable (arg) == 0)
4718 return error_mark_node;
4721 tree addr;
4723 if (TREE_CODE (arg) == COMPONENT_REF)
4724 addr = build_component_addr (arg, argtype);
4725 else
4726 addr = build1 (ADDR_EXPR, argtype, arg);
4728 /* Address of a static or external variable or
4729 function counts as a constant */
4730 if (staticp (arg))
4731 TREE_CONSTANT (addr) = 1;
4733 if (TREE_CODE (argtype) == POINTER_TYPE
4734 && TREE_CODE (TREE_TYPE (argtype)) == METHOD_TYPE)
4736 build_ptrmemfunc_type (argtype);
4737 addr = build_ptrmemfunc (argtype, addr, 0);
4740 return addr;
4743 default:
4744 break;
4747 if (!errstring)
4749 if (argtype == 0)
4750 argtype = TREE_TYPE (arg);
4751 return fold (build1 (code, argtype, arg));
4754 error ("%s", errstring);
4755 return error_mark_node;
4758 /* Apply unary lvalue-demanding operator CODE to the expression ARG
4759 for certain kinds of expressions which are not really lvalues
4760 but which we can accept as lvalues.
4762 If ARG is not a kind of expression we can handle, return zero. */
4764 tree
4765 unary_complex_lvalue (code, arg)
4766 enum tree_code code;
4767 tree arg;
4769 /* Handle (a, b) used as an "lvalue". */
4770 if (TREE_CODE (arg) == COMPOUND_EXPR)
4772 tree real_result = build_unary_op (code, TREE_OPERAND (arg, 1), 0);
4773 return build (COMPOUND_EXPR, TREE_TYPE (real_result),
4774 TREE_OPERAND (arg, 0), real_result);
4777 /* Handle (a ? b : c) used as an "lvalue". */
4778 if (TREE_CODE (arg) == COND_EXPR
4779 || TREE_CODE (arg) == MIN_EXPR || TREE_CODE (arg) == MAX_EXPR)
4780 return rationalize_conditional_expr (code, arg);
4782 /* Handle (a = b), (++a), and (--a) used as an "lvalue". */
4783 if (TREE_CODE (arg) == MODIFY_EXPR
4784 || TREE_CODE (arg) == PREINCREMENT_EXPR
4785 || TREE_CODE (arg) == PREDECREMENT_EXPR)
4787 tree lvalue = TREE_OPERAND (arg, 0);
4788 if (TREE_SIDE_EFFECTS (lvalue))
4790 lvalue = stabilize_reference (lvalue);
4791 arg = build (TREE_CODE (arg), TREE_TYPE (arg),
4792 lvalue, TREE_OPERAND (arg, 1));
4794 return unary_complex_lvalue
4795 (code, build (COMPOUND_EXPR, TREE_TYPE (lvalue), arg, lvalue));
4798 if (code != ADDR_EXPR)
4799 return 0;
4801 /* Handle (a = b) used as an "lvalue" for `&'. */
4802 if (TREE_CODE (arg) == MODIFY_EXPR
4803 || TREE_CODE (arg) == INIT_EXPR)
4805 tree real_result = build_unary_op (code, TREE_OPERAND (arg, 0), 0);
4806 arg = build (COMPOUND_EXPR, TREE_TYPE (real_result), arg, real_result);
4807 TREE_NO_UNUSED_WARNING (arg) = 1;
4808 return arg;
4811 if (TREE_CODE (TREE_TYPE (arg)) == FUNCTION_TYPE
4812 || TREE_CODE (TREE_TYPE (arg)) == METHOD_TYPE
4813 || TREE_CODE (TREE_TYPE (arg)) == OFFSET_TYPE)
4815 /* The representation of something of type OFFSET_TYPE
4816 is really the representation of a pointer to it.
4817 Here give the representation its true type. */
4818 tree t;
4820 my_friendly_assert (TREE_CODE (arg) != SCOPE_REF, 313);
4822 if (TREE_CODE (arg) != OFFSET_REF)
4823 return 0;
4825 t = TREE_OPERAND (arg, 1);
4827 /* Check all this code for right semantics. */
4828 if (TREE_CODE (t) == FUNCTION_DECL)
4830 if (DECL_DESTRUCTOR_P (t))
4831 cp_error ("taking address of destructor");
4832 return build_unary_op (ADDR_EXPR, t, 0);
4834 if (TREE_CODE (t) == VAR_DECL)
4835 return build_unary_op (ADDR_EXPR, t, 0);
4836 else
4838 tree type;
4840 if (TREE_OPERAND (arg, 0)
4841 && ! is_dummy_object (TREE_OPERAND (arg, 0))
4842 && TREE_CODE (t) != FIELD_DECL)
4844 cp_error ("taking address of bound pointer-to-member expression");
4845 return error_mark_node;
4848 type = build_offset_type (DECL_FIELD_CONTEXT (t), TREE_TYPE (t));
4849 type = build_pointer_type (type);
4851 t = make_ptrmem_cst (type, TREE_OPERAND (arg, 1));
4852 return t;
4857 /* We permit compiler to make function calls returning
4858 objects of aggregate type look like lvalues. */
4860 tree targ = arg;
4862 if (TREE_CODE (targ) == SAVE_EXPR)
4863 targ = TREE_OPERAND (targ, 0);
4865 if (TREE_CODE (targ) == CALL_EXPR && IS_AGGR_TYPE (TREE_TYPE (targ)))
4867 if (TREE_CODE (arg) == SAVE_EXPR)
4868 targ = arg;
4869 else
4870 targ = build_cplus_new (TREE_TYPE (arg), arg);
4871 return build1 (ADDR_EXPR, build_pointer_type (TREE_TYPE (arg)), targ);
4874 if (TREE_CODE (arg) == SAVE_EXPR && TREE_CODE (targ) == INDIRECT_REF)
4875 return build (SAVE_EXPR, build_pointer_type (TREE_TYPE (arg)),
4876 TREE_OPERAND (targ, 0), current_function_decl, NULL);
4879 /* Don't let anything else be handled specially. */
4880 return 0;
4883 /* Mark EXP saying that we need to be able to take the
4884 address of it; it should not be allocated in a register.
4885 Value is 1 if successful.
4887 C++: we do not allow `current_class_ptr' to be addressable. */
4890 mark_addressable (exp)
4891 tree exp;
4893 register tree x = exp;
4895 if (TREE_ADDRESSABLE (x) == 1)
4896 return 1;
4898 while (1)
4899 switch (TREE_CODE (x))
4901 case ADDR_EXPR:
4902 case COMPONENT_REF:
4903 case ARRAY_REF:
4904 case REALPART_EXPR:
4905 case IMAGPART_EXPR:
4906 x = TREE_OPERAND (x, 0);
4907 break;
4909 case PARM_DECL:
4910 if (x == current_class_ptr)
4912 error ("cannot take the address of `this', which is an rvalue expression");
4913 TREE_ADDRESSABLE (x) = 1; /* so compiler doesn't die later */
4914 return 1;
4916 case VAR_DECL:
4917 /* Caller should not be trying to mark initialized
4918 constant fields addressable. */
4919 my_friendly_assert (DECL_LANG_SPECIFIC (x) == 0
4920 || DECL_IN_AGGR_P (x) == 0
4921 || TREE_STATIC (x)
4922 || DECL_EXTERNAL (x), 314);
4924 case CONST_DECL:
4925 case RESULT_DECL:
4926 if (DECL_REGISTER (x) && !TREE_ADDRESSABLE (x)
4927 && !DECL_ARTIFICIAL (x) && extra_warnings)
4928 cp_warning ("address requested for `%D', which is declared `register'",
4930 TREE_ADDRESSABLE (x) = 1;
4931 return 1;
4933 case FUNCTION_DECL:
4934 TREE_ADDRESSABLE (x) = 1;
4935 TREE_ADDRESSABLE (DECL_ASSEMBLER_NAME (x)) = 1;
4936 return 1;
4938 case CONSTRUCTOR:
4939 TREE_ADDRESSABLE (x) = 1;
4940 return 1;
4942 case TARGET_EXPR:
4943 TREE_ADDRESSABLE (x) = 1;
4944 mark_addressable (TREE_OPERAND (x, 0));
4945 return 1;
4947 default:
4948 return 1;
4952 /* Build and return a conditional expression IFEXP ? OP1 : OP2. */
4954 tree
4955 build_x_conditional_expr (ifexp, op1, op2)
4956 tree ifexp, op1, op2;
4958 if (processing_template_decl)
4959 return build_min_nt (COND_EXPR, ifexp, op1, op2);
4961 return build_conditional_expr (ifexp, op1, op2);
4964 /* Handle overloading of the ',' operator when needed. Otherwise,
4965 this function just builds an expression list. */
4967 tree
4968 build_x_compound_expr (list)
4969 tree list;
4971 tree rest = TREE_CHAIN (list);
4972 tree result;
4974 if (processing_template_decl)
4975 return build_min_nt (COMPOUND_EXPR, list, NULL_TREE);
4977 if (rest == NULL_TREE)
4978 return build_compound_expr (list);
4980 result = build_opfncall (COMPOUND_EXPR, LOOKUP_NORMAL,
4981 TREE_VALUE (list), TREE_VALUE (rest), NULL_TREE);
4982 if (result)
4983 return build_x_compound_expr (tree_cons (NULL_TREE, result,
4984 TREE_CHAIN (rest)));
4986 if (! TREE_SIDE_EFFECTS (TREE_VALUE (list)))
4988 /* FIXME: This test should be in the implicit cast to void of the LHS. */
4989 /* the left-hand operand of a comma expression is like an expression
4990 statement: we should warn if it doesn't have any side-effects,
4991 unless it was explicitly cast to (void). */
4992 if ((extra_warnings || warn_unused_value)
4993 && !(TREE_CODE (TREE_VALUE(list)) == CONVERT_EXPR
4994 && VOID_TYPE_P (TREE_TYPE (TREE_VALUE(list)))))
4995 warning("left-hand operand of comma expression has no effect");
4997 #if 0 /* this requires a gcc backend patch to export warn_if_unused_value */
4998 else if (warn_unused_value)
4999 warn_if_unused_value (TREE_VALUE(list));
5000 #endif
5002 return build_compound_expr
5003 (tree_cons (NULL_TREE, TREE_VALUE (list),
5004 build_tree_list (NULL_TREE,
5005 build_x_compound_expr (rest))));
5008 /* Given a list of expressions, return a compound expression
5009 that performs them all and returns the value of the last of them. */
5011 tree
5012 build_compound_expr (list)
5013 tree list;
5015 register tree rest;
5016 tree first;
5018 TREE_VALUE (list) = decl_constant_value (TREE_VALUE (list));
5020 if (TREE_CHAIN (list) == 0)
5022 /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
5023 Strip such NOP_EXPRs, since LIST is used in non-lvalue context. */
5024 if (TREE_CODE (list) == NOP_EXPR
5025 && TREE_TYPE (list) == TREE_TYPE (TREE_OPERAND (list, 0)))
5026 list = TREE_OPERAND (list, 0);
5028 return TREE_VALUE (list);
5031 first = TREE_VALUE (list);
5032 first = convert_to_void (first, "left-hand operand of comma");
5033 if (first == error_mark_node)
5034 return error_mark_node;
5036 rest = build_compound_expr (TREE_CHAIN (list));
5037 if (rest == error_mark_node)
5038 return error_mark_node;
5040 /* When pedantic, a compound expression cannot be a constant expression. */
5041 if (! TREE_SIDE_EFFECTS (first) && ! pedantic)
5042 return rest;
5044 return build (COMPOUND_EXPR, TREE_TYPE (rest), first, rest);
5047 tree
5048 build_static_cast (type, expr)
5049 tree type, expr;
5051 tree intype;
5052 int ok;
5054 if (type == error_mark_node || expr == error_mark_node)
5055 return error_mark_node;
5057 if (TREE_CODE (expr) == OFFSET_REF)
5058 expr = resolve_offset_ref (expr);
5060 if (processing_template_decl)
5062 tree t = build_min (STATIC_CAST_EXPR, type, expr);
5063 return t;
5066 /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
5067 Strip such NOP_EXPRs if VALUE is being used in non-lvalue context. */
5068 if (TREE_CODE (type) != REFERENCE_TYPE
5069 && TREE_CODE (expr) == NOP_EXPR
5070 && TREE_TYPE (expr) == TREE_TYPE (TREE_OPERAND (expr, 0)))
5071 expr = TREE_OPERAND (expr, 0);
5073 if (TREE_CODE (type) == VOID_TYPE)
5075 expr = convert_to_void (expr, /*implicit=*/NULL);
5076 return expr;
5079 if (TREE_CODE (type) == REFERENCE_TYPE)
5080 return (convert_from_reference
5081 (convert_to_reference (type, expr, CONV_STATIC|CONV_IMPLICIT,
5082 LOOKUP_COMPLAIN, NULL_TREE)));
5084 if (IS_AGGR_TYPE (type))
5085 return build_cplus_new (type, (build_method_call
5086 (NULL_TREE, complete_ctor_identifier,
5087 build_tree_list (NULL_TREE, expr),
5088 TYPE_BINFO (type), LOOKUP_NORMAL)));
5090 intype = TREE_TYPE (expr);
5092 /* FIXME handle casting to array type. */
5094 ok = 0;
5095 if (IS_AGGR_TYPE (intype)
5096 ? can_convert_arg (type, intype, expr)
5097 : can_convert_arg (strip_all_pointer_quals (type),
5098 strip_all_pointer_quals (intype), expr))
5099 /* This is a standard conversion. */
5100 ok = 1;
5101 else if (TYPE_PTROB_P (type) && TYPE_PTROB_P (intype))
5103 /* They're pointers to objects. They must be aggregates that
5104 are related non-virtually. */
5106 tree binfo;
5108 if (IS_AGGR_TYPE (TREE_TYPE (type)) && IS_AGGR_TYPE (TREE_TYPE (intype))
5109 && (binfo = get_binfo (TREE_TYPE (intype), TREE_TYPE (type), 0))
5110 && !binfo_from_vbase (binfo))
5111 ok = 1;
5113 else if (TREE_CODE (intype) != BOOLEAN_TYPE
5114 && TREE_CODE (type) != ARRAY_TYPE
5115 && TREE_CODE (type) != FUNCTION_TYPE
5116 && can_convert (intype, strip_all_pointer_quals (type)))
5117 ok = 1;
5118 else if (TREE_CODE (intype) == ENUMERAL_TYPE
5119 && TREE_CODE (type) == ENUMERAL_TYPE)
5120 /* DR 128: "A value of integral _or enumeration_ type can be explicitly
5121 converted to an enumeration type."
5122 The integral to enumeration will be accepted by the previous clause.
5123 We need to explicitly check for enumeration to enumeration. */
5124 ok = 1;
5126 /* [expr.static.cast]
5128 The static_cast operator shall not be used to cast away
5129 constness. */
5130 if (ok && casts_away_constness (intype, type))
5132 cp_error ("static_cast from type `%T' to type `%T' casts away constness",
5133 intype, type);
5134 return error_mark_node;
5137 if (ok)
5138 return build_c_cast (type, expr);
5140 cp_error ("invalid static_cast from type `%T' to type `%T'", intype, type);
5141 return error_mark_node;
5144 tree
5145 build_reinterpret_cast (type, expr)
5146 tree type, expr;
5148 tree intype;
5150 if (type == error_mark_node || expr == error_mark_node)
5151 return error_mark_node;
5153 if (TREE_CODE (expr) == OFFSET_REF)
5154 expr = resolve_offset_ref (expr);
5156 if (processing_template_decl)
5158 tree t = build_min (REINTERPRET_CAST_EXPR, type, expr);
5159 return t;
5162 if (TREE_CODE (type) != REFERENCE_TYPE)
5164 expr = decay_conversion (expr);
5166 /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
5167 Strip such NOP_EXPRs if VALUE is being used in non-lvalue context. */
5168 if (TREE_CODE (expr) == NOP_EXPR
5169 && TREE_TYPE (expr) == TREE_TYPE (TREE_OPERAND (expr, 0)))
5170 expr = TREE_OPERAND (expr, 0);
5173 intype = TREE_TYPE (expr);
5175 if (TREE_CODE (type) == REFERENCE_TYPE)
5177 if (! real_lvalue_p (expr))
5179 cp_error ("invalid reinterpret_cast of an rvalue expression of type `%T' to type `%T'", intype, type);
5180 return error_mark_node;
5182 expr = build_unary_op (ADDR_EXPR, expr, 0);
5183 if (expr != error_mark_node)
5184 expr = build_reinterpret_cast
5185 (build_pointer_type (TREE_TYPE (type)), expr);
5186 if (expr != error_mark_node)
5187 expr = build_indirect_ref (expr, 0);
5188 return expr;
5190 else if (same_type_ignoring_top_level_qualifiers_p (intype, type))
5191 return build_static_cast (type, expr);
5193 if (TYPE_PTR_P (type) && (TREE_CODE (intype) == INTEGER_TYPE
5194 || TREE_CODE (intype) == ENUMERAL_TYPE))
5195 /* OK */;
5196 else if (TREE_CODE (type) == INTEGER_TYPE && TYPE_PTR_P (intype))
5198 if (TYPE_PRECISION (type) < TYPE_PRECISION (intype))
5199 cp_pedwarn ("reinterpret_cast from `%T' to `%T' loses precision",
5200 intype, type);
5202 else if ((TYPE_PTRFN_P (type) && TYPE_PTRFN_P (intype))
5203 || (TYPE_PTRMEMFUNC_P (type) && TYPE_PTRMEMFUNC_P (intype)))
5205 expr = decl_constant_value (expr);
5206 return fold (build1 (NOP_EXPR, type, expr));
5208 else if ((TYPE_PTRMEM_P (type) && TYPE_PTRMEM_P (intype))
5209 || (TYPE_PTROBV_P (type) && TYPE_PTROBV_P (intype)))
5211 if (! comp_ptr_ttypes_reinterpret (TREE_TYPE (type), TREE_TYPE (intype)))
5212 cp_pedwarn ("reinterpret_cast from `%T' to `%T' casts away const (or volatile)",
5213 intype, type);
5215 expr = decl_constant_value (expr);
5216 return fold (build1 (NOP_EXPR, type, expr));
5218 else if ((TYPE_PTRFN_P (type) && TYPE_PTROBV_P (intype))
5219 || (TYPE_PTRFN_P (intype) && TYPE_PTROBV_P (type)))
5221 pedwarn ("ISO C++ forbids casting between pointer-to-function and pointer-to-object");
5222 expr = decl_constant_value (expr);
5223 return fold (build1 (NOP_EXPR, type, expr));
5225 else
5227 cp_error ("invalid reinterpret_cast from type `%T' to type `%T'",
5228 intype, type);
5229 return error_mark_node;
5232 return cp_convert (type, expr);
5235 tree
5236 build_const_cast (type, expr)
5237 tree type, expr;
5239 tree intype;
5241 if (type == error_mark_node || expr == error_mark_node)
5242 return error_mark_node;
5244 if (TREE_CODE (expr) == OFFSET_REF)
5245 expr = resolve_offset_ref (expr);
5247 if (processing_template_decl)
5249 tree t = build_min (CONST_CAST_EXPR, type, expr);
5250 return t;
5253 if (!POINTER_TYPE_P (type))
5254 cp_error ("invalid use of const_cast with type `%T', which is not a pointer, reference, nor a pointer-to-data-member type", type);
5255 else if (TREE_CODE (TREE_TYPE (type)) == FUNCTION_TYPE)
5257 cp_error ("invalid use of const_cast with type `%T', which is a pointer or reference to a function type", type);
5258 return error_mark_node;
5261 if (TREE_CODE (type) != REFERENCE_TYPE)
5263 expr = decay_conversion (expr);
5265 /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
5266 Strip such NOP_EXPRs if VALUE is being used in non-lvalue context. */
5267 if (TREE_CODE (expr) == NOP_EXPR
5268 && TREE_TYPE (expr) == TREE_TYPE (TREE_OPERAND (expr, 0)))
5269 expr = TREE_OPERAND (expr, 0);
5272 intype = TREE_TYPE (expr);
5274 if (same_type_ignoring_top_level_qualifiers_p (intype, type))
5275 return build_static_cast (type, expr);
5276 else if (TREE_CODE (type) == REFERENCE_TYPE)
5278 if (! real_lvalue_p (expr))
5280 cp_error ("invalid const_cast of an rvalue of type `%T' to type `%T'", intype, type);
5281 return error_mark_node;
5284 if (comp_ptr_ttypes_const (TREE_TYPE (type), intype))
5286 expr = build_unary_op (ADDR_EXPR, expr, 0);
5287 expr = build1 (NOP_EXPR, type, expr);
5288 return convert_from_reference (expr);
5291 else if (TREE_CODE (type) == POINTER_TYPE
5292 && TREE_CODE (intype) == POINTER_TYPE
5293 && comp_ptr_ttypes_const (TREE_TYPE (type), TREE_TYPE (intype)))
5294 return cp_convert (type, expr);
5296 cp_error ("invalid const_cast from type `%T' to type `%T'", intype, type);
5297 return error_mark_node;
5300 /* Build an expression representing a cast to type TYPE of expression EXPR.
5302 ALLOW_NONCONVERTING is true if we should allow non-converting constructors
5303 when doing the cast. */
5305 tree
5306 build_c_cast (type, expr)
5307 tree type, expr;
5309 register tree value = expr;
5310 tree otype;
5312 if (type == error_mark_node || expr == error_mark_node)
5313 return error_mark_node;
5315 if (processing_template_decl)
5317 tree t = build_min (CAST_EXPR, type,
5318 tree_cons (NULL_TREE, value, NULL_TREE));
5319 return t;
5322 /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
5323 Strip such NOP_EXPRs if VALUE is being used in non-lvalue context. */
5324 if (TREE_CODE (type) != REFERENCE_TYPE
5325 && TREE_CODE (value) == NOP_EXPR
5326 && TREE_TYPE (value) == TREE_TYPE (TREE_OPERAND (value, 0)))
5327 value = TREE_OPERAND (value, 0);
5329 if (TREE_CODE (value) == OFFSET_REF)
5330 value = resolve_offset_ref (value);
5332 if (TREE_CODE (type) == ARRAY_TYPE)
5334 /* Allow casting from T1* to T2[] because Cfront allows it.
5335 NIHCL uses it. It is not valid ISO C++ however. */
5336 if (TREE_CODE (TREE_TYPE (expr)) == POINTER_TYPE)
5338 cp_pedwarn ("ISO C++ forbids casting to an array type `%T'", type);
5339 type = build_pointer_type (TREE_TYPE (type));
5341 else
5343 cp_error ("ISO C++ forbids casting to an array type `%T'", type);
5344 return error_mark_node;
5348 if (TREE_CODE (type) == FUNCTION_TYPE
5349 || TREE_CODE (type) == METHOD_TYPE)
5351 cp_error ("invalid cast to function type `%T'", type);
5352 return error_mark_node;
5355 if (TREE_CODE (type) == VOID_TYPE)
5357 /* Conversion to void does not cause any of the normal function to
5358 * pointer, array to pointer and lvalue to rvalue decays. */
5360 value = convert_to_void (value, /*implicit=*/NULL);
5361 return value;
5363 /* Convert functions and arrays to pointers and
5364 convert references to their expanded types,
5365 but don't convert any other types. If, however, we are
5366 casting to a class type, there's no reason to do this: the
5367 cast will only succeed if there is a converting constructor,
5368 and the default conversions will be done at that point. In
5369 fact, doing the default conversion here is actually harmful
5370 in cases like this:
5372 typedef int A[2];
5373 struct S { S(const A&); };
5375 since we don't want the array-to-pointer conversion done. */
5376 if (!IS_AGGR_TYPE (type))
5378 if (TREE_CODE (TREE_TYPE (value)) == FUNCTION_TYPE
5379 || (TREE_CODE (TREE_TYPE (value)) == METHOD_TYPE
5380 /* Don't do the default conversion on a ->* expression. */
5381 && ! (TREE_CODE (type) == POINTER_TYPE
5382 && bound_pmf_p (value)))
5383 || TREE_CODE (TREE_TYPE (value)) == ARRAY_TYPE
5384 || TREE_CODE (TREE_TYPE (value)) == REFERENCE_TYPE)
5385 value = default_conversion (value);
5387 else if (TREE_CODE (TREE_TYPE (value)) == REFERENCE_TYPE)
5388 /* However, even for class types, we still need to strip away
5389 the reference type, since the call to convert_force below
5390 does not expect the input expression to be of reference
5391 type. */
5392 value = convert_from_reference (value);
5394 otype = TREE_TYPE (value);
5396 /* Optionally warn about potentially worrisome casts. */
5398 if (warn_cast_qual
5399 && TREE_CODE (type) == POINTER_TYPE
5400 && TREE_CODE (otype) == POINTER_TYPE
5401 && !at_least_as_qualified_p (TREE_TYPE (type),
5402 TREE_TYPE (otype)))
5403 cp_warning ("cast from `%T' to `%T' discards qualifiers from pointer target type",
5404 otype, type);
5406 if (TREE_CODE (type) == INTEGER_TYPE
5407 && TREE_CODE (otype) == POINTER_TYPE
5408 && TYPE_PRECISION (type) != TYPE_PRECISION (otype))
5409 warning ("cast from pointer to integer of different size");
5411 if (TREE_CODE (type) == POINTER_TYPE
5412 && TREE_CODE (otype) == INTEGER_TYPE
5413 && TYPE_PRECISION (type) != TYPE_PRECISION (otype)
5414 /* Don't warn about converting any constant. */
5415 && !TREE_CONSTANT (value))
5416 warning ("cast to pointer from integer of different size");
5418 if (TREE_CODE (type) == REFERENCE_TYPE)
5419 value = (convert_from_reference
5420 (convert_to_reference (type, value, CONV_C_CAST,
5421 LOOKUP_COMPLAIN, NULL_TREE)));
5422 else
5424 tree ovalue;
5426 value = decl_constant_value (value);
5428 ovalue = value;
5429 value = convert_force (type, value, CONV_C_CAST);
5431 /* Ignore any integer overflow caused by the cast. */
5432 if (TREE_CODE (value) == INTEGER_CST)
5434 TREE_OVERFLOW (value) = TREE_OVERFLOW (ovalue);
5435 TREE_CONSTANT_OVERFLOW (value) = TREE_CONSTANT_OVERFLOW (ovalue);
5439 /* Warn about possible alignment problems. Do this here when we will have
5440 instantiated any necessary template types. */
5441 if (STRICT_ALIGNMENT && warn_cast_align
5442 && TREE_CODE (type) == POINTER_TYPE
5443 && TREE_CODE (otype) == POINTER_TYPE
5444 && TREE_CODE (TREE_TYPE (otype)) != VOID_TYPE
5445 && TREE_CODE (TREE_TYPE (otype)) != FUNCTION_TYPE
5446 && COMPLETE_TYPE_P (TREE_TYPE (otype))
5447 && COMPLETE_TYPE_P (TREE_TYPE (type))
5448 && TYPE_ALIGN (TREE_TYPE (type)) > TYPE_ALIGN (TREE_TYPE (otype)))
5449 cp_warning ("cast from `%T' to `%T' increases required alignment of target type",
5450 otype, type);
5452 /* Always produce some operator for an explicit cast,
5453 so we can tell (for -pedantic) that the cast is no lvalue. */
5454 if (TREE_CODE (type) != REFERENCE_TYPE && value == expr
5455 && real_lvalue_p (value))
5456 value = non_lvalue (value);
5458 return value;
5461 /* Build an assignment expression of lvalue LHS from value RHS.
5462 MODIFYCODE is the code for a binary operator that we use
5463 to combine the old value of LHS with RHS to get the new value.
5464 Or else MODIFYCODE is NOP_EXPR meaning do a simple assignment.
5466 C++: If MODIFYCODE is INIT_EXPR, then leave references unbashed. */
5468 tree
5469 build_modify_expr (lhs, modifycode, rhs)
5470 tree lhs;
5471 enum tree_code modifycode;
5472 tree rhs;
5474 register tree result;
5475 tree newrhs = rhs;
5476 tree lhstype = TREE_TYPE (lhs);
5477 tree olhstype = lhstype;
5478 tree olhs = lhs;
5480 /* Avoid duplicate error messages from operands that had errors. */
5481 if (lhs == error_mark_node || rhs == error_mark_node)
5482 return error_mark_node;
5484 /* Types that aren't fully specified cannot be used in assignments. */
5485 lhs = require_complete_type (lhs);
5487 newrhs = rhs;
5489 /* Handle control structure constructs used as "lvalues". */
5491 switch (TREE_CODE (lhs))
5493 /* Handle --foo = 5; as these are valid constructs in C++ */
5494 case PREDECREMENT_EXPR:
5495 case PREINCREMENT_EXPR:
5496 if (TREE_SIDE_EFFECTS (TREE_OPERAND (lhs, 0)))
5497 lhs = build (TREE_CODE (lhs), TREE_TYPE (lhs),
5498 stabilize_reference (TREE_OPERAND (lhs, 0)),
5499 TREE_OPERAND (lhs, 1));
5500 return build (COMPOUND_EXPR, lhstype,
5501 lhs,
5502 build_modify_expr (TREE_OPERAND (lhs, 0),
5503 modifycode, rhs));
5505 /* Handle (a, b) used as an "lvalue". */
5506 case COMPOUND_EXPR:
5507 newrhs = build_modify_expr (TREE_OPERAND (lhs, 1),
5508 modifycode, rhs);
5509 if (newrhs == error_mark_node)
5510 return error_mark_node;
5511 return build (COMPOUND_EXPR, lhstype,
5512 TREE_OPERAND (lhs, 0), newrhs);
5514 case MODIFY_EXPR:
5515 newrhs = build_modify_expr (TREE_OPERAND (lhs, 0), modifycode, rhs);
5516 if (newrhs == error_mark_node)
5517 return error_mark_node;
5518 return build (COMPOUND_EXPR, lhstype, lhs, newrhs);
5520 /* Handle (a ? b : c) used as an "lvalue". */
5521 case COND_EXPR:
5522 rhs = save_expr (rhs);
5524 /* Produce (a ? (b = rhs) : (c = rhs))
5525 except that the RHS goes through a save-expr
5526 so the code to compute it is only emitted once. */
5527 tree cond;
5529 /* Check this here to avoid odd errors when trying to convert
5530 a throw to the type of the COND_EXPR. */
5531 if (!lvalue_or_else (lhs, "assignment"))
5532 return error_mark_node;
5534 cond = build_conditional_expr
5535 (TREE_OPERAND (lhs, 0),
5536 build_modify_expr (cp_convert (TREE_TYPE (lhs),
5537 TREE_OPERAND (lhs, 1)),
5538 modifycode, rhs),
5539 build_modify_expr (cp_convert (TREE_TYPE (lhs),
5540 TREE_OPERAND (lhs, 2)),
5541 modifycode, rhs));
5543 if (cond == error_mark_node)
5544 return cond;
5545 /* Make sure the code to compute the rhs comes out
5546 before the split. */
5547 return build (COMPOUND_EXPR, TREE_TYPE (lhs),
5548 /* Case to void to suppress warning
5549 from warn_if_unused_value. */
5550 cp_convert (void_type_node, rhs), cond);
5553 default:
5554 break;
5557 if (TREE_CODE (lhs) == OFFSET_REF)
5559 if (TREE_OPERAND (lhs, 0) == NULL_TREE)
5561 /* Static class member? */
5562 tree member = TREE_OPERAND (lhs, 1);
5563 if (TREE_CODE (member) == VAR_DECL)
5564 lhs = member;
5565 else
5567 compiler_error ("invalid static class member");
5568 return error_mark_node;
5571 else
5572 lhs = resolve_offset_ref (lhs);
5574 olhstype = lhstype = TREE_TYPE (lhs);
5577 if (lhs == error_mark_node)
5578 return lhs;
5580 if (TREE_CODE (lhstype) == REFERENCE_TYPE
5581 && modifycode != INIT_EXPR)
5583 lhs = convert_from_reference (lhs);
5584 olhstype = lhstype = TREE_TYPE (lhs);
5587 /* If a binary op has been requested, combine the old LHS value with the RHS
5588 producing the value we should actually store into the LHS. */
5590 if (modifycode == INIT_EXPR)
5592 if (TREE_CODE (rhs) == CONSTRUCTOR)
5594 if (! same_type_p (TREE_TYPE (rhs), lhstype))
5595 abort ();
5596 result = build (INIT_EXPR, lhstype, lhs, rhs);
5597 TREE_SIDE_EFFECTS (result) = 1;
5598 return result;
5600 else if (! IS_AGGR_TYPE (lhstype))
5601 /* Do the default thing */;
5602 else
5604 result = build_method_call (lhs, complete_ctor_identifier,
5605 build_tree_list (NULL_TREE, rhs),
5606 TYPE_BINFO (lhstype), LOOKUP_NORMAL);
5607 if (result == NULL_TREE)
5608 return error_mark_node;
5609 return result;
5612 else if (modifycode == NOP_EXPR)
5614 /* `operator=' is not an inheritable operator. */
5615 if (! IS_AGGR_TYPE (lhstype))
5616 /* Do the default thing */;
5617 else
5619 result = build_opfncall (MODIFY_EXPR, LOOKUP_NORMAL,
5620 lhs, rhs, make_node (NOP_EXPR));
5621 if (result == NULL_TREE)
5622 return error_mark_node;
5623 return result;
5625 lhstype = olhstype;
5627 else if (PROMOTES_TO_AGGR_TYPE (lhstype, REFERENCE_TYPE))
5629 my_friendly_abort (978652);
5631 else
5633 lhs = stabilize_reference (lhs);
5634 newrhs = cp_build_binary_op (modifycode, lhs, rhs);
5635 if (newrhs == error_mark_node)
5637 cp_error (" in evaluation of `%Q(%#T, %#T)'", modifycode,
5638 TREE_TYPE (lhs), TREE_TYPE (rhs));
5639 return error_mark_node;
5643 /* Handle a cast used as an "lvalue".
5644 We have already performed any binary operator using the value as cast.
5645 Now convert the result to the cast type of the lhs,
5646 and then true type of the lhs and store it there;
5647 then convert result back to the cast type to be the value
5648 of the assignment. */
5650 switch (TREE_CODE (lhs))
5652 case NOP_EXPR:
5653 case CONVERT_EXPR:
5654 case FLOAT_EXPR:
5655 case FIX_TRUNC_EXPR:
5656 case FIX_FLOOR_EXPR:
5657 case FIX_ROUND_EXPR:
5658 case FIX_CEIL_EXPR:
5659 if (TREE_CODE (TREE_TYPE (newrhs)) == ARRAY_TYPE
5660 || TREE_CODE (TREE_TYPE (newrhs)) == FUNCTION_TYPE
5661 || TREE_CODE (TREE_TYPE (newrhs)) == METHOD_TYPE
5662 || TREE_CODE (TREE_TYPE (newrhs)) == OFFSET_TYPE)
5663 newrhs = default_conversion (newrhs);
5665 tree inner_lhs = TREE_OPERAND (lhs, 0);
5666 tree result;
5668 /* ISO C++ 5.4/1: The result is an lvalue if T is a reference
5669 type, otherwise the result is an rvalue. */
5670 if (! lvalue_p (lhs))
5671 pedwarn ("ISO C++ forbids cast to non-reference type used as lvalue");
5673 result = build_modify_expr (inner_lhs, NOP_EXPR,
5674 cp_convert (TREE_TYPE (inner_lhs),
5675 cp_convert (lhstype, newrhs)));
5676 if (result == error_mark_node)
5677 return result;
5678 return cp_convert (TREE_TYPE (lhs), result);
5681 default:
5682 break;
5685 /* Now we have handled acceptable kinds of LHS that are not truly lvalues.
5686 Reject anything strange now. */
5688 if (!lvalue_or_else (lhs, "assignment"))
5689 return error_mark_node;
5691 GNU_xref_assign (lhs);
5693 /* Warn about storing in something that is `const'. */
5694 /* For C++, don't warn if this is initialization. */
5695 if (modifycode != INIT_EXPR
5696 && (TREE_READONLY (lhs) || CP_TYPE_CONST_P (lhstype)
5697 /* Functions are not modifiable, even though they are
5698 lvalues. */
5699 || TREE_CODE (TREE_TYPE (lhs)) == FUNCTION_TYPE
5700 || (IS_AGGR_TYPE_CODE (TREE_CODE (lhstype))
5701 && C_TYPE_FIELDS_READONLY (lhstype))
5702 || (TREE_CODE (lhstype) == REFERENCE_TYPE
5703 && CP_TYPE_CONST_P (TREE_TYPE (lhstype)))))
5704 readonly_error (lhs, "assignment", 0);
5706 /* If storing into a structure or union member,
5707 it has probably been given type `int'.
5708 Compute the type that would go with
5709 the actual amount of storage the member occupies. */
5711 if (TREE_CODE (lhs) == COMPONENT_REF
5712 && (TREE_CODE (lhstype) == INTEGER_TYPE
5713 || TREE_CODE (lhstype) == REAL_TYPE
5714 || TREE_CODE (lhstype) == ENUMERAL_TYPE))
5716 lhstype = TREE_TYPE (get_unwidened (lhs, 0));
5718 /* If storing in a field that is in actuality a short or narrower
5719 than one, we must store in the field in its actual type. */
5721 if (lhstype != TREE_TYPE (lhs))
5723 lhs = copy_node (lhs);
5724 TREE_TYPE (lhs) = lhstype;
5728 if (modifycode != INIT_EXPR)
5730 /* Make modifycode now either a NOP_EXPR or an INIT_EXPR. */
5731 modifycode = NOP_EXPR;
5732 /* Reference-bashing */
5733 if (TREE_CODE (lhstype) == REFERENCE_TYPE)
5735 tree tmp = convert_from_reference (lhs);
5736 lhstype = TREE_TYPE (tmp);
5737 if (!COMPLETE_TYPE_P (lhstype))
5739 incomplete_type_error (lhs, lhstype);
5740 return error_mark_node;
5742 lhs = tmp;
5743 olhstype = lhstype;
5745 if (TREE_CODE (TREE_TYPE (newrhs)) == REFERENCE_TYPE)
5747 tree tmp = convert_from_reference (newrhs);
5748 if (!COMPLETE_TYPE_P (TREE_TYPE (tmp)))
5750 incomplete_type_error (newrhs, TREE_TYPE (tmp));
5751 return error_mark_node;
5753 newrhs = tmp;
5757 if (TREE_SIDE_EFFECTS (lhs))
5758 lhs = stabilize_reference (lhs);
5759 if (TREE_SIDE_EFFECTS (newrhs))
5760 newrhs = stabilize_reference (newrhs);
5762 /* Convert new value to destination type. */
5764 if (TREE_CODE (lhstype) == ARRAY_TYPE)
5766 int from_array;
5768 if (!same_or_base_type_p (lhstype, TREE_TYPE (rhs)))
5770 cp_error ("incompatible types in assignment of `%T' to `%T'",
5771 TREE_TYPE (rhs), lhstype);
5772 return error_mark_node;
5775 /* Allow array assignment in compiler-generated code. */
5776 if (pedantic && ! DECL_ARTIFICIAL (current_function_decl))
5777 pedwarn ("ISO C++ forbids assignment of arrays");
5779 from_array = TREE_CODE (TREE_TYPE (newrhs)) == ARRAY_TYPE
5780 ? 1 + (modifycode != INIT_EXPR): 0;
5781 return build_vec_init (lhs, newrhs, from_array);
5784 if (modifycode == INIT_EXPR)
5786 newrhs = convert_for_initialization (lhs, lhstype, newrhs, LOOKUP_NORMAL,
5787 "initialization", NULL_TREE, 0);
5788 if (current_function_decl &&
5789 lhs == DECL_RESULT (current_function_decl))
5791 if (DECL_INITIAL (lhs))
5792 warning ("return value from function receives multiple initializations");
5793 DECL_INITIAL (lhs) = newrhs;
5796 else
5798 /* Avoid warnings on enum bit fields. */
5799 if (TREE_CODE (olhstype) == ENUMERAL_TYPE
5800 && TREE_CODE (lhstype) == INTEGER_TYPE)
5802 newrhs = convert_for_assignment (olhstype, newrhs, "assignment",
5803 NULL_TREE, 0);
5804 newrhs = convert_force (lhstype, newrhs, 0);
5806 else
5807 newrhs = convert_for_assignment (lhstype, newrhs, "assignment",
5808 NULL_TREE, 0);
5809 if (TREE_CODE (newrhs) == CALL_EXPR
5810 && TYPE_NEEDS_CONSTRUCTING (lhstype))
5811 newrhs = build_cplus_new (lhstype, newrhs);
5813 /* Can't initialize directly from a TARGET_EXPR, since that would
5814 cause the lhs to be constructed twice, and possibly result in
5815 accidental self-initialization. So we force the TARGET_EXPR to be
5816 expanded without a target. */
5817 if (TREE_CODE (newrhs) == TARGET_EXPR)
5818 newrhs = build (COMPOUND_EXPR, TREE_TYPE (newrhs), newrhs,
5819 TREE_OPERAND (newrhs, 0));
5822 if (newrhs == error_mark_node)
5823 return error_mark_node;
5825 if (TREE_CODE (newrhs) == COND_EXPR)
5827 tree lhs1;
5828 tree cond = TREE_OPERAND (newrhs, 0);
5830 if (TREE_SIDE_EFFECTS (lhs))
5831 cond = build_compound_expr (tree_cons
5832 (NULL_TREE, lhs,
5833 build_tree_list (NULL_TREE, cond)));
5835 /* Cannot have two identical lhs on this one tree (result) as preexpand
5836 calls will rip them out and fill in RTL for them, but when the
5837 rtl is generated, the calls will only be in the first side of the
5838 condition, not on both, or before the conditional jump! (mrs) */
5839 lhs1 = break_out_calls (lhs);
5841 if (lhs == lhs1)
5842 /* If there's no change, the COND_EXPR behaves like any other rhs. */
5843 result = build (modifycode == NOP_EXPR ? MODIFY_EXPR : INIT_EXPR,
5844 lhstype, lhs, newrhs);
5845 else
5847 tree result_type = TREE_TYPE (newrhs);
5848 /* We have to convert each arm to the proper type because the
5849 types may have been munged by constant folding. */
5850 result
5851 = build (COND_EXPR, result_type, cond,
5852 build_modify_expr (lhs, modifycode,
5853 cp_convert (result_type,
5854 TREE_OPERAND (newrhs, 1))),
5855 build_modify_expr (lhs1, modifycode,
5856 cp_convert (result_type,
5857 TREE_OPERAND (newrhs, 2))));
5860 else
5861 result = build (modifycode == NOP_EXPR ? MODIFY_EXPR : INIT_EXPR,
5862 lhstype, lhs, newrhs);
5864 TREE_SIDE_EFFECTS (result) = 1;
5866 /* If we got the LHS in a different type for storing in,
5867 convert the result back to the nominal type of LHS
5868 so that the value we return always has the same type
5869 as the LHS argument. */
5871 if (olhstype == TREE_TYPE (result))
5872 return result;
5873 /* Avoid warnings converting integral types back into enums
5874 for enum bit fields. */
5875 if (TREE_CODE (TREE_TYPE (result)) == INTEGER_TYPE
5876 && TREE_CODE (olhstype) == ENUMERAL_TYPE)
5878 result = build (COMPOUND_EXPR, olhstype, result, olhs);
5879 TREE_NO_UNUSED_WARNING (result) = 1;
5880 return result;
5882 return convert_for_assignment (olhstype, result, "assignment",
5883 NULL_TREE, 0);
5886 tree
5887 build_x_modify_expr (lhs, modifycode, rhs)
5888 tree lhs;
5889 enum tree_code modifycode;
5890 tree rhs;
5892 if (processing_template_decl)
5893 return build_min_nt (MODOP_EXPR, lhs,
5894 build_min_nt (modifycode, NULL_TREE, NULL_TREE), rhs);
5896 if (modifycode != NOP_EXPR)
5898 tree rval = build_opfncall (MODIFY_EXPR, LOOKUP_NORMAL, lhs, rhs,
5899 make_node (modifycode));
5900 if (rval)
5901 return rval;
5903 return build_modify_expr (lhs, modifycode, rhs);
5907 /* Get difference in deltas for different pointer to member function
5908 types. Return integer_zero_node, if FROM cannot be converted to a
5909 TO type. If FORCE is true, then allow reverse conversions as well.
5911 Note that the naming of FROM and TO is kind of backwards; the return
5912 value is what we add to a TO in order to get a FROM. They are named
5913 this way because we call this function to find out how to convert from
5914 a pointer to member of FROM to a pointer to member of TO. */
5916 static tree
5917 get_delta_difference (from, to, force)
5918 tree from, to;
5919 int force;
5921 tree delta = integer_zero_node;
5922 tree binfo;
5923 tree virt_binfo;
5925 if (to == from)
5926 return delta;
5928 /* Should get_base_distance here, so we can check if any thing along
5929 the path is virtual, and we need to make sure we stay inside the
5930 real binfos when going through virtual bases. Maybe we should
5931 replace virtual bases with BINFO_FOR_VBASE ... (mrs) */
5932 binfo = get_binfo (from, to, 1);
5933 if (binfo == error_mark_node)
5935 error (" in pointer to member function conversion");
5936 return delta;
5938 if (binfo == 0)
5940 if (!force)
5942 error_not_base_type (from, to);
5943 error (" in pointer to member conversion");
5944 return delta;
5946 binfo = get_binfo (to, from, 1);
5947 if (binfo == 0 || binfo == error_mark_node)
5948 return delta;
5949 virt_binfo = binfo_from_vbase (binfo);
5951 if (virt_binfo)
5953 /* This is a reinterpret cast, we choose to do nothing. */
5954 cp_warning ("pointer to member cast via virtual base `%T' of `%T'",
5955 BINFO_TYPE (virt_binfo),
5956 BINFO_TYPE (BINFO_INHERITANCE_CHAIN (virt_binfo)));
5957 return delta;
5959 delta = BINFO_OFFSET (binfo);
5960 delta = cp_convert (ptrdiff_type_node, delta);
5961 delta = cp_build_binary_op (MINUS_EXPR,
5962 integer_zero_node,
5963 delta);
5965 return delta;
5968 virt_binfo = binfo_from_vbase (binfo);
5969 if (virt_binfo)
5971 /* This is a reinterpret cast, we choose to do nothing. */
5972 if (force)
5973 cp_warning ("pointer to member cast via virtual base `%T' of `%T'",
5974 BINFO_TYPE (virt_binfo),
5975 BINFO_TYPE (BINFO_INHERITANCE_CHAIN (virt_binfo)));
5976 else
5977 cp_error ("pointer to member conversion via virtual base `%T' of `%T'",
5978 BINFO_TYPE (virt_binfo),
5979 BINFO_TYPE (BINFO_INHERITANCE_CHAIN (virt_binfo)));
5980 return delta;
5982 delta = BINFO_OFFSET (binfo);
5984 return cp_convert (ptrdiff_type_node, delta);
5987 /* Return a constructor for the pointer-to-member-function TYPE using
5988 the other components as specified. */
5990 tree
5991 build_ptrmemfunc1 (type, delta, pfn)
5992 tree type, delta, pfn;
5994 tree u = NULL_TREE;
5995 tree delta_field;
5996 tree pfn_field;
5998 /* Pull the FIELD_DECLs out of the type. */
5999 pfn_field = TYPE_FIELDS (type);
6000 delta_field = TREE_CHAIN (pfn_field);
6002 /* Make sure DELTA has the type we want. */
6003 delta = convert_and_check (delta_type_node, delta);
6005 /* Finish creating the initializer. */
6006 u = tree_cons (pfn_field, pfn,
6007 build_tree_list (delta_field, delta));
6008 u = build (CONSTRUCTOR, type, NULL_TREE, u);
6009 TREE_CONSTANT (u) = TREE_CONSTANT (pfn) && TREE_CONSTANT (delta);
6010 TREE_STATIC (u) = (TREE_CONSTANT (u)
6011 && (initializer_constant_valid_p (pfn, TREE_TYPE (pfn))
6012 != NULL_TREE)
6013 && (initializer_constant_valid_p (delta, TREE_TYPE (delta))
6014 != NULL_TREE));
6015 return u;
6018 /* Build a constructor for a pointer to member function. It can be
6019 used to initialize global variables, local variable, or used
6020 as a value in expressions. TYPE is the POINTER to METHOD_TYPE we
6021 want to be.
6023 If FORCE is non-zero, then force this conversion, even if
6024 we would rather not do it. Usually set when using an explicit
6025 cast.
6027 Return error_mark_node, if something goes wrong. */
6029 tree
6030 build_ptrmemfunc (type, pfn, force)
6031 tree type, pfn;
6032 int force;
6034 tree fn;
6035 tree pfn_type = TREE_TYPE (pfn);
6036 tree to_type = build_ptrmemfunc_type (type);
6038 /* Handle multiple conversions of pointer to member functions. */
6039 if (TYPE_PTRMEMFUNC_P (TREE_TYPE (pfn)))
6041 tree delta = NULL_TREE;
6042 tree npfn = NULL_TREE;
6043 tree n;
6045 if (!force
6046 && !can_convert_arg (to_type, TREE_TYPE (pfn), pfn))
6047 cp_error ("invalid conversion to type `%T' from type `%T'",
6048 to_type, pfn_type);
6050 n = get_delta_difference (TYPE_PTRMEMFUNC_OBJECT_TYPE (pfn_type),
6051 TYPE_PTRMEMFUNC_OBJECT_TYPE (to_type),
6052 force);
6054 /* We don't have to do any conversion to convert a
6055 pointer-to-member to its own type. But, we don't want to
6056 just return a PTRMEM_CST if there's an explicit cast; that
6057 cast should make the expression an invalid template argument. */
6058 if (TREE_CODE (pfn) != PTRMEM_CST)
6060 if (same_type_p (to_type, pfn_type))
6061 return pfn;
6062 else if (integer_zerop (n))
6063 return build_reinterpret_cast (to_type, pfn);
6066 if (TREE_SIDE_EFFECTS (pfn))
6067 pfn = save_expr (pfn);
6069 /* Obtain the function pointer and the current DELTA. */
6070 if (TREE_CODE (pfn) == PTRMEM_CST)
6071 expand_ptrmemfunc_cst (pfn, &delta, &npfn);
6072 else
6074 npfn = build_component_ref (pfn, pfn_identifier, NULL_TREE, 0);
6075 delta = build_component_ref (pfn, delta_identifier, NULL_TREE, 0);
6078 /* Just adjust the DELTA field. */
6079 delta = cp_convert (ptrdiff_type_node, delta);
6080 if (TARGET_PTRMEMFUNC_VBIT_LOCATION == ptrmemfunc_vbit_in_delta)
6081 n = cp_build_binary_op (LSHIFT_EXPR, n, integer_one_node);
6082 delta = cp_build_binary_op (PLUS_EXPR, delta, n);
6083 return build_ptrmemfunc1 (to_type, delta, npfn);
6086 /* Handle null pointer to member function conversions. */
6087 if (integer_zerop (pfn))
6089 pfn = build_c_cast (type, integer_zero_node);
6090 return build_ptrmemfunc1 (to_type,
6091 integer_zero_node,
6092 pfn);
6095 if (type_unknown_p (pfn))
6096 return instantiate_type (type, pfn, itf_complain);
6098 fn = TREE_OPERAND (pfn, 0);
6099 my_friendly_assert (TREE_CODE (fn) == FUNCTION_DECL, 0);
6100 return make_ptrmem_cst (to_type, fn);
6103 /* Return the DELTA, IDX, PFN, and DELTA2 values for the PTRMEM_CST
6104 given by CST.
6106 ??? There is no consistency as to the types returned for the above
6107 values. Some code acts as if its a sizetype and some as if its
6108 integer_type_node. */
6110 void
6111 expand_ptrmemfunc_cst (cst, delta, pfn)
6112 tree cst;
6113 tree *delta;
6114 tree *pfn;
6116 tree type = TREE_TYPE (cst);
6117 tree fn = PTRMEM_CST_MEMBER (cst);
6118 tree ptr_class, fn_class;
6120 my_friendly_assert (TREE_CODE (fn) == FUNCTION_DECL, 0);
6122 /* The class that the function belongs to. */
6123 fn_class = DECL_CONTEXT (fn);
6125 /* The class that we're creating a pointer to member of. */
6126 ptr_class = TYPE_PTRMEMFUNC_OBJECT_TYPE (type);
6128 /* First, calculate the adjustment to the function's class. */
6129 *delta = get_delta_difference (fn_class, ptr_class, /*force=*/0);
6131 if (!DECL_VIRTUAL_P (fn))
6132 *pfn = convert (TYPE_PTRMEMFUNC_FN_TYPE (type), build_addr_func (fn));
6133 else
6135 /* If we're dealing with a virtual function, we have to adjust 'this'
6136 again, to point to the base which provides the vtable entry for
6137 fn; the call will do the opposite adjustment. */
6138 tree orig_class = DECL_VIRTUAL_CONTEXT (fn);
6139 tree binfo = binfo_or_else (orig_class, fn_class);
6140 *delta = fold (build (PLUS_EXPR, TREE_TYPE (*delta),
6141 *delta, BINFO_OFFSET (binfo)));
6143 /* We set PFN to the vtable offset at which the function can be
6144 found, plus one (unless ptrmemfunc_vbit_in_delta, in which
6145 case delta is shifted left, and then incremented). */
6146 *pfn = DECL_VINDEX (fn);
6147 *pfn = fold (build (MULT_EXPR, integer_type_node, *pfn,
6148 TYPE_SIZE_UNIT (vtable_entry_type)));
6150 switch (TARGET_PTRMEMFUNC_VBIT_LOCATION)
6152 case ptrmemfunc_vbit_in_pfn:
6153 *pfn = fold (build (PLUS_EXPR, integer_type_node, *pfn,
6154 integer_one_node));
6155 break;
6157 case ptrmemfunc_vbit_in_delta:
6158 *delta = fold (build (LSHIFT_EXPR, TREE_TYPE (*delta),
6159 *delta, integer_one_node));
6160 *delta = fold (build (PLUS_EXPR, TREE_TYPE (*delta),
6161 *delta, integer_one_node));
6162 break;
6164 default:
6165 abort ();
6168 *pfn = fold (build1 (NOP_EXPR, TYPE_PTRMEMFUNC_FN_TYPE (type),
6169 *pfn));
6173 /* Return an expression for PFN from the pointer-to-member function
6174 given by T. */
6176 tree
6177 pfn_from_ptrmemfunc (t)
6178 tree t;
6180 if (TREE_CODE (t) == PTRMEM_CST)
6182 tree delta;
6183 tree pfn;
6185 expand_ptrmemfunc_cst (t, &delta, &pfn);
6186 if (pfn)
6187 return pfn;
6190 return build_component_ref (t, pfn_identifier, NULL_TREE, 0);
6193 /* Expression EXPR is about to be implicitly converted to TYPE. Warn
6194 if this is a potentially dangerous thing to do. Returns a possibly
6195 marked EXPR. */
6197 tree
6198 dubious_conversion_warnings (type, expr, errtype, fndecl, parmnum)
6199 tree type;
6200 tree expr;
6201 const char *errtype;
6202 tree fndecl;
6203 int parmnum;
6205 if (TREE_CODE (type) == REFERENCE_TYPE)
6206 type = TREE_TYPE (type);
6208 /* Issue warnings about peculiar, but legal, uses of NULL. */
6209 if (ARITHMETIC_TYPE_P (type) && expr == null_node)
6211 if (fndecl)
6212 cp_warning ("passing NULL used for non-pointer %s %P of `%D'",
6213 errtype, parmnum, fndecl);
6214 else
6215 cp_warning ("%s to non-pointer type `%T' from NULL", errtype, type);
6218 /* Warn about assigning a floating-point type to an integer type. */
6219 if (TREE_CODE (TREE_TYPE (expr)) == REAL_TYPE
6220 && TREE_CODE (type) == INTEGER_TYPE)
6222 if (fndecl)
6223 cp_warning ("passing `%T' for %s %P of `%D'",
6224 TREE_TYPE (expr), errtype, parmnum, fndecl);
6225 else
6226 cp_warning ("%s to `%T' from `%T'", errtype, type, TREE_TYPE (expr));
6228 /* And warn about assigning a negative value to an unsigned
6229 variable. */
6230 else if (TREE_UNSIGNED (type) && TREE_CODE (type) != BOOLEAN_TYPE)
6232 if (TREE_CODE (expr) == INTEGER_CST
6233 && TREE_NEGATED_INT (expr))
6235 if (fndecl)
6236 cp_warning ("passing negative value `%E' for %s %P of `%D'",
6237 expr, errtype, parmnum, fndecl);
6238 else
6239 cp_warning ("%s of negative value `%E' to `%T'",
6240 errtype, expr, type);
6243 overflow_warning (expr);
6245 if (TREE_CONSTANT (expr))
6246 expr = fold (expr);
6248 return expr;
6251 /* Convert value RHS to type TYPE as preparation for an assignment to
6252 an lvalue of type TYPE. ERRTYPE is a string to use in error
6253 messages: "assignment", "return", etc. If FNDECL is non-NULL, we
6254 are doing the conversion in order to pass the PARMNUMth argument of
6255 FNDECL. */
6257 static tree
6258 convert_for_assignment (type, rhs, errtype, fndecl, parmnum)
6259 tree type, rhs;
6260 const char *errtype;
6261 tree fndecl;
6262 int parmnum;
6264 register enum tree_code codel = TREE_CODE (type);
6265 register tree rhstype;
6266 register enum tree_code coder;
6268 if (codel == OFFSET_TYPE)
6269 my_friendly_abort (990505);
6271 if (TREE_CODE (rhs) == OFFSET_REF)
6272 rhs = resolve_offset_ref (rhs);
6274 /* Strip NON_LVALUE_EXPRs since we aren't using as an lvalue. */
6275 if (TREE_CODE (rhs) == NON_LVALUE_EXPR)
6276 rhs = TREE_OPERAND (rhs, 0);
6278 rhstype = TREE_TYPE (rhs);
6279 coder = TREE_CODE (rhstype);
6281 if (rhs == error_mark_node || rhstype == error_mark_node)
6282 return error_mark_node;
6283 if (TREE_CODE (rhs) == TREE_LIST && TREE_VALUE (rhs) == error_mark_node)
6284 return error_mark_node;
6286 rhs = dubious_conversion_warnings (type, rhs, errtype, fndecl, parmnum);
6288 /* The RHS of an assignment cannot have void type. */
6289 if (coder == VOID_TYPE)
6291 error ("void value not ignored as it ought to be");
6292 return error_mark_node;
6295 /* Simplify the RHS if possible. */
6296 if (TREE_CODE (rhs) == CONST_DECL)
6297 rhs = DECL_INITIAL (rhs);
6298 else if (coder != ARRAY_TYPE)
6299 rhs = decl_constant_value (rhs);
6301 /* [expr.ass]
6303 The expression is implicitly converted (clause _conv_) to the
6304 cv-unqualified type of the left operand.
6306 We allow bad conversions here because by the time we get to this point
6307 we are committed to doing the conversion. If we end up doing a bad
6308 conversion, convert_like will complain. */
6309 if (!can_convert_arg_bad (type, rhstype, rhs))
6311 /* When -Wno-pmf-conversions is use, we just silently allow
6312 conversions from pointers-to-members to plain pointers. If
6313 the conversion doesn't work, cp_convert will complain. */
6314 if (!warn_pmf2ptr
6315 && TYPE_PTR_P (type)
6316 && TYPE_PTRMEMFUNC_P (rhstype))
6317 rhs = cp_convert (strip_top_quals (type), rhs);
6318 else
6320 /* If the right-hand side has unknown type, then it is an
6321 overloaded function. Call instantiate_type to get error
6322 messages. */
6323 if (rhstype == unknown_type_node)
6324 instantiate_type (type, rhs, itf_complain);
6325 else if (fndecl)
6326 cp_error ("cannot convert `%T' to `%T' for argument `%P' to `%D'",
6327 rhstype, type, parmnum, fndecl);
6328 else
6329 cp_error ("cannot convert `%T' to `%T' in %s", rhstype, type,
6330 errtype);
6331 return error_mark_node;
6334 return perform_implicit_conversion (strip_top_quals (type), rhs);
6337 /* Convert RHS to be of type TYPE.
6338 If EXP is non-zero, it is the target of the initialization.
6339 ERRTYPE is a string to use in error messages.
6341 Two major differences between the behavior of
6342 `convert_for_assignment' and `convert_for_initialization'
6343 are that references are bashed in the former, while
6344 copied in the latter, and aggregates are assigned in
6345 the former (operator=) while initialized in the
6346 latter (X(X&)).
6348 If using constructor make sure no conversion operator exists, if one does
6349 exist, an ambiguity exists.
6351 If flags doesn't include LOOKUP_COMPLAIN, don't complain about anything. */
6353 tree
6354 convert_for_initialization (exp, type, rhs, flags, errtype, fndecl, parmnum)
6355 tree exp, type, rhs;
6356 int flags;
6357 const char *errtype;
6358 tree fndecl;
6359 int parmnum;
6361 register enum tree_code codel = TREE_CODE (type);
6362 register tree rhstype;
6363 register enum tree_code coder;
6365 /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
6366 Strip such NOP_EXPRs, since RHS is used in non-lvalue context. */
6367 if (TREE_CODE (rhs) == NOP_EXPR
6368 && TREE_TYPE (rhs) == TREE_TYPE (TREE_OPERAND (rhs, 0))
6369 && codel != REFERENCE_TYPE)
6370 rhs = TREE_OPERAND (rhs, 0);
6372 if (rhs == error_mark_node
6373 || (TREE_CODE (rhs) == TREE_LIST && TREE_VALUE (rhs) == error_mark_node))
6374 return error_mark_node;
6376 if (TREE_CODE (rhs) == OFFSET_REF)
6378 rhs = resolve_offset_ref (rhs);
6379 if (rhs == error_mark_node)
6380 return error_mark_node;
6383 if (TREE_CODE (TREE_TYPE (rhs)) == REFERENCE_TYPE)
6384 rhs = convert_from_reference (rhs);
6386 if ((TREE_CODE (TREE_TYPE (rhs)) == ARRAY_TYPE
6387 && TREE_CODE (type) != ARRAY_TYPE
6388 && (TREE_CODE (type) != REFERENCE_TYPE
6389 || TREE_CODE (TREE_TYPE (type)) != ARRAY_TYPE))
6390 || (TREE_CODE (TREE_TYPE (rhs)) == FUNCTION_TYPE
6391 && (TREE_CODE (type) != REFERENCE_TYPE
6392 || TREE_CODE (TREE_TYPE (type)) != FUNCTION_TYPE))
6393 || TREE_CODE (TREE_TYPE (rhs)) == METHOD_TYPE)
6394 rhs = default_conversion (rhs);
6396 rhstype = TREE_TYPE (rhs);
6397 coder = TREE_CODE (rhstype);
6399 if (coder == ERROR_MARK)
6400 return error_mark_node;
6402 /* We accept references to incomplete types, so we can
6403 return here before checking if RHS is of complete type. */
6405 if (codel == REFERENCE_TYPE)
6407 /* This should eventually happen in convert_arguments. */
6408 int savew = 0, savee = 0;
6410 if (fndecl)
6411 savew = warningcount, savee = errorcount;
6412 rhs = initialize_reference (type, rhs);
6413 if (fndecl)
6415 if (warningcount > savew)
6416 cp_warning_at ("in passing argument %P of `%+D'", parmnum, fndecl);
6417 else if (errorcount > savee)
6418 cp_error_at ("in passing argument %P of `%+D'", parmnum, fndecl);
6420 return rhs;
6423 if (exp != 0)
6424 exp = require_complete_type (exp);
6425 if (exp == error_mark_node)
6426 return error_mark_node;
6428 if (TREE_CODE (rhstype) == REFERENCE_TYPE)
6429 rhstype = TREE_TYPE (rhstype);
6431 type = complete_type (type);
6433 if (IS_AGGR_TYPE (type))
6434 return ocp_convert (type, rhs, CONV_IMPLICIT|CONV_FORCE_TEMP, flags);
6436 return convert_for_assignment (type, rhs, errtype, fndecl, parmnum);
6439 /* Expand an ASM statement with operands, handling output operands
6440 that are not variables or INDIRECT_REFS by transforming such
6441 cases into cases that expand_asm_operands can handle.
6443 Arguments are same as for expand_asm_operands.
6445 We don't do default conversions on all inputs, because it can screw
6446 up operands that are expected to be in memory. */
6448 void
6449 c_expand_asm_operands (string, outputs, inputs, clobbers, vol, filename, line)
6450 tree string, outputs, inputs, clobbers;
6451 int vol;
6452 const char *filename;
6453 int line;
6455 int noutputs = list_length (outputs);
6456 register int i;
6457 /* o[I] is the place that output number I should be written. */
6458 register tree *o = (tree *) alloca (noutputs * sizeof (tree));
6459 register tree tail;
6461 /* Record the contents of OUTPUTS before it is modified. */
6462 for (i = 0, tail = outputs; tail; tail = TREE_CHAIN (tail), i++)
6463 o[i] = TREE_VALUE (tail);
6465 /* Generate the ASM_OPERANDS insn;
6466 store into the TREE_VALUEs of OUTPUTS some trees for
6467 where the values were actually stored. */
6468 expand_asm_operands (string, outputs, inputs, clobbers, vol, filename, line);
6470 /* Copy all the intermediate outputs into the specified outputs. */
6471 for (i = 0, tail = outputs; tail; tail = TREE_CHAIN (tail), i++)
6473 if (o[i] != TREE_VALUE (tail))
6475 expand_expr (build_modify_expr (o[i], NOP_EXPR, TREE_VALUE (tail)),
6476 const0_rtx, VOIDmode, EXPAND_NORMAL);
6477 free_temp_slots ();
6479 /* Restore the original value so that it's correct the next
6480 time we expand this function. */
6481 TREE_VALUE (tail) = o[i];
6483 /* Detect modification of read-only values.
6484 (Otherwise done by build_modify_expr.) */
6485 else
6487 tree type = TREE_TYPE (o[i]);
6488 if (CP_TYPE_CONST_P (type)
6489 || (IS_AGGR_TYPE_CODE (TREE_CODE (type))
6490 && C_TYPE_FIELDS_READONLY (type)))
6491 readonly_error (o[i], "modification by `asm'", 1);
6495 /* Those MODIFY_EXPRs could do autoincrements. */
6496 emit_queue ();
6499 /* If RETVAL is the address of, or a reference to, a local variable or
6500 temporary give an appropraite warning. */
6502 static void
6503 maybe_warn_about_returning_address_of_local (retval)
6504 tree retval;
6506 tree valtype = TREE_TYPE (DECL_RESULT (current_function_decl));
6507 tree whats_returned = retval;
6509 for (;;)
6511 if (TREE_CODE (whats_returned) == COMPOUND_EXPR)
6512 whats_returned = TREE_OPERAND (whats_returned, 1);
6513 else if (TREE_CODE (whats_returned) == CONVERT_EXPR
6514 || TREE_CODE (whats_returned) == NON_LVALUE_EXPR
6515 || TREE_CODE (whats_returned) == NOP_EXPR)
6516 whats_returned = TREE_OPERAND (whats_returned, 0);
6517 else
6518 break;
6521 if (TREE_CODE (whats_returned) != ADDR_EXPR)
6522 return;
6523 whats_returned = TREE_OPERAND (whats_returned, 0);
6525 if (TREE_CODE (valtype) == REFERENCE_TYPE)
6527 if (TREE_CODE (whats_returned) == AGGR_INIT_EXPR
6528 || TREE_CODE (whats_returned) == TARGET_EXPR)
6530 /* Get the target. */
6531 whats_returned = TREE_OPERAND (whats_returned, 0);
6532 warning ("returning reference to temporary");
6533 return;
6535 if (TREE_CODE (whats_returned) == VAR_DECL
6536 && DECL_NAME (whats_returned)
6537 && TEMP_NAME_P (DECL_NAME (whats_returned)))
6539 warning ("reference to non-lvalue returned");
6540 return;
6544 if (TREE_CODE (whats_returned) == VAR_DECL
6545 && DECL_NAME (whats_returned)
6546 && DECL_FUNCTION_SCOPE_P (whats_returned)
6547 && !(TREE_STATIC (whats_returned)
6548 || TREE_PUBLIC (whats_returned)))
6550 if (TREE_CODE (valtype) == REFERENCE_TYPE)
6551 cp_warning_at ("reference to local variable `%D' returned",
6552 whats_returned);
6553 else
6554 cp_warning_at ("address of local variable `%D' returned",
6555 whats_returned);
6556 return;
6560 /* Check that returning RETVAL from the current function is legal.
6561 Return an expression explicitly showing all conversions required to
6562 change RETVAL into the function return type, and to assign it to
6563 the DECL_RESULT for the function. */
6565 tree
6566 check_return_expr (retval)
6567 tree retval;
6569 tree result;
6570 /* The type actually returned by the function, after any
6571 promotions. */
6572 tree valtype;
6573 int fn_returns_value_p;
6575 /* A `volatile' function is one that isn't supposed to return, ever.
6576 (This is a G++ extension, used to get better code for functions
6577 that call the `volatile' function.) */
6578 if (TREE_THIS_VOLATILE (current_function_decl))
6579 warning ("function declared `noreturn' has a `return' statement");
6581 /* Check for various simple errors. */
6582 if (dtor_label)
6584 if (retval)
6585 error ("returning a value from a destructor");
6586 return NULL_TREE;
6588 else if (DECL_CONSTRUCTOR_P (current_function_decl))
6590 if (in_function_try_handler)
6591 /* If a return statement appears in a handler of the
6592 function-try-block of a constructor, the program is ill-formed. */
6593 error ("cannot return from a handler of a function-try-block of a constructor");
6594 else if (retval)
6595 /* You can't return a value from a constructor. */
6596 error ("returning a value from a constructor");
6597 return NULL_TREE;
6600 /* When no explicit return-value is given in a function with a named
6601 return value, the named return value is used. */
6602 result = DECL_RESULT (current_function_decl);
6603 valtype = TREE_TYPE (result);
6604 my_friendly_assert (valtype != NULL_TREE, 19990924);
6605 fn_returns_value_p = !VOID_TYPE_P (valtype);
6606 if (!retval && DECL_NAME (result) && fn_returns_value_p)
6607 retval = result;
6609 /* Check for a return statement with no return value in a function
6610 that's supposed to return a value. */
6611 if (!retval && fn_returns_value_p)
6613 pedwarn ("return-statement with no value, in function declared with a non-void return type");
6614 /* Clear this, so finish_function won't say that we reach the
6615 end of a non-void function (which we don't, we gave a
6616 return!). */
6617 current_function_returns_null = 0;
6619 /* Check for a return statement with a value in a function that
6620 isn't supposed to return a value. */
6621 else if (retval && !fn_returns_value_p)
6623 if (VOID_TYPE_P (TREE_TYPE (retval)))
6624 /* You can return a `void' value from a function of `void'
6625 type. In that case, we have to evaluate the expression for
6626 its side-effects. */
6627 finish_expr_stmt (retval);
6628 else
6629 pedwarn ("return-statement with a value, in function declared with a void return type");
6631 current_function_returns_null = 1;
6633 /* There's really no value to return, after all. */
6634 return NULL_TREE;
6636 else if (!retval)
6637 /* Remember that this function can sometimes return without a
6638 value. */
6639 current_function_returns_null = 1;
6640 else
6641 /* Remember that this function did return a value. */
6642 current_function_returns_value = 1;
6644 /* Only operator new(...) throw(), can return NULL [expr.new/13]. */
6645 if ((DECL_OVERLOADED_OPERATOR_P (current_function_decl) == NEW_EXPR
6646 || DECL_OVERLOADED_OPERATOR_P (current_function_decl) == VEC_NEW_EXPR)
6647 && !TYPE_NOTHROW_P (TREE_TYPE (current_function_decl))
6648 && ! flag_check_new
6649 && null_ptr_cst_p (retval))
6650 cp_warning ("`operator new' must not return NULL unless it is declared `throw()' (or -fcheck-new is in effect)");
6652 /* Effective C++ rule 15. See also start_function. */
6653 if (warn_ecpp
6654 && DECL_NAME (current_function_decl) == ansi_assopname(NOP_EXPR)
6655 && retval != current_class_ref)
6656 cp_warning ("`operator=' should return a reference to `*this'");
6658 /* The fabled Named Return Value optimization, as per [class.copy]/15:
6660 [...] For a function with a class return type, if the expression
6661 in the return statement is the name of a local object, and the cv-
6662 unqualified type of the local object is the same as the function
6663 return type, an implementation is permitted to omit creating the tem-
6664 porary object to hold the function return value [...]
6666 So, if this is a value-returning function that always returns the same
6667 local variable, remember it.
6669 It might be nice to be more flexible, and choose the first suitable
6670 variable even if the function sometimes returns something else, but
6671 then we run the risk of clobbering the variable we chose if the other
6672 returned expression uses the chosen variable somehow. And people expect
6673 this restriction, anyway. (jason 2000-11-19)
6675 See finish_function, genrtl_start_function, and declare_return_variable
6676 for other pieces of this optimization. */
6678 if (fn_returns_value_p && flag_elide_constructors)
6680 if (retval != NULL_TREE
6681 && (current_function_return_value == NULL_TREE
6682 || current_function_return_value == retval)
6683 && TREE_CODE (retval) == VAR_DECL
6684 && DECL_CONTEXT (retval) == current_function_decl
6685 && ! TREE_STATIC (retval)
6686 && (DECL_ALIGN (retval)
6687 >= DECL_ALIGN (DECL_RESULT (current_function_decl)))
6688 && same_type_p ((TYPE_MAIN_VARIANT
6689 (TREE_TYPE (retval))),
6690 (TYPE_MAIN_VARIANT
6691 (TREE_TYPE (TREE_TYPE (current_function_decl))))))
6692 current_function_return_value = retval;
6693 else
6694 current_function_return_value = error_mark_node;
6697 /* We don't need to do any conversions when there's nothing being
6698 returned. */
6699 if (!retval || retval == error_mark_node)
6700 return retval;
6702 /* Do any required conversions. */
6703 if (retval == result || DECL_CONSTRUCTOR_P (current_function_decl))
6704 /* No conversions are required. */
6706 else
6708 /* The type the function is declared to return. */
6709 tree functype = TREE_TYPE (TREE_TYPE (current_function_decl));
6711 /* First convert the value to the function's return type, then
6712 to the type of return value's location to handle the
6713 case that functype is smaller than the valtype. */
6714 retval = convert_for_initialization
6715 (NULL_TREE, functype, retval, LOOKUP_NORMAL|LOOKUP_ONLYCONVERTING,
6716 "return", NULL_TREE, 0);
6717 retval = convert (valtype, retval);
6719 /* If the conversion failed, treat this just like `return;'. */
6720 if (retval == error_mark_node)
6721 return retval;
6722 /* We can't initialize a register from a AGGR_INIT_EXPR. */
6723 else if (! current_function_returns_struct
6724 && TREE_CODE (retval) == TARGET_EXPR
6725 && TREE_CODE (TREE_OPERAND (retval, 1)) == AGGR_INIT_EXPR)
6726 retval = build (COMPOUND_EXPR, TREE_TYPE (retval), retval,
6727 TREE_OPERAND (retval, 0));
6728 else
6729 maybe_warn_about_returning_address_of_local (retval);
6732 /* Actually copy the value returned into the appropriate location. */
6733 if (retval && retval != result)
6734 retval = build (INIT_EXPR, TREE_TYPE (result), result, retval);
6736 return retval;
6740 /* Returns non-zero if the pointer-type FROM can be converted to the
6741 pointer-type TO via a qualification conversion. If CONSTP is -1,
6742 then we return non-zero if the pointers are similar, and the
6743 cv-qualification signature of FROM is a proper subset of that of TO.
6745 If CONSTP is positive, then all outer pointers have been
6746 const-qualified. */
6748 static int
6749 comp_ptr_ttypes_real (to, from, constp)
6750 tree to, from;
6751 int constp;
6753 int to_more_cv_qualified = 0;
6755 for (; ; to = TREE_TYPE (to), from = TREE_TYPE (from))
6757 if (TREE_CODE (to) != TREE_CODE (from))
6758 return 0;
6760 if (TREE_CODE (from) == OFFSET_TYPE
6761 && same_type_p (TYPE_OFFSET_BASETYPE (from),
6762 TYPE_OFFSET_BASETYPE (to)))
6763 continue;
6765 /* Const and volatile mean something different for function types,
6766 so the usual checks are not appropriate. */
6767 if (TREE_CODE (to) != FUNCTION_TYPE && TREE_CODE (to) != METHOD_TYPE)
6769 if (!at_least_as_qualified_p (to, from))
6770 return 0;
6772 if (!at_least_as_qualified_p (from, to))
6774 if (constp == 0)
6775 return 0;
6776 else
6777 ++to_more_cv_qualified;
6780 if (constp > 0)
6781 constp &= TYPE_READONLY (to);
6784 if (TREE_CODE (to) != POINTER_TYPE)
6785 return
6786 same_type_ignoring_top_level_qualifiers_p (to, from)
6787 && (constp >= 0 || to_more_cv_qualified);
6791 /* When comparing, say, char ** to char const **, this function takes the
6792 'char *' and 'char const *'. Do not pass non-pointer types to this
6793 function. */
6796 comp_ptr_ttypes (to, from)
6797 tree to, from;
6799 return comp_ptr_ttypes_real (to, from, 1);
6802 /* Returns 1 if to and from are (possibly multi-level) pointers to the same
6803 type or inheritance-related types, regardless of cv-quals. */
6806 ptr_reasonably_similar (to, from)
6807 tree to, from;
6809 for (; ; to = TREE_TYPE (to), from = TREE_TYPE (from))
6811 /* Any target type is similar enough to void. */
6812 if (TREE_CODE (to) == VOID_TYPE
6813 || TREE_CODE (from) == VOID_TYPE)
6814 return 1;
6816 if (TREE_CODE (to) != TREE_CODE (from))
6817 return 0;
6819 if (TREE_CODE (from) == OFFSET_TYPE
6820 && comptypes (TYPE_OFFSET_BASETYPE (to),
6821 TYPE_OFFSET_BASETYPE (from),
6822 COMPARE_BASE | COMPARE_RELAXED))
6823 continue;
6825 if (TREE_CODE (to) == INTEGER_TYPE
6826 && TYPE_PRECISION (to) == TYPE_PRECISION (from))
6827 return 1;
6829 if (TREE_CODE (to) == FUNCTION_TYPE)
6830 return 1;
6832 if (TREE_CODE (to) != POINTER_TYPE)
6833 return comptypes
6834 (TYPE_MAIN_VARIANT (to), TYPE_MAIN_VARIANT (from),
6835 COMPARE_BASE | COMPARE_RELAXED);
6839 /* Like comp_ptr_ttypes, for const_cast. */
6841 static int
6842 comp_ptr_ttypes_const (to, from)
6843 tree to, from;
6845 for (; ; to = TREE_TYPE (to), from = TREE_TYPE (from))
6847 if (TREE_CODE (to) != TREE_CODE (from))
6848 return 0;
6850 if (TREE_CODE (from) == OFFSET_TYPE
6851 && same_type_p (TYPE_OFFSET_BASETYPE (from),
6852 TYPE_OFFSET_BASETYPE (to)))
6853 continue;
6855 if (TREE_CODE (to) != POINTER_TYPE)
6856 return same_type_ignoring_top_level_qualifiers_p (to, from);
6860 /* Like comp_ptr_ttypes, for reinterpret_cast. */
6862 static int
6863 comp_ptr_ttypes_reinterpret (to, from)
6864 tree to, from;
6866 int constp = 1;
6868 for (; ; to = TREE_TYPE (to), from = TREE_TYPE (from))
6870 if (TREE_CODE (from) == OFFSET_TYPE)
6871 from = TREE_TYPE (from);
6872 if (TREE_CODE (to) == OFFSET_TYPE)
6873 to = TREE_TYPE (to);
6875 /* Const and volatile mean something different for function types,
6876 so the usual checks are not appropriate. */
6877 if (TREE_CODE (from) != FUNCTION_TYPE && TREE_CODE (from) != METHOD_TYPE
6878 && TREE_CODE (to) != FUNCTION_TYPE && TREE_CODE (to) != METHOD_TYPE)
6880 if (!at_least_as_qualified_p (to, from))
6881 return 0;
6883 if (! constp
6884 && !at_least_as_qualified_p (from, to))
6885 return 0;
6886 constp &= TYPE_READONLY (to);
6889 if (TREE_CODE (from) != POINTER_TYPE
6890 || TREE_CODE (to) != POINTER_TYPE)
6891 return 1;
6895 /* Returns the type-qualifier set corresponding to TYPE. */
6898 cp_type_quals (type)
6899 tree type;
6901 type = strip_array_types (type);
6902 return TYPE_QUALS (type);
6905 /* Returns non-zero if the TYPE contains a mutable member */
6908 cp_has_mutable_p (type)
6909 tree type;
6911 type = strip_array_types (type);
6913 return CLASS_TYPE_P (type) && CLASSTYPE_HAS_MUTABLE (type);
6916 /* Subroutine of casts_away_constness. Make T1 and T2 point at
6917 exemplar types such that casting T1 to T2 is casting away castness
6918 if and only if there is no implicit conversion from T1 to T2. */
6920 static void
6921 casts_away_constness_r (t1, t2)
6922 tree *t1;
6923 tree *t2;
6925 int quals1;
6926 int quals2;
6928 /* [expr.const.cast]
6930 For multi-level pointer to members and multi-level mixed pointers
6931 and pointers to members (conv.qual), the "member" aspect of a
6932 pointer to member level is ignored when determining if a const
6933 cv-qualifier has been cast away. */
6934 if (TYPE_PTRMEM_P (*t1))
6935 *t1 = build_pointer_type (TREE_TYPE (TREE_TYPE (*t1)));
6936 if (TYPE_PTRMEM_P (*t2))
6937 *t2 = build_pointer_type (TREE_TYPE (TREE_TYPE (*t2)));
6939 /* [expr.const.cast]
6941 For two pointer types:
6943 X1 is T1cv1,1 * ... cv1,N * where T1 is not a pointer type
6944 X2 is T2cv2,1 * ... cv2,M * where T2 is not a pointer type
6945 K is min(N,M)
6947 casting from X1 to X2 casts away constness if, for a non-pointer
6948 type T there does not exist an implicit conversion (clause
6949 _conv_) from:
6951 Tcv1,(N-K+1) * cv1,(N-K+2) * ... cv1,N *
6955 Tcv2,(M-K+1) * cv2,(M-K+2) * ... cv2,M *. */
6957 if (TREE_CODE (*t1) != POINTER_TYPE
6958 || TREE_CODE (*t2) != POINTER_TYPE)
6960 *t1 = cp_build_qualified_type (void_type_node,
6961 CP_TYPE_QUALS (*t1));
6962 *t2 = cp_build_qualified_type (void_type_node,
6963 CP_TYPE_QUALS (*t2));
6964 return;
6967 quals1 = CP_TYPE_QUALS (*t1);
6968 quals2 = CP_TYPE_QUALS (*t2);
6969 *t1 = TREE_TYPE (*t1);
6970 *t2 = TREE_TYPE (*t2);
6971 casts_away_constness_r (t1, t2);
6972 *t1 = build_pointer_type (*t1);
6973 *t2 = build_pointer_type (*t2);
6974 *t1 = cp_build_qualified_type (*t1, quals1);
6975 *t2 = cp_build_qualified_type (*t2, quals2);
6978 /* Returns non-zero if casting from TYPE1 to TYPE2 casts away
6979 constness. */
6981 static int
6982 casts_away_constness (t1, t2)
6983 tree t1;
6984 tree t2;
6986 if (TREE_CODE (t2) == REFERENCE_TYPE)
6988 /* [expr.const.cast]
6990 Casting from an lvalue of type T1 to an lvalue of type T2
6991 using a reference cast casts away constness if a cast from an
6992 rvalue of type "pointer to T1" to the type "pointer to T2"
6993 casts away constness. */
6994 t1 = (TREE_CODE (t1) == REFERENCE_TYPE
6995 ? TREE_TYPE (t1) : t1);
6996 return casts_away_constness (build_pointer_type (t1),
6997 build_pointer_type (TREE_TYPE (t2)));
7000 if (TYPE_PTRMEM_P (t1) && TYPE_PTRMEM_P (t2))
7001 /* [expr.const.cast]
7003 Casting from an rvalue of type "pointer to data member of X
7004 of type T1" to the type "pointer to data member of Y of type
7005 T2" casts away constness if a cast from an rvalue of type
7006 "poitner to T1" to the type "pointer to T2" casts away
7007 constness. */
7008 return casts_away_constness
7009 (build_pointer_type (TREE_TYPE (TREE_TYPE (t1))),
7010 build_pointer_type (TREE_TYPE (TREE_TYPE (t2))));
7012 /* Casting away constness is only something that makes sense for
7013 pointer or reference types. */
7014 if (TREE_CODE (t1) != POINTER_TYPE
7015 || TREE_CODE (t2) != POINTER_TYPE)
7016 return 0;
7018 /* Top-level qualifiers don't matter. */
7019 t1 = TYPE_MAIN_VARIANT (t1);
7020 t2 = TYPE_MAIN_VARIANT (t2);
7021 casts_away_constness_r (&t1, &t2);
7022 if (!can_convert (t2, t1))
7023 return 1;
7025 return 0;
7028 /* Returns TYPE with its cv qualifiers removed
7029 TYPE is T cv* .. *cv where T is not a pointer type,
7030 returns T * .. *. (If T is an array type, then the cv qualifiers
7031 above are those of the array members.) */
7033 static tree
7034 strip_all_pointer_quals (type)
7035 tree type;
7037 if (TREE_CODE (type) == POINTER_TYPE)
7038 return build_pointer_type (strip_all_pointer_quals (TREE_TYPE (type)));
7039 else if (TREE_CODE (type) == OFFSET_TYPE)
7040 return build_offset_type (TYPE_OFFSET_BASETYPE (type),
7041 strip_all_pointer_quals (TREE_TYPE (type)));
7042 else
7043 return TYPE_MAIN_VARIANT (type);