OSDN Git Service

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