home *** CD-ROM | disk | FTP | other *** search
/ OpenStep 4.2J (Developer) / os42jdev.iso / NextDeveloper / Source / GNU / gcc / cp / class.c < prev    next >
C/C++ Source or Header  |  1996-12-12  |  157KB  |  5,129 lines

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