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