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