home *** CD-ROM | disk | FTP | other *** search
/ Fresh Fish 4 / FreshFish_May-June1994.bin / bbs / gnu / gcc-2.5.8-src.lha / src / amiga / gcc-2.5.8 / cp-class.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-11-25  |  140.6 KB  |  4,599 lines

  1. /* Functions related to building classes and their related objects.
  2.    Copyright (C) 1987, 1992, 1993 Free Software Foundation, Inc.
  3.    Contributed by Michael Tiemann (tiemann@cygnus.com)
  4.  
  5. This file is part of GNU CC.
  6.  
  7. GNU CC is free software; you can redistribute it and/or modify
  8. it under the terms of the GNU General Public License as published by
  9. the Free Software Foundation; either version 2, or (at your option)
  10. any later version.
  11.  
  12. GNU CC is distributed in the hope that it will be useful,
  13. but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  15. GNU General Public License for more details.
  16.  
  17. You should have received a copy of the GNU General Public License
  18. along with GNU CC; see the file COPYING.  If not, write to
  19. the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.  */
  20.  
  21.  
  22. /* High-level class interface. */
  23.  
  24. #include "config.h"
  25. #include "tree.h"
  26. #include <stdio.h>
  27. #include "cp-tree.h"
  28. #include "flags.h"
  29.  
  30. #include "obstack.h"
  31. #define obstack_chunk_alloc xmalloc
  32. #define obstack_chunk_free free
  33.  
  34. extern struct obstack permanent_obstack;
  35.  
  36. extern void set_class_shadows PROTO ((tree));
  37.  
  38. /* Way of stacking class types.  */
  39. static tree *current_class_base, *current_class_stack;
  40. static int current_class_stacksize;
  41. int current_class_depth;
  42.  
  43. struct class_level
  44. {
  45.   /* The previous class level.  */
  46.   struct class_level *level_chain;
  47.  
  48.   /* The class instance variable, as a PARM_DECL.  */
  49.   tree decl;
  50.   /* The class instance variable, as an object.  */
  51.   tree object;
  52.   /* The virtual function table pointer
  53.      for the class instance variable.  */
  54.   tree vtable_decl;
  55.  
  56.   /* Name of the current class.  */
  57.   tree name;
  58.   /* Type of the current class.  */
  59.   tree type;
  60.  
  61.   /* Flags for this class level.  */
  62.   int this_is_variable;
  63.   int memoized_lookups;
  64.   int save_memoized;
  65.   int unused;
  66. };
  67.  
  68. tree current_class_decl, C_C_D;    /* PARM_DECL: the class instance variable */
  69. tree current_vtable_decl;
  70.  
  71. /* The following two can be derived from the previous one */
  72. tree current_class_name;    /* IDENTIFIER_NODE: name of current class */
  73. tree current_class_type;    /* _TYPE: the type of the current class */
  74. tree previous_class_type;    /* _TYPE: the previous type that was a class */
  75. tree previous_class_values;        /* TREE_LIST: copy of the class_shadowed list
  76.                    when leaving an outermost class scope.  */
  77. static tree get_vfield_name PROTO((tree));
  78. tree the_null_vtable_entry;
  79.  
  80. /* Way of stacking language names.  */
  81. tree *current_lang_base, *current_lang_stack;
  82. static int current_lang_stacksize;
  83.  
  84. /* Names of languages we recognize.  */
  85. tree lang_name_c, lang_name_cplusplus;
  86. tree current_lang_name;
  87.  
  88. /* When layout out an aggregate type, the size of the
  89.    basetypes (virtual and non-virtual) is passed to layout_record
  90.    via this node.  */
  91. static tree base_layout_decl;
  92.  
  93. /* Variables shared between cp-class.c and cp-call.c.  */
  94.  
  95. int n_vtables = 0;
  96. int n_vtable_entries = 0;
  97. int n_vtable_searches = 0;
  98. int n_vtable_elems = 0;
  99. int n_convert_harshness = 0;
  100. int n_compute_conversion_costs = 0;
  101. int n_build_method_call = 0;
  102. int n_inner_fields_searched = 0;
  103.  
  104. /* Virtual baseclass things.  */
  105. tree
  106. build_vbase_pointer (exp, type)
  107.      tree exp, type;
  108. {
  109.   char *name;
  110.  
  111.   name = (char *) alloca (TYPE_NAME_LENGTH (type) + sizeof (VBASE_NAME) + 1);
  112.   sprintf (name, VBASE_NAME_FORMAT, TYPE_NAME_STRING (type));
  113.   return build_component_ref (exp, get_identifier (name), 0, 0);
  114. }
  115.  
  116. /* Build multi-level access to EXPR using hierarchy path PATH.
  117.    CODE is PLUS_EXPR if we are going with the grain,
  118.    and MINUS_EXPR if we are not (in which case, we cannot traverse
  119.    virtual baseclass links).
  120.  
  121.    TYPE is the type we want this path to have on exit.
  122.  
  123.    ALIAS_THIS is non-zero if EXPR in an expression involving `this'.  */
  124. tree
  125. build_vbase_path (code, type, expr, path, alias_this)
  126.      enum tree_code code;
  127.      tree type, expr, path;
  128.      int alias_this;
  129. {
  130.   register int changed = 0;
  131.   tree last = NULL_TREE, last_virtual = NULL_TREE;
  132.   int nonnull = 0;
  133.   int fixed_type_p = resolves_to_fixed_type_p (expr, &nonnull);
  134.   tree null_expr = 0, nonnull_expr;
  135.   tree basetype;
  136.   tree offset = integer_zero_node;
  137.  
  138.   if (!fixed_type_p && TREE_SIDE_EFFECTS (expr))
  139.     expr = save_expr (expr);
  140.   nonnull_expr = expr;
  141.  
  142.   if (BINFO_INHERITANCE_CHAIN (path))
  143.     {
  144.       tree reverse_path = NULL_TREE;
  145.  
  146.       while (path)
  147.     {
  148.       tree r = copy_node (path);
  149.       BINFO_INHERITANCE_CHAIN (r) = reverse_path;
  150.       reverse_path = r;
  151.       path = BINFO_INHERITANCE_CHAIN (path);
  152.     }
  153.       path = reverse_path;
  154.     }
  155.  
  156.   basetype = BINFO_TYPE (path);
  157.  
  158.   while (path)
  159.     {
  160.       if (TREE_VIA_VIRTUAL (path))
  161.     {
  162.       last_virtual = BINFO_TYPE (path);
  163.       if (code == PLUS_EXPR)
  164.         {
  165.           changed = ! fixed_type_p;
  166.  
  167.           if (changed)
  168.         {
  169.           extern int flag_assume_nonnull_objects;
  170.           tree ind;
  171.  
  172.           if (last)
  173.             nonnull_expr = convert_pointer_to (last, nonnull_expr);
  174.           ind = build_indirect_ref (nonnull_expr, NULL_PTR);
  175.           nonnull_expr = build_vbase_pointer (ind, last_virtual);
  176.           if (nonnull == 0 && !flag_assume_nonnull_objects
  177.               && null_expr == NULL_TREE)
  178.             {
  179.               null_expr = build1 (NOP_EXPR, TYPE_POINTER_TO (last_virtual), integer_zero_node);
  180.               expr = build (COND_EXPR, TYPE_POINTER_TO (last_virtual),
  181.                     build (EQ_EXPR, integer_type_node, expr,
  182.                        integer_zero_node),
  183.                     null_expr, nonnull_expr);
  184.             }
  185.         }
  186.           /* else we'll figure out the offset below.  */
  187.  
  188.           /* Happens in the case of parse errors.  */
  189.           if (nonnull_expr == error_mark_node)
  190.         return error_mark_node;
  191.         }
  192.       else
  193.         {
  194.           cp_error ("cannot cast up from virtual baseclass `%T'",
  195.               last_virtual);
  196.           return error_mark_node;
  197.         }
  198.     }
  199.       last = path;
  200.       path = BINFO_INHERITANCE_CHAIN (path);
  201.     }
  202.   /* LAST is now the last basetype assoc on the path.  */
  203.  
  204.   /* A pointer to a virtual base member of a non-null object
  205.      is non-null.  Therefore, we only need to test for zeroness once.
  206.      Make EXPR the canonical expression to deal with here.  */
  207.   if (null_expr)
  208.     {
  209.       TREE_OPERAND (expr, 2) = nonnull_expr;
  210.       TREE_TYPE (TREE_OPERAND (expr, 1)) = TREE_TYPE (nonnull_expr);
  211.     }
  212.   else
  213.     expr = nonnull_expr;
  214.  
  215.   /* If we go through any virtual base pointers, make sure that
  216.      casts to BASETYPE from the last virtual base class use
  217.      the right value for BASETYPE.  */
  218.   if (changed)
  219.     {
  220.       tree intype = TREE_TYPE (TREE_TYPE (expr));
  221.       if (TYPE_MAIN_VARIANT (intype) == BINFO_TYPE (last))
  222.     basetype = intype;
  223.       else
  224.     {
  225.       tree binfo = get_binfo (last, TYPE_MAIN_VARIANT (intype), 0);
  226.       basetype = last;
  227.       offset = BINFO_OFFSET (binfo);
  228.     }
  229.     }
  230.   else
  231.     {
  232.       if (last_virtual)
  233.     {
  234.       offset = BINFO_OFFSET (binfo_member (last_virtual,
  235.                            CLASSTYPE_VBASECLASSES (basetype)));
  236.       offset = size_binop (PLUS_EXPR, offset, BINFO_OFFSET (last));
  237.     }
  238.       else
  239.     offset = BINFO_OFFSET (last);
  240.  
  241. #if 0
  242.       /* why unconditionally set this? (mrs) see deja-gnu/g++.mike/net15.C
  243.      for a test case. */
  244.       code = PLUS_EXPR;
  245. #endif
  246.     }
  247.  
  248.   if (TREE_INT_CST_LOW (offset))
  249.     {
  250.       /* For multiple inheritance: if `this' can be set by any
  251.      function, then it could be 0 on entry to any function.
  252.      Preserve such zeroness here.  Otherwise, only in the
  253.      case of constructors need we worry, and in those cases,
  254.      it will be zero, or initialized to some legal value to
  255.      which we may add.  */
  256.       if (nonnull == 0 && (alias_this == 0 || flag_this_is_variable > 0))
  257.     {
  258.       if (null_expr)
  259.         TREE_TYPE (null_expr) = type;
  260.       else
  261.         null_expr = build1 (NOP_EXPR, type, integer_zero_node);
  262.       if (TREE_SIDE_EFFECTS (expr))
  263.         expr = save_expr (expr);
  264.  
  265.       return build (COND_EXPR, type,
  266.             build (EQ_EXPR, integer_type_node, expr, integer_zero_node),
  267.             null_expr,
  268.             build (code, type, expr, offset));
  269.     }
  270.       else return build (code, type, expr, offset);
  271.     }
  272.  
  273.   /* Cannot change the TREE_TYPE of a NOP_EXPR here, since it may
  274.      be used multiple times in initialization of multiple inheritance.  */
  275.   if (null_expr)
  276.     {
  277.       TREE_TYPE (expr) = type;
  278.       return expr;
  279.     }
  280.   else
  281.     return build1 (NOP_EXPR, type, expr);
  282. }
  283.  
  284. /* Virtual function things.  */
  285.  
  286. /* Virtual functions to be dealt with after laying out our
  287.    base classes.  Usually this is used only when classes have virtual
  288.    baseclasses, but it can happen also when classes have non-virtual
  289.    baseclasses if the derived class overrides baseclass functions
  290.    at different offsets.  */
  291. static tree pending_hard_virtuals;
  292. static int doing_hard_virtuals;
  293.  
  294. /* XXX This is set but never used.  (bpk) */
  295. #if 0
  296. /* Temporary binfo list to memoize lookups of the left-most non-virtual
  297.    baseclass B in a lattice topped by T.  B can appear multiple times
  298.    in the lattice.
  299.    TREE_PURPOSE is B's TYPE_MAIN_VARIANT.
  300.    TREE_VALUE is the path by which B is reached from T.
  301.    TREE_TYPE is B's real type.
  302.  
  303.    If TREE_TYPE is NULL_TREE, it means that B was reached via
  304.    a virtual baseclass.
  305.    N.B.: This list consists of nodes on the temporary obstack.  */
  306. static tree leftmost_baseclasses;
  307. #endif
  308.  
  309. /* Build an entry in the virtual function table.
  310.    DELTA is the offset for the `this' pointer.
  311.    PFN is an ADDR_EXPR containing a pointer to the virtual function.
  312.    Note that the index (DELTA2) in the virtual function table
  313.    is always 0.  */
  314. tree
  315. build_vtable_entry (delta, pfn)
  316.      tree delta, pfn;
  317. {
  318.   tree elems = tree_cons (NULL_TREE, delta,
  319.               tree_cons (NULL_TREE, integer_zero_node,
  320.                      build_tree_list (NULL_TREE, pfn)));
  321.   tree entry = build (CONSTRUCTOR, vtable_entry_type, NULL_TREE, elems);
  322.  
  323.   /* DELTA is constructed by `size_int', which means it may be an
  324.      unsigned quantity on some platforms.  Therefore, we cannot use
  325.      `int_fits_type_p', because when DELTA is really negative,
  326.      `force_fit_type' will make it look like a very large number.  */
  327.  
  328.   if ((TREE_INT_CST_LOW (TYPE_MAX_VALUE (short_integer_type_node))
  329.        < TREE_INT_CST_LOW (delta))
  330.       || (TREE_INT_CST_LOW (delta)
  331.       < TREE_INT_CST_LOW (TYPE_MIN_VALUE (short_integer_type_node))))
  332.     sorry ("object size exceeds built-in limit for virtual function table implementation");
  333.  
  334.   TREE_CONSTANT (entry) = 1;
  335.   TREE_STATIC (entry) = 1;
  336.   TREE_READONLY (entry) = 1;
  337.  
  338. #ifdef GATHER_STATISTICS
  339.   n_vtable_entries += 1;
  340. #endif
  341.  
  342.   return entry;
  343. }
  344.  
  345. /* Given an object INSTANCE, return an expression which yields the
  346.    virtual function corresponding to INDEX.  There are many special
  347.    cases for INSTANCE which we take care of here, mainly to avoid
  348.    creating extra tree nodes when we don't have to.  */
  349. tree
  350. build_vfn_ref (ptr_to_instptr, instance, idx)
  351.      tree *ptr_to_instptr, instance;
  352.      tree idx;
  353. {
  354.   extern int building_cleanup;
  355.   tree vtbl, aref;
  356.   tree basetype = TREE_TYPE (instance);
  357.  
  358.   if (TREE_CODE (basetype) == REFERENCE_TYPE)
  359.     basetype = TREE_TYPE (basetype);
  360.  
  361.   if (instance == C_C_D)
  362.     {
  363.       if (current_vtable_decl == NULL_TREE
  364.       || current_vtable_decl == error_mark_node
  365.       || !UNIQUELY_DERIVED_FROM_P (DECL_FCONTEXT (CLASSTYPE_VFIELD (current_class_type)), basetype))
  366.     vtbl = build_indirect_ref (build_vfield_ref (instance, basetype), NULL_PTR);
  367.       else
  368.     vtbl = current_vtable_decl;
  369.     }
  370.   else
  371.     {
  372.       if (optimize)
  373.     {
  374.       /* Try to figure out what a reference refers to, and
  375.          access its virtual function table directly.  */
  376.       tree ref = NULL_TREE;
  377.  
  378.       if (TREE_CODE (instance) == INDIRECT_REF
  379.           && TREE_CODE (TREE_TYPE (TREE_OPERAND (instance, 0))) == REFERENCE_TYPE)
  380.         ref = TREE_OPERAND (instance, 0);
  381.       else if (TREE_CODE (TREE_TYPE (instance)) == REFERENCE_TYPE)
  382.         ref = instance;
  383.  
  384.       if (ref && TREE_CODE (ref) == VAR_DECL
  385.           && DECL_INITIAL (ref))
  386.         {
  387.           tree init = DECL_INITIAL (ref);
  388.  
  389.           while (TREE_CODE (init) == NOP_EXPR
  390.              || TREE_CODE (init) == NON_LVALUE_EXPR)
  391.         init = TREE_OPERAND (init, 0);
  392.           if (TREE_CODE (init) == ADDR_EXPR)
  393.         {
  394.           init = TREE_OPERAND (init, 0);
  395.           if (IS_AGGR_TYPE (TREE_TYPE (init))
  396.               && (TREE_CODE (init) == PARM_DECL
  397.               || TREE_CODE (init) == VAR_DECL))
  398.             instance = init;
  399.         }
  400.         }
  401.     }
  402.  
  403.       if (IS_AGGR_TYPE (TREE_TYPE (instance))
  404.       && (TREE_CODE (instance) == RESULT_DECL
  405.           || TREE_CODE (instance) == PARM_DECL
  406.           || TREE_CODE (instance) == VAR_DECL))
  407.     vtbl = TYPE_BINFO_VTABLE (basetype);
  408.       else
  409.     vtbl = build_indirect_ref (build_vfield_ref (instance, basetype),
  410.                    NULL_PTR);
  411.     }
  412.   assemble_external (vtbl);
  413.   aref = build_array_ref (vtbl, idx);
  414.  
  415.   /* Save the intermediate result in a SAVE_EXPR so we don't have to
  416.      compute each component of the virtual function pointer twice.  */ 
  417.   if (!building_cleanup && TREE_CODE (aref) == INDIRECT_REF)
  418.     TREE_OPERAND (aref, 0) = save_expr (TREE_OPERAND (aref, 0));
  419.  
  420.   /* FIXME: This is what breaks vtables on the alpha.  */
  421.   *ptr_to_instptr = build (PLUS_EXPR, TREE_TYPE (*ptr_to_instptr),
  422.                *ptr_to_instptr,
  423.                convert (integer_type_node, build_component_ref (aref, delta_identifier, 0, 0)));
  424.   return build_component_ref (aref, pfn_identifier, 0, 0);
  425. }
  426.  
  427. /* Set TREE_PUBLIC and/or TREE_EXTERN on the vtable DECL,
  428.    based on TYPE and other static flags.
  429.  
  430.    Note that anything public is tagged TREE_PUBLIC, whether
  431.    it's public in this file or in another one.  */
  432.  
  433. static void
  434. import_export_vtable (decl, type)
  435.   tree decl, type;
  436. {
  437.   if (write_virtuals >= 2)
  438.     {
  439.       if (CLASSTYPE_INTERFACE_KNOWN (type))
  440.     {
  441.       TREE_PUBLIC (decl) = 1;
  442.       DECL_EXTERNAL (decl) = ! CLASSTYPE_VTABLE_NEEDS_WRITING (type);
  443.     }
  444.     }
  445.   else if (write_virtuals != 0)
  446.     {
  447.       TREE_PUBLIC (decl) = 1;
  448.       if (write_virtuals < 0)
  449.     DECL_EXTERNAL (decl) = 1;
  450.     }
  451. }
  452.  
  453. /* Return the name of the virtual function table (as an IDENTIFIER_NODE)
  454.    for the given TYPE.  */
  455. static tree
  456. get_vtable_name (type)
  457.      tree type;
  458. {
  459.   tree type_id = build_typename_overload (type);
  460.   char *buf = (char *)alloca (sizeof (VTABLE_NAME_FORMAT)
  461.                   + IDENTIFIER_LENGTH (type_id) + 2);
  462.   char *ptr = IDENTIFIER_POINTER (type_id);
  463.   int i;
  464.   for (i = 0; ptr[i] == OPERATOR_TYPENAME_FORMAT[i]; i++) ;
  465. #if 0
  466.   /* We don't take off the numbers; prepare_fresh_vtable uses the
  467.      DECL_ASSEMBLER_NAME for the type, which includes the number
  468.      in `3foo'.  If we were to pull them off here, we'd end up with
  469.      something like `_vt.foo.3bar', instead of a uniform definition.  */
  470.   while (ptr[i] >= '0' && ptr[i] <= '9')
  471.     i += 1;
  472. #endif
  473.   sprintf (buf, VTABLE_NAME_FORMAT, ptr+i);
  474.   return get_identifier (buf);
  475. }
  476.  
  477. /* Build a virtual function for type TYPE.
  478.    If BINFO is non-NULL, build the vtable starting with the initial
  479.    approximation that it is the same as the one which is the head of
  480.    the association list.  */
  481. static tree
  482. build_vtable (binfo, type)
  483.      tree binfo, type;
  484. {
  485.   tree name = get_vtable_name (type);
  486.   tree virtuals, decl;
  487.  
  488.   if (binfo)
  489.     {
  490.       virtuals = copy_list (BINFO_VIRTUALS (binfo));
  491.       decl = build_decl (VAR_DECL, name, TREE_TYPE (BINFO_VTABLE (binfo)));
  492.     }
  493.   else
  494.     {
  495.       virtuals = NULL_TREE;
  496.       decl = build_decl (VAR_DECL, name, void_type_node);
  497.     }
  498.  
  499. #ifdef GATHER_STATISTICS
  500.   n_vtables += 1;
  501.   n_vtable_elems += list_length (virtuals);
  502. #endif
  503.  
  504.   /* Set TREE_PUBLIC and TREE_EXTERN as appropriate.  */
  505.   import_export_vtable (decl, type);
  506.  
  507.   IDENTIFIER_GLOBAL_VALUE (name) = decl = pushdecl_top_level (decl);
  508.   /* Initialize the association list for this type, based
  509.      on our first approximation.  */
  510.   TYPE_BINFO_VTABLE (type) = decl;
  511.   TYPE_BINFO_VIRTUALS (type) = virtuals;
  512.  
  513.   TREE_STATIC (decl) = 1;
  514. #ifndef WRITABLE_VTABLES
  515.   /* Make them READONLY by default. (mrs) */
  516.   TREE_READONLY (decl) = 1;
  517. #endif
  518.   /* At one time the vtable info was grabbed 2 words at a time.  This
  519.      fails on sparc unless you have 8-byte alignment.  (tiemann) */
  520.   DECL_ALIGN (decl) = MAX (TYPE_ALIGN (double_type_node),
  521.                DECL_ALIGN (decl));
  522.  
  523.   /* Why is this conditional? (mrs) */
  524.   if (binfo && write_virtuals >= 0)
  525.     DECL_VIRTUAL_P (decl) = 1;
  526. #if 0
  527.   /* Remember which class this vtable is really for.  */
  528.   if (binfo)
  529.     DECL_VPARENT (decl) = BINFO_TYPE (binfo);
  530.   else
  531.     DECL_VPARENT (decl) = type;
  532. #endif
  533.   DECL_CONTEXT (decl) = type;
  534.  
  535.   binfo = TYPE_BINFO (type);
  536.   SET_BINFO_VTABLE_PATH_MARKED (binfo);
  537.   SET_BINFO_NEW_VTABLE_MARKED (binfo);
  538.   return decl;
  539. }
  540.  
  541. /* Given a base type PARENT, and a derived type TYPE, build
  542.    a name which distinguishes exactly the PARENT member of TYPE's type.
  543.  
  544.    FORMAT is a string which controls how sprintf formats the name
  545.    we have generated.
  546.  
  547.    For example, given
  548.  
  549.     class A; class B; class C : A, B;
  550.  
  551.    it is possible to distinguish "A" from "C's A".  And given
  552.  
  553.     class L;
  554.     class A : L; class B : L; class C : A, B;
  555.  
  556.    it is possible to distinguish "L" from "A's L", and also from
  557.    "C's L from A".
  558.  
  559.    Make sure to use the DECL_ASSEMBLER_NAME of the TYPE_NAME of the
  560.    type, as template have DECL_NAMEs like: X<int>, whereas the
  561.    DECL_ASSEMBLER_NAME is set to be something the assembler can handle.
  562.   */
  563. static tree
  564. build_type_pathname (format, parent, type)
  565.      char *format;
  566.      tree parent, type;
  567. {
  568.   extern struct obstack temporary_obstack;
  569.   char *first, *base, *name;
  570.   int i;
  571.   tree id;
  572.  
  573.   parent = TYPE_MAIN_VARIANT (parent);
  574.  
  575.   /* Remember where to cut the obstack to.  */
  576.   first = obstack_base (&temporary_obstack);
  577.  
  578.   /* Put on TYPE+PARENT.  */
  579.   obstack_grow (&temporary_obstack,
  580.         TYPE_ASSEMBLER_NAME_STRING (type),
  581.         TYPE_ASSEMBLER_NAME_LENGTH (type));
  582. #ifdef JOINER
  583.   obstack_1grow (&temporary_obstack, JOINER);
  584. #else
  585.   obstack_1grow (&temporary_obstack, '_');
  586. #endif
  587.   obstack_grow0 (&temporary_obstack,
  588.          TYPE_ASSEMBLER_NAME_STRING (parent),
  589.          TYPE_ASSEMBLER_NAME_LENGTH (parent));
  590.   i = obstack_object_size (&temporary_obstack);
  591.   base = obstack_base (&temporary_obstack);
  592.   obstack_finish (&temporary_obstack);
  593.  
  594.   /* Put on FORMAT+TYPE+PARENT.  */
  595.   obstack_blank (&temporary_obstack, strlen (format) + i + 1);
  596.   name = obstack_base (&temporary_obstack);
  597.   sprintf (name, format, base);
  598.   id = get_identifier (name);
  599.   obstack_free (&temporary_obstack, first);
  600.  
  601.   return id;
  602. }
  603.  
  604. /* Give TYPE a new virtual function table which is initialized
  605.    with a skeleton-copy of its original initialization.  The only
  606.    entry that changes is the `delta' entry, so we can really
  607.    share a lot of structure.
  608.  
  609.    FOR_TYPE is the derived type which caused this table to
  610.    be needed.
  611.  
  612.    BINFO is the type association which provided TYPE for FOR_TYPE.
  613.  
  614.    The way we update BASE_BINFO's vtable information is just to change the
  615.    association information in FOR_TYPE's association list.  */
  616. static void
  617. prepare_fresh_vtable (binfo, base_binfo, for_type)
  618.      tree binfo, base_binfo, for_type;
  619. {
  620.   tree basetype = BINFO_TYPE (binfo);
  621.   tree orig_decl = BINFO_VTABLE (binfo);
  622.   tree name = build_type_pathname (VTABLE_NAME_FORMAT, basetype, for_type);
  623.   tree new_decl = build_decl (VAR_DECL, name, TREE_TYPE (orig_decl));
  624.   tree path;
  625.   int result;
  626.  
  627.   /* Remember which class this vtable is really for.  */
  628. #if 0
  629.   DECL_VPARENT (new_decl) = BINFO_TYPE (base_binfo);
  630. #endif
  631.   DECL_CONTEXT (new_decl) = for_type;
  632.  
  633.   TREE_STATIC (new_decl) = 1;
  634.   BINFO_VTABLE (binfo) = pushdecl_top_level (new_decl);
  635.   DECL_VIRTUAL_P (new_decl) = 1;
  636. #ifndef WRITABLE_VTABLES
  637.   /* Make them READONLY by default. (mrs) */
  638.   TREE_READONLY (new_decl) = 1;
  639. #endif
  640.   DECL_ALIGN (new_decl) = DECL_ALIGN (orig_decl);
  641.  
  642.   /* Make fresh virtual list, so we can smash it later.  */
  643.   BINFO_VIRTUALS (binfo) = copy_list (BINFO_VIRTUALS (binfo));
  644.   /* Install the value for `headof' if that's what we're doing.  */
  645.   if (flag_dossier)
  646.     TREE_VALUE (TREE_CHAIN (BINFO_VIRTUALS (binfo)))
  647.       = build_vtable_entry (size_binop (MINUS_EXPR, integer_zero_node, BINFO_OFFSET (binfo)),
  648.                 FNADDR_FROM_VTABLE_ENTRY (TREE_VALUE (TREE_CHAIN (BINFO_VIRTUALS (binfo)))));
  649.  
  650. #ifdef GATHER_STATISTICS
  651.   n_vtables += 1;
  652.   n_vtable_elems += list_length (BINFO_VIRTUALS (binfo));
  653. #endif
  654.  
  655.   /* Set TREE_PUBLIC and TREE_EXTERN as appropriate.  */
  656.   import_export_vtable (new_decl, for_type);
  657.  
  658.   if (TREE_VIA_VIRTUAL (binfo))
  659.     my_friendly_assert (binfo == binfo_member (BINFO_TYPE (binfo),
  660.                    CLASSTYPE_VBASECLASSES (current_class_type)),
  661.             170);
  662.   SET_BINFO_NEW_VTABLE_MARKED (binfo);
  663.   SET_BINFO_VTABLE_PATH_MARKED (binfo);
  664.  
  665.   /* Mark all types between FOR_TYPE and TYPE as having been
  666.      touched, so that if we change virtual function table entries,
  667.      new vtables will be initialized.  We may reach the virtual
  668.      baseclass via ambiguous intervening baseclasses.  This
  669.      loop makes sure we get through to the actual baseclass we marked.
  670.  
  671.      Also, update the vtable entries to reflect the overrides
  672.      of the top-most class (short of the top type).  */
  673.  
  674.   do
  675.     {
  676.       result = get_base_distance (basetype, for_type, 0, &path);
  677.       for_type = path;
  678.       while (path)
  679.     {
  680.       tree path_binfo = path;
  681.       tree path_type = BINFO_TYPE (path);
  682.  
  683.       if (TREE_VIA_VIRTUAL (path))
  684.         path_binfo = binfo_member (path_type,
  685.                        CLASSTYPE_VBASECLASSES (current_class_type));
  686.  
  687.       SET_BINFO_VTABLE_PATH_MARKED (path_binfo);
  688.       if (BINFO_INHERITANCE_CHAIN (path)
  689.           && CLASSTYPE_VFIELD (path_type) != NULL_TREE
  690.           && (DECL_NAME (CLASSTYPE_VFIELD (BINFO_TYPE (binfo)))
  691.           == DECL_NAME (CLASSTYPE_VFIELD (path_type)))
  692.           /* This is the baseclass just before the original FOR_TYPE.  */
  693.           && BINFO_INHERITANCE_CHAIN (BINFO_INHERITANCE_CHAIN (path)) == NULL_TREE)
  694.         {
  695.           tree old_virtuals = TREE_CHAIN (BINFO_VIRTUALS (binfo));
  696.           tree new_virtuals = TREE_CHAIN (BINFO_VIRTUALS (path_binfo));
  697.           if (flag_dossier)
  698.         {
  699.           old_virtuals = TREE_CHAIN (old_virtuals);
  700.           new_virtuals = TREE_CHAIN (new_virtuals);
  701.         }
  702.           while (old_virtuals)
  703.         {
  704.           TREE_VALUE (old_virtuals) = TREE_VALUE (new_virtuals);
  705.           old_virtuals = TREE_CHAIN (old_virtuals);
  706.           new_virtuals = TREE_CHAIN (new_virtuals);
  707.         }
  708.         }
  709.       path = BINFO_INHERITANCE_CHAIN (path);
  710.     }
  711.     }
  712.   while (result == -2);
  713. }
  714.  
  715. /* Access the virtual function table entry that logically
  716.    contains BASE_FNDECL.  VIRTUALS is the virtual function table's
  717.    initializer.  */
  718. static tree
  719. get_vtable_entry (virtuals, base_fndecl)
  720.      tree virtuals, base_fndecl;
  721. {
  722.   unsigned HOST_WIDE_INT i = (HOST_BITS_PER_WIDE_INT >= BITS_PER_WORD
  723. #ifdef VTABLE_USES_MASK
  724.        && 0
  725. #endif
  726.        ? (TREE_INT_CST_LOW (DECL_VINDEX (base_fndecl))
  727.           & (((unsigned HOST_WIDE_INT)1<<(BITS_PER_WORD-1))-1))
  728.        : TREE_INT_CST_LOW (DECL_VINDEX (base_fndecl)));
  729.  
  730. #ifdef GATHER_STATISTICS
  731.   n_vtable_searches += i;
  732. #endif
  733.  
  734.   while (i > 0)
  735.     {
  736.       virtuals = TREE_CHAIN (virtuals);
  737.       i -= 1;
  738.     }
  739.   return virtuals;
  740. }
  741.  
  742. /* Put new entry ENTRY into virtual function table initializer
  743.    VIRTUALS.
  744.  
  745.    Also update DECL_VINDEX (FNDECL).  */
  746.  
  747. static void
  748. modify_vtable_entry (old_entry_in_list, new_entry, fndecl)
  749.      tree old_entry_in_list, new_entry, fndecl;
  750. {
  751.   tree base_pfn = FNADDR_FROM_VTABLE_ENTRY (TREE_VALUE (old_entry_in_list));
  752.   tree vindex;
  753.  
  754.   /* We can't put in the really right offset information
  755.      here, since we have not yet laid out the class to
  756.      take into account virtual base classes.  */
  757.   TREE_VALUE (old_entry_in_list) = new_entry;
  758.   vindex = DECL_VINDEX (TREE_OPERAND (base_pfn, 0));
  759.   if (TREE_CODE (DECL_VINDEX (fndecl)) != INTEGER_CST)
  760.     DECL_VINDEX (fndecl) = vindex;
  761.   else
  762.     {
  763.       if (! tree_int_cst_equal (DECL_VINDEX (fndecl), vindex))
  764.     {
  765.       tree elts = CONSTRUCTOR_ELTS (new_entry);
  766.  
  767.       if (! doing_hard_virtuals)
  768.         {
  769.           pending_hard_virtuals
  770.         = tree_cons (fndecl, FNADDR_FROM_VTABLE_ENTRY (new_entry),
  771.                  pending_hard_virtuals);
  772.           TREE_TYPE (pending_hard_virtuals) = TREE_OPERAND (base_pfn, 0);
  773.           return;
  774.         }
  775.     }
  776.     }
  777. }
  778.  
  779. /* Check to ensure that the virtual function table slot in VFIELD,
  780.    found by DECL_VINDEX of the BASE_FNDECL is in fact from a parent
  781.    virtual function table that is the same parent as for the
  782.    BASE_FNDECL given to us.  */
  783.  
  784. static int
  785. related_vslot (base_fndecl, vfields, type)
  786.      tree base_fndecl, vfields, type;
  787. {
  788.   tree base_context = TYPE_MAIN_VARIANT (DECL_CONTEXT (base_fndecl));
  789.   tree base;
  790.   tree path;
  791.   int distance;
  792.  
  793.   if (TREE_CODE (vfields) != TREE_LIST)
  794.     abort ();
  795.   base = VF_NORMAL_VALUE (vfields);
  796.   if (base == NULL_TREE)
  797.     base = VF_BASETYPE_VALUE (vfields);
  798.  
  799.   /* The simple right way to do this is to ensure that the context of
  800.      the base virtual function is found along the leftmost path
  801.      between the most derived type associated with the vfield and the
  802.      current type.  */
  803.   distance = get_base_distance (base, type, 0, &path);
  804.   if (distance == -1)
  805.     abort ();
  806.   while (path)
  807.     {
  808.       if (BINFO_TYPE (path) == base_context)
  809.     return 1;
  810.       path = BINFO_INHERITANCE_CHAIN (path);
  811.     }
  812.  
  813.   /* given:
  814.         Rr
  815.            / \
  816.           Mm  Hh
  817.            \ /
  818.             P
  819.  
  820.      make sure we fill in P's vtable for H with overrides of r,
  821.      but be cautious of virtual base classes.  */
  822.   /* Combine the two below after debugging. */
  823.   if (get_base_distance (base_context, base, 0, &path) != -1)
  824.     {
  825.       while (path)
  826.     {
  827.       if (TREE_VIA_VIRTUAL (path))
  828.         return 0;
  829.       path = BINFO_INHERITANCE_CHAIN (path);
  830.     }
  831.       /* Make sure that:
  832.  
  833.          RRB
  834.          |
  835.         RL   RR
  836.           \ /
  837.        L   R
  838.         \ /
  839.          C
  840.  
  841.      returns 0.   VF_BASETYPE_VALUE is RL, base_context is RRB, type is C,
  842.      and the vfield we are checking is R.  */
  843.       if (VF_BASETYPE_VALUE (vfields)
  844.       && get_base_distance (base_context, VF_BASETYPE_VALUE (vfields), 0, &path) == -1
  845.       && get_base_distance (VF_BASETYPE_VALUE (vfields), base_context, 0, &path) == -1)
  846.     return 0;
  847.       return 1;
  848.     }
  849.   return 0;
  850. }
  851.  
  852. /* Modify virtual function tables in lattice topped by T to
  853.    place FNDECL in tables which previously held BASE_FNDECL.
  854.    PFN is just FNDECL wrapped in an ADDR_EXPR, so that it
  855.    is suitable for placement directly into an initializer.
  856.  
  857.    All distinct virtual function tables that this type uses
  858.    must be updated.  */
  859. static void
  860. modify_vtable_entries (t, fndecl, base_fndecl, pfn)
  861.      tree t;
  862.      tree fndecl, base_fndecl, pfn;
  863. {
  864.   tree base_offset, offset;
  865.   tree base_context = DECL_CLASS_CONTEXT (base_fndecl);
  866.   tree context = DECL_CLASS_CONTEXT (fndecl);
  867.   tree vfield = CLASSTYPE_VFIELD (t);
  868.   tree vfields, vbases;
  869.  
  870.   DECL_CONTEXT (fndecl) = DECL_CONTEXT (base_fndecl);
  871.  
  872.   offset = integer_zero_node;
  873.   if (context != t && TYPE_USES_COMPLEX_INHERITANCE (t))
  874.     {
  875.       offset = virtual_offset (context, CLASSTYPE_VBASECLASSES (t), offset);
  876.       if (offset == NULL_TREE)
  877.     {
  878.       tree binfo = binfo_value (context, t);
  879.       offset = BINFO_OFFSET (binfo);
  880.     }
  881.     }
  882.  
  883.   /* For each layer of base class (i.e., the first base class, and each
  884.      virtual base class from that one), modify the virtual function table
  885.      of the derived class to contain the new virtual function.
  886.      A class has as many vfields as it has virtual base classes (total).  */
  887.   for (vfields = CLASSTYPE_VFIELDS (t); vfields; vfields = TREE_CHAIN (vfields))
  888.     {
  889.       int normal = 1;
  890.       tree binfo, this_offset;
  891.       tree base, path;
  892.  
  893.       if (!related_vslot (base_fndecl, vfields, t))
  894.       continue;
  895.  
  896.       /* Find the right base class for this derived class, call it BASE.  */
  897.       base = VF_BASETYPE_VALUE (vfields);
  898.  
  899.       /* Get the path starting from the deepest base class CONTEXT
  900.      of T (i.e., first defn of BASE_FNDECL).  */
  901.       get_base_distance (DECL_CONTEXT (base_fndecl), t, 0, &path);
  902.  
  903.       /* Get our best approximation of what to use for constructing
  904.      the virtual function table for T.  */
  905.       do
  906.     {
  907.       /* Walk from base toward derived, stopping at the
  908.          most derived baseclass that matters.  That baseclass
  909.          is exactly the one which provides the vtable along
  910.          the VFIELD spine, but no more.  */
  911.       if (TREE_VIA_VIRTUAL (path))
  912.         {
  913.           base = path;
  914.           binfo = binfo_member (BINFO_TYPE (base), CLASSTYPE_VBASECLASSES (t));
  915.           break;
  916.         }
  917.       if (BINFO_INHERITANCE_CHAIN (path) == NULL_TREE
  918.           || (BINFO_TYPE (BINFO_BASETYPE (BINFO_INHERITANCE_CHAIN (path), 0))
  919.           != BINFO_TYPE (path))
  920.           || BINFO_INHERITANCE_CHAIN (BINFO_INHERITANCE_CHAIN (path)) == NULL_TREE)
  921.         {
  922.           base = path;
  923.           binfo = base;
  924.           break;
  925.         }
  926.       path = BINFO_INHERITANCE_CHAIN (path);
  927.     }
  928.       while (1);
  929.  
  930.       /* Find the right offset for the this pointer based on the base
  931.      class we just found.  */
  932.       base_offset = BINFO_OFFSET (binfo);
  933.       this_offset = size_binop (MINUS_EXPR, offset, base_offset);
  934.  
  935.       /* Make sure we can modify the derived association with immunity.  */
  936.       if (TREE_USED (TYPE_BINFO (t)))
  937.     TYPE_BINFO (t) = copy_binfo (TYPE_BINFO (t));
  938.  
  939.       /* We call this case NORMAL iff this virtual function table
  940.      pointer field has its storage reserved in this class.
  941.      This is normally the case without virtual baseclasses
  942.      or off-center multiple baseclasses.  */
  943.       normal = (vfield != NULL_TREE
  944.         && VF_BASETYPE_VALUE (vfields) == DECL_FCONTEXT (vfield)
  945.         && (VF_BINFO_VALUE (vfields) == NULL_TREE
  946.             || ! TREE_VIA_VIRTUAL (VF_BINFO_VALUE (vfields))));
  947.  
  948.       if (normal && VF_BINFO_VALUE (vfields))
  949.     /* Everything looks normal so far...check that we are really
  950.        working from VFIELD's basetype, and not some other appearance
  951.        of that basetype in the lattice.  */
  952.     normal = (VF_BINFO_VALUE (vfields)
  953.           == get_binfo (VF_BASETYPE_VALUE (vfields), t, 0));
  954.  
  955.       if (normal)
  956.     {
  957.       /* In this case, it is *type*'s vtable we are modifying.
  958.          We start with the approximation that it's vtable is that
  959.          of the immediate base class.  */
  960.       base_context = t;
  961.       binfo = TYPE_BINFO (t);
  962.       if (! BINFO_NEW_VTABLE_MARKED (binfo))
  963.         build_vtable (TYPE_BINFO (DECL_CONTEXT (vfield)), t);
  964.     }
  965.       else
  966.     {
  967.       /* This is our very own copy of `basetype' to play with.
  968.          Later, we will fill in all the virtual functions
  969.          that override the virtual functions in these base classes
  970.          which are not defined by the current type.  */
  971.       if (! BINFO_NEW_VTABLE_MARKED (binfo))
  972.         prepare_fresh_vtable (binfo, base, t);
  973.     }
  974.  
  975.       modify_vtable_entry (get_vtable_entry (BINFO_VIRTUALS (binfo), base_fndecl),
  976.                build_vtable_entry (this_offset, pfn),
  977.                fndecl);
  978.     }
  979.   for (vbases = CLASSTYPE_VBASECLASSES (t); vbases; vbases = TREE_CHAIN (vbases))
  980.     {
  981.       tree this_offset;
  982.       tree base, path;
  983.  
  984.       if (! BINFO_VTABLE (vbases))
  985.     /* There are only two ways that a type can fail to have
  986.        virtual functions: neither it nor any of its base
  987.        types define virtual functions (in which case
  988.        no updating need be done), or virtual functions
  989.        accessible to it come from virtual base classes
  990.        (in which case we have or will get them modified
  991.        in other passes of this loop).  */
  992.     continue;
  993.  
  994.       base = BINFO_TYPE (vbases);
  995.       path = NULL_TREE;
  996.  
  997.       if (base != base_context
  998.       && get_base_distance (base_context, base, 0, &path) == -1)
  999.     continue;
  1000.  
  1001.       if (path)
  1002.     this_offset = size_binop (MINUS_EXPR, offset, BINFO_OFFSET (path));
  1003.       else
  1004.     this_offset = offset;
  1005.  
  1006.       /* Doesn't matter if not actually from this virtual base class,
  1007.          but shouldn't come from deeper virtual baseclasses.  The enclosing
  1008.      loop should take care of such baseclasses.  */
  1009.       while (path)
  1010.     {
  1011.       if (TREE_VIA_VIRTUAL (path))
  1012.         goto skip;
  1013.       path = BINFO_INHERITANCE_CHAIN (path);
  1014.     }
  1015.  
  1016.       base_offset = BINFO_OFFSET (vbases);
  1017.       this_offset = size_binop (MINUS_EXPR, this_offset, base_offset);
  1018.  
  1019.       /* Make sure we can modify the derived association with immunity.  */
  1020.       if (TREE_USED (TYPE_BINFO (t)))
  1021.     TYPE_BINFO (t) = copy_binfo (TYPE_BINFO (t));
  1022.  
  1023.       /* This is our very own copy of `basetype' to play with.  */
  1024.       if (! BINFO_NEW_VTABLE_MARKED (vbases))
  1025.     {
  1026.       tree context_binfo = binfo_value (base_context, base);
  1027.       prepare_fresh_vtable (vbases, context_binfo, t);
  1028.     }
  1029.       modify_vtable_entry (get_vtable_entry (BINFO_VIRTUALS (vbases),
  1030.                          base_fndecl),
  1031.                build_vtable_entry (this_offset, pfn),
  1032.                fndecl);
  1033.     skip: {}
  1034.     }
  1035. }
  1036.  
  1037. static tree
  1038. add_virtual_function (pending_virtuals, has_virtual, x, t)
  1039.      tree pending_virtuals;
  1040.      int *has_virtual;
  1041.      tree x;
  1042.      tree t; /* Structure type. */
  1043. {
  1044.   int debug_vbase = 1;
  1045.  
  1046.   /* FUNCTION_TYPEs and OFFSET_TYPEs no longer freely
  1047.      convert to void *.  Make such a conversion here.  */
  1048.   tree vfn = build1 (ADDR_EXPR, ptr_type_node, x);
  1049.   TREE_CONSTANT (vfn) = 1;
  1050.  
  1051.   /* current_class_type may be NULL_TREE in case of error.  */
  1052.   if (current_class_type)
  1053.     TREE_ADDRESSABLE (x) = CLASSTYPE_VTABLE_NEEDS_WRITING (current_class_type);
  1054.  
  1055.   /* If the virtual function is a redefinition of a prior one,
  1056.      figure out in which base class the new definition goes,
  1057.      and if necessary, make a fresh virtual function table
  1058.      to hold that entry.  */
  1059.   if (DECL_VINDEX (x) == error_mark_node)
  1060.     {
  1061.       tree entry = build_vtable_entry (integer_zero_node, vfn);
  1062.  
  1063.       if (flag_dossier && *has_virtual == 0)
  1064.     {
  1065.       /* CLASSTYPE_DOSSIER is only used as a Boolean (NULL or not). */
  1066.       CLASSTYPE_DOSSIER (t) = integer_one_node;
  1067.       *has_virtual = 1;
  1068.         }
  1069.  
  1070.       /* Build a new INT_CST for this DECL_VINDEX.  */
  1071. #ifdef VTABLE_USES_MASK
  1072.       SET_DECL_VINDEX (x, build_int_2 (++(*has_virtual), 0));
  1073. #else
  1074.       {
  1075.     static tree index_table[256];
  1076.     tree index;
  1077.     int i = ++(*has_virtual);
  1078.  
  1079.     if (i >= 256 || index_table[i] == 0)
  1080.       {
  1081.         index = build_int_2 (i, 0);
  1082.         if (i < 256)
  1083.           index_table[i] = index;
  1084.       }
  1085.     else
  1086.       index = index_table[i];
  1087.  
  1088.     DECL_VINDEX (x) = index;
  1089.       }
  1090. #endif
  1091.       pending_virtuals = tree_cons (DECL_VINDEX (x), entry, pending_virtuals);
  1092.     }
  1093.   /* Happens if declared twice in class or we're not in a class definition.
  1094.      We will give error later or we've already given it.  */
  1095.   else if (TREE_CODE (DECL_VINDEX (x)) == INTEGER_CST
  1096.        || current_class_type == NULL_TREE)
  1097.     return pending_virtuals;
  1098.   else if (debug_vbase && TYPE_USES_VIRTUAL_BASECLASSES (current_class_type))
  1099.     {
  1100.       /* Need an entry in some other virtual function table.
  1101.          Deal with this after we have laid out our virtual base classes.  */
  1102.       pending_hard_virtuals = temp_tree_cons (x, vfn, pending_hard_virtuals);
  1103.     }
  1104.   else
  1105.     {
  1106.       /* Need an entry in some other virtual function table.
  1107.          We can do this now.  */
  1108.       tree base_fndecl_list = DECL_VINDEX (x), base_fndecls, prev = 0;
  1109.       tree vtable_context = DECL_FCONTEXT (CLASSTYPE_VFIELD (current_class_type));
  1110.       tree true_base_fndecl = 0;
  1111.  
  1112.       /* First assign DECL_VINDEX from the base vfn with which
  1113.      we share our vtable.  */
  1114.       base_fndecls = base_fndecl_list;
  1115.       while (base_fndecls)
  1116.     {
  1117.       if (TREE_CHAIN (base_fndecls) == NULL_TREE
  1118.           || DECL_FCONTEXT (CLASSTYPE_VFIELD (DECL_CLASS_CONTEXT (TREE_VALUE (base_fndecls)))) == vtable_context)
  1119.         {
  1120.           true_base_fndecl = TREE_VALUE (base_fndecls);
  1121.           modify_vtable_entries (current_class_type, x,
  1122.                      true_base_fndecl, vfn);
  1123.           if (prev)
  1124.         TREE_CHAIN (prev) = TREE_CHAIN (base_fndecls);
  1125.           else
  1126.         base_fndecl_list = prev;
  1127.           break;
  1128.         }
  1129.       prev = base_fndecls;
  1130.       base_fndecls = TREE_CHAIN (base_fndecls);
  1131.     }
  1132.  
  1133.       /* Now fill in the rest of the vtables.  */
  1134.       base_fndecls = base_fndecl_list;
  1135.       while (base_fndecls)
  1136.     {
  1137.       /* If we haven't found one we like, first one wins.  */
  1138.       if (true_base_fndecl == 0)
  1139.         true_base_fndecl = TREE_VALUE (base_fndecls);
  1140.  
  1141.       modify_vtable_entries (current_class_type, x,
  1142.                  TREE_VALUE (base_fndecls), vfn);
  1143.       base_fndecls = TREE_CHAIN (base_fndecls);
  1144.     }
  1145.  
  1146.       DECL_CONTEXT (x) = DECL_CONTEXT (true_base_fndecl);
  1147.     }
  1148.   return pending_virtuals;
  1149. }
  1150.  
  1151. /* Obstack on which to build the vector of class methods.  */
  1152. struct obstack class_obstack;
  1153. extern struct obstack *current_obstack;
  1154.  
  1155. /* Add method METHOD to class TYPE.  This is used when a method
  1156.    has been defined which did not initially appear in the class definition,
  1157.    and helps cut down on spurious error messages.
  1158.  
  1159.    FIELDS is the entry in the METHOD_VEC vector entry of the class type where
  1160.    the method should be added.  */
  1161. void
  1162. add_method (type, fields, method)
  1163.      tree type, *fields, method;
  1164. {
  1165.   /* We must make a copy of METHOD here, since we must be sure that
  1166.      we have exclusive title to this method's DECL_CHAIN.  */
  1167.   tree decl;
  1168.  
  1169.   push_obstacks (&permanent_obstack, &permanent_obstack);
  1170.   {
  1171.     decl = copy_node (method);
  1172.     if (DECL_RTL (decl) == 0
  1173.         && (!processing_template_decl
  1174.             || !uses_template_parms (decl)))
  1175.       {
  1176.     make_function_rtl (decl);
  1177.     DECL_RTL (method) = DECL_RTL (decl);
  1178.       }
  1179.   }
  1180.  
  1181.   if (fields && *fields)
  1182.     {
  1183.       /* Take care not to hide destructor.  */
  1184.       DECL_CHAIN (decl) = DECL_CHAIN (*fields);
  1185.       DECL_CHAIN (*fields) = decl;
  1186.     }
  1187.   else if (CLASSTYPE_METHOD_VEC (type) == 0)
  1188.     {
  1189.       tree method_vec = make_node (TREE_VEC);
  1190.       if (TYPE_IDENTIFIER (type) == DECL_NAME (decl))
  1191.     {
  1192.       TREE_VEC_ELT (method_vec, 0) = decl;
  1193.       TREE_VEC_LENGTH (method_vec) = 1;
  1194.     }
  1195.       else
  1196.     {
  1197.       /* ??? Is it possible for there to have been enough room in the
  1198.          current chunk for the tree_vec structure but not a tree_vec
  1199.          plus a tree*?  Will this work in that case?  */
  1200.       obstack_free (current_obstack, method_vec);
  1201.       obstack_blank (current_obstack, sizeof (struct tree_vec) + sizeof (tree *));
  1202.       TREE_VEC_ELT (method_vec, 1) = decl;
  1203.       TREE_VEC_LENGTH (method_vec) = 2;
  1204.       obstack_finish (current_obstack);
  1205.     }
  1206.       CLASSTYPE_METHOD_VEC (type) = method_vec;
  1207.     }
  1208.   else
  1209.     {
  1210.       tree method_vec = CLASSTYPE_METHOD_VEC (type);
  1211.       int len = TREE_VEC_LENGTH (method_vec);
  1212.  
  1213.       /* Adding a new ctor or dtor.  This is easy because our
  1214.          METHOD_VEC always has a slot for such entries.  */
  1215.       if (TYPE_IDENTIFIER (type) == DECL_NAME (decl))
  1216.     {
  1217.       /* TREE_VEC_ELT (method_vec, 0) = decl; */
  1218.       if (decl != TREE_VEC_ELT (method_vec, 0))
  1219.         {
  1220.           DECL_CHAIN (decl) = TREE_VEC_ELT (method_vec, 0);
  1221.           TREE_VEC_ELT (method_vec, 0) = decl;
  1222.         }
  1223.     }
  1224.       else
  1225.     {
  1226.       /* This is trickier.  We try to extend the TREE_VEC in-place,
  1227.          but if that does not work, we copy all its data to a new
  1228.          TREE_VEC that's large enough.  */
  1229.       struct obstack *ob = &class_obstack;
  1230.       tree *end = (tree *)obstack_next_free (ob);
  1231.  
  1232.       if (end != TREE_VEC_END (method_vec))
  1233.         {
  1234.           ob = current_obstack;
  1235.           TREE_VEC_LENGTH (method_vec) += 1;
  1236.           TREE_VEC_ELT (method_vec, len) = NULL_TREE;
  1237.           method_vec = copy_node (method_vec);
  1238.           TREE_VEC_LENGTH (method_vec) -= 1;
  1239.         }
  1240.       else
  1241.         {
  1242.           tree tmp_vec = (tree) obstack_base (ob);
  1243.           if (obstack_room (ob) < sizeof (tree))
  1244.         {
  1245.           obstack_blank (ob, sizeof (struct tree_common)
  1246.                  + tree_code_length[(int) TREE_VEC]
  1247.                    * sizeof (char *)
  1248.                  + len * sizeof (tree));
  1249.           tmp_vec = (tree) obstack_base (ob);
  1250.           bcopy (method_vec, tmp_vec,
  1251.              (sizeof (struct tree_common)
  1252.               + tree_code_length[(int) TREE_VEC] * sizeof (char *)
  1253.               + (len-1) * sizeof (tree)));
  1254.           method_vec = tmp_vec;
  1255.         }
  1256.           else
  1257.         obstack_blank (ob, sizeof (tree));
  1258.         }
  1259.  
  1260.       obstack_finish (ob);
  1261.       TREE_VEC_ELT (method_vec, len) = decl;
  1262.       TREE_VEC_LENGTH (method_vec) = len + 1;
  1263.       CLASSTYPE_METHOD_VEC (type) = method_vec;
  1264.  
  1265.       if (TYPE_BINFO_BASETYPES (type) && CLASSTYPE_BASELINK_VEC (type))
  1266.         {
  1267.           /* ??? May be better to know whether these can be extended?  */
  1268.           tree baselink_vec = CLASSTYPE_BASELINK_VEC (type);
  1269.  
  1270.           TREE_VEC_LENGTH (baselink_vec) += 1;
  1271.           CLASSTYPE_BASELINK_VEC (type) = copy_node (baselink_vec);
  1272.           TREE_VEC_LENGTH (baselink_vec) -= 1;
  1273.  
  1274.           TREE_VEC_ELT (CLASSTYPE_BASELINK_VEC (type), len) = 0;
  1275.         }
  1276.     }
  1277.     }
  1278.   DECL_CONTEXT (decl) = type;
  1279.   DECL_CLASS_CONTEXT (decl) = type;
  1280.  
  1281.   pop_obstacks ();
  1282. }
  1283.  
  1284. /* Subroutines of finish_struct.  */
  1285.  
  1286. /* Look through the list of fields for this struct, deleting
  1287.    duplicates as we go.  This must be recursive to handle
  1288.    anonymous unions.
  1289.  
  1290.    FIELD is the field which may not appear anywhere in FIELDS.
  1291.    FIELD_PTR, if non-null, is the starting point at which
  1292.    chained deletions may take place.
  1293.    The value returned is the first acceptable entry found
  1294.    in FIELDS.
  1295.  
  1296.    Note that anonymous fields which are not of UNION_TYPE are
  1297.    not duplicates, they are just anonymous fields.  This happens
  1298.    when we have unnamed bitfields, for example.  */
  1299. static tree
  1300. delete_duplicate_fields_1 (field, field_ptr, fields)
  1301.      tree field, *field_ptr, fields;
  1302. {
  1303.   tree x;
  1304.   tree prev = field_ptr ? *field_ptr : 0;
  1305.   if (DECL_NAME (field) == 0)
  1306.     {
  1307.       if (TREE_CODE (TREE_TYPE (field)) != UNION_TYPE)
  1308.     return fields;
  1309.  
  1310.       for (x = TYPE_FIELDS (TREE_TYPE (field)); x; x = TREE_CHAIN (x))
  1311.     fields = delete_duplicate_fields_1 (x, field_ptr, fields);
  1312.       if (prev)
  1313.     TREE_CHAIN (prev) = fields;
  1314.       return fields;
  1315.     }
  1316.   else
  1317.     {
  1318.       for (x = fields; x; prev = x, x = TREE_CHAIN (x))
  1319.     {
  1320.       if (DECL_NAME (x) == 0)
  1321.         {
  1322.           if (TREE_CODE (TREE_TYPE (x)) != UNION_TYPE)
  1323.         continue;
  1324.           TYPE_FIELDS (TREE_TYPE (x))
  1325.         = delete_duplicate_fields_1 (field, (tree *)0, TYPE_FIELDS (TREE_TYPE (x)));
  1326.           if (TYPE_FIELDS (TREE_TYPE (x)) == 0)
  1327.         {
  1328.           if (prev == 0)
  1329.             fields = TREE_CHAIN (fields);
  1330.           else
  1331.             TREE_CHAIN (prev) = TREE_CHAIN (x);
  1332.         }
  1333.         }
  1334.       else
  1335.         {
  1336.           if (DECL_NAME (field) == DECL_NAME (x))
  1337.         {
  1338.           if (TREE_CODE (field) == CONST_DECL
  1339.               && TREE_CODE (x) == CONST_DECL)
  1340.             cp_error_at ("duplicate enum value `%D'", x);
  1341.           else if (TREE_CODE (field) == CONST_DECL
  1342.                || TREE_CODE (x) == CONST_DECL)
  1343.             cp_error_at ("duplicate field `%D' (as enum and non-enum)",
  1344.                 x);
  1345.           else if (TREE_CODE (field) == TYPE_DECL
  1346.                && TREE_CODE (x) == TYPE_DECL)
  1347.             cp_error_at ("duplicate class scope type `%D'", x);
  1348.           else if (TREE_CODE (field) == TYPE_DECL
  1349.                || TREE_CODE (x) == TYPE_DECL)
  1350.             cp_error_at ("duplicate field `%D' (as type and non-type)",
  1351.                 x);
  1352.           else
  1353.             cp_error_at ("duplicate member `%D'", x);
  1354.           if (prev == 0)
  1355.             fields = TREE_CHAIN (fields);
  1356.           else
  1357.             TREE_CHAIN (prev) = TREE_CHAIN (x);
  1358.         }
  1359.         }
  1360.     }
  1361.     }
  1362.   return fields;
  1363. }
  1364.  
  1365. static void
  1366. delete_duplicate_fields (fields)
  1367.      tree fields;
  1368. {
  1369.   tree x;
  1370.   for (x = fields; x && TREE_CHAIN (x); x = TREE_CHAIN (x))
  1371.     TREE_CHAIN (x) = delete_duplicate_fields_1 (x, &x, TREE_CHAIN (x));
  1372. }
  1373.  
  1374. /* Change the visibility of T::FDECL to VISIBILITY.
  1375.    Return 1 if change was legit, otherwise return 0.  */
  1376. static int
  1377. alter_visibility (t, fdecl, visibility)
  1378.      tree t;
  1379.      tree fdecl;
  1380.      enum visibility_type visibility;
  1381. {
  1382.   tree elem = purpose_member (t, DECL_VISIBILITY (fdecl));
  1383.   if (elem && TREE_VALUE (elem) != (tree)visibility)
  1384.     {
  1385.       if (TREE_CODE (TREE_TYPE (fdecl)) == FUNCTION_DECL)
  1386.     {
  1387.       cp_error_at ("conflicting visibility specifications for method `%D', ignored", TREE_TYPE (fdecl));
  1388.     }
  1389.       else
  1390.     error ("conflicting visibility specifications for field `%s', ignored",
  1391.            IDENTIFIER_POINTER (DECL_NAME (fdecl)));
  1392.     }
  1393.   else if (TREE_PRIVATE (fdecl) && visibility != visibility_private)
  1394.     cp_error_at ("cannot make private `%D' non-private", fdecl);
  1395.   else if (TREE_PROTECTED (fdecl))
  1396.     {
  1397.       if (visibility == visibility_public)
  1398.     cp_error_at ("cannot make protected `%D' public", fdecl);
  1399.       goto alter;
  1400.     }
  1401.   /* ARM 11.3: an access declaration may not be used to restrict access
  1402.      to a member that is accessible in the base class.  */
  1403.   else if (TREE_PUBLIC (fdecl)
  1404.        && (visibility == visibility_private
  1405.            || visibility == visibility_protected))
  1406.     cp_error_at ("cannot reduce visibility of public member `%D'", fdecl);
  1407.   else if (elem == NULL_TREE)
  1408.     {
  1409.     alter:
  1410.       DECL_VISIBILITY (fdecl) = tree_cons (t, (tree)visibility,
  1411.                        DECL_VISIBILITY (fdecl));
  1412.       return 1;
  1413.     }
  1414.   return 0;
  1415. }
  1416.  
  1417. static tree
  1418. get_vfield_offset (binfo)
  1419.      tree binfo;
  1420. {
  1421.   return size_binop (PLUS_EXPR,
  1422.              DECL_FIELD_BITPOS (CLASSTYPE_VFIELD (BINFO_TYPE (binfo))),
  1423.              BINFO_OFFSET (binfo));
  1424. }
  1425.  
  1426. /* If FOR_TYPE needs to reinitialize virtual function table pointers
  1427.    for TYPE's sub-objects, add such reinitializations to BASE_INIT_LIST.
  1428.    Returns BASE_INIT_LIST appropriately modified.  */
  1429.  
  1430. static tree
  1431. maybe_fixup_vptrs (for_type, binfo, base_init_list)
  1432.      tree for_type, binfo, base_init_list;
  1433. {
  1434.   /* Now reinitialize any slots that don't fall under our virtual
  1435.      function table pointer.  */
  1436.   tree vfields = CLASSTYPE_VFIELDS (BINFO_TYPE (binfo));
  1437.   while (vfields)
  1438.     {
  1439.       tree basetype = VF_NORMAL_VALUE (vfields)
  1440.     ? TYPE_MAIN_VARIANT (VF_NORMAL_VALUE (vfields))
  1441.       : VF_BASETYPE_VALUE (vfields);
  1442.  
  1443.       tree base_binfo = get_binfo (basetype, for_type, 0);
  1444.       if (CLASSTYPE_NEEDS_VIRTUAL_REINIT (basetype))
  1445.     {
  1446.       tree base_offset = get_vfield_offset (base_binfo);
  1447.       if (! tree_int_cst_equal (base_offset, get_vfield_offset (TYPE_BINFO (for_type)))
  1448.           && ! tree_int_cst_equal (base_offset, get_vfield_offset (binfo)))
  1449.         base_init_list = tree_cons (error_mark_node, base_binfo,
  1450.                     base_init_list);
  1451.     }
  1452.       vfields = TREE_CHAIN (vfields);
  1453.     }
  1454.   return base_init_list;
  1455. }
  1456.  
  1457. /* If TYPE does not have a constructor, then the compiler must
  1458.    manually deal with all of the initialization this type requires.
  1459.  
  1460.    If a base initializer exists only to fill in the virtual function
  1461.    table pointer, then we mark that fact with the TREE_VIRTUAL bit.
  1462.    This way, we avoid multiple initializations of the same field by
  1463.    each virtual function table up the class hierarchy.
  1464.  
  1465.    Virtual base class pointers are not initialized here.  They are
  1466.    initialized only at the "top level" of object creation.  If we
  1467.    initialized them here, we would have to skip a lot of work.  */
  1468.  
  1469. static void
  1470. build_class_init_list (type)
  1471.      tree type;
  1472. {
  1473.   tree base_init_list = NULL_TREE;
  1474.   tree member_init_list = NULL_TREE;
  1475.  
  1476.   /* Since we build member_init_list and base_init_list using
  1477.      tree_cons, backwards fields the all through work.  */
  1478.   tree x;
  1479.   tree binfos = BINFO_BASETYPES (TYPE_BINFO (type));
  1480.   int i, n_baseclasses = binfos ? TREE_VEC_LENGTH (binfos) : 0;
  1481.  
  1482.   for (x = TYPE_FIELDS (type); x; x = TREE_CHAIN (x))
  1483.     {
  1484.       if (TREE_CODE (x) != FIELD_DECL)
  1485.     continue;
  1486.  
  1487.       if (TYPE_NEEDS_CONSTRUCTING (TREE_TYPE (x))
  1488.       || DECL_INITIAL (x) != NULL_TREE)
  1489.     member_init_list = tree_cons (x, type, member_init_list);
  1490.     }
  1491.   member_init_list = nreverse (member_init_list);
  1492.  
  1493.   /* We will end up doing this last.  Need special marker
  1494.      to avoid infinite regress.  */
  1495.   if (TYPE_VIRTUAL_P (type))
  1496.     {
  1497.       base_init_list = build_tree_list (error_mark_node, TYPE_BINFO (type));
  1498.       if (CLASSTYPE_NEEDS_VIRTUAL_REINIT (type) == 0)
  1499.     TREE_VALUE (base_init_list) = NULL_TREE;
  1500.       TREE_ADDRESSABLE (base_init_list) = 1;
  1501.     }
  1502.  
  1503.   /* Each base class which needs to have initialization
  1504.      of some kind gets to make such requests known here.  */
  1505.   for (i = n_baseclasses-1; i >= 0; i--)
  1506.     {
  1507.       tree base_binfo = TREE_VEC_ELT (binfos, i);
  1508.       tree blist;
  1509.  
  1510.       /* Don't initialize virtual baseclasses this way.  */
  1511.       if (TREE_VIA_VIRTUAL (base_binfo))
  1512.     continue;
  1513.  
  1514.       if (TYPE_HAS_CONSTRUCTOR (BINFO_TYPE (base_binfo)))
  1515.     {
  1516.       /* ...and the last shall come first...  */
  1517.       base_init_list = maybe_fixup_vptrs (type, base_binfo, base_init_list);
  1518.       base_init_list = tree_cons (NULL_TREE, base_binfo, base_init_list);
  1519.       continue;
  1520.     }
  1521.  
  1522.       if ((blist = CLASSTYPE_BASE_INIT_LIST (BINFO_TYPE (base_binfo))) == NULL_TREE)
  1523.     /* Nothing to initialize.  */
  1524.     continue;
  1525.  
  1526.       /* ...ditto...  */
  1527.       base_init_list = maybe_fixup_vptrs (type, base_binfo, base_init_list);
  1528.  
  1529.       /* This is normally true for single inheritance.
  1530.      The win is we can shrink the chain of initializations
  1531.      to be done by only converting to the actual type
  1532.      we are interested in.  */
  1533.       if (TREE_VALUE (blist)
  1534.       && TREE_CODE (TREE_VALUE (blist)) == TREE_VEC
  1535.       && tree_int_cst_equal (BINFO_OFFSET (base_binfo),
  1536.                  BINFO_OFFSET (TREE_VALUE (blist))))
  1537.     {
  1538.       if (base_init_list)
  1539.         {
  1540.           /* Does it do more than just fill in a
  1541.          virtual function table pointer?  */
  1542.           if (! TREE_ADDRESSABLE (blist))
  1543.         base_init_list = build_tree_list (blist, base_init_list);
  1544.           /* Can we get by just with the virtual function table
  1545.          pointer that it fills in?  */
  1546.           else if (TREE_ADDRESSABLE (base_init_list)
  1547.                && TREE_VALUE (base_init_list) == 0)
  1548.         base_init_list = blist;
  1549.           /* Maybe, but it is not obvious as the previous case.  */
  1550.           else if (! CLASSTYPE_NEEDS_VIRTUAL_REINIT (type))
  1551.         {
  1552.           tree last = tree_last (base_init_list);
  1553.           while (TREE_VALUE (last)
  1554.              && TREE_CODE (TREE_VALUE (last)) == TREE_LIST)
  1555.             last = tree_last (TREE_VALUE (last));
  1556.           if (TREE_VALUE (last) == 0)
  1557.             base_init_list = build_tree_list (blist, base_init_list);
  1558.         }
  1559.         }
  1560.       else
  1561.         base_init_list = blist;
  1562.     }
  1563.       else
  1564.     {
  1565.       /* The function expand_aggr_init knows how to do the
  1566.          initialization of `basetype' without getting
  1567.          an explicit `blist'.  */
  1568.       if (base_init_list)
  1569.         base_init_list = tree_cons (NULL_TREE, base_binfo, base_init_list);
  1570.       else
  1571.         base_init_list = CLASSTYPE_BINFO_AS_LIST (BINFO_TYPE (base_binfo));
  1572.     }
  1573.     }
  1574.  
  1575.   if (base_init_list)
  1576.     if (member_init_list)
  1577.       CLASSTYPE_BASE_INIT_LIST (type) = build_tree_list (base_init_list, member_init_list);
  1578.     else
  1579.       CLASSTYPE_BASE_INIT_LIST (type) = base_init_list;
  1580.   else if (member_init_list)
  1581.     CLASSTYPE_BASE_INIT_LIST (type) = member_init_list;
  1582. }
  1583.  
  1584. struct base_info
  1585. {
  1586.   int has_virtual;
  1587.   int max_has_virtual;
  1588.   int n_ancestors;
  1589.   tree vfield;
  1590.   tree vfields;
  1591.   char needs_default_ctor;
  1592.   char cant_have_default_ctor;
  1593.   char cant_have_const_ctor;
  1594.   char members_need_dtors;
  1595.   char needs_virtual_dtor;
  1596. };
  1597.  
  1598. /* Record information about type T derived from its base classes.
  1599.    Store most of that information in T itself, and place the
  1600.    remaining information in the struct BASE_INFO.
  1601.  
  1602.    Propagate basetype offsets throughout the lattice.  Note that the
  1603.    lattice topped by T is really a pair: it's a DAG that gives the
  1604.    structure of the derivation hierarchy, and it's a list of the
  1605.    virtual baseclasses that appear anywhere in the DAG.  When a vbase
  1606.    type appears in the DAG, it's offset is 0, and it's children start
  1607.    their offsets from that point.  When a vbase type appears in the list,
  1608.    its offset is the offset it has in the hierarchy, and its children's
  1609.    offsets include that offset in theirs.
  1610.  
  1611.    Returns the index of the first base class to have virtual functions,
  1612.    or zero if no such base class.  */
  1613.  
  1614. static int
  1615. finish_base_struct (t, b, binfos)
  1616.      tree t;
  1617.      struct base_info *b;
  1618.      tree binfos;
  1619. {
  1620.   int i, n_baseclasses = binfos ? TREE_VEC_LENGTH (binfos) : 0;
  1621.   int first_vfn_base_index = -1;
  1622.   bzero (b, sizeof (struct base_info));
  1623.  
  1624.   for (i = 0; i < n_baseclasses; i++)
  1625.     {
  1626.       tree base_binfo = TREE_VEC_ELT (binfos, i);
  1627.       tree basetype = BINFO_TYPE (base_binfo);
  1628.  
  1629.       /* If the type of basetype is incomplete, then
  1630.      we already complained about that fact
  1631.      (and we should have fixed it up as well).  */
  1632.       if (TYPE_SIZE (basetype) == 0)
  1633.     {
  1634.       int j;
  1635.       /* The base type is of incomplete type.  It is
  1636.          probably best to pretend that it does not
  1637.          exist.  */
  1638.       if (i == n_baseclasses-1)
  1639.         TREE_VEC_ELT (binfos, i) = NULL_TREE;
  1640.       TREE_VEC_LENGTH (binfos) -= 1;
  1641.       n_baseclasses -= 1;
  1642.       for (j = i; j+1 < n_baseclasses; j++)
  1643.         TREE_VEC_ELT (binfos, j) = TREE_VEC_ELT (binfos, j+1);
  1644.     }
  1645.  
  1646.       if (TYPE_NEEDS_DESTRUCTOR (basetype))
  1647.     b->members_need_dtors = 1;
  1648.       if (TYPE_HAS_DEFAULT_CONSTRUCTOR (basetype))
  1649.     b->needs_default_ctor = 1;
  1650.       else if (TYPE_HAS_CONSTRUCTOR (basetype))
  1651.     b->cant_have_default_ctor = 1;
  1652.       if (TYPE_GETS_INIT_REF (basetype)
  1653.       && !TYPE_GETS_CONST_INIT_REF (basetype))
  1654.     b->cant_have_const_ctor = 1;
  1655.  
  1656.       CLASSTYPE_ALTERS_VISIBILITIES_P (t)
  1657.     |= CLASSTYPE_ALTERS_VISIBILITIES_P (basetype);
  1658.  
  1659.       b->n_ancestors += CLASSTYPE_N_SUPERCLASSES (basetype);
  1660.       TYPE_NEEDS_CONSTRUCTING (t) |= TYPE_NEEDS_CONSTRUCTING (basetype);
  1661.       TYPE_NEEDS_CONSTRUCTOR (t) |= TYPE_NEEDS_CONSTRUCTOR (basetype);
  1662.       TYPE_NEEDS_DESTRUCTOR (t) |= TYPE_NEEDS_DESTRUCTOR (basetype);
  1663.       TYPE_GETS_ASSIGNMENT (t) |= TYPE_GETS_ASSIGNMENT (basetype);
  1664.       TYPE_GETS_INIT_REF (t) |= TYPE_GETS_INIT_REF (basetype);
  1665.  
  1666.       TYPE_OVERLOADS_CALL_EXPR (t) |= TYPE_OVERLOADS_CALL_EXPR (basetype);
  1667.       TYPE_OVERLOADS_ARRAY_REF (t) |= TYPE_OVERLOADS_ARRAY_REF (basetype);
  1668.       TYPE_OVERLOADS_ARROW (t) |= TYPE_OVERLOADS_ARROW (basetype);
  1669.  
  1670.       if (! TREE_VIA_VIRTUAL (base_binfo)
  1671. #if 0
  1672.       /* This cannot be done, as prepare_fresh_vtable wants to modify
  1673.          binfos associated with vfields anywhere in the hierarchy, not
  1674.          just immediate base classes.  Due to unsharing, the compiler
  1675.          might consume 3% more memory on a real program.
  1676.          */
  1677.       && ! BINFO_OFFSET_ZEROP (base_binfo)
  1678. #endif
  1679.       && BINFO_BASETYPES (base_binfo))
  1680.     {
  1681.       tree base_binfos = BINFO_BASETYPES (base_binfo);
  1682.       tree chain = NULL_TREE;
  1683.       int j;
  1684.  
  1685.       /* Now unshare the structure beneath BASE_BINFO.  */
  1686.       for (j = TREE_VEC_LENGTH (base_binfos)-1;
  1687.            j >= 0; j--)
  1688.         {
  1689.           tree base_base_binfo = TREE_VEC_ELT (base_binfos, j);
  1690.           if (! TREE_VIA_VIRTUAL (base_base_binfo))
  1691.         TREE_VEC_ELT (base_binfos, j)
  1692.           = make_binfo (BINFO_OFFSET (base_base_binfo),
  1693.                 BINFO_TYPE (base_base_binfo),
  1694.                 BINFO_VTABLE (base_base_binfo),
  1695.                 BINFO_VIRTUALS (base_base_binfo),
  1696.                 chain);
  1697.           chain = TREE_VEC_ELT (base_binfos, j);
  1698.           TREE_VIA_PUBLIC (chain) = TREE_VIA_PUBLIC (base_base_binfo);
  1699.           TREE_VIA_PROTECTED (chain) = TREE_VIA_PROTECTED (base_base_binfo);
  1700.         }
  1701.  
  1702.       /* Completely unshare potentially shared data, and
  1703.          update what is ours.  */
  1704.       propagate_binfo_offsets (base_binfo, BINFO_OFFSET (base_binfo));
  1705.     }
  1706.  
  1707.       if (! TREE_VIA_VIRTUAL (base_binfo))
  1708.     CLASSTYPE_N_SUPERCLASSES (t) += 1;
  1709.  
  1710.       if (TYPE_VIRTUAL_P (basetype))
  1711.     {
  1712.       /* If there's going to be a destructor needed, make
  1713.          sure it will be virtual.  */
  1714.       b->needs_virtual_dtor = 1;
  1715.  
  1716.       /* Don't borrow virtuals from virtual baseclasses.  */
  1717.       if (TREE_VIA_VIRTUAL (base_binfo))
  1718.         continue;
  1719.  
  1720.       if (first_vfn_base_index < 0)
  1721.         {
  1722.           tree vfields;
  1723.           first_vfn_base_index = i;
  1724.  
  1725.           b->has_virtual = CLASSTYPE_VSIZE (basetype);
  1726.           b->vfield = CLASSTYPE_VFIELD (basetype);
  1727.           b->vfields = copy_list (CLASSTYPE_VFIELDS (basetype));
  1728.           vfields = b->vfields;
  1729.           while (vfields)
  1730.         {
  1731.           if (VF_BINFO_VALUE (vfields) == NULL_TREE
  1732.               || ! TREE_VIA_VIRTUAL (VF_BINFO_VALUE (vfields)))
  1733.             {
  1734.               tree value = VF_BASETYPE_VALUE (vfields);
  1735.               if (DECL_NAME (CLASSTYPE_VFIELD (value))
  1736.               == DECL_NAME (CLASSTYPE_VFIELD (basetype)))
  1737.             VF_NORMAL_VALUE (b->vfields) = basetype;
  1738.               else
  1739.             VF_NORMAL_VALUE (b->vfields) = VF_NORMAL_VALUE (vfields);
  1740.             }
  1741.           vfields = TREE_CHAIN (vfields);
  1742.         }
  1743.           CLASSTYPE_VFIELD (t) = b->vfield;
  1744.         }
  1745.       else
  1746.         {
  1747.           /* Only add unique vfields, and flatten them out as we go.  */
  1748.           tree vfields = CLASSTYPE_VFIELDS (basetype);
  1749.           while (vfields)
  1750.         {
  1751.           if (VF_BINFO_VALUE (vfields) == NULL_TREE
  1752.               || ! TREE_VIA_VIRTUAL (VF_BINFO_VALUE (vfields)))
  1753.             {
  1754.               tree value = VF_BASETYPE_VALUE (vfields);
  1755.               b->vfields = tree_cons (base_binfo, value, b->vfields);
  1756.               if (DECL_NAME (CLASSTYPE_VFIELD (value))
  1757.               == DECL_NAME (CLASSTYPE_VFIELD (basetype)))
  1758.             VF_NORMAL_VALUE (b->vfields) = basetype;
  1759.               else
  1760.             VF_NORMAL_VALUE (b->vfields) = VF_NORMAL_VALUE (vfields);
  1761.             }
  1762.           vfields = TREE_CHAIN (vfields);
  1763.         }
  1764.  
  1765.           if (b->has_virtual == 0)
  1766.         {
  1767.           first_vfn_base_index = i;
  1768.           b->has_virtual = CLASSTYPE_VSIZE (basetype);
  1769.           b->vfield = CLASSTYPE_VFIELD (basetype);
  1770.           CLASSTYPE_VFIELD (t) = b->vfield;
  1771.           /* When we install the first one, set the VF_NORMAL_VALUE
  1772.              to be the current class, as this it is the most derived
  1773.              class.  Hopefully, this is not set to something else
  1774.              later.  (mrs) */
  1775.           vfields = b->vfields;
  1776.           while (vfields)
  1777.             {
  1778.               if (DECL_NAME (CLASSTYPE_VFIELD (t))
  1779.               == DECL_NAME (CLASSTYPE_VFIELD (basetype)))
  1780.             {
  1781.               VF_NORMAL_VALUE (vfields) = t;
  1782.               /* There should only be one of them!  And it should
  1783.                  always be found, if we get into here.  (mrs)  */
  1784.               break;
  1785.             }
  1786.               vfields = TREE_CHAIN (vfields);
  1787.             }
  1788.         }
  1789.         }
  1790.     }
  1791.     }
  1792.  
  1793.   {
  1794.     tree vfields;
  1795.     /* Find the base class with the largest number of virtual functions.  */
  1796.     for (vfields = b->vfields; vfields; vfields = TREE_CHAIN (vfields))
  1797.       {
  1798.     if (CLASSTYPE_VSIZE (VF_BASETYPE_VALUE (vfields)) > b->max_has_virtual)
  1799.       b->max_has_virtual = CLASSTYPE_VSIZE (VF_BASETYPE_VALUE (vfields));
  1800.     if (VF_DERIVED_VALUE (vfields)
  1801.         && CLASSTYPE_VSIZE (VF_DERIVED_VALUE (vfields)) > b->max_has_virtual)
  1802.       b->max_has_virtual = CLASSTYPE_VSIZE (VF_DERIVED_VALUE (vfields));
  1803.       }
  1804.   }
  1805.  
  1806.   if (b->vfield == 0)
  1807.     /* If all virtual functions come only from virtual baseclasses.  */
  1808.     return -1;
  1809.   return first_vfn_base_index;
  1810. }
  1811.  
  1812. static int
  1813. typecode_p (type, code)
  1814.      tree type;
  1815.      enum tree_code code;
  1816. {
  1817.   return (TREE_CODE (type) == code
  1818.       || (TREE_CODE (type) == REFERENCE_TYPE
  1819.           && TREE_CODE (TREE_TYPE (type)) == code));
  1820. }
  1821.  
  1822. /* Set memoizing fields and bits of T (and its variants) for later use.
  1823.    MAX_HAS_VIRTUAL is the largest size of any T's virtual function tables.  */
  1824. static void
  1825. finish_struct_bits (t, max_has_virtual)
  1826.      tree t;
  1827.      int max_has_virtual;
  1828. {
  1829.   int i, n_baseclasses = CLASSTYPE_N_BASECLASSES (t);
  1830.   tree method_vec = CLASSTYPE_METHOD_VEC (t);
  1831.  
  1832.   /* Fix up variants (if any).  */
  1833.   tree variants = TYPE_NEXT_VARIANT (t);
  1834.   while (variants)
  1835.     {
  1836.       /* These fields are in the _TYPE part of the node, not in
  1837.      the TYPE_LANG_SPECIFIC component, so they are not shared.  */
  1838.       TYPE_HAS_CONSTRUCTOR (variants) = TYPE_HAS_CONSTRUCTOR (t);
  1839.       TYPE_HAS_DESTRUCTOR (variants) = TYPE_HAS_DESTRUCTOR (t);
  1840.       TYPE_NEEDS_CONSTRUCTOR (variants) = TYPE_NEEDS_CONSTRUCTOR (t);
  1841.       TYPE_NEEDS_CONSTRUCTING (variants) = TYPE_NEEDS_CONSTRUCTING (t);
  1842.       TYPE_NEEDS_DESTRUCTOR (variants) = TYPE_NEEDS_DESTRUCTOR (t);
  1843.  
  1844.       TYPE_USES_COMPLEX_INHERITANCE (variants) = TYPE_USES_COMPLEX_INHERITANCE (t);
  1845.       TYPE_VIRTUAL_P (variants) = TYPE_VIRTUAL_P (t);
  1846.       TYPE_USES_VIRTUAL_BASECLASSES (variants) = TYPE_USES_VIRTUAL_BASECLASSES (t);
  1847.       /* Copy whatever these are holding today.  */
  1848.       TYPE_MIN_VALUE (variants) = TYPE_MIN_VALUE (t);
  1849.       TYPE_MAX_VALUE (variants) = TYPE_MAX_VALUE (t);
  1850.       variants = TYPE_NEXT_VARIANT (variants);
  1851.     }
  1852.  
  1853.   if (n_baseclasses && max_has_virtual)
  1854.     {
  1855.       /* Done by `finish_struct' for classes without baseclasses.  */
  1856.       int has_abstract_virtuals = CLASSTYPE_ABSTRACT_VIRTUALS (t) != 0;
  1857.       tree binfos = TYPE_BINFO_BASETYPES (t);
  1858.       for (i = n_baseclasses-1; i >= 0; i--)
  1859.     {
  1860.       has_abstract_virtuals
  1861.         |= (CLASSTYPE_ABSTRACT_VIRTUALS (BINFO_TYPE (TREE_VEC_ELT (binfos, i))) != 0);
  1862.       if (has_abstract_virtuals)
  1863.         break;
  1864.     }
  1865.       if (has_abstract_virtuals)
  1866.     CLASSTYPE_ABSTRACT_VIRTUALS (t) = get_abstract_virtuals (t);
  1867.     }
  1868.  
  1869.   if (n_baseclasses)
  1870.     {
  1871.       /* Notice whether this class has type conversion functions defined.  */
  1872.       tree binfo = TYPE_BINFO (t);
  1873.       tree binfos = BINFO_BASETYPES (binfo);
  1874.       int n_binfos = list_length (binfo);
  1875.       tree vbases = CLASSTYPE_VBASECLASSES (t), basetype;
  1876.       int n_vbases = list_length (vbases), j;
  1877.  
  1878.       build_mi_virtuals (n_binfos+n_vbases*n_baseclasses, max_has_virtual);
  1879.       /* Fill in virtual function table with values which do not come
  1880.      "normal"ly, i.e., those which come from virtual and/or
  1881.      non-leftmost base classes.  */
  1882.       for (i = 0; binfo; binfo = TREE_CHAIN (binfo))
  1883.     {
  1884.       if (TREE_VIA_VIRTUAL (binfo))
  1885.         /* Virtual functions from virtual baseclasses are done below.  */;
  1886.       else if (CLASSTYPE_VSIZE (BINFO_TYPE (binfo)))
  1887.         {
  1888.           tree virtuals = TREE_CHAIN (BINFO_VIRTUALS (binfo));
  1889.           if (flag_dossier)
  1890.         virtuals = TREE_CHAIN (virtuals);
  1891.           add_mi_virtuals (++i, virtuals);
  1892.         }
  1893.     }
  1894.       for (; vbases; vbases = TREE_CHAIN (vbases))
  1895.     {
  1896.       basetype = BINFO_TYPE (vbases);
  1897.       if (CLASSTYPE_VSIZE (basetype))
  1898.         for (j = n_baseclasses-1; j >= 0; j--)
  1899.           {
  1900.         tree this_binfo = TREE_VEC_ELT (binfos, j);
  1901.         if (UNIQUELY_DERIVED_FROM_P (basetype, this_binfo))
  1902.           {
  1903.             tree virtuals = TREE_CHAIN (BINFO_VIRTUALS (vbases));
  1904.             if (flag_dossier)
  1905.               virtuals = TREE_CHAIN (virtuals);
  1906.             add_mi_virtuals (++i, virtuals);
  1907.           }
  1908.           }
  1909.     }
  1910.       for (i = n_baseclasses-1; i >= 0; i--)
  1911.     {
  1912.       basetype = BINFO_TYPE (TREE_VEC_ELT (binfos, i));
  1913.  
  1914.       if (TYPE_HAS_CONVERSION (basetype))
  1915.         {
  1916.           TYPE_HAS_CONVERSION (t) = 1;
  1917.           TYPE_HAS_INT_CONVERSION (t) |= TYPE_HAS_INT_CONVERSION (basetype);
  1918.           TYPE_HAS_REAL_CONVERSION (t) |= TYPE_HAS_REAL_CONVERSION (basetype);
  1919.         }
  1920.       if (CLASSTYPE_MAX_DEPTH (basetype) >= CLASSTYPE_MAX_DEPTH (t))
  1921.         CLASSTYPE_MAX_DEPTH (t) = CLASSTYPE_MAX_DEPTH (basetype) + 1;
  1922.     }
  1923.       report_ambiguous_mi_virtuals (n_binfos+n_vbases*n_baseclasses, t);
  1924. #if 0
  1925.       /* Now that we know what the virtual function table looks like,
  1926.      fix up offsets in the presence of virtual base classes.  */
  1927.       if (n_vbases)
  1928.     fixup_vbase_offsets (t);
  1929. #endif
  1930.     }
  1931.  
  1932.   /* Need to test METHOD_VEC here in case all methods
  1933.      (conversions and otherwise) are inherited.  */
  1934.   if (TYPE_HAS_CONVERSION (t) && method_vec != NULL_TREE)
  1935.     {
  1936.       tree first_conversions[last_conversion_type];
  1937.       tree last_conversions[last_conversion_type];
  1938.       enum conversion_type conv_index;
  1939.       tree *tmp;
  1940.       int i;
  1941.  
  1942.       bzero (first_conversions, sizeof (first_conversions));
  1943.       bzero (last_conversions, sizeof (last_conversions));
  1944.       for (tmp = &TREE_VEC_ELT (method_vec, 1);
  1945.        tmp != TREE_VEC_END (method_vec); tmp += 1)
  1946.     {
  1947.       /* ??? This should compare DECL_NAME (*tmp) == ansi_opname[TYPE_EXPR].  */
  1948.       if (IDENTIFIER_TYPENAME_P (DECL_ASSEMBLER_NAME (*tmp)))
  1949.         {
  1950.           tree fntype = TREE_TYPE (*tmp);
  1951.           tree return_type = TREE_TYPE (fntype);
  1952.           my_friendly_assert (TREE_CODE (fntype) == METHOD_TYPE, 171);
  1953.  
  1954.           if (typecode_p (return_type, POINTER_TYPE))
  1955.         {
  1956.           if (TYPE_READONLY (TREE_TYPE (return_type)))
  1957.             conv_index = constptr_conv;
  1958.           else
  1959.             conv_index = ptr_conv;
  1960.         }
  1961.           else if (typecode_p (return_type, INTEGER_TYPE))
  1962.         {
  1963.           TYPE_HAS_INT_CONVERSION (t) = 1;
  1964.           conv_index = int_conv;
  1965.         }
  1966.           else if (typecode_p (return_type, REAL_TYPE))
  1967.         {
  1968.           TYPE_HAS_REAL_CONVERSION (t) = 1;
  1969.           conv_index = real_conv;
  1970.         }
  1971.           else
  1972.         continue;
  1973.  
  1974.           if (first_conversions[(int) conv_index] == NULL_TREE)
  1975.         first_conversions[(int) conv_index] = *tmp;
  1976.           last_conversions[(int) conv_index] = *tmp;
  1977.         }
  1978.     }
  1979.  
  1980.       for (i = 0; i < (int) last_conversion_type; i++)
  1981.     if (first_conversions[i] != last_conversions[i])
  1982.       CLASSTYPE_CONVERSION (t, i) = error_mark_node;
  1983.     else
  1984.       CLASSTYPE_CONVERSION (t, i) = first_conversions[i];
  1985.     }
  1986.  
  1987.   /* If this type has constructors, force its mode to be BLKmode,
  1988.      and force its TREE_ADDRESSABLE bit to be nonzero.  */
  1989.   if (TYPE_NEEDS_CONSTRUCTING (t) || TYPE_NEEDS_DESTRUCTOR (t))
  1990.     {
  1991.       tree variants = t;
  1992.  
  1993.       if (TREE_CODE (TYPE_NAME (t)) == TYPE_DECL)
  1994.     DECL_MODE (TYPE_NAME (t)) = BLKmode;
  1995.       while (variants)
  1996.     {
  1997.       TYPE_MODE (variants) = BLKmode;
  1998.       TREE_ADDRESSABLE (variants) = 1;
  1999.       variants = TYPE_NEXT_VARIANT (variants);
  2000.     }
  2001.     }
  2002. }
  2003.  
  2004. /* Warn about duplicate methods in fn_fields.  Also compact method
  2005.    lists so that lookup can be made faster.
  2006.  
  2007.    Algorithm: Outer loop builds lists by method name.  Inner loop
  2008.    checks for redundant method names within a list.
  2009.  
  2010.    Data Structure: List of method lists.  The outer list is a
  2011.    TREE_LIST, whose TREE_PURPOSE field is the field name and the
  2012.    TREE_VALUE is the TREE_CHAIN of the FUNCTION_DECLs.  Friends are
  2013.    chained in the same way as member functions, but they live in the
  2014.    TREE_TYPE field of the outer list.  That allows them to be quickly
  2015.    deleted, and requires no extra storage.
  2016.  
  2017.    If there are any constructors/destructors, they are moved to the
  2018.    front of the list.  This makes pushclass more efficient.
  2019.  
  2020.    We also link each field which has shares a name with its baseclass
  2021.    to the head of the list of fields for that base class.  This allows
  2022.    us to reduce search time in places like `build_method_call' to
  2023.    consider only reasonably likely functions.  */
  2024.  
  2025. static tree
  2026. finish_struct_methods (t, fn_fields, nonprivate_method)
  2027.      tree t;
  2028.      tree fn_fields;
  2029.      int nonprivate_method;
  2030. {
  2031.   tree method_vec;
  2032.   tree name = constructor_name (t);
  2033.   int i, n_baseclasses = CLASSTYPE_N_BASECLASSES (t);
  2034.  
  2035.   /* Now prepare to gather fn_fields into vector.  */
  2036.   struct obstack *ambient_obstack = current_obstack;
  2037.   current_obstack = &class_obstack;
  2038.   method_vec = make_node (TREE_VEC);
  2039.   /* Room has been saved for constructors and destructors.  */
  2040.   current_obstack = ambient_obstack;
  2041.   /* Now make this a live vector.  */
  2042.   obstack_free (&class_obstack, method_vec);
  2043.   obstack_blank (&class_obstack, sizeof (struct tree_vec));
  2044.  
  2045.   while (fn_fields)
  2046.     {
  2047.       /* NEXT Pointer, TEST Pointer, and BASE Pointer.  */
  2048.       tree nextp, *testp;
  2049.       tree fn_name = DECL_NAME (fn_fields);
  2050.       if (fn_name == NULL_TREE)
  2051.     fn_name = name;
  2052.  
  2053.       nextp = TREE_CHAIN (fn_fields);
  2054.       TREE_CHAIN (fn_fields) = NULL_TREE;
  2055.       /* Constructors are handled easily in search routines.
  2056.      Besides, we know we won't find any, so do not bother looking.  */
  2057.       if (fn_name == name && TREE_VEC_ELT (method_vec, 0) == 0)
  2058.     TREE_VEC_ELT (method_vec, 0) = fn_fields;
  2059.       else
  2060.     {
  2061.       testp = &TREE_VEC_ELT (method_vec, 0);
  2062.       if (*testp == NULL_TREE)
  2063.         testp++;
  2064.       while (((HOST_WIDE_INT) testp
  2065.           < (HOST_WIDE_INT) obstack_next_free (&class_obstack))
  2066.          && DECL_NAME (*testp) != fn_name)
  2067.         testp++;
  2068.       if ((HOST_WIDE_INT) testp
  2069.           < (HOST_WIDE_INT) obstack_next_free (&class_obstack))
  2070.         {
  2071.           tree x, prev_x;
  2072.  
  2073.           for (x = *testp; x; x = DECL_CHAIN (x))
  2074.         {
  2075.           if (DECL_NAME (fn_fields) == ansi_opname[(int) DELETE_EXPR])
  2076.             {
  2077.               /* ANSI C++ June 5 1992 WP 12.5.5.1 */
  2078.               cp_error_at ("`%D' overloaded", fn_fields);
  2079.               cp_error_at ("previous declaration as `%D' here", x);
  2080.             }
  2081.           if (DECL_ASSEMBLER_NAME (fn_fields)==DECL_ASSEMBLER_NAME (x))
  2082.             {
  2083.               /* We complain about multiple destructors on sight,
  2084.              so we do not repeat the warning here.  Friend-friend
  2085.              ambiguities are warned about outside this loop.  */
  2086.               if (!DESTRUCTOR_NAME_P (DECL_ASSEMBLER_NAME (fn_fields)))
  2087.             cp_error_at ("ambiguous method `%#D' in structure",
  2088.                      fn_fields);
  2089.               break;
  2090.             }
  2091.           prev_x = x;
  2092.         }
  2093.           if (x == 0)
  2094.         {
  2095.           if (*testp)
  2096.             DECL_CHAIN (prev_x) = fn_fields;
  2097.           else
  2098.             *testp = fn_fields;
  2099.         }
  2100.         }
  2101.       else
  2102.         {
  2103.           obstack_ptr_grow (&class_obstack, fn_fields);
  2104.           method_vec = (tree)obstack_base (&class_obstack);
  2105.         }
  2106.     }
  2107.       fn_fields = nextp;
  2108.     }
  2109.  
  2110.   TREE_VEC_LENGTH (method_vec) = (tree *)obstack_next_free (&class_obstack)
  2111.     - (&TREE_VEC_ELT (method_vec, 0));
  2112.   obstack_finish (&class_obstack);
  2113.   CLASSTYPE_METHOD_VEC (t) = method_vec;
  2114.  
  2115.   if (nonprivate_method == 0
  2116.       && CLASSTYPE_FRIEND_CLASSES (t) == NULL_TREE
  2117.       && DECL_FRIENDLIST (TYPE_NAME (t)) == NULL_TREE)
  2118.     {
  2119.       tree binfos = BINFO_BASETYPES (TYPE_BINFO (t));
  2120.       for (i = 0; i < n_baseclasses; i++)
  2121.     if (TREE_VIA_PUBLIC (TREE_VEC_ELT (binfos, i))
  2122.         || TREE_VIA_PROTECTED (TREE_VEC_ELT (binfos, i)))
  2123.       {
  2124.         nonprivate_method = 1;
  2125.         break;
  2126.       }
  2127.       if (nonprivate_method == 0)
  2128.     cp_warning ("all member functions in class `%T' are private", t);
  2129.     }
  2130.  
  2131.   /* If there are constructors (and destructors), they are at the
  2132.      front.  Place destructors at very front.  Also warn if all
  2133.      constructors and/or destructors are private (in which case this
  2134.      class is effectively unusable.  */
  2135.   if (TYPE_HAS_DESTRUCTOR (t))
  2136.     {
  2137.       tree dtor, prev;
  2138.  
  2139.       for (dtor = TREE_VEC_ELT (method_vec, 0);
  2140.        dtor;
  2141.        prev = dtor, dtor = DECL_CHAIN (dtor))
  2142.     {
  2143.       if (DESTRUCTOR_NAME_P (DECL_ASSEMBLER_NAME (dtor)))
  2144.         {
  2145.           if (TREE_PRIVATE (dtor)
  2146.           && CLASSTYPE_FRIEND_CLASSES (t) == NULL_TREE
  2147.           && DECL_FRIENDLIST (TYPE_NAME (t)) == NULL_TREE
  2148.           && warn_ctor_dtor_privacy)
  2149.         warning ("class `%s' only defines a private destructor and has no friends",
  2150.              TYPE_NAME_STRING (t));
  2151.           break;
  2152.         }
  2153.     }
  2154.  
  2155.       /* Wild parse errors can cause this to happen.  */
  2156.       if (dtor == NULL_TREE)
  2157.     TYPE_HAS_DESTRUCTOR (t) = 0;
  2158.       else if (dtor != TREE_VEC_ELT (method_vec, 0))
  2159.     {
  2160.       DECL_CHAIN (prev) = DECL_CHAIN (dtor);
  2161.       DECL_CHAIN (dtor) = TREE_VEC_ELT (method_vec, 0);
  2162.       TREE_VEC_ELT (method_vec, 0) = dtor;
  2163.     }
  2164.     }
  2165.  
  2166.   /* Now for each member function (except for constructors and
  2167.      destructors), compute where member functions of the same
  2168.      name reside in base classes.  */
  2169.   if (n_baseclasses != 0
  2170.       && TREE_VEC_LENGTH (method_vec) > 1)
  2171.     {
  2172.       int len = TREE_VEC_LENGTH (method_vec);
  2173.       tree baselink_vec = make_tree_vec (len);
  2174.       int any_links = 0;
  2175.       tree baselink_binfo = build_tree_list (NULL_TREE, TYPE_BINFO (t));
  2176.  
  2177.       for (i = 1; i < len; i++)
  2178.     {
  2179.       TREE_VEC_ELT (baselink_vec, i)
  2180.         = get_baselinks (baselink_binfo, t, DECL_NAME (TREE_VEC_ELT (method_vec, i)));
  2181.       if (TREE_VEC_ELT (baselink_vec, i) != 0)
  2182.         any_links = 1;
  2183.     }
  2184.       if (any_links != 0)
  2185.     CLASSTYPE_BASELINK_VEC (t) = baselink_vec;
  2186.       else
  2187.     obstack_free (current_obstack, baselink_vec);
  2188.     }
  2189.  
  2190.   /* Now add the methods to the TYPE_METHODS of T, arranged in a chain.  */
  2191.   {
  2192.     tree x, last_x = NULL_TREE;
  2193.     int limit = TREE_VEC_LENGTH (method_vec);
  2194.  
  2195.     for (i = 1; i < limit; i++)
  2196.       {
  2197.     for (x = TREE_VEC_ELT (method_vec, i); x; x = DECL_CHAIN (x))
  2198.       {
  2199.         if (last_x != NULL_TREE)
  2200.           TREE_CHAIN (last_x) = x;
  2201.         last_x = x;
  2202.       }
  2203.       }
  2204.  
  2205.     /* Put ctors and dtors at the front of the list.  */
  2206.     x = TREE_VEC_ELT (method_vec, 0);
  2207.     if (x)
  2208.       {
  2209.     while (DECL_CHAIN (x))
  2210.       {
  2211.         /* Let's avoid being circular about this.  */
  2212.         if (x == DECL_CHAIN (x))
  2213.           break;
  2214.         TREE_CHAIN (x) = DECL_CHAIN (x);
  2215.         x = DECL_CHAIN (x);
  2216.       }
  2217.     if (TREE_VEC_LENGTH (method_vec) > 1)
  2218.       TREE_CHAIN (x) = TREE_VEC_ELT (method_vec, 1);
  2219.     else
  2220.       TREE_CHAIN (x) = NULL_TREE;
  2221.       }
  2222.   }
  2223.  
  2224. #if 0
  2225.   TYPE_METHODS (t) = TREE_VEC_ELT (method_vec, 0)
  2226.     ? TREE_VEC_ELT (method_vec, 0) : TREE_VEC_ELT (method_vec, 1);
  2227. #else
  2228.   TYPE_METHODS (t) = method_vec;
  2229. #endif
  2230.  
  2231.   return method_vec;
  2232. }
  2233.  
  2234. /* Emit error when a duplicate definition of a type is seen.  Patch up. */
  2235.  
  2236. void
  2237. duplicate_tag_error (t)
  2238.      tree t;
  2239. {
  2240.   char *err_name;
  2241.   tree name = TYPE_NAME (t);
  2242.   if (TREE_CODE (name) == TYPE_DECL)
  2243.     name = DECL_NAME (name);
  2244.   err_name = IDENTIFIER_POINTER (name);
  2245.   if (TREE_CODE (t) == UNION_TYPE)
  2246.     error ("redefinition of `union %s'", err_name);
  2247.   else if (TREE_CODE (t) == RECORD_TYPE)
  2248.     error ("redefinition of `struct %s'", err_name);
  2249.   else
  2250.     error ("redefinition of tag %s", err_name);
  2251.  
  2252.   /* Pretend we haven't defined this type.  */
  2253.  
  2254.   /* All of the component_decl's were TREE_CHAINed together in the parser.
  2255.      finish_struct_methods walks these chains and assembles all methods with
  2256.      the same base name into DECL_CHAINs. Now we don't need the parser chains
  2257.      anymore, so we unravel them.
  2258.    */
  2259.   /*
  2260.    * This used to be in finish_struct, but it turns out that the
  2261.    * TREE_CHAIN is used by dbxout_type_methods and perhaps some other things...
  2262.    */
  2263.   if (CLASSTYPE_METHOD_VEC(t)) 
  2264.     {
  2265.       tree tv = CLASSTYPE_METHOD_VEC(t);
  2266.       int i, len  = TREE_VEC_LENGTH (tv);
  2267.       for (i = 0; i < len; i++)
  2268.     {
  2269.       tree unchain = TREE_VEC_ELT (tv, i);
  2270.       while(unchain != NULL_TREE) 
  2271.         {
  2272.           TREE_CHAIN (unchain) = NULL_TREE;
  2273.           unchain = DECL_CHAIN(unchain);
  2274.         }
  2275.     }
  2276.     }
  2277.  
  2278.   if (TYPE_LANG_SPECIFIC (t))
  2279.     {
  2280.       tree as_list = CLASSTYPE_AS_LIST (t);
  2281.       tree binfo = TYPE_BINFO (t);
  2282.       tree binfo_as_list = CLASSTYPE_BINFO_AS_LIST (t);
  2283.       int interface_only = CLASSTYPE_INTERFACE_ONLY (t);
  2284.       int interface_unknown = CLASSTYPE_INTERFACE_UNKNOWN (t);
  2285.  
  2286.       bzero (TYPE_LANG_SPECIFIC (t), sizeof (struct lang_type));
  2287.       BINFO_BASETYPES(binfo) = NULL_TREE;
  2288.  
  2289.       CLASSTYPE_AS_LIST (t) = as_list;
  2290.       TYPE_BINFO (t) = binfo;
  2291.       CLASSTYPE_BINFO_AS_LIST (t) = binfo_as_list;
  2292.       CLASSTYPE_INTERFACE_ONLY (t) = interface_only;
  2293.       SET_CLASSTYPE_INTERFACE_UNKNOWN_X (t, interface_unknown);
  2294.       CLASSTYPE_VBASE_SIZE (t) = integer_zero_node;
  2295.       TYPE_REDEFINED (t) = 1;
  2296.     }
  2297.   TYPE_SIZE (t) = NULL_TREE;
  2298.   TYPE_MODE (t) = VOIDmode;
  2299.   TYPE_FIELDS (t) = NULL_TREE;
  2300.   TYPE_METHODS (t) = NULL_TREE;
  2301.   TYPE_VFIELD (t) = NULL_TREE;
  2302.   TYPE_CONTEXT (t) = NULL_TREE;
  2303. }
  2304.  
  2305. /* Create a RECORD_TYPE or UNION_TYPE node for a C struct or union declaration
  2306.    (or C++ class declaration).
  2307.  
  2308.    For C++, we must handle the building of derived classes.
  2309.    Also, C++ allows static class members.  The way that this is
  2310.    handled is to keep the field name where it is (as the DECL_NAME
  2311.    of the field), and place the overloaded decl in the DECL_FIELD_BITPOS
  2312.    of the field.  layout_record and layout_union will know about this.
  2313.  
  2314.    More C++ hair: inline functions have text in their
  2315.    DECL_PENDING_INLINE_INFO nodes which must somehow be parsed into
  2316.    meaningful tree structure.  After the struct has been laid out, set
  2317.    things up so that this can happen.
  2318.  
  2319.    And still more: virtual functions.  In the case of single inheritance,
  2320.    when a new virtual function is seen which redefines a virtual function
  2321.    from the base class, the new virtual function is placed into
  2322.    the virtual function table at exactly the same address that
  2323.    it had in the base class.  When this is extended to multiple
  2324.    inheritance, the same thing happens, except that multiple virtual
  2325.    function tables must be maintained.  The first virtual function
  2326.    table is treated in exactly the same way as in the case of single
  2327.    inheritance.  Additional virtual function tables have different
  2328.    DELTAs, which tell how to adjust `this' to point to the right thing.
  2329.  
  2330.    LIST_OF_FIELDLISTS is just that.  The elements of the list are
  2331.    TREE_LIST elements, whose TREE_PURPOSE field tells what visibility
  2332.    the list has, and the TREE_VALUE slot gives the actual fields.
  2333.  
  2334.    If flag_all_virtual == 1, then we lay all functions into
  2335.    the virtual function table, as though they were declared
  2336.    virtual.  Constructors do not lay down in the virtual function table.
  2337.  
  2338.    If flag_all_virtual == 2, then we lay all functions into
  2339.    the virtual function table, such that virtual functions
  2340.    occupy a space by themselves, and then all functions
  2341.    of the class occupy a space by themselves.  This is illustrated
  2342.    in the following diagram:
  2343.  
  2344.    class A; class B : A;
  2345.  
  2346.     Class A's vtbl:            Class B's vtbl:
  2347.     --------------------------------------------------------------------
  2348.    | A's virtual functions|        | B's virtual functions        |
  2349.    |              |        | (may inherit some from A).    |
  2350.     --------------------------------------------------------------------
  2351.    | All of A's functions |        | All of A's functions        |
  2352.    | (such as a->A::f).      |        | (such as b->A::f)        |
  2353.     --------------------------------------------------------------------
  2354.                     | B's new virtual functions    |
  2355.                     | (not defined in A.)        |
  2356.                      -------------------------------
  2357.                     | All of B's functions        |
  2358.                     | (such as b->B::f)        |
  2359.                      -------------------------------
  2360.  
  2361.    this allows the program to make references to any function, virtual
  2362.    or otherwise in a type-consistent manner.  */
  2363.  
  2364. tree
  2365. finish_struct (t, list_of_fieldlists, warn_anon)
  2366.      tree t;
  2367.      tree list_of_fieldlists;
  2368.      int warn_anon;
  2369. {
  2370.   extern int interface_only, interface_unknown;
  2371.   extern tree EHS_type;
  2372.  
  2373.   int old;
  2374.   int round_up_size = 1;
  2375.  
  2376.   enum tree_code code = TREE_CODE (t);
  2377.   register tree x, last_x, method_vec;
  2378.   int needs_ctor = 0, needs_dtor = 0;
  2379.   int members_need_dtors, needs_virtual_dtor;
  2380.   tree name = TYPE_NAME (t), fields, fn_fields, tail;
  2381.   enum visibility_type visibility;
  2382.   int all_virtual;
  2383.   int has_virtual;
  2384.   int max_has_virtual;
  2385.   tree pending_virtuals = NULL_TREE;
  2386.   tree abstract_virtuals = NULL_TREE;
  2387.   tree vfield;
  2388.   tree vfields;
  2389.   int needs_default_ctor;
  2390.   int cant_have_default_ctor;
  2391.   int cant_have_const_ctor;
  2392.  
  2393.   /* The index of the first base class which has virtual
  2394.      functions.  Only applied to non-virtual baseclasses.  */
  2395.   int first_vfn_base_index;
  2396.  
  2397.   int n_baseclasses;
  2398.   int any_default_members = 0;
  2399.   char *err_name;
  2400.   int const_sans_init = 0;
  2401.   int ref_sans_init = 0;
  2402.   int nonprivate_method = 0;
  2403.   tree t_binfo = TYPE_BINFO (t);
  2404.  
  2405.   if (TREE_CODE (name) == TYPE_DECL)
  2406.     {
  2407. #if 0                /* Maybe later.  -jason  */
  2408.       struct tinst_level *til = tinst_for_decl();
  2409.  
  2410.       if (til)
  2411.     {
  2412.       DECL_SOURCE_FILE (name) = til->file;
  2413.       if (DECL_SOURCE_LINE (name))
  2414.         DECL_SOURCE_LINE (name) = til->line;
  2415.     }
  2416.       else
  2417. #endif
  2418.     {
  2419.       extern int lineno;
  2420.       
  2421.       DECL_SOURCE_FILE (name) = input_filename;
  2422.       /* For TYPE_DECL that are not typedefs (those marked with a line
  2423.          number of zero, we don't want to mark them as real typedefs.
  2424.          If this fails one needs to make sure real typedefs have a
  2425.          previous line number, even if it is wrong, that way the below
  2426.          will fill in the right line number.  (mrs) */
  2427.       if (DECL_SOURCE_LINE (name))
  2428.         DECL_SOURCE_LINE (name) = lineno;
  2429.     }
  2430.       name = DECL_NAME (name);
  2431.     }
  2432.   err_name = IDENTIFIER_POINTER (name);
  2433.  
  2434.   if (warn_anon && code != UNION_TYPE && ANON_AGGRNAME_P (name))
  2435.     {
  2436.       warning ("anonymous class type not used to declare any objects");
  2437.       err_name = "(anon)";
  2438.     }
  2439.  
  2440. #if 0
  2441.   /* This is set here, but it's never actually used anywhere.  (bpk) */
  2442.   leftmost_baseclasses = NULL_TREE;
  2443. #endif
  2444.   if (TYPE_SIZE (t))
  2445.     {
  2446.       if (TREE_CODE (t) == UNION_TYPE)
  2447.     error ("redefinition of `union %s'", err_name);
  2448.       else if (TREE_CODE (t) == RECORD_TYPE)
  2449.     error ("redefinition of `struct %s'", err_name);
  2450.       else
  2451.     my_friendly_abort (172);
  2452.       popclass (0);
  2453.       return t;
  2454.     }
  2455.  
  2456.   GNU_xref_decl (current_function_decl, t);
  2457.  
  2458.   /* If this type was previously laid out as a forward reference,
  2459.      make sure we lay it out again.  */
  2460.  
  2461.   TYPE_SIZE (t) = 0;
  2462.   CLASSTYPE_GOT_SEMICOLON (t) = 0;
  2463.   CLASSTYPE_INTERFACE_ONLY (t) = interface_only;
  2464.   SET_CLASSTYPE_INTERFACE_UNKNOWN_X (t, interface_unknown);
  2465.  
  2466.   if (flag_dossier)
  2467.     build_t_desc (t, 0);
  2468.  
  2469.   TYPE_BINFO (t) = NULL_TREE;
  2470.  
  2471.   old = suspend_momentary ();
  2472.  
  2473.   /* Install struct as DECL_FIELD_CONTEXT of each field decl.
  2474.      Also process specified field sizes.
  2475.      Set DECL_FIELD_SIZE to the specified size, or 0 if none specified.
  2476.      The specified size is found in the DECL_INITIAL.
  2477.      Store 0 there, except for ": 0" fields (so we can find them
  2478.      and delete them, below).  */
  2479.  
  2480.   if (t_binfo && BINFO_BASETYPES (t_binfo))
  2481.     n_baseclasses = TREE_VEC_LENGTH (BINFO_BASETYPES (t_binfo));
  2482.   else
  2483.     n_baseclasses = 0;
  2484.  
  2485.   if (n_baseclasses > 0)
  2486.     {
  2487.       struct base_info base_info;
  2488.  
  2489.       /* If using multiple inheritance, this may cause variants of our
  2490.      basetypes to be used (instead of their canonical forms).  */
  2491.       fields = layout_basetypes (t, BINFO_BASETYPES (t_binfo));
  2492.       last_x = tree_last (fields);
  2493.  
  2494.       first_vfn_base_index = finish_base_struct (t, &base_info,
  2495.                          BINFO_BASETYPES (t_binfo));
  2496.       has_virtual = base_info.has_virtual;
  2497.       max_has_virtual = base_info.max_has_virtual;
  2498.       CLASSTYPE_N_SUPERCLASSES (t) += base_info.n_ancestors;
  2499.       vfield = base_info.vfield;
  2500.       vfields = base_info.vfields;
  2501.       needs_default_ctor = base_info.needs_default_ctor;
  2502.       cant_have_default_ctor = base_info.cant_have_default_ctor;
  2503.       cant_have_const_ctor = base_info.cant_have_const_ctor;
  2504.       members_need_dtors = base_info.members_need_dtors;
  2505.       needs_virtual_dtor = base_info.needs_virtual_dtor;
  2506.       n_baseclasses = TREE_VEC_LENGTH (BINFO_BASETYPES (t_binfo));
  2507.     }
  2508.   else
  2509.     {
  2510.       first_vfn_base_index = -1;
  2511.       has_virtual = 0;
  2512.       max_has_virtual = has_virtual;
  2513.       vfield = NULL_TREE;
  2514.       vfields = NULL_TREE;
  2515.       fields = NULL_TREE;
  2516.       last_x = NULL_TREE;
  2517.       needs_default_ctor = 0;
  2518.       cant_have_default_ctor = 0;
  2519.       cant_have_const_ctor = 0;
  2520.       members_need_dtors = 0;
  2521.       needs_virtual_dtor = 0;
  2522.     }
  2523.  
  2524.   if (write_virtuals == 3 && CLASSTYPE_INTERFACE_KNOWN (t)
  2525.       && current_lang_name == lang_name_cplusplus)
  2526.     {
  2527.       CLASSTYPE_INTERFACE_ONLY (t) = interface_only;
  2528.       CLASSTYPE_VTABLE_NEEDS_WRITING (t) = ! interface_only;
  2529.     }
  2530.  
  2531.   /* The three of these are approximations which may later be
  2532.      modified.  Needed at this point to make add_virtual_function
  2533.      and modify_vtable_entries work.  */
  2534.   TREE_CHAIN (t_binfo) = TYPE_BINFO (t);
  2535.   TYPE_BINFO (t) = t_binfo;
  2536.   CLASSTYPE_VFIELDS (t) = vfields;
  2537.   CLASSTYPE_VFIELD (t) = vfield;
  2538.  
  2539.   fn_fields = NULL_TREE;
  2540.   tail = NULL_TREE;
  2541.   if (last_x && list_of_fieldlists)
  2542.     TREE_CHAIN (last_x) = TREE_VALUE (list_of_fieldlists);
  2543.  
  2544.   if (flag_all_virtual == 1 && TYPE_OVERLOADS_METHOD_CALL_EXPR (t))
  2545.     all_virtual = 1;
  2546.   else
  2547.     all_virtual = 0;
  2548.  
  2549.   if (CLASSTYPE_DECLARED_CLASS (t) == 0)
  2550.     {
  2551.       nonprivate_method = 1;
  2552.       if (list_of_fieldlists
  2553.       && TREE_PURPOSE (list_of_fieldlists) == (tree)visibility_default)
  2554.     TREE_PURPOSE (list_of_fieldlists) = (tree)visibility_public;
  2555.     }
  2556.   else if (list_of_fieldlists
  2557.        && TREE_PURPOSE (list_of_fieldlists) == (tree)visibility_default)
  2558.     TREE_PURPOSE (list_of_fieldlists) = (tree)visibility_private;
  2559.  
  2560.   while (list_of_fieldlists)
  2561.     {
  2562.       visibility = (enum visibility_type)TREE_PURPOSE (list_of_fieldlists);
  2563.  
  2564.       for (x = TREE_VALUE (list_of_fieldlists); x; x = TREE_CHAIN (x))
  2565.     {
  2566.       TREE_PRIVATE (x) = visibility == visibility_private;
  2567.       TREE_PROTECTED (x) = visibility == visibility_protected;
  2568.       GNU_xref_member (current_class_name, x);
  2569.  
  2570.           if (TREE_CODE (x) == TYPE_DECL
  2571.               && TREE_CODE (TREE_TYPE (x)) == RECORD_TYPE)
  2572.             {
  2573.           /* Make sure we set this up.  In find_scoped_type, it explicitly
  2574.          looks for a TYPE_DECL in the TYPE_FIELDS list.  If we don't
  2575.          do this here, we'll miss including this TYPE_DECL in the
  2576.          list.  */
  2577.           if (! fields)
  2578.         fields = x;
  2579.           last_x = x;
  2580.           DECL_CONTEXT (x) = t;
  2581.           continue;
  2582.         }
  2583.  
  2584.  
  2585.       if (TREE_CODE (x) == FUNCTION_DECL)
  2586.         {
  2587.           /* Clear out this flag.
  2588.  
  2589.              @@ Doug may figure out how to break
  2590.          @@ this with nested classes and friends.  */
  2591.           DECL_IN_AGGR_P (x) = 0;
  2592.  
  2593.           nonprivate_method |= ! TREE_PRIVATE (x);
  2594.  
  2595.           /* If this was an evil function, don't keep it in class.  */
  2596.           if (IDENTIFIER_ERROR_LOCUS (DECL_ASSEMBLER_NAME (x)))
  2597.         continue;
  2598.  
  2599.           if (last_x)
  2600.         TREE_CHAIN (last_x) = TREE_CHAIN (x);
  2601.           if (! fn_fields)
  2602.         fn_fields = x;
  2603.           else
  2604.         TREE_CHAIN (tail) = x;
  2605.           tail = x;
  2606.  
  2607. #if 0
  2608.           /* ??? What if we have duplicate declarations
  2609.          in T's definition?  */
  2610.           if (DECL_CLASS_CONTEXT (x))
  2611.         continue;
  2612. #endif
  2613.           DECL_CLASS_CONTEXT (x) = t;
  2614.  
  2615.           DECL_FIELD_SIZE (x) = 0;
  2616.  
  2617.           /* The name of the field is the original field name
  2618.          Save this in auxiliary field for later overloading.  */
  2619.           if (DECL_VINDEX (x)
  2620.           || (all_virtual == 1 && ! DECL_CONSTRUCTOR_P (x)))
  2621.         {
  2622.           pending_virtuals = add_virtual_function (pending_virtuals,
  2623.                                &has_virtual, x, t);
  2624.           if (DECL_ABSTRACT_VIRTUAL_P (x))
  2625.             abstract_virtuals = tree_cons (NULL_TREE, x, abstract_virtuals);
  2626.         }
  2627.           continue;
  2628.         }
  2629.  
  2630.       /* Handle visibility declarations.  */
  2631.       if (DECL_NAME (x) && TREE_CODE (DECL_NAME (x)) == SCOPE_REF)
  2632.         {
  2633.           tree fdecl = TREE_OPERAND (DECL_NAME (x), 1);
  2634.  
  2635.           if (last_x)
  2636.         TREE_CHAIN (last_x) = TREE_CHAIN (x);
  2637.           /* Make type T see field decl FDECL with visibility VISIBILITY.*/
  2638.           if (TREE_CODE (fdecl) == TREE_LIST)
  2639.         {
  2640.           fdecl = TREE_VALUE (fdecl);
  2641.           while (fdecl)
  2642.             {
  2643.               if (alter_visibility (t, fdecl, visibility) == 0)
  2644.             break;
  2645.               fdecl = DECL_CHAIN (fdecl);
  2646.             }
  2647.         }
  2648.           else
  2649.         alter_visibility (t, fdecl, visibility);
  2650.           CLASSTYPE_ALTERS_VISIBILITIES_P (t) = 1;
  2651.           continue;
  2652.         }
  2653.  
  2654.       /* If this is of reference type, check if it needs an init.  Also
  2655.          do a little ANSI jig if necessary.  */
  2656.       if (TREE_CODE (x) != TYPE_DECL
  2657.           && TREE_CODE (TREE_TYPE (x)) == REFERENCE_TYPE)
  2658.         {
  2659.           if (DECL_INITIAL (x) == NULL_TREE)
  2660.         ref_sans_init = 1;
  2661.  
  2662.           /* ARM $12.6.2: [A member initializer list] is the only
  2663.          way to initialize a nonstatic const and reference
  2664.          [member].  */
  2665.           if (! TYPE_HAS_CONSTRUCTOR (t) && !TREE_STATIC (x))
  2666.         {
  2667.           if (DECL_NAME (x))
  2668.             cp_pedwarn_at ("non-static reference `%D' in class without a constructor", x);
  2669.           else
  2670.             cp_pedwarn_at ("non-static reference in class without a constructor", x);
  2671.         }
  2672.         }
  2673.  
  2674.       /* ``A local class cannot have static data members.'' ARM 9.4 */
  2675.       if (current_function_decl && TREE_STATIC (x))
  2676.         cp_error ("field `%D' in local class cannot be static", x);
  2677.  
  2678.       /* When this goes into scope, it will be a non-local reference.  */
  2679.       DECL_NONLOCAL (x) = 1;
  2680.  
  2681.       /* Perform error checking that did not get done in grokdeclarator.  */
  2682.       if (TREE_CODE (x) == FIELD_DECL || TREE_CODE (x) == VAR_DECL)
  2683.         {
  2684.           if (TREE_CODE (TREE_TYPE (x)) == FUNCTION_TYPE)
  2685.         {
  2686.           cp_error ("field `%D' invalidly declared function type",
  2687.                   x);
  2688.           TREE_TYPE (x) = build_pointer_type (TREE_TYPE (x));
  2689.         }
  2690.           else if (TREE_CODE (TREE_TYPE (x)) == METHOD_TYPE)
  2691.         {
  2692.           cp_error ("field `%D' invalidly declared method type", x);
  2693.           TREE_TYPE (x) = build_pointer_type (TREE_TYPE (x));
  2694.         }
  2695.           else if (TREE_CODE (TREE_TYPE (x)) == OFFSET_TYPE)
  2696.         {
  2697.           cp_error ("field `%D' invalidly declared offset type", x);
  2698.           TREE_TYPE (x) = build_pointer_type (TREE_TYPE (x));
  2699.         }
  2700.         }
  2701.  
  2702.       if (TREE_CODE (x) == FIELD_DECL)
  2703.         {
  2704.           /* If the field has a bogus type, don't bother with it.  */
  2705.           if (TREE_TYPE (x) != error_mark_node)
  2706.         {
  2707.           /* Never let anything with uninheritable virtuals
  2708.              make it through without complaint.  */
  2709.           if (TYPE_LANG_SPECIFIC (TREE_TYPE (x))
  2710.               && CLASSTYPE_ABSTRACT_VIRTUALS (TREE_TYPE (x)))
  2711.             abstract_virtuals_error (x, TREE_TYPE (x));
  2712.  
  2713.           if (TYPE_LANG_SPECIFIC (TREE_TYPE (x)))
  2714.             {
  2715.               /* It's possible that the type does have a default
  2716.              constructor, *and* have GETS_INIT_REF set, if
  2717.              the class has a non-const copy constructor.  */
  2718.               if (TYPE_HAS_DEFAULT_CONSTRUCTOR (TREE_TYPE (x)))
  2719.             needs_default_ctor = 1;
  2720.               if (TYPE_GETS_INIT_REF (TREE_TYPE (x))
  2721.               && !TYPE_GETS_CONST_INIT_REF (TREE_TYPE (x)))
  2722.             cant_have_const_ctor = 1;
  2723.             }
  2724.           else if (DECL_INITIAL (x) == NULL_TREE
  2725.                && (TYPE_HAS_CONSTRUCTOR (TREE_TYPE (x))
  2726.                    || TREE_CODE (TREE_TYPE (x)) == REFERENCE_TYPE))
  2727.             cant_have_default_ctor = 1;
  2728.         }
  2729.  
  2730.           /* If any field is const, the structure type is pseudo-const.  */
  2731.           if (TREE_READONLY (x))
  2732.         {
  2733.           C_TYPE_FIELDS_READONLY (t) = 1;
  2734.           if (DECL_INITIAL (x) == NULL_TREE)
  2735.             const_sans_init = 1;
  2736.  
  2737.           /* ARM $12.6.2: [A member initializer list] is the only
  2738.              way to initialize a nonstatic const and reference
  2739.              [member].  */
  2740.           if (! TYPE_HAS_CONSTRUCTOR (t) && !TREE_STATIC (x))
  2741.             {
  2742.               if (DECL_NAME (x))
  2743.             cp_pedwarn_at ("non-static const member `%D' in class without a constructor", x);
  2744.               else
  2745.             cp_pedwarn_at ("non-static const member in class without a constructor", x);
  2746.             }
  2747.         }
  2748.           else
  2749.         {
  2750.           /* A field that is pseudo-const makes the structure likewise.  */
  2751.           tree t1 = TREE_TYPE (x);
  2752.           while (TREE_CODE (t1) == ARRAY_TYPE)
  2753.             t1 = TREE_TYPE (t1);
  2754.           if (IS_AGGR_TYPE (t1))
  2755.             {
  2756.               if (C_TYPE_FIELDS_READONLY (t1))
  2757.             C_TYPE_FIELDS_READONLY (t) = 1;
  2758.               if (CLASSTYPE_READONLY_FIELDS_NEED_INIT (t1))
  2759.             const_sans_init = 1;
  2760.             }
  2761.         }
  2762.         }
  2763.       else if (TREE_CODE (x) == VAR_DECL && TREE_CODE (t) == UNION_TYPE)
  2764.         /* Unions cannot have static members.  */
  2765.         cp_error ("field `%D' declared static in union", x);
  2766.  
  2767.       if (! fields)
  2768.         fields = x;
  2769.       DECL_FIELD_CONTEXT (x) = t;
  2770.  
  2771.       /* We could be making an extern "C" function a friend. */
  2772.       if (TREE_CODE (x) == FUNCTION_DECL
  2773.           && DECL_LANG_SPECIFIC (x)
  2774.           && DECL_VIRTUAL_P (x))
  2775.         DECL_CLASS_CONTEXT (x) = t;
  2776.  
  2777.       DECL_FIELD_SIZE (x) = 0;
  2778.  
  2779.       /* We set DECL_BIT_FIELD tentatively in grokbitfield.
  2780.          If the type and width are valid, we'll keep it set.
  2781.          Otherwise, the flag is cleared.  */
  2782.       if (DECL_BIT_FIELD (x))
  2783.         {
  2784.           DECL_BIT_FIELD (x) = 0;
  2785.           /* Invalid bit-field size done by grokfield.  */
  2786.           /* Detect invalid bit-field type.  */
  2787.           if (DECL_INITIAL (x)
  2788.           && TREE_CODE (TREE_TYPE (x)) != INTEGER_TYPE
  2789.           && TREE_CODE (TREE_TYPE (x)) != ENUMERAL_TYPE)
  2790.         {
  2791.           cp_error ("bit-field `%D' has invalid type", x);
  2792.           DECL_INITIAL (x) = NULL;
  2793.         }
  2794.  
  2795.           /* Detect and ignore out of range field width.  */
  2796.           if (DECL_INITIAL (x))
  2797.         {
  2798.           register int width = TREE_INT_CST_LOW (DECL_INITIAL (x));
  2799.  
  2800.           if (width < 0)
  2801.             {
  2802.               DECL_INITIAL (x) = NULL;
  2803.               cp_error ("negative width in bit-field `%D'", x);
  2804.             }
  2805.           else if (width == 0 && DECL_NAME (x) != 0)
  2806.             {
  2807.               DECL_INITIAL (x) = NULL;
  2808.               cp_error ("zero width for bit-field `%D'", x);
  2809.             }
  2810.           else if ((unsigned)width > TYPE_PRECISION (TREE_TYPE (x)))
  2811.             {
  2812.               DECL_INITIAL (x) = NULL;
  2813.               cp_error ("width of `%D' exceeds its type", x);
  2814.             }
  2815.         }
  2816.  
  2817.           /* Process valid field width.  */
  2818.           if (DECL_INITIAL (x))
  2819.         {
  2820.           register int width = TREE_INT_CST_LOW (DECL_INITIAL (x));
  2821.  
  2822.           if (width == 0)
  2823.             {
  2824. #ifdef EMPTY_FIELD_BOUNDARY
  2825.               /* field size 0 => mark following field as "aligned" */
  2826.               if (TREE_CHAIN (x))
  2827.             DECL_ALIGN (TREE_CHAIN (x))
  2828.               = MAX (DECL_ALIGN (TREE_CHAIN (x)), EMPTY_FIELD_BOUNDARY);
  2829.               /* field of size 0 at the end => round up the size.  */
  2830.               else
  2831.             round_up_size = EMPTY_FIELD_BOUNDARY;
  2832. #endif
  2833. #ifdef PCC_BITFIELD_TYPE_MATTERS
  2834.               DECL_ALIGN (x) = MAX (DECL_ALIGN (x),
  2835.                         TYPE_ALIGN (TREE_TYPE (x)));
  2836. #endif
  2837.             }
  2838.           else
  2839.             {
  2840.               DECL_INITIAL (x) = NULL_TREE;
  2841.               DECL_FIELD_SIZE (x) = width;
  2842.               DECL_BIT_FIELD (x) = 1;
  2843.               /* Traditionally a bit field is unsigned
  2844.              even if declared signed.  */
  2845.               if (flag_traditional
  2846.               && TREE_CODE (TREE_TYPE (x)) == INTEGER_TYPE)
  2847.             TREE_TYPE (x) = unsigned_type_node;
  2848.             }
  2849.         }
  2850.           else
  2851.         /* Non-bit-fields are aligned for their type.  */
  2852.         DECL_ALIGN (x) = MAX (DECL_ALIGN (x), TYPE_ALIGN (TREE_TYPE (x)));
  2853.         }
  2854.       else if (TREE_CODE (x) == FIELD_DECL)
  2855.         {
  2856.           tree type = TREE_TYPE (x);
  2857.           if (TREE_CODE (type) == ARRAY_TYPE)
  2858.         type = TREE_TYPE (type);
  2859.           if (code == UNION_TYPE && IS_AGGR_TYPE (type))
  2860.         {
  2861.           if (TYPE_NEEDS_CONSTRUCTING (type)
  2862.               || TYPE_NEEDS_DESTRUCTOR (type))
  2863.             cp_error ("member `%D' with constructor or destructor not allowed in union", x);
  2864.           TYPE_GETS_ASSIGNMENT (t) |= TYPE_GETS_ASSIGNMENT (type);
  2865.           TYPE_GETS_INIT_REF (t) |= TYPE_GETS_INIT_REF (type);
  2866.         }
  2867.           else if (code == RECORD_TYPE)
  2868.         {
  2869.           /* Array of record type doesn't matter for this bit.  */
  2870.           TYPE_NEEDS_CONSTRUCTING (t) |= TYPE_NEEDS_CONSTRUCTING (type);
  2871.           if (IS_AGGR_TYPE (type))
  2872.             {
  2873.               needs_ctor |= TYPE_NEEDS_CONSTRUCTOR (type);
  2874.               needs_dtor |= TYPE_NEEDS_DESTRUCTOR (type);
  2875.               members_need_dtors |= TYPE_NEEDS_DESTRUCTOR (type);
  2876.               TYPE_GETS_CONST_INIT_REF (t) |= TYPE_GETS_CONST_INIT_REF (type);
  2877.               TYPE_GETS_ASSIGNMENT (t) |= TYPE_GETS_ASSIGNMENT (type);
  2878.               TYPE_GETS_INIT_REF (t) |= TYPE_GETS_INIT_REF (type);
  2879.             }
  2880.         }
  2881.           if (DECL_INITIAL (x) != NULL_TREE)
  2882.         {
  2883.           /* `build_class_init_list' does not recognize non-FIELD_DECLs.  */
  2884.           if (code == UNION_TYPE && any_default_members != 0)
  2885.             error ("multiple fields in union initialized");
  2886.           any_default_members = 1;
  2887.         }
  2888.         }
  2889.       last_x = x;
  2890.     }
  2891.       list_of_fieldlists = TREE_CHAIN (list_of_fieldlists);
  2892.       /* link the tail while we have it! */
  2893.       if (last_x)
  2894.     {
  2895.       TREE_CHAIN (last_x) = NULL_TREE;
  2896.  
  2897.       if (list_of_fieldlists
  2898.           && TREE_VALUE (list_of_fieldlists)
  2899.           && TREE_CODE (TREE_VALUE (list_of_fieldlists)) != FUNCTION_DECL)
  2900.         TREE_CHAIN (last_x) = TREE_VALUE (list_of_fieldlists);
  2901.     }
  2902.     }
  2903.  
  2904.   if (tail) TREE_CHAIN (tail) = NULL_TREE;
  2905.  
  2906.   /* If this type has any constant members which did not come
  2907.      with their own initialization, mark that fact here.  It is
  2908.      not an error here, since such types can be saved either by their
  2909.      constructors, or by fortuitous initialization.  */
  2910.   CLASSTYPE_READONLY_FIELDS_NEED_INIT (t) = const_sans_init;
  2911.   CLASSTYPE_REF_FIELDS_NEED_INIT (t) = ref_sans_init;
  2912.   CLASSTYPE_ABSTRACT_VIRTUALS (t) = abstract_virtuals;
  2913.  
  2914.   if (members_need_dtors && !TYPE_HAS_DESTRUCTOR (t))
  2915.     {
  2916.       /* Here we must cons up a destructor on the fly.  */
  2917.       tree dtor = cons_up_default_function (t, name, fields,
  2918.                         needs_virtual_dtor != 0);
  2919.  
  2920.       /* If we couldn't make it work, then pretend we didn't need it.  */
  2921.       if (dtor == void_type_node)
  2922.     TYPE_NEEDS_DESTRUCTOR (t) = 0;
  2923.       else
  2924.     {
  2925.       if (! fn_fields)
  2926.         fn_fields = dtor;
  2927.       else
  2928.         TREE_CHAIN (tail) = dtor;
  2929.       tail = dtor;
  2930.  
  2931.       if (DECL_VINDEX (dtor) == NULL_TREE
  2932.           && ! CLASSTYPE_DECLARED_EXCEPTION (t)
  2933.           && (needs_virtual_dtor
  2934.           || pending_virtuals != NULL_TREE
  2935.           || pending_hard_virtuals != NULL_TREE))
  2936.         DECL_VINDEX (dtor) = error_mark_node;
  2937.       if (DECL_VINDEX (dtor))
  2938.         pending_virtuals = add_virtual_function (pending_virtuals,
  2939.                              &has_virtual, dtor, NULL_TREE);
  2940.       nonprivate_method = 1;
  2941.       TYPE_HAS_DESTRUCTOR (t) = 1;
  2942.     }
  2943.     }
  2944.  
  2945.   /* Create default constructor, if needed.  */
  2946.  
  2947.   /* ARM $12.1: A default constructor will be generated for a class X
  2948.      only if no constructor has been declared for class X.  So we
  2949.      check TYPE_HAS_CONSTRUCTOR also, to make sure we don't generate
  2950.      one if they declared a constructor in this class.  */
  2951.   if (! TYPE_HAS_DEFAULT_CONSTRUCTOR (t)
  2952.       && ! TYPE_HAS_CONSTRUCTOR (t)
  2953.       && needs_default_ctor && ! cant_have_default_ctor)
  2954.     {
  2955.       tree default_fn = cons_up_default_function (t, name, fields, 2);
  2956.       TREE_CHAIN (default_fn) = fn_fields;
  2957.       fn_fields = default_fn;
  2958.       TYPE_HAS_DEFAULT_CONSTRUCTOR (t) = 1;
  2959.       nonprivate_method = 1;
  2960.     }
  2961.  
  2962.   /* Create default copy constructor, if needed.  Don't do it for
  2963.      the exception handler.  */
  2964.   if ((TYPE_NEEDS_CONSTRUCTOR (t) || TYPE_HAS_CONSTRUCTOR (t) || needs_ctor)
  2965.       && ! TYPE_HAS_INIT_REF (t) && t != EHS_type)
  2966.     {
  2967.       /* ARM 12.18: You get either X(X&) or X(const X&), but
  2968.      not both.  --Chip  */
  2969.       tree default_fn =
  2970.     cons_up_default_function (t, name, fields,
  2971.                   cant_have_const_ctor ? 4 : 3);
  2972.       TREE_CHAIN (default_fn) = fn_fields;
  2973.       fn_fields = default_fn;
  2974.       TYPE_HAS_INIT_REF (t) = 1;
  2975.       nonprivate_method = 1;
  2976.     }
  2977.  
  2978.   if (fn_fields)
  2979.     {
  2980.       method_vec = finish_struct_methods (t, fn_fields, nonprivate_method);
  2981.  
  2982.       if (TYPE_HAS_CONSTRUCTOR (t)
  2983.       && ! CLASSTYPE_DECLARED_EXCEPTION (t)
  2984.       && CLASSTYPE_FRIEND_CLASSES (t) == NULL_TREE
  2985.       && DECL_FRIENDLIST (TYPE_NAME (t)) == NULL_TREE)
  2986.     {
  2987.       int nonprivate_ctor = 0;
  2988.       tree ctor;
  2989.  
  2990.       for (ctor = TREE_VEC_ELT (method_vec, 0);
  2991.            ctor;
  2992.            ctor = DECL_CHAIN (ctor))
  2993.         if (! TREE_PRIVATE (ctor))
  2994.           {
  2995.         nonprivate_ctor = 1;
  2996.         break;
  2997.           }
  2998.  
  2999.       if (nonprivate_ctor == 0 && warn_ctor_dtor_privacy)
  3000.         warning ("class `%s' only defines private constructors and has no friends",
  3001.              err_name);
  3002.     }
  3003.     }
  3004.   else
  3005.     {
  3006.       method_vec = 0;
  3007.  
  3008.       /* Just in case these got accidentally
  3009.      filled in by syntax errors.  */
  3010.       TYPE_HAS_CONSTRUCTOR (t) = 0;
  3011.       TYPE_HAS_DESTRUCTOR (t) = 0;
  3012.     }
  3013.  
  3014.   if (vfield == NULL_TREE && has_virtual)
  3015.     {
  3016.       /* We build this decl with ptr_type_node, and
  3017.      change the type when we know what it should be.  */
  3018.       vfield = build_lang_field_decl (FIELD_DECL, get_vfield_name (t),
  3019.                       ptr_type_node);
  3020.       /* If you change any of the below, take a look at all the
  3021.      other VFIELD_BASEs and VTABLE_BASEs in the code, and change
  3022.      them too. */
  3023.       DECL_ASSEMBLER_NAME (vfield) = get_identifier (VFIELD_BASE);
  3024.       CLASSTYPE_VFIELD (t) = vfield;
  3025.       DECL_VIRTUAL_P (vfield) = 1;
  3026.       DECL_FIELD_CONTEXT (vfield) = t;
  3027.       DECL_CLASS_CONTEXT (vfield) = t;
  3028.       DECL_FCONTEXT (vfield) = t;
  3029.       DECL_FIELD_SIZE (vfield) = 0;
  3030.       DECL_ALIGN (vfield) = TYPE_ALIGN (ptr_type_node);
  3031.       if (CLASSTYPE_DOSSIER (t))
  3032.     {
  3033.       /* vfield is always first entry in structure.  */
  3034.       TREE_CHAIN (vfield) = fields;
  3035.       fields = vfield;
  3036.     }
  3037.       else if (last_x)
  3038.     {
  3039.       my_friendly_assert (TREE_CHAIN (last_x) == 0, 175);
  3040.       TREE_CHAIN (last_x) = vfield;
  3041.       last_x = vfield;
  3042.     }
  3043.       else
  3044.     fields = vfield;
  3045.       vfields = chainon (vfields, CLASSTYPE_AS_LIST (t));
  3046.     }
  3047.  
  3048.   /* Now DECL_INITIAL is null on all members except for zero-width bit-fields.
  3049.      And they have already done their work.
  3050.  
  3051.      C++: maybe we will support default field initialization some day...  */
  3052.  
  3053.   /* Delete all zero-width bit-fields from the front of the fieldlist */
  3054.   while (fields && DECL_BIT_FIELD (fields)
  3055.      && DECL_INITIAL (fields))
  3056.     fields = TREE_CHAIN (fields);
  3057.   /* Delete all such fields from the rest of the fields.  */
  3058.   for (x = fields; x;)
  3059.     {
  3060.       if (TREE_CHAIN (x) && DECL_BIT_FIELD (TREE_CHAIN (x))
  3061.       && DECL_INITIAL (TREE_CHAIN (x)))
  3062.     TREE_CHAIN (x) = TREE_CHAIN (TREE_CHAIN (x));
  3063.       else
  3064.     x = TREE_CHAIN (x);
  3065.     }
  3066.   /* Delete all duplicate fields from the fields */
  3067.   delete_duplicate_fields (fields);
  3068.  
  3069.   /* Now we have the final fieldlist for the data fields.  Record it,
  3070.      then lay out the structure or union (including the fields).  */
  3071.  
  3072.   TYPE_FIELDS (t) = fields;
  3073.  
  3074.   /* If there's a :0 field at the end, round the size to the
  3075.      EMPTY_FIELD_BOUNDARY.  */
  3076.   TYPE_ALIGN (t) = round_up_size;
  3077.  
  3078.   /* Pass layout information about base classes to layout_type, if any.  */
  3079.  
  3080.   {
  3081.     tree field;
  3082.     for (field = TYPE_FIELDS (t); field; field = TREE_CHAIN (field))
  3083.       {
  3084.     if (TREE_STATIC (field))
  3085.       continue;
  3086.     if (TREE_CODE (field) != FIELD_DECL)
  3087.       continue;
  3088.  
  3089.     /* If this field is an anonymous union,
  3090.        give each union-member the same position as the union has.
  3091.  
  3092.        ??? This is a real kludge because it makes the structure
  3093.        of the types look strange.  This feature is only used by
  3094.        C++, which should have build_component_ref build two
  3095.        COMPONENT_REF operations, one for the union and one for
  3096.        the inner field.  We set the offset of this field to zero
  3097.        so that either the old or the correct method will work.
  3098.        Setting DECL_FIELD_CONTEXT is wrong unless the inner fields are
  3099.        moved into the type of this field, but nothing seems to break
  3100.        by doing this.  */
  3101.  
  3102.     if (DECL_NAME (field) == 0
  3103.         && TREE_CODE (TREE_TYPE (field)) == UNION_TYPE)
  3104.       {
  3105.         tree uelt = TYPE_FIELDS (TREE_TYPE (field));
  3106.         for (; uelt; uelt = TREE_CHAIN (uelt))
  3107.           {
  3108.         DECL_FIELD_CONTEXT (uelt) = DECL_FIELD_CONTEXT (field);
  3109.         DECL_FIELD_BITPOS (uelt) = DECL_FIELD_BITPOS (field);
  3110.           }
  3111.  
  3112.         DECL_FIELD_BITPOS (field) = integer_zero_node;
  3113.       }
  3114.       }
  3115.   }
  3116.  
  3117.   if (n_baseclasses)
  3118.     {
  3119.       tree pseudo_basetype = TREE_TYPE (base_layout_decl);
  3120.  
  3121.       TREE_CHAIN (base_layout_decl) = TYPE_FIELDS (t);
  3122.       TYPE_FIELDS (t) = base_layout_decl;
  3123.  
  3124.       TYPE_SIZE (pseudo_basetype) = CLASSTYPE_SIZE (t);
  3125.       TYPE_MODE (pseudo_basetype) = TYPE_MODE (t);
  3126.       TYPE_ALIGN (pseudo_basetype) = CLASSTYPE_ALIGN (t);
  3127.       DECL_ALIGN (base_layout_decl) = TYPE_ALIGN (pseudo_basetype);
  3128.       /* Don't re-use old size. */
  3129.       DECL_SIZE (base_layout_decl) = 0;
  3130.     }
  3131.  
  3132.   layout_type (t);
  3133.  
  3134.   {
  3135.     tree field;
  3136.     for (field = TYPE_FIELDS (t); field; field = TREE_CHAIN (field))
  3137.       {
  3138.     if (TREE_STATIC (field))
  3139.       continue;
  3140.     if (TREE_CODE (field) != FIELD_DECL)
  3141.       continue;
  3142.  
  3143.     /* If this field is an anonymous union,
  3144.        give each union-member the same position as the union has.
  3145.  
  3146.        ??? This is a real kludge because it makes the structure
  3147.        of the types look strange.  This feature is only used by
  3148.        C++, which should have build_component_ref build two
  3149.        COMPONENT_REF operations, one for the union and one for
  3150.        the inner field.  We set the offset of this field to zero
  3151.        so that either the old or the correct method will work.
  3152.        Setting DECL_FIELD_CONTEXT is wrong unless the inner fields are
  3153.        moved into the type of this field, but nothing seems to break
  3154.        by doing this.  */
  3155.  
  3156.     if (DECL_NAME (field) == 0
  3157.         && TREE_CODE (TREE_TYPE (field)) == UNION_TYPE)
  3158.       {
  3159.         tree uelt = TYPE_FIELDS (TREE_TYPE (field));
  3160.         for (; uelt; uelt = TREE_CHAIN (uelt))
  3161.           {
  3162.         DECL_FIELD_CONTEXT (uelt) = DECL_FIELD_CONTEXT (field);
  3163.         DECL_FIELD_BITPOS (uelt) = DECL_FIELD_BITPOS (field);
  3164.           }
  3165.  
  3166.         DECL_FIELD_BITPOS (field) = integer_zero_node;
  3167.       }
  3168.       }
  3169.   }
  3170.  
  3171.   if (n_baseclasses)
  3172.     TYPE_FIELDS (t) = TREE_CHAIN (TYPE_FIELDS (t));
  3173.  
  3174.   /* C++: do not let empty structures exist.  */
  3175.   if (integer_zerop (TYPE_SIZE (t)))
  3176.     TYPE_SIZE (t) = TYPE_SIZE (char_type_node);
  3177.  
  3178.   /* Set the TYPE_DECL for this type to contain the right
  3179.      value for DECL_OFFSET, so that we can use it as part
  3180.      of a COMPONENT_REF for multiple inheritance.  */
  3181.  
  3182.   if (TREE_CODE (TYPE_NAME (t)) == TYPE_DECL)
  3183.     layout_decl (TYPE_NAME (t), 0);
  3184.  
  3185.   /* Now fix up any virtual base class types that we
  3186.      left lying around.  We must get these done
  3187.      before we try to lay out the virtual function table.  */
  3188.   doing_hard_virtuals = 1;
  3189.   pending_hard_virtuals = nreverse (pending_hard_virtuals);
  3190.  
  3191.   if (TYPE_USES_VIRTUAL_BASECLASSES (t))
  3192.     {
  3193.       tree vbases;
  3194.  
  3195.       max_has_virtual = layout_vbasetypes (t, max_has_virtual);
  3196.       vbases = CLASSTYPE_VBASECLASSES (t);
  3197.       CLASSTYPE_N_VBASECLASSES (t) = list_length (vbases);
  3198.  
  3199.       /* This loop makes all the entries in the virtual function tables
  3200.      of interest contain the "latest" version of the functions
  3201.      we have defined.  */
  3202.  
  3203.       while (vbases)
  3204.     {
  3205.       tree virtuals = BINFO_VIRTUALS (vbases);
  3206.  
  3207.       if (virtuals)
  3208.         {
  3209.           /* Get past the `null' vtable entry...  */
  3210.           virtuals = TREE_CHAIN (virtuals);
  3211.           /* and the `dossier' vtable entry if we're doing dossiers.  */
  3212.           if (flag_dossier)
  3213.         virtuals = TREE_CHAIN (virtuals);
  3214.         }
  3215.  
  3216.       while (virtuals != NULL_TREE)
  3217.         {
  3218.           tree pfn = FNADDR_FROM_VTABLE_ENTRY (TREE_VALUE (virtuals));
  3219.           tree base_fndecl = TREE_OPERAND (pfn, 0);
  3220.           tree decl = get_first_matching_virtual (TYPE_BINFO (t), base_fndecl,
  3221.                               DESTRUCTOR_NAME_P (DECL_ASSEMBLER_NAME (base_fndecl)));
  3222.           tree context = DECL_CLASS_CONTEXT (decl);
  3223.           if (decl != base_fndecl && context != t)
  3224.         {
  3225.           tree base_context = DECL_CLASS_CONTEXT (base_fndecl);
  3226.           tree binfo = NULL_TREE, these_virtuals;
  3227. #if 0
  3228.           unsigned HOST_WIDE_INT i
  3229.             = (TREE_INT_CST_LOW (DECL_VINDEX (base_fndecl))
  3230.                & (((unsigned HOST_WIDE_INT)1<<(BITS_PER_WORD-1))-1));
  3231. #endif
  3232.  
  3233.           if (TYPE_USES_VIRTUAL_BASECLASSES (context))
  3234.             binfo = virtual_member (base_context,
  3235.                         CLASSTYPE_VBASECLASSES (context));
  3236.           if (binfo == NULL_TREE)
  3237.             binfo = binfo_value (base_context, context);
  3238.           if (binfo != NULL_TREE)
  3239.             {
  3240. #if 1
  3241.               pfn = FNADDR_FROM_VTABLE_ENTRY (TREE_VALUE (get_vtable_entry (BINFO_VIRTUALS (binfo), base_fndecl)));
  3242. #else
  3243.               these_virtuals = BINFO_VIRTUALS (binfo);
  3244.  
  3245.               while (i-- > 0)
  3246.             these_virtuals = TREE_CHAIN (these_virtuals);
  3247.               pfn = FNADDR_FROM_VTABLE_ENTRY (TREE_VALUE (these_virtuals));
  3248. #endif
  3249.               modify_vtable_entries (t, decl, base_fndecl, pfn);
  3250.             }
  3251.         }
  3252.           virtuals = TREE_CHAIN (virtuals);
  3253.         }
  3254.       /* Update dossier info with offsets for virtual baseclasses.  */
  3255.       if (flag_dossier && ! BINFO_NEW_VTABLE_MARKED (vbases))
  3256.         prepare_fresh_vtable (vbases, vbases, t);
  3257.  
  3258.       vbases = TREE_CHAIN (vbases);
  3259.     }
  3260.     }
  3261.  
  3262.   while (pending_hard_virtuals)
  3263.     {
  3264.       /* Need an entry in some other virtual function table.  */
  3265.       if (TREE_TYPE (pending_hard_virtuals))
  3266.     {
  3267.       /* This is how we modify entries when a vfn's index changes
  3268.          between derived and base type.  */
  3269.       modify_vtable_entries (t, TREE_PURPOSE (pending_hard_virtuals),
  3270.                  TREE_TYPE (pending_hard_virtuals),
  3271.                  TREE_VALUE (pending_hard_virtuals));
  3272.     }
  3273.       else
  3274.     {
  3275.       /* This is how we modify entries when a vfn comes from
  3276.          a virtual baseclass.  */
  3277.       tree base_fndecls = DECL_VINDEX (TREE_PURPOSE (pending_hard_virtuals));
  3278.       my_friendly_assert (base_fndecls != error_mark_node, 176);
  3279.       while (base_fndecls)
  3280.         {
  3281.           modify_vtable_entries (t, TREE_PURPOSE (pending_hard_virtuals),
  3282.                      TREE_VALUE (base_fndecls),
  3283.                      TREE_VALUE (pending_hard_virtuals));
  3284.           base_fndecls = TREE_CHAIN (base_fndecls);
  3285.         }
  3286.     }
  3287.       pending_hard_virtuals = TREE_CHAIN (pending_hard_virtuals);
  3288.     }
  3289.   doing_hard_virtuals = 0;
  3290.  
  3291.   /* Under our model of GC, every C++ class gets its own virtual
  3292.      function table, at least virtually.  */
  3293.   if (pending_virtuals || CLASSTYPE_DOSSIER (t))
  3294.     {
  3295.       pending_virtuals = nreverse (pending_virtuals);
  3296.       /* We must enter these virtuals into the table.  */
  3297.       if (first_vfn_base_index < 0)
  3298.     {
  3299.       if (flag_dossier)
  3300.         pending_virtuals = tree_cons (NULL_TREE,
  3301.                       build_vtable_entry (integer_zero_node,
  3302.                                   build_t_desc (t, 0)),
  3303.                       pending_virtuals);
  3304.       pending_virtuals = tree_cons (NULL_TREE, the_null_vtable_entry,
  3305.                     pending_virtuals);
  3306.       build_vtable (NULL_TREE, t);
  3307.     }
  3308.       else
  3309.     {
  3310.       /* Here we know enough to change the type of our virtual
  3311.          function table, but we will wait until later this function.  */
  3312.  
  3313.       if (! BINFO_NEW_VTABLE_MARKED (TYPE_BINFO (t)))
  3314.         build_vtable (TREE_VEC_ELT (TYPE_BINFO_BASETYPES (t), first_vfn_base_index), t);
  3315.  
  3316.       /* Update the dossier pointer for this class.  */
  3317.       if (flag_dossier)
  3318.         TREE_VALUE (TREE_CHAIN (TYPE_BINFO_VIRTUALS (t)))
  3319.           = build_vtable_entry (integer_zero_node, build_t_desc (t, 0));
  3320.     }
  3321.  
  3322.       /* If this type has basetypes with constructors, then those
  3323.      constructors might clobber the virtual function table.  But
  3324.      they don't if the derived class shares the exact vtable of the base
  3325.      class.  */
  3326.  
  3327.       CLASSTYPE_NEEDS_VIRTUAL_REINIT (t) = 1;
  3328.     }
  3329.   else if (first_vfn_base_index >= 0)
  3330.     {
  3331.       tree binfo = TREE_VEC_ELT (TYPE_BINFO_BASETYPES (t), first_vfn_base_index);
  3332. #if 0
  3333.       /* For testing. */
  3334.       tree binfo1 = get_binfo (DECL_FIELD_CONTEXT (vfield), t, 0);
  3335.       if (binfo != binfo1)
  3336.     warning ("binfos are different in vtable creation");
  3337. #endif
  3338.  
  3339.       /* This class contributes nothing new to the virtual function
  3340.      table.  However, it may have declared functions which
  3341.      went into the virtual function table "inherited" from the
  3342.      base class.  If so, we grab a copy of those updated functions,
  3343.      and pretend they are ours.  */
  3344.  
  3345.       /* See if we should steal the virtual info from base class.  */
  3346.       if (TYPE_BINFO_VTABLE (t) == NULL_TREE)
  3347.     TYPE_BINFO_VTABLE (t) = BINFO_VTABLE (binfo);
  3348.       if (TYPE_BINFO_VIRTUALS (t) == NULL_TREE)
  3349.     TYPE_BINFO_VIRTUALS (t) = BINFO_VIRTUALS (binfo);
  3350.       if (TYPE_BINFO_VTABLE (t) != BINFO_VTABLE (binfo))
  3351.     CLASSTYPE_NEEDS_VIRTUAL_REINIT (t) = 1;
  3352.     }
  3353.  
  3354.   if (has_virtual > max_has_virtual)
  3355.     max_has_virtual = has_virtual;
  3356.   if (max_has_virtual || first_vfn_base_index >= 0)
  3357.     {
  3358. #ifdef VTABLE_USES_MASK
  3359.       if (max_has_virtual >= VINDEX_MAX)
  3360.     {
  3361.       error ("too many virtual functions for class `%s' (VINDEX_MAX < %d)",
  3362.          err_name, has_virtual);
  3363.     }
  3364. #endif
  3365.       TYPE_VIRTUAL_P (t) = 1;
  3366.       CLASSTYPE_VSIZE (t) = has_virtual;
  3367.       if (first_vfn_base_index >= 0)
  3368.     {
  3369.       if (pending_virtuals)
  3370.         TYPE_BINFO_VIRTUALS (t) = chainon (TYPE_BINFO_VIRTUALS (t),
  3371.                         pending_virtuals);
  3372.     }
  3373.       else if (has_virtual)
  3374.     {
  3375.       TYPE_BINFO_VIRTUALS (t) = pending_virtuals;
  3376.       if (write_virtuals >= 0)
  3377.         DECL_VIRTUAL_P (TYPE_BINFO_VTABLE (t)) = 1;
  3378.     }
  3379.     }
  3380.  
  3381.   /* Now lay out the virtual function table.  */
  3382.   if (has_virtual)
  3383.     {
  3384.       tree atype, itype;
  3385.  
  3386.       if (TREE_TYPE (vfield) == ptr_type_node)
  3387.     {
  3388.       /* We must create a pointer to this table because
  3389.          the one inherited from base class does not exist.
  3390.          We will fill in the type when we know what it
  3391.          should really be.  Use `size_int' so values are memoized
  3392.          in common cases.  */
  3393.       itype = build_index_type (size_int (has_virtual));
  3394.       atype = build_array_type (vtable_entry_type, itype);
  3395.       layout_type (atype);
  3396.       TREE_TYPE (vfield) = build_pointer_type (atype);
  3397.     }
  3398.       else
  3399.     {
  3400.       atype = TREE_TYPE (TREE_TYPE (vfield));
  3401.  
  3402.       if (has_virtual != TREE_INT_CST_LOW (TYPE_MAX_VALUE (TYPE_DOMAIN (atype))))
  3403.         {
  3404.           /* We must extend (or create) the boundaries on this array,
  3405.          because we picked up virtual functions from multiple
  3406.          base classes.  */
  3407.           itype = build_index_type (size_int (has_virtual));
  3408.           atype = build_array_type (vtable_entry_type, itype);
  3409.           layout_type (atype);
  3410.           vfield = copy_node (vfield);
  3411.           TREE_TYPE (vfield) = build_pointer_type (atype);
  3412.         }
  3413.     }
  3414.  
  3415.       CLASSTYPE_VFIELD (t) = vfield;
  3416.       if (TREE_TYPE (TYPE_BINFO_VTABLE (t)) != atype)
  3417.     {
  3418.       TREE_TYPE (TYPE_BINFO_VTABLE (t)) = atype;
  3419.       layout_decl (TYPE_BINFO_VTABLE (t), 0);
  3420.       /* At one time the vtable info was grabbed 2 words at a time.  This
  3421.          fails on sparc unless you have 8-byte alignment.  (tiemann) */
  3422.       DECL_ALIGN (TYPE_BINFO_VTABLE (t))
  3423.         = MAX (TYPE_ALIGN (double_type_node),
  3424.            DECL_ALIGN (TYPE_BINFO_VTABLE (t)));
  3425.     }
  3426.     }
  3427.   else if (first_vfn_base_index >= 0)
  3428.     CLASSTYPE_VFIELD (t) = vfield;
  3429.   CLASSTYPE_VFIELDS (t) = vfields;
  3430.  
  3431.   /* Set all appropriate CLASSTYPE_... flags for this type
  3432.      and its variants.  */
  3433.   TYPE_NEEDS_CONSTRUCTOR (t) |= needs_ctor || TYPE_HAS_CONSTRUCTOR (t);
  3434.   TYPE_NEEDS_CONSTRUCTING (t)
  3435.     |= ((TYPE_NEEDS_CONSTRUCTOR (t)|TYPE_USES_VIRTUAL_BASECLASSES (t))
  3436.     || has_virtual || any_default_members
  3437.     || first_vfn_base_index >= 0);
  3438.   TYPE_NEEDS_DESTRUCTOR (t) |= needs_dtor || TYPE_HAS_DESTRUCTOR (t);
  3439.   finish_struct_bits (t, max_has_virtual);
  3440.  
  3441.   /* Promote each bit-field's type to int if it is narrower than that.
  3442.      There's more: complete the rtl for any static member objects which
  3443.      is of the same type we're working on.  */
  3444.   for (x = fields; x; x = TREE_CHAIN (x))
  3445.     {
  3446.       if (DECL_BIT_FIELD (x)
  3447.       && (C_PROMOTING_INTEGER_TYPE_P (TREE_TYPE (x))
  3448.           || DECL_FIELD_SIZE (x) < TYPE_PRECISION (integer_type_node)))
  3449.     {
  3450.       tree type = TREE_TYPE (x);
  3451.  
  3452.       /* Preserve unsignedness if traditional or if not really getting
  3453.          any wider.  */
  3454.       if (TREE_UNSIGNED (type)
  3455.           && (flag_traditional
  3456.           ||
  3457.           (TYPE_PRECISION (type) == TYPE_PRECISION (integer_type_node)
  3458.            && DECL_FIELD_SIZE (x) == TYPE_PRECISION (integer_type_node))))
  3459.         TREE_TYPE (x) = unsigned_type_node;
  3460.       else
  3461.         TREE_TYPE (x) = integer_type_node;
  3462.     }
  3463.  
  3464.       if (TREE_CODE (x) == VAR_DECL && TREE_STATIC (x)
  3465.       && TREE_TYPE (x) == t)
  3466.     {
  3467.       DECL_MODE (x) = TYPE_MODE (t);
  3468.       make_decl_rtl (x, NULL, 0);
  3469.     }
  3470.     }
  3471.  
  3472.   /* Now add the tags, if any, to the list of TYPE_DECLs
  3473.      defined for this type.  */
  3474.   if (CLASSTYPE_TAGS (t))
  3475.     {
  3476.       x = CLASSTYPE_TAGS (t);
  3477.       last_x = tree_last (TYPE_FIELDS (t));
  3478.       while (x)
  3479.     {
  3480.       tree tag = build_lang_decl (TYPE_DECL, TREE_PURPOSE (x), TREE_VALUE (x));
  3481. #ifdef DWARF_DEBUGGING_INFO
  3482.       if (write_symbols == DWARF_DEBUG)
  3483.         {
  3484.           /* Notify dwarfout.c that this TYPE_DECL node represent a
  3485.          gratuitous typedef.  */
  3486.           DECL_IGNORED_P (tag) = 1;
  3487.         }
  3488. #endif /* DWARF_DEBUGGING_INFO */
  3489.       DECL_CONTEXT (tag) = t;
  3490.       DECL_CLASS_CONTEXT (tag) = t;
  3491.       x = TREE_CHAIN (x);
  3492.       last_x = chainon (last_x, tag);
  3493.     }
  3494.       if (TYPE_FIELDS (t) == 0)
  3495.     TYPE_FIELDS (t) = last_x;
  3496.       CLASSTYPE_LOCAL_TYPEDECLS (t) = 1;
  3497.     }
  3498.  
  3499.   if (TYPE_HAS_CONSTRUCTOR (t))
  3500.     {
  3501.       tree vfields = CLASSTYPE_VFIELDS (t);
  3502.  
  3503.       while (vfields)
  3504.     {
  3505.       /* Mark the fact that constructor for T
  3506.          could affect anybody inheriting from T
  3507.          who wants to initialize vtables for VFIELDS's type.  */
  3508.       if (VF_DERIVED_VALUE (vfields))
  3509.         TREE_ADDRESSABLE (vfields) = 1;
  3510.       vfields = TREE_CHAIN (vfields);
  3511.     }
  3512.       if (any_default_members != 0)
  3513.     build_class_init_list (t);
  3514.     }
  3515.   else if (TYPE_NEEDS_CONSTRUCTING (t))
  3516.     build_class_init_list (t);
  3517.  
  3518.   if (current_lang_name == lang_name_cplusplus)
  3519.     {
  3520.       if (! CLASSTYPE_DECLARED_EXCEPTION (t))
  3521.     embrace_waiting_friends (t);
  3522.  
  3523.       /* Write out inline function definitions.  */
  3524.       do_inline_function_hair (t, CLASSTYPE_INLINE_FRIENDS (t));
  3525.       CLASSTYPE_INLINE_FRIENDS (t) = 0;
  3526.     }
  3527.  
  3528.   if (CLASSTYPE_VSIZE (t) != 0)
  3529.     {
  3530.       if ((flag_this_is_variable & 1) == 0)
  3531.     {
  3532.       tree vtbl_ptr = build_decl (VAR_DECL, get_identifier (VPTR_NAME),
  3533.                       TREE_TYPE (vfield));
  3534.       DECL_REGISTER (vtbl_ptr) = 1;
  3535.       CLASSTYPE_VTBL_PTR (t) = vtbl_ptr;
  3536.     }
  3537.       if (DECL_FIELD_CONTEXT (vfield) != t)
  3538.     {
  3539.       tree binfo = get_binfo (DECL_FIELD_CONTEXT (vfield), t, 0);
  3540.       tree offset = BINFO_OFFSET (binfo);
  3541.  
  3542.       vfield = copy_node (vfield);
  3543.       copy_lang_decl (vfield);
  3544.  
  3545.       if (! integer_zerop (offset))
  3546.         offset = size_binop (MULT_EXPR, offset, size_int (BITS_PER_UNIT));
  3547.       DECL_FIELD_CONTEXT (vfield) = t;
  3548.       DECL_CLASS_CONTEXT (vfield) = t;
  3549.       DECL_FIELD_BITPOS (vfield)
  3550.         = size_binop (PLUS_EXPR, offset, DECL_FIELD_BITPOS (vfield));
  3551.       CLASSTYPE_VFIELD (t) = vfield;
  3552.     }
  3553.  
  3554.       /* In addition to this one, all the other vfields should be listed. */
  3555.       /* Before that can be done, we have to have FIELD_DECLs for them, and
  3556.      a place to find them.  */
  3557.       TYPE_NONCOPIED_PARTS (t) = build_tree_list (default_conversion (TYPE_BINFO_VTABLE (t)), vfield);
  3558.  
  3559.       if (warn_nonvdtor && TYPE_HAS_DESTRUCTOR (t)
  3560.       && DECL_VINDEX (TREE_VEC_ELT (method_vec, 0)) == NULL_TREE)
  3561.     warning ("class `%s' has virtual functions but non-virtual destructor",
  3562.          err_name);
  3563.     }
  3564.  
  3565.   /* Make the rtl for any new vtables we have created, and unmark
  3566.      the base types we marked.  */
  3567.   unmark_finished_struct (t);
  3568.   TYPE_BEING_DEFINED (t) = 0;
  3569.  
  3570.   if (flag_dossier && CLASSTYPE_VTABLE_NEEDS_WRITING (t))
  3571.     {
  3572.       tree variants;
  3573.       tree tdecl;
  3574.  
  3575.       /* Now instantiate its type descriptors.  */
  3576.       tdecl = TREE_OPERAND (build_t_desc (t, 1), 0);
  3577.       variants = TYPE_POINTER_TO (t);
  3578.       build_type_variant (variants, 1, 0);
  3579.       while (variants)
  3580.     {
  3581.       build_t_desc (variants, 1);
  3582.       variants = TYPE_NEXT_VARIANT (variants);
  3583.     }
  3584.       variants = build_reference_type (t);
  3585.       build_type_variant (variants, 1, 0);
  3586.       while (variants)
  3587.     {
  3588.       build_t_desc (variants, 1);
  3589.       variants = TYPE_NEXT_VARIANT (variants);
  3590.     }
  3591. #if 0
  3592.       DECL_VPARENT (tdecl) = t;
  3593. #endif
  3594.       DECL_CONTEXT (tdecl) = t;
  3595.     }
  3596.   /* Still need to instantiate this C struct's type descriptor.  */
  3597.   else if (flag_dossier && ! CLASSTYPE_DOSSIER (t))
  3598.     build_t_desc (t, 1);
  3599.  
  3600.   if (TYPE_NAME (t) && TYPE_IDENTIFIER (t))
  3601.     undo_template_name_overload (TYPE_IDENTIFIER (t), 1);
  3602.   if (current_class_type)
  3603.     popclass (0);
  3604.   else
  3605.     error ("trying to finish struct, but kicked out due to previous parse errors.");
  3606.  
  3607.   hack_incomplete_structures (t);
  3608.  
  3609.   resume_momentary (old);
  3610.  
  3611.   if (flag_cadillac)
  3612.     cadillac_finish_struct (t);
  3613.  
  3614. #if 0
  3615.   /* This has to be done after we have sorted out what to do with
  3616.      the enclosing type.  */
  3617.   if (write_symbols != DWARF_DEBUG)
  3618.     {
  3619.       /* Be smarter about nested classes here.  If a type is nested,
  3620.      only output it if we would output the enclosing type.  */
  3621.       if (DECL_CONTEXT (TYPE_NAME (t))
  3622.       && TREE_CODE_CLASS (TREE_CODE (DECL_CONTEXT (TYPE_NAME (t)))) == 't')
  3623.     DECL_IGNORED_P (TYPE_NAME (t)) = TREE_ASM_WRITTEN (TYPE_NAME (t));
  3624.     }
  3625. #endif
  3626.  
  3627.   if (write_symbols != DWARF_DEBUG)
  3628.     {
  3629.       /* If the type has methods, we want to think about cutting down
  3630.      the amount of symbol table stuff we output.  The value stored in
  3631.      the TYPE_DECL's DECL_IGNORED_P slot is a first approximation.
  3632.      For example, if a member function is seen and we decide to
  3633.      write out that member function, then we can change the value
  3634.      of the DECL_IGNORED_P slot, and the type will be output when
  3635.      that member function's debug info is written out.  */
  3636.       if (CLASSTYPE_METHOD_VEC (t))
  3637.     {
  3638.       extern tree pending_vtables;
  3639.  
  3640.       /* Don't output full info about any type
  3641.          which does not have its implementation defined here.  */
  3642.       if (TYPE_VIRTUAL_P (t) && write_virtuals == 2)
  3643.         DECL_IGNORED_P (TYPE_NAME (t))
  3644.           = (value_member (TYPE_IDENTIFIER (t), pending_vtables) == 0);
  3645.       else if (CLASSTYPE_INTERFACE_ONLY (t))
  3646.         DECL_IGNORED_P (TYPE_NAME (t)) = 1;
  3647.       else if (CLASSTYPE_INTERFACE_UNKNOWN (t))
  3648.         /* Only a first approximation!  */
  3649.         DECL_IGNORED_P (TYPE_NAME (t)) = 1;
  3650.     }
  3651.       else if (CLASSTYPE_INTERFACE_ONLY (t))
  3652.     DECL_IGNORED_P (TYPE_NAME (t)) = 1;
  3653.     }
  3654.  
  3655.   /* Finish debugging output for this type.  */
  3656.   rest_of_type_compilation (t, global_bindings_p ());
  3657.  
  3658.   return t;
  3659. }
  3660.  
  3661. /* Return non-zero if the effective type of INSTANCE is static.
  3662.    Used to determine whether the virtual function table is needed
  3663.    or not.
  3664.  
  3665.    *NONNULL is set iff INSTANCE can be known to be nonnull, regardless
  3666.    of our knowledge of its type.  */
  3667. int
  3668. resolves_to_fixed_type_p (instance, nonnull)
  3669.      tree instance;
  3670.      int *nonnull;
  3671. {
  3672.   switch (TREE_CODE (instance))
  3673.     {
  3674.     case INDIRECT_REF:
  3675.       /* Check that we are not going through a cast of some sort.  */
  3676.       if (TREE_TYPE (instance)
  3677.       == TREE_TYPE (TREE_TYPE (TREE_OPERAND (instance, 0))))
  3678.     instance = TREE_OPERAND (instance, 0);
  3679.       /* fall through...  */
  3680.     case CALL_EXPR:
  3681.       /* This is a call to a constructor, hence it's never zero.  */
  3682.       if (TREE_HAS_CONSTRUCTOR (instance))
  3683.     {
  3684.       if (nonnull)
  3685.         *nonnull = 1;
  3686.       return 1;
  3687.     }
  3688.       return 0;
  3689.  
  3690.     case SAVE_EXPR:
  3691.       /* This is a call to a constructor, hence it's never zero.  */
  3692.       if (TREE_HAS_CONSTRUCTOR (instance))
  3693.     {
  3694.       if (nonnull)
  3695.         *nonnull = 1;
  3696.       return 1;
  3697.     }
  3698.       return resolves_to_fixed_type_p (TREE_OPERAND (instance, 0), nonnull);
  3699.  
  3700.     case RTL_EXPR:
  3701.       /* This is a call to `new', hence it's never zero.  */
  3702.       if (TREE_CALLS_NEW (instance))
  3703.     {
  3704.       if (nonnull)
  3705.         *nonnull = 1;
  3706.       return 1;
  3707.     }
  3708.       return 0;
  3709.  
  3710.     case PLUS_EXPR:
  3711.     case MINUS_EXPR:
  3712.       if (TREE_CODE (TREE_OPERAND (instance, 1)) == INTEGER_CST)
  3713.     /* Propagate nonnull.  */
  3714.     resolves_to_fixed_type_p (TREE_OPERAND (instance, 0), nonnull);
  3715.       if (TREE_CODE (TREE_OPERAND (instance, 0)) == ADDR_EXPR)
  3716.     return resolves_to_fixed_type_p (TREE_OPERAND (instance, 0), nonnull);
  3717.       return 0;
  3718.  
  3719.     case NOP_EXPR:
  3720.     case CONVERT_EXPR:
  3721.       return resolves_to_fixed_type_p (TREE_OPERAND (instance, 0), nonnull);
  3722.  
  3723.     case ADDR_EXPR:
  3724.       if (nonnull)
  3725.     *nonnull = 1;
  3726.       return resolves_to_fixed_type_p (TREE_OPERAND (instance, 0), nonnull);
  3727.  
  3728.     case COMPONENT_REF:
  3729.       return resolves_to_fixed_type_p (TREE_OPERAND (instance, 1), nonnull);
  3730.  
  3731.     case WITH_CLEANUP_EXPR:
  3732.       if (TREE_CODE (TREE_OPERAND (instance, 0)) == ADDR_EXPR)
  3733.     return resolves_to_fixed_type_p (TREE_OPERAND (instance, 0), nonnull);
  3734.       /* fall through... */
  3735.     case VAR_DECL:
  3736.     case FIELD_DECL:
  3737.       if (TREE_CODE (TREE_TYPE (instance)) == ARRAY_TYPE
  3738.       && IS_AGGR_TYPE (TREE_TYPE (TREE_TYPE (instance))))
  3739.     {
  3740.       if (nonnull)
  3741.         *nonnull = 1;
  3742.       return 1;
  3743.     }
  3744.       /* fall through... */
  3745.     case TARGET_EXPR:
  3746.     case PARM_DECL:
  3747.       if (IS_AGGR_TYPE (TREE_TYPE (instance)))
  3748.     {
  3749.       if (nonnull)
  3750.         *nonnull = 1;
  3751.       return 1;
  3752.     }
  3753.       else if (nonnull)
  3754.     {
  3755.       if (instance == current_class_decl
  3756.           && flag_this_is_variable <= 0)
  3757.         {
  3758.           /* Some people still use `this = 0' inside destructors.  */
  3759.           *nonnull = ! DESTRUCTOR_NAME_P (DECL_ASSEMBLER_NAME (current_function_decl));
  3760.           /* In a constructor, we know our type.  */
  3761.           if (flag_this_is_variable < 0)
  3762.         return 1;
  3763.         }
  3764.       else if (TREE_CODE (TREE_TYPE (instance)) == REFERENCE_TYPE)
  3765.         /* Reference variables should be references to objects.  */
  3766.         *nonnull = 1;
  3767.     }
  3768.       return 0;
  3769.  
  3770.     default:
  3771.       return 0;
  3772.     }
  3773. }
  3774.  
  3775. void
  3776. init_class_processing ()
  3777. {
  3778.   current_class_depth = 0;
  3779.   current_class_stacksize = 10;
  3780.   current_class_base = (tree *)xmalloc(current_class_stacksize * sizeof (tree));
  3781.   current_class_stack = current_class_base;
  3782.  
  3783.   current_lang_stacksize = 10;
  3784.   current_lang_base = (tree *)xmalloc(current_lang_stacksize * sizeof (tree));
  3785.   current_lang_stack = current_lang_base;
  3786.  
  3787.   /* Keep these values lying around.  */
  3788.   the_null_vtable_entry = build_vtable_entry (integer_zero_node, integer_zero_node);
  3789.   base_layout_decl = build_lang_field_decl (FIELD_DECL, NULL_TREE, error_mark_node);
  3790.   TREE_TYPE (base_layout_decl) = make_node (RECORD_TYPE);
  3791.  
  3792.   gcc_obstack_init (&class_obstack);
  3793. }
  3794.  
  3795. /* Set current scope to NAME. CODE tells us if this is a
  3796.    STRUCT, UNION, or ENUM environment.
  3797.  
  3798.    NAME may end up being NULL_TREE if this is an anonymous or
  3799.    late-bound struct (as in "struct { ... } foo;")  */
  3800.  
  3801. /* Here's a subroutine we need because C lacks lambdas.  */
  3802. static void
  3803. unuse_fields (type)
  3804.      tree type;
  3805. {
  3806.   tree fields;
  3807.  
  3808.   for (fields = TYPE_FIELDS (type); fields; fields = TREE_CHAIN (fields))
  3809.     {
  3810.       if (TREE_CODE (fields) != FIELD_DECL)
  3811.     continue;
  3812.  
  3813.       TREE_USED (fields) = 0;
  3814.       if (DECL_NAME (fields) == NULL_TREE
  3815.       && TREE_CODE (TREE_TYPE (fields)) == UNION_TYPE)
  3816.     unuse_fields (TREE_TYPE (fields));
  3817.     }
  3818. }
  3819.  
  3820. /* Set global variables CURRENT_CLASS_NAME and CURRENT_CLASS_TYPE to
  3821.    appropriate values, found by looking up the type definition of
  3822.    NAME (as a CODE).
  3823.  
  3824.    If MODIFY is 1, we set IDENTIFIER_CLASS_VALUE's of names
  3825.    which can be seen locally to the class.  They are shadowed by
  3826.    any subsequent local declaration (including parameter names).
  3827.  
  3828.    If MODIFY is 2, we set IDENTIFIER_CLASS_VALUE's of names
  3829.    which have static meaning (i.e., static members, static
  3830.    member functions, enum declarations, etc).
  3831.  
  3832.    If MODIFY is 3, we set IDENTIFIER_CLASS_VALUE of names
  3833.    which can be seen locally to the class (as in 1), but
  3834.    know that we are doing this for declaration purposes
  3835.    (i.e. friend foo::bar (int)).
  3836.  
  3837.    So that we may avoid calls to lookup_name, we cache the _TYPE
  3838.    nodes of local TYPE_DECLs in the TREE_TYPE field of the name.
  3839.  
  3840.    For multiple inheritance, we perform a two-pass depth-first search
  3841.    of the type lattice.  The first pass performs a pre-order search,
  3842.    marking types after the type has had its fields installed in
  3843.    the appropriate IDENTIFIER_CLASS_VALUE slot.  The second pass merely
  3844.    unmarks the marked types.  If a field or member function name
  3845.    appears in an ambiguous way, the IDENTIFIER_CLASS_VALUE of
  3846.    that name becomes `error_mark_node'.  */
  3847.  
  3848. void
  3849. pushclass (type, modify)
  3850.      tree type;
  3851.      int modify;
  3852. {
  3853.   push_memoized_context (type, modify);
  3854.  
  3855.   current_class_depth++;
  3856.   *current_class_stack++ = current_class_name;
  3857.   *current_class_stack++ = current_class_type;
  3858.   if (current_class_stack >= current_class_base + current_class_stacksize)
  3859.     {
  3860.       current_class_base =
  3861.     (tree *)xrealloc (current_class_base,
  3862.               sizeof (tree) * (current_class_stacksize + 10));
  3863.       current_class_stack = current_class_base + current_class_stacksize;
  3864.       current_class_stacksize += 10;
  3865.     }
  3866.  
  3867.   current_class_name = TYPE_NAME (type);
  3868.   if (TREE_CODE (current_class_name) == TYPE_DECL)
  3869.     current_class_name = DECL_NAME (current_class_name);
  3870.   current_class_type = type;
  3871.  
  3872. #if NEW_CLASS_SCOPING
  3873.   if (previous_class_type != NULL_TREE
  3874.       && (type != previous_class_type || TYPE_SIZE (previous_class_type) == NULL_TREE)
  3875.       && current_class_depth == 1)
  3876. #else
  3877.   if (previous_class_type != NULL_TREE
  3878.       && (type != previous_class_type
  3879.       || TYPE_SIZE (previous_class_type) == NULL_TREE
  3880.       /* ??? Is this necessary any more?  */
  3881.       || IDENTIFIER_TEMPLATE (TYPE_IDENTIFIER (previous_class_type)))
  3882.       && (current_class_depth == 1 || modify == 3))
  3883. #endif
  3884.     {
  3885.       /* Forcibly remove any old class remnants.  */
  3886.       popclass (-1);
  3887.       previous_class_type = NULL_TREE;
  3888.     }
  3889.  
  3890.   pushlevel_class ();
  3891.  
  3892.   if (modify)
  3893.     {
  3894.       tree tags;
  3895.       tree this_fndecl = current_function_decl;
  3896.  
  3897.       if (current_function_decl
  3898.       && DECL_CONTEXT (current_function_decl)
  3899.       && TREE_CODE (DECL_CONTEXT (current_function_decl)) == FUNCTION_DECL)
  3900.     current_function_decl = DECL_CONTEXT (current_function_decl);
  3901.       else
  3902.     current_function_decl = NULL_TREE;
  3903.  
  3904.       if (TREE_CODE (type) == UNINSTANTIATED_P_TYPE)
  3905.         {
  3906.           declare_uninstantiated_type_level ();
  3907.       overload_template_name (current_class_name, 0);
  3908.         }
  3909. #if !NEW_CLASS_SCOPING
  3910.       else if (type != previous_class_type)
  3911. #else
  3912.       else if (type != previous_class_type || current_class_depth > 1)
  3913. #endif
  3914.     {
  3915.       build_mi_matrix (type);
  3916.       push_class_decls (type);
  3917.       free_mi_matrix ();
  3918. #if NEW_CLASS_SCOPING
  3919.       if (current_class_depth == 1)
  3920. #endif
  3921.         previous_class_type = type;
  3922.     }
  3923. #if NEW_CLASS_SCOPING
  3924.       else
  3925.     {
  3926.       tree item;
  3927.  
  3928.       /* Hooray, our cacheing was successful, let's just install the
  3929.          cached class_shadowed list, and walk through it to get the
  3930.          IDENTIFIER_TYPE_VALUEs correct.  */
  3931.       set_class_shadows (previous_class_values);
  3932.       for (item = previous_class_values; item; item = TREE_CHAIN (item))
  3933.         {
  3934.           tree id = TREE_PURPOSE (item);
  3935.           tree decl = IDENTIFIER_CLASS_VALUE (id);
  3936.  
  3937.           if (TREE_CODE (decl) == TYPE_DECL)
  3938.         set_identifier_type_value (id, TREE_TYPE (decl));
  3939.         }
  3940.     }
  3941. #endif
  3942.  
  3943.       for (tags = CLASSTYPE_TAGS (type); tags; tags = TREE_CHAIN (tags))
  3944.     {
  3945.       TREE_NONLOCAL_FLAG (TREE_VALUE (tags)) = 1;
  3946.       if (! TREE_PURPOSE (tags))
  3947.         continue;
  3948.       pushtag (TREE_PURPOSE (tags), TREE_VALUE (tags));
  3949. #if !NEW_CLASS_SCOPING
  3950.       if (IDENTIFIER_CLASS_VALUE (TREE_PURPOSE (tags)) == NULL_TREE
  3951.           && TREE_CODE (TYPE_NAME (TREE_VALUE (tags))) == TYPE_DECL)
  3952.         IDENTIFIER_CLASS_VALUE (TREE_PURPOSE (tags))
  3953.           = TYPE_NAME (TREE_VALUE (tags));
  3954. #endif
  3955.     }
  3956.  
  3957.       current_function_decl = this_fndecl;
  3958.     }
  3959.  
  3960.   if (flag_cadillac)
  3961.     cadillac_push_class (type);
  3962. }
  3963.  
  3964. /* Get out of the current class scope. If we were in a class scope
  3965.    previously, that is the one popped to.  The flag MODIFY tells
  3966.    whether the current scope declarations needs to be modified
  3967.    as a result of popping to the previous scope.  */
  3968. void
  3969. popclass (modify)
  3970.      int modify;
  3971. {
  3972.   if (flag_cadillac)
  3973.     cadillac_pop_class ();
  3974.  
  3975.   if (modify < 0)
  3976.     {
  3977.       /* Back this old class out completely.  */
  3978.       tree tags = CLASSTYPE_TAGS (previous_class_type);
  3979. #if NEW_CLASS_SCOPING
  3980.       tree t;
  3981.  
  3982.       /* This code can be seen as a cache miss.  When we've cached a
  3983.      class' scope's bindings and we can't use them, we need to reset
  3984.      them.  This is it!  */
  3985.       for (t = previous_class_values; t; t = TREE_CHAIN(t))
  3986.     IDENTIFIER_CLASS_VALUE (TREE_PURPOSE (t)) = NULL_TREE;
  3987. #else
  3988.       pop_class_decls (previous_class_type);
  3989. #endif
  3990.       while (tags)
  3991.     {
  3992.       TREE_NONLOCAL_FLAG (TREE_VALUE (tags)) = 0;
  3993. #if !NEW_CLASS_SCOPING
  3994.       IDENTIFIER_CLASS_VALUE (TREE_PURPOSE (tags)) = NULL_TREE;
  3995. #endif
  3996.       tags = TREE_CHAIN (tags);
  3997.     }
  3998.       goto ret;
  3999.     }
  4000.  
  4001.   if (modify)
  4002.     {
  4003.       /* Just remove from this class what didn't make
  4004.      it into IDENTIFIER_CLASS_VALUE.  */
  4005.       tree tags = CLASSTYPE_TAGS (current_class_type);
  4006.  
  4007.       while (tags)
  4008.     {
  4009.       TREE_NONLOCAL_FLAG (TREE_VALUE (tags)) = 0;
  4010. #if !NEW_CLASS_SCOPING
  4011.       if (TREE_PURPOSE (tags))
  4012.         IDENTIFIER_CLASS_VALUE (TREE_PURPOSE (tags)) = NULL_TREE;
  4013. #endif
  4014.       tags = TREE_CHAIN (tags);
  4015.     }
  4016.     }
  4017.   if (TREE_CODE (current_class_type) == UNINSTANTIATED_P_TYPE)
  4018.     undo_template_name_overload (current_class_name, 0);
  4019.  
  4020.   poplevel_class ();
  4021.  
  4022. #if NEW_CLASS_SCOPING
  4023.   /* Since poplevel_class does the popping of class decls nowadays,
  4024.      this really only frees the obstack used for these decls.
  4025.      That's why it had to be moved down here.  */
  4026.   if (modify)
  4027.     pop_class_decls (current_class_type);
  4028. #endif
  4029.  
  4030.   current_class_depth--;
  4031.   current_class_type = *--current_class_stack;
  4032.   current_class_name = *--current_class_stack;
  4033.  
  4034.   if (current_class_type)
  4035.     {
  4036.       if (CLASSTYPE_VTBL_PTR (current_class_type))
  4037.     {
  4038.       current_vtable_decl = lookup_name (DECL_NAME (CLASSTYPE_VTBL_PTR (current_class_type)), 0);
  4039.       if (current_vtable_decl)
  4040.         current_vtable_decl = build_indirect_ref (current_vtable_decl,
  4041.                               NULL_PTR);
  4042.     }
  4043.       current_class_decl = lookup_name (this_identifier, 0);
  4044.       if (current_class_decl)
  4045.     {
  4046.       if (TREE_CODE (TREE_TYPE (current_class_decl)) == POINTER_TYPE)
  4047.         {
  4048.           tree temp;
  4049.           /* Can't call build_indirect_ref here, because it has special
  4050.          logic to return C_C_D given this argument.  */
  4051.           C_C_D = build1 (INDIRECT_REF, current_class_type, current_class_decl);
  4052.           temp = TREE_TYPE (TREE_TYPE (current_class_decl));
  4053.           TREE_READONLY (C_C_D) = TYPE_READONLY (temp);
  4054.           TREE_SIDE_EFFECTS (C_C_D) = TYPE_VOLATILE (temp);
  4055.           TREE_THIS_VOLATILE (C_C_D) = TYPE_VOLATILE (temp);
  4056.         }
  4057.       else
  4058.         C_C_D = current_class_decl;
  4059.     }
  4060.       else
  4061.     C_C_D = NULL_TREE;
  4062.     }
  4063.   else
  4064.     {
  4065.       current_class_decl = NULL_TREE;
  4066.       current_vtable_decl = NULL_TREE;
  4067.       C_C_D = NULL_TREE;
  4068.     }
  4069.  
  4070.   pop_memoized_context (modify);
  4071.  
  4072.  ret:
  4073.   ;
  4074. }
  4075.  
  4076. #if NEW_CLASS_SCOPING
  4077. /* When entering a class scope, all enclosing class scopes' names with
  4078.    static meaning (static variables, static functions, types and enumerators)
  4079.    have to be visible.  This recursive function calls pushclass for all
  4080.    enclosing class contexts until global or a local scope is reached.
  4081.    TYPE is the enclosed class and MODIFY is equivalent with the pushclass
  4082.    formal of the same name.  */
  4083.  
  4084. void
  4085. push_nested_class (type, modify)
  4086.      tree type;
  4087.      int modify;
  4088. {
  4089.   tree context = DECL_CONTEXT (TYPE_NAME (type));
  4090.  
  4091.   if (context && TREE_CODE (context) == RECORD_TYPE)
  4092.     push_nested_class (context, 2);
  4093.   pushclass (type, modify);
  4094. }
  4095.  
  4096. /* Undoes a push_nested_class call.  MODIFY is passed on to popclass.  */
  4097.  
  4098. void
  4099. pop_nested_class (modify)
  4100.      int modify;
  4101. {
  4102.   tree context = DECL_CONTEXT (TYPE_NAME (current_class_type));
  4103.  
  4104.   popclass (modify);
  4105.   if (context && TREE_CODE (context) == RECORD_TYPE)
  4106.     pop_nested_class (modify);
  4107. }
  4108. #endif
  4109.  
  4110. /* Set global variables CURRENT_LANG_NAME to appropriate value
  4111.    so that behavior of name-mangling machinery is correct.  */
  4112.  
  4113. void
  4114. push_lang_context (name)
  4115.      tree name;
  4116. {
  4117.   *current_lang_stack++ = current_lang_name;
  4118.   if (current_lang_stack >= current_lang_base + current_lang_stacksize)
  4119.     {
  4120.       current_lang_base =
  4121.     (tree *)xrealloc (current_lang_base,
  4122.               sizeof (tree) * (current_lang_stacksize + 10));
  4123.       current_lang_stack = current_lang_base + current_lang_stacksize;
  4124.       current_lang_stacksize += 10;
  4125.     }
  4126.  
  4127.   if (name == lang_name_cplusplus)
  4128.     {
  4129.       strict_prototype = strict_prototypes_lang_cplusplus;
  4130.       current_lang_name = name;
  4131.     }
  4132.   else if (name == lang_name_c)
  4133.     {
  4134.       strict_prototype = strict_prototypes_lang_c;
  4135.       current_lang_name = name;
  4136.     }
  4137.   else
  4138.     error ("language string `\"%s\"' not recognized", IDENTIFIER_POINTER (name));
  4139.  
  4140.   if (flag_cadillac)
  4141.     cadillac_push_lang (name);
  4142. }
  4143.   
  4144. /* Get out of the current language scope.  */
  4145. void
  4146. pop_lang_context ()
  4147. {
  4148.   if (flag_cadillac)
  4149.     cadillac_pop_lang ();
  4150.  
  4151.   current_lang_name = *--current_lang_stack;
  4152.   if (current_lang_name == lang_name_cplusplus)
  4153.     strict_prototype = strict_prototypes_lang_cplusplus;
  4154.   else if (current_lang_name == lang_name_c)
  4155.     strict_prototype = strict_prototypes_lang_c;
  4156. }
  4157.  
  4158. int
  4159. root_lang_context_p ()
  4160. {
  4161.   return current_lang_stack == current_lang_base;
  4162. }
  4163.  
  4164. /* Type instantiation routines.  */
  4165.  
  4166. /* This function will instantiate the type of the expression given
  4167.    in RHS to match the type of LHSTYPE.  If LHSTYPE is NULL_TREE,
  4168.    or other errors exist, the TREE_TYPE of RHS will be ERROR_MARK_NODE.
  4169.  
  4170.    This function is used in build_modify_expr, convert_arguments,
  4171.    build_c_cast, and compute_conversion_costs.  */
  4172. tree
  4173. instantiate_type (lhstype, rhs, complain)
  4174.      tree lhstype, rhs;
  4175.      int complain;
  4176. {
  4177.   if (TREE_CODE (lhstype) == UNKNOWN_TYPE)
  4178.     {
  4179.       if (complain)
  4180.     error ("not enough type information");
  4181.       return error_mark_node;
  4182.     }
  4183.  
  4184.   if (TREE_TYPE (rhs) != NULL_TREE && ! (type_unknown_p (rhs)))
  4185.     return rhs;
  4186.  
  4187.   /* This should really only be used when attempting to distinguish
  4188.      what sort of a pointer to function we have.  For now, any
  4189.      arithmetic operation which is not supported on pointers
  4190.      is rejected as an error.  */
  4191.  
  4192.   switch (TREE_CODE (rhs))
  4193.     {
  4194.     case TYPE_EXPR:
  4195.     case CONVERT_EXPR:
  4196.     case SAVE_EXPR:
  4197.     case CONSTRUCTOR:
  4198.     case BUFFER_REF:
  4199.       my_friendly_abort (177);
  4200.       return error_mark_node;
  4201.  
  4202.     case INDIRECT_REF:
  4203.     case ARRAY_REF:
  4204.       TREE_TYPE (rhs) = lhstype;
  4205.       lhstype = build_pointer_type (lhstype);
  4206.       TREE_OPERAND (rhs, 0)
  4207.     = instantiate_type (lhstype, TREE_OPERAND (rhs, 0), complain);
  4208.       if (TREE_OPERAND (rhs, 0) == error_mark_node)
  4209.     return error_mark_node;
  4210.  
  4211.       return rhs;
  4212.  
  4213.     case NOP_EXPR:
  4214.       rhs = copy_node (TREE_OPERAND (rhs, 0));
  4215.       TREE_TYPE (rhs) = unknown_type_node;
  4216.       return instantiate_type (lhstype, rhs, complain);
  4217.  
  4218.     case COMPONENT_REF:
  4219.       {
  4220.     tree field = TREE_OPERAND (rhs, 1);
  4221.     if (TREE_CODE (field) == TREE_LIST)
  4222.       {
  4223.         tree function = instantiate_type (lhstype, field, complain);
  4224.         if (function == error_mark_node)
  4225.           return error_mark_node;
  4226.         my_friendly_assert (TREE_CODE (function) == FUNCTION_DECL, 185);
  4227.         if (DECL_VINDEX (function))
  4228.           {
  4229.         tree base = TREE_OPERAND (rhs, 0);
  4230.         tree base_ptr = build_unary_op (ADDR_EXPR, base, 0);
  4231.         if (base_ptr == error_mark_node)
  4232.           return error_mark_node;
  4233.         base_ptr = convert_pointer_to (DECL_CONTEXT (function), base_ptr);
  4234.         if (base_ptr == error_mark_node)
  4235.           return error_mark_node;
  4236.         return build_vfn_ref (&base_ptr, base, DECL_VINDEX (function));
  4237.           }
  4238.         return function;
  4239.       }
  4240.  
  4241.     my_friendly_assert (TREE_CODE (field) == FIELD_DECL, 178);
  4242.     my_friendly_assert (!(TREE_CODE (TREE_TYPE (field)) == FUNCTION_TYPE
  4243.                   || TREE_CODE (TREE_TYPE (field)) == METHOD_TYPE),
  4244.                 179);
  4245.  
  4246.     TREE_TYPE (rhs) = lhstype;
  4247.     /* First look for an exact match  */
  4248.  
  4249.     while (field && TREE_TYPE (field) != lhstype)
  4250.       field = TREE_CHAIN (field);
  4251.     if (field)
  4252.       {
  4253.         TREE_OPERAND (rhs, 1) = field;
  4254.         return rhs;
  4255.       }
  4256.  
  4257.     /* No exact match found, look for a compatible function.  */
  4258.     field = TREE_OPERAND (rhs, 1);
  4259.     while (field && ! comptypes (lhstype, TREE_TYPE (field), 0))
  4260.       field = TREE_CHAIN (field);
  4261.     if (field)
  4262.       {
  4263.         TREE_OPERAND (rhs, 1) = field;
  4264.         field = TREE_CHAIN (field);
  4265.         while (field && ! comptypes (lhstype, TREE_TYPE (field), 0))
  4266.           field = TREE_CHAIN (field);
  4267.         if (field)
  4268.           {
  4269.         if (complain)
  4270.           error ("ambiguous overload for COMPONENT_REF requested");
  4271.         return error_mark_node;
  4272.           }
  4273.       }
  4274.     else
  4275.       {
  4276.         if (complain)
  4277.           error ("no appropriate overload exists for COMPONENT_REF");
  4278.         return error_mark_node;
  4279.       }
  4280.     return rhs;
  4281.       }
  4282.  
  4283.     case TREE_LIST:
  4284.       {
  4285.     tree elem, baselink, name;
  4286.     int globals = overloaded_globals_p (rhs);
  4287.  
  4288.     /* If there's only one function we know about, return that.  */
  4289.     if (globals > 0 && TREE_CHAIN (rhs) == NULL_TREE)
  4290.       return TREE_VALUE (rhs);
  4291.  
  4292.     /* First look for an exact match.  Search either overloaded
  4293.        functions or member functions.  May have to undo what
  4294.        `default_conversion' might do to lhstype.  */
  4295.  
  4296.     if (TREE_CODE (lhstype) == POINTER_TYPE)
  4297.       if (TREE_CODE (TREE_TYPE (lhstype)) == FUNCTION_TYPE
  4298.           || TREE_CODE (TREE_TYPE (lhstype)) == METHOD_TYPE)
  4299.         lhstype = TREE_TYPE (lhstype);
  4300.       else
  4301.         {
  4302.           if (complain)
  4303.         error ("invalid type combination for overload");
  4304.           return error_mark_node;
  4305.         }
  4306.  
  4307.     if (TREE_CODE (lhstype) != FUNCTION_TYPE && globals > 0)
  4308.       {
  4309.         if (complain)
  4310.           cp_error ("cannot resolve overloaded function `%D' based on non-function type",
  4311.              TREE_PURPOSE (rhs));
  4312.         return error_mark_node;
  4313.       }
  4314.  
  4315.     if (globals > 0)
  4316.       {
  4317.         my_friendly_assert (TREE_CODE (TREE_VALUE (rhs)) == FUNCTION_DECL,
  4318.                 180);
  4319.         elem = rhs;
  4320.         while (elem)
  4321.           if (TREE_TYPE (TREE_VALUE (elem)) != lhstype)
  4322.         elem = TREE_CHAIN (elem);
  4323.           else
  4324.         return TREE_VALUE (elem);
  4325.         /* No exact match found, look for a compatible function.  */
  4326.         elem = rhs;
  4327.         while (elem && ! comp_target_types (lhstype, TREE_TYPE (TREE_VALUE (elem)), 1))
  4328.           elem = TREE_CHAIN (elem);
  4329.         if (elem)
  4330.           {
  4331.         tree save_elem = TREE_VALUE (elem);
  4332.         elem = TREE_CHAIN (elem);
  4333.         while (elem && ! comp_target_types (lhstype, TREE_TYPE (TREE_VALUE (elem)), 0))
  4334.           elem = TREE_CHAIN (elem);
  4335.         if (elem)
  4336.           {
  4337.             if (complain)
  4338.               error ("ambiguous overload for overloaded function requested");
  4339.             return error_mark_node;
  4340.           }
  4341.         return save_elem;
  4342.           }
  4343.         if (complain)
  4344.           {
  4345.         cp_error ("cannot resolve overload to target type `%#T';",
  4346.               lhstype);
  4347.         cp_error ("no suitable overload of function `%D' exists",
  4348.               TREE_PURPOSE (rhs));
  4349.           }
  4350.         return error_mark_node;
  4351.       }
  4352.  
  4353.     if (TREE_NONLOCAL_FLAG (rhs))
  4354.       {
  4355.         /* Got to get it as a baselink.  */
  4356.         rhs = lookup_fnfields (TYPE_BINFO (current_class_type),
  4357.                    TREE_PURPOSE (rhs), 0);
  4358.       }
  4359.     else
  4360.       {
  4361.         my_friendly_assert (TREE_CHAIN (rhs) == NULL_TREE, 181);
  4362.         if (TREE_CODE (TREE_VALUE (rhs)) == TREE_LIST)
  4363.           rhs = TREE_VALUE (rhs);
  4364.         my_friendly_assert (TREE_CODE (TREE_VALUE (rhs)) == FUNCTION_DECL,
  4365.                 182);
  4366.       }
  4367.  
  4368.     for (baselink = rhs; baselink;
  4369.          baselink = next_baselink (baselink))
  4370.       {
  4371.         elem = TREE_VALUE (baselink);
  4372.         while (elem)
  4373.           if (TREE_TYPE (elem) != lhstype)
  4374.         elem = TREE_CHAIN (elem);
  4375.           else
  4376.         return elem;
  4377.       }
  4378.  
  4379.     /* No exact match found, look for a compatible method.  */
  4380.     for (baselink = rhs; baselink;
  4381.          baselink = next_baselink (baselink))
  4382.       {
  4383.         elem = TREE_VALUE (baselink);
  4384.         while (elem && ! comp_target_types (lhstype, TREE_TYPE (elem), 1))
  4385.           elem = TREE_CHAIN (elem);
  4386.         if (elem)
  4387.           {
  4388.         tree save_elem = elem;
  4389.         elem = TREE_CHAIN (elem);
  4390.         while (elem && ! comp_target_types (lhstype, TREE_TYPE (elem), 0))
  4391.           elem = TREE_CHAIN (elem);
  4392.         if (elem)
  4393.           {
  4394.             if (complain)
  4395.               error ("ambiguous overload for overloaded method requested");
  4396.             return error_mark_node;
  4397.           }
  4398.         return save_elem;
  4399.           }
  4400.         name = DECL_NAME (TREE_VALUE (rhs));
  4401.         if (TREE_CODE (lhstype) == FUNCTION_TYPE && globals < 0)
  4402.           {
  4403.         /* Try to instantiate from non-member functions.  */
  4404.         rhs = IDENTIFIER_GLOBAL_VALUE (name);
  4405.         if (rhs && TREE_CODE (rhs) == TREE_LIST)
  4406.           {
  4407.             /* This code seems to be missing a `return'.  */
  4408.             my_friendly_abort (4);
  4409.             instantiate_type (lhstype, rhs, complain);
  4410.           }
  4411.           }
  4412.       }
  4413.     if (complain)
  4414.       error ("no static member functions named `%s'",
  4415.          IDENTIFIER_POINTER (name));
  4416.     return error_mark_node;
  4417.       }
  4418.  
  4419.     case CALL_EXPR:
  4420.       /* This is too hard for now.  */
  4421.       my_friendly_abort (183);
  4422.       return error_mark_node;
  4423.  
  4424.     case PLUS_EXPR:
  4425.     case MINUS_EXPR:
  4426.     case COMPOUND_EXPR:
  4427.       TREE_OPERAND (rhs, 0) = instantiate_type (lhstype, TREE_OPERAND (rhs, 0), complain);
  4428.       if (TREE_OPERAND (rhs, 0) == error_mark_node)
  4429.     return error_mark_node;
  4430.       TREE_OPERAND (rhs, 1) = instantiate_type (lhstype, TREE_OPERAND (rhs, 1), complain);
  4431.       if (TREE_OPERAND (rhs, 1) == error_mark_node)
  4432.     return error_mark_node;
  4433.  
  4434.       TREE_TYPE (rhs) = lhstype;
  4435.       return rhs;
  4436.  
  4437.     case MULT_EXPR:
  4438.     case TRUNC_DIV_EXPR:
  4439.     case FLOOR_DIV_EXPR:
  4440.     case CEIL_DIV_EXPR:
  4441.     case ROUND_DIV_EXPR:
  4442.     case RDIV_EXPR:
  4443.     case TRUNC_MOD_EXPR:
  4444.     case FLOOR_MOD_EXPR:
  4445.     case CEIL_MOD_EXPR:
  4446.     case ROUND_MOD_EXPR:
  4447.     case FIX_ROUND_EXPR:
  4448.     case FIX_FLOOR_EXPR:
  4449.     case FIX_CEIL_EXPR:
  4450.     case FIX_TRUNC_EXPR:
  4451.     case FLOAT_EXPR:
  4452.     case NEGATE_EXPR:
  4453.     case ABS_EXPR:
  4454.     case MAX_EXPR:
  4455.     case MIN_EXPR:
  4456.     case FFS_EXPR:
  4457.  
  4458.     case BIT_AND_EXPR:
  4459.     case BIT_IOR_EXPR:
  4460.     case BIT_XOR_EXPR:
  4461.     case LSHIFT_EXPR:
  4462.     case RSHIFT_EXPR:
  4463.     case LROTATE_EXPR:
  4464.     case RROTATE_EXPR:
  4465.  
  4466.     case PREINCREMENT_EXPR:
  4467.     case PREDECREMENT_EXPR:
  4468.     case POSTINCREMENT_EXPR:
  4469.     case POSTDECREMENT_EXPR:
  4470.       if (complain)
  4471.     error ("illegal operation on uninstantiated type");
  4472.       return error_mark_node;
  4473.  
  4474.     case TRUTH_AND_EXPR:
  4475.     case TRUTH_OR_EXPR:
  4476.     case TRUTH_XOR_EXPR:
  4477.     case LT_EXPR:
  4478.     case LE_EXPR:
  4479.     case GT_EXPR:
  4480.     case GE_EXPR:
  4481.     case EQ_EXPR:
  4482.     case NE_EXPR:
  4483.     case TRUTH_ANDIF_EXPR:
  4484.     case TRUTH_ORIF_EXPR:
  4485.     case TRUTH_NOT_EXPR:
  4486.       if (complain)
  4487.     error ("not enough type information");
  4488.       return error_mark_node;
  4489.  
  4490.     case COND_EXPR:
  4491.       if (type_unknown_p (TREE_OPERAND (rhs, 0)))
  4492.     {
  4493.       if (complain)
  4494.         error ("not enough type information");
  4495.       return error_mark_node;
  4496.     }
  4497.       TREE_OPERAND (rhs, 1) = instantiate_type (lhstype, TREE_OPERAND (rhs, 1), complain);
  4498.       if (TREE_OPERAND (rhs, 1) == error_mark_node)
  4499.     return error_mark_node;
  4500.       TREE_OPERAND (rhs, 2) = instantiate_type (lhstype, TREE_OPERAND (rhs, 2), complain);
  4501.       if (TREE_OPERAND (rhs, 2) == error_mark_node)
  4502.     return error_mark_node;
  4503.  
  4504.       TREE_TYPE (rhs) = lhstype;
  4505.       return rhs;
  4506.  
  4507.     case MODIFY_EXPR:
  4508.       TREE_OPERAND (rhs, 1) = instantiate_type (lhstype, TREE_OPERAND (rhs, 1), complain);
  4509.       if (TREE_OPERAND (rhs, 1) == error_mark_node)
  4510.     return error_mark_node;
  4511.  
  4512.       TREE_TYPE (rhs) = lhstype;
  4513.       return rhs;
  4514.       
  4515.     case ADDR_EXPR:
  4516.       if (TREE_CODE (lhstype) != POINTER_TYPE)
  4517.     {
  4518.       if (complain)
  4519.         error ("type for resolving address of overloaded function must be pointer type");
  4520.       return error_mark_node;
  4521.     }
  4522.       TREE_TYPE (rhs) = lhstype;
  4523.       lhstype = TREE_TYPE (lhstype);
  4524.       TREE_OPERAND (rhs, 0) = instantiate_type (lhstype, TREE_OPERAND (rhs, 0), complain);
  4525.       if (TREE_OPERAND (rhs, 0) == error_mark_node)
  4526.     return error_mark_node;
  4527.  
  4528.       mark_addressable (TREE_OPERAND (rhs, 0));
  4529.       return rhs;
  4530.  
  4531.     case ENTRY_VALUE_EXPR:
  4532.       my_friendly_abort (184);
  4533.       return error_mark_node;
  4534.  
  4535.     case ERROR_MARK:
  4536.       return error_mark_node;
  4537.  
  4538.     default:
  4539.       my_friendly_abort (185);
  4540.       return error_mark_node;
  4541.     }
  4542. }
  4543.  
  4544. /* Return the name of the virtual function pointer field
  4545.    (as an IDENTIFIER_NODE) for the given TYPE.  Note that
  4546.    this may have to look back through base types to find the
  4547.    ultimate field name.  (For single inheritance, these could
  4548.    all be the same name.  Who knows for multiple inheritance).  */
  4549. static tree
  4550. get_vfield_name (type)
  4551.      tree type;
  4552. {
  4553.   tree binfo = TYPE_BINFO (type);
  4554.   char *buf;
  4555.  
  4556.   while (BINFO_BASETYPES (binfo)
  4557.      && TYPE_VIRTUAL_P (BINFO_TYPE (BINFO_BASETYPE (binfo, 0)))
  4558.      && ! TREE_VIA_VIRTUAL (BINFO_BASETYPE (binfo, 0)))
  4559.     binfo = BINFO_BASETYPE (binfo, 0);
  4560.  
  4561.   type = BINFO_TYPE (binfo);
  4562.   buf = (char *)alloca (sizeof (VFIELD_NAME_FORMAT)
  4563.             + TYPE_NAME_LENGTH (type) + 2);
  4564.   sprintf (buf, VFIELD_NAME_FORMAT, TYPE_NAME_STRING (type));
  4565.   return get_identifier (buf);
  4566. }
  4567.  
  4568. void
  4569. print_class_statistics ()
  4570. {
  4571. #ifdef GATHER_STATISTICS
  4572.   fprintf (stderr, "convert_harshness = %d\n", n_convert_harshness);
  4573.   fprintf (stderr, "compute_conversion_costs = %d\n", n_compute_conversion_costs);
  4574.   fprintf (stderr, "build_method_call = %d (inner = %d)\n",
  4575.        n_build_method_call, n_inner_fields_searched);
  4576.   if (n_vtables)
  4577.     {
  4578.       fprintf (stderr, "vtables = %d; vtable searches = %d\n",
  4579.            n_vtables, n_vtable_searches);
  4580.       fprintf (stderr, "vtable entries = %d; vtable elems = %d\n",
  4581.            n_vtable_entries, n_vtable_elems);
  4582.     }
  4583. #endif
  4584. }
  4585.  
  4586. #if NEW_CLASS_SCOPING
  4587. /* Push an obstack which is sufficiently long-lived to hold such class
  4588.    decls that may be cached in the previous_class_values list.  For now, let's
  4589.    use the permanent obstack, later we may create a dedicated obstack just
  4590.    for this purpose.  The effect is undone by pop_obstacks.  */
  4591. void
  4592. maybe_push_cache_obstack ()
  4593. {
  4594.   push_obstacks_nochange ();
  4595.   if (current_class_depth == 1)
  4596.     current_obstack = &permanent_obstack;
  4597. }
  4598. #endif
  4599.