home *** CD-ROM | disk | FTP | other *** search
/ Education Sampler 1992 [NeXTSTEP] / Education_1992_Sampler.iso / NeXT / GnuSource / cplusplus-8 / cplus-search.c < prev    next >
Encoding:
C/C++ Source or Header  |  1991-05-01  |  88.0 KB  |  3,226 lines

  1. /* Breadth-first and depth-first routines for
  2.    searching multiple-inheritance lattice for GNU C++.
  3.    Copyright (C) 1987 Free Software Foundation, Inc.
  4.    Contributed by Michael Tiemann (tiemann@mcc.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 1, 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, 675 Mass Ave, Cambridge, MA 02139, USA.  */
  21.  
  22.  
  23. /* High-level class interface. */
  24.  
  25. #include "config.h"
  26. #include "tree.h"
  27. #include "cplus-tree.h"
  28. #include "obstack.h"
  29. #include "flags.h"
  30. #include "assert.h"
  31. #include <stdio.h>
  32.  
  33. /* For expand_asm_operands.  */
  34. extern char *input_filename;
  35. extern int lineno;
  36.  
  37. #define NULL 0
  38.  
  39. #define obstack_chunk_alloc xmalloc
  40. #define obstack_chunk_free free
  41.  
  42. extern int xmalloc ();
  43. extern void free ();
  44.  
  45. void init_search ();
  46. extern struct obstack *current_obstack;
  47.  
  48. #include "stack.h"
  49.  
  50. /* Obstack used for remembering decision points of breadth-first.  */
  51. static struct obstack search_obstack;
  52.  
  53. /* Obstack used to bridge from one function context to another.  */
  54. static struct obstack bridge_obstack;
  55.  
  56. /* Methods for pushing and popping objects to and from obstacks.  */
  57.  
  58. struct stack_level *
  59. push_stack_level (obstack, tp, size)
  60.      struct obstack *obstack;
  61.      void *tp;
  62.      int size;
  63. {
  64.   struct stack_level *stack;
  65.   stack = (struct stack_level *) obstack_next_free (obstack);
  66.   obstack_grow (obstack, tp, size);
  67.   obstack_finish (obstack);
  68.   stack->obstack = obstack;
  69.   stack->first = (tree *) obstack_base (obstack);
  70.   stack->limit = obstack_room (obstack) / sizeof (tree *);
  71.   return stack;
  72. }
  73.  
  74. struct stack_level *
  75. pop_stack_level (stack)
  76.      struct stack_level *stack;
  77. {
  78.   struct stack_level *tem = stack;
  79.   struct obstack *obstack = tem->obstack;
  80.   stack = tem->prev;
  81.   obstack_free (obstack, tem);
  82.   return stack;
  83. }
  84.  
  85. #define search_level stack_level
  86. static struct search_level *search_stack;
  87.  
  88. static tree lookup_field_1 ();
  89. static int lookup_fnfields_1 ();
  90.  
  91. /* Allocate a level of searching.  */
  92. static struct search_level *
  93. push_search_level (stack, obstack)
  94.      struct stack_level *stack;
  95.      struct obstack *obstack;
  96. {
  97.   struct search_level tem;
  98.   tem.prev = stack;
  99.  
  100.   return push_stack_level (obstack, &tem, sizeof (tem));
  101. }
  102.  
  103. /* Discard a level of search allocation.  */
  104. #define pop_search_level pop_stack_level
  105.  
  106. /* Search memoization.  */
  107. struct type_level
  108. {
  109.   struct stack_level base;
  110.  
  111.   /* First object allocated in obstack of entries.  */
  112.   char *entries;
  113.  
  114.   /* Number of types memoized in this context.  */
  115.   int len;
  116.  
  117.   /* Type being memoized; save this if we are saving
  118.      memoized contexts.  */
  119.   tree type;
  120. };
  121.  
  122. /* Obstack used for memoizing member and member function lookup.  */
  123.  
  124. static struct obstack type_obstack, type_obstack_entries;
  125. static struct type_level *type_stack;
  126. static tree _vptr_name;
  127.  
  128. /* Make things that look like tree nodes, but allocate them
  129.    on type_obstack_entries.  */
  130. static int my_tree_node_counter;
  131. static tree my_tree_cons (), my_build_string ();
  132.  
  133. extern int flag_memoize_lookups, flag_save_memoized_contexts;
  134.  
  135. /* Variables for gathering statistics.  */
  136. static int my_memoized_entry_counter;
  137. static int memoized_fast_finds[2], memoized_adds[2], memoized_fast_rejects[2];
  138. static int memoized_fields_searched[2];
  139. static int n_fields_searched;
  140. static int n_calls_lookup_field, n_calls_lookup_field_1;
  141. static int n_calls_lookup_fnfields, n_calls_lookup_fnfields_1;
  142. static int n_calls_get_base_type;
  143. static int n_outer_fields_searched;
  144. static int n_contexts_saved;
  145.  
  146. /* Local variables to help save memoization contexts.  */
  147. static tree prev_type_memoized;
  148. static struct type_level *prev_type_stack;
  149.  
  150. /* Allocate a level of type memoziation context.  */
  151. static struct type_level *
  152. push_type_level (stack, obstack)
  153.      struct stack_level *stack;
  154.      struct obstack *obstack;
  155. {
  156.   struct type_level tem;
  157.  
  158.   tem.base.prev = stack;
  159.  
  160.   obstack_finish (&type_obstack_entries);
  161.   tem.entries = (char *) obstack_base (&type_obstack_entries);
  162.   tem.len = 0;
  163.   tem.type = NULL_TREE;
  164.  
  165.   return (struct type_level *)push_stack_level (obstack, &tem, sizeof (tem));
  166. }
  167.  
  168. /* Discard a level of type memoziation context.  */
  169.  
  170. static struct type_level *
  171. pop_type_level (stack)
  172.      struct type_level *stack;
  173. {
  174.   obstack_free (&type_obstack_entries, stack->entries);
  175.   return (struct type_level *)pop_stack_level (stack);
  176. }
  177.  
  178. /* Make something that looks like a TREE_LIST, but
  179.    do it on the type_obstack_entries obstack.  */
  180. static tree
  181. my_tree_cons (purpose, value, chain)
  182.      tree purpose, value, chain;
  183. {
  184.   tree p = (tree)obstack_alloc (&type_obstack_entries, sizeof (struct tree_list));
  185.   TREE_UID (p) = ++my_tree_node_counter;
  186.   TREE_TYPE (p) = 0;
  187.   ((int *)p)[3] = 0;
  188.   TREE_SET_CODE (p, TREE_LIST);
  189.   TREE_PURPOSE (p) = purpose;
  190.   TREE_VALUE (p) = value;
  191.   TREE_CHAIN (p) = chain;
  192.   return p;
  193. }
  194.  
  195. static tree
  196. my_build_string (str)
  197.      char *str;
  198. {
  199.   tree p = (tree)obstack_alloc (&type_obstack_entries, sizeof (struct tree_string));
  200.   TREE_UID (p) = ++my_tree_node_counter;
  201.   TREE_TYPE (p) = 0;
  202.   ((int *)p)[3] = 0;
  203.   TREE_SET_CODE (p, STRING_CST);
  204.   TREE_STRING_POINTER (p) = str;
  205.   TREE_STRING_LENGTH (p) = strlen (str);
  206.   return p;
  207. }
  208.  
  209. static tree
  210. my_copy_node (node)
  211.      tree node;
  212. {
  213.   struct obstack *ambient_obstack = current_obstack;
  214.   tree t;
  215.  
  216.   current_obstack = &type_obstack_entries;
  217.  
  218.   t = copy_node (node);
  219.   TREE_UID (t) = ++my_tree_node_counter;
  220.  
  221.   current_obstack = ambient_obstack;
  222.   return t;
  223. }
  224.  
  225. /* Memoizing machinery to make searches for multiple inheritance
  226.    reasonably efficient.  */
  227. #define MEMOIZE_HASHSIZE 8
  228. typedef struct memoized_entry
  229. {
  230.   struct memoized_entry *chain;
  231.   int uid;
  232.   tree data_members[MEMOIZE_HASHSIZE];
  233.   tree function_members[MEMOIZE_HASHSIZE];
  234. } *ME;
  235.  
  236. #define MEMOIZED_CHAIN(ENTRY) (((ME)ENTRY)->chain)
  237. #define MEMOIZED_UID(ENTRY) (((ME)ENTRY)->uid)
  238. #define MEMOIZED_FIELDS(ENTRY,INDEX) (((ME)ENTRY)->data_members[INDEX])
  239. #define MEMOIZED_FNFIELDS(ENTRY,INDEX) (((ME)ENTRY)->function_members[INDEX])
  240. #define MEMOIZED_HASH_FN(NODE) (TREE_UID (NODE)&(MEMOIZE_HASHSIZE - 1))
  241.  
  242. static struct memoized_entry *
  243. my_new_memoized_entry (chain)
  244.      struct memoized_entry *chain;
  245. {
  246.   struct memoized_entry *p =
  247.     (struct memoized_entry *)obstack_alloc (&type_obstack_entries,
  248.                         sizeof (struct memoized_entry));
  249.   bzero (p, sizeof (struct memoized_entry));
  250.   MEMOIZED_CHAIN (p) = chain;
  251.   MEMOIZED_UID (p) = ++my_memoized_entry_counter;
  252.   return p;
  253. }
  254.  
  255. /* When a new function or class context is entered, we build
  256.    a table of types which have been searched for members.
  257.    The table is an array (obstack) of types.  When a type is
  258.    entered into the obstack, its CLASSTYPE_MTABLE_ENTRY
  259.    field is set to point to a new record, of type struct memoized_entry.
  260.  
  261.    A non-NULL TREE_TYPE of the entry contains a visibility error message.
  262.  
  263.    The slots for the data members are arrays of tree nodes.
  264.    These tree nodes are lists, with the TREE_PURPOSE
  265.    of this list the known member name, and the TREE_VALUE
  266.    as the FIELD_DECL for the member.
  267.  
  268.    For member functions, the TREE_PURPOSE is again the
  269.    name of the member functions for that class,
  270.    and the TREE_VALUE of the list is a pairs
  271.    whose TREE_PURPOSE is a member functions of this name,
  272.    and whose TREE_VALUE is a list of known argument lists this
  273.    member function has been called with.  The TREE_TYPE of the pair,
  274.    if non-NULL, is an error message to print.  */
  275.  
  276. /* Tell search machinery that we are entering a new context, and
  277.    to update tables appropriately.
  278.  
  279.    TYPE is the type of the context we are entering, which can
  280.    be NULL_TREE if we are not in a class's scope.
  281.  
  282.    USE_OLD, if nonzero tries to use previous context.  */
  283. void
  284. push_memoized_context (type, use_old)
  285.      tree type;
  286.      int use_old;
  287. {
  288.   int len;
  289.   tree *tem;
  290.  
  291.   if (prev_type_stack)
  292.     {
  293.       if (use_old && prev_type_memoized == type)
  294.     {
  295. #ifdef GATHER_STATISTICS
  296.       n_contexts_saved++;
  297. #endif
  298.       type_stack = prev_type_stack;
  299.       prev_type_stack = 0;
  300.  
  301.       tem = &type_stack->base.first[0];
  302.       len = type_stack->len;
  303.       while (len--)
  304.         CLASSTYPE_MTABLE_ENTRY (tem[len*2]) = tem[len*2+1];
  305.       return;
  306.     }
  307.       /* Otherwise, need to pop old stack here.  */
  308.       type_stack = pop_type_level (prev_type_stack);
  309.       prev_type_memoized = 0;
  310.       prev_type_stack = 0;
  311.     }
  312.  
  313.   type_stack = push_type_level (type_stack, &type_obstack);
  314.   type_stack->type = type;
  315. }
  316.  
  317. /* Tell search machinery that we have left a context.
  318.    We do not currently save these contexts for later use.
  319.    If we wanted to, we could not use pop_search_level, since
  320.    poping that level allows the data we have collected to
  321.    be clobbered; a stack of obstacks would be needed.  */
  322. pop_memoized_context (use_old)
  323.      int use_old;
  324. {
  325.   int len;
  326.   tree *tem = &type_stack->base.first[0];
  327.  
  328.   if (! flag_save_memoized_contexts)
  329.     use_old = 0;
  330.   else if (use_old)
  331.     {
  332.       len = type_stack->len;
  333.       while (len--)
  334.     tem[len*2+1] = (tree)CLASSTYPE_MTABLE_ENTRY (tem[len*2]);
  335.  
  336.       prev_type_stack = type_stack;
  337.       prev_type_memoized = type_stack->type;
  338.     }
  339.  
  340.   if (flag_memoize_lookups)
  341.     {
  342.       len = type_stack->len;
  343.       while (len--)
  344.     CLASSTYPE_MTABLE_ENTRY (tem[len*2])
  345.       = MEMOIZED_CHAIN (CLASSTYPE_MTABLE_ENTRY (tem[len*2]));
  346.     }
  347.   if (! use_old)
  348.     type_stack = pop_type_level (type_stack);
  349.   else
  350.     type_stack = (struct type_level *)type_stack->base.prev;
  351. }
  352.  
  353. /* Some simple list processing predicates.  */
  354.  
  355. /* Check whether TYPE is immediately derived from PARENT.
  356.    Return actual base information if so.  Otherwise, return 0.  */
  357. tree
  358. get_base_type_1 (parent, type)
  359.      register tree parent, type;
  360. {
  361.   register int i;
  362.  
  363.   parent = TYPE_MAIN_VARIANT (parent);
  364.   type = TYPE_MAIN_VARIANT (type);
  365.  
  366.   for (i = 1; i <= CLASSTYPE_N_BASECLASSES (type); i++)
  367.     if (TYPE_MAIN_VARIANT (CLASSTYPE_BASECLASS (type, i)) == parent)
  368.       return CLASSTYPE_BASECLASS (type, i);
  369.  
  370.   return 0;
  371. }
  372.  
  373. /* Check whether TYPE is derived from PARENT.
  374.    Return the actual base information if so.  Otherwise return 0.
  375.    If PROTECT is 1, then emit an error message if access to
  376.    a public field of PARENT would be private.
  377.    If PROTECT is 2, then emit an error message if
  378.    TYPE is derived from PARENT via private visibility rules.
  379.    If PROTECT is 3, then immediately private baseclass is ok,
  380.    but deeper than that, if private, emit error message.  */
  381. tree
  382. get_base_type (parent, type, protect)
  383.      register tree parent, type;
  384. {
  385.   tree xtype = type;
  386.   tree otype;
  387.   int head = 0, tail = 0;
  388.   int is_private = 0;
  389.   tree rval = NULL_TREE;
  390.   int rval_private = 0;
  391.   tree friends = current_class_type
  392.     ? CLASSTYPE_FRIEND_CLASSES (type) : NULL_TREE;
  393.  
  394. #ifdef GATHER_STATISTICS
  395.   n_calls_get_base_type++;
  396. #endif
  397.  
  398.   parent = TYPE_MAIN_VARIANT (parent);
  399.   search_stack = push_search_level (search_stack, &search_obstack);
  400.  
  401.   while (1)
  402.     {
  403.       int i, n_baselinks = CLASSTYPE_N_BASECLASSES (type);
  404.  
  405.       /* Process and/or queue base types.  */
  406.       for (i = 1; i <= n_baselinks; i++)
  407.     if (CLASSTYPE_MARKED5 (CLASSTYPE_BASECLASS (type, i)) == 0)
  408.       {
  409.         int via_private = is_private || !CLASSTYPE_VIA_PUBLIC (type, i);
  410.  
  411.         if (via_private == 0)
  412.           ;
  413.         else if (protect == 0)
  414.           via_private = 0;
  415.         else if (protect == 1 && type == current_class_type)
  416.           /* The immediate base class of the class we are in
  417.          does let its public members through.  */
  418.           via_private = 0;
  419. #ifndef NOJJG
  420.         else if (protect
  421.              && friends != NULL_TREE
  422.              && type == xtype
  423.              && value_member (current_class_type, friends))
  424.           /* Friend types of the most derived type have access
  425.          to its baseclass pointers.  */
  426.           via_private = 0;
  427. #endif
  428.  
  429.         CLASSTYPE_MARKED5 (CLASSTYPE_BASECLASS (type, i)) = 1;
  430.         otype = type;
  431.         obstack_ptr_grow (&search_obstack, CLASSTYPE_BASECLASS (type, i));
  432.         obstack_int_grow (&search_obstack, via_private);
  433.         tail += 2;
  434.         if (tail >= search_stack->limit)
  435.           abort ();
  436.       }
  437.     else if (protect && ! CLASSTYPE_VIA_VIRTUAL (type, i))
  438.       {
  439.         error_with_aggr_type (parent, "type `%s' is ambiguous base class for type `%s'",
  440.                   TYPE_NAME_STRING (xtype));
  441.         error ("(base class for types `%s' and `%s')",
  442.            TYPE_NAME_STRING (type),
  443.            TYPE_NAME_STRING (otype));
  444.         rval = error_mark_node;
  445.         break;
  446.       }
  447.  
  448.     dont_queue:
  449.       /* Process head of queue, if one exists.  */
  450.       if (head >= tail)
  451.     break;
  452.  
  453.       type = search_stack->first[head++];
  454.       is_private = (int)search_stack->first[head++];
  455.       if (TYPE_MAIN_VARIANT (type) == parent)
  456.     {
  457.       if (rval == 0)
  458.         {
  459.           rval = type;
  460.           rval_private = is_private;
  461.         }
  462.       goto dont_queue;
  463.     }
  464.     }
  465.   {
  466.     tree *tp = search_stack->first;
  467.     tree *search_tail = tp + tail;
  468.  
  469.     while (tp < search_tail)
  470.       {
  471.     CLASSTYPE_MARKED5 (*tp) = 0;
  472.     tp += 2;
  473.       }
  474.   }
  475.   search_stack = pop_search_level (search_stack);
  476.  
  477.   if (rval == error_mark_node)
  478.     return error_mark_node;
  479.  
  480.   if (rval && protect && rval_private)
  481.     {
  482.       if (protect == 3)
  483.     {
  484.       int i;
  485.  
  486.       for (i = 1; i <= CLASSTYPE_N_BASECLASSES (xtype); i++)
  487.         if (parent == TYPE_MAIN_VARIANT (CLASSTYPE_BASECLASS (xtype, i)))
  488.           /* It's ok, since it's immedate.  */
  489.           return rval;
  490.     }
  491.       error ("type `%s' is derived from private `%s'",
  492.          TYPE_NAME_STRING (xtype),
  493.          TYPE_NAME_STRING (parent));
  494.       return error_mark_node;
  495.     }
  496.  
  497.   return rval;
  498. }
  499.  
  500. /* Return the number of levels between type PARENT and type TYPE,
  501.    following the leftmost path to PARENT.  If PARENT is its own main
  502.    type variant, then if PARENT appears in different places from TYPE's
  503.    point of view, the leftmost PARENT will be the one chosen.
  504.  
  505.    Return -1 if TYPE is not derived from PARENT.
  506.    Return -2 if PARENT is an ambiguous base class of TYPE.
  507.    Return -3 if PARENT is private to TYPE, and protect is non-zero.
  508.  
  509.    If PATH_PTR is non-NULL, then also build the list of types
  510.    from PARENT to TYPE, with TREE_VIA_VIRUAL and TREE_VIA_PUBLIC
  511.    set.  */
  512. get_base_distance (parent, type, protect, path_ptr)
  513.      register tree parent, type;
  514.      int protect;
  515.      tree *path_ptr;
  516. {
  517.   int head = 0, tail = 0;
  518.   int is_private = 0;
  519.   int rval;
  520.   int depth = 0;
  521.   int rval_private = 0;
  522.   tree basetypes;
  523.   tree friends = current_class_type
  524.     ? CLASSTYPE_FRIEND_CLASSES (type) : NULL_TREE;
  525.   int use_leftmost;
  526.  
  527.   if (TREE_READONLY (parent) || TREE_VOLATILE (parent))
  528.     parent = TYPE_MAIN_VARIANT (parent);
  529.   use_leftmost = (parent == TYPE_MAIN_VARIANT (parent));
  530.  
  531.   if (path_ptr)
  532.     basetypes = CLASSTYPE_AS_LIST (type);
  533.  
  534.   if (TYPE_MAIN_VARIANT (parent) == type)
  535.     {
  536.       /* If the distance is 0, then we don't really need
  537.      a path pointer, but we shouldn't let garbage go back.  */
  538.       if (path_ptr)
  539.     *path_ptr = basetypes;
  540.       return 0;
  541.     }
  542.  
  543.   search_stack = push_search_level (search_stack, &search_obstack);
  544.  
  545.   while (1)
  546.     {
  547.       int i, n_baselinks = CLASSTYPE_N_BASECLASSES (type);
  548.  
  549.       /* Process and/or queue base types.  */
  550.       for (i = 1; i <= n_baselinks; i++)
  551.     if (CLASSTYPE_MARKED5 (CLASSTYPE_BASECLASS (type, i)) == 0)
  552.       {
  553.         tree btypes;
  554.  
  555.         int via_private = is_private || !CLASSTYPE_VIA_PUBLIC (type, i);
  556.  
  557.         if (via_private == 0)
  558.           ;
  559.         else if (protect == 0)
  560.           via_private = 0;
  561. #if 0
  562.         /* 13 Jan, 1990: I guess this is turned off because
  563.            `get_base_type' will emit a more eloquent message
  564.            if a message desired [--Michael].  */
  565.         /* The immediate base class of the class we are in
  566.            does let its public members through.  */
  567.         else if (type == current_class_type)
  568.           via_private = 0;
  569.         else if (protect
  570.              && friends != NULL_TREE
  571.              && type == xtype
  572.              && value_member (current_class_type, friends))
  573.           /* Friend types of the most derived type have access
  574.          to its baseclass pointers.  */
  575.           via_private = 0;
  576. #endif
  577.  
  578.         CLASSTYPE_MARKED5 (CLASSTYPE_BASECLASS (type, i)) = 1;
  579.         obstack_ptr_grow (&search_obstack, CLASSTYPE_BASECLASS (type, i));
  580.  
  581.         obstack_ptr_grow (&search_obstack, depth);
  582.         obstack_int_grow (&search_obstack, via_private);
  583.         if (path_ptr)
  584.           {
  585.         btypes = tree_cons (NULL_TREE, CLASSTYPE_BASECLASS (type, i),
  586.                     basetypes);
  587.         TREE_VIA_PUBLIC (btypes) = CLASSTYPE_VIA_PUBLIC (type, i);
  588.         TREE_VIA_VIRTUAL (btypes) = CLASSTYPE_VIA_VIRTUAL (type, i);
  589.         obstack_ptr_grow (&search_obstack, btypes);
  590.         tail += 1;
  591.           }
  592.         tail += 3;
  593.         if (tail >= search_stack->limit)
  594.           abort ();
  595.       }
  596.     else if (! CLASSTYPE_VIA_VIRTUAL (type, i))
  597.       {
  598.         rval = -2;
  599.         goto done;
  600.       }
  601.  
  602.       /* Process head of queue, if one exists.  */
  603.       if (head >= tail)
  604.     {
  605.       rval = -1;
  606.       break;
  607.     }
  608.  
  609.       type = search_stack->first[head++];
  610.       depth = (int)search_stack->first[head++] + 1;
  611.       is_private = (int)search_stack->first[head++];
  612.       if (path_ptr)
  613.     basetypes = search_stack->first[head++];
  614.       if (type == parent
  615.       || (use_leftmost && TYPE_MAIN_VARIANT (type) == parent))
  616.     {
  617.       rval = depth;
  618.       rval_private = is_private;
  619.       break;
  620.     }
  621.     }
  622.  done:
  623.   {
  624.     tree *tp = search_stack->first;
  625.     tree *search_tail = tp + tail;
  626.     int increment = path_ptr ? 4 : 3;
  627.  
  628.     while (tp < search_tail)
  629.       {
  630.     CLASSTYPE_MARKED5 (*tp) = 0;
  631.     tp += increment;
  632.       }
  633.  
  634.     /* Now, guarantee that we are following the leftmost
  635.        path in the chain.  */
  636.     if (use_leftmost
  637.     && rval > 0
  638.     && (DECL_OFFSET (TYPE_NAME (type)) != 0
  639.         || TREE_VIA_VIRTUAL (type)))
  640.       {
  641.     /* Reduce all types yet to be fully processed into
  642.        the base type we are looking for, or NULL_TREE.  */
  643.     for (tp = search_stack->first; tp < search_tail; tp += increment)
  644.       {
  645.         tree *sub_tp, sub_path_ptr;
  646.         int sub_rval;
  647.  
  648.         /* Don't chase down more right-most paths.  */
  649.         if (TREE_VIA_VIRTUAL (*tp)
  650.         || DECL_OFFSET (TYPE_NAME (*tp)) > DECL_OFFSET (TYPE_NAME (type)))
  651.           {
  652.         *tp = NULL_TREE;
  653.         continue;
  654.           }
  655.  
  656.         /* Don't hassle with duplicates.  */
  657.         if (*tp == type)
  658.           goto skip;
  659.  
  660.         for (sub_tp = search_stack->first; sub_tp < tp; sub_tp += increment)
  661.           if (*tp == *sub_tp)
  662.         goto skip;
  663.  
  664.         /* Find this type's TYPE basetype, if it has one.  */
  665.         sub_rval = get_base_distance (parent, *tp, 0, &sub_path_ptr);
  666.         if (sub_rval == -1)
  667.           *tp = NULL_TREE;
  668.         else
  669.           {
  670.         if (path_ptr && TREE_CHAIN (tp[3]))
  671.           {
  672.             tree last;
  673.             tree next_to_last = sub_path_ptr;
  674.             while (TREE_CHAIN (next_to_last)
  675.                && TREE_CHAIN (TREE_CHAIN (next_to_last)))
  676.               next_to_last = TREE_CHAIN (next_to_last);
  677.             if (next_to_last == sub_path_ptr)
  678.               {
  679.             sub_path_ptr = copy_node (sub_path_ptr);
  680.             last = sub_path_ptr;
  681.               }
  682.             else
  683.               {
  684.             last = copy_node (TREE_CHAIN (next_to_last));
  685.             TREE_CHAIN (next_to_last) = last;
  686.               }
  687.             TREE_CHAIN (last) = TREE_CHAIN (tp[3]);
  688.           }
  689.         *tp = TREE_VALUE (sub_path_ptr);
  690.         if (path_ptr)
  691.           tp[3] = sub_path_ptr;
  692.           }
  693.       skip: {}
  694.       }
  695.  
  696.     /* For all the types which reduce to TYPE, choose
  697.        the leftmost non-virtual one of them.  */
  698.     for (tp = search_stack->first; tp < search_tail; tp += increment)
  699.       {
  700.         if (*tp == NULL_TREE)
  701.           continue;
  702.  
  703.         if (DECL_OFFSET (TYPE_NAME (*tp)) < DECL_OFFSET (TYPE_NAME (type)))
  704.           {
  705.         rval = -2;
  706.         type = *tp;
  707.         if (path_ptr)
  708.           basetypes = tp[3];
  709.           }
  710.       }
  711.     if (rval == -2)
  712.       rval_private = 0;
  713.       }
  714.   }
  715.   search_stack = pop_search_level (search_stack);
  716.  
  717.   if (rval && protect && rval_private)
  718.     return -3;
  719.  
  720.   if (path_ptr)
  721.     *path_ptr = basetypes;
  722.   return rval;
  723. }
  724.  
  725. /* Search for a member with name NAME in a multiple inheritance lattice
  726.    specified by TYPE.  If it does not exist, return NULL_TREE.
  727.    If the member is ambiguously referenced, return `error_mark_node'.
  728.    Otherwise, return the FIELD_DECL.  */
  729.  
  730. /* Do a 1-level search for NAME as a member of TYPE.  The caller
  731.    must figure out whether it has a visible path to this field.
  732.    (Since it is only one level, this is reasonable.)  */
  733. static tree
  734. lookup_field_1 (type, name)
  735.      tree type, name;
  736. {
  737.   register tree field = TYPE_FIELDS (type);
  738.  
  739. #ifdef GATHER_STATISTICS
  740.   n_calls_lookup_field_1++;
  741. #endif
  742.   while (field)
  743.     {
  744. #ifdef GATHER_STATISTICS
  745.       n_fields_searched++;
  746. #endif
  747.       if (DECL_ANON_UNION_ELEM (field))
  748.     {
  749.       tree temp = lookup_field_1 (TREE_TYPE (field), name);
  750.       if (temp)
  751.         return temp;
  752.     }
  753.       if (DECL_NAME (field) == name)
  754.     return field;
  755.       field = TREE_CHAIN (field);
  756.     }
  757.   /* Not found.  */
  758.   if (name == _vptr_name)
  759.     {
  760.       /* Give the user what s/he thinks s/he wants.  */
  761.       if (TYPE_VIRTUAL_P (type))
  762.     return CLASSTYPE_VFIELD (type);
  763.     }
  764.   return NULL_TREE;
  765. }
  766.  
  767. /* Compute the visibility of FIELD.  This is done by computing
  768.    the visibility available to each type in BASETYPES (which comes
  769.    as a list of [via_public/basetype] in reverse order, namely base
  770.    class before derived class).  The first one which defines a
  771.    visibility defines the visibility for the field.  Otherwise, the
  772.    visibility of the field is that which occurs normally.
  773.  
  774.    Uses global variables CURRENT_CLASS_TYPE and
  775.    CURRENT_FUNCTION_DECL to use friend relationships
  776.    if necessary.
  777.  
  778.    This will be static when lookup_fnfield comes into this file.  */
  779.  
  780. #define PUBLIC_RETURN do { TREE_FIELD_PUBLIC (field) = 1; return visibility_public; } while (0)
  781. #define PROTECTED_RETURN do { TREE_FIELD_PROTECTED (field) = 1; return visibility_protected; } while (0)
  782. #define PRIVATE_RETURN do { TREE_FIELD_PRIVATE (field) = 1; return visibility_private; } while (0)
  783.  
  784. enum visibility_type
  785. compute_visibility (basetypes, field)
  786.      tree basetypes, field;
  787. {
  788.   enum visibility_type visibility = visibility_public;
  789.   tree types, type;
  790.   tree context = DECL_FIELD_CONTEXT (field);
  791.  
  792.   /* Virtual function tables are never private.
  793.      But we should know that we are looking for this,
  794.      and not even try to hide it.  */
  795.   if (VFIELD_NAME_P (DECL_NAME (field)) == 1)
  796.     return visibility_public;
  797.  
  798.   /* Make these special cases fast.  */
  799.   if (TREE_VALUE (basetypes) == current_class_type)
  800.     {
  801.       if (TREE_FIELD_PUBLIC (field))
  802.     return visibility_public;
  803.       if (TREE_FIELD_PROTECTED (field))
  804.     return visibility_protected;
  805.       if (TREE_FIELD_PRIVATE (field))
  806.     return visibility_private;
  807.     }
  808.  
  809.   /* Member function manipulating its own members.  */
  810.   if (current_class_type == context)
  811.     PUBLIC_RETURN;
  812.  
  813.   /* Member found immediately within object.  */
  814.   if (TREE_CHAIN (basetypes) == NULL_TREE)
  815.     {
  816.       /* At object's top level, public members are public.  */
  817.       if (TREE_PROTECTED (field) == 0 && TREE_PRIVATE (field) == 0)
  818.     PUBLIC_RETURN;
  819.  
  820.       /* Friend function manipulating members it gets (for being a friend).  */
  821.       if (is_friend (context, current_function_decl))
  822.     PUBLIC_RETURN;
  823.  
  824.       /* Inner than that, without special visibility,
  825.  
  826.        protected members are ok if type of object is current_class_type
  827.        is derived therefrom.  This means that if the type of the object
  828.        is a base type for our current class type, we cannot access
  829.        protected members.
  830.  
  831.        private members are not ok.  */
  832.       if (current_class_type && DECL_VISIBILITY (field) == NULL_TREE)
  833.     {
  834.       if (TREE_PRIVATE (field))
  835.         PRIVATE_RETURN;
  836.  
  837.       if (TREE_PROTECTED (field))
  838.         {
  839.           if (context == current_class_type
  840.           || (type = get_base_type (current_class_type, context, 0)))
  841.         PUBLIC_RETURN;
  842.           else
  843.         PROTECTED_RETURN;
  844.         }
  845.       else abort ();
  846.     }
  847.     }
  848.   /* Friend function manipulating members it gets (for being a friend).  */
  849.   if (is_friend (context, current_function_decl))
  850.     PUBLIC_RETURN;
  851.  
  852.   /* must reverse more than one element */
  853.   basetypes = nreverse (basetypes);
  854.  
  855.   types = basetypes;
  856.  
  857.   while (types)
  858.     {
  859.       tree member;
  860.       type = TYPE_MAIN_VARIANT (TREE_VALUE (types));
  861.  
  862.       member = purpose_member (type, DECL_VISIBILITY (field));
  863.       if (member)
  864.     {
  865.       visibility = (enum visibility_type)TREE_VALUE (member);
  866.       if (visibility == visibility_public
  867.           || is_friend (type, current_function_decl)
  868.           || (visibility == visibility_protected
  869.           && current_class_type
  870.           && get_base_type (context, current_class_type, 0)))
  871.         visibility = visibility_public;
  872.       goto ret;
  873.     }
  874.  
  875.       /* Friends inherit the visibility of the class they inherit from.  */
  876.       if (is_friend (type, current_function_decl))
  877.     {
  878.       if (type == context)
  879.         {
  880.           visibility = visibility_public;
  881.           goto ret;
  882.         }
  883.       if (TREE_PROTECTED (field))
  884.         {
  885.           visibility = visibility_public;
  886.           goto ret;
  887.         }
  888. #if 0
  889.       /* This short-cut is too short.  */
  890.       if (visibility == visibility_public)
  891.         goto ret;
  892. #endif
  893.       /* else, may be a friend of a deeper base class */
  894.     }
  895.  
  896.       if (type == context)
  897.     break;
  898.  
  899.       types = TREE_CHAIN (types);
  900.       /* If the next type was not VIA_PUBLIC, then fields of all
  901.      remaining class past that one are private.  */
  902.       if (types && ! TREE_VIA_PUBLIC (types))
  903.     visibility = visibility_private;
  904.     }
  905.  
  906.   /* No special visibilities apply.  Use normal rules.
  907.      No assignment needed for BASETYPEs here from the nreverse.
  908.      This is because we use it only for information about the
  909.      path to the base.  The code earlier dealt with what
  910.      happens when we are at the base level.  */
  911.  
  912.   if (visibility == visibility_public)
  913.     {
  914.       basetypes = nreverse (basetypes);
  915.       if (TREE_PRIVATE (field))
  916.     PRIVATE_RETURN;
  917.       if (TREE_PROTECTED (field))
  918.     {
  919.       /* Used to check if the current class type was derived from
  920.          the type that contains the field.  This is wrong for
  921.          multiple inheritance because is gives one class reference
  922.          to protected members via another classes protected path.
  923.          I.e., if A; B1 : A; B2 : A;  Then B1 and B2 can access
  924.          their own members which are protected in A, but not
  925.          those same members in one another.  */
  926.       if (
  927. #if 1
  928.           current_class_type
  929.           && get_base_type (context, current_class_type, 0)
  930. #else
  931.           current_class_type
  932.           && value_member (current_class_type, basetypes)
  933. #endif
  934.           )
  935.         PUBLIC_RETURN;
  936.       PROTECTED_RETURN;
  937.     }
  938.       PUBLIC_RETURN;
  939.     }
  940.  
  941.   if (visibility == visibility_private
  942.       && current_class_type != NULL_TREE)
  943.     {
  944.       if (TREE_PRIVATE (field))
  945.     {
  946.       nreverse (basetypes);
  947.       PRIVATE_RETURN;
  948.     }
  949.  
  950.       /* See if the field isn't protected.  */
  951.       if (TREE_PROTECTED (field))
  952.     {
  953.       tree test;
  954. #if 0
  955.       test = get_base_type (type, current_class_type, 0);
  956. #else
  957.       test = value_member (current_class_type, basetypes);
  958. #endif
  959.       nreverse (basetypes);
  960.       if (test)
  961.         PUBLIC_RETURN;
  962.       PROTECTED_RETURN;
  963.     }
  964.  
  965.       /* See if the field isn't a public member of
  966.      a private base class.  */
  967.  
  968.       visibility = visibility_public;
  969.       types = TREE_CHAIN (basetypes);
  970.       while (types)
  971.     {
  972.       if (! TREE_VIA_PUBLIC (types))
  973.         {
  974.           if (visibility == visibility_private)
  975.         {
  976.           visibility = visibility_private;
  977.           goto ret;
  978.         }
  979.           visibility = visibility_private;
  980.         }
  981.       if (TYPE_MAIN_VARIANT (TREE_VALUE (types)) == context)
  982.         {
  983.           visibility = visibility_public;
  984.           goto ret;
  985.         }
  986.       types = TREE_CHAIN (types);
  987.     }
  988.       abort ();
  989.     }
  990.  
  991.  ret:
  992.   nreverse (basetypes);
  993.  
  994.   if (visibility == visibility_public)
  995.     TREE_FIELD_PUBLIC (field) = 1;
  996.   else if (visibility == visibility_protected)
  997.     TREE_FIELD_PROTECTED (field) = 1;
  998.   else if (visibility == visibility_private)
  999.     TREE_FIELD_PRIVATE (field) = 1;
  1000.   else abort ();
  1001.   return visibility;
  1002. }
  1003.  
  1004. /* Make an entry in the memoized table for type TYPE
  1005.    that the entry for NAME is FIELD.  */
  1006.  
  1007. tree
  1008. make_memoized_table_entry (type, name, function_p)
  1009.      tree type, name;
  1010.      int function_p;     
  1011. {
  1012.   int index = MEMOIZED_HASH_FN (name);
  1013.   tree entry, *prev_entry;
  1014.  
  1015.   memoized_adds[function_p] += 1;
  1016.   if (CLASSTYPE_MTABLE_ENTRY (type) == NULL_TREE)
  1017.     {
  1018.       obstack_ptr_grow (&type_obstack, type);
  1019.       obstack_blank (&type_obstack, sizeof (struct memoized_entry *));
  1020.       CLASSTYPE_MTABLE_ENTRY (type) = my_new_memoized_entry (0);
  1021.       type_stack->len++;
  1022.       if (type_stack->len * 2 >= type_stack->base.limit)
  1023.     abort ();
  1024.     }
  1025.   if (function_p)
  1026.     prev_entry = &MEMOIZED_FNFIELDS (CLASSTYPE_MTABLE_ENTRY (type), index);
  1027.   else
  1028.     prev_entry = &MEMOIZED_FIELDS (CLASSTYPE_MTABLE_ENTRY (type), index);
  1029.  
  1030.   entry = my_tree_cons (name, 0, *prev_entry);
  1031.   *prev_entry = entry;
  1032.  
  1033.   /* Don't know the error message to give yet.  */
  1034.   TREE_TYPE (entry) = error_mark_node;
  1035.  
  1036.   return entry;
  1037. }
  1038.  
  1039. tree
  1040. lookup_field (xbasetype, name, protect)
  1041.      register tree xbasetype, name;
  1042.      int protect;
  1043. {
  1044.   int head = 0, tail = 0;
  1045.   tree type, rval;
  1046.   tree basetype, basetypes;
  1047.   enum visibility_type this_v = visibility_default;
  1048.   tree entry;
  1049.   enum visibility_type own_visibility = visibility_default;
  1050.   int vbase_name_p = VBASE_NAME_P (name);
  1051.  
  1052.   /* Things for memoization.  */
  1053.   char *errstr = 0;
  1054.  
  1055.   /* Set this to nonzero if we don't know how to compute
  1056.      accurate error messages for visibility.  */
  1057.   int index = MEMOIZED_HASH_FN (name);
  1058.  
  1059.   if (TREE_CODE (xbasetype) == TREE_LIST)
  1060.     basetypes = xbasetype, basetype = TREE_VALUE (xbasetype);
  1061.   else
  1062.     basetypes = CLASSTYPE_AS_LIST (xbasetype), basetype = xbasetype;
  1063.  
  1064.   if (CLASSTYPE_MTABLE_ENTRY (basetype))
  1065.     {
  1066.       tree tem = MEMOIZED_FIELDS (CLASSTYPE_MTABLE_ENTRY (basetype), index);
  1067.  
  1068.       while (tem && TREE_PURPOSE (tem) != name)
  1069.     {
  1070.       memoized_fields_searched[0]++;
  1071.       tem = TREE_CHAIN (tem);
  1072.     }
  1073.       if (tem)
  1074.     {
  1075.       if (protect && TREE_TYPE (tem))
  1076.         {
  1077.           error (TREE_STRING_POINTER (TREE_TYPE (tem)),
  1078.              IDENTIFIER_POINTER (name),
  1079.              TYPE_NAME_STRING (DECL_FIELD_CONTEXT (TREE_VALUE (tem))));
  1080.           return error_mark_node;
  1081.         }
  1082.       if (TREE_VALUE (tem) == NULL_TREE)
  1083.         memoized_fast_rejects[0] += 1;
  1084.       else
  1085.         memoized_fast_finds[0] += 1;
  1086.       return TREE_VALUE (tem);
  1087.     }
  1088.     }
  1089.  
  1090. #ifdef GATHER_STATISTICS
  1091.   n_calls_lookup_field++;
  1092. #endif
  1093.   if (flag_memoize_lookups && ! global_bindings_p ())
  1094.     entry = make_memoized_table_entry (basetype, name, 0);
  1095.   else
  1096.     entry = 0;
  1097.  
  1098.   rval = lookup_field_1 (basetype, name);
  1099.  
  1100.   if (rval)
  1101.     {
  1102.       if (flag_memoize_lookups || protect)
  1103.     {
  1104.       if (TREE_PRIVATE (rval) | TREE_PROTECTED (rval))
  1105.         this_v = compute_visibility (basetypes, rval);
  1106.       if (TREE_CODE (rval) == CONST_DECL)
  1107.         {
  1108.           if (this_v == visibility_private)
  1109.         errstr = "enum `%s' is a private value of class `%s'";
  1110.           else if (this_v == visibility_protected)
  1111.         errstr = "enum `%s' is a protected value of class `%s'";
  1112.         }
  1113.       else
  1114.         {
  1115.           if (this_v == visibility_private)
  1116.         errstr = "member `%s' is a private member of class `%s'";
  1117.           else if (this_v == visibility_protected)
  1118.         errstr = "member `%s' is a protected member of class `%s'";
  1119.         }
  1120.     }
  1121.  
  1122.       if (entry)
  1123.     {
  1124.       if (errstr)
  1125.         {
  1126.           /* This depends on behavior of lookup_field_1!  */
  1127.           tree error_string = my_build_string (errstr);
  1128.           TREE_TYPE (entry) = error_string;
  1129.         }
  1130.       else
  1131.         {
  1132.           /* Let entry know there is no problem with this access.  */
  1133.           TREE_TYPE (entry) = NULL_TREE;
  1134. #if 0
  1135.           /* And since everything is ok, bear the
  1136.          cost of generating correct code.  */
  1137.           if (DECL_OFFSET (TYPE_NAME (basetype)) != 0
  1138.           || TREE_VIA_VIRTUAL (basetype))
  1139.         {
  1140.           rval = my_copy_node (rval);
  1141.           DECL_FIELD_CONTEXT (rval) = basetype;
  1142.         }
  1143. #endif
  1144.         }
  1145.       TREE_VALUE (entry) = rval;
  1146.     }
  1147. #if 0
  1148.       else if ((DECL_OFFSET (TYPE_NAME (basetype)) != 0
  1149.         || TREE_VIA_VIRTUAL (basetype))
  1150.            && ! (errstr && protect))
  1151.     {
  1152.       rval = my_copy_node (rval);
  1153.       DECL_FIELD_CONTEXT (rval) = basetype;
  1154.     }
  1155. #endif
  1156.  
  1157.       if (errstr && protect)
  1158.     {
  1159.       error (errstr, IDENTIFIER_POINTER (name), TYPE_NAME_STRING (basetype));
  1160.       return error_mark_node;
  1161.     }
  1162.       return rval;
  1163.     }
  1164.  
  1165.   type = TYPE_MAIN_VARIANT (basetype);
  1166.  
  1167.   search_stack = push_search_level (search_stack, &search_obstack);
  1168.   TREE_VIA_PUBLIC (basetypes) = 1;
  1169.  
  1170.   while (1)
  1171.     {
  1172.       int i, n_baselinks = CLASSTYPE_N_BASECLASSES (type);
  1173.  
  1174.       /* Process and/or queue base types.  */
  1175.       for (i = 1; i <= n_baselinks; i++)
  1176.     {
  1177.       if (CLASSTYPE_MARKED2 (CLASSTYPE_BASECLASS (type, i)) == 0)
  1178.         {
  1179.           tree btypes;
  1180.  
  1181.           CLASSTYPE_MARKED2 (CLASSTYPE_BASECLASS (type, i)) = 1;
  1182.           btypes = my_tree_cons (NULL_TREE, CLASSTYPE_BASECLASS (type, i),
  1183.                      basetypes);
  1184.           TREE_VIA_PUBLIC (btypes) = CLASSTYPE_VIA_PUBLIC (type, i);
  1185.           TREE_VIA_VIRTUAL (btypes) = CLASSTYPE_VIA_VIRTUAL (type, i);
  1186.           obstack_ptr_grow (&search_obstack, btypes);
  1187.           tail += 1;
  1188.           if (tail >= search_stack->limit)
  1189.         abort ();
  1190.         }
  1191.     }
  1192.  
  1193.       /* Process head of queue, if one exists.  */
  1194.       if (head >= tail)
  1195.     break;
  1196.  
  1197.       basetypes = search_stack->first[head++];
  1198.       type = TREE_VALUE (basetypes);
  1199.  
  1200.       /* See if we can find NAME in TYPE.  If RVAL is nonzero,
  1201.      and we do find NAME in TYPE, verify that such a second
  1202.      sighting is in fact legal.  */
  1203.  
  1204.       if (rval)
  1205.     {
  1206.       /* Just another way of finding the same member.  */
  1207.       if (DECL_FIELD_CONTEXT (rval) == type)
  1208.         {
  1209.           enum visibility_type new_v
  1210.         = compute_visibility (basetypes, rval);
  1211.           if (this_v != new_v)
  1212.         errstr = "conflicting visibilities to member `%s'";
  1213.         }
  1214.       /* Same baseclass, different places in the lattice.  */
  1215.       else if (DECL_FIELD_CONTEXT (rval) == TYPE_MAIN_VARIANT (type))
  1216.         errstr = "member `%s' belongs to distinct base classes `%s'";
  1217.       else
  1218.         {
  1219.           tree nval = lookup_field_1 (type, name);
  1220.  
  1221.           if (nval && get_base_type (type, DECL_FIELD_CONTEXT (rval), 0) == 0)
  1222.         {
  1223.           /* We found it in other than a baseclass of RVAL's.  */
  1224.           errstr = "request for member `%s' is ambiguous";
  1225.         }
  1226.         }
  1227.       if (errstr && entry)
  1228.         {
  1229.           tree error_string = my_build_string (errstr);
  1230.           TREE_TYPE (entry) = error_string;
  1231.         }
  1232.       if (errstr && protect)
  1233.         break;
  1234.     }
  1235.       else
  1236.     {
  1237.       rval = lookup_field_1 (type, name);
  1238.       if (rval)
  1239.         {
  1240. #if 0
  1241.           if (DECL_OFFSET (TYPE_NAME (type)) != 0
  1242.           || TREE_VIA_VIRTUAL (type))
  1243.         {
  1244.           rval = my_copy_node (rval);
  1245.           DECL_FIELD_CONTEXT (rval) = type;
  1246.         }
  1247. #endif
  1248.  
  1249.           if (entry || protect)
  1250.         this_v = compute_visibility (basetypes, rval);
  1251.           if (entry)
  1252.         TREE_VALUE (entry) = rval;
  1253.  
  1254.           /* These may look ambiguous, but they really are not.  */
  1255.           if (vbase_name_p)
  1256.         break;
  1257.         }
  1258.     }
  1259.     }
  1260.   {
  1261.     tree *tp = search_stack->first;
  1262.     tree *search_tail = tp + tail;
  1263.  
  1264.     /* If this FIELD_DECL defines its own visibility, deal with that.  */
  1265.     if (rval && errstr == 0
  1266.     && DECL_VISIBILITY (rval)
  1267.     && (protect || entry))
  1268.       {
  1269.     while (tp < search_tail)
  1270.       {
  1271.         /* If is possible for one of the derived types on the
  1272.            path to have defined special visibility for this
  1273.            field.  Look for such declarations and report an
  1274.            error if a conflict is found.  */
  1275.         enum visibility_type new_v;
  1276.  
  1277.         if (this_v != visibility_default)
  1278.           new_v = compute_visibility (*tp, rval);
  1279.         if (this_v != visibility_default && new_v != this_v)
  1280.           {
  1281.         errstr = "conflicting visibilities to member `%s'";
  1282.         this_v = visibility_default;
  1283.           }
  1284.         own_visibility = new_v;
  1285.         CLASSTYPE_MARKED2 (TREE_VALUE (*tp++)) = 0;
  1286.       }
  1287.       }
  1288.     else
  1289.       {
  1290.     while (tp < search_tail)
  1291.       CLASSTYPE_MARKED2 (TREE_VALUE (*tp++)) = 0;
  1292.       }
  1293.   }
  1294.   search_stack = pop_search_level (search_stack);
  1295.  
  1296.   if (errstr == 0)
  1297.     {
  1298.       if (own_visibility == visibility_private)
  1299.     errstr = "member `%s' declared private";
  1300.       else if (own_visibility == visibility_protected)
  1301.     errstr = "member `%s' declared protected";
  1302.       else if (this_v == visibility_private)
  1303.     errstr = TREE_PRIVATE (rval) ? "member `%s' is private" : "member `%s' is from private base class";
  1304.       else if (this_v == visibility_protected)
  1305.     errstr = "member `%s' is protected";
  1306.     }
  1307.  
  1308.   if (entry)
  1309.     {
  1310.       if (errstr)
  1311.     {
  1312.       tree error_string = my_build_string (errstr);
  1313.       /* Save error message with entry.  */
  1314.       TREE_TYPE (entry) = error_string;
  1315.     }
  1316.       else
  1317.     {
  1318.       /* Mark entry as having no error string.  */
  1319.       TREE_TYPE (entry) = NULL_TREE;
  1320.     }
  1321.     }
  1322.  
  1323.   if (errstr && protect)
  1324.     {
  1325.       error (errstr, IDENTIFIER_POINTER (name), TYPE_NAME_STRING (type));
  1326.       rval = error_mark_node;
  1327.     }
  1328.   return rval;
  1329. }
  1330.  
  1331. /* TYPE is a class type. Return the index of the fields within
  1332.    the method vector with name NAME, or -1 is no such field exists.  */
  1333. static int
  1334. lookup_fnfields_1 (type, name)
  1335.      tree type, name;
  1336. {
  1337.   register tree method_vec = CLASSTYPE_METHOD_VEC (type);
  1338.  
  1339.   if (method_vec != 0)
  1340.     {
  1341.       register tree *methods = &TREE_VEC_ELT (method_vec, 0);
  1342.       register tree *end = TREE_VEC_END (method_vec);
  1343.  
  1344. #ifdef GATHER_STATISTICS
  1345.       n_calls_lookup_fnfields_1++;
  1346. #endif
  1347.       if (*methods == 0)
  1348.     methods++;
  1349.       while (methods != end)
  1350.     {
  1351. #ifdef GATHER_STATISTICS
  1352.       n_outer_fields_searched++;    
  1353. #endif
  1354.       if (DECL_ORIGINAL_NAME (*methods) == name)
  1355.         break;
  1356.       methods++;
  1357.     }
  1358.       if (methods != end)
  1359.     return methods - &TREE_VEC_ELT (method_vec, 0);
  1360.     }
  1361.  
  1362.   return -1;
  1363. }
  1364.  
  1365. /* Given a list of member functions FIELDS (which are implicitly
  1366.    named TREE_PURPOSE (FIELDS), and come from base type
  1367.    DECL_FIELD_CONTEXT (TREE_VALUE (FIELDS))), attempt to find the
  1368.    actual method which can accept (using conversions) PARMS.
  1369.    The types of PARMS are already computed in PARMTYPES.  */
  1370. tree
  1371. lookup_fnfield (fields, parms, parmtypes)
  1372.      tree fields, parms, parmtypes;
  1373. {
  1374.   abort ();
  1375. }
  1376.  
  1377. /* Starting from BASETYPE, return a TREE_BASELINK-like object
  1378.    which gives the following information (in a list):
  1379.  
  1380.    TREE_TYPE: list of basetypes needed to get to...
  1381.    TREE_VALUE: list of all functions in of given type
  1382.    which have name NAME.
  1383.  
  1384.    No visibility information is computed by this function,
  1385.    other then to adorn the list of basetypes with
  1386.    TREE_VIA_PUBLIC.
  1387.  
  1388.    If FIND_AMBIGUOUS is non-zero, then if we find two ways to get
  1389.    to the same member function, both those ways are found,
  1390.    and the caller must know what to do about this.  */
  1391. tree
  1392. lookup_fnfields (basetypes, name, find_ambiguous)
  1393.      tree basetypes, name;
  1394.      int find_ambiguous;
  1395. {
  1396.   int head = 0, tail = 0;
  1397.   tree type, rval, rvals = NULL_TREE;
  1398.   tree basetype;
  1399.   tree entry;
  1400.  
  1401.   /* For now, don't try this.  */
  1402.   int protect = find_ambiguous;
  1403.  
  1404.   /* Things for memoization.  */
  1405.   char *errstr = 0;
  1406.  
  1407.   /* Set this to nonzero if we don't know how to compute
  1408.      accurate error messages for visibility.  */
  1409.   int index = MEMOIZED_HASH_FN (name);
  1410.  
  1411.   basetype = TREE_VALUE (basetypes);
  1412.  
  1413.   if (CLASSTYPE_MTABLE_ENTRY (basetype))
  1414.     {
  1415.       tree tem = MEMOIZED_FNFIELDS (CLASSTYPE_MTABLE_ENTRY (basetype), index);
  1416.  
  1417.       while (tem && TREE_PURPOSE (tem) != name)
  1418.     {
  1419.       memoized_fields_searched[1]++;
  1420.       tem = TREE_CHAIN (tem);
  1421.     }
  1422.       if (tem)
  1423.     {
  1424.       if (protect && TREE_TYPE (tem))
  1425.         {
  1426.           error (TREE_STRING_POINTER (TREE_TYPE (tem)),
  1427.              IDENTIFIER_POINTER (name),
  1428.              TYPE_NAME_STRING (DECL_FIELD_CONTEXT (TREE_VALUE (TREE_VALUE (tem)))));
  1429.           return error_mark_node;
  1430.         }
  1431.       if (TREE_VALUE (tem) == NULL_TREE)
  1432.         {
  1433.           memoized_fast_rejects[1] += 1;
  1434.           return NULL_TREE;
  1435.         }
  1436.       else
  1437.         {
  1438.           /* Want to return this, but we must make sure
  1439.          that visibility information is consistent.  */
  1440.           tree baselink = TREE_VALUE (tem);
  1441.           tree memoized_basetypes = TREE_PURPOSE (baselink);
  1442.           tree these_basetypes = basetypes;
  1443.           while (memoized_basetypes && these_basetypes)
  1444.         {
  1445.           memoized_fields_searched[1]++;
  1446.           if (TREE_VALUE (memoized_basetypes) != TREE_VALUE (these_basetypes))
  1447.             break;
  1448.           memoized_basetypes = TREE_CHAIN (memoized_basetypes);
  1449.           these_basetypes = TREE_CHAIN (these_basetypes);
  1450.         }
  1451.           if (memoized_basetypes == these_basetypes)
  1452.         {
  1453.           memoized_fast_finds[1] += 1;
  1454.           return TREE_VALUE (tem);
  1455.         }
  1456.           /* else, we must re-find this field by hand.  */
  1457.           baselink = tree_cons (basetypes, TREE_VALUE (baselink), TREE_CHAIN (baselink));
  1458.           return baselink;
  1459.         }
  1460.     }
  1461.     }
  1462.  
  1463. #ifdef GATHER_STATISTICS
  1464.   n_calls_lookup_fnfields++;
  1465. #endif
  1466.   if (flag_memoize_lookups && ! global_bindings_p ())
  1467.     entry = make_memoized_table_entry (basetype, name, 1);
  1468.   else
  1469.     entry = 0;
  1470.  
  1471.   index = lookup_fnfields_1 (basetype, name);
  1472.  
  1473.   if (index >= 0)
  1474.     {
  1475.       rval = TREE_VEC_ELT (CLASSTYPE_METHOD_VEC (basetype), index);
  1476.       rvals = my_tree_cons (basetypes, rval, NULL_TREE);
  1477.       if (CLASSTYPE_BASELINK_VEC (basetype))
  1478.     TREE_TYPE (rvals) = TREE_VEC_ELT (CLASSTYPE_BASELINK_VEC (basetype), index);
  1479.  
  1480.       if (entry)
  1481.     {
  1482.       TREE_VALUE (entry) = rvals;
  1483.       TREE_TYPE (entry) = NULL_TREE;
  1484.     }
  1485.  
  1486.       if (errstr && protect)
  1487.     {
  1488.       error (errstr, IDENTIFIER_POINTER (name), TYPE_NAME_STRING (basetype));
  1489.       return error_mark_node;
  1490.     }
  1491.       return rvals;
  1492.     }
  1493.   rval = NULL_TREE;
  1494.   type = TYPE_MAIN_VARIANT (basetype);
  1495.  
  1496.   search_stack = push_search_level (search_stack, &search_obstack);
  1497.   TREE_VIA_PUBLIC (basetypes) = 1;
  1498.  
  1499.   while (1)
  1500.     {
  1501.       int i, n_baselinks = CLASSTYPE_N_BASECLASSES (type);
  1502.  
  1503.       /* Process and/or queue base types.  */
  1504.       for (i = 1; i <= n_baselinks; i++)
  1505.     {
  1506.       if (CLASSTYPE_MARKED2 (CLASSTYPE_BASECLASS (type, i)) == 0)
  1507.         {
  1508.           tree btypes;
  1509.  
  1510.           CLASSTYPE_MARKED2 (CLASSTYPE_BASECLASS (type, i)) = 1;
  1511.           btypes = my_tree_cons (NULL_TREE, CLASSTYPE_BASECLASS (type, i),
  1512.                      basetypes);
  1513.           TREE_VIA_PUBLIC (btypes) = CLASSTYPE_VIA_PUBLIC (type, i);
  1514.           TREE_VIA_VIRTUAL (btypes) = CLASSTYPE_VIA_VIRTUAL (type, i);
  1515.           obstack_ptr_grow (&search_obstack, btypes);
  1516.           tail += 1;
  1517.           if (tail >= search_stack->limit)
  1518.         abort ();
  1519.         }
  1520.     }
  1521.  
  1522.       /* Process head of queue, if one exists.  */
  1523.       if (head >= tail)
  1524.     break;
  1525.  
  1526.       basetypes = search_stack->first[head++];
  1527.       type = TREE_VALUE (basetypes);
  1528.  
  1529.       /* See if we can find NAME in TYPE.  If RVAL is nonzero,
  1530.      and we do find NAME in TYPE, verify that such a second
  1531.      sighting is in fact legal.  */
  1532.  
  1533.       if (rval)
  1534.     {
  1535.       tree context = DECL_FIELD_CONTEXT (rval);
  1536.       /* Just another way of finding the same member.  */
  1537.       if (context == type)
  1538.         ;
  1539.       /* Same baseclass, maybe different places in the lattice.  */
  1540.       else if (context == TYPE_MAIN_VARIANT (type))
  1541.         {
  1542.           if (TREE_VIA_VIRTUAL (TREE_PURPOSE (rvals)))
  1543.         if (TREE_VIA_VIRTUAL (basetypes))
  1544.           ;
  1545.         else
  1546.           errstr = "member `%s' belongs to virtual and non-virtual baseclasses `%s'";
  1547.           else if (TREE_VIA_VIRTUAL (basetypes))
  1548.         errstr = "member `%s' belongs to virtual and non-virtual baseclasses `%s'";
  1549.           else
  1550.         errstr = "member `%s' belongs to MI-distinct base classes `%s'";
  1551.         }
  1552.       else
  1553.         {
  1554.           int index = lookup_fnfields_1 (type, name);
  1555.  
  1556.           if (index >= 0 && get_base_type (type, context, 0) == 0)
  1557.         {
  1558.           /* We found it in other than a baseclass of RVAL's.  */
  1559.           rvals = my_tree_cons (basetypes, TREE_VEC_ELT (CLASSTYPE_METHOD_VEC (type), index), rvals);
  1560.           if (CLASSTYPE_BASELINK_VEC (type))
  1561.             TREE_TYPE (rvals) = TREE_VEC_ELT (CLASSTYPE_BASELINK_VEC (type), index);
  1562.         }
  1563.         }
  1564.       if (errstr && entry)
  1565.         {
  1566.           tree error_string = my_build_string (errstr);
  1567.           TREE_TYPE (entry) = error_string;
  1568.         }
  1569.       if (errstr && find_ambiguous)
  1570.         {
  1571.           rvals = error_mark_node;
  1572.           break;
  1573.         }
  1574.     }
  1575.       else
  1576.     {
  1577.       int index = lookup_fnfields_1 (type, name);
  1578.       if (index >= 0)
  1579.         {
  1580.           rval = TREE_VEC_ELT (CLASSTYPE_METHOD_VEC (type), index);
  1581.           rvals = my_tree_cons (basetypes, rval, NULL_TREE);
  1582.           if (CLASSTYPE_BASELINK_VEC (type))
  1583.         TREE_TYPE (rvals) = TREE_VEC_ELT (CLASSTYPE_BASELINK_VEC (type), index);
  1584.           if (entry)
  1585.         TREE_VALUE (entry) = rvals;
  1586.         }
  1587.       else
  1588.         rval = NULL_TREE;
  1589.     }
  1590.     }
  1591.   {
  1592.     tree *tp = search_stack->first;
  1593.     tree *search_tail = tp + tail;
  1594.  
  1595.     while (tp < search_tail)
  1596.       CLASSTYPE_MARKED2 (TREE_VALUE (*tp++)) = 0;
  1597.   }
  1598.   search_stack = pop_search_level (search_stack);
  1599.  
  1600.   if (entry)
  1601.     {
  1602.       if (errstr)
  1603.     {
  1604.       tree error_string = my_build_string (errstr);
  1605.       /* Save error message with entry.  */
  1606.       TREE_TYPE (entry) = error_string;
  1607.     }
  1608.       else
  1609.     {
  1610.       /* Mark entry as having no error string.  */
  1611.       TREE_TYPE (entry) = NULL_TREE;
  1612.     }
  1613.     }
  1614.  
  1615.   if (errstr && protect)
  1616.     {
  1617.       error (errstr, IDENTIFIER_POINTER (name), TYPE_NAME_STRING (type));
  1618.       rvals = error_mark_node;
  1619.     }
  1620.  
  1621.   return rvals;
  1622. }
  1623.  
  1624. /* BREADTH-FIRST SEARCH ROUTINES.  */
  1625.  
  1626. /* Search a multiple inheritance hierarchy by breadth-first search.
  1627.  
  1628.    TYPE is an aggregate type, possibly in a multiple-inheritance hierarchy.
  1629.    TESTFN is a function, which, if true, means that our condition has been met,
  1630.    and its return value should be returned.
  1631.    QFN, if non-NULL, is a predicate dictating whether the type should
  1632.    even be queued.  */
  1633.  
  1634. int
  1635. breadth_first_search (type, testfn, qfn)
  1636.      tree type;
  1637.      int (*testfn)();
  1638.      int (*qfn)();
  1639. {
  1640.   int head = 0, tail = 0;
  1641.   int rval = 0;
  1642.  
  1643.   search_stack = push_search_level (search_stack, &search_obstack);
  1644.  
  1645.   while (1)
  1646.     {
  1647.       int n_baselinks = CLASSTYPE_N_BASECLASSES (type);
  1648.       int i;
  1649.  
  1650.       /* Process and/or queue base types.  */
  1651.       for (i = 1; i <= n_baselinks; i++)
  1652.     if (CLASSTYPE_MARKED (CLASSTYPE_BASECLASS (type, i)) == 0
  1653.         && (qfn == 0 || (*qfn) (type, i)))
  1654.       {
  1655.         CLASSTYPE_MARKED (CLASSTYPE_BASECLASS (type, i)) = 1;
  1656.         obstack_ptr_grow (&search_obstack, type);
  1657.         obstack_int_grow (&search_obstack, i);
  1658.         tail += 2;
  1659.         if (tail >= search_stack->limit)
  1660.           abort ();
  1661.       }
  1662.  
  1663.       /* Process head of queue, if one exists.  */
  1664.       if (head >= tail)
  1665.     {
  1666.       rval = 0;
  1667.       break;
  1668.     }
  1669.  
  1670.       type = search_stack->first[head++];
  1671.       i = (int)search_stack->first[head++];
  1672.       if (rval = (*testfn) (type, i))
  1673.     break;
  1674.       type = CLASSTYPE_BASECLASS (type, i);
  1675.     }
  1676.   {
  1677.     tree *tp = search_stack->first;
  1678.     tree *search_tail = tp + tail;
  1679.     while (tp < search_tail)
  1680.       {
  1681.     tree type = *tp++;
  1682.     int i = (int)(*tp++);
  1683.     CLASSTYPE_MARKED (CLASSTYPE_BASECLASS (type, i)) = 0;
  1684.       }
  1685.   }
  1686.  
  1687.   search_stack = pop_search_level (search_stack);
  1688.   return rval;
  1689. }
  1690.  
  1691. /* Functions to use in breadth first searches.  */
  1692. typedef tree (*pft)();
  1693. typedef int (*pfi)();
  1694.  
  1695. int tree_needs_constructor_p (type, i)
  1696.      tree type;
  1697. {
  1698.   tree basetype = i == 0 ? type : CLASSTYPE_BASECLASS (type, i);
  1699.   return TYPE_NEEDS_CONSTRUCTOR (basetype);
  1700. }
  1701.  
  1702. static tree declarator;
  1703.  
  1704. static tree
  1705. get_virtuals_named_this (type, i)
  1706.      tree type;
  1707.      int i;
  1708. {
  1709.   tree basetype = i == 0? type : CLASSTYPE_BASECLASS (type, i);
  1710.   tree fields = lookup_fnfields (CLASSTYPE_AS_LIST (basetype), declarator, 0);
  1711.  
  1712.   if (fields == 0 || fields == error_mark_node)
  1713.     return 0;
  1714.  
  1715.   /* Get to the function decls, and return the first virtual function
  1716.      with this name, if there is one.  */
  1717.   while (fields)
  1718.     {
  1719.       tree fndecl;
  1720.  
  1721.       for (fndecl = TREE_VALUE (fields); fndecl; fndecl = TREE_CHAIN (fndecl))
  1722.     if (DECL_VIRTUAL_P (fndecl))
  1723.       return fields;
  1724.       fields = next_baselink (fields);
  1725.     }
  1726.   return NULL_TREE;
  1727. }
  1728.  
  1729. static tree get_virtual_destructor (type, i)
  1730.      tree type;
  1731.      int i;
  1732. {
  1733.   type = i == 0 ? type : CLASSTYPE_BASECLASS (type, i);
  1734.   if (TYPE_HAS_DESTRUCTOR (type)
  1735.       && DECL_VIRTUAL_P (TREE_VEC_ELT (CLASSTYPE_METHOD_VEC (type), 0)))
  1736.     return TREE_VEC_ELT (CLASSTYPE_METHOD_VEC (type), 0);
  1737.   return 0;
  1738. }
  1739.  
  1740. int tree_has_any_destructor_p (type, i)
  1741.      tree type;
  1742.      int i;
  1743. {
  1744.   if (i == 0)
  1745.     return TYPE_NEEDS_DESTRUCTOR (type);
  1746.   return TYPE_NEEDS_DESTRUCTOR (CLASSTYPE_BASECLASS (type, i));
  1747. }
  1748.  
  1749. /* Given a class type TYPE, and a function decl FNDECL,
  1750.    look for the first function the TYPE's heirarchy which
  1751.    FNDECL could match as a virtual function.
  1752.  
  1753.    DTORP is nonzero if we are looking for a destructor.  Destructors
  1754.    need special treatment because they do not match by name.  */
  1755. tree
  1756. get_first_matching_virtual (type, fndecl, dtorp)
  1757.      tree type, fndecl;
  1758.      int dtorp;
  1759. {
  1760.   tree tmp = NULL_TREE;
  1761.  
  1762.   /* Breadth first search routines start searching basetypes
  1763.      of TYPE, so we must perform first ply of search here.  */
  1764.   if (dtorp)
  1765.     {
  1766.       if (tree_has_any_destructor_p (type, 0))
  1767.     tmp = get_virtual_destructor (type, 0);
  1768.  
  1769.       if (tmp)
  1770.     return tmp;
  1771.  
  1772.       tmp = (tree) breadth_first_search (type,
  1773.                      (pfi) get_virtual_destructor,
  1774.                      tree_has_any_destructor_p);
  1775.       return tmp;
  1776.     }
  1777.   else
  1778.     {
  1779.       tree drettype, dtypes, btypes, instptr_type;
  1780.       tree basetype = TYPE_METHOD_BASETYPE (fndecl);
  1781.       tree baselink, best = NULL_TREE;
  1782.       tree name = DECL_NAME (fndecl);
  1783.  
  1784.       declarator = DECL_ORIGINAL_NAME (fndecl);
  1785.       if (DECL_VIRTUAL_P (declarator) == 0)
  1786.     return 0;
  1787.  
  1788.       drettype = TREE_TYPE (TREE_TYPE (fndecl));
  1789.       dtypes = TYPE_ARG_TYPES (TREE_TYPE (fndecl));
  1790.       if (DECL_STATIC_FUNCTION_P (fndecl))
  1791.     instptr_type = NULL_TREE;
  1792.       else
  1793.     instptr_type = TREE_TYPE (TREE_VALUE (dtypes));
  1794.  
  1795.       for (baselink = get_virtuals_named_this (type, 0);
  1796.        baselink; baselink = next_baselink (baselink))
  1797.     {
  1798.       for (tmp = TREE_VALUE (baselink); tmp; tmp = TREE_CHAIN (tmp))
  1799.         {
  1800.           if (! DECL_VIRTUAL_P (tmp))
  1801.         continue;
  1802.  
  1803.           btypes = TYPE_ARG_TYPES (TREE_TYPE (tmp));
  1804.           if (instptr_type == NULL_TREE
  1805.           && compparms (TREE_CHAIN (btypes), dtypes, 1))
  1806.         /* Caller knows to give error in this case.  */
  1807.         return tmp;
  1808.  
  1809.           if ((TREE_READONLY (TREE_TYPE (TREE_VALUE (btypes)))
  1810.            == TREE_READONLY (instptr_type))
  1811.           && compparms (TREE_CHAIN (btypes), TREE_CHAIN (dtypes), 1))
  1812.         {
  1813.           if (IDENTIFIER_ERROR_LOCUS (name) == NULL_TREE
  1814.               && ! comptypes (TREE_TYPE (TREE_TYPE (tmp)), drettype, 1))
  1815.             {
  1816.               error_with_decl (fndecl, "conflicting return type specified for virtual function `%s'");
  1817.               SET_IDENTIFIER_ERROR_LOCUS (name, basetype);
  1818.             }
  1819.           break;
  1820.         }
  1821.         }
  1822.       if (tmp)
  1823.         {
  1824.           /* If this is ambiguous, we will warn about it later.  */
  1825.           if (best)
  1826.         {
  1827.           if (get_base_distance (TYPE_METHOD_BASETYPE (TREE_TYPE (best)),
  1828.                      TYPE_METHOD_BASETYPE (TREE_TYPE (tmp)), 0, 0) > 0)
  1829.             best = tmp;
  1830.         }
  1831.           else
  1832.         best = tmp;
  1833.         }
  1834.     }
  1835.       if (IDENTIFIER_ERROR_LOCUS (name) == NULL_TREE
  1836.       && best == NULL_TREE && warn_overloaded_virtual)
  1837.     {
  1838.       error_with_decl (fndecl, "conficting specification deriving virtual function `%s'");
  1839.       SET_IDENTIFIER_ERROR_LOCUS (name, basetype);
  1840.     }
  1841.       return best;
  1842.     }
  1843. }
  1844.  
  1845. /* Return the list of virtual functions which are abstract in type TYPE.
  1846.    This information is cached, and so must be built on a
  1847.    non-temporary obstack.  */
  1848. tree
  1849. get_abstract_virtuals (type)
  1850.      tree type;
  1851. {
  1852.   /* For each layer of base class (i.e., the first base class, and each
  1853.      virtual base class from that one), modify the virtual function table
  1854.      of the derived class to contain the new virtual function.
  1855.      A class has as many vfields as it has virtual base classes (total).  */
  1856.   tree vfields, vbases, base, tmp;
  1857.   tree vfield = CLASSTYPE_VFIELD (type);
  1858.   tree fcontext = vfield ? DECL_FCONTEXT (vfield) : NULL_TREE;
  1859.   tree abstract_virtuals = CLASSTYPE_ABSTRACT_VIRTUALS (type);
  1860.  
  1861.   for (vfields = CLASSTYPE_VFIELDS (type); vfields; vfields = TREE_CHAIN (vfields))
  1862.     {
  1863.       int normal;
  1864.  
  1865.       /* Find the right base class for this derived class, call it BASE.  */
  1866.       base = TREE_VALUE (vfields);
  1867.       if (base == type)
  1868.     continue;
  1869.  
  1870.       /* We call this case NORMAL iff this virtual function table
  1871.      pointer field has its storage reserved in this class.
  1872.      This is normally the case without virtual baseclasses
  1873.      or off-center multiple baseclasses.  */
  1874.       normal = (base == fcontext
  1875.         && (TREE_PURPOSE (vfields) == NULL_TREE
  1876.             || ! TREE_VIA_VIRTUAL (TREE_PURPOSE (vfields))));
  1877.  
  1878.       if (normal)
  1879.     tmp = TREE_CHAIN (CLASS_ASSOC_VIRTUALS (type));
  1880.       else
  1881.     {
  1882.       tree assoc = assoc_value (base, type);
  1883.       tmp = TREE_CHAIN (ASSOC_VIRTUALS (assoc));
  1884.     }
  1885.  
  1886.       while (tmp)
  1887.     {
  1888.       tree base_pfn = FNADDR_FROM_VTABLE_ENTRY (TREE_VALUE (tmp));
  1889.       tree base_fndecl = TREE_OPERAND (base_pfn, 0);
  1890.       if (DECL_ABSTRACT_VIRTUAL_P (base_fndecl))
  1891.         abstract_virtuals = tree_cons (NULL_TREE, base_fndecl, abstract_virtuals);
  1892.       tmp = TREE_CHAIN (tmp);
  1893.     }
  1894.     }
  1895.   for (vbases = CLASSTYPE_VBASECLASSES (type); vbases; vbases = TREE_CHAIN (vbases))
  1896.     {
  1897.       if (! ASSOC_VIRTUALS (vbases));
  1898.     continue;
  1899.  
  1900.       base = TREE_TYPE (vbases);
  1901.       tmp = TREE_CHAIN (ASSOC_VIRTUALS (vbases));
  1902.       while (tmp)
  1903.     {
  1904.       tree base_pfn = FNADDR_FROM_VTABLE_ENTRY (TREE_VALUE (tmp));
  1905.       tree base_fndecl = TREE_OPERAND (base_pfn, 0);
  1906.       if (DECL_ABSTRACT_VIRTUAL_P (base_fndecl))
  1907.         abstract_virtuals = tree_cons (NULL_TREE, base_fndecl, abstract_virtuals);
  1908.       tmp = TREE_CHAIN (tmp);
  1909.     }
  1910.     }
  1911.   return nreverse (abstract_virtuals);
  1912. }
  1913.  
  1914. /* For the type TYPE, return a list of member functions available from
  1915.    base classes with name NAME.  The TREE_VALUE of the list is a chain of
  1916.    member functions with name NAME.  The TREE_PURPOSE of the list is a
  1917.    basetype, or a list of base types (in reverse order) which were
  1918.    traversed to reach the chain of member functions.  If we reach a base
  1919.    type which provides a member function of name NAME, and which has at
  1920.    most one base type itself, then we can terminate the search.  */
  1921.  
  1922. tree
  1923. get_baselinks (type, name)
  1924.      tree type, name;
  1925. {
  1926.   tree hash_tree_cons ();
  1927.   int head = 0, tail = 0, index;
  1928.   tree rval = 0, nval = 0;
  1929.   tree basetypes = CLASSTYPE_AS_LIST (type);
  1930.  
  1931.   search_stack = push_search_level (search_stack, &search_obstack);
  1932.  
  1933.   while (1)
  1934.     {
  1935.       int i, n_baselinks = CLASSTYPE_N_BASECLASSES (type);
  1936.  
  1937.       /* Process and/or queue base types.  */
  1938.       for (i = 1; i <= n_baselinks; i++)
  1939.     if (CLASSTYPE_MARKED (CLASSTYPE_BASECLASS (type, i)) == 0)
  1940.       {
  1941.         tree btypes;
  1942.  
  1943.         btypes = hash_tree_cons (CLASSTYPE_VIA_PUBLIC (type, i),
  1944.                      CLASSTYPE_VIA_VIRTUAL (type, i),
  1945.                      NULL_TREE, CLASSTYPE_BASECLASS (type, i),
  1946.                      basetypes);
  1947.         CLASSTYPE_MARKED (CLASSTYPE_BASECLASS (type, i)) = 1;
  1948.         obstack_ptr_grow (&search_obstack, btypes);
  1949.  
  1950.         tail += 1;
  1951.         if (tail >= search_stack->limit)
  1952.           abort ();
  1953.       }
  1954.  
  1955.     dont_queue:
  1956.       /* Process head of queue, if one exists.  */
  1957.       if (head >= tail)
  1958.     break;
  1959.  
  1960.       basetypes = search_stack->first[head++];
  1961.       type = TREE_VALUE (basetypes);
  1962.       index = lookup_fnfields_1 (type, name);
  1963.       if (index >= 0)
  1964.     {
  1965.       nval = TREE_VEC_ELT (CLASSTYPE_METHOD_VEC (type), index);
  1966.       rval = hash_tree_cons (0, 0, basetypes, nval, rval);
  1967.       if (CLASSTYPE_N_BASECLASSES (type) <= 1)
  1968.         {
  1969.           if (CLASSTYPE_BASELINK_VEC (type))
  1970.         TREE_TYPE (rval) = TREE_VEC_ELT (CLASSTYPE_BASELINK_VEC (type), index);
  1971.           goto dont_queue;
  1972.         }
  1973.     }
  1974.       nval = NULL_TREE;
  1975.     }
  1976.   {
  1977.     tree *tp = search_stack->first;
  1978.     tree *search_tail = tp + tail;
  1979.  
  1980.     while (tp < search_tail)
  1981.       {
  1982.     CLASSTYPE_MARKED (TREE_VALUE (*tp++)) = 0;
  1983.       }
  1984.   }
  1985.   search_stack = pop_search_level (search_stack);
  1986.   return rval;
  1987. }
  1988.  
  1989. tree
  1990. next_baselink (baselink)
  1991.      tree baselink;
  1992. {
  1993.   tree tmp = TREE_TYPE (baselink);
  1994.   baselink = TREE_CHAIN (baselink);
  1995.   while (tmp)
  1996.     {
  1997.       /* @@ does not yet add previous base types.  */
  1998.       baselink = tree_cons (TREE_PURPOSE (tmp), TREE_VALUE (tmp),
  1999.                 baselink);
  2000.       TREE_TYPE (baselink) = TREE_TYPE (tmp);
  2001.       tmp = TREE_CHAIN (tmp);
  2002.     }
  2003.   return baselink;
  2004. }
  2005.  
  2006. /* DEPTH-FIRST SEARCH ROUTINES.  */
  2007.  
  2008. /* Assign unique numbers to _CLASSTYPE members of the lattice
  2009.    specified by TYPE.  The root nodes are marked first; the nodes
  2010.    are marked depth-fisrt, left-right.  */
  2011.  
  2012. static int cid;
  2013.  
  2014. /* Matrix implementing a relation from CLASSTYPE X CLASSTYPE => INT.
  2015.    Relation yields 1 if C1 <= C2, 0 otherwise.  */
  2016. typedef char mi_boolean;
  2017. static mi_boolean *mi_matrix;
  2018.  
  2019. /* Type for which this matrix is defined.  */
  2020. static tree mi_type;
  2021.  
  2022. /* Size of the matrix for indexing purposes.  */
  2023. static int mi_size;
  2024.  
  2025. /* Return nonzero if class C2 derives from class C1.  */
  2026. #define DERIVES_FROM(C1, C2)    \
  2027.   ((mi_matrix+mi_size*(CLASSTYPE_CID (C1)-1))[CLASSTYPE_CID (C2)-1])
  2028. #define DERIVES_FROM_STAR(C)    \
  2029.   (mi_matrix+(CLASSTYPE_CID (C)-1))
  2030.  
  2031. /* The main function which implements depth first search.  */
  2032. static void
  2033. dfs_walk (type, fn, qfn)
  2034.      tree type;
  2035.      void (*fn)();
  2036.      int (*qfn)();
  2037. {
  2038.   int i, n_baselinks = CLASSTYPE_N_BASECLASSES (type);
  2039.  
  2040.   for (i = 1; i <= n_baselinks; i++)
  2041.     if ((*qfn)(CLASSTYPE_BASECLASS (type, i)))
  2042.       {
  2043.     dfs_walk (CLASSTYPE_BASECLASS (type, i), fn, qfn);
  2044.       }
  2045.  
  2046.   fn (type);
  2047. }
  2048.  
  2049. /* Predicate functions which serve for dfs_walk.  */
  2050. static int numberedp (type) tree type;
  2051. { return CLASSTYPE_CID (type); }
  2052. static int unnumberedp (type) tree type;
  2053. { return CLASSTYPE_CID (type) == 0; }
  2054.  
  2055. static int markedp (type) tree type;
  2056. { return CLASSTYPE_MARKED (type); }
  2057. static int bfs_markedp (type, i) tree type; int i;
  2058. { return CLASSTYPE_MARKED (CLASSTYPE_BASECLASS (type, i)); }
  2059. static int unmarkedp (type) tree type;
  2060. { return CLASSTYPE_MARKED (type) == 0; }
  2061. static int bfs_unmarkedp (type, i) tree type; int i;
  2062. { return CLASSTYPE_MARKED (CLASSTYPE_BASECLASS (type, i)) == 0; }
  2063. static int marked2p (type) tree type;
  2064. { return CLASSTYPE_MARKED2 (type); }
  2065. static int bfs_marked2p (type, i) tree type; int i;
  2066. { return CLASSTYPE_MARKED2 (CLASSTYPE_BASECLASS (type, i)); }
  2067. static int unmarked2p (type) tree type;
  2068. { return CLASSTYPE_MARKED2 (type) == 0; }
  2069. static int bfs_unmarked2p (type, i) tree type; int i;
  2070. { return CLASSTYPE_MARKED2 (CLASSTYPE_BASECLASS (type, i)) == 0; }
  2071. static int marked3p (type) tree type;
  2072. { return CLASSTYPE_MARKED3 (type); }
  2073. static int bfs_marked3p (type, i) tree type; int i;
  2074. { return CLASSTYPE_MARKED3 (CLASSTYPE_BASECLASS (type, i)); }
  2075. static int unmarked3p (type) tree type;
  2076. { return CLASSTYPE_MARKED3 (type) == 0; }
  2077. static int bfs_unmarked3p (type, i) tree type; int i;
  2078. { return CLASSTYPE_MARKED3 (CLASSTYPE_BASECLASS (type, i)) == 0; }
  2079. static int marked4p (type) tree type;
  2080. { return CLASSTYPE_MARKED4 (type); }
  2081. static int bfs_marked4p (type, i) tree type; int i;
  2082. { return CLASSTYPE_MARKED4 (CLASSTYPE_BASECLASS (type, i)); }
  2083. static int unmarked4p (type) tree type;
  2084. { return CLASSTYPE_MARKED4 (type) == 0; }
  2085. static int bfs_unmarked4p (type, i) tree type; int i;
  2086. { return CLASSTYPE_MARKED4 (CLASSTYPE_BASECLASS (type, i)) == 0; }
  2087.  
  2088. static int dfs_search_slot_nonempty_p (type) tree type;
  2089. { return CLASSTYPE_SEARCH_SLOT (type) != 0; }
  2090.  
  2091.  
  2092. /* The worker functions for `dfs_walk'.  These do not need to
  2093.    test anything (vis a vis marking) if they are paired with
  2094.    a predicate function (above).  */
  2095.  
  2096. /* Assign each type within the lattice a number which is unique
  2097.    in the lattice.  The first number assigned is 1.  */
  2098.  
  2099. static void
  2100. dfs_number (type)
  2101.      tree type;
  2102. {
  2103.   CLASSTYPE_CID (type) = ++cid;
  2104. }
  2105.  
  2106. static void
  2107. dfs_unnumber (type)
  2108.      tree type;
  2109. {
  2110.   CLASSTYPE_CID (type) = 0;
  2111. }
  2112.  
  2113. static void
  2114. dfs_mark (type) tree type;
  2115. { CLASSTYPE_MARKED (type) = 1; }
  2116.  
  2117. static void
  2118. dfs_unmark (type) tree type;
  2119. { CLASSTYPE_MARKED (type) = 0; }
  2120.  
  2121. static void
  2122. dfs_mark2 (type) tree type;
  2123. { CLASSTYPE_MARKED2 (type) = 1; }
  2124.  
  2125. static void
  2126. dfs_unmark2 (type) tree type;
  2127. { CLASSTYPE_MARKED2 (type) = 0; }
  2128.  
  2129. static void
  2130. dfs_mark3 (type) tree type;
  2131. { CLASSTYPE_MARKED3 (type) = 1; }
  2132.  
  2133. static void
  2134. dfs_unmark3 (type) tree type;
  2135. { CLASSTYPE_MARKED3 (type) = 0; }
  2136.  
  2137. static void
  2138. dfs_mark4 (type) tree type;
  2139. { CLASSTYPE_MARKED4 (type) = 1; }
  2140.  
  2141. static void
  2142. dfs_unmark4 (type) tree type;
  2143. { CLASSTYPE_MARKED4 (type) = 0; }
  2144.  
  2145. static void
  2146. dfs_unmark12 (type) tree type;
  2147. { CLASSTYPE_MARKED (type) = 0;
  2148.   CLASSTYPE_MARKED2 (type) = 0; }
  2149.  
  2150. static void
  2151. dfs_unmark34 (type) tree type;
  2152. { CLASSTYPE_MARKED3 (type) = 0;
  2153.   CLASSTYPE_MARKED4 (type) = 0; }
  2154.  
  2155. static tree
  2156. dfs_clear_search_slot (type) tree type;
  2157. { CLASSTYPE_SEARCH_SLOT (type) = 0; }
  2158.  
  2159. static tree vbase_types;
  2160. static tree vbase_decl, vbase_decl_ptr;
  2161. static tree vbase_init_result;
  2162.  
  2163. static void
  2164. dfs_find_vbases (type)
  2165.      tree type;
  2166. {
  2167.   int i, n_baselinks = CLASSTYPE_N_BASECLASSES (type);
  2168.  
  2169.   for (i = n_baselinks; i > 0; i--)
  2170.     if (CLASSTYPE_VIA_VIRTUAL (type, i)
  2171.     && CLASSTYPE_SEARCH_SLOT (CLASSTYPE_BASECLASS (type, i)) == 0)
  2172.       {
  2173.     tree vbase = CLASSTYPE_BASECLASS (type, i);
  2174.     /* ??? ASSOC_VALUE and TREE_VALUE must be the same for this to work.  */
  2175.     tree assoc = value_member (TYPE_MAIN_VARIANT (vbase), vbase_types);
  2176.  
  2177.     CLASSTYPE_SEARCH_SLOT (vbase)
  2178.       = build (PLUS_EXPR, TYPE_POINTER_TO (vbase),
  2179.            vbase_decl_ptr, ASSOC_OFFSET (assoc));
  2180.       }
  2181.   CLASSTYPE_MARKED3 (type) = 1;
  2182.   CLASSTYPE_MARKED4 (type) = 1;
  2183. }
  2184.  
  2185. static void
  2186. dfs_init_vbase_pointers (type)
  2187.      tree type;
  2188. {
  2189.   tree fields = TYPE_FIELDS (type);
  2190.   tree path, this_vbase_ptr;
  2191.   int distance;
  2192.  
  2193.   CLASSTYPE_MARKED3 (type) = 0;
  2194.  
  2195.   if (fields == NULL_TREE
  2196.       || DECL_NAME (fields) == NULL_TREE
  2197.       || ! VBASE_NAME_P (DECL_NAME (fields)))
  2198.     return;
  2199.  
  2200.   distance = get_base_distance (type, TREE_TYPE (vbase_decl), 0, &path);
  2201.   while (path)
  2202.     {
  2203.       if (TREE_VIA_VIRTUAL (path))
  2204.     break;
  2205.       distance -= 1;
  2206.       path = TREE_CHAIN (path);
  2207.     }
  2208.  
  2209.   if (distance > 0)
  2210.     this_vbase_ptr = convert_pointer_to (type, CLASSTYPE_SEARCH_SLOT (TREE_VALUE (path)));
  2211.   else
  2212.     this_vbase_ptr = convert_pointer_to (type, vbase_decl_ptr);
  2213.  
  2214.   while (fields && DECL_NAME (fields)
  2215.      && VBASE_NAME_P (DECL_NAME (fields)))
  2216.     {
  2217.       tree ref = build (COMPONENT_REF, TREE_TYPE (fields),
  2218.             build_indirect_ref (this_vbase_ptr, 0), fields);
  2219.       tree init = CLASSTYPE_SEARCH_SLOT (TREE_TYPE (TREE_TYPE (fields)));
  2220.       vbase_init_result = tree_cons (TREE_TYPE (TREE_TYPE (fields)),
  2221.                      build_modify_expr (ref, NOP_EXPR, init),
  2222.                      vbase_init_result);
  2223.       fields = TREE_CHAIN (fields);
  2224.     }
  2225. }
  2226.  
  2227. /* Sometimes this needs to clear both 3 and 4.  Other times,
  2228.    just 4, but optimizer should make both with equal efficiency
  2229.    (though it does not currently).  */
  2230. static void
  2231. dfs_clear_vbase_slots (type)
  2232.      tree type;
  2233. {
  2234.   CLASSTYPE_SEARCH_SLOT (type) = 0;
  2235.   CLASSTYPE_MARKED3 (type) = 0;
  2236.   CLASSTYPE_MARKED4 (type) = 0;
  2237. }
  2238.  
  2239. tree
  2240. init_vbase_pointers (type, decl_ptr)
  2241.      tree type;
  2242.      tree decl_ptr;
  2243. {
  2244.   if (TYPE_USES_VIRTUAL_BASECLASSES (type))
  2245.     {
  2246.       int old_flag = flag_this_is_variable;
  2247.       flag_this_is_variable = 0;
  2248.       vbase_types = CLASSTYPE_VBASECLASSES (type);
  2249.       vbase_decl_ptr = decl_ptr;
  2250.       vbase_decl = build_indirect_ref (decl_ptr, 0);
  2251.       vbase_init_result = NULL_TREE;
  2252. #ifdef sparc
  2253.       expand_asm_operands (build_string (32, "! start of vbase initialization"), 0, 0, 0, 0, input_filename, lineno);
  2254. #endif
  2255.       dfs_walk (type, dfs_find_vbases, unmarked3p);
  2256.       dfs_walk (type, dfs_init_vbase_pointers, marked3p);
  2257.       dfs_walk (type, dfs_clear_vbase_slots, marked4p);
  2258.       flag_this_is_variable = old_flag;
  2259.       return vbase_init_result;
  2260.     }
  2261.   return 0;
  2262. }
  2263.  
  2264. /* Build a COMPOUND_EXPR which when expanded will generate the code
  2265.    needed to initialize all the virtual function table slots of all
  2266.    the virtual baseclasses.  FOR_TYPE is the type which determines the
  2267.    virtual baseclasses to use; TYPE is the type of the object to which
  2268.    the initialization applies.  TRUE_EXP is the true object we are
  2269.    initializing, and DECL_PTR is the pointer to the sub-object we
  2270.    are initializing.
  2271.  
  2272.    CTOR_P is non-zero if the caller of this function is a top-level
  2273.    constructor.  It is zero when called from a destructor.  When
  2274.    non-zero, we can use computed offsets to store the vtables.  When
  2275.    zero, we must store new vtables through virtual baseclass pointers.  */
  2276.  
  2277. tree
  2278. build_vbase_vtables_init (for_type, type, true_exp, decl_ptr, ctor_p)
  2279.      tree for_type, type;
  2280.      tree true_exp, decl_ptr;
  2281.      int ctor_p;
  2282. {
  2283.   if (TYPE_USES_VIRTUAL_BASECLASSES (type))
  2284.     {
  2285.       int old_flag = flag_this_is_variable;
  2286.       tree vtable_init_result = NULL_TREE;
  2287.       tree vbases = CLASSTYPE_VBASECLASSES (type);
  2288.  
  2289.       vbase_types = CLASSTYPE_VBASECLASSES (for_type);
  2290.       vbase_decl_ptr = true_exp ? build_unary_op (ADDR_EXPR, true_exp, 0) : decl_ptr;
  2291.       vbase_decl = true_exp ? true_exp : build_indirect_ref (decl_ptr, 0);
  2292. #ifdef sparc
  2293.       expand_asm_operands (build_string (32, "! start of vtable initialization"), 0, 0, 0, 0, input_filename, lineno);
  2294. #endif
  2295.       flag_this_is_variable = 0;
  2296.  
  2297.       if (ctor_p)
  2298.     /* This is an object of type IN_TYPE,  */
  2299.     dfs_walk (for_type, dfs_find_vbases, unmarked4p);
  2300.  
  2301.       /* Initialized with vtables of type TYPE.  */
  2302.       while (vbases)
  2303.     {
  2304.       tree basetype = get_base_type (ASSOC_VALUE (vbases), type, 0);
  2305.  
  2306.       /* This time through, not every class's vtable
  2307.          is going to be initialized.  That is, we only initialize
  2308.          the "last" vtable pointer.  */
  2309.  
  2310.       assert (basetype == ASSOC_TYPE (vbases));
  2311.  
  2312.       if (basetype
  2313.           && CLASSTYPE_VSIZE (basetype)
  2314.           && TYPE_MAIN_VARIANT (basetype) == ASSOC_VALUE (vbases))
  2315.         {
  2316.           tree addr;
  2317.           tree vtbl = ASSOC_VTABLE (vbases);
  2318.           tree init = build_unary_op (ADDR_EXPR, vtbl, 0);
  2319.           TREE_USED (vtbl) = 1;
  2320.  
  2321.           if (ctor_p == 0)
  2322.         addr = convert_pointer_to (basetype, vbase_decl_ptr);
  2323.           else
  2324.         addr = CLASSTYPE_SEARCH_SLOT (basetype);
  2325.  
  2326.           if (addr)
  2327.         {
  2328.           tree ref = build_vfield_ref (build_indirect_ref (addr, 0), basetype);
  2329.           init = convert_force (TREE_TYPE (ref), init);
  2330.           vtable_init_result = tree_cons (NULL_TREE, build_modify_expr (ref, NOP_EXPR, init),
  2331.                           vtable_init_result);
  2332.         }
  2333.         }
  2334.       vbases = TREE_CHAIN (vbases);
  2335.     }
  2336.  
  2337.       dfs_walk (type, dfs_clear_vbase_slots, marked4p);
  2338.  
  2339.       flag_this_is_variable = old_flag;
  2340.       if (vtable_init_result)
  2341.     return build_compound_expr (vtable_init_result);
  2342.     }
  2343.   return error_mark_node;
  2344. }
  2345.  
  2346. tree
  2347. clear_search_slots (type)
  2348.      tree type;
  2349. {
  2350.   dfs_walk (type, dfs_clear_search_slot, dfs_search_slot_nonempty_p);
  2351. }
  2352.  
  2353. static void
  2354. dfs_get_vbase_types (type)
  2355.      tree type;
  2356. {
  2357.   int i;
  2358.   tree these_vbase_types = CLASSTYPE_VBASECLASSES (type);
  2359.   tree basetype;
  2360.  
  2361.   if (these_vbase_types)
  2362.     {
  2363.       while (these_vbase_types)
  2364.     {
  2365.       basetype = ASSOC_TYPE (these_vbase_types);
  2366.       if (! CLASSTYPE_MARKED2 (basetype))
  2367.         {
  2368.           vbase_types = make_assoc (integer_zero_node,
  2369.                     basetype,
  2370.                     CLASS_ASSOC_VTABLE (basetype),
  2371.                     CLASS_ASSOC_VIRTUALS (basetype),
  2372.                     vbase_types);
  2373.           CLASSTYPE_MARKED2 (basetype) = 1;
  2374.         }
  2375.       these_vbase_types = TREE_CHAIN (these_vbase_types);
  2376.     }
  2377.     }
  2378.   else for (i = CLASSTYPE_N_BASECLASSES (type); i > 0; i--)
  2379.     {
  2380.       basetype = CLASSTYPE_BASECLASS (type, i);
  2381.       if (CLASSTYPE_VIA_VIRTUAL (type, i) && ! CLASSTYPE_MARKED2 (basetype))
  2382.     {
  2383.       vbase_types = make_assoc (integer_zero_node,
  2384.                     basetype,
  2385.                     CLASS_ASSOC_VTABLE (basetype),
  2386.                     CLASS_ASSOC_VIRTUALS (basetype),
  2387.                     vbase_types);
  2388.       CLASSTYPE_MARKED2 (basetype) = 1;
  2389.     }
  2390.     }
  2391.   CLASSTYPE_MARKED (type) = 1;
  2392. }
  2393.  
  2394. /* Some virtual baseclasses might be virtual baseclasses for
  2395.    other virtual baseclasses.  We sort the virtual baseclasses
  2396.    topologically: in the list returned, the first virtual base
  2397.    classes have no virtual baseclasses themselves, and any entry
  2398.    on the list has no dependency on virtual base classes later in the
  2399.    list.  */
  2400. tree
  2401. get_vbase_types (type)
  2402.      tree type;
  2403. {
  2404.   tree ordered_vbase_types = NULL_TREE, prev, next;
  2405.   tree vbases;
  2406.  
  2407.   vbase_types = NULL_TREE;
  2408.   dfs_walk (type, dfs_get_vbase_types, unmarkedp);
  2409.   dfs_walk (type, dfs_unmark, markedp);
  2410.  
  2411.   while (vbase_types)
  2412.     {
  2413.       /* Now sort these types.  This is essentially a bubble merge.  */
  2414.  
  2415.       /* Farm out virtual baseclasses which have no marked ancestors.  */
  2416.       for (vbases = vbase_types, prev = NULL_TREE;
  2417.        vbases; vbases = next)
  2418.     {
  2419.       next = TREE_CHAIN (vbases);
  2420.       if (! TYPE_USES_VIRTUAL_BASECLASSES (ASSOC_TYPE (vbases))
  2421.           || CLASSTYPE_MARKED2 (ASSOC_TYPE (vbases)) == 0)
  2422.         {
  2423.           if (prev)
  2424.         TREE_CHAIN (prev) = TREE_CHAIN (vbases);
  2425.           else
  2426.         vbase_types = TREE_CHAIN (vbases);
  2427.           TREE_CHAIN (vbases) = NULL_TREE;
  2428.           ordered_vbase_types = chainon (ordered_vbase_types, vbases);
  2429.           CLASSTYPE_MARKED2 (ASSOC_TYPE (vbases)) = 0;
  2430.         }
  2431.       else
  2432.         prev = vbases;
  2433.     }
  2434.  
  2435.       /* Now unmark types all of whose ancestors are now on the
  2436.      `ordered_vbase_types' list.  */
  2437.       for (vbases = vbase_types; vbases; vbases = TREE_CHAIN (vbases))
  2438.     {
  2439.       /* If all our virtual baseclasses are unmarked, ok.  */
  2440.       tree t = CLASSTYPE_VBASECLASSES (ASSOC_VALUE (vbases));
  2441.       while (t && (CLASSTYPE_MARKED2 (ASSOC_TYPE (t)) == 0
  2442.                || !TYPE_USES_VIRTUAL_BASECLASSES (ASSOC_TYPE (t))))
  2443.         t = TREE_CHAIN (t);
  2444.       if (t == NULL_TREE)
  2445.         CLASSTYPE_MARKED2 (ASSOC_TYPE (vbases)) = 0;
  2446.     }
  2447.     }
  2448.  
  2449.   return ordered_vbase_types;
  2450. }
  2451.  
  2452. static void
  2453. dfs_record_inheritance (type)
  2454.      tree type;
  2455. {
  2456.   int i, n_baselinks = CLASSTYPE_N_BASECLASSES (type);
  2457.   mi_boolean *derived_row = DERIVES_FROM_STAR (type);
  2458.  
  2459.   for (i = n_baselinks; i > 0; i--)
  2460.     {
  2461.       int j;
  2462.       tree baseclass = TYPE_MAIN_VARIANT (CLASSTYPE_BASECLASS (type, i));
  2463.       mi_boolean *base_row = DERIVES_FROM_STAR (baseclass);
  2464.  
  2465.       /* Don't search if there's nothing there!  MI_SIZE can be
  2466.      zero as a result of parse errors.  */
  2467.       if (CLASSTYPE_N_BASECLASSES (baseclass) && mi_size > 0)
  2468.     for (j = mi_size*(CLASSTYPE_CID (baseclass)-1); j >= 0; j -= mi_size)
  2469.       derived_row[j] |= base_row[j];
  2470.       DERIVES_FROM (baseclass, type) = 1;
  2471.     }
  2472.  
  2473.   CLASSTYPE_MARKED (type) = 1;
  2474. }
  2475.  
  2476. /* Given a _CLASSTYPE node in a multiple inheritance lattice,
  2477.    convert the lattice into a simple relation such that,
  2478.    given to CIDs, C1 and C2, one can determine if C1 <= C2
  2479.    or C2 <= C1 or C1 <> C2.
  2480.  
  2481.    Once constructed, we walk the lattice depth fisrt,
  2482.    applying various functions to elements as they are encountered.
  2483.  
  2484.    We use malloc here, in case we want to randomly free these tables.  */
  2485.  
  2486. #define SAVE_MI_MATRIX
  2487.  
  2488. void
  2489. build_mi_matrix (type)
  2490.      tree type;
  2491. {
  2492.   cid = 0;
  2493.  
  2494. #ifdef SAVE_MI_MATRIX
  2495.   if (CLASSTYPE_MI_MATRIX (type))
  2496.     {
  2497.       mi_size = CLASSTYPE_N_SUPERCLASSES (type) + CLASSTYPE_N_VBASECLASSES (type);
  2498.       mi_matrix = CLASSTYPE_MI_MATRIX (type);
  2499.       mi_type = type;
  2500.       dfs_walk (type, dfs_number, unnumberedp);
  2501.       return;
  2502.     }
  2503. #endif
  2504.  
  2505.   mi_size = CLASSTYPE_N_SUPERCLASSES (type) + CLASSTYPE_N_VBASECLASSES (type);
  2506.   mi_matrix = (char *)malloc ((mi_size+1) * (mi_size+1));
  2507.   mi_type = type;
  2508.   bzero (mi_matrix, mi_size * mi_size);
  2509.   dfs_walk (type, dfs_number, unnumberedp);
  2510.   dfs_walk (type, dfs_record_inheritance, unmarkedp);
  2511.   dfs_walk (type, dfs_unmark, markedp);
  2512. }
  2513.  
  2514. void
  2515. free_mi_matrix ()
  2516. {
  2517.   dfs_walk (mi_type, dfs_unnumber, numberedp);
  2518.  
  2519. #ifdef SAVE_MI_MATRIX
  2520.   CLASSTYPE_MI_MATRIX (mi_type) = mi_matrix;
  2521. #else
  2522.   free (mi_matrix);
  2523.   mi_size = 0;
  2524.   cid = 0;
  2525. #endif
  2526. }
  2527.  
  2528. /* Local variables for detecting ambiguities of virtual functions
  2529.    when two or more classes are joined at a multiple inheritance
  2530.    seam.  */
  2531. typedef tree mi_ventry[3];
  2532. static mi_ventry *mi_vmatrix;
  2533. static int *mi_vmax;
  2534. static int mi_vrows, mi_vcols;
  2535. #define MI_VMATRIX(ROW,COL) ((mi_vmatrix + (ROW)*mi_vcols)[COL])
  2536.  
  2537. /* Build a table of virtual functions for a multiple-inheritance
  2538.    structure.  Here, there are N base classes, and at most
  2539.    M entries per class.
  2540.  
  2541.    This function does nothing if N is 0 or 1.  */
  2542. void
  2543. build_mi_virtuals (rows, cols)
  2544.      int rows, cols;
  2545. {
  2546.   if (rows < 2)
  2547.     return;
  2548.   mi_vrows = rows;
  2549.   mi_vcols = cols;
  2550.   mi_vmatrix = (mi_ventry *)malloc ((rows+1) * cols * sizeof (mi_ventry));
  2551.   mi_vmax = (int *)malloc ((rows+1) * sizeof (int));
  2552.  
  2553.   bzero (mi_vmax, rows * sizeof (int));
  2554.  
  2555.   /* Row indicies start at 1, so adjust this.  */
  2556.   mi_vmatrix -= cols;
  2557.   mi_vmax -= 1;
  2558. }
  2559.  
  2560. /* Comparison function for ordering virtual function table entries.  */
  2561. static int
  2562. rank_mi_virtuals (v1, v2)
  2563.      mi_ventry *v1, *v2;
  2564. {
  2565.   tree p1, p2;
  2566.   int i;
  2567.  
  2568.   i = (TREE_UID (DECL_ORIGINAL_NAME ((*v1)[0]))
  2569.        - TREE_UID (DECL_ORIGINAL_NAME ((*v2)[0])));
  2570.   if (i)
  2571.     return i;
  2572.   p1 = (*v1)[1];
  2573.   p2 = (*v2)[1];
  2574.  
  2575.   if (p1 == p2)
  2576.     return 0;
  2577.  
  2578.   while (p1 && p2)
  2579.     {
  2580.       i = (TREE_UID (TREE_VALUE (p1))
  2581.        - TREE_UID (TREE_VALUE (p2)));
  2582.       if (i)
  2583.     return i;
  2584.  
  2585.       if (TREE_CHAIN (p1))
  2586.     {
  2587.       if (! TREE_CHAIN (p2))
  2588.         return 1;
  2589.       p1 = TREE_CHAIN (p1);
  2590.       p2 = TREE_CHAIN (p2);
  2591.     }
  2592.       else if (TREE_CHAIN (p2))
  2593.     return -1;
  2594.       else
  2595.     {
  2596.       /* When matches of argument lists occur, pick lowest
  2597.          TREE_UID to keep searching time to a minimum on
  2598.          later passes--like hashing, only different.
  2599.          *MUST BE STABLE*.  */
  2600.       if (TREE_UID ((*v2)[1]) < TREE_UID ((*v1)[1]))
  2601.         (*v1)[1] = (*v2)[1];
  2602.       else
  2603.         (*v2)[1] = (*v1)[1];
  2604.       return 0;
  2605.     }
  2606.     }
  2607.   return 0;
  2608. }
  2609.  
  2610. /* Install the virtuals functions got from the initializer VIRTUALS to
  2611.    the table at index ROW.  */
  2612. void
  2613. add_mi_virtuals (row, virtuals)
  2614.      int row;
  2615.      tree virtuals;
  2616. {
  2617.   int col = 0;
  2618.  
  2619.   if (mi_vmatrix == 0)
  2620.     return;
  2621.   while (virtuals)
  2622.     {
  2623.       tree decl = TREE_OPERAND (FNADDR_FROM_VTABLE_ENTRY (TREE_VALUE (virtuals)), 0);
  2624.       MI_VMATRIX (row, col)[0] = decl;
  2625.       MI_VMATRIX (row, col)[1] = TREE_CHAIN (TYPE_ARG_TYPES (TREE_TYPE (decl)));
  2626.       MI_VMATRIX (row, col)[2] = TREE_VALUE (virtuals);
  2627.       virtuals = TREE_CHAIN (virtuals);
  2628.       col += 1;
  2629.     }
  2630.   mi_vmax[row] = col;
  2631.  
  2632.   qsort (mi_vmatrix + row * mi_vcols,
  2633.      col,
  2634.      sizeof (mi_ventry),
  2635.      rank_mi_virtuals);
  2636. }
  2637.  
  2638. /* If joining two types results in an ambiguity in the virtual
  2639.    function table, report such here.  */
  2640. void
  2641. report_ambiguous_mi_virtuals (rows, type)
  2642.      int rows;
  2643.      tree type;
  2644. {
  2645.   int *mi_vmin;
  2646.   int row1, col1, row, col;
  2647.  
  2648.   if (mi_vmatrix == 0)
  2649.     return;
  2650.  
  2651.   /* Now virtuals are all sorted, so we merge to find ambiguous cases.  */
  2652.   mi_vmin = (int *)alloca ((rows+1) * sizeof (int));
  2653.   bzero (mi_vmin, rows * sizeof (int));
  2654.  
  2655.   /* adjust.  */
  2656.   mi_vmin -= 1;
  2657.  
  2658.   /* For each base class with virtual functions (and this includes views
  2659.      of the virtual baseclasses from different base classes), see that
  2660.      each virtual function in that base class has a unique meet.
  2661.  
  2662.      When the column loop is finished, THIS_DECL is in fact the meet.
  2663.      If that value does not appear in the virtual function table for
  2664.      the row, install it.  This happens when that virtual function comes
  2665.      from a virtual baseclass, or a non-leftmost baseclass.  */
  2666.      
  2667.   for (row1 = 1; row1 < rows; row1++)
  2668.     {
  2669.       tree this_decl = 0;
  2670.  
  2671.       for (col1 = mi_vmax[row1]-1; col1 >= mi_vmin[row1]; col1--)
  2672.     {
  2673.       tree these_args = MI_VMATRIX (row1, col1)[1];
  2674.       tree this_context;
  2675.  
  2676.       this_decl = MI_VMATRIX (row1, col1)[0];
  2677.       if (this_decl == 0)
  2678.         continue;
  2679.       this_context = DECL_CONTEXT (this_decl);
  2680.  
  2681.       if (this_context != type)
  2682.         this_context = get_base_type (this_context, type, 0);
  2683.  
  2684.       for (row = row1+1; row <= rows; row++)
  2685.         for (col = mi_vmax[row]-1; col >= mi_vmin[row]; col--)
  2686.           {
  2687.         mi_ventry this_entry;
  2688.  
  2689.         if (MI_VMATRIX (row, col)[0] == 0)
  2690.           continue;
  2691.  
  2692.         this_entry[0] = this_decl;
  2693.         this_entry[1] = these_args;
  2694.         this_entry[2] = MI_VMATRIX (row1, col1)[2];
  2695.         if (rank_mi_virtuals (&this_entry,
  2696.                       &MI_VMATRIX (row, col)) == 0)
  2697.           {
  2698.             /* They are equal.  There are four possibilities:
  2699.                
  2700.                (1) Derived class is defining this virtual function.
  2701.                (2) Two paths to the same virtual function in the
  2702.                same base class.
  2703.                (3) A path to a virtual function declared in one base
  2704.                class, and another path to a virtual function in a
  2705.                base class of the base class.
  2706.                (4) Two paths to the same virtual function in different
  2707.                base classes.
  2708.                
  2709.                The first three cases are ok (non-ambiguous).  */
  2710.  
  2711.             tree that_context, tmp;
  2712.             int this_before_that;
  2713.  
  2714.             if (type == this_context)
  2715.               /* case 1.  */
  2716.               goto ok;
  2717.             that_context = get_base_type (DECL_CONTEXT (MI_VMATRIX (row, col)[0]), type, 0);
  2718.             if (that_context == this_context)
  2719.               /* case 2.  */
  2720.               goto ok;
  2721.             if (that_context != NULL_TREE)
  2722.               {
  2723.             tmp = get_base_type (that_context, this_context, 0);
  2724.             this_before_that = (that_context != tmp);
  2725.             if (this_before_that == 0)
  2726.               /* case 3a.  */
  2727.               goto ok;
  2728.             tmp = get_base_type (this_context, that_context, 0);
  2729.             this_before_that = (this_context == tmp);
  2730.             if (this_before_that != 0)
  2731.               /* case 3b.  */
  2732.               goto ok;
  2733.  
  2734.             /* case 4.  */
  2735.             error_with_decl (MI_VMATRIX (row, col)[0], "ambiguous virtual function `%s'");
  2736.             error_with_decl (this_decl, "ambiguating function `%s' (joined by type `%s')", IDENTIFIER_POINTER (current_class_name));
  2737.               }
  2738.           ok:
  2739.             MI_VMATRIX (row, col)[0] = 0;
  2740.  
  2741.             /* Let zeros propagate.  */
  2742.             if (col == mi_vmax[row]-1)
  2743.               {
  2744.             int i = col;
  2745.             while (i >= mi_vmin[row]
  2746.                    && MI_VMATRIX (row, i)[0] == 0)
  2747.               i--;
  2748.             mi_vmax[row] = i;
  2749.               }
  2750.             else if (col == mi_vmin[row])
  2751.               {
  2752.             int i = col;
  2753.             while (i < mi_vmax[row]
  2754.                    && MI_VMATRIX (row, i)[0] == 0)
  2755.               i++;
  2756.             mi_vmin[row] = i;
  2757.               }
  2758.           }
  2759.           }
  2760.     }
  2761.     }
  2762.   free (mi_vmatrix + mi_vcols);
  2763.   mi_vmatrix = 0;
  2764.   free (mi_vmax + 1);
  2765.   mi_vmax = 0;
  2766. }
  2767.  
  2768. /* Subroutines of push_class_decls ().  */
  2769.  
  2770. /* Add the instance variables which this class contributed to the
  2771.    current class binding contour.  When a redefinition occurs,
  2772.    if the redefinition is strictly within a single inheritance path,
  2773.    we just overwrite (in the case of a data field) or
  2774.    cons (in the case of a member function) the old declaration with
  2775.    the new.  If the fields are not within a single inheritance path,
  2776.    we must cons them in either case.  */
  2777.  
  2778. static void
  2779. dfs_pushdecls (type)
  2780.      tree type;
  2781. {
  2782.   tree fields, *methods, *end;
  2783.   tree method_vec;
  2784.  
  2785.   for (fields = TYPE_FIELDS (type); fields; fields = TREE_CHAIN (fields))
  2786.     {
  2787.       /* Unmark so that if we are in a constructor, and then find that
  2788.      this field was initialized by a base initializer,
  2789.      we can emit an error message.  */
  2790.       if (TREE_CODE (fields) == FIELD_DECL)
  2791.     TREE_USED (fields) = 0;
  2792.  
  2793.       if (DECL_ANON_UNION_ELEM (fields))
  2794.     {
  2795.       dfs_pushdecls (TREE_TYPE (fields));
  2796.       continue;
  2797.     }
  2798.       if (TREE_CODE (fields) != TYPE_DECL)
  2799.     {
  2800.       TREE_FIELD_PUBLIC (fields) = 0;
  2801.       TREE_FIELD_PROTECTED (fields) = 0;
  2802.       TREE_FIELD_PRIVATE (fields) = 0;
  2803.     }
  2804.  
  2805.       if (DECL_NAME (fields))
  2806.     {
  2807.       tree value = IDENTIFIER_CLASS_VALUE (DECL_NAME (fields));
  2808.       if (value)
  2809.         {
  2810.           /* Possible ambiguity.  If its defining type(s)
  2811.          is (are all) derived from us, no problem.  */
  2812.  
  2813.           if (TREE_CODE (value) != TREE_LIST)
  2814.         {
  2815.           if (DECL_FIELD_CONTEXT (value) == type
  2816.               || DERIVES_FROM (DECL_FIELD_CONTEXT (value), type))
  2817.             value = fields;
  2818.           else
  2819.             value = tree_cons (NULL_TREE, fields,
  2820.                        build_tree_list (NULL_TREE, value));
  2821.         }
  2822.           else
  2823.         {
  2824.           /* All children may derive from us, in which case
  2825.              there is no problem.  Otherwise, we have to
  2826.              keep lists around of what the ambiguities might be.  */
  2827.           tree values;
  2828.           int problem = 0;
  2829.  
  2830.           for (values = value; values; values = TREE_CHAIN (values))
  2831.             {
  2832.               tree sub_values = TREE_VALUE (values);
  2833.               if (TREE_CODE (sub_values) == TREE_LIST)
  2834.             {
  2835.               for (; sub_values; sub_values = TREE_CHAIN (sub_values))
  2836.                 if (! DERIVES_FROM (DECL_FIELD_CONTEXT (TREE_VALUE (sub_values)), type))
  2837.                   {
  2838.                 value = tree_cons (NULL_TREE, TREE_VALUE (values), value);
  2839.                 problem = 1;
  2840.                 break;
  2841.                   }
  2842.             }
  2843.               else
  2844.             {
  2845.               if (! DERIVES_FROM (DECL_FIELD_CONTEXT (sub_values), type))
  2846.                 {
  2847.                   value = tree_cons (NULL_TREE, values, value);
  2848.                   problem = 1;
  2849.                   break;
  2850.                 }
  2851.             }
  2852.             }
  2853.           if (! problem) value = fields;
  2854.         }
  2855.  
  2856.           /* Mark this as a potentially ambiguous member.  */
  2857.           if (TREE_CODE (value) == TREE_LIST)
  2858.         {
  2859.           /* Leaving TREE_TYPE blank is intentional.
  2860.              We cannot use `error_mark_node' (lookup_name)
  2861.              or `unknown_type_node' (all member functions use this).  */
  2862.           TREE_NONLOCAL (value) = 1;
  2863.         }
  2864.  
  2865.           IDENTIFIER_CLASS_VALUE (DECL_NAME (fields)) = value;
  2866.         }
  2867.       else IDENTIFIER_CLASS_VALUE (DECL_NAME (fields)) = fields;
  2868.     }
  2869.     }
  2870.  
  2871.   method_vec = CLASSTYPE_METHOD_VEC (type);
  2872.   if (method_vec != 0)
  2873.     {
  2874.       /* Farm out constructors and destructors.  */
  2875.       methods = &TREE_VEC_ELT (method_vec, 1);
  2876.       end = TREE_VEC_END (method_vec);
  2877.  
  2878.       /* This does not work for multiple inheritance yet.  */
  2879.       while (methods != end)
  2880.     {
  2881.       /* This will cause lookup_name to return a pointer
  2882.          to the tree_list of possible methods of this name.
  2883.          If the order is a problem, we can nreverse them.  */
  2884.       tree tmp;
  2885.       tree old = IDENTIFIER_CLASS_VALUE (DECL_ORIGINAL_NAME (*methods));
  2886.  
  2887.       if (old && TREE_CODE (old) == TREE_LIST)
  2888.         tmp = tree_cons (DECL_ORIGINAL_NAME (*methods), *methods, old);
  2889.       else
  2890.         {
  2891.           /* Only complain if we shadow something we can access.  */
  2892.           if (old && (DECL_CONTEXT (old) == current_class_type
  2893.               || ! TREE_PRIVATE (old)))
  2894.         /* Should figure out visibility more accurately.  */
  2895.         warning ("shadowing member `%s' with member function",
  2896.              IDENTIFIER_POINTER (DECL_ORIGINAL_NAME (*methods)));
  2897.           tmp = build_tree_list (DECL_ORIGINAL_NAME (*methods), *methods);
  2898.         }
  2899.  
  2900.       TREE_TYPE (tmp) = unknown_type_node;
  2901. #if 0
  2902.       TREE_OVERLOADED (tmp) = DECL_OVERLOADED (*methods);
  2903. #endif
  2904.       TREE_NONLOCAL (tmp) = 1;
  2905.       IDENTIFIER_CLASS_VALUE (DECL_ORIGINAL_NAME (*methods)) = tmp;
  2906.  
  2907.       tmp = *methods;
  2908.       while (tmp != 0)
  2909.         {
  2910.           TREE_FIELD_PUBLIC (tmp) = 0;
  2911.           TREE_FIELD_PROTECTED (tmp) = 0;
  2912.           TREE_FIELD_PRIVATE (tmp) = 0;
  2913.           tmp = TREE_CHAIN (tmp);
  2914.         }
  2915.  
  2916.       methods++;
  2917.     }
  2918.     }
  2919.   CLASSTYPE_MARKED (type) = 1;
  2920. }
  2921.  
  2922. /* Consolidate unique (by name) member functions.  */
  2923. static void
  2924. dfs_compress_decls (type)
  2925.      tree type;
  2926. {
  2927.   tree method_vec = CLASSTYPE_METHOD_VEC (type);
  2928.  
  2929.   if (method_vec != 0)
  2930.     {
  2931.       /* Farm out constructors and destructors.  */
  2932.       tree *methods = &TREE_VEC_ELT (method_vec, 1);
  2933.       tree *end = TREE_VEC_END (method_vec);
  2934.  
  2935.       for (; methods != end; methods++)
  2936.     {
  2937.       tree tmp = IDENTIFIER_CLASS_VALUE (DECL_ORIGINAL_NAME (*methods));
  2938.  
  2939.       /* This was replaced in scope by somebody else.  Just leave it
  2940.          alone.  */
  2941.       if (TREE_CODE (tmp) != TREE_LIST)
  2942.         continue;
  2943.  
  2944.       if (TREE_CHAIN (tmp) == NULL_TREE
  2945.           && TREE_VALUE (tmp)
  2946.           && TREE_CHAIN (TREE_VALUE (tmp)) == NULL_TREE)
  2947.         {
  2948.           IDENTIFIER_CLASS_VALUE (DECL_ORIGINAL_NAME (*methods))
  2949.         = TREE_VALUE (tmp);
  2950.         }
  2951.     }
  2952.     }
  2953.   CLASSTYPE_MARKED (type) = 0;
  2954. }
  2955.  
  2956. /* When entering the scope of a class, we cache all of the
  2957.    fields that that class provides within its inheritance
  2958.    lattice.  Where ambiguities result, we mark them
  2959.    with `error_mark_node' so that if they are encountered
  2960.    without explicit qualification, we can emit an error
  2961.    message.  */
  2962. void
  2963. push_class_decls (type)
  2964.      tree type;
  2965. {
  2966.   struct obstack *ambient_obstack = current_obstack;
  2967.  
  2968. #if 0
  2969.   tree tags = CLASSTYPE_TAGS (type);
  2970.  
  2971.   while (tags)
  2972.     {
  2973.       tree code_type_node;
  2974.       tree tag;
  2975.  
  2976.       switch (TREE_CODE (TREE_VALUE (tags)))
  2977.     {
  2978.     case ENUMERAL_TYPE:
  2979.       code_type_node = enum_type_node;
  2980.       break;
  2981.     case RECORD_TYPE:
  2982.       code_type_node = record_type_node;
  2983.       break;
  2984.     case CLASS_TYPE:
  2985.       code_type_node = class_type_node;
  2986.       break;
  2987.     case UNION_TYPE:
  2988.       code_type_node = union_type_node;
  2989.       break;
  2990.     default:
  2991.       assert (0);
  2992.     }
  2993.       tag = xref_tag (code_type_node, TREE_PURPOSE (tags),
  2994.               CLASSTYPE_BASECLASS (TREE_VALUE (tags), 1));
  2995.       pushdecl (build_decl (TYPE_DECL, TREE_PURPOSE (tags), TREE_VALUE (tags)));
  2996.     }
  2997. #endif
  2998.  
  2999.   current_obstack = &bridge_obstack;
  3000.   search_stack = push_search_level (search_stack, &bridge_obstack);
  3001.  
  3002.   /* Push class fields into CLASS_VALUE scope, and mark.  */
  3003.   dfs_walk (type, dfs_pushdecls, unmarkedp);
  3004.  
  3005.   /* Compress fields which have only a single entry
  3006.      by a given name, and unmark.  */
  3007.   dfs_walk (type, dfs_compress_decls, markedp);
  3008.   current_obstack = ambient_obstack;
  3009. }
  3010.  
  3011. static void
  3012. dfs_popdecls (type)
  3013.      tree type;
  3014. {
  3015.   tree fields = TYPE_FIELDS (type);
  3016.   tree method_vec = CLASSTYPE_METHOD_VEC (type);
  3017.  
  3018.   while (fields)
  3019.     {
  3020.       if (DECL_ANON_UNION_ELEM (fields))
  3021.     {
  3022.       dfs_popdecls (TREE_TYPE (fields));
  3023.     }
  3024.       else if (DECL_NAME (fields))
  3025.     IDENTIFIER_CLASS_VALUE (DECL_NAME (fields)) = NULL_TREE;
  3026.       fields = TREE_CHAIN (fields);
  3027.     }
  3028.   if (method_vec != 0)
  3029.     {
  3030.       tree *methods = &TREE_VEC_ELT (method_vec, 0);
  3031.       tree *end = TREE_VEC_END (method_vec);
  3032.  
  3033.       if (*methods == 0)
  3034.     methods += 1;
  3035.  
  3036.       for (; methods != end; methods++)
  3037.     IDENTIFIER_CLASS_VALUE (DECL_ORIGINAL_NAME (*methods)) = NULL_TREE;
  3038.     }
  3039.  
  3040.   CLASSTYPE_MARKED (type) = 1;
  3041. }
  3042.  
  3043. void
  3044. pop_class_decls (type)
  3045.      tree type;
  3046. {
  3047.   /* Clear out the IDENTIFIER_CLASS_VALUE which this
  3048.      class may have occupied, and mark.  */
  3049.   dfs_walk (type, dfs_popdecls, unmarkedp);
  3050.  
  3051.   /* Unmark.  */
  3052.   dfs_walk (type, dfs_unmark, markedp);
  3053.   search_stack = pop_search_level (search_stack);
  3054. }
  3055.  
  3056. /* Given a base type PARENT, and a derived type TYPE, build
  3057.    a name which distinguishes exactly the PARENT member of TYPE's type.
  3058.  
  3059.    FORMAT is a string which controls how sprintf formats the name
  3060.    we have generated.
  3061.  
  3062.    For example, given
  3063.  
  3064.     class A; class B; class C : A, B;
  3065.  
  3066.    it is possible to distinguish "A" from "C's A".  And given
  3067.  
  3068.     class L;
  3069.     class A : L; class B : L; class C : A, B;
  3070.  
  3071.    it is possible to distinguish "L" from "A's L", and also from
  3072.    "C's L from A".  */
  3073. tree
  3074. build_type_pathname (format, parent, type)
  3075.      char *format;
  3076.      tree parent, type;
  3077. {
  3078.   extern struct obstack temporary_obstack;
  3079.   char *first, *base, *name;
  3080.   int i;
  3081.   tree id;
  3082.  
  3083.   parent = TYPE_MAIN_VARIANT (parent);
  3084.  
  3085.   /* Remember where to cut the obstack to.  */
  3086.   first = obstack_base (&temporary_obstack);
  3087.  
  3088.   /* Put on TYPE+PARENT.  */
  3089.   obstack_grow (&temporary_obstack,
  3090.         TYPE_NAME_STRING (type), TYPE_NAME_LENGTH (type));
  3091.   obstack_1grow (&temporary_obstack, JOINER);
  3092.   obstack_grow0 (&temporary_obstack,
  3093.          TYPE_NAME_STRING (parent), TYPE_NAME_LENGTH (parent));
  3094.   i = obstack_object_size (&temporary_obstack);
  3095.   base = obstack_base (&temporary_obstack);
  3096.   obstack_finish (&temporary_obstack);
  3097.  
  3098.   /* Put on FORMAT+TYPE+PARENT.  */
  3099.   obstack_blank (&temporary_obstack, strlen (format) + i + 1);
  3100.   name = obstack_base (&temporary_obstack);
  3101.   sprintf (name, format, base);
  3102.   id = get_identifier (name);
  3103.   obstack_free (&temporary_obstack, first);
  3104.  
  3105.   return id;
  3106. }
  3107.  
  3108. static int
  3109. bfs_unmark_finished_struct (type, i)
  3110.      tree type;
  3111.      int i;
  3112. {
  3113.   type = i == 0 ? type : CLASSTYPE_BASECLASS (type, i);
  3114.   if (CLASSTYPE_MARKED4 (type))
  3115.     {
  3116.       tree assoc, decl, context;
  3117.  
  3118.       if (type == current_class_type)
  3119.     assoc = CLASSTYPE_ASSOC (type);
  3120.       else if (TREE_VIA_VIRTUAL (type))
  3121.     assoc = value_member (TYPE_MAIN_VARIANT (type), CLASSTYPE_VBASECLASSES (current_class_type));
  3122.       else
  3123.     assoc = assoc_value (TYPE_MAIN_VARIANT (type), current_class_type);
  3124.       decl = ASSOC_VTABLE (assoc);
  3125.       context = DECL_CONTEXT (decl);
  3126.       DECL_CONTEXT (decl) = 0;
  3127.       if (write_virtuals >= 0
  3128.       && DECL_INITIAL (decl) != ASSOC_VIRTUALS (assoc))
  3129.     DECL_INITIAL (decl) = build_nt (CONSTRUCTOR, NULL_TREE,
  3130.                     ASSOC_VIRTUALS (assoc));
  3131.       finish_decl (decl, DECL_INITIAL (decl), NULL_TREE);
  3132.       DECL_CONTEXT (decl) = context;
  3133.     }
  3134.   CLASSTYPE_MARKED3 (type) = 0;
  3135.   CLASSTYPE_MARKED4 (type) = 0;
  3136.   return 0;
  3137. }
  3138.  
  3139. void
  3140. unmark_finished_struct (type)
  3141.      tree type;
  3142. {
  3143.   bfs_unmark_finished_struct (type, 0);
  3144.   breadth_first_search (type, bfs_unmark_finished_struct, bfs_marked3p);
  3145. }
  3146.  
  3147. void
  3148. print_search_statistics ()
  3149. {
  3150. #ifdef GATHER_STATISTICS
  3151.   if (flag_memoize_lookups)
  3152.     {
  3153.       fprintf (stderr, "%d memoized contexts saved\n",
  3154.            n_contexts_saved);
  3155.       fprintf (stderr, "%d local tree nodes made\n", my_tree_node_counter);
  3156.       fprintf (stderr, "%d local hash nodes made\n", my_memoized_entry_counter);
  3157.       fprintf (stderr, "fields statistics:\n");
  3158.       fprintf (stderr, "  memoized finds = %d; rejects = %d; (searches = %d)\n",
  3159.            memoized_fast_finds[0], memoized_fast_rejects[0],
  3160.            memoized_fields_searched[0]);
  3161.       fprintf (stderr, "  memoized_adds = %d\n", memoized_adds[0]);
  3162.       fprintf (stderr, "fnfields statistics:\n");
  3163.       fprintf (stderr, "  memoized finds = %d; rejects = %d; (searches = %d)\n",
  3164.            memoized_fast_finds[1], memoized_fast_rejects[1],
  3165.            memoized_fields_searched[1]);
  3166.       fprintf (stderr, "  memoized_adds = %d\n", memoized_adds[1]);
  3167.     }
  3168.   fprintf (stderr, "%d fields searched in %d[%d] calls to lookup_field[_1]\n",
  3169.        n_fields_searched, n_calls_lookup_field, n_calls_lookup_field_1);
  3170.   fprintf (stderr, "%d fnfields searched in %d calls to lookup_fnfields\n",
  3171.        n_outer_fields_searched, n_calls_lookup_fnfields);
  3172.   fprintf (stderr, "%d calls to get_base_type\n", n_calls_get_base_type);
  3173. #else
  3174.   fprintf (stderr, "no search statistics\n");
  3175. #endif
  3176. }
  3177.  
  3178. void
  3179. init_search_processing ()
  3180. {
  3181.   obstack_init (&search_obstack);
  3182.   obstack_init (&type_obstack);
  3183.   obstack_init (&type_obstack_entries);
  3184.   obstack_init (&bridge_obstack);
  3185.  
  3186.   /* This gives us room to build our chains of basetypes,
  3187.      whether or not we decide to memoize them.  */
  3188.   type_stack = push_type_level (0, &type_obstack);
  3189.   _vptr_name = get_identifier ("_vptr");
  3190. }
  3191.  
  3192. tree
  3193. get_wrapper (type)
  3194.      tree type;
  3195. {
  3196.   tree wrap_type;
  3197.   char *name;
  3198.   assert (IS_AGGR_TYPE (type));
  3199.   wrap_type = TYPE_WRAP_TYPE (type);
  3200.   name = (char *)alloca (TYPE_NAME_LENGTH (wrap_type)
  3201.              + strlen (WRAPPER_NAME_FORMAT));
  3202.   sprintf (name, WRAPPER_NAME_FORMAT, TYPE_NAME_STRING (wrap_type));
  3203.   return lookup_fnfields (CLASSTYPE_AS_LIST (wrap_type),
  3204.               get_identifier (name), 0);
  3205. }
  3206.  
  3207. void
  3208. reinit_search_statistics ()
  3209. {
  3210.   my_memoized_entry_counter = 0;
  3211.   memoized_fast_finds[0] = 0;
  3212.   memoized_fast_finds[1] = 0;
  3213.   memoized_adds[0] = 0;
  3214.   memoized_adds[1] = 0;
  3215.   memoized_fast_rejects[0] = 0;
  3216.   memoized_fast_rejects[1] = 0;
  3217.   memoized_fields_searched[0] = 0;
  3218.   memoized_fields_searched[1] = 0;
  3219.   n_fields_searched = 0;
  3220.   n_calls_lookup_field = 0, n_calls_lookup_field_1 = 0;
  3221.   n_calls_lookup_fnfields = 0, n_calls_lookup_fnfields_1 = 0;
  3222.   n_calls_get_base_type = 0;
  3223.   n_outer_fields_searched = 0;
  3224.   n_contexts_saved = 0;
  3225. }
  3226.