]> gcc.gnu.org Git - gcc.git/blame - gcc/cp/cvt.c
calls.c (expand_call): Only destroy temporaries at the end of function calls, if...
[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
189 if (form == INTEGER_TYPE || form == ENUMERAL_TYPE)
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))
307 target_type = build_type_variant (target_type, TYPE_READONLY (type),
308 TYPE_VOLATILE (type));
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
375 goto done_but_maybe_warn;
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
412 goto done_but_maybe_warn;
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:
428 if (targ == current_class_decl)
429 {
430 error ("address of `this' not available");
431#if 0
432 /* This code makes the following core dump the compiler on a sun4,
433 if the code below is used.
434
435 class e_decl;
436 class a_decl;
437 typedef a_decl* a_ref;
438
439 class a_s {
440 public:
441 a_s();
442 void* append(a_ref& item);
443 };
444 class a_decl {
445 public:
446 a_decl (e_decl *parent);
447 a_s generic_s;
448 a_s decls;
449 e_decl* parent;
450 };
451
452 class e_decl {
453 public:
454 e_decl();
455 a_s implementations;
456 };
457
458 void foobar(void *);
459
460 a_decl::a_decl(e_decl *parent) {
461 parent->implementations.append(this);
462 }
463 */
464
465 TREE_ADDRESSABLE (targ) = 1; /* so compiler doesn't die later */
466 put_var_into_stack (targ);
467 break;
468#else
469 return error_mark_node;
470#endif
471 }
472 /* Fall through. */
473 case VAR_DECL:
474 case CONST_DECL:
475 if (DECL_REGISTER (targ) && !TREE_ADDRESSABLE (targ))
476 warning ("address needed to build reference for `%s', which is declared `register'",
477 IDENTIFIER_POINTER (DECL_NAME (targ)));
478 else if (staticp (targ))
479 literal_flag = 1;
480
481 TREE_ADDRESSABLE (targ) = 1;
482 put_var_into_stack (targ);
483 break;
484
485 case COMPOUND_EXPR:
486 {
487 tree real_reference = build_up_reference (type, TREE_OPERAND (targ, 1),
488 LOOKUP_PROTECT, checkconst);
489 rval = build (COMPOUND_EXPR, type, TREE_OPERAND (targ, 0), real_reference);
490 TREE_CONSTANT (rval) = staticp (TREE_OPERAND (targ, 1));
491 return rval;
492 }
493
494 case MODIFY_EXPR:
495 case INIT_EXPR:
496 {
497 tree real_reference = build_up_reference (type, TREE_OPERAND (targ, 0),
498 LOOKUP_PROTECT, checkconst);
499 rval = build (COMPOUND_EXPR, type, arg, real_reference);
500 TREE_CONSTANT (rval) = staticp (TREE_OPERAND (targ, 0));
501 return rval;
502 }
503
504 case COND_EXPR:
505 return build (COND_EXPR, type,
506 TREE_OPERAND (targ, 0),
507 build_up_reference (type, TREE_OPERAND (targ, 1),
508 LOOKUP_PROTECT, checkconst),
509 build_up_reference (type, TREE_OPERAND (targ, 2),
510 LOOKUP_PROTECT, checkconst));
511
512 case WITH_CLEANUP_EXPR:
513 return build (WITH_CLEANUP_EXPR, type,
514 build_up_reference (type, TREE_OPERAND (targ, 0),
515 LOOKUP_PROTECT, checkconst),
516 0, TREE_OPERAND (targ, 2));
517
518 case BIND_EXPR:
519 arg = TREE_OPERAND (targ, 1);
520 if (arg == NULL_TREE)
521 {
522 compiler_error ("({ ... }) expression not expanded when needed for reference");
523 return error_mark_node;
524 }
525 rval = build1 (ADDR_EXPR, type, arg);
526 TREE_REFERENCE_EXPR (rval) = 1;
527 return rval;
528
529 default:
530 break;
531 }
532
533 if (TREE_ADDRESSABLE (targ) == 0)
534 {
535 tree temp;
536
537 if (TREE_CODE (targ) == CALL_EXPR && IS_AGGR_TYPE (argtype))
538 {
539 temp = build_cplus_new (argtype, targ, 1);
540 rval = build1 (ADDR_EXPR, type, temp);
541 goto done;
542 }
543 else
544 {
545 temp = get_temp_name (argtype, 0);
546 if (global_bindings_p ())
547 {
548 /* Give this new temp some rtl and initialize it. */
549 DECL_INITIAL (temp) = targ;
550 TREE_STATIC (temp) = 1;
551 finish_decl (temp, targ, NULL_TREE, 0);
552 /* Do this after declaring it static. */
553 rval = build_unary_op (ADDR_EXPR, temp, 0);
554 TREE_TYPE (rval) = type;
555 literal_flag = TREE_CONSTANT (rval);
556 goto done;
557 }
558 else
559 {
560 rval = build_unary_op (ADDR_EXPR, temp, 0);
561 if (binfo && !BINFO_OFFSET_ZEROP (binfo))
562 rval = convert_pointer_to (target_type, rval);
563 else
564 TREE_TYPE (rval) = type;
565
566 temp = build (MODIFY_EXPR, argtype, temp, arg);
567 TREE_SIDE_EFFECTS (temp) = 1;
568 return build (COMPOUND_EXPR, type, temp, rval);
569 }
570 }
571 }
572 else
573 rval = build1 (ADDR_EXPR, type, arg);
574
575 done_but_maybe_warn:
576 if (checkconst && TREE_READONLY (arg) && ! TYPE_READONLY (target_type))
577 readonly_error (arg, "conversion to reference", 1);
578
579 done:
580 if (TYPE_USES_COMPLEX_INHERITANCE (argtype))
581 {
39211cd5 582 TREE_TYPE (rval) = build_pointer_type (argtype);
51c184be
MS
583 if (flags & LOOKUP_PROTECT)
584 rval = convert_pointer_to (target_type, rval);
585 else
586 rval
587 = convert_to_pointer_force (build_pointer_type (target_type), rval);
8d08fdba
MS
588 TREE_TYPE (rval) = type;
589 }
590 TREE_CONSTANT (rval) = literal_flag;
591 return rval;
592}
593
594/* For C++: Only need to do one-level references, but cannot
595 get tripped up on signed/unsigned differences.
596
597 If DECL is NULL_TREE it means convert as though casting (by force).
598 If it is ERROR_MARK_NODE, it means the conversion is implicit,
599 and that temporaries may be created.
600 Make sure the use of user-defined conversion operators is un-ambiguous.
601 Otherwise, DECL is a _DECL node which can be used in error reporting.
602
603 FNDECL, PARMNUM, and ERRTYPE are only used when checking for use of
604 volatile or const references where they aren't desired. */
605
606tree
607convert_to_reference (decl, reftype, expr, fndecl, parmnum,
608 errtype, strict, flags)
609 tree decl;
610 tree reftype, expr;
611 tree fndecl;
612 int parmnum;
613 char *errtype;
614 int strict, flags;
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
39211cd5 621#if 0
8926095f
MS
622 if (TREE_CODE (type) == ARRAY_TYPE)
623 type = build_pointer_type (TREE_TYPE (type));
39211cd5
MS
624#endif
625
8d08fdba
MS
626 if (form == REFERENCE_TYPE)
627 intype = TREE_TYPE (intype);
628 intype = TYPE_MAIN_VARIANT (intype);
629
630 if (IS_AGGR_TYPE (intype)
631 && (rval = build_type_conversion (CONVERT_EXPR, reftype, expr, 1)))
632 {
633 if (rval == error_mark_node)
634 cp_error ("conversion from `%T' to `%T' is ambiguous",
635 intype, reftype);
636 return rval;
637 }
638
639 if (comptypes (type, intype, strict))
640 {
641 /* Section 13. */
642 if (flags & LOOKUP_COMPLAIN)
643 {
8926095f
MS
644 /* Since convert_for_initialization didn't call
645 convert_for_assignment, we have to do this checking here.
646 FIXME: We should have a common routine between here and
647 convert_for_assignment. */
39211cd5
MS
648
649 tree ttl = TREE_TYPE (reftype);
650 tree ttr;
651
8926095f 652 if (form == REFERENCE_TYPE)
39211cd5
MS
653 ttr = TREE_TYPE (TREE_TYPE (expr));
654 else
655 ttr = TREE_TYPE (expr);
8d08fdba 656
39211cd5
MS
657 if (! TYPE_READONLY (ttl))
658 {
659 if (TYPE_READONLY (ttr) && decl != NULL_TREE)
8d08fdba
MS
660 {
661 if (fndecl)
662 cp_pedwarn ("passing `%T' as argument %P of `%D' discards const",
663 TREE_TYPE (expr), parmnum, fndecl);
664 else
665 cp_pedwarn ("%s to `%T' from `%T' discards const",
666 errtype, reftype, TREE_TYPE (expr));
667 }
39211cd5 668 else if (! lvalue_p (expr))
8d08fdba 669 {
39211cd5 670 /* Ensure semantics of 8.4.3 */
8d08fdba 671 if (fndecl)
39211cd5 672 cp_pedwarn ("ANSI C++ forbids passing non-lvalue `%T' as argument %P of `%D' into non-const &",
8d08fdba
MS
673 TREE_TYPE (expr), parmnum, fndecl);
674 else
39211cd5 675 cp_pedwarn ("ANSI C++ forbids %s to `%T' from non-lvalue `%T'",
8d08fdba
MS
676 errtype, reftype, TREE_TYPE (expr));
677 }
39211cd5
MS
678 }
679 else if (! TYPE_VOLATILE (ttl) && TYPE_VOLATILE (ttr)
680 && decl != NULL_TREE)
8d08fdba 681 {
8d08fdba 682 if (fndecl)
39211cd5 683 cp_pedwarn ("passing `%T' as argument %P of `%D' discards volatile",
8d08fdba
MS
684 TREE_TYPE (expr), parmnum, fndecl);
685 else
39211cd5 686 cp_pedwarn ("%s to `%T' from `%T' discards volatile",
8d08fdba
MS
687 errtype, reftype, TREE_TYPE (expr));
688 }
689 }
39211cd5 690
8d08fdba
MS
691 /* If EXPR is of aggregate type, and is really a CALL_EXPR,
692 then we don't need to convert it to reference type if
693 it is only being used to initialize DECL which is also
694 of the same aggregate type. */
8926095f
MS
695 if (decl != NULL_TREE && decl != error_mark_node
696 && IS_AGGR_TYPE (type)
697 && TREE_CODE (expr) == CALL_EXPR
698 && TYPE_MAIN_VARIANT (type) == intype)
8d08fdba 699 {
8926095f
MS
700 tree e1 = build (INIT_EXPR, void_type_node, decl, expr);
701 tree e2;
8d08fdba 702
8926095f
MS
703 TREE_SIDE_EFFECTS (e1) = 1;
704 if (form == REFERENCE_TYPE)
705 e2 = build1 (NOP_EXPR, reftype, decl);
706 else
707 {
708 e2 = build_unary_op (ADDR_EXPR, decl, 0);
709 TREE_TYPE (e2) = reftype;
710 TREE_REFERENCE_EXPR (e2) = 1;
8d08fdba 711 }
8926095f
MS
712 return build_compound_expr
713 (tree_cons (NULL_TREE, e1, build_tree_list (NULL_TREE, e2)));
714 }
715
716 else if (form == REFERENCE_TYPE)
717 {
7177d104
MS
718 rval = build1 (NOP_EXPR,
719 build_pointer_type (TREE_TYPE (TREE_TYPE (expr))),
720 expr);
8926095f 721 rval = convert (build_pointer_type (TREE_TYPE (reftype)), rval);
7177d104 722 rval = build1 (NOP_EXPR, reftype, rval);
8926095f 723 return rval;
8d08fdba 724 }
8926095f 725
8d08fdba
MS
726 return build_up_reference (reftype, expr, flags, decl!=NULL_TREE);
727 }
728
8926095f
MS
729 if (decl == NULL_TREE && lvalue_p (expr))
730 {
731 /* When casting an lvalue to a reference type, just convert into
732 a pointer to the new type and deference it. This is allowed
a28e3c7f 733 by San Diego WP section 5.2.9 paragraph 12, though perhaps it
8926095f 734 should be done directly (jason). (int &)ri ---> *(int*)&ri */
a28e3c7f
MS
735
736 /* B* bp; A& ar = (A&)bp; is legal, but it's probably not what they
737 meant. */
738 if (form == POINTER_TYPE
739 && (comptypes (TREE_TYPE (intype), type, strict)))
740 cp_warning ("casting `%T' to `%T' does not dereference pointer",
741 intype, reftype);
742
8926095f
MS
743 rval = build_unary_op (ADDR_EXPR, expr, 0);
744 if (rval != error_mark_node)
745 rval = convert_force (build_pointer_type (TREE_TYPE (reftype)), rval);
746 if (rval != error_mark_node)
7177d104 747 rval = build1 (NOP_EXPR, reftype, rval);
8926095f
MS
748 }
749 else if (decl == error_mark_node || decl == NULL_TREE)
8d08fdba
MS
750 {
751 tree rval_as_conversion = NULL_TREE;
752 tree rval_as_ctor = NULL_TREE;
753
754 if (IS_AGGR_TYPE (intype)
755 && (rval = build_type_conversion (CONVERT_EXPR, type, expr, 1)))
756 {
757 if (rval == error_mark_node)
758 return rval;
759
760 rval_as_conversion = build_up_reference (reftype, rval, flags, 1);
761 }
762
763 /* Definitely need to go through a constructor here. */
764 if (TYPE_HAS_CONSTRUCTOR (type)
a28e3c7f 765 && ! CLASSTYPE_ABSTRACT_VIRTUALS (type)
8d08fdba
MS
766 && (rval = build_method_call
767 (NULL_TREE, constructor_name_full (type),
768 build_tree_list (NULL_TREE, expr), TYPE_BINFO (type),
769 LOOKUP_NO_CONVERSION|LOOKUP_SPECULATIVELY)))
770 {
771 tree init;
772
773 if (global_bindings_p ())
774 {
775 extern tree static_aggregates;
776 decl = get_temp_name (type, global_bindings_p ());
777 init = build_method_call (decl, constructor_name_full (type),
778 build_tree_list (NULL_TREE, expr),
779 TYPE_BINFO (type), LOOKUP_NORMAL|LOOKUP_NO_CONVERSION);
780
781 if (init == error_mark_node)
782 return error_mark_node;
783
784 make_decl_rtl (decl, NULL_PTR, 1);
785 static_aggregates = perm_tree_cons (expr, decl, static_aggregates);
786 rval = build_unary_op (ADDR_EXPR, decl, 0);
787 }
788 else
789 {
790 init = build_method_call (NULL_TREE, constructor_name_full (type),
791 build_tree_list (NULL_TREE, expr),
792 TYPE_BINFO (type), LOOKUP_NORMAL|LOOKUP_NO_CONVERSION);
793
794 if (init == error_mark_node)
795 return error_mark_node;
796
797 rval = build_cplus_new (type, init, 1);
798 rval = build_up_reference (reftype, rval, flags, 1);
799 }
800 rval_as_ctor = rval;
801 }
802
803 if (rval_as_ctor && rval_as_conversion)
804 {
805 cp_error ("ambiguous conversion from `%T' to `%T'; both user-defined conversion and constructor apply",
806 intype, reftype);
807 return error_mark_node;
808 }
809 else if (rval_as_ctor)
810 rval = rval_as_ctor;
811 else if (rval_as_conversion)
812 rval = rval_as_conversion;
813 else if (! IS_AGGR_TYPE (type) && ! IS_AGGR_TYPE (intype))
814 {
815 rval = convert (type, expr);
816 if (rval == error_mark_node)
817 return error_mark_node;
818
819 rval = build_up_reference (reftype, rval, flags, 1);
820 }
821
822 if (rval && ! TYPE_READONLY (TREE_TYPE (reftype)))
823 cp_pedwarn ("converting `%T' to non-const `%T' will use a temporary",
824 intype, reftype);
825 }
826
827 if (rval)
828 {
829 /* If we found a way to convert earlier, then use it. */
830 return rval;
831 }
832
833 my_friendly_assert (form != OFFSET_TYPE, 189);
834
8d08fdba
MS
835 if (flags & LOOKUP_SPECULATIVELY)
836 return NULL_TREE;
837
8926095f
MS
838 else if (flags & LOOKUP_COMPLAIN)
839 cp_error ("cannot convert type `%T' to type `%T'", intype, reftype);
840
8d08fdba
MS
841 return error_mark_node;
842}
843
844/* We are using a reference VAL for its value. Bash that reference all the
845 way down to its lowest form. */
846tree
847convert_from_reference (val)
848 tree val;
849{
850 tree type = TREE_TYPE (val);
851
852 if (TREE_CODE (type) == OFFSET_TYPE)
853 type = TREE_TYPE (type);
854 if (TREE_CODE (type) == REFERENCE_TYPE)
855 {
856 tree target_type = TREE_TYPE (type);
857 tree nval;
858
859 /* This can happen if we cast to a reference type. */
860 if (TREE_CODE (val) == ADDR_EXPR)
861 {
862 nval = build1 (NOP_EXPR, build_pointer_type (target_type), val);
863 nval = build_indirect_ref (nval, NULL_PTR);
864 /* The below was missing, are other important flags missing too? */
865 TREE_SIDE_EFFECTS (nval) = TREE_SIDE_EFFECTS (val);
866 return nval;
867 }
868
39211cd5 869 nval = build1 (INDIRECT_REF, target_type, val);
8d08fdba
MS
870
871 TREE_THIS_VOLATILE (nval) = TYPE_VOLATILE (target_type);
872 TREE_SIDE_EFFECTS (nval) = TYPE_VOLATILE (target_type);
873 TREE_READONLY (nval) = TYPE_READONLY (target_type);
874 /* The below was missing, are other important flags missing too? */
875 TREE_SIDE_EFFECTS (nval) |= TREE_SIDE_EFFECTS (val);
876 return nval;
877 }
878 return val;
879}
880\f
881/* See if there is a constructor of type TYPE which will convert
882 EXPR. The reference manual seems to suggest (8.5.6) that we need
883 not worry about finding constructors for base classes, then converting
884 to the derived class.
885
886 MSGP is a pointer to a message that would be an appropriate error
887 string. If MSGP is NULL, then we are not interested in reporting
888 errors. */
889tree
890convert_to_aggr (type, expr, msgp, protect)
891 tree type, expr;
892 char **msgp;
893 int protect;
894{
895 tree basetype = type;
896 tree name = TYPE_IDENTIFIER (basetype);
897 tree function, fndecl, fntype, parmtypes, parmlist, result;
898 tree method_name;
899 enum access_type access;
900 int can_be_private, can_be_protected;
901
902 if (! TYPE_HAS_CONSTRUCTOR (basetype))
903 {
904 if (msgp)
905 *msgp = "type `%s' does not have a constructor";
906 return error_mark_node;
907 }
908
909 access = access_public;
910 can_be_private = 0;
911 can_be_protected = IDENTIFIER_CLASS_VALUE (name) || name == current_class_name;
912
913 parmlist = build_tree_list (NULL_TREE, expr);
914 parmtypes = tree_cons (NULL_TREE, TREE_TYPE (expr), void_list_node);
915
916 if (TYPE_USES_VIRTUAL_BASECLASSES (basetype))
917 {
918 parmtypes = tree_cons (NULL_TREE, integer_type_node, parmtypes);
919 parmlist = tree_cons (NULL_TREE, integer_one_node, parmlist);
920 }
921
922 /* The type of the first argument will be filled in inside the loop. */
923 parmlist = tree_cons (NULL_TREE, integer_zero_node, parmlist);
924 parmtypes = tree_cons (NULL_TREE, TYPE_POINTER_TO (basetype), parmtypes);
925
926 method_name = build_decl_overload (name, parmtypes, 1);
927
928 /* constructors are up front. */
929 fndecl = TREE_VEC_ELT (CLASSTYPE_METHOD_VEC (basetype), 0);
930 if (TYPE_HAS_DESTRUCTOR (basetype))
931 fndecl = DECL_CHAIN (fndecl);
932
933 while (fndecl)
934 {
935 if (DECL_ASSEMBLER_NAME (fndecl) == method_name)
936 {
937 function = fndecl;
938 if (protect)
939 {
940 if (TREE_PRIVATE (fndecl))
941 {
942 can_be_private =
943 (basetype == current_class_type
944 || is_friend (basetype, current_function_decl)
945 || purpose_member (basetype, DECL_ACCESS (fndecl)));
946 if (! can_be_private)
947 goto found;
948 }
949 else if (TREE_PROTECTED (fndecl))
950 {
951 if (! can_be_protected)
952 goto found;
953 }
954 }
955 goto found_and_ok;
956 }
957 fndecl = DECL_CHAIN (fndecl);
958 }
959
960 /* No exact conversion was found. See if an approximate
961 one will do. */
962 fndecl = TREE_VEC_ELT (CLASSTYPE_METHOD_VEC (basetype), 0);
963 if (TYPE_HAS_DESTRUCTOR (basetype))
964 fndecl = DECL_CHAIN (fndecl);
965
966 {
967 int saw_private = 0;
968 int saw_protected = 0;
969 struct candidate *candidates =
970 (struct candidate *) alloca ((decl_list_length (fndecl)+1) * sizeof (struct candidate));
971 struct candidate *cp = candidates;
972
973 while (fndecl)
974 {
975 function = fndecl;
976 cp->h_len = 2;
977 if (flag_ansi_overloading)
978 cp->v.ansi_harshness = (struct harshness_code *)
979 alloca (3 * sizeof (struct harshness_code));
980 else
981 cp->v.old_harshness = (unsigned short *)
982 alloca (3 * sizeof (short));
983
984 compute_conversion_costs (fndecl, parmlist, cp, 2);
985 if ((flag_ansi_overloading && (cp->h.code & EVIL_CODE) == 0)
986 || (!flag_ansi_overloading && cp->evil == 0))
987 {
988 cp->u.field = fndecl;
989 if (protect)
990 {
991 if (TREE_PRIVATE (fndecl))
992 access = access_private;
993 else if (TREE_PROTECTED (fndecl))
994 access = access_protected;
995 else
996 access = access_public;
997 }
998 else
999 access = access_public;
1000
1001 if (access == access_private
1002 ? (basetype == current_class_type
1003 || is_friend (basetype, cp->function)
1004 || purpose_member (basetype, DECL_ACCESS (fndecl)))
1005 : access == access_protected
1006 ? (can_be_protected
1007 || purpose_member (basetype, DECL_ACCESS (fndecl)))
1008 : 1)
1009 {
1010 if ((flag_ansi_overloading && cp->h.code <= TRIVIAL_CODE)
1011 || (!flag_ansi_overloading
1012 && cp->user == 0 && cp->b_or_d == 0
1013 && cp->easy <= 1))
1014 goto found_and_ok;
1015 cp++;
1016 }
1017 else
1018 {
1019 if (access == access_private)
1020 saw_private = 1;
1021 else
1022 saw_protected = 1;
1023 }
1024 }
1025 fndecl = DECL_CHAIN (fndecl);
1026 }
1027 if (cp - candidates)
1028 {
1029 /* Rank from worst to best. Then cp will point to best one.
1030 Private fields have their bits flipped. For unsigned
1031 numbers, this should make them look very large.
1032 If the best alternate has a (signed) negative value,
1033 then all we ever saw were private members. */
1034 if (cp - candidates > 1)
1035 qsort (candidates, /* char *base */
1036 cp - candidates, /* int nel */
1037 sizeof (struct candidate), /* int width */
1038 rank_for_overload); /* int (*compar)() */
1039
1040 --cp;
1041 if ((flag_ansi_overloading && (cp->h.code & EVIL_CODE))
1042 || (!flag_ansi_overloading && cp->evil > 1))
1043 {
1044 if (msgp)
1045 *msgp = "ambiguous type conversion possible for `%s'";
1046 return error_mark_node;
1047 }
1048
1049 function = cp->function;
1050 fndecl = cp->u.field;
1051 goto found_and_ok;
1052 }
1053 else if (msgp)
1054 {
1055 if (saw_private)
1056 if (saw_protected)
1057 *msgp = "only private and protected conversions apply";
1058 else
1059 *msgp = "only private conversions apply";
1060 else if (saw_protected)
1061 *msgp = "only protected conversions apply";
8926095f
MS
1062 else
1063 *msgp = "no appropriate conversion to type `%s'";
8d08fdba
MS
1064 }
1065 return error_mark_node;
1066 }
1067 /* NOTREACHED */
1068
8d08fdba
MS
1069 found:
1070 if (access == access_private)
1071 if (! can_be_private)
1072 {
1073 if (msgp)
1074 *msgp = TREE_PRIVATE (fndecl)
1075 ? "conversion to type `%s' is private"
1076 : "conversion to type `%s' is from private base class";
1077 return error_mark_node;
1078 }
1079 if (access == access_protected)
1080 if (! can_be_protected)
1081 {
1082 if (msgp)
1083 *msgp = TREE_PRIVATE (fndecl)
1084 ? "conversion to type `%s' is protected"
1085 : "conversion to type `%s' is from protected base class";
1086 return error_mark_node;
1087 }
1088 function = fndecl;
1089 found_and_ok:
1090
1091 /* It will convert, but we don't do anything about it yet. */
1092 if (msgp == 0)
1093 return NULL_TREE;
1094
1095 fntype = TREE_TYPE (function);
1096 if (DECL_INLINE (function) && TREE_CODE (function) == FUNCTION_DECL)
1097 function = build1 (ADDR_EXPR, build_pointer_type (fntype), function);
1098 else
1099 function = default_conversion (function);
1100
1101 result = build_nt (CALL_EXPR, function,
1102 convert_arguments (NULL_TREE, TYPE_ARG_TYPES (fntype),
1103 parmlist, NULL_TREE, LOOKUP_NORMAL),
1104 NULL_TREE);
1105 TREE_TYPE (result) = TREE_TYPE (fntype);
1106 TREE_SIDE_EFFECTS (result) = 1;
1107 TREE_RAISES (result) = !! TYPE_RAISES_EXCEPTIONS (fntype);
1108 return result;
1109}
1110
1111/* Call this when we know (for any reason) that expr is not, in fact,
1112 zero. This routine is like convert_pointer_to, but it pays
1113 attention to which specific instance of what type we want to
1114 convert to. This routine should eventually become
1115 convert_to_pointer after all references to convert_to_pointer
1116 are removed. */
1117tree
1118convert_pointer_to_real (binfo, expr)
1119 tree binfo, expr;
1120{
1121 register tree intype = TREE_TYPE (expr);
1122 tree ptr_type;
1123 tree type, rval;
1124
1125 if (TREE_CODE (binfo) == TREE_VEC)
1126 type = BINFO_TYPE (binfo);
1127 else if (IS_AGGR_TYPE (binfo))
1128 {
1129 type = binfo;
1130 }
1131 else
1132 {
1133 type = binfo;
1134 binfo = NULL_TREE;
1135 }
1136
1137 ptr_type = build_pointer_type (type);
1138 if (ptr_type == TYPE_MAIN_VARIANT (intype))
1139 return expr;
1140
1141 if (intype == error_mark_node)
1142 return error_mark_node;
1143
1144 my_friendly_assert (!integer_zerop (expr), 191);
1145
1146 if (TREE_CODE (type) == RECORD_TYPE
1147 && TREE_CODE (TREE_TYPE (intype)) == RECORD_TYPE
1148 && type != TYPE_MAIN_VARIANT (TREE_TYPE (intype)))
1149 {
1150 tree path;
1151 int distance
1152 = get_base_distance (binfo, TYPE_MAIN_VARIANT (TREE_TYPE (intype)),
1153 0, &path);
1154
1155 /* This function shouldn't be called with unqualified arguments
1156 but if it is, give them an error message that they can read. */
1157 if (distance < 0)
1158 {
7177d104
MS
1159 cp_error ("cannot convert a pointer of type `%T' to a pointer of type `%T'",
1160 TREE_TYPE (intype), type);
8d08fdba
MS
1161
1162 if (distance == -2)
1163 cp_error ("because `%T' is an ambiguous base class", type);
1164 return error_mark_node;
1165 }
1166
1167 return build_vbase_path (PLUS_EXPR, ptr_type, expr, path, 1);
1168 }
1169 rval = build1 (NOP_EXPR, ptr_type,
1170 TREE_CODE (expr) == NOP_EXPR ? TREE_OPERAND (expr, 0) : expr);
1171 TREE_CONSTANT (rval) = TREE_CONSTANT (expr);
1172 return rval;
1173}
1174
1175/* Call this when we know (for any reason) that expr is
1176 not, in fact, zero. This routine gets a type out of the first
1177 argument and uses it to search for the type to convert to. If there
1178 is more than one instance of that type in the expr, the conversion is
1179 ambiguous. This routine should eventually go away, and all
1180 callers should use convert_to_pointer_real. */
1181tree
1182convert_pointer_to (binfo, expr)
1183 tree binfo, expr;
1184{
1185 tree type;
1186
1187 if (TREE_CODE (binfo) == TREE_VEC)
1188 type = BINFO_TYPE (binfo);
1189 else if (IS_AGGR_TYPE (binfo))
1190 type = binfo;
1191 else
1192 type = binfo;
1193 return convert_pointer_to_real (type, expr);
1194}
1195
1196/* Same as above, but don't abort if we get an "ambiguous" baseclass.
1197 There's only one virtual baseclass we are looking for, and once
1198 we find one such virtual baseclass, we have found them all. */
1199
1200tree
1201convert_pointer_to_vbase (binfo, expr)
1202 tree binfo;
1203 tree expr;
1204{
1205 tree intype = TREE_TYPE (TREE_TYPE (expr));
1206 tree binfos = TYPE_BINFO_BASETYPES (intype);
1207 int i;
1208
1209 for (i = TREE_VEC_LENGTH (binfos)-1; i >= 0; i--)
1210 {
1211 tree basetype = BINFO_TYPE (TREE_VEC_ELT (binfos, i));
1212 if (BINFO_TYPE (binfo) == basetype)
1213 return convert_pointer_to (binfo, expr);
1214 if (binfo_member (BINFO_TYPE (binfo), CLASSTYPE_VBASECLASSES (basetype)))
1215 return convert_pointer_to_vbase (binfo, convert_pointer_to (basetype, expr));
1216 }
1217 my_friendly_abort (6);
1218 /* NOTREACHED */
1219 return NULL_TREE;
1220}
1221\f
1222/* Create an expression whose value is that of EXPR,
1223 converted to type TYPE. The TREE_TYPE of the value
1224 is always TYPE. This function implements all reasonable
1225 conversions; callers should filter out those that are
1226 not permitted by the language being compiled. */
1227
1228tree
1229convert (type, expr)
1230 tree type, expr;
1231{
1232 register tree e = expr;
1233 register enum tree_code code = TREE_CODE (type);
1234
1235 if (type == TREE_TYPE (expr)
1236 || TREE_CODE (expr) == ERROR_MARK)
1237 return expr;
1238 if (TYPE_MAIN_VARIANT (type) == TYPE_MAIN_VARIANT (TREE_TYPE (expr)))
1239 return fold (build1 (NOP_EXPR, type, expr));
1240 if (TREE_CODE (TREE_TYPE (expr)) == ERROR_MARK)
1241 return error_mark_node;
1242 if (TREE_CODE (TREE_TYPE (expr)) == VOID_TYPE)
1243 {
1244 error ("void value not ignored as it ought to be");
1245 return error_mark_node;
1246 }
1247 if (code == VOID_TYPE)
1248 {
1249 /* We're converting to a void type; see if they have an
1250 `operator void'. */
1251 tree rval = build_type_conversion (NOP_EXPR, type, e, 0);
1252 /* If we can convert to void type via a type conversion, do so. */
1253 if (rval)
1254 return rval;
1255 return build1 (CONVERT_EXPR, type, e);
1256 }
1257#if 0
1258 /* This is incorrect. A truncation can't be stripped this way.
1259 Extensions will be stripped by the use of get_unwidened. */
1260 if (TREE_CODE (expr) == NOP_EXPR)
1261 return convert (type, TREE_OPERAND (expr, 0));
1262#endif
1263
1264 /* Just convert to the type of the member. */
1265 if (code == OFFSET_TYPE)
1266 {
1267 type = TREE_TYPE (type);
1268 code = TREE_CODE (type);
1269 }
1270
1271 /* C++ */
1272 if (code == REFERENCE_TYPE)
1273 return fold (convert_to_reference (error_mark_node, type, e, NULL_TREE,
1274 -1, "conversion", -1, LOOKUP_NORMAL));
1275 else if (TREE_CODE (TREE_TYPE (e)) == REFERENCE_TYPE)
1276 e = convert_from_reference (e);
1277
1278 if (code == INTEGER_TYPE || code == ENUMERAL_TYPE)
1279 {
1280 tree intype = TREE_TYPE (expr);
1281 enum tree_code form = TREE_CODE (intype);
1282 /* enum = enum, enum = int, enum = float are all errors. */
1283 if (flag_int_enum_equivalence == 0
1284 && TREE_CODE (type) == ENUMERAL_TYPE
1285 && (form == INTEGER_TYPE || form == REAL_TYPE
1286 || form == ENUMERAL_TYPE))
1287 {
1288 cp_pedwarn ("conversion from `%#T' to `%#T'", intype, type);
1289
1290 if (flag_pedantic_errors)
1291 return error_mark_node;
1292 }
1293 if (form == OFFSET_TYPE)
1294 cp_error_at ("pointer-to-member expression object not composed with type `%D' object",
1295 TYPE_NAME (TYPE_OFFSET_BASETYPE (intype)));
1296 else if (IS_AGGR_TYPE (intype))
1297 {
1298 tree rval;
1299 rval = build_type_conversion (CONVERT_EXPR, type, expr, 1);
1300 if (rval) return rval;
1301 cp_error ("`%#T' used where an `int' was expected", intype);
1302 return error_mark_node;
1303 }
1304 return fold (convert_to_integer (type, e));
1305 }
1306 if (code == POINTER_TYPE)
1307 return fold (cp_convert_to_pointer (type, e));
1308 if (code == REAL_TYPE)
1309 {
1310 if (IS_AGGR_TYPE (TREE_TYPE (e)))
1311 {
1312 tree rval;
1313 rval = build_type_conversion (CONVERT_EXPR, type, e, 1);
1314 if (rval)
1315 return rval;
1316 else
1317 cp_error ("`%#T' used where a floating point value was expected",
1318 TREE_TYPE (e));
1319 }
1320 return fold (convert_to_real (type, e));
1321 }
1322
1323 /* New C++ semantics: since assignment is now based on
1324 memberwise copying, if the rhs type is derived from the
1325 lhs type, then we may still do a conversion. */
1326 if (IS_AGGR_TYPE_CODE (code))
1327 {
1328 tree dtype = TREE_TYPE (e);
1329
1330 if (TREE_CODE (dtype) == REFERENCE_TYPE)
1331 {
1332 e = convert_from_reference (e);
1333 dtype = TREE_TYPE (e);
1334 }
1335 dtype = TYPE_MAIN_VARIANT (dtype);
1336
1337 /* Conversion of object pointers or signature pointers/references
1338 to signature pointers/references. */
1339
1340 if (TYPE_LANG_SPECIFIC (type)
1341 && (IS_SIGNATURE_POINTER (type) || IS_SIGNATURE_REFERENCE (type)))
1342 {
1343 tree constructor = build_signature_pointer_constructor (type, expr);
1344 tree sig_ty = SIGNATURE_TYPE (type);
1345 tree sig_ptr;
1346
1347 if (constructor == error_mark_node)
1348 return error_mark_node;
1349
1350 sig_ptr = get_temp_name (type, 1);
1351 DECL_INITIAL (sig_ptr) = constructor;
1352 CLEAR_SIGNATURE (sig_ty);
1353 finish_decl (sig_ptr, constructor, 0, 0);
1354 SET_SIGNATURE (sig_ty);
1355 TREE_READONLY (sig_ptr) = 1;
1356
1357 return sig_ptr;
1358 }
1359
1360 /* Conversion between aggregate types. New C++ semantics allow
1361 objects of derived type to be cast to objects of base type.
1362 Old semantics only allowed this between pointers.
1363
1364 There may be some ambiguity between using a constructor
1365 vs. using a type conversion operator when both apply. */
1366
1367 else if (IS_AGGR_TYPE (dtype))
1368 {
1369 tree binfo;
1370
1371 tree conversion = TYPE_HAS_CONVERSION (dtype)
1372 ? build_type_conversion (CONVERT_EXPR, type, e, 1) : NULL_TREE;
1373
1374 if (TYPE_HAS_CONSTRUCTOR (type))
1375 {
1376 tree rval = build_method_call (NULL_TREE, constructor_name_full (type),
1377 build_tree_list (NULL_TREE, e),
1378 TYPE_BINFO (type),
1379 conversion ? LOOKUP_NO_CONVERSION : 0);
1380
1381 if (rval != error_mark_node)
1382 {
1383 if (conversion)
1384 {
1385 error ("both constructor and type conversion operator apply");
1386 return error_mark_node;
1387 }
1388 /* call to constructor successful. */
1389 rval = build_cplus_new (type, rval, 0);
1390 return rval;
1391 }
1392 }
1393 /* Type conversion successful/applies. */
1394 if (conversion)
1395 {
1396 if (conversion == error_mark_node)
1397 error ("ambiguous pointer conversion");
1398 return conversion;
1399 }
1400
1401 /* now try normal C++ assignment semantics. */
1402 binfo = TYPE_BINFO (dtype);
1403 if (BINFO_TYPE (binfo) == type
1404 || (binfo = get_binfo (type, dtype, 1)))
1405 {
1406 if (binfo == error_mark_node)
1407 return error_mark_node;
1408 }
1409 if (binfo != NULL_TREE)
1410 {
1411 if (lvalue_p (e))
1412 {
1413 e = build_unary_op (ADDR_EXPR, e, 0);
1414
1415 if (! BINFO_OFFSET_ZEROP (binfo))
1416 e = build (PLUS_EXPR, TYPE_POINTER_TO (type),
1417 e, BINFO_OFFSET (binfo));
1418 return build1 (INDIRECT_REF, type, e);
1419 }
1420
1421 sorry ("addressable aggregates");
1422 return error_mark_node;
1423 }
1424 error ("conversion between incompatible aggregate types requested");
1425 return error_mark_node;
1426 }
1427 /* conversion from non-aggregate to aggregate type requires
1428 constructor. */
1429 else if (TYPE_HAS_CONSTRUCTOR (type))
1430 {
1431 tree rval;
1432 tree init = build_method_call (NULL_TREE, constructor_name_full (type),
1433 build_tree_list (NULL_TREE, e),
1434 TYPE_BINFO (type), LOOKUP_NORMAL);
1435 if (init == error_mark_node)
1436 {
1437 cp_error ("in conversion to type `%T'", type);
1438 return error_mark_node;
1439 }
1440 rval = build_cplus_new (type, init, 0);
1441 return rval;
1442 }
1443 }
1444
1445 /* If TYPE or TREE_TYPE (EXPR) is not on the permanent_obstack,
1446 then the it won't be hashed and hence compare as not equal,
1447 even when it is. */
1448 if (code == ARRAY_TYPE
1449 && TREE_TYPE (TREE_TYPE (expr)) == TREE_TYPE (type)
1450 && index_type_equal (TYPE_DOMAIN (TREE_TYPE (expr)), TYPE_DOMAIN (type)))
1451 return expr;
1452
1453 cp_error ("conversion from `%T' to non-scalar type `%T' requested",
1454 TREE_TYPE (expr), type);
1455 return error_mark_node;
1456}
1457
1458/* Like convert, except permit conversions to take place which
1459 are not normally allowed due to access restrictions
1460 (such as conversion from sub-type to private super-type). */
1461tree
1462convert_force (type, expr)
1463 tree type;
1464 tree expr;
1465{
1466 register tree e = expr;
1467 register enum tree_code code = TREE_CODE (type);
1468
1469 if (code == REFERENCE_TYPE)
1470 return fold (convert_to_reference (0, type, e, NULL_TREE, -1,
39211cd5 1471 "casting", -1, LOOKUP_COMPLAIN));
8d08fdba
MS
1472 else if (TREE_CODE (TREE_TYPE (e)) == REFERENCE_TYPE)
1473 e = convert_from_reference (e);
1474
1475 if (code == POINTER_TYPE)
1476 return fold (convert_to_pointer_force (type, e));
1477
51c184be 1478 /* From typeck.c convert_for_assignment */
8d08fdba
MS
1479 if (((TREE_CODE (TREE_TYPE (e)) == POINTER_TYPE && TREE_CODE (e) == ADDR_EXPR
1480 && TREE_CODE (TREE_TYPE (e)) == POINTER_TYPE
1481 && TREE_CODE (TREE_TYPE (TREE_TYPE (e))) == METHOD_TYPE)
51c184be
MS
1482 || integer_zerop (e)
1483 || TYPE_PTRMEMFUNC_P (TREE_TYPE (e)))
8d08fdba
MS
1484 && TYPE_PTRMEMFUNC_P (type))
1485 {
1486 /* compatible pointer to member functions. */
51c184be 1487 return build_ptrmemfunc (TYPE_PTRMEMFUNC_FN_TYPE (type), e, 1);
8d08fdba
MS
1488 }
1489 {
1490 int old_equiv = flag_int_enum_equivalence;
1491 flag_int_enum_equivalence = 1;
1492 e = convert (type, e);
1493 flag_int_enum_equivalence = old_equiv;
1494 }
1495 return e;
1496}
1497
1498/* Subroutine of build_type_conversion. */
1499static tree
1500build_type_conversion_1 (xtype, basetype, expr, typename, for_sure)
1501 tree xtype, basetype;
1502 tree expr;
1503 tree typename;
1504 int for_sure;
1505{
1506 tree first_arg = expr;
1507 tree rval;
1508 int flags;
1509
1510 if (for_sure == 0)
1511 {
1512 if (! lvalue_p (expr))
1513 first_arg = build1 (NOP_EXPR, TYPE_POINTER_TO (basetype), integer_zero_node);
1514 flags = LOOKUP_PROTECT;
1515 }
1516 else
1517 flags = LOOKUP_NORMAL;
1518
1519 rval = build_method_call (first_arg, constructor_name_full (typename),
1520 NULL_TREE, NULL_TREE, flags);
1521 if (rval == error_mark_node)
1522 {
1523 if (for_sure == 0)
1524 return NULL_TREE;
1525 return error_mark_node;
1526 }
1527 if (first_arg != expr)
1528 {
1529 expr = build_up_reference (build_reference_type (TREE_TYPE (expr)), expr,
1530 LOOKUP_COMPLAIN, 1);
1531 TREE_VALUE (TREE_OPERAND (rval, 1)) = build_unary_op (ADDR_EXPR, expr, 0);
1532 }
1533 if (TREE_CODE (TREE_TYPE (rval)) == REFERENCE_TYPE
1534 && TREE_CODE (xtype) != REFERENCE_TYPE)
1535 rval = default_conversion (rval);
1536
1537 if (warn_cast_qual
1538 && TREE_TYPE (xtype)
1539 && (TREE_READONLY (TREE_TYPE (TREE_TYPE (rval)))
1540 > TREE_READONLY (TREE_TYPE (xtype))))
1541 warning ("user-defined conversion casting away `const'");
700f8a87
MS
1542 rval = convert_for_initialization (NULL_TREE, xtype, rval, flags,
1543 "conversion", NULL_TREE, 0);
1544 if (rval == error_mark_node)
1545 return NULL_TREE;
1546 return rval;
8d08fdba
MS
1547}
1548
1549/* Convert an aggregate EXPR to type XTYPE. If a conversion
1550 exists, return the attempted conversion. This may
1551 return ERROR_MARK_NODE if the conversion is not
1552 allowed (references private members, etc).
1553 If no conversion exists, NULL_TREE is returned.
1554
1555 If (FOR_SURE & 1) is non-zero, then we allow this type conversion
1556 to take place immediately. Otherwise, we build a SAVE_EXPR
1557 which can be evaluated if the results are ever needed.
1558
1559 If FOR_SURE >= 2, then we only look for exact conversions.
1560
1561 TYPE may be a reference type, in which case we first look
1562 for something that will convert to a reference type. If
1563 that fails, we will try to look for something of the
1564 reference's target type, and then return a reference to that. */
1565tree
1566build_type_conversion (code, xtype, expr, for_sure)
1567 enum tree_code code;
1568 tree xtype, expr;
1569 int for_sure;
1570{
1571 /* C++: check to see if we can convert this aggregate type
1572 into the required scalar type. */
1573 tree type, type_default;
1574 tree typename = build_typename_overload (xtype), *typenames;
1575 int n_variants = 0;
1576 tree basetype, save_basetype;
1577 tree rval;
1578 int exact_conversion = for_sure >= 2;
1579 for_sure &= 1;
1580
1581 if (expr == error_mark_node)
1582 return error_mark_node;
1583
1584 basetype = TREE_TYPE (expr);
1585 if (TREE_CODE (basetype) == REFERENCE_TYPE)
1586 basetype = TREE_TYPE (basetype);
1587
1588 basetype = TYPE_MAIN_VARIANT (basetype);
1589 if (! TYPE_LANG_SPECIFIC (basetype) || ! TYPE_HAS_CONVERSION (basetype))
1590 return NULL_TREE;
1591
1592 if (TREE_CODE (xtype) == POINTER_TYPE
1593 || TREE_CODE (xtype) == REFERENCE_TYPE)
1594 {
1595 /* Prepare to match a variant of this type. */
1596 type = TYPE_MAIN_VARIANT (TREE_TYPE (xtype));
1597 for (n_variants = 0; type; type = TYPE_NEXT_VARIANT (type))
1598 n_variants++;
1599 typenames = (tree *)alloca (n_variants * sizeof (tree));
1600 for (n_variants = 0, type = TYPE_MAIN_VARIANT (TREE_TYPE (xtype));
1601 type; n_variants++, type = TYPE_NEXT_VARIANT (type))
1602 {
1603 if (type == TREE_TYPE (xtype))
1604 typenames[n_variants] = typename;
1605 else if (TREE_CODE (xtype) == POINTER_TYPE)
1606 typenames[n_variants] = build_typename_overload (build_pointer_type (type));
1607 else
1608 typenames[n_variants] = build_typename_overload (build_reference_type (type));
1609 }
1610 }
1611
1612 save_basetype = basetype;
1613 type = xtype;
1614
1615 while (TYPE_HAS_CONVERSION (basetype))
1616 {
1617 int i;
1618 if (lookup_fnfields (TYPE_BINFO (basetype), typename, 0))
1619 return build_type_conversion_1 (xtype, basetype, expr, typename, for_sure);
1620 for (i = 0; i < n_variants; i++)
1621 if (typenames[i] != typename
1622 && lookup_fnfields (TYPE_BINFO (basetype), typenames[i], 0))
1623 return build_type_conversion_1 (xtype, basetype, expr, typenames[i], for_sure);
1624
1625 if (TYPE_BINFO_BASETYPES (basetype))
1626 basetype = TYPE_BINFO_BASETYPE (basetype, 0);
1627 else
1628 break;
1629 }
1630
1631 if (TREE_CODE (type) == REFERENCE_TYPE)
1632 {
1633 tree first_arg = expr;
1634 type = TYPE_MAIN_VARIANT (TREE_TYPE (type));
1635 basetype = save_basetype;
1636
1637 /* May need to build a temporary for this. */
1638 while (TYPE_HAS_CONVERSION (basetype))
1639 {
1640 if (lookup_fnfields (TYPE_BINFO (basetype), typename, 0))
1641 {
1642 int flags;
1643
1644 if (for_sure == 0)
1645 {
1646 if (! lvalue_p (expr))
1647 first_arg = build1 (NOP_EXPR, TYPE_POINTER_TO (basetype), integer_zero_node);
1648 flags = LOOKUP_PROTECT;
1649 }
1650 else
1651 flags = LOOKUP_NORMAL;
1652 rval = build_method_call (first_arg, constructor_name_full (typename),
1653 NULL_TREE, NULL_TREE, flags);
1654 if (rval == error_mark_node)
1655 {
1656 if (for_sure == 0)
1657 return NULL_TREE;
1658 return error_mark_node;
1659 }
1660 TREE_VALUE (TREE_OPERAND (rval, 1)) = expr;
1661
1662 if (IS_AGGR_TYPE (type))
1663 {
1664 tree init = build_method_call (NULL_TREE,
1665 constructor_name_full (type),
1666 build_tree_list (NULL_TREE, rval), NULL_TREE, LOOKUP_NORMAL);
1667 tree temp = build_cplus_new (type, init, 1);
1668 return build_up_reference (TYPE_REFERENCE_TO (type), temp,
1669 LOOKUP_COMPLAIN, 1);
1670 }
1671 return convert (xtype, rval);
1672 }
1673 if (TYPE_BINFO_BASETYPES (basetype))
1674 basetype = TYPE_BINFO_BASETYPE (basetype, 0);
1675 else
1676 break;
1677 }
1678 /* No free conversions for reference types, right?. */
1679 return NULL_TREE;
1680 }
1681
1682 if (exact_conversion)
1683 return NULL_TREE;
1684
1685 /* No perfect match found, try default. */
1686#if 0 /* This is wrong; there is no standard conversion from void* to
1687 anything. -jason */
1688 if (code == CONVERT_EXPR && TREE_CODE (type) == POINTER_TYPE)
1689 type_default = ptr_type_node;
1690 else
1691#endif
1692 if (type == void_type_node)
1693 return NULL_TREE;
1694 else
1695 {
1696 tree tmp = default_conversion (build1 (NOP_EXPR, type, integer_zero_node));
1697 if (tmp == error_mark_node)
1698 return NULL_TREE;
1699 type_default = TREE_TYPE (tmp);
1700 }
1701
1702 basetype = save_basetype;
1703
1704 if (type_default != type)
1705 {
1706 type = type_default;
1707 typename = build_typename_overload (type);
1708
1709 while (TYPE_HAS_CONVERSION (basetype))
1710 {
1711 if (lookup_fnfields (TYPE_BINFO (basetype), typename, 0))
1712 return build_type_conversion_1 (xtype, basetype, expr, typename, for_sure);
1713 if (TYPE_BINFO_BASETYPES (basetype))
1714 basetype = TYPE_BINFO_BASETYPE (basetype, 0);
1715 else
1716 break;
1717 }
1718 }
1719
1720 try_pointer:
1721
39211cd5 1722 if (TREE_CODE (type) == POINTER_TYPE && TYPE_READONLY (TREE_TYPE (type)))
8d08fdba 1723 {
39211cd5
MS
1724 /* Try converting to some other const pointer type and then using
1725 standard conversions. */
8d08fdba
MS
1726
1727 while (TYPE_HAS_CONVERSION (basetype))
1728 {
39211cd5 1729 if (CLASSTYPE_CONVERSION (basetype, constptr_conv) != 0)
8d08fdba 1730 {
39211cd5 1731 if (CLASSTYPE_CONVERSION (basetype, constptr_conv) == error_mark_node)
8d08fdba 1732 return error_mark_node;
39211cd5 1733 typename = DECL_NAME (CLASSTYPE_CONVERSION (basetype, constptr_conv));
8d08fdba
MS
1734 return build_type_conversion_1 (xtype, basetype, expr, typename, for_sure);
1735 }
1736 if (TYPE_BINFO_BASETYPES (basetype))
1737 basetype = TYPE_BINFO_BASETYPE (basetype, 0);
1738 else
1739 break;
1740 }
1741 }
39211cd5 1742 if (TREE_CODE (type) == POINTER_TYPE)
8d08fdba 1743 {
39211cd5
MS
1744 /* Try converting to some other pointer type and then using standard
1745 conversions. */
8d08fdba
MS
1746
1747 while (TYPE_HAS_CONVERSION (basetype))
1748 {
39211cd5 1749 if (CLASSTYPE_CONVERSION (basetype, ptr_conv) != 0)
8d08fdba 1750 {
39211cd5 1751 if (CLASSTYPE_CONVERSION (basetype, ptr_conv) == error_mark_node)
8d08fdba 1752 return error_mark_node;
39211cd5 1753 typename = DECL_NAME (CLASSTYPE_CONVERSION (basetype, ptr_conv));
8d08fdba
MS
1754 return build_type_conversion_1 (xtype, basetype, expr, typename, for_sure);
1755 }
1756 if (TYPE_BINFO_BASETYPES (basetype))
1757 basetype = TYPE_BINFO_BASETYPE (basetype, 0);
1758 else
1759 break;
1760 }
1761 }
39211cd5 1762
8d08fdba
MS
1763 /* Use the longer or shorter conversion that is appropriate. Have
1764 to check against 0 because the conversion may come from a baseclass. */
1765 if (TREE_CODE (type) == INTEGER_TYPE
1766 && TYPE_HAS_INT_CONVERSION (basetype)
1767 && CLASSTYPE_CONVERSION (basetype, int_conv) != 0
1768 && CLASSTYPE_CONVERSION (basetype, int_conv) != error_mark_node)
1769 {
1770 typename = DECL_NAME (CLASSTYPE_CONVERSION (basetype, int_conv));
1771 return build_type_conversion_1 (xtype, basetype, expr, typename, for_sure);
1772 }
1773
1774 if (TREE_CODE (type) == REAL_TYPE
1775 && TYPE_HAS_REAL_CONVERSION (basetype)
1776 && CLASSTYPE_CONVERSION (basetype, real_conv) != 0
1777 && CLASSTYPE_CONVERSION (basetype, real_conv) != error_mark_node)
1778 {
1779 /* Only accept using an operator double() if there isn't a conflicting
1780 operator int(). */
1781 if (flag_ansi_overloading && TYPE_HAS_INT_CONVERSION (basetype))
1782 {
1783 error ("two possible conversions for type `%s'",
1784 TYPE_NAME_STRING (type));
1785 return error_mark_node;
1786 }
1787
1788 typename = DECL_NAME (CLASSTYPE_CONVERSION (basetype, real_conv));
1789 return build_type_conversion_1 (xtype, basetype, expr, typename, for_sure);
1790 }
1791
1792 /* THIS IS A KLUDGE. */
1793 if (TREE_CODE (type) != POINTER_TYPE
1794 && (code == TRUTH_ANDIF_EXPR
1795 || code == TRUTH_ORIF_EXPR
1796 || code == TRUTH_NOT_EXPR))
1797 {
1798 /* Here's when we can convert to a pointer. */
1799 type = ptr_type_node;
1800 goto try_pointer;
1801 }
1802
1803 /* THESE ARE TOTAL KLUDGES. */
1804 /* Default promotion yields no new alternatives, try
1805 conversions which are anti-default, such as
1806
1807 double -> float or int -> unsigned or unsigned -> long
1808
1809 */
1810 if (type_default == type
1811 && (TREE_CODE (type) == INTEGER_TYPE || TREE_CODE (type) == REAL_TYPE))
1812 {
1813 int not_again = 0;
1814
1815 if (type == double_type_node)
1816 typename = build_typename_overload (float_type_node);
1817 else if (type == integer_type_node)
1818 typename = build_typename_overload (unsigned_type_node);
1819 else if (type == unsigned_type_node)
1820 typename = build_typename_overload (long_integer_type_node);
1821
1822 again:
1823 basetype = save_basetype;
1824 while (TYPE_HAS_CONVERSION (basetype))
1825 {
1826 if (lookup_fnfields (TYPE_BINFO (basetype), typename, 0))
1827 return build_type_conversion_1 (xtype, basetype, expr, typename, for_sure);
1828 if (TYPE_BINFO_BASETYPES (basetype))
1829 basetype = TYPE_BINFO_BASETYPE (basetype, 0);
1830 else
1831 break;
1832 }
1833 if (! not_again)
1834 {
1835 if (type == integer_type_node)
1836 {
1837 typename = build_typename_overload (long_integer_type_node);
1838 not_again = 1;
1839 goto again;
1840 }
1841 else
1842 {
1843 typename = build_typename_overload (integer_type_node);
1844 not_again = 1;
1845 goto again;
1846 }
1847 }
1848 }
1849
1850 /* Now, try C promotions...
1851
1852 float -> int
1853 int -> float, void *
1854 void * -> int
1855
1856 Truthvalue conversions let us try to convert
1857 to pointer if we were going for int, and to int
1858 if we were looking for pointer. */
1859
1860 basetype = save_basetype;
1861 if (TREE_CODE (type) == REAL_TYPE
1862 || (TREE_CODE (type) == POINTER_TYPE
1863 && (code == TRUTH_ANDIF_EXPR
1864 || code == TRUTH_ORIF_EXPR
1865 || code == TRUTH_NOT_EXPR)))
1866 type = integer_type_node;
1867 else if (TREE_CODE (type) == INTEGER_TYPE)
1868 if (TYPE_HAS_REAL_CONVERSION (basetype))
1869 type = double_type_node;
1870 else
1871 return NULL_TREE;
1872 else
1873 return NULL_TREE;
1874
1875 typename = build_typename_overload (type);
1876 while (TYPE_HAS_CONVERSION (basetype))
1877 {
1878 if (lookup_fnfields (TYPE_BINFO (basetype), typename, 0))
1879 {
1880 rval = build_type_conversion_1 (xtype, basetype, expr, typename, for_sure);
1881 return rval;
1882 }
1883 if (TYPE_BINFO_BASETYPES (basetype))
1884 basetype = TYPE_BINFO_BASETYPE (basetype, 0);
1885 else
1886 break;
1887 }
1888
1889 return NULL_TREE;
1890}
1891
1892/* Must convert two aggregate types to non-aggregate type.
1893 Attempts to find a non-ambiguous, "best" type conversion.
1894
1895 Return 1 on success, 0 on failure.
1896
1897 @@ What are the real semantics of this supposed to be??? */
1898int
1899build_default_binary_type_conversion (code, arg1, arg2)
1900 enum tree_code code;
1901 tree *arg1, *arg2;
1902{
1903 tree type1 = TREE_TYPE (*arg1);
1904 tree type2 = TREE_TYPE (*arg2);
1905
1906 if (TREE_CODE (type1) == REFERENCE_TYPE
1907 || TREE_CODE (type1) == POINTER_TYPE)
1908 type1 = TREE_TYPE (type1);
1909 if (TREE_CODE (type2) == REFERENCE_TYPE
1910 || TREE_CODE (type2) == POINTER_TYPE)
1911 type2 = TREE_TYPE (type2);
1912
1913 if (TREE_CODE (TYPE_NAME (type1)) != TYPE_DECL)
1914 {
1915 tree decl = typedecl_for_tag (type1);
1916 if (decl)
1917 error ("type conversion nonexistent for type `%s'",
1918 IDENTIFIER_POINTER (DECL_NAME (decl)));
1919 else
1920 error ("type conversion nonexistent for non-C++ type");
1921 return 0;
1922 }
1923 if (TREE_CODE (TYPE_NAME (type2)) != TYPE_DECL)
1924 {
1925 tree decl = typedecl_for_tag (type2);
1926 if (decl)
1927 error ("type conversion nonexistent for type `%s'",
1928 IDENTIFIER_POINTER (decl));
1929 else
1930 error ("type conversion nonexistent for non-C++ type");
1931 return 0;
1932 }
1933
1934 if (!IS_AGGR_TYPE (type1) || !TYPE_HAS_CONVERSION (type1))
1935 {
1936 if (!IS_AGGR_TYPE (type2) || !TYPE_HAS_CONVERSION (type2))
1937 cp_error ("no conversion from `%T' and `%T' to types with default `%O' ",
1938 type1, type2, code);
1939 else
1940 cp_error ("no conversion from `%T' to type with default `%O'",
1941 type1, code);
1942 return 0;
1943 }
1944 else if (!IS_AGGR_TYPE (type2) || !TYPE_HAS_CONVERSION (type2))
1945 {
1946 cp_error ("no conversion from `%T' to type with default `%O'",
1947 type2, code);
1948 return 0;
1949 }
1950
1951 if (TYPE_HAS_INT_CONVERSION (type1) && TYPE_HAS_REAL_CONVERSION (type1))
1952 cp_warning ("ambiguous type conversion for type `%T', defaulting to int",
1953 type1);
1954 if (TYPE_HAS_INT_CONVERSION (type1))
1955 {
1956 *arg1 = build_type_conversion (code, integer_type_node, *arg1, 1);
1957 *arg2 = build_type_conversion (code, integer_type_node, *arg2, 1);
1958 }
1959 else if (TYPE_HAS_REAL_CONVERSION (type1))
1960 {
1961 *arg1 = build_type_conversion (code, double_type_node, *arg1, 1);
1962 *arg2 = build_type_conversion (code, double_type_node, *arg2, 1);
1963 }
1964 else
1965 {
1966 *arg1 = build_type_conversion (code, ptr_type_node, *arg1, 1);
1967 if (*arg1 == error_mark_node)
1968 error ("ambiguous pointer conversion");
1969 *arg2 = build_type_conversion (code, ptr_type_node, *arg2, 1);
1970 if (*arg1 != error_mark_node && *arg2 == error_mark_node)
1971 error ("ambiguous pointer conversion");
1972 }
1973 if (*arg1 == 0)
1974 {
1975 if (*arg2 == 0 && type1 != type2)
1976 cp_error ("default type conversion for types `%T' and `%T' failed",
1977 type1, type2);
1978 else
1979 cp_error ("default type conversion for type `%T' failed", type1);
1980 return 0;
1981 }
1982 else if (*arg2 == 0)
1983 {
1984 cp_error ("default type conversion for type `%T' failed", type2);
1985 return 0;
1986 }
1987 return 1;
1988}
1989
1990/* Must convert two aggregate types to non-aggregate type.
1991 Attempts to find a non-ambiguous, "best" type conversion.
1992
1993 Return 1 on success, 0 on failure.
1994
1995 The type of the argument is expected to be of aggregate type here.
1996
1997 @@ What are the real semantics of this supposed to be??? */
1998int
1999build_default_unary_type_conversion (code, arg)
2000 enum tree_code code;
2001 tree *arg;
2002{
2003 tree type = TREE_TYPE (*arg);
2004 tree id = TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
2005 ? TYPE_IDENTIFIER (type) : TYPE_NAME (type);
2006 char *name = IDENTIFIER_POINTER (id);
2007
2008 if (! TYPE_HAS_CONVERSION (type))
2009 {
2010 error ("type conversion required for type `%s'", name);
2011 return 0;
2012 }
2013
2014 if (TYPE_HAS_INT_CONVERSION (type) && TYPE_HAS_REAL_CONVERSION (type))
2015 warning ("ambiguous type conversion for type `%s', defaulting to int",
2016 name);
2017 if (TYPE_HAS_INT_CONVERSION (type))
2018 *arg = build_type_conversion (code, integer_type_node, *arg, 1);
2019 else if (TYPE_HAS_REAL_CONVERSION (type))
2020 *arg = build_type_conversion (code, double_type_node, *arg, 1);
2021 else
2022 {
2023 *arg = build_type_conversion (code, ptr_type_node, *arg, 1);
2024 if (*arg == error_mark_node)
2025 error ("ambiguous pointer conversion");
2026 }
2027 if (*arg == NULL_TREE)
2028 {
2029 error ("default type conversion for type `%s' failed", name);
2030 return 0;
2031 }
2032 return 1;
2033}
39211cd5
MS
2034
2035/* Implements integral promotion (4.1) and float->double promotion. */
2036tree
2037type_promotes_to (type)
2038 tree type;
2039{
2040 int constp = TYPE_READONLY (type);
2041 int volatilep = TYPE_VOLATILE (type);
2042 type = TYPE_MAIN_VARIANT (type);
2043
2044 /* Normally convert enums to int,
2045 but convert wide enums to something wider. */
2046 if (TREE_CODE (type) == ENUMERAL_TYPE
2047 || type == wchar_type_node)
2048 type = type_for_size (MAX (TYPE_PRECISION (type),
2049 TYPE_PRECISION (integer_type_node)),
2050 ((flag_traditional
2051 || (TYPE_PRECISION (type)
2052 >= TYPE_PRECISION (integer_type_node)))
2053 && TREE_UNSIGNED (type)));
2054 else if (C_PROMOTING_INTEGER_TYPE_P (type))
2055 {
2056 /* Traditionally, unsignedness is preserved in default promotions.
2057 Otherwise, retain unsignedness if really not getting bigger. */
2058 if (TREE_UNSIGNED (type)
2059 && (flag_traditional
2060 || TYPE_PRECISION (type) == TYPE_PRECISION (integer_type_node)))
2061 type = unsigned_type_node;
2062 else
2063 type = integer_type_node;
2064 }
2065 else if (type == float_type_node)
2066 type = double_type_node;
2067
2068 return build_type_variant (type, constp, volatilep);
2069}
This page took 0.263689 seconds and 5 git commands to generate.