OSDN Git Service

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