1 /* Language-level data type conversion for GNU C++.
2 Copyright (C) 1987, 1988, 1992, 1993, 1994, 1995, 1996, 1997, 1998,
3 1999, 2000, 2001, 2002 Free Software Foundation, Inc.
4 Hacked by Michael Tiemann (tiemann@cygnus.com)
5
6 This file is part of GNU CC.
7
8 GNU CC is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2, or (at your option)
11 any later version.
12
13 GNU CC is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
17
18 You should have received a copy of the GNU General Public License
19 along with GNU CC; see the file COPYING. If not, write to
20 the Free Software Foundation, 59 Temple Place - Suite 330,
21 Boston, MA 02111-1307, USA. */
22
23
24 /* This file contains the functions for converting C expressions
25 to different data types. The only entry point is `convert'.
26 Every language front end must have a `convert' function
27 but what kind of conversions it does will depend on the language. */
28
29 #include "config.h"
30 #include "system.h"
31 #include "tree.h"
32 #include "flags.h"
33 #include "cp-tree.h"
34 #include "convert.h"
35 #include "toplev.h"
36 #include "decl.h"
37
38 static tree cp_convert_to_pointer PARAMS ((tree, tree, int));
39 static tree convert_to_pointer_force PARAMS ((tree, tree));
40 static tree build_up_reference PARAMS ((tree, tree, int, tree));
41 static void warn_ref_binding PARAMS ((tree, tree, tree));
42
43 /* Change of width--truncation and extension of integers or reals--
44 is represented with NOP_EXPR. Proper functioning of many things
45 assumes that no other conversions can be NOP_EXPRs.
46
47 Conversion between integer and pointer is represented with CONVERT_EXPR.
48 Converting integer to real uses FLOAT_EXPR
49 and real to integer uses FIX_TRUNC_EXPR.
50
51 Here is a list of all the functions that assume that widening and
52 narrowing is always done with a NOP_EXPR:
53 In convert.c, convert_to_integer.
54 In c-typeck.c, build_binary_op_nodefault (boolean ops),
55 and c_common_truthvalue_conversion.
56 In expr.c: expand_expr, for operands of a MULT_EXPR.
57 In fold-const.c: fold.
58 In tree.c: get_narrower and get_unwidened.
59
60 C++: in multiple-inheritance, converting between pointers may involve
61 adjusting them by a delta stored within the class definition. */
62
63 /* Subroutines of `convert'. */
64
65 /* if converting pointer to pointer
66 if dealing with classes, check for derived->base or vice versa
67 else if dealing with method pointers, delegate
68 else convert blindly
69 else if converting class, pass off to build_type_conversion
70 else try C-style pointer conversion. If FORCE is true then allow
71 conversions via virtual bases (these are permitted by reinterpret_cast,
72 but not static_cast). */
73
74 static tree
cp_convert_to_pointer(type,expr,force)75 cp_convert_to_pointer (type, expr, force)
76 tree type, expr;
77 int force;
78 {
79 register tree intype = TREE_TYPE (expr);
80 register enum tree_code form;
81 tree rval;
82
83 if (IS_AGGR_TYPE (intype))
84 {
85 intype = complete_type (intype);
86 if (!COMPLETE_TYPE_P (intype))
87 {
88 error ("can't convert from incomplete type `%T' to `%T'",
89 intype, type);
90 return error_mark_node;
91 }
92
93 rval = build_type_conversion (type, expr);
94 if (rval)
95 {
96 if (rval == error_mark_node)
97 error ("conversion of `%E' from `%T' to `%T' is ambiguous",
98 expr, intype, type);
99 return rval;
100 }
101 }
102
103 /* Handle anachronistic conversions from (::*)() to cv void* or (*)(). */
104 if (TREE_CODE (type) == POINTER_TYPE
105 && (TREE_CODE (TREE_TYPE (type)) == FUNCTION_TYPE
106 || VOID_TYPE_P (TREE_TYPE (type))))
107 {
108 /* Allow an implicit this pointer for pointer to member
109 functions. */
110 if (TYPE_PTRMEMFUNC_P (intype))
111 {
112 tree fntype = TREE_TYPE (TYPE_PTRMEMFUNC_FN_TYPE (intype));
113 tree decl = maybe_dummy_object (TYPE_METHOD_BASETYPE (fntype), 0);
114 expr = build (OFFSET_REF, fntype, decl, expr);
115 }
116
117 if (TREE_CODE (expr) == OFFSET_REF
118 && TREE_CODE (TREE_TYPE (expr)) == METHOD_TYPE)
119 expr = resolve_offset_ref (expr);
120 if (TREE_CODE (TREE_TYPE (expr)) == METHOD_TYPE)
121 expr = build_addr_func (expr);
122 if (TREE_CODE (TREE_TYPE (expr)) == POINTER_TYPE)
123 {
124 if (TREE_CODE (TREE_TYPE (TREE_TYPE (expr))) == METHOD_TYPE)
125 if (pedantic || warn_pmf2ptr)
126 pedwarn ("converting from `%T' to `%T'", TREE_TYPE (expr),
127 type);
128 return build1 (NOP_EXPR, type, expr);
129 }
130 intype = TREE_TYPE (expr);
131 }
132
133 if (expr == error_mark_node)
134 return error_mark_node;
135
136 form = TREE_CODE (intype);
137
138 if (POINTER_TYPE_P (intype))
139 {
140 intype = TYPE_MAIN_VARIANT (intype);
141
142 if (TYPE_MAIN_VARIANT (type) != intype
143 && TREE_CODE (type) == POINTER_TYPE
144 && TREE_CODE (TREE_TYPE (type)) == RECORD_TYPE
145 && IS_AGGR_TYPE (TREE_TYPE (type))
146 && IS_AGGR_TYPE (TREE_TYPE (intype))
147 && TREE_CODE (TREE_TYPE (intype)) == RECORD_TYPE)
148 {
149 enum tree_code code = PLUS_EXPR;
150 tree binfo;
151 tree intype_class;
152 tree type_class;
153 bool same_p;
154
155 intype_class = TREE_TYPE (intype);
156 type_class = TREE_TYPE (type);
157
158 same_p = same_type_p (TYPE_MAIN_VARIANT (intype_class),
159 TYPE_MAIN_VARIANT (type_class));
160 binfo = NULL_TREE;
161 /* Try derived to base conversion. */
162 if (!same_p)
163 binfo = lookup_base (intype_class, type_class, ba_check, NULL);
164 if (!same_p && !binfo)
165 {
166 /* Try base to derived conversion. */
167 binfo = lookup_base (type_class, intype_class, ba_check, NULL);
168 code = MINUS_EXPR;
169 }
170 if (binfo == error_mark_node)
171 return error_mark_node;
172 if (binfo || same_p)
173 {
174 if (binfo)
175 expr = build_base_path (code, expr, binfo, 0);
176 /* Add any qualifier conversions. */
177 return build_nop (type, expr);
178 }
179 }
180
181 if (TYPE_PTRMEM_P (type) && TYPE_PTRMEM_P (intype))
182 {
183 tree b1;
184 tree b2;
185 tree binfo;
186 enum tree_code code = PLUS_EXPR;
187 base_kind bk;
188
189 b1 = TYPE_OFFSET_BASETYPE (TREE_TYPE (type));
190 b2 = TYPE_OFFSET_BASETYPE (TREE_TYPE (intype));
191 binfo = lookup_base (b1, b2, ba_check, &bk);
192 if (!binfo)
193 {
194 binfo = lookup_base (b2, b1, ba_check, &bk);
195 code = MINUS_EXPR;
196 }
197 if (binfo == error_mark_node)
198 return error_mark_node;
199
200 if (bk == bk_via_virtual)
201 {
202 if (force)
203 warning ("pointer to member cast from `%T' to `%T' is via virtual base",
204 TREE_TYPE (intype), TREE_TYPE (type));
205 else
206 {
207 error ("pointer to member cast from `%T' to `%T' is via virtual base",
208 TREE_TYPE (intype), TREE_TYPE (type));
209 return error_mark_node;
210 }
211 /* This is a reinterpret cast, whose result is unspecified.
212 We choose to do nothing. */
213 return build1 (NOP_EXPR, type, expr);
214 }
215
216 if (TREE_CODE (expr) == PTRMEM_CST)
217 expr = cplus_expand_constant (expr);
218
219 if (binfo)
220 expr = size_binop (code, convert (sizetype, expr),
221 BINFO_OFFSET (binfo));
222 }
223 else if (TYPE_PTRMEMFUNC_P (type))
224 {
225 error ("cannot convert `%E' from type `%T' to type `%T'",
226 expr, intype, type);
227 return error_mark_node;
228 }
229
230 return build_nop (type, expr);
231 }
232 else if (TYPE_PTRMEMFUNC_P (type) && TYPE_PTRMEMFUNC_P (intype))
233 return build_ptrmemfunc (TYPE_PTRMEMFUNC_FN_TYPE (type), expr, 0);
234 else if (TYPE_PTRMEMFUNC_P (intype))
235 {
236 error ("cannot convert `%E' from type `%T' to type `%T'",
237 expr, intype, type);
238 return error_mark_node;
239 }
240
241 my_friendly_assert (form != OFFSET_TYPE, 186);
242
243 if (integer_zerop (expr))
244 {
245 if (TYPE_PTRMEMFUNC_P (type))
246 return build_ptrmemfunc (TYPE_PTRMEMFUNC_FN_TYPE (type), expr, 0);
247
248 if (TYPE_PTRMEM_P (type))
249 /* A NULL pointer-to-member is represented by -1, not by
250 zero. */
251 expr = build_int_2 (-1, -1);
252 else
253 expr = build_int_2 (0, 0);
254 TREE_TYPE (expr) = type;
255 /* Fix up the representation of -1 if appropriate. */
256 force_fit_type (expr, 0);
257 return expr;
258 }
259 else if ((TYPE_PTRMEM_P (type) || TYPE_PTRMEMFUNC_P (type))
260 && INTEGRAL_CODE_P (form))
261 {
262 error ("invalid conversion from '%T' to '%T'", intype, type);
263 return error_mark_node;
264 }
265
266 if (INTEGRAL_CODE_P (form))
267 {
268 if (TYPE_PRECISION (intype) == POINTER_SIZE)
269 return build1 (CONVERT_EXPR, type, expr);
270 expr = cp_convert (c_common_type_for_size (POINTER_SIZE, 0), expr);
271 /* Modes may be different but sizes should be the same. */
272 if (GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (expr)))
273 != GET_MODE_SIZE (TYPE_MODE (type)))
274 /* There is supposed to be some integral type
275 that is the same width as a pointer. */
276 abort ();
277 return convert_to_pointer (type, expr);
278 }
279
280 if (type_unknown_p (expr))
281 return instantiate_type (type, expr, tf_error | tf_warning);
282
283 error ("cannot convert `%E' from type `%T' to type `%T'",
284 expr, intype, type);
285 return error_mark_node;
286 }
287
288 /* Like convert, except permit conversions to take place which
289 are not normally allowed due to access restrictions
290 (such as conversion from sub-type to private super-type). */
291
292 static tree
convert_to_pointer_force(type,expr)293 convert_to_pointer_force (type, expr)
294 tree type, expr;
295 {
296 register tree intype = TREE_TYPE (expr);
297 register enum tree_code form = TREE_CODE (intype);
298
299 if (form == POINTER_TYPE)
300 {
301 intype = TYPE_MAIN_VARIANT (intype);
302
303 if (TYPE_MAIN_VARIANT (type) != intype
304 && TREE_CODE (TREE_TYPE (type)) == RECORD_TYPE
305 && IS_AGGR_TYPE (TREE_TYPE (type))
306 && IS_AGGR_TYPE (TREE_TYPE (intype))
307 && TREE_CODE (TREE_TYPE (intype)) == RECORD_TYPE)
308 {
309 enum tree_code code = PLUS_EXPR;
310 tree binfo;
311
312 binfo = lookup_base (TREE_TYPE (intype), TREE_TYPE (type),
313 ba_ignore, NULL);
314 if (!binfo)
315 {
316 binfo = lookup_base (TREE_TYPE (type), TREE_TYPE (intype),
317 ba_ignore, NULL);
318 code = MINUS_EXPR;
319 }
320 if (binfo == error_mark_node)
321 return error_mark_node;
322 if (binfo)
323 {
324 expr = build_base_path (code, expr, binfo, 0);
325 if (expr == error_mark_node)
326 return error_mark_node;
327 /* Add any qualifier conversions. */
328 if (!same_type_p (TREE_TYPE (TREE_TYPE (expr)),
329 TREE_TYPE (type)))
330 expr = build_nop (type, expr);
331 return expr;
332 }
333 }
334 }
335
336 return cp_convert_to_pointer (type, expr, 1);
337 }
338
339 /* We are passing something to a function which requires a reference.
340 The type we are interested in is in TYPE. The initial
341 value we have to begin with is in ARG.
342
343 FLAGS controls how we manage access checking.
344 DIRECT_BIND in FLAGS controls how any temporaries are generated.
345 If DIRECT_BIND is set, DECL is the reference we're binding to. */
346
347 static tree
build_up_reference(type,arg,flags,decl)348 build_up_reference (type, arg, flags, decl)
349 tree type, arg, decl;
350 int flags;
351 {
352 tree rval;
353 tree argtype = TREE_TYPE (arg);
354 tree target_type = TREE_TYPE (type);
355
356 my_friendly_assert (TREE_CODE (type) == REFERENCE_TYPE, 187);
357
358 if ((flags & DIRECT_BIND) && ! real_lvalue_p (arg))
359 {
360 /* Create a new temporary variable. We can't just use a TARGET_EXPR
361 here because it needs to live as long as DECL. */
362 tree targ = arg;
363
364 arg = make_temporary_var_for_ref_to_temp (decl, TREE_TYPE (arg));
365
366 /* Process the initializer for the declaration. */
367 DECL_INITIAL (arg) = targ;
368 cp_finish_decl (arg, targ, NULL_TREE,
369 LOOKUP_ONLYCONVERTING|DIRECT_BIND);
370 }
371 else if (!(flags & DIRECT_BIND) && ! lvalue_p (arg))
372 return get_target_expr (arg);
373
374 /* If we had a way to wrap this up, and say, if we ever needed its
375 address, transform all occurrences of the register, into a memory
376 reference we could win better. */
377 rval = build_unary_op (ADDR_EXPR, arg, 1);
378 if (rval == error_mark_node)
379 return error_mark_node;
380
381 if ((flags & LOOKUP_PROTECT)
382 && TYPE_MAIN_VARIANT (argtype) != TYPE_MAIN_VARIANT (target_type)
383 && IS_AGGR_TYPE (argtype)
384 && IS_AGGR_TYPE (target_type))
385 {
386 /* We go through lookup_base for the access control. */
387 tree binfo = lookup_base (argtype, target_type, ba_check, NULL);
388 if (binfo == error_mark_node)
389 return error_mark_node;
390 if (binfo == NULL_TREE)
391 return error_not_base_type (target_type, argtype);
392 rval = build_base_path (PLUS_EXPR, rval, binfo, 1);
393 }
394 else
395 rval
396 = convert_to_pointer_force (build_pointer_type (target_type), rval);
397 return build_nop (type, rval);
398 }
399
400 /* Subroutine of convert_to_reference. REFTYPE is the target reference type.
401 INTYPE is the original rvalue type and DECL is an optional _DECL node
402 for diagnostics.
403
404 [dcl.init.ref] says that if an rvalue is used to
405 initialize a reference, then the reference must be to a
406 non-volatile const type. */
407
408 static void
warn_ref_binding(reftype,intype,decl)409 warn_ref_binding (reftype, intype, decl)
410 tree reftype, intype, decl;
411 {
412 tree ttl = TREE_TYPE (reftype);
413
414 if (!CP_TYPE_CONST_NON_VOLATILE_P (ttl))
415 {
416 const char *msg;
417
418 if (CP_TYPE_VOLATILE_P (ttl) && decl)
419 msg = "initialization of volatile reference type `%#T' from rvalue of type `%T'";
420 else if (CP_TYPE_VOLATILE_P (ttl))
421 msg = "conversion to volatile reference type `%#T' from rvalue of type `%T'";
422 else if (decl)
423 msg = "initialization of non-const reference type `%#T' from rvalue of type `%T'";
424 else
425 msg = "conversion to non-const reference type `%#T' from rvalue of type `%T'";
426
427 pedwarn (msg, reftype, intype);
428 }
429 }
430
431 /* For C++: Only need to do one-level references, but cannot
432 get tripped up on signed/unsigned differences.
433
434 DECL is either NULL_TREE or the _DECL node for a reference that is being
435 initialized. It can be error_mark_node if we don't know the _DECL but
436 we know it's an initialization. */
437
438 tree
convert_to_reference(reftype,expr,convtype,flags,decl)439 convert_to_reference (reftype, expr, convtype, flags, decl)
440 tree reftype, expr;
441 int convtype, flags;
442 tree decl;
443 {
444 register tree type = TYPE_MAIN_VARIANT (TREE_TYPE (reftype));
445 register tree intype;
446 tree rval = NULL_TREE;
447 tree rval_as_conversion = NULL_TREE;
448 int i;
449
450 if (TREE_CODE (type) == FUNCTION_TYPE
451 && TREE_TYPE (expr) == unknown_type_node)
452 {
453 expr = instantiate_type (type, expr,
454 (flags & LOOKUP_COMPLAIN)
455 ? tf_error | tf_warning : tf_none);
456 if (expr == error_mark_node)
457 return error_mark_node;
458
459 intype = TREE_TYPE (expr);
460 }
461 else
462 {
463 expr = convert_from_reference (expr);
464 intype = TREE_TYPE (expr);
465 }
466
467 my_friendly_assert (TREE_CODE (intype) != REFERENCE_TYPE, 364);
468
469 intype = TYPE_MAIN_VARIANT (intype);
470
471 i = comp_target_types (type, intype, 0);
472
473 if (i <= 0 && (convtype & CONV_IMPLICIT) && IS_AGGR_TYPE (intype)
474 && ! (flags & LOOKUP_NO_CONVERSION))
475 {
476 /* Look for a user-defined conversion to lvalue that we can use. */
477
478 rval_as_conversion
479 = build_type_conversion (reftype, expr);
480
481 if (rval_as_conversion && rval_as_conversion != error_mark_node
482 && real_lvalue_p (rval_as_conversion))
483 {
484 expr = rval_as_conversion;
485 rval_as_conversion = NULL_TREE;
486 intype = type;
487 i = 1;
488 }
489 }
490
491 if (((convtype & CONV_STATIC) && i == -1)
492 || ((convtype & CONV_IMPLICIT) && i == 1))
493 {
494 if (flags & LOOKUP_COMPLAIN)
495 {
496 tree ttl = TREE_TYPE (reftype);
497 tree ttr = lvalue_type (expr);
498
499 if (! real_lvalue_p (expr))
500 warn_ref_binding (reftype, intype, decl);
501
502 if (! (convtype & CONV_CONST)
503 && !at_least_as_qualified_p (ttl, ttr))
504 pedwarn ("conversion from `%T' to `%T' discards qualifiers",
505 ttr, reftype);
506 }
507
508 return build_up_reference (reftype, expr, flags, decl);
509 }
510 else if ((convtype & CONV_REINTERPRET) && lvalue_p (expr))
511 {
512 /* When casting an lvalue to a reference type, just convert into
513 a pointer to the new type and deference it. This is allowed
514 by San Diego WP section 5.2.9 paragraph 12, though perhaps it
515 should be done directly (jason). (int &)ri ---> *(int*)&ri */
516
517 /* B* bp; A& ar = (A&)bp; is valid, but it's probably not what they
518 meant. */
519 if (TREE_CODE (intype) == POINTER_TYPE
520 && (comptypes (TREE_TYPE (intype), type,
521 COMPARE_BASE | COMPARE_RELAXED )))
522 warning ("casting `%T' to `%T' does not dereference pointer",
523 intype, reftype);
524
525 rval = build_unary_op (ADDR_EXPR, expr, 0);
526 if (rval != error_mark_node)
527 rval = convert_force (build_pointer_type (TREE_TYPE (reftype)),
528 rval, 0);
529 if (rval != error_mark_node)
530 rval = build1 (NOP_EXPR, reftype, rval);
531 }
532 else
533 {
534 rval = convert_for_initialization (NULL_TREE, type, expr, flags,
535 "converting", 0, 0);
536 if (rval == NULL_TREE || rval == error_mark_node)
537 return rval;
538 warn_ref_binding (reftype, intype, decl);
539 rval = build_up_reference (reftype, rval, flags, decl);
540 }
541
542 if (rval)
543 {
544 /* If we found a way to convert earlier, then use it. */
545 return rval;
546 }
547
548 my_friendly_assert (TREE_CODE (intype) != OFFSET_TYPE, 189);
549
550 if (flags & LOOKUP_COMPLAIN)
551 error ("cannot convert type `%T' to type `%T'", intype, reftype);
552
553 if (flags & LOOKUP_SPECULATIVELY)
554 return NULL_TREE;
555
556 return error_mark_node;
557 }
558
559 /* We are using a reference VAL for its value. Bash that reference all the
560 way down to its lowest form. */
561
562 tree
convert_from_reference(val)563 convert_from_reference (val)
564 tree val;
565 {
566 tree type = TREE_TYPE (val);
567
568 if (TREE_CODE (type) == OFFSET_TYPE)
569 type = TREE_TYPE (type);
570 if (TREE_CODE (type) == REFERENCE_TYPE)
571 return build_indirect_ref (val, NULL);
572 return val;
573 }
574
575 /* Implicitly convert the lvalue EXPR to another lvalue of type TOTYPE,
576 preserving cv-qualification. */
577
578 tree
convert_lvalue(totype,expr)579 convert_lvalue (totype, expr)
580 tree totype, expr;
581 {
582 totype = cp_build_qualified_type (totype, TYPE_QUALS (TREE_TYPE (expr)));
583 totype = build_reference_type (totype);
584 expr = convert_to_reference (totype, expr, CONV_IMPLICIT, LOOKUP_NORMAL,
585 NULL_TREE);
586 return convert_from_reference (expr);
587 }
588
589 /* Really perform an lvalue-to-rvalue conversion, including copying an
590 argument of class type into a temporary. */
591
592 tree
force_rvalue(tree expr)593 force_rvalue (tree expr)
594 {
595 if (IS_AGGR_TYPE (TREE_TYPE (expr)) && TREE_CODE (expr) != TARGET_EXPR)
596 expr = ocp_convert (TREE_TYPE (expr), expr,
597 CONV_IMPLICIT|CONV_FORCE_TEMP, LOOKUP_NORMAL);
598 else
599 expr = decay_conversion (expr);
600
601 return expr;
602 }
603
604 /* C++ conversions, preference to static cast conversions. */
605
606 tree
cp_convert(type,expr)607 cp_convert (type, expr)
608 tree type, expr;
609 {
610 return ocp_convert (type, expr, CONV_OLD_CONVERT, LOOKUP_NORMAL);
611 }
612
613 /* Conversion...
614
615 FLAGS indicates how we should behave. */
616
617 tree
ocp_convert(type,expr,convtype,flags)618 ocp_convert (type, expr, convtype, flags)
619 tree type, expr;
620 int convtype, flags;
621 {
622 register tree e = expr;
623 register enum tree_code code = TREE_CODE (type);
624
625 if (e == error_mark_node
626 || TREE_TYPE (e) == error_mark_node)
627 return error_mark_node;
628
629 complete_type (type);
630 complete_type (TREE_TYPE (expr));
631
632 e = decl_constant_value (e);
633
634 if (IS_AGGR_TYPE (type) && (convtype & CONV_FORCE_TEMP)
635 /* Some internal structures (vtable_entry_type, sigtbl_ptr_type)
636 don't go through finish_struct, so they don't have the synthesized
637 constructors. So don't force a temporary. */
638 && TYPE_HAS_CONSTRUCTOR (type))
639 /* We need a new temporary; don't take this shortcut. */;
640 else if (TYPE_MAIN_VARIANT (type) == TYPE_MAIN_VARIANT (TREE_TYPE (e)))
641 {
642 if (same_type_p (type, TREE_TYPE (e)))
643 /* The call to fold will not always remove the NOP_EXPR as
644 might be expected, since if one of the types is a typedef;
645 the comparsion in fold is just equality of pointers, not a
646 call to comptypes. We don't call fold in this case because
647 that can result in infinite recursion; fold will call
648 convert, which will call ocp_convert, etc. */
649 return e;
650 /* For complex data types, we need to perform componentwise
651 conversion. */
652 else if (TREE_CODE (type) == COMPLEX_TYPE)
653 return fold (convert_to_complex (type, e));
654 else
655 return fold (build1 (NOP_EXPR, type, e));
656 }
657
658 if (code == VOID_TYPE && (convtype & CONV_STATIC))
659 {
660 e = convert_to_void (e, /*implicit=*/NULL);
661 return e;
662 }
663
664 /* Just convert to the type of the member. */
665 if (code == OFFSET_TYPE)
666 {
667 type = TREE_TYPE (type);
668 code = TREE_CODE (type);
669 }
670
671 if (TREE_CODE (e) == OFFSET_REF)
672 e = resolve_offset_ref (e);
673
674 if (INTEGRAL_CODE_P (code))
675 {
676 tree intype = TREE_TYPE (e);
677 /* enum = enum, enum = int, enum = float, (enum)pointer are all
678 errors. */
679 if (TREE_CODE (type) == ENUMERAL_TYPE
680 && ((ARITHMETIC_TYPE_P (intype) && ! (convtype & CONV_STATIC))
681 || (TREE_CODE (intype) == POINTER_TYPE)))
682 {
683 pedwarn ("conversion from `%#T' to `%#T'", intype, type);
684
685 if (flag_pedantic_errors)
686 return error_mark_node;
687 }
688 if (IS_AGGR_TYPE (intype))
689 {
690 tree rval;
691 rval = build_type_conversion (type, e);
692 if (rval)
693 return rval;
694 if (flags & LOOKUP_COMPLAIN)
695 error ("`%#T' used where a `%T' was expected", intype, type);
696 if (flags & LOOKUP_SPECULATIVELY)
697 return NULL_TREE;
698 return error_mark_node;
699 }
700 if (code == BOOLEAN_TYPE)
701 {
702 tree fn = NULL_TREE;
703
704 /* Common Ada/Pascal programmer's mistake. We always warn
705 about this since it is so bad. */
706 if (TREE_CODE (expr) == FUNCTION_DECL)
707 fn = expr;
708 else if (TREE_CODE (expr) == ADDR_EXPR
709 && TREE_CODE (TREE_OPERAND (expr, 0)) == FUNCTION_DECL)
710 fn = TREE_OPERAND (expr, 0);
711 if (fn && !DECL_WEAK (fn))
712 warning ("the address of `%D', will always be `true'", fn);
713 return cp_truthvalue_conversion (e);
714 }
715 return fold (convert_to_integer (type, e));
716 }
717 if (code == POINTER_TYPE || code == REFERENCE_TYPE
718 || TYPE_PTRMEMFUNC_P (type))
719 return fold (cp_convert_to_pointer (type, e, 0));
720 if (code == VECTOR_TYPE)
721 return fold (convert_to_vector (type, e));
722 if (code == REAL_TYPE || code == COMPLEX_TYPE)
723 {
724 if (IS_AGGR_TYPE (TREE_TYPE (e)))
725 {
726 tree rval;
727 rval = build_type_conversion (type, e);
728 if (rval)
729 return rval;
730 else
731 if (flags & LOOKUP_COMPLAIN)
732 error ("`%#T' used where a floating point value was expected",
733 TREE_TYPE (e));
734 }
735 if (code == REAL_TYPE)
736 return fold (convert_to_real (type, e));
737 else if (code == COMPLEX_TYPE)
738 return fold (convert_to_complex (type, e));
739 }
740
741 /* New C++ semantics: since assignment is now based on
742 memberwise copying, if the rhs type is derived from the
743 lhs type, then we may still do a conversion. */
744 if (IS_AGGR_TYPE_CODE (code))
745 {
746 tree dtype = TREE_TYPE (e);
747 tree ctor = NULL_TREE;
748
749 dtype = TYPE_MAIN_VARIANT (dtype);
750
751 /* Conversion between aggregate types. New C++ semantics allow
752 objects of derived type to be cast to objects of base type.
753 Old semantics only allowed this between pointers.
754
755 There may be some ambiguity between using a constructor
756 vs. using a type conversion operator when both apply. */
757
758 ctor = e;
759
760 if (abstract_virtuals_error (NULL_TREE, type))
761 return error_mark_node;
762
763 if ((flags & LOOKUP_ONLYCONVERTING)
764 && ! (IS_AGGR_TYPE (dtype) && DERIVED_FROM_P (type, dtype)))
765 /* For copy-initialization, first we create a temp of the proper type
766 with a user-defined conversion sequence, then we direct-initialize
767 the target with the temp (see [dcl.init]). */
768 ctor = build_user_type_conversion (type, ctor, flags);
769 else
770 ctor = build_special_member_call (NULL_TREE,
771 complete_ctor_identifier,
772 build_tree_list (NULL_TREE, ctor),
773 TYPE_BINFO (type), flags);
774 if (ctor)
775 return build_cplus_new (type, ctor);
776 }
777
778 if (flags & LOOKUP_COMPLAIN)
779 error ("conversion from `%T' to non-scalar type `%T' requested",
780 TREE_TYPE (expr), type);
781 if (flags & LOOKUP_SPECULATIVELY)
782 return NULL_TREE;
783 return error_mark_node;
784 }
785
786 /* When an expression is used in a void context, its value is discarded and
787 no lvalue-rvalue and similar conversions happen [expr.static.cast/4,
788 stmt.expr/1, expr.comma/1]. This permits dereferencing an incomplete type
789 in a void context. The C++ standard does not define what an `access' to an
790 object is, but there is reason to beleive that it is the lvalue to rvalue
791 conversion -- if it were not, `*&*p = 1' would violate [expr]/4 in that it
792 accesses `*p' not to calculate the value to be stored. But, dcl.type.cv/8
793 indicates that volatile semantics should be the same between C and C++
794 where ever possible. C leaves it implementation defined as to what
795 constitutes an access to a volatile. So, we interpret `*vp' as a read of
796 the volatile object `vp' points to, unless that is an incomplete type. For
797 volatile references we do not do this interpretation, because that would
798 make it impossible to ignore the reference return value from functions. We
799 issue warnings in the confusing cases.
800
801 IMPLICIT is tells us the context of an implicit void conversion. */
802
803 tree
convert_to_void(expr,implicit)804 convert_to_void (expr, implicit)
805 tree expr;
806 const char *implicit;
807 {
808 if (expr == error_mark_node
809 || TREE_TYPE (expr) == error_mark_node)
810 return error_mark_node;
811 if (!TREE_TYPE (expr))
812 return expr;
813 if (VOID_TYPE_P (TREE_TYPE (expr)))
814 return expr;
815 switch (TREE_CODE (expr))
816 {
817 case COND_EXPR:
818 {
819 /* The two parts of a cond expr might be separate lvalues. */
820 tree op1 = TREE_OPERAND (expr,1);
821 tree op2 = TREE_OPERAND (expr,2);
822 tree new_op1 = convert_to_void (op1, implicit);
823 tree new_op2 = convert_to_void (op2, implicit);
824
825 expr = build (COND_EXPR, TREE_TYPE (new_op1),
826 TREE_OPERAND (expr, 0), new_op1, new_op2);
827 break;
828 }
829
830 case COMPOUND_EXPR:
831 {
832 /* The second part of a compound expr contains the value. */
833 tree op1 = TREE_OPERAND (expr,1);
834 tree new_op1 = convert_to_void (op1, implicit);
835
836 if (new_op1 != op1)
837 {
838 tree t = build (COMPOUND_EXPR, TREE_TYPE (new_op1),
839 TREE_OPERAND (expr, 0), new_op1);
840 TREE_SIDE_EFFECTS (t) = TREE_SIDE_EFFECTS (expr);
841 TREE_NO_UNUSED_WARNING (t) = TREE_NO_UNUSED_WARNING (expr);
842 expr = t;
843 }
844
845 break;
846 }
847
848 case NON_LVALUE_EXPR:
849 case NOP_EXPR:
850 /* These have already decayed to rvalue. */
851 break;
852
853 case CALL_EXPR: /* we have a special meaning for volatile void fn() */
854 break;
855
856 case INDIRECT_REF:
857 {
858 tree type = TREE_TYPE (expr);
859 int is_reference = TREE_CODE (TREE_TYPE (TREE_OPERAND (expr, 0)))
860 == REFERENCE_TYPE;
861 int is_volatile = TYPE_VOLATILE (type);
862 int is_complete = COMPLETE_TYPE_P (complete_type (type));
863
864 if (is_volatile && !is_complete)
865 warning ("object of incomplete type `%T' will not be accessed in %s",
866 type, implicit ? implicit : "void context");
867 else if (is_reference && is_volatile)
868 warning ("object of type `%T' will not be accessed in %s",
869 TREE_TYPE (TREE_OPERAND (expr, 0)),
870 implicit ? implicit : "void context");
871 if (is_reference || !is_volatile || !is_complete)
872 expr = TREE_OPERAND (expr, 0);
873
874 break;
875 }
876
877 case VAR_DECL:
878 {
879 /* External variables might be incomplete. */
880 tree type = TREE_TYPE (expr);
881 int is_complete = COMPLETE_TYPE_P (complete_type (type));
882
883 if (TYPE_VOLATILE (type) && !is_complete)
884 warning ("object `%E' of incomplete type `%T' will not be accessed in %s",
885 expr, type, implicit ? implicit : "void context");
886 break;
887 }
888
889 case OFFSET_REF:
890 expr = resolve_offset_ref (expr);
891 break;
892
893 default:;
894 }
895 {
896 tree probe = expr;
897
898 if (TREE_CODE (probe) == ADDR_EXPR)
899 probe = TREE_OPERAND (expr, 0);
900 if (type_unknown_p (probe))
901 {
902 /* [over.over] enumerates the places where we can take the address
903 of an overloaded function, and this is not one of them. */
904 pedwarn ("%s cannot resolve address of overloaded function",
905 implicit ? implicit : "void cast");
906 expr = void_zero_node;
907 }
908 else if (implicit && probe == expr && is_overloaded_fn (probe))
909 /* Only warn when there is no &. */
910 warning ("%s is a reference, not call, to function `%E'",
911 implicit, expr);
912 }
913
914 if (expr != error_mark_node && !VOID_TYPE_P (TREE_TYPE (expr)))
915 {
916 /* FIXME: This is where we should check for expressions with no
917 effects. At the moment we do that in both build_x_component_expr
918 and expand_expr_stmt -- inconsistently too. For the moment
919 leave implicit void conversions unadorned so that expand_expr_stmt
920 has a chance of detecting some of the cases. */
921 if (!implicit)
922 expr = build1 (CONVERT_EXPR, void_type_node, expr);
923 }
924 return expr;
925 }
926
927 /* Create an expression whose value is that of EXPR,
928 converted to type TYPE. The TREE_TYPE of the value
929 is always TYPE. This function implements all reasonable
930 conversions; callers should filter out those that are
931 not permitted by the language being compiled.
932
933 Most of this routine is from build_reinterpret_cast.
934
935 The backend cannot call cp_convert (what was convert) because
936 conversions to/from basetypes may involve memory references
937 (vbases) and adding or subtracting small values (multiple
938 inheritance), but it calls convert from the constant folding code
939 on subtrees of already built trees after it has ripped them apart.
940
941 Also, if we ever support range variables, we'll probably also have to
942 do a little bit more work. */
943
944 tree
convert(type,expr)945 convert (type, expr)
946 tree type, expr;
947 {
948 tree intype;
949
950 if (type == error_mark_node || expr == error_mark_node)
951 return error_mark_node;
952
953 intype = TREE_TYPE (expr);
954
955 if (POINTER_TYPE_P (type) && POINTER_TYPE_P (intype))
956 {
957 expr = decl_constant_value (expr);
958 return fold (build1 (NOP_EXPR, type, expr));
959 }
960
961 return ocp_convert (type, expr, CONV_OLD_CONVERT,
962 LOOKUP_NORMAL|LOOKUP_NO_CONVERSION);
963 }
964
965 /* Like cp_convert, except permit conversions to take place which
966 are not normally allowed due to access restrictions
967 (such as conversion from sub-type to private super-type). */
968
969 tree
convert_force(type,expr,convtype)970 convert_force (type, expr, convtype)
971 tree type;
972 tree expr;
973 int convtype;
974 {
975 register tree e = expr;
976 register enum tree_code code = TREE_CODE (type);
977
978 if (code == REFERENCE_TYPE)
979 return fold (convert_to_reference (type, e, CONV_C_CAST, LOOKUP_COMPLAIN,
980 NULL_TREE));
981 else if (TREE_CODE (TREE_TYPE (e)) == REFERENCE_TYPE)
982 e = convert_from_reference (e);
983
984 if (code == POINTER_TYPE)
985 return fold (convert_to_pointer_force (type, e));
986
987 /* From typeck.c convert_for_assignment */
988 if (((TREE_CODE (TREE_TYPE (e)) == POINTER_TYPE && TREE_CODE (e) == ADDR_EXPR
989 && TREE_CODE (TREE_TYPE (e)) == POINTER_TYPE
990 && TREE_CODE (TREE_TYPE (TREE_TYPE (e))) == METHOD_TYPE)
991 || integer_zerop (e)
992 || TYPE_PTRMEMFUNC_P (TREE_TYPE (e)))
993 && TYPE_PTRMEMFUNC_P (type))
994 {
995 /* compatible pointer to member functions. */
996 return build_ptrmemfunc (TYPE_PTRMEMFUNC_FN_TYPE (type), e, 1);
997 }
998
999 return ocp_convert (type, e, CONV_C_CAST|convtype, LOOKUP_NORMAL);
1000 }
1001
1002 /* Convert an aggregate EXPR to type XTYPE. If a conversion
1003 exists, return the attempted conversion. This may
1004 return ERROR_MARK_NODE if the conversion is not
1005 allowed (references private members, etc).
1006 If no conversion exists, NULL_TREE is returned.
1007
1008 FIXME: Ambiguity checking is wrong. Should choose one by the implicit
1009 object parameter, or by the second standard conversion sequence if
1010 that doesn't do it. This will probably wait for an overloading rewrite.
1011 (jason 8/9/95) */
1012
1013 tree
build_type_conversion(xtype,expr)1014 build_type_conversion (xtype, expr)
1015 tree xtype, expr;
1016 {
1017 /* C++: check to see if we can convert this aggregate type
1018 into the required type. */
1019 return build_user_type_conversion (xtype, expr, LOOKUP_NORMAL);
1020 }
1021
1022 /* Convert the given EXPR to one of a group of types suitable for use in an
1023 expression. DESIRES is a combination of various WANT_* flags (q.v.)
1024 which indicates which types are suitable. If COMPLAIN is 1, complain
1025 about ambiguity; otherwise, the caller will deal with it. */
1026
1027 tree
build_expr_type_conversion(desires,expr,complain)1028 build_expr_type_conversion (desires, expr, complain)
1029 int desires;
1030 tree expr;
1031 int complain;
1032 {
1033 tree basetype = TREE_TYPE (expr);
1034 tree conv = NULL_TREE;
1035 tree winner = NULL_TREE;
1036
1037 if (expr == null_node
1038 && (desires & WANT_INT)
1039 && !(desires & WANT_NULL))
1040 warning ("converting NULL to non-pointer type");
1041
1042 if (TREE_CODE (expr) == OFFSET_REF)
1043 expr = resolve_offset_ref (expr);
1044 expr = convert_from_reference (expr);
1045 basetype = TREE_TYPE (expr);
1046
1047 if (basetype == error_mark_node)
1048 return error_mark_node;
1049
1050 if (! IS_AGGR_TYPE (basetype))
1051 switch (TREE_CODE (basetype))
1052 {
1053 case INTEGER_TYPE:
1054 if ((desires & WANT_NULL) && null_ptr_cst_p (expr))
1055 return expr;
1056 /* else fall through... */
1057
1058 case BOOLEAN_TYPE:
1059 return (desires & WANT_INT) ? expr : NULL_TREE;
1060 case ENUMERAL_TYPE:
1061 return (desires & WANT_ENUM) ? expr : NULL_TREE;
1062 case REAL_TYPE:
1063 return (desires & WANT_FLOAT) ? expr : NULL_TREE;
1064 case POINTER_TYPE:
1065 return (desires & WANT_POINTER) ? expr : NULL_TREE;
1066
1067 case FUNCTION_TYPE:
1068 case ARRAY_TYPE:
1069 return (desires & WANT_POINTER) ? default_conversion (expr)
1070 : NULL_TREE;
1071 default:
1072 return NULL_TREE;
1073 }
1074
1075 /* The code for conversions from class type is currently only used for
1076 delete expressions. Other expressions are handled by build_new_op. */
1077 if (!complete_type_or_else (basetype, expr))
1078 return error_mark_node;
1079 if (!TYPE_HAS_CONVERSION (basetype))
1080 return NULL_TREE;
1081
1082 for (conv = lookup_conversions (basetype); conv; conv = TREE_CHAIN (conv))
1083 {
1084 int win = 0;
1085 tree candidate;
1086 tree cand = TREE_VALUE (conv);
1087
1088 if (winner && winner == cand)
1089 continue;
1090
1091 candidate = TREE_TYPE (TREE_TYPE (cand));
1092 if (TREE_CODE (candidate) == REFERENCE_TYPE)
1093 candidate = TREE_TYPE (candidate);
1094
1095 switch (TREE_CODE (candidate))
1096 {
1097 case BOOLEAN_TYPE:
1098 case INTEGER_TYPE:
1099 win = (desires & WANT_INT); break;
1100 case ENUMERAL_TYPE:
1101 win = (desires & WANT_ENUM); break;
1102 case REAL_TYPE:
1103 win = (desires & WANT_FLOAT); break;
1104 case POINTER_TYPE:
1105 win = (desires & WANT_POINTER); break;
1106
1107 default:
1108 break;
1109 }
1110
1111 if (win)
1112 {
1113 if (winner)
1114 {
1115 if (complain)
1116 {
1117 error ("ambiguous default type conversion from `%T'",
1118 basetype);
1119 error (" candidate conversions include `%D' and `%D'",
1120 winner, cand);
1121 }
1122 return error_mark_node;
1123 }
1124 else
1125 winner = cand;
1126 }
1127 }
1128
1129 if (winner)
1130 {
1131 tree type = TREE_TYPE (TREE_TYPE (winner));
1132 if (TREE_CODE (type) == REFERENCE_TYPE)
1133 type = TREE_TYPE (type);
1134 return build_user_type_conversion (type, expr, LOOKUP_NORMAL);
1135 }
1136
1137 return NULL_TREE;
1138 }
1139
1140 /* Implements integral promotion (4.1) and float->double promotion. */
1141
1142 tree
type_promotes_to(type)1143 type_promotes_to (type)
1144 tree type;
1145 {
1146 int type_quals;
1147
1148 if (type == error_mark_node)
1149 return error_mark_node;
1150
1151 type_quals = cp_type_quals (type);
1152 type = TYPE_MAIN_VARIANT (type);
1153
1154 /* bool always promotes to int (not unsigned), even if it's the same
1155 size. */
1156 if (type == boolean_type_node)
1157 type = integer_type_node;
1158
1159 /* Normally convert enums to int, but convert wide enums to something
1160 wider. */
1161 else if (TREE_CODE (type) == ENUMERAL_TYPE
1162 || type == wchar_type_node)
1163 {
1164 int precision = MAX (TYPE_PRECISION (type),
1165 TYPE_PRECISION (integer_type_node));
1166 tree totype = c_common_type_for_size (precision, 0);
1167 if (TREE_UNSIGNED (type)
1168 && ! int_fits_type_p (TYPE_MAX_VALUE (type), totype))
1169 type = c_common_type_for_size (precision, 1);
1170 else
1171 type = totype;
1172 }
1173 else if (c_promoting_integer_type_p (type))
1174 {
1175 /* Retain unsignedness if really not getting bigger. */
1176 if (TREE_UNSIGNED (type)
1177 && TYPE_PRECISION (type) == TYPE_PRECISION (integer_type_node))
1178 type = unsigned_type_node;
1179 else
1180 type = integer_type_node;
1181 }
1182 else if (type == float_type_node)
1183 type = double_type_node;
1184
1185 return cp_build_qualified_type (type, type_quals);
1186 }
1187
1188 /* The routines below this point are carefully written to conform to
1189 the standard. They use the same terminology, and follow the rules
1190 closely. Although they are used only in pt.c at the moment, they
1191 should presumably be used everywhere in the future. */
1192
1193 /* Attempt to perform qualification conversions on EXPR to convert it
1194 to TYPE. Return the resulting expression, or error_mark_node if
1195 the conversion was impossible. */
1196
1197 tree
perform_qualification_conversions(type,expr)1198 perform_qualification_conversions (type, expr)
1199 tree type;
1200 tree expr;
1201 {
1202 if (TREE_CODE (type) == POINTER_TYPE
1203 && TREE_CODE (TREE_TYPE (expr)) == POINTER_TYPE
1204 && comp_ptr_ttypes (TREE_TYPE (type), TREE_TYPE (TREE_TYPE (expr))))
1205 return build1 (NOP_EXPR, type, expr);
1206 else
1207 return error_mark_node;
1208 }
1209