]> git.ipfire.org Git - thirdparty/gcc.git/blame - gcc/cp/cvt.c
New test from Joern.
[thirdparty/gcc.git] / gcc / cp / cvt.c
CommitLineData
471086d6 1/* Language-level data type conversion for GNU C++.
50a72189 2 Copyright (C) 1987, 88, 92, 93, 94, 95, 1996 Free Software Foundation, Inc.
471086d6 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
c58d4270 19the Free Software Foundation, 59 Temple Place - Suite 330,
20Boston, MA 02111-1307, USA. */
471086d6 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"
b3ef7553 29#include "system.h"
471086d6 30#include "tree.h"
31#include "flags.h"
32#include "cp-tree.h"
471086d6 33#include "convert.h"
34
860740a7 35extern tree static_aggregates;
96624a9e 36
71ccdfff 37static tree cp_convert_to_pointer PROTO((tree, tree));
38static tree convert_to_pointer_force PROTO((tree, tree));
39static tree build_up_reference PROTO((tree, tree, int, int));
71ccdfff 40
471086d6 41/* Change of width--truncation and extension of integers or reals--
42 is represented with NOP_EXPR. Proper functioning of many things
43 assumes that no other conversions can be NOP_EXPRs.
44
45 Conversion between integer and pointer is represented with CONVERT_EXPR.
46 Converting integer to real uses FLOAT_EXPR
47 and real to integer uses FIX_TRUNC_EXPR.
48
49 Here is a list of all the functions that assume that widening and
50 narrowing is always done with a NOP_EXPR:
51 In convert.c, convert_to_integer.
52 In c-typeck.c, build_binary_op_nodefault (boolean ops),
53 and truthvalue_conversion.
54 In expr.c: expand_expr, for operands of a MULT_EXPR.
55 In fold-const.c: fold.
56 In tree.c: get_narrower and get_unwidened.
57
58 C++: in multiple-inheritance, converting between pointers may involve
59 adjusting them by a delta stored within the class definition. */
60\f
61/* Subroutines of `convert'. */
62
471086d6 63/* if converting pointer to pointer
64 if dealing with classes, check for derived->base or vice versa
65 else if dealing with method pointers, delegate
66 else convert blindly
67 else if converting class, pass off to build_type_conversion
68 else try C-style pointer conversion */
96624a9e 69
471086d6 70static tree
71cp_convert_to_pointer (type, expr)
72 tree type, expr;
73{
74 register tree intype = TREE_TYPE (expr);
74002e1d 75 register enum tree_code form;
f82eeb87 76 tree rval;
74002e1d 77
96624a9e 78 if (IS_AGGR_TYPE (intype))
79 {
96624a9e 80 intype = complete_type (intype);
81 if (TYPE_SIZE (intype) == NULL_TREE)
82 {
83 cp_error ("can't convert from incomplete type `%T' to `%T'",
84 intype, type);
85 return error_mark_node;
86 }
87
88 rval = build_type_conversion (CONVERT_EXPR, type, expr, 1);
89 if (rval)
90 {
91 if (rval == error_mark_node)
92 cp_error ("conversion of `%E' from `%T' to `%T' is ambiguous",
93 expr, intype, type);
94 return rval;
95 }
96 }
97
74002e1d 98 if (TYPE_PTRMEMFUNC_P (type))
99 type = TYPE_PTRMEMFUNC_FN_TYPE (type);
74002e1d 100
860740a7 101 /* Handle anachronistic conversions from (::*)() to cv void* or (*)(). */
38281c46 102 if (TREE_CODE (type) == POINTER_TYPE
103 && (TREE_CODE (TREE_TYPE (type)) == FUNCTION_TYPE
860740a7 104 || TYPE_MAIN_VARIANT (TREE_TYPE (type)) == void_type_node))
38281c46 105 {
106 /* Allow an implicit this pointer for pointer to member
107 functions. */
860740a7 108 if (TYPE_PTRMEMFUNC_P (intype))
38281c46 109 {
110 tree decl, basebinfo;
860740a7 111 tree fntype = TREE_TYPE (TYPE_PTRMEMFUNC_FN_TYPE (intype));
38281c46 112 tree t = TYPE_METHOD_BASETYPE (fntype);
113
114 if (current_class_type == 0
115 || get_base_distance (t, current_class_type, 0, &basebinfo)
116 == -1)
117 {
118 decl = build1 (NOP_EXPR, t, error_mark_node);
119 }
120 else if (current_class_ptr == 0)
121 decl = build1 (NOP_EXPR, t, error_mark_node);
122 else
123 decl = current_class_ref;
124
125 expr = build (OFFSET_REF, fntype, decl, expr);
126 }
127
128 if (TREE_CODE (expr) == OFFSET_REF
129 && TREE_CODE (TREE_TYPE (expr)) == METHOD_TYPE)
130 expr = resolve_offset_ref (expr);
131 if (TREE_CODE (TREE_TYPE (expr)) == METHOD_TYPE)
132 expr = build_addr_func (expr);
133 if (TREE_CODE (TREE_TYPE (expr)) == POINTER_TYPE)
134 {
135 if (TREE_CODE (TREE_TYPE (TREE_TYPE (expr))) == METHOD_TYPE)
136 if (pedantic || warn_pmf2ptr)
137 cp_pedwarn ("converting from `%T' to `%T'", TREE_TYPE (expr),
138 type);
139 return build1 (NOP_EXPR, type, expr);
140 }
141 intype = TREE_TYPE (expr);
142 }
143
860740a7 144 if (TYPE_PTRMEMFUNC_P (intype))
145 intype = TYPE_PTRMEMFUNC_FN_TYPE (intype);
146
74002e1d 147 form = TREE_CODE (intype);
148
1a3f833b 149 if (form == POINTER_TYPE || form == REFERENCE_TYPE)
471086d6 150 {
151 intype = TYPE_MAIN_VARIANT (intype);
152
153 if (TYPE_MAIN_VARIANT (type) != intype
154 && TREE_CODE (TREE_TYPE (type)) == RECORD_TYPE
bccffb3a 155 && IS_AGGR_TYPE (TREE_TYPE (type))
156 && IS_AGGR_TYPE (TREE_TYPE (intype))
471086d6 157 && TREE_CODE (TREE_TYPE (intype)) == RECORD_TYPE)
158 {
159 enum tree_code code = PLUS_EXPR;
160 tree binfo = get_binfo (TREE_TYPE (type), TREE_TYPE (intype), 1);
161 if (binfo == error_mark_node)
162 return error_mark_node;
163 if (binfo == NULL_TREE)
164 {
165 binfo = get_binfo (TREE_TYPE (intype), TREE_TYPE (type), 1);
166 if (binfo == error_mark_node)
167 return error_mark_node;
168 code = MINUS_EXPR;
169 }
170 if (binfo)
171 {
172 if (TYPE_USES_VIRTUAL_BASECLASSES (TREE_TYPE (type))
173 || TYPE_USES_VIRTUAL_BASECLASSES (TREE_TYPE (intype))
174 || ! BINFO_OFFSET_ZEROP (binfo))
175 {
176 /* Need to get the path we took. */
177 tree path;
178
179 if (code == PLUS_EXPR)
985e9ee0 180 get_base_distance (TREE_TYPE (type), TREE_TYPE (intype),
181 0, &path);
471086d6 182 else
985e9ee0 183 get_base_distance (TREE_TYPE (intype), TREE_TYPE (type),
184 0, &path);
471086d6 185 return build_vbase_path (code, type, expr, path, 0);
186 }
187 }
188 }
74002e1d 189 if (TREE_CODE (TREE_TYPE (intype)) == METHOD_TYPE
471086d6 190 && TREE_CODE (type) == POINTER_TYPE
74002e1d 191 && TREE_CODE (TREE_TYPE (type)) == METHOD_TYPE)
985e9ee0 192 return build_ptrmemfunc (type, expr, 1);
471086d6 193
bea7d742 194 if (TREE_CODE (TREE_TYPE (type)) == OFFSET_TYPE
195 && TREE_CODE (TREE_TYPE (intype)) == OFFSET_TYPE)
196 {
197 tree b1 = TYPE_OFFSET_BASETYPE (TREE_TYPE (type));
198 tree b2 = TYPE_OFFSET_BASETYPE (TREE_TYPE (intype));
fc9fc8d8 199 tree binfo = get_binfo (b2, b1, 1);
200 enum tree_code code = PLUS_EXPR;
201
bea7d742 202 if (binfo == NULL_TREE)
fc9fc8d8 203 {
204 binfo = get_binfo (b1, b2, 1);
205 code = MINUS_EXPR;
206 }
207
bea7d742 208 if (binfo == error_mark_node)
209 return error_mark_node;
fc9fc8d8 210 if (binfo && ! TREE_VIA_VIRTUAL (binfo))
211 expr = size_binop (code, expr, BINFO_OFFSET (binfo));
bea7d742 212 }
213
ce28ee2e 214 if (TREE_CODE (TREE_TYPE (intype)) == METHOD_TYPE
215 || (TREE_CODE (type) == POINTER_TYPE
216 && TREE_CODE (TREE_TYPE (type)) == METHOD_TYPE))
217 {
218 cp_error ("cannot convert `%E' from type `%T' to type `%T'",
219 expr, intype, type);
220 return error_mark_node;
221 }
222
f82eeb87 223 rval = build1 (NOP_EXPR, type, expr);
224 TREE_CONSTANT (rval) = TREE_CONSTANT (expr);
225 return rval;
471086d6 226 }
227
228 my_friendly_assert (form != OFFSET_TYPE, 186);
229
230 if (TYPE_LANG_SPECIFIC (intype)
231 && (IS_SIGNATURE_POINTER (intype) || IS_SIGNATURE_REFERENCE (intype)))
232 return convert_to_pointer (type, build_optr_ref (expr));
233
471086d6 234 if (integer_zerop (expr))
235 {
11066dd5 236 if (TREE_CODE (TREE_TYPE (type)) == METHOD_TYPE)
237 return build_ptrmemfunc (type, expr, 0);
471086d6 238 expr = build_int_2 (0, 0);
239 TREE_TYPE (expr) = type;
240 return expr;
241 }
242
bb0726a1 243 if (INTEGRAL_CODE_P (form))
471086d6 244 {
245 if (type_precision (intype) == POINTER_SIZE)
246 return build1 (CONVERT_EXPR, type, expr);
c4a8ac95 247 expr = cp_convert (type_for_size (POINTER_SIZE, 0), expr);
471086d6 248 /* Modes may be different but sizes should be the same. */
249 if (GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (expr)))
250 != GET_MODE_SIZE (TYPE_MODE (type)))
251 /* There is supposed to be some integral type
252 that is the same width as a pointer. */
253 abort ();
254 return convert_to_pointer (type, expr);
255 }
256
257 cp_error ("cannot convert `%E' from type `%T' to type `%T'",
258 expr, intype, type);
259 return error_mark_node;
260}
261
262/* Like convert, except permit conversions to take place which
263 are not normally allowed due to access restrictions
264 (such as conversion from sub-type to private super-type). */
96624a9e 265
471086d6 266static tree
267convert_to_pointer_force (type, expr)
268 tree type, expr;
269{
270 register tree intype = TREE_TYPE (expr);
271 register enum tree_code form = TREE_CODE (intype);
272
273 if (integer_zerop (expr))
274 {
471086d6 275 expr = build_int_2 (0, 0);
276 TREE_TYPE (expr) = type;
277 return expr;
278 }
279
280 /* Convert signature pointer/reference to `void *' first. */
281 if (form == RECORD_TYPE
282 && (IS_SIGNATURE_POINTER (intype) || IS_SIGNATURE_REFERENCE (intype)))
283 {
284 expr = build_optr_ref (expr);
285 intype = TREE_TYPE (expr);
286 form = TREE_CODE (intype);
287 }
288
289 if (form == POINTER_TYPE)
290 {
291 intype = TYPE_MAIN_VARIANT (intype);
292
293 if (TYPE_MAIN_VARIANT (type) != intype
294 && TREE_CODE (TREE_TYPE (type)) == RECORD_TYPE
bccffb3a 295 && IS_AGGR_TYPE (TREE_TYPE (type))
296 && IS_AGGR_TYPE (TREE_TYPE (intype))
471086d6 297 && TREE_CODE (TREE_TYPE (intype)) == RECORD_TYPE)
298 {
299 enum tree_code code = PLUS_EXPR;
300 tree path;
301 int distance = get_base_distance (TREE_TYPE (type),
302 TREE_TYPE (intype), 0, &path);
303 if (distance == -2)
304 {
305 ambig:
b0df6589 306 cp_error ("type `%T' is ambiguous baseclass of `%s'",
307 TREE_TYPE (type),
308 TYPE_NAME_STRING (TREE_TYPE (intype)));
471086d6 309 return error_mark_node;
310 }
311 if (distance == -1)
312 {
313 distance = get_base_distance (TREE_TYPE (intype),
314 TREE_TYPE (type), 0, &path);
315 if (distance == -2)
316 goto ambig;
317 if (distance < 0)
318 /* Doesn't need any special help from us. */
319 return build1 (NOP_EXPR, type, expr);
320
321 code = MINUS_EXPR;
322 }
323 return build_vbase_path (code, type, expr, path, 0);
324 }
471086d6 325 }
326
327 return cp_convert_to_pointer (type, expr);
328}
329
330/* We are passing something to a function which requires a reference.
331 The type we are interested in is in TYPE. The initial
332 value we have to begin with is in ARG.
333
334 FLAGS controls how we manage access checking.
c76251c1 335 DIRECT_BIND in FLAGS controls how any temporaries are generated. */
96624a9e 336
471086d6 337static tree
338build_up_reference (type, arg, flags, checkconst)
339 tree type, arg;
340 int flags, checkconst;
341{
c76251c1 342 tree rval;
0543e7a9 343 tree argtype = TREE_TYPE (arg);
471086d6 344 tree target_type = TREE_TYPE (type);
471086d6 345
346 my_friendly_assert (TREE_CODE (type) == REFERENCE_TYPE, 187);
471086d6 347
c76251c1 348 if ((flags & DIRECT_BIND) && ! real_lvalue_p (arg))
471086d6 349 {
c76251c1 350 tree targ = arg;
38281c46 351 if (toplevel_bindings_p ())
c76251c1 352 arg = get_temp_name (argtype, 1);
471086d6 353 else
354 {
38281c46 355 arg = pushdecl (build_decl (VAR_DECL, NULL_TREE, argtype));
356 DECL_ARTIFICIAL (arg) = 1;
1ad432f2 357 }
38281c46 358 DECL_INITIAL (arg) = targ;
359 cp_finish_decl (arg, targ, NULL_TREE, 0, LOOKUP_ONLYCONVERTING);
1ad432f2 360 }
c76251c1 361 else if (!(flags & DIRECT_BIND) && ! lvalue_p (arg))
1ad432f2 362 {
38281c46 363 tree slot = build_decl (VAR_DECL, NULL_TREE, argtype);
364 arg = build (TARGET_EXPR, argtype, slot, arg, NULL_TREE, NULL_TREE);
0f76a66d 365 TREE_SIDE_EFFECTS (arg) = 1;
471086d6 366 }
1ad432f2 367
b0df6589 368 /* If we had a way to wrap this up, and say, if we ever needed it's
369 address, transform all occurrences of the register, into a memory
370 reference we could win better. */
c76251c1 371 rval = build_unary_op (ADDR_EXPR, arg, 1);
6686e84d 372 if ((flags & LOOKUP_PROTECT)
373 && TYPE_MAIN_VARIANT (argtype) != TYPE_MAIN_VARIANT (target_type)
374 && IS_AGGR_TYPE (argtype)
375 && IS_AGGR_TYPE (target_type))
376 {
377 /* We go through get_binfo for the access control. */
378 tree binfo = get_binfo (target_type, argtype, 1);
379 if (binfo == error_mark_node)
380 return error_mark_node;
381 if (binfo == NULL_TREE)
382 return error_not_base_type (target_type, argtype);
383 rval = convert_pointer_to_real (binfo, rval);
384 }
c76251c1 385 else
386 rval
387 = convert_to_pointer_force (build_pointer_type (target_type), rval);
6686e84d 388 rval = build1 (NOP_EXPR, type, rval);
c76251c1 389 TREE_CONSTANT (rval) = TREE_CONSTANT (TREE_OPERAND (rval, 0));
471086d6 390 return rval;
391}
392
393/* For C++: Only need to do one-level references, but cannot
394 get tripped up on signed/unsigned differences.
395
d81e00a4 396 DECL is either NULL_TREE or the _DECL node for a reference that is being
397 initialized. It can be error_mark_node if we don't know the _DECL but
398 we know it's an initialization. */
471086d6 399
471086d6 400tree
d81e00a4 401convert_to_reference (reftype, expr, convtype, flags, decl)
471086d6 402 tree reftype, expr;
d81e00a4 403 int convtype, flags;
404 tree decl;
471086d6 405{
406 register tree type = TYPE_MAIN_VARIANT (TREE_TYPE (reftype));
407 register tree intype = TREE_TYPE (expr);
471086d6 408 tree rval = NULL_TREE;
1a3f833b 409 tree rval_as_conversion = NULL_TREE;
410 int i;
411
412 if (TREE_CODE (intype) == REFERENCE_TYPE)
413 my_friendly_abort (364);
471086d6 414
471086d6 415 intype = TYPE_MAIN_VARIANT (intype);
416
1a3f833b 417 i = comp_target_types (type, intype, 0);
418
419 if (i <= 0 && (convtype & CONV_IMPLICIT) && IS_AGGR_TYPE (intype)
420 && ! (flags & LOOKUP_NO_CONVERSION))
421 {
422 /* Look for a user-defined conversion to lvalue that we can use. */
423
0a4be248 424 rval_as_conversion
425 = build_type_conversion (CONVERT_EXPR, reftype, expr, 1);
1a3f833b 426
427 if (rval_as_conversion && rval_as_conversion != error_mark_node
428 && real_lvalue_p (rval_as_conversion))
429 {
430 expr = rval_as_conversion;
431 rval_as_conversion = NULL_TREE;
432 intype = type;
433 i = 1;
434 }
435 }
436
437 if (((convtype & CONV_STATIC) && i == -1)
438 || ((convtype & CONV_IMPLICIT) && i == 1))
471086d6 439 {
471086d6 440 if (flags & LOOKUP_COMPLAIN)
441 {
bc3887cf 442 tree ttl = TREE_TYPE (reftype);
443 tree ttr;
444
1a3f833b 445 {
446 int r = TREE_READONLY (expr);
447 int v = TREE_THIS_VOLATILE (expr);
448 ttr = cp_build_type_variant (TREE_TYPE (expr), r, v);
449 }
471086d6 450
f9670f72 451 if (! real_lvalue_p (expr) && ! TYPE_READONLY (ttl))
bc3887cf 452 {
d81e00a4 453 if (decl)
454 /* Ensure semantics of [dcl.init.ref] */
f9670f72 455 cp_pedwarn ("initialization of non-const reference `%#T' from rvalue `%T'",
d81e00a4 456 reftype, intype);
457 else
f9670f72 458 cp_pedwarn ("conversion to non-const `%T' from rvalue `%T'",
d81e00a4 459 reftype, intype);
bc3887cf 460 }
d81e00a4 461 else if (! (convtype & CONV_CONST))
471086d6 462 {
d81e00a4 463 if (! TYPE_READONLY (ttl) && TYPE_READONLY (ttr))
464 cp_pedwarn ("conversion from `%T' to `%T' discards const",
ad91f3ed 465 ttr, reftype);
d81e00a4 466 else if (! TYPE_VOLATILE (ttl) && TYPE_VOLATILE (ttr))
467 cp_pedwarn ("conversion from `%T' to `%T' discards volatile",
ad91f3ed 468 ttr, reftype);
471086d6 469 }
0543e7a9 470 }
471
d81e00a4 472 return build_up_reference (reftype, expr, flags,
473 ! (convtype & CONV_CONST));
471086d6 474 }
c07b1ad1 475 else if ((convtype & CONV_REINTERPRET) && lvalue_p (expr))
0543e7a9 476 {
477 /* When casting an lvalue to a reference type, just convert into
478 a pointer to the new type and deference it. This is allowed
e581f478 479 by San Diego WP section 5.2.9 paragraph 12, though perhaps it
0543e7a9 480 should be done directly (jason). (int &)ri ---> *(int*)&ri */
e581f478 481
3748625f 482 /* B* bp; A& ar = (A&)bp; is valid, but it's probably not what they
e581f478 483 meant. */
1a3f833b 484 if (TREE_CODE (intype) == POINTER_TYPE
d81e00a4 485 && (comptypes (TREE_TYPE (intype), type, -1)))
e581f478 486 cp_warning ("casting `%T' to `%T' does not dereference pointer",
487 intype, reftype);
488
0543e7a9 489 rval = build_unary_op (ADDR_EXPR, expr, 0);
490 if (rval != error_mark_node)
985e9ee0 491 rval = convert_force (build_pointer_type (TREE_TYPE (reftype)),
492 rval, 0);
0543e7a9 493 if (rval != error_mark_node)
b0722fac 494 rval = build1 (NOP_EXPR, reftype, rval);
0543e7a9 495 }
0a4be248 496 else
860740a7 497 {
498 rval = convert_for_initialization (NULL_TREE, type, expr, flags,
499 "converting", 0, 0);
500 if (rval == error_mark_node)
501 return error_mark_node;
502 rval = build_up_reference (reftype, rval, flags, 1);
503
504 if (rval && ! TYPE_READONLY (TREE_TYPE (reftype)))
505 cp_pedwarn ("initializing non-const `%T' with `%T' will use a temporary",
506 reftype, intype);
507 }
471086d6 508
509 if (rval)
510 {
96624a9e 511 /* If we found a way to convert earlier, then use it. */
471086d6 512 return rval;
513 }
514
1a3f833b 515 my_friendly_assert (TREE_CODE (intype) != OFFSET_TYPE, 189);
471086d6 516
b248d3f7 517 if (flags & LOOKUP_COMPLAIN)
518 cp_error ("cannot convert type `%T' to type `%T'", intype, reftype);
519
471086d6 520 if (flags & LOOKUP_SPECULATIVELY)
521 return NULL_TREE;
522
523 return error_mark_node;
524}
525
526/* We are using a reference VAL for its value. Bash that reference all the
96624a9e 527 way down to its lowest form. */
528
471086d6 529tree
530convert_from_reference (val)
531 tree val;
532{
533 tree type = TREE_TYPE (val);
534
535 if (TREE_CODE (type) == OFFSET_TYPE)
536 type = TREE_TYPE (type);
8c5c575d 537 if (TREE_CODE (type) == REFERENCE_TYPE)
538 return build_indirect_ref (val, NULL_PTR);
471086d6 539 return val;
540}
541\f
471086d6 542/* Call this when we know (for any reason) that expr is not, in fact,
543 zero. This routine is like convert_pointer_to, but it pays
544 attention to which specific instance of what type we want to
545 convert to. This routine should eventually become
546 convert_to_pointer after all references to convert_to_pointer
547 are removed. */
96624a9e 548
471086d6 549tree
550convert_pointer_to_real (binfo, expr)
551 tree binfo, expr;
552{
553 register tree intype = TREE_TYPE (expr);
554 tree ptr_type;
555 tree type, rval;
556
557 if (TREE_CODE (binfo) == TREE_VEC)
558 type = BINFO_TYPE (binfo);
559 else if (IS_AGGR_TYPE (binfo))
560 {
561 type = binfo;
562 }
563 else
564 {
565 type = binfo;
566 binfo = NULL_TREE;
567 }
568
d2a15a12 569 ptr_type = cp_build_type_variant (type, TYPE_READONLY (TREE_TYPE (intype)),
570 TYPE_VOLATILE (TREE_TYPE (intype)));
571 ptr_type = build_pointer_type (ptr_type);
471086d6 572 if (ptr_type == TYPE_MAIN_VARIANT (intype))
573 return expr;
574
575 if (intype == error_mark_node)
576 return error_mark_node;
577
578 my_friendly_assert (!integer_zerop (expr), 191);
579
580 if (TREE_CODE (type) == RECORD_TYPE
581 && TREE_CODE (TREE_TYPE (intype)) == RECORD_TYPE
582 && type != TYPE_MAIN_VARIANT (TREE_TYPE (intype)))
583 {
584 tree path;
585 int distance
586 = get_base_distance (binfo, TYPE_MAIN_VARIANT (TREE_TYPE (intype)),
587 0, &path);
588
589 /* This function shouldn't be called with unqualified arguments
590 but if it is, give them an error message that they can read. */
591 if (distance < 0)
592 {
b0722fac 593 cp_error ("cannot convert a pointer of type `%T' to a pointer of type `%T'",
594 TREE_TYPE (intype), type);
471086d6 595
596 if (distance == -2)
597 cp_error ("because `%T' is an ambiguous base class", type);
598 return error_mark_node;
599 }
600
601 return build_vbase_path (PLUS_EXPR, ptr_type, expr, path, 1);
602 }
603 rval = build1 (NOP_EXPR, ptr_type,
604 TREE_CODE (expr) == NOP_EXPR ? TREE_OPERAND (expr, 0) : expr);
605 TREE_CONSTANT (rval) = TREE_CONSTANT (expr);
606 return rval;
607}
608
609/* Call this when we know (for any reason) that expr is
610 not, in fact, zero. This routine gets a type out of the first
611 argument and uses it to search for the type to convert to. If there
612 is more than one instance of that type in the expr, the conversion is
613 ambiguous. This routine should eventually go away, and all
614 callers should use convert_to_pointer_real. */
96624a9e 615
471086d6 616tree
617convert_pointer_to (binfo, expr)
618 tree binfo, expr;
619{
620 tree type;
621
622 if (TREE_CODE (binfo) == TREE_VEC)
623 type = BINFO_TYPE (binfo);
624 else if (IS_AGGR_TYPE (binfo))
625 type = binfo;
626 else
627 type = binfo;
628 return convert_pointer_to_real (type, expr);
629}
471086d6 630\f
c4a8ac95 631/* C++ conversions, preference to static cast conversions. */
632
633tree
634cp_convert (type, expr)
635 tree type, expr;
636{
637 return ocp_convert (type, expr, CONV_OLD_CONVERT, LOOKUP_NORMAL);
638}
639
b248d3f7 640/* Conversion...
641
642 FLAGS indicates how we should behave. */
643
471086d6 644tree
c4a8ac95 645ocp_convert (type, expr, convtype, flags)
471086d6 646 tree type, expr;
d81e00a4 647 int convtype, flags;
471086d6 648{
649 register tree e = expr;
650 register enum tree_code code = TREE_CODE (type);
651
b465397d 652 if (e == error_mark_node
653 || TREE_TYPE (e) == error_mark_node)
471086d6 654 return error_mark_node;
d81e00a4 655
7745052b 656 if (TREE_READONLY_DECL_P (e))
657 e = decl_constant_value (e);
658
2133b374 659 if (IS_AGGR_TYPE (type) && (convtype & CONV_FORCE_TEMP)
660 /* Some internal structures (vtable_entry_type, sigtbl_ptr_type)
661 don't go through finish_struct, so they don't have the synthesized
662 constructors. So don't force a temporary. */
663 && TYPE_HAS_CONSTRUCTOR (type))
1a3f833b 664 /* We need a new temporary; don't take this shortcut. */;
665 else if (TYPE_MAIN_VARIANT (type) == TYPE_MAIN_VARIANT (TREE_TYPE (e)))
666 /* Trivial conversion: cv-qualifiers do not matter on rvalues. */
617abf06 667 return fold (build1 (NOP_EXPR, type, e));
d81e00a4 668
669 if (code == VOID_TYPE && (convtype & CONV_STATIC))
670 return build1 (CONVERT_EXPR, type, e);
671
471086d6 672#if 0
673 /* This is incorrect. A truncation can't be stripped this way.
674 Extensions will be stripped by the use of get_unwidened. */
617abf06 675 if (TREE_CODE (e) == NOP_EXPR)
c4a8ac95 676 return cp_convert (type, TREE_OPERAND (e, 0));
471086d6 677#endif
678
679 /* Just convert to the type of the member. */
680 if (code == OFFSET_TYPE)
681 {
682 type = TREE_TYPE (type);
683 code = TREE_CODE (type);
684 }
685
1a3f833b 686#if 0
471086d6 687 if (code == REFERENCE_TYPE)
d81e00a4 688 return fold (convert_to_reference (type, e, convtype, flags, NULL_TREE));
471086d6 689 else if (TREE_CODE (TREE_TYPE (e)) == REFERENCE_TYPE)
690 e = convert_from_reference (e);
1a3f833b 691#endif
471086d6 692
f3ba5c6a 693 if (TREE_CODE (e) == OFFSET_REF)
694 e = resolve_offset_ref (e);
695
bb0726a1 696 if (INTEGRAL_CODE_P (code))
471086d6 697 {
617abf06 698 tree intype = TREE_TYPE (e);
02d7f858 699 /* enum = enum, enum = int, enum = float, (enum)pointer are all
700 errors. */
471086d6 701 if (flag_int_enum_equivalence == 0
702 && TREE_CODE (type) == ENUMERAL_TYPE
02d7f858 703 && ((ARITHMETIC_TYPE_P (intype) && ! (convtype & CONV_STATIC))
704 || (TREE_CODE (intype) == POINTER_TYPE)))
471086d6 705 {
706 cp_pedwarn ("conversion from `%#T' to `%#T'", intype, type);
707
708 if (flag_pedantic_errors)
709 return error_mark_node;
710 }
f3ba5c6a 711 if (IS_AGGR_TYPE (intype))
471086d6 712 {
713 tree rval;
617abf06 714 rval = build_type_conversion (CONVERT_EXPR, type, e, 1);
6495357a 715 if (rval)
716 return rval;
b248d3f7 717 if (flags & LOOKUP_COMPLAIN)
718 cp_error ("`%#T' used where a `%T' was expected", intype, type);
719 if (flags & LOOKUP_SPECULATIVELY)
720 return NULL_TREE;
471086d6 721 return error_mark_node;
722 }
bb0726a1 723 if (code == BOOLEAN_TYPE)
e4ce2dc4 724 {
725 /* Common Ada/Pascal programmer's mistake. We always warn
726 about this since it is so bad. */
727 if (TREE_CODE (expr) == FUNCTION_DECL)
728 cp_warning ("the address of `%D', will always be `true'", expr);
729 return truthvalue_conversion (e);
730 }
471086d6 731 return fold (convert_to_integer (type, e));
732 }
74002e1d 733 if (code == POINTER_TYPE || code == REFERENCE_TYPE
734 || TYPE_PTRMEMFUNC_P (type))
471086d6 735 return fold (cp_convert_to_pointer (type, e));
c4a8ac95 736 if (code == REAL_TYPE || code == COMPLEX_TYPE)
471086d6 737 {
738 if (IS_AGGR_TYPE (TREE_TYPE (e)))
739 {
740 tree rval;
741 rval = build_type_conversion (CONVERT_EXPR, type, e, 1);
742 if (rval)
743 return rval;
744 else
b248d3f7 745 if (flags & LOOKUP_COMPLAIN)
746 cp_error ("`%#T' used where a floating point value was expected",
747 TREE_TYPE (e));
471086d6 748 }
c4a8ac95 749 if (code == REAL_TYPE)
750 return fold (convert_to_real (type, e));
751 else if (code == COMPLEX_TYPE)
752 return fold (convert_to_complex (type, e));
471086d6 753 }
754
755 /* New C++ semantics: since assignment is now based on
756 memberwise copying, if the rhs type is derived from the
757 lhs type, then we may still do a conversion. */
758 if (IS_AGGR_TYPE_CODE (code))
759 {
760 tree dtype = TREE_TYPE (e);
c25194fd 761 tree ctor = NULL_TREE;
471086d6 762
471086d6 763 dtype = TYPE_MAIN_VARIANT (dtype);
764
765 /* Conversion of object pointers or signature pointers/references
766 to signature pointers/references. */
767
768 if (TYPE_LANG_SPECIFIC (type)
769 && (IS_SIGNATURE_POINTER (type) || IS_SIGNATURE_REFERENCE (type)))
770 {
771 tree constructor = build_signature_pointer_constructor (type, expr);
772 tree sig_ty = SIGNATURE_TYPE (type);
773 tree sig_ptr;
774
775 if (constructor == error_mark_node)
776 return error_mark_node;
777
778 sig_ptr = get_temp_name (type, 1);
779 DECL_INITIAL (sig_ptr) = constructor;
780 CLEAR_SIGNATURE (sig_ty);
6f553a8d 781 cp_finish_decl (sig_ptr, constructor, NULL_TREE, 0, 0);
471086d6 782 SET_SIGNATURE (sig_ty);
783 TREE_READONLY (sig_ptr) = 1;
784
785 return sig_ptr;
786 }
787
788 /* Conversion between aggregate types. New C++ semantics allow
789 objects of derived type to be cast to objects of base type.
790 Old semantics only allowed this between pointers.
791
792 There may be some ambiguity between using a constructor
793 vs. using a type conversion operator when both apply. */
794
0a4be248 795 ctor = e;
860740a7 796
0a4be248 797 if ((flags & LOOKUP_ONLYCONVERTING)
798 && ! (IS_AGGR_TYPE (dtype) && DERIVED_FROM_P (type, dtype)))
799 {
800 ctor = build_user_type_conversion (type, ctor, flags);
801 flags |= LOOKUP_NO_CONVERSION;
471086d6 802 }
0a4be248 803 if (ctor)
804 ctor = build_method_call (NULL_TREE, ctor_identifier,
805 build_expr_list (NULL_TREE, ctor),
806 TYPE_BINFO (type), flags);
807 if (ctor)
808 return build_cplus_new (type, ctor);
471086d6 809 }
810
617abf06 811 /* If TYPE or TREE_TYPE (E) is not on the permanent_obstack,
7745052b 812 then it won't be hashed and hence compare as not equal,
471086d6 813 even when it is. */
814 if (code == ARRAY_TYPE
617abf06 815 && TREE_TYPE (TREE_TYPE (e)) == TREE_TYPE (type)
816 && index_type_equal (TYPE_DOMAIN (TREE_TYPE (e)), TYPE_DOMAIN (type)))
817 return e;
471086d6 818
b248d3f7 819 if (flags & LOOKUP_COMPLAIN)
820 cp_error ("conversion from `%T' to non-scalar type `%T' requested",
821 TREE_TYPE (expr), type);
822 if (flags & LOOKUP_SPECULATIVELY)
823 return NULL_TREE;
471086d6 824 return error_mark_node;
825}
826
d81e00a4 827/* Create an expression whose value is that of EXPR,
828 converted to type TYPE. The TREE_TYPE of the value
829 is always TYPE. This function implements all reasonable
830 conversions; callers should filter out those that are
c4a8ac95 831 not permitted by the language being compiled.
832
833 Most of this routine is from build_reinterpret_cast.
834
835 The backend cannot call cp_convert (what was convert) because
836 conversions to/from basetypes may involve memory references
837 (vbases) and adding or subtracting small values (multiple
838 inheritance), but it calls convert from the constant folding code
839 on subtrees of already build trees after it has ripped them apart.
840
841 Also, if we ever support range variables, we'll probably also have to
842 do a little bit more work. */
d81e00a4 843
844tree
845convert (type, expr)
846 tree type, expr;
847{
c4a8ac95 848 tree intype;
849
850 if (type == error_mark_node || expr == error_mark_node)
851 return error_mark_node;
852
c4a8ac95 853 intype = TREE_TYPE (expr);
854
6686e84d 855 if (POINTER_TYPE_P (type) && POINTER_TYPE_P (intype))
c4a8ac95 856 {
857 if (TREE_READONLY_DECL_P (expr))
858 expr = decl_constant_value (expr);
859 return fold (build1 (NOP_EXPR, type, expr));
860 }
861
862 return ocp_convert (type, expr, CONV_OLD_CONVERT,
863 LOOKUP_NORMAL|LOOKUP_NO_CONVERSION);
d81e00a4 864}
865
c4a8ac95 866/* Like cp_convert, except permit conversions to take place which
471086d6 867 are not normally allowed due to access restrictions
868 (such as conversion from sub-type to private super-type). */
96624a9e 869
471086d6 870tree
a74e8896 871convert_force (type, expr, convtype)
471086d6 872 tree type;
873 tree expr;
a74e8896 874 int convtype;
471086d6 875{
876 register tree e = expr;
877 register enum tree_code code = TREE_CODE (type);
878
879 if (code == REFERENCE_TYPE)
d81e00a4 880 return fold (convert_to_reference (type, e, CONV_C_CAST, LOOKUP_COMPLAIN,
881 NULL_TREE));
471086d6 882 else if (TREE_CODE (TREE_TYPE (e)) == REFERENCE_TYPE)
883 e = convert_from_reference (e);
884
885 if (code == POINTER_TYPE)
886 return fold (convert_to_pointer_force (type, e));
887
ac9386a0 888 /* From typeck.c convert_for_assignment */
471086d6 889 if (((TREE_CODE (TREE_TYPE (e)) == POINTER_TYPE && TREE_CODE (e) == ADDR_EXPR
890 && TREE_CODE (TREE_TYPE (e)) == POINTER_TYPE
891 && TREE_CODE (TREE_TYPE (TREE_TYPE (e))) == METHOD_TYPE)
ac9386a0 892 || integer_zerop (e)
893 || TYPE_PTRMEMFUNC_P (TREE_TYPE (e)))
471086d6 894 && TYPE_PTRMEMFUNC_P (type))
895 {
96624a9e 896 /* compatible pointer to member functions. */
ac9386a0 897 return build_ptrmemfunc (TYPE_PTRMEMFUNC_FN_TYPE (type), e, 1);
471086d6 898 }
a74e8896 899
c4a8ac95 900 return ocp_convert (type, e, CONV_C_CAST|convtype, LOOKUP_NORMAL);
471086d6 901}
902
471086d6 903/* Convert an aggregate EXPR to type XTYPE. If a conversion
904 exists, return the attempted conversion. This may
905 return ERROR_MARK_NODE if the conversion is not
906 allowed (references private members, etc).
907 If no conversion exists, NULL_TREE is returned.
908
909 If (FOR_SURE & 1) is non-zero, then we allow this type conversion
910 to take place immediately. Otherwise, we build a SAVE_EXPR
ce28ee2e 911 which can be evaluated if the results are ever needed.
912
913 Changes to this functions should be mirrored in user_harshness.
914
915 FIXME: Ambiguity checking is wrong. Should choose one by the implicit
916 object parameter, or by the second standard conversion sequence if
917 that doesn't do it. This will probably wait for an overloading rewrite.
918 (jason 8/9/95) */
471086d6 919
471086d6 920tree
921build_type_conversion (code, xtype, expr, for_sure)
922 enum tree_code code;
923 tree xtype, expr;
924 int for_sure;
925{
926 /* C++: check to see if we can convert this aggregate type
bcf789d7 927 into the required type. */
0a4be248 928 return build_user_type_conversion
929 (xtype, expr, for_sure ? LOOKUP_NORMAL : 0);
471086d6 930}
931
1e66592c 932/* Convert the given EXPR to one of a group of types suitable for use in an
933 expression. DESIRES is a combination of various WANT_* flags (q.v.)
934 which indicates which types are suitable. If COMPLAIN is 1, complain
935 about ambiguity; otherwise, the caller will deal with it. */
471086d6 936
1e66592c 937tree
938build_expr_type_conversion (desires, expr, complain)
939 int desires;
940 tree expr;
941 int complain;
471086d6 942{
1e66592c 943 tree basetype = TREE_TYPE (expr);
471086d6 944
1e66592c 945 if (TREE_CODE (basetype) == OFFSET_TYPE)
ce28ee2e 946 expr = resolve_offset_ref (expr);
947 expr = convert_from_reference (expr);
948 basetype = TREE_TYPE (expr);
471086d6 949
985e9ee0 950 switch (TREE_CODE (basetype))
471086d6 951 {
985e9ee0 952 case INTEGER_TYPE:
953 if ((desires & WANT_NULL) && TREE_CODE (expr) == INTEGER_CST
954 && integer_zerop (expr))
955 return expr;
956 /* else fall through... */
957
958 case BOOLEAN_TYPE:
959 return (desires & WANT_INT) ? expr : NULL_TREE;
960 case ENUMERAL_TYPE:
961 return (desires & WANT_ENUM) ? expr : NULL_TREE;
962 case REAL_TYPE:
963 return (desires & WANT_FLOAT) ? expr : NULL_TREE;
964 case POINTER_TYPE:
965 return (desires & WANT_POINTER) ? expr : NULL_TREE;
966
967 case FUNCTION_TYPE:
968 case ARRAY_TYPE:
969 return ((desires & WANT_POINTER) ? default_conversion (expr)
970 : NULL_TREE);
1e66592c 971
972 default:
985e9ee0 973 break;
471086d6 974 }
1e66592c 975
985e9ee0 976 return NULL_TREE;
471086d6 977}
bc3887cf 978
96624a9e 979/* Implements integral promotion (4.1) and float->double promotion. */
980
bc3887cf 981tree
982type_promotes_to (type)
983 tree type;
984{
ce28ee2e 985 int constp, volatilep;
986
987 if (type == error_mark_node)
988 return error_mark_node;
989
990 constp = TYPE_READONLY (type);
991 volatilep = TYPE_VOLATILE (type);
bc3887cf 992 type = TYPE_MAIN_VARIANT (type);
bb0726a1 993
994 /* bool always promotes to int (not unsigned), even if it's the same
995 size. */
028ea8b4 996 if (type == boolean_type_node)
bb0726a1 997 type = integer_type_node;
998
999 /* Normally convert enums to int, but convert wide enums to something
1000 wider. */
1001 else if (TREE_CODE (type) == ENUMERAL_TYPE
1002 || type == wchar_type_node)
6c9497b1 1003 {
1004 int precision = MAX (TYPE_PRECISION (type),
1005 TYPE_PRECISION (integer_type_node));
1006 tree totype = type_for_size (precision, 0);
1007 if (TREE_UNSIGNED (type)
1008 && ! int_fits_type_p (TYPE_MAX_VALUE (type), totype))
1009 type = type_for_size (precision, 1);
1010 else
1011 type = totype;
1012 }
bc3887cf 1013 else if (C_PROMOTING_INTEGER_TYPE_P (type))
1014 {
d0acef9e 1015 /* Retain unsignedness if really not getting bigger. */
bc3887cf 1016 if (TREE_UNSIGNED (type)
d0acef9e 1017 && TYPE_PRECISION (type) == TYPE_PRECISION (integer_type_node))
bc3887cf 1018 type = unsigned_type_node;
1019 else
1020 type = integer_type_node;
1021 }
1022 else if (type == float_type_node)
1023 type = double_type_node;
1024
c38086bd 1025 return cp_build_type_variant (type, constp, volatilep);
bc3887cf 1026}
668ae905 1027
1028
1029/* The routines below this point are carefully written to conform to
1030 the standard. They use the same terminology, and follow the rules
1031 closely. Although they are used only in pt.c at the moment, they
1032 should presumably be used everywhere in the future. */
1033
1146f179 1034/* Attempt to perform qualification conversions on EXPR to convert it
1035 to TYPE. Return the resulting expression, or error_mark_node if
1036 the conversion was impossible. */
1037
668ae905 1038tree
1039perform_qualification_conversions (type, expr)
1040 tree type;
1041 tree expr;
1042{
77a851e4 1043 if (comp_target_types (type, TREE_TYPE (expr), 0) == 1)
1146f179 1044 return build1 (NOP_EXPR, type, expr);
1045 else
1046 return error_mark_node;
668ae905 1047}