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