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