]> gcc.gnu.org Git - gcc.git/blame - gcc/cp/cvt.c
(instantiate_type): Change error message text.
[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);
6060a796
MS
312 while (TREE_CODE (targ) == NOP_EXPR
313 && (TYPE_MAIN_VARIANT (argtype)
314 == TYPE_MAIN_VARIANT (TREE_TYPE (TREE_OPERAND (targ, 0)))))
315 targ = TREE_OPERAND (targ, 0);
8d08fdba
MS
316
317 switch (TREE_CODE (targ))
318 {
319 case INDIRECT_REF:
320 /* This is a call to a constructor which did not know what it was
321 initializing until now: it needs to initialize a temporary. */
322 if (TREE_HAS_CONSTRUCTOR (targ))
323 {
324 tree temp = build_cplus_new (argtype, TREE_OPERAND (targ, 0), 1);
325 TREE_HAS_CONSTRUCTOR (targ) = 0;
326 return build_up_reference (type, temp, flags, 1);
327 }
328 /* Let &* cancel out to simplify resulting code.
329 Also, throw away intervening NOP_EXPRs. */
330 arg = TREE_OPERAND (targ, 0);
331 if (TREE_CODE (arg) == NOP_EXPR || TREE_CODE (arg) == NON_LVALUE_EXPR
332 || (TREE_CODE (arg) == CONVERT_EXPR && TREE_REFERENCE_EXPR (arg)))
333 arg = TREE_OPERAND (arg, 0);
334
335 /* in doing a &*, we have to get rid of the const'ness on the pointer
336 value. Haven't thought about volatile here. Pointers come to mind
337 here. */
338 if (TREE_READONLY (arg))
339 {
340 arg = copy_node (arg);
341 TREE_READONLY (arg) = 0;
342 }
343
344 rval = build1 (CONVERT_EXPR, type, arg);
345 TREE_REFERENCE_EXPR (rval) = 1;
346
347 /* propagate the const flag on something like:
348
349 class Base {
350 public:
351 int foo;
352 };
353
354 class Derived : public Base {
355 public:
356 int bar;
357 };
358
359 void func(Base&);
360
361 void func2(const Derived& d) {
362 func(d);
363 }
364
365 on the d parameter. The below could have been avoided, if the flags
366 were down in the tree, not sure why they are not. (mrs) */
367 /* The below code may have to be propagated to other parts of this
368 switch. */
369 if (TREE_READONLY (targ) && !TREE_READONLY (arg)
370 && (TREE_CODE (arg) == PARM_DECL || TREE_CODE (arg) == VAR_DECL)
371 && TREE_CODE (TREE_TYPE (arg)) == REFERENCE_TYPE
372 && (TYPE_READONLY (target_type) && checkconst))
373 {
374 arg = copy_node (arg);
375 TREE_READONLY (arg) = TREE_READONLY (targ);
376 }
377 literal_flag = TREE_CONSTANT (arg);
378
a3203465 379 goto done;
8d08fdba
MS
380
381 /* Get this out of a register if we happened to be in one by accident.
382 Also, build up references to non-lvalues it we must. */
383 /* For &x[y], return (&) x+y */
384 case ARRAY_REF:
385 if (mark_addressable (TREE_OPERAND (targ, 0)) == 0)
386 return error_mark_node;
387 rval = build_binary_op (PLUS_EXPR, TREE_OPERAND (targ, 0),
388 TREE_OPERAND (targ, 1), 1);
389 TREE_TYPE (rval) = type;
390 if (TREE_CONSTANT (TREE_OPERAND (targ, 1))
391 && staticp (TREE_OPERAND (targ, 0)))
392 TREE_CONSTANT (rval) = 1;
393 goto done;
394
395 case SCOPE_REF:
396 /* Could be a reference to a static member. */
397 {
398 tree field = TREE_OPERAND (targ, 1);
399 if (TREE_STATIC (field))
400 {
401 rval = build1 (ADDR_EXPR, type, field);
402 literal_flag = 1;
403 goto done;
404 }
405 }
406
407 /* We should have farmed out member pointers above. */
408 my_friendly_abort (188);
409
410 case COMPONENT_REF:
411 rval = build_component_addr (targ, build_pointer_type (argtype),
412 "attempt to make a reference to bit-field structure member `%s'");
413 TREE_TYPE (rval) = type;
414 literal_flag = staticp (TREE_OPERAND (targ, 0));
415
a3203465 416 goto done;
8d08fdba
MS
417
418 /* Anything not already handled and not a true memory reference
419 needs to have a reference built up. Do so silently for
420 things like integers and return values from function,
421 but complain if we need a reference to something declared
422 as `register'. */
423
424 case RESULT_DECL:
425 if (staticp (targ))
426 literal_flag = 1;
427 TREE_ADDRESSABLE (targ) = 1;
428 put_var_into_stack (targ);
429 break;
430
431 case PARM_DECL:
a292b002 432#if 0
8d08fdba
MS
433 if (targ == current_class_decl)
434 {
435 error ("address of `this' not available");
a292b002 436/* #if 0 */
8d08fdba
MS
437 /* This code makes the following core dump the compiler on a sun4,
438 if the code below is used.
439
440 class e_decl;
441 class a_decl;
442 typedef a_decl* a_ref;
443
444 class a_s {
445 public:
446 a_s();
447 void* append(a_ref& item);
448 };
449 class a_decl {
450 public:
451 a_decl (e_decl *parent);
452 a_s generic_s;
453 a_s decls;
454 e_decl* parent;
455 };
456
457 class e_decl {
458 public:
459 e_decl();
460 a_s implementations;
461 };
462
463 void foobar(void *);
464
465 a_decl::a_decl(e_decl *parent) {
466 parent->implementations.append(this);
467 }
468 */
469
470 TREE_ADDRESSABLE (targ) = 1; /* so compiler doesn't die later */
471 put_var_into_stack (targ);
472 break;
a292b002 473/* #else */
8d08fdba 474 return error_mark_node;
a292b002 475/* #endif */
8d08fdba 476 }
a292b002 477#endif
8d08fdba
MS
478 /* Fall through. */
479 case VAR_DECL:
480 case CONST_DECL:
a292b002
MS
481 if (DECL_REGISTER (targ) && !TREE_ADDRESSABLE (targ)
482 && !DECL_ARTIFICIAL (targ))
483 cp_warning ("address needed to build reference for `%D', which is declared `register'",
484 targ);
8d08fdba
MS
485 else if (staticp (targ))
486 literal_flag = 1;
487
488 TREE_ADDRESSABLE (targ) = 1;
489 put_var_into_stack (targ);
490 break;
491
492 case COMPOUND_EXPR:
493 {
494 tree real_reference = build_up_reference (type, TREE_OPERAND (targ, 1),
495 LOOKUP_PROTECT, checkconst);
496 rval = build (COMPOUND_EXPR, type, TREE_OPERAND (targ, 0), real_reference);
497 TREE_CONSTANT (rval) = staticp (TREE_OPERAND (targ, 1));
498 return rval;
499 }
500
f376e137
MS
501 case PREINCREMENT_EXPR:
502 case PREDECREMENT_EXPR:
8d08fdba
MS
503 case MODIFY_EXPR:
504 case INIT_EXPR:
505 {
506 tree real_reference = build_up_reference (type, TREE_OPERAND (targ, 0),
507 LOOKUP_PROTECT, checkconst);
508 rval = build (COMPOUND_EXPR, type, arg, real_reference);
509 TREE_CONSTANT (rval) = staticp (TREE_OPERAND (targ, 0));
510 return rval;
511 }
512
513 case COND_EXPR:
514 return build (COND_EXPR, type,
515 TREE_OPERAND (targ, 0),
516 build_up_reference (type, TREE_OPERAND (targ, 1),
517 LOOKUP_PROTECT, checkconst),
518 build_up_reference (type, TREE_OPERAND (targ, 2),
519 LOOKUP_PROTECT, checkconst));
520
e1cd6e56
MS
521 /* Undo the folding... */
522 case MIN_EXPR:
523 case MAX_EXPR:
524 return build (COND_EXPR, type,
525 build (TREE_CODE (targ) == MIN_EXPR ? LT_EXPR : GT_EXPR,
526 boolean_type_node, TREE_OPERAND (targ, 0),
527 TREE_OPERAND (targ, 1)),
528 build_up_reference (type, TREE_OPERAND (targ, 0),
529 LOOKUP_PROTECT, checkconst),
530 build_up_reference (type, TREE_OPERAND (targ, 1),
531 LOOKUP_PROTECT, checkconst));
532
8d08fdba
MS
533 case WITH_CLEANUP_EXPR:
534 return build (WITH_CLEANUP_EXPR, type,
535 build_up_reference (type, TREE_OPERAND (targ, 0),
536 LOOKUP_PROTECT, checkconst),
537 0, TREE_OPERAND (targ, 2));
538
539 case BIND_EXPR:
540 arg = TREE_OPERAND (targ, 1);
541 if (arg == NULL_TREE)
542 {
543 compiler_error ("({ ... }) expression not expanded when needed for reference");
544 return error_mark_node;
545 }
546 rval = build1 (ADDR_EXPR, type, arg);
547 TREE_REFERENCE_EXPR (rval) = 1;
548 return rval;
549
550 default:
551 break;
552 }
553
554 if (TREE_ADDRESSABLE (targ) == 0)
555 {
556 tree temp;
557
558 if (TREE_CODE (targ) == CALL_EXPR && IS_AGGR_TYPE (argtype))
559 {
560 temp = build_cplus_new (argtype, targ, 1);
a3203465
MS
561 if (TREE_CODE (temp) == WITH_CLEANUP_EXPR)
562 rval = build (WITH_CLEANUP_EXPR, type,
563 build1 (ADDR_EXPR, type, TREE_OPERAND (temp, 0)),
564 0, TREE_OPERAND (temp, 2));
565 else
566 rval = build1 (ADDR_EXPR, type, temp);
8d08fdba
MS
567 goto done;
568 }
569 else
570 {
571 temp = get_temp_name (argtype, 0);
572 if (global_bindings_p ())
573 {
574 /* Give this new temp some rtl and initialize it. */
575 DECL_INITIAL (temp) = targ;
576 TREE_STATIC (temp) = 1;
6060a796 577 finish_decl (temp, targ, NULL_TREE, 0, LOOKUP_ONLYCONVERTING);
8d08fdba
MS
578 /* Do this after declaring it static. */
579 rval = build_unary_op (ADDR_EXPR, temp, 0);
580 TREE_TYPE (rval) = type;
581 literal_flag = TREE_CONSTANT (rval);
582 goto done;
583 }
584 else
585 {
586 rval = build_unary_op (ADDR_EXPR, temp, 0);
587 if (binfo && !BINFO_OFFSET_ZEROP (binfo))
588 rval = convert_pointer_to (target_type, rval);
589 else
590 TREE_TYPE (rval) = type;
591
592 temp = build (MODIFY_EXPR, argtype, temp, arg);
593 TREE_SIDE_EFFECTS (temp) = 1;
594 return build (COMPOUND_EXPR, type, temp, rval);
595 }
596 }
597 }
598 else
599 rval = build1 (ADDR_EXPR, type, arg);
600
8d08fdba 601 done:
21474714
MS
602 if (TYPE_USES_COMPLEX_INHERITANCE (argtype)
603 || TYPE_USES_COMPLEX_INHERITANCE (target_type))
8d08fdba 604 {
39211cd5 605 TREE_TYPE (rval) = build_pointer_type (argtype);
51c184be
MS
606 if (flags & LOOKUP_PROTECT)
607 rval = convert_pointer_to (target_type, rval);
608 else
609 rval
610 = convert_to_pointer_force (build_pointer_type (target_type), rval);
8d08fdba
MS
611 TREE_TYPE (rval) = type;
612 }
613 TREE_CONSTANT (rval) = literal_flag;
614 return rval;
615}
616
617/* For C++: Only need to do one-level references, but cannot
618 get tripped up on signed/unsigned differences.
619
a4443a08
MS
620 DECL is either NULL_TREE or the _DECL node for a reference that is being
621 initialized. It can be error_mark_node if we don't know the _DECL but
622 we know it's an initialization. */
8d08fdba 623
8d08fdba 624tree
a4443a08 625convert_to_reference (reftype, expr, convtype, flags, decl)
8d08fdba 626 tree reftype, expr;
a4443a08
MS
627 int convtype, flags;
628 tree decl;
8d08fdba
MS
629{
630 register tree type = TYPE_MAIN_VARIANT (TREE_TYPE (reftype));
631 register tree intype = TREE_TYPE (expr);
632 register enum tree_code form = TREE_CODE (intype);
633 tree rval = NULL_TREE;
634
8d08fdba
MS
635 if (form == REFERENCE_TYPE)
636 intype = TREE_TYPE (intype);
637 intype = TYPE_MAIN_VARIANT (intype);
638
a4443a08
MS
639 if (((convtype & CONV_STATIC) && comptypes (type, intype, -1))
640 || ((convtype & CONV_IMPLICIT) && comptypes (type, intype, 0)))
8d08fdba 641 {
8d08fdba
MS
642 if (flags & LOOKUP_COMPLAIN)
643 {
39211cd5
MS
644 tree ttl = TREE_TYPE (reftype);
645 tree ttr;
646
8926095f 647 if (form == REFERENCE_TYPE)
39211cd5
MS
648 ttr = TREE_TYPE (TREE_TYPE (expr));
649 else
a3203465
MS
650 {
651 int r = TREE_READONLY (expr);
652 int v = TREE_THIS_VOLATILE (expr);
f376e137 653 ttr = cp_build_type_variant (TREE_TYPE (expr), r, v);
a3203465 654 }
8d08fdba 655
a4443a08
MS
656 if (! lvalue_p (expr) &&
657 (decl == NULL_TREE || ! TYPE_READONLY (ttl)))
39211cd5 658 {
a4443a08
MS
659 if (decl)
660 /* Ensure semantics of [dcl.init.ref] */
661 cp_pedwarn ("initialization of non-const `%T' from rvalue `%T'",
662 reftype, intype);
663 else
664 cp_pedwarn ("conversion to `%T' from rvalue `%T'",
665 reftype, intype);
39211cd5 666 }
a4443a08 667 else if (! (convtype & CONV_CONST))
8d08fdba 668 {
a4443a08
MS
669 if (! TYPE_READONLY (ttl) && TYPE_READONLY (ttr))
670 cp_pedwarn ("conversion from `%T' to `%T' discards const",
a3203465 671 ttr, reftype);
a4443a08
MS
672 else if (! TYPE_VOLATILE (ttl) && TYPE_VOLATILE (ttr))
673 cp_pedwarn ("conversion from `%T' to `%T' discards volatile",
a3203465 674 ttr, reftype);
8d08fdba 675 }
8926095f
MS
676 }
677
a3203465 678 if (form == REFERENCE_TYPE)
8926095f 679 {
db5ae43f 680 tree type = TREE_TYPE (expr);
e1cd6e56
MS
681 TREE_TYPE (expr) = build_pointer_type (TREE_TYPE (type));
682 rval = cp_convert (build_pointer_type (TREE_TYPE (reftype)), expr,
a4443a08 683 convtype, flags);
e1cd6e56 684 TREE_TYPE (expr) = type;
a3203465 685 TREE_TYPE (rval) = reftype;
8926095f 686 return rval;
8d08fdba 687 }
8926095f 688
a4443a08
MS
689 return build_up_reference (reftype, expr, flags,
690 ! (convtype & CONV_CONST));
8d08fdba
MS
691 }
692
21474714
MS
693 if ((convtype & CONV_IMPLICIT)
694 && IS_AGGR_TYPE (intype)
695 && ! (flags & LOOKUP_NO_CONVERSION)
696 && (rval = build_type_conversion (CONVERT_EXPR, reftype, expr, 1)))
697 {
698 if (rval == error_mark_node)
699 cp_error ("conversion from `%T' to `%T' is ambiguous",
700 intype, reftype);
701 return rval;
702 }
703 else if ((convtype & CONV_REINTERPRET) && lvalue_p (expr))
8926095f
MS
704 {
705 /* When casting an lvalue to a reference type, just convert into
706 a pointer to the new type and deference it. This is allowed
a28e3c7f 707 by San Diego WP section 5.2.9 paragraph 12, though perhaps it
8926095f 708 should be done directly (jason). (int &)ri ---> *(int*)&ri */
a28e3c7f
MS
709
710 /* B* bp; A& ar = (A&)bp; is legal, but it's probably not what they
711 meant. */
712 if (form == POINTER_TYPE
a4443a08 713 && (comptypes (TREE_TYPE (intype), type, -1)))
a28e3c7f
MS
714 cp_warning ("casting `%T' to `%T' does not dereference pointer",
715 intype, reftype);
716
8926095f
MS
717 rval = build_unary_op (ADDR_EXPR, expr, 0);
718 if (rval != error_mark_node)
6060a796 719 rval = convert_force (build_pointer_type (TREE_TYPE (reftype)), rval, 0);
8926095f 720 if (rval != error_mark_node)
7177d104 721 rval = build1 (NOP_EXPR, reftype, rval);
8926095f 722 }
a4443a08 723 else if (decl)
8d08fdba
MS
724 {
725 tree rval_as_conversion = NULL_TREE;
726 tree rval_as_ctor = NULL_TREE;
727
728 if (IS_AGGR_TYPE (intype)
729 && (rval = build_type_conversion (CONVERT_EXPR, type, expr, 1)))
730 {
731 if (rval == error_mark_node)
732 return rval;
733
734 rval_as_conversion = build_up_reference (reftype, rval, flags, 1);
735 }
736
737 /* Definitely need to go through a constructor here. */
738 if (TYPE_HAS_CONSTRUCTOR (type)
a28e3c7f 739 && ! CLASSTYPE_ABSTRACT_VIRTUALS (type)
8d08fdba
MS
740 && (rval = build_method_call
741 (NULL_TREE, constructor_name_full (type),
742 build_tree_list (NULL_TREE, expr), TYPE_BINFO (type),
db5ae43f
MS
743 LOOKUP_NO_CONVERSION|LOOKUP_SPECULATIVELY
744 | LOOKUP_ONLYCONVERTING)))
8d08fdba
MS
745 {
746 tree init;
747
748 if (global_bindings_p ())
749 {
750 extern tree static_aggregates;
a4443a08
MS
751 tree t = get_temp_name (type, global_bindings_p ());
752 init = build_method_call (t, constructor_name_full (type),
8d08fdba 753 build_tree_list (NULL_TREE, expr),
a4443a08 754 TYPE_BINFO (type),
db5ae43f
MS
755 LOOKUP_NORMAL|LOOKUP_NO_CONVERSION
756 | LOOKUP_ONLYCONVERTING);
8d08fdba
MS
757
758 if (init == error_mark_node)
759 return error_mark_node;
760
a4443a08
MS
761 make_decl_rtl (t, NULL_PTR, 1);
762 static_aggregates = perm_tree_cons (expr, t, static_aggregates);
763 rval = build_unary_op (ADDR_EXPR, t, 0);
8d08fdba
MS
764 }
765 else
766 {
767 init = build_method_call (NULL_TREE, constructor_name_full (type),
768 build_tree_list (NULL_TREE, expr),
a4443a08 769 TYPE_BINFO (type),
db5ae43f
MS
770 LOOKUP_NORMAL|LOOKUP_NO_CONVERSION
771 |LOOKUP_ONLYCONVERTING);
8d08fdba
MS
772
773 if (init == error_mark_node)
774 return error_mark_node;
775
776 rval = build_cplus_new (type, init, 1);
777 rval = build_up_reference (reftype, rval, flags, 1);
778 }
779 rval_as_ctor = rval;
780 }
781
782 if (rval_as_ctor && rval_as_conversion)
783 {
784 cp_error ("ambiguous conversion from `%T' to `%T'; both user-defined conversion and constructor apply",
785 intype, reftype);
786 return error_mark_node;
787 }
788 else if (rval_as_ctor)
789 rval = rval_as_ctor;
790 else if (rval_as_conversion)
791 rval = rval_as_conversion;
792 else if (! IS_AGGR_TYPE (type) && ! IS_AGGR_TYPE (intype))
793 {
794 rval = convert (type, expr);
795 if (rval == error_mark_node)
796 return error_mark_node;
797
798 rval = build_up_reference (reftype, rval, flags, 1);
799 }
800
801 if (rval && ! TYPE_READONLY (TREE_TYPE (reftype)))
a4443a08
MS
802 cp_pedwarn ("initializing non-const `%T' with `%T' will use a temporary",
803 reftype, intype);
8d08fdba
MS
804 }
805
806 if (rval)
807 {
808 /* If we found a way to convert earlier, then use it. */
809 return rval;
810 }
811
812 my_friendly_assert (form != OFFSET_TYPE, 189);
813
8d08fdba
MS
814 if (flags & LOOKUP_SPECULATIVELY)
815 return NULL_TREE;
816
8926095f
MS
817 else if (flags & LOOKUP_COMPLAIN)
818 cp_error ("cannot convert type `%T' to type `%T'", intype, reftype);
819
8d08fdba
MS
820 return error_mark_node;
821}
822
823/* We are using a reference VAL for its value. Bash that reference all the
824 way down to its lowest form. */
825tree
826convert_from_reference (val)
827 tree val;
828{
829 tree type = TREE_TYPE (val);
830
831 if (TREE_CODE (type) == OFFSET_TYPE)
832 type = TREE_TYPE (type);
833 if (TREE_CODE (type) == REFERENCE_TYPE)
834 {
835 tree target_type = TREE_TYPE (type);
836 tree nval;
837
838 /* This can happen if we cast to a reference type. */
839 if (TREE_CODE (val) == ADDR_EXPR)
840 {
841 nval = build1 (NOP_EXPR, build_pointer_type (target_type), val);
842 nval = build_indirect_ref (nval, NULL_PTR);
843 /* The below was missing, are other important flags missing too? */
844 TREE_SIDE_EFFECTS (nval) = TREE_SIDE_EFFECTS (val);
845 return nval;
846 }
847
39211cd5 848 nval = build1 (INDIRECT_REF, target_type, val);
8d08fdba
MS
849
850 TREE_THIS_VOLATILE (nval) = TYPE_VOLATILE (target_type);
851 TREE_SIDE_EFFECTS (nval) = TYPE_VOLATILE (target_type);
852 TREE_READONLY (nval) = TYPE_READONLY (target_type);
853 /* The below was missing, are other important flags missing too? */
854 TREE_SIDE_EFFECTS (nval) |= TREE_SIDE_EFFECTS (val);
855 return nval;
856 }
857 return val;
858}
859\f
860/* See if there is a constructor of type TYPE which will convert
861 EXPR. The reference manual seems to suggest (8.5.6) that we need
862 not worry about finding constructors for base classes, then converting
863 to the derived class.
864
865 MSGP is a pointer to a message that would be an appropriate error
866 string. If MSGP is NULL, then we are not interested in reporting
867 errors. */
868tree
869convert_to_aggr (type, expr, msgp, protect)
870 tree type, expr;
871 char **msgp;
872 int protect;
873{
874 tree basetype = type;
875 tree name = TYPE_IDENTIFIER (basetype);
876 tree function, fndecl, fntype, parmtypes, parmlist, result;
877 tree method_name;
878 enum access_type access;
879 int can_be_private, can_be_protected;
880
881 if (! TYPE_HAS_CONSTRUCTOR (basetype))
882 {
883 if (msgp)
884 *msgp = "type `%s' does not have a constructor";
885 return error_mark_node;
886 }
887
888 access = access_public;
889 can_be_private = 0;
890 can_be_protected = IDENTIFIER_CLASS_VALUE (name) || name == current_class_name;
891
892 parmlist = build_tree_list (NULL_TREE, expr);
893 parmtypes = tree_cons (NULL_TREE, TREE_TYPE (expr), void_list_node);
894
895 if (TYPE_USES_VIRTUAL_BASECLASSES (basetype))
896 {
897 parmtypes = tree_cons (NULL_TREE, integer_type_node, parmtypes);
898 parmlist = tree_cons (NULL_TREE, integer_one_node, parmlist);
899 }
900
901 /* The type of the first argument will be filled in inside the loop. */
902 parmlist = tree_cons (NULL_TREE, integer_zero_node, parmlist);
903 parmtypes = tree_cons (NULL_TREE, TYPE_POINTER_TO (basetype), parmtypes);
904
905 method_name = build_decl_overload (name, parmtypes, 1);
906
907 /* constructors are up front. */
908 fndecl = TREE_VEC_ELT (CLASSTYPE_METHOD_VEC (basetype), 0);
909 if (TYPE_HAS_DESTRUCTOR (basetype))
910 fndecl = DECL_CHAIN (fndecl);
911
912 while (fndecl)
913 {
914 if (DECL_ASSEMBLER_NAME (fndecl) == method_name)
915 {
916 function = fndecl;
917 if (protect)
918 {
919 if (TREE_PRIVATE (fndecl))
920 {
921 can_be_private =
922 (basetype == current_class_type
923 || is_friend (basetype, current_function_decl)
924 || purpose_member (basetype, DECL_ACCESS (fndecl)));
925 if (! can_be_private)
926 goto found;
927 }
928 else if (TREE_PROTECTED (fndecl))
929 {
930 if (! can_be_protected)
931 goto found;
932 }
933 }
934 goto found_and_ok;
935 }
936 fndecl = DECL_CHAIN (fndecl);
937 }
938
939 /* No exact conversion was found. See if an approximate
940 one will do. */
941 fndecl = TREE_VEC_ELT (CLASSTYPE_METHOD_VEC (basetype), 0);
942 if (TYPE_HAS_DESTRUCTOR (basetype))
943 fndecl = DECL_CHAIN (fndecl);
944
945 {
946 int saw_private = 0;
947 int saw_protected = 0;
948 struct candidate *candidates =
949 (struct candidate *) alloca ((decl_list_length (fndecl)+1) * sizeof (struct candidate));
950 struct candidate *cp = candidates;
951
952 while (fndecl)
953 {
954 function = fndecl;
955 cp->h_len = 2;
2986ae00
MS
956 cp->harshness = (struct harshness_code *)
957 alloca (3 * sizeof (struct harshness_code));
8d08fdba
MS
958
959 compute_conversion_costs (fndecl, parmlist, cp, 2);
2986ae00 960 if ((cp->h.code & EVIL_CODE) == 0)
8d08fdba
MS
961 {
962 cp->u.field = fndecl;
963 if (protect)
964 {
965 if (TREE_PRIVATE (fndecl))
966 access = access_private;
967 else if (TREE_PROTECTED (fndecl))
968 access = access_protected;
969 else
970 access = access_public;
971 }
972 else
973 access = access_public;
974
975 if (access == access_private
976 ? (basetype == current_class_type
977 || is_friend (basetype, cp->function)
978 || purpose_member (basetype, DECL_ACCESS (fndecl)))
979 : access == access_protected
980 ? (can_be_protected
981 || purpose_member (basetype, DECL_ACCESS (fndecl)))
982 : 1)
983 {
2986ae00 984 if (cp->h.code <= TRIVIAL_CODE)
8d08fdba
MS
985 goto found_and_ok;
986 cp++;
987 }
988 else
989 {
990 if (access == access_private)
991 saw_private = 1;
992 else
993 saw_protected = 1;
994 }
995 }
996 fndecl = DECL_CHAIN (fndecl);
997 }
998 if (cp - candidates)
999 {
1000 /* Rank from worst to best. Then cp will point to best one.
1001 Private fields have their bits flipped. For unsigned
1002 numbers, this should make them look very large.
1003 If the best alternate has a (signed) negative value,
1004 then all we ever saw were private members. */
1005 if (cp - candidates > 1)
1006 qsort (candidates, /* char *base */
1007 cp - candidates, /* int nel */
1008 sizeof (struct candidate), /* int width */
1009 rank_for_overload); /* int (*compar)() */
1010
1011 --cp;
2986ae00 1012 if (cp->h.code & EVIL_CODE)
8d08fdba
MS
1013 {
1014 if (msgp)
1015 *msgp = "ambiguous type conversion possible for `%s'";
1016 return error_mark_node;
1017 }
1018
1019 function = cp->function;
1020 fndecl = cp->u.field;
1021 goto found_and_ok;
1022 }
1023 else if (msgp)
1024 {
1025 if (saw_private)
1026 if (saw_protected)
1027 *msgp = "only private and protected conversions apply";
1028 else
1029 *msgp = "only private conversions apply";
1030 else if (saw_protected)
1031 *msgp = "only protected conversions apply";
8926095f
MS
1032 else
1033 *msgp = "no appropriate conversion to type `%s'";
8d08fdba
MS
1034 }
1035 return error_mark_node;
1036 }
1037 /* NOTREACHED */
1038
8d08fdba
MS
1039 found:
1040 if (access == access_private)
1041 if (! can_be_private)
1042 {
1043 if (msgp)
1044 *msgp = TREE_PRIVATE (fndecl)
1045 ? "conversion to type `%s' is private"
1046 : "conversion to type `%s' is from private base class";
1047 return error_mark_node;
1048 }
1049 if (access == access_protected)
1050 if (! can_be_protected)
1051 {
1052 if (msgp)
1053 *msgp = TREE_PRIVATE (fndecl)
1054 ? "conversion to type `%s' is protected"
1055 : "conversion to type `%s' is from protected base class";
1056 return error_mark_node;
1057 }
1058 function = fndecl;
1059 found_and_ok:
1060
1061 /* It will convert, but we don't do anything about it yet. */
1062 if (msgp == 0)
1063 return NULL_TREE;
1064
1065 fntype = TREE_TYPE (function);
1066 if (DECL_INLINE (function) && TREE_CODE (function) == FUNCTION_DECL)
1067 function = build1 (ADDR_EXPR, build_pointer_type (fntype), function);
1068 else
1069 function = default_conversion (function);
1070
1071 result = build_nt (CALL_EXPR, function,
1072 convert_arguments (NULL_TREE, TYPE_ARG_TYPES (fntype),
1073 parmlist, NULL_TREE, LOOKUP_NORMAL),
1074 NULL_TREE);
1075 TREE_TYPE (result) = TREE_TYPE (fntype);
1076 TREE_SIDE_EFFECTS (result) = 1;
8d08fdba
MS
1077 return result;
1078}
1079
1080/* Call this when we know (for any reason) that expr is not, in fact,
1081 zero. This routine is like convert_pointer_to, but it pays
1082 attention to which specific instance of what type we want to
1083 convert to. This routine should eventually become
1084 convert_to_pointer after all references to convert_to_pointer
1085 are removed. */
1086tree
1087convert_pointer_to_real (binfo, expr)
1088 tree binfo, expr;
1089{
1090 register tree intype = TREE_TYPE (expr);
1091 tree ptr_type;
1092 tree type, rval;
1093
1094 if (TREE_CODE (binfo) == TREE_VEC)
1095 type = BINFO_TYPE (binfo);
1096 else if (IS_AGGR_TYPE (binfo))
1097 {
1098 type = binfo;
1099 }
1100 else
1101 {
1102 type = binfo;
1103 binfo = NULL_TREE;
1104 }
1105
1106 ptr_type = build_pointer_type (type);
1107 if (ptr_type == TYPE_MAIN_VARIANT (intype))
1108 return expr;
1109
1110 if (intype == error_mark_node)
1111 return error_mark_node;
1112
1113 my_friendly_assert (!integer_zerop (expr), 191);
1114
1115 if (TREE_CODE (type) == RECORD_TYPE
1116 && TREE_CODE (TREE_TYPE (intype)) == RECORD_TYPE
1117 && type != TYPE_MAIN_VARIANT (TREE_TYPE (intype)))
1118 {
1119 tree path;
1120 int distance
1121 = get_base_distance (binfo, TYPE_MAIN_VARIANT (TREE_TYPE (intype)),
1122 0, &path);
1123
1124 /* This function shouldn't be called with unqualified arguments
1125 but if it is, give them an error message that they can read. */
1126 if (distance < 0)
1127 {
7177d104
MS
1128 cp_error ("cannot convert a pointer of type `%T' to a pointer of type `%T'",
1129 TREE_TYPE (intype), type);
8d08fdba
MS
1130
1131 if (distance == -2)
1132 cp_error ("because `%T' is an ambiguous base class", type);
1133 return error_mark_node;
1134 }
1135
1136 return build_vbase_path (PLUS_EXPR, ptr_type, expr, path, 1);
1137 }
1138 rval = build1 (NOP_EXPR, ptr_type,
1139 TREE_CODE (expr) == NOP_EXPR ? TREE_OPERAND (expr, 0) : expr);
1140 TREE_CONSTANT (rval) = TREE_CONSTANT (expr);
1141 return rval;
1142}
1143
1144/* Call this when we know (for any reason) that expr is
1145 not, in fact, zero. This routine gets a type out of the first
1146 argument and uses it to search for the type to convert to. If there
1147 is more than one instance of that type in the expr, the conversion is
1148 ambiguous. This routine should eventually go away, and all
1149 callers should use convert_to_pointer_real. */
1150tree
1151convert_pointer_to (binfo, expr)
1152 tree binfo, expr;
1153{
1154 tree type;
1155
1156 if (TREE_CODE (binfo) == TREE_VEC)
1157 type = BINFO_TYPE (binfo);
1158 else if (IS_AGGR_TYPE (binfo))
1159 type = binfo;
1160 else
1161 type = binfo;
1162 return convert_pointer_to_real (type, expr);
1163}
1164
1165/* Same as above, but don't abort if we get an "ambiguous" baseclass.
1166 There's only one virtual baseclass we are looking for, and once
1167 we find one such virtual baseclass, we have found them all. */
1168
1169tree
1170convert_pointer_to_vbase (binfo, expr)
1171 tree binfo;
1172 tree expr;
1173{
1174 tree intype = TREE_TYPE (TREE_TYPE (expr));
1175 tree binfos = TYPE_BINFO_BASETYPES (intype);
1176 int i;
1177
1178 for (i = TREE_VEC_LENGTH (binfos)-1; i >= 0; i--)
1179 {
1180 tree basetype = BINFO_TYPE (TREE_VEC_ELT (binfos, i));
1181 if (BINFO_TYPE (binfo) == basetype)
1182 return convert_pointer_to (binfo, expr);
1183 if (binfo_member (BINFO_TYPE (binfo), CLASSTYPE_VBASECLASSES (basetype)))
1184 return convert_pointer_to_vbase (binfo, convert_pointer_to (basetype, expr));
1185 }
1186 my_friendly_abort (6);
1187 /* NOTREACHED */
1188 return NULL_TREE;
1189}
1190\f
8d08fdba 1191tree
a4443a08 1192cp_convert (type, expr, convtype, flags)
8d08fdba 1193 tree type, expr;
a4443a08 1194 int convtype, flags;
8d08fdba
MS
1195{
1196 register tree e = expr;
1197 register enum tree_code code = TREE_CODE (type);
1198
6060a796
MS
1199 if (TREE_CODE (e) == ERROR_MARK
1200 || TREE_CODE (TREE_TYPE (e)) == ERROR_MARK)
8d08fdba 1201 return error_mark_node;
a4443a08
MS
1202
1203 /* Trivial conversion: cv-qualifiers do not matter on rvalues. */
f0e01782
MS
1204 if (TYPE_MAIN_VARIANT (type) == TYPE_MAIN_VARIANT (TREE_TYPE (e)))
1205 return fold (build1 (NOP_EXPR, type, e));
a4443a08
MS
1206
1207 if (code == VOID_TYPE && (convtype & CONV_STATIC))
1208 return build1 (CONVERT_EXPR, type, e);
1209
8d08fdba
MS
1210#if 0
1211 /* This is incorrect. A truncation can't be stripped this way.
1212 Extensions will be stripped by the use of get_unwidened. */
f0e01782
MS
1213 if (TREE_CODE (e) == NOP_EXPR)
1214 return convert (type, TREE_OPERAND (e, 0));
8d08fdba
MS
1215#endif
1216
1217 /* Just convert to the type of the member. */
1218 if (code == OFFSET_TYPE)
1219 {
1220 type = TREE_TYPE (type);
1221 code = TREE_CODE (type);
1222 }
1223
8d08fdba 1224 if (code == REFERENCE_TYPE)
a4443a08 1225 return fold (convert_to_reference (type, e, convtype, flags, NULL_TREE));
8d08fdba
MS
1226 else if (TREE_CODE (TREE_TYPE (e)) == REFERENCE_TYPE)
1227 e = convert_from_reference (e);
1228
a292b002
MS
1229 if (TREE_CODE (e) == OFFSET_REF)
1230 e = resolve_offset_ref (e);
1231
a0a33927
MS
1232 if (TREE_READONLY_DECL_P (e))
1233 e = decl_constant_value (e);
1234
2986ae00 1235 if (INTEGRAL_CODE_P (code))
8d08fdba 1236 {
f0e01782 1237 tree intype = TREE_TYPE (e);
8d08fdba
MS
1238 enum tree_code form = TREE_CODE (intype);
1239 /* enum = enum, enum = int, enum = float are all errors. */
1240 if (flag_int_enum_equivalence == 0
1241 && TREE_CODE (type) == ENUMERAL_TYPE
6060a796
MS
1242 && ARITHMETIC_TYPE_P (intype)
1243 && ! (convtype & CONV_STATIC))
8d08fdba
MS
1244 {
1245 cp_pedwarn ("conversion from `%#T' to `%#T'", intype, type);
1246
1247 if (flag_pedantic_errors)
1248 return error_mark_node;
1249 }
a292b002 1250 if (IS_AGGR_TYPE (intype))
8d08fdba
MS
1251 {
1252 tree rval;
f0e01782 1253 rval = build_type_conversion (CONVERT_EXPR, type, e, 1);
00595019
MS
1254 if (rval)
1255 return rval;
1256 cp_error ("`%#T' used where a `%T' was expected", intype, type);
8d08fdba
MS
1257 return error_mark_node;
1258 }
2986ae00 1259 if (code == BOOLEAN_TYPE)
28ed4616 1260 return truthvalue_conversion (e);
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);
db5ae43f
MS
1286 tree ctor = NULL_TREE;
1287 tree conversion = NULL_TREE;
8d08fdba 1288
8d08fdba
MS
1289 dtype = TYPE_MAIN_VARIANT (dtype);
1290
1291 /* Conversion of object pointers or signature pointers/references
1292 to signature pointers/references. */
1293
1294 if (TYPE_LANG_SPECIFIC (type)
1295 && (IS_SIGNATURE_POINTER (type) || IS_SIGNATURE_REFERENCE (type)))
1296 {
1297 tree constructor = build_signature_pointer_constructor (type, expr);
1298 tree sig_ty = SIGNATURE_TYPE (type);
1299 tree sig_ptr;
1300
1301 if (constructor == error_mark_node)
1302 return error_mark_node;
1303
1304 sig_ptr = get_temp_name (type, 1);
1305 DECL_INITIAL (sig_ptr) = constructor;
1306 CLEAR_SIGNATURE (sig_ty);
6060a796 1307 finish_decl (sig_ptr, constructor, NULL_TREE, 0, 0);
8d08fdba
MS
1308 SET_SIGNATURE (sig_ty);
1309 TREE_READONLY (sig_ptr) = 1;
1310
1311 return sig_ptr;
1312 }
1313
1314 /* Conversion between aggregate types. New C++ semantics allow
1315 objects of derived type to be cast to objects of base type.
1316 Old semantics only allowed this between pointers.
1317
1318 There may be some ambiguity between using a constructor
1319 vs. using a type conversion operator when both apply. */
1320
db5ae43f
MS
1321 if (IS_AGGR_TYPE (dtype) && ! DERIVED_FROM_P (type, dtype)
1322 && TYPE_HAS_CONVERSION (dtype))
1323 conversion = build_type_conversion (CONVERT_EXPR, type, e, 1);
8d08fdba 1324
db5ae43f
MS
1325 if (conversion == error_mark_node)
1326 {
1327 error ("ambiguous pointer conversion");
1328 return conversion;
1329 }
8d08fdba 1330
db5ae43f
MS
1331 if (TYPE_HAS_CONSTRUCTOR (type))
1332 ctor = build_method_call (NULL_TREE, constructor_name_full (type),
1333 build_tree_list (NULL_TREE, e),
1334 TYPE_BINFO (type),
1335 LOOKUP_NORMAL | LOOKUP_SPECULATIVELY
6060a796 1336 | (convtype&CONV_NONCONVERTING ? 0 : LOOKUP_ONLYCONVERTING)
db5ae43f 1337 | (conversion ? LOOKUP_NO_CONVERSION : 0));
8d08fdba 1338
db5ae43f
MS
1339 if (ctor == error_mark_node)
1340 {
1341 cp_error ("in conversion to type `%T'", type);
8d08fdba
MS
1342 return error_mark_node;
1343 }
db5ae43f
MS
1344
1345 if (conversion && ctor)
8d08fdba 1346 {
db5ae43f
MS
1347 error ("both constructor and type conversion operator apply");
1348 return error_mark_node;
1349 }
1350 else if (conversion)
1351 return conversion;
1352 else if (ctor)
1353 {
1354 if (current_function_decl)
1355 /* We can't pass 1 to the with_cleanup_p arg here, because that
1356 screws up passing classes by value. */
1357 ctor = build_cplus_new (type, ctor, 0);
1358 else
8d08fdba 1359 {
db5ae43f
MS
1360 register tree parm = TREE_OPERAND (ctor, 1);
1361
1362 /* Initializers for static variables and parameters
1363 have to handle doing the initialization and
1364 cleanup themselves. */
1365 my_friendly_assert (TREE_CODE (ctor) == CALL_EXPR, 322);
1366#if 0
1367 /* The following assertion fails in cases where we
1368 are initializing a static member variable of a
1369 particular instance of a template class with a
1370 call to a constructor of the given instance, as
1371 in:
1372
1373 TMPL<int> object = TMPL<int>();
1374
1375 Curiously, the assertion does not fail if we do
1376 the same thing for a static member of a
1377 non-template class, as in:
1378
1379 T object = T();
1380
1381 I can't see why we should care here whether or not
1382 the initializer expression involves a call to
1383 `new', so for the time being, it seems best to
1384 just avoid doing this assertion. */
1385 my_friendly_assert (TREE_CALLS_NEW (TREE_VALUE (parm)),
1386 323);
1387#endif
1388 TREE_VALUE (parm) = NULL_TREE;
1389 ctor = build_indirect_ref (ctor, NULL_PTR);
1390 TREE_HAS_CONSTRUCTOR (ctor) = 1;
8d08fdba 1391 }
db5ae43f 1392 return ctor;
8d08fdba
MS
1393 }
1394 }
1395
f0e01782 1396 /* If TYPE or TREE_TYPE (E) is not on the permanent_obstack,
8d08fdba
MS
1397 then the it won't be hashed and hence compare as not equal,
1398 even when it is. */
1399 if (code == ARRAY_TYPE
f0e01782
MS
1400 && TREE_TYPE (TREE_TYPE (e)) == TREE_TYPE (type)
1401 && index_type_equal (TYPE_DOMAIN (TREE_TYPE (e)), TYPE_DOMAIN (type)))
1402 return e;
8d08fdba
MS
1403
1404 cp_error ("conversion from `%T' to non-scalar type `%T' requested",
1405 TREE_TYPE (expr), type);
1406 return error_mark_node;
1407}
1408
a4443a08
MS
1409/* Create an expression whose value is that of EXPR,
1410 converted to type TYPE. The TREE_TYPE of the value
1411 is always TYPE. This function implements all reasonable
1412 conversions; callers should filter out those that are
1413 not permitted by the language being compiled. */
1414
1415tree
1416convert (type, expr)
1417 tree type, expr;
1418{
1419 return cp_convert (type, expr, CONV_OLD_CONVERT, 0);
1420}
1421
8d08fdba
MS
1422/* Like convert, except permit conversions to take place which
1423 are not normally allowed due to access restrictions
1424 (such as conversion from sub-type to private super-type). */
1425tree
6060a796 1426convert_force (type, expr, convtype)
8d08fdba
MS
1427 tree type;
1428 tree expr;
6060a796 1429 int convtype;
8d08fdba
MS
1430{
1431 register tree e = expr;
1432 register enum tree_code code = TREE_CODE (type);
1433
1434 if (code == REFERENCE_TYPE)
a4443a08
MS
1435 return fold (convert_to_reference (type, e, CONV_C_CAST, LOOKUP_COMPLAIN,
1436 NULL_TREE));
8d08fdba
MS
1437 else if (TREE_CODE (TREE_TYPE (e)) == REFERENCE_TYPE)
1438 e = convert_from_reference (e);
1439
1440 if (code == POINTER_TYPE)
1441 return fold (convert_to_pointer_force (type, e));
1442
51c184be 1443 /* From typeck.c convert_for_assignment */
8d08fdba
MS
1444 if (((TREE_CODE (TREE_TYPE (e)) == POINTER_TYPE && TREE_CODE (e) == ADDR_EXPR
1445 && TREE_CODE (TREE_TYPE (e)) == POINTER_TYPE
1446 && TREE_CODE (TREE_TYPE (TREE_TYPE (e))) == METHOD_TYPE)
51c184be
MS
1447 || integer_zerop (e)
1448 || TYPE_PTRMEMFUNC_P (TREE_TYPE (e)))
8d08fdba
MS
1449 && TYPE_PTRMEMFUNC_P (type))
1450 {
1451 /* compatible pointer to member functions. */
51c184be 1452 return build_ptrmemfunc (TYPE_PTRMEMFUNC_FN_TYPE (type), e, 1);
8d08fdba 1453 }
6060a796
MS
1454
1455 return cp_convert (type, e, CONV_OLD_CONVERT|convtype, 0);
8d08fdba
MS
1456}
1457
1458/* Subroutine of build_type_conversion. */
1459static tree
1460build_type_conversion_1 (xtype, basetype, expr, typename, for_sure)
1461 tree xtype, basetype;
1462 tree expr;
1463 tree typename;
1464 int for_sure;
1465{
8d08fdba
MS
1466 tree rval;
1467 int flags;
1468
1469 if (for_sure == 0)
db5ae43f 1470 flags = LOOKUP_PROTECT|LOOKUP_ONLYCONVERTING;
8d08fdba 1471 else
db5ae43f 1472 flags = LOOKUP_NORMAL|LOOKUP_ONLYCONVERTING;
8d08fdba 1473
21474714 1474 rval = build_method_call (expr, typename, NULL_TREE, NULL_TREE, flags);
8d08fdba
MS
1475 if (rval == error_mark_node)
1476 {
1477 if (for_sure == 0)
1478 return NULL_TREE;
1479 return error_mark_node;
1480 }
8d08fdba
MS
1481 if (TREE_CODE (TREE_TYPE (rval)) == REFERENCE_TYPE
1482 && TREE_CODE (xtype) != REFERENCE_TYPE)
1483 rval = default_conversion (rval);
1484
1485 if (warn_cast_qual
1486 && TREE_TYPE (xtype)
1487 && (TREE_READONLY (TREE_TYPE (TREE_TYPE (rval)))
1488 > TREE_READONLY (TREE_TYPE (xtype))))
1489 warning ("user-defined conversion casting away `const'");
a4443a08 1490 return convert (xtype, rval);
8d08fdba
MS
1491}
1492
1493/* Convert an aggregate EXPR to type XTYPE. If a conversion
1494 exists, return the attempted conversion. This may
1495 return ERROR_MARK_NODE if the conversion is not
1496 allowed (references private members, etc).
1497 If no conversion exists, NULL_TREE is returned.
1498
1499 If (FOR_SURE & 1) is non-zero, then we allow this type conversion
1500 to take place immediately. Otherwise, we build a SAVE_EXPR
e1cd6e56 1501 which can be evaluated if the results are ever needed. */
8d08fdba 1502
8d08fdba
MS
1503tree
1504build_type_conversion (code, xtype, expr, for_sure)
1505 enum tree_code code;
1506 tree xtype, expr;
1507 int for_sure;
1508{
1509 /* C++: check to see if we can convert this aggregate type
e1cd6e56
MS
1510 into the required type. */
1511 tree basetype;
1512 tree conv;
1513 tree winner = NULL_TREE;
8d08fdba
MS
1514
1515 if (expr == error_mark_node)
1516 return error_mark_node;
1517
1518 basetype = TREE_TYPE (expr);
1519 if (TREE_CODE (basetype) == REFERENCE_TYPE)
1520 basetype = TREE_TYPE (basetype);
1521
1522 basetype = TYPE_MAIN_VARIANT (basetype);
1523 if (! TYPE_LANG_SPECIFIC (basetype) || ! TYPE_HAS_CONVERSION (basetype))
1524 return NULL_TREE;
1525
e1cd6e56
MS
1526 /* Do we have an exact match? */
1527 {
1528 tree typename = build_typename_overload (xtype);
1529 if (lookup_fnfields (TYPE_BINFO (basetype), typename, 0))
1530 return build_type_conversion_1 (xtype, basetype, expr, typename,
1531 for_sure);
1532 }
8d08fdba 1533
e1cd6e56
MS
1534 /* Nope; try looking for others. */
1535 for (conv = lookup_conversions (basetype); conv; conv = TREE_CHAIN (conv))
8d08fdba 1536 {
e1cd6e56
MS
1537 if (winner && TREE_PURPOSE (winner) == TREE_PURPOSE (conv))
1538 continue;
8d08fdba 1539
e1cd6e56 1540 if (can_convert (xtype, TREE_VALUE (conv)))
8d08fdba 1541 {
e1cd6e56 1542 if (winner)
8d08fdba 1543 {
e1cd6e56
MS
1544 cp_error ("ambiguous conversion from `%T' to `%T'", basetype,
1545 xtype);
1546 cp_error (" candidate conversion functions include `%T' and `%T'",
1547 TREE_VALUE (winner), TREE_VALUE (conv));
1548 return NULL_TREE;
8d08fdba
MS
1549 }
1550 else
e1cd6e56 1551 winner = conv;
8d08fdba
MS
1552 }
1553 }
1554
e1cd6e56
MS
1555 if (winner)
1556 return build_type_conversion_1 (xtype, basetype, expr,
1557 TREE_PURPOSE (winner), for_sure);
8d08fdba
MS
1558
1559 return NULL_TREE;
1560}
1561
1562/* Must convert two aggregate types to non-aggregate type.
1563 Attempts to find a non-ambiguous, "best" type conversion.
1564
1565 Return 1 on success, 0 on failure.
1566
1567 @@ What are the real semantics of this supposed to be??? */
1568int
1569build_default_binary_type_conversion (code, arg1, arg2)
1570 enum tree_code code;
1571 tree *arg1, *arg2;
1572{
1573 tree type1 = TREE_TYPE (*arg1);
1574 tree type2 = TREE_TYPE (*arg2);
1575
1576 if (TREE_CODE (type1) == REFERENCE_TYPE
1577 || TREE_CODE (type1) == POINTER_TYPE)
1578 type1 = TREE_TYPE (type1);
1579 if (TREE_CODE (type2) == REFERENCE_TYPE
1580 || TREE_CODE (type2) == POINTER_TYPE)
1581 type2 = TREE_TYPE (type2);
1582
1583 if (TREE_CODE (TYPE_NAME (type1)) != TYPE_DECL)
1584 {
1585 tree decl = typedecl_for_tag (type1);
1586 if (decl)
1587 error ("type conversion nonexistent for type `%s'",
1588 IDENTIFIER_POINTER (DECL_NAME (decl)));
1589 else
1590 error ("type conversion nonexistent for non-C++ type");
1591 return 0;
1592 }
1593 if (TREE_CODE (TYPE_NAME (type2)) != TYPE_DECL)
1594 {
1595 tree decl = typedecl_for_tag (type2);
1596 if (decl)
1597 error ("type conversion nonexistent for type `%s'",
1598 IDENTIFIER_POINTER (decl));
1599 else
1600 error ("type conversion nonexistent for non-C++ type");
1601 return 0;
1602 }
1603
1604 if (!IS_AGGR_TYPE (type1) || !TYPE_HAS_CONVERSION (type1))
1605 {
1606 if (!IS_AGGR_TYPE (type2) || !TYPE_HAS_CONVERSION (type2))
1607 cp_error ("no conversion from `%T' and `%T' to types with default `%O' ",
1608 type1, type2, code);
1609 else
1610 cp_error ("no conversion from `%T' to type with default `%O'",
1611 type1, code);
1612 return 0;
1613 }
1614 else if (!IS_AGGR_TYPE (type2) || !TYPE_HAS_CONVERSION (type2))
1615 {
1616 cp_error ("no conversion from `%T' to type with default `%O'",
1617 type2, code);
1618 return 0;
1619 }
1620
2986ae00
MS
1621 if (code == TRUTH_ANDIF_EXPR
1622 || code == TRUTH_ORIF_EXPR)
8d08fdba 1623 {
28ed4616
JM
1624 *arg1 = convert (boolean_type_node, *arg1);
1625 *arg2 = convert (boolean_type_node, *arg2);
2986ae00
MS
1626 }
1627 else if (TYPE_HAS_INT_CONVERSION (type1))
1628 {
1629 if (TYPE_HAS_REAL_CONVERSION (type1))
1630 cp_pedwarn ("ambiguous type conversion for type `%T', defaulting to int",
1631 type1);
8d08fdba
MS
1632 *arg1 = build_type_conversion (code, integer_type_node, *arg1, 1);
1633 *arg2 = build_type_conversion (code, integer_type_node, *arg2, 1);
1634 }
1635 else if (TYPE_HAS_REAL_CONVERSION (type1))
1636 {
1637 *arg1 = build_type_conversion (code, double_type_node, *arg1, 1);
1638 *arg2 = build_type_conversion (code, double_type_node, *arg2, 1);
1639 }
1640 else
1641 {
1642 *arg1 = build_type_conversion (code, ptr_type_node, *arg1, 1);
1643 if (*arg1 == error_mark_node)
1644 error ("ambiguous pointer conversion");
1645 *arg2 = build_type_conversion (code, ptr_type_node, *arg2, 1);
1646 if (*arg1 != error_mark_node && *arg2 == error_mark_node)
1647 error ("ambiguous pointer conversion");
1648 }
1649 if (*arg1 == 0)
1650 {
1651 if (*arg2 == 0 && type1 != type2)
1652 cp_error ("default type conversion for types `%T' and `%T' failed",
1653 type1, type2);
1654 else
1655 cp_error ("default type conversion for type `%T' failed", type1);
1656 return 0;
1657 }
1658 else if (*arg2 == 0)
1659 {
1660 cp_error ("default type conversion for type `%T' failed", type2);
1661 return 0;
1662 }
1663 return 1;
1664}
1665
2986ae00 1666/* Must convert an aggregate type to non-aggregate type.
8d08fdba
MS
1667 Attempts to find a non-ambiguous, "best" type conversion.
1668
1669 Return 1 on success, 0 on failure.
1670
1671 The type of the argument is expected to be of aggregate type here.
1672
1673 @@ What are the real semantics of this supposed to be??? */
1674int
1675build_default_unary_type_conversion (code, arg)
1676 enum tree_code code;
1677 tree *arg;
1678{
1679 tree type = TREE_TYPE (*arg);
8d08fdba
MS
1680
1681 if (! TYPE_HAS_CONVERSION (type))
1682 {
2986ae00 1683 cp_error ("type conversion required for type `%T'", type);
8d08fdba
MS
1684 return 0;
1685 }
1686
2986ae00 1687 if (code == TRUTH_NOT_EXPR)
28ed4616 1688 *arg = convert (boolean_type_node, *arg);
2986ae00
MS
1689 else if (TYPE_HAS_INT_CONVERSION (type))
1690 {
1691 if (TYPE_HAS_REAL_CONVERSION (type))
1692 cp_pedwarn ("ambiguous type conversion for type `%T', defaulting to int",
1693 type);
1694 *arg = build_type_conversion (code, integer_type_node, *arg, 1);
1695 }
8d08fdba
MS
1696 else if (TYPE_HAS_REAL_CONVERSION (type))
1697 *arg = build_type_conversion (code, double_type_node, *arg, 1);
1698 else
1699 {
1700 *arg = build_type_conversion (code, ptr_type_node, *arg, 1);
1701 if (*arg == error_mark_node)
1702 error ("ambiguous pointer conversion");
1703 }
1704 if (*arg == NULL_TREE)
1705 {
2986ae00 1706 cp_error ("default type conversion for type `%T' failed", type);
8d08fdba
MS
1707 return 0;
1708 }
1709 return 1;
1710}
39211cd5
MS
1711
1712/* Implements integral promotion (4.1) and float->double promotion. */
1713tree
1714type_promotes_to (type)
1715 tree type;
1716{
1717 int constp = TYPE_READONLY (type);
1718 int volatilep = TYPE_VOLATILE (type);
1719 type = TYPE_MAIN_VARIANT (type);
2986ae00
MS
1720
1721 /* bool always promotes to int (not unsigned), even if it's the same
1722 size. */
28ed4616 1723 if (type == boolean_type_node)
2986ae00
MS
1724 type = integer_type_node;
1725
1726 /* Normally convert enums to int, but convert wide enums to something
1727 wider. */
1728 else if (TREE_CODE (type) == ENUMERAL_TYPE
1729 || type == wchar_type_node)
cf2ac46f
JM
1730 {
1731 int precision = MAX (TYPE_PRECISION (type),
1732 TYPE_PRECISION (integer_type_node));
1733 tree totype = type_for_size (precision, 0);
1734 if (TREE_UNSIGNED (type)
1735 && ! int_fits_type_p (TYPE_MAX_VALUE (type), totype))
1736 type = type_for_size (precision, 1);
1737 else
1738 type = totype;
1739 }
39211cd5
MS
1740 else if (C_PROMOTING_INTEGER_TYPE_P (type))
1741 {
1742 /* Traditionally, unsignedness is preserved in default promotions.
1743 Otherwise, retain unsignedness if really not getting bigger. */
1744 if (TREE_UNSIGNED (type)
1745 && (flag_traditional
1746 || TYPE_PRECISION (type) == TYPE_PRECISION (integer_type_node)))
1747 type = unsigned_type_node;
1748 else
1749 type = integer_type_node;
1750 }
1751 else if (type == float_type_node)
1752 type = double_type_node;
1753
f376e137 1754 return cp_build_type_variant (type, constp, volatilep);
39211cd5 1755}
This page took 0.274049 seconds and 5 git commands to generate.