* doc/gcc.texi, doc/install.texi, doc/invoke.texi: Remove trailing
[official-gcc.git] / gcc / cp / typeck.c
blobdb1044adeee688cc4b45d6c9d43421ea1ff97c85
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 = (*target.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 = (*target.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 = (*target.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 /* ??? COMP_TYPE_ATTRIBUTES is currently useless for variables as each
1000 attribute is its own main variant (`val' will remain 0). */
1001 #ifndef COMP_TYPE_ATTRIBUTES
1002 #define COMP_TYPE_ATTRIBUTES(t1,t2) 1
1003 #endif
1005 if (strict & COMPARE_NO_ATTRIBUTES)
1006 attrval = 1;
1007 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
1008 else if (! (attrval = COMP_TYPE_ATTRIBUTES (t1, t2)))
1009 return 0;
1011 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
1012 val = 0;
1014 switch (TREE_CODE (t1))
1016 case TEMPLATE_TEMPLATE_PARM:
1017 case BOUND_TEMPLATE_TEMPLATE_PARM:
1018 if (TEMPLATE_TYPE_IDX (t1) != TEMPLATE_TYPE_IDX (t2)
1019 || TEMPLATE_TYPE_LEVEL (t1) != TEMPLATE_TYPE_LEVEL (t2))
1020 return 0;
1021 if (! comp_template_parms
1022 (DECL_TEMPLATE_PARMS (TEMPLATE_TEMPLATE_PARM_TEMPLATE_DECL (t1)),
1023 DECL_TEMPLATE_PARMS (TEMPLATE_TEMPLATE_PARM_TEMPLATE_DECL (t2))))
1024 return 0;
1025 if (TREE_CODE (t1) == TEMPLATE_TEMPLATE_PARM)
1026 return 1;
1027 /* Don't check inheritance. */
1028 strict = COMPARE_STRICT;
1029 /* fall through */
1031 case RECORD_TYPE:
1032 case UNION_TYPE:
1033 if (TYPE_TEMPLATE_INFO (t1) && TYPE_TEMPLATE_INFO (t2)
1034 && (TYPE_TI_TEMPLATE (t1) == TYPE_TI_TEMPLATE (t2)
1035 || TREE_CODE (t1) == BOUND_TEMPLATE_TEMPLATE_PARM))
1036 val = comp_template_args (TYPE_TI_ARGS (t1),
1037 TYPE_TI_ARGS (t2));
1038 look_hard:
1039 if ((strict & COMPARE_BASE) && DERIVED_FROM_P (t1, t2))
1040 val = 1;
1041 else if ((strict & COMPARE_RELAXED) && DERIVED_FROM_P (t2, t1))
1042 val = 1;
1043 break;
1045 case OFFSET_TYPE:
1046 val = (comptypes (build_pointer_type (TYPE_OFFSET_BASETYPE (t1)),
1047 build_pointer_type (TYPE_OFFSET_BASETYPE (t2)), strict)
1048 && comptypes (TREE_TYPE (t1), TREE_TYPE (t2), strict));
1049 break;
1051 case METHOD_TYPE:
1052 if (! comp_except_specs (TYPE_RAISES_EXCEPTIONS (t1),
1053 TYPE_RAISES_EXCEPTIONS (t2), 1))
1054 return 0;
1056 /* This case is anti-symmetrical!
1057 One can pass a base member (or member function)
1058 to something expecting a derived member (or member function),
1059 but not vice-versa! */
1061 val = (comptypes (TREE_TYPE (t1), TREE_TYPE (t2), strict)
1062 && compparms (TYPE_ARG_TYPES (t1), TYPE_ARG_TYPES (t2)));
1063 break;
1065 case POINTER_TYPE:
1066 case REFERENCE_TYPE:
1067 t1 = TREE_TYPE (t1);
1068 t2 = TREE_TYPE (t2);
1069 /* first, check whether the referred types match with the
1070 required level of strictness */
1071 val = comptypes (t1, t2, strict);
1072 if (val)
1073 break;
1074 if (TREE_CODE (t1) == RECORD_TYPE
1075 && TREE_CODE (t2) == RECORD_TYPE)
1076 goto look_hard;
1077 break;
1079 case FUNCTION_TYPE:
1080 if (! comp_except_specs (TYPE_RAISES_EXCEPTIONS (t1),
1081 TYPE_RAISES_EXCEPTIONS (t2), 1))
1082 return 0;
1084 val = ((TREE_TYPE (t1) == TREE_TYPE (t2)
1085 || comptypes (TREE_TYPE (t1), TREE_TYPE (t2), strict))
1086 && compparms (TYPE_ARG_TYPES (t1), TYPE_ARG_TYPES (t2)));
1087 break;
1089 case ARRAY_TYPE:
1090 /* Target types must match incl. qualifiers. We use ORIG_STRICT
1091 here since this is the one place where
1092 COMPARE_REDECLARATION should be used. */
1093 val = comp_array_types (comptypes, t1, t2, orig_strict);
1094 break;
1096 case TEMPLATE_TYPE_PARM:
1097 return TEMPLATE_TYPE_IDX (t1) == TEMPLATE_TYPE_IDX (t2)
1098 && TEMPLATE_TYPE_LEVEL (t1) == TEMPLATE_TYPE_LEVEL (t2);
1100 case TYPENAME_TYPE:
1101 if (cp_tree_equal (TYPENAME_TYPE_FULLNAME (t1),
1102 TYPENAME_TYPE_FULLNAME (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 (SIZEOF_EXPR, sizetype, 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 (SIZEOF_EXPR, sizetype, 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 /* Implement the __alignof keyword: Return the minimum required
1681 alignment of TYPE, measured in bytes. */
1683 tree
1684 c_alignof (type)
1685 tree type;
1687 enum tree_code code = TREE_CODE (type);
1688 tree t;
1690 if (processing_template_decl)
1691 return build_min (ALIGNOF_EXPR, sizetype, type);
1693 if (code == FUNCTION_TYPE || code == METHOD_TYPE)
1694 t = size_int (FUNCTION_BOUNDARY / BITS_PER_UNIT);
1695 else if (code == VOID_TYPE || code == ERROR_MARK)
1696 t = size_one_node;
1697 else
1699 /* Similar to sizeof, __alignof applies to the referant. */
1700 if (code == REFERENCE_TYPE)
1701 type = TREE_TYPE (type);
1703 t = size_int (TYPE_ALIGN (type) / BITS_PER_UNIT);
1706 return fold (build1 (NOP_EXPR, c_size_type_node, t));
1709 /* Perform the array-to-pointer and function-to-pointer conversions
1710 for EXP.
1712 In addition, references are converted to lvalues and manifest
1713 constants are replaced by their values. */
1715 tree
1716 decay_conversion (exp)
1717 tree exp;
1719 register tree type;
1720 register enum tree_code code;
1722 if (TREE_CODE (exp) == OFFSET_REF)
1723 exp = resolve_offset_ref (exp);
1725 type = TREE_TYPE (exp);
1726 code = TREE_CODE (type);
1728 if (code == REFERENCE_TYPE)
1730 exp = convert_from_reference (exp);
1731 type = TREE_TYPE (exp);
1732 code = TREE_CODE (type);
1735 if (type == error_mark_node)
1736 return error_mark_node;
1738 /* Constants can be used directly unless they're not loadable. */
1739 if (TREE_CODE (exp) == CONST_DECL)
1740 exp = DECL_INITIAL (exp);
1741 /* Replace a nonvolatile const static variable with its value. We
1742 don't do this for arrays, though; we want the address of the
1743 first element of the array, not the address of the first element
1744 of its initializing constant. */
1745 else if (code != ARRAY_TYPE)
1747 exp = decl_constant_value (exp);
1748 type = TREE_TYPE (exp);
1751 /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
1752 Leave such NOP_EXPRs, since RHS is being used in non-lvalue context. */
1754 if (code == VOID_TYPE)
1756 error ("void value not ignored as it ought to be");
1757 return error_mark_node;
1759 if (code == METHOD_TYPE)
1760 my_friendly_abort (990506);
1761 if (code == FUNCTION_TYPE || is_overloaded_fn (exp))
1762 return build_unary_op (ADDR_EXPR, exp, 0);
1763 if (code == ARRAY_TYPE)
1765 register tree adr;
1766 tree ptrtype;
1768 if (TREE_CODE (exp) == INDIRECT_REF)
1770 /* Stripping away the INDIRECT_REF is not the right
1771 thing to do for references... */
1772 tree inner = TREE_OPERAND (exp, 0);
1773 if (TREE_CODE (TREE_TYPE (inner)) == REFERENCE_TYPE)
1775 inner = build1 (CONVERT_EXPR,
1776 build_pointer_type (TREE_TYPE
1777 (TREE_TYPE (inner))),
1778 inner);
1779 TREE_CONSTANT (inner) = TREE_CONSTANT (TREE_OPERAND (inner, 0));
1781 return cp_convert (build_pointer_type (TREE_TYPE (type)), inner);
1784 if (TREE_CODE (exp) == COMPOUND_EXPR)
1786 tree op1 = decay_conversion (TREE_OPERAND (exp, 1));
1787 return build (COMPOUND_EXPR, TREE_TYPE (op1),
1788 TREE_OPERAND (exp, 0), op1);
1791 if (!lvalue_p (exp)
1792 && ! (TREE_CODE (exp) == CONSTRUCTOR && TREE_STATIC (exp)))
1794 error ("invalid use of non-lvalue array");
1795 return error_mark_node;
1798 ptrtype = build_pointer_type (TREE_TYPE (type));
1800 if (TREE_CODE (exp) == VAR_DECL)
1802 /* ??? This is not really quite correct
1803 in that the type of the operand of ADDR_EXPR
1804 is not the target type of the type of the ADDR_EXPR itself.
1805 Question is, can this lossage be avoided? */
1806 adr = build1 (ADDR_EXPR, ptrtype, exp);
1807 if (mark_addressable (exp) == 0)
1808 return error_mark_node;
1809 TREE_CONSTANT (adr) = staticp (exp);
1810 TREE_SIDE_EFFECTS (adr) = 0; /* Default would be, same as EXP. */
1811 return adr;
1813 /* This way is better for a COMPONENT_REF since it can
1814 simplify the offset for a component. */
1815 adr = build_unary_op (ADDR_EXPR, exp, 1);
1816 return cp_convert (ptrtype, adr);
1819 /* [basic.lval]: Class rvalues can have cv-qualified types; non-class
1820 rvalues always have cv-unqualified types. */
1821 if (! CLASS_TYPE_P (type))
1822 exp = cp_convert (TYPE_MAIN_VARIANT (type), exp);
1824 return exp;
1827 tree
1828 default_conversion (exp)
1829 tree exp;
1831 tree type;
1832 enum tree_code code;
1834 exp = decay_conversion (exp);
1836 type = TREE_TYPE (exp);
1837 code = TREE_CODE (type);
1839 if (INTEGRAL_CODE_P (code))
1841 tree t = type_promotes_to (type);
1842 if (t != type)
1843 return cp_convert (t, exp);
1846 return exp;
1849 /* Take the address of an inline function without setting TREE_ADDRESSABLE
1850 or TREE_USED. */
1852 tree
1853 inline_conversion (exp)
1854 tree exp;
1856 if (TREE_CODE (exp) == FUNCTION_DECL)
1857 exp = build1 (ADDR_EXPR, build_pointer_type (TREE_TYPE (exp)), exp);
1859 return exp;
1862 /* Returns nonzero iff exp is a STRING_CST or the result of applying
1863 decay_conversion to one. */
1866 string_conv_p (totype, exp, warn)
1867 tree totype, exp;
1868 int warn;
1870 tree t;
1872 if (! flag_const_strings || TREE_CODE (totype) != POINTER_TYPE)
1873 return 0;
1875 t = TREE_TYPE (totype);
1876 if (!same_type_p (t, char_type_node)
1877 && !same_type_p (t, wchar_type_node))
1878 return 0;
1880 if (TREE_CODE (exp) == STRING_CST)
1882 /* Make sure that we don't try to convert between char and wchar_t. */
1883 if (!same_type_p (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (exp))), t))
1884 return 0;
1886 else
1888 /* Is this a string constant which has decayed to 'const char *'? */
1889 t = build_pointer_type (build_qualified_type (t, TYPE_QUAL_CONST));
1890 if (!same_type_p (TREE_TYPE (exp), t))
1891 return 0;
1892 STRIP_NOPS (exp);
1893 if (TREE_CODE (exp) != ADDR_EXPR
1894 || TREE_CODE (TREE_OPERAND (exp, 0)) != STRING_CST)
1895 return 0;
1898 /* This warning is not very useful, as it complains about printf. */
1899 if (warn && warn_write_strings)
1900 cp_warning ("deprecated conversion from string constant to `%T'", totype);
1902 return 1;
1905 tree
1906 build_object_ref (datum, basetype, field)
1907 tree datum, basetype, field;
1909 tree dtype;
1910 if (datum == error_mark_node)
1911 return error_mark_node;
1913 dtype = TREE_TYPE (datum);
1914 if (TREE_CODE (dtype) == REFERENCE_TYPE)
1915 dtype = TREE_TYPE (dtype);
1916 if (! IS_AGGR_TYPE_CODE (TREE_CODE (dtype)))
1918 cp_error ("request for member `%T::%D' in expression of non-aggregate type `%T'",
1919 basetype, field, dtype);
1920 return error_mark_node;
1922 else if (is_aggr_type (basetype, 1))
1924 tree binfo = binfo_or_else (basetype, dtype);
1925 if (binfo)
1926 return build_x_component_ref (build_scoped_ref (datum, basetype),
1927 field, binfo, 1);
1929 return error_mark_node;
1932 /* Like `build_component_ref, but uses an already found field, and converts
1933 from a reference. Must compute access for current_class_ref.
1934 Otherwise, ok. */
1936 tree
1937 build_component_ref_1 (datum, field, protect)
1938 tree datum, field;
1939 int protect;
1941 return convert_from_reference
1942 (build_component_ref (datum, field, NULL_TREE, protect));
1945 /* Given a COND_EXPR, MIN_EXPR, or MAX_EXPR in T, return it in a form that we
1946 can, for example, use as an lvalue. This code used to be in
1947 unary_complex_lvalue, but we needed it to deal with `a = (d == c) ? b : c'
1948 expressions, where we're dealing with aggregates. But now it's again only
1949 called from unary_complex_lvalue. The case (in particular) that led to
1950 this was with CODE == ADDR_EXPR, since it's not an lvalue when we'd
1951 get it there. */
1953 static tree
1954 rationalize_conditional_expr (code, t)
1955 enum tree_code code;
1956 tree t;
1958 /* For MIN_EXPR or MAX_EXPR, fold-const.c has arranged things so that
1959 the first operand is always the one to be used if both operands
1960 are equal, so we know what conditional expression this used to be. */
1961 if (TREE_CODE (t) == MIN_EXPR || TREE_CODE (t) == MAX_EXPR)
1963 return
1964 build_conditional_expr (build_x_binary_op ((TREE_CODE (t) == MIN_EXPR
1965 ? LE_EXPR : GE_EXPR),
1966 TREE_OPERAND (t, 0),
1967 TREE_OPERAND (t, 1)),
1968 build_unary_op (code, TREE_OPERAND (t, 0), 0),
1969 build_unary_op (code, TREE_OPERAND (t, 1), 0));
1972 return
1973 build_conditional_expr (TREE_OPERAND (t, 0),
1974 build_unary_op (code, TREE_OPERAND (t, 1), 0),
1975 build_unary_op (code, TREE_OPERAND (t, 2), 0));
1978 /* Given the TYPE of an anonymous union field inside T, return the
1979 FIELD_DECL for the field. If not found return NULL_TREE. Because
1980 anonymous unions can nest, we must also search all anonymous unions
1981 that are directly reachable. */
1983 static tree
1984 lookup_anon_field (t, type)
1985 tree t, type;
1987 tree field;
1989 for (field = TYPE_FIELDS (t); field; field = TREE_CHAIN (field))
1991 if (TREE_STATIC (field))
1992 continue;
1993 if (TREE_CODE (field) != FIELD_DECL)
1994 continue;
1996 /* If we find it directly, return the field. */
1997 if (DECL_NAME (field) == NULL_TREE
1998 && type == TYPE_MAIN_VARIANT (TREE_TYPE (field)))
2000 return field;
2003 /* Otherwise, it could be nested, search harder. */
2004 if (DECL_NAME (field) == NULL_TREE
2005 && ANON_AGGR_TYPE_P (TREE_TYPE (field)))
2007 tree subfield = lookup_anon_field (TREE_TYPE (field), type);
2008 if (subfield)
2009 return subfield;
2012 return NULL_TREE;
2015 /* Build a COMPONENT_REF for a given DATUM, and it's member COMPONENT.
2016 COMPONENT can be an IDENTIFIER_NODE that is the name of the member
2017 that we are interested in, or it can be a FIELD_DECL. */
2019 tree
2020 build_component_ref (datum, component, basetype_path, protect)
2021 tree datum, component, basetype_path;
2022 int protect;
2024 register tree basetype;
2025 register enum tree_code code;
2026 register tree field = NULL;
2027 register tree ref;
2028 tree field_type;
2029 int type_quals;
2031 if (processing_template_decl)
2032 return build_min_nt (COMPONENT_REF, datum, component);
2034 if (datum == error_mark_node
2035 || TREE_TYPE (datum) == error_mark_node)
2036 return error_mark_node;
2038 /* BASETYPE holds the type of the class containing the COMPONENT. */
2039 basetype = TYPE_MAIN_VARIANT (TREE_TYPE (datum));
2041 /* If DATUM is a COMPOUND_EXPR or COND_EXPR, move our reference
2042 inside it. */
2043 switch (TREE_CODE (datum))
2045 case COMPOUND_EXPR:
2047 tree value = build_component_ref (TREE_OPERAND (datum, 1), component,
2048 basetype_path, protect);
2049 return build (COMPOUND_EXPR, TREE_TYPE (value),
2050 TREE_OPERAND (datum, 0), value);
2052 case COND_EXPR:
2053 return build_conditional_expr
2054 (TREE_OPERAND (datum, 0),
2055 build_component_ref (TREE_OPERAND (datum, 1), component,
2056 basetype_path, protect),
2057 build_component_ref (TREE_OPERAND (datum, 2), component,
2058 basetype_path, protect));
2060 case TEMPLATE_DECL:
2061 cp_error ("invalid use of %D", datum);
2062 datum = error_mark_node;
2063 break;
2065 default:
2066 break;
2069 code = TREE_CODE (basetype);
2071 if (code == REFERENCE_TYPE)
2073 datum = convert_from_reference (datum);
2074 basetype = TYPE_MAIN_VARIANT (TREE_TYPE (datum));
2075 code = TREE_CODE (basetype);
2077 if (TREE_CODE (datum) == OFFSET_REF)
2079 datum = resolve_offset_ref (datum);
2080 basetype = TYPE_MAIN_VARIANT (TREE_TYPE (datum));
2081 code = TREE_CODE (basetype);
2084 /* First, see if there is a field or component with name COMPONENT. */
2085 if (TREE_CODE (component) == TREE_LIST)
2087 /* I could not trigger this code. MvL */
2088 my_friendly_abort (980326);
2089 #ifdef DEAD
2090 my_friendly_assert (!(TREE_CHAIN (component) == NULL_TREE
2091 && DECL_CHAIN (TREE_VALUE (component)) == NULL_TREE), 309);
2092 #endif
2093 return build (COMPONENT_REF, TREE_TYPE (component), datum, component);
2096 if (! IS_AGGR_TYPE_CODE (code))
2098 if (code != ERROR_MARK)
2099 cp_error ("request for member `%D' in `%E', which is of non-aggregate type `%T'",
2100 component, datum, basetype);
2101 return error_mark_node;
2104 if (!complete_type_or_else (basetype, datum))
2105 return error_mark_node;
2107 if (TREE_CODE (component) == BIT_NOT_EXPR)
2109 if (TYPE_IDENTIFIER (basetype) != TREE_OPERAND (component, 0))
2111 cp_error ("destructor specifier `%T::~%T' must have matching names",
2112 basetype, TREE_OPERAND (component, 0));
2113 return error_mark_node;
2115 if (! TYPE_HAS_DESTRUCTOR (basetype))
2117 cp_error ("type `%T' has no destructor", basetype);
2118 return error_mark_node;
2120 return TREE_VEC_ELT (CLASSTYPE_METHOD_VEC (basetype), 1);
2123 /* Look up component name in the structure type definition. */
2124 if (TYPE_VFIELD (basetype)
2125 && DECL_NAME (TYPE_VFIELD (basetype)) == component)
2126 /* Special-case this because if we use normal lookups in an ambiguous
2127 hierarchy, the compiler will abort (because vptr lookups are
2128 not supposed to be ambiguous. */
2129 field = TYPE_VFIELD (basetype);
2130 else if (TREE_CODE (component) == FIELD_DECL)
2131 field = component;
2132 else if (TREE_CODE (component) == TYPE_DECL)
2134 cp_error ("invalid use of type decl `%#D' as expression", component);
2135 return error_mark_node;
2137 else if (TREE_CODE (component) == TEMPLATE_DECL)
2139 cp_error ("invalid use of template `%#D' as expression", component);
2140 return error_mark_node;
2142 else
2144 tree name = component;
2145 if (TREE_CODE (component) == VAR_DECL)
2146 name = DECL_NAME (component);
2147 if (TREE_CODE (component) == NAMESPACE_DECL)
2148 /* Source is in error, but produce a sensible diagnostic. */
2149 name = DECL_NAME (component);
2150 if (basetype_path == NULL_TREE)
2151 basetype_path = TYPE_BINFO (basetype);
2152 field = lookup_field (basetype_path, name,
2153 protect && !VFIELD_NAME_P (name), 0);
2154 if (field == error_mark_node)
2155 return error_mark_node;
2157 if (field == NULL_TREE)
2159 /* Not found as a data field, look for it as a method. If found,
2160 then if this is the only possible one, return it, else
2161 report ambiguity error. */
2162 tree fndecls = lookup_fnfields (basetype_path, name, 1);
2163 if (fndecls == error_mark_node)
2164 return error_mark_node;
2165 if (fndecls)
2167 /* If the function is unique and static, we can resolve it
2168 now. Otherwise, we have to wait and see what context it is
2169 used in; a component_ref involving a non-static member
2170 function can only be used in a call (expr.ref). */
2172 if (TREE_CHAIN (fndecls) == NULL_TREE
2173 && TREE_CODE (TREE_VALUE (fndecls)) == FUNCTION_DECL)
2175 if (DECL_STATIC_FUNCTION_P (TREE_VALUE (fndecls)))
2177 tree fndecl = TREE_VALUE (fndecls);
2178 enforce_access (basetype_path, fndecl);
2179 mark_used (fndecl);
2180 return fndecl;
2182 else
2184 /* A unique non-static member function. Other parts
2185 of the compiler expect something with
2186 unknown_type_node to be really overloaded, so
2187 let's oblige. */
2188 TREE_VALUE (fndecls)
2189 = ovl_cons (TREE_VALUE (fndecls), NULL_TREE);
2193 ref = build (COMPONENT_REF, unknown_type_node,
2194 datum, TREE_VALUE (fndecls));
2195 return ref;
2198 cp_error ("`%#T' has no member named `%D'", basetype, name);
2199 return error_mark_node;
2201 else if (TREE_TYPE (field) == error_mark_node)
2202 return error_mark_node;
2204 if (TREE_CODE (field) != FIELD_DECL)
2206 if (TREE_CODE (field) == TYPE_DECL)
2207 cp_pedwarn ("invalid use of type decl `%#D' as expression", field);
2208 else if (DECL_RTL (field) != 0)
2209 mark_used (field);
2210 else
2211 TREE_USED (field) = 1;
2213 /* Do evaluate the object when accessing a static member. */
2214 if (TREE_SIDE_EFFECTS (datum))
2215 field = build (COMPOUND_EXPR, TREE_TYPE (field), datum, field);
2217 return field;
2221 /* See if we have to do any conversions so that we pick up the field from the
2222 right context. */
2223 if (DECL_FIELD_CONTEXT (field) != basetype)
2225 tree context = DECL_FIELD_CONTEXT (field);
2226 tree base = context;
2227 while (!same_type_p (base, basetype) && TYPE_NAME (base)
2228 && ANON_AGGR_TYPE_P (base))
2229 base = TYPE_CONTEXT (base);
2231 /* Handle base classes here... */
2232 if (base != basetype && TYPE_BASE_CONVS_MAY_REQUIRE_CODE_P (basetype))
2234 tree addr = build_unary_op (ADDR_EXPR, datum, 0);
2235 if (integer_zerop (addr))
2237 error ("invalid reference to NULL ptr, use ptr-to-member instead");
2238 return error_mark_node;
2240 if (VBASE_NAME_P (DECL_NAME (field)))
2242 /* It doesn't matter which vbase pointer we grab, just
2243 find one of them. */
2244 tree binfo = get_binfo (base,
2245 TREE_TYPE (TREE_TYPE (addr)), 0);
2246 addr = convert_pointer_to_real (binfo, addr);
2248 else
2249 addr = convert_pointer_to (base, addr);
2250 datum = build_indirect_ref (addr, NULL);
2251 if (datum == error_mark_node)
2252 return error_mark_node;
2254 basetype = base;
2256 /* Handle things from anon unions here... */
2257 if (TYPE_NAME (context) && ANON_AGGR_TYPE_P (context))
2259 tree subfield = lookup_anon_field (basetype, context);
2260 tree subdatum = build_component_ref (datum, subfield,
2261 basetype_path, protect);
2262 return build_component_ref (subdatum, field, basetype_path, protect);
2266 /* Compute the type of the field, as described in [expr.ref]. */
2267 type_quals = TYPE_UNQUALIFIED;
2268 field_type = TREE_TYPE (field);
2269 if (TREE_CODE (field_type) == REFERENCE_TYPE)
2270 /* The standard says that the type of the result should be the
2271 type referred to by the reference. But for now, at least, we
2272 do the conversion from reference type later. */
2274 else
2276 type_quals = (CP_TYPE_QUALS (field_type)
2277 | CP_TYPE_QUALS (TREE_TYPE (datum)));
2279 /* A field is const (volatile) if the enclosing object, or the
2280 field itself, is const (volatile). But, a mutable field is
2281 not const, even within a const object. */
2282 if (DECL_MUTABLE_P (field))
2283 type_quals &= ~TYPE_QUAL_CONST;
2284 field_type = cp_build_qualified_type (field_type, type_quals);
2287 ref = fold (build (COMPONENT_REF, field_type, datum, field));
2289 /* Mark the expression const or volatile, as appropriate. Even
2290 though we've dealt with the type above, we still have to mark the
2291 expression itself. */
2292 if (type_quals & TYPE_QUAL_CONST)
2293 TREE_READONLY (ref) = 1;
2294 else if (type_quals & TYPE_QUAL_VOLATILE)
2295 TREE_THIS_VOLATILE (ref) = 1;
2297 return ref;
2300 /* Variant of build_component_ref for use in expressions, which should
2301 never have REFERENCE_TYPE. */
2303 tree
2304 build_x_component_ref (datum, component, basetype_path, protect)
2305 tree datum, component, basetype_path;
2306 int protect;
2308 tree t = build_component_ref (datum, component, basetype_path, protect);
2310 if (! processing_template_decl)
2311 t = convert_from_reference (t);
2313 return t;
2316 /* Given an expression PTR for a pointer, return an expression
2317 for the value pointed to.
2318 ERRORSTRING is the name of the operator to appear in error messages.
2320 This function may need to overload OPERATOR_FNNAME.
2321 Must also handle REFERENCE_TYPEs for C++. */
2323 tree
2324 build_x_indirect_ref (ptr, errorstring)
2325 tree ptr;
2326 const char *errorstring;
2328 tree rval;
2330 if (processing_template_decl)
2331 return build_min_nt (INDIRECT_REF, ptr);
2333 rval = build_opfncall (INDIRECT_REF, LOOKUP_NORMAL, ptr, NULL_TREE,
2334 NULL_TREE);
2335 if (rval)
2336 return rval;
2337 return build_indirect_ref (ptr, errorstring);
2340 tree
2341 build_indirect_ref (ptr, errorstring)
2342 tree ptr;
2343 const char *errorstring;
2345 register tree pointer, type;
2347 if (ptr == error_mark_node)
2348 return error_mark_node;
2350 if (ptr == current_class_ptr)
2351 return current_class_ref;
2353 pointer = (TREE_CODE (TREE_TYPE (ptr)) == REFERENCE_TYPE
2354 ? ptr : default_conversion (ptr));
2355 type = TREE_TYPE (pointer);
2357 if (TYPE_PTR_P (type) || TREE_CODE (type) == REFERENCE_TYPE)
2359 /* [expr.unary.op]
2361 If the type of the expression is "pointer to T," the type
2362 of the result is "T."
2364 We must use the canonical variant because certain parts of
2365 the back end, like fold, do pointer comparisons between
2366 types. */
2367 tree t = canonical_type_variant (TREE_TYPE (type));
2369 if (VOID_TYPE_P (t))
2371 /* A pointer to incomplete type (other than cv void) can be
2372 dereferenced [expr.unary.op]/1 */
2373 cp_error ("`%T' is not a pointer-to-object type", type);
2374 return error_mark_node;
2376 else if (TREE_CODE (pointer) == ADDR_EXPR
2377 && !flag_volatile
2378 && same_type_p (t, TREE_TYPE (TREE_OPERAND (pointer, 0))))
2379 /* The POINTER was something like `&x'. We simplify `*&x' to
2380 `x'. */
2381 return TREE_OPERAND (pointer, 0);
2382 else
2384 tree ref = build1 (INDIRECT_REF, t, pointer);
2386 /* We *must* set TREE_READONLY when dereferencing a pointer to const,
2387 so that we get the proper error message if the result is used
2388 to assign to. Also, &* is supposed to be a no-op. */
2389 TREE_READONLY (ref) = CP_TYPE_CONST_P (t);
2390 TREE_THIS_VOLATILE (ref) = CP_TYPE_VOLATILE_P (t);
2391 TREE_SIDE_EFFECTS (ref)
2392 = (TREE_THIS_VOLATILE (ref) || TREE_SIDE_EFFECTS (pointer)
2393 || flag_volatile);
2394 return ref;
2397 /* `pointer' won't be an error_mark_node if we were given a
2398 pointer to member, so it's cool to check for this here. */
2399 else if (TYPE_PTRMEM_P (type) || TYPE_PTRMEMFUNC_P (type))
2400 error ("invalid use of `%s' on pointer to member", errorstring);
2401 else if (pointer != error_mark_node)
2403 if (errorstring)
2404 error ("invalid type argument of `%s'", errorstring);
2405 else
2406 error ("invalid type argument");
2408 return error_mark_node;
2411 /* This handles expressions of the form "a[i]", which denotes
2412 an array reference.
2414 This is logically equivalent in C to *(a+i), but we may do it differently.
2415 If A is a variable or a member, we generate a primitive ARRAY_REF.
2416 This avoids forcing the array out of registers, and can work on
2417 arrays that are not lvalues (for example, members of structures returned
2418 by functions).
2420 If INDEX is of some user-defined type, it must be converted to
2421 integer type. Otherwise, to make a compatible PLUS_EXPR, it
2422 will inherit the type of the array, which will be some pointer type. */
2424 tree
2425 build_array_ref (array, idx)
2426 tree array, idx;
2428 if (idx == 0)
2430 error ("subscript missing in array reference");
2431 return error_mark_node;
2434 if (TREE_TYPE (array) == error_mark_node
2435 || TREE_TYPE (idx) == error_mark_node)
2436 return error_mark_node;
2438 /* If ARRAY is a COMPOUND_EXPR or COND_EXPR, move our reference
2439 inside it. */
2440 switch (TREE_CODE (array))
2442 case COMPOUND_EXPR:
2444 tree value = build_array_ref (TREE_OPERAND (array, 1), idx);
2445 return build (COMPOUND_EXPR, TREE_TYPE (value),
2446 TREE_OPERAND (array, 0), value);
2449 case COND_EXPR:
2450 return build_conditional_expr
2451 (TREE_OPERAND (array, 0),
2452 build_array_ref (TREE_OPERAND (array, 1), idx),
2453 build_array_ref (TREE_OPERAND (array, 2), idx));
2455 default:
2456 break;
2459 if (TREE_CODE (TREE_TYPE (array)) == ARRAY_TYPE
2460 && TREE_CODE (array) != INDIRECT_REF)
2462 tree rval, type;
2464 /* Subscripting with type char is likely to lose
2465 on a machine where chars are signed.
2466 So warn on any machine, but optionally.
2467 Don't warn for unsigned char since that type is safe.
2468 Don't warn for signed char because anyone who uses that
2469 must have done so deliberately. */
2470 if (warn_char_subscripts
2471 && TYPE_MAIN_VARIANT (TREE_TYPE (idx)) == char_type_node)
2472 warning ("array subscript has type `char'");
2474 /* Apply default promotions *after* noticing character types. */
2475 idx = default_conversion (idx);
2477 if (TREE_CODE (TREE_TYPE (idx)) != INTEGER_TYPE)
2479 error ("array subscript is not an integer");
2480 return error_mark_node;
2483 /* An array that is indexed by a non-constant
2484 cannot be stored in a register; we must be able to do
2485 address arithmetic on its address.
2486 Likewise an array of elements of variable size. */
2487 if (TREE_CODE (idx) != INTEGER_CST
2488 || (COMPLETE_TYPE_P (TREE_TYPE (TREE_TYPE (array)))
2489 && (TREE_CODE (TYPE_SIZE (TREE_TYPE (TREE_TYPE (array))))
2490 != INTEGER_CST)))
2492 if (mark_addressable (array) == 0)
2493 return error_mark_node;
2496 /* An array that is indexed by a constant value which is not within
2497 the array bounds cannot be stored in a register either; because we
2498 would get a crash in store_bit_field/extract_bit_field when trying
2499 to access a non-existent part of the register. */
2500 if (TREE_CODE (idx) == INTEGER_CST
2501 && TYPE_VALUES (TREE_TYPE (array))
2502 && ! int_fits_type_p (idx, TYPE_VALUES (TREE_TYPE (array))))
2504 if (mark_addressable (array) == 0)
2505 return error_mark_node;
2508 if (pedantic && !lvalue_p (array))
2509 pedwarn ("ISO C++ forbids subscripting non-lvalue array");
2511 /* Note in C++ it is valid to subscript a `register' array, since
2512 it is valid to take the address of something with that
2513 storage specification. */
2514 if (extra_warnings)
2516 tree foo = array;
2517 while (TREE_CODE (foo) == COMPONENT_REF)
2518 foo = TREE_OPERAND (foo, 0);
2519 if (TREE_CODE (foo) == VAR_DECL && DECL_REGISTER (foo))
2520 warning ("subscripting array declared `register'");
2523 type = TREE_TYPE (TREE_TYPE (array));
2524 rval = build (ARRAY_REF, type, array, idx);
2525 /* Array ref is const/volatile if the array elements are
2526 or if the array is.. */
2527 TREE_READONLY (rval)
2528 |= (CP_TYPE_CONST_P (type) | TREE_READONLY (array));
2529 TREE_SIDE_EFFECTS (rval)
2530 |= (CP_TYPE_VOLATILE_P (type) | TREE_SIDE_EFFECTS (array));
2531 TREE_THIS_VOLATILE (rval)
2532 |= (CP_TYPE_VOLATILE_P (type) | TREE_THIS_VOLATILE (array));
2533 return require_complete_type (fold (rval));
2537 tree ar = default_conversion (array);
2538 tree ind = default_conversion (idx);
2540 /* Put the integer in IND to simplify error checking. */
2541 if (TREE_CODE (TREE_TYPE (ar)) == INTEGER_TYPE)
2543 tree temp = ar;
2544 ar = ind;
2545 ind = temp;
2548 if (ar == error_mark_node)
2549 return ar;
2551 if (TREE_CODE (TREE_TYPE (ar)) != POINTER_TYPE)
2553 error ("subscripted value is neither array nor pointer");
2554 return error_mark_node;
2556 if (TREE_CODE (TREE_TYPE (ind)) != INTEGER_TYPE)
2558 error ("array subscript is not an integer");
2559 return error_mark_node;
2562 return build_indirect_ref (cp_build_binary_op (PLUS_EXPR, ar, ind),
2563 "array indexing");
2567 /* Build a function call to function FUNCTION with parameters PARAMS.
2568 PARAMS is a list--a chain of TREE_LIST nodes--in which the
2569 TREE_VALUE of each node is a parameter-expression. The PARAMS do
2570 not include any object pointer that may be required. FUNCTION's
2571 data type may be a function type or a pointer-to-function.
2573 For C++: If FUNCTION's data type is a TREE_LIST, then the tree list
2574 is the list of possible methods that FUNCTION could conceivably
2575 be. If the list of methods comes from a class, then it will be
2576 a list of lists (where each element is associated with the class
2577 that produced it), otherwise it will be a simple list (for
2578 functions overloaded in global scope).
2580 In the first case, TREE_VALUE (function) is the head of one of those
2581 lists, and TREE_PURPOSE is the name of the function.
2583 In the second case, TREE_PURPOSE (function) is the function's
2584 name directly.
2586 DECL is the class instance variable, usually CURRENT_CLASS_REF.
2588 When calling a TEMPLATE_DECL, we don't require a complete return
2589 type. */
2591 tree
2592 build_x_function_call (function, params, decl)
2593 tree function, params, decl;
2595 tree type;
2596 tree template_id = NULL_TREE;
2597 int is_method;
2599 if (function == error_mark_node)
2600 return error_mark_node;
2602 if (processing_template_decl)
2603 return build_min_nt (CALL_EXPR, function, params, NULL_TREE);
2605 /* Save explicit template arguments if found */
2606 if (TREE_CODE (function) == TEMPLATE_ID_EXPR)
2608 template_id = function;
2609 function = TREE_OPERAND (function, 0);
2612 type = TREE_TYPE (function);
2614 if (TREE_CODE (type) == OFFSET_TYPE
2615 && TREE_TYPE (type) == unknown_type_node
2616 && TREE_CODE (function) == TREE_LIST
2617 && TREE_CHAIN (function) == NULL_TREE)
2619 /* Undo (Foo:bar)()... */
2620 type = TYPE_OFFSET_BASETYPE (type);
2621 function = TREE_VALUE (function);
2622 my_friendly_assert (TREE_CODE (function) == TREE_LIST, 999);
2623 my_friendly_assert (TREE_CHAIN (function) == NULL_TREE, 999);
2624 function = TREE_VALUE (function);
2625 if (TREE_CODE (function) == OVERLOAD)
2626 function = OVL_FUNCTION (function);
2627 my_friendly_assert (TREE_CODE (function) == FUNCTION_DECL, 999);
2628 function = DECL_NAME (function);
2629 return build_method_call (decl, function, params,
2630 TYPE_BINFO (type), LOOKUP_NORMAL);
2633 if (TREE_CODE (function) == OFFSET_REF
2634 && TREE_CODE (type) != METHOD_TYPE)
2635 function = resolve_offset_ref (function);
2637 if ((TREE_CODE (function) == FUNCTION_DECL
2638 && DECL_STATIC_FUNCTION_P (function))
2639 || (DECL_FUNCTION_TEMPLATE_P (function)
2640 && DECL_STATIC_FUNCTION_P (DECL_TEMPLATE_RESULT (function))))
2641 return build_member_call (DECL_CONTEXT (function),
2642 template_id
2643 ? template_id : DECL_NAME (function),
2644 params);
2646 is_method = ((TREE_CODE (function) == TREE_LIST
2647 && current_class_type != NULL_TREE
2648 && (IDENTIFIER_CLASS_VALUE (TREE_PURPOSE (function))
2649 == function))
2650 || (TREE_CODE (function) == OVERLOAD
2651 && DECL_FUNCTION_MEMBER_P (OVL_CURRENT (function)))
2652 || TREE_CODE (function) == IDENTIFIER_NODE
2653 || TREE_CODE (type) == METHOD_TYPE
2654 || TYPE_PTRMEMFUNC_P (type));
2656 /* A friend template. Make it look like a toplevel declaration. */
2657 if (! is_method && TREE_CODE (function) == TEMPLATE_DECL)
2658 function = ovl_cons (function, NULL_TREE);
2660 /* Handle methods, friends, and overloaded functions, respectively. */
2661 if (is_method)
2663 tree basetype = NULL_TREE;
2665 if (TREE_CODE (function) == OVERLOAD)
2666 function = OVL_CURRENT (function);
2668 if (TREE_CODE (function) == FUNCTION_DECL
2669 || DECL_FUNCTION_TEMPLATE_P (function))
2671 basetype = DECL_CONTEXT (function);
2673 if (DECL_NAME (function))
2674 function = DECL_NAME (function);
2675 else
2676 function = TYPE_IDENTIFIER (DECL_CONTEXT (function));
2678 else if (TREE_CODE (function) == TREE_LIST)
2680 my_friendly_assert (TREE_CODE (TREE_VALUE (function))
2681 == FUNCTION_DECL, 312);
2682 basetype = DECL_CONTEXT (TREE_VALUE (function));
2683 function = TREE_PURPOSE (function);
2685 else if (TREE_CODE (function) != IDENTIFIER_NODE)
2687 if (TREE_CODE (function) == OFFSET_REF)
2689 if (TREE_OPERAND (function, 0))
2690 decl = TREE_OPERAND (function, 0);
2692 /* Call via a pointer to member function. */
2693 if (decl == NULL_TREE)
2695 error ("pointer to member function called, but not in class scope");
2696 return error_mark_node;
2698 /* What other type of POINTER_TYPE could this be? */
2699 if (TREE_CODE (TREE_TYPE (function)) != POINTER_TYPE
2700 && ! TYPE_PTRMEMFUNC_P (TREE_TYPE (function))
2701 && TREE_CODE (function) != OFFSET_REF)
2702 function = build (OFFSET_REF, TREE_TYPE (type), NULL_TREE,
2703 function);
2704 goto do_x_function;
2707 /* this is an abbreviated method call.
2708 must go through here in case it is a virtual function.
2709 @@ Perhaps this could be optimized. */
2711 if (basetype && (! current_class_type
2712 || ! DERIVED_FROM_P (basetype, current_class_type)))
2713 return build_member_call (basetype, function, params);
2715 if (decl == NULL_TREE)
2717 if (current_class_type == NULL_TREE)
2719 cp_error ("object missing in call to method `%D'", function);
2720 return error_mark_node;
2722 /* Yow: call from a static member function. */
2723 decl = build_dummy_object (current_class_type);
2726 /* Put back explicit template arguments, if any. */
2727 if (template_id)
2728 function = template_id;
2729 return build_method_call (decl, function, params,
2730 NULL_TREE, LOOKUP_NORMAL);
2732 else if (TREE_CODE (function) == COMPONENT_REF
2733 && type == unknown_type_node)
2735 /* Undo what we did in build_component_ref. */
2736 decl = TREE_OPERAND (function, 0);
2737 function = TREE_OPERAND (function, 1);
2738 function = DECL_NAME (OVL_CURRENT (function));
2740 if (template_id)
2742 TREE_OPERAND (template_id, 0) = function;
2743 function = template_id;
2746 return build_method_call (decl, function, params,
2747 NULL_TREE, LOOKUP_NORMAL);
2749 else if (really_overloaded_fn (function))
2751 if (OVL_FUNCTION (function) == NULL_TREE)
2753 cp_error ("function `%D' declared overloaded, but no definitions appear with which to resolve it?!?",
2754 TREE_PURPOSE (function));
2755 return error_mark_node;
2757 else
2759 /* Put back explicit template arguments, if any. */
2760 if (template_id)
2761 function = template_id;
2762 return build_new_function_call (function, params);
2765 else
2766 /* Remove a potential OVERLOAD around it */
2767 function = OVL_CURRENT (function);
2769 do_x_function:
2770 if (TREE_CODE (function) == OFFSET_REF)
2772 /* If the component is a data element (or a virtual function), we play
2773 games here to make things work. */
2774 tree decl_addr;
2776 if (TREE_OPERAND (function, 0))
2777 decl = TREE_OPERAND (function, 0);
2778 else
2779 decl = current_class_ref;
2781 decl_addr = build_unary_op (ADDR_EXPR, decl, 0);
2783 /* Sigh. OFFSET_REFs are being used for too many things.
2784 They're being used both for -> and ->*, and we want to resolve
2785 the -> cases here, but leave the ->*. We could use
2786 resolve_offset_ref for those, too, but it would call
2787 get_member_function_from_ptrfunc and decl_addr wouldn't get
2788 updated properly. Nasty. */
2789 if (TREE_CODE (TREE_OPERAND (function, 1)) == FIELD_DECL)
2790 function = resolve_offset_ref (function);
2791 else
2792 function = TREE_OPERAND (function, 1);
2794 function = get_member_function_from_ptrfunc (&decl_addr, function);
2795 params = tree_cons (NULL_TREE, decl_addr, params);
2796 return build_function_call (function, params);
2799 type = TREE_TYPE (function);
2800 if (type != error_mark_node)
2802 if (TREE_CODE (type) == REFERENCE_TYPE)
2803 type = TREE_TYPE (type);
2805 if (IS_AGGR_TYPE (type))
2806 return build_opfncall (CALL_EXPR, LOOKUP_NORMAL, function, params, NULL_TREE);
2809 if (is_method)
2811 tree fntype = TREE_TYPE (function);
2812 tree ctypeptr = NULL_TREE;
2814 /* Explicitly named method? */
2815 if (TREE_CODE (function) == FUNCTION_DECL)
2816 ctypeptr = build_pointer_type (DECL_CLASS_CONTEXT (function));
2817 /* Expression with ptr-to-method type? It could either be a plain
2818 usage, or it might be a case where the ptr-to-method is being
2819 passed in as an argument. */
2820 else if (TYPE_PTRMEMFUNC_P (fntype))
2822 tree rec = TYPE_METHOD_BASETYPE (TREE_TYPE
2823 (TYPE_PTRMEMFUNC_FN_TYPE (fntype)));
2824 ctypeptr = build_pointer_type (rec);
2826 /* Unexpected node type? */
2827 else
2828 my_friendly_abort (116);
2829 if (decl == NULL_TREE)
2831 if (current_function_decl
2832 && DECL_STATIC_FUNCTION_P (current_function_decl))
2833 error ("invalid call to member function needing `this' in static member function scope");
2834 else
2835 error ("pointer to member function called, but not in class scope");
2836 return error_mark_node;
2838 if (TREE_CODE (TREE_TYPE (decl)) != POINTER_TYPE
2839 && ! TYPE_PTRMEMFUNC_P (TREE_TYPE (decl)))
2841 decl = build_unary_op (ADDR_EXPR, decl, 0);
2842 decl = convert_pointer_to (TREE_TYPE (ctypeptr), decl);
2844 else
2845 decl = build_c_cast (ctypeptr, decl);
2846 params = tree_cons (NULL_TREE, decl, params);
2849 return build_function_call (function, params);
2852 /* Resolve a pointer to member function. INSTANCE is the object
2853 instance to use, if the member points to a virtual member. */
2855 tree
2856 get_member_function_from_ptrfunc (instance_ptrptr, function)
2857 tree *instance_ptrptr;
2858 tree function;
2860 if (TREE_CODE (function) == OFFSET_REF)
2862 function = TREE_OPERAND (function, 1);
2865 if (TYPE_PTRMEMFUNC_P (TREE_TYPE (function)))
2867 tree fntype, idx, e1, delta, delta2, e2, e3, aref, vtbl;
2868 tree instance, basetype;
2870 tree instance_ptr = *instance_ptrptr;
2872 if (instance_ptr == error_mark_node
2873 && TREE_CODE (function) == PTRMEM_CST)
2875 /* Extracting the function address from a pmf is only
2876 allowed with -Wno-pmf-conversions. It only works for
2877 pmf constants. */
2878 e1 = build_addr_func (PTRMEM_CST_MEMBER (function));
2879 e1 = convert (TYPE_PTRMEMFUNC_FN_TYPE (TREE_TYPE (function)), e1);
2880 return e1;
2883 if (TREE_SIDE_EFFECTS (instance_ptr))
2884 instance_ptr = save_expr (instance_ptr);
2886 if (TREE_SIDE_EFFECTS (function))
2887 function = save_expr (function);
2889 fntype = TYPE_PTRMEMFUNC_FN_TYPE (TREE_TYPE (function));
2890 basetype = TYPE_METHOD_BASETYPE (TREE_TYPE (fntype));
2892 /* Convert down to the right base, before using the instance. */
2893 instance = convert_pointer_to_real (basetype, instance_ptr);
2894 if (instance == error_mark_node && instance_ptr != error_mark_node)
2895 return instance;
2897 e3 = PFN_FROM_PTRMEMFUNC (function);
2899 vtbl = convert_pointer_to (ptr_type_node, instance);
2900 delta = cp_convert (ptrdiff_type_node,
2901 build_component_ref (function, delta_identifier,
2902 NULL_TREE, 0));
2904 /* This used to avoid checking for virtual functions if basetype
2905 has no virtual functions, according to an earlier ANSI draft.
2906 With the final ISO C++ rules, such an optimization is
2907 incorrect: A pointer to a derived member can be static_cast
2908 to pointer-to-base-member, as long as the dynamic object
2909 later has the right member. */
2911 /* Promoting idx before saving it improves performance on RISC
2912 targets. Without promoting, the first compare used
2913 load-with-sign-extend, while the second used normal load then
2914 shift to sign-extend. An optimizer flaw, perhaps, but it's
2915 easier to make this change. */
2916 switch (TARGET_PTRMEMFUNC_VBIT_LOCATION)
2918 case ptrmemfunc_vbit_in_pfn:
2919 idx = cp_build_binary_op (TRUNC_DIV_EXPR,
2920 build1 (NOP_EXPR, vtable_index_type, e3),
2921 TYPE_SIZE_UNIT (vtable_entry_type));
2922 e1 = cp_build_binary_op (BIT_AND_EXPR,
2923 build1 (NOP_EXPR, vtable_index_type, e3),
2924 integer_one_node);
2925 break;
2927 case ptrmemfunc_vbit_in_delta:
2928 idx = build1 (NOP_EXPR, vtable_index_type, e3);
2929 e1 = cp_build_binary_op (BIT_AND_EXPR,
2930 delta, integer_one_node);
2931 delta = cp_build_binary_op (RSHIFT_EXPR,
2932 build1 (NOP_EXPR, vtable_index_type,
2933 delta),
2934 integer_one_node);
2935 break;
2937 default:
2938 abort ();
2941 /* DELTA2 is the amount by which to adjust the `this' pointer
2942 to find the vtbl. */
2943 delta2 = delta;
2944 vtbl = build
2945 (PLUS_EXPR,
2946 build_pointer_type (build_pointer_type (vtable_entry_type)),
2947 vtbl, cp_convert (ptrdiff_type_node, delta2));
2948 vtbl = build_indirect_ref (vtbl, NULL);
2949 aref = build_array_ref (vtbl, idx);
2951 if (! flag_vtable_thunks)
2953 aref = save_expr (aref);
2955 delta = cp_build_binary_op
2956 (PLUS_EXPR,
2957 build_conditional_expr (e1,
2958 build_component_ref (aref,
2959 delta_identifier,
2960 NULL_TREE, 0),
2961 integer_zero_node),
2962 delta);
2965 if (flag_vtable_thunks)
2966 e2 = aref;
2967 else
2968 e2 = build_component_ref (aref, pfn_identifier, NULL_TREE, 0);
2969 TREE_TYPE (e2) = TREE_TYPE (e3);
2970 e1 = build_conditional_expr (e1, e2, e3);
2972 /* Make sure this doesn't get evaluated first inside one of the
2973 branches of the COND_EXPR. */
2974 if (TREE_CODE (instance_ptr) == SAVE_EXPR)
2975 e1 = build (COMPOUND_EXPR, TREE_TYPE (e1),
2976 instance_ptr, e1);
2978 *instance_ptrptr = build (PLUS_EXPR, TREE_TYPE (instance_ptr),
2979 instance_ptr, delta);
2981 if (instance_ptr == error_mark_node
2982 && TREE_CODE (e1) != ADDR_EXPR
2983 && TREE_CODE (TREE_OPERAND (e1, 0)) != FUNCTION_DECL)
2984 cp_error ("object missing in `%E'", function);
2986 function = e1;
2988 return function;
2991 tree
2992 build_function_call_real (function, params, require_complete, flags)
2993 tree function, params;
2994 int require_complete, flags;
2996 register tree fntype, fndecl;
2997 register tree value_type;
2998 register tree coerced_params;
2999 tree result;
3000 tree name = NULL_TREE, assembler_name = NULL_TREE;
3001 int is_method;
3003 /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
3004 Strip such NOP_EXPRs, since FUNCTION is used in non-lvalue context. */
3005 if (TREE_CODE (function) == NOP_EXPR
3006 && TREE_TYPE (function) == TREE_TYPE (TREE_OPERAND (function, 0)))
3007 function = TREE_OPERAND (function, 0);
3009 if (TREE_CODE (function) == FUNCTION_DECL)
3011 name = DECL_NAME (function);
3012 assembler_name = DECL_ASSEMBLER_NAME (function);
3014 GNU_xref_call (current_function_decl,
3015 IDENTIFIER_POINTER (name ? name
3016 : TYPE_IDENTIFIER (DECL_CLASS_CONTEXT
3017 (function))));
3018 mark_used (function);
3019 fndecl = function;
3021 /* Convert anything with function type to a pointer-to-function. */
3022 if (pedantic && DECL_MAIN_P (function))
3023 pedwarn ("ISO C++ forbids calling `::main' from within program");
3025 /* Differs from default_conversion by not setting TREE_ADDRESSABLE
3026 (because calling an inline function does not mean the function
3027 needs to be separately compiled). */
3029 if (DECL_INLINE (function))
3030 function = inline_conversion (function);
3031 else
3032 function = build_addr_func (function);
3034 else
3036 fndecl = NULL_TREE;
3038 function = build_addr_func (function);
3041 if (function == error_mark_node)
3042 return error_mark_node;
3044 fntype = TREE_TYPE (function);
3046 if (TYPE_PTRMEMFUNC_P (fntype))
3048 cp_error ("must use .* or ->* to call pointer-to-member function in `%E (...)'",
3049 function);
3050 return error_mark_node;
3053 is_method = (TREE_CODE (fntype) == POINTER_TYPE
3054 && TREE_CODE (TREE_TYPE (fntype)) == METHOD_TYPE);
3056 if (!((TREE_CODE (fntype) == POINTER_TYPE
3057 && TREE_CODE (TREE_TYPE (fntype)) == FUNCTION_TYPE)
3058 || is_method
3059 || TREE_CODE (function) == TEMPLATE_ID_EXPR))
3061 cp_error ("`%E' cannot be used as a function", function);
3062 return error_mark_node;
3065 /* fntype now gets the type of function pointed to. */
3066 fntype = TREE_TYPE (fntype);
3068 /* Convert the parameters to the types declared in the
3069 function prototype, or apply default promotions. */
3071 if (flags & LOOKUP_COMPLAIN)
3072 coerced_params = convert_arguments (TYPE_ARG_TYPES (fntype),
3073 params, fndecl, LOOKUP_NORMAL);
3074 else
3075 coerced_params = convert_arguments (TYPE_ARG_TYPES (fntype),
3076 params, fndecl, 0);
3078 if (coerced_params == error_mark_node)
3080 if (flags & LOOKUP_SPECULATIVELY)
3081 return NULL_TREE;
3082 else
3083 return error_mark_node;
3086 /* Check for errors in format strings. */
3088 if (warn_format && (name || assembler_name))
3089 check_function_format (NULL, name, assembler_name, coerced_params);
3091 /* Recognize certain built-in functions so we can make tree-codes
3092 other than CALL_EXPR. We do this when it enables fold-const.c
3093 to do something useful. */
3095 if (TREE_CODE (function) == ADDR_EXPR
3096 && TREE_CODE (TREE_OPERAND (function, 0)) == FUNCTION_DECL
3097 && DECL_BUILT_IN (TREE_OPERAND (function, 0)))
3099 result = expand_tree_builtin (TREE_OPERAND (function, 0),
3100 params, coerced_params);
3101 if (result)
3102 return result;
3105 /* Some built-in function calls will be evaluated at
3106 compile-time in fold (). */
3107 result = fold (build_call (function, coerced_params));
3108 value_type = TREE_TYPE (result);
3110 if (require_complete)
3112 if (TREE_CODE (value_type) == VOID_TYPE)
3113 return result;
3114 result = require_complete_type (result);
3116 if (IS_AGGR_TYPE (value_type))
3117 result = build_cplus_new (value_type, result);
3118 return convert_from_reference (result);
3121 tree
3122 build_function_call (function, params)
3123 tree function, params;
3125 return build_function_call_real (function, params, 1, LOOKUP_NORMAL);
3128 /* Convert the actual parameter expressions in the list VALUES
3129 to the types in the list TYPELIST.
3130 If parmdecls is exhausted, or when an element has NULL as its type,
3131 perform the default conversions.
3133 NAME is an IDENTIFIER_NODE or 0. It is used only for error messages.
3135 This is also where warnings about wrong number of args are generated.
3137 Return a list of expressions for the parameters as converted.
3139 Both VALUES and the returned value are chains of TREE_LIST nodes
3140 with the elements of the list in the TREE_VALUE slots of those nodes.
3142 In C++, unspecified trailing parameters can be filled in with their
3143 default arguments, if such were specified. Do so here. */
3145 tree
3146 convert_arguments (typelist, values, fndecl, flags)
3147 tree typelist, values, fndecl;
3148 int flags;
3150 register tree typetail, valtail;
3151 register tree result = NULL_TREE;
3152 const char *called_thing = 0;
3153 int i = 0;
3155 /* Argument passing is always copy-initialization. */
3156 flags |= LOOKUP_ONLYCONVERTING;
3158 if (fndecl)
3160 if (TREE_CODE (TREE_TYPE (fndecl)) == METHOD_TYPE)
3162 if (DECL_NAME (fndecl) == NULL_TREE
3163 || IDENTIFIER_HAS_TYPE_VALUE (DECL_NAME (fndecl)))
3164 called_thing = "constructor";
3165 else
3166 called_thing = "member function";
3168 else
3169 called_thing = "function";
3172 for (valtail = values, typetail = typelist;
3173 valtail;
3174 valtail = TREE_CHAIN (valtail), i++)
3176 register tree type = typetail ? TREE_VALUE (typetail) : 0;
3177 register tree val = TREE_VALUE (valtail);
3179 if (val == error_mark_node)
3180 return error_mark_node;
3182 if (type == void_type_node)
3184 if (fndecl)
3186 cp_error_at ("too many arguments to %s `%+#D'", called_thing,
3187 fndecl);
3188 error ("at this point in file");
3190 else
3191 error ("too many arguments to function");
3192 /* In case anybody wants to know if this argument
3193 list is valid. */
3194 if (result)
3195 TREE_TYPE (tree_last (result)) = error_mark_node;
3196 break;
3199 if (TREE_CODE (val) == OFFSET_REF)
3200 val = resolve_offset_ref (val);
3202 /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
3203 Strip such NOP_EXPRs, since VAL is used in non-lvalue context. */
3204 if (TREE_CODE (val) == NOP_EXPR
3205 && TREE_TYPE (val) == TREE_TYPE (TREE_OPERAND (val, 0))
3206 && (type == 0 || TREE_CODE (type) != REFERENCE_TYPE))
3207 val = TREE_OPERAND (val, 0);
3209 if (type == 0 || TREE_CODE (type) != REFERENCE_TYPE)
3211 if (TREE_CODE (TREE_TYPE (val)) == ARRAY_TYPE
3212 || TREE_CODE (TREE_TYPE (val)) == FUNCTION_TYPE
3213 || TREE_CODE (TREE_TYPE (val)) == METHOD_TYPE)
3214 val = default_conversion (val);
3217 if (val == error_mark_node)
3218 return error_mark_node;
3220 if (type != 0)
3222 /* Formal parm type is specified by a function prototype. */
3223 tree parmval;
3225 if (!COMPLETE_TYPE_P (complete_type (type)))
3227 error ("parameter type of called function is incomplete");
3228 parmval = val;
3230 else
3232 parmval = convert_for_initialization
3233 (NULL_TREE, type, val, flags,
3234 "argument passing", fndecl, i);
3235 if (PROMOTE_PROTOTYPES
3236 && INTEGRAL_TYPE_P (type)
3237 && (TYPE_PRECISION (type)
3238 < TYPE_PRECISION (integer_type_node)))
3239 parmval = default_conversion (parmval);
3242 if (parmval == error_mark_node)
3243 return error_mark_node;
3245 result = tree_cons (NULL_TREE, parmval, result);
3247 else
3249 if (TREE_CODE (TREE_TYPE (val)) == REFERENCE_TYPE)
3250 val = convert_from_reference (val);
3252 if (fndecl && DECL_BUILT_IN (fndecl)
3253 && DECL_FUNCTION_CODE (fndecl) == BUILT_IN_CONSTANT_P)
3254 /* Don't do ellipsis conversion for __built_in_constant_p
3255 as this will result in spurious warnings for non-POD
3256 types. */
3257 val = require_complete_type (val);
3258 else
3259 val = convert_arg_to_ellipsis (val);
3261 result = tree_cons (NULL_TREE, val, result);
3264 if (typetail)
3265 typetail = TREE_CHAIN (typetail);
3268 if (typetail != 0 && typetail != void_list_node)
3270 /* See if there are default arguments that can be used */
3271 if (TREE_PURPOSE (typetail))
3273 for (; typetail != void_list_node; ++i)
3275 tree parmval
3276 = convert_default_arg (TREE_VALUE (typetail),
3277 TREE_PURPOSE (typetail),
3278 fndecl, i);
3280 if (parmval == error_mark_node)
3281 return error_mark_node;
3283 result = tree_cons (0, parmval, result);
3284 typetail = TREE_CHAIN (typetail);
3285 /* ends with `...'. */
3286 if (typetail == NULL_TREE)
3287 break;
3290 else
3292 if (fndecl)
3294 cp_error_at ("too few arguments to %s `%+#D'",
3295 called_thing, fndecl);
3296 error ("at this point in file");
3298 else
3299 error ("too few arguments to function");
3300 return error_mark_list;
3304 return nreverse (result);
3307 /* Build a binary-operation expression, after performing default
3308 conversions on the operands. CODE is the kind of expression to build. */
3310 tree
3311 build_x_binary_op (code, arg1, arg2)
3312 enum tree_code code;
3313 tree arg1, arg2;
3315 if (processing_template_decl)
3316 return build_min_nt (code, arg1, arg2);
3318 return build_new_op (code, LOOKUP_NORMAL, arg1, arg2, NULL_TREE);
3321 /* Build a binary-operation expression without default conversions.
3322 CODE is the kind of expression to build.
3323 This function differs from `build' in several ways:
3324 the data type of the result is computed and recorded in it,
3325 warnings are generated if arg data types are invalid,
3326 special handling for addition and subtraction of pointers is known,
3327 and some optimization is done (operations on narrow ints
3328 are done in the narrower type when that gives the same result).
3329 Constant folding is also done before the result is returned.
3331 Note that the operands will never have enumeral types
3332 because either they have just had the default conversions performed
3333 or they have both just been converted to some other type in which
3334 the arithmetic is to be done.
3336 C++: must do special pointer arithmetic when implementing
3337 multiple inheritance, and deal with pointer to member functions. */
3339 tree
3340 build_binary_op (code, orig_op0, orig_op1, convert_p)
3341 enum tree_code code;
3342 tree orig_op0, orig_op1;
3343 int convert_p ATTRIBUTE_UNUSED;
3345 tree op0, op1;
3346 register enum tree_code code0, code1;
3347 tree type0, type1;
3349 /* Expression code to give to the expression when it is built.
3350 Normally this is CODE, which is what the caller asked for,
3351 but in some special cases we change it. */
3352 register enum tree_code resultcode = code;
3354 /* Data type in which the computation is to be performed.
3355 In the simplest cases this is the common type of the arguments. */
3356 register tree result_type = NULL;
3358 /* Nonzero means operands have already been type-converted
3359 in whatever way is necessary.
3360 Zero means they need to be converted to RESULT_TYPE. */
3361 int converted = 0;
3363 /* Nonzero means create the expression with this type, rather than
3364 RESULT_TYPE. */
3365 tree build_type = 0;
3367 /* Nonzero means after finally constructing the expression
3368 convert it to this type. */
3369 tree final_type = 0;
3371 /* Nonzero if this is an operation like MIN or MAX which can
3372 safely be computed in short if both args are promoted shorts.
3373 Also implies COMMON.
3374 -1 indicates a bitwise operation; this makes a difference
3375 in the exact conditions for when it is safe to do the operation
3376 in a narrower mode. */
3377 int shorten = 0;
3379 /* Nonzero if this is a comparison operation;
3380 if both args are promoted shorts, compare the original shorts.
3381 Also implies COMMON. */
3382 int short_compare = 0;
3384 /* Nonzero if this is a right-shift operation, which can be computed on the
3385 original short and then promoted if the operand is a promoted short. */
3386 int short_shift = 0;
3388 /* Nonzero means set RESULT_TYPE to the common type of the args. */
3389 int common = 0;
3391 /* Apply default conversions. */
3392 if (code == TRUTH_AND_EXPR || code == TRUTH_ANDIF_EXPR
3393 || code == TRUTH_OR_EXPR || code == TRUTH_ORIF_EXPR
3394 || code == TRUTH_XOR_EXPR)
3396 op0 = decay_conversion (orig_op0);
3397 op1 = decay_conversion (orig_op1);
3399 else
3401 op0 = default_conversion (orig_op0);
3402 op1 = default_conversion (orig_op1);
3405 /* Strip NON_LVALUE_EXPRs, etc., since we aren't using as an lvalue. */
3406 STRIP_TYPE_NOPS (op0);
3407 STRIP_TYPE_NOPS (op1);
3409 /* DTRT if one side is an overloaded function, but complain about it. */
3410 if (type_unknown_p (op0))
3412 tree t = instantiate_type (TREE_TYPE (op1), op0, itf_none);
3413 if (t != error_mark_node)
3415 cp_pedwarn ("assuming cast to type `%T' from overloaded function",
3416 TREE_TYPE (t));
3417 op0 = t;
3420 if (type_unknown_p (op1))
3422 tree t = instantiate_type (TREE_TYPE (op0), op1, itf_none);
3423 if (t != error_mark_node)
3425 cp_pedwarn ("assuming cast to type `%T' from overloaded function",
3426 TREE_TYPE (t));
3427 op1 = t;
3431 type0 = TREE_TYPE (op0);
3432 type1 = TREE_TYPE (op1);
3434 /* The expression codes of the data types of the arguments tell us
3435 whether the arguments are integers, floating, pointers, etc. */
3436 code0 = TREE_CODE (type0);
3437 code1 = TREE_CODE (type1);
3439 /* If an error was already reported for one of the arguments,
3440 avoid reporting another error. */
3442 if (code0 == ERROR_MARK || code1 == ERROR_MARK)
3443 return error_mark_node;
3445 switch (code)
3447 case PLUS_EXPR:
3448 /* Handle the pointer + int case. */
3449 if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
3450 return pointer_int_sum (PLUS_EXPR, op0, op1);
3451 else if (code1 == POINTER_TYPE && code0 == INTEGER_TYPE)
3452 return pointer_int_sum (PLUS_EXPR, op1, op0);
3453 else
3454 common = 1;
3455 break;
3457 case MINUS_EXPR:
3458 /* Subtraction of two similar pointers.
3459 We must subtract them as integers, then divide by object size. */
3460 if (code0 == POINTER_TYPE && code1 == POINTER_TYPE
3461 && comp_target_types (type0, type1, 1))
3462 return pointer_diff (op0, op1, common_type (type0, type1));
3463 /* Handle pointer minus int. Just like pointer plus int. */
3464 else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
3465 return pointer_int_sum (MINUS_EXPR, op0, op1);
3466 else
3467 common = 1;
3468 break;
3470 case MULT_EXPR:
3471 common = 1;
3472 break;
3474 case TRUNC_DIV_EXPR:
3475 case CEIL_DIV_EXPR:
3476 case FLOOR_DIV_EXPR:
3477 case ROUND_DIV_EXPR:
3478 case EXACT_DIV_EXPR:
3479 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE
3480 || code0 == COMPLEX_TYPE)
3481 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
3482 || code1 == COMPLEX_TYPE))
3484 if (TREE_CODE (op1) == INTEGER_CST && integer_zerop (op1))
3485 cp_warning ("division by zero in `%E / 0'", op0);
3486 else if (TREE_CODE (op1) == REAL_CST && real_zerop (op1))
3487 cp_warning ("division by zero in `%E / 0.'", op0);
3489 if (!(code0 == INTEGER_TYPE && code1 == INTEGER_TYPE))
3490 resultcode = RDIV_EXPR;
3491 else
3492 /* When dividing two signed integers, we have to promote to int.
3493 unless we divide by a constant != -1. Note that default
3494 conversion will have been performed on the operands at this
3495 point, so we have to dig out the original type to find out if
3496 it was unsigned. */
3497 shorten = ((TREE_CODE (op0) == NOP_EXPR
3498 && TREE_UNSIGNED (TREE_TYPE (TREE_OPERAND (op0, 0))))
3499 || (TREE_CODE (op1) == INTEGER_CST
3500 && ! integer_all_onesp (op1)));
3502 common = 1;
3504 break;
3506 case BIT_AND_EXPR:
3507 case BIT_ANDTC_EXPR:
3508 case BIT_IOR_EXPR:
3509 case BIT_XOR_EXPR:
3510 if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
3511 shorten = -1;
3512 /* If one operand is a constant, and the other is a short type
3513 that has been converted to an int,
3514 really do the work in the short type and then convert the
3515 result to int. If we are lucky, the constant will be 0 or 1
3516 in the short type, making the entire operation go away. */
3517 if (TREE_CODE (op0) == INTEGER_CST
3518 && TREE_CODE (op1) == NOP_EXPR
3519 && (TYPE_PRECISION (type1)
3520 > TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (op1, 0))))
3521 && TREE_UNSIGNED (TREE_TYPE (TREE_OPERAND (op1, 0))))
3523 final_type = result_type;
3524 op1 = TREE_OPERAND (op1, 0);
3525 result_type = TREE_TYPE (op1);
3527 if (TREE_CODE (op1) == INTEGER_CST
3528 && TREE_CODE (op0) == NOP_EXPR
3529 && (TYPE_PRECISION (type0)
3530 > TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (op0, 0))))
3531 && TREE_UNSIGNED (TREE_TYPE (TREE_OPERAND (op0, 0))))
3533 final_type = result_type;
3534 op0 = TREE_OPERAND (op0, 0);
3535 result_type = TREE_TYPE (op0);
3537 break;
3539 case TRUNC_MOD_EXPR:
3540 case FLOOR_MOD_EXPR:
3541 if (code1 == INTEGER_TYPE && integer_zerop (op1))
3542 cp_warning ("division by zero in `%E %% 0'", op0);
3543 else if (code1 == REAL_TYPE && real_zerop (op1))
3544 cp_warning ("division by zero in `%E %% 0.'", op0);
3546 if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
3548 /* Although it would be tempting to shorten always here, that loses
3549 on some targets, since the modulo instruction is undefined if the
3550 quotient can't be represented in the computation mode. We shorten
3551 only if unsigned or if dividing by something we know != -1. */
3552 shorten = ((TREE_CODE (op0) == NOP_EXPR
3553 && TREE_UNSIGNED (TREE_TYPE (TREE_OPERAND (op0, 0))))
3554 || (TREE_CODE (op1) == INTEGER_CST
3555 && ! integer_all_onesp (op1)));
3556 common = 1;
3558 break;
3560 case TRUTH_ANDIF_EXPR:
3561 case TRUTH_ORIF_EXPR:
3562 case TRUTH_AND_EXPR:
3563 case TRUTH_OR_EXPR:
3564 result_type = boolean_type_node;
3565 break;
3567 /* Shift operations: result has same type as first operand;
3568 always convert second operand to int.
3569 Also set SHORT_SHIFT if shifting rightward. */
3571 case RSHIFT_EXPR:
3572 if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
3574 result_type = type0;
3575 if (TREE_CODE (op1) == INTEGER_CST)
3577 if (tree_int_cst_lt (op1, integer_zero_node))
3578 warning ("right shift count is negative");
3579 else
3581 if (! integer_zerop (op1))
3582 short_shift = 1;
3583 if (compare_tree_int (op1, TYPE_PRECISION (type0)) >= 0)
3584 warning ("right shift count >= width of type");
3587 /* Convert the shift-count to an integer, regardless of
3588 size of value being shifted. */
3589 if (TYPE_MAIN_VARIANT (TREE_TYPE (op1)) != integer_type_node)
3590 op1 = cp_convert (integer_type_node, op1);
3591 /* Avoid converting op1 to result_type later. */
3592 converted = 1;
3594 break;
3596 case LSHIFT_EXPR:
3597 if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
3599 result_type = type0;
3600 if (TREE_CODE (op1) == INTEGER_CST)
3602 if (tree_int_cst_lt (op1, integer_zero_node))
3603 warning ("left shift count is negative");
3604 else if (compare_tree_int (op1, TYPE_PRECISION (type0)) >= 0)
3605 warning ("left shift count >= width of type");
3607 /* Convert the shift-count to an integer, regardless of
3608 size of value being shifted. */
3609 if (TYPE_MAIN_VARIANT (TREE_TYPE (op1)) != integer_type_node)
3610 op1 = cp_convert (integer_type_node, op1);
3611 /* Avoid converting op1 to result_type later. */
3612 converted = 1;
3614 break;
3616 case RROTATE_EXPR:
3617 case LROTATE_EXPR:
3618 if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
3620 result_type = type0;
3621 if (TREE_CODE (op1) == INTEGER_CST)
3623 if (tree_int_cst_lt (op1, integer_zero_node))
3624 warning ("%s rotate count is negative",
3625 (code == LROTATE_EXPR) ? "left" : "right");
3626 else if (compare_tree_int (op1, TYPE_PRECISION (type0)) >= 0)
3627 warning ("%s rotate count >= width of type",
3628 (code == LROTATE_EXPR) ? "left" : "right");
3630 /* Convert the shift-count to an integer, regardless of
3631 size of value being shifted. */
3632 if (TYPE_MAIN_VARIANT (TREE_TYPE (op1)) != integer_type_node)
3633 op1 = cp_convert (integer_type_node, op1);
3635 break;
3637 case EQ_EXPR:
3638 case NE_EXPR:
3639 if (warn_float_equal && (code0 == REAL_TYPE || code1 == REAL_TYPE))
3640 warning ("comparing floating point with == or != is unsafe");
3642 build_type = boolean_type_node;
3643 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE
3644 || code0 == COMPLEX_TYPE)
3645 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
3646 || code1 == COMPLEX_TYPE))
3647 short_compare = 1;
3648 else if (code0 == POINTER_TYPE && code1 == POINTER_TYPE)
3649 result_type = composite_pointer_type (type0, type1, op0, op1,
3650 "comparison");
3651 else if (code0 == POINTER_TYPE && null_ptr_cst_p (op1))
3652 result_type = type0;
3653 else if (code1 == POINTER_TYPE && null_ptr_cst_p (op0))
3654 result_type = type1;
3655 else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
3657 result_type = type0;
3658 error ("ISO C++ forbids comparison between pointer and integer");
3660 else if (code0 == INTEGER_TYPE && code1 == POINTER_TYPE)
3662 result_type = type1;
3663 error ("ISO C++ forbids comparison between pointer and integer");
3665 else if (TYPE_PTRMEMFUNC_P (type0) && null_ptr_cst_p (op1))
3667 op0 = build_component_ref (op0, pfn_identifier, NULL_TREE, 0);
3668 op1 = cp_convert (TREE_TYPE (op0), integer_zero_node);
3669 result_type = TREE_TYPE (op0);
3671 else if (TYPE_PTRMEMFUNC_P (type1) && null_ptr_cst_p (op0))
3672 return cp_build_binary_op (code, op1, op0);
3673 else if (TYPE_PTRMEMFUNC_P (type0) && TYPE_PTRMEMFUNC_P (type1)
3674 && same_type_p (type0, type1))
3676 /* E will be the final comparison. */
3677 tree e;
3678 /* E1 and E2 are for scratch. */
3679 tree e1;
3680 tree e2;
3681 tree pfn0;
3682 tree pfn1;
3683 tree delta0;
3684 tree delta1;
3686 if (TREE_SIDE_EFFECTS (op0))
3687 op0 = save_expr (op0);
3688 if (TREE_SIDE_EFFECTS (op1))
3689 op1 = save_expr (op1);
3691 /* We generate:
3693 (op0.pfn == op1.pfn
3694 && (!op0.pfn || op0.delta == op1.delta))
3696 The reason for the `!op0.pfn' bit is that a NULL
3697 pointer-to-member is any member with a zero PFN; the
3698 DELTA field is unspecified. */
3699 pfn0 = pfn_from_ptrmemfunc (op0);
3700 pfn1 = pfn_from_ptrmemfunc (op1);
3701 delta0 = build_component_ref (op0, delta_identifier,
3702 NULL_TREE, 0);
3703 delta1 = build_component_ref (op1, delta_identifier,
3704 NULL_TREE, 0);
3705 e1 = cp_build_binary_op (EQ_EXPR, delta0, delta1);
3706 e2 = cp_build_binary_op (EQ_EXPR,
3707 pfn0,
3708 cp_convert (TREE_TYPE (pfn0),
3709 integer_zero_node));
3710 e1 = cp_build_binary_op (TRUTH_ORIF_EXPR, e1, e2);
3711 e2 = build (EQ_EXPR, boolean_type_node, pfn0, pfn1);
3712 e = cp_build_binary_op (TRUTH_ANDIF_EXPR, e2, e1);
3713 if (code == EQ_EXPR)
3714 return e;
3715 return cp_build_binary_op (EQ_EXPR, e, integer_zero_node);
3717 else if ((TYPE_PTRMEMFUNC_P (type0)
3718 && same_type_p (TYPE_PTRMEMFUNC_FN_TYPE (type0), type1))
3719 || (TYPE_PTRMEMFUNC_P (type1)
3720 && same_type_p (TYPE_PTRMEMFUNC_FN_TYPE (type1), type0)))
3721 my_friendly_abort (20000221);
3722 break;
3724 case MAX_EXPR:
3725 case MIN_EXPR:
3726 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE)
3727 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE))
3728 shorten = 1;
3729 else if (code0 == POINTER_TYPE && code1 == POINTER_TYPE)
3730 result_type = composite_pointer_type (type0, type1, op0, op1,
3731 "comparison");
3732 break;
3734 case LE_EXPR:
3735 case GE_EXPR:
3736 case LT_EXPR:
3737 case GT_EXPR:
3738 build_type = boolean_type_node;
3739 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE)
3740 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE))
3741 short_compare = 1;
3742 else if (code0 == POINTER_TYPE && code1 == POINTER_TYPE)
3743 result_type = composite_pointer_type (type0, type1, op0, op1,
3744 "comparison");
3745 else if (code0 == POINTER_TYPE && TREE_CODE (op1) == INTEGER_CST
3746 && integer_zerop (op1))
3747 result_type = type0;
3748 else if (code1 == POINTER_TYPE && TREE_CODE (op0) == INTEGER_CST
3749 && integer_zerop (op0))
3750 result_type = type1;
3751 else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
3753 result_type = type0;
3754 pedwarn ("ISO C++ forbids comparison between pointer and integer");
3756 else if (code0 == INTEGER_TYPE && code1 == POINTER_TYPE)
3758 result_type = type1;
3759 pedwarn ("ISO C++ forbids comparison between pointer and integer");
3761 break;
3763 case UNORDERED_EXPR:
3764 case ORDERED_EXPR:
3765 case UNLT_EXPR:
3766 case UNLE_EXPR:
3767 case UNGT_EXPR:
3768 case UNGE_EXPR:
3769 case UNEQ_EXPR:
3770 build_type = integer_type_node;
3771 if (code0 != REAL_TYPE || code1 != REAL_TYPE)
3773 error ("unordered comparison on non-floating point argument");
3774 return error_mark_node;
3776 common = 1;
3777 break;
3779 default:
3780 break;
3783 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE || code0 == COMPLEX_TYPE)
3785 (code1 == INTEGER_TYPE || code1 == REAL_TYPE || code1 == COMPLEX_TYPE))
3787 int none_complex = (code0 != COMPLEX_TYPE && code1 != COMPLEX_TYPE);
3789 if (shorten || common || short_compare)
3790 result_type = common_type (type0, type1);
3792 /* For certain operations (which identify themselves by shorten != 0)
3793 if both args were extended from the same smaller type,
3794 do the arithmetic in that type and then extend.
3796 shorten !=0 and !=1 indicates a bitwise operation.
3797 For them, this optimization is safe only if
3798 both args are zero-extended or both are sign-extended.
3799 Otherwise, we might change the result.
3800 Eg, (short)-1 | (unsigned short)-1 is (int)-1
3801 but calculated in (unsigned short) it would be (unsigned short)-1. */
3803 if (shorten && none_complex)
3805 int unsigned0, unsigned1;
3806 tree arg0 = get_narrower (op0, &unsigned0);
3807 tree arg1 = get_narrower (op1, &unsigned1);
3808 /* UNS is 1 if the operation to be done is an unsigned one. */
3809 int uns = TREE_UNSIGNED (result_type);
3810 tree type;
3812 final_type = result_type;
3814 /* Handle the case that OP0 does not *contain* a conversion
3815 but it *requires* conversion to FINAL_TYPE. */
3817 if (op0 == arg0 && TREE_TYPE (op0) != final_type)
3818 unsigned0 = TREE_UNSIGNED (TREE_TYPE (op0));
3819 if (op1 == arg1 && TREE_TYPE (op1) != final_type)
3820 unsigned1 = TREE_UNSIGNED (TREE_TYPE (op1));
3822 /* Now UNSIGNED0 is 1 if ARG0 zero-extends to FINAL_TYPE. */
3824 /* For bitwise operations, signedness of nominal type
3825 does not matter. Consider only how operands were extended. */
3826 if (shorten == -1)
3827 uns = unsigned0;
3829 /* Note that in all three cases below we refrain from optimizing
3830 an unsigned operation on sign-extended args.
3831 That would not be valid. */
3833 /* Both args variable: if both extended in same way
3834 from same width, do it in that width.
3835 Do it unsigned if args were zero-extended. */
3836 if ((TYPE_PRECISION (TREE_TYPE (arg0))
3837 < TYPE_PRECISION (result_type))
3838 && (TYPE_PRECISION (TREE_TYPE (arg1))
3839 == TYPE_PRECISION (TREE_TYPE (arg0)))
3840 && unsigned0 == unsigned1
3841 && (unsigned0 || !uns))
3842 result_type
3843 = signed_or_unsigned_type (unsigned0,
3844 common_type (TREE_TYPE (arg0),
3845 TREE_TYPE (arg1)));
3846 else if (TREE_CODE (arg0) == INTEGER_CST
3847 && (unsigned1 || !uns)
3848 && (TYPE_PRECISION (TREE_TYPE (arg1))
3849 < TYPE_PRECISION (result_type))
3850 && (type = signed_or_unsigned_type (unsigned1,
3851 TREE_TYPE (arg1)),
3852 int_fits_type_p (arg0, type)))
3853 result_type = type;
3854 else if (TREE_CODE (arg1) == INTEGER_CST
3855 && (unsigned0 || !uns)
3856 && (TYPE_PRECISION (TREE_TYPE (arg0))
3857 < TYPE_PRECISION (result_type))
3858 && (type = signed_or_unsigned_type (unsigned0,
3859 TREE_TYPE (arg0)),
3860 int_fits_type_p (arg1, type)))
3861 result_type = type;
3864 /* Shifts can be shortened if shifting right. */
3866 if (short_shift)
3868 int unsigned_arg;
3869 tree arg0 = get_narrower (op0, &unsigned_arg);
3871 final_type = result_type;
3873 if (arg0 == op0 && final_type == TREE_TYPE (op0))
3874 unsigned_arg = TREE_UNSIGNED (TREE_TYPE (op0));
3876 if (TYPE_PRECISION (TREE_TYPE (arg0)) < TYPE_PRECISION (result_type)
3877 /* We can shorten only if the shift count is less than the
3878 number of bits in the smaller type size. */
3879 && compare_tree_int (op1, TYPE_PRECISION (TREE_TYPE (arg0))) < 0
3880 /* If arg is sign-extended and then unsigned-shifted,
3881 we can simulate this with a signed shift in arg's type
3882 only if the extended result is at least twice as wide
3883 as the arg. Otherwise, the shift could use up all the
3884 ones made by sign-extension and bring in zeros.
3885 We can't optimize that case at all, but in most machines
3886 it never happens because available widths are 2**N. */
3887 && (!TREE_UNSIGNED (final_type)
3888 || unsigned_arg
3889 || (((unsigned) 2 * TYPE_PRECISION (TREE_TYPE (arg0)))
3890 <= TYPE_PRECISION (result_type))))
3892 /* Do an unsigned shift if the operand was zero-extended. */
3893 result_type
3894 = signed_or_unsigned_type (unsigned_arg,
3895 TREE_TYPE (arg0));
3896 /* Convert value-to-be-shifted to that type. */
3897 if (TREE_TYPE (op0) != result_type)
3898 op0 = cp_convert (result_type, op0);
3899 converted = 1;
3903 /* Comparison operations are shortened too but differently.
3904 They identify themselves by setting short_compare = 1. */
3906 if (short_compare)
3908 /* Don't write &op0, etc., because that would prevent op0
3909 from being kept in a register.
3910 Instead, make copies of the our local variables and
3911 pass the copies by reference, then copy them back afterward. */
3912 tree xop0 = op0, xop1 = op1, xresult_type = result_type;
3913 enum tree_code xresultcode = resultcode;
3914 tree val
3915 = shorten_compare (&xop0, &xop1, &xresult_type, &xresultcode);
3916 if (val != 0)
3917 return cp_convert (boolean_type_node, val);
3918 op0 = xop0, op1 = xop1;
3919 converted = 1;
3920 resultcode = xresultcode;
3923 if ((short_compare || code == MIN_EXPR || code == MAX_EXPR)
3924 && warn_sign_compare)
3926 int op0_signed = ! TREE_UNSIGNED (TREE_TYPE (orig_op0));
3927 int op1_signed = ! TREE_UNSIGNED (TREE_TYPE (orig_op1));
3929 int unsignedp0, unsignedp1;
3930 tree primop0 = get_narrower (op0, &unsignedp0);
3931 tree primop1 = get_narrower (op1, &unsignedp1);
3933 /* Check for comparison of different enum types. */
3934 if (TREE_CODE (TREE_TYPE (orig_op0)) == ENUMERAL_TYPE
3935 && TREE_CODE (TREE_TYPE (orig_op1)) == ENUMERAL_TYPE
3936 && TYPE_MAIN_VARIANT (TREE_TYPE (orig_op0))
3937 != TYPE_MAIN_VARIANT (TREE_TYPE (orig_op1)))
3939 cp_warning ("comparison between types `%#T' and `%#T'",
3940 TREE_TYPE (orig_op0), TREE_TYPE (orig_op1));
3943 /* Give warnings for comparisons between signed and unsigned
3944 quantities that may fail. */
3945 /* Do the checking based on the original operand trees, so that
3946 casts will be considered, but default promotions won't be. */
3948 /* Do not warn if the comparison is being done in a signed type,
3949 since the signed type will only be chosen if it can represent
3950 all the values of the unsigned type. */
3951 if (! TREE_UNSIGNED (result_type))
3952 /* OK */;
3953 /* Do not warn if both operands are unsigned. */
3954 else if (op0_signed == op1_signed)
3955 /* OK */;
3956 /* Do not warn if the signed quantity is an unsuffixed
3957 integer literal (or some static constant expression
3958 involving such literals or a conditional expression
3959 involving such literals) and it is non-negative. */
3960 else if ((op0_signed && tree_expr_nonnegative_p (orig_op0))
3961 || (op1_signed && tree_expr_nonnegative_p (orig_op1)))
3962 /* OK */;
3963 /* Do not warn if the comparison is an equality operation,
3964 the unsigned quantity is an integral constant and it does
3965 not use the most significant bit of result_type. */
3966 else if ((resultcode == EQ_EXPR || resultcode == NE_EXPR)
3967 && ((op0_signed && TREE_CODE (orig_op1) == INTEGER_CST
3968 && int_fits_type_p (orig_op1,
3969 signed_type (result_type)))
3970 || (op1_signed && TREE_CODE (orig_op0) == INTEGER_CST
3971 && int_fits_type_p (orig_op0,
3972 signed_type (result_type)))))
3973 /* OK */;
3974 else
3975 warning ("comparison between signed and unsigned integer expressions");
3977 /* Warn if two unsigned values are being compared in a size
3978 larger than their original size, and one (and only one) is the
3979 result of a `~' operator. This comparison will always fail.
3981 Also warn if one operand is a constant, and the constant does not
3982 have all bits set that are set in the ~ operand when it is
3983 extended. */
3985 if ((TREE_CODE (primop0) == BIT_NOT_EXPR)
3986 ^ (TREE_CODE (primop1) == BIT_NOT_EXPR))
3988 if (TREE_CODE (primop0) == BIT_NOT_EXPR)
3989 primop0 = get_narrower (TREE_OPERAND (op0, 0), &unsignedp0);
3990 if (TREE_CODE (primop1) == BIT_NOT_EXPR)
3991 primop1 = get_narrower (TREE_OPERAND (op1, 0), &unsignedp1);
3993 if (host_integerp (primop0, 0) || host_integerp (primop1, 0))
3995 tree primop;
3996 HOST_WIDE_INT constant, mask;
3997 int unsignedp;
3998 unsigned int bits;
4000 if (host_integerp (primop0, 0))
4002 primop = primop1;
4003 unsignedp = unsignedp1;
4004 constant = tree_low_cst (primop0, 0);
4006 else
4008 primop = primop0;
4009 unsignedp = unsignedp0;
4010 constant = tree_low_cst (primop1, 0);
4013 bits = TYPE_PRECISION (TREE_TYPE (primop));
4014 if (bits < TYPE_PRECISION (result_type)
4015 && bits < HOST_BITS_PER_LONG && unsignedp)
4017 mask = (~ (HOST_WIDE_INT) 0) << bits;
4018 if ((mask & constant) != mask)
4019 warning ("comparison of promoted ~unsigned with constant");
4022 else if (unsignedp0 && unsignedp1
4023 && (TYPE_PRECISION (TREE_TYPE (primop0))
4024 < TYPE_PRECISION (result_type))
4025 && (TYPE_PRECISION (TREE_TYPE (primop1))
4026 < TYPE_PRECISION (result_type)))
4027 warning ("comparison of promoted ~unsigned with unsigned");
4032 /* At this point, RESULT_TYPE must be nonzero to avoid an error message.
4033 If CONVERTED is zero, both args will be converted to type RESULT_TYPE.
4034 Then the expression will be built.
4035 It will be given type FINAL_TYPE if that is nonzero;
4036 otherwise, it will be given type RESULT_TYPE. */
4038 if (!result_type)
4040 cp_error ("invalid operands of types `%T' and `%T' to binary `%O'",
4041 TREE_TYPE (orig_op0), TREE_TYPE (orig_op1), code);
4042 return error_mark_node;
4045 /* Issue warnings about peculiar, but legal, uses of NULL. */
4046 if (/* It's reasonable to use pointer values as operands of &&
4047 and ||, so NULL is no exception. */
4048 !(code == TRUTH_ANDIF_EXPR || code == TRUTH_ORIF_EXPR)
4049 && (/* If OP0 is NULL and OP1 is not a pointer, or vice versa. */
4050 (orig_op0 == null_node
4051 && TREE_CODE (TREE_TYPE (op1)) != POINTER_TYPE)
4052 /* Or vice versa. */
4053 || (orig_op1 == null_node
4054 && TREE_CODE (TREE_TYPE (op0)) != POINTER_TYPE)
4055 /* Or, both are NULL and the operation was not a comparison. */
4056 || (orig_op0 == null_node && orig_op1 == null_node
4057 && code != EQ_EXPR && code != NE_EXPR)))
4058 /* Some sort of arithmetic operation involving NULL was
4059 performed. Note that pointer-difference and pointer-addition
4060 have already been handled above, and so we don't end up here in
4061 that case. */
4062 cp_warning ("NULL used in arithmetic");
4064 if (! converted)
4066 if (TREE_TYPE (op0) != result_type)
4067 op0 = cp_convert (result_type, op0);
4068 if (TREE_TYPE (op1) != result_type)
4069 op1 = cp_convert (result_type, op1);
4071 if (op0 == error_mark_node || op1 == error_mark_node)
4072 return error_mark_node;
4075 if (build_type == NULL_TREE)
4076 build_type = result_type;
4079 register tree result = build (resultcode, build_type, op0, op1);
4080 register tree folded;
4082 folded = fold (result);
4083 if (folded == result)
4084 TREE_CONSTANT (folded) = TREE_CONSTANT (op0) & TREE_CONSTANT (op1);
4085 if (final_type != 0)
4086 return cp_convert (final_type, folded);
4087 return folded;
4091 /* Return a tree for the sum or difference (RESULTCODE says which)
4092 of pointer PTROP and integer INTOP. */
4094 static tree
4095 pointer_int_sum (resultcode, ptrop, intop)
4096 enum tree_code resultcode;
4097 register tree ptrop, intop;
4099 tree size_exp;
4101 register tree result;
4102 register tree folded = fold (intop);
4104 /* The result is a pointer of the same type that is being added. */
4106 register tree result_type = TREE_TYPE (ptrop);
4108 if (!complete_type_or_else (result_type, ptrop))
4109 return error_mark_node;
4111 if (TREE_CODE (TREE_TYPE (result_type)) == VOID_TYPE)
4113 if (pedantic || warn_pointer_arith)
4114 pedwarn ("ISO C++ forbids using pointer of type `void *' in pointer arithmetic");
4115 size_exp = integer_one_node;
4117 else if (TREE_CODE (TREE_TYPE (result_type)) == FUNCTION_TYPE)
4119 if (pedantic || warn_pointer_arith)
4120 pedwarn ("ISO C++ forbids using a pointer-to-function in pointer arithmetic");
4121 size_exp = integer_one_node;
4123 else if (TREE_CODE (TREE_TYPE (result_type)) == METHOD_TYPE)
4125 if (pedantic || warn_pointer_arith)
4126 pedwarn ("ISO C++ forbids using a pointer to member function in pointer arithmetic");
4127 size_exp = integer_one_node;
4129 else if (TREE_CODE (TREE_TYPE (result_type)) == OFFSET_TYPE)
4131 if (pedantic || warn_pointer_arith)
4132 pedwarn ("ISO C++ forbids using pointer to a member in pointer arithmetic");
4133 size_exp = integer_one_node;
4135 else
4136 size_exp = size_in_bytes (complete_type (TREE_TYPE (result_type)));
4138 /* Needed to make OOPS V2R3 work. */
4139 intop = folded;
4140 if (integer_zerop (intop))
4141 return ptrop;
4143 /* If what we are about to multiply by the size of the elements
4144 contains a constant term, apply distributive law
4145 and multiply that constant term separately.
4146 This helps produce common subexpressions. */
4148 if ((TREE_CODE (intop) == PLUS_EXPR || TREE_CODE (intop) == MINUS_EXPR)
4149 && ! TREE_CONSTANT (intop)
4150 && TREE_CONSTANT (TREE_OPERAND (intop, 1))
4151 && TREE_CONSTANT (size_exp))
4153 enum tree_code subcode = resultcode;
4154 if (TREE_CODE (intop) == MINUS_EXPR)
4155 subcode = (subcode == PLUS_EXPR ? MINUS_EXPR : PLUS_EXPR);
4156 ptrop = cp_build_binary_op (subcode, ptrop, TREE_OPERAND (intop, 1));
4157 intop = TREE_OPERAND (intop, 0);
4160 /* Convert the integer argument to a type the same size as sizetype
4161 so the multiply won't overflow spuriously. */
4163 if (TYPE_PRECISION (TREE_TYPE (intop)) != TYPE_PRECISION (sizetype))
4164 intop = cp_convert (type_for_size (TYPE_PRECISION (sizetype), 0), intop);
4166 /* Replace the integer argument with a suitable product by the object size.
4167 Do this multiplication as signed, then convert to the appropriate
4168 pointer type (actually unsigned integral). */
4170 intop = cp_convert (result_type,
4171 cp_build_binary_op (MULT_EXPR, intop,
4172 cp_convert (TREE_TYPE (intop),
4173 size_exp)));
4175 /* Create the sum or difference. */
4177 result = build (resultcode, result_type, ptrop, intop);
4179 folded = fold (result);
4180 if (folded == result)
4181 TREE_CONSTANT (folded) = TREE_CONSTANT (ptrop) & TREE_CONSTANT (intop);
4182 return folded;
4185 /* Return a tree for the difference of pointers OP0 and OP1.
4186 The resulting tree has type int. */
4188 static tree
4189 pointer_diff (op0, op1, ptrtype)
4190 register tree op0, op1;
4191 register tree ptrtype;
4193 register tree result, folded;
4194 tree restype = ptrdiff_type_node;
4195 tree target_type = TREE_TYPE (ptrtype);
4197 if (!complete_type_or_else (target_type, NULL_TREE))
4198 return error_mark_node;
4200 if (pedantic || warn_pointer_arith)
4202 if (TREE_CODE (target_type) == VOID_TYPE)
4203 pedwarn ("ISO C++ forbids using pointer of type `void *' in subtraction");
4204 if (TREE_CODE (target_type) == FUNCTION_TYPE)
4205 pedwarn ("ISO C++ forbids using pointer to a function in subtraction");
4206 if (TREE_CODE (target_type) == METHOD_TYPE)
4207 pedwarn ("ISO C++ forbids using pointer to a method in subtraction");
4208 if (TREE_CODE (target_type) == OFFSET_TYPE)
4209 pedwarn ("ISO C++ forbids using pointer to a member in subtraction");
4212 /* First do the subtraction as integers;
4213 then drop through to build the divide operator. */
4215 op0 = cp_build_binary_op (MINUS_EXPR,
4216 cp_convert (restype, op0),
4217 cp_convert (restype, op1));
4219 /* This generates an error if op1 is a pointer to an incomplete type. */
4220 if (!COMPLETE_TYPE_P (TREE_TYPE (TREE_TYPE (op1))))
4221 error ("invalid use of a pointer to an incomplete type in pointer arithmetic");
4223 op1 = ((TREE_CODE (target_type) == VOID_TYPE
4224 || TREE_CODE (target_type) == FUNCTION_TYPE
4225 || TREE_CODE (target_type) == METHOD_TYPE
4226 || TREE_CODE (target_type) == OFFSET_TYPE)
4227 ? integer_one_node
4228 : size_in_bytes (target_type));
4230 /* Do the division. */
4232 result = build (EXACT_DIV_EXPR, restype, op0, cp_convert (restype, op1));
4234 folded = fold (result);
4235 if (folded == result)
4236 TREE_CONSTANT (folded) = TREE_CONSTANT (op0) & TREE_CONSTANT (op1);
4237 return folded;
4240 /* Handle the case of taking the address of a COMPONENT_REF.
4241 Called by `build_unary_op'.
4243 ARG is the COMPONENT_REF whose address we want.
4244 ARGTYPE is the pointer type that this address should have. */
4246 static tree
4247 build_component_addr (arg, argtype)
4248 tree arg, argtype;
4250 tree field = TREE_OPERAND (arg, 1);
4251 tree basetype = decl_type_context (field);
4252 tree rval = build_unary_op (ADDR_EXPR, TREE_OPERAND (arg, 0), 0);
4254 my_friendly_assert (TREE_CODE (field) == FIELD_DECL, 981018);
4256 if (DECL_C_BIT_FIELD (field))
4258 cp_error ("attempt to take address of bit-field structure member `%D'",
4259 field);
4260 return error_mark_node;
4263 if (TREE_CODE (field) == FIELD_DECL
4264 && TYPE_BASE_CONVS_MAY_REQUIRE_CODE_P (basetype))
4266 /* Can't convert directly to ARGTYPE, since that
4267 may have the same pointer type as one of our
4268 baseclasses. */
4269 rval = build1 (NOP_EXPR, argtype,
4270 convert_pointer_to (basetype, rval));
4271 TREE_CONSTANT (rval) = TREE_CONSTANT (TREE_OPERAND (rval, 0));
4273 else
4274 /* This conversion is harmless. */
4275 rval = convert_force (argtype, rval, 0);
4277 return fold (build (PLUS_EXPR, argtype, rval,
4278 cp_convert (argtype, byte_position (field))));
4281 /* Construct and perhaps optimize a tree representation
4282 for a unary operation. CODE, a tree_code, specifies the operation
4283 and XARG is the operand. */
4285 tree
4286 build_x_unary_op (code, xarg)
4287 enum tree_code code;
4288 tree xarg;
4290 tree exp;
4291 int ptrmem = 0;
4293 if (processing_template_decl)
4294 return build_min_nt (code, xarg, NULL_TREE);
4296 /* & rec, on incomplete RECORD_TYPEs is the simple opr &, not an
4297 error message. */
4298 if (code == ADDR_EXPR
4299 && TREE_CODE (xarg) != TEMPLATE_ID_EXPR
4300 && ((IS_AGGR_TYPE_CODE (TREE_CODE (TREE_TYPE (xarg)))
4301 && !COMPLETE_TYPE_P (TREE_TYPE (xarg)))
4302 || (TREE_CODE (xarg) == OFFSET_REF)))
4303 /* don't look for a function */;
4304 else
4306 tree rval;
4308 rval = build_new_op (code, LOOKUP_NORMAL, xarg,
4309 NULL_TREE, NULL_TREE);
4310 if (rval || code != ADDR_EXPR)
4311 return rval;
4313 if (code == ADDR_EXPR)
4315 if (TREE_CODE (xarg) == OFFSET_REF)
4317 ptrmem = PTRMEM_OK_P (xarg);
4319 if (!ptrmem && !flag_ms_extensions
4320 && TREE_CODE (TREE_TYPE (TREE_OPERAND (xarg, 1))) == METHOD_TYPE)
4321 /* A single non-static member, make sure we don't allow a
4322 pointer-to-member. */
4323 xarg = ovl_cons (TREE_OPERAND (xarg, 1), NULL_TREE);
4325 else if (TREE_CODE (xarg) == TARGET_EXPR)
4326 warning ("taking address of temporary");
4328 exp = build_unary_op (code, xarg, 0);
4329 if (TREE_CODE (exp) == ADDR_EXPR)
4330 PTRMEM_OK_P (exp) = ptrmem;
4332 return exp;
4335 /* Like truthvalue_conversion, but handle pointer-to-member constants, where
4336 a null value is represented by an INTEGER_CST of -1. */
4338 tree
4339 cp_truthvalue_conversion (expr)
4340 tree expr;
4342 tree type = TREE_TYPE (expr);
4343 if (TYPE_PTRMEM_P (type))
4344 return build_binary_op (NE_EXPR, expr, integer_zero_node, 1);
4345 else
4346 return truthvalue_conversion (expr);
4349 /* Just like cp_truthvalue_conversion, but we want a CLEANUP_POINT_EXPR. */
4351 tree
4352 condition_conversion (expr)
4353 tree expr;
4355 tree t;
4356 if (processing_template_decl)
4357 return expr;
4358 t = perform_implicit_conversion (boolean_type_node, expr);
4359 t = fold (build1 (CLEANUP_POINT_EXPR, boolean_type_node, t));
4360 return t;
4363 /* C++: Must handle pointers to members.
4365 Perhaps type instantiation should be extended to handle conversion
4366 from aggregates to types we don't yet know we want? (Or are those
4367 cases typically errors which should be reported?)
4369 NOCONVERT nonzero suppresses the default promotions
4370 (such as from short to int). */
4372 tree
4373 build_unary_op (code, xarg, noconvert)
4374 enum tree_code code;
4375 tree xarg;
4376 int noconvert;
4378 /* No default_conversion here. It causes trouble for ADDR_EXPR. */
4379 register tree arg = xarg;
4380 register tree argtype = 0;
4381 const char *errstring = NULL;
4382 tree val;
4384 if (arg == error_mark_node)
4385 return error_mark_node;
4387 switch (code)
4389 case CONVERT_EXPR:
4390 /* This is used for unary plus, because a CONVERT_EXPR
4391 is enough to prevent anybody from looking inside for
4392 associativity, but won't generate any code. */
4393 if (!(arg = build_expr_type_conversion
4394 (WANT_ARITH | WANT_ENUM | WANT_POINTER, arg, 1)))
4395 errstring = "wrong type argument to unary plus";
4396 else
4398 if (!noconvert)
4399 arg = default_conversion (arg);
4400 arg = build1 (NON_LVALUE_EXPR, TREE_TYPE (arg), arg);
4401 TREE_CONSTANT (arg) = TREE_CONSTANT (TREE_OPERAND (arg, 0));
4403 break;
4405 case NEGATE_EXPR:
4406 if (!(arg = build_expr_type_conversion (WANT_ARITH | WANT_ENUM, arg, 1)))
4407 errstring = "wrong type argument to unary minus";
4408 else if (!noconvert)
4409 arg = default_conversion (arg);
4410 break;
4412 case BIT_NOT_EXPR:
4413 if (TREE_CODE (TREE_TYPE (arg)) == COMPLEX_TYPE)
4415 code = CONJ_EXPR;
4416 if (!noconvert)
4417 arg = default_conversion (arg);
4419 else if (!(arg = build_expr_type_conversion (WANT_INT | WANT_ENUM,
4420 arg, 1)))
4421 errstring = "wrong type argument to bit-complement";
4422 else if (!noconvert)
4423 arg = default_conversion (arg);
4424 break;
4426 case ABS_EXPR:
4427 if (!(arg = build_expr_type_conversion (WANT_ARITH | WANT_ENUM, arg, 1)))
4428 errstring = "wrong type argument to abs";
4429 else if (!noconvert)
4430 arg = default_conversion (arg);
4431 break;
4433 case CONJ_EXPR:
4434 /* Conjugating a real value is a no-op, but allow it anyway. */
4435 if (!(arg = build_expr_type_conversion (WANT_ARITH | WANT_ENUM, arg, 1)))
4436 errstring = "wrong type argument to conjugation";
4437 else if (!noconvert)
4438 arg = default_conversion (arg);
4439 break;
4441 case TRUTH_NOT_EXPR:
4442 arg = cp_convert (boolean_type_node, arg);
4443 val = invert_truthvalue (arg);
4444 if (arg != error_mark_node)
4445 return val;
4446 errstring = "in argument to unary !";
4447 break;
4449 case NOP_EXPR:
4450 break;
4452 case REALPART_EXPR:
4453 if (TREE_CODE (arg) == COMPLEX_CST)
4454 return TREE_REALPART (arg);
4455 else if (TREE_CODE (TREE_TYPE (arg)) == COMPLEX_TYPE)
4456 return fold (build1 (REALPART_EXPR, TREE_TYPE (TREE_TYPE (arg)), arg));
4457 else
4458 return arg;
4460 case IMAGPART_EXPR:
4461 if (TREE_CODE (arg) == COMPLEX_CST)
4462 return TREE_IMAGPART (arg);
4463 else if (TREE_CODE (TREE_TYPE (arg)) == COMPLEX_TYPE)
4464 return fold (build1 (IMAGPART_EXPR, TREE_TYPE (TREE_TYPE (arg)), arg));
4465 else
4466 return cp_convert (TREE_TYPE (arg), integer_zero_node);
4468 case PREINCREMENT_EXPR:
4469 case POSTINCREMENT_EXPR:
4470 case PREDECREMENT_EXPR:
4471 case POSTDECREMENT_EXPR:
4472 /* Handle complex lvalues (when permitted)
4473 by reduction to simpler cases. */
4475 val = unary_complex_lvalue (code, arg);
4476 if (val != 0)
4477 return val;
4479 /* Increment or decrement the real part of the value,
4480 and don't change the imaginary part. */
4481 if (TREE_CODE (TREE_TYPE (arg)) == COMPLEX_TYPE)
4483 tree real, imag;
4485 arg = stabilize_reference (arg);
4486 real = build_unary_op (REALPART_EXPR, arg, 1);
4487 imag = build_unary_op (IMAGPART_EXPR, arg, 1);
4488 return build (COMPLEX_EXPR, TREE_TYPE (arg),
4489 build_unary_op (code, real, 1), imag);
4492 /* Report invalid types. */
4494 if (!(arg = build_expr_type_conversion (WANT_ARITH | WANT_POINTER,
4495 arg, 1)))
4497 if (code == PREINCREMENT_EXPR)
4498 errstring ="no pre-increment operator for type";
4499 else if (code == POSTINCREMENT_EXPR)
4500 errstring ="no post-increment operator for type";
4501 else if (code == PREDECREMENT_EXPR)
4502 errstring ="no pre-decrement operator for type";
4503 else
4504 errstring ="no post-decrement operator for type";
4505 break;
4508 /* Report something read-only. */
4510 if (CP_TYPE_CONST_P (TREE_TYPE (arg))
4511 || TREE_READONLY (arg))
4512 readonly_error (arg, ((code == PREINCREMENT_EXPR
4513 || code == POSTINCREMENT_EXPR)
4514 ? "increment" : "decrement"),
4518 register tree inc;
4519 tree result_type = TREE_TYPE (arg);
4521 arg = get_unwidened (arg, 0);
4522 argtype = TREE_TYPE (arg);
4524 /* ARM $5.2.5 last annotation says this should be forbidden. */
4525 if (TREE_CODE (argtype) == ENUMERAL_TYPE)
4526 pedwarn ("ISO C++ forbids %sing an enum",
4527 (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
4528 ? "increment" : "decrement");
4530 /* Compute the increment. */
4532 if (TREE_CODE (argtype) == POINTER_TYPE)
4534 enum tree_code tmp = TREE_CODE (TREE_TYPE (argtype));
4535 tree type = complete_type (TREE_TYPE (argtype));
4537 if (!COMPLETE_OR_VOID_TYPE_P (type))
4538 cp_error ("cannot %s a pointer to incomplete type `%T'",
4539 ((code == PREINCREMENT_EXPR
4540 || code == POSTINCREMENT_EXPR)
4541 ? "increment" : "decrement"), TREE_TYPE (argtype));
4542 else if ((pedantic || warn_pointer_arith)
4543 && (tmp == FUNCTION_TYPE || tmp == METHOD_TYPE
4544 || tmp == VOID_TYPE || tmp == OFFSET_TYPE))
4545 cp_pedwarn ("ISO C++ forbids %sing a pointer of type `%T'",
4546 ((code == PREINCREMENT_EXPR
4547 || code == POSTINCREMENT_EXPR)
4548 ? "increment" : "decrement"), argtype);
4549 inc = c_sizeof_nowarn (TREE_TYPE (argtype));
4551 else
4552 inc = integer_one_node;
4554 inc = cp_convert (argtype, inc);
4556 /* Handle incrementing a cast-expression. */
4558 switch (TREE_CODE (arg))
4560 case NOP_EXPR:
4561 case CONVERT_EXPR:
4562 case FLOAT_EXPR:
4563 case FIX_TRUNC_EXPR:
4564 case FIX_FLOOR_EXPR:
4565 case FIX_ROUND_EXPR:
4566 case FIX_CEIL_EXPR:
4568 tree incremented, modify, value, compound;
4569 if (! lvalue_p (arg) && pedantic)
4570 pedwarn ("cast to non-reference type used as lvalue");
4571 arg = stabilize_reference (arg);
4572 if (code == PREINCREMENT_EXPR || code == PREDECREMENT_EXPR)
4573 value = arg;
4574 else
4575 value = save_expr (arg);
4576 incremented = build (((code == PREINCREMENT_EXPR
4577 || code == POSTINCREMENT_EXPR)
4578 ? PLUS_EXPR : MINUS_EXPR),
4579 argtype, value, inc);
4581 modify = build_modify_expr (arg, NOP_EXPR, incremented);
4582 compound = build (COMPOUND_EXPR, TREE_TYPE (arg), modify, value);
4584 /* Eliminate warning about unused result of + or -. */
4585 TREE_NO_UNUSED_WARNING (compound) = 1;
4586 return compound;
4589 default:
4590 break;
4593 /* Complain about anything else that is not a true lvalue. */
4594 if (!lvalue_or_else (arg, ((code == PREINCREMENT_EXPR
4595 || code == POSTINCREMENT_EXPR)
4596 ? "increment" : "decrement")))
4597 return error_mark_node;
4599 /* Forbid using -- on `bool'. */
4600 if (TREE_TYPE (arg) == boolean_type_node)
4602 if (code == POSTDECREMENT_EXPR || code == PREDECREMENT_EXPR)
4604 cp_error ("invalid use of `--' on bool variable `%D'", arg);
4605 return error_mark_node;
4607 #if 0
4608 /* This will only work if someone can convince Kenner to accept
4609 my patch to expand_increment. (jason) */
4610 val = build (code, TREE_TYPE (arg), arg, inc);
4611 #else
4612 val = boolean_increment (code, arg);
4613 #endif
4615 else
4616 val = build (code, TREE_TYPE (arg), arg, inc);
4618 TREE_SIDE_EFFECTS (val) = 1;
4619 return cp_convert (result_type, val);
4622 case ADDR_EXPR:
4623 /* Note that this operation never does default_conversion
4624 regardless of NOCONVERT. */
4626 argtype = lvalue_type (arg);
4627 if (TREE_CODE (argtype) == REFERENCE_TYPE)
4629 arg = build1
4630 (CONVERT_EXPR,
4631 build_pointer_type (TREE_TYPE (argtype)), arg);
4632 TREE_CONSTANT (arg) = TREE_CONSTANT (TREE_OPERAND (arg, 0));
4633 return arg;
4635 else if (pedantic && DECL_MAIN_P (arg))
4636 /* ARM $3.4 */
4637 pedwarn ("ISO C++ forbids taking address of function `::main'");
4639 /* Let &* cancel out to simplify resulting code. */
4640 if (TREE_CODE (arg) == INDIRECT_REF)
4642 /* We don't need to have `current_class_ptr' wrapped in a
4643 NON_LVALUE_EXPR node. */
4644 if (arg == current_class_ref)
4645 return current_class_ptr;
4647 arg = TREE_OPERAND (arg, 0);
4648 if (TREE_CODE (TREE_TYPE (arg)) == REFERENCE_TYPE)
4650 arg = build1
4651 (CONVERT_EXPR,
4652 build_pointer_type (TREE_TYPE (TREE_TYPE (arg))), arg);
4653 TREE_CONSTANT (arg) = TREE_CONSTANT (TREE_OPERAND (arg, 0));
4655 else if (lvalue_p (arg))
4656 /* Don't let this be an lvalue. */
4657 return non_lvalue (arg);
4658 return arg;
4661 /* For &x[y], return x+y */
4662 if (TREE_CODE (arg) == ARRAY_REF)
4664 if (mark_addressable (TREE_OPERAND (arg, 0)) == 0)
4665 return error_mark_node;
4666 return cp_build_binary_op (PLUS_EXPR, TREE_OPERAND (arg, 0),
4667 TREE_OPERAND (arg, 1));
4670 /* Uninstantiated types are all functions. Taking the
4671 address of a function is a no-op, so just return the
4672 argument. */
4674 if (TREE_CODE (arg) == IDENTIFIER_NODE
4675 && IDENTIFIER_OPNAME_P (arg))
4677 my_friendly_abort (117);
4678 /* We don't know the type yet, so just work around the problem.
4679 We know that this will resolve to an lvalue. */
4680 return build1 (ADDR_EXPR, unknown_type_node, arg);
4683 if (TREE_CODE (arg) == COMPONENT_REF && type_unknown_p (arg)
4684 && OVL_NEXT (TREE_OPERAND (arg, 1)) == NULL_TREE)
4686 /* They're trying to take the address of a unique non-static
4687 member function. This is ill-formed (except in MS-land),
4688 but let's try to DTRT.
4689 Note: We only handle unique functions here because we don't
4690 want to complain if there's a static overload; non-unique
4691 cases will be handled by instantiate_type. But we need to
4692 handle this case here to allow casts on the resulting PMF.
4693 We could defer this in non-MS mode, but it's easier to give
4694 a useful error here. */
4696 tree base = TREE_TYPE (TREE_OPERAND (arg, 0));
4697 tree name = DECL_NAME (OVL_CURRENT (TREE_OPERAND (arg, 1)));
4699 if (! flag_ms_extensions)
4701 if (current_class_type
4702 && TREE_OPERAND (arg, 0) == current_class_ref)
4703 /* An expression like &memfn. */
4704 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);
4705 else
4706 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);
4708 arg = build_offset_ref (base, name);
4711 if (type_unknown_p (arg))
4712 return build1 (ADDR_EXPR, unknown_type_node, arg);
4714 /* Handle complex lvalues (when permitted)
4715 by reduction to simpler cases. */
4716 val = unary_complex_lvalue (code, arg);
4717 if (val != 0)
4718 return val;
4720 switch (TREE_CODE (arg))
4722 case NOP_EXPR:
4723 case CONVERT_EXPR:
4724 case FLOAT_EXPR:
4725 case FIX_TRUNC_EXPR:
4726 case FIX_FLOOR_EXPR:
4727 case FIX_ROUND_EXPR:
4728 case FIX_CEIL_EXPR:
4729 if (! lvalue_p (arg) && pedantic)
4730 pedwarn ("ISO C++ forbids taking the address of a cast to a non-lvalue expression");
4731 break;
4733 default:
4734 break;
4737 /* Allow the address of a constructor if all the elements
4738 are constant. */
4739 if (TREE_CODE (arg) == CONSTRUCTOR && TREE_HAS_CONSTRUCTOR (arg)
4740 && TREE_CONSTANT (arg))
4742 /* Anything not already handled and not a true memory reference
4743 is an error. */
4744 else if (TREE_CODE (argtype) != FUNCTION_TYPE
4745 && TREE_CODE (argtype) != METHOD_TYPE
4746 && !lvalue_or_else (arg, "unary `&'"))
4747 return error_mark_node;
4749 if (argtype != error_mark_node)
4750 argtype = build_pointer_type (argtype);
4752 if (mark_addressable (arg) == 0)
4753 return error_mark_node;
4756 tree addr;
4758 if (TREE_CODE (arg) == COMPONENT_REF)
4759 addr = build_component_addr (arg, argtype);
4760 else
4761 addr = build1 (ADDR_EXPR, argtype, arg);
4763 /* Address of a static or external variable or
4764 function counts as a constant */
4765 if (staticp (arg))
4766 TREE_CONSTANT (addr) = 1;
4768 if (TREE_CODE (argtype) == POINTER_TYPE
4769 && TREE_CODE (TREE_TYPE (argtype)) == METHOD_TYPE)
4771 build_ptrmemfunc_type (argtype);
4772 addr = build_ptrmemfunc (argtype, addr, 0);
4775 return addr;
4778 default:
4779 break;
4782 if (!errstring)
4784 if (argtype == 0)
4785 argtype = TREE_TYPE (arg);
4786 return fold (build1 (code, argtype, arg));
4789 error ("%s", errstring);
4790 return error_mark_node;
4793 /* Apply unary lvalue-demanding operator CODE to the expression ARG
4794 for certain kinds of expressions which are not really lvalues
4795 but which we can accept as lvalues.
4797 If ARG is not a kind of expression we can handle, return zero. */
4799 tree
4800 unary_complex_lvalue (code, arg)
4801 enum tree_code code;
4802 tree arg;
4804 /* Handle (a, b) used as an "lvalue". */
4805 if (TREE_CODE (arg) == COMPOUND_EXPR)
4807 tree real_result = build_unary_op (code, TREE_OPERAND (arg, 1), 0);
4808 return build (COMPOUND_EXPR, TREE_TYPE (real_result),
4809 TREE_OPERAND (arg, 0), real_result);
4812 /* Handle (a ? b : c) used as an "lvalue". */
4813 if (TREE_CODE (arg) == COND_EXPR
4814 || TREE_CODE (arg) == MIN_EXPR || TREE_CODE (arg) == MAX_EXPR)
4815 return rationalize_conditional_expr (code, arg);
4817 if (TREE_CODE (arg) == MODIFY_EXPR
4818 || TREE_CODE (arg) == PREINCREMENT_EXPR
4819 || TREE_CODE (arg) == PREDECREMENT_EXPR)
4820 return unary_complex_lvalue
4821 (code, build (COMPOUND_EXPR, TREE_TYPE (TREE_OPERAND (arg, 0)),
4822 arg, TREE_OPERAND (arg, 0)));
4824 if (code != ADDR_EXPR)
4825 return 0;
4827 /* Handle (a = b) used as an "lvalue" for `&'. */
4828 if (TREE_CODE (arg) == MODIFY_EXPR
4829 || TREE_CODE (arg) == INIT_EXPR)
4831 tree real_result = build_unary_op (code, TREE_OPERAND (arg, 0), 0);
4832 arg = build (COMPOUND_EXPR, TREE_TYPE (real_result), arg, real_result);
4833 TREE_NO_UNUSED_WARNING (arg) = 1;
4834 return arg;
4837 if (TREE_CODE (TREE_TYPE (arg)) == FUNCTION_TYPE
4838 || TREE_CODE (TREE_TYPE (arg)) == METHOD_TYPE
4839 || TREE_CODE (TREE_TYPE (arg)) == OFFSET_TYPE)
4841 /* The representation of something of type OFFSET_TYPE
4842 is really the representation of a pointer to it.
4843 Here give the representation its true type. */
4844 tree t;
4846 my_friendly_assert (TREE_CODE (arg) != SCOPE_REF, 313);
4848 if (TREE_CODE (arg) != OFFSET_REF)
4849 return 0;
4851 t = TREE_OPERAND (arg, 1);
4853 /* Check all this code for right semantics. */
4854 if (TREE_CODE (t) == FUNCTION_DECL)
4856 if (DECL_DESTRUCTOR_P (t))
4857 cp_error ("taking address of destructor");
4858 return build_unary_op (ADDR_EXPR, t, 0);
4860 if (TREE_CODE (t) == VAR_DECL)
4861 return build_unary_op (ADDR_EXPR, t, 0);
4862 else
4864 tree type;
4866 if (TREE_OPERAND (arg, 0)
4867 && ! is_dummy_object (TREE_OPERAND (arg, 0))
4868 && TREE_CODE (t) != FIELD_DECL)
4870 cp_error ("taking address of bound pointer-to-member expression");
4871 return error_mark_node;
4874 type = build_offset_type (DECL_FIELD_CONTEXT (t), TREE_TYPE (t));
4875 type = build_pointer_type (type);
4877 t = make_ptrmem_cst (type, TREE_OPERAND (arg, 1));
4878 return t;
4883 /* We permit compiler to make function calls returning
4884 objects of aggregate type look like lvalues. */
4886 tree targ = arg;
4888 if (TREE_CODE (targ) == SAVE_EXPR)
4889 targ = TREE_OPERAND (targ, 0);
4891 if (TREE_CODE (targ) == CALL_EXPR && IS_AGGR_TYPE (TREE_TYPE (targ)))
4893 if (TREE_CODE (arg) == SAVE_EXPR)
4894 targ = arg;
4895 else
4896 targ = build_cplus_new (TREE_TYPE (arg), arg);
4897 return build1 (ADDR_EXPR, build_pointer_type (TREE_TYPE (arg)), targ);
4900 if (TREE_CODE (arg) == SAVE_EXPR && TREE_CODE (targ) == INDIRECT_REF)
4901 return build (SAVE_EXPR, build_pointer_type (TREE_TYPE (arg)),
4902 TREE_OPERAND (targ, 0), current_function_decl, NULL);
4905 /* Don't let anything else be handled specially. */
4906 return 0;
4909 /* Mark EXP saying that we need to be able to take the
4910 address of it; it should not be allocated in a register.
4911 Value is 1 if successful.
4913 C++: we do not allow `current_class_ptr' to be addressable. */
4916 mark_addressable (exp)
4917 tree exp;
4919 register tree x = exp;
4921 if (TREE_ADDRESSABLE (x) == 1)
4922 return 1;
4924 while (1)
4925 switch (TREE_CODE (x))
4927 case ADDR_EXPR:
4928 case COMPONENT_REF:
4929 case ARRAY_REF:
4930 case REALPART_EXPR:
4931 case IMAGPART_EXPR:
4932 x = TREE_OPERAND (x, 0);
4933 break;
4935 case PARM_DECL:
4936 if (x == current_class_ptr)
4938 error ("cannot take the address of `this', which is an rvalue expression");
4939 TREE_ADDRESSABLE (x) = 1; /* so compiler doesn't die later */
4940 return 1;
4942 case VAR_DECL:
4943 /* Caller should not be trying to mark initialized
4944 constant fields addressable. */
4945 my_friendly_assert (DECL_LANG_SPECIFIC (x) == 0
4946 || DECL_IN_AGGR_P (x) == 0
4947 || TREE_STATIC (x)
4948 || DECL_EXTERNAL (x), 314);
4950 case CONST_DECL:
4951 case RESULT_DECL:
4952 if (DECL_REGISTER (x) && !TREE_ADDRESSABLE (x)
4953 && !DECL_ARTIFICIAL (x) && extra_warnings)
4954 cp_warning ("address requested for `%D', which is declared `register'",
4956 TREE_ADDRESSABLE (x) = 1;
4957 return 1;
4959 case FUNCTION_DECL:
4960 TREE_ADDRESSABLE (x) = 1;
4961 TREE_ADDRESSABLE (DECL_ASSEMBLER_NAME (x)) = 1;
4962 return 1;
4964 case CONSTRUCTOR:
4965 TREE_ADDRESSABLE (x) = 1;
4966 return 1;
4968 case TARGET_EXPR:
4969 TREE_ADDRESSABLE (x) = 1;
4970 mark_addressable (TREE_OPERAND (x, 0));
4971 return 1;
4973 default:
4974 return 1;
4978 /* Build and return a conditional expression IFEXP ? OP1 : OP2. */
4980 tree
4981 build_x_conditional_expr (ifexp, op1, op2)
4982 tree ifexp, op1, op2;
4984 if (processing_template_decl)
4985 return build_min_nt (COND_EXPR, ifexp, op1, op2);
4987 return build_conditional_expr (ifexp, op1, op2);
4990 /* Handle overloading of the ',' operator when needed. Otherwise,
4991 this function just builds an expression list. */
4993 tree
4994 build_x_compound_expr (list)
4995 tree list;
4997 tree rest = TREE_CHAIN (list);
4998 tree result;
5000 if (processing_template_decl)
5001 return build_min_nt (COMPOUND_EXPR, list, NULL_TREE);
5003 if (rest == NULL_TREE)
5004 return build_compound_expr (list);
5006 result = build_opfncall (COMPOUND_EXPR, LOOKUP_NORMAL,
5007 TREE_VALUE (list), TREE_VALUE (rest), NULL_TREE);
5008 if (result)
5009 return build_x_compound_expr (tree_cons (NULL_TREE, result,
5010 TREE_CHAIN (rest)));
5012 if (! TREE_SIDE_EFFECTS (TREE_VALUE (list)))
5014 /* FIXME: This test should be in the implicit cast to void of the LHS. */
5015 /* the left-hand operand of a comma expression is like an expression
5016 statement: we should warn if it doesn't have any side-effects,
5017 unless it was explicitly cast to (void). */
5018 if ((extra_warnings || warn_unused_value)
5019 && !(TREE_CODE (TREE_VALUE(list)) == CONVERT_EXPR
5020 && VOID_TYPE_P (TREE_TYPE (TREE_VALUE(list)))))
5021 warning("left-hand operand of comma expression has no effect");
5023 #if 0 /* this requires a gcc backend patch to export warn_if_unused_value */
5024 else if (warn_unused_value)
5025 warn_if_unused_value (TREE_VALUE(list));
5026 #endif
5028 return build_compound_expr
5029 (tree_cons (NULL_TREE, TREE_VALUE (list),
5030 build_tree_list (NULL_TREE,
5031 build_x_compound_expr (rest))));
5034 /* Given a list of expressions, return a compound expression
5035 that performs them all and returns the value of the last of them. */
5037 tree
5038 build_compound_expr (list)
5039 tree list;
5041 register tree rest;
5042 tree first;
5044 TREE_VALUE (list) = decl_constant_value (TREE_VALUE (list));
5046 if (TREE_CHAIN (list) == 0)
5048 /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
5049 Strip such NOP_EXPRs, since LIST is used in non-lvalue context. */
5050 if (TREE_CODE (list) == NOP_EXPR
5051 && TREE_TYPE (list) == TREE_TYPE (TREE_OPERAND (list, 0)))
5052 list = TREE_OPERAND (list, 0);
5054 return TREE_VALUE (list);
5057 first = TREE_VALUE (list);
5058 first = convert_to_void (first, "left-hand operand of comma");
5059 if (first == error_mark_node)
5060 return error_mark_node;
5062 rest = build_compound_expr (TREE_CHAIN (list));
5063 if (rest == error_mark_node)
5064 return error_mark_node;
5066 /* When pedantic, a compound expression cannot be a constant expression. */
5067 if (! TREE_SIDE_EFFECTS (first) && ! pedantic)
5068 return rest;
5070 return build (COMPOUND_EXPR, TREE_TYPE (rest), first, rest);
5073 tree
5074 build_static_cast (type, expr)
5075 tree type, expr;
5077 tree intype;
5078 int ok;
5080 if (type == error_mark_node || expr == error_mark_node)
5081 return error_mark_node;
5083 if (TREE_CODE (expr) == OFFSET_REF)
5084 expr = resolve_offset_ref (expr);
5086 if (processing_template_decl)
5088 tree t = build_min (STATIC_CAST_EXPR, type, expr);
5089 return t;
5092 /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
5093 Strip such NOP_EXPRs if VALUE is being used in non-lvalue context. */
5094 if (TREE_CODE (type) != REFERENCE_TYPE
5095 && TREE_CODE (expr) == NOP_EXPR
5096 && TREE_TYPE (expr) == TREE_TYPE (TREE_OPERAND (expr, 0)))
5097 expr = TREE_OPERAND (expr, 0);
5099 if (TREE_CODE (type) == VOID_TYPE)
5101 expr = convert_to_void (expr, /*implicit=*/NULL);
5102 return expr;
5105 if (TREE_CODE (type) == REFERENCE_TYPE)
5106 return (convert_from_reference
5107 (convert_to_reference (type, expr, CONV_STATIC|CONV_IMPLICIT,
5108 LOOKUP_COMPLAIN, NULL_TREE)));
5110 if (IS_AGGR_TYPE (type))
5111 return build_cplus_new (type, (build_method_call
5112 (NULL_TREE, complete_ctor_identifier,
5113 build_tree_list (NULL_TREE, expr),
5114 TYPE_BINFO (type), LOOKUP_NORMAL)));
5116 expr = decay_conversion (expr);
5117 intype = TREE_TYPE (expr);
5119 /* FIXME handle casting to array type. */
5121 ok = 0;
5122 if (IS_AGGR_TYPE (intype)
5123 ? can_convert_arg (type, intype, expr)
5124 : can_convert_arg (strip_all_pointer_quals (type),
5125 strip_all_pointer_quals (intype), expr))
5126 /* This is a standard conversion. */
5127 ok = 1;
5128 else if (TYPE_PTROB_P (type) && TYPE_PTROB_P (intype))
5130 /* They're pointers to objects. They must be aggregates that
5131 are related non-virtually. */
5133 tree binfo;
5135 if (IS_AGGR_TYPE (TREE_TYPE (type)) && IS_AGGR_TYPE (TREE_TYPE (intype))
5136 && (binfo = get_binfo (TREE_TYPE (intype), TREE_TYPE (type), 0))
5137 && !binfo_from_vbase (binfo))
5138 ok = 1;
5140 else if (TREE_CODE (intype) != BOOLEAN_TYPE
5141 && TREE_CODE (type) != ARRAY_TYPE
5142 && TREE_CODE (type) != FUNCTION_TYPE
5143 && can_convert (intype, strip_all_pointer_quals (type)))
5144 ok = 1;
5145 else if (TREE_CODE (intype) == ENUMERAL_TYPE
5146 && TREE_CODE (type) == ENUMERAL_TYPE)
5147 /* DR 128: "A value of integral _or enumeration_ type can be explicitly
5148 converted to an enumeration type."
5149 The integral to enumeration will be accepted by the previous clause.
5150 We need to explicitly check for enumeration to enumeration. */
5151 ok = 1;
5153 /* [expr.static.cast]
5155 The static_cast operator shall not be used to cast away
5156 constness. */
5157 if (ok && casts_away_constness (intype, type))
5159 cp_error ("static_cast from type `%T' to type `%T' casts away constness",
5160 intype, type);
5161 return error_mark_node;
5164 if (ok)
5165 return build_c_cast (type, expr);
5167 cp_error ("invalid static_cast from type `%T' to type `%T'", intype, type);
5168 return error_mark_node;
5171 tree
5172 build_reinterpret_cast (type, expr)
5173 tree type, expr;
5175 tree intype;
5177 if (type == error_mark_node || expr == error_mark_node)
5178 return error_mark_node;
5180 if (TREE_CODE (expr) == OFFSET_REF)
5181 expr = resolve_offset_ref (expr);
5183 if (processing_template_decl)
5185 tree t = build_min (REINTERPRET_CAST_EXPR, type, expr);
5186 return t;
5189 if (TREE_CODE (type) != REFERENCE_TYPE)
5191 expr = decay_conversion (expr);
5193 /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
5194 Strip such NOP_EXPRs if VALUE is being used in non-lvalue context. */
5195 if (TREE_CODE (expr) == NOP_EXPR
5196 && TREE_TYPE (expr) == TREE_TYPE (TREE_OPERAND (expr, 0)))
5197 expr = TREE_OPERAND (expr, 0);
5200 intype = TREE_TYPE (expr);
5202 if (TREE_CODE (type) == REFERENCE_TYPE)
5204 if (! real_lvalue_p (expr))
5206 cp_error ("invalid reinterpret_cast of an rvalue expression of type `%T' to type `%T'", intype, type);
5207 return error_mark_node;
5209 expr = build_unary_op (ADDR_EXPR, expr, 0);
5210 if (expr != error_mark_node)
5211 expr = build_reinterpret_cast
5212 (build_pointer_type (TREE_TYPE (type)), expr);
5213 if (expr != error_mark_node)
5214 expr = build_indirect_ref (expr, 0);
5215 return expr;
5217 else if (same_type_ignoring_top_level_qualifiers_p (intype, type))
5218 return build_static_cast (type, expr);
5220 if (TYPE_PTR_P (type) && (TREE_CODE (intype) == INTEGER_TYPE
5221 || TREE_CODE (intype) == ENUMERAL_TYPE))
5222 /* OK */;
5223 else if (TREE_CODE (type) == INTEGER_TYPE && TYPE_PTR_P (intype))
5225 if (TYPE_PRECISION (type) < TYPE_PRECISION (intype))
5226 cp_pedwarn ("reinterpret_cast from `%T' to `%T' loses precision",
5227 intype, type);
5229 else if ((TYPE_PTRFN_P (type) && TYPE_PTRFN_P (intype))
5230 || (TYPE_PTRMEMFUNC_P (type) && TYPE_PTRMEMFUNC_P (intype)))
5232 expr = decl_constant_value (expr);
5233 return fold (build1 (NOP_EXPR, type, expr));
5235 else if ((TYPE_PTRMEM_P (type) && TYPE_PTRMEM_P (intype))
5236 || (TYPE_PTROBV_P (type) && TYPE_PTROBV_P (intype)))
5238 if (! comp_ptr_ttypes_reinterpret (TREE_TYPE (type), TREE_TYPE (intype)))
5239 cp_pedwarn ("reinterpret_cast from `%T' to `%T' casts away const (or volatile)",
5240 intype, type);
5242 expr = decl_constant_value (expr);
5243 return fold (build1 (NOP_EXPR, type, expr));
5245 else if ((TYPE_PTRFN_P (type) && TYPE_PTROBV_P (intype))
5246 || (TYPE_PTRFN_P (intype) && TYPE_PTROBV_P (type)))
5248 pedwarn ("ISO C++ forbids casting between pointer-to-function and pointer-to-object");
5249 expr = decl_constant_value (expr);
5250 return fold (build1 (NOP_EXPR, type, expr));
5252 else
5254 cp_error ("invalid reinterpret_cast from type `%T' to type `%T'",
5255 intype, type);
5256 return error_mark_node;
5259 return cp_convert (type, expr);
5262 tree
5263 build_const_cast (type, expr)
5264 tree type, expr;
5266 tree intype;
5268 if (type == error_mark_node || expr == error_mark_node)
5269 return error_mark_node;
5271 if (TREE_CODE (expr) == OFFSET_REF)
5272 expr = resolve_offset_ref (expr);
5274 if (processing_template_decl)
5276 tree t = build_min (CONST_CAST_EXPR, type, expr);
5277 return t;
5280 if (!POINTER_TYPE_P (type))
5281 cp_error ("invalid use of const_cast with type `%T', which is not a pointer, reference, nor a pointer-to-data-member type", type);
5282 else if (TREE_CODE (TREE_TYPE (type)) == FUNCTION_TYPE)
5284 cp_error ("invalid use of const_cast with type `%T', which is a pointer or reference to a function type", type);
5285 return error_mark_node;
5288 if (TREE_CODE (type) != REFERENCE_TYPE)
5290 expr = decay_conversion (expr);
5292 /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
5293 Strip such NOP_EXPRs if VALUE is being used in non-lvalue context. */
5294 if (TREE_CODE (expr) == NOP_EXPR
5295 && TREE_TYPE (expr) == TREE_TYPE (TREE_OPERAND (expr, 0)))
5296 expr = TREE_OPERAND (expr, 0);
5299 intype = TREE_TYPE (expr);
5301 if (same_type_ignoring_top_level_qualifiers_p (intype, type))
5302 return build_static_cast (type, expr);
5303 else if (TREE_CODE (type) == REFERENCE_TYPE)
5305 if (! real_lvalue_p (expr))
5307 cp_error ("invalid const_cast of an rvalue of type `%T' to type `%T'", intype, type);
5308 return error_mark_node;
5311 if (comp_ptr_ttypes_const (TREE_TYPE (type), intype))
5313 expr = build_unary_op (ADDR_EXPR, expr, 0);
5314 expr = build1 (NOP_EXPR, type, expr);
5315 return convert_from_reference (expr);
5318 else if (TREE_CODE (type) == POINTER_TYPE
5319 && TREE_CODE (intype) == POINTER_TYPE
5320 && comp_ptr_ttypes_const (TREE_TYPE (type), TREE_TYPE (intype)))
5321 return cp_convert (type, expr);
5323 cp_error ("invalid const_cast from type `%T' to type `%T'", intype, type);
5324 return error_mark_node;
5327 /* Build an expression representing a cast to type TYPE of expression EXPR.
5329 ALLOW_NONCONVERTING is true if we should allow non-converting constructors
5330 when doing the cast. */
5332 tree
5333 build_c_cast (type, expr)
5334 tree type, expr;
5336 register tree value = expr;
5337 tree otype;
5339 if (type == error_mark_node || expr == error_mark_node)
5340 return error_mark_node;
5342 if (processing_template_decl)
5344 tree t = build_min (CAST_EXPR, type,
5345 tree_cons (NULL_TREE, value, NULL_TREE));
5346 return t;
5349 /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
5350 Strip such NOP_EXPRs if VALUE is being used in non-lvalue context. */
5351 if (TREE_CODE (type) != REFERENCE_TYPE
5352 && TREE_CODE (value) == NOP_EXPR
5353 && TREE_TYPE (value) == TREE_TYPE (TREE_OPERAND (value, 0)))
5354 value = TREE_OPERAND (value, 0);
5356 if (TREE_CODE (value) == OFFSET_REF)
5357 value = resolve_offset_ref (value);
5359 if (TREE_CODE (type) == ARRAY_TYPE)
5361 /* Allow casting from T1* to T2[] because Cfront allows it.
5362 NIHCL uses it. It is not valid ISO C++ however. */
5363 if (TREE_CODE (TREE_TYPE (expr)) == POINTER_TYPE)
5365 cp_pedwarn ("ISO C++ forbids casting to an array type `%T'", type);
5366 type = build_pointer_type (TREE_TYPE (type));
5368 else
5370 cp_error ("ISO C++ forbids casting to an array type `%T'", type);
5371 return error_mark_node;
5375 if (TREE_CODE (type) == FUNCTION_TYPE
5376 || TREE_CODE (type) == METHOD_TYPE)
5378 cp_error ("invalid cast to function type `%T'", type);
5379 return error_mark_node;
5382 if (TREE_CODE (type) == VOID_TYPE)
5384 /* Conversion to void does not cause any of the normal function to
5385 * pointer, array to pointer and lvalue to rvalue decays. */
5387 value = convert_to_void (value, /*implicit=*/NULL);
5388 return value;
5390 /* Convert functions and arrays to pointers and
5391 convert references to their expanded types,
5392 but don't convert any other types. If, however, we are
5393 casting to a class type, there's no reason to do this: the
5394 cast will only succeed if there is a converting constructor,
5395 and the default conversions will be done at that point. In
5396 fact, doing the default conversion here is actually harmful
5397 in cases like this:
5399 typedef int A[2];
5400 struct S { S(const A&); };
5402 since we don't want the array-to-pointer conversion done. */
5403 if (!IS_AGGR_TYPE (type))
5405 if (TREE_CODE (TREE_TYPE (value)) == FUNCTION_TYPE
5406 || (TREE_CODE (TREE_TYPE (value)) == METHOD_TYPE
5407 /* Don't do the default conversion on a ->* expression. */
5408 && ! (TREE_CODE (type) == POINTER_TYPE
5409 && bound_pmf_p (value)))
5410 || TREE_CODE (TREE_TYPE (value)) == ARRAY_TYPE
5411 || TREE_CODE (TREE_TYPE (value)) == REFERENCE_TYPE)
5412 value = default_conversion (value);
5414 else if (TREE_CODE (TREE_TYPE (value)) == REFERENCE_TYPE)
5415 /* However, even for class types, we still need to strip away
5416 the reference type, since the call to convert_force below
5417 does not expect the input expression to be of reference
5418 type. */
5419 value = convert_from_reference (value);
5421 otype = TREE_TYPE (value);
5423 /* Optionally warn about potentially worrisome casts. */
5425 if (warn_cast_qual
5426 && TREE_CODE (type) == POINTER_TYPE
5427 && TREE_CODE (otype) == POINTER_TYPE
5428 && !at_least_as_qualified_p (TREE_TYPE (type),
5429 TREE_TYPE (otype)))
5430 cp_warning ("cast from `%T' to `%T' discards qualifiers from pointer target type",
5431 otype, type);
5433 if (TREE_CODE (type) == INTEGER_TYPE
5434 && TREE_CODE (otype) == POINTER_TYPE
5435 && TYPE_PRECISION (type) != TYPE_PRECISION (otype))
5436 warning ("cast from pointer to integer of different size");
5438 if (TREE_CODE (type) == POINTER_TYPE
5439 && TREE_CODE (otype) == INTEGER_TYPE
5440 && TYPE_PRECISION (type) != TYPE_PRECISION (otype)
5441 /* Don't warn about converting any constant. */
5442 && !TREE_CONSTANT (value))
5443 warning ("cast to pointer from integer of different size");
5445 if (TREE_CODE (type) == REFERENCE_TYPE)
5446 value = (convert_from_reference
5447 (convert_to_reference (type, value, CONV_C_CAST,
5448 LOOKUP_COMPLAIN, NULL_TREE)));
5449 else
5451 tree ovalue;
5453 value = decl_constant_value (value);
5455 ovalue = value;
5456 value = convert_force (type, value, CONV_C_CAST);
5458 /* Ignore any integer overflow caused by the cast. */
5459 if (TREE_CODE (value) == INTEGER_CST)
5461 TREE_OVERFLOW (value) = TREE_OVERFLOW (ovalue);
5462 TREE_CONSTANT_OVERFLOW (value) = TREE_CONSTANT_OVERFLOW (ovalue);
5466 /* Warn about possible alignment problems. Do this here when we will have
5467 instantiated any necessary template types. */
5468 if (STRICT_ALIGNMENT && warn_cast_align
5469 && TREE_CODE (type) == POINTER_TYPE
5470 && TREE_CODE (otype) == POINTER_TYPE
5471 && TREE_CODE (TREE_TYPE (otype)) != VOID_TYPE
5472 && TREE_CODE (TREE_TYPE (otype)) != FUNCTION_TYPE
5473 && COMPLETE_TYPE_P (TREE_TYPE (otype))
5474 && COMPLETE_TYPE_P (TREE_TYPE (type))
5475 && TYPE_ALIGN (TREE_TYPE (type)) > TYPE_ALIGN (TREE_TYPE (otype)))
5476 cp_warning ("cast from `%T' to `%T' increases required alignment of target type",
5477 otype, type);
5479 /* Always produce some operator for an explicit cast,
5480 so we can tell (for -pedantic) that the cast is no lvalue. */
5481 if (TREE_CODE (type) != REFERENCE_TYPE && value == expr
5482 && real_lvalue_p (value))
5483 value = non_lvalue (value);
5485 return value;
5488 /* Build an assignment expression of lvalue LHS from value RHS.
5489 MODIFYCODE is the code for a binary operator that we use
5490 to combine the old value of LHS with RHS to get the new value.
5491 Or else MODIFYCODE is NOP_EXPR meaning do a simple assignment.
5493 C++: If MODIFYCODE is INIT_EXPR, then leave references unbashed. */
5495 tree
5496 build_modify_expr (lhs, modifycode, rhs)
5497 tree lhs;
5498 enum tree_code modifycode;
5499 tree rhs;
5501 register tree result;
5502 tree newrhs = rhs;
5503 tree lhstype = TREE_TYPE (lhs);
5504 tree olhstype = lhstype;
5505 tree olhs = lhs;
5507 /* Avoid duplicate error messages from operands that had errors. */
5508 if (lhs == error_mark_node || rhs == error_mark_node)
5509 return error_mark_node;
5511 /* Types that aren't fully specified cannot be used in assignments. */
5512 lhs = require_complete_type (lhs);
5514 newrhs = rhs;
5516 /* Handle control structure constructs used as "lvalues". */
5518 switch (TREE_CODE (lhs))
5520 /* Handle --foo = 5; as these are valid constructs in C++ */
5521 case PREDECREMENT_EXPR:
5522 case PREINCREMENT_EXPR:
5523 if (TREE_SIDE_EFFECTS (TREE_OPERAND (lhs, 0)))
5524 lhs = build (TREE_CODE (lhs), TREE_TYPE (lhs),
5525 stabilize_reference (TREE_OPERAND (lhs, 0)),
5526 TREE_OPERAND (lhs, 1));
5527 return build (COMPOUND_EXPR, lhstype,
5528 lhs,
5529 build_modify_expr (TREE_OPERAND (lhs, 0),
5530 modifycode, rhs));
5532 /* Handle (a, b) used as an "lvalue". */
5533 case COMPOUND_EXPR:
5534 newrhs = build_modify_expr (TREE_OPERAND (lhs, 1),
5535 modifycode, rhs);
5536 if (newrhs == error_mark_node)
5537 return error_mark_node;
5538 return build (COMPOUND_EXPR, lhstype,
5539 TREE_OPERAND (lhs, 0), newrhs);
5541 case MODIFY_EXPR:
5542 newrhs = build_modify_expr (TREE_OPERAND (lhs, 0), modifycode, rhs);
5543 if (newrhs == error_mark_node)
5544 return error_mark_node;
5545 return build (COMPOUND_EXPR, lhstype, lhs, newrhs);
5547 /* Handle (a ? b : c) used as an "lvalue". */
5548 case COND_EXPR:
5549 rhs = save_expr (rhs);
5551 /* Produce (a ? (b = rhs) : (c = rhs))
5552 except that the RHS goes through a save-expr
5553 so the code to compute it is only emitted once. */
5554 tree cond;
5556 /* Check this here to avoid odd errors when trying to convert
5557 a throw to the type of the COND_EXPR. */
5558 if (!lvalue_or_else (lhs, "assignment"))
5559 return error_mark_node;
5561 cond = build_conditional_expr
5562 (TREE_OPERAND (lhs, 0),
5563 build_modify_expr (cp_convert (TREE_TYPE (lhs),
5564 TREE_OPERAND (lhs, 1)),
5565 modifycode, rhs),
5566 build_modify_expr (cp_convert (TREE_TYPE (lhs),
5567 TREE_OPERAND (lhs, 2)),
5568 modifycode, rhs));
5570 if (cond == error_mark_node)
5571 return cond;
5572 /* Make sure the code to compute the rhs comes out
5573 before the split. */
5574 return build (COMPOUND_EXPR, TREE_TYPE (lhs),
5575 /* Case to void to suppress warning
5576 from warn_if_unused_value. */
5577 cp_convert (void_type_node, rhs), cond);
5580 default:
5581 break;
5584 if (TREE_CODE (lhs) == OFFSET_REF)
5586 if (TREE_OPERAND (lhs, 0) == NULL_TREE)
5588 /* Static class member? */
5589 tree member = TREE_OPERAND (lhs, 1);
5590 if (TREE_CODE (member) == VAR_DECL)
5591 lhs = member;
5592 else
5594 compiler_error ("invalid static class member");
5595 return error_mark_node;
5598 else
5599 lhs = resolve_offset_ref (lhs);
5601 olhstype = lhstype = TREE_TYPE (lhs);
5604 if (lhs == error_mark_node)
5605 return lhs;
5607 if (TREE_CODE (lhstype) == REFERENCE_TYPE
5608 && modifycode != INIT_EXPR)
5610 lhs = convert_from_reference (lhs);
5611 olhstype = lhstype = TREE_TYPE (lhs);
5614 /* If a binary op has been requested, combine the old LHS value with the RHS
5615 producing the value we should actually store into the LHS. */
5617 if (modifycode == INIT_EXPR)
5619 if (TREE_CODE (rhs) == CONSTRUCTOR)
5621 if (! same_type_p (TREE_TYPE (rhs), lhstype))
5622 abort ();
5623 result = build (INIT_EXPR, lhstype, lhs, rhs);
5624 TREE_SIDE_EFFECTS (result) = 1;
5625 return result;
5627 else if (! IS_AGGR_TYPE (lhstype))
5628 /* Do the default thing */;
5629 else
5631 result = build_method_call (lhs, complete_ctor_identifier,
5632 build_tree_list (NULL_TREE, rhs),
5633 TYPE_BINFO (lhstype), LOOKUP_NORMAL);
5634 if (result == NULL_TREE)
5635 return error_mark_node;
5636 return result;
5639 else if (modifycode == NOP_EXPR)
5641 /* `operator=' is not an inheritable operator. */
5642 if (! IS_AGGR_TYPE (lhstype))
5643 /* Do the default thing */;
5644 else
5646 result = build_opfncall (MODIFY_EXPR, LOOKUP_NORMAL,
5647 lhs, rhs, make_node (NOP_EXPR));
5648 if (result == NULL_TREE)
5649 return error_mark_node;
5650 return result;
5652 lhstype = olhstype;
5654 else if (PROMOTES_TO_AGGR_TYPE (lhstype, REFERENCE_TYPE))
5656 my_friendly_abort (978652);
5658 else
5660 lhs = stabilize_reference (lhs);
5661 newrhs = cp_build_binary_op (modifycode, lhs, rhs);
5662 if (newrhs == error_mark_node)
5664 cp_error (" in evaluation of `%Q(%#T, %#T)'", modifycode,
5665 TREE_TYPE (lhs), TREE_TYPE (rhs));
5666 return error_mark_node;
5670 /* Handle a cast used as an "lvalue".
5671 We have already performed any binary operator using the value as cast.
5672 Now convert the result to the cast type of the lhs,
5673 and then true type of the lhs and store it there;
5674 then convert result back to the cast type to be the value
5675 of the assignment. */
5677 switch (TREE_CODE (lhs))
5679 case NOP_EXPR:
5680 case CONVERT_EXPR:
5681 case FLOAT_EXPR:
5682 case FIX_TRUNC_EXPR:
5683 case FIX_FLOOR_EXPR:
5684 case FIX_ROUND_EXPR:
5685 case FIX_CEIL_EXPR:
5686 if (TREE_CODE (TREE_TYPE (newrhs)) == ARRAY_TYPE
5687 || TREE_CODE (TREE_TYPE (newrhs)) == FUNCTION_TYPE
5688 || TREE_CODE (TREE_TYPE (newrhs)) == METHOD_TYPE
5689 || TREE_CODE (TREE_TYPE (newrhs)) == OFFSET_TYPE)
5690 newrhs = default_conversion (newrhs);
5692 tree inner_lhs = TREE_OPERAND (lhs, 0);
5693 tree result;
5695 /* ISO C++ 5.4/1: The result is an lvalue if T is a reference
5696 type, otherwise the result is an rvalue. */
5697 if (! lvalue_p (lhs))
5698 pedwarn ("ISO C++ forbids cast to non-reference type used as lvalue");
5700 result = build_modify_expr (inner_lhs, NOP_EXPR,
5701 cp_convert (TREE_TYPE (inner_lhs),
5702 cp_convert (lhstype, newrhs)));
5703 if (result == error_mark_node)
5704 return result;
5705 return cp_convert (TREE_TYPE (lhs), result);
5708 default:
5709 break;
5712 /* Now we have handled acceptable kinds of LHS that are not truly lvalues.
5713 Reject anything strange now. */
5715 if (!lvalue_or_else (lhs, "assignment"))
5716 return error_mark_node;
5718 GNU_xref_assign (lhs);
5720 /* Warn about storing in something that is `const'. */
5721 /* For C++, don't warn if this is initialization. */
5722 if (modifycode != INIT_EXPR
5723 && (TREE_READONLY (lhs) || CP_TYPE_CONST_P (lhstype)
5724 /* Functions are not modifiable, even though they are
5725 lvalues. */
5726 || TREE_CODE (TREE_TYPE (lhs)) == FUNCTION_TYPE
5727 || (IS_AGGR_TYPE_CODE (TREE_CODE (lhstype))
5728 && C_TYPE_FIELDS_READONLY (lhstype))
5729 || (TREE_CODE (lhstype) == REFERENCE_TYPE
5730 && CP_TYPE_CONST_P (TREE_TYPE (lhstype)))))
5731 readonly_error (lhs, "assignment", 0);
5733 /* If storing into a structure or union member,
5734 it has probably been given type `int'.
5735 Compute the type that would go with
5736 the actual amount of storage the member occupies. */
5738 if (TREE_CODE (lhs) == COMPONENT_REF
5739 && (TREE_CODE (lhstype) == INTEGER_TYPE
5740 || TREE_CODE (lhstype) == REAL_TYPE
5741 || TREE_CODE (lhstype) == ENUMERAL_TYPE))
5743 lhstype = TREE_TYPE (get_unwidened (lhs, 0));
5745 /* If storing in a field that is in actuality a short or narrower
5746 than one, we must store in the field in its actual type. */
5748 if (lhstype != TREE_TYPE (lhs))
5750 lhs = copy_node (lhs);
5751 TREE_TYPE (lhs) = lhstype;
5755 if (modifycode != INIT_EXPR)
5757 /* Make modifycode now either a NOP_EXPR or an INIT_EXPR. */
5758 modifycode = NOP_EXPR;
5759 /* Reference-bashing */
5760 if (TREE_CODE (lhstype) == REFERENCE_TYPE)
5762 tree tmp = convert_from_reference (lhs);
5763 lhstype = TREE_TYPE (tmp);
5764 if (!COMPLETE_TYPE_P (lhstype))
5766 incomplete_type_error (lhs, lhstype);
5767 return error_mark_node;
5769 lhs = tmp;
5770 olhstype = lhstype;
5772 if (TREE_CODE (TREE_TYPE (newrhs)) == REFERENCE_TYPE)
5774 tree tmp = convert_from_reference (newrhs);
5775 if (!COMPLETE_TYPE_P (TREE_TYPE (tmp)))
5777 incomplete_type_error (newrhs, TREE_TYPE (tmp));
5778 return error_mark_node;
5780 newrhs = tmp;
5784 if (TREE_SIDE_EFFECTS (lhs))
5785 lhs = stabilize_reference (lhs);
5786 if (TREE_SIDE_EFFECTS (newrhs))
5787 newrhs = stabilize_reference (newrhs);
5789 /* Convert new value to destination type. */
5791 if (TREE_CODE (lhstype) == ARRAY_TYPE)
5793 int from_array;
5795 if (!same_or_base_type_p (lhstype, TREE_TYPE (rhs)))
5797 cp_error ("incompatible types in assignment of `%T' to `%T'",
5798 TREE_TYPE (rhs), lhstype);
5799 return error_mark_node;
5802 /* Allow array assignment in compiler-generated code. */
5803 if (pedantic && ! DECL_ARTIFICIAL (current_function_decl))
5804 pedwarn ("ISO C++ forbids assignment of arrays");
5806 from_array = TREE_CODE (TREE_TYPE (newrhs)) == ARRAY_TYPE
5807 ? 1 + (modifycode != INIT_EXPR): 0;
5808 return build_vec_init (lhs, newrhs, from_array);
5811 if (modifycode == INIT_EXPR)
5813 newrhs = convert_for_initialization (lhs, lhstype, newrhs, LOOKUP_NORMAL,
5814 "initialization", NULL_TREE, 0);
5815 if (current_function_decl &&
5816 lhs == DECL_RESULT (current_function_decl))
5818 if (DECL_INITIAL (lhs))
5819 warning ("return value from function receives multiple initializations");
5820 DECL_INITIAL (lhs) = newrhs;
5823 else
5825 /* Avoid warnings on enum bit fields. */
5826 if (TREE_CODE (olhstype) == ENUMERAL_TYPE
5827 && TREE_CODE (lhstype) == INTEGER_TYPE)
5829 newrhs = convert_for_assignment (olhstype, newrhs, "assignment",
5830 NULL_TREE, 0);
5831 newrhs = convert_force (lhstype, newrhs, 0);
5833 else
5834 newrhs = convert_for_assignment (lhstype, newrhs, "assignment",
5835 NULL_TREE, 0);
5836 if (TREE_CODE (newrhs) == CALL_EXPR
5837 && TYPE_NEEDS_CONSTRUCTING (lhstype))
5838 newrhs = build_cplus_new (lhstype, newrhs);
5840 /* Can't initialize directly from a TARGET_EXPR, since that would
5841 cause the lhs to be constructed twice, and possibly result in
5842 accidental self-initialization. So we force the TARGET_EXPR to be
5843 expanded without a target. */
5844 if (TREE_CODE (newrhs) == TARGET_EXPR)
5845 newrhs = build (COMPOUND_EXPR, TREE_TYPE (newrhs), newrhs,
5846 TREE_OPERAND (newrhs, 0));
5849 if (newrhs == error_mark_node)
5850 return error_mark_node;
5852 if (TREE_CODE (newrhs) == COND_EXPR)
5854 tree lhs1;
5855 tree cond = TREE_OPERAND (newrhs, 0);
5857 if (TREE_SIDE_EFFECTS (lhs))
5858 cond = build_compound_expr (tree_cons
5859 (NULL_TREE, lhs,
5860 build_tree_list (NULL_TREE, cond)));
5862 /* Cannot have two identical lhs on this one tree (result) as preexpand
5863 calls will rip them out and fill in RTL for them, but when the
5864 rtl is generated, the calls will only be in the first side of the
5865 condition, not on both, or before the conditional jump! (mrs) */
5866 lhs1 = break_out_calls (lhs);
5868 if (lhs == lhs1)
5869 /* If there's no change, the COND_EXPR behaves like any other rhs. */
5870 result = build (modifycode == NOP_EXPR ? MODIFY_EXPR : INIT_EXPR,
5871 lhstype, lhs, newrhs);
5872 else
5874 tree result_type = TREE_TYPE (newrhs);
5875 /* We have to convert each arm to the proper type because the
5876 types may have been munged by constant folding. */
5877 result
5878 = build (COND_EXPR, result_type, cond,
5879 build_modify_expr (lhs, modifycode,
5880 cp_convert (result_type,
5881 TREE_OPERAND (newrhs, 1))),
5882 build_modify_expr (lhs1, modifycode,
5883 cp_convert (result_type,
5884 TREE_OPERAND (newrhs, 2))));
5887 else
5888 result = build (modifycode == NOP_EXPR ? MODIFY_EXPR : INIT_EXPR,
5889 lhstype, lhs, newrhs);
5891 TREE_SIDE_EFFECTS (result) = 1;
5893 /* If we got the LHS in a different type for storing in,
5894 convert the result back to the nominal type of LHS
5895 so that the value we return always has the same type
5896 as the LHS argument. */
5898 if (olhstype == TREE_TYPE (result))
5899 return result;
5900 /* Avoid warnings converting integral types back into enums
5901 for enum bit fields. */
5902 if (TREE_CODE (TREE_TYPE (result)) == INTEGER_TYPE
5903 && TREE_CODE (olhstype) == ENUMERAL_TYPE)
5905 result = build (COMPOUND_EXPR, olhstype, result, olhs);
5906 TREE_NO_UNUSED_WARNING (result) = 1;
5907 return result;
5909 return convert_for_assignment (olhstype, result, "assignment",
5910 NULL_TREE, 0);
5913 tree
5914 build_x_modify_expr (lhs, modifycode, rhs)
5915 tree lhs;
5916 enum tree_code modifycode;
5917 tree rhs;
5919 if (processing_template_decl)
5920 return build_min_nt (MODOP_EXPR, lhs,
5921 build_min_nt (modifycode, NULL_TREE, NULL_TREE), rhs);
5923 if (modifycode != NOP_EXPR)
5925 tree rval = build_opfncall (MODIFY_EXPR, LOOKUP_NORMAL, lhs, rhs,
5926 make_node (modifycode));
5927 if (rval)
5928 return rval;
5930 return build_modify_expr (lhs, modifycode, rhs);
5934 /* Get difference in deltas for different pointer to member function
5935 types. Return integer_zero_node, if FROM cannot be converted to a
5936 TO type. If FORCE is true, then allow reverse conversions as well.
5938 Note that the naming of FROM and TO is kind of backwards; the return
5939 value is what we add to a TO in order to get a FROM. They are named
5940 this way because we call this function to find out how to convert from
5941 a pointer to member of FROM to a pointer to member of TO. */
5943 static tree
5944 get_delta_difference (from, to, force)
5945 tree from, to;
5946 int force;
5948 tree delta = integer_zero_node;
5949 tree binfo;
5950 tree virt_binfo;
5952 if (to == from)
5953 return delta;
5955 /* Should get_base_distance here, so we can check if any thing along
5956 the path is virtual, and we need to make sure we stay inside the
5957 real binfos when going through virtual bases. Maybe we should
5958 replace virtual bases with BINFO_FOR_VBASE ... (mrs) */
5959 binfo = get_binfo (from, to, 1);
5960 if (binfo == error_mark_node)
5962 error (" in pointer to member function conversion");
5963 return delta;
5965 if (binfo == 0)
5967 if (!force)
5969 error_not_base_type (from, to);
5970 error (" in pointer to member conversion");
5971 return delta;
5973 binfo = get_binfo (to, from, 1);
5974 if (binfo == 0 || binfo == error_mark_node)
5975 return delta;
5976 virt_binfo = binfo_from_vbase (binfo);
5978 if (virt_binfo)
5980 /* This is a reinterpret cast, we choose to do nothing. */
5981 cp_warning ("pointer to member cast via virtual base `%T' of `%T'",
5982 BINFO_TYPE (virt_binfo),
5983 BINFO_TYPE (BINFO_INHERITANCE_CHAIN (virt_binfo)));
5984 return delta;
5986 delta = BINFO_OFFSET (binfo);
5987 delta = cp_convert (ptrdiff_type_node, delta);
5988 delta = cp_build_binary_op (MINUS_EXPR,
5989 integer_zero_node,
5990 delta);
5992 return delta;
5995 virt_binfo = binfo_from_vbase (binfo);
5996 if (virt_binfo)
5998 /* This is a reinterpret cast, we choose to do nothing. */
5999 if (force)
6000 cp_warning ("pointer to member cast via virtual base `%T' of `%T'",
6001 BINFO_TYPE (virt_binfo),
6002 BINFO_TYPE (BINFO_INHERITANCE_CHAIN (virt_binfo)));
6003 else
6004 cp_error ("pointer to member conversion via virtual base `%T' of `%T'",
6005 BINFO_TYPE (virt_binfo),
6006 BINFO_TYPE (BINFO_INHERITANCE_CHAIN (virt_binfo)));
6007 return delta;
6009 delta = BINFO_OFFSET (binfo);
6011 return cp_convert (ptrdiff_type_node, delta);
6014 /* Return a constructor for the pointer-to-member-function TYPE using
6015 the other components as specified. */
6017 tree
6018 build_ptrmemfunc1 (type, delta, pfn)
6019 tree type, delta, pfn;
6021 tree u = NULL_TREE;
6022 tree delta_field;
6023 tree pfn_field;
6025 /* Pull the FIELD_DECLs out of the type. */
6026 pfn_field = TYPE_FIELDS (type);
6027 delta_field = TREE_CHAIN (pfn_field);
6029 /* Make sure DELTA has the type we want. */
6030 delta = convert_and_check (delta_type_node, delta);
6032 /* Finish creating the initializer. */
6033 u = tree_cons (pfn_field, pfn,
6034 build_tree_list (delta_field, delta));
6035 u = build (CONSTRUCTOR, type, NULL_TREE, u);
6036 TREE_CONSTANT (u) = TREE_CONSTANT (pfn) && TREE_CONSTANT (delta);
6037 TREE_STATIC (u) = (TREE_CONSTANT (u)
6038 && (initializer_constant_valid_p (pfn, TREE_TYPE (pfn))
6039 != NULL_TREE)
6040 && (initializer_constant_valid_p (delta, TREE_TYPE (delta))
6041 != NULL_TREE));
6042 return u;
6045 /* Build a constructor for a pointer to member function. It can be
6046 used to initialize global variables, local variable, or used
6047 as a value in expressions. TYPE is the POINTER to METHOD_TYPE we
6048 want to be.
6050 If FORCE is non-zero, then force this conversion, even if
6051 we would rather not do it. Usually set when using an explicit
6052 cast.
6054 Return error_mark_node, if something goes wrong. */
6056 tree
6057 build_ptrmemfunc (type, pfn, force)
6058 tree type, pfn;
6059 int force;
6061 tree fn;
6062 tree pfn_type = TREE_TYPE (pfn);
6063 tree to_type = build_ptrmemfunc_type (type);
6065 /* Handle multiple conversions of pointer to member functions. */
6066 if (TYPE_PTRMEMFUNC_P (TREE_TYPE (pfn)))
6068 tree delta = NULL_TREE;
6069 tree npfn = NULL_TREE;
6070 tree n;
6072 if (!force
6073 && !can_convert_arg (to_type, TREE_TYPE (pfn), pfn))
6074 cp_error ("invalid conversion to type `%T' from type `%T'",
6075 to_type, pfn_type);
6077 n = get_delta_difference (TYPE_PTRMEMFUNC_OBJECT_TYPE (pfn_type),
6078 TYPE_PTRMEMFUNC_OBJECT_TYPE (to_type),
6079 force);
6081 /* We don't have to do any conversion to convert a
6082 pointer-to-member to its own type. But, we don't want to
6083 just return a PTRMEM_CST if there's an explicit cast; that
6084 cast should make the expression an invalid template argument. */
6085 if (TREE_CODE (pfn) != PTRMEM_CST)
6087 if (same_type_p (to_type, pfn_type))
6088 return pfn;
6089 else if (integer_zerop (n))
6090 return build_reinterpret_cast (to_type, pfn);
6093 if (TREE_SIDE_EFFECTS (pfn))
6094 pfn = save_expr (pfn);
6096 /* Obtain the function pointer and the current DELTA. */
6097 if (TREE_CODE (pfn) == PTRMEM_CST)
6098 expand_ptrmemfunc_cst (pfn, &delta, &npfn);
6099 else
6101 npfn = build_component_ref (pfn, pfn_identifier, NULL_TREE, 0);
6102 delta = build_component_ref (pfn, delta_identifier, NULL_TREE, 0);
6105 /* Under the new ABI, the conversion is easy. Just adjust
6106 the DELTA field. */
6107 delta = cp_convert (ptrdiff_type_node, delta);
6108 if (TARGET_PTRMEMFUNC_VBIT_LOCATION == ptrmemfunc_vbit_in_delta)
6109 n = cp_build_binary_op (LSHIFT_EXPR, n, integer_one_node);
6110 delta = cp_build_binary_op (PLUS_EXPR, delta, n);
6111 return build_ptrmemfunc1 (to_type, delta, npfn);
6114 /* Handle null pointer to member function conversions. */
6115 if (integer_zerop (pfn))
6117 pfn = build_c_cast (type, integer_zero_node);
6118 return build_ptrmemfunc1 (to_type,
6119 integer_zero_node,
6120 pfn);
6123 if (type_unknown_p (pfn))
6124 return instantiate_type (type, pfn, itf_complain);
6126 fn = TREE_OPERAND (pfn, 0);
6127 my_friendly_assert (TREE_CODE (fn) == FUNCTION_DECL, 0);
6128 return make_ptrmem_cst (to_type, fn);
6131 /* Return the DELTA, IDX, PFN, and DELTA2 values for the PTRMEM_CST
6132 given by CST.
6134 ??? There is no consistency as to the types returned for the above
6135 values. Some code acts as if its a sizetype and some as if its
6136 integer_type_node. */
6138 void
6139 expand_ptrmemfunc_cst (cst, delta, pfn)
6140 tree cst;
6141 tree *delta;
6142 tree *pfn;
6144 tree type = TREE_TYPE (cst);
6145 tree fn = PTRMEM_CST_MEMBER (cst);
6146 tree ptr_class, fn_class;
6148 my_friendly_assert (TREE_CODE (fn) == FUNCTION_DECL, 0);
6150 /* The class that the function belongs to. */
6151 fn_class = DECL_CONTEXT (fn);
6153 /* The class that we're creating a pointer to member of. */
6154 ptr_class = TYPE_PTRMEMFUNC_OBJECT_TYPE (type);
6156 /* First, calculate the adjustment to the function's class. */
6157 *delta = get_delta_difference (fn_class, ptr_class, /*force=*/0);
6159 if (!DECL_VIRTUAL_P (fn))
6160 *pfn = convert (TYPE_PTRMEMFUNC_FN_TYPE (type), build_addr_func (fn));
6161 else
6163 /* If we're dealing with a virtual function, we have to adjust 'this'
6164 again, to point to the base which provides the vtable entry for
6165 fn; the call will do the opposite adjustment. */
6166 tree orig_class = DECL_VIRTUAL_CONTEXT (fn);
6167 tree binfo = binfo_or_else (orig_class, fn_class);
6168 *delta = fold (build (PLUS_EXPR, TREE_TYPE (*delta),
6169 *delta, BINFO_OFFSET (binfo)));
6171 /* Under the new ABI, we set PFN to the vtable offset at
6172 which the function can be found, plus one (unless
6173 ptrmemfunc_vbit_in_delta, in which case delta is shifted
6174 left, and then incremented). */
6175 *pfn = DECL_VINDEX (fn);
6176 *pfn = fold (build (MULT_EXPR, integer_type_node, *pfn,
6177 TYPE_SIZE_UNIT (vtable_entry_type)));
6179 switch (TARGET_PTRMEMFUNC_VBIT_LOCATION)
6181 case ptrmemfunc_vbit_in_pfn:
6182 *pfn = fold (build (PLUS_EXPR, integer_type_node, *pfn,
6183 integer_one_node));
6184 break;
6186 case ptrmemfunc_vbit_in_delta:
6187 *delta = fold (build (LSHIFT_EXPR, TREE_TYPE (*delta),
6188 *delta, integer_one_node));
6189 *delta = fold (build (PLUS_EXPR, TREE_TYPE (*delta),
6190 *delta, integer_one_node));
6191 break;
6193 default:
6194 abort ();
6197 *pfn = fold (build1 (NOP_EXPR, TYPE_PTRMEMFUNC_FN_TYPE (type),
6198 *pfn));
6202 /* Return an expression for PFN from the pointer-to-member function
6203 given by T. */
6205 tree
6206 pfn_from_ptrmemfunc (t)
6207 tree t;
6209 if (TREE_CODE (t) == PTRMEM_CST)
6211 tree delta;
6212 tree pfn;
6214 expand_ptrmemfunc_cst (t, &delta, &pfn);
6215 if (pfn)
6216 return pfn;
6219 return build_component_ref (t, pfn_identifier, NULL_TREE, 0);
6222 /* Expression EXPR is about to be implicitly converted to TYPE. Warn
6223 if this is a potentially dangerous thing to do. Returns a possibly
6224 marked EXPR. */
6226 tree
6227 dubious_conversion_warnings (type, expr, errtype, fndecl, parmnum)
6228 tree type;
6229 tree expr;
6230 const char *errtype;
6231 tree fndecl;
6232 int parmnum;
6234 if (TREE_CODE (type) == REFERENCE_TYPE)
6235 type = TREE_TYPE (type);
6237 /* Issue warnings about peculiar, but legal, uses of NULL. */
6238 if (ARITHMETIC_TYPE_P (type) && expr == null_node)
6240 if (fndecl)
6241 cp_warning ("passing NULL used for non-pointer %s %P of `%D'",
6242 errtype, parmnum, fndecl);
6243 else
6244 cp_warning ("%s to non-pointer type `%T' from NULL", errtype, type);
6247 /* Warn about assigning a floating-point type to an integer type. */
6248 if (TREE_CODE (TREE_TYPE (expr)) == REAL_TYPE
6249 && TREE_CODE (type) == INTEGER_TYPE)
6251 if (fndecl)
6252 cp_warning ("passing `%T' for %s %P of `%D'",
6253 TREE_TYPE (expr), errtype, parmnum, fndecl);
6254 else
6255 cp_warning ("%s to `%T' from `%T'", errtype, type, TREE_TYPE (expr));
6257 /* And warn about assigning a negative value to an unsigned
6258 variable. */
6259 else if (TREE_UNSIGNED (type) && TREE_CODE (type) != BOOLEAN_TYPE)
6261 if (TREE_CODE (expr) == INTEGER_CST
6262 && TREE_NEGATED_INT (expr))
6264 if (fndecl)
6265 cp_warning ("passing negative value `%E' for %s %P of `%D'",
6266 expr, errtype, parmnum, fndecl);
6267 else
6268 cp_warning ("%s of negative value `%E' to `%T'",
6269 errtype, expr, type);
6272 overflow_warning (expr);
6274 if (TREE_CONSTANT (expr))
6275 expr = fold (expr);
6277 return expr;
6280 /* Convert value RHS to type TYPE as preparation for an assignment to
6281 an lvalue of type TYPE. ERRTYPE is a string to use in error
6282 messages: "assignment", "return", etc. If FNDECL is non-NULL, we
6283 are doing the conversion in order to pass the PARMNUMth argument of
6284 FNDECL. */
6286 static tree
6287 convert_for_assignment (type, rhs, errtype, fndecl, parmnum)
6288 tree type, rhs;
6289 const char *errtype;
6290 tree fndecl;
6291 int parmnum;
6293 register enum tree_code codel = TREE_CODE (type);
6294 register tree rhstype;
6295 register enum tree_code coder;
6297 if (codel == OFFSET_TYPE)
6298 my_friendly_abort (990505);
6300 if (TREE_CODE (rhs) == OFFSET_REF)
6301 rhs = resolve_offset_ref (rhs);
6303 /* Strip NON_LVALUE_EXPRs since we aren't using as an lvalue. */
6304 if (TREE_CODE (rhs) == NON_LVALUE_EXPR)
6305 rhs = TREE_OPERAND (rhs, 0);
6307 rhstype = TREE_TYPE (rhs);
6308 coder = TREE_CODE (rhstype);
6310 if (rhs == error_mark_node || rhstype == error_mark_node)
6311 return error_mark_node;
6312 if (TREE_CODE (rhs) == TREE_LIST && TREE_VALUE (rhs) == error_mark_node)
6313 return error_mark_node;
6315 rhs = dubious_conversion_warnings (type, rhs, errtype, fndecl, parmnum);
6317 /* The RHS of an assignment cannot have void type. */
6318 if (coder == VOID_TYPE)
6320 error ("void value not ignored as it ought to be");
6321 return error_mark_node;
6324 /* Simplify the RHS if possible. */
6325 if (TREE_CODE (rhs) == CONST_DECL)
6326 rhs = DECL_INITIAL (rhs);
6327 else if (coder != ARRAY_TYPE)
6328 rhs = decl_constant_value (rhs);
6330 /* [expr.ass]
6332 The expression is implicitly converted (clause _conv_) to the
6333 cv-unqualified type of the left operand. */
6334 if (!can_convert_arg (type, rhstype, rhs))
6336 /* When -Wno-pmf-conversions is use, we just silently allow
6337 conversions from pointers-to-members to plain pointers. If
6338 the conversion doesn't work, cp_convert will complain. */
6339 if (!warn_pmf2ptr
6340 && TYPE_PTR_P (type)
6341 && TYPE_PTRMEMFUNC_P (rhstype))
6342 rhs = cp_convert (strip_top_quals (type), rhs);
6343 else
6345 /* If the right-hand side has unknown type, then it is an
6346 overloaded function. Call instantiate_type to get error
6347 messages. */
6348 if (rhstype == unknown_type_node)
6349 instantiate_type (type, rhs, itf_complain);
6350 else if (fndecl)
6351 cp_error ("cannot convert `%T' to `%T' for argument `%P' to `%D'",
6352 rhstype, type, parmnum, fndecl);
6353 else
6354 cp_error ("cannot convert `%T' to `%T' in %s", rhstype, type,
6355 errtype);
6356 return error_mark_node;
6359 return perform_implicit_conversion (strip_top_quals (type), rhs);
6362 /* Convert RHS to be of type TYPE.
6363 If EXP is non-zero, it is the target of the initialization.
6364 ERRTYPE is a string to use in error messages.
6366 Two major differences between the behavior of
6367 `convert_for_assignment' and `convert_for_initialization'
6368 are that references are bashed in the former, while
6369 copied in the latter, and aggregates are assigned in
6370 the former (operator=) while initialized in the
6371 latter (X(X&)).
6373 If using constructor make sure no conversion operator exists, if one does
6374 exist, an ambiguity exists.
6376 If flags doesn't include LOOKUP_COMPLAIN, don't complain about anything. */
6378 tree
6379 convert_for_initialization (exp, type, rhs, flags, errtype, fndecl, parmnum)
6380 tree exp, type, rhs;
6381 int flags;
6382 const char *errtype;
6383 tree fndecl;
6384 int parmnum;
6386 register enum tree_code codel = TREE_CODE (type);
6387 register tree rhstype;
6388 register enum tree_code coder;
6390 /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
6391 Strip such NOP_EXPRs, since RHS is used in non-lvalue context. */
6392 if (TREE_CODE (rhs) == NOP_EXPR
6393 && TREE_TYPE (rhs) == TREE_TYPE (TREE_OPERAND (rhs, 0))
6394 && codel != REFERENCE_TYPE)
6395 rhs = TREE_OPERAND (rhs, 0);
6397 if (rhs == error_mark_node
6398 || (TREE_CODE (rhs) == TREE_LIST && TREE_VALUE (rhs) == error_mark_node))
6399 return error_mark_node;
6401 if (TREE_CODE (rhs) == OFFSET_REF)
6403 rhs = resolve_offset_ref (rhs);
6404 if (rhs == error_mark_node)
6405 return error_mark_node;
6408 if (TREE_CODE (TREE_TYPE (rhs)) == REFERENCE_TYPE)
6409 rhs = convert_from_reference (rhs);
6411 if ((TREE_CODE (TREE_TYPE (rhs)) == ARRAY_TYPE
6412 && TREE_CODE (type) != ARRAY_TYPE
6413 && (TREE_CODE (type) != REFERENCE_TYPE
6414 || TREE_CODE (TREE_TYPE (type)) != ARRAY_TYPE))
6415 || (TREE_CODE (TREE_TYPE (rhs)) == FUNCTION_TYPE
6416 && (TREE_CODE (type) != REFERENCE_TYPE
6417 || TREE_CODE (TREE_TYPE (type)) != FUNCTION_TYPE))
6418 || TREE_CODE (TREE_TYPE (rhs)) == METHOD_TYPE)
6419 rhs = default_conversion (rhs);
6421 rhstype = TREE_TYPE (rhs);
6422 coder = TREE_CODE (rhstype);
6424 if (coder == ERROR_MARK)
6425 return error_mark_node;
6427 /* We accept references to incomplete types, so we can
6428 return here before checking if RHS is of complete type. */
6430 if (codel == REFERENCE_TYPE)
6432 /* This should eventually happen in convert_arguments. */
6433 int savew = 0, savee = 0;
6435 if (fndecl)
6436 savew = warningcount, savee = errorcount;
6437 rhs = initialize_reference (type, rhs);
6438 if (fndecl)
6440 if (warningcount > savew)
6441 cp_warning_at ("in passing argument %P of `%+D'", parmnum, fndecl);
6442 else if (errorcount > savee)
6443 cp_error_at ("in passing argument %P of `%+D'", parmnum, fndecl);
6445 return rhs;
6448 if (exp != 0)
6449 exp = require_complete_type (exp);
6450 if (exp == error_mark_node)
6451 return error_mark_node;
6453 if (TREE_CODE (rhstype) == REFERENCE_TYPE)
6454 rhstype = TREE_TYPE (rhstype);
6456 type = complete_type (type);
6458 if (IS_AGGR_TYPE (type))
6459 return ocp_convert (type, rhs, CONV_IMPLICIT|CONV_FORCE_TEMP, flags);
6461 return convert_for_assignment (type, rhs, errtype, fndecl, parmnum);
6464 /* Expand an ASM statement with operands, handling output operands
6465 that are not variables or INDIRECT_REFS by transforming such
6466 cases into cases that expand_asm_operands can handle.
6468 Arguments are same as for expand_asm_operands.
6470 We don't do default conversions on all inputs, because it can screw
6471 up operands that are expected to be in memory. */
6473 void
6474 c_expand_asm_operands (string, outputs, inputs, clobbers, vol, filename, line)
6475 tree string, outputs, inputs, clobbers;
6476 int vol;
6477 const char *filename;
6478 int line;
6480 int noutputs = list_length (outputs);
6481 register int i;
6482 /* o[I] is the place that output number I should be written. */
6483 register tree *o = (tree *) alloca (noutputs * sizeof (tree));
6484 register tree tail;
6486 /* Record the contents of OUTPUTS before it is modified. */
6487 for (i = 0, tail = outputs; tail; tail = TREE_CHAIN (tail), i++)
6488 o[i] = TREE_VALUE (tail);
6490 /* Generate the ASM_OPERANDS insn;
6491 store into the TREE_VALUEs of OUTPUTS some trees for
6492 where the values were actually stored. */
6493 expand_asm_operands (string, outputs, inputs, clobbers, vol, filename, line);
6495 /* Copy all the intermediate outputs into the specified outputs. */
6496 for (i = 0, tail = outputs; tail; tail = TREE_CHAIN (tail), i++)
6498 if (o[i] != TREE_VALUE (tail))
6500 expand_expr (build_modify_expr (o[i], NOP_EXPR, TREE_VALUE (tail)),
6501 const0_rtx, VOIDmode, EXPAND_NORMAL);
6502 free_temp_slots ();
6504 /* Restore the original value so that it's correct the next
6505 time we expand this function. */
6506 TREE_VALUE (tail) = o[i];
6508 /* Detect modification of read-only values.
6509 (Otherwise done by build_modify_expr.) */
6510 else
6512 tree type = TREE_TYPE (o[i]);
6513 if (CP_TYPE_CONST_P (type)
6514 || (IS_AGGR_TYPE_CODE (TREE_CODE (type))
6515 && C_TYPE_FIELDS_READONLY (type)))
6516 readonly_error (o[i], "modification by `asm'", 1);
6520 /* Those MODIFY_EXPRs could do autoincrements. */
6521 emit_queue ();
6524 /* If RETVAL is the address of, or a reference to, a local variable or
6525 temporary give an appropraite warning. */
6527 static void
6528 maybe_warn_about_returning_address_of_local (retval)
6529 tree retval;
6531 tree valtype = TREE_TYPE (DECL_RESULT (current_function_decl));
6532 tree whats_returned = retval;
6534 for (;;)
6536 if (TREE_CODE (whats_returned) == COMPOUND_EXPR)
6537 whats_returned = TREE_OPERAND (whats_returned, 1);
6538 else if (TREE_CODE (whats_returned) == CONVERT_EXPR
6539 || TREE_CODE (whats_returned) == NON_LVALUE_EXPR
6540 || TREE_CODE (whats_returned) == NOP_EXPR)
6541 whats_returned = TREE_OPERAND (whats_returned, 0);
6542 else
6543 break;
6546 if (TREE_CODE (whats_returned) != ADDR_EXPR)
6547 return;
6548 whats_returned = TREE_OPERAND (whats_returned, 0);
6550 if (TREE_CODE (valtype) == REFERENCE_TYPE)
6552 if (TREE_CODE (whats_returned) == AGGR_INIT_EXPR
6553 || TREE_CODE (whats_returned) == TARGET_EXPR)
6555 /* Get the target. */
6556 whats_returned = TREE_OPERAND (whats_returned, 0);
6557 warning ("returning reference to temporary");
6558 return;
6560 if (TREE_CODE (whats_returned) == VAR_DECL
6561 && DECL_NAME (whats_returned)
6562 && TEMP_NAME_P (DECL_NAME (whats_returned)))
6564 warning ("reference to non-lvalue returned");
6565 return;
6569 if (TREE_CODE (whats_returned) == VAR_DECL
6570 && DECL_NAME (whats_returned)
6571 && DECL_FUNCTION_SCOPE_P (whats_returned)
6572 && !(TREE_STATIC (whats_returned)
6573 || TREE_PUBLIC (whats_returned)))
6575 if (TREE_CODE (valtype) == REFERENCE_TYPE)
6576 cp_warning_at ("reference to local variable `%D' returned",
6577 whats_returned);
6578 else
6579 cp_warning_at ("address of local variable `%D' returned",
6580 whats_returned);
6581 return;
6585 /* Check that returning RETVAL from the current function is legal.
6586 Return an expression explicitly showing all conversions required to
6587 change RETVAL into the function return type, and to assign it to
6588 the DECL_RESULT for the function. */
6590 tree
6591 check_return_expr (retval)
6592 tree retval;
6594 tree result;
6595 /* The type actually returned by the function, after any
6596 promotions. */
6597 tree valtype;
6598 int fn_returns_value_p;
6600 /* A `volatile' function is one that isn't supposed to return, ever.
6601 (This is a G++ extension, used to get better code for functions
6602 that call the `volatile' function.) */
6603 if (TREE_THIS_VOLATILE (current_function_decl))
6604 warning ("function declared `noreturn' has a `return' statement");
6606 /* Check for various simple errors. */
6607 if (dtor_label)
6609 if (retval)
6610 error ("returning a value from a destructor");
6611 return NULL_TREE;
6613 else if (DECL_CONSTRUCTOR_P (current_function_decl))
6615 if (in_function_try_handler)
6616 /* If a return statement appears in a handler of the
6617 function-try-block of a constructor, the program is ill-formed. */
6618 error ("cannot return from a handler of a function-try-block of a constructor");
6619 else if (retval)
6620 /* You can't return a value from a constructor. */
6621 error ("returning a value from a constructor");
6622 return NULL_TREE;
6625 /* When no explicit return-value is given in a function with a named
6626 return value, the named return value is used. */
6627 result = DECL_RESULT (current_function_decl);
6628 valtype = TREE_TYPE (result);
6629 my_friendly_assert (valtype != NULL_TREE, 19990924);
6630 fn_returns_value_p = !VOID_TYPE_P (valtype);
6631 if (!retval && DECL_NAME (result) && fn_returns_value_p)
6632 retval = result;
6634 /* Check for a return statement with no return value in a function
6635 that's supposed to return a value. */
6636 if (!retval && fn_returns_value_p)
6638 pedwarn ("return-statement with no value, in function declared with a non-void return type");
6639 /* Clear this, so finish_function won't say that we reach the
6640 end of a non-void function (which we don't, we gave a
6641 return!). */
6642 current_function_returns_null = 0;
6644 /* Check for a return statement with a value in a function that
6645 isn't supposed to return a value. */
6646 else if (retval && !fn_returns_value_p)
6648 if (VOID_TYPE_P (TREE_TYPE (retval)))
6649 /* You can return a `void' value from a function of `void'
6650 type. In that case, we have to evaluate the expression for
6651 its side-effects. */
6652 finish_expr_stmt (retval);
6653 else
6654 pedwarn ("return-statement with a value, in function declared with a void return type");
6656 current_function_returns_null = 1;
6658 /* There's really no value to return, after all. */
6659 return NULL_TREE;
6661 else if (!retval)
6662 /* Remember that this function can sometimes return without a
6663 value. */
6664 current_function_returns_null = 1;
6665 else
6666 /* Remember that this function did return a value. */
6667 current_function_returns_value = 1;
6669 /* Only operator new(...) throw(), can return NULL [expr.new/13]. */
6670 if ((DECL_OVERLOADED_OPERATOR_P (current_function_decl) == NEW_EXPR
6671 || DECL_OVERLOADED_OPERATOR_P (current_function_decl) == VEC_NEW_EXPR)
6672 && !TYPE_NOTHROW_P (TREE_TYPE (current_function_decl))
6673 && ! flag_check_new
6674 && null_ptr_cst_p (retval))
6675 cp_warning ("`operator new' must not return NULL unless it is declared `throw()' (or -fcheck-new is in effect)");
6677 /* Effective C++ rule 15. See also start_function. */
6678 if (warn_ecpp
6679 && DECL_NAME (current_function_decl) == ansi_assopname(NOP_EXPR)
6680 && retval != current_class_ref)
6681 cp_warning ("`operator=' should return a reference to `*this'");
6683 /* The fabled Named Return Value optimization: If this is a
6684 value-returning function that always returns the same local
6685 variable, remember it.
6687 It might be nice to be more flexible, and choose the first suitable
6688 variable even if the function sometimes returns something else, but
6689 then we run the risk of clobbering the variable we chose if the other
6690 returned expression uses the chosen variable somehow. And people expect
6691 this restriction, anyway. (jason 2000-11-19) */
6693 if (fn_returns_value_p && optimize)
6695 if (retval != NULL_TREE
6696 && (current_function_return_value == NULL_TREE
6697 || current_function_return_value == retval)
6698 && TREE_CODE (retval) == VAR_DECL
6699 && DECL_CONTEXT (retval) == current_function_decl
6700 && ! TREE_STATIC (retval)
6701 && ! DECL_USER_ALIGN (retval)
6702 && same_type_p (TREE_TYPE (retval),
6703 TREE_TYPE (TREE_TYPE (current_function_decl))))
6704 current_function_return_value = retval;
6705 else
6706 current_function_return_value = error_mark_node;
6709 /* We don't need to do any conversions when there's nothing being
6710 returned. */
6711 if (!retval || retval == error_mark_node)
6712 return retval;
6714 /* Do any required conversions. */
6715 if (retval == result || DECL_CONSTRUCTOR_P (current_function_decl))
6716 /* No conversions are required. */
6718 else
6720 /* The type the function is declared to return. */
6721 tree functype = TREE_TYPE (TREE_TYPE (current_function_decl));
6723 /* First convert the value to the function's return type, then
6724 to the type of return value's location to handle the
6725 case that functype is smaller than the valtype. */
6726 retval = convert_for_initialization
6727 (NULL_TREE, functype, retval, LOOKUP_NORMAL|LOOKUP_ONLYCONVERTING,
6728 "return", NULL_TREE, 0);
6729 retval = convert (valtype, retval);
6731 /* If the conversion failed, treat this just like `return;'. */
6732 if (retval == error_mark_node)
6733 return retval;
6734 /* We can't initialize a register from a AGGR_INIT_EXPR. */
6735 else if (! current_function_returns_struct
6736 && TREE_CODE (retval) == TARGET_EXPR
6737 && TREE_CODE (TREE_OPERAND (retval, 1)) == AGGR_INIT_EXPR)
6738 retval = build (COMPOUND_EXPR, TREE_TYPE (retval), retval,
6739 TREE_OPERAND (retval, 0));
6740 else
6741 maybe_warn_about_returning_address_of_local (retval);
6744 /* Actually copy the value returned into the appropriate location. */
6745 if (retval && retval != result)
6746 retval = build (INIT_EXPR, TREE_TYPE (result), result, retval);
6748 return retval;
6752 /* Returns non-zero if the pointer-type FROM can be converted to the
6753 pointer-type TO via a qualification conversion. If CONSTP is -1,
6754 then we return non-zero if the pointers are similar, and the
6755 cv-qualification signature of FROM is a proper subset of that of TO.
6757 If CONSTP is positive, then all outer pointers have been
6758 const-qualified. */
6760 static int
6761 comp_ptr_ttypes_real (to, from, constp)
6762 tree to, from;
6763 int constp;
6765 int to_more_cv_qualified = 0;
6767 for (; ; to = TREE_TYPE (to), from = TREE_TYPE (from))
6769 if (TREE_CODE (to) != TREE_CODE (from))
6770 return 0;
6772 if (TREE_CODE (from) == OFFSET_TYPE
6773 && same_type_p (TYPE_OFFSET_BASETYPE (from),
6774 TYPE_OFFSET_BASETYPE (to)))
6775 continue;
6777 /* Const and volatile mean something different for function types,
6778 so the usual checks are not appropriate. */
6779 if (TREE_CODE (to) != FUNCTION_TYPE && TREE_CODE (to) != METHOD_TYPE)
6781 if (!at_least_as_qualified_p (to, from))
6782 return 0;
6784 if (!at_least_as_qualified_p (from, to))
6786 if (constp == 0)
6787 return 0;
6788 else
6789 ++to_more_cv_qualified;
6792 if (constp > 0)
6793 constp &= TYPE_READONLY (to);
6796 if (TREE_CODE (to) != POINTER_TYPE)
6797 return
6798 same_type_ignoring_top_level_qualifiers_p (to, from)
6799 && (constp >= 0 || to_more_cv_qualified);
6803 /* When comparing, say, char ** to char const **, this function takes the
6804 'char *' and 'char const *'. Do not pass non-pointer types to this
6805 function. */
6808 comp_ptr_ttypes (to, from)
6809 tree to, from;
6811 return comp_ptr_ttypes_real (to, from, 1);
6814 /* Returns 1 if to and from are (possibly multi-level) pointers to the same
6815 type or inheritance-related types, regardless of cv-quals. */
6818 ptr_reasonably_similar (to, from)
6819 tree to, from;
6821 for (; ; to = TREE_TYPE (to), from = TREE_TYPE (from))
6823 if (TREE_CODE (to) != TREE_CODE (from))
6824 return 0;
6826 if (TREE_CODE (from) == OFFSET_TYPE
6827 && comptypes (TYPE_OFFSET_BASETYPE (to),
6828 TYPE_OFFSET_BASETYPE (from),
6829 COMPARE_BASE | COMPARE_RELAXED))
6830 continue;
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);