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

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