OSDN Git Service

2f7823f76d930b85a4087441a5348f19ae8937b6
[pf3gnuchains/gcc-fork.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, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010
4    Free Software Foundation, Inc.
5    Hacked by Michael Tiemann (tiemann@cygnus.com)
6
7 This file is part of GCC.
8
9 GCC is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 3, or (at your option)
12 any later version.
13
14 GCC is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17 GNU General Public License for more details.
18
19 You should have received a copy of the GNU General Public License
20 along with GCC; see the file COPYING3.  If not see
21 <http://www.gnu.org/licenses/>.  */
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 "intl.h"
37 #include "convert.h"
38 #include "toplev.h"
39 #include "decl.h"
40 #include "target.h"
41
42 static tree cp_convert_to_pointer (tree, tree);
43 static tree convert_to_pointer_force (tree, tree);
44 static tree build_type_conversion (tree, tree);
45 static tree build_up_reference (tree, tree, int, tree);
46 static void warn_ref_binding (tree, tree, tree);
47
48 /* Change of width--truncation and extension of integers or reals--
49    is represented with NOP_EXPR.  Proper functioning of many things
50    assumes that no other conversions can be NOP_EXPRs.
51
52    Conversion between integer and pointer is represented with CONVERT_EXPR.
53    Converting integer to real uses FLOAT_EXPR
54    and real to integer uses FIX_TRUNC_EXPR.
55
56    Here is a list of all the functions that assume that widening and
57    narrowing is always done with a NOP_EXPR:
58      In convert.c, convert_to_integer.
59      In c-typeck.c, build_binary_op_nodefault (boolean ops),
60         and c_common_truthvalue_conversion.
61      In expr.c: expand_expr, for operands of a MULT_EXPR.
62      In fold-const.c: fold.
63      In tree.c: get_narrower and get_unwidened.
64
65    C++: in multiple-inheritance, converting between pointers may involve
66    adjusting them by a delta stored within the class definition.  */
67 \f
68 /* Subroutines of `convert'.  */
69
70 /* if converting pointer to pointer
71      if dealing with classes, check for derived->base or vice versa
72      else if dealing with method pointers, delegate
73      else convert blindly
74    else if converting class, pass off to build_type_conversion
75    else try C-style pointer conversion.  */
76
77 static tree
78 cp_convert_to_pointer (tree type, tree expr)
79 {
80   tree intype = TREE_TYPE (expr);
81   enum tree_code form;
82   tree rval;
83   if (intype == error_mark_node)
84     return error_mark_node;
85
86   if (MAYBE_CLASS_TYPE_P (intype))
87     {
88       intype = complete_type (intype);
89       if (!COMPLETE_TYPE_P (intype))
90         {
91           error ("can't convert from incomplete type %qT to %qT",
92                  intype, type);
93           return error_mark_node;
94         }
95
96       rval = build_type_conversion (type, expr);
97       if (rval)
98         {
99           if (rval == error_mark_node)
100             error ("conversion of %qE from %qT to %qT is ambiguous",
101                    expr, intype, type);
102           return rval;
103         }
104     }
105
106   /* Handle anachronistic conversions from (::*)() to cv void* or (*)().  */
107   if (TREE_CODE (type) == POINTER_TYPE
108       && (TREE_CODE (TREE_TYPE (type)) == FUNCTION_TYPE
109           || VOID_TYPE_P (TREE_TYPE (type))))
110     {
111       if (TYPE_PTRMEMFUNC_P (intype)
112           || TREE_CODE (intype) == METHOD_TYPE)
113         return convert_member_func_to_ptr (type, expr);
114       if (TREE_CODE (TREE_TYPE (expr)) == POINTER_TYPE)
115         return build_nop (type, expr);
116       intype = TREE_TYPE (expr);
117     }
118
119   if (expr == error_mark_node)
120     return error_mark_node;
121
122   form = TREE_CODE (intype);
123
124   if (POINTER_TYPE_P (intype))
125     {
126       intype = TYPE_MAIN_VARIANT (intype);
127
128       if (TYPE_MAIN_VARIANT (type) != intype
129           && TREE_CODE (type) == POINTER_TYPE
130           && TREE_CODE (TREE_TYPE (type)) == RECORD_TYPE
131           && MAYBE_CLASS_TYPE_P (TREE_TYPE (type))
132           && MAYBE_CLASS_TYPE_P (TREE_TYPE (intype))
133           && TREE_CODE (TREE_TYPE (intype)) == RECORD_TYPE)
134         {
135           enum tree_code code = PLUS_EXPR;
136           tree binfo;
137           tree intype_class;
138           tree type_class;
139           bool same_p;
140
141           intype_class = TREE_TYPE (intype);
142           type_class = TREE_TYPE (type);
143
144           same_p = same_type_p (TYPE_MAIN_VARIANT (intype_class),
145                                 TYPE_MAIN_VARIANT (type_class));
146           binfo = NULL_TREE;
147           /* Try derived to base conversion.  */
148           if (!same_p)
149             binfo = lookup_base (intype_class, type_class, ba_check, NULL);
150           if (!same_p && !binfo)
151             {
152               /* Try base to derived conversion.  */
153               binfo = lookup_base (type_class, intype_class, ba_check, NULL);
154               code = MINUS_EXPR;
155             }
156           if (binfo == error_mark_node)
157             return error_mark_node;
158           if (binfo || same_p)
159             {
160               if (binfo)
161                 expr = build_base_path (code, expr, binfo, 0);
162               /* Add any qualifier conversions.  */
163               return build_nop (type, expr);
164             }
165         }
166
167       if (TYPE_PTRMEMFUNC_P (type))
168         {
169           error ("cannot convert %qE from type %qT to type %qT",
170                  expr, intype, type);
171           return error_mark_node;
172         }
173
174       return build_nop (type, expr);
175     }
176   else if ((TYPE_PTRMEM_P (type) && TYPE_PTRMEM_P (intype))
177            || (TYPE_PTRMEMFUNC_P (type) && TYPE_PTRMEMFUNC_P (intype)))
178     return convert_ptrmem (type, expr, /*allow_inverse_p=*/false,
179                            /*c_cast_p=*/false, tf_warning_or_error);
180   else if (TYPE_PTRMEMFUNC_P (intype))
181     {
182       if (!warn_pmf2ptr)
183         {
184           if (TREE_CODE (expr) == PTRMEM_CST)
185             return cp_convert_to_pointer (type,
186                                           PTRMEM_CST_MEMBER (expr));
187           else if (TREE_CODE (expr) == OFFSET_REF)
188             {
189               tree object = TREE_OPERAND (expr, 0);
190               return get_member_function_from_ptrfunc (&object,
191                                                        TREE_OPERAND (expr, 1));
192             }
193         }
194       error ("cannot convert %qE from type %qT to type %qT",
195              expr, intype, type);
196       return error_mark_node;
197     }
198
199   if (null_ptr_cst_p (expr))
200     {
201       if (TYPE_PTRMEMFUNC_P (type))
202         return build_ptrmemfunc (TYPE_PTRMEMFUNC_FN_TYPE (type), expr, 0,
203                                  /*c_cast_p=*/false, tf_warning_or_error);
204
205       if (TYPE_PTRMEM_P (type))
206         {
207           /* A NULL pointer-to-member is represented by -1, not by
208              zero.  */
209           expr = build_int_cst_type (type, -1);
210         }
211       else
212         expr = build_int_cst (type, 0);
213
214       return expr;
215     }
216   else if (TYPE_PTR_TO_MEMBER_P (type) && INTEGRAL_CODE_P (form))
217     {
218       error ("invalid conversion from %qT to %qT", intype, type);
219       return error_mark_node;
220     }
221
222   if (INTEGRAL_CODE_P (form))
223     {
224       if (TYPE_PRECISION (intype) == POINTER_SIZE)
225         return build1 (CONVERT_EXPR, type, expr);
226       expr = cp_convert (c_common_type_for_size (POINTER_SIZE, 0), expr);
227       /* Modes may be different but sizes should be the same.  There
228          is supposed to be some integral type that is the same width
229          as a pointer.  */
230       gcc_assert (GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (expr)))
231                   == GET_MODE_SIZE (TYPE_MODE (type)));
232
233       return convert_to_pointer (type, expr);
234     }
235
236   if (type_unknown_p (expr))
237     return instantiate_type (type, expr, tf_warning_or_error);
238
239   error ("cannot convert %qE from type %qT to type %qT",
240          expr, intype, type);
241   return error_mark_node;
242 }
243
244 /* Like convert, except permit conversions to take place which
245    are not normally allowed due to access restrictions
246    (such as conversion from sub-type to private super-type).  */
247
248 static tree
249 convert_to_pointer_force (tree type, tree expr)
250 {
251   tree intype = TREE_TYPE (expr);
252   enum tree_code form = TREE_CODE (intype);
253
254   if (form == POINTER_TYPE)
255     {
256       intype = TYPE_MAIN_VARIANT (intype);
257
258       if (TYPE_MAIN_VARIANT (type) != intype
259           && TREE_CODE (TREE_TYPE (type)) == RECORD_TYPE
260           && MAYBE_CLASS_TYPE_P (TREE_TYPE (type))
261           && MAYBE_CLASS_TYPE_P (TREE_TYPE (intype))
262           && TREE_CODE (TREE_TYPE (intype)) == RECORD_TYPE)
263         {
264           enum tree_code code = PLUS_EXPR;
265           tree binfo;
266
267           binfo = lookup_base (TREE_TYPE (intype), TREE_TYPE (type),
268                                ba_unique, NULL);
269           if (!binfo)
270             {
271               binfo = lookup_base (TREE_TYPE (type), TREE_TYPE (intype),
272                                    ba_unique, NULL);
273               code = MINUS_EXPR;
274             }
275           if (binfo == error_mark_node)
276             return error_mark_node;
277           if (binfo)
278             {
279               expr = build_base_path (code, expr, binfo, 0);
280               if (expr == error_mark_node)
281                  return error_mark_node;
282               /* Add any qualifier conversions.  */
283               if (!same_type_p (TREE_TYPE (TREE_TYPE (expr)),
284                                 TREE_TYPE (type)))
285                 expr = build_nop (type, expr);
286               return expr;
287             }
288         }
289     }
290
291   return cp_convert_to_pointer (type, expr);
292 }
293
294 /* We are passing something to a function which requires a reference.
295    The type we are interested in is in TYPE. The initial
296    value we have to begin with is in ARG.
297
298    FLAGS controls how we manage access checking.
299    DIRECT_BIND in FLAGS controls how any temporaries are generated.
300      If DIRECT_BIND is set, DECL is the reference we're binding to.  */
301
302 static tree
303 build_up_reference (tree type, tree arg, int flags, tree decl)
304 {
305   tree rval;
306   tree argtype = TREE_TYPE (arg);
307   tree target_type = TREE_TYPE (type);
308
309   gcc_assert (TREE_CODE (type) == REFERENCE_TYPE);
310
311   if ((flags & DIRECT_BIND) && ! real_lvalue_p (arg))
312     {
313       /* Create a new temporary variable.  We can't just use a TARGET_EXPR
314          here because it needs to live as long as DECL.  */
315       tree targ = arg;
316
317       arg = make_temporary_var_for_ref_to_temp (decl, target_type);
318
319       /* Process the initializer for the declaration.  */
320       DECL_INITIAL (arg) = targ;
321       cp_finish_decl (arg, targ, /*init_const_expr_p=*/false, NULL_TREE,
322                       LOOKUP_ONLYCONVERTING|DIRECT_BIND);
323     }
324   else if (!(flags & DIRECT_BIND) && ! lvalue_p (arg))
325     return get_target_expr (arg);
326
327   /* If we had a way to wrap this up, and say, if we ever needed its
328      address, transform all occurrences of the register, into a memory
329      reference we could win better.  */
330   rval = cp_build_addr_expr (arg, tf_warning_or_error);
331   if (rval == error_mark_node)
332     return error_mark_node;
333
334   if ((flags & LOOKUP_PROTECT)
335       && TYPE_MAIN_VARIANT (argtype) != TYPE_MAIN_VARIANT (target_type)
336       && MAYBE_CLASS_TYPE_P (argtype)
337       && MAYBE_CLASS_TYPE_P (target_type))
338     {
339       /* We go through lookup_base for the access control.  */
340       tree binfo = lookup_base (argtype, target_type, ba_check, NULL);
341       if (binfo == error_mark_node)
342         return error_mark_node;
343       if (binfo == NULL_TREE)
344         return error_not_base_type (target_type, argtype);
345       rval = build_base_path (PLUS_EXPR, rval, binfo, 1);
346     }
347   else
348     rval
349       = convert_to_pointer_force (build_pointer_type (target_type), rval);
350   return build_nop (type, rval);
351 }
352
353 /* Subroutine of convert_to_reference. REFTYPE is the target reference type.
354    INTYPE is the original rvalue type and DECL is an optional _DECL node
355    for diagnostics.
356
357    [dcl.init.ref] says that if an rvalue is used to
358    initialize a reference, then the reference must be to a
359    non-volatile const type.  */
360
361 static void
362 warn_ref_binding (tree reftype, tree intype, tree decl)
363 {
364   tree ttl = TREE_TYPE (reftype);
365
366   if (!CP_TYPE_CONST_NON_VOLATILE_P (ttl))
367     {
368       const char *msg;
369
370       if (CP_TYPE_VOLATILE_P (ttl) && decl)
371         msg = G_("initialization of volatile reference type %q#T from "
372                  "rvalue of type %qT");
373       else if (CP_TYPE_VOLATILE_P (ttl))
374         msg = G_("conversion to volatile reference type %q#T "
375                  "from rvalue of type %qT");
376       else if (decl)
377         msg = G_("initialization of non-const reference type %q#T from "
378                  "rvalue of type %qT");
379       else
380         msg = G_("conversion to non-const reference type %q#T from "
381                  "rvalue of type %qT");
382
383       permerror (input_location, msg, reftype, intype);
384     }
385 }
386
387 /* For C++: Only need to do one-level references, but cannot
388    get tripped up on signed/unsigned differences.
389
390    DECL is either NULL_TREE or the _DECL node for a reference that is being
391    initialized.  It can be error_mark_node if we don't know the _DECL but
392    we know it's an initialization.  */
393
394 tree
395 convert_to_reference (tree reftype, tree expr, int convtype,
396                       int flags, tree decl)
397 {
398   tree type = TYPE_MAIN_VARIANT (TREE_TYPE (reftype));
399   tree intype;
400   tree rval = NULL_TREE;
401   tree rval_as_conversion = NULL_TREE;
402   bool can_convert_intype_to_type;
403
404   if (TREE_CODE (type) == FUNCTION_TYPE
405       && TREE_TYPE (expr) == unknown_type_node)
406     expr = instantiate_type (type, expr,
407                              (flags & LOOKUP_COMPLAIN)
408                              ? tf_warning_or_error : tf_none);
409
410   if (expr == error_mark_node)
411     return error_mark_node;
412
413   intype = TREE_TYPE (expr);
414
415   gcc_assert (TREE_CODE (intype) != REFERENCE_TYPE);
416   gcc_assert (TREE_CODE (reftype) == REFERENCE_TYPE);
417
418   intype = TYPE_MAIN_VARIANT (intype);
419
420   can_convert_intype_to_type = can_convert (type, intype);
421   if (!can_convert_intype_to_type
422       && (convtype & CONV_IMPLICIT) && MAYBE_CLASS_TYPE_P (intype)
423       && ! (flags & LOOKUP_NO_CONVERSION))
424     {
425       /* Look for a user-defined conversion to lvalue that we can use.  */
426
427       rval_as_conversion
428         = build_type_conversion (reftype, expr);
429
430       if (rval_as_conversion && rval_as_conversion != error_mark_node
431           && real_lvalue_p (rval_as_conversion))
432         {
433           expr = rval_as_conversion;
434           rval_as_conversion = NULL_TREE;
435           intype = type;
436           can_convert_intype_to_type = 1;
437         }
438     }
439
440   if (((convtype & CONV_STATIC) && can_convert (intype, type))
441       || ((convtype & CONV_IMPLICIT) && can_convert_intype_to_type))
442     {
443       if (flags & LOOKUP_COMPLAIN)
444         {
445           tree ttl = TREE_TYPE (reftype);
446           tree ttr = lvalue_type (expr);
447
448           if (! real_lvalue_p (expr))
449             warn_ref_binding (reftype, intype, decl);
450
451           if (! (convtype & CONV_CONST)
452                    && !at_least_as_qualified_p (ttl, ttr))
453             permerror (input_location, "conversion from %qT to %qT discards qualifiers",
454                        ttr, reftype);
455         }
456
457       return build_up_reference (reftype, expr, flags, decl);
458     }
459   else if ((convtype & CONV_REINTERPRET) && lvalue_p (expr))
460     {
461       /* When casting an lvalue to a reference type, just convert into
462          a pointer to the new type and deference it.  This is allowed
463          by San Diego WP section 5.2.9 paragraph 12, though perhaps it
464          should be done directly (jason).  (int &)ri ---> *(int*)&ri */
465
466       /* B* bp; A& ar = (A&)bp; is valid, but it's probably not what they
467          meant.  */
468       if (TREE_CODE (intype) == POINTER_TYPE
469           && (comptypes (TREE_TYPE (intype), type,
470                          COMPARE_BASE | COMPARE_DERIVED)))
471         warning (0, "casting %qT to %qT does not dereference pointer",
472                  intype, reftype);
473
474       rval = cp_build_addr_expr (expr, tf_warning_or_error);
475       if (rval != error_mark_node)
476         rval = convert_force (build_pointer_type (TREE_TYPE (reftype)),
477                               rval, 0);
478       if (rval != error_mark_node)
479         rval = build1 (NOP_EXPR, reftype, rval);
480     }
481   else
482     {
483       rval = convert_for_initialization (NULL_TREE, type, expr, flags,
484                                          ICR_CONVERTING, 0, 0,
485                                          tf_warning_or_error);
486       if (rval == NULL_TREE || rval == error_mark_node)
487         return rval;
488       warn_ref_binding (reftype, intype, decl);
489       rval = build_up_reference (reftype, rval, flags, decl);
490     }
491
492   if (rval)
493     {
494       /* If we found a way to convert earlier, then use it.  */
495       return rval;
496     }
497
498   if (flags & LOOKUP_COMPLAIN)
499     error ("cannot convert type %qT to type %qT", intype, reftype);
500
501   return error_mark_node;
502 }
503
504 /* We are using a reference VAL for its value. Bash that reference all the
505    way down to its lowest form.  */
506
507 tree
508 convert_from_reference (tree val)
509 {
510   if (TREE_TYPE (val)
511       && TREE_CODE (TREE_TYPE (val)) == REFERENCE_TYPE)
512     {
513       tree t = TREE_TYPE (TREE_TYPE (val));
514       tree ref = build1 (INDIRECT_REF, t, val);
515
516        /* We *must* set TREE_READONLY when dereferencing a pointer to const,
517           so that we get the proper error message if the result is used
518           to assign to.  Also, &* is supposed to be a no-op.  */
519       TREE_READONLY (ref) = CP_TYPE_CONST_P (t);
520       TREE_THIS_VOLATILE (ref) = CP_TYPE_VOLATILE_P (t);
521       TREE_SIDE_EFFECTS (ref)
522         = (TREE_THIS_VOLATILE (ref) || TREE_SIDE_EFFECTS (val));
523       REFERENCE_REF_P (ref) = 1;
524       val = ref;
525     }
526
527   return val;
528 }
529
530 /* Really perform an lvalue-to-rvalue conversion, including copying an
531    argument of class type into a temporary.  */
532
533 tree
534 force_rvalue (tree expr)
535 {
536   if (MAYBE_CLASS_TYPE_P (TREE_TYPE (expr)) && TREE_CODE (expr) != TARGET_EXPR)
537     expr = ocp_convert (TREE_TYPE (expr), expr,
538                         CONV_IMPLICIT|CONV_FORCE_TEMP, LOOKUP_NORMAL);
539   else
540     expr = decay_conversion (expr);
541
542   return expr;
543 }
544
545 \f
546 /* If EXPR and ORIG are INTEGER_CSTs, return a version of EXPR that has
547    TREE_OVERFLOW set only if it is set in ORIG.  Otherwise, return EXPR
548    unchanged.  */
549
550 static tree
551 ignore_overflows (tree expr, tree orig)
552 {
553   if (TREE_CODE (expr) == INTEGER_CST
554       && TREE_CODE (orig) == INTEGER_CST
555       && TREE_OVERFLOW (expr) != TREE_OVERFLOW (orig))
556     {
557       gcc_assert (!TREE_OVERFLOW (orig));
558       /* Ensure constant sharing.  */
559       expr = build_int_cst_wide (TREE_TYPE (expr),
560                                  TREE_INT_CST_LOW (expr),
561                                  TREE_INT_CST_HIGH (expr));
562     }
563   return expr;
564 }
565
566 /* Fold away simple conversions, but make sure TREE_OVERFLOW is set
567    properly.  */
568
569 tree
570 cp_fold_convert (tree type, tree expr)
571 {
572   tree conv = fold_convert (type, expr);
573   conv = ignore_overflows (conv, expr);
574   return conv;
575 }
576
577 /* C++ conversions, preference to static cast conversions.  */
578
579 tree
580 cp_convert (tree type, tree expr)
581 {
582   return ocp_convert (type, expr, CONV_OLD_CONVERT, LOOKUP_NORMAL);
583 }
584
585 /* C++ equivalent of convert_and_check but using cp_convert as the
586    conversion function.
587
588    Convert EXPR to TYPE, warning about conversion problems with constants.
589    Invoke this function on every expression that is converted implicitly,
590    i.e. because of language rules and not because of an explicit cast.  */
591
592 tree
593 cp_convert_and_check (tree type, tree expr)
594 {
595   tree result;
596
597   if (TREE_TYPE (expr) == type)
598     return expr;
599   
600   result = cp_convert (type, expr);
601
602   if (c_inhibit_evaluation_warnings == 0
603       && !TREE_OVERFLOW_P (expr)
604       && result != error_mark_node)
605     warnings_for_convert_and_check (type, expr, result);
606
607   return result;
608 }
609
610 /* Conversion...
611
612    FLAGS indicates how we should behave.  */
613
614 tree
615 ocp_convert (tree type, tree expr, int convtype, int flags)
616 {
617   tree e = expr;
618   enum tree_code code = TREE_CODE (type);
619   const char *invalid_conv_diag;
620   tree e1;
621
622   if (error_operand_p (e) || type == error_mark_node)
623     return error_mark_node;
624
625   complete_type (type);
626   complete_type (TREE_TYPE (expr));
627
628   if ((invalid_conv_diag
629        = targetm.invalid_conversion (TREE_TYPE (expr), type)))
630     {
631       error (invalid_conv_diag);
632       return error_mark_node;
633     }
634
635   /* FIXME remove when moving to c_fully_fold model.  */
636   /* FIXME do we still need this test?  */
637   if (!CLASS_TYPE_P (type))
638     e = integral_constant_value (e);
639   if (error_operand_p (e))
640     return error_mark_node;
641
642   if (MAYBE_CLASS_TYPE_P (type) && (convtype & CONV_FORCE_TEMP))
643     /* We need a new temporary; don't take this shortcut.  */;
644   else if (same_type_ignoring_top_level_qualifiers_p (type, TREE_TYPE (e)))
645     {
646       if (same_type_p (type, TREE_TYPE (e)))
647         /* The call to fold will not always remove the NOP_EXPR as
648            might be expected, since if one of the types is a typedef;
649            the comparison in fold is just equality of pointers, not a
650            call to comptypes.  We don't call fold in this case because
651            that can result in infinite recursion; fold will call
652            convert, which will call ocp_convert, etc.  */
653         return e;
654       /* For complex data types, we need to perform componentwise
655          conversion.  */
656       else if (TREE_CODE (type) == COMPLEX_TYPE)
657         return fold_if_not_in_template (convert_to_complex (type, e));
658       else if (TREE_CODE (e) == TARGET_EXPR)
659         {
660           /* Don't build a NOP_EXPR of class type.  Instead, change the
661              type of the temporary.  */
662           TREE_TYPE (e) = TREE_TYPE (TARGET_EXPR_SLOT (e)) = type;
663           return e;
664         }
665       else
666         {
667           /* We shouldn't be treating objects of ADDRESSABLE type as
668              rvalues.  */
669           gcc_assert (!TREE_ADDRESSABLE (type));
670           return fold_if_not_in_template (build_nop (type, e));
671         }
672     }
673
674   e1 = targetm.convert_to_type (type, e);
675   if (e1)
676     return e1;
677
678   if (code == VOID_TYPE && (convtype & CONV_STATIC))
679     {
680       e = convert_to_void (e, ICV_CAST, tf_warning_or_error);
681       return e;
682     }
683
684   if (INTEGRAL_CODE_P (code))
685     {
686       tree intype = TREE_TYPE (e);
687       tree converted;
688
689       if (TREE_CODE (type) == ENUMERAL_TYPE)
690         {
691           /* enum = enum, enum = int, enum = float, (enum)pointer are all
692              errors.  */
693           if (((INTEGRAL_OR_ENUMERATION_TYPE_P (intype)
694                 || TREE_CODE (intype) == REAL_TYPE)
695                && ! (convtype & CONV_STATIC))
696               || TREE_CODE (intype) == POINTER_TYPE)
697             {
698               if (flags & LOOKUP_COMPLAIN)
699                 permerror (input_location, "conversion from %q#T to %q#T", intype, type);
700
701               if (!flag_permissive)
702                 return error_mark_node;
703             }
704
705           /* [expr.static.cast]
706
707              8. A value of integral or enumeration type can be explicitly
708              converted to an enumeration type. The value is unchanged if
709              the original value is within the range of the enumeration
710              values. Otherwise, the resulting enumeration value is
711              unspecified.  */
712           if (TREE_CODE (expr) == INTEGER_CST
713               && !int_fits_type_p (expr, ENUM_UNDERLYING_TYPE (type)))
714             warning (OPT_Wconversion, 
715                      "the result of the conversion is unspecified because "
716                      "%qE is outside the range of type %qT",
717                      expr, type);
718         }
719       if (MAYBE_CLASS_TYPE_P (intype))
720         {
721           tree rval;
722           rval = build_type_conversion (type, e);
723           if (rval)
724             return rval;
725           if (flags & LOOKUP_COMPLAIN)
726             error ("%q#T used where a %qT was expected", intype, type);
727           return error_mark_node;
728         }
729       if (code == BOOLEAN_TYPE)
730         return cp_truthvalue_conversion (e);
731
732       converted = fold_if_not_in_template (convert_to_integer (type, e));
733
734       /* Ignore any integer overflow caused by the conversion.  */
735       return ignore_overflows (converted, e);
736     }
737   if (NULLPTR_TYPE_P (type) && e && null_ptr_cst_p (e))
738     return nullptr_node;
739   if (POINTER_TYPE_P (type) || TYPE_PTR_TO_MEMBER_P (type))
740     return fold_if_not_in_template (cp_convert_to_pointer (type, e));
741   if (code == VECTOR_TYPE)
742     {
743       tree in_vtype = TREE_TYPE (e);
744       if (MAYBE_CLASS_TYPE_P (in_vtype))
745         {
746           tree ret_val;
747           ret_val = build_type_conversion (type, e);
748           if (ret_val)
749             return ret_val;
750           if (flags & LOOKUP_COMPLAIN)
751             error ("%q#T used where a %qT was expected", in_vtype, type);
752           return error_mark_node;
753         }
754       return fold_if_not_in_template (convert_to_vector (type, e));
755     }
756   if (code == REAL_TYPE || code == COMPLEX_TYPE)
757     {
758       if (MAYBE_CLASS_TYPE_P (TREE_TYPE (e)))
759         {
760           tree rval;
761           rval = build_type_conversion (type, e);
762           if (rval)
763             return rval;
764           else
765             if (flags & LOOKUP_COMPLAIN)
766               error ("%q#T used where a floating point value was expected",
767                         TREE_TYPE (e));
768         }
769       if (code == REAL_TYPE)
770         return fold_if_not_in_template (convert_to_real (type, e));
771       else if (code == COMPLEX_TYPE)
772         return fold_if_not_in_template (convert_to_complex (type, e));
773     }
774
775   /* New C++ semantics:  since assignment is now based on
776      memberwise copying,  if the rhs type is derived from the
777      lhs type, then we may still do a conversion.  */
778   if (RECORD_OR_UNION_CODE_P (code))
779     {
780       tree dtype = TREE_TYPE (e);
781       tree ctor = NULL_TREE;
782
783       dtype = TYPE_MAIN_VARIANT (dtype);
784
785       /* Conversion between aggregate types.  New C++ semantics allow
786          objects of derived type to be cast to objects of base type.
787          Old semantics only allowed this between pointers.
788
789          There may be some ambiguity between using a constructor
790          vs. using a type conversion operator when both apply.  */
791
792       ctor = e;
793
794       if (abstract_virtuals_error (NULL_TREE, type))
795         return error_mark_node;
796
797       if (BRACE_ENCLOSED_INITIALIZER_P (ctor))
798         ctor = perform_implicit_conversion (type, ctor, tf_warning_or_error);
799       else if ((flags & LOOKUP_ONLYCONVERTING)
800                && ! (CLASS_TYPE_P (dtype) && DERIVED_FROM_P (type, dtype)))
801         /* For copy-initialization, first we create a temp of the proper type
802            with a user-defined conversion sequence, then we direct-initialize
803            the target with the temp (see [dcl.init]).  */
804         ctor = build_user_type_conversion (type, ctor, flags);
805       else
806         {
807           VEC(tree,gc) *ctor_vec = make_tree_vector_single (ctor);
808           ctor = build_special_member_call (NULL_TREE,
809                                             complete_ctor_identifier,
810                                             &ctor_vec,
811                                             type, flags,
812                                             tf_warning_or_error);
813           release_tree_vector (ctor_vec);
814         }
815       if (ctor)
816         return build_cplus_new (type, ctor);
817     }
818
819   if (flags & LOOKUP_COMPLAIN)
820     {
821       /* If the conversion failed and expr was an invalid use of pointer to
822          member function, try to report a meaningful error.  */
823       if (invalid_nonstatic_memfn_p (expr, tf_warning_or_error))
824         /* We displayed the error message.  */;
825       else
826         error ("conversion from %qT to non-scalar type %qT requested",
827                TREE_TYPE (expr), type);
828     }
829   return error_mark_node;
830 }
831
832 /* When an expression is used in a void context, its value is discarded and
833    no lvalue-rvalue and similar conversions happen [expr.static.cast/4,
834    stmt.expr/1, expr.comma/1].  This permits dereferencing an incomplete type
835    in a void context. The C++ standard does not define what an `access' to an
836    object is, but there is reason to believe that it is the lvalue to rvalue
837    conversion -- if it were not, `*&*p = 1' would violate [expr]/4 in that it
838    accesses `*p' not to calculate the value to be stored. But, dcl.type.cv/8
839    indicates that volatile semantics should be the same between C and C++
840    where ever possible. C leaves it implementation defined as to what
841    constitutes an access to a volatile. So, we interpret `*vp' as a read of
842    the volatile object `vp' points to, unless that is an incomplete type. For
843    volatile references we do not do this interpretation, because that would
844    make it impossible to ignore the reference return value from functions. We
845    issue warnings in the confusing cases.
846
847    The IMPLICIT is ICV_CAST when the user is explicitly converting an expression
848    to void via a cast. If an expression is being implicitly converted, IMPLICIT
849    indicates the context of the implicit conversion.  */
850
851 tree
852 convert_to_void (tree expr, impl_conv_void implicit, tsubst_flags_t complain)
853 {
854   if (expr == error_mark_node
855       || TREE_TYPE (expr) == error_mark_node)
856     return error_mark_node;
857
858   if (implicit == ICV_CAST)
859     mark_exp_read (expr);
860   else
861     {
862       tree exprv = expr;
863
864       while (TREE_CODE (exprv) == COMPOUND_EXPR)
865         exprv = TREE_OPERAND (exprv, 1);
866       if (DECL_P (exprv)
867           || handled_component_p (exprv)
868           || TREE_CODE (exprv) == INDIRECT_REF)
869         /* Expr is not being 'used' here, otherwise we whould have
870            called mark_{rl}value_use use here, which would have in turn
871            called mark_exp_read.  Rather, we call mark_exp_read directly
872            to avoid some warnings when
873            -Wunused-but-set-{variable,parameter} is in effect.  */
874         mark_exp_read (exprv);
875     }
876
877   if (!TREE_TYPE (expr))
878     return expr;
879   if (invalid_nonstatic_memfn_p (expr, complain))
880     return error_mark_node;
881   if (TREE_CODE (expr) == PSEUDO_DTOR_EXPR)
882     {
883       if (complain & tf_error)
884         error ("pseudo-destructor is not called");
885       return error_mark_node;
886     }
887   if (VOID_TYPE_P (TREE_TYPE (expr)))
888     return expr;
889   switch (TREE_CODE (expr))
890     {
891     case COND_EXPR:
892       {
893         /* The two parts of a cond expr might be separate lvalues.  */
894         tree op1 = TREE_OPERAND (expr,1);
895         tree op2 = TREE_OPERAND (expr,2);
896         bool side_effects = TREE_SIDE_EFFECTS (op1) || TREE_SIDE_EFFECTS (op2);
897         tree new_op1, new_op2;
898         if (implicit != ICV_CAST && !side_effects)
899           {
900             new_op1 = convert_to_void (op1, ICV_SECOND_OF_COND, complain);
901             new_op2 = convert_to_void (op2, ICV_THIRD_OF_COND, complain);
902           }
903         else
904           {
905             new_op1 = convert_to_void (op1, ICV_CAST, complain);
906             new_op2 = convert_to_void (op2, ICV_CAST, complain);
907           }
908
909         expr = build3 (COND_EXPR, TREE_TYPE (new_op1),
910                        TREE_OPERAND (expr, 0), new_op1, new_op2);
911         break;
912       }
913
914     case COMPOUND_EXPR:
915       {
916         /* The second part of a compound expr contains the value.  */
917         tree op1 = TREE_OPERAND (expr,1);
918         tree new_op1;
919         if (implicit != ICV_CAST && !TREE_NO_WARNING (expr))
920           new_op1 = convert_to_void (op1, ICV_RIGHT_OF_COMMA, complain);
921         else
922           new_op1 = convert_to_void (op1, ICV_CAST, complain);
923
924         if (new_op1 != op1)
925           {
926             tree t = build2 (COMPOUND_EXPR, TREE_TYPE (new_op1),
927                              TREE_OPERAND (expr, 0), new_op1);
928             expr = t;
929           }
930
931         break;
932       }
933
934     case NON_LVALUE_EXPR:
935     case NOP_EXPR:
936       /* These have already decayed to rvalue.  */
937       break;
938
939     case CALL_EXPR:   /* We have a special meaning for volatile void fn().  */
940       break;
941
942     case INDIRECT_REF:
943       {
944         tree type = TREE_TYPE (expr);
945         int is_reference = TREE_CODE (TREE_TYPE (TREE_OPERAND (expr, 0)))
946                            == REFERENCE_TYPE;
947         int is_volatile = TYPE_VOLATILE (type);
948         int is_complete = COMPLETE_TYPE_P (complete_type (type));
949
950         /* Can't load the value if we don't know the type.  */
951         if (is_volatile && !is_complete)
952           {
953             if (complain & tf_warning)
954               switch (implicit)
955                 {
956                   case ICV_CAST:
957                     warning (0, "conversion to void will not access "
958                                 "object of incomplete type %qT", type);
959                     break;
960                   case ICV_SECOND_OF_COND:
961                     warning (0, "indirection will not access object of "
962                                 "incomplete type %qT in second operand "
963                                 "of conditional expression", type);
964                     break;
965                   case ICV_THIRD_OF_COND:
966                     warning (0, "indirection will not access object of "
967                                 "incomplete type %qT in third operand "
968                                 "of conditional expression", type);
969                     break;
970                   case ICV_RIGHT_OF_COMMA:
971                     warning (0, "indirection will not access object of "
972                                 "incomplete type %qT in right operand of "
973                                 "comma operator", type);
974                     break;
975                   case ICV_LEFT_OF_COMMA:
976                     warning (0, "indirection will not access object of "
977                                 "incomplete type %qT in left operand of "
978                                 "comma operator", type);
979                     break;
980                   case ICV_STATEMENT:
981                     warning (0, "indirection will not access object of "
982                                 "incomplete type %qT in statement", type);
983                      break;
984                   case ICV_THIRD_IN_FOR:
985                     warning (0, "indirection will not access object of "
986                                 "incomplete type %qT in for increment "
987                                 "expression", type);
988                     break;
989                   default:
990                     gcc_unreachable ();
991                 }
992           }
993         /* Don't load the value if this is an implicit dereference, or if
994            the type needs to be handled by ctors/dtors.  */
995         else if (is_volatile && is_reference)
996           {
997             if (complain & tf_warning)
998               switch (implicit)
999                 {
1000                   case ICV_CAST:
1001                     warning (0, "conversion to void will not access "
1002                                 "object of type %qT", type);
1003                     break;
1004                   case ICV_SECOND_OF_COND:
1005                     warning (0, "implicit dereference will not access object "
1006                                 "of type %qT in second operand of "
1007                                 "conditional expression", type);
1008                     break;
1009                   case ICV_THIRD_OF_COND:
1010                     warning (0, "implicit dereference will not access object "
1011                                 "of type %qT in third operand of "
1012                                 "conditional expression", type);
1013                     break;
1014                   case ICV_RIGHT_OF_COMMA:
1015                     warning (0, "implicit dereference will not access object "
1016                                 "of type %qT in right operand of "
1017                                 "comma operator", type);
1018                     break;
1019                   case ICV_LEFT_OF_COMMA:
1020                     warning (0, "implicit dereference will not access object "
1021                                 "of type %qT in left operand of comma operator",
1022                              type);
1023                     break;
1024                   case ICV_STATEMENT:
1025                     warning (0, "implicit dereference will not access object "
1026                                 "of type %qT in statement",  type);
1027                      break;
1028                   case ICV_THIRD_IN_FOR:
1029                     warning (0, "implicit dereference will not access object "
1030                                 "of type %qT in for increment expression",
1031                              type);
1032                     break;
1033                   default:
1034                     gcc_unreachable ();
1035                 }
1036           }
1037         else if (is_volatile && TREE_ADDRESSABLE (type))
1038           {
1039             if (complain & tf_warning)
1040               switch (implicit)
1041                 {
1042                   case ICV_CAST:
1043                     warning (0, "conversion to void will not access "
1044                                 "object of non-trivially-copyable type %qT",
1045                              type);
1046                     break;
1047                   case ICV_SECOND_OF_COND:
1048                     warning (0, "indirection will not access object of "
1049                                 "non-trivially-copyable type %qT in second "
1050                                 "operand of conditional expression", type);
1051                     break;
1052                   case ICV_THIRD_OF_COND:
1053                     warning (0, "indirection will not access object of "
1054                                 "non-trivially-copyable type %qT in third "
1055                                 "operand of conditional expression", type);
1056                     break;
1057                   case ICV_RIGHT_OF_COMMA:
1058                     warning (0, "indirection will not access object of "
1059                                 "non-trivially-copyable type %qT in right "
1060                                 "operand of comma operator", type);
1061                     break;
1062                   case ICV_LEFT_OF_COMMA:
1063                     warning (0, "indirection will not access object of "
1064                                 "non-trivially-copyable type %qT in left "
1065                                 "operand of comma operator", type);
1066                     break;
1067                   case ICV_STATEMENT:
1068                     warning (0, "indirection will not access object of "
1069                                 "non-trivially-copyable type %qT in statement",
1070                               type);
1071                      break;
1072                   case ICV_THIRD_IN_FOR:
1073                     warning (0, "indirection will not access object of "
1074                                 "non-trivially-copyable type %qT in for "
1075                                 "increment expression", type);
1076                     break;
1077                   default:
1078                     gcc_unreachable ();
1079                 }
1080           }
1081         if (is_reference || !is_volatile || !is_complete || TREE_ADDRESSABLE (type))
1082           {
1083             /* Emit a warning (if enabled) when the "effect-less" INDIRECT_REF
1084                operation is stripped off. Note that we don't warn about
1085                - an expression with TREE_NO_WARNING set. (For an example of
1086                  such expressions, see build_over_call in call.c.)
1087                - automatic dereferencing of references, since the user cannot
1088                  control it. (See also warn_if_unused_value() in stmt.c.)  */
1089             if (warn_unused_value
1090                 && implicit != ICV_CAST
1091                 && (complain & tf_warning)
1092                 && !TREE_NO_WARNING (expr)
1093                 && !is_reference)
1094               warning (OPT_Wunused_value, "value computed is not used");
1095             expr = TREE_OPERAND (expr, 0);
1096           }
1097
1098         break;
1099       }
1100
1101     case VAR_DECL:
1102       {
1103         /* External variables might be incomplete.  */
1104         tree type = TREE_TYPE (expr);
1105         int is_complete = COMPLETE_TYPE_P (complete_type (type));
1106
1107         if (TYPE_VOLATILE (type) && !is_complete && (complain & tf_warning))
1108           switch (implicit)
1109             {
1110               case ICV_CAST:
1111                 warning (0, "conversion to void will not access "
1112                             "object %qE of incomplete type %qT", expr, type);
1113                 break;
1114               case ICV_SECOND_OF_COND:
1115                 warning (0, "variable %qE of incomplete type %qT will not "
1116                             "be accessed in second operand of "
1117                             "conditional expression", expr, type);
1118                 break;
1119               case ICV_THIRD_OF_COND:
1120                 warning (0, "variable %qE of incomplete type %qT will not "
1121                             "be accessed in third operand of "
1122                             "conditional expression", expr, type);
1123                 break;
1124               case ICV_RIGHT_OF_COMMA:
1125                 warning (0, "variable %qE of incomplete type %qT will not "
1126                             "be accessed in right operand of comma operator",
1127                          expr, type);
1128                 break;
1129               case ICV_LEFT_OF_COMMA:
1130                 warning (0, "variable %qE of incomplete type %qT will not "
1131                             "be accessed in left operand of comma operator",
1132                          expr, type);
1133                 break;
1134               case ICV_STATEMENT:
1135                 warning (0, "variable %qE of incomplete type %qT will not "
1136                             "be accessed in statement", expr, type);
1137                 break;
1138               case ICV_THIRD_IN_FOR:
1139                 warning (0, "variable %qE of incomplete type %qT will not "
1140                             "be accessed in for increment expression",
1141                          expr, type);
1142                 break;
1143               default:
1144                 gcc_unreachable ();
1145             }
1146
1147         break;
1148       }
1149
1150     case TARGET_EXPR:
1151       /* Don't bother with the temporary object returned from a function if
1152          we don't use it and don't need to destroy it.  We'll still
1153          allocate space for it in expand_call or declare_return_variable,
1154          but we don't need to track it through all the tree phases.  */
1155       if (TARGET_EXPR_IMPLICIT_P (expr)
1156           && TYPE_HAS_TRIVIAL_DESTRUCTOR (TREE_TYPE (expr)))
1157         {
1158           tree init = TARGET_EXPR_INITIAL (expr);
1159           if (TREE_CODE (init) == AGGR_INIT_EXPR
1160               && !AGGR_INIT_VIA_CTOR_P (init))
1161             {
1162               tree fn = AGGR_INIT_EXPR_FN (init);
1163               expr = build_call_array_loc (input_location,
1164                                            TREE_TYPE (TREE_TYPE (TREE_TYPE (fn))),
1165                                            fn,
1166                                            aggr_init_expr_nargs (init),
1167                                            AGGR_INIT_EXPR_ARGP (init));
1168             }
1169         }
1170       break;
1171
1172     default:;
1173     }
1174   expr = resolve_nondeduced_context (expr);
1175   {
1176     tree probe = expr;
1177
1178     if (TREE_CODE (probe) == ADDR_EXPR)
1179       probe = TREE_OPERAND (expr, 0);
1180     if (type_unknown_p (probe))
1181       {
1182         /* [over.over] enumerates the places where we can take the address
1183            of an overloaded function, and this is not one of them.  */
1184         if (complain & tf_error)
1185           switch (implicit)
1186             {
1187               case ICV_CAST:
1188                 error ("conversion to void "
1189                        "cannot resolve address of overloaded function");
1190                 break;
1191               case ICV_SECOND_OF_COND:
1192                 error ("second operand of conditional expression "
1193                        "cannot resolve address of overloaded function");
1194                 break;
1195               case ICV_THIRD_OF_COND:
1196                 error ("third operand of conditional expression "
1197                        "cannot resolve address of overloaded function");
1198                 break;
1199               case ICV_RIGHT_OF_COMMA:
1200                 error ("right operand of comma operator "
1201                        "cannot resolve address of overloaded function");
1202                 break;
1203               case ICV_LEFT_OF_COMMA:
1204                 error ("left operand of comma operator "
1205                        "cannot resolve address of overloaded function");
1206                 break;
1207               case ICV_STATEMENT:
1208                 error ("statement "
1209                        "cannot resolve address of overloaded function");
1210                 break;
1211               case ICV_THIRD_IN_FOR:
1212                 error ("for increment expression "
1213                        "cannot resolve address of overloaded function");
1214                 break;
1215             }
1216         else
1217           return error_mark_node;
1218         expr = void_zero_node;
1219       }
1220     else if (implicit != ICV_CAST && probe == expr && is_overloaded_fn (probe))
1221       {
1222         /* Only warn when there is no &.  */
1223         if (complain & tf_warning)
1224           switch (implicit)
1225             {
1226               case ICV_SECOND_OF_COND:
1227                 warning (OPT_Waddress,
1228                          "second operand of conditional expression "
1229                          "is a reference, not call, to function %qE", expr);
1230                 break;
1231               case ICV_THIRD_OF_COND:
1232                 warning (OPT_Waddress,
1233                          "third operand of conditional expression "
1234                          "is a reference, not call, to function %qE", expr);
1235                 break;
1236               case ICV_RIGHT_OF_COMMA:
1237                 warning (OPT_Waddress,
1238                          "right operand of comma operator "
1239                          "is a reference, not call, to function %qE", expr);
1240                 break;
1241               case ICV_LEFT_OF_COMMA:
1242                 warning (OPT_Waddress,
1243                          "left operand of comma operator "
1244                          "is a reference, not call, to function %qE", expr);
1245                 break;
1246               case ICV_STATEMENT:
1247                 warning (OPT_Waddress,
1248                          "statement is a reference, not call, to function %qE",
1249                          expr);
1250                 break;
1251               case ICV_THIRD_IN_FOR:
1252                 warning (OPT_Waddress,
1253                          "for increment expression "
1254                          "is a reference, not call, to function %qE", expr);
1255                 break;
1256               default:
1257                 gcc_unreachable ();
1258             }
1259
1260         if (TREE_CODE (expr) == COMPONENT_REF)
1261           expr = TREE_OPERAND (expr, 0);
1262       }
1263   }
1264
1265   if (expr != error_mark_node && !VOID_TYPE_P (TREE_TYPE (expr)))
1266     {
1267       if (implicit != ICV_CAST
1268           && warn_unused_value
1269           && !TREE_NO_WARNING (expr)
1270           && !processing_template_decl)
1271         {
1272           /* The middle end does not warn about expressions that have
1273              been explicitly cast to void, so we must do so here.  */
1274           if (!TREE_SIDE_EFFECTS (expr)) {
1275             if (complain & tf_warning)
1276               switch (implicit)
1277                 {
1278                   case ICV_SECOND_OF_COND:
1279                     warning (OPT_Wunused_value,
1280                              "second operand of conditional expression has no effect");
1281                     break;
1282                   case ICV_THIRD_OF_COND:
1283                     warning (OPT_Wunused_value,
1284                              "third operand of conditional expression has no effect");
1285                     break;
1286                   case ICV_RIGHT_OF_COMMA:
1287                     warning (OPT_Wunused_value,
1288                              "right operand of comma operator has no effect");
1289                     break;
1290                   case ICV_LEFT_OF_COMMA:
1291                     warning (OPT_Wunused_value,
1292                              "left operand of comma operator has no effect");
1293                     break;
1294                   case ICV_STATEMENT:
1295                     warning (OPT_Wunused_value,
1296                              "statement has no effect");
1297                     break;
1298                   case ICV_THIRD_IN_FOR:
1299                     warning (OPT_Wunused_value,
1300                              "for increment expression has no effect");
1301                     break;
1302                   default:
1303                     gcc_unreachable ();
1304                 }
1305           }
1306           else
1307             {
1308               tree e;
1309               enum tree_code code;
1310               enum tree_code_class tclass;
1311
1312               e = expr;
1313               /* We might like to warn about (say) "(int) f()", as the
1314                  cast has no effect, but the compiler itself will
1315                  generate implicit conversions under some
1316                  circumstances.  (For example a block copy will be
1317                  turned into a call to "__builtin_memcpy", with a
1318                  conversion of the return value to an appropriate
1319                  type.)  So, to avoid false positives, we strip
1320                  conversions.  Do not use STRIP_NOPs because it will
1321                  not strip conversions to "void", as that is not a
1322                  mode-preserving conversion.  */
1323               while (TREE_CODE (e) == NOP_EXPR)
1324                 e = TREE_OPERAND (e, 0);
1325
1326               code = TREE_CODE (e);
1327               tclass = TREE_CODE_CLASS (code);
1328               if ((tclass == tcc_comparison
1329                    || tclass == tcc_unary
1330                    || (tclass == tcc_binary
1331                        && !(code == MODIFY_EXPR
1332                             || code == INIT_EXPR
1333                             || code == PREDECREMENT_EXPR
1334                             || code == PREINCREMENT_EXPR
1335                             || code == POSTDECREMENT_EXPR
1336                             || code == POSTINCREMENT_EXPR)))
1337                   && (complain & tf_warning))
1338                 warning (OPT_Wunused_value, "value computed is not used");
1339             }
1340         }
1341       expr = build1 (CONVERT_EXPR, void_type_node, expr);
1342     }
1343   if (! TREE_SIDE_EFFECTS (expr))
1344     expr = void_zero_node;
1345   return expr;
1346 }
1347
1348 /* Create an expression whose value is that of EXPR,
1349    converted to type TYPE.  The TREE_TYPE of the value
1350    is always TYPE.  This function implements all reasonable
1351    conversions; callers should filter out those that are
1352    not permitted by the language being compiled.
1353
1354    Most of this routine is from build_reinterpret_cast.
1355
1356    The back end cannot call cp_convert (what was convert) because
1357    conversions to/from basetypes may involve memory references
1358    (vbases) and adding or subtracting small values (multiple
1359    inheritance), but it calls convert from the constant folding code
1360    on subtrees of already built trees after it has ripped them apart.
1361
1362    Also, if we ever support range variables, we'll probably also have to
1363    do a little bit more work.  */
1364
1365 tree
1366 convert (tree type, tree expr)
1367 {
1368   tree intype;
1369
1370   if (type == error_mark_node || expr == error_mark_node)
1371     return error_mark_node;
1372
1373   intype = TREE_TYPE (expr);
1374
1375   if (POINTER_TYPE_P (type) && POINTER_TYPE_P (intype))
1376     return fold_if_not_in_template (build_nop (type, expr));
1377
1378   return ocp_convert (type, expr, CONV_OLD_CONVERT,
1379                       LOOKUP_NORMAL|LOOKUP_NO_CONVERSION);
1380 }
1381
1382 /* Like cp_convert, except permit conversions to take place which
1383    are not normally allowed due to access restrictions
1384    (such as conversion from sub-type to private super-type).  */
1385
1386 tree
1387 convert_force (tree type, tree expr, int convtype)
1388 {
1389   tree e = expr;
1390   enum tree_code code = TREE_CODE (type);
1391
1392   if (code == REFERENCE_TYPE)
1393     return (fold_if_not_in_template
1394             (convert_to_reference (type, e, CONV_C_CAST, LOOKUP_COMPLAIN,
1395                                    NULL_TREE)));
1396
1397   if (code == POINTER_TYPE)
1398     return fold_if_not_in_template (convert_to_pointer_force (type, e));
1399
1400   /* From typeck.c convert_for_assignment */
1401   if (((TREE_CODE (TREE_TYPE (e)) == POINTER_TYPE && TREE_CODE (e) == ADDR_EXPR
1402         && TREE_CODE (TREE_TYPE (e)) == POINTER_TYPE
1403         && TREE_CODE (TREE_TYPE (TREE_TYPE (e))) == METHOD_TYPE)
1404        || integer_zerop (e)
1405        || TYPE_PTRMEMFUNC_P (TREE_TYPE (e)))
1406       && TYPE_PTRMEMFUNC_P (type))
1407     /* compatible pointer to member functions.  */
1408     return build_ptrmemfunc (TYPE_PTRMEMFUNC_FN_TYPE (type), e, 1,
1409                              /*c_cast_p=*/1, tf_warning_or_error);
1410
1411   return ocp_convert (type, e, CONV_C_CAST|convtype, LOOKUP_NORMAL);
1412 }
1413
1414 /* Convert an aggregate EXPR to type XTYPE.  If a conversion
1415    exists, return the attempted conversion.  This may
1416    return ERROR_MARK_NODE if the conversion is not
1417    allowed (references private members, etc).
1418    If no conversion exists, NULL_TREE is returned.
1419
1420    FIXME: Ambiguity checking is wrong.  Should choose one by the implicit
1421    object parameter, or by the second standard conversion sequence if
1422    that doesn't do it.  This will probably wait for an overloading rewrite.
1423    (jason 8/9/95)  */
1424
1425 static tree
1426 build_type_conversion (tree xtype, tree expr)
1427 {
1428   /* C++: check to see if we can convert this aggregate type
1429      into the required type.  */
1430   return build_user_type_conversion (xtype, expr, LOOKUP_NORMAL);
1431 }
1432
1433 /* Convert the given EXPR to one of a group of types suitable for use in an
1434    expression.  DESIRES is a combination of various WANT_* flags (q.v.)
1435    which indicates which types are suitable.  If COMPLAIN is true, complain
1436    about ambiguity; otherwise, the caller will deal with it.  */
1437
1438 tree
1439 build_expr_type_conversion (int desires, tree expr, bool complain)
1440 {
1441   tree basetype = TREE_TYPE (expr);
1442   tree conv = NULL_TREE;
1443   tree winner = NULL_TREE;
1444
1445   if (expr == null_node
1446       && (desires & WANT_INT)
1447       && !(desires & WANT_NULL))
1448     warning_at (input_location, OPT_Wconversion_null,
1449                 "converting NULL to non-pointer type");
1450
1451   basetype = TREE_TYPE (expr);
1452
1453   if (basetype == error_mark_node)
1454     return error_mark_node;
1455
1456   if (! MAYBE_CLASS_TYPE_P (basetype))
1457     switch (TREE_CODE (basetype))
1458       {
1459       case INTEGER_TYPE:
1460         if ((desires & WANT_NULL) && null_ptr_cst_p (expr))
1461           return expr;
1462         /* else fall through...  */
1463
1464       case BOOLEAN_TYPE:
1465         return (desires & WANT_INT) ? expr : NULL_TREE;
1466       case ENUMERAL_TYPE:
1467         return (desires & WANT_ENUM) ? expr : NULL_TREE;
1468       case REAL_TYPE:
1469         return (desires & WANT_FLOAT) ? expr : NULL_TREE;
1470       case POINTER_TYPE:
1471         return (desires & WANT_POINTER) ? expr : NULL_TREE;
1472
1473       case FUNCTION_TYPE:
1474       case ARRAY_TYPE:
1475         return (desires & WANT_POINTER) ? decay_conversion (expr)
1476                                         : NULL_TREE;
1477
1478       case COMPLEX_TYPE:
1479       case VECTOR_TYPE:
1480         if ((desires & WANT_VECTOR_OR_COMPLEX) == 0)
1481           return NULL_TREE;
1482         switch (TREE_CODE (TREE_TYPE (basetype)))
1483           {
1484           case INTEGER_TYPE:
1485           case BOOLEAN_TYPE:
1486             return (desires & WANT_INT) ? expr : NULL_TREE;
1487           case ENUMERAL_TYPE:
1488             return (desires & WANT_ENUM) ? expr : NULL_TREE;
1489           case REAL_TYPE:
1490             return (desires & WANT_FLOAT) ? expr : NULL_TREE;
1491           default:
1492             return NULL_TREE;
1493           }
1494
1495       default:
1496         return NULL_TREE;
1497       }
1498
1499   /* The code for conversions from class type is currently only used for
1500      delete expressions.  Other expressions are handled by build_new_op.  */
1501   if (!complete_type_or_maybe_complain (basetype, expr, complain))
1502     return error_mark_node;
1503   if (!TYPE_HAS_CONVERSION (basetype))
1504     return NULL_TREE;
1505
1506   for (conv = lookup_conversions (basetype, /*lookup_template_convs_p=*/true);
1507        conv;
1508        conv = TREE_CHAIN (conv))
1509     {
1510       int win = 0;
1511       tree candidate;
1512       tree cand = TREE_VALUE (conv);
1513       cand = OVL_CURRENT (cand);
1514
1515       if (winner && winner == cand)
1516         continue;
1517
1518       if (DECL_NONCONVERTING_P (cand))
1519         continue;
1520
1521       candidate = non_reference (TREE_TYPE (TREE_TYPE (cand)));
1522
1523       switch (TREE_CODE (candidate))
1524         {
1525         case BOOLEAN_TYPE:
1526         case INTEGER_TYPE:
1527           win = (desires & WANT_INT); break;
1528         case ENUMERAL_TYPE:
1529           win = (desires & WANT_ENUM); break;
1530         case REAL_TYPE:
1531           win = (desires & WANT_FLOAT); break;
1532         case POINTER_TYPE:
1533           win = (desires & WANT_POINTER); break;
1534
1535         case COMPLEX_TYPE:
1536         case VECTOR_TYPE:
1537           if ((desires & WANT_VECTOR_OR_COMPLEX) == 0)
1538             break;
1539           switch (TREE_CODE (TREE_TYPE (candidate)))
1540             {
1541             case BOOLEAN_TYPE:
1542             case INTEGER_TYPE:
1543               win = (desires & WANT_INT); break;
1544             case ENUMERAL_TYPE:
1545               win = (desires & WANT_ENUM); break;
1546             case REAL_TYPE:
1547               win = (desires & WANT_FLOAT); break;
1548             default:
1549               break;
1550             }
1551           break;
1552
1553         default:
1554           break;
1555         }
1556
1557       if (win)
1558         {
1559           if (winner)
1560             {
1561               if (complain)
1562                 {
1563                   error ("ambiguous default type conversion from %qT",
1564                          basetype);
1565                   error ("  candidate conversions include %qD and %qD",
1566                          winner, cand);
1567                 }
1568               return error_mark_node;
1569             }
1570           else
1571             winner = cand;
1572         }
1573     }
1574
1575   if (winner)
1576     {
1577       tree type = non_reference (TREE_TYPE (TREE_TYPE (winner)));
1578       return build_user_type_conversion (type, expr, LOOKUP_NORMAL);
1579     }
1580
1581   return NULL_TREE;
1582 }
1583
1584 /* Implements integral promotion (4.1) and float->double promotion.  */
1585
1586 tree
1587 type_promotes_to (tree type)
1588 {
1589   tree promoted_type;
1590
1591   if (type == error_mark_node)
1592     return error_mark_node;
1593
1594   type = TYPE_MAIN_VARIANT (type);
1595
1596   /* Check for promotions of target-defined types first.  */
1597   promoted_type = targetm.promoted_type (type);
1598   if (promoted_type)
1599     return promoted_type;
1600
1601   /* bool always promotes to int (not unsigned), even if it's the same
1602      size.  */
1603   if (TREE_CODE (type) == BOOLEAN_TYPE)
1604     type = integer_type_node;
1605
1606   /* Normally convert enums to int, but convert wide enums to something
1607      wider.  */
1608   else if (TREE_CODE (type) == ENUMERAL_TYPE
1609            || type == char16_type_node
1610            || type == char32_type_node
1611            || type == wchar_type_node)
1612     {
1613       int precision = MAX (TYPE_PRECISION (type),
1614                            TYPE_PRECISION (integer_type_node));
1615       tree totype = c_common_type_for_size (precision, 0);
1616       if (TREE_CODE (type) == ENUMERAL_TYPE)
1617         type = ENUM_UNDERLYING_TYPE (type);
1618       if (TYPE_UNSIGNED (type)
1619           && ! int_fits_type_p (TYPE_MAX_VALUE (type), totype))
1620         type = c_common_type_for_size (precision, 1);
1621       else
1622         type = totype;
1623     }
1624   else if (c_promoting_integer_type_p (type))
1625     {
1626       /* Retain unsignedness if really not getting bigger.  */
1627       if (TYPE_UNSIGNED (type)
1628           && TYPE_PRECISION (type) == TYPE_PRECISION (integer_type_node))
1629         type = unsigned_type_node;
1630       else
1631         type = integer_type_node;
1632     }
1633   else if (type == float_type_node)
1634     type = double_type_node;
1635
1636   return type;
1637 }
1638
1639 /* The routines below this point are carefully written to conform to
1640    the standard.  They use the same terminology, and follow the rules
1641    closely.  Although they are used only in pt.c at the moment, they
1642    should presumably be used everywhere in the future.  */
1643
1644 /* Attempt to perform qualification conversions on EXPR to convert it
1645    to TYPE.  Return the resulting expression, or error_mark_node if
1646    the conversion was impossible.  */
1647
1648 tree
1649 perform_qualification_conversions (tree type, tree expr)
1650 {
1651   tree expr_type;
1652
1653   expr_type = TREE_TYPE (expr);
1654
1655   if (same_type_p (type, expr_type))
1656     return expr;
1657   else if (TYPE_PTR_P (type) && TYPE_PTR_P (expr_type)
1658            && comp_ptr_ttypes (TREE_TYPE (type), TREE_TYPE (expr_type)))
1659     return build_nop (type, expr);
1660   else if (TYPE_PTR_TO_MEMBER_P (type)
1661            && TYPE_PTR_TO_MEMBER_P (expr_type)
1662            && same_type_p (TYPE_PTRMEM_CLASS_TYPE (type),
1663                            TYPE_PTRMEM_CLASS_TYPE (expr_type))
1664            && comp_ptr_ttypes (TYPE_PTRMEM_POINTED_TO_TYPE (type),
1665                                TYPE_PTRMEM_POINTED_TO_TYPE (expr_type)))
1666     return build_nop (type, expr);
1667   else
1668     return error_mark_node;
1669 }