home *** CD-ROM | disk | FTP | other *** search
/ OpenStep 4.2J (Developer) / os42jdev.iso / NextDeveloper / Source / GNU / gcc / cp / search.c < prev    next >
C/C++ Source or Header  |  1996-02-07  |  105KB  |  3,533 lines

  1. /* Breadth-first and depth-first routines for
  2.    searching multiple-inheritance lattice for GNU C++.
  3.    Copyright (C) 1987, 89, 92, 93, 94, 1995 Free Software Foundation, Inc.
  4.    Contributed by Michael Tiemann (tiemann@cygnus.com)
  5.  
  6. This file is part of GNU CC.
  7.  
  8. GNU CC is free software; you can redistribute it and/or modify
  9. it under the terms of the GNU General Public License as published by
  10. the Free Software Foundation; either version 2, or (at your option)
  11. any later version.
  12.  
  13. GNU CC is distributed in the hope that it will be useful,
  14. but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  16. GNU General Public License for more details.
  17.  
  18. You should have received a copy of the GNU General Public License
  19. along with GNU CC; see the file COPYING.  If not, write to
  20. the Free Software Foundation, 59 Temple Place - Suite 330,
  21. Boston, MA 02111-1307, USA.  */
  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 "obstack.h"
  30. #include "flags.h"
  31. #include "rtl.h"
  32. #include "output.h"
  33.  
  34. #define obstack_chunk_alloc xmalloc
  35. #define obstack_chunk_free free
  36.  
  37. void init_search ();
  38. extern struct obstack *current_obstack;
  39. extern tree abort_fndecl;
  40.  
  41. #include "stack.h"
  42.  
  43. /* Obstack used for remembering decision points of breadth-first.  */
  44. static struct obstack search_obstack;
  45.  
  46. /* Methods for pushing and popping objects to and from obstacks.  */
  47. struct stack_level *
  48. push_stack_level (obstack, tp, size)
  49.      struct obstack *obstack;
  50.      char *tp;  /* Sony NewsOS 5.0 compiler doesn't like void * here.  */
  51.      int size;
  52. {
  53.   struct stack_level *stack;
  54.   obstack_grow (obstack, tp, size);
  55.   stack = (struct stack_level *) ((char*)obstack_next_free (obstack) - size);
  56.   obstack_finish (obstack);
  57.   stack->obstack = obstack;
  58.   stack->first = (tree *) obstack_base (obstack);
  59.   stack->limit = obstack_room (obstack) / sizeof (tree *);
  60.   return stack;
  61. }
  62.  
  63. struct stack_level *
  64. pop_stack_level (stack)
  65.      struct stack_level *stack;
  66. {
  67.   struct stack_level *tem = stack;
  68.   struct obstack *obstack = tem->obstack;
  69.   stack = tem->prev;
  70.   obstack_free (obstack, tem);
  71.   return stack;
  72. }
  73.  
  74. #define search_level stack_level
  75. static struct search_level *search_stack;
  76.  
  77. static tree lookup_field_1 ();
  78. static int lookup_fnfields_1 ();
  79. static void dfs_walk ();
  80. static int markedp ();
  81. static void dfs_unmark ();
  82. static void dfs_init_vbase_pointers ();
  83.  
  84. static tree vbase_types;
  85. static tree vbase_decl, vbase_decl_ptr;
  86. static tree vbase_decl_ptr_intermediate;
  87. static tree vbase_init_result;
  88.  
  89. /* Allocate a level of searching.  */
  90. static struct search_level *
  91. push_search_level (stack, obstack)
  92.      struct stack_level *stack;
  93.      struct obstack *obstack;
  94. {
  95.   struct search_level tem;
  96.  
  97.   tem.prev = stack;
  98.   return push_stack_level (obstack, (char *)&tem, sizeof (tem));
  99. }
  100.  
  101. /* Discard a level of search allocation.  */
  102. static struct search_level *
  103. pop_search_level (obstack)
  104.      struct stack_level *obstack;
  105. {
  106.   register struct search_level *stack = pop_stack_level (obstack);
  107.  
  108.   return stack;
  109. }
  110.  
  111. /* Search memoization.  */
  112. struct type_level
  113. {
  114.   struct stack_level base;
  115.  
  116.   /* First object allocated in obstack of entries.  */
  117.   char *entries;
  118.  
  119.   /* Number of types memoized in this context.  */
  120.   int len;
  121.  
  122.   /* Type being memoized; save this if we are saving
  123.      memoized contexts.  */
  124.   tree type;
  125. };
  126.  
  127. /* Obstack used for memoizing member and member function lookup.  */
  128.  
  129. static struct obstack type_obstack, type_obstack_entries;
  130. static struct type_level *type_stack;
  131. static tree _vptr_name;
  132.  
  133. /* Make things that look like tree nodes, but allocate them
  134.    on type_obstack_entries.  */
  135. static int my_tree_node_counter;
  136. static tree my_tree_cons (), my_build_string ();
  137.  
  138. extern int flag_memoize_lookups, flag_save_memoized_contexts;
  139.  
  140. /* Variables for gathering statistics.  */
  141. static int my_memoized_entry_counter;
  142. static int memoized_fast_finds[2], memoized_adds[2], memoized_fast_rejects[2];
  143. static int memoized_fields_searched[2];
  144. static int n_fields_searched;
  145. static int n_calls_lookup_field, n_calls_lookup_field_1;
  146. static int n_calls_lookup_fnfields, n_calls_lookup_fnfields_1;
  147. static int n_calls_get_base_type;
  148. static int n_outer_fields_searched;
  149. static int n_contexts_saved;
  150.  
  151. /* Local variables to help save memoization contexts.  */
  152. static tree prev_type_memoized;
  153. static struct type_level *prev_type_stack;
  154.  
  155. /* This list is used by push_class_decls to know what decls need to
  156.    be pushed into class scope.  */
  157. static tree closed_envelopes = NULL_TREE;
  158.  
  159. /* Allocate a level of type memoization context.  */
  160. static struct type_level *
  161. push_type_level (stack, obstack)
  162.      struct stack_level *stack;
  163.      struct obstack *obstack;
  164. {
  165.   struct type_level tem;
  166.  
  167.   tem.base.prev = stack;
  168.  
  169.   obstack_finish (&type_obstack_entries);
  170.   tem.entries = (char *) obstack_base (&type_obstack_entries);
  171.   tem.len = 0;
  172.   tem.type = NULL_TREE;
  173.  
  174.   return (struct type_level *)push_stack_level (obstack, (char *)&tem, sizeof (tem));
  175. }
  176.  
  177. /* Discard a level of type memoization context.  */
  178.  
  179. static struct type_level *
  180. pop_type_level (stack)
  181.      struct type_level *stack;
  182. {
  183.   obstack_free (&type_obstack_entries, stack->entries);
  184.   return (struct type_level *)pop_stack_level ((struct stack_level *)stack);
  185. }
  186.  
  187. /* Make something that looks like a TREE_LIST, but
  188.    do it on the type_obstack_entries obstack.  */
  189. static tree
  190. my_tree_cons (purpose, value, chain)
  191.      tree purpose, value, chain;
  192. {
  193.   tree p = (tree)obstack_alloc (&type_obstack_entries, sizeof (struct tree_list));
  194.   ++my_tree_node_counter;
  195.   TREE_TYPE (p) = NULL_TREE;
  196.   ((HOST_WIDE_INT *)p)[3] = 0;
  197.   TREE_SET_CODE (p, TREE_LIST);
  198.   TREE_PURPOSE (p) = purpose;
  199.   TREE_VALUE (p) = value;
  200.   TREE_CHAIN (p) = chain;
  201.   return p;
  202. }
  203.  
  204. static tree
  205. my_build_string (str)
  206.      char *str;
  207. {
  208.   tree p = (tree)obstack_alloc (&type_obstack_entries, sizeof (struct tree_string));
  209.   ++my_tree_node_counter;
  210.   TREE_TYPE (p) = 0;
  211.   ((int *)p)[3] = 0;
  212.   TREE_SET_CODE (p, STRING_CST);
  213.   TREE_STRING_POINTER (p) = str;
  214.   TREE_STRING_LENGTH (p) = strlen (str);
  215.   return p;
  216. }
  217.  
  218. /* Memoizing machinery to make searches for multiple inheritance
  219.    reasonably efficient.  */
  220. #define MEMOIZE_HASHSIZE 8
  221. typedef struct memoized_entry
  222. {
  223.   struct memoized_entry *chain;
  224.   int uid;
  225.   tree data_members[MEMOIZE_HASHSIZE];
  226.   tree function_members[MEMOIZE_HASHSIZE];
  227. } *ME;
  228.  
  229. #define MEMOIZED_CHAIN(ENTRY) (((ME)ENTRY)->chain)
  230. #define MEMOIZED_UID(ENTRY) (((ME)ENTRY)->uid)
  231. #define MEMOIZED_FIELDS(ENTRY,INDEX) (((ME)ENTRY)->data_members[INDEX])
  232. #define MEMOIZED_FNFIELDS(ENTRY,INDEX) (((ME)ENTRY)->function_members[INDEX])
  233. /* The following is probably a lousy hash function.  */
  234. #define MEMOIZED_HASH_FN(NODE) (((long)(NODE)>>4)&(MEMOIZE_HASHSIZE - 1))
  235.  
  236. static struct memoized_entry *
  237. my_new_memoized_entry (chain)
  238.      struct memoized_entry *chain;
  239. {
  240.   struct memoized_entry *p =
  241.     (struct memoized_entry *)obstack_alloc (&type_obstack_entries,
  242.                         sizeof (struct memoized_entry));
  243.   bzero ((char *) p, sizeof (struct memoized_entry));
  244.   MEMOIZED_CHAIN (p) = chain;
  245.   MEMOIZED_UID (p) = ++my_memoized_entry_counter;
  246.   return p;
  247. }
  248.  
  249. /* Make an entry in the memoized table for type TYPE
  250.    that the entry for NAME is FIELD.  */
  251.  
  252. tree
  253. make_memoized_table_entry (type, name, function_p)
  254.      tree type, name;
  255.      int function_p;
  256. {
  257.   int index = MEMOIZED_HASH_FN (name);
  258.   tree entry, *prev_entry;
  259.  
  260.   memoized_adds[function_p] += 1;
  261.   if (CLASSTYPE_MTABLE_ENTRY (type) == 0)
  262.     {
  263.       obstack_ptr_grow (&type_obstack, type);
  264.       obstack_blank (&type_obstack, sizeof (struct memoized_entry *));
  265.       CLASSTYPE_MTABLE_ENTRY (type) = (char *)my_new_memoized_entry ((struct memoized_entry *)0);
  266.       type_stack->len++;
  267.       if (type_stack->len * 2 >= type_stack->base.limit)
  268.     my_friendly_abort (88);
  269.     }
  270.   if (function_p)
  271.     prev_entry = &MEMOIZED_FNFIELDS (CLASSTYPE_MTABLE_ENTRY (type), index);
  272.   else
  273.     prev_entry = &MEMOIZED_FIELDS (CLASSTYPE_MTABLE_ENTRY (type), index);
  274.  
  275.   entry = my_tree_cons (name, NULL_TREE, *prev_entry);
  276.   *prev_entry = entry;
  277.  
  278.   /* Don't know the error message to give yet.  */
  279.   TREE_TYPE (entry) = error_mark_node;
  280.  
  281.   return entry;
  282. }
  283.  
  284. /* When a new function or class context is entered, we build
  285.    a table of types which have been searched for members.
  286.    The table is an array (obstack) of types.  When a type is
  287.    entered into the obstack, its CLASSTYPE_MTABLE_ENTRY
  288.    field is set to point to a new record, of type struct memoized_entry.
  289.  
  290.    A non-NULL TREE_TYPE of the entry contains an access control error message.
  291.  
  292.    The slots for the data members are arrays of tree nodes.
  293.    These tree nodes are lists, with the TREE_PURPOSE
  294.    of this list the known member name, and the TREE_VALUE
  295.    as the FIELD_DECL for the member.
  296.  
  297.    For member functions, the TREE_PURPOSE is again the
  298.    name of the member functions for that class,
  299.    and the TREE_VALUE of the list is a pairs
  300.    whose TREE_PURPOSE is a member functions of this name,
  301.    and whose TREE_VALUE is a list of known argument lists this
  302.    member function has been called with.  The TREE_TYPE of the pair,
  303.    if non-NULL, is an error message to print.  */
  304.  
  305. /* Tell search machinery that we are entering a new context, and
  306.    to update tables appropriately.
  307.  
  308.    TYPE is the type of the context we are entering, which can
  309.    be NULL_TREE if we are not in a class's scope.
  310.  
  311.    USE_OLD, if nonzero tries to use previous context.  */
  312. void
  313. push_memoized_context (type, use_old)
  314.      tree type;
  315.      int use_old;
  316. {
  317.   int len;
  318.   tree *tem;
  319.  
  320.   if (prev_type_stack)
  321.     {
  322.       if (use_old && prev_type_memoized == type)
  323.     {
  324. #ifdef GATHER_STATISTICS
  325.       n_contexts_saved++;
  326. #endif
  327.       type_stack = prev_type_stack;
  328.       prev_type_stack = 0;
  329.  
  330.       tem = &type_stack->base.first[0];
  331.       len = type_stack->len;
  332.       while (len--)
  333.         CLASSTYPE_MTABLE_ENTRY (tem[len*2]) = (char *)tem[len*2+1];
  334.       return;
  335.     }
  336.       /* Otherwise, need to pop old stack here.  */
  337.       type_stack = pop_type_level (prev_type_stack);
  338.       prev_type_memoized = 0;
  339.       prev_type_stack = 0;
  340.     }
  341.  
  342.   type_stack = push_type_level ((struct stack_level *)type_stack,
  343.                 &type_obstack);
  344.   type_stack->type = type;
  345. }
  346.  
  347. /* Tell search machinery that we have left a context.
  348.    We do not currently save these contexts for later use.
  349.    If we wanted to, we could not use pop_search_level, since
  350.    poping that level allows the data we have collected to
  351.    be clobbered; a stack of obstacks would be needed.  */
  352. void
  353. pop_memoized_context (use_old)
  354.      int use_old;
  355. {
  356.   int len;
  357.   tree *tem = &type_stack->base.first[0];
  358.  
  359.   if (! flag_save_memoized_contexts)
  360.     use_old = 0;
  361.   else if (use_old)
  362.     {
  363.       len = type_stack->len;
  364.       while (len--)
  365.     tem[len*2+1] = (tree)CLASSTYPE_MTABLE_ENTRY (tem[len*2]);
  366.  
  367.       prev_type_stack = type_stack;
  368.       prev_type_memoized = type_stack->type;
  369.     }
  370.  
  371.   if (flag_memoize_lookups)
  372.     {
  373.       len = type_stack->len;
  374.       while (len--)
  375.     CLASSTYPE_MTABLE_ENTRY (tem[len*2])
  376.       = (char *)MEMOIZED_CHAIN (CLASSTYPE_MTABLE_ENTRY (tem[len*2]));
  377.     }
  378.   if (! use_old)
  379.     type_stack = pop_type_level (type_stack);
  380.   else
  381.     type_stack = (struct type_level *)type_stack->base.prev;
  382. }
  383.  
  384. /* Get a virtual binfo that is found inside BINFO's hierarchy that is
  385.    the same type as the type given in PARENT.  To be optimal, we want
  386.    the first one that is found by going through the least number of
  387.    virtual bases.  DEPTH should be NULL_PTR.  */
  388. static tree
  389. get_vbase (parent, binfo, depth)
  390.      tree parent, binfo;
  391.      unsigned int *depth;
  392. {
  393.   tree binfos;
  394.   int i, n_baselinks;
  395.   tree rval = NULL_TREE;
  396.  
  397.   if (depth == 0)
  398.     {
  399.       unsigned int d = (unsigned int)-1;
  400.       return get_vbase (parent, binfo, &d);
  401.     }
  402.  
  403.   if (BINFO_TYPE (binfo) == parent && TREE_VIA_VIRTUAL (binfo))
  404.     {
  405.       *depth = 0;
  406.       return binfo;
  407.     }
  408.  
  409.   *depth = *depth - 1;
  410.  
  411.   binfos = BINFO_BASETYPES (binfo);
  412.   n_baselinks = binfos ? TREE_VEC_LENGTH (binfos) : 0;
  413.  
  414.   /* Process base types.  */
  415.   for (i = 0; i < n_baselinks; i++)
  416.     {
  417.       tree base_binfo = TREE_VEC_ELT (binfos, i);
  418.       tree nrval;
  419.  
  420.       if (*depth == 0)
  421.     break;
  422.  
  423.       nrval = get_vbase (parent, base_binfo, depth);
  424.       if (nrval)
  425.     rval = nrval;
  426.     }
  427.   *depth = *depth+1;
  428.   return rval;
  429. }
  430.  
  431. /* Convert EXPR to a virtual base class of type TYPE.  We know that
  432.    EXPR is a non-null POINTER_TYPE to RECORD_TYPE.  We also know that
  433.    the type of what expr points to has a virtual base of type TYPE.  */
  434. tree
  435. convert_pointer_to_vbase (type, expr)
  436.      tree type;
  437.      tree expr;
  438. {
  439.   tree vb = get_vbase (type, TYPE_BINFO (TREE_TYPE (TREE_TYPE (expr))), NULL_PTR);
  440.   return convert_pointer_to_real (vb, expr);
  441. }
  442.  
  443. /* This is the newer recursive depth first search routine. */
  444. #if 0                /* unused */
  445. /* Return non-zero if PARENT is directly derived from TYPE.  By directly
  446.    we mean it's only one step up the inheritance lattice.  We check this
  447.    by walking horizontally across the types that TYPE directly inherits
  448.    from, to see if PARENT is among them.  This is used by get_binfo and
  449.    by compute_access.  */
  450. static int
  451. immediately_derived (parent, type)
  452.      tree parent, type;
  453. {
  454.   if (TYPE_BINFO (type))
  455.     {
  456.       tree binfos = BINFO_BASETYPES (TYPE_BINFO (type));
  457.       int i, n_baselinks = binfos ? TREE_VEC_LENGTH (binfos) : 0;
  458.  
  459.       for (i = 0; i < n_baselinks; i++)
  460.     {
  461.       tree base_binfo = TREE_VEC_ELT (binfos, i);
  462.  
  463.       if (parent == BINFO_TYPE (base_binfo))
  464.         return 1;
  465.     }
  466.     }
  467.   return 0;
  468. }
  469. #endif
  470.  
  471. /* Check whether the type given in BINFO is derived from PARENT.  If
  472.    it isn't, return 0.  If it is, but the derivation is MI-ambiguous
  473.    AND protect != 0, emit an error message and return error_mark_node.
  474.  
  475.    Otherwise, if TYPE is derived from PARENT, return the actual base
  476.    information, unless a one of the protection violations below
  477.    occurs, in which case emit an error message and return error_mark_node.
  478.  
  479.    If PROTECT is 1, then check if access to a public field of PARENT
  480.    would be private.  Also check for ambiguity.  */
  481.  
  482. tree
  483. get_binfo (parent, binfo, protect)
  484.      register tree parent, binfo;
  485.      int protect;
  486. {
  487.   tree type;
  488.   int dist;
  489.   tree rval = NULL_TREE;
  490.   
  491.   if (TREE_CODE (parent) == TREE_VEC)
  492.     parent = BINFO_TYPE (parent);
  493.   else if (! IS_AGGR_TYPE_CODE (TREE_CODE (parent)))
  494.     my_friendly_abort (89);
  495.  
  496.   if (TREE_CODE (binfo) == TREE_VEC)
  497.     type = BINFO_TYPE (binfo);
  498.   else if (IS_AGGR_TYPE_CODE (TREE_CODE (binfo)))
  499.     type = binfo;
  500.   else
  501.     my_friendly_abort (90);
  502.   
  503.   dist = get_base_distance (parent, binfo, protect, &rval);
  504.  
  505.   if (dist == -3)
  506.     {
  507.       cp_error ("fields of `%T' are inaccessible in `%T' due to private inheritance",
  508.         parent, type);
  509.       return error_mark_node;
  510.     }
  511.   else if (dist == -2 && protect)
  512.     {
  513.       cp_error ("type `%T' is ambiguous base class for type `%T'", parent,
  514.         type);
  515.       return error_mark_node;
  516.     }
  517.  
  518. #ifdef OBJCPLUS
  519.   if (!rval)
  520.     {
  521.       if (objc_comptypes (parent, type, 1) == 1)
  522.     return parent;
  523.     }
  524. #endif
  525.  
  526.   return rval;
  527. }
  528.  
  529. /* This is the newer depth first get_base_distance routine.  */
  530. static int
  531. get_base_distance_recursive (binfo, depth, is_private, basetype_path, rval,
  532.                  rval_private_ptr, new_binfo_ptr, parent, path_ptr,
  533.                  protect, via_virtual_ptr, via_virtual)
  534.      tree binfo, basetype_path, *new_binfo_ptr, parent, *path_ptr;
  535.      int *rval_private_ptr, depth, is_private, rval, protect, *via_virtual_ptr,
  536.        via_virtual;
  537. {
  538.   tree binfos;
  539.   int i, n_baselinks;
  540.  
  541.   if (BINFO_TYPE (binfo) == parent || binfo == parent)
  542.     {
  543.       if (rval == -1)
  544.     {
  545.       rval = depth;
  546.       *rval_private_ptr = is_private;
  547.       *new_binfo_ptr = binfo;
  548.       *via_virtual_ptr = via_virtual;
  549.     }
  550.       else
  551.     {
  552.       int same_object = (tree_int_cst_equal (BINFO_OFFSET (*new_binfo_ptr),
  553.                          BINFO_OFFSET (binfo))
  554.                  && *via_virtual_ptr && via_virtual);
  555.                  
  556.       if (*via_virtual_ptr && via_virtual==0)
  557.         {
  558.           *rval_private_ptr = is_private;
  559.           *new_binfo_ptr = binfo;
  560.           *via_virtual_ptr = via_virtual;
  561.         }
  562.       else if (same_object)
  563.         {
  564.           if (*rval_private_ptr && ! is_private)
  565.         {
  566.           *rval_private_ptr = is_private;
  567.           *new_binfo_ptr = binfo;
  568.           *via_virtual_ptr = via_virtual;
  569.         }
  570.           return rval;
  571.         }
  572.  
  573.       rval = -2;
  574.     }
  575.       return rval;
  576.     }
  577.  
  578.   binfos = BINFO_BASETYPES (binfo);
  579.   n_baselinks = binfos ? TREE_VEC_LENGTH (binfos) : 0;
  580.   depth += 1;
  581.  
  582.   /* Process base types.  */
  583.   for (i = 0; i < n_baselinks; i++)
  584.     {
  585.       tree base_binfo = TREE_VEC_ELT (binfos, i);
  586.  
  587.       /* Find any specific instance of a virtual base, when searching with
  588.      a binfo... */
  589.       if (BINFO_MARKED (base_binfo) == 0 || TREE_CODE (parent) == TREE_VEC)
  590.     {
  591.       int via_private
  592.         = (protect
  593.            && (is_private
  594.            || (!TREE_VIA_PUBLIC (base_binfo)
  595.                && !is_friend (BINFO_TYPE (binfo), current_scope ()))));
  596.       int this_virtual = via_virtual || TREE_VIA_VIRTUAL (base_binfo);
  597.       int was;
  598.  
  599.       /* When searching for a non-virtual, we cannot mark
  600.          virtually found binfos. */
  601.       if (! this_virtual)
  602.         SET_BINFO_MARKED (base_binfo);
  603.  
  604. #define WATCH_VALUES(rval, via_private) (rval == -1 ? 3 : via_private)
  605.  
  606.       was = WATCH_VALUES (rval, *via_virtual_ptr);
  607.       rval = get_base_distance_recursive (base_binfo, depth, via_private,
  608.                           binfo, rval, rval_private_ptr,
  609.                           new_binfo_ptr, parent, path_ptr,
  610.                           protect, via_virtual_ptr,
  611.                           this_virtual);
  612.       /* watch for updates; only update if path is good. */
  613.       if (path_ptr && WATCH_VALUES (rval, *via_virtual_ptr) != was)
  614.         BINFO_INHERITANCE_CHAIN (base_binfo) = binfo;
  615.       if (rval == -2 && *via_virtual_ptr == 0)
  616.         return rval;
  617.  
  618. #undef WATCH_VALUES
  619.  
  620.     }
  621.     }
  622.  
  623.   return rval;
  624. }
  625.  
  626. /* Return the number of levels between type PARENT and the type given
  627.    in BINFO, following the leftmost path to PARENT not found along a
  628.    virtual path, if there are no real PARENTs (all come from virtual
  629.    base classes), then follow the leftmost path to PARENT.
  630.  
  631.    Return -1 if TYPE is not derived from PARENT.
  632.    Return -2 if PARENT is an ambiguous base class of TYPE, and PROTECT is
  633.     non-negative.
  634.    Return -3 if PARENT is private to TYPE, and PROTECT is non-zero.
  635.  
  636.    If PATH_PTR is non-NULL, then also build the list of types
  637.    from PARENT to TYPE, with TREE_VIA_VIRTUAL and TREE_VIA_PUBLIC
  638.    set.
  639.  
  640.    PARENT can also be a binfo, in which case that exact parent is found
  641.    and no other.  convert_pointer_to_real uses this functionality.
  642.  
  643.    If BINFO is a binfo, its BINFO_INHERITANCE_CHAIN will be left alone.  */
  644.  
  645. int
  646. get_base_distance (parent, binfo, protect, path_ptr)
  647.      register tree parent, binfo;
  648.      int protect;
  649.      tree *path_ptr;
  650. {
  651.   int rval;
  652.   int rval_private = 0;
  653.   tree type;
  654.   tree new_binfo = NULL_TREE;
  655.   int via_virtual;
  656.   int watch_access = protect;
  657.  
  658.   if (TREE_CODE (parent) != TREE_VEC)
  659.     parent = TYPE_MAIN_VARIANT (parent);
  660.  
  661.   if (TREE_CODE (binfo) == TREE_VEC)
  662.     type = BINFO_TYPE (binfo);
  663.   else if (IS_AGGR_TYPE_CODE (TREE_CODE (binfo)))
  664.     {
  665.       type = binfo;
  666.       binfo = TYPE_BINFO (type);
  667.  
  668.       if (path_ptr)
  669.     BINFO_INHERITANCE_CHAIN (binfo) = NULL_TREE;
  670.     }
  671.   else
  672.     my_friendly_abort (92);
  673.  
  674.   if (parent == type || parent == binfo)
  675.     {
  676.       /* If the distance is 0, then we don't really need
  677.      a path pointer, but we shouldn't let garbage go back.  */
  678.       if (path_ptr)
  679.     *path_ptr = binfo;
  680.       return 0;
  681.     }
  682.  
  683.   if (path_ptr)
  684.     watch_access = 1;
  685.  
  686.   rval = get_base_distance_recursive (binfo, 0, 0, NULL_TREE, -1,
  687.                       &rval_private, &new_binfo, parent,
  688.                       path_ptr, watch_access, &via_virtual, 0);
  689.  
  690.   dfs_walk (binfo, dfs_unmark, markedp);
  691.  
  692.   /* Access restrictions don't count if we found an ambiguous basetype.  */
  693.   if (rval == -2 && protect >= 0)
  694.     rval_private = 0;
  695.  
  696.   if (rval && protect && rval_private)
  697.     return -3;
  698.  
  699.   /* find real virtual base classes. */
  700.   if (rval == -1 && TREE_CODE (parent) == TREE_VEC
  701.       && parent == binfo_member (BINFO_TYPE (parent),
  702.                  CLASSTYPE_VBASECLASSES (type)))
  703.     {
  704.       BINFO_INHERITANCE_CHAIN (parent) = binfo;
  705.       new_binfo = parent;
  706.       rval = 1;
  707.     }
  708.  
  709.   if (path_ptr)
  710.     *path_ptr = new_binfo;
  711.   return rval;
  712. }
  713.  
  714. /* Search for a member with name NAME in a multiple inheritance lattice
  715.    specified by TYPE.  If it does not exist, return NULL_TREE.
  716.    If the member is ambiguously referenced, return `error_mark_node'.
  717.    Otherwise, return the FIELD_DECL.  */
  718.  
  719. /* Do a 1-level search for NAME as a member of TYPE.  The caller must
  720.    figure out whether it can access this field.  (Since it is only one
  721.    level, this is reasonable.)  */
  722. static tree
  723. lookup_field_1 (type, name)
  724.      tree type, name;
  725. {
  726.   register tree field = TYPE_FIELDS (type);
  727.  
  728. #ifdef GATHER_STATISTICS
  729.   n_calls_lookup_field_1++;
  730. #endif
  731.   while (field)
  732.     {
  733. #ifdef GATHER_STATISTICS
  734.       n_fields_searched++;
  735. #endif
  736.       if (DECL_NAME (field) == NULL_TREE
  737.       && TREE_CODE (TREE_TYPE (field)) == UNION_TYPE)
  738.     {
  739.       tree temp = lookup_field_1 (TREE_TYPE (field), name);
  740.       if (temp)
  741.         return temp;
  742.     }
  743.       if (DECL_NAME (field) == name)
  744.     {
  745.       if ((TREE_CODE(field) == VAR_DECL || TREE_CODE(field) == CONST_DECL)
  746.           && DECL_ASSEMBLER_NAME (field) != NULL)
  747.         GNU_xref_ref(current_function_decl,
  748.              IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (field)));
  749.       return field;
  750.     }
  751.       field = TREE_CHAIN (field);
  752.     }
  753.   /* Not found.  */
  754.   if (name == _vptr_name)
  755.     {
  756.       /* Give the user what s/he thinks s/he wants.  */
  757.       if (TYPE_VIRTUAL_P (type))
  758.     return CLASSTYPE_VFIELD (type);
  759.     }
  760.   return NULL_TREE;
  761. }
  762.  
  763. /* There are a number of cases we need to be aware of here:
  764.              current_class_type    current_function_decl
  765.    * global            NULL            NULL
  766.    * fn-local            NULL            SET
  767.    * class-local        SET            NULL
  768.    * class->fn            SET            SET
  769.    * fn->class            SET            SET
  770.  
  771.    Those last two make life interesting.  If we're in a function which is
  772.    itself inside a class, we need decls to go into the fn's decls (our
  773.    second case below).  But if we're in a class and the class itself is
  774.    inside a function, we need decls to go into the decls for the class.  To
  775.    achieve this last goal, we must see if, when both current_class_decl and
  776.    current_function_decl are set, the class was declared inside that
  777.    function.  If so, we know to put the decls into the class's scope.  */
  778.  
  779. tree
  780. current_scope ()
  781. {
  782.   if (current_function_decl == NULL_TREE)
  783.     return current_class_type;
  784.   if (current_class_type == NULL_TREE)
  785.     return current_function_decl;
  786.   if (DECL_CLASS_CONTEXT (current_function_decl) == current_class_type)
  787.     return current_function_decl;
  788.  
  789.   return current_class_type;
  790. }
  791.  
  792. /* Compute the access of FIELD.  This is done by computing
  793.    the access available to each type in BASETYPES (which comes
  794.    as a list of [via_public/basetype] in reverse order, namely base
  795.    class before derived class).  The first one which defines a
  796.    access defines the access for the field.  Otherwise, the
  797.    access of the field is that which occurs normally.
  798.  
  799.    Uses global variables CURRENT_CLASS_TYPE and
  800.    CURRENT_FUNCTION_DECL to use friend relationships
  801.    if necessary.
  802.  
  803.    This will be static when lookup_fnfield comes into this file.
  804.  
  805.    access_public means that the field can be accessed by the current lexical
  806.    scope.
  807.  
  808.    access_protected means that the field cannot be accessed by the current
  809.    lexical scope because it is protected.
  810.  
  811.    access_private means that the field cannot be accessed by the current
  812.    lexical scope because it is private. */
  813.  
  814. #if 0
  815. #define PUBLIC_RETURN return (DECL_PUBLIC (field) = 1), access_public
  816. #define PROTECTED_RETURN return (DECL_PROTECTED (field) = 1), access_protected
  817. #define PRIVATE_RETURN return (DECL_PRIVATE (field) = 1), access_private
  818. #else
  819. #define PUBLIC_RETURN return access_public
  820. #define PROTECTED_RETURN return access_protected
  821. #define PRIVATE_RETURN return access_private
  822. #endif
  823.  
  824. #if 0
  825. /* Disabled with DECL_PUBLIC &c.  */
  826. static tree previous_scope = NULL_TREE;
  827. #endif
  828.  
  829. enum access_type
  830. compute_access (basetype_path, field)
  831.      tree basetype_path, field;
  832. {
  833.   enum access_type access;
  834.   tree types;
  835.   tree context;
  836.   int protected_ok, via_protected;
  837.   extern int flag_access_control;
  838. #if 1
  839.   /* Replaces static decl above.  */
  840.   tree previous_scope;
  841. #endif
  842.   int static_mem =
  843.     ((TREE_CODE (field) == FUNCTION_DECL && DECL_STATIC_FUNCTION_P (field))
  844.      || (TREE_CODE (field) != FUNCTION_DECL && TREE_STATIC (field)));
  845.  
  846.   if (! flag_access_control)
  847.     return access_public;
  848.  
  849.   /* The field lives in the current class.  */
  850.   if (BINFO_TYPE (basetype_path) == current_class_type)
  851.     return access_public;
  852.  
  853. #if 0
  854.   /* Disabled until pushing function scope clears these out.  If ever.  */
  855.   /* Make these special cases fast.  */
  856.   if (current_scope () == previous_scope)
  857.     {
  858.       if (DECL_PUBLIC (field))
  859.     return access_public;
  860.       if (DECL_PROTECTED (field))
  861.     return access_protected;
  862.       if (DECL_PRIVATE (field))
  863.     return access_private;
  864.     }
  865. #endif
  866.  
  867.   /* We don't currently support access control on nested types.  */
  868.   if (TREE_CODE (field) == TYPE_DECL)
  869.     return access_public;
  870.  
  871.   previous_scope = current_scope ();
  872.   
  873.   context = DECL_CLASS_CONTEXT (field);
  874.   if (context == NULL_TREE)
  875.     context = DECL_CONTEXT (field);
  876.  
  877.   /* Fields coming from nested anonymous unions have their DECL_CLASS_CONTEXT
  878.      slot set to the union type rather than the record type containing
  879.      the anonymous union.  In this case, DECL_FIELD_CONTEXT is correct.  */
  880.   if (context && TREE_CODE (context) == UNION_TYPE
  881.       && ANON_AGGRNAME_P (TYPE_IDENTIFIER (context)))
  882.     context = DECL_FIELD_CONTEXT (field);
  883.  
  884.   /* Virtual function tables are never private.  But we should know that
  885.      we are looking for this, and not even try to hide it.  */
  886.   if (DECL_NAME (field) && VFIELD_NAME_P (DECL_NAME (field)) == 1)
  887.     PUBLIC_RETURN;
  888.  
  889.   /* Member found immediately within object.  */
  890.   if (BINFO_INHERITANCE_CHAIN (basetype_path) == NULL_TREE)
  891.     {
  892.       /* Are we (or an enclosing scope) friends with the class that has
  893.          FIELD? */
  894.       if (is_friend (context, previous_scope))
  895.     PUBLIC_RETURN;
  896.  
  897.       /* If it's private, it's private, you letch.  */
  898.       if (TREE_PRIVATE (field))
  899.     PRIVATE_RETURN;
  900.  
  901.       /* ARM $11.5.  Member functions of a derived class can access the
  902.      non-static protected members of a base class only through a
  903.      pointer to the derived class, a reference to it, or an object
  904.      of it. Also any subsequently derived classes also have
  905.      access.  */
  906.       else if (TREE_PROTECTED (field))
  907.     {
  908.       if (current_class_type
  909.           && static_mem
  910.             && ACCESSIBLY_DERIVED_FROM_P (context, current_class_type))
  911.         PUBLIC_RETURN;
  912.       else
  913.         PROTECTED_RETURN;
  914.     }
  915.       else
  916.     PUBLIC_RETURN;
  917.     }
  918.  
  919.   /* must reverse more than one element */
  920.   basetype_path = reverse_path (basetype_path);
  921.   types = basetype_path;
  922.   via_protected = 0;
  923.   access = access_default;
  924.   protected_ok = static_mem && current_class_type
  925.     && ACCESSIBLY_DERIVED_FROM_P (BINFO_TYPE (types), current_class_type);
  926.  
  927.   while (1)
  928.     {
  929.       tree member;
  930.       tree binfo = types;
  931.       tree type = BINFO_TYPE (binfo);
  932.       int private_ok = 0;
  933.  
  934.       /* Friends of a class can see protected members of its bases.
  935.          Note that classes are their own friends.  */
  936.       if (is_friend (type, previous_scope))
  937.     {
  938.       protected_ok = 1;
  939.       private_ok = 1;
  940.     }
  941.  
  942.       member = purpose_member (type, DECL_ACCESS (field));
  943.       if (member)
  944.     {
  945.       access = (enum access_type) TREE_VALUE (member);
  946.       break;
  947.     }
  948.  
  949.       types = BINFO_INHERITANCE_CHAIN (types);
  950.  
  951.       /* If the next type was VIA_PROTECTED, then fields of all remaining
  952.      classes past that one are *at least* protected.  */
  953.       if (types)
  954.     {
  955.       if (TREE_VIA_PROTECTED (types))
  956.         via_protected = 1;
  957.       else if (! TREE_VIA_PUBLIC (types) && ! private_ok)
  958.         {
  959.           access = access_private;
  960.           break;
  961.         }
  962.     }
  963.       else
  964.     break;
  965.     }
  966.   reverse_path (basetype_path);
  967.  
  968.   /* No special visibilities apply.  Use normal rules.  */
  969.  
  970.   if (access == access_default)
  971.     {
  972.       if (is_friend (context, previous_scope))
  973.     access = access_public;
  974.       else if (TREE_PRIVATE (field))
  975.     access = access_private;
  976.       else if (TREE_PROTECTED (field))
  977.     access = access_protected;
  978.       else
  979.     access = access_public;
  980.     }
  981.  
  982.   if (access == access_public && via_protected)
  983.     access = access_protected;
  984.  
  985.   if (access == access_protected && protected_ok)
  986.     access = access_public;
  987.  
  988. #if 0
  989.   if (access == access_public)
  990.     DECL_PUBLIC (field) = 1;
  991.   else if (access == access_protected)
  992.     DECL_PROTECTED (field) = 1;
  993.   else if (access == access_private)
  994.     DECL_PRIVATE (field) = 1;
  995.   else my_friendly_abort (96);
  996. #endif
  997.   return access;
  998. }
  999.  
  1000. /* Routine to see if the sub-object denoted by the binfo PARENT can be
  1001.    found as a base class and sub-object of the object denoted by
  1002.    BINFO.  This routine relies upon binfos not being shared, except
  1003.    for binfos for virtual bases.  */
  1004. static int
  1005. is_subobject_of_p (parent, binfo)
  1006.      tree parent, binfo;
  1007. {
  1008.   tree binfos = BINFO_BASETYPES (binfo);
  1009.   int i, n_baselinks = binfos ? TREE_VEC_LENGTH (binfos) : 0;
  1010.  
  1011.   if (parent == binfo)
  1012.     return 1;
  1013.  
  1014.   /* Process and/or queue base types.  */
  1015.   for (i = 0; i < n_baselinks; i++)
  1016.     {
  1017.       tree base_binfo = TREE_VEC_ELT (binfos, i);
  1018.       if (TREE_VIA_VIRTUAL (base_binfo))
  1019.     base_binfo = TYPE_BINFO (BINFO_TYPE (base_binfo));
  1020.       if (is_subobject_of_p (parent, base_binfo))
  1021.     return 1;
  1022.     }
  1023.   return 0;
  1024. }
  1025.  
  1026. /* See if a one FIELD_DECL hides another.  This routine is meant to
  1027.    correspond to ANSI working paper Sept 17, 1992 10p4.  The two
  1028.    binfos given are the binfos corresponding to the particular places
  1029.    the FIELD_DECLs are found.  This routine relies upon binfos not
  1030.    being shared, except for virtual bases. */
  1031. static int
  1032. hides (hider_binfo, hidee_binfo)
  1033.      tree hider_binfo, hidee_binfo;
  1034. {
  1035.   /* hider hides hidee, if hider has hidee as a base class and
  1036.      the instance of hidee is a sub-object of hider.  The first
  1037.      part is always true is the second part is true.
  1038.  
  1039.      When hider and hidee are the same (two ways to get to the exact
  1040.      same member) we consider either one as hiding the other. */
  1041.   return is_subobject_of_p (hidee_binfo, hider_binfo);
  1042. }
  1043.  
  1044. /* Very similar to lookup_fnfields_1 but it ensures that at least one
  1045.    function was declared inside the class given by TYPE.  It really should
  1046.    only return functions that match the given TYPE.  */
  1047. static int
  1048. lookup_fnfields_here (type, name)
  1049.      tree type, name;
  1050. {
  1051.   int index = lookup_fnfields_1 (type, name);
  1052.   tree fndecls;
  1053.  
  1054.   if (index <= 0)
  1055.     return index;
  1056.   fndecls = TREE_VEC_ELT (CLASSTYPE_METHOD_VEC (type), index);
  1057.   while (fndecls)
  1058.     {
  1059.       if (TYPE_MAIN_VARIANT (DECL_CLASS_CONTEXT (fndecls))
  1060.       == TYPE_MAIN_VARIANT (type))
  1061.     return index;
  1062.       fndecls = TREE_CHAIN (fndecls);
  1063.     }
  1064.   return -1;
  1065. }
  1066.  
  1067. /* Look for a field named NAME in an inheritance lattice dominated by
  1068.    XBASETYPE.  PROTECT is zero if we can avoid computing access
  1069.    information, otherwise it is 1.  WANT_TYPE is 1 when we should only
  1070.    return TYPE_DECLs, if no TYPE_DECL can be found return NULL_TREE.
  1071.  
  1072.    It was not clear what should happen if WANT_TYPE is set, and an
  1073.    ambiguity is found.  At least one use (lookup_name) to not see
  1074.    the error.  */
  1075. tree
  1076. lookup_field (xbasetype, name, protect, want_type)
  1077.      register tree xbasetype, name;
  1078.      int protect, want_type;
  1079. {
  1080.   int head = 0, tail = 0;
  1081.   tree rval, rval_binfo = NULL_TREE, rval_binfo_h;
  1082.   tree type, basetype_chain, basetype_path;
  1083.   enum access_type this_v = access_default;
  1084.   tree entry, binfo, binfo_h;
  1085.   enum access_type own_access = access_default;
  1086.   int vbase_name_p = VBASE_NAME_P (name);
  1087.  
  1088.   /* rval_binfo is the binfo associated with the found member, note,
  1089.      this can be set with useful information, even when rval is not
  1090.      set, because it must deal with ALL members, not just non-function
  1091.      members.  It is used for ambiguity checking and the hidden
  1092.      checks.  Whereas rval is only set if a proper (not hidden)
  1093.      non-function member is found.  */
  1094.  
  1095.   /* rval_binfo_h and binfo_h are binfo values used when we perform the
  1096.      hiding checks, as virtual base classes may not be shared.  The strategy
  1097.      is we always go into the the binfo hierarchy owned by TYPE_BINFO of
  1098.      virtual base classes, as we cross virtual base class lines.  This way
  1099.      we know that binfo of a virtual base class will always == itself when
  1100.      found along any line.  (mrs)  */
  1101.  
  1102.   char *errstr = 0;
  1103.  
  1104.   /* Set this to nonzero if we don't know how to compute
  1105.      accurate error messages for access control.  */
  1106.   int index = MEMOIZED_HASH_FN (name);
  1107.  
  1108.   /* If we are looking for a constructor in a templated type, use the
  1109.      unspecialized name, as that is how we store it.  */
  1110.   if (IDENTIFIER_TEMPLATE (name))
  1111.     name = constructor_name (name);
  1112.  
  1113.   if (TREE_CODE (xbasetype) == TREE_VEC)
  1114.     {
  1115.       type = BINFO_TYPE (xbasetype);
  1116.       basetype_path = xbasetype;
  1117.     }
  1118.   else if (IS_AGGR_TYPE_CODE (TREE_CODE (xbasetype)))
  1119.     {
  1120.       type = xbasetype;
  1121.       basetype_path = TYPE_BINFO (xbasetype);
  1122.       BINFO_VIA_PUBLIC (basetype_path) = 1;
  1123.       BINFO_INHERITANCE_CHAIN (basetype_path) = NULL_TREE;
  1124.     }
  1125.   else my_friendly_abort (97);
  1126.  
  1127.   if (CLASSTYPE_MTABLE_ENTRY (type))
  1128.     {
  1129.       tree tem = MEMOIZED_FIELDS (CLASSTYPE_MTABLE_ENTRY (type), index);
  1130.  
  1131.       while (tem && TREE_PURPOSE (tem) != name)
  1132.     {
  1133.       memoized_fields_searched[0]++;
  1134.       tem = TREE_CHAIN (tem);
  1135.     }
  1136.       if (tem)
  1137.     {
  1138.       if (protect && TREE_TYPE (tem))
  1139.         {
  1140.           error (TREE_STRING_POINTER (TREE_TYPE (tem)),
  1141.              IDENTIFIER_POINTER (name),
  1142.              TYPE_NAME_STRING (DECL_FIELD_CONTEXT (TREE_VALUE (tem))));
  1143.           return error_mark_node;
  1144.         }
  1145.       if (TREE_VALUE (tem) == NULL_TREE)
  1146.         memoized_fast_rejects[0] += 1;
  1147.       else
  1148.         memoized_fast_finds[0] += 1;
  1149.       return TREE_VALUE (tem);
  1150.     }
  1151.     }
  1152.  
  1153. #ifdef GATHER_STATISTICS
  1154.   n_calls_lookup_field++;
  1155. #endif
  1156.   if (protect && flag_memoize_lookups && ! global_bindings_p ())
  1157.     entry = make_memoized_table_entry (type, name, 0);
  1158.   else
  1159.     entry = 0;
  1160.  
  1161.   rval = lookup_field_1 (type, name);
  1162.  
  1163.   if (rval || lookup_fnfields_here (type, name) >= 0)
  1164.     {
  1165.       if (rval)
  1166.     {
  1167.       if (want_type)
  1168.         {
  1169.           if (TREE_CODE (rval) != TYPE_DECL)
  1170.         {
  1171.           rval = purpose_member (name, CLASSTYPE_TAGS (type));
  1172.           if (rval)
  1173.             rval = TYPE_MAIN_DECL (TREE_VALUE (rval));
  1174.         }
  1175.         }
  1176.       else
  1177.         {
  1178.           if (TREE_CODE (rval) == TYPE_DECL
  1179.           && lookup_fnfields_here (type, name) >= 0)
  1180.         rval = NULL_TREE;
  1181.         }
  1182.     }
  1183.  
  1184.       if (protect && rval)
  1185.     {
  1186.       if (TREE_PRIVATE (rval) | TREE_PROTECTED (rval))
  1187.         this_v = compute_access (basetype_path, rval);
  1188.       if (TREE_CODE (rval) == CONST_DECL)
  1189.         {
  1190.           if (this_v == access_private)
  1191.         errstr = "enum `%D' is a private value of class `%T'";
  1192.           else if (this_v == access_protected)
  1193.         errstr = "enum `%D' is a protected value of class `%T'";
  1194.         }
  1195.       else
  1196.         {
  1197.           if (this_v == access_private)
  1198.         errstr = "member `%D' is a private member of class `%T'";
  1199.           else if (this_v == access_protected)
  1200.         errstr = "member `%D' is a protected member of class `%T'";
  1201.         }
  1202.     }
  1203.  
  1204.       if (entry)
  1205.     {
  1206.       if (errstr)
  1207.         {
  1208.           /* This depends on behavior of lookup_field_1!  */
  1209.           tree error_string = my_build_string (errstr);
  1210.           TREE_TYPE (entry) = error_string;
  1211.         }
  1212.       else
  1213.         {
  1214.           /* Let entry know there is no problem with this access.  */
  1215.           TREE_TYPE (entry) = NULL_TREE;
  1216.         }
  1217.       TREE_VALUE (entry) = rval;
  1218.     }
  1219.  
  1220.       if (errstr && protect)
  1221.     {
  1222.       cp_error (errstr, name, type);
  1223.       return error_mark_node;
  1224.     }
  1225.       return rval;
  1226.     }
  1227.  
  1228.   basetype_chain = build_tree_list (NULL_TREE, basetype_path);
  1229.   TREE_VIA_PUBLIC (basetype_chain) = TREE_VIA_PUBLIC (basetype_path);
  1230.   TREE_VIA_PROTECTED (basetype_chain) = TREE_VIA_PROTECTED (basetype_path);
  1231.   TREE_VIA_VIRTUAL (basetype_chain) = TREE_VIA_VIRTUAL (basetype_path);
  1232.  
  1233.   /* The ambiguity check relies upon breadth first searching. */
  1234.  
  1235.   search_stack = push_search_level (search_stack, &search_obstack);
  1236.   binfo = basetype_path;
  1237.   binfo_h = binfo;
  1238.  
  1239.   while (1)
  1240.     {
  1241.       tree binfos = BINFO_BASETYPES (binfo);
  1242.       int i, n_baselinks = binfos ? TREE_VEC_LENGTH (binfos) : 0;
  1243.       tree nval;
  1244.  
  1245.       /* Process and/or queue base types.  */
  1246.       for (i = 0; i < n_baselinks; i++)
  1247.     {
  1248.       tree base_binfo = TREE_VEC_ELT (binfos, i);
  1249.       if (BINFO_FIELDS_MARKED (base_binfo) == 0)
  1250.         {
  1251.           tree btypes;
  1252.  
  1253.           SET_BINFO_FIELDS_MARKED (base_binfo);
  1254.           btypes = my_tree_cons (NULL_TREE, base_binfo, basetype_chain);
  1255.           TREE_VIA_PUBLIC (btypes) = TREE_VIA_PUBLIC (base_binfo);
  1256.           TREE_VIA_PROTECTED (btypes) = TREE_VIA_PROTECTED (base_binfo);
  1257.           TREE_VIA_VIRTUAL (btypes) = TREE_VIA_VIRTUAL (base_binfo);
  1258.           if (TREE_VIA_VIRTUAL (base_binfo))
  1259.         btypes = tree_cons (NULL_TREE,
  1260.                     TYPE_BINFO (BINFO_TYPE (TREE_VEC_ELT (BINFO_BASETYPES (binfo_h), i))),
  1261.                     btypes);
  1262.           else
  1263.         btypes = tree_cons (NULL_TREE,
  1264.                     TREE_VEC_ELT (BINFO_BASETYPES (binfo_h), i),
  1265.                     btypes);
  1266.           obstack_ptr_grow (&search_obstack, btypes);
  1267.           tail += 1;
  1268.           if (tail >= search_stack->limit)
  1269.         my_friendly_abort (98);
  1270.         }
  1271.     }
  1272.  
  1273.       /* Process head of queue, if one exists.  */
  1274.       if (head >= tail)
  1275.     break;
  1276.  
  1277.       basetype_chain = search_stack->first[head++];
  1278.       binfo_h = TREE_VALUE (basetype_chain);
  1279.       basetype_chain = TREE_CHAIN (basetype_chain);
  1280.       basetype_path = TREE_VALUE (basetype_chain);
  1281.       if (TREE_CHAIN (basetype_chain))
  1282.     BINFO_INHERITANCE_CHAIN (basetype_path) = TREE_VALUE (TREE_CHAIN (basetype_chain));
  1283.       else
  1284.     BINFO_INHERITANCE_CHAIN (basetype_path) = NULL_TREE;
  1285.  
  1286.       binfo = basetype_path;
  1287.       type = BINFO_TYPE (binfo);
  1288.  
  1289.       /* See if we can find NAME in TYPE.  If RVAL is nonzero,
  1290.      and we do find NAME in TYPE, verify that such a second
  1291.      sighting is in fact valid.  */
  1292.  
  1293.       nval = lookup_field_1 (type, name);
  1294.  
  1295.       if (nval || lookup_fnfields_here (type, name)>=0)
  1296.     {
  1297.       if (nval && nval == rval && SHARED_MEMBER_P (nval))
  1298.         {
  1299.           /* This is ok, the member found is the same [class.ambig] */
  1300.         }
  1301.       else if (rval_binfo && hides (rval_binfo_h, binfo_h))
  1302.         {
  1303.           /* This is ok, the member found is in rval_binfo, not
  1304.          here (binfo). */
  1305.         }
  1306.       else if (rval_binfo==NULL_TREE || hides (binfo_h, rval_binfo_h))
  1307.         {
  1308.           /* This is ok, the member found is here (binfo), not in
  1309.          rval_binfo. */
  1310.           if (nval)
  1311.         {
  1312.           rval = nval;
  1313.           if (entry || protect)
  1314.             this_v = compute_access (basetype_path, rval);
  1315.           /* These may look ambiguous, but they really are not.  */
  1316.           if (vbase_name_p)
  1317.             break;
  1318.         }
  1319.           else
  1320.         {
  1321.           /* Undo finding it before, as something else hides it. */
  1322.           rval = NULL_TREE;
  1323.         }
  1324.           rval_binfo = binfo;
  1325.           rval_binfo_h = binfo_h;
  1326.         }
  1327.       else
  1328.         {
  1329.           /* This is ambiguous. */
  1330.           errstr = "request for member `%D' is ambiguous";
  1331.           protect = 2;
  1332.           break;
  1333.         }
  1334.     }
  1335.     }
  1336.   {
  1337.     tree *tp = search_stack->first;
  1338.     tree *search_tail = tp + tail;
  1339.  
  1340.     if (entry)
  1341.       TREE_VALUE (entry) = rval;
  1342.  
  1343.     if (rval_binfo)
  1344.       {
  1345.     type = BINFO_TYPE (rval_binfo);
  1346.  
  1347.     if (rval)
  1348.       {
  1349.         if (want_type)
  1350.           {
  1351.         if (TREE_CODE (rval) != TYPE_DECL)
  1352.           {
  1353.             rval = purpose_member (name, CLASSTYPE_TAGS (type));
  1354.             if (rval)
  1355.               rval = TYPE_MAIN_DECL (TREE_VALUE (rval));
  1356.           }
  1357.           }
  1358.         else
  1359.           {
  1360.         if (TREE_CODE (rval) == TYPE_DECL
  1361.             && lookup_fnfields_here (type, name) >= 0)
  1362.           rval = NULL_TREE;
  1363.           }
  1364.       }
  1365.       }
  1366.  
  1367.     if (rval == NULL_TREE)
  1368.       errstr = 0;
  1369.  
  1370.     /* If this FIELD_DECL defines its own access level, deal with that.  */
  1371.     if (rval && errstr == 0
  1372.     && ((protect&1) || entry)
  1373.     && DECL_LANG_SPECIFIC (rval)
  1374.     && DECL_ACCESS (rval))
  1375.       {
  1376.     while (tp < search_tail)
  1377.       {
  1378.         /* If is possible for one of the derived types on the path to
  1379.            have defined special access for this field.  Look for such
  1380.            declarations and report an error if a conflict is found.  */
  1381.         enum access_type new_v;
  1382.  
  1383.         if (this_v != access_default)
  1384.           new_v = compute_access (TREE_VALUE (TREE_CHAIN (*tp)), rval);
  1385.         if (this_v != access_default && new_v != this_v)
  1386.           {
  1387.         errstr = "conflicting access to member `%D'";
  1388.         this_v = access_default;
  1389.           }
  1390.         own_access = new_v;
  1391.         CLEAR_BINFO_FIELDS_MARKED (TREE_VALUE (TREE_CHAIN (*tp)));
  1392.         tp += 1;
  1393.       }
  1394.       }
  1395.     else
  1396.       {
  1397.     while (tp < search_tail)
  1398.       {
  1399.         CLEAR_BINFO_FIELDS_MARKED (TREE_VALUE (TREE_CHAIN (*tp)));
  1400.         tp += 1;
  1401.       }
  1402.       }
  1403.   }
  1404.   search_stack = pop_search_level (search_stack);
  1405.  
  1406.   if (errstr == 0)
  1407.     {
  1408.       if (own_access == access_private)
  1409.     errstr = "member `%D' declared private";
  1410.       else if (own_access == access_protected)
  1411.     errstr = "member `%D' declared protected";
  1412.       else if (this_v == access_private)
  1413.     errstr = TREE_PRIVATE (rval)
  1414.       ? "member `%D' is private"
  1415.         : "member `%D' is from private base class";
  1416.       else if (this_v == access_protected)
  1417.     errstr = TREE_PROTECTED (rval)
  1418.       ? "member `%D' is protected"
  1419.         : "member `%D' is from protected base class";
  1420.     }
  1421.  
  1422.   if (entry)
  1423.     {
  1424.       if (errstr)
  1425.     {
  1426.       tree error_string = my_build_string (errstr);
  1427.       /* Save error message with entry.  */
  1428.       TREE_TYPE (entry) = error_string;
  1429.     }
  1430.       else
  1431.     {
  1432.       /* Mark entry as having no error string.  */
  1433.       TREE_TYPE (entry) = NULL_TREE;
  1434.     }
  1435.     }
  1436.  
  1437.   if (errstr && protect)
  1438.     {
  1439.       cp_error (errstr, name, type);
  1440.       rval = error_mark_node;
  1441.     }
  1442.   return rval;
  1443. }
  1444.  
  1445. /* Try to find NAME inside a nested class.  */
  1446. tree
  1447. lookup_nested_field (name, complain)
  1448.      tree name;
  1449.      int complain;
  1450. {
  1451.   register tree t;
  1452.  
  1453.   tree id = NULL_TREE;
  1454.   if (TREE_CHAIN (current_class_type))
  1455.     {
  1456.       /* Climb our way up the nested ladder, seeing if we're trying to
  1457.      modify a field in an enclosing class.  If so, we should only
  1458.      be able to modify if it's static.  */
  1459.       for (t = TREE_CHAIN (current_class_type);
  1460.        t && DECL_CONTEXT (t);
  1461.        t = TREE_CHAIN (DECL_CONTEXT (t)))
  1462.     {
  1463.       if (TREE_CODE (DECL_CONTEXT (t)) != RECORD_TYPE)
  1464.         break;
  1465.  
  1466.       /* N.B.: lookup_field will do the access checking for us */
  1467.       id = lookup_field (DECL_CONTEXT (t), name, complain, 0);
  1468.       if (id == error_mark_node)
  1469.         {
  1470.           id = NULL_TREE;
  1471.           continue;
  1472.         }
  1473.  
  1474.       if (id != NULL_TREE)
  1475.         {
  1476.           if (TREE_CODE (id) == FIELD_DECL
  1477.           && ! TREE_STATIC (id)
  1478.           && TREE_TYPE (id) != error_mark_node)
  1479.         {
  1480.           if (complain)
  1481.             {
  1482.               /* At parse time, we don't want to give this error, since
  1483.              we won't have enough state to make this kind of
  1484.              decision properly.  But there are times (e.g., with
  1485.              enums in nested classes) when we do need to call
  1486.              this fn at parse time.  So, in those cases, we pass
  1487.              complain as a 0 and just return a NULL_TREE.  */
  1488.               error ("assignment to non-static member `%s' of enclosing class `%s'",
  1489.                  lang_printable_name (id),
  1490.                  IDENTIFIER_POINTER (TYPE_IDENTIFIER
  1491.                          (DECL_CONTEXT (t))));
  1492.               /* Mark this for do_identifier().  It would otherwise
  1493.              claim that the variable was undeclared.  */
  1494.               TREE_TYPE (id) = error_mark_node;
  1495.             }
  1496.           else
  1497.             {
  1498.               id = NULL_TREE;
  1499.               continue;
  1500.             }
  1501.         }
  1502.           break;
  1503.         }
  1504.     }
  1505.     }
  1506.  
  1507.   return id;
  1508. }
  1509.  
  1510. /* TYPE is a class type. Return the index of the fields within
  1511.    the method vector with name NAME, or -1 is no such field exists.  */
  1512. static int
  1513. lookup_fnfields_1 (type, name)
  1514.      tree type, name;
  1515. {
  1516.   register tree method_vec = CLASSTYPE_METHOD_VEC (type);
  1517.  
  1518.   if (method_vec != 0)
  1519.     {
  1520.       register tree *methods = &TREE_VEC_ELT (method_vec, 0);
  1521.       register tree *end = TREE_VEC_END (method_vec);
  1522.  
  1523. #ifdef GATHER_STATISTICS
  1524.       n_calls_lookup_fnfields_1++;
  1525. #endif
  1526.       if (*methods && name == constructor_name (type))
  1527.     return 0;
  1528.  
  1529.       while (++methods != end)
  1530.     {
  1531. #ifdef GATHER_STATISTICS
  1532.       n_outer_fields_searched++;
  1533. #endif
  1534.       if (DECL_NAME (*methods) == name)
  1535.         break;
  1536.     }
  1537.       if (methods != end)
  1538.     return methods - &TREE_VEC_ELT (method_vec, 0);
  1539.     }
  1540.  
  1541.   return -1;
  1542. }
  1543.  
  1544. /* Starting from BASETYPE, return a TREE_BASELINK-like object
  1545.    which gives the following information (in a list):
  1546.  
  1547.    TREE_TYPE: list of basetypes needed to get to...
  1548.    TREE_VALUE: list of all functions in of given type
  1549.    which have name NAME.
  1550.  
  1551.    No access information is computed by this function,
  1552.    other then to adorn the list of basetypes with
  1553.    TREE_VIA_PUBLIC.
  1554.  
  1555.    If there are two ways to find a name (two members), if COMPLAIN is
  1556.    non-zero, then error_mark_node is returned, and an error message is
  1557.    printed, otherwise, just an error_mark_node is returned.
  1558.  
  1559.    As a special case, is COMPLAIN is -1, we don't complain, and we
  1560.    don't return error_mark_node, but rather the complete list of
  1561.    virtuals.  This is used by get_virtuals_named_this.  */
  1562. tree
  1563. lookup_fnfields (basetype_path, name, complain)
  1564.      tree basetype_path, name;
  1565.      int complain;
  1566. {
  1567.   int head = 0, tail = 0;
  1568.   tree type, rval, rval_binfo = NULL_TREE, rvals = NULL_TREE, rval_binfo_h;
  1569.   tree entry, binfo, basetype_chain, binfo_h;
  1570.   int find_all = 0;
  1571.  
  1572.   /* rval_binfo is the binfo associated with the found member, note,
  1573.      this can be set with useful information, even when rval is not
  1574.      set, because it must deal with ALL members, not just function
  1575.      members.  It is used for ambiguity checking and the hidden
  1576.      checks.  Whereas rval is only set if a proper (not hidden)
  1577.      function member is found.  */
  1578.  
  1579.   /* rval_binfo_h and binfo_h are binfo values used when we perform the
  1580.      hiding checks, as virtual base classes may not be shared.  The strategy
  1581.      is we always go into the the binfo hierarchy owned by TYPE_BINFO of
  1582.      virtual base classes, as we cross virtual base class lines.  This way
  1583.      we know that binfo of a virtual base class will always == itself when
  1584.      found along any line.  (mrs)  */
  1585.  
  1586.   /* For now, don't try this.  */
  1587.   int protect = complain;
  1588.  
  1589.   char *errstr = 0;
  1590.  
  1591.   /* Set this to nonzero if we don't know how to compute
  1592.      accurate error messages for access control.  */
  1593.   int index = MEMOIZED_HASH_FN (name);
  1594.  
  1595.   if (complain == -1)
  1596.     {
  1597.       find_all = 1;
  1598.       protect = complain = 0;
  1599.     }
  1600.  
  1601.   /* If we are looking for a constructor in a templated type, use the
  1602.      unspecialized name, as that is how we store it.  */
  1603.   if (IDENTIFIER_TEMPLATE (name))
  1604.     name = constructor_name (name);
  1605.  
  1606.   binfo = basetype_path;
  1607.   binfo_h = binfo;
  1608.   type = BINFO_TYPE (basetype_path);
  1609.  
  1610.   /* The memoization code is in need of maintenance. */
  1611.   if (!find_all && CLASSTYPE_MTABLE_ENTRY (type))
  1612.     {
  1613.       tree tem = MEMOIZED_FNFIELDS (CLASSTYPE_MTABLE_ENTRY (type), index);
  1614.  
  1615.       while (tem && TREE_PURPOSE (tem) != name)
  1616.     {
  1617.       memoized_fields_searched[1]++;
  1618.       tem = TREE_CHAIN (tem);
  1619.     }
  1620.       if (tem)
  1621.     {
  1622.       if (protect && TREE_TYPE (tem))
  1623.         {
  1624.           error (TREE_STRING_POINTER (TREE_TYPE (tem)),
  1625.              IDENTIFIER_POINTER (name),
  1626.              TYPE_NAME_STRING (DECL_CLASS_CONTEXT (TREE_VALUE (TREE_VALUE (tem)))));
  1627.           return error_mark_node;
  1628.         }
  1629.       if (TREE_VALUE (tem) == NULL_TREE)
  1630.         {
  1631.           memoized_fast_rejects[1] += 1;
  1632.           return NULL_TREE;
  1633.         }
  1634.       else
  1635.         {
  1636.           /* Want to return this, but we must make sure
  1637.          that access information is consistent.  */
  1638.           tree baselink = TREE_VALUE (tem);
  1639.           tree memoized_basetypes = TREE_PURPOSE (baselink);
  1640.           tree these_basetypes = basetype_path;
  1641.           while (memoized_basetypes && these_basetypes)
  1642.         {
  1643.           memoized_fields_searched[1]++;
  1644.           if (TREE_VALUE (memoized_basetypes) != these_basetypes)
  1645.             break;
  1646.           memoized_basetypes = TREE_CHAIN (memoized_basetypes);
  1647.           these_basetypes = BINFO_INHERITANCE_CHAIN (these_basetypes);
  1648.         }
  1649.           /* The following statement is true only when both are NULL.  */
  1650.           if (memoized_basetypes == these_basetypes)
  1651.         {
  1652.           memoized_fast_finds[1] += 1;
  1653.           return TREE_VALUE (tem);
  1654.         }
  1655.           /* else, we must re-find this field by hand.  */
  1656.           baselink = tree_cons (basetype_path, TREE_VALUE (baselink), TREE_CHAIN (baselink));
  1657.           return baselink;
  1658.         }
  1659.     }
  1660.     }
  1661.  
  1662. #ifdef GATHER_STATISTICS
  1663.   n_calls_lookup_fnfields++;
  1664. #endif
  1665.   if (protect && flag_memoize_lookups && ! global_bindings_p ())
  1666.     entry = make_memoized_table_entry (type, name, 1);
  1667.   else
  1668.     entry = 0;
  1669.  
  1670.   index = lookup_fnfields_here (type, name);
  1671.   if (index >= 0 || lookup_field_1 (type, name))
  1672.     {
  1673.       rval_binfo = basetype_path;
  1674.       rval_binfo_h = rval_binfo;
  1675.     }
  1676.  
  1677.   if (index >= 0)
  1678.     {
  1679.       rval = TREE_VEC_ELT (CLASSTYPE_METHOD_VEC (type), index);
  1680.       rvals = my_tree_cons (basetype_path, rval, rvals);
  1681.       if (BINFO_BASETYPES (binfo) && CLASSTYPE_BASELINK_VEC (type))
  1682.     TREE_TYPE (rvals) = TREE_VEC_ELT (CLASSTYPE_BASELINK_VEC (type), index);
  1683.  
  1684.       if (entry)
  1685.     {
  1686.       TREE_VALUE (entry) = rvals;
  1687.       TREE_TYPE (entry) = NULL_TREE;
  1688.     }
  1689.  
  1690.       return rvals;
  1691.     }
  1692.   rval = NULL_TREE;
  1693.  
  1694.   if (basetype_path == TYPE_BINFO (type))
  1695.     {
  1696.       basetype_chain = CLASSTYPE_BINFO_AS_LIST (type);
  1697.       TREE_VIA_PUBLIC (basetype_chain) = 1;
  1698.       BINFO_VIA_PUBLIC (basetype_path) = 1;
  1699.       BINFO_INHERITANCE_CHAIN (basetype_path) = NULL_TREE;
  1700.     }
  1701.   else
  1702.     {
  1703.       basetype_chain = build_tree_list (NULL_TREE, basetype_path);
  1704.       TREE_VIA_PUBLIC (basetype_chain) = TREE_VIA_PUBLIC (basetype_path);
  1705.       TREE_VIA_PROTECTED (basetype_chain) = TREE_VIA_PROTECTED (basetype_path);
  1706.       TREE_VIA_VIRTUAL (basetype_chain) = TREE_VIA_VIRTUAL (basetype_path);
  1707.     }
  1708.  
  1709.   /* The ambiguity check relies upon breadth first searching. */
  1710.  
  1711.   search_stack = push_search_level (search_stack, &search_obstack);
  1712.   binfo = basetype_path;
  1713.   binfo_h = binfo;
  1714.  
  1715.   while (1)
  1716.     {
  1717.       tree binfos = BINFO_BASETYPES (binfo);
  1718.       int i, n_baselinks = binfos ? TREE_VEC_LENGTH (binfos) : 0;
  1719.       int index;
  1720.  
  1721.       /* Process and/or queue base types.  */
  1722.       for (i = 0; i < n_baselinks; i++)
  1723.     {
  1724.       tree base_binfo = TREE_VEC_ELT (binfos, i);
  1725.       if (BINFO_FIELDS_MARKED (base_binfo) == 0)
  1726.         {
  1727.           tree btypes;
  1728.  
  1729.           SET_BINFO_FIELDS_MARKED (base_binfo);
  1730.           btypes = my_tree_cons (NULL_TREE, base_binfo, basetype_chain);
  1731.           TREE_VIA_PUBLIC (btypes) = TREE_VIA_PUBLIC (base_binfo);
  1732.           TREE_VIA_PROTECTED (btypes) = TREE_VIA_PROTECTED (base_binfo);
  1733.           TREE_VIA_VIRTUAL (btypes) = TREE_VIA_VIRTUAL (base_binfo);
  1734.           if (TREE_VIA_VIRTUAL (base_binfo))
  1735.         btypes = tree_cons (NULL_TREE,
  1736.                     TYPE_BINFO (BINFO_TYPE (TREE_VEC_ELT (BINFO_BASETYPES (binfo_h), i))),
  1737.                     btypes);
  1738.           else
  1739.         btypes = tree_cons (NULL_TREE,
  1740.                     TREE_VEC_ELT (BINFO_BASETYPES (binfo_h), i),
  1741.                     btypes);
  1742.           obstack_ptr_grow (&search_obstack, btypes);
  1743.           tail += 1;
  1744.           if (tail >= search_stack->limit)
  1745.         my_friendly_abort (99);
  1746.         }
  1747.     }
  1748.  
  1749.       /* Process head of queue, if one exists.  */
  1750.       if (head >= tail)
  1751.     break;
  1752.  
  1753.       basetype_chain = search_stack->first[head++];
  1754.       binfo_h = TREE_VALUE (basetype_chain);
  1755.       basetype_chain = TREE_CHAIN (basetype_chain);
  1756.       basetype_path = TREE_VALUE (basetype_chain);
  1757.       if (TREE_CHAIN (basetype_chain))
  1758.     BINFO_INHERITANCE_CHAIN (basetype_path) = TREE_VALUE (TREE_CHAIN (basetype_chain));
  1759.       else
  1760.     BINFO_INHERITANCE_CHAIN (basetype_path) = NULL_TREE;
  1761.  
  1762.       binfo = basetype_path;
  1763.       type = BINFO_TYPE (binfo);
  1764.  
  1765.       /* See if we can find NAME in TYPE.  If RVAL is nonzero,
  1766.      and we do find NAME in TYPE, verify that such a second
  1767.      sighting is in fact valid.  */
  1768.  
  1769.       index = lookup_fnfields_here (type, name);
  1770.  
  1771.       if (index >= 0 || (lookup_field_1 (type, name)!=NULL_TREE && !find_all))
  1772.     {
  1773.       if (rval_binfo && !find_all && hides (rval_binfo_h, binfo_h))
  1774.         {
  1775.           /* This is ok, the member found is in rval_binfo, not
  1776.          here (binfo). */
  1777.         }
  1778.       else if (rval_binfo==NULL_TREE || find_all || hides (binfo_h, rval_binfo_h))
  1779.         {
  1780.           /* This is ok, the member found is here (binfo), not in
  1781.          rval_binfo. */
  1782.           if (index >= 0)
  1783.         {
  1784.           rval = TREE_VEC_ELT (CLASSTYPE_METHOD_VEC (type), index);
  1785.           /* Note, rvals can only be previously set if find_all is
  1786.              true.  */
  1787.           rvals = my_tree_cons (basetype_path, rval, rvals);
  1788.           if (TYPE_BINFO_BASETYPES (type)
  1789.               && CLASSTYPE_BASELINK_VEC (type))
  1790.             TREE_TYPE (rvals) = TREE_VEC_ELT (CLASSTYPE_BASELINK_VEC (type), index);
  1791.         }
  1792.           else
  1793.         {
  1794.           /* Undo finding it before, as something else hides it. */
  1795.           rval = NULL_TREE;
  1796.           rvals = NULL_TREE;
  1797.         }
  1798.           rval_binfo = binfo;
  1799.           rval_binfo_h = binfo_h;
  1800.         }
  1801.       else
  1802.         {
  1803.           /* This is ambiguous. */
  1804.           errstr = "request for method `%D' is ambiguous";
  1805.           rvals = error_mark_node;
  1806.           break;
  1807.         }
  1808.     }
  1809.     }
  1810.   {
  1811.     tree *tp = search_stack->first;
  1812.     tree *search_tail = tp + tail;
  1813.  
  1814.     while (tp < search_tail)
  1815.       {
  1816.     CLEAR_BINFO_FIELDS_MARKED (TREE_VALUE (TREE_CHAIN (*tp)));
  1817.     tp += 1;
  1818.       }
  1819.   }
  1820.   search_stack = pop_search_level (search_stack);
  1821.  
  1822.   if (entry)
  1823.     {
  1824.       if (errstr)
  1825.     {
  1826.       tree error_string = my_build_string (errstr);
  1827.       /* Save error message with entry.  */
  1828.       TREE_TYPE (entry) = error_string;
  1829.     }
  1830.       else
  1831.     {
  1832.       /* Mark entry as having no error string.  */
  1833.       TREE_TYPE (entry) = NULL_TREE;
  1834.       TREE_VALUE (entry) = rvals;
  1835.     }
  1836.     }
  1837.  
  1838.   if (errstr && protect)
  1839.     {
  1840.       cp_error (errstr, name);
  1841.       rvals = error_mark_node;
  1842.     }
  1843.  
  1844.   return rvals;
  1845. }
  1846.  
  1847. /* BREADTH-FIRST SEARCH ROUTINES.  */
  1848.  
  1849. /* Search a multiple inheritance hierarchy by breadth-first search.
  1850.  
  1851.    TYPE is an aggregate type, possibly in a multiple-inheritance hierarchy.
  1852.    TESTFN is a function, which, if true, means that our condition has been met,
  1853.    and its return value should be returned.
  1854.    QFN, if non-NULL, is a predicate dictating whether the type should
  1855.    even be queued.  */
  1856.  
  1857. HOST_WIDE_INT
  1858. breadth_first_search (binfo, testfn, qfn)
  1859.      tree binfo;
  1860.      int (*testfn)();
  1861.      int (*qfn)();
  1862. {
  1863.   int head = 0, tail = 0;
  1864.   int rval = 0;
  1865.  
  1866.   search_stack = push_search_level (search_stack, &search_obstack);
  1867.  
  1868.   while (1)
  1869.     {
  1870.       tree binfos = BINFO_BASETYPES (binfo);
  1871.       int n_baselinks = binfos ? TREE_VEC_LENGTH (binfos) : 0;
  1872.       int i;
  1873.  
  1874.       /* Process and/or queue base types.  */
  1875.       for (i = 0; i < n_baselinks; i++)
  1876.     {
  1877.       tree base_binfo = TREE_VEC_ELT (binfos, i);
  1878.  
  1879.       if (BINFO_MARKED (base_binfo) == 0
  1880.           && (qfn == 0 || (*qfn) (binfo, i)))
  1881.         {
  1882.           SET_BINFO_MARKED (base_binfo);
  1883.           obstack_ptr_grow (&search_obstack, binfo);
  1884.           obstack_ptr_grow (&search_obstack, (HOST_WIDE_INT) i);
  1885.           tail += 2;
  1886.           if (tail >= search_stack->limit)
  1887.         my_friendly_abort (100);
  1888.         }
  1889.     }
  1890.       /* Process head of queue, if one exists.  */
  1891.       if (head >= tail)
  1892.     {
  1893.       rval = 0;
  1894.       break;
  1895.     }
  1896.  
  1897.       binfo = search_stack->first[head++];
  1898.       i = (HOST_WIDE_INT) search_stack->first[head++];
  1899.       if (rval = (*testfn) (binfo, i))
  1900.     break;
  1901.       binfo = BINFO_BASETYPE (binfo, i);
  1902.     }
  1903.   {
  1904.     tree *tp = search_stack->first;
  1905.     tree *search_tail = tp + tail;
  1906.     while (tp < search_tail)
  1907.       {
  1908.     tree binfo = *tp++;
  1909.     int i = (HOST_WIDE_INT)(*tp++);
  1910.     CLEAR_BINFO_MARKED (BINFO_BASETYPE (binfo, i));
  1911.       }
  1912.   }
  1913.  
  1914.   search_stack = pop_search_level (search_stack);
  1915.   return rval;
  1916. }
  1917.  
  1918. /* Functions to use in breadth first searches.  */
  1919. typedef tree (*pft)();
  1920. typedef int (*pfi)();
  1921.  
  1922. int tree_needs_constructor_p (binfo, i)
  1923.      tree binfo;
  1924.      int i;
  1925. {
  1926.   tree basetype;
  1927.   my_friendly_assert (i != 0, 296);
  1928.   basetype = BINFO_TYPE (BINFO_BASETYPE (binfo, i));
  1929.   return TYPE_NEEDS_CONSTRUCTING (basetype);
  1930. }
  1931.  
  1932. static tree declarator;
  1933.  
  1934. static tree
  1935. get_virtuals_named_this (binfo)
  1936.      tree binfo;
  1937. {
  1938.   tree fields;
  1939.  
  1940.   fields = lookup_fnfields (binfo, declarator, -1);
  1941.   /* fields cannot be error_mark_node */
  1942.  
  1943.   if (fields == 0)
  1944.     return 0;
  1945.  
  1946.   /* Get to the function decls, and return the first virtual function
  1947.      with this name, if there is one.  */
  1948.   while (fields)
  1949.     {
  1950.       tree fndecl;
  1951.  
  1952.       for (fndecl = TREE_VALUE (fields); fndecl; fndecl = DECL_CHAIN (fndecl))
  1953.     if (DECL_VINDEX (fndecl))
  1954.       return fields;
  1955.       fields = next_baselink (fields);
  1956.     }
  1957.   return NULL_TREE;
  1958. }
  1959.  
  1960. static tree get_virtual_destructor (binfo, i)
  1961.      tree binfo;
  1962.      int i;
  1963. {
  1964.   tree type = BINFO_TYPE (binfo);
  1965.   if (i >= 0)
  1966.     type = BINFO_TYPE (TREE_VEC_ELT (BINFO_BASETYPES (binfo), i));
  1967.   if (TYPE_HAS_DESTRUCTOR (type)
  1968.       && DECL_VINDEX (TREE_VEC_ELT (CLASSTYPE_METHOD_VEC (type), 0)))
  1969.     return TREE_VEC_ELT (CLASSTYPE_METHOD_VEC (type), 0);
  1970.   return 0;
  1971. }
  1972.  
  1973. int tree_has_any_destructor_p (binfo, i)
  1974.      tree binfo;
  1975.      int i;
  1976. {
  1977.   tree type = BINFO_TYPE (binfo);
  1978.   if (i >= 0)
  1979.     type = BINFO_TYPE (TREE_VEC_ELT (BINFO_BASETYPES (binfo), i));
  1980.   return TYPE_NEEDS_DESTRUCTOR (type);
  1981. }
  1982.  
  1983. /* Given a class type TYPE, and a function decl FNDECL, look for a
  1984.    virtual function in TYPE's hierarchy which FNDECL could match as a
  1985.    virtual function.  It doesn't matter which one we find.
  1986.  
  1987.    DTORP is nonzero if we are looking for a destructor.  Destructors
  1988.    need special treatment because they do not match by name.  */
  1989. tree
  1990. get_matching_virtual (binfo, fndecl, dtorp)
  1991.      tree binfo, fndecl;
  1992.      int dtorp;
  1993. {
  1994.   tree tmp = NULL_TREE;
  1995.  
  1996.   /* Breadth first search routines start searching basetypes
  1997.      of TYPE, so we must perform first ply of search here.  */
  1998.   if (dtorp)
  1999.     {
  2000.       if (tree_has_any_destructor_p (binfo, -1))
  2001.     tmp = get_virtual_destructor (binfo, -1);
  2002.  
  2003.       if (tmp)
  2004.     return tmp;
  2005.  
  2006.       tmp = (tree) breadth_first_search (binfo,
  2007.                      (pfi) get_virtual_destructor,
  2008.                      tree_has_any_destructor_p);
  2009.       return tmp;
  2010.     }
  2011.   else
  2012.     {
  2013.       tree drettype, dtypes, btypes, instptr_type;
  2014.       tree basetype = DECL_CLASS_CONTEXT (fndecl);
  2015.       tree baselink, best = NULL_TREE;
  2016.       tree name = DECL_ASSEMBLER_NAME (fndecl);
  2017.  
  2018.       declarator = DECL_NAME (fndecl);
  2019.       if (IDENTIFIER_VIRTUAL_P (declarator) == 0)
  2020.     return NULL_TREE;
  2021.  
  2022.       baselink = get_virtuals_named_this (binfo);
  2023.       if (baselink == NULL_TREE)
  2024.     return NULL_TREE;
  2025.  
  2026.       drettype = TREE_TYPE (TREE_TYPE (fndecl));
  2027.       dtypes = TYPE_ARG_TYPES (TREE_TYPE (fndecl));
  2028.       if (DECL_STATIC_FUNCTION_P (fndecl))
  2029.     instptr_type = NULL_TREE;
  2030.       else
  2031.     instptr_type = TREE_TYPE (TREE_VALUE (dtypes));
  2032.  
  2033.       for (; baselink; baselink = next_baselink (baselink))
  2034.     {
  2035.       for (tmp = TREE_VALUE (baselink); tmp; tmp = DECL_CHAIN (tmp))
  2036.         {
  2037.           if (! DECL_VINDEX (tmp))
  2038.         continue;
  2039.  
  2040.           btypes = TYPE_ARG_TYPES (TREE_TYPE (tmp));
  2041.           if (instptr_type == NULL_TREE)
  2042.         {
  2043.           if (compparms (TREE_CHAIN (btypes), dtypes, 3))
  2044.             /* Caller knows to give error in this case.  */
  2045.             return tmp;
  2046.           return NULL_TREE;
  2047.         }
  2048.  
  2049.           if ((TYPE_READONLY (TREE_TYPE (TREE_VALUE (btypes)))
  2050.            == TYPE_READONLY (instptr_type))
  2051.           && compparms (TREE_CHAIN (btypes), TREE_CHAIN (dtypes), 3))
  2052.         {
  2053.           tree brettype = TREE_TYPE (TREE_TYPE (tmp));
  2054.           if (comptypes (brettype, drettype, 1))
  2055.             /* OK */;
  2056.           else if
  2057.             (TREE_CODE (brettype) == TREE_CODE (drettype)
  2058.              && (TREE_CODE (brettype) == POINTER_TYPE
  2059.              || TREE_CODE (brettype) == REFERENCE_TYPE)
  2060.              && comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (brettype)),
  2061.                    TYPE_MAIN_VARIANT (TREE_TYPE (drettype)),
  2062.                    0))
  2063.               /* covariant return type */
  2064.             {
  2065.               tree b = TREE_TYPE (brettype), d = TREE_TYPE (drettype);
  2066.               if (TYPE_MAIN_VARIANT (b) != TYPE_MAIN_VARIANT (d))
  2067.             {
  2068.               tree binfo = get_binfo (b, d, 1);
  2069.               if (binfo != error_mark_node
  2070.                   && ! BINFO_OFFSET_ZEROP (binfo))
  2071.                 sorry ("adjusting pointers for covariant returns");
  2072.             }
  2073.               if (TYPE_READONLY (d) > TYPE_READONLY (b))
  2074.             {
  2075.               cp_error ("return type of `%#D' adds const", fndecl);
  2076.               cp_error_at ("  overriding definition as `%#D'",
  2077.                        tmp);
  2078.             }
  2079.               else if (TYPE_VOLATILE (d) > TYPE_VOLATILE (b))
  2080.             {
  2081.               cp_error ("return type of `%#D' adds volatile",
  2082.                     fndecl);
  2083.               cp_error_at ("  overriding definition as `%#D'",
  2084.                        tmp);
  2085.             }
  2086.             }
  2087.           else if (IS_AGGR_TYPE_2 (brettype, drettype)
  2088.                && comptypes (brettype, drettype, 0))
  2089.             {
  2090.               error ("invalid covariant return type (must use pointer or reference)");
  2091.               cp_error_at ("  overriding `%#D'", tmp);
  2092.               cp_error ("  with `%#D'", fndecl);
  2093.             }
  2094.           else if (IDENTIFIER_ERROR_LOCUS (name) == NULL_TREE)
  2095.             {
  2096.               cp_error ("conflicting return type specified for virtual function `%#D'", fndecl);
  2097.               cp_error_at ("  overriding definition as `%#D'", tmp);
  2098.               SET_IDENTIFIER_ERROR_LOCUS (name, basetype);
  2099.             }
  2100.           break;
  2101.         }
  2102.         }
  2103.       if (tmp)
  2104.         {
  2105.           best = tmp;
  2106.           break;
  2107.         }
  2108.     }
  2109.       if (best == NULL_TREE && warn_overloaded_virtual)
  2110.     cp_warning_at ("conflicting specification deriving virtual function `%D'", fndecl);
  2111.  
  2112.       return best;
  2113.     }
  2114. }
  2115.  
  2116. /* Return the list of virtual functions which are abstract in type
  2117.    TYPE that come from non virtual base classes.  See
  2118.    expand_direct_vtbls_init for the style of search we do.  */
  2119. static tree
  2120. get_abstract_virtuals_1 (binfo, do_self, abstract_virtuals)
  2121.      tree binfo, abstract_virtuals;
  2122.      int do_self;
  2123. {
  2124.   tree binfos = BINFO_BASETYPES (binfo);
  2125.   int i, n_baselinks = binfos ? TREE_VEC_LENGTH (binfos) : 0;
  2126.  
  2127.   for (i = 0; i < n_baselinks; i++)
  2128.     {
  2129.       tree base_binfo = TREE_VEC_ELT (binfos, i);
  2130.       int is_not_base_vtable =
  2131.     i != CLASSTYPE_VFIELD_PARENT (BINFO_TYPE (binfo));
  2132.       if (! TREE_VIA_VIRTUAL (base_binfo))
  2133.     abstract_virtuals
  2134.       = get_abstract_virtuals_1 (base_binfo, is_not_base_vtable,
  2135.                      abstract_virtuals);
  2136.     }
  2137.   /* Should we use something besides CLASSTYPE_VFIELDS? */
  2138.   if (do_self && CLASSTYPE_VFIELDS (BINFO_TYPE (binfo)))
  2139.     {
  2140.       tree virtuals = BINFO_VIRTUALS (binfo);
  2141.  
  2142.       skip_rtti_stuff (&virtuals);
  2143.  
  2144.       while (virtuals)
  2145.     {
  2146.       tree base_pfn = FNADDR_FROM_VTABLE_ENTRY (TREE_VALUE (virtuals));
  2147.       tree base_fndecl = TREE_OPERAND (base_pfn, 0);
  2148.       if (DECL_ABSTRACT_VIRTUAL_P (base_fndecl))
  2149.         abstract_virtuals = tree_cons (NULL_TREE, base_fndecl, abstract_virtuals);
  2150.       virtuals = TREE_CHAIN (virtuals);
  2151.     }
  2152.     }
  2153.   return abstract_virtuals;
  2154. }
  2155.  
  2156. /* Return the list of virtual functions which are abstract in type TYPE.
  2157.    This information is cached, and so must be built on a
  2158.    non-temporary obstack.  */
  2159. tree
  2160. get_abstract_virtuals (type)
  2161.      tree type;
  2162. {
  2163.   tree vbases;
  2164.   tree abstract_virtuals = CLASSTYPE_ABSTRACT_VIRTUALS (type);
  2165.  
  2166.   /* First get all from non-virtual bases. */
  2167.   abstract_virtuals
  2168.     = get_abstract_virtuals_1 (TYPE_BINFO (type), 1, abstract_virtuals);
  2169.                            
  2170.   for (vbases = CLASSTYPE_VBASECLASSES (type); vbases; vbases = TREE_CHAIN (vbases))
  2171.     {
  2172.       tree virtuals = BINFO_VIRTUALS (vbases);
  2173.  
  2174.       skip_rtti_stuff (&virtuals);
  2175.  
  2176.       while (virtuals)
  2177.     {
  2178.       tree base_pfn = FNADDR_FROM_VTABLE_ENTRY (TREE_VALUE (virtuals));
  2179.       tree base_fndecl = TREE_OPERAND (base_pfn, 0);
  2180.       if (DECL_ABSTRACT_VIRTUAL_P (base_fndecl))
  2181.         abstract_virtuals = tree_cons (NULL_TREE, base_fndecl, abstract_virtuals);
  2182.       virtuals = TREE_CHAIN (virtuals);
  2183.     }
  2184.     }
  2185.   return nreverse (abstract_virtuals);
  2186. }
  2187.  
  2188. /* For the type TYPE, return a list of member functions available from
  2189.    base classes with name NAME.  The TREE_VALUE of the list is a chain of
  2190.    member functions with name NAME.  The TREE_PURPOSE of the list is a
  2191.    basetype, or a list of base types (in reverse order) which were
  2192.    traversed to reach the chain of member functions.  If we reach a base
  2193.    type which provides a member function of name NAME, and which has at
  2194.    most one base type itself, then we can terminate the search.  */
  2195.  
  2196. tree
  2197. get_baselinks (type_as_binfo_list, type, name)
  2198.      tree type_as_binfo_list;
  2199.      tree type, name;
  2200. {
  2201.   int head = 0, tail = 0, index;
  2202.   tree rval = 0, nval = 0;
  2203.   tree basetypes = type_as_binfo_list;
  2204.   tree binfo = TYPE_BINFO (type);
  2205.  
  2206.   search_stack = push_search_level (search_stack, &search_obstack);
  2207.  
  2208.   while (1)
  2209.     {
  2210.       tree binfos = BINFO_BASETYPES (binfo);
  2211.       int i, n_baselinks = binfos ? TREE_VEC_LENGTH (binfos) : 0;
  2212.  
  2213.       /* Process and/or queue base types.  */
  2214.       for (i = 0; i < n_baselinks; i++)
  2215.     {
  2216.       tree base_binfo = TREE_VEC_ELT (binfos, i);
  2217.       tree btypes;
  2218.  
  2219.       btypes = hash_tree_cons (TREE_VIA_PUBLIC (base_binfo),
  2220.                    TREE_VIA_VIRTUAL (base_binfo),
  2221.                    TREE_VIA_PROTECTED (base_binfo),
  2222.                    NULL_TREE, base_binfo,
  2223.                    basetypes);
  2224.       obstack_ptr_grow (&search_obstack, btypes);
  2225.       search_stack->first = (tree *)obstack_base (&search_obstack);
  2226.       tail += 1;
  2227.     }
  2228.  
  2229.     dont_queue:
  2230.       /* Process head of queue, if one exists.  */
  2231.       if (head >= tail)
  2232.     break;
  2233.  
  2234.       basetypes = search_stack->first[head++];
  2235.       binfo = TREE_VALUE (basetypes);
  2236.       type = BINFO_TYPE (binfo);
  2237.       index = lookup_fnfields_1 (type, name);
  2238.       if (index >= 0)
  2239.     {
  2240.       nval = TREE_VEC_ELT (CLASSTYPE_METHOD_VEC (type), index);
  2241.       rval = hash_tree_cons (0, 0, 0, basetypes, nval, rval);
  2242.       if (TYPE_BINFO_BASETYPES (type) == 0)
  2243.         goto dont_queue;
  2244.       else if (TREE_VEC_LENGTH (TYPE_BINFO_BASETYPES (type)) == 1)
  2245.         {
  2246.           if (CLASSTYPE_BASELINK_VEC (type))
  2247.         TREE_TYPE (rval) = TREE_VEC_ELT (CLASSTYPE_BASELINK_VEC (type), index);
  2248.           goto dont_queue;
  2249.         }
  2250.     }
  2251.       nval = NULL_TREE;
  2252.     }
  2253.  
  2254.   search_stack = pop_search_level (search_stack);
  2255.   return rval;
  2256. }
  2257.  
  2258. tree
  2259. next_baselink (baselink)
  2260.      tree baselink;
  2261. {
  2262.   tree tmp = TREE_TYPE (baselink);
  2263.   baselink = TREE_CHAIN (baselink);
  2264.   while (tmp)
  2265.     {
  2266.       /* @@ does not yet add previous base types.  */
  2267.       baselink = tree_cons (TREE_PURPOSE (tmp), TREE_VALUE (tmp),
  2268.                 baselink);
  2269.       TREE_TYPE (baselink) = TREE_TYPE (tmp);
  2270.       tmp = TREE_CHAIN (tmp);
  2271.     }
  2272.   return baselink;
  2273. }
  2274.  
  2275. /* DEPTH-FIRST SEARCH ROUTINES.  */
  2276.  
  2277. /* Assign unique numbers to _CLASSTYPE members of the lattice
  2278.    specified by TYPE.  The root nodes are marked first; the nodes
  2279.    are marked depth-fisrt, left-right.  */
  2280.  
  2281. static int cid;
  2282.  
  2283. /* Matrix implementing a relation from CLASSTYPE X CLASSTYPE => INT.
  2284.    Relation yields 1 if C1 <= C2, 0 otherwise.  */
  2285. typedef char mi_boolean;
  2286. static mi_boolean *mi_matrix;
  2287.  
  2288. /* Type for which this matrix is defined.  */
  2289. static tree mi_type;
  2290.  
  2291. /* Size of the matrix for indexing purposes.  */
  2292. static int mi_size;
  2293.  
  2294. /* Return nonzero if class C2 derives from class C1.  */
  2295. #define BINFO_DERIVES_FROM(C1, C2)    \
  2296.   ((mi_matrix+mi_size*(BINFO_CID (C1)-1))[BINFO_CID (C2)-1])
  2297. #define TYPE_DERIVES_FROM(C1, C2)    \
  2298.   ((mi_matrix+mi_size*(CLASSTYPE_CID (C1)-1))[CLASSTYPE_CID (C2)-1])
  2299. #define BINFO_DERIVES_FROM_STAR(C)    \
  2300.   (mi_matrix+(BINFO_CID (C)-1))
  2301.  
  2302. /* This routine converts a pointer to be a pointer of an immediate
  2303.    base class.  The normal convert_pointer_to routine would diagnose
  2304.    the conversion as ambiguous, under MI code that has the base class
  2305.    as an ambiguous base class. */
  2306. static tree
  2307. convert_pointer_to_single_level (to_type, expr)
  2308.      tree to_type, expr;
  2309. {
  2310.   tree binfo_of_derived;
  2311.   tree last;
  2312.  
  2313.   binfo_of_derived = TYPE_BINFO (TREE_TYPE (TREE_TYPE (expr)));
  2314.   last = get_binfo (to_type, TREE_TYPE (TREE_TYPE (expr)), 0);
  2315.   BINFO_INHERITANCE_CHAIN (last) = binfo_of_derived;
  2316.   BINFO_INHERITANCE_CHAIN (binfo_of_derived) = NULL_TREE;
  2317.   return build_vbase_path (PLUS_EXPR, build_pointer_type (to_type), expr, last, 1);
  2318. }
  2319.  
  2320. /* The main function which implements depth first search.
  2321.  
  2322.    This routine has to remember the path it walked up, when
  2323.    dfs_init_vbase_pointers is the work function, as otherwise there
  2324.    would be no record. */
  2325. static void
  2326. dfs_walk (binfo, fn, qfn)
  2327.      tree binfo;
  2328.      void (*fn)();
  2329.      int (*qfn)();
  2330. {
  2331.   tree binfos = BINFO_BASETYPES (binfo);
  2332.   int i, n_baselinks = binfos ? TREE_VEC_LENGTH (binfos) : 0;
  2333.  
  2334.   for (i = 0; i < n_baselinks; i++)
  2335.     {
  2336.       tree base_binfo = TREE_VEC_ELT (binfos, i);
  2337.  
  2338.       if (qfn == 0 || (*qfn)(base_binfo))
  2339.     {
  2340.       if (fn == dfs_init_vbase_pointers)
  2341.         {
  2342.           /* When traversing an arbitrary MI hierarchy, we need to keep
  2343.          a record of the path we took to get down to the final base
  2344.          type, as otherwise there would be no record of it, and just
  2345.          trying to blindly convert at the bottom would be ambiguous.
  2346.  
  2347.          The easiest way is to do the conversions one step at a time,
  2348.          as we know we want the immediate base class at each step.
  2349.  
  2350.          The only special trick to converting one step at a time,
  2351.          is that when we hit the last virtual base class, we must
  2352.          use the SLOT value for it, and not use the normal convert
  2353.          routine.  We use the last virtual base class, as in our
  2354.          implementation, we have pointers to all virtual base
  2355.          classes in the base object.  */
  2356.  
  2357.           tree saved_vbase_decl_ptr_intermediate
  2358.         = vbase_decl_ptr_intermediate;
  2359.  
  2360.           if (TREE_VIA_VIRTUAL (base_binfo))
  2361.         {
  2362.           /* No need for the conversion here, as we know it is the
  2363.              right type.  */
  2364.           vbase_decl_ptr_intermediate
  2365.             = (tree)CLASSTYPE_SEARCH_SLOT (BINFO_TYPE (base_binfo));
  2366.         }
  2367.           else
  2368.         {
  2369.           vbase_decl_ptr_intermediate
  2370.             = convert_pointer_to_single_level (BINFO_TYPE (base_binfo),
  2371.                                vbase_decl_ptr_intermediate);
  2372.         }
  2373.  
  2374.           dfs_walk (base_binfo, fn, qfn);
  2375.  
  2376.           vbase_decl_ptr_intermediate = saved_vbase_decl_ptr_intermediate;
  2377.         } else
  2378.           dfs_walk (base_binfo, fn, qfn);
  2379.     }
  2380.     }
  2381.  
  2382.   fn (binfo);
  2383. }
  2384.  
  2385. /* Predicate functions which serve for dfs_walk.  */
  2386. static int numberedp (binfo) tree binfo;
  2387. { return BINFO_CID (binfo); }
  2388. static int unnumberedp (binfo) tree binfo;
  2389. { return BINFO_CID (binfo) == 0; }
  2390.  
  2391. static int markedp (binfo) tree binfo;
  2392. { return BINFO_MARKED (binfo); }
  2393. static int bfs_markedp (binfo, i) tree binfo; int i;
  2394. { return BINFO_MARKED (BINFO_BASETYPE (binfo, i)); }
  2395. static int unmarkedp (binfo) tree binfo;
  2396. { return BINFO_MARKED (binfo) == 0; }
  2397. static int bfs_unmarkedp (binfo, i) tree binfo; int i;
  2398. { return BINFO_MARKED (BINFO_BASETYPE (binfo, i)) == 0; }
  2399. static int marked_vtable_pathp (binfo) tree binfo;
  2400. { return BINFO_VTABLE_PATH_MARKED (binfo); }
  2401. static int bfs_marked_vtable_pathp (binfo, i) tree binfo; int i;
  2402. { return BINFO_VTABLE_PATH_MARKED (BINFO_BASETYPE (binfo, i)); }
  2403. static int unmarked_vtable_pathp (binfo) tree binfo;
  2404. { return BINFO_VTABLE_PATH_MARKED (binfo) == 0; }
  2405. static int bfs_unmarked_vtable_pathp (binfo, i) tree binfo; int i;
  2406. { return BINFO_VTABLE_PATH_MARKED (BINFO_BASETYPE (binfo, i)) == 0; }
  2407. static int marked_new_vtablep (binfo) tree binfo;
  2408. { return BINFO_NEW_VTABLE_MARKED (binfo); }
  2409. static int bfs_marked_new_vtablep (binfo, i) tree binfo; int i;
  2410. { return BINFO_NEW_VTABLE_MARKED (BINFO_BASETYPE (binfo, i)); }
  2411. static int unmarked_new_vtablep (binfo) tree binfo;
  2412. { return BINFO_NEW_VTABLE_MARKED (binfo) == 0; }
  2413. static int bfs_unmarked_new_vtablep (binfo, i) tree binfo; int i;
  2414. { return BINFO_NEW_VTABLE_MARKED (BINFO_BASETYPE (binfo, i)) == 0; }
  2415.  
  2416. static int dfs_search_slot_nonempty_p (binfo) tree binfo;
  2417. { return CLASSTYPE_SEARCH_SLOT (BINFO_TYPE (binfo)) != 0; }
  2418.  
  2419. static int dfs_debug_unmarkedp (binfo) tree binfo;
  2420. { return CLASSTYPE_DEBUG_REQUESTED (BINFO_TYPE (binfo)) == 0; }
  2421.  
  2422. /* The worker functions for `dfs_walk'.  These do not need to
  2423.    test anything (vis a vis marking) if they are paired with
  2424.    a predicate function (above).  */
  2425.  
  2426. /* Assign each type within the lattice a number which is unique
  2427.    in the lattice.  The first number assigned is 1.  */
  2428.  
  2429. static void
  2430. dfs_number (binfo)
  2431.      tree binfo;
  2432. {
  2433.   BINFO_CID (binfo) = ++cid;
  2434. }
  2435.  
  2436. static void
  2437. dfs_unnumber (binfo)
  2438.      tree binfo;
  2439. {
  2440.   BINFO_CID (binfo) = 0;
  2441. }
  2442.  
  2443. static void
  2444. dfs_mark (binfo) tree binfo;
  2445. { SET_BINFO_MARKED (binfo); }
  2446.  
  2447. static void
  2448. dfs_unmark (binfo) tree binfo;
  2449. { CLEAR_BINFO_MARKED (binfo); }
  2450.  
  2451. static void
  2452. dfs_mark_vtable_path (binfo) tree binfo;
  2453. { SET_BINFO_VTABLE_PATH_MARKED (binfo); }
  2454.  
  2455. static void
  2456. dfs_unmark_vtable_path (binfo) tree binfo;
  2457. { CLEAR_BINFO_VTABLE_PATH_MARKED (binfo); }
  2458.  
  2459. static void
  2460. dfs_mark_new_vtable (binfo) tree binfo;
  2461. { SET_BINFO_NEW_VTABLE_MARKED (binfo); }
  2462.  
  2463. static void
  2464. dfs_unmark_new_vtable (binfo) tree binfo;
  2465. { CLEAR_BINFO_NEW_VTABLE_MARKED (binfo); }
  2466.  
  2467. static void
  2468. dfs_clear_search_slot (binfo) tree binfo;
  2469. { CLASSTYPE_SEARCH_SLOT (BINFO_TYPE (binfo)) = 0; }
  2470.  
  2471. static void
  2472. dfs_debug_mark (binfo)
  2473.      tree binfo;
  2474. {
  2475.   tree t = BINFO_TYPE (binfo);
  2476.  
  2477.   /* Use heuristic that if there are virtual functions,
  2478.      ignore until we see a non-inline virtual function.  */
  2479.   tree methods = CLASSTYPE_METHOD_VEC (t);
  2480.  
  2481.   CLASSTYPE_DEBUG_REQUESTED (t) = 1;
  2482.  
  2483.   /* If interface info is known, the value of (?@@?) is correct.  */
  2484.   if (methods == 0
  2485.       || CLASSTYPE_INTERFACE_KNOWN (t)
  2486.       || (write_virtuals == 2 && TYPE_VIRTUAL_P (t)))
  2487.     return;
  2488.  
  2489.   /* If debug info is requested from this context for this type, supply it.
  2490.      If debug info is requested from another context for this type,
  2491.      see if some third context can supply it.  */
  2492.   if (current_function_decl == NULL_TREE
  2493.       || DECL_CLASS_CONTEXT (current_function_decl) != t)
  2494.     {
  2495.       if (TREE_VEC_ELT (methods, 0))
  2496.     methods = TREE_VEC_ELT (methods, 0);
  2497.       else
  2498.     methods = TREE_VEC_ELT (methods, 1);
  2499.       while (methods)
  2500.     {
  2501.       if (DECL_VINDEX (methods)
  2502.           && DECL_THIS_INLINE (methods) == 0
  2503.           && DECL_ABSTRACT_VIRTUAL_P (methods) == 0)
  2504.         {
  2505.           /* Somebody, somewhere is going to have to define this
  2506.          virtual function.  When they do, they will provide
  2507.          the debugging info.  */
  2508.           return;
  2509.         }
  2510.       methods = TREE_CHAIN (methods);
  2511.     }
  2512.     }
  2513.   /* We cannot rely on some alien method to solve our problems,
  2514.      so we must write out the debug info ourselves.  */
  2515.   TYPE_DECL_SUPPRESS_DEBUG (TYPE_NAME (t)) = 0;
  2516.   rest_of_type_compilation (t, global_bindings_p ());
  2517. }
  2518.  
  2519. /*  Attach to the type of the virtual base class, the pointer to the
  2520.     virtual base class, given the global pointer vbase_decl_ptr.
  2521.  
  2522.     We use the global vbase_types.  ICK!  */
  2523. static void
  2524. dfs_find_vbases (binfo)
  2525.      tree binfo;
  2526. {
  2527.   tree binfos = BINFO_BASETYPES (binfo);
  2528.   int i, n_baselinks = binfos ? TREE_VEC_LENGTH (binfos) : 0;
  2529.  
  2530.   for (i = n_baselinks-1; i >= 0; i--)
  2531.     {
  2532.       tree base_binfo = TREE_VEC_ELT (binfos, i);
  2533.  
  2534.       if (TREE_VIA_VIRTUAL (base_binfo)
  2535.       && CLASSTYPE_SEARCH_SLOT (BINFO_TYPE (base_binfo)) == 0)
  2536.     {
  2537.       tree vbase = BINFO_TYPE (base_binfo);
  2538.       tree binfo = binfo_member (vbase, vbase_types);
  2539.  
  2540.       CLASSTYPE_SEARCH_SLOT (vbase)
  2541.         = (char *) build (PLUS_EXPR, build_pointer_type (vbase),
  2542.                   vbase_decl_ptr, BINFO_OFFSET (binfo));
  2543.     }
  2544.     }
  2545.   SET_BINFO_VTABLE_PATH_MARKED (binfo);
  2546.   SET_BINFO_NEW_VTABLE_MARKED (binfo);
  2547. }
  2548.  
  2549. static void
  2550. dfs_init_vbase_pointers (binfo)
  2551.      tree binfo;
  2552. {
  2553.   tree type = BINFO_TYPE (binfo);
  2554.   tree fields = TYPE_FIELDS (type);
  2555.   tree this_vbase_ptr;
  2556.  
  2557.   CLEAR_BINFO_VTABLE_PATH_MARKED (binfo);
  2558.  
  2559.   /* If there is a rtti, it is the first field, though perhaps from
  2560.      the base class.  Otherwise, the first fields are virtual base class
  2561.      pointer fields.  */
  2562.   if (CLASSTYPE_RTTI (type) && VFIELD_NAME_P (DECL_NAME (fields)))
  2563.     /* Get past vtable for the object.  */
  2564.     fields = TREE_CHAIN (fields);
  2565.  
  2566.   if (fields == NULL_TREE
  2567.       || DECL_NAME (fields) == NULL_TREE
  2568.       || ! VBASE_NAME_P (DECL_NAME (fields)))
  2569.     return;
  2570.  
  2571.   this_vbase_ptr = vbase_decl_ptr_intermediate;
  2572.  
  2573.   if (build_pointer_type (type) != TYPE_MAIN_VARIANT (TREE_TYPE (this_vbase_ptr)))
  2574.     my_friendly_abort (125);
  2575.  
  2576.   while (fields && DECL_NAME (fields)
  2577.      && VBASE_NAME_P (DECL_NAME (fields)))
  2578.     {
  2579.       tree ref = build (COMPONENT_REF, TREE_TYPE (fields),
  2580.             build_indirect_ref (this_vbase_ptr, NULL_PTR), fields);
  2581.       tree init = (tree)CLASSTYPE_SEARCH_SLOT (TREE_TYPE (TREE_TYPE (fields)));
  2582.       vbase_init_result = tree_cons (binfo_member (TREE_TYPE (TREE_TYPE (fields)),
  2583.                            vbase_types),
  2584.                      build_modify_expr (ref, NOP_EXPR, init),
  2585.                      vbase_init_result);
  2586.       fields = TREE_CHAIN (fields);
  2587.     }
  2588. }
  2589.  
  2590. /* Sometimes this needs to clear both VTABLE_PATH and NEW_VTABLE.  Other
  2591.    times, just NEW_VTABLE, but optimizer should make both with equal
  2592.    efficiency (though it does not currently).  */
  2593. static void
  2594. dfs_clear_vbase_slots (binfo)
  2595.      tree binfo;
  2596. {
  2597.   tree type = BINFO_TYPE (binfo);
  2598.   CLASSTYPE_SEARCH_SLOT (type) = 0;
  2599.   CLEAR_BINFO_VTABLE_PATH_MARKED (binfo);
  2600.   CLEAR_BINFO_NEW_VTABLE_MARKED (binfo);
  2601. }
  2602.  
  2603. tree
  2604. init_vbase_pointers (type, decl_ptr)
  2605.      tree type;
  2606.      tree decl_ptr;
  2607. {
  2608.   if (TYPE_USES_VIRTUAL_BASECLASSES (type))
  2609.     {
  2610.       int old_flag = flag_this_is_variable;
  2611.       tree binfo = TYPE_BINFO (type);
  2612.       flag_this_is_variable = -2;
  2613.       vbase_types = CLASSTYPE_VBASECLASSES (type);
  2614.       vbase_decl_ptr = decl_ptr;
  2615.       vbase_decl = build_indirect_ref (decl_ptr, NULL_PTR);
  2616.       vbase_decl_ptr_intermediate = vbase_decl_ptr;
  2617.       vbase_init_result = NULL_TREE;
  2618.       dfs_walk (binfo, dfs_find_vbases, unmarked_vtable_pathp);
  2619.       dfs_walk (binfo, dfs_init_vbase_pointers, marked_vtable_pathp);
  2620.       dfs_walk (binfo, dfs_clear_vbase_slots, marked_new_vtablep);
  2621.       flag_this_is_variable = old_flag;
  2622.       return vbase_init_result;
  2623.     }
  2624.   return 0;
  2625. }
  2626.  
  2627. /* get the virtual context (the vbase that directly contains the
  2628.    DECL_CLASS_CONTEXT of the FNDECL) that the given FNDECL is declared in,
  2629.    or NULL_TREE if there is none.
  2630.  
  2631.    FNDECL must come from a virtual table from a virtual base to ensure that
  2632.    there is only one possible DECL_CLASS_CONTEXT.
  2633.  
  2634.    We know that if there is more than one place (binfo) the fndecl that the
  2635.    declared, they all refer to the same binfo.  See get_class_offset_1 for
  2636.    the check that ensures this.  */
  2637. static tree
  2638. virtual_context (fndecl, t, vbase)
  2639.      tree fndecl, t, vbase;
  2640. {
  2641.   tree path;
  2642.   if (get_base_distance (DECL_CLASS_CONTEXT (fndecl), t, 0, &path) < 0)
  2643.     {
  2644.       /* DECL_CLASS_CONTEXT can be ambiguous in t.  */
  2645.       if (get_base_distance (DECL_CLASS_CONTEXT (fndecl), vbase, 0, &path) >= 0)
  2646.     {
  2647.       while (path)
  2648.         {
  2649.           /* Not sure if checking path == vbase is necessary here, but just in
  2650.          case it is.  */
  2651.           if (TREE_VIA_VIRTUAL (path) || path == vbase)
  2652.         return binfo_member (BINFO_TYPE (path), CLASSTYPE_VBASECLASSES (t));
  2653.           path = BINFO_INHERITANCE_CHAIN (path);
  2654.         }
  2655.     }
  2656.       /* This shouldn't happen, I don't want errors! */
  2657.       warning ("recoverable compiler error, fixups for virtual function");
  2658.       return vbase;
  2659.     }
  2660.   while (path)
  2661.     {
  2662.       if (TREE_VIA_VIRTUAL (path))
  2663.     return binfo_member (BINFO_TYPE (path), CLASSTYPE_VBASECLASSES (t));
  2664.       path = BINFO_INHERITANCE_CHAIN (path);
  2665.     }
  2666.   return 0;
  2667. }
  2668.  
  2669. /* Fixups upcast offsets for one vtable.
  2670.    Entries may stay within the VBASE given, or
  2671.    they may upcast into a direct base, or
  2672.    they may upcast into a different vbase.
  2673.  
  2674.    We only need to do fixups in case 2 and 3.
  2675.  
  2676.    This routine mirrors fixup_vtable_deltas in functionality, though
  2677.    this one is runtime based, and the other is compile time based.
  2678.    Conceivably that routine could be removed entirely, and all fixups
  2679.    done at runtime.
  2680.  
  2681.    VBASE_OFFSETS is an association list of virtual bases that contains
  2682.    offset information, so the offsets are only calculated once.  */
  2683. static void
  2684. expand_upcast_fixups (binfo, addr, orig_addr, vbase, t, vbase_offsets)
  2685.      tree binfo, addr, orig_addr, vbase, t, *vbase_offsets;
  2686. {
  2687.   tree virtuals = BINFO_VIRTUALS (binfo);
  2688.   tree vc;
  2689.   tree delta;
  2690.   unsigned HOST_WIDE_INT n;
  2691.   
  2692.   delta = purpose_member (vbase, *vbase_offsets);
  2693.   if (! delta)
  2694.     {
  2695.       delta = (tree)CLASSTYPE_SEARCH_SLOT (BINFO_TYPE (vbase));
  2696.       delta = build (MINUS_EXPR, ptrdiff_type_node, delta, addr);
  2697.       delta = save_expr (delta);
  2698.       delta = tree_cons (vbase, delta, *vbase_offsets);
  2699.       *vbase_offsets = delta;
  2700.     }
  2701.  
  2702.   n = skip_rtti_stuff (&virtuals);
  2703.  
  2704.   while (virtuals)
  2705.     {
  2706.       tree current_fndecl = TREE_VALUE (virtuals);
  2707.       current_fndecl = FNADDR_FROM_VTABLE_ENTRY (current_fndecl);
  2708.       current_fndecl = TREE_OPERAND (current_fndecl, 0);
  2709.       if (current_fndecl
  2710.       && current_fndecl != abort_fndecl
  2711.       && (vc=virtual_context (current_fndecl, t, vbase)) != vbase)
  2712.     {
  2713.       /* This may in fact need a runtime fixup. */
  2714.       tree idx = DECL_VINDEX (current_fndecl);
  2715.       tree vtbl = BINFO_VTABLE (binfo);
  2716.       tree nvtbl = lookup_name (DECL_NAME (vtbl), 0);
  2717.       tree aref, ref, naref;
  2718.       tree old_delta, new_delta;
  2719.       tree init;
  2720.  
  2721.       if (nvtbl == NULL_TREE
  2722.           || nvtbl == IDENTIFIER_GLOBAL_VALUE (DECL_NAME (vtbl)))
  2723.         {
  2724.           /* Dup it if it isn't in local scope yet.  */
  2725.           nvtbl = build_decl (VAR_DECL,
  2726.                   DECL_NAME (vtbl),
  2727.                   TYPE_MAIN_VARIANT (TREE_TYPE (BINFO_VTABLE (binfo))));
  2728.           DECL_ALIGN (nvtbl) = MAX (TYPE_ALIGN (double_type_node),
  2729.                     DECL_ALIGN (nvtbl));
  2730.           TREE_READONLY (nvtbl) = 0;
  2731.           nvtbl = pushdecl (nvtbl);
  2732.           init = NULL_TREE;
  2733.           cp_finish_decl (nvtbl, init, NULL_TREE, 0, LOOKUP_ONLYCONVERTING);
  2734.           DECL_VIRTUAL_P (nvtbl) = 1;
  2735.           DECL_CONTEXT (nvtbl) = t;
  2736.           init = build (MODIFY_EXPR, TREE_TYPE (nvtbl),
  2737.                 nvtbl, vtbl);
  2738.           TREE_SIDE_EFFECTS (init) = 1;
  2739.           expand_expr_stmt (init);
  2740.           /* Update the vtable pointers as necessary. */
  2741.           ref = build_vfield_ref (build_indirect_ref (addr, NULL_PTR), DECL_CONTEXT (CLASSTYPE_VFIELD (BINFO_TYPE (binfo))));
  2742.           expand_expr_stmt (build_modify_expr (ref, NOP_EXPR,
  2743.                            build_unary_op (ADDR_EXPR, nvtbl, 0)));
  2744.         }
  2745.       assemble_external (vtbl);
  2746.       aref = build_array_ref (vtbl, idx);
  2747.       naref = build_array_ref (nvtbl, idx);
  2748.       old_delta = build_component_ref (aref, delta_identifier, 0, 0);
  2749.       new_delta = build_component_ref (naref, delta_identifier, 0, 0);
  2750.       old_delta = build_binary_op (PLUS_EXPR, old_delta,
  2751.                        TREE_VALUE (delta), 0);
  2752.       if (vc)
  2753.         {
  2754.           /* If this is set, we need to add in delta adjustments for
  2755.          the other virtual base.  */
  2756.           tree vc_delta = purpose_member (vc, *vbase_offsets);
  2757.           if (! vc_delta)
  2758.         {
  2759.           tree vc_addr = convert_pointer_to_real (vc, orig_addr);
  2760.           vc_delta = (tree)CLASSTYPE_SEARCH_SLOT (BINFO_TYPE (vc));
  2761.           vc_delta = build (MINUS_EXPR, ptrdiff_type_node,
  2762.                     vc_addr, vc_delta);
  2763.           vc_delta = save_expr (vc_delta);
  2764.           *vbase_offsets = tree_cons (vc, vc_delta, *vbase_offsets);
  2765.         }
  2766.           else
  2767.         vc_delta = TREE_VALUE (vc_delta);
  2768.    
  2769.           old_delta = build_binary_op (PLUS_EXPR, old_delta, vc_delta, 0);
  2770.         }
  2771.  
  2772.       TREE_READONLY (new_delta) = 0;
  2773.       expand_expr_stmt (build_modify_expr (new_delta, NOP_EXPR,
  2774.                            old_delta));
  2775.     }
  2776.       ++n;
  2777.       virtuals = TREE_CHAIN (virtuals);
  2778.     }
  2779. }
  2780.  
  2781. /* Fixup upcast offsets for all direct vtables.  Patterned after
  2782.    expand_direct_vtbls_init.  */
  2783. static void
  2784. fixup_virtual_upcast_offsets (real_binfo, binfo, init_self, can_elide, addr, orig_addr, type, vbase, vbase_offsets)
  2785.      tree real_binfo, binfo, addr, orig_addr, type, vbase, *vbase_offsets;
  2786.      int init_self, can_elide;
  2787. {
  2788.   tree real_binfos = BINFO_BASETYPES (real_binfo);
  2789.   tree binfos = BINFO_BASETYPES (binfo);
  2790.   int i, n_baselinks = real_binfos ? TREE_VEC_LENGTH (real_binfos) : 0;
  2791.  
  2792.   for (i = 0; i < n_baselinks; i++)
  2793.     {
  2794.       tree real_base_binfo = TREE_VEC_ELT (real_binfos, i);
  2795.       tree base_binfo = TREE_VEC_ELT (binfos, i);
  2796.       int is_not_base_vtable =
  2797.     i != CLASSTYPE_VFIELD_PARENT (BINFO_TYPE (real_binfo));
  2798.       if (! TREE_VIA_VIRTUAL (real_base_binfo))
  2799.     fixup_virtual_upcast_offsets (real_base_binfo, base_binfo,
  2800.                       is_not_base_vtable, can_elide, addr,
  2801.                       orig_addr, type, vbase, vbase_offsets);
  2802.     }
  2803. #if 0
  2804.   /* Before turning this on, make sure it is correct.  */
  2805.   if (can_elide && ! BINFO_MODIFIED (binfo))
  2806.     return;
  2807. #endif
  2808.   /* Should we use something besides CLASSTYPE_VFIELDS? */
  2809.   if (init_self && CLASSTYPE_VFIELDS (BINFO_TYPE (real_binfo)))
  2810.     {
  2811.       addr = convert_pointer_to_real (binfo, addr);
  2812.       expand_upcast_fixups (real_binfo, addr, orig_addr, vbase, type, vbase_offsets);
  2813.     }
  2814. }
  2815.  
  2816. /* Build a COMPOUND_EXPR which when expanded will generate the code
  2817.    needed to initialize all the virtual function table slots of all
  2818.    the virtual baseclasses.  MAIN_BINFO is the binfo which determines
  2819.    the virtual baseclasses to use; TYPE is the type of the object to
  2820.    which the initialization applies.  TRUE_EXP is the true object we
  2821.    are initializing, and DECL_PTR is the pointer to the sub-object we
  2822.    are initializing.
  2823.  
  2824.    When USE_COMPUTED_OFFSETS is non-zero, we can assume that the
  2825.    object was laid out by a top-level constructor and the computed
  2826.    offsets are valid to store vtables.  When zero, we must store new
  2827.    vtables through virtual baseclass pointers.
  2828.  
  2829.    We setup and use the globals: vbase_decl, vbase_decl_ptr, vbase_types
  2830.    ICK!  */
  2831.  
  2832. void
  2833. expand_indirect_vtbls_init (binfo, true_exp, decl_ptr, use_computed_offsets)
  2834.      tree binfo;
  2835.      tree true_exp, decl_ptr;
  2836.      int use_computed_offsets;
  2837. {
  2838.   tree type = BINFO_TYPE (binfo);
  2839.   if (TYPE_USES_VIRTUAL_BASECLASSES (type))
  2840.     {
  2841.       rtx fixup_insns = NULL_RTX;
  2842.       int old_flag = flag_this_is_variable;
  2843.       tree vbases = CLASSTYPE_VBASECLASSES (type);
  2844.       vbase_types = vbases;
  2845.       vbase_decl_ptr = true_exp ? build_unary_op (ADDR_EXPR, true_exp, 0) : decl_ptr;
  2846.       vbase_decl = true_exp ? true_exp : build_indirect_ref (decl_ptr, NULL_PTR);
  2847.  
  2848.       if (use_computed_offsets)
  2849.     {
  2850.       /* This is an object of type IN_TYPE,  */
  2851.       flag_this_is_variable = -2;
  2852.     }
  2853.  
  2854.       dfs_walk (binfo, dfs_find_vbases, unmarked_new_vtablep);
  2855.  
  2856.       /* Initialized with vtables of type TYPE.  */
  2857.       for (; vbases; vbases = TREE_CHAIN (vbases))
  2858.     {
  2859.       tree addr;
  2860.       if (use_computed_offsets)
  2861.         addr = (tree)CLASSTYPE_SEARCH_SLOT (BINFO_TYPE (vbases));
  2862.       else
  2863.         {
  2864. #if 1
  2865.           addr = convert_pointer_to_vbase (TREE_TYPE (vbases), vbase_decl_ptr);
  2866. #else
  2867.           /* This should should never work better than the above.  (mrs) */
  2868.           tree vbinfo = get_binfo (TREE_TYPE (vbases),
  2869.                        TREE_TYPE (vbase_decl),
  2870.                        0);
  2871.  
  2872.           /* See is we can get lucky.  */
  2873.           if (TREE_VIA_VIRTUAL (vbinfo))
  2874.         addr = convert_pointer_to_real (vbinfo, vbase_decl_ptr);
  2875.           else
  2876.         {
  2877.           /* We go through all these contortions to avoid this
  2878.              call, as it will fail when the virtual base type
  2879.              is ambiguous from here.  We don't yet have a way
  2880.              to search for and find just an instance of the
  2881.              virtual base class.  Searching for the binfo in
  2882.              vbases won't work, as we don't have the vbase
  2883.              pointer field, for all vbases in the main class,
  2884.              only direct vbases.  */
  2885.           addr = convert_pointer_to_real (TREE_TYPE (vbases),
  2886.                           vbase_decl_ptr);
  2887.           if (addr == error_mark_node)
  2888.             continue;
  2889.         }
  2890. #endif
  2891.         }
  2892.  
  2893.       /* Do all vtables from this virtual base. */
  2894.       /* This assumes that virtual bases can never serve as parent
  2895.          binfos.  (in the CLASSTPE_VFIELD_PARENT sense)  */
  2896.       expand_direct_vtbls_init (vbases, TYPE_BINFO (BINFO_TYPE (vbases)),
  2897.                     1, 0, addr);
  2898.  
  2899.       /* If we are using computed offsets we can skip fixups.  */
  2900.       if (use_computed_offsets)
  2901.         continue;
  2902.  
  2903.       /* Now we adjust the offsets for virtual functions that cross
  2904.          virtual boundaries on an implicit upcast on vf call so that
  2905.          the layout of the most complete type is used, instead of
  2906.          assuming the layout of the virtual bases from our current type. */
  2907.  
  2908.       if (flag_vtable_thunks)
  2909.         {
  2910.           /* We don't have dynamic thunks yet!  So for now, just fail silently. */
  2911.         }
  2912.       else
  2913.         {
  2914.           tree vbase_offsets = NULL_TREE;
  2915.           push_to_sequence (fixup_insns);
  2916.           fixup_virtual_upcast_offsets (vbases,
  2917.                         TYPE_BINFO (BINFO_TYPE (vbases)),
  2918.                         1, 0, addr, vbase_decl_ptr,
  2919.                         type, vbases, &vbase_offsets);
  2920.           fixup_insns = get_insns ();
  2921.           end_sequence ();
  2922.         }
  2923.     }
  2924.  
  2925.       if (fixup_insns)
  2926.     {
  2927.       extern tree in_charge_identifier;
  2928.       tree in_charge_node = lookup_name (in_charge_identifier, 0);
  2929.       if (! in_charge_node)
  2930.         {
  2931.           warning ("recoverable internal compiler error, nobody's in charge!");
  2932.           in_charge_node = integer_zero_node;
  2933.         }
  2934.       in_charge_node = build_binary_op (EQ_EXPR, in_charge_node, integer_zero_node, 1);
  2935.       expand_start_cond (in_charge_node, 0);
  2936.       emit_insns (fixup_insns);
  2937.       expand_end_cond ();
  2938.     }
  2939.  
  2940.       dfs_walk (binfo, dfs_clear_vbase_slots, marked_new_vtablep);
  2941.  
  2942.       flag_this_is_variable = old_flag;
  2943.     }
  2944. }
  2945.  
  2946. void
  2947. clear_search_slots (type)
  2948.      tree type;
  2949. {
  2950.   dfs_walk (TYPE_BINFO (type),
  2951.         dfs_clear_search_slot, dfs_search_slot_nonempty_p);
  2952. }
  2953.  
  2954. /* get virtual base class types.
  2955.    This adds type to the vbase_types list in reverse dfs order.
  2956.    Ordering is very important, so don't change it.  */
  2957.  
  2958. static void
  2959. dfs_get_vbase_types (binfo)
  2960.      tree binfo;
  2961. {
  2962.   if (TREE_VIA_VIRTUAL (binfo) && ! BINFO_VBASE_MARKED (binfo))
  2963.     {
  2964.       vbase_types = make_binfo (integer_zero_node, binfo,
  2965.                 BINFO_VTABLE (binfo),
  2966.                 BINFO_VIRTUALS (binfo), vbase_types);
  2967.       TREE_VIA_VIRTUAL (vbase_types) = 1;
  2968.       SET_BINFO_VBASE_MARKED (binfo);
  2969.     }
  2970.   SET_BINFO_MARKED (binfo);
  2971. }
  2972.  
  2973. /* get a list of virtual base classes in dfs order.  */
  2974. tree
  2975. get_vbase_types (type)
  2976.      tree type;
  2977. {
  2978.   tree vbases;
  2979.   tree binfo;
  2980.  
  2981.   if (TREE_CODE (type) == TREE_VEC)
  2982.     binfo = type;
  2983.   else
  2984.     binfo = TYPE_BINFO (type);
  2985.  
  2986.   vbase_types = NULL_TREE;
  2987.   dfs_walk (binfo, dfs_get_vbase_types, unmarkedp);
  2988.   dfs_walk (binfo, dfs_unmark, markedp);
  2989.   /* Rely upon the reverse dfs ordering from dfs_get_vbase_types, and now
  2990.      reverse it so that we get normal dfs ordering.  */
  2991.   vbase_types = nreverse (vbase_types);
  2992.  
  2993.   /* unmark marked vbases */
  2994.   for (vbases = vbase_types; vbases; vbases = TREE_CHAIN (vbases))
  2995.     CLEAR_BINFO_VBASE_MARKED (vbases);
  2996.  
  2997.   return vbase_types;
  2998. }
  2999.  
  3000. static void
  3001. dfs_record_inheritance (binfo)
  3002.      tree binfo;
  3003. {
  3004.   tree binfos = BINFO_BASETYPES (binfo);
  3005.   int i, n_baselinks = binfos ? TREE_VEC_LENGTH (binfos) : 0;
  3006.   mi_boolean *derived_row = BINFO_DERIVES_FROM_STAR (binfo);
  3007.  
  3008.   for (i = n_baselinks-1; i >= 0; i--)
  3009.     {
  3010.       int j;
  3011.       tree base_binfo = TREE_VEC_ELT (binfos, i);
  3012.       tree baseclass = BINFO_TYPE (base_binfo);
  3013.       mi_boolean *base_row = BINFO_DERIVES_FROM_STAR (base_binfo);
  3014.  
  3015.       /* Don't search if there's nothing there!  MI_SIZE can be
  3016.      zero as a result of parse errors.  */
  3017.       if (TYPE_BINFO_BASETYPES (baseclass) && mi_size > 0)
  3018.     for (j = mi_size*(CLASSTYPE_CID (baseclass)-1); j >= 0; j -= mi_size)
  3019.       derived_row[j] |= base_row[j];
  3020.       TYPE_DERIVES_FROM (baseclass, BINFO_TYPE (binfo)) = 1;
  3021.     }
  3022.  
  3023.   SET_BINFO_MARKED (binfo);
  3024. }
  3025.  
  3026. /* Given a _CLASSTYPE node in a multiple inheritance lattice,
  3027.    convert the lattice into a simple relation such that,
  3028.    given to CIDs, C1 and C2, one can determine if C1 <= C2
  3029.    or C2 <= C1 or C1 <> C2.
  3030.  
  3031.    Once constructed, we walk the lattice depth fisrt,
  3032.    applying various functions to elements as they are encountered.
  3033.  
  3034.    We use xmalloc here, in case we want to randomly free these tables.  */
  3035.  
  3036. #define SAVE_MI_MATRIX
  3037.  
  3038. void
  3039. build_mi_matrix (type)
  3040.      tree type;
  3041. {
  3042.   tree binfo = TYPE_BINFO (type);
  3043.   cid = 0;
  3044.  
  3045. #ifdef SAVE_MI_MATRIX
  3046.   if (CLASSTYPE_MI_MATRIX (type))
  3047.     {
  3048.       mi_size = CLASSTYPE_N_SUPERCLASSES (type) + CLASSTYPE_N_VBASECLASSES (type);
  3049.       mi_matrix = CLASSTYPE_MI_MATRIX (type);
  3050.       mi_type = type;
  3051.       dfs_walk (binfo, dfs_number, unnumberedp);
  3052.       return;
  3053.     }
  3054. #endif
  3055.  
  3056.   mi_size = CLASSTYPE_N_SUPERCLASSES (type) + CLASSTYPE_N_VBASECLASSES (type);
  3057.   mi_matrix = (char *)xmalloc ((mi_size + 1) * (mi_size + 1));
  3058.   mi_type = type;
  3059.   bzero (mi_matrix, (mi_size + 1) * (mi_size + 1));
  3060.   dfs_walk (binfo, dfs_number, unnumberedp);
  3061.   dfs_walk (binfo, dfs_record_inheritance, unmarkedp);
  3062.   dfs_walk (binfo, dfs_unmark, markedp);
  3063. }
  3064.  
  3065. void
  3066. free_mi_matrix ()
  3067. {
  3068.   dfs_walk (TYPE_BINFO (mi_type), dfs_unnumber, numberedp);
  3069.  
  3070. #ifdef SAVE_MI_MATRIX
  3071.   CLASSTYPE_MI_MATRIX (mi_type) = mi_matrix;
  3072. #else
  3073.   free (mi_matrix);
  3074.   mi_size = 0;
  3075.   cid = 0;
  3076. #endif
  3077. }
  3078.  
  3079. /* If we want debug info for a type TYPE, make sure all its base types
  3080.    are also marked as being potentially interesting.  This avoids
  3081.    the problem of not writing any debug info for intermediate basetypes
  3082.    that have abstract virtual functions.  Also mark member types.  */
  3083.  
  3084. void
  3085. note_debug_info_needed (type)
  3086.      tree type;
  3087. {
  3088.   tree field;
  3089.   dfs_walk (TYPE_BINFO (type), dfs_debug_mark, dfs_debug_unmarkedp);
  3090.   for (field = TYPE_FIELDS (type); field; field = TREE_CHAIN (field))
  3091.     {
  3092.       tree ttype;
  3093.       if (TREE_CODE (field) == FIELD_DECL
  3094.       && IS_AGGR_TYPE (ttype = target_type (TREE_TYPE (field)))
  3095.       && dfs_debug_unmarkedp (TYPE_BINFO (ttype)))
  3096.     note_debug_info_needed (ttype);
  3097.     }
  3098. }
  3099.  
  3100. /* Subroutines of push_class_decls ().  */
  3101.  
  3102. /* Add in a decl to the envelope.  */
  3103. static void
  3104. envelope_add_decl (type, decl, values)
  3105.      tree type, decl, *values;
  3106. {
  3107.   tree context, *tmp;
  3108.   tree name = DECL_NAME (decl);
  3109.   int dont_add = 0;
  3110.  
  3111.   /* virtual base names are always unique. */
  3112.   if (VBASE_NAME_P (name))
  3113.     *values = NULL_TREE;
  3114.  
  3115.   /* Possible ambiguity.  If its defining type(s)
  3116.      is (are all) derived from us, no problem.  */
  3117.   else if (*values && TREE_CODE (*values) != TREE_LIST)
  3118.     {
  3119.       tree value = *values;
  3120.       /* Only complain if we shadow something we can access.  */
  3121.       if (warn_shadow && TREE_CODE (decl) == FUNCTION_DECL
  3122.       && ((DECL_LANG_SPECIFIC (*values)
  3123.            && DECL_CLASS_CONTEXT (value) == current_class_type)
  3124.           || ! TREE_PRIVATE (value)))
  3125.     /* Should figure out access control more accurately.  */
  3126.     {
  3127.       cp_warning_at ("member `%#D' is shadowed", value);
  3128.       cp_warning_at ("by member function `%#D'", decl);
  3129.       warning ("in this context");
  3130.     }
  3131.  
  3132.       context = (TREE_CODE (value) == FUNCTION_DECL
  3133.          && DECL_VIRTUAL_P (value))
  3134.     ? DECL_CLASS_CONTEXT (value)
  3135.       : DECL_CONTEXT (value);
  3136.  
  3137.       if (context == type)
  3138.     {
  3139.       if (TREE_CODE (value) == TYPE_DECL
  3140.           && DECL_ARTIFICIAL (value))
  3141.         *values = NULL_TREE;
  3142.       else
  3143.         dont_add = 1;
  3144.     }
  3145.       else if (context && TYPE_DERIVES_FROM (context, type))
  3146.     {
  3147.       /* Don't add in *values to list */
  3148.       *values = NULL_TREE;
  3149.     }
  3150.       else
  3151.     *values = build_tree_list (NULL_TREE, value);
  3152.     }
  3153.   else
  3154.     for (tmp = values; *tmp;)
  3155.       {
  3156.     tree value = TREE_VALUE (*tmp);
  3157.     my_friendly_assert (TREE_CODE (value) != TREE_LIST, 999);
  3158.     context = (TREE_CODE (value) == FUNCTION_DECL
  3159.            && DECL_VIRTUAL_P (value))
  3160.       ? DECL_CLASS_CONTEXT (value)
  3161.         : DECL_CONTEXT (value);
  3162.  
  3163.     if (context && TYPE_DERIVES_FROM (context, type))
  3164.       {
  3165.         /* remove *tmp from list */
  3166.         *tmp = TREE_CHAIN (*tmp);
  3167.       }
  3168.     else
  3169.       tmp = &TREE_CHAIN (*tmp);
  3170.       }
  3171.  
  3172.   if (! dont_add)
  3173.     {
  3174.       /* Put the new contents in our envelope.  */
  3175.       if (TREE_CODE (decl) == FUNCTION_DECL)
  3176.     {
  3177.       *values = tree_cons (name, decl, *values);
  3178.       TREE_NONLOCAL_FLAG (*values) = 1;
  3179.       TREE_TYPE (*values) = unknown_type_node;
  3180.     }
  3181.       else
  3182.     {
  3183.       if (*values)
  3184.         {
  3185.           *values = tree_cons (NULL_TREE, decl, *values);
  3186.           /* Mark this as a potentially ambiguous member.  */
  3187.           /* Leaving TREE_TYPE blank is intentional.
  3188.          We cannot use `error_mark_node' (lookup_name)
  3189.          or `unknown_type_node' (all member functions use this).  */
  3190.           TREE_NONLOCAL_FLAG (*values) = 1;
  3191.         }
  3192.       else
  3193.         *values = decl;
  3194.     }
  3195.     }
  3196. }
  3197.  
  3198. /* Add the instance variables which this class contributed to the
  3199.    current class binding contour.  When a redefinition occurs, if the
  3200.    redefinition is strictly within a single inheritance path, we just
  3201.    overwrite the old declaration with the new.  If the fields are not
  3202.    within a single inheritance path, we must cons them.
  3203.  
  3204.    In order to know what decls are new (stemming from the current
  3205.    invocation of push_class_decls) we enclose them in an "envelope",
  3206.    which is a TREE_LIST node where the TREE_PURPOSE slot contains the
  3207.    new decl (or possibly a list of competing ones), the TREE_VALUE slot
  3208.    points to the old value and the TREE_CHAIN slot chains together all
  3209.    envelopes which needs to be "opened" in push_class_decls.  Opening an
  3210.    envelope means: push the old value onto the class_shadowed list,
  3211.    install the new one and if it's a TYPE_DECL do the same to the
  3212.    IDENTIFIER_TYPE_VALUE.  Such an envelope is recognized by seeing that
  3213.    the TREE_PURPOSE slot is non-null, and that it is not an identifier.
  3214.    Because if it is, it could be a set of overloaded methods from an
  3215.    outer scope.  */
  3216.  
  3217. static void
  3218. dfs_pushdecls (binfo)
  3219.      tree binfo;
  3220. {
  3221.   tree type = BINFO_TYPE (binfo);
  3222.   tree fields, *methods, *end;
  3223.   tree method_vec;
  3224.  
  3225.   for (fields = TYPE_FIELDS (type); fields; fields = TREE_CHAIN (fields))
  3226.     {
  3227.       /* Unmark so that if we are in a constructor, and then find that
  3228.      this field was initialized by a base initializer,
  3229.      we can emit an error message.  */
  3230.       if (TREE_CODE (fields) == FIELD_DECL)
  3231.     TREE_USED (fields) = 0;
  3232.  
  3233.       /* Recurse into anonymous unions.  */
  3234.       if (DECL_NAME (fields) == NULL_TREE
  3235.       && TREE_CODE (TREE_TYPE (fields)) == UNION_TYPE)
  3236.     {
  3237.       dfs_pushdecls (TYPE_BINFO (TREE_TYPE (fields)));
  3238.       continue;
  3239.     }
  3240.  
  3241.       if (DECL_NAME (fields))
  3242.     {
  3243.       tree name = DECL_NAME (fields);
  3244.       tree class_value = IDENTIFIER_CLASS_VALUE (name);
  3245.  
  3246.       /* If the class value is not an envelope of the kind described in
  3247.          the comment above, we create a new envelope.  */
  3248.       if (class_value == NULL_TREE || TREE_CODE (class_value) != TREE_LIST
  3249.           || TREE_PURPOSE (class_value) == NULL_TREE
  3250.           || TREE_CODE (TREE_PURPOSE (class_value)) == IDENTIFIER_NODE)
  3251.         {
  3252.           /* See comment above for a description of envelopes.  */
  3253.           closed_envelopes = tree_cons (NULL_TREE, class_value,
  3254.                         closed_envelopes);
  3255.           IDENTIFIER_CLASS_VALUE (name) = closed_envelopes;
  3256.           class_value = IDENTIFIER_CLASS_VALUE (name);
  3257.         }
  3258.  
  3259.       envelope_add_decl (type, fields, &TREE_PURPOSE (class_value));
  3260.     }
  3261.     }
  3262.  
  3263.   method_vec = CLASSTYPE_METHOD_VEC (type);
  3264.   if (method_vec != 0)
  3265.     {
  3266.       /* Farm out constructors and destructors.  */
  3267.       methods = &TREE_VEC_ELT (method_vec, 1);
  3268.       end = TREE_VEC_END (method_vec);
  3269.  
  3270.       while (methods != end)
  3271.     {
  3272.       /* This will cause lookup_name to return a pointer
  3273.          to the tree_list of possible methods of this name.  */
  3274.       tree name = DECL_NAME (*methods);
  3275.       tree class_value = IDENTIFIER_CLASS_VALUE (name);
  3276.  
  3277.       /* If the class value is not an envelope of the kind described in
  3278.          the comment above, we create a new envelope.  */
  3279.       if (class_value == NULL_TREE || TREE_CODE (class_value) != TREE_LIST
  3280.           || TREE_PURPOSE (class_value) == NULL_TREE
  3281.           || TREE_CODE (TREE_PURPOSE (class_value)) == IDENTIFIER_NODE)
  3282.         {
  3283.           /* See comment above for a description of envelopes.  */
  3284.           closed_envelopes = tree_cons (NULL_TREE, class_value,
  3285.                         closed_envelopes);
  3286.           IDENTIFIER_CLASS_VALUE (name) = closed_envelopes;
  3287.           class_value = IDENTIFIER_CLASS_VALUE (name);
  3288.         }
  3289.  
  3290.       /* Here we try to rule out possible ambiguities.
  3291.          If we can't do that, keep a TREE_LIST with possibly ambiguous
  3292.          decls in there.  */
  3293.       maybe_push_cache_obstack ();
  3294.       envelope_add_decl (type, *methods, &TREE_PURPOSE (class_value));
  3295.       pop_obstacks ();
  3296.  
  3297.       methods++;
  3298.     }
  3299.     }
  3300.   SET_BINFO_MARKED (binfo);
  3301. }
  3302.  
  3303. /* Consolidate unique (by name) member functions.  */
  3304. static void
  3305. dfs_compress_decls (binfo)
  3306.      tree binfo;
  3307. {
  3308.   tree type = BINFO_TYPE (binfo);
  3309.   tree method_vec = CLASSTYPE_METHOD_VEC (type);
  3310.  
  3311.   if (method_vec != 0)
  3312.     {
  3313.       /* Farm out constructors and destructors.  */
  3314.       tree *methods = &TREE_VEC_ELT (method_vec, 1);
  3315.       tree *end = TREE_VEC_END (method_vec);
  3316.  
  3317.       for (; methods != end; methods++)
  3318.     {
  3319.       /* This is known to be an envelope of the kind described before
  3320.          dfs_pushdecls.  */
  3321.       tree class_value = IDENTIFIER_CLASS_VALUE (DECL_NAME (*methods));
  3322.       tree tmp = TREE_PURPOSE (class_value);
  3323.  
  3324.       /* This was replaced in scope by somebody else.  Just leave it
  3325.          alone.  */
  3326.       if (TREE_CODE (tmp) != TREE_LIST)
  3327.         continue;
  3328.  
  3329.       if (TREE_CHAIN (tmp) == NULL_TREE
  3330.           && TREE_VALUE (tmp)
  3331.           && DECL_CHAIN (TREE_VALUE (tmp)) == NULL_TREE)
  3332.         {
  3333.           TREE_PURPOSE (class_value) = TREE_VALUE (tmp);
  3334.         }
  3335.     }
  3336.     }
  3337.   CLEAR_BINFO_MARKED (binfo);
  3338. }
  3339.  
  3340. /* When entering the scope of a class, we cache all of the
  3341.    fields that that class provides within its inheritance
  3342.    lattice.  Where ambiguities result, we mark them
  3343.    with `error_mark_node' so that if they are encountered
  3344.    without explicit qualification, we can emit an error
  3345.    message.  */
  3346. void
  3347. push_class_decls (type)
  3348.      tree type;
  3349. {
  3350.   tree id;
  3351.   struct obstack *ambient_obstack = current_obstack;
  3352.  
  3353.   search_stack = push_search_level (search_stack, &search_obstack);
  3354.  
  3355.   id = TYPE_IDENTIFIER (type);
  3356. #if 0
  3357.   if (IDENTIFIER_TEMPLATE (id) != 0)
  3358.     {
  3359.       tree tmpl = IDENTIFIER_TEMPLATE (id);
  3360.       push_template_decls (DECL_ARGUMENTS (TREE_PURPOSE (tmpl)),
  3361.                TREE_VALUE (tmpl), 1);
  3362.       overload_template_name (id, 1);
  3363.     }
  3364. #endif
  3365.  
  3366.   /* Push class fields into CLASS_VALUE scope, and mark.  */
  3367.   dfs_walk (TYPE_BINFO (type), dfs_pushdecls, unmarkedp);
  3368.  
  3369.   /* Compress fields which have only a single entry
  3370.      by a given name, and unmark.  */
  3371.   dfs_walk (TYPE_BINFO (type), dfs_compress_decls, markedp);
  3372.  
  3373.   /* Open up all the closed envelopes and push the contained decls into
  3374.      class scope.  */
  3375.   while (closed_envelopes)
  3376.     {
  3377.       tree new = TREE_PURPOSE (closed_envelopes);
  3378.       tree id;
  3379.  
  3380.       /* This is messy because the class value may be a *_DECL, or a
  3381.      TREE_LIST of overloaded *_DECLs or even a TREE_LIST of ambiguous
  3382.      *_DECLs.  The name is stored at different places in these three
  3383.      cases.  */
  3384.       if (TREE_CODE (new) == TREE_LIST)
  3385.     {
  3386.       if (TREE_PURPOSE (new) != NULL_TREE)
  3387.         id = TREE_PURPOSE (new);
  3388.       else
  3389.         {
  3390.           tree node = TREE_VALUE (new);
  3391.  
  3392.           while (TREE_CODE (node) == TREE_LIST)
  3393.         node = TREE_VALUE (node);
  3394.           id = DECL_NAME (node);
  3395.         }
  3396.     }
  3397.       else
  3398.     id = DECL_NAME (new);
  3399.  
  3400.       /* Install the original class value in order to make
  3401.      pushdecl_class_level work correctly.  */
  3402.       IDENTIFIER_CLASS_VALUE (id) = TREE_VALUE (closed_envelopes);
  3403.       if (TREE_CODE (new) == TREE_LIST)
  3404.     push_class_level_binding (id, new);
  3405.       else
  3406.     pushdecl_class_level (new);
  3407.       closed_envelopes = TREE_CHAIN (closed_envelopes);
  3408.     }
  3409.   current_obstack = ambient_obstack;
  3410. }
  3411.  
  3412. /* Here's a subroutine we need because C lacks lambdas.  */
  3413. static void
  3414. dfs_unuse_fields (binfo)
  3415.      tree binfo;
  3416. {
  3417.   tree type = TREE_TYPE (binfo);
  3418.   tree fields;
  3419.  
  3420.   for (fields = TYPE_FIELDS (type); fields; fields = TREE_CHAIN (fields))
  3421.     {
  3422.       if (TREE_CODE (fields) != FIELD_DECL)
  3423.     continue;
  3424.  
  3425.       TREE_USED (fields) = 0;
  3426.       if (DECL_NAME (fields) == NULL_TREE
  3427.       && TREE_CODE (TREE_TYPE (fields)) == UNION_TYPE)
  3428.     unuse_fields (TREE_TYPE (fields));
  3429.     }
  3430. }
  3431.  
  3432. void
  3433. unuse_fields (type)
  3434.      tree type;
  3435. {
  3436.   dfs_walk (TYPE_BINFO (type), dfs_unuse_fields, unmarkedp);
  3437. }
  3438.  
  3439. void
  3440. pop_class_decls (type)
  3441.      tree type;
  3442. {
  3443.   /* We haven't pushed a search level when dealing with cached classes,
  3444.      so we'd better not try to pop it.  */
  3445.   if (search_stack)
  3446.     search_stack = pop_search_level (search_stack);
  3447. }
  3448.  
  3449. void
  3450. print_search_statistics ()
  3451. {
  3452. #ifdef GATHER_STATISTICS
  3453.   if (flag_memoize_lookups)
  3454.     {
  3455.       fprintf (stderr, "%d memoized contexts saved\n",
  3456.            n_contexts_saved);
  3457.       fprintf (stderr, "%d local tree nodes made\n", my_tree_node_counter);
  3458.       fprintf (stderr, "%d local hash nodes made\n", my_memoized_entry_counter);
  3459.       fprintf (stderr, "fields statistics:\n");
  3460.       fprintf (stderr, "  memoized finds = %d; rejects = %d; (searches = %d)\n",
  3461.            memoized_fast_finds[0], memoized_fast_rejects[0],
  3462.            memoized_fields_searched[0]);
  3463.       fprintf (stderr, "  memoized_adds = %d\n", memoized_adds[0]);
  3464.       fprintf (stderr, "fnfields statistics:\n");
  3465.       fprintf (stderr, "  memoized finds = %d; rejects = %d; (searches = %d)\n",
  3466.            memoized_fast_finds[1], memoized_fast_rejects[1],
  3467.            memoized_fields_searched[1]);
  3468.       fprintf (stderr, "  memoized_adds = %d\n", memoized_adds[1]);
  3469.     }
  3470.   fprintf (stderr, "%d fields searched in %d[%d] calls to lookup_field[_1]\n",
  3471.        n_fields_searched, n_calls_lookup_field, n_calls_lookup_field_1);
  3472.   fprintf (stderr, "%d fnfields searched in %d calls to lookup_fnfields\n",
  3473.        n_outer_fields_searched, n_calls_lookup_fnfields);
  3474.   fprintf (stderr, "%d calls to get_base_type\n", n_calls_get_base_type);
  3475. #else
  3476.   fprintf (stderr, "no search statistics\n");
  3477. #endif
  3478. }
  3479.  
  3480. void
  3481. init_search_processing ()
  3482. {
  3483.   gcc_obstack_init (&search_obstack);
  3484.   gcc_obstack_init (&type_obstack);
  3485.   gcc_obstack_init (&type_obstack_entries);
  3486.  
  3487.   /* This gives us room to build our chains of basetypes,
  3488.      whether or not we decide to memoize them.  */
  3489.   type_stack = push_type_level (0, &type_obstack);
  3490.   _vptr_name = get_identifier ("_vptr");
  3491. }
  3492.  
  3493. void
  3494. reinit_search_statistics ()
  3495. {
  3496.   my_memoized_entry_counter = 0;
  3497.   memoized_fast_finds[0] = 0;
  3498.   memoized_fast_finds[1] = 0;
  3499.   memoized_adds[0] = 0;
  3500.   memoized_adds[1] = 0;
  3501.   memoized_fast_rejects[0] = 0;
  3502.   memoized_fast_rejects[1] = 0;
  3503.   memoized_fields_searched[0] = 0;
  3504.   memoized_fields_searched[1] = 0;
  3505.   n_fields_searched = 0;
  3506.   n_calls_lookup_field = 0, n_calls_lookup_field_1 = 0;
  3507.   n_calls_lookup_fnfields = 0, n_calls_lookup_fnfields_1 = 0;
  3508.   n_calls_get_base_type = 0;
  3509.   n_outer_fields_searched = 0;
  3510.   n_contexts_saved = 0;
  3511. }
  3512.  
  3513. static tree conversions;
  3514. static void
  3515. add_conversions (binfo)
  3516.      tree binfo;
  3517. {
  3518.   tree tmp = CLASSTYPE_FIRST_CONVERSION (BINFO_TYPE (binfo));
  3519.   for (; tmp && IDENTIFIER_TYPENAME_P (DECL_NAME (tmp));
  3520.        tmp = TREE_CHAIN (tmp))
  3521.     conversions = tree_cons (DECL_NAME (tmp), TREE_TYPE (TREE_TYPE (tmp)),
  3522.                  conversions);
  3523. }
  3524.  
  3525. tree
  3526. lookup_conversions (type)
  3527.      tree type;
  3528. {
  3529.   conversions = NULL_TREE;
  3530.   dfs_walk (TYPE_BINFO (type), add_conversions, 0);
  3531.   return conversions;
  3532. }
  3533.