home *** CD-ROM | disk | FTP | other *** search
/ InfoMagic Source Code 1993 July / THE_SOURCE_CODE_CD_ROM.iso / gnu / gcc-2.4.5 / cp-init.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-04-26  |  123.3 KB  |  4,009 lines

  1. /* Handle initialization things in C++.
  2.    Copyright (C) 1987, 1989, 1992, 1993 Free Software Foundation, Inc.
  3.    Contributed 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. /* High-level class interface. */
  23.  
  24. #include "config.h"
  25. #include "tree.h"
  26. #include "rtl.h"
  27. #include "cp-tree.h"
  28. #include "flags.h"
  29.  
  30. #undef NULL
  31. #define NULL 0
  32.  
  33. /* In C++, structures with well-defined constructors are initialized by
  34.    those constructors, unasked.  CURRENT_BASE_INIT_LIST
  35.    holds a list of stmts for a BASE_INIT term in the grammar.
  36.    This list has one element for each base class which must be
  37.    initialized.  The list elements are [basename, init], with
  38.    type basetype.  This allows the possibly anachronistic form
  39.    (assuming d : a, b, c) "d (int a) : c(a+5), b (a-4), a (a+3)"
  40.    where each successive term can be handed down the constructor
  41.    line.  Perhaps this was not intended.  */
  42. tree current_base_init_list, current_member_init_list;
  43.  
  44. void emit_base_init ();
  45. void check_base_init ();
  46. static void expand_aggr_vbase_init ();
  47. void expand_member_init ();
  48. void expand_aggr_init ();
  49.  
  50. static void expand_aggr_init_1 ();
  51. static void expand_recursive_init_1 ();
  52. static void expand_recursive_init ();
  53. tree expand_vec_init ();
  54. tree build_vec_delete ();
  55.  
  56. static void add_friend (), add_friends ();
  57.  
  58. /* Cache _builtin_new and _builtin_delete exprs.  */
  59. static tree BIN, BID;
  60.  
  61. static tree minus_one;
  62.  
  63. /* Set up local variable for this file.  MUST BE CALLED AFTER
  64.    INIT_DECL_PROCESSING.  */
  65.  
  66. tree BI_header_type, BI_header_size;
  67.  
  68. void init_init_processing ()
  69. {
  70.   tree op_id;
  71.   tree fields[2];
  72.  
  73.   /* Define implicit `operator new' and `operator delete' functions.  */
  74.   BIN = default_conversion (TREE_VALUE (IDENTIFIER_GLOBAL_VALUE (ansi_opname[(int) NEW_EXPR])));
  75.   TREE_USED (TREE_OPERAND (BIN, 0)) = 0;
  76.   BID = default_conversion (TREE_VALUE (IDENTIFIER_GLOBAL_VALUE (ansi_opname[(int) DELETE_EXPR])));
  77.   TREE_USED (TREE_OPERAND (BID, 0)) = 0;
  78.   minus_one = build_int_2 (-1, -1);
  79.  
  80.   op_id = ansi_opname[(int) NEW_EXPR];
  81.   IDENTIFIER_GLOBAL_VALUE (op_id) = BIN;
  82.   op_id = ansi_opname[(int) DELETE_EXPR];
  83.   IDENTIFIER_GLOBAL_VALUE (op_id) = BID;
  84.  
  85.   /* Define the structure that holds header information for
  86.      arrays allocated via operator new.  */
  87.   BI_header_type = make_lang_type (RECORD_TYPE);
  88.   fields[0] = build_lang_field_decl (FIELD_DECL, get_identifier ("nelts"),
  89.                      sizetype);
  90.   fields[1] = build_lang_field_decl (FIELD_DECL, get_identifier ("ptr_2comp"),
  91.                      ptr_type_node);
  92.   finish_builtin_type (BI_header_type, "__new_cookie", fields, 1, double_type_node);
  93.   BI_header_size = size_in_bytes (BI_header_type);
  94. }
  95.  
  96. /* Recursive subroutine of emit_base_init.  For main type T,
  97.    recursively initialize the vfields of the base type PARENT.
  98.    RECURSE is non-zero when this function is being called
  99.    recursively.  */
  100.  
  101. static void
  102. init_vfields (t, parent, recurse)
  103.      tree t, parent;
  104.      int recurse;
  105. {
  106.   tree vfields;
  107.  
  108.   /* Initialize all the virtual function table fields that
  109.      do not come from virtual base classes.  */
  110.   vfields = CLASSTYPE_VFIELDS (parent);
  111.   while (vfields)
  112.     {
  113.       tree basetype = VF_DERIVED_VALUE (vfields)
  114.     ? TYPE_MAIN_VARIANT (VF_DERIVED_VALUE (vfields))
  115.       : VF_BASETYPE_VALUE (vfields);
  116.  
  117.       /* If the vtable installed by the constructor was not
  118.      the right one, fix that here.  */
  119.       if (TREE_ADDRESSABLE (vfields)
  120.       && CLASSTYPE_NEEDS_VIRTUAL_REINIT (basetype)
  121.       && (recurse > 0
  122.           || TYPE_HAS_CONSTRUCTOR (basetype)
  123.           /* BASE_INIT_LIST has already initialized the immediate basetypes.  */
  124.           || get_base_distance (basetype, t, 0, (tree *) 0) > 1))
  125.     {
  126.       tree binfo = binfo_value (basetype, t);
  127.       if ((recurse != 0 && (binfo != binfo_value (basetype, parent)))
  128.           || (recurse == 0
  129.           && BINFO_VTABLE (binfo) != TYPE_BINFO_VTABLE (basetype)))
  130.         {
  131.           tree ptr = convert_pointer_to (binfo, current_class_decl);
  132.           expand_expr_stmt (build_virtual_init (TYPE_BINFO (t), binfo, ptr));
  133.         }
  134.       init_vfields (t, basetype, recurse+1);
  135.     }
  136.       vfields = TREE_CHAIN (vfields);
  137.     }
  138. }
  139.  
  140. /* Perform whatever initialization have yet to be done on the
  141.    base class of the class variable.  These actions are in
  142.    the global variable CURRENT_BASE_INIT_LIST.  Such an
  143.    action could be NULL_TREE, meaning that the user has explicitly
  144.    called the base class constructor with no arguments.
  145.  
  146.    If there is a need for a call to a constructor, we
  147.    must surround that call with a pushlevel/poplevel pair,
  148.    since we are technically at the PARM level of scope.
  149.  
  150.    Argument IMMEDIATELY, if zero, forces a new sequence to be generated
  151.    to contain these new insns, so it can be emitted later.  This sequence
  152.    is saved in the global variable BASE_INIT_INSNS.  Otherwise, the insns
  153.    are emitted into the current sequence.
  154.  
  155.    Note that emit_base_init does *not* initialize virtual
  156.    base classes.  That is done specially, elsewhere.  */
  157.    
  158. void
  159. emit_base_init (t, immediately)
  160.      tree t;
  161.      int immediately;
  162. {
  163.   extern tree in_charge_identifier;
  164.  
  165.   tree member, decl, vbases;
  166.   tree init_list;
  167.   int pass, start;
  168.   tree t_binfo = TYPE_BINFO (t);
  169.   tree binfos = BINFO_BASETYPES (t_binfo);
  170.   int i, n_baseclasses = binfos ? TREE_VEC_LENGTH (binfos) : 0;
  171.   tree fields_to_unmark = NULL_TREE;
  172.  
  173.   if (! immediately)
  174.     {
  175.       do_pending_stack_adjust ();
  176.       start_sequence ();
  177.     }
  178.  
  179.   if (write_symbols == NO_DEBUG)
  180.     /* As a matter of principle, `start_sequence' should do this.  */
  181.     emit_note (0, -1);
  182.   else
  183.     /* Always emit a line number note so we can step into constructors.  */
  184.     emit_line_note_force (DECL_SOURCE_FILE (current_function_decl),
  185.               DECL_SOURCE_LINE (current_function_decl));
  186.  
  187.   /* In this case, we always need IN_CHARGE_NODE, because we have
  188.      to know whether to deallocate or not before exiting.  */
  189.   if (flag_handle_exceptions == 2
  190.       && lookup_name (in_charge_identifier, 0) == NULL_TREE)
  191.     {
  192.       tree in_charge_node = pushdecl (build_decl (VAR_DECL, in_charge_identifier,
  193.                           integer_type_node));
  194.       store_init_value (in_charge_node, build (EQ_EXPR, integer_type_node,
  195.                            current_class_decl,
  196.                            integer_zero_node));
  197.       expand_decl (in_charge_node);
  198.       expand_decl_init (in_charge_node);
  199.     }
  200.  
  201.   start = ! TYPE_USES_VIRTUAL_BASECLASSES (t);
  202.   for (pass = start; pass < 2; pass++)
  203.     {
  204.       tree vbase_init_list = NULL_TREE;
  205.  
  206.       for (init_list = current_base_init_list; init_list;
  207.        init_list = TREE_CHAIN (init_list))
  208.     {
  209.       tree basename = TREE_PURPOSE (init_list);
  210.       tree binfo;
  211.       tree init = TREE_VALUE (init_list);
  212.  
  213.       if (basename == NULL_TREE)
  214.         {
  215.           /* Initializer for single base class.  Must not
  216.          use multiple inheritance or this is ambiguous.  */
  217.           switch (n_baseclasses)
  218.         {
  219.         case 0:
  220.           error ("type `%s' does not have a base class to initialize",
  221.              IDENTIFIER_POINTER (current_class_name));
  222.           return;
  223.         case 1:
  224.           break;
  225.         default:
  226.           error ("unnamed initializer ambiguous for type `%s' which uses multiple inheritance", IDENTIFIER_POINTER (current_class_name));
  227.           return;
  228.         }
  229.           binfo = TREE_VEC_ELT (binfos, 0);
  230.         }
  231.       else if (is_aggr_typedef (basename, 1))
  232.         {
  233.           binfo = binfo_or_else (IDENTIFIER_TYPE_VALUE (basename), t);
  234.           if (binfo == NULL_TREE)
  235.         continue;
  236.  
  237.           /* Virtual base classes are special cases.  Their initializers
  238.          are recorded with this constructor, and they are used when
  239.          this constructor is the top-level constructor called.  */
  240.           if (! TREE_VIA_VIRTUAL (binfo))
  241.         {
  242.           /* Otherwise, if it is not an immediate base class, complain.  */
  243.           for (i = n_baseclasses-1; i >= 0; i--)
  244.             if (BINFO_TYPE (binfo) == BINFO_TYPE (TREE_VEC_ELT (binfos, i)))
  245.               break;
  246.           if (i < 0)
  247.             {
  248.               error ("type `%s' is not an immediate base class of type `%s'",
  249.                  IDENTIFIER_POINTER (basename),
  250.                  IDENTIFIER_POINTER (current_class_name));
  251.               continue;
  252.             }
  253.         }
  254.         }
  255.       else
  256.         continue;
  257.  
  258.       /* The base initialization list goes up to the first
  259.          base class which can actually use it.  */
  260.  
  261.       if (pass == start)
  262.         {
  263.           char *msgp = (! TYPE_HAS_CONSTRUCTOR (BINFO_TYPE (binfo)))
  264.         ? "cannot pass initialization up to class `%s'" : 0;
  265.  
  266.           while (! TYPE_HAS_CONSTRUCTOR (BINFO_TYPE (binfo))
  267.              && BINFO_BASETYPES (binfo) != NULL_TREE
  268.              && TREE_VEC_LENGTH (BINFO_BASETYPES (binfo)) == 1)
  269.         {
  270.           /* ?? This should be fixed in RENO by forcing
  271.              default constructors to exist.  */
  272.           SET_BINFO_BASEINIT_MARKED (binfo);
  273.           binfo = BINFO_BASETYPE (binfo, 0);
  274.         }
  275.  
  276.           if (TYPE_HAS_CONSTRUCTOR (BINFO_TYPE (binfo)))
  277.         {
  278.           if (msgp)
  279.             {
  280.               if (pedantic)
  281.             error_with_aggr_type (binfo, msgp);
  282.               else
  283.             msgp = 0;
  284.             }
  285.         }
  286.           else
  287.         {
  288.           msgp = "no constructor found for initialization of `%s'";
  289.           error (msgp, IDENTIFIER_POINTER (basename));
  290.         }
  291.  
  292.           if (BINFO_BASEINIT_MARKED (binfo))
  293.         {
  294.           msgp = "class `%s' initializer already specified";
  295.           error (msgp, IDENTIFIER_POINTER (basename));
  296.         }
  297.           if (msgp)
  298.         continue;
  299.  
  300.           SET_BINFO_BASEINIT_MARKED (binfo);
  301.           if (TREE_VIA_VIRTUAL (binfo))
  302.         {
  303.           vbase_init_list = tree_cons (init, BINFO_TYPE (binfo),
  304.                            vbase_init_list);
  305.           continue;
  306.         }
  307.           if (pass == 0)
  308.         continue;
  309.         }
  310.       else if (TREE_VIA_VIRTUAL (binfo))
  311.         continue;
  312.  
  313.       member = convert_pointer_to (binfo, current_class_decl);
  314.       expand_aggr_init_1 (t_binfo, 0,
  315.                   build_indirect_ref (member, 0), init,
  316.                   BINFO_OFFSET_ZEROP (binfo),
  317.                   LOOKUP_PROTECTED_OK|LOOKUP_COMPLAIN);
  318.       if (flag_handle_exceptions == 2 && TYPE_NEEDS_DESTRUCTOR (BINFO_TYPE (binfo)))
  319.         {
  320.           cplus_expand_start_try (1);
  321.           push_exception_cleanup (member);
  322.         }
  323.     }
  324.  
  325.       if (pass == 0)
  326.     {
  327.       tree first_arg = TREE_CHAIN (DECL_ARGUMENTS (current_function_decl));
  328.       tree vbases;
  329.  
  330.       if (DECL_NAME (current_function_decl) == NULL_TREE
  331.           && TREE_CHAIN (first_arg) != NULL_TREE)
  332.         {
  333.           /* If there are virtual baseclasses without initialization
  334.          specified, and this is a default X(X&) constructor,
  335.          build the initialization list so that each virtual baseclass
  336.          of the new object is initialized from the virtual baseclass
  337.          of the incoming arg.  */
  338.           tree init_arg = build_unary_op (ADDR_EXPR, TREE_CHAIN (first_arg), 0);
  339.           for (vbases = CLASSTYPE_VBASECLASSES (t);
  340.            vbases; vbases = TREE_CHAIN (vbases))
  341.         {
  342.           if (BINFO_BASEINIT_MARKED (vbases) == 0)
  343.             {
  344.               member = convert_pointer_to (vbases, init_arg);
  345.               if (member == init_arg)
  346.             member = TREE_CHAIN (first_arg);
  347.               else
  348.             TREE_TYPE (member) = build_reference_type (BINFO_TYPE (vbases));
  349.               vbase_init_list = tree_cons (convert_from_reference (member),
  350.                            vbases, vbase_init_list);
  351.               SET_BINFO_BASEINIT_MARKED (vbases);
  352.             }
  353.         }
  354.         }
  355.       expand_start_cond (first_arg, 0);
  356.       expand_aggr_vbase_init (t_binfo, C_C_D, current_class_decl,
  357.                   vbase_init_list);
  358.       expand_expr_stmt (build_vbase_vtables_init (t_binfo, t_binfo,
  359.                               C_C_D, current_class_decl, 1));
  360.       expand_end_cond ();
  361.     }
  362.     }
  363.   current_base_init_list = NULL_TREE;
  364.  
  365.   /* Now, perform default initialization of all base classes which
  366.      have not yet been initialized, and unmark baseclasses which
  367.      have been initialized.  */
  368.   for (i = 0; i < n_baseclasses; i++)
  369.     {
  370.       tree base = current_class_decl;
  371.       tree base_binfo = TREE_VEC_ELT (binfos, i);
  372.  
  373.       if (TYPE_NEEDS_CONSTRUCTING (BINFO_TYPE (base_binfo)))
  374.     {
  375.       if (! TREE_VIA_VIRTUAL (base_binfo)
  376.           && ! BINFO_BASEINIT_MARKED (base_binfo))
  377.         {
  378.           tree ref;
  379.  
  380.           if (BINFO_OFFSET_ZEROP (base_binfo))
  381.         base = build1 (NOP_EXPR,
  382.                    TYPE_POINTER_TO (BINFO_TYPE (base_binfo)),
  383.                    current_class_decl);
  384.           else
  385.         base = build (PLUS_EXPR,
  386.                   TYPE_POINTER_TO (BINFO_TYPE (base_binfo)),
  387.                   current_class_decl, BINFO_OFFSET (base_binfo));
  388.  
  389.           ref = build_indirect_ref (base, 0);
  390.           expand_aggr_init_1 (t_binfo, 0, ref, NULL_TREE,
  391.                   BINFO_OFFSET_ZEROP (base_binfo),
  392.                   LOOKUP_PROTECTED_OK|LOOKUP_COMPLAIN);
  393.           if (flag_handle_exceptions == 2
  394.           && TYPE_NEEDS_DESTRUCTOR (BINFO_TYPE (base_binfo)))
  395.         {
  396.           cplus_expand_start_try (1);
  397.           push_exception_cleanup (base);
  398.         }
  399.         }
  400.     }
  401.       CLEAR_BINFO_BASEINIT_MARKED (base_binfo);
  402.     }
  403.   for (vbases = CLASSTYPE_VBASECLASSES (t); vbases; vbases = TREE_CHAIN (vbases))
  404.     CLEAR_BINFO_BASEINIT_MARKED (vbases);
  405.  
  406.   /* Initialize all the virtual function table fields that
  407.      do not come from virtual base classes.  */
  408.   init_vfields (t, t, 0);
  409.  
  410.   if (CLASSTYPE_NEEDS_VIRTUAL_REINIT (t))
  411.     expand_expr_stmt (build_virtual_init (TYPE_BINFO (t), t,
  412.                       current_class_decl));
  413.  
  414.   /* Members we through expand_member_init.  We initialize all the members
  415.      needing initialization that did not get it so far.  */
  416.   for (; current_member_init_list;
  417.        current_member_init_list = TREE_CHAIN (current_member_init_list))
  418.     {
  419.       tree name = TREE_PURPOSE (current_member_init_list);
  420.       tree init = TREE_VALUE (current_member_init_list);
  421.       tree field = (TREE_CODE (name) == COMPONENT_REF
  422.             ? TREE_OPERAND (name, 1) : IDENTIFIER_CLASS_VALUE (name));
  423.       tree type;
  424.       
  425.       /* If one member shadows another, get the outermost one.  */
  426.       if (TREE_CODE (field) == TREE_LIST)
  427.     {
  428.       field = TREE_VALUE (field);
  429.       if (decl_type_context (field) != current_class_type)
  430.         error ("field `%s' not in immediate context");
  431.     }
  432.  
  433.       type = TREE_TYPE (field);
  434.  
  435.       if (TREE_STATIC (field))
  436.     {
  437.       error_with_aggr_type (DECL_FIELD_CONTEXT (field),
  438.                 "field `%s::%s' is static; only point of initialization is its declaration", IDENTIFIER_POINTER (name));
  439.       continue;
  440.     }
  441.  
  442.       if (DECL_NAME (field))
  443.     {
  444.       if (TREE_HAS_CONSTRUCTOR (field))
  445.         error ("multiple initializations given for member `%s'",
  446.            IDENTIFIER_POINTER (DECL_NAME (field)));
  447.     }
  448.  
  449.       /* Mark this node as having been initialized.  */
  450.       TREE_HAS_CONSTRUCTOR (field) = 1;
  451.       if (DECL_FIELD_CONTEXT (field) != t)
  452.     fields_to_unmark = tree_cons (NULL_TREE, field, fields_to_unmark);
  453.  
  454.       if (TREE_CODE (name) == COMPONENT_REF)
  455.     {
  456.       /* Initialization of anonymous union.  */
  457.       expand_assignment (name, init, 0, 0);
  458.       continue;
  459.     }
  460.       decl = build_component_ref (C_C_D, name, 0, 1);
  461.  
  462.       if (TYPE_NEEDS_CONSTRUCTING (type))
  463.     {
  464.       if (TREE_CODE (type) == ARRAY_TYPE
  465.           && init != NULL_TREE
  466.           && TREE_CHAIN (init) == NULL_TREE
  467.           && TREE_CODE (TREE_TYPE (TREE_VALUE (init))) == ARRAY_TYPE)
  468.         {
  469.           /* Initialization of one array from another.  */
  470.           expand_vec_init (TREE_OPERAND (decl, 1), decl,
  471.                    array_type_nelts (type), TREE_VALUE (init), 1);
  472.         }
  473.       else
  474.         expand_aggr_init (decl, init, 0);
  475.     }
  476.       else
  477.     {
  478.       if (init == NULL_TREE)
  479.         {
  480.           error ("types without constructors must have complete initializers");
  481.           init = error_mark_node;
  482.         }
  483.       else if (TREE_CHAIN (init))
  484.         {
  485.           warning ("initializer list treated as compound expression");
  486.           init = build_compound_expr (init);
  487.         }
  488.       else
  489.         init = TREE_VALUE (init);
  490.  
  491.       expand_expr_stmt (build_modify_expr (decl, INIT_EXPR, init));
  492.     }
  493.       if (flag_handle_exceptions == 2 && TYPE_NEEDS_DESTRUCTOR (type))
  494.     {
  495.       cplus_expand_start_try (1);
  496.       push_exception_cleanup (build_unary_op (ADDR_EXPR, decl, 0));
  497.     }
  498.     }
  499.  
  500.   for (member = TYPE_FIELDS (t); member; member = TREE_CHAIN (member))
  501.     {
  502.       /* All we care about is this unique member.  It contains
  503.      all the information we need to know, and that right early.  */
  504.       tree type = TREE_TYPE (member);
  505.       tree init = TREE_HAS_CONSTRUCTOR (member)
  506.     ? error_mark_node : DECL_INITIAL (member);
  507.  
  508.       /* Unmark this field.  If it is from an anonymous union,
  509.          then unmark the field recursively.  */
  510.       TREE_HAS_CONSTRUCTOR (member) = 0;
  511.       if (TREE_ANON_UNION_ELEM (member))
  512.     emit_base_init (TREE_TYPE (member), 1);
  513.  
  514.       /* Member had explicit initializer.  */
  515.       if (init == error_mark_node)
  516.     continue;
  517.  
  518.       if (TREE_CODE (member) != FIELD_DECL)
  519.     continue;
  520.  
  521.       if (type == error_mark_node)
  522.     continue;
  523.  
  524.       if (TYPE_NEEDS_CONSTRUCTING (type))
  525.     {
  526.       if (init)
  527.         init = build_tree_list (NULL_TREE, init);
  528.       decl = build_component_ref (C_C_D, DECL_NAME (member), 0, 0);
  529.       expand_aggr_init (decl, init, 0);
  530.     }
  531.       else
  532.     {
  533.       if (init)
  534.         {
  535.           decl = build_component_ref (C_C_D, DECL_NAME (member), 0, 0);
  536.           expand_expr_stmt (build_modify_expr (decl, INIT_EXPR, init));
  537.         }
  538.       else if (TREE_CODE (TREE_TYPE (member)) == REFERENCE_TYPE)
  539.         warning ("uninitialized reference member `%s'",
  540.              IDENTIFIER_POINTER (DECL_NAME (member)));
  541.     }
  542.       if (flag_handle_exceptions == 2 && TYPE_NEEDS_DESTRUCTOR (type))
  543.     {
  544.       cplus_expand_start_try (1);
  545.       push_exception_cleanup (build_unary_op (ADDR_EXPR, decl, 0));
  546.     }
  547.     }
  548.   /* Unmark fields which are initialized for the base class.  */
  549.   while (fields_to_unmark)
  550.     {
  551.       TREE_HAS_CONSTRUCTOR (TREE_VALUE (fields_to_unmark)) = 0;
  552.       fields_to_unmark = TREE_CHAIN (fields_to_unmark);
  553.     }
  554.  
  555.   /* It is possible for the initializers to need cleanups.
  556.      Expand those cleanups now that all the initialization
  557.      has been done.  */
  558.   expand_cleanups_to (NULL_TREE);
  559.  
  560.   if (! immediately)
  561.     {
  562.       extern rtx base_init_insns;
  563.  
  564.       do_pending_stack_adjust ();
  565.       my_friendly_assert (base_init_insns == 0, 207);
  566.       base_init_insns = get_insns ();
  567.       end_sequence ();
  568.     }
  569.  
  570.   /* All the implicit try blocks we built up will be zapped
  571.      when we come to a real binding contour boundary.  */
  572. }
  573.  
  574. /* Check that all fields are properly initialized after
  575.    an assignment to `this'.  */
  576. void
  577. check_base_init (t)
  578.      tree t;
  579. {
  580.   tree member;
  581.   for (member = TYPE_FIELDS (t); member; member = TREE_CHAIN (member))
  582.     if (DECL_NAME (member) && TREE_USED (member))
  583.       error ("field `%s' used before initialized (after assignment to `this')",
  584.          IDENTIFIER_POINTER (DECL_NAME (member)));
  585. }
  586.  
  587. /* This code sets up the virtual function tables appropriate for
  588.    the pointer DECL.  It is a one-ply initialization.
  589.  
  590.    BINFO is the exact type that DECL is supposed to be.  In
  591.    multiple inheritance, this might mean "C's A" if C : A, B.  */
  592. tree
  593. build_virtual_init (main_binfo, binfo, decl)
  594.      tree main_binfo, binfo;
  595.      tree decl;
  596. {
  597.   tree type;
  598.   tree vtbl, vtbl_ptr;
  599.   tree vtype;
  600.  
  601.   if (TREE_CODE (binfo) == TREE_VEC)
  602.     type = BINFO_TYPE (binfo);
  603.   else if (TREE_CODE (binfo) == RECORD_TYPE)
  604.     {
  605.       type = binfo;
  606.       binfo = TYPE_BINFO (type);
  607.     }
  608.   else
  609.     my_friendly_abort (46);
  610.  
  611.   vtype = DECL_CONTEXT (CLASSTYPE_VFIELD (type));
  612. #if 0
  613.   /* This code suggests that it's time to rewrite how we handle
  614.      replicated baseclasses in G++.  */
  615.   if (get_base_distance (vtype, TREE_TYPE (TREE_TYPE (decl)),
  616.              0, (tree *) 0) == -2)
  617.     {
  618.       tree binfos = TYPE_BINFO_BASETYPES (TREE_TYPE (TREE_TYPE (decl)));
  619.       int i, n_baselinks = binfos ? TREE_VEC_LENGTH (binfos) : 0;
  620.       tree result = NULL_TREE;
  621.  
  622.       for (i = n_baselinks-1; i >= 0; i--)
  623.     {
  624.       tree base_binfo = TREE_VEC_ELT (binfos, i);
  625.       tree this_decl;
  626.  
  627.       if (get_base_distance (vtype, BINFO_TYPE (base_binfo), 0, 0) == -1)
  628.         continue;
  629.  
  630.       if (TREE_VIA_VIRTUAL (base_binfo))
  631.         this_decl = build_vbase_pointer (build_indirect_ref (decl), BINFO_TYPE (base_binfo));
  632.       else if (BINFO_OFFSET_ZEROP (base_binfo))
  633.         this_decl = build1 (NOP_EXPR, TYPE_POINTER_TO (BINFO_TYPE (base_binfo)),
  634.                 decl);
  635.       else
  636.         this_decl = build (PLUS_EXPR, TYPE_POINTER_TO (BINFO_TYPE (base_binfo)),
  637.                    decl, BINFO_OFFSET (base_binfo));
  638.       result = tree_cons (NULL_TREE, build_virtual_init (main_binfo, base_binfo, this_decl), result);
  639.     }
  640.       return build_compound_expr (result);
  641.     }
  642. #endif
  643.  
  644.     {
  645. #if 1
  646. #if 1
  647.       vtbl = BINFO_VTABLE (binfo_value (DECL_FIELD_CONTEXT (CLASSTYPE_VFIELD (type)), binfo));
  648. #else
  649.       /* The below does not work when we have to step through the
  650.      vfield, on our way down to the most base class for the
  651.      vfield. */
  652.       vtbl = BINFO_VTABLE (binfo_value (DECL_FIELD_CONTEXT (CLASSTYPE_VFIELD (type)),
  653.                     BINFO_TYPE (main_binfo)));
  654. #endif
  655. #else
  656.       my_friendly_assert (BINFO_TYPE (main_binfo) == BINFO_TYPE (binfo), 208);
  657.       vtbl = BINFO_VTABLE (main_binfo);
  658. #endif /* 1 */
  659.       assemble_external (vtbl);
  660.       TREE_USED (vtbl) = 1;
  661.       vtbl = build1 (ADDR_EXPR, TYPE_POINTER_TO (TREE_TYPE (vtbl)), vtbl);
  662.     }
  663.   decl = convert_pointer_to (vtype, decl);
  664.   vtbl_ptr = build_vfield_ref (build_indirect_ref (decl, 0), vtype);
  665.   if (vtbl_ptr == error_mark_node)
  666.     return error_mark_node;
  667.  
  668.   /* Have to convert VTBL since array sizes may be different.  */
  669.   return build_modify_expr (vtbl_ptr, NOP_EXPR,
  670.                 convert (TREE_TYPE (vtbl_ptr), vtbl));
  671. }
  672.  
  673. /* Subroutine of `expand_aggr_vbase_init'.
  674.    BINFO is the binfo of the type that is being initialized.
  675.    INIT_LIST is the list of initializers for the virtual baseclass.  */
  676. static void
  677. expand_aggr_vbase_init_1 (binfo, exp, addr, init_list)
  678.      tree binfo, exp, addr, init_list;
  679. {
  680.   tree init = value_member (BINFO_TYPE (binfo), init_list);
  681.   tree ref = build_indirect_ref (addr, 0);
  682.   if (init)
  683.     init = TREE_PURPOSE (init);
  684.   /* Call constructors, but don't set up vtables.  */
  685.   expand_aggr_init_1 (binfo, exp, ref, init, 0,
  686.               LOOKUP_PROTECTED_OK|LOOKUP_COMPLAIN|LOOKUP_SPECULATIVELY);
  687.   CLEAR_BINFO_VBASE_INIT_MARKED (binfo);
  688. }
  689.  
  690. /* Initialize this object's virtual base class pointers.  This must be
  691.    done only at the top-level of the object being constructed.
  692.  
  693.    INIT_LIST is list of initialization for constructor to perform.  */
  694. static void
  695. expand_aggr_vbase_init (binfo, exp, addr, init_list)
  696.      tree binfo;
  697.      tree exp;
  698.      tree addr;
  699.      tree init_list;
  700. {
  701.   tree type = BINFO_TYPE (binfo);
  702.  
  703.   if (TYPE_USES_VIRTUAL_BASECLASSES (type))
  704.     {
  705.       tree result = init_vbase_pointers (type, addr);
  706.       tree vbases;
  707.  
  708.       if (result)
  709.     expand_expr_stmt (build_compound_expr (result));
  710.  
  711.       /* Mark everything as having an initializer
  712.      (either explicit or default).  */
  713.       for (vbases = CLASSTYPE_VBASECLASSES (type);
  714.        vbases; vbases = TREE_CHAIN (vbases))
  715.     SET_BINFO_VBASE_INIT_MARKED (vbases);
  716.  
  717.       /* First, initialize baseclasses which could be baseclasses
  718.      for other virtual baseclasses.  */
  719.       for (vbases = CLASSTYPE_VBASECLASSES (type);
  720.        vbases; vbases = TREE_CHAIN (vbases))
  721.     /* Don't initialize twice.  */
  722.     if (BINFO_VBASE_INIT_MARKED (vbases))
  723.       {
  724.         tree tmp = result;
  725.  
  726.         while (BINFO_TYPE (vbases) != BINFO_TYPE (TREE_PURPOSE (tmp)))
  727.           tmp = TREE_CHAIN (tmp);
  728.         expand_aggr_vbase_init_1 (vbases, exp,
  729.                       TREE_OPERAND (TREE_VALUE (tmp), 0),
  730.                       init_list);
  731.       }
  732.  
  733.       /* Now initialize the baseclasses which don't have virtual baseclasses.  */
  734.       for (; result; result = TREE_CHAIN (result))
  735.     /* Don't initialize twice.  */
  736.     if (BINFO_VBASE_INIT_MARKED (TREE_PURPOSE (result)))
  737.       {
  738.         my_friendly_abort (47);
  739.         expand_aggr_vbase_init_1 (TREE_PURPOSE (result), exp,
  740.                       TREE_OPERAND (TREE_VALUE (result), 0),
  741.                       init_list);
  742.       }
  743.     }
  744. }
  745.  
  746. /* Subroutine to perform parser actions for member initialization.
  747.    S_ID is the scoped identifier.
  748.    NAME is the name of the member.
  749.    INIT is the initializer, or `void_type_node' if none.  */
  750. void
  751. do_member_init (s_id, name, init)
  752.      tree s_id, name, init;
  753. {
  754.   tree binfo, base;
  755.  
  756.   if (current_class_type == NULL_TREE
  757.       || ! is_aggr_typedef (s_id, 1))
  758.     return;
  759.   binfo = get_binfo (IDENTIFIER_TYPE_VALUE (s_id),
  760.               current_class_type, 1);
  761.   if (binfo == error_mark_node)
  762.     return;
  763.   if (binfo == 0)
  764.     {
  765.       error_not_base_type (IDENTIFIER_TYPE_VALUE (s_id), current_class_type);
  766.       return;
  767.     }
  768.  
  769.   base = convert_pointer_to (binfo, current_class_decl);
  770.   expand_member_init (build_indirect_ref (base, NULL_PTR), name, init);
  771. }
  772.  
  773. /* Function to give error message if member initialization specification
  774.    is erroneous.  FIELD is the member we decided to initialize.
  775.    TYPE is the type for which the initialization is being performed.
  776.    FIELD must be a member of TYPE, or the base type from which FIELD
  777.    comes must not need a constructor.
  778.    
  779.    MEMBER_NAME is the name of the member.  */
  780.  
  781. static int
  782. member_init_ok_or_else (field, type, member_name)
  783.      tree field;
  784.      tree type;
  785.      char *member_name;
  786. {
  787.   if (field == error_mark_node) return 0;
  788.   if (field == NULL_TREE)
  789.     {
  790.       error_with_aggr_type (type, "class `%s' does not have any field named `%s'",
  791.                 member_name);
  792.       return 0;
  793.     }
  794.   if (DECL_CONTEXT (field) != type
  795.       && TYPE_NEEDS_CONSTRUCTOR (DECL_CONTEXT (field)))
  796.     {
  797.       error ("member `%s' comes from base class needing constructor", member_name);
  798.       return 0;
  799.     }
  800.   return 1;
  801. }
  802.  
  803. /* If NAME is a viable field name for the aggregate DECL,
  804.    and PARMS is a viable parameter list, then expand an _EXPR
  805.    which describes this initialization.
  806.  
  807.    Note that we do not need to chase through the class's base classes
  808.    to look for NAME, because if it's in that list, it will be handled
  809.    by the constructor for that base class.
  810.  
  811.    We do not yet have a fixed-point finder to instantiate types
  812.    being fed to overloaded constructors.  If there is a unique
  813.    constructor, then argument types can be got from that one.
  814.  
  815.    If INIT is non-NULL, then it the initialization should
  816.    be placed in `current_base_init_list', where it will be processed
  817.    by `emit_base_init'.  */
  818. void
  819. expand_member_init (exp, name, init)
  820.      tree exp, name, init;
  821. {
  822.   extern tree ptr_type_node;    /* should be in tree.h */
  823.  
  824.   tree basetype = NULL_TREE, field;
  825.   tree parm;
  826.   tree rval, type;
  827.   tree actual_name;
  828.  
  829.   if (exp == NULL_TREE)
  830.     return;            /* complain about this later */
  831.  
  832.   type = TYPE_MAIN_VARIANT (TREE_TYPE (exp));
  833.  
  834.   if (name == NULL_TREE && IS_AGGR_TYPE (type))
  835.     switch (CLASSTYPE_N_BASECLASSES (type))
  836.       {
  837.       case 0:
  838.     error ("base class initializer specified, but no base class to initialize");
  839.     return;
  840.       case 1:
  841.     basetype = TYPE_BINFO_BASETYPE (type, 0);
  842.     break;
  843.       default:
  844.     error ("initializer for unnamed base class ambiguous");
  845.     error_with_aggr_type (type, "(type `%s' uses multiple inheritance)");
  846.     return;
  847.       }
  848.  
  849.   if (init)
  850.     {
  851.       /* The grammar should not allow fields which have names
  852.      that are TYPENAMEs.  Therefore, if the field has
  853.      a non-NULL TREE_TYPE, we may assume that this is an
  854.      attempt to initialize a base class member of the current
  855.      type.  Otherwise, it is an attempt to initialize a
  856.      member field.  */
  857.  
  858.       if (init == void_type_node)
  859.     init = NULL_TREE;
  860.  
  861.       if (name == NULL_TREE || IDENTIFIER_HAS_TYPE_VALUE (name))
  862.     {
  863.       tree base_init;
  864.  
  865.       if (name == NULL_TREE)
  866.         if (basetype)
  867.           name = TYPE_IDENTIFIER (basetype);
  868.         else
  869.           {
  870.         error ("no base class to initialize");
  871.         return;
  872.           }
  873.       else
  874.         {
  875.           basetype = IDENTIFIER_TYPE_VALUE (name);
  876.           if (basetype != type
  877.           && ! binfo_member (basetype, TYPE_BINFO (type))
  878.           && ! binfo_member (basetype, CLASSTYPE_VBASECLASSES (type)))
  879.         {
  880.           if (IDENTIFIER_CLASS_VALUE (name))
  881.             goto try_member;
  882.           if (TYPE_USES_VIRTUAL_BASECLASSES (type))
  883.             error ("type `%s' is not an immediate or virtual basetype for `%s'",
  884.                IDENTIFIER_POINTER (name),
  885.                TYPE_NAME_STRING (type));
  886.           else
  887.             error ("type `%s' is not an immediate basetype for `%s'",
  888.                IDENTIFIER_POINTER (name),
  889.                TYPE_NAME_STRING (type));
  890.           return;
  891.         }
  892.         }
  893.  
  894.       if (purpose_member (name, current_base_init_list))
  895.         {
  896.           error ("base class `%s' already initialized",
  897.              IDENTIFIER_POINTER (name));
  898.           return;
  899.         }
  900.  
  901.       base_init = build_tree_list (name, init);
  902.       TREE_TYPE (base_init) = basetype;
  903.       current_base_init_list = chainon (current_base_init_list, base_init);
  904.     }
  905.       else
  906.     {
  907.       tree member_init;
  908.  
  909.     try_member:
  910.       field = lookup_field (type, name, 1, 0);
  911.  
  912.       if (! member_init_ok_or_else (field, type, IDENTIFIER_POINTER (name)))
  913.         return;
  914.  
  915.       if (purpose_member (name, current_member_init_list))
  916.         {
  917.           error ("field `%s' already initialized", IDENTIFIER_POINTER (name));
  918.           return;
  919.         }
  920.  
  921.       member_init = build_tree_list (name, init);
  922.       TREE_TYPE (member_init) = TREE_TYPE (field);
  923.       current_member_init_list = chainon (current_member_init_list, member_init);
  924.     }
  925.       return;
  926.     }
  927.   else if (name == NULL_TREE)
  928.     {
  929.       compiler_error ("expand_member_init: name == NULL_TREE");
  930.       return;
  931.     }
  932.  
  933.   basetype = type;
  934.   field = lookup_field (basetype, name, 0, 0);
  935.  
  936.   if (! member_init_ok_or_else (field, basetype, IDENTIFIER_POINTER (name)))
  937.     return;
  938.  
  939.   /* now see if there is a constructor for this type
  940.      which will take these args. */
  941.  
  942.   if (TYPE_HAS_CONSTRUCTOR (TREE_TYPE (field)))
  943.     {
  944.       tree parmtypes, fndecl;
  945.  
  946.       if (TREE_CODE (exp) == VAR_DECL || TREE_CODE (exp) == PARM_DECL)
  947.     {
  948.       /* just know that we've seen something for this node */
  949.       DECL_INITIAL (exp) = error_mark_node;
  950.       TREE_USED (exp) = 1;
  951.     }
  952.       type = TYPE_MAIN_VARIANT (TREE_TYPE (field));
  953.       actual_name = TYPE_IDENTIFIER (type);
  954.       parm = build_component_ref (exp, name, 0, 0);
  955.  
  956.       /* Now get to the constructor.  */
  957.       fndecl = TREE_VEC_ELT (CLASSTYPE_METHOD_VEC (type), 0);
  958.       /* Get past destructor, if any.  */
  959.       if (TYPE_HAS_DESTRUCTOR (type))
  960.     fndecl = DECL_CHAIN (fndecl);
  961.  
  962.       if (fndecl)
  963.     my_friendly_assert (TREE_CODE (fndecl) == FUNCTION_DECL, 209);
  964.  
  965.       /* If the field is unique, we can use the parameter
  966.      types to guide possible type instantiation.  */
  967.       if (DECL_CHAIN (fndecl) == NULL_TREE)
  968.     {
  969.       /* There was a confusion here between
  970.          FIELD and FNDECL.  The following code
  971.          should be correct, but abort is here
  972.          to make sure.  */
  973.       my_friendly_abort (48);
  974.       parmtypes = FUNCTION_ARG_CHAIN (fndecl);
  975.     }
  976.       else
  977.     {
  978.       parmtypes = NULL_TREE;
  979.       fndecl = NULL_TREE;
  980.     }
  981.  
  982.       init = convert_arguments (parm, parmtypes, NULL_TREE, fndecl, LOOKUP_NORMAL);
  983.       if (init == NULL_TREE || TREE_TYPE (init) != error_mark_node)
  984.     rval = build_method_call (NULL_TREE, actual_name, init, NULL_TREE, LOOKUP_NORMAL);
  985.       else
  986.     return;
  987.  
  988.       if (rval != error_mark_node)
  989.     {
  990.       /* Now, fill in the first parm with our guy */
  991.       TREE_VALUE (TREE_OPERAND (rval, 1))
  992.         = build_unary_op (ADDR_EXPR, parm, 0);
  993.       TREE_TYPE (rval) = ptr_type_node;
  994.       TREE_SIDE_EFFECTS (rval) = 1;
  995.     }
  996.     }
  997.   else if (TYPE_NEEDS_CONSTRUCTING (TREE_TYPE (field)))
  998.     {
  999.       parm = build_component_ref (exp, name, 0, 0);
  1000.       expand_aggr_init (parm, NULL_TREE, 0);
  1001.       rval = error_mark_node;
  1002.     }
  1003.  
  1004.   /* Now initialize the member.  It does not have to
  1005.      be of aggregate type to receive initialization.  */
  1006.   if (rval != error_mark_node)
  1007.     expand_expr_stmt (rval);
  1008. }
  1009.  
  1010. /* This is like `expand_member_init', only it stores one aggregate
  1011.    value into another.
  1012.  
  1013.    INIT comes in two flavors: it is either a value which
  1014.    is to be stored in EXP, or it is a parameter list
  1015.    to go to a constructor, which will operate on EXP.
  1016.    If `init' is a CONSTRUCTOR, then we emit a warning message,
  1017.    explaining that such initializations are illegal.
  1018.  
  1019.    ALIAS_THIS is nonzero iff we are initializing something which is
  1020.    essentially an alias for C_C_D.  In this case, the base constructor
  1021.    may move it on us, and we must keep track of such deviations.
  1022.  
  1023.    If INIT resolves to a CALL_EXPR which happens to return
  1024.    something of the type we are looking for, then we know
  1025.    that we can safely use that call to perform the
  1026.    initialization.
  1027.  
  1028.    The virtual function table pointer cannot be set up here, because
  1029.    we do not really know its type.
  1030.  
  1031.    Virtual baseclass pointers are also set up here.
  1032.  
  1033.    This never calls operator=().
  1034.  
  1035.    When initializing, nothing is CONST.
  1036.  
  1037.    A default copy constructor may have to be used to perform the
  1038.    initialization.
  1039.  
  1040.    A constructor or a conversion operator may have to be used to
  1041.    perform the initialization, but not both, as it would be ambiguous.
  1042.    */
  1043.  
  1044. void
  1045. expand_aggr_init (exp, init, alias_this)
  1046.      tree exp, init;
  1047.      int alias_this;
  1048. {
  1049.   tree type = TREE_TYPE (exp);
  1050.   int was_const = TREE_READONLY (exp);
  1051.  
  1052.   if (init == error_mark_node)
  1053.     return;
  1054.  
  1055.   TREE_READONLY (exp) = 0;
  1056.  
  1057.   if (TREE_CODE (type) == ARRAY_TYPE)
  1058.     {
  1059.       /* Must arrange to initialize each element of EXP
  1060.      from elements of INIT.  */
  1061.       int was_const_elts = TYPE_READONLY (TREE_TYPE (type));
  1062.       tree itype = init ? TREE_TYPE (init) : NULL_TREE;
  1063.       if (was_const_elts)
  1064.     {
  1065.       tree atype = build_cplus_array_type (TYPE_MAIN_VARIANT (TREE_TYPE (type)),
  1066.                            TYPE_DOMAIN (type));
  1067.       if (init && (TREE_TYPE (exp) == TREE_TYPE (init)))
  1068.         TREE_TYPE (init) = atype;
  1069.       TREE_TYPE (exp) = atype;
  1070.     }
  1071.       expand_vec_init (exp, exp, array_type_nelts (type), init,
  1072.                init && comptypes (TREE_TYPE (init), TREE_TYPE (exp), 1));
  1073.       TREE_READONLY (exp) = was_const;
  1074.       TREE_TYPE (exp) = type;
  1075.       if (init) TREE_TYPE (init) = itype;
  1076.       return;
  1077.     }
  1078.  
  1079.   if (TREE_CODE (exp) == VAR_DECL || TREE_CODE (exp) == PARM_DECL)
  1080.     /* just know that we've seen something for this node */
  1081.     TREE_USED (exp) = 1;
  1082.  
  1083.   /* If initializing from a GNU C CONSTRUCTOR, consider the elts in the
  1084.      constructor as parameters to an implicit GNU C++ constructor.  */
  1085.   if (init && TREE_CODE (init) == CONSTRUCTOR
  1086.       && TYPE_HAS_CONSTRUCTOR (type)
  1087.       && TREE_TYPE (init) == type)
  1088.     init = CONSTRUCTOR_ELTS (init);
  1089.   expand_aggr_init_1 (TYPE_BINFO (type), exp, exp,
  1090.               init, alias_this, LOOKUP_NORMAL);
  1091.   TREE_READONLY (exp) = was_const;
  1092. }
  1093.  
  1094. static void
  1095. expand_default_init (binfo, true_exp, exp, type, init, alias_this, flags)
  1096.      tree binfo;
  1097.      tree true_exp, exp;
  1098.      tree type;
  1099.      tree init;
  1100.      int alias_this;
  1101.      int flags;
  1102. {
  1103.   /* It fails because there may not be a constructor which takes
  1104.      its own type as the first (or only parameter), but which does
  1105.      take other types via a conversion.  So, if the thing initializing
  1106.      the expression is a unit element of type X, first try X(X&),
  1107.      followed by initialization by X.  If neither of these work
  1108.      out, then look hard.  */
  1109.   tree rval;
  1110.   tree parms;
  1111.   int xxref_init_possible;
  1112.  
  1113.   if (init == NULL_TREE || TREE_CODE (init) == TREE_LIST)
  1114.     {
  1115.       parms = init;
  1116.       if (parms) init = TREE_VALUE (parms);
  1117.     }
  1118.   else if (TREE_CODE (init) == INDIRECT_REF && TREE_HAS_CONSTRUCTOR (init))
  1119.     {
  1120.       rval = convert_for_initialization (exp, type, init, 0, 0, 0, 0);
  1121.       expand_expr_stmt (rval);
  1122.       return;
  1123.     }
  1124.   else parms = build_tree_list (NULL_TREE, init);
  1125.  
  1126.   if (TYPE_HAS_INIT_REF (type)
  1127.       || init == NULL_TREE
  1128.       || TREE_CHAIN (parms) != NULL_TREE)
  1129.     xxref_init_possible = 0;
  1130.   else
  1131.     {
  1132.       xxref_init_possible = LOOKUP_SPECULATIVELY;
  1133.       flags &= ~LOOKUP_COMPLAIN;
  1134.     }
  1135.  
  1136.   if (TYPE_USES_VIRTUAL_BASECLASSES (type))
  1137.     {
  1138.       if (true_exp == exp)
  1139.     parms = tree_cons (NULL_TREE, integer_one_node, parms);
  1140.       else
  1141.     parms = tree_cons (NULL_TREE, integer_zero_node, parms);
  1142.       flags |= LOOKUP_HAS_IN_CHARGE;
  1143.     }
  1144.  
  1145.   rval = build_method_call (exp, constructor_name (type),
  1146.                 parms, binfo, flags|xxref_init_possible);
  1147.   if (rval == NULL_TREE && xxref_init_possible)
  1148.     {
  1149.       /* It is an error to implement a default copy constructor if
  1150.      (see ARM 12.8 for details) ... one case being if another
  1151.      copy constructor already exists. */
  1152.       tree init_type = TREE_TYPE (init);
  1153.       if (TREE_CODE (init_type) == REFERENCE_TYPE)
  1154.     init_type = TREE_TYPE (init_type);
  1155.       if (TYPE_MAIN_VARIANT (init_type) == TYPE_MAIN_VARIANT (type)
  1156.       || (IS_AGGR_TYPE (init_type)
  1157.           && UNIQUELY_DERIVED_FROM_P (type, init_type)))
  1158.     {
  1159.       if (type == BINFO_TYPE (binfo)
  1160.           && TYPE_USES_VIRTUAL_BASECLASSES (type))
  1161.         {
  1162.           tree addr = build_unary_op (ADDR_EXPR, exp, 0);
  1163.           expand_aggr_vbase_init (binfo, exp, addr, NULL_TREE);
  1164.  
  1165.           expand_expr_stmt (build_vbase_vtables_init (binfo, binfo,
  1166.                               exp, addr, 1));
  1167.         }
  1168.       expand_expr_stmt (build_modify_expr (exp, INIT_EXPR, init));
  1169.       return;
  1170.     }
  1171.       else
  1172.     rval = build_method_call (exp, constructor_name (type), parms,
  1173.                   binfo, flags);
  1174.     }
  1175.  
  1176.   /* Private, protected, or otherwise unavailable.  */
  1177.   if (rval == error_mark_node && (flags&LOOKUP_COMPLAIN))
  1178.     error_with_aggr_type (binfo, "in base initialization for class `%s'");
  1179.   /* A valid initialization using constructor.  */
  1180.   else if (rval != error_mark_node && rval != NULL_TREE)
  1181.     {
  1182.       /* p. 222: if the base class assigns to `this', then that
  1183.      value is used in the derived class.  */
  1184.       if ((flag_this_is_variable & 1) && alias_this)
  1185.     {
  1186.       TREE_TYPE (rval) = TREE_TYPE (current_class_decl);
  1187.       expand_assignment (current_class_decl, rval, 0, 0);
  1188.     }
  1189.       else
  1190.     expand_expr_stmt (rval);
  1191.     }
  1192.   else if (parms && TREE_CHAIN (parms) == NULL_TREE)
  1193.     {
  1194.       /* If we are initializing one aggregate value
  1195.      from another, and though there are constructors,
  1196.      and none accept the initializer, just do a bitwise
  1197.      copy.
  1198.  
  1199.      The above sounds wrong, ``If a class has any copy
  1200.      constructor defined, the default copy constructor will
  1201.      not be generated.'' 12.8 Copying Class Objects  (mrs)
  1202.  
  1203.      @@ This should reject initializer which a constructor
  1204.      @@ rejected on visibility gounds, but there is
  1205.      @@ no way right now to recognize that case with
  1206.      @@ just `error_mark_node'.  */
  1207.       tree itype;
  1208.       init = TREE_VALUE (parms);
  1209.       itype = TREE_TYPE (init);
  1210.       if (TREE_CODE (itype) == REFERENCE_TYPE)
  1211.     {
  1212.       init = convert_from_reference (init);
  1213.       itype = TREE_TYPE (init);
  1214.     }
  1215.       itype = TYPE_MAIN_VARIANT (itype);
  1216.  
  1217.       /* This is currently how the default X(X&) constructor
  1218.      is implemented.  */
  1219.       if (comptypes (TYPE_MAIN_VARIANT (type), itype, 0))
  1220.     {
  1221. #if 0
  1222.       warning ("bitwise copy in initialization of type `%s'",
  1223.            TYPE_NAME_STRING (type));
  1224. #endif
  1225.       rval = build (INIT_EXPR, type, exp, init);
  1226.       expand_expr_stmt (rval);
  1227.     }
  1228.       else
  1229.     {
  1230.       error_with_aggr_type (binfo, "in base initialization for class `%s',");
  1231.       error_with_aggr_type (type, "invalid initializer to constructor for type `%s'");
  1232.       return;
  1233.     }
  1234.     }
  1235.   else
  1236.     {
  1237.       if (init == NULL_TREE)
  1238.     my_friendly_assert (parms == NULL_TREE, 210);
  1239.       if (parms == NULL_TREE && TREE_VIA_VIRTUAL (binfo))
  1240.     error_with_aggr_type (binfo, "virtual baseclass `%s' does not have default initializer");
  1241.       else
  1242.     {
  1243.       error_with_aggr_type (binfo, "in base initialization for class `%s',");
  1244.       /* This will make an error message for us.  */
  1245.       build_method_call (exp, constructor_name (type), parms, binfo,
  1246.                  (TYPE_USES_VIRTUAL_BASECLASSES (type)
  1247.                   ? LOOKUP_NORMAL|LOOKUP_HAS_IN_CHARGE
  1248.                   : LOOKUP_NORMAL));
  1249.     }
  1250.       return;
  1251.     }
  1252.   /* Constructor has been called, but vtables may be for TYPE
  1253.      rather than for FOR_TYPE.  */
  1254. }
  1255.  
  1256. /* This function is responsible for initializing EXP with INIT
  1257.    (if any).
  1258.  
  1259.    BINFO is the binfo of the type for who we are performing the
  1260.    initialization.  For example, if W is a virtual base class of A and B,
  1261.    and C : A, B.
  1262.    If we are initializing B, then W must contain B's W vtable, whereas
  1263.    were we initializing C, W must contain C's W vtable.
  1264.  
  1265.    TRUE_EXP is nonzero if it is the true expression being initialized.
  1266.    In this case, it may be EXP, or may just contain EXP.  The reason we
  1267.    need this is because if EXP is a base element of TRUE_EXP, we
  1268.    don't necessarily know by looking at EXP where its virtual
  1269.    baseclass fields should really be pointing.  But we do know
  1270.    from TRUE_EXP.  In constructors, we don't know anything about
  1271.    the value being initialized.
  1272.  
  1273.    ALIAS_THIS serves the same purpose it serves for expand_aggr_init.
  1274.  
  1275.    FLAGS is just passes to `build_method_call'.  See that function for
  1276.    its description.  */
  1277.  
  1278. static void
  1279. expand_aggr_init_1 (binfo, true_exp, exp, init, alias_this, flags)
  1280.      tree binfo;
  1281.      tree true_exp, exp;
  1282.      tree init;
  1283.      int alias_this;
  1284.      int flags;
  1285. {
  1286.   tree type = TREE_TYPE (exp);
  1287.   tree init_type = NULL_TREE;
  1288.   tree rval;
  1289.  
  1290.   my_friendly_assert (init != error_mark_node && type != error_mark_node, 211);
  1291.  
  1292.   /* Use a function returning the desired type to initialize EXP for us.
  1293.      If the function is a constructor, and its first argument is
  1294.      NULL_TREE, know that it was meant for us--just slide exp on
  1295.      in and expand the constructor.  Constructors now come
  1296.      as TARGET_EXPRs.  */
  1297.   if (init)
  1298.     {
  1299.       tree init_list = NULL_TREE;
  1300.  
  1301.       if (TREE_CODE (init) == TREE_LIST)
  1302.     {
  1303.       init_list = init;
  1304.       if (TREE_CHAIN (init) == NULL_TREE)
  1305.         init = TREE_VALUE (init);
  1306.     }
  1307.  
  1308.       init_type = TREE_TYPE (init);
  1309.  
  1310.       if (TREE_CODE (init) != TREE_LIST)
  1311.     {
  1312.       if (TREE_CODE (init_type) == ERROR_MARK)
  1313.         return;
  1314.  
  1315. #if 0
  1316.       /* These lines are found troublesome 5/11/89.  */
  1317.       if (TREE_CODE (init_type) == REFERENCE_TYPE)
  1318.         init_type = TREE_TYPE (init_type);
  1319. #endif
  1320.  
  1321.       /* This happens when we use C++'s functional cast notation.
  1322.          If the types match, then just use the TARGET_EXPR
  1323.          directly.  Otherwise, we need to create the initializer
  1324.          separately from the object being initialized.  */
  1325.       if (TREE_CODE (init) == TARGET_EXPR)
  1326.         {
  1327.           if (init_type == type)
  1328.         {
  1329.           if (TREE_CODE (exp) == VAR_DECL
  1330.               || TREE_CODE (exp) == RESULT_DECL)
  1331.             /* Unify the initialization targets.  */
  1332.             DECL_RTL (TREE_OPERAND (init, 0)) = DECL_RTL (exp);
  1333.           else
  1334.             DECL_RTL (TREE_OPERAND (init, 0)) = expand_expr (exp, NULL_RTX, 0, 0);
  1335.  
  1336.           expand_expr_stmt (init);
  1337.           return;
  1338.         }
  1339.           else
  1340.         {
  1341.           init = TREE_OPERAND (init, 1);
  1342.           init = build (CALL_EXPR, init_type,
  1343.                 TREE_OPERAND (init, 0), TREE_OPERAND (init, 1), 0);
  1344.           TREE_SIDE_EFFECTS (init) = 1;
  1345. #if 0
  1346.           TREE_RAISES (init) = ??
  1347. #endif
  1348.             if (init_list)
  1349.               TREE_VALUE (init_list) = init;
  1350.         }
  1351.         }
  1352.  
  1353.       if (init_type == type && TREE_CODE (init) == CALL_EXPR
  1354. #if 0
  1355.           /* It is legal to directly initialize from a CALL_EXPR
  1356.          without going through X(X&), apparently.  */
  1357.           && ! TYPE_GETS_INIT_REF (type)
  1358. #endif
  1359.           )
  1360.         {
  1361.           /* A CALL_EXPR is a legitimate form of initialization, so
  1362.          we should not print this warning message.  */
  1363. #if 0
  1364.           /* Should have gone away due to 5/11/89 change.  */
  1365.           if (TREE_CODE (TREE_TYPE (init)) == REFERENCE_TYPE)
  1366.         init = convert_from_reference (init);
  1367. #endif
  1368.           expand_assignment (exp, init, 0, 0);
  1369.           if (exp == DECL_RESULT (current_function_decl))
  1370.         {
  1371.           /* Failing this assertion means that the return value
  1372.              from receives multiple initializations.  */
  1373.           my_friendly_assert (DECL_INITIAL (exp) == NULL_TREE
  1374.                       || DECL_INITIAL (exp) == error_mark_node,
  1375.                       212);
  1376.           DECL_INITIAL (exp) = init;
  1377.         }
  1378.           return;
  1379.         }
  1380.       else if (init_type == type
  1381.            && TREE_CODE (init) == COND_EXPR)
  1382.         {
  1383.           /* Push value to be initialized into the cond, where possible.
  1384.              Avoid spurious warning messages when initializing the
  1385.          result of this function.  */
  1386.           TREE_OPERAND (init, 1)
  1387.         = build_modify_expr (exp, INIT_EXPR, TREE_OPERAND (init, 1));
  1388.           if (exp == DECL_RESULT (current_function_decl))
  1389.         DECL_INITIAL (exp) = NULL_TREE;
  1390.           TREE_OPERAND (init, 2)
  1391.         = build_modify_expr (exp, INIT_EXPR, TREE_OPERAND (init, 2));
  1392.           if (exp == DECL_RESULT (current_function_decl))
  1393.         DECL_INITIAL (exp) = init;
  1394.           expand_expr (init, const0_rtx, VOIDmode, 0);
  1395.           free_temp_slots ();
  1396.           return;
  1397.         }
  1398.     }
  1399.  
  1400.       /* We did not know what we were initializing before.  Now we do.  */
  1401.       if (TREE_CODE (init) == TARGET_EXPR)
  1402.     {
  1403.       tree tmp = TREE_OPERAND (TREE_OPERAND (init, 1), 1);
  1404.  
  1405.       if (TREE_CODE (TREE_VALUE (tmp)) == NOP_EXPR
  1406.           && TREE_OPERAND (TREE_VALUE (tmp), 0) == integer_zero_node)
  1407.         {
  1408.           /* In order for this to work for RESULT_DECLs, if their
  1409.          type has a constructor, then they must be BLKmode
  1410.          so that they will be meaningfully addressable.  */
  1411.           tree arg = build_unary_op (ADDR_EXPR, exp, 0);
  1412.           init = TREE_OPERAND (init, 1);
  1413.           init = build (CALL_EXPR, build_pointer_type (TREE_TYPE (init)),
  1414.                 TREE_OPERAND (init, 0), TREE_OPERAND (init, 1), 0);
  1415.           TREE_SIDE_EFFECTS (init) = 1;
  1416. #if 0
  1417.           TREE_RAISES (init) = ??
  1418. #endif
  1419.           TREE_VALUE (TREE_OPERAND (init, 1))
  1420.         = convert_pointer_to (TREE_TYPE (TREE_TYPE (TREE_VALUE (tmp))), arg);
  1421.  
  1422.           if (alias_this)
  1423.         {
  1424.           expand_assignment (current_function_decl, init, 0, 0);
  1425.           return;
  1426.         }
  1427.           if (exp == DECL_RESULT (current_function_decl))
  1428.         {
  1429.           if (DECL_INITIAL (DECL_RESULT (current_function_decl)))
  1430.             fatal ("return value from function receives multiple initializations");
  1431.           DECL_INITIAL (exp) = init;
  1432.         }
  1433.           expand_expr_stmt (init);
  1434.           return;
  1435.         }
  1436.     }
  1437.  
  1438.       /* Handle this case: when calling a constructor: xyzzy foo(bar);
  1439.      which really means:  xyzzy foo = bar; Ugh!
  1440.  
  1441.      We can also be called with an initializer for an object
  1442.      which has virtual functions, but no constructors.  In that
  1443.      case, we perform the assignment first, then initialize
  1444.      the virtual function table pointer fields.  */
  1445.  
  1446.       if (! TYPE_NEEDS_CONSTRUCTING (type))
  1447.     {
  1448.       if (init_list && TREE_CHAIN (init_list))
  1449.         {
  1450.           warning ("initializer list being treated as compound expression");
  1451.           init = convert (TREE_TYPE (exp), build_compound_expr (init_list));
  1452.           if (init == error_mark_node)
  1453.         return;
  1454.         }
  1455.       if (TREE_CODE (exp) == VAR_DECL
  1456.           && TREE_CODE (init) == CONSTRUCTOR
  1457.           && TREE_HAS_CONSTRUCTOR (init)
  1458.           && flag_pic == 0)
  1459.         store_init_value (exp, init);
  1460.       else
  1461.         expand_assignment (exp, init, 0, 0);
  1462.  
  1463.       if (TYPE_VIRTUAL_P (type))
  1464.         expand_recursive_init (binfo, true_exp, exp, init, CLASSTYPE_BASE_INIT_LIST (type), alias_this);
  1465.       return;
  1466.     }
  1467.  
  1468.       /* See whether we can go through a type conversion operator.
  1469.      This wins over going through a non-existent constructor.  If
  1470.      there is a constructor, it is ambiguous.  */
  1471.       if (TREE_CODE (init) != TREE_LIST)
  1472.     {
  1473.       tree ttype = TREE_CODE (init_type) == REFERENCE_TYPE
  1474.         ? TREE_TYPE (init_type) : init_type;
  1475.  
  1476.       if (ttype != type && IS_AGGR_TYPE (ttype))
  1477.         {
  1478.           tree rval = build_type_conversion (CONVERT_EXPR, type, init, 0);
  1479.  
  1480.           if (rval)
  1481.         {
  1482.           /* See if there is a constructor for``type'' that takes a
  1483.              ``ttype''-typed object. */
  1484.           tree parms = build_tree_list (NULL_TREE, init);
  1485.           tree as_cons = NULL_TREE;
  1486.           if (TYPE_HAS_CONSTRUCTOR (type))
  1487.             as_cons = build_method_call (exp, constructor_name (type),
  1488.                          parms, binfo,
  1489.                          LOOKUP_SPECULATIVELY|LOOKUP_NO_CONVERSION);
  1490.           if (as_cons != NULL_TREE && as_cons != error_mark_node)
  1491.             {
  1492.               tree name = TYPE_NAME (type);
  1493.               if (TREE_CODE (name) == TYPE_DECL)
  1494.             name = DECL_NAME (name);
  1495.               /* ANSI C++ June 5 1992 WP 12.3.2.6.1 */
  1496.               error ("ambiguity between conversion to `%s' and constructor",
  1497.                  IDENTIFIER_POINTER (name));
  1498.             }
  1499.           else
  1500.             expand_assignment (exp, rval, 0, 0);
  1501.           return;
  1502.         }
  1503.         }
  1504.     }
  1505.     }
  1506.  
  1507.   /* Handle default copy constructors here, does not matter if there is
  1508.      a constructor or not.  */
  1509.   if (type == init_type && IS_AGGR_TYPE (type)
  1510.       && init && TREE_CODE (init) != TREE_LIST)
  1511.     expand_default_init (binfo, true_exp, exp, type, init, alias_this, flags);
  1512.   /* Not sure why this is here... */
  1513.   else if (TYPE_HAS_CONSTRUCTOR (type))
  1514.     expand_default_init (binfo, true_exp, exp, type, init, alias_this, flags);
  1515.   else if (TREE_CODE (type) == ARRAY_TYPE)
  1516.     {
  1517.       if (TYPE_NEEDS_CONSTRUCTING (TREE_TYPE (type)))
  1518.     expand_vec_init (exp, exp, array_type_nelts (type), init, 0);
  1519.       else if (TYPE_VIRTUAL_P (TREE_TYPE (type)))
  1520.     sorry ("arrays of objects with virtual functions but no constructors");
  1521.     }
  1522.   else
  1523.     expand_recursive_init (binfo, true_exp, exp, init,
  1524.                CLASSTYPE_BASE_INIT_LIST (type), alias_this);
  1525. }
  1526.  
  1527. /* A pointer which holds the initializer.  First call to
  1528.    expand_aggr_init gets this value pointed to, and sets it to init_null.  */
  1529. static tree *init_ptr, init_null;
  1530.  
  1531. /* Subroutine of expand_recursive_init:
  1532.  
  1533.    ADDR is the address of the expression being initialized.
  1534.    INIT_LIST is the cons-list of initializations to be performed.
  1535.    ALIAS_THIS is its same, lovable self.  */
  1536. static void
  1537. expand_recursive_init_1 (binfo, true_exp, addr, init_list, alias_this)
  1538.      tree binfo, true_exp, addr;
  1539.      tree init_list;
  1540.      int alias_this;
  1541. {
  1542.   while (init_list)
  1543.     {
  1544.       if (TREE_PURPOSE (init_list))
  1545.     {
  1546.       if (TREE_CODE (TREE_PURPOSE (init_list)) == FIELD_DECL)
  1547.         {
  1548.           tree member = TREE_PURPOSE (init_list);
  1549.           tree subexp = build_indirect_ref (convert_pointer_to (TREE_VALUE (init_list), addr), 0);
  1550.           tree member_base = build (COMPONENT_REF, TREE_TYPE (member), subexp, member);
  1551.           if (IS_AGGR_TYPE (TREE_TYPE (member)))
  1552.         expand_aggr_init (member_base, DECL_INITIAL (member), 0);
  1553.           else if (TREE_CODE (TREE_TYPE (member)) == ARRAY_TYPE
  1554.                && TYPE_NEEDS_CONSTRUCTING (TREE_TYPE (member)))
  1555.         {
  1556.           member_base = save_expr (default_conversion (member_base));
  1557.           expand_vec_init (member, member_base,
  1558.                    array_type_nelts (TREE_TYPE (member)),
  1559.                    DECL_INITIAL (member), 0);
  1560.         }
  1561.           else expand_expr_stmt (build_modify_expr (member_base, INIT_EXPR, DECL_INITIAL (member)));
  1562.         }
  1563.       else if (TREE_CODE (TREE_PURPOSE (init_list)) == TREE_LIST)
  1564.         {
  1565.           expand_recursive_init_1 (binfo, true_exp, addr, TREE_PURPOSE (init_list), alias_this);
  1566.           expand_recursive_init_1 (binfo, true_exp, addr, TREE_VALUE (init_list), alias_this);
  1567.         }
  1568.       else if (TREE_CODE (TREE_PURPOSE (init_list)) == ERROR_MARK)
  1569.         {
  1570.           /* Only initialize the virtual function tables if we
  1571.          are initializing the ultimate users of those vtables.  */
  1572.           if (TREE_VALUE (init_list))
  1573.         {
  1574.           /* We have to ensure that the second argment to
  1575.              build_virtual_init is in binfo's hierarchy.  */
  1576.           expand_expr_stmt (build_virtual_init (binfo,
  1577.                             get_binfo (TREE_VALUE (init_list), binfo, 0),
  1578.                             addr));
  1579.           if (TREE_VALUE (init_list) == binfo
  1580.               && TYPE_USES_VIRTUAL_BASECLASSES (BINFO_TYPE (binfo)))
  1581.             expand_expr_stmt (build_vbase_vtables_init (binfo, binfo, true_exp, addr, 1));
  1582.         }
  1583.         }
  1584.       else my_friendly_abort (49);
  1585.     }
  1586.       else if (TREE_VALUE (init_list)
  1587.            && TREE_CODE (TREE_VALUE (init_list)) == TREE_VEC)
  1588.     {
  1589.       tree subexp = build_indirect_ref (convert_pointer_to (TREE_VALUE (init_list), addr), 0);
  1590.       expand_aggr_init_1 (binfo, true_exp, subexp, *init_ptr,
  1591.                   alias_this && BINFO_OFFSET_ZEROP (TREE_VALUE (init_list)),
  1592.                   LOOKUP_PROTECTED_OK|LOOKUP_COMPLAIN);
  1593.  
  1594.       /* INIT_PTR is used up.  */
  1595.       init_ptr = &init_null;
  1596.     }
  1597.       else
  1598.     my_friendly_abort (50);
  1599.       init_list = TREE_CHAIN (init_list);
  1600.     }
  1601. }
  1602.  
  1603. /* Initialize EXP with INIT.  Type EXP does not have a constructor,
  1604.    but it has a baseclass with a constructor or a virtual function
  1605.    table which needs initializing.
  1606.  
  1607.    INIT_LIST is a cons-list describing what parts of EXP actually
  1608.    need to be initialized.  INIT is given to the *unique*, first
  1609.    constructor within INIT_LIST.  If there are multiple first
  1610.    constructors, such as with multiple inheritance, INIT must
  1611.    be zero or an ambiguity error is reported.
  1612.  
  1613.    ALIAS_THIS is passed from `expand_aggr_init'.  See comments
  1614.    there.  */
  1615.  
  1616. static void
  1617. expand_recursive_init (binfo, true_exp, exp, init, init_list, alias_this)
  1618.      tree binfo, true_exp, exp, init;
  1619.      tree init_list;
  1620.      int alias_this;
  1621. {
  1622.   tree *old_init_ptr = init_ptr;
  1623.   tree addr = build_unary_op (ADDR_EXPR, exp, 0);
  1624.   init_ptr = &init;
  1625.  
  1626.   if (true_exp == exp && TYPE_USES_VIRTUAL_BASECLASSES (BINFO_TYPE (binfo)))
  1627.     {
  1628.       expand_aggr_vbase_init (binfo, exp, addr, init_list);
  1629.       expand_expr_stmt (build_vbase_vtables_init (binfo, binfo, true_exp, addr, 1));
  1630.     }
  1631.   expand_recursive_init_1 (binfo, true_exp, addr, init_list, alias_this);
  1632.  
  1633.   if (*init_ptr)
  1634.     {
  1635.       tree type = TREE_TYPE (exp);
  1636.  
  1637.       if (TREE_CODE (type) == REFERENCE_TYPE)
  1638.     type = TREE_TYPE (type);
  1639.       if (IS_AGGR_TYPE (type))
  1640.     error_with_aggr_type (type, "unexpected argument to constructor `%s'");
  1641.       else
  1642.     error ("unexpected argument to constructor");
  1643.     }
  1644.   init_ptr = old_init_ptr;
  1645. }
  1646.  
  1647. /* Report an error if NAME is not the name of a user-defined,
  1648.    aggregate type.  If OR_ELSE is nonzero, give an error message.  */
  1649. int
  1650. is_aggr_typedef (name, or_else)
  1651.      tree name;
  1652.      int or_else;
  1653. {
  1654.   tree type;
  1655.  
  1656.   if (name == error_mark_node)
  1657.     return 0;
  1658.  
  1659.   if (! IDENTIFIER_HAS_TYPE_VALUE (name))
  1660.     {
  1661.       if (or_else)
  1662.     error ("`%s' fails to be an aggregate typedef",
  1663.            IDENTIFIER_POINTER (name));
  1664.       return 0;
  1665.     }
  1666.   type = IDENTIFIER_TYPE_VALUE (name);
  1667.   if (! IS_AGGR_TYPE (type))
  1668.     {
  1669.       if (or_else)
  1670.     error ("type `%s' is of non-aggregate type",
  1671.            IDENTIFIER_POINTER (name));
  1672.       return 0;
  1673.     }
  1674.   return 1;
  1675. }
  1676.  
  1677. /* This code could just as well go in `cp-class.c', but is placed here for
  1678.    modularity.  */
  1679.  
  1680. /* For an expression of the form CNAME :: NAME (PARMLIST), build
  1681.    the appropriate function call.  */
  1682. tree
  1683. build_member_call (cname, name, parmlist)
  1684.      tree cname, name, parmlist;
  1685. {
  1686.   tree type, t;
  1687.   tree method_name = name;
  1688.   int dtor = 0;
  1689.   int dont_use_this = 0;
  1690.   tree basetype_path, decl;
  1691.  
  1692.   if (TREE_CODE (method_name) == BIT_NOT_EXPR)
  1693.     {
  1694.       method_name = TREE_OPERAND (method_name, 0);
  1695.       dtor = 1;
  1696.     }
  1697.  
  1698.   if (TREE_CODE (cname) == SCOPE_REF)
  1699.     cname = resolve_scope_to_name (NULL_TREE, cname);
  1700.  
  1701.   if (cname == NULL_TREE || ! is_aggr_typedef (cname, 1))
  1702.     return error_mark_node;
  1703.  
  1704.   /* An operator we did not like.  */
  1705.   if (name == NULL_TREE)
  1706.     return error_mark_node;
  1707.  
  1708.   if (dtor)
  1709.     {
  1710.       if (! TYPE_HAS_DESTRUCTOR (IDENTIFIER_TYPE_VALUE (cname)))
  1711.     error ("type `%s' does not have a destructor",
  1712.            IDENTIFIER_POINTER (cname));
  1713.       else
  1714.     error ("destructor specification error");
  1715.       return error_mark_node;
  1716.     }
  1717.  
  1718.   type = IDENTIFIER_TYPE_VALUE (cname);
  1719.  
  1720.   /* No object?  Then just fake one up, and let build_method_call
  1721.      figure out what to do.  */
  1722.   if (current_class_type == 0
  1723.       || get_base_distance (type, current_class_type, 0, &basetype_path) == -1)
  1724.     dont_use_this = 1;
  1725.  
  1726.   if (dont_use_this)
  1727.     {
  1728.       basetype_path = NULL_TREE;
  1729.       decl = build1 (NOP_EXPR, TYPE_POINTER_TO (type), error_mark_node);
  1730.     }
  1731.   else if (current_class_decl == 0)
  1732.     {
  1733.       dont_use_this = 1;
  1734.       decl = build1 (NOP_EXPR, TYPE_POINTER_TO (type), error_mark_node);
  1735.     }
  1736.   else
  1737.     {
  1738.       decl = current_class_decl;
  1739.       if (TREE_TYPE (decl) != type)
  1740.     decl = convert (TYPE_POINTER_TO (type), decl);
  1741.     }
  1742.  
  1743.   if (t = lookup_fnfields (TYPE_BINFO (type), method_name, 0))
  1744.     return build_method_call (decl, method_name, parmlist, basetype_path,
  1745.                   LOOKUP_NORMAL|LOOKUP_NONVIRTUAL);
  1746.   if (TREE_CODE (name) == IDENTIFIER_NODE
  1747.       && (t = lookup_field (TYPE_BINFO (type), name, 1, 0)))
  1748.     {
  1749.       if (t == error_mark_node)
  1750.     return error_mark_node;
  1751.       if (TREE_CODE (t) == FIELD_DECL)
  1752.     {
  1753.       if (dont_use_this)
  1754.         {
  1755.           error ("invalid use of non-static field `%s'",
  1756.              IDENTIFIER_POINTER (name));
  1757.           return error_mark_node;
  1758.         }
  1759.       decl = build (COMPONENT_REF, TREE_TYPE (t), decl, t);
  1760.     }
  1761.       else if (TREE_CODE (t) == VAR_DECL)
  1762.     decl = t;
  1763.       else
  1764.     {
  1765.       error ("invalid use of member `%s::%s'",
  1766.          IDENTIFIER_POINTER (cname), name);
  1767.       return error_mark_node;
  1768.     }
  1769.       if (TYPE_LANG_SPECIFIC (TREE_TYPE (decl))
  1770.       && TYPE_OVERLOADS_CALL_EXPR (TREE_TYPE (decl)))
  1771.     return build_opfncall (CALL_EXPR, LOOKUP_NORMAL, decl, parmlist, NULL_TREE);
  1772.       return build_function_call (decl, parmlist);
  1773.     }
  1774.   else
  1775.     {
  1776.       char *err_name;
  1777.       if (TREE_CODE (name) == IDENTIFIER_NODE)
  1778.     {
  1779.       if (IDENTIFIER_OPNAME_P (name))
  1780.         {
  1781.           char *op_name = operator_name_string (method_name);
  1782.           err_name = (char *)alloca (13 + strlen (op_name));
  1783.           sprintf (err_name, "operator %s", op_name);
  1784.         }
  1785.       else
  1786.         err_name = IDENTIFIER_POINTER (name);
  1787.     }
  1788.       else
  1789.     my_friendly_abort (51);
  1790.  
  1791.       error ("no method `%s::%s'", IDENTIFIER_POINTER (cname), err_name);
  1792.       return error_mark_node;
  1793.     }
  1794. }
  1795.  
  1796. /* Build a reference to a member of an aggregate.  This is not a
  1797.    C++ `&', but really something which can have its address taken,
  1798.    and then act as a pointer to member, for example CNAME :: FIELD
  1799.    can have its address taken by saying & CNAME :: FIELD.
  1800.  
  1801.    @@ Prints out lousy diagnostics for operator <typename>
  1802.    @@ fields.
  1803.  
  1804.    @@ This function should be rewritten and placed in cp-search.c.  */
  1805. tree
  1806. build_offset_ref (cname, name)
  1807.      tree cname, name;
  1808. {
  1809.   tree decl, type, fnfields, fields, t = error_mark_node;
  1810.   tree basetypes = NULL_TREE;
  1811.   int dtor = 0;
  1812.  
  1813.   if (TREE_CODE (cname) == SCOPE_REF)
  1814.     cname = resolve_scope_to_name (NULL_TREE, cname);
  1815.  
  1816.   if (cname == NULL_TREE || ! is_aggr_typedef (cname, 1))
  1817.     return error_mark_node;
  1818.  
  1819.   type = IDENTIFIER_TYPE_VALUE (cname);
  1820.  
  1821.   if (TREE_CODE (name) == BIT_NOT_EXPR)
  1822.     {
  1823.       dtor = 1;
  1824.       name = TREE_OPERAND (name, 0);
  1825.     }
  1826.  
  1827.   if (TYPE_SIZE (type) == 0)
  1828.     {
  1829.       t = IDENTIFIER_CLASS_VALUE (name);
  1830.       if (t == 0)
  1831.     {
  1832.       error_with_aggr_type (type, "incomplete type `%s' does not have member `%s'", IDENTIFIER_POINTER (name));
  1833.       return error_mark_node;
  1834.     }
  1835.       if (TREE_CODE (t) == TYPE_DECL)
  1836.     {
  1837.       error_with_decl (t, "member `%s' is just a type declaration");
  1838.       return error_mark_node;
  1839.     }
  1840.       if (TREE_CODE (t) == VAR_DECL || TREE_CODE (t) == CONST_DECL)
  1841.     {
  1842.       TREE_USED (t) = 1;
  1843.       return t;
  1844.     }
  1845.       if (TREE_CODE (t) == FIELD_DECL)
  1846.     sorry ("use of member in incomplete aggregate type");
  1847.       else if (TREE_CODE (t) == FUNCTION_DECL)
  1848.     sorry ("use of member function in incomplete aggregate type");
  1849.       else
  1850.     my_friendly_abort (52);
  1851.       return error_mark_node;
  1852.     }
  1853.  
  1854.   if (TREE_CODE (name) == TYPE_EXPR)
  1855.     /* Pass a TYPE_DECL to build_component_type_expr.  */
  1856.     return build_component_type_expr (TYPE_NAME (TREE_TYPE (cname)),
  1857.                       name, NULL_TREE, 1);
  1858.  
  1859.   fnfields = lookup_fnfields (TYPE_BINFO (type), name, 1);
  1860.   fields = lookup_field (type, name, 0, 0);
  1861.  
  1862.   if (fields == error_mark_node || fnfields == error_mark_node)
  1863.     return error_mark_node;
  1864.  
  1865.   if (current_class_type == 0
  1866.       || get_base_distance (type, current_class_type, 0, &basetypes) == -1)
  1867.     {
  1868.       basetypes = TYPE_BINFO (type);
  1869.       decl = build1 (NOP_EXPR,
  1870.              IDENTIFIER_TYPE_VALUE (cname),
  1871.              error_mark_node);
  1872.     }
  1873.   else if (current_class_decl == 0)
  1874.     decl = build1 (NOP_EXPR, IDENTIFIER_TYPE_VALUE (cname),
  1875.            error_mark_node);
  1876.   else decl = C_C_D;
  1877.  
  1878.   /* A lot of this logic is now handled in lookup_field and
  1879.      lookup_fnfield. */
  1880.   if (fnfields)
  1881.     {
  1882.       basetypes = TREE_PURPOSE (fnfields);
  1883.  
  1884.       /* Go from the TREE_BASELINK to the member function info.  */
  1885.       t = TREE_VALUE (fnfields);
  1886.  
  1887.       if (fields)
  1888.     {
  1889.       if (DECL_FIELD_CONTEXT (fields) == DECL_FIELD_CONTEXT (t))
  1890.         {
  1891.           error ("ambiguous member reference: member `%s' defined as both field and function",
  1892.              IDENTIFIER_POINTER (name));
  1893.           return error_mark_node;
  1894.         }
  1895.       if (UNIQUELY_DERIVED_FROM_P (DECL_FIELD_CONTEXT (fields), DECL_FIELD_CONTEXT (t)))
  1896.         ;
  1897.       else if (UNIQUELY_DERIVED_FROM_P (DECL_FIELD_CONTEXT (t), DECL_FIELD_CONTEXT (fields)))
  1898.         t = fields;
  1899.       else
  1900.         {
  1901.           error ("ambiguous member reference: member `%s' derives from distinct classes in multiple inheritance lattice");
  1902.           return error_mark_node;
  1903.         }
  1904.     }
  1905.  
  1906.       if (t == TREE_VALUE (fnfields))
  1907.     {
  1908.       extern int flag_save_memoized_contexts;
  1909.  
  1910.       /* This does not handle visibility checking yet.  */
  1911.       if (DECL_CHAIN (t) == NULL_TREE || dtor)
  1912.         {
  1913.           enum visibility_type visibility;
  1914.  
  1915.           /* unique functions are handled easily.  */
  1916.         unique:
  1917.           visibility = compute_visibility (basetypes, t);
  1918.           if (visibility == visibility_protected)
  1919.         {
  1920.           error_with_decl (t, "member function `%s' is protected");
  1921.           error ("in this context");
  1922.           return error_mark_node;
  1923.         }
  1924.           if (visibility == visibility_private)
  1925.         {
  1926.           error_with_decl (t, "member function `%s' is private");
  1927.           error ("in this context");
  1928.           return error_mark_node;
  1929.         }
  1930.           assemble_external (t);
  1931.           return build (OFFSET_REF, TREE_TYPE (t), decl, t);
  1932.         }
  1933.  
  1934.       /* overloaded functions may need more work.  */
  1935.       if (cname == name)
  1936.         {
  1937.           if (TYPE_HAS_DESTRUCTOR (type)
  1938.           && DECL_CHAIN (DECL_CHAIN (t)) == NULL_TREE)
  1939.         {
  1940.           t = DECL_CHAIN (t);
  1941.           goto unique;
  1942.         }
  1943.         }
  1944.       /* FNFIELDS is most likely allocated on the search_obstack,
  1945.          which will go away after this class scope.  If we need
  1946.          to save this value for later (either for memoization
  1947.          or for use as an initializer for a static variable), then
  1948.          do so here.
  1949.  
  1950.          ??? The smart thing to do for the case of saving initializers
  1951.          is to resolve them before we're done with this scope.  */
  1952.       if (!TREE_PERMANENT (fnfields)
  1953.           && ((flag_save_memoized_contexts && global_bindings_p ())
  1954.           || ! allocation_temporary_p ()))
  1955.         fnfields = copy_list (fnfields);
  1956.       t = build_tree_list (error_mark_node, fnfields);
  1957.       TREE_TYPE (t) = build_offset_type (type, unknown_type_node);
  1958.       return t;
  1959.     }
  1960.     }
  1961.  
  1962.   /* Now that we know we are looking for a field, see if we
  1963.      have access to that field.  Lookup_field will give us the
  1964.      error message.  */
  1965.  
  1966.   t = lookup_field (basetypes, name, 1, 0);
  1967.  
  1968.   if (t == error_mark_node)
  1969.     return error_mark_node;
  1970.  
  1971.   if (t == NULL_TREE)
  1972.     {
  1973.       char *print_name, *non_operator = "<";
  1974.  
  1975.       if (name == ansi_opname[(int) TYPE_EXPR])
  1976.     {
  1977.       error ("type conversion operator not a member of type `%s'",
  1978.          IDENTIFIER_POINTER (cname));
  1979.       return error_mark_node;
  1980.     }
  1981.  
  1982.       if ((IDENTIFIER_POINTER (name))[0] == '_'
  1983.       && (IDENTIFIER_POINTER (name))[1] == '_')
  1984.     print_name = operator_name_string (name);
  1985.       else
  1986.     print_name = non_operator;
  1987.  
  1988.       /* First character of "<invalid operator>".  */
  1989.       if (print_name[0] == '<')
  1990.     error ("field `%s' is not a member of type `%s'",
  1991.            IDENTIFIER_POINTER (name),
  1992.            IDENTIFIER_POINTER (cname));
  1993.       else
  1994.     error ("operator `%s' is not a member of type `%s'",
  1995.            print_name, IDENTIFIER_POINTER (cname));
  1996.       return error_mark_node;
  1997.     }
  1998.  
  1999.   if (TREE_CODE (t) == TYPE_DECL)
  2000.     {
  2001.       error_with_decl (t, "member `%s' is just a type declaration");
  2002.       return error_mark_node;
  2003.     }
  2004.   /* static class members and class-specific enum
  2005.      values can be returned without further ado.  */
  2006.   if (TREE_CODE (t) == VAR_DECL || TREE_CODE (t) == CONST_DECL)
  2007.     {
  2008.       assemble_external (t);
  2009.       TREE_USED (t) = 1;
  2010.       return t;
  2011.     }
  2012.  
  2013.   /* static class functions too.  */
  2014.   if (TREE_CODE (t) == FUNCTION_DECL && TREE_CODE (TREE_TYPE (t)) == FUNCTION_TYPE)
  2015.     my_friendly_abort (53);
  2016.  
  2017.   /* In member functions, the form `cname::name' is no longer
  2018.      equivalent to `this->cname::name'.  */
  2019.   return build (OFFSET_REF, build_offset_type (type, TREE_TYPE (t)), decl, t);
  2020. }
  2021.  
  2022. /* Given an object EXP and a member function reference MEMBER,
  2023.    return the address of the actual member function.  */
  2024. tree
  2025. get_member_function (exp_addr_ptr, exp, member)
  2026.      tree *exp_addr_ptr;
  2027.      tree exp, member;
  2028. {
  2029.   tree ctype = TREE_TYPE (exp);
  2030.   tree function = save_expr (build_unary_op (ADDR_EXPR, member, 0));
  2031.  
  2032.   if (TYPE_VIRTUAL_P (ctype)
  2033.       || (flag_all_virtual == 1 && TYPE_OVERLOADS_METHOD_CALL_EXPR (ctype)))
  2034.     {
  2035.       tree e0, e1, e3;
  2036.       tree exp_addr;
  2037.  
  2038.       /* Save away the unadulterated `this' pointer.  */
  2039.       exp_addr = save_expr (*exp_addr_ptr);
  2040.  
  2041.       /* Cast function to signed integer.  */
  2042.       e0 = build1 (NOP_EXPR, integer_type_node, function);
  2043.  
  2044. #ifdef VTABLE_USES_MASK
  2045.       /* If we are willing to limit the number of
  2046.      virtual functions a class may have to some
  2047.      *small* number, then if, for a function address,
  2048.      we are passed some small number, we know that
  2049.      it is a virtual function index, and work from there.  */
  2050.       e1 = build (BIT_AND_EXPR, integer_type_node, e0, vtbl_mask);
  2051. #else
  2052.       /* There is a hack here that takes advantage of
  2053.      twos complement arithmetic, and the fact that
  2054.      there are more than one UNITS to the WORD.
  2055.      If the high bit is set for the `function',
  2056.      then we pretend it is a virtual function,
  2057.      and the array indexing will knock this bit
  2058.      out the top, leaving a valid index.  */
  2059.       if (UNITS_PER_WORD <= 1)
  2060.     my_friendly_abort (54);
  2061.  
  2062.       e1 = build (GT_EXPR, integer_type_node, e0, integer_zero_node);
  2063.       e1 = build_compound_expr (tree_cons (NULL_TREE, exp_addr,
  2064.                        build_tree_list (NULL_TREE, e1)));
  2065.       e1 = save_expr (e1);
  2066. #endif
  2067.  
  2068.       if (TREE_SIDE_EFFECTS (*exp_addr_ptr))
  2069.     {
  2070.       exp = build_indirect_ref (exp_addr, 0);
  2071.       *exp_addr_ptr = exp_addr;
  2072.     }
  2073.  
  2074.       /* This is really hairy: if the function pointer is a pointer
  2075.      to a non-virtual member function, then we can't go mucking
  2076.      with the `this' pointer (any more than we already have to
  2077.      this point).  If it is a pointer to a virtual member function,
  2078.      then we have to adjust the `this' pointer according to
  2079.      what the virtual function table tells us.  */
  2080.  
  2081.       e3 = build_vfn_ref (exp_addr_ptr, exp, e0);
  2082.       my_friendly_assert (e3 != error_mark_node, 213);
  2083.  
  2084.       /* Change this pointer type from `void *' to the
  2085.      type it is really supposed to be.  */
  2086.       TREE_TYPE (e3) = TREE_TYPE (function);
  2087.  
  2088.       /* If non-virtual, use what we had originally.  Otherwise,
  2089.      use the value we get from the virtual function table.  */
  2090.       *exp_addr_ptr = build_conditional_expr (e1, exp_addr, *exp_addr_ptr);
  2091.  
  2092.       function = build_conditional_expr (e1, function, e3);
  2093.     }
  2094.   return build_indirect_ref (function, 0);
  2095. }
  2096.  
  2097. /* If a OFFSET_REF made it through to here, then it did
  2098.    not have its address taken.  */
  2099.  
  2100. tree
  2101. resolve_offset_ref (exp)
  2102.      tree exp;
  2103. {
  2104.   tree type = TREE_TYPE (exp);
  2105.   tree base = NULL_TREE;
  2106.   tree member;
  2107.   tree basetype, addr;
  2108.  
  2109.   if (TREE_CODE (exp) == TREE_LIST)
  2110.     return build_unary_op (ADDR_EXPR, exp, 0);
  2111.  
  2112.   if (TREE_CODE (exp) != OFFSET_REF)
  2113.     {
  2114.       my_friendly_assert (TREE_CODE (type) == OFFSET_TYPE, 214);
  2115.       if (TYPE_OFFSET_BASETYPE (type) != current_class_type)
  2116.     {
  2117.       error ("object missing in use of pointer-to-member construct");
  2118.       return error_mark_node;
  2119.     }
  2120.       member = exp;
  2121.       type = TREE_TYPE (type);
  2122.       base = C_C_D;
  2123.     }
  2124.   else
  2125.     {
  2126.       member = TREE_OPERAND (exp, 1);
  2127.       base = TREE_OPERAND (exp, 0);
  2128.     }
  2129.  
  2130.   if (TREE_CODE (member) == VAR_DECL
  2131.       || TREE_CODE (TREE_TYPE (member)) == FUNCTION_TYPE)
  2132.     {
  2133.       /* These were static members.  */
  2134.       if (mark_addressable (member) == 0)
  2135.     return error_mark_node;
  2136.       return member;
  2137.     }
  2138.  
  2139.   /* Syntax error can cause a member which should
  2140.      have been seen as static to be grok'd as non-static.  */
  2141.   if (TREE_CODE (member) == FIELD_DECL && C_C_D == NULL_TREE)
  2142.     {
  2143.       if (TREE_ADDRESSABLE (member) == 0)
  2144.     {
  2145.       error_with_decl (member, "member `%s' is non-static in static member function context");
  2146.       error ("at this point in file");
  2147.       TREE_ADDRESSABLE (member) = 1;
  2148.     }
  2149.       return error_mark_node;
  2150.     }
  2151.  
  2152.   /* The first case is really just a reference to a member of `this'.  */
  2153.   if (TREE_CODE (member) == FIELD_DECL
  2154.       && (base == C_C_D
  2155.       || (TREE_CODE (base) == NOP_EXPR
  2156.           && TREE_OPERAND (base, 0) == error_mark_node)))
  2157.     {
  2158.       tree basetype_path;
  2159.       enum visibility_type visibility;
  2160.  
  2161.       basetype = DECL_CONTEXT (member);
  2162.       if (get_base_distance (basetype, current_class_type, 0, &basetype_path) < 0)
  2163.     {
  2164.       error_not_base_type (basetype, current_class_type);
  2165.       return error_mark_node;
  2166.     }
  2167.       addr = convert_pointer_to (basetype, current_class_decl);
  2168.       visibility = compute_visibility (basetype_path, member);
  2169.       if (visibility == visibility_public)
  2170.     return build (COMPONENT_REF, TREE_TYPE (member),
  2171.               build_indirect_ref (addr, 0), member);
  2172.       if (visibility == visibility_protected)
  2173.     {
  2174.       error_with_decl (member, "member `%s' is protected");
  2175.       error ("in this context");
  2176.       return error_mark_node;
  2177.     }
  2178.       if (visibility == visibility_private)
  2179.     {
  2180.       error_with_decl (member, "member `%s' is private");
  2181.       error ("in this context");
  2182.       return error_mark_node;
  2183.     }
  2184.       my_friendly_abort (55);
  2185.     }
  2186.  
  2187.   /* If this is a reference to a member function, then return
  2188.      the address of the member function (which may involve going
  2189.      through the object's vtable), otherwise, return an expression
  2190.      for the dereferenced pointer-to-member construct.  */
  2191.   addr = build_unary_op (ADDR_EXPR, base, 0);
  2192.  
  2193.   if (TREE_CODE (TREE_TYPE (member)) == METHOD_TYPE)
  2194.     {
  2195.       basetype = DECL_CLASS_CONTEXT (member);
  2196.       addr = convert_pointer_to (basetype, addr);
  2197.       return build_unary_op (ADDR_EXPR, get_member_function (&addr, build_indirect_ref (addr, 0), member), 0);
  2198.     }
  2199.   else if (TREE_CODE (TREE_TYPE (member)) == OFFSET_TYPE)
  2200.     {
  2201.       basetype = TYPE_OFFSET_BASETYPE (TREE_TYPE (member));
  2202.       addr = convert_pointer_to (basetype, addr);
  2203.       member = convert (ptr_type_node, build_unary_op (ADDR_EXPR, member, 0));
  2204.       return build1 (INDIRECT_REF, type,
  2205.              build (PLUS_EXPR, ptr_type_node, addr, member));
  2206.     }
  2207.   my_friendly_abort (56);
  2208.   /* NOTREACHED */
  2209.   return NULL_TREE;
  2210. }
  2211.  
  2212. /* Return either DECL or its known constant value (if it has one).  */
  2213.  
  2214. tree
  2215. decl_constant_value (decl)
  2216.      tree decl;
  2217. {
  2218.   if (! TREE_THIS_VOLATILE (decl)
  2219. #if 0
  2220.       /* These may be necessary for C, but they break C++.  */
  2221.       ! TREE_PUBLIC (decl)
  2222.       /* Don't change a variable array bound or initial value to a constant
  2223.      in a place where a variable is invalid.  */
  2224.       && ! pedantic
  2225. #endif /* 0 */
  2226.       && DECL_INITIAL (decl) != 0
  2227.       && TREE_CODE (DECL_INITIAL (decl)) != ERROR_MARK
  2228.       /* This is invalid if initial value is not constant.
  2229.      If it has either a function call, a memory reference,
  2230.      or a variable, then re-evaluating it could give different results.  */
  2231.       && TREE_CONSTANT (DECL_INITIAL (decl))
  2232.       /* Check for cases where this is sub-optimal, even though valid.  */
  2233.       && TREE_CODE (DECL_INITIAL (decl)) != CONSTRUCTOR
  2234. #if 0
  2235.       /* We must allow this to work outside of functions so that
  2236.      static constants can be used for array sizes.  */
  2237.       && current_function_decl != 0
  2238.       && DECL_MODE (decl) != BLKmode
  2239. #endif
  2240.       )
  2241.     return DECL_INITIAL (decl);
  2242.   return decl;
  2243. }
  2244.  
  2245. /* Friend handling routines.  */
  2246. /* Friend data structures:
  2247.  
  2248.    Friend lists come from TYPE_DECL nodes.  Since all aggregate
  2249.    types are automatically typedef'd, these node are guaranteed
  2250.    to exist.
  2251.  
  2252.    The TREE_PURPOSE of a friend list is the name of the friend,
  2253.    and its TREE_VALUE is another list.
  2254.  
  2255.    The TREE_PURPOSE of that list is a type, which allows
  2256.    all functions of a given type to be friends.
  2257.    The TREE_VALUE of that list is an individual function
  2258.    which is a friend.
  2259.  
  2260.    Non-member friends will match only by their DECL.  Their
  2261.    member type is NULL_TREE, while the type of the inner
  2262.    list will either be of aggregate type or error_mark_node.  */
  2263.  
  2264. /* Tell if this function specified by DECL
  2265.    can be a friend of type TYPE.
  2266.    Return nonzero if friend, zero otherwise.
  2267.  
  2268.    DECL can be zero if we are calling a constructor or accessing a
  2269.    member in global scope.  */
  2270. int
  2271. is_friend (type, decl)
  2272.      tree type, decl;
  2273. {
  2274.   tree typedecl = TYPE_NAME (type);
  2275.   tree ctype;
  2276.   tree list;
  2277.   tree name;
  2278.  
  2279.   if (decl == NULL_TREE)
  2280.     return 0;
  2281.  
  2282.   ctype = DECL_CLASS_CONTEXT (decl);
  2283.   if (ctype)
  2284.     {
  2285.       list = CLASSTYPE_FRIEND_CLASSES (TREE_TYPE (typedecl));
  2286.       while (list)
  2287.     {
  2288.       if (ctype == TREE_VALUE (list))
  2289.         return 1;
  2290.       list = TREE_CHAIN (list);
  2291.     }
  2292.     }
  2293.  
  2294.   list = DECL_FRIENDLIST (typedecl);
  2295.   name = DECL_NAME (decl);
  2296.   while (list)
  2297.     {
  2298.       if (name == TREE_PURPOSE (list))
  2299.     {
  2300.       tree friends = TREE_VALUE (list);
  2301.       name = DECL_ASSEMBLER_NAME (decl);
  2302.       while (friends)
  2303.         {
  2304.           if (ctype == TREE_PURPOSE (friends))
  2305.         return 1;
  2306.           if (name == DECL_ASSEMBLER_NAME (TREE_VALUE (friends)))
  2307.         return 1;
  2308.           friends = TREE_CHAIN (friends);
  2309.         }
  2310.       return 0;
  2311.     }
  2312.       list = TREE_CHAIN (list);
  2313.     }
  2314.   return 0;
  2315. }
  2316.  
  2317. /* Add a new friend to the friends of the aggregate type TYPE.
  2318.    DECL is the FUNCTION_DECL of the friend being added.  */
  2319. static void
  2320. add_friend (type, decl)
  2321.      tree type, decl;
  2322. {
  2323.   tree typedecl = TYPE_NAME (type);
  2324.   tree list = DECL_FRIENDLIST (typedecl);
  2325.   tree name = DECL_NAME (decl);
  2326.   tree ctype = TREE_CODE (TREE_TYPE (decl)) == METHOD_TYPE
  2327.     ? DECL_CLASS_CONTEXT (decl) : error_mark_node;
  2328.  
  2329.   while (list)
  2330.     {
  2331.       if (name == TREE_PURPOSE (list))
  2332.     {
  2333.       tree friends = TREE_VALUE (list);
  2334.       while (friends)
  2335.         {
  2336.           if (decl == TREE_VALUE (friends))
  2337.         {
  2338.           warning_with_decl (decl, "`%s' is already a friend of class `%s'", IDENTIFIER_POINTER (DECL_NAME (typedecl)));
  2339.           return;
  2340.         }
  2341.           friends = TREE_CHAIN (friends);
  2342.         }
  2343.       TREE_VALUE (list) = tree_cons (ctype, decl, TREE_VALUE (list));
  2344.       return;
  2345.     }
  2346.       list = TREE_CHAIN (list);
  2347.     }
  2348.   DECL_FRIENDLIST (typedecl)
  2349.     = tree_cons (DECL_NAME (decl), build_tree_list (error_mark_node, decl),
  2350.          DECL_FRIENDLIST (typedecl));
  2351.   if (DECL_NAME (decl) == ansi_opname[(int) MODIFY_EXPR])
  2352.     {
  2353.       tree parmtypes = TYPE_ARG_TYPES (TREE_TYPE (decl));
  2354.       TYPE_HAS_ASSIGNMENT (TREE_TYPE (typedecl)) = 1;
  2355.       TYPE_GETS_ASSIGNMENT (TREE_TYPE (typedecl)) = 1;
  2356.       if (parmtypes && TREE_CHAIN (parmtypes))
  2357.     {
  2358.       tree parmtype = TREE_VALUE (TREE_CHAIN (parmtypes));
  2359.       if (TREE_CODE (parmtype) == REFERENCE_TYPE
  2360.           && TREE_TYPE (parmtypes) == TREE_TYPE (typedecl))
  2361.         {
  2362.           TYPE_HAS_ASSIGN_REF (TREE_TYPE (typedecl)) = 1;
  2363.           TYPE_GETS_ASSIGN_REF (TREE_TYPE (typedecl)) = 1;
  2364.         }
  2365.     }
  2366.     }
  2367. }
  2368.  
  2369. /* Declare that every member function NAME in FRIEND_TYPE
  2370.    (which may be NULL_TREE) is a friend of type TYPE.  */
  2371. static void
  2372. add_friends (type, name, friend_type)
  2373.      tree type, name, friend_type;
  2374. {
  2375.   tree typedecl = TYPE_NAME (type);
  2376.   tree list = DECL_FRIENDLIST (typedecl);
  2377.  
  2378.   while (list)
  2379.     {
  2380.       if (name == TREE_PURPOSE (list))
  2381.     {
  2382.       tree friends = TREE_VALUE (list);
  2383.       while (friends && TREE_PURPOSE (friends) != friend_type)
  2384.         friends = TREE_CHAIN (friends);
  2385.       if (friends)
  2386.         if (friend_type)
  2387.           warning ("method `%s::%s' is already a friend of class",
  2388.                TYPE_NAME_STRING (friend_type),
  2389.                IDENTIFIER_POINTER (name));
  2390.         else
  2391.           warning ("function `%s' is already a friend of class `%s'",
  2392.                IDENTIFIER_POINTER (name),
  2393.                IDENTIFIER_POINTER (DECL_NAME (typedecl)));
  2394.       else
  2395.         TREE_VALUE (list) = tree_cons (friend_type, NULL_TREE,
  2396.                        TREE_VALUE (list));
  2397.       return;
  2398.     }
  2399.       list = TREE_CHAIN (list);
  2400.     }
  2401.   DECL_FRIENDLIST (typedecl) =
  2402.     tree_cons (name,
  2403.            build_tree_list (friend_type, NULL_TREE),
  2404.            DECL_FRIENDLIST (typedecl));
  2405.   if (! strncmp (IDENTIFIER_POINTER (name),
  2406.          IDENTIFIER_POINTER (ansi_opname[(int) MODIFY_EXPR]),
  2407.          strlen (IDENTIFIER_POINTER (ansi_opname[(int) MODIFY_EXPR]))))
  2408.     {
  2409.       TYPE_HAS_ASSIGNMENT (TREE_TYPE (typedecl)) = 1;
  2410.       TYPE_GETS_ASSIGNMENT (TREE_TYPE (typedecl)) = 1;
  2411.       sorry ("declaring \"friend operator =\" will not find \"operator = (X&)\" if it exists");
  2412.     }
  2413. }
  2414.  
  2415. /* Set up a cross reference so that type TYPE will
  2416.    make member function CTYPE::DECL a friend when CTYPE
  2417.    is finally defined.  */
  2418. void
  2419. xref_friend (type, decl, ctype)
  2420.      tree type, decl, ctype;
  2421. {
  2422.   tree typedecl = TYPE_NAME (type);
  2423.   tree friend_decl = TYPE_NAME (ctype);
  2424.   tree t = tree_cons (NULL_TREE, ctype, DECL_UNDEFINED_FRIENDS (typedecl));
  2425.  
  2426.   DECL_UNDEFINED_FRIENDS (typedecl) = t;
  2427.   SET_DECL_WAITING_FRIENDS (friend_decl, tree_cons (type, t, DECL_WAITING_FRIENDS (friend_decl)));
  2428.   TREE_TYPE (DECL_WAITING_FRIENDS (friend_decl)) = decl;
  2429. }
  2430.  
  2431. /* Set up a cross reference so that functions with name NAME and
  2432.    type CTYPE know that they are friends of TYPE.  */
  2433. void
  2434. xref_friends (type, name, ctype)
  2435.      tree type, name, ctype;
  2436. {
  2437.   tree typedecl = TYPE_NAME (type);
  2438.   tree friend_decl = TYPE_NAME (ctype);
  2439.   tree t = tree_cons (NULL_TREE, ctype,
  2440.               DECL_UNDEFINED_FRIENDS (typedecl));
  2441.  
  2442.   DECL_UNDEFINED_FRIENDS (typedecl) = t;
  2443.   SET_DECL_WAITING_FRIENDS (friend_decl, tree_cons (type, t, DECL_WAITING_FRIENDS (friend_decl)));
  2444.   TREE_TYPE (DECL_WAITING_FRIENDS (friend_decl)) = name;
  2445. }
  2446.  
  2447. /* Make FRIEND_TYPE a friend class to TYPE.  If FRIEND_TYPE has already
  2448.    been defined, we make all of its member functions friends of
  2449.    TYPE.  If not, we make it a pending friend, which can later be added
  2450.    when its definition is seen.  If a type is defined, then its TYPE_DECL's
  2451.    DECL_UNDEFINED_FRIENDS contains a (possibly empty) list of friend
  2452.    classes that are not defined.  If a type has not yet been defined,
  2453.    then the DECL_WAITING_FRIENDS contains a list of types
  2454.    waiting to make it their friend.  Note that these two can both
  2455.    be in use at the same time!  */
  2456. void
  2457. make_friend_class (type, friend_type)
  2458.      tree type, friend_type;
  2459. {
  2460.   tree classes;
  2461.  
  2462.   if (type == friend_type)
  2463.     {
  2464.       warning ("class `%s' is implicitly friends with itself",
  2465.            TYPE_NAME_STRING (type));
  2466.       return;
  2467.     }
  2468.  
  2469.   GNU_xref_hier (TYPE_NAME_STRING (type),
  2470.          TYPE_NAME_STRING (friend_type), 0, 0, 1);
  2471.  
  2472.   classes = CLASSTYPE_FRIEND_CLASSES (type);
  2473.   while (classes && TREE_VALUE (classes) != friend_type)
  2474.     classes = TREE_CHAIN (classes);
  2475.   if (classes)
  2476.     warning ("class `%s' is already friends with class `%s'",
  2477.          TYPE_NAME_STRING (TREE_VALUE (classes)), TYPE_NAME_STRING (type));
  2478.   else
  2479.     {
  2480.       CLASSTYPE_FRIEND_CLASSES (type)
  2481.     = tree_cons (NULL_TREE, friend_type, CLASSTYPE_FRIEND_CLASSES (type));
  2482.     }
  2483. }
  2484.  
  2485. /* Main friend processor.  This is large, and for modularity purposes,
  2486.    has been removed from grokdeclarator.  It returns `void_type_node'
  2487.    to indicate that something happened, though a FIELD_DECL is
  2488.    not returned.
  2489.  
  2490.    CTYPE is the class this friend belongs to.
  2491.  
  2492.    DECLARATOR is the name of the friend.
  2493.  
  2494.    DECL is the FUNCTION_DECL that the friend is.
  2495.  
  2496.    In case we are parsing a friend which is part of an inline
  2497.    definition, we will need to store PARM_DECL chain that comes
  2498.    with it into the DECL_ARGUMENTS slot of the FUNCTION_DECL.
  2499.  
  2500.    FLAGS is just used for `grokclassfn'.
  2501.  
  2502.    QUALS say what special qualifies should apply to the object
  2503.    pointed to by `this'.  */
  2504. tree
  2505. do_friend (ctype, declarator, decl, parmdecls, flags, quals)
  2506.      tree ctype, declarator, decl, parmdecls;
  2507.      enum overload_flags flags;
  2508.      tree quals;
  2509. {
  2510.   /* first, lets find out if what we are making a friend needs overloading */
  2511.   tree previous_decl;
  2512.   int was_c_linkage = 0;
  2513.   /* if we find something in scope, let see if it has extern "C" linkage */
  2514.   /* This code is pretty general and should be ripped out and reused
  2515.      as a separate function. */
  2516.   if (DECL_NAME (decl))
  2517.     {
  2518.       previous_decl=lookup_name (DECL_NAME (decl), 0);
  2519.       if (previous_decl && TREE_CODE (previous_decl) == TREE_LIST)
  2520.     {
  2521.       do
  2522.         {
  2523.           if (TREE_TYPE (TREE_VALUE (previous_decl)) == TREE_TYPE (decl))
  2524.         {
  2525.           previous_decl = TREE_VALUE (previous_decl);
  2526.           break;
  2527.         }
  2528.           previous_decl = TREE_CHAIN (previous_decl);
  2529.         }
  2530.       while (previous_decl);
  2531.     }
  2532.       if (previous_decl && TREE_CODE (previous_decl) == FUNCTION_DECL)
  2533.     if (TREE_TYPE (decl) == TREE_TYPE (previous_decl))
  2534.         if (DECL_LANGUAGE (previous_decl) == lang_c)
  2535.         {
  2536.           /* it did, so lets not overload this */
  2537.           was_c_linkage = 1;
  2538.         }
  2539.     }
  2540.       
  2541.   if (ctype)
  2542.     {
  2543.       tree cname = TYPE_NAME (ctype);
  2544.       if (TREE_CODE (cname) == TYPE_DECL)
  2545.     cname = DECL_NAME (cname);
  2546.  
  2547.       /* A method friend.  */
  2548.       if (TREE_CODE (decl) == FUNCTION_DECL)
  2549.     {
  2550.       if (flags == NO_SPECIAL && ctype && declarator == cname)
  2551.         DECL_CONSTRUCTOR_P (decl) = 1;
  2552.  
  2553.       /* This will set up DECL_ARGUMENTS for us.  */
  2554.       grokclassfn (ctype, cname, decl, flags, quals);
  2555.       if (TYPE_SIZE (ctype) != 0)
  2556.         check_classfn (ctype, cname, decl);
  2557.  
  2558.       if (TREE_TYPE (decl) != error_mark_node)
  2559.         {
  2560.           if (TYPE_SIZE (ctype))
  2561.         {
  2562.           /* We don't call pushdecl here yet, or ever on this
  2563.              actual FUNCTION_DECL.  We must preserve its TREE_CHAIN
  2564.              until the end.  */
  2565.           make_decl_rtl (decl, NULL_PTR, 1);
  2566.           add_friend (current_class_type, decl);
  2567.         }
  2568.           else
  2569.         xref_friend (current_class_type, decl, ctype);
  2570.           DECL_FRIEND_P (decl) = 1;
  2571.         }
  2572.     }
  2573.       else
  2574.     {
  2575.       /* Possibly a bunch of method friends.  */
  2576.  
  2577.       /* Get the class they belong to.  */
  2578.       tree ctype = IDENTIFIER_TYPE_VALUE (cname);
  2579.  
  2580.       /* This class is defined, use its methods now.  */
  2581.       if (TYPE_SIZE (ctype))
  2582.         {
  2583.           tree fields = lookup_fnfields (TYPE_BINFO (ctype), declarator, 0);
  2584.           if (fields)
  2585.         add_friends (current_class_type, declarator, ctype);
  2586.           else
  2587.         error ("method `%s' is not a member of class `%s'",
  2588.                IDENTIFIER_POINTER (declarator),
  2589.                IDENTIFIER_POINTER (cname));
  2590.         }
  2591.       else
  2592.         xref_friends (current_class_type, declarator, ctype);
  2593.       decl = void_type_node;
  2594.     }
  2595.     }
  2596.   /* never overload C functions */
  2597.   else if (TREE_CODE (decl) == FUNCTION_DECL
  2598.        && ((IDENTIFIER_LENGTH (declarator) == 4
  2599.         && IDENTIFIER_POINTER (declarator)[0] == 'm'
  2600.         && ! strcmp (IDENTIFIER_POINTER (declarator), "main"))
  2601.            || (IDENTIFIER_LENGTH (declarator) > 10
  2602.            && IDENTIFIER_POINTER (declarator)[0] == '_'
  2603.            && IDENTIFIER_POINTER (declarator)[1] == '_'
  2604.            && strncmp (IDENTIFIER_POINTER (declarator)+2,
  2605.                    "builtin_", 8) == 0)
  2606.            || was_c_linkage))
  2607.     {
  2608.       /* raw "main", and builtin functions never gets overloaded,
  2609.      but they can become friends.  */
  2610.       TREE_PUBLIC (decl) = 1;
  2611.       add_friend (current_class_type, decl);
  2612.       DECL_FRIEND_P (decl) = 1;
  2613.       if (IDENTIFIER_POINTER (declarator)[0] == '_')
  2614.     {
  2615.       if (! strcmp (IDENTIFIER_POINTER (declarator)+10, "new"))
  2616.         TREE_GETS_NEW (current_class_type) = 0;
  2617.       else if (! strcmp (IDENTIFIER_POINTER (declarator)+10, "delete"))
  2618.         TREE_GETS_DELETE (current_class_type) = 0;
  2619.     }
  2620.       decl = void_type_node;
  2621.     }
  2622.   /* A global friend.
  2623.      @@ or possibly a friend from a base class ?!?  */
  2624.   else if (TREE_CODE (decl) == FUNCTION_DECL)
  2625.     {
  2626.       /* Friends must all go through the overload machinery,
  2627.      even though they may not technically be overloaded.
  2628.  
  2629.      Note that because classes all wind up being top-level
  2630.      in their scope, their friend wind up in top-level scope as well.  */
  2631.       DECL_ASSEMBLER_NAME (decl)
  2632.     = build_decl_overload (declarator, TYPE_ARG_TYPES (TREE_TYPE (decl)),
  2633.                    TREE_CODE (TREE_TYPE (decl)) == METHOD_TYPE);
  2634.       DECL_ARGUMENTS (decl) = parmdecls;
  2635.  
  2636.       /* We can call pushdecl here, because the TREE_CHAIN of this
  2637.      FUNCTION_DECL is not needed for other purposes.  */
  2638.       decl = pushdecl_top_level (decl);
  2639.  
  2640.       make_decl_rtl (decl, NULL_PTR, 1);
  2641.       add_friend (current_class_type, decl);
  2642.  
  2643.       if (! TREE_OVERLOADED (declarator)
  2644.       && IDENTIFIER_GLOBAL_VALUE (declarator)
  2645.       && TREE_CODE (IDENTIFIER_GLOBAL_VALUE (declarator)) == FUNCTION_DECL)
  2646.     {
  2647.       error ("friend `%s' implicitly overloaded",
  2648.          IDENTIFIER_POINTER (declarator));
  2649.       error_with_decl (IDENTIFIER_GLOBAL_VALUE (declarator),
  2650.                "after declaration of non-overloaded `%s'");
  2651.     }
  2652.       DECL_FRIEND_P (decl) = 1;
  2653.       DECL_OVERLOADED (decl) = 1;
  2654.       TREE_OVERLOADED (declarator) = 1;
  2655.       decl = push_overloaded_decl (decl, 1);
  2656.     }
  2657.   else
  2658.     {
  2659.       /* @@ Should be able to ingest later definitions of this function
  2660.      before use.  */
  2661.       tree decl = IDENTIFIER_GLOBAL_VALUE (declarator);
  2662.       if (decl == NULL_TREE)
  2663.     {
  2664.       warning ("implicitly declaring `%s' as struct",
  2665.            IDENTIFIER_POINTER (declarator));
  2666.       decl = xref_tag (record_type_node, declarator, NULL_TREE);
  2667.       decl = TYPE_NAME (decl);
  2668.     }
  2669.  
  2670.       /* Allow abbreviated declarations of overloaded functions,
  2671.      but not if those functions are really class names.  */
  2672.       if (TREE_CODE (decl) == TREE_LIST && TREE_TYPE (TREE_PURPOSE (decl)))
  2673.     {
  2674.       warning ("`friend %s' archaic, use `friend class %s' instead",
  2675.            IDENTIFIER_POINTER (declarator),
  2676.            IDENTIFIER_POINTER (declarator));
  2677.       decl = TREE_TYPE (TREE_PURPOSE (decl));
  2678.     }
  2679.  
  2680.       if (TREE_CODE (decl) == TREE_LIST)
  2681.     add_friends (current_class_type, TREE_PURPOSE (decl), NULL_TREE);
  2682.       else
  2683.     make_friend_class (current_class_type, TREE_TYPE (decl));
  2684.       decl = void_type_node;
  2685.     }
  2686.   return decl;
  2687. }
  2688.  
  2689. /* TYPE has now been defined.  It may, however, have a number of things
  2690.    waiting make make it their friend.  We resolve these references
  2691.    here.  */
  2692. void
  2693. embrace_waiting_friends (type)
  2694.      tree type;
  2695. {
  2696.   tree decl = TYPE_NAME (type);
  2697.   tree waiters;
  2698.  
  2699.   if (TREE_CODE (decl) != TYPE_DECL)
  2700.     return;
  2701.  
  2702.   for (waiters = DECL_WAITING_FRIENDS (decl); waiters;
  2703.        waiters = TREE_CHAIN (waiters))
  2704.     {
  2705.       tree waiter = TREE_PURPOSE (waiters);
  2706.       tree waiter_prev = TREE_VALUE (waiters);
  2707.       tree decl = TREE_TYPE (waiters);
  2708.       tree name = decl ? (TREE_CODE (decl) == IDENTIFIER_NODE
  2709.               ? decl : DECL_NAME (decl)) : NULL_TREE;
  2710.       if (name)
  2711.     {
  2712.       /* @@ There may be work to be done since we have not verified
  2713.          @@ consistency between original and friend declarations
  2714.          @@ of the functions waiting to become friends.  */
  2715.       tree field = lookup_fnfields (TYPE_BINFO (type), name, 0);
  2716.       if (field)
  2717.         if (decl == name)
  2718.           add_friends (waiter, name, type);
  2719.         else
  2720.           add_friend (waiter, decl);
  2721.       else
  2722.         error_with_file_and_line (DECL_SOURCE_FILE (TYPE_NAME (waiter)),
  2723.                       DECL_SOURCE_LINE (TYPE_NAME (waiter)),
  2724.                       "no method `%s' defined in class `%s' to be friend",
  2725.                       IDENTIFIER_POINTER (DECL_NAME (TREE_TYPE (waiters))),
  2726.                       TYPE_NAME_STRING (type));
  2727.     }
  2728.       else
  2729.     make_friend_class (type, waiter);
  2730.  
  2731.       if (TREE_CHAIN (waiter_prev))
  2732.     TREE_CHAIN (waiter_prev) = TREE_CHAIN (TREE_CHAIN (waiter_prev));
  2733.       else
  2734.     DECL_UNDEFINED_FRIENDS (TYPE_NAME (waiter)) = NULL_TREE;
  2735.     }
  2736. }
  2737.  
  2738. /* Common subroutines of build_new and build_vec_delete.  */
  2739.  
  2740. /* Common interface for calling "builtin" functions that are not
  2741.    really builtin.  */
  2742.  
  2743. tree
  2744. build_builtin_call (type, node, arglist)
  2745.      tree type;
  2746.      tree node;
  2747.      tree arglist;
  2748. {
  2749.   tree rval = build (CALL_EXPR, type, node, arglist, 0);
  2750.   TREE_SIDE_EFFECTS (rval) = 1;
  2751.   assemble_external (TREE_OPERAND (node, 0));
  2752.   TREE_USED (TREE_OPERAND (node, 0)) = 1;
  2753.   return rval;
  2754. }
  2755.  
  2756. /* Generate a C++ "new" expression. DECL is either a TREE_LIST
  2757.    (which needs to go through some sort of groktypename) or it
  2758.    is the name of the class we are newing. INIT is an initialization value.
  2759.    It is either an EXPRLIST, an EXPR_NO_COMMAS, or something in braces.
  2760.    If INIT is void_type_node, it means do *not* call a constructor
  2761.    for this instance.
  2762.  
  2763.    For types with constructors, the data returned is initialized
  2764.    by the appropriate constructor.
  2765.  
  2766.    Whether the type has a constructor or not, if it has a pointer
  2767.    to a virtual function table, then that pointer is set up
  2768.    here.
  2769.  
  2770.    Unless I am mistaken, a call to new () will return initialized
  2771.    data regardless of whether the constructor itself is private or
  2772.    not.
  2773.  
  2774.    Note that build_new does nothing to assure that any special
  2775.    alignment requirements of the type are met.  Rather, it leaves
  2776.    it up to malloc to do the right thing.  Otherwise, folding to
  2777.    the right alignment cal cause problems if the user tries to later
  2778.    free the memory returned by `new'.
  2779.  
  2780.    PLACEMENT is the `placement' list for user-defined operator new ().  */
  2781.  
  2782. tree
  2783. build_new (placement, decl, init, use_global_new)
  2784.      tree placement;
  2785.      tree decl, init;
  2786.      int use_global_new;
  2787. {
  2788.   tree type, true_type, size, rval;
  2789.   tree init1 = NULL_TREE, nelts;
  2790.   int has_call = 0, has_array = 0;
  2791.  
  2792.   tree pending_sizes = NULL_TREE;
  2793.  
  2794.   if (decl == error_mark_node)
  2795.     return error_mark_node;
  2796.  
  2797.   if (TREE_CODE (decl) == TREE_LIST)
  2798.     {
  2799.       tree absdcl = TREE_VALUE (decl);
  2800.       tree last_absdcl = NULL_TREE;
  2801.       int old_immediate_size_expand;
  2802.  
  2803.       if (current_function_decl
  2804.       && DECL_CONSTRUCTOR_P (current_function_decl))
  2805.     {
  2806.       old_immediate_size_expand = immediate_size_expand;
  2807.       immediate_size_expand = 0;
  2808.     }
  2809.  
  2810.       nelts = integer_one_node;
  2811.  
  2812.       if (absdcl && TREE_CODE (absdcl) == CALL_EXPR)
  2813.     {
  2814.       /* probably meant to be a call */
  2815.       has_call = 1;
  2816.       init1 = TREE_OPERAND (absdcl, 1);
  2817.       absdcl = TREE_OPERAND (absdcl, 0);
  2818.       TREE_VALUE (decl) = absdcl;
  2819.     }
  2820.       while (absdcl && TREE_CODE (absdcl) == INDIRECT_REF)
  2821.     {
  2822.       last_absdcl = absdcl;
  2823.       absdcl = TREE_OPERAND (absdcl, 0);
  2824.     }
  2825.  
  2826.       if (absdcl && TREE_CODE (absdcl) == ARRAY_REF)
  2827.     {
  2828.       /* probably meant to be a vec new */
  2829.       tree this_nelts;
  2830.  
  2831.       has_array = 1;
  2832.       this_nelts = TREE_OPERAND (absdcl, 1);
  2833.       if (this_nelts != error_mark_node)
  2834.         {
  2835.           if (this_nelts == NULL_TREE)
  2836.         error ("new of array type fails to specify size");
  2837.           else
  2838.         {
  2839.           this_nelts = save_expr (this_nelts);
  2840.           absdcl = TREE_OPERAND (absdcl, 0);
  2841.               if (this_nelts == integer_zero_node)
  2842.             {
  2843.               warning ("zero size array reserves no space");
  2844.               nelts = integer_zero_node;
  2845.             }
  2846.           else
  2847.             nelts = build_binary_op (MULT_EXPR, nelts, this_nelts, 1);
  2848.         }
  2849.         }
  2850.       else
  2851.         nelts = integer_zero_node;
  2852.     }
  2853.  
  2854.       if (last_absdcl)
  2855.     TREE_OPERAND (last_absdcl, 0) = absdcl;
  2856.       else
  2857.     TREE_VALUE (decl) = absdcl;
  2858.  
  2859.       type = true_type = groktypename (decl);
  2860.       if (! type || type == error_mark_node
  2861.       || true_type == error_mark_node)
  2862.     return error_mark_node;
  2863.  
  2864.       /* ``A reference cannot be created by the new operator.  A reference
  2865.      is not an object (8.2.2, 8.4.3), so a pointer to it could not be
  2866.      returned by new.'' ARM 5.3.3 */
  2867.       if (TREE_CODE (type) == REFERENCE_TYPE)
  2868.     error ("new cannot be applied to a reference type");
  2869.  
  2870.       type = TYPE_MAIN_VARIANT (type);
  2871.       if (type == void_type_node)
  2872.     {
  2873.       error ("invalid type: `void []'");
  2874.       return error_mark_node;
  2875.     }
  2876.       if (current_function_decl
  2877.       && DECL_CONSTRUCTOR_P (current_function_decl))
  2878.     {
  2879.       pending_sizes = get_pending_sizes ();
  2880.       immediate_size_expand = old_immediate_size_expand;
  2881.     }
  2882.     }
  2883.   else if (TREE_CODE (decl) == IDENTIFIER_NODE)
  2884.     {
  2885.       if (IDENTIFIER_HAS_TYPE_VALUE (decl))
  2886.     {
  2887.       /* An aggregate type.  */
  2888.       type = IDENTIFIER_TYPE_VALUE (decl);
  2889.       decl = TYPE_NAME (type);
  2890.     }
  2891.       else
  2892.     {
  2893.       /* A builtin type.  */
  2894.       decl = lookup_name (decl, 1);
  2895.       my_friendly_assert (TREE_CODE (decl) == TYPE_DECL, 215);
  2896.       type = TREE_TYPE (decl);
  2897.     }
  2898.       true_type = type;
  2899.     }
  2900.   else if (TREE_CODE (decl) == TYPE_DECL)
  2901.     {
  2902.       type = TREE_TYPE (decl);
  2903.       true_type = type;
  2904.     }
  2905.   else
  2906.     {
  2907.       type = decl;
  2908.       true_type = type;
  2909.       decl = TYPE_NAME (type);
  2910.     }
  2911.  
  2912.   if (TYPE_SIZE (type) == 0)
  2913.     {
  2914.       if (type == void_type_node)
  2915.     error ("invalid type for new: `void'");
  2916.       else
  2917.     incomplete_type_error (0, type);
  2918.       return error_mark_node;
  2919.     }
  2920.  
  2921.   if (TYPE_LANG_SPECIFIC (type) && CLASSTYPE_ABSTRACT_VIRTUALS (type))
  2922.     {
  2923.       abstract_virtuals_error (NULL_TREE, type);
  2924.       return error_mark_node;
  2925.     }
  2926.  
  2927.   /* If our base type is an array, then make sure we know how many elements
  2928.      it has.  */
  2929.   while (TREE_CODE (type) == ARRAY_TYPE)
  2930.     {
  2931.       tree this_nelts = array_type_nelts_top (type);
  2932.       if (nelts == integer_one_node)
  2933.     {
  2934.       has_array = 1;
  2935.       nelts = this_nelts;
  2936.     }
  2937.       else
  2938.     {
  2939.       my_friendly_assert (has_array != 0, 216);
  2940.       nelts = build_binary_op (MULT_EXPR, nelts, this_nelts, 1);
  2941.     }
  2942.       type = TREE_TYPE (type);
  2943.     }
  2944.   if (has_array)
  2945.     size = fold (build_binary_op (MULT_EXPR, size_in_bytes (type), nelts, 1));
  2946.   else
  2947.     size = size_in_bytes (type);
  2948.  
  2949.   if (has_call)
  2950.     init = init1;
  2951.  
  2952.   /* Get to the target type of TRUE_TYPE, so we can decide whether
  2953.      any constructors need to be called or not.  */
  2954.   type = true_type;
  2955.   while (TREE_CODE (type) == ARRAY_TYPE)
  2956.     type = TREE_TYPE (type);
  2957.  
  2958.   /* Get a little extra space to store a couple of things before the new'ed
  2959.      array. */
  2960.   if (has_array && TYPE_NEEDS_DESTRUCTOR (true_type))
  2961.     {
  2962.       tree extra = BI_header_size;
  2963.  
  2964.       size = size_binop (PLUS_EXPR, size, extra);
  2965.     }
  2966.  
  2967.   /* Allocate the object. */
  2968.   if (TYPE_LANG_SPECIFIC (true_type)
  2969.       && (TREE_GETS_NEW (true_type) && !use_global_new))
  2970.     rval = build_opfncall (NEW_EXPR, LOOKUP_NORMAL,
  2971.                TYPE_POINTER_TO (true_type), size, placement);
  2972.   else if (placement)
  2973.     {
  2974.       rval = build_opfncall (NEW_EXPR, LOOKUP_GLOBAL|LOOKUP_COMPLAIN,
  2975.                  ptr_type_node, size, placement);
  2976.       rval = convert (TYPE_POINTER_TO (true_type), rval);
  2977.     }
  2978.   else if (flag_this_is_variable > 0
  2979.        && TYPE_HAS_CONSTRUCTOR (true_type) && init != void_type_node)
  2980.     {
  2981.       if (init == NULL_TREE || TREE_CODE (init) == TREE_LIST)
  2982.     rval = NULL_TREE;
  2983.       else
  2984.     {
  2985.       error ("constructors take parameter lists");
  2986.       return error_mark_node;
  2987.     }
  2988.     }
  2989.   else
  2990.     {
  2991.       rval = build_builtin_call (build_pointer_type (true_type),
  2992.                  BIN, build_tree_list (NULL_TREE, size));
  2993. #if 0
  2994.       /* See comment above as to why this is disabled.  */
  2995.       if (alignment)
  2996.     {
  2997.       rval = build (PLUS_EXPR, TYPE_POINTER_TO (true_type), rval,
  2998.             alignment);
  2999.       rval = build (BIT_AND_EXPR, TYPE_POINTER_TO (true_type),
  3000.             rval, build1 (BIT_NOT_EXPR, integer_type_node,
  3001.                       alignment));
  3002.     }
  3003. #endif
  3004.       TREE_CALLS_NEW (rval) = 1;
  3005.       TREE_SIDE_EFFECTS (rval) = 1;
  3006.     }
  3007.  
  3008.   /* if rval is NULL_TREE I don't have to allocate it, but are we totally
  3009.      sure we have some extra bytes in that case for the BI_header_size
  3010.      cookies? And how does that interact with the code below? (mrs) */
  3011.   /* Finish up some magic for new'ed arrays */
  3012.   if (has_array && TYPE_NEEDS_DESTRUCTOR (true_type) && rval != NULL_TREE)
  3013.     {
  3014.       tree extra = BI_header_size;
  3015.       tree cookie, exp1, exp2;
  3016.       rval = convert (ptr_type_node, rval);    /* convert to void * first */
  3017.       rval = convert (string_type_node, rval); /* lets not add void* and ints */
  3018.       rval = save_expr (build_binary_op (PLUS_EXPR, rval, extra, 1));
  3019.       /* Store header info.  */
  3020.       cookie = build_indirect_ref (build (MINUS_EXPR, TYPE_POINTER_TO (BI_header_type),
  3021.                       rval, extra), 0);
  3022.       exp1 = build (MODIFY_EXPR, void_type_node,
  3023.             build_component_ref (cookie, get_identifier ("nelts"), 0, 0),
  3024.             nelts);
  3025.       TREE_SIDE_EFFECTS (exp1) = 1;
  3026.       exp2 = build (MODIFY_EXPR, void_type_node,
  3027.             build_component_ref (cookie, get_identifier ("ptr_2comp"), 0, 0),
  3028.             build (MINUS_EXPR, ptr_type_node, integer_zero_node, rval));
  3029.       TREE_SIDE_EFFECTS (exp2) = 1;
  3030.       rval = convert (build_pointer_type (true_type), rval);
  3031.       TREE_CALLS_NEW (rval) = 1;
  3032.       TREE_SIDE_EFFECTS (rval) = 1;
  3033.       rval = build_compound_expr (tree_cons (NULL_TREE, exp1,
  3034.                          tree_cons (NULL_TREE, exp2,
  3035.                             build_tree_list (NULL_TREE, rval))));
  3036.     }
  3037.  
  3038.   /* We've figured out where the allocation is to go.
  3039.      If we're not eliding constructors, then if a constructor
  3040.      is defined, we must go through it.  */
  3041.   if (!has_array && (rval == NULL_TREE || !flag_elide_constructors)
  3042.       && TYPE_HAS_CONSTRUCTOR (true_type) && init != void_type_node)
  3043.     {
  3044.       tree newrval;
  3045.       /* Constructors are never virtual.  */
  3046.       int flags = LOOKUP_NORMAL|LOOKUP_NONVIRTUAL;
  3047.       /* If a copy constructor might work, set things up so that we can
  3048.      try that after this. */
  3049.       if (rval != NULL_TREE)
  3050.     flags = ~LOOKUP_COMPLAIN & (flags|LOOKUP_SPECULATIVELY);
  3051.       
  3052.       if (rval && TYPE_USES_VIRTUAL_BASECLASSES (true_type))
  3053.     {
  3054.       init = tree_cons (NULL_TREE, integer_one_node, init);
  3055.       flags |= LOOKUP_HAS_IN_CHARGE;
  3056.     }
  3057.       newrval = build_method_call (rval, constructor_name (true_type),
  3058.                 init, NULL_TREE, flags);
  3059.       if (newrval)
  3060.     {
  3061.       rval = newrval;
  3062.       TREE_HAS_CONSTRUCTOR (rval) = 1;
  3063.       goto done;
  3064.     }
  3065.       /* Didn't find the constructor, maybe it is a call to a copy constructor
  3066.      that we should implement. */
  3067.     }
  3068.  
  3069.   if (rval == error_mark_node)
  3070.     return error_mark_node;
  3071.   rval = save_expr (rval);
  3072.   TREE_HAS_CONSTRUCTOR (rval) = 1;
  3073.  
  3074.   /* Don't call any constructors or do any initialization.  */
  3075.   if (init == void_type_node)
  3076.     goto done;
  3077.  
  3078.   if (TYPE_NEEDS_CONSTRUCTING (type)
  3079.       || (has_call || init))
  3080.     {
  3081.       extern tree static_aggregates;
  3082.  
  3083.       if (current_function_decl == NULL_TREE)
  3084.     {
  3085.       /* In case of static initialization, SAVE_EXPR is good enough.  */
  3086.       init = copy_to_permanent (init);
  3087.       rval = copy_to_permanent (rval);
  3088.       static_aggregates = perm_tree_cons (init, rval, static_aggregates);
  3089.     }
  3090.       else
  3091.     {
  3092.       /* Have to wrap this in RTL_EXPR for two cases:
  3093.          in base or member initialization and if we
  3094.          are a branch of a ?: operator.  Since we
  3095.          can't easily know the latter, just do it always.  */
  3096.       tree xval = make_node (RTL_EXPR);
  3097.  
  3098.       TREE_TYPE (xval) = TREE_TYPE (rval);
  3099.       do_pending_stack_adjust ();
  3100.       start_sequence ();
  3101.  
  3102.       /* As a matter of principle, `start_sequence' should do this.  */
  3103.       emit_note (0, -1);
  3104.  
  3105.       if (has_array)
  3106.         rval = expand_vec_init (decl, rval,
  3107.                     build_binary_op (MINUS_EXPR, nelts, integer_one_node, 1),
  3108.                     init, 0);
  3109.       else
  3110.         expand_aggr_init (build_indirect_ref (rval, 0), init, 0);
  3111.  
  3112.       do_pending_stack_adjust ();
  3113.  
  3114.       TREE_SIDE_EFFECTS (xval) = 1;
  3115.       TREE_CALLS_NEW (xval) = 1;
  3116.       RTL_EXPR_SEQUENCE (xval) = get_insns ();
  3117.       end_sequence ();
  3118.  
  3119.       if (TREE_CODE (rval) == SAVE_EXPR)
  3120.         {
  3121.           /* Errors may cause this to not get evaluated.  */
  3122.           if (SAVE_EXPR_RTL (rval) == 0)
  3123.         SAVE_EXPR_RTL (rval) = const0_rtx;
  3124.           RTL_EXPR_RTL (xval) = SAVE_EXPR_RTL (rval);
  3125.         }
  3126.       else
  3127.         {
  3128.           my_friendly_assert (TREE_CODE (rval) == VAR_DECL, 217);
  3129.           RTL_EXPR_RTL (xval) = DECL_RTL (rval);
  3130.         }
  3131.       rval = xval;
  3132.     }
  3133.     }
  3134. #if 0
  3135.   /* It would seem that the above code handles this better than the code
  3136.      below. (mrs) */
  3137.   else if (has_call || init)
  3138.     {
  3139.       if (IS_AGGR_TYPE (type))
  3140.     {
  3141.       /*  default copy constructor may be missing from the below. (mrs) */
  3142.       error_with_aggr_type (type, "no constructor for type `%s'");
  3143.       rval = error_mark_node;
  3144.     }
  3145.       else
  3146.     {
  3147.       /* New 2.0 interpretation: `new int (10)' means
  3148.          allocate an int, and initialize it with 10.  */
  3149.  
  3150.       init = build_c_cast (type, init);
  3151.       rval = build (COMPOUND_EXPR, TREE_TYPE (rval),
  3152.             build_modify_expr (build_indirect_ref (rval, 0),
  3153.                        NOP_EXPR, init),
  3154.             rval);
  3155.       TREE_SIDE_EFFECTS (rval) = 1;
  3156.     }
  3157.     }
  3158. #endif
  3159.  done:
  3160.   if (pending_sizes)
  3161.     rval = build_compound_expr (chainon (pending_sizes,
  3162.                      build_tree_list (NULL_TREE, rval)));
  3163.  
  3164.   if (flag_gc)
  3165.     {
  3166.       extern tree gc_visible;
  3167.       tree objbits;
  3168.       tree update_expr;
  3169.  
  3170.       rval = save_expr (rval);
  3171.       /* We don't need a `headof' operation to do this because
  3172.      we know where the object starts.  */
  3173.       objbits = build1 (INDIRECT_REF, unsigned_type_node,
  3174.             build (MINUS_EXPR, ptr_type_node,
  3175.                    rval, c_sizeof_nowarn (unsigned_type_node)));
  3176.       update_expr = build_modify_expr (objbits, BIT_IOR_EXPR, gc_visible);
  3177.       rval = build_compound_expr (tree_cons (NULL_TREE, rval,
  3178.                          tree_cons (NULL_TREE, update_expr,
  3179.                             build_tree_list (NULL_TREE, rval))));
  3180.     }
  3181.  
  3182.   return save_expr (rval);
  3183. }
  3184.  
  3185. /* `expand_vec_init' performs initialization of a vector of aggregate
  3186.    types.
  3187.  
  3188.    DECL is passed only for error reporting, and provides line number
  3189.    and source file name information.
  3190.    BASE is the space where the vector will be.
  3191.    MAXINDEX is the maximum index of the array (one less than the
  3192.         number of elements).
  3193.    INIT is the (possibly NULL) initializer.
  3194.  
  3195.    FROM_ARRAY is 0 if we should init everything with INIT
  3196.    (i.e., every element initialized from INIT).
  3197.    FROM_ARRAY is 1 if we should index into INIT in parallel
  3198.    with initialization of DECL.
  3199.    FROM_ARRAY is 2 if we should index into INIT in parallel,
  3200.    but use assignment instead of initialization.  */
  3201.  
  3202. tree
  3203. expand_vec_init (decl, base, maxindex, init, from_array)
  3204.      tree decl, base, maxindex, init;
  3205.      int from_array;
  3206. {
  3207.   tree rval;
  3208.   tree iterator, base2 = NULL_TREE;
  3209.   tree type = TREE_TYPE (TREE_TYPE (base));
  3210.   tree size;
  3211.  
  3212.   maxindex = convert (integer_type_node, maxindex);
  3213.   if (maxindex == error_mark_node)
  3214.     return error_mark_node;
  3215.  
  3216.   if (current_function_decl == NULL_TREE)
  3217.     {
  3218.       rval = make_tree_vec (3);
  3219.       TREE_VEC_ELT (rval, 0) = base;
  3220.       TREE_VEC_ELT (rval, 1) = maxindex;
  3221.       TREE_VEC_ELT (rval, 2) = init;
  3222.       return rval;
  3223.     }
  3224.  
  3225.   size = size_in_bytes (type);
  3226.  
  3227.   /* Set to zero in case size is <= 0.  Optimizer will delete this if
  3228.      it is not needed.  */
  3229.   rval = get_temp_regvar (TYPE_POINTER_TO (type), convert (TYPE_POINTER_TO (type),
  3230.                                null_pointer_node));
  3231.   base = default_conversion (base);
  3232.   base = convert (TYPE_POINTER_TO (type), base);
  3233.   expand_assignment (rval, base, 0, 0);
  3234.   base = get_temp_regvar (TYPE_POINTER_TO (type), base);
  3235.  
  3236.   if (init != NULL_TREE
  3237.       && TREE_CODE (init) == CONSTRUCTOR
  3238.       && TREE_TYPE (init) == TREE_TYPE (decl))
  3239.     {
  3240.       /* Initialization of array from {...}.  */
  3241.       tree elts = CONSTRUCTOR_ELTS (init);
  3242.       tree baseref = build1 (INDIRECT_REF, type, base);
  3243.       tree baseinc = build (PLUS_EXPR, TYPE_POINTER_TO (type), base, size);
  3244.       int host_i = TREE_INT_CST_LOW (maxindex);
  3245.  
  3246.       if (IS_AGGR_TYPE (type))
  3247.     {
  3248.       while (elts)
  3249.         {
  3250.           host_i -= 1;
  3251.           expand_aggr_init (baseref, TREE_VALUE (elts), 0);
  3252.  
  3253.           expand_assignment (base, baseinc, 0, 0);
  3254.           elts = TREE_CHAIN (elts);
  3255.         }
  3256.       /* Initialize any elements by default if possible.  */
  3257.       if (host_i >= 0)
  3258.         {
  3259.           if (TYPE_NEEDS_CONSTRUCTING (type) == 0)
  3260.         {
  3261.           if (obey_regdecls)
  3262.             use_variable (DECL_RTL (base));
  3263.           goto done_init;
  3264.         }
  3265.  
  3266.           iterator = get_temp_regvar (integer_type_node,
  3267.                       build_int_2 (host_i, 0));
  3268.           init = NULL_TREE;
  3269.           goto init_by_default;
  3270.         }
  3271.     }
  3272.       else
  3273.     while (elts)
  3274.       {
  3275.         expand_assignment (baseref, TREE_VALUE (elts), 0, 0);
  3276.  
  3277.         expand_assignment (base, baseinc, 0, 0);
  3278.         elts = TREE_CHAIN (elts);
  3279.       }
  3280.  
  3281.       if (obey_regdecls)
  3282.     use_variable (DECL_RTL (base));
  3283.     }
  3284.   else
  3285.     {
  3286.       iterator = get_temp_regvar (integer_type_node, maxindex);
  3287.  
  3288.     init_by_default:
  3289.  
  3290.       /* If initializing one array from another,
  3291.      initialize element by element.  */
  3292.       if (from_array)
  3293.     {
  3294.       if (decl == NULL_TREE
  3295.           || (init && !comptypes (TREE_TYPE (init), TREE_TYPE (decl), 1)))
  3296.         {
  3297.           sorry ("initialization of array from dissimilar array type");
  3298.           return error_mark_node;
  3299.         }
  3300.       if (init)
  3301.         {
  3302.           base2 = default_conversion (init);
  3303.           base2 = get_temp_regvar (TYPE_POINTER_TO (type), base2);
  3304.         }
  3305.       else if (TYPE_LANG_SPECIFIC (type)
  3306.            && TYPE_NEEDS_CONSTRUCTING (type)
  3307.            && ! TYPE_HAS_DEFAULT_CONSTRUCTOR (type))
  3308.         {
  3309.           error ("initializer ends prematurely");
  3310.           return error_mark_node;
  3311.         }
  3312.     }
  3313.  
  3314.       expand_start_cond (build (GE_EXPR, integer_type_node,
  3315.                 iterator, integer_zero_node), 0);
  3316.       expand_start_loop_continue_elsewhere (1);
  3317.  
  3318.       if (from_array)
  3319.     {
  3320.       tree to = build1 (INDIRECT_REF, type, base);
  3321.       tree from;
  3322.  
  3323.       if (base2)
  3324.         from = build1 (INDIRECT_REF, type, base2);
  3325.       else
  3326.         from = NULL_TREE;
  3327.  
  3328.       if (from_array == 2)
  3329.         expand_expr_stmt (build_modify_expr (to, NOP_EXPR, from));
  3330.       else if (TYPE_NEEDS_CONSTRUCTING (type))
  3331.         expand_aggr_init (to, from, 0);
  3332.       else if (from)
  3333.         expand_assignment (to, from, 0, 0);
  3334.       else my_friendly_abort (57);
  3335.     }
  3336.       else if (TREE_CODE (type) == ARRAY_TYPE)
  3337.     {
  3338.       if (init != 0)
  3339.         sorry ("cannot initialize multi-dimensional array with initializer");
  3340.       expand_vec_init (decl, build1 (NOP_EXPR, TYPE_POINTER_TO (TREE_TYPE (type)), base),
  3341.                array_type_nelts (type), 0, 0);
  3342.     }
  3343.       else
  3344.     expand_aggr_init (build1 (INDIRECT_REF, type, base), init, 0);
  3345.  
  3346.       expand_assignment (base,
  3347.              build (PLUS_EXPR, TYPE_POINTER_TO (type), base, size),
  3348.              0, 0);
  3349.       if (base2)
  3350.     expand_assignment (base2,
  3351.                build (PLUS_EXPR, TYPE_POINTER_TO (type), base2, size), 0, 0);
  3352.       expand_loop_continue_here ();
  3353.       expand_exit_loop_if_false (0, build (NE_EXPR, integer_type_node,
  3354.                        build (PREDECREMENT_EXPR, integer_type_node, iterator, integer_one_node), minus_one));
  3355.  
  3356.       if (obey_regdecls)
  3357.     {
  3358.       use_variable (DECL_RTL (base));
  3359.       if (base2)
  3360.         use_variable (DECL_RTL (base2));
  3361.     }
  3362.       expand_end_loop ();
  3363.       expand_end_cond ();
  3364.       if (obey_regdecls)
  3365.     use_variable (DECL_RTL (iterator));
  3366.     }
  3367.  done_init:
  3368.  
  3369.   if (obey_regdecls)
  3370.     use_variable (DECL_RTL (rval));
  3371.   return rval;
  3372. }
  3373.  
  3374. /* Free up storage of type TYPE, at address ADDR.
  3375.  
  3376.    TYPE is a POINTER_TYPE and can be ptr_type_node for no special type
  3377.    of pointer.
  3378.  
  3379.    VIRTUAL_SIZE is the ammount of storage that was allocated, and is
  3380.    used as the second argument to operator delete.  It can include
  3381.    things like padding and magic size cookies.  It has virtual in it,
  3382.    because if you have a base pointer and you delete through a virtual
  3383.    destructor, it should be the size of the dynamic object, not the
  3384.    static object, see Free Store 12.5 ANSI C++ WP.
  3385.  
  3386.    This does not call any destructors.  */
  3387. tree
  3388. build_x_delete (type, addr, use_global_delete, virtual_size)
  3389.      tree type, addr;
  3390.      int use_global_delete;
  3391.      tree virtual_size;
  3392. {
  3393.   tree rval;
  3394.  
  3395.   if (!use_global_delete
  3396.       && TYPE_LANG_SPECIFIC (TREE_TYPE (type))
  3397.       && TREE_GETS_DELETE (TREE_TYPE (type)))
  3398.     rval = build_opfncall (DELETE_EXPR, LOOKUP_NORMAL, addr, virtual_size, NULL_TREE);
  3399.   else
  3400.     rval = build_builtin_call (void_type_node, BID,
  3401.                    tree_cons (NULL_TREE, addr,
  3402.                       build_tree_list (NULL_TREE,
  3403.                                virtual_size)));
  3404.   return rval;
  3405. }
  3406.  
  3407. /* Objects returned by `build_new' may point to just what the user
  3408.    requested (in the case of `new X'), or they may have a cookie
  3409.    consisting of a special value (the two's complement of the pointer
  3410.    address) and the number of elements allocated (in the case of
  3411.    `new X[N]'.  In the latter case, we need to adjust the pointer
  3412.    that's passed back to the storage allocator.  */
  3413.  
  3414. static tree
  3415. maybe_adjust_addr_for_delete (addr)
  3416.      tree addr;
  3417. {
  3418.   tree cookie_addr;
  3419.   tree cookie;
  3420.   tree adjusted_addr, ptr_2comp;
  3421.  
  3422.   if (TREE_SIDE_EFFECTS (addr))
  3423.     addr = save_expr (addr);
  3424.  
  3425.   cookie_addr = build (MINUS_EXPR, TYPE_POINTER_TO (BI_header_type),
  3426.                addr, BI_header_size);
  3427.   cookie = build_indirect_ref (cookie_addr, 0);
  3428.  
  3429.   ptr_2comp = build_component_ref (cookie, get_identifier ("ptr_2comp"), 0, 0);
  3430.   adjusted_addr = save_expr (build (MINUS_EXPR, TREE_TYPE (addr), addr, BI_header_size));
  3431.  
  3432.   /* We must zero out the storage here because if the memory is freed,
  3433.      then later reallocated, we might get a false positive when the
  3434.      address is reused.  */
  3435.   adjusted_addr = build_compound_expr (tree_cons (NULL_TREE,
  3436.                           build_modify_expr (ptr_2comp, NOP_EXPR, null_pointer_node),
  3437.                           build_tree_list (NULL_TREE, adjusted_addr)));
  3438.  
  3439.   addr = build (COND_EXPR, TREE_TYPE (addr),
  3440.         build (TRUTH_ORIF_EXPR, integer_type_node,
  3441.                build (EQ_EXPR, integer_type_node,
  3442.                   addr, integer_zero_node),
  3443.                build (PLUS_EXPR, integer_type_node,
  3444.                   convert (ptr_type_node, addr), ptr_2comp)),
  3445.         addr,
  3446.         adjusted_addr);
  3447.   return addr;
  3448. }
  3449.  
  3450. /* Generate a call to a destructor. TYPE is the type to cast ADDR to.
  3451.    ADDR is an expression which yields the store to be destroyed.
  3452.    AUTO_DELETE is nonzero if a call to DELETE should be made or not.
  3453.    If in the program, (AUTO_DELETE & 2) is non-zero, we tear down the
  3454.    virtual baseclasses.
  3455.    If in the program, (AUTO_DELETE & 1) is non-zero, then we deallocate.
  3456.  
  3457.    FLAGS is the logical disjunction of zero or more LOOKUP_
  3458.    flags.  See cp-tree.h for more info.
  3459.  
  3460.    MAYBE_ADJUST is nonzero iff we may need to adjust the address
  3461.    of the object being deleted before calling `operator delete'.
  3462.    This can happen when a user allocates an array with `operator new'
  3463.    and simply calls delete.  Ideally this is unnecessary, but there
  3464.    is much code that does `p = new char[n]; ... delete p;' and this code
  3465.    would crash otherwise.
  3466.  
  3467.    This function does not delete an object's virtual base classes.  */
  3468. tree
  3469. build_delete (type, addr, auto_delete, flags, use_global_delete, maybe_adjust)
  3470.      tree type, addr;
  3471.      tree auto_delete;
  3472.      int flags;
  3473.      int use_global_delete;
  3474.      int maybe_adjust;
  3475. {
  3476.   tree function, parms;
  3477.   tree member;
  3478.   tree expr;
  3479.   tree ref;
  3480.   int ptr;
  3481.  
  3482.   if (addr == error_mark_node)
  3483.     return error_mark_node;
  3484.  
  3485.   /* Can happen when CURRENT_EXCEPTION_OBJECT gets its type
  3486.      set to `error_mark_node' before it gets properly cleaned up.  */
  3487.   if (type == error_mark_node)
  3488.     return error_mark_node;
  3489.  
  3490.   type = TYPE_MAIN_VARIANT (type);
  3491.  
  3492.   if (TREE_CODE (type) == POINTER_TYPE)
  3493.     {
  3494.       type = TREE_TYPE (type);
  3495.       if (TYPE_SIZE (type) == 0)
  3496.     {
  3497.       incomplete_type_error (0, type);
  3498.       return error_mark_node;
  3499.     }
  3500.       if (TREE_CODE (type) == ARRAY_TYPE)
  3501.     goto handle_array;
  3502.       if (! IS_AGGR_TYPE (type))
  3503.     {
  3504.       tree virtual_size;
  3505.  
  3506.       /* This is probably wrong. It should be the size of the virtual
  3507.          object being deleted.  */
  3508.       virtual_size = c_sizeof_nowarn (type);
  3509.  
  3510.       if (maybe_adjust)
  3511.         addr = maybe_adjust_addr_for_delete (addr);
  3512.       return build_builtin_call (void_type_node, BID,
  3513.                      tree_cons (NULL_TREE, addr,
  3514.                         build_tree_list (NULL_TREE, virtual_size)));
  3515.     }
  3516.       if (TREE_SIDE_EFFECTS (addr))
  3517.     addr = save_expr (addr);
  3518.       ref = build_indirect_ref (addr, 0);
  3519.       ptr = 1;
  3520.     }
  3521.   else if (TREE_CODE (type) == ARRAY_TYPE)
  3522.     {
  3523.     handle_array:
  3524.       if (TREE_SIDE_EFFECTS (addr))
  3525.     addr = save_expr (addr);
  3526.       return build_vec_delete (addr, array_type_nelts (type),
  3527.                    c_sizeof_nowarn (TREE_TYPE (type)),
  3528.                    NULL_TREE, auto_delete, integer_two_node);
  3529.     }
  3530.   else
  3531.     {
  3532.       /* Don't check PROTECT here; leave that decision to the
  3533.      destructor.  If the destructor is visible, call it,
  3534.      else report error.  */
  3535.       addr = build_unary_op (ADDR_EXPR, addr, 0);
  3536.       if (TREE_SIDE_EFFECTS (addr))
  3537.     addr = save_expr (addr);
  3538.  
  3539.       if (TREE_CONSTANT (addr))
  3540.     addr = convert_pointer_to (type, addr);
  3541.       else
  3542.     addr = convert_force (build_pointer_type (type), addr);
  3543.  
  3544.       if (TREE_CODE (addr) == NOP_EXPR
  3545.       && TREE_OPERAND (addr, 0) == current_class_decl)
  3546.     ref = C_C_D;
  3547.       else
  3548.     ref = build_indirect_ref (addr, 0);
  3549.       ptr = 0;
  3550.     }
  3551.  
  3552.   my_friendly_assert (IS_AGGR_TYPE (type), 220);
  3553.  
  3554.   if (! TYPE_NEEDS_DESTRUCTOR (type))
  3555.     {
  3556.       tree virtual_size;
  3557.  
  3558.       /* This is probably wrong. It should be the size of the virtual object
  3559.      being deleted.  */
  3560.       virtual_size = c_sizeof_nowarn (type);
  3561.  
  3562.       if (auto_delete == integer_zero_node)
  3563.     return void_zero_node;
  3564.       if (maybe_adjust && addr != current_class_decl)
  3565.     addr = maybe_adjust_addr_for_delete (addr);
  3566.       if (TREE_GETS_DELETE (type) && !use_global_delete)
  3567.     return build_opfncall (DELETE_EXPR, LOOKUP_NORMAL, addr, virtual_size, NULL_TREE);
  3568.       return build_builtin_call (void_type_node, BID,
  3569.                  tree_cons (NULL_TREE, addr,
  3570.                         build_tree_list (NULL_TREE, virtual_size)));
  3571.     }
  3572.   parms = build_tree_list (NULL_TREE, addr);
  3573.  
  3574.   /* Below, we will reverse the order in which these calls are made.
  3575.      If we have a destructor, then that destructor will take care
  3576.      of the base classes; otherwise, we must do that here.  */
  3577.   if (TYPE_HAS_DESTRUCTOR (type))
  3578.     {
  3579.       tree dtor = DECL_MAIN_VARIANT (TREE_VEC_ELT (CLASSTYPE_METHOD_VEC (type), 0));
  3580.       tree basetypes = TYPE_BINFO (type);
  3581.  
  3582.       if (flags & LOOKUP_PROTECT)
  3583.     {
  3584.       enum visibility_type visibility = compute_visibility (basetypes, dtor);
  3585.  
  3586.       if (visibility == visibility_private)
  3587.         {
  3588.           if (flags & LOOKUP_COMPLAIN)
  3589.         error_with_aggr_type (type, "destructor for type `%s' is private in this scope");
  3590.           return error_mark_node;
  3591.         }
  3592.       else if (visibility == visibility_protected
  3593.            && (flags & LOOKUP_PROTECTED_OK) == 0)
  3594.         {
  3595.           if (flags & LOOKUP_COMPLAIN)
  3596.         error_with_aggr_type (type, "destructor for type `%s' is protected in this scope");
  3597.           return error_mark_node;
  3598.         }
  3599.     }
  3600.  
  3601.       /* Once we are in a destructor, try not going through
  3602.      the virtual function table to find the next destructor.  */
  3603.       if (DECL_VINDEX (dtor)
  3604.       && ! (flags & LOOKUP_NONVIRTUAL)
  3605.       && TREE_CODE (auto_delete) != PARM_DECL
  3606.       && (ptr == 1 || ! resolves_to_fixed_type_p (ref, 0)))
  3607.     {
  3608.       /* This destructor must be called via virtual function table.  */
  3609.       dtor = TREE_VEC_ELT (CLASSTYPE_METHOD_VEC (DECL_CONTEXT (dtor)), 0);
  3610.       expr = convert_pointer_to (DECL_CLASS_CONTEXT (dtor), TREE_VALUE (parms));
  3611.       if (expr != TREE_VALUE (parms))
  3612.         {
  3613.           expr = fold (expr);
  3614.           ref = build_indirect_ref (expr, 0);
  3615.           TREE_VALUE (parms) = expr;
  3616.         }
  3617.       function = build_vfn_ref (&TREE_VALUE (parms), ref, DECL_VINDEX (dtor));
  3618.       if (function == error_mark_node)
  3619.         return error_mark_node;
  3620.       TREE_TYPE (function) = build_pointer_type (TREE_TYPE (dtor));
  3621.       TREE_CHAIN (parms) = build_tree_list (NULL_TREE, auto_delete);
  3622.       expr = build_function_call (function, parms);
  3623.       if (ptr && (flags & LOOKUP_DESTRUCTOR) == 0)
  3624.         {
  3625.           /* Handle the case where a virtual destructor is
  3626.          being called on an item that is 0.
  3627.  
  3628.          @@ Does this really need to be done?  */
  3629.           tree ifexp = build_binary_op(NE_EXPR, addr, integer_zero_node,1);
  3630. #if 0
  3631.           if (TREE_CODE (ref) == VAR_DECL
  3632.           || TREE_CODE (ref) == COMPONENT_REF)
  3633.         warning ("losing in build_delete");
  3634. #endif
  3635.           expr = build (COND_EXPR, void_type_node,
  3636.                 ifexp, expr, void_zero_node);
  3637.         }
  3638.     }
  3639.       else
  3640.     {
  3641.       tree ifexp;
  3642.  
  3643.       if ((flags & LOOKUP_DESTRUCTOR)
  3644.           || TREE_CODE (ref) == VAR_DECL
  3645.           || TREE_CODE (ref) == PARM_DECL
  3646.           || TREE_CODE (ref) == COMPONENT_REF
  3647.           || TREE_CODE (ref) == ARRAY_REF)
  3648.         /* These can't be 0.  */
  3649.         ifexp = integer_one_node;
  3650.       else
  3651.         /* Handle the case where a non-virtual destructor is
  3652.            being called on an item that is 0.  */
  3653.         ifexp = build_binary_op (NE_EXPR, addr, integer_zero_node, 1);
  3654.  
  3655.       /* Used to mean that this destructor was known to be empty,
  3656.          but that's now obsolete.  */
  3657.       my_friendly_assert (DECL_INITIAL (dtor) != void_type_node, 221);
  3658.  
  3659.       TREE_CHAIN (parms) = build_tree_list (NULL_TREE, auto_delete);
  3660.       expr = build_function_call (dtor, parms);
  3661.  
  3662.       if (ifexp != integer_one_node)
  3663.         expr = build (COND_EXPR, void_type_node,
  3664.               ifexp, expr, void_zero_node);
  3665.     }
  3666.       return expr;
  3667.     }
  3668.   else
  3669.     {
  3670.       /* This can get visibilities wrong.  */
  3671.       tree binfos = BINFO_BASETYPES (TYPE_BINFO (type));
  3672.       int i, n_baseclasses = binfos ? TREE_VEC_LENGTH (binfos) : 0;
  3673.       tree base_binfo = n_baseclasses > 0 ? TREE_VEC_ELT (binfos, 0) : NULL_TREE;
  3674.       tree exprstmt = NULL_TREE;
  3675.       tree parent_auto_delete = auto_delete;
  3676.       tree cond;
  3677.  
  3678.       /* If this type does not have a destructor, but does have
  3679.      operator delete, call the parent parent destructor (if any),
  3680.      but let this node do the deleting.  Otherwise, it is ok
  3681.      to let the parent destructor do the deleting.  */
  3682.       if (TREE_GETS_DELETE (type) && !use_global_delete)
  3683.     {
  3684.       parent_auto_delete = integer_zero_node;
  3685.       if (auto_delete == integer_zero_node)
  3686.         cond = NULL_TREE;
  3687.       else
  3688.         {
  3689.           tree virtual_size;
  3690.  
  3691.             /* This is probably wrong. It should be the size of the
  3692.            virtual object being deleted.  */
  3693.           virtual_size = c_sizeof_nowarn (type);
  3694.  
  3695.           expr = build_opfncall (DELETE_EXPR, LOOKUP_NORMAL, addr,
  3696.                      virtual_size, NULL_TREE);
  3697.           if (expr == error_mark_node)
  3698.         return error_mark_node;
  3699.           if (auto_delete != integer_one_node)
  3700.         cond = build (COND_EXPR, void_type_node,
  3701.                   build (BIT_AND_EXPR, integer_type_node,
  3702.                      auto_delete, integer_one_node),
  3703.                   expr, void_zero_node);
  3704.           else cond = expr;
  3705.         }
  3706.     }
  3707.       else if (base_binfo == NULL_TREE
  3708.            || (TREE_VIA_VIRTUAL (base_binfo) == 0
  3709.            && ! TYPE_NEEDS_DESTRUCTOR (BINFO_TYPE (base_binfo))))
  3710.     {
  3711.       tree virtual_size;
  3712.  
  3713.       /* This is probably wrong. It should be the size of the virtual
  3714.          object being deleted.  */
  3715.       virtual_size = c_sizeof_nowarn (type);
  3716.  
  3717.       cond = build (COND_EXPR, void_type_node,
  3718.             build (BIT_AND_EXPR, integer_type_node, auto_delete, integer_one_node),
  3719.             build_builtin_call (void_type_node, BID,
  3720.                         tree_cons (NULL_TREE, addr,
  3721.                                build_tree_list (NULL_TREE, virtual_size))),
  3722.             void_zero_node);
  3723.     }
  3724.       else cond = NULL_TREE;
  3725.  
  3726.       if (cond)
  3727.     exprstmt = build_tree_list (NULL_TREE, cond);
  3728.  
  3729.       if (base_binfo
  3730.       && ! TREE_VIA_VIRTUAL (base_binfo)
  3731.       && TYPE_NEEDS_DESTRUCTOR (BINFO_TYPE (base_binfo)))
  3732.     {
  3733.       tree this_auto_delete;
  3734.  
  3735.       if (BINFO_OFFSET_ZEROP (base_binfo))
  3736.         this_auto_delete = parent_auto_delete;
  3737.       else
  3738.         this_auto_delete = integer_zero_node;
  3739.  
  3740.       expr = build_delete (TYPE_POINTER_TO (BINFO_TYPE (base_binfo)), addr,
  3741.                    this_auto_delete, flags|LOOKUP_PROTECTED_OK, 0, 0);
  3742.       exprstmt = tree_cons (NULL_TREE, expr, exprstmt);
  3743.     }
  3744.  
  3745.       /* Take care of the remaining baseclasses.  */
  3746.       for (i = 1; i < n_baseclasses; i++)
  3747.     {
  3748.       base_binfo = TREE_VEC_ELT (binfos, i);
  3749.       if (! TYPE_NEEDS_DESTRUCTOR (BINFO_TYPE (base_binfo))
  3750.           || TREE_VIA_VIRTUAL (base_binfo))
  3751.         continue;
  3752.  
  3753.       /* May be zero offset if other baseclasses are virtual.  */
  3754.       expr = fold (build (PLUS_EXPR, TYPE_POINTER_TO (BINFO_TYPE (base_binfo)),
  3755.                   addr, BINFO_OFFSET (base_binfo)));
  3756.  
  3757.       expr = build_delete (TYPE_POINTER_TO (BINFO_TYPE (base_binfo)), expr,
  3758.                    integer_zero_node,
  3759.                    flags|LOOKUP_PROTECTED_OK, 0, 0);
  3760.  
  3761.       exprstmt = tree_cons (NULL_TREE, expr, exprstmt);
  3762.     }
  3763.  
  3764.       for (member = TYPE_FIELDS (type); member; member = TREE_CHAIN (member))
  3765.     {
  3766.       if (TREE_CODE (member) != FIELD_DECL)
  3767.         continue;
  3768.       if (TYPE_NEEDS_DESTRUCTOR (TREE_TYPE (member)))
  3769.         {
  3770.           tree this_member = build_component_ref (ref, DECL_NAME (member), 0, 0);
  3771.           tree this_type = TREE_TYPE (member);
  3772.           expr = build_delete (this_type, this_member, integer_two_node, flags, 0, 0);
  3773.           exprstmt = tree_cons (NULL_TREE, expr, exprstmt);
  3774.         }
  3775.     }
  3776.  
  3777.       if (exprstmt)
  3778.     return build_compound_expr (exprstmt);
  3779.       /* Virtual base classes make this function do nothing.  */
  3780.       return void_zero_node;
  3781.     }
  3782. }
  3783.  
  3784. /* For type TYPE, delete the virtual baseclass objects of DECL.  */
  3785.  
  3786. tree
  3787. build_vbase_delete (type, decl)
  3788.      tree type, decl;
  3789. {
  3790.   tree vbases = CLASSTYPE_VBASECLASSES (type);
  3791.   tree result = NULL_TREE;
  3792.   tree addr = build_unary_op (ADDR_EXPR, decl, 0);
  3793.   my_friendly_assert (addr != error_mark_node, 222);
  3794.   while (vbases)
  3795.     {
  3796.       tree this_addr = convert_force (TYPE_POINTER_TO (BINFO_TYPE (vbases)), addr);
  3797.       result = tree_cons (NULL_TREE,
  3798.               build_delete (TREE_TYPE (this_addr), this_addr,
  3799.                     integer_zero_node,
  3800.                     LOOKUP_NORMAL|LOOKUP_DESTRUCTOR, 0, 0),
  3801.               result);
  3802.       vbases = TREE_CHAIN (vbases);
  3803.     }
  3804.   return build_compound_expr (nreverse (result));
  3805. }
  3806.  
  3807. /* Build a C++ vector delete expression.
  3808.    MAXINDEX is the number of elements to be deleted.
  3809.    ELT_SIZE is the nominal size of each element in the vector.
  3810.    BASE is the expression that should yield the store to be deleted.
  3811.    DTOR_DUMMY is a placeholder for a destructor.  The library function
  3812.    __builtin_vec_delete has a pointer to function in this position.
  3813.    This function expands (or synthesizes) these calls itself.
  3814.    AUTO_DELETE_VEC says whether the container (vector) should be deallocated.
  3815.    AUTO_DELETE say whether each item in the container should be deallocated.
  3816.  
  3817.    This also calls delete for virtual baseclasses of elements of the vector.
  3818.  
  3819.    Update: MAXINDEX is no longer needed.  The size can be extracted from the
  3820.    start of the vector for pointers, and from the type for arrays.  We still
  3821.    use MAXINDEX for arrays because it happens to already have one of the
  3822.    values we'd have to extract.  (We could use MAXINDEX with pointers to
  3823.    confirm the size, and trap if the numbers differ; not clear that it'd
  3824.    be worth bothering.)  */
  3825. tree
  3826. build_vec_delete (base, maxindex, elt_size, dtor_dummy, auto_delete_vec, auto_delete)
  3827.      tree base, maxindex, elt_size;
  3828.      tree dtor_dummy;
  3829.      tree auto_delete_vec, auto_delete;
  3830. {
  3831.   tree ptype = TREE_TYPE (base);
  3832.   tree type;
  3833.   tree virtual_size;
  3834.   /* Temporary variables used by the loop.  */
  3835.   tree tbase, size_exp, tbase_init;
  3836.  
  3837.   /* This is the body of the loop that implements the deletion of a
  3838.      single element, and moves temp variables to next elements.  */
  3839.   tree body;
  3840.  
  3841.   /* This is the LOOP_EXPR that governs the deletion of the elements.  */
  3842.   tree loop;
  3843.  
  3844.   /* This is the thing that governs what to do after the loop has run.  */
  3845.   tree deallocate_expr = 0;
  3846.  
  3847.   /* This is the BIND_EXPR which holds the outermost iterator of the
  3848.      loop.  It is convenient to set this variable up and test it before
  3849.      executing any other code in the loop.
  3850.      This is also the containing expression returned by this function.  */
  3851.   tree controller = NULL_TREE;
  3852.  
  3853.   /* This is the BLOCK to record the symbol binding for debugging.  */
  3854.   tree block;
  3855.  
  3856.   base = stabilize_reference (base);
  3857.  
  3858.   /* Since we can use base many times, save_epr it. */
  3859.   if (TREE_SIDE_EFFECTS (base))
  3860.     base = save_expr (base);
  3861.  
  3862.   if (TREE_CODE (ptype) == POINTER_TYPE)
  3863.     {
  3864.       /* Step back one from start of vector, and read dimension.  */
  3865.       tree cookie_addr = build (MINUS_EXPR, TYPE_POINTER_TO (BI_header_type),
  3866.                 base, BI_header_size);
  3867.       tree cookie = build_indirect_ref (cookie_addr, 0);
  3868.       maxindex = build_component_ref (cookie, get_identifier ("nelts"), 0, 0);
  3869.       do
  3870.     ptype = TREE_TYPE (ptype);
  3871.       while (TREE_CODE (ptype) == ARRAY_TYPE);
  3872.     }
  3873.   else if (TREE_CODE (ptype) == ARRAY_TYPE)
  3874.     {
  3875.       /* get the total number of things in the array, maxindex is a bad name */
  3876.       maxindex = array_type_nelts_total (ptype);
  3877.       while (TREE_CODE (ptype) == ARRAY_TYPE)
  3878.     ptype = TREE_TYPE (ptype);
  3879.       base = build_unary_op (ADDR_EXPR, base, 1);
  3880.     }
  3881.   else
  3882.     {
  3883.       error ("type to vector delete is neither pointer or array type");
  3884.       return error_mark_node;
  3885.     }
  3886.   type = ptype;
  3887.   ptype = TYPE_POINTER_TO (type);
  3888.  
  3889.   size_exp = size_in_bytes (type);
  3890.  
  3891.   if (! IS_AGGR_TYPE (type) || ! TYPE_NEEDS_DESTRUCTOR (type))
  3892.     {
  3893.       loop = integer_zero_node;
  3894.       goto no_destructor;
  3895.     }
  3896.  
  3897.   /* The below is short by BI_header_size */
  3898.   virtual_size = fold (size_binop (MULT_EXPR, size_exp, maxindex));
  3899.  
  3900.   tbase = build_decl (VAR_DECL, NULL_TREE, ptype);
  3901.   tbase_init = build_modify_expr (tbase, NOP_EXPR,
  3902.                   fold (build (PLUS_EXPR, ptype,
  3903.                            base,
  3904.                            virtual_size)));
  3905.   DECL_REGISTER (tbase) = 1;
  3906.   controller = build (BIND_EXPR, void_type_node, tbase, 0, 0);
  3907.   TREE_SIDE_EFFECTS (controller) = 1;
  3908.   block = build_block (tbase, 0, 0, 0, 0);
  3909.   add_block_current_level (block);
  3910.  
  3911.   if (auto_delete != integer_zero_node
  3912.       && auto_delete != integer_two_node)
  3913.     {
  3914.       tree base_tbd = convert (ptype,
  3915.                    build_binary_op (MINUS_EXPR,
  3916.                         convert (ptr_type_node, base),
  3917.                         BI_header_size,
  3918.                         1));
  3919.       /* This is the real size */
  3920.       virtual_size = size_binop (PLUS_EXPR, virtual_size, BI_header_size);
  3921.       body = build_tree_list (NULL_TREE,
  3922.                   build_x_delete (ptr_type_node, base_tbd, 0,
  3923.                           virtual_size));
  3924.       body = build (COND_EXPR, void_type_node,
  3925.             build (BIT_AND_EXPR, integer_type_node,
  3926.                auto_delete, integer_one_node),
  3927.             body, integer_zero_node);
  3928.     }
  3929.   else
  3930.     body = NULL_TREE;
  3931.  
  3932.   body = tree_cons (NULL_TREE,
  3933.             build_delete (ptype, tbase, auto_delete,
  3934.                   LOOKUP_NORMAL|LOOKUP_DESTRUCTOR, 0, 0),
  3935.             body);
  3936.  
  3937.   body = tree_cons (NULL_TREE,
  3938.             build_modify_expr (tbase, NOP_EXPR, build (MINUS_EXPR, ptype, tbase, size_exp)),
  3939.             body);
  3940.  
  3941.   body = tree_cons (NULL_TREE,
  3942.             build (EXIT_EXPR, void_type_node,
  3943.                build (EQ_EXPR, integer_type_node, base, tbase)),
  3944.             body);
  3945.  
  3946.   loop = build (LOOP_EXPR, void_type_node, build_compound_expr (body));
  3947.  
  3948.   loop = tree_cons (NULL_TREE, tbase_init,
  3949.             tree_cons (NULL_TREE, loop, NULL_TREE));
  3950.   loop = build_compound_expr (loop);
  3951.  
  3952.  no_destructor:
  3953.   /* If the delete flag is one, or anything else with the low bit set,
  3954.      delete the storage.  */
  3955.   if (auto_delete_vec == integer_zero_node
  3956.       || auto_delete_vec == integer_two_node)
  3957.     deallocate_expr = integer_zero_node;
  3958.   else
  3959.     {
  3960.       tree base_tbd;
  3961.  
  3962.       /* The below is short by BI_header_size */
  3963.       virtual_size = fold (size_binop (MULT_EXPR, size_exp, maxindex));
  3964.  
  3965.       if (loop == integer_zero_node)
  3966.     /* no header */
  3967.     base_tbd = base;
  3968.       else
  3969.     {
  3970.       base_tbd = convert (ptype,
  3971.                   build_binary_op (MINUS_EXPR,
  3972.                            convert (string_type_node, base),
  3973.                            BI_header_size,
  3974.                            1));
  3975.       /* True size with header. */
  3976.       virtual_size = size_binop (PLUS_EXPR, virtual_size, BI_header_size);
  3977.     }
  3978.       deallocate_expr = build_x_delete (ptr_type_node, base_tbd, 1,
  3979.                     virtual_size);
  3980.       if (auto_delete_vec != integer_one_node)
  3981.     deallocate_expr = build (COND_EXPR, void_type_node,
  3982.                  build (BIT_AND_EXPR, integer_type_node,
  3983.                     auto_delete_vec, integer_one_node),
  3984.                  deallocate_expr, integer_zero_node);
  3985.     }
  3986.  
  3987.   if (loop && deallocate_expr != integer_zero_node)
  3988.     {
  3989.       body = tree_cons (NULL_TREE, loop,
  3990.             tree_cons (NULL_TREE, deallocate_expr, NULL_TREE));
  3991.       body = build_compound_expr (body);
  3992.     }
  3993.   else
  3994.     body = loop;
  3995.  
  3996.   /* Outermost wrapper: If pointer is null, punt.  */
  3997.   body = build (COND_EXPR, void_type_node,
  3998.         build (NE_EXPR, integer_type_node, base, integer_zero_node),
  3999.         body, integer_zero_node);
  4000.  
  4001.   if (controller)
  4002.     {
  4003.       TREE_OPERAND (controller, 1) = body;
  4004.       return controller;
  4005.     }
  4006.   else
  4007.     return convert (void_type_node, body);
  4008. }
  4009.