home *** CD-ROM | disk | FTP | other *** search
/ Education Sampler 1992 [NeXTSTEP] / Education_1992_Sampler.iso / NeXT / GnuSource / cc-61.0.1 / cc / cplus-cvt.c < prev    next >
C/C++ Source or Header  |  1991-06-03  |  56KB  |  1,852 lines

  1. /* Language-level data type conversion for GNU C++.
  2.    Copyright (C) 1987, 1988 Free Software Foundation, Inc.
  3.    Hacked by Michael Tiemann (tiemann@cygnus.com)
  4.  
  5. This file is part of GNU CC.
  6.  
  7. GNU CC is free software; you can redistribute it and/or modify
  8. it under the terms of the GNU General Public License as published by
  9. the Free Software Foundation; either version 2, or (at your option)
  10. any later version.
  11.  
  12. GNU CC is distributed in the hope that it will be useful,
  13. but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  15. GNU General Public License for more details.
  16.  
  17. You should have received a copy of the GNU General Public License
  18. along with GNU CC; see the file COPYING.  If not, write to
  19. the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.  */
  20.  
  21.  
  22. /* This file contains the functions for converting C expressions
  23.    to different data types.  The only entry point is `convert'.
  24.    Every language front end must have a `convert' function
  25.    but what kind of conversions it does will depend on the language.  */
  26.  
  27. #include "config.h"
  28. #include "tree.h"
  29. #include "cplus-tree.h"
  30. #include "assert.h"
  31.  
  32. #define NULL 0
  33.  
  34. static tree build_up_reference ();
  35.  
  36. /* Change of width--truncation and extension of integers or reals--
  37.    is represented with NOP_EXPR.  Proper functioning of many things
  38.    assumes that no other conversions can be NOP_EXPRs.
  39.  
  40.    Conversion between integer and pointer is represented with CONVERT_EXPR.
  41.    Converting integer to real uses FLOAT_EXPR
  42.    and real to integer uses FIX_TRUNC_EXPR.
  43.  
  44.    Here is a list of all the functions that assume that widening and
  45.    narrowing is always done with a NOP_EXPR:
  46.      In c-convert.c, convert_to_integer.
  47.      In c-typeck.c, build_binary_op_nodefault (boolean ops),
  48.         and truthvalue_conversion.
  49.      In expr.c: expand_expr, for operands of a MULT_EXPR.
  50.      In fold-const.c: fold.
  51.      In tree.c: get_narrower and get_unwidened.
  52.  
  53.    C++: in multiple-inheritance, converting between pointers may involve
  54.    adjusting them by a delta stored within the class definition.  */
  55.  
  56. /* Subroutines of `convert'.  */
  57.  
  58. static tree
  59. convert_to_pointer (type, expr)
  60.      tree type, expr;
  61. {
  62.   register tree intype = TREE_TYPE (expr);
  63.   register enum tree_code form = TREE_CODE (intype);
  64.   
  65.   if (integer_zerop (expr))
  66.     {
  67.       if (type == TREE_TYPE (null_pointer_node))
  68.     return null_pointer_node;
  69.       expr = build_int_2 (0, 0);
  70.       TREE_TYPE (expr) = type;
  71.       return expr;
  72.     }
  73.  
  74.   if (form == POINTER_TYPE)
  75.     {
  76.       intype = TYPE_MAIN_VARIANT (intype);
  77.  
  78.       if (TYPE_MAIN_VARIANT (type) != intype
  79.       && IS_AGGR_TYPE_2 (TREE_TYPE (type), TREE_TYPE (intype)))
  80.     {
  81.       enum tree_code code = PLUS_EXPR;
  82.       tree basetype = get_base_type (TREE_TYPE (TYPE_MAIN_VARIANT (type)),
  83.                      TREE_TYPE (intype), 1);
  84.       if (basetype == error_mark_node)
  85.         return error_mark_node;
  86.       if (basetype == NULL_TREE)
  87.         {
  88.           basetype = get_base_type (TREE_TYPE (intype),
  89.                     TREE_TYPE (TYPE_MAIN_VARIANT (type)), 1);
  90.           if (basetype == error_mark_node)
  91.         return error_mark_node;
  92.           code = MINUS_EXPR;
  93.         }
  94.       if (basetype)
  95.         {
  96.           if (TYPE_USES_VIRTUAL_BASECLASSES (TREE_TYPE (type))
  97.           || TYPE_USES_VIRTUAL_BASECLASSES (TREE_TYPE (intype))
  98.           || ! CLASSTYPE_OFFSET_ZEROP (basetype))
  99.         {
  100.           /* Need to get the path we took.  */
  101.           tree path;
  102.  
  103.           if (code == PLUS_EXPR)
  104.             get_base_distance (TREE_TYPE (type), TREE_TYPE (intype), 0, &path);
  105.           else
  106.             get_base_distance (TREE_TYPE (intype), TREE_TYPE (type), 0, &path);
  107.           return build_vbase_path (code, type, expr, path, 0);
  108.         }
  109.         }
  110.     }
  111.       return build1 (NOP_EXPR, type, expr);
  112.     }
  113.  
  114.   if (form == INTEGER_TYPE || form == ENUMERAL_TYPE)
  115.     {
  116.       if (type_precision (intype) == POINTER_SIZE)
  117.     return build1 (CONVERT_EXPR, type, expr);
  118.       return convert_to_pointer (type,
  119.                  convert (type_for_size (POINTER_SIZE, 0),
  120.                       expr));
  121.     }
  122.  
  123.   assert (form != OFFSET_TYPE);
  124.  
  125.   if (IS_AGGR_TYPE (intype))
  126.     {
  127.       /* If we cannot convert to the specific pointer type,
  128.      try to convert to the type `void *'.  */
  129.       tree rval;
  130.       rval = build_type_conversion (CONVERT_EXPR, type, expr, 1);
  131.       if (rval)
  132.     {
  133.       if (rval == error_mark_node)
  134.         error ("ambiguous pointer conversion");
  135.       return rval;
  136.     }
  137.     }
  138.  
  139.   error ("cannot convert to a pointer type");
  140.  
  141.   return null_pointer_node;
  142. }
  143.  
  144. /* Like convert, except permit conversions to take place which
  145.    are not normally allowed due to visibility restrictions
  146.    (such as conversion from sub-type to private super-type).  */
  147. static tree
  148. convert_to_pointer_force (type, expr)
  149.      tree type, expr;
  150. {
  151.   register tree intype = TREE_TYPE (expr);
  152.   register enum tree_code form = TREE_CODE (intype);
  153.   
  154.   if (integer_zerop (expr))
  155.     {
  156.       if (type == TREE_TYPE (null_pointer_node))
  157.     return null_pointer_node;
  158.       expr = build_int_2 (0, 0);
  159.       TREE_TYPE (expr) = type;
  160.       return expr;
  161.     }
  162.  
  163.   if (form == POINTER_TYPE)
  164.     {
  165.       intype = TYPE_MAIN_VARIANT (intype);
  166.  
  167.       if (TYPE_MAIN_VARIANT (type) != intype
  168.       && IS_AGGR_TYPE_2 (TREE_TYPE (type), TREE_TYPE (intype)))
  169.     {
  170.       enum tree_code code = PLUS_EXPR;
  171.       tree path, basetype;
  172.       int distance = get_base_distance (TREE_TYPE (type),
  173.                         TYPE_MAIN_VARIANT (TREE_TYPE (intype)), 0, &path);
  174.       if (distance == -2)
  175.         {
  176.         ambig:
  177.           error_with_aggr_type (TREE_TYPE (type), "type `%s' is ambiguous baseclass of `%s'",
  178.                     TYPE_NAME_STRING (TREE_TYPE (intype)));
  179.           return error_mark_node;
  180.         }
  181.       if (distance == -1)
  182.         {
  183.           distance = get_base_distance (TREE_TYPE (intype),
  184.                         TYPE_MAIN_VARIANT (TREE_TYPE (type)), 0, &path);
  185.           if (distance == -2)
  186.         goto ambig;
  187.           if (distance < 0)
  188.         /* Doesn't need any special help from us.  */
  189.         return build1 (NOP_EXPR, type, expr);
  190.  
  191.           code = MINUS_EXPR;
  192.         }
  193.       return build_vbase_path (code, type, expr, path, 0);
  194.     }
  195.       return build1 (NOP_EXPR, type, expr);
  196.     }
  197.  
  198.   return convert_to_pointer (type, expr);
  199. }
  200.  
  201. /* We are passing something to a function which requires a reference.
  202.    The type we are interested in is in TYPE. The initial
  203.    value we have to begin with is in ARG.
  204.  
  205.    FLAGS controls how we manage visibility checking.  */
  206. static tree
  207. build_up_reference (type, arg, flags)
  208.      tree type, arg;
  209.      int flags;
  210. {
  211.   tree rval;
  212.   int literal_flag = 0;
  213.   tree argtype = TREE_TYPE (arg), basetype = argtype;
  214.   tree target_type = TREE_TYPE (type);
  215.  
  216.   assert (TREE_CODE (type) == REFERENCE_TYPE);
  217.   if (TYPE_MAIN_VARIANT (argtype) != TYPE_MAIN_VARIANT (target_type)
  218.       && IS_AGGR_TYPE (argtype)
  219.       && IS_AGGR_TYPE (target_type))
  220.     {
  221.       basetype = get_base_type (target_type, TYPE_MAIN_VARIANT (argtype),
  222.                 (flags & LOOKUP_PROTECTED_OK) ? 3 : 2);
  223.       if ((flags & LOOKUP_PROTECT) && basetype == error_mark_node)
  224.     return error_mark_node;
  225.       if (basetype == NULL_TREE)
  226.     {
  227.       error_not_base_type (target_type, argtype);
  228.       return error_mark_node;
  229.     }
  230.     }
  231.  
  232.   switch (TREE_CODE (arg))
  233.     {
  234.     case INDIRECT_REF:
  235.       /* This is a call to a constructor which did not know what it was
  236.      initializing until now: it needs to initialize a temporary.  */
  237.       if (TREE_HAS_CONSTRUCTOR (arg))
  238.     {
  239.       tree temp = build_cplus_new (argtype, TREE_OPERAND (arg, 0), 1);
  240.       TREE_HAS_CONSTRUCTOR (arg) = 0;
  241.       return build_up_reference (type, temp, flags);
  242.     }
  243.       /* Let &* cancel out to simplify resulting code.
  244.          Also, throw away intervening NOP_EXPRs.  */
  245.       arg = TREE_OPERAND (arg, 0);
  246.       if (TREE_CODE (arg) == NOP_EXPR || TREE_CODE (arg) == NON_LVALUE_EXPR
  247.       || (TREE_CODE (arg) == CONVERT_EXPR && TREE_REFERENCE_EXPR (arg)))
  248.     arg = TREE_OPERAND (arg, 0);
  249.  
  250.       rval = build1 (CONVERT_EXPR, type, arg);
  251.       TREE_REFERENCE_EXPR (rval) = 1;
  252.       literal_flag = TREE_CONSTANT (arg);
  253.       goto done;
  254.  
  255.       /* Get this out of a register if we happened to be in one by accident.
  256.      Also, build up references to non-lvalues it we must.  */
  257.       /* For &x[y], return (&) x+y */
  258.     case ARRAY_REF:
  259.       if (mark_addressable (TREE_OPERAND (arg, 0)) == 0)
  260.     return error_mark_node;
  261.       rval = build_binary_op (PLUS_EXPR, TREE_OPERAND (arg, 0),
  262.                   TREE_OPERAND (arg, 1));
  263.       TREE_TYPE (rval) = type;
  264.       if (TREE_CONSTANT (TREE_OPERAND (arg, 1))
  265.       && staticp (TREE_OPERAND (arg, 0)))
  266.     TREE_CONSTANT (rval) = 1;
  267.       return rval;
  268.  
  269.     case SCOPE_REF:
  270.       /* Could be a reference to a static member.  */
  271.       {
  272.     tree field = TREE_OPERAND (arg, 1);
  273.     if (TREE_STATIC (field))
  274.       {
  275.         rval = build1 (ADDR_EXPR, type, field);
  276.         literal_flag = 1;
  277.         goto done;
  278.       }
  279.       }
  280.       /* we should have farmed out member pointers above.  */
  281.       assert (0);
  282.  
  283.     case COMPONENT_REF:
  284.       rval = build_component_addr (arg, build_pointer_type (argtype),
  285.                    "attempt to make a reference to bit-field structure member `%s'");
  286.       TREE_TYPE (rval) = type;
  287.       literal_flag = staticp (TREE_OPERAND (arg, 0));
  288. #if 0
  289.       goto done_but_maybe_warn;
  290. #else
  291.       goto done;
  292. #endif
  293.  
  294.       /* Anything not already handled and not a true memory reference
  295.      needs to have a reference built up.  Do so silently for
  296.      things like integers and return values from function,
  297.      but complain if we need a reference to something declared
  298.      as `register'.  */
  299.  
  300.     case RESULT_DECL:
  301.       if (staticp (arg))
  302.     literal_flag = 1;
  303.       TREE_ADDRESSABLE (arg) = 1;
  304.       put_var_into_stack (arg);
  305.       break;
  306.  
  307.     case PARM_DECL:
  308.       if (arg == current_class_decl)
  309.     {
  310.       error ("address of `this' not available");
  311.       TREE_ADDRESSABLE (arg) = 1; /* so compiler doesn't die later */
  312.       put_var_into_stack (arg);
  313.       break;
  314.     }
  315.       /* Fall through.  */
  316.     case VAR_DECL:
  317.     case CONST_DECL:
  318.       if (TREE_REGDECL (arg) && !TREE_ADDRESSABLE (arg))
  319.     warning ("address needed to build reference for `%s', which is declared `register'",
  320.          IDENTIFIER_POINTER (DECL_NAME (arg)));
  321.       else if (staticp (arg))
  322.     literal_flag = 1;
  323.  
  324.       TREE_ADDRESSABLE (arg) = 1;
  325.       put_var_into_stack (arg);
  326.       break;
  327.  
  328.     case COMPOUND_EXPR:
  329.       {
  330.     tree real_reference = build_up_reference (type, TREE_OPERAND (arg, 1), 1);
  331.     rval = build (COMPOUND_EXPR, type, TREE_OPERAND (arg, 0), real_reference);
  332.     TREE_CONSTANT (rval) = staticp (TREE_OPERAND (arg, 1));
  333.     return rval;
  334.       }
  335.  
  336.     case MODIFY_EXPR:
  337.     case INIT_EXPR:
  338.       {
  339.     tree real_reference = build_up_reference (type, TREE_OPERAND (arg, 0), 1);
  340.     rval = build (COMPOUND_EXPR, type, arg, real_reference);
  341.     TREE_CONSTANT (rval) = staticp (TREE_OPERAND (arg, 0));
  342.     return rval;
  343.       }
  344.  
  345.     case COND_EXPR:
  346.       return build (COND_EXPR, type,
  347.             TREE_OPERAND (arg, 0),
  348.             build_up_reference (type, TREE_OPERAND (arg, 1), 1),
  349.             build_up_reference (type, TREE_OPERAND (arg, 2), 1));
  350.  
  351.     case WITH_CLEANUP_EXPR:
  352.       rval = build (WITH_CLEANUP_EXPR, type,
  353.             build_up_reference (type, TREE_OPERAND (arg, 0), 1),
  354.             0, TREE_OPERAND (arg, 2));
  355.       return rval;
  356.  
  357.     case BIND_EXPR:
  358.       arg = TREE_OPERAND (arg, 1);
  359.       if (arg == NULL_TREE)
  360.     {
  361.       compiler_error ("({ ... }) expression not expanded when needed for reference");
  362.       return error_mark_node;
  363.     }
  364.       rval = build1 (ADDR_EXPR, type, arg);
  365.       TREE_REFERENCE_EXPR (rval) = 1;
  366.       return rval;
  367.  
  368.     default:
  369.       break;
  370.     }
  371.  
  372.   if (TREE_ADDRESSABLE (arg) == 0)
  373.     {
  374.       tree temp;
  375.  
  376.       if (TREE_CODE (arg) == CALL_EXPR && IS_AGGR_TYPE (argtype))
  377.     {
  378.       temp = build_cplus_new (argtype, arg, 1);
  379.       rval = build1 (ADDR_EXPR, type, temp);
  380.       goto done;
  381.     }
  382.       else
  383.     {
  384.       temp = get_temp_name (argtype, 0);
  385.       if (global_bindings_p ())
  386.         {
  387.           /* Give this new temp some rtl and initialize it.  */
  388.           DECL_INITIAL (temp) = arg;
  389.           TREE_STATIC (temp) = 1;
  390.           finish_decl (temp, arg, NULL_TREE);
  391.           /* Do this after declaring it static.  */
  392.           rval = build_unary_op (ADDR_EXPR, temp, 0);
  393.           literal_flag = TREE_CONSTANT (rval);
  394.           goto done;
  395.         }
  396.       else
  397.         {
  398.           rval = build_unary_op (ADDR_EXPR, temp, 0);
  399.           /* Put a value into the rtl.  */
  400.           if (IS_AGGR_TYPE (argtype))
  401.         {
  402.           /* This may produce surprising results,
  403.              since we commit to initializing the temp
  404.              when the temp may not actually get used.  */
  405.           expand_aggr_init (temp, arg, 0);
  406.           TREE_TYPE (rval) = type;
  407.           literal_flag = TREE_CONSTANT (rval);
  408.           goto done;
  409.         }
  410.           else
  411.         {
  412.           if (basetype != argtype)
  413.             rval = convert_pointer_to (target_type, rval);
  414.           else
  415.             TREE_TYPE (rval) = type;
  416.           return build (COMPOUND_EXPR, type,
  417.                 build (MODIFY_EXPR, argtype, temp, arg), rval);
  418.         }
  419.         }
  420.     }
  421.     }
  422.   else
  423.     rval = build1 (ADDR_EXPR, type, arg);
  424.  
  425.  done_but_maybe_warn:
  426.   if (TREE_READONLY (arg)
  427.       && ! TYPE_READONLY (target_type))
  428.     readonly_warning_or_error (arg, "conversion to reference");
  429.  
  430.  done:
  431.   if (TYPE_USES_COMPLEX_INHERITANCE (argtype))
  432.     {
  433.       TREE_TYPE (rval) = TYPE_POINTER_TO (argtype);
  434.       rval = convert_pointer_to (target_type, rval);
  435.       TREE_TYPE (rval) = type;
  436.     }
  437.   TREE_CONSTANT (rval) = literal_flag;
  438.   return rval;
  439. }
  440.  
  441. /* For C++: Only need to do one-level references, but cannot
  442.    get tripped up on signed/unsigned differences.
  443.  
  444.    If DECL is NULL_TREE it means convert as though casting (by force).
  445.    If it is ERROR_MARK_NODE, it means the conversion is implicit,
  446.    and that temporaries may be created.
  447.    Otherwise, DECL is a _DECL node which can be used in error reporting.  */
  448. tree
  449. convert_to_reference (decl, reftype, expr, strict, flags)
  450.      tree decl;
  451.      tree reftype, expr;
  452.      int strict, flags;
  453. {
  454.   register tree type = TYPE_MAIN_VARIANT (TREE_TYPE (reftype));
  455.   register tree intype = TREE_TYPE (expr);
  456.   register enum tree_code form = TREE_CODE (intype);
  457.  
  458.   assert (TREE_CODE (reftype) == REFERENCE_TYPE);
  459.  
  460.   if (form == REFERENCE_TYPE)
  461.     intype = TREE_TYPE (intype);
  462.   intype = TYPE_MAIN_VARIANT (intype);
  463.  
  464.   /* @@ Probably need to have a check for X(X&) here.  */
  465.  
  466.   if (IS_AGGR_TYPE (intype))
  467.     {
  468.       tree rval = build_type_conversion (CONVERT_EXPR, reftype, expr, 1);
  469.       if (rval)
  470.     {
  471.       if (rval == error_mark_node)
  472.         error ("ambiguous pointer conversion");
  473.       return rval;
  474.     }
  475.       else if (rval = build_type_conversion (CONVERT_EXPR, type, expr, 1))
  476.     {
  477.       if (TYPE_NEEDS_DESTRUCTOR (type))
  478.         rval = cleanup_after_call (rval);
  479.       else
  480.         {
  481.           decl = get_temp_name (type, 0);
  482.           rval = build (INIT_EXPR, type, decl, rval);
  483.           rval = build (COMPOUND_EXPR, reftype, rval,
  484.                 convert_to_reference (NULL_TREE, reftype, decl,
  485.                           strict, flags));
  486.  
  487.         }
  488.       return rval;
  489.     }
  490.  
  491.       if (form == REFERENCE_TYPE
  492.       && type != intype
  493.       && TYPE_USES_COMPLEX_INHERITANCE (intype))
  494.     {
  495.       /* If it may move around, build a fresh reference.  */
  496.       expr = convert_from_reference (expr);
  497.       form = TREE_CODE (TREE_TYPE (expr));
  498.     }
  499.     }
  500.  
  501.   /* @@ Perhaps this should try to go through a constructor first
  502.      @@ for proper initialization, but I am not sure when that
  503.      @@ is needed or desirable.
  504.  
  505.      @@ The second disjunct is provided to make references behave
  506.      @@ as some people think they should, i.e., an interconvertability
  507.      @@ between references to builtin types (such as short and
  508.      @@ unsigned short).  There should be no conversion between
  509.      @@ types whose codes are different, or whose sizes are different.  */
  510.  
  511.   if (((IS_AGGR_TYPE (type) || IS_AGGR_TYPE (intype))
  512.        && comptypes (type, intype, strict))
  513.       || (!IS_AGGR_TYPE (type)
  514.       && TREE_CODE (type) == TREE_CODE (intype)
  515.       && int_size_in_bytes (type) == int_size_in_bytes (intype)))
  516.     {
  517.       /* If EXPR is of aggregate type, and is really a CALL_EXPR,
  518.      then we don't need to convert it to reference type if
  519.      it is only being used to initialize DECL which is also
  520.      of the same aggregate type.  */
  521.       if (form == REFERENCE_TYPE
  522.       || (decl != NULL_TREE && decl != error_mark_node
  523.           && IS_AGGR_TYPE (type)
  524.           && TREE_CODE (expr) == CALL_EXPR
  525.           && TYPE_MAIN_VARIANT (type) == intype))
  526.     {
  527.       if (decl && decl != error_mark_node)
  528.         {
  529.           tree e1 = build (INIT_EXPR, void_type_node, decl, expr);
  530.           tree e2;
  531.  
  532.           TREE_SIDE_EFFECTS (e1) = 1;
  533.           if (form == REFERENCE_TYPE)
  534.         e2 = build1 (NOP_EXPR, reftype, decl);
  535.           else
  536.         {
  537.           e2 = build_unary_op (ADDR_EXPR, decl, 0);
  538.           TREE_TYPE (e2) = reftype;
  539.           TREE_REFERENCE_EXPR (e2) = 1;
  540.         }
  541.           return build_compound_expr (tree_cons (NULL_TREE, e1,
  542.                              build_tree_list (NULL_TREE, e2)));
  543.         }
  544.       expr = copy_node (expr);
  545.       TREE_TYPE (expr) = reftype;
  546.       return expr;
  547.     }
  548.       if (decl == error_mark_node)
  549.     flags |= LOOKUP_PROTECTED_OK;
  550.       return build_up_reference (reftype, expr, flags);
  551.     }
  552.  
  553.   /* Definitely need to go through a constructor here.  */
  554.   if (TYPE_HAS_CONSTRUCTOR (type))
  555.     {
  556.       tree init = build_method_call (NULL_TREE, TYPE_IDENTIFIER (type), build_tree_list (NULL_TREE, expr), CLASSTYPE_AS_LIST (type), LOOKUP_NORMAL);
  557.       tree rval;
  558.  
  559.       if (init == error_mark_node)
  560.     return error_mark_node;
  561.       rval = build_cplus_new (type, init, 1);
  562.       if (decl == error_mark_node)
  563.     flags |= LOOKUP_PROTECTED_OK;
  564.       return build_up_reference (reftype, rval, flags);
  565.     }
  566.  
  567.   assert (form != OFFSET_TYPE);
  568.  
  569.   error ("cannot convert to a reference type");
  570.  
  571.   return error_mark_node;
  572. }
  573.  
  574. /* We are using a reference VAL for its value. Bash that reference all the
  575.    way down to its lowest form. */
  576. tree
  577. convert_from_reference (val)
  578.      tree val;
  579. {
  580.   tree type = TREE_TYPE (val);
  581.  
  582.   if (TREE_CODE (type) == OFFSET_TYPE)
  583.     type = TREE_TYPE (type);
  584.  if (TREE_CODE (type) == REFERENCE_TYPE)
  585.     {
  586.       tree target_type = TREE_TYPE (type);
  587.  
  588.       /* This can happen if we cast to a reference type.  */
  589.       if (TREE_CODE (val) == ADDR_EXPR)
  590.     {
  591.       val = build1 (NOP_EXPR, build_pointer_type (target_type), val);
  592.       val = build_indirect_ref (val, 0);
  593.       return val;
  594.     }
  595.  
  596.       val = build1 (INDIRECT_REF, TYPE_MAIN_VARIANT (target_type), val);
  597.  
  598.       TREE_THIS_VOLATILE (val) = TYPE_VOLATILE (target_type);
  599.       TREE_SIDE_EFFECTS (val) = TYPE_VOLATILE (target_type);
  600.       TREE_READONLY (val) = TYPE_READONLY (target_type);
  601.     }
  602.   return val;
  603. }
  604.  
  605. static tree
  606. convert_to_real (type, expr)
  607.      tree type, expr;
  608. {
  609.   register enum tree_code form = TREE_CODE (TREE_TYPE (expr));
  610.   extern int flag_float_store;
  611.  
  612.   if (form == REAL_TYPE)
  613.     return build1 (flag_float_store ? CONVERT_EXPR : NOP_EXPR,
  614.           type, expr);
  615.  
  616.   if (form == INTEGER_TYPE || form == ENUMERAL_TYPE)
  617.     return build1 (FLOAT_EXPR, type, expr);
  618.  
  619.   assert (form != OFFSET_TYPE);
  620.  
  621.   if (form == POINTER_TYPE)
  622.     error ("pointer value used where a floating point value was expected");
  623.   /* C++: check to see if we can convert this aggregate type
  624.      into the required scalar type.  */
  625.   else if (IS_AGGR_TYPE (TREE_TYPE (expr)))
  626.     {
  627.       tree rval;
  628.       rval = build_type_conversion (CONVERT_EXPR, type, expr, 1);
  629.       if (rval)
  630.     return rval;
  631.       else
  632.     error ("aggregate value used where a floating point value was expected");
  633.     }
  634.  
  635.   {
  636.     register tree tem = make_node (REAL_CST);
  637.     TREE_TYPE (tem) = type;
  638.     TREE_REAL_CST (tem) = 0;
  639.     return tem;
  640.   }
  641. }
  642.  
  643. /* The result of this is always supposed to be a newly created tree node
  644.    not in use in any existing structure.  */
  645.  
  646. static tree
  647. convert_to_integer (type, expr)
  648.      tree type, expr;
  649. {
  650.   register tree intype = TREE_TYPE (expr);
  651.   register enum tree_code form = TREE_CODE (intype);
  652.   extern tree build_binary_op_nodefault ();
  653.   extern tree build_unary_op ();
  654.  
  655.   if (form == POINTER_TYPE)
  656.     {
  657.       if (integer_zerop (expr))
  658.     expr = integer_zero_node;
  659.       else
  660.     expr = fold (build1 (CONVERT_EXPR,
  661.                  type_for_size (POINTER_SIZE, 0), expr));
  662.       intype = TREE_TYPE (expr);
  663.       form = TREE_CODE (intype);
  664.       if (intype == type)
  665.     return expr;
  666.     }
  667.  
  668.   if (form == INTEGER_TYPE || form == ENUMERAL_TYPE)
  669.     {
  670.       register int outprec = TYPE_PRECISION (type);
  671.       register int inprec = TYPE_PRECISION (intype);
  672.       register enum tree_code ex_form = TREE_CODE (expr);
  673.  
  674.       if (outprec >= inprec)
  675.     return build1 (NOP_EXPR, type, expr);
  676.  
  677. /* Here detect when we can distribute the truncation down past some arithmetic.
  678.    For example, if adding two longs and converting to an int,
  679.    we can equally well convert both to ints and then add.
  680.    For the operations handled here, such truncation distribution
  681.    is always safe.
  682.    It is desirable in these cases:
  683.    1) when truncating down to full-word from a larger size
  684.    2) when truncating takes no work.
  685.    3) when at least one operand of the arithmetic has been extended
  686.    (as by C's default conversions).  In this case we need two conversions
  687.    if we do the arithmetic as already requested, so we might as well
  688.    truncate both and then combine.  Perhaps that way we need only one.
  689.  
  690.    Note that in general we cannot do the arithmetic in a type
  691.    shorter than the desired result of conversion, even if the operands
  692.    are both extended from a shorter type, because they might overflow
  693.    if combined in that type.  The exceptions to this--the times when
  694.    two narrow values can be combined in their narrow type even to
  695.    make a wider result--are handled by "shorten" in build_binary_op.  */
  696.  
  697.       switch (ex_form)
  698.     {
  699.     case RSHIFT_EXPR:
  700.       /* We can pass truncation down through right shifting
  701.          when the shift count is a negative constant.  */
  702.       if (TREE_CODE (TREE_OPERAND (expr, 1)) != INTEGER_CST
  703.           || TREE_INT_CST_LOW (TREE_OPERAND (expr, 1)) > 0)
  704.         break;
  705.       goto trunc1;
  706.  
  707.     case LSHIFT_EXPR:
  708.       /* We can pass truncation down through left shifting
  709.          when the shift count is a positive constant.  */
  710.       if (TREE_CODE (TREE_OPERAND (expr, 1)) != INTEGER_CST
  711.           || TREE_INT_CST_LOW (TREE_OPERAND (expr, 1)) < 0)
  712.         break;
  713.       /* In this case, shifting is like multiplication.  */
  714.       goto trunc1;
  715.  
  716.     case MAX_EXPR:
  717.     case MIN_EXPR:
  718.     case MULT_EXPR:
  719.       {
  720.         tree arg0 = get_unwidened (TREE_OPERAND (expr, 0), type);
  721.         tree arg1 = get_unwidened (TREE_OPERAND (expr, 1), type);
  722.  
  723.         /* Don't distribute unless the output precision is at least as big
  724.            as the actual inputs.  Otherwise, the comparison of the
  725.            truncated values will be wrong.  */
  726.         if (outprec >= TYPE_PRECISION (TREE_TYPE (arg0))
  727.         && outprec >= TYPE_PRECISION (TREE_TYPE (arg1))
  728.         /* If signedness of arg0 and arg1 don't match,
  729.            we can't necessarily find a type to compare them in.  */
  730.         && (TREE_UNSIGNED (TREE_TYPE (arg0))
  731.             == TREE_UNSIGNED (TREE_TYPE (arg1))))
  732.           goto trunc1;
  733.         break;
  734.       }
  735.  
  736.     case PLUS_EXPR:
  737.     case MINUS_EXPR:
  738.     case BIT_AND_EXPR:
  739.     case BIT_IOR_EXPR:
  740.     case BIT_XOR_EXPR:
  741.     case BIT_ANDTC_EXPR:
  742.     trunc1:
  743.       {
  744.         tree arg0 = get_unwidened (TREE_OPERAND (expr, 0), type);
  745.         tree arg1 = get_unwidened (TREE_OPERAND (expr, 1), type);
  746.  
  747.         if (outprec >= BITS_PER_WORD
  748.         || TRULY_NOOP_TRUNCATION (outprec, inprec)
  749.         || inprec > TYPE_PRECISION (TREE_TYPE (arg0))
  750.         || inprec > TYPE_PRECISION (TREE_TYPE (arg1)))
  751.           {
  752.         /* Do the arithmetic in type TYPEX,
  753.            then convert result to TYPE.  */
  754.         register tree typex = type;
  755.  
  756.         /* Can't do arithmetic in enumeral types
  757.            so use an integer type that will hold the values.  */
  758.         if (TREE_CODE (typex) == ENUMERAL_TYPE)
  759.           typex = type_for_size (TYPE_PRECISION (typex),
  760.                      TREE_UNSIGNED (typex));
  761.  
  762.         /* But now perhaps TYPEX is as wide as INPREC.
  763.            In that case, do nothing special here.
  764.            (Otherwise would recurse infinitely in convert.  */
  765.         if (TYPE_PRECISION (typex) != inprec)
  766.           {
  767.             /* Don't do unsigned arithmetic where signed was wanted,
  768.                or vice versa.
  769.                Exception: if the original operands were unsigned
  770.                then can safely do the work as unsigned.
  771.                And we may need to do it as unsigned
  772.                if we truncate to the original size.  */
  773.             typex = ((TREE_UNSIGNED (TREE_TYPE (expr))
  774.                   || TREE_UNSIGNED (TREE_TYPE (arg0)))
  775.                  ? unsigned_type (typex) : signed_type (typex));
  776.             return convert (type,
  777.                     build_binary_op_nodefault (ex_form,
  778.                                    convert (typex, arg0),
  779.                                    convert (typex, arg1),
  780.                                    ex_form));
  781.           }
  782.           }
  783.       }
  784.       break;
  785.  
  786.     case EQ_EXPR:
  787.     case NE_EXPR:
  788.     case GT_EXPR:
  789.     case GE_EXPR:
  790.     case LT_EXPR:
  791.     case LE_EXPR:
  792.     case TRUTH_AND_EXPR:
  793.     case TRUTH_ANDIF_EXPR:
  794.     case TRUTH_OR_EXPR:
  795.     case TRUTH_ORIF_EXPR:
  796.     case TRUTH_NOT_EXPR:
  797.       /* If we want result of comparison converted to a byte,
  798.          we can just regard it as a byte, since it is 0 or 1.  */
  799.       TREE_TYPE (expr) = type;
  800.       return expr;
  801.  
  802.     case NEGATE_EXPR:
  803.     case BIT_NOT_EXPR:
  804.     case ABS_EXPR:
  805.       {
  806.         register tree typex = type;
  807.  
  808.         /* Can't do arithmetic in enumeral types
  809.            so use an integer type that will hold the values.  */
  810.         if (TREE_CODE (typex) == ENUMERAL_TYPE)
  811.           typex = type_for_size (TYPE_PRECISION (typex),
  812.                      TREE_UNSIGNED (typex));
  813.  
  814.         /* But now perhaps TYPEX is as wide as INPREC.
  815.            In that case, do nothing special here.
  816.            (Otherwise would recurse infinitely in convert.  */
  817.         if (TYPE_PRECISION (typex) != inprec)
  818.           {
  819.         /* Don't do unsigned arithmetic where signed was wanted,
  820.            or vice versa.  */
  821.         typex = (TREE_UNSIGNED (TREE_TYPE (expr))
  822.              ? unsigned_type (typex) : signed_type (typex));
  823.         return convert (type,
  824.                 build_unary_op (ex_form,
  825.                         convert (typex, TREE_OPERAND (expr, 0)),
  826.                         1));
  827.           }
  828.       }
  829.  
  830.     case NOP_EXPR:
  831.       /* If truncating after truncating, might as well do all at once.
  832.          If truncating after extending, we may get rid of wasted work.  */
  833.       return convert (type, get_unwidened (TREE_OPERAND (expr, 0), type));
  834.  
  835.     case COND_EXPR:
  836.       /* Can treat the two alternative values like the operands
  837.          of an arithmetic expression.  */
  838.       {
  839.         tree arg1 = get_unwidened (TREE_OPERAND (expr, 1), type);
  840.         tree arg2 = get_unwidened (TREE_OPERAND (expr, 2), type);
  841.  
  842.         if (outprec >= BITS_PER_WORD
  843.         || TRULY_NOOP_TRUNCATION (outprec, inprec)
  844.         || inprec > TYPE_PRECISION (TREE_TYPE (arg1))
  845.         || inprec > TYPE_PRECISION (TREE_TYPE (arg2)))
  846.           {
  847.         /* Do the arithmetic in type TYPEX,
  848.            then convert result to TYPE.  */
  849.         register tree typex = type;
  850.  
  851.         /* Can't do arithmetic in enumeral types
  852.            so use an integer type that will hold the values.  */
  853.         if (TREE_CODE (typex) == ENUMERAL_TYPE)
  854.           typex = type_for_size (TYPE_PRECISION (typex),
  855.                      TREE_UNSIGNED (typex));
  856.  
  857.         /* But now perhaps TYPEX is as wide as INPREC.
  858.            In that case, do nothing special here.
  859.            (Otherwise would recurse infinitely in convert.  */
  860.         if (TYPE_PRECISION (typex) != inprec)
  861.           {
  862.             /* Don't do unsigned arithmetic where signed was wanted,
  863.                or vice versa.  */
  864.             typex = (TREE_UNSIGNED (TREE_TYPE (expr))
  865.                  ? unsigned_type (typex) : signed_type (typex));
  866.             return convert (type,
  867.                     fold (build (COND_EXPR, typex,
  868.                          TREE_OPERAND (expr, 0),
  869.                          convert (typex, arg1),
  870.                          convert (typex, arg2))));
  871.           }
  872.           }
  873.       }
  874.  
  875.     }
  876.  
  877.       return build1 (NOP_EXPR, type, expr);
  878.     }
  879.  
  880.   if (form == REAL_TYPE)
  881.     return build1 (FIX_TRUNC_EXPR, type, expr);
  882.  
  883.   if (form == OFFSET_TYPE)
  884.     error_with_decl (TYPE_NAME (TYPE_OFFSET_BASETYPE (intype)),
  885.              "pointer-to-member expression object not composed with type `%s' object");
  886.   else
  887.     {
  888.       if (IS_AGGR_TYPE (intype))
  889.     {
  890.       tree rval;
  891.       rval = build_type_conversion (CONVERT_EXPR, type, expr, 1);
  892.       if (rval) return rval;
  893.     }
  894.  
  895.       error ("aggregate value used where an integer was expected");
  896.     }
  897.  
  898.   {
  899.     register tree tem = build_int_2 (0, 0);
  900.     TREE_TYPE (tem) = type;
  901.     return tem;
  902.   }
  903. }
  904.  
  905. /* See if there is a constructor of type TYPE which will convert
  906.    EXPR.  The reference manual seems to suggest (8.5.6) that we need
  907.    not worry about finding constructors for base classes, then converting
  908.    to the derived class.
  909.  
  910.    MSGP is a pointer to a message that would be an appropriate error
  911.    string.  If MSGP is NULL, then we are not interested in reporting
  912.    errors.  */
  913. tree
  914. convert_to_aggr (type, expr, msgp, protect)
  915.      tree type, expr;
  916.      char **msgp;
  917. {
  918.   tree basetype = TYPE_MAIN_VARIANT (type);
  919.   tree name = TYPE_IDENTIFIER (basetype);
  920.   tree function, fndecl, fntype, parmtypes, parmlist, result;
  921.   tree method_name;
  922.   enum visibility_type visibility;
  923.   int can_be_private, can_be_protected;
  924.  
  925.   if (! TYPE_HAS_CONSTRUCTOR (basetype))
  926.     {
  927.       if (msgp)
  928.     *msgp = "type `%s' does not have a constructor";
  929.       return error_mark_node;
  930.     }
  931.  
  932.   visibility = visibility_public;
  933.   can_be_private = 0;
  934.   can_be_protected = IDENTIFIER_CLASS_VALUE (name) || name == current_class_name;
  935.  
  936.   parmlist = build_tree_list (NULL_TREE, expr);
  937.   parmtypes = tree_cons (NULL_TREE, TREE_TYPE (expr), void_list_node);
  938.  
  939.   if (TYPE_USES_VIRTUAL_BASECLASSES (basetype))
  940.     {
  941.       parmtypes = tree_cons (NULL_TREE, integer_type_node, parmtypes);
  942.       parmlist = tree_cons (NULL_TREE, integer_one_node, parmlist);
  943.     }
  944.  
  945.   /* The type of the first argument will be filled in inside the loop.  */
  946.   parmlist = tree_cons (NULL_TREE, integer_zero_node, parmlist);
  947.   parmtypes = tree_cons (NULL_TREE, TYPE_POINTER_TO (basetype), parmtypes);
  948.  
  949.   method_name = build_decl_overload (IDENTIFIER_POINTER (name), parmtypes, 1);
  950.  
  951.   /* constructors are up front.  */
  952.   fndecl = TREE_VEC_ELT (CLASSTYPE_METHOD_VEC (basetype), 0);
  953.   if (TYPE_HAS_DESTRUCTOR (basetype))
  954.     fndecl = DECL_CHAIN (fndecl);
  955.  
  956.   while (fndecl)
  957.     {
  958.       if (DECL_NAME (fndecl) == method_name)
  959.     {
  960.       function = fndecl;
  961.       if (protect)
  962.         {
  963.           if (TREE_PRIVATE (fndecl))
  964.         {
  965.           can_be_private =
  966.             (basetype == current_class_type
  967.              || is_friend (basetype, current_function_decl)
  968.              || purpose_member (basetype, DECL_VISIBILITY (fndecl)));
  969.           if (! can_be_private)
  970.             goto found;
  971.         }
  972.           else if (TREE_PROTECTED (fndecl))
  973.         {
  974.           if (! can_be_protected)
  975.             goto found;
  976.         }
  977.         }
  978.       goto found_and_ok;
  979.     }
  980.       fndecl = DECL_CHAIN (fndecl);
  981.     }
  982.  
  983.   /* No exact conversion was found.  See if an approximate
  984.      one will do.  */
  985.   fndecl = TREE_VEC_ELT (CLASSTYPE_METHOD_VEC (basetype), 0);
  986.   if (TYPE_HAS_DESTRUCTOR (basetype))
  987.     fndecl = DECL_CHAIN (fndecl);
  988.  
  989.   {
  990.     int saw_private = 0;
  991.     int saw_protected = 0;
  992.     struct candidate *candidates =
  993.       (struct candidate *) alloca ((decl_list_length (fndecl)+1) * sizeof (struct candidate));
  994.     struct candidate *cp = candidates;
  995.  
  996.     while (fndecl)
  997.       {
  998.     function = fndecl;
  999.     cp->harshness = (unsigned short *)alloca (3 * sizeof (short));
  1000.     compute_conversion_costs (fndecl, parmlist, cp, 2);
  1001.     if (cp->evil == 0)
  1002.       {
  1003.         cp->u.field = fndecl;
  1004.         if (protect)
  1005.           {
  1006.         if (TREE_PRIVATE (fndecl))
  1007.           visibility = visibility_private;
  1008.         else if (TREE_PROTECTED (fndecl))
  1009.           visibility = visibility_protected;
  1010.         else
  1011.           visibility = visibility_public;
  1012.           }
  1013.         else
  1014.           visibility = visibility_public;
  1015.  
  1016.         if (visibility == visibility_private
  1017.         ? (basetype == current_class_type
  1018.            || is_friend (basetype, cp->function)
  1019.            || purpose_member (basetype, DECL_VISIBILITY (fndecl)))
  1020.         : visibility == visibility_protected
  1021.         ? (can_be_protected
  1022.            || purpose_member (basetype, DECL_VISIBILITY (fndecl)))
  1023.         : 1)
  1024.           {
  1025.         if (cp->user == 0 && cp->b_or_d == 0
  1026.             && cp->easy <= 1)
  1027.           {
  1028.             goto found_and_ok;
  1029.           }
  1030.         cp++;
  1031.           }
  1032.         else
  1033.           {
  1034.         if (visibility == visibility_private)
  1035.           saw_private = 1;
  1036.         else
  1037.           saw_protected = 1;
  1038.           }
  1039.       }
  1040.     fndecl = DECL_CHAIN (fndecl);
  1041.       }
  1042.     if (cp - candidates)
  1043.       {
  1044.     /* Rank from worst to best.  Then cp will point to best one.
  1045.        Private fields have their bits flipped.  For unsigned
  1046.        numbers, this should make them look very large.
  1047.        If the best alternate has a (signed) negative value,
  1048.        then all we ever saw were private members.  */
  1049.     if (cp - candidates > 1)
  1050.       qsort (candidates,    /* char *base */
  1051.          cp - candidates, /* int nel */
  1052.          sizeof (struct candidate), /* int width */
  1053.          rank_for_overload); /* int (*compar)() */
  1054.  
  1055.     --cp;
  1056.     if (cp->evil > 1)
  1057.       {
  1058.         if (msgp)
  1059.           *msgp = "ambiguous type conversion possible for `%s'";
  1060.         return error_mark_node;
  1061.       }
  1062.  
  1063.     function = cp->function;
  1064.     fndecl = cp->u.field;
  1065.     goto found_and_ok;
  1066.       }
  1067.     else if (msgp)
  1068.       {
  1069.     if (saw_private)
  1070.       if (saw_protected)
  1071.         *msgp = "only private and protected conversions apply";
  1072.       else
  1073.         *msgp = "only private conversions apply";
  1074.     else if (saw_protected)
  1075.       *msgp = "only protected conversions apply";
  1076.       }
  1077.     return error_mark_node;
  1078.   }
  1079.   /* NOTREACHED */
  1080.  
  1081.  not_found:
  1082.   if (msgp) *msgp = "no appropriate conversion to type `%s'";
  1083.   return error_mark_node;
  1084.  found:
  1085.   if (visibility == visibility_private)
  1086.     if (! can_be_private)
  1087.       {
  1088.     if (msgp)
  1089.       *msgp = TREE_PRIVATE (fndecl)
  1090.         ? "conversion to type `%s' is private"
  1091.         : "conversion to type `%s' is from private base class";
  1092.     return error_mark_node;
  1093.       }
  1094.   if (visibility == visibility_protected)
  1095.     if (! can_be_protected)
  1096.       {
  1097.     if (msgp)
  1098.       *msgp = TREE_PRIVATE (fndecl)
  1099.         ? "conversion to type `%s' is protected"
  1100.         : "conversion to type `%s' is from protected base class";
  1101.     return error_mark_node;
  1102.       }
  1103.   function = fndecl;
  1104.  found_and_ok:
  1105.  
  1106.   /* It will convert, but we don't do anything about it yet.  */
  1107.   if (msgp == 0)
  1108.     return NULL_TREE;
  1109.  
  1110.   fntype = TREE_TYPE (function);
  1111.   if (TREE_INLINE (function) && TREE_CODE (function) == FUNCTION_DECL)
  1112.     function = build1 (ADDR_EXPR, build_pointer_type (fntype), function);
  1113.   else
  1114.     function = default_conversion (function);
  1115.  
  1116.   result = build_nt (CALL_EXPR, function,
  1117.              convert_arguments (NULL_TREE, TYPE_ARG_TYPES (fntype),
  1118.                     parmlist, NULL_TREE, LOOKUP_NORMAL),
  1119.              NULL_TREE);
  1120.   TREE_TYPE (result) = TREE_TYPE (fntype);
  1121.   TREE_SIDE_EFFECTS (result) = 1;
  1122.   TREE_RAISES (result) = !! TYPE_RAISES_EXCEPTIONS (fntype);
  1123.   return result;
  1124. }
  1125.  
  1126. /* Call this when we know (for any reason) that expr is
  1127.    not, in fact, zero.  */
  1128. tree
  1129. convert_pointer_to (type, expr)
  1130.      tree type, expr;
  1131. {
  1132.   register tree intype = TREE_TYPE (expr);
  1133.   register enum tree_code form = TREE_CODE (intype);
  1134.   tree ptr_type = build_pointer_type (type);
  1135.   tree rval;
  1136.  
  1137.   if (TYPE_MAIN_VARIANT (ptr_type) == TYPE_MAIN_VARIANT (intype))
  1138.     return expr;
  1139.  
  1140.   if (intype == error_mark_node)
  1141.     return error_mark_node;
  1142.  
  1143.   assert (form == POINTER_TYPE);
  1144.   assert (!integer_zerop (expr));
  1145.  
  1146.   if (IS_AGGR_TYPE (type)
  1147.       && IS_AGGR_TYPE (TREE_TYPE (intype))
  1148.       && TYPE_MAIN_VARIANT (type) != TYPE_MAIN_VARIANT (TREE_TYPE (intype)))
  1149.     {
  1150.       tree path, basetype;
  1151.       int distance = get_base_distance (type, TYPE_MAIN_VARIANT (TREE_TYPE (intype)), 0, &path);
  1152.  
  1153.       /* This function shouldn't be called with
  1154.      unqualified arguments.  */
  1155.       assert (distance >= 0);
  1156.  
  1157.       return build_vbase_path (PLUS_EXPR, ptr_type, expr, path, 1);
  1158.     }
  1159.   rval = build1 (NOP_EXPR, ptr_type,
  1160.          TREE_CODE (expr) == NOP_EXPR
  1161.          ? TREE_OPERAND (expr, 0) : expr);
  1162.   TREE_CONSTANT (rval) = TREE_CONSTANT (expr);
  1163.   return rval;
  1164. }
  1165.  
  1166. /* Same as above, but don't abort if we get an "ambiguous" baseclass.
  1167.    There's only one virtual baseclass we are looking for, and once
  1168.    we find one such virtual baseclass, we have found them all.  */
  1169.  
  1170. tree
  1171. convert_pointer_to_vbase (type, expr)
  1172.      tree type;
  1173.      tree expr;
  1174. {
  1175.   tree intype = TREE_TYPE (TREE_TYPE (expr));
  1176.   int i;
  1177.  
  1178.   for (i = CLASSTYPE_N_BASECLASSES (intype)-1; i >= 0; i--)
  1179.     {
  1180.       tree basetype = CLASSTYPE_BASECLASS (intype, i);
  1181.       if (type == basetype)
  1182.     return convert_pointer_to (type, expr);
  1183.       if (value_member (TYPE_MAIN_VARIANT (type),
  1184.             CLASSTYPE_VBASECLASSES (basetype)))
  1185.     return convert_pointer_to_vbase (type, convert_pointer_to (TYPE_MAIN_VARIANT (basetype), expr));
  1186.     }
  1187.   abort ();
  1188. }
  1189.  
  1190. /* Create an expression whose value is that of EXPR,
  1191.    converted to type TYPE.  The TREE_TYPE of the value
  1192.    is always TYPE.  This function implements all reasonable
  1193.    conversions; callers should filter out those that are
  1194.    not permitted by the language being compiled.  */
  1195.  
  1196. tree
  1197. convert (type, expr)
  1198.      tree type, expr;
  1199. {
  1200.   register tree e = expr;
  1201.   register enum tree_code code = TREE_CODE (type);
  1202.  
  1203.   if (type == TREE_TYPE (expr) || TREE_CODE (expr) == ERROR_MARK)
  1204.     return expr;
  1205.   if (TREE_CODE (TREE_TYPE (expr)) == ERROR_MARK)
  1206.     return error_mark_node;
  1207.   if (TREE_CODE (TREE_TYPE (expr)) == VOID_TYPE)
  1208.     {
  1209.       error ("void value not ignored as it ought to be");
  1210.       return error_mark_node;
  1211.     }
  1212.   if (code == VOID_TYPE)
  1213.     {
  1214.       tree rval = build_type_conversion (NOP_EXPR, type, e, 0);
  1215.       /* If we can convert to void type via a type conversion, do so.  */
  1216.       if (rval)
  1217.     return rval;
  1218.       return build1 (CONVERT_EXPR, type, e);
  1219.     }
  1220. #if 0
  1221.   /* This is incorrect.  A truncation can't be stripped this way.
  1222.      Extensions will be stripped by the use of get_unwidened.  */
  1223.   if (TREE_CODE (expr) == NOP_EXPR)
  1224.     return convert (type, TREE_OPERAND (expr, 0));
  1225. #endif
  1226.  
  1227.   /* Just convert to the type of the member.  */
  1228.   if (code == OFFSET_TYPE)
  1229.     {
  1230.       type = TREE_TYPE (type);
  1231.       code = TREE_CODE (type);
  1232.     }
  1233.  
  1234.   /* C++ */
  1235.   if (code == REFERENCE_TYPE)
  1236.     return fold (convert_to_reference (error_mark_node, type, e, -1, LOOKUP_NORMAL));
  1237.   else if (TREE_CODE (TREE_TYPE (e)) == REFERENCE_TYPE)
  1238.     e = convert_from_reference (e);
  1239.  
  1240.   if (code == INTEGER_TYPE || code == ENUMERAL_TYPE)
  1241.     return fold (convert_to_integer (type, e));
  1242.   if (code == POINTER_TYPE)
  1243.     return fold (convert_to_pointer (type, e));
  1244.   if (code == REAL_TYPE)
  1245.     return fold (convert_to_real (type, e));
  1246.  
  1247.   /* New C++ semantics:  since assignment is now based on
  1248.      memberwise copying,  if the rhs type is derived from the
  1249.      lhs type, then we may still do a conversion.  */
  1250.   if (IS_AGGR_TYPE_CODE (code))
  1251.     {
  1252.       tree dtype = TREE_TYPE (e);
  1253.  
  1254.       if (TREE_CODE (dtype) == REFERENCE_TYPE)
  1255.     {
  1256.       e = convert_from_reference (e);
  1257.       dtype = TREE_TYPE (e);
  1258.     }
  1259.       dtype = TYPE_MAIN_VARIANT (dtype);
  1260.  
  1261.       /* Conversion between aggregate types.  New C++ semantics allow
  1262.      objects of derived type to be cast to objects of base type.
  1263.      Old semantics only allowed this bwteen pointers.
  1264.  
  1265.      There may be some ambiguity between using a constructor
  1266.      vs. using a type conversion operator when both apply.  */
  1267.  
  1268.       if (IS_AGGR_TYPE (dtype))
  1269.     {
  1270.       tree basetype;
  1271.  
  1272.       tree conversion = TYPE_HAS_CONVERSION (dtype)
  1273.         ? build_type_conversion (CONVERT_EXPR, type, e, 1) : NULL_TREE;
  1274.  
  1275.       if (TYPE_HAS_CONSTRUCTOR (type))
  1276.         {
  1277.           tree rval = build_method_call (NULL_TREE, TYPE_IDENTIFIER (type), build_tree_list (NULL_TREE, e), CLASSTYPE_AS_LIST (type),
  1278.                          conversion ? LOOKUP_NO_CONVERSION : 0);
  1279.  
  1280.           if (rval != error_mark_node)
  1281.         {
  1282.           if (conversion)
  1283.             {
  1284.               error ("both constructor and type conversion operator apply");
  1285.               return error_mark_node;
  1286.             }
  1287.           /* call to constructor successful.  */
  1288.           rval = build_cplus_new (type, rval, 0);
  1289.           return rval;
  1290.         }
  1291.         }
  1292.       /* Type conversion successful/applies.  */
  1293.       if (conversion)
  1294.         {
  1295.           if (conversion == error_mark_node)
  1296.         error ("ambiguous pointer conversion");
  1297.           return conversion;
  1298.         }
  1299.  
  1300.       /* now try normal C++ assignment semantics.  */
  1301.       basetype = dtype;
  1302.       if (type == basetype
  1303.           || (basetype = get_base_type (type, dtype, 1)))
  1304.         {
  1305.           if (basetype == error_mark_node)
  1306.         return error_mark_node;
  1307.  
  1308.           if (lvalue_p (e))
  1309.         {
  1310.           e = build_unary_op (ADDR_EXPR, e, 0);
  1311.  
  1312.           if (! CLASSTYPE_OFFSET_ZEROP (basetype))
  1313.             e = build (PLUS_EXPR, TYPE_POINTER_TO (type),
  1314.                    e, CLASSTYPE_OFFSET (basetype));
  1315.           return build1 (INDIRECT_REF, type, e);
  1316.         }
  1317.  
  1318.           sorry ("addressable aggregates");
  1319.           return error_mark_node;
  1320.         }
  1321.       error ("conversion between incompatible aggregate types requested");
  1322.       return error_mark_node;
  1323.     }
  1324.       /* conversion from non-aggregate to aggregate type requires constructor.  */
  1325.       else if (TYPE_HAS_CONSTRUCTOR (type))
  1326.     {
  1327.       tree rval;
  1328.       tree init = build_method_call (NULL_TREE, TYPE_IDENTIFIER (type), build_tree_list (NULL_TREE, e), CLASSTYPE_AS_LIST (type), LOOKUP_NORMAL);
  1329.       if (init == error_mark_node)
  1330.         {
  1331.           error_with_aggr_type (type, "in conversion to type `%s'");
  1332.           return error_mark_node;
  1333.         }
  1334.       rval = build_cplus_new (type, init, 0);
  1335.       return rval;
  1336.     }
  1337.     }
  1338.  
  1339.   error ("conversion to non-scalar type requested");
  1340.   return error_mark_node;
  1341. }
  1342.  
  1343. /* Like convert, except permit conversions to take place which
  1344.    are not normally allowed due to visibility restrictions
  1345.    (such as conversion from sub-type to private super-type).  */
  1346. tree
  1347. convert_force (type, expr)
  1348.      tree type;
  1349.      tree expr;
  1350. {
  1351.   register tree e = expr;
  1352.   register enum tree_code code = TREE_CODE (type);
  1353.  
  1354.   if (code == REFERENCE_TYPE)
  1355.     return fold (convert_to_reference (0, type, e, -1, 0));
  1356.   else if (TREE_CODE (TREE_TYPE (e)) == REFERENCE_TYPE)
  1357.     e = convert_from_reference (e);
  1358.  
  1359.   if (code == POINTER_TYPE)
  1360.     return fold (convert_to_pointer_force (type, e));
  1361.   return convert (type, e);
  1362. }
  1363.  
  1364. /* Subroutine of build_type_conversion.  */
  1365. static tree
  1366. build_type_conversion_1 (xtype, basetype, expr, typename, for_sure)
  1367.      tree xtype, basetype;
  1368.      tree expr;
  1369.      tree typename;
  1370.      int for_sure;
  1371. {
  1372.   tree first_arg = expr;
  1373.   tree rval;
  1374.   int flags;
  1375.  
  1376.   if (for_sure == 0)
  1377.     {
  1378.       if (! lvalue_p (expr))
  1379.     first_arg = build1 (NOP_EXPR, TYPE_POINTER_TO (basetype), integer_zero_node);
  1380.       flags = LOOKUP_PROTECT;
  1381.     }
  1382.   else
  1383.     flags = LOOKUP_NORMAL;
  1384.  
  1385.   rval = build_method_call (first_arg, typename, NULL_TREE, NULL_TREE, flags);
  1386.   if (rval == error_mark_node)
  1387.     {
  1388.       if (for_sure == 0)
  1389.     return NULL_TREE;
  1390.       return error_mark_node;
  1391.     }
  1392.   if (first_arg != expr)
  1393.     {
  1394.       expr = build_up_reference (build_reference_type (TREE_TYPE (expr)), expr, 0);
  1395.       TREE_VALUE (TREE_OPERAND (rval, 1)) = build_unary_op (ADDR_EXPR, expr, 0);
  1396.     }
  1397.   if (TREE_CODE (TREE_TYPE (rval)) == REFERENCE_TYPE
  1398.       && TREE_CODE (xtype) != REFERENCE_TYPE)
  1399.     rval = default_conversion (rval);
  1400.   return convert (xtype, rval);
  1401. }
  1402.  
  1403. /* Convert an aggregate EXPR to type XTYPE.  If a conversion
  1404.    exists, return the attempted conversion.  This may
  1405.    return ERROR_MARK_NODE if the conversion is not
  1406.    allowed (references private members, etc).
  1407.    If no conversion exists, NULL_TREE is returned.
  1408.  
  1409.    If (FOR_SURE & 1) is non-zero, then we allow this type conversion
  1410.    to take place immediately.  Otherwise, we build a SAVE_EXPR
  1411.    which can be evaluated if the results are ever needed.
  1412.  
  1413.    If FOR_SURE >= 2, then we only look for exact conversions.
  1414.  
  1415.    TYPE may be a reference type, in which case we first look
  1416.    for something that will convert to a reference type.  If
  1417.    that fails, we will try to look for something of the
  1418.    reference's target type, and then return a reference to that.  */
  1419. tree
  1420. build_type_conversion (code, xtype, expr, for_sure)
  1421.      enum tree_code code;
  1422.      tree xtype, expr;
  1423.      int for_sure;
  1424. {
  1425.   /* C++: check to see if we can convert this aggregate type
  1426.      into the required scalar type.  */
  1427.   tree type, type_default;
  1428.   tree typename = build_typename_overload (xtype), *typenames;
  1429.   int n_variants = 0;
  1430.   tree basetype, save_basetype;
  1431.   tree rval;
  1432.   int exact_conversion = for_sure >= 2;
  1433.   for_sure &= 1;
  1434.  
  1435.   if (expr == error_mark_node)
  1436.     return error_mark_node;
  1437.  
  1438.   basetype = TREE_TYPE (expr);
  1439.   if (TREE_CODE (basetype) == REFERENCE_TYPE)
  1440.     basetype = TREE_TYPE (basetype);
  1441.  
  1442.   basetype = TYPE_MAIN_VARIANT (basetype);
  1443.   if (! TYPE_LANG_SPECIFIC (basetype) || ! TYPE_HAS_CONVERSION (basetype))
  1444.     return 0;
  1445.  
  1446.   if (TREE_CODE (xtype) == POINTER_TYPE
  1447.       || TREE_CODE (xtype) == REFERENCE_TYPE)
  1448.     {
  1449.       /* Prepare to match a variant of this type.  */
  1450.       type = TYPE_MAIN_VARIANT (TREE_TYPE (xtype));
  1451.       for (n_variants = 0; type; type = TYPE_NEXT_VARIANT (type))
  1452.     n_variants++;
  1453.       typenames = (tree *)alloca (n_variants * sizeof (tree));
  1454.       for (n_variants = 0, type = TYPE_MAIN_VARIANT (TREE_TYPE (xtype));
  1455.        type; n_variants++, type = TYPE_NEXT_VARIANT (type))
  1456.     {
  1457.       if (type == TREE_TYPE (xtype))
  1458.         typenames[n_variants] = typename;
  1459.       else if (TREE_CODE (xtype) == POINTER_TYPE)
  1460.         typenames[n_variants] = build_typename_overload (build_pointer_type (type));
  1461.       else
  1462.         typenames[n_variants] = build_typename_overload (build_reference_type (type));
  1463.     }
  1464.     }
  1465.  
  1466.   save_basetype = basetype;
  1467.   type = xtype;
  1468.  
  1469.   while (TYPE_HAS_CONVERSION (basetype))
  1470.     {
  1471.       int i;
  1472.       if (lookup_fnfields (CLASSTYPE_AS_LIST (basetype), typename, 0))
  1473.     return build_type_conversion_1 (xtype, basetype, expr, typename, for_sure);
  1474.       for (i = 0; i < n_variants; i++)
  1475.     if (typenames[i] != typename
  1476.         && lookup_fnfields (CLASSTYPE_AS_LIST (basetype), typenames[i], 0))
  1477.       return build_type_conversion_1 (xtype, basetype, expr, typenames[i], for_sure);
  1478.  
  1479.       if (TYPE_BASETYPES (basetype))
  1480.     basetype = CLASSTYPE_BASECLASS (basetype, 0);
  1481.       else break;
  1482.     }
  1483.  
  1484.   if (TREE_CODE (type) == REFERENCE_TYPE)
  1485.     {
  1486.       tree first_arg = expr;
  1487.       type = TYPE_MAIN_VARIANT (TREE_TYPE (type));
  1488.       basetype = save_basetype;
  1489.  
  1490.       /* May need to build a temporary for this.  */
  1491.       while (TYPE_HAS_CONVERSION (basetype))
  1492.     {
  1493.       if (lookup_fnfields (CLASSTYPE_AS_LIST (basetype), typename, 0))
  1494.         {
  1495.           int flags;
  1496.  
  1497.           if (for_sure == 0)
  1498.         {
  1499.           if (! lvalue_p (expr))
  1500.             first_arg = build1 (NOP_EXPR, TYPE_POINTER_TO (basetype), integer_zero_node);
  1501.           flags = LOOKUP_PROTECT;
  1502.         }
  1503.           else
  1504.         flags = LOOKUP_NORMAL;
  1505.           rval = build_method_call (first_arg, typename, NULL_TREE, NULL_TREE, flags);
  1506.           if (rval == error_mark_node)
  1507.         {
  1508.           if (for_sure == 0)
  1509.             return NULL_TREE;
  1510.           return error_mark_node;
  1511.         }
  1512.           TREE_VALUE (TREE_OPERAND (rval, 1)) = expr;
  1513.  
  1514.           if (IS_AGGR_TYPE (type))
  1515.         {
  1516.           tree init = build_method_call (NULL_TREE, TYPE_IDENTIFIER (type), build_tree_list (NULL_TREE, rval), NULL_TREE, LOOKUP_NORMAL);
  1517.           tree temp = build_cplus_new (type, init, 1);
  1518.           return build_up_reference (TYPE_REFERENCE_TO (type), temp, 0);
  1519.         }
  1520.           return convert (xtype, rval);
  1521.         }
  1522.       if (TYPE_BASETYPES (basetype))
  1523.         basetype = CLASSTYPE_BASECLASS (basetype, 0);
  1524.       else break;
  1525.     }
  1526.       /* No free conversions for reference types, right?.  */
  1527.       return NULL_TREE;
  1528.     }
  1529.  
  1530.   if (exact_conversion)
  1531.     return NULL_TREE;
  1532.  
  1533.   /* No perfect match found, try default.  */
  1534.   if (code == CONVERT_EXPR && TREE_CODE (type) == POINTER_TYPE)
  1535.     type_default = ptr_type_node;
  1536.   else if (type == void_type_node)
  1537.     return NULL_TREE;
  1538.   else
  1539.     {
  1540.       extern tree default_conversion ();
  1541.       tree tmp = default_conversion (build1 (NOP_EXPR, type, integer_zero_node));
  1542.       if (tmp == error_mark_node)
  1543.     return NULL_TREE;
  1544.       type_default = TREE_TYPE (tmp);
  1545.     }
  1546.  
  1547.   basetype = save_basetype;
  1548.  
  1549.   if (type_default != type)
  1550.     {
  1551.       type = type_default;
  1552.       typename = build_typename_overload (type);
  1553.  
  1554.       while (TYPE_HAS_CONVERSION (basetype))
  1555.     {
  1556.       if (lookup_fnfields (CLASSTYPE_AS_LIST (basetype), typename, 0))
  1557.         return build_type_conversion_1 (xtype, basetype, expr, typename, for_sure);
  1558.       if (TYPE_BASETYPES (basetype))
  1559.         basetype = CLASSTYPE_BASECLASS (basetype, 0);
  1560.       else break;
  1561.     }
  1562.     }
  1563.  
  1564.  try_pointer:
  1565.  
  1566.   if (type == ptr_type_node)
  1567.     {
  1568.       /* Try converting to some other pointer type
  1569.      with which void* is compatible, or in situations
  1570.      in which void* is appropriate (such as &&,||, and !).  */
  1571.  
  1572.       while (TYPE_HAS_CONVERSION (basetype))
  1573.     {
  1574.       if (CLASSTYPE_CONVERSION (basetype, ptr_conv) != 0)
  1575.         {
  1576.           if (CLASSTYPE_CONVERSION (basetype, ptr_conv) == error_mark_node)
  1577.         return error_mark_node;
  1578.           typename = DECL_ORIGINAL_NAME (CLASSTYPE_CONVERSION (basetype, ptr_conv));
  1579.           return build_type_conversion_1 (xtype, basetype, expr, typename, for_sure);
  1580.         }
  1581.       if (TYPE_BASETYPES (basetype))
  1582.         basetype = CLASSTYPE_BASECLASS (basetype, 0);
  1583.       else break;
  1584.     }
  1585.     }
  1586.   if (TREE_CODE (type) == POINTER_TYPE
  1587.       && TYPE_READONLY (TREE_TYPE (type))
  1588.       && TYPE_MAIN_VARIANT (TREE_TYPE (type)) == void_type_node)
  1589.     {
  1590.       /* Try converting to some other pointer type
  1591.      with which const void* is compatible.  */
  1592.  
  1593.       while (TYPE_HAS_CONVERSION (basetype))
  1594.     {
  1595.       if (CLASSTYPE_CONVERSION (basetype, constptr_conv) != 0)
  1596.         {
  1597.           if (CLASSTYPE_CONVERSION (basetype, constptr_conv) == error_mark_node)
  1598.         return error_mark_node;
  1599.           typename = DECL_ORIGINAL_NAME (CLASSTYPE_CONVERSION (basetype, constptr_conv));
  1600.           return build_type_conversion_1 (xtype, basetype, expr, typename, for_sure);
  1601.         }
  1602.       if (TYPE_BASETYPES (basetype))
  1603.         basetype = CLASSTYPE_BASECLASS (basetype, 0);
  1604.       else break;
  1605.     }
  1606.     }
  1607.   /* Use the longer or shorter conversion that is appropriate.  */
  1608.   if (TREE_CODE (type) == INTEGER_TYPE
  1609.       && TYPE_HAS_INT_CONVERSION (basetype)
  1610.       && CLASSTYPE_CONVERSION (basetype, int_conv) != error_mark_node)
  1611.     {
  1612.       typename = DECL_ORIGINAL_NAME (CLASSTYPE_CONVERSION (basetype, int_conv));
  1613.       return build_type_conversion_1 (xtype, basetype, expr, typename, for_sure);
  1614.     }
  1615.   if (TREE_CODE (type) == REAL_TYPE
  1616.       && TYPE_HAS_REAL_CONVERSION (basetype)
  1617.       && CLASSTYPE_CONVERSION (basetype, real_conv) != error_mark_node)
  1618.     {
  1619.       typename = DECL_ORIGINAL_NAME (CLASSTYPE_CONVERSION (basetype, real_conv));
  1620.       return build_type_conversion_1 (xtype, basetype, expr, typename, for_sure);
  1621.     }
  1622.  
  1623.   /* THIS IS A KLUDGE.  */
  1624.   if (TREE_CODE (type) != POINTER_TYPE
  1625.       && (code == TRUTH_ANDIF_EXPR
  1626.       || code == TRUTH_ORIF_EXPR
  1627.       || code == TRUTH_NOT_EXPR))
  1628.     {
  1629.       /* Here's when we can convert to a pointer.  */
  1630.       type = ptr_type_node;
  1631.       goto try_pointer;
  1632.     }
  1633.  
  1634.   /* THESE ARE TOTAL KLUDGES.  */
  1635.   /* Default promotion yields no new alternatives, try
  1636.      conversions which are anti-default, such as
  1637.  
  1638.      double -> float or int -> unsigned or unsigned -> long
  1639.  
  1640.      */
  1641.   if (type_default == type)
  1642.     {
  1643.       int not_again = 0;
  1644.  
  1645.       if (type == double_type_node)
  1646.     typename = build_typename_overload (float_type_node);
  1647.       else if (type == integer_type_node)
  1648.     typename = build_typename_overload (unsigned_type_node);
  1649.       else if (type == unsigned_type_node)
  1650.     typename = build_typename_overload (long_integer_type_node);
  1651.  
  1652.     again:
  1653.       basetype = save_basetype;
  1654.       while (TYPE_HAS_CONVERSION (basetype))
  1655.     {
  1656.       if (lookup_fnfields (CLASSTYPE_AS_LIST (basetype), typename, 0))
  1657.         return build_type_conversion_1 (xtype, basetype, expr, typename, for_sure);
  1658.       if (TYPE_BASETYPES (basetype))
  1659.         basetype = CLASSTYPE_BASECLASS (basetype, 0);
  1660.       else break;
  1661.     }
  1662.       if (! not_again && type == integer_type_node)
  1663.     {
  1664.       typename = build_typename_overload (long_integer_type_node);
  1665.       not_again = 1;
  1666.       goto again;
  1667.     }
  1668.     }
  1669.  
  1670.   /* Now, try C promotions...
  1671.  
  1672.      float -> int
  1673.      int -> float, void *
  1674.      void * -> int
  1675.  
  1676.      Truthvalue conversions let us try to convert
  1677.      to pointer if we were going for int, and to int
  1678.      if we were looking for pointer.  */
  1679.  
  1680.     basetype = save_basetype;
  1681.     if (TREE_CODE (type) == REAL_TYPE
  1682.     || (TREE_CODE (type) == POINTER_TYPE
  1683.         && (code == TRUTH_ANDIF_EXPR
  1684.         || code == TRUTH_ORIF_EXPR
  1685.         || code == TRUTH_NOT_EXPR)))
  1686.       type = integer_type_node;
  1687.     else if (TREE_CODE (type) == INTEGER_TYPE)
  1688.       if (TYPE_HAS_REAL_CONVERSION (basetype))
  1689.     type = double_type_node;
  1690.       else
  1691.     return NULL_TREE;
  1692.     else
  1693.       return NULL_TREE;
  1694.  
  1695.     typename = build_typename_overload (type);
  1696.     while (TYPE_HAS_CONVERSION (basetype))
  1697.       {
  1698.     if (lookup_fnfields (CLASSTYPE_AS_LIST (basetype), typename, 0))
  1699.       {
  1700.         rval = build_type_conversion_1 (xtype, basetype, expr, typename, for_sure);
  1701.         return rval;
  1702.       }
  1703.     if (TYPE_BASETYPES (basetype))
  1704.       basetype = CLASSTYPE_BASECLASS (basetype, 0);
  1705.     else
  1706.       break;
  1707.       }
  1708.  
  1709.   return NULL_TREE;
  1710. }
  1711.  
  1712. /* Must convert two aggregate types to non-aggregate type.
  1713.    Attempts to find a non-ambiguous, "best" type conversion.
  1714.  
  1715.    Return 1 on success, 0 on failure.
  1716.  
  1717.    @@ What are the real semantics of this supposed to be??? */
  1718. int
  1719. build_default_binary_type_conversion (code, arg1, arg2)
  1720.      enum tree_code code;
  1721.      tree *arg1, *arg2;
  1722. {
  1723.   tree type1 = TREE_TYPE (*arg1);
  1724.   tree type2 = TREE_TYPE (*arg2);
  1725.   char *name1, *name2;
  1726.  
  1727.   if (TREE_CODE (type1) == REFERENCE_TYPE)
  1728.     type1 = TREE_TYPE (type1);
  1729.   if (TREE_CODE (type2) == REFERENCE_TYPE)
  1730.     type2 = TREE_TYPE (type2);
  1731.  
  1732.   if (TREE_CODE (TYPE_NAME (type1)) != TYPE_DECL)
  1733.     {
  1734.       tree decl = typedecl_for_tag (type1);
  1735.       if (decl)
  1736.     error ("type conversion nonexistant for type `%s'",
  1737.            IDENTIFIER_POINTER (DECL_NAME (decl)));
  1738.       else
  1739.     error ("type conversion nonexistant for non-C++ type");
  1740.       return 0;
  1741.     }
  1742.   if (TREE_CODE (TYPE_NAME (type2)) != TYPE_DECL)
  1743.     {
  1744.       tree decl = typedecl_for_tag (type2);
  1745.       if (decl)
  1746.     error ("type conversion nonexistant for type `%s'",
  1747.            IDENTIFIER_POINTER (decl));
  1748.       else
  1749.     error ("type conversion nonexistant for non-C++ type");
  1750.       return 0;
  1751.     }
  1752.  
  1753.   name1 = TYPE_NAME_STRING (type1);
  1754.   name2 = TYPE_NAME_STRING (type2);
  1755.  
  1756.   if (! TYPE_HAS_CONVERSION (type1))
  1757.     {
  1758.       if (! TYPE_HAS_CONVERSION (type2))
  1759.     error ("type conversion required for binary operation on types `%s' and `%s'",
  1760.            name1, name2);
  1761.       else
  1762.     error ("type conversion required for type `%s'", name1);
  1763.       return 0;
  1764.     }
  1765.   else if (! TYPE_HAS_CONVERSION (type2))
  1766.     {
  1767.       error ("type conversion required for type `%s'", name2);
  1768.       return 0;
  1769.     }
  1770.  
  1771.   if (TYPE_HAS_INT_CONVERSION (type1) && TYPE_HAS_REAL_CONVERSION (type1))
  1772.     warning ("ambiguous type conversion for type `%s', defaulting to int", name1);
  1773.   if (TYPE_HAS_INT_CONVERSION (type1))
  1774.     {
  1775.       *arg1 = build_type_conversion (code, integer_type_node, *arg1, 1);
  1776.       *arg2 = build_type_conversion (code, integer_type_node, *arg2, 1);
  1777.     }
  1778.   else if (TYPE_HAS_REAL_CONVERSION (type1))
  1779.     {
  1780.       *arg1 = build_type_conversion (code, double_type_node, *arg1, 1);
  1781.       *arg2 = build_type_conversion (code, double_type_node, *arg2, 1);
  1782.     }
  1783.   else
  1784.     {
  1785.       *arg1 = build_type_conversion (code, ptr_type_node, *arg1, 1);
  1786.       if (*arg1 == error_mark_node)
  1787.     error ("ambiguous pointer conversion");
  1788.       *arg2 = build_type_conversion (code, ptr_type_node, *arg2, 1);
  1789.       if (*arg1 != error_mark_node && *arg2 == error_mark_node)
  1790.     error ("ambiguous pointer conversion");
  1791.     }
  1792.   if (*arg1 == 0)
  1793.     {
  1794.       if (*arg2 == 0 && type1 != type2)
  1795.     error ("default type conversion for types `%s' and `%s' failed",
  1796.            name1, name2);
  1797.       else
  1798.     error ("default type conversion for type `%s' failed", name1);
  1799.       return 0;
  1800.     }
  1801.   else if (*arg2 == 0)
  1802.     {
  1803.       error ("default type conversion for type `%s' failed", name2);
  1804.       return 0;
  1805.     }
  1806.   return 1;
  1807. }
  1808.  
  1809. /* Must convert two aggregate types to non-aggregate type.
  1810.    Attempts to find a non-ambiguous, "best" type conversion.
  1811.  
  1812.    Return 1 on success, 0 on failure.
  1813.  
  1814.    The type of the argument is expected to be of aggregate type here.
  1815.  
  1816.    @@ What are the real semantics of this supposed to be??? */
  1817. int
  1818. build_default_unary_type_conversion (code, arg)
  1819.      enum tree_code code;
  1820.      tree *arg;
  1821. {
  1822.   tree type = TREE_TYPE (*arg);
  1823.   tree id = TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
  1824.     ? TYPE_IDENTIFIER (type) : TYPE_NAME (type);
  1825.   char *name = IDENTIFIER_POINTER (id);
  1826.  
  1827.   if (! TYPE_HAS_CONVERSION (type))
  1828.     {
  1829.       error ("type conversion required for type `%s'", name);
  1830.       return 0;
  1831.     }
  1832.  
  1833.   if (TYPE_HAS_INT_CONVERSION (type) && TYPE_HAS_REAL_CONVERSION (type))
  1834.     warning ("ambiguous type conversion for type `%s', defaulting to int", name);
  1835.   if (TYPE_HAS_INT_CONVERSION (type))
  1836.     *arg = build_type_conversion (code, integer_type_node, *arg, 1);
  1837.   else if (TYPE_HAS_REAL_CONVERSION (type))
  1838.     *arg = build_type_conversion (code, double_type_node, *arg, 1);
  1839.   else
  1840.     {
  1841.       *arg = build_type_conversion (code, ptr_type_node, *arg, 1);
  1842.       if (*arg == error_mark_node)
  1843.     error ("ambiguous pointer conversion");
  1844.     }
  1845.   if (*arg == 0)
  1846.     {
  1847.       error ("default type conversion for type `%s' failed", name);
  1848.       return 0;
  1849.     }
  1850.   return 1;
  1851. }
  1852.