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