OSDN Git Service

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