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

  1. /* Language-level data type conversion for GNU C.
  2.    Copyright (C) 1987, 1988 Free Software Foundation, Inc.
  3.  
  4. This file is part of GNU CC.
  5.  
  6. GNU CC is free software; you can redistribute it and/or modify
  7. it under the terms of the GNU General Public License as published by
  8. the Free Software Foundation; either version 2, or (at your option)
  9. any later version.
  10.  
  11. GNU CC is distributed in the hope that it will be useful,
  12. but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14. GNU General Public License for more details.
  15.  
  16. You should have received a copy of the GNU General Public License
  17. along with GNU CC; see the file COPYING.  If not, write to
  18. the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.  */
  19.  
  20.  
  21. /* This file contains the functions for converting C expressions
  22.    to different data types.  The only entry point is `convert'.
  23.    Every language front end must have a `convert' function
  24.    but what kind of conversions it does will depend on the language.  */
  25.  
  26. #include "config.h"
  27. #include "tree.h"
  28.  
  29. /* Change of width--truncation and extension of integers or reals--
  30.    is represented with NOP_EXPR.  Proper functioning of many things
  31.    assumes that no other conversions can be NOP_EXPRs.
  32.  
  33.    Conversion between integer and pointer is represented with CONVERT_EXPR.
  34.    Converting integer to real uses FLOAT_EXPR
  35.    and real to integer uses FIX_TRUNC_EXPR.
  36.  
  37.    Here is a list of all the functions that assume that widening and
  38.    narrowing is always done with a NOP_EXPR:
  39.      In c-convert.c, convert_to_integer.
  40.      In c-typeck.c, build_binary_op_nodefault (boolean ops),
  41.         and truthvalue_conversion.
  42.      In expr.c: expand_expr, for operands of a MULT_EXPR.
  43.      In fold-const.c: fold.
  44.      In tree.c: get_narrower and get_unwidened.  */
  45.  
  46. /* Subroutines of `convert'.  */
  47.  
  48. static tree
  49. convert_to_pointer (type, expr)
  50.      tree type, expr;
  51. {
  52.   register tree intype = TREE_TYPE (expr);
  53.   register enum tree_code form = TREE_CODE (intype);
  54.   
  55.   if (integer_zerop (expr))
  56.     {
  57.       if (type == TREE_TYPE (null_pointer_node))
  58.     return null_pointer_node;
  59.       expr = build_int_2 (0, 0);
  60.       TREE_TYPE (expr) = type;
  61.       return expr;
  62.     }
  63.  
  64.   if (form == POINTER_TYPE)
  65.     return build1 (NOP_EXPR, type, expr);
  66.  
  67.  
  68.   if (form == INTEGER_TYPE || form == ENUMERAL_TYPE)
  69.     {
  70.       if (type_precision (intype) == POINTER_SIZE)
  71.     return build1 (CONVERT_EXPR, type, expr);
  72.       return convert_to_pointer (type,
  73.                  convert (type_for_size (POINTER_SIZE, 0),
  74.                       expr));
  75.     }
  76.  
  77.   error ("cannot convert to a pointer type");
  78.  
  79.   return null_pointer_node;
  80. }
  81.  
  82. static tree
  83. convert_to_real (type, expr)
  84.      tree type, expr;
  85. {
  86.   register enum tree_code form = TREE_CODE (TREE_TYPE (expr));
  87.   extern int flag_float_store;
  88.  
  89.   if (form == REAL_TYPE)
  90.     return build1 (flag_float_store ? CONVERT_EXPR : NOP_EXPR,
  91.            type, expr);
  92.  
  93.   if (form == INTEGER_TYPE || form == ENUMERAL_TYPE)
  94.     return build1 (FLOAT_EXPR, type, expr);
  95.  
  96.   if (form == POINTER_TYPE)
  97.     error ("pointer value used where a float was expected");
  98.   else
  99.     error ("aggregate value used where a float was expected");
  100.  
  101.   {
  102.     register tree tem = make_node (REAL_CST);
  103.     TREE_TYPE (tem) = type;
  104.     TREE_REAL_CST (tem) = REAL_VALUE_ATOF ("0.0");
  105.     return tem;
  106.   }
  107. }
  108.  
  109. /* The result of this is always supposed to be a newly created tree node
  110.    not in use in any existing structure.  */
  111.  
  112. static tree
  113. convert_to_integer (type, expr)
  114.      tree type, expr;
  115. {
  116.   register tree intype = TREE_TYPE (expr);
  117.   register enum tree_code form = TREE_CODE (intype);
  118.   extern tree build_binary_op_nodefault ();
  119.   extern tree build_unary_op ();
  120.  
  121.   if (form == POINTER_TYPE)
  122.     {
  123.       if (integer_zerop (expr))
  124.     expr = integer_zero_node;
  125.       else
  126.     expr = fold (build1 (CONVERT_EXPR,
  127.                  type_for_size (POINTER_SIZE, 0), expr));
  128.       intype = TREE_TYPE (expr);
  129.       form = TREE_CODE (intype);
  130.       if (intype == type)
  131.     return expr;
  132.     }
  133.  
  134.   if (form == INTEGER_TYPE || form == ENUMERAL_TYPE)
  135.     {
  136.       register int outprec = TYPE_PRECISION (type);
  137.       register int inprec = TYPE_PRECISION (intype);
  138.       register enum tree_code ex_form = TREE_CODE (expr);
  139.  
  140.       if (outprec >= inprec)
  141.     return build1 (NOP_EXPR, type, expr);
  142.  
  143. /* Here detect when we can distribute the truncation down past some arithmetic.
  144.    For example, if adding two longs and converting to an int,
  145.    we can equally well convert both to ints and then add.
  146.    For the operations handled here, such truncation distribution
  147.    is always safe.
  148.    It is desirable in these cases:
  149.    1) when truncating down to full-word from a larger size
  150.    2) when truncating takes no work.
  151.    3) when at least one operand of the arithmetic has been extended
  152.    (as by C's default conversions).  In this case we need two conversions
  153.    if we do the arithmetic as already requested, so we might as well
  154.    truncate both and then combine.  Perhaps that way we need only one.
  155.  
  156.    Note that in general we cannot do the arithmetic in a type
  157.    shorter than the desired result of conversion, even if the operands
  158.    are both extended from a shorter type, because they might overflow
  159.    if combined in that type.  The exceptions to this--the times when
  160.    two narrow values can be combined in their narrow type even to
  161.    make a wider result--are handled by "shorten" in build_binary_op.  */
  162.  
  163.       switch (ex_form)
  164.     {
  165.     case RSHIFT_EXPR:
  166.       /* We can pass truncation down through right shifting
  167.          when the shift count is a negative constant.  */
  168.       if (TREE_CODE (TREE_OPERAND (expr, 1)) != INTEGER_CST
  169.           || TREE_INT_CST_LOW (TREE_OPERAND (expr, 1)) > 0)
  170.         break;
  171.       goto trunc1;
  172.  
  173.     case LSHIFT_EXPR:
  174.       /* We can pass truncation down through left shifting
  175.          when the shift count is a positive constant.  */
  176.       if (TREE_CODE (TREE_OPERAND (expr, 1)) != INTEGER_CST
  177.           || TREE_INT_CST_LOW (TREE_OPERAND (expr, 1)) < 0)
  178.         break;
  179.       /* In this case, shifting is like multiplication.  */
  180.       goto trunc1;
  181.  
  182.     case MAX_EXPR:
  183.     case MIN_EXPR:
  184.     case MULT_EXPR:
  185.       {
  186.         tree arg0 = get_unwidened (TREE_OPERAND (expr, 0), type);
  187.         tree arg1 = get_unwidened (TREE_OPERAND (expr, 1), type);
  188.  
  189.         /* Don't distribute unless the output precision is at least as big
  190.            as the actual inputs.  Otherwise, the comparison of the
  191.            truncated values will be wrong.  */
  192.         if (outprec >= TYPE_PRECISION (TREE_TYPE (arg0))
  193.         && outprec >= TYPE_PRECISION (TREE_TYPE (arg1))
  194.         /* If signedness of arg0 and arg1 don't match,
  195.            we can't necessarily find a type to compare them in.  */
  196.         && (TREE_UNSIGNED (TREE_TYPE (arg0))
  197.             == TREE_UNSIGNED (TREE_TYPE (arg1))))
  198.           goto trunc1;
  199.         break;
  200.       }
  201.  
  202.     case PLUS_EXPR:
  203.     case MINUS_EXPR:
  204.     case BIT_AND_EXPR:
  205.     case BIT_IOR_EXPR:
  206.     case BIT_XOR_EXPR:
  207.     case BIT_ANDTC_EXPR:
  208.     trunc1:
  209.       {
  210.         tree arg0 = get_unwidened (TREE_OPERAND (expr, 0), type);
  211.         tree arg1 = get_unwidened (TREE_OPERAND (expr, 1), type);
  212.  
  213.         if (outprec >= BITS_PER_WORD
  214.         || TRULY_NOOP_TRUNCATION (outprec, inprec)
  215.         || inprec > TYPE_PRECISION (TREE_TYPE (arg0))
  216.         || inprec > TYPE_PRECISION (TREE_TYPE (arg1)))
  217.           {
  218.         /* Do the arithmetic in type TYPEX,
  219.            then convert result to TYPE.  */
  220.         register tree typex = type;
  221.  
  222.         /* Can't do arithmetic in enumeral types
  223.            so use an integer type that will hold the values.  */
  224.         if (TREE_CODE (typex) == ENUMERAL_TYPE)
  225.           typex = type_for_size (TYPE_PRECISION (typex),
  226.                      TREE_UNSIGNED (typex));
  227.  
  228.         /* But now perhaps TYPEX is as wide as INPREC.
  229.            In that case, do nothing special here.
  230.            (Otherwise would recurse infinitely in convert.  */
  231.         if (TYPE_PRECISION (typex) != inprec)
  232.           {
  233.             /* Don't do unsigned arithmetic where signed was wanted,
  234.                or vice versa.
  235.                Exception: if the original operands were unsigned
  236.                then can safely do the work as unsigned.
  237.                And we may need to do it as unsigned
  238.                if we truncate to the original size.  */
  239.             typex = ((TREE_UNSIGNED (TREE_TYPE (expr))
  240.                   || TREE_UNSIGNED (TREE_TYPE (arg0)))
  241.                  ? unsigned_type (typex) : signed_type (typex));
  242.             return convert (type,
  243.                     build_binary_op_nodefault (ex_form,
  244.                                    convert (typex, arg0),
  245.                                    convert (typex, arg1),
  246.                                    ex_form));
  247.           }
  248.           }
  249.       }
  250.       break;
  251.  
  252.     case EQ_EXPR:
  253.     case NE_EXPR:
  254.     case GT_EXPR:
  255.     case GE_EXPR:
  256.     case LT_EXPR:
  257.     case LE_EXPR:
  258.     case TRUTH_AND_EXPR:
  259.     case TRUTH_ANDIF_EXPR:
  260.     case TRUTH_OR_EXPR:
  261.     case TRUTH_ORIF_EXPR:
  262.     case TRUTH_NOT_EXPR:
  263.       /* If we want result of comparison converted to a byte,
  264.          we can just regard it as a byte, since it is 0 or 1.  */
  265.       TREE_TYPE (expr) = type;
  266.       return expr;
  267.  
  268.     case NEGATE_EXPR:
  269.     case BIT_NOT_EXPR:
  270.     case ABS_EXPR:
  271.       {
  272.         register tree typex = type;
  273.  
  274.         /* Can't do arithmetic in enumeral types
  275.            so use an integer type that will hold the values.  */
  276.         if (TREE_CODE (typex) == ENUMERAL_TYPE)
  277.           typex = type_for_size (TYPE_PRECISION (typex),
  278.                      TREE_UNSIGNED (typex));
  279.  
  280.         /* But now perhaps TYPEX is as wide as INPREC.
  281.            In that case, do nothing special here.
  282.            (Otherwise would recurse infinitely in convert.  */
  283.         if (TYPE_PRECISION (typex) != inprec)
  284.           {
  285.         /* Don't do unsigned arithmetic where signed was wanted,
  286.            or vice versa.  */
  287.         typex = (TREE_UNSIGNED (TREE_TYPE (expr))
  288.              ? unsigned_type (typex) : signed_type (typex));
  289.         return convert (type,
  290.                 build_unary_op (ex_form,
  291.                         convert (typex, TREE_OPERAND (expr, 0)),
  292.                         1));
  293.           }
  294.       }
  295.  
  296.     case NOP_EXPR:
  297.       /* If truncating after truncating, might as well do all at once.
  298.          If truncating after extending, we may get rid of wasted work.  */
  299.       return convert (type, get_unwidened (TREE_OPERAND (expr, 0), type));
  300.  
  301.     case COND_EXPR:
  302.       /* Can treat the two alternative values like the operands
  303.          of an arithmetic expression.  */
  304.       {
  305.         tree arg1 = get_unwidened (TREE_OPERAND (expr, 1), type);
  306.         tree arg2 = get_unwidened (TREE_OPERAND (expr, 2), type);
  307.  
  308.         if (outprec >= BITS_PER_WORD
  309.         || TRULY_NOOP_TRUNCATION (outprec, inprec)
  310.         || inprec > TYPE_PRECISION (TREE_TYPE (arg1))
  311.         || inprec > TYPE_PRECISION (TREE_TYPE (arg2)))
  312.           {
  313.         /* Do the arithmetic in type TYPEX,
  314.            then convert result to TYPE.  */
  315.         register tree typex = type;
  316.  
  317.         /* Can't do arithmetic in enumeral types
  318.            so use an integer type that will hold the values.  */
  319.         if (TREE_CODE (typex) == ENUMERAL_TYPE)
  320.           typex = type_for_size (TYPE_PRECISION (typex),
  321.                      TREE_UNSIGNED (typex));
  322.  
  323.         /* But now perhaps TYPEX is as wide as INPREC.
  324.            In that case, do nothing special here.
  325.            (Otherwise would recurse infinitely in convert.  */
  326.         if (TYPE_PRECISION (typex) != inprec)
  327.           {
  328.             /* Don't do unsigned arithmetic where signed was wanted,
  329.                or vice versa.  */
  330.             typex = (TREE_UNSIGNED (TREE_TYPE (expr))
  331.                  ? unsigned_type (typex) : signed_type (typex));
  332.             return convert (type,
  333.                     fold (build (COND_EXPR, typex,
  334.                          TREE_OPERAND (expr, 0),
  335.                          convert (typex, arg1),
  336.                          convert (typex, arg2))));
  337.           }
  338.           }
  339.       }
  340.     }
  341.  
  342.       return build1 (NOP_EXPR, type, expr);
  343.     }
  344.  
  345.   if (form == REAL_TYPE)
  346.     return build1 (FIX_TRUNC_EXPR, type, expr);
  347.  
  348.   error ("aggregate value used where an integer was expected");
  349.  
  350.   {
  351.     register tree tem = build_int_2 (0, 0);
  352.     TREE_TYPE (tem) = type;
  353.     return tem;
  354.   }
  355. }
  356.  
  357. /* Create an expression whose value is that of EXPR,
  358.    converted to type TYPE.  The TREE_TYPE of the value
  359.    is always TYPE.  This function implements all reasonable
  360.    conversions; callers should filter out those that are
  361.    not permitted by the language being compiled.  */
  362.  
  363. tree
  364. convert (type, expr)
  365.      tree type, expr;
  366. {
  367.   register tree e = expr;
  368.   register enum tree_code code = TREE_CODE (type);
  369.  
  370.   if (type == TREE_TYPE (expr) || TREE_CODE (expr) == ERROR_MARK)
  371.     return expr;
  372.   if (TREE_CODE (TREE_TYPE (expr)) == ERROR_MARK)
  373.     return error_mark_node;
  374.   if (TREE_CODE (TREE_TYPE (expr)) == VOID_TYPE)
  375.     {
  376.       error ("void value not ignored as it ought to be");
  377.       return error_mark_node;
  378.     }
  379.   if (code == VOID_TYPE)
  380.     return build1 (CONVERT_EXPR, type, e);
  381. #if 0
  382.   /* This is incorrect.  A truncation can't be stripped this way.
  383.      Extensions will be stripped by the use of get_unwidened.  */
  384.   if (TREE_CODE (expr) == NOP_EXPR)
  385.     return convert (type, TREE_OPERAND (expr, 0));
  386. #endif
  387.   if (code == INTEGER_TYPE || code == ENUMERAL_TYPE)
  388.     return fold (convert_to_integer (type, e));
  389.   if (code == POINTER_TYPE)
  390.     return fold (convert_to_pointer (type, e));
  391.   if (code == REAL_TYPE)
  392.     return fold (convert_to_real (type, e));
  393.  
  394.   error ("conversion to non-scalar type requested");
  395.   return error_mark_node;
  396. }
  397.