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