OSDN Git Service

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