0ceabf01533d29d0c120a896dcec8c9649a3ad29
[gcc.git] / gcc / cp / cvt.c
1 /* Language-level data type conversion for GNU C++.
2 Copyright (C) 1987, 1988, 1992, 1993, 1995 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, 675 Mass Ave, Cambridge, MA 02139, USA. */
20
21
22 /* This file contains the functions for converting C expressions
23 to different data types. The only entry point is `convert'.
24 Every language front end must have a `convert' function
25 but what kind of conversions it does will depend on the language. */
26
27 #include "config.h"
28 #include "tree.h"
29 #include "flags.h"
30 #include "cp-tree.h"
31 #include "class.h"
32 #include "convert.h"
33
34 #undef NULL
35 #define NULL (char *)0
36
37 /* Change of width--truncation and extension of integers or reals--
38 is represented with NOP_EXPR. Proper functioning of many things
39 assumes that no other conversions can be NOP_EXPRs.
40
41 Conversion between integer and pointer is represented with CONVERT_EXPR.
42 Converting integer to real uses FLOAT_EXPR
43 and real to integer uses FIX_TRUNC_EXPR.
44
45 Here is a list of all the functions that assume that widening and
46 narrowing is always done with a NOP_EXPR:
47 In convert.c, convert_to_integer.
48 In c-typeck.c, build_binary_op_nodefault (boolean ops),
49 and truthvalue_conversion.
50 In expr.c: expand_expr, for operands of a MULT_EXPR.
51 In fold-const.c: fold.
52 In tree.c: get_narrower and get_unwidened.
53
54 C++: in multiple-inheritance, converting between pointers may involve
55 adjusting them by a delta stored within the class definition. */
56 \f
57 /* Subroutines of `convert'. */
58
59 /* Build a thunk. What it is, is an entry point that when called will
60 adjust the this pointer (the first argument) by offset, and then
61 goto the real address of the function given by REAL_ADDR that we
62 would like called. What we return is the address of the thunk. */
63 static tree
64 build_thunk (offset, real_addr)
65 tree offset, real_addr;
66 {
67 if (TREE_CODE (real_addr) != ADDR_EXPR
68 || TREE_CODE (TREE_OPERAND (real_addr, 0)) != FUNCTION_DECL)
69 {
70 sorry ("MI pointer to member conversion too complex");
71 return error_mark_node;
72 }
73 sorry ("MI pointer to member conversion too complex");
74 return error_mark_node;
75 }
76
77 /* Convert a `pointer to member' (POINTER_TYPE to METHOD_TYPE) into
78 another `pointer to method'. This may involved the creation of
79 a thunk to handle the this offset calculation. */
80 static tree
81 convert_fn_ptr (type, expr)
82 tree type, expr;
83 {
84 if (flag_vtable_thunks)
85 {
86 tree intype = TREE_TYPE (expr);
87 tree binfo = get_binfo (TYPE_METHOD_BASETYPE (TREE_TYPE (intype)),
88 TYPE_METHOD_BASETYPE (TREE_TYPE (type)), 1);
89 if (binfo == error_mark_node)
90 {
91 error (" in pointer to member conversion");
92 return error_mark_node;
93 }
94 if (binfo == NULL_TREE)
95 {
96 /* ARM 4.8 restriction. */
97 error ("invalid pointer to member conversion");
98 return error_mark_node;
99 }
100
101 if (BINFO_OFFSET_ZEROP (binfo))
102 return build1 (NOP_EXPR, type, expr);
103 return build1 (NOP_EXPR, type, build_thunk (BINFO_OFFSET (binfo), expr));
104 }
105 else
106 return build_ptrmemfunc (type, expr, 1);
107 }
108
109 /* if converting pointer to pointer
110 if dealing with classes, check for derived->base or vice versa
111 else if dealing with method pointers, delegate
112 else convert blindly
113 else if converting class, pass off to build_type_conversion
114 else try C-style pointer conversion */
115 static tree
116 cp_convert_to_pointer (type, expr)
117 tree type, expr;
118 {
119 register tree intype = TREE_TYPE (expr);
120 register enum tree_code form;
121
122 if (TYPE_PTRMEMFUNC_P (type))
123 type = TYPE_PTRMEMFUNC_FN_TYPE (type);
124 if (TYPE_PTRMEMFUNC_P (intype))
125 intype = TYPE_PTRMEMFUNC_FN_TYPE (intype);
126
127 form = TREE_CODE (intype);
128
129 if (form == POINTER_TYPE || form == REFERENCE_TYPE)
130 {
131 intype = TYPE_MAIN_VARIANT (intype);
132
133 if (TYPE_MAIN_VARIANT (type) != intype
134 && TREE_CODE (TREE_TYPE (type)) == RECORD_TYPE
135 && TREE_CODE (TREE_TYPE (intype)) == RECORD_TYPE)
136 {
137 enum tree_code code = PLUS_EXPR;
138 tree binfo = get_binfo (TREE_TYPE (type), TREE_TYPE (intype), 1);
139 if (binfo == error_mark_node)
140 return error_mark_node;
141 if (binfo == NULL_TREE)
142 {
143 binfo = get_binfo (TREE_TYPE (intype), TREE_TYPE (type), 1);
144 if (binfo == error_mark_node)
145 return error_mark_node;
146 code = MINUS_EXPR;
147 }
148 if (binfo)
149 {
150 if (TYPE_USES_VIRTUAL_BASECLASSES (TREE_TYPE (type))
151 || TYPE_USES_VIRTUAL_BASECLASSES (TREE_TYPE (intype))
152 || ! BINFO_OFFSET_ZEROP (binfo))
153 {
154 /* Need to get the path we took. */
155 tree path;
156
157 if (code == PLUS_EXPR)
158 get_base_distance (TREE_TYPE (type), TREE_TYPE (intype), 0, &path);
159 else
160 get_base_distance (TREE_TYPE (intype), TREE_TYPE (type), 0, &path);
161 return build_vbase_path (code, type, expr, path, 0);
162 }
163 }
164 }
165 if (TREE_CODE (TREE_TYPE (intype)) == METHOD_TYPE
166 && TREE_CODE (type) == POINTER_TYPE
167 && TREE_CODE (TREE_TYPE (type)) == METHOD_TYPE)
168 return convert_fn_ptr (type, expr);
169
170 if (TREE_CODE (TREE_TYPE (type)) == OFFSET_TYPE
171 && TREE_CODE (TREE_TYPE (intype)) == OFFSET_TYPE)
172 {
173 tree b1 = TYPE_OFFSET_BASETYPE (TREE_TYPE (type));
174 tree b2 = TYPE_OFFSET_BASETYPE (TREE_TYPE (intype));
175 tree binfo = get_binfo (b1, b2, 1);
176 if (binfo == NULL_TREE)
177 binfo = get_binfo (b2, b1, 1);
178 if (binfo == error_mark_node)
179 return error_mark_node;
180 }
181
182 return build1 (NOP_EXPR, type, expr);
183 }
184
185 my_friendly_assert (form != OFFSET_TYPE, 186);
186
187 if (TYPE_LANG_SPECIFIC (intype)
188 && (IS_SIGNATURE_POINTER (intype) || IS_SIGNATURE_REFERENCE (intype)))
189 return convert_to_pointer (type, build_optr_ref (expr));
190
191 if (IS_AGGR_TYPE (intype))
192 {
193 tree rval;
194 rval = build_type_conversion (CONVERT_EXPR, type, expr, 1);
195 if (rval)
196 {
197 if (rval == error_mark_node)
198 cp_error ("conversion of `%E' from `%T' to `%T' is ambiguous",
199 expr, intype, type);
200 return rval;
201 }
202 }
203
204 if (integer_zerop (expr))
205 {
206 if (type == TREE_TYPE (null_pointer_node))
207 return null_pointer_node;
208 expr = build_int_2 (0, 0);
209 TREE_TYPE (expr) = type;
210 return expr;
211 }
212
213 if (INTEGRAL_CODE_P (form))
214 {
215 if (type_precision (intype) == POINTER_SIZE)
216 return build1 (CONVERT_EXPR, type, expr);
217 expr = convert (type_for_size (POINTER_SIZE, 0), expr);
218 /* Modes may be different but sizes should be the same. */
219 if (GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (expr)))
220 != GET_MODE_SIZE (TYPE_MODE (type)))
221 /* There is supposed to be some integral type
222 that is the same width as a pointer. */
223 abort ();
224 return convert_to_pointer (type, expr);
225 }
226
227 cp_error ("cannot convert `%E' from type `%T' to type `%T'",
228 expr, intype, type);
229 return error_mark_node;
230 }
231
232 /* Like convert, except permit conversions to take place which
233 are not normally allowed due to access restrictions
234 (such as conversion from sub-type to private super-type). */
235 static tree
236 convert_to_pointer_force (type, expr)
237 tree type, expr;
238 {
239 register tree intype = TREE_TYPE (expr);
240 register enum tree_code form = TREE_CODE (intype);
241
242 if (integer_zerop (expr))
243 {
244 if (type == TREE_TYPE (null_pointer_node))
245 return null_pointer_node;
246 expr = build_int_2 (0, 0);
247 TREE_TYPE (expr) = type;
248 return expr;
249 }
250
251 /* Convert signature pointer/reference to `void *' first. */
252 if (form == RECORD_TYPE
253 && (IS_SIGNATURE_POINTER (intype) || IS_SIGNATURE_REFERENCE (intype)))
254 {
255 expr = build_optr_ref (expr);
256 intype = TREE_TYPE (expr);
257 form = TREE_CODE (intype);
258 }
259
260 if (form == POINTER_TYPE)
261 {
262 intype = TYPE_MAIN_VARIANT (intype);
263
264 if (TYPE_MAIN_VARIANT (type) != intype
265 && TREE_CODE (TREE_TYPE (type)) == RECORD_TYPE
266 && TREE_CODE (TREE_TYPE (intype)) == RECORD_TYPE)
267 {
268 enum tree_code code = PLUS_EXPR;
269 tree path;
270 int distance = get_base_distance (TREE_TYPE (type),
271 TREE_TYPE (intype), 0, &path);
272 if (distance == -2)
273 {
274 ambig:
275 cp_error ("type `%T' is ambiguous baseclass of `%s'", TREE_TYPE (type),
276 TYPE_NAME_STRING (TREE_TYPE (intype)));
277 return error_mark_node;
278 }
279 if (distance == -1)
280 {
281 distance = get_base_distance (TREE_TYPE (intype),
282 TREE_TYPE (type), 0, &path);
283 if (distance == -2)
284 goto ambig;
285 if (distance < 0)
286 /* Doesn't need any special help from us. */
287 return build1 (NOP_EXPR, type, expr);
288
289 code = MINUS_EXPR;
290 }
291 return build_vbase_path (code, type, expr, path, 0);
292 }
293 return build1 (NOP_EXPR, type, expr);
294 }
295
296 return cp_convert_to_pointer (type, expr);
297 }
298
299 /* We are passing something to a function which requires a reference.
300 The type we are interested in is in TYPE. The initial
301 value we have to begin with is in ARG.
302
303 FLAGS controls how we manage access checking.
304 CHECKCONST controls if we report error messages on const subversion. */
305 static tree
306 build_up_reference (type, arg, flags, checkconst)
307 tree type, arg;
308 int flags, checkconst;
309 {
310 tree rval, targ;
311 int literal_flag = 0;
312 tree argtype = TREE_TYPE (arg);
313 tree target_type = TREE_TYPE (type);
314 tree binfo = NULL_TREE;
315
316 my_friendly_assert (TREE_CODE (type) == REFERENCE_TYPE, 187);
317 if ((flags & LOOKUP_PROTECT)
318 && TYPE_MAIN_VARIANT (argtype) != TYPE_MAIN_VARIANT (target_type)
319 && IS_AGGR_TYPE (argtype)
320 && IS_AGGR_TYPE (target_type))
321 {
322 binfo = get_binfo (target_type, argtype, 1);
323 if (binfo == error_mark_node)
324 return error_mark_node;
325 if (binfo == NULL_TREE)
326 return error_not_base_type (target_type, argtype);
327 }
328
329 /* Pass along const and volatile down into the type. */
330 if (TYPE_READONLY (type) || TYPE_VOLATILE (type))
331 target_type = cp_build_type_variant (target_type, TYPE_READONLY (type),
332 TYPE_VOLATILE (type));
333 targ = arg;
334 if (TREE_CODE (targ) == SAVE_EXPR)
335 targ = TREE_OPERAND (targ, 0);
336 while (TREE_CODE (targ) == NOP_EXPR
337 && (TYPE_MAIN_VARIANT (argtype)
338 == TYPE_MAIN_VARIANT (TREE_TYPE (TREE_OPERAND (targ, 0)))))
339 targ = TREE_OPERAND (targ, 0);
340
341 switch (TREE_CODE (targ))
342 {
343 case INDIRECT_REF:
344 /* This is a call to a constructor which did not know what it was
345 initializing until now: it needs to initialize a temporary. */
346 if (TREE_HAS_CONSTRUCTOR (targ))
347 {
348 tree temp = build_cplus_new (argtype, TREE_OPERAND (targ, 0), 1);
349 TREE_HAS_CONSTRUCTOR (targ) = 0;
350 return build_up_reference (type, temp, flags, 1);
351 }
352 /* Let &* cancel out to simplify resulting code.
353 Also, throw away intervening NOP_EXPRs. */
354 arg = TREE_OPERAND (targ, 0);
355 if (TREE_CODE (arg) == NOP_EXPR || TREE_CODE (arg) == NON_LVALUE_EXPR
356 || (TREE_CODE (arg) == CONVERT_EXPR && TREE_REFERENCE_EXPR (arg)))
357 arg = TREE_OPERAND (arg, 0);
358
359 /* in doing a &*, we have to get rid of the const'ness on the pointer
360 value. Haven't thought about volatile here. Pointers come to mind
361 here. */
362 if (TREE_READONLY (arg))
363 {
364 arg = copy_node (arg);
365 TREE_READONLY (arg) = 0;
366 }
367
368 rval = build1 (CONVERT_EXPR, type, arg);
369 TREE_REFERENCE_EXPR (rval) = 1;
370
371 /* propagate the const flag on something like:
372
373 class Base {
374 public:
375 int foo;
376 };
377
378 class Derived : public Base {
379 public:
380 int bar;
381 };
382
383 void func(Base&);
384
385 void func2(const Derived& d) {
386 func(d);
387 }
388
389 on the d parameter. The below could have been avoided, if the flags
390 were down in the tree, not sure why they are not. (mrs) */
391 /* The below code may have to be propagated to other parts of this
392 switch. */
393 if (TREE_READONLY (targ) && !TREE_READONLY (arg)
394 && (TREE_CODE (arg) == PARM_DECL || TREE_CODE (arg) == VAR_DECL)
395 && TREE_CODE (TREE_TYPE (arg)) == REFERENCE_TYPE
396 && (TYPE_READONLY (target_type) && checkconst))
397 {
398 arg = copy_node (arg);
399 TREE_READONLY (arg) = TREE_READONLY (targ);
400 }
401 literal_flag = TREE_CONSTANT (arg);
402
403 goto done;
404
405 /* Get this out of a register if we happened to be in one by accident.
406 Also, build up references to non-lvalues it we must. */
407 /* For &x[y], return (&) x+y */
408 case ARRAY_REF:
409 if (mark_addressable (TREE_OPERAND (targ, 0)) == 0)
410 return error_mark_node;
411 rval = build_binary_op (PLUS_EXPR, TREE_OPERAND (targ, 0),
412 TREE_OPERAND (targ, 1), 1);
413 TREE_TYPE (rval) = type;
414 if (TREE_CONSTANT (TREE_OPERAND (targ, 1))
415 && staticp (TREE_OPERAND (targ, 0)))
416 TREE_CONSTANT (rval) = 1;
417 goto done;
418
419 case SCOPE_REF:
420 /* Could be a reference to a static member. */
421 {
422 tree field = TREE_OPERAND (targ, 1);
423 if (TREE_STATIC (field))
424 {
425 rval = build1 (ADDR_EXPR, type, field);
426 literal_flag = 1;
427 goto done;
428 }
429 }
430
431 /* We should have farmed out member pointers above. */
432 my_friendly_abort (188);
433
434 case COMPONENT_REF:
435 rval = build_component_addr (targ, build_pointer_type (argtype),
436 "attempt to make a reference to bit-field structure member `%s'");
437 TREE_TYPE (rval) = type;
438 literal_flag = staticp (TREE_OPERAND (targ, 0));
439
440 goto done;
441
442 /* Anything not already handled and not a true memory reference
443 needs to have a reference built up. Do so silently for
444 things like integers and return values from function,
445 but complain if we need a reference to something declared
446 as `register'. */
447
448 case RESULT_DECL:
449 if (staticp (targ))
450 literal_flag = 1;
451 TREE_ADDRESSABLE (targ) = 1;
452 put_var_into_stack (targ);
453 break;
454
455 case PARM_DECL:
456 #if 0
457 if (targ == current_class_decl)
458 {
459 error ("address of `this' not available");
460 /* #if 0 */
461 /* This code makes the following core dump the compiler on a sun4,
462 if the code below is used.
463
464 class e_decl;
465 class a_decl;
466 typedef a_decl* a_ref;
467
468 class a_s {
469 public:
470 a_s();
471 void* append(a_ref& item);
472 };
473 class a_decl {
474 public:
475 a_decl (e_decl *parent);
476 a_s generic_s;
477 a_s decls;
478 e_decl* parent;
479 };
480
481 class e_decl {
482 public:
483 e_decl();
484 a_s implementations;
485 };
486
487 void foobar(void *);
488
489 a_decl::a_decl(e_decl *parent) {
490 parent->implementations.append(this);
491 }
492 */
493
494 TREE_ADDRESSABLE (targ) = 1; /* so compiler doesn't die later */
495 put_var_into_stack (targ);
496 break;
497 /* #else */
498 return error_mark_node;
499 /* #endif */
500 }
501 #endif
502 /* Fall through. */
503 case VAR_DECL:
504 case CONST_DECL:
505 if (DECL_REGISTER (targ) && !TREE_ADDRESSABLE (targ)
506 && !DECL_ARTIFICIAL (targ))
507 cp_warning ("address needed to build reference for `%D', which is declared `register'",
508 targ);
509 else if (staticp (targ))
510 literal_flag = 1;
511
512 TREE_ADDRESSABLE (targ) = 1;
513 put_var_into_stack (targ);
514 break;
515
516 case COMPOUND_EXPR:
517 {
518 tree real_reference = build_up_reference (type, TREE_OPERAND (targ, 1),
519 LOOKUP_PROTECT, checkconst);
520 rval = build (COMPOUND_EXPR, type, TREE_OPERAND (targ, 0), real_reference);
521 TREE_CONSTANT (rval) = staticp (TREE_OPERAND (targ, 1));
522 return rval;
523 }
524
525 case PREINCREMENT_EXPR:
526 case PREDECREMENT_EXPR:
527 case MODIFY_EXPR:
528 case INIT_EXPR:
529 {
530 tree real_reference = build_up_reference (type, TREE_OPERAND (targ, 0),
531 LOOKUP_PROTECT, checkconst);
532 rval = build (COMPOUND_EXPR, type, arg, real_reference);
533 TREE_CONSTANT (rval) = staticp (TREE_OPERAND (targ, 0));
534 return rval;
535 }
536
537 case COND_EXPR:
538 return build (COND_EXPR, type,
539 TREE_OPERAND (targ, 0),
540 build_up_reference (type, TREE_OPERAND (targ, 1),
541 LOOKUP_PROTECT, checkconst),
542 build_up_reference (type, TREE_OPERAND (targ, 2),
543 LOOKUP_PROTECT, checkconst));
544
545 /* Undo the folding... */
546 case MIN_EXPR:
547 case MAX_EXPR:
548 return build (COND_EXPR, type,
549 build (TREE_CODE (targ) == MIN_EXPR ? LT_EXPR : GT_EXPR,
550 boolean_type_node, TREE_OPERAND (targ, 0),
551 TREE_OPERAND (targ, 1)),
552 build_up_reference (type, TREE_OPERAND (targ, 0),
553 LOOKUP_PROTECT, checkconst),
554 build_up_reference (type, TREE_OPERAND (targ, 1),
555 LOOKUP_PROTECT, checkconst));
556
557 case WITH_CLEANUP_EXPR:
558 return build (WITH_CLEANUP_EXPR, type,
559 build_up_reference (type, TREE_OPERAND (targ, 0),
560 LOOKUP_PROTECT, checkconst),
561 0, TREE_OPERAND (targ, 2));
562
563 case BIND_EXPR:
564 arg = TREE_OPERAND (targ, 1);
565 if (arg == NULL_TREE)
566 {
567 compiler_error ("({ ... }) expression not expanded when needed for reference");
568 return error_mark_node;
569 }
570 rval = build1 (ADDR_EXPR, type, arg);
571 TREE_REFERENCE_EXPR (rval) = 1;
572 return rval;
573
574 default:
575 break;
576 }
577
578 if (TREE_ADDRESSABLE (targ) == 0)
579 {
580 tree temp;
581
582 if (TREE_CODE (targ) == CALL_EXPR && IS_AGGR_TYPE (argtype))
583 {
584 temp = build_cplus_new (argtype, targ, 1);
585 if (TREE_CODE (temp) == WITH_CLEANUP_EXPR)
586 rval = build (WITH_CLEANUP_EXPR, type,
587 build1 (ADDR_EXPR, type, TREE_OPERAND (temp, 0)),
588 0, TREE_OPERAND (temp, 2));
589 else
590 rval = build1 (ADDR_EXPR, type, temp);
591 goto done;
592 }
593 else
594 {
595 temp = get_temp_name (argtype, 0);
596 if (global_bindings_p ())
597 {
598 /* Give this new temp some rtl and initialize it. */
599 DECL_INITIAL (temp) = targ;
600 TREE_STATIC (temp) = 1;
601 finish_decl (temp, targ, NULL_TREE, 0, LOOKUP_ONLYCONVERTING);
602 /* Do this after declaring it static. */
603 rval = build_unary_op (ADDR_EXPR, temp, 0);
604 TREE_TYPE (rval) = type;
605 literal_flag = TREE_CONSTANT (rval);
606 goto done;
607 }
608 else
609 {
610 rval = build_unary_op (ADDR_EXPR, temp, 0);
611 if (binfo && !BINFO_OFFSET_ZEROP (binfo))
612 rval = convert_pointer_to (target_type, rval);
613 else
614 TREE_TYPE (rval) = type;
615
616 temp = build (MODIFY_EXPR, argtype, temp, arg);
617 TREE_SIDE_EFFECTS (temp) = 1;
618 return build (COMPOUND_EXPR, type, temp, rval);
619 }
620 }
621 }
622 else
623 rval = build1 (ADDR_EXPR, type, arg);
624
625 done:
626 if (TYPE_USES_COMPLEX_INHERITANCE (argtype)
627 || TYPE_USES_COMPLEX_INHERITANCE (target_type))
628 {
629 TREE_TYPE (rval) = build_pointer_type (argtype);
630 if (flags & LOOKUP_PROTECT)
631 rval = convert_pointer_to (target_type, rval);
632 else
633 rval
634 = convert_to_pointer_force (build_pointer_type (target_type), rval);
635 TREE_TYPE (rval) = type;
636 if (TREE_CODE (rval) == PLUS_EXPR || TREE_CODE (rval) == MINUS_EXPR)
637 TREE_TYPE (TREE_OPERAND (rval, 0))
638 = TREE_TYPE (TREE_OPERAND (rval, 1)) = type;
639 }
640 TREE_CONSTANT (rval) = literal_flag;
641 return rval;
642 }
643
644 /* For C++: Only need to do one-level references, but cannot
645 get tripped up on signed/unsigned differences.
646
647 DECL is either NULL_TREE or the _DECL node for a reference that is being
648 initialized. It can be error_mark_node if we don't know the _DECL but
649 we know it's an initialization. */
650
651 tree
652 convert_to_reference (reftype, expr, convtype, flags, decl)
653 tree reftype, expr;
654 int convtype, flags;
655 tree decl;
656 {
657 register tree type = TYPE_MAIN_VARIANT (TREE_TYPE (reftype));
658 register tree intype = TREE_TYPE (expr);
659 tree rval = NULL_TREE;
660 tree rval_as_conversion = NULL_TREE;
661 int i;
662
663 if (TREE_CODE (intype) == REFERENCE_TYPE)
664 my_friendly_abort (364);
665
666 intype = TYPE_MAIN_VARIANT (intype);
667
668 i = comp_target_types (type, intype, 0);
669
670 if (i <= 0 && (convtype & CONV_IMPLICIT) && IS_AGGR_TYPE (intype)
671 && ! (flags & LOOKUP_NO_CONVERSION))
672 {
673 /* Look for a user-defined conversion to lvalue that we can use. */
674
675 rval_as_conversion = build_type_conversion (CONVERT_EXPR, type, expr, 1);
676
677 if (rval_as_conversion && rval_as_conversion != error_mark_node
678 && real_lvalue_p (rval_as_conversion))
679 {
680 expr = rval_as_conversion;
681 rval_as_conversion = NULL_TREE;
682 intype = type;
683 i = 1;
684 }
685 }
686
687 if (((convtype & CONV_STATIC) && i == -1)
688 || ((convtype & CONV_IMPLICIT) && i == 1))
689 {
690 if (flags & LOOKUP_COMPLAIN)
691 {
692 tree ttl = TREE_TYPE (reftype);
693 tree ttr;
694
695 {
696 int r = TREE_READONLY (expr);
697 int v = TREE_THIS_VOLATILE (expr);
698 ttr = cp_build_type_variant (TREE_TYPE (expr), r, v);
699 }
700
701 if (! real_lvalue_p (expr) &&
702 (decl == NULL_TREE || ! TYPE_READONLY (ttl)))
703 {
704 if (decl)
705 /* Ensure semantics of [dcl.init.ref] */
706 cp_pedwarn ("initialization of non-const `%T' from rvalue `%T'",
707 reftype, intype);
708 else
709 cp_pedwarn ("conversion to `%T' from rvalue `%T'",
710 reftype, intype);
711 }
712 else if (! (convtype & CONV_CONST))
713 {
714 if (! TYPE_READONLY (ttl) && TYPE_READONLY (ttr))
715 cp_pedwarn ("conversion from `%T' to `%T' discards const",
716 ttr, reftype);
717 else if (! TYPE_VOLATILE (ttl) && TYPE_VOLATILE (ttr))
718 cp_pedwarn ("conversion from `%T' to `%T' discards volatile",
719 ttr, reftype);
720 }
721 }
722
723 return build_up_reference (reftype, expr, flags,
724 ! (convtype & CONV_CONST));
725 }
726 else if ((convtype & CONV_REINTERPRET) && lvalue_p (expr))
727 {
728 /* When casting an lvalue to a reference type, just convert into
729 a pointer to the new type and deference it. This is allowed
730 by San Diego WP section 5.2.9 paragraph 12, though perhaps it
731 should be done directly (jason). (int &)ri ---> *(int*)&ri */
732
733 /* B* bp; A& ar = (A&)bp; is valid, but it's probably not what they
734 meant. */
735 if (TREE_CODE (intype) == POINTER_TYPE
736 && (comptypes (TREE_TYPE (intype), type, -1)))
737 cp_warning ("casting `%T' to `%T' does not dereference pointer",
738 intype, reftype);
739
740 rval = build_unary_op (ADDR_EXPR, expr, 0);
741 if (rval != error_mark_node)
742 rval = convert_force (build_pointer_type (TREE_TYPE (reftype)), rval, 0);
743 if (rval != error_mark_node)
744 rval = build1 (NOP_EXPR, reftype, rval);
745 }
746 else if (decl)
747 {
748 tree rval_as_ctor = NULL_TREE;
749
750 if (rval_as_conversion)
751 {
752 if (rval_as_conversion == error_mark_node)
753 {
754 cp_error ("conversion from `%T' to `%T' is ambiguous",
755 intype, reftype);
756 return error_mark_node;
757 }
758 rval_as_conversion = build_up_reference (reftype, rval_as_conversion,
759 flags, 1);
760 }
761
762 /* Definitely need to go through a constructor here. */
763 if (TYPE_HAS_CONSTRUCTOR (type)
764 && ! CLASSTYPE_ABSTRACT_VIRTUALS (type)
765 && (rval = build_method_call
766 (NULL_TREE, constructor_name_full (type),
767 build_tree_list (NULL_TREE, expr), TYPE_BINFO (type),
768 LOOKUP_NO_CONVERSION|LOOKUP_SPECULATIVELY
769 | LOOKUP_ONLYCONVERTING)))
770 {
771 tree init;
772
773 if (global_bindings_p ())
774 {
775 extern tree static_aggregates;
776 tree t = get_temp_name (type, global_bindings_p ());
777 init = build_method_call (t, constructor_name_full (type),
778 build_tree_list (NULL_TREE, expr),
779 TYPE_BINFO (type),
780 LOOKUP_NORMAL|LOOKUP_NO_CONVERSION
781 | LOOKUP_ONLYCONVERTING);
782
783 if (init == error_mark_node)
784 return error_mark_node;
785
786 make_decl_rtl (t, NULL_PTR, 1);
787 static_aggregates = perm_tree_cons (expr, t, static_aggregates);
788 rval = build_unary_op (ADDR_EXPR, t, 0);
789 }
790 else
791 {
792 init = build_method_call (NULL_TREE, constructor_name_full (type),
793 build_tree_list (NULL_TREE, expr),
794 TYPE_BINFO (type),
795 LOOKUP_NORMAL|LOOKUP_NO_CONVERSION
796 |LOOKUP_ONLYCONVERTING);
797
798 if (init == error_mark_node)
799 return error_mark_node;
800
801 rval = build_cplus_new (type, init, 1);
802 rval = build_up_reference (reftype, rval, flags, 1);
803 }
804 rval_as_ctor = rval;
805 }
806
807 if (rval_as_ctor && rval_as_conversion)
808 {
809 cp_error ("ambiguous conversion from `%T' to `%T'; both user-defined conversion and constructor apply",
810 intype, reftype);
811 return error_mark_node;
812 }
813 else if (rval_as_ctor)
814 rval = rval_as_ctor;
815 else if (rval_as_conversion)
816 rval = rval_as_conversion;
817 else if (! IS_AGGR_TYPE (type) && ! IS_AGGR_TYPE (intype))
818 {
819 rval = convert (type, expr);
820 if (rval == error_mark_node)
821 return error_mark_node;
822
823 rval = build_up_reference (reftype, rval, flags, 1);
824 }
825
826 if (rval && ! TYPE_READONLY (TREE_TYPE (reftype)))
827 cp_pedwarn ("initializing non-const `%T' with `%T' will use a temporary",
828 reftype, intype);
829 }
830
831 if (rval)
832 {
833 /* If we found a way to convert earlier, then use it. */
834 return rval;
835 }
836
837 my_friendly_assert (TREE_CODE (intype) != OFFSET_TYPE, 189);
838
839 if (flags & LOOKUP_SPECULATIVELY)
840 return NULL_TREE;
841
842 else if (flags & LOOKUP_COMPLAIN)
843 cp_error ("cannot convert type `%T' to type `%T'", intype, reftype);
844
845 return error_mark_node;
846 }
847
848 /* We are using a reference VAL for its value. Bash that reference all the
849 way down to its lowest form. */
850 tree
851 convert_from_reference (val)
852 tree val;
853 {
854 tree type = TREE_TYPE (val);
855
856 if (TREE_CODE (type) == OFFSET_TYPE)
857 type = TREE_TYPE (type);
858 if (TREE_CODE (type) == REFERENCE_TYPE)
859 {
860 tree target_type = TREE_TYPE (type);
861 tree nval;
862
863 /* This can happen if we cast to a reference type. */
864 if (TREE_CODE (val) == ADDR_EXPR)
865 {
866 nval = build1 (NOP_EXPR, build_pointer_type (target_type), val);
867 nval = build_indirect_ref (nval, NULL_PTR);
868 /* The below was missing, are other important flags missing too? */
869 TREE_SIDE_EFFECTS (nval) = TREE_SIDE_EFFECTS (val);
870 return nval;
871 }
872
873 nval = build1 (INDIRECT_REF, target_type, val);
874
875 TREE_THIS_VOLATILE (nval) = TYPE_VOLATILE (target_type);
876 TREE_SIDE_EFFECTS (nval) = TYPE_VOLATILE (target_type);
877 TREE_READONLY (nval) = TYPE_READONLY (target_type);
878 /* The below was missing, are other important flags missing too? */
879 TREE_SIDE_EFFECTS (nval) |= TREE_SIDE_EFFECTS (val);
880 return nval;
881 }
882 return val;
883 }
884 \f
885 /* See if there is a constructor of type TYPE which will convert
886 EXPR. The reference manual seems to suggest (8.5.6) that we need
887 not worry about finding constructors for base classes, then converting
888 to the derived class.
889
890 MSGP is a pointer to a message that would be an appropriate error
891 string. If MSGP is NULL, then we are not interested in reporting
892 errors. */
893 tree
894 convert_to_aggr (type, expr, msgp, protect)
895 tree type, expr;
896 char **msgp;
897 int protect;
898 {
899 tree basetype = type;
900 tree name = TYPE_IDENTIFIER (basetype);
901 tree function, fndecl, fntype, parmtypes, parmlist, result;
902 tree method_name;
903 enum access_type access;
904 int can_be_private, can_be_protected;
905
906 if (! TYPE_HAS_CONSTRUCTOR (basetype))
907 {
908 if (msgp)
909 *msgp = "type `%s' does not have a constructor";
910 return error_mark_node;
911 }
912
913 access = access_public;
914 can_be_private = 0;
915 can_be_protected = IDENTIFIER_CLASS_VALUE (name) || name == current_class_name;
916
917 parmlist = build_tree_list (NULL_TREE, expr);
918 parmtypes = tree_cons (NULL_TREE, TREE_TYPE (expr), void_list_node);
919
920 if (TYPE_USES_VIRTUAL_BASECLASSES (basetype))
921 {
922 parmtypes = tree_cons (NULL_TREE, integer_type_node, parmtypes);
923 parmlist = tree_cons (NULL_TREE, integer_one_node, parmlist);
924 }
925
926 /* The type of the first argument will be filled in inside the loop. */
927 parmlist = tree_cons (NULL_TREE, integer_zero_node, parmlist);
928 parmtypes = tree_cons (NULL_TREE, TYPE_POINTER_TO (basetype), parmtypes);
929
930 #if 0
931 method_name = build_decl_overload (name, parmtypes, 1);
932
933 /* constructors are up front. */
934 fndecl = TREE_VEC_ELT (CLASSTYPE_METHOD_VEC (basetype), 0);
935 if (TYPE_HAS_DESTRUCTOR (basetype))
936 fndecl = DECL_CHAIN (fndecl);
937
938 while (fndecl)
939 {
940 if (DECL_ASSEMBLER_NAME (fndecl) == method_name)
941 {
942 function = fndecl;
943 if (protect)
944 {
945 if (TREE_PRIVATE (fndecl))
946 {
947 can_be_private =
948 (basetype == current_class_type
949 || is_friend (basetype, current_function_decl)
950 || purpose_member (basetype, DECL_ACCESS (fndecl)));
951 if (! can_be_private)
952 goto found;
953 }
954 else if (TREE_PROTECTED (fndecl))
955 {
956 if (! can_be_protected)
957 goto found;
958 }
959 }
960 goto found_and_ok;
961 }
962 fndecl = DECL_CHAIN (fndecl);
963 }
964 #endif
965
966 /* No exact conversion was found. See if an approximate
967 one will do. */
968 fndecl = TREE_VEC_ELT (CLASSTYPE_METHOD_VEC (basetype), 0);
969 if (TYPE_HAS_DESTRUCTOR (basetype))
970 fndecl = DECL_CHAIN (fndecl);
971
972 {
973 int saw_private = 0;
974 int saw_protected = 0;
975 struct candidate *candidates =
976 (struct candidate *) alloca ((decl_list_length (fndecl)+1) * sizeof (struct candidate));
977 struct candidate *cp = candidates;
978
979 while (fndecl)
980 {
981 function = fndecl;
982 cp->h_len = 2;
983 cp->harshness = (struct harshness_code *)
984 alloca (3 * sizeof (struct harshness_code));
985
986 compute_conversion_costs (fndecl, parmlist, cp, 2);
987 if ((cp->h.code & EVIL_CODE) == 0)
988 {
989 cp->u.field = fndecl;
990 if (protect)
991 {
992 if (TREE_PRIVATE (fndecl))
993 access = access_private;
994 else if (TREE_PROTECTED (fndecl))
995 access = access_protected;
996 else
997 access = access_public;
998 }
999 else
1000 access = access_public;
1001
1002 if (access == access_private
1003 ? (basetype == current_class_type
1004 || is_friend (basetype, cp->function)
1005 || purpose_member (basetype, DECL_ACCESS (fndecl)))
1006 : access == access_protected
1007 ? (can_be_protected
1008 || purpose_member (basetype, DECL_ACCESS (fndecl)))
1009 : 1)
1010 {
1011 if (cp->h.code <= TRIVIAL_CODE)
1012 goto found_and_ok;
1013 cp++;
1014 }
1015 else
1016 {
1017 if (access == access_private)
1018 saw_private = 1;
1019 else
1020 saw_protected = 1;
1021 }
1022 }
1023 fndecl = DECL_CHAIN (fndecl);
1024 }
1025 if (cp - candidates)
1026 {
1027 /* Rank from worst to best. Then cp will point to best one.
1028 Private fields have their bits flipped. For unsigned
1029 numbers, this should make them look very large.
1030 If the best alternate has a (signed) negative value,
1031 then all we ever saw were private members. */
1032 if (cp - candidates > 1)
1033 qsort (candidates, /* char *base */
1034 cp - candidates, /* int nel */
1035 sizeof (struct candidate), /* int width */
1036 rank_for_overload); /* int (*compar)() */
1037
1038 --cp;
1039 if (cp->h.code & EVIL_CODE)
1040 {
1041 if (msgp)
1042 *msgp = "ambiguous type conversion possible for `%s'";
1043 return error_mark_node;
1044 }
1045
1046 function = cp->function;
1047 fndecl = cp->u.field;
1048 goto found_and_ok;
1049 }
1050 else if (msgp)
1051 {
1052 if (saw_private)
1053 if (saw_protected)
1054 *msgp = "only private and protected conversions apply";
1055 else
1056 *msgp = "only private conversions apply";
1057 else if (saw_protected)
1058 *msgp = "only protected conversions apply";
1059 else
1060 *msgp = "no appropriate conversion to type `%s'";
1061 }
1062 return error_mark_node;
1063 }
1064 /* NOTREACHED */
1065
1066 found:
1067 if (access == access_private)
1068 if (! can_be_private)
1069 {
1070 if (msgp)
1071 *msgp = TREE_PRIVATE (fndecl)
1072 ? "conversion to type `%s' is private"
1073 : "conversion to type `%s' is from private base class";
1074 return error_mark_node;
1075 }
1076 if (access == access_protected)
1077 if (! can_be_protected)
1078 {
1079 if (msgp)
1080 *msgp = TREE_PRIVATE (fndecl)
1081 ? "conversion to type `%s' is protected"
1082 : "conversion to type `%s' is from protected base class";
1083 return error_mark_node;
1084 }
1085 function = fndecl;
1086 found_and_ok:
1087
1088 /* It will convert, but we don't do anything about it yet. */
1089 if (msgp == 0)
1090 return NULL_TREE;
1091
1092 fntype = TREE_TYPE (function);
1093 if (DECL_INLINE (function) && TREE_CODE (function) == FUNCTION_DECL)
1094 function = build1 (ADDR_EXPR, build_pointer_type (fntype), function);
1095 else
1096 function = default_conversion (function);
1097
1098 result = build_nt (CALL_EXPR, function,
1099 convert_arguments (NULL_TREE, TYPE_ARG_TYPES (fntype),
1100 parmlist, NULL_TREE, LOOKUP_NORMAL),
1101 NULL_TREE);
1102 TREE_TYPE (result) = TREE_TYPE (fntype);
1103 TREE_SIDE_EFFECTS (result) = 1;
1104 return result;
1105 }
1106
1107 /* Call this when we know (for any reason) that expr is not, in fact,
1108 zero. This routine is like convert_pointer_to, but it pays
1109 attention to which specific instance of what type we want to
1110 convert to. This routine should eventually become
1111 convert_to_pointer after all references to convert_to_pointer
1112 are removed. */
1113 tree
1114 convert_pointer_to_real (binfo, expr)
1115 tree binfo, expr;
1116 {
1117 register tree intype = TREE_TYPE (expr);
1118 tree ptr_type;
1119 tree type, rval;
1120
1121 if (TREE_CODE (binfo) == TREE_VEC)
1122 type = BINFO_TYPE (binfo);
1123 else if (IS_AGGR_TYPE (binfo))
1124 {
1125 type = binfo;
1126 }
1127 else
1128 {
1129 type = binfo;
1130 binfo = NULL_TREE;
1131 }
1132
1133 ptr_type = build_pointer_type (type);
1134 if (ptr_type == TYPE_MAIN_VARIANT (intype))
1135 return expr;
1136
1137 if (intype == error_mark_node)
1138 return error_mark_node;
1139
1140 my_friendly_assert (!integer_zerop (expr), 191);
1141
1142 if (TREE_CODE (type) == RECORD_TYPE
1143 && TREE_CODE (TREE_TYPE (intype)) == RECORD_TYPE
1144 && type != TYPE_MAIN_VARIANT (TREE_TYPE (intype)))
1145 {
1146 tree path;
1147 int distance
1148 = get_base_distance (binfo, TYPE_MAIN_VARIANT (TREE_TYPE (intype)),
1149 0, &path);
1150
1151 /* This function shouldn't be called with unqualified arguments
1152 but if it is, give them an error message that they can read. */
1153 if (distance < 0)
1154 {
1155 cp_error ("cannot convert a pointer of type `%T' to a pointer of type `%T'",
1156 TREE_TYPE (intype), type);
1157
1158 if (distance == -2)
1159 cp_error ("because `%T' is an ambiguous base class", type);
1160 return error_mark_node;
1161 }
1162
1163 return build_vbase_path (PLUS_EXPR, ptr_type, expr, path, 1);
1164 }
1165 rval = build1 (NOP_EXPR, ptr_type,
1166 TREE_CODE (expr) == NOP_EXPR ? TREE_OPERAND (expr, 0) : expr);
1167 TREE_CONSTANT (rval) = TREE_CONSTANT (expr);
1168 return rval;
1169 }
1170
1171 /* Call this when we know (for any reason) that expr is
1172 not, in fact, zero. This routine gets a type out of the first
1173 argument and uses it to search for the type to convert to. If there
1174 is more than one instance of that type in the expr, the conversion is
1175 ambiguous. This routine should eventually go away, and all
1176 callers should use convert_to_pointer_real. */
1177 tree
1178 convert_pointer_to (binfo, expr)
1179 tree binfo, expr;
1180 {
1181 tree type;
1182
1183 if (TREE_CODE (binfo) == TREE_VEC)
1184 type = BINFO_TYPE (binfo);
1185 else if (IS_AGGR_TYPE (binfo))
1186 type = binfo;
1187 else
1188 type = binfo;
1189 return convert_pointer_to_real (type, expr);
1190 }
1191
1192 /* Same as above, but don't abort if we get an "ambiguous" baseclass.
1193 There's only one virtual baseclass we are looking for, and once
1194 we find one such virtual baseclass, we have found them all. */
1195
1196 tree
1197 convert_pointer_to_vbase (binfo, expr)
1198 tree binfo;
1199 tree expr;
1200 {
1201 tree intype = TREE_TYPE (TREE_TYPE (expr));
1202 tree binfos = TYPE_BINFO_BASETYPES (intype);
1203 int i;
1204
1205 for (i = TREE_VEC_LENGTH (binfos)-1; i >= 0; i--)
1206 {
1207 tree basetype = BINFO_TYPE (TREE_VEC_ELT (binfos, i));
1208 if (BINFO_TYPE (binfo) == basetype)
1209 return convert_pointer_to (binfo, expr);
1210 if (binfo_member (BINFO_TYPE (binfo), CLASSTYPE_VBASECLASSES (basetype)))
1211 return convert_pointer_to_vbase (binfo, convert_pointer_to (basetype, expr));
1212 }
1213 my_friendly_abort (6);
1214 /* NOTREACHED */
1215 return NULL_TREE;
1216 }
1217 \f
1218 tree
1219 cp_convert (type, expr, convtype, flags)
1220 tree type, expr;
1221 int convtype, flags;
1222 {
1223 register tree e = expr;
1224 register enum tree_code code = TREE_CODE (type);
1225
1226 if (TREE_CODE (e) == ERROR_MARK
1227 || TREE_CODE (TREE_TYPE (e)) == ERROR_MARK)
1228 return error_mark_node;
1229
1230 if (IS_AGGR_TYPE (type) && (convtype & CONV_FORCE_TEMP))
1231 /* We need a new temporary; don't take this shortcut. */;
1232 else if (TYPE_MAIN_VARIANT (type) == TYPE_MAIN_VARIANT (TREE_TYPE (e)))
1233 /* Trivial conversion: cv-qualifiers do not matter on rvalues. */
1234 return fold (build1 (NOP_EXPR, type, e));
1235
1236 if (code == VOID_TYPE && (convtype & CONV_STATIC))
1237 return build1 (CONVERT_EXPR, type, e);
1238
1239 #if 0
1240 /* This is incorrect. A truncation can't be stripped this way.
1241 Extensions will be stripped by the use of get_unwidened. */
1242 if (TREE_CODE (e) == NOP_EXPR)
1243 return convert (type, TREE_OPERAND (e, 0));
1244 #endif
1245
1246 /* Just convert to the type of the member. */
1247 if (code == OFFSET_TYPE)
1248 {
1249 type = TREE_TYPE (type);
1250 code = TREE_CODE (type);
1251 }
1252
1253 #if 0
1254 if (code == REFERENCE_TYPE)
1255 return fold (convert_to_reference (type, e, convtype, flags, NULL_TREE));
1256 else if (TREE_CODE (TREE_TYPE (e)) == REFERENCE_TYPE)
1257 e = convert_from_reference (e);
1258 #endif
1259
1260 if (TREE_CODE (e) == OFFSET_REF)
1261 e = resolve_offset_ref (e);
1262
1263 if (TREE_READONLY_DECL_P (e))
1264 e = decl_constant_value (e);
1265
1266 if (INTEGRAL_CODE_P (code))
1267 {
1268 tree intype = TREE_TYPE (e);
1269 enum tree_code form = TREE_CODE (intype);
1270 /* enum = enum, enum = int, enum = float are all errors. */
1271 if (flag_int_enum_equivalence == 0
1272 && TREE_CODE (type) == ENUMERAL_TYPE
1273 && ARITHMETIC_TYPE_P (intype)
1274 && ! (convtype & CONV_STATIC))
1275 {
1276 cp_pedwarn ("conversion from `%#T' to `%#T'", intype, type);
1277
1278 if (flag_pedantic_errors)
1279 return error_mark_node;
1280 }
1281 if (IS_AGGR_TYPE (intype))
1282 {
1283 tree rval;
1284 rval = build_type_conversion (CONVERT_EXPR, type, e, 1);
1285 if (rval)
1286 return rval;
1287 cp_error ("`%#T' used where a `%T' was expected", intype, type);
1288 return error_mark_node;
1289 }
1290 if (code == BOOLEAN_TYPE)
1291 return truthvalue_conversion (e);
1292 return fold (convert_to_integer (type, e));
1293 }
1294 if (code == POINTER_TYPE || code == REFERENCE_TYPE
1295 || TYPE_PTRMEMFUNC_P (type))
1296 return fold (cp_convert_to_pointer (type, e));
1297 if (code == REAL_TYPE)
1298 {
1299 if (IS_AGGR_TYPE (TREE_TYPE (e)))
1300 {
1301 tree rval;
1302 rval = build_type_conversion (CONVERT_EXPR, type, e, 1);
1303 if (rval)
1304 return rval;
1305 else
1306 cp_error ("`%#T' used where a floating point value was expected",
1307 TREE_TYPE (e));
1308 }
1309 return fold (convert_to_real (type, e));
1310 }
1311
1312 /* New C++ semantics: since assignment is now based on
1313 memberwise copying, if the rhs type is derived from the
1314 lhs type, then we may still do a conversion. */
1315 if (IS_AGGR_TYPE_CODE (code))
1316 {
1317 tree dtype = TREE_TYPE (e);
1318 tree ctor = NULL_TREE;
1319 tree conversion = NULL_TREE;
1320
1321 dtype = TYPE_MAIN_VARIANT (dtype);
1322
1323 /* Conversion of object pointers or signature pointers/references
1324 to signature pointers/references. */
1325
1326 if (TYPE_LANG_SPECIFIC (type)
1327 && (IS_SIGNATURE_POINTER (type) || IS_SIGNATURE_REFERENCE (type)))
1328 {
1329 tree constructor = build_signature_pointer_constructor (type, expr);
1330 tree sig_ty = SIGNATURE_TYPE (type);
1331 tree sig_ptr;
1332
1333 if (constructor == error_mark_node)
1334 return error_mark_node;
1335
1336 sig_ptr = get_temp_name (type, 1);
1337 DECL_INITIAL (sig_ptr) = constructor;
1338 CLEAR_SIGNATURE (sig_ty);
1339 finish_decl (sig_ptr, constructor, NULL_TREE, 0, 0);
1340 SET_SIGNATURE (sig_ty);
1341 TREE_READONLY (sig_ptr) = 1;
1342
1343 return sig_ptr;
1344 }
1345
1346 /* Conversion between aggregate types. New C++ semantics allow
1347 objects of derived type to be cast to objects of base type.
1348 Old semantics only allowed this between pointers.
1349
1350 There may be some ambiguity between using a constructor
1351 vs. using a type conversion operator when both apply. */
1352
1353 if (IS_AGGR_TYPE (dtype) && ! DERIVED_FROM_P (type, dtype)
1354 && TYPE_HAS_CONVERSION (dtype))
1355 conversion = build_type_conversion (CONVERT_EXPR, type, e, 1);
1356
1357 if (conversion == error_mark_node)
1358 {
1359 error ("ambiguous pointer conversion");
1360 return conversion;
1361 }
1362
1363 if (TYPE_HAS_CONSTRUCTOR (type))
1364 ctor = build_method_call (NULL_TREE, constructor_name_full (type),
1365 build_tree_list (NULL_TREE, e),
1366 TYPE_BINFO (type),
1367 LOOKUP_NORMAL | LOOKUP_SPECULATIVELY
1368 | (convtype&CONV_NONCONVERTING ? 0 : LOOKUP_ONLYCONVERTING)
1369 | (conversion ? LOOKUP_NO_CONVERSION : 0));
1370
1371 if (ctor == error_mark_node)
1372 {
1373 cp_error ("in conversion to type `%T'", type);
1374 return error_mark_node;
1375 }
1376
1377 if (conversion && ctor)
1378 {
1379 error ("both constructor and type conversion operator apply");
1380 return error_mark_node;
1381 }
1382 else if (conversion)
1383 return conversion;
1384 else if (ctor)
1385 {
1386 if (current_function_decl)
1387 /* We can't pass 1 to the with_cleanup_p arg here, because that
1388 screws up passing classes by value. */
1389 ctor = build_cplus_new (type, ctor, 0);
1390 else
1391 {
1392 register tree parm = TREE_OPERAND (ctor, 1);
1393
1394 /* Initializers for static variables and parameters
1395 have to handle doing the initialization and
1396 cleanup themselves. */
1397 my_friendly_assert (TREE_CODE (ctor) == CALL_EXPR, 322);
1398 #if 0
1399 /* The following assertion fails in cases where we
1400 are initializing a static member variable of a
1401 particular instance of a template class with a
1402 call to a constructor of the given instance, as
1403 in:
1404
1405 TMPL<int> object = TMPL<int>();
1406
1407 Curiously, the assertion does not fail if we do
1408 the same thing for a static member of a
1409 non-template class, as in:
1410
1411 T object = T();
1412
1413 I can't see why we should care here whether or not
1414 the initializer expression involves a call to
1415 `new', so for the time being, it seems best to
1416 just avoid doing this assertion. */
1417 my_friendly_assert (TREE_CALLS_NEW (TREE_VALUE (parm)),
1418 323);
1419 #endif
1420 TREE_VALUE (parm) = NULL_TREE;
1421 ctor = build_indirect_ref (ctor, NULL_PTR);
1422 TREE_HAS_CONSTRUCTOR (ctor) = 1;
1423 }
1424 return ctor;
1425 }
1426 }
1427
1428 /* If TYPE or TREE_TYPE (E) is not on the permanent_obstack,
1429 then the it won't be hashed and hence compare as not equal,
1430 even when it is. */
1431 if (code == ARRAY_TYPE
1432 && TREE_TYPE (TREE_TYPE (e)) == TREE_TYPE (type)
1433 && index_type_equal (TYPE_DOMAIN (TREE_TYPE (e)), TYPE_DOMAIN (type)))
1434 return e;
1435
1436 cp_error ("conversion from `%T' to non-scalar type `%T' requested",
1437 TREE_TYPE (expr), type);
1438 return error_mark_node;
1439 }
1440
1441 /* Create an expression whose value is that of EXPR,
1442 converted to type TYPE. The TREE_TYPE of the value
1443 is always TYPE. This function implements all reasonable
1444 conversions; callers should filter out those that are
1445 not permitted by the language being compiled. */
1446
1447 tree
1448 convert (type, expr)
1449 tree type, expr;
1450 {
1451 return cp_convert (type, expr, CONV_OLD_CONVERT, 0);
1452 }
1453
1454 /* Like convert, except permit conversions to take place which
1455 are not normally allowed due to access restrictions
1456 (such as conversion from sub-type to private super-type). */
1457 tree
1458 convert_force (type, expr, convtype)
1459 tree type;
1460 tree expr;
1461 int convtype;
1462 {
1463 register tree e = expr;
1464 register enum tree_code code = TREE_CODE (type);
1465
1466 if (code == REFERENCE_TYPE)
1467 return fold (convert_to_reference (type, e, CONV_C_CAST, LOOKUP_COMPLAIN,
1468 NULL_TREE));
1469 else if (TREE_CODE (TREE_TYPE (e)) == REFERENCE_TYPE)
1470 e = convert_from_reference (e);
1471
1472 if (code == POINTER_TYPE)
1473 return fold (convert_to_pointer_force (type, e));
1474
1475 /* From typeck.c convert_for_assignment */
1476 if (((TREE_CODE (TREE_TYPE (e)) == POINTER_TYPE && TREE_CODE (e) == ADDR_EXPR
1477 && TREE_CODE (TREE_TYPE (e)) == POINTER_TYPE
1478 && TREE_CODE (TREE_TYPE (TREE_TYPE (e))) == METHOD_TYPE)
1479 || integer_zerop (e)
1480 || TYPE_PTRMEMFUNC_P (TREE_TYPE (e)))
1481 && TYPE_PTRMEMFUNC_P (type))
1482 {
1483 /* compatible pointer to member functions. */
1484 return build_ptrmemfunc (TYPE_PTRMEMFUNC_FN_TYPE (type), e, 1);
1485 }
1486
1487 return cp_convert (type, e, CONV_C_CAST|convtype, 0);
1488 }
1489
1490 /* Subroutine of build_type_conversion. */
1491 static tree
1492 build_type_conversion_1 (xtype, basetype, expr, typename, for_sure)
1493 tree xtype, basetype;
1494 tree expr;
1495 tree typename;
1496 int for_sure;
1497 {
1498 tree rval;
1499 int flags;
1500
1501 if (for_sure == 0)
1502 flags = LOOKUP_PROTECT|LOOKUP_ONLYCONVERTING;
1503 else
1504 flags = LOOKUP_NORMAL|LOOKUP_ONLYCONVERTING;
1505
1506 rval = build_method_call (expr, typename, NULL_TREE, NULL_TREE, flags);
1507 if (rval == error_mark_node)
1508 {
1509 if (for_sure == 0)
1510 return NULL_TREE;
1511 return error_mark_node;
1512 }
1513
1514 if (IS_AGGR_TYPE (TREE_TYPE (rval)))
1515 return rval;
1516
1517 if (warn_cast_qual
1518 && TREE_TYPE (xtype)
1519 && (TREE_READONLY (TREE_TYPE (TREE_TYPE (rval)))
1520 > TREE_READONLY (TREE_TYPE (xtype))))
1521 warning ("user-defined conversion casting away `const'");
1522 return convert (xtype, rval);
1523 }
1524
1525 /* Convert an aggregate EXPR to type XTYPE. If a conversion
1526 exists, return the attempted conversion. This may
1527 return ERROR_MARK_NODE if the conversion is not
1528 allowed (references private members, etc).
1529 If no conversion exists, NULL_TREE is returned.
1530
1531 If (FOR_SURE & 1) is non-zero, then we allow this type conversion
1532 to take place immediately. Otherwise, we build a SAVE_EXPR
1533 which can be evaluated if the results are ever needed. */
1534
1535 tree
1536 build_type_conversion (code, xtype, expr, for_sure)
1537 enum tree_code code;
1538 tree xtype, expr;
1539 int for_sure;
1540 {
1541 /* C++: check to see if we can convert this aggregate type
1542 into the required type. */
1543 tree basetype;
1544 tree conv;
1545 tree winner = NULL_TREE;
1546
1547 if (expr == error_mark_node)
1548 return error_mark_node;
1549
1550 basetype = TREE_TYPE (expr);
1551 if (TREE_CODE (basetype) == REFERENCE_TYPE)
1552 basetype = TREE_TYPE (basetype);
1553
1554 basetype = TYPE_MAIN_VARIANT (basetype);
1555 if (! TYPE_LANG_SPECIFIC (basetype) || ! TYPE_HAS_CONVERSION (basetype))
1556 return NULL_TREE;
1557
1558 /* Do we have an exact match? */
1559 {
1560 tree typename = build_typename_overload (xtype);
1561 if (lookup_fnfields (TYPE_BINFO (basetype), typename, 0))
1562 return build_type_conversion_1 (xtype, basetype, expr, typename,
1563 for_sure);
1564 }
1565
1566 /* Nope; try looking for others. */
1567 for (conv = lookup_conversions (basetype); conv; conv = TREE_CHAIN (conv))
1568 {
1569 if (winner && TREE_PURPOSE (winner) == TREE_PURPOSE (conv))
1570 continue;
1571
1572 if (can_convert (xtype, TREE_VALUE (conv)))
1573 {
1574 if (winner)
1575 {
1576 if (for_sure)
1577 {
1578 cp_error ("ambiguous conversion from `%T' to `%T'", basetype,
1579 xtype);
1580 cp_error (" candidate conversions include `%T' and `%T'",
1581 TREE_VALUE (winner), TREE_VALUE (conv));
1582 }
1583 return NULL_TREE;
1584 }
1585 else
1586 winner = conv;
1587 }
1588 }
1589
1590 if (winner)
1591 return build_type_conversion_1 (xtype, basetype, expr,
1592 TREE_PURPOSE (winner), for_sure);
1593
1594 return NULL_TREE;
1595 }
1596
1597 /* Convert the given EXPR to one of a group of types suitable for use in an
1598 expression. DESIRES is a combination of various WANT_* flags (q.v.)
1599 which indicates which types are suitable. If COMPLAIN is 1, complain
1600 about ambiguity; otherwise, the caller will deal with it. */
1601
1602 tree
1603 build_expr_type_conversion (desires, expr, complain)
1604 int desires;
1605 tree expr;
1606 int complain;
1607 {
1608 tree basetype = TREE_TYPE (expr);
1609 tree conv;
1610 tree winner = NULL_TREE;
1611
1612 if (TREE_CODE (basetype) == OFFSET_TYPE)
1613 {
1614 expr = resolve_offset_ref (expr);
1615 basetype = TREE_TYPE (expr);
1616 }
1617
1618 if (! IS_AGGR_TYPE (basetype))
1619 switch (TREE_CODE (basetype))
1620 {
1621 case INTEGER_TYPE:
1622 if ((desires & WANT_NULL) && TREE_CODE (expr) == INTEGER_CST
1623 && integer_zerop (expr))
1624 return expr;
1625 /* else fall through... */
1626
1627 case BOOLEAN_TYPE:
1628 return (desires & WANT_INT) ? expr : NULL_TREE;
1629 case ENUMERAL_TYPE:
1630 return (desires & WANT_ENUM) ? expr : NULL_TREE;
1631 case REAL_TYPE:
1632 return (desires & WANT_FLOAT) ? expr : NULL_TREE;
1633 case POINTER_TYPE:
1634 return (desires & WANT_POINTER) ? expr : NULL_TREE;
1635
1636 case FUNCTION_TYPE:
1637 case ARRAY_TYPE:
1638 return (desires & WANT_POINTER) ? default_conversion (expr)
1639 : NULL_TREE;
1640 default:
1641 return NULL_TREE;
1642 }
1643
1644 if (! TYPE_HAS_CONVERSION (basetype))
1645 return NULL_TREE;
1646
1647 for (conv = lookup_conversions (basetype); conv; conv = TREE_CHAIN (conv))
1648 {
1649 int win = 0;
1650
1651 if (winner && TREE_PURPOSE (winner) == TREE_PURPOSE (conv))
1652 continue;
1653
1654 switch (TREE_CODE (TREE_VALUE (conv)))
1655 {
1656 case BOOLEAN_TYPE:
1657 case INTEGER_TYPE:
1658 win = (desires & WANT_INT); break;
1659 case ENUMERAL_TYPE:
1660 win = (desires & WANT_ENUM); break;
1661 case REAL_TYPE:
1662 win = (desires & WANT_FLOAT); break;
1663 case POINTER_TYPE:
1664 win = (desires & WANT_POINTER); break;
1665 }
1666
1667 if (win)
1668 {
1669 if (winner)
1670 {
1671 if (complain)
1672 {
1673 cp_error ("ambiguous default type conversion from `%T'",
1674 basetype);
1675 cp_error (" candidate conversions include `%T' and `%T'",
1676 TREE_VALUE (winner), TREE_VALUE (conv));
1677 }
1678 return error_mark_node;
1679 }
1680 else
1681 winner = conv;
1682 }
1683 }
1684
1685 if (winner)
1686 return build_type_conversion_1 (TREE_VALUE (winner), basetype, expr,
1687 TREE_PURPOSE (winner), 1);
1688
1689 return NULL_TREE;
1690 }
1691
1692 /* Must convert two aggregate types to non-aggregate type.
1693 Attempts to find a non-ambiguous, "best" type conversion.
1694
1695 Return 1 on success, 0 on failure.
1696
1697 @@ What are the real semantics of this supposed to be??? */
1698 int
1699 build_default_binary_type_conversion (code, arg1, arg2)
1700 enum tree_code code;
1701 tree *arg1, *arg2;
1702 {
1703 switch (code)
1704 {
1705 case MULT_EXPR:
1706 case TRUNC_DIV_EXPR:
1707 case CEIL_DIV_EXPR:
1708 case FLOOR_DIV_EXPR:
1709 case ROUND_DIV_EXPR:
1710 case EXACT_DIV_EXPR:
1711 *arg1 = build_expr_type_conversion (WANT_ARITH | WANT_ENUM, *arg1, 0);
1712 *arg2 = build_expr_type_conversion (WANT_ARITH | WANT_ENUM, *arg2, 0);
1713 break;
1714
1715 case TRUNC_MOD_EXPR:
1716 case FLOOR_MOD_EXPR:
1717 case LSHIFT_EXPR:
1718 case RSHIFT_EXPR:
1719 case BIT_AND_EXPR:
1720 case BIT_XOR_EXPR:
1721 case BIT_IOR_EXPR:
1722 *arg1 = build_expr_type_conversion (WANT_INT | WANT_ENUM, *arg1, 0);
1723 *arg2 = build_expr_type_conversion (WANT_INT | WANT_ENUM, *arg2, 0);
1724 break;
1725
1726 case PLUS_EXPR:
1727 {
1728 tree a1, a2, p1, p2;
1729 int wins;
1730
1731 a1 = build_expr_type_conversion (WANT_ARITH | WANT_ENUM, *arg1, 0);
1732 a2 = build_expr_type_conversion (WANT_ARITH | WANT_ENUM, *arg2, 0);
1733 p1 = build_expr_type_conversion (WANT_POINTER, *arg1, 0);
1734 p2 = build_expr_type_conversion (WANT_POINTER, *arg2, 0);
1735
1736 wins = (a1 && a2) + (a1 && p2) + (p1 && a2);
1737
1738 if (wins > 1)
1739 error ("ambiguous default type conversion for `operator +'");
1740
1741 if (a1 && a2)
1742 *arg1 = a1, *arg2 = a2;
1743 else if (a1 && p2)
1744 *arg1 = a1, *arg2 = p2;
1745 else
1746 *arg1 = p1, *arg2 = a2;
1747 break;
1748 }
1749
1750 case MINUS_EXPR:
1751 {
1752 tree a1, a2, p1, p2;
1753 int wins;
1754
1755 a1 = build_expr_type_conversion (WANT_ARITH | WANT_ENUM, *arg1, 0);
1756 a2 = build_expr_type_conversion (WANT_ARITH | WANT_ENUM, *arg2, 0);
1757 p1 = build_expr_type_conversion (WANT_POINTER, *arg1, 0);
1758 p2 = build_expr_type_conversion (WANT_POINTER, *arg2, 0);
1759
1760 wins = (a1 && a2) + (p1 && p2) + (p1 && a2);
1761
1762 if (wins > 1)
1763 error ("ambiguous default type conversion for `operator -'");
1764
1765 if (a1 && a2)
1766 *arg1 = a1, *arg2 = a2;
1767 else if (p1 && p2)
1768 *arg1 = p1, *arg2 = p2;
1769 else
1770 *arg1 = p1, *arg2 = a2;
1771 break;
1772 }
1773
1774 case GT_EXPR:
1775 case LT_EXPR:
1776 case GE_EXPR:
1777 case LE_EXPR:
1778 case EQ_EXPR:
1779 case NE_EXPR:
1780 {
1781 tree a1, a2, p1, p2;
1782 int wins;
1783
1784 a1 = build_expr_type_conversion (WANT_ARITH | WANT_ENUM, *arg1, 0);
1785 a2 = build_expr_type_conversion (WANT_ARITH | WANT_ENUM, *arg2, 0);
1786 p1 = build_expr_type_conversion (WANT_POINTER | WANT_NULL, *arg1, 0);
1787 p2 = build_expr_type_conversion (WANT_POINTER | WANT_NULL, *arg2, 0);
1788
1789 wins = (a1 && a2) + (p1 && p2);
1790
1791 if (wins > 1)
1792 cp_error ("ambiguous default type conversion for `%O'", code);
1793
1794 if (a1 && a2)
1795 *arg1 = a1, *arg2 = a2;
1796 else
1797 *arg1 = p1, *arg2 = p2;
1798 break;
1799 }
1800
1801 case TRUTH_ANDIF_EXPR:
1802 case TRUTH_ORIF_EXPR:
1803 *arg1 = convert (boolean_type_node, *arg1);
1804 *arg2 = convert (boolean_type_node, *arg2);
1805 break;
1806
1807 default:
1808 *arg1 = NULL_TREE;
1809 *arg2 = NULL_TREE;
1810 }
1811
1812 if (*arg1 == error_mark_node || *arg2 == error_mark_node)
1813 cp_error ("ambiguous default type conversion for `%O'", code);
1814
1815 if (*arg1 && *arg2)
1816 return 1;
1817
1818 return 0;
1819 }
1820
1821 /* Implements integral promotion (4.1) and float->double promotion. */
1822 tree
1823 type_promotes_to (type)
1824 tree type;
1825 {
1826 int constp = TYPE_READONLY (type);
1827 int volatilep = TYPE_VOLATILE (type);
1828 type = TYPE_MAIN_VARIANT (type);
1829
1830 /* bool always promotes to int (not unsigned), even if it's the same
1831 size. */
1832 if (type == boolean_type_node)
1833 type = integer_type_node;
1834
1835 /* Normally convert enums to int, but convert wide enums to something
1836 wider. */
1837 else if (TREE_CODE (type) == ENUMERAL_TYPE
1838 || type == wchar_type_node)
1839 {
1840 int precision = MAX (TYPE_PRECISION (type),
1841 TYPE_PRECISION (integer_type_node));
1842 tree totype = type_for_size (precision, 0);
1843 if (TREE_UNSIGNED (type)
1844 && ! int_fits_type_p (TYPE_MAX_VALUE (type), totype))
1845 type = type_for_size (precision, 1);
1846 else
1847 type = totype;
1848 }
1849 else if (C_PROMOTING_INTEGER_TYPE_P (type))
1850 {
1851 /* Traditionally, unsignedness is preserved in default promotions.
1852 Otherwise, retain unsignedness if really not getting bigger. */
1853 if (TREE_UNSIGNED (type)
1854 && (flag_traditional
1855 || TYPE_PRECISION (type) == TYPE_PRECISION (integer_type_node)))
1856 type = unsigned_type_node;
1857 else
1858 type = integer_type_node;
1859 }
1860 else if (type == float_type_node)
1861 type = double_type_node;
1862
1863 return cp_build_type_variant (type, constp, volatilep);
1864 }