gdb/dictionary.c - gdb

Global variables defined

Data types defined

Functions defined

Macros defined

Source code

  1. /* Routines for name->symbol lookups in GDB.

  2.    Copyright (C) 2003-2015 Free Software Foundation, Inc.

  3.    Contributed by David Carlton <carlton@bactrian.org> and by Kealia,
  4.    Inc.

  5.    This file is part of GDB.

  6.    This program is free software; you can redistribute it and/or modify
  7.    it under the terms of the GNU General Public License as published by
  8.    the Free Software Foundation; either version 3 of the License, or
  9.    (at your option) any later version.

  10.    This program is distributed in the hope that it will be useful,
  11.    but WITHOUT ANY WARRANTY; without even the implied warranty of
  12.    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  13.    GNU General Public License for more details.

  14.    You should have received a copy of the GNU General Public License
  15.    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */

  16. #include "defs.h"
  17. #include <ctype.h>
  18. #include "gdb_obstack.h"
  19. #include "symtab.h"
  20. #include "buildsym.h"
  21. #include "dictionary.h"

  22. /* This file implements dictionaries, which are tables that associate
  23.    symbols to names.  They are represented by an opaque type 'struct
  24.    dictionary'.  That type has various internal implementations, which
  25.    you can choose between depending on what properties you need
  26.    (e.g. fast lookup, order-preserving, expandable).

  27.    Each dictionary starts with a 'virtual function table' that
  28.    contains the functions that actually implement the various
  29.    operations that dictionaries provide.  (Note, however, that, for
  30.    the sake of client code, we also provide some functions that can be
  31.    implemented generically in terms of the functions in the vtable.)

  32.    To add a new dictionary implementation <impl>, what you should do
  33.    is:

  34.    * Add a new element DICT_<IMPL> to dict_type.

  35.    * Create a new structure dictionary_<impl>.  If your new
  36.    implementation is a variant of an existing one, make sure that
  37.    their structs have the same initial data members.  Define accessor
  38.    macros for your new data members.

  39.    * Implement all the functions in dict_vector as static functions,
  40.    whose name is the same as the corresponding member of dict_vector
  41.    plus _<impl>.  You don't have to do this for those members where
  42.    you can reuse existing generic functions
  43.    (e.g. add_symbol_nonexpandable, free_obstack) or in the case where
  44.    your new implementation is a variant of an existing implementation
  45.    and where the variant doesn't affect the member function in
  46.    question.

  47.    * Define a static const struct dict_vector dict_<impl>_vector.

  48.    * Define a function dict_create_<impl> to create these
  49.    gizmos.  Add its declaration to dictionary.h.

  50.    To add a new operation <op> on all existing implementations, what
  51.    you should do is:

  52.    * Add a new member <op> to struct dict_vector.

  53.    * If there is useful generic behavior <op>, define a static
  54.    function <op>_something_informative that implements that behavior.
  55.    (E.g. add_symbol_nonexpandable, free_obstack.)

  56.    * For every implementation <impl> that should have its own specific
  57.    behavior for <op>, define a static function <op>_<impl>
  58.    implementing it.

  59.    * Modify all existing dict_vector_<impl>'s to include the appropriate
  60.    member.

  61.    * Define a function dict_<op> that looks up <op> in the dict_vector
  62.    and calls the appropriate function.  Add a declaration for
  63.    dict_<op> to dictionary.h.  */

  64. /* An enum representing the various implementations of dictionaries.
  65.    Used only for debugging.  */

  66. enum dict_type
  67.   {
  68.     /* Symbols are stored in a fixed-size hash table.  */
  69.     DICT_HASHED,
  70.     /* Symbols are stored in an expandable hash table.  */
  71.     DICT_HASHED_EXPANDABLE,
  72.     /* Symbols are stored in a fixed-size array.  */
  73.     DICT_LINEAR,
  74.     /* Symbols are stored in an expandable array.  */
  75.     DICT_LINEAR_EXPANDABLE
  76.   };

  77. /* The virtual function table.  */

  78. struct dict_vector
  79. {
  80.   /* The type of the dictionary.  This is only here to make debugging
  81.      a bit easier; it's not actually used.  */
  82.   enum dict_type type;
  83.   /* The function to free a dictionary.  */
  84.   void (*free) (struct dictionary *dict);
  85.   /* Add a symbol to a dictionary, if possible.  */
  86.   void (*add_symbol) (struct dictionary *dict, struct symbol *sym);
  87.   /* Iterator functions.  */
  88.   struct symbol *(*iterator_first) (const struct dictionary *dict,
  89.                                     struct dict_iterator *iterator);
  90.   struct symbol *(*iterator_next) (struct dict_iterator *iterator);
  91.   /* Functions to iterate over symbols with a given name.  */
  92.   struct symbol *(*iter_match_first) (const struct dictionary *dict,
  93.                                       const char *name,
  94.                                       symbol_compare_ftype *equiv,
  95.                                       struct dict_iterator *iterator);
  96.   struct symbol *(*iter_match_next) (const char *name,
  97.                                      symbol_compare_ftype *equiv,
  98.                                      struct dict_iterator *iterator);
  99.   /* A size function, for maint print symtabs.  */
  100.   int (*size) (const struct dictionary *dict);
  101. };

  102. /* Now comes the structs used to store the data for different
  103.    implementations.  If two implementations have data in common, put
  104.    the common data at the top of their structs, ordered in the same
  105.    way.  */

  106. struct dictionary_hashed
  107. {
  108.   int nbuckets;
  109.   struct symbol **buckets;
  110. };

  111. struct dictionary_hashed_expandable
  112. {
  113.   /* How many buckets we currently have.  */
  114.   int nbuckets;
  115.   struct symbol **buckets;
  116.   /* How many syms we currently have; we need this so we will know
  117.      when to add more buckets.  */
  118.   int nsyms;
  119. };

  120. struct dictionary_linear
  121. {
  122.   int nsyms;
  123.   struct symbol **syms;
  124. };

  125. struct dictionary_linear_expandable
  126. {
  127.   /* How many symbols we currently have.  */
  128.   int nsyms;
  129.   struct symbol **syms;
  130.   /* How many symbols we can store before needing to reallocate.  */
  131.   int capacity;
  132. };

  133. /* And now, the star of our show.  */

  134. struct dictionary
  135. {
  136.   const struct dict_vector *vector;
  137.   union
  138.   {
  139.     struct dictionary_hashed hashed;
  140.     struct dictionary_hashed_expandable hashed_expandable;
  141.     struct dictionary_linear linear;
  142.     struct dictionary_linear_expandable linear_expandable;
  143.   }
  144.   data;
  145. };

  146. /* Accessor macros.  */

  147. #define DICT_VECTOR(d)                        (d)->vector

  148. /* These can be used for DICT_HASHED_EXPANDABLE, too.  */

  149. #define DICT_HASHED_NBUCKETS(d)                (d)->data.hashed.nbuckets
  150. #define DICT_HASHED_BUCKETS(d)                (d)->data.hashed.buckets
  151. #define DICT_HASHED_BUCKET(d,i)                DICT_HASHED_BUCKETS (d) [i]

  152. #define DICT_HASHED_EXPANDABLE_NSYMS(d)        (d)->data.hashed_expandable.nsyms

  153. /* These can be used for DICT_LINEAR_EXPANDABLEs, too.  */

  154. #define DICT_LINEAR_NSYMS(d)                (d)->data.linear.nsyms
  155. #define DICT_LINEAR_SYMS(d)                (d)->data.linear.syms
  156. #define DICT_LINEAR_SYM(d,i)                DICT_LINEAR_SYMS (d) [i]

  157. #define DICT_LINEAR_EXPANDABLE_CAPACITY(d) \
  158.                 (d)->data.linear_expandable.capacity

  159. /* The initial size of a DICT_*_EXPANDABLE dictionary.  */

  160. #define DICT_EXPANDABLE_INITIAL_CAPACITY 10

  161. /* This calculates the number of buckets we'll use in a hashtable,
  162.    given the number of symbols that it will contain.  */

  163. #define DICT_HASHTABLE_SIZE(n)        ((n)/5 + 1)

  164. /* Accessor macros for dict_iterators; they're here rather than
  165.    dictionary.h because code elsewhere should treat dict_iterators as
  166.    opaque.  */

  167. /* The dictionary that the iterator is associated to.  */
  168. #define DICT_ITERATOR_DICT(iter)                (iter)->dict
  169. /* For linear dictionaries, the index of the last symbol returned; for
  170.    hashed dictionaries, the bucket of the last symbol returned.  */
  171. #define DICT_ITERATOR_INDEX(iter)                (iter)->index
  172. /* For hashed dictionaries, this points to the last symbol returned;
  173.    otherwise, this is unused.  */
  174. #define DICT_ITERATOR_CURRENT(iter)                (iter)->current

  175. /* Declarations of functions for vectors.  */

  176. /* Functions that might work across a range of dictionary types.  */

  177. static void add_symbol_nonexpandable (struct dictionary *dict,
  178.                                       struct symbol *sym);

  179. static void free_obstack (struct dictionary *dict);

  180. /* Functions for DICT_HASHED and DICT_HASHED_EXPANDABLE
  181.    dictionaries.  */

  182. static struct symbol *iterator_first_hashed (const struct dictionary *dict,
  183.                                              struct dict_iterator *iterator);

  184. static struct symbol *iterator_next_hashed (struct dict_iterator *iterator);

  185. static struct symbol *iter_match_first_hashed (const struct dictionary *dict,
  186.                                                const char *name,
  187.                                                symbol_compare_ftype *compare,
  188.                                               struct dict_iterator *iterator);

  189. static struct symbol *iter_match_next_hashed (const char *name,
  190.                                               symbol_compare_ftype *compare,
  191.                                               struct dict_iterator *iterator);

  192. static unsigned int dict_hash (const char *string);

  193. /* Functions only for DICT_HASHED.  */

  194. static int size_hashed (const struct dictionary *dict);

  195. /* Functions only for DICT_HASHED_EXPANDABLE.  */

  196. static void free_hashed_expandable (struct dictionary *dict);

  197. static void add_symbol_hashed_expandable (struct dictionary *dict,
  198.                                           struct symbol *sym);

  199. static int size_hashed_expandable (const struct dictionary *dict);

  200. /* Functions for DICT_LINEAR and DICT_LINEAR_EXPANDABLE
  201.    dictionaries.  */

  202. static struct symbol *iterator_first_linear (const struct dictionary *dict,
  203.                                              struct dict_iterator *iterator);

  204. static struct symbol *iterator_next_linear (struct dict_iterator *iterator);

  205. static struct symbol *iter_match_first_linear (const struct dictionary *dict,
  206.                                                const char *name,
  207.                                                symbol_compare_ftype *compare,
  208.                                                struct dict_iterator *iterator);

  209. static struct symbol *iter_match_next_linear (const char *name,
  210.                                               symbol_compare_ftype *compare,
  211.                                               struct dict_iterator *iterator);

  212. static int size_linear (const struct dictionary *dict);

  213. /* Functions only for DICT_LINEAR_EXPANDABLE.  */

  214. static void free_linear_expandable (struct dictionary *dict);

  215. static void add_symbol_linear_expandable (struct dictionary *dict,
  216.                                           struct symbol *sym);

  217. /* Various vectors that we'll actually use.  */

  218. static const struct dict_vector dict_hashed_vector =
  219.   {
  220.     DICT_HASHED,                        /* type */
  221.     free_obstack,                        /* free */
  222.     add_symbol_nonexpandable,                /* add_symbol */
  223.     iterator_first_hashed,                /* iterator_first */
  224.     iterator_next_hashed,                /* iterator_next */
  225.     iter_match_first_hashed,                /* iter_name_first */
  226.     iter_match_next_hashed,                /* iter_name_next */
  227.     size_hashed,                        /* size */
  228.   };

  229. static const struct dict_vector dict_hashed_expandable_vector =
  230.   {
  231.     DICT_HASHED_EXPANDABLE,                /* type */
  232.     free_hashed_expandable,                /* free */
  233.     add_symbol_hashed_expandable,        /* add_symbol */
  234.     iterator_first_hashed,                /* iterator_first */
  235.     iterator_next_hashed,                /* iterator_next */
  236.     iter_match_first_hashed,                /* iter_name_first */
  237.     iter_match_next_hashed,                /* iter_name_next */
  238.     size_hashed_expandable,                /* size */
  239.   };

  240. static const struct dict_vector dict_linear_vector =
  241.   {
  242.     DICT_LINEAR,                        /* type */
  243.     free_obstack,                        /* free */
  244.     add_symbol_nonexpandable,                /* add_symbol */
  245.     iterator_first_linear,                /* iterator_first */
  246.     iterator_next_linear,                /* iterator_next */
  247.     iter_match_first_linear,                /* iter_name_first */
  248.     iter_match_next_linear,                /* iter_name_next */
  249.     size_linear,                        /* size */
  250.   };

  251. static const struct dict_vector dict_linear_expandable_vector =
  252.   {
  253.     DICT_LINEAR_EXPANDABLE,                /* type */
  254.     free_linear_expandable,                /* free */
  255.     add_symbol_linear_expandable,        /* add_symbol */
  256.     iterator_first_linear,                /* iterator_first */
  257.     iterator_next_linear,                /* iterator_next */
  258.     iter_match_first_linear,                /* iter_name_first */
  259.     iter_match_next_linear,                /* iter_name_next */
  260.     size_linear,                        /* size */
  261.   };

  262. /* Declarations of helper functions (i.e. ones that don't go into
  263.    vectors).  */

  264. static struct symbol *iterator_hashed_advance (struct dict_iterator *iter);

  265. static void insert_symbol_hashed (struct dictionary *dict,
  266.                                   struct symbol *sym);

  267. static void expand_hashtable (struct dictionary *dict);

  268. /* The creation functions.  */

  269. /* Create a dictionary implemented via a fixed-size hashtable.  All
  270.    memory it uses is allocated on OBSTACK; the environment is
  271.    initialized from SYMBOL_LIST.  */

  272. struct dictionary *
  273. dict_create_hashed (struct obstack *obstack,
  274.                     const struct pending *symbol_list)
  275. {
  276.   struct dictionary *retval;
  277.   int nsyms = 0, nbuckets, i;
  278.   struct symbol **buckets;
  279.   const struct pending *list_counter;

  280.   retval = obstack_alloc (obstack, sizeof (struct dictionary));
  281.   DICT_VECTOR (retval) = &dict_hashed_vector;

  282.   /* Calculate the number of symbols, and allocate space for them.  */
  283.   for (list_counter = symbol_list;
  284.        list_counter != NULL;
  285.        list_counter = list_counter->next)
  286.     {
  287.       nsyms += list_counter->nsyms;
  288.     }
  289.   nbuckets = DICT_HASHTABLE_SIZE (nsyms);
  290.   DICT_HASHED_NBUCKETS (retval) = nbuckets;
  291.   buckets = obstack_alloc (obstack, nbuckets * sizeof (struct symbol *));
  292.   memset (buckets, 0, nbuckets * sizeof (struct symbol *));
  293.   DICT_HASHED_BUCKETS (retval) = buckets;

  294.   /* Now fill the buckets.  */
  295.   for (list_counter = symbol_list;
  296.        list_counter != NULL;
  297.        list_counter = list_counter->next)
  298.     {
  299.       for (i = list_counter->nsyms - 1; i >= 0; --i)
  300.         {
  301.           insert_symbol_hashed (retval, list_counter->symbol[i]);
  302.         }
  303.     }

  304.   return retval;
  305. }

  306. /* Create a dictionary implemented via a hashtable that grows as
  307.    necessary.  The dictionary is initially empty; to add symbols to
  308.    it, call dict_add_symbol().  Call dict_free() when you're done with
  309.    it.  */

  310. extern struct dictionary *
  311. dict_create_hashed_expandable (void)
  312. {
  313.   struct dictionary *retval;

  314.   retval = xmalloc (sizeof (struct dictionary));
  315.   DICT_VECTOR (retval) = &dict_hashed_expandable_vector;
  316.   DICT_HASHED_NBUCKETS (retval) = DICT_EXPANDABLE_INITIAL_CAPACITY;
  317.   DICT_HASHED_BUCKETS (retval) = xcalloc (DICT_EXPANDABLE_INITIAL_CAPACITY,
  318.                                           sizeof (struct symbol *));
  319.   DICT_HASHED_EXPANDABLE_NSYMS (retval) = 0;

  320.   return retval;
  321. }

  322. /* Create a dictionary implemented via a fixed-size array.  All memory
  323.    it uses is allocated on OBSTACK; the environment is initialized
  324.    from the SYMBOL_LIST.  The symbols are ordered in the same order
  325.    that they're found in SYMBOL_LIST.  */

  326. struct dictionary *
  327. dict_create_linear (struct obstack *obstack,
  328.                     const struct pending *symbol_list)
  329. {
  330.   struct dictionary *retval;
  331.   int nsyms = 0, i, j;
  332.   struct symbol **syms;
  333.   const struct pending *list_counter;

  334.   retval = obstack_alloc (obstack, sizeof (struct dictionary));
  335.   DICT_VECTOR (retval) = &dict_linear_vector;

  336.   /* Calculate the number of symbols, and allocate space for them.  */
  337.   for (list_counter = symbol_list;
  338.        list_counter != NULL;
  339.        list_counter = list_counter->next)
  340.     {
  341.       nsyms += list_counter->nsyms;
  342.     }
  343.   DICT_LINEAR_NSYMS (retval) = nsyms;
  344.   syms = obstack_alloc (obstack, nsyms * sizeof (struct symbol *));
  345.   DICT_LINEAR_SYMS (retval) = syms;

  346.   /* Now fill in the symbols.  Start filling in from the back, so as
  347.      to preserve the original order of the symbols.  */
  348.   for (list_counter = symbol_list, j = nsyms - 1;
  349.        list_counter != NULL;
  350.        list_counter = list_counter->next)
  351.     {
  352.       for (i = list_counter->nsyms - 1;
  353.            i >= 0;
  354.            --i, --j)
  355.         {
  356.           syms[j] = list_counter->symbol[i];
  357.         }
  358.     }

  359.   return retval;
  360. }

  361. /* Create a dictionary implemented via an array that grows as
  362.    necessary.  The dictionary is initially empty; to add symbols to
  363.    it, call dict_add_symbol().  Call dict_free() when you're done with
  364.    it.  */

  365. struct dictionary *
  366. dict_create_linear_expandable (void)
  367. {
  368.   struct dictionary *retval;

  369.   retval = xmalloc (sizeof (struct dictionary));
  370.   DICT_VECTOR (retval) = &dict_linear_expandable_vector;
  371.   DICT_LINEAR_NSYMS (retval) = 0;
  372.   DICT_LINEAR_EXPANDABLE_CAPACITY (retval)
  373.     = DICT_EXPANDABLE_INITIAL_CAPACITY;
  374.   DICT_LINEAR_SYMS (retval)
  375.     = xmalloc (DICT_LINEAR_EXPANDABLE_CAPACITY (retval)
  376.                * sizeof (struct symbol *));

  377.   return retval;
  378. }

  379. /* The functions providing the dictionary interface.  */

  380. /* Free the memory used by a dictionary that's not on an obstack.  (If
  381.    any.)  */

  382. void
  383. dict_free (struct dictionary *dict)
  384. {
  385.   (DICT_VECTOR (dict))->free (dict);
  386. }

  387. /* Add SYM to DICT.  DICT had better be expandable.  */

  388. void
  389. dict_add_symbol (struct dictionary *dict, struct symbol *sym)
  390. {
  391.   (DICT_VECTOR (dict))->add_symbol (dict, sym);
  392. }

  393. /* Utility to add a list of symbols to a dictionary.
  394.    DICT must be an expandable dictionary.  */

  395. void
  396. dict_add_pending (struct dictionary *dict, const struct pending *symbol_list)
  397. {
  398.   const struct pending *list;
  399.   int i;

  400.   for (list = symbol_list; list != NULL; list = list->next)
  401.     {
  402.       for (i = 0; i < list->nsyms; ++i)
  403.         dict_add_symbol (dict, list->symbol[i]);
  404.     }
  405. }

  406. /* Initialize ITERATOR to point at the first symbol in DICT, and
  407.    return that first symbol, or NULL if DICT is empty.  */

  408. struct symbol *
  409. dict_iterator_first (const struct dictionary *dict,
  410.                      struct dict_iterator *iterator)
  411. {
  412.   return (DICT_VECTOR (dict))->iterator_first (dict, iterator);
  413. }

  414. /* Advance ITERATOR, and return the next symbol, or NULL if there are
  415.    no more symbols.  */

  416. struct symbol *
  417. dict_iterator_next (struct dict_iterator *iterator)
  418. {
  419.   return (DICT_VECTOR (DICT_ITERATOR_DICT (iterator)))
  420.     ->iterator_next (iterator);
  421. }

  422. struct symbol *
  423. dict_iter_name_first (const struct dictionary *dict,
  424.                       const char *name,
  425.                       struct dict_iterator *iterator)
  426. {
  427.   return dict_iter_match_first (dict, name, strcmp_iw, iterator);
  428. }

  429. struct symbol *
  430. dict_iter_name_next (const char *name, struct dict_iterator *iterator)
  431. {
  432.   return dict_iter_match_next (name, strcmp_iw, iterator);
  433. }

  434. struct symbol *
  435. dict_iter_match_first (const struct dictionary *dict,
  436.                        const char *name, symbol_compare_ftype *compare,
  437.                        struct dict_iterator *iterator)
  438. {
  439.   return (DICT_VECTOR (dict))->iter_match_first (dict, name,
  440.                                                  compare, iterator);
  441. }

  442. struct symbol *
  443. dict_iter_match_next (const char *name, symbol_compare_ftype *compare,
  444.                       struct dict_iterator *iterator)
  445. {
  446.   return (DICT_VECTOR (DICT_ITERATOR_DICT (iterator)))
  447.     ->iter_match_next (name, compare, iterator);
  448. }

  449. int
  450. dict_size (const struct dictionary *dict)
  451. {
  452.   return (DICT_VECTOR (dict))->size (dict);
  453. }

  454. /* Now come functions (well, one function, currently) that are
  455.    implemented generically by means of the vtable.  Typically, they're
  456.    rarely used.  */

  457. /* Test to see if DICT is empty.  */

  458. int
  459. dict_empty (struct dictionary *dict)
  460. {
  461.   struct dict_iterator iter;

  462.   return (dict_iterator_first (dict, &iter) == NULL);
  463. }


  464. /* The functions implementing the dictionary interface.  */

  465. /* Generic functions, where appropriate.  */

  466. static void
  467. free_obstack (struct dictionary *dict)
  468. {
  469.   /* Do nothing!  */
  470. }

  471. static void
  472. add_symbol_nonexpandable (struct dictionary *dict, struct symbol *sym)
  473. {
  474.   internal_error (__FILE__, __LINE__,
  475.                   _("dict_add_symbol: non-expandable dictionary"));
  476. }

  477. /* Functions for DICT_HASHED and DICT_HASHED_EXPANDABLE.  */

  478. static struct symbol *
  479. iterator_first_hashed (const struct dictionary *dict,
  480.                        struct dict_iterator *iterator)
  481. {
  482.   DICT_ITERATOR_DICT (iterator) = dict;
  483.   DICT_ITERATOR_INDEX (iterator) = -1;
  484.   return iterator_hashed_advance (iterator);
  485. }

  486. static struct symbol *
  487. iterator_next_hashed (struct dict_iterator *iterator)
  488. {
  489.   struct symbol *next;

  490.   next = DICT_ITERATOR_CURRENT (iterator)->hash_next;

  491.   if (next == NULL)
  492.     return iterator_hashed_advance (iterator);
  493.   else
  494.     {
  495.       DICT_ITERATOR_CURRENT (iterator) = next;
  496.       return next;
  497.     }
  498. }

  499. static struct symbol *
  500. iterator_hashed_advance (struct dict_iterator *iterator)
  501. {
  502.   const struct dictionary *dict = DICT_ITERATOR_DICT (iterator);
  503.   int nbuckets = DICT_HASHED_NBUCKETS (dict);
  504.   int i;

  505.   for (i = DICT_ITERATOR_INDEX (iterator) + 1; i < nbuckets; ++i)
  506.     {
  507.       struct symbol *sym = DICT_HASHED_BUCKET (dict, i);

  508.       if (sym != NULL)
  509.         {
  510.           DICT_ITERATOR_INDEX (iterator) = i;
  511.           DICT_ITERATOR_CURRENT (iterator) = sym;
  512.           return sym;
  513.         }
  514.     }

  515.   return NULL;
  516. }

  517. static struct symbol *
  518. iter_match_first_hashed (const struct dictionary *dict, const char *name,
  519.                          symbol_compare_ftype *compare,
  520.                          struct dict_iterator *iterator)
  521. {
  522.   unsigned int hash_index = dict_hash (name) % DICT_HASHED_NBUCKETS (dict);
  523.   struct symbol *sym;

  524.   DICT_ITERATOR_DICT (iterator) = dict;

  525.   /* Loop through the symbols in the given bucket, breaking when SYM
  526.      first matches.  If SYM never matches, it will be set to NULL;
  527.      either way, we have the right return value.  */

  528.   for (sym = DICT_HASHED_BUCKET (dict, hash_index);
  529.        sym != NULL;
  530.        sym = sym->hash_next)
  531.     {
  532.       /* Warning: the order of arguments to compare matters!  */
  533.       if (compare (SYMBOL_SEARCH_NAME (sym), name) == 0)
  534.         {
  535.           break;
  536.         }

  537.     }

  538.   DICT_ITERATOR_CURRENT (iterator) = sym;
  539.   return sym;
  540. }

  541. static struct symbol *
  542. iter_match_next_hashed (const char *name, symbol_compare_ftype *compare,
  543.                         struct dict_iterator *iterator)
  544. {
  545.   struct symbol *next;

  546.   for (next = DICT_ITERATOR_CURRENT (iterator)->hash_next;
  547.        next != NULL;
  548.        next = next->hash_next)
  549.     {
  550.       if (compare (SYMBOL_SEARCH_NAME (next), name) == 0)
  551.         break;
  552.     }

  553.   DICT_ITERATOR_CURRENT (iterator) = next;

  554.   return next;
  555. }

  556. /* Insert SYM into DICT.  */

  557. static void
  558. insert_symbol_hashed (struct dictionary *dict,
  559.                       struct symbol *sym)
  560. {
  561.   unsigned int hash_index;
  562.   struct symbol **buckets = DICT_HASHED_BUCKETS (dict);

  563.   hash_index =
  564.     dict_hash (SYMBOL_SEARCH_NAME (sym)) % DICT_HASHED_NBUCKETS (dict);
  565.   sym->hash_next = buckets[hash_index];
  566.   buckets[hash_index] = sym;
  567. }

  568. static int
  569. size_hashed (const struct dictionary *dict)
  570. {
  571.   return DICT_HASHED_NBUCKETS (dict);
  572. }

  573. /* Functions only for DICT_HASHED_EXPANDABLE.  */

  574. static void
  575. free_hashed_expandable (struct dictionary *dict)
  576. {
  577.   xfree (DICT_HASHED_BUCKETS (dict));
  578.   xfree (dict);
  579. }

  580. static void
  581. add_symbol_hashed_expandable (struct dictionary *dict,
  582.                               struct symbol *sym)
  583. {
  584.   int nsyms = ++DICT_HASHED_EXPANDABLE_NSYMS (dict);

  585.   if (DICT_HASHTABLE_SIZE (nsyms) > DICT_HASHED_NBUCKETS (dict))
  586.     expand_hashtable (dict);

  587.   insert_symbol_hashed (dict, sym);
  588.   DICT_HASHED_EXPANDABLE_NSYMS (dict) = nsyms;
  589. }

  590. static int
  591. size_hashed_expandable (const struct dictionary *dict)
  592. {
  593.   return DICT_HASHED_EXPANDABLE_NSYMS (dict);
  594. }

  595. static void
  596. expand_hashtable (struct dictionary *dict)
  597. {
  598.   int old_nbuckets = DICT_HASHED_NBUCKETS (dict);
  599.   struct symbol **old_buckets = DICT_HASHED_BUCKETS (dict);
  600.   int new_nbuckets = 2*old_nbuckets + 1;
  601.   struct symbol **new_buckets = xcalloc (new_nbuckets,
  602.                                          sizeof (struct symbol *));
  603.   int i;

  604.   DICT_HASHED_NBUCKETS (dict) = new_nbuckets;
  605.   DICT_HASHED_BUCKETS (dict) = new_buckets;

  606.   for (i = 0; i < old_nbuckets; ++i)
  607.     {
  608.       struct symbol *sym, *next_sym;

  609.       sym = old_buckets[i];
  610.       if (sym != NULL)
  611.         {
  612.           for (next_sym = sym->hash_next;
  613.                next_sym != NULL;
  614.                next_sym = sym->hash_next)
  615.             {
  616.               insert_symbol_hashed (dict, sym);
  617.               sym = next_sym;
  618.             }

  619.           insert_symbol_hashed (dict, sym);
  620.         }
  621.     }

  622.   xfree (old_buckets);
  623. }

  624. /* Produce an unsigned hash value from STRING0 that is consistent
  625.    with strcmp_iw, strcmp, and, at least on Ada symbols, wild_match.
  626.    That is, two identifiers equivalent according to any of those three
  627.    comparison operators hash to the same value.  */

  628. static unsigned int
  629. dict_hash (const char *string0)
  630. {
  631.   /* The Ada-encoded version of a name P1.P2...Pn has either the form
  632.      P1__P2__...Pn<suffix> or _ada_P1__P2__...Pn<suffix> (where the Pi
  633.      are lower-cased identifiers).  The <suffix> (which can be empty)
  634.      encodes additional information about the denoted entity.  This
  635.      routine hashes such names to msymbol_hash_iw(Pn).  It actually
  636.      does this for a superset of both valid Pi and of <suffix>, but
  637.      in other cases it simply returns msymbol_hash_iw(STRING0).  */

  638.   const char *string;
  639.   unsigned int hash;

  640.   string = string0;
  641.   if (*string == '_')
  642.     {
  643.       if (strncmp (string, "_ada_", 5) == 0)
  644.         string += 5;
  645.       else
  646.         return msymbol_hash_iw (string0);
  647.     }

  648.   hash = 0;
  649.   while (*string)
  650.     {
  651.       /* Ignore "TKB" suffixes.

  652.          These are used by Ada for subprograms implementing a task body.
  653.          For instance for a task T inside package Pck, the name of the
  654.          subprogram implementing T's body is `pck__tTKB'.  We need to
  655.          ignore the "TKB" suffix because searches for this task body
  656.          subprogram are going to be performed using `pck__t' (the encoded
  657.          version of the natural name `pck.t').  */
  658.       if (strcmp (string, "TKB") == 0)
  659.         return hash;

  660.       switch (*string)
  661.         {
  662.         case '$':
  663.         case '.':
  664.         case 'X':
  665.           if (string0 == string)
  666.             return msymbol_hash_iw (string0);
  667.           else
  668.             return hash;
  669.         case ' ':
  670.         case '(':
  671.           return msymbol_hash_iw (string0);
  672.         case '_':
  673.           if (string[1] == '_' && string != string0)
  674.             {
  675.               int c = string[2];

  676.               if ((c < 'a' || c > 'z') && c != 'O')
  677.                 return hash;
  678.               hash = 0;
  679.               string += 2;
  680.               break;
  681.             }
  682.           /* FALL THROUGH */
  683.         default:
  684.           hash = SYMBOL_HASH_NEXT (hash, *string);
  685.           string += 1;
  686.           break;
  687.         }
  688.     }
  689.   return hash;
  690. }

  691. /* Functions for DICT_LINEAR and DICT_LINEAR_EXPANDABLE.  */

  692. static struct symbol *
  693. iterator_first_linear (const struct dictionary *dict,
  694.                        struct dict_iterator *iterator)
  695. {
  696.   DICT_ITERATOR_DICT (iterator) = dict;
  697.   DICT_ITERATOR_INDEX (iterator) = 0;
  698.   return DICT_LINEAR_NSYMS (dict) ? DICT_LINEAR_SYM (dict, 0) : NULL;
  699. }

  700. static struct symbol *
  701. iterator_next_linear (struct dict_iterator *iterator)
  702. {
  703.   const struct dictionary *dict = DICT_ITERATOR_DICT (iterator);

  704.   if (++DICT_ITERATOR_INDEX (iterator) >= DICT_LINEAR_NSYMS (dict))
  705.     return NULL;
  706.   else
  707.     return DICT_LINEAR_SYM (dict, DICT_ITERATOR_INDEX (iterator));
  708. }

  709. static struct symbol *
  710. iter_match_first_linear (const struct dictionary *dict,
  711.                          const char *name, symbol_compare_ftype *compare,
  712.                          struct dict_iterator *iterator)
  713. {
  714.   DICT_ITERATOR_DICT (iterator) = dict;
  715.   DICT_ITERATOR_INDEX (iterator) = -1;

  716.   return iter_match_next_linear (name, compare, iterator);
  717. }

  718. static struct symbol *
  719. iter_match_next_linear (const char *name, symbol_compare_ftype *compare,
  720.                         struct dict_iterator *iterator)
  721. {
  722.   const struct dictionary *dict = DICT_ITERATOR_DICT (iterator);
  723.   int i, nsyms = DICT_LINEAR_NSYMS (dict);
  724.   struct symbol *sym, *retval = NULL;

  725.   for (i = DICT_ITERATOR_INDEX (iterator) + 1; i < nsyms; ++i)
  726.     {
  727.       sym = DICT_LINEAR_SYM (dict, i);
  728.       if (compare (SYMBOL_SEARCH_NAME (sym), name) == 0)
  729.         {
  730.           retval = sym;
  731.           break;
  732.         }
  733.     }

  734.   DICT_ITERATOR_INDEX (iterator) = i;

  735.   return retval;
  736. }

  737. static int
  738. size_linear (const struct dictionary *dict)
  739. {
  740.   return DICT_LINEAR_NSYMS (dict);
  741. }

  742. /* Functions only for DICT_LINEAR_EXPANDABLE.  */

  743. static void
  744. free_linear_expandable (struct dictionary *dict)
  745. {
  746.   xfree (DICT_LINEAR_SYMS (dict));
  747.   xfree (dict);
  748. }


  749. static void
  750. add_symbol_linear_expandable (struct dictionary *dict,
  751.                               struct symbol *sym)
  752. {
  753.   int nsyms = ++DICT_LINEAR_NSYMS (dict);

  754.   /* Do we have enough room?  If not, grow it.  */
  755.   if (nsyms > DICT_LINEAR_EXPANDABLE_CAPACITY (dict))
  756.     {
  757.       DICT_LINEAR_EXPANDABLE_CAPACITY (dict) *= 2;
  758.       DICT_LINEAR_SYMS (dict)
  759.         = xrealloc (DICT_LINEAR_SYMS (dict),
  760.                     DICT_LINEAR_EXPANDABLE_CAPACITY (dict)
  761.                     * sizeof (struct symbol *));
  762.     }

  763.   DICT_LINEAR_SYM (dict, nsyms - 1) = sym;
  764. }