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