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

  1. /* Report error messages, build initializers, and perform
  2.    some front-end optimizations for C++ compiler.
  3.    Copyright (C) 1987, 1988, 1989 Free Software Foundation, Inc.
  4.    Hacked by Michael Tiemann (tiemann@cygnus.com)
  5.  
  6. This file is part of GNU CC.
  7.  
  8. GNU CC is free software; you can redistribute it and/or modify
  9. it under the terms of the GNU General Public License as published by
  10. the Free Software Foundation; either version 2, or (at your option)
  11. any later version.
  12.  
  13. GNU CC is distributed in the hope that it will be useful,
  14. but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  16. GNU General Public License for more details.
  17.  
  18. You should have received a copy of the GNU General Public License
  19. along with GNU CC; see the file COPYING.  If not, write to
  20. the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.  */
  21.  
  22.  
  23. /* This file is part of the C front end.
  24.    It contains routines to build C expressions given their operands,
  25.    including computing the types of the result, C-specific error checks,
  26.    and some optimization.
  27.  
  28.    There are also routines to build RETURN_STMT nodes and CASE_STMT nodes,
  29.    and to process initializations in declarations (since they work
  30.    like a strange sort of assignment).  */
  31.  
  32. #include "config.h"
  33. #include <stdio.h>
  34. #include "tree.h"
  35. #include "cplus-tree.h"
  36. #include "flags.h"
  37. #include "assert.h"
  38.  
  39. static tree process_init_constructor ();
  40. tree digest_init ();
  41. void incomplete_type_error ();
  42. void readonly_warning_or_error ();
  43. extern tree convert_for_initialization ();
  44.  
  45. /* Print an error message stemming from an attempt to use
  46.    BASETYPE as a base class for TYPE.  */
  47. void
  48. error_not_base_type (basetype, type)
  49.      tree basetype, type;
  50. {
  51.   tree name1 = TYPE_NAME (basetype);
  52.   tree name2 = TYPE_NAME (type);
  53.   if (TREE_CODE (name1) == TYPE_DECL)
  54.     name1 = DECL_NAME (name1);
  55.   if (TREE_CODE (name2) == TYPE_DECL)
  56.     name2 = DECL_NAME (name2);
  57.   error ("type `%s' is not a base type for type `%s'",
  58.      IDENTIFIER_POINTER (name1), IDENTIFIER_POINTER (name2));
  59. }
  60.  
  61. tree
  62. basetype_or_else (parent_or_type, type)
  63.      tree parent_or_type, type;
  64. {
  65.   tree basetype;
  66.   if (TYPE_MAIN_VARIANT (parent_or_type) == TYPE_MAIN_VARIANT (type))
  67.     return parent_or_type;
  68.   if (basetype = get_base_type (parent_or_type, TYPE_MAIN_VARIANT (type), 0))
  69.     {
  70.       if (basetype == error_mark_node)
  71.     return NULL_TREE;
  72.       return basetype;
  73.     }
  74.   error_not_base_type (parent_or_type, type);
  75.   return NULL_TREE;
  76. }
  77.  
  78. /* Print an error message stemming from an invalid use of an
  79.    aggregate type.
  80.  
  81.    TYPE is the type which draws the error.
  82.    MSG is the message to print.
  83.    ARG is an optional argument which may provide more information.  */
  84. void
  85. error_with_aggr_type (type, msg, arg)
  86.      tree type;
  87.      char *msg;
  88.      int arg;
  89. {
  90.   tree name = TYPE_NAME (type);
  91.   if (TREE_CODE (name) == TYPE_DECL)
  92.     name = DECL_NAME (name);
  93.   error (msg, IDENTIFIER_POINTER (name), arg);
  94. }
  95.  
  96. /* Warn or give error about storing in something that is `const'.  */
  97.  
  98. void
  99. readonly_warning_or_error (arg, string)
  100.      tree arg;
  101.      char *string;
  102. {
  103.   char buf[80];
  104.   strcpy (buf, string);
  105.  
  106.   if (TREE_CODE (arg) == COMPONENT_REF)
  107.     {
  108.       if (TYPE_READONLY (TREE_TYPE (TREE_OPERAND (arg, 0))))
  109.         strcat (buf, " of member `%s' in read-only structure");
  110.       else
  111.         strcat (buf, " of read-only member `%s'");
  112.       error (buf, lang_printable_name (TREE_OPERAND (arg, 1)));
  113.     }
  114.   else if (TREE_CODE (arg) == VAR_DECL)
  115.     {
  116.       if (DECL_LANG_SPECIFIC (arg)
  117.       && DECL_IN_AGGR_P (arg)
  118.       && !TREE_STATIC (arg))
  119.     strcat (buf, " of constant field `%s'");
  120.       else
  121.     strcat (buf, " of read-only variable `%s'");
  122.       error (buf, lang_printable_name (arg));
  123.     }
  124.   else if (TREE_CODE (arg) == PARM_DECL)
  125.     {
  126.       strcat (buf, " of read-only parameter `%s'");
  127.       error (buf, lang_printable_name (arg));
  128.     }
  129.   else if (TREE_CODE (arg) == INDIRECT_REF
  130.            && TREE_CODE (TREE_TYPE (TREE_OPERAND (arg, 0))) == REFERENCE_TYPE
  131.            && (TREE_CODE (TREE_OPERAND (arg, 0)) == VAR_DECL
  132.                || TREE_CODE (TREE_OPERAND (arg, 0)) == PARM_DECL))
  133.     {
  134.       strcat (buf, " of read-only reference `%s'");
  135.       error (buf, lang_printable_name (TREE_OPERAND (arg, 0)));
  136.     }
  137.   else           
  138.     {
  139.       warning ("%s of read-only location", buf);
  140.     }
  141. }
  142.  
  143. /* Print an error message for invalid use of a type which declares
  144.    virtual functions which are not inheritable.  */
  145. void
  146. abstract_virtuals_error (decl, type)
  147.      tree decl;
  148.      tree type;
  149. {
  150.   char *typename = TYPE_NAME_STRING (type);
  151.   tree u = CLASSTYPE_ABSTRACT_VIRTUALS (type);
  152.  
  153.   if (decl)
  154.     {
  155.       if (TREE_CODE (decl) == RESULT_DECL)
  156.     return;
  157.  
  158.       if (TREE_CODE (decl) == VAR_DECL)
  159.     error_with_decl (decl, "cannot declare variable `%s' to be of type `%s'", typename);
  160.       else if (TREE_CODE (decl) == PARM_DECL)
  161.     error_with_decl (decl, "cannot declare parameter `%s' to be of type `%s'", typename);
  162.       else if (TREE_CODE (decl) == FIELD_DECL)
  163.     error_with_decl (decl, "cannot declare field `%s' to be of type `%s'", typename);
  164.       else if (TREE_CODE (decl) == FUNCTION_DECL)
  165.     error_with_decl (decl, "invalid return type for function `%s'");
  166.     }
  167.   else error ("cannot allocate an object of type `%s'", typename);
  168.   /* Only go through this once.  */
  169.   if (TREE_PURPOSE (u) == NULL_TREE)
  170.     {
  171.       error ("since the following virtual functions are abstract:");
  172.       TREE_PURPOSE (u) = error_mark_node;
  173.       while (u)
  174.     {
  175.       error_with_decl (TREE_VALUE (u), "%s");
  176.       u = TREE_CHAIN (u);
  177.     }
  178.     }
  179.   else error ("since type `%s' has abstract virtual functions", typename);
  180. }
  181.  
  182. /* Print an error message for invalid use of an incomplete type.
  183.    VALUE is the expression that was used (or 0 if that isn't known)
  184.    and TYPE is the type that was invalid.  */
  185.  
  186. void
  187. incomplete_type_error (value, type)
  188.      tree value;
  189.      tree type;
  190. {
  191.   char *errmsg;
  192.  
  193.   /* Avoid duplicate error message.  */
  194.   if (TREE_CODE (type) == ERROR_MARK)
  195.     return;
  196.  
  197.   if (value != 0 && (TREE_CODE (value) == VAR_DECL
  198.              || TREE_CODE (value) == PARM_DECL))
  199.     error ("`%s' has an incomplete type",
  200.        IDENTIFIER_POINTER (DECL_NAME (value)));
  201.   else
  202.     {
  203.     retry:
  204.       /* We must print an error message.  Be clever about what it says.  */
  205.  
  206.       switch (TREE_CODE (type))
  207.     {
  208.     case RECORD_TYPE:
  209.       errmsg = "invalid use of undefined type `struct %s'";
  210.       break;
  211.  
  212.     case UNION_TYPE:
  213.       errmsg = "invalid use of undefined type `union %s'";
  214.       break;
  215.  
  216.     case ENUMERAL_TYPE:
  217.       errmsg = "invalid use of undefined type `enum %s'";
  218.       break;
  219.  
  220.     case VOID_TYPE:
  221.       error ("invalid use of void expression");
  222.       return;
  223.  
  224.     case ARRAY_TYPE:
  225.       if (TYPE_DOMAIN (type))
  226.         {
  227.           type = TREE_TYPE (type);
  228.           goto retry;
  229.         }
  230.       error ("invalid use of array with unspecified bounds");
  231.       return;
  232.  
  233.     case OFFSET_TYPE:
  234.       error ("invalid use of member type (did you forget the `&' ?)");
  235.       return;
  236.  
  237.     default:
  238.       abort ();
  239.     }
  240.  
  241.       error_with_aggr_type (type, errmsg);
  242.     }
  243. }
  244.  
  245. /* Return nonzero if VALUE is a valid constant-valued expression
  246.    for use in initializing a static variable; one that can be an
  247.    element of a "constant" initializer.
  248.  
  249.    Return 1 if the value is absolute; return 2 if it is relocatable.
  250.    We assume that VALUE has been folded as much as possible;
  251.    therefore, we do not need to check for such things as
  252.    arithmetic-combinations of integers.  */
  253.  
  254. static int
  255. initializer_constant_valid_p (value)
  256.      tree value;
  257. {
  258.   switch (TREE_CODE (value))
  259.     {
  260.     case CONSTRUCTOR:
  261.       return TREE_STATIC (value);
  262.  
  263.     case INTEGER_CST:
  264.     case REAL_CST:
  265.     case STRING_CST:
  266.       return 1;
  267.  
  268.     case ADDR_EXPR:
  269.       return 2;
  270.  
  271.     case CONVERT_EXPR:
  272.     case NOP_EXPR:
  273.       /* Allow conversions between types of the same kind.  */
  274.       if (TREE_CODE (TREE_TYPE (value))
  275.       == TREE_CODE (TREE_TYPE (TREE_OPERAND (value, 0))))
  276.     return initializer_constant_valid_p (TREE_OPERAND (value, 0));
  277.       /* Allow (int) &foo.  */
  278.       if (TREE_CODE (TREE_TYPE (value)) == INTEGER_TYPE
  279.       && TREE_CODE (TREE_TYPE (TREE_OPERAND (value, 0))) == POINTER_TYPE)
  280.     return initializer_constant_valid_p (TREE_OPERAND (value, 0));
  281.       return 0;
  282.  
  283.     case PLUS_EXPR:
  284.       {
  285.     int valid0 = initializer_constant_valid_p (TREE_OPERAND (value, 0));
  286.     int valid1 = initializer_constant_valid_p (TREE_OPERAND (value, 1));
  287.     if (valid0 == 1 && valid1 == 2)
  288.       return 2;
  289.     if (valid0 == 2 && valid1 == 1)
  290.       return 2;
  291.     return 0;
  292.       }
  293.  
  294.     case MINUS_EXPR:
  295.       {
  296.     int valid0 = initializer_constant_valid_p (TREE_OPERAND (value, 0));
  297.     int valid1 = initializer_constant_valid_p (TREE_OPERAND (value, 1));
  298.     if (valid0 == 2 && valid1 == 1)
  299.       return 2;
  300.     return 0;
  301.       }
  302.     }
  303.  
  304.   return 0;
  305. }
  306.  
  307. /* Perform appropriate conversions on the initial value of a variable,
  308.    store it in the declaration DECL,
  309.    and print any error messages that are appropriate.
  310.    If the init is invalid, store an ERROR_MARK.
  311.  
  312.    C++: Note that INIT might be a TREE_LIST, which would mean that it is
  313.    a base class initializer for some aggregate type, hopefully compatible
  314.    with DECL.  If INIT is a single element, and DECL is an aggregate
  315.    type, we silently convert INIT into a TREE_LIST, allowing a constructor
  316.    to be called.
  317.  
  318.    If INIT is a TREE_LIST and there is no constructor, turn INIT
  319.    into a CONSTRUCTOR and use standard initialization techniques.
  320.    Perhaps a warning should be generated?
  321.  
  322.    Returns value of initializer if initialization could not be
  323.    performed for static variable.  In that case, caller must do
  324.    the storing.  */
  325.  
  326. tree
  327. store_init_value (decl, init)
  328.      tree decl, init;
  329. {
  330.   register tree value, type;
  331.  
  332.   /* If variable's type was invalidly declared, just ignore it.  */
  333.  
  334.   type = TREE_TYPE (decl);
  335.   if (TREE_CODE (type) == ERROR_MARK)
  336.     return NULL_TREE;
  337.  
  338.   /* Take care of C++ business up here.  */
  339.   type = TYPE_MAIN_VARIANT (type);
  340.  
  341.   /* implicitly tests if IS_AGGR_TYPE.  */
  342.   if (TYPE_NEEDS_CONSTRUCTING (type))
  343.     abort ();
  344.   else if (IS_AGGR_TYPE (type))
  345.     {
  346.       /* @@ This may be wrong, but I do not know what is right.  */
  347.       if (TREE_CODE (init) == TREE_LIST)
  348.     {
  349.       error_with_aggr_type (type, "constructor syntax used, but no constructor declared for type `%s'");
  350.       init = build_nt (CONSTRUCTOR, NULL_TREE, nreverse (init));
  351.     }
  352.     }
  353.   else if (TREE_CODE (init) == TREE_LIST
  354.        && TREE_TYPE (init) != unknown_type_node)
  355.     {
  356.       if (TREE_CODE (decl) == RESULT_DECL)
  357.     {
  358.       if (TREE_CHAIN (init))
  359.         {
  360.           warning ("comma expression used to initialize return value");
  361.           init = build_compound_expr (init);
  362.         }
  363.       else
  364.         init = TREE_VALUE (init);
  365.     }
  366.       else if (TREE_TYPE (init) != 0
  367.            && TREE_CODE (TREE_TYPE (init)) == OFFSET_TYPE)
  368.     {
  369.       /* Use the type of our variable to instantiate
  370.          the type of our initializer.  */
  371.       init = instantiate_type (type, init, 1);
  372.     }
  373.       else abort ();
  374.     }
  375.  
  376.   /* End of special C++ code.  */
  377.  
  378.   /* Digest the specified initializer into an expression.  */
  379.  
  380.   value = digest_init (type, init, 0);
  381.  
  382.   /* Store the expression if valid; else report error.  */
  383.  
  384.   if (TREE_CODE (value) == ERROR_MARK)
  385.     ;
  386.   else if (TREE_STATIC (decl)
  387.        && (! TREE_CONSTANT (value)
  388.            || ! initializer_constant_valid_p (value)
  389.            || (flag_pic && TREE_PUBLIC (decl))))
  390.     return value;
  391.   else
  392.     {
  393.       if (pedantic && TREE_CODE (value) == CONSTRUCTOR)
  394.     {
  395.       if (! TREE_CONSTANT (value))
  396.         warning ("aggregate initializer is not constant");
  397.       else if (! TREE_STATIC (value))
  398.         warning ("aggregate initializer uses complicated arithmetic");
  399.     }
  400.     }
  401.   DECL_INITIAL (decl) = value;
  402.   return NULL_TREE;
  403. }
  404.  
  405. /* Digest the parser output INIT as an initializer for type TYPE.
  406.    Return a C expression of type TYPE to represent the initial value.
  407.  
  408.    If TAIL is nonzero, it points to a variable holding a list of elements
  409.    of which INIT is the first.  We update the list stored there by
  410.    removing from the head all the elements that we use.
  411.    Normally this is only one; we use more than one element only if
  412.    TYPE is an aggregate and INIT is not a constructor.  */
  413.  
  414. tree
  415. digest_init (type, init, tail)
  416.      tree type, init, *tail;
  417. {
  418.   enum tree_code code = TREE_CODE (type);
  419.   tree element = 0;
  420.   tree old_tail_contents;
  421.   /* Nonzero if INIT is a braced grouping, which comes in as a CONSTRUCTOR
  422.      tree node which has no TREE_TYPE.  */
  423.   int raw_constructor
  424.     = TREE_CODE (init) == CONSTRUCTOR && TREE_TYPE (init) == 0;
  425.  
  426.   /* By default, assume we use one element from a list.
  427.      We correct this later in the sole case where it is not true.  */
  428.  
  429.   if (tail)
  430.     {
  431.       old_tail_contents = *tail;
  432.       *tail = TREE_CHAIN (*tail);
  433.     }
  434.  
  435.   if (init == error_mark_node)
  436.     return init;
  437.  
  438.   /* Strip NON_LVALUE_EXPRs since we aren't using as an lvalue.  */
  439.   if (TREE_CODE (init) == NON_LVALUE_EXPR)
  440.     init = TREE_OPERAND (init, 0);
  441.  
  442.   if (init && raw_constructor
  443.       && CONSTRUCTOR_ELTS (init) != 0
  444.       && TREE_CHAIN (CONSTRUCTOR_ELTS (init)) == 0)
  445.     {
  446.       element = TREE_VALUE (CONSTRUCTOR_ELTS (init));
  447.       /* Strip NON_LVALUE_EXPRs since we aren't using as an lvalue.  */
  448.       if (element && TREE_CODE (element) == NON_LVALUE_EXPR)
  449.     element = TREE_OPERAND (element, 0);
  450.       if (element == error_mark_node)
  451.     return element;
  452.     }
  453.  
  454.   /* Any type can be initialized from an expression of the same type,
  455.      optionally with braces.  */
  456.  
  457.   if (init && TREE_TYPE (init)
  458.       && (TYPE_MAIN_VARIANT (TREE_TYPE (init)) == type
  459.       || (code == ARRAY_TYPE && comptypes (TREE_TYPE (init), type, 1))))
  460.     {
  461.       if (pedantic && code == ARRAY_TYPE
  462.       && TREE_CODE (init) != STRING_CST)
  463.     warning ("ANSI C forbids initializing array from array expression");
  464.       if (TREE_CODE (init) == CONST_DECL)
  465.     init = DECL_INITIAL (init);
  466.       else if (TREE_READONLY_DECL_P (init))
  467.     init = decl_constant_value (init);
  468.       return init;
  469.     }
  470.  
  471.   if (element && (TREE_TYPE (element) == type
  472.           || (code == ARRAY_TYPE && TREE_TYPE (element)
  473.               && comptypes (TREE_TYPE (element), type, 1))))
  474.     {
  475.       if (pedantic && code == ARRAY_TYPE)
  476.     warning ("ANSI C forbids initializing array from array expression");
  477.       if (pedantic && (code == RECORD_TYPE || code == UNION_TYPE))
  478.     warning ("single-expression nonscalar initializer has braces");
  479.       if (TREE_CODE (element) == CONST_DECL)
  480.     element = DECL_INITIAL (element);
  481.       else if (TREE_READONLY_DECL_P (element))
  482.     element = decl_constant_value (element);
  483.       return element;
  484.     }
  485.  
  486.   /* Check for initializing a union by its first field.
  487.      Such an initializer must use braces.  */
  488.  
  489.   if (code == UNION_TYPE)
  490.     {
  491.       tree result, field = TYPE_FIELDS (type);
  492.  
  493.       /* Find the first named field.  ANSI decided in September 1990
  494.      that only named fields count here.  */
  495.       while (field && DECL_NAME (field) == 0)
  496.     field = TREE_CHAIN (field);
  497.  
  498.       if (field == 0)
  499.     {
  500.       error ("union with no named members cannot be initialized");
  501.       return error_mark_node;
  502.     }
  503.       if (! raw_constructor)
  504.     {
  505.       error ("type mismatch in initialization");
  506.       return error_mark_node;
  507.     }
  508.       if (element == 0)
  509.     {
  510.       if (!TYPE_NEEDS_CONSTRUCTING (type))
  511.         {
  512.           error ("union initializer requires one element");
  513.           return error_mark_node;
  514.         }
  515.     }
  516.       else
  517.     {
  518.       /* Take just the first element from within the constructor
  519.          and it should match the type of the first element.  */
  520.       element = digest_init (TREE_TYPE (TYPE_FIELDS (type)), element, 0);
  521.       result = build (CONSTRUCTOR, type, 0, build_tree_list (0, element));
  522.       TREE_CONSTANT (result) = TREE_CONSTANT (element);
  523.       TREE_STATIC (result) = (initializer_constant_valid_p (element)
  524.                   && TREE_CONSTANT (element));
  525.       return result;
  526.     }
  527.     }
  528.  
  529.   /* Initialization of an array of chars from a string constant
  530.      optionally enclosed in braces.  */
  531.  
  532.   if (code == ARRAY_TYPE)
  533.     {
  534.       tree typ1 = TYPE_MAIN_VARIANT (TREE_TYPE (type));
  535.       if ((typ1 == char_type_node
  536.        || typ1 == signed_char_type_node
  537.        || typ1 == unsigned_char_type_node
  538.        || typ1 == unsigned_wchar_type_node
  539.        || typ1 == signed_wchar_type_node)
  540.       && ((init && TREE_CODE (init) == STRING_CST)
  541.           || (element && TREE_CODE (element) == STRING_CST)))
  542.     {
  543.       tree string = element ? element : init;
  544.  
  545.       if ((TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (string)))
  546.            != char_type_node)
  547.           && TYPE_PRECISION (typ1) == BITS_PER_UNIT)
  548.         {
  549.           error ("char-array initialized from wide string");
  550.           return error_mark_node;
  551.         }
  552.       if ((TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (string)))
  553.            == char_type_node)
  554.           && TYPE_PRECISION (typ1) != BITS_PER_UNIT)
  555.         {
  556.           error ("int-array initialized from non-wide string");
  557.           return error_mark_node;
  558.         }
  559.  
  560.       if (pedantic && typ1 != char_type_node)
  561.         warning ("ANSI C forbids string initializer except for `char' elements");
  562.       TREE_TYPE (string) = type;
  563.       if (TYPE_DOMAIN (type) != 0
  564.           && TREE_CONSTANT (TYPE_SIZE (type)))
  565.         {
  566.           register int size
  567.         = TREE_INT_CST_LOW (TYPE_SIZE (type));
  568.           size = (size + BITS_PER_UNIT - 1) / BITS_PER_UNIT;
  569.           /* Subtract 1 because it's ok to ignore the terminating null char
  570.          that is counted in the length of the constant.  */
  571.           if (size < TREE_STRING_LENGTH (string) - 1)
  572.         warning ("initializer-string for array of chars is too long");
  573.         }
  574.       return string;
  575.     }
  576.     }
  577.  
  578.   /* Handle scalar types, including conversions.  */
  579.  
  580.   if (code == INTEGER_TYPE || code == REAL_TYPE || code == POINTER_TYPE
  581.       || code == ENUMERAL_TYPE || code == REFERENCE_TYPE)
  582.     {
  583.       if (raw_constructor)
  584.     {
  585.       if (element == 0)
  586.         {
  587.           error ("initializer for scalar variable requires one element");
  588.           return error_mark_node;
  589.         }
  590.       init = element;
  591.     }
  592.  
  593.       return convert_for_initialization (0, type, init, LOOKUP_NORMAL,
  594.                      "initialization", NULL_TREE, 0);
  595.     }
  596.  
  597.   /* Come here only for records and arrays (and unions with constructors).  */
  598.  
  599.   if (TYPE_SIZE (type) && ! TREE_CONSTANT (TYPE_SIZE (type)))
  600.     {
  601.       error ("variable-sized object may not be initialized");
  602.       return error_mark_node;
  603.     }
  604.  
  605.   if (code == ARRAY_TYPE || code == RECORD_TYPE || code == UNION_TYPE)
  606.     {
  607.       if (raw_constructor)
  608.     return process_init_constructor (type, init, 0);
  609.       else if (TYPE_NEEDS_CONSTRUCTING (type))
  610.     {
  611.       /* This can only be reached when caller is initializing
  612.          ARRAY_TYPE.  In that case, we don't want to convert
  613.          INIT to TYPE.  We will let `expand_vec_init' do it.  */
  614.       return init;
  615.     }
  616.       else if (tail != 0)
  617.     {
  618.       *tail = old_tail_contents;
  619.       return process_init_constructor (type, 0, tail);
  620.     }
  621.       else if (flag_traditional)
  622.     /* Traditionally one can say `char x[100] = 0;'.  */
  623.     return process_init_constructor (type,
  624.                      build_nt (CONSTRUCTOR, 0,
  625.                            tree_cons (0, init, 0)),
  626.                      0);
  627.       if (code != ARRAY_TYPE)
  628.     return convert_for_initialization (0, type, init, LOOKUP_NORMAL,
  629.                        "initialization", NULL_TREE, 0);
  630.     }
  631.  
  632.   error ("invalid initializer");
  633.   return error_mark_node;
  634. }
  635.  
  636. /* Process a constructor for a variable of type TYPE.
  637.    The constructor elements may be specified either with INIT or with ELTS,
  638.    only one of which should be non-null.
  639.  
  640.    If INIT is specified, it is a CONSTRUCTOR node which is specifically
  641.    and solely for initializing this datum.
  642.  
  643.    If ELTS is specified, it is the address of a variable containing
  644.    a list of expressions.  We take as many elements as we need
  645.    from the head of the list and update the list.
  646.  
  647.    In the resulting constructor, TREE_CONSTANT is set if all elts are
  648.    constant, and TREE_STATIC is set if, in addition, all elts are simple enough
  649.    constants that the assembler and linker can compute them.  */
  650.  
  651. static tree
  652. process_init_constructor (type, init, elts)
  653.      tree type, init, *elts;
  654. {
  655.   extern tree empty_init_node;
  656.   register tree tail;
  657.   /* List of the elements of the result constructor,
  658.      in reverse order.  */
  659.   register tree members = NULL;
  660.   tree result;
  661.   int allconstant = 1;
  662.   int allsimple = 1;
  663.   int erred = 0;
  664.  
  665.   /* Make TAIL be the list of elements to use for the initialization,
  666.      no matter how the data was given to us.  */
  667.  
  668.   if (elts)
  669.     tail = *elts;
  670.   else
  671.     tail = CONSTRUCTOR_ELTS (init);
  672.  
  673.   /* Gobble as many elements as needed, and make a constructor or initial value
  674.      for each element of this aggregate.  Chain them together in result.
  675.      If there are too few, use 0 for each scalar ultimate component.  */
  676.  
  677.   if (TREE_CODE (type) == ARRAY_TYPE)
  678.     {
  679.       tree domain = TYPE_DOMAIN (type);
  680.       register long len;
  681.       register int i;
  682.  
  683.       if (domain)
  684.     len = (TREE_INT_CST_LOW (TYPE_MAX_VALUE (domain))
  685.            - TREE_INT_CST_LOW (TYPE_MIN_VALUE (domain))
  686.            + 1);
  687.       else
  688.     len = -1;  /* Take as many as there are */
  689.  
  690.       for (i = 0; (len < 0 || i < len) && tail != 0; i++)
  691.     {
  692.       register tree next1;
  693.  
  694.       if (TREE_VALUE (tail) != 0)
  695.         {
  696.           tree tail1 = tail;
  697.           next1 = digest_init (TYPE_MAIN_VARIANT (TREE_TYPE (type)),
  698.                    TREE_VALUE (tail), &tail1);
  699.           assert (tail1 == 0 || TREE_CODE (tail1) == TREE_LIST);
  700.           if (tail == tail1 && len < 0)
  701.         {
  702.           error ("non-empty initializer for array of empty elements");
  703.           /* Just ignore what we were supposed to use.  */
  704.           tail1 = 0;
  705.         }
  706.           tail = tail1;
  707.         }
  708.       else
  709.         {
  710.           next1 = error_mark_node;
  711.           tail = TREE_CHAIN (tail);
  712.         }
  713.  
  714.       if (next1 == error_mark_node)
  715.         erred = 1;
  716.       else if (!TREE_CONSTANT (next1))
  717.         allconstant = 0;
  718.       else if (! initializer_constant_valid_p (next1))
  719.         allsimple = 0;
  720.       members = tree_cons (NULL_TREE, next1, members);
  721.     }
  722.     }
  723.   if (TREE_CODE (type) == RECORD_TYPE && init != empty_init_node)
  724.     {
  725.       register tree field;
  726.  
  727.       if (tail)
  728.     {
  729.       if (TYPE_USES_VIRTUAL_BASECLASSES (type))
  730.         {
  731.           sorry ("initializer list for object of class with virtual baseclasses");
  732.           return error_mark_node;
  733.         }
  734.  
  735.       if (TYPE_BASETYPES (type))
  736.         {
  737.           sorry ("initializer list for object of class with baseclasses");
  738.           return error_mark_node;
  739.         }
  740.  
  741.       if (TYPE_VIRTUAL_P (type))
  742.         {
  743.           sorry ("initializer list for object using virtual functions");
  744.           return error_mark_node;
  745.         }
  746.     }
  747.  
  748.       for (field = TYPE_FIELDS (type); field && tail;
  749.        field = TREE_CHAIN (field))
  750.     {
  751.       register tree next1;
  752.  
  753.       if (! DECL_NAME (field))
  754.         {
  755.           members = tree_cons (field, integer_zero_node, members);
  756.           continue;
  757.         }
  758.  
  759.       if (TREE_CODE (field) == CONST_DECL || TREE_CODE (field) == TYPE_DECL)
  760.         continue;
  761.       if (TREE_CODE (field) == VAR_DECL && !TREE_STATIC (field))
  762.         continue;
  763.  
  764.       if (TREE_VALUE (tail) != 0)
  765.         {
  766.           tree tail1 = tail;
  767.           next1 = digest_init (TREE_TYPE (field),
  768.                    TREE_VALUE (tail), &tail1);
  769.           assert (tail1 == 0 || TREE_CODE (tail1) == TREE_LIST);
  770.           if (TREE_CODE (field) == VAR_DECL
  771.           && ! global_bindings_p ())
  772.         warning_with_decl (field, "initialization of static member `%s'");
  773.           tail = tail1;
  774.         }
  775.       else
  776.         {
  777.           next1 = error_mark_node;
  778.           tail = TREE_CHAIN (tail);
  779.         }
  780.  
  781.       if (next1 == error_mark_node)
  782.         erred = 1;
  783.       else if (!TREE_CONSTANT (next1))
  784.         allconstant = 0;
  785.       else if (! initializer_constant_valid_p (next1))
  786.         allsimple = 0;
  787.       members = tree_cons (field, next1, members);
  788.     }
  789.       for (; field; field = TREE_CHAIN (field))
  790.     {
  791.       if (TREE_CODE (field) != FIELD_DECL)
  792.         continue;
  793.  
  794.       /* Does this field have a default initializtion?  */
  795.       if (DECL_INITIAL (field))
  796.         {
  797.           register tree next1 = DECL_INITIAL (field);
  798.           if (TREE_CODE (next1) == ERROR_MARK)
  799.         erred = 1;
  800.           else if (!TREE_CONSTANT (next1))
  801.         allconstant = 0;
  802.           else if (! initializer_constant_valid_p (next1))
  803.         allsimple = 0;
  804.           members = tree_cons (field, next1, members);
  805.         }
  806.       else if (TREE_READONLY (field))
  807.         error ("uninitialized const member `%s'",
  808.            IDENTIFIER_POINTER (DECL_NAME (field)));
  809.       else if (TYPE_LANG_SPECIFIC (TREE_TYPE (field))
  810.            && CLASSTYPE_READONLY_FIELDS_NEED_INIT (TREE_TYPE (field)))
  811.         error ("member `%s' with uninitialized const fields",
  812.            IDENTIFIER_POINTER (DECL_NAME (field)));
  813.       else if (TREE_CODE (TREE_TYPE (field)) == REFERENCE_TYPE)
  814.         error ("member `%s' is uninitialized reference",
  815.            IDENTIFIER_POINTER (DECL_NAME (field)));
  816.     }
  817.     }
  818.  
  819.   /* If arguments were specified as a list, just remove the ones we used.  */
  820.   if (elts)
  821.     *elts = tail;
  822.   /* If arguments were specified as a constructor,
  823.      complain unless we used all the elements of the constructor.  */
  824.   else if (tail)
  825.     warning ("excess elements in aggregate initializer");
  826.  
  827.   if (erred)
  828.     return error_mark_node;
  829.  
  830.   result = build (CONSTRUCTOR, type, NULL_TREE, nreverse (members));
  831.   if (init)
  832.     TREE_HAS_CONSTRUCTOR (result) = TREE_HAS_CONSTRUCTOR (init);
  833.   if (allconstant) TREE_CONSTANT (result) = 1;
  834.   if (allconstant && allsimple) TREE_STATIC (result) = 1;
  835.   return result;
  836. }
  837.  
  838. /* Given a structure or union value DATUM, construct and return
  839.    the structure or union component which results from narrowing
  840.    that value by the types specified in TYPES.  For example, given the
  841.    hierarchy
  842.  
  843.    class L { int ii; };
  844.    class A : L { ... };
  845.    class B : L { ... };
  846.    class C : A, B { ... };
  847.  
  848.    and the declaration
  849.  
  850.    C x;
  851.  
  852.    then the expression
  853.  
  854.    x::C::A::L::ii refers to the ii member of the L part of
  855.    of A part of the C object named by X.  In this case,
  856.    DATUM would be x, and TYPES would be a SCOPE_REF consisting of
  857.  
  858.     SCOPE_REF
  859.         SCOPE_REF
  860.             C    A
  861.         L
  862.  
  863.    The last entry in the SCOPE_REF is always an IDENTIFIER_NODE.
  864.  
  865. */
  866.  
  867. tree
  868. build_scoped_ref (datum, types)
  869.      tree datum;
  870.      tree types;
  871. {
  872.   tree orig_ref, ref;
  873.   tree type = TREE_TYPE (datum);
  874.  
  875.   if (datum == error_mark_node)
  876.     return error_mark_node;
  877.   type = TYPE_MAIN_VARIANT (type);
  878.  
  879.   if (TREE_CODE (types) == SCOPE_REF)
  880.     {
  881.       /* We have some work to do.  */
  882.       struct type_chain { tree type; struct type_chain *next; } *chain = 0, *head = 0;
  883.       orig_ref = ref = build_unary_op (ADDR_EXPR, datum, 0);
  884.       while (TREE_CODE (types) == SCOPE_REF)
  885.     {
  886.       tree t = TREE_OPERAND (types, 1);
  887.       if (is_aggr_typedef (t, 1))
  888.         {
  889.           head = (struct type_chain *)alloca (sizeof (struct type_chain));
  890.           head->type = IDENTIFIER_TYPE_VALUE (t);
  891.           head->next = chain;
  892.           chain = head;
  893.           types = TREE_OPERAND (types, 0);
  894.         }
  895.       else return error_mark_node;
  896.     }
  897.       if (! is_aggr_typedef (types, 1))
  898.     return error_mark_node;
  899.  
  900.       head = (struct type_chain *)alloca (sizeof (struct type_chain));
  901.       head->type = IDENTIFIER_TYPE_VALUE (types);
  902.       head->next = chain;
  903.       chain = head;
  904.       while (chain)
  905.     {
  906.       tree basetype = chain->type;
  907.       type = TREE_TYPE (TREE_TYPE (ref));
  908.       if (basetype != type)
  909.         {
  910.           basetype = get_base_type (basetype, type, 1);
  911.           if (basetype == error_mark_node)
  912.         return error_mark_node;
  913.           if (basetype == 0)
  914.         {
  915.           error_not_base_type (TYPE_NAME_STRING (chain->type), TYPE_NAME_STRING (type));
  916.           return error_mark_node;
  917.         }
  918.           ref = convert_pointer_to (basetype, ref);
  919.         }
  920.       chain = chain->next;
  921.     }
  922.       return build_indirect_ref (ref, "(compiler error in build_scoped_ref)");
  923.     }
  924.  
  925.   /* This is an easy conversion.  */
  926.   if (is_aggr_typedef (types, 1))
  927.     {
  928.       tree basetype = IDENTIFIER_TYPE_VALUE (types);
  929.       if (basetype != type)
  930.     {
  931.       basetype = get_base_type (basetype, type, 1);
  932.       if (basetype == error_mark_node)
  933.         return error_mark_node;
  934.       if (basetype == 0)
  935.         {
  936.           error_not_base_type (IDENTIFIER_TYPE_VALUE (types), type);
  937.           return error_mark_node;
  938.         }
  939.     }
  940.  
  941.       switch (TREE_CODE (datum))
  942.     {
  943.     case NOP_EXPR:
  944.     case CONVERT_EXPR:
  945.     case FLOAT_EXPR:
  946.     case FIX_TRUNC_EXPR:
  947.     case FIX_FLOOR_EXPR:
  948.     case FIX_ROUND_EXPR:
  949.     case FIX_CEIL_EXPR:
  950.       ref = convert_pointer_to (basetype,
  951.                     build_unary_op (ADDR_EXPR, TREE_OPERAND (datum, 0), 0));
  952.       break;
  953.     default:
  954.       ref = convert_pointer_to (basetype,
  955.                     build_unary_op (ADDR_EXPR, datum, 0));
  956.     }
  957.       return build_indirect_ref (ref, "(compiler error in build_scoped_ref)");
  958.     }
  959.   return error_mark_node;
  960. }
  961.  
  962. /* Build a reference to an object specified by the C++ `->' operator.
  963.    Usually this just involves dereferencing the object, but if the
  964.    `->' operator is overloaded, then such overloads must be
  965.    performed until an object which does not have the `->' operator
  966.    overloaded is found.  An error is reported when circular pointer
  967.    delegation is detected.  */
  968. tree
  969. build_x_arrow (datum)
  970.      tree datum;
  971. {
  972.   tree types_memoized = NULL_TREE;
  973.   tree rval = datum;
  974.   tree last_rval = default_conversion (datum);
  975.  
  976.   if (last_rval == error_mark_node)
  977.     return error_mark_node;
  978.  
  979.   while (rval = build_opfncall (COMPONENT_REF, LOOKUP_NORMAL, rval))
  980.     {
  981.       if (rval == error_mark_node)
  982.     return error_mark_node;
  983.  
  984.       if (value_member (TREE_TYPE (rval), types_memoized))
  985.     {
  986.       error ("circular pointer delegation detected");
  987.       return error_mark_node;
  988.     }
  989.       else
  990.     {
  991.       types_memoized = tree_cons (NULL_TREE, TREE_TYPE (rval),
  992.                       types_memoized);
  993.     }
  994.       last_rval = rval;
  995.     }
  996.  
  997.  more:
  998.   if (TREE_CODE (TREE_TYPE (last_rval)) == REFERENCE_TYPE)
  999.     last_rval = convert_from_reference (last_rval);
  1000.  
  1001.   if (TREE_CODE (TREE_TYPE (last_rval)) == POINTER_TYPE)
  1002.     return build_indirect_ref (last_rval, 0);
  1003.  
  1004.   if (TREE_CODE (TREE_TYPE (last_rval)) == OFFSET_TYPE)
  1005.     {
  1006.       if (TREE_CODE (last_rval) == OFFSET_REF
  1007.       && TREE_STATIC (TREE_OPERAND (last_rval, 1)))
  1008.     {
  1009.       last_rval = TREE_OPERAND (last_rval, 1);
  1010.       goto more;
  1011.     }
  1012.       compiler_error ("invalid member type in build_x_arrow");
  1013.       return error_mark_node;
  1014.     }
  1015.  
  1016.   if (types_memoized)
  1017.     error ("result of `operator->()' yields non-pointer result");
  1018.   else
  1019.     error ("base operand of `->' is not a pointer");
  1020.   return error_mark_node;
  1021. }
  1022.  
  1023. /* Make an expression to refer to the COMPONENT field of
  1024.    structure or union value DATUM.  COMPONENT is an arbitrary
  1025.    expression.  DATUM has already been checked out to be of
  1026.    aggregate type.
  1027.  
  1028.    For C++, COMPONENT may be a TREE_LIST.  This happens when we must
  1029.    return an object of member type to a method of the current class,
  1030.    but there is not yet enough typing information to know which one.
  1031.    As a special case, if there is only one method by that name,
  1032.    it is returned.  Otherwise we return an expression which other
  1033.    routines will have to know how to deal with later.  */
  1034. tree
  1035. build_m_component_ref (datum, component)
  1036.      tree datum, component;
  1037. {
  1038.   tree type = TREE_TYPE (component);
  1039.   tree objtype = TREE_TYPE (datum);
  1040.  
  1041.   if (datum == error_mark_node || component == error_mark_node)
  1042.     return error_mark_node;
  1043.  
  1044.   if (TREE_CODE (type) != OFFSET_TYPE && TREE_CODE (type) != METHOD_TYPE)
  1045.     {
  1046.       error ("non-member type composed with object");
  1047.       return error_mark_node;
  1048.     }
  1049.  
  1050.   if (TREE_CODE (objtype) == REFERENCE_TYPE)
  1051.     objtype = TREE_TYPE (objtype);
  1052.  
  1053.   if (! comptypes (TYPE_METHOD_BASETYPE (type), objtype, 0))
  1054.     {
  1055.       error ("member type `%s::' incompatible with object type `%s'",
  1056.          TYPE_NAME_STRING (TYPE_METHOD_BASETYPE (type)),
  1057.          TYPE_NAME_STRING (objtype));
  1058.       return error_mark_node;
  1059.     }
  1060.  
  1061.   return build (OFFSET_REF, TREE_TYPE (TREE_TYPE (component)), datum, component);
  1062. }
  1063.  
  1064. /* Return a tree node for the expression TYPENAME '(' PARMS ')'.  */
  1065. tree
  1066. build_functional_cast (exp, parms)
  1067.      tree exp;
  1068.      tree parms;
  1069. {
  1070.   /* This is either a call to a constructor,
  1071.      or a C cast in C++'s `functional' notation.  */
  1072.   tree type, name = NULL_TREE;
  1073.  
  1074.   if (parms == error_mark_node)
  1075.     return error_mark_node;
  1076.  
  1077.   if (TREE_CODE (exp) == IDENTIFIER_NODE)
  1078.     {
  1079.       name = exp;
  1080.  
  1081.       if (IDENTIFIER_HAS_TYPE_VALUE (exp))
  1082.     /* Either an enum or an aggregate type.  */
  1083.     type = IDENTIFIER_TYPE_VALUE (exp);
  1084.       else
  1085.     {
  1086.       type = lookup_name (exp);
  1087.       if (!type || TREE_CODE (type) != TYPE_DECL)
  1088.         {
  1089.           error ("`%s' fails to be a typedef or built-in type",
  1090.              IDENTIFIER_POINTER (name));
  1091.           return error_mark_node;
  1092.         }
  1093.       type = TREE_TYPE (type);
  1094.     }
  1095.     }
  1096.   else type = exp;
  1097.  
  1098.   if (! IS_AGGR_TYPE (type))
  1099.     {
  1100.       /* this must build a C cast */
  1101.       if (parms == NULL_TREE)
  1102.     {
  1103.       error ("cannot cast null list to type `%s'",
  1104.          IDENTIFIER_POINTER (name));
  1105.       return error_mark_node;
  1106.     }
  1107.       return build_c_cast (type, build_compound_expr (parms));
  1108.     }
  1109.  
  1110.   /* Call to a consructor.  If this expression
  1111.      is actually used, for example,
  1112.      
  1113.      return X (arg1, arg2, ...);
  1114.      
  1115.      then the slot being initialized will be filled in.  */
  1116.  
  1117.   if (name == NULL_TREE)
  1118.     {
  1119.       name = TYPE_NAME (type);
  1120.       if (TREE_CODE (name) == TYPE_DECL)
  1121.     name = DECL_NAME (name);
  1122.     }
  1123.  
  1124.   if (TYPE_SIZE (type) == NULL_TREE)
  1125.     {
  1126.       error ("type `%s' is not yet defined", IDENTIFIER_POINTER (name));
  1127.       return error_mark_node;
  1128.     }
  1129.  
  1130.   if (! TYPE_NEEDS_CONSTRUCTOR (type))
  1131.     {
  1132.       if (TREE_CHAIN (parms) == NULL_TREE)
  1133.     {
  1134.       tree rval = build_type_conversion (CONVERT_EXPR, type,
  1135.                          TREE_VALUE (parms), 1);
  1136.       if (rval)
  1137.         return rval;
  1138.     }
  1139.       error ("type `%s' does not have a constructor",
  1140.          IDENTIFIER_POINTER (name));
  1141.       return error_mark_node;
  1142.     }
  1143.  
  1144.   if (! TYPE_HAS_CONSTRUCTOR (type))
  1145.     {
  1146.       /* Look through this type until we find the
  1147.      base type which has a constructor.  */
  1148.       do
  1149.     {
  1150.       int i, index = 0;
  1151.  
  1152.       while (CLASSTYPE_N_BASECLASSES (type) == 1
  1153.          && ! TYPE_HAS_CONSTRUCTOR (type))
  1154.         type = CLASSTYPE_BASECLASS (type, 0);
  1155.       if (TYPE_HAS_CONSTRUCTOR (type))
  1156.         break;
  1157.       /* Hack for MI.  */
  1158.       i = CLASSTYPE_N_BASECLASSES (type);
  1159.       while (--i > 0)
  1160.         {
  1161.           if (TYPE_HAS_CONSTRUCTOR (CLASSTYPE_BASECLASS (type, i)))
  1162.         {
  1163.           if (index == 0)
  1164.             index = i;
  1165.           else
  1166.             {
  1167.               error ("multiple base classes with constructor, ambiguous");
  1168.               type = 0;
  1169.               break;
  1170.             }
  1171.         }
  1172.         }
  1173.       if (type == 0)
  1174.         break;
  1175.     } while (! TYPE_HAS_CONSTRUCTOR (type));
  1176.       if (type == 0)
  1177.     return error_mark_node;
  1178.     }
  1179.   name = TYPE_NAME (type);
  1180.   if (TREE_CODE (name) == TYPE_DECL)
  1181.     name = DECL_NAME (name);
  1182.  
  1183.   assert (TREE_CODE (name) == IDENTIFIER_NODE);
  1184.  
  1185.   {
  1186.     int flags = (parms && TREE_CHAIN (parms) == NULL_TREE
  1187.          ? LOOKUP_PROTECT|LOOKUP_NO_CONVERSION : LOOKUP_COMPLAIN);
  1188.     tree rval;
  1189.  
  1190.   try_again:
  1191.     rval = build_method_call (NULL_TREE, name, parms, NULL_TREE, flags);
  1192.  
  1193.     if (rval != error_mark_node)
  1194.       {
  1195.     rval = build_cplus_new (type, rval, 0);
  1196.     return rval;
  1197.       }
  1198.  
  1199.     /* If it didn't work going through constructor, try type conversion.  */
  1200.     if (! (flags & LOOKUP_COMPLAIN))
  1201.       {
  1202.     rval = build_type_conversion (CONVERT_EXPR, type, TREE_VALUE (parms),
  1203.                       !! (flags & LOOKUP_NO_CONVERSION));
  1204.     if (rval)
  1205.       return rval;
  1206.     if (flags & LOOKUP_NO_CONVERSION)
  1207.       {
  1208.         flags = LOOKUP_NORMAL;
  1209.         goto try_again;
  1210.       }
  1211.       }
  1212.  
  1213.     return error_mark_node;
  1214.   }
  1215. }
  1216.  
  1217. /* Return the character string for the name that encodes the
  1218.    enumeral value VALUE in the domain TYPE.  */
  1219. char *
  1220. enum_name_string (value, type)
  1221.      tree value;
  1222.      tree type;
  1223. {
  1224.   register tree values = TYPE_VALUES (type);
  1225.   register int intval = TREE_INT_CST_LOW (value);
  1226.  
  1227.   assert (TREE_CODE (type) == ENUMERAL_TYPE);
  1228.   while (values
  1229.      && TREE_INT_CST_LOW (TREE_VALUE (values)) != intval)
  1230.     values = TREE_CHAIN (values);
  1231.   if (values == NULL_TREE)
  1232.     {
  1233.       char *buf = (char *)oballoc (16 + TYPE_NAME_LENGTH (type));
  1234.  
  1235.       /* Value must have been cast.  */
  1236.       sprintf (buf, "(enum %s)%d",
  1237.            TYPE_NAME_STRING (type), intval);
  1238.       return buf;
  1239.     }
  1240.   return IDENTIFIER_POINTER (TREE_PURPOSE (values));
  1241. }
  1242.  
  1243. /* Print out a language-specific error message for
  1244.    (Pascal) case or (C) switch statements.
  1245.    CODE tells what sort of message to print. 
  1246.    TYPE is the type of the switch index expression.
  1247.    NEW is the new value that we were trying to add.
  1248.    OLD is the old value that stopped us from adding it.  */
  1249. void
  1250. report_case_error (code, type, new_value, old_value)
  1251.      int code;
  1252.      tree type;
  1253.      tree new_value, old_value;
  1254. {
  1255.   if (code == 1)
  1256.     {
  1257.       if (new_value)
  1258.     error ("case label not within a switch statement");
  1259.       else
  1260.     error ("default label not within a switch statement");
  1261.     }
  1262.   else if (code == 2)
  1263.     {
  1264.       if (new_value == 0)
  1265.     {
  1266.       error ("multiple default labels in one switch");
  1267.       return;
  1268.     }
  1269.       if (TREE_CODE (new_value) == RANGE_EXPR)
  1270.     if (TREE_CODE (old_value) == RANGE_EXPR)
  1271.       {
  1272.         char *buf = (char *)alloca (4 * (8 + TYPE_NAME_LENGTH (type)));
  1273.         if (TREE_CODE (type) == ENUMERAL_TYPE)
  1274.           sprintf (buf, "overlapping ranges [%s..%s], [%s..%s] in case expression",
  1275.                enum_name_string (TREE_OPERAND (new_value, 0), type),
  1276.                enum_name_string (TREE_OPERAND (new_value, 1), type),
  1277.                enum_name_string (TREE_OPERAND (old_value, 0), type),
  1278.                enum_name_string (TREE_OPERAND (old_value, 1), type));
  1279.         else
  1280.           sprintf (buf, "overlapping ranges [%d..%d], [%d..%d] in case expression",
  1281.                TREE_INT_CST_LOW (TREE_OPERAND (new_value, 0)),
  1282.                TREE_INT_CST_LOW (TREE_OPERAND (new_value, 1)),
  1283.                TREE_INT_CST_LOW (TREE_OPERAND (old_value, 0)),
  1284.                TREE_INT_CST_LOW (TREE_OPERAND (old_value, 1)));
  1285.         error (buf);
  1286.       }
  1287.     else
  1288.       {
  1289.         char *buf = (char *)alloca (4 * (8 + TYPE_NAME_LENGTH (type)));
  1290.         if (TREE_CODE (type) == ENUMERAL_TYPE)
  1291.           sprintf (buf, "range [%s..%s] includes element `%s' in case expression",
  1292.                enum_name_string (TREE_OPERAND (new_value, 0), type),
  1293.                enum_name_string (TREE_OPERAND (new_value, 1), type),
  1294.                enum_name_string (old_value, type));
  1295.         else
  1296.           sprintf (buf, "range [%d..%d] includes (%d) in case expression",
  1297.                TREE_INT_CST_LOW (TREE_OPERAND (new_value, 0)),
  1298.                TREE_INT_CST_LOW (TREE_OPERAND (new_value, 1)),
  1299.                TREE_INT_CST_LOW (old_value));
  1300.         error (buf);
  1301.       }
  1302.       else if (TREE_CODE (old_value) == RANGE_EXPR)
  1303.     {
  1304.       char *buf = (char *)alloca (4 * (8 + TYPE_NAME_LENGTH (type)));
  1305.       if (TREE_CODE (type) == ENUMERAL_TYPE)
  1306.         sprintf (buf, "range [%s..%s] includes element `%s' in case expression",
  1307.              enum_name_string (TREE_OPERAND (old_value, 0), type),
  1308.              enum_name_string (TREE_OPERAND (old_value, 1), type),
  1309.              enum_name_string (new_value, type));
  1310.       else
  1311.         sprintf (buf, "range [%d..%d] includes (%d) in case expression",
  1312.              TREE_INT_CST_LOW (TREE_OPERAND (old_value, 0)),
  1313.              TREE_INT_CST_LOW (TREE_OPERAND (old_value, 1)),
  1314.              TREE_INT_CST_LOW (new_value));
  1315.       error (buf);
  1316.     }
  1317.       else
  1318.     {
  1319.       if (TREE_CODE (type) == ENUMERAL_TYPE)
  1320.         error ("duplicate label `%s' in switch statement",
  1321.            enum_name_string (new_value, type));
  1322.       else
  1323.         error ("duplicate label (%d) in switch statement",
  1324.            TREE_INT_CST_LOW (new_value));
  1325.     }
  1326.     }
  1327.   else if (code == 3)
  1328.     {
  1329.       if (TREE_CODE (type) == ENUMERAL_TYPE)
  1330.     warning ("case value out of range for enum %s",
  1331.          TYPE_NAME_STRING (type));
  1332.       else
  1333.     warning ("case value out of range");
  1334.     }
  1335.   else if (code == 4)
  1336.     {
  1337.       if (TREE_CODE (type) == ENUMERAL_TYPE)
  1338.     error ("range values `%s' and `%s' reversed",
  1339.            enum_name_string (new_value, type),
  1340.            enum_name_string (old_value, type));
  1341.       else
  1342.     error ("range values reversed");
  1343.     }
  1344. }
  1345.