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