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